blob: f11f45e70d93ebc6ca403b8873d7d9ae6f0cc277 [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 Salomon5ec9def2016-12-20 15:34:05 -05009#include "GrColor.h"
10#include "GrDrawOpTest.h"
robertphillips77a2e522015-10-17 07:43:27 -070011#include "GrDrawingManager.h"
csmartdalton02fa32c2016-08-19 13:29:27 -070012#include "GrFixedClip.h"
robertphillips714712b2016-08-04 06:20:45 -070013#include "GrGpuResourcePriv.h"
robertphillipsea461502015-05-26 11:38:03 -070014#include "GrPathRenderer.h"
robertphillips5fa7f302016-07-21 09:21:04 -070015#include "GrPipelineBuilder.h"
robertphillips2334fb62015-06-17 05:43:33 -070016#include "GrRenderTarget.h"
Brian Salomon5ec9def2016-12-20 15:34:05 -050017#include "GrRenderTargetContextPriv.h"
robertphillips2334fb62015-06-17 05:43:33 -070018#include "GrRenderTargetPriv.h"
bsalomon473addf2015-10-02 07:49:05 -070019#include "GrResourceProvider.h"
robertphillips2d70dcb2015-10-06 07:38:23 -070020#include "SkSurfacePriv.h"
robertphillipsea461502015-05-26 11:38:03 -070021
Brian Salomon89527432016-12-16 09:52:16 -050022#include "ops/GrClearOp.h"
23#include "ops/GrDrawAtlasOp.h"
24#include "ops/GrDrawVerticesOp.h"
25#include "ops/GrLatticeOp.h"
26#include "ops/GrOp.h"
27#include "ops/GrOvalOpFactory.h"
28#include "ops/GrRectOpFactory.h"
29#include "ops/GrRegionOp.h"
30#include "ops/GrShadowRRectOp.h"
joshualitt74417822015-08-07 11:42:16 -070031
robertphillips00095892016-02-29 13:50:40 -080032#include "effects/GrRRectEffect.h"
33
csmartdaltona7f29642016-07-07 08:49:11 -070034#include "instanced/InstancedRendering.h"
35
joshualitte8042922015-12-11 06:11:21 -080036#include "text/GrAtlasTextContext.h"
37#include "text/GrStencilAndCoverTextContext.h"
38
joshualittbc907352016-01-13 06:45:40 -080039#include "../private/GrAuditTrail.h"
40
robertphillips1da3ecd2016-08-31 14:54:15 -070041#include "SkGr.h"
msarett10e3d9b2016-08-18 15:46:03 -070042#include "SkLatticeIter.h"
reeda39667c2016-08-22 06:39:49 -070043#include "SkMatrixPriv.h"
msarett10e3d9b2016-08-18 15:46:03 -070044
Robert Phillips72152832017-01-25 17:31:35 -050045#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this->drawingManager()->getContext())
joshualitt1de610a2016-01-06 08:26:09 -080046#define ASSERT_SINGLE_OWNER \
joshualittde8dc7e2016-01-08 10:09:13 -080047 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fSingleOwner);)
robertphillips391395d2016-03-02 09:26:36 -080048#define ASSERT_SINGLE_OWNER_PRIV \
Brian Osman11052242016-10-27 14:47:55 -040049 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fRenderTargetContext->fSingleOwner);)
Robert Phillips72152832017-01-25 17:31:35 -050050#define RETURN_IF_ABANDONED if (this->drawingManager()->wasAbandoned()) { return; }
51#define RETURN_IF_ABANDONED_PRIV if (fRenderTargetContext->drawingManager()->wasAbandoned()) { return; }
52#define RETURN_FALSE_IF_ABANDONED if (this->drawingManager()->wasAbandoned()) { return false; }
53#define RETURN_FALSE_IF_ABANDONED_PRIV if (fRenderTargetContext->drawingManager()->wasAbandoned()) { return false; }
54#define RETURN_NULL_IF_ABANDONED if (this->drawingManager()->wasAbandoned()) { return nullptr; }
robertphillipsea461502015-05-26 11:38:03 -070055
csmartdaltona7f29642016-07-07 08:49:11 -070056using gr_instanced::InstancedRendering;
57
robertphillipsea461502015-05-26 11:38:03 -070058class 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))
86 , fOpList(SkSafeRef(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
Brian Osman526972e2016-10-24 09:24:02 -040092 auto srgbColorSpace = SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named);
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 Phillipsc7635fa2016-10-28 13:25:24 -0400104 SkASSERT(fRenderTargetProxy->getLastOpList() == fOpList);
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
Robert Phillipsf2361d22016-10-25 14:20:06 -0400111 SkSafeUnref(fOpList);
robertphillipsa106c622015-10-16 09:07:06 -0700112}
113
Robert Phillipseaa86252016-11-08 13:49:39 +0000114GrRenderTarget* GrRenderTargetContext::instantiate() {
115 return fRenderTargetProxy->instantiate(fContext->textureProvider());
116}
117
118GrTextureProxy* GrRenderTargetContext::asDeferredTexture() {
119 return fRenderTargetProxy->asTextureProxy();
120}
121
Brian Osman11052242016-10-27 14:47:55 -0400122GrRenderTargetOpList* GrRenderTargetContext::getOpList() {
joshualitt1de610a2016-01-06 08:26:09 -0800123 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700124 SkDEBUGCODE(this->validate();)
125
Robert Phillipsf2361d22016-10-25 14:20:06 -0400126 if (!fOpList || fOpList->isClosed()) {
Robert Phillips72152832017-01-25 17:31:35 -0500127 fOpList = this->drawingManager()->newOpList(fRenderTargetProxy.get());
robertphillipsa106c622015-10-16 09:07:06 -0700128 }
129
Robert Phillipsf2361d22016-10-25 14:20:06 -0400130 return fOpList;
robertphillipsa106c622015-10-16 09:07:06 -0700131}
132
Robert Phillipse2f7d182016-12-15 09:23:05 -0500133// TODO: move this (and GrTextContext::copy) to GrSurfaceContext?
134bool GrRenderTargetContext::onCopy(GrSurfaceProxy* srcProxy,
135 const SkIRect& srcRect,
136 const SkIPoint& dstPoint) {
joshualitt1de610a2016-01-06 08:26:09 -0800137 ASSERT_SINGLE_OWNER
bsalomonb8fea972016-02-16 07:34:17 -0800138 RETURN_FALSE_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700139 SkDEBUGCODE(this->validate();)
Robert Phillipse2f7d182016-12-15 09:23:05 -0500140 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::copy");
141
142 // TODO: defer instantiation until flush time
143 sk_sp<GrSurface> src(sk_ref_sp(srcProxy->instantiate(fContext->textureProvider())));
144 if (!src) {
145 return false;
146 }
robertphillips2d70dcb2015-10-06 07:38:23 -0700147
Brian Salomon69868af2016-12-22 15:42:51 -0500148 // TODO: This needs to be fixed up since it ends the deferral of the GrRenderTarget.
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400149 sk_sp<GrRenderTarget> rt(
150 sk_ref_sp(fRenderTargetProxy->instantiate(fContext->textureProvider())));
Robert Phillipse60ad622016-11-17 10:22:48 -0500151 if (!rt) {
152 return false;
153 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400154
Robert Phillipse2f7d182016-12-15 09:23:05 -0500155 return this->getOpList()->copySurface(rt.get(), src.get(), srcRect, dstPoint);
robertphillipsea461502015-05-26 11:38:03 -0700156}
157
Robert Phillips2c862492017-01-18 10:08:39 -0500158// TODO: move this (and GrTextureContext::onReadPixels) to GrSurfaceContext?
159bool GrRenderTargetContext::onReadPixels(const SkImageInfo& dstInfo, void* dstBuffer,
160 size_t dstRowBytes, int x, int y) {
161 // TODO: teach GrRenderTarget to take ImageInfo directly to specify the src pixels
162 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo, *fContext->caps());
163 if (kUnknown_GrPixelConfig == config) {
164 return false;
165 }
166
167 uint32_t flags = 0;
168 if (kUnpremul_SkAlphaType == dstInfo.alphaType()) {
169 flags = GrContext::kUnpremul_PixelOpsFlag;
170 }
171
172 // Deferral of the VRAM resources must end in this instance anyway
173 sk_sp<GrRenderTarget> rt(
174 sk_ref_sp(fRenderTargetProxy->instantiate(fContext->textureProvider())));
175 if (!rt) {
176 return false;
177 }
178
179 return rt->readPixels(this->getColorSpace(), x, y, dstInfo.width(), dstInfo.height(),
180 config, dstInfo.colorSpace(), dstBuffer, dstRowBytes, flags);
181}
182
183// TODO: move this (and GrTextureContext::onReadPixels) to GrSurfaceContext?
184bool GrRenderTargetContext::onWritePixels(const SkImageInfo& srcInfo, const void* srcBuffer,
185 size_t srcRowBytes, int x, int y) {
186 // TODO: teach GrRenderTarget to take ImageInfo directly to specify the src pixels
187 GrPixelConfig config = SkImageInfo2GrPixelConfig(srcInfo, *fContext->caps());
188 if (kUnknown_GrPixelConfig == config) {
189 return false;
190 }
191 uint32_t flags = 0;
192 if (kUnpremul_SkAlphaType == srcInfo.alphaType()) {
193 flags = GrContext::kUnpremul_PixelOpsFlag;
194 }
195
196 // Deferral of the VRAM resources must end in this instance anyway
197 sk_sp<GrRenderTarget> rt(
198 sk_ref_sp(fRenderTargetProxy->instantiate(fContext->textureProvider())));
199 if (!rt) {
200 return false;
201 }
202
203 return rt->writePixels(this->getColorSpace(), x, y, srcInfo.width(), srcInfo.height(),
204 config, srcInfo.colorSpace(), srcBuffer, srcRowBytes, flags);
205}
206
207
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500208void GrRenderTargetContext::drawText(const GrClip& clip, const SkPaint& skPaint,
Brian Salomon82f44312017-01-11 13:42:54 -0500209 const SkMatrix& viewMatrix, const char text[],
210 size_t byteLength, SkScalar x, SkScalar y,
211 const SkIRect& clipBounds) {
joshualitt1de610a2016-01-06 08:26:09 -0800212 ASSERT_SINGLE_OWNER
robertphillips2d70dcb2015-10-06 07:38:23 -0700213 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700214 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400215 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawText");
robertphillips2d70dcb2015-10-06 07:38:23 -0700216
Robert Phillips72152832017-01-25 17:31:35 -0500217 GrAtlasTextContext* atlasTextContext = this->drawingManager()->getAtlasTextContext();
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500218 atlasTextContext->drawText(fContext, this, clip, skPaint, viewMatrix, fSurfaceProps, text,
219 byteLength, x, y, clipBounds);
robertphillips2334fb62015-06-17 05:43:33 -0700220}
robertphillipscaef3452015-11-11 13:18:11 -0800221
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500222void GrRenderTargetContext::drawPosText(const GrClip& clip, const SkPaint& paint,
223 const SkMatrix& viewMatrix, const char text[],
224 size_t byteLength, const SkScalar pos[],
Brian Salomon82f44312017-01-11 13:42:54 -0500225 int scalarsPerPosition, const SkPoint& offset,
226 const SkIRect& clipBounds) {
joshualitt1de610a2016-01-06 08:26:09 -0800227 ASSERT_SINGLE_OWNER
robertphillips2d70dcb2015-10-06 07:38:23 -0700228 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700229 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400230 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawPosText");
robertphillips2d70dcb2015-10-06 07:38:23 -0700231
Robert Phillips72152832017-01-25 17:31:35 -0500232 GrAtlasTextContext* atlasTextContext = this->drawingManager()->getAtlasTextContext();
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500233 atlasTextContext->drawPosText(fContext, this, clip, paint, viewMatrix, fSurfaceProps, text,
234 byteLength, pos, scalarsPerPosition, offset, clipBounds);
robertphillips2334fb62015-06-17 05:43:33 -0700235}
robertphillipscaef3452015-11-11 13:18:11 -0800236
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500237void GrRenderTargetContext::drawTextBlob(const GrClip& clip, const SkPaint& paint,
Brian Osman11052242016-10-27 14:47:55 -0400238 const SkMatrix& viewMatrix, const SkTextBlob* blob,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500239 SkScalar x, SkScalar y, SkDrawFilter* filter,
240 const SkIRect& clipBounds) {
joshualitt1de610a2016-01-06 08:26:09 -0800241 ASSERT_SINGLE_OWNER
robertphillips2d70dcb2015-10-06 07:38:23 -0700242 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700243 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400244 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawTextBlob");
robertphillips2d70dcb2015-10-06 07:38:23 -0700245
Robert Phillips72152832017-01-25 17:31:35 -0500246 GrAtlasTextContext* atlasTextContext = this->drawingManager()->getAtlasTextContext();
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500247 atlasTextContext->drawTextBlob(fContext, this, clip, paint, viewMatrix, fSurfaceProps, blob, x,
248 y, filter, clipBounds);
robertphillipsea461502015-05-26 11:38:03 -0700249}
250
Brian Osman11052242016-10-27 14:47:55 -0400251void GrRenderTargetContext::discard() {
joshualitt1de610a2016-01-06 08:26:09 -0800252 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700253 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700254 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400255 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::discard");
robertphillips2e1e51f2015-10-15 08:01:48 -0700256
Robert Phillips72152832017-01-25 17:31:35 -0500257 AutoCheckFlush acf(this->drawingManager());
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400258
Brian Salomon69868af2016-12-22 15:42:51 -0500259 // TODO: This needs to be fixed up since it ends the deferral of the GrRenderTarget.
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400260 sk_sp<GrRenderTarget> rt(
261 sk_ref_sp(fRenderTargetProxy->instantiate(fContext->textureProvider())));
Robert Phillipse60ad622016-11-17 10:22:48 -0500262 if (!rt) {
263 return;
264 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400265
Brian Salomon69868af2016-12-22 15:42:51 -0500266 this->getOpList()->discard(this);
robertphillipsea461502015-05-26 11:38:03 -0700267}
268
Brian Osman11052242016-10-27 14:47:55 -0400269void GrRenderTargetContext::clear(const SkIRect* rect,
270 const GrColor color,
271 bool canIgnoreRect) {
joshualitt1de610a2016-01-06 08:26:09 -0800272 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700273 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700274 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400275 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::clear");
robertphillipsea461502015-05-26 11:38:03 -0700276
Robert Phillips72152832017-01-25 17:31:35 -0500277 AutoCheckFlush acf(this->drawingManager());
csmartdalton29df7602016-08-31 11:55:52 -0700278 this->internalClear(rect ? GrFixedClip(*rect) : GrFixedClip::Disabled(), color, canIgnoreRect);
279}
robertphillips9199a9f2016-07-13 07:48:43 -0700280
Robert Phillips784b7bf2016-12-09 13:35:02 -0500281void GrRenderTargetContextPriv::absClear(const SkIRect* clearRect, const GrColor color) {
282 ASSERT_SINGLE_OWNER_PRIV
283 RETURN_IF_ABANDONED_PRIV
284 SkDEBUGCODE(fRenderTargetContext->validate();)
285 GR_AUDIT_TRAIL_AUTO_FRAME(fRenderTargetContext->fAuditTrail,
286 "GrRenderTargetContext::absClear");
287
Robert Phillips72152832017-01-25 17:31:35 -0500288 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500289
290 SkIRect rtRect = SkIRect::MakeWH(fRenderTargetContext->fRenderTargetProxy->worstCaseWidth(
291 *fRenderTargetContext->caps()),
292 fRenderTargetContext->fRenderTargetProxy->worstCaseHeight(
293 *fRenderTargetContext->caps()));
294
295 if (clearRect) {
296 if (clearRect->contains(rtRect)) {
297 clearRect = nullptr; // full screen
298 } else {
299 if (!rtRect.intersect(*clearRect)) {
300 return;
301 }
302 }
303 }
304
305 // TODO: in a post-MDB world this should be handled at the OpList level.
306 // An op-list that is initially cleared and has no other ops should receive an
307 // extra draw.
308 if (fRenderTargetContext->fContext->caps()->useDrawInsteadOfClear()) {
309 // This works around a driver bug with clear by drawing a rect instead.
310 // The driver will ignore a clear if it is the only thing rendered to a
311 // target before the target is read.
312 GrPaint paint;
313 paint.setColor4f(GrColor4f::FromGrColor(color));
Brian Salomona1633922017-01-09 11:46:10 -0500314 paint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
Robert Phillips784b7bf2016-12-09 13:35:02 -0500315
316 // We don't call drawRect() here to avoid the cropping to the, possibly smaller,
317 // RenderTargetProxy bounds
Brian Salomon82f44312017-01-11 13:42:54 -0500318 fRenderTargetContext->drawNonAAFilledRect(GrNoClip(), std::move(paint), SkMatrix::I(),
319 SkRect::Make(rtRect), nullptr, nullptr, nullptr,
320 GrAAType::kNone);
Robert Phillips784b7bf2016-12-09 13:35:02 -0500321
322 } else {
323 if (!fRenderTargetContext->accessRenderTarget()) {
324 return;
325 }
326
327 // This path doesn't handle coalescing of full screen clears b.c. it
328 // has to clear the entire render target - not just the content area.
329 // It could be done but will take more finagling.
Brian Salomonf8334782017-01-03 09:42:58 -0500330 std::unique_ptr<GrOp> op(GrClearOp::Make(
331 rtRect, color, fRenderTargetContext->accessRenderTarget(), !clearRect));
Brian Salomonfc527d22016-12-14 21:07:01 -0500332 if (!op) {
Robert Phillips784b7bf2016-12-09 13:35:02 -0500333 return;
334 }
Brian Salomon69868af2016-12-22 15:42:51 -0500335 fRenderTargetContext->getOpList()->addOp(std::move(op), fRenderTargetContext);
Robert Phillips784b7bf2016-12-09 13:35:02 -0500336 }
337}
338
Brian Osman11052242016-10-27 14:47:55 -0400339void GrRenderTargetContextPriv::clear(const GrFixedClip& clip,
340 const GrColor color,
341 bool canIgnoreClip) {
csmartdalton29df7602016-08-31 11:55:52 -0700342 ASSERT_SINGLE_OWNER_PRIV
343 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400344 SkDEBUGCODE(fRenderTargetContext->validate();)
345 GR_AUDIT_TRAIL_AUTO_FRAME(fRenderTargetContext->fAuditTrail,
346 "GrRenderTargetContextPriv::clear");
csmartdalton29df7602016-08-31 11:55:52 -0700347
Robert Phillips72152832017-01-25 17:31:35 -0500348 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Brian Osman11052242016-10-27 14:47:55 -0400349 fRenderTargetContext->internalClear(clip, color, canIgnoreClip);
csmartdalton29df7602016-08-31 11:55:52 -0700350}
351
Brian Osman11052242016-10-27 14:47:55 -0400352void GrRenderTargetContext::internalClear(const GrFixedClip& clip,
353 const GrColor color,
354 bool canIgnoreClip) {
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700355 bool isFull = false;
356 if (!clip.hasWindowRectangles()) {
357 isFull = !clip.scissorEnabled() ||
358 (canIgnoreClip && fContext->caps()->fullClearIsFree()) ||
359 clip.scissorRect().contains(SkIRect::MakeWH(this->width(), this->height()));
360 }
robertphillips9199a9f2016-07-13 07:48:43 -0700361
362 if (fContext->caps()->useDrawInsteadOfClear()) {
363 // This works around a driver bug with clear by drawing a rect instead.
364 // The driver will ignore a clear if it is the only thing rendered to a
365 // target before the target is read.
Robert Phillips784b7bf2016-12-09 13:35:02 -0500366 SkIRect clearRect = SkIRect::MakeWH(this->width(), this->height());
csmartdalton29df7602016-08-31 11:55:52 -0700367 if (isFull) {
robertphillips9199a9f2016-07-13 07:48:43 -0700368 this->discard();
Robert Phillips93f16332016-11-23 19:37:13 -0500369 } else if (!clearRect.intersect(clip.scissorRect())) {
csmartdalton29df7602016-08-31 11:55:52 -0700370 return;
robertphillips9199a9f2016-07-13 07:48:43 -0700371 }
372
373 GrPaint paint;
374 paint.setColor4f(GrColor4f::FromGrColor(color));
Brian Salomona1633922017-01-09 11:46:10 -0500375 paint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
robertphillips9199a9f2016-07-13 07:48:43 -0700376
Brian Salomon82f44312017-01-11 13:42:54 -0500377 this->drawRect(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), SkRect::Make(clearRect));
bsalomon9f129de2016-08-10 16:31:05 -0700378 } else if (isFull) {
Robert Phillipse60ad622016-11-17 10:22:48 -0500379 if (this->accessRenderTarget()) {
Brian Salomon69868af2016-12-22 15:42:51 -0500380 this->getOpList()->fullClear(this, color);
Robert Phillipse60ad622016-11-17 10:22:48 -0500381 }
robertphillips9199a9f2016-07-13 07:48:43 -0700382 } else {
Robert Phillipse60ad622016-11-17 10:22:48 -0500383 if (!this->accessRenderTarget()) {
384 return;
385 }
Brian Salomonf8334782017-01-03 09:42:58 -0500386 std::unique_ptr<GrOp> op(GrClearOp::Make(clip, color, this->accessRenderTarget()));
Brian Salomon42521e82016-12-07 16:44:58 -0500387 if (!op) {
csmartdalton29df7602016-08-31 11:55:52 -0700388 return;
389 }
Brian Salomon69868af2016-12-22 15:42:51 -0500390 this->getOpList()->addOp(std::move(op), this);
robertphillips9199a9f2016-07-13 07:48:43 -0700391 }
robertphillipsea461502015-05-26 11:38:03 -0700392}
393
Brian Osman11052242016-10-27 14:47:55 -0400394void GrRenderTargetContext::drawPaint(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500395 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400396 const SkMatrix& viewMatrix) {
joshualitt1de610a2016-01-06 08:26:09 -0800397 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700398 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700399 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400400 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawPaint");
robertphillips2e1e51f2015-10-15 08:01:48 -0700401
robertphillipsea461502015-05-26 11:38:03 -0700402 // set rect to be big enough to fill the space, but not super-huge, so we
403 // don't overflow fixed-point implementations
robertphillips13a7eee2016-08-31 15:06:24 -0700404
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400405 SkRect r = fRenderTargetProxy->getBoundsRect();
robertphillipsea461502015-05-26 11:38:03 -0700406
bsalomoncb31e512016-08-26 10:48:19 -0700407 SkRRect rrect;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500408 GrAA aa;
bsalomoncb31e512016-08-26 10:48:19 -0700409 // Check if we can replace a clipRRect()/drawPaint() with a drawRRect(). We only do the
410 // transformation for non-rect rrects. Rects caused a performance regression on an Android
411 // test that needs investigation. We also skip cases where there are fragment processors
412 // because they may depend on having correct local coords and this path draws in device space
413 // without a local matrix.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500414 if (!paint.numTotalFragmentProcessors() && clip.isRRect(r, &rrect, &aa) && !rrect.isRect()) {
Brian Salomon82f44312017-01-11 13:42:54 -0500415 this->drawRRect(GrNoClip(), std::move(paint), aa, SkMatrix::I(), rrect,
416 GrStyle::SimpleFill());
bsalomoncb31e512016-08-26 10:48:19 -0700417 return;
418 }
419
robertphillipsea461502015-05-26 11:38:03 -0700420
421 bool isPerspective = viewMatrix.hasPerspective();
422
423 // We attempt to map r by the inverse matrix and draw that. mapRect will
424 // map the four corners and bound them with a new rect. This will not
425 // produce a correct result for some perspective matrices.
426 if (!isPerspective) {
reeda39667c2016-08-22 06:39:49 -0700427 if (!SkMatrixPriv::InverseMapRect(viewMatrix, &r, r)) {
robertphillipsea461502015-05-26 11:38:03 -0700428 SkDebugf("Could not invert matrix\n");
429 return;
430 }
Brian Salomon82f44312017-01-11 13:42:54 -0500431 this->drawRect(clip, std::move(paint), GrAA::kNo, viewMatrix, r);
robertphillipsea461502015-05-26 11:38:03 -0700432 } else {
433 SkMatrix localMatrix;
434 if (!viewMatrix.invert(&localMatrix)) {
435 SkDebugf("Could not invert matrix\n");
436 return;
437 }
438
Robert Phillips72152832017-01-25 17:31:35 -0500439 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700440
Brian Salomon82f44312017-01-11 13:42:54 -0500441 this->drawNonAAFilledRect(clip, std::move(paint), SkMatrix::I(), r, nullptr, &localMatrix,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500442 nullptr, GrAAType::kNone);
robertphillipsea461502015-05-26 11:38:03 -0700443 }
444}
445
robertphillipsea461502015-05-26 11:38:03 -0700446static inline bool rect_contains_inclusive(const SkRect& rect, const SkPoint& point) {
447 return point.fX >= rect.fLeft && point.fX <= rect.fRight &&
448 point.fY >= rect.fTop && point.fY <= rect.fBottom;
449}
450
bsalomonc55271f2015-11-09 11:55:57 -0800451static bool view_matrix_ok_for_aa_fill_rect(const SkMatrix& viewMatrix) {
452 return viewMatrix.preservesRightAngles();
453}
454
csmartdalton97f6cd52016-07-13 13:37:08 -0700455// Attempts to crop a rect and optional local rect to the clip boundaries.
456// Returns false if the draw can be skipped entirely.
robertphillips13a7eee2016-08-31 15:06:24 -0700457static bool crop_filled_rect(int width, int height, const GrClip& clip,
csmartdalton97f6cd52016-07-13 13:37:08 -0700458 const SkMatrix& viewMatrix, SkRect* rect,
459 SkRect* localRect = nullptr) {
460 if (!viewMatrix.rectStaysRect()) {
461 return true;
462 }
463
csmartdalton97f6cd52016-07-13 13:37:08 -0700464 SkIRect clipDevBounds;
465 SkRect clipBounds;
csmartdalton97f6cd52016-07-13 13:37:08 -0700466
robertphillips13a7eee2016-08-31 15:06:24 -0700467 clip.getConservativeBounds(width, height, &clipDevBounds);
reeda39667c2016-08-22 06:39:49 -0700468 if (!SkMatrixPriv::InverseMapRect(viewMatrix, &clipBounds, SkRect::Make(clipDevBounds))) {
469 return false;
470 }
csmartdalton97f6cd52016-07-13 13:37:08 -0700471
472 if (localRect) {
473 if (!rect->intersects(clipBounds)) {
474 return false;
475 }
476 const SkScalar dx = localRect->width() / rect->width();
477 const SkScalar dy = localRect->height() / rect->height();
478 if (clipBounds.fLeft > rect->fLeft) {
479 localRect->fLeft += (clipBounds.fLeft - rect->fLeft) * dx;
480 rect->fLeft = clipBounds.fLeft;
481 }
482 if (clipBounds.fTop > rect->fTop) {
483 localRect->fTop += (clipBounds.fTop - rect->fTop) * dy;
484 rect->fTop = clipBounds.fTop;
485 }
486 if (clipBounds.fRight < rect->fRight) {
487 localRect->fRight -= (rect->fRight - clipBounds.fRight) * dx;
488 rect->fRight = clipBounds.fRight;
489 }
490 if (clipBounds.fBottom < rect->fBottom) {
491 localRect->fBottom -= (rect->fBottom - clipBounds.fBottom) * dy;
492 rect->fBottom = clipBounds.fBottom;
493 }
494 return true;
495 }
496
497 return rect->intersect(clipBounds);
498}
499
Brian Osman11052242016-10-27 14:47:55 -0400500bool GrRenderTargetContext::drawFilledRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500501 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500502 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400503 const SkMatrix& viewMatrix,
504 const SkRect& rect,
505 const GrUserStencilSettings* ss) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700506 SkRect croppedRect = rect;
Robert Phillips784b7bf2016-12-09 13:35:02 -0500507 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix, &croppedRect)) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700508 return true;
509 }
robertphillips44302392016-07-08 14:43:03 -0700510
Brian Salomonf8334782017-01-03 09:42:58 -0500511 std::unique_ptr<GrDrawOp> op;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500512 GrAAType aaType;
robertphillips44302392016-07-08 14:43:03 -0700513
csmartdaltone0d36292016-07-29 08:14:20 -0700514 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport()) {
Robert Phillipsf2361d22016-10-25 14:20:06 -0400515 InstancedRendering* ir = this->getOpList()->instancedRendering();
Brian Salomon99ad1642016-12-16 09:50:45 -0500516 op = ir->recordRect(croppedRect, viewMatrix, paint.getColor(), aa, fInstancedPipelineInfo,
517 &aaType);
Brian Salomon42521e82016-12-07 16:44:58 -0500518 if (op) {
Brian Salomon82f44312017-01-11 13:42:54 -0500519 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
robertphillips44302392016-07-08 14:43:03 -0700520 if (ss) {
521 pipelineBuilder.setUserStencil(ss);
522 }
Brian Salomon21aa35f2016-12-09 16:01:53 -0500523 this->getOpList()->addDrawOp(pipelineBuilder, this, clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700524 return true;
csmartdaltona7f29642016-07-07 08:49:11 -0700525 }
526 }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500527 aaType = this->decideAAType(aa);
528 if (GrAAType::kCoverage == aaType) {
robertphillips391395d2016-03-02 09:26:36 -0800529 // The fill path can handle rotation but not skew.
530 if (view_matrix_ok_for_aa_fill_rect(viewMatrix)) {
531 SkRect devBoundRect;
csmartdalton97f6cd52016-07-13 13:37:08 -0700532 viewMatrix.mapRect(&devBoundRect, croppedRect);
robertphillips44302392016-07-08 14:43:03 -0700533
Brian Salomon6a639042016-12-14 11:08:17 -0500534 op = GrRectOpFactory::MakeAAFill(paint, viewMatrix, rect, croppedRect, devBoundRect);
Brian Salomon42521e82016-12-07 16:44:58 -0500535 if (op) {
Brian Salomon82f44312017-01-11 13:42:54 -0500536 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
robertphillips44302392016-07-08 14:43:03 -0700537 if (ss) {
538 pipelineBuilder.setUserStencil(ss);
539 }
Brian Salomon21aa35f2016-12-09 16:01:53 -0500540 this->getOpList()->addDrawOp(pipelineBuilder, this, clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700541 return true;
542 }
robertphillips391395d2016-03-02 09:26:36 -0800543 }
544 } else {
Brian Salomon82f44312017-01-11 13:42:54 -0500545 this->drawNonAAFilledRect(clip, std::move(paint), viewMatrix, croppedRect, nullptr, nullptr,
546 ss, aaType);
robertphillips44302392016-07-08 14:43:03 -0700547 return true;
robertphillips391395d2016-03-02 09:26:36 -0800548 }
549
robertphillips44302392016-07-08 14:43:03 -0700550 return false;
robertphillips391395d2016-03-02 09:26:36 -0800551}
552
Brian Osman11052242016-10-27 14:47:55 -0400553void GrRenderTargetContext::drawRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500554 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500555 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400556 const SkMatrix& viewMatrix,
557 const SkRect& rect,
558 const GrStyle* style) {
bsalomon6663acf2016-05-10 09:14:17 -0700559 if (!style) {
560 style = &GrStyle::SimpleFill();
561 }
joshualitt1de610a2016-01-06 08:26:09 -0800562 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700563 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700564 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400565 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawRect");
robertphillips2e1e51f2015-10-15 08:01:48 -0700566
bsalomon6663acf2016-05-10 09:14:17 -0700567 // Path effects should've been devolved to a path in SkGpuDevice
568 SkASSERT(!style->pathEffect());
robertphillipsea461502015-05-26 11:38:03 -0700569
Robert Phillips72152832017-01-25 17:31:35 -0500570 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700571
bsalomon6663acf2016-05-10 09:14:17 -0700572 const SkStrokeRec& stroke = style->strokeRec();
robertphillips3ab14ca2016-07-10 11:49:39 -0700573 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
574
575 if (!fContext->caps()->useDrawInsteadOfClear()) {
576 // Check if this is a full RT draw and can be replaced with a clear. We don't bother
577 // checking cases where the RT is fully inside a stroke.
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400578 SkRect rtRect = fRenderTargetProxy->getBoundsRect();
robertphillips3ab14ca2016-07-10 11:49:39 -0700579 // Does the clip contain the entire RT?
580 if (clip.quickContains(rtRect)) {
581 SkMatrix invM;
582 if (!viewMatrix.invert(&invM)) {
robertphillipsea461502015-05-26 11:38:03 -0700583 return;
584 }
robertphillips3ab14ca2016-07-10 11:49:39 -0700585 // Does the rect bound the RT?
586 SkPoint srcSpaceRTQuad[4];
587 invM.mapRectToQuad(srcSpaceRTQuad, rtRect);
588 if (rect_contains_inclusive(rect, srcSpaceRTQuad[0]) &&
589 rect_contains_inclusive(rect, srcSpaceRTQuad[1]) &&
590 rect_contains_inclusive(rect, srcSpaceRTQuad[2]) &&
591 rect_contains_inclusive(rect, srcSpaceRTQuad[3])) {
592 // Will it blend?
593 GrColor clearColor;
594 if (paint.isConstantBlendedColor(&clearColor)) {
robertphillips9199a9f2016-07-13 07:48:43 -0700595 this->clear(nullptr, clearColor, true);
robertphillips3ab14ca2016-07-10 11:49:39 -0700596 return;
597 }
598 }
robertphillipsea461502015-05-26 11:38:03 -0700599 }
600 }
robertphillips44302392016-07-08 14:43:03 -0700601
Brian Salomon82f44312017-01-11 13:42:54 -0500602 if (this->drawFilledRect(clip, std::move(paint), aa, viewMatrix, rect, nullptr)) {
robertphillips44302392016-07-08 14:43:03 -0700603 return;
604 }
bsalomona7d85ba2016-07-06 11:54:59 -0700605 } else if (stroke.getStyle() == SkStrokeRec::kStroke_Style ||
606 stroke.getStyle() == SkStrokeRec::kHairline_Style) {
607 if ((!rect.width() || !rect.height()) &&
608 SkStrokeRec::kHairline_Style != stroke.getStyle()) {
609 SkScalar r = stroke.getWidth() / 2;
610 // TODO: Move these stroke->fill fallbacks to GrShape?
611 switch (stroke.getJoin()) {
612 case SkPaint::kMiter_Join:
Brian Salomon82f44312017-01-11 13:42:54 -0500613 this->drawRect(
614 clip, std::move(paint), aa, viewMatrix,
615 {rect.fLeft - r, rect.fTop - r, rect.fRight + r, rect.fBottom + r},
616 &GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700617 return;
618 case SkPaint::kRound_Join:
619 // Raster draws nothing when both dimensions are empty.
620 if (rect.width() || rect.height()){
621 SkRRect rrect = SkRRect::MakeRectXY(rect.makeOutset(r, r), r, r);
Brian Salomon82f44312017-01-11 13:42:54 -0500622 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect,
623 GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700624 return;
625 }
626 case SkPaint::kBevel_Join:
627 if (!rect.width()) {
Brian Salomon82f44312017-01-11 13:42:54 -0500628 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700629 {rect.fLeft - r, rect.fTop, rect.fRight + r, rect.fBottom},
630 &GrStyle::SimpleFill());
631 } else {
Brian Salomon82f44312017-01-11 13:42:54 -0500632 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700633 {rect.fLeft, rect.fTop - r, rect.fRight, rect.fBottom + r},
634 &GrStyle::SimpleFill());
635 }
636 return;
637 }
638 }
robertphillips44302392016-07-08 14:43:03 -0700639
robertphillips44302392016-07-08 14:43:03 -0700640 bool snapToPixelCenters = false;
Brian Salomonf8334782017-01-03 09:42:58 -0500641 std::unique_ptr<GrDrawOp> op;
robertphillips44302392016-07-08 14:43:03 -0700642
robertphillips391395d2016-03-02 09:26:36 -0800643 GrColor color = paint.getColor();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500644 GrAAType aaType = this->decideAAType(aa);
645 if (GrAAType::kCoverage == aaType) {
cdaltonbb539482016-01-04 09:48:25 -0800646 // The stroke path needs the rect to remain axis aligned (no rotation or skew).
647 if (viewMatrix.rectStaysRect()) {
Brian Salomon6a639042016-12-14 11:08:17 -0500648 op = GrRectOpFactory::MakeAAStroke(color, viewMatrix, rect, stroke);
cdaltonbb539482016-01-04 09:48:25 -0800649 }
robertphillipsea461502015-05-26 11:38:03 -0700650 } else {
robertphillips391395d2016-03-02 09:26:36 -0800651 // Depending on sub-pixel coordinates and the particular GPU, we may lose a corner of
halcanary9d524f22016-03-29 09:03:52 -0700652 // hairline rects. We jam all the vertices to pixel centers to avoid this, but not
robertphillips391395d2016-03-02 09:26:36 -0800653 // when MSAA is enabled because it can cause ugly artifacts.
bsalomona7d85ba2016-07-06 11:54:59 -0700654 snapToPixelCenters = stroke.getStyle() == SkStrokeRec::kHairline_Style &&
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400655 !fRenderTargetProxy->isUnifiedMultisampled();
Brian Salomon6a639042016-12-14 11:08:17 -0500656 op = GrRectOpFactory::MakeNonAAStroke(color, viewMatrix, rect, stroke,
657 snapToPixelCenters);
robertphillips391395d2016-03-02 09:26:36 -0800658 }
robertphillips4bc31812016-03-01 12:22:49 -0800659
Brian Salomon42521e82016-12-07 16:44:58 -0500660 if (op) {
Brian Salomon82f44312017-01-11 13:42:54 -0500661 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
Brian Salomon189098e72017-01-19 09:55:19 -0500662 pipelineBuilder.setSnapVerticesToPixelCenters(snapToPixelCenters);
Brian Salomon21aa35f2016-12-09 16:01:53 -0500663 this->getOpList()->addDrawOp(pipelineBuilder, this, clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700664 return;
robertphillips4bc31812016-03-01 12:22:49 -0800665 }
robertphillips4bc31812016-03-01 12:22:49 -0800666 }
halcanary9d524f22016-03-29 09:03:52 -0700667
robertphillips4bc31812016-03-01 12:22:49 -0800668 SkPath path;
669 path.setIsVolatile(true);
670 path.addRect(rect);
Brian Salomon82f44312017-01-11 13:42:54 -0500671 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, *style);
robertphillipsea461502015-05-26 11:38:03 -0700672}
673
Robert Phillipsec2249f2016-11-09 08:54:35 -0500674int GrRenderTargetContextPriv::maxWindowRectangles() const {
675 return fRenderTargetContext->fRenderTargetProxy->maxWindowRectangles(
676 *fRenderTargetContext->fContext->caps());
677}
678
Brian Osman11052242016-10-27 14:47:55 -0400679void GrRenderTargetContextPriv::clearStencilClip(const GrFixedClip& clip, bool insideStencilMask) {
robertphillips976f5f02016-06-03 10:59:20 -0700680 ASSERT_SINGLE_OWNER_PRIV
681 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400682 SkDEBUGCODE(fRenderTargetContext->validate();)
683 GR_AUDIT_TRAIL_AUTO_FRAME(fRenderTargetContext->fAuditTrail,
684 "GrRenderTargetContextPriv::clearStencilClip");
robertphillips976f5f02016-06-03 10:59:20 -0700685
Robert Phillips72152832017-01-25 17:31:35 -0500686 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Brian Salomon69868af2016-12-22 15:42:51 -0500687 // TODO: This needs to be fixed up since it ends the deferral of the GrRenderTarget.
Robert Phillipse60ad622016-11-17 10:22:48 -0500688 if (!fRenderTargetContext->accessRenderTarget()) {
689 return;
690 }
Brian Osman11052242016-10-27 14:47:55 -0400691 fRenderTargetContext->getOpList()->clearStencilClip(clip, insideStencilMask,
Brian Salomon69868af2016-12-22 15:42:51 -0500692 fRenderTargetContext);
robertphillips976f5f02016-06-03 10:59:20 -0700693}
694
Brian Osman11052242016-10-27 14:47:55 -0400695void GrRenderTargetContextPriv::stencilPath(const GrClip& clip,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500696 GrAAType aaType,
Brian Osman11052242016-10-27 14:47:55 -0400697 const SkMatrix& viewMatrix,
698 const GrPath* path) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500699 SkASSERT(aaType != GrAAType::kCoverage);
700 fRenderTargetContext->getOpList()->stencilPath(fRenderTargetContext, clip, aaType, viewMatrix,
Brian Osman11052242016-10-27 14:47:55 -0400701 path);
robertphillips976f5f02016-06-03 10:59:20 -0700702}
703
Brian Osman11052242016-10-27 14:47:55 -0400704void GrRenderTargetContextPriv::stencilRect(const GrClip& clip,
705 const GrUserStencilSettings* ss,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500706 GrAAType aaType,
Brian Osman11052242016-10-27 14:47:55 -0400707 const SkMatrix& viewMatrix,
708 const SkRect& rect) {
robertphillips976f5f02016-06-03 10:59:20 -0700709 ASSERT_SINGLE_OWNER_PRIV
710 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400711 SkDEBUGCODE(fRenderTargetContext->validate();)
712 GR_AUDIT_TRAIL_AUTO_FRAME(fRenderTargetContext->fAuditTrail,
713 "GrRenderTargetContext::stencilRect");
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500714 SkASSERT(GrAAType::kCoverage != aaType);
Robert Phillips72152832017-01-25 17:31:35 -0500715 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips976f5f02016-06-03 10:59:20 -0700716
717 GrPaint paint;
Brian Salomona1633922017-01-09 11:46:10 -0500718 paint.setXPFactory(GrDisableColorXPFactory::Get());
robertphillips976f5f02016-06-03 10:59:20 -0700719
Brian Salomon82f44312017-01-11 13:42:54 -0500720 fRenderTargetContext->drawNonAAFilledRect(clip, std::move(paint), viewMatrix, rect, nullptr,
721 nullptr, ss, aaType);
robertphillips976f5f02016-06-03 10:59:20 -0700722}
723
Brian Osman11052242016-10-27 14:47:55 -0400724bool GrRenderTargetContextPriv::drawAndStencilRect(const GrClip& clip,
725 const GrUserStencilSettings* ss,
726 SkRegion::Op op,
727 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500728 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400729 const SkMatrix& viewMatrix,
730 const SkRect& rect) {
robertphillips391395d2016-03-02 09:26:36 -0800731 ASSERT_SINGLE_OWNER_PRIV
732 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400733 SkDEBUGCODE(fRenderTargetContext->validate();)
734 GR_AUDIT_TRAIL_AUTO_FRAME(fRenderTargetContext->fAuditTrail,
735 "GrRenderTargetContext::drawAndStencilRect");
robertphillips391395d2016-03-02 09:26:36 -0800736
Robert Phillips72152832017-01-25 17:31:35 -0500737 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -0800738
739 GrPaint paint;
robertphillips391395d2016-03-02 09:26:36 -0800740 paint.setCoverageSetOpXPFactory(op, invert);
741
Brian Salomon82f44312017-01-11 13:42:54 -0500742 if (fRenderTargetContext->drawFilledRect(clip, std::move(paint), aa, viewMatrix, rect, ss)) {
robertphillips391395d2016-03-02 09:26:36 -0800743 return true;
744 }
robertphillips391395d2016-03-02 09:26:36 -0800745 SkPath path;
746 path.setIsVolatile(true);
747 path.addRect(rect);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500748 return this->drawAndStencilPath(clip, ss, op, invert, aa, viewMatrix, path);
robertphillips391395d2016-03-02 09:26:36 -0800749}
750
Brian Osman11052242016-10-27 14:47:55 -0400751void GrRenderTargetContext::fillRectToRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500752 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500753 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400754 const SkMatrix& viewMatrix,
755 const SkRect& rectToDraw,
756 const SkRect& localRect) {
joshualitt1de610a2016-01-06 08:26:09 -0800757 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700758 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700759 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400760 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::fillRectToRect");
robertphillipsea461502015-05-26 11:38:03 -0700761
csmartdalton97f6cd52016-07-13 13:37:08 -0700762 SkRect croppedRect = rectToDraw;
763 SkRect croppedLocalRect = localRect;
robertphillips13a7eee2016-08-31 15:06:24 -0700764 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix,
765 &croppedRect, &croppedLocalRect)) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700766 return;
767 }
768
Robert Phillips72152832017-01-25 17:31:35 -0500769 AutoCheckFlush acf(this->drawingManager());
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500770 GrAAType aaType;
csmartdaltona7f29642016-07-07 08:49:11 -0700771
csmartdaltone0d36292016-07-29 08:14:20 -0700772 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport()) {
Robert Phillipsf2361d22016-10-25 14:20:06 -0400773 InstancedRendering* ir = this->getOpList()->instancedRendering();
Brian Salomonf8334782017-01-03 09:42:58 -0500774 std::unique_ptr<GrDrawOp> op(ir->recordRect(croppedRect, viewMatrix, paint.getColor(),
775 croppedLocalRect, aa, fInstancedPipelineInfo,
776 &aaType));
Brian Salomon42521e82016-12-07 16:44:58 -0500777 if (op) {
Brian Salomon82f44312017-01-11 13:42:54 -0500778 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
Brian Salomon21aa35f2016-12-09 16:01:53 -0500779 this->getOpList()->addDrawOp(pipelineBuilder, this, clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -0700780 return;
781 }
782 }
783
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500784 aaType = this->decideAAType(aa);
785 if (GrAAType::kCoverage != aaType) {
Brian Salomon82f44312017-01-11 13:42:54 -0500786 this->drawNonAAFilledRect(clip, std::move(paint), viewMatrix, croppedRect,
787 &croppedLocalRect, nullptr, nullptr, aaType);
csmartdaltonfc49d562016-07-26 17:05:47 -0700788 return;
joshualitt04194f32016-01-13 10:08:27 -0800789 }
bsalomonbb243832016-07-22 07:10:19 -0700790
csmartdaltonfc49d562016-07-26 17:05:47 -0700791 if (view_matrix_ok_for_aa_fill_rect(viewMatrix)) {
Brian Salomonf8334782017-01-03 09:42:58 -0500792 std::unique_ptr<GrDrawOp> op = GrAAFillRectOp::MakeWithLocalRect(
793 paint.getColor(), viewMatrix, croppedRect, croppedLocalRect);
Brian Salomon82f44312017-01-11 13:42:54 -0500794 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
Brian Salomon24f19782016-12-13 15:10:11 -0500795 this->addDrawOp(pipelineBuilder, clip, std::move(op));
csmartdaltonfc49d562016-07-26 17:05:47 -0700796 return;
797 }
798
799 SkMatrix viewAndUnLocalMatrix;
800 if (!viewAndUnLocalMatrix.setRectToRect(localRect, rectToDraw, SkMatrix::kFill_ScaleToFit)) {
801 SkDebugf("fillRectToRect called with empty local matrix.\n");
802 return;
803 }
804 viewAndUnLocalMatrix.postConcat(viewMatrix);
805
806 SkPath path;
807 path.setIsVolatile(true);
808 path.addRect(localRect);
Brian Salomon82f44312017-01-11 13:42:54 -0500809 this->internalDrawPath(clip, std::move(paint), aa, viewAndUnLocalMatrix, path, GrStyle());
joshualittb6b513b2015-08-21 10:25:18 -0700810}
811
Brian Osman11052242016-10-27 14:47:55 -0400812void GrRenderTargetContext::fillRectWithLocalMatrix(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500813 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500814 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400815 const SkMatrix& viewMatrix,
816 const SkRect& rectToDraw,
817 const SkMatrix& localMatrix) {
joshualitt1de610a2016-01-06 08:26:09 -0800818 ASSERT_SINGLE_OWNER
joshualittb6b513b2015-08-21 10:25:18 -0700819 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700820 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400821 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::fillRectWithLocalMatrix");
joshualittb6b513b2015-08-21 10:25:18 -0700822
csmartdalton97f6cd52016-07-13 13:37:08 -0700823 SkRect croppedRect = rectToDraw;
robertphillips13a7eee2016-08-31 15:06:24 -0700824 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix, &croppedRect)) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700825 return;
826 }
827
Robert Phillips72152832017-01-25 17:31:35 -0500828 AutoCheckFlush acf(this->drawingManager());
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500829 GrAAType aaType;
csmartdaltona7f29642016-07-07 08:49:11 -0700830
csmartdaltone0d36292016-07-29 08:14:20 -0700831 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport()) {
Robert Phillipsf2361d22016-10-25 14:20:06 -0400832 InstancedRendering* ir = this->getOpList()->instancedRendering();
Brian Salomonf8334782017-01-03 09:42:58 -0500833 std::unique_ptr<GrDrawOp> op(ir->recordRect(croppedRect, viewMatrix, paint.getColor(),
834 localMatrix, aa, fInstancedPipelineInfo,
835 &aaType));
Brian Salomon42521e82016-12-07 16:44:58 -0500836 if (op) {
Brian Salomon82f44312017-01-11 13:42:54 -0500837 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
Brian Salomon21aa35f2016-12-09 16:01:53 -0500838 this->getOpList()->addDrawOp(pipelineBuilder, this, clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -0700839 return;
840 }
841 }
842
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500843 aaType = this->decideAAType(aa);
844 if (GrAAType::kCoverage != aaType) {
Brian Salomon82f44312017-01-11 13:42:54 -0500845 this->drawNonAAFilledRect(clip, std::move(paint), viewMatrix, croppedRect, nullptr,
846 &localMatrix, nullptr, aaType);
csmartdaltonfc49d562016-07-26 17:05:47 -0700847 return;
bsalomonc55271f2015-11-09 11:55:57 -0800848 }
robertphillips4bc31812016-03-01 12:22:49 -0800849
csmartdaltonfc49d562016-07-26 17:05:47 -0700850 if (view_matrix_ok_for_aa_fill_rect(viewMatrix)) {
Brian Salomonf8334782017-01-03 09:42:58 -0500851 std::unique_ptr<GrDrawOp> op =
Brian Salomon6a639042016-12-14 11:08:17 -0500852 GrAAFillRectOp::Make(paint.getColor(), viewMatrix, localMatrix, croppedRect);
Brian Salomon82f44312017-01-11 13:42:54 -0500853 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
Brian Salomon21aa35f2016-12-09 16:01:53 -0500854 this->getOpList()->addDrawOp(pipelineBuilder, this, clip, std::move(op));
csmartdaltonfc49d562016-07-26 17:05:47 -0700855 return;
856 }
857
858 SkMatrix viewAndUnLocalMatrix;
859 if (!localMatrix.invert(&viewAndUnLocalMatrix)) {
860 SkDebugf("fillRectWithLocalMatrix called with degenerate local matrix.\n");
861 return;
862 }
863 viewAndUnLocalMatrix.postConcat(viewMatrix);
864
865 SkPath path;
866 path.setIsVolatile(true);
867 path.addRect(rectToDraw);
868 path.transform(localMatrix);
Brian Salomon82f44312017-01-11 13:42:54 -0500869 this->internalDrawPath(clip, std::move(paint), aa, viewAndUnLocalMatrix, path, GrStyle());
robertphillipsea461502015-05-26 11:38:03 -0700870}
871
Brian Osman11052242016-10-27 14:47:55 -0400872void GrRenderTargetContext::drawVertices(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500873 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400874 const SkMatrix& viewMatrix,
875 GrPrimitiveType primitiveType,
876 int vertexCount,
877 const SkPoint positions[],
878 const SkPoint texCoords[],
879 const GrColor colors[],
880 const uint16_t indices[],
881 int indexCount) {
joshualitt1de610a2016-01-06 08:26:09 -0800882 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700883 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700884 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400885 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawVertices");
robertphillipsea461502015-05-26 11:38:03 -0700886
Robert Phillips72152832017-01-25 17:31:35 -0500887 AutoCheckFlush acf(this->drawingManager());
robertphillips2e1e51f2015-10-15 08:01:48 -0700888
robertphillipsea461502015-05-26 11:38:03 -0700889 // TODO clients should give us bounds
890 SkRect bounds;
891 if (!bounds.setBoundsCheck(positions, vertexCount)) {
892 SkDebugf("drawVertices call empty bounds\n");
893 return;
894 }
895
896 viewMatrix.mapRect(&bounds);
897
Brian Salomonf8334782017-01-03 09:42:58 -0500898 std::unique_ptr<GrDrawOp> op =
Brian Salomonfc527d22016-12-14 21:07:01 -0500899 GrDrawVerticesOp::Make(paint.getColor(), primitiveType, viewMatrix, positions,
900 vertexCount, indices, indexCount, colors, texCoords, bounds);
robertphillipsea461502015-05-26 11:38:03 -0700901
Brian Salomon82f44312017-01-11 13:42:54 -0500902 GrPipelineBuilder pipelineBuilder(std::move(paint), GrAAType::kNone);
Brian Salomon21aa35f2016-12-09 16:01:53 -0500903 this->getOpList()->addDrawOp(pipelineBuilder, this, clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -0700904}
905
906///////////////////////////////////////////////////////////////////////////////
907
Brian Osman11052242016-10-27 14:47:55 -0400908void GrRenderTargetContext::drawAtlas(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500909 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400910 const SkMatrix& viewMatrix,
911 int spriteCount,
912 const SkRSXform xform[],
913 const SkRect texRect[],
914 const SkColor colors[]) {
joshualitt1de610a2016-01-06 08:26:09 -0800915 ASSERT_SINGLE_OWNER
jvanverth31ff7622015-08-07 10:09:28 -0700916 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700917 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400918 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawAtlas");
robertphillips2e1e51f2015-10-15 08:01:48 -0700919
Robert Phillips72152832017-01-25 17:31:35 -0500920 AutoCheckFlush acf(this->drawingManager());
halcanary9d524f22016-03-29 09:03:52 -0700921
Brian Salomonf8334782017-01-03 09:42:58 -0500922 std::unique_ptr<GrDrawOp> op =
Brian Salomonfc527d22016-12-14 21:07:01 -0500923 GrDrawAtlasOp::Make(paint.getColor(), viewMatrix, spriteCount, xform, texRect, colors);
Brian Salomon82f44312017-01-11 13:42:54 -0500924 GrPipelineBuilder pipelineBuilder(std::move(paint), GrAAType::kNone);
Brian Salomon21aa35f2016-12-09 16:01:53 -0500925 this->getOpList()->addDrawOp(pipelineBuilder, this, clip, std::move(op));
jvanverth31ff7622015-08-07 10:09:28 -0700926}
927
928///////////////////////////////////////////////////////////////////////////////
929
Brian Osman11052242016-10-27 14:47:55 -0400930void GrRenderTargetContext::drawRRect(const GrClip& origClip,
Brian Salomon82f44312017-01-11 13:42:54 -0500931 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500932 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400933 const SkMatrix& viewMatrix,
934 const SkRRect& rrect,
935 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -0800936 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700937 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700938 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400939 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawRRect");
robertphillipsea461502015-05-26 11:38:03 -0700940 if (rrect.isEmpty()) {
941 return;
942 }
943
bsalomon7f0d9f32016-08-15 14:49:10 -0700944 GrNoClip noclip;
945 const GrClip* clip = &origClip;
946#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
947 // The Android framework frequently clips rrects to themselves where the clip is non-aa and the
Brian Salomon42521e82016-12-07 16:44:58 -0500948 // draw is aa. Since our lower level clip code works from op bounds, which are SkRects, it
bsalomon7f0d9f32016-08-15 14:49:10 -0700949 // doesn't detect that the clip can be ignored (modulo antialiasing). The following test
950 // attempts to mitigate the stencil clip cost but will only help when the entire clip stack
951 // can be ignored. We'd prefer to fix this in the framework by removing the clips calls.
952 SkRRect devRRect;
953 if (rrect.transform(viewMatrix, &devRRect) && clip->quickContains(devRRect)) {
954 clip = &noclip;
955 }
956#endif
bsalomon6663acf2016-05-10 09:14:17 -0700957 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
ksakamotoec7f2ac2016-07-05 03:54:53 -0700958
Robert Phillips72152832017-01-25 17:31:35 -0500959 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -0700960 const SkStrokeRec stroke = style.strokeRec();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500961 GrAAType aaType;
csmartdaltona7f29642016-07-07 08:49:11 -0700962
csmartdaltone0d36292016-07-29 08:14:20 -0700963 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport() &&
964 stroke.isFillStyle()) {
Robert Phillipsf2361d22016-10-25 14:20:06 -0400965 InstancedRendering* ir = this->getOpList()->instancedRendering();
Brian Salomonf8334782017-01-03 09:42:58 -0500966 std::unique_ptr<GrDrawOp> op(ir->recordRRect(rrect, viewMatrix, paint.getColor(), aa,
967 fInstancedPipelineInfo, &aaType));
Brian Salomon42521e82016-12-07 16:44:58 -0500968 if (op) {
Brian Salomon82f44312017-01-11 13:42:54 -0500969 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
Brian Salomon21aa35f2016-12-09 16:01:53 -0500970 this->getOpList()->addDrawOp(pipelineBuilder, this, *clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -0700971 return;
972 }
973 }
974
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500975 aaType = this->decideAAType(aa);
976 if (GrAAType::kCoverage == aaType) {
Brian Salomon94efbf52016-11-29 13:43:05 -0500977 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomonf8334782017-01-03 09:42:58 -0500978 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeRRectOp(paint.getColor(),
979 paint.usesDistanceVectorField(),
980 viewMatrix,
981 rrect,
982 stroke,
983 shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -0500984 if (op) {
Brian Salomon82f44312017-01-11 13:42:54 -0500985 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
Brian Salomon21aa35f2016-12-09 16:01:53 -0500986 this->getOpList()->addDrawOp(pipelineBuilder, this, *clip, std::move(op));
robertphillipsb56f9272016-02-25 11:03:52 -0800987 return;
988 }
robertphillipsea461502015-05-26 11:38:03 -0700989 }
robertphillipsb56f9272016-02-25 11:03:52 -0800990
991 SkPath path;
992 path.setIsVolatile(true);
993 path.addRRect(rrect);
Brian Salomon82f44312017-01-11 13:42:54 -0500994 this->internalDrawPath(*clip, std::move(paint), aa, viewMatrix, path, style);
robertphillipsea461502015-05-26 11:38:03 -0700995}
996
Jim Van Verthc5903412016-11-17 15:27:09 -0500997///////////////////////////////////////////////////////////////////////////////
998
999void GrRenderTargetContext::drawShadowRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001000 GrPaint&& paint,
Jim Van Verthc5903412016-11-17 15:27:09 -05001001 const SkMatrix& viewMatrix,
1002 const SkRRect& rrect,
1003 SkScalar blurRadius,
1004 const GrStyle& style) {
1005 ASSERT_SINGLE_OWNER
1006 RETURN_IF_ABANDONED
1007 SkDEBUGCODE(this->validate();)
1008 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawShadowRRect");
1009 if (rrect.isEmpty()) {
1010 return;
1011 }
1012
1013 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
1014
Robert Phillips72152832017-01-25 17:31:35 -05001015 AutoCheckFlush acf(this->drawingManager());
Jim Van Verthc5903412016-11-17 15:27:09 -05001016 const SkStrokeRec stroke = style.strokeRec();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001017 // TODO: add instancing support?
Jim Van Verthc5903412016-11-17 15:27:09 -05001018
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001019 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomonf8334782017-01-03 09:42:58 -05001020 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(paint.getColor(), viewMatrix, rrect,
1021 blurRadius, stroke, shaderCaps);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001022 if (op) {
Brian Salomon82f44312017-01-11 13:42:54 -05001023 GrPipelineBuilder pipelineBuilder(std::move(paint), GrAAType::kNone);
Brian Salomon21aa35f2016-12-09 16:01:53 -05001024 this->getOpList()->addDrawOp(pipelineBuilder, this, clip, std::move(op));
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001025 return;
Jim Van Verthc5903412016-11-17 15:27:09 -05001026 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001027}
1028
1029///////////////////////////////////////////////////////////////////////////////
1030
Brian Osman11052242016-10-27 14:47:55 -04001031bool GrRenderTargetContext::drawFilledDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001032 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001033 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001034 const SkMatrix& viewMatrix,
1035 const SkRRect& origOuter,
1036 const SkRRect& origInner) {
robertphillips00095892016-02-29 13:50:40 -08001037 SkASSERT(!origInner.isEmpty());
1038 SkASSERT(!origOuter.isEmpty());
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001039 GrAAType aaType;
robertphillips00095892016-02-29 13:50:40 -08001040
csmartdaltone0d36292016-07-29 08:14:20 -07001041 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport()) {
Robert Phillipsf2361d22016-10-25 14:20:06 -04001042 InstancedRendering* ir = this->getOpList()->instancedRendering();
Brian Salomonf8334782017-01-03 09:42:58 -05001043 std::unique_ptr<GrDrawOp> op(ir->recordDRRect(origOuter, origInner, viewMatrix,
Brian Salomon82f44312017-01-11 13:42:54 -05001044 paint.getColor(), aa, fInstancedPipelineInfo,
1045 &aaType));
Brian Salomon42521e82016-12-07 16:44:58 -05001046 if (op) {
Brian Salomon82f44312017-01-11 13:42:54 -05001047 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
Brian Salomon21aa35f2016-12-09 16:01:53 -05001048 this->getOpList()->addDrawOp(pipelineBuilder, this, clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -07001049 return true;
1050 }
1051 }
1052
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001053 aaType = this->decideAAType(aa);
robertphillips00095892016-02-29 13:50:40 -08001054
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001055 GrPrimitiveEdgeType innerEdgeType, outerEdgeType;
1056 if (GrAAType::kCoverage == aaType) {
1057 innerEdgeType = kInverseFillAA_GrProcessorEdgeType;
1058 outerEdgeType = kFillAA_GrProcessorEdgeType;
1059 } else {
1060 innerEdgeType = kInverseFillBW_GrProcessorEdgeType;
1061 outerEdgeType = kFillBW_GrProcessorEdgeType;
1062 }
robertphillips00095892016-02-29 13:50:40 -08001063
1064 SkTCopyOnFirstWrite<SkRRect> inner(origInner), outer(origOuter);
1065 SkMatrix inverseVM;
1066 if (!viewMatrix.isIdentity()) {
1067 if (!origInner.transform(viewMatrix, inner.writable())) {
1068 return false;
1069 }
1070 if (!origOuter.transform(viewMatrix, outer.writable())) {
1071 return false;
1072 }
1073 if (!viewMatrix.invert(&inverseVM)) {
1074 return false;
1075 }
1076 } else {
1077 inverseVM.reset();
halcanary9d524f22016-03-29 09:03:52 -07001078 }
robertphillips00095892016-02-29 13:50:40 -08001079
robertphillips00095892016-02-29 13:50:40 -08001080 // TODO these need to be a geometry processors
bungeman06ca8ec2016-06-09 08:01:03 -07001081 sk_sp<GrFragmentProcessor> innerEffect(GrRRectEffect::Make(innerEdgeType, *inner));
robertphillips00095892016-02-29 13:50:40 -08001082 if (!innerEffect) {
1083 return false;
1084 }
1085
bungeman06ca8ec2016-06-09 08:01:03 -07001086 sk_sp<GrFragmentProcessor> outerEffect(GrRRectEffect::Make(outerEdgeType, *outer));
robertphillips00095892016-02-29 13:50:40 -08001087 if (!outerEffect) {
1088 return false;
1089 }
1090
Brian Salomon82f44312017-01-11 13:42:54 -05001091 paint.addCoverageFragmentProcessor(std::move(innerEffect));
1092 paint.addCoverageFragmentProcessor(std::move(outerEffect));
robertphillips00095892016-02-29 13:50:40 -08001093
1094 SkRect bounds = outer->getBounds();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001095 if (GrAAType::kCoverage == aaType) {
robertphillips00095892016-02-29 13:50:40 -08001096 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1097 }
halcanary9d524f22016-03-29 09:03:52 -07001098
Brian Salomon82f44312017-01-11 13:42:54 -05001099 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), bounds,
1100 inverseVM);
robertphillips00095892016-02-29 13:50:40 -08001101 return true;
1102}
1103
Brian Osman11052242016-10-27 14:47:55 -04001104void GrRenderTargetContext::drawDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001105 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001106 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001107 const SkMatrix& viewMatrix,
1108 const SkRRect& outer,
1109 const SkRRect& inner) {
robertphillips00095892016-02-29 13:50:40 -08001110 ASSERT_SINGLE_OWNER
1111 RETURN_IF_ABANDONED
1112 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -04001113 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawDRRect");
robertphillips00095892016-02-29 13:50:40 -08001114
1115 SkASSERT(!outer.isEmpty());
1116 SkASSERT(!inner.isEmpty());
1117
Robert Phillips72152832017-01-25 17:31:35 -05001118 AutoCheckFlush acf(this->drawingManager());
robertphillips00095892016-02-29 13:50:40 -08001119
Brian Salomon82f44312017-01-11 13:42:54 -05001120 if (this->drawFilledDRRect(clip, std::move(paint), aa, viewMatrix, outer, inner)) {
robertphillips00095892016-02-29 13:50:40 -08001121 return;
1122 }
1123
1124 SkPath path;
1125 path.setIsVolatile(true);
1126 path.addRRect(inner);
1127 path.addRRect(outer);
1128 path.setFillType(SkPath::kEvenOdd_FillType);
1129
Brian Salomon82f44312017-01-11 13:42:54 -05001130 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, GrStyle::SimpleFill());
robertphillips00095892016-02-29 13:50:40 -08001131}
1132
robertphillipsea461502015-05-26 11:38:03 -07001133///////////////////////////////////////////////////////////////////////////////
1134
msarettcc319b92016-08-25 18:07:18 -07001135static inline bool is_int(float x) {
1136 return x == (float) sk_float_round2int(x);
1137}
1138
Brian Osman11052242016-10-27 14:47:55 -04001139void GrRenderTargetContext::drawRegion(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001140 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001141 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001142 const SkMatrix& viewMatrix,
1143 const SkRegion& region,
1144 const GrStyle& style) {
msarettcc319b92016-08-25 18:07:18 -07001145 ASSERT_SINGLE_OWNER
1146 RETURN_IF_ABANDONED
1147 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -04001148 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawRegion");
msarettcc319b92016-08-25 18:07:18 -07001149
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001150 if (GrAA::kYes == aa) {
Brian Salomonfc527d22016-12-14 21:07:01 -05001151 // GrRegionOp performs no antialiasing but is much faster, so here we check the matrix
Brian Salomonc57c7c92016-12-06 14:47:34 -05001152 // to see whether aa is really required.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001153 if (!SkToBool(viewMatrix.getType() & ~(SkMatrix::kTranslate_Mask)) &&
1154 is_int(viewMatrix.getTranslateX()) &&
1155 is_int(viewMatrix.getTranslateY())) {
1156 aa = GrAA::kNo;
1157 }
Brian Salomonc57c7c92016-12-06 14:47:34 -05001158 }
msarettcc319b92016-08-25 18:07:18 -07001159 bool complexStyle = !style.isSimpleFill();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001160 if (complexStyle || GrAA::kYes == aa) {
msarettcc319b92016-08-25 18:07:18 -07001161 SkPath path;
1162 region.getBoundaryPath(&path);
Brian Salomon82f44312017-01-11 13:42:54 -05001163 return this->drawPath(clip, std::move(paint), aa, viewMatrix, path, style);
msarettcc319b92016-08-25 18:07:18 -07001164 }
1165
Brian Salomonf8334782017-01-03 09:42:58 -05001166 std::unique_ptr<GrDrawOp> op = GrRegionOp::Make(paint.getColor(), viewMatrix, region);
Brian Salomon82f44312017-01-11 13:42:54 -05001167 GrPipelineBuilder pipelineBuilder(std::move(paint), GrAAType::kNone);
Brian Salomon21aa35f2016-12-09 16:01:53 -05001168 this->getOpList()->addDrawOp(pipelineBuilder, this, clip, std::move(op));
msarettcc319b92016-08-25 18:07:18 -07001169}
1170
Brian Osman11052242016-10-27 14:47:55 -04001171void GrRenderTargetContext::drawOval(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001172 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001173 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001174 const SkMatrix& viewMatrix,
1175 const SkRect& oval,
1176 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001177 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001178 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001179 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -04001180 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawOval");
robertphillips2e1e51f2015-10-15 08:01:48 -07001181
robertphillipsea461502015-05-26 11:38:03 -07001182 if (oval.isEmpty()) {
1183 return;
1184 }
1185
bsalomon6663acf2016-05-10 09:14:17 -07001186 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
robertphillipsea461502015-05-26 11:38:03 -07001187
Robert Phillips72152832017-01-25 17:31:35 -05001188 AutoCheckFlush acf(this->drawingManager());
bsalomon6663acf2016-05-10 09:14:17 -07001189 const SkStrokeRec& stroke = style.strokeRec();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001190 GrAAType aaType;
csmartdaltona7f29642016-07-07 08:49:11 -07001191
csmartdaltone0d36292016-07-29 08:14:20 -07001192 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport() &&
1193 stroke.isFillStyle()) {
Robert Phillipsf2361d22016-10-25 14:20:06 -04001194 InstancedRendering* ir = this->getOpList()->instancedRendering();
Brian Salomonf8334782017-01-03 09:42:58 -05001195 std::unique_ptr<GrDrawOp> op(ir->recordOval(oval, viewMatrix, paint.getColor(), aa,
1196 fInstancedPipelineInfo, &aaType));
Brian Salomon42521e82016-12-07 16:44:58 -05001197 if (op) {
Brian Salomon82f44312017-01-11 13:42:54 -05001198 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
Brian Salomon21aa35f2016-12-09 16:01:53 -05001199 this->getOpList()->addDrawOp(pipelineBuilder, this, clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -07001200 return;
1201 }
1202 }
1203
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001204 aaType = this->decideAAType(aa);
1205 if (GrAAType::kCoverage == aaType) {
Brian Salomon94efbf52016-11-29 13:43:05 -05001206 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomonf8334782017-01-03 09:42:58 -05001207 std::unique_ptr<GrDrawOp> op =
Brian Salomon289e3d82016-12-14 15:52:56 -05001208 GrOvalOpFactory::MakeOvalOp(paint.getColor(), viewMatrix, oval, stroke, shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -05001209 if (op) {
Brian Salomon82f44312017-01-11 13:42:54 -05001210 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
Brian Salomon21aa35f2016-12-09 16:01:53 -05001211 this->getOpList()->addDrawOp(pipelineBuilder, this, clip, std::move(op));
robertphillipsb56f9272016-02-25 11:03:52 -08001212 return;
1213 }
robertphillipsea461502015-05-26 11:38:03 -07001214 }
robertphillipsb56f9272016-02-25 11:03:52 -08001215
1216 SkPath path;
1217 path.setIsVolatile(true);
1218 path.addOval(oval);
Brian Salomon82f44312017-01-11 13:42:54 -05001219 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, style);
robertphillipsea461502015-05-26 11:38:03 -07001220}
1221
Brian Osman11052242016-10-27 14:47:55 -04001222void GrRenderTargetContext::drawArc(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001223 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001224 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001225 const SkMatrix& viewMatrix,
1226 const SkRect& oval,
1227 SkScalar startAngle,
1228 SkScalar sweepAngle,
1229 bool useCenter,
1230 const GrStyle& style) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001231 GrAAType aaType = this->decideAAType(aa);
1232 if (GrAAType::kCoverage == aaType) {
Brian Salomon94efbf52016-11-29 13:43:05 -05001233 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomonf8334782017-01-03 09:42:58 -05001234 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeArcOp(paint.getColor(),
1235 viewMatrix,
1236 oval,
1237 startAngle,
1238 sweepAngle,
1239 useCenter,
1240 style,
1241 shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -05001242 if (op) {
Brian Salomon82f44312017-01-11 13:42:54 -05001243 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
Brian Salomon21aa35f2016-12-09 16:01:53 -05001244 this->getOpList()->addDrawOp(pipelineBuilder, this, clip, std::move(op));
bsalomon4f3a0ca2016-08-22 13:14:26 -07001245 return;
1246 }
1247 }
1248 SkPath path;
bsalomon21af9ca2016-08-25 12:29:23 -07001249 SkPathPriv::CreateDrawArcPath(&path, oval, startAngle, sweepAngle, useCenter,
1250 style.isSimpleFill());
Brian Salomon82f44312017-01-11 13:42:54 -05001251 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, style);
bsalomon4f3a0ca2016-08-22 13:14:26 -07001252}
1253
Brian Osman11052242016-10-27 14:47:55 -04001254void GrRenderTargetContext::drawImageLattice(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001255 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001256 const SkMatrix& viewMatrix,
1257 int imageWidth,
1258 int imageHeight,
1259 std::unique_ptr<SkLatticeIter> iter,
1260 const SkRect& dst) {
joshualitt1de610a2016-01-06 08:26:09 -08001261 ASSERT_SINGLE_OWNER
joshualitt33a5fce2015-11-18 13:28:51 -08001262 RETURN_IF_ABANDONED
1263 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -04001264 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawImageLattice");
joshualitt33a5fce2015-11-18 13:28:51 -08001265
Robert Phillips72152832017-01-25 17:31:35 -05001266 AutoCheckFlush acf(this->drawingManager());
joshualitt33a5fce2015-11-18 13:28:51 -08001267
Brian Salomonf8334782017-01-03 09:42:58 -05001268 std::unique_ptr<GrDrawOp> op = GrLatticeOp::MakeNonAA(paint.getColor(), viewMatrix, imageWidth,
1269 imageHeight, std::move(iter), dst);
joshualitt33a5fce2015-11-18 13:28:51 -08001270
Brian Salomon82f44312017-01-11 13:42:54 -05001271 GrPipelineBuilder pipelineBuilder(std::move(paint), GrAAType::kNone);
Brian Salomon21aa35f2016-12-09 16:01:53 -05001272 this->getOpList()->addDrawOp(pipelineBuilder, this, clip, std::move(op));
joshualitt33a5fce2015-11-18 13:28:51 -08001273}
1274
Brian Osman11052242016-10-27 14:47:55 -04001275void GrRenderTargetContext::prepareForExternalIO() {
robertphillips8c523e02016-07-26 07:41:00 -07001276 ASSERT_SINGLE_OWNER
1277 RETURN_IF_ABANDONED
1278 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -04001279 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::prepareForExternalIO");
robertphillips8c523e02016-07-26 07:41:00 -07001280
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001281 // Deferral of the VRAM resources must end in this instance anyway
1282 sk_sp<GrRenderTarget> rt(
1283 sk_ref_sp(fRenderTargetProxy->instantiate(fContext->textureProvider())));
Robert Phillipse60ad622016-11-17 10:22:48 -05001284 if (!rt) {
1285 return;
1286 }
robertphillips8c523e02016-07-26 07:41:00 -07001287
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001288 ASSERT_OWNED_RESOURCE(rt);
1289
Robert Phillips72152832017-01-25 17:31:35 -05001290 this->drawingManager()->prepareSurfaceForExternalIO(rt.get());
robertphillips8c523e02016-07-26 07:41:00 -07001291}
joshualitt33a5fce2015-11-18 13:28:51 -08001292
Brian Osman11052242016-10-27 14:47:55 -04001293void GrRenderTargetContext::drawNonAAFilledRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001294 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001295 const SkMatrix& viewMatrix,
1296 const SkRect& rect,
1297 const SkRect* localRect,
1298 const SkMatrix* localMatrix,
1299 const GrUserStencilSettings* ss,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001300 GrAAType hwOrNoneAAType) {
1301 SkASSERT(GrAAType::kCoverage != hwOrNoneAAType);
1302 SkASSERT(hwOrNoneAAType == GrAAType::kNone || this->isStencilBufferMultisampled());
Brian Salomonf8334782017-01-03 09:42:58 -05001303 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeNonAAFill(paint.getColor(), viewMatrix,
1304 rect, localRect, localMatrix);
Brian Salomon82f44312017-01-11 13:42:54 -05001305 GrPipelineBuilder pipelineBuilder(std::move(paint), hwOrNoneAAType);
robertphillips44302392016-07-08 14:43:03 -07001306 if (ss) {
1307 pipelineBuilder.setUserStencil(ss);
1308 }
Brian Salomon21aa35f2016-12-09 16:01:53 -05001309 this->getOpList()->addDrawOp(pipelineBuilder, this, clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -07001310}
1311
robertphillipsea461502015-05-26 11:38:03 -07001312// Can 'path' be drawn as a pair of filled nested rectangles?
bsalomon6663acf2016-05-10 09:14:17 -07001313static bool fills_as_nested_rects(const SkMatrix& viewMatrix, const SkPath& path, SkRect rects[2]) {
robertphillipsea461502015-05-26 11:38:03 -07001314
1315 if (path.isInverseFillType()) {
1316 return false;
1317 }
1318
1319 // TODO: this restriction could be lifted if we were willing to apply
1320 // the matrix to all the points individually rather than just to the rect
robertphillips0e7029e2015-11-30 05:45:06 -08001321 if (!viewMatrix.rectStaysRect()) {
robertphillipsea461502015-05-26 11:38:03 -07001322 return false;
1323 }
1324
1325 SkPath::Direction dirs[2];
1326 if (!path.isNestedFillRects(rects, dirs)) {
1327 return false;
1328 }
1329
1330 if (SkPath::kWinding_FillType == path.getFillType() && dirs[0] == dirs[1]) {
1331 // The two rects need to be wound opposite to each other
1332 return false;
1333 }
1334
1335 // Right now, nested rects where the margin is not the same width
1336 // all around do not render correctly
1337 const SkScalar* outer = rects[0].asScalars();
1338 const SkScalar* inner = rects[1].asScalars();
1339
1340 bool allEq = true;
1341
1342 SkScalar margin = SkScalarAbs(outer[0] - inner[0]);
1343 bool allGoE1 = margin >= SK_Scalar1;
1344
1345 for (int i = 1; i < 4; ++i) {
1346 SkScalar temp = SkScalarAbs(outer[i] - inner[i]);
1347 if (temp < SK_Scalar1) {
1348 allGoE1 = false;
1349 }
1350 if (!SkScalarNearlyEqual(margin, temp)) {
1351 allEq = false;
1352 }
1353 }
1354
1355 return allEq || allGoE1;
1356}
1357
Brian Osman11052242016-10-27 14:47:55 -04001358void GrRenderTargetContext::drawPath(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001359 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001360 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001361 const SkMatrix& viewMatrix,
1362 const SkPath& path,
1363 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001364 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001365 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001366 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -04001367 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawPath");
robertphillips2e1e51f2015-10-15 08:01:48 -07001368
robertphillipsea461502015-05-26 11:38:03 -07001369 if (path.isEmpty()) {
1370 if (path.isInverseFillType()) {
Brian Salomon82f44312017-01-11 13:42:54 -05001371 this->drawPaint(clip, std::move(paint), viewMatrix);
robertphillipsea461502015-05-26 11:38:03 -07001372 }
1373 return;
1374 }
1375
Robert Phillips72152832017-01-25 17:31:35 -05001376 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -07001377
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001378 GrAAType aaType = this->decideAAType(aa);
1379 if (GrAAType::kCoverage == aaType && !style.pathEffect()) {
bsalomon6663acf2016-05-10 09:14:17 -07001380 if (style.isSimpleFill() && !path.isConvex()) {
robertphillipsea461502015-05-26 11:38:03 -07001381 // Concave AA paths are expensive - try to avoid them for special cases
1382 SkRect rects[2];
1383
bsalomon6663acf2016-05-10 09:14:17 -07001384 if (fills_as_nested_rects(viewMatrix, path, rects)) {
Brian Salomonf8334782017-01-03 09:42:58 -05001385 std::unique_ptr<GrDrawOp> op =
Brian Salomon6a639042016-12-14 11:08:17 -05001386 GrRectOpFactory::MakeAAFillNestedRects(paint.getColor(), viewMatrix, rects);
Brian Salomon21aa35f2016-12-09 16:01:53 -05001387 if (op) {
Brian Salomon82f44312017-01-11 13:42:54 -05001388 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
Brian Salomon21aa35f2016-12-09 16:01:53 -05001389 this->getOpList()->addDrawOp(pipelineBuilder, this, clip, std::move(op));
bsalomon40ef4852016-05-02 13:22:13 -07001390 }
robertphillipsea461502015-05-26 11:38:03 -07001391 return;
1392 }
1393 }
1394 SkRect ovalRect;
1395 bool isOval = path.isOval(&ovalRect);
1396
1397 if (isOval && !path.isInverseFillType()) {
Brian Salomon94efbf52016-11-29 13:43:05 -05001398 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomonf8334782017-01-03 09:42:58 -05001399 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeOvalOp(
Brian Salomon289e3d82016-12-14 15:52:56 -05001400 paint.getColor(), viewMatrix, ovalRect, style.strokeRec(), shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -05001401 if (op) {
Brian Salomon82f44312017-01-11 13:42:54 -05001402 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
Brian Salomon21aa35f2016-12-09 16:01:53 -05001403 this->getOpList()->addDrawOp(pipelineBuilder, this, clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -07001404 return;
1405 }
1406 }
1407 }
robertphillips4bc31812016-03-01 12:22:49 -08001408
1409 // Note that internalDrawPath may sw-rasterize the path into a scratch texture.
1410 // Scratch textures can be recycled after they are returned to the texture
1411 // cache. This presents a potential hazard for buffered drawing. However,
1412 // the writePixels that uploads to the scratch will perform a flush so we're
1413 // OK.
Brian Salomon82f44312017-01-11 13:42:54 -05001414 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, style);
robertphillipsea461502015-05-26 11:38:03 -07001415}
1416
Brian Osman11052242016-10-27 14:47:55 -04001417bool GrRenderTargetContextPriv::drawAndStencilPath(const GrClip& clip,
1418 const GrUserStencilSettings* ss,
1419 SkRegion::Op op,
1420 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001421 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001422 const SkMatrix& viewMatrix,
1423 const SkPath& path) {
robertphillips391395d2016-03-02 09:26:36 -08001424 ASSERT_SINGLE_OWNER_PRIV
1425 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -04001426 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001427 GR_AUDIT_TRAIL_AUTO_FRAME(fRenderTargetContext->fAuditTrail,
1428 "GrRenderTargetContextPriv::drawAndStencilPath");
robertphillips391395d2016-03-02 09:26:36 -08001429
1430 if (path.isEmpty() && path.isInverseFillType()) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001431 this->drawAndStencilRect(clip, ss, op, invert, GrAA::kNo, SkMatrix::I(),
Brian Osman11052242016-10-27 14:47:55 -04001432 SkRect::MakeIWH(fRenderTargetContext->width(),
1433 fRenderTargetContext->height()));
robertphillips391395d2016-03-02 09:26:36 -08001434 return true;
1435 }
1436
Robert Phillips72152832017-01-25 17:31:35 -05001437 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -08001438
1439 // An Assumption here is that path renderer would use some form of tweaking
1440 // the src color (either the input alpha or in the frag shader) to implement
1441 // aa. If we have some future driver-mojo path AA that can do the right
1442 // thing WRT to the blend then we'll need some query on the PR.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001443 GrAAType aaType = fRenderTargetContext->decideAAType(aa);
robertphillips976f5f02016-06-03 10:59:20 -07001444 bool hasUserStencilSettings = !ss->isUnused();
robertphillips391395d2016-03-02 09:26:36 -08001445
bsalomon8acedde2016-06-24 10:42:16 -07001446 GrShape shape(path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -08001447 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Osman11052242016-10-27 14:47:55 -04001448 canDrawArgs.fShaderCaps =
Robert Phillips72152832017-01-25 17:31:35 -05001449 fRenderTargetContext->drawingManager()->getContext()->caps()->shaderCaps();
robertphillips391395d2016-03-02 09:26:36 -08001450 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07001451 canDrawArgs.fShape = &shape;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001452 canDrawArgs.fAAType = aaType;
cdalton93a379b2016-05-11 13:58:08 -07001453 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips391395d2016-03-02 09:26:36 -08001454
1455 // Don't allow the SW renderer
Robert Phillips72152832017-01-25 17:31:35 -05001456 GrPathRenderer* pr = fRenderTargetContext->drawingManager()->getPathRenderer(
Brian Salomon36aa1762016-12-10 13:24:02 -05001457 canDrawArgs, false, GrPathRendererChain::DrawType::kStencilAndColor);
robertphillips391395d2016-03-02 09:26:36 -08001458 if (!pr) {
1459 return false;
1460 }
1461
1462 GrPaint paint;
1463 paint.setCoverageSetOpXPFactory(op, invert);
1464
Brian Salomon82f44312017-01-11 13:42:54 -05001465 GrPathRenderer::DrawPathArgs args{
Robert Phillips72152832017-01-25 17:31:35 -05001466 fRenderTargetContext->drawingManager()->getContext()->resourceProvider(),
Brian Salomon82f44312017-01-11 13:42:54 -05001467 std::move(paint),
1468 ss,
1469 fRenderTargetContext,
1470 &clip,
1471 &viewMatrix,
1472 &shape,
1473 aaType,
1474 fRenderTargetContext->isGammaCorrect()};
robertphillips391395d2016-03-02 09:26:36 -08001475 pr->drawPath(args);
1476 return true;
1477}
1478
Brian Osman11052242016-10-27 14:47:55 -04001479SkBudgeted GrRenderTargetContextPriv::isBudgeted() const {
robertphillips714712b2016-08-04 06:20:45 -07001480 ASSERT_SINGLE_OWNER_PRIV
1481
Brian Osman11052242016-10-27 14:47:55 -04001482 if (fRenderTargetContext->wasAbandoned()) {
robertphillips714712b2016-08-04 06:20:45 -07001483 return SkBudgeted::kNo;
1484 }
1485
Brian Osman11052242016-10-27 14:47:55 -04001486 SkDEBUGCODE(fRenderTargetContext->validate();)
robertphillips714712b2016-08-04 06:20:45 -07001487
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001488 return fRenderTargetContext->fRenderTargetProxy->isBudgeted();
robertphillips714712b2016-08-04 06:20:45 -07001489}
1490
Brian Osman11052242016-10-27 14:47:55 -04001491void GrRenderTargetContext::internalDrawPath(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001492 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001493 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001494 const SkMatrix& viewMatrix,
1495 const SkPath& path,
1496 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001497 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001498 RETURN_IF_ABANDONED
bsalomon8acedde2016-06-24 10:42:16 -07001499 SkASSERT(!path.isEmpty());
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001500 GrShape shape;
robertphillipsea461502015-05-26 11:38:03 -07001501
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001502 GrAAType aaType = this->decideAAType(aa, /*allowMixedSamples*/ true);
1503 if (style.isSimpleHairline() && aaType == GrAAType::kMixedSamples) {
1504 // NVPR cannot handle hairlines, so this will would get picked up by a different stencil and
1505 // cover path renderer (i.e. default path renderer). The hairline renderer produces much
1506 // smoother hairlines than MSAA.
1507 aaType = GrAAType::kCoverage;
bsalomon0a0f67e2016-06-28 11:56:42 -07001508 }
robertphillips68737822015-10-29 12:12:21 -07001509 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Robert Phillips72152832017-01-25 17:31:35 -05001510 canDrawArgs.fShaderCaps = this->drawingManager()->getContext()->caps()->shaderCaps();
robertphillips68737822015-10-29 12:12:21 -07001511 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07001512 canDrawArgs.fShape = &shape;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001513 canDrawArgs.fHasUserStencilSettings = false;
robertphillips68737822015-10-29 12:12:21 -07001514
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001515 GrPathRenderer* pr;
Brian Salomon82125e92016-12-10 09:35:48 -05001516 static constexpr GrPathRendererChain::DrawType kType = GrPathRendererChain::DrawType::kColor;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001517 do {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001518 shape = GrShape(path, style);
bsalomon8acedde2016-06-24 10:42:16 -07001519 if (shape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001520 return;
1521 }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001522
1523 canDrawArgs.fAAType = aaType;
1524
1525 // Try a 1st time without applying any of the style to the geometry (and barring sw)
Robert Phillips72152832017-01-25 17:31:35 -05001526 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001527 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
1528
1529 if (!pr && shape.style().pathEffect()) {
1530 // It didn't work above, so try again with the path effect applied.
1531 shape = shape.applyStyle(GrStyle::Apply::kPathEffectOnly, styleScale);
bsalomon8acedde2016-06-24 10:42:16 -07001532 if (shape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001533 return;
1534 }
Robert Phillips72152832017-01-25 17:31:35 -05001535 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
bsalomon6663acf2016-05-10 09:14:17 -07001536 }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001537 if (!pr) {
1538 if (shape.style().applies()) {
1539 shape = shape.applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec, styleScale);
1540 if (shape.isEmpty()) {
1541 return;
1542 }
1543 }
1544 // This time, allow SW renderer
Robert Phillips72152832017-01-25 17:31:35 -05001545 pr = this->drawingManager()->getPathRenderer(canDrawArgs, true, kType);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001546 }
Brian Salomon0abc8b42016-12-13 10:22:54 -05001547 if (!pr && GrAATypeIsHW(aaType)) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001548 // There are exceptional cases where we may wind up falling back to coverage based AA
1549 // when the target is MSAA (e.g. through disabling path renderers via GrContextOptions).
1550 aaType = GrAAType::kCoverage;
1551 } else {
1552 break;
1553 }
1554 } while(true);
robertphillipsea461502015-05-26 11:38:03 -07001555
bsalomon8acedde2016-06-24 10:42:16 -07001556 if (!pr) {
robertphillipsea461502015-05-26 11:38:03 -07001557#ifdef SK_DEBUG
1558 SkDebugf("Unable to find path renderer compatible with path.\n");
1559#endif
1560 return;
1561 }
1562
Robert Phillips72152832017-01-25 17:31:35 -05001563 GrPathRenderer::DrawPathArgs args{this->drawingManager()->getContext()->resourceProvider(),
Brian Salomon82f44312017-01-11 13:42:54 -05001564 std::move(paint),
1565 &GrUserStencilSettings::kUnused,
1566 this,
1567 &clip,
1568 &viewMatrix,
1569 &shape,
1570 aaType,
1571 this->isGammaCorrect()};
bsalomon0aff2fa2015-07-31 06:48:27 -07001572 pr->drawPath(args);
robertphillipsea461502015-05-26 11:38:03 -07001573}
1574
Brian Salomon42521e82016-12-07 16:44:58 -05001575void GrRenderTargetContext::addDrawOp(const GrPipelineBuilder& pipelineBuilder, const GrClip& clip,
Brian Salomonf8334782017-01-03 09:42:58 -05001576 std::unique_ptr<GrDrawOp> op) {
joshualitt1de610a2016-01-06 08:26:09 -08001577 ASSERT_SINGLE_OWNER
robertphillips2d70dcb2015-10-06 07:38:23 -07001578 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001579 SkDEBUGCODE(this->validate();)
Brian Salomon42521e82016-12-07 16:44:58 -05001580 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::addDrawOp");
robertphillips2d70dcb2015-10-06 07:38:23 -07001581
Brian Salomon24f19782016-12-13 15:10:11 -05001582 this->getOpList()->addDrawOp(pipelineBuilder, this, clip, std::move(op));
robertphillips2334fb62015-06-17 05:43:33 -07001583}