blob: f4db697e4137106408b2da59d8bce3c0ce8e6086 [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"
Herb Derby26cbe512018-05-24 14:39:01 -040051#include "text/GrTextContext.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
Herb Derby26cbe512018-05-24 14:39:01 -0400229 GrTextContext* atlasTextContext = this->drawingManager()->getTextContext();
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
Herb Derby26cbe512018-05-24 14:39:01 -0400244 GrTextContext* atlasTextContext = this->drawingManager()->getTextContext();
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
Herb Derby26cbe512018-05-24 14:39:01 -0400259 GrTextContext* atlasTextContext = this->drawingManager()->getTextContext();
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 &&
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400343 this->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 return;
393 }
Brian Salomon82f44312017-01-11 13:42:54 -0500394 this->drawRect(clip, std::move(paint), GrAA::kNo, viewMatrix, r);
robertphillipsea461502015-05-26 11:38:03 -0700395 } else {
396 SkMatrix localMatrix;
397 if (!viewMatrix.invert(&localMatrix)) {
robertphillipsea461502015-05-26 11:38:03 -0700398 return;
399 }
400
Robert Phillips72152832017-01-25 17:31:35 -0500401 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700402
Brian Salomonbaaf4392017-06-15 09:59:23 -0400403 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeNonAAFillWithLocalMatrix(
404 std::move(paint), SkMatrix::I(), localMatrix, r, GrAAType::kNone);
405 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -0700406 }
407}
408
robertphillipsea461502015-05-26 11:38:03 -0700409static inline bool rect_contains_inclusive(const SkRect& rect, const SkPoint& point) {
410 return point.fX >= rect.fLeft && point.fX <= rect.fRight &&
411 point.fY >= rect.fTop && point.fY <= rect.fBottom;
412}
413
csmartdalton97f6cd52016-07-13 13:37:08 -0700414// Attempts to crop a rect and optional local rect to the clip boundaries.
415// Returns false if the draw can be skipped entirely.
robertphillips13a7eee2016-08-31 15:06:24 -0700416static bool crop_filled_rect(int width, int height, const GrClip& clip,
csmartdalton97f6cd52016-07-13 13:37:08 -0700417 const SkMatrix& viewMatrix, SkRect* rect,
418 SkRect* localRect = nullptr) {
419 if (!viewMatrix.rectStaysRect()) {
420 return true;
421 }
422
csmartdalton97f6cd52016-07-13 13:37:08 -0700423 SkIRect clipDevBounds;
424 SkRect clipBounds;
csmartdalton97f6cd52016-07-13 13:37:08 -0700425
robertphillips13a7eee2016-08-31 15:06:24 -0700426 clip.getConservativeBounds(width, height, &clipDevBounds);
reeda39667c2016-08-22 06:39:49 -0700427 if (!SkMatrixPriv::InverseMapRect(viewMatrix, &clipBounds, SkRect::Make(clipDevBounds))) {
428 return false;
429 }
csmartdalton97f6cd52016-07-13 13:37:08 -0700430
431 if (localRect) {
432 if (!rect->intersects(clipBounds)) {
433 return false;
434 }
435 const SkScalar dx = localRect->width() / rect->width();
436 const SkScalar dy = localRect->height() / rect->height();
437 if (clipBounds.fLeft > rect->fLeft) {
438 localRect->fLeft += (clipBounds.fLeft - rect->fLeft) * dx;
439 rect->fLeft = clipBounds.fLeft;
440 }
441 if (clipBounds.fTop > rect->fTop) {
442 localRect->fTop += (clipBounds.fTop - rect->fTop) * dy;
443 rect->fTop = clipBounds.fTop;
444 }
445 if (clipBounds.fRight < rect->fRight) {
446 localRect->fRight -= (rect->fRight - clipBounds.fRight) * dx;
447 rect->fRight = clipBounds.fRight;
448 }
449 if (clipBounds.fBottom < rect->fBottom) {
450 localRect->fBottom -= (rect->fBottom - clipBounds.fBottom) * dy;
451 rect->fBottom = clipBounds.fBottom;
452 }
453 return true;
454 }
455
456 return rect->intersect(clipBounds);
457}
458
Brian Osman11052242016-10-27 14:47:55 -0400459bool GrRenderTargetContext::drawFilledRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500460 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500461 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400462 const SkMatrix& viewMatrix,
463 const SkRect& rect,
464 const GrUserStencilSettings* ss) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700465 SkRect croppedRect = rect;
Robert Phillips784b7bf2016-12-09 13:35:02 -0500466 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix, &croppedRect)) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700467 return true;
468 }
robertphillips44302392016-07-08 14:43:03 -0700469
Brian Salomon7c8460e2017-05-12 11:36:10 -0400470 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400471 std::unique_ptr<GrDrawOp> op;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500472 if (GrAAType::kCoverage == aaType) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400473 op = GrRectOpFactory::MakeAAFill(std::move(paint), viewMatrix, croppedRect, ss);
robertphillips391395d2016-03-02 09:26:36 -0800474 } else {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400475 op = GrRectOpFactory::MakeNonAAFill(std::move(paint), viewMatrix, croppedRect, aaType, ss);
robertphillips391395d2016-03-02 09:26:36 -0800476 }
Brian Salomonbaaf4392017-06-15 09:59:23 -0400477 if (!op) {
478 return false;
479 }
480 this->addDrawOp(clip, std::move(op));
481 return true;
robertphillips391395d2016-03-02 09:26:36 -0800482}
483
Brian Osman11052242016-10-27 14:47:55 -0400484void GrRenderTargetContext::drawRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500485 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500486 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400487 const SkMatrix& viewMatrix,
488 const SkRect& rect,
489 const GrStyle* style) {
bsalomon6663acf2016-05-10 09:14:17 -0700490 if (!style) {
491 style = &GrStyle::SimpleFill();
492 }
joshualitt1de610a2016-01-06 08:26:09 -0800493 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700494 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700495 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400496 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRect", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700497
bsalomon6663acf2016-05-10 09:14:17 -0700498 // Path effects should've been devolved to a path in SkGpuDevice
499 SkASSERT(!style->pathEffect());
robertphillipsea461502015-05-26 11:38:03 -0700500
Robert Phillips72152832017-01-25 17:31:35 -0500501 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700502
bsalomon6663acf2016-05-10 09:14:17 -0700503 const SkStrokeRec& stroke = style->strokeRec();
Robert Phillipsc90b4db2018-04-23 15:12:00 +0000504 if (stroke.getStyle() == SkStrokeRec::kFill_Style && !paint.numCoverageFragmentProcessors()) {
Brian Salomon43f8bf02017-10-18 08:33:29 -0400505 // Check if this is a full RT draw and can be replaced with a clear. We don't bother
506 // checking cases where the RT is fully inside a stroke.
507 SkRect rtRect = fRenderTargetProxy->getBoundsRect();
508 // Does the clip contain the entire RT?
509 if (clip.quickContains(rtRect)) {
510 SkMatrix invM;
511 if (!viewMatrix.invert(&invM)) {
512 return;
513 }
514 // Does the rect bound the RT?
Brian Salomona33b67c2018-05-17 10:42:14 -0400515 GrQuad quad(rtRect, invM);
Brian Salomon43f8bf02017-10-18 08:33:29 -0400516 if (rect_contains_inclusive(rect, quad.point(0)) &&
517 rect_contains_inclusive(rect, quad.point(1)) &&
518 rect_contains_inclusive(rect, quad.point(2)) &&
519 rect_contains_inclusive(rect, quad.point(3))) {
520 // Will it blend?
521 GrColor clearColor;
522 if (paint.isConstantBlendedColor(&clearColor)) {
Chris Dalton344e9032017-12-11 15:42:09 -0700523 this->clear(nullptr, clearColor,
524 GrRenderTargetContext::CanClearFullscreen::kYes);
robertphillipsea461502015-05-26 11:38:03 -0700525 return;
526 }
527 }
528 }
robertphillips44302392016-07-08 14:43:03 -0700529
Brian Salomon82f44312017-01-11 13:42:54 -0500530 if (this->drawFilledRect(clip, std::move(paint), aa, viewMatrix, rect, nullptr)) {
robertphillips44302392016-07-08 14:43:03 -0700531 return;
532 }
bsalomona7d85ba2016-07-06 11:54:59 -0700533 } else if (stroke.getStyle() == SkStrokeRec::kStroke_Style ||
534 stroke.getStyle() == SkStrokeRec::kHairline_Style) {
535 if ((!rect.width() || !rect.height()) &&
536 SkStrokeRec::kHairline_Style != stroke.getStyle()) {
537 SkScalar r = stroke.getWidth() / 2;
538 // TODO: Move these stroke->fill fallbacks to GrShape?
539 switch (stroke.getJoin()) {
540 case SkPaint::kMiter_Join:
Brian Salomon82f44312017-01-11 13:42:54 -0500541 this->drawRect(
542 clip, std::move(paint), aa, viewMatrix,
543 {rect.fLeft - r, rect.fTop - r, rect.fRight + r, rect.fBottom + r},
544 &GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700545 return;
546 case SkPaint::kRound_Join:
547 // Raster draws nothing when both dimensions are empty.
548 if (rect.width() || rect.height()){
549 SkRRect rrect = SkRRect::MakeRectXY(rect.makeOutset(r, r), r, r);
Brian Salomon82f44312017-01-11 13:42:54 -0500550 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect,
551 GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700552 return;
553 }
554 case SkPaint::kBevel_Join:
555 if (!rect.width()) {
Brian Salomon82f44312017-01-11 13:42:54 -0500556 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700557 {rect.fLeft - r, rect.fTop, rect.fRight + r, rect.fBottom},
558 &GrStyle::SimpleFill());
559 } else {
Brian Salomon82f44312017-01-11 13:42:54 -0500560 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700561 {rect.fLeft, rect.fTop - r, rect.fRight, rect.fBottom + r},
562 &GrStyle::SimpleFill());
563 }
564 return;
565 }
566 }
robertphillips44302392016-07-08 14:43:03 -0700567
Brian Salomonbaaf4392017-06-15 09:59:23 -0400568 std::unique_ptr<GrDrawOp> op;
robertphillips44302392016-07-08 14:43:03 -0700569
Brian Salomon7c8460e2017-05-12 11:36:10 -0400570 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500571 if (GrAAType::kCoverage == aaType) {
cdaltonbb539482016-01-04 09:48:25 -0800572 // The stroke path needs the rect to remain axis aligned (no rotation or skew).
573 if (viewMatrix.rectStaysRect()) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400574 op = GrRectOpFactory::MakeAAStroke(std::move(paint), viewMatrix, rect, stroke);
cdaltonbb539482016-01-04 09:48:25 -0800575 }
robertphillipsea461502015-05-26 11:38:03 -0700576 } else {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400577 op = GrRectOpFactory::MakeNonAAStroke(std::move(paint), viewMatrix, rect, stroke,
578 aaType);
robertphillips391395d2016-03-02 09:26:36 -0800579 }
robertphillips4bc31812016-03-01 12:22:49 -0800580
Brian Salomon42521e82016-12-07 16:44:58 -0500581 if (op) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400582 this->addDrawOp(clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700583 return;
robertphillips4bc31812016-03-01 12:22:49 -0800584 }
robertphillips4bc31812016-03-01 12:22:49 -0800585 }
Brian Salomon2fad74a2017-12-20 13:28:55 -0500586 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, GrShape(rect, *style));
robertphillipsea461502015-05-26 11:38:03 -0700587}
588
Robert Phillipsec2249f2016-11-09 08:54:35 -0500589int GrRenderTargetContextPriv::maxWindowRectangles() const {
590 return fRenderTargetContext->fRenderTargetProxy->maxWindowRectangles(
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400591 *fRenderTargetContext->caps());
Robert Phillipsec2249f2016-11-09 08:54:35 -0500592}
593
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000594void GrRenderTargetContextPriv::clearStencilClip(const GrFixedClip& clip, bool insideStencilMask) {
robertphillips976f5f02016-06-03 10:59:20 -0700595 ASSERT_SINGLE_OWNER_PRIV
596 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400597 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400598 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "clearStencilClip",
599 fRenderTargetContext->fContext);
robertphillips976f5f02016-06-03 10:59:20 -0700600
Robert Phillips72152832017-01-25 17:31:35 -0500601 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400602
Robert Phillips2f4ddf62017-06-01 08:48:19 -0400603 std::unique_ptr<GrOp> op(GrClearStencilClipOp::Make(
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000604 clip, insideStencilMask,
605 fRenderTargetContext->fRenderTargetProxy.get()));
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400606 if (!op) {
Robert Phillipse60ad622016-11-17 10:22:48 -0500607 return;
608 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400609 fRenderTargetContext->getRTOpList()->addOp(std::move(op), *fRenderTargetContext->caps());
robertphillips976f5f02016-06-03 10:59:20 -0700610}
611
Chris Daltonbbfd5162017-11-07 13:35:22 -0700612void GrRenderTargetContextPriv::stencilPath(const GrHardClip& clip,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500613 GrAAType aaType,
Brian Osman11052242016-10-27 14:47:55 -0400614 const SkMatrix& viewMatrix,
615 const GrPath* path) {
Brian Salomon467921e2017-03-06 16:17:12 -0500616 ASSERT_SINGLE_OWNER_PRIV
617 RETURN_IF_ABANDONED_PRIV
618 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400619 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "stencilPath",
620 fRenderTargetContext->fContext);
Brian Salomon467921e2017-03-06 16:17:12 -0500621
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500622 SkASSERT(aaType != GrAAType::kCoverage);
Brian Salomon467921e2017-03-06 16:17:12 -0500623
624 bool useHWAA = GrAATypeIsHW(aaType);
625 // TODO: extract portions of checkDraw that are relevant to path stenciling.
626 SkASSERT(path);
627 SkASSERT(fRenderTargetContext->caps()->shaderCaps()->pathRenderingSupport());
628
629 // FIXME: Use path bounds instead of this WAR once
630 // https://bugs.chromium.org/p/skia/issues/detail?id=5640 is resolved.
631 SkRect bounds = SkRect::MakeIWH(fRenderTargetContext->width(), fRenderTargetContext->height());
632
633 // Setup clip
Chris Daltonbbfd5162017-11-07 13:35:22 -0700634 GrAppliedHardClip appliedClip;
635 if (!clip.apply(fRenderTargetContext->width(), fRenderTargetContext->height(), &appliedClip,
636 &bounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -0500637 return;
638 }
639
Robert Phillips65048132017-08-10 08:44:49 -0400640 fRenderTargetContext->setNeedsStencil();
Brian Salomon467921e2017-03-06 16:17:12 -0500641
642 std::unique_ptr<GrOp> op = GrStencilPathOp::Make(viewMatrix,
643 useHWAA,
644 path->getFillType(),
645 appliedClip.hasStencilClip(),
Brian Salomon467921e2017-03-06 16:17:12 -0500646 appliedClip.scissorState(),
Brian Salomon467921e2017-03-06 16:17:12 -0500647 path);
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400648 if (!op) {
649 return;
650 }
Brian Salomon97180af2017-03-14 13:42:58 -0400651 op->setClippedBounds(bounds);
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400652 fRenderTargetContext->getRTOpList()->addOp(std::move(op), *fRenderTargetContext->caps());
robertphillips976f5f02016-06-03 10:59:20 -0700653}
654
Chris Daltonbbfd5162017-11-07 13:35:22 -0700655void GrRenderTargetContextPriv::stencilRect(const GrHardClip& clip,
Brian Osman11052242016-10-27 14:47:55 -0400656 const GrUserStencilSettings* ss,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500657 GrAAType aaType,
Brian Osman11052242016-10-27 14:47:55 -0400658 const SkMatrix& viewMatrix,
659 const SkRect& rect) {
robertphillips976f5f02016-06-03 10:59:20 -0700660 ASSERT_SINGLE_OWNER_PRIV
661 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400662 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400663 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "stencilRect",
664 fRenderTargetContext->fContext);
665
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500666 SkASSERT(GrAAType::kCoverage != aaType);
Robert Phillips72152832017-01-25 17:31:35 -0500667 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips976f5f02016-06-03 10:59:20 -0700668
669 GrPaint paint;
Brian Salomona1633922017-01-09 11:46:10 -0500670 paint.setXPFactory(GrDisableColorXPFactory::Get());
Brian Salomonbaaf4392017-06-15 09:59:23 -0400671 std::unique_ptr<GrDrawOp> op =
672 GrRectOpFactory::MakeNonAAFill(std::move(paint), viewMatrix, rect, aaType, ss);
673 fRenderTargetContext->addDrawOp(clip, std::move(op));
robertphillips976f5f02016-06-03 10:59:20 -0700674}
675
Chris Daltonbbfd5162017-11-07 13:35:22 -0700676bool GrRenderTargetContextPriv::drawAndStencilRect(const GrHardClip& clip,
Brian Osman11052242016-10-27 14:47:55 -0400677 const GrUserStencilSettings* ss,
678 SkRegion::Op op,
679 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500680 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400681 const SkMatrix& viewMatrix,
682 const SkRect& rect) {
robertphillips391395d2016-03-02 09:26:36 -0800683 ASSERT_SINGLE_OWNER_PRIV
684 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400685 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400686 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawAndStencilRect",
687 fRenderTargetContext->fContext);
robertphillips391395d2016-03-02 09:26:36 -0800688
Robert Phillips72152832017-01-25 17:31:35 -0500689 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -0800690
691 GrPaint paint;
robertphillips391395d2016-03-02 09:26:36 -0800692 paint.setCoverageSetOpXPFactory(op, invert);
693
Brian Salomon82f44312017-01-11 13:42:54 -0500694 if (fRenderTargetContext->drawFilledRect(clip, std::move(paint), aa, viewMatrix, rect, ss)) {
robertphillips391395d2016-03-02 09:26:36 -0800695 return true;
696 }
robertphillips391395d2016-03-02 09:26:36 -0800697 SkPath path;
698 path.setIsVolatile(true);
699 path.addRect(rect);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500700 return this->drawAndStencilPath(clip, ss, op, invert, aa, viewMatrix, path);
robertphillips391395d2016-03-02 09:26:36 -0800701}
702
Brian Osman11052242016-10-27 14:47:55 -0400703void GrRenderTargetContext::fillRectToRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500704 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500705 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400706 const SkMatrix& viewMatrix,
707 const SkRect& rectToDraw,
708 const SkRect& localRect) {
joshualitt1de610a2016-01-06 08:26:09 -0800709 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700710 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700711 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400712 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "fillRectToRect", fContext);
robertphillipsea461502015-05-26 11:38:03 -0700713
csmartdalton97f6cd52016-07-13 13:37:08 -0700714 SkRect croppedRect = rectToDraw;
715 SkRect croppedLocalRect = localRect;
robertphillips13a7eee2016-08-31 15:06:24 -0700716 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix,
717 &croppedRect, &croppedLocalRect)) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700718 return;
719 }
720
Robert Phillips72152832017-01-25 17:31:35 -0500721 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -0700722
Brian Salomon7c8460e2017-05-12 11:36:10 -0400723 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500724 if (GrAAType::kCoverage != aaType) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400725 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeNonAAFillWithLocalRect(
726 std::move(paint), viewMatrix, croppedRect, croppedLocalRect, aaType);
727 this->addDrawOp(clip, std::move(op));
csmartdaltonfc49d562016-07-26 17:05:47 -0700728 return;
joshualitt04194f32016-01-13 10:08:27 -0800729 }
bsalomonbb243832016-07-22 07:10:19 -0700730
Brian Salomonbaaf4392017-06-15 09:59:23 -0400731 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeAAFillWithLocalRect(
732 std::move(paint), viewMatrix, croppedRect, croppedLocalRect);
733 if (op) {
734 this->addDrawOp(clip, std::move(op));
csmartdaltonfc49d562016-07-26 17:05:47 -0700735 return;
736 }
737
738 SkMatrix viewAndUnLocalMatrix;
739 if (!viewAndUnLocalMatrix.setRectToRect(localRect, rectToDraw, SkMatrix::kFill_ScaleToFit)) {
740 SkDebugf("fillRectToRect called with empty local matrix.\n");
741 return;
742 }
743 viewAndUnLocalMatrix.postConcat(viewMatrix);
744
Brian Salomon2fad74a2017-12-20 13:28:55 -0500745 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewAndUnLocalMatrix,
746 GrShape(localRect));
joshualittb6b513b2015-08-21 10:25:18 -0700747}
748
Brian Salomon34169692017-08-28 15:32:01 -0400749static bool must_filter(const SkRect& src, const SkRect& dst, const SkMatrix& ctm) {
750 // We don't currently look for 90 degree rotations, mirroring, or downscales that sample at
751 // texel centers.
752 if (!ctm.isTranslate()) {
753 return true;
754 }
755 if (src.width() != dst.width() || src.height() != dst.height()) {
756 return true;
757 }
758 // Check that the device space rectangle's fractional offset is the same as the src rectangle,
759 // and that therefore integers in the src image fall on integers in device space.
760 SkScalar x = ctm.getTranslateX(), y = ctm.getTranslateY();
761 x += dst.fLeft; y += dst.fTop;
762 x -= src.fLeft; y -= src.fTop;
763 return !SkScalarIsInt(x) || !SkScalarIsInt(y);
764}
765
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400766void GrRenderTargetContext::drawTexture(const GrClip& clip, sk_sp<GrTextureProxy> proxy,
767 GrSamplerState::Filter filter, GrColor color,
768 const SkRect& srcRect, const SkRect& dstRect, GrAA aa,
Brian Salomona0047bc2018-05-23 16:39:39 -0400769 SkCanvas::SrcRectConstraint constraint,
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400770 const SkMatrix& viewMatrix,
771 sk_sp<GrColorSpaceXform> colorSpaceXform) {
Brian Salomon34169692017-08-28 15:32:01 -0400772 ASSERT_SINGLE_OWNER
773 RETURN_IF_ABANDONED
774 SkDEBUGCODE(this->validate();)
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400775 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTexture", fContext);
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400776 if (filter != GrSamplerState::Filter::kNearest && !must_filter(srcRect, dstRect, viewMatrix)) {
777 filter = GrSamplerState::Filter::kNearest;
Brian Salomon34169692017-08-28 15:32:01 -0400778 }
Brian Salomonff9d6d32017-08-30 10:27:49 -0400779 SkRect clippedDstRect = dstRect;
780 SkRect clippedSrcRect = srcRect;
781 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix, &clippedDstRect,
782 &clippedSrcRect)) {
783 return;
784 }
Brian Salomon485b8c62018-01-12 15:11:06 -0500785 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomonf3569f02017-10-24 12:52:33 -0400786 bool allowSRGB = SkToBool(this->colorSpaceInfo().colorSpace());
Brian Salomona0047bc2018-05-23 16:39:39 -0400787 this->addDrawOp(clip, GrTextureOp::Make(std::move(proxy), filter, color, clippedSrcRect,
788 clippedDstRect, aaType, constraint, viewMatrix,
789 std::move(colorSpaceXform), allowSRGB));
Brian Salomon34169692017-08-28 15:32:01 -0400790}
791
Brian Osman11052242016-10-27 14:47:55 -0400792void GrRenderTargetContext::fillRectWithLocalMatrix(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500793 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500794 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400795 const SkMatrix& viewMatrix,
796 const SkRect& rectToDraw,
797 const SkMatrix& localMatrix) {
joshualitt1de610a2016-01-06 08:26:09 -0800798 ASSERT_SINGLE_OWNER
joshualittb6b513b2015-08-21 10:25:18 -0700799 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700800 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400801 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "fillRectWithLocalMatrix", fContext);
joshualittb6b513b2015-08-21 10:25:18 -0700802
csmartdalton97f6cd52016-07-13 13:37:08 -0700803 SkRect croppedRect = rectToDraw;
robertphillips13a7eee2016-08-31 15:06:24 -0700804 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix, &croppedRect)) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700805 return;
806 }
807
Robert Phillips72152832017-01-25 17:31:35 -0500808 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -0700809
Brian Salomon7c8460e2017-05-12 11:36:10 -0400810 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500811 if (GrAAType::kCoverage != aaType) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400812 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeNonAAFillWithLocalMatrix(
813 std::move(paint), viewMatrix, localMatrix, croppedRect, aaType);
814 this->addDrawOp(clip, std::move(op));
csmartdaltonfc49d562016-07-26 17:05:47 -0700815 return;
bsalomonc55271f2015-11-09 11:55:57 -0800816 }
robertphillips4bc31812016-03-01 12:22:49 -0800817
Brian Salomonbaaf4392017-06-15 09:59:23 -0400818 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeAAFillWithLocalMatrix(
819 std::move(paint), viewMatrix, localMatrix, croppedRect);
820 if (op) {
821 this->addDrawOp(clip, std::move(op));
csmartdaltonfc49d562016-07-26 17:05:47 -0700822 return;
823 }
824
825 SkMatrix viewAndUnLocalMatrix;
826 if (!localMatrix.invert(&viewAndUnLocalMatrix)) {
827 SkDebugf("fillRectWithLocalMatrix called with degenerate local matrix.\n");
828 return;
829 }
830 viewAndUnLocalMatrix.postConcat(viewMatrix);
831
832 SkPath path;
833 path.setIsVolatile(true);
834 path.addRect(rectToDraw);
835 path.transform(localMatrix);
Brian Salomon2fad74a2017-12-20 13:28:55 -0500836 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewAndUnLocalMatrix,
837 GrShape(path));
robertphillipsea461502015-05-26 11:38:03 -0700838}
839
Brian Osman11052242016-10-27 14:47:55 -0400840void GrRenderTargetContext::drawVertices(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500841 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400842 const SkMatrix& viewMatrix,
Brian Osmanae0c50c2017-05-25 16:56:34 -0400843 sk_sp<SkVertices> vertices,
844 GrPrimitiveType* overridePrimType) {
Brian Salomon199fb872017-02-06 09:41:10 -0500845 ASSERT_SINGLE_OWNER
846 RETURN_IF_ABANDONED
847 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400848 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawVertices", fContext);
Brian Salomon199fb872017-02-06 09:41:10 -0500849
850 AutoCheckFlush acf(this->drawingManager());
851
852 SkASSERT(vertices);
Brian Salomonc2f42542017-07-12 14:11:22 -0400853 GrAAType aaType = this->chooseAAType(GrAA::kNo, GrAllowMixedSamples::kNo);
Brian Salomonf3569f02017-10-24 12:52:33 -0400854 std::unique_ptr<GrDrawOp> op = GrDrawVerticesOp::Make(
855 std::move(paint), std::move(vertices), viewMatrix, aaType,
856 this->colorSpaceInfo().isGammaCorrect(),
857 this->colorSpaceInfo().refColorSpaceXformFromSRGB(), overridePrimType);
Brian Salomonc2f42542017-07-12 14:11:22 -0400858 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -0700859}
860
861///////////////////////////////////////////////////////////////////////////////
862
Brian Osman11052242016-10-27 14:47:55 -0400863void GrRenderTargetContext::drawAtlas(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500864 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400865 const SkMatrix& viewMatrix,
866 int spriteCount,
867 const SkRSXform xform[],
868 const SkRect texRect[],
869 const SkColor colors[]) {
joshualitt1de610a2016-01-06 08:26:09 -0800870 ASSERT_SINGLE_OWNER
jvanverth31ff7622015-08-07 10:09:28 -0700871 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700872 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400873 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawAtlas", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700874
Robert Phillips72152832017-01-25 17:31:35 -0500875 AutoCheckFlush acf(this->drawingManager());
halcanary9d524f22016-03-29 09:03:52 -0700876
Brian Salomon0088f942017-07-12 11:51:27 -0400877 GrAAType aaType = this->chooseAAType(GrAA::kNo, GrAllowMixedSamples::kNo);
878 std::unique_ptr<GrDrawOp> op = GrDrawAtlasOp::Make(std::move(paint), viewMatrix, aaType,
879 spriteCount, xform, texRect, colors);
880 this->addDrawOp(clip, std::move(op));
jvanverth31ff7622015-08-07 10:09:28 -0700881}
882
883///////////////////////////////////////////////////////////////////////////////
884
Brian Osman11052242016-10-27 14:47:55 -0400885void GrRenderTargetContext::drawRRect(const GrClip& origClip,
Brian Salomon82f44312017-01-11 13:42:54 -0500886 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500887 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400888 const SkMatrix& viewMatrix,
889 const SkRRect& rrect,
890 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -0800891 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700892 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700893 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400894 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRRect", fContext);
robertphillipsea461502015-05-26 11:38:03 -0700895 if (rrect.isEmpty()) {
896 return;
897 }
898
bsalomon7f0d9f32016-08-15 14:49:10 -0700899 GrNoClip noclip;
900 const GrClip* clip = &origClip;
901#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
902 // The Android framework frequently clips rrects to themselves where the clip is non-aa and the
Brian Salomon42521e82016-12-07 16:44:58 -0500903 // draw is aa. Since our lower level clip code works from op bounds, which are SkRects, it
bsalomon7f0d9f32016-08-15 14:49:10 -0700904 // doesn't detect that the clip can be ignored (modulo antialiasing). The following test
905 // attempts to mitigate the stencil clip cost but will only help when the entire clip stack
906 // can be ignored. We'd prefer to fix this in the framework by removing the clips calls.
907 SkRRect devRRect;
908 if (rrect.transform(viewMatrix, &devRRect) && clip->quickContains(devRRect)) {
909 clip = &noclip;
910 }
911#endif
bsalomon6663acf2016-05-10 09:14:17 -0700912 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
ksakamotoec7f2ac2016-07-05 03:54:53 -0700913
Robert Phillips72152832017-01-25 17:31:35 -0500914 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -0700915 const SkStrokeRec stroke = style.strokeRec();
csmartdaltona7f29642016-07-07 08:49:11 -0700916
Brian Salomonea26d6b2018-01-23 20:33:21 +0000917 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500918 if (GrAAType::kCoverage == aaType) {
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400919 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
Brian Salomonea26d6b2018-01-23 20:33:21 +0000920 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeRRectOp(std::move(paint),
921 viewMatrix,
922 rrect,
923 stroke,
924 shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -0500925 if (op) {
Brian Salomon05441c42017-05-15 16:45:49 -0400926 this->addDrawOp(*clip, std::move(op));
robertphillipsb56f9272016-02-25 11:03:52 -0800927 return;
928 }
robertphillipsea461502015-05-26 11:38:03 -0700929 }
robertphillipsb56f9272016-02-25 11:03:52 -0800930
Brian Salomon2fad74a2017-12-20 13:28:55 -0500931 this->drawShapeUsingPathRenderer(*clip, std::move(paint), aa, viewMatrix,
932 GrShape(rrect, style));
robertphillipsea461502015-05-26 11:38:03 -0700933}
934
Jim Van Verthc5903412016-11-17 15:27:09 -0500935///////////////////////////////////////////////////////////////////////////////
936
Jim Van Verth3af1af92017-05-18 15:06:54 -0400937static SkPoint3 map(const SkMatrix& m, const SkPoint3& pt) {
938 SkPoint3 result;
939 m.mapXY(pt.fX, pt.fY, (SkPoint*)&result.fX);
940 result.fZ = pt.fZ;
941 return result;
942}
943
944bool GrRenderTargetContext::drawFastShadow(const GrClip& clip,
Jim Van Verth3af1af92017-05-18 15:06:54 -0400945 const SkMatrix& viewMatrix,
946 const SkPath& path,
947 const SkDrawShadowRec& rec) {
Jim Van Verthc5903412016-11-17 15:27:09 -0500948 ASSERT_SINGLE_OWNER
Jim Van Verth3af1af92017-05-18 15:06:54 -0400949 if (this->drawingManager()->wasAbandoned()) {
950 return true;
951 }
Jim Van Verthc5903412016-11-17 15:27:09 -0500952 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400953 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFastShadow", fContext);
Jim Van Verth3af1af92017-05-18 15:06:54 -0400954
955 // check z plane
956 bool tiltZPlane = SkToBool(!SkScalarNearlyZero(rec.fZPlaneParams.fX) ||
957 !SkScalarNearlyZero(rec.fZPlaneParams.fY));
958 bool skipAnalytic = SkToBool(rec.fFlags & SkShadowFlags::kGeometricOnly_ShadowFlag);
959 if (tiltZPlane || skipAnalytic || !viewMatrix.rectStaysRect() || !viewMatrix.isSimilarity()) {
960 return false;
961 }
962
963 SkRRect rrect;
964 SkRect rect;
965 // we can only handle rects, circles, and rrects with circular corners
Mike Reed242135a2018-02-22 13:41:39 -0500966 bool isRRect = path.isRRect(&rrect) && SkRRectPriv::IsSimpleCircular(rrect) &&
Jim Van Verth3af1af92017-05-18 15:06:54 -0400967 rrect.radii(SkRRect::kUpperLeft_Corner).fX > SK_ScalarNearlyZero;
968 if (!isRRect &&
969 path.isOval(&rect) && SkScalarNearlyEqual(rect.width(), rect.height()) &&
970 rect.width() > SK_ScalarNearlyZero) {
971 rrect.setOval(rect);
972 isRRect = true;
973 }
974 if (!isRRect && path.isRect(&rect)) {
975 rrect.setRect(rect);
976 isRRect = true;
977 }
978
979 if (!isRRect) {
980 return false;
981 }
982
Jim Van Verthc5903412016-11-17 15:27:09 -0500983 if (rrect.isEmpty()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -0400984 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -0500985 }
986
Robert Phillips72152832017-01-25 17:31:35 -0500987 AutoCheckFlush acf(this->drawingManager());
Jim Van Verthc5903412016-11-17 15:27:09 -0500988
Jim Van Verth3af1af92017-05-18 15:06:54 -0400989 // transform light
990 SkPoint3 devLightPos = map(viewMatrix, rec.fLightPos);
991
992 // 1/scale
993 SkScalar devToSrcScale = viewMatrix.isScaleTranslate() ?
994 SkScalarInvert(viewMatrix[SkMatrix::kMScaleX]) :
995 sk_float_rsqrt(viewMatrix[SkMatrix::kMScaleX] * viewMatrix[SkMatrix::kMScaleX] +
996 viewMatrix[SkMatrix::kMSkewX] * viewMatrix[SkMatrix::kMSkewX]);
997
998 SkScalar occluderHeight = rec.fZPlaneParams.fZ;
Jim Van Verth3af1af92017-05-18 15:06:54 -0400999 bool transparent = SkToBool(rec.fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag);
1000
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001001 if (SkColorGetA(rec.fAmbientColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001002 SkScalar devSpaceInsetWidth = SkDrawShadowMetrics::AmbientBlurRadius(occluderHeight);
1003 const SkScalar umbraRecipAlpha = SkDrawShadowMetrics::AmbientRecipAlpha(occluderHeight);
1004 const SkScalar devSpaceAmbientBlur = devSpaceInsetWidth * umbraRecipAlpha;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001005
1006 // Outset the shadow rrect to the border of the penumbra
1007 SkScalar ambientPathOutset = devSpaceInsetWidth * devToSrcScale;
1008 SkRRect ambientRRect;
1009 SkRect outsetRect = rrect.rect().makeOutset(ambientPathOutset, ambientPathOutset);
1010 // If the rrect was an oval then its outset will also be one.
1011 // We set it explicitly to avoid errors.
1012 if (rrect.isOval()) {
1013 ambientRRect = SkRRect::MakeOval(outsetRect);
1014 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001015 SkScalar outsetRad = SkRRectPriv::GetSimpleRadii(rrect).fX + ambientPathOutset;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001016 ambientRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1017 }
1018
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001019 GrColor ambientColor = SkColorToPremulGrColor(rec.fAmbientColor);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001020 if (transparent) {
1021 // set a large inset to force a fill
1022 devSpaceInsetWidth = ambientRRect.width();
1023 }
Jim Van Verth39e71652018-04-23 18:08:45 +00001024 // the fraction of the blur we want to apply is devSpaceInsetWidth/devSpaceAmbientBlur,
1025 // which is just 1/umbraRecipAlpha.
1026 SkScalar blurClamp = SkScalarInvert(umbraRecipAlpha);
1027
Brian Salomon05969092017-07-13 11:20:51 -04001028 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(ambientColor, viewMatrix,
1029 ambientRRect,
1030 devSpaceAmbientBlur,
Jim Van Verth39e71652018-04-23 18:08:45 +00001031 devSpaceInsetWidth,
1032 blurClamp);
Brian Salomon05969092017-07-13 11:20:51 -04001033 SkASSERT(op);
1034 this->addDrawOp(clip, std::move(op));
Jim Van Verthc5903412016-11-17 15:27:09 -05001035 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001036
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001037 if (SkColorGetA(rec.fSpotColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001038 SkScalar devSpaceSpotBlur;
1039 SkScalar spotScale;
1040 SkVector spotOffset;
1041 SkDrawShadowMetrics::GetSpotParams(occluderHeight, devLightPos.fX, devLightPos.fY,
1042 devLightPos.fZ, rec.fLightRadius,
1043 &devSpaceSpotBlur, &spotScale, &spotOffset);
1044 // handle scale of radius due to CTM
Jim Van Verth3af1af92017-05-18 15:06:54 -04001045 const SkScalar srcSpaceSpotBlur = devSpaceSpotBlur * devToSrcScale;
1046
Jim Van Verth3af1af92017-05-18 15:06:54 -04001047 // Adjust translate for the effect of the scale.
1048 spotOffset.fX += spotScale*viewMatrix[SkMatrix::kMTransX];
1049 spotOffset.fY += spotScale*viewMatrix[SkMatrix::kMTransY];
1050 // This offset is in dev space, need to transform it into source space.
1051 SkMatrix ctmInverse;
1052 if (viewMatrix.invert(&ctmInverse)) {
1053 ctmInverse.mapPoints(&spotOffset, 1);
1054 } else {
1055 // Since the matrix is a similarity, this should never happen, but just in case...
1056 SkDebugf("Matrix is degenerate. Will not render spot shadow correctly!\n");
1057 SkASSERT(false);
1058 }
1059
1060 // Compute the transformed shadow rrect
1061 SkRRect spotShadowRRect;
1062 SkMatrix shadowTransform;
1063 shadowTransform.setScaleTranslate(spotScale, spotScale, spotOffset.fX, spotOffset.fY);
1064 rrect.transform(shadowTransform, &spotShadowRRect);
Mike Reed242135a2018-02-22 13:41:39 -05001065 SkScalar spotRadius = SkRRectPriv::GetSimpleRadii(spotShadowRRect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001066
1067 // Compute the insetWidth
Jim Van Verth1af03d42017-07-31 09:34:58 -04001068 SkScalar blurOutset = srcSpaceSpotBlur;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001069 SkScalar insetWidth = blurOutset;
1070 if (transparent) {
1071 // If transparent, just do a fill
1072 insetWidth += spotShadowRRect.width();
1073 } else {
1074 // For shadows, instead of using a stroke we specify an inset from the penumbra
1075 // border. We want to extend this inset area so that it meets up with the caster
1076 // geometry. The inset geometry will by default already be inset by the blur width.
1077 //
1078 // We compare the min and max corners inset by the radius between the original
1079 // rrect and the shadow rrect. The distance between the two plus the difference
1080 // between the scaled radius and the original radius gives the distance from the
1081 // transformed shadow shape to the original shape in that corner. The max
1082 // of these gives the maximum distance we need to cover.
1083 //
1084 // Since we are outsetting by 1/2 the blur distance, we just add the maxOffset to
1085 // that to get the full insetWidth.
1086 SkScalar maxOffset;
1087 if (rrect.isRect()) {
1088 // Manhattan distance works better for rects
1089 maxOffset = SkTMax(SkTMax(SkTAbs(spotShadowRRect.rect().fLeft -
1090 rrect.rect().fLeft),
1091 SkTAbs(spotShadowRRect.rect().fTop -
1092 rrect.rect().fTop)),
1093 SkTMax(SkTAbs(spotShadowRRect.rect().fRight -
1094 rrect.rect().fRight),
1095 SkTAbs(spotShadowRRect.rect().fBottom -
1096 rrect.rect().fBottom)));
1097 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001098 SkScalar dr = spotRadius - SkRRectPriv::GetSimpleRadii(rrect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001099 SkPoint upperLeftOffset = SkPoint::Make(spotShadowRRect.rect().fLeft -
1100 rrect.rect().fLeft + dr,
1101 spotShadowRRect.rect().fTop -
1102 rrect.rect().fTop + dr);
1103 SkPoint lowerRightOffset = SkPoint::Make(spotShadowRRect.rect().fRight -
1104 rrect.rect().fRight - dr,
1105 spotShadowRRect.rect().fBottom -
1106 rrect.rect().fBottom - dr);
Cary Clarkdf429f32017-11-08 11:44:31 -05001107 maxOffset = SkScalarSqrt(SkTMax(SkPointPriv::LengthSqd(upperLeftOffset),
1108 SkPointPriv::LengthSqd(lowerRightOffset))) + dr;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001109 }
Jim Van Verth4c8c1e82018-04-23 17:14:48 -04001110 insetWidth += SkTMax(blurOutset, maxOffset);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001111 }
1112
1113 // Outset the shadow rrect to the border of the penumbra
1114 SkRect outsetRect = spotShadowRRect.rect().makeOutset(blurOutset, blurOutset);
1115 if (spotShadowRRect.isOval()) {
1116 spotShadowRRect = SkRRect::MakeOval(outsetRect);
1117 } else {
1118 SkScalar outsetRad = spotRadius + blurOutset;
1119 spotShadowRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1120 }
1121
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001122 GrColor spotColor = SkColorToPremulGrColor(rec.fSpotColor);
Jim Van Verth34d6e4b2017-06-09 11:09:03 -04001123
Brian Salomon05969092017-07-13 11:20:51 -04001124 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(spotColor, viewMatrix,
1125 spotShadowRRect,
Jim Van Verth1af03d42017-07-31 09:34:58 -04001126 2.0f * devSpaceSpotBlur,
Brian Salomon05969092017-07-13 11:20:51 -04001127 insetWidth);
1128 SkASSERT(op);
1129 this->addDrawOp(clip, std::move(op));
Jim Van Verth3af1af92017-05-18 15:06:54 -04001130 }
1131
1132 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001133}
1134
1135///////////////////////////////////////////////////////////////////////////////
1136
Brian Osman11052242016-10-27 14:47:55 -04001137bool GrRenderTargetContext::drawFilledDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001138 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001139 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001140 const SkMatrix& viewMatrix,
1141 const SkRRect& origOuter,
1142 const SkRRect& origInner) {
robertphillips00095892016-02-29 13:50:40 -08001143 SkASSERT(!origInner.isEmpty());
1144 SkASSERT(!origOuter.isEmpty());
1145
Brian Salomon65749212017-12-01 16:01:47 -05001146 SkTCopyOnFirstWrite<SkRRect> inner(origInner), outer(origOuter);
1147
Brian Salomon45839f92017-12-04 09:02:35 -05001148 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
1149
1150 if (GrAAType::kMSAA == aaType) {
1151 return false;
1152 }
1153
Mike Reed242135a2018-02-22 13:41:39 -05001154 if (GrAAType::kCoverage == aaType && SkRRectPriv::IsCircle(*inner)
1155 && SkRRectPriv::IsCircle(*outer)) {
Brian Salomon65749212017-12-01 16:01:47 -05001156 auto outerR = outer->width() / 2.f;
1157 auto innerR = inner->width() / 2.f;
1158 auto cx = outer->getBounds().fLeft + outerR;
1159 auto cy = outer->getBounds().fTop + outerR;
1160 if (SkScalarNearlyEqual(cx, inner->getBounds().fLeft + innerR) &&
1161 SkScalarNearlyEqual(cy, inner->getBounds().fTop + innerR)) {
1162 auto avgR = (innerR + outerR) / 2.f;
1163 auto circleBounds = SkRect::MakeLTRB(cx - avgR, cy - avgR, cx + avgR, cy + avgR);
1164 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
1165 stroke.setStrokeStyle(outerR - innerR);
Brian Salomonea26d6b2018-01-23 20:33:21 +00001166 auto op = GrOvalOpFactory::MakeOvalOp(std::move(paint), viewMatrix, circleBounds,
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001167 GrStyle(stroke, nullptr),
1168 this->caps()->shaderCaps());
Brian Salomon65749212017-12-01 16:01:47 -05001169 if (op) {
1170 this->addDrawOp(clip, std::move(op));
1171 return true;
1172 }
1173 }
1174 }
1175
Ethan Nicholas0f3c7322017-11-09 14:51:17 -05001176 GrClipEdgeType innerEdgeType, outerEdgeType;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001177 if (GrAAType::kCoverage == aaType) {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001178 innerEdgeType = GrClipEdgeType::kInverseFillAA;
1179 outerEdgeType = GrClipEdgeType::kFillAA;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001180 } else {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001181 innerEdgeType = GrClipEdgeType::kInverseFillBW;
1182 outerEdgeType = GrClipEdgeType::kFillBW;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001183 }
robertphillips00095892016-02-29 13:50:40 -08001184
robertphillips00095892016-02-29 13:50:40 -08001185 SkMatrix inverseVM;
1186 if (!viewMatrix.isIdentity()) {
1187 if (!origInner.transform(viewMatrix, inner.writable())) {
1188 return false;
1189 }
1190 if (!origOuter.transform(viewMatrix, outer.writable())) {
1191 return false;
1192 }
1193 if (!viewMatrix.invert(&inverseVM)) {
1194 return false;
1195 }
1196 } else {
1197 inverseVM.reset();
halcanary9d524f22016-03-29 09:03:52 -07001198 }
robertphillips00095892016-02-29 13:50:40 -08001199
Brian Salomon14471772017-12-05 10:35:15 -05001200 const auto& caps = *this->caps()->shaderCaps();
robertphillips00095892016-02-29 13:50:40 -08001201 // TODO these need to be a geometry processors
Brian Salomon14471772017-12-05 10:35:15 -05001202 auto innerEffect = GrRRectEffect::Make(innerEdgeType, *inner, caps);
robertphillips00095892016-02-29 13:50:40 -08001203 if (!innerEffect) {
1204 return false;
1205 }
1206
Brian Salomon14471772017-12-05 10:35:15 -05001207 auto outerEffect = GrRRectEffect::Make(outerEdgeType, *outer, caps);
robertphillips00095892016-02-29 13:50:40 -08001208 if (!outerEffect) {
1209 return false;
1210 }
1211
Brian Salomon82f44312017-01-11 13:42:54 -05001212 paint.addCoverageFragmentProcessor(std::move(innerEffect));
1213 paint.addCoverageFragmentProcessor(std::move(outerEffect));
robertphillips00095892016-02-29 13:50:40 -08001214
1215 SkRect bounds = outer->getBounds();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001216 if (GrAAType::kCoverage == aaType) {
robertphillips00095892016-02-29 13:50:40 -08001217 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1218 }
halcanary9d524f22016-03-29 09:03:52 -07001219
Brian Salomon82f44312017-01-11 13:42:54 -05001220 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), bounds,
1221 inverseVM);
robertphillips00095892016-02-29 13:50:40 -08001222 return true;
1223}
1224
Brian Osman11052242016-10-27 14:47:55 -04001225void GrRenderTargetContext::drawDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001226 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001227 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001228 const SkMatrix& viewMatrix,
1229 const SkRRect& outer,
1230 const SkRRect& inner) {
robertphillips00095892016-02-29 13:50:40 -08001231 ASSERT_SINGLE_OWNER
1232 RETURN_IF_ABANDONED
1233 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001234 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawDRRect", fContext);
robertphillips00095892016-02-29 13:50:40 -08001235
1236 SkASSERT(!outer.isEmpty());
1237 SkASSERT(!inner.isEmpty());
1238
Robert Phillips72152832017-01-25 17:31:35 -05001239 AutoCheckFlush acf(this->drawingManager());
robertphillips00095892016-02-29 13:50:40 -08001240
Brian Salomon82f44312017-01-11 13:42:54 -05001241 if (this->drawFilledDRRect(clip, std::move(paint), aa, viewMatrix, outer, inner)) {
robertphillips00095892016-02-29 13:50:40 -08001242 return;
1243 }
1244
1245 SkPath path;
1246 path.setIsVolatile(true);
1247 path.addRRect(inner);
1248 path.addRRect(outer);
1249 path.setFillType(SkPath::kEvenOdd_FillType);
Brian Salomon2fad74a2017-12-20 13:28:55 -05001250 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, GrShape(path));
robertphillips00095892016-02-29 13:50:40 -08001251}
1252
robertphillipsea461502015-05-26 11:38:03 -07001253///////////////////////////////////////////////////////////////////////////////
1254
Brian Osman11052242016-10-27 14:47:55 -04001255void GrRenderTargetContext::drawRegion(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001256 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001257 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001258 const SkMatrix& viewMatrix,
1259 const SkRegion& region,
Stan Iliev73d8fd92017-08-02 15:36:24 -04001260 const GrStyle& style,
1261 const GrUserStencilSettings* ss) {
msarettcc319b92016-08-25 18:07:18 -07001262 ASSERT_SINGLE_OWNER
1263 RETURN_IF_ABANDONED
1264 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001265 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRegion", fContext);
msarettcc319b92016-08-25 18:07:18 -07001266
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001267 if (GrAA::kYes == aa) {
Brian Salomonfc527d22016-12-14 21:07:01 -05001268 // GrRegionOp performs no antialiasing but is much faster, so here we check the matrix
Brian Salomonc57c7c92016-12-06 14:47:34 -05001269 // to see whether aa is really required.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001270 if (!SkToBool(viewMatrix.getType() & ~(SkMatrix::kTranslate_Mask)) &&
Brian Salomon34169692017-08-28 15:32:01 -04001271 SkScalarIsInt(viewMatrix.getTranslateX()) &&
1272 SkScalarIsInt(viewMatrix.getTranslateY())) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001273 aa = GrAA::kNo;
1274 }
Brian Salomonc57c7c92016-12-06 14:47:34 -05001275 }
msarettcc319b92016-08-25 18:07:18 -07001276 bool complexStyle = !style.isSimpleFill();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001277 if (complexStyle || GrAA::kYes == aa) {
msarettcc319b92016-08-25 18:07:18 -07001278 SkPath path;
1279 region.getBoundaryPath(&path);
Brian Salomon82f44312017-01-11 13:42:54 -05001280 return this->drawPath(clip, std::move(paint), aa, viewMatrix, path, style);
msarettcc319b92016-08-25 18:07:18 -07001281 }
1282
Brian Salomonf0366322017-07-11 15:53:05 -04001283 GrAAType aaType = this->chooseAAType(GrAA::kNo, GrAllowMixedSamples::kNo);
Stan Iliev73d8fd92017-08-02 15:36:24 -04001284 std::unique_ptr<GrDrawOp> op = GrRegionOp::Make(std::move(paint), viewMatrix, region, aaType,
1285 ss);
Brian Salomonf0366322017-07-11 15:53:05 -04001286 this->addDrawOp(clip, std::move(op));
msarettcc319b92016-08-25 18:07:18 -07001287}
1288
Brian Osman11052242016-10-27 14:47:55 -04001289void GrRenderTargetContext::drawOval(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001290 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001291 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001292 const SkMatrix& viewMatrix,
1293 const SkRect& oval,
1294 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001295 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001296 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001297 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001298 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawOval", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -07001299
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001300 if (oval.isEmpty() && !style.pathEffect()) {
1301 return;
robertphillipsea461502015-05-26 11:38:03 -07001302 }
1303
Robert Phillips72152832017-01-25 17:31:35 -05001304 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001305
Brian Salomonea26d6b2018-01-23 20:33:21 +00001306 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001307 if (GrAAType::kCoverage == aaType) {
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001308 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001309 if (auto op = GrOvalOpFactory::MakeOvalOp(std::move(paint), viewMatrix, oval, style,
1310 shaderCaps)) {
Brian Salomon05441c42017-05-15 16:45:49 -04001311 this->addDrawOp(clip, std::move(op));
robertphillipsb56f9272016-02-25 11:03:52 -08001312 return;
1313 }
robertphillipsea461502015-05-26 11:38:03 -07001314 }
robertphillipsb56f9272016-02-25 11:03:52 -08001315
Brian Salomon5209d7f2018-04-20 16:52:42 -04001316 this->drawShapeUsingPathRenderer(
1317 clip, std::move(paint), aa, viewMatrix,
1318 GrShape(SkRRect::MakeOval(oval), SkPath::kCW_Direction, 2, false, style));
robertphillipsea461502015-05-26 11:38:03 -07001319}
1320
Brian Osman11052242016-10-27 14:47:55 -04001321void GrRenderTargetContext::drawArc(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001322 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001323 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001324 const SkMatrix& viewMatrix,
1325 const SkRect& oval,
1326 SkScalar startAngle,
1327 SkScalar sweepAngle,
1328 bool useCenter,
1329 const GrStyle& style) {
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001330 ASSERT_SINGLE_OWNER
1331 RETURN_IF_ABANDONED
1332 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001333 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawArc", fContext);
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001334
1335 AutoCheckFlush acf(this->drawingManager());
1336
Brian Salomonea26d6b2018-01-23 20:33:21 +00001337 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001338 if (GrAAType::kCoverage == aaType) {
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001339 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
Brian Salomonea26d6b2018-01-23 20:33:21 +00001340 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeArcOp(std::move(paint),
1341 viewMatrix,
1342 oval,
1343 startAngle,
1344 sweepAngle,
1345 useCenter,
1346 style,
1347 shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -05001348 if (op) {
Brian Salomon05441c42017-05-15 16:45:49 -04001349 this->addDrawOp(clip, std::move(op));
bsalomon4f3a0ca2016-08-22 13:14:26 -07001350 return;
1351 }
1352 }
Brian Salomone4949402018-04-26 15:22:04 -04001353 this->drawShapeUsingPathRenderer(
1354 clip, std::move(paint), aa, viewMatrix,
1355 GrShape::MakeArc(oval, startAngle, sweepAngle, useCenter, style));
bsalomon4f3a0ca2016-08-22 13:14:26 -07001356}
1357
Brian Osman11052242016-10-27 14:47:55 -04001358void GrRenderTargetContext::drawImageLattice(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001359 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001360 const SkMatrix& viewMatrix,
Brian Salomon2a943df2018-05-04 13:43:19 -04001361 sk_sp<GrTextureProxy> image,
1362 sk_sp<GrColorSpaceXform> csxf,
1363 GrSamplerState::Filter filter,
Brian Osman11052242016-10-27 14:47:55 -04001364 std::unique_ptr<SkLatticeIter> iter,
1365 const SkRect& dst) {
joshualitt1de610a2016-01-06 08:26:09 -08001366 ASSERT_SINGLE_OWNER
joshualitt33a5fce2015-11-18 13:28:51 -08001367 RETURN_IF_ABANDONED
1368 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001369 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawImageLattice", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001370
Robert Phillips72152832017-01-25 17:31:35 -05001371 AutoCheckFlush acf(this->drawingManager());
joshualitt33a5fce2015-11-18 13:28:51 -08001372
Brian Salomon2a943df2018-05-04 13:43:19 -04001373 std::unique_ptr<GrDrawOp> op =
1374 GrLatticeOp::MakeNonAA(std::move(paint), viewMatrix, std::move(image), std::move(csxf),
1375 filter, std::move(iter), dst);
Brian Salomon815486c2017-07-11 08:52:13 -04001376 this->addDrawOp(clip, std::move(op));
joshualitt33a5fce2015-11-18 13:28:51 -08001377}
1378
Greg Daniel51316782017-08-02 15:10:09 +00001379GrSemaphoresSubmitted GrRenderTargetContext::prepareForExternalIO(
1380 int numSemaphores, GrBackendSemaphore backendSemaphores[]) {
robertphillips8c523e02016-07-26 07:41:00 -07001381 ASSERT_SINGLE_OWNER
Greg Daniel51316782017-08-02 15:10:09 +00001382 if (this->drawingManager()->wasAbandoned()) { return GrSemaphoresSubmitted::kNo; }
robertphillips8c523e02016-07-26 07:41:00 -07001383 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001384 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "prepareForExternalIO", fContext);
robertphillips8c523e02016-07-26 07:41:00 -07001385
Greg Daniel51316782017-08-02 15:10:09 +00001386 return this->drawingManager()->prepareSurfaceForExternalIO(fRenderTargetProxy.get(),
1387 numSemaphores,
1388 backendSemaphores);
Greg Daniela5cb7812017-06-16 09:45:32 -04001389}
1390
Greg Danielc64ee462017-06-15 16:59:49 -04001391bool GrRenderTargetContext::waitOnSemaphores(int numSemaphores,
Greg Daniela5cb7812017-06-16 09:45:32 -04001392 const GrBackendSemaphore* waitSemaphores) {
1393 ASSERT_SINGLE_OWNER
Greg Danielc64ee462017-06-15 16:59:49 -04001394 RETURN_FALSE_IF_ABANDONED
Greg Daniela5cb7812017-06-16 09:45:32 -04001395 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001396 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "waitOnSemaphores", fContext);
Greg Daniela5cb7812017-06-16 09:45:32 -04001397
1398 AutoCheckFlush acf(this->drawingManager());
1399
Greg Danielc64ee462017-06-15 16:59:49 -04001400 if (numSemaphores && !this->caps()->fenceSyncSupport()) {
1401 return false;
1402 }
1403
Robert Phillips6be756b2018-01-16 15:07:54 -05001404 auto resourceProvider = fContext->contextPriv().resourceProvider();
1405
Greg Daniela5cb7812017-06-16 09:45:32 -04001406 SkTArray<sk_sp<GrSemaphore>> semaphores(numSemaphores);
1407 for (int i = 0; i < numSemaphores; ++i) {
Robert Phillips6be756b2018-01-16 15:07:54 -05001408 sk_sp<GrSemaphore> sema = resourceProvider->wrapBackendSemaphore(
Greg Daniel17b7c052018-01-09 13:55:33 -05001409 waitSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillWait,
1410 kAdopt_GrWrapOwnership);
Greg Daniela5cb7812017-06-16 09:45:32 -04001411 std::unique_ptr<GrOp> waitOp(GrSemaphoreOp::MakeWait(sema, fRenderTargetProxy.get()));
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001412 this->getRTOpList()->addOp(std::move(waitOp), *this->caps());
Greg Daniela5cb7812017-06-16 09:45:32 -04001413 }
Greg Danielc64ee462017-06-15 16:59:49 -04001414 return true;
robertphillips8c523e02016-07-26 07:41:00 -07001415}
joshualitt33a5fce2015-11-18 13:28:51 -08001416
Robert Phillips65a88fa2017-08-08 08:36:22 -04001417void GrRenderTargetContext::insertEventMarker(const SkString& str) {
1418 std::unique_ptr<GrOp> op(GrDebugMarkerOp::Make(fRenderTargetProxy.get(), str));
1419 this->getRTOpList()->addOp(std::move(op), *this->caps());
1420}
1421
1422
robertphillipsea461502015-05-26 11:38:03 -07001423// Can 'path' be drawn as a pair of filled nested rectangles?
bsalomon6663acf2016-05-10 09:14:17 -07001424static bool fills_as_nested_rects(const SkMatrix& viewMatrix, const SkPath& path, SkRect rects[2]) {
robertphillipsea461502015-05-26 11:38:03 -07001425
1426 if (path.isInverseFillType()) {
1427 return false;
1428 }
1429
1430 // TODO: this restriction could be lifted if we were willing to apply
1431 // the matrix to all the points individually rather than just to the rect
robertphillips0e7029e2015-11-30 05:45:06 -08001432 if (!viewMatrix.rectStaysRect()) {
robertphillipsea461502015-05-26 11:38:03 -07001433 return false;
1434 }
1435
1436 SkPath::Direction dirs[2];
1437 if (!path.isNestedFillRects(rects, dirs)) {
1438 return false;
1439 }
1440
1441 if (SkPath::kWinding_FillType == path.getFillType() && dirs[0] == dirs[1]) {
1442 // The two rects need to be wound opposite to each other
1443 return false;
1444 }
1445
1446 // Right now, nested rects where the margin is not the same width
1447 // all around do not render correctly
1448 const SkScalar* outer = rects[0].asScalars();
1449 const SkScalar* inner = rects[1].asScalars();
1450
1451 bool allEq = true;
1452
1453 SkScalar margin = SkScalarAbs(outer[0] - inner[0]);
1454 bool allGoE1 = margin >= SK_Scalar1;
1455
1456 for (int i = 1; i < 4; ++i) {
1457 SkScalar temp = SkScalarAbs(outer[i] - inner[i]);
1458 if (temp < SK_Scalar1) {
1459 allGoE1 = false;
1460 }
1461 if (!SkScalarNearlyEqual(margin, temp)) {
1462 allEq = false;
1463 }
1464 }
1465
1466 return allEq || allGoE1;
1467}
1468
Brian Osman11052242016-10-27 14:47:55 -04001469void GrRenderTargetContext::drawPath(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001470 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001471 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001472 const SkMatrix& viewMatrix,
Brian Salomon40b77a62017-12-22 11:25:52 -05001473 const SkPath& path,
Brian Osman11052242016-10-27 14:47:55 -04001474 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001475 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001476 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001477 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001478 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawPath", fContext);
Brian Salomon40b77a62017-12-22 11:25:52 -05001479
1480 GrShape shape(path, style);
Brian Salomon2fad74a2017-12-20 13:28:55 -05001481 if (shape.isEmpty()) {
1482 if (shape.inverseFilled()) {
1483 this->drawPaint(clip, std::move(paint), viewMatrix);
1484 }
1485 return;
robertphillipsea461502015-05-26 11:38:03 -07001486 }
1487
Robert Phillips72152832017-01-25 17:31:35 -05001488 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -07001489
Brian Salomon2fad74a2017-12-20 13:28:55 -05001490 if (!shape.style().hasPathEffect()) {
1491 SkRRect rrect;
1492 // We can ignore the starting point and direction since there is no path effect.
1493 bool inverted;
1494 if (shape.asRRect(&rrect, nullptr, nullptr, &inverted) && !inverted) {
1495 if (rrect.isRect()) {
1496 this->drawRect(clip, std::move(paint), aa, viewMatrix, rrect.rect(),
1497 &shape.style());
1498 return;
1499 } else if (rrect.isOval()) {
1500 this->drawOval(clip, std::move(paint), aa, viewMatrix, rrect.rect(), shape.style());
robertphillipsea461502015-05-26 11:38:03 -07001501 return;
1502 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05001503 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect, shape.style());
1504 return;
robertphillipsea461502015-05-26 11:38:03 -07001505 }
1506 }
robertphillips4bc31812016-03-01 12:22:49 -08001507
Brian Salomon40b77a62017-12-22 11:25:52 -05001508 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
1509 if (GrAAType::kCoverage == aaType) {
1510 // TODO: Make GrShape check for nested rects.
1511 SkRect rects[2];
1512 if (shape.style().isSimpleFill() && fills_as_nested_rects(viewMatrix, path, rects)) {
1513 // Concave AA paths are expensive - try to avoid them for special cases
1514 SkRect rects[2];
1515
1516 if (fills_as_nested_rects(viewMatrix, path, rects)) {
1517 std::unique_ptr<GrDrawOp> op =
1518 GrRectOpFactory::MakeAAFillNestedRects(std::move(paint), viewMatrix, rects);
1519 if (op) {
1520 this->addDrawOp(clip, std::move(op));
1521 }
1522 // A null return indicates that there is nothing to draw in this case.
1523 return;
1524 }
1525 }
1526 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05001527 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, shape);
robertphillipsea461502015-05-26 11:38:03 -07001528}
1529
Chris Daltonbbfd5162017-11-07 13:35:22 -07001530bool GrRenderTargetContextPriv::drawAndStencilPath(const GrHardClip& clip,
Brian Osman11052242016-10-27 14:47:55 -04001531 const GrUserStencilSettings* ss,
1532 SkRegion::Op op,
1533 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001534 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001535 const SkMatrix& viewMatrix,
1536 const SkPath& path) {
robertphillips391395d2016-03-02 09:26:36 -08001537 ASSERT_SINGLE_OWNER_PRIV
1538 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -04001539 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001540 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawAndStencilPath",
1541 fRenderTargetContext->fContext);
robertphillips391395d2016-03-02 09:26:36 -08001542
1543 if (path.isEmpty() && path.isInverseFillType()) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001544 this->drawAndStencilRect(clip, ss, op, invert, GrAA::kNo, SkMatrix::I(),
Brian Osman11052242016-10-27 14:47:55 -04001545 SkRect::MakeIWH(fRenderTargetContext->width(),
1546 fRenderTargetContext->height()));
robertphillips391395d2016-03-02 09:26:36 -08001547 return true;
1548 }
1549
Robert Phillips72152832017-01-25 17:31:35 -05001550 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -08001551
1552 // An Assumption here is that path renderer would use some form of tweaking
1553 // the src color (either the input alpha or in the frag shader) to implement
1554 // aa. If we have some future driver-mojo path AA that can do the right
1555 // thing WRT to the blend then we'll need some query on the PR.
Brian Salomon7c8460e2017-05-12 11:36:10 -04001556 GrAAType aaType = fRenderTargetContext->chooseAAType(aa, GrAllowMixedSamples::kNo);
robertphillips976f5f02016-06-03 10:59:20 -07001557 bool hasUserStencilSettings = !ss->isUnused();
robertphillips391395d2016-03-02 09:26:36 -08001558
Chris Daltondb91c6e2017-09-08 16:25:08 -06001559 SkIRect clipConservativeBounds;
1560 clip.getConservativeBounds(fRenderTargetContext->width(), fRenderTargetContext->height(),
1561 &clipConservativeBounds, nullptr);
1562
bsalomon8acedde2016-06-24 10:42:16 -07001563 GrShape shape(path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -08001564 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001565 canDrawArgs.fCaps = fRenderTargetContext->caps();
robertphillips391395d2016-03-02 09:26:36 -08001566 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07001567 canDrawArgs.fShape = &shape;
Chris Daltondb91c6e2017-09-08 16:25:08 -06001568 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001569 canDrawArgs.fAAType = aaType;
cdalton93a379b2016-05-11 13:58:08 -07001570 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips391395d2016-03-02 09:26:36 -08001571
1572 // Don't allow the SW renderer
Robert Phillips72152832017-01-25 17:31:35 -05001573 GrPathRenderer* pr = fRenderTargetContext->drawingManager()->getPathRenderer(
Brian Salomon36aa1762016-12-10 13:24:02 -05001574 canDrawArgs, false, GrPathRendererChain::DrawType::kStencilAndColor);
robertphillips391395d2016-03-02 09:26:36 -08001575 if (!pr) {
1576 return false;
1577 }
1578
1579 GrPaint paint;
1580 paint.setCoverageSetOpXPFactory(op, invert);
1581
Brian Salomonf3569f02017-10-24 12:52:33 -04001582 GrPathRenderer::DrawPathArgs args{fRenderTargetContext->drawingManager()->getContext(),
1583 std::move(paint),
1584 ss,
1585 fRenderTargetContext,
1586 &clip,
1587 &clipConservativeBounds,
1588 &viewMatrix,
1589 &shape,
1590 aaType,
1591 fRenderTargetContext->colorSpaceInfo().isGammaCorrect()};
robertphillips391395d2016-03-02 09:26:36 -08001592 pr->drawPath(args);
1593 return true;
1594}
1595
Brian Osman11052242016-10-27 14:47:55 -04001596SkBudgeted GrRenderTargetContextPriv::isBudgeted() const {
robertphillips714712b2016-08-04 06:20:45 -07001597 ASSERT_SINGLE_OWNER_PRIV
1598
Brian Osman11052242016-10-27 14:47:55 -04001599 if (fRenderTargetContext->wasAbandoned()) {
robertphillips714712b2016-08-04 06:20:45 -07001600 return SkBudgeted::kNo;
1601 }
1602
Brian Osman11052242016-10-27 14:47:55 -04001603 SkDEBUGCODE(fRenderTargetContext->validate();)
robertphillips714712b2016-08-04 06:20:45 -07001604
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001605 return fRenderTargetContext->fRenderTargetProxy->isBudgeted();
robertphillips714712b2016-08-04 06:20:45 -07001606}
1607
Brian Salomon2fad74a2017-12-20 13:28:55 -05001608void GrRenderTargetContext::drawShapeUsingPathRenderer(const GrClip& clip,
1609 GrPaint&& paint,
1610 GrAA aa,
1611 const SkMatrix& viewMatrix,
1612 const GrShape& originalShape) {
joshualitt1de610a2016-01-06 08:26:09 -08001613 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001614 RETURN_IF_ABANDONED
Brian Salomondcbb9d92017-07-19 10:53:20 -04001615 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "internalDrawPath", fContext);
1616
Chris Daltondb91c6e2017-09-08 16:25:08 -06001617 SkIRect clipConservativeBounds;
1618 clip.getConservativeBounds(this->width(), this->height(), &clipConservativeBounds, nullptr);
1619
Brian Salomon2fad74a2017-12-20 13:28:55 -05001620 GrShape tempShape;
Brian Salomon7c8460e2017-05-12 11:36:10 -04001621 // NVPR cannot handle hairlines, so this would get picked up by a different stencil and
1622 // cover path renderer (i.e. default path renderer). The hairline renderer produces much
1623 // smoother hairlines than MSAA.
Brian Salomon2fad74a2017-12-20 13:28:55 -05001624 GrAllowMixedSamples allowMixedSamples = originalShape.style().isSimpleHairline()
1625 ? GrAllowMixedSamples::kNo
1626 : GrAllowMixedSamples::kYes;
Brian Salomon7c8460e2017-05-12 11:36:10 -04001627 GrAAType aaType = this->chooseAAType(aa, allowMixedSamples);
robertphillips68737822015-10-29 12:12:21 -07001628 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001629 canDrawArgs.fCaps = this->caps();
robertphillips68737822015-10-29 12:12:21 -07001630 canDrawArgs.fViewMatrix = &viewMatrix;
Brian Salomon2fad74a2017-12-20 13:28:55 -05001631 canDrawArgs.fShape = &originalShape;
Chris Daltondb91c6e2017-09-08 16:25:08 -06001632 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001633 canDrawArgs.fHasUserStencilSettings = false;
robertphillips68737822015-10-29 12:12:21 -07001634
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001635 GrPathRenderer* pr;
Brian Salomon82125e92016-12-10 09:35:48 -05001636 static constexpr GrPathRendererChain::DrawType kType = GrPathRendererChain::DrawType::kColor;
Brian Salomon2fad74a2017-12-20 13:28:55 -05001637 if (originalShape.isEmpty() && !originalShape.inverseFilled()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05001638 return;
1639 }
1640
1641 canDrawArgs.fAAType = aaType;
1642
1643 // Try a 1st time without applying any of the style to the geometry (and barring sw)
1644 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
1645 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
1646
Brian Salomon2fad74a2017-12-20 13:28:55 -05001647 if (!pr && originalShape.style().pathEffect()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05001648 // It didn't work above, so try again with the path effect applied.
Brian Salomon2fad74a2017-12-20 13:28:55 -05001649 tempShape = originalShape.applyStyle(GrStyle::Apply::kPathEffectOnly, styleScale);
1650 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001651 return;
1652 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05001653 canDrawArgs.fShape = &tempShape;
Robert Phillips72152832017-01-25 17:31:35 -05001654 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05001655 }
1656 if (!pr) {
Brian Salomon2fad74a2017-12-20 13:28:55 -05001657 if (canDrawArgs.fShape->style().applies()) {
1658 tempShape = canDrawArgs.fShape->applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec,
1659 styleScale);
1660 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001661 return;
1662 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05001663 canDrawArgs.fShape = &tempShape;
Brian Salomone7df0bb2018-05-07 14:44:57 -04001664 // This time, allow SW renderer
1665 pr = this->drawingManager()->getPathRenderer(canDrawArgs, true, kType);
1666 } else {
1667 pr = this->drawingManager()->getSoftwarePathRenderer();
bsalomon6663acf2016-05-10 09:14:17 -07001668 }
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05001669 }
robertphillipsea461502015-05-26 11:38:03 -07001670
bsalomon8acedde2016-06-24 10:42:16 -07001671 if (!pr) {
robertphillipsea461502015-05-26 11:38:03 -07001672#ifdef SK_DEBUG
1673 SkDebugf("Unable to find path renderer compatible with path.\n");
1674#endif
1675 return;
1676 }
1677
Robert Phillips256c37b2017-03-01 14:32:46 -05001678 GrPathRenderer::DrawPathArgs args{this->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05001679 std::move(paint),
1680 &GrUserStencilSettings::kUnused,
1681 this,
1682 &clip,
Chris Daltondb91c6e2017-09-08 16:25:08 -06001683 &clipConservativeBounds,
Brian Salomon82f44312017-01-11 13:42:54 -05001684 &viewMatrix,
Brian Salomon2fad74a2017-12-20 13:28:55 -05001685 canDrawArgs.fShape,
Brian Salomon82f44312017-01-11 13:42:54 -05001686 aaType,
Brian Salomonf3569f02017-10-24 12:52:33 -04001687 this->colorSpaceInfo().isGammaCorrect()};
bsalomon0aff2fa2015-07-31 06:48:27 -07001688 pr->drawPath(args);
robertphillipsea461502015-05-26 11:38:03 -07001689}
1690
Brian Salomon467921e2017-03-06 16:17:12 -05001691static void op_bounds(SkRect* bounds, const GrOp* op) {
1692 *bounds = op->bounds();
1693 if (op->hasZeroArea()) {
1694 if (op->hasAABloat()) {
1695 bounds->outset(0.5f, 0.5f);
1696 } else {
1697 // We don't know which way the particular GPU will snap lines or points at integer
1698 // coords. So we ensure that the bounds is large enough for either snap.
1699 SkRect before = *bounds;
1700 bounds->roundOut(bounds);
1701 if (bounds->fLeft == before.fLeft) {
1702 bounds->fLeft -= 1;
1703 }
1704 if (bounds->fTop == before.fTop) {
1705 bounds->fTop -= 1;
1706 }
1707 if (bounds->fRight == before.fRight) {
1708 bounds->fRight += 1;
1709 }
1710 if (bounds->fBottom == before.fBottom) {
1711 bounds->fBottom += 1;
1712 }
1713 }
1714 }
1715}
1716
Brian Salomon54d212e2017-03-21 14:22:38 -04001717uint32_t GrRenderTargetContext::addDrawOp(const GrClip& clip, std::unique_ptr<GrDrawOp> op) {
joshualitt1de610a2016-01-06 08:26:09 -08001718 ASSERT_SINGLE_OWNER
Robert Phillipsc0138922017-03-08 11:50:55 -05001719 if (this->drawingManager()->wasAbandoned()) {
1720 return SK_InvalidUniqueID;
1721 }
robertphillips2e1e51f2015-10-15 08:01:48 -07001722 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001723 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "addDrawOp", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -07001724
Brian Salomon467921e2017-03-06 16:17:12 -05001725 // Setup clip
1726 SkRect bounds;
1727 op_bounds(&bounds, op.get());
Brian Salomon97180af2017-03-14 13:42:58 -04001728 GrAppliedClip appliedClip;
Brian Salomon54d212e2017-03-21 14:22:38 -04001729 GrDrawOp::FixedFunctionFlags fixedFunctionFlags = op->fixedFunctionFlags();
1730 if (!clip.apply(fContext, this, fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesHWAA,
1731 fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil, &appliedClip,
1732 &bounds)) {
1733 return SK_InvalidUniqueID;
1734 }
1735
Brian Salomon54d212e2017-03-21 14:22:38 -04001736 if (fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil ||
1737 appliedClip.hasStencilClip()) {
Robert Phillips6b47c7d2017-08-29 07:24:09 -04001738 this->getOpList()->setStencilLoadOp(GrLoadOp::kClear);
Robert Phillips95214472017-08-08 18:00:03 -04001739
Robert Phillips65048132017-08-10 08:44:49 -04001740 this->setNeedsStencil();
Brian Salomon54d212e2017-03-21 14:22:38 -04001741 }
1742
Brian Salomonf3569f02017-10-24 12:52:33 -04001743 GrPixelConfigIsClamped dstIsClamped =
1744 GrGetPixelConfigIsClamped(this->colorSpaceInfo().config());
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001745 GrXferProcessor::DstProxy dstProxy;
Brian Osman9a725dd2017-09-20 09:53:22 -04001746 if (GrDrawOp::RequiresDstTexture::kYes == op->finalize(*this->caps(), &appliedClip,
1747 dstIsClamped)) {
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001748 if (!this->setupDstProxy(this->asRenderTargetProxy(), clip, op->bounds(), &dstProxy)) {
Brian Salomon54d212e2017-03-21 14:22:38 -04001749 return SK_InvalidUniqueID;
1750 }
1751 }
1752
1753 op->setClippedBounds(bounds);
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001754 return this->getRTOpList()->addOp(std::move(op), *this->caps(),
1755 std::move(appliedClip), dstProxy);
Brian Salomon54d212e2017-03-21 14:22:38 -04001756}
1757
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001758bool GrRenderTargetContext::setupDstProxy(GrRenderTargetProxy* rtProxy, const GrClip& clip,
Robert Phillips16d8ec62017-07-27 16:16:25 -04001759 const SkRect& opBounds,
1760 GrXferProcessor::DstProxy* dstProxy) {
Brian Salomon467921e2017-03-06 16:17:12 -05001761 if (this->caps()->textureBarrierSupport()) {
Robert Phillipsbf25d432017-04-07 10:08:53 -04001762 if (GrTextureProxy* texProxy = rtProxy->asTextureProxy()) {
Brian Salomon467921e2017-03-06 16:17:12 -05001763 // The render target is a texture, so we can read from it directly in the shader. The XP
1764 // will be responsible to detect this situation and request a texture barrier.
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001765 dstProxy->setProxy(sk_ref_sp(texProxy));
1766 dstProxy->setOffset(0, 0);
Robert Phillipsbf25d432017-04-07 10:08:53 -04001767 return true;
Brian Salomon467921e2017-03-06 16:17:12 -05001768 }
1769 }
1770
Robert Phillipsbf25d432017-04-07 10:08:53 -04001771 SkIRect copyRect = SkIRect::MakeWH(rtProxy->width(), rtProxy->height());
Brian Salomon467921e2017-03-06 16:17:12 -05001772
Eric Karl74480882017-04-03 14:49:05 -07001773 SkIRect clippedRect;
Robert Phillipsbf25d432017-04-07 10:08:53 -04001774 clip.getConservativeBounds(rtProxy->width(), rtProxy->height(), &clippedRect);
Eric Karl72e551e2017-04-04 13:42:10 -07001775 SkIRect drawIBounds;
Brian Salomon467921e2017-03-06 16:17:12 -05001776 opBounds.roundOut(&drawIBounds);
Brian Salomon859621f2017-03-16 09:21:54 -04001777 // Cover up for any precision issues by outsetting the op bounds a pixel in each direction.
1778 drawIBounds.outset(1, 1);
Eric Karl72e551e2017-04-04 13:42:10 -07001779 if (!clippedRect.intersect(drawIBounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -05001780#ifdef SK_DEBUG
Robert Phillipsbf25d432017-04-07 10:08:53 -04001781 GrCapsDebugf(this->caps(), "setupDstTexture: Missed an early reject bailing on draw.");
Brian Salomon467921e2017-03-06 16:17:12 -05001782#endif
Robert Phillipsbf25d432017-04-07 10:08:53 -04001783 return false;
Brian Salomon467921e2017-03-06 16:17:12 -05001784 }
1785
1786 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
1787 // have per-sample dst values by making the copy multisampled.
1788 GrSurfaceDesc desc;
Eric Karl74480882017-04-03 14:49:05 -07001789 bool rectsMustMatch = false;
1790 bool disallowSubrect = false;
Brian Salomon2a4f9832018-03-03 22:43:43 -05001791 GrSurfaceOrigin origin;
1792 if (!this->caps()->initDescForDstCopy(rtProxy, &desc, &origin, &rectsMustMatch,
1793 &disallowSubrect)) {
Brian Salomon467921e2017-03-06 16:17:12 -05001794 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillipsbf25d432017-04-07 10:08:53 -04001795 desc.fConfig = rtProxy->config();
Greg Daniel1efe3222018-04-04 14:02:51 -04001796 origin = rtProxy->origin();
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 desc.fWidth = rtProxy->width();
1807 desc.fHeight = rtProxy->height();
Eric Karl74480882017-04-03 14:49:05 -07001808 dstPoint = {copyRect.fLeft, copyRect.fTop};
1809 dstOffset = {0, 0};
Robert Phillipsbf25d432017-04-07 10:08:53 -04001810 fit = SkBackingFit::kExact;
Eric Karl74480882017-04-03 14:49:05 -07001811 } else {
1812 desc.fWidth = copyRect.width();
1813 desc.fHeight = copyRect.height();
1814 dstPoint = {0, 0};
1815 dstOffset = {copyRect.fLeft, copyRect.fTop};
Robert Phillipsbf25d432017-04-07 10:08:53 -04001816 fit = SkBackingFit::kApprox;
Eric Karl74480882017-04-03 14:49:05 -07001817 }
Brian Salomon467921e2017-03-06 16:17:12 -05001818
Robert Phillipsbf25d432017-04-07 10:08:53 -04001819 sk_sp<GrSurfaceContext> sContext = fContext->contextPriv().makeDeferredSurfaceContext(
Brian Salomon2a4f9832018-03-03 22:43:43 -05001820 desc, origin, GrMipMapped::kNo, fit, SkBudgeted::kYes,
Brian Salomonf802e752018-02-13 17:13:31 -05001821 sk_ref_sp(this->colorSpaceInfo().colorSpace()));
Robert Phillipsbf25d432017-04-07 10:08:53 -04001822 if (!sContext) {
1823 SkDebugf("setupDstTexture: surfaceContext creation failed.\n");
1824 return false;
1825 }
1826
1827 if (!sContext->copy(rtProxy, copyRect, dstPoint)) {
1828 SkDebugf("setupDstTexture: copy failed.\n");
1829 return false;
1830 }
1831
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001832 dstProxy->setProxy(sContext->asTextureProxyRef());
1833 dstProxy->setOffset(dstOffset);
Robert Phillipsbf25d432017-04-07 10:08:53 -04001834 return true;
robertphillips2334fb62015-06-17 05:43:33 -07001835}