blob: b0a1b6d4af799a25935cd66378aa6b944cb11ea6 [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 Salomon467921e2017-03-06 16:17:12 -05009#include "GrAppliedClip.h"
Brian Salomon5ec9def2016-12-20 15:34:05 -050010#include "GrColor.h"
Robert Phillipse78b7252017-04-06 07:59:41 -040011#include "GrContextPriv.h"
robertphillips77a2e522015-10-17 07:43:27 -070012#include "GrDrawingManager.h"
csmartdalton02fa32c2016-08-19 13:29:27 -070013#include "GrFixedClip.h"
robertphillips714712b2016-08-04 06:20:45 -070014#include "GrGpuResourcePriv.h"
robertphillipsea461502015-05-26 11:38:03 -070015#include "GrPathRenderer.h"
robertphillips5fa7f302016-07-21 09:21:04 -070016#include "GrPipelineBuilder.h"
robertphillips2334fb62015-06-17 05:43:33 -070017#include "GrRenderTarget.h"
Brian Salomon5ec9def2016-12-20 15:34:05 -050018#include "GrRenderTargetContextPriv.h"
robertphillips2334fb62015-06-17 05:43:33 -070019#include "GrRenderTargetPriv.h"
bsalomon473addf2015-10-02 07:49:05 -070020#include "GrResourceProvider.h"
Brian Salomon467921e2017-03-06 16:17:12 -050021#include "GrStencilAttachment.h"
22#include "SkLatticeIter.h"
23#include "SkMatrixPriv.h"
robertphillips2d70dcb2015-10-06 07:38:23 -070024#include "SkSurfacePriv.h"
Brian Salomon467921e2017-03-06 16:17:12 -050025#include "effects/GrRRectEffect.h"
26#include "instanced/InstancedRendering.h"
Brian Salomon89527432016-12-16 09:52:16 -050027#include "ops/GrClearOp.h"
Robert Phillipsb9a02a12017-04-06 11:08:40 -040028#include "ops/GrClearStencilClipOp.h"
Brian Salomon467921e2017-03-06 16:17:12 -050029#include "ops/GrDrawOp.h"
Brian Salomon89527432016-12-16 09:52:16 -050030#include "ops/GrDrawAtlasOp.h"
31#include "ops/GrDrawVerticesOp.h"
32#include "ops/GrLatticeOp.h"
33#include "ops/GrOp.h"
34#include "ops/GrOvalOpFactory.h"
35#include "ops/GrRectOpFactory.h"
36#include "ops/GrRegionOp.h"
37#include "ops/GrShadowRRectOp.h"
Brian Salomon467921e2017-03-06 16:17:12 -050038#include "ops/GrStencilPathOp.h"
joshualitte8042922015-12-11 06:11:21 -080039#include "text/GrAtlasTextContext.h"
40#include "text/GrStencilAndCoverTextContext.h"
joshualittbc907352016-01-13 06:45:40 -080041#include "../private/GrAuditTrail.h"
42
Robert Phillips72152832017-01-25 17:31:35 -050043#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this->drawingManager()->getContext())
joshualitt1de610a2016-01-06 08:26:09 -080044#define ASSERT_SINGLE_OWNER \
joshualittde8dc7e2016-01-08 10:09:13 -080045 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fSingleOwner);)
robertphillips391395d2016-03-02 09:26:36 -080046#define ASSERT_SINGLE_OWNER_PRIV \
Brian Osman11052242016-10-27 14:47:55 -040047 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fRenderTargetContext->fSingleOwner);)
Robert Phillips72152832017-01-25 17:31:35 -050048#define RETURN_IF_ABANDONED if (this->drawingManager()->wasAbandoned()) { return; }
49#define RETURN_IF_ABANDONED_PRIV if (fRenderTargetContext->drawingManager()->wasAbandoned()) { return; }
50#define RETURN_FALSE_IF_ABANDONED if (this->drawingManager()->wasAbandoned()) { return false; }
51#define RETURN_FALSE_IF_ABANDONED_PRIV if (fRenderTargetContext->drawingManager()->wasAbandoned()) { return false; }
52#define RETURN_NULL_IF_ABANDONED if (this->drawingManager()->wasAbandoned()) { return nullptr; }
robertphillipsea461502015-05-26 11:38:03 -070053
csmartdaltona7f29642016-07-07 08:49:11 -070054using gr_instanced::InstancedRendering;
55
robertphillipsea461502015-05-26 11:38:03 -070056class AutoCheckFlush {
57public:
halcanary9d524f22016-03-29 09:03:52 -070058 AutoCheckFlush(GrDrawingManager* drawingManager) : fDrawingManager(drawingManager) {
robertphillips77a2e522015-10-17 07:43:27 -070059 SkASSERT(fDrawingManager);
60 }
bsalomonb77a9072016-09-07 10:02:04 -070061 ~AutoCheckFlush() { fDrawingManager->flushIfNecessary(); }
robertphillipsea461502015-05-26 11:38:03 -070062
63private:
robertphillips77a2e522015-10-17 07:43:27 -070064 GrDrawingManager* fDrawingManager;
robertphillipsea461502015-05-26 11:38:03 -070065};
66
Brian Osman11052242016-10-27 14:47:55 -040067bool GrRenderTargetContext::wasAbandoned() const {
Robert Phillips72152832017-01-25 17:31:35 -050068 return this->drawingManager()->wasAbandoned();
robertphillips7761d612016-05-16 09:14:53 -070069}
70
Robert Phillipsf2361d22016-10-25 14:20:06 -040071// In MDB mode the reffing of the 'getLastOpList' call's result allows in-progress
Brian Osman11052242016-10-27 14:47:55 -040072// GrOpLists to be picked up and added to by renderTargetContexts lower in the call
Robert Phillipsf2361d22016-10-25 14:20:06 -040073// stack. When this occurs with a closed GrOpList, a new one will be allocated
Brian Osman11052242016-10-27 14:47:55 -040074// when the renderTargetContext attempts to use it (via getOpList).
75GrRenderTargetContext::GrRenderTargetContext(GrContext* context,
76 GrDrawingManager* drawingMgr,
Robert Phillipsc7635fa2016-10-28 13:25:24 -040077 sk_sp<GrRenderTargetProxy> rtp,
Brian Osman11052242016-10-27 14:47:55 -040078 sk_sp<SkColorSpace> colorSpace,
79 const SkSurfaceProps* surfaceProps,
80 GrAuditTrail* auditTrail,
81 GrSingleOwner* singleOwner)
Robert Phillips72152832017-01-25 17:31:35 -050082 : GrSurfaceContext(context, drawingMgr, std::move(colorSpace), auditTrail, singleOwner)
Robert Phillipsc7635fa2016-10-28 13:25:24 -040083 , fRenderTargetProxy(std::move(rtp))
84 , fOpList(SkSafeRef(fRenderTargetProxy->getLastRenderTargetOpList()))
Robert Phillipsc7635fa2016-10-28 13:25:24 -040085 , fInstancedPipelineInfo(fRenderTargetProxy.get())
brianosman5a7ae7e2016-09-12 12:07:25 -070086 , fColorXformFromSRGB(nullptr)
Robert Phillips2c862492017-01-18 10:08:39 -050087 , fSurfaceProps(SkSurfacePropsCopyOrDefault(surfaceProps)) {
brianosman5a7ae7e2016-09-12 12:07:25 -070088 if (fColorSpace) {
89 // sRGB sources are very common (SkColor, etc...), so we cache that gamut transformation
Matt Sarett77a7a1b2017-02-07 13:56:11 -050090 auto srgbColorSpace = SkColorSpace::MakeSRGB();
msarettc71a9b72016-09-16 11:01:27 -070091 fColorXformFromSRGB = GrColorSpaceXform::Make(srgbColorSpace.get(), fColorSpace.get());
brianosman5a7ae7e2016-09-12 12:07:25 -070092 }
robertphillips2e1e51f2015-10-15 08:01:48 -070093 SkDEBUGCODE(this->validate();)
robertphillipsea461502015-05-26 11:38:03 -070094}
95
robertphillips2e1e51f2015-10-15 08:01:48 -070096#ifdef SK_DEBUG
Brian Osman11052242016-10-27 14:47:55 -040097void GrRenderTargetContext::validate() const {
Robert Phillipsc7635fa2016-10-28 13:25:24 -040098 SkASSERT(fRenderTargetProxy);
99 fRenderTargetProxy->validate(fContext);
robertphillipsa106c622015-10-16 09:07:06 -0700100
Robert Phillipsf2361d22016-10-25 14:20:06 -0400101 if (fOpList && !fOpList->isClosed()) {
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400102 SkASSERT(fRenderTargetProxy->getLastOpList() == fOpList);
robertphillipsa106c622015-10-16 09:07:06 -0700103 }
robertphillips2e1e51f2015-10-15 08:01:48 -0700104}
105#endif
106
Brian Osman11052242016-10-27 14:47:55 -0400107GrRenderTargetContext::~GrRenderTargetContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800108 ASSERT_SINGLE_OWNER
Robert Phillipsf2361d22016-10-25 14:20:06 -0400109 SkSafeUnref(fOpList);
robertphillipsa106c622015-10-16 09:07:06 -0700110}
111
Robert Phillipsf200a902017-01-30 13:27:37 -0500112GrTextureProxy* GrRenderTargetContext::asTextureProxy() {
Robert Phillipseaa86252016-11-08 13:49:39 +0000113 return fRenderTargetProxy->asTextureProxy();
114}
115
Robert Phillipsf200a902017-01-30 13:27:37 -0500116sk_sp<GrTextureProxy> GrRenderTargetContext::asTextureProxyRef() {
117 return sk_ref_sp(fRenderTargetProxy->asTextureProxy());
118}
119
Brian Osman11052242016-10-27 14:47:55 -0400120GrRenderTargetOpList* GrRenderTargetContext::getOpList() {
joshualitt1de610a2016-01-06 08:26:09 -0800121 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700122 SkDEBUGCODE(this->validate();)
123
Robert Phillipsf2361d22016-10-25 14:20:06 -0400124 if (!fOpList || fOpList->isClosed()) {
Robert Phillips72152832017-01-25 17:31:35 -0500125 fOpList = this->drawingManager()->newOpList(fRenderTargetProxy.get());
robertphillipsa106c622015-10-16 09:07:06 -0700126 }
127
Robert Phillipsf2361d22016-10-25 14:20:06 -0400128 return fOpList;
robertphillipsa106c622015-10-16 09:07:06 -0700129}
130
Robert Phillipse2f7d182016-12-15 09:23:05 -0500131// TODO: move this (and GrTextContext::copy) to GrSurfaceContext?
132bool GrRenderTargetContext::onCopy(GrSurfaceProxy* srcProxy,
133 const SkIRect& srcRect,
134 const SkIPoint& dstPoint) {
joshualitt1de610a2016-01-06 08:26:09 -0800135 ASSERT_SINGLE_OWNER
bsalomonb8fea972016-02-16 07:34:17 -0800136 RETURN_FALSE_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700137 SkDEBUGCODE(this->validate();)
Robert Phillipsbf25d432017-04-07 10:08:53 -0400138 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::onCopy");
Robert Phillipse2f7d182016-12-15 09:23:05 -0500139
Robert Phillipsbf25d432017-04-07 10:08:53 -0400140 return this->getOpList()->copySurface(fContext->resourceProvider(),
141 fRenderTargetProxy.get(), srcProxy, srcRect, dstPoint);
robertphillipsea461502015-05-26 11:38:03 -0700142}
143
Robert Phillips2c862492017-01-18 10:08:39 -0500144// TODO: move this (and GrTextureContext::onReadPixels) to GrSurfaceContext?
145bool GrRenderTargetContext::onReadPixels(const SkImageInfo& dstInfo, void* dstBuffer,
Robert Phillipsb726d582017-03-09 16:36:32 -0500146 size_t dstRowBytes, int x, int y, uint32_t flags) {
Robert Phillips2c862492017-01-18 10:08:39 -0500147 // TODO: teach GrRenderTarget to take ImageInfo directly to specify the src pixels
148 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo, *fContext->caps());
149 if (kUnknown_GrPixelConfig == config) {
150 return false;
151 }
152
Robert Phillipsb726d582017-03-09 16:36:32 -0500153 // TODO: this seems to duplicate code in SkImage_Gpu::onReadPixels
Robert Phillips2c862492017-01-18 10:08:39 -0500154 if (kUnpremul_SkAlphaType == dstInfo.alphaType()) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400155 flags |= GrContextPriv::kUnpremul_PixelOpsFlag;
Robert Phillips2c862492017-01-18 10:08:39 -0500156 }
157
Robert Phillipse78b7252017-04-06 07:59:41 -0400158 return fContext->contextPriv().readSurfacePixels(fRenderTargetProxy.get(),
159 this->getColorSpace(), x, y,
160 dstInfo.width(), dstInfo.height(), config,
161 dstInfo.colorSpace(),
162 dstBuffer, dstRowBytes, flags);
Robert Phillips2c862492017-01-18 10:08:39 -0500163}
164
165// TODO: move this (and GrTextureContext::onReadPixels) to GrSurfaceContext?
166bool GrRenderTargetContext::onWritePixels(const SkImageInfo& srcInfo, const void* srcBuffer,
Robert Phillips30f9bc62017-02-22 15:28:38 -0500167 size_t srcRowBytes, int x, int y, uint32_t flags) {
Robert Phillips2c862492017-01-18 10:08:39 -0500168 // TODO: teach GrRenderTarget to take ImageInfo directly to specify the src pixels
169 GrPixelConfig config = SkImageInfo2GrPixelConfig(srcInfo, *fContext->caps());
170 if (kUnknown_GrPixelConfig == config) {
171 return false;
172 }
Robert Phillips2c862492017-01-18 10:08:39 -0500173 if (kUnpremul_SkAlphaType == srcInfo.alphaType()) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400174 flags |= GrContextPriv::kUnpremul_PixelOpsFlag;
Robert Phillips2c862492017-01-18 10:08:39 -0500175 }
176
Robert Phillipse78b7252017-04-06 07:59:41 -0400177 return fContext->contextPriv().writeSurfacePixels(fRenderTargetProxy.get(),
178 this->getColorSpace(), x, y,
179 srcInfo.width(), srcInfo.height(),
180 config, srcInfo.colorSpace(),
181 srcBuffer, srcRowBytes, flags);
Robert Phillips2c862492017-01-18 10:08:39 -0500182}
183
184
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500185void GrRenderTargetContext::drawText(const GrClip& clip, const SkPaint& skPaint,
Brian Salomon82f44312017-01-11 13:42:54 -0500186 const SkMatrix& viewMatrix, const char text[],
187 size_t byteLength, SkScalar x, SkScalar y,
188 const SkIRect& clipBounds) {
joshualitt1de610a2016-01-06 08:26:09 -0800189 ASSERT_SINGLE_OWNER
robertphillips2d70dcb2015-10-06 07:38:23 -0700190 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700191 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400192 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawText");
robertphillips2d70dcb2015-10-06 07:38:23 -0700193
Robert Phillips72152832017-01-25 17:31:35 -0500194 GrAtlasTextContext* atlasTextContext = this->drawingManager()->getAtlasTextContext();
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500195 atlasTextContext->drawText(fContext, this, clip, skPaint, viewMatrix, fSurfaceProps, text,
196 byteLength, x, y, clipBounds);
robertphillips2334fb62015-06-17 05:43:33 -0700197}
robertphillipscaef3452015-11-11 13:18:11 -0800198
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500199void GrRenderTargetContext::drawPosText(const GrClip& clip, const SkPaint& paint,
200 const SkMatrix& viewMatrix, const char text[],
201 size_t byteLength, const SkScalar pos[],
Brian Salomon82f44312017-01-11 13:42:54 -0500202 int scalarsPerPosition, const SkPoint& offset,
203 const SkIRect& clipBounds) {
joshualitt1de610a2016-01-06 08:26:09 -0800204 ASSERT_SINGLE_OWNER
robertphillips2d70dcb2015-10-06 07:38:23 -0700205 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700206 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400207 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawPosText");
robertphillips2d70dcb2015-10-06 07:38:23 -0700208
Robert Phillips72152832017-01-25 17:31:35 -0500209 GrAtlasTextContext* atlasTextContext = this->drawingManager()->getAtlasTextContext();
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500210 atlasTextContext->drawPosText(fContext, this, clip, paint, viewMatrix, fSurfaceProps, text,
211 byteLength, pos, scalarsPerPosition, offset, clipBounds);
robertphillips2334fb62015-06-17 05:43:33 -0700212}
robertphillipscaef3452015-11-11 13:18:11 -0800213
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500214void GrRenderTargetContext::drawTextBlob(const GrClip& clip, const SkPaint& paint,
Brian Osman11052242016-10-27 14:47:55 -0400215 const SkMatrix& viewMatrix, const SkTextBlob* blob,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500216 SkScalar x, SkScalar y, SkDrawFilter* filter,
217 const SkIRect& clipBounds) {
joshualitt1de610a2016-01-06 08:26:09 -0800218 ASSERT_SINGLE_OWNER
robertphillips2d70dcb2015-10-06 07:38:23 -0700219 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700220 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400221 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawTextBlob");
robertphillips2d70dcb2015-10-06 07:38:23 -0700222
Robert Phillips72152832017-01-25 17:31:35 -0500223 GrAtlasTextContext* atlasTextContext = this->drawingManager()->getAtlasTextContext();
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500224 atlasTextContext->drawTextBlob(fContext, this, clip, paint, viewMatrix, fSurfaceProps, blob, x,
225 y, filter, clipBounds);
robertphillipsea461502015-05-26 11:38:03 -0700226}
227
Brian Osman11052242016-10-27 14:47:55 -0400228void GrRenderTargetContext::discard() {
joshualitt1de610a2016-01-06 08:26:09 -0800229 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700230 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700231 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400232 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::discard");
robertphillips2e1e51f2015-10-15 08:01:48 -0700233
Robert Phillips72152832017-01-25 17:31:35 -0500234 AutoCheckFlush acf(this->drawingManager());
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400235
Brian Salomon69868af2016-12-22 15:42:51 -0500236 // TODO: This needs to be fixed up since it ends the deferral of the GrRenderTarget.
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400237 sk_sp<GrRenderTarget> rt(
Brian Osman32342f02017-03-04 08:12:46 -0500238 sk_ref_sp(fRenderTargetProxy->instantiate(fContext->resourceProvider())));
Robert Phillipse60ad622016-11-17 10:22:48 -0500239 if (!rt) {
240 return;
241 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400242
Brian Salomon69868af2016-12-22 15:42:51 -0500243 this->getOpList()->discard(this);
robertphillipsea461502015-05-26 11:38:03 -0700244}
245
Brian Osman11052242016-10-27 14:47:55 -0400246void GrRenderTargetContext::clear(const SkIRect* rect,
247 const GrColor color,
248 bool canIgnoreRect) {
joshualitt1de610a2016-01-06 08:26:09 -0800249 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700250 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700251 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400252 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::clear");
robertphillipsea461502015-05-26 11:38:03 -0700253
Robert Phillips72152832017-01-25 17:31:35 -0500254 AutoCheckFlush acf(this->drawingManager());
csmartdalton29df7602016-08-31 11:55:52 -0700255 this->internalClear(rect ? GrFixedClip(*rect) : GrFixedClip::Disabled(), color, canIgnoreRect);
256}
robertphillips9199a9f2016-07-13 07:48:43 -0700257
Robert Phillips784b7bf2016-12-09 13:35:02 -0500258void GrRenderTargetContextPriv::absClear(const SkIRect* clearRect, const GrColor color) {
259 ASSERT_SINGLE_OWNER_PRIV
260 RETURN_IF_ABANDONED_PRIV
261 SkDEBUGCODE(fRenderTargetContext->validate();)
262 GR_AUDIT_TRAIL_AUTO_FRAME(fRenderTargetContext->fAuditTrail,
263 "GrRenderTargetContext::absClear");
264
Robert Phillips72152832017-01-25 17:31:35 -0500265 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500266
267 SkIRect rtRect = SkIRect::MakeWH(fRenderTargetContext->fRenderTargetProxy->worstCaseWidth(
268 *fRenderTargetContext->caps()),
269 fRenderTargetContext->fRenderTargetProxy->worstCaseHeight(
270 *fRenderTargetContext->caps()));
271
272 if (clearRect) {
273 if (clearRect->contains(rtRect)) {
274 clearRect = nullptr; // full screen
275 } else {
276 if (!rtRect.intersect(*clearRect)) {
277 return;
278 }
279 }
280 }
281
282 // TODO: in a post-MDB world this should be handled at the OpList level.
283 // An op-list that is initially cleared and has no other ops should receive an
284 // extra draw.
285 if (fRenderTargetContext->fContext->caps()->useDrawInsteadOfClear()) {
286 // This works around a driver bug with clear by drawing a rect instead.
287 // The driver will ignore a clear if it is the only thing rendered to a
288 // target before the target is read.
289 GrPaint paint;
290 paint.setColor4f(GrColor4f::FromGrColor(color));
Brian Salomona1633922017-01-09 11:46:10 -0500291 paint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
Robert Phillips784b7bf2016-12-09 13:35:02 -0500292
293 // We don't call drawRect() here to avoid the cropping to the, possibly smaller,
294 // RenderTargetProxy bounds
Brian Salomon82f44312017-01-11 13:42:54 -0500295 fRenderTargetContext->drawNonAAFilledRect(GrNoClip(), std::move(paint), SkMatrix::I(),
296 SkRect::Make(rtRect), nullptr, nullptr, nullptr,
297 GrAAType::kNone);
Robert Phillips784b7bf2016-12-09 13:35:02 -0500298
299 } else {
Robert Phillips784b7bf2016-12-09 13:35:02 -0500300 // This path doesn't handle coalescing of full screen clears b.c. it
301 // has to clear the entire render target - not just the content area.
302 // It could be done but will take more finagling.
Robert Phillipsf7a72612017-03-31 10:03:45 -0400303 std::unique_ptr<GrOp> op(GrClearOp::Make(rtRect, color, fRenderTargetContext, !clearRect));
Brian Salomonfc527d22016-12-14 21:07:01 -0500304 if (!op) {
Robert Phillips784b7bf2016-12-09 13:35:02 -0500305 return;
306 }
Brian Salomon69868af2016-12-22 15:42:51 -0500307 fRenderTargetContext->getOpList()->addOp(std::move(op), fRenderTargetContext);
Robert Phillips784b7bf2016-12-09 13:35:02 -0500308 }
309}
310
Brian Osman11052242016-10-27 14:47:55 -0400311void GrRenderTargetContextPriv::clear(const GrFixedClip& clip,
312 const GrColor color,
313 bool canIgnoreClip) {
csmartdalton29df7602016-08-31 11:55:52 -0700314 ASSERT_SINGLE_OWNER_PRIV
315 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400316 SkDEBUGCODE(fRenderTargetContext->validate();)
317 GR_AUDIT_TRAIL_AUTO_FRAME(fRenderTargetContext->fAuditTrail,
318 "GrRenderTargetContextPriv::clear");
csmartdalton29df7602016-08-31 11:55:52 -0700319
Robert Phillips72152832017-01-25 17:31:35 -0500320 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Brian Osman11052242016-10-27 14:47:55 -0400321 fRenderTargetContext->internalClear(clip, color, canIgnoreClip);
csmartdalton29df7602016-08-31 11:55:52 -0700322}
323
Brian Osman11052242016-10-27 14:47:55 -0400324void GrRenderTargetContext::internalClear(const GrFixedClip& clip,
325 const GrColor color,
326 bool canIgnoreClip) {
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700327 bool isFull = false;
328 if (!clip.hasWindowRectangles()) {
329 isFull = !clip.scissorEnabled() ||
330 (canIgnoreClip && fContext->caps()->fullClearIsFree()) ||
331 clip.scissorRect().contains(SkIRect::MakeWH(this->width(), this->height()));
332 }
robertphillips9199a9f2016-07-13 07:48:43 -0700333
334 if (fContext->caps()->useDrawInsteadOfClear()) {
335 // This works around a driver bug with clear by drawing a rect instead.
336 // The driver will ignore a clear if it is the only thing rendered to a
337 // target before the target is read.
Robert Phillips784b7bf2016-12-09 13:35:02 -0500338 SkIRect clearRect = SkIRect::MakeWH(this->width(), this->height());
csmartdalton29df7602016-08-31 11:55:52 -0700339 if (isFull) {
robertphillips9199a9f2016-07-13 07:48:43 -0700340 this->discard();
Robert Phillips93f16332016-11-23 19:37:13 -0500341 } else if (!clearRect.intersect(clip.scissorRect())) {
csmartdalton29df7602016-08-31 11:55:52 -0700342 return;
robertphillips9199a9f2016-07-13 07:48:43 -0700343 }
344
345 GrPaint paint;
346 paint.setColor4f(GrColor4f::FromGrColor(color));
Brian Salomona1633922017-01-09 11:46:10 -0500347 paint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
robertphillips9199a9f2016-07-13 07:48:43 -0700348
Brian Salomon82f44312017-01-11 13:42:54 -0500349 this->drawRect(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), SkRect::Make(clearRect));
bsalomon9f129de2016-08-10 16:31:05 -0700350 } else if (isFull) {
Robert Phillipsf7a72612017-03-31 10:03:45 -0400351 this->getOpList()->fullClear(this, color);
robertphillips9199a9f2016-07-13 07:48:43 -0700352 } else {
Robert Phillipsf7a72612017-03-31 10:03:45 -0400353 std::unique_ptr<GrOp> op(GrClearOp::Make(clip, color, this));
Brian Salomon42521e82016-12-07 16:44:58 -0500354 if (!op) {
csmartdalton29df7602016-08-31 11:55:52 -0700355 return;
356 }
Brian Salomon69868af2016-12-22 15:42:51 -0500357 this->getOpList()->addOp(std::move(op), this);
robertphillips9199a9f2016-07-13 07:48:43 -0700358 }
robertphillipsea461502015-05-26 11:38:03 -0700359}
360
Brian Osman11052242016-10-27 14:47:55 -0400361void GrRenderTargetContext::drawPaint(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500362 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400363 const SkMatrix& viewMatrix) {
joshualitt1de610a2016-01-06 08:26:09 -0800364 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700365 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700366 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400367 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawPaint");
robertphillips2e1e51f2015-10-15 08:01:48 -0700368
robertphillipsea461502015-05-26 11:38:03 -0700369 // set rect to be big enough to fill the space, but not super-huge, so we
370 // don't overflow fixed-point implementations
robertphillips13a7eee2016-08-31 15:06:24 -0700371
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400372 SkRect r = fRenderTargetProxy->getBoundsRect();
robertphillipsea461502015-05-26 11:38:03 -0700373
bsalomoncb31e512016-08-26 10:48:19 -0700374 SkRRect rrect;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500375 GrAA aa;
bsalomoncb31e512016-08-26 10:48:19 -0700376 // Check if we can replace a clipRRect()/drawPaint() with a drawRRect(). We only do the
377 // transformation for non-rect rrects. Rects caused a performance regression on an Android
378 // test that needs investigation. We also skip cases where there are fragment processors
379 // because they may depend on having correct local coords and this path draws in device space
380 // without a local matrix.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500381 if (!paint.numTotalFragmentProcessors() && clip.isRRect(r, &rrect, &aa) && !rrect.isRect()) {
Brian Salomon82f44312017-01-11 13:42:54 -0500382 this->drawRRect(GrNoClip(), std::move(paint), aa, SkMatrix::I(), rrect,
383 GrStyle::SimpleFill());
bsalomoncb31e512016-08-26 10:48:19 -0700384 return;
385 }
386
robertphillipsea461502015-05-26 11:38:03 -0700387
388 bool isPerspective = viewMatrix.hasPerspective();
389
390 // We attempt to map r by the inverse matrix and draw that. mapRect will
391 // map the four corners and bound them with a new rect. This will not
392 // produce a correct result for some perspective matrices.
393 if (!isPerspective) {
reeda39667c2016-08-22 06:39:49 -0700394 if (!SkMatrixPriv::InverseMapRect(viewMatrix, &r, r)) {
robertphillipsea461502015-05-26 11:38:03 -0700395 SkDebugf("Could not invert matrix\n");
396 return;
397 }
Brian Salomon82f44312017-01-11 13:42:54 -0500398 this->drawRect(clip, std::move(paint), GrAA::kNo, viewMatrix, r);
robertphillipsea461502015-05-26 11:38:03 -0700399 } else {
400 SkMatrix localMatrix;
401 if (!viewMatrix.invert(&localMatrix)) {
402 SkDebugf("Could not invert matrix\n");
403 return;
404 }
405
Robert Phillips72152832017-01-25 17:31:35 -0500406 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700407
Brian Salomon82f44312017-01-11 13:42:54 -0500408 this->drawNonAAFilledRect(clip, std::move(paint), SkMatrix::I(), r, nullptr, &localMatrix,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500409 nullptr, GrAAType::kNone);
robertphillipsea461502015-05-26 11:38:03 -0700410 }
411}
412
robertphillipsea461502015-05-26 11:38:03 -0700413static inline bool rect_contains_inclusive(const SkRect& rect, const SkPoint& point) {
414 return point.fX >= rect.fLeft && point.fX <= rect.fRight &&
415 point.fY >= rect.fTop && point.fY <= rect.fBottom;
416}
417
bsalomonc55271f2015-11-09 11:55:57 -0800418static bool view_matrix_ok_for_aa_fill_rect(const SkMatrix& viewMatrix) {
419 return viewMatrix.preservesRightAngles();
420}
421
csmartdalton97f6cd52016-07-13 13:37:08 -0700422// Attempts to crop a rect and optional local rect to the clip boundaries.
423// Returns false if the draw can be skipped entirely.
robertphillips13a7eee2016-08-31 15:06:24 -0700424static bool crop_filled_rect(int width, int height, const GrClip& clip,
csmartdalton97f6cd52016-07-13 13:37:08 -0700425 const SkMatrix& viewMatrix, SkRect* rect,
426 SkRect* localRect = nullptr) {
427 if (!viewMatrix.rectStaysRect()) {
428 return true;
429 }
430
csmartdalton97f6cd52016-07-13 13:37:08 -0700431 SkIRect clipDevBounds;
432 SkRect clipBounds;
csmartdalton97f6cd52016-07-13 13:37:08 -0700433
robertphillips13a7eee2016-08-31 15:06:24 -0700434 clip.getConservativeBounds(width, height, &clipDevBounds);
reeda39667c2016-08-22 06:39:49 -0700435 if (!SkMatrixPriv::InverseMapRect(viewMatrix, &clipBounds, SkRect::Make(clipDevBounds))) {
436 return false;
437 }
csmartdalton97f6cd52016-07-13 13:37:08 -0700438
439 if (localRect) {
440 if (!rect->intersects(clipBounds)) {
441 return false;
442 }
443 const SkScalar dx = localRect->width() / rect->width();
444 const SkScalar dy = localRect->height() / rect->height();
445 if (clipBounds.fLeft > rect->fLeft) {
446 localRect->fLeft += (clipBounds.fLeft - rect->fLeft) * dx;
447 rect->fLeft = clipBounds.fLeft;
448 }
449 if (clipBounds.fTop > rect->fTop) {
450 localRect->fTop += (clipBounds.fTop - rect->fTop) * dy;
451 rect->fTop = clipBounds.fTop;
452 }
453 if (clipBounds.fRight < rect->fRight) {
454 localRect->fRight -= (rect->fRight - clipBounds.fRight) * dx;
455 rect->fRight = clipBounds.fRight;
456 }
457 if (clipBounds.fBottom < rect->fBottom) {
458 localRect->fBottom -= (rect->fBottom - clipBounds.fBottom) * dy;
459 rect->fBottom = clipBounds.fBottom;
460 }
461 return true;
462 }
463
464 return rect->intersect(clipBounds);
465}
466
Brian Osman11052242016-10-27 14:47:55 -0400467bool GrRenderTargetContext::drawFilledRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500468 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500469 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400470 const SkMatrix& viewMatrix,
471 const SkRect& rect,
472 const GrUserStencilSettings* ss) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700473 SkRect croppedRect = rect;
Robert Phillips784b7bf2016-12-09 13:35:02 -0500474 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix, &croppedRect)) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700475 return true;
476 }
robertphillips44302392016-07-08 14:43:03 -0700477
Brian Salomon54d212e2017-03-21 14:22:38 -0400478 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport() &&
479 (!ss || ss->isDisabled(false))) {
Robert Phillipsf2361d22016-10-25 14:20:06 -0400480 InstancedRendering* ir = this->getOpList()->instancedRendering();
Brian Salomon54d212e2017-03-21 14:22:38 -0400481 std::unique_ptr<GrDrawOp> op = ir->recordRect(croppedRect, viewMatrix, std::move(paint), aa,
482 fInstancedPipelineInfo);
Brian Salomon42521e82016-12-07 16:44:58 -0500483 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -0400484 this->addDrawOp(clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700485 return true;
csmartdaltona7f29642016-07-07 08:49:11 -0700486 }
487 }
Brian Salomon54d212e2017-03-21 14:22:38 -0400488 GrAAType aaType = this->decideAAType(aa);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500489 if (GrAAType::kCoverage == aaType) {
robertphillips391395d2016-03-02 09:26:36 -0800490 // The fill path can handle rotation but not skew.
491 if (view_matrix_ok_for_aa_fill_rect(viewMatrix)) {
492 SkRect devBoundRect;
csmartdalton97f6cd52016-07-13 13:37:08 -0700493 viewMatrix.mapRect(&devBoundRect, croppedRect);
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400494 std::unique_ptr<GrLegacyMeshDrawOp> op =
Brian Salomon649a3412017-03-09 13:50:43 -0500495 GrRectOpFactory::MakeAAFill(paint, viewMatrix, rect, croppedRect, devBoundRect);
Brian Salomon42521e82016-12-07 16:44:58 -0500496 if (op) {
Brian Salomon82f44312017-01-11 13:42:54 -0500497 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
robertphillips44302392016-07-08 14:43:03 -0700498 if (ss) {
499 pipelineBuilder.setUserStencil(ss);
500 }
Brian Salomone14bd802017-04-04 15:13:25 -0400501 this->addLegacyMeshDrawOp(std::move(pipelineBuilder), clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700502 return true;
503 }
robertphillips391395d2016-03-02 09:26:36 -0800504 }
505 } else {
Brian Salomon82f44312017-01-11 13:42:54 -0500506 this->drawNonAAFilledRect(clip, std::move(paint), viewMatrix, croppedRect, nullptr, nullptr,
507 ss, aaType);
robertphillips44302392016-07-08 14:43:03 -0700508 return true;
robertphillips391395d2016-03-02 09:26:36 -0800509 }
510
robertphillips44302392016-07-08 14:43:03 -0700511 return false;
robertphillips391395d2016-03-02 09:26:36 -0800512}
513
Brian Osman11052242016-10-27 14:47:55 -0400514void GrRenderTargetContext::drawRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500515 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500516 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400517 const SkMatrix& viewMatrix,
518 const SkRect& rect,
519 const GrStyle* style) {
bsalomon6663acf2016-05-10 09:14:17 -0700520 if (!style) {
521 style = &GrStyle::SimpleFill();
522 }
joshualitt1de610a2016-01-06 08:26:09 -0800523 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700524 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700525 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400526 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawRect");
robertphillips2e1e51f2015-10-15 08:01:48 -0700527
bsalomon6663acf2016-05-10 09:14:17 -0700528 // Path effects should've been devolved to a path in SkGpuDevice
529 SkASSERT(!style->pathEffect());
robertphillipsea461502015-05-26 11:38:03 -0700530
Robert Phillips72152832017-01-25 17:31:35 -0500531 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700532
bsalomon6663acf2016-05-10 09:14:17 -0700533 const SkStrokeRec& stroke = style->strokeRec();
robertphillips3ab14ca2016-07-10 11:49:39 -0700534 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
535
536 if (!fContext->caps()->useDrawInsteadOfClear()) {
537 // Check if this is a full RT draw and can be replaced with a clear. We don't bother
538 // checking cases where the RT is fully inside a stroke.
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400539 SkRect rtRect = fRenderTargetProxy->getBoundsRect();
robertphillips3ab14ca2016-07-10 11:49:39 -0700540 // Does the clip contain the entire RT?
541 if (clip.quickContains(rtRect)) {
542 SkMatrix invM;
543 if (!viewMatrix.invert(&invM)) {
robertphillipsea461502015-05-26 11:38:03 -0700544 return;
545 }
robertphillips3ab14ca2016-07-10 11:49:39 -0700546 // Does the rect bound the RT?
547 SkPoint srcSpaceRTQuad[4];
548 invM.mapRectToQuad(srcSpaceRTQuad, rtRect);
549 if (rect_contains_inclusive(rect, srcSpaceRTQuad[0]) &&
550 rect_contains_inclusive(rect, srcSpaceRTQuad[1]) &&
551 rect_contains_inclusive(rect, srcSpaceRTQuad[2]) &&
552 rect_contains_inclusive(rect, srcSpaceRTQuad[3])) {
553 // Will it blend?
554 GrColor clearColor;
555 if (paint.isConstantBlendedColor(&clearColor)) {
robertphillips9199a9f2016-07-13 07:48:43 -0700556 this->clear(nullptr, clearColor, true);
robertphillips3ab14ca2016-07-10 11:49:39 -0700557 return;
558 }
559 }
robertphillipsea461502015-05-26 11:38:03 -0700560 }
561 }
robertphillips44302392016-07-08 14:43:03 -0700562
Brian Salomon82f44312017-01-11 13:42:54 -0500563 if (this->drawFilledRect(clip, std::move(paint), aa, viewMatrix, rect, nullptr)) {
robertphillips44302392016-07-08 14:43:03 -0700564 return;
565 }
bsalomona7d85ba2016-07-06 11:54:59 -0700566 } else if (stroke.getStyle() == SkStrokeRec::kStroke_Style ||
567 stroke.getStyle() == SkStrokeRec::kHairline_Style) {
568 if ((!rect.width() || !rect.height()) &&
569 SkStrokeRec::kHairline_Style != stroke.getStyle()) {
570 SkScalar r = stroke.getWidth() / 2;
571 // TODO: Move these stroke->fill fallbacks to GrShape?
572 switch (stroke.getJoin()) {
573 case SkPaint::kMiter_Join:
Brian Salomon82f44312017-01-11 13:42:54 -0500574 this->drawRect(
575 clip, std::move(paint), aa, viewMatrix,
576 {rect.fLeft - r, rect.fTop - r, rect.fRight + r, rect.fBottom + r},
577 &GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700578 return;
579 case SkPaint::kRound_Join:
580 // Raster draws nothing when both dimensions are empty.
581 if (rect.width() || rect.height()){
582 SkRRect rrect = SkRRect::MakeRectXY(rect.makeOutset(r, r), r, r);
Brian Salomon82f44312017-01-11 13:42:54 -0500583 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect,
584 GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700585 return;
586 }
587 case SkPaint::kBevel_Join:
588 if (!rect.width()) {
Brian Salomon82f44312017-01-11 13:42:54 -0500589 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700590 {rect.fLeft - r, rect.fTop, rect.fRight + r, rect.fBottom},
591 &GrStyle::SimpleFill());
592 } else {
Brian Salomon82f44312017-01-11 13:42:54 -0500593 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700594 {rect.fLeft, rect.fTop - r, rect.fRight, rect.fBottom + r},
595 &GrStyle::SimpleFill());
596 }
597 return;
598 }
599 }
robertphillips44302392016-07-08 14:43:03 -0700600
robertphillips44302392016-07-08 14:43:03 -0700601 bool snapToPixelCenters = false;
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400602 std::unique_ptr<GrLegacyMeshDrawOp> op;
robertphillips44302392016-07-08 14:43:03 -0700603
robertphillips391395d2016-03-02 09:26:36 -0800604 GrColor color = paint.getColor();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500605 GrAAType aaType = this->decideAAType(aa);
606 if (GrAAType::kCoverage == aaType) {
cdaltonbb539482016-01-04 09:48:25 -0800607 // The stroke path needs the rect to remain axis aligned (no rotation or skew).
608 if (viewMatrix.rectStaysRect()) {
Brian Salomon6a639042016-12-14 11:08:17 -0500609 op = GrRectOpFactory::MakeAAStroke(color, viewMatrix, rect, stroke);
cdaltonbb539482016-01-04 09:48:25 -0800610 }
robertphillipsea461502015-05-26 11:38:03 -0700611 } else {
robertphillips391395d2016-03-02 09:26:36 -0800612 // Depending on sub-pixel coordinates and the particular GPU, we may lose a corner of
halcanary9d524f22016-03-29 09:03:52 -0700613 // hairline rects. We jam all the vertices to pixel centers to avoid this, but not
robertphillips391395d2016-03-02 09:26:36 -0800614 // when MSAA is enabled because it can cause ugly artifacts.
bsalomona7d85ba2016-07-06 11:54:59 -0700615 snapToPixelCenters = stroke.getStyle() == SkStrokeRec::kHairline_Style &&
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400616 !fRenderTargetProxy->isUnifiedMultisampled();
Brian Salomon6a639042016-12-14 11:08:17 -0500617 op = GrRectOpFactory::MakeNonAAStroke(color, viewMatrix, rect, stroke,
618 snapToPixelCenters);
robertphillips391395d2016-03-02 09:26:36 -0800619 }
robertphillips4bc31812016-03-01 12:22:49 -0800620
Brian Salomon42521e82016-12-07 16:44:58 -0500621 if (op) {
Brian Salomon82f44312017-01-11 13:42:54 -0500622 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
Brian Salomon189098e72017-01-19 09:55:19 -0500623 pipelineBuilder.setSnapVerticesToPixelCenters(snapToPixelCenters);
Brian Salomone14bd802017-04-04 15:13:25 -0400624 this->addLegacyMeshDrawOp(std::move(pipelineBuilder), clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700625 return;
robertphillips4bc31812016-03-01 12:22:49 -0800626 }
robertphillips4bc31812016-03-01 12:22:49 -0800627 }
halcanary9d524f22016-03-29 09:03:52 -0700628
robertphillips4bc31812016-03-01 12:22:49 -0800629 SkPath path;
630 path.setIsVolatile(true);
631 path.addRect(rect);
Brian Salomon82f44312017-01-11 13:42:54 -0500632 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, *style);
robertphillipsea461502015-05-26 11:38:03 -0700633}
634
Robert Phillipsec2249f2016-11-09 08:54:35 -0500635int GrRenderTargetContextPriv::maxWindowRectangles() const {
636 return fRenderTargetContext->fRenderTargetProxy->maxWindowRectangles(
637 *fRenderTargetContext->fContext->caps());
638}
639
Brian Osman11052242016-10-27 14:47:55 -0400640void GrRenderTargetContextPriv::clearStencilClip(const GrFixedClip& clip, bool insideStencilMask) {
robertphillips976f5f02016-06-03 10:59:20 -0700641 ASSERT_SINGLE_OWNER_PRIV
642 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400643 SkDEBUGCODE(fRenderTargetContext->validate();)
644 GR_AUDIT_TRAIL_AUTO_FRAME(fRenderTargetContext->fAuditTrail,
645 "GrRenderTargetContextPriv::clearStencilClip");
robertphillips976f5f02016-06-03 10:59:20 -0700646
Robert Phillips72152832017-01-25 17:31:35 -0500647 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400648
649 std::unique_ptr<GrOp> op(GrClearStencilClipOp::Make(clip, insideStencilMask,
650 fRenderTargetContext));
651 if (!op) {
Robert Phillipse60ad622016-11-17 10:22:48 -0500652 return;
653 }
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400654 fRenderTargetContext->getOpList()->addOp(std::move(op), fRenderTargetContext);
robertphillips976f5f02016-06-03 10:59:20 -0700655}
656
Brian Osman11052242016-10-27 14:47:55 -0400657void GrRenderTargetContextPriv::stencilPath(const GrClip& clip,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500658 GrAAType aaType,
Brian Osman11052242016-10-27 14:47:55 -0400659 const SkMatrix& viewMatrix,
660 const GrPath* path) {
Brian Salomon467921e2017-03-06 16:17:12 -0500661 ASSERT_SINGLE_OWNER_PRIV
662 RETURN_IF_ABANDONED_PRIV
663 SkDEBUGCODE(fRenderTargetContext->validate();)
664 GR_AUDIT_TRAIL_AUTO_FRAME(fRenderTargetContext->fAuditTrail,
665 "GrRenderTargetContext::stencilPath");
666
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500667 SkASSERT(aaType != GrAAType::kCoverage);
Brian Salomon467921e2017-03-06 16:17:12 -0500668
669 bool useHWAA = GrAATypeIsHW(aaType);
670 // TODO: extract portions of checkDraw that are relevant to path stenciling.
671 SkASSERT(path);
672 SkASSERT(fRenderTargetContext->caps()->shaderCaps()->pathRenderingSupport());
673
674 // FIXME: Use path bounds instead of this WAR once
675 // https://bugs.chromium.org/p/skia/issues/detail?id=5640 is resolved.
676 SkRect bounds = SkRect::MakeIWH(fRenderTargetContext->width(), fRenderTargetContext->height());
677
678 // Setup clip
Brian Salomon97180af2017-03-14 13:42:58 -0400679 GrAppliedClip appliedClip;
Brian Salomon467921e2017-03-06 16:17:12 -0500680 if (!clip.apply(fRenderTargetContext->fContext, fRenderTargetContext, useHWAA, true,
Brian Salomon97180af2017-03-14 13:42:58 -0400681 &appliedClip, &bounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -0500682 return;
683 }
684
685 // Coverage AA does not make sense when rendering to the stencil buffer. The caller should never
686 // attempt this in a situation that would require coverage AA.
687 SkASSERT(!appliedClip.clipCoverageFragmentProcessor());
688
689 GrRenderTarget* rt = fRenderTargetContext->accessRenderTarget();
690 if (!rt) {
691 return;
692 }
693 GrStencilAttachment* stencilAttachment =
694 fRenderTargetContext->fContext->resourceProvider()->attachStencilAttachment(rt);
695 if (!stencilAttachment) {
696 SkDebugf("ERROR creating stencil attachment. Draw skipped.\n");
697 return;
698 }
699
700 std::unique_ptr<GrOp> op = GrStencilPathOp::Make(viewMatrix,
701 useHWAA,
702 path->getFillType(),
703 appliedClip.hasStencilClip(),
704 stencilAttachment->bits(),
705 appliedClip.scissorState(),
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400706 fRenderTargetContext,
Brian Salomon467921e2017-03-06 16:17:12 -0500707 path);
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400708 if (!op) {
709 return;
710 }
Brian Salomon97180af2017-03-14 13:42:58 -0400711 op->setClippedBounds(bounds);
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400712 fRenderTargetContext->getOpList()->addOp(std::move(op), fRenderTargetContext);
robertphillips976f5f02016-06-03 10:59:20 -0700713}
714
Brian Osman11052242016-10-27 14:47:55 -0400715void GrRenderTargetContextPriv::stencilRect(const GrClip& clip,
716 const GrUserStencilSettings* ss,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500717 GrAAType aaType,
Brian Osman11052242016-10-27 14:47:55 -0400718 const SkMatrix& viewMatrix,
719 const SkRect& rect) {
robertphillips976f5f02016-06-03 10:59:20 -0700720 ASSERT_SINGLE_OWNER_PRIV
721 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400722 SkDEBUGCODE(fRenderTargetContext->validate();)
723 GR_AUDIT_TRAIL_AUTO_FRAME(fRenderTargetContext->fAuditTrail,
724 "GrRenderTargetContext::stencilRect");
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500725 SkASSERT(GrAAType::kCoverage != aaType);
Robert Phillips72152832017-01-25 17:31:35 -0500726 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips976f5f02016-06-03 10:59:20 -0700727
728 GrPaint paint;
Brian Salomona1633922017-01-09 11:46:10 -0500729 paint.setXPFactory(GrDisableColorXPFactory::Get());
robertphillips976f5f02016-06-03 10:59:20 -0700730
Brian Salomon82f44312017-01-11 13:42:54 -0500731 fRenderTargetContext->drawNonAAFilledRect(clip, std::move(paint), viewMatrix, rect, nullptr,
732 nullptr, ss, aaType);
robertphillips976f5f02016-06-03 10:59:20 -0700733}
734
Brian Osman11052242016-10-27 14:47:55 -0400735bool GrRenderTargetContextPriv::drawAndStencilRect(const GrClip& clip,
736 const GrUserStencilSettings* ss,
737 SkRegion::Op op,
738 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500739 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400740 const SkMatrix& viewMatrix,
741 const SkRect& rect) {
robertphillips391395d2016-03-02 09:26:36 -0800742 ASSERT_SINGLE_OWNER_PRIV
743 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400744 SkDEBUGCODE(fRenderTargetContext->validate();)
745 GR_AUDIT_TRAIL_AUTO_FRAME(fRenderTargetContext->fAuditTrail,
746 "GrRenderTargetContext::drawAndStencilRect");
robertphillips391395d2016-03-02 09:26:36 -0800747
Robert Phillips72152832017-01-25 17:31:35 -0500748 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -0800749
750 GrPaint paint;
robertphillips391395d2016-03-02 09:26:36 -0800751 paint.setCoverageSetOpXPFactory(op, invert);
752
Brian Salomon82f44312017-01-11 13:42:54 -0500753 if (fRenderTargetContext->drawFilledRect(clip, std::move(paint), aa, viewMatrix, rect, ss)) {
robertphillips391395d2016-03-02 09:26:36 -0800754 return true;
755 }
robertphillips391395d2016-03-02 09:26:36 -0800756 SkPath path;
757 path.setIsVolatile(true);
758 path.addRect(rect);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500759 return this->drawAndStencilPath(clip, ss, op, invert, aa, viewMatrix, path);
robertphillips391395d2016-03-02 09:26:36 -0800760}
761
Brian Osman11052242016-10-27 14:47:55 -0400762void GrRenderTargetContext::fillRectToRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500763 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500764 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400765 const SkMatrix& viewMatrix,
766 const SkRect& rectToDraw,
767 const SkRect& localRect) {
joshualitt1de610a2016-01-06 08:26:09 -0800768 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700769 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700770 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400771 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::fillRectToRect");
robertphillipsea461502015-05-26 11:38:03 -0700772
csmartdalton97f6cd52016-07-13 13:37:08 -0700773 SkRect croppedRect = rectToDraw;
774 SkRect croppedLocalRect = localRect;
robertphillips13a7eee2016-08-31 15:06:24 -0700775 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix,
776 &croppedRect, &croppedLocalRect)) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700777 return;
778 }
779
Robert Phillips72152832017-01-25 17:31:35 -0500780 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -0700781
csmartdaltone0d36292016-07-29 08:14:20 -0700782 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport()) {
Robert Phillipsf2361d22016-10-25 14:20:06 -0400783 InstancedRendering* ir = this->getOpList()->instancedRendering();
Brian Salomon54d212e2017-03-21 14:22:38 -0400784 std::unique_ptr<GrDrawOp> op(ir->recordRect(croppedRect, viewMatrix, std::move(paint),
785 croppedLocalRect, aa, fInstancedPipelineInfo));
Brian Salomon42521e82016-12-07 16:44:58 -0500786 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -0400787 this->addDrawOp(clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -0700788 return;
789 }
790 }
791
Brian Salomon54d212e2017-03-21 14:22:38 -0400792 GrAAType aaType = this->decideAAType(aa);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500793 if (GrAAType::kCoverage != aaType) {
Brian Salomon82f44312017-01-11 13:42:54 -0500794 this->drawNonAAFilledRect(clip, std::move(paint), viewMatrix, croppedRect,
795 &croppedLocalRect, nullptr, nullptr, aaType);
csmartdaltonfc49d562016-07-26 17:05:47 -0700796 return;
joshualitt04194f32016-01-13 10:08:27 -0800797 }
bsalomonbb243832016-07-22 07:10:19 -0700798
csmartdaltonfc49d562016-07-26 17:05:47 -0700799 if (view_matrix_ok_for_aa_fill_rect(viewMatrix)) {
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400800 std::unique_ptr<GrLegacyMeshDrawOp> op = GrAAFillRectOp::MakeWithLocalRect(
Brian Salomonf8334782017-01-03 09:42:58 -0500801 paint.getColor(), viewMatrix, croppedRect, croppedLocalRect);
Brian Salomon82f44312017-01-11 13:42:54 -0500802 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
Brian Salomone14bd802017-04-04 15:13:25 -0400803 this->addLegacyMeshDrawOp(std::move(pipelineBuilder), clip, std::move(op));
csmartdaltonfc49d562016-07-26 17:05:47 -0700804 return;
805 }
806
807 SkMatrix viewAndUnLocalMatrix;
808 if (!viewAndUnLocalMatrix.setRectToRect(localRect, rectToDraw, SkMatrix::kFill_ScaleToFit)) {
809 SkDebugf("fillRectToRect called with empty local matrix.\n");
810 return;
811 }
812 viewAndUnLocalMatrix.postConcat(viewMatrix);
813
814 SkPath path;
815 path.setIsVolatile(true);
816 path.addRect(localRect);
Brian Salomon82f44312017-01-11 13:42:54 -0500817 this->internalDrawPath(clip, std::move(paint), aa, viewAndUnLocalMatrix, path, GrStyle());
joshualittb6b513b2015-08-21 10:25:18 -0700818}
819
Brian Osman11052242016-10-27 14:47:55 -0400820void GrRenderTargetContext::fillRectWithLocalMatrix(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500821 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500822 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400823 const SkMatrix& viewMatrix,
824 const SkRect& rectToDraw,
825 const SkMatrix& localMatrix) {
joshualitt1de610a2016-01-06 08:26:09 -0800826 ASSERT_SINGLE_OWNER
joshualittb6b513b2015-08-21 10:25:18 -0700827 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700828 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400829 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::fillRectWithLocalMatrix");
joshualittb6b513b2015-08-21 10:25:18 -0700830
csmartdalton97f6cd52016-07-13 13:37:08 -0700831 SkRect croppedRect = rectToDraw;
robertphillips13a7eee2016-08-31 15:06:24 -0700832 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix, &croppedRect)) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700833 return;
834 }
835
Robert Phillips72152832017-01-25 17:31:35 -0500836 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -0700837
csmartdaltone0d36292016-07-29 08:14:20 -0700838 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport()) {
Robert Phillipsf2361d22016-10-25 14:20:06 -0400839 InstancedRendering* ir = this->getOpList()->instancedRendering();
Brian Salomon54d212e2017-03-21 14:22:38 -0400840 std::unique_ptr<GrDrawOp> op(ir->recordRect(croppedRect, viewMatrix, std::move(paint),
841 localMatrix, aa, fInstancedPipelineInfo));
Brian Salomon42521e82016-12-07 16:44:58 -0500842 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -0400843 this->addDrawOp(clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -0700844 return;
845 }
846 }
847
Brian Salomon54d212e2017-03-21 14:22:38 -0400848 GrAAType aaType = this->decideAAType(aa);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500849 if (GrAAType::kCoverage != aaType) {
Brian Salomon82f44312017-01-11 13:42:54 -0500850 this->drawNonAAFilledRect(clip, std::move(paint), viewMatrix, croppedRect, nullptr,
851 &localMatrix, nullptr, aaType);
csmartdaltonfc49d562016-07-26 17:05:47 -0700852 return;
bsalomonc55271f2015-11-09 11:55:57 -0800853 }
robertphillips4bc31812016-03-01 12:22:49 -0800854
csmartdaltonfc49d562016-07-26 17:05:47 -0700855 if (view_matrix_ok_for_aa_fill_rect(viewMatrix)) {
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400856 std::unique_ptr<GrLegacyMeshDrawOp> op =
Brian Salomon6a639042016-12-14 11:08:17 -0500857 GrAAFillRectOp::Make(paint.getColor(), viewMatrix, localMatrix, croppedRect);
Brian Salomon82f44312017-01-11 13:42:54 -0500858 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
Brian Salomone14bd802017-04-04 15:13:25 -0400859 this->addLegacyMeshDrawOp(std::move(pipelineBuilder), clip, std::move(op));
csmartdaltonfc49d562016-07-26 17:05:47 -0700860 return;
861 }
862
863 SkMatrix viewAndUnLocalMatrix;
864 if (!localMatrix.invert(&viewAndUnLocalMatrix)) {
865 SkDebugf("fillRectWithLocalMatrix called with degenerate local matrix.\n");
866 return;
867 }
868 viewAndUnLocalMatrix.postConcat(viewMatrix);
869
870 SkPath path;
871 path.setIsVolatile(true);
872 path.addRect(rectToDraw);
873 path.transform(localMatrix);
Brian Salomon82f44312017-01-11 13:42:54 -0500874 this->internalDrawPath(clip, std::move(paint), aa, viewAndUnLocalMatrix, path, GrStyle());
robertphillipsea461502015-05-26 11:38:03 -0700875}
876
Brian Osman11052242016-10-27 14:47:55 -0400877void GrRenderTargetContext::drawVertices(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500878 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400879 const SkMatrix& viewMatrix,
880 GrPrimitiveType primitiveType,
881 int vertexCount,
882 const SkPoint positions[],
883 const SkPoint texCoords[],
Brian Salomon3de0aee2017-01-29 09:34:17 -0500884 const uint32_t colors[],
Brian Osman11052242016-10-27 14:47:55 -0400885 const uint16_t indices[],
Brian Salomon3de0aee2017-01-29 09:34:17 -0500886 int indexCount,
887 ColorArrayType colorArrayType) {
joshualitt1de610a2016-01-06 08:26:09 -0800888 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700889 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700890 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400891 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawVertices");
robertphillipsea461502015-05-26 11:38:03 -0700892
Robert Phillips72152832017-01-25 17:31:35 -0500893 AutoCheckFlush acf(this->drawingManager());
robertphillips2e1e51f2015-10-15 08:01:48 -0700894
robertphillipsea461502015-05-26 11:38:03 -0700895 // TODO clients should give us bounds
896 SkRect bounds;
897 if (!bounds.setBoundsCheck(positions, vertexCount)) {
898 SkDebugf("drawVertices call empty bounds\n");
899 return;
900 }
901
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400902 std::unique_ptr<GrLegacyMeshDrawOp> op = GrDrawVerticesOp::Make(
Brian Salomon3de0aee2017-01-29 09:34:17 -0500903 paint.getColor(), primitiveType, viewMatrix, positions, vertexCount, indices,
904 indexCount, colors, texCoords, bounds, colorArrayType);
Brian Salomon199fb872017-02-06 09:41:10 -0500905 if (!op) {
906 return;
907 }
908 GrPipelineBuilder pipelineBuilder(std::move(paint), GrAAType::kNone);
Brian Salomone14bd802017-04-04 15:13:25 -0400909 this->addLegacyMeshDrawOp(std::move(pipelineBuilder), clip, std::move(op));
Brian Salomon199fb872017-02-06 09:41:10 -0500910}
robertphillipsea461502015-05-26 11:38:03 -0700911
Brian Salomon199fb872017-02-06 09:41:10 -0500912void GrRenderTargetContext::drawVertices(const GrClip& clip,
913 GrPaint&& paint,
914 const SkMatrix& viewMatrix,
Mike Reed5fa66452017-03-16 09:06:34 -0400915 sk_sp<SkVertices> vertices) {
Brian Salomon199fb872017-02-06 09:41:10 -0500916 ASSERT_SINGLE_OWNER
917 RETURN_IF_ABANDONED
918 SkDEBUGCODE(this->validate();)
919 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawVertices");
920
921 AutoCheckFlush acf(this->drawingManager());
922
923 SkASSERT(vertices);
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400924 std::unique_ptr<GrLegacyMeshDrawOp> op =
Mike Reed5fa66452017-03-16 09:06:34 -0400925 GrDrawVerticesOp::Make(paint.getColor(), std::move(vertices), viewMatrix);
Brian Salomon199fb872017-02-06 09:41:10 -0500926 if (!op) {
927 return;
928 }
Brian Salomon82f44312017-01-11 13:42:54 -0500929 GrPipelineBuilder pipelineBuilder(std::move(paint), GrAAType::kNone);
Brian Salomone14bd802017-04-04 15:13:25 -0400930 this->addLegacyMeshDrawOp(std::move(pipelineBuilder), clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -0700931}
932
933///////////////////////////////////////////////////////////////////////////////
934
Brian Osman11052242016-10-27 14:47:55 -0400935void GrRenderTargetContext::drawAtlas(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500936 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400937 const SkMatrix& viewMatrix,
938 int spriteCount,
939 const SkRSXform xform[],
940 const SkRect texRect[],
941 const SkColor colors[]) {
joshualitt1de610a2016-01-06 08:26:09 -0800942 ASSERT_SINGLE_OWNER
jvanverth31ff7622015-08-07 10:09:28 -0700943 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700944 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400945 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawAtlas");
robertphillips2e1e51f2015-10-15 08:01:48 -0700946
Robert Phillips72152832017-01-25 17:31:35 -0500947 AutoCheckFlush acf(this->drawingManager());
halcanary9d524f22016-03-29 09:03:52 -0700948
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400949 std::unique_ptr<GrLegacyMeshDrawOp> op =
Brian Salomonfc527d22016-12-14 21:07:01 -0500950 GrDrawAtlasOp::Make(paint.getColor(), viewMatrix, spriteCount, xform, texRect, colors);
Brian Salomon82f44312017-01-11 13:42:54 -0500951 GrPipelineBuilder pipelineBuilder(std::move(paint), GrAAType::kNone);
Brian Salomone14bd802017-04-04 15:13:25 -0400952 this->addLegacyMeshDrawOp(std::move(pipelineBuilder), clip, std::move(op));
jvanverth31ff7622015-08-07 10:09:28 -0700953}
954
955///////////////////////////////////////////////////////////////////////////////
956
Brian Osman11052242016-10-27 14:47:55 -0400957void GrRenderTargetContext::drawRRect(const GrClip& origClip,
Brian Salomon82f44312017-01-11 13:42:54 -0500958 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500959 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400960 const SkMatrix& viewMatrix,
961 const SkRRect& rrect,
962 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -0800963 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700964 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700965 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400966 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawRRect");
robertphillipsea461502015-05-26 11:38:03 -0700967 if (rrect.isEmpty()) {
968 return;
969 }
970
bsalomon7f0d9f32016-08-15 14:49:10 -0700971 GrNoClip noclip;
972 const GrClip* clip = &origClip;
973#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
974 // The Android framework frequently clips rrects to themselves where the clip is non-aa and the
Brian Salomon42521e82016-12-07 16:44:58 -0500975 // draw is aa. Since our lower level clip code works from op bounds, which are SkRects, it
bsalomon7f0d9f32016-08-15 14:49:10 -0700976 // doesn't detect that the clip can be ignored (modulo antialiasing). The following test
977 // attempts to mitigate the stencil clip cost but will only help when the entire clip stack
978 // can be ignored. We'd prefer to fix this in the framework by removing the clips calls.
979 SkRRect devRRect;
980 if (rrect.transform(viewMatrix, &devRRect) && clip->quickContains(devRRect)) {
981 clip = &noclip;
982 }
983#endif
bsalomon6663acf2016-05-10 09:14:17 -0700984 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
ksakamotoec7f2ac2016-07-05 03:54:53 -0700985
Robert Phillips72152832017-01-25 17:31:35 -0500986 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -0700987 const SkStrokeRec stroke = style.strokeRec();
csmartdaltona7f29642016-07-07 08:49:11 -0700988
csmartdaltone0d36292016-07-29 08:14:20 -0700989 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport() &&
990 stroke.isFillStyle()) {
Robert Phillipsf2361d22016-10-25 14:20:06 -0400991 InstancedRendering* ir = this->getOpList()->instancedRendering();
Brian Salomon54d212e2017-03-21 14:22:38 -0400992 std::unique_ptr<GrDrawOp> op(
993 ir->recordRRect(rrect, viewMatrix, std::move(paint), aa, fInstancedPipelineInfo));
Brian Salomon42521e82016-12-07 16:44:58 -0500994 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -0400995 this->addDrawOp(*clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -0700996 return;
997 }
998 }
999
Brian Salomon54d212e2017-03-21 14:22:38 -04001000 GrAAType aaType = this->decideAAType(aa);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001001 if (GrAAType::kCoverage == aaType) {
Brian Salomon94efbf52016-11-29 13:43:05 -05001002 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomond3ccb0a2017-04-03 10:38:00 -04001003 std::unique_ptr<GrLegacyMeshDrawOp> op =
Brian Salomon649a3412017-03-09 13:50:43 -05001004 GrOvalOpFactory::MakeRRectOp(paint.getColor(),
1005 paint.usesDistanceVectorField(),
1006 viewMatrix,
1007 rrect,
1008 stroke,
1009 shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -05001010 if (op) {
Brian Salomon82f44312017-01-11 13:42:54 -05001011 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
Brian Salomone14bd802017-04-04 15:13:25 -04001012 this->addLegacyMeshDrawOp(std::move(pipelineBuilder), *clip, std::move(op));
robertphillipsb56f9272016-02-25 11:03:52 -08001013 return;
1014 }
robertphillipsea461502015-05-26 11:38:03 -07001015 }
robertphillipsb56f9272016-02-25 11:03:52 -08001016
1017 SkPath path;
1018 path.setIsVolatile(true);
1019 path.addRRect(rrect);
Brian Salomon82f44312017-01-11 13:42:54 -05001020 this->internalDrawPath(*clip, std::move(paint), aa, viewMatrix, path, style);
robertphillipsea461502015-05-26 11:38:03 -07001021}
1022
Jim Van Verthc5903412016-11-17 15:27:09 -05001023///////////////////////////////////////////////////////////////////////////////
1024
1025void GrRenderTargetContext::drawShadowRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001026 GrPaint&& paint,
Jim Van Verthc5903412016-11-17 15:27:09 -05001027 const SkMatrix& viewMatrix,
1028 const SkRRect& rrect,
1029 SkScalar blurRadius,
1030 const GrStyle& style) {
1031 ASSERT_SINGLE_OWNER
1032 RETURN_IF_ABANDONED
1033 SkDEBUGCODE(this->validate();)
1034 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawShadowRRect");
1035 if (rrect.isEmpty()) {
1036 return;
1037 }
1038
1039 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
1040
Robert Phillips72152832017-01-25 17:31:35 -05001041 AutoCheckFlush acf(this->drawingManager());
Jim Van Verthc5903412016-11-17 15:27:09 -05001042 const SkStrokeRec stroke = style.strokeRec();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001043 // TODO: add instancing support?
Jim Van Verthc5903412016-11-17 15:27:09 -05001044
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001045 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomond3ccb0a2017-04-03 10:38:00 -04001046 std::unique_ptr<GrLegacyMeshDrawOp> op = GrShadowRRectOp::Make(
1047 paint.getColor(), viewMatrix, rrect, blurRadius, stroke, shaderCaps);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001048 if (op) {
Brian Salomon82f44312017-01-11 13:42:54 -05001049 GrPipelineBuilder pipelineBuilder(std::move(paint), GrAAType::kNone);
Brian Salomone14bd802017-04-04 15:13:25 -04001050 this->addLegacyMeshDrawOp(std::move(pipelineBuilder), clip, std::move(op));
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001051 return;
Jim Van Verthc5903412016-11-17 15:27:09 -05001052 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001053}
1054
1055///////////////////////////////////////////////////////////////////////////////
1056
Brian Osman11052242016-10-27 14:47:55 -04001057bool GrRenderTargetContext::drawFilledDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001058 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001059 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001060 const SkMatrix& viewMatrix,
1061 const SkRRect& origOuter,
1062 const SkRRect& origInner) {
robertphillips00095892016-02-29 13:50:40 -08001063 SkASSERT(!origInner.isEmpty());
1064 SkASSERT(!origOuter.isEmpty());
1065
csmartdaltone0d36292016-07-29 08:14:20 -07001066 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport()) {
Robert Phillipsf2361d22016-10-25 14:20:06 -04001067 InstancedRendering* ir = this->getOpList()->instancedRendering();
Brian Salomon54d212e2017-03-21 14:22:38 -04001068 std::unique_ptr<GrDrawOp> op(ir->recordDRRect(
1069 origOuter, origInner, viewMatrix, std::move(paint), aa, fInstancedPipelineInfo));
Brian Salomon42521e82016-12-07 16:44:58 -05001070 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -04001071 this->addDrawOp(clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -07001072 return true;
1073 }
1074 }
1075
Brian Salomon54d212e2017-03-21 14:22:38 -04001076 GrAAType aaType = this->decideAAType(aa);
robertphillips00095892016-02-29 13:50:40 -08001077
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001078 GrPrimitiveEdgeType innerEdgeType, outerEdgeType;
1079 if (GrAAType::kCoverage == aaType) {
1080 innerEdgeType = kInverseFillAA_GrProcessorEdgeType;
1081 outerEdgeType = kFillAA_GrProcessorEdgeType;
1082 } else {
1083 innerEdgeType = kInverseFillBW_GrProcessorEdgeType;
1084 outerEdgeType = kFillBW_GrProcessorEdgeType;
1085 }
robertphillips00095892016-02-29 13:50:40 -08001086
1087 SkTCopyOnFirstWrite<SkRRect> inner(origInner), outer(origOuter);
1088 SkMatrix inverseVM;
1089 if (!viewMatrix.isIdentity()) {
1090 if (!origInner.transform(viewMatrix, inner.writable())) {
1091 return false;
1092 }
1093 if (!origOuter.transform(viewMatrix, outer.writable())) {
1094 return false;
1095 }
1096 if (!viewMatrix.invert(&inverseVM)) {
1097 return false;
1098 }
1099 } else {
1100 inverseVM.reset();
halcanary9d524f22016-03-29 09:03:52 -07001101 }
robertphillips00095892016-02-29 13:50:40 -08001102
robertphillips00095892016-02-29 13:50:40 -08001103 // TODO these need to be a geometry processors
bungeman06ca8ec2016-06-09 08:01:03 -07001104 sk_sp<GrFragmentProcessor> innerEffect(GrRRectEffect::Make(innerEdgeType, *inner));
robertphillips00095892016-02-29 13:50:40 -08001105 if (!innerEffect) {
1106 return false;
1107 }
1108
bungeman06ca8ec2016-06-09 08:01:03 -07001109 sk_sp<GrFragmentProcessor> outerEffect(GrRRectEffect::Make(outerEdgeType, *outer));
robertphillips00095892016-02-29 13:50:40 -08001110 if (!outerEffect) {
1111 return false;
1112 }
1113
Brian Salomon82f44312017-01-11 13:42:54 -05001114 paint.addCoverageFragmentProcessor(std::move(innerEffect));
1115 paint.addCoverageFragmentProcessor(std::move(outerEffect));
robertphillips00095892016-02-29 13:50:40 -08001116
1117 SkRect bounds = outer->getBounds();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001118 if (GrAAType::kCoverage == aaType) {
robertphillips00095892016-02-29 13:50:40 -08001119 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1120 }
halcanary9d524f22016-03-29 09:03:52 -07001121
Brian Salomon82f44312017-01-11 13:42:54 -05001122 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), bounds,
1123 inverseVM);
robertphillips00095892016-02-29 13:50:40 -08001124 return true;
1125}
1126
Brian Osman11052242016-10-27 14:47:55 -04001127void GrRenderTargetContext::drawDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001128 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001129 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001130 const SkMatrix& viewMatrix,
1131 const SkRRect& outer,
1132 const SkRRect& inner) {
robertphillips00095892016-02-29 13:50:40 -08001133 ASSERT_SINGLE_OWNER
1134 RETURN_IF_ABANDONED
1135 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -04001136 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawDRRect");
robertphillips00095892016-02-29 13:50:40 -08001137
1138 SkASSERT(!outer.isEmpty());
1139 SkASSERT(!inner.isEmpty());
1140
Robert Phillips72152832017-01-25 17:31:35 -05001141 AutoCheckFlush acf(this->drawingManager());
robertphillips00095892016-02-29 13:50:40 -08001142
Brian Salomon82f44312017-01-11 13:42:54 -05001143 if (this->drawFilledDRRect(clip, std::move(paint), aa, viewMatrix, outer, inner)) {
robertphillips00095892016-02-29 13:50:40 -08001144 return;
1145 }
1146
1147 SkPath path;
1148 path.setIsVolatile(true);
1149 path.addRRect(inner);
1150 path.addRRect(outer);
1151 path.setFillType(SkPath::kEvenOdd_FillType);
1152
Brian Salomon82f44312017-01-11 13:42:54 -05001153 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, GrStyle::SimpleFill());
robertphillips00095892016-02-29 13:50:40 -08001154}
1155
robertphillipsea461502015-05-26 11:38:03 -07001156///////////////////////////////////////////////////////////////////////////////
1157
msarettcc319b92016-08-25 18:07:18 -07001158static inline bool is_int(float x) {
1159 return x == (float) sk_float_round2int(x);
1160}
1161
Brian Osman11052242016-10-27 14:47:55 -04001162void GrRenderTargetContext::drawRegion(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001163 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001164 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001165 const SkMatrix& viewMatrix,
1166 const SkRegion& region,
1167 const GrStyle& style) {
msarettcc319b92016-08-25 18:07:18 -07001168 ASSERT_SINGLE_OWNER
1169 RETURN_IF_ABANDONED
1170 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -04001171 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawRegion");
msarettcc319b92016-08-25 18:07:18 -07001172
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001173 if (GrAA::kYes == aa) {
Brian Salomonfc527d22016-12-14 21:07:01 -05001174 // GrRegionOp performs no antialiasing but is much faster, so here we check the matrix
Brian Salomonc57c7c92016-12-06 14:47:34 -05001175 // to see whether aa is really required.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001176 if (!SkToBool(viewMatrix.getType() & ~(SkMatrix::kTranslate_Mask)) &&
1177 is_int(viewMatrix.getTranslateX()) &&
1178 is_int(viewMatrix.getTranslateY())) {
1179 aa = GrAA::kNo;
1180 }
Brian Salomonc57c7c92016-12-06 14:47:34 -05001181 }
msarettcc319b92016-08-25 18:07:18 -07001182 bool complexStyle = !style.isSimpleFill();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001183 if (complexStyle || GrAA::kYes == aa) {
msarettcc319b92016-08-25 18:07:18 -07001184 SkPath path;
1185 region.getBoundaryPath(&path);
Brian Salomon82f44312017-01-11 13:42:54 -05001186 return this->drawPath(clip, std::move(paint), aa, viewMatrix, path, style);
msarettcc319b92016-08-25 18:07:18 -07001187 }
1188
Brian Salomond3ccb0a2017-04-03 10:38:00 -04001189 std::unique_ptr<GrLegacyMeshDrawOp> op = GrRegionOp::Make(paint.getColor(), viewMatrix, region);
Brian Salomon82f44312017-01-11 13:42:54 -05001190 GrPipelineBuilder pipelineBuilder(std::move(paint), GrAAType::kNone);
Brian Salomone14bd802017-04-04 15:13:25 -04001191 this->addLegacyMeshDrawOp(std::move(pipelineBuilder), clip, std::move(op));
msarettcc319b92016-08-25 18:07:18 -07001192}
1193
Brian Osman11052242016-10-27 14:47:55 -04001194void GrRenderTargetContext::drawOval(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001195 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001196 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001197 const SkMatrix& viewMatrix,
1198 const SkRect& oval,
1199 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001200 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001201 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001202 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -04001203 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawOval");
robertphillips2e1e51f2015-10-15 08:01:48 -07001204
robertphillipsea461502015-05-26 11:38:03 -07001205 if (oval.isEmpty()) {
1206 return;
1207 }
1208
bsalomon6663acf2016-05-10 09:14:17 -07001209 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
robertphillipsea461502015-05-26 11:38:03 -07001210
Robert Phillips72152832017-01-25 17:31:35 -05001211 AutoCheckFlush acf(this->drawingManager());
bsalomon6663acf2016-05-10 09:14:17 -07001212 const SkStrokeRec& stroke = style.strokeRec();
csmartdaltona7f29642016-07-07 08:49:11 -07001213
csmartdaltone0d36292016-07-29 08:14:20 -07001214 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport() &&
1215 stroke.isFillStyle()) {
Robert Phillipsf2361d22016-10-25 14:20:06 -04001216 InstancedRendering* ir = this->getOpList()->instancedRendering();
Brian Salomon54d212e2017-03-21 14:22:38 -04001217 std::unique_ptr<GrDrawOp> op(
1218 ir->recordOval(oval, viewMatrix, std::move(paint), aa, fInstancedPipelineInfo));
Brian Salomon42521e82016-12-07 16:44:58 -05001219 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -04001220 this->addDrawOp(clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -07001221 return;
1222 }
1223 }
1224
Brian Salomon54d212e2017-03-21 14:22:38 -04001225 GrAAType aaType = this->decideAAType(aa);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001226 if (GrAAType::kCoverage == aaType) {
Brian Salomon94efbf52016-11-29 13:43:05 -05001227 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomond3ccb0a2017-04-03 10:38:00 -04001228 std::unique_ptr<GrLegacyMeshDrawOp> op =
Brian Salomon289e3d82016-12-14 15:52:56 -05001229 GrOvalOpFactory::MakeOvalOp(paint.getColor(), viewMatrix, oval, stroke, shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -05001230 if (op) {
Brian Salomon82f44312017-01-11 13:42:54 -05001231 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
Brian Salomone14bd802017-04-04 15:13:25 -04001232 this->addLegacyMeshDrawOp(std::move(pipelineBuilder), clip, std::move(op));
robertphillipsb56f9272016-02-25 11:03:52 -08001233 return;
1234 }
robertphillipsea461502015-05-26 11:38:03 -07001235 }
robertphillipsb56f9272016-02-25 11:03:52 -08001236
1237 SkPath path;
1238 path.setIsVolatile(true);
1239 path.addOval(oval);
Brian Salomon82f44312017-01-11 13:42:54 -05001240 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, style);
robertphillipsea461502015-05-26 11:38:03 -07001241}
1242
Brian Osman11052242016-10-27 14:47:55 -04001243void GrRenderTargetContext::drawArc(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001244 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001245 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001246 const SkMatrix& viewMatrix,
1247 const SkRect& oval,
1248 SkScalar startAngle,
1249 SkScalar sweepAngle,
1250 bool useCenter,
1251 const GrStyle& style) {
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001252 ASSERT_SINGLE_OWNER
1253 RETURN_IF_ABANDONED
1254 SkDEBUGCODE(this->validate();)
1255 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawArc");
1256
1257 AutoCheckFlush acf(this->drawingManager());
1258
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001259 GrAAType aaType = this->decideAAType(aa);
1260 if (GrAAType::kCoverage == aaType) {
Brian Salomon94efbf52016-11-29 13:43:05 -05001261 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomond3ccb0a2017-04-03 10:38:00 -04001262 std::unique_ptr<GrLegacyMeshDrawOp> op = GrOvalOpFactory::MakeArcOp(paint.getColor(),
1263 viewMatrix,
1264 oval,
1265 startAngle,
1266 sweepAngle,
1267 useCenter,
1268 style,
1269 shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -05001270 if (op) {
Brian Salomon82f44312017-01-11 13:42:54 -05001271 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
Brian Salomone14bd802017-04-04 15:13:25 -04001272 this->addLegacyMeshDrawOp(std::move(pipelineBuilder), clip, std::move(op));
bsalomon4f3a0ca2016-08-22 13:14:26 -07001273 return;
1274 }
1275 }
1276 SkPath path;
bsalomon21af9ca2016-08-25 12:29:23 -07001277 SkPathPriv::CreateDrawArcPath(&path, oval, startAngle, sweepAngle, useCenter,
1278 style.isSimpleFill());
Brian Salomon82f44312017-01-11 13:42:54 -05001279 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, style);
bsalomon4f3a0ca2016-08-22 13:14:26 -07001280}
1281
Brian Osman11052242016-10-27 14:47:55 -04001282void GrRenderTargetContext::drawImageLattice(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001283 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001284 const SkMatrix& viewMatrix,
1285 int imageWidth,
1286 int imageHeight,
1287 std::unique_ptr<SkLatticeIter> iter,
1288 const SkRect& dst) {
joshualitt1de610a2016-01-06 08:26:09 -08001289 ASSERT_SINGLE_OWNER
joshualitt33a5fce2015-11-18 13:28:51 -08001290 RETURN_IF_ABANDONED
1291 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -04001292 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawImageLattice");
joshualitt33a5fce2015-11-18 13:28:51 -08001293
Robert Phillips72152832017-01-25 17:31:35 -05001294 AutoCheckFlush acf(this->drawingManager());
joshualitt33a5fce2015-11-18 13:28:51 -08001295
Brian Salomond3ccb0a2017-04-03 10:38:00 -04001296 std::unique_ptr<GrLegacyMeshDrawOp> op = GrLatticeOp::MakeNonAA(
Brian Salomon649a3412017-03-09 13:50:43 -05001297 paint.getColor(), viewMatrix, imageWidth, imageHeight, std::move(iter), dst);
joshualitt33a5fce2015-11-18 13:28:51 -08001298
Brian Salomon82f44312017-01-11 13:42:54 -05001299 GrPipelineBuilder pipelineBuilder(std::move(paint), GrAAType::kNone);
Brian Salomone14bd802017-04-04 15:13:25 -04001300 this->addLegacyMeshDrawOp(std::move(pipelineBuilder), clip, std::move(op));
joshualitt33a5fce2015-11-18 13:28:51 -08001301}
1302
Brian Osman11052242016-10-27 14:47:55 -04001303void GrRenderTargetContext::prepareForExternalIO() {
robertphillips8c523e02016-07-26 07:41:00 -07001304 ASSERT_SINGLE_OWNER
1305 RETURN_IF_ABANDONED
1306 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -04001307 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::prepareForExternalIO");
robertphillips8c523e02016-07-26 07:41:00 -07001308
Robert Phillips7ee385e2017-03-30 08:02:11 -04001309 this->drawingManager()->prepareSurfaceForExternalIO(fRenderTargetProxy.get());
robertphillips8c523e02016-07-26 07:41:00 -07001310}
joshualitt33a5fce2015-11-18 13:28:51 -08001311
Brian Osman11052242016-10-27 14:47:55 -04001312void GrRenderTargetContext::drawNonAAFilledRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001313 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001314 const SkMatrix& viewMatrix,
1315 const SkRect& rect,
1316 const SkRect* localRect,
1317 const SkMatrix* localMatrix,
1318 const GrUserStencilSettings* ss,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001319 GrAAType hwOrNoneAAType) {
1320 SkASSERT(GrAAType::kCoverage != hwOrNoneAAType);
1321 SkASSERT(hwOrNoneAAType == GrAAType::kNone || this->isStencilBufferMultisampled());
Brian Salomond3ccb0a2017-04-03 10:38:00 -04001322 std::unique_ptr<GrLegacyMeshDrawOp> op = GrRectOpFactory::MakeNonAAFill(
1323 paint.getColor(), viewMatrix, rect, localRect, localMatrix);
Brian Salomon82f44312017-01-11 13:42:54 -05001324 GrPipelineBuilder pipelineBuilder(std::move(paint), hwOrNoneAAType);
robertphillips44302392016-07-08 14:43:03 -07001325 if (ss) {
1326 pipelineBuilder.setUserStencil(ss);
1327 }
Brian Salomone14bd802017-04-04 15:13:25 -04001328 this->addLegacyMeshDrawOp(std::move(pipelineBuilder), clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -07001329}
1330
robertphillipsea461502015-05-26 11:38:03 -07001331// Can 'path' be drawn as a pair of filled nested rectangles?
bsalomon6663acf2016-05-10 09:14:17 -07001332static bool fills_as_nested_rects(const SkMatrix& viewMatrix, const SkPath& path, SkRect rects[2]) {
robertphillipsea461502015-05-26 11:38:03 -07001333
1334 if (path.isInverseFillType()) {
1335 return false;
1336 }
1337
1338 // TODO: this restriction could be lifted if we were willing to apply
1339 // the matrix to all the points individually rather than just to the rect
robertphillips0e7029e2015-11-30 05:45:06 -08001340 if (!viewMatrix.rectStaysRect()) {
robertphillipsea461502015-05-26 11:38:03 -07001341 return false;
1342 }
1343
1344 SkPath::Direction dirs[2];
1345 if (!path.isNestedFillRects(rects, dirs)) {
1346 return false;
1347 }
1348
1349 if (SkPath::kWinding_FillType == path.getFillType() && dirs[0] == dirs[1]) {
1350 // The two rects need to be wound opposite to each other
1351 return false;
1352 }
1353
1354 // Right now, nested rects where the margin is not the same width
1355 // all around do not render correctly
1356 const SkScalar* outer = rects[0].asScalars();
1357 const SkScalar* inner = rects[1].asScalars();
1358
1359 bool allEq = true;
1360
1361 SkScalar margin = SkScalarAbs(outer[0] - inner[0]);
1362 bool allGoE1 = margin >= SK_Scalar1;
1363
1364 for (int i = 1; i < 4; ++i) {
1365 SkScalar temp = SkScalarAbs(outer[i] - inner[i]);
1366 if (temp < SK_Scalar1) {
1367 allGoE1 = false;
1368 }
1369 if (!SkScalarNearlyEqual(margin, temp)) {
1370 allEq = false;
1371 }
1372 }
1373
1374 return allEq || allGoE1;
1375}
1376
Brian Osman11052242016-10-27 14:47:55 -04001377void GrRenderTargetContext::drawPath(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001378 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001379 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001380 const SkMatrix& viewMatrix,
1381 const SkPath& path,
1382 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001383 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001384 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001385 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -04001386 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawPath");
robertphillips2e1e51f2015-10-15 08:01:48 -07001387
robertphillipsea461502015-05-26 11:38:03 -07001388 if (path.isEmpty()) {
1389 if (path.isInverseFillType()) {
Brian Salomon82f44312017-01-11 13:42:54 -05001390 this->drawPaint(clip, std::move(paint), viewMatrix);
robertphillipsea461502015-05-26 11:38:03 -07001391 }
1392 return;
1393 }
1394
Robert Phillips72152832017-01-25 17:31:35 -05001395 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -07001396
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001397 GrAAType aaType = this->decideAAType(aa);
1398 if (GrAAType::kCoverage == aaType && !style.pathEffect()) {
bsalomon6663acf2016-05-10 09:14:17 -07001399 if (style.isSimpleFill() && !path.isConvex()) {
robertphillipsea461502015-05-26 11:38:03 -07001400 // Concave AA paths are expensive - try to avoid them for special cases
1401 SkRect rects[2];
1402
bsalomon6663acf2016-05-10 09:14:17 -07001403 if (fills_as_nested_rects(viewMatrix, path, rects)) {
Brian Salomond3ccb0a2017-04-03 10:38:00 -04001404 std::unique_ptr<GrLegacyMeshDrawOp> op =
Brian Salomon6a639042016-12-14 11:08:17 -05001405 GrRectOpFactory::MakeAAFillNestedRects(paint.getColor(), viewMatrix, rects);
Brian Salomon21aa35f2016-12-09 16:01:53 -05001406 if (op) {
Brian Salomon82f44312017-01-11 13:42:54 -05001407 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
Brian Salomone14bd802017-04-04 15:13:25 -04001408 this->addLegacyMeshDrawOp(std::move(pipelineBuilder), clip, std::move(op));
bsalomon40ef4852016-05-02 13:22:13 -07001409 }
robertphillipsea461502015-05-26 11:38:03 -07001410 return;
1411 }
1412 }
1413 SkRect ovalRect;
1414 bool isOval = path.isOval(&ovalRect);
1415
1416 if (isOval && !path.isInverseFillType()) {
Brian Salomon94efbf52016-11-29 13:43:05 -05001417 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomond3ccb0a2017-04-03 10:38:00 -04001418 std::unique_ptr<GrLegacyMeshDrawOp> op = GrOvalOpFactory::MakeOvalOp(
Brian Salomon289e3d82016-12-14 15:52:56 -05001419 paint.getColor(), viewMatrix, ovalRect, style.strokeRec(), shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -05001420 if (op) {
Brian Salomon82f44312017-01-11 13:42:54 -05001421 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
Brian Salomone14bd802017-04-04 15:13:25 -04001422 this->addLegacyMeshDrawOp(std::move(pipelineBuilder), clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -07001423 return;
1424 }
1425 }
1426 }
robertphillips4bc31812016-03-01 12:22:49 -08001427
1428 // Note that internalDrawPath may sw-rasterize the path into a scratch texture.
1429 // Scratch textures can be recycled after they are returned to the texture
1430 // cache. This presents a potential hazard for buffered drawing. However,
1431 // the writePixels that uploads to the scratch will perform a flush so we're
1432 // OK.
Brian Salomon82f44312017-01-11 13:42:54 -05001433 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, style);
robertphillipsea461502015-05-26 11:38:03 -07001434}
1435
Brian Osman11052242016-10-27 14:47:55 -04001436bool GrRenderTargetContextPriv::drawAndStencilPath(const GrClip& clip,
1437 const GrUserStencilSettings* ss,
1438 SkRegion::Op op,
1439 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001440 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001441 const SkMatrix& viewMatrix,
1442 const SkPath& path) {
robertphillips391395d2016-03-02 09:26:36 -08001443 ASSERT_SINGLE_OWNER_PRIV
1444 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -04001445 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001446 GR_AUDIT_TRAIL_AUTO_FRAME(fRenderTargetContext->fAuditTrail,
1447 "GrRenderTargetContextPriv::drawAndStencilPath");
robertphillips391395d2016-03-02 09:26:36 -08001448
1449 if (path.isEmpty() && path.isInverseFillType()) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001450 this->drawAndStencilRect(clip, ss, op, invert, GrAA::kNo, SkMatrix::I(),
Brian Osman11052242016-10-27 14:47:55 -04001451 SkRect::MakeIWH(fRenderTargetContext->width(),
1452 fRenderTargetContext->height()));
robertphillips391395d2016-03-02 09:26:36 -08001453 return true;
1454 }
1455
Robert Phillips72152832017-01-25 17:31:35 -05001456 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -08001457
1458 // An Assumption here is that path renderer would use some form of tweaking
1459 // the src color (either the input alpha or in the frag shader) to implement
1460 // aa. If we have some future driver-mojo path AA that can do the right
1461 // thing WRT to the blend then we'll need some query on the PR.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001462 GrAAType aaType = fRenderTargetContext->decideAAType(aa);
robertphillips976f5f02016-06-03 10:59:20 -07001463 bool hasUserStencilSettings = !ss->isUnused();
robertphillips391395d2016-03-02 09:26:36 -08001464
bsalomon8acedde2016-06-24 10:42:16 -07001465 GrShape shape(path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -08001466 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Osman11052242016-10-27 14:47:55 -04001467 canDrawArgs.fShaderCaps =
Robert Phillips72152832017-01-25 17:31:35 -05001468 fRenderTargetContext->drawingManager()->getContext()->caps()->shaderCaps();
robertphillips391395d2016-03-02 09:26:36 -08001469 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07001470 canDrawArgs.fShape = &shape;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001471 canDrawArgs.fAAType = aaType;
cdalton93a379b2016-05-11 13:58:08 -07001472 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips391395d2016-03-02 09:26:36 -08001473
1474 // Don't allow the SW renderer
Robert Phillips72152832017-01-25 17:31:35 -05001475 GrPathRenderer* pr = fRenderTargetContext->drawingManager()->getPathRenderer(
Brian Salomon36aa1762016-12-10 13:24:02 -05001476 canDrawArgs, false, GrPathRendererChain::DrawType::kStencilAndColor);
robertphillips391395d2016-03-02 09:26:36 -08001477 if (!pr) {
1478 return false;
1479 }
1480
1481 GrPaint paint;
1482 paint.setCoverageSetOpXPFactory(op, invert);
1483
Brian Salomon82f44312017-01-11 13:42:54 -05001484 GrPathRenderer::DrawPathArgs args{
Robert Phillips256c37b2017-03-01 14:32:46 -05001485 fRenderTargetContext->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05001486 std::move(paint),
1487 ss,
1488 fRenderTargetContext,
1489 &clip,
1490 &viewMatrix,
1491 &shape,
1492 aaType,
1493 fRenderTargetContext->isGammaCorrect()};
robertphillips391395d2016-03-02 09:26:36 -08001494 pr->drawPath(args);
1495 return true;
1496}
1497
Brian Osman11052242016-10-27 14:47:55 -04001498SkBudgeted GrRenderTargetContextPriv::isBudgeted() const {
robertphillips714712b2016-08-04 06:20:45 -07001499 ASSERT_SINGLE_OWNER_PRIV
1500
Brian Osman11052242016-10-27 14:47:55 -04001501 if (fRenderTargetContext->wasAbandoned()) {
robertphillips714712b2016-08-04 06:20:45 -07001502 return SkBudgeted::kNo;
1503 }
1504
Brian Osman11052242016-10-27 14:47:55 -04001505 SkDEBUGCODE(fRenderTargetContext->validate();)
robertphillips714712b2016-08-04 06:20:45 -07001506
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001507 return fRenderTargetContext->fRenderTargetProxy->isBudgeted();
robertphillips714712b2016-08-04 06:20:45 -07001508}
1509
Brian Osman11052242016-10-27 14:47:55 -04001510void GrRenderTargetContext::internalDrawPath(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001511 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001512 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001513 const SkMatrix& viewMatrix,
1514 const SkPath& path,
1515 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001516 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001517 RETURN_IF_ABANDONED
bsalomon8acedde2016-06-24 10:42:16 -07001518 SkASSERT(!path.isEmpty());
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001519 GrShape shape;
robertphillipsea461502015-05-26 11:38:03 -07001520
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001521 GrAAType aaType = this->decideAAType(aa, /*allowMixedSamples*/ true);
1522 if (style.isSimpleHairline() && aaType == GrAAType::kMixedSamples) {
1523 // NVPR cannot handle hairlines, so this will would get picked up by a different stencil and
1524 // cover path renderer (i.e. default path renderer). The hairline renderer produces much
1525 // smoother hairlines than MSAA.
1526 aaType = GrAAType::kCoverage;
bsalomon0a0f67e2016-06-28 11:56:42 -07001527 }
robertphillips68737822015-10-29 12:12:21 -07001528 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Robert Phillips72152832017-01-25 17:31:35 -05001529 canDrawArgs.fShaderCaps = this->drawingManager()->getContext()->caps()->shaderCaps();
robertphillips68737822015-10-29 12:12:21 -07001530 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07001531 canDrawArgs.fShape = &shape;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001532 canDrawArgs.fHasUserStencilSettings = false;
robertphillips68737822015-10-29 12:12:21 -07001533
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001534 GrPathRenderer* pr;
Brian Salomon82125e92016-12-10 09:35:48 -05001535 static constexpr GrPathRendererChain::DrawType kType = GrPathRendererChain::DrawType::kColor;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001536 do {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001537 shape = GrShape(path, style);
bsalomon8acedde2016-06-24 10:42:16 -07001538 if (shape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001539 return;
1540 }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001541
1542 canDrawArgs.fAAType = aaType;
1543
1544 // Try a 1st time without applying any of the style to the geometry (and barring sw)
Robert Phillips72152832017-01-25 17:31:35 -05001545 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001546 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
1547
1548 if (!pr && shape.style().pathEffect()) {
1549 // It didn't work above, so try again with the path effect applied.
1550 shape = shape.applyStyle(GrStyle::Apply::kPathEffectOnly, styleScale);
bsalomon8acedde2016-06-24 10:42:16 -07001551 if (shape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001552 return;
1553 }
Robert Phillips72152832017-01-25 17:31:35 -05001554 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
bsalomon6663acf2016-05-10 09:14:17 -07001555 }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001556 if (!pr) {
1557 if (shape.style().applies()) {
1558 shape = shape.applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec, styleScale);
1559 if (shape.isEmpty()) {
1560 return;
1561 }
1562 }
1563 // This time, allow SW renderer
Robert Phillips72152832017-01-25 17:31:35 -05001564 pr = this->drawingManager()->getPathRenderer(canDrawArgs, true, kType);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001565 }
Brian Salomon0abc8b42016-12-13 10:22:54 -05001566 if (!pr && GrAATypeIsHW(aaType)) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001567 // There are exceptional cases where we may wind up falling back to coverage based AA
1568 // when the target is MSAA (e.g. through disabling path renderers via GrContextOptions).
1569 aaType = GrAAType::kCoverage;
1570 } else {
1571 break;
1572 }
1573 } while(true);
robertphillipsea461502015-05-26 11:38:03 -07001574
bsalomon8acedde2016-06-24 10:42:16 -07001575 if (!pr) {
robertphillipsea461502015-05-26 11:38:03 -07001576#ifdef SK_DEBUG
1577 SkDebugf("Unable to find path renderer compatible with path.\n");
1578#endif
1579 return;
1580 }
1581
Robert Phillips256c37b2017-03-01 14:32:46 -05001582 GrPathRenderer::DrawPathArgs args{this->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05001583 std::move(paint),
1584 &GrUserStencilSettings::kUnused,
1585 this,
1586 &clip,
1587 &viewMatrix,
1588 &shape,
1589 aaType,
1590 this->isGammaCorrect()};
bsalomon0aff2fa2015-07-31 06:48:27 -07001591 pr->drawPath(args);
robertphillipsea461502015-05-26 11:38:03 -07001592}
1593
Brian Salomon467921e2017-03-06 16:17:12 -05001594static void op_bounds(SkRect* bounds, const GrOp* op) {
1595 *bounds = op->bounds();
1596 if (op->hasZeroArea()) {
1597 if (op->hasAABloat()) {
1598 bounds->outset(0.5f, 0.5f);
1599 } else {
1600 // We don't know which way the particular GPU will snap lines or points at integer
1601 // coords. So we ensure that the bounds is large enough for either snap.
1602 SkRect before = *bounds;
1603 bounds->roundOut(bounds);
1604 if (bounds->fLeft == before.fLeft) {
1605 bounds->fLeft -= 1;
1606 }
1607 if (bounds->fTop == before.fTop) {
1608 bounds->fTop -= 1;
1609 }
1610 if (bounds->fRight == before.fRight) {
1611 bounds->fRight += 1;
1612 }
1613 if (bounds->fBottom == before.fBottom) {
1614 bounds->fBottom += 1;
1615 }
1616 }
1617 }
1618}
1619
Brian Salomon54d212e2017-03-21 14:22:38 -04001620uint32_t GrRenderTargetContext::addDrawOp(const GrClip& clip, std::unique_ptr<GrDrawOp> op) {
joshualitt1de610a2016-01-06 08:26:09 -08001621 ASSERT_SINGLE_OWNER
Robert Phillipsc0138922017-03-08 11:50:55 -05001622 if (this->drawingManager()->wasAbandoned()) {
1623 return SK_InvalidUniqueID;
1624 }
robertphillips2e1e51f2015-10-15 08:01:48 -07001625 SkDEBUGCODE(this->validate();)
Brian Salomon42521e82016-12-07 16:44:58 -05001626 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::addDrawOp");
robertphillips2d70dcb2015-10-06 07:38:23 -07001627
Brian Salomon467921e2017-03-06 16:17:12 -05001628 // Setup clip
1629 SkRect bounds;
1630 op_bounds(&bounds, op.get());
Brian Salomon97180af2017-03-14 13:42:58 -04001631 GrAppliedClip appliedClip;
Brian Salomon54d212e2017-03-21 14:22:38 -04001632 GrDrawOp::FixedFunctionFlags fixedFunctionFlags = op->fixedFunctionFlags();
1633 if (!clip.apply(fContext, this, fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesHWAA,
1634 fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil, &appliedClip,
1635 &bounds)) {
1636 return SK_InvalidUniqueID;
1637 }
1638
1639 // This forces instantiation of the render target.
1640 GrRenderTarget* rt = this->accessRenderTarget();
1641 if (!rt) {
1642 return SK_InvalidUniqueID;
1643 }
1644
1645 if (fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil ||
1646 appliedClip.hasStencilClip()) {
1647 if (!fContext->resourceProvider()->attachStencilAttachment(rt)) {
1648 SkDebugf("ERROR creating stencil attachment. Draw skipped.\n");
1649 return SK_InvalidUniqueID;
1650 }
1651 }
1652
1653 GrXferProcessor::DstTexture dstTexture;
1654 if (op->xpRequiresDstTexture(*this->caps(), &appliedClip)) {
Robert Phillipsbf25d432017-04-07 10:08:53 -04001655 if (!this->setupDstTexture(fRenderTargetProxy.get(), clip, op->bounds(), &dstTexture)) {
Brian Salomon54d212e2017-03-21 14:22:38 -04001656 return SK_InvalidUniqueID;
1657 }
1658 }
1659
1660 op->setClippedBounds(bounds);
1661 return this->getOpList()->addOp(std::move(op), this, std::move(appliedClip), dstTexture);
1662}
1663
Brian Salomone14bd802017-04-04 15:13:25 -04001664uint32_t GrRenderTargetContext::addLegacyMeshDrawOp(GrPipelineBuilder&& pipelineBuilder,
Brian Salomond3ccb0a2017-04-03 10:38:00 -04001665 const GrClip& clip,
1666 std::unique_ptr<GrLegacyMeshDrawOp> op) {
Brian Salomon54d212e2017-03-21 14:22:38 -04001667 ASSERT_SINGLE_OWNER
1668 if (this->drawingManager()->wasAbandoned()) {
1669 return SK_InvalidUniqueID;
1670 }
1671 SkDEBUGCODE(this->validate();)
Brian Salomond3ccb0a2017-04-03 10:38:00 -04001672 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::addLegacyMeshDrawOp");
Brian Salomon54d212e2017-03-21 14:22:38 -04001673
1674 // Setup clip
1675 SkRect bounds;
1676 op_bounds(&bounds, op.get());
1677 GrAppliedClip appliedClip;
Brian Salomon467921e2017-03-06 16:17:12 -05001678 if (!clip.apply(fContext, this, pipelineBuilder.isHWAntialias(),
Brian Salomon97180af2017-03-14 13:42:58 -04001679 pipelineBuilder.hasUserStencilSettings(), &appliedClip, &bounds)) {
Robert Phillipsc0138922017-03-08 11:50:55 -05001680 return SK_InvalidUniqueID;
Brian Salomon467921e2017-03-06 16:17:12 -05001681 }
1682
1683 // This forces instantiation of the render target. Pipeline creation is moving to flush time
1684 // by which point instantiation must have occurred anyway.
1685 GrRenderTarget* rt = this->accessRenderTarget();
1686 if (!rt) {
Robert Phillipsc0138922017-03-08 11:50:55 -05001687 return SK_InvalidUniqueID;
Brian Salomon467921e2017-03-06 16:17:12 -05001688 }
1689
1690 GrResourceProvider* resourceProvider = fContext->resourceProvider();
Brian Salomon48d1b4c2017-04-08 07:38:53 -04001691 bool usesStencil = pipelineBuilder.hasUserStencilSettings() || appliedClip.hasStencilClip();
1692 if (usesStencil) {
Brian Salomon467921e2017-03-06 16:17:12 -05001693 if (!resourceProvider->attachStencilAttachment(this->accessRenderTarget())) {
1694 SkDebugf("ERROR creating stencil attachment. Draw skipped.\n");
Robert Phillipsc0138922017-03-08 11:50:55 -05001695 return SK_InvalidUniqueID;
Brian Salomon467921e2017-03-06 16:17:12 -05001696 }
1697 }
1698
Brian Salomon48d1b4c2017-04-08 07:38:53 -04001699 bool isMixedSamples = fRenderTargetProxy->isMixedSampled() &&
1700 (pipelineBuilder.isHWAntialias() || usesStencil);
1701
1702 GrColor overrideColor;
1703 GrProcessorSet::Analysis analysis = op->analyzeUpdateAndRecordProcessors(
1704 &pipelineBuilder, &appliedClip, isMixedSamples, *this->caps(), &overrideColor);
Brian Salomon467921e2017-03-06 16:17:12 -05001705
1706 GrPipeline::InitArgs args;
1707 pipelineBuilder.getPipelineInitArgs(&args);
1708 args.fAppliedClip = &appliedClip;
1709 args.fRenderTarget = rt;
1710 args.fCaps = this->caps();
Brian Salomon467921e2017-03-06 16:17:12 -05001711
Brian Salomon31853842017-03-28 16:32:05 -04001712 if (analysis.requiresDstTexture()) {
Robert Phillipsbf25d432017-04-07 10:08:53 -04001713 if (!this->setupDstTexture(fRenderTargetProxy.get(), clip, bounds, &args.fDstTexture)) {
Robert Phillipsc0138922017-03-08 11:50:55 -05001714 return SK_InvalidUniqueID;
Brian Salomon467921e2017-03-06 16:17:12 -05001715 }
1716 }
Brian Salomon48d1b4c2017-04-08 07:38:53 -04001717 op->initPipeline(args, analysis, overrideColor);
Brian Salomon467921e2017-03-06 16:17:12 -05001718 // TODO: We need to add pipeline dependencies on textures, etc before recording this op.
Brian Salomon97180af2017-03-14 13:42:58 -04001719 op->setClippedBounds(bounds);
Robert Phillipsc0138922017-03-08 11:50:55 -05001720 return this->getOpList()->addOp(std::move(op), this);
Brian Salomon467921e2017-03-06 16:17:12 -05001721}
1722
Robert Phillipsbf25d432017-04-07 10:08:53 -04001723bool GrRenderTargetContext::setupDstTexture(GrRenderTargetProxy* rtProxy, const GrClip& clip,
Brian Salomon467921e2017-03-06 16:17:12 -05001724 const SkRect& opBounds,
1725 GrXferProcessor::DstTexture* dstTexture) {
1726 if (this->caps()->textureBarrierSupport()) {
Robert Phillipsbf25d432017-04-07 10:08:53 -04001727 if (GrTextureProxy* texProxy = rtProxy->asTextureProxy()) {
1728 // MDB TODO: remove this instantiation. Blocked on making DstTexture be proxy-based
1729 sk_sp<GrTexture> tex(sk_ref_sp(texProxy->instantiate(fContext->resourceProvider())));
1730 if (!tex) {
1731 SkDebugf("setupDstTexture: instantiation of src texture failed.\n");
1732 return false; // We have bigger problems now
1733 }
1734
Brian Salomon467921e2017-03-06 16:17:12 -05001735 // The render target is a texture, so we can read from it directly in the shader. The XP
1736 // will be responsible to detect this situation and request a texture barrier.
Robert Phillipsbf25d432017-04-07 10:08:53 -04001737 dstTexture->setTexture(std::move(tex));
Brian Salomon467921e2017-03-06 16:17:12 -05001738 dstTexture->setOffset(0, 0);
Robert Phillipsbf25d432017-04-07 10:08:53 -04001739 return true;
Brian Salomon467921e2017-03-06 16:17:12 -05001740 }
1741 }
1742
Robert Phillipsbf25d432017-04-07 10:08:53 -04001743 SkIRect copyRect = SkIRect::MakeWH(rtProxy->width(), rtProxy->height());
Brian Salomon467921e2017-03-06 16:17:12 -05001744
Eric Karl74480882017-04-03 14:49:05 -07001745 SkIRect clippedRect;
Robert Phillipsbf25d432017-04-07 10:08:53 -04001746 clip.getConservativeBounds(rtProxy->width(), rtProxy->height(), &clippedRect);
Eric Karl72e551e2017-04-04 13:42:10 -07001747 SkIRect drawIBounds;
Brian Salomon467921e2017-03-06 16:17:12 -05001748 opBounds.roundOut(&drawIBounds);
Brian Salomon859621f2017-03-16 09:21:54 -04001749 // Cover up for any precision issues by outsetting the op bounds a pixel in each direction.
1750 drawIBounds.outset(1, 1);
Eric Karl72e551e2017-04-04 13:42:10 -07001751 if (!clippedRect.intersect(drawIBounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -05001752#ifdef SK_DEBUG
Robert Phillipsbf25d432017-04-07 10:08:53 -04001753 GrCapsDebugf(this->caps(), "setupDstTexture: Missed an early reject bailing on draw.");
Brian Salomon467921e2017-03-06 16:17:12 -05001754#endif
Robert Phillipsbf25d432017-04-07 10:08:53 -04001755 return false;
Brian Salomon467921e2017-03-06 16:17:12 -05001756 }
1757
1758 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
1759 // have per-sample dst values by making the copy multisampled.
1760 GrSurfaceDesc desc;
Eric Karl74480882017-04-03 14:49:05 -07001761 bool rectsMustMatch = false;
1762 bool disallowSubrect = false;
Robert Phillipsbf25d432017-04-07 10:08:53 -04001763 if (!this->caps()->initDescForDstCopy(rtProxy, &desc, &rectsMustMatch, &disallowSubrect)) {
1764 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
Brian Salomon467921e2017-03-06 16:17:12 -05001765 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillipsbf25d432017-04-07 10:08:53 -04001766 desc.fConfig = rtProxy->config();
Brian Salomon467921e2017-03-06 16:17:12 -05001767 }
1768
Eric Karl74480882017-04-03 14:49:05 -07001769 if (!disallowSubrect) {
1770 copyRect = clippedRect;
1771 }
Brian Salomon467921e2017-03-06 16:17:12 -05001772
Robert Phillipsbf25d432017-04-07 10:08:53 -04001773 SkIPoint dstPoint, dstOffset;
1774 SkBackingFit fit;
Eric Karl74480882017-04-03 14:49:05 -07001775 if (rectsMustMatch) {
Robert Phillipsbf25d432017-04-07 10:08:53 -04001776 SkASSERT(desc.fOrigin == rtProxy->origin());
1777 desc.fWidth = rtProxy->width();
1778 desc.fHeight = rtProxy->height();
Eric Karl74480882017-04-03 14:49:05 -07001779 dstPoint = {copyRect.fLeft, copyRect.fTop};
1780 dstOffset = {0, 0};
Robert Phillipsbf25d432017-04-07 10:08:53 -04001781 fit = SkBackingFit::kExact;
Eric Karl74480882017-04-03 14:49:05 -07001782 } else {
1783 desc.fWidth = copyRect.width();
1784 desc.fHeight = copyRect.height();
1785 dstPoint = {0, 0};
1786 dstOffset = {copyRect.fLeft, copyRect.fTop};
Robert Phillipsbf25d432017-04-07 10:08:53 -04001787 fit = SkBackingFit::kApprox;
Eric Karl74480882017-04-03 14:49:05 -07001788 }
Brian Salomon467921e2017-03-06 16:17:12 -05001789
Robert Phillipsbf25d432017-04-07 10:08:53 -04001790 sk_sp<GrSurfaceContext> sContext = fContext->contextPriv().makeDeferredSurfaceContext(
1791 desc,
1792 fit,
1793 SkBudgeted::kYes);
1794 if (!sContext) {
1795 SkDebugf("setupDstTexture: surfaceContext creation failed.\n");
1796 return false;
1797 }
1798
1799 if (!sContext->copy(rtProxy, copyRect, dstPoint)) {
1800 SkDebugf("setupDstTexture: copy failed.\n");
1801 return false;
1802 }
1803
1804 GrTextureProxy* copyProxy = sContext->asTextureProxy();
1805 // MDB TODO: remove this instantiation once DstTexture is proxy-backed
1806 sk_sp<GrTexture> copy(sk_ref_sp(copyProxy->instantiate(fContext->resourceProvider())));
Brian Salomon467921e2017-03-06 16:17:12 -05001807 if (!copy) {
Robert Phillipsbf25d432017-04-07 10:08:53 -04001808 SkDebugf("setupDstTexture: instantiation of copied texture failed.\n");
1809 return false;
Brian Salomon467921e2017-03-06 16:17:12 -05001810 }
Eric Karl74480882017-04-03 14:49:05 -07001811
Brian Salomon467921e2017-03-06 16:17:12 -05001812 dstTexture->setTexture(std::move(copy));
Eric Karl74480882017-04-03 14:49:05 -07001813 dstTexture->setOffset(dstOffset);
Robert Phillipsbf25d432017-04-07 10:08:53 -04001814 return true;
robertphillips2334fb62015-06-17 05:43:33 -07001815}