blob: 1dbde175758b132c08ef941b1cd496e87073a40e [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
Robert Phillips7c525e62018-06-12 10:11:12 -040061 void addDrawOp(const GrClip& clip, std::unique_ptr<GrAtlasTextOp> op) override {
Brian Salomonf18b1d82017-10-27 11:30:49 -040062 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,
Robert Phillips7c525e62018-06-12 10:11:12 -040067 const SkIRect& clipBounds) override {
Brian Salomonf18b1d82017-10-27 11:30:49 -040068 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,
Robert Phillips7c525e62018-06-12 10:11:12 -040074 GrPaint* grPaint) override {
Brian Salomonf18b1d82017-10-27 11:30:49 -040075 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
Robert Phillips7c525e62018-06-12 10:11:12 -040084 GrContext* getContext() override {
85 return fRenderTargetContext->fContext;
86 }
87
Brian Salomonf18b1d82017-10-27 11:30:49 -040088private:
89 GrRenderTargetContext* fRenderTargetContext;
90};
joshualittbc907352016-01-13 06:45:40 -080091
Robert Phillips72152832017-01-25 17:31:35 -050092#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this->drawingManager()->getContext())
joshualitt1de610a2016-01-06 08:26:09 -080093#define ASSERT_SINGLE_OWNER \
Robert Phillipsa90aa2b2017-04-10 08:19:26 -040094 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(this->singleOwner());)
robertphillips391395d2016-03-02 09:26:36 -080095#define ASSERT_SINGLE_OWNER_PRIV \
Robert Phillipsa90aa2b2017-04-10 08:19:26 -040096 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fRenderTargetContext->singleOwner());)
Robert Phillips72152832017-01-25 17:31:35 -050097#define RETURN_IF_ABANDONED if (this->drawingManager()->wasAbandoned()) { return; }
98#define RETURN_IF_ABANDONED_PRIV if (fRenderTargetContext->drawingManager()->wasAbandoned()) { return; }
99#define RETURN_FALSE_IF_ABANDONED if (this->drawingManager()->wasAbandoned()) { return false; }
100#define RETURN_FALSE_IF_ABANDONED_PRIV if (fRenderTargetContext->drawingManager()->wasAbandoned()) { return false; }
101#define RETURN_NULL_IF_ABANDONED if (this->drawingManager()->wasAbandoned()) { return nullptr; }
robertphillipsea461502015-05-26 11:38:03 -0700102
Brian Salomone225b562017-06-14 13:00:03 -0400103//////////////////////////////////////////////////////////////////////////////
104
105GrAAType GrChooseAAType(GrAA aa, GrFSAAType fsaaType, GrAllowMixedSamples allowMixedSamples,
106 const GrCaps& caps) {
107 if (GrAA::kNo == aa) {
108 // On some devices we cannot disable MSAA if it is enabled so we make the AA type reflect
109 // that.
110 if (fsaaType == GrFSAAType::kUnifiedMSAA && !caps.multisampleDisableSupport()) {
111 return GrAAType::kMSAA;
112 }
113 return GrAAType::kNone;
114 }
115 switch (fsaaType) {
116 case GrFSAAType::kNone:
117 return GrAAType::kCoverage;
118 case GrFSAAType::kUnifiedMSAA:
119 return GrAAType::kMSAA;
120 case GrFSAAType::kMixedSamples:
121 return GrAllowMixedSamples::kYes == allowMixedSamples ? GrAAType::kMixedSamples
122 : GrAAType::kCoverage;
123 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400124 SK_ABORT("Unexpected fsaa type");
Brian Salomone225b562017-06-14 13:00:03 -0400125 return GrAAType::kNone;
126}
127
128//////////////////////////////////////////////////////////////////////////////
129
robertphillipsea461502015-05-26 11:38:03 -0700130class AutoCheckFlush {
131public:
halcanary9d524f22016-03-29 09:03:52 -0700132 AutoCheckFlush(GrDrawingManager* drawingManager) : fDrawingManager(drawingManager) {
robertphillips77a2e522015-10-17 07:43:27 -0700133 SkASSERT(fDrawingManager);
134 }
bsalomonb77a9072016-09-07 10:02:04 -0700135 ~AutoCheckFlush() { fDrawingManager->flushIfNecessary(); }
robertphillipsea461502015-05-26 11:38:03 -0700136
137private:
robertphillips77a2e522015-10-17 07:43:27 -0700138 GrDrawingManager* fDrawingManager;
robertphillipsea461502015-05-26 11:38:03 -0700139};
140
Brian Osman11052242016-10-27 14:47:55 -0400141bool GrRenderTargetContext::wasAbandoned() const {
Robert Phillips72152832017-01-25 17:31:35 -0500142 return this->drawingManager()->wasAbandoned();
robertphillips7761d612016-05-16 09:14:53 -0700143}
144
Robert Phillipsf2361d22016-10-25 14:20:06 -0400145// In MDB mode the reffing of the 'getLastOpList' call's result allows in-progress
Brian Osman11052242016-10-27 14:47:55 -0400146// GrOpLists to be picked up and added to by renderTargetContexts lower in the call
Robert Phillipsf2361d22016-10-25 14:20:06 -0400147// stack. When this occurs with a closed GrOpList, a new one will be allocated
Brian Osman11052242016-10-27 14:47:55 -0400148// when the renderTargetContext attempts to use it (via getOpList).
149GrRenderTargetContext::GrRenderTargetContext(GrContext* context,
150 GrDrawingManager* drawingMgr,
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400151 sk_sp<GrRenderTargetProxy> rtp,
Brian Osman11052242016-10-27 14:47:55 -0400152 sk_sp<SkColorSpace> colorSpace,
153 const SkSurfaceProps* surfaceProps,
154 GrAuditTrail* auditTrail,
Robert Phillips941d1442017-06-14 16:37:02 -0400155 GrSingleOwner* singleOwner,
156 bool managedOpList)
Brian Salomonf3569f02017-10-24 12:52:33 -0400157 : GrSurfaceContext(context, drawingMgr, rtp->config(), std::move(colorSpace), auditTrail,
158 singleOwner)
159 , fRenderTargetProxy(std::move(rtp))
160 , fOpList(sk_ref_sp(fRenderTargetProxy->getLastRenderTargetOpList()))
Brian Salomonf3569f02017-10-24 12:52:33 -0400161 , fSurfaceProps(SkSurfacePropsCopyOrDefault(surfaceProps))
162 , fManagedOpList(managedOpList) {
Robert Phillips4150eea2018-02-07 17:08:21 -0500163 GrResourceProvider* resourceProvider = context->contextPriv().resourceProvider();
164 if (resourceProvider && !resourceProvider->explicitlyAllocateGPUResources()) {
165 // MDB TODO: to ensure all resources still get allocated in the correct order in the hybrid
166 // world we need to get the correct opList here so that it, in turn, can grab and hold
167 // its rendertarget.
168 this->getRTOpList();
169 }
170
Brian Salomonf18b1d82017-10-27 11:30:49 -0400171 fTextTarget.reset(new TextTarget(this));
robertphillips2e1e51f2015-10-15 08:01:48 -0700172 SkDEBUGCODE(this->validate();)
robertphillipsea461502015-05-26 11:38:03 -0700173}
174
robertphillips2e1e51f2015-10-15 08:01:48 -0700175#ifdef SK_DEBUG
Brian Osman11052242016-10-27 14:47:55 -0400176void GrRenderTargetContext::validate() const {
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400177 SkASSERT(fRenderTargetProxy);
178 fRenderTargetProxy->validate(fContext);
robertphillipsa106c622015-10-16 09:07:06 -0700179
Robert Phillipsf2361d22016-10-25 14:20:06 -0400180 if (fOpList && !fOpList->isClosed()) {
Robert Phillipsdc83b892017-04-13 12:23:54 -0400181 SkASSERT(fRenderTargetProxy->getLastOpList() == fOpList.get());
robertphillipsa106c622015-10-16 09:07:06 -0700182 }
robertphillips2e1e51f2015-10-15 08:01:48 -0700183}
184#endif
185
Brian Osman11052242016-10-27 14:47:55 -0400186GrRenderTargetContext::~GrRenderTargetContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800187 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700188}
189
Robert Phillipsf200a902017-01-30 13:27:37 -0500190GrTextureProxy* GrRenderTargetContext::asTextureProxy() {
Robert Phillipseaa86252016-11-08 13:49:39 +0000191 return fRenderTargetProxy->asTextureProxy();
192}
193
Greg Daniele252f082017-10-23 16:05:23 -0400194const GrTextureProxy* GrRenderTargetContext::asTextureProxy() const {
195 return fRenderTargetProxy->asTextureProxy();
196}
197
Robert Phillipsf200a902017-01-30 13:27:37 -0500198sk_sp<GrTextureProxy> GrRenderTargetContext::asTextureProxyRef() {
199 return sk_ref_sp(fRenderTargetProxy->asTextureProxy());
200}
201
Greg Daniele252f082017-10-23 16:05:23 -0400202GrMipMapped GrRenderTargetContext::mipMapped() const {
203 if (const GrTextureProxy* proxy = this->asTextureProxy()) {
204 return proxy->mipMapped();
205 }
206 return GrMipMapped::kNo;
207}
208
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400209GrRenderTargetOpList* GrRenderTargetContext::getRTOpList() {
joshualitt1de610a2016-01-06 08:26:09 -0800210 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700211 SkDEBUGCODE(this->validate();)
212
Robert Phillipsf2361d22016-10-25 14:20:06 -0400213 if (!fOpList || fOpList->isClosed()) {
Robert Phillips941d1442017-06-14 16:37:02 -0400214 fOpList = this->drawingManager()->newRTOpList(fRenderTargetProxy.get(), fManagedOpList);
robertphillipsa106c622015-10-16 09:07:06 -0700215 }
216
Robert Phillipsdc83b892017-04-13 12:23:54 -0400217 return fOpList.get();
robertphillipsa106c622015-10-16 09:07:06 -0700218}
219
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400220GrOpList* GrRenderTargetContext::getOpList() {
221 return this->getRTOpList();
robertphillipsea461502015-05-26 11:38:03 -0700222}
223
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500224void GrRenderTargetContext::drawPosText(const GrClip& clip, const SkPaint& paint,
225 const SkMatrix& viewMatrix, const char text[],
226 size_t byteLength, const SkScalar pos[],
Brian Salomon82f44312017-01-11 13:42:54 -0500227 int scalarsPerPosition, const SkPoint& offset,
228 const SkIRect& clipBounds) {
joshualitt1de610a2016-01-06 08:26:09 -0800229 ASSERT_SINGLE_OWNER
robertphillips2d70dcb2015-10-06 07:38:23 -0700230 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700231 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400232 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawPosText", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -0700233
Herb Derby26cbe512018-05-24 14:39:01 -0400234 GrTextContext* atlasTextContext = this->drawingManager()->getTextContext();
Brian Salomonf18b1d82017-10-27 11:30:49 -0400235 atlasTextContext->drawPosText(fContext, fTextTarget.get(), clip, paint, viewMatrix,
236 fSurfaceProps, text, byteLength, pos, scalarsPerPosition, offset,
237 clipBounds);
robertphillips2334fb62015-06-17 05:43:33 -0700238}
robertphillipscaef3452015-11-11 13:18:11 -0800239
Robert Phillips38580452018-06-28 12:00:35 +0000240void GrRenderTargetContext::drawTextBlob(const GrClip& clip, const SkPaint& paint,
241 const SkMatrix& viewMatrix, const SkTextBlob* blob,
242 SkScalar x, SkScalar y, SkDrawFilter* filter,
243 const SkIRect& clipBounds) {
joshualitt1de610a2016-01-06 08:26:09 -0800244 ASSERT_SINGLE_OWNER
robertphillips2d70dcb2015-10-06 07:38:23 -0700245 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700246 SkDEBUGCODE(this->validate();)
Robert Phillips38580452018-06-28 12:00:35 +0000247 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTextBlob", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -0700248
Herb Derby26cbe512018-05-24 14:39:01 -0400249 GrTextContext* atlasTextContext = this->drawingManager()->getTextContext();
Robert Phillips38580452018-06-28 12:00:35 +0000250 atlasTextContext->drawTextBlob(fContext, fTextTarget.get(), clip, paint, viewMatrix,
251 fSurfaceProps, blob, x, y, filter, clipBounds);
robertphillipsea461502015-05-26 11:38:03 -0700252}
253
Brian Osman11052242016-10-27 14:47:55 -0400254void GrRenderTargetContext::discard() {
joshualitt1de610a2016-01-06 08:26:09 -0800255 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700256 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700257 SkDEBUGCODE(this->validate();)
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400258 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "discard", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700259
Robert Phillips72152832017-01-25 17:31:35 -0500260 AutoCheckFlush acf(this->drawingManager());
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400261
Robert Phillips380b90c2017-08-30 07:41:07 -0400262 this->getRTOpList()->discard();
robertphillipsea461502015-05-26 11:38:03 -0700263}
264
Brian Osman11052242016-10-27 14:47:55 -0400265void GrRenderTargetContext::clear(const SkIRect* rect,
266 const GrColor color,
Chris Dalton344e9032017-12-11 15:42:09 -0700267 CanClearFullscreen canClearFullscreen) {
joshualitt1de610a2016-01-06 08:26:09 -0800268 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700269 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700270 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400271 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "clear", fContext);
robertphillipsea461502015-05-26 11:38:03 -0700272
Robert Phillips72152832017-01-25 17:31:35 -0500273 AutoCheckFlush acf(this->drawingManager());
Chris Dalton344e9032017-12-11 15:42:09 -0700274 this->internalClear(rect ? GrFixedClip(*rect) : GrFixedClip::Disabled(), color,
275 canClearFullscreen);
csmartdalton29df7602016-08-31 11:55:52 -0700276}
robertphillips9199a9f2016-07-13 07:48:43 -0700277
Robert Phillips784b7bf2016-12-09 13:35:02 -0500278void GrRenderTargetContextPriv::absClear(const SkIRect* clearRect, const GrColor color) {
279 ASSERT_SINGLE_OWNER_PRIV
280 RETURN_IF_ABANDONED_PRIV
281 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400282 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "absClear",
283 fRenderTargetContext->fContext);
Robert Phillips784b7bf2016-12-09 13:35:02 -0500284
Robert Phillips72152832017-01-25 17:31:35 -0500285 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500286
Brian Salomonbb5711a2017-05-17 13:49:59 -0400287 SkIRect rtRect = SkIRect::MakeWH(fRenderTargetContext->fRenderTargetProxy->worstCaseWidth(),
288 fRenderTargetContext->fRenderTargetProxy->worstCaseHeight());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500289
290 if (clearRect) {
291 if (clearRect->contains(rtRect)) {
292 clearRect = nullptr; // full screen
293 } else {
294 if (!rtRect.intersect(*clearRect)) {
295 return;
296 }
297 }
298 }
299
300 // TODO: in a post-MDB world this should be handled at the OpList level.
301 // An op-list that is initially cleared and has no other ops should receive an
302 // extra draw.
Brian Salomon43f8bf02017-10-18 08:33:29 -0400303 // This path doesn't handle coalescing of full screen clears b.c. it
304 // has to clear the entire render target - not just the content area.
305 // It could be done but will take more finagling.
Robert Phillips7c525e62018-06-12 10:11:12 -0400306 std::unique_ptr<GrOp> op(GrClearOp::Make(fRenderTargetContext->fContext, rtRect,
307 color, !clearRect));
Brian Salomon43f8bf02017-10-18 08:33:29 -0400308 if (!op) {
309 return;
Robert Phillips784b7bf2016-12-09 13:35:02 -0500310 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400311 fRenderTargetContext->getRTOpList()->addOp(std::move(op), *fRenderTargetContext->caps());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500312}
313
Brian Osman11052242016-10-27 14:47:55 -0400314void GrRenderTargetContextPriv::clear(const GrFixedClip& clip,
315 const GrColor color,
Chris Dalton344e9032017-12-11 15:42:09 -0700316 CanClearFullscreen canClearFullscreen) {
csmartdalton29df7602016-08-31 11:55:52 -0700317 ASSERT_SINGLE_OWNER_PRIV
318 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400319 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400320 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "clear",
321 fRenderTargetContext->fContext);
csmartdalton29df7602016-08-31 11:55:52 -0700322
Robert Phillips72152832017-01-25 17:31:35 -0500323 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Chris Dalton344e9032017-12-11 15:42:09 -0700324 fRenderTargetContext->internalClear(clip, color, canClearFullscreen);
csmartdalton29df7602016-08-31 11:55:52 -0700325}
326
Brian Osman11052242016-10-27 14:47:55 -0400327void GrRenderTargetContext::internalClear(const GrFixedClip& clip,
328 const GrColor color,
Chris Dalton344e9032017-12-11 15:42:09 -0700329 CanClearFullscreen canClearFullscreen) {
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700330 bool isFull = false;
331 if (!clip.hasWindowRectangles()) {
Brian Salomond818ebf2018-07-02 14:08:49 +0000332 isFull = !clip.scissorEnabled() ||
Chris Dalton344e9032017-12-11 15:42:09 -0700333 (CanClearFullscreen::kYes == canClearFullscreen &&
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400334 this->caps()->preferFullscreenClears()) ||
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700335 clip.scissorRect().contains(SkIRect::MakeWH(this->width(), this->height()));
336 }
robertphillips9199a9f2016-07-13 07:48:43 -0700337
Brian Salomon43f8bf02017-10-18 08:33:29 -0400338 if (isFull) {
Robert Phillips7c525e62018-06-12 10:11:12 -0400339 this->getRTOpList()->fullClear(fContext, color);
robertphillips9199a9f2016-07-13 07:48:43 -0700340 } else {
Robert Phillips7c525e62018-06-12 10:11:12 -0400341 std::unique_ptr<GrOp> op(GrClearOp::Make(fContext, clip, color, this->asSurfaceProxy()));
Brian Salomon42521e82016-12-07 16:44:58 -0500342 if (!op) {
csmartdalton29df7602016-08-31 11:55:52 -0700343 return;
344 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400345 this->getRTOpList()->addOp(std::move(op), *this->caps());
robertphillips9199a9f2016-07-13 07:48:43 -0700346 }
robertphillipsea461502015-05-26 11:38:03 -0700347}
348
Brian Osman11052242016-10-27 14:47:55 -0400349void GrRenderTargetContext::drawPaint(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500350 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400351 const SkMatrix& viewMatrix) {
joshualitt1de610a2016-01-06 08:26:09 -0800352 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700353 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700354 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400355 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawPaint", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700356
robertphillipsea461502015-05-26 11:38:03 -0700357 // set rect to be big enough to fill the space, but not super-huge, so we
358 // don't overflow fixed-point implementations
robertphillips13a7eee2016-08-31 15:06:24 -0700359
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400360 SkRect r = fRenderTargetProxy->getBoundsRect();
robertphillipsea461502015-05-26 11:38:03 -0700361
bsalomoncb31e512016-08-26 10:48:19 -0700362 SkRRect rrect;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500363 GrAA aa;
bsalomoncb31e512016-08-26 10:48:19 -0700364 // Check if we can replace a clipRRect()/drawPaint() with a drawRRect(). We only do the
365 // transformation for non-rect rrects. Rects caused a performance regression on an Android
366 // test that needs investigation. We also skip cases where there are fragment processors
367 // because they may depend on having correct local coords and this path draws in device space
368 // without a local matrix.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500369 if (!paint.numTotalFragmentProcessors() && clip.isRRect(r, &rrect, &aa) && !rrect.isRect()) {
Brian Salomon82f44312017-01-11 13:42:54 -0500370 this->drawRRect(GrNoClip(), std::move(paint), aa, SkMatrix::I(), rrect,
371 GrStyle::SimpleFill());
bsalomoncb31e512016-08-26 10:48:19 -0700372 return;
373 }
374
robertphillipsea461502015-05-26 11:38:03 -0700375
376 bool isPerspective = viewMatrix.hasPerspective();
377
378 // We attempt to map r by the inverse matrix and draw that. mapRect will
379 // map the four corners and bound them with a new rect. This will not
380 // produce a correct result for some perspective matrices.
381 if (!isPerspective) {
reeda39667c2016-08-22 06:39:49 -0700382 if (!SkMatrixPriv::InverseMapRect(viewMatrix, &r, r)) {
robertphillipsea461502015-05-26 11:38:03 -0700383 return;
384 }
Brian Salomon82f44312017-01-11 13:42:54 -0500385 this->drawRect(clip, std::move(paint), GrAA::kNo, viewMatrix, r);
robertphillipsea461502015-05-26 11:38:03 -0700386 } else {
387 SkMatrix localMatrix;
388 if (!viewMatrix.invert(&localMatrix)) {
robertphillipsea461502015-05-26 11:38:03 -0700389 return;
390 }
391
Robert Phillips72152832017-01-25 17:31:35 -0500392 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700393
Brian Salomonbaaf4392017-06-15 09:59:23 -0400394 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeNonAAFillWithLocalMatrix(
Robert Phillips7c525e62018-06-12 10:11:12 -0400395 fContext, std::move(paint), SkMatrix::I(), localMatrix, r, GrAAType::kNone);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400396 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -0700397 }
398}
399
robertphillipsea461502015-05-26 11:38:03 -0700400static inline bool rect_contains_inclusive(const SkRect& rect, const SkPoint& point) {
401 return point.fX >= rect.fLeft && point.fX <= rect.fRight &&
402 point.fY >= rect.fTop && point.fY <= rect.fBottom;
403}
404
csmartdalton97f6cd52016-07-13 13:37:08 -0700405// Attempts to crop a rect and optional local rect to the clip boundaries.
406// Returns false if the draw can be skipped entirely.
robertphillips13a7eee2016-08-31 15:06:24 -0700407static bool crop_filled_rect(int width, int height, const GrClip& clip,
csmartdalton97f6cd52016-07-13 13:37:08 -0700408 const SkMatrix& viewMatrix, SkRect* rect,
409 SkRect* localRect = nullptr) {
410 if (!viewMatrix.rectStaysRect()) {
411 return true;
412 }
413
csmartdalton97f6cd52016-07-13 13:37:08 -0700414 SkIRect clipDevBounds;
415 SkRect clipBounds;
csmartdalton97f6cd52016-07-13 13:37:08 -0700416
robertphillips13a7eee2016-08-31 15:06:24 -0700417 clip.getConservativeBounds(width, height, &clipDevBounds);
reeda39667c2016-08-22 06:39:49 -0700418 if (!SkMatrixPriv::InverseMapRect(viewMatrix, &clipBounds, SkRect::Make(clipDevBounds))) {
419 return false;
420 }
csmartdalton97f6cd52016-07-13 13:37:08 -0700421
422 if (localRect) {
423 if (!rect->intersects(clipBounds)) {
424 return false;
425 }
426 const SkScalar dx = localRect->width() / rect->width();
427 const SkScalar dy = localRect->height() / rect->height();
428 if (clipBounds.fLeft > rect->fLeft) {
429 localRect->fLeft += (clipBounds.fLeft - rect->fLeft) * dx;
430 rect->fLeft = clipBounds.fLeft;
431 }
432 if (clipBounds.fTop > rect->fTop) {
433 localRect->fTop += (clipBounds.fTop - rect->fTop) * dy;
434 rect->fTop = clipBounds.fTop;
435 }
436 if (clipBounds.fRight < rect->fRight) {
437 localRect->fRight -= (rect->fRight - clipBounds.fRight) * dx;
438 rect->fRight = clipBounds.fRight;
439 }
440 if (clipBounds.fBottom < rect->fBottom) {
441 localRect->fBottom -= (rect->fBottom - clipBounds.fBottom) * dy;
442 rect->fBottom = clipBounds.fBottom;
443 }
444 return true;
445 }
446
447 return rect->intersect(clipBounds);
448}
449
Brian Osman11052242016-10-27 14:47:55 -0400450bool GrRenderTargetContext::drawFilledRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500451 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500452 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400453 const SkMatrix& viewMatrix,
454 const SkRect& rect,
455 const GrUserStencilSettings* ss) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700456 SkRect croppedRect = rect;
Robert Phillips784b7bf2016-12-09 13:35:02 -0500457 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix, &croppedRect)) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700458 return true;
459 }
robertphillips44302392016-07-08 14:43:03 -0700460
Brian Salomon7c8460e2017-05-12 11:36:10 -0400461 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400462 std::unique_ptr<GrDrawOp> op;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500463 if (GrAAType::kCoverage == aaType) {
Robert Phillips7c525e62018-06-12 10:11:12 -0400464 op = GrRectOpFactory::MakeAAFill(fContext, std::move(paint), viewMatrix, croppedRect, ss);
robertphillips391395d2016-03-02 09:26:36 -0800465 } else {
Robert Phillips7c525e62018-06-12 10:11:12 -0400466 op = GrRectOpFactory::MakeNonAAFill(fContext, std::move(paint), viewMatrix, croppedRect,
467 aaType, ss);
robertphillips391395d2016-03-02 09:26:36 -0800468 }
Brian Salomonbaaf4392017-06-15 09:59:23 -0400469 if (!op) {
470 return false;
471 }
472 this->addDrawOp(clip, std::move(op));
473 return true;
robertphillips391395d2016-03-02 09:26:36 -0800474}
475
Brian Osman11052242016-10-27 14:47:55 -0400476void GrRenderTargetContext::drawRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500477 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500478 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400479 const SkMatrix& viewMatrix,
480 const SkRect& rect,
481 const GrStyle* style) {
bsalomon6663acf2016-05-10 09:14:17 -0700482 if (!style) {
483 style = &GrStyle::SimpleFill();
484 }
joshualitt1de610a2016-01-06 08:26:09 -0800485 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700486 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700487 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400488 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRect", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700489
bsalomon6663acf2016-05-10 09:14:17 -0700490 // Path effects should've been devolved to a path in SkGpuDevice
491 SkASSERT(!style->pathEffect());
robertphillipsea461502015-05-26 11:38:03 -0700492
Robert Phillips72152832017-01-25 17:31:35 -0500493 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700494
bsalomon6663acf2016-05-10 09:14:17 -0700495 const SkStrokeRec& stroke = style->strokeRec();
Robert Phillipsc90b4db2018-04-23 15:12:00 +0000496 if (stroke.getStyle() == SkStrokeRec::kFill_Style && !paint.numCoverageFragmentProcessors()) {
Brian Salomon43f8bf02017-10-18 08:33:29 -0400497 // Check if this is a full RT draw and can be replaced with a clear. We don't bother
498 // checking cases where the RT is fully inside a stroke.
499 SkRect rtRect = fRenderTargetProxy->getBoundsRect();
500 // Does the clip contain the entire RT?
501 if (clip.quickContains(rtRect)) {
502 SkMatrix invM;
503 if (!viewMatrix.invert(&invM)) {
504 return;
505 }
506 // Does the rect bound the RT?
Brian Salomona33b67c2018-05-17 10:42:14 -0400507 GrQuad quad(rtRect, invM);
Brian Salomon43f8bf02017-10-18 08:33:29 -0400508 if (rect_contains_inclusive(rect, quad.point(0)) &&
509 rect_contains_inclusive(rect, quad.point(1)) &&
510 rect_contains_inclusive(rect, quad.point(2)) &&
511 rect_contains_inclusive(rect, quad.point(3))) {
512 // Will it blend?
513 GrColor clearColor;
514 if (paint.isConstantBlendedColor(&clearColor)) {
Chris Dalton344e9032017-12-11 15:42:09 -0700515 this->clear(nullptr, clearColor,
516 GrRenderTargetContext::CanClearFullscreen::kYes);
robertphillipsea461502015-05-26 11:38:03 -0700517 return;
518 }
519 }
520 }
robertphillips44302392016-07-08 14:43:03 -0700521
Brian Salomon82f44312017-01-11 13:42:54 -0500522 if (this->drawFilledRect(clip, std::move(paint), aa, viewMatrix, rect, nullptr)) {
robertphillips44302392016-07-08 14:43:03 -0700523 return;
524 }
bsalomona7d85ba2016-07-06 11:54:59 -0700525 } else if (stroke.getStyle() == SkStrokeRec::kStroke_Style ||
526 stroke.getStyle() == SkStrokeRec::kHairline_Style) {
527 if ((!rect.width() || !rect.height()) &&
528 SkStrokeRec::kHairline_Style != stroke.getStyle()) {
529 SkScalar r = stroke.getWidth() / 2;
530 // TODO: Move these stroke->fill fallbacks to GrShape?
531 switch (stroke.getJoin()) {
532 case SkPaint::kMiter_Join:
Brian Salomon82f44312017-01-11 13:42:54 -0500533 this->drawRect(
534 clip, std::move(paint), aa, viewMatrix,
535 {rect.fLeft - r, rect.fTop - r, rect.fRight + r, rect.fBottom + r},
536 &GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700537 return;
538 case SkPaint::kRound_Join:
539 // Raster draws nothing when both dimensions are empty.
540 if (rect.width() || rect.height()){
541 SkRRect rrect = SkRRect::MakeRectXY(rect.makeOutset(r, r), r, r);
Brian Salomon82f44312017-01-11 13:42:54 -0500542 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect,
543 GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700544 return;
545 }
546 case SkPaint::kBevel_Join:
547 if (!rect.width()) {
Brian Salomon82f44312017-01-11 13:42:54 -0500548 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700549 {rect.fLeft - r, rect.fTop, rect.fRight + r, rect.fBottom},
550 &GrStyle::SimpleFill());
551 } else {
Brian Salomon82f44312017-01-11 13:42:54 -0500552 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700553 {rect.fLeft, rect.fTop - r, rect.fRight, rect.fBottom + r},
554 &GrStyle::SimpleFill());
555 }
556 return;
557 }
558 }
robertphillips44302392016-07-08 14:43:03 -0700559
Brian Salomonbaaf4392017-06-15 09:59:23 -0400560 std::unique_ptr<GrDrawOp> op;
robertphillips44302392016-07-08 14:43:03 -0700561
Brian Salomon7c8460e2017-05-12 11:36:10 -0400562 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500563 if (GrAAType::kCoverage == aaType) {
cdaltonbb539482016-01-04 09:48:25 -0800564 // The stroke path needs the rect to remain axis aligned (no rotation or skew).
565 if (viewMatrix.rectStaysRect()) {
Robert Phillips7c525e62018-06-12 10:11:12 -0400566 op = GrRectOpFactory::MakeAAStroke(fContext, std::move(paint), viewMatrix, rect,
567 stroke);
cdaltonbb539482016-01-04 09:48:25 -0800568 }
robertphillipsea461502015-05-26 11:38:03 -0700569 } else {
Robert Phillips7c525e62018-06-12 10:11:12 -0400570 op = GrRectOpFactory::MakeNonAAStroke(fContext, std::move(paint), viewMatrix, rect,
571 stroke, aaType);
robertphillips391395d2016-03-02 09:26:36 -0800572 }
robertphillips4bc31812016-03-01 12:22:49 -0800573
Brian Salomon42521e82016-12-07 16:44:58 -0500574 if (op) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400575 this->addDrawOp(clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700576 return;
robertphillips4bc31812016-03-01 12:22:49 -0800577 }
robertphillips4bc31812016-03-01 12:22:49 -0800578 }
Brian Salomon2fad74a2017-12-20 13:28:55 -0500579 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, GrShape(rect, *style));
robertphillipsea461502015-05-26 11:38:03 -0700580}
581
Robert Phillipsec2249f2016-11-09 08:54:35 -0500582int GrRenderTargetContextPriv::maxWindowRectangles() const {
583 return fRenderTargetContext->fRenderTargetProxy->maxWindowRectangles(
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400584 *fRenderTargetContext->caps());
Robert Phillipsec2249f2016-11-09 08:54:35 -0500585}
586
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000587void GrRenderTargetContextPriv::clearStencilClip(const GrFixedClip& clip, bool insideStencilMask) {
robertphillips976f5f02016-06-03 10:59:20 -0700588 ASSERT_SINGLE_OWNER_PRIV
589 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400590 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400591 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "clearStencilClip",
592 fRenderTargetContext->fContext);
robertphillips976f5f02016-06-03 10:59:20 -0700593
Robert Phillips72152832017-01-25 17:31:35 -0500594 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400595
Robert Phillips7c525e62018-06-12 10:11:12 -0400596 GrRenderTargetProxy* rtProxy = fRenderTargetContext->fRenderTargetProxy.get();
597 std::unique_ptr<GrOp> op(GrClearStencilClipOp::Make(fRenderTargetContext->fContext,
598 clip, insideStencilMask, rtProxy));
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400599 if (!op) {
Robert Phillipse60ad622016-11-17 10:22:48 -0500600 return;
601 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400602 fRenderTargetContext->getRTOpList()->addOp(std::move(op), *fRenderTargetContext->caps());
robertphillips976f5f02016-06-03 10:59:20 -0700603}
604
Chris Daltonbbfd5162017-11-07 13:35:22 -0700605void GrRenderTargetContextPriv::stencilPath(const GrHardClip& clip,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500606 GrAAType aaType,
Brian Osman11052242016-10-27 14:47:55 -0400607 const SkMatrix& viewMatrix,
608 const GrPath* path) {
Brian Salomon467921e2017-03-06 16:17:12 -0500609 ASSERT_SINGLE_OWNER_PRIV
610 RETURN_IF_ABANDONED_PRIV
611 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400612 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "stencilPath",
613 fRenderTargetContext->fContext);
Brian Salomon467921e2017-03-06 16:17:12 -0500614
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500615 SkASSERT(aaType != GrAAType::kCoverage);
Brian Salomon467921e2017-03-06 16:17:12 -0500616
617 bool useHWAA = GrAATypeIsHW(aaType);
618 // TODO: extract portions of checkDraw that are relevant to path stenciling.
619 SkASSERT(path);
620 SkASSERT(fRenderTargetContext->caps()->shaderCaps()->pathRenderingSupport());
621
622 // FIXME: Use path bounds instead of this WAR once
623 // https://bugs.chromium.org/p/skia/issues/detail?id=5640 is resolved.
624 SkRect bounds = SkRect::MakeIWH(fRenderTargetContext->width(), fRenderTargetContext->height());
625
626 // Setup clip
Chris Daltonbbfd5162017-11-07 13:35:22 -0700627 GrAppliedHardClip appliedClip;
628 if (!clip.apply(fRenderTargetContext->width(), fRenderTargetContext->height(), &appliedClip,
629 &bounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -0500630 return;
631 }
632
Robert Phillips65048132017-08-10 08:44:49 -0400633 fRenderTargetContext->setNeedsStencil();
Brian Salomon467921e2017-03-06 16:17:12 -0500634
Robert Phillips7c525e62018-06-12 10:11:12 -0400635 std::unique_ptr<GrOp> op = GrStencilPathOp::Make(fRenderTargetContext->fContext,
636 viewMatrix,
Brian Salomon467921e2017-03-06 16:17:12 -0500637 useHWAA,
638 path->getFillType(),
639 appliedClip.hasStencilClip(),
Brian Salomon467921e2017-03-06 16:17:12 -0500640 appliedClip.scissorState(),
Brian Salomon467921e2017-03-06 16:17:12 -0500641 path);
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400642 if (!op) {
643 return;
644 }
Brian Salomon97180af2017-03-14 13:42:58 -0400645 op->setClippedBounds(bounds);
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400646 fRenderTargetContext->getRTOpList()->addOp(std::move(op), *fRenderTargetContext->caps());
robertphillips976f5f02016-06-03 10:59:20 -0700647}
648
Chris Daltonbbfd5162017-11-07 13:35:22 -0700649void GrRenderTargetContextPriv::stencilRect(const GrHardClip& clip,
Brian Osman11052242016-10-27 14:47:55 -0400650 const GrUserStencilSettings* ss,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500651 GrAAType aaType,
Brian Osman11052242016-10-27 14:47:55 -0400652 const SkMatrix& viewMatrix,
653 const SkRect& rect) {
robertphillips976f5f02016-06-03 10:59:20 -0700654 ASSERT_SINGLE_OWNER_PRIV
655 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400656 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400657 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "stencilRect",
658 fRenderTargetContext->fContext);
659
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500660 SkASSERT(GrAAType::kCoverage != aaType);
Robert Phillips72152832017-01-25 17:31:35 -0500661 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips976f5f02016-06-03 10:59:20 -0700662
663 GrPaint paint;
Brian Salomona1633922017-01-09 11:46:10 -0500664 paint.setXPFactory(GrDisableColorXPFactory::Get());
Robert Phillips7c525e62018-06-12 10:11:12 -0400665 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeNonAAFill(fRenderTargetContext->fContext,
666 std::move(paint), viewMatrix,
667 rect, aaType, ss);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400668 fRenderTargetContext->addDrawOp(clip, std::move(op));
robertphillips976f5f02016-06-03 10:59:20 -0700669}
670
Chris Daltonbbfd5162017-11-07 13:35:22 -0700671bool GrRenderTargetContextPriv::drawAndStencilRect(const GrHardClip& clip,
Brian Osman11052242016-10-27 14:47:55 -0400672 const GrUserStencilSettings* ss,
673 SkRegion::Op op,
674 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500675 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400676 const SkMatrix& viewMatrix,
677 const SkRect& rect) {
robertphillips391395d2016-03-02 09:26:36 -0800678 ASSERT_SINGLE_OWNER_PRIV
679 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400680 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400681 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawAndStencilRect",
682 fRenderTargetContext->fContext);
robertphillips391395d2016-03-02 09:26:36 -0800683
Robert Phillips72152832017-01-25 17:31:35 -0500684 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -0800685
686 GrPaint paint;
robertphillips391395d2016-03-02 09:26:36 -0800687 paint.setCoverageSetOpXPFactory(op, invert);
688
Brian Salomon82f44312017-01-11 13:42:54 -0500689 if (fRenderTargetContext->drawFilledRect(clip, std::move(paint), aa, viewMatrix, rect, ss)) {
robertphillips391395d2016-03-02 09:26:36 -0800690 return true;
691 }
robertphillips391395d2016-03-02 09:26:36 -0800692 SkPath path;
693 path.setIsVolatile(true);
694 path.addRect(rect);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500695 return this->drawAndStencilPath(clip, ss, op, invert, aa, viewMatrix, path);
robertphillips391395d2016-03-02 09:26:36 -0800696}
697
Brian Osman11052242016-10-27 14:47:55 -0400698void GrRenderTargetContext::fillRectToRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500699 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500700 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400701 const SkMatrix& viewMatrix,
702 const SkRect& rectToDraw,
703 const SkRect& localRect) {
joshualitt1de610a2016-01-06 08:26:09 -0800704 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700705 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700706 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400707 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "fillRectToRect", fContext);
robertphillipsea461502015-05-26 11:38:03 -0700708
csmartdalton97f6cd52016-07-13 13:37:08 -0700709 SkRect croppedRect = rectToDraw;
710 SkRect croppedLocalRect = localRect;
robertphillips13a7eee2016-08-31 15:06:24 -0700711 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix,
712 &croppedRect, &croppedLocalRect)) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700713 return;
714 }
715
Robert Phillips72152832017-01-25 17:31:35 -0500716 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -0700717
Brian Salomon7c8460e2017-05-12 11:36:10 -0400718 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500719 if (GrAAType::kCoverage != aaType) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400720 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeNonAAFillWithLocalRect(
Robert Phillips7c525e62018-06-12 10:11:12 -0400721 fContext, std::move(paint), viewMatrix, croppedRect, croppedLocalRect, aaType);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400722 this->addDrawOp(clip, std::move(op));
csmartdaltonfc49d562016-07-26 17:05:47 -0700723 return;
joshualitt04194f32016-01-13 10:08:27 -0800724 }
bsalomonbb243832016-07-22 07:10:19 -0700725
Brian Salomonbaaf4392017-06-15 09:59:23 -0400726 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeAAFillWithLocalRect(
Robert Phillips7c525e62018-06-12 10:11:12 -0400727 fContext, std::move(paint), viewMatrix, croppedRect, croppedLocalRect);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400728 if (op) {
729 this->addDrawOp(clip, std::move(op));
csmartdaltonfc49d562016-07-26 17:05:47 -0700730 return;
731 }
732
733 SkMatrix viewAndUnLocalMatrix;
734 if (!viewAndUnLocalMatrix.setRectToRect(localRect, rectToDraw, SkMatrix::kFill_ScaleToFit)) {
735 SkDebugf("fillRectToRect called with empty local matrix.\n");
736 return;
737 }
738 viewAndUnLocalMatrix.postConcat(viewMatrix);
739
Brian Salomon2fad74a2017-12-20 13:28:55 -0500740 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewAndUnLocalMatrix,
741 GrShape(localRect));
joshualittb6b513b2015-08-21 10:25:18 -0700742}
743
Brian Salomon34169692017-08-28 15:32:01 -0400744static bool must_filter(const SkRect& src, const SkRect& dst, const SkMatrix& ctm) {
745 // We don't currently look for 90 degree rotations, mirroring, or downscales that sample at
746 // texel centers.
747 if (!ctm.isTranslate()) {
748 return true;
749 }
750 if (src.width() != dst.width() || src.height() != dst.height()) {
751 return true;
752 }
753 // Check that the device space rectangle's fractional offset is the same as the src rectangle,
754 // and that therefore integers in the src image fall on integers in device space.
755 SkScalar x = ctm.getTranslateX(), y = ctm.getTranslateY();
756 x += dst.fLeft; y += dst.fTop;
757 x -= src.fLeft; y -= src.fTop;
758 return !SkScalarIsInt(x) || !SkScalarIsInt(y);
759}
760
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400761void GrRenderTargetContext::drawTexture(const GrClip& clip, sk_sp<GrTextureProxy> proxy,
762 GrSamplerState::Filter filter, GrColor color,
763 const SkRect& srcRect, const SkRect& dstRect, GrAA aa,
Brian Salomonb80ffee2018-05-23 16:39:39 -0400764 SkCanvas::SrcRectConstraint constraint,
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400765 const SkMatrix& viewMatrix,
766 sk_sp<GrColorSpaceXform> colorSpaceXform) {
Brian Salomon34169692017-08-28 15:32:01 -0400767 ASSERT_SINGLE_OWNER
768 RETURN_IF_ABANDONED
769 SkDEBUGCODE(this->validate();)
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400770 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTexture", fContext);
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400771 if (filter != GrSamplerState::Filter::kNearest && !must_filter(srcRect, dstRect, viewMatrix)) {
772 filter = GrSamplerState::Filter::kNearest;
Brian Salomon34169692017-08-28 15:32:01 -0400773 }
Brian Salomond8eb7b62018-05-25 10:08:05 -0400774 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
775 if (constraint == SkCanvas::kStrict_SrcRectConstraint) {
776 // No need to use a texture domain with nearest filtering unless there is AA bloating.
777 // Also, no need if the srcRect contains the entire texture.
778 if (filter == GrSamplerState::Filter::kNearest && aaType != GrAAType::kCoverage) {
779 constraint = SkCanvas::kFast_SrcRectConstraint;
780 } else if (srcRect.contains(proxy->getWorstCaseBoundsRect())) {
781 constraint = SkCanvas::kFast_SrcRectConstraint;
782 }
783 }
Brian Salomonff9d6d32017-08-30 10:27:49 -0400784 SkRect clippedDstRect = dstRect;
785 SkRect clippedSrcRect = srcRect;
786 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix, &clippedDstRect,
787 &clippedSrcRect)) {
788 return;
789 }
Robert Phillips7c525e62018-06-12 10:11:12 -0400790 this->addDrawOp(clip, GrTextureOp::Make(fContext, std::move(proxy), filter, color,
791 clippedSrcRect, clippedDstRect, aaType, constraint,
792 viewMatrix, std::move(colorSpaceXform)));
Brian Salomon34169692017-08-28 15:32:01 -0400793}
794
Brian Osman11052242016-10-27 14:47:55 -0400795void GrRenderTargetContext::fillRectWithLocalMatrix(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500796 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500797 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400798 const SkMatrix& viewMatrix,
799 const SkRect& rectToDraw,
800 const SkMatrix& localMatrix) {
joshualitt1de610a2016-01-06 08:26:09 -0800801 ASSERT_SINGLE_OWNER
joshualittb6b513b2015-08-21 10:25:18 -0700802 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700803 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400804 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "fillRectWithLocalMatrix", fContext);
joshualittb6b513b2015-08-21 10:25:18 -0700805
csmartdalton97f6cd52016-07-13 13:37:08 -0700806 SkRect croppedRect = rectToDraw;
robertphillips13a7eee2016-08-31 15:06:24 -0700807 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix, &croppedRect)) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700808 return;
809 }
810
Robert Phillips72152832017-01-25 17:31:35 -0500811 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -0700812
Brian Salomon7c8460e2017-05-12 11:36:10 -0400813 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500814 if (GrAAType::kCoverage != aaType) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400815 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeNonAAFillWithLocalMatrix(
Robert Phillips7c525e62018-06-12 10:11:12 -0400816 fContext, std::move(paint), viewMatrix, localMatrix, croppedRect, aaType);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400817 this->addDrawOp(clip, std::move(op));
csmartdaltonfc49d562016-07-26 17:05:47 -0700818 return;
bsalomonc55271f2015-11-09 11:55:57 -0800819 }
robertphillips4bc31812016-03-01 12:22:49 -0800820
Brian Salomonbaaf4392017-06-15 09:59:23 -0400821 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeAAFillWithLocalMatrix(
Robert Phillips7c525e62018-06-12 10:11:12 -0400822 fContext, std::move(paint), viewMatrix, localMatrix, croppedRect);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400823 if (op) {
824 this->addDrawOp(clip, std::move(op));
csmartdaltonfc49d562016-07-26 17:05:47 -0700825 return;
826 }
827
828 SkMatrix viewAndUnLocalMatrix;
829 if (!localMatrix.invert(&viewAndUnLocalMatrix)) {
830 SkDebugf("fillRectWithLocalMatrix called with degenerate local matrix.\n");
831 return;
832 }
833 viewAndUnLocalMatrix.postConcat(viewMatrix);
834
835 SkPath path;
836 path.setIsVolatile(true);
837 path.addRect(rectToDraw);
838 path.transform(localMatrix);
Brian Salomon2fad74a2017-12-20 13:28:55 -0500839 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewAndUnLocalMatrix,
840 GrShape(path));
robertphillipsea461502015-05-26 11:38:03 -0700841}
842
Brian Osman11052242016-10-27 14:47:55 -0400843void GrRenderTargetContext::drawVertices(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500844 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400845 const SkMatrix& viewMatrix,
Brian Osmanae0c50c2017-05-25 16:56:34 -0400846 sk_sp<SkVertices> vertices,
847 GrPrimitiveType* overridePrimType) {
Brian Salomon199fb872017-02-06 09:41:10 -0500848 ASSERT_SINGLE_OWNER
849 RETURN_IF_ABANDONED
850 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400851 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawVertices", fContext);
Brian Salomon199fb872017-02-06 09:41:10 -0500852
853 AutoCheckFlush acf(this->drawingManager());
854
855 SkASSERT(vertices);
Brian Salomonc2f42542017-07-12 14:11:22 -0400856 GrAAType aaType = this->chooseAAType(GrAA::kNo, GrAllowMixedSamples::kNo);
Brian Salomonf3569f02017-10-24 12:52:33 -0400857 std::unique_ptr<GrDrawOp> op = GrDrawVerticesOp::Make(
Robert Phillips7c525e62018-06-12 10:11:12 -0400858 fContext, std::move(paint), std::move(vertices), viewMatrix, aaType,
Brian Salomonf3569f02017-10-24 12:52:33 -0400859 this->colorSpaceInfo().refColorSpaceXformFromSRGB(), overridePrimType);
Brian Salomonc2f42542017-07-12 14:11:22 -0400860 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -0700861}
862
863///////////////////////////////////////////////////////////////////////////////
864
Brian Osman11052242016-10-27 14:47:55 -0400865void GrRenderTargetContext::drawAtlas(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500866 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400867 const SkMatrix& viewMatrix,
868 int spriteCount,
869 const SkRSXform xform[],
870 const SkRect texRect[],
871 const SkColor colors[]) {
joshualitt1de610a2016-01-06 08:26:09 -0800872 ASSERT_SINGLE_OWNER
jvanverth31ff7622015-08-07 10:09:28 -0700873 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700874 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400875 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawAtlas", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700876
Robert Phillips72152832017-01-25 17:31:35 -0500877 AutoCheckFlush acf(this->drawingManager());
halcanary9d524f22016-03-29 09:03:52 -0700878
Brian Salomon0088f942017-07-12 11:51:27 -0400879 GrAAType aaType = this->chooseAAType(GrAA::kNo, GrAllowMixedSamples::kNo);
Robert Phillips7c525e62018-06-12 10:11:12 -0400880 std::unique_ptr<GrDrawOp> op = GrDrawAtlasOp::Make(fContext, std::move(paint), viewMatrix,
881 aaType, spriteCount, xform, texRect, colors);
Brian Salomon0088f942017-07-12 11:51:27 -0400882 this->addDrawOp(clip, std::move(op));
jvanverth31ff7622015-08-07 10:09:28 -0700883}
884
885///////////////////////////////////////////////////////////////////////////////
886
Brian Osman11052242016-10-27 14:47:55 -0400887void GrRenderTargetContext::drawRRect(const GrClip& origClip,
Brian Salomon82f44312017-01-11 13:42:54 -0500888 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500889 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400890 const SkMatrix& viewMatrix,
891 const SkRRect& rrect,
892 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -0800893 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700894 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700895 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400896 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRRect", fContext);
robertphillipsea461502015-05-26 11:38:03 -0700897 if (rrect.isEmpty()) {
898 return;
899 }
900
bsalomon7f0d9f32016-08-15 14:49:10 -0700901 GrNoClip noclip;
902 const GrClip* clip = &origClip;
903#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
904 // The Android framework frequently clips rrects to themselves where the clip is non-aa and the
Brian Salomon42521e82016-12-07 16:44:58 -0500905 // draw is aa. Since our lower level clip code works from op bounds, which are SkRects, it
bsalomon7f0d9f32016-08-15 14:49:10 -0700906 // doesn't detect that the clip can be ignored (modulo antialiasing). The following test
907 // attempts to mitigate the stencil clip cost but will only help when the entire clip stack
908 // can be ignored. We'd prefer to fix this in the framework by removing the clips calls.
909 SkRRect devRRect;
910 if (rrect.transform(viewMatrix, &devRRect) && clip->quickContains(devRRect)) {
911 clip = &noclip;
912 }
913#endif
bsalomon6663acf2016-05-10 09:14:17 -0700914 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
ksakamotoec7f2ac2016-07-05 03:54:53 -0700915
Robert Phillips72152832017-01-25 17:31:35 -0500916 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -0700917 const SkStrokeRec stroke = style.strokeRec();
csmartdaltona7f29642016-07-07 08:49:11 -0700918
Brian Salomonea26d6b2018-01-23 20:33:21 +0000919 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500920 if (GrAAType::kCoverage == aaType) {
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400921 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
Robert Phillips7c525e62018-06-12 10:11:12 -0400922 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeRRectOp(fContext,
923 std::move(paint),
Brian Salomonea26d6b2018-01-23 20:33:21 +0000924 viewMatrix,
925 rrect,
926 stroke,
927 shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -0500928 if (op) {
Brian Salomon05441c42017-05-15 16:45:49 -0400929 this->addDrawOp(*clip, std::move(op));
robertphillipsb56f9272016-02-25 11:03:52 -0800930 return;
931 }
robertphillipsea461502015-05-26 11:38:03 -0700932 }
robertphillipsb56f9272016-02-25 11:03:52 -0800933
Brian Salomon2fad74a2017-12-20 13:28:55 -0500934 this->drawShapeUsingPathRenderer(*clip, std::move(paint), aa, viewMatrix,
935 GrShape(rrect, style));
robertphillipsea461502015-05-26 11:38:03 -0700936}
937
Jim Van Verthc5903412016-11-17 15:27:09 -0500938///////////////////////////////////////////////////////////////////////////////
939
Jim Van Verth3af1af92017-05-18 15:06:54 -0400940static SkPoint3 map(const SkMatrix& m, const SkPoint3& pt) {
941 SkPoint3 result;
942 m.mapXY(pt.fX, pt.fY, (SkPoint*)&result.fX);
943 result.fZ = pt.fZ;
944 return result;
945}
946
947bool GrRenderTargetContext::drawFastShadow(const GrClip& clip,
Jim Van Verth3af1af92017-05-18 15:06:54 -0400948 const SkMatrix& viewMatrix,
949 const SkPath& path,
950 const SkDrawShadowRec& rec) {
Jim Van Verthc5903412016-11-17 15:27:09 -0500951 ASSERT_SINGLE_OWNER
Jim Van Verth3af1af92017-05-18 15:06:54 -0400952 if (this->drawingManager()->wasAbandoned()) {
953 return true;
954 }
Jim Van Verthc5903412016-11-17 15:27:09 -0500955 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400956 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFastShadow", fContext);
Jim Van Verth3af1af92017-05-18 15:06:54 -0400957
958 // check z plane
959 bool tiltZPlane = SkToBool(!SkScalarNearlyZero(rec.fZPlaneParams.fX) ||
960 !SkScalarNearlyZero(rec.fZPlaneParams.fY));
961 bool skipAnalytic = SkToBool(rec.fFlags & SkShadowFlags::kGeometricOnly_ShadowFlag);
962 if (tiltZPlane || skipAnalytic || !viewMatrix.rectStaysRect() || !viewMatrix.isSimilarity()) {
963 return false;
964 }
965
966 SkRRect rrect;
967 SkRect rect;
968 // we can only handle rects, circles, and rrects with circular corners
Mike Reed242135a2018-02-22 13:41:39 -0500969 bool isRRect = path.isRRect(&rrect) && SkRRectPriv::IsSimpleCircular(rrect) &&
Jim Van Verth3af1af92017-05-18 15:06:54 -0400970 rrect.radii(SkRRect::kUpperLeft_Corner).fX > SK_ScalarNearlyZero;
971 if (!isRRect &&
972 path.isOval(&rect) && SkScalarNearlyEqual(rect.width(), rect.height()) &&
973 rect.width() > SK_ScalarNearlyZero) {
974 rrect.setOval(rect);
975 isRRect = true;
976 }
977 if (!isRRect && path.isRect(&rect)) {
978 rrect.setRect(rect);
979 isRRect = true;
980 }
981
982 if (!isRRect) {
983 return false;
984 }
985
Jim Van Verthc5903412016-11-17 15:27:09 -0500986 if (rrect.isEmpty()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -0400987 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -0500988 }
989
Robert Phillips72152832017-01-25 17:31:35 -0500990 AutoCheckFlush acf(this->drawingManager());
Jim Van Verthc5903412016-11-17 15:27:09 -0500991
Jim Van Verth3af1af92017-05-18 15:06:54 -0400992 // transform light
993 SkPoint3 devLightPos = map(viewMatrix, rec.fLightPos);
994
995 // 1/scale
996 SkScalar devToSrcScale = viewMatrix.isScaleTranslate() ?
997 SkScalarInvert(viewMatrix[SkMatrix::kMScaleX]) :
998 sk_float_rsqrt(viewMatrix[SkMatrix::kMScaleX] * viewMatrix[SkMatrix::kMScaleX] +
999 viewMatrix[SkMatrix::kMSkewX] * viewMatrix[SkMatrix::kMSkewX]);
1000
1001 SkScalar occluderHeight = rec.fZPlaneParams.fZ;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001002 bool transparent = SkToBool(rec.fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag);
1003
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001004 if (SkColorGetA(rec.fAmbientColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001005 SkScalar devSpaceInsetWidth = SkDrawShadowMetrics::AmbientBlurRadius(occluderHeight);
1006 const SkScalar umbraRecipAlpha = SkDrawShadowMetrics::AmbientRecipAlpha(occluderHeight);
1007 const SkScalar devSpaceAmbientBlur = devSpaceInsetWidth * umbraRecipAlpha;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001008
1009 // Outset the shadow rrect to the border of the penumbra
1010 SkScalar ambientPathOutset = devSpaceInsetWidth * devToSrcScale;
1011 SkRRect ambientRRect;
1012 SkRect outsetRect = rrect.rect().makeOutset(ambientPathOutset, ambientPathOutset);
1013 // If the rrect was an oval then its outset will also be one.
1014 // We set it explicitly to avoid errors.
1015 if (rrect.isOval()) {
1016 ambientRRect = SkRRect::MakeOval(outsetRect);
1017 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001018 SkScalar outsetRad = SkRRectPriv::GetSimpleRadii(rrect).fX + ambientPathOutset;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001019 ambientRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1020 }
1021
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001022 GrColor ambientColor = SkColorToPremulGrColor(rec.fAmbientColor);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001023 if (transparent) {
1024 // set a large inset to force a fill
1025 devSpaceInsetWidth = ambientRRect.width();
1026 }
Jim Van Verth39e71652018-04-23 18:08:45 +00001027 // the fraction of the blur we want to apply is devSpaceInsetWidth/devSpaceAmbientBlur,
1028 // which is just 1/umbraRecipAlpha.
1029 SkScalar blurClamp = SkScalarInvert(umbraRecipAlpha);
1030
Robert Phillips7c525e62018-06-12 10:11:12 -04001031 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1032 ambientColor,
1033 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001034 ambientRRect,
1035 devSpaceAmbientBlur,
Jim Van Verth39e71652018-04-23 18:08:45 +00001036 devSpaceInsetWidth,
1037 blurClamp);
Brian Salomon05969092017-07-13 11:20:51 -04001038 SkASSERT(op);
1039 this->addDrawOp(clip, std::move(op));
Jim Van Verthc5903412016-11-17 15:27:09 -05001040 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001041
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001042 if (SkColorGetA(rec.fSpotColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001043 SkScalar devSpaceSpotBlur;
1044 SkScalar spotScale;
1045 SkVector spotOffset;
1046 SkDrawShadowMetrics::GetSpotParams(occluderHeight, devLightPos.fX, devLightPos.fY,
1047 devLightPos.fZ, rec.fLightRadius,
1048 &devSpaceSpotBlur, &spotScale, &spotOffset);
1049 // handle scale of radius due to CTM
Jim Van Verth3af1af92017-05-18 15:06:54 -04001050 const SkScalar srcSpaceSpotBlur = devSpaceSpotBlur * devToSrcScale;
1051
Jim Van Verth3af1af92017-05-18 15:06:54 -04001052 // Adjust translate for the effect of the scale.
1053 spotOffset.fX += spotScale*viewMatrix[SkMatrix::kMTransX];
1054 spotOffset.fY += spotScale*viewMatrix[SkMatrix::kMTransY];
1055 // This offset is in dev space, need to transform it into source space.
1056 SkMatrix ctmInverse;
1057 if (viewMatrix.invert(&ctmInverse)) {
1058 ctmInverse.mapPoints(&spotOffset, 1);
1059 } else {
1060 // Since the matrix is a similarity, this should never happen, but just in case...
1061 SkDebugf("Matrix is degenerate. Will not render spot shadow correctly!\n");
1062 SkASSERT(false);
1063 }
1064
1065 // Compute the transformed shadow rrect
1066 SkRRect spotShadowRRect;
1067 SkMatrix shadowTransform;
1068 shadowTransform.setScaleTranslate(spotScale, spotScale, spotOffset.fX, spotOffset.fY);
1069 rrect.transform(shadowTransform, &spotShadowRRect);
Mike Reed242135a2018-02-22 13:41:39 -05001070 SkScalar spotRadius = SkRRectPriv::GetSimpleRadii(spotShadowRRect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001071
1072 // Compute the insetWidth
Jim Van Verth1af03d42017-07-31 09:34:58 -04001073 SkScalar blurOutset = srcSpaceSpotBlur;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001074 SkScalar insetWidth = blurOutset;
1075 if (transparent) {
1076 // If transparent, just do a fill
1077 insetWidth += spotShadowRRect.width();
1078 } else {
1079 // For shadows, instead of using a stroke we specify an inset from the penumbra
1080 // border. We want to extend this inset area so that it meets up with the caster
1081 // geometry. The inset geometry will by default already be inset by the blur width.
1082 //
1083 // We compare the min and max corners inset by the radius between the original
1084 // rrect and the shadow rrect. The distance between the two plus the difference
1085 // between the scaled radius and the original radius gives the distance from the
1086 // transformed shadow shape to the original shape in that corner. The max
1087 // of these gives the maximum distance we need to cover.
1088 //
1089 // Since we are outsetting by 1/2 the blur distance, we just add the maxOffset to
1090 // that to get the full insetWidth.
1091 SkScalar maxOffset;
1092 if (rrect.isRect()) {
1093 // Manhattan distance works better for rects
1094 maxOffset = SkTMax(SkTMax(SkTAbs(spotShadowRRect.rect().fLeft -
1095 rrect.rect().fLeft),
1096 SkTAbs(spotShadowRRect.rect().fTop -
1097 rrect.rect().fTop)),
1098 SkTMax(SkTAbs(spotShadowRRect.rect().fRight -
1099 rrect.rect().fRight),
1100 SkTAbs(spotShadowRRect.rect().fBottom -
1101 rrect.rect().fBottom)));
1102 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001103 SkScalar dr = spotRadius - SkRRectPriv::GetSimpleRadii(rrect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001104 SkPoint upperLeftOffset = SkPoint::Make(spotShadowRRect.rect().fLeft -
1105 rrect.rect().fLeft + dr,
1106 spotShadowRRect.rect().fTop -
1107 rrect.rect().fTop + dr);
1108 SkPoint lowerRightOffset = SkPoint::Make(spotShadowRRect.rect().fRight -
1109 rrect.rect().fRight - dr,
1110 spotShadowRRect.rect().fBottom -
1111 rrect.rect().fBottom - dr);
Cary Clarkdf429f32017-11-08 11:44:31 -05001112 maxOffset = SkScalarSqrt(SkTMax(SkPointPriv::LengthSqd(upperLeftOffset),
1113 SkPointPriv::LengthSqd(lowerRightOffset))) + dr;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001114 }
Jim Van Verth4c8c1e82018-04-23 17:14:48 -04001115 insetWidth += SkTMax(blurOutset, maxOffset);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001116 }
1117
1118 // Outset the shadow rrect to the border of the penumbra
1119 SkRect outsetRect = spotShadowRRect.rect().makeOutset(blurOutset, blurOutset);
1120 if (spotShadowRRect.isOval()) {
1121 spotShadowRRect = SkRRect::MakeOval(outsetRect);
1122 } else {
1123 SkScalar outsetRad = spotRadius + blurOutset;
1124 spotShadowRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1125 }
1126
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001127 GrColor spotColor = SkColorToPremulGrColor(rec.fSpotColor);
Jim Van Verth34d6e4b2017-06-09 11:09:03 -04001128
Robert Phillips7c525e62018-06-12 10:11:12 -04001129 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1130 spotColor,
1131 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001132 spotShadowRRect,
Jim Van Verth1af03d42017-07-31 09:34:58 -04001133 2.0f * devSpaceSpotBlur,
Brian Salomon05969092017-07-13 11:20:51 -04001134 insetWidth);
1135 SkASSERT(op);
1136 this->addDrawOp(clip, std::move(op));
Jim Van Verth3af1af92017-05-18 15:06:54 -04001137 }
1138
1139 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001140}
1141
1142///////////////////////////////////////////////////////////////////////////////
1143
Brian Osman11052242016-10-27 14:47:55 -04001144bool GrRenderTargetContext::drawFilledDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001145 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001146 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001147 const SkMatrix& viewMatrix,
1148 const SkRRect& origOuter,
1149 const SkRRect& origInner) {
robertphillips00095892016-02-29 13:50:40 -08001150 SkASSERT(!origInner.isEmpty());
1151 SkASSERT(!origOuter.isEmpty());
1152
Brian Salomon65749212017-12-01 16:01:47 -05001153 SkTCopyOnFirstWrite<SkRRect> inner(origInner), outer(origOuter);
1154
Brian Salomon45839f92017-12-04 09:02:35 -05001155 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
1156
1157 if (GrAAType::kMSAA == aaType) {
1158 return false;
1159 }
1160
Mike Reed242135a2018-02-22 13:41:39 -05001161 if (GrAAType::kCoverage == aaType && SkRRectPriv::IsCircle(*inner)
1162 && SkRRectPriv::IsCircle(*outer)) {
Brian Salomon65749212017-12-01 16:01:47 -05001163 auto outerR = outer->width() / 2.f;
1164 auto innerR = inner->width() / 2.f;
1165 auto cx = outer->getBounds().fLeft + outerR;
1166 auto cy = outer->getBounds().fTop + outerR;
1167 if (SkScalarNearlyEqual(cx, inner->getBounds().fLeft + innerR) &&
1168 SkScalarNearlyEqual(cy, inner->getBounds().fTop + innerR)) {
1169 auto avgR = (innerR + outerR) / 2.f;
1170 auto circleBounds = SkRect::MakeLTRB(cx - avgR, cy - avgR, cx + avgR, cy + avgR);
1171 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
1172 stroke.setStrokeStyle(outerR - innerR);
Robert Phillips7c525e62018-06-12 10:11:12 -04001173 auto op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix,
1174 circleBounds, GrStyle(stroke, nullptr),
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001175 this->caps()->shaderCaps());
Brian Salomon65749212017-12-01 16:01:47 -05001176 if (op) {
1177 this->addDrawOp(clip, std::move(op));
1178 return true;
1179 }
1180 }
1181 }
1182
Ethan Nicholas0f3c7322017-11-09 14:51:17 -05001183 GrClipEdgeType innerEdgeType, outerEdgeType;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001184 if (GrAAType::kCoverage == aaType) {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001185 innerEdgeType = GrClipEdgeType::kInverseFillAA;
1186 outerEdgeType = GrClipEdgeType::kFillAA;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001187 } else {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001188 innerEdgeType = GrClipEdgeType::kInverseFillBW;
1189 outerEdgeType = GrClipEdgeType::kFillBW;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001190 }
robertphillips00095892016-02-29 13:50:40 -08001191
robertphillips00095892016-02-29 13:50:40 -08001192 SkMatrix inverseVM;
1193 if (!viewMatrix.isIdentity()) {
1194 if (!origInner.transform(viewMatrix, inner.writable())) {
1195 return false;
1196 }
1197 if (!origOuter.transform(viewMatrix, outer.writable())) {
1198 return false;
1199 }
1200 if (!viewMatrix.invert(&inverseVM)) {
1201 return false;
1202 }
1203 } else {
1204 inverseVM.reset();
halcanary9d524f22016-03-29 09:03:52 -07001205 }
robertphillips00095892016-02-29 13:50:40 -08001206
Brian Salomon14471772017-12-05 10:35:15 -05001207 const auto& caps = *this->caps()->shaderCaps();
robertphillips00095892016-02-29 13:50:40 -08001208 // TODO these need to be a geometry processors
Brian Salomon14471772017-12-05 10:35:15 -05001209 auto innerEffect = GrRRectEffect::Make(innerEdgeType, *inner, caps);
robertphillips00095892016-02-29 13:50:40 -08001210 if (!innerEffect) {
1211 return false;
1212 }
1213
Brian Salomon14471772017-12-05 10:35:15 -05001214 auto outerEffect = GrRRectEffect::Make(outerEdgeType, *outer, caps);
robertphillips00095892016-02-29 13:50:40 -08001215 if (!outerEffect) {
1216 return false;
1217 }
1218
Brian Salomon82f44312017-01-11 13:42:54 -05001219 paint.addCoverageFragmentProcessor(std::move(innerEffect));
1220 paint.addCoverageFragmentProcessor(std::move(outerEffect));
robertphillips00095892016-02-29 13:50:40 -08001221
1222 SkRect bounds = outer->getBounds();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001223 if (GrAAType::kCoverage == aaType) {
robertphillips00095892016-02-29 13:50:40 -08001224 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1225 }
halcanary9d524f22016-03-29 09:03:52 -07001226
Brian Salomon82f44312017-01-11 13:42:54 -05001227 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), bounds,
1228 inverseVM);
robertphillips00095892016-02-29 13:50:40 -08001229 return true;
1230}
1231
Brian Osman11052242016-10-27 14:47:55 -04001232void GrRenderTargetContext::drawDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001233 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001234 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001235 const SkMatrix& viewMatrix,
1236 const SkRRect& outer,
1237 const SkRRect& inner) {
robertphillips00095892016-02-29 13:50:40 -08001238 ASSERT_SINGLE_OWNER
1239 RETURN_IF_ABANDONED
1240 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001241 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawDRRect", fContext);
robertphillips00095892016-02-29 13:50:40 -08001242
1243 SkASSERT(!outer.isEmpty());
1244 SkASSERT(!inner.isEmpty());
1245
Robert Phillips72152832017-01-25 17:31:35 -05001246 AutoCheckFlush acf(this->drawingManager());
robertphillips00095892016-02-29 13:50:40 -08001247
Brian Salomon82f44312017-01-11 13:42:54 -05001248 if (this->drawFilledDRRect(clip, std::move(paint), aa, viewMatrix, outer, inner)) {
robertphillips00095892016-02-29 13:50:40 -08001249 return;
1250 }
1251
1252 SkPath path;
1253 path.setIsVolatile(true);
1254 path.addRRect(inner);
1255 path.addRRect(outer);
1256 path.setFillType(SkPath::kEvenOdd_FillType);
Brian Salomon2fad74a2017-12-20 13:28:55 -05001257 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, GrShape(path));
robertphillips00095892016-02-29 13:50:40 -08001258}
1259
robertphillipsea461502015-05-26 11:38:03 -07001260///////////////////////////////////////////////////////////////////////////////
1261
Brian Osman11052242016-10-27 14:47:55 -04001262void GrRenderTargetContext::drawRegion(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001263 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001264 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001265 const SkMatrix& viewMatrix,
1266 const SkRegion& region,
Stan Iliev73d8fd92017-08-02 15:36:24 -04001267 const GrStyle& style,
1268 const GrUserStencilSettings* ss) {
msarettcc319b92016-08-25 18:07:18 -07001269 ASSERT_SINGLE_OWNER
1270 RETURN_IF_ABANDONED
1271 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001272 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRegion", fContext);
msarettcc319b92016-08-25 18:07:18 -07001273
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001274 if (GrAA::kYes == aa) {
Brian Salomonfc527d22016-12-14 21:07:01 -05001275 // GrRegionOp performs no antialiasing but is much faster, so here we check the matrix
Brian Salomonc57c7c92016-12-06 14:47:34 -05001276 // to see whether aa is really required.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001277 if (!SkToBool(viewMatrix.getType() & ~(SkMatrix::kTranslate_Mask)) &&
Brian Salomon34169692017-08-28 15:32:01 -04001278 SkScalarIsInt(viewMatrix.getTranslateX()) &&
1279 SkScalarIsInt(viewMatrix.getTranslateY())) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001280 aa = GrAA::kNo;
1281 }
Brian Salomonc57c7c92016-12-06 14:47:34 -05001282 }
msarettcc319b92016-08-25 18:07:18 -07001283 bool complexStyle = !style.isSimpleFill();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001284 if (complexStyle || GrAA::kYes == aa) {
msarettcc319b92016-08-25 18:07:18 -07001285 SkPath path;
1286 region.getBoundaryPath(&path);
Brian Salomon82f44312017-01-11 13:42:54 -05001287 return this->drawPath(clip, std::move(paint), aa, viewMatrix, path, style);
msarettcc319b92016-08-25 18:07:18 -07001288 }
1289
Brian Salomonf0366322017-07-11 15:53:05 -04001290 GrAAType aaType = this->chooseAAType(GrAA::kNo, GrAllowMixedSamples::kNo);
Robert Phillips7c525e62018-06-12 10:11:12 -04001291 std::unique_ptr<GrDrawOp> op = GrRegionOp::Make(fContext, std::move(paint), viewMatrix, region,
1292 aaType, ss);
Brian Salomonf0366322017-07-11 15:53:05 -04001293 this->addDrawOp(clip, std::move(op));
msarettcc319b92016-08-25 18:07:18 -07001294}
1295
Brian Osman11052242016-10-27 14:47:55 -04001296void GrRenderTargetContext::drawOval(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001297 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001298 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001299 const SkMatrix& viewMatrix,
1300 const SkRect& oval,
1301 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001302 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001303 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001304 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001305 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawOval", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -07001306
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001307 if (oval.isEmpty() && !style.pathEffect()) {
1308 return;
robertphillipsea461502015-05-26 11:38:03 -07001309 }
1310
Robert Phillips72152832017-01-25 17:31:35 -05001311 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001312
Brian Salomonea26d6b2018-01-23 20:33:21 +00001313 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001314 if (GrAAType::kCoverage == aaType) {
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001315 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
Robert Phillips7c525e62018-06-12 10:11:12 -04001316 if (auto op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix, oval,
1317 style, shaderCaps)) {
Brian Salomon05441c42017-05-15 16:45:49 -04001318 this->addDrawOp(clip, std::move(op));
robertphillipsb56f9272016-02-25 11:03:52 -08001319 return;
1320 }
robertphillipsea461502015-05-26 11:38:03 -07001321 }
robertphillipsb56f9272016-02-25 11:03:52 -08001322
Brian Salomon5209d7f2018-04-20 16:52:42 -04001323 this->drawShapeUsingPathRenderer(
1324 clip, std::move(paint), aa, viewMatrix,
1325 GrShape(SkRRect::MakeOval(oval), SkPath::kCW_Direction, 2, false, style));
robertphillipsea461502015-05-26 11:38:03 -07001326}
1327
Brian Osman11052242016-10-27 14:47:55 -04001328void GrRenderTargetContext::drawArc(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001329 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001330 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001331 const SkMatrix& viewMatrix,
1332 const SkRect& oval,
1333 SkScalar startAngle,
1334 SkScalar sweepAngle,
1335 bool useCenter,
1336 const GrStyle& style) {
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001337 ASSERT_SINGLE_OWNER
1338 RETURN_IF_ABANDONED
1339 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001340 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawArc", fContext);
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001341
1342 AutoCheckFlush acf(this->drawingManager());
1343
Brian Salomonea26d6b2018-01-23 20:33:21 +00001344 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001345 if (GrAAType::kCoverage == aaType) {
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001346 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
Robert Phillips7c525e62018-06-12 10:11:12 -04001347 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeArcOp(fContext,
1348 std::move(paint),
Brian Salomonea26d6b2018-01-23 20:33:21 +00001349 viewMatrix,
1350 oval,
1351 startAngle,
1352 sweepAngle,
1353 useCenter,
1354 style,
1355 shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -05001356 if (op) {
Brian Salomon05441c42017-05-15 16:45:49 -04001357 this->addDrawOp(clip, std::move(op));
bsalomon4f3a0ca2016-08-22 13:14:26 -07001358 return;
1359 }
1360 }
Brian Salomone4949402018-04-26 15:22:04 -04001361 this->drawShapeUsingPathRenderer(
1362 clip, std::move(paint), aa, viewMatrix,
1363 GrShape::MakeArc(oval, startAngle, sweepAngle, useCenter, style));
bsalomon4f3a0ca2016-08-22 13:14:26 -07001364}
1365
Brian Osman11052242016-10-27 14:47:55 -04001366void GrRenderTargetContext::drawImageLattice(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001367 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001368 const SkMatrix& viewMatrix,
Brian Salomon2a943df2018-05-04 13:43:19 -04001369 sk_sp<GrTextureProxy> image,
1370 sk_sp<GrColorSpaceXform> csxf,
1371 GrSamplerState::Filter filter,
Brian Osman11052242016-10-27 14:47:55 -04001372 std::unique_ptr<SkLatticeIter> iter,
1373 const SkRect& dst) {
joshualitt1de610a2016-01-06 08:26:09 -08001374 ASSERT_SINGLE_OWNER
joshualitt33a5fce2015-11-18 13:28:51 -08001375 RETURN_IF_ABANDONED
1376 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001377 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawImageLattice", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001378
Robert Phillips72152832017-01-25 17:31:35 -05001379 AutoCheckFlush acf(this->drawingManager());
joshualitt33a5fce2015-11-18 13:28:51 -08001380
Brian Salomon2a943df2018-05-04 13:43:19 -04001381 std::unique_ptr<GrDrawOp> op =
Robert Phillips7c525e62018-06-12 10:11:12 -04001382 GrLatticeOp::MakeNonAA(fContext, std::move(paint), viewMatrix, std::move(image),
1383 std::move(csxf), filter, std::move(iter), dst);
Brian Salomon815486c2017-07-11 08:52:13 -04001384 this->addDrawOp(clip, std::move(op));
joshualitt33a5fce2015-11-18 13:28:51 -08001385}
1386
Greg Daniel51316782017-08-02 15:10:09 +00001387GrSemaphoresSubmitted GrRenderTargetContext::prepareForExternalIO(
1388 int numSemaphores, GrBackendSemaphore backendSemaphores[]) {
robertphillips8c523e02016-07-26 07:41:00 -07001389 ASSERT_SINGLE_OWNER
Greg Daniel51316782017-08-02 15:10:09 +00001390 if (this->drawingManager()->wasAbandoned()) { return GrSemaphoresSubmitted::kNo; }
robertphillips8c523e02016-07-26 07:41:00 -07001391 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001392 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "prepareForExternalIO", fContext);
robertphillips8c523e02016-07-26 07:41:00 -07001393
Greg Daniel51316782017-08-02 15:10:09 +00001394 return this->drawingManager()->prepareSurfaceForExternalIO(fRenderTargetProxy.get(),
1395 numSemaphores,
1396 backendSemaphores);
Greg Daniela5cb7812017-06-16 09:45:32 -04001397}
1398
Greg Danielc64ee462017-06-15 16:59:49 -04001399bool GrRenderTargetContext::waitOnSemaphores(int numSemaphores,
Greg Daniela5cb7812017-06-16 09:45:32 -04001400 const GrBackendSemaphore* waitSemaphores) {
1401 ASSERT_SINGLE_OWNER
Greg Danielc64ee462017-06-15 16:59:49 -04001402 RETURN_FALSE_IF_ABANDONED
Greg Daniela5cb7812017-06-16 09:45:32 -04001403 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001404 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "waitOnSemaphores", fContext);
Greg Daniela5cb7812017-06-16 09:45:32 -04001405
1406 AutoCheckFlush acf(this->drawingManager());
1407
Greg Danielc64ee462017-06-15 16:59:49 -04001408 if (numSemaphores && !this->caps()->fenceSyncSupport()) {
1409 return false;
1410 }
1411
Robert Phillips6be756b2018-01-16 15:07:54 -05001412 auto resourceProvider = fContext->contextPriv().resourceProvider();
1413
Greg Daniela5cb7812017-06-16 09:45:32 -04001414 SkTArray<sk_sp<GrSemaphore>> semaphores(numSemaphores);
1415 for (int i = 0; i < numSemaphores; ++i) {
Robert Phillips6be756b2018-01-16 15:07:54 -05001416 sk_sp<GrSemaphore> sema = resourceProvider->wrapBackendSemaphore(
Greg Daniel17b7c052018-01-09 13:55:33 -05001417 waitSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillWait,
1418 kAdopt_GrWrapOwnership);
Robert Phillips7c525e62018-06-12 10:11:12 -04001419 std::unique_ptr<GrOp> waitOp(GrSemaphoreOp::MakeWait(fContext, sema,
1420 fRenderTargetProxy.get()));
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001421 this->getRTOpList()->addOp(std::move(waitOp), *this->caps());
Greg Daniela5cb7812017-06-16 09:45:32 -04001422 }
Greg Danielc64ee462017-06-15 16:59:49 -04001423 return true;
robertphillips8c523e02016-07-26 07:41:00 -07001424}
joshualitt33a5fce2015-11-18 13:28:51 -08001425
Robert Phillips65a88fa2017-08-08 08:36:22 -04001426void GrRenderTargetContext::insertEventMarker(const SkString& str) {
Robert Phillips88a32ef2018-06-07 11:05:56 -04001427 std::unique_ptr<GrOp> op(GrDebugMarkerOp::Make(fContext, fRenderTargetProxy.get(), str));
Robert Phillips65a88fa2017-08-08 08:36:22 -04001428 this->getRTOpList()->addOp(std::move(op), *this->caps());
1429}
1430
1431
robertphillipsea461502015-05-26 11:38:03 -07001432// Can 'path' be drawn as a pair of filled nested rectangles?
bsalomon6663acf2016-05-10 09:14:17 -07001433static bool fills_as_nested_rects(const SkMatrix& viewMatrix, const SkPath& path, SkRect rects[2]) {
robertphillipsea461502015-05-26 11:38:03 -07001434
1435 if (path.isInverseFillType()) {
1436 return false;
1437 }
1438
1439 // TODO: this restriction could be lifted if we were willing to apply
1440 // the matrix to all the points individually rather than just to the rect
robertphillips0e7029e2015-11-30 05:45:06 -08001441 if (!viewMatrix.rectStaysRect()) {
robertphillipsea461502015-05-26 11:38:03 -07001442 return false;
1443 }
1444
1445 SkPath::Direction dirs[2];
1446 if (!path.isNestedFillRects(rects, dirs)) {
1447 return false;
1448 }
1449
1450 if (SkPath::kWinding_FillType == path.getFillType() && dirs[0] == dirs[1]) {
1451 // The two rects need to be wound opposite to each other
1452 return false;
1453 }
1454
1455 // Right now, nested rects where the margin is not the same width
1456 // all around do not render correctly
1457 const SkScalar* outer = rects[0].asScalars();
1458 const SkScalar* inner = rects[1].asScalars();
1459
1460 bool allEq = true;
1461
1462 SkScalar margin = SkScalarAbs(outer[0] - inner[0]);
1463 bool allGoE1 = margin >= SK_Scalar1;
1464
1465 for (int i = 1; i < 4; ++i) {
1466 SkScalar temp = SkScalarAbs(outer[i] - inner[i]);
1467 if (temp < SK_Scalar1) {
1468 allGoE1 = false;
1469 }
1470 if (!SkScalarNearlyEqual(margin, temp)) {
1471 allEq = false;
1472 }
1473 }
1474
1475 return allEq || allGoE1;
1476}
1477
Brian Osman11052242016-10-27 14:47:55 -04001478void GrRenderTargetContext::drawPath(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001479 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001480 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001481 const SkMatrix& viewMatrix,
Brian Salomon40b77a62017-12-22 11:25:52 -05001482 const SkPath& path,
Brian Osman11052242016-10-27 14:47:55 -04001483 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001484 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001485 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001486 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001487 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawPath", fContext);
Brian Salomon40b77a62017-12-22 11:25:52 -05001488
1489 GrShape shape(path, style);
Brian Salomon2fad74a2017-12-20 13:28:55 -05001490 if (shape.isEmpty()) {
1491 if (shape.inverseFilled()) {
1492 this->drawPaint(clip, std::move(paint), viewMatrix);
1493 }
1494 return;
robertphillipsea461502015-05-26 11:38:03 -07001495 }
1496
Robert Phillips72152832017-01-25 17:31:35 -05001497 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -07001498
Brian Salomon2fad74a2017-12-20 13:28:55 -05001499 if (!shape.style().hasPathEffect()) {
1500 SkRRect rrect;
1501 // We can ignore the starting point and direction since there is no path effect.
1502 bool inverted;
1503 if (shape.asRRect(&rrect, nullptr, nullptr, &inverted) && !inverted) {
1504 if (rrect.isRect()) {
1505 this->drawRect(clip, std::move(paint), aa, viewMatrix, rrect.rect(),
1506 &shape.style());
1507 return;
1508 } else if (rrect.isOval()) {
1509 this->drawOval(clip, std::move(paint), aa, viewMatrix, rrect.rect(), shape.style());
robertphillipsea461502015-05-26 11:38:03 -07001510 return;
1511 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05001512 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect, shape.style());
1513 return;
robertphillipsea461502015-05-26 11:38:03 -07001514 }
1515 }
robertphillips4bc31812016-03-01 12:22:49 -08001516
Brian Salomon40b77a62017-12-22 11:25:52 -05001517 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
1518 if (GrAAType::kCoverage == aaType) {
1519 // TODO: Make GrShape check for nested rects.
1520 SkRect rects[2];
1521 if (shape.style().isSimpleFill() && fills_as_nested_rects(viewMatrix, path, rects)) {
1522 // Concave AA paths are expensive - try to avoid them for special cases
1523 SkRect rects[2];
1524
1525 if (fills_as_nested_rects(viewMatrix, path, rects)) {
Robert Phillips7c525e62018-06-12 10:11:12 -04001526 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeAAFillNestedRects(
1527 fContext, std::move(paint), viewMatrix, rects);
Brian Salomon40b77a62017-12-22 11:25:52 -05001528 if (op) {
1529 this->addDrawOp(clip, std::move(op));
1530 }
1531 // A null return indicates that there is nothing to draw in this case.
1532 return;
1533 }
1534 }
1535 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05001536 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, shape);
robertphillipsea461502015-05-26 11:38:03 -07001537}
1538
Chris Daltonbbfd5162017-11-07 13:35:22 -07001539bool GrRenderTargetContextPriv::drawAndStencilPath(const GrHardClip& clip,
Brian Osman11052242016-10-27 14:47:55 -04001540 const GrUserStencilSettings* ss,
1541 SkRegion::Op op,
1542 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001543 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001544 const SkMatrix& viewMatrix,
1545 const SkPath& path) {
robertphillips391395d2016-03-02 09:26:36 -08001546 ASSERT_SINGLE_OWNER_PRIV
1547 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -04001548 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001549 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawAndStencilPath",
1550 fRenderTargetContext->fContext);
robertphillips391395d2016-03-02 09:26:36 -08001551
1552 if (path.isEmpty() && path.isInverseFillType()) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001553 this->drawAndStencilRect(clip, ss, op, invert, GrAA::kNo, SkMatrix::I(),
Brian Osman11052242016-10-27 14:47:55 -04001554 SkRect::MakeIWH(fRenderTargetContext->width(),
1555 fRenderTargetContext->height()));
robertphillips391395d2016-03-02 09:26:36 -08001556 return true;
1557 }
1558
Robert Phillips72152832017-01-25 17:31:35 -05001559 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -08001560
1561 // An Assumption here is that path renderer would use some form of tweaking
1562 // the src color (either the input alpha or in the frag shader) to implement
1563 // aa. If we have some future driver-mojo path AA that can do the right
1564 // thing WRT to the blend then we'll need some query on the PR.
Brian Salomon7c8460e2017-05-12 11:36:10 -04001565 GrAAType aaType = fRenderTargetContext->chooseAAType(aa, GrAllowMixedSamples::kNo);
robertphillips976f5f02016-06-03 10:59:20 -07001566 bool hasUserStencilSettings = !ss->isUnused();
robertphillips391395d2016-03-02 09:26:36 -08001567
Chris Daltondb91c6e2017-09-08 16:25:08 -06001568 SkIRect clipConservativeBounds;
1569 clip.getConservativeBounds(fRenderTargetContext->width(), fRenderTargetContext->height(),
1570 &clipConservativeBounds, nullptr);
1571
bsalomon8acedde2016-06-24 10:42:16 -07001572 GrShape shape(path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -08001573 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001574 canDrawArgs.fCaps = fRenderTargetContext->caps();
robertphillips391395d2016-03-02 09:26:36 -08001575 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07001576 canDrawArgs.fShape = &shape;
Chris Daltondb91c6e2017-09-08 16:25:08 -06001577 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001578 canDrawArgs.fAAType = aaType;
cdalton93a379b2016-05-11 13:58:08 -07001579 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips391395d2016-03-02 09:26:36 -08001580
1581 // Don't allow the SW renderer
Robert Phillips72152832017-01-25 17:31:35 -05001582 GrPathRenderer* pr = fRenderTargetContext->drawingManager()->getPathRenderer(
Brian Salomon36aa1762016-12-10 13:24:02 -05001583 canDrawArgs, false, GrPathRendererChain::DrawType::kStencilAndColor);
robertphillips391395d2016-03-02 09:26:36 -08001584 if (!pr) {
1585 return false;
1586 }
1587
1588 GrPaint paint;
1589 paint.setCoverageSetOpXPFactory(op, invert);
1590
Brian Salomonf3569f02017-10-24 12:52:33 -04001591 GrPathRenderer::DrawPathArgs args{fRenderTargetContext->drawingManager()->getContext(),
1592 std::move(paint),
1593 ss,
1594 fRenderTargetContext,
1595 &clip,
1596 &clipConservativeBounds,
1597 &viewMatrix,
1598 &shape,
1599 aaType,
1600 fRenderTargetContext->colorSpaceInfo().isGammaCorrect()};
robertphillips391395d2016-03-02 09:26:36 -08001601 pr->drawPath(args);
1602 return true;
1603}
1604
Brian Osman11052242016-10-27 14:47:55 -04001605SkBudgeted GrRenderTargetContextPriv::isBudgeted() const {
robertphillips714712b2016-08-04 06:20:45 -07001606 ASSERT_SINGLE_OWNER_PRIV
1607
Brian Osman11052242016-10-27 14:47:55 -04001608 if (fRenderTargetContext->wasAbandoned()) {
robertphillips714712b2016-08-04 06:20:45 -07001609 return SkBudgeted::kNo;
1610 }
1611
Brian Osman11052242016-10-27 14:47:55 -04001612 SkDEBUGCODE(fRenderTargetContext->validate();)
robertphillips714712b2016-08-04 06:20:45 -07001613
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001614 return fRenderTargetContext->fRenderTargetProxy->isBudgeted();
robertphillips714712b2016-08-04 06:20:45 -07001615}
1616
Brian Salomon2fad74a2017-12-20 13:28:55 -05001617void GrRenderTargetContext::drawShapeUsingPathRenderer(const GrClip& clip,
1618 GrPaint&& paint,
1619 GrAA aa,
1620 const SkMatrix& viewMatrix,
1621 const GrShape& originalShape) {
joshualitt1de610a2016-01-06 08:26:09 -08001622 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001623 RETURN_IF_ABANDONED
Brian Salomondcbb9d92017-07-19 10:53:20 -04001624 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "internalDrawPath", fContext);
1625
Chris Daltondb91c6e2017-09-08 16:25:08 -06001626 SkIRect clipConservativeBounds;
1627 clip.getConservativeBounds(this->width(), this->height(), &clipConservativeBounds, nullptr);
1628
Brian Salomon2fad74a2017-12-20 13:28:55 -05001629 GrShape tempShape;
Brian Salomon7c8460e2017-05-12 11:36:10 -04001630 // NVPR cannot handle hairlines, so this would get picked up by a different stencil and
1631 // cover path renderer (i.e. default path renderer). The hairline renderer produces much
1632 // smoother hairlines than MSAA.
Brian Salomon2fad74a2017-12-20 13:28:55 -05001633 GrAllowMixedSamples allowMixedSamples = originalShape.style().isSimpleHairline()
1634 ? GrAllowMixedSamples::kNo
1635 : GrAllowMixedSamples::kYes;
Brian Salomon7c8460e2017-05-12 11:36:10 -04001636 GrAAType aaType = this->chooseAAType(aa, allowMixedSamples);
robertphillips68737822015-10-29 12:12:21 -07001637 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001638 canDrawArgs.fCaps = this->caps();
robertphillips68737822015-10-29 12:12:21 -07001639 canDrawArgs.fViewMatrix = &viewMatrix;
Brian Salomon2fad74a2017-12-20 13:28:55 -05001640 canDrawArgs.fShape = &originalShape;
Chris Daltondb91c6e2017-09-08 16:25:08 -06001641 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001642 canDrawArgs.fHasUserStencilSettings = false;
robertphillips68737822015-10-29 12:12:21 -07001643
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001644 GrPathRenderer* pr;
Brian Salomon82125e92016-12-10 09:35:48 -05001645 static constexpr GrPathRendererChain::DrawType kType = GrPathRendererChain::DrawType::kColor;
Brian Salomon2fad74a2017-12-20 13:28:55 -05001646 if (originalShape.isEmpty() && !originalShape.inverseFilled()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05001647 return;
1648 }
1649
1650 canDrawArgs.fAAType = aaType;
1651
1652 // Try a 1st time without applying any of the style to the geometry (and barring sw)
1653 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
1654 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
1655
Brian Salomon2fad74a2017-12-20 13:28:55 -05001656 if (!pr && originalShape.style().pathEffect()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05001657 // It didn't work above, so try again with the path effect applied.
Brian Salomon2fad74a2017-12-20 13:28:55 -05001658 tempShape = originalShape.applyStyle(GrStyle::Apply::kPathEffectOnly, styleScale);
1659 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001660 return;
1661 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05001662 canDrawArgs.fShape = &tempShape;
Robert Phillips72152832017-01-25 17:31:35 -05001663 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05001664 }
1665 if (!pr) {
Brian Salomon2fad74a2017-12-20 13:28:55 -05001666 if (canDrawArgs.fShape->style().applies()) {
1667 tempShape = canDrawArgs.fShape->applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec,
1668 styleScale);
1669 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001670 return;
1671 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05001672 canDrawArgs.fShape = &tempShape;
Brian Salomone7df0bb2018-05-07 14:44:57 -04001673 // This time, allow SW renderer
1674 pr = this->drawingManager()->getPathRenderer(canDrawArgs, true, kType);
1675 } else {
1676 pr = this->drawingManager()->getSoftwarePathRenderer();
bsalomon6663acf2016-05-10 09:14:17 -07001677 }
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05001678 }
robertphillipsea461502015-05-26 11:38:03 -07001679
bsalomon8acedde2016-06-24 10:42:16 -07001680 if (!pr) {
robertphillipsea461502015-05-26 11:38:03 -07001681#ifdef SK_DEBUG
1682 SkDebugf("Unable to find path renderer compatible with path.\n");
1683#endif
1684 return;
1685 }
1686
Robert Phillips256c37b2017-03-01 14:32:46 -05001687 GrPathRenderer::DrawPathArgs args{this->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05001688 std::move(paint),
1689 &GrUserStencilSettings::kUnused,
1690 this,
1691 &clip,
Chris Daltondb91c6e2017-09-08 16:25:08 -06001692 &clipConservativeBounds,
Brian Salomon82f44312017-01-11 13:42:54 -05001693 &viewMatrix,
Brian Salomon2fad74a2017-12-20 13:28:55 -05001694 canDrawArgs.fShape,
Brian Salomon82f44312017-01-11 13:42:54 -05001695 aaType,
Brian Salomonf3569f02017-10-24 12:52:33 -04001696 this->colorSpaceInfo().isGammaCorrect()};
bsalomon0aff2fa2015-07-31 06:48:27 -07001697 pr->drawPath(args);
robertphillipsea461502015-05-26 11:38:03 -07001698}
1699
Brian Salomon467921e2017-03-06 16:17:12 -05001700static void op_bounds(SkRect* bounds, const GrOp* op) {
1701 *bounds = op->bounds();
1702 if (op->hasZeroArea()) {
1703 if (op->hasAABloat()) {
1704 bounds->outset(0.5f, 0.5f);
1705 } else {
1706 // We don't know which way the particular GPU will snap lines or points at integer
1707 // coords. So we ensure that the bounds is large enough for either snap.
1708 SkRect before = *bounds;
1709 bounds->roundOut(bounds);
1710 if (bounds->fLeft == before.fLeft) {
1711 bounds->fLeft -= 1;
1712 }
1713 if (bounds->fTop == before.fTop) {
1714 bounds->fTop -= 1;
1715 }
1716 if (bounds->fRight == before.fRight) {
1717 bounds->fRight += 1;
1718 }
1719 if (bounds->fBottom == before.fBottom) {
1720 bounds->fBottom += 1;
1721 }
1722 }
1723 }
1724}
1725
Brian Salomon54d212e2017-03-21 14:22:38 -04001726uint32_t GrRenderTargetContext::addDrawOp(const GrClip& clip, std::unique_ptr<GrDrawOp> op) {
joshualitt1de610a2016-01-06 08:26:09 -08001727 ASSERT_SINGLE_OWNER
Robert Phillipsc0138922017-03-08 11:50:55 -05001728 if (this->drawingManager()->wasAbandoned()) {
Robert Phillipsc994a932018-06-19 13:09:54 -04001729 fContext->contextPriv().opMemoryPool()->release(std::move(op));
Robert Phillipsc0138922017-03-08 11:50:55 -05001730 return SK_InvalidUniqueID;
1731 }
robertphillips2e1e51f2015-10-15 08:01:48 -07001732 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001733 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "addDrawOp", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -07001734
Brian Salomon467921e2017-03-06 16:17:12 -05001735 // Setup clip
1736 SkRect bounds;
1737 op_bounds(&bounds, op.get());
Brian Salomon97180af2017-03-14 13:42:58 -04001738 GrAppliedClip appliedClip;
Brian Salomon54d212e2017-03-21 14:22:38 -04001739 GrDrawOp::FixedFunctionFlags fixedFunctionFlags = op->fixedFunctionFlags();
1740 if (!clip.apply(fContext, this, fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesHWAA,
1741 fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil, &appliedClip,
1742 &bounds)) {
Robert Phillipsc994a932018-06-19 13:09:54 -04001743 fContext->contextPriv().opMemoryPool()->release(std::move(op));
Brian Salomon54d212e2017-03-21 14:22:38 -04001744 return SK_InvalidUniqueID;
1745 }
1746
Brian Salomon54d212e2017-03-21 14:22:38 -04001747 if (fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil ||
1748 appliedClip.hasStencilClip()) {
Robert Phillips6b47c7d2017-08-29 07:24:09 -04001749 this->getOpList()->setStencilLoadOp(GrLoadOp::kClear);
Robert Phillips95214472017-08-08 18:00:03 -04001750
Robert Phillips65048132017-08-10 08:44:49 -04001751 this->setNeedsStencil();
Brian Salomon54d212e2017-03-21 14:22:38 -04001752 }
1753
Brian Salomonf3569f02017-10-24 12:52:33 -04001754 GrPixelConfigIsClamped dstIsClamped =
1755 GrGetPixelConfigIsClamped(this->colorSpaceInfo().config());
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001756 GrXferProcessor::DstProxy dstProxy;
Brian Osman9a725dd2017-09-20 09:53:22 -04001757 if (GrDrawOp::RequiresDstTexture::kYes == op->finalize(*this->caps(), &appliedClip,
1758 dstIsClamped)) {
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001759 if (!this->setupDstProxy(this->asRenderTargetProxy(), clip, op->bounds(), &dstProxy)) {
Robert Phillipsc994a932018-06-19 13:09:54 -04001760 fContext->contextPriv().opMemoryPool()->release(std::move(op));
Brian Salomon54d212e2017-03-21 14:22:38 -04001761 return SK_InvalidUniqueID;
1762 }
1763 }
1764
1765 op->setClippedBounds(bounds);
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001766 return this->getRTOpList()->addOp(std::move(op), *this->caps(),
1767 std::move(appliedClip), dstProxy);
Brian Salomon54d212e2017-03-21 14:22:38 -04001768}
1769
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001770bool GrRenderTargetContext::setupDstProxy(GrRenderTargetProxy* rtProxy, const GrClip& clip,
Robert Phillips16d8ec62017-07-27 16:16:25 -04001771 const SkRect& opBounds,
1772 GrXferProcessor::DstProxy* dstProxy) {
Brian Salomon467921e2017-03-06 16:17:12 -05001773 if (this->caps()->textureBarrierSupport()) {
Robert Phillipsbf25d432017-04-07 10:08:53 -04001774 if (GrTextureProxy* texProxy = rtProxy->asTextureProxy()) {
Brian Salomon467921e2017-03-06 16:17:12 -05001775 // The render target is a texture, so we can read from it directly in the shader. The XP
1776 // will be responsible to detect this situation and request a texture barrier.
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001777 dstProxy->setProxy(sk_ref_sp(texProxy));
1778 dstProxy->setOffset(0, 0);
Robert Phillipsbf25d432017-04-07 10:08:53 -04001779 return true;
Brian Salomon467921e2017-03-06 16:17:12 -05001780 }
1781 }
1782
Robert Phillipsbf25d432017-04-07 10:08:53 -04001783 SkIRect copyRect = SkIRect::MakeWH(rtProxy->width(), rtProxy->height());
Brian Salomon467921e2017-03-06 16:17:12 -05001784
Eric Karl74480882017-04-03 14:49:05 -07001785 SkIRect clippedRect;
Robert Phillipsbf25d432017-04-07 10:08:53 -04001786 clip.getConservativeBounds(rtProxy->width(), rtProxy->height(), &clippedRect);
Eric Karl72e551e2017-04-04 13:42:10 -07001787 SkIRect drawIBounds;
Brian Salomon467921e2017-03-06 16:17:12 -05001788 opBounds.roundOut(&drawIBounds);
Brian Salomon859621f2017-03-16 09:21:54 -04001789 // Cover up for any precision issues by outsetting the op bounds a pixel in each direction.
1790 drawIBounds.outset(1, 1);
Eric Karl72e551e2017-04-04 13:42:10 -07001791 if (!clippedRect.intersect(drawIBounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -05001792#ifdef SK_DEBUG
Robert Phillipsbf25d432017-04-07 10:08:53 -04001793 GrCapsDebugf(this->caps(), "setupDstTexture: Missed an early reject bailing on draw.");
Brian Salomon467921e2017-03-06 16:17:12 -05001794#endif
Robert Phillipsbf25d432017-04-07 10:08:53 -04001795 return false;
Brian Salomon467921e2017-03-06 16:17:12 -05001796 }
1797
1798 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
1799 // have per-sample dst values by making the copy multisampled.
1800 GrSurfaceDesc desc;
Eric Karl74480882017-04-03 14:49:05 -07001801 bool rectsMustMatch = false;
1802 bool disallowSubrect = false;
Brian Salomon2a4f9832018-03-03 22:43:43 -05001803 GrSurfaceOrigin origin;
1804 if (!this->caps()->initDescForDstCopy(rtProxy, &desc, &origin, &rectsMustMatch,
1805 &disallowSubrect)) {
Brian Salomon467921e2017-03-06 16:17:12 -05001806 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillipsbf25d432017-04-07 10:08:53 -04001807 desc.fConfig = rtProxy->config();
Greg Daniel1efe3222018-04-04 14:02:51 -04001808 origin = rtProxy->origin();
Brian Salomon467921e2017-03-06 16:17:12 -05001809 }
1810
Eric Karl74480882017-04-03 14:49:05 -07001811 if (!disallowSubrect) {
1812 copyRect = clippedRect;
1813 }
Brian Salomon467921e2017-03-06 16:17:12 -05001814
Robert Phillipsbf25d432017-04-07 10:08:53 -04001815 SkIPoint dstPoint, dstOffset;
1816 SkBackingFit fit;
Eric Karl74480882017-04-03 14:49:05 -07001817 if (rectsMustMatch) {
Robert Phillipsbf25d432017-04-07 10:08:53 -04001818 desc.fWidth = rtProxy->width();
1819 desc.fHeight = rtProxy->height();
Eric Karl74480882017-04-03 14:49:05 -07001820 dstPoint = {copyRect.fLeft, copyRect.fTop};
1821 dstOffset = {0, 0};
Robert Phillipsbf25d432017-04-07 10:08:53 -04001822 fit = SkBackingFit::kExact;
Eric Karl74480882017-04-03 14:49:05 -07001823 } else {
1824 desc.fWidth = copyRect.width();
1825 desc.fHeight = copyRect.height();
1826 dstPoint = {0, 0};
1827 dstOffset = {copyRect.fLeft, copyRect.fTop};
Robert Phillipsbf25d432017-04-07 10:08:53 -04001828 fit = SkBackingFit::kApprox;
Eric Karl74480882017-04-03 14:49:05 -07001829 }
Brian Salomon467921e2017-03-06 16:17:12 -05001830
Robert Phillipsbf25d432017-04-07 10:08:53 -04001831 sk_sp<GrSurfaceContext> sContext = fContext->contextPriv().makeDeferredSurfaceContext(
Brian Salomon2a4f9832018-03-03 22:43:43 -05001832 desc, origin, GrMipMapped::kNo, fit, SkBudgeted::kYes,
Brian Salomonf802e752018-02-13 17:13:31 -05001833 sk_ref_sp(this->colorSpaceInfo().colorSpace()));
Robert Phillipsbf25d432017-04-07 10:08:53 -04001834 if (!sContext) {
1835 SkDebugf("setupDstTexture: surfaceContext creation failed.\n");
1836 return false;
1837 }
1838
1839 if (!sContext->copy(rtProxy, copyRect, dstPoint)) {
1840 SkDebugf("setupDstTexture: copy failed.\n");
1841 return false;
1842 }
1843
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001844 dstProxy->setProxy(sContext->asTextureProxyRef());
1845 dstProxy->setOffset(dstOffset);
Robert Phillipsbf25d432017-04-07 10:08:53 -04001846 return true;
robertphillips2334fb62015-06-17 05:43:33 -07001847}