blob: 352cac90af9bd8c9045f84cf2ac0b82820b0155c [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"
robertphillips77a2e522015-10-17 07:43:27 -070011#include "GrDrawingManager.h"
csmartdalton02fa32c2016-08-19 13:29:27 -070012#include "GrFixedClip.h"
robertphillips714712b2016-08-04 06:20:45 -070013#include "GrGpuResourcePriv.h"
robertphillipsea461502015-05-26 11:38:03 -070014#include "GrPathRenderer.h"
robertphillips5fa7f302016-07-21 09:21:04 -070015#include "GrPipelineBuilder.h"
robertphillips2334fb62015-06-17 05:43:33 -070016#include "GrRenderTarget.h"
Brian Salomon5ec9def2016-12-20 15:34:05 -050017#include "GrRenderTargetContextPriv.h"
robertphillips2334fb62015-06-17 05:43:33 -070018#include "GrRenderTargetPriv.h"
bsalomon473addf2015-10-02 07:49:05 -070019#include "GrResourceProvider.h"
Brian Salomon467921e2017-03-06 16:17:12 -050020#include "GrStencilAttachment.h"
21#include "SkLatticeIter.h"
22#include "SkMatrixPriv.h"
robertphillips2d70dcb2015-10-06 07:38:23 -070023#include "SkSurfacePriv.h"
Brian Salomon467921e2017-03-06 16:17:12 -050024#include "effects/GrRRectEffect.h"
25#include "instanced/InstancedRendering.h"
Brian Salomon89527432016-12-16 09:52:16 -050026#include "ops/GrClearOp.h"
Brian Salomon467921e2017-03-06 16:17:12 -050027#include "ops/GrDrawOp.h"
Brian Salomon89527432016-12-16 09:52:16 -050028#include "ops/GrDrawAtlasOp.h"
29#include "ops/GrDrawVerticesOp.h"
30#include "ops/GrLatticeOp.h"
31#include "ops/GrOp.h"
32#include "ops/GrOvalOpFactory.h"
33#include "ops/GrRectOpFactory.h"
34#include "ops/GrRegionOp.h"
35#include "ops/GrShadowRRectOp.h"
Brian Salomon467921e2017-03-06 16:17:12 -050036#include "ops/GrStencilPathOp.h"
joshualitte8042922015-12-11 06:11:21 -080037#include "text/GrAtlasTextContext.h"
38#include "text/GrStencilAndCoverTextContext.h"
joshualittbc907352016-01-13 06:45:40 -080039#include "../private/GrAuditTrail.h"
40
Robert Phillips72152832017-01-25 17:31:35 -050041#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this->drawingManager()->getContext())
joshualitt1de610a2016-01-06 08:26:09 -080042#define ASSERT_SINGLE_OWNER \
joshualittde8dc7e2016-01-08 10:09:13 -080043 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fSingleOwner);)
robertphillips391395d2016-03-02 09:26:36 -080044#define ASSERT_SINGLE_OWNER_PRIV \
Brian Osman11052242016-10-27 14:47:55 -040045 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fRenderTargetContext->fSingleOwner);)
Robert Phillips72152832017-01-25 17:31:35 -050046#define RETURN_IF_ABANDONED if (this->drawingManager()->wasAbandoned()) { return; }
47#define RETURN_IF_ABANDONED_PRIV if (fRenderTargetContext->drawingManager()->wasAbandoned()) { return; }
48#define RETURN_FALSE_IF_ABANDONED if (this->drawingManager()->wasAbandoned()) { return false; }
49#define RETURN_FALSE_IF_ABANDONED_PRIV if (fRenderTargetContext->drawingManager()->wasAbandoned()) { return false; }
50#define RETURN_NULL_IF_ABANDONED if (this->drawingManager()->wasAbandoned()) { return nullptr; }
robertphillipsea461502015-05-26 11:38:03 -070051
csmartdaltona7f29642016-07-07 08:49:11 -070052using gr_instanced::InstancedRendering;
53
robertphillipsea461502015-05-26 11:38:03 -070054class AutoCheckFlush {
55public:
halcanary9d524f22016-03-29 09:03:52 -070056 AutoCheckFlush(GrDrawingManager* drawingManager) : fDrawingManager(drawingManager) {
robertphillips77a2e522015-10-17 07:43:27 -070057 SkASSERT(fDrawingManager);
58 }
bsalomonb77a9072016-09-07 10:02:04 -070059 ~AutoCheckFlush() { fDrawingManager->flushIfNecessary(); }
robertphillipsea461502015-05-26 11:38:03 -070060
61private:
robertphillips77a2e522015-10-17 07:43:27 -070062 GrDrawingManager* fDrawingManager;
robertphillipsea461502015-05-26 11:38:03 -070063};
64
Brian Osman11052242016-10-27 14:47:55 -040065bool GrRenderTargetContext::wasAbandoned() const {
Robert Phillips72152832017-01-25 17:31:35 -050066 return this->drawingManager()->wasAbandoned();
robertphillips7761d612016-05-16 09:14:53 -070067}
68
Robert Phillipsf2361d22016-10-25 14:20:06 -040069// In MDB mode the reffing of the 'getLastOpList' call's result allows in-progress
Brian Osman11052242016-10-27 14:47:55 -040070// GrOpLists to be picked up and added to by renderTargetContexts lower in the call
Robert Phillipsf2361d22016-10-25 14:20:06 -040071// stack. When this occurs with a closed GrOpList, a new one will be allocated
Brian Osman11052242016-10-27 14:47:55 -040072// when the renderTargetContext attempts to use it (via getOpList).
73GrRenderTargetContext::GrRenderTargetContext(GrContext* context,
74 GrDrawingManager* drawingMgr,
Robert Phillipsc7635fa2016-10-28 13:25:24 -040075 sk_sp<GrRenderTargetProxy> rtp,
Brian Osman11052242016-10-27 14:47:55 -040076 sk_sp<SkColorSpace> colorSpace,
77 const SkSurfaceProps* surfaceProps,
78 GrAuditTrail* auditTrail,
79 GrSingleOwner* singleOwner)
Robert Phillips72152832017-01-25 17:31:35 -050080 : GrSurfaceContext(context, drawingMgr, std::move(colorSpace), auditTrail, singleOwner)
Robert Phillipsc7635fa2016-10-28 13:25:24 -040081 , fRenderTargetProxy(std::move(rtp))
82 , fOpList(SkSafeRef(fRenderTargetProxy->getLastRenderTargetOpList()))
Robert Phillipsc7635fa2016-10-28 13:25:24 -040083 , fInstancedPipelineInfo(fRenderTargetProxy.get())
brianosman5a7ae7e2016-09-12 12:07:25 -070084 , fColorXformFromSRGB(nullptr)
Robert Phillips2c862492017-01-18 10:08:39 -050085 , fSurfaceProps(SkSurfacePropsCopyOrDefault(surfaceProps)) {
brianosman5a7ae7e2016-09-12 12:07:25 -070086 if (fColorSpace) {
87 // sRGB sources are very common (SkColor, etc...), so we cache that gamut transformation
Matt Sarett77a7a1b2017-02-07 13:56:11 -050088 auto srgbColorSpace = SkColorSpace::MakeSRGB();
msarettc71a9b72016-09-16 11:01:27 -070089 fColorXformFromSRGB = GrColorSpaceXform::Make(srgbColorSpace.get(), fColorSpace.get());
brianosman5a7ae7e2016-09-12 12:07:25 -070090 }
robertphillips2e1e51f2015-10-15 08:01:48 -070091 SkDEBUGCODE(this->validate();)
robertphillipsea461502015-05-26 11:38:03 -070092}
93
robertphillips2e1e51f2015-10-15 08:01:48 -070094#ifdef SK_DEBUG
Brian Osman11052242016-10-27 14:47:55 -040095void GrRenderTargetContext::validate() const {
Robert Phillipsc7635fa2016-10-28 13:25:24 -040096 SkASSERT(fRenderTargetProxy);
97 fRenderTargetProxy->validate(fContext);
robertphillipsa106c622015-10-16 09:07:06 -070098
Robert Phillipsf2361d22016-10-25 14:20:06 -040099 if (fOpList && !fOpList->isClosed()) {
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400100 SkASSERT(fRenderTargetProxy->getLastOpList() == fOpList);
robertphillipsa106c622015-10-16 09:07:06 -0700101 }
robertphillips2e1e51f2015-10-15 08:01:48 -0700102}
103#endif
104
Brian Osman11052242016-10-27 14:47:55 -0400105GrRenderTargetContext::~GrRenderTargetContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800106 ASSERT_SINGLE_OWNER
Robert Phillipsf2361d22016-10-25 14:20:06 -0400107 SkSafeUnref(fOpList);
robertphillipsa106c622015-10-16 09:07:06 -0700108}
109
Robert Phillipseaa86252016-11-08 13:49:39 +0000110GrRenderTarget* GrRenderTargetContext::instantiate() {
Brian Osman32342f02017-03-04 08:12:46 -0500111 return fRenderTargetProxy->instantiate(fContext->resourceProvider());
Robert Phillipseaa86252016-11-08 13:49:39 +0000112}
113
Robert Phillipsf200a902017-01-30 13:27:37 -0500114GrTextureProxy* GrRenderTargetContext::asTextureProxy() {
Robert Phillipseaa86252016-11-08 13:49:39 +0000115 return fRenderTargetProxy->asTextureProxy();
116}
117
Robert Phillipsf200a902017-01-30 13:27:37 -0500118sk_sp<GrTextureProxy> GrRenderTargetContext::asTextureProxyRef() {
119 return sk_ref_sp(fRenderTargetProxy->asTextureProxy());
120}
121
Brian Osman11052242016-10-27 14:47:55 -0400122GrRenderTargetOpList* GrRenderTargetContext::getOpList() {
joshualitt1de610a2016-01-06 08:26:09 -0800123 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700124 SkDEBUGCODE(this->validate();)
125
Robert Phillipsf2361d22016-10-25 14:20:06 -0400126 if (!fOpList || fOpList->isClosed()) {
Robert Phillips72152832017-01-25 17:31:35 -0500127 fOpList = this->drawingManager()->newOpList(fRenderTargetProxy.get());
robertphillipsa106c622015-10-16 09:07:06 -0700128 }
129
Robert Phillipsf2361d22016-10-25 14:20:06 -0400130 return fOpList;
robertphillipsa106c622015-10-16 09:07:06 -0700131}
132
Robert Phillipse2f7d182016-12-15 09:23:05 -0500133// TODO: move this (and GrTextContext::copy) to GrSurfaceContext?
134bool GrRenderTargetContext::onCopy(GrSurfaceProxy* srcProxy,
135 const SkIRect& srcRect,
136 const SkIPoint& dstPoint) {
joshualitt1de610a2016-01-06 08:26:09 -0800137 ASSERT_SINGLE_OWNER
bsalomonb8fea972016-02-16 07:34:17 -0800138 RETURN_FALSE_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700139 SkDEBUGCODE(this->validate();)
Robert Phillipse2f7d182016-12-15 09:23:05 -0500140 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::copy");
141
142 // TODO: defer instantiation until flush time
Brian Osman32342f02017-03-04 08:12:46 -0500143 sk_sp<GrSurface> src(sk_ref_sp(srcProxy->instantiate(fContext->resourceProvider())));
Robert Phillipse2f7d182016-12-15 09:23:05 -0500144 if (!src) {
145 return false;
146 }
robertphillips2d70dcb2015-10-06 07:38:23 -0700147
Brian Salomon69868af2016-12-22 15:42:51 -0500148 // TODO: This needs to be fixed up since it ends the deferral of the GrRenderTarget.
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400149 sk_sp<GrRenderTarget> rt(
Brian Osman32342f02017-03-04 08:12:46 -0500150 sk_ref_sp(fRenderTargetProxy->instantiate(fContext->resourceProvider())));
Robert Phillipse60ad622016-11-17 10:22:48 -0500151 if (!rt) {
152 return false;
153 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400154
Robert Phillipse2f7d182016-12-15 09:23:05 -0500155 return this->getOpList()->copySurface(rt.get(), src.get(), srcRect, dstPoint);
robertphillipsea461502015-05-26 11:38:03 -0700156}
157
Robert Phillips2c862492017-01-18 10:08:39 -0500158// TODO: move this (and GrTextureContext::onReadPixels) to GrSurfaceContext?
159bool GrRenderTargetContext::onReadPixels(const SkImageInfo& dstInfo, void* dstBuffer,
Robert Phillipsb726d582017-03-09 16:36:32 -0500160 size_t dstRowBytes, int x, int y, uint32_t flags) {
Robert Phillips2c862492017-01-18 10:08:39 -0500161 // TODO: teach GrRenderTarget to take ImageInfo directly to specify the src pixels
162 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo, *fContext->caps());
163 if (kUnknown_GrPixelConfig == config) {
164 return false;
165 }
166
Robert Phillipsb726d582017-03-09 16:36:32 -0500167 // TODO: this seems to duplicate code in SkImage_Gpu::onReadPixels
Robert Phillips2c862492017-01-18 10:08:39 -0500168 if (kUnpremul_SkAlphaType == dstInfo.alphaType()) {
Robert Phillipsb726d582017-03-09 16:36:32 -0500169 flags |= GrContext::kUnpremul_PixelOpsFlag;
Robert Phillips2c862492017-01-18 10:08:39 -0500170 }
171
172 // Deferral of the VRAM resources must end in this instance anyway
173 sk_sp<GrRenderTarget> rt(
Brian Osman32342f02017-03-04 08:12:46 -0500174 sk_ref_sp(fRenderTargetProxy->instantiate(fContext->resourceProvider())));
Robert Phillips2c862492017-01-18 10:08:39 -0500175 if (!rt) {
176 return false;
177 }
178
179 return rt->readPixels(this->getColorSpace(), x, y, dstInfo.width(), dstInfo.height(),
180 config, dstInfo.colorSpace(), dstBuffer, dstRowBytes, flags);
181}
182
183// TODO: move this (and GrTextureContext::onReadPixels) to GrSurfaceContext?
184bool GrRenderTargetContext::onWritePixels(const SkImageInfo& srcInfo, const void* srcBuffer,
Robert Phillips30f9bc62017-02-22 15:28:38 -0500185 size_t srcRowBytes, int x, int y, uint32_t flags) {
Robert Phillips2c862492017-01-18 10:08:39 -0500186 // TODO: teach GrRenderTarget to take ImageInfo directly to specify the src pixels
187 GrPixelConfig config = SkImageInfo2GrPixelConfig(srcInfo, *fContext->caps());
188 if (kUnknown_GrPixelConfig == config) {
189 return false;
190 }
Robert Phillips2c862492017-01-18 10:08:39 -0500191 if (kUnpremul_SkAlphaType == srcInfo.alphaType()) {
Robert Phillips30f9bc62017-02-22 15:28:38 -0500192 flags |= GrContext::kUnpremul_PixelOpsFlag;
Robert Phillips2c862492017-01-18 10:08:39 -0500193 }
194
195 // Deferral of the VRAM resources must end in this instance anyway
196 sk_sp<GrRenderTarget> rt(
Brian Osman32342f02017-03-04 08:12:46 -0500197 sk_ref_sp(fRenderTargetProxy->instantiate(fContext->resourceProvider())));
Robert Phillips2c862492017-01-18 10:08:39 -0500198 if (!rt) {
199 return false;
200 }
201
202 return rt->writePixels(this->getColorSpace(), x, y, srcInfo.width(), srcInfo.height(),
203 config, srcInfo.colorSpace(), srcBuffer, srcRowBytes, flags);
204}
205
206
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500207void GrRenderTargetContext::drawText(const GrClip& clip, const SkPaint& skPaint,
Brian Salomon82f44312017-01-11 13:42:54 -0500208 const SkMatrix& viewMatrix, const char text[],
209 size_t byteLength, SkScalar x, SkScalar y,
210 const SkIRect& clipBounds) {
joshualitt1de610a2016-01-06 08:26:09 -0800211 ASSERT_SINGLE_OWNER
robertphillips2d70dcb2015-10-06 07:38:23 -0700212 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700213 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400214 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawText");
robertphillips2d70dcb2015-10-06 07:38:23 -0700215
Robert Phillips72152832017-01-25 17:31:35 -0500216 GrAtlasTextContext* atlasTextContext = this->drawingManager()->getAtlasTextContext();
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500217 atlasTextContext->drawText(fContext, this, clip, skPaint, viewMatrix, fSurfaceProps, text,
218 byteLength, x, y, clipBounds);
robertphillips2334fb62015-06-17 05:43:33 -0700219}
robertphillipscaef3452015-11-11 13:18:11 -0800220
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500221void GrRenderTargetContext::drawPosText(const GrClip& clip, const SkPaint& paint,
222 const SkMatrix& viewMatrix, const char text[],
223 size_t byteLength, const SkScalar pos[],
Brian Salomon82f44312017-01-11 13:42:54 -0500224 int scalarsPerPosition, const SkPoint& offset,
225 const SkIRect& clipBounds) {
joshualitt1de610a2016-01-06 08:26:09 -0800226 ASSERT_SINGLE_OWNER
robertphillips2d70dcb2015-10-06 07:38:23 -0700227 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700228 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400229 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawPosText");
robertphillips2d70dcb2015-10-06 07:38:23 -0700230
Robert Phillips72152832017-01-25 17:31:35 -0500231 GrAtlasTextContext* atlasTextContext = this->drawingManager()->getAtlasTextContext();
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500232 atlasTextContext->drawPosText(fContext, this, clip, paint, viewMatrix, fSurfaceProps, text,
233 byteLength, pos, scalarsPerPosition, offset, clipBounds);
robertphillips2334fb62015-06-17 05:43:33 -0700234}
robertphillipscaef3452015-11-11 13:18:11 -0800235
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500236void GrRenderTargetContext::drawTextBlob(const GrClip& clip, const SkPaint& paint,
Brian Osman11052242016-10-27 14:47:55 -0400237 const SkMatrix& viewMatrix, const SkTextBlob* blob,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500238 SkScalar x, SkScalar y, SkDrawFilter* filter,
239 const SkIRect& clipBounds) {
joshualitt1de610a2016-01-06 08:26:09 -0800240 ASSERT_SINGLE_OWNER
robertphillips2d70dcb2015-10-06 07:38:23 -0700241 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700242 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400243 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawTextBlob");
robertphillips2d70dcb2015-10-06 07:38:23 -0700244
Robert Phillips72152832017-01-25 17:31:35 -0500245 GrAtlasTextContext* atlasTextContext = this->drawingManager()->getAtlasTextContext();
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500246 atlasTextContext->drawTextBlob(fContext, this, clip, paint, viewMatrix, fSurfaceProps, blob, x,
247 y, filter, clipBounds);
robertphillipsea461502015-05-26 11:38:03 -0700248}
249
Brian Osman11052242016-10-27 14:47:55 -0400250void GrRenderTargetContext::discard() {
joshualitt1de610a2016-01-06 08:26:09 -0800251 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700252 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700253 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400254 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::discard");
robertphillips2e1e51f2015-10-15 08:01:48 -0700255
Robert Phillips72152832017-01-25 17:31:35 -0500256 AutoCheckFlush acf(this->drawingManager());
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400257
Brian Salomon69868af2016-12-22 15:42:51 -0500258 // TODO: This needs to be fixed up since it ends the deferral of the GrRenderTarget.
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400259 sk_sp<GrRenderTarget> rt(
Brian Osman32342f02017-03-04 08:12:46 -0500260 sk_ref_sp(fRenderTargetProxy->instantiate(fContext->resourceProvider())));
Robert Phillipse60ad622016-11-17 10:22:48 -0500261 if (!rt) {
262 return;
263 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400264
Brian Salomon69868af2016-12-22 15:42:51 -0500265 this->getOpList()->discard(this);
robertphillipsea461502015-05-26 11:38:03 -0700266}
267
Brian Osman11052242016-10-27 14:47:55 -0400268void GrRenderTargetContext::clear(const SkIRect* rect,
269 const GrColor color,
270 bool canIgnoreRect) {
joshualitt1de610a2016-01-06 08:26:09 -0800271 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700272 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700273 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400274 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::clear");
robertphillipsea461502015-05-26 11:38:03 -0700275
Robert Phillips72152832017-01-25 17:31:35 -0500276 AutoCheckFlush acf(this->drawingManager());
csmartdalton29df7602016-08-31 11:55:52 -0700277 this->internalClear(rect ? GrFixedClip(*rect) : GrFixedClip::Disabled(), color, canIgnoreRect);
278}
robertphillips9199a9f2016-07-13 07:48:43 -0700279
Robert Phillips784b7bf2016-12-09 13:35:02 -0500280void GrRenderTargetContextPriv::absClear(const SkIRect* clearRect, const GrColor color) {
281 ASSERT_SINGLE_OWNER_PRIV
282 RETURN_IF_ABANDONED_PRIV
283 SkDEBUGCODE(fRenderTargetContext->validate();)
284 GR_AUDIT_TRAIL_AUTO_FRAME(fRenderTargetContext->fAuditTrail,
285 "GrRenderTargetContext::absClear");
286
Robert Phillips72152832017-01-25 17:31:35 -0500287 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500288
289 SkIRect rtRect = SkIRect::MakeWH(fRenderTargetContext->fRenderTargetProxy->worstCaseWidth(
290 *fRenderTargetContext->caps()),
291 fRenderTargetContext->fRenderTargetProxy->worstCaseHeight(
292 *fRenderTargetContext->caps()));
293
294 if (clearRect) {
295 if (clearRect->contains(rtRect)) {
296 clearRect = nullptr; // full screen
297 } else {
298 if (!rtRect.intersect(*clearRect)) {
299 return;
300 }
301 }
302 }
303
304 // TODO: in a post-MDB world this should be handled at the OpList level.
305 // An op-list that is initially cleared and has no other ops should receive an
306 // extra draw.
307 if (fRenderTargetContext->fContext->caps()->useDrawInsteadOfClear()) {
308 // This works around a driver bug with clear by drawing a rect instead.
309 // The driver will ignore a clear if it is the only thing rendered to a
310 // target before the target is read.
311 GrPaint paint;
312 paint.setColor4f(GrColor4f::FromGrColor(color));
Brian Salomona1633922017-01-09 11:46:10 -0500313 paint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
Robert Phillips784b7bf2016-12-09 13:35:02 -0500314
315 // We don't call drawRect() here to avoid the cropping to the, possibly smaller,
316 // RenderTargetProxy bounds
Brian Salomon82f44312017-01-11 13:42:54 -0500317 fRenderTargetContext->drawNonAAFilledRect(GrNoClip(), std::move(paint), SkMatrix::I(),
318 SkRect::Make(rtRect), nullptr, nullptr, nullptr,
319 GrAAType::kNone);
Robert Phillips784b7bf2016-12-09 13:35:02 -0500320
321 } else {
322 if (!fRenderTargetContext->accessRenderTarget()) {
323 return;
324 }
325
326 // This path doesn't handle coalescing of full screen clears b.c. it
327 // has to clear the entire render target - not just the content area.
328 // It could be done but will take more finagling.
Brian Salomonf8334782017-01-03 09:42:58 -0500329 std::unique_ptr<GrOp> op(GrClearOp::Make(
330 rtRect, color, fRenderTargetContext->accessRenderTarget(), !clearRect));
Brian Salomonfc527d22016-12-14 21:07:01 -0500331 if (!op) {
Robert Phillips784b7bf2016-12-09 13:35:02 -0500332 return;
333 }
Brian Salomon69868af2016-12-22 15:42:51 -0500334 fRenderTargetContext->getOpList()->addOp(std::move(op), fRenderTargetContext);
Robert Phillips784b7bf2016-12-09 13:35:02 -0500335 }
336}
337
Brian Osman11052242016-10-27 14:47:55 -0400338void GrRenderTargetContextPriv::clear(const GrFixedClip& clip,
339 const GrColor color,
340 bool canIgnoreClip) {
csmartdalton29df7602016-08-31 11:55:52 -0700341 ASSERT_SINGLE_OWNER_PRIV
342 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400343 SkDEBUGCODE(fRenderTargetContext->validate();)
344 GR_AUDIT_TRAIL_AUTO_FRAME(fRenderTargetContext->fAuditTrail,
345 "GrRenderTargetContextPriv::clear");
csmartdalton29df7602016-08-31 11:55:52 -0700346
Robert Phillips72152832017-01-25 17:31:35 -0500347 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Brian Osman11052242016-10-27 14:47:55 -0400348 fRenderTargetContext->internalClear(clip, color, canIgnoreClip);
csmartdalton29df7602016-08-31 11:55:52 -0700349}
350
Brian Osman11052242016-10-27 14:47:55 -0400351void GrRenderTargetContext::internalClear(const GrFixedClip& clip,
352 const GrColor color,
353 bool canIgnoreClip) {
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700354 bool isFull = false;
355 if (!clip.hasWindowRectangles()) {
356 isFull = !clip.scissorEnabled() ||
357 (canIgnoreClip && fContext->caps()->fullClearIsFree()) ||
358 clip.scissorRect().contains(SkIRect::MakeWH(this->width(), this->height()));
359 }
robertphillips9199a9f2016-07-13 07:48:43 -0700360
361 if (fContext->caps()->useDrawInsteadOfClear()) {
362 // This works around a driver bug with clear by drawing a rect instead.
363 // The driver will ignore a clear if it is the only thing rendered to a
364 // target before the target is read.
Robert Phillips784b7bf2016-12-09 13:35:02 -0500365 SkIRect clearRect = SkIRect::MakeWH(this->width(), this->height());
csmartdalton29df7602016-08-31 11:55:52 -0700366 if (isFull) {
robertphillips9199a9f2016-07-13 07:48:43 -0700367 this->discard();
Robert Phillips93f16332016-11-23 19:37:13 -0500368 } else if (!clearRect.intersect(clip.scissorRect())) {
csmartdalton29df7602016-08-31 11:55:52 -0700369 return;
robertphillips9199a9f2016-07-13 07:48:43 -0700370 }
371
372 GrPaint paint;
373 paint.setColor4f(GrColor4f::FromGrColor(color));
Brian Salomona1633922017-01-09 11:46:10 -0500374 paint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
robertphillips9199a9f2016-07-13 07:48:43 -0700375
Brian Salomon82f44312017-01-11 13:42:54 -0500376 this->drawRect(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), SkRect::Make(clearRect));
bsalomon9f129de2016-08-10 16:31:05 -0700377 } else if (isFull) {
Robert Phillipse60ad622016-11-17 10:22:48 -0500378 if (this->accessRenderTarget()) {
Brian Salomon69868af2016-12-22 15:42:51 -0500379 this->getOpList()->fullClear(this, color);
Robert Phillipse60ad622016-11-17 10:22:48 -0500380 }
robertphillips9199a9f2016-07-13 07:48:43 -0700381 } else {
Robert Phillipse60ad622016-11-17 10:22:48 -0500382 if (!this->accessRenderTarget()) {
383 return;
384 }
Brian Salomonf8334782017-01-03 09:42:58 -0500385 std::unique_ptr<GrOp> op(GrClearOp::Make(clip, color, this->accessRenderTarget()));
Brian Salomon42521e82016-12-07 16:44:58 -0500386 if (!op) {
csmartdalton29df7602016-08-31 11:55:52 -0700387 return;
388 }
Brian Salomon69868af2016-12-22 15:42:51 -0500389 this->getOpList()->addOp(std::move(op), this);
robertphillips9199a9f2016-07-13 07:48:43 -0700390 }
robertphillipsea461502015-05-26 11:38:03 -0700391}
392
Brian Osman11052242016-10-27 14:47:55 -0400393void GrRenderTargetContext::drawPaint(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500394 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400395 const SkMatrix& viewMatrix) {
joshualitt1de610a2016-01-06 08:26:09 -0800396 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700397 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700398 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400399 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawPaint");
robertphillips2e1e51f2015-10-15 08:01:48 -0700400
robertphillipsea461502015-05-26 11:38:03 -0700401 // set rect to be big enough to fill the space, but not super-huge, so we
402 // don't overflow fixed-point implementations
robertphillips13a7eee2016-08-31 15:06:24 -0700403
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400404 SkRect r = fRenderTargetProxy->getBoundsRect();
robertphillipsea461502015-05-26 11:38:03 -0700405
bsalomoncb31e512016-08-26 10:48:19 -0700406 SkRRect rrect;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500407 GrAA aa;
bsalomoncb31e512016-08-26 10:48:19 -0700408 // Check if we can replace a clipRRect()/drawPaint() with a drawRRect(). We only do the
409 // transformation for non-rect rrects. Rects caused a performance regression on an Android
410 // test that needs investigation. We also skip cases where there are fragment processors
411 // because they may depend on having correct local coords and this path draws in device space
412 // without a local matrix.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500413 if (!paint.numTotalFragmentProcessors() && clip.isRRect(r, &rrect, &aa) && !rrect.isRect()) {
Brian Salomon82f44312017-01-11 13:42:54 -0500414 this->drawRRect(GrNoClip(), std::move(paint), aa, SkMatrix::I(), rrect,
415 GrStyle::SimpleFill());
bsalomoncb31e512016-08-26 10:48:19 -0700416 return;
417 }
418
robertphillipsea461502015-05-26 11:38:03 -0700419
420 bool isPerspective = viewMatrix.hasPerspective();
421
422 // We attempt to map r by the inverse matrix and draw that. mapRect will
423 // map the four corners and bound them with a new rect. This will not
424 // produce a correct result for some perspective matrices.
425 if (!isPerspective) {
reeda39667c2016-08-22 06:39:49 -0700426 if (!SkMatrixPriv::InverseMapRect(viewMatrix, &r, r)) {
robertphillipsea461502015-05-26 11:38:03 -0700427 SkDebugf("Could not invert matrix\n");
428 return;
429 }
Brian Salomon82f44312017-01-11 13:42:54 -0500430 this->drawRect(clip, std::move(paint), GrAA::kNo, viewMatrix, r);
robertphillipsea461502015-05-26 11:38:03 -0700431 } else {
432 SkMatrix localMatrix;
433 if (!viewMatrix.invert(&localMatrix)) {
434 SkDebugf("Could not invert matrix\n");
435 return;
436 }
437
Robert Phillips72152832017-01-25 17:31:35 -0500438 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700439
Brian Salomon82f44312017-01-11 13:42:54 -0500440 this->drawNonAAFilledRect(clip, std::move(paint), SkMatrix::I(), r, nullptr, &localMatrix,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500441 nullptr, GrAAType::kNone);
robertphillipsea461502015-05-26 11:38:03 -0700442 }
443}
444
robertphillipsea461502015-05-26 11:38:03 -0700445static inline bool rect_contains_inclusive(const SkRect& rect, const SkPoint& point) {
446 return point.fX >= rect.fLeft && point.fX <= rect.fRight &&
447 point.fY >= rect.fTop && point.fY <= rect.fBottom;
448}
449
bsalomonc55271f2015-11-09 11:55:57 -0800450static bool view_matrix_ok_for_aa_fill_rect(const SkMatrix& viewMatrix) {
451 return viewMatrix.preservesRightAngles();
452}
453
csmartdalton97f6cd52016-07-13 13:37:08 -0700454// Attempts to crop a rect and optional local rect to the clip boundaries.
455// Returns false if the draw can be skipped entirely.
robertphillips13a7eee2016-08-31 15:06:24 -0700456static bool crop_filled_rect(int width, int height, const GrClip& clip,
csmartdalton97f6cd52016-07-13 13:37:08 -0700457 const SkMatrix& viewMatrix, SkRect* rect,
458 SkRect* localRect = nullptr) {
459 if (!viewMatrix.rectStaysRect()) {
460 return true;
461 }
462
csmartdalton97f6cd52016-07-13 13:37:08 -0700463 SkIRect clipDevBounds;
464 SkRect clipBounds;
csmartdalton97f6cd52016-07-13 13:37:08 -0700465
robertphillips13a7eee2016-08-31 15:06:24 -0700466 clip.getConservativeBounds(width, height, &clipDevBounds);
reeda39667c2016-08-22 06:39:49 -0700467 if (!SkMatrixPriv::InverseMapRect(viewMatrix, &clipBounds, SkRect::Make(clipDevBounds))) {
468 return false;
469 }
csmartdalton97f6cd52016-07-13 13:37:08 -0700470
471 if (localRect) {
472 if (!rect->intersects(clipBounds)) {
473 return false;
474 }
475 const SkScalar dx = localRect->width() / rect->width();
476 const SkScalar dy = localRect->height() / rect->height();
477 if (clipBounds.fLeft > rect->fLeft) {
478 localRect->fLeft += (clipBounds.fLeft - rect->fLeft) * dx;
479 rect->fLeft = clipBounds.fLeft;
480 }
481 if (clipBounds.fTop > rect->fTop) {
482 localRect->fTop += (clipBounds.fTop - rect->fTop) * dy;
483 rect->fTop = clipBounds.fTop;
484 }
485 if (clipBounds.fRight < rect->fRight) {
486 localRect->fRight -= (rect->fRight - clipBounds.fRight) * dx;
487 rect->fRight = clipBounds.fRight;
488 }
489 if (clipBounds.fBottom < rect->fBottom) {
490 localRect->fBottom -= (rect->fBottom - clipBounds.fBottom) * dy;
491 rect->fBottom = clipBounds.fBottom;
492 }
493 return true;
494 }
495
496 return rect->intersect(clipBounds);
497}
498
Brian Osman11052242016-10-27 14:47:55 -0400499bool GrRenderTargetContext::drawFilledRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500500 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500501 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400502 const SkMatrix& viewMatrix,
503 const SkRect& rect,
504 const GrUserStencilSettings* ss) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700505 SkRect croppedRect = rect;
Robert Phillips784b7bf2016-12-09 13:35:02 -0500506 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix, &croppedRect)) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700507 return true;
508 }
robertphillips44302392016-07-08 14:43:03 -0700509
Brian Salomon54d212e2017-03-21 14:22:38 -0400510 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport() &&
511 (!ss || ss->isDisabled(false))) {
Robert Phillipsf2361d22016-10-25 14:20:06 -0400512 InstancedRendering* ir = this->getOpList()->instancedRendering();
Brian Salomon54d212e2017-03-21 14:22:38 -0400513 std::unique_ptr<GrDrawOp> op = ir->recordRect(croppedRect, viewMatrix, std::move(paint), aa,
514 fInstancedPipelineInfo);
Brian Salomon42521e82016-12-07 16:44:58 -0500515 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -0400516 this->addDrawOp(clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700517 return true;
csmartdaltona7f29642016-07-07 08:49:11 -0700518 }
519 }
Brian Salomon54d212e2017-03-21 14:22:38 -0400520 GrAAType aaType = this->decideAAType(aa);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500521 if (GrAAType::kCoverage == aaType) {
robertphillips391395d2016-03-02 09:26:36 -0800522 // The fill path can handle rotation but not skew.
523 if (view_matrix_ok_for_aa_fill_rect(viewMatrix)) {
524 SkRect devBoundRect;
csmartdalton97f6cd52016-07-13 13:37:08 -0700525 viewMatrix.mapRect(&devBoundRect, croppedRect);
Brian Salomon649a3412017-03-09 13:50:43 -0500526 std::unique_ptr<GrMeshDrawOp> op =
527 GrRectOpFactory::MakeAAFill(paint, viewMatrix, rect, croppedRect, devBoundRect);
Brian Salomon42521e82016-12-07 16:44:58 -0500528 if (op) {
Brian Salomon82f44312017-01-11 13:42:54 -0500529 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
robertphillips44302392016-07-08 14:43:03 -0700530 if (ss) {
531 pipelineBuilder.setUserStencil(ss);
532 }
Brian Salomon649a3412017-03-09 13:50:43 -0500533 this->addMeshDrawOp(pipelineBuilder, clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700534 return true;
535 }
robertphillips391395d2016-03-02 09:26:36 -0800536 }
537 } else {
Brian Salomon82f44312017-01-11 13:42:54 -0500538 this->drawNonAAFilledRect(clip, std::move(paint), viewMatrix, croppedRect, nullptr, nullptr,
539 ss, aaType);
robertphillips44302392016-07-08 14:43:03 -0700540 return true;
robertphillips391395d2016-03-02 09:26:36 -0800541 }
542
robertphillips44302392016-07-08 14:43:03 -0700543 return false;
robertphillips391395d2016-03-02 09:26:36 -0800544}
545
Brian Osman11052242016-10-27 14:47:55 -0400546void GrRenderTargetContext::drawRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500547 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500548 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400549 const SkMatrix& viewMatrix,
550 const SkRect& rect,
551 const GrStyle* style) {
bsalomon6663acf2016-05-10 09:14:17 -0700552 if (!style) {
553 style = &GrStyle::SimpleFill();
554 }
joshualitt1de610a2016-01-06 08:26:09 -0800555 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700556 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700557 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400558 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawRect");
robertphillips2e1e51f2015-10-15 08:01:48 -0700559
bsalomon6663acf2016-05-10 09:14:17 -0700560 // Path effects should've been devolved to a path in SkGpuDevice
561 SkASSERT(!style->pathEffect());
robertphillipsea461502015-05-26 11:38:03 -0700562
Robert Phillips72152832017-01-25 17:31:35 -0500563 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700564
bsalomon6663acf2016-05-10 09:14:17 -0700565 const SkStrokeRec& stroke = style->strokeRec();
robertphillips3ab14ca2016-07-10 11:49:39 -0700566 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
567
568 if (!fContext->caps()->useDrawInsteadOfClear()) {
569 // Check if this is a full RT draw and can be replaced with a clear. We don't bother
570 // checking cases where the RT is fully inside a stroke.
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400571 SkRect rtRect = fRenderTargetProxy->getBoundsRect();
robertphillips3ab14ca2016-07-10 11:49:39 -0700572 // Does the clip contain the entire RT?
573 if (clip.quickContains(rtRect)) {
574 SkMatrix invM;
575 if (!viewMatrix.invert(&invM)) {
robertphillipsea461502015-05-26 11:38:03 -0700576 return;
577 }
robertphillips3ab14ca2016-07-10 11:49:39 -0700578 // Does the rect bound the RT?
579 SkPoint srcSpaceRTQuad[4];
580 invM.mapRectToQuad(srcSpaceRTQuad, rtRect);
581 if (rect_contains_inclusive(rect, srcSpaceRTQuad[0]) &&
582 rect_contains_inclusive(rect, srcSpaceRTQuad[1]) &&
583 rect_contains_inclusive(rect, srcSpaceRTQuad[2]) &&
584 rect_contains_inclusive(rect, srcSpaceRTQuad[3])) {
585 // Will it blend?
586 GrColor clearColor;
587 if (paint.isConstantBlendedColor(&clearColor)) {
robertphillips9199a9f2016-07-13 07:48:43 -0700588 this->clear(nullptr, clearColor, true);
robertphillips3ab14ca2016-07-10 11:49:39 -0700589 return;
590 }
591 }
robertphillipsea461502015-05-26 11:38:03 -0700592 }
593 }
robertphillips44302392016-07-08 14:43:03 -0700594
Brian Salomon82f44312017-01-11 13:42:54 -0500595 if (this->drawFilledRect(clip, std::move(paint), aa, viewMatrix, rect, nullptr)) {
robertphillips44302392016-07-08 14:43:03 -0700596 return;
597 }
bsalomona7d85ba2016-07-06 11:54:59 -0700598 } else if (stroke.getStyle() == SkStrokeRec::kStroke_Style ||
599 stroke.getStyle() == SkStrokeRec::kHairline_Style) {
600 if ((!rect.width() || !rect.height()) &&
601 SkStrokeRec::kHairline_Style != stroke.getStyle()) {
602 SkScalar r = stroke.getWidth() / 2;
603 // TODO: Move these stroke->fill fallbacks to GrShape?
604 switch (stroke.getJoin()) {
605 case SkPaint::kMiter_Join:
Brian Salomon82f44312017-01-11 13:42:54 -0500606 this->drawRect(
607 clip, std::move(paint), aa, viewMatrix,
608 {rect.fLeft - r, rect.fTop - r, rect.fRight + r, rect.fBottom + r},
609 &GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700610 return;
611 case SkPaint::kRound_Join:
612 // Raster draws nothing when both dimensions are empty.
613 if (rect.width() || rect.height()){
614 SkRRect rrect = SkRRect::MakeRectXY(rect.makeOutset(r, r), r, r);
Brian Salomon82f44312017-01-11 13:42:54 -0500615 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect,
616 GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700617 return;
618 }
619 case SkPaint::kBevel_Join:
620 if (!rect.width()) {
Brian Salomon82f44312017-01-11 13:42:54 -0500621 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700622 {rect.fLeft - r, rect.fTop, rect.fRight + r, rect.fBottom},
623 &GrStyle::SimpleFill());
624 } else {
Brian Salomon82f44312017-01-11 13:42:54 -0500625 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700626 {rect.fLeft, rect.fTop - r, rect.fRight, rect.fBottom + r},
627 &GrStyle::SimpleFill());
628 }
629 return;
630 }
631 }
robertphillips44302392016-07-08 14:43:03 -0700632
robertphillips44302392016-07-08 14:43:03 -0700633 bool snapToPixelCenters = false;
Brian Salomon649a3412017-03-09 13:50:43 -0500634 std::unique_ptr<GrMeshDrawOp> op;
robertphillips44302392016-07-08 14:43:03 -0700635
robertphillips391395d2016-03-02 09:26:36 -0800636 GrColor color = paint.getColor();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500637 GrAAType aaType = this->decideAAType(aa);
638 if (GrAAType::kCoverage == aaType) {
cdaltonbb539482016-01-04 09:48:25 -0800639 // The stroke path needs the rect to remain axis aligned (no rotation or skew).
640 if (viewMatrix.rectStaysRect()) {
Brian Salomon6a639042016-12-14 11:08:17 -0500641 op = GrRectOpFactory::MakeAAStroke(color, viewMatrix, rect, stroke);
cdaltonbb539482016-01-04 09:48:25 -0800642 }
robertphillipsea461502015-05-26 11:38:03 -0700643 } else {
robertphillips391395d2016-03-02 09:26:36 -0800644 // Depending on sub-pixel coordinates and the particular GPU, we may lose a corner of
halcanary9d524f22016-03-29 09:03:52 -0700645 // hairline rects. We jam all the vertices to pixel centers to avoid this, but not
robertphillips391395d2016-03-02 09:26:36 -0800646 // when MSAA is enabled because it can cause ugly artifacts.
bsalomona7d85ba2016-07-06 11:54:59 -0700647 snapToPixelCenters = stroke.getStyle() == SkStrokeRec::kHairline_Style &&
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400648 !fRenderTargetProxy->isUnifiedMultisampled();
Brian Salomon6a639042016-12-14 11:08:17 -0500649 op = GrRectOpFactory::MakeNonAAStroke(color, viewMatrix, rect, stroke,
650 snapToPixelCenters);
robertphillips391395d2016-03-02 09:26:36 -0800651 }
robertphillips4bc31812016-03-01 12:22:49 -0800652
Brian Salomon42521e82016-12-07 16:44:58 -0500653 if (op) {
Brian Salomon82f44312017-01-11 13:42:54 -0500654 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
Brian Salomon189098e72017-01-19 09:55:19 -0500655 pipelineBuilder.setSnapVerticesToPixelCenters(snapToPixelCenters);
Brian Salomon649a3412017-03-09 13:50:43 -0500656 this->addMeshDrawOp(pipelineBuilder, clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700657 return;
robertphillips4bc31812016-03-01 12:22:49 -0800658 }
robertphillips4bc31812016-03-01 12:22:49 -0800659 }
halcanary9d524f22016-03-29 09:03:52 -0700660
robertphillips4bc31812016-03-01 12:22:49 -0800661 SkPath path;
662 path.setIsVolatile(true);
663 path.addRect(rect);
Brian Salomon82f44312017-01-11 13:42:54 -0500664 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, *style);
robertphillipsea461502015-05-26 11:38:03 -0700665}
666
Robert Phillipsec2249f2016-11-09 08:54:35 -0500667int GrRenderTargetContextPriv::maxWindowRectangles() const {
668 return fRenderTargetContext->fRenderTargetProxy->maxWindowRectangles(
669 *fRenderTargetContext->fContext->caps());
670}
671
Brian Osman11052242016-10-27 14:47:55 -0400672void GrRenderTargetContextPriv::clearStencilClip(const GrFixedClip& clip, bool insideStencilMask) {
robertphillips976f5f02016-06-03 10:59:20 -0700673 ASSERT_SINGLE_OWNER_PRIV
674 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400675 SkDEBUGCODE(fRenderTargetContext->validate();)
676 GR_AUDIT_TRAIL_AUTO_FRAME(fRenderTargetContext->fAuditTrail,
677 "GrRenderTargetContextPriv::clearStencilClip");
robertphillips976f5f02016-06-03 10:59:20 -0700678
Robert Phillips72152832017-01-25 17:31:35 -0500679 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Brian Salomon69868af2016-12-22 15:42:51 -0500680 // TODO: This needs to be fixed up since it ends the deferral of the GrRenderTarget.
Robert Phillipse60ad622016-11-17 10:22:48 -0500681 if (!fRenderTargetContext->accessRenderTarget()) {
682 return;
683 }
Brian Osman11052242016-10-27 14:47:55 -0400684 fRenderTargetContext->getOpList()->clearStencilClip(clip, insideStencilMask,
Brian Salomon69868af2016-12-22 15:42:51 -0500685 fRenderTargetContext);
robertphillips976f5f02016-06-03 10:59:20 -0700686}
687
Brian Osman11052242016-10-27 14:47:55 -0400688void GrRenderTargetContextPriv::stencilPath(const GrClip& clip,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500689 GrAAType aaType,
Brian Osman11052242016-10-27 14:47:55 -0400690 const SkMatrix& viewMatrix,
691 const GrPath* path) {
Brian Salomon467921e2017-03-06 16:17:12 -0500692 ASSERT_SINGLE_OWNER_PRIV
693 RETURN_IF_ABANDONED_PRIV
694 SkDEBUGCODE(fRenderTargetContext->validate();)
695 GR_AUDIT_TRAIL_AUTO_FRAME(fRenderTargetContext->fAuditTrail,
696 "GrRenderTargetContext::stencilPath");
697
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500698 SkASSERT(aaType != GrAAType::kCoverage);
Brian Salomon467921e2017-03-06 16:17:12 -0500699
700 bool useHWAA = GrAATypeIsHW(aaType);
701 // TODO: extract portions of checkDraw that are relevant to path stenciling.
702 SkASSERT(path);
703 SkASSERT(fRenderTargetContext->caps()->shaderCaps()->pathRenderingSupport());
704
705 // FIXME: Use path bounds instead of this WAR once
706 // https://bugs.chromium.org/p/skia/issues/detail?id=5640 is resolved.
707 SkRect bounds = SkRect::MakeIWH(fRenderTargetContext->width(), fRenderTargetContext->height());
708
709 // Setup clip
Brian Salomon97180af2017-03-14 13:42:58 -0400710 GrAppliedClip appliedClip;
Brian Salomon467921e2017-03-06 16:17:12 -0500711 if (!clip.apply(fRenderTargetContext->fContext, fRenderTargetContext, useHWAA, true,
Brian Salomon97180af2017-03-14 13:42:58 -0400712 &appliedClip, &bounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -0500713 return;
714 }
715
716 // Coverage AA does not make sense when rendering to the stencil buffer. The caller should never
717 // attempt this in a situation that would require coverage AA.
718 SkASSERT(!appliedClip.clipCoverageFragmentProcessor());
719
720 GrRenderTarget* rt = fRenderTargetContext->accessRenderTarget();
721 if (!rt) {
722 return;
723 }
724 GrStencilAttachment* stencilAttachment =
725 fRenderTargetContext->fContext->resourceProvider()->attachStencilAttachment(rt);
726 if (!stencilAttachment) {
727 SkDebugf("ERROR creating stencil attachment. Draw skipped.\n");
728 return;
729 }
730
731 std::unique_ptr<GrOp> op = GrStencilPathOp::Make(viewMatrix,
732 useHWAA,
733 path->getFillType(),
734 appliedClip.hasStencilClip(),
735 stencilAttachment->bits(),
736 appliedClip.scissorState(),
737 fRenderTargetContext->accessRenderTarget(),
738 path);
Brian Salomon97180af2017-03-14 13:42:58 -0400739 op->setClippedBounds(bounds);
Brian Salomon467921e2017-03-06 16:17:12 -0500740 fRenderTargetContext->getOpList()->recordOp(std::move(op), fRenderTargetContext);
robertphillips976f5f02016-06-03 10:59:20 -0700741}
742
Brian Osman11052242016-10-27 14:47:55 -0400743void GrRenderTargetContextPriv::stencilRect(const GrClip& clip,
744 const GrUserStencilSettings* ss,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500745 GrAAType aaType,
Brian Osman11052242016-10-27 14:47:55 -0400746 const SkMatrix& viewMatrix,
747 const SkRect& rect) {
robertphillips976f5f02016-06-03 10:59:20 -0700748 ASSERT_SINGLE_OWNER_PRIV
749 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400750 SkDEBUGCODE(fRenderTargetContext->validate();)
751 GR_AUDIT_TRAIL_AUTO_FRAME(fRenderTargetContext->fAuditTrail,
752 "GrRenderTargetContext::stencilRect");
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500753 SkASSERT(GrAAType::kCoverage != aaType);
Robert Phillips72152832017-01-25 17:31:35 -0500754 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips976f5f02016-06-03 10:59:20 -0700755
756 GrPaint paint;
Brian Salomona1633922017-01-09 11:46:10 -0500757 paint.setXPFactory(GrDisableColorXPFactory::Get());
robertphillips976f5f02016-06-03 10:59:20 -0700758
Brian Salomon82f44312017-01-11 13:42:54 -0500759 fRenderTargetContext->drawNonAAFilledRect(clip, std::move(paint), viewMatrix, rect, nullptr,
760 nullptr, ss, aaType);
robertphillips976f5f02016-06-03 10:59:20 -0700761}
762
Brian Osman11052242016-10-27 14:47:55 -0400763bool GrRenderTargetContextPriv::drawAndStencilRect(const GrClip& clip,
764 const GrUserStencilSettings* ss,
765 SkRegion::Op op,
766 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500767 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400768 const SkMatrix& viewMatrix,
769 const SkRect& rect) {
robertphillips391395d2016-03-02 09:26:36 -0800770 ASSERT_SINGLE_OWNER_PRIV
771 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400772 SkDEBUGCODE(fRenderTargetContext->validate();)
773 GR_AUDIT_TRAIL_AUTO_FRAME(fRenderTargetContext->fAuditTrail,
774 "GrRenderTargetContext::drawAndStencilRect");
robertphillips391395d2016-03-02 09:26:36 -0800775
Robert Phillips72152832017-01-25 17:31:35 -0500776 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -0800777
778 GrPaint paint;
robertphillips391395d2016-03-02 09:26:36 -0800779 paint.setCoverageSetOpXPFactory(op, invert);
780
Brian Salomon82f44312017-01-11 13:42:54 -0500781 if (fRenderTargetContext->drawFilledRect(clip, std::move(paint), aa, viewMatrix, rect, ss)) {
robertphillips391395d2016-03-02 09:26:36 -0800782 return true;
783 }
robertphillips391395d2016-03-02 09:26:36 -0800784 SkPath path;
785 path.setIsVolatile(true);
786 path.addRect(rect);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500787 return this->drawAndStencilPath(clip, ss, op, invert, aa, viewMatrix, path);
robertphillips391395d2016-03-02 09:26:36 -0800788}
789
Brian Osman11052242016-10-27 14:47:55 -0400790void GrRenderTargetContext::fillRectToRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500791 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500792 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400793 const SkMatrix& viewMatrix,
794 const SkRect& rectToDraw,
795 const SkRect& localRect) {
joshualitt1de610a2016-01-06 08:26:09 -0800796 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700797 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700798 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400799 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::fillRectToRect");
robertphillipsea461502015-05-26 11:38:03 -0700800
csmartdalton97f6cd52016-07-13 13:37:08 -0700801 SkRect croppedRect = rectToDraw;
802 SkRect croppedLocalRect = localRect;
robertphillips13a7eee2016-08-31 15:06:24 -0700803 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix,
804 &croppedRect, &croppedLocalRect)) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700805 return;
806 }
807
Robert Phillips72152832017-01-25 17:31:35 -0500808 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -0700809
csmartdaltone0d36292016-07-29 08:14:20 -0700810 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport()) {
Robert Phillipsf2361d22016-10-25 14:20:06 -0400811 InstancedRendering* ir = this->getOpList()->instancedRendering();
Brian Salomon54d212e2017-03-21 14:22:38 -0400812 std::unique_ptr<GrDrawOp> op(ir->recordRect(croppedRect, viewMatrix, std::move(paint),
813 croppedLocalRect, aa, fInstancedPipelineInfo));
Brian Salomon42521e82016-12-07 16:44:58 -0500814 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -0400815 this->addDrawOp(clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -0700816 return;
817 }
818 }
819
Brian Salomon54d212e2017-03-21 14:22:38 -0400820 GrAAType aaType = this->decideAAType(aa);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500821 if (GrAAType::kCoverage != aaType) {
Brian Salomon82f44312017-01-11 13:42:54 -0500822 this->drawNonAAFilledRect(clip, std::move(paint), viewMatrix, croppedRect,
823 &croppedLocalRect, nullptr, nullptr, aaType);
csmartdaltonfc49d562016-07-26 17:05:47 -0700824 return;
joshualitt04194f32016-01-13 10:08:27 -0800825 }
bsalomonbb243832016-07-22 07:10:19 -0700826
csmartdaltonfc49d562016-07-26 17:05:47 -0700827 if (view_matrix_ok_for_aa_fill_rect(viewMatrix)) {
Brian Salomon54d212e2017-03-21 14:22:38 -0400828 std::unique_ptr<GrMeshDrawOp> op = GrAAFillRectOp::MakeWithLocalRect(
Brian Salomonf8334782017-01-03 09:42:58 -0500829 paint.getColor(), viewMatrix, croppedRect, croppedLocalRect);
Brian Salomon82f44312017-01-11 13:42:54 -0500830 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
Brian Salomon54d212e2017-03-21 14:22:38 -0400831 this->addMeshDrawOp(pipelineBuilder, clip, std::move(op));
csmartdaltonfc49d562016-07-26 17:05:47 -0700832 return;
833 }
834
835 SkMatrix viewAndUnLocalMatrix;
836 if (!viewAndUnLocalMatrix.setRectToRect(localRect, rectToDraw, SkMatrix::kFill_ScaleToFit)) {
837 SkDebugf("fillRectToRect called with empty local matrix.\n");
838 return;
839 }
840 viewAndUnLocalMatrix.postConcat(viewMatrix);
841
842 SkPath path;
843 path.setIsVolatile(true);
844 path.addRect(localRect);
Brian Salomon82f44312017-01-11 13:42:54 -0500845 this->internalDrawPath(clip, std::move(paint), aa, viewAndUnLocalMatrix, path, GrStyle());
joshualittb6b513b2015-08-21 10:25:18 -0700846}
847
Brian Osman11052242016-10-27 14:47:55 -0400848void GrRenderTargetContext::fillRectWithLocalMatrix(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500849 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500850 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400851 const SkMatrix& viewMatrix,
852 const SkRect& rectToDraw,
853 const SkMatrix& localMatrix) {
joshualitt1de610a2016-01-06 08:26:09 -0800854 ASSERT_SINGLE_OWNER
joshualittb6b513b2015-08-21 10:25:18 -0700855 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700856 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400857 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::fillRectWithLocalMatrix");
joshualittb6b513b2015-08-21 10:25:18 -0700858
csmartdalton97f6cd52016-07-13 13:37:08 -0700859 SkRect croppedRect = rectToDraw;
robertphillips13a7eee2016-08-31 15:06:24 -0700860 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix, &croppedRect)) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700861 return;
862 }
863
Robert Phillips72152832017-01-25 17:31:35 -0500864 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -0700865
csmartdaltone0d36292016-07-29 08:14:20 -0700866 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport()) {
Robert Phillipsf2361d22016-10-25 14:20:06 -0400867 InstancedRendering* ir = this->getOpList()->instancedRendering();
Brian Salomon54d212e2017-03-21 14:22:38 -0400868 std::unique_ptr<GrDrawOp> op(ir->recordRect(croppedRect, viewMatrix, std::move(paint),
869 localMatrix, aa, fInstancedPipelineInfo));
Brian Salomon42521e82016-12-07 16:44:58 -0500870 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -0400871 this->addDrawOp(clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -0700872 return;
873 }
874 }
875
Brian Salomon54d212e2017-03-21 14:22:38 -0400876 GrAAType aaType = this->decideAAType(aa);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500877 if (GrAAType::kCoverage != aaType) {
Brian Salomon82f44312017-01-11 13:42:54 -0500878 this->drawNonAAFilledRect(clip, std::move(paint), viewMatrix, croppedRect, nullptr,
879 &localMatrix, nullptr, aaType);
csmartdaltonfc49d562016-07-26 17:05:47 -0700880 return;
bsalomonc55271f2015-11-09 11:55:57 -0800881 }
robertphillips4bc31812016-03-01 12:22:49 -0800882
csmartdaltonfc49d562016-07-26 17:05:47 -0700883 if (view_matrix_ok_for_aa_fill_rect(viewMatrix)) {
Brian Salomon649a3412017-03-09 13:50:43 -0500884 std::unique_ptr<GrMeshDrawOp> op =
Brian Salomon6a639042016-12-14 11:08:17 -0500885 GrAAFillRectOp::Make(paint.getColor(), viewMatrix, localMatrix, croppedRect);
Brian Salomon82f44312017-01-11 13:42:54 -0500886 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
Brian Salomon649a3412017-03-09 13:50:43 -0500887 this->addMeshDrawOp(pipelineBuilder, clip, std::move(op));
csmartdaltonfc49d562016-07-26 17:05:47 -0700888 return;
889 }
890
891 SkMatrix viewAndUnLocalMatrix;
892 if (!localMatrix.invert(&viewAndUnLocalMatrix)) {
893 SkDebugf("fillRectWithLocalMatrix called with degenerate local matrix.\n");
894 return;
895 }
896 viewAndUnLocalMatrix.postConcat(viewMatrix);
897
898 SkPath path;
899 path.setIsVolatile(true);
900 path.addRect(rectToDraw);
901 path.transform(localMatrix);
Brian Salomon82f44312017-01-11 13:42:54 -0500902 this->internalDrawPath(clip, std::move(paint), aa, viewAndUnLocalMatrix, path, GrStyle());
robertphillipsea461502015-05-26 11:38:03 -0700903}
904
Brian Osman11052242016-10-27 14:47:55 -0400905void GrRenderTargetContext::drawVertices(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500906 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400907 const SkMatrix& viewMatrix,
908 GrPrimitiveType primitiveType,
909 int vertexCount,
910 const SkPoint positions[],
911 const SkPoint texCoords[],
Brian Salomon3de0aee2017-01-29 09:34:17 -0500912 const uint32_t colors[],
Brian Osman11052242016-10-27 14:47:55 -0400913 const uint16_t indices[],
Brian Salomon3de0aee2017-01-29 09:34:17 -0500914 int indexCount,
915 ColorArrayType colorArrayType) {
joshualitt1de610a2016-01-06 08:26:09 -0800916 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700917 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700918 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400919 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawVertices");
robertphillipsea461502015-05-26 11:38:03 -0700920
Robert Phillips72152832017-01-25 17:31:35 -0500921 AutoCheckFlush acf(this->drawingManager());
robertphillips2e1e51f2015-10-15 08:01:48 -0700922
robertphillipsea461502015-05-26 11:38:03 -0700923 // TODO clients should give us bounds
924 SkRect bounds;
925 if (!bounds.setBoundsCheck(positions, vertexCount)) {
926 SkDebugf("drawVertices call empty bounds\n");
927 return;
928 }
929
Brian Salomon649a3412017-03-09 13:50:43 -0500930 std::unique_ptr<GrMeshDrawOp> op = GrDrawVerticesOp::Make(
Brian Salomon3de0aee2017-01-29 09:34:17 -0500931 paint.getColor(), primitiveType, viewMatrix, positions, vertexCount, indices,
932 indexCount, colors, texCoords, bounds, colorArrayType);
Brian Salomon199fb872017-02-06 09:41:10 -0500933 if (!op) {
934 return;
935 }
936 GrPipelineBuilder pipelineBuilder(std::move(paint), GrAAType::kNone);
Brian Salomon649a3412017-03-09 13:50:43 -0500937 this->addMeshDrawOp(pipelineBuilder, clip, std::move(op));
Brian Salomon199fb872017-02-06 09:41:10 -0500938}
robertphillipsea461502015-05-26 11:38:03 -0700939
Brian Salomon199fb872017-02-06 09:41:10 -0500940void GrRenderTargetContext::drawVertices(const GrClip& clip,
941 GrPaint&& paint,
942 const SkMatrix& viewMatrix,
Mike Reed5fa66452017-03-16 09:06:34 -0400943 sk_sp<SkVertices> vertices) {
Brian Salomon199fb872017-02-06 09:41:10 -0500944 ASSERT_SINGLE_OWNER
945 RETURN_IF_ABANDONED
946 SkDEBUGCODE(this->validate();)
947 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawVertices");
948
949 AutoCheckFlush acf(this->drawingManager());
950
951 SkASSERT(vertices);
Brian Salomon649a3412017-03-09 13:50:43 -0500952 std::unique_ptr<GrMeshDrawOp> op =
Mike Reed5fa66452017-03-16 09:06:34 -0400953 GrDrawVerticesOp::Make(paint.getColor(), std::move(vertices), viewMatrix);
Brian Salomon199fb872017-02-06 09:41:10 -0500954 if (!op) {
955 return;
956 }
Brian Salomon82f44312017-01-11 13:42:54 -0500957 GrPipelineBuilder pipelineBuilder(std::move(paint), GrAAType::kNone);
Brian Salomon649a3412017-03-09 13:50:43 -0500958 this->addMeshDrawOp(pipelineBuilder, clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -0700959}
960
961///////////////////////////////////////////////////////////////////////////////
962
Brian Osman11052242016-10-27 14:47:55 -0400963void GrRenderTargetContext::drawAtlas(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500964 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400965 const SkMatrix& viewMatrix,
966 int spriteCount,
967 const SkRSXform xform[],
968 const SkRect texRect[],
969 const SkColor colors[]) {
joshualitt1de610a2016-01-06 08:26:09 -0800970 ASSERT_SINGLE_OWNER
jvanverth31ff7622015-08-07 10:09:28 -0700971 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700972 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400973 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawAtlas");
robertphillips2e1e51f2015-10-15 08:01:48 -0700974
Robert Phillips72152832017-01-25 17:31:35 -0500975 AutoCheckFlush acf(this->drawingManager());
halcanary9d524f22016-03-29 09:03:52 -0700976
Brian Salomon649a3412017-03-09 13:50:43 -0500977 std::unique_ptr<GrMeshDrawOp> op =
Brian Salomonfc527d22016-12-14 21:07:01 -0500978 GrDrawAtlasOp::Make(paint.getColor(), viewMatrix, spriteCount, xform, texRect, colors);
Brian Salomon82f44312017-01-11 13:42:54 -0500979 GrPipelineBuilder pipelineBuilder(std::move(paint), GrAAType::kNone);
Brian Salomon649a3412017-03-09 13:50:43 -0500980 this->addMeshDrawOp(pipelineBuilder, clip, std::move(op));
jvanverth31ff7622015-08-07 10:09:28 -0700981}
982
983///////////////////////////////////////////////////////////////////////////////
984
Brian Osman11052242016-10-27 14:47:55 -0400985void GrRenderTargetContext::drawRRect(const GrClip& origClip,
Brian Salomon82f44312017-01-11 13:42:54 -0500986 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500987 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400988 const SkMatrix& viewMatrix,
989 const SkRRect& rrect,
990 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -0800991 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700992 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700993 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400994 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawRRect");
robertphillipsea461502015-05-26 11:38:03 -0700995 if (rrect.isEmpty()) {
996 return;
997 }
998
bsalomon7f0d9f32016-08-15 14:49:10 -0700999 GrNoClip noclip;
1000 const GrClip* clip = &origClip;
1001#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
1002 // The Android framework frequently clips rrects to themselves where the clip is non-aa and the
Brian Salomon42521e82016-12-07 16:44:58 -05001003 // draw is aa. Since our lower level clip code works from op bounds, which are SkRects, it
bsalomon7f0d9f32016-08-15 14:49:10 -07001004 // doesn't detect that the clip can be ignored (modulo antialiasing). The following test
1005 // attempts to mitigate the stencil clip cost but will only help when the entire clip stack
1006 // can be ignored. We'd prefer to fix this in the framework by removing the clips calls.
1007 SkRRect devRRect;
1008 if (rrect.transform(viewMatrix, &devRRect) && clip->quickContains(devRRect)) {
1009 clip = &noclip;
1010 }
1011#endif
bsalomon6663acf2016-05-10 09:14:17 -07001012 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
ksakamotoec7f2ac2016-07-05 03:54:53 -07001013
Robert Phillips72152832017-01-25 17:31:35 -05001014 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001015 const SkStrokeRec stroke = style.strokeRec();
csmartdaltona7f29642016-07-07 08:49:11 -07001016
csmartdaltone0d36292016-07-29 08:14:20 -07001017 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport() &&
1018 stroke.isFillStyle()) {
Robert Phillipsf2361d22016-10-25 14:20:06 -04001019 InstancedRendering* ir = this->getOpList()->instancedRendering();
Brian Salomon54d212e2017-03-21 14:22:38 -04001020 std::unique_ptr<GrDrawOp> op(
1021 ir->recordRRect(rrect, viewMatrix, std::move(paint), aa, fInstancedPipelineInfo));
Brian Salomon42521e82016-12-07 16:44:58 -05001022 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -04001023 this->addDrawOp(*clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -07001024 return;
1025 }
1026 }
1027
Brian Salomon54d212e2017-03-21 14:22:38 -04001028 GrAAType aaType = this->decideAAType(aa);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001029 if (GrAAType::kCoverage == aaType) {
Brian Salomon94efbf52016-11-29 13:43:05 -05001030 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomon649a3412017-03-09 13:50:43 -05001031 std::unique_ptr<GrMeshDrawOp> op =
1032 GrOvalOpFactory::MakeRRectOp(paint.getColor(),
1033 paint.usesDistanceVectorField(),
1034 viewMatrix,
1035 rrect,
1036 stroke,
1037 shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -05001038 if (op) {
Brian Salomon82f44312017-01-11 13:42:54 -05001039 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
Brian Salomon649a3412017-03-09 13:50:43 -05001040 this->addMeshDrawOp(pipelineBuilder, *clip, std::move(op));
robertphillipsb56f9272016-02-25 11:03:52 -08001041 return;
1042 }
robertphillipsea461502015-05-26 11:38:03 -07001043 }
robertphillipsb56f9272016-02-25 11:03:52 -08001044
1045 SkPath path;
1046 path.setIsVolatile(true);
1047 path.addRRect(rrect);
Brian Salomon82f44312017-01-11 13:42:54 -05001048 this->internalDrawPath(*clip, std::move(paint), aa, viewMatrix, path, style);
robertphillipsea461502015-05-26 11:38:03 -07001049}
1050
Jim Van Verthc5903412016-11-17 15:27:09 -05001051///////////////////////////////////////////////////////////////////////////////
1052
1053void GrRenderTargetContext::drawShadowRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001054 GrPaint&& paint,
Jim Van Verthc5903412016-11-17 15:27:09 -05001055 const SkMatrix& viewMatrix,
1056 const SkRRect& rrect,
1057 SkScalar blurRadius,
1058 const GrStyle& style) {
1059 ASSERT_SINGLE_OWNER
1060 RETURN_IF_ABANDONED
1061 SkDEBUGCODE(this->validate();)
1062 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawShadowRRect");
1063 if (rrect.isEmpty()) {
1064 return;
1065 }
1066
1067 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
1068
Robert Phillips72152832017-01-25 17:31:35 -05001069 AutoCheckFlush acf(this->drawingManager());
Jim Van Verthc5903412016-11-17 15:27:09 -05001070 const SkStrokeRec stroke = style.strokeRec();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001071 // TODO: add instancing support?
Jim Van Verthc5903412016-11-17 15:27:09 -05001072
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001073 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomon649a3412017-03-09 13:50:43 -05001074 std::unique_ptr<GrMeshDrawOp> op = GrShadowRRectOp::Make(paint.getColor(), viewMatrix, rrect,
1075 blurRadius, stroke, shaderCaps);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001076 if (op) {
Brian Salomon82f44312017-01-11 13:42:54 -05001077 GrPipelineBuilder pipelineBuilder(std::move(paint), GrAAType::kNone);
Brian Salomon649a3412017-03-09 13:50:43 -05001078 this->addMeshDrawOp(pipelineBuilder, clip, std::move(op));
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001079 return;
Jim Van Verthc5903412016-11-17 15:27:09 -05001080 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001081}
1082
1083///////////////////////////////////////////////////////////////////////////////
1084
Brian Osman11052242016-10-27 14:47:55 -04001085bool GrRenderTargetContext::drawFilledDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001086 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001087 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001088 const SkMatrix& viewMatrix,
1089 const SkRRect& origOuter,
1090 const SkRRect& origInner) {
robertphillips00095892016-02-29 13:50:40 -08001091 SkASSERT(!origInner.isEmpty());
1092 SkASSERT(!origOuter.isEmpty());
1093
csmartdaltone0d36292016-07-29 08:14:20 -07001094 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport()) {
Robert Phillipsf2361d22016-10-25 14:20:06 -04001095 InstancedRendering* ir = this->getOpList()->instancedRendering();
Brian Salomon54d212e2017-03-21 14:22:38 -04001096 std::unique_ptr<GrDrawOp> op(ir->recordDRRect(
1097 origOuter, origInner, viewMatrix, std::move(paint), aa, fInstancedPipelineInfo));
Brian Salomon42521e82016-12-07 16:44:58 -05001098 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -04001099 this->addDrawOp(clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -07001100 return true;
1101 }
1102 }
1103
Brian Salomon54d212e2017-03-21 14:22:38 -04001104 GrAAType aaType = this->decideAAType(aa);
robertphillips00095892016-02-29 13:50:40 -08001105
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001106 GrPrimitiveEdgeType innerEdgeType, outerEdgeType;
1107 if (GrAAType::kCoverage == aaType) {
1108 innerEdgeType = kInverseFillAA_GrProcessorEdgeType;
1109 outerEdgeType = kFillAA_GrProcessorEdgeType;
1110 } else {
1111 innerEdgeType = kInverseFillBW_GrProcessorEdgeType;
1112 outerEdgeType = kFillBW_GrProcessorEdgeType;
1113 }
robertphillips00095892016-02-29 13:50:40 -08001114
1115 SkTCopyOnFirstWrite<SkRRect> inner(origInner), outer(origOuter);
1116 SkMatrix inverseVM;
1117 if (!viewMatrix.isIdentity()) {
1118 if (!origInner.transform(viewMatrix, inner.writable())) {
1119 return false;
1120 }
1121 if (!origOuter.transform(viewMatrix, outer.writable())) {
1122 return false;
1123 }
1124 if (!viewMatrix.invert(&inverseVM)) {
1125 return false;
1126 }
1127 } else {
1128 inverseVM.reset();
halcanary9d524f22016-03-29 09:03:52 -07001129 }
robertphillips00095892016-02-29 13:50:40 -08001130
robertphillips00095892016-02-29 13:50:40 -08001131 // TODO these need to be a geometry processors
bungeman06ca8ec2016-06-09 08:01:03 -07001132 sk_sp<GrFragmentProcessor> innerEffect(GrRRectEffect::Make(innerEdgeType, *inner));
robertphillips00095892016-02-29 13:50:40 -08001133 if (!innerEffect) {
1134 return false;
1135 }
1136
bungeman06ca8ec2016-06-09 08:01:03 -07001137 sk_sp<GrFragmentProcessor> outerEffect(GrRRectEffect::Make(outerEdgeType, *outer));
robertphillips00095892016-02-29 13:50:40 -08001138 if (!outerEffect) {
1139 return false;
1140 }
1141
Brian Salomon82f44312017-01-11 13:42:54 -05001142 paint.addCoverageFragmentProcessor(std::move(innerEffect));
1143 paint.addCoverageFragmentProcessor(std::move(outerEffect));
robertphillips00095892016-02-29 13:50:40 -08001144
1145 SkRect bounds = outer->getBounds();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001146 if (GrAAType::kCoverage == aaType) {
robertphillips00095892016-02-29 13:50:40 -08001147 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1148 }
halcanary9d524f22016-03-29 09:03:52 -07001149
Brian Salomon82f44312017-01-11 13:42:54 -05001150 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), bounds,
1151 inverseVM);
robertphillips00095892016-02-29 13:50:40 -08001152 return true;
1153}
1154
Brian Osman11052242016-10-27 14:47:55 -04001155void GrRenderTargetContext::drawDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001156 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001157 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001158 const SkMatrix& viewMatrix,
1159 const SkRRect& outer,
1160 const SkRRect& inner) {
robertphillips00095892016-02-29 13:50:40 -08001161 ASSERT_SINGLE_OWNER
1162 RETURN_IF_ABANDONED
1163 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -04001164 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawDRRect");
robertphillips00095892016-02-29 13:50:40 -08001165
1166 SkASSERT(!outer.isEmpty());
1167 SkASSERT(!inner.isEmpty());
1168
Robert Phillips72152832017-01-25 17:31:35 -05001169 AutoCheckFlush acf(this->drawingManager());
robertphillips00095892016-02-29 13:50:40 -08001170
Brian Salomon82f44312017-01-11 13:42:54 -05001171 if (this->drawFilledDRRect(clip, std::move(paint), aa, viewMatrix, outer, inner)) {
robertphillips00095892016-02-29 13:50:40 -08001172 return;
1173 }
1174
1175 SkPath path;
1176 path.setIsVolatile(true);
1177 path.addRRect(inner);
1178 path.addRRect(outer);
1179 path.setFillType(SkPath::kEvenOdd_FillType);
1180
Brian Salomon82f44312017-01-11 13:42:54 -05001181 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, GrStyle::SimpleFill());
robertphillips00095892016-02-29 13:50:40 -08001182}
1183
robertphillipsea461502015-05-26 11:38:03 -07001184///////////////////////////////////////////////////////////////////////////////
1185
msarettcc319b92016-08-25 18:07:18 -07001186static inline bool is_int(float x) {
1187 return x == (float) sk_float_round2int(x);
1188}
1189
Brian Osman11052242016-10-27 14:47:55 -04001190void GrRenderTargetContext::drawRegion(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001191 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001192 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001193 const SkMatrix& viewMatrix,
1194 const SkRegion& region,
1195 const GrStyle& style) {
msarettcc319b92016-08-25 18:07:18 -07001196 ASSERT_SINGLE_OWNER
1197 RETURN_IF_ABANDONED
1198 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -04001199 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawRegion");
msarettcc319b92016-08-25 18:07:18 -07001200
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001201 if (GrAA::kYes == aa) {
Brian Salomonfc527d22016-12-14 21:07:01 -05001202 // GrRegionOp performs no antialiasing but is much faster, so here we check the matrix
Brian Salomonc57c7c92016-12-06 14:47:34 -05001203 // to see whether aa is really required.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001204 if (!SkToBool(viewMatrix.getType() & ~(SkMatrix::kTranslate_Mask)) &&
1205 is_int(viewMatrix.getTranslateX()) &&
1206 is_int(viewMatrix.getTranslateY())) {
1207 aa = GrAA::kNo;
1208 }
Brian Salomonc57c7c92016-12-06 14:47:34 -05001209 }
msarettcc319b92016-08-25 18:07:18 -07001210 bool complexStyle = !style.isSimpleFill();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001211 if (complexStyle || GrAA::kYes == aa) {
msarettcc319b92016-08-25 18:07:18 -07001212 SkPath path;
1213 region.getBoundaryPath(&path);
Brian Salomon82f44312017-01-11 13:42:54 -05001214 return this->drawPath(clip, std::move(paint), aa, viewMatrix, path, style);
msarettcc319b92016-08-25 18:07:18 -07001215 }
1216
Brian Salomon649a3412017-03-09 13:50:43 -05001217 std::unique_ptr<GrMeshDrawOp> op = GrRegionOp::Make(paint.getColor(), viewMatrix, region);
Brian Salomon82f44312017-01-11 13:42:54 -05001218 GrPipelineBuilder pipelineBuilder(std::move(paint), GrAAType::kNone);
Brian Salomon649a3412017-03-09 13:50:43 -05001219 this->addMeshDrawOp(pipelineBuilder, clip, std::move(op));
msarettcc319b92016-08-25 18:07:18 -07001220}
1221
Brian Osman11052242016-10-27 14:47:55 -04001222void GrRenderTargetContext::drawOval(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001223 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001224 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001225 const SkMatrix& viewMatrix,
1226 const SkRect& oval,
1227 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001228 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001229 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001230 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -04001231 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawOval");
robertphillips2e1e51f2015-10-15 08:01:48 -07001232
robertphillipsea461502015-05-26 11:38:03 -07001233 if (oval.isEmpty()) {
1234 return;
1235 }
1236
bsalomon6663acf2016-05-10 09:14:17 -07001237 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
robertphillipsea461502015-05-26 11:38:03 -07001238
Robert Phillips72152832017-01-25 17:31:35 -05001239 AutoCheckFlush acf(this->drawingManager());
bsalomon6663acf2016-05-10 09:14:17 -07001240 const SkStrokeRec& stroke = style.strokeRec();
csmartdaltona7f29642016-07-07 08:49:11 -07001241
csmartdaltone0d36292016-07-29 08:14:20 -07001242 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport() &&
1243 stroke.isFillStyle()) {
Robert Phillipsf2361d22016-10-25 14:20:06 -04001244 InstancedRendering* ir = this->getOpList()->instancedRendering();
Brian Salomon54d212e2017-03-21 14:22:38 -04001245 std::unique_ptr<GrDrawOp> op(
1246 ir->recordOval(oval, viewMatrix, std::move(paint), aa, fInstancedPipelineInfo));
Brian Salomon42521e82016-12-07 16:44:58 -05001247 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -04001248 this->addDrawOp(clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -07001249 return;
1250 }
1251 }
1252
Brian Salomon54d212e2017-03-21 14:22:38 -04001253 GrAAType aaType = this->decideAAType(aa);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001254 if (GrAAType::kCoverage == aaType) {
Brian Salomon94efbf52016-11-29 13:43:05 -05001255 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomon649a3412017-03-09 13:50:43 -05001256 std::unique_ptr<GrMeshDrawOp> op =
Brian Salomon289e3d82016-12-14 15:52:56 -05001257 GrOvalOpFactory::MakeOvalOp(paint.getColor(), viewMatrix, oval, stroke, shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -05001258 if (op) {
Brian Salomon82f44312017-01-11 13:42:54 -05001259 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
Brian Salomon649a3412017-03-09 13:50:43 -05001260 this->addMeshDrawOp(pipelineBuilder, clip, std::move(op));
robertphillipsb56f9272016-02-25 11:03:52 -08001261 return;
1262 }
robertphillipsea461502015-05-26 11:38:03 -07001263 }
robertphillipsb56f9272016-02-25 11:03:52 -08001264
1265 SkPath path;
1266 path.setIsVolatile(true);
1267 path.addOval(oval);
Brian Salomon82f44312017-01-11 13:42:54 -05001268 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, style);
robertphillipsea461502015-05-26 11:38:03 -07001269}
1270
Brian Osman11052242016-10-27 14:47:55 -04001271void GrRenderTargetContext::drawArc(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001272 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001273 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001274 const SkMatrix& viewMatrix,
1275 const SkRect& oval,
1276 SkScalar startAngle,
1277 SkScalar sweepAngle,
1278 bool useCenter,
1279 const GrStyle& style) {
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001280 ASSERT_SINGLE_OWNER
1281 RETURN_IF_ABANDONED
1282 SkDEBUGCODE(this->validate();)
1283 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawArc");
1284
1285 AutoCheckFlush acf(this->drawingManager());
1286
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001287 GrAAType aaType = this->decideAAType(aa);
1288 if (GrAAType::kCoverage == aaType) {
Brian Salomon94efbf52016-11-29 13:43:05 -05001289 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomon649a3412017-03-09 13:50:43 -05001290 std::unique_ptr<GrMeshDrawOp> op = GrOvalOpFactory::MakeArcOp(paint.getColor(),
1291 viewMatrix,
1292 oval,
1293 startAngle,
1294 sweepAngle,
1295 useCenter,
1296 style,
1297 shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -05001298 if (op) {
Brian Salomon82f44312017-01-11 13:42:54 -05001299 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
Brian Salomon649a3412017-03-09 13:50:43 -05001300 this->addMeshDrawOp(pipelineBuilder, clip, std::move(op));
bsalomon4f3a0ca2016-08-22 13:14:26 -07001301 return;
1302 }
1303 }
1304 SkPath path;
bsalomon21af9ca2016-08-25 12:29:23 -07001305 SkPathPriv::CreateDrawArcPath(&path, oval, startAngle, sweepAngle, useCenter,
1306 style.isSimpleFill());
Brian Salomon82f44312017-01-11 13:42:54 -05001307 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, style);
bsalomon4f3a0ca2016-08-22 13:14:26 -07001308}
1309
Brian Osman11052242016-10-27 14:47:55 -04001310void GrRenderTargetContext::drawImageLattice(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001311 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001312 const SkMatrix& viewMatrix,
1313 int imageWidth,
1314 int imageHeight,
1315 std::unique_ptr<SkLatticeIter> iter,
1316 const SkRect& dst) {
joshualitt1de610a2016-01-06 08:26:09 -08001317 ASSERT_SINGLE_OWNER
joshualitt33a5fce2015-11-18 13:28:51 -08001318 RETURN_IF_ABANDONED
1319 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -04001320 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawImageLattice");
joshualitt33a5fce2015-11-18 13:28:51 -08001321
Robert Phillips72152832017-01-25 17:31:35 -05001322 AutoCheckFlush acf(this->drawingManager());
joshualitt33a5fce2015-11-18 13:28:51 -08001323
Brian Salomon649a3412017-03-09 13:50:43 -05001324 std::unique_ptr<GrMeshDrawOp> op = GrLatticeOp::MakeNonAA(
1325 paint.getColor(), viewMatrix, imageWidth, imageHeight, std::move(iter), dst);
joshualitt33a5fce2015-11-18 13:28:51 -08001326
Brian Salomon82f44312017-01-11 13:42:54 -05001327 GrPipelineBuilder pipelineBuilder(std::move(paint), GrAAType::kNone);
Brian Salomon649a3412017-03-09 13:50:43 -05001328 this->addMeshDrawOp(pipelineBuilder, clip, std::move(op));
joshualitt33a5fce2015-11-18 13:28:51 -08001329}
1330
Brian Osman11052242016-10-27 14:47:55 -04001331void GrRenderTargetContext::prepareForExternalIO() {
robertphillips8c523e02016-07-26 07:41:00 -07001332 ASSERT_SINGLE_OWNER
1333 RETURN_IF_ABANDONED
1334 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -04001335 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::prepareForExternalIO");
robertphillips8c523e02016-07-26 07:41:00 -07001336
Robert Phillips7ee385e2017-03-30 08:02:11 -04001337 this->drawingManager()->prepareSurfaceForExternalIO(fRenderTargetProxy.get());
robertphillips8c523e02016-07-26 07:41:00 -07001338}
joshualitt33a5fce2015-11-18 13:28:51 -08001339
Brian Osman11052242016-10-27 14:47:55 -04001340void GrRenderTargetContext::drawNonAAFilledRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001341 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001342 const SkMatrix& viewMatrix,
1343 const SkRect& rect,
1344 const SkRect* localRect,
1345 const SkMatrix* localMatrix,
1346 const GrUserStencilSettings* ss,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001347 GrAAType hwOrNoneAAType) {
1348 SkASSERT(GrAAType::kCoverage != hwOrNoneAAType);
1349 SkASSERT(hwOrNoneAAType == GrAAType::kNone || this->isStencilBufferMultisampled());
Brian Salomon649a3412017-03-09 13:50:43 -05001350 std::unique_ptr<GrMeshDrawOp> op = GrRectOpFactory::MakeNonAAFill(paint.getColor(), viewMatrix,
1351 rect, localRect, localMatrix);
Brian Salomon82f44312017-01-11 13:42:54 -05001352 GrPipelineBuilder pipelineBuilder(std::move(paint), hwOrNoneAAType);
robertphillips44302392016-07-08 14:43:03 -07001353 if (ss) {
1354 pipelineBuilder.setUserStencil(ss);
1355 }
Brian Salomon649a3412017-03-09 13:50:43 -05001356 this->addMeshDrawOp(pipelineBuilder, clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -07001357}
1358
robertphillipsea461502015-05-26 11:38:03 -07001359// Can 'path' be drawn as a pair of filled nested rectangles?
bsalomon6663acf2016-05-10 09:14:17 -07001360static bool fills_as_nested_rects(const SkMatrix& viewMatrix, const SkPath& path, SkRect rects[2]) {
robertphillipsea461502015-05-26 11:38:03 -07001361
1362 if (path.isInverseFillType()) {
1363 return false;
1364 }
1365
1366 // TODO: this restriction could be lifted if we were willing to apply
1367 // the matrix to all the points individually rather than just to the rect
robertphillips0e7029e2015-11-30 05:45:06 -08001368 if (!viewMatrix.rectStaysRect()) {
robertphillipsea461502015-05-26 11:38:03 -07001369 return false;
1370 }
1371
1372 SkPath::Direction dirs[2];
1373 if (!path.isNestedFillRects(rects, dirs)) {
1374 return false;
1375 }
1376
1377 if (SkPath::kWinding_FillType == path.getFillType() && dirs[0] == dirs[1]) {
1378 // The two rects need to be wound opposite to each other
1379 return false;
1380 }
1381
1382 // Right now, nested rects where the margin is not the same width
1383 // all around do not render correctly
1384 const SkScalar* outer = rects[0].asScalars();
1385 const SkScalar* inner = rects[1].asScalars();
1386
1387 bool allEq = true;
1388
1389 SkScalar margin = SkScalarAbs(outer[0] - inner[0]);
1390 bool allGoE1 = margin >= SK_Scalar1;
1391
1392 for (int i = 1; i < 4; ++i) {
1393 SkScalar temp = SkScalarAbs(outer[i] - inner[i]);
1394 if (temp < SK_Scalar1) {
1395 allGoE1 = false;
1396 }
1397 if (!SkScalarNearlyEqual(margin, temp)) {
1398 allEq = false;
1399 }
1400 }
1401
1402 return allEq || allGoE1;
1403}
1404
Brian Osman11052242016-10-27 14:47:55 -04001405void GrRenderTargetContext::drawPath(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001406 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001407 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001408 const SkMatrix& viewMatrix,
1409 const SkPath& path,
1410 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001411 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001412 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001413 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -04001414 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawPath");
robertphillips2e1e51f2015-10-15 08:01:48 -07001415
robertphillipsea461502015-05-26 11:38:03 -07001416 if (path.isEmpty()) {
1417 if (path.isInverseFillType()) {
Brian Salomon82f44312017-01-11 13:42:54 -05001418 this->drawPaint(clip, std::move(paint), viewMatrix);
robertphillipsea461502015-05-26 11:38:03 -07001419 }
1420 return;
1421 }
1422
Robert Phillips72152832017-01-25 17:31:35 -05001423 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -07001424
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001425 GrAAType aaType = this->decideAAType(aa);
1426 if (GrAAType::kCoverage == aaType && !style.pathEffect()) {
bsalomon6663acf2016-05-10 09:14:17 -07001427 if (style.isSimpleFill() && !path.isConvex()) {
robertphillipsea461502015-05-26 11:38:03 -07001428 // Concave AA paths are expensive - try to avoid them for special cases
1429 SkRect rects[2];
1430
bsalomon6663acf2016-05-10 09:14:17 -07001431 if (fills_as_nested_rects(viewMatrix, path, rects)) {
Brian Salomon649a3412017-03-09 13:50:43 -05001432 std::unique_ptr<GrMeshDrawOp> op =
Brian Salomon6a639042016-12-14 11:08:17 -05001433 GrRectOpFactory::MakeAAFillNestedRects(paint.getColor(), viewMatrix, rects);
Brian Salomon21aa35f2016-12-09 16:01:53 -05001434 if (op) {
Brian Salomon82f44312017-01-11 13:42:54 -05001435 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
Brian Salomon649a3412017-03-09 13:50:43 -05001436 this->addMeshDrawOp(pipelineBuilder, clip, std::move(op));
bsalomon40ef4852016-05-02 13:22:13 -07001437 }
robertphillipsea461502015-05-26 11:38:03 -07001438 return;
1439 }
1440 }
1441 SkRect ovalRect;
1442 bool isOval = path.isOval(&ovalRect);
1443
1444 if (isOval && !path.isInverseFillType()) {
Brian Salomon94efbf52016-11-29 13:43:05 -05001445 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomon649a3412017-03-09 13:50:43 -05001446 std::unique_ptr<GrMeshDrawOp> op = GrOvalOpFactory::MakeOvalOp(
Brian Salomon289e3d82016-12-14 15:52:56 -05001447 paint.getColor(), viewMatrix, ovalRect, style.strokeRec(), shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -05001448 if (op) {
Brian Salomon82f44312017-01-11 13:42:54 -05001449 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
Brian Salomon649a3412017-03-09 13:50:43 -05001450 this->addMeshDrawOp(pipelineBuilder, clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -07001451 return;
1452 }
1453 }
1454 }
robertphillips4bc31812016-03-01 12:22:49 -08001455
1456 // Note that internalDrawPath may sw-rasterize the path into a scratch texture.
1457 // Scratch textures can be recycled after they are returned to the texture
1458 // cache. This presents a potential hazard for buffered drawing. However,
1459 // the writePixels that uploads to the scratch will perform a flush so we're
1460 // OK.
Brian Salomon82f44312017-01-11 13:42:54 -05001461 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, style);
robertphillipsea461502015-05-26 11:38:03 -07001462}
1463
Brian Osman11052242016-10-27 14:47:55 -04001464bool GrRenderTargetContextPriv::drawAndStencilPath(const GrClip& clip,
1465 const GrUserStencilSettings* ss,
1466 SkRegion::Op op,
1467 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001468 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001469 const SkMatrix& viewMatrix,
1470 const SkPath& path) {
robertphillips391395d2016-03-02 09:26:36 -08001471 ASSERT_SINGLE_OWNER_PRIV
1472 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -04001473 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001474 GR_AUDIT_TRAIL_AUTO_FRAME(fRenderTargetContext->fAuditTrail,
1475 "GrRenderTargetContextPriv::drawAndStencilPath");
robertphillips391395d2016-03-02 09:26:36 -08001476
1477 if (path.isEmpty() && path.isInverseFillType()) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001478 this->drawAndStencilRect(clip, ss, op, invert, GrAA::kNo, SkMatrix::I(),
Brian Osman11052242016-10-27 14:47:55 -04001479 SkRect::MakeIWH(fRenderTargetContext->width(),
1480 fRenderTargetContext->height()));
robertphillips391395d2016-03-02 09:26:36 -08001481 return true;
1482 }
1483
Robert Phillips72152832017-01-25 17:31:35 -05001484 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -08001485
1486 // An Assumption here is that path renderer would use some form of tweaking
1487 // the src color (either the input alpha or in the frag shader) to implement
1488 // aa. If we have some future driver-mojo path AA that can do the right
1489 // thing WRT to the blend then we'll need some query on the PR.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001490 GrAAType aaType = fRenderTargetContext->decideAAType(aa);
robertphillips976f5f02016-06-03 10:59:20 -07001491 bool hasUserStencilSettings = !ss->isUnused();
robertphillips391395d2016-03-02 09:26:36 -08001492
bsalomon8acedde2016-06-24 10:42:16 -07001493 GrShape shape(path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -08001494 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Osman11052242016-10-27 14:47:55 -04001495 canDrawArgs.fShaderCaps =
Robert Phillips72152832017-01-25 17:31:35 -05001496 fRenderTargetContext->drawingManager()->getContext()->caps()->shaderCaps();
robertphillips391395d2016-03-02 09:26:36 -08001497 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07001498 canDrawArgs.fShape = &shape;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001499 canDrawArgs.fAAType = aaType;
cdalton93a379b2016-05-11 13:58:08 -07001500 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips391395d2016-03-02 09:26:36 -08001501
1502 // Don't allow the SW renderer
Robert Phillips72152832017-01-25 17:31:35 -05001503 GrPathRenderer* pr = fRenderTargetContext->drawingManager()->getPathRenderer(
Brian Salomon36aa1762016-12-10 13:24:02 -05001504 canDrawArgs, false, GrPathRendererChain::DrawType::kStencilAndColor);
robertphillips391395d2016-03-02 09:26:36 -08001505 if (!pr) {
1506 return false;
1507 }
1508
1509 GrPaint paint;
1510 paint.setCoverageSetOpXPFactory(op, invert);
1511
Brian Salomon82f44312017-01-11 13:42:54 -05001512 GrPathRenderer::DrawPathArgs args{
Robert Phillips256c37b2017-03-01 14:32:46 -05001513 fRenderTargetContext->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05001514 std::move(paint),
1515 ss,
1516 fRenderTargetContext,
1517 &clip,
1518 &viewMatrix,
1519 &shape,
1520 aaType,
1521 fRenderTargetContext->isGammaCorrect()};
robertphillips391395d2016-03-02 09:26:36 -08001522 pr->drawPath(args);
1523 return true;
1524}
1525
Brian Osman11052242016-10-27 14:47:55 -04001526SkBudgeted GrRenderTargetContextPriv::isBudgeted() const {
robertphillips714712b2016-08-04 06:20:45 -07001527 ASSERT_SINGLE_OWNER_PRIV
1528
Brian Osman11052242016-10-27 14:47:55 -04001529 if (fRenderTargetContext->wasAbandoned()) {
robertphillips714712b2016-08-04 06:20:45 -07001530 return SkBudgeted::kNo;
1531 }
1532
Brian Osman11052242016-10-27 14:47:55 -04001533 SkDEBUGCODE(fRenderTargetContext->validate();)
robertphillips714712b2016-08-04 06:20:45 -07001534
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001535 return fRenderTargetContext->fRenderTargetProxy->isBudgeted();
robertphillips714712b2016-08-04 06:20:45 -07001536}
1537
Brian Osman11052242016-10-27 14:47:55 -04001538void GrRenderTargetContext::internalDrawPath(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001539 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001540 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001541 const SkMatrix& viewMatrix,
1542 const SkPath& path,
1543 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001544 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001545 RETURN_IF_ABANDONED
bsalomon8acedde2016-06-24 10:42:16 -07001546 SkASSERT(!path.isEmpty());
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001547 GrShape shape;
robertphillipsea461502015-05-26 11:38:03 -07001548
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001549 GrAAType aaType = this->decideAAType(aa, /*allowMixedSamples*/ true);
1550 if (style.isSimpleHairline() && aaType == GrAAType::kMixedSamples) {
1551 // NVPR cannot handle hairlines, so this will would get picked up by a different stencil and
1552 // cover path renderer (i.e. default path renderer). The hairline renderer produces much
1553 // smoother hairlines than MSAA.
1554 aaType = GrAAType::kCoverage;
bsalomon0a0f67e2016-06-28 11:56:42 -07001555 }
robertphillips68737822015-10-29 12:12:21 -07001556 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Robert Phillips72152832017-01-25 17:31:35 -05001557 canDrawArgs.fShaderCaps = this->drawingManager()->getContext()->caps()->shaderCaps();
robertphillips68737822015-10-29 12:12:21 -07001558 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07001559 canDrawArgs.fShape = &shape;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001560 canDrawArgs.fHasUserStencilSettings = false;
robertphillips68737822015-10-29 12:12:21 -07001561
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001562 GrPathRenderer* pr;
Brian Salomon82125e92016-12-10 09:35:48 -05001563 static constexpr GrPathRendererChain::DrawType kType = GrPathRendererChain::DrawType::kColor;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001564 do {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001565 shape = GrShape(path, style);
bsalomon8acedde2016-06-24 10:42:16 -07001566 if (shape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001567 return;
1568 }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001569
1570 canDrawArgs.fAAType = aaType;
1571
1572 // Try a 1st time without applying any of the style to the geometry (and barring sw)
Robert Phillips72152832017-01-25 17:31:35 -05001573 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001574 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
1575
1576 if (!pr && shape.style().pathEffect()) {
1577 // It didn't work above, so try again with the path effect applied.
1578 shape = shape.applyStyle(GrStyle::Apply::kPathEffectOnly, styleScale);
bsalomon8acedde2016-06-24 10:42:16 -07001579 if (shape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001580 return;
1581 }
Robert Phillips72152832017-01-25 17:31:35 -05001582 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
bsalomon6663acf2016-05-10 09:14:17 -07001583 }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001584 if (!pr) {
1585 if (shape.style().applies()) {
1586 shape = shape.applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec, styleScale);
1587 if (shape.isEmpty()) {
1588 return;
1589 }
1590 }
1591 // This time, allow SW renderer
Robert Phillips72152832017-01-25 17:31:35 -05001592 pr = this->drawingManager()->getPathRenderer(canDrawArgs, true, kType);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001593 }
Brian Salomon0abc8b42016-12-13 10:22:54 -05001594 if (!pr && GrAATypeIsHW(aaType)) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001595 // There are exceptional cases where we may wind up falling back to coverage based AA
1596 // when the target is MSAA (e.g. through disabling path renderers via GrContextOptions).
1597 aaType = GrAAType::kCoverage;
1598 } else {
1599 break;
1600 }
1601 } while(true);
robertphillipsea461502015-05-26 11:38:03 -07001602
bsalomon8acedde2016-06-24 10:42:16 -07001603 if (!pr) {
robertphillipsea461502015-05-26 11:38:03 -07001604#ifdef SK_DEBUG
1605 SkDebugf("Unable to find path renderer compatible with path.\n");
1606#endif
1607 return;
1608 }
1609
Robert Phillips256c37b2017-03-01 14:32:46 -05001610 GrPathRenderer::DrawPathArgs args{this->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05001611 std::move(paint),
1612 &GrUserStencilSettings::kUnused,
1613 this,
1614 &clip,
1615 &viewMatrix,
1616 &shape,
1617 aaType,
1618 this->isGammaCorrect()};
bsalomon0aff2fa2015-07-31 06:48:27 -07001619 pr->drawPath(args);
robertphillipsea461502015-05-26 11:38:03 -07001620}
1621
Brian Salomon467921e2017-03-06 16:17:12 -05001622static void op_bounds(SkRect* bounds, const GrOp* op) {
1623 *bounds = op->bounds();
1624 if (op->hasZeroArea()) {
1625 if (op->hasAABloat()) {
1626 bounds->outset(0.5f, 0.5f);
1627 } else {
1628 // We don't know which way the particular GPU will snap lines or points at integer
1629 // coords. So we ensure that the bounds is large enough for either snap.
1630 SkRect before = *bounds;
1631 bounds->roundOut(bounds);
1632 if (bounds->fLeft == before.fLeft) {
1633 bounds->fLeft -= 1;
1634 }
1635 if (bounds->fTop == before.fTop) {
1636 bounds->fTop -= 1;
1637 }
1638 if (bounds->fRight == before.fRight) {
1639 bounds->fRight += 1;
1640 }
1641 if (bounds->fBottom == before.fBottom) {
1642 bounds->fBottom += 1;
1643 }
1644 }
1645 }
1646}
1647
Brian Salomon54d212e2017-03-21 14:22:38 -04001648uint32_t GrRenderTargetContext::addDrawOp(const GrClip& clip, std::unique_ptr<GrDrawOp> op) {
joshualitt1de610a2016-01-06 08:26:09 -08001649 ASSERT_SINGLE_OWNER
Robert Phillipsc0138922017-03-08 11:50:55 -05001650 if (this->drawingManager()->wasAbandoned()) {
1651 return SK_InvalidUniqueID;
1652 }
robertphillips2e1e51f2015-10-15 08:01:48 -07001653 SkDEBUGCODE(this->validate();)
Brian Salomon42521e82016-12-07 16:44:58 -05001654 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::addDrawOp");
robertphillips2d70dcb2015-10-06 07:38:23 -07001655
Brian Salomon467921e2017-03-06 16:17:12 -05001656 // Setup clip
1657 SkRect bounds;
1658 op_bounds(&bounds, op.get());
Brian Salomon97180af2017-03-14 13:42:58 -04001659 GrAppliedClip appliedClip;
Brian Salomon54d212e2017-03-21 14:22:38 -04001660 GrDrawOp::FixedFunctionFlags fixedFunctionFlags = op->fixedFunctionFlags();
1661 if (!clip.apply(fContext, this, fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesHWAA,
1662 fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil, &appliedClip,
1663 &bounds)) {
1664 return SK_InvalidUniqueID;
1665 }
1666
1667 // This forces instantiation of the render target.
1668 GrRenderTarget* rt = this->accessRenderTarget();
1669 if (!rt) {
1670 return SK_InvalidUniqueID;
1671 }
1672
1673 if (fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil ||
1674 appliedClip.hasStencilClip()) {
1675 if (!fContext->resourceProvider()->attachStencilAttachment(rt)) {
1676 SkDebugf("ERROR creating stencil attachment. Draw skipped.\n");
1677 return SK_InvalidUniqueID;
1678 }
1679 }
1680
1681 GrXferProcessor::DstTexture dstTexture;
1682 if (op->xpRequiresDstTexture(*this->caps(), &appliedClip)) {
1683 this->setupDstTexture(rt, clip, op->bounds(), &dstTexture);
1684 if (!dstTexture.texture()) {
1685 return SK_InvalidUniqueID;
1686 }
1687 }
1688
1689 op->setClippedBounds(bounds);
1690 return this->getOpList()->addOp(std::move(op), this, std::move(appliedClip), dstTexture);
1691}
1692
1693uint32_t GrRenderTargetContext::addMeshDrawOp(const GrPipelineBuilder& pipelineBuilder,
1694 const GrClip& clip,
1695 std::unique_ptr<GrMeshDrawOp> op) {
1696 ASSERT_SINGLE_OWNER
1697 if (this->drawingManager()->wasAbandoned()) {
1698 return SK_InvalidUniqueID;
1699 }
1700 SkDEBUGCODE(this->validate();)
1701 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::addMeshDrawOp");
1702
1703 // Setup clip
1704 SkRect bounds;
1705 op_bounds(&bounds, op.get());
1706 GrAppliedClip appliedClip;
Brian Salomon467921e2017-03-06 16:17:12 -05001707 if (!clip.apply(fContext, this, pipelineBuilder.isHWAntialias(),
Brian Salomon97180af2017-03-14 13:42:58 -04001708 pipelineBuilder.hasUserStencilSettings(), &appliedClip, &bounds)) {
Robert Phillipsc0138922017-03-08 11:50:55 -05001709 return SK_InvalidUniqueID;
Brian Salomon467921e2017-03-06 16:17:12 -05001710 }
1711
1712 // This forces instantiation of the render target. Pipeline creation is moving to flush time
1713 // by which point instantiation must have occurred anyway.
1714 GrRenderTarget* rt = this->accessRenderTarget();
1715 if (!rt) {
Robert Phillipsc0138922017-03-08 11:50:55 -05001716 return SK_InvalidUniqueID;
Brian Salomon467921e2017-03-06 16:17:12 -05001717 }
1718
1719 GrResourceProvider* resourceProvider = fContext->resourceProvider();
1720 if (pipelineBuilder.hasUserStencilSettings() || appliedClip.hasStencilClip()) {
1721 if (!resourceProvider->attachStencilAttachment(this->accessRenderTarget())) {
1722 SkDebugf("ERROR creating stencil attachment. Draw skipped.\n");
Robert Phillipsc0138922017-03-08 11:50:55 -05001723 return SK_InvalidUniqueID;
Brian Salomon467921e2017-03-06 16:17:12 -05001724 }
1725 }
1726
Brian Salomona811b122017-03-30 08:21:32 -04001727 GrProcessorSet::Analysis analysis;
Brian Salomon8d2f90b2017-03-13 09:11:58 -04001728 op->analyzeProcessors(&analysis, pipelineBuilder.processors(), &appliedClip, *this->caps());
Brian Salomon467921e2017-03-06 16:17:12 -05001729
1730 GrPipeline::InitArgs args;
1731 pipelineBuilder.getPipelineInitArgs(&args);
1732 args.fAppliedClip = &appliedClip;
1733 args.fRenderTarget = rt;
1734 args.fCaps = this->caps();
1735 args.fAnalysis = &analysis;
Brian Salomon1c6025c2017-03-29 14:25:04 -04001736 args.fInputColor = analysis.outputColor();
1737 args.fInputCoverage = analysis.outputCoverage();
Brian Salomon467921e2017-03-06 16:17:12 -05001738
Brian Salomon31853842017-03-28 16:32:05 -04001739 if (analysis.requiresDstTexture()) {
Brian Salomon467921e2017-03-06 16:17:12 -05001740 this->setupDstTexture(rt, clip, bounds, &args.fDstTexture);
1741 if (!args.fDstTexture.texture()) {
Robert Phillipsc0138922017-03-08 11:50:55 -05001742 return SK_InvalidUniqueID;
Brian Salomon467921e2017-03-06 16:17:12 -05001743 }
1744 }
1745 op->initPipeline(args);
1746 // TODO: We need to add pipeline dependencies on textures, etc before recording this op.
Brian Salomon97180af2017-03-14 13:42:58 -04001747 op->setClippedBounds(bounds);
Robert Phillipsc0138922017-03-08 11:50:55 -05001748 return this->getOpList()->addOp(std::move(op), this);
Brian Salomon467921e2017-03-06 16:17:12 -05001749}
1750
1751void GrRenderTargetContext::setupDstTexture(GrRenderTarget* rt, const GrClip& clip,
1752 const SkRect& opBounds,
1753 GrXferProcessor::DstTexture* dstTexture) {
1754 if (this->caps()->textureBarrierSupport()) {
1755 if (GrTexture* rtTex = rt->asTexture()) {
1756 // The render target is a texture, so we can read from it directly in the shader. The XP
1757 // will be responsible to detect this situation and request a texture barrier.
1758 dstTexture->setTexture(sk_ref_sp(rtTex));
1759 dstTexture->setOffset(0, 0);
1760 return;
1761 }
1762 }
1763
1764 SkIRect copyRect;
1765 clip.getConservativeBounds(rt->width(), rt->height(), &copyRect);
1766
1767 SkIRect drawIBounds;
1768 opBounds.roundOut(&drawIBounds);
Brian Salomon859621f2017-03-16 09:21:54 -04001769 // Cover up for any precision issues by outsetting the op bounds a pixel in each direction.
1770 drawIBounds.outset(1, 1);
Brian Salomon467921e2017-03-06 16:17:12 -05001771 if (!copyRect.intersect(drawIBounds)) {
1772#ifdef SK_DEBUG
1773 GrCapsDebugf(this->caps(), "Missed an early reject. "
1774 "Bailing on draw from setupDstTexture.\n");
1775#endif
1776 return;
1777 }
1778
1779 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
1780 // have per-sample dst values by making the copy multisampled.
1781 GrSurfaceDesc desc;
1782 if (!this->caps()->initDescForDstCopy(rt, &desc)) {
1783 desc.fOrigin = kDefault_GrSurfaceOrigin;
1784 desc.fFlags = kRenderTarget_GrSurfaceFlag;
1785 desc.fConfig = rt->config();
1786 }
1787
1788 desc.fWidth = copyRect.width();
1789 desc.fHeight = copyRect.height();
1790
1791 static const uint32_t kFlags = 0;
1792 sk_sp<GrTexture> copy(fContext->resourceProvider()->createApproxTexture(desc, kFlags));
1793
1794 if (!copy) {
1795 SkDebugf("Failed to create temporary copy of destination texture.\n");
1796 return;
1797 }
1798 SkIPoint dstPoint = {0, 0};
1799 this->getOpList()->copySurface(copy.get(), rt, copyRect, dstPoint);
1800 dstTexture->setTexture(std::move(copy));
1801 dstTexture->setOffset(copyRect.fLeft, copyRect.fTop);
robertphillips2334fb62015-06-17 05:43:33 -07001802}