blob: c72c4d1d6f37218ef766f542ff7fb5eaaab7a503 [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 Phillipsa16f6cb2017-06-01 11:06:13 -0400141 return this->getOpList()->copySurface(*this->caps(),
142 this->asSurfaceProxy(), 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
Robert Phillips2f4ddf62017-06-01 08:48:19 -0400608 std::unique_ptr<GrOp> op(GrClearStencilClipOp::Make(
609 clip, insideStencilMask,
610 fRenderTargetContext->fRenderTargetProxy.get()));
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400611 if (!op) {
Robert Phillipse60ad622016-11-17 10:22:48 -0500612 return;
613 }
Robert Phillips5efd5ea2017-05-30 13:47:32 -0400614 fRenderTargetContext->getOpList()->addOp(std::move(op), *fRenderTargetContext->caps());
robertphillips976f5f02016-06-03 10:59:20 -0700615}
616
Brian Osman11052242016-10-27 14:47:55 -0400617void GrRenderTargetContextPriv::stencilPath(const GrClip& clip,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500618 GrAAType aaType,
Brian Osman11052242016-10-27 14:47:55 -0400619 const SkMatrix& viewMatrix,
620 const GrPath* path) {
Brian Salomon467921e2017-03-06 16:17:12 -0500621 ASSERT_SINGLE_OWNER_PRIV
622 RETURN_IF_ABANDONED_PRIV
623 SkDEBUGCODE(fRenderTargetContext->validate();)
624 GR_AUDIT_TRAIL_AUTO_FRAME(fRenderTargetContext->fAuditTrail,
625 "GrRenderTargetContext::stencilPath");
626
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500627 SkASSERT(aaType != GrAAType::kCoverage);
Brian Salomon467921e2017-03-06 16:17:12 -0500628
629 bool useHWAA = GrAATypeIsHW(aaType);
630 // TODO: extract portions of checkDraw that are relevant to path stenciling.
631 SkASSERT(path);
632 SkASSERT(fRenderTargetContext->caps()->shaderCaps()->pathRenderingSupport());
633
634 // FIXME: Use path bounds instead of this WAR once
635 // https://bugs.chromium.org/p/skia/issues/detail?id=5640 is resolved.
636 SkRect bounds = SkRect::MakeIWH(fRenderTargetContext->width(), fRenderTargetContext->height());
637
638 // Setup clip
Brian Salomon97180af2017-03-14 13:42:58 -0400639 GrAppliedClip appliedClip;
Brian Salomon467921e2017-03-06 16:17:12 -0500640 if (!clip.apply(fRenderTargetContext->fContext, fRenderTargetContext, useHWAA, true,
Brian Salomon97180af2017-03-14 13:42:58 -0400641 &appliedClip, &bounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -0500642 return;
643 }
644
645 // Coverage AA does not make sense when rendering to the stencil buffer. The caller should never
646 // attempt this in a situation that would require coverage AA.
647 SkASSERT(!appliedClip.clipCoverageFragmentProcessor());
648
649 GrRenderTarget* rt = fRenderTargetContext->accessRenderTarget();
650 if (!rt) {
651 return;
652 }
653 GrStencilAttachment* stencilAttachment =
654 fRenderTargetContext->fContext->resourceProvider()->attachStencilAttachment(rt);
655 if (!stencilAttachment) {
656 SkDebugf("ERROR creating stencil attachment. Draw skipped.\n");
657 return;
658 }
659
660 std::unique_ptr<GrOp> op = GrStencilPathOp::Make(viewMatrix,
661 useHWAA,
662 path->getFillType(),
663 appliedClip.hasStencilClip(),
664 stencilAttachment->bits(),
665 appliedClip.scissorState(),
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,
Brian Osmanfa6d8652017-05-31 09:37:27 -0400851 this->isGammaCorrect(),
852 fColorXformFromSRGB,
Brian Osmanae0c50c2017-05-25 16:56:34 -0400853 overridePrimType);
Brian Salomon199fb872017-02-06 09:41:10 -0500854 if (!op) {
855 return;
856 }
Brian Salomon82f44312017-01-11 13:42:54 -0500857 GrPipelineBuilder pipelineBuilder(std::move(paint), GrAAType::kNone);
Brian Salomone14bd802017-04-04 15:13:25 -0400858 this->addLegacyMeshDrawOp(std::move(pipelineBuilder), clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -0700859}
860
861///////////////////////////////////////////////////////////////////////////////
862
Brian Osman11052242016-10-27 14:47:55 -0400863void GrRenderTargetContext::drawAtlas(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500864 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400865 const SkMatrix& viewMatrix,
866 int spriteCount,
867 const SkRSXform xform[],
868 const SkRect texRect[],
869 const SkColor colors[]) {
joshualitt1de610a2016-01-06 08:26:09 -0800870 ASSERT_SINGLE_OWNER
jvanverth31ff7622015-08-07 10:09:28 -0700871 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700872 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400873 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawAtlas");
robertphillips2e1e51f2015-10-15 08:01:48 -0700874
Robert Phillips72152832017-01-25 17:31:35 -0500875 AutoCheckFlush acf(this->drawingManager());
halcanary9d524f22016-03-29 09:03:52 -0700876
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400877 std::unique_ptr<GrLegacyMeshDrawOp> op =
Brian Salomonfc527d22016-12-14 21:07:01 -0500878 GrDrawAtlasOp::Make(paint.getColor(), viewMatrix, spriteCount, xform, texRect, colors);
Brian Salomon82f44312017-01-11 13:42:54 -0500879 GrPipelineBuilder pipelineBuilder(std::move(paint), GrAAType::kNone);
Brian Salomone14bd802017-04-04 15:13:25 -0400880 this->addLegacyMeshDrawOp(std::move(pipelineBuilder), clip, std::move(op));
jvanverth31ff7622015-08-07 10:09:28 -0700881}
882
883///////////////////////////////////////////////////////////////////////////////
884
Brian Osman11052242016-10-27 14:47:55 -0400885void GrRenderTargetContext::drawRRect(const GrClip& origClip,
Brian Salomon82f44312017-01-11 13:42:54 -0500886 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500887 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400888 const SkMatrix& viewMatrix,
889 const SkRRect& rrect,
890 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -0800891 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700892 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700893 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400894 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawRRect");
robertphillipsea461502015-05-26 11:38:03 -0700895 if (rrect.isEmpty()) {
896 return;
897 }
898
bsalomon7f0d9f32016-08-15 14:49:10 -0700899 GrNoClip noclip;
900 const GrClip* clip = &origClip;
901#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
902 // The Android framework frequently clips rrects to themselves where the clip is non-aa and the
Brian Salomon42521e82016-12-07 16:44:58 -0500903 // draw is aa. Since our lower level clip code works from op bounds, which are SkRects, it
bsalomon7f0d9f32016-08-15 14:49:10 -0700904 // doesn't detect that the clip can be ignored (modulo antialiasing). The following test
905 // attempts to mitigate the stencil clip cost but will only help when the entire clip stack
906 // can be ignored. We'd prefer to fix this in the framework by removing the clips calls.
907 SkRRect devRRect;
908 if (rrect.transform(viewMatrix, &devRRect) && clip->quickContains(devRRect)) {
909 clip = &noclip;
910 }
911#endif
bsalomon6663acf2016-05-10 09:14:17 -0700912 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
ksakamotoec7f2ac2016-07-05 03:54:53 -0700913
Robert Phillips72152832017-01-25 17:31:35 -0500914 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -0700915 const SkStrokeRec stroke = style.strokeRec();
csmartdaltona7f29642016-07-07 08:49:11 -0700916
csmartdaltone0d36292016-07-29 08:14:20 -0700917 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport() &&
918 stroke.isFillStyle()) {
Robert Phillipse3302df2017-04-24 07:31:02 -0400919 gr_instanced::OpAllocator* oa = this->drawingManager()->instancingAllocator();
Brian Salomon54d212e2017-03-21 14:22:38 -0400920 std::unique_ptr<GrDrawOp> op(
Robert Phillipse3302df2017-04-24 07:31:02 -0400921 oa->recordRRect(rrect, viewMatrix, std::move(paint), aa, fInstancedPipelineInfo));
Brian Salomon42521e82016-12-07 16:44:58 -0500922 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -0400923 this->addDrawOp(*clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -0700924 return;
925 }
926 }
927
Brian Salomon7c8460e2017-05-12 11:36:10 -0400928 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500929 if (GrAAType::kCoverage == aaType) {
Brian Salomon94efbf52016-11-29 13:43:05 -0500930 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomon05441c42017-05-15 16:45:49 -0400931 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeRRectOp(std::move(paint),
932 paint.usesDistanceVectorField(),
933 viewMatrix,
934 rrect,
935 stroke,
936 shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -0500937 if (op) {
Brian Salomon05441c42017-05-15 16:45:49 -0400938 this->addDrawOp(*clip, std::move(op));
robertphillipsb56f9272016-02-25 11:03:52 -0800939 return;
940 }
robertphillipsea461502015-05-26 11:38:03 -0700941 }
robertphillipsb56f9272016-02-25 11:03:52 -0800942
943 SkPath path;
944 path.setIsVolatile(true);
945 path.addRRect(rrect);
Brian Salomon82f44312017-01-11 13:42:54 -0500946 this->internalDrawPath(*clip, std::move(paint), aa, viewMatrix, path, style);
robertphillipsea461502015-05-26 11:38:03 -0700947}
948
Jim Van Verthc5903412016-11-17 15:27:09 -0500949///////////////////////////////////////////////////////////////////////////////
950
Jim Van Verth3af1af92017-05-18 15:06:54 -0400951static SkPoint3 map(const SkMatrix& m, const SkPoint3& pt) {
952 SkPoint3 result;
953 m.mapXY(pt.fX, pt.fY, (SkPoint*)&result.fX);
954 result.fZ = pt.fZ;
955 return result;
956}
957
958bool GrRenderTargetContext::drawFastShadow(const GrClip& clip,
959 GrPaint&& paint,
960 const SkMatrix& viewMatrix,
961 const SkPath& path,
962 const SkDrawShadowRec& rec) {
Jim Van Verthc5903412016-11-17 15:27:09 -0500963 ASSERT_SINGLE_OWNER
Jim Van Verth3af1af92017-05-18 15:06:54 -0400964 if (this->drawingManager()->wasAbandoned()) {
965 return true;
966 }
Jim Van Verthc5903412016-11-17 15:27:09 -0500967 SkDEBUGCODE(this->validate();)
Jim Van Verth3af1af92017-05-18 15:06:54 -0400968 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawFastShadow");
969
970 // check z plane
971 bool tiltZPlane = SkToBool(!SkScalarNearlyZero(rec.fZPlaneParams.fX) ||
972 !SkScalarNearlyZero(rec.fZPlaneParams.fY));
973 bool skipAnalytic = SkToBool(rec.fFlags & SkShadowFlags::kGeometricOnly_ShadowFlag);
974 if (tiltZPlane || skipAnalytic || !viewMatrix.rectStaysRect() || !viewMatrix.isSimilarity()) {
975 return false;
976 }
977
978 SkRRect rrect;
979 SkRect rect;
980 // we can only handle rects, circles, and rrects with circular corners
981 bool isRRect = path.isRRect(&rrect) && rrect.isSimpleCircular() &&
982 rrect.radii(SkRRect::kUpperLeft_Corner).fX > SK_ScalarNearlyZero;
983 if (!isRRect &&
984 path.isOval(&rect) && SkScalarNearlyEqual(rect.width(), rect.height()) &&
985 rect.width() > SK_ScalarNearlyZero) {
986 rrect.setOval(rect);
987 isRRect = true;
988 }
989 if (!isRRect && path.isRect(&rect)) {
990 rrect.setRect(rect);
991 isRRect = true;
992 }
993
994 if (!isRRect) {
995 return false;
996 }
997
Jim Van Verthc5903412016-11-17 15:27:09 -0500998 if (rrect.isEmpty()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -0400999 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001000 }
1001
Robert Phillips72152832017-01-25 17:31:35 -05001002 AutoCheckFlush acf(this->drawingManager());
Jim Van Verthc5903412016-11-17 15:27:09 -05001003
Jim Van Verth3af1af92017-05-18 15:06:54 -04001004 // transform light
1005 SkPoint3 devLightPos = map(viewMatrix, rec.fLightPos);
1006
1007 // 1/scale
1008 SkScalar devToSrcScale = viewMatrix.isScaleTranslate() ?
1009 SkScalarInvert(viewMatrix[SkMatrix::kMScaleX]) :
1010 sk_float_rsqrt(viewMatrix[SkMatrix::kMScaleX] * viewMatrix[SkMatrix::kMScaleX] +
1011 viewMatrix[SkMatrix::kMSkewX] * viewMatrix[SkMatrix::kMSkewX]);
1012
1013 SkScalar occluderHeight = rec.fZPlaneParams.fZ;
1014 GrColor4f color = paint.getColor4f();
1015 bool transparent = SkToBool(rec.fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag);
1016
1017 if (rec.fAmbientAlpha > 0) {
1018 static constexpr float kHeightFactor = 1.0f / 128.0f;
1019 static constexpr float kGeomFactor = 64.0f;
1020
1021 SkScalar devSpaceInsetWidth = occluderHeight * kHeightFactor * kGeomFactor;
1022 const float umbraAlpha = (1.0f + SkTMax(occluderHeight * kHeightFactor, 0.0f));
1023 const SkScalar devSpaceAmbientBlur = devSpaceInsetWidth * umbraAlpha;
1024
1025 // Outset the shadow rrect to the border of the penumbra
1026 SkScalar ambientPathOutset = devSpaceInsetWidth * devToSrcScale;
1027 SkRRect ambientRRect;
1028 SkRect outsetRect = rrect.rect().makeOutset(ambientPathOutset, ambientPathOutset);
1029 // If the rrect was an oval then its outset will also be one.
1030 // We set it explicitly to avoid errors.
1031 if (rrect.isOval()) {
1032 ambientRRect = SkRRect::MakeOval(outsetRect);
1033 } else {
1034 SkScalar outsetRad = rrect.getSimpleRadii().fX + ambientPathOutset;
1035 ambientRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1036 }
1037
1038 GrColor ambientColor = color.mulByScalar(rec.fAmbientAlpha).toGrColor();
1039 if (transparent) {
1040 // set a large inset to force a fill
1041 devSpaceInsetWidth = ambientRRect.width();
1042 }
1043 // the fraction of the blur we want to apply is devSpaceInsetWidth/devSpaceAmbientBlur,
1044 // which is just 1/umbraAlpha.
1045 SkScalar blurClamp = SkScalarInvert(umbraAlpha);
1046
1047 std::unique_ptr<GrLegacyMeshDrawOp> op = GrShadowRRectOp::Make(ambientColor, viewMatrix,
1048 ambientRRect,
1049 devSpaceAmbientBlur,
1050 devSpaceInsetWidth,
1051 blurClamp);
1052 if (op) {
1053 GrPipelineBuilder pipelineBuilder(std::move(paint), GrAAType::kNone);
1054 this->addLegacyMeshDrawOp(std::move(pipelineBuilder), clip, std::move(op));
1055 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001056 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001057
1058 if (rec.fSpotAlpha > 0) {
1059 float zRatio = SkTPin(occluderHeight / (devLightPos.fZ - occluderHeight), 0.0f, 0.95f);
1060
1061 SkScalar devSpaceSpotBlur = 2.0f * rec.fLightRadius * zRatio;
1062 // handle scale of radius and pad due to CTM
1063 const SkScalar srcSpaceSpotBlur = devSpaceSpotBlur * devToSrcScale;
1064
1065 // Compute the scale and translation for the spot shadow.
1066 const SkScalar spotScale = devLightPos.fZ / (devLightPos.fZ - occluderHeight);
1067 SkPoint spotOffset = SkPoint::Make(zRatio*(-devLightPos.fX), zRatio*(-devLightPos.fY));
1068 // Adjust translate for the effect of the scale.
1069 spotOffset.fX += spotScale*viewMatrix[SkMatrix::kMTransX];
1070 spotOffset.fY += spotScale*viewMatrix[SkMatrix::kMTransY];
1071 // This offset is in dev space, need to transform it into source space.
1072 SkMatrix ctmInverse;
1073 if (viewMatrix.invert(&ctmInverse)) {
1074 ctmInverse.mapPoints(&spotOffset, 1);
1075 } else {
1076 // Since the matrix is a similarity, this should never happen, but just in case...
1077 SkDebugf("Matrix is degenerate. Will not render spot shadow correctly!\n");
1078 SkASSERT(false);
1079 }
1080
1081 // Compute the transformed shadow rrect
1082 SkRRect spotShadowRRect;
1083 SkMatrix shadowTransform;
1084 shadowTransform.setScaleTranslate(spotScale, spotScale, spotOffset.fX, spotOffset.fY);
1085 rrect.transform(shadowTransform, &spotShadowRRect);
1086 SkScalar spotRadius = spotShadowRRect.getSimpleRadii().fX;
1087
1088 // Compute the insetWidth
1089 SkScalar blurOutset = 0.5f*srcSpaceSpotBlur;
1090 SkScalar insetWidth = blurOutset;
1091 if (transparent) {
1092 // If transparent, just do a fill
1093 insetWidth += spotShadowRRect.width();
1094 } else {
1095 // For shadows, instead of using a stroke we specify an inset from the penumbra
1096 // border. We want to extend this inset area so that it meets up with the caster
1097 // geometry. The inset geometry will by default already be inset by the blur width.
1098 //
1099 // We compare the min and max corners inset by the radius between the original
1100 // rrect and the shadow rrect. The distance between the two plus the difference
1101 // between the scaled radius and the original radius gives the distance from the
1102 // transformed shadow shape to the original shape in that corner. The max
1103 // of these gives the maximum distance we need to cover.
1104 //
1105 // Since we are outsetting by 1/2 the blur distance, we just add the maxOffset to
1106 // that to get the full insetWidth.
1107 SkScalar maxOffset;
1108 if (rrect.isRect()) {
1109 // Manhattan distance works better for rects
1110 maxOffset = SkTMax(SkTMax(SkTAbs(spotShadowRRect.rect().fLeft -
1111 rrect.rect().fLeft),
1112 SkTAbs(spotShadowRRect.rect().fTop -
1113 rrect.rect().fTop)),
1114 SkTMax(SkTAbs(spotShadowRRect.rect().fRight -
1115 rrect.rect().fRight),
1116 SkTAbs(spotShadowRRect.rect().fBottom -
1117 rrect.rect().fBottom)));
1118 } else {
1119 SkScalar dr = spotRadius - rrect.getSimpleRadii().fX;
1120 SkPoint upperLeftOffset = SkPoint::Make(spotShadowRRect.rect().fLeft -
1121 rrect.rect().fLeft + dr,
1122 spotShadowRRect.rect().fTop -
1123 rrect.rect().fTop + dr);
1124 SkPoint lowerRightOffset = SkPoint::Make(spotShadowRRect.rect().fRight -
1125 rrect.rect().fRight - dr,
1126 spotShadowRRect.rect().fBottom -
1127 rrect.rect().fBottom - dr);
1128 maxOffset = SkScalarSqrt(SkTMax(upperLeftOffset.lengthSqd(),
1129 lowerRightOffset.lengthSqd())) + dr;
1130 }
1131 insetWidth += maxOffset;
1132 }
1133
1134 // Outset the shadow rrect to the border of the penumbra
1135 SkRect outsetRect = spotShadowRRect.rect().makeOutset(blurOutset, blurOutset);
1136 if (spotShadowRRect.isOval()) {
1137 spotShadowRRect = SkRRect::MakeOval(outsetRect);
1138 } else {
1139 SkScalar outsetRad = spotRadius + blurOutset;
1140 spotShadowRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1141 }
1142
1143 GrColor spotColor = color.mulByScalar(rec.fSpotAlpha).toGrColor();
1144 std::unique_ptr<GrLegacyMeshDrawOp> op = GrShadowRRectOp::Make(spotColor, viewMatrix,
1145 spotShadowRRect,
1146 devSpaceSpotBlur,
1147 insetWidth);
1148 if (op) {
1149 GrPipelineBuilder pipelineBuilder(std::move(paint), GrAAType::kNone);
1150 this->addLegacyMeshDrawOp(std::move(pipelineBuilder), clip, std::move(op));
1151 }
1152 }
1153
1154 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001155}
1156
1157///////////////////////////////////////////////////////////////////////////////
1158
Brian Osman11052242016-10-27 14:47:55 -04001159bool GrRenderTargetContext::drawFilledDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001160 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001161 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001162 const SkMatrix& viewMatrix,
1163 const SkRRect& origOuter,
1164 const SkRRect& origInner) {
robertphillips00095892016-02-29 13:50:40 -08001165 SkASSERT(!origInner.isEmpty());
1166 SkASSERT(!origOuter.isEmpty());
1167
csmartdaltone0d36292016-07-29 08:14:20 -07001168 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport()) {
Robert Phillipse3302df2017-04-24 07:31:02 -04001169 gr_instanced::OpAllocator* oa = this->drawingManager()->instancingAllocator();
1170 std::unique_ptr<GrDrawOp> op(oa->recordDRRect(
Brian Salomon54d212e2017-03-21 14:22:38 -04001171 origOuter, origInner, viewMatrix, std::move(paint), aa, fInstancedPipelineInfo));
Brian Salomon42521e82016-12-07 16:44:58 -05001172 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -04001173 this->addDrawOp(clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -07001174 return true;
1175 }
1176 }
1177
Brian Salomon7c8460e2017-05-12 11:36:10 -04001178 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
robertphillips00095892016-02-29 13:50:40 -08001179
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001180 GrPrimitiveEdgeType innerEdgeType, outerEdgeType;
1181 if (GrAAType::kCoverage == aaType) {
1182 innerEdgeType = kInverseFillAA_GrProcessorEdgeType;
1183 outerEdgeType = kFillAA_GrProcessorEdgeType;
1184 } else {
1185 innerEdgeType = kInverseFillBW_GrProcessorEdgeType;
1186 outerEdgeType = kFillBW_GrProcessorEdgeType;
1187 }
robertphillips00095892016-02-29 13:50:40 -08001188
1189 SkTCopyOnFirstWrite<SkRRect> inner(origInner), outer(origOuter);
1190 SkMatrix inverseVM;
1191 if (!viewMatrix.isIdentity()) {
1192 if (!origInner.transform(viewMatrix, inner.writable())) {
1193 return false;
1194 }
1195 if (!origOuter.transform(viewMatrix, outer.writable())) {
1196 return false;
1197 }
1198 if (!viewMatrix.invert(&inverseVM)) {
1199 return false;
1200 }
1201 } else {
1202 inverseVM.reset();
halcanary9d524f22016-03-29 09:03:52 -07001203 }
robertphillips00095892016-02-29 13:50:40 -08001204
robertphillips00095892016-02-29 13:50:40 -08001205 // TODO these need to be a geometry processors
bungeman06ca8ec2016-06-09 08:01:03 -07001206 sk_sp<GrFragmentProcessor> innerEffect(GrRRectEffect::Make(innerEdgeType, *inner));
robertphillips00095892016-02-29 13:50:40 -08001207 if (!innerEffect) {
1208 return false;
1209 }
1210
bungeman06ca8ec2016-06-09 08:01:03 -07001211 sk_sp<GrFragmentProcessor> outerEffect(GrRRectEffect::Make(outerEdgeType, *outer));
robertphillips00095892016-02-29 13:50:40 -08001212 if (!outerEffect) {
1213 return false;
1214 }
1215
Brian Salomon82f44312017-01-11 13:42:54 -05001216 paint.addCoverageFragmentProcessor(std::move(innerEffect));
1217 paint.addCoverageFragmentProcessor(std::move(outerEffect));
robertphillips00095892016-02-29 13:50:40 -08001218
1219 SkRect bounds = outer->getBounds();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001220 if (GrAAType::kCoverage == aaType) {
robertphillips00095892016-02-29 13:50:40 -08001221 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1222 }
halcanary9d524f22016-03-29 09:03:52 -07001223
Brian Salomon82f44312017-01-11 13:42:54 -05001224 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), bounds,
1225 inverseVM);
robertphillips00095892016-02-29 13:50:40 -08001226 return true;
1227}
1228
Brian Osman11052242016-10-27 14:47:55 -04001229void GrRenderTargetContext::drawDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001230 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001231 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001232 const SkMatrix& viewMatrix,
1233 const SkRRect& outer,
1234 const SkRRect& inner) {
robertphillips00095892016-02-29 13:50:40 -08001235 ASSERT_SINGLE_OWNER
1236 RETURN_IF_ABANDONED
1237 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -04001238 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawDRRect");
robertphillips00095892016-02-29 13:50:40 -08001239
1240 SkASSERT(!outer.isEmpty());
1241 SkASSERT(!inner.isEmpty());
1242
Robert Phillips72152832017-01-25 17:31:35 -05001243 AutoCheckFlush acf(this->drawingManager());
robertphillips00095892016-02-29 13:50:40 -08001244
Brian Salomon82f44312017-01-11 13:42:54 -05001245 if (this->drawFilledDRRect(clip, std::move(paint), aa, viewMatrix, outer, inner)) {
robertphillips00095892016-02-29 13:50:40 -08001246 return;
1247 }
1248
1249 SkPath path;
1250 path.setIsVolatile(true);
1251 path.addRRect(inner);
1252 path.addRRect(outer);
1253 path.setFillType(SkPath::kEvenOdd_FillType);
1254
Brian Salomon82f44312017-01-11 13:42:54 -05001255 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, GrStyle::SimpleFill());
robertphillips00095892016-02-29 13:50:40 -08001256}
1257
robertphillipsea461502015-05-26 11:38:03 -07001258///////////////////////////////////////////////////////////////////////////////
1259
msarettcc319b92016-08-25 18:07:18 -07001260static inline bool is_int(float x) {
1261 return x == (float) sk_float_round2int(x);
1262}
1263
Brian Osman11052242016-10-27 14:47:55 -04001264void GrRenderTargetContext::drawRegion(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001265 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001266 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001267 const SkMatrix& viewMatrix,
1268 const SkRegion& region,
1269 const GrStyle& style) {
msarettcc319b92016-08-25 18:07:18 -07001270 ASSERT_SINGLE_OWNER
1271 RETURN_IF_ABANDONED
1272 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -04001273 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawRegion");
msarettcc319b92016-08-25 18:07:18 -07001274
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001275 if (GrAA::kYes == aa) {
Brian Salomonfc527d22016-12-14 21:07:01 -05001276 // GrRegionOp performs no antialiasing but is much faster, so here we check the matrix
Brian Salomonc57c7c92016-12-06 14:47:34 -05001277 // to see whether aa is really required.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001278 if (!SkToBool(viewMatrix.getType() & ~(SkMatrix::kTranslate_Mask)) &&
1279 is_int(viewMatrix.getTranslateX()) &&
1280 is_int(viewMatrix.getTranslateY())) {
1281 aa = GrAA::kNo;
1282 }
Brian Salomonc57c7c92016-12-06 14:47:34 -05001283 }
msarettcc319b92016-08-25 18:07:18 -07001284 bool complexStyle = !style.isSimpleFill();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001285 if (complexStyle || GrAA::kYes == aa) {
msarettcc319b92016-08-25 18:07:18 -07001286 SkPath path;
1287 region.getBoundaryPath(&path);
Brian Salomon82f44312017-01-11 13:42:54 -05001288 return this->drawPath(clip, std::move(paint), aa, viewMatrix, path, style);
msarettcc319b92016-08-25 18:07:18 -07001289 }
1290
Brian Salomond3ccb0a2017-04-03 10:38:00 -04001291 std::unique_ptr<GrLegacyMeshDrawOp> op = GrRegionOp::Make(paint.getColor(), viewMatrix, region);
Brian Salomon82f44312017-01-11 13:42:54 -05001292 GrPipelineBuilder pipelineBuilder(std::move(paint), GrAAType::kNone);
Brian Salomone14bd802017-04-04 15:13:25 -04001293 this->addLegacyMeshDrawOp(std::move(pipelineBuilder), clip, std::move(op));
msarettcc319b92016-08-25 18:07:18 -07001294}
1295
Brian Osman11052242016-10-27 14:47:55 -04001296void GrRenderTargetContext::drawOval(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001297 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001298 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001299 const SkMatrix& viewMatrix,
1300 const SkRect& oval,
1301 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001302 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001303 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001304 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -04001305 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawOval");
robertphillips2e1e51f2015-10-15 08:01:48 -07001306
robertphillipsea461502015-05-26 11:38:03 -07001307 if (oval.isEmpty()) {
1308 return;
1309 }
1310
bsalomon6663acf2016-05-10 09:14:17 -07001311 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
robertphillipsea461502015-05-26 11:38:03 -07001312
Robert Phillips72152832017-01-25 17:31:35 -05001313 AutoCheckFlush acf(this->drawingManager());
bsalomon6663acf2016-05-10 09:14:17 -07001314 const SkStrokeRec& stroke = style.strokeRec();
csmartdaltona7f29642016-07-07 08:49:11 -07001315
csmartdaltone0d36292016-07-29 08:14:20 -07001316 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport() &&
1317 stroke.isFillStyle()) {
Robert Phillipse3302df2017-04-24 07:31:02 -04001318 gr_instanced::OpAllocator* oa = this->drawingManager()->instancingAllocator();
Brian Salomon54d212e2017-03-21 14:22:38 -04001319 std::unique_ptr<GrDrawOp> op(
Robert Phillipse3302df2017-04-24 07:31:02 -04001320 oa->recordOval(oval, viewMatrix, std::move(paint), aa, fInstancedPipelineInfo));
Brian Salomon42521e82016-12-07 16:44:58 -05001321 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -04001322 this->addDrawOp(clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -07001323 return;
1324 }
1325 }
1326
Brian Salomon7c8460e2017-05-12 11:36:10 -04001327 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001328 if (GrAAType::kCoverage == aaType) {
Brian Salomon94efbf52016-11-29 13:43:05 -05001329 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomon05441c42017-05-15 16:45:49 -04001330 std::unique_ptr<GrDrawOp> op =
1331 GrOvalOpFactory::MakeOvalOp(std::move(paint), viewMatrix, oval, stroke, shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -05001332 if (op) {
Brian Salomon05441c42017-05-15 16:45:49 -04001333 this->addDrawOp(clip, std::move(op));
robertphillipsb56f9272016-02-25 11:03:52 -08001334 return;
1335 }
robertphillipsea461502015-05-26 11:38:03 -07001336 }
robertphillipsb56f9272016-02-25 11:03:52 -08001337
1338 SkPath path;
1339 path.setIsVolatile(true);
1340 path.addOval(oval);
Brian Salomon82f44312017-01-11 13:42:54 -05001341 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, style);
robertphillipsea461502015-05-26 11:38:03 -07001342}
1343
Brian Osman11052242016-10-27 14:47:55 -04001344void GrRenderTargetContext::drawArc(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001345 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001346 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001347 const SkMatrix& viewMatrix,
1348 const SkRect& oval,
1349 SkScalar startAngle,
1350 SkScalar sweepAngle,
1351 bool useCenter,
1352 const GrStyle& style) {
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001353 ASSERT_SINGLE_OWNER
1354 RETURN_IF_ABANDONED
1355 SkDEBUGCODE(this->validate();)
1356 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawArc");
1357
1358 AutoCheckFlush acf(this->drawingManager());
1359
Brian Salomon7c8460e2017-05-12 11:36:10 -04001360 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001361 if (GrAAType::kCoverage == aaType) {
Brian Salomon94efbf52016-11-29 13:43:05 -05001362 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomon05441c42017-05-15 16:45:49 -04001363 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeArcOp(std::move(paint),
1364 viewMatrix,
1365 oval,
1366 startAngle,
1367 sweepAngle,
1368 useCenter,
1369 style,
1370 shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -05001371 if (op) {
Brian Salomon05441c42017-05-15 16:45:49 -04001372 this->addDrawOp(clip, std::move(op));
bsalomon4f3a0ca2016-08-22 13:14:26 -07001373 return;
1374 }
1375 }
1376 SkPath path;
bsalomon21af9ca2016-08-25 12:29:23 -07001377 SkPathPriv::CreateDrawArcPath(&path, oval, startAngle, sweepAngle, useCenter,
1378 style.isSimpleFill());
Brian Salomon82f44312017-01-11 13:42:54 -05001379 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, style);
bsalomon4f3a0ca2016-08-22 13:14:26 -07001380}
1381
Brian Osman11052242016-10-27 14:47:55 -04001382void GrRenderTargetContext::drawImageLattice(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001383 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001384 const SkMatrix& viewMatrix,
1385 int imageWidth,
1386 int imageHeight,
1387 std::unique_ptr<SkLatticeIter> iter,
1388 const SkRect& dst) {
joshualitt1de610a2016-01-06 08:26:09 -08001389 ASSERT_SINGLE_OWNER
joshualitt33a5fce2015-11-18 13:28:51 -08001390 RETURN_IF_ABANDONED
1391 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -04001392 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawImageLattice");
joshualitt33a5fce2015-11-18 13:28:51 -08001393
Robert Phillips72152832017-01-25 17:31:35 -05001394 AutoCheckFlush acf(this->drawingManager());
joshualitt33a5fce2015-11-18 13:28:51 -08001395
Brian Salomond3ccb0a2017-04-03 10:38:00 -04001396 std::unique_ptr<GrLegacyMeshDrawOp> op = GrLatticeOp::MakeNonAA(
Brian Salomon649a3412017-03-09 13:50:43 -05001397 paint.getColor(), viewMatrix, imageWidth, imageHeight, std::move(iter), dst);
joshualitt33a5fce2015-11-18 13:28:51 -08001398
Brian Salomon82f44312017-01-11 13:42:54 -05001399 GrPipelineBuilder pipelineBuilder(std::move(paint), GrAAType::kNone);
Brian Salomone14bd802017-04-04 15:13:25 -04001400 this->addLegacyMeshDrawOp(std::move(pipelineBuilder), clip, std::move(op));
joshualitt33a5fce2015-11-18 13:28:51 -08001401}
1402
Brian Osman11052242016-10-27 14:47:55 -04001403void GrRenderTargetContext::prepareForExternalIO() {
robertphillips8c523e02016-07-26 07:41:00 -07001404 ASSERT_SINGLE_OWNER
1405 RETURN_IF_ABANDONED
1406 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -04001407 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::prepareForExternalIO");
robertphillips8c523e02016-07-26 07:41:00 -07001408
Robert Phillips7ee385e2017-03-30 08:02:11 -04001409 this->drawingManager()->prepareSurfaceForExternalIO(fRenderTargetProxy.get());
robertphillips8c523e02016-07-26 07:41:00 -07001410}
joshualitt33a5fce2015-11-18 13:28:51 -08001411
Brian Osman11052242016-10-27 14:47:55 -04001412void GrRenderTargetContext::drawNonAAFilledRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001413 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001414 const SkMatrix& viewMatrix,
1415 const SkRect& rect,
1416 const SkRect* localRect,
1417 const SkMatrix* localMatrix,
1418 const GrUserStencilSettings* ss,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001419 GrAAType hwOrNoneAAType) {
1420 SkASSERT(GrAAType::kCoverage != hwOrNoneAAType);
Brian Salomon7c8460e2017-05-12 11:36:10 -04001421 SkASSERT(GrAAType::kNone == hwOrNoneAAType || GrFSAAType::kNone != this->fsaaType());
Brian Salomonac70f842017-05-08 10:43:33 -04001422 std::unique_ptr<GrDrawOp> op = GrNonAAFillRectOp::Make(
1423 std::move(paint), viewMatrix, rect, localRect, localMatrix, hwOrNoneAAType, ss);
1424 this->addDrawOp(clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -07001425}
1426
robertphillipsea461502015-05-26 11:38:03 -07001427// Can 'path' be drawn as a pair of filled nested rectangles?
bsalomon6663acf2016-05-10 09:14:17 -07001428static bool fills_as_nested_rects(const SkMatrix& viewMatrix, const SkPath& path, SkRect rects[2]) {
robertphillipsea461502015-05-26 11:38:03 -07001429
1430 if (path.isInverseFillType()) {
1431 return false;
1432 }
1433
1434 // TODO: this restriction could be lifted if we were willing to apply
1435 // the matrix to all the points individually rather than just to the rect
robertphillips0e7029e2015-11-30 05:45:06 -08001436 if (!viewMatrix.rectStaysRect()) {
robertphillipsea461502015-05-26 11:38:03 -07001437 return false;
1438 }
1439
1440 SkPath::Direction dirs[2];
1441 if (!path.isNestedFillRects(rects, dirs)) {
1442 return false;
1443 }
1444
1445 if (SkPath::kWinding_FillType == path.getFillType() && dirs[0] == dirs[1]) {
1446 // The two rects need to be wound opposite to each other
1447 return false;
1448 }
1449
1450 // Right now, nested rects where the margin is not the same width
1451 // all around do not render correctly
1452 const SkScalar* outer = rects[0].asScalars();
1453 const SkScalar* inner = rects[1].asScalars();
1454
1455 bool allEq = true;
1456
1457 SkScalar margin = SkScalarAbs(outer[0] - inner[0]);
1458 bool allGoE1 = margin >= SK_Scalar1;
1459
1460 for (int i = 1; i < 4; ++i) {
1461 SkScalar temp = SkScalarAbs(outer[i] - inner[i]);
1462 if (temp < SK_Scalar1) {
1463 allGoE1 = false;
1464 }
1465 if (!SkScalarNearlyEqual(margin, temp)) {
1466 allEq = false;
1467 }
1468 }
1469
1470 return allEq || allGoE1;
1471}
1472
Brian Osman11052242016-10-27 14:47:55 -04001473void GrRenderTargetContext::drawPath(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001474 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001475 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001476 const SkMatrix& viewMatrix,
1477 const SkPath& path,
1478 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001479 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001480 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001481 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -04001482 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawPath");
robertphillips2e1e51f2015-10-15 08:01:48 -07001483
robertphillipsea461502015-05-26 11:38:03 -07001484 if (path.isEmpty()) {
1485 if (path.isInverseFillType()) {
Brian Salomon82f44312017-01-11 13:42:54 -05001486 this->drawPaint(clip, std::move(paint), viewMatrix);
robertphillipsea461502015-05-26 11:38:03 -07001487 }
1488 return;
1489 }
1490
Robert Phillips72152832017-01-25 17:31:35 -05001491 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -07001492
Brian Salomon7c8460e2017-05-12 11:36:10 -04001493 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001494 if (GrAAType::kCoverage == aaType && !style.pathEffect()) {
bsalomon6663acf2016-05-10 09:14:17 -07001495 if (style.isSimpleFill() && !path.isConvex()) {
robertphillipsea461502015-05-26 11:38:03 -07001496 // Concave AA paths are expensive - try to avoid them for special cases
1497 SkRect rects[2];
1498
bsalomon6663acf2016-05-10 09:14:17 -07001499 if (fills_as_nested_rects(viewMatrix, path, rects)) {
Brian Salomond3ccb0a2017-04-03 10:38:00 -04001500 std::unique_ptr<GrLegacyMeshDrawOp> op =
Brian Salomon6a639042016-12-14 11:08:17 -05001501 GrRectOpFactory::MakeAAFillNestedRects(paint.getColor(), viewMatrix, rects);
Brian Salomon21aa35f2016-12-09 16:01:53 -05001502 if (op) {
Brian Salomon82f44312017-01-11 13:42:54 -05001503 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
Brian Salomone14bd802017-04-04 15:13:25 -04001504 this->addLegacyMeshDrawOp(std::move(pipelineBuilder), clip, std::move(op));
bsalomon40ef4852016-05-02 13:22:13 -07001505 }
robertphillipsea461502015-05-26 11:38:03 -07001506 return;
1507 }
1508 }
1509 SkRect ovalRect;
1510 bool isOval = path.isOval(&ovalRect);
1511
1512 if (isOval && !path.isInverseFillType()) {
Brian Salomon94efbf52016-11-29 13:43:05 -05001513 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomon05441c42017-05-15 16:45:49 -04001514 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeOvalOp(
1515 std::move(paint), viewMatrix, ovalRect, style.strokeRec(), shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -05001516 if (op) {
Brian Salomon05441c42017-05-15 16:45:49 -04001517 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -07001518 return;
1519 }
1520 }
1521 }
robertphillips4bc31812016-03-01 12:22:49 -08001522
1523 // Note that internalDrawPath may sw-rasterize the path into a scratch texture.
1524 // Scratch textures can be recycled after they are returned to the texture
1525 // cache. This presents a potential hazard for buffered drawing. However,
1526 // the writePixels that uploads to the scratch will perform a flush so we're
1527 // OK.
Brian Salomon82f44312017-01-11 13:42:54 -05001528 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, style);
robertphillipsea461502015-05-26 11:38:03 -07001529}
1530
Brian Osman11052242016-10-27 14:47:55 -04001531bool GrRenderTargetContextPriv::drawAndStencilPath(const GrClip& clip,
1532 const GrUserStencilSettings* ss,
1533 SkRegion::Op op,
1534 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001535 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001536 const SkMatrix& viewMatrix,
1537 const SkPath& path) {
robertphillips391395d2016-03-02 09:26:36 -08001538 ASSERT_SINGLE_OWNER_PRIV
1539 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -04001540 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001541 GR_AUDIT_TRAIL_AUTO_FRAME(fRenderTargetContext->fAuditTrail,
1542 "GrRenderTargetContextPriv::drawAndStencilPath");
robertphillips391395d2016-03-02 09:26:36 -08001543
1544 if (path.isEmpty() && path.isInverseFillType()) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001545 this->drawAndStencilRect(clip, ss, op, invert, GrAA::kNo, SkMatrix::I(),
Brian Osman11052242016-10-27 14:47:55 -04001546 SkRect::MakeIWH(fRenderTargetContext->width(),
1547 fRenderTargetContext->height()));
robertphillips391395d2016-03-02 09:26:36 -08001548 return true;
1549 }
1550
Robert Phillips72152832017-01-25 17:31:35 -05001551 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -08001552
1553 // An Assumption here is that path renderer would use some form of tweaking
1554 // the src color (either the input alpha or in the frag shader) to implement
1555 // aa. If we have some future driver-mojo path AA that can do the right
1556 // thing WRT to the blend then we'll need some query on the PR.
Brian Salomon7c8460e2017-05-12 11:36:10 -04001557 GrAAType aaType = fRenderTargetContext->chooseAAType(aa, GrAllowMixedSamples::kNo);
robertphillips976f5f02016-06-03 10:59:20 -07001558 bool hasUserStencilSettings = !ss->isUnused();
robertphillips391395d2016-03-02 09:26:36 -08001559
bsalomon8acedde2016-06-24 10:42:16 -07001560 GrShape shape(path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -08001561 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Eric Karl5c779752017-05-08 12:02:07 -07001562 canDrawArgs.fCaps = fRenderTargetContext->drawingManager()->getContext()->caps();
robertphillips391395d2016-03-02 09:26:36 -08001563 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07001564 canDrawArgs.fShape = &shape;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001565 canDrawArgs.fAAType = aaType;
cdalton93a379b2016-05-11 13:58:08 -07001566 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips391395d2016-03-02 09:26:36 -08001567
1568 // Don't allow the SW renderer
Robert Phillips72152832017-01-25 17:31:35 -05001569 GrPathRenderer* pr = fRenderTargetContext->drawingManager()->getPathRenderer(
Brian Salomon36aa1762016-12-10 13:24:02 -05001570 canDrawArgs, false, GrPathRendererChain::DrawType::kStencilAndColor);
robertphillips391395d2016-03-02 09:26:36 -08001571 if (!pr) {
1572 return false;
1573 }
1574
1575 GrPaint paint;
1576 paint.setCoverageSetOpXPFactory(op, invert);
1577
Brian Salomon82f44312017-01-11 13:42:54 -05001578 GrPathRenderer::DrawPathArgs args{
Robert Phillips256c37b2017-03-01 14:32:46 -05001579 fRenderTargetContext->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05001580 std::move(paint),
1581 ss,
1582 fRenderTargetContext,
1583 &clip,
1584 &viewMatrix,
1585 &shape,
1586 aaType,
1587 fRenderTargetContext->isGammaCorrect()};
robertphillips391395d2016-03-02 09:26:36 -08001588 pr->drawPath(args);
1589 return true;
1590}
1591
Brian Osman11052242016-10-27 14:47:55 -04001592SkBudgeted GrRenderTargetContextPriv::isBudgeted() const {
robertphillips714712b2016-08-04 06:20:45 -07001593 ASSERT_SINGLE_OWNER_PRIV
1594
Brian Osman11052242016-10-27 14:47:55 -04001595 if (fRenderTargetContext->wasAbandoned()) {
robertphillips714712b2016-08-04 06:20:45 -07001596 return SkBudgeted::kNo;
1597 }
1598
Brian Osman11052242016-10-27 14:47:55 -04001599 SkDEBUGCODE(fRenderTargetContext->validate();)
robertphillips714712b2016-08-04 06:20:45 -07001600
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001601 return fRenderTargetContext->fRenderTargetProxy->isBudgeted();
robertphillips714712b2016-08-04 06:20:45 -07001602}
1603
Brian Osman11052242016-10-27 14:47:55 -04001604void GrRenderTargetContext::internalDrawPath(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001605 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001606 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001607 const SkMatrix& viewMatrix,
1608 const SkPath& path,
1609 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001610 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001611 RETURN_IF_ABANDONED
bsalomon8acedde2016-06-24 10:42:16 -07001612 SkASSERT(!path.isEmpty());
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001613 GrShape shape;
Brian Salomon7c8460e2017-05-12 11:36:10 -04001614 // NVPR cannot handle hairlines, so this would get picked up by a different stencil and
1615 // cover path renderer (i.e. default path renderer). The hairline renderer produces much
1616 // smoother hairlines than MSAA.
1617 GrAllowMixedSamples allowMixedSamples =
1618 style.isSimpleHairline() ? GrAllowMixedSamples::kNo : GrAllowMixedSamples::kYes;
1619 GrAAType aaType = this->chooseAAType(aa, allowMixedSamples);
robertphillips68737822015-10-29 12:12:21 -07001620 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Eric Karl5c779752017-05-08 12:02:07 -07001621 canDrawArgs.fCaps = this->drawingManager()->getContext()->caps();
robertphillips68737822015-10-29 12:12:21 -07001622 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07001623 canDrawArgs.fShape = &shape;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001624 canDrawArgs.fHasUserStencilSettings = false;
robertphillips68737822015-10-29 12:12:21 -07001625
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001626 GrPathRenderer* pr;
Brian Salomon82125e92016-12-10 09:35:48 -05001627 static constexpr GrPathRendererChain::DrawType kType = GrPathRendererChain::DrawType::kColor;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001628 do {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001629 shape = GrShape(path, style);
bsalomon8acedde2016-06-24 10:42:16 -07001630 if (shape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001631 return;
1632 }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001633
1634 canDrawArgs.fAAType = aaType;
1635
1636 // Try a 1st time without applying any of the style to the geometry (and barring sw)
Robert Phillips72152832017-01-25 17:31:35 -05001637 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001638 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
1639
1640 if (!pr && shape.style().pathEffect()) {
1641 // It didn't work above, so try again with the path effect applied.
1642 shape = shape.applyStyle(GrStyle::Apply::kPathEffectOnly, styleScale);
bsalomon8acedde2016-06-24 10:42:16 -07001643 if (shape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001644 return;
1645 }
Robert Phillips72152832017-01-25 17:31:35 -05001646 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
bsalomon6663acf2016-05-10 09:14:17 -07001647 }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001648 if (!pr) {
1649 if (shape.style().applies()) {
1650 shape = shape.applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec, styleScale);
1651 if (shape.isEmpty()) {
1652 return;
1653 }
1654 }
1655 // This time, allow SW renderer
Robert Phillips72152832017-01-25 17:31:35 -05001656 pr = this->drawingManager()->getPathRenderer(canDrawArgs, true, kType);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001657 }
Brian Salomon0abc8b42016-12-13 10:22:54 -05001658 if (!pr && GrAATypeIsHW(aaType)) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001659 // There are exceptional cases where we may wind up falling back to coverage based AA
1660 // when the target is MSAA (e.g. through disabling path renderers via GrContextOptions).
1661 aaType = GrAAType::kCoverage;
1662 } else {
1663 break;
1664 }
1665 } while(true);
robertphillipsea461502015-05-26 11:38:03 -07001666
bsalomon8acedde2016-06-24 10:42:16 -07001667 if (!pr) {
robertphillipsea461502015-05-26 11:38:03 -07001668#ifdef SK_DEBUG
1669 SkDebugf("Unable to find path renderer compatible with path.\n");
1670#endif
1671 return;
1672 }
1673
Robert Phillips256c37b2017-03-01 14:32:46 -05001674 GrPathRenderer::DrawPathArgs args{this->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05001675 std::move(paint),
1676 &GrUserStencilSettings::kUnused,
1677 this,
1678 &clip,
1679 &viewMatrix,
1680 &shape,
1681 aaType,
1682 this->isGammaCorrect()};
bsalomon0aff2fa2015-07-31 06:48:27 -07001683 pr->drawPath(args);
robertphillipsea461502015-05-26 11:38:03 -07001684}
1685
Brian Salomon467921e2017-03-06 16:17:12 -05001686static void op_bounds(SkRect* bounds, const GrOp* op) {
1687 *bounds = op->bounds();
1688 if (op->hasZeroArea()) {
1689 if (op->hasAABloat()) {
1690 bounds->outset(0.5f, 0.5f);
1691 } else {
1692 // We don't know which way the particular GPU will snap lines or points at integer
1693 // coords. So we ensure that the bounds is large enough for either snap.
1694 SkRect before = *bounds;
1695 bounds->roundOut(bounds);
1696 if (bounds->fLeft == before.fLeft) {
1697 bounds->fLeft -= 1;
1698 }
1699 if (bounds->fTop == before.fTop) {
1700 bounds->fTop -= 1;
1701 }
1702 if (bounds->fRight == before.fRight) {
1703 bounds->fRight += 1;
1704 }
1705 if (bounds->fBottom == before.fBottom) {
1706 bounds->fBottom += 1;
1707 }
1708 }
1709 }
1710}
1711
Brian Salomon54d212e2017-03-21 14:22:38 -04001712uint32_t GrRenderTargetContext::addDrawOp(const GrClip& clip, std::unique_ptr<GrDrawOp> op) {
joshualitt1de610a2016-01-06 08:26:09 -08001713 ASSERT_SINGLE_OWNER
Robert Phillipsc0138922017-03-08 11:50:55 -05001714 if (this->drawingManager()->wasAbandoned()) {
1715 return SK_InvalidUniqueID;
1716 }
robertphillips2e1e51f2015-10-15 08:01:48 -07001717 SkDEBUGCODE(this->validate();)
Brian Salomon42521e82016-12-07 16:44:58 -05001718 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::addDrawOp");
robertphillips2d70dcb2015-10-06 07:38:23 -07001719
Brian Salomon467921e2017-03-06 16:17:12 -05001720 // Setup clip
1721 SkRect bounds;
1722 op_bounds(&bounds, op.get());
Brian Salomon97180af2017-03-14 13:42:58 -04001723 GrAppliedClip appliedClip;
Brian Salomon54d212e2017-03-21 14:22:38 -04001724 GrDrawOp::FixedFunctionFlags fixedFunctionFlags = op->fixedFunctionFlags();
1725 if (!clip.apply(fContext, this, fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesHWAA,
1726 fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil, &appliedClip,
1727 &bounds)) {
1728 return SK_InvalidUniqueID;
1729 }
1730
Brian Salomon54d212e2017-03-21 14:22:38 -04001731 if (fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil ||
1732 appliedClip.hasStencilClip()) {
Robert Phillips5efd5ea2017-05-30 13:47:32 -04001733 // This forces instantiation of the render target.
1734 GrRenderTarget* rt = this->accessRenderTarget();
1735 if (!rt) {
1736 return SK_InvalidUniqueID;
1737 }
1738
Brian Salomon54d212e2017-03-21 14:22:38 -04001739 if (!fContext->resourceProvider()->attachStencilAttachment(rt)) {
1740 SkDebugf("ERROR creating stencil attachment. Draw skipped.\n");
1741 return SK_InvalidUniqueID;
1742 }
1743 }
1744
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001745 GrXferProcessor::DstProxy dstProxy;
Brian Salomon54d212e2017-03-21 14:22:38 -04001746 if (op->xpRequiresDstTexture(*this->caps(), &appliedClip)) {
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001747 if (!this->setupDstProxy(this->asRenderTargetProxy(), clip, op->bounds(), &dstProxy)) {
Brian Salomon54d212e2017-03-21 14:22:38 -04001748 return SK_InvalidUniqueID;
1749 }
1750 }
1751
1752 op->setClippedBounds(bounds);
Robert Phillips5efd5ea2017-05-30 13:47:32 -04001753 return this->getOpList()->addOp(std::move(op), *this->caps(),
1754 std::move(appliedClip), dstProxy);
Brian Salomon54d212e2017-03-21 14:22:38 -04001755}
1756
Brian Salomone14bd802017-04-04 15:13:25 -04001757uint32_t GrRenderTargetContext::addLegacyMeshDrawOp(GrPipelineBuilder&& pipelineBuilder,
Brian Salomond3ccb0a2017-04-03 10:38:00 -04001758 const GrClip& clip,
1759 std::unique_ptr<GrLegacyMeshDrawOp> op) {
Brian Salomon54d212e2017-03-21 14:22:38 -04001760 ASSERT_SINGLE_OWNER
1761 if (this->drawingManager()->wasAbandoned()) {
1762 return SK_InvalidUniqueID;
1763 }
1764 SkDEBUGCODE(this->validate();)
Brian Salomond3ccb0a2017-04-03 10:38:00 -04001765 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::addLegacyMeshDrawOp");
Brian Salomon54d212e2017-03-21 14:22:38 -04001766
1767 // Setup clip
1768 SkRect bounds;
1769 op_bounds(&bounds, op.get());
1770 GrAppliedClip appliedClip;
Brian Salomon467921e2017-03-06 16:17:12 -05001771 if (!clip.apply(fContext, this, pipelineBuilder.isHWAntialias(),
Brian Salomon97180af2017-03-14 13:42:58 -04001772 pipelineBuilder.hasUserStencilSettings(), &appliedClip, &bounds)) {
Robert Phillipsc0138922017-03-08 11:50:55 -05001773 return SK_InvalidUniqueID;
Brian Salomon467921e2017-03-06 16:17:12 -05001774 }
1775
1776 // This forces instantiation of the render target. Pipeline creation is moving to flush time
1777 // by which point instantiation must have occurred anyway.
1778 GrRenderTarget* rt = this->accessRenderTarget();
1779 if (!rt) {
Robert Phillipsc0138922017-03-08 11:50:55 -05001780 return SK_InvalidUniqueID;
Brian Salomon467921e2017-03-06 16:17:12 -05001781 }
1782
1783 GrResourceProvider* resourceProvider = fContext->resourceProvider();
Brian Salomon48d1b4c2017-04-08 07:38:53 -04001784 bool usesStencil = pipelineBuilder.hasUserStencilSettings() || appliedClip.hasStencilClip();
1785 if (usesStencil) {
Brian Salomon467921e2017-03-06 16:17:12 -05001786 if (!resourceProvider->attachStencilAttachment(this->accessRenderTarget())) {
1787 SkDebugf("ERROR creating stencil attachment. Draw skipped.\n");
Robert Phillipsc0138922017-03-08 11:50:55 -05001788 return SK_InvalidUniqueID;
Brian Salomon467921e2017-03-06 16:17:12 -05001789 }
1790 }
1791
Brian Salomon7c8460e2017-05-12 11:36:10 -04001792 bool isMixedSamples = GrFSAAType::kMixedSamples == this->fsaaType() &&
Brian Salomon48d1b4c2017-04-08 07:38:53 -04001793 (pipelineBuilder.isHWAntialias() || usesStencil);
1794
1795 GrColor overrideColor;
1796 GrProcessorSet::Analysis analysis = op->analyzeUpdateAndRecordProcessors(
1797 &pipelineBuilder, &appliedClip, isMixedSamples, *this->caps(), &overrideColor);
Brian Salomon467921e2017-03-06 16:17:12 -05001798
1799 GrPipeline::InitArgs args;
1800 pipelineBuilder.getPipelineInitArgs(&args);
1801 args.fAppliedClip = &appliedClip;
1802 args.fRenderTarget = rt;
1803 args.fCaps = this->caps();
Robert Phillips9bee2e52017-05-29 12:37:20 -04001804 args.fResourceProvider = this->resourceProvider();
Brian Salomon467921e2017-03-06 16:17:12 -05001805
Brian Salomon31853842017-03-28 16:32:05 -04001806 if (analysis.requiresDstTexture()) {
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001807 if (!this->setupDstProxy(this->asRenderTargetProxy(), clip, bounds, &args.fDstProxy)) {
Robert Phillipsc0138922017-03-08 11:50:55 -05001808 return SK_InvalidUniqueID;
Brian Salomon467921e2017-03-06 16:17:12 -05001809 }
1810 }
Brian Salomon48d1b4c2017-04-08 07:38:53 -04001811 op->initPipeline(args, analysis, overrideColor);
Robert Phillipsf5442bb2017-04-17 14:18:34 -04001812
1813 // Add the pipeline dependencies on textures, etc before recording this op.
1814 op->addDependenciesTo(fRenderTargetProxy.get());
1815
Brian Salomon97180af2017-03-14 13:42:58 -04001816 op->setClippedBounds(bounds);
Robert Phillips5efd5ea2017-05-30 13:47:32 -04001817 return this->getOpList()->addOp(std::move(op), *this->caps());
Brian Salomon467921e2017-03-06 16:17:12 -05001818}
1819
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001820bool GrRenderTargetContext::setupDstProxy(GrRenderTargetProxy* rtProxy, const GrClip& clip,
Brian Salomon467921e2017-03-06 16:17:12 -05001821 const SkRect& opBounds,
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001822 GrXferProcessor::DstProxy* dstProxy) {
Brian Salomon467921e2017-03-06 16:17:12 -05001823 if (this->caps()->textureBarrierSupport()) {
Robert Phillipsbf25d432017-04-07 10:08:53 -04001824 if (GrTextureProxy* texProxy = rtProxy->asTextureProxy()) {
Brian Salomon467921e2017-03-06 16:17:12 -05001825 // The render target is a texture, so we can read from it directly in the shader. The XP
1826 // will be responsible to detect this situation and request a texture barrier.
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001827 dstProxy->setProxy(sk_ref_sp(texProxy));
1828 dstProxy->setOffset(0, 0);
Robert Phillipsbf25d432017-04-07 10:08:53 -04001829 return true;
Brian Salomon467921e2017-03-06 16:17:12 -05001830 }
1831 }
1832
Robert Phillipsbf25d432017-04-07 10:08:53 -04001833 SkIRect copyRect = SkIRect::MakeWH(rtProxy->width(), rtProxy->height());
Brian Salomon467921e2017-03-06 16:17:12 -05001834
Eric Karl74480882017-04-03 14:49:05 -07001835 SkIRect clippedRect;
Robert Phillipsbf25d432017-04-07 10:08:53 -04001836 clip.getConservativeBounds(rtProxy->width(), rtProxy->height(), &clippedRect);
Eric Karl72e551e2017-04-04 13:42:10 -07001837 SkIRect drawIBounds;
Brian Salomon467921e2017-03-06 16:17:12 -05001838 opBounds.roundOut(&drawIBounds);
Brian Salomon859621f2017-03-16 09:21:54 -04001839 // Cover up for any precision issues by outsetting the op bounds a pixel in each direction.
1840 drawIBounds.outset(1, 1);
Eric Karl72e551e2017-04-04 13:42:10 -07001841 if (!clippedRect.intersect(drawIBounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -05001842#ifdef SK_DEBUG
Robert Phillipsbf25d432017-04-07 10:08:53 -04001843 GrCapsDebugf(this->caps(), "setupDstTexture: Missed an early reject bailing on draw.");
Brian Salomon467921e2017-03-06 16:17:12 -05001844#endif
Robert Phillipsbf25d432017-04-07 10:08:53 -04001845 return false;
Brian Salomon467921e2017-03-06 16:17:12 -05001846 }
1847
1848 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
1849 // have per-sample dst values by making the copy multisampled.
1850 GrSurfaceDesc desc;
Eric Karl74480882017-04-03 14:49:05 -07001851 bool rectsMustMatch = false;
1852 bool disallowSubrect = false;
Robert Phillipsbf25d432017-04-07 10:08:53 -04001853 if (!this->caps()->initDescForDstCopy(rtProxy, &desc, &rectsMustMatch, &disallowSubrect)) {
1854 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
Brian Salomon467921e2017-03-06 16:17:12 -05001855 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillipsbf25d432017-04-07 10:08:53 -04001856 desc.fConfig = rtProxy->config();
Brian Salomon467921e2017-03-06 16:17:12 -05001857 }
1858
Eric Karl74480882017-04-03 14:49:05 -07001859 if (!disallowSubrect) {
1860 copyRect = clippedRect;
1861 }
Brian Salomon467921e2017-03-06 16:17:12 -05001862
Robert Phillipsbf25d432017-04-07 10:08:53 -04001863 SkIPoint dstPoint, dstOffset;
1864 SkBackingFit fit;
Eric Karl74480882017-04-03 14:49:05 -07001865 if (rectsMustMatch) {
Robert Phillipsbf25d432017-04-07 10:08:53 -04001866 SkASSERT(desc.fOrigin == rtProxy->origin());
1867 desc.fWidth = rtProxy->width();
1868 desc.fHeight = rtProxy->height();
Eric Karl74480882017-04-03 14:49:05 -07001869 dstPoint = {copyRect.fLeft, copyRect.fTop};
1870 dstOffset = {0, 0};
Robert Phillipsbf25d432017-04-07 10:08:53 -04001871 fit = SkBackingFit::kExact;
Eric Karl74480882017-04-03 14:49:05 -07001872 } else {
1873 desc.fWidth = copyRect.width();
1874 desc.fHeight = copyRect.height();
1875 dstPoint = {0, 0};
1876 dstOffset = {copyRect.fLeft, copyRect.fTop};
Robert Phillipsbf25d432017-04-07 10:08:53 -04001877 fit = SkBackingFit::kApprox;
Eric Karl74480882017-04-03 14:49:05 -07001878 }
Brian Salomon467921e2017-03-06 16:17:12 -05001879
Robert Phillipsbf25d432017-04-07 10:08:53 -04001880 sk_sp<GrSurfaceContext> sContext = fContext->contextPriv().makeDeferredSurfaceContext(
1881 desc,
1882 fit,
1883 SkBudgeted::kYes);
1884 if (!sContext) {
1885 SkDebugf("setupDstTexture: surfaceContext creation failed.\n");
1886 return false;
1887 }
1888
1889 if (!sContext->copy(rtProxy, copyRect, dstPoint)) {
1890 SkDebugf("setupDstTexture: copy failed.\n");
1891 return false;
1892 }
1893
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001894 dstProxy->setProxy(sContext->asTextureProxyRef());
1895 dstProxy->setOffset(dstOffset);
Robert Phillipsbf25d432017-04-07 10:08:53 -04001896 return true;
robertphillips2334fb62015-06-17 05:43:33 -07001897}