blob: e76b5ff26ef8ae4d1a8b251e10736c99549cf36a [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 Salomon5ec9def2016-12-20 15:34:05 -050013#include "GrColor.h"
Robert Phillipse78b7252017-04-06 07:59:41 -040014#include "GrContextPriv.h"
robertphillips77a2e522015-10-17 07:43:27 -070015#include "GrDrawingManager.h"
csmartdalton02fa32c2016-08-19 13:29:27 -070016#include "GrFixedClip.h"
robertphillips714712b2016-08-04 06:20:45 -070017#include "GrGpuResourcePriv.h"
robertphillipsea461502015-05-26 11:38:03 -070018#include "GrPathRenderer.h"
robertphillips2334fb62015-06-17 05:43:33 -070019#include "GrRenderTarget.h"
Brian Salomon5ec9def2016-12-20 15:34:05 -050020#include "GrRenderTargetContextPriv.h"
bsalomon473addf2015-10-02 07:49:05 -070021#include "GrResourceProvider.h"
Brian Salomon467921e2017-03-06 16:17:12 -050022#include "GrStencilAttachment.h"
Brian Salomondcbb9d92017-07-19 10:53:20 -040023#include "GrTracing.h"
Jim Van Verth1af03d42017-07-31 09:34:58 -040024#include "SkDrawShadowInfo.h"
Brian Salomon467921e2017-03-06 16:17:12 -050025#include "SkLatticeIter.h"
26#include "SkMatrixPriv.h"
Jim Van Verth34d6e4b2017-06-09 11:09:03 -040027#include "SkShadowUtils.h"
robertphillips2d70dcb2015-10-06 07:38:23 -070028#include "SkSurfacePriv.h"
Brian Salomon467921e2017-03-06 16:17:12 -050029#include "effects/GrRRectEffect.h"
30#include "instanced/InstancedRendering.h"
Brian Salomon89527432016-12-16 09:52:16 -050031#include "ops/GrClearOp.h"
Robert Phillipsb9a02a12017-04-06 11:08:40 -040032#include "ops/GrClearStencilClipOp.h"
Robert Phillips65a88fa2017-08-08 08:36:22 -040033#include "ops/GrDebugMarkerOp.h"
Robert Phillips54190c42017-08-09 20:09:38 +000034#include "ops/GrDiscardOp.h"
Brian Salomon0f353322017-05-03 20:58:59 +000035#include "ops/GrDrawAtlasOp.h"
Brian Salomon6d4b65e2017-05-03 17:06:09 -040036#include "ops/GrDrawOp.h"
Brian Salomon89527432016-12-16 09:52:16 -050037#include "ops/GrDrawVerticesOp.h"
38#include "ops/GrLatticeOp.h"
39#include "ops/GrOp.h"
40#include "ops/GrOvalOpFactory.h"
41#include "ops/GrRectOpFactory.h"
42#include "ops/GrRegionOp.h"
Greg Daniela5cb7812017-06-16 09:45:32 -040043#include "ops/GrSemaphoreOp.h"
Brian Salomon89527432016-12-16 09:52:16 -050044#include "ops/GrShadowRRectOp.h"
Brian Salomon467921e2017-03-06 16:17:12 -050045#include "ops/GrStencilPathOp.h"
joshualitte8042922015-12-11 06:11:21 -080046#include "text/GrAtlasTextContext.h"
47#include "text/GrStencilAndCoverTextContext.h"
joshualittbc907352016-01-13 06:45:40 -080048
Robert Phillips72152832017-01-25 17:31:35 -050049#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this->drawingManager()->getContext())
joshualitt1de610a2016-01-06 08:26:09 -080050#define ASSERT_SINGLE_OWNER \
Robert Phillipsa90aa2b2017-04-10 08:19:26 -040051 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(this->singleOwner());)
robertphillips391395d2016-03-02 09:26:36 -080052#define ASSERT_SINGLE_OWNER_PRIV \
Robert Phillipsa90aa2b2017-04-10 08:19:26 -040053 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fRenderTargetContext->singleOwner());)
Robert Phillips72152832017-01-25 17:31:35 -050054#define RETURN_IF_ABANDONED if (this->drawingManager()->wasAbandoned()) { return; }
55#define RETURN_IF_ABANDONED_PRIV if (fRenderTargetContext->drawingManager()->wasAbandoned()) { return; }
56#define RETURN_FALSE_IF_ABANDONED if (this->drawingManager()->wasAbandoned()) { return false; }
57#define RETURN_FALSE_IF_ABANDONED_PRIV if (fRenderTargetContext->drawingManager()->wasAbandoned()) { return false; }
58#define RETURN_NULL_IF_ABANDONED if (this->drawingManager()->wasAbandoned()) { return nullptr; }
robertphillipsea461502015-05-26 11:38:03 -070059
Brian Salomone225b562017-06-14 13:00:03 -040060//////////////////////////////////////////////////////////////////////////////
61
62GrAAType GrChooseAAType(GrAA aa, GrFSAAType fsaaType, GrAllowMixedSamples allowMixedSamples,
63 const GrCaps& caps) {
64 if (GrAA::kNo == aa) {
65 // On some devices we cannot disable MSAA if it is enabled so we make the AA type reflect
66 // that.
67 if (fsaaType == GrFSAAType::kUnifiedMSAA && !caps.multisampleDisableSupport()) {
68 return GrAAType::kMSAA;
69 }
70 return GrAAType::kNone;
71 }
72 switch (fsaaType) {
73 case GrFSAAType::kNone:
74 return GrAAType::kCoverage;
75 case GrFSAAType::kUnifiedMSAA:
76 return GrAAType::kMSAA;
77 case GrFSAAType::kMixedSamples:
78 return GrAllowMixedSamples::kYes == allowMixedSamples ? GrAAType::kMixedSamples
79 : GrAAType::kCoverage;
80 }
81 SkFAIL("Unexpected fsaa type");
82 return GrAAType::kNone;
83}
84
85//////////////////////////////////////////////////////////////////////////////
86
robertphillipsea461502015-05-26 11:38:03 -070087class AutoCheckFlush {
88public:
halcanary9d524f22016-03-29 09:03:52 -070089 AutoCheckFlush(GrDrawingManager* drawingManager) : fDrawingManager(drawingManager) {
robertphillips77a2e522015-10-17 07:43:27 -070090 SkASSERT(fDrawingManager);
91 }
bsalomonb77a9072016-09-07 10:02:04 -070092 ~AutoCheckFlush() { fDrawingManager->flushIfNecessary(); }
robertphillipsea461502015-05-26 11:38:03 -070093
94private:
robertphillips77a2e522015-10-17 07:43:27 -070095 GrDrawingManager* fDrawingManager;
robertphillipsea461502015-05-26 11:38:03 -070096};
97
Brian Osman11052242016-10-27 14:47:55 -040098bool GrRenderTargetContext::wasAbandoned() const {
Robert Phillips72152832017-01-25 17:31:35 -050099 return this->drawingManager()->wasAbandoned();
robertphillips7761d612016-05-16 09:14:53 -0700100}
101
Robert Phillipsf2361d22016-10-25 14:20:06 -0400102// In MDB mode the reffing of the 'getLastOpList' call's result allows in-progress
Brian Osman11052242016-10-27 14:47:55 -0400103// GrOpLists to be picked up and added to by renderTargetContexts lower in the call
Robert Phillipsf2361d22016-10-25 14:20:06 -0400104// stack. When this occurs with a closed GrOpList, a new one will be allocated
Brian Osman11052242016-10-27 14:47:55 -0400105// when the renderTargetContext attempts to use it (via getOpList).
106GrRenderTargetContext::GrRenderTargetContext(GrContext* context,
107 GrDrawingManager* drawingMgr,
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400108 sk_sp<GrRenderTargetProxy> rtp,
Brian Osman11052242016-10-27 14:47:55 -0400109 sk_sp<SkColorSpace> colorSpace,
110 const SkSurfaceProps* surfaceProps,
111 GrAuditTrail* auditTrail,
Robert Phillips941d1442017-06-14 16:37:02 -0400112 GrSingleOwner* singleOwner,
113 bool managedOpList)
Robert Phillips72152832017-01-25 17:31:35 -0500114 : GrSurfaceContext(context, drawingMgr, std::move(colorSpace), auditTrail, singleOwner)
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400115 , fRenderTargetProxy(std::move(rtp))
Robert Phillipsdc83b892017-04-13 12:23:54 -0400116 , fOpList(sk_ref_sp(fRenderTargetProxy->getLastRenderTargetOpList()))
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400117 , fInstancedPipelineInfo(fRenderTargetProxy.get())
brianosman5a7ae7e2016-09-12 12:07:25 -0700118 , fColorXformFromSRGB(nullptr)
Robert Phillips941d1442017-06-14 16:37:02 -0400119 , fSurfaceProps(SkSurfacePropsCopyOrDefault(surfaceProps))
120 , fManagedOpList(managedOpList) {
brianosman5a7ae7e2016-09-12 12:07:25 -0700121 if (fColorSpace) {
122 // sRGB sources are very common (SkColor, etc...), so we cache that gamut transformation
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500123 auto srgbColorSpace = SkColorSpace::MakeSRGB();
msarettc71a9b72016-09-16 11:01:27 -0700124 fColorXformFromSRGB = GrColorSpaceXform::Make(srgbColorSpace.get(), fColorSpace.get());
brianosman5a7ae7e2016-09-12 12:07:25 -0700125 }
Robert Phillipsd9914862017-06-14 15:16:59 -0400126
127 // MDB TODO: to ensure all resources still get allocated in the correct order in the hybrid
128 // world we need to get the correct opList here so that it, in turn, can grab and hold
129 // its rendertarget.
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400130 this->getRTOpList();
robertphillips2e1e51f2015-10-15 08:01:48 -0700131 SkDEBUGCODE(this->validate();)
robertphillipsea461502015-05-26 11:38:03 -0700132}
133
robertphillips2e1e51f2015-10-15 08:01:48 -0700134#ifdef SK_DEBUG
Brian Osman11052242016-10-27 14:47:55 -0400135void GrRenderTargetContext::validate() const {
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400136 SkASSERT(fRenderTargetProxy);
137 fRenderTargetProxy->validate(fContext);
robertphillipsa106c622015-10-16 09:07:06 -0700138
Robert Phillipsf2361d22016-10-25 14:20:06 -0400139 if (fOpList && !fOpList->isClosed()) {
Robert Phillipsdc83b892017-04-13 12:23:54 -0400140 SkASSERT(fRenderTargetProxy->getLastOpList() == fOpList.get());
robertphillipsa106c622015-10-16 09:07:06 -0700141 }
robertphillips2e1e51f2015-10-15 08:01:48 -0700142}
143#endif
144
Brian Osman11052242016-10-27 14:47:55 -0400145GrRenderTargetContext::~GrRenderTargetContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800146 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700147}
148
Robert Phillipsf200a902017-01-30 13:27:37 -0500149GrTextureProxy* GrRenderTargetContext::asTextureProxy() {
Robert Phillipseaa86252016-11-08 13:49:39 +0000150 return fRenderTargetProxy->asTextureProxy();
151}
152
Robert Phillipsf200a902017-01-30 13:27:37 -0500153sk_sp<GrTextureProxy> GrRenderTargetContext::asTextureProxyRef() {
154 return sk_ref_sp(fRenderTargetProxy->asTextureProxy());
155}
156
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400157GrRenderTargetOpList* GrRenderTargetContext::getRTOpList() {
joshualitt1de610a2016-01-06 08:26:09 -0800158 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700159 SkDEBUGCODE(this->validate();)
160
Robert Phillipsf2361d22016-10-25 14:20:06 -0400161 if (!fOpList || fOpList->isClosed()) {
Robert Phillips941d1442017-06-14 16:37:02 -0400162 fOpList = this->drawingManager()->newRTOpList(fRenderTargetProxy.get(), fManagedOpList);
robertphillipsa106c622015-10-16 09:07:06 -0700163 }
164
Robert Phillipsdc83b892017-04-13 12:23:54 -0400165 return fOpList.get();
robertphillipsa106c622015-10-16 09:07:06 -0700166}
167
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400168GrOpList* GrRenderTargetContext::getOpList() {
169 return this->getRTOpList();
robertphillipsea461502015-05-26 11:38:03 -0700170}
171
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500172void GrRenderTargetContext::drawText(const GrClip& clip, const SkPaint& skPaint,
Brian Salomon82f44312017-01-11 13:42:54 -0500173 const SkMatrix& viewMatrix, const char text[],
174 size_t byteLength, SkScalar x, SkScalar y,
175 const SkIRect& clipBounds) {
joshualitt1de610a2016-01-06 08:26:09 -0800176 ASSERT_SINGLE_OWNER
robertphillips2d70dcb2015-10-06 07:38:23 -0700177 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700178 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400179 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawText", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -0700180
Robert Phillips72152832017-01-25 17:31:35 -0500181 GrAtlasTextContext* atlasTextContext = this->drawingManager()->getAtlasTextContext();
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500182 atlasTextContext->drawText(fContext, this, clip, skPaint, viewMatrix, fSurfaceProps, text,
183 byteLength, x, y, clipBounds);
robertphillips2334fb62015-06-17 05:43:33 -0700184}
robertphillipscaef3452015-11-11 13:18:11 -0800185
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500186void GrRenderTargetContext::drawPosText(const GrClip& clip, const SkPaint& paint,
187 const SkMatrix& viewMatrix, const char text[],
188 size_t byteLength, const SkScalar pos[],
Brian Salomon82f44312017-01-11 13:42:54 -0500189 int scalarsPerPosition, const SkPoint& offset,
190 const SkIRect& clipBounds) {
joshualitt1de610a2016-01-06 08:26:09 -0800191 ASSERT_SINGLE_OWNER
robertphillips2d70dcb2015-10-06 07:38:23 -0700192 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700193 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400194 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawPosText", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -0700195
Robert Phillips72152832017-01-25 17:31:35 -0500196 GrAtlasTextContext* atlasTextContext = this->drawingManager()->getAtlasTextContext();
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500197 atlasTextContext->drawPosText(fContext, this, clip, paint, viewMatrix, fSurfaceProps, text,
198 byteLength, pos, scalarsPerPosition, offset, clipBounds);
robertphillips2334fb62015-06-17 05:43:33 -0700199}
robertphillipscaef3452015-11-11 13:18:11 -0800200
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500201void GrRenderTargetContext::drawTextBlob(const GrClip& clip, const SkPaint& paint,
Brian Osman11052242016-10-27 14:47:55 -0400202 const SkMatrix& viewMatrix, const SkTextBlob* blob,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500203 SkScalar x, SkScalar y, SkDrawFilter* filter,
204 const SkIRect& clipBounds) {
joshualitt1de610a2016-01-06 08:26:09 -0800205 ASSERT_SINGLE_OWNER
robertphillips2d70dcb2015-10-06 07:38:23 -0700206 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700207 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400208 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTextBlob", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -0700209
Robert Phillips72152832017-01-25 17:31:35 -0500210 GrAtlasTextContext* atlasTextContext = this->drawingManager()->getAtlasTextContext();
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500211 atlasTextContext->drawTextBlob(fContext, this, clip, paint, viewMatrix, fSurfaceProps, blob, x,
212 y, filter, clipBounds);
robertphillipsea461502015-05-26 11:38:03 -0700213}
214
Brian Osman11052242016-10-27 14:47:55 -0400215void GrRenderTargetContext::discard() {
joshualitt1de610a2016-01-06 08:26:09 -0800216 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700217 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700218 SkDEBUGCODE(this->validate();)
Robert Phillips54190c42017-08-09 20:09:38 +0000219 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "discard", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700220
Robert Phillips72152832017-01-25 17:31:35 -0500221 AutoCheckFlush acf(this->drawingManager());
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400222
Robert Phillips54190c42017-08-09 20:09:38 +0000223 // Currently this just inserts a discard op. However, once in MDB this can remove all the
224 // previously recorded ops and change the load op to discard.
225 if (this->caps()->discardRenderTargetSupport()) {
226 std::unique_ptr<GrOp> op(GrDiscardOp::Make(fRenderTargetProxy.get()));
227 if (!op) {
228 return;
Robert Phillipse4407212017-04-13 10:13:16 -0400229 }
Robert Phillips54190c42017-08-09 20:09:38 +0000230 this->getRTOpList()->addOp(std::move(op), *this->caps());
Robert Phillipse4407212017-04-13 10:13:16 -0400231 }
robertphillipsea461502015-05-26 11:38:03 -0700232}
233
Brian Osman11052242016-10-27 14:47:55 -0400234void GrRenderTargetContext::clear(const SkIRect* rect,
235 const GrColor color,
236 bool canIgnoreRect) {
joshualitt1de610a2016-01-06 08:26:09 -0800237 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700238 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700239 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400240 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "clear", fContext);
robertphillipsea461502015-05-26 11:38:03 -0700241
Robert Phillips72152832017-01-25 17:31:35 -0500242 AutoCheckFlush acf(this->drawingManager());
csmartdalton29df7602016-08-31 11:55:52 -0700243 this->internalClear(rect ? GrFixedClip(*rect) : GrFixedClip::Disabled(), color, canIgnoreRect);
244}
robertphillips9199a9f2016-07-13 07:48:43 -0700245
Robert Phillips784b7bf2016-12-09 13:35:02 -0500246void GrRenderTargetContextPriv::absClear(const SkIRect* clearRect, const GrColor color) {
247 ASSERT_SINGLE_OWNER_PRIV
248 RETURN_IF_ABANDONED_PRIV
249 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400250 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "absClear",
251 fRenderTargetContext->fContext);
Robert Phillips784b7bf2016-12-09 13:35:02 -0500252
Robert Phillips72152832017-01-25 17:31:35 -0500253 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500254
Brian Salomonbb5711a2017-05-17 13:49:59 -0400255 SkIRect rtRect = SkIRect::MakeWH(fRenderTargetContext->fRenderTargetProxy->worstCaseWidth(),
256 fRenderTargetContext->fRenderTargetProxy->worstCaseHeight());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500257
258 if (clearRect) {
259 if (clearRect->contains(rtRect)) {
260 clearRect = nullptr; // full screen
261 } else {
262 if (!rtRect.intersect(*clearRect)) {
263 return;
264 }
265 }
266 }
267
268 // TODO: in a post-MDB world this should be handled at the OpList level.
269 // An op-list that is initially cleared and has no other ops should receive an
270 // extra draw.
271 if (fRenderTargetContext->fContext->caps()->useDrawInsteadOfClear()) {
272 // This works around a driver bug with clear by drawing a rect instead.
273 // The driver will ignore a clear if it is the only thing rendered to a
274 // target before the target is read.
275 GrPaint paint;
276 paint.setColor4f(GrColor4f::FromGrColor(color));
Brian Salomona1633922017-01-09 11:46:10 -0500277 paint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
Robert Phillips784b7bf2016-12-09 13:35:02 -0500278
279 // We don't call drawRect() here to avoid the cropping to the, possibly smaller,
280 // RenderTargetProxy bounds
Brian Salomonbaaf4392017-06-15 09:59:23 -0400281 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeNonAAFill(
282 std::move(paint), SkMatrix::I(), SkRect::Make(rtRect), GrAAType::kNone);
283 fRenderTargetContext->addDrawOp(GrNoClip(), std::move(op));
Robert Phillips784b7bf2016-12-09 13:35:02 -0500284 } else {
Robert Phillips784b7bf2016-12-09 13:35:02 -0500285 // This path doesn't handle coalescing of full screen clears b.c. it
286 // has to clear the entire render target - not just the content area.
287 // It could be done but will take more finagling.
Robert Phillips5efd5ea2017-05-30 13:47:32 -0400288 std::unique_ptr<GrOp> op(GrClearOp::Make(rtRect, color, !clearRect));
Brian Salomonfc527d22016-12-14 21:07:01 -0500289 if (!op) {
Robert Phillips784b7bf2016-12-09 13:35:02 -0500290 return;
291 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400292 fRenderTargetContext->getRTOpList()->addOp(std::move(op), *fRenderTargetContext->caps());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500293 }
294}
295
Brian Osman11052242016-10-27 14:47:55 -0400296void GrRenderTargetContextPriv::clear(const GrFixedClip& clip,
297 const GrColor color,
298 bool canIgnoreClip) {
csmartdalton29df7602016-08-31 11:55:52 -0700299 ASSERT_SINGLE_OWNER_PRIV
300 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400301 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400302 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "clear",
303 fRenderTargetContext->fContext);
csmartdalton29df7602016-08-31 11:55:52 -0700304
Robert Phillips72152832017-01-25 17:31:35 -0500305 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Brian Osman11052242016-10-27 14:47:55 -0400306 fRenderTargetContext->internalClear(clip, color, canIgnoreClip);
csmartdalton29df7602016-08-31 11:55:52 -0700307}
308
Brian Osman11052242016-10-27 14:47:55 -0400309void GrRenderTargetContext::internalClear(const GrFixedClip& clip,
310 const GrColor color,
311 bool canIgnoreClip) {
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700312 bool isFull = false;
313 if (!clip.hasWindowRectangles()) {
314 isFull = !clip.scissorEnabled() ||
315 (canIgnoreClip && fContext->caps()->fullClearIsFree()) ||
316 clip.scissorRect().contains(SkIRect::MakeWH(this->width(), this->height()));
317 }
robertphillips9199a9f2016-07-13 07:48:43 -0700318
319 if (fContext->caps()->useDrawInsteadOfClear()) {
320 // This works around a driver bug with clear by drawing a rect instead.
321 // The driver will ignore a clear if it is the only thing rendered to a
322 // target before the target is read.
Robert Phillips784b7bf2016-12-09 13:35:02 -0500323 SkIRect clearRect = SkIRect::MakeWH(this->width(), this->height());
csmartdalton29df7602016-08-31 11:55:52 -0700324 if (isFull) {
robertphillips9199a9f2016-07-13 07:48:43 -0700325 this->discard();
Robert Phillips93f16332016-11-23 19:37:13 -0500326 } else if (!clearRect.intersect(clip.scissorRect())) {
csmartdalton29df7602016-08-31 11:55:52 -0700327 return;
robertphillips9199a9f2016-07-13 07:48:43 -0700328 }
329
330 GrPaint paint;
331 paint.setColor4f(GrColor4f::FromGrColor(color));
Brian Salomona1633922017-01-09 11:46:10 -0500332 paint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
robertphillips9199a9f2016-07-13 07:48:43 -0700333
Brian Salomon82f44312017-01-11 13:42:54 -0500334 this->drawRect(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), SkRect::Make(clearRect));
bsalomon9f129de2016-08-10 16:31:05 -0700335 } else if (isFull) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400336 this->getRTOpList()->fullClear(*this->caps(), color);
robertphillips9199a9f2016-07-13 07:48:43 -0700337 } else {
Robert Phillips5efd5ea2017-05-30 13:47:32 -0400338 std::unique_ptr<GrOp> op(GrClearOp::Make(clip, color, this->asSurfaceProxy()));
Brian Salomon42521e82016-12-07 16:44:58 -0500339 if (!op) {
csmartdalton29df7602016-08-31 11:55:52 -0700340 return;
341 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400342 this->getRTOpList()->addOp(std::move(op), *this->caps());
robertphillips9199a9f2016-07-13 07:48:43 -0700343 }
robertphillipsea461502015-05-26 11:38:03 -0700344}
345
Brian Osman11052242016-10-27 14:47:55 -0400346void GrRenderTargetContext::drawPaint(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500347 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400348 const SkMatrix& viewMatrix) {
joshualitt1de610a2016-01-06 08:26:09 -0800349 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700350 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700351 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400352 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawPaint", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700353
robertphillipsea461502015-05-26 11:38:03 -0700354 // set rect to be big enough to fill the space, but not super-huge, so we
355 // don't overflow fixed-point implementations
robertphillips13a7eee2016-08-31 15:06:24 -0700356
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400357 SkRect r = fRenderTargetProxy->getBoundsRect();
robertphillipsea461502015-05-26 11:38:03 -0700358
bsalomoncb31e512016-08-26 10:48:19 -0700359 SkRRect rrect;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500360 GrAA aa;
bsalomoncb31e512016-08-26 10:48:19 -0700361 // Check if we can replace a clipRRect()/drawPaint() with a drawRRect(). We only do the
362 // transformation for non-rect rrects. Rects caused a performance regression on an Android
363 // test that needs investigation. We also skip cases where there are fragment processors
364 // because they may depend on having correct local coords and this path draws in device space
365 // without a local matrix.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500366 if (!paint.numTotalFragmentProcessors() && clip.isRRect(r, &rrect, &aa) && !rrect.isRect()) {
Brian Salomon82f44312017-01-11 13:42:54 -0500367 this->drawRRect(GrNoClip(), std::move(paint), aa, SkMatrix::I(), rrect,
368 GrStyle::SimpleFill());
bsalomoncb31e512016-08-26 10:48:19 -0700369 return;
370 }
371
robertphillipsea461502015-05-26 11:38:03 -0700372
373 bool isPerspective = viewMatrix.hasPerspective();
374
375 // We attempt to map r by the inverse matrix and draw that. mapRect will
376 // map the four corners and bound them with a new rect. This will not
377 // produce a correct result for some perspective matrices.
378 if (!isPerspective) {
reeda39667c2016-08-22 06:39:49 -0700379 if (!SkMatrixPriv::InverseMapRect(viewMatrix, &r, r)) {
robertphillipsea461502015-05-26 11:38:03 -0700380 SkDebugf("Could not invert matrix\n");
381 return;
382 }
Brian Salomon82f44312017-01-11 13:42:54 -0500383 this->drawRect(clip, std::move(paint), GrAA::kNo, viewMatrix, r);
robertphillipsea461502015-05-26 11:38:03 -0700384 } else {
385 SkMatrix localMatrix;
386 if (!viewMatrix.invert(&localMatrix)) {
387 SkDebugf("Could not invert matrix\n");
388 return;
389 }
390
Robert Phillips72152832017-01-25 17:31:35 -0500391 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700392
Brian Salomonbaaf4392017-06-15 09:59:23 -0400393 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeNonAAFillWithLocalMatrix(
394 std::move(paint), SkMatrix::I(), localMatrix, r, GrAAType::kNone);
395 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -0700396 }
397}
398
robertphillipsea461502015-05-26 11:38:03 -0700399static inline bool rect_contains_inclusive(const SkRect& rect, const SkPoint& point) {
400 return point.fX >= rect.fLeft && point.fX <= rect.fRight &&
401 point.fY >= rect.fTop && point.fY <= rect.fBottom;
402}
403
csmartdalton97f6cd52016-07-13 13:37:08 -0700404// Attempts to crop a rect and optional local rect to the clip boundaries.
405// Returns false if the draw can be skipped entirely.
robertphillips13a7eee2016-08-31 15:06:24 -0700406static bool crop_filled_rect(int width, int height, const GrClip& clip,
csmartdalton97f6cd52016-07-13 13:37:08 -0700407 const SkMatrix& viewMatrix, SkRect* rect,
408 SkRect* localRect = nullptr) {
409 if (!viewMatrix.rectStaysRect()) {
410 return true;
411 }
412
csmartdalton97f6cd52016-07-13 13:37:08 -0700413 SkIRect clipDevBounds;
414 SkRect clipBounds;
csmartdalton97f6cd52016-07-13 13:37:08 -0700415
robertphillips13a7eee2016-08-31 15:06:24 -0700416 clip.getConservativeBounds(width, height, &clipDevBounds);
reeda39667c2016-08-22 06:39:49 -0700417 if (!SkMatrixPriv::InverseMapRect(viewMatrix, &clipBounds, SkRect::Make(clipDevBounds))) {
418 return false;
419 }
csmartdalton97f6cd52016-07-13 13:37:08 -0700420
421 if (localRect) {
422 if (!rect->intersects(clipBounds)) {
423 return false;
424 }
425 const SkScalar dx = localRect->width() / rect->width();
426 const SkScalar dy = localRect->height() / rect->height();
427 if (clipBounds.fLeft > rect->fLeft) {
428 localRect->fLeft += (clipBounds.fLeft - rect->fLeft) * dx;
429 rect->fLeft = clipBounds.fLeft;
430 }
431 if (clipBounds.fTop > rect->fTop) {
432 localRect->fTop += (clipBounds.fTop - rect->fTop) * dy;
433 rect->fTop = clipBounds.fTop;
434 }
435 if (clipBounds.fRight < rect->fRight) {
436 localRect->fRight -= (rect->fRight - clipBounds.fRight) * dx;
437 rect->fRight = clipBounds.fRight;
438 }
439 if (clipBounds.fBottom < rect->fBottom) {
440 localRect->fBottom -= (rect->fBottom - clipBounds.fBottom) * dy;
441 rect->fBottom = clipBounds.fBottom;
442 }
443 return true;
444 }
445
446 return rect->intersect(clipBounds);
447}
448
Brian Osman11052242016-10-27 14:47:55 -0400449bool GrRenderTargetContext::drawFilledRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500450 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500451 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400452 const SkMatrix& viewMatrix,
453 const SkRect& rect,
454 const GrUserStencilSettings* ss) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700455 SkRect croppedRect = rect;
Robert Phillips784b7bf2016-12-09 13:35:02 -0500456 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix, &croppedRect)) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700457 return true;
458 }
robertphillips44302392016-07-08 14:43:03 -0700459
Brian Salomon54d212e2017-03-21 14:22:38 -0400460 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport() &&
461 (!ss || ss->isDisabled(false))) {
Robert Phillipse3302df2017-04-24 07:31:02 -0400462 gr_instanced::OpAllocator* oa = this->drawingManager()->instancingAllocator();
463 std::unique_ptr<GrDrawOp> op = oa->recordRect(croppedRect, viewMatrix, std::move(paint),
464 aa, fInstancedPipelineInfo);
Brian Salomon42521e82016-12-07 16:44:58 -0500465 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -0400466 this->addDrawOp(clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700467 return true;
csmartdaltona7f29642016-07-07 08:49:11 -0700468 }
469 }
Brian Salomon7c8460e2017-05-12 11:36:10 -0400470 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400471 std::unique_ptr<GrDrawOp> op;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500472 if (GrAAType::kCoverage == aaType) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400473 op = GrRectOpFactory::MakeAAFill(std::move(paint), viewMatrix, croppedRect, ss);
robertphillips391395d2016-03-02 09:26:36 -0800474 } else {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400475 op = GrRectOpFactory::MakeNonAAFill(std::move(paint), viewMatrix, croppedRect, aaType, ss);
robertphillips391395d2016-03-02 09:26:36 -0800476 }
Brian Salomonbaaf4392017-06-15 09:59:23 -0400477 if (!op) {
478 return false;
479 }
480 this->addDrawOp(clip, std::move(op));
481 return true;
robertphillips391395d2016-03-02 09:26:36 -0800482}
483
Brian Osman11052242016-10-27 14:47:55 -0400484void GrRenderTargetContext::drawRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500485 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500486 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400487 const SkMatrix& viewMatrix,
488 const SkRect& rect,
489 const GrStyle* style) {
bsalomon6663acf2016-05-10 09:14:17 -0700490 if (!style) {
491 style = &GrStyle::SimpleFill();
492 }
joshualitt1de610a2016-01-06 08:26:09 -0800493 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700494 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700495 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400496 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRect", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700497
bsalomon6663acf2016-05-10 09:14:17 -0700498 // Path effects should've been devolved to a path in SkGpuDevice
499 SkASSERT(!style->pathEffect());
robertphillipsea461502015-05-26 11:38:03 -0700500
Robert Phillips72152832017-01-25 17:31:35 -0500501 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700502
bsalomon6663acf2016-05-10 09:14:17 -0700503 const SkStrokeRec& stroke = style->strokeRec();
robertphillips3ab14ca2016-07-10 11:49:39 -0700504 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
Greg Daniela5cb7812017-06-16 09:45:32 -0400505
robertphillips3ab14ca2016-07-10 11:49:39 -0700506 if (!fContext->caps()->useDrawInsteadOfClear()) {
507 // Check if this is a full RT draw and can be replaced with a clear. We don't bother
508 // checking cases where the RT is fully inside a stroke.
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400509 SkRect rtRect = fRenderTargetProxy->getBoundsRect();
robertphillips3ab14ca2016-07-10 11:49:39 -0700510 // Does the clip contain the entire RT?
511 if (clip.quickContains(rtRect)) {
512 SkMatrix invM;
513 if (!viewMatrix.invert(&invM)) {
robertphillipsea461502015-05-26 11:38:03 -0700514 return;
515 }
robertphillips3ab14ca2016-07-10 11:49:39 -0700516 // Does the rect bound the RT?
517 SkPoint srcSpaceRTQuad[4];
518 invM.mapRectToQuad(srcSpaceRTQuad, rtRect);
519 if (rect_contains_inclusive(rect, srcSpaceRTQuad[0]) &&
520 rect_contains_inclusive(rect, srcSpaceRTQuad[1]) &&
521 rect_contains_inclusive(rect, srcSpaceRTQuad[2]) &&
522 rect_contains_inclusive(rect, srcSpaceRTQuad[3])) {
523 // Will it blend?
524 GrColor clearColor;
525 if (paint.isConstantBlendedColor(&clearColor)) {
robertphillips9199a9f2016-07-13 07:48:43 -0700526 this->clear(nullptr, clearColor, true);
robertphillips3ab14ca2016-07-10 11:49:39 -0700527 return;
528 }
529 }
robertphillipsea461502015-05-26 11:38:03 -0700530 }
531 }
robertphillips44302392016-07-08 14:43:03 -0700532
Brian Salomon82f44312017-01-11 13:42:54 -0500533 if (this->drawFilledRect(clip, std::move(paint), aa, viewMatrix, rect, nullptr)) {
robertphillips44302392016-07-08 14:43:03 -0700534 return;
535 }
bsalomona7d85ba2016-07-06 11:54:59 -0700536 } else if (stroke.getStyle() == SkStrokeRec::kStroke_Style ||
537 stroke.getStyle() == SkStrokeRec::kHairline_Style) {
538 if ((!rect.width() || !rect.height()) &&
539 SkStrokeRec::kHairline_Style != stroke.getStyle()) {
540 SkScalar r = stroke.getWidth() / 2;
541 // TODO: Move these stroke->fill fallbacks to GrShape?
542 switch (stroke.getJoin()) {
543 case SkPaint::kMiter_Join:
Brian Salomon82f44312017-01-11 13:42:54 -0500544 this->drawRect(
545 clip, std::move(paint), aa, viewMatrix,
546 {rect.fLeft - r, rect.fTop - r, rect.fRight + r, rect.fBottom + r},
547 &GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700548 return;
549 case SkPaint::kRound_Join:
550 // Raster draws nothing when both dimensions are empty.
551 if (rect.width() || rect.height()){
552 SkRRect rrect = SkRRect::MakeRectXY(rect.makeOutset(r, r), r, r);
Brian Salomon82f44312017-01-11 13:42:54 -0500553 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect,
554 GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700555 return;
556 }
557 case SkPaint::kBevel_Join:
558 if (!rect.width()) {
Brian Salomon82f44312017-01-11 13:42:54 -0500559 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700560 {rect.fLeft - r, rect.fTop, rect.fRight + r, rect.fBottom},
561 &GrStyle::SimpleFill());
562 } else {
Brian Salomon82f44312017-01-11 13:42:54 -0500563 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700564 {rect.fLeft, rect.fTop - r, rect.fRight, rect.fBottom + r},
565 &GrStyle::SimpleFill());
566 }
567 return;
568 }
569 }
robertphillips44302392016-07-08 14:43:03 -0700570
Brian Salomonbaaf4392017-06-15 09:59:23 -0400571 std::unique_ptr<GrDrawOp> op;
robertphillips44302392016-07-08 14:43:03 -0700572
Brian Salomon7c8460e2017-05-12 11:36:10 -0400573 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500574 if (GrAAType::kCoverage == aaType) {
cdaltonbb539482016-01-04 09:48:25 -0800575 // The stroke path needs the rect to remain axis aligned (no rotation or skew).
576 if (viewMatrix.rectStaysRect()) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400577 op = GrRectOpFactory::MakeAAStroke(std::move(paint), viewMatrix, rect, stroke);
cdaltonbb539482016-01-04 09:48:25 -0800578 }
robertphillipsea461502015-05-26 11:38:03 -0700579 } else {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400580 op = GrRectOpFactory::MakeNonAAStroke(std::move(paint), viewMatrix, rect, stroke,
581 aaType);
robertphillips391395d2016-03-02 09:26:36 -0800582 }
robertphillips4bc31812016-03-01 12:22:49 -0800583
Brian Salomon42521e82016-12-07 16:44:58 -0500584 if (op) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400585 this->addDrawOp(clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700586 return;
robertphillips4bc31812016-03-01 12:22:49 -0800587 }
robertphillips4bc31812016-03-01 12:22:49 -0800588 }
halcanary9d524f22016-03-29 09:03:52 -0700589
robertphillips4bc31812016-03-01 12:22:49 -0800590 SkPath path;
591 path.setIsVolatile(true);
592 path.addRect(rect);
Brian Salomon82f44312017-01-11 13:42:54 -0500593 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, *style);
robertphillipsea461502015-05-26 11:38:03 -0700594}
595
Robert Phillipsec2249f2016-11-09 08:54:35 -0500596int GrRenderTargetContextPriv::maxWindowRectangles() const {
597 return fRenderTargetContext->fRenderTargetProxy->maxWindowRectangles(
598 *fRenderTargetContext->fContext->caps());
599}
600
Brian Osman11052242016-10-27 14:47:55 -0400601void GrRenderTargetContextPriv::clearStencilClip(const GrFixedClip& clip, bool insideStencilMask) {
robertphillips976f5f02016-06-03 10:59:20 -0700602 ASSERT_SINGLE_OWNER_PRIV
603 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400604 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400605 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "clearStencilClip",
606 fRenderTargetContext->fContext);
robertphillips976f5f02016-06-03 10:59:20 -0700607
Robert Phillips72152832017-01-25 17:31:35 -0500608 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400609
Robert Phillips2f4ddf62017-06-01 08:48:19 -0400610 std::unique_ptr<GrOp> op(GrClearStencilClipOp::Make(
611 clip, insideStencilMask,
612 fRenderTargetContext->fRenderTargetProxy.get()));
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400613 if (!op) {
Robert Phillipse60ad622016-11-17 10:22:48 -0500614 return;
615 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400616 fRenderTargetContext->getRTOpList()->addOp(std::move(op), *fRenderTargetContext->caps());
robertphillips976f5f02016-06-03 10:59:20 -0700617}
618
Brian Osman11052242016-10-27 14:47:55 -0400619void GrRenderTargetContextPriv::stencilPath(const GrClip& clip,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500620 GrAAType aaType,
Brian Osman11052242016-10-27 14:47:55 -0400621 const SkMatrix& viewMatrix,
622 const GrPath* path) {
Brian Salomon467921e2017-03-06 16:17:12 -0500623 ASSERT_SINGLE_OWNER_PRIV
624 RETURN_IF_ABANDONED_PRIV
625 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400626 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "stencilPath",
627 fRenderTargetContext->fContext);
Brian Salomon467921e2017-03-06 16:17:12 -0500628
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500629 SkASSERT(aaType != GrAAType::kCoverage);
Brian Salomon467921e2017-03-06 16:17:12 -0500630
631 bool useHWAA = GrAATypeIsHW(aaType);
632 // TODO: extract portions of checkDraw that are relevant to path stenciling.
633 SkASSERT(path);
634 SkASSERT(fRenderTargetContext->caps()->shaderCaps()->pathRenderingSupport());
635
636 // FIXME: Use path bounds instead of this WAR once
637 // https://bugs.chromium.org/p/skia/issues/detail?id=5640 is resolved.
638 SkRect bounds = SkRect::MakeIWH(fRenderTargetContext->width(), fRenderTargetContext->height());
639
640 // Setup clip
Brian Salomon97180af2017-03-14 13:42:58 -0400641 GrAppliedClip appliedClip;
Brian Salomon467921e2017-03-06 16:17:12 -0500642 if (!clip.apply(fRenderTargetContext->fContext, fRenderTargetContext, useHWAA, true,
Brian Salomon97180af2017-03-14 13:42:58 -0400643 &appliedClip, &bounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -0500644 return;
645 }
646
647 // Coverage AA does not make sense when rendering to the stencil buffer. The caller should never
648 // attempt this in a situation that would require coverage AA.
649 SkASSERT(!appliedClip.clipCoverageFragmentProcessor());
650
651 GrRenderTarget* rt = fRenderTargetContext->accessRenderTarget();
652 if (!rt) {
653 return;
654 }
655 GrStencilAttachment* stencilAttachment =
656 fRenderTargetContext->fContext->resourceProvider()->attachStencilAttachment(rt);
657 if (!stencilAttachment) {
658 SkDebugf("ERROR creating stencil attachment. Draw skipped.\n");
659 return;
660 }
661
662 std::unique_ptr<GrOp> op = GrStencilPathOp::Make(viewMatrix,
663 useHWAA,
664 path->getFillType(),
665 appliedClip.hasStencilClip(),
666 stencilAttachment->bits(),
667 appliedClip.scissorState(),
Brian Salomon467921e2017-03-06 16:17:12 -0500668 path);
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400669 if (!op) {
670 return;
671 }
Brian Salomon97180af2017-03-14 13:42:58 -0400672 op->setClippedBounds(bounds);
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400673 fRenderTargetContext->getRTOpList()->addOp(std::move(op), *fRenderTargetContext->caps());
robertphillips976f5f02016-06-03 10:59:20 -0700674}
675
Brian Osman11052242016-10-27 14:47:55 -0400676void GrRenderTargetContextPriv::stencilRect(const GrClip& clip,
677 const GrUserStencilSettings* ss,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500678 GrAAType aaType,
Brian Osman11052242016-10-27 14:47:55 -0400679 const SkMatrix& viewMatrix,
680 const SkRect& rect) {
robertphillips976f5f02016-06-03 10:59:20 -0700681 ASSERT_SINGLE_OWNER_PRIV
682 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400683 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400684 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "stencilRect",
685 fRenderTargetContext->fContext);
686
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500687 SkASSERT(GrAAType::kCoverage != aaType);
Robert Phillips72152832017-01-25 17:31:35 -0500688 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips976f5f02016-06-03 10:59:20 -0700689
690 GrPaint paint;
Brian Salomona1633922017-01-09 11:46:10 -0500691 paint.setXPFactory(GrDisableColorXPFactory::Get());
Brian Salomonbaaf4392017-06-15 09:59:23 -0400692 std::unique_ptr<GrDrawOp> op =
693 GrRectOpFactory::MakeNonAAFill(std::move(paint), viewMatrix, rect, aaType, ss);
694 fRenderTargetContext->addDrawOp(clip, std::move(op));
robertphillips976f5f02016-06-03 10:59:20 -0700695}
696
Brian Osman11052242016-10-27 14:47:55 -0400697bool GrRenderTargetContextPriv::drawAndStencilRect(const GrClip& clip,
698 const GrUserStencilSettings* ss,
699 SkRegion::Op op,
700 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500701 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400702 const SkMatrix& viewMatrix,
703 const SkRect& rect) {
robertphillips391395d2016-03-02 09:26:36 -0800704 ASSERT_SINGLE_OWNER_PRIV
705 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400706 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400707 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawAndStencilRect",
708 fRenderTargetContext->fContext);
robertphillips391395d2016-03-02 09:26:36 -0800709
Robert Phillips72152832017-01-25 17:31:35 -0500710 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -0800711
712 GrPaint paint;
robertphillips391395d2016-03-02 09:26:36 -0800713 paint.setCoverageSetOpXPFactory(op, invert);
714
Brian Salomon82f44312017-01-11 13:42:54 -0500715 if (fRenderTargetContext->drawFilledRect(clip, std::move(paint), aa, viewMatrix, rect, ss)) {
robertphillips391395d2016-03-02 09:26:36 -0800716 return true;
717 }
robertphillips391395d2016-03-02 09:26:36 -0800718 SkPath path;
719 path.setIsVolatile(true);
720 path.addRect(rect);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500721 return this->drawAndStencilPath(clip, ss, op, invert, aa, viewMatrix, path);
robertphillips391395d2016-03-02 09:26:36 -0800722}
723
Brian Osman11052242016-10-27 14:47:55 -0400724void GrRenderTargetContext::fillRectToRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500725 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500726 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400727 const SkMatrix& viewMatrix,
728 const SkRect& rectToDraw,
729 const SkRect& localRect) {
joshualitt1de610a2016-01-06 08:26:09 -0800730 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700731 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700732 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400733 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "fillRectToRect", fContext);
robertphillipsea461502015-05-26 11:38:03 -0700734
csmartdalton97f6cd52016-07-13 13:37:08 -0700735 SkRect croppedRect = rectToDraw;
736 SkRect croppedLocalRect = localRect;
robertphillips13a7eee2016-08-31 15:06:24 -0700737 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix,
738 &croppedRect, &croppedLocalRect)) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700739 return;
740 }
741
Robert Phillips72152832017-01-25 17:31:35 -0500742 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -0700743
csmartdaltone0d36292016-07-29 08:14:20 -0700744 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport()) {
Robert Phillipse3302df2017-04-24 07:31:02 -0400745 gr_instanced::OpAllocator* oa = this->drawingManager()->instancingAllocator();
746 std::unique_ptr<GrDrawOp> op(oa->recordRect(croppedRect, viewMatrix, std::move(paint),
Brian Salomon54d212e2017-03-21 14:22:38 -0400747 croppedLocalRect, aa, fInstancedPipelineInfo));
Brian Salomon42521e82016-12-07 16:44:58 -0500748 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -0400749 this->addDrawOp(clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -0700750 return;
751 }
752 }
753
Brian Salomon7c8460e2017-05-12 11:36:10 -0400754 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500755 if (GrAAType::kCoverage != aaType) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400756 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeNonAAFillWithLocalRect(
757 std::move(paint), viewMatrix, croppedRect, croppedLocalRect, aaType);
758 this->addDrawOp(clip, std::move(op));
csmartdaltonfc49d562016-07-26 17:05:47 -0700759 return;
joshualitt04194f32016-01-13 10:08:27 -0800760 }
bsalomonbb243832016-07-22 07:10:19 -0700761
Brian Salomonbaaf4392017-06-15 09:59:23 -0400762 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeAAFillWithLocalRect(
763 std::move(paint), viewMatrix, croppedRect, croppedLocalRect);
764 if (op) {
765 this->addDrawOp(clip, std::move(op));
csmartdaltonfc49d562016-07-26 17:05:47 -0700766 return;
767 }
768
769 SkMatrix viewAndUnLocalMatrix;
770 if (!viewAndUnLocalMatrix.setRectToRect(localRect, rectToDraw, SkMatrix::kFill_ScaleToFit)) {
771 SkDebugf("fillRectToRect called with empty local matrix.\n");
772 return;
773 }
774 viewAndUnLocalMatrix.postConcat(viewMatrix);
775
776 SkPath path;
777 path.setIsVolatile(true);
778 path.addRect(localRect);
Brian Salomon82f44312017-01-11 13:42:54 -0500779 this->internalDrawPath(clip, std::move(paint), aa, viewAndUnLocalMatrix, path, GrStyle());
joshualittb6b513b2015-08-21 10:25:18 -0700780}
781
Brian Osman11052242016-10-27 14:47:55 -0400782void GrRenderTargetContext::fillRectWithLocalMatrix(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500783 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500784 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400785 const SkMatrix& viewMatrix,
786 const SkRect& rectToDraw,
787 const SkMatrix& localMatrix) {
joshualitt1de610a2016-01-06 08:26:09 -0800788 ASSERT_SINGLE_OWNER
joshualittb6b513b2015-08-21 10:25:18 -0700789 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700790 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400791 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "fillRectWithLocalMatrix", fContext);
joshualittb6b513b2015-08-21 10:25:18 -0700792
csmartdalton97f6cd52016-07-13 13:37:08 -0700793 SkRect croppedRect = rectToDraw;
robertphillips13a7eee2016-08-31 15:06:24 -0700794 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix, &croppedRect)) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700795 return;
796 }
797
Robert Phillips72152832017-01-25 17:31:35 -0500798 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -0700799
csmartdaltone0d36292016-07-29 08:14:20 -0700800 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport()) {
Robert Phillipse3302df2017-04-24 07:31:02 -0400801 gr_instanced::OpAllocator* oa = this->drawingManager()->instancingAllocator();
802 std::unique_ptr<GrDrawOp> op(oa->recordRect(croppedRect, viewMatrix, std::move(paint),
Brian Salomon54d212e2017-03-21 14:22:38 -0400803 localMatrix, aa, fInstancedPipelineInfo));
Brian Salomon42521e82016-12-07 16:44:58 -0500804 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -0400805 this->addDrawOp(clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -0700806 return;
807 }
808 }
809
Brian Salomon7c8460e2017-05-12 11:36:10 -0400810 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500811 if (GrAAType::kCoverage != aaType) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400812 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeNonAAFillWithLocalMatrix(
813 std::move(paint), viewMatrix, localMatrix, croppedRect, aaType);
814 this->addDrawOp(clip, std::move(op));
csmartdaltonfc49d562016-07-26 17:05:47 -0700815 return;
bsalomonc55271f2015-11-09 11:55:57 -0800816 }
robertphillips4bc31812016-03-01 12:22:49 -0800817
Brian Salomonbaaf4392017-06-15 09:59:23 -0400818 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeAAFillWithLocalMatrix(
819 std::move(paint), viewMatrix, localMatrix, croppedRect);
820 if (op) {
821 this->addDrawOp(clip, std::move(op));
csmartdaltonfc49d562016-07-26 17:05:47 -0700822 return;
823 }
824
825 SkMatrix viewAndUnLocalMatrix;
826 if (!localMatrix.invert(&viewAndUnLocalMatrix)) {
827 SkDebugf("fillRectWithLocalMatrix called with degenerate local matrix.\n");
828 return;
829 }
830 viewAndUnLocalMatrix.postConcat(viewMatrix);
831
832 SkPath path;
833 path.setIsVolatile(true);
834 path.addRect(rectToDraw);
835 path.transform(localMatrix);
Brian Salomon82f44312017-01-11 13:42:54 -0500836 this->internalDrawPath(clip, std::move(paint), aa, viewAndUnLocalMatrix, path, GrStyle());
robertphillipsea461502015-05-26 11:38:03 -0700837}
838
Brian Osman11052242016-10-27 14:47:55 -0400839void GrRenderTargetContext::drawVertices(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500840 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400841 const SkMatrix& viewMatrix,
Brian Osmanae0c50c2017-05-25 16:56:34 -0400842 sk_sp<SkVertices> vertices,
843 GrPrimitiveType* overridePrimType) {
Brian Salomon199fb872017-02-06 09:41:10 -0500844 ASSERT_SINGLE_OWNER
845 RETURN_IF_ABANDONED
846 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400847 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawVertices", fContext);
Brian Salomon199fb872017-02-06 09:41:10 -0500848
849 AutoCheckFlush acf(this->drawingManager());
850
851 SkASSERT(vertices);
Brian Salomonc2f42542017-07-12 14:11:22 -0400852 GrAAType aaType = this->chooseAAType(GrAA::kNo, GrAllowMixedSamples::kNo);
853 std::unique_ptr<GrDrawOp> op =
854 GrDrawVerticesOp::Make(std::move(paint), std::move(vertices), viewMatrix, aaType,
855 this->isGammaCorrect(), fColorXformFromSRGB, overridePrimType);
856 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -0700857}
858
859///////////////////////////////////////////////////////////////////////////////
860
Brian Osman11052242016-10-27 14:47:55 -0400861void GrRenderTargetContext::drawAtlas(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500862 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400863 const SkMatrix& viewMatrix,
864 int spriteCount,
865 const SkRSXform xform[],
866 const SkRect texRect[],
867 const SkColor colors[]) {
joshualitt1de610a2016-01-06 08:26:09 -0800868 ASSERT_SINGLE_OWNER
jvanverth31ff7622015-08-07 10:09:28 -0700869 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700870 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400871 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawAtlas", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700872
Robert Phillips72152832017-01-25 17:31:35 -0500873 AutoCheckFlush acf(this->drawingManager());
halcanary9d524f22016-03-29 09:03:52 -0700874
Brian Salomon0088f942017-07-12 11:51:27 -0400875 GrAAType aaType = this->chooseAAType(GrAA::kNo, GrAllowMixedSamples::kNo);
876 std::unique_ptr<GrDrawOp> op = GrDrawAtlasOp::Make(std::move(paint), viewMatrix, aaType,
877 spriteCount, xform, texRect, colors);
878 this->addDrawOp(clip, std::move(op));
jvanverth31ff7622015-08-07 10:09:28 -0700879}
880
881///////////////////////////////////////////////////////////////////////////////
882
Brian Osman11052242016-10-27 14:47:55 -0400883void GrRenderTargetContext::drawRRect(const GrClip& origClip,
Brian Salomon82f44312017-01-11 13:42:54 -0500884 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500885 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400886 const SkMatrix& viewMatrix,
887 const SkRRect& rrect,
888 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -0800889 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700890 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700891 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400892 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRRect", fContext);
robertphillipsea461502015-05-26 11:38:03 -0700893 if (rrect.isEmpty()) {
894 return;
895 }
896
bsalomon7f0d9f32016-08-15 14:49:10 -0700897 GrNoClip noclip;
898 const GrClip* clip = &origClip;
899#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
900 // The Android framework frequently clips rrects to themselves where the clip is non-aa and the
Brian Salomon42521e82016-12-07 16:44:58 -0500901 // draw is aa. Since our lower level clip code works from op bounds, which are SkRects, it
bsalomon7f0d9f32016-08-15 14:49:10 -0700902 // doesn't detect that the clip can be ignored (modulo antialiasing). The following test
903 // attempts to mitigate the stencil clip cost but will only help when the entire clip stack
904 // can be ignored. We'd prefer to fix this in the framework by removing the clips calls.
905 SkRRect devRRect;
906 if (rrect.transform(viewMatrix, &devRRect) && clip->quickContains(devRRect)) {
907 clip = &noclip;
908 }
909#endif
bsalomon6663acf2016-05-10 09:14:17 -0700910 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
ksakamotoec7f2ac2016-07-05 03:54:53 -0700911
Robert Phillips72152832017-01-25 17:31:35 -0500912 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -0700913 const SkStrokeRec stroke = style.strokeRec();
csmartdaltona7f29642016-07-07 08:49:11 -0700914
csmartdaltone0d36292016-07-29 08:14:20 -0700915 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport() &&
916 stroke.isFillStyle()) {
Robert Phillipse3302df2017-04-24 07:31:02 -0400917 gr_instanced::OpAllocator* oa = this->drawingManager()->instancingAllocator();
Brian Salomon54d212e2017-03-21 14:22:38 -0400918 std::unique_ptr<GrDrawOp> op(
Robert Phillipse3302df2017-04-24 07:31:02 -0400919 oa->recordRRect(rrect, viewMatrix, std::move(paint), aa, fInstancedPipelineInfo));
Brian Salomon42521e82016-12-07 16:44:58 -0500920 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -0400921 this->addDrawOp(*clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -0700922 return;
923 }
924 }
925
Brian Salomon7c8460e2017-05-12 11:36:10 -0400926 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500927 if (GrAAType::kCoverage == aaType) {
Brian Salomon94efbf52016-11-29 13:43:05 -0500928 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomon05441c42017-05-15 16:45:49 -0400929 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeRRectOp(std::move(paint),
Brian Salomon05441c42017-05-15 16:45:49 -0400930 viewMatrix,
931 rrect,
932 stroke,
933 shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -0500934 if (op) {
Brian Salomon05441c42017-05-15 16:45:49 -0400935 this->addDrawOp(*clip, std::move(op));
robertphillipsb56f9272016-02-25 11:03:52 -0800936 return;
937 }
robertphillipsea461502015-05-26 11:38:03 -0700938 }
robertphillipsb56f9272016-02-25 11:03:52 -0800939
940 SkPath path;
941 path.setIsVolatile(true);
942 path.addRRect(rrect);
Brian Salomon82f44312017-01-11 13:42:54 -0500943 this->internalDrawPath(*clip, std::move(paint), aa, viewMatrix, path, style);
robertphillipsea461502015-05-26 11:38:03 -0700944}
945
Jim Van Verthc5903412016-11-17 15:27:09 -0500946///////////////////////////////////////////////////////////////////////////////
947
Jim Van Verth3af1af92017-05-18 15:06:54 -0400948static SkPoint3 map(const SkMatrix& m, const SkPoint3& pt) {
949 SkPoint3 result;
950 m.mapXY(pt.fX, pt.fY, (SkPoint*)&result.fX);
951 result.fZ = pt.fZ;
952 return result;
953}
954
955bool GrRenderTargetContext::drawFastShadow(const GrClip& clip,
Brian Salomon05969092017-07-13 11:20:51 -0400956 GrColor color4ub,
Jim Van Verth3af1af92017-05-18 15:06:54 -0400957 const SkMatrix& viewMatrix,
958 const SkPath& path,
959 const SkDrawShadowRec& rec) {
Jim Van Verthc5903412016-11-17 15:27:09 -0500960 ASSERT_SINGLE_OWNER
Jim Van Verth3af1af92017-05-18 15:06:54 -0400961 if (this->drawingManager()->wasAbandoned()) {
962 return true;
963 }
Jim Van Verthc5903412016-11-17 15:27:09 -0500964 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400965 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFastShadow", fContext);
Jim Van Verth3af1af92017-05-18 15:06:54 -0400966
967 // check z plane
968 bool tiltZPlane = SkToBool(!SkScalarNearlyZero(rec.fZPlaneParams.fX) ||
969 !SkScalarNearlyZero(rec.fZPlaneParams.fY));
970 bool skipAnalytic = SkToBool(rec.fFlags & SkShadowFlags::kGeometricOnly_ShadowFlag);
971 if (tiltZPlane || skipAnalytic || !viewMatrix.rectStaysRect() || !viewMatrix.isSimilarity()) {
972 return false;
973 }
974
975 SkRRect rrect;
976 SkRect rect;
977 // we can only handle rects, circles, and rrects with circular corners
978 bool isRRect = path.isRRect(&rrect) && rrect.isSimpleCircular() &&
979 rrect.radii(SkRRect::kUpperLeft_Corner).fX > SK_ScalarNearlyZero;
980 if (!isRRect &&
981 path.isOval(&rect) && SkScalarNearlyEqual(rect.width(), rect.height()) &&
982 rect.width() > SK_ScalarNearlyZero) {
983 rrect.setOval(rect);
984 isRRect = true;
985 }
986 if (!isRRect && path.isRect(&rect)) {
987 rrect.setRect(rect);
988 isRRect = true;
989 }
990
991 if (!isRRect) {
992 return false;
993 }
994
Jim Van Verthc5903412016-11-17 15:27:09 -0500995 if (rrect.isEmpty()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -0400996 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -0500997 }
998
Robert Phillips72152832017-01-25 17:31:35 -0500999 AutoCheckFlush acf(this->drawingManager());
Jim Van Verthc5903412016-11-17 15:27:09 -05001000
Jim Van Verth3af1af92017-05-18 15:06:54 -04001001 // transform light
1002 SkPoint3 devLightPos = map(viewMatrix, rec.fLightPos);
1003
1004 // 1/scale
1005 SkScalar devToSrcScale = viewMatrix.isScaleTranslate() ?
1006 SkScalarInvert(viewMatrix[SkMatrix::kMScaleX]) :
1007 sk_float_rsqrt(viewMatrix[SkMatrix::kMScaleX] * viewMatrix[SkMatrix::kMScaleX] +
1008 viewMatrix[SkMatrix::kMSkewX] * viewMatrix[SkMatrix::kMSkewX]);
1009
1010 SkScalar occluderHeight = rec.fZPlaneParams.fZ;
Brian Salomon05969092017-07-13 11:20:51 -04001011 GrColor4f color = GrColor4f::FromGrColor(color4ub);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001012 bool transparent = SkToBool(rec.fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag);
Jim Van Verth34d6e4b2017-06-09 11:09:03 -04001013 bool tonalColor = SkToBool(rec.fFlags & SkShadowFlags::kTonalColor_ShadowFlag);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001014
1015 if (rec.fAmbientAlpha > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001016 SkScalar devSpaceInsetWidth = SkDrawShadowMetrics::AmbientBlurRadius(occluderHeight);
1017 const SkScalar umbraRecipAlpha = SkDrawShadowMetrics::AmbientRecipAlpha(occluderHeight);
1018 const SkScalar devSpaceAmbientBlur = devSpaceInsetWidth * umbraRecipAlpha;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001019
1020 // Outset the shadow rrect to the border of the penumbra
1021 SkScalar ambientPathOutset = devSpaceInsetWidth * devToSrcScale;
1022 SkRRect ambientRRect;
1023 SkRect outsetRect = rrect.rect().makeOutset(ambientPathOutset, ambientPathOutset);
1024 // If the rrect was an oval then its outset will also be one.
1025 // We set it explicitly to avoid errors.
1026 if (rrect.isOval()) {
1027 ambientRRect = SkRRect::MakeOval(outsetRect);
1028 } else {
1029 SkScalar outsetRad = rrect.getSimpleRadii().fX + ambientPathOutset;
1030 ambientRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1031 }
1032
Jim Van Verth34d6e4b2017-06-09 11:09:03 -04001033 GrColor ambientColor;
1034 if (tonalColor) {
1035 // with tonal color, the color only applies to the spot shadow
1036 ambientColor = GrColorPackRGBA(0, 0, 0, 255.999f*rec.fAmbientAlpha);
1037 } else {
1038 ambientColor = color.mulByScalar(rec.fAmbientAlpha).toGrColor();
1039 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001040 if (transparent) {
1041 // set a large inset to force a fill
1042 devSpaceInsetWidth = ambientRRect.width();
1043 }
1044 // the fraction of the blur we want to apply is devSpaceInsetWidth/devSpaceAmbientBlur,
Jim Van Verth1af03d42017-07-31 09:34:58 -04001045 // which is just 1/umbraRecipAlpha.
1046 SkScalar blurClamp = SkScalarInvert(umbraRecipAlpha);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001047
Brian Salomon05969092017-07-13 11:20:51 -04001048 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(ambientColor, viewMatrix,
1049 ambientRRect,
1050 devSpaceAmbientBlur,
1051 devSpaceInsetWidth,
1052 blurClamp);
1053 SkASSERT(op);
1054 this->addDrawOp(clip, std::move(op));
Jim Van Verthc5903412016-11-17 15:27:09 -05001055 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001056
1057 if (rec.fSpotAlpha > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001058 SkScalar devSpaceSpotBlur;
1059 SkScalar spotScale;
1060 SkVector spotOffset;
1061 SkDrawShadowMetrics::GetSpotParams(occluderHeight, devLightPos.fX, devLightPos.fY,
1062 devLightPos.fZ, rec.fLightRadius,
1063 &devSpaceSpotBlur, &spotScale, &spotOffset);
1064 // handle scale of radius due to CTM
Jim Van Verth3af1af92017-05-18 15:06:54 -04001065 const SkScalar srcSpaceSpotBlur = devSpaceSpotBlur * devToSrcScale;
1066
Jim Van Verth3af1af92017-05-18 15:06:54 -04001067 // Adjust translate for the effect of the scale.
1068 spotOffset.fX += spotScale*viewMatrix[SkMatrix::kMTransX];
1069 spotOffset.fY += spotScale*viewMatrix[SkMatrix::kMTransY];
1070 // This offset is in dev space, need to transform it into source space.
1071 SkMatrix ctmInverse;
1072 if (viewMatrix.invert(&ctmInverse)) {
1073 ctmInverse.mapPoints(&spotOffset, 1);
1074 } else {
1075 // Since the matrix is a similarity, this should never happen, but just in case...
1076 SkDebugf("Matrix is degenerate. Will not render spot shadow correctly!\n");
1077 SkASSERT(false);
1078 }
1079
1080 // Compute the transformed shadow rrect
1081 SkRRect spotShadowRRect;
1082 SkMatrix shadowTransform;
1083 shadowTransform.setScaleTranslate(spotScale, spotScale, spotOffset.fX, spotOffset.fY);
1084 rrect.transform(shadowTransform, &spotShadowRRect);
1085 SkScalar spotRadius = spotShadowRRect.getSimpleRadii().fX;
1086
1087 // Compute the insetWidth
Jim Van Verth1af03d42017-07-31 09:34:58 -04001088 SkScalar blurOutset = srcSpaceSpotBlur;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001089 SkScalar insetWidth = blurOutset;
1090 if (transparent) {
1091 // If transparent, just do a fill
1092 insetWidth += spotShadowRRect.width();
1093 } else {
1094 // For shadows, instead of using a stroke we specify an inset from the penumbra
1095 // border. We want to extend this inset area so that it meets up with the caster
1096 // geometry. The inset geometry will by default already be inset by the blur width.
1097 //
1098 // We compare the min and max corners inset by the radius between the original
1099 // rrect and the shadow rrect. The distance between the two plus the difference
1100 // between the scaled radius and the original radius gives the distance from the
1101 // transformed shadow shape to the original shape in that corner. The max
1102 // of these gives the maximum distance we need to cover.
1103 //
1104 // Since we are outsetting by 1/2 the blur distance, we just add the maxOffset to
1105 // that to get the full insetWidth.
1106 SkScalar maxOffset;
1107 if (rrect.isRect()) {
1108 // Manhattan distance works better for rects
1109 maxOffset = SkTMax(SkTMax(SkTAbs(spotShadowRRect.rect().fLeft -
1110 rrect.rect().fLeft),
1111 SkTAbs(spotShadowRRect.rect().fTop -
1112 rrect.rect().fTop)),
1113 SkTMax(SkTAbs(spotShadowRRect.rect().fRight -
1114 rrect.rect().fRight),
1115 SkTAbs(spotShadowRRect.rect().fBottom -
1116 rrect.rect().fBottom)));
1117 } else {
1118 SkScalar dr = spotRadius - rrect.getSimpleRadii().fX;
1119 SkPoint upperLeftOffset = SkPoint::Make(spotShadowRRect.rect().fLeft -
1120 rrect.rect().fLeft + dr,
1121 spotShadowRRect.rect().fTop -
1122 rrect.rect().fTop + dr);
1123 SkPoint lowerRightOffset = SkPoint::Make(spotShadowRRect.rect().fRight -
1124 rrect.rect().fRight - dr,
1125 spotShadowRRect.rect().fBottom -
1126 rrect.rect().fBottom - dr);
1127 maxOffset = SkScalarSqrt(SkTMax(upperLeftOffset.lengthSqd(),
1128 lowerRightOffset.lengthSqd())) + dr;
1129 }
1130 insetWidth += maxOffset;
1131 }
1132
1133 // Outset the shadow rrect to the border of the penumbra
1134 SkRect outsetRect = spotShadowRRect.rect().makeOutset(blurOutset, blurOutset);
1135 if (spotShadowRRect.isOval()) {
1136 spotShadowRRect = SkRRect::MakeOval(outsetRect);
1137 } else {
1138 SkScalar outsetRad = spotRadius + blurOutset;
1139 spotShadowRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1140 }
1141
Jim Van Verth34d6e4b2017-06-09 11:09:03 -04001142 GrColor spotColor;
1143 if (tonalColor) {
1144 SkScalar colorScale;
1145 SkScalar tonalAlpha;
1146 SkShadowUtils::ComputeTonalColorParams(color.fRGBA[0], color.fRGBA[1],
1147 color.fRGBA[2], rec.fSpotAlpha,
1148 &colorScale, &tonalAlpha);
1149 color.fRGBA[0] *= colorScale;
1150 color.fRGBA[1] *= colorScale;
1151 color.fRGBA[2] *= colorScale;
1152 color.fRGBA[3] = tonalAlpha;
1153 spotColor = color.toGrColor();
1154 } else {
1155 spotColor = color.mulByScalar(rec.fSpotAlpha).toGrColor();
1156 }
1157
Brian Salomon05969092017-07-13 11:20:51 -04001158 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(spotColor, viewMatrix,
1159 spotShadowRRect,
Jim Van Verth1af03d42017-07-31 09:34:58 -04001160 2.0f * devSpaceSpotBlur,
Brian Salomon05969092017-07-13 11:20:51 -04001161 insetWidth);
1162 SkASSERT(op);
1163 this->addDrawOp(clip, std::move(op));
Jim Van Verth3af1af92017-05-18 15:06:54 -04001164 }
1165
1166 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001167}
1168
1169///////////////////////////////////////////////////////////////////////////////
1170
Brian Osman11052242016-10-27 14:47:55 -04001171bool GrRenderTargetContext::drawFilledDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001172 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001173 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001174 const SkMatrix& viewMatrix,
1175 const SkRRect& origOuter,
1176 const SkRRect& origInner) {
robertphillips00095892016-02-29 13:50:40 -08001177 SkASSERT(!origInner.isEmpty());
1178 SkASSERT(!origOuter.isEmpty());
1179
csmartdaltone0d36292016-07-29 08:14:20 -07001180 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport()) {
Robert Phillipse3302df2017-04-24 07:31:02 -04001181 gr_instanced::OpAllocator* oa = this->drawingManager()->instancingAllocator();
1182 std::unique_ptr<GrDrawOp> op(oa->recordDRRect(
Brian Salomon54d212e2017-03-21 14:22:38 -04001183 origOuter, origInner, viewMatrix, std::move(paint), aa, fInstancedPipelineInfo));
Brian Salomon42521e82016-12-07 16:44:58 -05001184 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -04001185 this->addDrawOp(clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -07001186 return true;
1187 }
1188 }
1189
Brian Salomon7c8460e2017-05-12 11:36:10 -04001190 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
robertphillips00095892016-02-29 13:50:40 -08001191
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001192 GrPrimitiveEdgeType innerEdgeType, outerEdgeType;
1193 if (GrAAType::kCoverage == aaType) {
1194 innerEdgeType = kInverseFillAA_GrProcessorEdgeType;
1195 outerEdgeType = kFillAA_GrProcessorEdgeType;
1196 } else {
1197 innerEdgeType = kInverseFillBW_GrProcessorEdgeType;
1198 outerEdgeType = kFillBW_GrProcessorEdgeType;
1199 }
robertphillips00095892016-02-29 13:50:40 -08001200
1201 SkTCopyOnFirstWrite<SkRRect> inner(origInner), outer(origOuter);
1202 SkMatrix inverseVM;
1203 if (!viewMatrix.isIdentity()) {
1204 if (!origInner.transform(viewMatrix, inner.writable())) {
1205 return false;
1206 }
1207 if (!origOuter.transform(viewMatrix, outer.writable())) {
1208 return false;
1209 }
1210 if (!viewMatrix.invert(&inverseVM)) {
1211 return false;
1212 }
1213 } else {
1214 inverseVM.reset();
halcanary9d524f22016-03-29 09:03:52 -07001215 }
robertphillips00095892016-02-29 13:50:40 -08001216
robertphillips00095892016-02-29 13:50:40 -08001217 // TODO these need to be a geometry processors
bungeman06ca8ec2016-06-09 08:01:03 -07001218 sk_sp<GrFragmentProcessor> innerEffect(GrRRectEffect::Make(innerEdgeType, *inner));
robertphillips00095892016-02-29 13:50:40 -08001219 if (!innerEffect) {
1220 return false;
1221 }
1222
bungeman06ca8ec2016-06-09 08:01:03 -07001223 sk_sp<GrFragmentProcessor> outerEffect(GrRRectEffect::Make(outerEdgeType, *outer));
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 }
1260
1261 SkPath path;
1262 path.setIsVolatile(true);
1263 path.addRRect(inner);
1264 path.addRRect(outer);
1265 path.setFillType(SkPath::kEvenOdd_FillType);
1266
Brian Salomon82f44312017-01-11 13:42:54 -05001267 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, GrStyle::SimpleFill());
robertphillips00095892016-02-29 13:50:40 -08001268}
1269
robertphillipsea461502015-05-26 11:38:03 -07001270///////////////////////////////////////////////////////////////////////////////
1271
msarettcc319b92016-08-25 18:07:18 -07001272static inline bool is_int(float x) {
1273 return x == (float) sk_float_round2int(x);
1274}
1275
Brian Osman11052242016-10-27 14:47:55 -04001276void GrRenderTargetContext::drawRegion(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001277 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001278 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001279 const SkMatrix& viewMatrix,
1280 const SkRegion& region,
Stan Iliev73d8fd92017-08-02 15:36:24 -04001281 const GrStyle& style,
1282 const GrUserStencilSettings* ss) {
msarettcc319b92016-08-25 18:07:18 -07001283 ASSERT_SINGLE_OWNER
1284 RETURN_IF_ABANDONED
1285 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001286 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRegion", fContext);
msarettcc319b92016-08-25 18:07:18 -07001287
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001288 if (GrAA::kYes == aa) {
Brian Salomonfc527d22016-12-14 21:07:01 -05001289 // GrRegionOp performs no antialiasing but is much faster, so here we check the matrix
Brian Salomonc57c7c92016-12-06 14:47:34 -05001290 // to see whether aa is really required.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001291 if (!SkToBool(viewMatrix.getType() & ~(SkMatrix::kTranslate_Mask)) &&
1292 is_int(viewMatrix.getTranslateX()) &&
1293 is_int(viewMatrix.getTranslateY())) {
1294 aa = GrAA::kNo;
1295 }
Brian Salomonc57c7c92016-12-06 14:47:34 -05001296 }
msarettcc319b92016-08-25 18:07:18 -07001297 bool complexStyle = !style.isSimpleFill();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001298 if (complexStyle || GrAA::kYes == aa) {
msarettcc319b92016-08-25 18:07:18 -07001299 SkPath path;
1300 region.getBoundaryPath(&path);
Brian Salomon82f44312017-01-11 13:42:54 -05001301 return this->drawPath(clip, std::move(paint), aa, viewMatrix, path, style);
msarettcc319b92016-08-25 18:07:18 -07001302 }
1303
Brian Salomonf0366322017-07-11 15:53:05 -04001304 GrAAType aaType = this->chooseAAType(GrAA::kNo, GrAllowMixedSamples::kNo);
Stan Iliev73d8fd92017-08-02 15:36:24 -04001305 std::unique_ptr<GrDrawOp> op = GrRegionOp::Make(std::move(paint), viewMatrix, region, aaType,
1306 ss);
Brian Salomonf0366322017-07-11 15:53:05 -04001307 this->addDrawOp(clip, std::move(op));
msarettcc319b92016-08-25 18:07:18 -07001308}
1309
Brian Osman11052242016-10-27 14:47:55 -04001310void GrRenderTargetContext::drawOval(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001311 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001312 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001313 const SkMatrix& viewMatrix,
1314 const SkRect& oval,
1315 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001316 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001317 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001318 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001319 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawOval", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -07001320
robertphillipsea461502015-05-26 11:38:03 -07001321 if (oval.isEmpty()) {
1322 return;
1323 }
1324
bsalomon6663acf2016-05-10 09:14:17 -07001325 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
robertphillipsea461502015-05-26 11:38:03 -07001326
Robert Phillips72152832017-01-25 17:31:35 -05001327 AutoCheckFlush acf(this->drawingManager());
bsalomon6663acf2016-05-10 09:14:17 -07001328 const SkStrokeRec& stroke = style.strokeRec();
csmartdaltona7f29642016-07-07 08:49:11 -07001329
csmartdaltone0d36292016-07-29 08:14:20 -07001330 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport() &&
1331 stroke.isFillStyle()) {
Robert Phillipse3302df2017-04-24 07:31:02 -04001332 gr_instanced::OpAllocator* oa = this->drawingManager()->instancingAllocator();
Brian Salomon54d212e2017-03-21 14:22:38 -04001333 std::unique_ptr<GrDrawOp> op(
Robert Phillipse3302df2017-04-24 07:31:02 -04001334 oa->recordOval(oval, viewMatrix, std::move(paint), aa, fInstancedPipelineInfo));
Brian Salomon42521e82016-12-07 16:44:58 -05001335 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -04001336 this->addDrawOp(clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -07001337 return;
1338 }
1339 }
1340
Brian Salomon7c8460e2017-05-12 11:36:10 -04001341 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001342 if (GrAAType::kCoverage == aaType) {
Brian Salomon94efbf52016-11-29 13:43:05 -05001343 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomon05441c42017-05-15 16:45:49 -04001344 std::unique_ptr<GrDrawOp> op =
1345 GrOvalOpFactory::MakeOvalOp(std::move(paint), viewMatrix, oval, stroke, shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -05001346 if (op) {
Brian Salomon05441c42017-05-15 16:45:49 -04001347 this->addDrawOp(clip, std::move(op));
robertphillipsb56f9272016-02-25 11:03:52 -08001348 return;
1349 }
robertphillipsea461502015-05-26 11:38:03 -07001350 }
robertphillipsb56f9272016-02-25 11:03:52 -08001351
1352 SkPath path;
1353 path.setIsVolatile(true);
1354 path.addOval(oval);
Brian Salomon82f44312017-01-11 13:42:54 -05001355 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, style);
robertphillipsea461502015-05-26 11:38:03 -07001356}
1357
Brian Osman11052242016-10-27 14:47:55 -04001358void GrRenderTargetContext::drawArc(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001359 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001360 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001361 const SkMatrix& viewMatrix,
1362 const SkRect& oval,
1363 SkScalar startAngle,
1364 SkScalar sweepAngle,
1365 bool useCenter,
1366 const GrStyle& style) {
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001367 ASSERT_SINGLE_OWNER
1368 RETURN_IF_ABANDONED
1369 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001370 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawArc", fContext);
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001371
1372 AutoCheckFlush acf(this->drawingManager());
1373
Brian Salomon7c8460e2017-05-12 11:36:10 -04001374 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001375 if (GrAAType::kCoverage == aaType) {
Brian Salomon94efbf52016-11-29 13:43:05 -05001376 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomon05441c42017-05-15 16:45:49 -04001377 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeArcOp(std::move(paint),
1378 viewMatrix,
1379 oval,
1380 startAngle,
1381 sweepAngle,
1382 useCenter,
1383 style,
1384 shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -05001385 if (op) {
Brian Salomon05441c42017-05-15 16:45:49 -04001386 this->addDrawOp(clip, std::move(op));
bsalomon4f3a0ca2016-08-22 13:14:26 -07001387 return;
1388 }
1389 }
1390 SkPath path;
bsalomon21af9ca2016-08-25 12:29:23 -07001391 SkPathPriv::CreateDrawArcPath(&path, oval, startAngle, sweepAngle, useCenter,
1392 style.isSimpleFill());
Brian Salomon82f44312017-01-11 13:42:54 -05001393 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, style);
bsalomon4f3a0ca2016-08-22 13:14:26 -07001394}
1395
Brian Osman11052242016-10-27 14:47:55 -04001396void GrRenderTargetContext::drawImageLattice(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001397 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001398 const SkMatrix& viewMatrix,
1399 int imageWidth,
1400 int imageHeight,
1401 std::unique_ptr<SkLatticeIter> iter,
1402 const SkRect& dst) {
joshualitt1de610a2016-01-06 08:26:09 -08001403 ASSERT_SINGLE_OWNER
joshualitt33a5fce2015-11-18 13:28:51 -08001404 RETURN_IF_ABANDONED
1405 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001406 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawImageLattice", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001407
Robert Phillips72152832017-01-25 17:31:35 -05001408 AutoCheckFlush acf(this->drawingManager());
joshualitt33a5fce2015-11-18 13:28:51 -08001409
Brian Salomon815486c2017-07-11 08:52:13 -04001410 std::unique_ptr<GrDrawOp> op = GrLatticeOp::MakeNonAA(std::move(paint), viewMatrix, imageWidth,
1411 imageHeight, std::move(iter), dst);
1412 this->addDrawOp(clip, std::move(op));
joshualitt33a5fce2015-11-18 13:28:51 -08001413}
1414
Greg Daniel51316782017-08-02 15:10:09 +00001415GrSemaphoresSubmitted GrRenderTargetContext::prepareForExternalIO(
1416 int numSemaphores, GrBackendSemaphore backendSemaphores[]) {
robertphillips8c523e02016-07-26 07:41:00 -07001417 ASSERT_SINGLE_OWNER
Greg Daniel51316782017-08-02 15:10:09 +00001418 if (this->drawingManager()->wasAbandoned()) { return GrSemaphoresSubmitted::kNo; }
robertphillips8c523e02016-07-26 07:41:00 -07001419 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001420 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "prepareForExternalIO", fContext);
robertphillips8c523e02016-07-26 07:41:00 -07001421
Greg Daniel51316782017-08-02 15:10:09 +00001422 return this->drawingManager()->prepareSurfaceForExternalIO(fRenderTargetProxy.get(),
1423 numSemaphores,
1424 backendSemaphores);
Greg Daniela5cb7812017-06-16 09:45:32 -04001425}
1426
Greg Danielc64ee462017-06-15 16:59:49 -04001427bool GrRenderTargetContext::waitOnSemaphores(int numSemaphores,
Greg Daniela5cb7812017-06-16 09:45:32 -04001428 const GrBackendSemaphore* waitSemaphores) {
1429 ASSERT_SINGLE_OWNER
Greg Danielc64ee462017-06-15 16:59:49 -04001430 RETURN_FALSE_IF_ABANDONED
Greg Daniela5cb7812017-06-16 09:45:32 -04001431 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001432 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "waitOnSemaphores", fContext);
Greg Daniela5cb7812017-06-16 09:45:32 -04001433
1434 AutoCheckFlush acf(this->drawingManager());
1435
Greg Danielc64ee462017-06-15 16:59:49 -04001436 if (numSemaphores && !this->caps()->fenceSyncSupport()) {
1437 return false;
1438 }
1439
Greg Daniela5cb7812017-06-16 09:45:32 -04001440 SkTArray<sk_sp<GrSemaphore>> semaphores(numSemaphores);
1441 for (int i = 0; i < numSemaphores; ++i) {
1442 sk_sp<GrSemaphore> sema = fContext->resourceProvider()->wrapBackendSemaphore(
1443 waitSemaphores[i], kAdopt_GrWrapOwnership);
1444 std::unique_ptr<GrOp> waitOp(GrSemaphoreOp::MakeWait(sema, fRenderTargetProxy.get()));
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001445 this->getRTOpList()->addOp(std::move(waitOp), *this->caps());
Greg Daniela5cb7812017-06-16 09:45:32 -04001446 }
Greg Danielc64ee462017-06-15 16:59:49 -04001447 return true;
robertphillips8c523e02016-07-26 07:41:00 -07001448}
joshualitt33a5fce2015-11-18 13:28:51 -08001449
Robert Phillips65a88fa2017-08-08 08:36:22 -04001450void GrRenderTargetContext::insertEventMarker(const SkString& str) {
1451 std::unique_ptr<GrOp> op(GrDebugMarkerOp::Make(fRenderTargetProxy.get(), str));
1452 this->getRTOpList()->addOp(std::move(op), *this->caps());
1453}
1454
1455
robertphillipsea461502015-05-26 11:38:03 -07001456// Can 'path' be drawn as a pair of filled nested rectangles?
bsalomon6663acf2016-05-10 09:14:17 -07001457static bool fills_as_nested_rects(const SkMatrix& viewMatrix, const SkPath& path, SkRect rects[2]) {
robertphillipsea461502015-05-26 11:38:03 -07001458
1459 if (path.isInverseFillType()) {
1460 return false;
1461 }
1462
1463 // TODO: this restriction could be lifted if we were willing to apply
1464 // the matrix to all the points individually rather than just to the rect
robertphillips0e7029e2015-11-30 05:45:06 -08001465 if (!viewMatrix.rectStaysRect()) {
robertphillipsea461502015-05-26 11:38:03 -07001466 return false;
1467 }
1468
1469 SkPath::Direction dirs[2];
1470 if (!path.isNestedFillRects(rects, dirs)) {
1471 return false;
1472 }
1473
1474 if (SkPath::kWinding_FillType == path.getFillType() && dirs[0] == dirs[1]) {
1475 // The two rects need to be wound opposite to each other
1476 return false;
1477 }
1478
1479 // Right now, nested rects where the margin is not the same width
1480 // all around do not render correctly
1481 const SkScalar* outer = rects[0].asScalars();
1482 const SkScalar* inner = rects[1].asScalars();
1483
1484 bool allEq = true;
1485
1486 SkScalar margin = SkScalarAbs(outer[0] - inner[0]);
1487 bool allGoE1 = margin >= SK_Scalar1;
1488
1489 for (int i = 1; i < 4; ++i) {
1490 SkScalar temp = SkScalarAbs(outer[i] - inner[i]);
1491 if (temp < SK_Scalar1) {
1492 allGoE1 = false;
1493 }
1494 if (!SkScalarNearlyEqual(margin, temp)) {
1495 allEq = false;
1496 }
1497 }
1498
1499 return allEq || allGoE1;
1500}
1501
Brian Osman11052242016-10-27 14:47:55 -04001502void GrRenderTargetContext::drawPath(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001503 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001504 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001505 const SkMatrix& viewMatrix,
1506 const SkPath& path,
1507 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001508 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001509 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001510 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001511 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawPath", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -07001512
robertphillipsea461502015-05-26 11:38:03 -07001513 if (path.isEmpty()) {
1514 if (path.isInverseFillType()) {
Brian Salomon82f44312017-01-11 13:42:54 -05001515 this->drawPaint(clip, std::move(paint), viewMatrix);
robertphillipsea461502015-05-26 11:38:03 -07001516 }
1517 return;
1518 }
1519
Robert Phillips72152832017-01-25 17:31:35 -05001520 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -07001521
Brian Salomon7c8460e2017-05-12 11:36:10 -04001522 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001523 if (GrAAType::kCoverage == aaType && !style.pathEffect()) {
bsalomon6663acf2016-05-10 09:14:17 -07001524 if (style.isSimpleFill() && !path.isConvex()) {
robertphillipsea461502015-05-26 11:38:03 -07001525 // Concave AA paths are expensive - try to avoid them for special cases
1526 SkRect rects[2];
1527
bsalomon6663acf2016-05-10 09:14:17 -07001528 if (fills_as_nested_rects(viewMatrix, path, rects)) {
Brian Salomonbaaf4392017-06-15 09:59:23 -04001529 std::unique_ptr<GrDrawOp> op =
1530 GrRectOpFactory::MakeAAFillNestedRects(std::move(paint), viewMatrix, rects);
Brian Salomon5f970fe2017-06-16 17:30:59 -04001531 if (op) {
1532 this->addDrawOp(clip, std::move(op));
bsalomon40ef4852016-05-02 13:22:13 -07001533 }
Brian Salomon5f970fe2017-06-16 17:30:59 -04001534 // A null return indicates that there is nothing to draw in this case.
1535 return;
robertphillipsea461502015-05-26 11:38:03 -07001536 }
1537 }
1538 SkRect ovalRect;
1539 bool isOval = path.isOval(&ovalRect);
1540
1541 if (isOval && !path.isInverseFillType()) {
Brian Salomon94efbf52016-11-29 13:43:05 -05001542 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomon05441c42017-05-15 16:45:49 -04001543 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeOvalOp(
1544 std::move(paint), viewMatrix, ovalRect, style.strokeRec(), shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -05001545 if (op) {
Brian Salomon05441c42017-05-15 16:45:49 -04001546 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -07001547 return;
1548 }
1549 }
1550 }
robertphillips4bc31812016-03-01 12:22:49 -08001551
1552 // Note that internalDrawPath may sw-rasterize the path into a scratch texture.
1553 // Scratch textures can be recycled after they are returned to the texture
1554 // cache. This presents a potential hazard for buffered drawing. However,
1555 // the writePixels that uploads to the scratch will perform a flush so we're
1556 // OK.
Brian Salomon82f44312017-01-11 13:42:54 -05001557 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, style);
robertphillipsea461502015-05-26 11:38:03 -07001558}
1559
Brian Osman11052242016-10-27 14:47:55 -04001560bool GrRenderTargetContextPriv::drawAndStencilPath(const GrClip& clip,
1561 const GrUserStencilSettings* ss,
1562 SkRegion::Op op,
1563 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001564 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001565 const SkMatrix& viewMatrix,
1566 const SkPath& path) {
robertphillips391395d2016-03-02 09:26:36 -08001567 ASSERT_SINGLE_OWNER_PRIV
1568 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -04001569 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001570 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawAndStencilPath",
1571 fRenderTargetContext->fContext);
robertphillips391395d2016-03-02 09:26:36 -08001572
1573 if (path.isEmpty() && path.isInverseFillType()) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001574 this->drawAndStencilRect(clip, ss, op, invert, GrAA::kNo, SkMatrix::I(),
Brian Osman11052242016-10-27 14:47:55 -04001575 SkRect::MakeIWH(fRenderTargetContext->width(),
1576 fRenderTargetContext->height()));
robertphillips391395d2016-03-02 09:26:36 -08001577 return true;
1578 }
1579
Robert Phillips72152832017-01-25 17:31:35 -05001580 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -08001581
1582 // An Assumption here is that path renderer would use some form of tweaking
1583 // the src color (either the input alpha or in the frag shader) to implement
1584 // aa. If we have some future driver-mojo path AA that can do the right
1585 // thing WRT to the blend then we'll need some query on the PR.
Brian Salomon7c8460e2017-05-12 11:36:10 -04001586 GrAAType aaType = fRenderTargetContext->chooseAAType(aa, GrAllowMixedSamples::kNo);
robertphillips976f5f02016-06-03 10:59:20 -07001587 bool hasUserStencilSettings = !ss->isUnused();
robertphillips391395d2016-03-02 09:26:36 -08001588
bsalomon8acedde2016-06-24 10:42:16 -07001589 GrShape shape(path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -08001590 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Eric Karl5c779752017-05-08 12:02:07 -07001591 canDrawArgs.fCaps = fRenderTargetContext->drawingManager()->getContext()->caps();
robertphillips391395d2016-03-02 09:26:36 -08001592 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07001593 canDrawArgs.fShape = &shape;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001594 canDrawArgs.fAAType = aaType;
cdalton93a379b2016-05-11 13:58:08 -07001595 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips391395d2016-03-02 09:26:36 -08001596
1597 // Don't allow the SW renderer
Robert Phillips72152832017-01-25 17:31:35 -05001598 GrPathRenderer* pr = fRenderTargetContext->drawingManager()->getPathRenderer(
Brian Salomon36aa1762016-12-10 13:24:02 -05001599 canDrawArgs, false, GrPathRendererChain::DrawType::kStencilAndColor);
robertphillips391395d2016-03-02 09:26:36 -08001600 if (!pr) {
1601 return false;
1602 }
1603
1604 GrPaint paint;
1605 paint.setCoverageSetOpXPFactory(op, invert);
1606
Brian Salomon82f44312017-01-11 13:42:54 -05001607 GrPathRenderer::DrawPathArgs args{
Robert Phillips256c37b2017-03-01 14:32:46 -05001608 fRenderTargetContext->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05001609 std::move(paint),
1610 ss,
1611 fRenderTargetContext,
1612 &clip,
1613 &viewMatrix,
1614 &shape,
1615 aaType,
1616 fRenderTargetContext->isGammaCorrect()};
robertphillips391395d2016-03-02 09:26:36 -08001617 pr->drawPath(args);
1618 return true;
1619}
1620
Brian Osman11052242016-10-27 14:47:55 -04001621SkBudgeted GrRenderTargetContextPriv::isBudgeted() const {
robertphillips714712b2016-08-04 06:20:45 -07001622 ASSERT_SINGLE_OWNER_PRIV
1623
Brian Osman11052242016-10-27 14:47:55 -04001624 if (fRenderTargetContext->wasAbandoned()) {
robertphillips714712b2016-08-04 06:20:45 -07001625 return SkBudgeted::kNo;
1626 }
1627
Brian Osman11052242016-10-27 14:47:55 -04001628 SkDEBUGCODE(fRenderTargetContext->validate();)
robertphillips714712b2016-08-04 06:20:45 -07001629
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001630 return fRenderTargetContext->fRenderTargetProxy->isBudgeted();
robertphillips714712b2016-08-04 06:20:45 -07001631}
1632
Brian Osman11052242016-10-27 14:47:55 -04001633void GrRenderTargetContext::internalDrawPath(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001634 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001635 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001636 const SkMatrix& viewMatrix,
1637 const SkPath& path,
1638 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001639 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001640 RETURN_IF_ABANDONED
Brian Salomondcbb9d92017-07-19 10:53:20 -04001641 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "internalDrawPath", fContext);
1642
bsalomon8acedde2016-06-24 10:42:16 -07001643 SkASSERT(!path.isEmpty());
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001644 GrShape shape;
Brian Salomon7c8460e2017-05-12 11:36:10 -04001645 // NVPR cannot handle hairlines, so this would get picked up by a different stencil and
1646 // cover path renderer (i.e. default path renderer). The hairline renderer produces much
1647 // smoother hairlines than MSAA.
1648 GrAllowMixedSamples allowMixedSamples =
1649 style.isSimpleHairline() ? GrAllowMixedSamples::kNo : GrAllowMixedSamples::kYes;
1650 GrAAType aaType = this->chooseAAType(aa, allowMixedSamples);
robertphillips68737822015-10-29 12:12:21 -07001651 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Eric Karl5c779752017-05-08 12:02:07 -07001652 canDrawArgs.fCaps = this->drawingManager()->getContext()->caps();
robertphillips68737822015-10-29 12:12:21 -07001653 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07001654 canDrawArgs.fShape = &shape;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001655 canDrawArgs.fHasUserStencilSettings = false;
robertphillips68737822015-10-29 12:12:21 -07001656
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001657 GrPathRenderer* pr;
Brian Salomon82125e92016-12-10 09:35:48 -05001658 static constexpr GrPathRendererChain::DrawType kType = GrPathRendererChain::DrawType::kColor;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001659 do {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001660 shape = GrShape(path, style);
bsalomon8acedde2016-06-24 10:42:16 -07001661 if (shape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001662 return;
1663 }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001664
1665 canDrawArgs.fAAType = aaType;
1666
1667 // Try a 1st time without applying any of the style to the geometry (and barring sw)
Robert Phillips72152832017-01-25 17:31:35 -05001668 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001669 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
1670
1671 if (!pr && shape.style().pathEffect()) {
1672 // It didn't work above, so try again with the path effect applied.
1673 shape = shape.applyStyle(GrStyle::Apply::kPathEffectOnly, styleScale);
bsalomon8acedde2016-06-24 10:42:16 -07001674 if (shape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001675 return;
1676 }
Robert Phillips72152832017-01-25 17:31:35 -05001677 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
bsalomon6663acf2016-05-10 09:14:17 -07001678 }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001679 if (!pr) {
1680 if (shape.style().applies()) {
1681 shape = shape.applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec, styleScale);
1682 if (shape.isEmpty()) {
1683 return;
1684 }
1685 }
1686 // This time, allow SW renderer
Robert Phillips72152832017-01-25 17:31:35 -05001687 pr = this->drawingManager()->getPathRenderer(canDrawArgs, true, kType);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001688 }
Brian Salomon0abc8b42016-12-13 10:22:54 -05001689 if (!pr && GrAATypeIsHW(aaType)) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001690 // There are exceptional cases where we may wind up falling back to coverage based AA
1691 // when the target is MSAA (e.g. through disabling path renderers via GrContextOptions).
1692 aaType = GrAAType::kCoverage;
1693 } else {
1694 break;
1695 }
1696 } while(true);
robertphillipsea461502015-05-26 11:38:03 -07001697
bsalomon8acedde2016-06-24 10:42:16 -07001698 if (!pr) {
robertphillipsea461502015-05-26 11:38:03 -07001699#ifdef SK_DEBUG
1700 SkDebugf("Unable to find path renderer compatible with path.\n");
1701#endif
1702 return;
1703 }
1704
Robert Phillips256c37b2017-03-01 14:32:46 -05001705 GrPathRenderer::DrawPathArgs args{this->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05001706 std::move(paint),
1707 &GrUserStencilSettings::kUnused,
1708 this,
1709 &clip,
1710 &viewMatrix,
1711 &shape,
1712 aaType,
1713 this->isGammaCorrect()};
bsalomon0aff2fa2015-07-31 06:48:27 -07001714 pr->drawPath(args);
robertphillipsea461502015-05-26 11:38:03 -07001715}
1716
Brian Salomon467921e2017-03-06 16:17:12 -05001717static void op_bounds(SkRect* bounds, const GrOp* op) {
1718 *bounds = op->bounds();
1719 if (op->hasZeroArea()) {
1720 if (op->hasAABloat()) {
1721 bounds->outset(0.5f, 0.5f);
1722 } else {
1723 // We don't know which way the particular GPU will snap lines or points at integer
1724 // coords. So we ensure that the bounds is large enough for either snap.
1725 SkRect before = *bounds;
1726 bounds->roundOut(bounds);
1727 if (bounds->fLeft == before.fLeft) {
1728 bounds->fLeft -= 1;
1729 }
1730 if (bounds->fTop == before.fTop) {
1731 bounds->fTop -= 1;
1732 }
1733 if (bounds->fRight == before.fRight) {
1734 bounds->fRight += 1;
1735 }
1736 if (bounds->fBottom == before.fBottom) {
1737 bounds->fBottom += 1;
1738 }
1739 }
1740 }
1741}
1742
Brian Salomon54d212e2017-03-21 14:22:38 -04001743uint32_t GrRenderTargetContext::addDrawOp(const GrClip& clip, std::unique_ptr<GrDrawOp> op) {
joshualitt1de610a2016-01-06 08:26:09 -08001744 ASSERT_SINGLE_OWNER
Robert Phillipsc0138922017-03-08 11:50:55 -05001745 if (this->drawingManager()->wasAbandoned()) {
1746 return SK_InvalidUniqueID;
1747 }
robertphillips2e1e51f2015-10-15 08:01:48 -07001748 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001749 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "addDrawOp", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -07001750
Brian Salomon467921e2017-03-06 16:17:12 -05001751 // Setup clip
1752 SkRect bounds;
1753 op_bounds(&bounds, op.get());
Brian Salomon97180af2017-03-14 13:42:58 -04001754 GrAppliedClip appliedClip;
Brian Salomon54d212e2017-03-21 14:22:38 -04001755 GrDrawOp::FixedFunctionFlags fixedFunctionFlags = op->fixedFunctionFlags();
1756 if (!clip.apply(fContext, this, fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesHWAA,
1757 fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil, &appliedClip,
1758 &bounds)) {
1759 return SK_InvalidUniqueID;
1760 }
1761
Brian Salomon54d212e2017-03-21 14:22:38 -04001762 if (fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil ||
1763 appliedClip.hasStencilClip()) {
Robert Phillips54190c42017-08-09 20:09:38 +00001764 this->getOpList()->setRequiresStencil();
Robert Phillips95214472017-08-08 18:00:03 -04001765
Robert Phillips5efd5ea2017-05-30 13:47:32 -04001766 // This forces instantiation of the render target.
1767 GrRenderTarget* rt = this->accessRenderTarget();
1768 if (!rt) {
1769 return SK_InvalidUniqueID;
1770 }
1771
Brian Salomon54d212e2017-03-21 14:22:38 -04001772 if (!fContext->resourceProvider()->attachStencilAttachment(rt)) {
1773 SkDebugf("ERROR creating stencil attachment. Draw skipped.\n");
1774 return SK_InvalidUniqueID;
1775 }
1776 }
1777
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001778 GrXferProcessor::DstProxy dstProxy;
Brian Salomonf86d37b2017-06-16 10:04:34 -04001779 if (op->finalize(*this->caps(), &appliedClip) == GrDrawOp::RequiresDstTexture::kYes) {
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001780 if (!this->setupDstProxy(this->asRenderTargetProxy(), clip, op->bounds(), &dstProxy)) {
Brian Salomon54d212e2017-03-21 14:22:38 -04001781 return SK_InvalidUniqueID;
1782 }
1783 }
1784
1785 op->setClippedBounds(bounds);
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001786 return this->getRTOpList()->addOp(std::move(op), *this->caps(),
1787 std::move(appliedClip), dstProxy);
Brian Salomon54d212e2017-03-21 14:22:38 -04001788}
1789
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001790bool GrRenderTargetContext::setupDstProxy(GrRenderTargetProxy* rtProxy, const GrClip& clip,
Robert Phillips16d8ec62017-07-27 16:16:25 -04001791 const SkRect& opBounds,
1792 GrXferProcessor::DstProxy* dstProxy) {
Brian Salomon467921e2017-03-06 16:17:12 -05001793 if (this->caps()->textureBarrierSupport()) {
Robert Phillipsbf25d432017-04-07 10:08:53 -04001794 if (GrTextureProxy* texProxy = rtProxy->asTextureProxy()) {
Brian Salomon467921e2017-03-06 16:17:12 -05001795 // The render target is a texture, so we can read from it directly in the shader. The XP
1796 // will be responsible to detect this situation and request a texture barrier.
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001797 dstProxy->setProxy(sk_ref_sp(texProxy));
1798 dstProxy->setOffset(0, 0);
Robert Phillipsbf25d432017-04-07 10:08:53 -04001799 return true;
Brian Salomon467921e2017-03-06 16:17:12 -05001800 }
1801 }
1802
Robert Phillipsbf25d432017-04-07 10:08:53 -04001803 SkIRect copyRect = SkIRect::MakeWH(rtProxy->width(), rtProxy->height());
Brian Salomon467921e2017-03-06 16:17:12 -05001804
Eric Karl74480882017-04-03 14:49:05 -07001805 SkIRect clippedRect;
Robert Phillipsbf25d432017-04-07 10:08:53 -04001806 clip.getConservativeBounds(rtProxy->width(), rtProxy->height(), &clippedRect);
Eric Karl72e551e2017-04-04 13:42:10 -07001807 SkIRect drawIBounds;
Brian Salomon467921e2017-03-06 16:17:12 -05001808 opBounds.roundOut(&drawIBounds);
Brian Salomon859621f2017-03-16 09:21:54 -04001809 // Cover up for any precision issues by outsetting the op bounds a pixel in each direction.
1810 drawIBounds.outset(1, 1);
Eric Karl72e551e2017-04-04 13:42:10 -07001811 if (!clippedRect.intersect(drawIBounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -05001812#ifdef SK_DEBUG
Robert Phillipsbf25d432017-04-07 10:08:53 -04001813 GrCapsDebugf(this->caps(), "setupDstTexture: Missed an early reject bailing on draw.");
Brian Salomon467921e2017-03-06 16:17:12 -05001814#endif
Robert Phillipsbf25d432017-04-07 10:08:53 -04001815 return false;
Brian Salomon467921e2017-03-06 16:17:12 -05001816 }
1817
1818 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
1819 // have per-sample dst values by making the copy multisampled.
1820 GrSurfaceDesc desc;
Eric Karl74480882017-04-03 14:49:05 -07001821 bool rectsMustMatch = false;
1822 bool disallowSubrect = false;
Robert Phillipsbf25d432017-04-07 10:08:53 -04001823 if (!this->caps()->initDescForDstCopy(rtProxy, &desc, &rectsMustMatch, &disallowSubrect)) {
Brian Salomon467921e2017-03-06 16:17:12 -05001824 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillips16d8ec62017-07-27 16:16:25 -04001825 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
Robert Phillipsbf25d432017-04-07 10:08:53 -04001826 desc.fConfig = rtProxy->config();
Brian Salomon467921e2017-03-06 16:17:12 -05001827 }
1828
Eric Karl74480882017-04-03 14:49:05 -07001829 if (!disallowSubrect) {
1830 copyRect = clippedRect;
1831 }
Brian Salomon467921e2017-03-06 16:17:12 -05001832
Robert Phillipsbf25d432017-04-07 10:08:53 -04001833 SkIPoint dstPoint, dstOffset;
1834 SkBackingFit fit;
Eric Karl74480882017-04-03 14:49:05 -07001835 if (rectsMustMatch) {
Robert Phillipsbf25d432017-04-07 10:08:53 -04001836 SkASSERT(desc.fOrigin == rtProxy->origin());
1837 desc.fWidth = rtProxy->width();
1838 desc.fHeight = rtProxy->height();
Eric Karl74480882017-04-03 14:49:05 -07001839 dstPoint = {copyRect.fLeft, copyRect.fTop};
1840 dstOffset = {0, 0};
Robert Phillipsbf25d432017-04-07 10:08:53 -04001841 fit = SkBackingFit::kExact;
Eric Karl74480882017-04-03 14:49:05 -07001842 } else {
1843 desc.fWidth = copyRect.width();
1844 desc.fHeight = copyRect.height();
1845 dstPoint = {0, 0};
1846 dstOffset = {copyRect.fLeft, copyRect.fTop};
Robert Phillipsbf25d432017-04-07 10:08:53 -04001847 fit = SkBackingFit::kApprox;
Eric Karl74480882017-04-03 14:49:05 -07001848 }
Brian Salomon467921e2017-03-06 16:17:12 -05001849
Robert Phillipsbf25d432017-04-07 10:08:53 -04001850 sk_sp<GrSurfaceContext> sContext = fContext->contextPriv().makeDeferredSurfaceContext(
1851 desc,
1852 fit,
1853 SkBudgeted::kYes);
1854 if (!sContext) {
1855 SkDebugf("setupDstTexture: surfaceContext creation failed.\n");
1856 return false;
1857 }
1858
1859 if (!sContext->copy(rtProxy, copyRect, dstPoint)) {
1860 SkDebugf("setupDstTexture: copy failed.\n");
1861 return false;
1862 }
1863
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001864 dstProxy->setProxy(sContext->asTextureProxyRef());
1865 dstProxy->setOffset(dstOffset);
Robert Phillipsbf25d432017-04-07 10:08:53 -04001866 return true;
robertphillips2334fb62015-06-17 05:43:33 -07001867}