blob: 48cea29d71bcb287784b04e5e1f8ce78d2ec35a6 [file] [log] [blame]
robertphillipsea461502015-05-26 11:38:03 -07001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Brian Osman11052242016-10-27 14:47:55 -04008#include "GrRenderTargetContext.h"
Brian Salomon6d4b65e2017-05-03 17:06:09 -04009#include "../private/GrAuditTrail.h"
Jim Van Verth3af1af92017-05-18 15:06:54 -040010#include "../private/SkShadowFlags.h"
Brian Salomon467921e2017-03-06 16:17:12 -050011#include "GrAppliedClip.h"
Greg Daniela5cb7812017-06-16 09:45:32 -040012#include "GrBackendSemaphore.h"
Brian Salomon5ec9def2016-12-20 15:34:05 -050013#include "GrColor.h"
Robert Phillipse78b7252017-04-06 07:59:41 -040014#include "GrContextPriv.h"
robertphillips77a2e522015-10-17 07:43:27 -070015#include "GrDrawingManager.h"
csmartdalton02fa32c2016-08-19 13:29:27 -070016#include "GrFixedClip.h"
robertphillips714712b2016-08-04 06:20:45 -070017#include "GrGpuResourcePriv.h"
robertphillipsea461502015-05-26 11:38:03 -070018#include "GrPathRenderer.h"
robertphillips2334fb62015-06-17 05:43:33 -070019#include "GrRenderTarget.h"
Brian Salomon5ec9def2016-12-20 15:34:05 -050020#include "GrRenderTargetContextPriv.h"
bsalomon473addf2015-10-02 07:49:05 -070021#include "GrResourceProvider.h"
Brian Salomon467921e2017-03-06 16:17:12 -050022#include "GrStencilAttachment.h"
Brian Salomondcbb9d92017-07-19 10:53:20 -040023#include "GrTracing.h"
Jim Van Verth1af03d42017-07-31 09:34:58 -040024#include "SkDrawShadowInfo.h"
Brian Salomon467921e2017-03-06 16:17:12 -050025#include "SkLatticeIter.h"
26#include "SkMatrixPriv.h"
Jim Van Verth34d6e4b2017-06-09 11:09:03 -040027#include "SkShadowUtils.h"
robertphillips2d70dcb2015-10-06 07:38:23 -070028#include "SkSurfacePriv.h"
Brian Salomon467921e2017-03-06 16:17:12 -050029#include "effects/GrRRectEffect.h"
30#include "instanced/InstancedRendering.h"
Brian Salomon89527432016-12-16 09:52:16 -050031#include "ops/GrClearOp.h"
Robert Phillipsb9a02a12017-04-06 11:08:40 -040032#include "ops/GrClearStencilClipOp.h"
Robert Phillips65a88fa2017-08-08 08:36:22 -040033#include "ops/GrDebugMarkerOp.h"
Brian Salomon0f353322017-05-03 20:58:59 +000034#include "ops/GrDrawAtlasOp.h"
Brian Salomon6d4b65e2017-05-03 17:06:09 -040035#include "ops/GrDrawOp.h"
Brian Salomon89527432016-12-16 09:52:16 -050036#include "ops/GrDrawVerticesOp.h"
37#include "ops/GrLatticeOp.h"
38#include "ops/GrOp.h"
39#include "ops/GrOvalOpFactory.h"
40#include "ops/GrRectOpFactory.h"
41#include "ops/GrRegionOp.h"
Greg Daniela5cb7812017-06-16 09:45:32 -040042#include "ops/GrSemaphoreOp.h"
Brian Salomon89527432016-12-16 09:52:16 -050043#include "ops/GrShadowRRectOp.h"
Brian Salomon467921e2017-03-06 16:17:12 -050044#include "ops/GrStencilPathOp.h"
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();)
Robert Phillipsb681a0f2017-08-09 13:31:36 -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 Phillipsb681a0f2017-08-09 13:31:36 -0400222 // Discard calls to in-progress opLists are ignored. Calls at the start update the
223 // opLists' color & stencil load ops.
224 if (this->getRTOpList()->isEmpty()) {
225 if (this->caps()->discardRenderTargetSupport()) {
226 this->getRTOpList()->setColorLoadOp(GrLoadOp::kDiscard);
227 this->getRTOpList()->setStencilLoadOp(GrLoadOp::kDiscard);
228 } else {
229 // Surely, if a discard was requested, a clear should be acceptable
230 this->getRTOpList()->setColorLoadOp(GrLoadOp::kClear);
231 this->getRTOpList()->setLoadClearColor(0x0);
232 this->getRTOpList()->setStencilLoadOp(GrLoadOp::kClear);
Robert Phillipse4407212017-04-13 10:13:16 -0400233 }
Robert Phillipse4407212017-04-13 10:13:16 -0400234 }
robertphillipsea461502015-05-26 11:38:03 -0700235}
236
Brian Osman11052242016-10-27 14:47:55 -0400237void GrRenderTargetContext::clear(const SkIRect* rect,
238 const GrColor color,
239 bool canIgnoreRect) {
joshualitt1de610a2016-01-06 08:26:09 -0800240 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700241 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700242 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400243 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "clear", fContext);
robertphillipsea461502015-05-26 11:38:03 -0700244
Robert Phillips72152832017-01-25 17:31:35 -0500245 AutoCheckFlush acf(this->drawingManager());
csmartdalton29df7602016-08-31 11:55:52 -0700246 this->internalClear(rect ? GrFixedClip(*rect) : GrFixedClip::Disabled(), color, canIgnoreRect);
247}
robertphillips9199a9f2016-07-13 07:48:43 -0700248
Robert Phillips784b7bf2016-12-09 13:35:02 -0500249void GrRenderTargetContextPriv::absClear(const SkIRect* clearRect, const GrColor color) {
250 ASSERT_SINGLE_OWNER_PRIV
251 RETURN_IF_ABANDONED_PRIV
252 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400253 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "absClear",
254 fRenderTargetContext->fContext);
Robert Phillips784b7bf2016-12-09 13:35:02 -0500255
Robert Phillips72152832017-01-25 17:31:35 -0500256 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500257
Brian Salomonbb5711a2017-05-17 13:49:59 -0400258 SkIRect rtRect = SkIRect::MakeWH(fRenderTargetContext->fRenderTargetProxy->worstCaseWidth(),
259 fRenderTargetContext->fRenderTargetProxy->worstCaseHeight());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500260
261 if (clearRect) {
262 if (clearRect->contains(rtRect)) {
263 clearRect = nullptr; // full screen
264 } else {
265 if (!rtRect.intersect(*clearRect)) {
266 return;
267 }
268 }
269 }
270
271 // TODO: in a post-MDB world this should be handled at the OpList level.
272 // An op-list that is initially cleared and has no other ops should receive an
273 // extra draw.
274 if (fRenderTargetContext->fContext->caps()->useDrawInsteadOfClear()) {
275 // This works around a driver bug with clear by drawing a rect instead.
276 // The driver will ignore a clear if it is the only thing rendered to a
277 // target before the target is read.
278 GrPaint paint;
279 paint.setColor4f(GrColor4f::FromGrColor(color));
Brian Salomona1633922017-01-09 11:46:10 -0500280 paint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
Robert Phillips784b7bf2016-12-09 13:35:02 -0500281
282 // We don't call drawRect() here to avoid the cropping to the, possibly smaller,
283 // RenderTargetProxy bounds
Brian Salomonbaaf4392017-06-15 09:59:23 -0400284 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeNonAAFill(
285 std::move(paint), SkMatrix::I(), SkRect::Make(rtRect), GrAAType::kNone);
286 fRenderTargetContext->addDrawOp(GrNoClip(), std::move(op));
Robert Phillips784b7bf2016-12-09 13:35:02 -0500287 } else {
Robert Phillips784b7bf2016-12-09 13:35:02 -0500288 // This path doesn't handle coalescing of full screen clears b.c. it
289 // has to clear the entire render target - not just the content area.
290 // It could be done but will take more finagling.
Robert Phillips5efd5ea2017-05-30 13:47:32 -0400291 std::unique_ptr<GrOp> op(GrClearOp::Make(rtRect, color, !clearRect));
Brian Salomonfc527d22016-12-14 21:07:01 -0500292 if (!op) {
Robert Phillips784b7bf2016-12-09 13:35:02 -0500293 return;
294 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400295 fRenderTargetContext->getRTOpList()->addOp(std::move(op), *fRenderTargetContext->caps());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500296 }
297}
298
Brian Osman11052242016-10-27 14:47:55 -0400299void GrRenderTargetContextPriv::clear(const GrFixedClip& clip,
300 const GrColor color,
301 bool canIgnoreClip) {
csmartdalton29df7602016-08-31 11:55:52 -0700302 ASSERT_SINGLE_OWNER_PRIV
303 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400304 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400305 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "clear",
306 fRenderTargetContext->fContext);
csmartdalton29df7602016-08-31 11:55:52 -0700307
Robert Phillips72152832017-01-25 17:31:35 -0500308 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Brian Osman11052242016-10-27 14:47:55 -0400309 fRenderTargetContext->internalClear(clip, color, canIgnoreClip);
csmartdalton29df7602016-08-31 11:55:52 -0700310}
311
Brian Osman11052242016-10-27 14:47:55 -0400312void GrRenderTargetContext::internalClear(const GrFixedClip& clip,
313 const GrColor color,
314 bool canIgnoreClip) {
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700315 bool isFull = false;
316 if (!clip.hasWindowRectangles()) {
317 isFull = !clip.scissorEnabled() ||
318 (canIgnoreClip && fContext->caps()->fullClearIsFree()) ||
319 clip.scissorRect().contains(SkIRect::MakeWH(this->width(), this->height()));
320 }
robertphillips9199a9f2016-07-13 07:48:43 -0700321
322 if (fContext->caps()->useDrawInsteadOfClear()) {
323 // This works around a driver bug with clear by drawing a rect instead.
324 // The driver will ignore a clear if it is the only thing rendered to a
325 // target before the target is read.
Robert Phillips784b7bf2016-12-09 13:35:02 -0500326 SkIRect clearRect = SkIRect::MakeWH(this->width(), this->height());
csmartdalton29df7602016-08-31 11:55:52 -0700327 if (isFull) {
robertphillips9199a9f2016-07-13 07:48:43 -0700328 this->discard();
Robert Phillips93f16332016-11-23 19:37:13 -0500329 } else if (!clearRect.intersect(clip.scissorRect())) {
csmartdalton29df7602016-08-31 11:55:52 -0700330 return;
robertphillips9199a9f2016-07-13 07:48:43 -0700331 }
332
333 GrPaint paint;
334 paint.setColor4f(GrColor4f::FromGrColor(color));
Brian Salomona1633922017-01-09 11:46:10 -0500335 paint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
robertphillips9199a9f2016-07-13 07:48:43 -0700336
Brian Salomon82f44312017-01-11 13:42:54 -0500337 this->drawRect(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), SkRect::Make(clearRect));
bsalomon9f129de2016-08-10 16:31:05 -0700338 } else if (isFull) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400339 this->getRTOpList()->fullClear(*this->caps(), color);
robertphillips9199a9f2016-07-13 07:48:43 -0700340 } else {
Robert Phillips5efd5ea2017-05-30 13:47:32 -0400341 std::unique_ptr<GrOp> op(GrClearOp::Make(clip, color, this->asSurfaceProxy()));
Brian Salomon42521e82016-12-07 16:44:58 -0500342 if (!op) {
csmartdalton29df7602016-08-31 11:55:52 -0700343 return;
344 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400345 this->getRTOpList()->addOp(std::move(op), *this->caps());
robertphillips9199a9f2016-07-13 07:48:43 -0700346 }
robertphillipsea461502015-05-26 11:38:03 -0700347}
348
Brian Osman11052242016-10-27 14:47:55 -0400349void GrRenderTargetContext::drawPaint(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500350 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400351 const SkMatrix& viewMatrix) {
joshualitt1de610a2016-01-06 08:26:09 -0800352 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700353 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700354 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400355 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawPaint", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700356
robertphillipsea461502015-05-26 11:38:03 -0700357 // set rect to be big enough to fill the space, but not super-huge, so we
358 // don't overflow fixed-point implementations
robertphillips13a7eee2016-08-31 15:06:24 -0700359
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400360 SkRect r = fRenderTargetProxy->getBoundsRect();
robertphillipsea461502015-05-26 11:38:03 -0700361
bsalomoncb31e512016-08-26 10:48:19 -0700362 SkRRect rrect;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500363 GrAA aa;
bsalomoncb31e512016-08-26 10:48:19 -0700364 // Check if we can replace a clipRRect()/drawPaint() with a drawRRect(). We only do the
365 // transformation for non-rect rrects. Rects caused a performance regression on an Android
366 // test that needs investigation. We also skip cases where there are fragment processors
367 // because they may depend on having correct local coords and this path draws in device space
368 // without a local matrix.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500369 if (!paint.numTotalFragmentProcessors() && clip.isRRect(r, &rrect, &aa) && !rrect.isRect()) {
Brian Salomon82f44312017-01-11 13:42:54 -0500370 this->drawRRect(GrNoClip(), std::move(paint), aa, SkMatrix::I(), rrect,
371 GrStyle::SimpleFill());
bsalomoncb31e512016-08-26 10:48:19 -0700372 return;
373 }
374
robertphillipsea461502015-05-26 11:38:03 -0700375
376 bool isPerspective = viewMatrix.hasPerspective();
377
378 // We attempt to map r by the inverse matrix and draw that. mapRect will
379 // map the four corners and bound them with a new rect. This will not
380 // produce a correct result for some perspective matrices.
381 if (!isPerspective) {
reeda39667c2016-08-22 06:39:49 -0700382 if (!SkMatrixPriv::InverseMapRect(viewMatrix, &r, r)) {
robertphillipsea461502015-05-26 11:38:03 -0700383 SkDebugf("Could not invert matrix\n");
384 return;
385 }
Brian Salomon82f44312017-01-11 13:42:54 -0500386 this->drawRect(clip, std::move(paint), GrAA::kNo, viewMatrix, r);
robertphillipsea461502015-05-26 11:38:03 -0700387 } else {
388 SkMatrix localMatrix;
389 if (!viewMatrix.invert(&localMatrix)) {
390 SkDebugf("Could not invert matrix\n");
391 return;
392 }
393
Robert Phillips72152832017-01-25 17:31:35 -0500394 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700395
Brian Salomonbaaf4392017-06-15 09:59:23 -0400396 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeNonAAFillWithLocalMatrix(
397 std::move(paint), SkMatrix::I(), localMatrix, r, GrAAType::kNone);
398 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -0700399 }
400}
401
robertphillipsea461502015-05-26 11:38:03 -0700402static inline bool rect_contains_inclusive(const SkRect& rect, const SkPoint& point) {
403 return point.fX >= rect.fLeft && point.fX <= rect.fRight &&
404 point.fY >= rect.fTop && point.fY <= rect.fBottom;
405}
406
csmartdalton97f6cd52016-07-13 13:37:08 -0700407// Attempts to crop a rect and optional local rect to the clip boundaries.
408// Returns false if the draw can be skipped entirely.
robertphillips13a7eee2016-08-31 15:06:24 -0700409static bool crop_filled_rect(int width, int height, const GrClip& clip,
csmartdalton97f6cd52016-07-13 13:37:08 -0700410 const SkMatrix& viewMatrix, SkRect* rect,
411 SkRect* localRect = nullptr) {
412 if (!viewMatrix.rectStaysRect()) {
413 return true;
414 }
415
csmartdalton97f6cd52016-07-13 13:37:08 -0700416 SkIRect clipDevBounds;
417 SkRect clipBounds;
csmartdalton97f6cd52016-07-13 13:37:08 -0700418
robertphillips13a7eee2016-08-31 15:06:24 -0700419 clip.getConservativeBounds(width, height, &clipDevBounds);
reeda39667c2016-08-22 06:39:49 -0700420 if (!SkMatrixPriv::InverseMapRect(viewMatrix, &clipBounds, SkRect::Make(clipDevBounds))) {
421 return false;
422 }
csmartdalton97f6cd52016-07-13 13:37:08 -0700423
424 if (localRect) {
425 if (!rect->intersects(clipBounds)) {
426 return false;
427 }
428 const SkScalar dx = localRect->width() / rect->width();
429 const SkScalar dy = localRect->height() / rect->height();
430 if (clipBounds.fLeft > rect->fLeft) {
431 localRect->fLeft += (clipBounds.fLeft - rect->fLeft) * dx;
432 rect->fLeft = clipBounds.fLeft;
433 }
434 if (clipBounds.fTop > rect->fTop) {
435 localRect->fTop += (clipBounds.fTop - rect->fTop) * dy;
436 rect->fTop = clipBounds.fTop;
437 }
438 if (clipBounds.fRight < rect->fRight) {
439 localRect->fRight -= (rect->fRight - clipBounds.fRight) * dx;
440 rect->fRight = clipBounds.fRight;
441 }
442 if (clipBounds.fBottom < rect->fBottom) {
443 localRect->fBottom -= (rect->fBottom - clipBounds.fBottom) * dy;
444 rect->fBottom = clipBounds.fBottom;
445 }
446 return true;
447 }
448
449 return rect->intersect(clipBounds);
450}
451
Brian Osman11052242016-10-27 14:47:55 -0400452bool GrRenderTargetContext::drawFilledRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500453 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500454 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400455 const SkMatrix& viewMatrix,
456 const SkRect& rect,
457 const GrUserStencilSettings* ss) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700458 SkRect croppedRect = rect;
Robert Phillips784b7bf2016-12-09 13:35:02 -0500459 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix, &croppedRect)) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700460 return true;
461 }
robertphillips44302392016-07-08 14:43:03 -0700462
Brian Salomon54d212e2017-03-21 14:22:38 -0400463 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport() &&
464 (!ss || ss->isDisabled(false))) {
Robert Phillipse3302df2017-04-24 07:31:02 -0400465 gr_instanced::OpAllocator* oa = this->drawingManager()->instancingAllocator();
466 std::unique_ptr<GrDrawOp> op = oa->recordRect(croppedRect, viewMatrix, std::move(paint),
467 aa, fInstancedPipelineInfo);
Brian Salomon42521e82016-12-07 16:44:58 -0500468 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -0400469 this->addDrawOp(clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700470 return true;
csmartdaltona7f29642016-07-07 08:49:11 -0700471 }
472 }
Brian Salomon7c8460e2017-05-12 11:36:10 -0400473 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400474 std::unique_ptr<GrDrawOp> op;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500475 if (GrAAType::kCoverage == aaType) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400476 op = GrRectOpFactory::MakeAAFill(std::move(paint), viewMatrix, croppedRect, ss);
robertphillips391395d2016-03-02 09:26:36 -0800477 } else {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400478 op = GrRectOpFactory::MakeNonAAFill(std::move(paint), viewMatrix, croppedRect, aaType, ss);
robertphillips391395d2016-03-02 09:26:36 -0800479 }
Brian Salomonbaaf4392017-06-15 09:59:23 -0400480 if (!op) {
481 return false;
482 }
483 this->addDrawOp(clip, std::move(op));
484 return true;
robertphillips391395d2016-03-02 09:26:36 -0800485}
486
Brian Osman11052242016-10-27 14:47:55 -0400487void GrRenderTargetContext::drawRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500488 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500489 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400490 const SkMatrix& viewMatrix,
491 const SkRect& rect,
492 const GrStyle* style) {
bsalomon6663acf2016-05-10 09:14:17 -0700493 if (!style) {
494 style = &GrStyle::SimpleFill();
495 }
joshualitt1de610a2016-01-06 08:26:09 -0800496 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700497 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700498 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400499 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRect", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700500
bsalomon6663acf2016-05-10 09:14:17 -0700501 // Path effects should've been devolved to a path in SkGpuDevice
502 SkASSERT(!style->pathEffect());
robertphillipsea461502015-05-26 11:38:03 -0700503
Robert Phillips72152832017-01-25 17:31:35 -0500504 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700505
bsalomon6663acf2016-05-10 09:14:17 -0700506 const SkStrokeRec& stroke = style->strokeRec();
robertphillips3ab14ca2016-07-10 11:49:39 -0700507 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
Greg Daniela5cb7812017-06-16 09:45:32 -0400508
robertphillips3ab14ca2016-07-10 11:49:39 -0700509 if (!fContext->caps()->useDrawInsteadOfClear()) {
510 // Check if this is a full RT draw and can be replaced with a clear. We don't bother
511 // checking cases where the RT is fully inside a stroke.
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400512 SkRect rtRect = fRenderTargetProxy->getBoundsRect();
robertphillips3ab14ca2016-07-10 11:49:39 -0700513 // Does the clip contain the entire RT?
514 if (clip.quickContains(rtRect)) {
515 SkMatrix invM;
516 if (!viewMatrix.invert(&invM)) {
robertphillipsea461502015-05-26 11:38:03 -0700517 return;
518 }
robertphillips3ab14ca2016-07-10 11:49:39 -0700519 // Does the rect bound the RT?
520 SkPoint srcSpaceRTQuad[4];
521 invM.mapRectToQuad(srcSpaceRTQuad, rtRect);
522 if (rect_contains_inclusive(rect, srcSpaceRTQuad[0]) &&
523 rect_contains_inclusive(rect, srcSpaceRTQuad[1]) &&
524 rect_contains_inclusive(rect, srcSpaceRTQuad[2]) &&
525 rect_contains_inclusive(rect, srcSpaceRTQuad[3])) {
526 // Will it blend?
527 GrColor clearColor;
528 if (paint.isConstantBlendedColor(&clearColor)) {
robertphillips9199a9f2016-07-13 07:48:43 -0700529 this->clear(nullptr, clearColor, true);
robertphillips3ab14ca2016-07-10 11:49:39 -0700530 return;
531 }
532 }
robertphillipsea461502015-05-26 11:38:03 -0700533 }
534 }
robertphillips44302392016-07-08 14:43:03 -0700535
Brian Salomon82f44312017-01-11 13:42:54 -0500536 if (this->drawFilledRect(clip, std::move(paint), aa, viewMatrix, rect, nullptr)) {
robertphillips44302392016-07-08 14:43:03 -0700537 return;
538 }
bsalomona7d85ba2016-07-06 11:54:59 -0700539 } else if (stroke.getStyle() == SkStrokeRec::kStroke_Style ||
540 stroke.getStyle() == SkStrokeRec::kHairline_Style) {
541 if ((!rect.width() || !rect.height()) &&
542 SkStrokeRec::kHairline_Style != stroke.getStyle()) {
543 SkScalar r = stroke.getWidth() / 2;
544 // TODO: Move these stroke->fill fallbacks to GrShape?
545 switch (stroke.getJoin()) {
546 case SkPaint::kMiter_Join:
Brian Salomon82f44312017-01-11 13:42:54 -0500547 this->drawRect(
548 clip, std::move(paint), aa, viewMatrix,
549 {rect.fLeft - r, rect.fTop - r, rect.fRight + r, rect.fBottom + r},
550 &GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700551 return;
552 case SkPaint::kRound_Join:
553 // Raster draws nothing when both dimensions are empty.
554 if (rect.width() || rect.height()){
555 SkRRect rrect = SkRRect::MakeRectXY(rect.makeOutset(r, r), r, r);
Brian Salomon82f44312017-01-11 13:42:54 -0500556 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect,
557 GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700558 return;
559 }
560 case SkPaint::kBevel_Join:
561 if (!rect.width()) {
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 - r, rect.fTop, rect.fRight + r, rect.fBottom},
564 &GrStyle::SimpleFill());
565 } else {
Brian Salomon82f44312017-01-11 13:42:54 -0500566 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700567 {rect.fLeft, rect.fTop - r, rect.fRight, rect.fBottom + r},
568 &GrStyle::SimpleFill());
569 }
570 return;
571 }
572 }
robertphillips44302392016-07-08 14:43:03 -0700573
Brian Salomonbaaf4392017-06-15 09:59:23 -0400574 std::unique_ptr<GrDrawOp> op;
robertphillips44302392016-07-08 14:43:03 -0700575
Brian Salomon7c8460e2017-05-12 11:36:10 -0400576 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500577 if (GrAAType::kCoverage == aaType) {
cdaltonbb539482016-01-04 09:48:25 -0800578 // The stroke path needs the rect to remain axis aligned (no rotation or skew).
579 if (viewMatrix.rectStaysRect()) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400580 op = GrRectOpFactory::MakeAAStroke(std::move(paint), viewMatrix, rect, stroke);
cdaltonbb539482016-01-04 09:48:25 -0800581 }
robertphillipsea461502015-05-26 11:38:03 -0700582 } else {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400583 op = GrRectOpFactory::MakeNonAAStroke(std::move(paint), viewMatrix, rect, stroke,
584 aaType);
robertphillips391395d2016-03-02 09:26:36 -0800585 }
robertphillips4bc31812016-03-01 12:22:49 -0800586
Brian Salomon42521e82016-12-07 16:44:58 -0500587 if (op) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400588 this->addDrawOp(clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700589 return;
robertphillips4bc31812016-03-01 12:22:49 -0800590 }
robertphillips4bc31812016-03-01 12:22:49 -0800591 }
halcanary9d524f22016-03-29 09:03:52 -0700592
robertphillips4bc31812016-03-01 12:22:49 -0800593 SkPath path;
594 path.setIsVolatile(true);
595 path.addRect(rect);
Brian Salomon82f44312017-01-11 13:42:54 -0500596 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, *style);
robertphillipsea461502015-05-26 11:38:03 -0700597}
598
Robert Phillipsec2249f2016-11-09 08:54:35 -0500599int GrRenderTargetContextPriv::maxWindowRectangles() const {
600 return fRenderTargetContext->fRenderTargetProxy->maxWindowRectangles(
601 *fRenderTargetContext->fContext->caps());
602}
603
Brian Osman11052242016-10-27 14:47:55 -0400604void GrRenderTargetContextPriv::clearStencilClip(const GrFixedClip& clip, bool insideStencilMask) {
robertphillips976f5f02016-06-03 10:59:20 -0700605 ASSERT_SINGLE_OWNER_PRIV
606 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400607 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400608 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "clearStencilClip",
609 fRenderTargetContext->fContext);
robertphillips976f5f02016-06-03 10:59:20 -0700610
Robert Phillips72152832017-01-25 17:31:35 -0500611 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400612
Robert Phillips2f4ddf62017-06-01 08:48:19 -0400613 std::unique_ptr<GrOp> op(GrClearStencilClipOp::Make(
614 clip, insideStencilMask,
615 fRenderTargetContext->fRenderTargetProxy.get()));
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400616 if (!op) {
Robert Phillipse60ad622016-11-17 10:22:48 -0500617 return;
618 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400619 fRenderTargetContext->getRTOpList()->addOp(std::move(op), *fRenderTargetContext->caps());
robertphillips976f5f02016-06-03 10:59:20 -0700620}
621
Brian Osman11052242016-10-27 14:47:55 -0400622void GrRenderTargetContextPriv::stencilPath(const GrClip& clip,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500623 GrAAType aaType,
Brian Osman11052242016-10-27 14:47:55 -0400624 const SkMatrix& viewMatrix,
625 const GrPath* path) {
Brian Salomon467921e2017-03-06 16:17:12 -0500626 ASSERT_SINGLE_OWNER_PRIV
627 RETURN_IF_ABANDONED_PRIV
628 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400629 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "stencilPath",
630 fRenderTargetContext->fContext);
Brian Salomon467921e2017-03-06 16:17:12 -0500631
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500632 SkASSERT(aaType != GrAAType::kCoverage);
Brian Salomon467921e2017-03-06 16:17:12 -0500633
634 bool useHWAA = GrAATypeIsHW(aaType);
635 // TODO: extract portions of checkDraw that are relevant to path stenciling.
636 SkASSERT(path);
637 SkASSERT(fRenderTargetContext->caps()->shaderCaps()->pathRenderingSupport());
638
639 // FIXME: Use path bounds instead of this WAR once
640 // https://bugs.chromium.org/p/skia/issues/detail?id=5640 is resolved.
641 SkRect bounds = SkRect::MakeIWH(fRenderTargetContext->width(), fRenderTargetContext->height());
642
643 // Setup clip
Brian Salomon97180af2017-03-14 13:42:58 -0400644 GrAppliedClip appliedClip;
Brian Salomon467921e2017-03-06 16:17:12 -0500645 if (!clip.apply(fRenderTargetContext->fContext, fRenderTargetContext, useHWAA, true,
Brian Salomon97180af2017-03-14 13:42:58 -0400646 &appliedClip, &bounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -0500647 return;
648 }
649
650 // Coverage AA does not make sense when rendering to the stencil buffer. The caller should never
651 // attempt this in a situation that would require coverage AA.
652 SkASSERT(!appliedClip.clipCoverageFragmentProcessor());
653
654 GrRenderTarget* rt = fRenderTargetContext->accessRenderTarget();
655 if (!rt) {
656 return;
657 }
658 GrStencilAttachment* stencilAttachment =
659 fRenderTargetContext->fContext->resourceProvider()->attachStencilAttachment(rt);
660 if (!stencilAttachment) {
661 SkDebugf("ERROR creating stencil attachment. Draw skipped.\n");
662 return;
663 }
664
665 std::unique_ptr<GrOp> op = GrStencilPathOp::Make(viewMatrix,
666 useHWAA,
667 path->getFillType(),
668 appliedClip.hasStencilClip(),
669 stencilAttachment->bits(),
670 appliedClip.scissorState(),
Brian Salomon467921e2017-03-06 16:17:12 -0500671 path);
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400672 if (!op) {
673 return;
674 }
Brian Salomon97180af2017-03-14 13:42:58 -0400675 op->setClippedBounds(bounds);
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400676 fRenderTargetContext->getRTOpList()->addOp(std::move(op), *fRenderTargetContext->caps());
robertphillips976f5f02016-06-03 10:59:20 -0700677}
678
Brian Osman11052242016-10-27 14:47:55 -0400679void GrRenderTargetContextPriv::stencilRect(const GrClip& clip,
680 const GrUserStencilSettings* ss,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500681 GrAAType aaType,
Brian Osman11052242016-10-27 14:47:55 -0400682 const SkMatrix& viewMatrix,
683 const SkRect& rect) {
robertphillips976f5f02016-06-03 10:59:20 -0700684 ASSERT_SINGLE_OWNER_PRIV
685 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400686 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400687 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "stencilRect",
688 fRenderTargetContext->fContext);
689
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500690 SkASSERT(GrAAType::kCoverage != aaType);
Robert Phillips72152832017-01-25 17:31:35 -0500691 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips976f5f02016-06-03 10:59:20 -0700692
693 GrPaint paint;
Brian Salomona1633922017-01-09 11:46:10 -0500694 paint.setXPFactory(GrDisableColorXPFactory::Get());
Brian Salomonbaaf4392017-06-15 09:59:23 -0400695 std::unique_ptr<GrDrawOp> op =
696 GrRectOpFactory::MakeNonAAFill(std::move(paint), viewMatrix, rect, aaType, ss);
697 fRenderTargetContext->addDrawOp(clip, std::move(op));
robertphillips976f5f02016-06-03 10:59:20 -0700698}
699
Brian Osman11052242016-10-27 14:47:55 -0400700bool GrRenderTargetContextPriv::drawAndStencilRect(const GrClip& clip,
701 const GrUserStencilSettings* ss,
702 SkRegion::Op op,
703 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500704 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400705 const SkMatrix& viewMatrix,
706 const SkRect& rect) {
robertphillips391395d2016-03-02 09:26:36 -0800707 ASSERT_SINGLE_OWNER_PRIV
708 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400709 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400710 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawAndStencilRect",
711 fRenderTargetContext->fContext);
robertphillips391395d2016-03-02 09:26:36 -0800712
Robert Phillips72152832017-01-25 17:31:35 -0500713 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -0800714
715 GrPaint paint;
robertphillips391395d2016-03-02 09:26:36 -0800716 paint.setCoverageSetOpXPFactory(op, invert);
717
Brian Salomon82f44312017-01-11 13:42:54 -0500718 if (fRenderTargetContext->drawFilledRect(clip, std::move(paint), aa, viewMatrix, rect, ss)) {
robertphillips391395d2016-03-02 09:26:36 -0800719 return true;
720 }
robertphillips391395d2016-03-02 09:26:36 -0800721 SkPath path;
722 path.setIsVolatile(true);
723 path.addRect(rect);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500724 return this->drawAndStencilPath(clip, ss, op, invert, aa, viewMatrix, path);
robertphillips391395d2016-03-02 09:26:36 -0800725}
726
Brian Osman11052242016-10-27 14:47:55 -0400727void GrRenderTargetContext::fillRectToRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500728 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500729 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400730 const SkMatrix& viewMatrix,
731 const SkRect& rectToDraw,
732 const SkRect& localRect) {
joshualitt1de610a2016-01-06 08:26:09 -0800733 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700734 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700735 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400736 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "fillRectToRect", fContext);
robertphillipsea461502015-05-26 11:38:03 -0700737
csmartdalton97f6cd52016-07-13 13:37:08 -0700738 SkRect croppedRect = rectToDraw;
739 SkRect croppedLocalRect = localRect;
robertphillips13a7eee2016-08-31 15:06:24 -0700740 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix,
741 &croppedRect, &croppedLocalRect)) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700742 return;
743 }
744
Robert Phillips72152832017-01-25 17:31:35 -0500745 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -0700746
csmartdaltone0d36292016-07-29 08:14:20 -0700747 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport()) {
Robert Phillipse3302df2017-04-24 07:31:02 -0400748 gr_instanced::OpAllocator* oa = this->drawingManager()->instancingAllocator();
749 std::unique_ptr<GrDrawOp> op(oa->recordRect(croppedRect, viewMatrix, std::move(paint),
Brian Salomon54d212e2017-03-21 14:22:38 -0400750 croppedLocalRect, aa, fInstancedPipelineInfo));
Brian Salomon42521e82016-12-07 16:44:58 -0500751 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -0400752 this->addDrawOp(clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -0700753 return;
754 }
755 }
756
Brian Salomon7c8460e2017-05-12 11:36:10 -0400757 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500758 if (GrAAType::kCoverage != aaType) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400759 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeNonAAFillWithLocalRect(
760 std::move(paint), viewMatrix, croppedRect, croppedLocalRect, aaType);
761 this->addDrawOp(clip, std::move(op));
csmartdaltonfc49d562016-07-26 17:05:47 -0700762 return;
joshualitt04194f32016-01-13 10:08:27 -0800763 }
bsalomonbb243832016-07-22 07:10:19 -0700764
Brian Salomonbaaf4392017-06-15 09:59:23 -0400765 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeAAFillWithLocalRect(
766 std::move(paint), viewMatrix, croppedRect, croppedLocalRect);
767 if (op) {
768 this->addDrawOp(clip, std::move(op));
csmartdaltonfc49d562016-07-26 17:05:47 -0700769 return;
770 }
771
772 SkMatrix viewAndUnLocalMatrix;
773 if (!viewAndUnLocalMatrix.setRectToRect(localRect, rectToDraw, SkMatrix::kFill_ScaleToFit)) {
774 SkDebugf("fillRectToRect called with empty local matrix.\n");
775 return;
776 }
777 viewAndUnLocalMatrix.postConcat(viewMatrix);
778
779 SkPath path;
780 path.setIsVolatile(true);
781 path.addRect(localRect);
Brian Salomon82f44312017-01-11 13:42:54 -0500782 this->internalDrawPath(clip, std::move(paint), aa, viewAndUnLocalMatrix, path, GrStyle());
joshualittb6b513b2015-08-21 10:25:18 -0700783}
784
Brian Osman11052242016-10-27 14:47:55 -0400785void GrRenderTargetContext::fillRectWithLocalMatrix(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500786 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500787 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400788 const SkMatrix& viewMatrix,
789 const SkRect& rectToDraw,
790 const SkMatrix& localMatrix) {
joshualitt1de610a2016-01-06 08:26:09 -0800791 ASSERT_SINGLE_OWNER
joshualittb6b513b2015-08-21 10:25:18 -0700792 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700793 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400794 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "fillRectWithLocalMatrix", fContext);
joshualittb6b513b2015-08-21 10:25:18 -0700795
csmartdalton97f6cd52016-07-13 13:37:08 -0700796 SkRect croppedRect = rectToDraw;
robertphillips13a7eee2016-08-31 15:06:24 -0700797 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix, &croppedRect)) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700798 return;
799 }
800
Robert Phillips72152832017-01-25 17:31:35 -0500801 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -0700802
csmartdaltone0d36292016-07-29 08:14:20 -0700803 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport()) {
Robert Phillipse3302df2017-04-24 07:31:02 -0400804 gr_instanced::OpAllocator* oa = this->drawingManager()->instancingAllocator();
805 std::unique_ptr<GrDrawOp> op(oa->recordRect(croppedRect, viewMatrix, std::move(paint),
Brian Salomon54d212e2017-03-21 14:22:38 -0400806 localMatrix, aa, fInstancedPipelineInfo));
Brian Salomon42521e82016-12-07 16:44:58 -0500807 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -0400808 this->addDrawOp(clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -0700809 return;
810 }
811 }
812
Brian Salomon7c8460e2017-05-12 11:36:10 -0400813 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500814 if (GrAAType::kCoverage != aaType) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400815 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeNonAAFillWithLocalMatrix(
816 std::move(paint), viewMatrix, localMatrix, croppedRect, aaType);
817 this->addDrawOp(clip, std::move(op));
csmartdaltonfc49d562016-07-26 17:05:47 -0700818 return;
bsalomonc55271f2015-11-09 11:55:57 -0800819 }
robertphillips4bc31812016-03-01 12:22:49 -0800820
Brian Salomonbaaf4392017-06-15 09:59:23 -0400821 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeAAFillWithLocalMatrix(
822 std::move(paint), viewMatrix, localMatrix, croppedRect);
823 if (op) {
824 this->addDrawOp(clip, std::move(op));
csmartdaltonfc49d562016-07-26 17:05:47 -0700825 return;
826 }
827
828 SkMatrix viewAndUnLocalMatrix;
829 if (!localMatrix.invert(&viewAndUnLocalMatrix)) {
830 SkDebugf("fillRectWithLocalMatrix called with degenerate local matrix.\n");
831 return;
832 }
833 viewAndUnLocalMatrix.postConcat(viewMatrix);
834
835 SkPath path;
836 path.setIsVolatile(true);
837 path.addRect(rectToDraw);
838 path.transform(localMatrix);
Brian Salomon82f44312017-01-11 13:42:54 -0500839 this->internalDrawPath(clip, std::move(paint), aa, viewAndUnLocalMatrix, path, GrStyle());
robertphillipsea461502015-05-26 11:38:03 -0700840}
841
Brian Osman11052242016-10-27 14:47:55 -0400842void GrRenderTargetContext::drawVertices(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500843 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400844 const SkMatrix& viewMatrix,
Brian Osmanae0c50c2017-05-25 16:56:34 -0400845 sk_sp<SkVertices> vertices,
846 GrPrimitiveType* overridePrimType) {
Brian Salomon199fb872017-02-06 09:41:10 -0500847 ASSERT_SINGLE_OWNER
848 RETURN_IF_ABANDONED
849 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400850 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawVertices", fContext);
Brian Salomon199fb872017-02-06 09:41:10 -0500851
852 AutoCheckFlush acf(this->drawingManager());
853
854 SkASSERT(vertices);
Brian Salomonc2f42542017-07-12 14:11:22 -0400855 GrAAType aaType = this->chooseAAType(GrAA::kNo, GrAllowMixedSamples::kNo);
856 std::unique_ptr<GrDrawOp> op =
857 GrDrawVerticesOp::Make(std::move(paint), std::move(vertices), viewMatrix, aaType,
858 this->isGammaCorrect(), fColorXformFromSRGB, overridePrimType);
859 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -0700860}
861
862///////////////////////////////////////////////////////////////////////////////
863
Brian Osman11052242016-10-27 14:47:55 -0400864void GrRenderTargetContext::drawAtlas(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500865 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400866 const SkMatrix& viewMatrix,
867 int spriteCount,
868 const SkRSXform xform[],
869 const SkRect texRect[],
870 const SkColor colors[]) {
joshualitt1de610a2016-01-06 08:26:09 -0800871 ASSERT_SINGLE_OWNER
jvanverth31ff7622015-08-07 10:09:28 -0700872 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700873 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400874 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawAtlas", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700875
Robert Phillips72152832017-01-25 17:31:35 -0500876 AutoCheckFlush acf(this->drawingManager());
halcanary9d524f22016-03-29 09:03:52 -0700877
Brian Salomon0088f942017-07-12 11:51:27 -0400878 GrAAType aaType = this->chooseAAType(GrAA::kNo, GrAllowMixedSamples::kNo);
879 std::unique_ptr<GrDrawOp> op = GrDrawAtlasOp::Make(std::move(paint), viewMatrix, aaType,
880 spriteCount, xform, texRect, colors);
881 this->addDrawOp(clip, std::move(op));
jvanverth31ff7622015-08-07 10:09:28 -0700882}
883
884///////////////////////////////////////////////////////////////////////////////
885
Brian Osman11052242016-10-27 14:47:55 -0400886void GrRenderTargetContext::drawRRect(const GrClip& origClip,
Brian Salomon82f44312017-01-11 13:42:54 -0500887 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500888 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400889 const SkMatrix& viewMatrix,
890 const SkRRect& rrect,
891 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -0800892 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700893 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700894 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400895 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRRect", fContext);
robertphillipsea461502015-05-26 11:38:03 -0700896 if (rrect.isEmpty()) {
897 return;
898 }
899
bsalomon7f0d9f32016-08-15 14:49:10 -0700900 GrNoClip noclip;
901 const GrClip* clip = &origClip;
902#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
903 // The Android framework frequently clips rrects to themselves where the clip is non-aa and the
Brian Salomon42521e82016-12-07 16:44:58 -0500904 // draw is aa. Since our lower level clip code works from op bounds, which are SkRects, it
bsalomon7f0d9f32016-08-15 14:49:10 -0700905 // doesn't detect that the clip can be ignored (modulo antialiasing). The following test
906 // attempts to mitigate the stencil clip cost but will only help when the entire clip stack
907 // can be ignored. We'd prefer to fix this in the framework by removing the clips calls.
908 SkRRect devRRect;
909 if (rrect.transform(viewMatrix, &devRRect) && clip->quickContains(devRRect)) {
910 clip = &noclip;
911 }
912#endif
bsalomon6663acf2016-05-10 09:14:17 -0700913 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
ksakamotoec7f2ac2016-07-05 03:54:53 -0700914
Robert Phillips72152832017-01-25 17:31:35 -0500915 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -0700916 const SkStrokeRec stroke = style.strokeRec();
csmartdaltona7f29642016-07-07 08:49:11 -0700917
csmartdaltone0d36292016-07-29 08:14:20 -0700918 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport() &&
919 stroke.isFillStyle()) {
Robert Phillipse3302df2017-04-24 07:31:02 -0400920 gr_instanced::OpAllocator* oa = this->drawingManager()->instancingAllocator();
Brian Salomon54d212e2017-03-21 14:22:38 -0400921 std::unique_ptr<GrDrawOp> op(
Robert Phillipse3302df2017-04-24 07:31:02 -0400922 oa->recordRRect(rrect, viewMatrix, std::move(paint), aa, fInstancedPipelineInfo));
Brian Salomon42521e82016-12-07 16:44:58 -0500923 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -0400924 this->addDrawOp(*clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -0700925 return;
926 }
927 }
928
Brian Salomon7c8460e2017-05-12 11:36:10 -0400929 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500930 if (GrAAType::kCoverage == aaType) {
Brian Salomon94efbf52016-11-29 13:43:05 -0500931 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomon05441c42017-05-15 16:45:49 -0400932 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeRRectOp(std::move(paint),
Brian Salomon05441c42017-05-15 16:45:49 -0400933 viewMatrix,
934 rrect,
935 stroke,
936 shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -0500937 if (op) {
Brian Salomon05441c42017-05-15 16:45:49 -0400938 this->addDrawOp(*clip, std::move(op));
robertphillipsb56f9272016-02-25 11:03:52 -0800939 return;
940 }
robertphillipsea461502015-05-26 11:38:03 -0700941 }
robertphillipsb56f9272016-02-25 11:03:52 -0800942
943 SkPath path;
944 path.setIsVolatile(true);
945 path.addRRect(rrect);
Brian Salomon82f44312017-01-11 13:42:54 -0500946 this->internalDrawPath(*clip, std::move(paint), aa, viewMatrix, path, style);
robertphillipsea461502015-05-26 11:38:03 -0700947}
948
Jim Van Verthc5903412016-11-17 15:27:09 -0500949///////////////////////////////////////////////////////////////////////////////
950
Jim Van Verth3af1af92017-05-18 15:06:54 -0400951static SkPoint3 map(const SkMatrix& m, const SkPoint3& pt) {
952 SkPoint3 result;
953 m.mapXY(pt.fX, pt.fY, (SkPoint*)&result.fX);
954 result.fZ = pt.fZ;
955 return result;
956}
957
958bool GrRenderTargetContext::drawFastShadow(const GrClip& clip,
Brian Salomon05969092017-07-13 11:20:51 -0400959 GrColor color4ub,
Jim Van Verth3af1af92017-05-18 15:06:54 -0400960 const SkMatrix& viewMatrix,
961 const SkPath& path,
962 const SkDrawShadowRec& rec) {
Jim Van Verthc5903412016-11-17 15:27:09 -0500963 ASSERT_SINGLE_OWNER
Jim Van Verth3af1af92017-05-18 15:06:54 -0400964 if (this->drawingManager()->wasAbandoned()) {
965 return true;
966 }
Jim Van Verthc5903412016-11-17 15:27:09 -0500967 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400968 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFastShadow", fContext);
Jim Van Verth3af1af92017-05-18 15:06:54 -0400969
970 // check z plane
971 bool tiltZPlane = SkToBool(!SkScalarNearlyZero(rec.fZPlaneParams.fX) ||
972 !SkScalarNearlyZero(rec.fZPlaneParams.fY));
973 bool skipAnalytic = SkToBool(rec.fFlags & SkShadowFlags::kGeometricOnly_ShadowFlag);
974 if (tiltZPlane || skipAnalytic || !viewMatrix.rectStaysRect() || !viewMatrix.isSimilarity()) {
975 return false;
976 }
977
978 SkRRect rrect;
979 SkRect rect;
980 // we can only handle rects, circles, and rrects with circular corners
981 bool isRRect = path.isRRect(&rrect) && rrect.isSimpleCircular() &&
982 rrect.radii(SkRRect::kUpperLeft_Corner).fX > SK_ScalarNearlyZero;
983 if (!isRRect &&
984 path.isOval(&rect) && SkScalarNearlyEqual(rect.width(), rect.height()) &&
985 rect.width() > SK_ScalarNearlyZero) {
986 rrect.setOval(rect);
987 isRRect = true;
988 }
989 if (!isRRect && path.isRect(&rect)) {
990 rrect.setRect(rect);
991 isRRect = true;
992 }
993
994 if (!isRRect) {
995 return false;
996 }
997
Jim Van Verthc5903412016-11-17 15:27:09 -0500998 if (rrect.isEmpty()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -0400999 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001000 }
1001
Robert Phillips72152832017-01-25 17:31:35 -05001002 AutoCheckFlush acf(this->drawingManager());
Jim Van Verthc5903412016-11-17 15:27:09 -05001003
Jim Van Verth3af1af92017-05-18 15:06:54 -04001004 // transform light
1005 SkPoint3 devLightPos = map(viewMatrix, rec.fLightPos);
1006
1007 // 1/scale
1008 SkScalar devToSrcScale = viewMatrix.isScaleTranslate() ?
1009 SkScalarInvert(viewMatrix[SkMatrix::kMScaleX]) :
1010 sk_float_rsqrt(viewMatrix[SkMatrix::kMScaleX] * viewMatrix[SkMatrix::kMScaleX] +
1011 viewMatrix[SkMatrix::kMSkewX] * viewMatrix[SkMatrix::kMSkewX]);
1012
1013 SkScalar occluderHeight = rec.fZPlaneParams.fZ;
Brian Salomon05969092017-07-13 11:20:51 -04001014 GrColor4f color = GrColor4f::FromGrColor(color4ub);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001015 bool transparent = SkToBool(rec.fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag);
Jim Van Verth34d6e4b2017-06-09 11:09:03 -04001016 bool tonalColor = SkToBool(rec.fFlags & SkShadowFlags::kTonalColor_ShadowFlag);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001017
1018 if (rec.fAmbientAlpha > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001019 SkScalar devSpaceInsetWidth = SkDrawShadowMetrics::AmbientBlurRadius(occluderHeight);
1020 const SkScalar umbraRecipAlpha = SkDrawShadowMetrics::AmbientRecipAlpha(occluderHeight);
1021 const SkScalar devSpaceAmbientBlur = devSpaceInsetWidth * umbraRecipAlpha;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001022
1023 // Outset the shadow rrect to the border of the penumbra
1024 SkScalar ambientPathOutset = devSpaceInsetWidth * devToSrcScale;
1025 SkRRect ambientRRect;
1026 SkRect outsetRect = rrect.rect().makeOutset(ambientPathOutset, ambientPathOutset);
1027 // If the rrect was an oval then its outset will also be one.
1028 // We set it explicitly to avoid errors.
1029 if (rrect.isOval()) {
1030 ambientRRect = SkRRect::MakeOval(outsetRect);
1031 } else {
1032 SkScalar outsetRad = rrect.getSimpleRadii().fX + ambientPathOutset;
1033 ambientRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1034 }
1035
Jim Van Verth34d6e4b2017-06-09 11:09:03 -04001036 GrColor ambientColor;
1037 if (tonalColor) {
1038 // with tonal color, the color only applies to the spot shadow
1039 ambientColor = GrColorPackRGBA(0, 0, 0, 255.999f*rec.fAmbientAlpha);
1040 } else {
1041 ambientColor = color.mulByScalar(rec.fAmbientAlpha).toGrColor();
1042 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001043 if (transparent) {
1044 // set a large inset to force a fill
1045 devSpaceInsetWidth = ambientRRect.width();
1046 }
1047 // the fraction of the blur we want to apply is devSpaceInsetWidth/devSpaceAmbientBlur,
Jim Van Verth1af03d42017-07-31 09:34:58 -04001048 // which is just 1/umbraRecipAlpha.
1049 SkScalar blurClamp = SkScalarInvert(umbraRecipAlpha);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001050
Brian Salomon05969092017-07-13 11:20:51 -04001051 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(ambientColor, viewMatrix,
1052 ambientRRect,
1053 devSpaceAmbientBlur,
1054 devSpaceInsetWidth,
1055 blurClamp);
1056 SkASSERT(op);
1057 this->addDrawOp(clip, std::move(op));
Jim Van Verthc5903412016-11-17 15:27:09 -05001058 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001059
1060 if (rec.fSpotAlpha > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001061 SkScalar devSpaceSpotBlur;
1062 SkScalar spotScale;
1063 SkVector spotOffset;
1064 SkDrawShadowMetrics::GetSpotParams(occluderHeight, devLightPos.fX, devLightPos.fY,
1065 devLightPos.fZ, rec.fLightRadius,
1066 &devSpaceSpotBlur, &spotScale, &spotOffset);
1067 // handle scale of radius due to CTM
Jim Van Verth3af1af92017-05-18 15:06:54 -04001068 const SkScalar srcSpaceSpotBlur = devSpaceSpotBlur * devToSrcScale;
1069
Jim Van Verth3af1af92017-05-18 15:06:54 -04001070 // Adjust translate for the effect of the scale.
1071 spotOffset.fX += spotScale*viewMatrix[SkMatrix::kMTransX];
1072 spotOffset.fY += spotScale*viewMatrix[SkMatrix::kMTransY];
1073 // This offset is in dev space, need to transform it into source space.
1074 SkMatrix ctmInverse;
1075 if (viewMatrix.invert(&ctmInverse)) {
1076 ctmInverse.mapPoints(&spotOffset, 1);
1077 } else {
1078 // Since the matrix is a similarity, this should never happen, but just in case...
1079 SkDebugf("Matrix is degenerate. Will not render spot shadow correctly!\n");
1080 SkASSERT(false);
1081 }
1082
1083 // Compute the transformed shadow rrect
1084 SkRRect spotShadowRRect;
1085 SkMatrix shadowTransform;
1086 shadowTransform.setScaleTranslate(spotScale, spotScale, spotOffset.fX, spotOffset.fY);
1087 rrect.transform(shadowTransform, &spotShadowRRect);
1088 SkScalar spotRadius = spotShadowRRect.getSimpleRadii().fX;
1089
1090 // Compute the insetWidth
Jim Van Verth1af03d42017-07-31 09:34:58 -04001091 SkScalar blurOutset = srcSpaceSpotBlur;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001092 SkScalar insetWidth = blurOutset;
1093 if (transparent) {
1094 // If transparent, just do a fill
1095 insetWidth += spotShadowRRect.width();
1096 } else {
1097 // For shadows, instead of using a stroke we specify an inset from the penumbra
1098 // border. We want to extend this inset area so that it meets up with the caster
1099 // geometry. The inset geometry will by default already be inset by the blur width.
1100 //
1101 // We compare the min and max corners inset by the radius between the original
1102 // rrect and the shadow rrect. The distance between the two plus the difference
1103 // between the scaled radius and the original radius gives the distance from the
1104 // transformed shadow shape to the original shape in that corner. The max
1105 // of these gives the maximum distance we need to cover.
1106 //
1107 // Since we are outsetting by 1/2 the blur distance, we just add the maxOffset to
1108 // that to get the full insetWidth.
1109 SkScalar maxOffset;
1110 if (rrect.isRect()) {
1111 // Manhattan distance works better for rects
1112 maxOffset = SkTMax(SkTMax(SkTAbs(spotShadowRRect.rect().fLeft -
1113 rrect.rect().fLeft),
1114 SkTAbs(spotShadowRRect.rect().fTop -
1115 rrect.rect().fTop)),
1116 SkTMax(SkTAbs(spotShadowRRect.rect().fRight -
1117 rrect.rect().fRight),
1118 SkTAbs(spotShadowRRect.rect().fBottom -
1119 rrect.rect().fBottom)));
1120 } else {
1121 SkScalar dr = spotRadius - rrect.getSimpleRadii().fX;
1122 SkPoint upperLeftOffset = SkPoint::Make(spotShadowRRect.rect().fLeft -
1123 rrect.rect().fLeft + dr,
1124 spotShadowRRect.rect().fTop -
1125 rrect.rect().fTop + dr);
1126 SkPoint lowerRightOffset = SkPoint::Make(spotShadowRRect.rect().fRight -
1127 rrect.rect().fRight - dr,
1128 spotShadowRRect.rect().fBottom -
1129 rrect.rect().fBottom - dr);
1130 maxOffset = SkScalarSqrt(SkTMax(upperLeftOffset.lengthSqd(),
1131 lowerRightOffset.lengthSqd())) + dr;
1132 }
1133 insetWidth += maxOffset;
1134 }
1135
1136 // Outset the shadow rrect to the border of the penumbra
1137 SkRect outsetRect = spotShadowRRect.rect().makeOutset(blurOutset, blurOutset);
1138 if (spotShadowRRect.isOval()) {
1139 spotShadowRRect = SkRRect::MakeOval(outsetRect);
1140 } else {
1141 SkScalar outsetRad = spotRadius + blurOutset;
1142 spotShadowRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1143 }
1144
Jim Van Verth34d6e4b2017-06-09 11:09:03 -04001145 GrColor spotColor;
1146 if (tonalColor) {
1147 SkScalar colorScale;
1148 SkScalar tonalAlpha;
1149 SkShadowUtils::ComputeTonalColorParams(color.fRGBA[0], color.fRGBA[1],
1150 color.fRGBA[2], rec.fSpotAlpha,
1151 &colorScale, &tonalAlpha);
1152 color.fRGBA[0] *= colorScale;
1153 color.fRGBA[1] *= colorScale;
1154 color.fRGBA[2] *= colorScale;
1155 color.fRGBA[3] = tonalAlpha;
1156 spotColor = color.toGrColor();
1157 } else {
1158 spotColor = color.mulByScalar(rec.fSpotAlpha).toGrColor();
1159 }
1160
Brian Salomon05969092017-07-13 11:20:51 -04001161 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(spotColor, viewMatrix,
1162 spotShadowRRect,
Jim Van Verth1af03d42017-07-31 09:34:58 -04001163 2.0f * devSpaceSpotBlur,
Brian Salomon05969092017-07-13 11:20:51 -04001164 insetWidth);
1165 SkASSERT(op);
1166 this->addDrawOp(clip, std::move(op));
Jim Van Verth3af1af92017-05-18 15:06:54 -04001167 }
1168
1169 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001170}
1171
1172///////////////////////////////////////////////////////////////////////////////
1173
Brian Osman11052242016-10-27 14:47:55 -04001174bool GrRenderTargetContext::drawFilledDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001175 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001176 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001177 const SkMatrix& viewMatrix,
1178 const SkRRect& origOuter,
1179 const SkRRect& origInner) {
robertphillips00095892016-02-29 13:50:40 -08001180 SkASSERT(!origInner.isEmpty());
1181 SkASSERT(!origOuter.isEmpty());
1182
csmartdaltone0d36292016-07-29 08:14:20 -07001183 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport()) {
Robert Phillipse3302df2017-04-24 07:31:02 -04001184 gr_instanced::OpAllocator* oa = this->drawingManager()->instancingAllocator();
1185 std::unique_ptr<GrDrawOp> op(oa->recordDRRect(
Brian Salomon54d212e2017-03-21 14:22:38 -04001186 origOuter, origInner, viewMatrix, std::move(paint), aa, fInstancedPipelineInfo));
Brian Salomon42521e82016-12-07 16:44:58 -05001187 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -04001188 this->addDrawOp(clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -07001189 return true;
1190 }
1191 }
1192
Brian Salomon7c8460e2017-05-12 11:36:10 -04001193 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
robertphillips00095892016-02-29 13:50:40 -08001194
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001195 GrPrimitiveEdgeType innerEdgeType, outerEdgeType;
1196 if (GrAAType::kCoverage == aaType) {
1197 innerEdgeType = kInverseFillAA_GrProcessorEdgeType;
1198 outerEdgeType = kFillAA_GrProcessorEdgeType;
1199 } else {
1200 innerEdgeType = kInverseFillBW_GrProcessorEdgeType;
1201 outerEdgeType = kFillBW_GrProcessorEdgeType;
1202 }
robertphillips00095892016-02-29 13:50:40 -08001203
1204 SkTCopyOnFirstWrite<SkRRect> inner(origInner), outer(origOuter);
1205 SkMatrix inverseVM;
1206 if (!viewMatrix.isIdentity()) {
1207 if (!origInner.transform(viewMatrix, inner.writable())) {
1208 return false;
1209 }
1210 if (!origOuter.transform(viewMatrix, outer.writable())) {
1211 return false;
1212 }
1213 if (!viewMatrix.invert(&inverseVM)) {
1214 return false;
1215 }
1216 } else {
1217 inverseVM.reset();
halcanary9d524f22016-03-29 09:03:52 -07001218 }
robertphillips00095892016-02-29 13:50:40 -08001219
robertphillips00095892016-02-29 13:50:40 -08001220 // TODO these need to be a geometry processors
bungeman06ca8ec2016-06-09 08:01:03 -07001221 sk_sp<GrFragmentProcessor> innerEffect(GrRRectEffect::Make(innerEdgeType, *inner));
robertphillips00095892016-02-29 13:50:40 -08001222 if (!innerEffect) {
1223 return false;
1224 }
1225
bungeman06ca8ec2016-06-09 08:01:03 -07001226 sk_sp<GrFragmentProcessor> outerEffect(GrRRectEffect::Make(outerEdgeType, *outer));
robertphillips00095892016-02-29 13:50:40 -08001227 if (!outerEffect) {
1228 return false;
1229 }
1230
Brian Salomon82f44312017-01-11 13:42:54 -05001231 paint.addCoverageFragmentProcessor(std::move(innerEffect));
1232 paint.addCoverageFragmentProcessor(std::move(outerEffect));
robertphillips00095892016-02-29 13:50:40 -08001233
1234 SkRect bounds = outer->getBounds();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001235 if (GrAAType::kCoverage == aaType) {
robertphillips00095892016-02-29 13:50:40 -08001236 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1237 }
halcanary9d524f22016-03-29 09:03:52 -07001238
Brian Salomon82f44312017-01-11 13:42:54 -05001239 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), bounds,
1240 inverseVM);
robertphillips00095892016-02-29 13:50:40 -08001241 return true;
1242}
1243
Brian Osman11052242016-10-27 14:47:55 -04001244void GrRenderTargetContext::drawDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001245 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001246 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001247 const SkMatrix& viewMatrix,
1248 const SkRRect& outer,
1249 const SkRRect& inner) {
robertphillips00095892016-02-29 13:50:40 -08001250 ASSERT_SINGLE_OWNER
1251 RETURN_IF_ABANDONED
1252 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001253 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawDRRect", fContext);
robertphillips00095892016-02-29 13:50:40 -08001254
1255 SkASSERT(!outer.isEmpty());
1256 SkASSERT(!inner.isEmpty());
1257
Robert Phillips72152832017-01-25 17:31:35 -05001258 AutoCheckFlush acf(this->drawingManager());
robertphillips00095892016-02-29 13:50:40 -08001259
Brian Salomon82f44312017-01-11 13:42:54 -05001260 if (this->drawFilledDRRect(clip, std::move(paint), aa, viewMatrix, outer, inner)) {
robertphillips00095892016-02-29 13:50:40 -08001261 return;
1262 }
1263
1264 SkPath path;
1265 path.setIsVolatile(true);
1266 path.addRRect(inner);
1267 path.addRRect(outer);
1268 path.setFillType(SkPath::kEvenOdd_FillType);
1269
Brian Salomon82f44312017-01-11 13:42:54 -05001270 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, GrStyle::SimpleFill());
robertphillips00095892016-02-29 13:50:40 -08001271}
1272
robertphillipsea461502015-05-26 11:38:03 -07001273///////////////////////////////////////////////////////////////////////////////
1274
msarettcc319b92016-08-25 18:07:18 -07001275static inline bool is_int(float x) {
1276 return x == (float) sk_float_round2int(x);
1277}
1278
Brian Osman11052242016-10-27 14:47:55 -04001279void GrRenderTargetContext::drawRegion(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001280 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001281 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001282 const SkMatrix& viewMatrix,
1283 const SkRegion& region,
Stan Iliev73d8fd92017-08-02 15:36:24 -04001284 const GrStyle& style,
1285 const GrUserStencilSettings* ss) {
msarettcc319b92016-08-25 18:07:18 -07001286 ASSERT_SINGLE_OWNER
1287 RETURN_IF_ABANDONED
1288 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001289 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRegion", fContext);
msarettcc319b92016-08-25 18:07:18 -07001290
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001291 if (GrAA::kYes == aa) {
Brian Salomonfc527d22016-12-14 21:07:01 -05001292 // GrRegionOp performs no antialiasing but is much faster, so here we check the matrix
Brian Salomonc57c7c92016-12-06 14:47:34 -05001293 // to see whether aa is really required.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001294 if (!SkToBool(viewMatrix.getType() & ~(SkMatrix::kTranslate_Mask)) &&
1295 is_int(viewMatrix.getTranslateX()) &&
1296 is_int(viewMatrix.getTranslateY())) {
1297 aa = GrAA::kNo;
1298 }
Brian Salomonc57c7c92016-12-06 14:47:34 -05001299 }
msarettcc319b92016-08-25 18:07:18 -07001300 bool complexStyle = !style.isSimpleFill();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001301 if (complexStyle || GrAA::kYes == aa) {
msarettcc319b92016-08-25 18:07:18 -07001302 SkPath path;
1303 region.getBoundaryPath(&path);
Brian Salomon82f44312017-01-11 13:42:54 -05001304 return this->drawPath(clip, std::move(paint), aa, viewMatrix, path, style);
msarettcc319b92016-08-25 18:07:18 -07001305 }
1306
Brian Salomonf0366322017-07-11 15:53:05 -04001307 GrAAType aaType = this->chooseAAType(GrAA::kNo, GrAllowMixedSamples::kNo);
Stan Iliev73d8fd92017-08-02 15:36:24 -04001308 std::unique_ptr<GrDrawOp> op = GrRegionOp::Make(std::move(paint), viewMatrix, region, aaType,
1309 ss);
Brian Salomonf0366322017-07-11 15:53:05 -04001310 this->addDrawOp(clip, std::move(op));
msarettcc319b92016-08-25 18:07:18 -07001311}
1312
Brian Osman11052242016-10-27 14:47:55 -04001313void GrRenderTargetContext::drawOval(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001314 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001315 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001316 const SkMatrix& viewMatrix,
1317 const SkRect& oval,
1318 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001319 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001320 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001321 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001322 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawOval", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -07001323
robertphillipsea461502015-05-26 11:38:03 -07001324 if (oval.isEmpty()) {
1325 return;
1326 }
1327
bsalomon6663acf2016-05-10 09:14:17 -07001328 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
robertphillipsea461502015-05-26 11:38:03 -07001329
Robert Phillips72152832017-01-25 17:31:35 -05001330 AutoCheckFlush acf(this->drawingManager());
bsalomon6663acf2016-05-10 09:14:17 -07001331 const SkStrokeRec& stroke = style.strokeRec();
csmartdaltona7f29642016-07-07 08:49:11 -07001332
csmartdaltone0d36292016-07-29 08:14:20 -07001333 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport() &&
1334 stroke.isFillStyle()) {
Robert Phillipse3302df2017-04-24 07:31:02 -04001335 gr_instanced::OpAllocator* oa = this->drawingManager()->instancingAllocator();
Brian Salomon54d212e2017-03-21 14:22:38 -04001336 std::unique_ptr<GrDrawOp> op(
Robert Phillipse3302df2017-04-24 07:31:02 -04001337 oa->recordOval(oval, viewMatrix, std::move(paint), aa, fInstancedPipelineInfo));
Brian Salomon42521e82016-12-07 16:44:58 -05001338 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -04001339 this->addDrawOp(clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -07001340 return;
1341 }
1342 }
1343
Brian Salomon7c8460e2017-05-12 11:36:10 -04001344 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001345 if (GrAAType::kCoverage == aaType) {
Brian Salomon94efbf52016-11-29 13:43:05 -05001346 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomon05441c42017-05-15 16:45:49 -04001347 std::unique_ptr<GrDrawOp> op =
1348 GrOvalOpFactory::MakeOvalOp(std::move(paint), viewMatrix, oval, stroke, shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -05001349 if (op) {
Brian Salomon05441c42017-05-15 16:45:49 -04001350 this->addDrawOp(clip, std::move(op));
robertphillipsb56f9272016-02-25 11:03:52 -08001351 return;
1352 }
robertphillipsea461502015-05-26 11:38:03 -07001353 }
robertphillipsb56f9272016-02-25 11:03:52 -08001354
1355 SkPath path;
1356 path.setIsVolatile(true);
1357 path.addOval(oval);
Brian Salomon82f44312017-01-11 13:42:54 -05001358 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, style);
robertphillipsea461502015-05-26 11:38:03 -07001359}
1360
Brian Osman11052242016-10-27 14:47:55 -04001361void GrRenderTargetContext::drawArc(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001362 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001363 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001364 const SkMatrix& viewMatrix,
1365 const SkRect& oval,
1366 SkScalar startAngle,
1367 SkScalar sweepAngle,
1368 bool useCenter,
1369 const GrStyle& style) {
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001370 ASSERT_SINGLE_OWNER
1371 RETURN_IF_ABANDONED
1372 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001373 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawArc", fContext);
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001374
1375 AutoCheckFlush acf(this->drawingManager());
1376
Brian Salomon7c8460e2017-05-12 11:36:10 -04001377 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001378 if (GrAAType::kCoverage == aaType) {
Brian Salomon94efbf52016-11-29 13:43:05 -05001379 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomon05441c42017-05-15 16:45:49 -04001380 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeArcOp(std::move(paint),
1381 viewMatrix,
1382 oval,
1383 startAngle,
1384 sweepAngle,
1385 useCenter,
1386 style,
1387 shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -05001388 if (op) {
Brian Salomon05441c42017-05-15 16:45:49 -04001389 this->addDrawOp(clip, std::move(op));
bsalomon4f3a0ca2016-08-22 13:14:26 -07001390 return;
1391 }
1392 }
1393 SkPath path;
bsalomon21af9ca2016-08-25 12:29:23 -07001394 SkPathPriv::CreateDrawArcPath(&path, oval, startAngle, sweepAngle, useCenter,
1395 style.isSimpleFill());
Brian Salomon82f44312017-01-11 13:42:54 -05001396 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, style);
bsalomon4f3a0ca2016-08-22 13:14:26 -07001397}
1398
Brian Osman11052242016-10-27 14:47:55 -04001399void GrRenderTargetContext::drawImageLattice(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001400 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001401 const SkMatrix& viewMatrix,
1402 int imageWidth,
1403 int imageHeight,
1404 std::unique_ptr<SkLatticeIter> iter,
1405 const SkRect& dst) {
joshualitt1de610a2016-01-06 08:26:09 -08001406 ASSERT_SINGLE_OWNER
joshualitt33a5fce2015-11-18 13:28:51 -08001407 RETURN_IF_ABANDONED
1408 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001409 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawImageLattice", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001410
Robert Phillips72152832017-01-25 17:31:35 -05001411 AutoCheckFlush acf(this->drawingManager());
joshualitt33a5fce2015-11-18 13:28:51 -08001412
Brian Salomon815486c2017-07-11 08:52:13 -04001413 std::unique_ptr<GrDrawOp> op = GrLatticeOp::MakeNonAA(std::move(paint), viewMatrix, imageWidth,
1414 imageHeight, std::move(iter), dst);
1415 this->addDrawOp(clip, std::move(op));
joshualitt33a5fce2015-11-18 13:28:51 -08001416}
1417
Greg Daniel51316782017-08-02 15:10:09 +00001418GrSemaphoresSubmitted GrRenderTargetContext::prepareForExternalIO(
1419 int numSemaphores, GrBackendSemaphore backendSemaphores[]) {
robertphillips8c523e02016-07-26 07:41:00 -07001420 ASSERT_SINGLE_OWNER
Greg Daniel51316782017-08-02 15:10:09 +00001421 if (this->drawingManager()->wasAbandoned()) { return GrSemaphoresSubmitted::kNo; }
robertphillips8c523e02016-07-26 07:41:00 -07001422 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001423 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "prepareForExternalIO", fContext);
robertphillips8c523e02016-07-26 07:41:00 -07001424
Greg Daniel51316782017-08-02 15:10:09 +00001425 return this->drawingManager()->prepareSurfaceForExternalIO(fRenderTargetProxy.get(),
1426 numSemaphores,
1427 backendSemaphores);
Greg Daniela5cb7812017-06-16 09:45:32 -04001428}
1429
Greg Danielc64ee462017-06-15 16:59:49 -04001430bool GrRenderTargetContext::waitOnSemaphores(int numSemaphores,
Greg Daniela5cb7812017-06-16 09:45:32 -04001431 const GrBackendSemaphore* waitSemaphores) {
1432 ASSERT_SINGLE_OWNER
Greg Danielc64ee462017-06-15 16:59:49 -04001433 RETURN_FALSE_IF_ABANDONED
Greg Daniela5cb7812017-06-16 09:45:32 -04001434 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001435 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "waitOnSemaphores", fContext);
Greg Daniela5cb7812017-06-16 09:45:32 -04001436
1437 AutoCheckFlush acf(this->drawingManager());
1438
Greg Danielc64ee462017-06-15 16:59:49 -04001439 if (numSemaphores && !this->caps()->fenceSyncSupport()) {
1440 return false;
1441 }
1442
Greg Daniela5cb7812017-06-16 09:45:32 -04001443 SkTArray<sk_sp<GrSemaphore>> semaphores(numSemaphores);
1444 for (int i = 0; i < numSemaphores; ++i) {
1445 sk_sp<GrSemaphore> sema = fContext->resourceProvider()->wrapBackendSemaphore(
1446 waitSemaphores[i], kAdopt_GrWrapOwnership);
1447 std::unique_ptr<GrOp> waitOp(GrSemaphoreOp::MakeWait(sema, fRenderTargetProxy.get()));
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001448 this->getRTOpList()->addOp(std::move(waitOp), *this->caps());
Greg Daniela5cb7812017-06-16 09:45:32 -04001449 }
Greg Danielc64ee462017-06-15 16:59:49 -04001450 return true;
robertphillips8c523e02016-07-26 07:41:00 -07001451}
joshualitt33a5fce2015-11-18 13:28:51 -08001452
Robert Phillips65a88fa2017-08-08 08:36:22 -04001453void GrRenderTargetContext::insertEventMarker(const SkString& str) {
1454 std::unique_ptr<GrOp> op(GrDebugMarkerOp::Make(fRenderTargetProxy.get(), str));
1455 this->getRTOpList()->addOp(std::move(op), *this->caps());
1456}
1457
1458
robertphillipsea461502015-05-26 11:38:03 -07001459// Can 'path' be drawn as a pair of filled nested rectangles?
bsalomon6663acf2016-05-10 09:14:17 -07001460static bool fills_as_nested_rects(const SkMatrix& viewMatrix, const SkPath& path, SkRect rects[2]) {
robertphillipsea461502015-05-26 11:38:03 -07001461
1462 if (path.isInverseFillType()) {
1463 return false;
1464 }
1465
1466 // TODO: this restriction could be lifted if we were willing to apply
1467 // the matrix to all the points individually rather than just to the rect
robertphillips0e7029e2015-11-30 05:45:06 -08001468 if (!viewMatrix.rectStaysRect()) {
robertphillipsea461502015-05-26 11:38:03 -07001469 return false;
1470 }
1471
1472 SkPath::Direction dirs[2];
1473 if (!path.isNestedFillRects(rects, dirs)) {
1474 return false;
1475 }
1476
1477 if (SkPath::kWinding_FillType == path.getFillType() && dirs[0] == dirs[1]) {
1478 // The two rects need to be wound opposite to each other
1479 return false;
1480 }
1481
1482 // Right now, nested rects where the margin is not the same width
1483 // all around do not render correctly
1484 const SkScalar* outer = rects[0].asScalars();
1485 const SkScalar* inner = rects[1].asScalars();
1486
1487 bool allEq = true;
1488
1489 SkScalar margin = SkScalarAbs(outer[0] - inner[0]);
1490 bool allGoE1 = margin >= SK_Scalar1;
1491
1492 for (int i = 1; i < 4; ++i) {
1493 SkScalar temp = SkScalarAbs(outer[i] - inner[i]);
1494 if (temp < SK_Scalar1) {
1495 allGoE1 = false;
1496 }
1497 if (!SkScalarNearlyEqual(margin, temp)) {
1498 allEq = false;
1499 }
1500 }
1501
1502 return allEq || allGoE1;
1503}
1504
Brian Osman11052242016-10-27 14:47:55 -04001505void GrRenderTargetContext::drawPath(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001506 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001507 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001508 const SkMatrix& viewMatrix,
1509 const SkPath& path,
1510 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001511 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001512 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001513 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001514 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawPath", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -07001515
robertphillipsea461502015-05-26 11:38:03 -07001516 if (path.isEmpty()) {
1517 if (path.isInverseFillType()) {
Brian Salomon82f44312017-01-11 13:42:54 -05001518 this->drawPaint(clip, std::move(paint), viewMatrix);
robertphillipsea461502015-05-26 11:38:03 -07001519 }
1520 return;
1521 }
1522
Robert Phillips72152832017-01-25 17:31:35 -05001523 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -07001524
Brian Salomon7c8460e2017-05-12 11:36:10 -04001525 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001526 if (GrAAType::kCoverage == aaType && !style.pathEffect()) {
bsalomon6663acf2016-05-10 09:14:17 -07001527 if (style.isSimpleFill() && !path.isConvex()) {
robertphillipsea461502015-05-26 11:38:03 -07001528 // Concave AA paths are expensive - try to avoid them for special cases
1529 SkRect rects[2];
1530
bsalomon6663acf2016-05-10 09:14:17 -07001531 if (fills_as_nested_rects(viewMatrix, path, rects)) {
Brian Salomonbaaf4392017-06-15 09:59:23 -04001532 std::unique_ptr<GrDrawOp> op =
1533 GrRectOpFactory::MakeAAFillNestedRects(std::move(paint), viewMatrix, rects);
Brian Salomon5f970fe2017-06-16 17:30:59 -04001534 if (op) {
1535 this->addDrawOp(clip, std::move(op));
bsalomon40ef4852016-05-02 13:22:13 -07001536 }
Brian Salomon5f970fe2017-06-16 17:30:59 -04001537 // A null return indicates that there is nothing to draw in this case.
1538 return;
robertphillipsea461502015-05-26 11:38:03 -07001539 }
1540 }
1541 SkRect ovalRect;
1542 bool isOval = path.isOval(&ovalRect);
1543
1544 if (isOval && !path.isInverseFillType()) {
Brian Salomon94efbf52016-11-29 13:43:05 -05001545 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomon05441c42017-05-15 16:45:49 -04001546 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeOvalOp(
1547 std::move(paint), viewMatrix, ovalRect, style.strokeRec(), shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -05001548 if (op) {
Brian Salomon05441c42017-05-15 16:45:49 -04001549 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -07001550 return;
1551 }
1552 }
1553 }
robertphillips4bc31812016-03-01 12:22:49 -08001554
1555 // Note that internalDrawPath may sw-rasterize the path into a scratch texture.
1556 // Scratch textures can be recycled after they are returned to the texture
1557 // cache. This presents a potential hazard for buffered drawing. However,
1558 // the writePixels that uploads to the scratch will perform a flush so we're
1559 // OK.
Brian Salomon82f44312017-01-11 13:42:54 -05001560 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, style);
robertphillipsea461502015-05-26 11:38:03 -07001561}
1562
Brian Osman11052242016-10-27 14:47:55 -04001563bool GrRenderTargetContextPriv::drawAndStencilPath(const GrClip& clip,
1564 const GrUserStencilSettings* ss,
1565 SkRegion::Op op,
1566 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001567 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001568 const SkMatrix& viewMatrix,
1569 const SkPath& path) {
robertphillips391395d2016-03-02 09:26:36 -08001570 ASSERT_SINGLE_OWNER_PRIV
1571 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -04001572 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001573 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawAndStencilPath",
1574 fRenderTargetContext->fContext);
robertphillips391395d2016-03-02 09:26:36 -08001575
1576 if (path.isEmpty() && path.isInverseFillType()) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001577 this->drawAndStencilRect(clip, ss, op, invert, GrAA::kNo, SkMatrix::I(),
Brian Osman11052242016-10-27 14:47:55 -04001578 SkRect::MakeIWH(fRenderTargetContext->width(),
1579 fRenderTargetContext->height()));
robertphillips391395d2016-03-02 09:26:36 -08001580 return true;
1581 }
1582
Robert Phillips72152832017-01-25 17:31:35 -05001583 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -08001584
1585 // An Assumption here is that path renderer would use some form of tweaking
1586 // the src color (either the input alpha or in the frag shader) to implement
1587 // aa. If we have some future driver-mojo path AA that can do the right
1588 // thing WRT to the blend then we'll need some query on the PR.
Brian Salomon7c8460e2017-05-12 11:36:10 -04001589 GrAAType aaType = fRenderTargetContext->chooseAAType(aa, GrAllowMixedSamples::kNo);
robertphillips976f5f02016-06-03 10:59:20 -07001590 bool hasUserStencilSettings = !ss->isUnused();
robertphillips391395d2016-03-02 09:26:36 -08001591
bsalomon8acedde2016-06-24 10:42:16 -07001592 GrShape shape(path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -08001593 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Eric Karl5c779752017-05-08 12:02:07 -07001594 canDrawArgs.fCaps = fRenderTargetContext->drawingManager()->getContext()->caps();
robertphillips391395d2016-03-02 09:26:36 -08001595 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07001596 canDrawArgs.fShape = &shape;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001597 canDrawArgs.fAAType = aaType;
cdalton93a379b2016-05-11 13:58:08 -07001598 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips391395d2016-03-02 09:26:36 -08001599
1600 // Don't allow the SW renderer
Robert Phillips72152832017-01-25 17:31:35 -05001601 GrPathRenderer* pr = fRenderTargetContext->drawingManager()->getPathRenderer(
Brian Salomon36aa1762016-12-10 13:24:02 -05001602 canDrawArgs, false, GrPathRendererChain::DrawType::kStencilAndColor);
robertphillips391395d2016-03-02 09:26:36 -08001603 if (!pr) {
1604 return false;
1605 }
1606
1607 GrPaint paint;
1608 paint.setCoverageSetOpXPFactory(op, invert);
1609
Brian Salomon82f44312017-01-11 13:42:54 -05001610 GrPathRenderer::DrawPathArgs args{
Robert Phillips256c37b2017-03-01 14:32:46 -05001611 fRenderTargetContext->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05001612 std::move(paint),
1613 ss,
1614 fRenderTargetContext,
1615 &clip,
1616 &viewMatrix,
1617 &shape,
1618 aaType,
1619 fRenderTargetContext->isGammaCorrect()};
robertphillips391395d2016-03-02 09:26:36 -08001620 pr->drawPath(args);
1621 return true;
1622}
1623
Brian Osman11052242016-10-27 14:47:55 -04001624SkBudgeted GrRenderTargetContextPriv::isBudgeted() const {
robertphillips714712b2016-08-04 06:20:45 -07001625 ASSERT_SINGLE_OWNER_PRIV
1626
Brian Osman11052242016-10-27 14:47:55 -04001627 if (fRenderTargetContext->wasAbandoned()) {
robertphillips714712b2016-08-04 06:20:45 -07001628 return SkBudgeted::kNo;
1629 }
1630
Brian Osman11052242016-10-27 14:47:55 -04001631 SkDEBUGCODE(fRenderTargetContext->validate();)
robertphillips714712b2016-08-04 06:20:45 -07001632
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001633 return fRenderTargetContext->fRenderTargetProxy->isBudgeted();
robertphillips714712b2016-08-04 06:20:45 -07001634}
1635
Brian Osman11052242016-10-27 14:47:55 -04001636void GrRenderTargetContext::internalDrawPath(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001637 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001638 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001639 const SkMatrix& viewMatrix,
1640 const SkPath& path,
1641 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001642 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001643 RETURN_IF_ABANDONED
Brian Salomondcbb9d92017-07-19 10:53:20 -04001644 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "internalDrawPath", fContext);
1645
bsalomon8acedde2016-06-24 10:42:16 -07001646 SkASSERT(!path.isEmpty());
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001647 GrShape shape;
Brian Salomon7c8460e2017-05-12 11:36:10 -04001648 // NVPR cannot handle hairlines, so this would get picked up by a different stencil and
1649 // cover path renderer (i.e. default path renderer). The hairline renderer produces much
1650 // smoother hairlines than MSAA.
1651 GrAllowMixedSamples allowMixedSamples =
1652 style.isSimpleHairline() ? GrAllowMixedSamples::kNo : GrAllowMixedSamples::kYes;
1653 GrAAType aaType = this->chooseAAType(aa, allowMixedSamples);
robertphillips68737822015-10-29 12:12:21 -07001654 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Eric Karl5c779752017-05-08 12:02:07 -07001655 canDrawArgs.fCaps = this->drawingManager()->getContext()->caps();
robertphillips68737822015-10-29 12:12:21 -07001656 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07001657 canDrawArgs.fShape = &shape;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001658 canDrawArgs.fHasUserStencilSettings = false;
robertphillips68737822015-10-29 12:12:21 -07001659
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001660 GrPathRenderer* pr;
Brian Salomon82125e92016-12-10 09:35:48 -05001661 static constexpr GrPathRendererChain::DrawType kType = GrPathRendererChain::DrawType::kColor;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001662 do {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001663 shape = GrShape(path, style);
bsalomon8acedde2016-06-24 10:42:16 -07001664 if (shape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001665 return;
1666 }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001667
1668 canDrawArgs.fAAType = aaType;
1669
1670 // Try a 1st time without applying any of the style to the geometry (and barring sw)
Robert Phillips72152832017-01-25 17:31:35 -05001671 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001672 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
1673
1674 if (!pr && shape.style().pathEffect()) {
1675 // It didn't work above, so try again with the path effect applied.
1676 shape = shape.applyStyle(GrStyle::Apply::kPathEffectOnly, styleScale);
bsalomon8acedde2016-06-24 10:42:16 -07001677 if (shape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001678 return;
1679 }
Robert Phillips72152832017-01-25 17:31:35 -05001680 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
bsalomon6663acf2016-05-10 09:14:17 -07001681 }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001682 if (!pr) {
1683 if (shape.style().applies()) {
1684 shape = shape.applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec, styleScale);
1685 if (shape.isEmpty()) {
1686 return;
1687 }
1688 }
1689 // This time, allow SW renderer
Robert Phillips72152832017-01-25 17:31:35 -05001690 pr = this->drawingManager()->getPathRenderer(canDrawArgs, true, kType);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001691 }
Brian Salomon0abc8b42016-12-13 10:22:54 -05001692 if (!pr && GrAATypeIsHW(aaType)) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001693 // There are exceptional cases where we may wind up falling back to coverage based AA
1694 // when the target is MSAA (e.g. through disabling path renderers via GrContextOptions).
1695 aaType = GrAAType::kCoverage;
1696 } else {
1697 break;
1698 }
1699 } while(true);
robertphillipsea461502015-05-26 11:38:03 -07001700
bsalomon8acedde2016-06-24 10:42:16 -07001701 if (!pr) {
robertphillipsea461502015-05-26 11:38:03 -07001702#ifdef SK_DEBUG
1703 SkDebugf("Unable to find path renderer compatible with path.\n");
1704#endif
1705 return;
1706 }
1707
Robert Phillips256c37b2017-03-01 14:32:46 -05001708 GrPathRenderer::DrawPathArgs args{this->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05001709 std::move(paint),
1710 &GrUserStencilSettings::kUnused,
1711 this,
1712 &clip,
1713 &viewMatrix,
1714 &shape,
1715 aaType,
1716 this->isGammaCorrect()};
bsalomon0aff2fa2015-07-31 06:48:27 -07001717 pr->drawPath(args);
robertphillipsea461502015-05-26 11:38:03 -07001718}
1719
Brian Salomon467921e2017-03-06 16:17:12 -05001720static void op_bounds(SkRect* bounds, const GrOp* op) {
1721 *bounds = op->bounds();
1722 if (op->hasZeroArea()) {
1723 if (op->hasAABloat()) {
1724 bounds->outset(0.5f, 0.5f);
1725 } else {
1726 // We don't know which way the particular GPU will snap lines or points at integer
1727 // coords. So we ensure that the bounds is large enough for either snap.
1728 SkRect before = *bounds;
1729 bounds->roundOut(bounds);
1730 if (bounds->fLeft == before.fLeft) {
1731 bounds->fLeft -= 1;
1732 }
1733 if (bounds->fTop == before.fTop) {
1734 bounds->fTop -= 1;
1735 }
1736 if (bounds->fRight == before.fRight) {
1737 bounds->fRight += 1;
1738 }
1739 if (bounds->fBottom == before.fBottom) {
1740 bounds->fBottom += 1;
1741 }
1742 }
1743 }
1744}
1745
Brian Salomon54d212e2017-03-21 14:22:38 -04001746uint32_t GrRenderTargetContext::addDrawOp(const GrClip& clip, std::unique_ptr<GrDrawOp> op) {
joshualitt1de610a2016-01-06 08:26:09 -08001747 ASSERT_SINGLE_OWNER
Robert Phillipsc0138922017-03-08 11:50:55 -05001748 if (this->drawingManager()->wasAbandoned()) {
1749 return SK_InvalidUniqueID;
1750 }
robertphillips2e1e51f2015-10-15 08:01:48 -07001751 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001752 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "addDrawOp", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -07001753
Brian Salomon467921e2017-03-06 16:17:12 -05001754 // Setup clip
1755 SkRect bounds;
1756 op_bounds(&bounds, op.get());
Brian Salomon97180af2017-03-14 13:42:58 -04001757 GrAppliedClip appliedClip;
Brian Salomon54d212e2017-03-21 14:22:38 -04001758 GrDrawOp::FixedFunctionFlags fixedFunctionFlags = op->fixedFunctionFlags();
1759 if (!clip.apply(fContext, this, fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesHWAA,
1760 fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil, &appliedClip,
1761 &bounds)) {
1762 return SK_InvalidUniqueID;
1763 }
1764
Brian Salomon54d212e2017-03-21 14:22:38 -04001765 if (fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil ||
1766 appliedClip.hasStencilClip()) {
Robert Phillipsb681a0f2017-08-09 13:31:36 -04001767 this->getOpList()->setStencilLoadOp(GrLoadOp::kClear);
Robert Phillips95214472017-08-08 18:00:03 -04001768
Robert Phillips5efd5ea2017-05-30 13:47:32 -04001769 // This forces instantiation of the render target.
1770 GrRenderTarget* rt = this->accessRenderTarget();
1771 if (!rt) {
1772 return SK_InvalidUniqueID;
1773 }
1774
Brian Salomon54d212e2017-03-21 14:22:38 -04001775 if (!fContext->resourceProvider()->attachStencilAttachment(rt)) {
1776 SkDebugf("ERROR creating stencil attachment. Draw skipped.\n");
1777 return SK_InvalidUniqueID;
1778 }
1779 }
1780
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001781 GrXferProcessor::DstProxy dstProxy;
Brian Salomonf86d37b2017-06-16 10:04:34 -04001782 if (op->finalize(*this->caps(), &appliedClip) == GrDrawOp::RequiresDstTexture::kYes) {
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001783 if (!this->setupDstProxy(this->asRenderTargetProxy(), clip, op->bounds(), &dstProxy)) {
Brian Salomon54d212e2017-03-21 14:22:38 -04001784 return SK_InvalidUniqueID;
1785 }
1786 }
1787
1788 op->setClippedBounds(bounds);
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001789 return this->getRTOpList()->addOp(std::move(op), *this->caps(),
1790 std::move(appliedClip), dstProxy);
Brian Salomon54d212e2017-03-21 14:22:38 -04001791}
1792
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001793bool GrRenderTargetContext::setupDstProxy(GrRenderTargetProxy* rtProxy, const GrClip& clip,
Robert Phillips16d8ec62017-07-27 16:16:25 -04001794 const SkRect& opBounds,
1795 GrXferProcessor::DstProxy* dstProxy) {
Brian Salomon467921e2017-03-06 16:17:12 -05001796 if (this->caps()->textureBarrierSupport()) {
Robert Phillipsbf25d432017-04-07 10:08:53 -04001797 if (GrTextureProxy* texProxy = rtProxy->asTextureProxy()) {
Brian Salomon467921e2017-03-06 16:17:12 -05001798 // The render target is a texture, so we can read from it directly in the shader. The XP
1799 // will be responsible to detect this situation and request a texture barrier.
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001800 dstProxy->setProxy(sk_ref_sp(texProxy));
1801 dstProxy->setOffset(0, 0);
Robert Phillipsbf25d432017-04-07 10:08:53 -04001802 return true;
Brian Salomon467921e2017-03-06 16:17:12 -05001803 }
1804 }
1805
Robert Phillipsbf25d432017-04-07 10:08:53 -04001806 SkIRect copyRect = SkIRect::MakeWH(rtProxy->width(), rtProxy->height());
Brian Salomon467921e2017-03-06 16:17:12 -05001807
Eric Karl74480882017-04-03 14:49:05 -07001808 SkIRect clippedRect;
Robert Phillipsbf25d432017-04-07 10:08:53 -04001809 clip.getConservativeBounds(rtProxy->width(), rtProxy->height(), &clippedRect);
Eric Karl72e551e2017-04-04 13:42:10 -07001810 SkIRect drawIBounds;
Brian Salomon467921e2017-03-06 16:17:12 -05001811 opBounds.roundOut(&drawIBounds);
Brian Salomon859621f2017-03-16 09:21:54 -04001812 // Cover up for any precision issues by outsetting the op bounds a pixel in each direction.
1813 drawIBounds.outset(1, 1);
Eric Karl72e551e2017-04-04 13:42:10 -07001814 if (!clippedRect.intersect(drawIBounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -05001815#ifdef SK_DEBUG
Robert Phillipsbf25d432017-04-07 10:08:53 -04001816 GrCapsDebugf(this->caps(), "setupDstTexture: Missed an early reject bailing on draw.");
Brian Salomon467921e2017-03-06 16:17:12 -05001817#endif
Robert Phillipsbf25d432017-04-07 10:08:53 -04001818 return false;
Brian Salomon467921e2017-03-06 16:17:12 -05001819 }
1820
1821 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
1822 // have per-sample dst values by making the copy multisampled.
1823 GrSurfaceDesc desc;
Eric Karl74480882017-04-03 14:49:05 -07001824 bool rectsMustMatch = false;
1825 bool disallowSubrect = false;
Robert Phillipsbf25d432017-04-07 10:08:53 -04001826 if (!this->caps()->initDescForDstCopy(rtProxy, &desc, &rectsMustMatch, &disallowSubrect)) {
Brian Salomon467921e2017-03-06 16:17:12 -05001827 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillips16d8ec62017-07-27 16:16:25 -04001828 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
Robert Phillipsbf25d432017-04-07 10:08:53 -04001829 desc.fConfig = rtProxy->config();
Brian Salomon467921e2017-03-06 16:17:12 -05001830 }
1831
Eric Karl74480882017-04-03 14:49:05 -07001832 if (!disallowSubrect) {
1833 copyRect = clippedRect;
1834 }
Brian Salomon467921e2017-03-06 16:17:12 -05001835
Robert Phillipsbf25d432017-04-07 10:08:53 -04001836 SkIPoint dstPoint, dstOffset;
1837 SkBackingFit fit;
Eric Karl74480882017-04-03 14:49:05 -07001838 if (rectsMustMatch) {
Robert Phillipsbf25d432017-04-07 10:08:53 -04001839 SkASSERT(desc.fOrigin == rtProxy->origin());
1840 desc.fWidth = rtProxy->width();
1841 desc.fHeight = rtProxy->height();
Eric Karl74480882017-04-03 14:49:05 -07001842 dstPoint = {copyRect.fLeft, copyRect.fTop};
1843 dstOffset = {0, 0};
Robert Phillipsbf25d432017-04-07 10:08:53 -04001844 fit = SkBackingFit::kExact;
Eric Karl74480882017-04-03 14:49:05 -07001845 } else {
1846 desc.fWidth = copyRect.width();
1847 desc.fHeight = copyRect.height();
1848 dstPoint = {0, 0};
1849 dstOffset = {copyRect.fLeft, copyRect.fTop};
Robert Phillipsbf25d432017-04-07 10:08:53 -04001850 fit = SkBackingFit::kApprox;
Eric Karl74480882017-04-03 14:49:05 -07001851 }
Brian Salomon467921e2017-03-06 16:17:12 -05001852
Robert Phillipsbf25d432017-04-07 10:08:53 -04001853 sk_sp<GrSurfaceContext> sContext = fContext->contextPriv().makeDeferredSurfaceContext(
1854 desc,
1855 fit,
1856 SkBudgeted::kYes);
1857 if (!sContext) {
1858 SkDebugf("setupDstTexture: surfaceContext creation failed.\n");
1859 return false;
1860 }
1861
1862 if (!sContext->copy(rtProxy, copyRect, dstPoint)) {
1863 SkDebugf("setupDstTexture: copy failed.\n");
1864 return false;
1865 }
1866
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001867 dstProxy->setProxy(sContext->asTextureProxyRef());
1868 dstProxy->setOffset(dstOffset);
Robert Phillipsbf25d432017-04-07 10:08:53 -04001869 return true;
robertphillips2334fb62015-06-17 05:43:33 -07001870}