blob: 1de4ab8510419dd5ff73007cf9bd1df0365d1339 [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 Salomonf18b1d82017-10-27 11:30:49 -040013#include "GrBlurUtils.h"
Brian Salomon5ec9def2016-12-20 15:34:05 -050014#include "GrColor.h"
Robert Phillipse78b7252017-04-06 07:59:41 -040015#include "GrContextPriv.h"
robertphillips77a2e522015-10-17 07:43:27 -070016#include "GrDrawingManager.h"
csmartdalton02fa32c2016-08-19 13:29:27 -070017#include "GrFixedClip.h"
robertphillips714712b2016-08-04 06:20:45 -070018#include "GrGpuResourcePriv.h"
Robert Phillips9d6c64f2017-09-14 10:56:45 -040019#include "GrOpList.h"
robertphillipsea461502015-05-26 11:38:03 -070020#include "GrPathRenderer.h"
Brian Salomon57caa662017-10-18 12:21:05 +000021#include "GrQuad.h"
robertphillips2334fb62015-06-17 05:43:33 -070022#include "GrRenderTarget.h"
Brian Salomon5ec9def2016-12-20 15:34:05 -050023#include "GrRenderTargetContextPriv.h"
bsalomon473addf2015-10-02 07:49:05 -070024#include "GrResourceProvider.h"
Brian Salomon467921e2017-03-06 16:17:12 -050025#include "GrStencilAttachment.h"
Brian Salomondcbb9d92017-07-19 10:53:20 -040026#include "GrTracing.h"
Jim Van Verth1af03d42017-07-31 09:34:58 -040027#include "SkDrawShadowInfo.h"
Brian Salomonf18b1d82017-10-27 11:30:49 -040028#include "SkGr.h"
Brian Salomon467921e2017-03-06 16:17:12 -050029#include "SkLatticeIter.h"
30#include "SkMatrixPriv.h"
Mike Reed242135a2018-02-22 13:41:39 -050031#include "SkRRectPriv.h"
Jim Van Verth34d6e4b2017-06-09 11:09:03 -040032#include "SkShadowUtils.h"
robertphillips2d70dcb2015-10-06 07:38:23 -070033#include "SkSurfacePriv.h"
Brian Salomon467921e2017-03-06 16:17:12 -050034#include "effects/GrRRectEffect.h"
Brian Salomonf18b1d82017-10-27 11:30:49 -040035#include "ops/GrAtlasTextOp.h"
Brian Salomon89527432016-12-16 09:52:16 -050036#include "ops/GrClearOp.h"
Robert Phillipsb9a02a12017-04-06 11:08:40 -040037#include "ops/GrClearStencilClipOp.h"
Robert Phillips65a88fa2017-08-08 08:36:22 -040038#include "ops/GrDebugMarkerOp.h"
Brian Salomon0f353322017-05-03 20:58:59 +000039#include "ops/GrDrawAtlasOp.h"
Brian Salomon6d4b65e2017-05-03 17:06:09 -040040#include "ops/GrDrawOp.h"
Brian Salomon89527432016-12-16 09:52:16 -050041#include "ops/GrDrawVerticesOp.h"
42#include "ops/GrLatticeOp.h"
43#include "ops/GrOp.h"
44#include "ops/GrOvalOpFactory.h"
45#include "ops/GrRectOpFactory.h"
46#include "ops/GrRegionOp.h"
Greg Daniela5cb7812017-06-16 09:45:32 -040047#include "ops/GrSemaphoreOp.h"
Brian Salomon89527432016-12-16 09:52:16 -050048#include "ops/GrShadowRRectOp.h"
Brian Salomon467921e2017-03-06 16:17:12 -050049#include "ops/GrStencilPathOp.h"
Brian Salomon34169692017-08-28 15:32:01 -040050#include "ops/GrTextureOp.h"
joshualitte8042922015-12-11 06:11:21 -080051#include "text/GrAtlasTextContext.h"
Brian Salomonf18b1d82017-10-27 11:30:49 -040052#include "text/GrTextUtils.h"
53
54class GrRenderTargetContext::TextTarget : public GrTextUtils::Target {
55public:
56 TextTarget(GrRenderTargetContext* renderTargetContext)
57 : Target(renderTargetContext->width(), renderTargetContext->height(),
58 renderTargetContext->colorSpaceInfo())
59 , fRenderTargetContext(renderTargetContext) {}
60
61 void addDrawOp(const GrClip& clip, std::unique_ptr<GrAtlasTextOp> op) {
62 fRenderTargetContext->addDrawOp(clip, std::move(op));
63 }
64
65 void drawPath(const GrClip& clip, const SkPath& path, const SkPaint& paint,
66 const SkMatrix& viewMatrix, const SkMatrix* pathMatrix,
67 const SkIRect& clipBounds) {
68 GrBlurUtils::drawPathWithMaskFilter(fRenderTargetContext->fContext, fRenderTargetContext,
69 clip, path, paint, viewMatrix, pathMatrix, clipBounds,
70 false);
71 }
72
73 void makeGrPaint(GrMaskFormat maskFormat, const SkPaint& skPaint, const SkMatrix& viewMatrix,
74 GrPaint* grPaint) {
75 GrContext* context = fRenderTargetContext->fContext;
76 const GrColorSpaceInfo& colorSpaceInfo = fRenderTargetContext->colorSpaceInfo();
77 if (kARGB_GrMaskFormat == maskFormat) {
78 SkPaintToGrPaintWithPrimitiveColor(context, colorSpaceInfo, skPaint, grPaint);
79 } else {
80 SkPaintToGrPaint(context, colorSpaceInfo, skPaint, viewMatrix, grPaint);
81 }
82 }
83
84private:
85 GrRenderTargetContext* fRenderTargetContext;
86};
joshualittbc907352016-01-13 06:45:40 -080087
Robert Phillips72152832017-01-25 17:31:35 -050088#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this->drawingManager()->getContext())
joshualitt1de610a2016-01-06 08:26:09 -080089#define ASSERT_SINGLE_OWNER \
Robert Phillipsa90aa2b2017-04-10 08:19:26 -040090 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(this->singleOwner());)
robertphillips391395d2016-03-02 09:26:36 -080091#define ASSERT_SINGLE_OWNER_PRIV \
Robert Phillipsa90aa2b2017-04-10 08:19:26 -040092 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fRenderTargetContext->singleOwner());)
Robert Phillips72152832017-01-25 17:31:35 -050093#define RETURN_IF_ABANDONED if (this->drawingManager()->wasAbandoned()) { return; }
94#define RETURN_IF_ABANDONED_PRIV if (fRenderTargetContext->drawingManager()->wasAbandoned()) { return; }
95#define RETURN_FALSE_IF_ABANDONED if (this->drawingManager()->wasAbandoned()) { return false; }
96#define RETURN_FALSE_IF_ABANDONED_PRIV if (fRenderTargetContext->drawingManager()->wasAbandoned()) { return false; }
97#define RETURN_NULL_IF_ABANDONED if (this->drawingManager()->wasAbandoned()) { return nullptr; }
robertphillipsea461502015-05-26 11:38:03 -070098
Brian Salomone225b562017-06-14 13:00:03 -040099//////////////////////////////////////////////////////////////////////////////
100
101GrAAType GrChooseAAType(GrAA aa, GrFSAAType fsaaType, GrAllowMixedSamples allowMixedSamples,
102 const GrCaps& caps) {
103 if (GrAA::kNo == aa) {
104 // On some devices we cannot disable MSAA if it is enabled so we make the AA type reflect
105 // that.
106 if (fsaaType == GrFSAAType::kUnifiedMSAA && !caps.multisampleDisableSupport()) {
107 return GrAAType::kMSAA;
108 }
109 return GrAAType::kNone;
110 }
111 switch (fsaaType) {
112 case GrFSAAType::kNone:
113 return GrAAType::kCoverage;
114 case GrFSAAType::kUnifiedMSAA:
115 return GrAAType::kMSAA;
116 case GrFSAAType::kMixedSamples:
117 return GrAllowMixedSamples::kYes == allowMixedSamples ? GrAAType::kMixedSamples
118 : GrAAType::kCoverage;
119 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400120 SK_ABORT("Unexpected fsaa type");
Brian Salomone225b562017-06-14 13:00:03 -0400121 return GrAAType::kNone;
122}
123
124//////////////////////////////////////////////////////////////////////////////
125
robertphillipsea461502015-05-26 11:38:03 -0700126class AutoCheckFlush {
127public:
halcanary9d524f22016-03-29 09:03:52 -0700128 AutoCheckFlush(GrDrawingManager* drawingManager) : fDrawingManager(drawingManager) {
robertphillips77a2e522015-10-17 07:43:27 -0700129 SkASSERT(fDrawingManager);
130 }
bsalomonb77a9072016-09-07 10:02:04 -0700131 ~AutoCheckFlush() { fDrawingManager->flushIfNecessary(); }
robertphillipsea461502015-05-26 11:38:03 -0700132
133private:
robertphillips77a2e522015-10-17 07:43:27 -0700134 GrDrawingManager* fDrawingManager;
robertphillipsea461502015-05-26 11:38:03 -0700135};
136
Brian Osman11052242016-10-27 14:47:55 -0400137bool GrRenderTargetContext::wasAbandoned() const {
Robert Phillips72152832017-01-25 17:31:35 -0500138 return this->drawingManager()->wasAbandoned();
robertphillips7761d612016-05-16 09:14:53 -0700139}
140
Robert Phillipsf2361d22016-10-25 14:20:06 -0400141// In MDB mode the reffing of the 'getLastOpList' call's result allows in-progress
Brian Osman11052242016-10-27 14:47:55 -0400142// GrOpLists to be picked up and added to by renderTargetContexts lower in the call
Robert Phillipsf2361d22016-10-25 14:20:06 -0400143// stack. When this occurs with a closed GrOpList, a new one will be allocated
Brian Osman11052242016-10-27 14:47:55 -0400144// when the renderTargetContext attempts to use it (via getOpList).
145GrRenderTargetContext::GrRenderTargetContext(GrContext* context,
146 GrDrawingManager* drawingMgr,
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400147 sk_sp<GrRenderTargetProxy> rtp,
Brian Osman11052242016-10-27 14:47:55 -0400148 sk_sp<SkColorSpace> colorSpace,
149 const SkSurfaceProps* surfaceProps,
150 GrAuditTrail* auditTrail,
Robert Phillips941d1442017-06-14 16:37:02 -0400151 GrSingleOwner* singleOwner,
152 bool managedOpList)
Brian Salomonf3569f02017-10-24 12:52:33 -0400153 : GrSurfaceContext(context, drawingMgr, rtp->config(), std::move(colorSpace), auditTrail,
154 singleOwner)
155 , fRenderTargetProxy(std::move(rtp))
156 , fOpList(sk_ref_sp(fRenderTargetProxy->getLastRenderTargetOpList()))
Brian Salomonf3569f02017-10-24 12:52:33 -0400157 , fSurfaceProps(SkSurfacePropsCopyOrDefault(surfaceProps))
158 , fManagedOpList(managedOpList) {
Robert Phillips4150eea2018-02-07 17:08:21 -0500159 GrResourceProvider* resourceProvider = context->contextPriv().resourceProvider();
160 if (resourceProvider && !resourceProvider->explicitlyAllocateGPUResources()) {
161 // MDB TODO: to ensure all resources still get allocated in the correct order in the hybrid
162 // world we need to get the correct opList here so that it, in turn, can grab and hold
163 // its rendertarget.
164 this->getRTOpList();
165 }
166
Brian Salomonf18b1d82017-10-27 11:30:49 -0400167 fTextTarget.reset(new TextTarget(this));
robertphillips2e1e51f2015-10-15 08:01:48 -0700168 SkDEBUGCODE(this->validate();)
robertphillipsea461502015-05-26 11:38:03 -0700169}
170
robertphillips2e1e51f2015-10-15 08:01:48 -0700171#ifdef SK_DEBUG
Brian Osman11052242016-10-27 14:47:55 -0400172void GrRenderTargetContext::validate() const {
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400173 SkASSERT(fRenderTargetProxy);
174 fRenderTargetProxy->validate(fContext);
robertphillipsa106c622015-10-16 09:07:06 -0700175
Robert Phillipsf2361d22016-10-25 14:20:06 -0400176 if (fOpList && !fOpList->isClosed()) {
Robert Phillipsdc83b892017-04-13 12:23:54 -0400177 SkASSERT(fRenderTargetProxy->getLastOpList() == fOpList.get());
robertphillipsa106c622015-10-16 09:07:06 -0700178 }
robertphillips2e1e51f2015-10-15 08:01:48 -0700179}
180#endif
181
Brian Osman11052242016-10-27 14:47:55 -0400182GrRenderTargetContext::~GrRenderTargetContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800183 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700184}
185
Robert Phillipsf200a902017-01-30 13:27:37 -0500186GrTextureProxy* GrRenderTargetContext::asTextureProxy() {
Robert Phillipseaa86252016-11-08 13:49:39 +0000187 return fRenderTargetProxy->asTextureProxy();
188}
189
Greg Daniele252f082017-10-23 16:05:23 -0400190const GrTextureProxy* GrRenderTargetContext::asTextureProxy() const {
191 return fRenderTargetProxy->asTextureProxy();
192}
193
Robert Phillipsf200a902017-01-30 13:27:37 -0500194sk_sp<GrTextureProxy> GrRenderTargetContext::asTextureProxyRef() {
195 return sk_ref_sp(fRenderTargetProxy->asTextureProxy());
196}
197
Greg Daniele252f082017-10-23 16:05:23 -0400198GrMipMapped GrRenderTargetContext::mipMapped() const {
199 if (const GrTextureProxy* proxy = this->asTextureProxy()) {
200 return proxy->mipMapped();
201 }
202 return GrMipMapped::kNo;
203}
204
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400205GrRenderTargetOpList* GrRenderTargetContext::getRTOpList() {
joshualitt1de610a2016-01-06 08:26:09 -0800206 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700207 SkDEBUGCODE(this->validate();)
208
Robert Phillipsf2361d22016-10-25 14:20:06 -0400209 if (!fOpList || fOpList->isClosed()) {
Robert Phillips941d1442017-06-14 16:37:02 -0400210 fOpList = this->drawingManager()->newRTOpList(fRenderTargetProxy.get(), fManagedOpList);
robertphillipsa106c622015-10-16 09:07:06 -0700211 }
212
Robert Phillipsdc83b892017-04-13 12:23:54 -0400213 return fOpList.get();
robertphillipsa106c622015-10-16 09:07:06 -0700214}
215
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400216GrOpList* GrRenderTargetContext::getOpList() {
217 return this->getRTOpList();
robertphillipsea461502015-05-26 11:38:03 -0700218}
219
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500220void GrRenderTargetContext::drawText(const GrClip& clip, const SkPaint& skPaint,
Brian Salomon82f44312017-01-11 13:42:54 -0500221 const SkMatrix& viewMatrix, const char text[],
222 size_t byteLength, SkScalar x, SkScalar y,
223 const SkIRect& clipBounds) {
joshualitt1de610a2016-01-06 08:26:09 -0800224 ASSERT_SINGLE_OWNER
robertphillips2d70dcb2015-10-06 07:38:23 -0700225 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700226 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400227 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawText", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -0700228
Robert Phillips72152832017-01-25 17:31:35 -0500229 GrAtlasTextContext* atlasTextContext = this->drawingManager()->getAtlasTextContext();
Brian Salomonf18b1d82017-10-27 11:30:49 -0400230 atlasTextContext->drawText(fContext, fTextTarget.get(), clip, skPaint, viewMatrix,
231 fSurfaceProps, text, byteLength, x, y, clipBounds);
robertphillips2334fb62015-06-17 05:43:33 -0700232}
robertphillipscaef3452015-11-11 13:18:11 -0800233
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500234void GrRenderTargetContext::drawPosText(const GrClip& clip, const SkPaint& paint,
235 const SkMatrix& viewMatrix, const char text[],
236 size_t byteLength, const SkScalar pos[],
Brian Salomon82f44312017-01-11 13:42:54 -0500237 int scalarsPerPosition, const SkPoint& offset,
238 const SkIRect& clipBounds) {
joshualitt1de610a2016-01-06 08:26:09 -0800239 ASSERT_SINGLE_OWNER
robertphillips2d70dcb2015-10-06 07:38:23 -0700240 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700241 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400242 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawPosText", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -0700243
Robert Phillips72152832017-01-25 17:31:35 -0500244 GrAtlasTextContext* atlasTextContext = this->drawingManager()->getAtlasTextContext();
Brian Salomonf18b1d82017-10-27 11:30:49 -0400245 atlasTextContext->drawPosText(fContext, fTextTarget.get(), clip, paint, viewMatrix,
246 fSurfaceProps, text, byteLength, pos, scalarsPerPosition, offset,
247 clipBounds);
robertphillips2334fb62015-06-17 05:43:33 -0700248}
robertphillipscaef3452015-11-11 13:18:11 -0800249
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500250void GrRenderTargetContext::drawTextBlob(const GrClip& clip, const SkPaint& paint,
Brian Osman11052242016-10-27 14:47:55 -0400251 const SkMatrix& viewMatrix, const SkTextBlob* blob,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500252 SkScalar x, SkScalar y, SkDrawFilter* filter,
253 const SkIRect& clipBounds) {
joshualitt1de610a2016-01-06 08:26:09 -0800254 ASSERT_SINGLE_OWNER
robertphillips2d70dcb2015-10-06 07:38:23 -0700255 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700256 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400257 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTextBlob", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -0700258
Robert Phillips72152832017-01-25 17:31:35 -0500259 GrAtlasTextContext* atlasTextContext = this->drawingManager()->getAtlasTextContext();
Brian Salomonf18b1d82017-10-27 11:30:49 -0400260 atlasTextContext->drawTextBlob(fContext, fTextTarget.get(), clip, paint, viewMatrix,
261 fSurfaceProps, blob, x, y, filter, clipBounds);
robertphillipsea461502015-05-26 11:38:03 -0700262}
263
Brian Osman11052242016-10-27 14:47:55 -0400264void GrRenderTargetContext::discard() {
joshualitt1de610a2016-01-06 08:26:09 -0800265 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700266 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700267 SkDEBUGCODE(this->validate();)
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400268 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "discard", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700269
Robert Phillips72152832017-01-25 17:31:35 -0500270 AutoCheckFlush acf(this->drawingManager());
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400271
Robert Phillips380b90c2017-08-30 07:41:07 -0400272 this->getRTOpList()->discard();
robertphillipsea461502015-05-26 11:38:03 -0700273}
274
Brian Osman11052242016-10-27 14:47:55 -0400275void GrRenderTargetContext::clear(const SkIRect* rect,
276 const GrColor color,
Chris Dalton344e9032017-12-11 15:42:09 -0700277 CanClearFullscreen canClearFullscreen) {
joshualitt1de610a2016-01-06 08:26:09 -0800278 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700279 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700280 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400281 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "clear", fContext);
robertphillipsea461502015-05-26 11:38:03 -0700282
Robert Phillips72152832017-01-25 17:31:35 -0500283 AutoCheckFlush acf(this->drawingManager());
Chris Dalton344e9032017-12-11 15:42:09 -0700284 this->internalClear(rect ? GrFixedClip(*rect) : GrFixedClip::Disabled(), color,
285 canClearFullscreen);
csmartdalton29df7602016-08-31 11:55:52 -0700286}
robertphillips9199a9f2016-07-13 07:48:43 -0700287
Robert Phillips784b7bf2016-12-09 13:35:02 -0500288void GrRenderTargetContextPriv::absClear(const SkIRect* clearRect, const GrColor color) {
289 ASSERT_SINGLE_OWNER_PRIV
290 RETURN_IF_ABANDONED_PRIV
291 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400292 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "absClear",
293 fRenderTargetContext->fContext);
Robert Phillips784b7bf2016-12-09 13:35:02 -0500294
Robert Phillips72152832017-01-25 17:31:35 -0500295 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500296
Brian Salomonbb5711a2017-05-17 13:49:59 -0400297 SkIRect rtRect = SkIRect::MakeWH(fRenderTargetContext->fRenderTargetProxy->worstCaseWidth(),
298 fRenderTargetContext->fRenderTargetProxy->worstCaseHeight());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500299
300 if (clearRect) {
301 if (clearRect->contains(rtRect)) {
302 clearRect = nullptr; // full screen
303 } else {
304 if (!rtRect.intersect(*clearRect)) {
305 return;
306 }
307 }
308 }
309
310 // TODO: in a post-MDB world this should be handled at the OpList level.
311 // An op-list that is initially cleared and has no other ops should receive an
312 // extra draw.
Brian Salomon43f8bf02017-10-18 08:33:29 -0400313 // This path doesn't handle coalescing of full screen clears b.c. it
314 // has to clear the entire render target - not just the content area.
315 // It could be done but will take more finagling.
316 std::unique_ptr<GrOp> op(GrClearOp::Make(rtRect, color, !clearRect));
317 if (!op) {
318 return;
Robert Phillips784b7bf2016-12-09 13:35:02 -0500319 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400320 fRenderTargetContext->getRTOpList()->addOp(std::move(op), *fRenderTargetContext->caps());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500321}
322
Brian Osman11052242016-10-27 14:47:55 -0400323void GrRenderTargetContextPriv::clear(const GrFixedClip& clip,
324 const GrColor color,
Chris Dalton344e9032017-12-11 15:42:09 -0700325 CanClearFullscreen canClearFullscreen) {
csmartdalton29df7602016-08-31 11:55:52 -0700326 ASSERT_SINGLE_OWNER_PRIV
327 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400328 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400329 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "clear",
330 fRenderTargetContext->fContext);
csmartdalton29df7602016-08-31 11:55:52 -0700331
Robert Phillips72152832017-01-25 17:31:35 -0500332 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Chris Dalton344e9032017-12-11 15:42:09 -0700333 fRenderTargetContext->internalClear(clip, color, canClearFullscreen);
csmartdalton29df7602016-08-31 11:55:52 -0700334}
335
Brian Osman11052242016-10-27 14:47:55 -0400336void GrRenderTargetContext::internalClear(const GrFixedClip& clip,
337 const GrColor color,
Chris Dalton344e9032017-12-11 15:42:09 -0700338 CanClearFullscreen canClearFullscreen) {
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700339 bool isFull = false;
340 if (!clip.hasWindowRectangles()) {
341 isFull = !clip.scissorEnabled() ||
Chris Dalton344e9032017-12-11 15:42:09 -0700342 (CanClearFullscreen::kYes == canClearFullscreen &&
343 fContext->caps()->preferFullscreenClears()) ||
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700344 clip.scissorRect().contains(SkIRect::MakeWH(this->width(), this->height()));
345 }
robertphillips9199a9f2016-07-13 07:48:43 -0700346
Brian Salomon43f8bf02017-10-18 08:33:29 -0400347 if (isFull) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400348 this->getRTOpList()->fullClear(*this->caps(), color);
robertphillips9199a9f2016-07-13 07:48:43 -0700349 } else {
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000350 std::unique_ptr<GrOp> op(GrClearOp::Make(clip, color, this->asSurfaceProxy()));
Brian Salomon42521e82016-12-07 16:44:58 -0500351 if (!op) {
csmartdalton29df7602016-08-31 11:55:52 -0700352 return;
353 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400354 this->getRTOpList()->addOp(std::move(op), *this->caps());
robertphillips9199a9f2016-07-13 07:48:43 -0700355 }
robertphillipsea461502015-05-26 11:38:03 -0700356}
357
Brian Osman11052242016-10-27 14:47:55 -0400358void GrRenderTargetContext::drawPaint(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500359 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400360 const SkMatrix& viewMatrix) {
joshualitt1de610a2016-01-06 08:26:09 -0800361 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700362 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700363 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400364 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawPaint", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700365
robertphillipsea461502015-05-26 11:38:03 -0700366 // set rect to be big enough to fill the space, but not super-huge, so we
367 // don't overflow fixed-point implementations
robertphillips13a7eee2016-08-31 15:06:24 -0700368
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400369 SkRect r = fRenderTargetProxy->getBoundsRect();
robertphillipsea461502015-05-26 11:38:03 -0700370
bsalomoncb31e512016-08-26 10:48:19 -0700371 SkRRect rrect;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500372 GrAA aa;
bsalomoncb31e512016-08-26 10:48:19 -0700373 // Check if we can replace a clipRRect()/drawPaint() with a drawRRect(). We only do the
374 // transformation for non-rect rrects. Rects caused a performance regression on an Android
375 // test that needs investigation. We also skip cases where there are fragment processors
376 // because they may depend on having correct local coords and this path draws in device space
377 // without a local matrix.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500378 if (!paint.numTotalFragmentProcessors() && clip.isRRect(r, &rrect, &aa) && !rrect.isRect()) {
Brian Salomon82f44312017-01-11 13:42:54 -0500379 this->drawRRect(GrNoClip(), std::move(paint), aa, SkMatrix::I(), rrect,
380 GrStyle::SimpleFill());
bsalomoncb31e512016-08-26 10:48:19 -0700381 return;
382 }
383
robertphillipsea461502015-05-26 11:38:03 -0700384
385 bool isPerspective = viewMatrix.hasPerspective();
386
387 // We attempt to map r by the inverse matrix and draw that. mapRect will
388 // map the four corners and bound them with a new rect. This will not
389 // produce a correct result for some perspective matrices.
390 if (!isPerspective) {
reeda39667c2016-08-22 06:39:49 -0700391 if (!SkMatrixPriv::InverseMapRect(viewMatrix, &r, r)) {
robertphillipsea461502015-05-26 11:38:03 -0700392 SkDebugf("Could not invert matrix\n");
393 return;
394 }
Brian Salomon82f44312017-01-11 13:42:54 -0500395 this->drawRect(clip, std::move(paint), GrAA::kNo, viewMatrix, r);
robertphillipsea461502015-05-26 11:38:03 -0700396 } else {
397 SkMatrix localMatrix;
398 if (!viewMatrix.invert(&localMatrix)) {
399 SkDebugf("Could not invert matrix\n");
400 return;
401 }
402
Robert Phillips72152832017-01-25 17:31:35 -0500403 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700404
Brian Salomonbaaf4392017-06-15 09:59:23 -0400405 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeNonAAFillWithLocalMatrix(
406 std::move(paint), SkMatrix::I(), localMatrix, r, GrAAType::kNone);
407 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -0700408 }
409}
410
robertphillipsea461502015-05-26 11:38:03 -0700411static inline bool rect_contains_inclusive(const SkRect& rect, const SkPoint& point) {
412 return point.fX >= rect.fLeft && point.fX <= rect.fRight &&
413 point.fY >= rect.fTop && point.fY <= rect.fBottom;
414}
415
csmartdalton97f6cd52016-07-13 13:37:08 -0700416// Attempts to crop a rect and optional local rect to the clip boundaries.
417// Returns false if the draw can be skipped entirely.
robertphillips13a7eee2016-08-31 15:06:24 -0700418static bool crop_filled_rect(int width, int height, const GrClip& clip,
csmartdalton97f6cd52016-07-13 13:37:08 -0700419 const SkMatrix& viewMatrix, SkRect* rect,
420 SkRect* localRect = nullptr) {
421 if (!viewMatrix.rectStaysRect()) {
422 return true;
423 }
424
csmartdalton97f6cd52016-07-13 13:37:08 -0700425 SkIRect clipDevBounds;
426 SkRect clipBounds;
csmartdalton97f6cd52016-07-13 13:37:08 -0700427
robertphillips13a7eee2016-08-31 15:06:24 -0700428 clip.getConservativeBounds(width, height, &clipDevBounds);
reeda39667c2016-08-22 06:39:49 -0700429 if (!SkMatrixPriv::InverseMapRect(viewMatrix, &clipBounds, SkRect::Make(clipDevBounds))) {
430 return false;
431 }
csmartdalton97f6cd52016-07-13 13:37:08 -0700432
433 if (localRect) {
434 if (!rect->intersects(clipBounds)) {
435 return false;
436 }
437 const SkScalar dx = localRect->width() / rect->width();
438 const SkScalar dy = localRect->height() / rect->height();
439 if (clipBounds.fLeft > rect->fLeft) {
440 localRect->fLeft += (clipBounds.fLeft - rect->fLeft) * dx;
441 rect->fLeft = clipBounds.fLeft;
442 }
443 if (clipBounds.fTop > rect->fTop) {
444 localRect->fTop += (clipBounds.fTop - rect->fTop) * dy;
445 rect->fTop = clipBounds.fTop;
446 }
447 if (clipBounds.fRight < rect->fRight) {
448 localRect->fRight -= (rect->fRight - clipBounds.fRight) * dx;
449 rect->fRight = clipBounds.fRight;
450 }
451 if (clipBounds.fBottom < rect->fBottom) {
452 localRect->fBottom -= (rect->fBottom - clipBounds.fBottom) * dy;
453 rect->fBottom = clipBounds.fBottom;
454 }
455 return true;
456 }
457
458 return rect->intersect(clipBounds);
459}
460
Brian Osman11052242016-10-27 14:47:55 -0400461bool GrRenderTargetContext::drawFilledRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500462 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500463 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400464 const SkMatrix& viewMatrix,
465 const SkRect& rect,
466 const GrUserStencilSettings* ss) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700467 SkRect croppedRect = rect;
Robert Phillips784b7bf2016-12-09 13:35:02 -0500468 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix, &croppedRect)) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700469 return true;
470 }
robertphillips44302392016-07-08 14:43:03 -0700471
Brian Salomon7c8460e2017-05-12 11:36:10 -0400472 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400473 std::unique_ptr<GrDrawOp> op;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500474 if (GrAAType::kCoverage == aaType) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400475 op = GrRectOpFactory::MakeAAFill(std::move(paint), viewMatrix, croppedRect, ss);
robertphillips391395d2016-03-02 09:26:36 -0800476 } else {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400477 op = GrRectOpFactory::MakeNonAAFill(std::move(paint), viewMatrix, croppedRect, aaType, ss);
robertphillips391395d2016-03-02 09:26:36 -0800478 }
Brian Salomonbaaf4392017-06-15 09:59:23 -0400479 if (!op) {
480 return false;
481 }
482 this->addDrawOp(clip, std::move(op));
483 return true;
robertphillips391395d2016-03-02 09:26:36 -0800484}
485
Brian Osman11052242016-10-27 14:47:55 -0400486void GrRenderTargetContext::drawRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500487 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500488 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400489 const SkMatrix& viewMatrix,
490 const SkRect& rect,
491 const GrStyle* style) {
bsalomon6663acf2016-05-10 09:14:17 -0700492 if (!style) {
493 style = &GrStyle::SimpleFill();
494 }
joshualitt1de610a2016-01-06 08:26:09 -0800495 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700496 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700497 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400498 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRect", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700499
bsalomon6663acf2016-05-10 09:14:17 -0700500 // Path effects should've been devolved to a path in SkGpuDevice
501 SkASSERT(!style->pathEffect());
robertphillipsea461502015-05-26 11:38:03 -0700502
Robert Phillips72152832017-01-25 17:31:35 -0500503 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700504
bsalomon6663acf2016-05-10 09:14:17 -0700505 const SkStrokeRec& stroke = style->strokeRec();
robertphillips3ab14ca2016-07-10 11:49:39 -0700506 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
Brian Salomon43f8bf02017-10-18 08:33:29 -0400507 // Check if this is a full RT draw and can be replaced with a clear. We don't bother
508 // checking cases where the RT is fully inside a stroke.
509 SkRect rtRect = fRenderTargetProxy->getBoundsRect();
510 // Does the clip contain the entire RT?
511 if (clip.quickContains(rtRect)) {
512 SkMatrix invM;
513 if (!viewMatrix.invert(&invM)) {
514 return;
515 }
516 // Does the rect bound the RT?
517 GrQuad quad;
518 quad.setFromMappedRect(rtRect, invM);
519 if (rect_contains_inclusive(rect, quad.point(0)) &&
520 rect_contains_inclusive(rect, quad.point(1)) &&
521 rect_contains_inclusive(rect, quad.point(2)) &&
522 rect_contains_inclusive(rect, quad.point(3))) {
523 // Will it blend?
524 GrColor clearColor;
525 if (paint.isConstantBlendedColor(&clearColor)) {
Chris Dalton344e9032017-12-11 15:42:09 -0700526 this->clear(nullptr, clearColor,
527 GrRenderTargetContext::CanClearFullscreen::kYes);
robertphillipsea461502015-05-26 11:38:03 -0700528 return;
529 }
530 }
531 }
robertphillips44302392016-07-08 14:43:03 -0700532
Brian Salomon82f44312017-01-11 13:42:54 -0500533 if (this->drawFilledRect(clip, std::move(paint), aa, viewMatrix, rect, nullptr)) {
robertphillips44302392016-07-08 14:43:03 -0700534 return;
535 }
bsalomona7d85ba2016-07-06 11:54:59 -0700536 } else if (stroke.getStyle() == SkStrokeRec::kStroke_Style ||
537 stroke.getStyle() == SkStrokeRec::kHairline_Style) {
538 if ((!rect.width() || !rect.height()) &&
539 SkStrokeRec::kHairline_Style != stroke.getStyle()) {
540 SkScalar r = stroke.getWidth() / 2;
541 // TODO: Move these stroke->fill fallbacks to GrShape?
542 switch (stroke.getJoin()) {
543 case SkPaint::kMiter_Join:
Brian Salomon82f44312017-01-11 13:42:54 -0500544 this->drawRect(
545 clip, std::move(paint), aa, viewMatrix,
546 {rect.fLeft - r, rect.fTop - r, rect.fRight + r, rect.fBottom + r},
547 &GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700548 return;
549 case SkPaint::kRound_Join:
550 // Raster draws nothing when both dimensions are empty.
551 if (rect.width() || rect.height()){
552 SkRRect rrect = SkRRect::MakeRectXY(rect.makeOutset(r, r), r, r);
Brian Salomon82f44312017-01-11 13:42:54 -0500553 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect,
554 GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700555 return;
556 }
557 case SkPaint::kBevel_Join:
558 if (!rect.width()) {
Brian Salomon82f44312017-01-11 13:42:54 -0500559 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700560 {rect.fLeft - r, rect.fTop, rect.fRight + r, rect.fBottom},
561 &GrStyle::SimpleFill());
562 } else {
Brian Salomon82f44312017-01-11 13:42:54 -0500563 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700564 {rect.fLeft, rect.fTop - r, rect.fRight, rect.fBottom + r},
565 &GrStyle::SimpleFill());
566 }
567 return;
568 }
569 }
robertphillips44302392016-07-08 14:43:03 -0700570
Brian Salomonbaaf4392017-06-15 09:59:23 -0400571 std::unique_ptr<GrDrawOp> op;
robertphillips44302392016-07-08 14:43:03 -0700572
Brian Salomon7c8460e2017-05-12 11:36:10 -0400573 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500574 if (GrAAType::kCoverage == aaType) {
cdaltonbb539482016-01-04 09:48:25 -0800575 // The stroke path needs the rect to remain axis aligned (no rotation or skew).
576 if (viewMatrix.rectStaysRect()) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400577 op = GrRectOpFactory::MakeAAStroke(std::move(paint), viewMatrix, rect, stroke);
cdaltonbb539482016-01-04 09:48:25 -0800578 }
robertphillipsea461502015-05-26 11:38:03 -0700579 } else {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400580 op = GrRectOpFactory::MakeNonAAStroke(std::move(paint), viewMatrix, rect, stroke,
581 aaType);
robertphillips391395d2016-03-02 09:26:36 -0800582 }
robertphillips4bc31812016-03-01 12:22:49 -0800583
Brian Salomon42521e82016-12-07 16:44:58 -0500584 if (op) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400585 this->addDrawOp(clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700586 return;
robertphillips4bc31812016-03-01 12:22:49 -0800587 }
robertphillips4bc31812016-03-01 12:22:49 -0800588 }
Brian Salomon2fad74a2017-12-20 13:28:55 -0500589 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, GrShape(rect, *style));
robertphillipsea461502015-05-26 11:38:03 -0700590}
591
Robert Phillipsec2249f2016-11-09 08:54:35 -0500592int GrRenderTargetContextPriv::maxWindowRectangles() const {
593 return fRenderTargetContext->fRenderTargetProxy->maxWindowRectangles(
594 *fRenderTargetContext->fContext->caps());
595}
596
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000597void GrRenderTargetContextPriv::clearStencilClip(const GrFixedClip& clip, bool insideStencilMask) {
robertphillips976f5f02016-06-03 10:59:20 -0700598 ASSERT_SINGLE_OWNER_PRIV
599 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400600 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400601 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "clearStencilClip",
602 fRenderTargetContext->fContext);
robertphillips976f5f02016-06-03 10:59:20 -0700603
Robert Phillips72152832017-01-25 17:31:35 -0500604 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400605
Robert Phillips2f4ddf62017-06-01 08:48:19 -0400606 std::unique_ptr<GrOp> op(GrClearStencilClipOp::Make(
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000607 clip, insideStencilMask,
608 fRenderTargetContext->fRenderTargetProxy.get()));
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400609 if (!op) {
Robert Phillipse60ad622016-11-17 10:22:48 -0500610 return;
611 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400612 fRenderTargetContext->getRTOpList()->addOp(std::move(op), *fRenderTargetContext->caps());
robertphillips976f5f02016-06-03 10:59:20 -0700613}
614
Chris Daltonbbfd5162017-11-07 13:35:22 -0700615void GrRenderTargetContextPriv::stencilPath(const GrHardClip& clip,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500616 GrAAType aaType,
Brian Osman11052242016-10-27 14:47:55 -0400617 const SkMatrix& viewMatrix,
618 const GrPath* path) {
Brian Salomon467921e2017-03-06 16:17:12 -0500619 ASSERT_SINGLE_OWNER_PRIV
620 RETURN_IF_ABANDONED_PRIV
621 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400622 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "stencilPath",
623 fRenderTargetContext->fContext);
Brian Salomon467921e2017-03-06 16:17:12 -0500624
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500625 SkASSERT(aaType != GrAAType::kCoverage);
Brian Salomon467921e2017-03-06 16:17:12 -0500626
627 bool useHWAA = GrAATypeIsHW(aaType);
628 // TODO: extract portions of checkDraw that are relevant to path stenciling.
629 SkASSERT(path);
630 SkASSERT(fRenderTargetContext->caps()->shaderCaps()->pathRenderingSupport());
631
632 // FIXME: Use path bounds instead of this WAR once
633 // https://bugs.chromium.org/p/skia/issues/detail?id=5640 is resolved.
634 SkRect bounds = SkRect::MakeIWH(fRenderTargetContext->width(), fRenderTargetContext->height());
635
636 // Setup clip
Chris Daltonbbfd5162017-11-07 13:35:22 -0700637 GrAppliedHardClip appliedClip;
638 if (!clip.apply(fRenderTargetContext->width(), fRenderTargetContext->height(), &appliedClip,
639 &bounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -0500640 return;
641 }
642
Robert Phillips65048132017-08-10 08:44:49 -0400643 fRenderTargetContext->setNeedsStencil();
Brian Salomon467921e2017-03-06 16:17:12 -0500644
645 std::unique_ptr<GrOp> op = GrStencilPathOp::Make(viewMatrix,
646 useHWAA,
647 path->getFillType(),
648 appliedClip.hasStencilClip(),
Brian Salomon467921e2017-03-06 16:17:12 -0500649 appliedClip.scissorState(),
Brian Salomon467921e2017-03-06 16:17:12 -0500650 path);
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400651 if (!op) {
652 return;
653 }
Brian Salomon97180af2017-03-14 13:42:58 -0400654 op->setClippedBounds(bounds);
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400655 fRenderTargetContext->getRTOpList()->addOp(std::move(op), *fRenderTargetContext->caps());
robertphillips976f5f02016-06-03 10:59:20 -0700656}
657
Chris Daltonbbfd5162017-11-07 13:35:22 -0700658void GrRenderTargetContextPriv::stencilRect(const GrHardClip& clip,
Brian Osman11052242016-10-27 14:47:55 -0400659 const GrUserStencilSettings* ss,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500660 GrAAType aaType,
Brian Osman11052242016-10-27 14:47:55 -0400661 const SkMatrix& viewMatrix,
662 const SkRect& rect) {
robertphillips976f5f02016-06-03 10:59:20 -0700663 ASSERT_SINGLE_OWNER_PRIV
664 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400665 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400666 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "stencilRect",
667 fRenderTargetContext->fContext);
668
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500669 SkASSERT(GrAAType::kCoverage != aaType);
Robert Phillips72152832017-01-25 17:31:35 -0500670 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips976f5f02016-06-03 10:59:20 -0700671
672 GrPaint paint;
Brian Salomona1633922017-01-09 11:46:10 -0500673 paint.setXPFactory(GrDisableColorXPFactory::Get());
Brian Salomonbaaf4392017-06-15 09:59:23 -0400674 std::unique_ptr<GrDrawOp> op =
675 GrRectOpFactory::MakeNonAAFill(std::move(paint), viewMatrix, rect, aaType, ss);
676 fRenderTargetContext->addDrawOp(clip, std::move(op));
robertphillips976f5f02016-06-03 10:59:20 -0700677}
678
Chris Daltonbbfd5162017-11-07 13:35:22 -0700679bool GrRenderTargetContextPriv::drawAndStencilRect(const GrHardClip& clip,
Brian Osman11052242016-10-27 14:47:55 -0400680 const GrUserStencilSettings* ss,
681 SkRegion::Op op,
682 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500683 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400684 const SkMatrix& viewMatrix,
685 const SkRect& rect) {
robertphillips391395d2016-03-02 09:26:36 -0800686 ASSERT_SINGLE_OWNER_PRIV
687 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400688 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400689 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawAndStencilRect",
690 fRenderTargetContext->fContext);
robertphillips391395d2016-03-02 09:26:36 -0800691
Robert Phillips72152832017-01-25 17:31:35 -0500692 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -0800693
694 GrPaint paint;
robertphillips391395d2016-03-02 09:26:36 -0800695 paint.setCoverageSetOpXPFactory(op, invert);
696
Brian Salomon82f44312017-01-11 13:42:54 -0500697 if (fRenderTargetContext->drawFilledRect(clip, std::move(paint), aa, viewMatrix, rect, ss)) {
robertphillips391395d2016-03-02 09:26:36 -0800698 return true;
699 }
robertphillips391395d2016-03-02 09:26:36 -0800700 SkPath path;
701 path.setIsVolatile(true);
702 path.addRect(rect);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500703 return this->drawAndStencilPath(clip, ss, op, invert, aa, viewMatrix, path);
robertphillips391395d2016-03-02 09:26:36 -0800704}
705
Brian Osman11052242016-10-27 14:47:55 -0400706void GrRenderTargetContext::fillRectToRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500707 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500708 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400709 const SkMatrix& viewMatrix,
710 const SkRect& rectToDraw,
711 const SkRect& localRect) {
joshualitt1de610a2016-01-06 08:26:09 -0800712 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700713 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700714 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400715 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "fillRectToRect", fContext);
robertphillipsea461502015-05-26 11:38:03 -0700716
csmartdalton97f6cd52016-07-13 13:37:08 -0700717 SkRect croppedRect = rectToDraw;
718 SkRect croppedLocalRect = localRect;
robertphillips13a7eee2016-08-31 15:06:24 -0700719 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix,
720 &croppedRect, &croppedLocalRect)) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700721 return;
722 }
723
Robert Phillips72152832017-01-25 17:31:35 -0500724 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -0700725
Brian Salomon7c8460e2017-05-12 11:36:10 -0400726 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500727 if (GrAAType::kCoverage != aaType) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400728 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeNonAAFillWithLocalRect(
729 std::move(paint), viewMatrix, croppedRect, croppedLocalRect, aaType);
730 this->addDrawOp(clip, std::move(op));
csmartdaltonfc49d562016-07-26 17:05:47 -0700731 return;
joshualitt04194f32016-01-13 10:08:27 -0800732 }
bsalomonbb243832016-07-22 07:10:19 -0700733
Brian Salomonbaaf4392017-06-15 09:59:23 -0400734 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeAAFillWithLocalRect(
735 std::move(paint), viewMatrix, croppedRect, croppedLocalRect);
736 if (op) {
737 this->addDrawOp(clip, std::move(op));
csmartdaltonfc49d562016-07-26 17:05:47 -0700738 return;
739 }
740
741 SkMatrix viewAndUnLocalMatrix;
742 if (!viewAndUnLocalMatrix.setRectToRect(localRect, rectToDraw, SkMatrix::kFill_ScaleToFit)) {
743 SkDebugf("fillRectToRect called with empty local matrix.\n");
744 return;
745 }
746 viewAndUnLocalMatrix.postConcat(viewMatrix);
747
Brian Salomon2fad74a2017-12-20 13:28:55 -0500748 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewAndUnLocalMatrix,
749 GrShape(localRect));
joshualittb6b513b2015-08-21 10:25:18 -0700750}
751
Brian Salomon34169692017-08-28 15:32:01 -0400752static bool must_filter(const SkRect& src, const SkRect& dst, const SkMatrix& ctm) {
753 // We don't currently look for 90 degree rotations, mirroring, or downscales that sample at
754 // texel centers.
755 if (!ctm.isTranslate()) {
756 return true;
757 }
758 if (src.width() != dst.width() || src.height() != dst.height()) {
759 return true;
760 }
761 // Check that the device space rectangle's fractional offset is the same as the src rectangle,
762 // and that therefore integers in the src image fall on integers in device space.
763 SkScalar x = ctm.getTranslateX(), y = ctm.getTranslateY();
764 x += dst.fLeft; y += dst.fTop;
765 x -= src.fLeft; y -= src.fTop;
766 return !SkScalarIsInt(x) || !SkScalarIsInt(y);
767}
768
769void GrRenderTargetContext::drawTextureAffine(const GrClip& clip, sk_sp<GrTextureProxy> proxy,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400770 GrSamplerState::Filter filter, GrColor color,
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500771 const SkRect& srcRect, const SkRect& dstRect, GrAA aa,
Brian Salomon34169692017-08-28 15:32:01 -0400772 const SkMatrix& viewMatrix,
773 sk_sp<GrColorSpaceXform> colorSpaceXform) {
774 ASSERT_SINGLE_OWNER
775 RETURN_IF_ABANDONED
776 SkDEBUGCODE(this->validate();)
777 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTextureAffine", fContext);
778 SkASSERT(!viewMatrix.hasPerspective());
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400779 if (filter != GrSamplerState::Filter::kNearest && !must_filter(srcRect, dstRect, viewMatrix)) {
780 filter = GrSamplerState::Filter::kNearest;
Brian Salomon34169692017-08-28 15:32:01 -0400781 }
Brian Salomonff9d6d32017-08-30 10:27:49 -0400782 SkRect clippedDstRect = dstRect;
783 SkRect clippedSrcRect = srcRect;
784 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix, &clippedDstRect,
785 &clippedSrcRect)) {
786 return;
787 }
Brian Salomon485b8c62018-01-12 15:11:06 -0500788 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomonf3569f02017-10-24 12:52:33 -0400789 bool allowSRGB = SkToBool(this->colorSpaceInfo().colorSpace());
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500790 this->addDrawOp(
791 clip, GrTextureOp::Make(std::move(proxy), filter, color, clippedSrcRect, clippedDstRect,
Brian Salomon485b8c62018-01-12 15:11:06 -0500792 aaType, viewMatrix, std::move(colorSpaceXform), allowSRGB));
Brian Salomon34169692017-08-28 15:32:01 -0400793}
794
Brian Osman11052242016-10-27 14:47:55 -0400795void GrRenderTargetContext::fillRectWithLocalMatrix(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500796 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500797 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400798 const SkMatrix& viewMatrix,
799 const SkRect& rectToDraw,
800 const SkMatrix& localMatrix) {
joshualitt1de610a2016-01-06 08:26:09 -0800801 ASSERT_SINGLE_OWNER
joshualittb6b513b2015-08-21 10:25:18 -0700802 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700803 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400804 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "fillRectWithLocalMatrix", fContext);
joshualittb6b513b2015-08-21 10:25:18 -0700805
csmartdalton97f6cd52016-07-13 13:37:08 -0700806 SkRect croppedRect = rectToDraw;
robertphillips13a7eee2016-08-31 15:06:24 -0700807 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix, &croppedRect)) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700808 return;
809 }
810
Robert Phillips72152832017-01-25 17:31:35 -0500811 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -0700812
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 Salomon2fad74a2017-12-20 13:28:55 -0500839 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewAndUnLocalMatrix,
840 GrShape(path));
robertphillipsea461502015-05-26 11:38:03 -0700841}
842
Brian Osman11052242016-10-27 14:47:55 -0400843void GrRenderTargetContext::drawVertices(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500844 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400845 const SkMatrix& viewMatrix,
Brian Osmanae0c50c2017-05-25 16:56:34 -0400846 sk_sp<SkVertices> vertices,
847 GrPrimitiveType* overridePrimType) {
Brian Salomon199fb872017-02-06 09:41:10 -0500848 ASSERT_SINGLE_OWNER
849 RETURN_IF_ABANDONED
850 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400851 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawVertices", fContext);
Brian Salomon199fb872017-02-06 09:41:10 -0500852
853 AutoCheckFlush acf(this->drawingManager());
854
855 SkASSERT(vertices);
Brian Salomonc2f42542017-07-12 14:11:22 -0400856 GrAAType aaType = this->chooseAAType(GrAA::kNo, GrAllowMixedSamples::kNo);
Brian Salomonf3569f02017-10-24 12:52:33 -0400857 std::unique_ptr<GrDrawOp> op = GrDrawVerticesOp::Make(
858 std::move(paint), std::move(vertices), viewMatrix, aaType,
859 this->colorSpaceInfo().isGammaCorrect(),
860 this->colorSpaceInfo().refColorSpaceXformFromSRGB(), overridePrimType);
Brian Salomonc2f42542017-07-12 14:11:22 -0400861 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -0700862}
863
864///////////////////////////////////////////////////////////////////////////////
865
Brian Osman11052242016-10-27 14:47:55 -0400866void GrRenderTargetContext::drawAtlas(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500867 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400868 const SkMatrix& viewMatrix,
869 int spriteCount,
870 const SkRSXform xform[],
871 const SkRect texRect[],
872 const SkColor colors[]) {
joshualitt1de610a2016-01-06 08:26:09 -0800873 ASSERT_SINGLE_OWNER
jvanverth31ff7622015-08-07 10:09:28 -0700874 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700875 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400876 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawAtlas", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700877
Robert Phillips72152832017-01-25 17:31:35 -0500878 AutoCheckFlush acf(this->drawingManager());
halcanary9d524f22016-03-29 09:03:52 -0700879
Brian Salomon0088f942017-07-12 11:51:27 -0400880 GrAAType aaType = this->chooseAAType(GrAA::kNo, GrAllowMixedSamples::kNo);
881 std::unique_ptr<GrDrawOp> op = GrDrawAtlasOp::Make(std::move(paint), viewMatrix, aaType,
882 spriteCount, xform, texRect, colors);
883 this->addDrawOp(clip, std::move(op));
jvanverth31ff7622015-08-07 10:09:28 -0700884}
885
886///////////////////////////////////////////////////////////////////////////////
887
Brian Osman11052242016-10-27 14:47:55 -0400888void GrRenderTargetContext::drawRRect(const GrClip& origClip,
Brian Salomon82f44312017-01-11 13:42:54 -0500889 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500890 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400891 const SkMatrix& viewMatrix,
892 const SkRRect& rrect,
893 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -0800894 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700895 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700896 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400897 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRRect", fContext);
robertphillipsea461502015-05-26 11:38:03 -0700898 if (rrect.isEmpty()) {
899 return;
900 }
901
bsalomon7f0d9f32016-08-15 14:49:10 -0700902 GrNoClip noclip;
903 const GrClip* clip = &origClip;
904#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
905 // The Android framework frequently clips rrects to themselves where the clip is non-aa and the
Brian Salomon42521e82016-12-07 16:44:58 -0500906 // draw is aa. Since our lower level clip code works from op bounds, which are SkRects, it
bsalomon7f0d9f32016-08-15 14:49:10 -0700907 // doesn't detect that the clip can be ignored (modulo antialiasing). The following test
908 // attempts to mitigate the stencil clip cost but will only help when the entire clip stack
909 // can be ignored. We'd prefer to fix this in the framework by removing the clips calls.
910 SkRRect devRRect;
911 if (rrect.transform(viewMatrix, &devRRect) && clip->quickContains(devRRect)) {
912 clip = &noclip;
913 }
914#endif
bsalomon6663acf2016-05-10 09:14:17 -0700915 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
ksakamotoec7f2ac2016-07-05 03:54:53 -0700916
Robert Phillips72152832017-01-25 17:31:35 -0500917 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -0700918 const SkStrokeRec stroke = style.strokeRec();
csmartdaltona7f29642016-07-07 08:49:11 -0700919
Brian Salomonea26d6b2018-01-23 20:33:21 +0000920 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500921 if (GrAAType::kCoverage == aaType) {
Brian Salomon94efbf52016-11-29 13:43:05 -0500922 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomonea26d6b2018-01-23 20:33:21 +0000923 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeRRectOp(std::move(paint),
924 viewMatrix,
925 rrect,
926 stroke,
927 shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -0500928 if (op) {
Brian Salomon05441c42017-05-15 16:45:49 -0400929 this->addDrawOp(*clip, std::move(op));
robertphillipsb56f9272016-02-25 11:03:52 -0800930 return;
931 }
robertphillipsea461502015-05-26 11:38:03 -0700932 }
robertphillipsb56f9272016-02-25 11:03:52 -0800933
Brian Salomon2fad74a2017-12-20 13:28:55 -0500934 this->drawShapeUsingPathRenderer(*clip, std::move(paint), aa, viewMatrix,
935 GrShape(rrect, style));
robertphillipsea461502015-05-26 11:38:03 -0700936}
937
Jim Van Verthc5903412016-11-17 15:27:09 -0500938///////////////////////////////////////////////////////////////////////////////
939
Jim Van Verth3af1af92017-05-18 15:06:54 -0400940static SkPoint3 map(const SkMatrix& m, const SkPoint3& pt) {
941 SkPoint3 result;
942 m.mapXY(pt.fX, pt.fY, (SkPoint*)&result.fX);
943 result.fZ = pt.fZ;
944 return result;
945}
946
947bool GrRenderTargetContext::drawFastShadow(const GrClip& clip,
Jim Van Verth3af1af92017-05-18 15:06:54 -0400948 const SkMatrix& viewMatrix,
949 const SkPath& path,
950 const SkDrawShadowRec& rec) {
Jim Van Verthc5903412016-11-17 15:27:09 -0500951 ASSERT_SINGLE_OWNER
Jim Van Verth3af1af92017-05-18 15:06:54 -0400952 if (this->drawingManager()->wasAbandoned()) {
953 return true;
954 }
Jim Van Verthc5903412016-11-17 15:27:09 -0500955 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400956 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFastShadow", fContext);
Jim Van Verth3af1af92017-05-18 15:06:54 -0400957
958 // check z plane
959 bool tiltZPlane = SkToBool(!SkScalarNearlyZero(rec.fZPlaneParams.fX) ||
960 !SkScalarNearlyZero(rec.fZPlaneParams.fY));
961 bool skipAnalytic = SkToBool(rec.fFlags & SkShadowFlags::kGeometricOnly_ShadowFlag);
962 if (tiltZPlane || skipAnalytic || !viewMatrix.rectStaysRect() || !viewMatrix.isSimilarity()) {
963 return false;
964 }
965
966 SkRRect rrect;
967 SkRect rect;
968 // we can only handle rects, circles, and rrects with circular corners
Mike Reed242135a2018-02-22 13:41:39 -0500969 bool isRRect = path.isRRect(&rrect) && SkRRectPriv::IsSimpleCircular(rrect) &&
Jim Van Verth3af1af92017-05-18 15:06:54 -0400970 rrect.radii(SkRRect::kUpperLeft_Corner).fX > SK_ScalarNearlyZero;
971 if (!isRRect &&
972 path.isOval(&rect) && SkScalarNearlyEqual(rect.width(), rect.height()) &&
973 rect.width() > SK_ScalarNearlyZero) {
974 rrect.setOval(rect);
975 isRRect = true;
976 }
977 if (!isRRect && path.isRect(&rect)) {
978 rrect.setRect(rect);
979 isRRect = true;
980 }
981
982 if (!isRRect) {
983 return false;
984 }
985
Jim Van Verthc5903412016-11-17 15:27:09 -0500986 if (rrect.isEmpty()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -0400987 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -0500988 }
989
Robert Phillips72152832017-01-25 17:31:35 -0500990 AutoCheckFlush acf(this->drawingManager());
Jim Van Verthc5903412016-11-17 15:27:09 -0500991
Jim Van Verth3af1af92017-05-18 15:06:54 -0400992 // transform light
993 SkPoint3 devLightPos = map(viewMatrix, rec.fLightPos);
994
995 // 1/scale
996 SkScalar devToSrcScale = viewMatrix.isScaleTranslate() ?
997 SkScalarInvert(viewMatrix[SkMatrix::kMScaleX]) :
998 sk_float_rsqrt(viewMatrix[SkMatrix::kMScaleX] * viewMatrix[SkMatrix::kMScaleX] +
999 viewMatrix[SkMatrix::kMSkewX] * viewMatrix[SkMatrix::kMSkewX]);
1000
1001 SkScalar occluderHeight = rec.fZPlaneParams.fZ;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001002 bool transparent = SkToBool(rec.fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag);
1003
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001004 if (SkColorGetA(rec.fAmbientColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001005 SkScalar devSpaceInsetWidth = SkDrawShadowMetrics::AmbientBlurRadius(occluderHeight);
1006 const SkScalar umbraRecipAlpha = SkDrawShadowMetrics::AmbientRecipAlpha(occluderHeight);
1007 const SkScalar devSpaceAmbientBlur = devSpaceInsetWidth * umbraRecipAlpha;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001008
1009 // Outset the shadow rrect to the border of the penumbra
1010 SkScalar ambientPathOutset = devSpaceInsetWidth * devToSrcScale;
1011 SkRRect ambientRRect;
1012 SkRect outsetRect = rrect.rect().makeOutset(ambientPathOutset, ambientPathOutset);
1013 // If the rrect was an oval then its outset will also be one.
1014 // We set it explicitly to avoid errors.
1015 if (rrect.isOval()) {
1016 ambientRRect = SkRRect::MakeOval(outsetRect);
1017 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001018 SkScalar outsetRad = SkRRectPriv::GetSimpleRadii(rrect).fX + ambientPathOutset;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001019 ambientRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1020 }
1021
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001022 GrColor ambientColor = SkColorToPremulGrColor(rec.fAmbientColor);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001023 if (transparent) {
1024 // set a large inset to force a fill
1025 devSpaceInsetWidth = ambientRRect.width();
1026 }
1027 // the fraction of the blur we want to apply is devSpaceInsetWidth/devSpaceAmbientBlur,
Jim Van Verth1af03d42017-07-31 09:34:58 -04001028 // which is just 1/umbraRecipAlpha.
1029 SkScalar blurClamp = SkScalarInvert(umbraRecipAlpha);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001030
Brian Salomon05969092017-07-13 11:20:51 -04001031 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(ambientColor, viewMatrix,
1032 ambientRRect,
1033 devSpaceAmbientBlur,
1034 devSpaceInsetWidth,
1035 blurClamp);
1036 SkASSERT(op);
1037 this->addDrawOp(clip, std::move(op));
Jim Van Verthc5903412016-11-17 15:27:09 -05001038 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001039
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001040 if (SkColorGetA(rec.fSpotColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001041 SkScalar devSpaceSpotBlur;
1042 SkScalar spotScale;
1043 SkVector spotOffset;
1044 SkDrawShadowMetrics::GetSpotParams(occluderHeight, devLightPos.fX, devLightPos.fY,
1045 devLightPos.fZ, rec.fLightRadius,
1046 &devSpaceSpotBlur, &spotScale, &spotOffset);
1047 // handle scale of radius due to CTM
Jim Van Verth3af1af92017-05-18 15:06:54 -04001048 const SkScalar srcSpaceSpotBlur = devSpaceSpotBlur * devToSrcScale;
1049
Jim Van Verth3af1af92017-05-18 15:06:54 -04001050 // Adjust translate for the effect of the scale.
1051 spotOffset.fX += spotScale*viewMatrix[SkMatrix::kMTransX];
1052 spotOffset.fY += spotScale*viewMatrix[SkMatrix::kMTransY];
1053 // This offset is in dev space, need to transform it into source space.
1054 SkMatrix ctmInverse;
1055 if (viewMatrix.invert(&ctmInverse)) {
1056 ctmInverse.mapPoints(&spotOffset, 1);
1057 } else {
1058 // Since the matrix is a similarity, this should never happen, but just in case...
1059 SkDebugf("Matrix is degenerate. Will not render spot shadow correctly!\n");
1060 SkASSERT(false);
1061 }
1062
1063 // Compute the transformed shadow rrect
1064 SkRRect spotShadowRRect;
1065 SkMatrix shadowTransform;
1066 shadowTransform.setScaleTranslate(spotScale, spotScale, spotOffset.fX, spotOffset.fY);
1067 rrect.transform(shadowTransform, &spotShadowRRect);
Mike Reed242135a2018-02-22 13:41:39 -05001068 SkScalar spotRadius = SkRRectPriv::GetSimpleRadii(spotShadowRRect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001069
1070 // Compute the insetWidth
Jim Van Verth1af03d42017-07-31 09:34:58 -04001071 SkScalar blurOutset = srcSpaceSpotBlur;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001072 SkScalar insetWidth = blurOutset;
1073 if (transparent) {
1074 // If transparent, just do a fill
1075 insetWidth += spotShadowRRect.width();
1076 } else {
1077 // For shadows, instead of using a stroke we specify an inset from the penumbra
1078 // border. We want to extend this inset area so that it meets up with the caster
1079 // geometry. The inset geometry will by default already be inset by the blur width.
1080 //
1081 // We compare the min and max corners inset by the radius between the original
1082 // rrect and the shadow rrect. The distance between the two plus the difference
1083 // between the scaled radius and the original radius gives the distance from the
1084 // transformed shadow shape to the original shape in that corner. The max
1085 // of these gives the maximum distance we need to cover.
1086 //
1087 // Since we are outsetting by 1/2 the blur distance, we just add the maxOffset to
1088 // that to get the full insetWidth.
1089 SkScalar maxOffset;
1090 if (rrect.isRect()) {
1091 // Manhattan distance works better for rects
1092 maxOffset = SkTMax(SkTMax(SkTAbs(spotShadowRRect.rect().fLeft -
1093 rrect.rect().fLeft),
1094 SkTAbs(spotShadowRRect.rect().fTop -
1095 rrect.rect().fTop)),
1096 SkTMax(SkTAbs(spotShadowRRect.rect().fRight -
1097 rrect.rect().fRight),
1098 SkTAbs(spotShadowRRect.rect().fBottom -
1099 rrect.rect().fBottom)));
1100 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001101 SkScalar dr = spotRadius - SkRRectPriv::GetSimpleRadii(rrect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001102 SkPoint upperLeftOffset = SkPoint::Make(spotShadowRRect.rect().fLeft -
1103 rrect.rect().fLeft + dr,
1104 spotShadowRRect.rect().fTop -
1105 rrect.rect().fTop + dr);
1106 SkPoint lowerRightOffset = SkPoint::Make(spotShadowRRect.rect().fRight -
1107 rrect.rect().fRight - dr,
1108 spotShadowRRect.rect().fBottom -
1109 rrect.rect().fBottom - dr);
Cary Clarkdf429f32017-11-08 11:44:31 -05001110 maxOffset = SkScalarSqrt(SkTMax(SkPointPriv::LengthSqd(upperLeftOffset),
1111 SkPointPriv::LengthSqd(lowerRightOffset))) + dr;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001112 }
1113 insetWidth += maxOffset;
1114 }
1115
1116 // Outset the shadow rrect to the border of the penumbra
1117 SkRect outsetRect = spotShadowRRect.rect().makeOutset(blurOutset, blurOutset);
1118 if (spotShadowRRect.isOval()) {
1119 spotShadowRRect = SkRRect::MakeOval(outsetRect);
1120 } else {
1121 SkScalar outsetRad = spotRadius + blurOutset;
1122 spotShadowRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1123 }
1124
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001125 GrColor spotColor = SkColorToPremulGrColor(rec.fSpotColor);
Jim Van Verth34d6e4b2017-06-09 11:09:03 -04001126
Brian Salomon05969092017-07-13 11:20:51 -04001127 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(spotColor, viewMatrix,
1128 spotShadowRRect,
Jim Van Verth1af03d42017-07-31 09:34:58 -04001129 2.0f * devSpaceSpotBlur,
Brian Salomon05969092017-07-13 11:20:51 -04001130 insetWidth);
1131 SkASSERT(op);
1132 this->addDrawOp(clip, std::move(op));
Jim Van Verth3af1af92017-05-18 15:06:54 -04001133 }
1134
1135 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001136}
1137
1138///////////////////////////////////////////////////////////////////////////////
1139
Brian Osman11052242016-10-27 14:47:55 -04001140bool GrRenderTargetContext::drawFilledDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001141 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001142 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001143 const SkMatrix& viewMatrix,
1144 const SkRRect& origOuter,
1145 const SkRRect& origInner) {
robertphillips00095892016-02-29 13:50:40 -08001146 SkASSERT(!origInner.isEmpty());
1147 SkASSERT(!origOuter.isEmpty());
1148
Brian Salomon65749212017-12-01 16:01:47 -05001149 SkTCopyOnFirstWrite<SkRRect> inner(origInner), outer(origOuter);
1150
Brian Salomon45839f92017-12-04 09:02:35 -05001151 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
1152
1153 if (GrAAType::kMSAA == aaType) {
1154 return false;
1155 }
1156
Mike Reed242135a2018-02-22 13:41:39 -05001157 if (GrAAType::kCoverage == aaType && SkRRectPriv::IsCircle(*inner)
1158 && SkRRectPriv::IsCircle(*outer)) {
Brian Salomon65749212017-12-01 16:01:47 -05001159 auto outerR = outer->width() / 2.f;
1160 auto innerR = inner->width() / 2.f;
1161 auto cx = outer->getBounds().fLeft + outerR;
1162 auto cy = outer->getBounds().fTop + outerR;
1163 if (SkScalarNearlyEqual(cx, inner->getBounds().fLeft + innerR) &&
1164 SkScalarNearlyEqual(cy, inner->getBounds().fTop + innerR)) {
1165 auto avgR = (innerR + outerR) / 2.f;
1166 auto circleBounds = SkRect::MakeLTRB(cx - avgR, cy - avgR, cx + avgR, cy + avgR);
1167 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
1168 stroke.setStrokeStyle(outerR - innerR);
Brian Salomonea26d6b2018-01-23 20:33:21 +00001169 auto op = GrOvalOpFactory::MakeOvalOp(std::move(paint), viewMatrix, circleBounds,
1170 stroke, this->caps()->shaderCaps());
Brian Salomon65749212017-12-01 16:01:47 -05001171 if (op) {
1172 this->addDrawOp(clip, std::move(op));
1173 return true;
1174 }
1175 }
1176 }
1177
Ethan Nicholas0f3c7322017-11-09 14:51:17 -05001178 GrClipEdgeType innerEdgeType, outerEdgeType;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001179 if (GrAAType::kCoverage == aaType) {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001180 innerEdgeType = GrClipEdgeType::kInverseFillAA;
1181 outerEdgeType = GrClipEdgeType::kFillAA;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001182 } else {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001183 innerEdgeType = GrClipEdgeType::kInverseFillBW;
1184 outerEdgeType = GrClipEdgeType::kFillBW;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001185 }
robertphillips00095892016-02-29 13:50:40 -08001186
robertphillips00095892016-02-29 13:50:40 -08001187 SkMatrix inverseVM;
1188 if (!viewMatrix.isIdentity()) {
1189 if (!origInner.transform(viewMatrix, inner.writable())) {
1190 return false;
1191 }
1192 if (!origOuter.transform(viewMatrix, outer.writable())) {
1193 return false;
1194 }
1195 if (!viewMatrix.invert(&inverseVM)) {
1196 return false;
1197 }
1198 } else {
1199 inverseVM.reset();
halcanary9d524f22016-03-29 09:03:52 -07001200 }
robertphillips00095892016-02-29 13:50:40 -08001201
Brian Salomon14471772017-12-05 10:35:15 -05001202 const auto& caps = *this->caps()->shaderCaps();
robertphillips00095892016-02-29 13:50:40 -08001203 // TODO these need to be a geometry processors
Brian Salomon14471772017-12-05 10:35:15 -05001204 auto innerEffect = GrRRectEffect::Make(innerEdgeType, *inner, caps);
robertphillips00095892016-02-29 13:50:40 -08001205 if (!innerEffect) {
1206 return false;
1207 }
1208
Brian Salomon14471772017-12-05 10:35:15 -05001209 auto outerEffect = GrRRectEffect::Make(outerEdgeType, *outer, caps);
robertphillips00095892016-02-29 13:50:40 -08001210 if (!outerEffect) {
1211 return false;
1212 }
1213
Brian Salomon82f44312017-01-11 13:42:54 -05001214 paint.addCoverageFragmentProcessor(std::move(innerEffect));
1215 paint.addCoverageFragmentProcessor(std::move(outerEffect));
robertphillips00095892016-02-29 13:50:40 -08001216
1217 SkRect bounds = outer->getBounds();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001218 if (GrAAType::kCoverage == aaType) {
robertphillips00095892016-02-29 13:50:40 -08001219 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1220 }
halcanary9d524f22016-03-29 09:03:52 -07001221
Brian Salomon82f44312017-01-11 13:42:54 -05001222 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), bounds,
1223 inverseVM);
robertphillips00095892016-02-29 13:50:40 -08001224 return true;
1225}
1226
Brian Osman11052242016-10-27 14:47:55 -04001227void GrRenderTargetContext::drawDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001228 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001229 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001230 const SkMatrix& viewMatrix,
1231 const SkRRect& outer,
1232 const SkRRect& inner) {
robertphillips00095892016-02-29 13:50:40 -08001233 ASSERT_SINGLE_OWNER
1234 RETURN_IF_ABANDONED
1235 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001236 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawDRRect", fContext);
robertphillips00095892016-02-29 13:50:40 -08001237
1238 SkASSERT(!outer.isEmpty());
1239 SkASSERT(!inner.isEmpty());
1240
Robert Phillips72152832017-01-25 17:31:35 -05001241 AutoCheckFlush acf(this->drawingManager());
robertphillips00095892016-02-29 13:50:40 -08001242
Brian Salomon82f44312017-01-11 13:42:54 -05001243 if (this->drawFilledDRRect(clip, std::move(paint), aa, viewMatrix, outer, inner)) {
robertphillips00095892016-02-29 13:50:40 -08001244 return;
1245 }
1246
1247 SkPath path;
1248 path.setIsVolatile(true);
1249 path.addRRect(inner);
1250 path.addRRect(outer);
1251 path.setFillType(SkPath::kEvenOdd_FillType);
Brian Salomon2fad74a2017-12-20 13:28:55 -05001252 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, GrShape(path));
robertphillips00095892016-02-29 13:50:40 -08001253}
1254
robertphillipsea461502015-05-26 11:38:03 -07001255///////////////////////////////////////////////////////////////////////////////
1256
Brian Osman11052242016-10-27 14:47:55 -04001257void GrRenderTargetContext::drawRegion(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001258 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001259 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001260 const SkMatrix& viewMatrix,
1261 const SkRegion& region,
Stan Iliev73d8fd92017-08-02 15:36:24 -04001262 const GrStyle& style,
1263 const GrUserStencilSettings* ss) {
msarettcc319b92016-08-25 18:07:18 -07001264 ASSERT_SINGLE_OWNER
1265 RETURN_IF_ABANDONED
1266 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001267 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRegion", fContext);
msarettcc319b92016-08-25 18:07:18 -07001268
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001269 if (GrAA::kYes == aa) {
Brian Salomonfc527d22016-12-14 21:07:01 -05001270 // GrRegionOp performs no antialiasing but is much faster, so here we check the matrix
Brian Salomonc57c7c92016-12-06 14:47:34 -05001271 // to see whether aa is really required.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001272 if (!SkToBool(viewMatrix.getType() & ~(SkMatrix::kTranslate_Mask)) &&
Brian Salomon34169692017-08-28 15:32:01 -04001273 SkScalarIsInt(viewMatrix.getTranslateX()) &&
1274 SkScalarIsInt(viewMatrix.getTranslateY())) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001275 aa = GrAA::kNo;
1276 }
Brian Salomonc57c7c92016-12-06 14:47:34 -05001277 }
msarettcc319b92016-08-25 18:07:18 -07001278 bool complexStyle = !style.isSimpleFill();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001279 if (complexStyle || GrAA::kYes == aa) {
msarettcc319b92016-08-25 18:07:18 -07001280 SkPath path;
1281 region.getBoundaryPath(&path);
Brian Salomon82f44312017-01-11 13:42:54 -05001282 return this->drawPath(clip, std::move(paint), aa, viewMatrix, path, style);
msarettcc319b92016-08-25 18:07:18 -07001283 }
1284
Brian Salomonf0366322017-07-11 15:53:05 -04001285 GrAAType aaType = this->chooseAAType(GrAA::kNo, GrAllowMixedSamples::kNo);
Stan Iliev73d8fd92017-08-02 15:36:24 -04001286 std::unique_ptr<GrDrawOp> op = GrRegionOp::Make(std::move(paint), viewMatrix, region, aaType,
1287 ss);
Brian Salomonf0366322017-07-11 15:53:05 -04001288 this->addDrawOp(clip, std::move(op));
msarettcc319b92016-08-25 18:07:18 -07001289}
1290
Brian Osman11052242016-10-27 14:47:55 -04001291void GrRenderTargetContext::drawOval(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001292 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001293 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001294 const SkMatrix& viewMatrix,
1295 const SkRect& oval,
1296 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001297 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001298 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001299 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001300 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawOval", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -07001301
robertphillipsea461502015-05-26 11:38:03 -07001302 if (oval.isEmpty()) {
1303 return;
1304 }
1305
bsalomon6663acf2016-05-10 09:14:17 -07001306 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
robertphillipsea461502015-05-26 11:38:03 -07001307
Robert Phillips72152832017-01-25 17:31:35 -05001308 AutoCheckFlush acf(this->drawingManager());
bsalomon6663acf2016-05-10 09:14:17 -07001309 const SkStrokeRec& stroke = style.strokeRec();
csmartdaltona7f29642016-07-07 08:49:11 -07001310
Brian Salomonea26d6b2018-01-23 20:33:21 +00001311 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001312 if (GrAAType::kCoverage == aaType) {
Brian Salomon94efbf52016-11-29 13:43:05 -05001313 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomonea26d6b2018-01-23 20:33:21 +00001314 std::unique_ptr<GrDrawOp> op =
1315 GrOvalOpFactory::MakeOvalOp(std::move(paint), viewMatrix, oval, stroke, shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -05001316 if (op) {
Brian Salomon05441c42017-05-15 16:45:49 -04001317 this->addDrawOp(clip, std::move(op));
robertphillipsb56f9272016-02-25 11:03:52 -08001318 return;
1319 }
robertphillipsea461502015-05-26 11:38:03 -07001320 }
robertphillipsb56f9272016-02-25 11:03:52 -08001321
Brian Salomon2fad74a2017-12-20 13:28:55 -05001322 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix,
1323 GrShape(SkRRect::MakeOval(oval), style));
robertphillipsea461502015-05-26 11:38:03 -07001324}
1325
Brian Osman11052242016-10-27 14:47:55 -04001326void GrRenderTargetContext::drawArc(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001327 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001328 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001329 const SkMatrix& viewMatrix,
1330 const SkRect& oval,
1331 SkScalar startAngle,
1332 SkScalar sweepAngle,
1333 bool useCenter,
1334 const GrStyle& style) {
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001335 ASSERT_SINGLE_OWNER
1336 RETURN_IF_ABANDONED
1337 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001338 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawArc", fContext);
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001339
1340 AutoCheckFlush acf(this->drawingManager());
1341
Brian Salomonea26d6b2018-01-23 20:33:21 +00001342 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001343 if (GrAAType::kCoverage == aaType) {
Brian Salomon94efbf52016-11-29 13:43:05 -05001344 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomonea26d6b2018-01-23 20:33:21 +00001345 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeArcOp(std::move(paint),
1346 viewMatrix,
1347 oval,
1348 startAngle,
1349 sweepAngle,
1350 useCenter,
1351 style,
1352 shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -05001353 if (op) {
Brian Salomon05441c42017-05-15 16:45:49 -04001354 this->addDrawOp(clip, std::move(op));
bsalomon4f3a0ca2016-08-22 13:14:26 -07001355 return;
1356 }
1357 }
1358 SkPath path;
bsalomon21af9ca2016-08-25 12:29:23 -07001359 SkPathPriv::CreateDrawArcPath(&path, oval, startAngle, sweepAngle, useCenter,
1360 style.isSimpleFill());
Brian Salomon2fad74a2017-12-20 13:28:55 -05001361 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, GrShape(path, style));
bsalomon4f3a0ca2016-08-22 13:14:26 -07001362}
1363
Brian Osman11052242016-10-27 14:47:55 -04001364void GrRenderTargetContext::drawImageLattice(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001365 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001366 const SkMatrix& viewMatrix,
1367 int imageWidth,
1368 int imageHeight,
1369 std::unique_ptr<SkLatticeIter> iter,
1370 const SkRect& dst) {
joshualitt1de610a2016-01-06 08:26:09 -08001371 ASSERT_SINGLE_OWNER
joshualitt33a5fce2015-11-18 13:28:51 -08001372 RETURN_IF_ABANDONED
1373 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001374 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawImageLattice", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001375
Robert Phillips72152832017-01-25 17:31:35 -05001376 AutoCheckFlush acf(this->drawingManager());
joshualitt33a5fce2015-11-18 13:28:51 -08001377
Brian Salomon815486c2017-07-11 08:52:13 -04001378 std::unique_ptr<GrDrawOp> op = GrLatticeOp::MakeNonAA(std::move(paint), viewMatrix, imageWidth,
1379 imageHeight, std::move(iter), dst);
1380 this->addDrawOp(clip, std::move(op));
joshualitt33a5fce2015-11-18 13:28:51 -08001381}
1382
Greg Daniel51316782017-08-02 15:10:09 +00001383GrSemaphoresSubmitted GrRenderTargetContext::prepareForExternalIO(
1384 int numSemaphores, GrBackendSemaphore backendSemaphores[]) {
robertphillips8c523e02016-07-26 07:41:00 -07001385 ASSERT_SINGLE_OWNER
Greg Daniel51316782017-08-02 15:10:09 +00001386 if (this->drawingManager()->wasAbandoned()) { return GrSemaphoresSubmitted::kNo; }
robertphillips8c523e02016-07-26 07:41:00 -07001387 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001388 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "prepareForExternalIO", fContext);
robertphillips8c523e02016-07-26 07:41:00 -07001389
Greg Daniel51316782017-08-02 15:10:09 +00001390 return this->drawingManager()->prepareSurfaceForExternalIO(fRenderTargetProxy.get(),
1391 numSemaphores,
1392 backendSemaphores);
Greg Daniela5cb7812017-06-16 09:45:32 -04001393}
1394
Greg Danielc64ee462017-06-15 16:59:49 -04001395bool GrRenderTargetContext::waitOnSemaphores(int numSemaphores,
Greg Daniela5cb7812017-06-16 09:45:32 -04001396 const GrBackendSemaphore* waitSemaphores) {
1397 ASSERT_SINGLE_OWNER
Greg Danielc64ee462017-06-15 16:59:49 -04001398 RETURN_FALSE_IF_ABANDONED
Greg Daniela5cb7812017-06-16 09:45:32 -04001399 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001400 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "waitOnSemaphores", fContext);
Greg Daniela5cb7812017-06-16 09:45:32 -04001401
1402 AutoCheckFlush acf(this->drawingManager());
1403
Greg Danielc64ee462017-06-15 16:59:49 -04001404 if (numSemaphores && !this->caps()->fenceSyncSupport()) {
1405 return false;
1406 }
1407
Robert Phillips6be756b2018-01-16 15:07:54 -05001408 auto resourceProvider = fContext->contextPriv().resourceProvider();
1409
Greg Daniela5cb7812017-06-16 09:45:32 -04001410 SkTArray<sk_sp<GrSemaphore>> semaphores(numSemaphores);
1411 for (int i = 0; i < numSemaphores; ++i) {
Robert Phillips6be756b2018-01-16 15:07:54 -05001412 sk_sp<GrSemaphore> sema = resourceProvider->wrapBackendSemaphore(
Greg Daniel17b7c052018-01-09 13:55:33 -05001413 waitSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillWait,
1414 kAdopt_GrWrapOwnership);
Greg Daniela5cb7812017-06-16 09:45:32 -04001415 std::unique_ptr<GrOp> waitOp(GrSemaphoreOp::MakeWait(sema, fRenderTargetProxy.get()));
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001416 this->getRTOpList()->addOp(std::move(waitOp), *this->caps());
Greg Daniela5cb7812017-06-16 09:45:32 -04001417 }
Greg Danielc64ee462017-06-15 16:59:49 -04001418 return true;
robertphillips8c523e02016-07-26 07:41:00 -07001419}
joshualitt33a5fce2015-11-18 13:28:51 -08001420
Robert Phillips65a88fa2017-08-08 08:36:22 -04001421void GrRenderTargetContext::insertEventMarker(const SkString& str) {
1422 std::unique_ptr<GrOp> op(GrDebugMarkerOp::Make(fRenderTargetProxy.get(), str));
1423 this->getRTOpList()->addOp(std::move(op), *this->caps());
1424}
1425
1426
robertphillipsea461502015-05-26 11:38:03 -07001427// Can 'path' be drawn as a pair of filled nested rectangles?
bsalomon6663acf2016-05-10 09:14:17 -07001428static bool fills_as_nested_rects(const SkMatrix& viewMatrix, const SkPath& path, SkRect rects[2]) {
robertphillipsea461502015-05-26 11:38:03 -07001429
1430 if (path.isInverseFillType()) {
1431 return false;
1432 }
1433
1434 // TODO: this restriction could be lifted if we were willing to apply
1435 // the matrix to all the points individually rather than just to the rect
robertphillips0e7029e2015-11-30 05:45:06 -08001436 if (!viewMatrix.rectStaysRect()) {
robertphillipsea461502015-05-26 11:38:03 -07001437 return false;
1438 }
1439
1440 SkPath::Direction dirs[2];
1441 if (!path.isNestedFillRects(rects, dirs)) {
1442 return false;
1443 }
1444
1445 if (SkPath::kWinding_FillType == path.getFillType() && dirs[0] == dirs[1]) {
1446 // The two rects need to be wound opposite to each other
1447 return false;
1448 }
1449
1450 // Right now, nested rects where the margin is not the same width
1451 // all around do not render correctly
1452 const SkScalar* outer = rects[0].asScalars();
1453 const SkScalar* inner = rects[1].asScalars();
1454
1455 bool allEq = true;
1456
1457 SkScalar margin = SkScalarAbs(outer[0] - inner[0]);
1458 bool allGoE1 = margin >= SK_Scalar1;
1459
1460 for (int i = 1; i < 4; ++i) {
1461 SkScalar temp = SkScalarAbs(outer[i] - inner[i]);
1462 if (temp < SK_Scalar1) {
1463 allGoE1 = false;
1464 }
1465 if (!SkScalarNearlyEqual(margin, temp)) {
1466 allEq = false;
1467 }
1468 }
1469
1470 return allEq || allGoE1;
1471}
1472
Brian Osman11052242016-10-27 14:47:55 -04001473void GrRenderTargetContext::drawPath(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001474 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001475 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001476 const SkMatrix& viewMatrix,
Brian Salomon40b77a62017-12-22 11:25:52 -05001477 const SkPath& path,
Brian Osman11052242016-10-27 14:47:55 -04001478 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001479 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001480 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001481 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001482 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawPath", fContext);
Brian Salomon40b77a62017-12-22 11:25:52 -05001483
1484 GrShape shape(path, style);
Brian Salomon2fad74a2017-12-20 13:28:55 -05001485 if (shape.isEmpty()) {
1486 if (shape.inverseFilled()) {
1487 this->drawPaint(clip, std::move(paint), viewMatrix);
1488 }
1489 return;
robertphillipsea461502015-05-26 11:38:03 -07001490 }
1491
Robert Phillips72152832017-01-25 17:31:35 -05001492 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -07001493
Brian Salomon2fad74a2017-12-20 13:28:55 -05001494 if (!shape.style().hasPathEffect()) {
1495 SkRRect rrect;
1496 // We can ignore the starting point and direction since there is no path effect.
1497 bool inverted;
1498 if (shape.asRRect(&rrect, nullptr, nullptr, &inverted) && !inverted) {
1499 if (rrect.isRect()) {
1500 this->drawRect(clip, std::move(paint), aa, viewMatrix, rrect.rect(),
1501 &shape.style());
1502 return;
1503 } else if (rrect.isOval()) {
1504 this->drawOval(clip, std::move(paint), aa, viewMatrix, rrect.rect(), shape.style());
robertphillipsea461502015-05-26 11:38:03 -07001505 return;
1506 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05001507 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect, shape.style());
1508 return;
robertphillipsea461502015-05-26 11:38:03 -07001509 }
1510 }
robertphillips4bc31812016-03-01 12:22:49 -08001511
Brian Salomon40b77a62017-12-22 11:25:52 -05001512 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
1513 if (GrAAType::kCoverage == aaType) {
1514 // TODO: Make GrShape check for nested rects.
1515 SkRect rects[2];
1516 if (shape.style().isSimpleFill() && fills_as_nested_rects(viewMatrix, path, rects)) {
1517 // Concave AA paths are expensive - try to avoid them for special cases
1518 SkRect rects[2];
1519
1520 if (fills_as_nested_rects(viewMatrix, path, rects)) {
1521 std::unique_ptr<GrDrawOp> op =
1522 GrRectOpFactory::MakeAAFillNestedRects(std::move(paint), viewMatrix, rects);
1523 if (op) {
1524 this->addDrawOp(clip, std::move(op));
1525 }
1526 // A null return indicates that there is nothing to draw in this case.
1527 return;
1528 }
1529 }
1530 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05001531 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, shape);
robertphillipsea461502015-05-26 11:38:03 -07001532}
1533
Chris Daltonbbfd5162017-11-07 13:35:22 -07001534bool GrRenderTargetContextPriv::drawAndStencilPath(const GrHardClip& clip,
Brian Osman11052242016-10-27 14:47:55 -04001535 const GrUserStencilSettings* ss,
1536 SkRegion::Op op,
1537 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001538 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001539 const SkMatrix& viewMatrix,
1540 const SkPath& path) {
robertphillips391395d2016-03-02 09:26:36 -08001541 ASSERT_SINGLE_OWNER_PRIV
1542 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -04001543 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001544 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawAndStencilPath",
1545 fRenderTargetContext->fContext);
robertphillips391395d2016-03-02 09:26:36 -08001546
1547 if (path.isEmpty() && path.isInverseFillType()) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001548 this->drawAndStencilRect(clip, ss, op, invert, GrAA::kNo, SkMatrix::I(),
Brian Osman11052242016-10-27 14:47:55 -04001549 SkRect::MakeIWH(fRenderTargetContext->width(),
1550 fRenderTargetContext->height()));
robertphillips391395d2016-03-02 09:26:36 -08001551 return true;
1552 }
1553
Robert Phillips72152832017-01-25 17:31:35 -05001554 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -08001555
1556 // An Assumption here is that path renderer would use some form of tweaking
1557 // the src color (either the input alpha or in the frag shader) to implement
1558 // aa. If we have some future driver-mojo path AA that can do the right
1559 // thing WRT to the blend then we'll need some query on the PR.
Brian Salomon7c8460e2017-05-12 11:36:10 -04001560 GrAAType aaType = fRenderTargetContext->chooseAAType(aa, GrAllowMixedSamples::kNo);
robertphillips976f5f02016-06-03 10:59:20 -07001561 bool hasUserStencilSettings = !ss->isUnused();
robertphillips391395d2016-03-02 09:26:36 -08001562
Chris Daltondb91c6e2017-09-08 16:25:08 -06001563 SkIRect clipConservativeBounds;
1564 clip.getConservativeBounds(fRenderTargetContext->width(), fRenderTargetContext->height(),
1565 &clipConservativeBounds, nullptr);
1566
bsalomon8acedde2016-06-24 10:42:16 -07001567 GrShape shape(path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -08001568 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Eric Karl5c779752017-05-08 12:02:07 -07001569 canDrawArgs.fCaps = fRenderTargetContext->drawingManager()->getContext()->caps();
robertphillips391395d2016-03-02 09:26:36 -08001570 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07001571 canDrawArgs.fShape = &shape;
Chris Daltondb91c6e2017-09-08 16:25:08 -06001572 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001573 canDrawArgs.fAAType = aaType;
cdalton93a379b2016-05-11 13:58:08 -07001574 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips391395d2016-03-02 09:26:36 -08001575
1576 // Don't allow the SW renderer
Robert Phillips72152832017-01-25 17:31:35 -05001577 GrPathRenderer* pr = fRenderTargetContext->drawingManager()->getPathRenderer(
Brian Salomon36aa1762016-12-10 13:24:02 -05001578 canDrawArgs, false, GrPathRendererChain::DrawType::kStencilAndColor);
robertphillips391395d2016-03-02 09:26:36 -08001579 if (!pr) {
1580 return false;
1581 }
1582
1583 GrPaint paint;
1584 paint.setCoverageSetOpXPFactory(op, invert);
1585
Brian Salomonf3569f02017-10-24 12:52:33 -04001586 GrPathRenderer::DrawPathArgs args{fRenderTargetContext->drawingManager()->getContext(),
1587 std::move(paint),
1588 ss,
1589 fRenderTargetContext,
1590 &clip,
1591 &clipConservativeBounds,
1592 &viewMatrix,
1593 &shape,
1594 aaType,
1595 fRenderTargetContext->colorSpaceInfo().isGammaCorrect()};
robertphillips391395d2016-03-02 09:26:36 -08001596 pr->drawPath(args);
1597 return true;
1598}
1599
Brian Osman11052242016-10-27 14:47:55 -04001600SkBudgeted GrRenderTargetContextPriv::isBudgeted() const {
robertphillips714712b2016-08-04 06:20:45 -07001601 ASSERT_SINGLE_OWNER_PRIV
1602
Brian Osman11052242016-10-27 14:47:55 -04001603 if (fRenderTargetContext->wasAbandoned()) {
robertphillips714712b2016-08-04 06:20:45 -07001604 return SkBudgeted::kNo;
1605 }
1606
Brian Osman11052242016-10-27 14:47:55 -04001607 SkDEBUGCODE(fRenderTargetContext->validate();)
robertphillips714712b2016-08-04 06:20:45 -07001608
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001609 return fRenderTargetContext->fRenderTargetProxy->isBudgeted();
robertphillips714712b2016-08-04 06:20:45 -07001610}
1611
Brian Salomon2fad74a2017-12-20 13:28:55 -05001612void GrRenderTargetContext::drawShapeUsingPathRenderer(const GrClip& clip,
1613 GrPaint&& paint,
1614 GrAA aa,
1615 const SkMatrix& viewMatrix,
1616 const GrShape& originalShape) {
joshualitt1de610a2016-01-06 08:26:09 -08001617 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001618 RETURN_IF_ABANDONED
Brian Salomondcbb9d92017-07-19 10:53:20 -04001619 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "internalDrawPath", fContext);
1620
Chris Daltondb91c6e2017-09-08 16:25:08 -06001621 SkIRect clipConservativeBounds;
1622 clip.getConservativeBounds(this->width(), this->height(), &clipConservativeBounds, nullptr);
1623
Brian Salomon2fad74a2017-12-20 13:28:55 -05001624 GrShape tempShape;
Brian Salomon7c8460e2017-05-12 11:36:10 -04001625 // NVPR cannot handle hairlines, so this would get picked up by a different stencil and
1626 // cover path renderer (i.e. default path renderer). The hairline renderer produces much
1627 // smoother hairlines than MSAA.
Brian Salomon2fad74a2017-12-20 13:28:55 -05001628 GrAllowMixedSamples allowMixedSamples = originalShape.style().isSimpleHairline()
1629 ? GrAllowMixedSamples::kNo
1630 : GrAllowMixedSamples::kYes;
Brian Salomon7c8460e2017-05-12 11:36:10 -04001631 GrAAType aaType = this->chooseAAType(aa, allowMixedSamples);
robertphillips68737822015-10-29 12:12:21 -07001632 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Eric Karl5c779752017-05-08 12:02:07 -07001633 canDrawArgs.fCaps = this->drawingManager()->getContext()->caps();
robertphillips68737822015-10-29 12:12:21 -07001634 canDrawArgs.fViewMatrix = &viewMatrix;
Brian Salomon2fad74a2017-12-20 13:28:55 -05001635 canDrawArgs.fShape = &originalShape;
Chris Daltondb91c6e2017-09-08 16:25:08 -06001636 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001637 canDrawArgs.fHasUserStencilSettings = false;
robertphillips68737822015-10-29 12:12:21 -07001638
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001639 GrPathRenderer* pr;
Brian Salomon82125e92016-12-10 09:35:48 -05001640 static constexpr GrPathRendererChain::DrawType kType = GrPathRendererChain::DrawType::kColor;
Brian Salomon2fad74a2017-12-20 13:28:55 -05001641 if (originalShape.isEmpty() && !originalShape.inverseFilled()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05001642 return;
1643 }
1644
1645 canDrawArgs.fAAType = aaType;
1646
1647 // Try a 1st time without applying any of the style to the geometry (and barring sw)
1648 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
1649 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
1650
Brian Salomon2fad74a2017-12-20 13:28:55 -05001651 if (!pr && originalShape.style().pathEffect()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05001652 // It didn't work above, so try again with the path effect applied.
Brian Salomon2fad74a2017-12-20 13:28:55 -05001653 tempShape = originalShape.applyStyle(GrStyle::Apply::kPathEffectOnly, styleScale);
1654 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001655 return;
1656 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05001657 canDrawArgs.fShape = &tempShape;
Robert Phillips72152832017-01-25 17:31:35 -05001658 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05001659 }
1660 if (!pr) {
Brian Salomon2fad74a2017-12-20 13:28:55 -05001661 if (canDrawArgs.fShape->style().applies()) {
1662 tempShape = canDrawArgs.fShape->applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec,
1663 styleScale);
1664 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001665 return;
1666 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05001667 canDrawArgs.fShape = &tempShape;
bsalomon6663acf2016-05-10 09:14:17 -07001668 }
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05001669 // This time, allow SW renderer
1670 pr = this->drawingManager()->getPathRenderer(canDrawArgs, true, kType);
1671 }
robertphillipsea461502015-05-26 11:38:03 -07001672
bsalomon8acedde2016-06-24 10:42:16 -07001673 if (!pr) {
robertphillipsea461502015-05-26 11:38:03 -07001674#ifdef SK_DEBUG
1675 SkDebugf("Unable to find path renderer compatible with path.\n");
1676#endif
1677 return;
1678 }
1679
Robert Phillips256c37b2017-03-01 14:32:46 -05001680 GrPathRenderer::DrawPathArgs args{this->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05001681 std::move(paint),
1682 &GrUserStencilSettings::kUnused,
1683 this,
1684 &clip,
Chris Daltondb91c6e2017-09-08 16:25:08 -06001685 &clipConservativeBounds,
Brian Salomon82f44312017-01-11 13:42:54 -05001686 &viewMatrix,
Brian Salomon2fad74a2017-12-20 13:28:55 -05001687 canDrawArgs.fShape,
Brian Salomon82f44312017-01-11 13:42:54 -05001688 aaType,
Brian Salomonf3569f02017-10-24 12:52:33 -04001689 this->colorSpaceInfo().isGammaCorrect()};
bsalomon0aff2fa2015-07-31 06:48:27 -07001690 pr->drawPath(args);
robertphillipsea461502015-05-26 11:38:03 -07001691}
1692
Brian Salomon467921e2017-03-06 16:17:12 -05001693static void op_bounds(SkRect* bounds, const GrOp* op) {
1694 *bounds = op->bounds();
1695 if (op->hasZeroArea()) {
1696 if (op->hasAABloat()) {
1697 bounds->outset(0.5f, 0.5f);
1698 } else {
1699 // We don't know which way the particular GPU will snap lines or points at integer
1700 // coords. So we ensure that the bounds is large enough for either snap.
1701 SkRect before = *bounds;
1702 bounds->roundOut(bounds);
1703 if (bounds->fLeft == before.fLeft) {
1704 bounds->fLeft -= 1;
1705 }
1706 if (bounds->fTop == before.fTop) {
1707 bounds->fTop -= 1;
1708 }
1709 if (bounds->fRight == before.fRight) {
1710 bounds->fRight += 1;
1711 }
1712 if (bounds->fBottom == before.fBottom) {
1713 bounds->fBottom += 1;
1714 }
1715 }
1716 }
1717}
1718
Brian Salomon54d212e2017-03-21 14:22:38 -04001719uint32_t GrRenderTargetContext::addDrawOp(const GrClip& clip, std::unique_ptr<GrDrawOp> op) {
joshualitt1de610a2016-01-06 08:26:09 -08001720 ASSERT_SINGLE_OWNER
Robert Phillipsc0138922017-03-08 11:50:55 -05001721 if (this->drawingManager()->wasAbandoned()) {
1722 return SK_InvalidUniqueID;
1723 }
robertphillips2e1e51f2015-10-15 08:01:48 -07001724 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001725 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "addDrawOp", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -07001726
Brian Salomon467921e2017-03-06 16:17:12 -05001727 // Setup clip
1728 SkRect bounds;
1729 op_bounds(&bounds, op.get());
Brian Salomon97180af2017-03-14 13:42:58 -04001730 GrAppliedClip appliedClip;
Brian Salomon54d212e2017-03-21 14:22:38 -04001731 GrDrawOp::FixedFunctionFlags fixedFunctionFlags = op->fixedFunctionFlags();
1732 if (!clip.apply(fContext, this, fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesHWAA,
1733 fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil, &appliedClip,
1734 &bounds)) {
1735 return SK_InvalidUniqueID;
1736 }
1737
Brian Salomon54d212e2017-03-21 14:22:38 -04001738 if (fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil ||
1739 appliedClip.hasStencilClip()) {
Robert Phillips6b47c7d2017-08-29 07:24:09 -04001740 this->getOpList()->setStencilLoadOp(GrLoadOp::kClear);
Robert Phillips95214472017-08-08 18:00:03 -04001741
Robert Phillips65048132017-08-10 08:44:49 -04001742 this->setNeedsStencil();
Brian Salomon54d212e2017-03-21 14:22:38 -04001743 }
1744
Brian Salomonf3569f02017-10-24 12:52:33 -04001745 GrPixelConfigIsClamped dstIsClamped =
1746 GrGetPixelConfigIsClamped(this->colorSpaceInfo().config());
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001747 GrXferProcessor::DstProxy dstProxy;
Brian Osman9a725dd2017-09-20 09:53:22 -04001748 if (GrDrawOp::RequiresDstTexture::kYes == op->finalize(*this->caps(), &appliedClip,
1749 dstIsClamped)) {
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001750 if (!this->setupDstProxy(this->asRenderTargetProxy(), clip, op->bounds(), &dstProxy)) {
Brian Salomon54d212e2017-03-21 14:22:38 -04001751 return SK_InvalidUniqueID;
1752 }
1753 }
1754
1755 op->setClippedBounds(bounds);
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001756 return this->getRTOpList()->addOp(std::move(op), *this->caps(),
1757 std::move(appliedClip), dstProxy);
Brian Salomon54d212e2017-03-21 14:22:38 -04001758}
1759
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001760bool GrRenderTargetContext::setupDstProxy(GrRenderTargetProxy* rtProxy, const GrClip& clip,
Robert Phillips16d8ec62017-07-27 16:16:25 -04001761 const SkRect& opBounds,
1762 GrXferProcessor::DstProxy* dstProxy) {
Brian Salomon467921e2017-03-06 16:17:12 -05001763 if (this->caps()->textureBarrierSupport()) {
Robert Phillipsbf25d432017-04-07 10:08:53 -04001764 if (GrTextureProxy* texProxy = rtProxy->asTextureProxy()) {
Brian Salomon467921e2017-03-06 16:17:12 -05001765 // The render target is a texture, so we can read from it directly in the shader. The XP
1766 // will be responsible to detect this situation and request a texture barrier.
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001767 dstProxy->setProxy(sk_ref_sp(texProxy));
1768 dstProxy->setOffset(0, 0);
Robert Phillipsbf25d432017-04-07 10:08:53 -04001769 return true;
Brian Salomon467921e2017-03-06 16:17:12 -05001770 }
1771 }
1772
Robert Phillipsbf25d432017-04-07 10:08:53 -04001773 SkIRect copyRect = SkIRect::MakeWH(rtProxy->width(), rtProxy->height());
Brian Salomon467921e2017-03-06 16:17:12 -05001774
Eric Karl74480882017-04-03 14:49:05 -07001775 SkIRect clippedRect;
Robert Phillipsbf25d432017-04-07 10:08:53 -04001776 clip.getConservativeBounds(rtProxy->width(), rtProxy->height(), &clippedRect);
Eric Karl72e551e2017-04-04 13:42:10 -07001777 SkIRect drawIBounds;
Brian Salomon467921e2017-03-06 16:17:12 -05001778 opBounds.roundOut(&drawIBounds);
Brian Salomon859621f2017-03-16 09:21:54 -04001779 // Cover up for any precision issues by outsetting the op bounds a pixel in each direction.
1780 drawIBounds.outset(1, 1);
Eric Karl72e551e2017-04-04 13:42:10 -07001781 if (!clippedRect.intersect(drawIBounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -05001782#ifdef SK_DEBUG
Robert Phillipsbf25d432017-04-07 10:08:53 -04001783 GrCapsDebugf(this->caps(), "setupDstTexture: Missed an early reject bailing on draw.");
Brian Salomon467921e2017-03-06 16:17:12 -05001784#endif
Robert Phillipsbf25d432017-04-07 10:08:53 -04001785 return false;
Brian Salomon467921e2017-03-06 16:17:12 -05001786 }
1787
1788 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
1789 // have per-sample dst values by making the copy multisampled.
1790 GrSurfaceDesc desc;
Eric Karl74480882017-04-03 14:49:05 -07001791 bool rectsMustMatch = false;
1792 bool disallowSubrect = false;
Robert Phillipsbf25d432017-04-07 10:08:53 -04001793 if (!this->caps()->initDescForDstCopy(rtProxy, &desc, &rectsMustMatch, &disallowSubrect)) {
Brian Salomon467921e2017-03-06 16:17:12 -05001794 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillips16d8ec62017-07-27 16:16:25 -04001795 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
Robert Phillipsbf25d432017-04-07 10:08:53 -04001796 desc.fConfig = rtProxy->config();
Brian Salomon467921e2017-03-06 16:17:12 -05001797 }
1798
Eric Karl74480882017-04-03 14:49:05 -07001799 if (!disallowSubrect) {
1800 copyRect = clippedRect;
1801 }
Brian Salomon467921e2017-03-06 16:17:12 -05001802
Robert Phillipsbf25d432017-04-07 10:08:53 -04001803 SkIPoint dstPoint, dstOffset;
1804 SkBackingFit fit;
Eric Karl74480882017-04-03 14:49:05 -07001805 if (rectsMustMatch) {
Robert Phillipsbf25d432017-04-07 10:08:53 -04001806 SkASSERT(desc.fOrigin == rtProxy->origin());
1807 desc.fWidth = rtProxy->width();
1808 desc.fHeight = rtProxy->height();
Eric Karl74480882017-04-03 14:49:05 -07001809 dstPoint = {copyRect.fLeft, copyRect.fTop};
1810 dstOffset = {0, 0};
Robert Phillipsbf25d432017-04-07 10:08:53 -04001811 fit = SkBackingFit::kExact;
Eric Karl74480882017-04-03 14:49:05 -07001812 } else {
1813 desc.fWidth = copyRect.width();
1814 desc.fHeight = copyRect.height();
1815 dstPoint = {0, 0};
1816 dstOffset = {copyRect.fLeft, copyRect.fTop};
Robert Phillipsbf25d432017-04-07 10:08:53 -04001817 fit = SkBackingFit::kApprox;
Eric Karl74480882017-04-03 14:49:05 -07001818 }
Brian Salomon467921e2017-03-06 16:17:12 -05001819
Robert Phillipsbf25d432017-04-07 10:08:53 -04001820 sk_sp<GrSurfaceContext> sContext = fContext->contextPriv().makeDeferredSurfaceContext(
Brian Salomonf802e752018-02-13 17:13:31 -05001821 desc, GrMipMapped::kNo, fit, SkBudgeted::kYes,
1822 sk_ref_sp(this->colorSpaceInfo().colorSpace()));
Robert Phillipsbf25d432017-04-07 10:08:53 -04001823 if (!sContext) {
1824 SkDebugf("setupDstTexture: surfaceContext creation failed.\n");
1825 return false;
1826 }
1827
1828 if (!sContext->copy(rtProxy, copyRect, dstPoint)) {
1829 SkDebugf("setupDstTexture: copy failed.\n");
1830 return false;
1831 }
1832
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001833 dstProxy->setProxy(sContext->asTextureProxyRef());
1834 dstProxy->setOffset(dstOffset);
Robert Phillipsbf25d432017-04-07 10:08:53 -04001835 return true;
robertphillips2334fb62015-06-17 05:43:33 -07001836}