blob: 03b120c2c1858f52d1ad9fd966109474a29f718b [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
Robert Phillips65048132017-08-10 08:44:49 -0400651 fRenderTargetContext->setNeedsStencil();
Brian Salomon467921e2017-03-06 16:17:12 -0500652
653 std::unique_ptr<GrOp> op = GrStencilPathOp::Make(viewMatrix,
654 useHWAA,
655 path->getFillType(),
656 appliedClip.hasStencilClip(),
Brian Salomon467921e2017-03-06 16:17:12 -0500657 appliedClip.scissorState(),
Brian Salomon467921e2017-03-06 16:17:12 -0500658 path);
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400659 if (!op) {
660 return;
661 }
Brian Salomon97180af2017-03-14 13:42:58 -0400662 op->setClippedBounds(bounds);
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400663 fRenderTargetContext->getRTOpList()->addOp(std::move(op), *fRenderTargetContext->caps());
robertphillips976f5f02016-06-03 10:59:20 -0700664}
665
Brian Osman11052242016-10-27 14:47:55 -0400666void GrRenderTargetContextPriv::stencilRect(const GrClip& clip,
667 const GrUserStencilSettings* ss,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500668 GrAAType aaType,
Brian Osman11052242016-10-27 14:47:55 -0400669 const SkMatrix& viewMatrix,
670 const SkRect& rect) {
robertphillips976f5f02016-06-03 10:59:20 -0700671 ASSERT_SINGLE_OWNER_PRIV
672 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400673 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400674 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "stencilRect",
675 fRenderTargetContext->fContext);
676
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500677 SkASSERT(GrAAType::kCoverage != aaType);
Robert Phillips72152832017-01-25 17:31:35 -0500678 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips976f5f02016-06-03 10:59:20 -0700679
680 GrPaint paint;
Brian Salomona1633922017-01-09 11:46:10 -0500681 paint.setXPFactory(GrDisableColorXPFactory::Get());
Brian Salomonbaaf4392017-06-15 09:59:23 -0400682 std::unique_ptr<GrDrawOp> op =
683 GrRectOpFactory::MakeNonAAFill(std::move(paint), viewMatrix, rect, aaType, ss);
684 fRenderTargetContext->addDrawOp(clip, std::move(op));
robertphillips976f5f02016-06-03 10:59:20 -0700685}
686
Brian Osman11052242016-10-27 14:47:55 -0400687bool GrRenderTargetContextPriv::drawAndStencilRect(const GrClip& clip,
688 const GrUserStencilSettings* ss,
689 SkRegion::Op op,
690 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500691 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400692 const SkMatrix& viewMatrix,
693 const SkRect& rect) {
robertphillips391395d2016-03-02 09:26:36 -0800694 ASSERT_SINGLE_OWNER_PRIV
695 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400696 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400697 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawAndStencilRect",
698 fRenderTargetContext->fContext);
robertphillips391395d2016-03-02 09:26:36 -0800699
Robert Phillips72152832017-01-25 17:31:35 -0500700 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -0800701
702 GrPaint paint;
robertphillips391395d2016-03-02 09:26:36 -0800703 paint.setCoverageSetOpXPFactory(op, invert);
704
Brian Salomon82f44312017-01-11 13:42:54 -0500705 if (fRenderTargetContext->drawFilledRect(clip, std::move(paint), aa, viewMatrix, rect, ss)) {
robertphillips391395d2016-03-02 09:26:36 -0800706 return true;
707 }
robertphillips391395d2016-03-02 09:26:36 -0800708 SkPath path;
709 path.setIsVolatile(true);
710 path.addRect(rect);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500711 return this->drawAndStencilPath(clip, ss, op, invert, aa, viewMatrix, path);
robertphillips391395d2016-03-02 09:26:36 -0800712}
713
Brian Osman11052242016-10-27 14:47:55 -0400714void GrRenderTargetContext::fillRectToRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500715 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500716 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400717 const SkMatrix& viewMatrix,
718 const SkRect& rectToDraw,
719 const SkRect& localRect) {
joshualitt1de610a2016-01-06 08:26:09 -0800720 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700721 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700722 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400723 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "fillRectToRect", fContext);
robertphillipsea461502015-05-26 11:38:03 -0700724
csmartdalton97f6cd52016-07-13 13:37:08 -0700725 SkRect croppedRect = rectToDraw;
726 SkRect croppedLocalRect = localRect;
robertphillips13a7eee2016-08-31 15:06:24 -0700727 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix,
728 &croppedRect, &croppedLocalRect)) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700729 return;
730 }
731
Robert Phillips72152832017-01-25 17:31:35 -0500732 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -0700733
csmartdaltone0d36292016-07-29 08:14:20 -0700734 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport()) {
Robert Phillipse3302df2017-04-24 07:31:02 -0400735 gr_instanced::OpAllocator* oa = this->drawingManager()->instancingAllocator();
736 std::unique_ptr<GrDrawOp> op(oa->recordRect(croppedRect, viewMatrix, std::move(paint),
Brian Salomon54d212e2017-03-21 14:22:38 -0400737 croppedLocalRect, aa, fInstancedPipelineInfo));
Brian Salomon42521e82016-12-07 16:44:58 -0500738 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -0400739 this->addDrawOp(clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -0700740 return;
741 }
742 }
743
Brian Salomon7c8460e2017-05-12 11:36:10 -0400744 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500745 if (GrAAType::kCoverage != aaType) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400746 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeNonAAFillWithLocalRect(
747 std::move(paint), viewMatrix, croppedRect, croppedLocalRect, aaType);
748 this->addDrawOp(clip, std::move(op));
csmartdaltonfc49d562016-07-26 17:05:47 -0700749 return;
joshualitt04194f32016-01-13 10:08:27 -0800750 }
bsalomonbb243832016-07-22 07:10:19 -0700751
Brian Salomonbaaf4392017-06-15 09:59:23 -0400752 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeAAFillWithLocalRect(
753 std::move(paint), viewMatrix, croppedRect, croppedLocalRect);
754 if (op) {
755 this->addDrawOp(clip, std::move(op));
csmartdaltonfc49d562016-07-26 17:05:47 -0700756 return;
757 }
758
759 SkMatrix viewAndUnLocalMatrix;
760 if (!viewAndUnLocalMatrix.setRectToRect(localRect, rectToDraw, SkMatrix::kFill_ScaleToFit)) {
761 SkDebugf("fillRectToRect called with empty local matrix.\n");
762 return;
763 }
764 viewAndUnLocalMatrix.postConcat(viewMatrix);
765
766 SkPath path;
767 path.setIsVolatile(true);
768 path.addRect(localRect);
Brian Salomon82f44312017-01-11 13:42:54 -0500769 this->internalDrawPath(clip, std::move(paint), aa, viewAndUnLocalMatrix, path, GrStyle());
joshualittb6b513b2015-08-21 10:25:18 -0700770}
771
Brian Osman11052242016-10-27 14:47:55 -0400772void GrRenderTargetContext::fillRectWithLocalMatrix(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500773 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500774 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400775 const SkMatrix& viewMatrix,
776 const SkRect& rectToDraw,
777 const SkMatrix& localMatrix) {
joshualitt1de610a2016-01-06 08:26:09 -0800778 ASSERT_SINGLE_OWNER
joshualittb6b513b2015-08-21 10:25:18 -0700779 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700780 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400781 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "fillRectWithLocalMatrix", fContext);
joshualittb6b513b2015-08-21 10:25:18 -0700782
csmartdalton97f6cd52016-07-13 13:37:08 -0700783 SkRect croppedRect = rectToDraw;
robertphillips13a7eee2016-08-31 15:06:24 -0700784 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix, &croppedRect)) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700785 return;
786 }
787
Robert Phillips72152832017-01-25 17:31:35 -0500788 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -0700789
csmartdaltone0d36292016-07-29 08:14:20 -0700790 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport()) {
Robert Phillipse3302df2017-04-24 07:31:02 -0400791 gr_instanced::OpAllocator* oa = this->drawingManager()->instancingAllocator();
792 std::unique_ptr<GrDrawOp> op(oa->recordRect(croppedRect, viewMatrix, std::move(paint),
Brian Salomon54d212e2017-03-21 14:22:38 -0400793 localMatrix, aa, fInstancedPipelineInfo));
Brian Salomon42521e82016-12-07 16:44:58 -0500794 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -0400795 this->addDrawOp(clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -0700796 return;
797 }
798 }
799
Brian Salomon7c8460e2017-05-12 11:36:10 -0400800 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500801 if (GrAAType::kCoverage != aaType) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400802 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeNonAAFillWithLocalMatrix(
803 std::move(paint), viewMatrix, localMatrix, croppedRect, aaType);
804 this->addDrawOp(clip, std::move(op));
csmartdaltonfc49d562016-07-26 17:05:47 -0700805 return;
bsalomonc55271f2015-11-09 11:55:57 -0800806 }
robertphillips4bc31812016-03-01 12:22:49 -0800807
Brian Salomonbaaf4392017-06-15 09:59:23 -0400808 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeAAFillWithLocalMatrix(
809 std::move(paint), viewMatrix, localMatrix, croppedRect);
810 if (op) {
811 this->addDrawOp(clip, std::move(op));
csmartdaltonfc49d562016-07-26 17:05:47 -0700812 return;
813 }
814
815 SkMatrix viewAndUnLocalMatrix;
816 if (!localMatrix.invert(&viewAndUnLocalMatrix)) {
817 SkDebugf("fillRectWithLocalMatrix called with degenerate local matrix.\n");
818 return;
819 }
820 viewAndUnLocalMatrix.postConcat(viewMatrix);
821
822 SkPath path;
823 path.setIsVolatile(true);
824 path.addRect(rectToDraw);
825 path.transform(localMatrix);
Brian Salomon82f44312017-01-11 13:42:54 -0500826 this->internalDrawPath(clip, std::move(paint), aa, viewAndUnLocalMatrix, path, GrStyle());
robertphillipsea461502015-05-26 11:38:03 -0700827}
828
Brian Osman11052242016-10-27 14:47:55 -0400829void GrRenderTargetContext::drawVertices(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500830 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400831 const SkMatrix& viewMatrix,
Brian Osmanae0c50c2017-05-25 16:56:34 -0400832 sk_sp<SkVertices> vertices,
833 GrPrimitiveType* overridePrimType) {
Brian Salomon199fb872017-02-06 09:41:10 -0500834 ASSERT_SINGLE_OWNER
835 RETURN_IF_ABANDONED
836 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400837 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawVertices", fContext);
Brian Salomon199fb872017-02-06 09:41:10 -0500838
839 AutoCheckFlush acf(this->drawingManager());
840
841 SkASSERT(vertices);
Brian Salomonc2f42542017-07-12 14:11:22 -0400842 GrAAType aaType = this->chooseAAType(GrAA::kNo, GrAllowMixedSamples::kNo);
843 std::unique_ptr<GrDrawOp> op =
844 GrDrawVerticesOp::Make(std::move(paint), std::move(vertices), viewMatrix, aaType,
845 this->isGammaCorrect(), fColorXformFromSRGB, overridePrimType);
846 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -0700847}
848
849///////////////////////////////////////////////////////////////////////////////
850
Brian Osman11052242016-10-27 14:47:55 -0400851void GrRenderTargetContext::drawAtlas(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500852 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400853 const SkMatrix& viewMatrix,
854 int spriteCount,
855 const SkRSXform xform[],
856 const SkRect texRect[],
857 const SkColor colors[]) {
joshualitt1de610a2016-01-06 08:26:09 -0800858 ASSERT_SINGLE_OWNER
jvanverth31ff7622015-08-07 10:09:28 -0700859 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700860 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400861 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawAtlas", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700862
Robert Phillips72152832017-01-25 17:31:35 -0500863 AutoCheckFlush acf(this->drawingManager());
halcanary9d524f22016-03-29 09:03:52 -0700864
Brian Salomon0088f942017-07-12 11:51:27 -0400865 GrAAType aaType = this->chooseAAType(GrAA::kNo, GrAllowMixedSamples::kNo);
866 std::unique_ptr<GrDrawOp> op = GrDrawAtlasOp::Make(std::move(paint), viewMatrix, aaType,
867 spriteCount, xform, texRect, colors);
868 this->addDrawOp(clip, std::move(op));
jvanverth31ff7622015-08-07 10:09:28 -0700869}
870
871///////////////////////////////////////////////////////////////////////////////
872
Brian Osman11052242016-10-27 14:47:55 -0400873void GrRenderTargetContext::drawRRect(const GrClip& origClip,
Brian Salomon82f44312017-01-11 13:42:54 -0500874 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500875 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400876 const SkMatrix& viewMatrix,
877 const SkRRect& rrect,
878 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -0800879 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700880 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700881 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400882 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRRect", fContext);
robertphillipsea461502015-05-26 11:38:03 -0700883 if (rrect.isEmpty()) {
884 return;
885 }
886
bsalomon7f0d9f32016-08-15 14:49:10 -0700887 GrNoClip noclip;
888 const GrClip* clip = &origClip;
889#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
890 // The Android framework frequently clips rrects to themselves where the clip is non-aa and the
Brian Salomon42521e82016-12-07 16:44:58 -0500891 // draw is aa. Since our lower level clip code works from op bounds, which are SkRects, it
bsalomon7f0d9f32016-08-15 14:49:10 -0700892 // doesn't detect that the clip can be ignored (modulo antialiasing). The following test
893 // attempts to mitigate the stencil clip cost but will only help when the entire clip stack
894 // can be ignored. We'd prefer to fix this in the framework by removing the clips calls.
895 SkRRect devRRect;
896 if (rrect.transform(viewMatrix, &devRRect) && clip->quickContains(devRRect)) {
897 clip = &noclip;
898 }
899#endif
bsalomon6663acf2016-05-10 09:14:17 -0700900 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
ksakamotoec7f2ac2016-07-05 03:54:53 -0700901
Robert Phillips72152832017-01-25 17:31:35 -0500902 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -0700903 const SkStrokeRec stroke = style.strokeRec();
csmartdaltona7f29642016-07-07 08:49:11 -0700904
csmartdaltone0d36292016-07-29 08:14:20 -0700905 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport() &&
906 stroke.isFillStyle()) {
Robert Phillipse3302df2017-04-24 07:31:02 -0400907 gr_instanced::OpAllocator* oa = this->drawingManager()->instancingAllocator();
Brian Salomon54d212e2017-03-21 14:22:38 -0400908 std::unique_ptr<GrDrawOp> op(
Robert Phillipse3302df2017-04-24 07:31:02 -0400909 oa->recordRRect(rrect, viewMatrix, std::move(paint), aa, fInstancedPipelineInfo));
Brian Salomon42521e82016-12-07 16:44:58 -0500910 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -0400911 this->addDrawOp(*clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -0700912 return;
913 }
914 }
915
Brian Salomon7c8460e2017-05-12 11:36:10 -0400916 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500917 if (GrAAType::kCoverage == aaType) {
Brian Salomon94efbf52016-11-29 13:43:05 -0500918 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomon05441c42017-05-15 16:45:49 -0400919 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeRRectOp(std::move(paint),
Brian Salomon05441c42017-05-15 16:45:49 -0400920 viewMatrix,
921 rrect,
922 stroke,
923 shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -0500924 if (op) {
Brian Salomon05441c42017-05-15 16:45:49 -0400925 this->addDrawOp(*clip, std::move(op));
robertphillipsb56f9272016-02-25 11:03:52 -0800926 return;
927 }
robertphillipsea461502015-05-26 11:38:03 -0700928 }
robertphillipsb56f9272016-02-25 11:03:52 -0800929
930 SkPath path;
931 path.setIsVolatile(true);
932 path.addRRect(rrect);
Brian Salomon82f44312017-01-11 13:42:54 -0500933 this->internalDrawPath(*clip, std::move(paint), aa, viewMatrix, path, style);
robertphillipsea461502015-05-26 11:38:03 -0700934}
935
Jim Van Verthc5903412016-11-17 15:27:09 -0500936///////////////////////////////////////////////////////////////////////////////
937
Jim Van Verth3af1af92017-05-18 15:06:54 -0400938static SkPoint3 map(const SkMatrix& m, const SkPoint3& pt) {
939 SkPoint3 result;
940 m.mapXY(pt.fX, pt.fY, (SkPoint*)&result.fX);
941 result.fZ = pt.fZ;
942 return result;
943}
944
945bool GrRenderTargetContext::drawFastShadow(const GrClip& clip,
Brian Salomon05969092017-07-13 11:20:51 -0400946 GrColor color4ub,
Jim Van Verth3af1af92017-05-18 15:06:54 -0400947 const SkMatrix& viewMatrix,
948 const SkPath& path,
949 const SkDrawShadowRec& rec) {
Jim Van Verthc5903412016-11-17 15:27:09 -0500950 ASSERT_SINGLE_OWNER
Jim Van Verth3af1af92017-05-18 15:06:54 -0400951 if (this->drawingManager()->wasAbandoned()) {
952 return true;
953 }
Jim Van Verthc5903412016-11-17 15:27:09 -0500954 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400955 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFastShadow", fContext);
Jim Van Verth3af1af92017-05-18 15:06:54 -0400956
957 // check z plane
958 bool tiltZPlane = SkToBool(!SkScalarNearlyZero(rec.fZPlaneParams.fX) ||
959 !SkScalarNearlyZero(rec.fZPlaneParams.fY));
960 bool skipAnalytic = SkToBool(rec.fFlags & SkShadowFlags::kGeometricOnly_ShadowFlag);
961 if (tiltZPlane || skipAnalytic || !viewMatrix.rectStaysRect() || !viewMatrix.isSimilarity()) {
962 return false;
963 }
964
965 SkRRect rrect;
966 SkRect rect;
967 // we can only handle rects, circles, and rrects with circular corners
968 bool isRRect = path.isRRect(&rrect) && rrect.isSimpleCircular() &&
969 rrect.radii(SkRRect::kUpperLeft_Corner).fX > SK_ScalarNearlyZero;
970 if (!isRRect &&
971 path.isOval(&rect) && SkScalarNearlyEqual(rect.width(), rect.height()) &&
972 rect.width() > SK_ScalarNearlyZero) {
973 rrect.setOval(rect);
974 isRRect = true;
975 }
976 if (!isRRect && path.isRect(&rect)) {
977 rrect.setRect(rect);
978 isRRect = true;
979 }
980
981 if (!isRRect) {
982 return false;
983 }
984
Jim Van Verthc5903412016-11-17 15:27:09 -0500985 if (rrect.isEmpty()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -0400986 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -0500987 }
988
Robert Phillips72152832017-01-25 17:31:35 -0500989 AutoCheckFlush acf(this->drawingManager());
Jim Van Verthc5903412016-11-17 15:27:09 -0500990
Jim Van Verth3af1af92017-05-18 15:06:54 -0400991 // transform light
992 SkPoint3 devLightPos = map(viewMatrix, rec.fLightPos);
993
994 // 1/scale
995 SkScalar devToSrcScale = viewMatrix.isScaleTranslate() ?
996 SkScalarInvert(viewMatrix[SkMatrix::kMScaleX]) :
997 sk_float_rsqrt(viewMatrix[SkMatrix::kMScaleX] * viewMatrix[SkMatrix::kMScaleX] +
998 viewMatrix[SkMatrix::kMSkewX] * viewMatrix[SkMatrix::kMSkewX]);
999
1000 SkScalar occluderHeight = rec.fZPlaneParams.fZ;
Brian Salomon05969092017-07-13 11:20:51 -04001001 GrColor4f color = GrColor4f::FromGrColor(color4ub);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001002 bool transparent = SkToBool(rec.fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag);
Jim Van Verth34d6e4b2017-06-09 11:09:03 -04001003 bool tonalColor = SkToBool(rec.fFlags & SkShadowFlags::kTonalColor_ShadowFlag);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001004
1005 if (rec.fAmbientAlpha > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001006 SkScalar devSpaceInsetWidth = SkDrawShadowMetrics::AmbientBlurRadius(occluderHeight);
1007 const SkScalar umbraRecipAlpha = SkDrawShadowMetrics::AmbientRecipAlpha(occluderHeight);
1008 const SkScalar devSpaceAmbientBlur = devSpaceInsetWidth * umbraRecipAlpha;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001009
1010 // Outset the shadow rrect to the border of the penumbra
1011 SkScalar ambientPathOutset = devSpaceInsetWidth * devToSrcScale;
1012 SkRRect ambientRRect;
1013 SkRect outsetRect = rrect.rect().makeOutset(ambientPathOutset, ambientPathOutset);
1014 // If the rrect was an oval then its outset will also be one.
1015 // We set it explicitly to avoid errors.
1016 if (rrect.isOval()) {
1017 ambientRRect = SkRRect::MakeOval(outsetRect);
1018 } else {
1019 SkScalar outsetRad = rrect.getSimpleRadii().fX + ambientPathOutset;
1020 ambientRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1021 }
1022
Jim Van Verth34d6e4b2017-06-09 11:09:03 -04001023 GrColor ambientColor;
1024 if (tonalColor) {
1025 // with tonal color, the color only applies to the spot shadow
1026 ambientColor = GrColorPackRGBA(0, 0, 0, 255.999f*rec.fAmbientAlpha);
1027 } else {
1028 ambientColor = color.mulByScalar(rec.fAmbientAlpha).toGrColor();
1029 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001030 if (transparent) {
1031 // set a large inset to force a fill
1032 devSpaceInsetWidth = ambientRRect.width();
1033 }
1034 // the fraction of the blur we want to apply is devSpaceInsetWidth/devSpaceAmbientBlur,
Jim Van Verth1af03d42017-07-31 09:34:58 -04001035 // which is just 1/umbraRecipAlpha.
1036 SkScalar blurClamp = SkScalarInvert(umbraRecipAlpha);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001037
Brian Salomon05969092017-07-13 11:20:51 -04001038 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(ambientColor, viewMatrix,
1039 ambientRRect,
1040 devSpaceAmbientBlur,
1041 devSpaceInsetWidth,
1042 blurClamp);
1043 SkASSERT(op);
1044 this->addDrawOp(clip, std::move(op));
Jim Van Verthc5903412016-11-17 15:27:09 -05001045 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001046
1047 if (rec.fSpotAlpha > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001048 SkScalar devSpaceSpotBlur;
1049 SkScalar spotScale;
1050 SkVector spotOffset;
1051 SkDrawShadowMetrics::GetSpotParams(occluderHeight, devLightPos.fX, devLightPos.fY,
1052 devLightPos.fZ, rec.fLightRadius,
1053 &devSpaceSpotBlur, &spotScale, &spotOffset);
1054 // handle scale of radius due to CTM
Jim Van Verth3af1af92017-05-18 15:06:54 -04001055 const SkScalar srcSpaceSpotBlur = devSpaceSpotBlur * devToSrcScale;
1056
Jim Van Verth3af1af92017-05-18 15:06:54 -04001057 // Adjust translate for the effect of the scale.
1058 spotOffset.fX += spotScale*viewMatrix[SkMatrix::kMTransX];
1059 spotOffset.fY += spotScale*viewMatrix[SkMatrix::kMTransY];
1060 // This offset is in dev space, need to transform it into source space.
1061 SkMatrix ctmInverse;
1062 if (viewMatrix.invert(&ctmInverse)) {
1063 ctmInverse.mapPoints(&spotOffset, 1);
1064 } else {
1065 // Since the matrix is a similarity, this should never happen, but just in case...
1066 SkDebugf("Matrix is degenerate. Will not render spot shadow correctly!\n");
1067 SkASSERT(false);
1068 }
1069
1070 // Compute the transformed shadow rrect
1071 SkRRect spotShadowRRect;
1072 SkMatrix shadowTransform;
1073 shadowTransform.setScaleTranslate(spotScale, spotScale, spotOffset.fX, spotOffset.fY);
1074 rrect.transform(shadowTransform, &spotShadowRRect);
1075 SkScalar spotRadius = spotShadowRRect.getSimpleRadii().fX;
1076
1077 // Compute the insetWidth
Jim Van Verth1af03d42017-07-31 09:34:58 -04001078 SkScalar blurOutset = srcSpaceSpotBlur;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001079 SkScalar insetWidth = blurOutset;
1080 if (transparent) {
1081 // If transparent, just do a fill
1082 insetWidth += spotShadowRRect.width();
1083 } else {
1084 // For shadows, instead of using a stroke we specify an inset from the penumbra
1085 // border. We want to extend this inset area so that it meets up with the caster
1086 // geometry. The inset geometry will by default already be inset by the blur width.
1087 //
1088 // We compare the min and max corners inset by the radius between the original
1089 // rrect and the shadow rrect. The distance between the two plus the difference
1090 // between the scaled radius and the original radius gives the distance from the
1091 // transformed shadow shape to the original shape in that corner. The max
1092 // of these gives the maximum distance we need to cover.
1093 //
1094 // Since we are outsetting by 1/2 the blur distance, we just add the maxOffset to
1095 // that to get the full insetWidth.
1096 SkScalar maxOffset;
1097 if (rrect.isRect()) {
1098 // Manhattan distance works better for rects
1099 maxOffset = SkTMax(SkTMax(SkTAbs(spotShadowRRect.rect().fLeft -
1100 rrect.rect().fLeft),
1101 SkTAbs(spotShadowRRect.rect().fTop -
1102 rrect.rect().fTop)),
1103 SkTMax(SkTAbs(spotShadowRRect.rect().fRight -
1104 rrect.rect().fRight),
1105 SkTAbs(spotShadowRRect.rect().fBottom -
1106 rrect.rect().fBottom)));
1107 } else {
1108 SkScalar dr = spotRadius - rrect.getSimpleRadii().fX;
1109 SkPoint upperLeftOffset = SkPoint::Make(spotShadowRRect.rect().fLeft -
1110 rrect.rect().fLeft + dr,
1111 spotShadowRRect.rect().fTop -
1112 rrect.rect().fTop + dr);
1113 SkPoint lowerRightOffset = SkPoint::Make(spotShadowRRect.rect().fRight -
1114 rrect.rect().fRight - dr,
1115 spotShadowRRect.rect().fBottom -
1116 rrect.rect().fBottom - dr);
1117 maxOffset = SkScalarSqrt(SkTMax(upperLeftOffset.lengthSqd(),
1118 lowerRightOffset.lengthSqd())) + dr;
1119 }
1120 insetWidth += maxOffset;
1121 }
1122
1123 // Outset the shadow rrect to the border of the penumbra
1124 SkRect outsetRect = spotShadowRRect.rect().makeOutset(blurOutset, blurOutset);
1125 if (spotShadowRRect.isOval()) {
1126 spotShadowRRect = SkRRect::MakeOval(outsetRect);
1127 } else {
1128 SkScalar outsetRad = spotRadius + blurOutset;
1129 spotShadowRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1130 }
1131
Jim Van Verth34d6e4b2017-06-09 11:09:03 -04001132 GrColor spotColor;
1133 if (tonalColor) {
1134 SkScalar colorScale;
1135 SkScalar tonalAlpha;
1136 SkShadowUtils::ComputeTonalColorParams(color.fRGBA[0], color.fRGBA[1],
1137 color.fRGBA[2], rec.fSpotAlpha,
1138 &colorScale, &tonalAlpha);
1139 color.fRGBA[0] *= colorScale;
1140 color.fRGBA[1] *= colorScale;
1141 color.fRGBA[2] *= colorScale;
1142 color.fRGBA[3] = tonalAlpha;
1143 spotColor = color.toGrColor();
1144 } else {
1145 spotColor = color.mulByScalar(rec.fSpotAlpha).toGrColor();
1146 }
1147
Brian Salomon05969092017-07-13 11:20:51 -04001148 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(spotColor, viewMatrix,
1149 spotShadowRRect,
Jim Van Verth1af03d42017-07-31 09:34:58 -04001150 2.0f * devSpaceSpotBlur,
Brian Salomon05969092017-07-13 11:20:51 -04001151 insetWidth);
1152 SkASSERT(op);
1153 this->addDrawOp(clip, std::move(op));
Jim Van Verth3af1af92017-05-18 15:06:54 -04001154 }
1155
1156 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001157}
1158
1159///////////////////////////////////////////////////////////////////////////////
1160
Brian Osman11052242016-10-27 14:47:55 -04001161bool GrRenderTargetContext::drawFilledDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001162 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001163 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001164 const SkMatrix& viewMatrix,
1165 const SkRRect& origOuter,
1166 const SkRRect& origInner) {
robertphillips00095892016-02-29 13:50:40 -08001167 SkASSERT(!origInner.isEmpty());
1168 SkASSERT(!origOuter.isEmpty());
1169
csmartdaltone0d36292016-07-29 08:14:20 -07001170 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport()) {
Robert Phillipse3302df2017-04-24 07:31:02 -04001171 gr_instanced::OpAllocator* oa = this->drawingManager()->instancingAllocator();
1172 std::unique_ptr<GrDrawOp> op(oa->recordDRRect(
Brian Salomon54d212e2017-03-21 14:22:38 -04001173 origOuter, origInner, viewMatrix, std::move(paint), aa, fInstancedPipelineInfo));
Brian Salomon42521e82016-12-07 16:44:58 -05001174 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -04001175 this->addDrawOp(clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -07001176 return true;
1177 }
1178 }
1179
Brian Salomon7c8460e2017-05-12 11:36:10 -04001180 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
robertphillips00095892016-02-29 13:50:40 -08001181
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001182 GrPrimitiveEdgeType innerEdgeType, outerEdgeType;
1183 if (GrAAType::kCoverage == aaType) {
1184 innerEdgeType = kInverseFillAA_GrProcessorEdgeType;
1185 outerEdgeType = kFillAA_GrProcessorEdgeType;
1186 } else {
1187 innerEdgeType = kInverseFillBW_GrProcessorEdgeType;
1188 outerEdgeType = kFillBW_GrProcessorEdgeType;
1189 }
robertphillips00095892016-02-29 13:50:40 -08001190
1191 SkTCopyOnFirstWrite<SkRRect> inner(origInner), outer(origOuter);
1192 SkMatrix inverseVM;
1193 if (!viewMatrix.isIdentity()) {
1194 if (!origInner.transform(viewMatrix, inner.writable())) {
1195 return false;
1196 }
1197 if (!origOuter.transform(viewMatrix, outer.writable())) {
1198 return false;
1199 }
1200 if (!viewMatrix.invert(&inverseVM)) {
1201 return false;
1202 }
1203 } else {
1204 inverseVM.reset();
halcanary9d524f22016-03-29 09:03:52 -07001205 }
robertphillips00095892016-02-29 13:50:40 -08001206
robertphillips00095892016-02-29 13:50:40 -08001207 // TODO these need to be a geometry processors
Brian Salomonaff329b2017-08-11 09:40:37 -04001208 std::unique_ptr<GrFragmentProcessor> innerEffect(GrRRectEffect::Make(innerEdgeType, *inner));
robertphillips00095892016-02-29 13:50:40 -08001209 if (!innerEffect) {
1210 return false;
1211 }
1212
Brian Salomonaff329b2017-08-11 09:40:37 -04001213 std::unique_ptr<GrFragmentProcessor> outerEffect(GrRRectEffect::Make(outerEdgeType, *outer));
robertphillips00095892016-02-29 13:50:40 -08001214 if (!outerEffect) {
1215 return false;
1216 }
1217
Brian Salomon82f44312017-01-11 13:42:54 -05001218 paint.addCoverageFragmentProcessor(std::move(innerEffect));
1219 paint.addCoverageFragmentProcessor(std::move(outerEffect));
robertphillips00095892016-02-29 13:50:40 -08001220
1221 SkRect bounds = outer->getBounds();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001222 if (GrAAType::kCoverage == aaType) {
robertphillips00095892016-02-29 13:50:40 -08001223 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1224 }
halcanary9d524f22016-03-29 09:03:52 -07001225
Brian Salomon82f44312017-01-11 13:42:54 -05001226 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), bounds,
1227 inverseVM);
robertphillips00095892016-02-29 13:50:40 -08001228 return true;
1229}
1230
Brian Osman11052242016-10-27 14:47:55 -04001231void GrRenderTargetContext::drawDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001232 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001233 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001234 const SkMatrix& viewMatrix,
1235 const SkRRect& outer,
1236 const SkRRect& inner) {
robertphillips00095892016-02-29 13:50:40 -08001237 ASSERT_SINGLE_OWNER
1238 RETURN_IF_ABANDONED
1239 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001240 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawDRRect", fContext);
robertphillips00095892016-02-29 13:50:40 -08001241
1242 SkASSERT(!outer.isEmpty());
1243 SkASSERT(!inner.isEmpty());
1244
Robert Phillips72152832017-01-25 17:31:35 -05001245 AutoCheckFlush acf(this->drawingManager());
robertphillips00095892016-02-29 13:50:40 -08001246
Brian Salomon82f44312017-01-11 13:42:54 -05001247 if (this->drawFilledDRRect(clip, std::move(paint), aa, viewMatrix, outer, inner)) {
robertphillips00095892016-02-29 13:50:40 -08001248 return;
1249 }
1250
1251 SkPath path;
1252 path.setIsVolatile(true);
1253 path.addRRect(inner);
1254 path.addRRect(outer);
1255 path.setFillType(SkPath::kEvenOdd_FillType);
1256
Brian Salomon82f44312017-01-11 13:42:54 -05001257 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, GrStyle::SimpleFill());
robertphillips00095892016-02-29 13:50:40 -08001258}
1259
robertphillipsea461502015-05-26 11:38:03 -07001260///////////////////////////////////////////////////////////////////////////////
1261
msarettcc319b92016-08-25 18:07:18 -07001262static inline bool is_int(float x) {
1263 return x == (float) sk_float_round2int(x);
1264}
1265
Brian Osman11052242016-10-27 14:47:55 -04001266void GrRenderTargetContext::drawRegion(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001267 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001268 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001269 const SkMatrix& viewMatrix,
1270 const SkRegion& region,
Stan Iliev73d8fd92017-08-02 15:36:24 -04001271 const GrStyle& style,
1272 const GrUserStencilSettings* ss) {
msarettcc319b92016-08-25 18:07:18 -07001273 ASSERT_SINGLE_OWNER
1274 RETURN_IF_ABANDONED
1275 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001276 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRegion", fContext);
msarettcc319b92016-08-25 18:07:18 -07001277
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001278 if (GrAA::kYes == aa) {
Brian Salomonfc527d22016-12-14 21:07:01 -05001279 // GrRegionOp performs no antialiasing but is much faster, so here we check the matrix
Brian Salomonc57c7c92016-12-06 14:47:34 -05001280 // to see whether aa is really required.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001281 if (!SkToBool(viewMatrix.getType() & ~(SkMatrix::kTranslate_Mask)) &&
1282 is_int(viewMatrix.getTranslateX()) &&
1283 is_int(viewMatrix.getTranslateY())) {
1284 aa = GrAA::kNo;
1285 }
Brian Salomonc57c7c92016-12-06 14:47:34 -05001286 }
msarettcc319b92016-08-25 18:07:18 -07001287 bool complexStyle = !style.isSimpleFill();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001288 if (complexStyle || GrAA::kYes == aa) {
msarettcc319b92016-08-25 18:07:18 -07001289 SkPath path;
1290 region.getBoundaryPath(&path);
Brian Salomon82f44312017-01-11 13:42:54 -05001291 return this->drawPath(clip, std::move(paint), aa, viewMatrix, path, style);
msarettcc319b92016-08-25 18:07:18 -07001292 }
1293
Brian Salomonf0366322017-07-11 15:53:05 -04001294 GrAAType aaType = this->chooseAAType(GrAA::kNo, GrAllowMixedSamples::kNo);
Stan Iliev73d8fd92017-08-02 15:36:24 -04001295 std::unique_ptr<GrDrawOp> op = GrRegionOp::Make(std::move(paint), viewMatrix, region, aaType,
1296 ss);
Brian Salomonf0366322017-07-11 15:53:05 -04001297 this->addDrawOp(clip, std::move(op));
msarettcc319b92016-08-25 18:07:18 -07001298}
1299
Brian Osman11052242016-10-27 14:47:55 -04001300void GrRenderTargetContext::drawOval(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001301 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001302 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001303 const SkMatrix& viewMatrix,
1304 const SkRect& oval,
1305 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001306 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001307 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001308 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001309 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawOval", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -07001310
robertphillipsea461502015-05-26 11:38:03 -07001311 if (oval.isEmpty()) {
1312 return;
1313 }
1314
bsalomon6663acf2016-05-10 09:14:17 -07001315 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
robertphillipsea461502015-05-26 11:38:03 -07001316
Robert Phillips72152832017-01-25 17:31:35 -05001317 AutoCheckFlush acf(this->drawingManager());
bsalomon6663acf2016-05-10 09:14:17 -07001318 const SkStrokeRec& stroke = style.strokeRec();
csmartdaltona7f29642016-07-07 08:49:11 -07001319
csmartdaltone0d36292016-07-29 08:14:20 -07001320 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport() &&
1321 stroke.isFillStyle()) {
Robert Phillipse3302df2017-04-24 07:31:02 -04001322 gr_instanced::OpAllocator* oa = this->drawingManager()->instancingAllocator();
Brian Salomon54d212e2017-03-21 14:22:38 -04001323 std::unique_ptr<GrDrawOp> op(
Robert Phillipse3302df2017-04-24 07:31:02 -04001324 oa->recordOval(oval, viewMatrix, std::move(paint), aa, fInstancedPipelineInfo));
Brian Salomon42521e82016-12-07 16:44:58 -05001325 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -04001326 this->addDrawOp(clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -07001327 return;
1328 }
1329 }
1330
Brian Salomon7c8460e2017-05-12 11:36:10 -04001331 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001332 if (GrAAType::kCoverage == aaType) {
Brian Salomon94efbf52016-11-29 13:43:05 -05001333 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomon05441c42017-05-15 16:45:49 -04001334 std::unique_ptr<GrDrawOp> op =
1335 GrOvalOpFactory::MakeOvalOp(std::move(paint), viewMatrix, oval, stroke, shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -05001336 if (op) {
Brian Salomon05441c42017-05-15 16:45:49 -04001337 this->addDrawOp(clip, std::move(op));
robertphillipsb56f9272016-02-25 11:03:52 -08001338 return;
1339 }
robertphillipsea461502015-05-26 11:38:03 -07001340 }
robertphillipsb56f9272016-02-25 11:03:52 -08001341
1342 SkPath path;
1343 path.setIsVolatile(true);
1344 path.addOval(oval);
Brian Salomon82f44312017-01-11 13:42:54 -05001345 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, style);
robertphillipsea461502015-05-26 11:38:03 -07001346}
1347
Brian Osman11052242016-10-27 14:47:55 -04001348void GrRenderTargetContext::drawArc(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001349 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001350 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001351 const SkMatrix& viewMatrix,
1352 const SkRect& oval,
1353 SkScalar startAngle,
1354 SkScalar sweepAngle,
1355 bool useCenter,
1356 const GrStyle& style) {
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001357 ASSERT_SINGLE_OWNER
1358 RETURN_IF_ABANDONED
1359 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001360 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawArc", fContext);
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001361
1362 AutoCheckFlush acf(this->drawingManager());
1363
Brian Salomon7c8460e2017-05-12 11:36:10 -04001364 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001365 if (GrAAType::kCoverage == aaType) {
Brian Salomon94efbf52016-11-29 13:43:05 -05001366 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomon05441c42017-05-15 16:45:49 -04001367 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeArcOp(std::move(paint),
1368 viewMatrix,
1369 oval,
1370 startAngle,
1371 sweepAngle,
1372 useCenter,
1373 style,
1374 shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -05001375 if (op) {
Brian Salomon05441c42017-05-15 16:45:49 -04001376 this->addDrawOp(clip, std::move(op));
bsalomon4f3a0ca2016-08-22 13:14:26 -07001377 return;
1378 }
1379 }
1380 SkPath path;
bsalomon21af9ca2016-08-25 12:29:23 -07001381 SkPathPriv::CreateDrawArcPath(&path, oval, startAngle, sweepAngle, useCenter,
1382 style.isSimpleFill());
Brian Salomon82f44312017-01-11 13:42:54 -05001383 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, style);
bsalomon4f3a0ca2016-08-22 13:14:26 -07001384}
1385
Brian Osman11052242016-10-27 14:47:55 -04001386void GrRenderTargetContext::drawImageLattice(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001387 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001388 const SkMatrix& viewMatrix,
1389 int imageWidth,
1390 int imageHeight,
1391 std::unique_ptr<SkLatticeIter> iter,
1392 const SkRect& dst) {
joshualitt1de610a2016-01-06 08:26:09 -08001393 ASSERT_SINGLE_OWNER
joshualitt33a5fce2015-11-18 13:28:51 -08001394 RETURN_IF_ABANDONED
1395 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001396 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawImageLattice", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001397
Robert Phillips72152832017-01-25 17:31:35 -05001398 AutoCheckFlush acf(this->drawingManager());
joshualitt33a5fce2015-11-18 13:28:51 -08001399
Brian Salomon815486c2017-07-11 08:52:13 -04001400 std::unique_ptr<GrDrawOp> op = GrLatticeOp::MakeNonAA(std::move(paint), viewMatrix, imageWidth,
1401 imageHeight, std::move(iter), dst);
1402 this->addDrawOp(clip, std::move(op));
joshualitt33a5fce2015-11-18 13:28:51 -08001403}
1404
Greg Daniel51316782017-08-02 15:10:09 +00001405GrSemaphoresSubmitted GrRenderTargetContext::prepareForExternalIO(
1406 int numSemaphores, GrBackendSemaphore backendSemaphores[]) {
robertphillips8c523e02016-07-26 07:41:00 -07001407 ASSERT_SINGLE_OWNER
Greg Daniel51316782017-08-02 15:10:09 +00001408 if (this->drawingManager()->wasAbandoned()) { return GrSemaphoresSubmitted::kNo; }
robertphillips8c523e02016-07-26 07:41:00 -07001409 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001410 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "prepareForExternalIO", fContext);
robertphillips8c523e02016-07-26 07:41:00 -07001411
Greg Daniel51316782017-08-02 15:10:09 +00001412 return this->drawingManager()->prepareSurfaceForExternalIO(fRenderTargetProxy.get(),
1413 numSemaphores,
1414 backendSemaphores);
Greg Daniela5cb7812017-06-16 09:45:32 -04001415}
1416
Greg Danielc64ee462017-06-15 16:59:49 -04001417bool GrRenderTargetContext::waitOnSemaphores(int numSemaphores,
Greg Daniela5cb7812017-06-16 09:45:32 -04001418 const GrBackendSemaphore* waitSemaphores) {
1419 ASSERT_SINGLE_OWNER
Greg Danielc64ee462017-06-15 16:59:49 -04001420 RETURN_FALSE_IF_ABANDONED
Greg Daniela5cb7812017-06-16 09:45:32 -04001421 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001422 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "waitOnSemaphores", fContext);
Greg Daniela5cb7812017-06-16 09:45:32 -04001423
1424 AutoCheckFlush acf(this->drawingManager());
1425
Greg Danielc64ee462017-06-15 16:59:49 -04001426 if (numSemaphores && !this->caps()->fenceSyncSupport()) {
1427 return false;
1428 }
1429
Greg Daniela5cb7812017-06-16 09:45:32 -04001430 SkTArray<sk_sp<GrSemaphore>> semaphores(numSemaphores);
1431 for (int i = 0; i < numSemaphores; ++i) {
1432 sk_sp<GrSemaphore> sema = fContext->resourceProvider()->wrapBackendSemaphore(
1433 waitSemaphores[i], kAdopt_GrWrapOwnership);
1434 std::unique_ptr<GrOp> waitOp(GrSemaphoreOp::MakeWait(sema, fRenderTargetProxy.get()));
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001435 this->getRTOpList()->addOp(std::move(waitOp), *this->caps());
Greg Daniela5cb7812017-06-16 09:45:32 -04001436 }
Greg Danielc64ee462017-06-15 16:59:49 -04001437 return true;
robertphillips8c523e02016-07-26 07:41:00 -07001438}
joshualitt33a5fce2015-11-18 13:28:51 -08001439
Robert Phillips65a88fa2017-08-08 08:36:22 -04001440void GrRenderTargetContext::insertEventMarker(const SkString& str) {
1441 std::unique_ptr<GrOp> op(GrDebugMarkerOp::Make(fRenderTargetProxy.get(), str));
1442 this->getRTOpList()->addOp(std::move(op), *this->caps());
1443}
1444
1445
robertphillipsea461502015-05-26 11:38:03 -07001446// Can 'path' be drawn as a pair of filled nested rectangles?
bsalomon6663acf2016-05-10 09:14:17 -07001447static bool fills_as_nested_rects(const SkMatrix& viewMatrix, const SkPath& path, SkRect rects[2]) {
robertphillipsea461502015-05-26 11:38:03 -07001448
1449 if (path.isInverseFillType()) {
1450 return false;
1451 }
1452
1453 // TODO: this restriction could be lifted if we were willing to apply
1454 // the matrix to all the points individually rather than just to the rect
robertphillips0e7029e2015-11-30 05:45:06 -08001455 if (!viewMatrix.rectStaysRect()) {
robertphillipsea461502015-05-26 11:38:03 -07001456 return false;
1457 }
1458
1459 SkPath::Direction dirs[2];
1460 if (!path.isNestedFillRects(rects, dirs)) {
1461 return false;
1462 }
1463
1464 if (SkPath::kWinding_FillType == path.getFillType() && dirs[0] == dirs[1]) {
1465 // The two rects need to be wound opposite to each other
1466 return false;
1467 }
1468
1469 // Right now, nested rects where the margin is not the same width
1470 // all around do not render correctly
1471 const SkScalar* outer = rects[0].asScalars();
1472 const SkScalar* inner = rects[1].asScalars();
1473
1474 bool allEq = true;
1475
1476 SkScalar margin = SkScalarAbs(outer[0] - inner[0]);
1477 bool allGoE1 = margin >= SK_Scalar1;
1478
1479 for (int i = 1; i < 4; ++i) {
1480 SkScalar temp = SkScalarAbs(outer[i] - inner[i]);
1481 if (temp < SK_Scalar1) {
1482 allGoE1 = false;
1483 }
1484 if (!SkScalarNearlyEqual(margin, temp)) {
1485 allEq = false;
1486 }
1487 }
1488
1489 return allEq || allGoE1;
1490}
1491
Brian Osman11052242016-10-27 14:47:55 -04001492void GrRenderTargetContext::drawPath(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001493 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001494 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001495 const SkMatrix& viewMatrix,
1496 const SkPath& path,
1497 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001498 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001499 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001500 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001501 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawPath", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -07001502
robertphillipsea461502015-05-26 11:38:03 -07001503 if (path.isEmpty()) {
1504 if (path.isInverseFillType()) {
Brian Salomon82f44312017-01-11 13:42:54 -05001505 this->drawPaint(clip, std::move(paint), viewMatrix);
robertphillipsea461502015-05-26 11:38:03 -07001506 }
1507 return;
1508 }
1509
Robert Phillips72152832017-01-25 17:31:35 -05001510 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -07001511
Brian Salomon7c8460e2017-05-12 11:36:10 -04001512 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001513 if (GrAAType::kCoverage == aaType && !style.pathEffect()) {
bsalomon6663acf2016-05-10 09:14:17 -07001514 if (style.isSimpleFill() && !path.isConvex()) {
robertphillipsea461502015-05-26 11:38:03 -07001515 // Concave AA paths are expensive - try to avoid them for special cases
1516 SkRect rects[2];
1517
bsalomon6663acf2016-05-10 09:14:17 -07001518 if (fills_as_nested_rects(viewMatrix, path, rects)) {
Brian Salomonbaaf4392017-06-15 09:59:23 -04001519 std::unique_ptr<GrDrawOp> op =
1520 GrRectOpFactory::MakeAAFillNestedRects(std::move(paint), viewMatrix, rects);
Brian Salomon5f970fe2017-06-16 17:30:59 -04001521 if (op) {
1522 this->addDrawOp(clip, std::move(op));
bsalomon40ef4852016-05-02 13:22:13 -07001523 }
Brian Salomon5f970fe2017-06-16 17:30:59 -04001524 // A null return indicates that there is nothing to draw in this case.
1525 return;
robertphillipsea461502015-05-26 11:38:03 -07001526 }
1527 }
1528 SkRect ovalRect;
1529 bool isOval = path.isOval(&ovalRect);
1530
1531 if (isOval && !path.isInverseFillType()) {
Brian Salomon94efbf52016-11-29 13:43:05 -05001532 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomon05441c42017-05-15 16:45:49 -04001533 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeOvalOp(
1534 std::move(paint), viewMatrix, ovalRect, style.strokeRec(), shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -05001535 if (op) {
Brian Salomon05441c42017-05-15 16:45:49 -04001536 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -07001537 return;
1538 }
1539 }
1540 }
robertphillips4bc31812016-03-01 12:22:49 -08001541
1542 // Note that internalDrawPath may sw-rasterize the path into a scratch texture.
1543 // Scratch textures can be recycled after they are returned to the texture
1544 // cache. This presents a potential hazard for buffered drawing. However,
1545 // the writePixels that uploads to the scratch will perform a flush so we're
1546 // OK.
Brian Salomon82f44312017-01-11 13:42:54 -05001547 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, style);
robertphillipsea461502015-05-26 11:38:03 -07001548}
1549
Brian Osman11052242016-10-27 14:47:55 -04001550bool GrRenderTargetContextPriv::drawAndStencilPath(const GrClip& clip,
1551 const GrUserStencilSettings* ss,
1552 SkRegion::Op op,
1553 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001554 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001555 const SkMatrix& viewMatrix,
1556 const SkPath& path) {
robertphillips391395d2016-03-02 09:26:36 -08001557 ASSERT_SINGLE_OWNER_PRIV
1558 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -04001559 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001560 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawAndStencilPath",
1561 fRenderTargetContext->fContext);
robertphillips391395d2016-03-02 09:26:36 -08001562
1563 if (path.isEmpty() && path.isInverseFillType()) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001564 this->drawAndStencilRect(clip, ss, op, invert, GrAA::kNo, SkMatrix::I(),
Brian Osman11052242016-10-27 14:47:55 -04001565 SkRect::MakeIWH(fRenderTargetContext->width(),
1566 fRenderTargetContext->height()));
robertphillips391395d2016-03-02 09:26:36 -08001567 return true;
1568 }
1569
Robert Phillips72152832017-01-25 17:31:35 -05001570 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -08001571
1572 // An Assumption here is that path renderer would use some form of tweaking
1573 // the src color (either the input alpha or in the frag shader) to implement
1574 // aa. If we have some future driver-mojo path AA that can do the right
1575 // thing WRT to the blend then we'll need some query on the PR.
Brian Salomon7c8460e2017-05-12 11:36:10 -04001576 GrAAType aaType = fRenderTargetContext->chooseAAType(aa, GrAllowMixedSamples::kNo);
robertphillips976f5f02016-06-03 10:59:20 -07001577 bool hasUserStencilSettings = !ss->isUnused();
robertphillips391395d2016-03-02 09:26:36 -08001578
bsalomon8acedde2016-06-24 10:42:16 -07001579 GrShape shape(path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -08001580 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Eric Karl5c779752017-05-08 12:02:07 -07001581 canDrawArgs.fCaps = fRenderTargetContext->drawingManager()->getContext()->caps();
robertphillips391395d2016-03-02 09:26:36 -08001582 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07001583 canDrawArgs.fShape = &shape;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001584 canDrawArgs.fAAType = aaType;
cdalton93a379b2016-05-11 13:58:08 -07001585 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips391395d2016-03-02 09:26:36 -08001586
1587 // Don't allow the SW renderer
Robert Phillips72152832017-01-25 17:31:35 -05001588 GrPathRenderer* pr = fRenderTargetContext->drawingManager()->getPathRenderer(
Brian Salomon36aa1762016-12-10 13:24:02 -05001589 canDrawArgs, false, GrPathRendererChain::DrawType::kStencilAndColor);
robertphillips391395d2016-03-02 09:26:36 -08001590 if (!pr) {
1591 return false;
1592 }
1593
1594 GrPaint paint;
1595 paint.setCoverageSetOpXPFactory(op, invert);
1596
Brian Salomon82f44312017-01-11 13:42:54 -05001597 GrPathRenderer::DrawPathArgs args{
Robert Phillips256c37b2017-03-01 14:32:46 -05001598 fRenderTargetContext->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05001599 std::move(paint),
1600 ss,
1601 fRenderTargetContext,
1602 &clip,
1603 &viewMatrix,
1604 &shape,
1605 aaType,
1606 fRenderTargetContext->isGammaCorrect()};
robertphillips391395d2016-03-02 09:26:36 -08001607 pr->drawPath(args);
1608 return true;
1609}
1610
Brian Osman11052242016-10-27 14:47:55 -04001611SkBudgeted GrRenderTargetContextPriv::isBudgeted() const {
robertphillips714712b2016-08-04 06:20:45 -07001612 ASSERT_SINGLE_OWNER_PRIV
1613
Brian Osman11052242016-10-27 14:47:55 -04001614 if (fRenderTargetContext->wasAbandoned()) {
robertphillips714712b2016-08-04 06:20:45 -07001615 return SkBudgeted::kNo;
1616 }
1617
Brian Osman11052242016-10-27 14:47:55 -04001618 SkDEBUGCODE(fRenderTargetContext->validate();)
robertphillips714712b2016-08-04 06:20:45 -07001619
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001620 return fRenderTargetContext->fRenderTargetProxy->isBudgeted();
robertphillips714712b2016-08-04 06:20:45 -07001621}
1622
Brian Osman11052242016-10-27 14:47:55 -04001623void GrRenderTargetContext::internalDrawPath(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001624 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001625 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001626 const SkMatrix& viewMatrix,
1627 const SkPath& path,
1628 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001629 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001630 RETURN_IF_ABANDONED
Brian Salomondcbb9d92017-07-19 10:53:20 -04001631 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "internalDrawPath", fContext);
1632
bsalomon8acedde2016-06-24 10:42:16 -07001633 SkASSERT(!path.isEmpty());
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001634 GrShape shape;
Brian Salomon7c8460e2017-05-12 11:36:10 -04001635 // NVPR cannot handle hairlines, so this would get picked up by a different stencil and
1636 // cover path renderer (i.e. default path renderer). The hairline renderer produces much
1637 // smoother hairlines than MSAA.
1638 GrAllowMixedSamples allowMixedSamples =
1639 style.isSimpleHairline() ? GrAllowMixedSamples::kNo : GrAllowMixedSamples::kYes;
1640 GrAAType aaType = this->chooseAAType(aa, allowMixedSamples);
robertphillips68737822015-10-29 12:12:21 -07001641 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Eric Karl5c779752017-05-08 12:02:07 -07001642 canDrawArgs.fCaps = this->drawingManager()->getContext()->caps();
robertphillips68737822015-10-29 12:12:21 -07001643 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07001644 canDrawArgs.fShape = &shape;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001645 canDrawArgs.fHasUserStencilSettings = false;
robertphillips68737822015-10-29 12:12:21 -07001646
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001647 GrPathRenderer* pr;
Brian Salomon82125e92016-12-10 09:35:48 -05001648 static constexpr GrPathRendererChain::DrawType kType = GrPathRendererChain::DrawType::kColor;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001649 do {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001650 shape = GrShape(path, style);
bsalomon8acedde2016-06-24 10:42:16 -07001651 if (shape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001652 return;
1653 }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001654
1655 canDrawArgs.fAAType = aaType;
1656
1657 // Try a 1st time without applying any of the style to the geometry (and barring sw)
Robert Phillips72152832017-01-25 17:31:35 -05001658 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001659 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
1660
1661 if (!pr && shape.style().pathEffect()) {
1662 // It didn't work above, so try again with the path effect applied.
1663 shape = shape.applyStyle(GrStyle::Apply::kPathEffectOnly, styleScale);
bsalomon8acedde2016-06-24 10:42:16 -07001664 if (shape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001665 return;
1666 }
Robert Phillips72152832017-01-25 17:31:35 -05001667 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
bsalomon6663acf2016-05-10 09:14:17 -07001668 }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001669 if (!pr) {
1670 if (shape.style().applies()) {
1671 shape = shape.applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec, styleScale);
1672 if (shape.isEmpty()) {
1673 return;
1674 }
1675 }
1676 // This time, allow SW renderer
Robert Phillips72152832017-01-25 17:31:35 -05001677 pr = this->drawingManager()->getPathRenderer(canDrawArgs, true, kType);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001678 }
Brian Salomon0abc8b42016-12-13 10:22:54 -05001679 if (!pr && GrAATypeIsHW(aaType)) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001680 // There are exceptional cases where we may wind up falling back to coverage based AA
1681 // when the target is MSAA (e.g. through disabling path renderers via GrContextOptions).
1682 aaType = GrAAType::kCoverage;
1683 } else {
1684 break;
1685 }
1686 } while(true);
robertphillipsea461502015-05-26 11:38:03 -07001687
bsalomon8acedde2016-06-24 10:42:16 -07001688 if (!pr) {
robertphillipsea461502015-05-26 11:38:03 -07001689#ifdef SK_DEBUG
1690 SkDebugf("Unable to find path renderer compatible with path.\n");
1691#endif
1692 return;
1693 }
1694
Robert Phillips256c37b2017-03-01 14:32:46 -05001695 GrPathRenderer::DrawPathArgs args{this->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05001696 std::move(paint),
1697 &GrUserStencilSettings::kUnused,
1698 this,
1699 &clip,
1700 &viewMatrix,
1701 &shape,
1702 aaType,
1703 this->isGammaCorrect()};
bsalomon0aff2fa2015-07-31 06:48:27 -07001704 pr->drawPath(args);
robertphillipsea461502015-05-26 11:38:03 -07001705}
1706
Brian Salomon467921e2017-03-06 16:17:12 -05001707static void op_bounds(SkRect* bounds, const GrOp* op) {
1708 *bounds = op->bounds();
1709 if (op->hasZeroArea()) {
1710 if (op->hasAABloat()) {
1711 bounds->outset(0.5f, 0.5f);
1712 } else {
1713 // We don't know which way the particular GPU will snap lines or points at integer
1714 // coords. So we ensure that the bounds is large enough for either snap.
1715 SkRect before = *bounds;
1716 bounds->roundOut(bounds);
1717 if (bounds->fLeft == before.fLeft) {
1718 bounds->fLeft -= 1;
1719 }
1720 if (bounds->fTop == before.fTop) {
1721 bounds->fTop -= 1;
1722 }
1723 if (bounds->fRight == before.fRight) {
1724 bounds->fRight += 1;
1725 }
1726 if (bounds->fBottom == before.fBottom) {
1727 bounds->fBottom += 1;
1728 }
1729 }
1730 }
1731}
1732
Brian Salomon54d212e2017-03-21 14:22:38 -04001733uint32_t GrRenderTargetContext::addDrawOp(const GrClip& clip, std::unique_ptr<GrDrawOp> op) {
joshualitt1de610a2016-01-06 08:26:09 -08001734 ASSERT_SINGLE_OWNER
Robert Phillipsc0138922017-03-08 11:50:55 -05001735 if (this->drawingManager()->wasAbandoned()) {
1736 return SK_InvalidUniqueID;
1737 }
robertphillips2e1e51f2015-10-15 08:01:48 -07001738 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001739 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "addDrawOp", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -07001740
Brian Salomon467921e2017-03-06 16:17:12 -05001741 // Setup clip
1742 SkRect bounds;
1743 op_bounds(&bounds, op.get());
Brian Salomon97180af2017-03-14 13:42:58 -04001744 GrAppliedClip appliedClip;
Brian Salomon54d212e2017-03-21 14:22:38 -04001745 GrDrawOp::FixedFunctionFlags fixedFunctionFlags = op->fixedFunctionFlags();
1746 if (!clip.apply(fContext, this, fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesHWAA,
1747 fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil, &appliedClip,
1748 &bounds)) {
1749 return SK_InvalidUniqueID;
1750 }
1751
Brian Salomon54d212e2017-03-21 14:22:38 -04001752 if (fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil ||
1753 appliedClip.hasStencilClip()) {
Robert Phillips54190c42017-08-09 20:09:38 +00001754 this->getOpList()->setRequiresStencil();
Robert Phillips95214472017-08-08 18:00:03 -04001755
Robert Phillips65048132017-08-10 08:44:49 -04001756 this->setNeedsStencil();
Brian Salomon54d212e2017-03-21 14:22:38 -04001757 }
1758
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001759 GrXferProcessor::DstProxy dstProxy;
Brian Salomonf86d37b2017-06-16 10:04:34 -04001760 if (op->finalize(*this->caps(), &appliedClip) == GrDrawOp::RequiresDstTexture::kYes) {
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001761 if (!this->setupDstProxy(this->asRenderTargetProxy(), clip, op->bounds(), &dstProxy)) {
Brian Salomon54d212e2017-03-21 14:22:38 -04001762 return SK_InvalidUniqueID;
1763 }
1764 }
1765
1766 op->setClippedBounds(bounds);
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001767 return this->getRTOpList()->addOp(std::move(op), *this->caps(),
1768 std::move(appliedClip), dstProxy);
Brian Salomon54d212e2017-03-21 14:22:38 -04001769}
1770
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001771bool GrRenderTargetContext::setupDstProxy(GrRenderTargetProxy* rtProxy, const GrClip& clip,
Robert Phillips16d8ec62017-07-27 16:16:25 -04001772 const SkRect& opBounds,
1773 GrXferProcessor::DstProxy* dstProxy) {
Brian Salomon467921e2017-03-06 16:17:12 -05001774 if (this->caps()->textureBarrierSupport()) {
Robert Phillipsbf25d432017-04-07 10:08:53 -04001775 if (GrTextureProxy* texProxy = rtProxy->asTextureProxy()) {
Brian Salomon467921e2017-03-06 16:17:12 -05001776 // The render target is a texture, so we can read from it directly in the shader. The XP
1777 // will be responsible to detect this situation and request a texture barrier.
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001778 dstProxy->setProxy(sk_ref_sp(texProxy));
1779 dstProxy->setOffset(0, 0);
Robert Phillipsbf25d432017-04-07 10:08:53 -04001780 return true;
Brian Salomon467921e2017-03-06 16:17:12 -05001781 }
1782 }
1783
Robert Phillipsbf25d432017-04-07 10:08:53 -04001784 SkIRect copyRect = SkIRect::MakeWH(rtProxy->width(), rtProxy->height());
Brian Salomon467921e2017-03-06 16:17:12 -05001785
Eric Karl74480882017-04-03 14:49:05 -07001786 SkIRect clippedRect;
Robert Phillipsbf25d432017-04-07 10:08:53 -04001787 clip.getConservativeBounds(rtProxy->width(), rtProxy->height(), &clippedRect);
Eric Karl72e551e2017-04-04 13:42:10 -07001788 SkIRect drawIBounds;
Brian Salomon467921e2017-03-06 16:17:12 -05001789 opBounds.roundOut(&drawIBounds);
Brian Salomon859621f2017-03-16 09:21:54 -04001790 // Cover up for any precision issues by outsetting the op bounds a pixel in each direction.
1791 drawIBounds.outset(1, 1);
Eric Karl72e551e2017-04-04 13:42:10 -07001792 if (!clippedRect.intersect(drawIBounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -05001793#ifdef SK_DEBUG
Robert Phillipsbf25d432017-04-07 10:08:53 -04001794 GrCapsDebugf(this->caps(), "setupDstTexture: Missed an early reject bailing on draw.");
Brian Salomon467921e2017-03-06 16:17:12 -05001795#endif
Robert Phillipsbf25d432017-04-07 10:08:53 -04001796 return false;
Brian Salomon467921e2017-03-06 16:17:12 -05001797 }
1798
1799 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
1800 // have per-sample dst values by making the copy multisampled.
1801 GrSurfaceDesc desc;
Eric Karl74480882017-04-03 14:49:05 -07001802 bool rectsMustMatch = false;
1803 bool disallowSubrect = false;
Robert Phillipsbf25d432017-04-07 10:08:53 -04001804 if (!this->caps()->initDescForDstCopy(rtProxy, &desc, &rectsMustMatch, &disallowSubrect)) {
Brian Salomon467921e2017-03-06 16:17:12 -05001805 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillips16d8ec62017-07-27 16:16:25 -04001806 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
Robert Phillipsbf25d432017-04-07 10:08:53 -04001807 desc.fConfig = rtProxy->config();
Brian Salomon467921e2017-03-06 16:17:12 -05001808 }
1809
Eric Karl74480882017-04-03 14:49:05 -07001810 if (!disallowSubrect) {
1811 copyRect = clippedRect;
1812 }
Brian Salomon467921e2017-03-06 16:17:12 -05001813
Robert Phillipsbf25d432017-04-07 10:08:53 -04001814 SkIPoint dstPoint, dstOffset;
1815 SkBackingFit fit;
Eric Karl74480882017-04-03 14:49:05 -07001816 if (rectsMustMatch) {
Robert Phillipsbf25d432017-04-07 10:08:53 -04001817 SkASSERT(desc.fOrigin == rtProxy->origin());
1818 desc.fWidth = rtProxy->width();
1819 desc.fHeight = rtProxy->height();
Eric Karl74480882017-04-03 14:49:05 -07001820 dstPoint = {copyRect.fLeft, copyRect.fTop};
1821 dstOffset = {0, 0};
Robert Phillipsbf25d432017-04-07 10:08:53 -04001822 fit = SkBackingFit::kExact;
Eric Karl74480882017-04-03 14:49:05 -07001823 } else {
1824 desc.fWidth = copyRect.width();
1825 desc.fHeight = copyRect.height();
1826 dstPoint = {0, 0};
1827 dstOffset = {copyRect.fLeft, copyRect.fTop};
Robert Phillipsbf25d432017-04-07 10:08:53 -04001828 fit = SkBackingFit::kApprox;
Eric Karl74480882017-04-03 14:49:05 -07001829 }
Brian Salomon467921e2017-03-06 16:17:12 -05001830
Robert Phillipsbf25d432017-04-07 10:08:53 -04001831 sk_sp<GrSurfaceContext> sContext = fContext->contextPriv().makeDeferredSurfaceContext(
1832 desc,
1833 fit,
1834 SkBudgeted::kYes);
1835 if (!sContext) {
1836 SkDebugf("setupDstTexture: surfaceContext creation failed.\n");
1837 return false;
1838 }
1839
1840 if (!sContext->copy(rtProxy, copyRect, dstPoint)) {
1841 SkDebugf("setupDstTexture: copy failed.\n");
1842 return false;
1843 }
1844
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001845 dstProxy->setProxy(sContext->asTextureProxyRef());
1846 dstProxy->setOffset(dstOffset);
Robert Phillipsbf25d432017-04-07 10:08:53 -04001847 return true;
robertphillips2334fb62015-06-17 05:43:33 -07001848}