blob: 14fa6fb2efe04e0d79af1a6cb7aebebc3f83f426 [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"
Brian Salomon0f353322017-05-03 20:58:59 +000034#include "ops/GrDrawAtlasOp.h"
Brian Salomon6d4b65e2017-05-03 17:06:09 -040035#include "ops/GrDrawOp.h"
Brian Salomon89527432016-12-16 09:52:16 -050036#include "ops/GrDrawVerticesOp.h"
37#include "ops/GrLatticeOp.h"
38#include "ops/GrOp.h"
39#include "ops/GrOvalOpFactory.h"
40#include "ops/GrRectOpFactory.h"
41#include "ops/GrRegionOp.h"
Greg Daniela5cb7812017-06-16 09:45:32 -040042#include "ops/GrSemaphoreOp.h"
Brian Salomon89527432016-12-16 09:52:16 -050043#include "ops/GrShadowRRectOp.h"
Brian Salomon467921e2017-03-06 16:17:12 -050044#include "ops/GrStencilPathOp.h"
Brian Salomon34169692017-08-28 15:32:01 -040045#include "ops/GrTextureOp.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 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -040081 SK_ABORT("Unexpected fsaa type");
Brian Salomone225b562017-06-14 13:00:03 -040082 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 Phillips6b47c7d2017-08-29 07:24:09 -0400219 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 Phillips380b90c2017-08-30 07:41:07 -0400223 this->getRTOpList()->discard();
robertphillipsea461502015-05-26 11:38:03 -0700224}
225
Brian Osman11052242016-10-27 14:47:55 -0400226void GrRenderTargetContext::clear(const SkIRect* rect,
227 const GrColor color,
228 bool canIgnoreRect) {
joshualitt1de610a2016-01-06 08:26:09 -0800229 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700230 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700231 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400232 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "clear", fContext);
robertphillipsea461502015-05-26 11:38:03 -0700233
Robert Phillips72152832017-01-25 17:31:35 -0500234 AutoCheckFlush acf(this->drawingManager());
csmartdalton29df7602016-08-31 11:55:52 -0700235 this->internalClear(rect ? GrFixedClip(*rect) : GrFixedClip::Disabled(), color, canIgnoreRect);
236}
robertphillips9199a9f2016-07-13 07:48:43 -0700237
Robert Phillips784b7bf2016-12-09 13:35:02 -0500238void GrRenderTargetContextPriv::absClear(const SkIRect* clearRect, const GrColor color) {
239 ASSERT_SINGLE_OWNER_PRIV
240 RETURN_IF_ABANDONED_PRIV
241 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400242 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "absClear",
243 fRenderTargetContext->fContext);
Robert Phillips784b7bf2016-12-09 13:35:02 -0500244
Robert Phillips72152832017-01-25 17:31:35 -0500245 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500246
Brian Salomonbb5711a2017-05-17 13:49:59 -0400247 SkIRect rtRect = SkIRect::MakeWH(fRenderTargetContext->fRenderTargetProxy->worstCaseWidth(),
248 fRenderTargetContext->fRenderTargetProxy->worstCaseHeight());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500249
250 if (clearRect) {
251 if (clearRect->contains(rtRect)) {
252 clearRect = nullptr; // full screen
253 } else {
254 if (!rtRect.intersect(*clearRect)) {
255 return;
256 }
257 }
258 }
259
260 // TODO: in a post-MDB world this should be handled at the OpList level.
261 // An op-list that is initially cleared and has no other ops should receive an
262 // extra draw.
263 if (fRenderTargetContext->fContext->caps()->useDrawInsteadOfClear()) {
264 // This works around a driver bug with clear by drawing a rect instead.
265 // The driver will ignore a clear if it is the only thing rendered to a
266 // target before the target is read.
267 GrPaint paint;
268 paint.setColor4f(GrColor4f::FromGrColor(color));
Brian Salomona1633922017-01-09 11:46:10 -0500269 paint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
Robert Phillips784b7bf2016-12-09 13:35:02 -0500270
271 // We don't call drawRect() here to avoid the cropping to the, possibly smaller,
272 // RenderTargetProxy bounds
Brian Salomonbaaf4392017-06-15 09:59:23 -0400273 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeNonAAFill(
274 std::move(paint), SkMatrix::I(), SkRect::Make(rtRect), GrAAType::kNone);
275 fRenderTargetContext->addDrawOp(GrNoClip(), std::move(op));
Robert Phillips784b7bf2016-12-09 13:35:02 -0500276 } else {
Robert Phillips784b7bf2016-12-09 13:35:02 -0500277 // This path doesn't handle coalescing of full screen clears b.c. it
278 // has to clear the entire render target - not just the content area.
279 // It could be done but will take more finagling.
Robert Phillips5efd5ea2017-05-30 13:47:32 -0400280 std::unique_ptr<GrOp> op(GrClearOp::Make(rtRect, color, !clearRect));
Brian Salomonfc527d22016-12-14 21:07:01 -0500281 if (!op) {
Robert Phillips784b7bf2016-12-09 13:35:02 -0500282 return;
283 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400284 fRenderTargetContext->getRTOpList()->addOp(std::move(op), *fRenderTargetContext->caps());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500285 }
286}
287
Brian Osman11052242016-10-27 14:47:55 -0400288void GrRenderTargetContextPriv::clear(const GrFixedClip& clip,
289 const GrColor color,
290 bool canIgnoreClip) {
csmartdalton29df7602016-08-31 11:55:52 -0700291 ASSERT_SINGLE_OWNER_PRIV
292 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400293 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400294 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "clear",
295 fRenderTargetContext->fContext);
csmartdalton29df7602016-08-31 11:55:52 -0700296
Robert Phillips72152832017-01-25 17:31:35 -0500297 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Brian Osman11052242016-10-27 14:47:55 -0400298 fRenderTargetContext->internalClear(clip, color, canIgnoreClip);
csmartdalton29df7602016-08-31 11:55:52 -0700299}
300
Brian Osman11052242016-10-27 14:47:55 -0400301void GrRenderTargetContext::internalClear(const GrFixedClip& clip,
302 const GrColor color,
303 bool canIgnoreClip) {
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700304 bool isFull = false;
305 if (!clip.hasWindowRectangles()) {
306 isFull = !clip.scissorEnabled() ||
307 (canIgnoreClip && fContext->caps()->fullClearIsFree()) ||
308 clip.scissorRect().contains(SkIRect::MakeWH(this->width(), this->height()));
309 }
robertphillips9199a9f2016-07-13 07:48:43 -0700310
311 if (fContext->caps()->useDrawInsteadOfClear()) {
312 // This works around a driver bug with clear by drawing a rect instead.
313 // The driver will ignore a clear if it is the only thing rendered to a
314 // target before the target is read.
Robert Phillips784b7bf2016-12-09 13:35:02 -0500315 SkIRect clearRect = SkIRect::MakeWH(this->width(), this->height());
csmartdalton29df7602016-08-31 11:55:52 -0700316 if (isFull) {
robertphillips9199a9f2016-07-13 07:48:43 -0700317 this->discard();
Robert Phillips93f16332016-11-23 19:37:13 -0500318 } else if (!clearRect.intersect(clip.scissorRect())) {
csmartdalton29df7602016-08-31 11:55:52 -0700319 return;
robertphillips9199a9f2016-07-13 07:48:43 -0700320 }
321
322 GrPaint paint;
323 paint.setColor4f(GrColor4f::FromGrColor(color));
Brian Salomona1633922017-01-09 11:46:10 -0500324 paint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
robertphillips9199a9f2016-07-13 07:48:43 -0700325
Brian Salomon82f44312017-01-11 13:42:54 -0500326 this->drawRect(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), SkRect::Make(clearRect));
bsalomon9f129de2016-08-10 16:31:05 -0700327 } else if (isFull) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400328 this->getRTOpList()->fullClear(*this->caps(), color);
robertphillips9199a9f2016-07-13 07:48:43 -0700329 } else {
Robert Phillips5efd5ea2017-05-30 13:47:32 -0400330 std::unique_ptr<GrOp> op(GrClearOp::Make(clip, color, this->asSurfaceProxy()));
Brian Salomon42521e82016-12-07 16:44:58 -0500331 if (!op) {
csmartdalton29df7602016-08-31 11:55:52 -0700332 return;
333 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400334 this->getRTOpList()->addOp(std::move(op), *this->caps());
robertphillips9199a9f2016-07-13 07:48:43 -0700335 }
robertphillipsea461502015-05-26 11:38:03 -0700336}
337
Brian Osman11052242016-10-27 14:47:55 -0400338void GrRenderTargetContext::drawPaint(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500339 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400340 const SkMatrix& viewMatrix) {
joshualitt1de610a2016-01-06 08:26:09 -0800341 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700342 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700343 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400344 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawPaint", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700345
robertphillipsea461502015-05-26 11:38:03 -0700346 // set rect to be big enough to fill the space, but not super-huge, so we
347 // don't overflow fixed-point implementations
robertphillips13a7eee2016-08-31 15:06:24 -0700348
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400349 SkRect r = fRenderTargetProxy->getBoundsRect();
robertphillipsea461502015-05-26 11:38:03 -0700350
bsalomoncb31e512016-08-26 10:48:19 -0700351 SkRRect rrect;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500352 GrAA aa;
bsalomoncb31e512016-08-26 10:48:19 -0700353 // Check if we can replace a clipRRect()/drawPaint() with a drawRRect(). We only do the
354 // transformation for non-rect rrects. Rects caused a performance regression on an Android
355 // test that needs investigation. We also skip cases where there are fragment processors
356 // because they may depend on having correct local coords and this path draws in device space
357 // without a local matrix.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500358 if (!paint.numTotalFragmentProcessors() && clip.isRRect(r, &rrect, &aa) && !rrect.isRect()) {
Brian Salomon82f44312017-01-11 13:42:54 -0500359 this->drawRRect(GrNoClip(), std::move(paint), aa, SkMatrix::I(), rrect,
360 GrStyle::SimpleFill());
bsalomoncb31e512016-08-26 10:48:19 -0700361 return;
362 }
363
robertphillipsea461502015-05-26 11:38:03 -0700364
365 bool isPerspective = viewMatrix.hasPerspective();
366
367 // We attempt to map r by the inverse matrix and draw that. mapRect will
368 // map the four corners and bound them with a new rect. This will not
369 // produce a correct result for some perspective matrices.
370 if (!isPerspective) {
reeda39667c2016-08-22 06:39:49 -0700371 if (!SkMatrixPriv::InverseMapRect(viewMatrix, &r, r)) {
robertphillipsea461502015-05-26 11:38:03 -0700372 SkDebugf("Could not invert matrix\n");
373 return;
374 }
Brian Salomon82f44312017-01-11 13:42:54 -0500375 this->drawRect(clip, std::move(paint), GrAA::kNo, viewMatrix, r);
robertphillipsea461502015-05-26 11:38:03 -0700376 } else {
377 SkMatrix localMatrix;
378 if (!viewMatrix.invert(&localMatrix)) {
379 SkDebugf("Could not invert matrix\n");
380 return;
381 }
382
Robert Phillips72152832017-01-25 17:31:35 -0500383 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700384
Brian Salomonbaaf4392017-06-15 09:59:23 -0400385 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeNonAAFillWithLocalMatrix(
386 std::move(paint), SkMatrix::I(), localMatrix, r, GrAAType::kNone);
387 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -0700388 }
389}
390
robertphillipsea461502015-05-26 11:38:03 -0700391static inline bool rect_contains_inclusive(const SkRect& rect, const SkPoint& point) {
392 return point.fX >= rect.fLeft && point.fX <= rect.fRight &&
393 point.fY >= rect.fTop && point.fY <= rect.fBottom;
394}
395
csmartdalton97f6cd52016-07-13 13:37:08 -0700396// Attempts to crop a rect and optional local rect to the clip boundaries.
397// Returns false if the draw can be skipped entirely.
robertphillips13a7eee2016-08-31 15:06:24 -0700398static bool crop_filled_rect(int width, int height, const GrClip& clip,
csmartdalton97f6cd52016-07-13 13:37:08 -0700399 const SkMatrix& viewMatrix, SkRect* rect,
400 SkRect* localRect = nullptr) {
401 if (!viewMatrix.rectStaysRect()) {
402 return true;
403 }
404
csmartdalton97f6cd52016-07-13 13:37:08 -0700405 SkIRect clipDevBounds;
406 SkRect clipBounds;
csmartdalton97f6cd52016-07-13 13:37:08 -0700407
robertphillips13a7eee2016-08-31 15:06:24 -0700408 clip.getConservativeBounds(width, height, &clipDevBounds);
reeda39667c2016-08-22 06:39:49 -0700409 if (!SkMatrixPriv::InverseMapRect(viewMatrix, &clipBounds, SkRect::Make(clipDevBounds))) {
410 return false;
411 }
csmartdalton97f6cd52016-07-13 13:37:08 -0700412
413 if (localRect) {
414 if (!rect->intersects(clipBounds)) {
415 return false;
416 }
417 const SkScalar dx = localRect->width() / rect->width();
418 const SkScalar dy = localRect->height() / rect->height();
419 if (clipBounds.fLeft > rect->fLeft) {
420 localRect->fLeft += (clipBounds.fLeft - rect->fLeft) * dx;
421 rect->fLeft = clipBounds.fLeft;
422 }
423 if (clipBounds.fTop > rect->fTop) {
424 localRect->fTop += (clipBounds.fTop - rect->fTop) * dy;
425 rect->fTop = clipBounds.fTop;
426 }
427 if (clipBounds.fRight < rect->fRight) {
428 localRect->fRight -= (rect->fRight - clipBounds.fRight) * dx;
429 rect->fRight = clipBounds.fRight;
430 }
431 if (clipBounds.fBottom < rect->fBottom) {
432 localRect->fBottom -= (rect->fBottom - clipBounds.fBottom) * dy;
433 rect->fBottom = clipBounds.fBottom;
434 }
435 return true;
436 }
437
438 return rect->intersect(clipBounds);
439}
440
Brian Osman11052242016-10-27 14:47:55 -0400441bool GrRenderTargetContext::drawFilledRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500442 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500443 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400444 const SkMatrix& viewMatrix,
445 const SkRect& rect,
446 const GrUserStencilSettings* ss) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700447 SkRect croppedRect = rect;
Robert Phillips784b7bf2016-12-09 13:35:02 -0500448 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix, &croppedRect)) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700449 return true;
450 }
robertphillips44302392016-07-08 14:43:03 -0700451
Brian Salomon54d212e2017-03-21 14:22:38 -0400452 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport() &&
453 (!ss || ss->isDisabled(false))) {
Robert Phillipse3302df2017-04-24 07:31:02 -0400454 gr_instanced::OpAllocator* oa = this->drawingManager()->instancingAllocator();
455 std::unique_ptr<GrDrawOp> op = oa->recordRect(croppedRect, viewMatrix, std::move(paint),
456 aa, fInstancedPipelineInfo);
Brian Salomon42521e82016-12-07 16:44:58 -0500457 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -0400458 this->addDrawOp(clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700459 return true;
csmartdaltona7f29642016-07-07 08:49:11 -0700460 }
461 }
Brian Salomon7c8460e2017-05-12 11:36:10 -0400462 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400463 std::unique_ptr<GrDrawOp> op;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500464 if (GrAAType::kCoverage == aaType) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400465 op = GrRectOpFactory::MakeAAFill(std::move(paint), viewMatrix, croppedRect, ss);
robertphillips391395d2016-03-02 09:26:36 -0800466 } else {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400467 op = GrRectOpFactory::MakeNonAAFill(std::move(paint), viewMatrix, croppedRect, aaType, ss);
robertphillips391395d2016-03-02 09:26:36 -0800468 }
Brian Salomonbaaf4392017-06-15 09:59:23 -0400469 if (!op) {
470 return false;
471 }
472 this->addDrawOp(clip, std::move(op));
473 return true;
robertphillips391395d2016-03-02 09:26:36 -0800474}
475
Brian Osman11052242016-10-27 14:47:55 -0400476void GrRenderTargetContext::drawRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500477 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500478 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400479 const SkMatrix& viewMatrix,
480 const SkRect& rect,
481 const GrStyle* style) {
bsalomon6663acf2016-05-10 09:14:17 -0700482 if (!style) {
483 style = &GrStyle::SimpleFill();
484 }
joshualitt1de610a2016-01-06 08:26:09 -0800485 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700486 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700487 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400488 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRect", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700489
bsalomon6663acf2016-05-10 09:14:17 -0700490 // Path effects should've been devolved to a path in SkGpuDevice
491 SkASSERT(!style->pathEffect());
robertphillipsea461502015-05-26 11:38:03 -0700492
Robert Phillips72152832017-01-25 17:31:35 -0500493 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700494
bsalomon6663acf2016-05-10 09:14:17 -0700495 const SkStrokeRec& stroke = style->strokeRec();
robertphillips3ab14ca2016-07-10 11:49:39 -0700496 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
Greg Daniela5cb7812017-06-16 09:45:32 -0400497
robertphillips3ab14ca2016-07-10 11:49:39 -0700498 if (!fContext->caps()->useDrawInsteadOfClear()) {
499 // Check if this is a full RT draw and can be replaced with a clear. We don't bother
500 // checking cases where the RT is fully inside a stroke.
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400501 SkRect rtRect = fRenderTargetProxy->getBoundsRect();
robertphillips3ab14ca2016-07-10 11:49:39 -0700502 // Does the clip contain the entire RT?
503 if (clip.quickContains(rtRect)) {
504 SkMatrix invM;
505 if (!viewMatrix.invert(&invM)) {
robertphillipsea461502015-05-26 11:38:03 -0700506 return;
507 }
robertphillips3ab14ca2016-07-10 11:49:39 -0700508 // Does the rect bound the RT?
509 SkPoint srcSpaceRTQuad[4];
510 invM.mapRectToQuad(srcSpaceRTQuad, rtRect);
511 if (rect_contains_inclusive(rect, srcSpaceRTQuad[0]) &&
512 rect_contains_inclusive(rect, srcSpaceRTQuad[1]) &&
513 rect_contains_inclusive(rect, srcSpaceRTQuad[2]) &&
514 rect_contains_inclusive(rect, srcSpaceRTQuad[3])) {
515 // Will it blend?
516 GrColor clearColor;
517 if (paint.isConstantBlendedColor(&clearColor)) {
robertphillips9199a9f2016-07-13 07:48:43 -0700518 this->clear(nullptr, clearColor, true);
robertphillips3ab14ca2016-07-10 11:49:39 -0700519 return;
520 }
521 }
robertphillipsea461502015-05-26 11:38:03 -0700522 }
523 }
robertphillips44302392016-07-08 14:43:03 -0700524
Brian Salomon82f44312017-01-11 13:42:54 -0500525 if (this->drawFilledRect(clip, std::move(paint), aa, viewMatrix, rect, nullptr)) {
robertphillips44302392016-07-08 14:43:03 -0700526 return;
527 }
bsalomona7d85ba2016-07-06 11:54:59 -0700528 } else if (stroke.getStyle() == SkStrokeRec::kStroke_Style ||
529 stroke.getStyle() == SkStrokeRec::kHairline_Style) {
530 if ((!rect.width() || !rect.height()) &&
531 SkStrokeRec::kHairline_Style != stroke.getStyle()) {
532 SkScalar r = stroke.getWidth() / 2;
533 // TODO: Move these stroke->fill fallbacks to GrShape?
534 switch (stroke.getJoin()) {
535 case SkPaint::kMiter_Join:
Brian Salomon82f44312017-01-11 13:42:54 -0500536 this->drawRect(
537 clip, std::move(paint), aa, viewMatrix,
538 {rect.fLeft - r, rect.fTop - r, rect.fRight + r, rect.fBottom + r},
539 &GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700540 return;
541 case SkPaint::kRound_Join:
542 // Raster draws nothing when both dimensions are empty.
543 if (rect.width() || rect.height()){
544 SkRRect rrect = SkRRect::MakeRectXY(rect.makeOutset(r, r), r, r);
Brian Salomon82f44312017-01-11 13:42:54 -0500545 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect,
546 GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700547 return;
548 }
549 case SkPaint::kBevel_Join:
550 if (!rect.width()) {
Brian Salomon82f44312017-01-11 13:42:54 -0500551 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700552 {rect.fLeft - r, rect.fTop, rect.fRight + r, rect.fBottom},
553 &GrStyle::SimpleFill());
554 } else {
Brian Salomon82f44312017-01-11 13:42:54 -0500555 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700556 {rect.fLeft, rect.fTop - r, rect.fRight, rect.fBottom + r},
557 &GrStyle::SimpleFill());
558 }
559 return;
560 }
561 }
robertphillips44302392016-07-08 14:43:03 -0700562
Brian Salomonbaaf4392017-06-15 09:59:23 -0400563 std::unique_ptr<GrDrawOp> op;
robertphillips44302392016-07-08 14:43:03 -0700564
Brian Salomon7c8460e2017-05-12 11:36:10 -0400565 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500566 if (GrAAType::kCoverage == aaType) {
cdaltonbb539482016-01-04 09:48:25 -0800567 // The stroke path needs the rect to remain axis aligned (no rotation or skew).
568 if (viewMatrix.rectStaysRect()) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400569 op = GrRectOpFactory::MakeAAStroke(std::move(paint), viewMatrix, rect, stroke);
cdaltonbb539482016-01-04 09:48:25 -0800570 }
robertphillipsea461502015-05-26 11:38:03 -0700571 } else {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400572 op = GrRectOpFactory::MakeNonAAStroke(std::move(paint), viewMatrix, rect, stroke,
573 aaType);
robertphillips391395d2016-03-02 09:26:36 -0800574 }
robertphillips4bc31812016-03-01 12:22:49 -0800575
Brian Salomon42521e82016-12-07 16:44:58 -0500576 if (op) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400577 this->addDrawOp(clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700578 return;
robertphillips4bc31812016-03-01 12:22:49 -0800579 }
robertphillips4bc31812016-03-01 12:22:49 -0800580 }
halcanary9d524f22016-03-29 09:03:52 -0700581
robertphillips4bc31812016-03-01 12:22:49 -0800582 SkPath path;
583 path.setIsVolatile(true);
584 path.addRect(rect);
Brian Salomon82f44312017-01-11 13:42:54 -0500585 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, *style);
robertphillipsea461502015-05-26 11:38:03 -0700586}
587
Robert Phillipsec2249f2016-11-09 08:54:35 -0500588int GrRenderTargetContextPriv::maxWindowRectangles() const {
589 return fRenderTargetContext->fRenderTargetProxy->maxWindowRectangles(
590 *fRenderTargetContext->fContext->caps());
591}
592
Brian Osman11052242016-10-27 14:47:55 -0400593void GrRenderTargetContextPriv::clearStencilClip(const GrFixedClip& clip, bool insideStencilMask) {
robertphillips976f5f02016-06-03 10:59:20 -0700594 ASSERT_SINGLE_OWNER_PRIV
595 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400596 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400597 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "clearStencilClip",
598 fRenderTargetContext->fContext);
robertphillips976f5f02016-06-03 10:59:20 -0700599
Robert Phillips72152832017-01-25 17:31:35 -0500600 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400601
Robert Phillips2f4ddf62017-06-01 08:48:19 -0400602 std::unique_ptr<GrOp> op(GrClearStencilClipOp::Make(
603 clip, insideStencilMask,
604 fRenderTargetContext->fRenderTargetProxy.get()));
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400605 if (!op) {
Robert Phillipse60ad622016-11-17 10:22:48 -0500606 return;
607 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400608 fRenderTargetContext->getRTOpList()->addOp(std::move(op), *fRenderTargetContext->caps());
robertphillips976f5f02016-06-03 10:59:20 -0700609}
610
Brian Osman11052242016-10-27 14:47:55 -0400611void GrRenderTargetContextPriv::stencilPath(const GrClip& clip,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500612 GrAAType aaType,
Brian Osman11052242016-10-27 14:47:55 -0400613 const SkMatrix& viewMatrix,
614 const GrPath* path) {
Brian Salomon467921e2017-03-06 16:17:12 -0500615 ASSERT_SINGLE_OWNER_PRIV
616 RETURN_IF_ABANDONED_PRIV
617 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400618 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "stencilPath",
619 fRenderTargetContext->fContext);
Brian Salomon467921e2017-03-06 16:17:12 -0500620
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500621 SkASSERT(aaType != GrAAType::kCoverage);
Brian Salomon467921e2017-03-06 16:17:12 -0500622
623 bool useHWAA = GrAATypeIsHW(aaType);
624 // TODO: extract portions of checkDraw that are relevant to path stenciling.
625 SkASSERT(path);
626 SkASSERT(fRenderTargetContext->caps()->shaderCaps()->pathRenderingSupport());
627
628 // FIXME: Use path bounds instead of this WAR once
629 // https://bugs.chromium.org/p/skia/issues/detail?id=5640 is resolved.
630 SkRect bounds = SkRect::MakeIWH(fRenderTargetContext->width(), fRenderTargetContext->height());
631
632 // Setup clip
Brian Salomon97180af2017-03-14 13:42:58 -0400633 GrAppliedClip appliedClip;
Brian Salomon467921e2017-03-06 16:17:12 -0500634 if (!clip.apply(fRenderTargetContext->fContext, fRenderTargetContext, useHWAA, true,
Brian Salomon97180af2017-03-14 13:42:58 -0400635 &appliedClip, &bounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -0500636 return;
637 }
638
639 // Coverage AA does not make sense when rendering to the stencil buffer. The caller should never
640 // attempt this in a situation that would require coverage AA.
641 SkASSERT(!appliedClip.clipCoverageFragmentProcessor());
642
Robert Phillips65048132017-08-10 08:44:49 -0400643 fRenderTargetContext->setNeedsStencil();
Brian Salomon467921e2017-03-06 16:17:12 -0500644
645 std::unique_ptr<GrOp> op = GrStencilPathOp::Make(viewMatrix,
646 useHWAA,
647 path->getFillType(),
648 appliedClip.hasStencilClip(),
Brian Salomon467921e2017-03-06 16:17:12 -0500649 appliedClip.scissorState(),
Brian Salomon467921e2017-03-06 16:17:12 -0500650 path);
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400651 if (!op) {
652 return;
653 }
Brian Salomon97180af2017-03-14 13:42:58 -0400654 op->setClippedBounds(bounds);
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400655 fRenderTargetContext->getRTOpList()->addOp(std::move(op), *fRenderTargetContext->caps());
robertphillips976f5f02016-06-03 10:59:20 -0700656}
657
Brian Osman11052242016-10-27 14:47:55 -0400658void GrRenderTargetContextPriv::stencilRect(const GrClip& clip,
659 const GrUserStencilSettings* ss,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500660 GrAAType aaType,
Brian Osman11052242016-10-27 14:47:55 -0400661 const SkMatrix& viewMatrix,
662 const SkRect& rect) {
robertphillips976f5f02016-06-03 10:59:20 -0700663 ASSERT_SINGLE_OWNER_PRIV
664 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400665 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400666 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "stencilRect",
667 fRenderTargetContext->fContext);
668
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500669 SkASSERT(GrAAType::kCoverage != aaType);
Robert Phillips72152832017-01-25 17:31:35 -0500670 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips976f5f02016-06-03 10:59:20 -0700671
672 GrPaint paint;
Brian Salomona1633922017-01-09 11:46:10 -0500673 paint.setXPFactory(GrDisableColorXPFactory::Get());
Brian Salomonbaaf4392017-06-15 09:59:23 -0400674 std::unique_ptr<GrDrawOp> op =
675 GrRectOpFactory::MakeNonAAFill(std::move(paint), viewMatrix, rect, aaType, ss);
676 fRenderTargetContext->addDrawOp(clip, std::move(op));
robertphillips976f5f02016-06-03 10:59:20 -0700677}
678
Brian Osman11052242016-10-27 14:47:55 -0400679bool GrRenderTargetContextPriv::drawAndStencilRect(const GrClip& clip,
680 const GrUserStencilSettings* ss,
681 SkRegion::Op op,
682 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500683 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400684 const SkMatrix& viewMatrix,
685 const SkRect& rect) {
robertphillips391395d2016-03-02 09:26:36 -0800686 ASSERT_SINGLE_OWNER_PRIV
687 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400688 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400689 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawAndStencilRect",
690 fRenderTargetContext->fContext);
robertphillips391395d2016-03-02 09:26:36 -0800691
Robert Phillips72152832017-01-25 17:31:35 -0500692 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -0800693
694 GrPaint paint;
robertphillips391395d2016-03-02 09:26:36 -0800695 paint.setCoverageSetOpXPFactory(op, invert);
696
Brian Salomon82f44312017-01-11 13:42:54 -0500697 if (fRenderTargetContext->drawFilledRect(clip, std::move(paint), aa, viewMatrix, rect, ss)) {
robertphillips391395d2016-03-02 09:26:36 -0800698 return true;
699 }
robertphillips391395d2016-03-02 09:26:36 -0800700 SkPath path;
701 path.setIsVolatile(true);
702 path.addRect(rect);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500703 return this->drawAndStencilPath(clip, ss, op, invert, aa, viewMatrix, path);
robertphillips391395d2016-03-02 09:26:36 -0800704}
705
Brian Osman11052242016-10-27 14:47:55 -0400706void GrRenderTargetContext::fillRectToRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500707 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500708 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400709 const SkMatrix& viewMatrix,
710 const SkRect& rectToDraw,
711 const SkRect& localRect) {
joshualitt1de610a2016-01-06 08:26:09 -0800712 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700713 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700714 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400715 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "fillRectToRect", fContext);
robertphillipsea461502015-05-26 11:38:03 -0700716
csmartdalton97f6cd52016-07-13 13:37:08 -0700717 SkRect croppedRect = rectToDraw;
718 SkRect croppedLocalRect = localRect;
robertphillips13a7eee2016-08-31 15:06:24 -0700719 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix,
720 &croppedRect, &croppedLocalRect)) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700721 return;
722 }
723
Robert Phillips72152832017-01-25 17:31:35 -0500724 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -0700725
csmartdaltone0d36292016-07-29 08:14:20 -0700726 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport()) {
Robert Phillipse3302df2017-04-24 07:31:02 -0400727 gr_instanced::OpAllocator* oa = this->drawingManager()->instancingAllocator();
728 std::unique_ptr<GrDrawOp> op(oa->recordRect(croppedRect, viewMatrix, std::move(paint),
Brian Salomon54d212e2017-03-21 14:22:38 -0400729 croppedLocalRect, aa, fInstancedPipelineInfo));
Brian Salomon42521e82016-12-07 16:44:58 -0500730 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -0400731 this->addDrawOp(clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -0700732 return;
733 }
734 }
735
Brian Salomon7c8460e2017-05-12 11:36:10 -0400736 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500737 if (GrAAType::kCoverage != aaType) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400738 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeNonAAFillWithLocalRect(
739 std::move(paint), viewMatrix, croppedRect, croppedLocalRect, aaType);
740 this->addDrawOp(clip, std::move(op));
csmartdaltonfc49d562016-07-26 17:05:47 -0700741 return;
joshualitt04194f32016-01-13 10:08:27 -0800742 }
bsalomonbb243832016-07-22 07:10:19 -0700743
Brian Salomonbaaf4392017-06-15 09:59:23 -0400744 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeAAFillWithLocalRect(
745 std::move(paint), viewMatrix, croppedRect, croppedLocalRect);
746 if (op) {
747 this->addDrawOp(clip, std::move(op));
csmartdaltonfc49d562016-07-26 17:05:47 -0700748 return;
749 }
750
751 SkMatrix viewAndUnLocalMatrix;
752 if (!viewAndUnLocalMatrix.setRectToRect(localRect, rectToDraw, SkMatrix::kFill_ScaleToFit)) {
753 SkDebugf("fillRectToRect called with empty local matrix.\n");
754 return;
755 }
756 viewAndUnLocalMatrix.postConcat(viewMatrix);
757
758 SkPath path;
759 path.setIsVolatile(true);
760 path.addRect(localRect);
Brian Salomon82f44312017-01-11 13:42:54 -0500761 this->internalDrawPath(clip, std::move(paint), aa, viewAndUnLocalMatrix, path, GrStyle());
joshualittb6b513b2015-08-21 10:25:18 -0700762}
763
Brian Salomon34169692017-08-28 15:32:01 -0400764static bool must_filter(const SkRect& src, const SkRect& dst, const SkMatrix& ctm) {
765 // We don't currently look for 90 degree rotations, mirroring, or downscales that sample at
766 // texel centers.
767 if (!ctm.isTranslate()) {
768 return true;
769 }
770 if (src.width() != dst.width() || src.height() != dst.height()) {
771 return true;
772 }
773 // Check that the device space rectangle's fractional offset is the same as the src rectangle,
774 // and that therefore integers in the src image fall on integers in device space.
775 SkScalar x = ctm.getTranslateX(), y = ctm.getTranslateY();
776 x += dst.fLeft; y += dst.fTop;
777 x -= src.fLeft; y -= src.fTop;
778 return !SkScalarIsInt(x) || !SkScalarIsInt(y);
779}
780
781void GrRenderTargetContext::drawTextureAffine(const GrClip& clip, sk_sp<GrTextureProxy> proxy,
782 GrSamplerParams::FilterMode filter, GrColor color,
783 const SkRect& srcRect, const SkRect& dstRect,
784 const SkMatrix& viewMatrix,
785 sk_sp<GrColorSpaceXform> colorSpaceXform) {
786 ASSERT_SINGLE_OWNER
787 RETURN_IF_ABANDONED
788 SkDEBUGCODE(this->validate();)
789 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTextureAffine", fContext);
790 SkASSERT(!viewMatrix.hasPerspective());
791 if (filter != GrSamplerParams::kNone_FilterMode && !must_filter(srcRect, dstRect, viewMatrix)) {
792 filter = GrSamplerParams::kNone_FilterMode;
793 }
794 bool allowSRGB = SkToBool(this->getColorSpace());
795 this->addDrawOp(clip, GrTextureOp::Make(std::move(proxy), filter, color, srcRect, dstRect,
796 viewMatrix, std::move(colorSpaceXform), allowSRGB));
797}
798
Brian Osman11052242016-10-27 14:47:55 -0400799void GrRenderTargetContext::fillRectWithLocalMatrix(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500800 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500801 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400802 const SkMatrix& viewMatrix,
803 const SkRect& rectToDraw,
804 const SkMatrix& localMatrix) {
joshualitt1de610a2016-01-06 08:26:09 -0800805 ASSERT_SINGLE_OWNER
joshualittb6b513b2015-08-21 10:25:18 -0700806 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700807 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400808 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "fillRectWithLocalMatrix", fContext);
joshualittb6b513b2015-08-21 10:25:18 -0700809
csmartdalton97f6cd52016-07-13 13:37:08 -0700810 SkRect croppedRect = rectToDraw;
robertphillips13a7eee2016-08-31 15:06:24 -0700811 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix, &croppedRect)) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700812 return;
813 }
814
Robert Phillips72152832017-01-25 17:31:35 -0500815 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -0700816
csmartdaltone0d36292016-07-29 08:14:20 -0700817 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport()) {
Robert Phillipse3302df2017-04-24 07:31:02 -0400818 gr_instanced::OpAllocator* oa = this->drawingManager()->instancingAllocator();
819 std::unique_ptr<GrDrawOp> op(oa->recordRect(croppedRect, viewMatrix, std::move(paint),
Brian Salomon54d212e2017-03-21 14:22:38 -0400820 localMatrix, aa, fInstancedPipelineInfo));
Brian Salomon42521e82016-12-07 16:44:58 -0500821 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -0400822 this->addDrawOp(clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -0700823 return;
824 }
825 }
826
Brian Salomon7c8460e2017-05-12 11:36:10 -0400827 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500828 if (GrAAType::kCoverage != aaType) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400829 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeNonAAFillWithLocalMatrix(
830 std::move(paint), viewMatrix, localMatrix, croppedRect, aaType);
831 this->addDrawOp(clip, std::move(op));
csmartdaltonfc49d562016-07-26 17:05:47 -0700832 return;
bsalomonc55271f2015-11-09 11:55:57 -0800833 }
robertphillips4bc31812016-03-01 12:22:49 -0800834
Brian Salomonbaaf4392017-06-15 09:59:23 -0400835 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeAAFillWithLocalMatrix(
836 std::move(paint), viewMatrix, localMatrix, croppedRect);
837 if (op) {
838 this->addDrawOp(clip, std::move(op));
csmartdaltonfc49d562016-07-26 17:05:47 -0700839 return;
840 }
841
842 SkMatrix viewAndUnLocalMatrix;
843 if (!localMatrix.invert(&viewAndUnLocalMatrix)) {
844 SkDebugf("fillRectWithLocalMatrix called with degenerate local matrix.\n");
845 return;
846 }
847 viewAndUnLocalMatrix.postConcat(viewMatrix);
848
849 SkPath path;
850 path.setIsVolatile(true);
851 path.addRect(rectToDraw);
852 path.transform(localMatrix);
Brian Salomon82f44312017-01-11 13:42:54 -0500853 this->internalDrawPath(clip, std::move(paint), aa, viewAndUnLocalMatrix, path, GrStyle());
robertphillipsea461502015-05-26 11:38:03 -0700854}
855
Brian Osman11052242016-10-27 14:47:55 -0400856void GrRenderTargetContext::drawVertices(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500857 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400858 const SkMatrix& viewMatrix,
Brian Osmanae0c50c2017-05-25 16:56:34 -0400859 sk_sp<SkVertices> vertices,
860 GrPrimitiveType* overridePrimType) {
Brian Salomon199fb872017-02-06 09:41:10 -0500861 ASSERT_SINGLE_OWNER
862 RETURN_IF_ABANDONED
863 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400864 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawVertices", fContext);
Brian Salomon199fb872017-02-06 09:41:10 -0500865
866 AutoCheckFlush acf(this->drawingManager());
867
868 SkASSERT(vertices);
Brian Salomonc2f42542017-07-12 14:11:22 -0400869 GrAAType aaType = this->chooseAAType(GrAA::kNo, GrAllowMixedSamples::kNo);
870 std::unique_ptr<GrDrawOp> op =
871 GrDrawVerticesOp::Make(std::move(paint), std::move(vertices), viewMatrix, aaType,
872 this->isGammaCorrect(), fColorXformFromSRGB, overridePrimType);
873 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -0700874}
875
876///////////////////////////////////////////////////////////////////////////////
877
Brian Osman11052242016-10-27 14:47:55 -0400878void GrRenderTargetContext::drawAtlas(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500879 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400880 const SkMatrix& viewMatrix,
881 int spriteCount,
882 const SkRSXform xform[],
883 const SkRect texRect[],
884 const SkColor colors[]) {
joshualitt1de610a2016-01-06 08:26:09 -0800885 ASSERT_SINGLE_OWNER
jvanverth31ff7622015-08-07 10:09:28 -0700886 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700887 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400888 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawAtlas", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700889
Robert Phillips72152832017-01-25 17:31:35 -0500890 AutoCheckFlush acf(this->drawingManager());
halcanary9d524f22016-03-29 09:03:52 -0700891
Brian Salomon0088f942017-07-12 11:51:27 -0400892 GrAAType aaType = this->chooseAAType(GrAA::kNo, GrAllowMixedSamples::kNo);
893 std::unique_ptr<GrDrawOp> op = GrDrawAtlasOp::Make(std::move(paint), viewMatrix, aaType,
894 spriteCount, xform, texRect, colors);
895 this->addDrawOp(clip, std::move(op));
jvanverth31ff7622015-08-07 10:09:28 -0700896}
897
898///////////////////////////////////////////////////////////////////////////////
899
Brian Osman11052242016-10-27 14:47:55 -0400900void GrRenderTargetContext::drawRRect(const GrClip& origClip,
Brian Salomon82f44312017-01-11 13:42:54 -0500901 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500902 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400903 const SkMatrix& viewMatrix,
904 const SkRRect& rrect,
905 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -0800906 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700907 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700908 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400909 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRRect", fContext);
robertphillipsea461502015-05-26 11:38:03 -0700910 if (rrect.isEmpty()) {
911 return;
912 }
913
bsalomon7f0d9f32016-08-15 14:49:10 -0700914 GrNoClip noclip;
915 const GrClip* clip = &origClip;
916#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
917 // The Android framework frequently clips rrects to themselves where the clip is non-aa and the
Brian Salomon42521e82016-12-07 16:44:58 -0500918 // draw is aa. Since our lower level clip code works from op bounds, which are SkRects, it
bsalomon7f0d9f32016-08-15 14:49:10 -0700919 // doesn't detect that the clip can be ignored (modulo antialiasing). The following test
920 // attempts to mitigate the stencil clip cost but will only help when the entire clip stack
921 // can be ignored. We'd prefer to fix this in the framework by removing the clips calls.
922 SkRRect devRRect;
923 if (rrect.transform(viewMatrix, &devRRect) && clip->quickContains(devRRect)) {
924 clip = &noclip;
925 }
926#endif
bsalomon6663acf2016-05-10 09:14:17 -0700927 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
ksakamotoec7f2ac2016-07-05 03:54:53 -0700928
Robert Phillips72152832017-01-25 17:31:35 -0500929 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -0700930 const SkStrokeRec stroke = style.strokeRec();
csmartdaltona7f29642016-07-07 08:49:11 -0700931
csmartdaltone0d36292016-07-29 08:14:20 -0700932 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport() &&
933 stroke.isFillStyle()) {
Robert Phillipse3302df2017-04-24 07:31:02 -0400934 gr_instanced::OpAllocator* oa = this->drawingManager()->instancingAllocator();
Brian Salomon54d212e2017-03-21 14:22:38 -0400935 std::unique_ptr<GrDrawOp> op(
Robert Phillipse3302df2017-04-24 07:31:02 -0400936 oa->recordRRect(rrect, viewMatrix, std::move(paint), aa, fInstancedPipelineInfo));
Brian Salomon42521e82016-12-07 16:44:58 -0500937 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -0400938 this->addDrawOp(*clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -0700939 return;
940 }
941 }
942
Brian Salomon7c8460e2017-05-12 11:36:10 -0400943 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500944 if (GrAAType::kCoverage == aaType) {
Brian Salomon94efbf52016-11-29 13:43:05 -0500945 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomon05441c42017-05-15 16:45:49 -0400946 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeRRectOp(std::move(paint),
Brian Salomon05441c42017-05-15 16:45:49 -0400947 viewMatrix,
948 rrect,
949 stroke,
950 shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -0500951 if (op) {
Brian Salomon05441c42017-05-15 16:45:49 -0400952 this->addDrawOp(*clip, std::move(op));
robertphillipsb56f9272016-02-25 11:03:52 -0800953 return;
954 }
robertphillipsea461502015-05-26 11:38:03 -0700955 }
robertphillipsb56f9272016-02-25 11:03:52 -0800956
957 SkPath path;
958 path.setIsVolatile(true);
959 path.addRRect(rrect);
Brian Salomon82f44312017-01-11 13:42:54 -0500960 this->internalDrawPath(*clip, std::move(paint), aa, viewMatrix, path, style);
robertphillipsea461502015-05-26 11:38:03 -0700961}
962
Jim Van Verthc5903412016-11-17 15:27:09 -0500963///////////////////////////////////////////////////////////////////////////////
964
Jim Van Verth3af1af92017-05-18 15:06:54 -0400965static SkPoint3 map(const SkMatrix& m, const SkPoint3& pt) {
966 SkPoint3 result;
967 m.mapXY(pt.fX, pt.fY, (SkPoint*)&result.fX);
968 result.fZ = pt.fZ;
969 return result;
970}
971
972bool GrRenderTargetContext::drawFastShadow(const GrClip& clip,
Brian Salomon05969092017-07-13 11:20:51 -0400973 GrColor color4ub,
Jim Van Verth3af1af92017-05-18 15:06:54 -0400974 const SkMatrix& viewMatrix,
975 const SkPath& path,
976 const SkDrawShadowRec& rec) {
Jim Van Verthc5903412016-11-17 15:27:09 -0500977 ASSERT_SINGLE_OWNER
Jim Van Verth3af1af92017-05-18 15:06:54 -0400978 if (this->drawingManager()->wasAbandoned()) {
979 return true;
980 }
Jim Van Verthc5903412016-11-17 15:27:09 -0500981 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400982 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFastShadow", fContext);
Jim Van Verth3af1af92017-05-18 15:06:54 -0400983
984 // check z plane
985 bool tiltZPlane = SkToBool(!SkScalarNearlyZero(rec.fZPlaneParams.fX) ||
986 !SkScalarNearlyZero(rec.fZPlaneParams.fY));
987 bool skipAnalytic = SkToBool(rec.fFlags & SkShadowFlags::kGeometricOnly_ShadowFlag);
988 if (tiltZPlane || skipAnalytic || !viewMatrix.rectStaysRect() || !viewMatrix.isSimilarity()) {
989 return false;
990 }
991
992 SkRRect rrect;
993 SkRect rect;
994 // we can only handle rects, circles, and rrects with circular corners
995 bool isRRect = path.isRRect(&rrect) && rrect.isSimpleCircular() &&
996 rrect.radii(SkRRect::kUpperLeft_Corner).fX > SK_ScalarNearlyZero;
997 if (!isRRect &&
998 path.isOval(&rect) && SkScalarNearlyEqual(rect.width(), rect.height()) &&
999 rect.width() > SK_ScalarNearlyZero) {
1000 rrect.setOval(rect);
1001 isRRect = true;
1002 }
1003 if (!isRRect && path.isRect(&rect)) {
1004 rrect.setRect(rect);
1005 isRRect = true;
1006 }
1007
1008 if (!isRRect) {
1009 return false;
1010 }
1011
Jim Van Verthc5903412016-11-17 15:27:09 -05001012 if (rrect.isEmpty()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001013 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001014 }
1015
Robert Phillips72152832017-01-25 17:31:35 -05001016 AutoCheckFlush acf(this->drawingManager());
Jim Van Verthc5903412016-11-17 15:27:09 -05001017
Jim Van Verth3af1af92017-05-18 15:06:54 -04001018 // transform light
1019 SkPoint3 devLightPos = map(viewMatrix, rec.fLightPos);
1020
1021 // 1/scale
1022 SkScalar devToSrcScale = viewMatrix.isScaleTranslate() ?
1023 SkScalarInvert(viewMatrix[SkMatrix::kMScaleX]) :
1024 sk_float_rsqrt(viewMatrix[SkMatrix::kMScaleX] * viewMatrix[SkMatrix::kMScaleX] +
1025 viewMatrix[SkMatrix::kMSkewX] * viewMatrix[SkMatrix::kMSkewX]);
1026
1027 SkScalar occluderHeight = rec.fZPlaneParams.fZ;
Brian Salomon05969092017-07-13 11:20:51 -04001028 GrColor4f color = GrColor4f::FromGrColor(color4ub);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001029 bool transparent = SkToBool(rec.fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag);
Jim Van Verth34d6e4b2017-06-09 11:09:03 -04001030 bool tonalColor = SkToBool(rec.fFlags & SkShadowFlags::kTonalColor_ShadowFlag);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001031
1032 if (rec.fAmbientAlpha > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001033 SkScalar devSpaceInsetWidth = SkDrawShadowMetrics::AmbientBlurRadius(occluderHeight);
1034 const SkScalar umbraRecipAlpha = SkDrawShadowMetrics::AmbientRecipAlpha(occluderHeight);
1035 const SkScalar devSpaceAmbientBlur = devSpaceInsetWidth * umbraRecipAlpha;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001036
1037 // Outset the shadow rrect to the border of the penumbra
1038 SkScalar ambientPathOutset = devSpaceInsetWidth * devToSrcScale;
1039 SkRRect ambientRRect;
1040 SkRect outsetRect = rrect.rect().makeOutset(ambientPathOutset, ambientPathOutset);
1041 // If the rrect was an oval then its outset will also be one.
1042 // We set it explicitly to avoid errors.
1043 if (rrect.isOval()) {
1044 ambientRRect = SkRRect::MakeOval(outsetRect);
1045 } else {
1046 SkScalar outsetRad = rrect.getSimpleRadii().fX + ambientPathOutset;
1047 ambientRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1048 }
1049
Jim Van Verth34d6e4b2017-06-09 11:09:03 -04001050 GrColor ambientColor;
1051 if (tonalColor) {
1052 // with tonal color, the color only applies to the spot shadow
1053 ambientColor = GrColorPackRGBA(0, 0, 0, 255.999f*rec.fAmbientAlpha);
1054 } else {
1055 ambientColor = color.mulByScalar(rec.fAmbientAlpha).toGrColor();
1056 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001057 if (transparent) {
1058 // set a large inset to force a fill
1059 devSpaceInsetWidth = ambientRRect.width();
1060 }
1061 // the fraction of the blur we want to apply is devSpaceInsetWidth/devSpaceAmbientBlur,
Jim Van Verth1af03d42017-07-31 09:34:58 -04001062 // which is just 1/umbraRecipAlpha.
1063 SkScalar blurClamp = SkScalarInvert(umbraRecipAlpha);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001064
Brian Salomon05969092017-07-13 11:20:51 -04001065 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(ambientColor, viewMatrix,
1066 ambientRRect,
1067 devSpaceAmbientBlur,
1068 devSpaceInsetWidth,
1069 blurClamp);
1070 SkASSERT(op);
1071 this->addDrawOp(clip, std::move(op));
Jim Van Verthc5903412016-11-17 15:27:09 -05001072 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001073
1074 if (rec.fSpotAlpha > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001075 SkScalar devSpaceSpotBlur;
1076 SkScalar spotScale;
1077 SkVector spotOffset;
1078 SkDrawShadowMetrics::GetSpotParams(occluderHeight, devLightPos.fX, devLightPos.fY,
1079 devLightPos.fZ, rec.fLightRadius,
1080 &devSpaceSpotBlur, &spotScale, &spotOffset);
1081 // handle scale of radius due to CTM
Jim Van Verth3af1af92017-05-18 15:06:54 -04001082 const SkScalar srcSpaceSpotBlur = devSpaceSpotBlur * devToSrcScale;
1083
Jim Van Verth3af1af92017-05-18 15:06:54 -04001084 // Adjust translate for the effect of the scale.
1085 spotOffset.fX += spotScale*viewMatrix[SkMatrix::kMTransX];
1086 spotOffset.fY += spotScale*viewMatrix[SkMatrix::kMTransY];
1087 // This offset is in dev space, need to transform it into source space.
1088 SkMatrix ctmInverse;
1089 if (viewMatrix.invert(&ctmInverse)) {
1090 ctmInverse.mapPoints(&spotOffset, 1);
1091 } else {
1092 // Since the matrix is a similarity, this should never happen, but just in case...
1093 SkDebugf("Matrix is degenerate. Will not render spot shadow correctly!\n");
1094 SkASSERT(false);
1095 }
1096
1097 // Compute the transformed shadow rrect
1098 SkRRect spotShadowRRect;
1099 SkMatrix shadowTransform;
1100 shadowTransform.setScaleTranslate(spotScale, spotScale, spotOffset.fX, spotOffset.fY);
1101 rrect.transform(shadowTransform, &spotShadowRRect);
1102 SkScalar spotRadius = spotShadowRRect.getSimpleRadii().fX;
1103
1104 // Compute the insetWidth
Jim Van Verth1af03d42017-07-31 09:34:58 -04001105 SkScalar blurOutset = srcSpaceSpotBlur;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001106 SkScalar insetWidth = blurOutset;
1107 if (transparent) {
1108 // If transparent, just do a fill
1109 insetWidth += spotShadowRRect.width();
1110 } else {
1111 // For shadows, instead of using a stroke we specify an inset from the penumbra
1112 // border. We want to extend this inset area so that it meets up with the caster
1113 // geometry. The inset geometry will by default already be inset by the blur width.
1114 //
1115 // We compare the min and max corners inset by the radius between the original
1116 // rrect and the shadow rrect. The distance between the two plus the difference
1117 // between the scaled radius and the original radius gives the distance from the
1118 // transformed shadow shape to the original shape in that corner. The max
1119 // of these gives the maximum distance we need to cover.
1120 //
1121 // Since we are outsetting by 1/2 the blur distance, we just add the maxOffset to
1122 // that to get the full insetWidth.
1123 SkScalar maxOffset;
1124 if (rrect.isRect()) {
1125 // Manhattan distance works better for rects
1126 maxOffset = SkTMax(SkTMax(SkTAbs(spotShadowRRect.rect().fLeft -
1127 rrect.rect().fLeft),
1128 SkTAbs(spotShadowRRect.rect().fTop -
1129 rrect.rect().fTop)),
1130 SkTMax(SkTAbs(spotShadowRRect.rect().fRight -
1131 rrect.rect().fRight),
1132 SkTAbs(spotShadowRRect.rect().fBottom -
1133 rrect.rect().fBottom)));
1134 } else {
1135 SkScalar dr = spotRadius - rrect.getSimpleRadii().fX;
1136 SkPoint upperLeftOffset = SkPoint::Make(spotShadowRRect.rect().fLeft -
1137 rrect.rect().fLeft + dr,
1138 spotShadowRRect.rect().fTop -
1139 rrect.rect().fTop + dr);
1140 SkPoint lowerRightOffset = SkPoint::Make(spotShadowRRect.rect().fRight -
1141 rrect.rect().fRight - dr,
1142 spotShadowRRect.rect().fBottom -
1143 rrect.rect().fBottom - dr);
1144 maxOffset = SkScalarSqrt(SkTMax(upperLeftOffset.lengthSqd(),
1145 lowerRightOffset.lengthSqd())) + dr;
1146 }
1147 insetWidth += maxOffset;
1148 }
1149
1150 // Outset the shadow rrect to the border of the penumbra
1151 SkRect outsetRect = spotShadowRRect.rect().makeOutset(blurOutset, blurOutset);
1152 if (spotShadowRRect.isOval()) {
1153 spotShadowRRect = SkRRect::MakeOval(outsetRect);
1154 } else {
1155 SkScalar outsetRad = spotRadius + blurOutset;
1156 spotShadowRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1157 }
1158
Jim Van Verth34d6e4b2017-06-09 11:09:03 -04001159 GrColor spotColor;
1160 if (tonalColor) {
1161 SkScalar colorScale;
1162 SkScalar tonalAlpha;
1163 SkShadowUtils::ComputeTonalColorParams(color.fRGBA[0], color.fRGBA[1],
1164 color.fRGBA[2], rec.fSpotAlpha,
1165 &colorScale, &tonalAlpha);
1166 color.fRGBA[0] *= colorScale;
1167 color.fRGBA[1] *= colorScale;
1168 color.fRGBA[2] *= colorScale;
1169 color.fRGBA[3] = tonalAlpha;
1170 spotColor = color.toGrColor();
1171 } else {
1172 spotColor = color.mulByScalar(rec.fSpotAlpha).toGrColor();
1173 }
1174
Brian Salomon05969092017-07-13 11:20:51 -04001175 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(spotColor, viewMatrix,
1176 spotShadowRRect,
Jim Van Verth1af03d42017-07-31 09:34:58 -04001177 2.0f * devSpaceSpotBlur,
Brian Salomon05969092017-07-13 11:20:51 -04001178 insetWidth);
1179 SkASSERT(op);
1180 this->addDrawOp(clip, std::move(op));
Jim Van Verth3af1af92017-05-18 15:06:54 -04001181 }
1182
1183 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001184}
1185
1186///////////////////////////////////////////////////////////////////////////////
1187
Brian Osman11052242016-10-27 14:47:55 -04001188bool GrRenderTargetContext::drawFilledDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001189 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001190 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001191 const SkMatrix& viewMatrix,
1192 const SkRRect& origOuter,
1193 const SkRRect& origInner) {
robertphillips00095892016-02-29 13:50:40 -08001194 SkASSERT(!origInner.isEmpty());
1195 SkASSERT(!origOuter.isEmpty());
1196
csmartdaltone0d36292016-07-29 08:14:20 -07001197 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport()) {
Robert Phillipse3302df2017-04-24 07:31:02 -04001198 gr_instanced::OpAllocator* oa = this->drawingManager()->instancingAllocator();
1199 std::unique_ptr<GrDrawOp> op(oa->recordDRRect(
Brian Salomon54d212e2017-03-21 14:22:38 -04001200 origOuter, origInner, viewMatrix, std::move(paint), aa, fInstancedPipelineInfo));
Brian Salomon42521e82016-12-07 16:44:58 -05001201 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -04001202 this->addDrawOp(clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -07001203 return true;
1204 }
1205 }
1206
Brian Salomon7c8460e2017-05-12 11:36:10 -04001207 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
robertphillips00095892016-02-29 13:50:40 -08001208
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001209 GrPrimitiveEdgeType innerEdgeType, outerEdgeType;
1210 if (GrAAType::kCoverage == aaType) {
1211 innerEdgeType = kInverseFillAA_GrProcessorEdgeType;
1212 outerEdgeType = kFillAA_GrProcessorEdgeType;
1213 } else {
1214 innerEdgeType = kInverseFillBW_GrProcessorEdgeType;
1215 outerEdgeType = kFillBW_GrProcessorEdgeType;
1216 }
robertphillips00095892016-02-29 13:50:40 -08001217
1218 SkTCopyOnFirstWrite<SkRRect> inner(origInner), outer(origOuter);
1219 SkMatrix inverseVM;
1220 if (!viewMatrix.isIdentity()) {
1221 if (!origInner.transform(viewMatrix, inner.writable())) {
1222 return false;
1223 }
1224 if (!origOuter.transform(viewMatrix, outer.writable())) {
1225 return false;
1226 }
1227 if (!viewMatrix.invert(&inverseVM)) {
1228 return false;
1229 }
1230 } else {
1231 inverseVM.reset();
halcanary9d524f22016-03-29 09:03:52 -07001232 }
robertphillips00095892016-02-29 13:50:40 -08001233
robertphillips00095892016-02-29 13:50:40 -08001234 // TODO these need to be a geometry processors
Brian Salomonaff329b2017-08-11 09:40:37 -04001235 std::unique_ptr<GrFragmentProcessor> innerEffect(GrRRectEffect::Make(innerEdgeType, *inner));
robertphillips00095892016-02-29 13:50:40 -08001236 if (!innerEffect) {
1237 return false;
1238 }
1239
Brian Salomonaff329b2017-08-11 09:40:37 -04001240 std::unique_ptr<GrFragmentProcessor> outerEffect(GrRRectEffect::Make(outerEdgeType, *outer));
robertphillips00095892016-02-29 13:50:40 -08001241 if (!outerEffect) {
1242 return false;
1243 }
1244
Brian Salomon82f44312017-01-11 13:42:54 -05001245 paint.addCoverageFragmentProcessor(std::move(innerEffect));
1246 paint.addCoverageFragmentProcessor(std::move(outerEffect));
robertphillips00095892016-02-29 13:50:40 -08001247
1248 SkRect bounds = outer->getBounds();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001249 if (GrAAType::kCoverage == aaType) {
robertphillips00095892016-02-29 13:50:40 -08001250 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1251 }
halcanary9d524f22016-03-29 09:03:52 -07001252
Brian Salomon82f44312017-01-11 13:42:54 -05001253 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), bounds,
1254 inverseVM);
robertphillips00095892016-02-29 13:50:40 -08001255 return true;
1256}
1257
Brian Osman11052242016-10-27 14:47:55 -04001258void GrRenderTargetContext::drawDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001259 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001260 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001261 const SkMatrix& viewMatrix,
1262 const SkRRect& outer,
1263 const SkRRect& inner) {
robertphillips00095892016-02-29 13:50:40 -08001264 ASSERT_SINGLE_OWNER
1265 RETURN_IF_ABANDONED
1266 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001267 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawDRRect", fContext);
robertphillips00095892016-02-29 13:50:40 -08001268
1269 SkASSERT(!outer.isEmpty());
1270 SkASSERT(!inner.isEmpty());
1271
Robert Phillips72152832017-01-25 17:31:35 -05001272 AutoCheckFlush acf(this->drawingManager());
robertphillips00095892016-02-29 13:50:40 -08001273
Brian Salomon82f44312017-01-11 13:42:54 -05001274 if (this->drawFilledDRRect(clip, std::move(paint), aa, viewMatrix, outer, inner)) {
robertphillips00095892016-02-29 13:50:40 -08001275 return;
1276 }
1277
1278 SkPath path;
1279 path.setIsVolatile(true);
1280 path.addRRect(inner);
1281 path.addRRect(outer);
1282 path.setFillType(SkPath::kEvenOdd_FillType);
1283
Brian Salomon82f44312017-01-11 13:42:54 -05001284 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, GrStyle::SimpleFill());
robertphillips00095892016-02-29 13:50:40 -08001285}
1286
robertphillipsea461502015-05-26 11:38:03 -07001287///////////////////////////////////////////////////////////////////////////////
1288
Brian Osman11052242016-10-27 14:47:55 -04001289void GrRenderTargetContext::drawRegion(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001290 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001291 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001292 const SkMatrix& viewMatrix,
1293 const SkRegion& region,
Stan Iliev73d8fd92017-08-02 15:36:24 -04001294 const GrStyle& style,
1295 const GrUserStencilSettings* ss) {
msarettcc319b92016-08-25 18:07:18 -07001296 ASSERT_SINGLE_OWNER
1297 RETURN_IF_ABANDONED
1298 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001299 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRegion", fContext);
msarettcc319b92016-08-25 18:07:18 -07001300
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001301 if (GrAA::kYes == aa) {
Brian Salomonfc527d22016-12-14 21:07:01 -05001302 // GrRegionOp performs no antialiasing but is much faster, so here we check the matrix
Brian Salomonc57c7c92016-12-06 14:47:34 -05001303 // to see whether aa is really required.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001304 if (!SkToBool(viewMatrix.getType() & ~(SkMatrix::kTranslate_Mask)) &&
Brian Salomon34169692017-08-28 15:32:01 -04001305 SkScalarIsInt(viewMatrix.getTranslateX()) &&
1306 SkScalarIsInt(viewMatrix.getTranslateY())) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001307 aa = GrAA::kNo;
1308 }
Brian Salomonc57c7c92016-12-06 14:47:34 -05001309 }
msarettcc319b92016-08-25 18:07:18 -07001310 bool complexStyle = !style.isSimpleFill();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001311 if (complexStyle || GrAA::kYes == aa) {
msarettcc319b92016-08-25 18:07:18 -07001312 SkPath path;
1313 region.getBoundaryPath(&path);
Brian Salomon82f44312017-01-11 13:42:54 -05001314 return this->drawPath(clip, std::move(paint), aa, viewMatrix, path, style);
msarettcc319b92016-08-25 18:07:18 -07001315 }
1316
Brian Salomonf0366322017-07-11 15:53:05 -04001317 GrAAType aaType = this->chooseAAType(GrAA::kNo, GrAllowMixedSamples::kNo);
Stan Iliev73d8fd92017-08-02 15:36:24 -04001318 std::unique_ptr<GrDrawOp> op = GrRegionOp::Make(std::move(paint), viewMatrix, region, aaType,
1319 ss);
Brian Salomonf0366322017-07-11 15:53:05 -04001320 this->addDrawOp(clip, std::move(op));
msarettcc319b92016-08-25 18:07:18 -07001321}
1322
Brian Osman11052242016-10-27 14:47:55 -04001323void GrRenderTargetContext::drawOval(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001324 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001325 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001326 const SkMatrix& viewMatrix,
1327 const SkRect& oval,
1328 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001329 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001330 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001331 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001332 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawOval", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -07001333
robertphillipsea461502015-05-26 11:38:03 -07001334 if (oval.isEmpty()) {
1335 return;
1336 }
1337
bsalomon6663acf2016-05-10 09:14:17 -07001338 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
robertphillipsea461502015-05-26 11:38:03 -07001339
Robert Phillips72152832017-01-25 17:31:35 -05001340 AutoCheckFlush acf(this->drawingManager());
bsalomon6663acf2016-05-10 09:14:17 -07001341 const SkStrokeRec& stroke = style.strokeRec();
csmartdaltona7f29642016-07-07 08:49:11 -07001342
csmartdaltone0d36292016-07-29 08:14:20 -07001343 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport() &&
1344 stroke.isFillStyle()) {
Robert Phillipse3302df2017-04-24 07:31:02 -04001345 gr_instanced::OpAllocator* oa = this->drawingManager()->instancingAllocator();
Brian Salomon54d212e2017-03-21 14:22:38 -04001346 std::unique_ptr<GrDrawOp> op(
Robert Phillipse3302df2017-04-24 07:31:02 -04001347 oa->recordOval(oval, viewMatrix, std::move(paint), aa, fInstancedPipelineInfo));
Brian Salomon42521e82016-12-07 16:44:58 -05001348 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -04001349 this->addDrawOp(clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -07001350 return;
1351 }
1352 }
1353
Brian Salomon7c8460e2017-05-12 11:36:10 -04001354 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001355 if (GrAAType::kCoverage == aaType) {
Brian Salomon94efbf52016-11-29 13:43:05 -05001356 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomon05441c42017-05-15 16:45:49 -04001357 std::unique_ptr<GrDrawOp> op =
1358 GrOvalOpFactory::MakeOvalOp(std::move(paint), viewMatrix, oval, stroke, shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -05001359 if (op) {
Brian Salomon05441c42017-05-15 16:45:49 -04001360 this->addDrawOp(clip, std::move(op));
robertphillipsb56f9272016-02-25 11:03:52 -08001361 return;
1362 }
robertphillipsea461502015-05-26 11:38:03 -07001363 }
robertphillipsb56f9272016-02-25 11:03:52 -08001364
1365 SkPath path;
1366 path.setIsVolatile(true);
1367 path.addOval(oval);
Brian Salomon82f44312017-01-11 13:42:54 -05001368 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, style);
robertphillipsea461502015-05-26 11:38:03 -07001369}
1370
Brian Osman11052242016-10-27 14:47:55 -04001371void GrRenderTargetContext::drawArc(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001372 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001373 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001374 const SkMatrix& viewMatrix,
1375 const SkRect& oval,
1376 SkScalar startAngle,
1377 SkScalar sweepAngle,
1378 bool useCenter,
1379 const GrStyle& style) {
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001380 ASSERT_SINGLE_OWNER
1381 RETURN_IF_ABANDONED
1382 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001383 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawArc", fContext);
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001384
1385 AutoCheckFlush acf(this->drawingManager());
1386
Brian Salomon7c8460e2017-05-12 11:36:10 -04001387 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001388 if (GrAAType::kCoverage == aaType) {
Brian Salomon94efbf52016-11-29 13:43:05 -05001389 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomon05441c42017-05-15 16:45:49 -04001390 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeArcOp(std::move(paint),
1391 viewMatrix,
1392 oval,
1393 startAngle,
1394 sweepAngle,
1395 useCenter,
1396 style,
1397 shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -05001398 if (op) {
Brian Salomon05441c42017-05-15 16:45:49 -04001399 this->addDrawOp(clip, std::move(op));
bsalomon4f3a0ca2016-08-22 13:14:26 -07001400 return;
1401 }
1402 }
1403 SkPath path;
bsalomon21af9ca2016-08-25 12:29:23 -07001404 SkPathPriv::CreateDrawArcPath(&path, oval, startAngle, sweepAngle, useCenter,
1405 style.isSimpleFill());
Brian Salomon82f44312017-01-11 13:42:54 -05001406 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, style);
bsalomon4f3a0ca2016-08-22 13:14:26 -07001407}
1408
Brian Osman11052242016-10-27 14:47:55 -04001409void GrRenderTargetContext::drawImageLattice(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001410 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001411 const SkMatrix& viewMatrix,
1412 int imageWidth,
1413 int imageHeight,
1414 std::unique_ptr<SkLatticeIter> iter,
1415 const SkRect& dst) {
joshualitt1de610a2016-01-06 08:26:09 -08001416 ASSERT_SINGLE_OWNER
joshualitt33a5fce2015-11-18 13:28:51 -08001417 RETURN_IF_ABANDONED
1418 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001419 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawImageLattice", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001420
Robert Phillips72152832017-01-25 17:31:35 -05001421 AutoCheckFlush acf(this->drawingManager());
joshualitt33a5fce2015-11-18 13:28:51 -08001422
Brian Salomon815486c2017-07-11 08:52:13 -04001423 std::unique_ptr<GrDrawOp> op = GrLatticeOp::MakeNonAA(std::move(paint), viewMatrix, imageWidth,
1424 imageHeight, std::move(iter), dst);
1425 this->addDrawOp(clip, std::move(op));
joshualitt33a5fce2015-11-18 13:28:51 -08001426}
1427
Greg Daniel51316782017-08-02 15:10:09 +00001428GrSemaphoresSubmitted GrRenderTargetContext::prepareForExternalIO(
1429 int numSemaphores, GrBackendSemaphore backendSemaphores[]) {
robertphillips8c523e02016-07-26 07:41:00 -07001430 ASSERT_SINGLE_OWNER
Greg Daniel51316782017-08-02 15:10:09 +00001431 if (this->drawingManager()->wasAbandoned()) { return GrSemaphoresSubmitted::kNo; }
robertphillips8c523e02016-07-26 07:41:00 -07001432 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001433 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "prepareForExternalIO", fContext);
robertphillips8c523e02016-07-26 07:41:00 -07001434
Greg Daniel51316782017-08-02 15:10:09 +00001435 return this->drawingManager()->prepareSurfaceForExternalIO(fRenderTargetProxy.get(),
1436 numSemaphores,
1437 backendSemaphores);
Greg Daniela5cb7812017-06-16 09:45:32 -04001438}
1439
Greg Danielc64ee462017-06-15 16:59:49 -04001440bool GrRenderTargetContext::waitOnSemaphores(int numSemaphores,
Greg Daniela5cb7812017-06-16 09:45:32 -04001441 const GrBackendSemaphore* waitSemaphores) {
1442 ASSERT_SINGLE_OWNER
Greg Danielc64ee462017-06-15 16:59:49 -04001443 RETURN_FALSE_IF_ABANDONED
Greg Daniela5cb7812017-06-16 09:45:32 -04001444 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001445 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "waitOnSemaphores", fContext);
Greg Daniela5cb7812017-06-16 09:45:32 -04001446
1447 AutoCheckFlush acf(this->drawingManager());
1448
Greg Danielc64ee462017-06-15 16:59:49 -04001449 if (numSemaphores && !this->caps()->fenceSyncSupport()) {
1450 return false;
1451 }
1452
Greg Daniela5cb7812017-06-16 09:45:32 -04001453 SkTArray<sk_sp<GrSemaphore>> semaphores(numSemaphores);
1454 for (int i = 0; i < numSemaphores; ++i) {
1455 sk_sp<GrSemaphore> sema = fContext->resourceProvider()->wrapBackendSemaphore(
1456 waitSemaphores[i], kAdopt_GrWrapOwnership);
1457 std::unique_ptr<GrOp> waitOp(GrSemaphoreOp::MakeWait(sema, fRenderTargetProxy.get()));
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001458 this->getRTOpList()->addOp(std::move(waitOp), *this->caps());
Greg Daniela5cb7812017-06-16 09:45:32 -04001459 }
Greg Danielc64ee462017-06-15 16:59:49 -04001460 return true;
robertphillips8c523e02016-07-26 07:41:00 -07001461}
joshualitt33a5fce2015-11-18 13:28:51 -08001462
Robert Phillips65a88fa2017-08-08 08:36:22 -04001463void GrRenderTargetContext::insertEventMarker(const SkString& str) {
1464 std::unique_ptr<GrOp> op(GrDebugMarkerOp::Make(fRenderTargetProxy.get(), str));
1465 this->getRTOpList()->addOp(std::move(op), *this->caps());
1466}
1467
1468
robertphillipsea461502015-05-26 11:38:03 -07001469// Can 'path' be drawn as a pair of filled nested rectangles?
bsalomon6663acf2016-05-10 09:14:17 -07001470static bool fills_as_nested_rects(const SkMatrix& viewMatrix, const SkPath& path, SkRect rects[2]) {
robertphillipsea461502015-05-26 11:38:03 -07001471
1472 if (path.isInverseFillType()) {
1473 return false;
1474 }
1475
1476 // TODO: this restriction could be lifted if we were willing to apply
1477 // the matrix to all the points individually rather than just to the rect
robertphillips0e7029e2015-11-30 05:45:06 -08001478 if (!viewMatrix.rectStaysRect()) {
robertphillipsea461502015-05-26 11:38:03 -07001479 return false;
1480 }
1481
1482 SkPath::Direction dirs[2];
1483 if (!path.isNestedFillRects(rects, dirs)) {
1484 return false;
1485 }
1486
1487 if (SkPath::kWinding_FillType == path.getFillType() && dirs[0] == dirs[1]) {
1488 // The two rects need to be wound opposite to each other
1489 return false;
1490 }
1491
1492 // Right now, nested rects where the margin is not the same width
1493 // all around do not render correctly
1494 const SkScalar* outer = rects[0].asScalars();
1495 const SkScalar* inner = rects[1].asScalars();
1496
1497 bool allEq = true;
1498
1499 SkScalar margin = SkScalarAbs(outer[0] - inner[0]);
1500 bool allGoE1 = margin >= SK_Scalar1;
1501
1502 for (int i = 1; i < 4; ++i) {
1503 SkScalar temp = SkScalarAbs(outer[i] - inner[i]);
1504 if (temp < SK_Scalar1) {
1505 allGoE1 = false;
1506 }
1507 if (!SkScalarNearlyEqual(margin, temp)) {
1508 allEq = false;
1509 }
1510 }
1511
1512 return allEq || allGoE1;
1513}
1514
Brian Osman11052242016-10-27 14:47:55 -04001515void GrRenderTargetContext::drawPath(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001516 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001517 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001518 const SkMatrix& viewMatrix,
1519 const SkPath& path,
1520 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001521 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001522 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001523 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001524 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawPath", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -07001525
robertphillipsea461502015-05-26 11:38:03 -07001526 if (path.isEmpty()) {
1527 if (path.isInverseFillType()) {
Brian Salomon82f44312017-01-11 13:42:54 -05001528 this->drawPaint(clip, std::move(paint), viewMatrix);
robertphillipsea461502015-05-26 11:38:03 -07001529 }
1530 return;
1531 }
1532
Robert Phillips72152832017-01-25 17:31:35 -05001533 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -07001534
Brian Salomon7c8460e2017-05-12 11:36:10 -04001535 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001536 if (GrAAType::kCoverage == aaType && !style.pathEffect()) {
bsalomon6663acf2016-05-10 09:14:17 -07001537 if (style.isSimpleFill() && !path.isConvex()) {
robertphillipsea461502015-05-26 11:38:03 -07001538 // Concave AA paths are expensive - try to avoid them for special cases
1539 SkRect rects[2];
1540
bsalomon6663acf2016-05-10 09:14:17 -07001541 if (fills_as_nested_rects(viewMatrix, path, rects)) {
Brian Salomonbaaf4392017-06-15 09:59:23 -04001542 std::unique_ptr<GrDrawOp> op =
1543 GrRectOpFactory::MakeAAFillNestedRects(std::move(paint), viewMatrix, rects);
Brian Salomon5f970fe2017-06-16 17:30:59 -04001544 if (op) {
1545 this->addDrawOp(clip, std::move(op));
bsalomon40ef4852016-05-02 13:22:13 -07001546 }
Brian Salomon5f970fe2017-06-16 17:30:59 -04001547 // A null return indicates that there is nothing to draw in this case.
1548 return;
robertphillipsea461502015-05-26 11:38:03 -07001549 }
1550 }
1551 SkRect ovalRect;
1552 bool isOval = path.isOval(&ovalRect);
1553
1554 if (isOval && !path.isInverseFillType()) {
Brian Salomon94efbf52016-11-29 13:43:05 -05001555 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomon05441c42017-05-15 16:45:49 -04001556 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeOvalOp(
1557 std::move(paint), viewMatrix, ovalRect, style.strokeRec(), shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -05001558 if (op) {
Brian Salomon05441c42017-05-15 16:45:49 -04001559 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -07001560 return;
1561 }
1562 }
1563 }
robertphillips4bc31812016-03-01 12:22:49 -08001564
1565 // Note that internalDrawPath may sw-rasterize the path into a scratch texture.
1566 // Scratch textures can be recycled after they are returned to the texture
1567 // cache. This presents a potential hazard for buffered drawing. However,
1568 // the writePixels that uploads to the scratch will perform a flush so we're
1569 // OK.
Brian Salomon82f44312017-01-11 13:42:54 -05001570 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, style);
robertphillipsea461502015-05-26 11:38:03 -07001571}
1572
Brian Osman11052242016-10-27 14:47:55 -04001573bool GrRenderTargetContextPriv::drawAndStencilPath(const GrClip& clip,
1574 const GrUserStencilSettings* ss,
1575 SkRegion::Op op,
1576 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001577 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001578 const SkMatrix& viewMatrix,
1579 const SkPath& path) {
robertphillips391395d2016-03-02 09:26:36 -08001580 ASSERT_SINGLE_OWNER_PRIV
1581 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -04001582 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001583 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawAndStencilPath",
1584 fRenderTargetContext->fContext);
robertphillips391395d2016-03-02 09:26:36 -08001585
1586 if (path.isEmpty() && path.isInverseFillType()) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001587 this->drawAndStencilRect(clip, ss, op, invert, GrAA::kNo, SkMatrix::I(),
Brian Osman11052242016-10-27 14:47:55 -04001588 SkRect::MakeIWH(fRenderTargetContext->width(),
1589 fRenderTargetContext->height()));
robertphillips391395d2016-03-02 09:26:36 -08001590 return true;
1591 }
1592
Robert Phillips72152832017-01-25 17:31:35 -05001593 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -08001594
1595 // An Assumption here is that path renderer would use some form of tweaking
1596 // the src color (either the input alpha or in the frag shader) to implement
1597 // aa. If we have some future driver-mojo path AA that can do the right
1598 // thing WRT to the blend then we'll need some query on the PR.
Brian Salomon7c8460e2017-05-12 11:36:10 -04001599 GrAAType aaType = fRenderTargetContext->chooseAAType(aa, GrAllowMixedSamples::kNo);
robertphillips976f5f02016-06-03 10:59:20 -07001600 bool hasUserStencilSettings = !ss->isUnused();
robertphillips391395d2016-03-02 09:26:36 -08001601
bsalomon8acedde2016-06-24 10:42:16 -07001602 GrShape shape(path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -08001603 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Eric Karl5c779752017-05-08 12:02:07 -07001604 canDrawArgs.fCaps = fRenderTargetContext->drawingManager()->getContext()->caps();
robertphillips391395d2016-03-02 09:26:36 -08001605 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07001606 canDrawArgs.fShape = &shape;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001607 canDrawArgs.fAAType = aaType;
cdalton93a379b2016-05-11 13:58:08 -07001608 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips391395d2016-03-02 09:26:36 -08001609
1610 // Don't allow the SW renderer
Robert Phillips72152832017-01-25 17:31:35 -05001611 GrPathRenderer* pr = fRenderTargetContext->drawingManager()->getPathRenderer(
Brian Salomon36aa1762016-12-10 13:24:02 -05001612 canDrawArgs, false, GrPathRendererChain::DrawType::kStencilAndColor);
robertphillips391395d2016-03-02 09:26:36 -08001613 if (!pr) {
1614 return false;
1615 }
1616
1617 GrPaint paint;
1618 paint.setCoverageSetOpXPFactory(op, invert);
1619
Brian Salomon82f44312017-01-11 13:42:54 -05001620 GrPathRenderer::DrawPathArgs args{
Robert Phillips256c37b2017-03-01 14:32:46 -05001621 fRenderTargetContext->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05001622 std::move(paint),
1623 ss,
1624 fRenderTargetContext,
1625 &clip,
1626 &viewMatrix,
1627 &shape,
1628 aaType,
1629 fRenderTargetContext->isGammaCorrect()};
robertphillips391395d2016-03-02 09:26:36 -08001630 pr->drawPath(args);
1631 return true;
1632}
1633
Brian Osman11052242016-10-27 14:47:55 -04001634SkBudgeted GrRenderTargetContextPriv::isBudgeted() const {
robertphillips714712b2016-08-04 06:20:45 -07001635 ASSERT_SINGLE_OWNER_PRIV
1636
Brian Osman11052242016-10-27 14:47:55 -04001637 if (fRenderTargetContext->wasAbandoned()) {
robertphillips714712b2016-08-04 06:20:45 -07001638 return SkBudgeted::kNo;
1639 }
1640
Brian Osman11052242016-10-27 14:47:55 -04001641 SkDEBUGCODE(fRenderTargetContext->validate();)
robertphillips714712b2016-08-04 06:20:45 -07001642
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001643 return fRenderTargetContext->fRenderTargetProxy->isBudgeted();
robertphillips714712b2016-08-04 06:20:45 -07001644}
1645
Brian Osman11052242016-10-27 14:47:55 -04001646void GrRenderTargetContext::internalDrawPath(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001647 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001648 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001649 const SkMatrix& viewMatrix,
1650 const SkPath& path,
1651 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001652 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001653 RETURN_IF_ABANDONED
Brian Salomondcbb9d92017-07-19 10:53:20 -04001654 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "internalDrawPath", fContext);
1655
bsalomon8acedde2016-06-24 10:42:16 -07001656 SkASSERT(!path.isEmpty());
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001657 GrShape shape;
Brian Salomon7c8460e2017-05-12 11:36:10 -04001658 // NVPR cannot handle hairlines, so this would get picked up by a different stencil and
1659 // cover path renderer (i.e. default path renderer). The hairline renderer produces much
1660 // smoother hairlines than MSAA.
1661 GrAllowMixedSamples allowMixedSamples =
1662 style.isSimpleHairline() ? GrAllowMixedSamples::kNo : GrAllowMixedSamples::kYes;
1663 GrAAType aaType = this->chooseAAType(aa, allowMixedSamples);
robertphillips68737822015-10-29 12:12:21 -07001664 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Eric Karl5c779752017-05-08 12:02:07 -07001665 canDrawArgs.fCaps = this->drawingManager()->getContext()->caps();
robertphillips68737822015-10-29 12:12:21 -07001666 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07001667 canDrawArgs.fShape = &shape;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001668 canDrawArgs.fHasUserStencilSettings = false;
robertphillips68737822015-10-29 12:12:21 -07001669
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001670 GrPathRenderer* pr;
Brian Salomon82125e92016-12-10 09:35:48 -05001671 static constexpr GrPathRendererChain::DrawType kType = GrPathRendererChain::DrawType::kColor;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001672 do {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001673 shape = GrShape(path, style);
Brian Salomon085c0862017-08-31 15:44:51 -04001674 if (shape.isEmpty() && !shape.inverseFilled()) {
robertphillipsea461502015-05-26 11:38:03 -07001675 return;
1676 }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001677
1678 canDrawArgs.fAAType = aaType;
1679
1680 // Try a 1st time without applying any of the style to the geometry (and barring sw)
Robert Phillips72152832017-01-25 17:31:35 -05001681 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001682 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
1683
1684 if (!pr && shape.style().pathEffect()) {
1685 // It didn't work above, so try again with the path effect applied.
1686 shape = shape.applyStyle(GrStyle::Apply::kPathEffectOnly, styleScale);
bsalomon8acedde2016-06-24 10:42:16 -07001687 if (shape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001688 return;
1689 }
Robert Phillips72152832017-01-25 17:31:35 -05001690 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
bsalomon6663acf2016-05-10 09:14:17 -07001691 }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001692 if (!pr) {
1693 if (shape.style().applies()) {
1694 shape = shape.applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec, styleScale);
1695 if (shape.isEmpty()) {
1696 return;
1697 }
1698 }
1699 // This time, allow SW renderer
Robert Phillips72152832017-01-25 17:31:35 -05001700 pr = this->drawingManager()->getPathRenderer(canDrawArgs, true, kType);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001701 }
Brian Salomon0abc8b42016-12-13 10:22:54 -05001702 if (!pr && GrAATypeIsHW(aaType)) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001703 // There are exceptional cases where we may wind up falling back to coverage based AA
1704 // when the target is MSAA (e.g. through disabling path renderers via GrContextOptions).
1705 aaType = GrAAType::kCoverage;
1706 } else {
1707 break;
1708 }
1709 } while(true);
robertphillipsea461502015-05-26 11:38:03 -07001710
bsalomon8acedde2016-06-24 10:42:16 -07001711 if (!pr) {
robertphillipsea461502015-05-26 11:38:03 -07001712#ifdef SK_DEBUG
1713 SkDebugf("Unable to find path renderer compatible with path.\n");
1714#endif
1715 return;
1716 }
1717
Robert Phillips256c37b2017-03-01 14:32:46 -05001718 GrPathRenderer::DrawPathArgs args{this->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05001719 std::move(paint),
1720 &GrUserStencilSettings::kUnused,
1721 this,
1722 &clip,
1723 &viewMatrix,
1724 &shape,
1725 aaType,
1726 this->isGammaCorrect()};
bsalomon0aff2fa2015-07-31 06:48:27 -07001727 pr->drawPath(args);
robertphillipsea461502015-05-26 11:38:03 -07001728}
1729
Brian Salomon467921e2017-03-06 16:17:12 -05001730static void op_bounds(SkRect* bounds, const GrOp* op) {
1731 *bounds = op->bounds();
1732 if (op->hasZeroArea()) {
1733 if (op->hasAABloat()) {
1734 bounds->outset(0.5f, 0.5f);
1735 } else {
1736 // We don't know which way the particular GPU will snap lines or points at integer
1737 // coords. So we ensure that the bounds is large enough for either snap.
1738 SkRect before = *bounds;
1739 bounds->roundOut(bounds);
1740 if (bounds->fLeft == before.fLeft) {
1741 bounds->fLeft -= 1;
1742 }
1743 if (bounds->fTop == before.fTop) {
1744 bounds->fTop -= 1;
1745 }
1746 if (bounds->fRight == before.fRight) {
1747 bounds->fRight += 1;
1748 }
1749 if (bounds->fBottom == before.fBottom) {
1750 bounds->fBottom += 1;
1751 }
1752 }
1753 }
1754}
1755
Brian Salomon54d212e2017-03-21 14:22:38 -04001756uint32_t GrRenderTargetContext::addDrawOp(const GrClip& clip, std::unique_ptr<GrDrawOp> op) {
joshualitt1de610a2016-01-06 08:26:09 -08001757 ASSERT_SINGLE_OWNER
Robert Phillipsc0138922017-03-08 11:50:55 -05001758 if (this->drawingManager()->wasAbandoned()) {
1759 return SK_InvalidUniqueID;
1760 }
robertphillips2e1e51f2015-10-15 08:01:48 -07001761 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001762 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "addDrawOp", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -07001763
Brian Salomon467921e2017-03-06 16:17:12 -05001764 // Setup clip
1765 SkRect bounds;
1766 op_bounds(&bounds, op.get());
Brian Salomon97180af2017-03-14 13:42:58 -04001767 GrAppliedClip appliedClip;
Brian Salomon54d212e2017-03-21 14:22:38 -04001768 GrDrawOp::FixedFunctionFlags fixedFunctionFlags = op->fixedFunctionFlags();
1769 if (!clip.apply(fContext, this, fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesHWAA,
1770 fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil, &appliedClip,
1771 &bounds)) {
1772 return SK_InvalidUniqueID;
1773 }
1774
Brian Salomon54d212e2017-03-21 14:22:38 -04001775 if (fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil ||
1776 appliedClip.hasStencilClip()) {
Robert Phillips6b47c7d2017-08-29 07:24:09 -04001777 this->getOpList()->setStencilLoadOp(GrLoadOp::kClear);
Robert Phillips95214472017-08-08 18:00:03 -04001778
Robert Phillips65048132017-08-10 08:44:49 -04001779 this->setNeedsStencil();
Brian Salomon54d212e2017-03-21 14:22:38 -04001780 }
1781
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001782 GrXferProcessor::DstProxy dstProxy;
Brian Salomonf86d37b2017-06-16 10:04:34 -04001783 if (op->finalize(*this->caps(), &appliedClip) == GrDrawOp::RequiresDstTexture::kYes) {
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001784 if (!this->setupDstProxy(this->asRenderTargetProxy(), clip, op->bounds(), &dstProxy)) {
Brian Salomon54d212e2017-03-21 14:22:38 -04001785 return SK_InvalidUniqueID;
1786 }
1787 }
1788
1789 op->setClippedBounds(bounds);
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001790 return this->getRTOpList()->addOp(std::move(op), *this->caps(),
1791 std::move(appliedClip), dstProxy);
Brian Salomon54d212e2017-03-21 14:22:38 -04001792}
1793
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001794bool GrRenderTargetContext::setupDstProxy(GrRenderTargetProxy* rtProxy, const GrClip& clip,
Robert Phillips16d8ec62017-07-27 16:16:25 -04001795 const SkRect& opBounds,
1796 GrXferProcessor::DstProxy* dstProxy) {
Brian Salomon467921e2017-03-06 16:17:12 -05001797 if (this->caps()->textureBarrierSupport()) {
Robert Phillipsbf25d432017-04-07 10:08:53 -04001798 if (GrTextureProxy* texProxy = rtProxy->asTextureProxy()) {
Brian Salomon467921e2017-03-06 16:17:12 -05001799 // The render target is a texture, so we can read from it directly in the shader. The XP
1800 // will be responsible to detect this situation and request a texture barrier.
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001801 dstProxy->setProxy(sk_ref_sp(texProxy));
1802 dstProxy->setOffset(0, 0);
Robert Phillipsbf25d432017-04-07 10:08:53 -04001803 return true;
Brian Salomon467921e2017-03-06 16:17:12 -05001804 }
1805 }
1806
Robert Phillipsbf25d432017-04-07 10:08:53 -04001807 SkIRect copyRect = SkIRect::MakeWH(rtProxy->width(), rtProxy->height());
Brian Salomon467921e2017-03-06 16:17:12 -05001808
Eric Karl74480882017-04-03 14:49:05 -07001809 SkIRect clippedRect;
Robert Phillipsbf25d432017-04-07 10:08:53 -04001810 clip.getConservativeBounds(rtProxy->width(), rtProxy->height(), &clippedRect);
Eric Karl72e551e2017-04-04 13:42:10 -07001811 SkIRect drawIBounds;
Brian Salomon467921e2017-03-06 16:17:12 -05001812 opBounds.roundOut(&drawIBounds);
Brian Salomon859621f2017-03-16 09:21:54 -04001813 // Cover up for any precision issues by outsetting the op bounds a pixel in each direction.
1814 drawIBounds.outset(1, 1);
Eric Karl72e551e2017-04-04 13:42:10 -07001815 if (!clippedRect.intersect(drawIBounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -05001816#ifdef SK_DEBUG
Robert Phillipsbf25d432017-04-07 10:08:53 -04001817 GrCapsDebugf(this->caps(), "setupDstTexture: Missed an early reject bailing on draw.");
Brian Salomon467921e2017-03-06 16:17:12 -05001818#endif
Robert Phillipsbf25d432017-04-07 10:08:53 -04001819 return false;
Brian Salomon467921e2017-03-06 16:17:12 -05001820 }
1821
1822 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
1823 // have per-sample dst values by making the copy multisampled.
1824 GrSurfaceDesc desc;
Eric Karl74480882017-04-03 14:49:05 -07001825 bool rectsMustMatch = false;
1826 bool disallowSubrect = false;
Robert Phillipsbf25d432017-04-07 10:08:53 -04001827 if (!this->caps()->initDescForDstCopy(rtProxy, &desc, &rectsMustMatch, &disallowSubrect)) {
Brian Salomon467921e2017-03-06 16:17:12 -05001828 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillips16d8ec62017-07-27 16:16:25 -04001829 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
Robert Phillipsbf25d432017-04-07 10:08:53 -04001830 desc.fConfig = rtProxy->config();
Brian Salomon467921e2017-03-06 16:17:12 -05001831 }
1832
Eric Karl74480882017-04-03 14:49:05 -07001833 if (!disallowSubrect) {
1834 copyRect = clippedRect;
1835 }
Brian Salomon467921e2017-03-06 16:17:12 -05001836
Robert Phillipsbf25d432017-04-07 10:08:53 -04001837 SkIPoint dstPoint, dstOffset;
1838 SkBackingFit fit;
Eric Karl74480882017-04-03 14:49:05 -07001839 if (rectsMustMatch) {
Robert Phillipsbf25d432017-04-07 10:08:53 -04001840 SkASSERT(desc.fOrigin == rtProxy->origin());
1841 desc.fWidth = rtProxy->width();
1842 desc.fHeight = rtProxy->height();
Eric Karl74480882017-04-03 14:49:05 -07001843 dstPoint = {copyRect.fLeft, copyRect.fTop};
1844 dstOffset = {0, 0};
Robert Phillipsbf25d432017-04-07 10:08:53 -04001845 fit = SkBackingFit::kExact;
Eric Karl74480882017-04-03 14:49:05 -07001846 } else {
1847 desc.fWidth = copyRect.width();
1848 desc.fHeight = copyRect.height();
1849 dstPoint = {0, 0};
1850 dstOffset = {copyRect.fLeft, copyRect.fTop};
Robert Phillipsbf25d432017-04-07 10:08:53 -04001851 fit = SkBackingFit::kApprox;
Eric Karl74480882017-04-03 14:49:05 -07001852 }
Brian Salomon467921e2017-03-06 16:17:12 -05001853
Robert Phillipsbf25d432017-04-07 10:08:53 -04001854 sk_sp<GrSurfaceContext> sContext = fContext->contextPriv().makeDeferredSurfaceContext(
1855 desc,
1856 fit,
1857 SkBudgeted::kYes);
1858 if (!sContext) {
1859 SkDebugf("setupDstTexture: surfaceContext creation failed.\n");
1860 return false;
1861 }
1862
1863 if (!sContext->copy(rtProxy, copyRect, dstPoint)) {
1864 SkDebugf("setupDstTexture: copy failed.\n");
1865 return false;
1866 }
1867
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001868 dstProxy->setProxy(sContext->asTextureProxyRef());
1869 dstProxy->setOffset(dstOffset);
Robert Phillipsbf25d432017-04-07 10:08:53 -04001870 return true;
robertphillips2334fb62015-06-17 05:43:33 -07001871}