blob: 6f93a1c1904816557ef4dc6439897f589b2bd8e5 [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"
Jim Van Verth34d6e4b2017-06-09 11:09:03 -040031#include "SkShadowUtils.h"
robertphillips2d70dcb2015-10-06 07:38:23 -070032#include "SkSurfacePriv.h"
Brian Salomon467921e2017-03-06 16:17:12 -050033#include "effects/GrRRectEffect.h"
Brian Salomonf18b1d82017-10-27 11:30:49 -040034#include "ops/GrAtlasTextOp.h"
Brian Salomon89527432016-12-16 09:52:16 -050035#include "ops/GrClearOp.h"
Robert Phillipsb9a02a12017-04-06 11:08:40 -040036#include "ops/GrClearStencilClipOp.h"
Robert Phillips65a88fa2017-08-08 08:36:22 -040037#include "ops/GrDebugMarkerOp.h"
Brian Salomon0f353322017-05-03 20:58:59 +000038#include "ops/GrDrawAtlasOp.h"
Brian Salomon6d4b65e2017-05-03 17:06:09 -040039#include "ops/GrDrawOp.h"
Brian Salomon89527432016-12-16 09:52:16 -050040#include "ops/GrDrawVerticesOp.h"
41#include "ops/GrLatticeOp.h"
42#include "ops/GrOp.h"
43#include "ops/GrOvalOpFactory.h"
44#include "ops/GrRectOpFactory.h"
45#include "ops/GrRegionOp.h"
Greg Daniela5cb7812017-06-16 09:45:32 -040046#include "ops/GrSemaphoreOp.h"
Brian Salomon89527432016-12-16 09:52:16 -050047#include "ops/GrShadowRRectOp.h"
Brian Salomon467921e2017-03-06 16:17:12 -050048#include "ops/GrStencilPathOp.h"
Brian Salomon34169692017-08-28 15:32:01 -040049#include "ops/GrTextureOp.h"
joshualitte8042922015-12-11 06:11:21 -080050#include "text/GrAtlasTextContext.h"
51#include "text/GrStencilAndCoverTextContext.h"
Brian Salomonf18b1d82017-10-27 11:30:49 -040052#include "text/GrTextUtils.h"
53
54class GrRenderTargetContext::TextTarget : public GrTextUtils::Target {
55public:
56 TextTarget(GrRenderTargetContext* renderTargetContext)
57 : Target(renderTargetContext->width(), renderTargetContext->height(),
58 renderTargetContext->colorSpaceInfo())
59 , fRenderTargetContext(renderTargetContext) {}
60
61 void addDrawOp(const GrClip& clip, std::unique_ptr<GrAtlasTextOp> op) {
62 fRenderTargetContext->addDrawOp(clip, std::move(op));
63 }
64
65 void drawPath(const GrClip& clip, const SkPath& path, const SkPaint& paint,
66 const SkMatrix& viewMatrix, const SkMatrix* pathMatrix,
67 const SkIRect& clipBounds) {
68 GrBlurUtils::drawPathWithMaskFilter(fRenderTargetContext->fContext, fRenderTargetContext,
69 clip, path, paint, viewMatrix, pathMatrix, clipBounds,
70 false);
71 }
72
73 void makeGrPaint(GrMaskFormat maskFormat, const SkPaint& skPaint, const SkMatrix& viewMatrix,
74 GrPaint* grPaint) {
75 GrContext* context = fRenderTargetContext->fContext;
76 const GrColorSpaceInfo& colorSpaceInfo = fRenderTargetContext->colorSpaceInfo();
77 if (kARGB_GrMaskFormat == maskFormat) {
78 SkPaintToGrPaintWithPrimitiveColor(context, colorSpaceInfo, skPaint, grPaint);
79 } else {
80 SkPaintToGrPaint(context, colorSpaceInfo, skPaint, viewMatrix, grPaint);
81 }
82 }
83
84private:
85 GrRenderTargetContext* fRenderTargetContext;
86};
joshualittbc907352016-01-13 06:45:40 -080087
Robert Phillips72152832017-01-25 17:31:35 -050088#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this->drawingManager()->getContext())
joshualitt1de610a2016-01-06 08:26:09 -080089#define ASSERT_SINGLE_OWNER \
Robert Phillipsa90aa2b2017-04-10 08:19:26 -040090 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(this->singleOwner());)
robertphillips391395d2016-03-02 09:26:36 -080091#define ASSERT_SINGLE_OWNER_PRIV \
Robert Phillipsa90aa2b2017-04-10 08:19:26 -040092 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fRenderTargetContext->singleOwner());)
Robert Phillips72152832017-01-25 17:31:35 -050093#define RETURN_IF_ABANDONED if (this->drawingManager()->wasAbandoned()) { return; }
94#define RETURN_IF_ABANDONED_PRIV if (fRenderTargetContext->drawingManager()->wasAbandoned()) { return; }
95#define RETURN_FALSE_IF_ABANDONED if (this->drawingManager()->wasAbandoned()) { return false; }
96#define RETURN_FALSE_IF_ABANDONED_PRIV if (fRenderTargetContext->drawingManager()->wasAbandoned()) { return false; }
97#define RETURN_NULL_IF_ABANDONED if (this->drawingManager()->wasAbandoned()) { return nullptr; }
robertphillipsea461502015-05-26 11:38:03 -070098
Brian Salomone225b562017-06-14 13:00:03 -040099//////////////////////////////////////////////////////////////////////////////
100
101GrAAType GrChooseAAType(GrAA aa, GrFSAAType fsaaType, GrAllowMixedSamples allowMixedSamples,
102 const GrCaps& caps) {
103 if (GrAA::kNo == aa) {
104 // On some devices we cannot disable MSAA if it is enabled so we make the AA type reflect
105 // that.
106 if (fsaaType == GrFSAAType::kUnifiedMSAA && !caps.multisampleDisableSupport()) {
107 return GrAAType::kMSAA;
108 }
109 return GrAAType::kNone;
110 }
111 switch (fsaaType) {
112 case GrFSAAType::kNone:
113 return GrAAType::kCoverage;
114 case GrFSAAType::kUnifiedMSAA:
115 return GrAAType::kMSAA;
116 case GrFSAAType::kMixedSamples:
117 return GrAllowMixedSamples::kYes == allowMixedSamples ? GrAAType::kMixedSamples
118 : GrAAType::kCoverage;
119 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400120 SK_ABORT("Unexpected fsaa type");
Brian Salomone225b562017-06-14 13:00:03 -0400121 return GrAAType::kNone;
122}
123
124//////////////////////////////////////////////////////////////////////////////
125
robertphillipsea461502015-05-26 11:38:03 -0700126class AutoCheckFlush {
127public:
halcanary9d524f22016-03-29 09:03:52 -0700128 AutoCheckFlush(GrDrawingManager* drawingManager) : fDrawingManager(drawingManager) {
robertphillips77a2e522015-10-17 07:43:27 -0700129 SkASSERT(fDrawingManager);
130 }
bsalomonb77a9072016-09-07 10:02:04 -0700131 ~AutoCheckFlush() { fDrawingManager->flushIfNecessary(); }
robertphillipsea461502015-05-26 11:38:03 -0700132
133private:
robertphillips77a2e522015-10-17 07:43:27 -0700134 GrDrawingManager* fDrawingManager;
robertphillipsea461502015-05-26 11:38:03 -0700135};
136
Brian Osman11052242016-10-27 14:47:55 -0400137bool GrRenderTargetContext::wasAbandoned() const {
Robert Phillips72152832017-01-25 17:31:35 -0500138 return this->drawingManager()->wasAbandoned();
robertphillips7761d612016-05-16 09:14:53 -0700139}
140
Robert Phillipsf2361d22016-10-25 14:20:06 -0400141// In MDB mode the reffing of the 'getLastOpList' call's result allows in-progress
Brian Osman11052242016-10-27 14:47:55 -0400142// GrOpLists to be picked up and added to by renderTargetContexts lower in the call
Robert Phillipsf2361d22016-10-25 14:20:06 -0400143// stack. When this occurs with a closed GrOpList, a new one will be allocated
Brian Osman11052242016-10-27 14:47:55 -0400144// when the renderTargetContext attempts to use it (via getOpList).
145GrRenderTargetContext::GrRenderTargetContext(GrContext* context,
146 GrDrawingManager* drawingMgr,
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400147 sk_sp<GrRenderTargetProxy> rtp,
Brian Osman11052242016-10-27 14:47:55 -0400148 sk_sp<SkColorSpace> colorSpace,
149 const SkSurfaceProps* surfaceProps,
150 GrAuditTrail* auditTrail,
Robert Phillips941d1442017-06-14 16:37:02 -0400151 GrSingleOwner* singleOwner,
152 bool managedOpList)
Brian Salomonf3569f02017-10-24 12:52:33 -0400153 : GrSurfaceContext(context, drawingMgr, rtp->config(), std::move(colorSpace), auditTrail,
154 singleOwner)
155 , fRenderTargetProxy(std::move(rtp))
156 , fOpList(sk_ref_sp(fRenderTargetProxy->getLastRenderTargetOpList()))
Brian Salomonf3569f02017-10-24 12:52:33 -0400157 , fSurfaceProps(SkSurfacePropsCopyOrDefault(surfaceProps))
158 , fManagedOpList(managedOpList) {
Robert Phillips2188e092017-11-09 13:57:06 -0500159#ifdef SK_DISABLE_EXPLICIT_GPU_RESOURCE_ALLOCATION
Robert Phillipsd9914862017-06-14 15:16:59 -0400160 // MDB TODO: to ensure all resources still get allocated in the correct order in the hybrid
161 // world we need to get the correct opList here so that it, in turn, can grab and hold
162 // its rendertarget.
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400163 this->getRTOpList();
Robert Phillips09dfc472017-09-13 15:25:47 -0400164#endif
Brian Salomonf18b1d82017-10-27 11:30:49 -0400165 fTextTarget.reset(new TextTarget(this));
robertphillips2e1e51f2015-10-15 08:01:48 -0700166 SkDEBUGCODE(this->validate();)
robertphillipsea461502015-05-26 11:38:03 -0700167}
168
robertphillips2e1e51f2015-10-15 08:01:48 -0700169#ifdef SK_DEBUG
Brian Osman11052242016-10-27 14:47:55 -0400170void GrRenderTargetContext::validate() const {
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400171 SkASSERT(fRenderTargetProxy);
172 fRenderTargetProxy->validate(fContext);
robertphillipsa106c622015-10-16 09:07:06 -0700173
Robert Phillipsf2361d22016-10-25 14:20:06 -0400174 if (fOpList && !fOpList->isClosed()) {
Robert Phillipsdc83b892017-04-13 12:23:54 -0400175 SkASSERT(fRenderTargetProxy->getLastOpList() == fOpList.get());
robertphillipsa106c622015-10-16 09:07:06 -0700176 }
robertphillips2e1e51f2015-10-15 08:01:48 -0700177}
178#endif
179
Brian Osman11052242016-10-27 14:47:55 -0400180GrRenderTargetContext::~GrRenderTargetContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800181 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700182}
183
Robert Phillipsf200a902017-01-30 13:27:37 -0500184GrTextureProxy* GrRenderTargetContext::asTextureProxy() {
Robert Phillipseaa86252016-11-08 13:49:39 +0000185 return fRenderTargetProxy->asTextureProxy();
186}
187
Greg Daniele252f082017-10-23 16:05:23 -0400188const GrTextureProxy* GrRenderTargetContext::asTextureProxy() const {
189 return fRenderTargetProxy->asTextureProxy();
190}
191
Robert Phillipsf200a902017-01-30 13:27:37 -0500192sk_sp<GrTextureProxy> GrRenderTargetContext::asTextureProxyRef() {
193 return sk_ref_sp(fRenderTargetProxy->asTextureProxy());
194}
195
Greg Daniele252f082017-10-23 16:05:23 -0400196GrMipMapped GrRenderTargetContext::mipMapped() const {
197 if (const GrTextureProxy* proxy = this->asTextureProxy()) {
198 return proxy->mipMapped();
199 }
200 return GrMipMapped::kNo;
201}
202
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400203GrRenderTargetOpList* GrRenderTargetContext::getRTOpList() {
joshualitt1de610a2016-01-06 08:26:09 -0800204 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700205 SkDEBUGCODE(this->validate();)
206
Robert Phillipsf2361d22016-10-25 14:20:06 -0400207 if (!fOpList || fOpList->isClosed()) {
Robert Phillips941d1442017-06-14 16:37:02 -0400208 fOpList = this->drawingManager()->newRTOpList(fRenderTargetProxy.get(), fManagedOpList);
robertphillipsa106c622015-10-16 09:07:06 -0700209 }
210
Robert Phillipsdc83b892017-04-13 12:23:54 -0400211 return fOpList.get();
robertphillipsa106c622015-10-16 09:07:06 -0700212}
213
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400214GrOpList* GrRenderTargetContext::getOpList() {
215 return this->getRTOpList();
robertphillipsea461502015-05-26 11:38:03 -0700216}
217
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500218void GrRenderTargetContext::drawText(const GrClip& clip, const SkPaint& skPaint,
Brian Salomon82f44312017-01-11 13:42:54 -0500219 const SkMatrix& viewMatrix, const char text[],
220 size_t byteLength, SkScalar x, SkScalar y,
221 const SkIRect& clipBounds) {
joshualitt1de610a2016-01-06 08:26:09 -0800222 ASSERT_SINGLE_OWNER
robertphillips2d70dcb2015-10-06 07:38:23 -0700223 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700224 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400225 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawText", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -0700226
Robert Phillips72152832017-01-25 17:31:35 -0500227 GrAtlasTextContext* atlasTextContext = this->drawingManager()->getAtlasTextContext();
Brian Salomonf18b1d82017-10-27 11:30:49 -0400228 atlasTextContext->drawText(fContext, fTextTarget.get(), clip, skPaint, viewMatrix,
229 fSurfaceProps, text, byteLength, x, y, clipBounds);
robertphillips2334fb62015-06-17 05:43:33 -0700230}
robertphillipscaef3452015-11-11 13:18:11 -0800231
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500232void GrRenderTargetContext::drawPosText(const GrClip& clip, const SkPaint& paint,
233 const SkMatrix& viewMatrix, const char text[],
234 size_t byteLength, const SkScalar pos[],
Brian Salomon82f44312017-01-11 13:42:54 -0500235 int scalarsPerPosition, const SkPoint& offset,
236 const SkIRect& clipBounds) {
joshualitt1de610a2016-01-06 08:26:09 -0800237 ASSERT_SINGLE_OWNER
robertphillips2d70dcb2015-10-06 07:38:23 -0700238 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700239 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400240 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawPosText", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -0700241
Robert Phillips72152832017-01-25 17:31:35 -0500242 GrAtlasTextContext* atlasTextContext = this->drawingManager()->getAtlasTextContext();
Brian Salomonf18b1d82017-10-27 11:30:49 -0400243 atlasTextContext->drawPosText(fContext, fTextTarget.get(), clip, paint, viewMatrix,
244 fSurfaceProps, text, byteLength, pos, scalarsPerPosition, offset,
245 clipBounds);
robertphillips2334fb62015-06-17 05:43:33 -0700246}
robertphillipscaef3452015-11-11 13:18:11 -0800247
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500248void GrRenderTargetContext::drawTextBlob(const GrClip& clip, const SkPaint& paint,
Brian Osman11052242016-10-27 14:47:55 -0400249 const SkMatrix& viewMatrix, const SkTextBlob* blob,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500250 SkScalar x, SkScalar y, SkDrawFilter* filter,
251 const SkIRect& clipBounds) {
joshualitt1de610a2016-01-06 08:26:09 -0800252 ASSERT_SINGLE_OWNER
robertphillips2d70dcb2015-10-06 07:38:23 -0700253 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700254 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400255 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTextBlob", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -0700256
Robert Phillips72152832017-01-25 17:31:35 -0500257 GrAtlasTextContext* atlasTextContext = this->drawingManager()->getAtlasTextContext();
Brian Salomonf18b1d82017-10-27 11:30:49 -0400258 atlasTextContext->drawTextBlob(fContext, fTextTarget.get(), clip, paint, viewMatrix,
259 fSurfaceProps, blob, x, y, filter, clipBounds);
robertphillipsea461502015-05-26 11:38:03 -0700260}
261
Brian Osman11052242016-10-27 14:47:55 -0400262void GrRenderTargetContext::discard() {
joshualitt1de610a2016-01-06 08:26:09 -0800263 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700264 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700265 SkDEBUGCODE(this->validate();)
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400266 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "discard", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700267
Robert Phillips72152832017-01-25 17:31:35 -0500268 AutoCheckFlush acf(this->drawingManager());
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400269
Robert Phillips380b90c2017-08-30 07:41:07 -0400270 this->getRTOpList()->discard();
robertphillipsea461502015-05-26 11:38:03 -0700271}
272
Brian Osman11052242016-10-27 14:47:55 -0400273void GrRenderTargetContext::clear(const SkIRect* rect,
274 const GrColor color,
Chris Dalton344e9032017-12-11 15:42:09 -0700275 CanClearFullscreen canClearFullscreen) {
joshualitt1de610a2016-01-06 08:26:09 -0800276 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700277 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700278 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400279 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "clear", fContext);
robertphillipsea461502015-05-26 11:38:03 -0700280
Robert Phillips72152832017-01-25 17:31:35 -0500281 AutoCheckFlush acf(this->drawingManager());
Chris Dalton344e9032017-12-11 15:42:09 -0700282 this->internalClear(rect ? GrFixedClip(*rect) : GrFixedClip::Disabled(), color,
283 canClearFullscreen);
csmartdalton29df7602016-08-31 11:55:52 -0700284}
robertphillips9199a9f2016-07-13 07:48:43 -0700285
Robert Phillips784b7bf2016-12-09 13:35:02 -0500286void GrRenderTargetContextPriv::absClear(const SkIRect* clearRect, const GrColor color) {
287 ASSERT_SINGLE_OWNER_PRIV
288 RETURN_IF_ABANDONED_PRIV
289 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400290 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "absClear",
291 fRenderTargetContext->fContext);
Robert Phillips784b7bf2016-12-09 13:35:02 -0500292
Robert Phillips72152832017-01-25 17:31:35 -0500293 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500294
Brian Salomonbb5711a2017-05-17 13:49:59 -0400295 SkIRect rtRect = SkIRect::MakeWH(fRenderTargetContext->fRenderTargetProxy->worstCaseWidth(),
296 fRenderTargetContext->fRenderTargetProxy->worstCaseHeight());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500297
298 if (clearRect) {
299 if (clearRect->contains(rtRect)) {
300 clearRect = nullptr; // full screen
301 } else {
302 if (!rtRect.intersect(*clearRect)) {
303 return;
304 }
305 }
306 }
307
308 // TODO: in a post-MDB world this should be handled at the OpList level.
309 // An op-list that is initially cleared and has no other ops should receive an
310 // extra draw.
Brian Salomon43f8bf02017-10-18 08:33:29 -0400311 // This path doesn't handle coalescing of full screen clears b.c. it
312 // has to clear the entire render target - not just the content area.
313 // It could be done but will take more finagling.
314 std::unique_ptr<GrOp> op(GrClearOp::Make(rtRect, color, !clearRect));
315 if (!op) {
316 return;
Robert Phillips784b7bf2016-12-09 13:35:02 -0500317 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400318 fRenderTargetContext->getRTOpList()->addOp(std::move(op), *fRenderTargetContext->caps());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500319}
320
Brian Osman11052242016-10-27 14:47:55 -0400321void GrRenderTargetContextPriv::clear(const GrFixedClip& clip,
322 const GrColor color,
Chris Dalton344e9032017-12-11 15:42:09 -0700323 CanClearFullscreen canClearFullscreen) {
csmartdalton29df7602016-08-31 11:55:52 -0700324 ASSERT_SINGLE_OWNER_PRIV
325 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400326 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400327 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "clear",
328 fRenderTargetContext->fContext);
csmartdalton29df7602016-08-31 11:55:52 -0700329
Robert Phillips72152832017-01-25 17:31:35 -0500330 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Chris Dalton344e9032017-12-11 15:42:09 -0700331 fRenderTargetContext->internalClear(clip, color, canClearFullscreen);
csmartdalton29df7602016-08-31 11:55:52 -0700332}
333
Brian Osman11052242016-10-27 14:47:55 -0400334void GrRenderTargetContext::internalClear(const GrFixedClip& clip,
335 const GrColor color,
Chris Dalton344e9032017-12-11 15:42:09 -0700336 CanClearFullscreen canClearFullscreen) {
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700337 bool isFull = false;
338 if (!clip.hasWindowRectangles()) {
339 isFull = !clip.scissorEnabled() ||
Chris Dalton344e9032017-12-11 15:42:09 -0700340 (CanClearFullscreen::kYes == canClearFullscreen &&
341 fContext->caps()->preferFullscreenClears()) ||
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700342 clip.scissorRect().contains(SkIRect::MakeWH(this->width(), this->height()));
343 }
robertphillips9199a9f2016-07-13 07:48:43 -0700344
Brian Salomon43f8bf02017-10-18 08:33:29 -0400345 if (isFull) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400346 this->getRTOpList()->fullClear(*this->caps(), color);
robertphillips9199a9f2016-07-13 07:48:43 -0700347 } else {
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000348 std::unique_ptr<GrOp> op(GrClearOp::Make(clip, color, this->asSurfaceProxy()));
Brian Salomon42521e82016-12-07 16:44:58 -0500349 if (!op) {
csmartdalton29df7602016-08-31 11:55:52 -0700350 return;
351 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400352 this->getRTOpList()->addOp(std::move(op), *this->caps());
robertphillips9199a9f2016-07-13 07:48:43 -0700353 }
robertphillipsea461502015-05-26 11:38:03 -0700354}
355
Brian Osman11052242016-10-27 14:47:55 -0400356void GrRenderTargetContext::drawPaint(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500357 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400358 const SkMatrix& viewMatrix) {
joshualitt1de610a2016-01-06 08:26:09 -0800359 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700360 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700361 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400362 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawPaint", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700363
robertphillipsea461502015-05-26 11:38:03 -0700364 // set rect to be big enough to fill the space, but not super-huge, so we
365 // don't overflow fixed-point implementations
robertphillips13a7eee2016-08-31 15:06:24 -0700366
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400367 SkRect r = fRenderTargetProxy->getBoundsRect();
robertphillipsea461502015-05-26 11:38:03 -0700368
bsalomoncb31e512016-08-26 10:48:19 -0700369 SkRRect rrect;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500370 GrAA aa;
bsalomoncb31e512016-08-26 10:48:19 -0700371 // Check if we can replace a clipRRect()/drawPaint() with a drawRRect(). We only do the
372 // transformation for non-rect rrects. Rects caused a performance regression on an Android
373 // test that needs investigation. We also skip cases where there are fragment processors
374 // because they may depend on having correct local coords and this path draws in device space
375 // without a local matrix.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500376 if (!paint.numTotalFragmentProcessors() && clip.isRRect(r, &rrect, &aa) && !rrect.isRect()) {
Brian Salomon82f44312017-01-11 13:42:54 -0500377 this->drawRRect(GrNoClip(), std::move(paint), aa, SkMatrix::I(), rrect,
378 GrStyle::SimpleFill());
bsalomoncb31e512016-08-26 10:48:19 -0700379 return;
380 }
381
robertphillipsea461502015-05-26 11:38:03 -0700382
383 bool isPerspective = viewMatrix.hasPerspective();
384
385 // We attempt to map r by the inverse matrix and draw that. mapRect will
386 // map the four corners and bound them with a new rect. This will not
387 // produce a correct result for some perspective matrices.
388 if (!isPerspective) {
reeda39667c2016-08-22 06:39:49 -0700389 if (!SkMatrixPriv::InverseMapRect(viewMatrix, &r, r)) {
robertphillipsea461502015-05-26 11:38:03 -0700390 SkDebugf("Could not invert matrix\n");
391 return;
392 }
Brian Salomon82f44312017-01-11 13:42:54 -0500393 this->drawRect(clip, std::move(paint), GrAA::kNo, viewMatrix, r);
robertphillipsea461502015-05-26 11:38:03 -0700394 } else {
395 SkMatrix localMatrix;
396 if (!viewMatrix.invert(&localMatrix)) {
397 SkDebugf("Could not invert matrix\n");
398 return;
399 }
400
Robert Phillips72152832017-01-25 17:31:35 -0500401 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700402
Brian Salomonbaaf4392017-06-15 09:59:23 -0400403 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeNonAAFillWithLocalMatrix(
404 std::move(paint), SkMatrix::I(), localMatrix, r, GrAAType::kNone);
405 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -0700406 }
407}
408
robertphillipsea461502015-05-26 11:38:03 -0700409static inline bool rect_contains_inclusive(const SkRect& rect, const SkPoint& point) {
410 return point.fX >= rect.fLeft && point.fX <= rect.fRight &&
411 point.fY >= rect.fTop && point.fY <= rect.fBottom;
412}
413
csmartdalton97f6cd52016-07-13 13:37:08 -0700414// Attempts to crop a rect and optional local rect to the clip boundaries.
415// Returns false if the draw can be skipped entirely.
robertphillips13a7eee2016-08-31 15:06:24 -0700416static bool crop_filled_rect(int width, int height, const GrClip& clip,
csmartdalton97f6cd52016-07-13 13:37:08 -0700417 const SkMatrix& viewMatrix, SkRect* rect,
418 SkRect* localRect = nullptr) {
419 if (!viewMatrix.rectStaysRect()) {
420 return true;
421 }
422
csmartdalton97f6cd52016-07-13 13:37:08 -0700423 SkIRect clipDevBounds;
424 SkRect clipBounds;
csmartdalton97f6cd52016-07-13 13:37:08 -0700425
robertphillips13a7eee2016-08-31 15:06:24 -0700426 clip.getConservativeBounds(width, height, &clipDevBounds);
reeda39667c2016-08-22 06:39:49 -0700427 if (!SkMatrixPriv::InverseMapRect(viewMatrix, &clipBounds, SkRect::Make(clipDevBounds))) {
428 return false;
429 }
csmartdalton97f6cd52016-07-13 13:37:08 -0700430
431 if (localRect) {
432 if (!rect->intersects(clipBounds)) {
433 return false;
434 }
435 const SkScalar dx = localRect->width() / rect->width();
436 const SkScalar dy = localRect->height() / rect->height();
437 if (clipBounds.fLeft > rect->fLeft) {
438 localRect->fLeft += (clipBounds.fLeft - rect->fLeft) * dx;
439 rect->fLeft = clipBounds.fLeft;
440 }
441 if (clipBounds.fTop > rect->fTop) {
442 localRect->fTop += (clipBounds.fTop - rect->fTop) * dy;
443 rect->fTop = clipBounds.fTop;
444 }
445 if (clipBounds.fRight < rect->fRight) {
446 localRect->fRight -= (rect->fRight - clipBounds.fRight) * dx;
447 rect->fRight = clipBounds.fRight;
448 }
449 if (clipBounds.fBottom < rect->fBottom) {
450 localRect->fBottom -= (rect->fBottom - clipBounds.fBottom) * dy;
451 rect->fBottom = clipBounds.fBottom;
452 }
453 return true;
454 }
455
456 return rect->intersect(clipBounds);
457}
458
Brian Osman11052242016-10-27 14:47:55 -0400459bool GrRenderTargetContext::drawFilledRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500460 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500461 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400462 const SkMatrix& viewMatrix,
463 const SkRect& rect,
464 const GrUserStencilSettings* ss) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700465 SkRect croppedRect = rect;
Robert Phillips784b7bf2016-12-09 13:35:02 -0500466 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix, &croppedRect)) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700467 return true;
468 }
robertphillips44302392016-07-08 14:43:03 -0700469
Brian Salomon7c8460e2017-05-12 11:36:10 -0400470 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400471 std::unique_ptr<GrDrawOp> op;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500472 if (GrAAType::kCoverage == aaType) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400473 op = GrRectOpFactory::MakeAAFill(std::move(paint), viewMatrix, croppedRect, ss);
robertphillips391395d2016-03-02 09:26:36 -0800474 } else {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400475 op = GrRectOpFactory::MakeNonAAFill(std::move(paint), viewMatrix, croppedRect, aaType, ss);
robertphillips391395d2016-03-02 09:26:36 -0800476 }
Brian Salomonbaaf4392017-06-15 09:59:23 -0400477 if (!op) {
478 return false;
479 }
480 this->addDrawOp(clip, std::move(op));
481 return true;
robertphillips391395d2016-03-02 09:26:36 -0800482}
483
Brian Osman11052242016-10-27 14:47:55 -0400484void GrRenderTargetContext::drawRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500485 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500486 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400487 const SkMatrix& viewMatrix,
488 const SkRect& rect,
489 const GrStyle* style) {
bsalomon6663acf2016-05-10 09:14:17 -0700490 if (!style) {
491 style = &GrStyle::SimpleFill();
492 }
joshualitt1de610a2016-01-06 08:26:09 -0800493 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700494 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700495 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400496 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRect", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700497
bsalomon6663acf2016-05-10 09:14:17 -0700498 // Path effects should've been devolved to a path in SkGpuDevice
499 SkASSERT(!style->pathEffect());
robertphillipsea461502015-05-26 11:38:03 -0700500
Robert Phillips72152832017-01-25 17:31:35 -0500501 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700502
bsalomon6663acf2016-05-10 09:14:17 -0700503 const SkStrokeRec& stroke = style->strokeRec();
robertphillips3ab14ca2016-07-10 11:49:39 -0700504 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
Brian Salomon43f8bf02017-10-18 08:33:29 -0400505 // Check if this is a full RT draw and can be replaced with a clear. We don't bother
506 // checking cases where the RT is fully inside a stroke.
507 SkRect rtRect = fRenderTargetProxy->getBoundsRect();
508 // Does the clip contain the entire RT?
509 if (clip.quickContains(rtRect)) {
510 SkMatrix invM;
511 if (!viewMatrix.invert(&invM)) {
512 return;
513 }
514 // Does the rect bound the RT?
515 GrQuad quad;
516 quad.setFromMappedRect(rtRect, invM);
517 if (rect_contains_inclusive(rect, quad.point(0)) &&
518 rect_contains_inclusive(rect, quad.point(1)) &&
519 rect_contains_inclusive(rect, quad.point(2)) &&
520 rect_contains_inclusive(rect, quad.point(3))) {
521 // Will it blend?
522 GrColor clearColor;
523 if (paint.isConstantBlendedColor(&clearColor)) {
Chris Dalton344e9032017-12-11 15:42:09 -0700524 this->clear(nullptr, clearColor,
525 GrRenderTargetContext::CanClearFullscreen::kYes);
robertphillipsea461502015-05-26 11:38:03 -0700526 return;
527 }
528 }
529 }
robertphillips44302392016-07-08 14:43:03 -0700530
Brian Salomon82f44312017-01-11 13:42:54 -0500531 if (this->drawFilledRect(clip, std::move(paint), aa, viewMatrix, rect, nullptr)) {
robertphillips44302392016-07-08 14:43:03 -0700532 return;
533 }
bsalomona7d85ba2016-07-06 11:54:59 -0700534 } else if (stroke.getStyle() == SkStrokeRec::kStroke_Style ||
535 stroke.getStyle() == SkStrokeRec::kHairline_Style) {
536 if ((!rect.width() || !rect.height()) &&
537 SkStrokeRec::kHairline_Style != stroke.getStyle()) {
538 SkScalar r = stroke.getWidth() / 2;
539 // TODO: Move these stroke->fill fallbacks to GrShape?
540 switch (stroke.getJoin()) {
541 case SkPaint::kMiter_Join:
Brian Salomon82f44312017-01-11 13:42:54 -0500542 this->drawRect(
543 clip, std::move(paint), aa, viewMatrix,
544 {rect.fLeft - r, rect.fTop - r, rect.fRight + r, rect.fBottom + r},
545 &GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700546 return;
547 case SkPaint::kRound_Join:
548 // Raster draws nothing when both dimensions are empty.
549 if (rect.width() || rect.height()){
550 SkRRect rrect = SkRRect::MakeRectXY(rect.makeOutset(r, r), r, r);
Brian Salomon82f44312017-01-11 13:42:54 -0500551 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect,
552 GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700553 return;
554 }
555 case SkPaint::kBevel_Join:
556 if (!rect.width()) {
Brian Salomon82f44312017-01-11 13:42:54 -0500557 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700558 {rect.fLeft - r, rect.fTop, rect.fRight + r, rect.fBottom},
559 &GrStyle::SimpleFill());
560 } else {
Brian Salomon82f44312017-01-11 13:42:54 -0500561 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700562 {rect.fLeft, rect.fTop - r, rect.fRight, rect.fBottom + r},
563 &GrStyle::SimpleFill());
564 }
565 return;
566 }
567 }
robertphillips44302392016-07-08 14:43:03 -0700568
Brian Salomonbaaf4392017-06-15 09:59:23 -0400569 std::unique_ptr<GrDrawOp> op;
robertphillips44302392016-07-08 14:43:03 -0700570
Brian Salomon7c8460e2017-05-12 11:36:10 -0400571 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500572 if (GrAAType::kCoverage == aaType) {
cdaltonbb539482016-01-04 09:48:25 -0800573 // The stroke path needs the rect to remain axis aligned (no rotation or skew).
574 if (viewMatrix.rectStaysRect()) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400575 op = GrRectOpFactory::MakeAAStroke(std::move(paint), viewMatrix, rect, stroke);
cdaltonbb539482016-01-04 09:48:25 -0800576 }
robertphillipsea461502015-05-26 11:38:03 -0700577 } else {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400578 op = GrRectOpFactory::MakeNonAAStroke(std::move(paint), viewMatrix, rect, stroke,
579 aaType);
robertphillips391395d2016-03-02 09:26:36 -0800580 }
robertphillips4bc31812016-03-01 12:22:49 -0800581
Brian Salomon42521e82016-12-07 16:44:58 -0500582 if (op) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400583 this->addDrawOp(clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700584 return;
robertphillips4bc31812016-03-01 12:22:49 -0800585 }
robertphillips4bc31812016-03-01 12:22:49 -0800586 }
halcanary9d524f22016-03-29 09:03:52 -0700587
robertphillips4bc31812016-03-01 12:22:49 -0800588 SkPath path;
589 path.setIsVolatile(true);
590 path.addRect(rect);
Brian Salomon82f44312017-01-11 13:42:54 -0500591 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, *style);
robertphillipsea461502015-05-26 11:38:03 -0700592}
593
Robert Phillipsec2249f2016-11-09 08:54:35 -0500594int GrRenderTargetContextPriv::maxWindowRectangles() const {
595 return fRenderTargetContext->fRenderTargetProxy->maxWindowRectangles(
596 *fRenderTargetContext->fContext->caps());
597}
598
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000599void GrRenderTargetContextPriv::clearStencilClip(const GrFixedClip& clip, bool insideStencilMask) {
robertphillips976f5f02016-06-03 10:59:20 -0700600 ASSERT_SINGLE_OWNER_PRIV
601 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400602 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400603 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "clearStencilClip",
604 fRenderTargetContext->fContext);
robertphillips976f5f02016-06-03 10:59:20 -0700605
Robert Phillips72152832017-01-25 17:31:35 -0500606 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400607
Robert Phillips2f4ddf62017-06-01 08:48:19 -0400608 std::unique_ptr<GrOp> op(GrClearStencilClipOp::Make(
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000609 clip, insideStencilMask,
610 fRenderTargetContext->fRenderTargetProxy.get()));
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400611 if (!op) {
Robert Phillipse60ad622016-11-17 10:22:48 -0500612 return;
613 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400614 fRenderTargetContext->getRTOpList()->addOp(std::move(op), *fRenderTargetContext->caps());
robertphillips976f5f02016-06-03 10:59:20 -0700615}
616
Chris Daltonbbfd5162017-11-07 13:35:22 -0700617void GrRenderTargetContextPriv::stencilPath(const GrHardClip& clip,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500618 GrAAType aaType,
Brian Osman11052242016-10-27 14:47:55 -0400619 const SkMatrix& viewMatrix,
620 const GrPath* path) {
Brian Salomon467921e2017-03-06 16:17:12 -0500621 ASSERT_SINGLE_OWNER_PRIV
622 RETURN_IF_ABANDONED_PRIV
623 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400624 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "stencilPath",
625 fRenderTargetContext->fContext);
Brian Salomon467921e2017-03-06 16:17:12 -0500626
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500627 SkASSERT(aaType != GrAAType::kCoverage);
Brian Salomon467921e2017-03-06 16:17:12 -0500628
629 bool useHWAA = GrAATypeIsHW(aaType);
630 // TODO: extract portions of checkDraw that are relevant to path stenciling.
631 SkASSERT(path);
632 SkASSERT(fRenderTargetContext->caps()->shaderCaps()->pathRenderingSupport());
633
634 // FIXME: Use path bounds instead of this WAR once
635 // https://bugs.chromium.org/p/skia/issues/detail?id=5640 is resolved.
636 SkRect bounds = SkRect::MakeIWH(fRenderTargetContext->width(), fRenderTargetContext->height());
637
638 // Setup clip
Chris Daltonbbfd5162017-11-07 13:35:22 -0700639 GrAppliedHardClip appliedClip;
640 if (!clip.apply(fRenderTargetContext->width(), fRenderTargetContext->height(), &appliedClip,
641 &bounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -0500642 return;
643 }
644
Robert Phillips65048132017-08-10 08:44:49 -0400645 fRenderTargetContext->setNeedsStencil();
Brian Salomon467921e2017-03-06 16:17:12 -0500646
647 std::unique_ptr<GrOp> op = GrStencilPathOp::Make(viewMatrix,
648 useHWAA,
649 path->getFillType(),
650 appliedClip.hasStencilClip(),
Brian Salomon467921e2017-03-06 16:17:12 -0500651 appliedClip.scissorState(),
Brian Salomon467921e2017-03-06 16:17:12 -0500652 path);
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400653 if (!op) {
654 return;
655 }
Brian Salomon97180af2017-03-14 13:42:58 -0400656 op->setClippedBounds(bounds);
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400657 fRenderTargetContext->getRTOpList()->addOp(std::move(op), *fRenderTargetContext->caps());
robertphillips976f5f02016-06-03 10:59:20 -0700658}
659
Chris Daltonbbfd5162017-11-07 13:35:22 -0700660void GrRenderTargetContextPriv::stencilRect(const GrHardClip& clip,
Brian Osman11052242016-10-27 14:47:55 -0400661 const GrUserStencilSettings* ss,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500662 GrAAType aaType,
Brian Osman11052242016-10-27 14:47:55 -0400663 const SkMatrix& viewMatrix,
664 const SkRect& rect) {
robertphillips976f5f02016-06-03 10:59:20 -0700665 ASSERT_SINGLE_OWNER_PRIV
666 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400667 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400668 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "stencilRect",
669 fRenderTargetContext->fContext);
670
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500671 SkASSERT(GrAAType::kCoverage != aaType);
Robert Phillips72152832017-01-25 17:31:35 -0500672 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips976f5f02016-06-03 10:59:20 -0700673
674 GrPaint paint;
Brian Salomona1633922017-01-09 11:46:10 -0500675 paint.setXPFactory(GrDisableColorXPFactory::Get());
Brian Salomonbaaf4392017-06-15 09:59:23 -0400676 std::unique_ptr<GrDrawOp> op =
677 GrRectOpFactory::MakeNonAAFill(std::move(paint), viewMatrix, rect, aaType, ss);
678 fRenderTargetContext->addDrawOp(clip, std::move(op));
robertphillips976f5f02016-06-03 10:59:20 -0700679}
680
Chris Daltonbbfd5162017-11-07 13:35:22 -0700681bool GrRenderTargetContextPriv::drawAndStencilRect(const GrHardClip& clip,
Brian Osman11052242016-10-27 14:47:55 -0400682 const GrUserStencilSettings* ss,
683 SkRegion::Op op,
684 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500685 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400686 const SkMatrix& viewMatrix,
687 const SkRect& rect) {
robertphillips391395d2016-03-02 09:26:36 -0800688 ASSERT_SINGLE_OWNER_PRIV
689 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400690 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400691 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawAndStencilRect",
692 fRenderTargetContext->fContext);
robertphillips391395d2016-03-02 09:26:36 -0800693
Robert Phillips72152832017-01-25 17:31:35 -0500694 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -0800695
696 GrPaint paint;
robertphillips391395d2016-03-02 09:26:36 -0800697 paint.setCoverageSetOpXPFactory(op, invert);
698
Brian Salomon82f44312017-01-11 13:42:54 -0500699 if (fRenderTargetContext->drawFilledRect(clip, std::move(paint), aa, viewMatrix, rect, ss)) {
robertphillips391395d2016-03-02 09:26:36 -0800700 return true;
701 }
robertphillips391395d2016-03-02 09:26:36 -0800702 SkPath path;
703 path.setIsVolatile(true);
704 path.addRect(rect);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500705 return this->drawAndStencilPath(clip, ss, op, invert, aa, viewMatrix, path);
robertphillips391395d2016-03-02 09:26:36 -0800706}
707
Brian Osman11052242016-10-27 14:47:55 -0400708void GrRenderTargetContext::fillRectToRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500709 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500710 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400711 const SkMatrix& viewMatrix,
712 const SkRect& rectToDraw,
713 const SkRect& localRect) {
joshualitt1de610a2016-01-06 08:26:09 -0800714 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700715 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700716 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400717 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "fillRectToRect", fContext);
robertphillipsea461502015-05-26 11:38:03 -0700718
csmartdalton97f6cd52016-07-13 13:37:08 -0700719 SkRect croppedRect = rectToDraw;
720 SkRect croppedLocalRect = localRect;
robertphillips13a7eee2016-08-31 15:06:24 -0700721 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix,
722 &croppedRect, &croppedLocalRect)) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700723 return;
724 }
725
Robert Phillips72152832017-01-25 17:31:35 -0500726 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -0700727
Brian Salomon7c8460e2017-05-12 11:36:10 -0400728 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500729 if (GrAAType::kCoverage != aaType) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400730 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeNonAAFillWithLocalRect(
731 std::move(paint), viewMatrix, croppedRect, croppedLocalRect, aaType);
732 this->addDrawOp(clip, std::move(op));
csmartdaltonfc49d562016-07-26 17:05:47 -0700733 return;
joshualitt04194f32016-01-13 10:08:27 -0800734 }
bsalomonbb243832016-07-22 07:10:19 -0700735
Brian Salomonbaaf4392017-06-15 09:59:23 -0400736 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeAAFillWithLocalRect(
737 std::move(paint), viewMatrix, croppedRect, croppedLocalRect);
738 if (op) {
739 this->addDrawOp(clip, std::move(op));
csmartdaltonfc49d562016-07-26 17:05:47 -0700740 return;
741 }
742
743 SkMatrix viewAndUnLocalMatrix;
744 if (!viewAndUnLocalMatrix.setRectToRect(localRect, rectToDraw, SkMatrix::kFill_ScaleToFit)) {
745 SkDebugf("fillRectToRect called with empty local matrix.\n");
746 return;
747 }
748 viewAndUnLocalMatrix.postConcat(viewMatrix);
749
750 SkPath path;
751 path.setIsVolatile(true);
752 path.addRect(localRect);
Brian Salomon82f44312017-01-11 13:42:54 -0500753 this->internalDrawPath(clip, std::move(paint), aa, viewAndUnLocalMatrix, path, GrStyle());
joshualittb6b513b2015-08-21 10:25:18 -0700754}
755
Brian Salomon34169692017-08-28 15:32:01 -0400756static bool must_filter(const SkRect& src, const SkRect& dst, const SkMatrix& ctm) {
757 // We don't currently look for 90 degree rotations, mirroring, or downscales that sample at
758 // texel centers.
759 if (!ctm.isTranslate()) {
760 return true;
761 }
762 if (src.width() != dst.width() || src.height() != dst.height()) {
763 return true;
764 }
765 // Check that the device space rectangle's fractional offset is the same as the src rectangle,
766 // and that therefore integers in the src image fall on integers in device space.
767 SkScalar x = ctm.getTranslateX(), y = ctm.getTranslateY();
768 x += dst.fLeft; y += dst.fTop;
769 x -= src.fLeft; y -= src.fTop;
770 return !SkScalarIsInt(x) || !SkScalarIsInt(y);
771}
772
773void GrRenderTargetContext::drawTextureAffine(const GrClip& clip, sk_sp<GrTextureProxy> proxy,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400774 GrSamplerState::Filter filter, GrColor color,
Brian Salomon34169692017-08-28 15:32:01 -0400775 const SkRect& srcRect, const SkRect& dstRect,
776 const SkMatrix& viewMatrix,
777 sk_sp<GrColorSpaceXform> colorSpaceXform) {
778 ASSERT_SINGLE_OWNER
779 RETURN_IF_ABANDONED
780 SkDEBUGCODE(this->validate();)
781 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTextureAffine", fContext);
782 SkASSERT(!viewMatrix.hasPerspective());
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400783 if (filter != GrSamplerState::Filter::kNearest && !must_filter(srcRect, dstRect, viewMatrix)) {
784 filter = GrSamplerState::Filter::kNearest;
Brian Salomon34169692017-08-28 15:32:01 -0400785 }
Brian Salomonff9d6d32017-08-30 10:27:49 -0400786 SkRect clippedDstRect = dstRect;
787 SkRect clippedSrcRect = srcRect;
788 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix, &clippedDstRect,
789 &clippedSrcRect)) {
790 return;
791 }
792
Brian Salomonf3569f02017-10-24 12:52:33 -0400793 bool allowSRGB = SkToBool(this->colorSpaceInfo().colorSpace());
Brian Salomonff9d6d32017-08-30 10:27:49 -0400794 this->addDrawOp(clip, GrTextureOp::Make(std::move(proxy), filter, color, clippedSrcRect,
795 clippedDstRect, viewMatrix, std::move(colorSpaceXform),
796 allowSRGB));
Brian Salomon34169692017-08-28 15:32:01 -0400797}
798
Brian Osman11052242016-10-27 14:47:55 -0400799void GrRenderTargetContext::fillRectWithLocalMatrix(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500800 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500801 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400802 const SkMatrix& viewMatrix,
803 const SkRect& rectToDraw,
804 const SkMatrix& localMatrix) {
joshualitt1de610a2016-01-06 08:26:09 -0800805 ASSERT_SINGLE_OWNER
joshualittb6b513b2015-08-21 10:25:18 -0700806 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700807 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400808 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "fillRectWithLocalMatrix", fContext);
joshualittb6b513b2015-08-21 10:25:18 -0700809
csmartdalton97f6cd52016-07-13 13:37:08 -0700810 SkRect croppedRect = rectToDraw;
robertphillips13a7eee2016-08-31 15:06:24 -0700811 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix, &croppedRect)) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700812 return;
813 }
814
Robert Phillips72152832017-01-25 17:31:35 -0500815 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -0700816
Brian Salomon7c8460e2017-05-12 11:36:10 -0400817 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500818 if (GrAAType::kCoverage != aaType) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400819 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeNonAAFillWithLocalMatrix(
820 std::move(paint), viewMatrix, localMatrix, croppedRect, aaType);
821 this->addDrawOp(clip, std::move(op));
csmartdaltonfc49d562016-07-26 17:05:47 -0700822 return;
bsalomonc55271f2015-11-09 11:55:57 -0800823 }
robertphillips4bc31812016-03-01 12:22:49 -0800824
Brian Salomonbaaf4392017-06-15 09:59:23 -0400825 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeAAFillWithLocalMatrix(
826 std::move(paint), viewMatrix, localMatrix, croppedRect);
827 if (op) {
828 this->addDrawOp(clip, std::move(op));
csmartdaltonfc49d562016-07-26 17:05:47 -0700829 return;
830 }
831
832 SkMatrix viewAndUnLocalMatrix;
833 if (!localMatrix.invert(&viewAndUnLocalMatrix)) {
834 SkDebugf("fillRectWithLocalMatrix called with degenerate local matrix.\n");
835 return;
836 }
837 viewAndUnLocalMatrix.postConcat(viewMatrix);
838
839 SkPath path;
840 path.setIsVolatile(true);
841 path.addRect(rectToDraw);
842 path.transform(localMatrix);
Brian Salomon82f44312017-01-11 13:42:54 -0500843 this->internalDrawPath(clip, std::move(paint), aa, viewAndUnLocalMatrix, path, GrStyle());
robertphillipsea461502015-05-26 11:38:03 -0700844}
845
Brian Osman11052242016-10-27 14:47:55 -0400846void GrRenderTargetContext::drawVertices(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500847 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400848 const SkMatrix& viewMatrix,
Brian Osmanae0c50c2017-05-25 16:56:34 -0400849 sk_sp<SkVertices> vertices,
850 GrPrimitiveType* overridePrimType) {
Brian Salomon199fb872017-02-06 09:41:10 -0500851 ASSERT_SINGLE_OWNER
852 RETURN_IF_ABANDONED
853 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400854 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawVertices", fContext);
Brian Salomon199fb872017-02-06 09:41:10 -0500855
856 AutoCheckFlush acf(this->drawingManager());
857
858 SkASSERT(vertices);
Brian Salomonc2f42542017-07-12 14:11:22 -0400859 GrAAType aaType = this->chooseAAType(GrAA::kNo, GrAllowMixedSamples::kNo);
Brian Salomonf3569f02017-10-24 12:52:33 -0400860 std::unique_ptr<GrDrawOp> op = GrDrawVerticesOp::Make(
861 std::move(paint), std::move(vertices), viewMatrix, aaType,
862 this->colorSpaceInfo().isGammaCorrect(),
863 this->colorSpaceInfo().refColorSpaceXformFromSRGB(), overridePrimType);
Brian Salomonc2f42542017-07-12 14:11:22 -0400864 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -0700865}
866
867///////////////////////////////////////////////////////////////////////////////
868
Brian Osman11052242016-10-27 14:47:55 -0400869void GrRenderTargetContext::drawAtlas(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500870 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400871 const SkMatrix& viewMatrix,
872 int spriteCount,
873 const SkRSXform xform[],
874 const SkRect texRect[],
875 const SkColor colors[]) {
joshualitt1de610a2016-01-06 08:26:09 -0800876 ASSERT_SINGLE_OWNER
jvanverth31ff7622015-08-07 10:09:28 -0700877 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700878 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400879 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawAtlas", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700880
Robert Phillips72152832017-01-25 17:31:35 -0500881 AutoCheckFlush acf(this->drawingManager());
halcanary9d524f22016-03-29 09:03:52 -0700882
Brian Salomon0088f942017-07-12 11:51:27 -0400883 GrAAType aaType = this->chooseAAType(GrAA::kNo, GrAllowMixedSamples::kNo);
884 std::unique_ptr<GrDrawOp> op = GrDrawAtlasOp::Make(std::move(paint), viewMatrix, aaType,
885 spriteCount, xform, texRect, colors);
886 this->addDrawOp(clip, std::move(op));
jvanverth31ff7622015-08-07 10:09:28 -0700887}
888
889///////////////////////////////////////////////////////////////////////////////
890
Brian Osman11052242016-10-27 14:47:55 -0400891void GrRenderTargetContext::drawRRect(const GrClip& origClip,
Brian Salomon82f44312017-01-11 13:42:54 -0500892 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500893 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400894 const SkMatrix& viewMatrix,
895 const SkRRect& rrect,
896 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -0800897 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700898 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700899 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400900 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRRect", fContext);
robertphillipsea461502015-05-26 11:38:03 -0700901 if (rrect.isEmpty()) {
902 return;
903 }
904
bsalomon7f0d9f32016-08-15 14:49:10 -0700905 GrNoClip noclip;
906 const GrClip* clip = &origClip;
907#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
908 // The Android framework frequently clips rrects to themselves where the clip is non-aa and the
Brian Salomon42521e82016-12-07 16:44:58 -0500909 // draw is aa. Since our lower level clip code works from op bounds, which are SkRects, it
bsalomon7f0d9f32016-08-15 14:49:10 -0700910 // doesn't detect that the clip can be ignored (modulo antialiasing). The following test
911 // attempts to mitigate the stencil clip cost but will only help when the entire clip stack
912 // can be ignored. We'd prefer to fix this in the framework by removing the clips calls.
913 SkRRect devRRect;
914 if (rrect.transform(viewMatrix, &devRRect) && clip->quickContains(devRRect)) {
915 clip = &noclip;
916 }
917#endif
bsalomon6663acf2016-05-10 09:14:17 -0700918 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
ksakamotoec7f2ac2016-07-05 03:54:53 -0700919
Robert Phillips72152832017-01-25 17:31:35 -0500920 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -0700921 const SkStrokeRec stroke = style.strokeRec();
csmartdaltona7f29642016-07-07 08:49:11 -0700922
Brian Salomon7c8460e2017-05-12 11:36:10 -0400923 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500924 if (GrAAType::kCoverage == aaType) {
Brian Salomon94efbf52016-11-29 13:43:05 -0500925 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomon05441c42017-05-15 16:45:49 -0400926 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeRRectOp(std::move(paint),
Brian Salomon05441c42017-05-15 16:45:49 -0400927 viewMatrix,
928 rrect,
929 stroke,
930 shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -0500931 if (op) {
Brian Salomon05441c42017-05-15 16:45:49 -0400932 this->addDrawOp(*clip, std::move(op));
robertphillipsb56f9272016-02-25 11:03:52 -0800933 return;
934 }
robertphillipsea461502015-05-26 11:38:03 -0700935 }
robertphillipsb56f9272016-02-25 11:03:52 -0800936
937 SkPath path;
938 path.setIsVolatile(true);
939 path.addRRect(rrect);
Brian Salomon82f44312017-01-11 13:42:54 -0500940 this->internalDrawPath(*clip, std::move(paint), aa, viewMatrix, path, style);
robertphillipsea461502015-05-26 11:38:03 -0700941}
942
Jim Van Verthc5903412016-11-17 15:27:09 -0500943///////////////////////////////////////////////////////////////////////////////
944
Jim Van Verth3af1af92017-05-18 15:06:54 -0400945static SkPoint3 map(const SkMatrix& m, const SkPoint3& pt) {
946 SkPoint3 result;
947 m.mapXY(pt.fX, pt.fY, (SkPoint*)&result.fX);
948 result.fZ = pt.fZ;
949 return result;
950}
951
952bool GrRenderTargetContext::drawFastShadow(const GrClip& clip,
Brian Salomon05969092017-07-13 11:20:51 -0400953 GrColor color4ub,
Jim Van Verth3af1af92017-05-18 15:06:54 -0400954 const SkMatrix& viewMatrix,
955 const SkPath& path,
956 const SkDrawShadowRec& rec) {
Jim Van Verthc5903412016-11-17 15:27:09 -0500957 ASSERT_SINGLE_OWNER
Jim Van Verth3af1af92017-05-18 15:06:54 -0400958 if (this->drawingManager()->wasAbandoned()) {
959 return true;
960 }
Jim Van Verthc5903412016-11-17 15:27:09 -0500961 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400962 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFastShadow", fContext);
Jim Van Verth3af1af92017-05-18 15:06:54 -0400963
964 // check z plane
965 bool tiltZPlane = SkToBool(!SkScalarNearlyZero(rec.fZPlaneParams.fX) ||
966 !SkScalarNearlyZero(rec.fZPlaneParams.fY));
967 bool skipAnalytic = SkToBool(rec.fFlags & SkShadowFlags::kGeometricOnly_ShadowFlag);
968 if (tiltZPlane || skipAnalytic || !viewMatrix.rectStaysRect() || !viewMatrix.isSimilarity()) {
969 return false;
970 }
971
972 SkRRect rrect;
973 SkRect rect;
974 // we can only handle rects, circles, and rrects with circular corners
975 bool isRRect = path.isRRect(&rrect) && rrect.isSimpleCircular() &&
976 rrect.radii(SkRRect::kUpperLeft_Corner).fX > SK_ScalarNearlyZero;
977 if (!isRRect &&
978 path.isOval(&rect) && SkScalarNearlyEqual(rect.width(), rect.height()) &&
979 rect.width() > SK_ScalarNearlyZero) {
980 rrect.setOval(rect);
981 isRRect = true;
982 }
983 if (!isRRect && path.isRect(&rect)) {
984 rrect.setRect(rect);
985 isRRect = true;
986 }
987
988 if (!isRRect) {
989 return false;
990 }
991
Jim Van Verthc5903412016-11-17 15:27:09 -0500992 if (rrect.isEmpty()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -0400993 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -0500994 }
995
Robert Phillips72152832017-01-25 17:31:35 -0500996 AutoCheckFlush acf(this->drawingManager());
Jim Van Verthc5903412016-11-17 15:27:09 -0500997
Jim Van Verth3af1af92017-05-18 15:06:54 -0400998 // transform light
999 SkPoint3 devLightPos = map(viewMatrix, rec.fLightPos);
1000
1001 // 1/scale
1002 SkScalar devToSrcScale = viewMatrix.isScaleTranslate() ?
1003 SkScalarInvert(viewMatrix[SkMatrix::kMScaleX]) :
1004 sk_float_rsqrt(viewMatrix[SkMatrix::kMScaleX] * viewMatrix[SkMatrix::kMScaleX] +
1005 viewMatrix[SkMatrix::kMSkewX] * viewMatrix[SkMatrix::kMSkewX]);
1006
1007 SkScalar occluderHeight = rec.fZPlaneParams.fZ;
Brian Salomon05969092017-07-13 11:20:51 -04001008 GrColor4f color = GrColor4f::FromGrColor(color4ub);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001009 bool transparent = SkToBool(rec.fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag);
Jim Van Verth53d863c2017-11-20 14:28:01 -05001010 bool tonalColor = !SkToBool(rec.fFlags & SkShadowFlags::kDisableTonalColor_ShadowFlag);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001011
1012 if (rec.fAmbientAlpha > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001013 SkScalar devSpaceInsetWidth = SkDrawShadowMetrics::AmbientBlurRadius(occluderHeight);
1014 const SkScalar umbraRecipAlpha = SkDrawShadowMetrics::AmbientRecipAlpha(occluderHeight);
1015 const SkScalar devSpaceAmbientBlur = devSpaceInsetWidth * umbraRecipAlpha;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001016
1017 // Outset the shadow rrect to the border of the penumbra
1018 SkScalar ambientPathOutset = devSpaceInsetWidth * devToSrcScale;
1019 SkRRect ambientRRect;
1020 SkRect outsetRect = rrect.rect().makeOutset(ambientPathOutset, ambientPathOutset);
1021 // If the rrect was an oval then its outset will also be one.
1022 // We set it explicitly to avoid errors.
1023 if (rrect.isOval()) {
1024 ambientRRect = SkRRect::MakeOval(outsetRect);
1025 } else {
1026 SkScalar outsetRad = rrect.getSimpleRadii().fX + ambientPathOutset;
1027 ambientRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1028 }
1029
Jim Van Verth34d6e4b2017-06-09 11:09:03 -04001030 GrColor ambientColor;
1031 if (tonalColor) {
1032 // with tonal color, the color only applies to the spot shadow
1033 ambientColor = GrColorPackRGBA(0, 0, 0, 255.999f*rec.fAmbientAlpha);
1034 } else {
1035 ambientColor = color.mulByScalar(rec.fAmbientAlpha).toGrColor();
1036 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001037 if (transparent) {
1038 // set a large inset to force a fill
1039 devSpaceInsetWidth = ambientRRect.width();
1040 }
1041 // the fraction of the blur we want to apply is devSpaceInsetWidth/devSpaceAmbientBlur,
Jim Van Verth1af03d42017-07-31 09:34:58 -04001042 // which is just 1/umbraRecipAlpha.
1043 SkScalar blurClamp = SkScalarInvert(umbraRecipAlpha);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001044
Brian Salomon05969092017-07-13 11:20:51 -04001045 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(ambientColor, viewMatrix,
1046 ambientRRect,
1047 devSpaceAmbientBlur,
1048 devSpaceInsetWidth,
1049 blurClamp);
1050 SkASSERT(op);
1051 this->addDrawOp(clip, std::move(op));
Jim Van Verthc5903412016-11-17 15:27:09 -05001052 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001053
1054 if (rec.fSpotAlpha > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001055 SkScalar devSpaceSpotBlur;
1056 SkScalar spotScale;
1057 SkVector spotOffset;
1058 SkDrawShadowMetrics::GetSpotParams(occluderHeight, devLightPos.fX, devLightPos.fY,
1059 devLightPos.fZ, rec.fLightRadius,
1060 &devSpaceSpotBlur, &spotScale, &spotOffset);
1061 // handle scale of radius due to CTM
Jim Van Verth3af1af92017-05-18 15:06:54 -04001062 const SkScalar srcSpaceSpotBlur = devSpaceSpotBlur * devToSrcScale;
1063
Jim Van Verth3af1af92017-05-18 15:06:54 -04001064 // Adjust translate for the effect of the scale.
1065 spotOffset.fX += spotScale*viewMatrix[SkMatrix::kMTransX];
1066 spotOffset.fY += spotScale*viewMatrix[SkMatrix::kMTransY];
1067 // This offset is in dev space, need to transform it into source space.
1068 SkMatrix ctmInverse;
1069 if (viewMatrix.invert(&ctmInverse)) {
1070 ctmInverse.mapPoints(&spotOffset, 1);
1071 } else {
1072 // Since the matrix is a similarity, this should never happen, but just in case...
1073 SkDebugf("Matrix is degenerate. Will not render spot shadow correctly!\n");
1074 SkASSERT(false);
1075 }
1076
1077 // Compute the transformed shadow rrect
1078 SkRRect spotShadowRRect;
1079 SkMatrix shadowTransform;
1080 shadowTransform.setScaleTranslate(spotScale, spotScale, spotOffset.fX, spotOffset.fY);
1081 rrect.transform(shadowTransform, &spotShadowRRect);
1082 SkScalar spotRadius = spotShadowRRect.getSimpleRadii().fX;
1083
1084 // Compute the insetWidth
Jim Van Verth1af03d42017-07-31 09:34:58 -04001085 SkScalar blurOutset = srcSpaceSpotBlur;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001086 SkScalar insetWidth = blurOutset;
1087 if (transparent) {
1088 // If transparent, just do a fill
1089 insetWidth += spotShadowRRect.width();
1090 } else {
1091 // For shadows, instead of using a stroke we specify an inset from the penumbra
1092 // border. We want to extend this inset area so that it meets up with the caster
1093 // geometry. The inset geometry will by default already be inset by the blur width.
1094 //
1095 // We compare the min and max corners inset by the radius between the original
1096 // rrect and the shadow rrect. The distance between the two plus the difference
1097 // between the scaled radius and the original radius gives the distance from the
1098 // transformed shadow shape to the original shape in that corner. The max
1099 // of these gives the maximum distance we need to cover.
1100 //
1101 // Since we are outsetting by 1/2 the blur distance, we just add the maxOffset to
1102 // that to get the full insetWidth.
1103 SkScalar maxOffset;
1104 if (rrect.isRect()) {
1105 // Manhattan distance works better for rects
1106 maxOffset = SkTMax(SkTMax(SkTAbs(spotShadowRRect.rect().fLeft -
1107 rrect.rect().fLeft),
1108 SkTAbs(spotShadowRRect.rect().fTop -
1109 rrect.rect().fTop)),
1110 SkTMax(SkTAbs(spotShadowRRect.rect().fRight -
1111 rrect.rect().fRight),
1112 SkTAbs(spotShadowRRect.rect().fBottom -
1113 rrect.rect().fBottom)));
1114 } else {
1115 SkScalar dr = spotRadius - rrect.getSimpleRadii().fX;
1116 SkPoint upperLeftOffset = SkPoint::Make(spotShadowRRect.rect().fLeft -
1117 rrect.rect().fLeft + dr,
1118 spotShadowRRect.rect().fTop -
1119 rrect.rect().fTop + dr);
1120 SkPoint lowerRightOffset = SkPoint::Make(spotShadowRRect.rect().fRight -
1121 rrect.rect().fRight - dr,
1122 spotShadowRRect.rect().fBottom -
1123 rrect.rect().fBottom - dr);
Cary Clarkdf429f32017-11-08 11:44:31 -05001124 maxOffset = SkScalarSqrt(SkTMax(SkPointPriv::LengthSqd(upperLeftOffset),
1125 SkPointPriv::LengthSqd(lowerRightOffset))) + dr;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001126 }
1127 insetWidth += maxOffset;
1128 }
1129
1130 // Outset the shadow rrect to the border of the penumbra
1131 SkRect outsetRect = spotShadowRRect.rect().makeOutset(blurOutset, blurOutset);
1132 if (spotShadowRRect.isOval()) {
1133 spotShadowRRect = SkRRect::MakeOval(outsetRect);
1134 } else {
1135 SkScalar outsetRad = spotRadius + blurOutset;
1136 spotShadowRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1137 }
1138
Jim Van Verth34d6e4b2017-06-09 11:09:03 -04001139 GrColor spotColor;
1140 if (tonalColor) {
1141 SkScalar colorScale;
1142 SkScalar tonalAlpha;
1143 SkShadowUtils::ComputeTonalColorParams(color.fRGBA[0], color.fRGBA[1],
1144 color.fRGBA[2], rec.fSpotAlpha,
1145 &colorScale, &tonalAlpha);
1146 color.fRGBA[0] *= colorScale;
1147 color.fRGBA[1] *= colorScale;
1148 color.fRGBA[2] *= colorScale;
1149 color.fRGBA[3] = tonalAlpha;
1150 spotColor = color.toGrColor();
1151 } else {
1152 spotColor = color.mulByScalar(rec.fSpotAlpha).toGrColor();
1153 }
1154
Brian Salomon05969092017-07-13 11:20:51 -04001155 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(spotColor, viewMatrix,
1156 spotShadowRRect,
Jim Van Verth1af03d42017-07-31 09:34:58 -04001157 2.0f * devSpaceSpotBlur,
Brian Salomon05969092017-07-13 11:20:51 -04001158 insetWidth);
1159 SkASSERT(op);
1160 this->addDrawOp(clip, std::move(op));
Jim Van Verth3af1af92017-05-18 15:06:54 -04001161 }
1162
1163 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001164}
1165
1166///////////////////////////////////////////////////////////////////////////////
1167
Brian Osman11052242016-10-27 14:47:55 -04001168bool GrRenderTargetContext::drawFilledDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001169 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001170 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001171 const SkMatrix& viewMatrix,
1172 const SkRRect& origOuter,
1173 const SkRRect& origInner) {
robertphillips00095892016-02-29 13:50:40 -08001174 SkASSERT(!origInner.isEmpty());
1175 SkASSERT(!origOuter.isEmpty());
1176
Brian Salomon65749212017-12-01 16:01:47 -05001177 SkTCopyOnFirstWrite<SkRRect> inner(origInner), outer(origOuter);
1178
Brian Salomon45839f92017-12-04 09:02:35 -05001179 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
1180
1181 if (GrAAType::kMSAA == aaType) {
1182 return false;
1183 }
1184
1185 if (GrAAType::kCoverage == aaType && inner->isCircle() && outer->isCircle()) {
Brian Salomon65749212017-12-01 16:01:47 -05001186 auto outerR = outer->width() / 2.f;
1187 auto innerR = inner->width() / 2.f;
1188 auto cx = outer->getBounds().fLeft + outerR;
1189 auto cy = outer->getBounds().fTop + outerR;
1190 if (SkScalarNearlyEqual(cx, inner->getBounds().fLeft + innerR) &&
1191 SkScalarNearlyEqual(cy, inner->getBounds().fTop + innerR)) {
1192 auto avgR = (innerR + outerR) / 2.f;
1193 auto circleBounds = SkRect::MakeLTRB(cx - avgR, cy - avgR, cx + avgR, cy + avgR);
1194 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
1195 stroke.setStrokeStyle(outerR - innerR);
1196 auto op = GrOvalOpFactory::MakeOvalOp(std::move(paint), viewMatrix, circleBounds,
1197 stroke, this->caps()->shaderCaps());
1198 if (op) {
1199 this->addDrawOp(clip, std::move(op));
1200 return true;
1201 }
1202 }
1203 }
1204
Ethan Nicholas0f3c7322017-11-09 14:51:17 -05001205 GrClipEdgeType innerEdgeType, outerEdgeType;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001206 if (GrAAType::kCoverage == aaType) {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001207 innerEdgeType = GrClipEdgeType::kInverseFillAA;
1208 outerEdgeType = GrClipEdgeType::kFillAA;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001209 } else {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001210 innerEdgeType = GrClipEdgeType::kInverseFillBW;
1211 outerEdgeType = GrClipEdgeType::kFillBW;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001212 }
robertphillips00095892016-02-29 13:50:40 -08001213
robertphillips00095892016-02-29 13:50:40 -08001214 SkMatrix inverseVM;
1215 if (!viewMatrix.isIdentity()) {
1216 if (!origInner.transform(viewMatrix, inner.writable())) {
1217 return false;
1218 }
1219 if (!origOuter.transform(viewMatrix, outer.writable())) {
1220 return false;
1221 }
1222 if (!viewMatrix.invert(&inverseVM)) {
1223 return false;
1224 }
1225 } else {
1226 inverseVM.reset();
halcanary9d524f22016-03-29 09:03:52 -07001227 }
robertphillips00095892016-02-29 13:50:40 -08001228
Brian Salomon14471772017-12-05 10:35:15 -05001229 const auto& caps = *this->caps()->shaderCaps();
robertphillips00095892016-02-29 13:50:40 -08001230 // TODO these need to be a geometry processors
Brian Salomon14471772017-12-05 10:35:15 -05001231 auto innerEffect = GrRRectEffect::Make(innerEdgeType, *inner, caps);
robertphillips00095892016-02-29 13:50:40 -08001232 if (!innerEffect) {
1233 return false;
1234 }
1235
Brian Salomon14471772017-12-05 10:35:15 -05001236 auto outerEffect = GrRRectEffect::Make(outerEdgeType, *outer, caps);
robertphillips00095892016-02-29 13:50:40 -08001237 if (!outerEffect) {
1238 return false;
1239 }
1240
Brian Salomon82f44312017-01-11 13:42:54 -05001241 paint.addCoverageFragmentProcessor(std::move(innerEffect));
1242 paint.addCoverageFragmentProcessor(std::move(outerEffect));
robertphillips00095892016-02-29 13:50:40 -08001243
1244 SkRect bounds = outer->getBounds();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001245 if (GrAAType::kCoverage == aaType) {
robertphillips00095892016-02-29 13:50:40 -08001246 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1247 }
halcanary9d524f22016-03-29 09:03:52 -07001248
Brian Salomon82f44312017-01-11 13:42:54 -05001249 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), bounds,
1250 inverseVM);
robertphillips00095892016-02-29 13:50:40 -08001251 return true;
1252}
1253
Brian Osman11052242016-10-27 14:47:55 -04001254void GrRenderTargetContext::drawDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001255 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001256 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001257 const SkMatrix& viewMatrix,
1258 const SkRRect& outer,
1259 const SkRRect& inner) {
robertphillips00095892016-02-29 13:50:40 -08001260 ASSERT_SINGLE_OWNER
1261 RETURN_IF_ABANDONED
1262 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001263 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawDRRect", fContext);
robertphillips00095892016-02-29 13:50:40 -08001264
1265 SkASSERT(!outer.isEmpty());
1266 SkASSERT(!inner.isEmpty());
1267
Robert Phillips72152832017-01-25 17:31:35 -05001268 AutoCheckFlush acf(this->drawingManager());
robertphillips00095892016-02-29 13:50:40 -08001269
Brian Salomon82f44312017-01-11 13:42:54 -05001270 if (this->drawFilledDRRect(clip, std::move(paint), aa, viewMatrix, outer, inner)) {
robertphillips00095892016-02-29 13:50:40 -08001271 return;
1272 }
1273
1274 SkPath path;
1275 path.setIsVolatile(true);
1276 path.addRRect(inner);
1277 path.addRRect(outer);
1278 path.setFillType(SkPath::kEvenOdd_FillType);
1279
Brian Salomon82f44312017-01-11 13:42:54 -05001280 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, GrStyle::SimpleFill());
robertphillips00095892016-02-29 13:50:40 -08001281}
1282
robertphillipsea461502015-05-26 11:38:03 -07001283///////////////////////////////////////////////////////////////////////////////
1284
Brian Osman11052242016-10-27 14:47:55 -04001285void GrRenderTargetContext::drawRegion(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001286 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001287 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001288 const SkMatrix& viewMatrix,
1289 const SkRegion& region,
Stan Iliev73d8fd92017-08-02 15:36:24 -04001290 const GrStyle& style,
1291 const GrUserStencilSettings* ss) {
msarettcc319b92016-08-25 18:07:18 -07001292 ASSERT_SINGLE_OWNER
1293 RETURN_IF_ABANDONED
1294 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001295 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRegion", fContext);
msarettcc319b92016-08-25 18:07:18 -07001296
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001297 if (GrAA::kYes == aa) {
Brian Salomonfc527d22016-12-14 21:07:01 -05001298 // GrRegionOp performs no antialiasing but is much faster, so here we check the matrix
Brian Salomonc57c7c92016-12-06 14:47:34 -05001299 // to see whether aa is really required.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001300 if (!SkToBool(viewMatrix.getType() & ~(SkMatrix::kTranslate_Mask)) &&
Brian Salomon34169692017-08-28 15:32:01 -04001301 SkScalarIsInt(viewMatrix.getTranslateX()) &&
1302 SkScalarIsInt(viewMatrix.getTranslateY())) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001303 aa = GrAA::kNo;
1304 }
Brian Salomonc57c7c92016-12-06 14:47:34 -05001305 }
msarettcc319b92016-08-25 18:07:18 -07001306 bool complexStyle = !style.isSimpleFill();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001307 if (complexStyle || GrAA::kYes == aa) {
msarettcc319b92016-08-25 18:07:18 -07001308 SkPath path;
1309 region.getBoundaryPath(&path);
Brian Salomon82f44312017-01-11 13:42:54 -05001310 return this->drawPath(clip, std::move(paint), aa, viewMatrix, path, style);
msarettcc319b92016-08-25 18:07:18 -07001311 }
1312
Brian Salomonf0366322017-07-11 15:53:05 -04001313 GrAAType aaType = this->chooseAAType(GrAA::kNo, GrAllowMixedSamples::kNo);
Stan Iliev73d8fd92017-08-02 15:36:24 -04001314 std::unique_ptr<GrDrawOp> op = GrRegionOp::Make(std::move(paint), viewMatrix, region, aaType,
1315 ss);
Brian Salomonf0366322017-07-11 15:53:05 -04001316 this->addDrawOp(clip, std::move(op));
msarettcc319b92016-08-25 18:07:18 -07001317}
1318
Brian Osman11052242016-10-27 14:47:55 -04001319void GrRenderTargetContext::drawOval(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001320 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001321 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001322 const SkMatrix& viewMatrix,
1323 const SkRect& oval,
1324 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001325 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001326 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001327 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001328 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawOval", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -07001329
robertphillipsea461502015-05-26 11:38:03 -07001330 if (oval.isEmpty()) {
1331 return;
1332 }
1333
bsalomon6663acf2016-05-10 09:14:17 -07001334 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
robertphillipsea461502015-05-26 11:38:03 -07001335
Robert Phillips72152832017-01-25 17:31:35 -05001336 AutoCheckFlush acf(this->drawingManager());
bsalomon6663acf2016-05-10 09:14:17 -07001337 const SkStrokeRec& stroke = style.strokeRec();
csmartdaltona7f29642016-07-07 08:49:11 -07001338
Brian Salomon7c8460e2017-05-12 11:36:10 -04001339 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001340 if (GrAAType::kCoverage == aaType) {
Brian Salomon94efbf52016-11-29 13:43:05 -05001341 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomon05441c42017-05-15 16:45:49 -04001342 std::unique_ptr<GrDrawOp> op =
1343 GrOvalOpFactory::MakeOvalOp(std::move(paint), viewMatrix, oval, stroke, shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -05001344 if (op) {
Brian Salomon05441c42017-05-15 16:45:49 -04001345 this->addDrawOp(clip, std::move(op));
robertphillipsb56f9272016-02-25 11:03:52 -08001346 return;
1347 }
robertphillipsea461502015-05-26 11:38:03 -07001348 }
robertphillipsb56f9272016-02-25 11:03:52 -08001349
1350 SkPath path;
1351 path.setIsVolatile(true);
1352 path.addOval(oval);
Brian Salomon82f44312017-01-11 13:42:54 -05001353 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, style);
robertphillipsea461502015-05-26 11:38:03 -07001354}
1355
Brian Osman11052242016-10-27 14:47:55 -04001356void GrRenderTargetContext::drawArc(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001357 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001358 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001359 const SkMatrix& viewMatrix,
1360 const SkRect& oval,
1361 SkScalar startAngle,
1362 SkScalar sweepAngle,
1363 bool useCenter,
1364 const GrStyle& style) {
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001365 ASSERT_SINGLE_OWNER
1366 RETURN_IF_ABANDONED
1367 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001368 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawArc", fContext);
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001369
1370 AutoCheckFlush acf(this->drawingManager());
1371
Brian Salomon7c8460e2017-05-12 11:36:10 -04001372 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001373 if (GrAAType::kCoverage == aaType) {
Brian Salomon94efbf52016-11-29 13:43:05 -05001374 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomon05441c42017-05-15 16:45:49 -04001375 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeArcOp(std::move(paint),
1376 viewMatrix,
1377 oval,
1378 startAngle,
1379 sweepAngle,
1380 useCenter,
1381 style,
1382 shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -05001383 if (op) {
Brian Salomon05441c42017-05-15 16:45:49 -04001384 this->addDrawOp(clip, std::move(op));
bsalomon4f3a0ca2016-08-22 13:14:26 -07001385 return;
1386 }
1387 }
1388 SkPath path;
bsalomon21af9ca2016-08-25 12:29:23 -07001389 SkPathPriv::CreateDrawArcPath(&path, oval, startAngle, sweepAngle, useCenter,
1390 style.isSimpleFill());
Brian Salomon82f44312017-01-11 13:42:54 -05001391 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, style);
bsalomon4f3a0ca2016-08-22 13:14:26 -07001392}
1393
Brian Osman11052242016-10-27 14:47:55 -04001394void GrRenderTargetContext::drawImageLattice(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001395 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001396 const SkMatrix& viewMatrix,
1397 int imageWidth,
1398 int imageHeight,
1399 std::unique_ptr<SkLatticeIter> iter,
1400 const SkRect& dst) {
joshualitt1de610a2016-01-06 08:26:09 -08001401 ASSERT_SINGLE_OWNER
joshualitt33a5fce2015-11-18 13:28:51 -08001402 RETURN_IF_ABANDONED
1403 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001404 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawImageLattice", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001405
Robert Phillips72152832017-01-25 17:31:35 -05001406 AutoCheckFlush acf(this->drawingManager());
joshualitt33a5fce2015-11-18 13:28:51 -08001407
Brian Salomon815486c2017-07-11 08:52:13 -04001408 std::unique_ptr<GrDrawOp> op = GrLatticeOp::MakeNonAA(std::move(paint), viewMatrix, imageWidth,
1409 imageHeight, std::move(iter), dst);
1410 this->addDrawOp(clip, std::move(op));
joshualitt33a5fce2015-11-18 13:28:51 -08001411}
1412
Greg Daniel51316782017-08-02 15:10:09 +00001413GrSemaphoresSubmitted GrRenderTargetContext::prepareForExternalIO(
1414 int numSemaphores, GrBackendSemaphore backendSemaphores[]) {
robertphillips8c523e02016-07-26 07:41:00 -07001415 ASSERT_SINGLE_OWNER
Greg Daniel51316782017-08-02 15:10:09 +00001416 if (this->drawingManager()->wasAbandoned()) { return GrSemaphoresSubmitted::kNo; }
robertphillips8c523e02016-07-26 07:41:00 -07001417 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001418 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "prepareForExternalIO", fContext);
robertphillips8c523e02016-07-26 07:41:00 -07001419
Greg Daniel51316782017-08-02 15:10:09 +00001420 return this->drawingManager()->prepareSurfaceForExternalIO(fRenderTargetProxy.get(),
1421 numSemaphores,
1422 backendSemaphores);
Greg Daniela5cb7812017-06-16 09:45:32 -04001423}
1424
Greg Danielc64ee462017-06-15 16:59:49 -04001425bool GrRenderTargetContext::waitOnSemaphores(int numSemaphores,
Greg Daniela5cb7812017-06-16 09:45:32 -04001426 const GrBackendSemaphore* waitSemaphores) {
1427 ASSERT_SINGLE_OWNER
Greg Danielc64ee462017-06-15 16:59:49 -04001428 RETURN_FALSE_IF_ABANDONED
Greg Daniela5cb7812017-06-16 09:45:32 -04001429 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001430 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "waitOnSemaphores", fContext);
Greg Daniela5cb7812017-06-16 09:45:32 -04001431
1432 AutoCheckFlush acf(this->drawingManager());
1433
Greg Danielc64ee462017-06-15 16:59:49 -04001434 if (numSemaphores && !this->caps()->fenceSyncSupport()) {
1435 return false;
1436 }
1437
Greg Daniela5cb7812017-06-16 09:45:32 -04001438 SkTArray<sk_sp<GrSemaphore>> semaphores(numSemaphores);
1439 for (int i = 0; i < numSemaphores; ++i) {
1440 sk_sp<GrSemaphore> sema = fContext->resourceProvider()->wrapBackendSemaphore(
1441 waitSemaphores[i], kAdopt_GrWrapOwnership);
1442 std::unique_ptr<GrOp> waitOp(GrSemaphoreOp::MakeWait(sema, fRenderTargetProxy.get()));
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001443 this->getRTOpList()->addOp(std::move(waitOp), *this->caps());
Greg Daniela5cb7812017-06-16 09:45:32 -04001444 }
Greg Danielc64ee462017-06-15 16:59:49 -04001445 return true;
robertphillips8c523e02016-07-26 07:41:00 -07001446}
joshualitt33a5fce2015-11-18 13:28:51 -08001447
Robert Phillips65a88fa2017-08-08 08:36:22 -04001448void GrRenderTargetContext::insertEventMarker(const SkString& str) {
1449 std::unique_ptr<GrOp> op(GrDebugMarkerOp::Make(fRenderTargetProxy.get(), str));
1450 this->getRTOpList()->addOp(std::move(op), *this->caps());
1451}
1452
1453
robertphillipsea461502015-05-26 11:38:03 -07001454// Can 'path' be drawn as a pair of filled nested rectangles?
bsalomon6663acf2016-05-10 09:14:17 -07001455static bool fills_as_nested_rects(const SkMatrix& viewMatrix, const SkPath& path, SkRect rects[2]) {
robertphillipsea461502015-05-26 11:38:03 -07001456
1457 if (path.isInverseFillType()) {
1458 return false;
1459 }
1460
1461 // TODO: this restriction could be lifted if we were willing to apply
1462 // the matrix to all the points individually rather than just to the rect
robertphillips0e7029e2015-11-30 05:45:06 -08001463 if (!viewMatrix.rectStaysRect()) {
robertphillipsea461502015-05-26 11:38:03 -07001464 return false;
1465 }
1466
1467 SkPath::Direction dirs[2];
1468 if (!path.isNestedFillRects(rects, dirs)) {
1469 return false;
1470 }
1471
1472 if (SkPath::kWinding_FillType == path.getFillType() && dirs[0] == dirs[1]) {
1473 // The two rects need to be wound opposite to each other
1474 return false;
1475 }
1476
1477 // Right now, nested rects where the margin is not the same width
1478 // all around do not render correctly
1479 const SkScalar* outer = rects[0].asScalars();
1480 const SkScalar* inner = rects[1].asScalars();
1481
1482 bool allEq = true;
1483
1484 SkScalar margin = SkScalarAbs(outer[0] - inner[0]);
1485 bool allGoE1 = margin >= SK_Scalar1;
1486
1487 for (int i = 1; i < 4; ++i) {
1488 SkScalar temp = SkScalarAbs(outer[i] - inner[i]);
1489 if (temp < SK_Scalar1) {
1490 allGoE1 = false;
1491 }
1492 if (!SkScalarNearlyEqual(margin, temp)) {
1493 allEq = false;
1494 }
1495 }
1496
1497 return allEq || allGoE1;
1498}
1499
Brian Osman11052242016-10-27 14:47:55 -04001500void GrRenderTargetContext::drawPath(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001501 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001502 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001503 const SkMatrix& viewMatrix,
1504 const SkPath& path,
1505 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001506 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001507 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001508 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001509 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawPath", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -07001510
robertphillipsea461502015-05-26 11:38:03 -07001511 if (path.isEmpty()) {
1512 if (path.isInverseFillType()) {
Brian Salomon82f44312017-01-11 13:42:54 -05001513 this->drawPaint(clip, std::move(paint), viewMatrix);
robertphillipsea461502015-05-26 11:38:03 -07001514 }
1515 return;
1516 }
1517
Robert Phillips72152832017-01-25 17:31:35 -05001518 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -07001519
Brian Salomon7c8460e2017-05-12 11:36:10 -04001520 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001521 if (GrAAType::kCoverage == aaType && !style.pathEffect()) {
bsalomon6663acf2016-05-10 09:14:17 -07001522 if (style.isSimpleFill() && !path.isConvex()) {
robertphillipsea461502015-05-26 11:38:03 -07001523 // Concave AA paths are expensive - try to avoid them for special cases
1524 SkRect rects[2];
1525
bsalomon6663acf2016-05-10 09:14:17 -07001526 if (fills_as_nested_rects(viewMatrix, path, rects)) {
Brian Salomonbaaf4392017-06-15 09:59:23 -04001527 std::unique_ptr<GrDrawOp> op =
1528 GrRectOpFactory::MakeAAFillNestedRects(std::move(paint), viewMatrix, rects);
Brian Salomon5f970fe2017-06-16 17:30:59 -04001529 if (op) {
1530 this->addDrawOp(clip, std::move(op));
bsalomon40ef4852016-05-02 13:22:13 -07001531 }
Brian Salomon5f970fe2017-06-16 17:30:59 -04001532 // A null return indicates that there is nothing to draw in this case.
1533 return;
robertphillipsea461502015-05-26 11:38:03 -07001534 }
1535 }
1536 SkRect ovalRect;
1537 bool isOval = path.isOval(&ovalRect);
1538
1539 if (isOval && !path.isInverseFillType()) {
Brian Salomon94efbf52016-11-29 13:43:05 -05001540 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomon05441c42017-05-15 16:45:49 -04001541 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeOvalOp(
1542 std::move(paint), viewMatrix, ovalRect, style.strokeRec(), shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -05001543 if (op) {
Brian Salomon05441c42017-05-15 16:45:49 -04001544 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -07001545 return;
1546 }
1547 }
1548 }
robertphillips4bc31812016-03-01 12:22:49 -08001549
1550 // Note that internalDrawPath may sw-rasterize the path into a scratch texture.
1551 // Scratch textures can be recycled after they are returned to the texture
1552 // cache. This presents a potential hazard for buffered drawing. However,
1553 // the writePixels that uploads to the scratch will perform a flush so we're
1554 // OK.
Brian Salomon82f44312017-01-11 13:42:54 -05001555 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, style);
robertphillipsea461502015-05-26 11:38:03 -07001556}
1557
Chris Daltonbbfd5162017-11-07 13:35:22 -07001558bool GrRenderTargetContextPriv::drawAndStencilPath(const GrHardClip& clip,
Brian Osman11052242016-10-27 14:47:55 -04001559 const GrUserStencilSettings* ss,
1560 SkRegion::Op op,
1561 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001562 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001563 const SkMatrix& viewMatrix,
1564 const SkPath& path) {
robertphillips391395d2016-03-02 09:26:36 -08001565 ASSERT_SINGLE_OWNER_PRIV
1566 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -04001567 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001568 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawAndStencilPath",
1569 fRenderTargetContext->fContext);
robertphillips391395d2016-03-02 09:26:36 -08001570
1571 if (path.isEmpty() && path.isInverseFillType()) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001572 this->drawAndStencilRect(clip, ss, op, invert, GrAA::kNo, SkMatrix::I(),
Brian Osman11052242016-10-27 14:47:55 -04001573 SkRect::MakeIWH(fRenderTargetContext->width(),
1574 fRenderTargetContext->height()));
robertphillips391395d2016-03-02 09:26:36 -08001575 return true;
1576 }
1577
Robert Phillips72152832017-01-25 17:31:35 -05001578 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -08001579
1580 // An Assumption here is that path renderer would use some form of tweaking
1581 // the src color (either the input alpha or in the frag shader) to implement
1582 // aa. If we have some future driver-mojo path AA that can do the right
1583 // thing WRT to the blend then we'll need some query on the PR.
Brian Salomon7c8460e2017-05-12 11:36:10 -04001584 GrAAType aaType = fRenderTargetContext->chooseAAType(aa, GrAllowMixedSamples::kNo);
robertphillips976f5f02016-06-03 10:59:20 -07001585 bool hasUserStencilSettings = !ss->isUnused();
robertphillips391395d2016-03-02 09:26:36 -08001586
Chris Daltondb91c6e2017-09-08 16:25:08 -06001587 SkIRect clipConservativeBounds;
1588 clip.getConservativeBounds(fRenderTargetContext->width(), fRenderTargetContext->height(),
1589 &clipConservativeBounds, nullptr);
1590
bsalomon8acedde2016-06-24 10:42:16 -07001591 GrShape shape(path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -08001592 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Eric Karl5c779752017-05-08 12:02:07 -07001593 canDrawArgs.fCaps = fRenderTargetContext->drawingManager()->getContext()->caps();
robertphillips391395d2016-03-02 09:26:36 -08001594 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07001595 canDrawArgs.fShape = &shape;
Chris Daltondb91c6e2017-09-08 16:25:08 -06001596 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001597 canDrawArgs.fAAType = aaType;
cdalton93a379b2016-05-11 13:58:08 -07001598 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips391395d2016-03-02 09:26:36 -08001599
1600 // Don't allow the SW renderer
Robert Phillips72152832017-01-25 17:31:35 -05001601 GrPathRenderer* pr = fRenderTargetContext->drawingManager()->getPathRenderer(
Brian Salomon36aa1762016-12-10 13:24:02 -05001602 canDrawArgs, false, GrPathRendererChain::DrawType::kStencilAndColor);
robertphillips391395d2016-03-02 09:26:36 -08001603 if (!pr) {
1604 return false;
1605 }
1606
1607 GrPaint paint;
1608 paint.setCoverageSetOpXPFactory(op, invert);
1609
Brian Salomonf3569f02017-10-24 12:52:33 -04001610 GrPathRenderer::DrawPathArgs args{fRenderTargetContext->drawingManager()->getContext(),
1611 std::move(paint),
1612 ss,
1613 fRenderTargetContext,
1614 &clip,
1615 &clipConservativeBounds,
1616 &viewMatrix,
1617 &shape,
1618 aaType,
1619 fRenderTargetContext->colorSpaceInfo().isGammaCorrect()};
robertphillips391395d2016-03-02 09:26:36 -08001620 pr->drawPath(args);
1621 return true;
1622}
1623
Brian Osman11052242016-10-27 14:47:55 -04001624SkBudgeted GrRenderTargetContextPriv::isBudgeted() const {
robertphillips714712b2016-08-04 06:20:45 -07001625 ASSERT_SINGLE_OWNER_PRIV
1626
Brian Osman11052242016-10-27 14:47:55 -04001627 if (fRenderTargetContext->wasAbandoned()) {
robertphillips714712b2016-08-04 06:20:45 -07001628 return SkBudgeted::kNo;
1629 }
1630
Brian Osman11052242016-10-27 14:47:55 -04001631 SkDEBUGCODE(fRenderTargetContext->validate();)
robertphillips714712b2016-08-04 06:20:45 -07001632
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001633 return fRenderTargetContext->fRenderTargetProxy->isBudgeted();
robertphillips714712b2016-08-04 06:20:45 -07001634}
1635
Brian Osman11052242016-10-27 14:47:55 -04001636void GrRenderTargetContext::internalDrawPath(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001637 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001638 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001639 const SkMatrix& viewMatrix,
1640 const SkPath& path,
1641 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001642 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001643 RETURN_IF_ABANDONED
Brian Salomondcbb9d92017-07-19 10:53:20 -04001644 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "internalDrawPath", fContext);
1645
Chris Daltondb91c6e2017-09-08 16:25:08 -06001646 SkIRect clipConservativeBounds;
1647 clip.getConservativeBounds(this->width(), this->height(), &clipConservativeBounds, nullptr);
1648
bsalomon8acedde2016-06-24 10:42:16 -07001649 SkASSERT(!path.isEmpty());
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001650 GrShape shape;
Brian Salomon7c8460e2017-05-12 11:36:10 -04001651 // NVPR cannot handle hairlines, so this would get picked up by a different stencil and
1652 // cover path renderer (i.e. default path renderer). The hairline renderer produces much
1653 // smoother hairlines than MSAA.
1654 GrAllowMixedSamples allowMixedSamples =
1655 style.isSimpleHairline() ? GrAllowMixedSamples::kNo : GrAllowMixedSamples::kYes;
1656 GrAAType aaType = this->chooseAAType(aa, allowMixedSamples);
robertphillips68737822015-10-29 12:12:21 -07001657 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Eric Karl5c779752017-05-08 12:02:07 -07001658 canDrawArgs.fCaps = this->drawingManager()->getContext()->caps();
robertphillips68737822015-10-29 12:12:21 -07001659 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07001660 canDrawArgs.fShape = &shape;
Chris Daltondb91c6e2017-09-08 16:25:08 -06001661 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001662 canDrawArgs.fHasUserStencilSettings = false;
robertphillips68737822015-10-29 12:12:21 -07001663
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001664 GrPathRenderer* pr;
Brian Salomon82125e92016-12-10 09:35:48 -05001665 static constexpr GrPathRendererChain::DrawType kType = GrPathRendererChain::DrawType::kColor;
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05001666 shape = GrShape(path, style);
1667 if (shape.isEmpty() && !shape.inverseFilled()) {
1668 return;
1669 }
1670
1671 canDrawArgs.fAAType = aaType;
1672
1673 // Try a 1st time without applying any of the style to the geometry (and barring sw)
1674 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
1675 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
1676
1677 if (!pr && shape.style().pathEffect()) {
1678 // It didn't work above, so try again with the path effect applied.
1679 shape = shape.applyStyle(GrStyle::Apply::kPathEffectOnly, styleScale);
1680 if (shape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001681 return;
1682 }
Robert Phillips72152832017-01-25 17:31:35 -05001683 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05001684 }
1685 if (!pr) {
1686 if (shape.style().applies()) {
1687 shape = shape.applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec, styleScale);
bsalomon8acedde2016-06-24 10:42:16 -07001688 if (shape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001689 return;
1690 }
bsalomon6663acf2016-05-10 09:14:17 -07001691 }
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05001692 // This time, allow SW renderer
1693 pr = this->drawingManager()->getPathRenderer(canDrawArgs, true, kType);
1694 }
robertphillipsea461502015-05-26 11:38:03 -07001695
bsalomon8acedde2016-06-24 10:42:16 -07001696 if (!pr) {
robertphillipsea461502015-05-26 11:38:03 -07001697#ifdef SK_DEBUG
1698 SkDebugf("Unable to find path renderer compatible with path.\n");
1699#endif
1700 return;
1701 }
1702
Robert Phillips256c37b2017-03-01 14:32:46 -05001703 GrPathRenderer::DrawPathArgs args{this->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05001704 std::move(paint),
1705 &GrUserStencilSettings::kUnused,
1706 this,
1707 &clip,
Chris Daltondb91c6e2017-09-08 16:25:08 -06001708 &clipConservativeBounds,
Brian Salomon82f44312017-01-11 13:42:54 -05001709 &viewMatrix,
1710 &shape,
1711 aaType,
Brian Salomonf3569f02017-10-24 12:52:33 -04001712 this->colorSpaceInfo().isGammaCorrect()};
bsalomon0aff2fa2015-07-31 06:48:27 -07001713 pr->drawPath(args);
robertphillipsea461502015-05-26 11:38:03 -07001714}
1715
Brian Salomon467921e2017-03-06 16:17:12 -05001716static void op_bounds(SkRect* bounds, const GrOp* op) {
1717 *bounds = op->bounds();
1718 if (op->hasZeroArea()) {
1719 if (op->hasAABloat()) {
1720 bounds->outset(0.5f, 0.5f);
1721 } else {
1722 // We don't know which way the particular GPU will snap lines or points at integer
1723 // coords. So we ensure that the bounds is large enough for either snap.
1724 SkRect before = *bounds;
1725 bounds->roundOut(bounds);
1726 if (bounds->fLeft == before.fLeft) {
1727 bounds->fLeft -= 1;
1728 }
1729 if (bounds->fTop == before.fTop) {
1730 bounds->fTop -= 1;
1731 }
1732 if (bounds->fRight == before.fRight) {
1733 bounds->fRight += 1;
1734 }
1735 if (bounds->fBottom == before.fBottom) {
1736 bounds->fBottom += 1;
1737 }
1738 }
1739 }
1740}
1741
Brian Salomon54d212e2017-03-21 14:22:38 -04001742uint32_t GrRenderTargetContext::addDrawOp(const GrClip& clip, std::unique_ptr<GrDrawOp> op) {
joshualitt1de610a2016-01-06 08:26:09 -08001743 ASSERT_SINGLE_OWNER
Robert Phillipsc0138922017-03-08 11:50:55 -05001744 if (this->drawingManager()->wasAbandoned()) {
1745 return SK_InvalidUniqueID;
1746 }
robertphillips2e1e51f2015-10-15 08:01:48 -07001747 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001748 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "addDrawOp", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -07001749
Brian Salomon467921e2017-03-06 16:17:12 -05001750 // Setup clip
1751 SkRect bounds;
1752 op_bounds(&bounds, op.get());
Brian Salomon97180af2017-03-14 13:42:58 -04001753 GrAppliedClip appliedClip;
Brian Salomon54d212e2017-03-21 14:22:38 -04001754 GrDrawOp::FixedFunctionFlags fixedFunctionFlags = op->fixedFunctionFlags();
1755 if (!clip.apply(fContext, this, fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesHWAA,
1756 fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil, &appliedClip,
1757 &bounds)) {
1758 return SK_InvalidUniqueID;
1759 }
1760
Brian Salomon54d212e2017-03-21 14:22:38 -04001761 if (fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil ||
1762 appliedClip.hasStencilClip()) {
Robert Phillips6b47c7d2017-08-29 07:24:09 -04001763 this->getOpList()->setStencilLoadOp(GrLoadOp::kClear);
Robert Phillips95214472017-08-08 18:00:03 -04001764
Robert Phillips65048132017-08-10 08:44:49 -04001765 this->setNeedsStencil();
Brian Salomon54d212e2017-03-21 14:22:38 -04001766 }
1767
Brian Salomonf3569f02017-10-24 12:52:33 -04001768 GrPixelConfigIsClamped dstIsClamped =
1769 GrGetPixelConfigIsClamped(this->colorSpaceInfo().config());
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001770 GrXferProcessor::DstProxy dstProxy;
Brian Osman9a725dd2017-09-20 09:53:22 -04001771 if (GrDrawOp::RequiresDstTexture::kYes == op->finalize(*this->caps(), &appliedClip,
1772 dstIsClamped)) {
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001773 if (!this->setupDstProxy(this->asRenderTargetProxy(), clip, op->bounds(), &dstProxy)) {
Brian Salomon54d212e2017-03-21 14:22:38 -04001774 return SK_InvalidUniqueID;
1775 }
1776 }
1777
1778 op->setClippedBounds(bounds);
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001779 return this->getRTOpList()->addOp(std::move(op), *this->caps(),
1780 std::move(appliedClip), dstProxy);
Brian Salomon54d212e2017-03-21 14:22:38 -04001781}
1782
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001783bool GrRenderTargetContext::setupDstProxy(GrRenderTargetProxy* rtProxy, const GrClip& clip,
Robert Phillips16d8ec62017-07-27 16:16:25 -04001784 const SkRect& opBounds,
1785 GrXferProcessor::DstProxy* dstProxy) {
Brian Salomon467921e2017-03-06 16:17:12 -05001786 if (this->caps()->textureBarrierSupport()) {
Robert Phillipsbf25d432017-04-07 10:08:53 -04001787 if (GrTextureProxy* texProxy = rtProxy->asTextureProxy()) {
Brian Salomon467921e2017-03-06 16:17:12 -05001788 // The render target is a texture, so we can read from it directly in the shader. The XP
1789 // will be responsible to detect this situation and request a texture barrier.
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001790 dstProxy->setProxy(sk_ref_sp(texProxy));
1791 dstProxy->setOffset(0, 0);
Robert Phillipsbf25d432017-04-07 10:08:53 -04001792 return true;
Brian Salomon467921e2017-03-06 16:17:12 -05001793 }
1794 }
1795
Robert Phillipsbf25d432017-04-07 10:08:53 -04001796 SkIRect copyRect = SkIRect::MakeWH(rtProxy->width(), rtProxy->height());
Brian Salomon467921e2017-03-06 16:17:12 -05001797
Eric Karl74480882017-04-03 14:49:05 -07001798 SkIRect clippedRect;
Robert Phillipsbf25d432017-04-07 10:08:53 -04001799 clip.getConservativeBounds(rtProxy->width(), rtProxy->height(), &clippedRect);
Eric Karl72e551e2017-04-04 13:42:10 -07001800 SkIRect drawIBounds;
Brian Salomon467921e2017-03-06 16:17:12 -05001801 opBounds.roundOut(&drawIBounds);
Brian Salomon859621f2017-03-16 09:21:54 -04001802 // Cover up for any precision issues by outsetting the op bounds a pixel in each direction.
1803 drawIBounds.outset(1, 1);
Eric Karl72e551e2017-04-04 13:42:10 -07001804 if (!clippedRect.intersect(drawIBounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -05001805#ifdef SK_DEBUG
Robert Phillipsbf25d432017-04-07 10:08:53 -04001806 GrCapsDebugf(this->caps(), "setupDstTexture: Missed an early reject bailing on draw.");
Brian Salomon467921e2017-03-06 16:17:12 -05001807#endif
Robert Phillipsbf25d432017-04-07 10:08:53 -04001808 return false;
Brian Salomon467921e2017-03-06 16:17:12 -05001809 }
1810
1811 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
1812 // have per-sample dst values by making the copy multisampled.
1813 GrSurfaceDesc desc;
Eric Karl74480882017-04-03 14:49:05 -07001814 bool rectsMustMatch = false;
1815 bool disallowSubrect = false;
Robert Phillipsbf25d432017-04-07 10:08:53 -04001816 if (!this->caps()->initDescForDstCopy(rtProxy, &desc, &rectsMustMatch, &disallowSubrect)) {
Brian Salomon467921e2017-03-06 16:17:12 -05001817 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillips16d8ec62017-07-27 16:16:25 -04001818 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
Robert Phillipsbf25d432017-04-07 10:08:53 -04001819 desc.fConfig = rtProxy->config();
Brian Salomon467921e2017-03-06 16:17:12 -05001820 }
1821
Eric Karl74480882017-04-03 14:49:05 -07001822 if (!disallowSubrect) {
1823 copyRect = clippedRect;
1824 }
Brian Salomon467921e2017-03-06 16:17:12 -05001825
Robert Phillipsbf25d432017-04-07 10:08:53 -04001826 SkIPoint dstPoint, dstOffset;
1827 SkBackingFit fit;
Eric Karl74480882017-04-03 14:49:05 -07001828 if (rectsMustMatch) {
Robert Phillipsbf25d432017-04-07 10:08:53 -04001829 SkASSERT(desc.fOrigin == rtProxy->origin());
1830 desc.fWidth = rtProxy->width();
1831 desc.fHeight = rtProxy->height();
Eric Karl74480882017-04-03 14:49:05 -07001832 dstPoint = {copyRect.fLeft, copyRect.fTop};
1833 dstOffset = {0, 0};
Robert Phillipsbf25d432017-04-07 10:08:53 -04001834 fit = SkBackingFit::kExact;
Eric Karl74480882017-04-03 14:49:05 -07001835 } else {
1836 desc.fWidth = copyRect.width();
1837 desc.fHeight = copyRect.height();
1838 dstPoint = {0, 0};
1839 dstOffset = {copyRect.fLeft, copyRect.fTop};
Robert Phillipsbf25d432017-04-07 10:08:53 -04001840 fit = SkBackingFit::kApprox;
Eric Karl74480882017-04-03 14:49:05 -07001841 }
Brian Salomon467921e2017-03-06 16:17:12 -05001842
Robert Phillipsbf25d432017-04-07 10:08:53 -04001843 sk_sp<GrSurfaceContext> sContext = fContext->contextPriv().makeDeferredSurfaceContext(
1844 desc,
Greg Daniel65c7f662017-10-30 13:39:09 -04001845 GrMipMapped::kNo,
Robert Phillipsbf25d432017-04-07 10:08:53 -04001846 fit,
1847 SkBudgeted::kYes);
1848 if (!sContext) {
1849 SkDebugf("setupDstTexture: surfaceContext creation failed.\n");
1850 return false;
1851 }
1852
1853 if (!sContext->copy(rtProxy, copyRect, dstPoint)) {
1854 SkDebugf("setupDstTexture: copy failed.\n");
1855 return false;
1856 }
1857
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001858 dstProxy->setProxy(sContext->asTextureProxyRef());
1859 dstProxy->setOffset(dstOffset);
Robert Phillipsbf25d432017-04-07 10:08:53 -04001860 return true;
robertphillips2334fb62015-06-17 05:43:33 -07001861}