blob: 66a707b52dfdf58cd03cdda4401ecad7b3c5f879 [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 Phillipse4407212017-04-13 10:13:16 -040033#include "ops/GrDiscardOp.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"
joshualitte8042922015-12-11 06:11:21 -080045#include "text/GrAtlasTextContext.h"
46#include "text/GrStencilAndCoverTextContext.h"
joshualittbc907352016-01-13 06:45:40 -080047
Robert Phillips72152832017-01-25 17:31:35 -050048#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this->drawingManager()->getContext())
joshualitt1de610a2016-01-06 08:26:09 -080049#define ASSERT_SINGLE_OWNER \
Robert Phillipsa90aa2b2017-04-10 08:19:26 -040050 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(this->singleOwner());)
robertphillips391395d2016-03-02 09:26:36 -080051#define ASSERT_SINGLE_OWNER_PRIV \
Robert Phillipsa90aa2b2017-04-10 08:19:26 -040052 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fRenderTargetContext->singleOwner());)
Robert Phillips72152832017-01-25 17:31:35 -050053#define RETURN_IF_ABANDONED if (this->drawingManager()->wasAbandoned()) { return; }
54#define RETURN_IF_ABANDONED_PRIV if (fRenderTargetContext->drawingManager()->wasAbandoned()) { return; }
55#define RETURN_FALSE_IF_ABANDONED if (this->drawingManager()->wasAbandoned()) { return false; }
56#define RETURN_FALSE_IF_ABANDONED_PRIV if (fRenderTargetContext->drawingManager()->wasAbandoned()) { return false; }
57#define RETURN_NULL_IF_ABANDONED if (this->drawingManager()->wasAbandoned()) { return nullptr; }
robertphillipsea461502015-05-26 11:38:03 -070058
Brian Salomone225b562017-06-14 13:00:03 -040059//////////////////////////////////////////////////////////////////////////////
60
61GrAAType GrChooseAAType(GrAA aa, GrFSAAType fsaaType, GrAllowMixedSamples allowMixedSamples,
62 const GrCaps& caps) {
63 if (GrAA::kNo == aa) {
64 // On some devices we cannot disable MSAA if it is enabled so we make the AA type reflect
65 // that.
66 if (fsaaType == GrFSAAType::kUnifiedMSAA && !caps.multisampleDisableSupport()) {
67 return GrAAType::kMSAA;
68 }
69 return GrAAType::kNone;
70 }
71 switch (fsaaType) {
72 case GrFSAAType::kNone:
73 return GrAAType::kCoverage;
74 case GrFSAAType::kUnifiedMSAA:
75 return GrAAType::kMSAA;
76 case GrFSAAType::kMixedSamples:
77 return GrAllowMixedSamples::kYes == allowMixedSamples ? GrAAType::kMixedSamples
78 : GrAAType::kCoverage;
79 }
80 SkFAIL("Unexpected fsaa type");
81 return GrAAType::kNone;
82}
83
84//////////////////////////////////////////////////////////////////////////////
85
robertphillipsea461502015-05-26 11:38:03 -070086class AutoCheckFlush {
87public:
halcanary9d524f22016-03-29 09:03:52 -070088 AutoCheckFlush(GrDrawingManager* drawingManager) : fDrawingManager(drawingManager) {
robertphillips77a2e522015-10-17 07:43:27 -070089 SkASSERT(fDrawingManager);
90 }
bsalomonb77a9072016-09-07 10:02:04 -070091 ~AutoCheckFlush() { fDrawingManager->flushIfNecessary(); }
robertphillipsea461502015-05-26 11:38:03 -070092
93private:
robertphillips77a2e522015-10-17 07:43:27 -070094 GrDrawingManager* fDrawingManager;
robertphillipsea461502015-05-26 11:38:03 -070095};
96
Brian Osman11052242016-10-27 14:47:55 -040097bool GrRenderTargetContext::wasAbandoned() const {
Robert Phillips72152832017-01-25 17:31:35 -050098 return this->drawingManager()->wasAbandoned();
robertphillips7761d612016-05-16 09:14:53 -070099}
100
Robert Phillipsf2361d22016-10-25 14:20:06 -0400101// In MDB mode the reffing of the 'getLastOpList' call's result allows in-progress
Brian Osman11052242016-10-27 14:47:55 -0400102// GrOpLists to be picked up and added to by renderTargetContexts lower in the call
Robert Phillipsf2361d22016-10-25 14:20:06 -0400103// stack. When this occurs with a closed GrOpList, a new one will be allocated
Brian Osman11052242016-10-27 14:47:55 -0400104// when the renderTargetContext attempts to use it (via getOpList).
105GrRenderTargetContext::GrRenderTargetContext(GrContext* context,
106 GrDrawingManager* drawingMgr,
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400107 sk_sp<GrRenderTargetProxy> rtp,
Brian Osman11052242016-10-27 14:47:55 -0400108 sk_sp<SkColorSpace> colorSpace,
109 const SkSurfaceProps* surfaceProps,
110 GrAuditTrail* auditTrail,
Robert Phillips941d1442017-06-14 16:37:02 -0400111 GrSingleOwner* singleOwner,
112 bool managedOpList)
Robert Phillips72152832017-01-25 17:31:35 -0500113 : GrSurfaceContext(context, drawingMgr, std::move(colorSpace), auditTrail, singleOwner)
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400114 , fRenderTargetProxy(std::move(rtp))
Robert Phillipsdc83b892017-04-13 12:23:54 -0400115 , fOpList(sk_ref_sp(fRenderTargetProxy->getLastRenderTargetOpList()))
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400116 , fInstancedPipelineInfo(fRenderTargetProxy.get())
brianosman5a7ae7e2016-09-12 12:07:25 -0700117 , fColorXformFromSRGB(nullptr)
Robert Phillips941d1442017-06-14 16:37:02 -0400118 , fSurfaceProps(SkSurfacePropsCopyOrDefault(surfaceProps))
119 , fManagedOpList(managedOpList) {
brianosman5a7ae7e2016-09-12 12:07:25 -0700120 if (fColorSpace) {
121 // sRGB sources are very common (SkColor, etc...), so we cache that gamut transformation
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500122 auto srgbColorSpace = SkColorSpace::MakeSRGB();
msarettc71a9b72016-09-16 11:01:27 -0700123 fColorXformFromSRGB = GrColorSpaceXform::Make(srgbColorSpace.get(), fColorSpace.get());
brianosman5a7ae7e2016-09-12 12:07:25 -0700124 }
Robert Phillipsd9914862017-06-14 15:16:59 -0400125
126 // MDB TODO: to ensure all resources still get allocated in the correct order in the hybrid
127 // world we need to get the correct opList here so that it, in turn, can grab and hold
128 // its rendertarget.
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400129 this->getRTOpList();
robertphillips2e1e51f2015-10-15 08:01:48 -0700130 SkDEBUGCODE(this->validate();)
robertphillipsea461502015-05-26 11:38:03 -0700131}
132
robertphillips2e1e51f2015-10-15 08:01:48 -0700133#ifdef SK_DEBUG
Brian Osman11052242016-10-27 14:47:55 -0400134void GrRenderTargetContext::validate() const {
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400135 SkASSERT(fRenderTargetProxy);
136 fRenderTargetProxy->validate(fContext);
robertphillipsa106c622015-10-16 09:07:06 -0700137
Robert Phillipsf2361d22016-10-25 14:20:06 -0400138 if (fOpList && !fOpList->isClosed()) {
Robert Phillipsdc83b892017-04-13 12:23:54 -0400139 SkASSERT(fRenderTargetProxy->getLastOpList() == fOpList.get());
robertphillipsa106c622015-10-16 09:07:06 -0700140 }
robertphillips2e1e51f2015-10-15 08:01:48 -0700141}
142#endif
143
Brian Osman11052242016-10-27 14:47:55 -0400144GrRenderTargetContext::~GrRenderTargetContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800145 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700146}
147
Robert Phillipsf200a902017-01-30 13:27:37 -0500148GrTextureProxy* GrRenderTargetContext::asTextureProxy() {
Robert Phillipseaa86252016-11-08 13:49:39 +0000149 return fRenderTargetProxy->asTextureProxy();
150}
151
Robert Phillipsf200a902017-01-30 13:27:37 -0500152sk_sp<GrTextureProxy> GrRenderTargetContext::asTextureProxyRef() {
153 return sk_ref_sp(fRenderTargetProxy->asTextureProxy());
154}
155
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400156GrRenderTargetOpList* GrRenderTargetContext::getRTOpList() {
joshualitt1de610a2016-01-06 08:26:09 -0800157 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700158 SkDEBUGCODE(this->validate();)
159
Robert Phillipsf2361d22016-10-25 14:20:06 -0400160 if (!fOpList || fOpList->isClosed()) {
Robert Phillips941d1442017-06-14 16:37:02 -0400161 fOpList = this->drawingManager()->newRTOpList(fRenderTargetProxy.get(), fManagedOpList);
robertphillipsa106c622015-10-16 09:07:06 -0700162 }
163
Robert Phillipsdc83b892017-04-13 12:23:54 -0400164 return fOpList.get();
robertphillipsa106c622015-10-16 09:07:06 -0700165}
166
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400167GrOpList* GrRenderTargetContext::getOpList() {
168 return this->getRTOpList();
robertphillipsea461502015-05-26 11:38:03 -0700169}
170
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500171void GrRenderTargetContext::drawText(const GrClip& clip, const SkPaint& skPaint,
Brian Salomon82f44312017-01-11 13:42:54 -0500172 const SkMatrix& viewMatrix, const char text[],
173 size_t byteLength, SkScalar x, SkScalar y,
174 const SkIRect& clipBounds) {
joshualitt1de610a2016-01-06 08:26:09 -0800175 ASSERT_SINGLE_OWNER
robertphillips2d70dcb2015-10-06 07:38:23 -0700176 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700177 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400178 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawText", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -0700179
Robert Phillips72152832017-01-25 17:31:35 -0500180 GrAtlasTextContext* atlasTextContext = this->drawingManager()->getAtlasTextContext();
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500181 atlasTextContext->drawText(fContext, this, clip, skPaint, viewMatrix, fSurfaceProps, text,
182 byteLength, x, y, clipBounds);
robertphillips2334fb62015-06-17 05:43:33 -0700183}
robertphillipscaef3452015-11-11 13:18:11 -0800184
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500185void GrRenderTargetContext::drawPosText(const GrClip& clip, const SkPaint& paint,
186 const SkMatrix& viewMatrix, const char text[],
187 size_t byteLength, const SkScalar pos[],
Brian Salomon82f44312017-01-11 13:42:54 -0500188 int scalarsPerPosition, const SkPoint& offset,
189 const SkIRect& clipBounds) {
joshualitt1de610a2016-01-06 08:26:09 -0800190 ASSERT_SINGLE_OWNER
robertphillips2d70dcb2015-10-06 07:38:23 -0700191 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700192 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400193 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawPosText", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -0700194
Robert Phillips72152832017-01-25 17:31:35 -0500195 GrAtlasTextContext* atlasTextContext = this->drawingManager()->getAtlasTextContext();
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500196 atlasTextContext->drawPosText(fContext, this, clip, paint, viewMatrix, fSurfaceProps, text,
197 byteLength, pos, scalarsPerPosition, offset, clipBounds);
robertphillips2334fb62015-06-17 05:43:33 -0700198}
robertphillipscaef3452015-11-11 13:18:11 -0800199
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500200void GrRenderTargetContext::drawTextBlob(const GrClip& clip, const SkPaint& paint,
Brian Osman11052242016-10-27 14:47:55 -0400201 const SkMatrix& viewMatrix, const SkTextBlob* blob,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500202 SkScalar x, SkScalar y, SkDrawFilter* filter,
203 const SkIRect& clipBounds) {
joshualitt1de610a2016-01-06 08:26:09 -0800204 ASSERT_SINGLE_OWNER
robertphillips2d70dcb2015-10-06 07:38:23 -0700205 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700206 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400207 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTextBlob", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -0700208
Robert Phillips72152832017-01-25 17:31:35 -0500209 GrAtlasTextContext* atlasTextContext = this->drawingManager()->getAtlasTextContext();
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500210 atlasTextContext->drawTextBlob(fContext, this, clip, paint, viewMatrix, fSurfaceProps, blob, x,
211 y, filter, clipBounds);
robertphillipsea461502015-05-26 11:38:03 -0700212}
213
Brian Osman11052242016-10-27 14:47:55 -0400214void GrRenderTargetContext::discard() {
joshualitt1de610a2016-01-06 08:26:09 -0800215 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700216 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700217 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400218 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "discard", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700219
Robert Phillips72152832017-01-25 17:31:35 -0500220 AutoCheckFlush acf(this->drawingManager());
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400221
Robert Phillipse4407212017-04-13 10:13:16 -0400222 // Currently this just inserts a discard op. However, once in MDB this can remove all the
223 // previously recorded ops and change the load op to discard.
224 if (this->caps()->discardRenderTargetSupport()) {
Robert Phillips955235f2017-05-18 12:17:35 -0400225 std::unique_ptr<GrOp> op(GrDiscardOp::Make(fRenderTargetProxy.get()));
Robert Phillipse4407212017-04-13 10:13:16 -0400226 if (!op) {
227 return;
228 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400229 this->getRTOpList()->addOp(std::move(op), *this->caps());
Robert Phillipse4407212017-04-13 10:13:16 -0400230 }
robertphillipsea461502015-05-26 11:38:03 -0700231}
232
Brian Osman11052242016-10-27 14:47:55 -0400233void GrRenderTargetContext::clear(const SkIRect* rect,
234 const GrColor color,
235 bool canIgnoreRect) {
joshualitt1de610a2016-01-06 08:26:09 -0800236 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700237 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700238 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400239 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "clear", fContext);
robertphillipsea461502015-05-26 11:38:03 -0700240
Robert Phillips72152832017-01-25 17:31:35 -0500241 AutoCheckFlush acf(this->drawingManager());
csmartdalton29df7602016-08-31 11:55:52 -0700242 this->internalClear(rect ? GrFixedClip(*rect) : GrFixedClip::Disabled(), color, canIgnoreRect);
243}
robertphillips9199a9f2016-07-13 07:48:43 -0700244
Robert Phillips784b7bf2016-12-09 13:35:02 -0500245void GrRenderTargetContextPriv::absClear(const SkIRect* clearRect, const GrColor color) {
246 ASSERT_SINGLE_OWNER_PRIV
247 RETURN_IF_ABANDONED_PRIV
248 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400249 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "absClear",
250 fRenderTargetContext->fContext);
Robert Phillips784b7bf2016-12-09 13:35:02 -0500251
Robert Phillips72152832017-01-25 17:31:35 -0500252 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500253
Brian Salomonbb5711a2017-05-17 13:49:59 -0400254 SkIRect rtRect = SkIRect::MakeWH(fRenderTargetContext->fRenderTargetProxy->worstCaseWidth(),
255 fRenderTargetContext->fRenderTargetProxy->worstCaseHeight());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500256
257 if (clearRect) {
258 if (clearRect->contains(rtRect)) {
259 clearRect = nullptr; // full screen
260 } else {
261 if (!rtRect.intersect(*clearRect)) {
262 return;
263 }
264 }
265 }
266
267 // TODO: in a post-MDB world this should be handled at the OpList level.
268 // An op-list that is initially cleared and has no other ops should receive an
269 // extra draw.
270 if (fRenderTargetContext->fContext->caps()->useDrawInsteadOfClear()) {
271 // This works around a driver bug with clear by drawing a rect instead.
272 // The driver will ignore a clear if it is the only thing rendered to a
273 // target before the target is read.
274 GrPaint paint;
275 paint.setColor4f(GrColor4f::FromGrColor(color));
Brian Salomona1633922017-01-09 11:46:10 -0500276 paint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
Robert Phillips784b7bf2016-12-09 13:35:02 -0500277
278 // We don't call drawRect() here to avoid the cropping to the, possibly smaller,
279 // RenderTargetProxy bounds
Brian Salomonbaaf4392017-06-15 09:59:23 -0400280 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeNonAAFill(
281 std::move(paint), SkMatrix::I(), SkRect::Make(rtRect), GrAAType::kNone);
282 fRenderTargetContext->addDrawOp(GrNoClip(), std::move(op));
Robert Phillips784b7bf2016-12-09 13:35:02 -0500283 } else {
Robert Phillips784b7bf2016-12-09 13:35:02 -0500284 // This path doesn't handle coalescing of full screen clears b.c. it
285 // has to clear the entire render target - not just the content area.
286 // It could be done but will take more finagling.
Robert Phillips5efd5ea2017-05-30 13:47:32 -0400287 std::unique_ptr<GrOp> op(GrClearOp::Make(rtRect, color, !clearRect));
Brian Salomonfc527d22016-12-14 21:07:01 -0500288 if (!op) {
Robert Phillips784b7bf2016-12-09 13:35:02 -0500289 return;
290 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400291 fRenderTargetContext->getRTOpList()->addOp(std::move(op), *fRenderTargetContext->caps());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500292 }
293}
294
Brian Osman11052242016-10-27 14:47:55 -0400295void GrRenderTargetContextPriv::clear(const GrFixedClip& clip,
296 const GrColor color,
297 bool canIgnoreClip) {
csmartdalton29df7602016-08-31 11:55:52 -0700298 ASSERT_SINGLE_OWNER_PRIV
299 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400300 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400301 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "clear",
302 fRenderTargetContext->fContext);
csmartdalton29df7602016-08-31 11:55:52 -0700303
Robert Phillips72152832017-01-25 17:31:35 -0500304 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Brian Osman11052242016-10-27 14:47:55 -0400305 fRenderTargetContext->internalClear(clip, color, canIgnoreClip);
csmartdalton29df7602016-08-31 11:55:52 -0700306}
307
Brian Osman11052242016-10-27 14:47:55 -0400308void GrRenderTargetContext::internalClear(const GrFixedClip& clip,
309 const GrColor color,
310 bool canIgnoreClip) {
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700311 bool isFull = false;
312 if (!clip.hasWindowRectangles()) {
313 isFull = !clip.scissorEnabled() ||
314 (canIgnoreClip && fContext->caps()->fullClearIsFree()) ||
315 clip.scissorRect().contains(SkIRect::MakeWH(this->width(), this->height()));
316 }
robertphillips9199a9f2016-07-13 07:48:43 -0700317
318 if (fContext->caps()->useDrawInsteadOfClear()) {
319 // This works around a driver bug with clear by drawing a rect instead.
320 // The driver will ignore a clear if it is the only thing rendered to a
321 // target before the target is read.
Robert Phillips784b7bf2016-12-09 13:35:02 -0500322 SkIRect clearRect = SkIRect::MakeWH(this->width(), this->height());
csmartdalton29df7602016-08-31 11:55:52 -0700323 if (isFull) {
robertphillips9199a9f2016-07-13 07:48:43 -0700324 this->discard();
Robert Phillips93f16332016-11-23 19:37:13 -0500325 } else if (!clearRect.intersect(clip.scissorRect())) {
csmartdalton29df7602016-08-31 11:55:52 -0700326 return;
robertphillips9199a9f2016-07-13 07:48:43 -0700327 }
328
329 GrPaint paint;
330 paint.setColor4f(GrColor4f::FromGrColor(color));
Brian Salomona1633922017-01-09 11:46:10 -0500331 paint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
robertphillips9199a9f2016-07-13 07:48:43 -0700332
Brian Salomon82f44312017-01-11 13:42:54 -0500333 this->drawRect(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), SkRect::Make(clearRect));
bsalomon9f129de2016-08-10 16:31:05 -0700334 } else if (isFull) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400335 this->getRTOpList()->fullClear(*this->caps(), color);
robertphillips9199a9f2016-07-13 07:48:43 -0700336 } else {
Robert Phillips5efd5ea2017-05-30 13:47:32 -0400337 std::unique_ptr<GrOp> op(GrClearOp::Make(clip, color, this->asSurfaceProxy()));
Brian Salomon42521e82016-12-07 16:44:58 -0500338 if (!op) {
csmartdalton29df7602016-08-31 11:55:52 -0700339 return;
340 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400341 this->getRTOpList()->addOp(std::move(op), *this->caps());
robertphillips9199a9f2016-07-13 07:48:43 -0700342 }
robertphillipsea461502015-05-26 11:38:03 -0700343}
344
Brian Osman11052242016-10-27 14:47:55 -0400345void GrRenderTargetContext::drawPaint(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500346 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400347 const SkMatrix& viewMatrix) {
joshualitt1de610a2016-01-06 08:26:09 -0800348 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700349 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700350 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400351 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawPaint", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700352
robertphillipsea461502015-05-26 11:38:03 -0700353 // set rect to be big enough to fill the space, but not super-huge, so we
354 // don't overflow fixed-point implementations
robertphillips13a7eee2016-08-31 15:06:24 -0700355
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400356 SkRect r = fRenderTargetProxy->getBoundsRect();
robertphillipsea461502015-05-26 11:38:03 -0700357
bsalomoncb31e512016-08-26 10:48:19 -0700358 SkRRect rrect;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500359 GrAA aa;
bsalomoncb31e512016-08-26 10:48:19 -0700360 // Check if we can replace a clipRRect()/drawPaint() with a drawRRect(). We only do the
361 // transformation for non-rect rrects. Rects caused a performance regression on an Android
362 // test that needs investigation. We also skip cases where there are fragment processors
363 // because they may depend on having correct local coords and this path draws in device space
364 // without a local matrix.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500365 if (!paint.numTotalFragmentProcessors() && clip.isRRect(r, &rrect, &aa) && !rrect.isRect()) {
Brian Salomon82f44312017-01-11 13:42:54 -0500366 this->drawRRect(GrNoClip(), std::move(paint), aa, SkMatrix::I(), rrect,
367 GrStyle::SimpleFill());
bsalomoncb31e512016-08-26 10:48:19 -0700368 return;
369 }
370
robertphillipsea461502015-05-26 11:38:03 -0700371
372 bool isPerspective = viewMatrix.hasPerspective();
373
374 // We attempt to map r by the inverse matrix and draw that. mapRect will
375 // map the four corners and bound them with a new rect. This will not
376 // produce a correct result for some perspective matrices.
377 if (!isPerspective) {
reeda39667c2016-08-22 06:39:49 -0700378 if (!SkMatrixPriv::InverseMapRect(viewMatrix, &r, r)) {
robertphillipsea461502015-05-26 11:38:03 -0700379 SkDebugf("Could not invert matrix\n");
380 return;
381 }
Brian Salomon82f44312017-01-11 13:42:54 -0500382 this->drawRect(clip, std::move(paint), GrAA::kNo, viewMatrix, r);
robertphillipsea461502015-05-26 11:38:03 -0700383 } else {
384 SkMatrix localMatrix;
385 if (!viewMatrix.invert(&localMatrix)) {
386 SkDebugf("Could not invert matrix\n");
387 return;
388 }
389
Robert Phillips72152832017-01-25 17:31:35 -0500390 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700391
Brian Salomonbaaf4392017-06-15 09:59:23 -0400392 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeNonAAFillWithLocalMatrix(
393 std::move(paint), SkMatrix::I(), localMatrix, r, GrAAType::kNone);
394 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -0700395 }
396}
397
robertphillipsea461502015-05-26 11:38:03 -0700398static inline bool rect_contains_inclusive(const SkRect& rect, const SkPoint& point) {
399 return point.fX >= rect.fLeft && point.fX <= rect.fRight &&
400 point.fY >= rect.fTop && point.fY <= rect.fBottom;
401}
402
csmartdalton97f6cd52016-07-13 13:37:08 -0700403// Attempts to crop a rect and optional local rect to the clip boundaries.
404// Returns false if the draw can be skipped entirely.
robertphillips13a7eee2016-08-31 15:06:24 -0700405static bool crop_filled_rect(int width, int height, const GrClip& clip,
csmartdalton97f6cd52016-07-13 13:37:08 -0700406 const SkMatrix& viewMatrix, SkRect* rect,
407 SkRect* localRect = nullptr) {
408 if (!viewMatrix.rectStaysRect()) {
409 return true;
410 }
411
csmartdalton97f6cd52016-07-13 13:37:08 -0700412 SkIRect clipDevBounds;
413 SkRect clipBounds;
csmartdalton97f6cd52016-07-13 13:37:08 -0700414
robertphillips13a7eee2016-08-31 15:06:24 -0700415 clip.getConservativeBounds(width, height, &clipDevBounds);
reeda39667c2016-08-22 06:39:49 -0700416 if (!SkMatrixPriv::InverseMapRect(viewMatrix, &clipBounds, SkRect::Make(clipDevBounds))) {
417 return false;
418 }
csmartdalton97f6cd52016-07-13 13:37:08 -0700419
420 if (localRect) {
421 if (!rect->intersects(clipBounds)) {
422 return false;
423 }
424 const SkScalar dx = localRect->width() / rect->width();
425 const SkScalar dy = localRect->height() / rect->height();
426 if (clipBounds.fLeft > rect->fLeft) {
427 localRect->fLeft += (clipBounds.fLeft - rect->fLeft) * dx;
428 rect->fLeft = clipBounds.fLeft;
429 }
430 if (clipBounds.fTop > rect->fTop) {
431 localRect->fTop += (clipBounds.fTop - rect->fTop) * dy;
432 rect->fTop = clipBounds.fTop;
433 }
434 if (clipBounds.fRight < rect->fRight) {
435 localRect->fRight -= (rect->fRight - clipBounds.fRight) * dx;
436 rect->fRight = clipBounds.fRight;
437 }
438 if (clipBounds.fBottom < rect->fBottom) {
439 localRect->fBottom -= (rect->fBottom - clipBounds.fBottom) * dy;
440 rect->fBottom = clipBounds.fBottom;
441 }
442 return true;
443 }
444
445 return rect->intersect(clipBounds);
446}
447
Brian Osman11052242016-10-27 14:47:55 -0400448bool GrRenderTargetContext::drawFilledRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500449 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500450 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400451 const SkMatrix& viewMatrix,
452 const SkRect& rect,
453 const GrUserStencilSettings* ss) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700454 SkRect croppedRect = rect;
Robert Phillips784b7bf2016-12-09 13:35:02 -0500455 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix, &croppedRect)) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700456 return true;
457 }
robertphillips44302392016-07-08 14:43:03 -0700458
Brian Salomon54d212e2017-03-21 14:22:38 -0400459 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport() &&
460 (!ss || ss->isDisabled(false))) {
Robert Phillipse3302df2017-04-24 07:31:02 -0400461 gr_instanced::OpAllocator* oa = this->drawingManager()->instancingAllocator();
462 std::unique_ptr<GrDrawOp> op = oa->recordRect(croppedRect, viewMatrix, std::move(paint),
463 aa, fInstancedPipelineInfo);
Brian Salomon42521e82016-12-07 16:44:58 -0500464 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -0400465 this->addDrawOp(clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700466 return true;
csmartdaltona7f29642016-07-07 08:49:11 -0700467 }
468 }
Brian Salomon7c8460e2017-05-12 11:36:10 -0400469 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400470 std::unique_ptr<GrDrawOp> op;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500471 if (GrAAType::kCoverage == aaType) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400472 op = GrRectOpFactory::MakeAAFill(std::move(paint), viewMatrix, croppedRect, ss);
robertphillips391395d2016-03-02 09:26:36 -0800473 } else {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400474 op = GrRectOpFactory::MakeNonAAFill(std::move(paint), viewMatrix, croppedRect, aaType, ss);
robertphillips391395d2016-03-02 09:26:36 -0800475 }
Brian Salomonbaaf4392017-06-15 09:59:23 -0400476 if (!op) {
477 return false;
478 }
479 this->addDrawOp(clip, std::move(op));
480 return true;
robertphillips391395d2016-03-02 09:26:36 -0800481}
482
Brian Osman11052242016-10-27 14:47:55 -0400483void GrRenderTargetContext::drawRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500484 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500485 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400486 const SkMatrix& viewMatrix,
487 const SkRect& rect,
488 const GrStyle* style) {
bsalomon6663acf2016-05-10 09:14:17 -0700489 if (!style) {
490 style = &GrStyle::SimpleFill();
491 }
joshualitt1de610a2016-01-06 08:26:09 -0800492 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700493 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700494 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400495 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRect", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700496
bsalomon6663acf2016-05-10 09:14:17 -0700497 // Path effects should've been devolved to a path in SkGpuDevice
498 SkASSERT(!style->pathEffect());
robertphillipsea461502015-05-26 11:38:03 -0700499
Robert Phillips72152832017-01-25 17:31:35 -0500500 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700501
bsalomon6663acf2016-05-10 09:14:17 -0700502 const SkStrokeRec& stroke = style->strokeRec();
robertphillips3ab14ca2016-07-10 11:49:39 -0700503 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
Greg Daniela5cb7812017-06-16 09:45:32 -0400504
robertphillips3ab14ca2016-07-10 11:49:39 -0700505 if (!fContext->caps()->useDrawInsteadOfClear()) {
506 // Check if this is a full RT draw and can be replaced with a clear. We don't bother
507 // checking cases where the RT is fully inside a stroke.
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400508 SkRect rtRect = fRenderTargetProxy->getBoundsRect();
robertphillips3ab14ca2016-07-10 11:49:39 -0700509 // Does the clip contain the entire RT?
510 if (clip.quickContains(rtRect)) {
511 SkMatrix invM;
512 if (!viewMatrix.invert(&invM)) {
robertphillipsea461502015-05-26 11:38:03 -0700513 return;
514 }
robertphillips3ab14ca2016-07-10 11:49:39 -0700515 // Does the rect bound the RT?
516 SkPoint srcSpaceRTQuad[4];
517 invM.mapRectToQuad(srcSpaceRTQuad, rtRect);
518 if (rect_contains_inclusive(rect, srcSpaceRTQuad[0]) &&
519 rect_contains_inclusive(rect, srcSpaceRTQuad[1]) &&
520 rect_contains_inclusive(rect, srcSpaceRTQuad[2]) &&
521 rect_contains_inclusive(rect, srcSpaceRTQuad[3])) {
522 // Will it blend?
523 GrColor clearColor;
524 if (paint.isConstantBlendedColor(&clearColor)) {
robertphillips9199a9f2016-07-13 07:48:43 -0700525 this->clear(nullptr, clearColor, true);
robertphillips3ab14ca2016-07-10 11:49:39 -0700526 return;
527 }
528 }
robertphillipsea461502015-05-26 11:38:03 -0700529 }
530 }
robertphillips44302392016-07-08 14:43:03 -0700531
Brian Salomon82f44312017-01-11 13:42:54 -0500532 if (this->drawFilledRect(clip, std::move(paint), aa, viewMatrix, rect, nullptr)) {
robertphillips44302392016-07-08 14:43:03 -0700533 return;
534 }
bsalomona7d85ba2016-07-06 11:54:59 -0700535 } else if (stroke.getStyle() == SkStrokeRec::kStroke_Style ||
536 stroke.getStyle() == SkStrokeRec::kHairline_Style) {
537 if ((!rect.width() || !rect.height()) &&
538 SkStrokeRec::kHairline_Style != stroke.getStyle()) {
539 SkScalar r = stroke.getWidth() / 2;
540 // TODO: Move these stroke->fill fallbacks to GrShape?
541 switch (stroke.getJoin()) {
542 case SkPaint::kMiter_Join:
Brian Salomon82f44312017-01-11 13:42:54 -0500543 this->drawRect(
544 clip, std::move(paint), aa, viewMatrix,
545 {rect.fLeft - r, rect.fTop - r, rect.fRight + r, rect.fBottom + r},
546 &GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700547 return;
548 case SkPaint::kRound_Join:
549 // Raster draws nothing when both dimensions are empty.
550 if (rect.width() || rect.height()){
551 SkRRect rrect = SkRRect::MakeRectXY(rect.makeOutset(r, r), r, r);
Brian Salomon82f44312017-01-11 13:42:54 -0500552 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect,
553 GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700554 return;
555 }
556 case SkPaint::kBevel_Join:
557 if (!rect.width()) {
Brian Salomon82f44312017-01-11 13:42:54 -0500558 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700559 {rect.fLeft - r, rect.fTop, rect.fRight + r, rect.fBottom},
560 &GrStyle::SimpleFill());
561 } else {
Brian Salomon82f44312017-01-11 13:42:54 -0500562 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700563 {rect.fLeft, rect.fTop - r, rect.fRight, rect.fBottom + r},
564 &GrStyle::SimpleFill());
565 }
566 return;
567 }
568 }
robertphillips44302392016-07-08 14:43:03 -0700569
Brian Salomonbaaf4392017-06-15 09:59:23 -0400570 std::unique_ptr<GrDrawOp> op;
robertphillips44302392016-07-08 14:43:03 -0700571
Brian Salomon7c8460e2017-05-12 11:36:10 -0400572 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500573 if (GrAAType::kCoverage == aaType) {
cdaltonbb539482016-01-04 09:48:25 -0800574 // The stroke path needs the rect to remain axis aligned (no rotation or skew).
575 if (viewMatrix.rectStaysRect()) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400576 op = GrRectOpFactory::MakeAAStroke(std::move(paint), viewMatrix, rect, stroke);
cdaltonbb539482016-01-04 09:48:25 -0800577 }
robertphillipsea461502015-05-26 11:38:03 -0700578 } else {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400579 op = GrRectOpFactory::MakeNonAAStroke(std::move(paint), viewMatrix, rect, stroke,
580 aaType);
robertphillips391395d2016-03-02 09:26:36 -0800581 }
robertphillips4bc31812016-03-01 12:22:49 -0800582
Brian Salomon42521e82016-12-07 16:44:58 -0500583 if (op) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400584 this->addDrawOp(clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700585 return;
robertphillips4bc31812016-03-01 12:22:49 -0800586 }
robertphillips4bc31812016-03-01 12:22:49 -0800587 }
halcanary9d524f22016-03-29 09:03:52 -0700588
robertphillips4bc31812016-03-01 12:22:49 -0800589 SkPath path;
590 path.setIsVolatile(true);
591 path.addRect(rect);
Brian Salomon82f44312017-01-11 13:42:54 -0500592 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, *style);
robertphillipsea461502015-05-26 11:38:03 -0700593}
594
Robert Phillipsec2249f2016-11-09 08:54:35 -0500595int GrRenderTargetContextPriv::maxWindowRectangles() const {
596 return fRenderTargetContext->fRenderTargetProxy->maxWindowRectangles(
597 *fRenderTargetContext->fContext->caps());
598}
599
Brian Osman11052242016-10-27 14:47:55 -0400600void GrRenderTargetContextPriv::clearStencilClip(const GrFixedClip& clip, bool insideStencilMask) {
robertphillips976f5f02016-06-03 10:59:20 -0700601 ASSERT_SINGLE_OWNER_PRIV
602 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400603 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400604 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "clearStencilClip",
605 fRenderTargetContext->fContext);
robertphillips976f5f02016-06-03 10:59:20 -0700606
Robert Phillips72152832017-01-25 17:31:35 -0500607 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400608
Robert Phillips2f4ddf62017-06-01 08:48:19 -0400609 std::unique_ptr<GrOp> op(GrClearStencilClipOp::Make(
610 clip, insideStencilMask,
611 fRenderTargetContext->fRenderTargetProxy.get()));
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400612 if (!op) {
Robert Phillipse60ad622016-11-17 10:22:48 -0500613 return;
614 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400615 fRenderTargetContext->getRTOpList()->addOp(std::move(op), *fRenderTargetContext->caps());
robertphillips976f5f02016-06-03 10:59:20 -0700616}
617
Brian Osman11052242016-10-27 14:47:55 -0400618void GrRenderTargetContextPriv::stencilPath(const GrClip& clip,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500619 GrAAType aaType,
Brian Osman11052242016-10-27 14:47:55 -0400620 const SkMatrix& viewMatrix,
621 const GrPath* path) {
Brian Salomon467921e2017-03-06 16:17:12 -0500622 ASSERT_SINGLE_OWNER_PRIV
623 RETURN_IF_ABANDONED_PRIV
624 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400625 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "stencilPath",
626 fRenderTargetContext->fContext);
Brian Salomon467921e2017-03-06 16:17:12 -0500627
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500628 SkASSERT(aaType != GrAAType::kCoverage);
Brian Salomon467921e2017-03-06 16:17:12 -0500629
630 bool useHWAA = GrAATypeIsHW(aaType);
631 // TODO: extract portions of checkDraw that are relevant to path stenciling.
632 SkASSERT(path);
633 SkASSERT(fRenderTargetContext->caps()->shaderCaps()->pathRenderingSupport());
634
635 // FIXME: Use path bounds instead of this WAR once
636 // https://bugs.chromium.org/p/skia/issues/detail?id=5640 is resolved.
637 SkRect bounds = SkRect::MakeIWH(fRenderTargetContext->width(), fRenderTargetContext->height());
638
639 // Setup clip
Brian Salomon97180af2017-03-14 13:42:58 -0400640 GrAppliedClip appliedClip;
Brian Salomon467921e2017-03-06 16:17:12 -0500641 if (!clip.apply(fRenderTargetContext->fContext, fRenderTargetContext, useHWAA, true,
Brian Salomon97180af2017-03-14 13:42:58 -0400642 &appliedClip, &bounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -0500643 return;
644 }
645
646 // Coverage AA does not make sense when rendering to the stencil buffer. The caller should never
647 // attempt this in a situation that would require coverage AA.
648 SkASSERT(!appliedClip.clipCoverageFragmentProcessor());
649
650 GrRenderTarget* rt = fRenderTargetContext->accessRenderTarget();
651 if (!rt) {
652 return;
653 }
654 GrStencilAttachment* stencilAttachment =
655 fRenderTargetContext->fContext->resourceProvider()->attachStencilAttachment(rt);
656 if (!stencilAttachment) {
657 SkDebugf("ERROR creating stencil attachment. Draw skipped.\n");
658 return;
659 }
660
661 std::unique_ptr<GrOp> op = GrStencilPathOp::Make(viewMatrix,
662 useHWAA,
663 path->getFillType(),
664 appliedClip.hasStencilClip(),
665 stencilAttachment->bits(),
666 appliedClip.scissorState(),
Brian Salomon467921e2017-03-06 16:17:12 -0500667 path);
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400668 if (!op) {
669 return;
670 }
Brian Salomon97180af2017-03-14 13:42:58 -0400671 op->setClippedBounds(bounds);
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400672 fRenderTargetContext->getRTOpList()->addOp(std::move(op), *fRenderTargetContext->caps());
robertphillips976f5f02016-06-03 10:59:20 -0700673}
674
Brian Osman11052242016-10-27 14:47:55 -0400675void GrRenderTargetContextPriv::stencilRect(const GrClip& clip,
676 const GrUserStencilSettings* ss,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500677 GrAAType aaType,
Brian Osman11052242016-10-27 14:47:55 -0400678 const SkMatrix& viewMatrix,
679 const SkRect& rect) {
robertphillips976f5f02016-06-03 10:59:20 -0700680 ASSERT_SINGLE_OWNER_PRIV
681 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400682 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400683 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "stencilRect",
684 fRenderTargetContext->fContext);
685
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500686 SkASSERT(GrAAType::kCoverage != aaType);
Robert Phillips72152832017-01-25 17:31:35 -0500687 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips976f5f02016-06-03 10:59:20 -0700688
689 GrPaint paint;
Brian Salomona1633922017-01-09 11:46:10 -0500690 paint.setXPFactory(GrDisableColorXPFactory::Get());
Brian Salomonbaaf4392017-06-15 09:59:23 -0400691 std::unique_ptr<GrDrawOp> op =
692 GrRectOpFactory::MakeNonAAFill(std::move(paint), viewMatrix, rect, aaType, ss);
693 fRenderTargetContext->addDrawOp(clip, std::move(op));
robertphillips976f5f02016-06-03 10:59:20 -0700694}
695
Brian Osman11052242016-10-27 14:47:55 -0400696bool GrRenderTargetContextPriv::drawAndStencilRect(const GrClip& clip,
697 const GrUserStencilSettings* ss,
698 SkRegion::Op op,
699 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500700 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400701 const SkMatrix& viewMatrix,
702 const SkRect& rect) {
robertphillips391395d2016-03-02 09:26:36 -0800703 ASSERT_SINGLE_OWNER_PRIV
704 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400705 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400706 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawAndStencilRect",
707 fRenderTargetContext->fContext);
robertphillips391395d2016-03-02 09:26:36 -0800708
Robert Phillips72152832017-01-25 17:31:35 -0500709 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -0800710
711 GrPaint paint;
robertphillips391395d2016-03-02 09:26:36 -0800712 paint.setCoverageSetOpXPFactory(op, invert);
713
Brian Salomon82f44312017-01-11 13:42:54 -0500714 if (fRenderTargetContext->drawFilledRect(clip, std::move(paint), aa, viewMatrix, rect, ss)) {
robertphillips391395d2016-03-02 09:26:36 -0800715 return true;
716 }
robertphillips391395d2016-03-02 09:26:36 -0800717 SkPath path;
718 path.setIsVolatile(true);
719 path.addRect(rect);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500720 return this->drawAndStencilPath(clip, ss, op, invert, aa, viewMatrix, path);
robertphillips391395d2016-03-02 09:26:36 -0800721}
722
Brian Osman11052242016-10-27 14:47:55 -0400723void GrRenderTargetContext::fillRectToRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500724 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500725 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400726 const SkMatrix& viewMatrix,
727 const SkRect& rectToDraw,
728 const SkRect& localRect) {
joshualitt1de610a2016-01-06 08:26:09 -0800729 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700730 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700731 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400732 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "fillRectToRect", fContext);
robertphillipsea461502015-05-26 11:38:03 -0700733
csmartdalton97f6cd52016-07-13 13:37:08 -0700734 SkRect croppedRect = rectToDraw;
735 SkRect croppedLocalRect = localRect;
robertphillips13a7eee2016-08-31 15:06:24 -0700736 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix,
737 &croppedRect, &croppedLocalRect)) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700738 return;
739 }
740
Robert Phillips72152832017-01-25 17:31:35 -0500741 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -0700742
csmartdaltone0d36292016-07-29 08:14:20 -0700743 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport()) {
Robert Phillipse3302df2017-04-24 07:31:02 -0400744 gr_instanced::OpAllocator* oa = this->drawingManager()->instancingAllocator();
745 std::unique_ptr<GrDrawOp> op(oa->recordRect(croppedRect, viewMatrix, std::move(paint),
Brian Salomon54d212e2017-03-21 14:22:38 -0400746 croppedLocalRect, aa, fInstancedPipelineInfo));
Brian Salomon42521e82016-12-07 16:44:58 -0500747 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -0400748 this->addDrawOp(clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -0700749 return;
750 }
751 }
752
Brian Salomon7c8460e2017-05-12 11:36:10 -0400753 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500754 if (GrAAType::kCoverage != aaType) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400755 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeNonAAFillWithLocalRect(
756 std::move(paint), viewMatrix, croppedRect, croppedLocalRect, aaType);
757 this->addDrawOp(clip, std::move(op));
csmartdaltonfc49d562016-07-26 17:05:47 -0700758 return;
joshualitt04194f32016-01-13 10:08:27 -0800759 }
bsalomonbb243832016-07-22 07:10:19 -0700760
Brian Salomonbaaf4392017-06-15 09:59:23 -0400761 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeAAFillWithLocalRect(
762 std::move(paint), viewMatrix, croppedRect, croppedLocalRect);
763 if (op) {
764 this->addDrawOp(clip, std::move(op));
csmartdaltonfc49d562016-07-26 17:05:47 -0700765 return;
766 }
767
768 SkMatrix viewAndUnLocalMatrix;
769 if (!viewAndUnLocalMatrix.setRectToRect(localRect, rectToDraw, SkMatrix::kFill_ScaleToFit)) {
770 SkDebugf("fillRectToRect called with empty local matrix.\n");
771 return;
772 }
773 viewAndUnLocalMatrix.postConcat(viewMatrix);
774
775 SkPath path;
776 path.setIsVolatile(true);
777 path.addRect(localRect);
Brian Salomon82f44312017-01-11 13:42:54 -0500778 this->internalDrawPath(clip, std::move(paint), aa, viewAndUnLocalMatrix, path, GrStyle());
joshualittb6b513b2015-08-21 10:25:18 -0700779}
780
Brian Osman11052242016-10-27 14:47:55 -0400781void GrRenderTargetContext::fillRectWithLocalMatrix(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500782 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500783 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400784 const SkMatrix& viewMatrix,
785 const SkRect& rectToDraw,
786 const SkMatrix& localMatrix) {
joshualitt1de610a2016-01-06 08:26:09 -0800787 ASSERT_SINGLE_OWNER
joshualittb6b513b2015-08-21 10:25:18 -0700788 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700789 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400790 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "fillRectWithLocalMatrix", fContext);
joshualittb6b513b2015-08-21 10:25:18 -0700791
csmartdalton97f6cd52016-07-13 13:37:08 -0700792 SkRect croppedRect = rectToDraw;
robertphillips13a7eee2016-08-31 15:06:24 -0700793 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix, &croppedRect)) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700794 return;
795 }
796
Robert Phillips72152832017-01-25 17:31:35 -0500797 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -0700798
csmartdaltone0d36292016-07-29 08:14:20 -0700799 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport()) {
Robert Phillipse3302df2017-04-24 07:31:02 -0400800 gr_instanced::OpAllocator* oa = this->drawingManager()->instancingAllocator();
801 std::unique_ptr<GrDrawOp> op(oa->recordRect(croppedRect, viewMatrix, std::move(paint),
Brian Salomon54d212e2017-03-21 14:22:38 -0400802 localMatrix, aa, fInstancedPipelineInfo));
Brian Salomon42521e82016-12-07 16:44:58 -0500803 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -0400804 this->addDrawOp(clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -0700805 return;
806 }
807 }
808
Brian Salomon7c8460e2017-05-12 11:36:10 -0400809 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500810 if (GrAAType::kCoverage != aaType) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400811 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeNonAAFillWithLocalMatrix(
812 std::move(paint), viewMatrix, localMatrix, croppedRect, aaType);
813 this->addDrawOp(clip, std::move(op));
csmartdaltonfc49d562016-07-26 17:05:47 -0700814 return;
bsalomonc55271f2015-11-09 11:55:57 -0800815 }
robertphillips4bc31812016-03-01 12:22:49 -0800816
Brian Salomonbaaf4392017-06-15 09:59:23 -0400817 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeAAFillWithLocalMatrix(
818 std::move(paint), viewMatrix, localMatrix, croppedRect);
819 if (op) {
820 this->addDrawOp(clip, std::move(op));
csmartdaltonfc49d562016-07-26 17:05:47 -0700821 return;
822 }
823
824 SkMatrix viewAndUnLocalMatrix;
825 if (!localMatrix.invert(&viewAndUnLocalMatrix)) {
826 SkDebugf("fillRectWithLocalMatrix called with degenerate local matrix.\n");
827 return;
828 }
829 viewAndUnLocalMatrix.postConcat(viewMatrix);
830
831 SkPath path;
832 path.setIsVolatile(true);
833 path.addRect(rectToDraw);
834 path.transform(localMatrix);
Brian Salomon82f44312017-01-11 13:42:54 -0500835 this->internalDrawPath(clip, std::move(paint), aa, viewAndUnLocalMatrix, path, GrStyle());
robertphillipsea461502015-05-26 11:38:03 -0700836}
837
Brian Osman11052242016-10-27 14:47:55 -0400838void GrRenderTargetContext::drawVertices(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500839 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400840 const SkMatrix& viewMatrix,
Brian Osmanae0c50c2017-05-25 16:56:34 -0400841 sk_sp<SkVertices> vertices,
842 GrPrimitiveType* overridePrimType) {
Brian Salomon199fb872017-02-06 09:41:10 -0500843 ASSERT_SINGLE_OWNER
844 RETURN_IF_ABANDONED
845 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400846 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawVertices", fContext);
Brian Salomon199fb872017-02-06 09:41:10 -0500847
848 AutoCheckFlush acf(this->drawingManager());
849
850 SkASSERT(vertices);
Brian Salomonc2f42542017-07-12 14:11:22 -0400851 GrAAType aaType = this->chooseAAType(GrAA::kNo, GrAllowMixedSamples::kNo);
852 std::unique_ptr<GrDrawOp> op =
853 GrDrawVerticesOp::Make(std::move(paint), std::move(vertices), viewMatrix, aaType,
854 this->isGammaCorrect(), fColorXformFromSRGB, overridePrimType);
855 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -0700856}
857
858///////////////////////////////////////////////////////////////////////////////
859
Brian Osman11052242016-10-27 14:47:55 -0400860void GrRenderTargetContext::drawAtlas(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500861 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400862 const SkMatrix& viewMatrix,
863 int spriteCount,
864 const SkRSXform xform[],
865 const SkRect texRect[],
866 const SkColor colors[]) {
joshualitt1de610a2016-01-06 08:26:09 -0800867 ASSERT_SINGLE_OWNER
jvanverth31ff7622015-08-07 10:09:28 -0700868 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700869 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400870 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawAtlas", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700871
Robert Phillips72152832017-01-25 17:31:35 -0500872 AutoCheckFlush acf(this->drawingManager());
halcanary9d524f22016-03-29 09:03:52 -0700873
Brian Salomon0088f942017-07-12 11:51:27 -0400874 GrAAType aaType = this->chooseAAType(GrAA::kNo, GrAllowMixedSamples::kNo);
875 std::unique_ptr<GrDrawOp> op = GrDrawAtlasOp::Make(std::move(paint), viewMatrix, aaType,
876 spriteCount, xform, texRect, colors);
877 this->addDrawOp(clip, std::move(op));
jvanverth31ff7622015-08-07 10:09:28 -0700878}
879
880///////////////////////////////////////////////////////////////////////////////
881
Brian Osman11052242016-10-27 14:47:55 -0400882void GrRenderTargetContext::drawRRect(const GrClip& origClip,
Brian Salomon82f44312017-01-11 13:42:54 -0500883 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500884 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400885 const SkMatrix& viewMatrix,
886 const SkRRect& rrect,
887 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -0800888 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700889 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700890 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400891 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRRect", fContext);
robertphillipsea461502015-05-26 11:38:03 -0700892 if (rrect.isEmpty()) {
893 return;
894 }
895
bsalomon7f0d9f32016-08-15 14:49:10 -0700896 GrNoClip noclip;
897 const GrClip* clip = &origClip;
898#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
899 // The Android framework frequently clips rrects to themselves where the clip is non-aa and the
Brian Salomon42521e82016-12-07 16:44:58 -0500900 // draw is aa. Since our lower level clip code works from op bounds, which are SkRects, it
bsalomon7f0d9f32016-08-15 14:49:10 -0700901 // doesn't detect that the clip can be ignored (modulo antialiasing). The following test
902 // attempts to mitigate the stencil clip cost but will only help when the entire clip stack
903 // can be ignored. We'd prefer to fix this in the framework by removing the clips calls.
904 SkRRect devRRect;
905 if (rrect.transform(viewMatrix, &devRRect) && clip->quickContains(devRRect)) {
906 clip = &noclip;
907 }
908#endif
bsalomon6663acf2016-05-10 09:14:17 -0700909 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
ksakamotoec7f2ac2016-07-05 03:54:53 -0700910
Robert Phillips72152832017-01-25 17:31:35 -0500911 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -0700912 const SkStrokeRec stroke = style.strokeRec();
csmartdaltona7f29642016-07-07 08:49:11 -0700913
csmartdaltone0d36292016-07-29 08:14:20 -0700914 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport() &&
915 stroke.isFillStyle()) {
Robert Phillipse3302df2017-04-24 07:31:02 -0400916 gr_instanced::OpAllocator* oa = this->drawingManager()->instancingAllocator();
Brian Salomon54d212e2017-03-21 14:22:38 -0400917 std::unique_ptr<GrDrawOp> op(
Robert Phillipse3302df2017-04-24 07:31:02 -0400918 oa->recordRRect(rrect, viewMatrix, std::move(paint), aa, fInstancedPipelineInfo));
Brian Salomon42521e82016-12-07 16:44:58 -0500919 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -0400920 this->addDrawOp(*clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -0700921 return;
922 }
923 }
924
Brian Salomon7c8460e2017-05-12 11:36:10 -0400925 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500926 if (GrAAType::kCoverage == aaType) {
Brian Salomon94efbf52016-11-29 13:43:05 -0500927 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomon05441c42017-05-15 16:45:49 -0400928 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeRRectOp(std::move(paint),
Brian Salomon05441c42017-05-15 16:45:49 -0400929 viewMatrix,
930 rrect,
931 stroke,
932 shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -0500933 if (op) {
Brian Salomon05441c42017-05-15 16:45:49 -0400934 this->addDrawOp(*clip, std::move(op));
robertphillipsb56f9272016-02-25 11:03:52 -0800935 return;
936 }
robertphillipsea461502015-05-26 11:38:03 -0700937 }
robertphillipsb56f9272016-02-25 11:03:52 -0800938
939 SkPath path;
940 path.setIsVolatile(true);
941 path.addRRect(rrect);
Brian Salomon82f44312017-01-11 13:42:54 -0500942 this->internalDrawPath(*clip, std::move(paint), aa, viewMatrix, path, style);
robertphillipsea461502015-05-26 11:38:03 -0700943}
944
Jim Van Verthc5903412016-11-17 15:27:09 -0500945///////////////////////////////////////////////////////////////////////////////
946
Jim Van Verth3af1af92017-05-18 15:06:54 -0400947static SkPoint3 map(const SkMatrix& m, const SkPoint3& pt) {
948 SkPoint3 result;
949 m.mapXY(pt.fX, pt.fY, (SkPoint*)&result.fX);
950 result.fZ = pt.fZ;
951 return result;
952}
953
954bool GrRenderTargetContext::drawFastShadow(const GrClip& clip,
Brian Salomon05969092017-07-13 11:20:51 -0400955 GrColor color4ub,
Jim Van Verth3af1af92017-05-18 15:06:54 -0400956 const SkMatrix& viewMatrix,
957 const SkPath& path,
958 const SkDrawShadowRec& rec) {
Jim Van Verthc5903412016-11-17 15:27:09 -0500959 ASSERT_SINGLE_OWNER
Jim Van Verth3af1af92017-05-18 15:06:54 -0400960 if (this->drawingManager()->wasAbandoned()) {
961 return true;
962 }
Jim Van Verthc5903412016-11-17 15:27:09 -0500963 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400964 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFastShadow", fContext);
Jim Van Verth3af1af92017-05-18 15:06:54 -0400965
966 // check z plane
967 bool tiltZPlane = SkToBool(!SkScalarNearlyZero(rec.fZPlaneParams.fX) ||
968 !SkScalarNearlyZero(rec.fZPlaneParams.fY));
969 bool skipAnalytic = SkToBool(rec.fFlags & SkShadowFlags::kGeometricOnly_ShadowFlag);
970 if (tiltZPlane || skipAnalytic || !viewMatrix.rectStaysRect() || !viewMatrix.isSimilarity()) {
971 return false;
972 }
973
974 SkRRect rrect;
975 SkRect rect;
976 // we can only handle rects, circles, and rrects with circular corners
977 bool isRRect = path.isRRect(&rrect) && rrect.isSimpleCircular() &&
978 rrect.radii(SkRRect::kUpperLeft_Corner).fX > SK_ScalarNearlyZero;
979 if (!isRRect &&
980 path.isOval(&rect) && SkScalarNearlyEqual(rect.width(), rect.height()) &&
981 rect.width() > SK_ScalarNearlyZero) {
982 rrect.setOval(rect);
983 isRRect = true;
984 }
985 if (!isRRect && path.isRect(&rect)) {
986 rrect.setRect(rect);
987 isRRect = true;
988 }
989
990 if (!isRRect) {
991 return false;
992 }
993
Jim Van Verthc5903412016-11-17 15:27:09 -0500994 if (rrect.isEmpty()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -0400995 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -0500996 }
997
Robert Phillips72152832017-01-25 17:31:35 -0500998 AutoCheckFlush acf(this->drawingManager());
Jim Van Verthc5903412016-11-17 15:27:09 -0500999
Jim Van Verth3af1af92017-05-18 15:06:54 -04001000 // transform light
1001 SkPoint3 devLightPos = map(viewMatrix, rec.fLightPos);
1002
1003 // 1/scale
1004 SkScalar devToSrcScale = viewMatrix.isScaleTranslate() ?
1005 SkScalarInvert(viewMatrix[SkMatrix::kMScaleX]) :
1006 sk_float_rsqrt(viewMatrix[SkMatrix::kMScaleX] * viewMatrix[SkMatrix::kMScaleX] +
1007 viewMatrix[SkMatrix::kMSkewX] * viewMatrix[SkMatrix::kMSkewX]);
1008
1009 SkScalar occluderHeight = rec.fZPlaneParams.fZ;
Brian Salomon05969092017-07-13 11:20:51 -04001010 GrColor4f color = GrColor4f::FromGrColor(color4ub);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001011 bool transparent = SkToBool(rec.fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag);
Jim Van Verth34d6e4b2017-06-09 11:09:03 -04001012 bool tonalColor = SkToBool(rec.fFlags & SkShadowFlags::kTonalColor_ShadowFlag);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001013
1014 if (rec.fAmbientAlpha > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001015 SkScalar devSpaceInsetWidth = SkDrawShadowMetrics::AmbientBlurRadius(occluderHeight);
1016 const SkScalar umbraRecipAlpha = SkDrawShadowMetrics::AmbientRecipAlpha(occluderHeight);
1017 const SkScalar devSpaceAmbientBlur = devSpaceInsetWidth * umbraRecipAlpha;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001018
1019 // Outset the shadow rrect to the border of the penumbra
1020 SkScalar ambientPathOutset = devSpaceInsetWidth * devToSrcScale;
1021 SkRRect ambientRRect;
1022 SkRect outsetRect = rrect.rect().makeOutset(ambientPathOutset, ambientPathOutset);
1023 // If the rrect was an oval then its outset will also be one.
1024 // We set it explicitly to avoid errors.
1025 if (rrect.isOval()) {
1026 ambientRRect = SkRRect::MakeOval(outsetRect);
1027 } else {
1028 SkScalar outsetRad = rrect.getSimpleRadii().fX + ambientPathOutset;
1029 ambientRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1030 }
1031
Jim Van Verth34d6e4b2017-06-09 11:09:03 -04001032 GrColor ambientColor;
1033 if (tonalColor) {
1034 // with tonal color, the color only applies to the spot shadow
1035 ambientColor = GrColorPackRGBA(0, 0, 0, 255.999f*rec.fAmbientAlpha);
1036 } else {
1037 ambientColor = color.mulByScalar(rec.fAmbientAlpha).toGrColor();
1038 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001039 if (transparent) {
1040 // set a large inset to force a fill
1041 devSpaceInsetWidth = ambientRRect.width();
1042 }
1043 // the fraction of the blur we want to apply is devSpaceInsetWidth/devSpaceAmbientBlur,
Jim Van Verth1af03d42017-07-31 09:34:58 -04001044 // which is just 1/umbraRecipAlpha.
1045 SkScalar blurClamp = SkScalarInvert(umbraRecipAlpha);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001046
Brian Salomon05969092017-07-13 11:20:51 -04001047 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(ambientColor, viewMatrix,
1048 ambientRRect,
1049 devSpaceAmbientBlur,
1050 devSpaceInsetWidth,
1051 blurClamp);
1052 SkASSERT(op);
1053 this->addDrawOp(clip, std::move(op));
Jim Van Verthc5903412016-11-17 15:27:09 -05001054 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001055
1056 if (rec.fSpotAlpha > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001057 SkScalar devSpaceSpotBlur;
1058 SkScalar spotScale;
1059 SkVector spotOffset;
1060 SkDrawShadowMetrics::GetSpotParams(occluderHeight, devLightPos.fX, devLightPos.fY,
1061 devLightPos.fZ, rec.fLightRadius,
1062 &devSpaceSpotBlur, &spotScale, &spotOffset);
1063 // handle scale of radius due to CTM
Jim Van Verth3af1af92017-05-18 15:06:54 -04001064 const SkScalar srcSpaceSpotBlur = devSpaceSpotBlur * devToSrcScale;
1065
Jim Van Verth3af1af92017-05-18 15:06:54 -04001066 // Adjust translate for the effect of the scale.
1067 spotOffset.fX += spotScale*viewMatrix[SkMatrix::kMTransX];
1068 spotOffset.fY += spotScale*viewMatrix[SkMatrix::kMTransY];
1069 // This offset is in dev space, need to transform it into source space.
1070 SkMatrix ctmInverse;
1071 if (viewMatrix.invert(&ctmInverse)) {
1072 ctmInverse.mapPoints(&spotOffset, 1);
1073 } else {
1074 // Since the matrix is a similarity, this should never happen, but just in case...
1075 SkDebugf("Matrix is degenerate. Will not render spot shadow correctly!\n");
1076 SkASSERT(false);
1077 }
1078
1079 // Compute the transformed shadow rrect
1080 SkRRect spotShadowRRect;
1081 SkMatrix shadowTransform;
1082 shadowTransform.setScaleTranslate(spotScale, spotScale, spotOffset.fX, spotOffset.fY);
1083 rrect.transform(shadowTransform, &spotShadowRRect);
1084 SkScalar spotRadius = spotShadowRRect.getSimpleRadii().fX;
1085
1086 // Compute the insetWidth
Jim Van Verth1af03d42017-07-31 09:34:58 -04001087 SkScalar blurOutset = srcSpaceSpotBlur;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001088 SkScalar insetWidth = blurOutset;
1089 if (transparent) {
1090 // If transparent, just do a fill
1091 insetWidth += spotShadowRRect.width();
1092 } else {
1093 // For shadows, instead of using a stroke we specify an inset from the penumbra
1094 // border. We want to extend this inset area so that it meets up with the caster
1095 // geometry. The inset geometry will by default already be inset by the blur width.
1096 //
1097 // We compare the min and max corners inset by the radius between the original
1098 // rrect and the shadow rrect. The distance between the two plus the difference
1099 // between the scaled radius and the original radius gives the distance from the
1100 // transformed shadow shape to the original shape in that corner. The max
1101 // of these gives the maximum distance we need to cover.
1102 //
1103 // Since we are outsetting by 1/2 the blur distance, we just add the maxOffset to
1104 // that to get the full insetWidth.
1105 SkScalar maxOffset;
1106 if (rrect.isRect()) {
1107 // Manhattan distance works better for rects
1108 maxOffset = SkTMax(SkTMax(SkTAbs(spotShadowRRect.rect().fLeft -
1109 rrect.rect().fLeft),
1110 SkTAbs(spotShadowRRect.rect().fTop -
1111 rrect.rect().fTop)),
1112 SkTMax(SkTAbs(spotShadowRRect.rect().fRight -
1113 rrect.rect().fRight),
1114 SkTAbs(spotShadowRRect.rect().fBottom -
1115 rrect.rect().fBottom)));
1116 } else {
1117 SkScalar dr = spotRadius - rrect.getSimpleRadii().fX;
1118 SkPoint upperLeftOffset = SkPoint::Make(spotShadowRRect.rect().fLeft -
1119 rrect.rect().fLeft + dr,
1120 spotShadowRRect.rect().fTop -
1121 rrect.rect().fTop + dr);
1122 SkPoint lowerRightOffset = SkPoint::Make(spotShadowRRect.rect().fRight -
1123 rrect.rect().fRight - dr,
1124 spotShadowRRect.rect().fBottom -
1125 rrect.rect().fBottom - dr);
1126 maxOffset = SkScalarSqrt(SkTMax(upperLeftOffset.lengthSqd(),
1127 lowerRightOffset.lengthSqd())) + dr;
1128 }
1129 insetWidth += maxOffset;
1130 }
1131
1132 // Outset the shadow rrect to the border of the penumbra
1133 SkRect outsetRect = spotShadowRRect.rect().makeOutset(blurOutset, blurOutset);
1134 if (spotShadowRRect.isOval()) {
1135 spotShadowRRect = SkRRect::MakeOval(outsetRect);
1136 } else {
1137 SkScalar outsetRad = spotRadius + blurOutset;
1138 spotShadowRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1139 }
1140
Jim Van Verth34d6e4b2017-06-09 11:09:03 -04001141 GrColor spotColor;
1142 if (tonalColor) {
1143 SkScalar colorScale;
1144 SkScalar tonalAlpha;
1145 SkShadowUtils::ComputeTonalColorParams(color.fRGBA[0], color.fRGBA[1],
1146 color.fRGBA[2], rec.fSpotAlpha,
1147 &colorScale, &tonalAlpha);
1148 color.fRGBA[0] *= colorScale;
1149 color.fRGBA[1] *= colorScale;
1150 color.fRGBA[2] *= colorScale;
1151 color.fRGBA[3] = tonalAlpha;
1152 spotColor = color.toGrColor();
1153 } else {
1154 spotColor = color.mulByScalar(rec.fSpotAlpha).toGrColor();
1155 }
1156
Brian Salomon05969092017-07-13 11:20:51 -04001157 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(spotColor, viewMatrix,
1158 spotShadowRRect,
Jim Van Verth1af03d42017-07-31 09:34:58 -04001159 2.0f * devSpaceSpotBlur,
Brian Salomon05969092017-07-13 11:20:51 -04001160 insetWidth);
1161 SkASSERT(op);
1162 this->addDrawOp(clip, std::move(op));
Jim Van Verth3af1af92017-05-18 15:06:54 -04001163 }
1164
1165 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001166}
1167
1168///////////////////////////////////////////////////////////////////////////////
1169
Brian Osman11052242016-10-27 14:47:55 -04001170bool GrRenderTargetContext::drawFilledDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001171 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001172 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001173 const SkMatrix& viewMatrix,
1174 const SkRRect& origOuter,
1175 const SkRRect& origInner) {
robertphillips00095892016-02-29 13:50:40 -08001176 SkASSERT(!origInner.isEmpty());
1177 SkASSERT(!origOuter.isEmpty());
1178
csmartdaltone0d36292016-07-29 08:14:20 -07001179 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport()) {
Robert Phillipse3302df2017-04-24 07:31:02 -04001180 gr_instanced::OpAllocator* oa = this->drawingManager()->instancingAllocator();
1181 std::unique_ptr<GrDrawOp> op(oa->recordDRRect(
Brian Salomon54d212e2017-03-21 14:22:38 -04001182 origOuter, origInner, viewMatrix, std::move(paint), aa, fInstancedPipelineInfo));
Brian Salomon42521e82016-12-07 16:44:58 -05001183 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -04001184 this->addDrawOp(clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -07001185 return true;
1186 }
1187 }
1188
Brian Salomon7c8460e2017-05-12 11:36:10 -04001189 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
robertphillips00095892016-02-29 13:50:40 -08001190
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001191 GrPrimitiveEdgeType innerEdgeType, outerEdgeType;
1192 if (GrAAType::kCoverage == aaType) {
1193 innerEdgeType = kInverseFillAA_GrProcessorEdgeType;
1194 outerEdgeType = kFillAA_GrProcessorEdgeType;
1195 } else {
1196 innerEdgeType = kInverseFillBW_GrProcessorEdgeType;
1197 outerEdgeType = kFillBW_GrProcessorEdgeType;
1198 }
robertphillips00095892016-02-29 13:50:40 -08001199
1200 SkTCopyOnFirstWrite<SkRRect> inner(origInner), outer(origOuter);
1201 SkMatrix inverseVM;
1202 if (!viewMatrix.isIdentity()) {
1203 if (!origInner.transform(viewMatrix, inner.writable())) {
1204 return false;
1205 }
1206 if (!origOuter.transform(viewMatrix, outer.writable())) {
1207 return false;
1208 }
1209 if (!viewMatrix.invert(&inverseVM)) {
1210 return false;
1211 }
1212 } else {
1213 inverseVM.reset();
halcanary9d524f22016-03-29 09:03:52 -07001214 }
robertphillips00095892016-02-29 13:50:40 -08001215
robertphillips00095892016-02-29 13:50:40 -08001216 // TODO these need to be a geometry processors
bungeman06ca8ec2016-06-09 08:01:03 -07001217 sk_sp<GrFragmentProcessor> innerEffect(GrRRectEffect::Make(innerEdgeType, *inner));
robertphillips00095892016-02-29 13:50:40 -08001218 if (!innerEffect) {
1219 return false;
1220 }
1221
bungeman06ca8ec2016-06-09 08:01:03 -07001222 sk_sp<GrFragmentProcessor> outerEffect(GrRRectEffect::Make(outerEdgeType, *outer));
robertphillips00095892016-02-29 13:50:40 -08001223 if (!outerEffect) {
1224 return false;
1225 }
1226
Brian Salomon82f44312017-01-11 13:42:54 -05001227 paint.addCoverageFragmentProcessor(std::move(innerEffect));
1228 paint.addCoverageFragmentProcessor(std::move(outerEffect));
robertphillips00095892016-02-29 13:50:40 -08001229
1230 SkRect bounds = outer->getBounds();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001231 if (GrAAType::kCoverage == aaType) {
robertphillips00095892016-02-29 13:50:40 -08001232 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1233 }
halcanary9d524f22016-03-29 09:03:52 -07001234
Brian Salomon82f44312017-01-11 13:42:54 -05001235 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), bounds,
1236 inverseVM);
robertphillips00095892016-02-29 13:50:40 -08001237 return true;
1238}
1239
Brian Osman11052242016-10-27 14:47:55 -04001240void GrRenderTargetContext::drawDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001241 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001242 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001243 const SkMatrix& viewMatrix,
1244 const SkRRect& outer,
1245 const SkRRect& inner) {
robertphillips00095892016-02-29 13:50:40 -08001246 ASSERT_SINGLE_OWNER
1247 RETURN_IF_ABANDONED
1248 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001249 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawDRRect", fContext);
robertphillips00095892016-02-29 13:50:40 -08001250
1251 SkASSERT(!outer.isEmpty());
1252 SkASSERT(!inner.isEmpty());
1253
Robert Phillips72152832017-01-25 17:31:35 -05001254 AutoCheckFlush acf(this->drawingManager());
robertphillips00095892016-02-29 13:50:40 -08001255
Brian Salomon82f44312017-01-11 13:42:54 -05001256 if (this->drawFilledDRRect(clip, std::move(paint), aa, viewMatrix, outer, inner)) {
robertphillips00095892016-02-29 13:50:40 -08001257 return;
1258 }
1259
1260 SkPath path;
1261 path.setIsVolatile(true);
1262 path.addRRect(inner);
1263 path.addRRect(outer);
1264 path.setFillType(SkPath::kEvenOdd_FillType);
1265
Brian Salomon82f44312017-01-11 13:42:54 -05001266 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, GrStyle::SimpleFill());
robertphillips00095892016-02-29 13:50:40 -08001267}
1268
robertphillipsea461502015-05-26 11:38:03 -07001269///////////////////////////////////////////////////////////////////////////////
1270
msarettcc319b92016-08-25 18:07:18 -07001271static inline bool is_int(float x) {
1272 return x == (float) sk_float_round2int(x);
1273}
1274
Brian Osman11052242016-10-27 14:47:55 -04001275void GrRenderTargetContext::drawRegion(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001276 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001277 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001278 const SkMatrix& viewMatrix,
1279 const SkRegion& region,
1280 const GrStyle& style) {
msarettcc319b92016-08-25 18:07:18 -07001281 ASSERT_SINGLE_OWNER
1282 RETURN_IF_ABANDONED
1283 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001284 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRegion", fContext);
msarettcc319b92016-08-25 18:07:18 -07001285
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001286 if (GrAA::kYes == aa) {
Brian Salomonfc527d22016-12-14 21:07:01 -05001287 // GrRegionOp performs no antialiasing but is much faster, so here we check the matrix
Brian Salomonc57c7c92016-12-06 14:47:34 -05001288 // to see whether aa is really required.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001289 if (!SkToBool(viewMatrix.getType() & ~(SkMatrix::kTranslate_Mask)) &&
1290 is_int(viewMatrix.getTranslateX()) &&
1291 is_int(viewMatrix.getTranslateY())) {
1292 aa = GrAA::kNo;
1293 }
Brian Salomonc57c7c92016-12-06 14:47:34 -05001294 }
msarettcc319b92016-08-25 18:07:18 -07001295 bool complexStyle = !style.isSimpleFill();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001296 if (complexStyle || GrAA::kYes == aa) {
msarettcc319b92016-08-25 18:07:18 -07001297 SkPath path;
1298 region.getBoundaryPath(&path);
Brian Salomon82f44312017-01-11 13:42:54 -05001299 return this->drawPath(clip, std::move(paint), aa, viewMatrix, path, style);
msarettcc319b92016-08-25 18:07:18 -07001300 }
1301
Brian Salomonf0366322017-07-11 15:53:05 -04001302 GrAAType aaType = this->chooseAAType(GrAA::kNo, GrAllowMixedSamples::kNo);
1303 std::unique_ptr<GrDrawOp> op = GrRegionOp::Make(std::move(paint), viewMatrix, region, aaType);
1304 this->addDrawOp(clip, std::move(op));
msarettcc319b92016-08-25 18:07:18 -07001305}
1306
Brian Osman11052242016-10-27 14:47:55 -04001307void GrRenderTargetContext::drawOval(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001308 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001309 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001310 const SkMatrix& viewMatrix,
1311 const SkRect& oval,
1312 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001313 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001314 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001315 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001316 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawOval", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -07001317
robertphillipsea461502015-05-26 11:38:03 -07001318 if (oval.isEmpty()) {
1319 return;
1320 }
1321
bsalomon6663acf2016-05-10 09:14:17 -07001322 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
robertphillipsea461502015-05-26 11:38:03 -07001323
Robert Phillips72152832017-01-25 17:31:35 -05001324 AutoCheckFlush acf(this->drawingManager());
bsalomon6663acf2016-05-10 09:14:17 -07001325 const SkStrokeRec& stroke = style.strokeRec();
csmartdaltona7f29642016-07-07 08:49:11 -07001326
csmartdaltone0d36292016-07-29 08:14:20 -07001327 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport() &&
1328 stroke.isFillStyle()) {
Robert Phillipse3302df2017-04-24 07:31:02 -04001329 gr_instanced::OpAllocator* oa = this->drawingManager()->instancingAllocator();
Brian Salomon54d212e2017-03-21 14:22:38 -04001330 std::unique_ptr<GrDrawOp> op(
Robert Phillipse3302df2017-04-24 07:31:02 -04001331 oa->recordOval(oval, viewMatrix, std::move(paint), aa, fInstancedPipelineInfo));
Brian Salomon42521e82016-12-07 16:44:58 -05001332 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -04001333 this->addDrawOp(clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -07001334 return;
1335 }
1336 }
1337
Brian Salomon7c8460e2017-05-12 11:36:10 -04001338 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001339 if (GrAAType::kCoverage == aaType) {
Brian Salomon94efbf52016-11-29 13:43:05 -05001340 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomon05441c42017-05-15 16:45:49 -04001341 std::unique_ptr<GrDrawOp> op =
1342 GrOvalOpFactory::MakeOvalOp(std::move(paint), viewMatrix, oval, stroke, shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -05001343 if (op) {
Brian Salomon05441c42017-05-15 16:45:49 -04001344 this->addDrawOp(clip, std::move(op));
robertphillipsb56f9272016-02-25 11:03:52 -08001345 return;
1346 }
robertphillipsea461502015-05-26 11:38:03 -07001347 }
robertphillipsb56f9272016-02-25 11:03:52 -08001348
1349 SkPath path;
1350 path.setIsVolatile(true);
1351 path.addOval(oval);
Brian Salomon82f44312017-01-11 13:42:54 -05001352 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, style);
robertphillipsea461502015-05-26 11:38:03 -07001353}
1354
Brian Osman11052242016-10-27 14:47:55 -04001355void GrRenderTargetContext::drawArc(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001356 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001357 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001358 const SkMatrix& viewMatrix,
1359 const SkRect& oval,
1360 SkScalar startAngle,
1361 SkScalar sweepAngle,
1362 bool useCenter,
1363 const GrStyle& style) {
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001364 ASSERT_SINGLE_OWNER
1365 RETURN_IF_ABANDONED
1366 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001367 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawArc", fContext);
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001368
1369 AutoCheckFlush acf(this->drawingManager());
1370
Brian Salomon7c8460e2017-05-12 11:36:10 -04001371 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001372 if (GrAAType::kCoverage == aaType) {
Brian Salomon94efbf52016-11-29 13:43:05 -05001373 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomon05441c42017-05-15 16:45:49 -04001374 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeArcOp(std::move(paint),
1375 viewMatrix,
1376 oval,
1377 startAngle,
1378 sweepAngle,
1379 useCenter,
1380 style,
1381 shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -05001382 if (op) {
Brian Salomon05441c42017-05-15 16:45:49 -04001383 this->addDrawOp(clip, std::move(op));
bsalomon4f3a0ca2016-08-22 13:14:26 -07001384 return;
1385 }
1386 }
1387 SkPath path;
bsalomon21af9ca2016-08-25 12:29:23 -07001388 SkPathPriv::CreateDrawArcPath(&path, oval, startAngle, sweepAngle, useCenter,
1389 style.isSimpleFill());
Brian Salomon82f44312017-01-11 13:42:54 -05001390 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, style);
bsalomon4f3a0ca2016-08-22 13:14:26 -07001391}
1392
Brian Osman11052242016-10-27 14:47:55 -04001393void GrRenderTargetContext::drawImageLattice(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001394 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001395 const SkMatrix& viewMatrix,
1396 int imageWidth,
1397 int imageHeight,
1398 std::unique_ptr<SkLatticeIter> iter,
1399 const SkRect& dst) {
joshualitt1de610a2016-01-06 08:26:09 -08001400 ASSERT_SINGLE_OWNER
joshualitt33a5fce2015-11-18 13:28:51 -08001401 RETURN_IF_ABANDONED
1402 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001403 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawImageLattice", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001404
Robert Phillips72152832017-01-25 17:31:35 -05001405 AutoCheckFlush acf(this->drawingManager());
joshualitt33a5fce2015-11-18 13:28:51 -08001406
Brian Salomon815486c2017-07-11 08:52:13 -04001407 std::unique_ptr<GrDrawOp> op = GrLatticeOp::MakeNonAA(std::move(paint), viewMatrix, imageWidth,
1408 imageHeight, std::move(iter), dst);
1409 this->addDrawOp(clip, std::move(op));
joshualitt33a5fce2015-11-18 13:28:51 -08001410}
1411
Yuqian Licc8eb602017-08-01 17:43:30 +00001412bool GrRenderTargetContext::prepareForExternalIO(int numSemaphores,
1413 GrBackendSemaphore* backendSemaphores) {
robertphillips8c523e02016-07-26 07:41:00 -07001414 ASSERT_SINGLE_OWNER
Yuqian Licc8eb602017-08-01 17:43:30 +00001415 RETURN_FALSE_IF_ABANDONED
robertphillips8c523e02016-07-26 07:41:00 -07001416 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001417 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "prepareForExternalIO", fContext);
robertphillips8c523e02016-07-26 07:41:00 -07001418
Yuqian Licc8eb602017-08-01 17:43:30 +00001419 if (numSemaphores && !this->caps()->fenceSyncSupport()) {
1420 this->drawingManager()->prepareSurfaceForExternalIO(fRenderTargetProxy.get());
1421 return false;
1422 }
1423
1424 SkTArray<sk_sp<GrSemaphore>> semaphores(numSemaphores);
1425 for (int i = 0; i < numSemaphores; ++i) {
1426 if (backendSemaphores[i].isInitialized()) {
1427 semaphores.push_back(fContext->resourceProvider()->wrapBackendSemaphore(
1428 backendSemaphores[i], kBorrow_GrWrapOwnership));
1429 } else {
1430 semaphores.push_back(fContext->resourceProvider()->makeSemaphore(false));
1431 }
1432 // Create signal semaphore ops and force the final one to call flush.
1433 bool forceFlush = (i == (numSemaphores - 1));
1434 std::unique_ptr<GrOp> signalOp(GrSemaphoreOp::MakeSignal(semaphores.back(),
1435 fRenderTargetProxy.get(),
1436 forceFlush));
1437 this->getRTOpList()->addOp(std::move(signalOp), *this->caps());
1438 }
1439
1440 this->drawingManager()->prepareSurfaceForExternalIO(fRenderTargetProxy.get());
1441
1442 for (int i = 0; i < numSemaphores; ++i) {
1443 if (!backendSemaphores[i].isInitialized()) {
1444 semaphores[i]->setBackendSemaphore(&backendSemaphores[i]);
1445 }
1446 }
1447 return true;
Greg Daniela5cb7812017-06-16 09:45:32 -04001448}
1449
Greg Danielc64ee462017-06-15 16:59:49 -04001450bool GrRenderTargetContext::waitOnSemaphores(int numSemaphores,
Greg Daniela5cb7812017-06-16 09:45:32 -04001451 const GrBackendSemaphore* waitSemaphores) {
1452 ASSERT_SINGLE_OWNER
Greg Danielc64ee462017-06-15 16:59:49 -04001453 RETURN_FALSE_IF_ABANDONED
Greg Daniela5cb7812017-06-16 09:45:32 -04001454 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001455 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "waitOnSemaphores", fContext);
Greg Daniela5cb7812017-06-16 09:45:32 -04001456
1457 AutoCheckFlush acf(this->drawingManager());
1458
Greg Danielc64ee462017-06-15 16:59:49 -04001459 if (numSemaphores && !this->caps()->fenceSyncSupport()) {
1460 return false;
1461 }
1462
Greg Daniela5cb7812017-06-16 09:45:32 -04001463 SkTArray<sk_sp<GrSemaphore>> semaphores(numSemaphores);
1464 for (int i = 0; i < numSemaphores; ++i) {
1465 sk_sp<GrSemaphore> sema = fContext->resourceProvider()->wrapBackendSemaphore(
1466 waitSemaphores[i], kAdopt_GrWrapOwnership);
1467 std::unique_ptr<GrOp> waitOp(GrSemaphoreOp::MakeWait(sema, fRenderTargetProxy.get()));
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001468 this->getRTOpList()->addOp(std::move(waitOp), *this->caps());
Greg Daniela5cb7812017-06-16 09:45:32 -04001469 }
Greg Danielc64ee462017-06-15 16:59:49 -04001470 return true;
robertphillips8c523e02016-07-26 07:41:00 -07001471}
joshualitt33a5fce2015-11-18 13:28:51 -08001472
robertphillipsea461502015-05-26 11:38:03 -07001473// Can 'path' be drawn as a pair of filled nested rectangles?
bsalomon6663acf2016-05-10 09:14:17 -07001474static bool fills_as_nested_rects(const SkMatrix& viewMatrix, const SkPath& path, SkRect rects[2]) {
robertphillipsea461502015-05-26 11:38:03 -07001475
1476 if (path.isInverseFillType()) {
1477 return false;
1478 }
1479
1480 // TODO: this restriction could be lifted if we were willing to apply
1481 // the matrix to all the points individually rather than just to the rect
robertphillips0e7029e2015-11-30 05:45:06 -08001482 if (!viewMatrix.rectStaysRect()) {
robertphillipsea461502015-05-26 11:38:03 -07001483 return false;
1484 }
1485
1486 SkPath::Direction dirs[2];
1487 if (!path.isNestedFillRects(rects, dirs)) {
1488 return false;
1489 }
1490
1491 if (SkPath::kWinding_FillType == path.getFillType() && dirs[0] == dirs[1]) {
1492 // The two rects need to be wound opposite to each other
1493 return false;
1494 }
1495
1496 // Right now, nested rects where the margin is not the same width
1497 // all around do not render correctly
1498 const SkScalar* outer = rects[0].asScalars();
1499 const SkScalar* inner = rects[1].asScalars();
1500
1501 bool allEq = true;
1502
1503 SkScalar margin = SkScalarAbs(outer[0] - inner[0]);
1504 bool allGoE1 = margin >= SK_Scalar1;
1505
1506 for (int i = 1; i < 4; ++i) {
1507 SkScalar temp = SkScalarAbs(outer[i] - inner[i]);
1508 if (temp < SK_Scalar1) {
1509 allGoE1 = false;
1510 }
1511 if (!SkScalarNearlyEqual(margin, temp)) {
1512 allEq = false;
1513 }
1514 }
1515
1516 return allEq || allGoE1;
1517}
1518
Brian Osman11052242016-10-27 14:47:55 -04001519void GrRenderTargetContext::drawPath(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001520 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001521 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001522 const SkMatrix& viewMatrix,
1523 const SkPath& path,
1524 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001525 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001526 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001527 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001528 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawPath", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -07001529
robertphillipsea461502015-05-26 11:38:03 -07001530 if (path.isEmpty()) {
1531 if (path.isInverseFillType()) {
Brian Salomon82f44312017-01-11 13:42:54 -05001532 this->drawPaint(clip, std::move(paint), viewMatrix);
robertphillipsea461502015-05-26 11:38:03 -07001533 }
1534 return;
1535 }
1536
Robert Phillips72152832017-01-25 17:31:35 -05001537 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -07001538
Brian Salomon7c8460e2017-05-12 11:36:10 -04001539 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001540 if (GrAAType::kCoverage == aaType && !style.pathEffect()) {
bsalomon6663acf2016-05-10 09:14:17 -07001541 if (style.isSimpleFill() && !path.isConvex()) {
robertphillipsea461502015-05-26 11:38:03 -07001542 // Concave AA paths are expensive - try to avoid them for special cases
1543 SkRect rects[2];
1544
bsalomon6663acf2016-05-10 09:14:17 -07001545 if (fills_as_nested_rects(viewMatrix, path, rects)) {
Brian Salomonbaaf4392017-06-15 09:59:23 -04001546 std::unique_ptr<GrDrawOp> op =
1547 GrRectOpFactory::MakeAAFillNestedRects(std::move(paint), viewMatrix, rects);
Brian Salomon5f970fe2017-06-16 17:30:59 -04001548 if (op) {
1549 this->addDrawOp(clip, std::move(op));
bsalomon40ef4852016-05-02 13:22:13 -07001550 }
Brian Salomon5f970fe2017-06-16 17:30:59 -04001551 // A null return indicates that there is nothing to draw in this case.
1552 return;
robertphillipsea461502015-05-26 11:38:03 -07001553 }
1554 }
1555 SkRect ovalRect;
1556 bool isOval = path.isOval(&ovalRect);
1557
1558 if (isOval && !path.isInverseFillType()) {
Brian Salomon94efbf52016-11-29 13:43:05 -05001559 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomon05441c42017-05-15 16:45:49 -04001560 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeOvalOp(
1561 std::move(paint), viewMatrix, ovalRect, style.strokeRec(), shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -05001562 if (op) {
Brian Salomon05441c42017-05-15 16:45:49 -04001563 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -07001564 return;
1565 }
1566 }
1567 }
robertphillips4bc31812016-03-01 12:22:49 -08001568
1569 // Note that internalDrawPath may sw-rasterize the path into a scratch texture.
1570 // Scratch textures can be recycled after they are returned to the texture
1571 // cache. This presents a potential hazard for buffered drawing. However,
1572 // the writePixels that uploads to the scratch will perform a flush so we're
1573 // OK.
Brian Salomon82f44312017-01-11 13:42:54 -05001574 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, style);
robertphillipsea461502015-05-26 11:38:03 -07001575}
1576
Brian Osman11052242016-10-27 14:47:55 -04001577bool GrRenderTargetContextPriv::drawAndStencilPath(const GrClip& clip,
1578 const GrUserStencilSettings* ss,
1579 SkRegion::Op op,
1580 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001581 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001582 const SkMatrix& viewMatrix,
1583 const SkPath& path) {
robertphillips391395d2016-03-02 09:26:36 -08001584 ASSERT_SINGLE_OWNER_PRIV
1585 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -04001586 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001587 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawAndStencilPath",
1588 fRenderTargetContext->fContext);
robertphillips391395d2016-03-02 09:26:36 -08001589
1590 if (path.isEmpty() && path.isInverseFillType()) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001591 this->drawAndStencilRect(clip, ss, op, invert, GrAA::kNo, SkMatrix::I(),
Brian Osman11052242016-10-27 14:47:55 -04001592 SkRect::MakeIWH(fRenderTargetContext->width(),
1593 fRenderTargetContext->height()));
robertphillips391395d2016-03-02 09:26:36 -08001594 return true;
1595 }
1596
Robert Phillips72152832017-01-25 17:31:35 -05001597 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -08001598
1599 // An Assumption here is that path renderer would use some form of tweaking
1600 // the src color (either the input alpha or in the frag shader) to implement
1601 // aa. If we have some future driver-mojo path AA that can do the right
1602 // thing WRT to the blend then we'll need some query on the PR.
Brian Salomon7c8460e2017-05-12 11:36:10 -04001603 GrAAType aaType = fRenderTargetContext->chooseAAType(aa, GrAllowMixedSamples::kNo);
robertphillips976f5f02016-06-03 10:59:20 -07001604 bool hasUserStencilSettings = !ss->isUnused();
robertphillips391395d2016-03-02 09:26:36 -08001605
bsalomon8acedde2016-06-24 10:42:16 -07001606 GrShape shape(path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -08001607 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Eric Karl5c779752017-05-08 12:02:07 -07001608 canDrawArgs.fCaps = fRenderTargetContext->drawingManager()->getContext()->caps();
robertphillips391395d2016-03-02 09:26:36 -08001609 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07001610 canDrawArgs.fShape = &shape;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001611 canDrawArgs.fAAType = aaType;
cdalton93a379b2016-05-11 13:58:08 -07001612 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips391395d2016-03-02 09:26:36 -08001613
1614 // Don't allow the SW renderer
Robert Phillips72152832017-01-25 17:31:35 -05001615 GrPathRenderer* pr = fRenderTargetContext->drawingManager()->getPathRenderer(
Brian Salomon36aa1762016-12-10 13:24:02 -05001616 canDrawArgs, false, GrPathRendererChain::DrawType::kStencilAndColor);
robertphillips391395d2016-03-02 09:26:36 -08001617 if (!pr) {
1618 return false;
1619 }
1620
1621 GrPaint paint;
1622 paint.setCoverageSetOpXPFactory(op, invert);
1623
Brian Salomon82f44312017-01-11 13:42:54 -05001624 GrPathRenderer::DrawPathArgs args{
Robert Phillips256c37b2017-03-01 14:32:46 -05001625 fRenderTargetContext->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05001626 std::move(paint),
1627 ss,
1628 fRenderTargetContext,
1629 &clip,
1630 &viewMatrix,
1631 &shape,
1632 aaType,
1633 fRenderTargetContext->isGammaCorrect()};
robertphillips391395d2016-03-02 09:26:36 -08001634 pr->drawPath(args);
1635 return true;
1636}
1637
Brian Osman11052242016-10-27 14:47:55 -04001638SkBudgeted GrRenderTargetContextPriv::isBudgeted() const {
robertphillips714712b2016-08-04 06:20:45 -07001639 ASSERT_SINGLE_OWNER_PRIV
1640
Brian Osman11052242016-10-27 14:47:55 -04001641 if (fRenderTargetContext->wasAbandoned()) {
robertphillips714712b2016-08-04 06:20:45 -07001642 return SkBudgeted::kNo;
1643 }
1644
Brian Osman11052242016-10-27 14:47:55 -04001645 SkDEBUGCODE(fRenderTargetContext->validate();)
robertphillips714712b2016-08-04 06:20:45 -07001646
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001647 return fRenderTargetContext->fRenderTargetProxy->isBudgeted();
robertphillips714712b2016-08-04 06:20:45 -07001648}
1649
Brian Osman11052242016-10-27 14:47:55 -04001650void GrRenderTargetContext::internalDrawPath(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001651 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001652 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001653 const SkMatrix& viewMatrix,
1654 const SkPath& path,
1655 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001656 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001657 RETURN_IF_ABANDONED
Brian Salomondcbb9d92017-07-19 10:53:20 -04001658 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "internalDrawPath", fContext);
1659
bsalomon8acedde2016-06-24 10:42:16 -07001660 SkASSERT(!path.isEmpty());
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001661 GrShape shape;
Brian Salomon7c8460e2017-05-12 11:36:10 -04001662 // NVPR cannot handle hairlines, so this would get picked up by a different stencil and
1663 // cover path renderer (i.e. default path renderer). The hairline renderer produces much
1664 // smoother hairlines than MSAA.
1665 GrAllowMixedSamples allowMixedSamples =
1666 style.isSimpleHairline() ? GrAllowMixedSamples::kNo : GrAllowMixedSamples::kYes;
1667 GrAAType aaType = this->chooseAAType(aa, allowMixedSamples);
robertphillips68737822015-10-29 12:12:21 -07001668 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Eric Karl5c779752017-05-08 12:02:07 -07001669 canDrawArgs.fCaps = this->drawingManager()->getContext()->caps();
robertphillips68737822015-10-29 12:12:21 -07001670 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07001671 canDrawArgs.fShape = &shape;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001672 canDrawArgs.fHasUserStencilSettings = false;
robertphillips68737822015-10-29 12:12:21 -07001673
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001674 GrPathRenderer* pr;
Brian Salomon82125e92016-12-10 09:35:48 -05001675 static constexpr GrPathRendererChain::DrawType kType = GrPathRendererChain::DrawType::kColor;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001676 do {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001677 shape = GrShape(path, style);
bsalomon8acedde2016-06-24 10:42:16 -07001678 if (shape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001679 return;
1680 }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001681
1682 canDrawArgs.fAAType = aaType;
1683
1684 // Try a 1st time without applying any of the style to the geometry (and barring sw)
Robert Phillips72152832017-01-25 17:31:35 -05001685 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001686 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
1687
1688 if (!pr && shape.style().pathEffect()) {
1689 // It didn't work above, so try again with the path effect applied.
1690 shape = shape.applyStyle(GrStyle::Apply::kPathEffectOnly, styleScale);
bsalomon8acedde2016-06-24 10:42:16 -07001691 if (shape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001692 return;
1693 }
Robert Phillips72152832017-01-25 17:31:35 -05001694 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
bsalomon6663acf2016-05-10 09:14:17 -07001695 }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001696 if (!pr) {
1697 if (shape.style().applies()) {
1698 shape = shape.applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec, styleScale);
1699 if (shape.isEmpty()) {
1700 return;
1701 }
1702 }
1703 // This time, allow SW renderer
Robert Phillips72152832017-01-25 17:31:35 -05001704 pr = this->drawingManager()->getPathRenderer(canDrawArgs, true, kType);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001705 }
Brian Salomon0abc8b42016-12-13 10:22:54 -05001706 if (!pr && GrAATypeIsHW(aaType)) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001707 // There are exceptional cases where we may wind up falling back to coverage based AA
1708 // when the target is MSAA (e.g. through disabling path renderers via GrContextOptions).
1709 aaType = GrAAType::kCoverage;
1710 } else {
1711 break;
1712 }
1713 } while(true);
robertphillipsea461502015-05-26 11:38:03 -07001714
bsalomon8acedde2016-06-24 10:42:16 -07001715 if (!pr) {
robertphillipsea461502015-05-26 11:38:03 -07001716#ifdef SK_DEBUG
1717 SkDebugf("Unable to find path renderer compatible with path.\n");
1718#endif
1719 return;
1720 }
1721
Robert Phillips256c37b2017-03-01 14:32:46 -05001722 GrPathRenderer::DrawPathArgs args{this->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05001723 std::move(paint),
1724 &GrUserStencilSettings::kUnused,
1725 this,
1726 &clip,
1727 &viewMatrix,
1728 &shape,
1729 aaType,
1730 this->isGammaCorrect()};
bsalomon0aff2fa2015-07-31 06:48:27 -07001731 pr->drawPath(args);
robertphillipsea461502015-05-26 11:38:03 -07001732}
1733
Brian Salomon467921e2017-03-06 16:17:12 -05001734static void op_bounds(SkRect* bounds, const GrOp* op) {
1735 *bounds = op->bounds();
1736 if (op->hasZeroArea()) {
1737 if (op->hasAABloat()) {
1738 bounds->outset(0.5f, 0.5f);
1739 } else {
1740 // We don't know which way the particular GPU will snap lines or points at integer
1741 // coords. So we ensure that the bounds is large enough for either snap.
1742 SkRect before = *bounds;
1743 bounds->roundOut(bounds);
1744 if (bounds->fLeft == before.fLeft) {
1745 bounds->fLeft -= 1;
1746 }
1747 if (bounds->fTop == before.fTop) {
1748 bounds->fTop -= 1;
1749 }
1750 if (bounds->fRight == before.fRight) {
1751 bounds->fRight += 1;
1752 }
1753 if (bounds->fBottom == before.fBottom) {
1754 bounds->fBottom += 1;
1755 }
1756 }
1757 }
1758}
1759
Brian Salomon54d212e2017-03-21 14:22:38 -04001760uint32_t GrRenderTargetContext::addDrawOp(const GrClip& clip, std::unique_ptr<GrDrawOp> op) {
joshualitt1de610a2016-01-06 08:26:09 -08001761 ASSERT_SINGLE_OWNER
Robert Phillipsc0138922017-03-08 11:50:55 -05001762 if (this->drawingManager()->wasAbandoned()) {
1763 return SK_InvalidUniqueID;
1764 }
robertphillips2e1e51f2015-10-15 08:01:48 -07001765 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001766 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "addDrawOp", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -07001767
Brian Salomon467921e2017-03-06 16:17:12 -05001768 // Setup clip
1769 SkRect bounds;
1770 op_bounds(&bounds, op.get());
Brian Salomon97180af2017-03-14 13:42:58 -04001771 GrAppliedClip appliedClip;
Brian Salomon54d212e2017-03-21 14:22:38 -04001772 GrDrawOp::FixedFunctionFlags fixedFunctionFlags = op->fixedFunctionFlags();
1773 if (!clip.apply(fContext, this, fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesHWAA,
1774 fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil, &appliedClip,
1775 &bounds)) {
1776 return SK_InvalidUniqueID;
1777 }
1778
Brian Salomon54d212e2017-03-21 14:22:38 -04001779 if (fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil ||
1780 appliedClip.hasStencilClip()) {
Robert Phillips5efd5ea2017-05-30 13:47:32 -04001781 // This forces instantiation of the render target.
1782 GrRenderTarget* rt = this->accessRenderTarget();
1783 if (!rt) {
1784 return SK_InvalidUniqueID;
1785 }
1786
Brian Salomon54d212e2017-03-21 14:22:38 -04001787 if (!fContext->resourceProvider()->attachStencilAttachment(rt)) {
1788 SkDebugf("ERROR creating stencil attachment. Draw skipped.\n");
1789 return SK_InvalidUniqueID;
1790 }
1791 }
1792
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001793 GrXferProcessor::DstProxy dstProxy;
Brian Salomonf86d37b2017-06-16 10:04:34 -04001794 if (op->finalize(*this->caps(), &appliedClip) == GrDrawOp::RequiresDstTexture::kYes) {
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001795 if (!this->setupDstProxy(this->asRenderTargetProxy(), clip, op->bounds(), &dstProxy)) {
Brian Salomon54d212e2017-03-21 14:22:38 -04001796 return SK_InvalidUniqueID;
1797 }
1798 }
1799
1800 op->setClippedBounds(bounds);
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001801 return this->getRTOpList()->addOp(std::move(op), *this->caps(),
1802 std::move(appliedClip), dstProxy);
Brian Salomon54d212e2017-03-21 14:22:38 -04001803}
1804
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001805bool GrRenderTargetContext::setupDstProxy(GrRenderTargetProxy* rtProxy, const GrClip& clip,
Robert Phillips16d8ec62017-07-27 16:16:25 -04001806 const SkRect& opBounds,
1807 GrXferProcessor::DstProxy* dstProxy) {
Brian Salomon467921e2017-03-06 16:17:12 -05001808 if (this->caps()->textureBarrierSupport()) {
Robert Phillipsbf25d432017-04-07 10:08:53 -04001809 if (GrTextureProxy* texProxy = rtProxy->asTextureProxy()) {
Brian Salomon467921e2017-03-06 16:17:12 -05001810 // The render target is a texture, so we can read from it directly in the shader. The XP
1811 // will be responsible to detect this situation and request a texture barrier.
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001812 dstProxy->setProxy(sk_ref_sp(texProxy));
1813 dstProxy->setOffset(0, 0);
Robert Phillipsbf25d432017-04-07 10:08:53 -04001814 return true;
Brian Salomon467921e2017-03-06 16:17:12 -05001815 }
1816 }
1817
Robert Phillipsbf25d432017-04-07 10:08:53 -04001818 SkIRect copyRect = SkIRect::MakeWH(rtProxy->width(), rtProxy->height());
Brian Salomon467921e2017-03-06 16:17:12 -05001819
Eric Karl74480882017-04-03 14:49:05 -07001820 SkIRect clippedRect;
Robert Phillipsbf25d432017-04-07 10:08:53 -04001821 clip.getConservativeBounds(rtProxy->width(), rtProxy->height(), &clippedRect);
Eric Karl72e551e2017-04-04 13:42:10 -07001822 SkIRect drawIBounds;
Brian Salomon467921e2017-03-06 16:17:12 -05001823 opBounds.roundOut(&drawIBounds);
Brian Salomon859621f2017-03-16 09:21:54 -04001824 // Cover up for any precision issues by outsetting the op bounds a pixel in each direction.
1825 drawIBounds.outset(1, 1);
Eric Karl72e551e2017-04-04 13:42:10 -07001826 if (!clippedRect.intersect(drawIBounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -05001827#ifdef SK_DEBUG
Robert Phillipsbf25d432017-04-07 10:08:53 -04001828 GrCapsDebugf(this->caps(), "setupDstTexture: Missed an early reject bailing on draw.");
Brian Salomon467921e2017-03-06 16:17:12 -05001829#endif
Robert Phillipsbf25d432017-04-07 10:08:53 -04001830 return false;
Brian Salomon467921e2017-03-06 16:17:12 -05001831 }
1832
1833 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
1834 // have per-sample dst values by making the copy multisampled.
1835 GrSurfaceDesc desc;
Eric Karl74480882017-04-03 14:49:05 -07001836 bool rectsMustMatch = false;
1837 bool disallowSubrect = false;
Robert Phillipsbf25d432017-04-07 10:08:53 -04001838 if (!this->caps()->initDescForDstCopy(rtProxy, &desc, &rectsMustMatch, &disallowSubrect)) {
Brian Salomon467921e2017-03-06 16:17:12 -05001839 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillips16d8ec62017-07-27 16:16:25 -04001840 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
Robert Phillipsbf25d432017-04-07 10:08:53 -04001841 desc.fConfig = rtProxy->config();
Brian Salomon467921e2017-03-06 16:17:12 -05001842 }
1843
Eric Karl74480882017-04-03 14:49:05 -07001844 if (!disallowSubrect) {
1845 copyRect = clippedRect;
1846 }
Brian Salomon467921e2017-03-06 16:17:12 -05001847
Robert Phillipsbf25d432017-04-07 10:08:53 -04001848 SkIPoint dstPoint, dstOffset;
1849 SkBackingFit fit;
Eric Karl74480882017-04-03 14:49:05 -07001850 if (rectsMustMatch) {
Robert Phillipsbf25d432017-04-07 10:08:53 -04001851 SkASSERT(desc.fOrigin == rtProxy->origin());
1852 desc.fWidth = rtProxy->width();
1853 desc.fHeight = rtProxy->height();
Eric Karl74480882017-04-03 14:49:05 -07001854 dstPoint = {copyRect.fLeft, copyRect.fTop};
1855 dstOffset = {0, 0};
Robert Phillipsbf25d432017-04-07 10:08:53 -04001856 fit = SkBackingFit::kExact;
Eric Karl74480882017-04-03 14:49:05 -07001857 } else {
1858 desc.fWidth = copyRect.width();
1859 desc.fHeight = copyRect.height();
1860 dstPoint = {0, 0};
1861 dstOffset = {copyRect.fLeft, copyRect.fTop};
Robert Phillipsbf25d432017-04-07 10:08:53 -04001862 fit = SkBackingFit::kApprox;
Eric Karl74480882017-04-03 14:49:05 -07001863 }
Brian Salomon467921e2017-03-06 16:17:12 -05001864
Robert Phillipsbf25d432017-04-07 10:08:53 -04001865 sk_sp<GrSurfaceContext> sContext = fContext->contextPriv().makeDeferredSurfaceContext(
1866 desc,
1867 fit,
1868 SkBudgeted::kYes);
1869 if (!sContext) {
1870 SkDebugf("setupDstTexture: surfaceContext creation failed.\n");
1871 return false;
1872 }
1873
1874 if (!sContext->copy(rtProxy, copyRect, dstPoint)) {
1875 SkDebugf("setupDstTexture: copy failed.\n");
1876 return false;
1877 }
1878
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001879 dstProxy->setProxy(sContext->asTextureProxyRef());
1880 dstProxy->setOffset(dstOffset);
Robert Phillipsbf25d432017-04-07 10:08:53 -04001881 return true;
robertphillips2334fb62015-06-17 05:43:33 -07001882}