blob: fa1e0b3d00e31522fb5e4e17afdbf55e9523d58d [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"
Brian Salomon5ec9def2016-12-20 15:34:05 -050012#include "GrColor.h"
Robert Phillipse78b7252017-04-06 07:59:41 -040013#include "GrContextPriv.h"
robertphillips77a2e522015-10-17 07:43:27 -070014#include "GrDrawingManager.h"
csmartdalton02fa32c2016-08-19 13:29:27 -070015#include "GrFixedClip.h"
robertphillips714712b2016-08-04 06:20:45 -070016#include "GrGpuResourcePriv.h"
robertphillipsea461502015-05-26 11:38:03 -070017#include "GrPathRenderer.h"
robertphillips5fa7f302016-07-21 09:21:04 -070018#include "GrPipelineBuilder.h"
robertphillips2334fb62015-06-17 05:43:33 -070019#include "GrRenderTarget.h"
Brian Salomon5ec9def2016-12-20 15:34:05 -050020#include "GrRenderTargetContextPriv.h"
robertphillips2334fb62015-06-17 05:43:33 -070021#include "GrRenderTargetPriv.h"
bsalomon473addf2015-10-02 07:49:05 -070022#include "GrResourceProvider.h"
Brian Salomon467921e2017-03-06 16:17:12 -050023#include "GrStencilAttachment.h"
Jim Van Verth3af1af92017-05-18 15:06:54 -040024#include "SkDrawShadowRec.h"
Brian Salomon467921e2017-03-06 16:17:12 -050025#include "SkLatticeIter.h"
26#include "SkMatrixPriv.h"
robertphillips2d70dcb2015-10-06 07:38:23 -070027#include "SkSurfacePriv.h"
Brian Salomon467921e2017-03-06 16:17:12 -050028#include "effects/GrRRectEffect.h"
29#include "instanced/InstancedRendering.h"
Brian Salomon89527432016-12-16 09:52:16 -050030#include "ops/GrClearOp.h"
Robert Phillipsb9a02a12017-04-06 11:08:40 -040031#include "ops/GrClearStencilClipOp.h"
Robert Phillipse4407212017-04-13 10:13:16 -040032#include "ops/GrDiscardOp.h"
Brian Salomon0f353322017-05-03 20:58:59 +000033#include "ops/GrDrawAtlasOp.h"
Brian Salomon6d4b65e2017-05-03 17:06:09 -040034#include "ops/GrDrawOp.h"
Brian Salomon89527432016-12-16 09:52:16 -050035#include "ops/GrDrawVerticesOp.h"
36#include "ops/GrLatticeOp.h"
Brian Salomonac70f842017-05-08 10:43:33 -040037#include "ops/GrNonAAFillRectOp.h"
Brian Salomon89527432016-12-16 09:52:16 -050038#include "ops/GrOp.h"
39#include "ops/GrOvalOpFactory.h"
40#include "ops/GrRectOpFactory.h"
41#include "ops/GrRegionOp.h"
42#include "ops/GrShadowRRectOp.h"
Brian Salomon467921e2017-03-06 16:17:12 -050043#include "ops/GrStencilPathOp.h"
joshualitte8042922015-12-11 06:11:21 -080044#include "text/GrAtlasTextContext.h"
45#include "text/GrStencilAndCoverTextContext.h"
joshualittbc907352016-01-13 06:45:40 -080046
Robert Phillips72152832017-01-25 17:31:35 -050047#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this->drawingManager()->getContext())
joshualitt1de610a2016-01-06 08:26:09 -080048#define ASSERT_SINGLE_OWNER \
Robert Phillipsa90aa2b2017-04-10 08:19:26 -040049 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(this->singleOwner());)
robertphillips391395d2016-03-02 09:26:36 -080050#define ASSERT_SINGLE_OWNER_PRIV \
Robert Phillipsa90aa2b2017-04-10 08:19:26 -040051 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fRenderTargetContext->singleOwner());)
Robert Phillips72152832017-01-25 17:31:35 -050052#define RETURN_IF_ABANDONED if (this->drawingManager()->wasAbandoned()) { return; }
53#define RETURN_IF_ABANDONED_PRIV if (fRenderTargetContext->drawingManager()->wasAbandoned()) { return; }
54#define RETURN_FALSE_IF_ABANDONED if (this->drawingManager()->wasAbandoned()) { return false; }
55#define RETURN_FALSE_IF_ABANDONED_PRIV if (fRenderTargetContext->drawingManager()->wasAbandoned()) { return false; }
56#define RETURN_NULL_IF_ABANDONED if (this->drawingManager()->wasAbandoned()) { return nullptr; }
robertphillipsea461502015-05-26 11:38:03 -070057
58class AutoCheckFlush {
59public:
halcanary9d524f22016-03-29 09:03:52 -070060 AutoCheckFlush(GrDrawingManager* drawingManager) : fDrawingManager(drawingManager) {
robertphillips77a2e522015-10-17 07:43:27 -070061 SkASSERT(fDrawingManager);
62 }
bsalomonb77a9072016-09-07 10:02:04 -070063 ~AutoCheckFlush() { fDrawingManager->flushIfNecessary(); }
robertphillipsea461502015-05-26 11:38:03 -070064
65private:
robertphillips77a2e522015-10-17 07:43:27 -070066 GrDrawingManager* fDrawingManager;
robertphillipsea461502015-05-26 11:38:03 -070067};
68
Brian Osman11052242016-10-27 14:47:55 -040069bool GrRenderTargetContext::wasAbandoned() const {
Robert Phillips72152832017-01-25 17:31:35 -050070 return this->drawingManager()->wasAbandoned();
robertphillips7761d612016-05-16 09:14:53 -070071}
72
Robert Phillipsf2361d22016-10-25 14:20:06 -040073// In MDB mode the reffing of the 'getLastOpList' call's result allows in-progress
Brian Osman11052242016-10-27 14:47:55 -040074// GrOpLists to be picked up and added to by renderTargetContexts lower in the call
Robert Phillipsf2361d22016-10-25 14:20:06 -040075// stack. When this occurs with a closed GrOpList, a new one will be allocated
Brian Osman11052242016-10-27 14:47:55 -040076// when the renderTargetContext attempts to use it (via getOpList).
77GrRenderTargetContext::GrRenderTargetContext(GrContext* context,
78 GrDrawingManager* drawingMgr,
Robert Phillipsc7635fa2016-10-28 13:25:24 -040079 sk_sp<GrRenderTargetProxy> rtp,
Brian Osman11052242016-10-27 14:47:55 -040080 sk_sp<SkColorSpace> colorSpace,
81 const SkSurfaceProps* surfaceProps,
82 GrAuditTrail* auditTrail,
83 GrSingleOwner* singleOwner)
Robert Phillips72152832017-01-25 17:31:35 -050084 : GrSurfaceContext(context, drawingMgr, std::move(colorSpace), auditTrail, singleOwner)
Robert Phillipsc7635fa2016-10-28 13:25:24 -040085 , fRenderTargetProxy(std::move(rtp))
Robert Phillipsdc83b892017-04-13 12:23:54 -040086 , fOpList(sk_ref_sp(fRenderTargetProxy->getLastRenderTargetOpList()))
Robert Phillipsc7635fa2016-10-28 13:25:24 -040087 , fInstancedPipelineInfo(fRenderTargetProxy.get())
brianosman5a7ae7e2016-09-12 12:07:25 -070088 , fColorXformFromSRGB(nullptr)
Robert Phillips2c862492017-01-18 10:08:39 -050089 , fSurfaceProps(SkSurfacePropsCopyOrDefault(surfaceProps)) {
brianosman5a7ae7e2016-09-12 12:07:25 -070090 if (fColorSpace) {
91 // sRGB sources are very common (SkColor, etc...), so we cache that gamut transformation
Matt Sarett77a7a1b2017-02-07 13:56:11 -050092 auto srgbColorSpace = SkColorSpace::MakeSRGB();
msarettc71a9b72016-09-16 11:01:27 -070093 fColorXformFromSRGB = GrColorSpaceXform::Make(srgbColorSpace.get(), fColorSpace.get());
brianosman5a7ae7e2016-09-12 12:07:25 -070094 }
robertphillips2e1e51f2015-10-15 08:01:48 -070095 SkDEBUGCODE(this->validate();)
robertphillipsea461502015-05-26 11:38:03 -070096}
97
robertphillips2e1e51f2015-10-15 08:01:48 -070098#ifdef SK_DEBUG
Brian Osman11052242016-10-27 14:47:55 -040099void GrRenderTargetContext::validate() const {
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400100 SkASSERT(fRenderTargetProxy);
101 fRenderTargetProxy->validate(fContext);
robertphillipsa106c622015-10-16 09:07:06 -0700102
Robert Phillipsf2361d22016-10-25 14:20:06 -0400103 if (fOpList && !fOpList->isClosed()) {
Robert Phillipsdc83b892017-04-13 12:23:54 -0400104 SkASSERT(fRenderTargetProxy->getLastOpList() == fOpList.get());
robertphillipsa106c622015-10-16 09:07:06 -0700105 }
robertphillips2e1e51f2015-10-15 08:01:48 -0700106}
107#endif
108
Brian Osman11052242016-10-27 14:47:55 -0400109GrRenderTargetContext::~GrRenderTargetContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800110 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700111}
112
Robert Phillipsf200a902017-01-30 13:27:37 -0500113GrTextureProxy* GrRenderTargetContext::asTextureProxy() {
Robert Phillipseaa86252016-11-08 13:49:39 +0000114 return fRenderTargetProxy->asTextureProxy();
115}
116
Robert Phillipsf200a902017-01-30 13:27:37 -0500117sk_sp<GrTextureProxy> GrRenderTargetContext::asTextureProxyRef() {
118 return sk_ref_sp(fRenderTargetProxy->asTextureProxy());
119}
120
Brian Osman11052242016-10-27 14:47:55 -0400121GrRenderTargetOpList* GrRenderTargetContext::getOpList() {
joshualitt1de610a2016-01-06 08:26:09 -0800122 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700123 SkDEBUGCODE(this->validate();)
124
Robert Phillipsf2361d22016-10-25 14:20:06 -0400125 if (!fOpList || fOpList->isClosed()) {
Robert Phillipsb6deea82017-05-11 14:14:30 -0400126 fOpList = this->drawingManager()->newRTOpList(fRenderTargetProxy.get());
robertphillipsa106c622015-10-16 09:07:06 -0700127 }
128
Robert Phillipsdc83b892017-04-13 12:23:54 -0400129 return fOpList.get();
robertphillipsa106c622015-10-16 09:07:06 -0700130}
131
Robert Phillipsc84c0302017-05-08 15:35:11 -0400132// MDB TODO: move this (and GrTextContext::copy) to GrSurfaceContext?
Robert Phillipse2f7d182016-12-15 09:23:05 -0500133bool GrRenderTargetContext::onCopy(GrSurfaceProxy* srcProxy,
134 const SkIRect& srcRect,
135 const SkIPoint& dstPoint) {
joshualitt1de610a2016-01-06 08:26:09 -0800136 ASSERT_SINGLE_OWNER
bsalomonb8fea972016-02-16 07:34:17 -0800137 RETURN_FALSE_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700138 SkDEBUGCODE(this->validate();)
Robert Phillipsbf25d432017-04-07 10:08:53 -0400139 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::onCopy");
Robert Phillipse2f7d182016-12-15 09:23:05 -0500140
Robert Phillipsbf25d432017-04-07 10:08:53 -0400141 return this->getOpList()->copySurface(fContext->resourceProvider(),
Robert Phillips178ce3e2017-04-13 09:15:47 -0400142 this, srcProxy, srcRect, dstPoint);
robertphillipsea461502015-05-26 11:38:03 -0700143}
144
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500145void GrRenderTargetContext::drawText(const GrClip& clip, const SkPaint& skPaint,
Brian Salomon82f44312017-01-11 13:42:54 -0500146 const SkMatrix& viewMatrix, const char text[],
147 size_t byteLength, SkScalar x, SkScalar y,
148 const SkIRect& clipBounds) {
joshualitt1de610a2016-01-06 08:26:09 -0800149 ASSERT_SINGLE_OWNER
robertphillips2d70dcb2015-10-06 07:38:23 -0700150 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700151 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400152 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawText");
robertphillips2d70dcb2015-10-06 07:38:23 -0700153
Robert Phillips72152832017-01-25 17:31:35 -0500154 GrAtlasTextContext* atlasTextContext = this->drawingManager()->getAtlasTextContext();
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500155 atlasTextContext->drawText(fContext, this, clip, skPaint, viewMatrix, fSurfaceProps, text,
156 byteLength, x, y, clipBounds);
robertphillips2334fb62015-06-17 05:43:33 -0700157}
robertphillipscaef3452015-11-11 13:18:11 -0800158
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500159void GrRenderTargetContext::drawPosText(const GrClip& clip, const SkPaint& paint,
160 const SkMatrix& viewMatrix, const char text[],
161 size_t byteLength, const SkScalar pos[],
Brian Salomon82f44312017-01-11 13:42:54 -0500162 int scalarsPerPosition, const SkPoint& offset,
163 const SkIRect& clipBounds) {
joshualitt1de610a2016-01-06 08:26:09 -0800164 ASSERT_SINGLE_OWNER
robertphillips2d70dcb2015-10-06 07:38:23 -0700165 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700166 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400167 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawPosText");
robertphillips2d70dcb2015-10-06 07:38:23 -0700168
Robert Phillips72152832017-01-25 17:31:35 -0500169 GrAtlasTextContext* atlasTextContext = this->drawingManager()->getAtlasTextContext();
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500170 atlasTextContext->drawPosText(fContext, this, clip, paint, viewMatrix, fSurfaceProps, text,
171 byteLength, pos, scalarsPerPosition, offset, clipBounds);
robertphillips2334fb62015-06-17 05:43:33 -0700172}
robertphillipscaef3452015-11-11 13:18:11 -0800173
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500174void GrRenderTargetContext::drawTextBlob(const GrClip& clip, const SkPaint& paint,
Brian Osman11052242016-10-27 14:47:55 -0400175 const SkMatrix& viewMatrix, const SkTextBlob* blob,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500176 SkScalar x, SkScalar y, SkDrawFilter* filter,
177 const SkIRect& clipBounds) {
joshualitt1de610a2016-01-06 08:26:09 -0800178 ASSERT_SINGLE_OWNER
robertphillips2d70dcb2015-10-06 07:38:23 -0700179 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700180 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400181 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawTextBlob");
robertphillips2d70dcb2015-10-06 07:38:23 -0700182
Robert Phillips72152832017-01-25 17:31:35 -0500183 GrAtlasTextContext* atlasTextContext = this->drawingManager()->getAtlasTextContext();
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500184 atlasTextContext->drawTextBlob(fContext, this, clip, paint, viewMatrix, fSurfaceProps, blob, x,
185 y, filter, clipBounds);
robertphillipsea461502015-05-26 11:38:03 -0700186}
187
Brian Osman11052242016-10-27 14:47:55 -0400188void GrRenderTargetContext::discard() {
joshualitt1de610a2016-01-06 08:26:09 -0800189 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700190 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700191 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400192 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::discard");
robertphillips2e1e51f2015-10-15 08:01:48 -0700193
Robert Phillips72152832017-01-25 17:31:35 -0500194 AutoCheckFlush acf(this->drawingManager());
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400195
Robert Phillipse4407212017-04-13 10:13:16 -0400196 // Currently this just inserts a discard op. However, once in MDB this can remove all the
197 // previously recorded ops and change the load op to discard.
198 if (this->caps()->discardRenderTargetSupport()) {
Robert Phillips955235f2017-05-18 12:17:35 -0400199 std::unique_ptr<GrOp> op(GrDiscardOp::Make(fRenderTargetProxy.get()));
Robert Phillipse4407212017-04-13 10:13:16 -0400200 if (!op) {
201 return;
202 }
Robert Phillips33f83152017-05-18 20:57:43 +0000203 this->getOpList()->addOp(std::move(op), this);
Robert Phillipse4407212017-04-13 10:13:16 -0400204 }
robertphillipsea461502015-05-26 11:38:03 -0700205}
206
Brian Osman11052242016-10-27 14:47:55 -0400207void GrRenderTargetContext::clear(const SkIRect* rect,
208 const GrColor color,
209 bool canIgnoreRect) {
joshualitt1de610a2016-01-06 08:26:09 -0800210 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700211 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700212 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400213 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::clear");
robertphillipsea461502015-05-26 11:38:03 -0700214
Robert Phillips72152832017-01-25 17:31:35 -0500215 AutoCheckFlush acf(this->drawingManager());
csmartdalton29df7602016-08-31 11:55:52 -0700216 this->internalClear(rect ? GrFixedClip(*rect) : GrFixedClip::Disabled(), color, canIgnoreRect);
217}
robertphillips9199a9f2016-07-13 07:48:43 -0700218
Robert Phillips784b7bf2016-12-09 13:35:02 -0500219void GrRenderTargetContextPriv::absClear(const SkIRect* clearRect, const GrColor color) {
220 ASSERT_SINGLE_OWNER_PRIV
221 RETURN_IF_ABANDONED_PRIV
222 SkDEBUGCODE(fRenderTargetContext->validate();)
223 GR_AUDIT_TRAIL_AUTO_FRAME(fRenderTargetContext->fAuditTrail,
224 "GrRenderTargetContext::absClear");
225
Robert Phillips72152832017-01-25 17:31:35 -0500226 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500227
Brian Salomonbb5711a2017-05-17 13:49:59 -0400228 SkIRect rtRect = SkIRect::MakeWH(fRenderTargetContext->fRenderTargetProxy->worstCaseWidth(),
229 fRenderTargetContext->fRenderTargetProxy->worstCaseHeight());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500230
231 if (clearRect) {
232 if (clearRect->contains(rtRect)) {
233 clearRect = nullptr; // full screen
234 } else {
235 if (!rtRect.intersect(*clearRect)) {
236 return;
237 }
238 }
239 }
240
241 // TODO: in a post-MDB world this should be handled at the OpList level.
242 // An op-list that is initially cleared and has no other ops should receive an
243 // extra draw.
244 if (fRenderTargetContext->fContext->caps()->useDrawInsteadOfClear()) {
245 // This works around a driver bug with clear by drawing a rect instead.
246 // The driver will ignore a clear if it is the only thing rendered to a
247 // target before the target is read.
248 GrPaint paint;
249 paint.setColor4f(GrColor4f::FromGrColor(color));
Brian Salomona1633922017-01-09 11:46:10 -0500250 paint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
Robert Phillips784b7bf2016-12-09 13:35:02 -0500251
252 // We don't call drawRect() here to avoid the cropping to the, possibly smaller,
253 // RenderTargetProxy bounds
Brian Salomon82f44312017-01-11 13:42:54 -0500254 fRenderTargetContext->drawNonAAFilledRect(GrNoClip(), std::move(paint), SkMatrix::I(),
255 SkRect::Make(rtRect), nullptr, nullptr, nullptr,
256 GrAAType::kNone);
Robert Phillips784b7bf2016-12-09 13:35:02 -0500257
258 } else {
Robert Phillips784b7bf2016-12-09 13:35:02 -0500259 // This path doesn't handle coalescing of full screen clears b.c. it
260 // has to clear the entire render target - not just the content area.
261 // It could be done but will take more finagling.
Robert Phillips33f83152017-05-18 20:57:43 +0000262 std::unique_ptr<GrOp> op(GrClearOp::Make(rtRect, color, fRenderTargetContext, !clearRect));
Brian Salomonfc527d22016-12-14 21:07:01 -0500263 if (!op) {
Robert Phillips784b7bf2016-12-09 13:35:02 -0500264 return;
265 }
Robert Phillips33f83152017-05-18 20:57:43 +0000266 fRenderTargetContext->getOpList()->addOp(std::move(op), fRenderTargetContext);
Robert Phillips784b7bf2016-12-09 13:35:02 -0500267 }
268}
269
Brian Osman11052242016-10-27 14:47:55 -0400270void GrRenderTargetContextPriv::clear(const GrFixedClip& clip,
271 const GrColor color,
272 bool canIgnoreClip) {
csmartdalton29df7602016-08-31 11:55:52 -0700273 ASSERT_SINGLE_OWNER_PRIV
274 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400275 SkDEBUGCODE(fRenderTargetContext->validate();)
276 GR_AUDIT_TRAIL_AUTO_FRAME(fRenderTargetContext->fAuditTrail,
277 "GrRenderTargetContextPriv::clear");
csmartdalton29df7602016-08-31 11:55:52 -0700278
Robert Phillips72152832017-01-25 17:31:35 -0500279 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Brian Osman11052242016-10-27 14:47:55 -0400280 fRenderTargetContext->internalClear(clip, color, canIgnoreClip);
csmartdalton29df7602016-08-31 11:55:52 -0700281}
282
Brian Osman11052242016-10-27 14:47:55 -0400283void GrRenderTargetContext::internalClear(const GrFixedClip& clip,
284 const GrColor color,
285 bool canIgnoreClip) {
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700286 bool isFull = false;
287 if (!clip.hasWindowRectangles()) {
288 isFull = !clip.scissorEnabled() ||
289 (canIgnoreClip && fContext->caps()->fullClearIsFree()) ||
290 clip.scissorRect().contains(SkIRect::MakeWH(this->width(), this->height()));
291 }
robertphillips9199a9f2016-07-13 07:48:43 -0700292
293 if (fContext->caps()->useDrawInsteadOfClear()) {
294 // This works around a driver bug with clear by drawing a rect instead.
295 // The driver will ignore a clear if it is the only thing rendered to a
296 // target before the target is read.
Robert Phillips784b7bf2016-12-09 13:35:02 -0500297 SkIRect clearRect = SkIRect::MakeWH(this->width(), this->height());
csmartdalton29df7602016-08-31 11:55:52 -0700298 if (isFull) {
robertphillips9199a9f2016-07-13 07:48:43 -0700299 this->discard();
Robert Phillips93f16332016-11-23 19:37:13 -0500300 } else if (!clearRect.intersect(clip.scissorRect())) {
csmartdalton29df7602016-08-31 11:55:52 -0700301 return;
robertphillips9199a9f2016-07-13 07:48:43 -0700302 }
303
304 GrPaint paint;
305 paint.setColor4f(GrColor4f::FromGrColor(color));
Brian Salomona1633922017-01-09 11:46:10 -0500306 paint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
robertphillips9199a9f2016-07-13 07:48:43 -0700307
Brian Salomon82f44312017-01-11 13:42:54 -0500308 this->drawRect(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), SkRect::Make(clearRect));
bsalomon9f129de2016-08-10 16:31:05 -0700309 } else if (isFull) {
Robert Phillips33f83152017-05-18 20:57:43 +0000310 this->getOpList()->fullClear(this, color);
robertphillips9199a9f2016-07-13 07:48:43 -0700311 } else {
Robert Phillips33f83152017-05-18 20:57:43 +0000312 std::unique_ptr<GrOp> op(GrClearOp::Make(clip, color, this));
Brian Salomon42521e82016-12-07 16:44:58 -0500313 if (!op) {
csmartdalton29df7602016-08-31 11:55:52 -0700314 return;
315 }
Robert Phillips33f83152017-05-18 20:57:43 +0000316 this->getOpList()->addOp(std::move(op), this);
robertphillips9199a9f2016-07-13 07:48:43 -0700317 }
robertphillipsea461502015-05-26 11:38:03 -0700318}
319
Brian Osman11052242016-10-27 14:47:55 -0400320void GrRenderTargetContext::drawPaint(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500321 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400322 const SkMatrix& viewMatrix) {
joshualitt1de610a2016-01-06 08:26:09 -0800323 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700324 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700325 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400326 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawPaint");
robertphillips2e1e51f2015-10-15 08:01:48 -0700327
robertphillipsea461502015-05-26 11:38:03 -0700328 // set rect to be big enough to fill the space, but not super-huge, so we
329 // don't overflow fixed-point implementations
robertphillips13a7eee2016-08-31 15:06:24 -0700330
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400331 SkRect r = fRenderTargetProxy->getBoundsRect();
robertphillipsea461502015-05-26 11:38:03 -0700332
bsalomoncb31e512016-08-26 10:48:19 -0700333 SkRRect rrect;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500334 GrAA aa;
bsalomoncb31e512016-08-26 10:48:19 -0700335 // Check if we can replace a clipRRect()/drawPaint() with a drawRRect(). We only do the
336 // transformation for non-rect rrects. Rects caused a performance regression on an Android
337 // test that needs investigation. We also skip cases where there are fragment processors
338 // because they may depend on having correct local coords and this path draws in device space
339 // without a local matrix.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500340 if (!paint.numTotalFragmentProcessors() && clip.isRRect(r, &rrect, &aa) && !rrect.isRect()) {
Brian Salomon82f44312017-01-11 13:42:54 -0500341 this->drawRRect(GrNoClip(), std::move(paint), aa, SkMatrix::I(), rrect,
342 GrStyle::SimpleFill());
bsalomoncb31e512016-08-26 10:48:19 -0700343 return;
344 }
345
robertphillipsea461502015-05-26 11:38:03 -0700346
347 bool isPerspective = viewMatrix.hasPerspective();
348
349 // We attempt to map r by the inverse matrix and draw that. mapRect will
350 // map the four corners and bound them with a new rect. This will not
351 // produce a correct result for some perspective matrices.
352 if (!isPerspective) {
reeda39667c2016-08-22 06:39:49 -0700353 if (!SkMatrixPriv::InverseMapRect(viewMatrix, &r, r)) {
robertphillipsea461502015-05-26 11:38:03 -0700354 SkDebugf("Could not invert matrix\n");
355 return;
356 }
Brian Salomon82f44312017-01-11 13:42:54 -0500357 this->drawRect(clip, std::move(paint), GrAA::kNo, viewMatrix, r);
robertphillipsea461502015-05-26 11:38:03 -0700358 } else {
359 SkMatrix localMatrix;
360 if (!viewMatrix.invert(&localMatrix)) {
361 SkDebugf("Could not invert matrix\n");
362 return;
363 }
364
Robert Phillips72152832017-01-25 17:31:35 -0500365 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700366
Brian Salomon82f44312017-01-11 13:42:54 -0500367 this->drawNonAAFilledRect(clip, std::move(paint), SkMatrix::I(), r, nullptr, &localMatrix,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500368 nullptr, GrAAType::kNone);
robertphillipsea461502015-05-26 11:38:03 -0700369 }
370}
371
robertphillipsea461502015-05-26 11:38:03 -0700372static inline bool rect_contains_inclusive(const SkRect& rect, const SkPoint& point) {
373 return point.fX >= rect.fLeft && point.fX <= rect.fRight &&
374 point.fY >= rect.fTop && point.fY <= rect.fBottom;
375}
376
bsalomonc55271f2015-11-09 11:55:57 -0800377static bool view_matrix_ok_for_aa_fill_rect(const SkMatrix& viewMatrix) {
378 return viewMatrix.preservesRightAngles();
379}
380
csmartdalton97f6cd52016-07-13 13:37:08 -0700381// Attempts to crop a rect and optional local rect to the clip boundaries.
382// Returns false if the draw can be skipped entirely.
robertphillips13a7eee2016-08-31 15:06:24 -0700383static bool crop_filled_rect(int width, int height, const GrClip& clip,
csmartdalton97f6cd52016-07-13 13:37:08 -0700384 const SkMatrix& viewMatrix, SkRect* rect,
385 SkRect* localRect = nullptr) {
386 if (!viewMatrix.rectStaysRect()) {
387 return true;
388 }
389
csmartdalton97f6cd52016-07-13 13:37:08 -0700390 SkIRect clipDevBounds;
391 SkRect clipBounds;
csmartdalton97f6cd52016-07-13 13:37:08 -0700392
robertphillips13a7eee2016-08-31 15:06:24 -0700393 clip.getConservativeBounds(width, height, &clipDevBounds);
reeda39667c2016-08-22 06:39:49 -0700394 if (!SkMatrixPriv::InverseMapRect(viewMatrix, &clipBounds, SkRect::Make(clipDevBounds))) {
395 return false;
396 }
csmartdalton97f6cd52016-07-13 13:37:08 -0700397
398 if (localRect) {
399 if (!rect->intersects(clipBounds)) {
400 return false;
401 }
402 const SkScalar dx = localRect->width() / rect->width();
403 const SkScalar dy = localRect->height() / rect->height();
404 if (clipBounds.fLeft > rect->fLeft) {
405 localRect->fLeft += (clipBounds.fLeft - rect->fLeft) * dx;
406 rect->fLeft = clipBounds.fLeft;
407 }
408 if (clipBounds.fTop > rect->fTop) {
409 localRect->fTop += (clipBounds.fTop - rect->fTop) * dy;
410 rect->fTop = clipBounds.fTop;
411 }
412 if (clipBounds.fRight < rect->fRight) {
413 localRect->fRight -= (rect->fRight - clipBounds.fRight) * dx;
414 rect->fRight = clipBounds.fRight;
415 }
416 if (clipBounds.fBottom < rect->fBottom) {
417 localRect->fBottom -= (rect->fBottom - clipBounds.fBottom) * dy;
418 rect->fBottom = clipBounds.fBottom;
419 }
420 return true;
421 }
422
423 return rect->intersect(clipBounds);
424}
425
Brian Osman11052242016-10-27 14:47:55 -0400426bool GrRenderTargetContext::drawFilledRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500427 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500428 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400429 const SkMatrix& viewMatrix,
430 const SkRect& rect,
431 const GrUserStencilSettings* ss) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700432 SkRect croppedRect = rect;
Robert Phillips784b7bf2016-12-09 13:35:02 -0500433 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix, &croppedRect)) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700434 return true;
435 }
robertphillips44302392016-07-08 14:43:03 -0700436
Brian Salomon54d212e2017-03-21 14:22:38 -0400437 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport() &&
438 (!ss || ss->isDisabled(false))) {
Robert Phillipse3302df2017-04-24 07:31:02 -0400439 gr_instanced::OpAllocator* oa = this->drawingManager()->instancingAllocator();
440 std::unique_ptr<GrDrawOp> op = oa->recordRect(croppedRect, viewMatrix, std::move(paint),
441 aa, fInstancedPipelineInfo);
Brian Salomon42521e82016-12-07 16:44:58 -0500442 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -0400443 this->addDrawOp(clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700444 return true;
csmartdaltona7f29642016-07-07 08:49:11 -0700445 }
446 }
Brian Salomon7c8460e2017-05-12 11:36:10 -0400447 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500448 if (GrAAType::kCoverage == aaType) {
robertphillips391395d2016-03-02 09:26:36 -0800449 // The fill path can handle rotation but not skew.
450 if (view_matrix_ok_for_aa_fill_rect(viewMatrix)) {
451 SkRect devBoundRect;
csmartdalton97f6cd52016-07-13 13:37:08 -0700452 viewMatrix.mapRect(&devBoundRect, croppedRect);
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400453 std::unique_ptr<GrLegacyMeshDrawOp> op =
Brian Salomon649a3412017-03-09 13:50:43 -0500454 GrRectOpFactory::MakeAAFill(paint, viewMatrix, rect, croppedRect, devBoundRect);
Brian Salomon42521e82016-12-07 16:44:58 -0500455 if (op) {
Brian Salomon82f44312017-01-11 13:42:54 -0500456 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
robertphillips44302392016-07-08 14:43:03 -0700457 if (ss) {
458 pipelineBuilder.setUserStencil(ss);
459 }
Brian Salomone14bd802017-04-04 15:13:25 -0400460 this->addLegacyMeshDrawOp(std::move(pipelineBuilder), clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700461 return true;
462 }
robertphillips391395d2016-03-02 09:26:36 -0800463 }
464 } else {
Brian Salomon82f44312017-01-11 13:42:54 -0500465 this->drawNonAAFilledRect(clip, std::move(paint), viewMatrix, croppedRect, nullptr, nullptr,
466 ss, aaType);
robertphillips44302392016-07-08 14:43:03 -0700467 return true;
robertphillips391395d2016-03-02 09:26:36 -0800468 }
469
robertphillips44302392016-07-08 14:43:03 -0700470 return false;
robertphillips391395d2016-03-02 09:26:36 -0800471}
472
Brian Osman11052242016-10-27 14:47:55 -0400473void GrRenderTargetContext::drawRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500474 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500475 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400476 const SkMatrix& viewMatrix,
477 const SkRect& rect,
478 const GrStyle* style) {
bsalomon6663acf2016-05-10 09:14:17 -0700479 if (!style) {
480 style = &GrStyle::SimpleFill();
481 }
joshualitt1de610a2016-01-06 08:26:09 -0800482 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700483 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700484 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400485 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawRect");
robertphillips2e1e51f2015-10-15 08:01:48 -0700486
bsalomon6663acf2016-05-10 09:14:17 -0700487 // Path effects should've been devolved to a path in SkGpuDevice
488 SkASSERT(!style->pathEffect());
robertphillipsea461502015-05-26 11:38:03 -0700489
Robert Phillips72152832017-01-25 17:31:35 -0500490 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700491
bsalomon6663acf2016-05-10 09:14:17 -0700492 const SkStrokeRec& stroke = style->strokeRec();
robertphillips3ab14ca2016-07-10 11:49:39 -0700493 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
494
495 if (!fContext->caps()->useDrawInsteadOfClear()) {
496 // Check if this is a full RT draw and can be replaced with a clear. We don't bother
497 // checking cases where the RT is fully inside a stroke.
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400498 SkRect rtRect = fRenderTargetProxy->getBoundsRect();
robertphillips3ab14ca2016-07-10 11:49:39 -0700499 // Does the clip contain the entire RT?
500 if (clip.quickContains(rtRect)) {
501 SkMatrix invM;
502 if (!viewMatrix.invert(&invM)) {
robertphillipsea461502015-05-26 11:38:03 -0700503 return;
504 }
robertphillips3ab14ca2016-07-10 11:49:39 -0700505 // Does the rect bound the RT?
506 SkPoint srcSpaceRTQuad[4];
507 invM.mapRectToQuad(srcSpaceRTQuad, rtRect);
508 if (rect_contains_inclusive(rect, srcSpaceRTQuad[0]) &&
509 rect_contains_inclusive(rect, srcSpaceRTQuad[1]) &&
510 rect_contains_inclusive(rect, srcSpaceRTQuad[2]) &&
511 rect_contains_inclusive(rect, srcSpaceRTQuad[3])) {
512 // Will it blend?
513 GrColor clearColor;
514 if (paint.isConstantBlendedColor(&clearColor)) {
robertphillips9199a9f2016-07-13 07:48:43 -0700515 this->clear(nullptr, clearColor, true);
robertphillips3ab14ca2016-07-10 11:49:39 -0700516 return;
517 }
518 }
robertphillipsea461502015-05-26 11:38:03 -0700519 }
520 }
robertphillips44302392016-07-08 14:43:03 -0700521
Brian Salomon82f44312017-01-11 13:42:54 -0500522 if (this->drawFilledRect(clip, std::move(paint), aa, viewMatrix, rect, nullptr)) {
robertphillips44302392016-07-08 14:43:03 -0700523 return;
524 }
bsalomona7d85ba2016-07-06 11:54:59 -0700525 } else if (stroke.getStyle() == SkStrokeRec::kStroke_Style ||
526 stroke.getStyle() == SkStrokeRec::kHairline_Style) {
527 if ((!rect.width() || !rect.height()) &&
528 SkStrokeRec::kHairline_Style != stroke.getStyle()) {
529 SkScalar r = stroke.getWidth() / 2;
530 // TODO: Move these stroke->fill fallbacks to GrShape?
531 switch (stroke.getJoin()) {
532 case SkPaint::kMiter_Join:
Brian Salomon82f44312017-01-11 13:42:54 -0500533 this->drawRect(
534 clip, std::move(paint), aa, viewMatrix,
535 {rect.fLeft - r, rect.fTop - r, rect.fRight + r, rect.fBottom + r},
536 &GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700537 return;
538 case SkPaint::kRound_Join:
539 // Raster draws nothing when both dimensions are empty.
540 if (rect.width() || rect.height()){
541 SkRRect rrect = SkRRect::MakeRectXY(rect.makeOutset(r, r), r, r);
Brian Salomon82f44312017-01-11 13:42:54 -0500542 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect,
543 GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700544 return;
545 }
546 case SkPaint::kBevel_Join:
547 if (!rect.width()) {
Brian Salomon82f44312017-01-11 13:42:54 -0500548 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700549 {rect.fLeft - r, rect.fTop, rect.fRight + r, rect.fBottom},
550 &GrStyle::SimpleFill());
551 } else {
Brian Salomon82f44312017-01-11 13:42:54 -0500552 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700553 {rect.fLeft, rect.fTop - r, rect.fRight, rect.fBottom + r},
554 &GrStyle::SimpleFill());
555 }
556 return;
557 }
558 }
robertphillips44302392016-07-08 14:43:03 -0700559
robertphillips44302392016-07-08 14:43:03 -0700560 bool snapToPixelCenters = false;
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400561 std::unique_ptr<GrLegacyMeshDrawOp> op;
robertphillips44302392016-07-08 14:43:03 -0700562
robertphillips391395d2016-03-02 09:26:36 -0800563 GrColor color = paint.getColor();
Brian Salomon7c8460e2017-05-12 11:36:10 -0400564 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500565 if (GrAAType::kCoverage == aaType) {
cdaltonbb539482016-01-04 09:48:25 -0800566 // The stroke path needs the rect to remain axis aligned (no rotation or skew).
567 if (viewMatrix.rectStaysRect()) {
Brian Salomon6a639042016-12-14 11:08:17 -0500568 op = GrRectOpFactory::MakeAAStroke(color, viewMatrix, rect, stroke);
cdaltonbb539482016-01-04 09:48:25 -0800569 }
robertphillipsea461502015-05-26 11:38:03 -0700570 } else {
robertphillips391395d2016-03-02 09:26:36 -0800571 // Depending on sub-pixel coordinates and the particular GPU, we may lose a corner of
halcanary9d524f22016-03-29 09:03:52 -0700572 // hairline rects. We jam all the vertices to pixel centers to avoid this, but not
robertphillips391395d2016-03-02 09:26:36 -0800573 // when MSAA is enabled because it can cause ugly artifacts.
bsalomona7d85ba2016-07-06 11:54:59 -0700574 snapToPixelCenters = stroke.getStyle() == SkStrokeRec::kHairline_Style &&
Brian Salomon7c8460e2017-05-12 11:36:10 -0400575 GrFSAAType::kUnifiedMSAA != fRenderTargetProxy->fsaaType();
Brian Salomon6a639042016-12-14 11:08:17 -0500576 op = GrRectOpFactory::MakeNonAAStroke(color, viewMatrix, rect, stroke,
577 snapToPixelCenters);
robertphillips391395d2016-03-02 09:26:36 -0800578 }
robertphillips4bc31812016-03-01 12:22:49 -0800579
Brian Salomon42521e82016-12-07 16:44:58 -0500580 if (op) {
Brian Salomon82f44312017-01-11 13:42:54 -0500581 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
Brian Salomon189098e72017-01-19 09:55:19 -0500582 pipelineBuilder.setSnapVerticesToPixelCenters(snapToPixelCenters);
Brian Salomone14bd802017-04-04 15:13:25 -0400583 this->addLegacyMeshDrawOp(std::move(pipelineBuilder), 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
Brian Osman11052242016-10-27 14:47:55 -0400599void 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();)
603 GR_AUDIT_TRAIL_AUTO_FRAME(fRenderTargetContext->fAuditTrail,
604 "GrRenderTargetContextPriv::clearStencilClip");
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
608 std::unique_ptr<GrOp> op(GrClearStencilClipOp::Make(clip, insideStencilMask,
609 fRenderTargetContext));
610 if (!op) {
Robert Phillipse60ad622016-11-17 10:22:48 -0500611 return;
612 }
Robert Phillips33f83152017-05-18 20:57:43 +0000613 fRenderTargetContext->getOpList()->addOp(std::move(op), fRenderTargetContext);
robertphillips976f5f02016-06-03 10:59:20 -0700614}
615
Brian Osman11052242016-10-27 14:47:55 -0400616void GrRenderTargetContextPriv::stencilPath(const GrClip& clip,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500617 GrAAType aaType,
Brian Osman11052242016-10-27 14:47:55 -0400618 const SkMatrix& viewMatrix,
619 const GrPath* path) {
Brian Salomon467921e2017-03-06 16:17:12 -0500620 ASSERT_SINGLE_OWNER_PRIV
621 RETURN_IF_ABANDONED_PRIV
622 SkDEBUGCODE(fRenderTargetContext->validate();)
623 GR_AUDIT_TRAIL_AUTO_FRAME(fRenderTargetContext->fAuditTrail,
624 "GrRenderTargetContext::stencilPath");
625
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500626 SkASSERT(aaType != GrAAType::kCoverage);
Brian Salomon467921e2017-03-06 16:17:12 -0500627
628 bool useHWAA = GrAATypeIsHW(aaType);
629 // TODO: extract portions of checkDraw that are relevant to path stenciling.
630 SkASSERT(path);
631 SkASSERT(fRenderTargetContext->caps()->shaderCaps()->pathRenderingSupport());
632
633 // FIXME: Use path bounds instead of this WAR once
634 // https://bugs.chromium.org/p/skia/issues/detail?id=5640 is resolved.
635 SkRect bounds = SkRect::MakeIWH(fRenderTargetContext->width(), fRenderTargetContext->height());
636
637 // Setup clip
Brian Salomon97180af2017-03-14 13:42:58 -0400638 GrAppliedClip appliedClip;
Brian Salomon467921e2017-03-06 16:17:12 -0500639 if (!clip.apply(fRenderTargetContext->fContext, fRenderTargetContext, useHWAA, true,
Brian Salomon97180af2017-03-14 13:42:58 -0400640 &appliedClip, &bounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -0500641 return;
642 }
643
644 // Coverage AA does not make sense when rendering to the stencil buffer. The caller should never
645 // attempt this in a situation that would require coverage AA.
646 SkASSERT(!appliedClip.clipCoverageFragmentProcessor());
647
648 GrRenderTarget* rt = fRenderTargetContext->accessRenderTarget();
649 if (!rt) {
650 return;
651 }
652 GrStencilAttachment* stencilAttachment =
653 fRenderTargetContext->fContext->resourceProvider()->attachStencilAttachment(rt);
654 if (!stencilAttachment) {
655 SkDebugf("ERROR creating stencil attachment. Draw skipped.\n");
656 return;
657 }
658
659 std::unique_ptr<GrOp> op = GrStencilPathOp::Make(viewMatrix,
660 useHWAA,
661 path->getFillType(),
662 appliedClip.hasStencilClip(),
663 stencilAttachment->bits(),
664 appliedClip.scissorState(),
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400665 fRenderTargetContext,
Brian Salomon467921e2017-03-06 16:17:12 -0500666 path);
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400667 if (!op) {
668 return;
669 }
Brian Salomon97180af2017-03-14 13:42:58 -0400670 op->setClippedBounds(bounds);
Robert Phillips33f83152017-05-18 20:57:43 +0000671 fRenderTargetContext->getOpList()->addOp(std::move(op), fRenderTargetContext);
robertphillips976f5f02016-06-03 10:59:20 -0700672}
673
Brian Osman11052242016-10-27 14:47:55 -0400674void GrRenderTargetContextPriv::stencilRect(const GrClip& clip,
675 const GrUserStencilSettings* ss,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500676 GrAAType aaType,
Brian Osman11052242016-10-27 14:47:55 -0400677 const SkMatrix& viewMatrix,
678 const SkRect& rect) {
robertphillips976f5f02016-06-03 10:59:20 -0700679 ASSERT_SINGLE_OWNER_PRIV
680 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400681 SkDEBUGCODE(fRenderTargetContext->validate();)
682 GR_AUDIT_TRAIL_AUTO_FRAME(fRenderTargetContext->fAuditTrail,
683 "GrRenderTargetContext::stencilRect");
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500684 SkASSERT(GrAAType::kCoverage != aaType);
Robert Phillips72152832017-01-25 17:31:35 -0500685 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips976f5f02016-06-03 10:59:20 -0700686
687 GrPaint paint;
Brian Salomona1633922017-01-09 11:46:10 -0500688 paint.setXPFactory(GrDisableColorXPFactory::Get());
robertphillips976f5f02016-06-03 10:59:20 -0700689
Brian Salomon82f44312017-01-11 13:42:54 -0500690 fRenderTargetContext->drawNonAAFilledRect(clip, std::move(paint), viewMatrix, rect, nullptr,
691 nullptr, ss, aaType);
robertphillips976f5f02016-06-03 10:59:20 -0700692}
693
Brian Osman11052242016-10-27 14:47:55 -0400694bool GrRenderTargetContextPriv::drawAndStencilRect(const GrClip& clip,
695 const GrUserStencilSettings* ss,
696 SkRegion::Op op,
697 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500698 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400699 const SkMatrix& viewMatrix,
700 const SkRect& rect) {
robertphillips391395d2016-03-02 09:26:36 -0800701 ASSERT_SINGLE_OWNER_PRIV
702 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400703 SkDEBUGCODE(fRenderTargetContext->validate();)
704 GR_AUDIT_TRAIL_AUTO_FRAME(fRenderTargetContext->fAuditTrail,
705 "GrRenderTargetContext::drawAndStencilRect");
robertphillips391395d2016-03-02 09:26:36 -0800706
Robert Phillips72152832017-01-25 17:31:35 -0500707 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -0800708
709 GrPaint paint;
robertphillips391395d2016-03-02 09:26:36 -0800710 paint.setCoverageSetOpXPFactory(op, invert);
711
Brian Salomon82f44312017-01-11 13:42:54 -0500712 if (fRenderTargetContext->drawFilledRect(clip, std::move(paint), aa, viewMatrix, rect, ss)) {
robertphillips391395d2016-03-02 09:26:36 -0800713 return true;
714 }
robertphillips391395d2016-03-02 09:26:36 -0800715 SkPath path;
716 path.setIsVolatile(true);
717 path.addRect(rect);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500718 return this->drawAndStencilPath(clip, ss, op, invert, aa, viewMatrix, path);
robertphillips391395d2016-03-02 09:26:36 -0800719}
720
Brian Osman11052242016-10-27 14:47:55 -0400721void GrRenderTargetContext::fillRectToRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500722 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500723 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400724 const SkMatrix& viewMatrix,
725 const SkRect& rectToDraw,
726 const SkRect& localRect) {
joshualitt1de610a2016-01-06 08:26:09 -0800727 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700728 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700729 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400730 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::fillRectToRect");
robertphillipsea461502015-05-26 11:38:03 -0700731
csmartdalton97f6cd52016-07-13 13:37:08 -0700732 SkRect croppedRect = rectToDraw;
733 SkRect croppedLocalRect = localRect;
robertphillips13a7eee2016-08-31 15:06:24 -0700734 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix,
735 &croppedRect, &croppedLocalRect)) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700736 return;
737 }
738
Robert Phillips72152832017-01-25 17:31:35 -0500739 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -0700740
csmartdaltone0d36292016-07-29 08:14:20 -0700741 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport()) {
Robert Phillipse3302df2017-04-24 07:31:02 -0400742 gr_instanced::OpAllocator* oa = this->drawingManager()->instancingAllocator();
743 std::unique_ptr<GrDrawOp> op(oa->recordRect(croppedRect, viewMatrix, std::move(paint),
Brian Salomon54d212e2017-03-21 14:22:38 -0400744 croppedLocalRect, aa, fInstancedPipelineInfo));
Brian Salomon42521e82016-12-07 16:44:58 -0500745 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -0400746 this->addDrawOp(clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -0700747 return;
748 }
749 }
750
Brian Salomon7c8460e2017-05-12 11:36:10 -0400751 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500752 if (GrAAType::kCoverage != aaType) {
Brian Salomon82f44312017-01-11 13:42:54 -0500753 this->drawNonAAFilledRect(clip, std::move(paint), viewMatrix, croppedRect,
754 &croppedLocalRect, nullptr, nullptr, aaType);
csmartdaltonfc49d562016-07-26 17:05:47 -0700755 return;
joshualitt04194f32016-01-13 10:08:27 -0800756 }
bsalomonbb243832016-07-22 07:10:19 -0700757
csmartdaltonfc49d562016-07-26 17:05:47 -0700758 if (view_matrix_ok_for_aa_fill_rect(viewMatrix)) {
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400759 std::unique_ptr<GrLegacyMeshDrawOp> op = GrAAFillRectOp::MakeWithLocalRect(
Brian Salomonf8334782017-01-03 09:42:58 -0500760 paint.getColor(), viewMatrix, croppedRect, croppedLocalRect);
Brian Salomon82f44312017-01-11 13:42:54 -0500761 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
Brian Salomone14bd802017-04-04 15:13:25 -0400762 this->addLegacyMeshDrawOp(std::move(pipelineBuilder), clip, std::move(op));
csmartdaltonfc49d562016-07-26 17:05:47 -0700763 return;
764 }
765
766 SkMatrix viewAndUnLocalMatrix;
767 if (!viewAndUnLocalMatrix.setRectToRect(localRect, rectToDraw, SkMatrix::kFill_ScaleToFit)) {
768 SkDebugf("fillRectToRect called with empty local matrix.\n");
769 return;
770 }
771 viewAndUnLocalMatrix.postConcat(viewMatrix);
772
773 SkPath path;
774 path.setIsVolatile(true);
775 path.addRect(localRect);
Brian Salomon82f44312017-01-11 13:42:54 -0500776 this->internalDrawPath(clip, std::move(paint), aa, viewAndUnLocalMatrix, path, GrStyle());
joshualittb6b513b2015-08-21 10:25:18 -0700777}
778
Brian Osman11052242016-10-27 14:47:55 -0400779void GrRenderTargetContext::fillRectWithLocalMatrix(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500780 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500781 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400782 const SkMatrix& viewMatrix,
783 const SkRect& rectToDraw,
784 const SkMatrix& localMatrix) {
joshualitt1de610a2016-01-06 08:26:09 -0800785 ASSERT_SINGLE_OWNER
joshualittb6b513b2015-08-21 10:25:18 -0700786 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700787 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400788 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::fillRectWithLocalMatrix");
joshualittb6b513b2015-08-21 10:25:18 -0700789
csmartdalton97f6cd52016-07-13 13:37:08 -0700790 SkRect croppedRect = rectToDraw;
robertphillips13a7eee2016-08-31 15:06:24 -0700791 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix, &croppedRect)) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700792 return;
793 }
794
Robert Phillips72152832017-01-25 17:31:35 -0500795 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -0700796
csmartdaltone0d36292016-07-29 08:14:20 -0700797 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport()) {
Robert Phillipse3302df2017-04-24 07:31:02 -0400798 gr_instanced::OpAllocator* oa = this->drawingManager()->instancingAllocator();
799 std::unique_ptr<GrDrawOp> op(oa->recordRect(croppedRect, viewMatrix, std::move(paint),
Brian Salomon54d212e2017-03-21 14:22:38 -0400800 localMatrix, aa, fInstancedPipelineInfo));
Brian Salomon42521e82016-12-07 16:44:58 -0500801 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -0400802 this->addDrawOp(clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -0700803 return;
804 }
805 }
806
Brian Salomon7c8460e2017-05-12 11:36:10 -0400807 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500808 if (GrAAType::kCoverage != aaType) {
Brian Salomon82f44312017-01-11 13:42:54 -0500809 this->drawNonAAFilledRect(clip, std::move(paint), viewMatrix, croppedRect, nullptr,
810 &localMatrix, nullptr, aaType);
csmartdaltonfc49d562016-07-26 17:05:47 -0700811 return;
bsalomonc55271f2015-11-09 11:55:57 -0800812 }
robertphillips4bc31812016-03-01 12:22:49 -0800813
csmartdaltonfc49d562016-07-26 17:05:47 -0700814 if (view_matrix_ok_for_aa_fill_rect(viewMatrix)) {
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400815 std::unique_ptr<GrLegacyMeshDrawOp> op =
Brian Salomon6a639042016-12-14 11:08:17 -0500816 GrAAFillRectOp::Make(paint.getColor(), viewMatrix, localMatrix, croppedRect);
Brian Salomon82f44312017-01-11 13:42:54 -0500817 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
Brian Salomone14bd802017-04-04 15:13:25 -0400818 this->addLegacyMeshDrawOp(std::move(pipelineBuilder), clip, std::move(op));
csmartdaltonfc49d562016-07-26 17:05:47 -0700819 return;
820 }
821
822 SkMatrix viewAndUnLocalMatrix;
823 if (!localMatrix.invert(&viewAndUnLocalMatrix)) {
824 SkDebugf("fillRectWithLocalMatrix called with degenerate local matrix.\n");
825 return;
826 }
827 viewAndUnLocalMatrix.postConcat(viewMatrix);
828
829 SkPath path;
830 path.setIsVolatile(true);
831 path.addRect(rectToDraw);
832 path.transform(localMatrix);
Brian Salomon82f44312017-01-11 13:42:54 -0500833 this->internalDrawPath(clip, std::move(paint), aa, viewAndUnLocalMatrix, path, GrStyle());
robertphillipsea461502015-05-26 11:38:03 -0700834}
835
Brian Osman11052242016-10-27 14:47:55 -0400836void GrRenderTargetContext::drawVertices(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500837 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400838 const SkMatrix& viewMatrix,
839 GrPrimitiveType primitiveType,
840 int vertexCount,
841 const SkPoint positions[],
842 const SkPoint texCoords[],
Brian Salomon3de0aee2017-01-29 09:34:17 -0500843 const uint32_t colors[],
Brian Osman11052242016-10-27 14:47:55 -0400844 const uint16_t indices[],
Brian Salomon3de0aee2017-01-29 09:34:17 -0500845 int indexCount,
846 ColorArrayType colorArrayType) {
joshualitt1de610a2016-01-06 08:26:09 -0800847 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700848 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700849 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400850 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawVertices");
robertphillipsea461502015-05-26 11:38:03 -0700851
Robert Phillips72152832017-01-25 17:31:35 -0500852 AutoCheckFlush acf(this->drawingManager());
robertphillips2e1e51f2015-10-15 08:01:48 -0700853
robertphillipsea461502015-05-26 11:38:03 -0700854 // TODO clients should give us bounds
855 SkRect bounds;
856 if (!bounds.setBoundsCheck(positions, vertexCount)) {
857 SkDebugf("drawVertices call empty bounds\n");
858 return;
859 }
860
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400861 std::unique_ptr<GrLegacyMeshDrawOp> op = GrDrawVerticesOp::Make(
Brian Salomon3de0aee2017-01-29 09:34:17 -0500862 paint.getColor(), primitiveType, viewMatrix, positions, vertexCount, indices,
863 indexCount, colors, texCoords, bounds, colorArrayType);
Brian Salomon199fb872017-02-06 09:41:10 -0500864 if (!op) {
865 return;
866 }
867 GrPipelineBuilder pipelineBuilder(std::move(paint), GrAAType::kNone);
Brian Salomone14bd802017-04-04 15:13:25 -0400868 this->addLegacyMeshDrawOp(std::move(pipelineBuilder), clip, std::move(op));
Brian Salomon199fb872017-02-06 09:41:10 -0500869}
robertphillipsea461502015-05-26 11:38:03 -0700870
Brian Salomon199fb872017-02-06 09:41:10 -0500871void GrRenderTargetContext::drawVertices(const GrClip& clip,
872 GrPaint&& paint,
873 const SkMatrix& viewMatrix,
Mike Reed5fa66452017-03-16 09:06:34 -0400874 sk_sp<SkVertices> vertices) {
Brian Salomon199fb872017-02-06 09:41:10 -0500875 ASSERT_SINGLE_OWNER
876 RETURN_IF_ABANDONED
877 SkDEBUGCODE(this->validate();)
878 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawVertices");
879
880 AutoCheckFlush acf(this->drawingManager());
881
882 SkASSERT(vertices);
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400883 std::unique_ptr<GrLegacyMeshDrawOp> op =
Mike Reed5fa66452017-03-16 09:06:34 -0400884 GrDrawVerticesOp::Make(paint.getColor(), std::move(vertices), viewMatrix);
Brian Salomon199fb872017-02-06 09:41:10 -0500885 if (!op) {
886 return;
887 }
Brian Salomon82f44312017-01-11 13:42:54 -0500888 GrPipelineBuilder pipelineBuilder(std::move(paint), GrAAType::kNone);
Brian Salomone14bd802017-04-04 15:13:25 -0400889 this->addLegacyMeshDrawOp(std::move(pipelineBuilder), clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -0700890}
891
892///////////////////////////////////////////////////////////////////////////////
893
Brian Osman11052242016-10-27 14:47:55 -0400894void GrRenderTargetContext::drawAtlas(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500895 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400896 const SkMatrix& viewMatrix,
897 int spriteCount,
898 const SkRSXform xform[],
899 const SkRect texRect[],
900 const SkColor colors[]) {
joshualitt1de610a2016-01-06 08:26:09 -0800901 ASSERT_SINGLE_OWNER
jvanverth31ff7622015-08-07 10:09:28 -0700902 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700903 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400904 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawAtlas");
robertphillips2e1e51f2015-10-15 08:01:48 -0700905
Robert Phillips72152832017-01-25 17:31:35 -0500906 AutoCheckFlush acf(this->drawingManager());
halcanary9d524f22016-03-29 09:03:52 -0700907
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400908 std::unique_ptr<GrLegacyMeshDrawOp> op =
Brian Salomonfc527d22016-12-14 21:07:01 -0500909 GrDrawAtlasOp::Make(paint.getColor(), viewMatrix, spriteCount, xform, texRect, colors);
Brian Salomon82f44312017-01-11 13:42:54 -0500910 GrPipelineBuilder pipelineBuilder(std::move(paint), GrAAType::kNone);
Brian Salomone14bd802017-04-04 15:13:25 -0400911 this->addLegacyMeshDrawOp(std::move(pipelineBuilder), clip, std::move(op));
jvanverth31ff7622015-08-07 10:09:28 -0700912}
913
914///////////////////////////////////////////////////////////////////////////////
915
Brian Osman11052242016-10-27 14:47:55 -0400916void GrRenderTargetContext::drawRRect(const GrClip& origClip,
Brian Salomon82f44312017-01-11 13:42:54 -0500917 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500918 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400919 const SkMatrix& viewMatrix,
920 const SkRRect& rrect,
921 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -0800922 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700923 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700924 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400925 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawRRect");
robertphillipsea461502015-05-26 11:38:03 -0700926 if (rrect.isEmpty()) {
927 return;
928 }
929
bsalomon7f0d9f32016-08-15 14:49:10 -0700930 GrNoClip noclip;
931 const GrClip* clip = &origClip;
932#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
933 // The Android framework frequently clips rrects to themselves where the clip is non-aa and the
Brian Salomon42521e82016-12-07 16:44:58 -0500934 // draw is aa. Since our lower level clip code works from op bounds, which are SkRects, it
bsalomon7f0d9f32016-08-15 14:49:10 -0700935 // doesn't detect that the clip can be ignored (modulo antialiasing). The following test
936 // attempts to mitigate the stencil clip cost but will only help when the entire clip stack
937 // can be ignored. We'd prefer to fix this in the framework by removing the clips calls.
938 SkRRect devRRect;
939 if (rrect.transform(viewMatrix, &devRRect) && clip->quickContains(devRRect)) {
940 clip = &noclip;
941 }
942#endif
bsalomon6663acf2016-05-10 09:14:17 -0700943 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
ksakamotoec7f2ac2016-07-05 03:54:53 -0700944
Robert Phillips72152832017-01-25 17:31:35 -0500945 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -0700946 const SkStrokeRec stroke = style.strokeRec();
csmartdaltona7f29642016-07-07 08:49:11 -0700947
csmartdaltone0d36292016-07-29 08:14:20 -0700948 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport() &&
949 stroke.isFillStyle()) {
Robert Phillipse3302df2017-04-24 07:31:02 -0400950 gr_instanced::OpAllocator* oa = this->drawingManager()->instancingAllocator();
Brian Salomon54d212e2017-03-21 14:22:38 -0400951 std::unique_ptr<GrDrawOp> op(
Robert Phillipse3302df2017-04-24 07:31:02 -0400952 oa->recordRRect(rrect, viewMatrix, std::move(paint), aa, fInstancedPipelineInfo));
Brian Salomon42521e82016-12-07 16:44:58 -0500953 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -0400954 this->addDrawOp(*clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -0700955 return;
956 }
957 }
958
Brian Salomon7c8460e2017-05-12 11:36:10 -0400959 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500960 if (GrAAType::kCoverage == aaType) {
Brian Salomon94efbf52016-11-29 13:43:05 -0500961 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomon05441c42017-05-15 16:45:49 -0400962 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeRRectOp(std::move(paint),
963 paint.usesDistanceVectorField(),
964 viewMatrix,
965 rrect,
966 stroke,
967 shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -0500968 if (op) {
Brian Salomon05441c42017-05-15 16:45:49 -0400969 this->addDrawOp(*clip, std::move(op));
robertphillipsb56f9272016-02-25 11:03:52 -0800970 return;
971 }
robertphillipsea461502015-05-26 11:38:03 -0700972 }
robertphillipsb56f9272016-02-25 11:03:52 -0800973
974 SkPath path;
975 path.setIsVolatile(true);
976 path.addRRect(rrect);
Brian Salomon82f44312017-01-11 13:42:54 -0500977 this->internalDrawPath(*clip, std::move(paint), aa, viewMatrix, path, style);
robertphillipsea461502015-05-26 11:38:03 -0700978}
979
Jim Van Verthc5903412016-11-17 15:27:09 -0500980///////////////////////////////////////////////////////////////////////////////
981
Jim Van Verth3af1af92017-05-18 15:06:54 -0400982static SkPoint3 map(const SkMatrix& m, const SkPoint3& pt) {
983 SkPoint3 result;
984 m.mapXY(pt.fX, pt.fY, (SkPoint*)&result.fX);
985 result.fZ = pt.fZ;
986 return result;
987}
988
989bool GrRenderTargetContext::drawFastShadow(const GrClip& clip,
990 GrPaint&& paint,
991 const SkMatrix& viewMatrix,
992 const SkPath& path,
993 const SkDrawShadowRec& rec) {
Jim Van Verthc5903412016-11-17 15:27:09 -0500994 ASSERT_SINGLE_OWNER
Jim Van Verth3af1af92017-05-18 15:06:54 -0400995 if (this->drawingManager()->wasAbandoned()) {
996 return true;
997 }
Jim Van Verthc5903412016-11-17 15:27:09 -0500998 SkDEBUGCODE(this->validate();)
Jim Van Verth3af1af92017-05-18 15:06:54 -0400999 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawFastShadow");
1000
1001 // check z plane
1002 bool tiltZPlane = SkToBool(!SkScalarNearlyZero(rec.fZPlaneParams.fX) ||
1003 !SkScalarNearlyZero(rec.fZPlaneParams.fY));
1004 bool skipAnalytic = SkToBool(rec.fFlags & SkShadowFlags::kGeometricOnly_ShadowFlag);
1005 if (tiltZPlane || skipAnalytic || !viewMatrix.rectStaysRect() || !viewMatrix.isSimilarity()) {
1006 return false;
1007 }
1008
1009 SkRRect rrect;
1010 SkRect rect;
1011 // we can only handle rects, circles, and rrects with circular corners
1012 bool isRRect = path.isRRect(&rrect) && rrect.isSimpleCircular() &&
1013 rrect.radii(SkRRect::kUpperLeft_Corner).fX > SK_ScalarNearlyZero;
1014 if (!isRRect &&
1015 path.isOval(&rect) && SkScalarNearlyEqual(rect.width(), rect.height()) &&
1016 rect.width() > SK_ScalarNearlyZero) {
1017 rrect.setOval(rect);
1018 isRRect = true;
1019 }
1020 if (!isRRect && path.isRect(&rect)) {
1021 rrect.setRect(rect);
1022 isRRect = true;
1023 }
1024
1025 if (!isRRect) {
1026 return false;
1027 }
1028
Jim Van Verthc5903412016-11-17 15:27:09 -05001029 if (rrect.isEmpty()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001030 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001031 }
1032
Robert Phillips72152832017-01-25 17:31:35 -05001033 AutoCheckFlush acf(this->drawingManager());
Jim Van Verthc5903412016-11-17 15:27:09 -05001034
Jim Van Verth3af1af92017-05-18 15:06:54 -04001035 // transform light
1036 SkPoint3 devLightPos = map(viewMatrix, rec.fLightPos);
1037
1038 // 1/scale
1039 SkScalar devToSrcScale = viewMatrix.isScaleTranslate() ?
1040 SkScalarInvert(viewMatrix[SkMatrix::kMScaleX]) :
1041 sk_float_rsqrt(viewMatrix[SkMatrix::kMScaleX] * viewMatrix[SkMatrix::kMScaleX] +
1042 viewMatrix[SkMatrix::kMSkewX] * viewMatrix[SkMatrix::kMSkewX]);
1043
1044 SkScalar occluderHeight = rec.fZPlaneParams.fZ;
1045 GrColor4f color = paint.getColor4f();
1046 bool transparent = SkToBool(rec.fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag);
1047
1048 if (rec.fAmbientAlpha > 0) {
1049 static constexpr float kHeightFactor = 1.0f / 128.0f;
1050 static constexpr float kGeomFactor = 64.0f;
1051
1052 SkScalar devSpaceInsetWidth = occluderHeight * kHeightFactor * kGeomFactor;
1053 const float umbraAlpha = (1.0f + SkTMax(occluderHeight * kHeightFactor, 0.0f));
1054 const SkScalar devSpaceAmbientBlur = devSpaceInsetWidth * umbraAlpha;
1055
1056 // Outset the shadow rrect to the border of the penumbra
1057 SkScalar ambientPathOutset = devSpaceInsetWidth * devToSrcScale;
1058 SkRRect ambientRRect;
1059 SkRect outsetRect = rrect.rect().makeOutset(ambientPathOutset, ambientPathOutset);
1060 // If the rrect was an oval then its outset will also be one.
1061 // We set it explicitly to avoid errors.
1062 if (rrect.isOval()) {
1063 ambientRRect = SkRRect::MakeOval(outsetRect);
1064 } else {
1065 SkScalar outsetRad = rrect.getSimpleRadii().fX + ambientPathOutset;
1066 ambientRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1067 }
1068
1069 GrColor ambientColor = color.mulByScalar(rec.fAmbientAlpha).toGrColor();
1070 if (transparent) {
1071 // set a large inset to force a fill
1072 devSpaceInsetWidth = ambientRRect.width();
1073 }
1074 // the fraction of the blur we want to apply is devSpaceInsetWidth/devSpaceAmbientBlur,
1075 // which is just 1/umbraAlpha.
1076 SkScalar blurClamp = SkScalarInvert(umbraAlpha);
1077
1078 std::unique_ptr<GrLegacyMeshDrawOp> op = GrShadowRRectOp::Make(ambientColor, viewMatrix,
1079 ambientRRect,
1080 devSpaceAmbientBlur,
1081 devSpaceInsetWidth,
1082 blurClamp);
1083 if (op) {
1084 GrPipelineBuilder pipelineBuilder(std::move(paint), GrAAType::kNone);
1085 this->addLegacyMeshDrawOp(std::move(pipelineBuilder), clip, std::move(op));
1086 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001087 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001088
1089 if (rec.fSpotAlpha > 0) {
1090 float zRatio = SkTPin(occluderHeight / (devLightPos.fZ - occluderHeight), 0.0f, 0.95f);
1091
1092 SkScalar devSpaceSpotBlur = 2.0f * rec.fLightRadius * zRatio;
1093 // handle scale of radius and pad due to CTM
1094 const SkScalar srcSpaceSpotBlur = devSpaceSpotBlur * devToSrcScale;
1095
1096 // Compute the scale and translation for the spot shadow.
1097 const SkScalar spotScale = devLightPos.fZ / (devLightPos.fZ - occluderHeight);
1098 SkPoint spotOffset = SkPoint::Make(zRatio*(-devLightPos.fX), zRatio*(-devLightPos.fY));
1099 // Adjust translate for the effect of the scale.
1100 spotOffset.fX += spotScale*viewMatrix[SkMatrix::kMTransX];
1101 spotOffset.fY += spotScale*viewMatrix[SkMatrix::kMTransY];
1102 // This offset is in dev space, need to transform it into source space.
1103 SkMatrix ctmInverse;
1104 if (viewMatrix.invert(&ctmInverse)) {
1105 ctmInverse.mapPoints(&spotOffset, 1);
1106 } else {
1107 // Since the matrix is a similarity, this should never happen, but just in case...
1108 SkDebugf("Matrix is degenerate. Will not render spot shadow correctly!\n");
1109 SkASSERT(false);
1110 }
1111
1112 // Compute the transformed shadow rrect
1113 SkRRect spotShadowRRect;
1114 SkMatrix shadowTransform;
1115 shadowTransform.setScaleTranslate(spotScale, spotScale, spotOffset.fX, spotOffset.fY);
1116 rrect.transform(shadowTransform, &spotShadowRRect);
1117 SkScalar spotRadius = spotShadowRRect.getSimpleRadii().fX;
1118
1119 // Compute the insetWidth
1120 SkScalar blurOutset = 0.5f*srcSpaceSpotBlur;
1121 SkScalar insetWidth = blurOutset;
1122 if (transparent) {
1123 // If transparent, just do a fill
1124 insetWidth += spotShadowRRect.width();
1125 } else {
1126 // For shadows, instead of using a stroke we specify an inset from the penumbra
1127 // border. We want to extend this inset area so that it meets up with the caster
1128 // geometry. The inset geometry will by default already be inset by the blur width.
1129 //
1130 // We compare the min and max corners inset by the radius between the original
1131 // rrect and the shadow rrect. The distance between the two plus the difference
1132 // between the scaled radius and the original radius gives the distance from the
1133 // transformed shadow shape to the original shape in that corner. The max
1134 // of these gives the maximum distance we need to cover.
1135 //
1136 // Since we are outsetting by 1/2 the blur distance, we just add the maxOffset to
1137 // that to get the full insetWidth.
1138 SkScalar maxOffset;
1139 if (rrect.isRect()) {
1140 // Manhattan distance works better for rects
1141 maxOffset = SkTMax(SkTMax(SkTAbs(spotShadowRRect.rect().fLeft -
1142 rrect.rect().fLeft),
1143 SkTAbs(spotShadowRRect.rect().fTop -
1144 rrect.rect().fTop)),
1145 SkTMax(SkTAbs(spotShadowRRect.rect().fRight -
1146 rrect.rect().fRight),
1147 SkTAbs(spotShadowRRect.rect().fBottom -
1148 rrect.rect().fBottom)));
1149 } else {
1150 SkScalar dr = spotRadius - rrect.getSimpleRadii().fX;
1151 SkPoint upperLeftOffset = SkPoint::Make(spotShadowRRect.rect().fLeft -
1152 rrect.rect().fLeft + dr,
1153 spotShadowRRect.rect().fTop -
1154 rrect.rect().fTop + dr);
1155 SkPoint lowerRightOffset = SkPoint::Make(spotShadowRRect.rect().fRight -
1156 rrect.rect().fRight - dr,
1157 spotShadowRRect.rect().fBottom -
1158 rrect.rect().fBottom - dr);
1159 maxOffset = SkScalarSqrt(SkTMax(upperLeftOffset.lengthSqd(),
1160 lowerRightOffset.lengthSqd())) + dr;
1161 }
1162 insetWidth += maxOffset;
1163 }
1164
1165 // Outset the shadow rrect to the border of the penumbra
1166 SkRect outsetRect = spotShadowRRect.rect().makeOutset(blurOutset, blurOutset);
1167 if (spotShadowRRect.isOval()) {
1168 spotShadowRRect = SkRRect::MakeOval(outsetRect);
1169 } else {
1170 SkScalar outsetRad = spotRadius + blurOutset;
1171 spotShadowRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1172 }
1173
1174 GrColor spotColor = color.mulByScalar(rec.fSpotAlpha).toGrColor();
1175 std::unique_ptr<GrLegacyMeshDrawOp> op = GrShadowRRectOp::Make(spotColor, viewMatrix,
1176 spotShadowRRect,
1177 devSpaceSpotBlur,
1178 insetWidth);
1179 if (op) {
1180 GrPipelineBuilder pipelineBuilder(std::move(paint), GrAAType::kNone);
1181 this->addLegacyMeshDrawOp(std::move(pipelineBuilder), clip, std::move(op));
1182 }
1183 }
1184
1185 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001186}
1187
1188///////////////////////////////////////////////////////////////////////////////
1189
Brian Osman11052242016-10-27 14:47:55 -04001190bool GrRenderTargetContext::drawFilledDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001191 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001192 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001193 const SkMatrix& viewMatrix,
1194 const SkRRect& origOuter,
1195 const SkRRect& origInner) {
robertphillips00095892016-02-29 13:50:40 -08001196 SkASSERT(!origInner.isEmpty());
1197 SkASSERT(!origOuter.isEmpty());
1198
csmartdaltone0d36292016-07-29 08:14:20 -07001199 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport()) {
Robert Phillipse3302df2017-04-24 07:31:02 -04001200 gr_instanced::OpAllocator* oa = this->drawingManager()->instancingAllocator();
1201 std::unique_ptr<GrDrawOp> op(oa->recordDRRect(
Brian Salomon54d212e2017-03-21 14:22:38 -04001202 origOuter, origInner, viewMatrix, std::move(paint), aa, fInstancedPipelineInfo));
Brian Salomon42521e82016-12-07 16:44:58 -05001203 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -04001204 this->addDrawOp(clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -07001205 return true;
1206 }
1207 }
1208
Brian Salomon7c8460e2017-05-12 11:36:10 -04001209 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
robertphillips00095892016-02-29 13:50:40 -08001210
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001211 GrPrimitiveEdgeType innerEdgeType, outerEdgeType;
1212 if (GrAAType::kCoverage == aaType) {
1213 innerEdgeType = kInverseFillAA_GrProcessorEdgeType;
1214 outerEdgeType = kFillAA_GrProcessorEdgeType;
1215 } else {
1216 innerEdgeType = kInverseFillBW_GrProcessorEdgeType;
1217 outerEdgeType = kFillBW_GrProcessorEdgeType;
1218 }
robertphillips00095892016-02-29 13:50:40 -08001219
1220 SkTCopyOnFirstWrite<SkRRect> inner(origInner), outer(origOuter);
1221 SkMatrix inverseVM;
1222 if (!viewMatrix.isIdentity()) {
1223 if (!origInner.transform(viewMatrix, inner.writable())) {
1224 return false;
1225 }
1226 if (!origOuter.transform(viewMatrix, outer.writable())) {
1227 return false;
1228 }
1229 if (!viewMatrix.invert(&inverseVM)) {
1230 return false;
1231 }
1232 } else {
1233 inverseVM.reset();
halcanary9d524f22016-03-29 09:03:52 -07001234 }
robertphillips00095892016-02-29 13:50:40 -08001235
robertphillips00095892016-02-29 13:50:40 -08001236 // TODO these need to be a geometry processors
bungeman06ca8ec2016-06-09 08:01:03 -07001237 sk_sp<GrFragmentProcessor> innerEffect(GrRRectEffect::Make(innerEdgeType, *inner));
robertphillips00095892016-02-29 13:50:40 -08001238 if (!innerEffect) {
1239 return false;
1240 }
1241
bungeman06ca8ec2016-06-09 08:01:03 -07001242 sk_sp<GrFragmentProcessor> outerEffect(GrRRectEffect::Make(outerEdgeType, *outer));
robertphillips00095892016-02-29 13:50:40 -08001243 if (!outerEffect) {
1244 return false;
1245 }
1246
Brian Salomon82f44312017-01-11 13:42:54 -05001247 paint.addCoverageFragmentProcessor(std::move(innerEffect));
1248 paint.addCoverageFragmentProcessor(std::move(outerEffect));
robertphillips00095892016-02-29 13:50:40 -08001249
1250 SkRect bounds = outer->getBounds();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001251 if (GrAAType::kCoverage == aaType) {
robertphillips00095892016-02-29 13:50:40 -08001252 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1253 }
halcanary9d524f22016-03-29 09:03:52 -07001254
Brian Salomon82f44312017-01-11 13:42:54 -05001255 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), bounds,
1256 inverseVM);
robertphillips00095892016-02-29 13:50:40 -08001257 return true;
1258}
1259
Brian Osman11052242016-10-27 14:47:55 -04001260void GrRenderTargetContext::drawDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001261 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001262 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001263 const SkMatrix& viewMatrix,
1264 const SkRRect& outer,
1265 const SkRRect& inner) {
robertphillips00095892016-02-29 13:50:40 -08001266 ASSERT_SINGLE_OWNER
1267 RETURN_IF_ABANDONED
1268 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -04001269 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawDRRect");
robertphillips00095892016-02-29 13:50:40 -08001270
1271 SkASSERT(!outer.isEmpty());
1272 SkASSERT(!inner.isEmpty());
1273
Robert Phillips72152832017-01-25 17:31:35 -05001274 AutoCheckFlush acf(this->drawingManager());
robertphillips00095892016-02-29 13:50:40 -08001275
Brian Salomon82f44312017-01-11 13:42:54 -05001276 if (this->drawFilledDRRect(clip, std::move(paint), aa, viewMatrix, outer, inner)) {
robertphillips00095892016-02-29 13:50:40 -08001277 return;
1278 }
1279
1280 SkPath path;
1281 path.setIsVolatile(true);
1282 path.addRRect(inner);
1283 path.addRRect(outer);
1284 path.setFillType(SkPath::kEvenOdd_FillType);
1285
Brian Salomon82f44312017-01-11 13:42:54 -05001286 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, GrStyle::SimpleFill());
robertphillips00095892016-02-29 13:50:40 -08001287}
1288
robertphillipsea461502015-05-26 11:38:03 -07001289///////////////////////////////////////////////////////////////////////////////
1290
msarettcc319b92016-08-25 18:07:18 -07001291static inline bool is_int(float x) {
1292 return x == (float) sk_float_round2int(x);
1293}
1294
Brian Osman11052242016-10-27 14:47:55 -04001295void GrRenderTargetContext::drawRegion(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001296 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001297 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001298 const SkMatrix& viewMatrix,
1299 const SkRegion& region,
1300 const GrStyle& style) {
msarettcc319b92016-08-25 18:07:18 -07001301 ASSERT_SINGLE_OWNER
1302 RETURN_IF_ABANDONED
1303 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -04001304 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawRegion");
msarettcc319b92016-08-25 18:07:18 -07001305
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001306 if (GrAA::kYes == aa) {
Brian Salomonfc527d22016-12-14 21:07:01 -05001307 // GrRegionOp performs no antialiasing but is much faster, so here we check the matrix
Brian Salomonc57c7c92016-12-06 14:47:34 -05001308 // to see whether aa is really required.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001309 if (!SkToBool(viewMatrix.getType() & ~(SkMatrix::kTranslate_Mask)) &&
1310 is_int(viewMatrix.getTranslateX()) &&
1311 is_int(viewMatrix.getTranslateY())) {
1312 aa = GrAA::kNo;
1313 }
Brian Salomonc57c7c92016-12-06 14:47:34 -05001314 }
msarettcc319b92016-08-25 18:07:18 -07001315 bool complexStyle = !style.isSimpleFill();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001316 if (complexStyle || GrAA::kYes == aa) {
msarettcc319b92016-08-25 18:07:18 -07001317 SkPath path;
1318 region.getBoundaryPath(&path);
Brian Salomon82f44312017-01-11 13:42:54 -05001319 return this->drawPath(clip, std::move(paint), aa, viewMatrix, path, style);
msarettcc319b92016-08-25 18:07:18 -07001320 }
1321
Brian Salomond3ccb0a2017-04-03 10:38:00 -04001322 std::unique_ptr<GrLegacyMeshDrawOp> op = GrRegionOp::Make(paint.getColor(), viewMatrix, region);
Brian Salomon82f44312017-01-11 13:42:54 -05001323 GrPipelineBuilder pipelineBuilder(std::move(paint), GrAAType::kNone);
Brian Salomone14bd802017-04-04 15:13:25 -04001324 this->addLegacyMeshDrawOp(std::move(pipelineBuilder), clip, std::move(op));
msarettcc319b92016-08-25 18:07:18 -07001325}
1326
Brian Osman11052242016-10-27 14:47:55 -04001327void GrRenderTargetContext::drawOval(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001328 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001329 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001330 const SkMatrix& viewMatrix,
1331 const SkRect& oval,
1332 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001333 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001334 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001335 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -04001336 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawOval");
robertphillips2e1e51f2015-10-15 08:01:48 -07001337
robertphillipsea461502015-05-26 11:38:03 -07001338 if (oval.isEmpty()) {
1339 return;
1340 }
1341
bsalomon6663acf2016-05-10 09:14:17 -07001342 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
robertphillipsea461502015-05-26 11:38:03 -07001343
Robert Phillips72152832017-01-25 17:31:35 -05001344 AutoCheckFlush acf(this->drawingManager());
bsalomon6663acf2016-05-10 09:14:17 -07001345 const SkStrokeRec& stroke = style.strokeRec();
csmartdaltona7f29642016-07-07 08:49:11 -07001346
csmartdaltone0d36292016-07-29 08:14:20 -07001347 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport() &&
1348 stroke.isFillStyle()) {
Robert Phillipse3302df2017-04-24 07:31:02 -04001349 gr_instanced::OpAllocator* oa = this->drawingManager()->instancingAllocator();
Brian Salomon54d212e2017-03-21 14:22:38 -04001350 std::unique_ptr<GrDrawOp> op(
Robert Phillipse3302df2017-04-24 07:31:02 -04001351 oa->recordOval(oval, viewMatrix, std::move(paint), aa, fInstancedPipelineInfo));
Brian Salomon42521e82016-12-07 16:44:58 -05001352 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -04001353 this->addDrawOp(clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -07001354 return;
1355 }
1356 }
1357
Brian Salomon7c8460e2017-05-12 11:36:10 -04001358 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001359 if (GrAAType::kCoverage == aaType) {
Brian Salomon94efbf52016-11-29 13:43:05 -05001360 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomon05441c42017-05-15 16:45:49 -04001361 std::unique_ptr<GrDrawOp> op =
1362 GrOvalOpFactory::MakeOvalOp(std::move(paint), viewMatrix, oval, stroke, shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -05001363 if (op) {
Brian Salomon05441c42017-05-15 16:45:49 -04001364 this->addDrawOp(clip, std::move(op));
robertphillipsb56f9272016-02-25 11:03:52 -08001365 return;
1366 }
robertphillipsea461502015-05-26 11:38:03 -07001367 }
robertphillipsb56f9272016-02-25 11:03:52 -08001368
1369 SkPath path;
1370 path.setIsVolatile(true);
1371 path.addOval(oval);
Brian Salomon82f44312017-01-11 13:42:54 -05001372 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, style);
robertphillipsea461502015-05-26 11:38:03 -07001373}
1374
Brian Osman11052242016-10-27 14:47:55 -04001375void GrRenderTargetContext::drawArc(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001376 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001377 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001378 const SkMatrix& viewMatrix,
1379 const SkRect& oval,
1380 SkScalar startAngle,
1381 SkScalar sweepAngle,
1382 bool useCenter,
1383 const GrStyle& style) {
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001384 ASSERT_SINGLE_OWNER
1385 RETURN_IF_ABANDONED
1386 SkDEBUGCODE(this->validate();)
1387 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawArc");
1388
1389 AutoCheckFlush acf(this->drawingManager());
1390
Brian Salomon7c8460e2017-05-12 11:36:10 -04001391 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001392 if (GrAAType::kCoverage == aaType) {
Brian Salomon94efbf52016-11-29 13:43:05 -05001393 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomon05441c42017-05-15 16:45:49 -04001394 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeArcOp(std::move(paint),
1395 viewMatrix,
1396 oval,
1397 startAngle,
1398 sweepAngle,
1399 useCenter,
1400 style,
1401 shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -05001402 if (op) {
Brian Salomon05441c42017-05-15 16:45:49 -04001403 this->addDrawOp(clip, std::move(op));
bsalomon4f3a0ca2016-08-22 13:14:26 -07001404 return;
1405 }
1406 }
1407 SkPath path;
bsalomon21af9ca2016-08-25 12:29:23 -07001408 SkPathPriv::CreateDrawArcPath(&path, oval, startAngle, sweepAngle, useCenter,
1409 style.isSimpleFill());
Brian Salomon82f44312017-01-11 13:42:54 -05001410 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, style);
bsalomon4f3a0ca2016-08-22 13:14:26 -07001411}
1412
Brian Osman11052242016-10-27 14:47:55 -04001413void GrRenderTargetContext::drawImageLattice(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001414 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001415 const SkMatrix& viewMatrix,
1416 int imageWidth,
1417 int imageHeight,
1418 std::unique_ptr<SkLatticeIter> iter,
1419 const SkRect& dst) {
joshualitt1de610a2016-01-06 08:26:09 -08001420 ASSERT_SINGLE_OWNER
joshualitt33a5fce2015-11-18 13:28:51 -08001421 RETURN_IF_ABANDONED
1422 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -04001423 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawImageLattice");
joshualitt33a5fce2015-11-18 13:28:51 -08001424
Robert Phillips72152832017-01-25 17:31:35 -05001425 AutoCheckFlush acf(this->drawingManager());
joshualitt33a5fce2015-11-18 13:28:51 -08001426
Brian Salomond3ccb0a2017-04-03 10:38:00 -04001427 std::unique_ptr<GrLegacyMeshDrawOp> op = GrLatticeOp::MakeNonAA(
Brian Salomon649a3412017-03-09 13:50:43 -05001428 paint.getColor(), viewMatrix, imageWidth, imageHeight, std::move(iter), dst);
joshualitt33a5fce2015-11-18 13:28:51 -08001429
Brian Salomon82f44312017-01-11 13:42:54 -05001430 GrPipelineBuilder pipelineBuilder(std::move(paint), GrAAType::kNone);
Brian Salomone14bd802017-04-04 15:13:25 -04001431 this->addLegacyMeshDrawOp(std::move(pipelineBuilder), clip, std::move(op));
joshualitt33a5fce2015-11-18 13:28:51 -08001432}
1433
Brian Osman11052242016-10-27 14:47:55 -04001434void GrRenderTargetContext::prepareForExternalIO() {
robertphillips8c523e02016-07-26 07:41:00 -07001435 ASSERT_SINGLE_OWNER
1436 RETURN_IF_ABANDONED
1437 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -04001438 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::prepareForExternalIO");
robertphillips8c523e02016-07-26 07:41:00 -07001439
Robert Phillips7ee385e2017-03-30 08:02:11 -04001440 this->drawingManager()->prepareSurfaceForExternalIO(fRenderTargetProxy.get());
robertphillips8c523e02016-07-26 07:41:00 -07001441}
joshualitt33a5fce2015-11-18 13:28:51 -08001442
Brian Osman11052242016-10-27 14:47:55 -04001443void GrRenderTargetContext::drawNonAAFilledRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001444 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001445 const SkMatrix& viewMatrix,
1446 const SkRect& rect,
1447 const SkRect* localRect,
1448 const SkMatrix* localMatrix,
1449 const GrUserStencilSettings* ss,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001450 GrAAType hwOrNoneAAType) {
1451 SkASSERT(GrAAType::kCoverage != hwOrNoneAAType);
Brian Salomon7c8460e2017-05-12 11:36:10 -04001452 SkASSERT(GrAAType::kNone == hwOrNoneAAType || GrFSAAType::kNone != this->fsaaType());
Brian Salomonac70f842017-05-08 10:43:33 -04001453 std::unique_ptr<GrDrawOp> op = GrNonAAFillRectOp::Make(
1454 std::move(paint), viewMatrix, rect, localRect, localMatrix, hwOrNoneAAType, ss);
1455 this->addDrawOp(clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -07001456}
1457
robertphillipsea461502015-05-26 11:38:03 -07001458// Can 'path' be drawn as a pair of filled nested rectangles?
bsalomon6663acf2016-05-10 09:14:17 -07001459static bool fills_as_nested_rects(const SkMatrix& viewMatrix, const SkPath& path, SkRect rects[2]) {
robertphillipsea461502015-05-26 11:38:03 -07001460
1461 if (path.isInverseFillType()) {
1462 return false;
1463 }
1464
1465 // TODO: this restriction could be lifted if we were willing to apply
1466 // the matrix to all the points individually rather than just to the rect
robertphillips0e7029e2015-11-30 05:45:06 -08001467 if (!viewMatrix.rectStaysRect()) {
robertphillipsea461502015-05-26 11:38:03 -07001468 return false;
1469 }
1470
1471 SkPath::Direction dirs[2];
1472 if (!path.isNestedFillRects(rects, dirs)) {
1473 return false;
1474 }
1475
1476 if (SkPath::kWinding_FillType == path.getFillType() && dirs[0] == dirs[1]) {
1477 // The two rects need to be wound opposite to each other
1478 return false;
1479 }
1480
1481 // Right now, nested rects where the margin is not the same width
1482 // all around do not render correctly
1483 const SkScalar* outer = rects[0].asScalars();
1484 const SkScalar* inner = rects[1].asScalars();
1485
1486 bool allEq = true;
1487
1488 SkScalar margin = SkScalarAbs(outer[0] - inner[0]);
1489 bool allGoE1 = margin >= SK_Scalar1;
1490
1491 for (int i = 1; i < 4; ++i) {
1492 SkScalar temp = SkScalarAbs(outer[i] - inner[i]);
1493 if (temp < SK_Scalar1) {
1494 allGoE1 = false;
1495 }
1496 if (!SkScalarNearlyEqual(margin, temp)) {
1497 allEq = false;
1498 }
1499 }
1500
1501 return allEq || allGoE1;
1502}
1503
Brian Osman11052242016-10-27 14:47:55 -04001504void GrRenderTargetContext::drawPath(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001505 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001506 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001507 const SkMatrix& viewMatrix,
1508 const SkPath& path,
1509 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001510 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001511 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001512 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -04001513 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawPath");
robertphillips2e1e51f2015-10-15 08:01:48 -07001514
robertphillipsea461502015-05-26 11:38:03 -07001515 if (path.isEmpty()) {
1516 if (path.isInverseFillType()) {
Brian Salomon82f44312017-01-11 13:42:54 -05001517 this->drawPaint(clip, std::move(paint), viewMatrix);
robertphillipsea461502015-05-26 11:38:03 -07001518 }
1519 return;
1520 }
1521
Robert Phillips72152832017-01-25 17:31:35 -05001522 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -07001523
Brian Salomon7c8460e2017-05-12 11:36:10 -04001524 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001525 if (GrAAType::kCoverage == aaType && !style.pathEffect()) {
bsalomon6663acf2016-05-10 09:14:17 -07001526 if (style.isSimpleFill() && !path.isConvex()) {
robertphillipsea461502015-05-26 11:38:03 -07001527 // Concave AA paths are expensive - try to avoid them for special cases
1528 SkRect rects[2];
1529
bsalomon6663acf2016-05-10 09:14:17 -07001530 if (fills_as_nested_rects(viewMatrix, path, rects)) {
Brian Salomond3ccb0a2017-04-03 10:38:00 -04001531 std::unique_ptr<GrLegacyMeshDrawOp> op =
Brian Salomon6a639042016-12-14 11:08:17 -05001532 GrRectOpFactory::MakeAAFillNestedRects(paint.getColor(), viewMatrix, rects);
Brian Salomon21aa35f2016-12-09 16:01:53 -05001533 if (op) {
Brian Salomon82f44312017-01-11 13:42:54 -05001534 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
Brian Salomone14bd802017-04-04 15:13:25 -04001535 this->addLegacyMeshDrawOp(std::move(pipelineBuilder), clip, std::move(op));
bsalomon40ef4852016-05-02 13:22:13 -07001536 }
robertphillipsea461502015-05-26 11:38:03 -07001537 return;
1538 }
1539 }
1540 SkRect ovalRect;
1541 bool isOval = path.isOval(&ovalRect);
1542
1543 if (isOval && !path.isInverseFillType()) {
Brian Salomon94efbf52016-11-29 13:43:05 -05001544 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomon05441c42017-05-15 16:45:49 -04001545 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeOvalOp(
1546 std::move(paint), viewMatrix, ovalRect, style.strokeRec(), shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -05001547 if (op) {
Brian Salomon05441c42017-05-15 16:45:49 -04001548 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -07001549 return;
1550 }
1551 }
1552 }
robertphillips4bc31812016-03-01 12:22:49 -08001553
1554 // Note that internalDrawPath may sw-rasterize the path into a scratch texture.
1555 // Scratch textures can be recycled after they are returned to the texture
1556 // cache. This presents a potential hazard for buffered drawing. However,
1557 // the writePixels that uploads to the scratch will perform a flush so we're
1558 // OK.
Brian Salomon82f44312017-01-11 13:42:54 -05001559 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, style);
robertphillipsea461502015-05-26 11:38:03 -07001560}
1561
Brian Osman11052242016-10-27 14:47:55 -04001562bool GrRenderTargetContextPriv::drawAndStencilPath(const GrClip& clip,
1563 const GrUserStencilSettings* ss,
1564 SkRegion::Op op,
1565 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001566 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001567 const SkMatrix& viewMatrix,
1568 const SkPath& path) {
robertphillips391395d2016-03-02 09:26:36 -08001569 ASSERT_SINGLE_OWNER_PRIV
1570 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -04001571 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001572 GR_AUDIT_TRAIL_AUTO_FRAME(fRenderTargetContext->fAuditTrail,
1573 "GrRenderTargetContextPriv::drawAndStencilPath");
robertphillips391395d2016-03-02 09:26:36 -08001574
1575 if (path.isEmpty() && path.isInverseFillType()) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001576 this->drawAndStencilRect(clip, ss, op, invert, GrAA::kNo, SkMatrix::I(),
Brian Osman11052242016-10-27 14:47:55 -04001577 SkRect::MakeIWH(fRenderTargetContext->width(),
1578 fRenderTargetContext->height()));
robertphillips391395d2016-03-02 09:26:36 -08001579 return true;
1580 }
1581
Robert Phillips72152832017-01-25 17:31:35 -05001582 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -08001583
1584 // An Assumption here is that path renderer would use some form of tweaking
1585 // the src color (either the input alpha or in the frag shader) to implement
1586 // aa. If we have some future driver-mojo path AA that can do the right
1587 // thing WRT to the blend then we'll need some query on the PR.
Brian Salomon7c8460e2017-05-12 11:36:10 -04001588 GrAAType aaType = fRenderTargetContext->chooseAAType(aa, GrAllowMixedSamples::kNo);
robertphillips976f5f02016-06-03 10:59:20 -07001589 bool hasUserStencilSettings = !ss->isUnused();
robertphillips391395d2016-03-02 09:26:36 -08001590
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;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001596 canDrawArgs.fAAType = aaType;
cdalton93a379b2016-05-11 13:58:08 -07001597 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips391395d2016-03-02 09:26:36 -08001598
1599 // Don't allow the SW renderer
Robert Phillips72152832017-01-25 17:31:35 -05001600 GrPathRenderer* pr = fRenderTargetContext->drawingManager()->getPathRenderer(
Brian Salomon36aa1762016-12-10 13:24:02 -05001601 canDrawArgs, false, GrPathRendererChain::DrawType::kStencilAndColor);
robertphillips391395d2016-03-02 09:26:36 -08001602 if (!pr) {
1603 return false;
1604 }
1605
1606 GrPaint paint;
1607 paint.setCoverageSetOpXPFactory(op, invert);
1608
Brian Salomon82f44312017-01-11 13:42:54 -05001609 GrPathRenderer::DrawPathArgs args{
Robert Phillips256c37b2017-03-01 14:32:46 -05001610 fRenderTargetContext->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05001611 std::move(paint),
1612 ss,
1613 fRenderTargetContext,
1614 &clip,
1615 &viewMatrix,
1616 &shape,
1617 aaType,
1618 fRenderTargetContext->isGammaCorrect()};
robertphillips391395d2016-03-02 09:26:36 -08001619 pr->drawPath(args);
1620 return true;
1621}
1622
Brian Osman11052242016-10-27 14:47:55 -04001623SkBudgeted GrRenderTargetContextPriv::isBudgeted() const {
robertphillips714712b2016-08-04 06:20:45 -07001624 ASSERT_SINGLE_OWNER_PRIV
1625
Brian Osman11052242016-10-27 14:47:55 -04001626 if (fRenderTargetContext->wasAbandoned()) {
robertphillips714712b2016-08-04 06:20:45 -07001627 return SkBudgeted::kNo;
1628 }
1629
Brian Osman11052242016-10-27 14:47:55 -04001630 SkDEBUGCODE(fRenderTargetContext->validate();)
robertphillips714712b2016-08-04 06:20:45 -07001631
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001632 return fRenderTargetContext->fRenderTargetProxy->isBudgeted();
robertphillips714712b2016-08-04 06:20:45 -07001633}
1634
Brian Osman11052242016-10-27 14:47:55 -04001635void GrRenderTargetContext::internalDrawPath(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001636 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001637 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001638 const SkMatrix& viewMatrix,
1639 const SkPath& path,
1640 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001641 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001642 RETURN_IF_ABANDONED
bsalomon8acedde2016-06-24 10:42:16 -07001643 SkASSERT(!path.isEmpty());
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001644 GrShape shape;
Brian Salomon7c8460e2017-05-12 11:36:10 -04001645 // NVPR cannot handle hairlines, so this would get picked up by a different stencil and
1646 // cover path renderer (i.e. default path renderer). The hairline renderer produces much
1647 // smoother hairlines than MSAA.
1648 GrAllowMixedSamples allowMixedSamples =
1649 style.isSimpleHairline() ? GrAllowMixedSamples::kNo : GrAllowMixedSamples::kYes;
1650 GrAAType aaType = this->chooseAAType(aa, allowMixedSamples);
robertphillips68737822015-10-29 12:12:21 -07001651 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Eric Karl5c779752017-05-08 12:02:07 -07001652 canDrawArgs.fCaps = this->drawingManager()->getContext()->caps();
robertphillips68737822015-10-29 12:12:21 -07001653 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07001654 canDrawArgs.fShape = &shape;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001655 canDrawArgs.fHasUserStencilSettings = false;
robertphillips68737822015-10-29 12:12:21 -07001656
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001657 GrPathRenderer* pr;
Brian Salomon82125e92016-12-10 09:35:48 -05001658 static constexpr GrPathRendererChain::DrawType kType = GrPathRendererChain::DrawType::kColor;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001659 do {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001660 shape = GrShape(path, style);
bsalomon8acedde2016-06-24 10:42:16 -07001661 if (shape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001662 return;
1663 }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001664
1665 canDrawArgs.fAAType = aaType;
1666
1667 // Try a 1st time without applying any of the style to the geometry (and barring sw)
Robert Phillips72152832017-01-25 17:31:35 -05001668 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001669 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
1670
1671 if (!pr && shape.style().pathEffect()) {
1672 // It didn't work above, so try again with the path effect applied.
1673 shape = shape.applyStyle(GrStyle::Apply::kPathEffectOnly, styleScale);
bsalomon8acedde2016-06-24 10:42:16 -07001674 if (shape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001675 return;
1676 }
Robert Phillips72152832017-01-25 17:31:35 -05001677 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
bsalomon6663acf2016-05-10 09:14:17 -07001678 }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001679 if (!pr) {
1680 if (shape.style().applies()) {
1681 shape = shape.applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec, styleScale);
1682 if (shape.isEmpty()) {
1683 return;
1684 }
1685 }
1686 // This time, allow SW renderer
Robert Phillips72152832017-01-25 17:31:35 -05001687 pr = this->drawingManager()->getPathRenderer(canDrawArgs, true, kType);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001688 }
Brian Salomon0abc8b42016-12-13 10:22:54 -05001689 if (!pr && GrAATypeIsHW(aaType)) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001690 // There are exceptional cases where we may wind up falling back to coverage based AA
1691 // when the target is MSAA (e.g. through disabling path renderers via GrContextOptions).
1692 aaType = GrAAType::kCoverage;
1693 } else {
1694 break;
1695 }
1696 } while(true);
robertphillipsea461502015-05-26 11:38:03 -07001697
bsalomon8acedde2016-06-24 10:42:16 -07001698 if (!pr) {
robertphillipsea461502015-05-26 11:38:03 -07001699#ifdef SK_DEBUG
1700 SkDebugf("Unable to find path renderer compatible with path.\n");
1701#endif
1702 return;
1703 }
1704
Robert Phillips256c37b2017-03-01 14:32:46 -05001705 GrPathRenderer::DrawPathArgs args{this->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05001706 std::move(paint),
1707 &GrUserStencilSettings::kUnused,
1708 this,
1709 &clip,
1710 &viewMatrix,
1711 &shape,
1712 aaType,
1713 this->isGammaCorrect()};
bsalomon0aff2fa2015-07-31 06:48:27 -07001714 pr->drawPath(args);
robertphillipsea461502015-05-26 11:38:03 -07001715}
1716
Brian Salomon467921e2017-03-06 16:17:12 -05001717static void op_bounds(SkRect* bounds, const GrOp* op) {
1718 *bounds = op->bounds();
1719 if (op->hasZeroArea()) {
1720 if (op->hasAABloat()) {
1721 bounds->outset(0.5f, 0.5f);
1722 } else {
1723 // We don't know which way the particular GPU will snap lines or points at integer
1724 // coords. So we ensure that the bounds is large enough for either snap.
1725 SkRect before = *bounds;
1726 bounds->roundOut(bounds);
1727 if (bounds->fLeft == before.fLeft) {
1728 bounds->fLeft -= 1;
1729 }
1730 if (bounds->fTop == before.fTop) {
1731 bounds->fTop -= 1;
1732 }
1733 if (bounds->fRight == before.fRight) {
1734 bounds->fRight += 1;
1735 }
1736 if (bounds->fBottom == before.fBottom) {
1737 bounds->fBottom += 1;
1738 }
1739 }
1740 }
1741}
1742
Brian Salomon54d212e2017-03-21 14:22:38 -04001743uint32_t GrRenderTargetContext::addDrawOp(const GrClip& clip, std::unique_ptr<GrDrawOp> op) {
joshualitt1de610a2016-01-06 08:26:09 -08001744 ASSERT_SINGLE_OWNER
Robert Phillipsc0138922017-03-08 11:50:55 -05001745 if (this->drawingManager()->wasAbandoned()) {
1746 return SK_InvalidUniqueID;
1747 }
robertphillips2e1e51f2015-10-15 08:01:48 -07001748 SkDEBUGCODE(this->validate();)
Brian Salomon42521e82016-12-07 16:44:58 -05001749 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::addDrawOp");
robertphillips2d70dcb2015-10-06 07:38:23 -07001750
Brian Salomon467921e2017-03-06 16:17:12 -05001751 // Setup clip
1752 SkRect bounds;
1753 op_bounds(&bounds, op.get());
Brian Salomon97180af2017-03-14 13:42:58 -04001754 GrAppliedClip appliedClip;
Brian Salomon54d212e2017-03-21 14:22:38 -04001755 GrDrawOp::FixedFunctionFlags fixedFunctionFlags = op->fixedFunctionFlags();
1756 if (!clip.apply(fContext, this, fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesHWAA,
1757 fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil, &appliedClip,
1758 &bounds)) {
1759 return SK_InvalidUniqueID;
1760 }
1761
1762 // This forces instantiation of the render target.
1763 GrRenderTarget* rt = this->accessRenderTarget();
1764 if (!rt) {
1765 return SK_InvalidUniqueID;
1766 }
1767
1768 if (fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil ||
1769 appliedClip.hasStencilClip()) {
1770 if (!fContext->resourceProvider()->attachStencilAttachment(rt)) {
1771 SkDebugf("ERROR creating stencil attachment. Draw skipped.\n");
1772 return SK_InvalidUniqueID;
1773 }
1774 }
1775
Robert Phillipsc3757042017-05-17 13:00:14 +00001776 GrXferProcessor::DstTexture dstTexture;
Brian Salomon54d212e2017-03-21 14:22:38 -04001777 if (op->xpRequiresDstTexture(*this->caps(), &appliedClip)) {
Robert Phillipsc3757042017-05-17 13:00:14 +00001778 if (!this->setupDstTexture(fRenderTargetProxy.get(), clip, op->bounds(), &dstTexture)) {
Brian Salomon54d212e2017-03-21 14:22:38 -04001779 return SK_InvalidUniqueID;
1780 }
1781 }
1782
1783 op->setClippedBounds(bounds);
Robert Phillips33f83152017-05-18 20:57:43 +00001784 return this->getOpList()->addOp(std::move(op), this, std::move(appliedClip), dstTexture);
Brian Salomon54d212e2017-03-21 14:22:38 -04001785}
1786
Brian Salomone14bd802017-04-04 15:13:25 -04001787uint32_t GrRenderTargetContext::addLegacyMeshDrawOp(GrPipelineBuilder&& pipelineBuilder,
Brian Salomond3ccb0a2017-04-03 10:38:00 -04001788 const GrClip& clip,
1789 std::unique_ptr<GrLegacyMeshDrawOp> op) {
Brian Salomon54d212e2017-03-21 14:22:38 -04001790 ASSERT_SINGLE_OWNER
1791 if (this->drawingManager()->wasAbandoned()) {
1792 return SK_InvalidUniqueID;
1793 }
1794 SkDEBUGCODE(this->validate();)
Brian Salomond3ccb0a2017-04-03 10:38:00 -04001795 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::addLegacyMeshDrawOp");
Brian Salomon54d212e2017-03-21 14:22:38 -04001796
1797 // Setup clip
1798 SkRect bounds;
1799 op_bounds(&bounds, op.get());
1800 GrAppliedClip appliedClip;
Brian Salomon467921e2017-03-06 16:17:12 -05001801 if (!clip.apply(fContext, this, pipelineBuilder.isHWAntialias(),
Brian Salomon97180af2017-03-14 13:42:58 -04001802 pipelineBuilder.hasUserStencilSettings(), &appliedClip, &bounds)) {
Robert Phillipsc0138922017-03-08 11:50:55 -05001803 return SK_InvalidUniqueID;
Brian Salomon467921e2017-03-06 16:17:12 -05001804 }
1805
1806 // This forces instantiation of the render target. Pipeline creation is moving to flush time
1807 // by which point instantiation must have occurred anyway.
1808 GrRenderTarget* rt = this->accessRenderTarget();
1809 if (!rt) {
Robert Phillipsc0138922017-03-08 11:50:55 -05001810 return SK_InvalidUniqueID;
Brian Salomon467921e2017-03-06 16:17:12 -05001811 }
1812
1813 GrResourceProvider* resourceProvider = fContext->resourceProvider();
Brian Salomon48d1b4c2017-04-08 07:38:53 -04001814 bool usesStencil = pipelineBuilder.hasUserStencilSettings() || appliedClip.hasStencilClip();
1815 if (usesStencil) {
Brian Salomon467921e2017-03-06 16:17:12 -05001816 if (!resourceProvider->attachStencilAttachment(this->accessRenderTarget())) {
1817 SkDebugf("ERROR creating stencil attachment. Draw skipped.\n");
Robert Phillipsc0138922017-03-08 11:50:55 -05001818 return SK_InvalidUniqueID;
Brian Salomon467921e2017-03-06 16:17:12 -05001819 }
1820 }
1821
Brian Salomon7c8460e2017-05-12 11:36:10 -04001822 bool isMixedSamples = GrFSAAType::kMixedSamples == this->fsaaType() &&
Brian Salomon48d1b4c2017-04-08 07:38:53 -04001823 (pipelineBuilder.isHWAntialias() || usesStencil);
1824
1825 GrColor overrideColor;
1826 GrProcessorSet::Analysis analysis = op->analyzeUpdateAndRecordProcessors(
1827 &pipelineBuilder, &appliedClip, isMixedSamples, *this->caps(), &overrideColor);
Brian Salomon467921e2017-03-06 16:17:12 -05001828
1829 GrPipeline::InitArgs args;
1830 pipelineBuilder.getPipelineInitArgs(&args);
1831 args.fAppliedClip = &appliedClip;
1832 args.fRenderTarget = rt;
1833 args.fCaps = this->caps();
Brian Salomon467921e2017-03-06 16:17:12 -05001834
Brian Salomon31853842017-03-28 16:32:05 -04001835 if (analysis.requiresDstTexture()) {
Robert Phillipsc3757042017-05-17 13:00:14 +00001836 if (!this->setupDstTexture(fRenderTargetProxy.get(), clip, bounds, &args.fDstTexture)) {
Robert Phillipsc0138922017-03-08 11:50:55 -05001837 return SK_InvalidUniqueID;
Brian Salomon467921e2017-03-06 16:17:12 -05001838 }
1839 }
Brian Salomon48d1b4c2017-04-08 07:38:53 -04001840 op->initPipeline(args, analysis, overrideColor);
Robert Phillipsf5442bb2017-04-17 14:18:34 -04001841
1842 // Add the pipeline dependencies on textures, etc before recording this op.
1843 op->addDependenciesTo(fRenderTargetProxy.get());
1844
Brian Salomon97180af2017-03-14 13:42:58 -04001845 op->setClippedBounds(bounds);
Robert Phillips33f83152017-05-18 20:57:43 +00001846 return this->getOpList()->addOp(std::move(op), this);
Brian Salomon467921e2017-03-06 16:17:12 -05001847}
1848
Robert Phillipsc3757042017-05-17 13:00:14 +00001849bool GrRenderTargetContext::setupDstTexture(GrRenderTargetProxy* rtProxy, const GrClip& clip,
Brian Salomon467921e2017-03-06 16:17:12 -05001850 const SkRect& opBounds,
Robert Phillipsc3757042017-05-17 13:00:14 +00001851 GrXferProcessor::DstTexture* dstTexture) {
Brian Salomon467921e2017-03-06 16:17:12 -05001852 if (this->caps()->textureBarrierSupport()) {
Robert Phillipsbf25d432017-04-07 10:08:53 -04001853 if (GrTextureProxy* texProxy = rtProxy->asTextureProxy()) {
Robert Phillipsc3757042017-05-17 13:00:14 +00001854 // MDB TODO: remove this instantiation. Blocked on making DstTexture be proxy-based
Brian Salomonbb5711a2017-05-17 13:49:59 -04001855 sk_sp<GrTexture> tex(
1856 sk_ref_sp(texProxy->instantiateTexture(fContext->resourceProvider())));
Robert Phillipsc3757042017-05-17 13:00:14 +00001857 if (!tex) {
1858 SkDebugf("setupDstTexture: instantiation of src texture failed.\n");
1859 return false; // We have bigger problems now
1860 }
1861
Brian Salomon467921e2017-03-06 16:17:12 -05001862 // The render target is a texture, so we can read from it directly in the shader. The XP
1863 // will be responsible to detect this situation and request a texture barrier.
Robert Phillipsc3757042017-05-17 13:00:14 +00001864 dstTexture->setTexture(std::move(tex));
1865 dstTexture->setOffset(0, 0);
Robert Phillipsbf25d432017-04-07 10:08:53 -04001866 return true;
Brian Salomon467921e2017-03-06 16:17:12 -05001867 }
1868 }
1869
Robert Phillipsbf25d432017-04-07 10:08:53 -04001870 SkIRect copyRect = SkIRect::MakeWH(rtProxy->width(), rtProxy->height());
Brian Salomon467921e2017-03-06 16:17:12 -05001871
Eric Karl74480882017-04-03 14:49:05 -07001872 SkIRect clippedRect;
Robert Phillipsbf25d432017-04-07 10:08:53 -04001873 clip.getConservativeBounds(rtProxy->width(), rtProxy->height(), &clippedRect);
Eric Karl72e551e2017-04-04 13:42:10 -07001874 SkIRect drawIBounds;
Brian Salomon467921e2017-03-06 16:17:12 -05001875 opBounds.roundOut(&drawIBounds);
Brian Salomon859621f2017-03-16 09:21:54 -04001876 // Cover up for any precision issues by outsetting the op bounds a pixel in each direction.
1877 drawIBounds.outset(1, 1);
Eric Karl72e551e2017-04-04 13:42:10 -07001878 if (!clippedRect.intersect(drawIBounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -05001879#ifdef SK_DEBUG
Robert Phillipsbf25d432017-04-07 10:08:53 -04001880 GrCapsDebugf(this->caps(), "setupDstTexture: Missed an early reject bailing on draw.");
Brian Salomon467921e2017-03-06 16:17:12 -05001881#endif
Robert Phillipsbf25d432017-04-07 10:08:53 -04001882 return false;
Brian Salomon467921e2017-03-06 16:17:12 -05001883 }
1884
1885 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
1886 // have per-sample dst values by making the copy multisampled.
1887 GrSurfaceDesc desc;
Eric Karl74480882017-04-03 14:49:05 -07001888 bool rectsMustMatch = false;
1889 bool disallowSubrect = false;
Robert Phillipsbf25d432017-04-07 10:08:53 -04001890 if (!this->caps()->initDescForDstCopy(rtProxy, &desc, &rectsMustMatch, &disallowSubrect)) {
1891 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
Brian Salomon467921e2017-03-06 16:17:12 -05001892 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillipsbf25d432017-04-07 10:08:53 -04001893 desc.fConfig = rtProxy->config();
Brian Salomon467921e2017-03-06 16:17:12 -05001894 }
1895
Eric Karl74480882017-04-03 14:49:05 -07001896 if (!disallowSubrect) {
1897 copyRect = clippedRect;
1898 }
Brian Salomon467921e2017-03-06 16:17:12 -05001899
Robert Phillipsbf25d432017-04-07 10:08:53 -04001900 SkIPoint dstPoint, dstOffset;
1901 SkBackingFit fit;
Eric Karl74480882017-04-03 14:49:05 -07001902 if (rectsMustMatch) {
Robert Phillipsbf25d432017-04-07 10:08:53 -04001903 SkASSERT(desc.fOrigin == rtProxy->origin());
1904 desc.fWidth = rtProxy->width();
1905 desc.fHeight = rtProxy->height();
Eric Karl74480882017-04-03 14:49:05 -07001906 dstPoint = {copyRect.fLeft, copyRect.fTop};
1907 dstOffset = {0, 0};
Robert Phillipsbf25d432017-04-07 10:08:53 -04001908 fit = SkBackingFit::kExact;
Eric Karl74480882017-04-03 14:49:05 -07001909 } else {
1910 desc.fWidth = copyRect.width();
1911 desc.fHeight = copyRect.height();
1912 dstPoint = {0, 0};
1913 dstOffset = {copyRect.fLeft, copyRect.fTop};
Robert Phillipsbf25d432017-04-07 10:08:53 -04001914 fit = SkBackingFit::kApprox;
Eric Karl74480882017-04-03 14:49:05 -07001915 }
Brian Salomon467921e2017-03-06 16:17:12 -05001916
Robert Phillipsbf25d432017-04-07 10:08:53 -04001917 sk_sp<GrSurfaceContext> sContext = fContext->contextPriv().makeDeferredSurfaceContext(
1918 desc,
1919 fit,
1920 SkBudgeted::kYes);
1921 if (!sContext) {
1922 SkDebugf("setupDstTexture: surfaceContext creation failed.\n");
1923 return false;
1924 }
1925
1926 if (!sContext->copy(rtProxy, copyRect, dstPoint)) {
1927 SkDebugf("setupDstTexture: copy failed.\n");
1928 return false;
1929 }
1930
Robert Phillipsc3757042017-05-17 13:00:14 +00001931 GrTextureProxy* copyProxy = sContext->asTextureProxy();
1932 // MDB TODO: remove this instantiation once DstTexture is proxy-backed
Brian Salomonbb5711a2017-05-17 13:49:59 -04001933 sk_sp<GrTexture> copy(sk_ref_sp(copyProxy->instantiateTexture(fContext->resourceProvider())));
Robert Phillipsc3757042017-05-17 13:00:14 +00001934 if (!copy) {
1935 SkDebugf("setupDstTexture: instantiation of copied texture failed.\n");
1936 return false;
1937 }
1938
1939 dstTexture->setTexture(std::move(copy));
1940 dstTexture->setOffset(dstOffset);
Robert Phillipsbf25d432017-04-07 10:08:53 -04001941 return true;
robertphillips2334fb62015-06-17 05:43:33 -07001942}