blob: 7d18448897a4214fad3c28b4f04721590bc3dc60 [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 Phillips5efd5ea2017-05-30 13:47:32 -0400203 this->getOpList()->addOp(std::move(op), *this->caps());
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 Phillips5efd5ea2017-05-30 13:47:32 -0400262 std::unique_ptr<GrOp> op(GrClearOp::Make(rtRect, color, !clearRect));
Brian Salomonfc527d22016-12-14 21:07:01 -0500263 if (!op) {
Robert Phillips784b7bf2016-12-09 13:35:02 -0500264 return;
265 }
Robert Phillips5efd5ea2017-05-30 13:47:32 -0400266 fRenderTargetContext->getOpList()->addOp(std::move(op), *fRenderTargetContext->caps());
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 Phillips5efd5ea2017-05-30 13:47:32 -0400310 this->getOpList()->fullClear(*this->caps(), color);
robertphillips9199a9f2016-07-13 07:48:43 -0700311 } else {
Robert Phillips5efd5ea2017-05-30 13:47:32 -0400312 std::unique_ptr<GrOp> op(GrClearOp::Make(clip, color, this->asSurfaceProxy()));
Brian Salomon42521e82016-12-07 16:44:58 -0500313 if (!op) {
csmartdalton29df7602016-08-31 11:55:52 -0700314 return;
315 }
Robert Phillips5efd5ea2017-05-30 13:47:32 -0400316 this->getOpList()->addOp(std::move(op), *this->caps());
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 Phillips5efd5ea2017-05-30 13:47:32 -0400613 fRenderTargetContext->getOpList()->addOp(std::move(op), *fRenderTargetContext->caps());
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 Phillips5efd5ea2017-05-30 13:47:32 -0400671 fRenderTargetContext->getOpList()->addOp(std::move(op), *fRenderTargetContext->caps());
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,
Brian Osmanae0c50c2017-05-25 16:56:34 -0400839 sk_sp<SkVertices> vertices,
840 GrPrimitiveType* overridePrimType) {
Brian Salomon199fb872017-02-06 09:41:10 -0500841 ASSERT_SINGLE_OWNER
842 RETURN_IF_ABANDONED
843 SkDEBUGCODE(this->validate();)
844 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawVertices");
845
846 AutoCheckFlush acf(this->drawingManager());
847
848 SkASSERT(vertices);
Brian Osmanae0c50c2017-05-25 16:56:34 -0400849 std::unique_ptr<GrLegacyMeshDrawOp> op = GrDrawVerticesOp::Make(paint.getColor(),
850 std::move(vertices), viewMatrix,
851 overridePrimType);
Brian Salomon199fb872017-02-06 09:41:10 -0500852 if (!op) {
853 return;
854 }
Brian Salomon82f44312017-01-11 13:42:54 -0500855 GrPipelineBuilder pipelineBuilder(std::move(paint), GrAAType::kNone);
Brian Salomone14bd802017-04-04 15:13:25 -0400856 this->addLegacyMeshDrawOp(std::move(pipelineBuilder), clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -0700857}
858
859///////////////////////////////////////////////////////////////////////////////
860
Brian Osman11052242016-10-27 14:47:55 -0400861void GrRenderTargetContext::drawAtlas(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500862 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400863 const SkMatrix& viewMatrix,
864 int spriteCount,
865 const SkRSXform xform[],
866 const SkRect texRect[],
867 const SkColor colors[]) {
joshualitt1de610a2016-01-06 08:26:09 -0800868 ASSERT_SINGLE_OWNER
jvanverth31ff7622015-08-07 10:09:28 -0700869 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700870 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400871 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawAtlas");
robertphillips2e1e51f2015-10-15 08:01:48 -0700872
Robert Phillips72152832017-01-25 17:31:35 -0500873 AutoCheckFlush acf(this->drawingManager());
halcanary9d524f22016-03-29 09:03:52 -0700874
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400875 std::unique_ptr<GrLegacyMeshDrawOp> op =
Brian Salomonfc527d22016-12-14 21:07:01 -0500876 GrDrawAtlasOp::Make(paint.getColor(), viewMatrix, spriteCount, xform, texRect, colors);
Brian Salomon82f44312017-01-11 13:42:54 -0500877 GrPipelineBuilder pipelineBuilder(std::move(paint), GrAAType::kNone);
Brian Salomone14bd802017-04-04 15:13:25 -0400878 this->addLegacyMeshDrawOp(std::move(pipelineBuilder), clip, std::move(op));
jvanverth31ff7622015-08-07 10:09:28 -0700879}
880
881///////////////////////////////////////////////////////////////////////////////
882
Brian Osman11052242016-10-27 14:47:55 -0400883void GrRenderTargetContext::drawRRect(const GrClip& origClip,
Brian Salomon82f44312017-01-11 13:42:54 -0500884 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500885 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400886 const SkMatrix& viewMatrix,
887 const SkRRect& rrect,
888 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -0800889 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700890 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700891 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400892 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawRRect");
robertphillipsea461502015-05-26 11:38:03 -0700893 if (rrect.isEmpty()) {
894 return;
895 }
896
bsalomon7f0d9f32016-08-15 14:49:10 -0700897 GrNoClip noclip;
898 const GrClip* clip = &origClip;
899#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
900 // The Android framework frequently clips rrects to themselves where the clip is non-aa and the
Brian Salomon42521e82016-12-07 16:44:58 -0500901 // draw is aa. Since our lower level clip code works from op bounds, which are SkRects, it
bsalomon7f0d9f32016-08-15 14:49:10 -0700902 // doesn't detect that the clip can be ignored (modulo antialiasing). The following test
903 // attempts to mitigate the stencil clip cost but will only help when the entire clip stack
904 // can be ignored. We'd prefer to fix this in the framework by removing the clips calls.
905 SkRRect devRRect;
906 if (rrect.transform(viewMatrix, &devRRect) && clip->quickContains(devRRect)) {
907 clip = &noclip;
908 }
909#endif
bsalomon6663acf2016-05-10 09:14:17 -0700910 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
ksakamotoec7f2ac2016-07-05 03:54:53 -0700911
Robert Phillips72152832017-01-25 17:31:35 -0500912 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -0700913 const SkStrokeRec stroke = style.strokeRec();
csmartdaltona7f29642016-07-07 08:49:11 -0700914
csmartdaltone0d36292016-07-29 08:14:20 -0700915 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport() &&
916 stroke.isFillStyle()) {
Robert Phillipse3302df2017-04-24 07:31:02 -0400917 gr_instanced::OpAllocator* oa = this->drawingManager()->instancingAllocator();
Brian Salomon54d212e2017-03-21 14:22:38 -0400918 std::unique_ptr<GrDrawOp> op(
Robert Phillipse3302df2017-04-24 07:31:02 -0400919 oa->recordRRect(rrect, viewMatrix, std::move(paint), aa, fInstancedPipelineInfo));
Brian Salomon42521e82016-12-07 16:44:58 -0500920 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -0400921 this->addDrawOp(*clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -0700922 return;
923 }
924 }
925
Brian Salomon7c8460e2017-05-12 11:36:10 -0400926 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500927 if (GrAAType::kCoverage == aaType) {
Brian Salomon94efbf52016-11-29 13:43:05 -0500928 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomon05441c42017-05-15 16:45:49 -0400929 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeRRectOp(std::move(paint),
930 paint.usesDistanceVectorField(),
931 viewMatrix,
932 rrect,
933 stroke,
934 shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -0500935 if (op) {
Brian Salomon05441c42017-05-15 16:45:49 -0400936 this->addDrawOp(*clip, std::move(op));
robertphillipsb56f9272016-02-25 11:03:52 -0800937 return;
938 }
robertphillipsea461502015-05-26 11:38:03 -0700939 }
robertphillipsb56f9272016-02-25 11:03:52 -0800940
941 SkPath path;
942 path.setIsVolatile(true);
943 path.addRRect(rrect);
Brian Salomon82f44312017-01-11 13:42:54 -0500944 this->internalDrawPath(*clip, std::move(paint), aa, viewMatrix, path, style);
robertphillipsea461502015-05-26 11:38:03 -0700945}
946
Jim Van Verthc5903412016-11-17 15:27:09 -0500947///////////////////////////////////////////////////////////////////////////////
948
Jim Van Verth3af1af92017-05-18 15:06:54 -0400949static SkPoint3 map(const SkMatrix& m, const SkPoint3& pt) {
950 SkPoint3 result;
951 m.mapXY(pt.fX, pt.fY, (SkPoint*)&result.fX);
952 result.fZ = pt.fZ;
953 return result;
954}
955
956bool GrRenderTargetContext::drawFastShadow(const GrClip& clip,
957 GrPaint&& paint,
958 const SkMatrix& viewMatrix,
959 const SkPath& path,
960 const SkDrawShadowRec& rec) {
Jim Van Verthc5903412016-11-17 15:27:09 -0500961 ASSERT_SINGLE_OWNER
Jim Van Verth3af1af92017-05-18 15:06:54 -0400962 if (this->drawingManager()->wasAbandoned()) {
963 return true;
964 }
Jim Van Verthc5903412016-11-17 15:27:09 -0500965 SkDEBUGCODE(this->validate();)
Jim Van Verth3af1af92017-05-18 15:06:54 -0400966 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawFastShadow");
967
968 // check z plane
969 bool tiltZPlane = SkToBool(!SkScalarNearlyZero(rec.fZPlaneParams.fX) ||
970 !SkScalarNearlyZero(rec.fZPlaneParams.fY));
971 bool skipAnalytic = SkToBool(rec.fFlags & SkShadowFlags::kGeometricOnly_ShadowFlag);
972 if (tiltZPlane || skipAnalytic || !viewMatrix.rectStaysRect() || !viewMatrix.isSimilarity()) {
973 return false;
974 }
975
976 SkRRect rrect;
977 SkRect rect;
978 // we can only handle rects, circles, and rrects with circular corners
979 bool isRRect = path.isRRect(&rrect) && rrect.isSimpleCircular() &&
980 rrect.radii(SkRRect::kUpperLeft_Corner).fX > SK_ScalarNearlyZero;
981 if (!isRRect &&
982 path.isOval(&rect) && SkScalarNearlyEqual(rect.width(), rect.height()) &&
983 rect.width() > SK_ScalarNearlyZero) {
984 rrect.setOval(rect);
985 isRRect = true;
986 }
987 if (!isRRect && path.isRect(&rect)) {
988 rrect.setRect(rect);
989 isRRect = true;
990 }
991
992 if (!isRRect) {
993 return false;
994 }
995
Jim Van Verthc5903412016-11-17 15:27:09 -0500996 if (rrect.isEmpty()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -0400997 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -0500998 }
999
Robert Phillips72152832017-01-25 17:31:35 -05001000 AutoCheckFlush acf(this->drawingManager());
Jim Van Verthc5903412016-11-17 15:27:09 -05001001
Jim Van Verth3af1af92017-05-18 15:06:54 -04001002 // transform light
1003 SkPoint3 devLightPos = map(viewMatrix, rec.fLightPos);
1004
1005 // 1/scale
1006 SkScalar devToSrcScale = viewMatrix.isScaleTranslate() ?
1007 SkScalarInvert(viewMatrix[SkMatrix::kMScaleX]) :
1008 sk_float_rsqrt(viewMatrix[SkMatrix::kMScaleX] * viewMatrix[SkMatrix::kMScaleX] +
1009 viewMatrix[SkMatrix::kMSkewX] * viewMatrix[SkMatrix::kMSkewX]);
1010
1011 SkScalar occluderHeight = rec.fZPlaneParams.fZ;
1012 GrColor4f color = paint.getColor4f();
1013 bool transparent = SkToBool(rec.fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag);
1014
1015 if (rec.fAmbientAlpha > 0) {
1016 static constexpr float kHeightFactor = 1.0f / 128.0f;
1017 static constexpr float kGeomFactor = 64.0f;
1018
1019 SkScalar devSpaceInsetWidth = occluderHeight * kHeightFactor * kGeomFactor;
1020 const float umbraAlpha = (1.0f + SkTMax(occluderHeight * kHeightFactor, 0.0f));
1021 const SkScalar devSpaceAmbientBlur = devSpaceInsetWidth * umbraAlpha;
1022
1023 // Outset the shadow rrect to the border of the penumbra
1024 SkScalar ambientPathOutset = devSpaceInsetWidth * devToSrcScale;
1025 SkRRect ambientRRect;
1026 SkRect outsetRect = rrect.rect().makeOutset(ambientPathOutset, ambientPathOutset);
1027 // If the rrect was an oval then its outset will also be one.
1028 // We set it explicitly to avoid errors.
1029 if (rrect.isOval()) {
1030 ambientRRect = SkRRect::MakeOval(outsetRect);
1031 } else {
1032 SkScalar outsetRad = rrect.getSimpleRadii().fX + ambientPathOutset;
1033 ambientRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1034 }
1035
1036 GrColor ambientColor = color.mulByScalar(rec.fAmbientAlpha).toGrColor();
1037 if (transparent) {
1038 // set a large inset to force a fill
1039 devSpaceInsetWidth = ambientRRect.width();
1040 }
1041 // the fraction of the blur we want to apply is devSpaceInsetWidth/devSpaceAmbientBlur,
1042 // which is just 1/umbraAlpha.
1043 SkScalar blurClamp = SkScalarInvert(umbraAlpha);
1044
1045 std::unique_ptr<GrLegacyMeshDrawOp> op = GrShadowRRectOp::Make(ambientColor, viewMatrix,
1046 ambientRRect,
1047 devSpaceAmbientBlur,
1048 devSpaceInsetWidth,
1049 blurClamp);
1050 if (op) {
1051 GrPipelineBuilder pipelineBuilder(std::move(paint), GrAAType::kNone);
1052 this->addLegacyMeshDrawOp(std::move(pipelineBuilder), clip, std::move(op));
1053 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001054 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001055
1056 if (rec.fSpotAlpha > 0) {
1057 float zRatio = SkTPin(occluderHeight / (devLightPos.fZ - occluderHeight), 0.0f, 0.95f);
1058
1059 SkScalar devSpaceSpotBlur = 2.0f * rec.fLightRadius * zRatio;
1060 // handle scale of radius and pad due to CTM
1061 const SkScalar srcSpaceSpotBlur = devSpaceSpotBlur * devToSrcScale;
1062
1063 // Compute the scale and translation for the spot shadow.
1064 const SkScalar spotScale = devLightPos.fZ / (devLightPos.fZ - occluderHeight);
1065 SkPoint spotOffset = SkPoint::Make(zRatio*(-devLightPos.fX), zRatio*(-devLightPos.fY));
1066 // Adjust translate for the effect of the scale.
1067 spotOffset.fX += spotScale*viewMatrix[SkMatrix::kMTransX];
1068 spotOffset.fY += spotScale*viewMatrix[SkMatrix::kMTransY];
1069 // This offset is in dev space, need to transform it into source space.
1070 SkMatrix ctmInverse;
1071 if (viewMatrix.invert(&ctmInverse)) {
1072 ctmInverse.mapPoints(&spotOffset, 1);
1073 } else {
1074 // Since the matrix is a similarity, this should never happen, but just in case...
1075 SkDebugf("Matrix is degenerate. Will not render spot shadow correctly!\n");
1076 SkASSERT(false);
1077 }
1078
1079 // Compute the transformed shadow rrect
1080 SkRRect spotShadowRRect;
1081 SkMatrix shadowTransform;
1082 shadowTransform.setScaleTranslate(spotScale, spotScale, spotOffset.fX, spotOffset.fY);
1083 rrect.transform(shadowTransform, &spotShadowRRect);
1084 SkScalar spotRadius = spotShadowRRect.getSimpleRadii().fX;
1085
1086 // Compute the insetWidth
1087 SkScalar blurOutset = 0.5f*srcSpaceSpotBlur;
1088 SkScalar insetWidth = blurOutset;
1089 if (transparent) {
1090 // If transparent, just do a fill
1091 insetWidth += spotShadowRRect.width();
1092 } else {
1093 // For shadows, instead of using a stroke we specify an inset from the penumbra
1094 // border. We want to extend this inset area so that it meets up with the caster
1095 // geometry. The inset geometry will by default already be inset by the blur width.
1096 //
1097 // We compare the min and max corners inset by the radius between the original
1098 // rrect and the shadow rrect. The distance between the two plus the difference
1099 // between the scaled radius and the original radius gives the distance from the
1100 // transformed shadow shape to the original shape in that corner. The max
1101 // of these gives the maximum distance we need to cover.
1102 //
1103 // Since we are outsetting by 1/2 the blur distance, we just add the maxOffset to
1104 // that to get the full insetWidth.
1105 SkScalar maxOffset;
1106 if (rrect.isRect()) {
1107 // Manhattan distance works better for rects
1108 maxOffset = SkTMax(SkTMax(SkTAbs(spotShadowRRect.rect().fLeft -
1109 rrect.rect().fLeft),
1110 SkTAbs(spotShadowRRect.rect().fTop -
1111 rrect.rect().fTop)),
1112 SkTMax(SkTAbs(spotShadowRRect.rect().fRight -
1113 rrect.rect().fRight),
1114 SkTAbs(spotShadowRRect.rect().fBottom -
1115 rrect.rect().fBottom)));
1116 } else {
1117 SkScalar dr = spotRadius - rrect.getSimpleRadii().fX;
1118 SkPoint upperLeftOffset = SkPoint::Make(spotShadowRRect.rect().fLeft -
1119 rrect.rect().fLeft + dr,
1120 spotShadowRRect.rect().fTop -
1121 rrect.rect().fTop + dr);
1122 SkPoint lowerRightOffset = SkPoint::Make(spotShadowRRect.rect().fRight -
1123 rrect.rect().fRight - dr,
1124 spotShadowRRect.rect().fBottom -
1125 rrect.rect().fBottom - dr);
1126 maxOffset = SkScalarSqrt(SkTMax(upperLeftOffset.lengthSqd(),
1127 lowerRightOffset.lengthSqd())) + dr;
1128 }
1129 insetWidth += maxOffset;
1130 }
1131
1132 // Outset the shadow rrect to the border of the penumbra
1133 SkRect outsetRect = spotShadowRRect.rect().makeOutset(blurOutset, blurOutset);
1134 if (spotShadowRRect.isOval()) {
1135 spotShadowRRect = SkRRect::MakeOval(outsetRect);
1136 } else {
1137 SkScalar outsetRad = spotRadius + blurOutset;
1138 spotShadowRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1139 }
1140
1141 GrColor spotColor = color.mulByScalar(rec.fSpotAlpha).toGrColor();
1142 std::unique_ptr<GrLegacyMeshDrawOp> op = GrShadowRRectOp::Make(spotColor, viewMatrix,
1143 spotShadowRRect,
1144 devSpaceSpotBlur,
1145 insetWidth);
1146 if (op) {
1147 GrPipelineBuilder pipelineBuilder(std::move(paint), GrAAType::kNone);
1148 this->addLegacyMeshDrawOp(std::move(pipelineBuilder), clip, std::move(op));
1149 }
1150 }
1151
1152 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001153}
1154
1155///////////////////////////////////////////////////////////////////////////////
1156
Brian Osman11052242016-10-27 14:47:55 -04001157bool GrRenderTargetContext::drawFilledDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001158 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001159 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001160 const SkMatrix& viewMatrix,
1161 const SkRRect& origOuter,
1162 const SkRRect& origInner) {
robertphillips00095892016-02-29 13:50:40 -08001163 SkASSERT(!origInner.isEmpty());
1164 SkASSERT(!origOuter.isEmpty());
1165
csmartdaltone0d36292016-07-29 08:14:20 -07001166 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport()) {
Robert Phillipse3302df2017-04-24 07:31:02 -04001167 gr_instanced::OpAllocator* oa = this->drawingManager()->instancingAllocator();
1168 std::unique_ptr<GrDrawOp> op(oa->recordDRRect(
Brian Salomon54d212e2017-03-21 14:22:38 -04001169 origOuter, origInner, viewMatrix, std::move(paint), aa, fInstancedPipelineInfo));
Brian Salomon42521e82016-12-07 16:44:58 -05001170 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -04001171 this->addDrawOp(clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -07001172 return true;
1173 }
1174 }
1175
Brian Salomon7c8460e2017-05-12 11:36:10 -04001176 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
robertphillips00095892016-02-29 13:50:40 -08001177
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001178 GrPrimitiveEdgeType innerEdgeType, outerEdgeType;
1179 if (GrAAType::kCoverage == aaType) {
1180 innerEdgeType = kInverseFillAA_GrProcessorEdgeType;
1181 outerEdgeType = kFillAA_GrProcessorEdgeType;
1182 } else {
1183 innerEdgeType = kInverseFillBW_GrProcessorEdgeType;
1184 outerEdgeType = kFillBW_GrProcessorEdgeType;
1185 }
robertphillips00095892016-02-29 13:50:40 -08001186
1187 SkTCopyOnFirstWrite<SkRRect> inner(origInner), outer(origOuter);
1188 SkMatrix inverseVM;
1189 if (!viewMatrix.isIdentity()) {
1190 if (!origInner.transform(viewMatrix, inner.writable())) {
1191 return false;
1192 }
1193 if (!origOuter.transform(viewMatrix, outer.writable())) {
1194 return false;
1195 }
1196 if (!viewMatrix.invert(&inverseVM)) {
1197 return false;
1198 }
1199 } else {
1200 inverseVM.reset();
halcanary9d524f22016-03-29 09:03:52 -07001201 }
robertphillips00095892016-02-29 13:50:40 -08001202
robertphillips00095892016-02-29 13:50:40 -08001203 // TODO these need to be a geometry processors
bungeman06ca8ec2016-06-09 08:01:03 -07001204 sk_sp<GrFragmentProcessor> innerEffect(GrRRectEffect::Make(innerEdgeType, *inner));
robertphillips00095892016-02-29 13:50:40 -08001205 if (!innerEffect) {
1206 return false;
1207 }
1208
bungeman06ca8ec2016-06-09 08:01:03 -07001209 sk_sp<GrFragmentProcessor> outerEffect(GrRRectEffect::Make(outerEdgeType, *outer));
robertphillips00095892016-02-29 13:50:40 -08001210 if (!outerEffect) {
1211 return false;
1212 }
1213
Brian Salomon82f44312017-01-11 13:42:54 -05001214 paint.addCoverageFragmentProcessor(std::move(innerEffect));
1215 paint.addCoverageFragmentProcessor(std::move(outerEffect));
robertphillips00095892016-02-29 13:50:40 -08001216
1217 SkRect bounds = outer->getBounds();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001218 if (GrAAType::kCoverage == aaType) {
robertphillips00095892016-02-29 13:50:40 -08001219 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1220 }
halcanary9d524f22016-03-29 09:03:52 -07001221
Brian Salomon82f44312017-01-11 13:42:54 -05001222 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), bounds,
1223 inverseVM);
robertphillips00095892016-02-29 13:50:40 -08001224 return true;
1225}
1226
Brian Osman11052242016-10-27 14:47:55 -04001227void GrRenderTargetContext::drawDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001228 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001229 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001230 const SkMatrix& viewMatrix,
1231 const SkRRect& outer,
1232 const SkRRect& inner) {
robertphillips00095892016-02-29 13:50:40 -08001233 ASSERT_SINGLE_OWNER
1234 RETURN_IF_ABANDONED
1235 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -04001236 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawDRRect");
robertphillips00095892016-02-29 13:50:40 -08001237
1238 SkASSERT(!outer.isEmpty());
1239 SkASSERT(!inner.isEmpty());
1240
Robert Phillips72152832017-01-25 17:31:35 -05001241 AutoCheckFlush acf(this->drawingManager());
robertphillips00095892016-02-29 13:50:40 -08001242
Brian Salomon82f44312017-01-11 13:42:54 -05001243 if (this->drawFilledDRRect(clip, std::move(paint), aa, viewMatrix, outer, inner)) {
robertphillips00095892016-02-29 13:50:40 -08001244 return;
1245 }
1246
1247 SkPath path;
1248 path.setIsVolatile(true);
1249 path.addRRect(inner);
1250 path.addRRect(outer);
1251 path.setFillType(SkPath::kEvenOdd_FillType);
1252
Brian Salomon82f44312017-01-11 13:42:54 -05001253 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, GrStyle::SimpleFill());
robertphillips00095892016-02-29 13:50:40 -08001254}
1255
robertphillipsea461502015-05-26 11:38:03 -07001256///////////////////////////////////////////////////////////////////////////////
1257
msarettcc319b92016-08-25 18:07:18 -07001258static inline bool is_int(float x) {
1259 return x == (float) sk_float_round2int(x);
1260}
1261
Brian Osman11052242016-10-27 14:47:55 -04001262void GrRenderTargetContext::drawRegion(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001263 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001264 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001265 const SkMatrix& viewMatrix,
1266 const SkRegion& region,
1267 const GrStyle& style) {
msarettcc319b92016-08-25 18:07:18 -07001268 ASSERT_SINGLE_OWNER
1269 RETURN_IF_ABANDONED
1270 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -04001271 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawRegion");
msarettcc319b92016-08-25 18:07:18 -07001272
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001273 if (GrAA::kYes == aa) {
Brian Salomonfc527d22016-12-14 21:07:01 -05001274 // GrRegionOp performs no antialiasing but is much faster, so here we check the matrix
Brian Salomonc57c7c92016-12-06 14:47:34 -05001275 // to see whether aa is really required.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001276 if (!SkToBool(viewMatrix.getType() & ~(SkMatrix::kTranslate_Mask)) &&
1277 is_int(viewMatrix.getTranslateX()) &&
1278 is_int(viewMatrix.getTranslateY())) {
1279 aa = GrAA::kNo;
1280 }
Brian Salomonc57c7c92016-12-06 14:47:34 -05001281 }
msarettcc319b92016-08-25 18:07:18 -07001282 bool complexStyle = !style.isSimpleFill();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001283 if (complexStyle || GrAA::kYes == aa) {
msarettcc319b92016-08-25 18:07:18 -07001284 SkPath path;
1285 region.getBoundaryPath(&path);
Brian Salomon82f44312017-01-11 13:42:54 -05001286 return this->drawPath(clip, std::move(paint), aa, viewMatrix, path, style);
msarettcc319b92016-08-25 18:07:18 -07001287 }
1288
Brian Salomond3ccb0a2017-04-03 10:38:00 -04001289 std::unique_ptr<GrLegacyMeshDrawOp> op = GrRegionOp::Make(paint.getColor(), viewMatrix, region);
Brian Salomon82f44312017-01-11 13:42:54 -05001290 GrPipelineBuilder pipelineBuilder(std::move(paint), GrAAType::kNone);
Brian Salomone14bd802017-04-04 15:13:25 -04001291 this->addLegacyMeshDrawOp(std::move(pipelineBuilder), clip, std::move(op));
msarettcc319b92016-08-25 18:07:18 -07001292}
1293
Brian Osman11052242016-10-27 14:47:55 -04001294void GrRenderTargetContext::drawOval(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001295 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001296 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001297 const SkMatrix& viewMatrix,
1298 const SkRect& oval,
1299 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001300 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001301 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001302 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -04001303 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawOval");
robertphillips2e1e51f2015-10-15 08:01:48 -07001304
robertphillipsea461502015-05-26 11:38:03 -07001305 if (oval.isEmpty()) {
1306 return;
1307 }
1308
bsalomon6663acf2016-05-10 09:14:17 -07001309 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
robertphillipsea461502015-05-26 11:38:03 -07001310
Robert Phillips72152832017-01-25 17:31:35 -05001311 AutoCheckFlush acf(this->drawingManager());
bsalomon6663acf2016-05-10 09:14:17 -07001312 const SkStrokeRec& stroke = style.strokeRec();
csmartdaltona7f29642016-07-07 08:49:11 -07001313
csmartdaltone0d36292016-07-29 08:14:20 -07001314 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport() &&
1315 stroke.isFillStyle()) {
Robert Phillipse3302df2017-04-24 07:31:02 -04001316 gr_instanced::OpAllocator* oa = this->drawingManager()->instancingAllocator();
Brian Salomon54d212e2017-03-21 14:22:38 -04001317 std::unique_ptr<GrDrawOp> op(
Robert Phillipse3302df2017-04-24 07:31:02 -04001318 oa->recordOval(oval, viewMatrix, std::move(paint), aa, fInstancedPipelineInfo));
Brian Salomon42521e82016-12-07 16:44:58 -05001319 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -04001320 this->addDrawOp(clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -07001321 return;
1322 }
1323 }
1324
Brian Salomon7c8460e2017-05-12 11:36:10 -04001325 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001326 if (GrAAType::kCoverage == aaType) {
Brian Salomon94efbf52016-11-29 13:43:05 -05001327 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomon05441c42017-05-15 16:45:49 -04001328 std::unique_ptr<GrDrawOp> op =
1329 GrOvalOpFactory::MakeOvalOp(std::move(paint), viewMatrix, oval, stroke, shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -05001330 if (op) {
Brian Salomon05441c42017-05-15 16:45:49 -04001331 this->addDrawOp(clip, std::move(op));
robertphillipsb56f9272016-02-25 11:03:52 -08001332 return;
1333 }
robertphillipsea461502015-05-26 11:38:03 -07001334 }
robertphillipsb56f9272016-02-25 11:03:52 -08001335
1336 SkPath path;
1337 path.setIsVolatile(true);
1338 path.addOval(oval);
Brian Salomon82f44312017-01-11 13:42:54 -05001339 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, style);
robertphillipsea461502015-05-26 11:38:03 -07001340}
1341
Brian Osman11052242016-10-27 14:47:55 -04001342void GrRenderTargetContext::drawArc(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001343 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001344 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001345 const SkMatrix& viewMatrix,
1346 const SkRect& oval,
1347 SkScalar startAngle,
1348 SkScalar sweepAngle,
1349 bool useCenter,
1350 const GrStyle& style) {
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001351 ASSERT_SINGLE_OWNER
1352 RETURN_IF_ABANDONED
1353 SkDEBUGCODE(this->validate();)
1354 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawArc");
1355
1356 AutoCheckFlush acf(this->drawingManager());
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 = GrOvalOpFactory::MakeArcOp(std::move(paint),
1362 viewMatrix,
1363 oval,
1364 startAngle,
1365 sweepAngle,
1366 useCenter,
1367 style,
1368 shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -05001369 if (op) {
Brian Salomon05441c42017-05-15 16:45:49 -04001370 this->addDrawOp(clip, std::move(op));
bsalomon4f3a0ca2016-08-22 13:14:26 -07001371 return;
1372 }
1373 }
1374 SkPath path;
bsalomon21af9ca2016-08-25 12:29:23 -07001375 SkPathPriv::CreateDrawArcPath(&path, oval, startAngle, sweepAngle, useCenter,
1376 style.isSimpleFill());
Brian Salomon82f44312017-01-11 13:42:54 -05001377 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, style);
bsalomon4f3a0ca2016-08-22 13:14:26 -07001378}
1379
Brian Osman11052242016-10-27 14:47:55 -04001380void GrRenderTargetContext::drawImageLattice(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001381 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001382 const SkMatrix& viewMatrix,
1383 int imageWidth,
1384 int imageHeight,
1385 std::unique_ptr<SkLatticeIter> iter,
1386 const SkRect& dst) {
joshualitt1de610a2016-01-06 08:26:09 -08001387 ASSERT_SINGLE_OWNER
joshualitt33a5fce2015-11-18 13:28:51 -08001388 RETURN_IF_ABANDONED
1389 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -04001390 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawImageLattice");
joshualitt33a5fce2015-11-18 13:28:51 -08001391
Robert Phillips72152832017-01-25 17:31:35 -05001392 AutoCheckFlush acf(this->drawingManager());
joshualitt33a5fce2015-11-18 13:28:51 -08001393
Brian Salomond3ccb0a2017-04-03 10:38:00 -04001394 std::unique_ptr<GrLegacyMeshDrawOp> op = GrLatticeOp::MakeNonAA(
Brian Salomon649a3412017-03-09 13:50:43 -05001395 paint.getColor(), viewMatrix, imageWidth, imageHeight, std::move(iter), dst);
joshualitt33a5fce2015-11-18 13:28:51 -08001396
Brian Salomon82f44312017-01-11 13:42:54 -05001397 GrPipelineBuilder pipelineBuilder(std::move(paint), GrAAType::kNone);
Brian Salomone14bd802017-04-04 15:13:25 -04001398 this->addLegacyMeshDrawOp(std::move(pipelineBuilder), clip, std::move(op));
joshualitt33a5fce2015-11-18 13:28:51 -08001399}
1400
Brian Osman11052242016-10-27 14:47:55 -04001401void GrRenderTargetContext::prepareForExternalIO() {
robertphillips8c523e02016-07-26 07:41:00 -07001402 ASSERT_SINGLE_OWNER
1403 RETURN_IF_ABANDONED
1404 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -04001405 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::prepareForExternalIO");
robertphillips8c523e02016-07-26 07:41:00 -07001406
Robert Phillips7ee385e2017-03-30 08:02:11 -04001407 this->drawingManager()->prepareSurfaceForExternalIO(fRenderTargetProxy.get());
robertphillips8c523e02016-07-26 07:41:00 -07001408}
joshualitt33a5fce2015-11-18 13:28:51 -08001409
Brian Osman11052242016-10-27 14:47:55 -04001410void GrRenderTargetContext::drawNonAAFilledRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001411 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001412 const SkMatrix& viewMatrix,
1413 const SkRect& rect,
1414 const SkRect* localRect,
1415 const SkMatrix* localMatrix,
1416 const GrUserStencilSettings* ss,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001417 GrAAType hwOrNoneAAType) {
1418 SkASSERT(GrAAType::kCoverage != hwOrNoneAAType);
Brian Salomon7c8460e2017-05-12 11:36:10 -04001419 SkASSERT(GrAAType::kNone == hwOrNoneAAType || GrFSAAType::kNone != this->fsaaType());
Brian Salomonac70f842017-05-08 10:43:33 -04001420 std::unique_ptr<GrDrawOp> op = GrNonAAFillRectOp::Make(
1421 std::move(paint), viewMatrix, rect, localRect, localMatrix, hwOrNoneAAType, ss);
1422 this->addDrawOp(clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -07001423}
1424
robertphillipsea461502015-05-26 11:38:03 -07001425// Can 'path' be drawn as a pair of filled nested rectangles?
bsalomon6663acf2016-05-10 09:14:17 -07001426static bool fills_as_nested_rects(const SkMatrix& viewMatrix, const SkPath& path, SkRect rects[2]) {
robertphillipsea461502015-05-26 11:38:03 -07001427
1428 if (path.isInverseFillType()) {
1429 return false;
1430 }
1431
1432 // TODO: this restriction could be lifted if we were willing to apply
1433 // the matrix to all the points individually rather than just to the rect
robertphillips0e7029e2015-11-30 05:45:06 -08001434 if (!viewMatrix.rectStaysRect()) {
robertphillipsea461502015-05-26 11:38:03 -07001435 return false;
1436 }
1437
1438 SkPath::Direction dirs[2];
1439 if (!path.isNestedFillRects(rects, dirs)) {
1440 return false;
1441 }
1442
1443 if (SkPath::kWinding_FillType == path.getFillType() && dirs[0] == dirs[1]) {
1444 // The two rects need to be wound opposite to each other
1445 return false;
1446 }
1447
1448 // Right now, nested rects where the margin is not the same width
1449 // all around do not render correctly
1450 const SkScalar* outer = rects[0].asScalars();
1451 const SkScalar* inner = rects[1].asScalars();
1452
1453 bool allEq = true;
1454
1455 SkScalar margin = SkScalarAbs(outer[0] - inner[0]);
1456 bool allGoE1 = margin >= SK_Scalar1;
1457
1458 for (int i = 1; i < 4; ++i) {
1459 SkScalar temp = SkScalarAbs(outer[i] - inner[i]);
1460 if (temp < SK_Scalar1) {
1461 allGoE1 = false;
1462 }
1463 if (!SkScalarNearlyEqual(margin, temp)) {
1464 allEq = false;
1465 }
1466 }
1467
1468 return allEq || allGoE1;
1469}
1470
Brian Osman11052242016-10-27 14:47:55 -04001471void GrRenderTargetContext::drawPath(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001472 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001473 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001474 const SkMatrix& viewMatrix,
1475 const SkPath& path,
1476 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001477 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001478 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001479 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -04001480 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawPath");
robertphillips2e1e51f2015-10-15 08:01:48 -07001481
robertphillipsea461502015-05-26 11:38:03 -07001482 if (path.isEmpty()) {
1483 if (path.isInverseFillType()) {
Brian Salomon82f44312017-01-11 13:42:54 -05001484 this->drawPaint(clip, std::move(paint), viewMatrix);
robertphillipsea461502015-05-26 11:38:03 -07001485 }
1486 return;
1487 }
1488
Robert Phillips72152832017-01-25 17:31:35 -05001489 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -07001490
Brian Salomon7c8460e2017-05-12 11:36:10 -04001491 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001492 if (GrAAType::kCoverage == aaType && !style.pathEffect()) {
bsalomon6663acf2016-05-10 09:14:17 -07001493 if (style.isSimpleFill() && !path.isConvex()) {
robertphillipsea461502015-05-26 11:38:03 -07001494 // Concave AA paths are expensive - try to avoid them for special cases
1495 SkRect rects[2];
1496
bsalomon6663acf2016-05-10 09:14:17 -07001497 if (fills_as_nested_rects(viewMatrix, path, rects)) {
Brian Salomond3ccb0a2017-04-03 10:38:00 -04001498 std::unique_ptr<GrLegacyMeshDrawOp> op =
Brian Salomon6a639042016-12-14 11:08:17 -05001499 GrRectOpFactory::MakeAAFillNestedRects(paint.getColor(), viewMatrix, rects);
Brian Salomon21aa35f2016-12-09 16:01:53 -05001500 if (op) {
Brian Salomon82f44312017-01-11 13:42:54 -05001501 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
Brian Salomone14bd802017-04-04 15:13:25 -04001502 this->addLegacyMeshDrawOp(std::move(pipelineBuilder), clip, std::move(op));
bsalomon40ef4852016-05-02 13:22:13 -07001503 }
robertphillipsea461502015-05-26 11:38:03 -07001504 return;
1505 }
1506 }
1507 SkRect ovalRect;
1508 bool isOval = path.isOval(&ovalRect);
1509
1510 if (isOval && !path.isInverseFillType()) {
Brian Salomon94efbf52016-11-29 13:43:05 -05001511 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomon05441c42017-05-15 16:45:49 -04001512 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeOvalOp(
1513 std::move(paint), viewMatrix, ovalRect, style.strokeRec(), shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -05001514 if (op) {
Brian Salomon05441c42017-05-15 16:45:49 -04001515 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -07001516 return;
1517 }
1518 }
1519 }
robertphillips4bc31812016-03-01 12:22:49 -08001520
1521 // Note that internalDrawPath may sw-rasterize the path into a scratch texture.
1522 // Scratch textures can be recycled after they are returned to the texture
1523 // cache. This presents a potential hazard for buffered drawing. However,
1524 // the writePixels that uploads to the scratch will perform a flush so we're
1525 // OK.
Brian Salomon82f44312017-01-11 13:42:54 -05001526 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, style);
robertphillipsea461502015-05-26 11:38:03 -07001527}
1528
Brian Osman11052242016-10-27 14:47:55 -04001529bool GrRenderTargetContextPriv::drawAndStencilPath(const GrClip& clip,
1530 const GrUserStencilSettings* ss,
1531 SkRegion::Op op,
1532 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001533 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001534 const SkMatrix& viewMatrix,
1535 const SkPath& path) {
robertphillips391395d2016-03-02 09:26:36 -08001536 ASSERT_SINGLE_OWNER_PRIV
1537 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -04001538 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001539 GR_AUDIT_TRAIL_AUTO_FRAME(fRenderTargetContext->fAuditTrail,
1540 "GrRenderTargetContextPriv::drawAndStencilPath");
robertphillips391395d2016-03-02 09:26:36 -08001541
1542 if (path.isEmpty() && path.isInverseFillType()) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001543 this->drawAndStencilRect(clip, ss, op, invert, GrAA::kNo, SkMatrix::I(),
Brian Osman11052242016-10-27 14:47:55 -04001544 SkRect::MakeIWH(fRenderTargetContext->width(),
1545 fRenderTargetContext->height()));
robertphillips391395d2016-03-02 09:26:36 -08001546 return true;
1547 }
1548
Robert Phillips72152832017-01-25 17:31:35 -05001549 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -08001550
1551 // An Assumption here is that path renderer would use some form of tweaking
1552 // the src color (either the input alpha or in the frag shader) to implement
1553 // aa. If we have some future driver-mojo path AA that can do the right
1554 // thing WRT to the blend then we'll need some query on the PR.
Brian Salomon7c8460e2017-05-12 11:36:10 -04001555 GrAAType aaType = fRenderTargetContext->chooseAAType(aa, GrAllowMixedSamples::kNo);
robertphillips976f5f02016-06-03 10:59:20 -07001556 bool hasUserStencilSettings = !ss->isUnused();
robertphillips391395d2016-03-02 09:26:36 -08001557
bsalomon8acedde2016-06-24 10:42:16 -07001558 GrShape shape(path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -08001559 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Eric Karl5c779752017-05-08 12:02:07 -07001560 canDrawArgs.fCaps = fRenderTargetContext->drawingManager()->getContext()->caps();
robertphillips391395d2016-03-02 09:26:36 -08001561 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07001562 canDrawArgs.fShape = &shape;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001563 canDrawArgs.fAAType = aaType;
cdalton93a379b2016-05-11 13:58:08 -07001564 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips391395d2016-03-02 09:26:36 -08001565
1566 // Don't allow the SW renderer
Robert Phillips72152832017-01-25 17:31:35 -05001567 GrPathRenderer* pr = fRenderTargetContext->drawingManager()->getPathRenderer(
Brian Salomon36aa1762016-12-10 13:24:02 -05001568 canDrawArgs, false, GrPathRendererChain::DrawType::kStencilAndColor);
robertphillips391395d2016-03-02 09:26:36 -08001569 if (!pr) {
1570 return false;
1571 }
1572
1573 GrPaint paint;
1574 paint.setCoverageSetOpXPFactory(op, invert);
1575
Brian Salomon82f44312017-01-11 13:42:54 -05001576 GrPathRenderer::DrawPathArgs args{
Robert Phillips256c37b2017-03-01 14:32:46 -05001577 fRenderTargetContext->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05001578 std::move(paint),
1579 ss,
1580 fRenderTargetContext,
1581 &clip,
1582 &viewMatrix,
1583 &shape,
1584 aaType,
1585 fRenderTargetContext->isGammaCorrect()};
robertphillips391395d2016-03-02 09:26:36 -08001586 pr->drawPath(args);
1587 return true;
1588}
1589
Brian Osman11052242016-10-27 14:47:55 -04001590SkBudgeted GrRenderTargetContextPriv::isBudgeted() const {
robertphillips714712b2016-08-04 06:20:45 -07001591 ASSERT_SINGLE_OWNER_PRIV
1592
Brian Osman11052242016-10-27 14:47:55 -04001593 if (fRenderTargetContext->wasAbandoned()) {
robertphillips714712b2016-08-04 06:20:45 -07001594 return SkBudgeted::kNo;
1595 }
1596
Brian Osman11052242016-10-27 14:47:55 -04001597 SkDEBUGCODE(fRenderTargetContext->validate();)
robertphillips714712b2016-08-04 06:20:45 -07001598
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001599 return fRenderTargetContext->fRenderTargetProxy->isBudgeted();
robertphillips714712b2016-08-04 06:20:45 -07001600}
1601
Brian Osman11052242016-10-27 14:47:55 -04001602void GrRenderTargetContext::internalDrawPath(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001603 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001604 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001605 const SkMatrix& viewMatrix,
1606 const SkPath& path,
1607 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001608 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001609 RETURN_IF_ABANDONED
bsalomon8acedde2016-06-24 10:42:16 -07001610 SkASSERT(!path.isEmpty());
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001611 GrShape shape;
Brian Salomon7c8460e2017-05-12 11:36:10 -04001612 // NVPR cannot handle hairlines, so this would get picked up by a different stencil and
1613 // cover path renderer (i.e. default path renderer). The hairline renderer produces much
1614 // smoother hairlines than MSAA.
1615 GrAllowMixedSamples allowMixedSamples =
1616 style.isSimpleHairline() ? GrAllowMixedSamples::kNo : GrAllowMixedSamples::kYes;
1617 GrAAType aaType = this->chooseAAType(aa, allowMixedSamples);
robertphillips68737822015-10-29 12:12:21 -07001618 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Eric Karl5c779752017-05-08 12:02:07 -07001619 canDrawArgs.fCaps = this->drawingManager()->getContext()->caps();
robertphillips68737822015-10-29 12:12:21 -07001620 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07001621 canDrawArgs.fShape = &shape;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001622 canDrawArgs.fHasUserStencilSettings = false;
robertphillips68737822015-10-29 12:12:21 -07001623
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001624 GrPathRenderer* pr;
Brian Salomon82125e92016-12-10 09:35:48 -05001625 static constexpr GrPathRendererChain::DrawType kType = GrPathRendererChain::DrawType::kColor;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001626 do {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001627 shape = GrShape(path, style);
bsalomon8acedde2016-06-24 10:42:16 -07001628 if (shape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001629 return;
1630 }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001631
1632 canDrawArgs.fAAType = aaType;
1633
1634 // Try a 1st time without applying any of the style to the geometry (and barring sw)
Robert Phillips72152832017-01-25 17:31:35 -05001635 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001636 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
1637
1638 if (!pr && shape.style().pathEffect()) {
1639 // It didn't work above, so try again with the path effect applied.
1640 shape = shape.applyStyle(GrStyle::Apply::kPathEffectOnly, styleScale);
bsalomon8acedde2016-06-24 10:42:16 -07001641 if (shape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001642 return;
1643 }
Robert Phillips72152832017-01-25 17:31:35 -05001644 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
bsalomon6663acf2016-05-10 09:14:17 -07001645 }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001646 if (!pr) {
1647 if (shape.style().applies()) {
1648 shape = shape.applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec, styleScale);
1649 if (shape.isEmpty()) {
1650 return;
1651 }
1652 }
1653 // This time, allow SW renderer
Robert Phillips72152832017-01-25 17:31:35 -05001654 pr = this->drawingManager()->getPathRenderer(canDrawArgs, true, kType);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001655 }
Brian Salomon0abc8b42016-12-13 10:22:54 -05001656 if (!pr && GrAATypeIsHW(aaType)) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001657 // There are exceptional cases where we may wind up falling back to coverage based AA
1658 // when the target is MSAA (e.g. through disabling path renderers via GrContextOptions).
1659 aaType = GrAAType::kCoverage;
1660 } else {
1661 break;
1662 }
1663 } while(true);
robertphillipsea461502015-05-26 11:38:03 -07001664
bsalomon8acedde2016-06-24 10:42:16 -07001665 if (!pr) {
robertphillipsea461502015-05-26 11:38:03 -07001666#ifdef SK_DEBUG
1667 SkDebugf("Unable to find path renderer compatible with path.\n");
1668#endif
1669 return;
1670 }
1671
Robert Phillips256c37b2017-03-01 14:32:46 -05001672 GrPathRenderer::DrawPathArgs args{this->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05001673 std::move(paint),
1674 &GrUserStencilSettings::kUnused,
1675 this,
1676 &clip,
1677 &viewMatrix,
1678 &shape,
1679 aaType,
1680 this->isGammaCorrect()};
bsalomon0aff2fa2015-07-31 06:48:27 -07001681 pr->drawPath(args);
robertphillipsea461502015-05-26 11:38:03 -07001682}
1683
Brian Salomon467921e2017-03-06 16:17:12 -05001684static void op_bounds(SkRect* bounds, const GrOp* op) {
1685 *bounds = op->bounds();
1686 if (op->hasZeroArea()) {
1687 if (op->hasAABloat()) {
1688 bounds->outset(0.5f, 0.5f);
1689 } else {
1690 // We don't know which way the particular GPU will snap lines or points at integer
1691 // coords. So we ensure that the bounds is large enough for either snap.
1692 SkRect before = *bounds;
1693 bounds->roundOut(bounds);
1694 if (bounds->fLeft == before.fLeft) {
1695 bounds->fLeft -= 1;
1696 }
1697 if (bounds->fTop == before.fTop) {
1698 bounds->fTop -= 1;
1699 }
1700 if (bounds->fRight == before.fRight) {
1701 bounds->fRight += 1;
1702 }
1703 if (bounds->fBottom == before.fBottom) {
1704 bounds->fBottom += 1;
1705 }
1706 }
1707 }
1708}
1709
Brian Salomon54d212e2017-03-21 14:22:38 -04001710uint32_t GrRenderTargetContext::addDrawOp(const GrClip& clip, std::unique_ptr<GrDrawOp> op) {
joshualitt1de610a2016-01-06 08:26:09 -08001711 ASSERT_SINGLE_OWNER
Robert Phillipsc0138922017-03-08 11:50:55 -05001712 if (this->drawingManager()->wasAbandoned()) {
1713 return SK_InvalidUniqueID;
1714 }
robertphillips2e1e51f2015-10-15 08:01:48 -07001715 SkDEBUGCODE(this->validate();)
Brian Salomon42521e82016-12-07 16:44:58 -05001716 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::addDrawOp");
robertphillips2d70dcb2015-10-06 07:38:23 -07001717
Brian Salomon467921e2017-03-06 16:17:12 -05001718 // Setup clip
1719 SkRect bounds;
1720 op_bounds(&bounds, op.get());
Brian Salomon97180af2017-03-14 13:42:58 -04001721 GrAppliedClip appliedClip;
Brian Salomon54d212e2017-03-21 14:22:38 -04001722 GrDrawOp::FixedFunctionFlags fixedFunctionFlags = op->fixedFunctionFlags();
1723 if (!clip.apply(fContext, this, fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesHWAA,
1724 fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil, &appliedClip,
1725 &bounds)) {
1726 return SK_InvalidUniqueID;
1727 }
1728
Brian Salomon54d212e2017-03-21 14:22:38 -04001729 if (fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil ||
1730 appliedClip.hasStencilClip()) {
Robert Phillips5efd5ea2017-05-30 13:47:32 -04001731 // This forces instantiation of the render target.
1732 GrRenderTarget* rt = this->accessRenderTarget();
1733 if (!rt) {
1734 return SK_InvalidUniqueID;
1735 }
1736
Brian Salomon54d212e2017-03-21 14:22:38 -04001737 if (!fContext->resourceProvider()->attachStencilAttachment(rt)) {
1738 SkDebugf("ERROR creating stencil attachment. Draw skipped.\n");
1739 return SK_InvalidUniqueID;
1740 }
1741 }
1742
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001743 GrXferProcessor::DstProxy dstProxy;
Brian Salomon54d212e2017-03-21 14:22:38 -04001744 if (op->xpRequiresDstTexture(*this->caps(), &appliedClip)) {
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001745 if (!this->setupDstProxy(this->asRenderTargetProxy(), clip, op->bounds(), &dstProxy)) {
Brian Salomon54d212e2017-03-21 14:22:38 -04001746 return SK_InvalidUniqueID;
1747 }
1748 }
1749
1750 op->setClippedBounds(bounds);
Robert Phillips5efd5ea2017-05-30 13:47:32 -04001751 return this->getOpList()->addOp(std::move(op), *this->caps(),
1752 std::move(appliedClip), dstProxy);
Brian Salomon54d212e2017-03-21 14:22:38 -04001753}
1754
Brian Salomone14bd802017-04-04 15:13:25 -04001755uint32_t GrRenderTargetContext::addLegacyMeshDrawOp(GrPipelineBuilder&& pipelineBuilder,
Brian Salomond3ccb0a2017-04-03 10:38:00 -04001756 const GrClip& clip,
1757 std::unique_ptr<GrLegacyMeshDrawOp> op) {
Brian Salomon54d212e2017-03-21 14:22:38 -04001758 ASSERT_SINGLE_OWNER
1759 if (this->drawingManager()->wasAbandoned()) {
1760 return SK_InvalidUniqueID;
1761 }
1762 SkDEBUGCODE(this->validate();)
Brian Salomond3ccb0a2017-04-03 10:38:00 -04001763 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::addLegacyMeshDrawOp");
Brian Salomon54d212e2017-03-21 14:22:38 -04001764
1765 // Setup clip
1766 SkRect bounds;
1767 op_bounds(&bounds, op.get());
1768 GrAppliedClip appliedClip;
Brian Salomon467921e2017-03-06 16:17:12 -05001769 if (!clip.apply(fContext, this, pipelineBuilder.isHWAntialias(),
Brian Salomon97180af2017-03-14 13:42:58 -04001770 pipelineBuilder.hasUserStencilSettings(), &appliedClip, &bounds)) {
Robert Phillipsc0138922017-03-08 11:50:55 -05001771 return SK_InvalidUniqueID;
Brian Salomon467921e2017-03-06 16:17:12 -05001772 }
1773
1774 // This forces instantiation of the render target. Pipeline creation is moving to flush time
1775 // by which point instantiation must have occurred anyway.
1776 GrRenderTarget* rt = this->accessRenderTarget();
1777 if (!rt) {
Robert Phillipsc0138922017-03-08 11:50:55 -05001778 return SK_InvalidUniqueID;
Brian Salomon467921e2017-03-06 16:17:12 -05001779 }
1780
1781 GrResourceProvider* resourceProvider = fContext->resourceProvider();
Brian Salomon48d1b4c2017-04-08 07:38:53 -04001782 bool usesStencil = pipelineBuilder.hasUserStencilSettings() || appliedClip.hasStencilClip();
1783 if (usesStencil) {
Brian Salomon467921e2017-03-06 16:17:12 -05001784 if (!resourceProvider->attachStencilAttachment(this->accessRenderTarget())) {
1785 SkDebugf("ERROR creating stencil attachment. Draw skipped.\n");
Robert Phillipsc0138922017-03-08 11:50:55 -05001786 return SK_InvalidUniqueID;
Brian Salomon467921e2017-03-06 16:17:12 -05001787 }
1788 }
1789
Brian Salomon7c8460e2017-05-12 11:36:10 -04001790 bool isMixedSamples = GrFSAAType::kMixedSamples == this->fsaaType() &&
Brian Salomon48d1b4c2017-04-08 07:38:53 -04001791 (pipelineBuilder.isHWAntialias() || usesStencil);
1792
1793 GrColor overrideColor;
1794 GrProcessorSet::Analysis analysis = op->analyzeUpdateAndRecordProcessors(
1795 &pipelineBuilder, &appliedClip, isMixedSamples, *this->caps(), &overrideColor);
Brian Salomon467921e2017-03-06 16:17:12 -05001796
1797 GrPipeline::InitArgs args;
1798 pipelineBuilder.getPipelineInitArgs(&args);
1799 args.fAppliedClip = &appliedClip;
1800 args.fRenderTarget = rt;
1801 args.fCaps = this->caps();
Robert Phillips9bee2e52017-05-29 12:37:20 -04001802 args.fResourceProvider = this->resourceProvider();
Brian Salomon467921e2017-03-06 16:17:12 -05001803
Brian Salomon31853842017-03-28 16:32:05 -04001804 if (analysis.requiresDstTexture()) {
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001805 if (!this->setupDstProxy(this->asRenderTargetProxy(), clip, bounds, &args.fDstProxy)) {
Robert Phillipsc0138922017-03-08 11:50:55 -05001806 return SK_InvalidUniqueID;
Brian Salomon467921e2017-03-06 16:17:12 -05001807 }
1808 }
Brian Salomon48d1b4c2017-04-08 07:38:53 -04001809 op->initPipeline(args, analysis, overrideColor);
Robert Phillipsf5442bb2017-04-17 14:18:34 -04001810
1811 // Add the pipeline dependencies on textures, etc before recording this op.
1812 op->addDependenciesTo(fRenderTargetProxy.get());
1813
Brian Salomon97180af2017-03-14 13:42:58 -04001814 op->setClippedBounds(bounds);
Robert Phillips5efd5ea2017-05-30 13:47:32 -04001815 return this->getOpList()->addOp(std::move(op), *this->caps());
Brian Salomon467921e2017-03-06 16:17:12 -05001816}
1817
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001818bool GrRenderTargetContext::setupDstProxy(GrRenderTargetProxy* rtProxy, const GrClip& clip,
Brian Salomon467921e2017-03-06 16:17:12 -05001819 const SkRect& opBounds,
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001820 GrXferProcessor::DstProxy* dstProxy) {
Brian Salomon467921e2017-03-06 16:17:12 -05001821 if (this->caps()->textureBarrierSupport()) {
Robert Phillipsbf25d432017-04-07 10:08:53 -04001822 if (GrTextureProxy* texProxy = rtProxy->asTextureProxy()) {
Brian Salomon467921e2017-03-06 16:17:12 -05001823 // The render target is a texture, so we can read from it directly in the shader. The XP
1824 // will be responsible to detect this situation and request a texture barrier.
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001825 dstProxy->setProxy(sk_ref_sp(texProxy));
1826 dstProxy->setOffset(0, 0);
Robert Phillipsbf25d432017-04-07 10:08:53 -04001827 return true;
Brian Salomon467921e2017-03-06 16:17:12 -05001828 }
1829 }
1830
Robert Phillipsbf25d432017-04-07 10:08:53 -04001831 SkIRect copyRect = SkIRect::MakeWH(rtProxy->width(), rtProxy->height());
Brian Salomon467921e2017-03-06 16:17:12 -05001832
Eric Karl74480882017-04-03 14:49:05 -07001833 SkIRect clippedRect;
Robert Phillipsbf25d432017-04-07 10:08:53 -04001834 clip.getConservativeBounds(rtProxy->width(), rtProxy->height(), &clippedRect);
Eric Karl72e551e2017-04-04 13:42:10 -07001835 SkIRect drawIBounds;
Brian Salomon467921e2017-03-06 16:17:12 -05001836 opBounds.roundOut(&drawIBounds);
Brian Salomon859621f2017-03-16 09:21:54 -04001837 // Cover up for any precision issues by outsetting the op bounds a pixel in each direction.
1838 drawIBounds.outset(1, 1);
Eric Karl72e551e2017-04-04 13:42:10 -07001839 if (!clippedRect.intersect(drawIBounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -05001840#ifdef SK_DEBUG
Robert Phillipsbf25d432017-04-07 10:08:53 -04001841 GrCapsDebugf(this->caps(), "setupDstTexture: Missed an early reject bailing on draw.");
Brian Salomon467921e2017-03-06 16:17:12 -05001842#endif
Robert Phillipsbf25d432017-04-07 10:08:53 -04001843 return false;
Brian Salomon467921e2017-03-06 16:17:12 -05001844 }
1845
1846 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
1847 // have per-sample dst values by making the copy multisampled.
1848 GrSurfaceDesc desc;
Eric Karl74480882017-04-03 14:49:05 -07001849 bool rectsMustMatch = false;
1850 bool disallowSubrect = false;
Robert Phillipsbf25d432017-04-07 10:08:53 -04001851 if (!this->caps()->initDescForDstCopy(rtProxy, &desc, &rectsMustMatch, &disallowSubrect)) {
1852 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
Brian Salomon467921e2017-03-06 16:17:12 -05001853 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillipsbf25d432017-04-07 10:08:53 -04001854 desc.fConfig = rtProxy->config();
Brian Salomon467921e2017-03-06 16:17:12 -05001855 }
1856
Eric Karl74480882017-04-03 14:49:05 -07001857 if (!disallowSubrect) {
1858 copyRect = clippedRect;
1859 }
Brian Salomon467921e2017-03-06 16:17:12 -05001860
Robert Phillipsbf25d432017-04-07 10:08:53 -04001861 SkIPoint dstPoint, dstOffset;
1862 SkBackingFit fit;
Eric Karl74480882017-04-03 14:49:05 -07001863 if (rectsMustMatch) {
Robert Phillipsbf25d432017-04-07 10:08:53 -04001864 SkASSERT(desc.fOrigin == rtProxy->origin());
1865 desc.fWidth = rtProxy->width();
1866 desc.fHeight = rtProxy->height();
Eric Karl74480882017-04-03 14:49:05 -07001867 dstPoint = {copyRect.fLeft, copyRect.fTop};
1868 dstOffset = {0, 0};
Robert Phillipsbf25d432017-04-07 10:08:53 -04001869 fit = SkBackingFit::kExact;
Eric Karl74480882017-04-03 14:49:05 -07001870 } else {
1871 desc.fWidth = copyRect.width();
1872 desc.fHeight = copyRect.height();
1873 dstPoint = {0, 0};
1874 dstOffset = {copyRect.fLeft, copyRect.fTop};
Robert Phillipsbf25d432017-04-07 10:08:53 -04001875 fit = SkBackingFit::kApprox;
Eric Karl74480882017-04-03 14:49:05 -07001876 }
Brian Salomon467921e2017-03-06 16:17:12 -05001877
Robert Phillipsbf25d432017-04-07 10:08:53 -04001878 sk_sp<GrSurfaceContext> sContext = fContext->contextPriv().makeDeferredSurfaceContext(
1879 desc,
1880 fit,
1881 SkBudgeted::kYes);
1882 if (!sContext) {
1883 SkDebugf("setupDstTexture: surfaceContext creation failed.\n");
1884 return false;
1885 }
1886
1887 if (!sContext->copy(rtProxy, copyRect, dstPoint)) {
1888 SkDebugf("setupDstTexture: copy failed.\n");
1889 return false;
1890 }
1891
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001892 dstProxy->setProxy(sContext->asTextureProxyRef());
1893 dstProxy->setOffset(dstOffset);
Robert Phillipsbf25d432017-04-07 10:08:53 -04001894 return true;
robertphillips2334fb62015-06-17 05:43:33 -07001895}