blob: 051af824b0d6033b875a82c4de8461ccf41afa2c [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 Salomon0e8fc8b2016-12-09 15:10:07 -0500510 GrAAType aaType;
robertphillips44302392016-07-08 14:43:03 -0700511
csmartdaltone0d36292016-07-29 08:14:20 -0700512 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport()) {
Robert Phillipsf2361d22016-10-25 14:20:06 -0400513 InstancedRendering* ir = this->getOpList()->instancedRendering();
Brian Salomon649a3412017-03-09 13:50:43 -0500514 std::unique_ptr<GrDrawOp> op = ir->recordRect(croppedRect, viewMatrix, paint.getColor(), aa,
515 fInstancedPipelineInfo, &aaType);
Brian Salomon42521e82016-12-07 16:44:58 -0500516 if (op) {
Brian Salomon82f44312017-01-11 13:42:54 -0500517 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
robertphillips44302392016-07-08 14:43:03 -0700518 if (ss) {
519 pipelineBuilder.setUserStencil(ss);
520 }
Brian Salomon467921e2017-03-06 16:17:12 -0500521 this->addDrawOp(pipelineBuilder, clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700522 return true;
csmartdaltona7f29642016-07-07 08:49:11 -0700523 }
524 }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500525 aaType = this->decideAAType(aa);
526 if (GrAAType::kCoverage == aaType) {
robertphillips391395d2016-03-02 09:26:36 -0800527 // The fill path can handle rotation but not skew.
528 if (view_matrix_ok_for_aa_fill_rect(viewMatrix)) {
529 SkRect devBoundRect;
csmartdalton97f6cd52016-07-13 13:37:08 -0700530 viewMatrix.mapRect(&devBoundRect, croppedRect);
Brian Salomon649a3412017-03-09 13:50:43 -0500531 std::unique_ptr<GrMeshDrawOp> op =
532 GrRectOpFactory::MakeAAFill(paint, viewMatrix, rect, croppedRect, devBoundRect);
Brian Salomon42521e82016-12-07 16:44:58 -0500533 if (op) {
Brian Salomon82f44312017-01-11 13:42:54 -0500534 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
robertphillips44302392016-07-08 14:43:03 -0700535 if (ss) {
536 pipelineBuilder.setUserStencil(ss);
537 }
Brian Salomon649a3412017-03-09 13:50:43 -0500538 this->addMeshDrawOp(pipelineBuilder, clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700539 return true;
540 }
robertphillips391395d2016-03-02 09:26:36 -0800541 }
542 } else {
Brian Salomon82f44312017-01-11 13:42:54 -0500543 this->drawNonAAFilledRect(clip, std::move(paint), viewMatrix, croppedRect, nullptr, nullptr,
544 ss, aaType);
robertphillips44302392016-07-08 14:43:03 -0700545 return true;
robertphillips391395d2016-03-02 09:26:36 -0800546 }
547
robertphillips44302392016-07-08 14:43:03 -0700548 return false;
robertphillips391395d2016-03-02 09:26:36 -0800549}
550
Brian Osman11052242016-10-27 14:47:55 -0400551void GrRenderTargetContext::drawRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500552 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500553 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400554 const SkMatrix& viewMatrix,
555 const SkRect& rect,
556 const GrStyle* style) {
bsalomon6663acf2016-05-10 09:14:17 -0700557 if (!style) {
558 style = &GrStyle::SimpleFill();
559 }
joshualitt1de610a2016-01-06 08:26:09 -0800560 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700561 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700562 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400563 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawRect");
robertphillips2e1e51f2015-10-15 08:01:48 -0700564
bsalomon6663acf2016-05-10 09:14:17 -0700565 // Path effects should've been devolved to a path in SkGpuDevice
566 SkASSERT(!style->pathEffect());
robertphillipsea461502015-05-26 11:38:03 -0700567
Robert Phillips72152832017-01-25 17:31:35 -0500568 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700569
bsalomon6663acf2016-05-10 09:14:17 -0700570 const SkStrokeRec& stroke = style->strokeRec();
robertphillips3ab14ca2016-07-10 11:49:39 -0700571 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
572
573 if (!fContext->caps()->useDrawInsteadOfClear()) {
574 // Check if this is a full RT draw and can be replaced with a clear. We don't bother
575 // checking cases where the RT is fully inside a stroke.
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400576 SkRect rtRect = fRenderTargetProxy->getBoundsRect();
robertphillips3ab14ca2016-07-10 11:49:39 -0700577 // Does the clip contain the entire RT?
578 if (clip.quickContains(rtRect)) {
579 SkMatrix invM;
580 if (!viewMatrix.invert(&invM)) {
robertphillipsea461502015-05-26 11:38:03 -0700581 return;
582 }
robertphillips3ab14ca2016-07-10 11:49:39 -0700583 // Does the rect bound the RT?
584 SkPoint srcSpaceRTQuad[4];
585 invM.mapRectToQuad(srcSpaceRTQuad, rtRect);
586 if (rect_contains_inclusive(rect, srcSpaceRTQuad[0]) &&
587 rect_contains_inclusive(rect, srcSpaceRTQuad[1]) &&
588 rect_contains_inclusive(rect, srcSpaceRTQuad[2]) &&
589 rect_contains_inclusive(rect, srcSpaceRTQuad[3])) {
590 // Will it blend?
591 GrColor clearColor;
592 if (paint.isConstantBlendedColor(&clearColor)) {
robertphillips9199a9f2016-07-13 07:48:43 -0700593 this->clear(nullptr, clearColor, true);
robertphillips3ab14ca2016-07-10 11:49:39 -0700594 return;
595 }
596 }
robertphillipsea461502015-05-26 11:38:03 -0700597 }
598 }
robertphillips44302392016-07-08 14:43:03 -0700599
Brian Salomon82f44312017-01-11 13:42:54 -0500600 if (this->drawFilledRect(clip, std::move(paint), aa, viewMatrix, rect, nullptr)) {
robertphillips44302392016-07-08 14:43:03 -0700601 return;
602 }
bsalomona7d85ba2016-07-06 11:54:59 -0700603 } else if (stroke.getStyle() == SkStrokeRec::kStroke_Style ||
604 stroke.getStyle() == SkStrokeRec::kHairline_Style) {
605 if ((!rect.width() || !rect.height()) &&
606 SkStrokeRec::kHairline_Style != stroke.getStyle()) {
607 SkScalar r = stroke.getWidth() / 2;
608 // TODO: Move these stroke->fill fallbacks to GrShape?
609 switch (stroke.getJoin()) {
610 case SkPaint::kMiter_Join:
Brian Salomon82f44312017-01-11 13:42:54 -0500611 this->drawRect(
612 clip, std::move(paint), aa, viewMatrix,
613 {rect.fLeft - r, rect.fTop - r, rect.fRight + r, rect.fBottom + r},
614 &GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700615 return;
616 case SkPaint::kRound_Join:
617 // Raster draws nothing when both dimensions are empty.
618 if (rect.width() || rect.height()){
619 SkRRect rrect = SkRRect::MakeRectXY(rect.makeOutset(r, r), r, r);
Brian Salomon82f44312017-01-11 13:42:54 -0500620 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect,
621 GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700622 return;
623 }
624 case SkPaint::kBevel_Join:
625 if (!rect.width()) {
Brian Salomon82f44312017-01-11 13:42:54 -0500626 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700627 {rect.fLeft - r, rect.fTop, rect.fRight + r, rect.fBottom},
628 &GrStyle::SimpleFill());
629 } else {
Brian Salomon82f44312017-01-11 13:42:54 -0500630 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700631 {rect.fLeft, rect.fTop - r, rect.fRight, rect.fBottom + r},
632 &GrStyle::SimpleFill());
633 }
634 return;
635 }
636 }
robertphillips44302392016-07-08 14:43:03 -0700637
robertphillips44302392016-07-08 14:43:03 -0700638 bool snapToPixelCenters = false;
Brian Salomon649a3412017-03-09 13:50:43 -0500639 std::unique_ptr<GrMeshDrawOp> op;
robertphillips44302392016-07-08 14:43:03 -0700640
robertphillips391395d2016-03-02 09:26:36 -0800641 GrColor color = paint.getColor();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500642 GrAAType aaType = this->decideAAType(aa);
643 if (GrAAType::kCoverage == aaType) {
cdaltonbb539482016-01-04 09:48:25 -0800644 // The stroke path needs the rect to remain axis aligned (no rotation or skew).
645 if (viewMatrix.rectStaysRect()) {
Brian Salomon6a639042016-12-14 11:08:17 -0500646 op = GrRectOpFactory::MakeAAStroke(color, viewMatrix, rect, stroke);
cdaltonbb539482016-01-04 09:48:25 -0800647 }
robertphillipsea461502015-05-26 11:38:03 -0700648 } else {
robertphillips391395d2016-03-02 09:26:36 -0800649 // Depending on sub-pixel coordinates and the particular GPU, we may lose a corner of
halcanary9d524f22016-03-29 09:03:52 -0700650 // hairline rects. We jam all the vertices to pixel centers to avoid this, but not
robertphillips391395d2016-03-02 09:26:36 -0800651 // when MSAA is enabled because it can cause ugly artifacts.
bsalomona7d85ba2016-07-06 11:54:59 -0700652 snapToPixelCenters = stroke.getStyle() == SkStrokeRec::kHairline_Style &&
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400653 !fRenderTargetProxy->isUnifiedMultisampled();
Brian Salomon6a639042016-12-14 11:08:17 -0500654 op = GrRectOpFactory::MakeNonAAStroke(color, viewMatrix, rect, stroke,
655 snapToPixelCenters);
robertphillips391395d2016-03-02 09:26:36 -0800656 }
robertphillips4bc31812016-03-01 12:22:49 -0800657
Brian Salomon42521e82016-12-07 16:44:58 -0500658 if (op) {
Brian Salomon82f44312017-01-11 13:42:54 -0500659 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
Brian Salomon189098e72017-01-19 09:55:19 -0500660 pipelineBuilder.setSnapVerticesToPixelCenters(snapToPixelCenters);
Brian Salomon649a3412017-03-09 13:50:43 -0500661 this->addMeshDrawOp(pipelineBuilder, clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700662 return;
robertphillips4bc31812016-03-01 12:22:49 -0800663 }
robertphillips4bc31812016-03-01 12:22:49 -0800664 }
halcanary9d524f22016-03-29 09:03:52 -0700665
robertphillips4bc31812016-03-01 12:22:49 -0800666 SkPath path;
667 path.setIsVolatile(true);
668 path.addRect(rect);
Brian Salomon82f44312017-01-11 13:42:54 -0500669 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, *style);
robertphillipsea461502015-05-26 11:38:03 -0700670}
671
Robert Phillipsec2249f2016-11-09 08:54:35 -0500672int GrRenderTargetContextPriv::maxWindowRectangles() const {
673 return fRenderTargetContext->fRenderTargetProxy->maxWindowRectangles(
674 *fRenderTargetContext->fContext->caps());
675}
676
Brian Osman11052242016-10-27 14:47:55 -0400677void GrRenderTargetContextPriv::clearStencilClip(const GrFixedClip& clip, bool insideStencilMask) {
robertphillips976f5f02016-06-03 10:59:20 -0700678 ASSERT_SINGLE_OWNER_PRIV
679 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400680 SkDEBUGCODE(fRenderTargetContext->validate();)
681 GR_AUDIT_TRAIL_AUTO_FRAME(fRenderTargetContext->fAuditTrail,
682 "GrRenderTargetContextPriv::clearStencilClip");
robertphillips976f5f02016-06-03 10:59:20 -0700683
Robert Phillips72152832017-01-25 17:31:35 -0500684 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Brian Salomon69868af2016-12-22 15:42:51 -0500685 // TODO: This needs to be fixed up since it ends the deferral of the GrRenderTarget.
Robert Phillipse60ad622016-11-17 10:22:48 -0500686 if (!fRenderTargetContext->accessRenderTarget()) {
687 return;
688 }
Brian Osman11052242016-10-27 14:47:55 -0400689 fRenderTargetContext->getOpList()->clearStencilClip(clip, insideStencilMask,
Brian Salomon69868af2016-12-22 15:42:51 -0500690 fRenderTargetContext);
robertphillips976f5f02016-06-03 10:59:20 -0700691}
692
Brian Osman11052242016-10-27 14:47:55 -0400693void GrRenderTargetContextPriv::stencilPath(const GrClip& clip,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500694 GrAAType aaType,
Brian Osman11052242016-10-27 14:47:55 -0400695 const SkMatrix& viewMatrix,
696 const GrPath* path) {
Brian Salomon467921e2017-03-06 16:17:12 -0500697 ASSERT_SINGLE_OWNER_PRIV
698 RETURN_IF_ABANDONED_PRIV
699 SkDEBUGCODE(fRenderTargetContext->validate();)
700 GR_AUDIT_TRAIL_AUTO_FRAME(fRenderTargetContext->fAuditTrail,
701 "GrRenderTargetContext::stencilPath");
702
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500703 SkASSERT(aaType != GrAAType::kCoverage);
Brian Salomon467921e2017-03-06 16:17:12 -0500704
705 bool useHWAA = GrAATypeIsHW(aaType);
706 // TODO: extract portions of checkDraw that are relevant to path stenciling.
707 SkASSERT(path);
708 SkASSERT(fRenderTargetContext->caps()->shaderCaps()->pathRenderingSupport());
709
710 // FIXME: Use path bounds instead of this WAR once
711 // https://bugs.chromium.org/p/skia/issues/detail?id=5640 is resolved.
712 SkRect bounds = SkRect::MakeIWH(fRenderTargetContext->width(), fRenderTargetContext->height());
713
714 // Setup clip
715 GrAppliedClip appliedClip(bounds);
716 if (!clip.apply(fRenderTargetContext->fContext, fRenderTargetContext, useHWAA, true,
717 &appliedClip)) {
718 return;
719 }
720
721 // Coverage AA does not make sense when rendering to the stencil buffer. The caller should never
722 // attempt this in a situation that would require coverage AA.
723 SkASSERT(!appliedClip.clipCoverageFragmentProcessor());
724
725 GrRenderTarget* rt = fRenderTargetContext->accessRenderTarget();
726 if (!rt) {
727 return;
728 }
729 GrStencilAttachment* stencilAttachment =
730 fRenderTargetContext->fContext->resourceProvider()->attachStencilAttachment(rt);
731 if (!stencilAttachment) {
732 SkDebugf("ERROR creating stencil attachment. Draw skipped.\n");
733 return;
734 }
735
736 std::unique_ptr<GrOp> op = GrStencilPathOp::Make(viewMatrix,
737 useHWAA,
738 path->getFillType(),
739 appliedClip.hasStencilClip(),
740 stencilAttachment->bits(),
741 appliedClip.scissorState(),
742 fRenderTargetContext->accessRenderTarget(),
743 path);
744 op->setClippedBounds(appliedClip.clippedDrawBounds());
745 fRenderTargetContext->getOpList()->recordOp(std::move(op), fRenderTargetContext);
robertphillips976f5f02016-06-03 10:59:20 -0700746}
747
Brian Osman11052242016-10-27 14:47:55 -0400748void GrRenderTargetContextPriv::stencilRect(const GrClip& clip,
749 const GrUserStencilSettings* ss,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500750 GrAAType aaType,
Brian Osman11052242016-10-27 14:47:55 -0400751 const SkMatrix& viewMatrix,
752 const SkRect& rect) {
robertphillips976f5f02016-06-03 10:59:20 -0700753 ASSERT_SINGLE_OWNER_PRIV
754 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400755 SkDEBUGCODE(fRenderTargetContext->validate();)
756 GR_AUDIT_TRAIL_AUTO_FRAME(fRenderTargetContext->fAuditTrail,
757 "GrRenderTargetContext::stencilRect");
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500758 SkASSERT(GrAAType::kCoverage != aaType);
Robert Phillips72152832017-01-25 17:31:35 -0500759 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips976f5f02016-06-03 10:59:20 -0700760
761 GrPaint paint;
Brian Salomona1633922017-01-09 11:46:10 -0500762 paint.setXPFactory(GrDisableColorXPFactory::Get());
robertphillips976f5f02016-06-03 10:59:20 -0700763
Brian Salomon82f44312017-01-11 13:42:54 -0500764 fRenderTargetContext->drawNonAAFilledRect(clip, std::move(paint), viewMatrix, rect, nullptr,
765 nullptr, ss, aaType);
robertphillips976f5f02016-06-03 10:59:20 -0700766}
767
Brian Osman11052242016-10-27 14:47:55 -0400768bool GrRenderTargetContextPriv::drawAndStencilRect(const GrClip& clip,
769 const GrUserStencilSettings* ss,
770 SkRegion::Op op,
771 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500772 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400773 const SkMatrix& viewMatrix,
774 const SkRect& rect) {
robertphillips391395d2016-03-02 09:26:36 -0800775 ASSERT_SINGLE_OWNER_PRIV
776 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400777 SkDEBUGCODE(fRenderTargetContext->validate();)
778 GR_AUDIT_TRAIL_AUTO_FRAME(fRenderTargetContext->fAuditTrail,
779 "GrRenderTargetContext::drawAndStencilRect");
robertphillips391395d2016-03-02 09:26:36 -0800780
Robert Phillips72152832017-01-25 17:31:35 -0500781 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -0800782
783 GrPaint paint;
robertphillips391395d2016-03-02 09:26:36 -0800784 paint.setCoverageSetOpXPFactory(op, invert);
785
Brian Salomon82f44312017-01-11 13:42:54 -0500786 if (fRenderTargetContext->drawFilledRect(clip, std::move(paint), aa, viewMatrix, rect, ss)) {
robertphillips391395d2016-03-02 09:26:36 -0800787 return true;
788 }
robertphillips391395d2016-03-02 09:26:36 -0800789 SkPath path;
790 path.setIsVolatile(true);
791 path.addRect(rect);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500792 return this->drawAndStencilPath(clip, ss, op, invert, aa, viewMatrix, path);
robertphillips391395d2016-03-02 09:26:36 -0800793}
794
Brian Osman11052242016-10-27 14:47:55 -0400795void GrRenderTargetContext::fillRectToRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500796 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500797 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400798 const SkMatrix& viewMatrix,
799 const SkRect& rectToDraw,
800 const SkRect& localRect) {
joshualitt1de610a2016-01-06 08:26:09 -0800801 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700802 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700803 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400804 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::fillRectToRect");
robertphillipsea461502015-05-26 11:38:03 -0700805
csmartdalton97f6cd52016-07-13 13:37:08 -0700806 SkRect croppedRect = rectToDraw;
807 SkRect croppedLocalRect = localRect;
robertphillips13a7eee2016-08-31 15:06:24 -0700808 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix,
809 &croppedRect, &croppedLocalRect)) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700810 return;
811 }
812
Robert Phillips72152832017-01-25 17:31:35 -0500813 AutoCheckFlush acf(this->drawingManager());
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500814 GrAAType aaType;
csmartdaltona7f29642016-07-07 08:49:11 -0700815
csmartdaltone0d36292016-07-29 08:14:20 -0700816 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport()) {
Robert Phillipsf2361d22016-10-25 14:20:06 -0400817 InstancedRendering* ir = this->getOpList()->instancedRendering();
Brian Salomonf8334782017-01-03 09:42:58 -0500818 std::unique_ptr<GrDrawOp> op(ir->recordRect(croppedRect, viewMatrix, paint.getColor(),
819 croppedLocalRect, aa, fInstancedPipelineInfo,
820 &aaType));
Brian Salomon42521e82016-12-07 16:44:58 -0500821 if (op) {
Brian Salomon82f44312017-01-11 13:42:54 -0500822 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
Brian Salomon467921e2017-03-06 16:17:12 -0500823 this->addDrawOp(pipelineBuilder, clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -0700824 return;
825 }
826 }
827
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500828 aaType = this->decideAAType(aa);
829 if (GrAAType::kCoverage != aaType) {
Brian Salomon82f44312017-01-11 13:42:54 -0500830 this->drawNonAAFilledRect(clip, std::move(paint), viewMatrix, croppedRect,
831 &croppedLocalRect, nullptr, nullptr, aaType);
csmartdaltonfc49d562016-07-26 17:05:47 -0700832 return;
joshualitt04194f32016-01-13 10:08:27 -0800833 }
bsalomonbb243832016-07-22 07:10:19 -0700834
csmartdaltonfc49d562016-07-26 17:05:47 -0700835 if (view_matrix_ok_for_aa_fill_rect(viewMatrix)) {
Brian Salomonf8334782017-01-03 09:42:58 -0500836 std::unique_ptr<GrDrawOp> op = GrAAFillRectOp::MakeWithLocalRect(
837 paint.getColor(), viewMatrix, croppedRect, croppedLocalRect);
Brian Salomon82f44312017-01-11 13:42:54 -0500838 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
Brian Salomon24f19782016-12-13 15:10:11 -0500839 this->addDrawOp(pipelineBuilder, clip, std::move(op));
csmartdaltonfc49d562016-07-26 17:05:47 -0700840 return;
841 }
842
843 SkMatrix viewAndUnLocalMatrix;
844 if (!viewAndUnLocalMatrix.setRectToRect(localRect, rectToDraw, SkMatrix::kFill_ScaleToFit)) {
845 SkDebugf("fillRectToRect called with empty local matrix.\n");
846 return;
847 }
848 viewAndUnLocalMatrix.postConcat(viewMatrix);
849
850 SkPath path;
851 path.setIsVolatile(true);
852 path.addRect(localRect);
Brian Salomon82f44312017-01-11 13:42:54 -0500853 this->internalDrawPath(clip, std::move(paint), aa, viewAndUnLocalMatrix, path, GrStyle());
joshualittb6b513b2015-08-21 10:25:18 -0700854}
855
Brian Osman11052242016-10-27 14:47:55 -0400856void GrRenderTargetContext::fillRectWithLocalMatrix(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500857 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500858 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400859 const SkMatrix& viewMatrix,
860 const SkRect& rectToDraw,
861 const SkMatrix& localMatrix) {
joshualitt1de610a2016-01-06 08:26:09 -0800862 ASSERT_SINGLE_OWNER
joshualittb6b513b2015-08-21 10:25:18 -0700863 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700864 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400865 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::fillRectWithLocalMatrix");
joshualittb6b513b2015-08-21 10:25:18 -0700866
csmartdalton97f6cd52016-07-13 13:37:08 -0700867 SkRect croppedRect = rectToDraw;
robertphillips13a7eee2016-08-31 15:06:24 -0700868 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix, &croppedRect)) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700869 return;
870 }
871
Robert Phillips72152832017-01-25 17:31:35 -0500872 AutoCheckFlush acf(this->drawingManager());
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500873 GrAAType aaType;
csmartdaltona7f29642016-07-07 08:49:11 -0700874
csmartdaltone0d36292016-07-29 08:14:20 -0700875 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport()) {
Robert Phillipsf2361d22016-10-25 14:20:06 -0400876 InstancedRendering* ir = this->getOpList()->instancedRendering();
Brian Salomonf8334782017-01-03 09:42:58 -0500877 std::unique_ptr<GrDrawOp> op(ir->recordRect(croppedRect, viewMatrix, paint.getColor(),
878 localMatrix, aa, fInstancedPipelineInfo,
879 &aaType));
Brian Salomon42521e82016-12-07 16:44:58 -0500880 if (op) {
Brian Salomon82f44312017-01-11 13:42:54 -0500881 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
Brian Salomon467921e2017-03-06 16:17:12 -0500882 this->addDrawOp(pipelineBuilder, clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -0700883 return;
884 }
885 }
886
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500887 aaType = this->decideAAType(aa);
888 if (GrAAType::kCoverage != aaType) {
Brian Salomon82f44312017-01-11 13:42:54 -0500889 this->drawNonAAFilledRect(clip, std::move(paint), viewMatrix, croppedRect, nullptr,
890 &localMatrix, nullptr, aaType);
csmartdaltonfc49d562016-07-26 17:05:47 -0700891 return;
bsalomonc55271f2015-11-09 11:55:57 -0800892 }
robertphillips4bc31812016-03-01 12:22:49 -0800893
csmartdaltonfc49d562016-07-26 17:05:47 -0700894 if (view_matrix_ok_for_aa_fill_rect(viewMatrix)) {
Brian Salomon649a3412017-03-09 13:50:43 -0500895 std::unique_ptr<GrMeshDrawOp> op =
Brian Salomon6a639042016-12-14 11:08:17 -0500896 GrAAFillRectOp::Make(paint.getColor(), viewMatrix, localMatrix, croppedRect);
Brian Salomon82f44312017-01-11 13:42:54 -0500897 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
Brian Salomon649a3412017-03-09 13:50:43 -0500898 this->addMeshDrawOp(pipelineBuilder, clip, std::move(op));
csmartdaltonfc49d562016-07-26 17:05:47 -0700899 return;
900 }
901
902 SkMatrix viewAndUnLocalMatrix;
903 if (!localMatrix.invert(&viewAndUnLocalMatrix)) {
904 SkDebugf("fillRectWithLocalMatrix called with degenerate local matrix.\n");
905 return;
906 }
907 viewAndUnLocalMatrix.postConcat(viewMatrix);
908
909 SkPath path;
910 path.setIsVolatile(true);
911 path.addRect(rectToDraw);
912 path.transform(localMatrix);
Brian Salomon82f44312017-01-11 13:42:54 -0500913 this->internalDrawPath(clip, std::move(paint), aa, viewAndUnLocalMatrix, path, GrStyle());
robertphillipsea461502015-05-26 11:38:03 -0700914}
915
Brian Osman11052242016-10-27 14:47:55 -0400916void GrRenderTargetContext::drawVertices(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500917 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400918 const SkMatrix& viewMatrix,
919 GrPrimitiveType primitiveType,
920 int vertexCount,
921 const SkPoint positions[],
922 const SkPoint texCoords[],
Brian Salomon3de0aee2017-01-29 09:34:17 -0500923 const uint32_t colors[],
Brian Osman11052242016-10-27 14:47:55 -0400924 const uint16_t indices[],
Brian Salomon3de0aee2017-01-29 09:34:17 -0500925 int indexCount,
926 ColorArrayType colorArrayType) {
joshualitt1de610a2016-01-06 08:26:09 -0800927 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700928 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700929 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400930 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawVertices");
robertphillipsea461502015-05-26 11:38:03 -0700931
Robert Phillips72152832017-01-25 17:31:35 -0500932 AutoCheckFlush acf(this->drawingManager());
robertphillips2e1e51f2015-10-15 08:01:48 -0700933
robertphillipsea461502015-05-26 11:38:03 -0700934 // TODO clients should give us bounds
935 SkRect bounds;
936 if (!bounds.setBoundsCheck(positions, vertexCount)) {
937 SkDebugf("drawVertices call empty bounds\n");
938 return;
939 }
940
Brian Salomon649a3412017-03-09 13:50:43 -0500941 std::unique_ptr<GrMeshDrawOp> op = GrDrawVerticesOp::Make(
Brian Salomon3de0aee2017-01-29 09:34:17 -0500942 paint.getColor(), primitiveType, viewMatrix, positions, vertexCount, indices,
943 indexCount, colors, texCoords, bounds, colorArrayType);
Brian Salomon199fb872017-02-06 09:41:10 -0500944 if (!op) {
945 return;
946 }
947 GrPipelineBuilder pipelineBuilder(std::move(paint), GrAAType::kNone);
Brian Salomon649a3412017-03-09 13:50:43 -0500948 this->addMeshDrawOp(pipelineBuilder, clip, std::move(op));
Brian Salomon199fb872017-02-06 09:41:10 -0500949}
robertphillipsea461502015-05-26 11:38:03 -0700950
Brian Salomon199fb872017-02-06 09:41:10 -0500951void GrRenderTargetContext::drawVertices(const GrClip& clip,
952 GrPaint&& paint,
953 const SkMatrix& viewMatrix,
954 sk_sp<SkVertices> vertices,
955 uint32_t flags) {
956 ASSERT_SINGLE_OWNER
957 RETURN_IF_ABANDONED
958 SkDEBUGCODE(this->validate();)
959 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawVertices");
960
961 AutoCheckFlush acf(this->drawingManager());
962
963 SkASSERT(vertices);
Brian Salomon649a3412017-03-09 13:50:43 -0500964 std::unique_ptr<GrMeshDrawOp> op =
Brian Salomon199fb872017-02-06 09:41:10 -0500965 GrDrawVerticesOp::Make(paint.getColor(), std::move(vertices), viewMatrix, flags);
966 if (!op) {
967 return;
968 }
Brian Salomon82f44312017-01-11 13:42:54 -0500969 GrPipelineBuilder pipelineBuilder(std::move(paint), GrAAType::kNone);
Brian Salomon649a3412017-03-09 13:50:43 -0500970 this->addMeshDrawOp(pipelineBuilder, clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -0700971}
972
973///////////////////////////////////////////////////////////////////////////////
974
Brian Osman11052242016-10-27 14:47:55 -0400975void GrRenderTargetContext::drawAtlas(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500976 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400977 const SkMatrix& viewMatrix,
978 int spriteCount,
979 const SkRSXform xform[],
980 const SkRect texRect[],
981 const SkColor colors[]) {
joshualitt1de610a2016-01-06 08:26:09 -0800982 ASSERT_SINGLE_OWNER
jvanverth31ff7622015-08-07 10:09:28 -0700983 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700984 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400985 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawAtlas");
robertphillips2e1e51f2015-10-15 08:01:48 -0700986
Robert Phillips72152832017-01-25 17:31:35 -0500987 AutoCheckFlush acf(this->drawingManager());
halcanary9d524f22016-03-29 09:03:52 -0700988
Brian Salomon649a3412017-03-09 13:50:43 -0500989 std::unique_ptr<GrMeshDrawOp> op =
Brian Salomonfc527d22016-12-14 21:07:01 -0500990 GrDrawAtlasOp::Make(paint.getColor(), viewMatrix, spriteCount, xform, texRect, colors);
Brian Salomon82f44312017-01-11 13:42:54 -0500991 GrPipelineBuilder pipelineBuilder(std::move(paint), GrAAType::kNone);
Brian Salomon649a3412017-03-09 13:50:43 -0500992 this->addMeshDrawOp(pipelineBuilder, clip, std::move(op));
jvanverth31ff7622015-08-07 10:09:28 -0700993}
994
995///////////////////////////////////////////////////////////////////////////////
996
Brian Osman11052242016-10-27 14:47:55 -0400997void GrRenderTargetContext::drawRRect(const GrClip& origClip,
Brian Salomon82f44312017-01-11 13:42:54 -0500998 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500999 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001000 const SkMatrix& viewMatrix,
1001 const SkRRect& rrect,
1002 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001003 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001004 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001005 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -04001006 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawRRect");
robertphillipsea461502015-05-26 11:38:03 -07001007 if (rrect.isEmpty()) {
1008 return;
1009 }
1010
bsalomon7f0d9f32016-08-15 14:49:10 -07001011 GrNoClip noclip;
1012 const GrClip* clip = &origClip;
1013#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
1014 // The Android framework frequently clips rrects to themselves where the clip is non-aa and the
Brian Salomon42521e82016-12-07 16:44:58 -05001015 // draw is aa. Since our lower level clip code works from op bounds, which are SkRects, it
bsalomon7f0d9f32016-08-15 14:49:10 -07001016 // doesn't detect that the clip can be ignored (modulo antialiasing). The following test
1017 // attempts to mitigate the stencil clip cost but will only help when the entire clip stack
1018 // can be ignored. We'd prefer to fix this in the framework by removing the clips calls.
1019 SkRRect devRRect;
1020 if (rrect.transform(viewMatrix, &devRRect) && clip->quickContains(devRRect)) {
1021 clip = &noclip;
1022 }
1023#endif
bsalomon6663acf2016-05-10 09:14:17 -07001024 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
ksakamotoec7f2ac2016-07-05 03:54:53 -07001025
Robert Phillips72152832017-01-25 17:31:35 -05001026 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001027 const SkStrokeRec stroke = style.strokeRec();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001028 GrAAType aaType;
csmartdaltona7f29642016-07-07 08:49:11 -07001029
csmartdaltone0d36292016-07-29 08:14:20 -07001030 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport() &&
1031 stroke.isFillStyle()) {
Robert Phillipsf2361d22016-10-25 14:20:06 -04001032 InstancedRendering* ir = this->getOpList()->instancedRendering();
Brian Salomonf8334782017-01-03 09:42:58 -05001033 std::unique_ptr<GrDrawOp> op(ir->recordRRect(rrect, viewMatrix, paint.getColor(), aa,
1034 fInstancedPipelineInfo, &aaType));
Brian Salomon42521e82016-12-07 16:44:58 -05001035 if (op) {
Brian Salomon82f44312017-01-11 13:42:54 -05001036 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
Brian Salomon467921e2017-03-06 16:17:12 -05001037 this->addDrawOp(pipelineBuilder, *clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -07001038 return;
1039 }
1040 }
1041
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001042 aaType = this->decideAAType(aa);
1043 if (GrAAType::kCoverage == aaType) {
Brian Salomon94efbf52016-11-29 13:43:05 -05001044 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomon649a3412017-03-09 13:50:43 -05001045 std::unique_ptr<GrMeshDrawOp> op =
1046 GrOvalOpFactory::MakeRRectOp(paint.getColor(),
1047 paint.usesDistanceVectorField(),
1048 viewMatrix,
1049 rrect,
1050 stroke,
1051 shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -05001052 if (op) {
Brian Salomon82f44312017-01-11 13:42:54 -05001053 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
Brian Salomon649a3412017-03-09 13:50:43 -05001054 this->addMeshDrawOp(pipelineBuilder, *clip, std::move(op));
robertphillipsb56f9272016-02-25 11:03:52 -08001055 return;
1056 }
robertphillipsea461502015-05-26 11:38:03 -07001057 }
robertphillipsb56f9272016-02-25 11:03:52 -08001058
1059 SkPath path;
1060 path.setIsVolatile(true);
1061 path.addRRect(rrect);
Brian Salomon82f44312017-01-11 13:42:54 -05001062 this->internalDrawPath(*clip, std::move(paint), aa, viewMatrix, path, style);
robertphillipsea461502015-05-26 11:38:03 -07001063}
1064
Jim Van Verthc5903412016-11-17 15:27:09 -05001065///////////////////////////////////////////////////////////////////////////////
1066
1067void GrRenderTargetContext::drawShadowRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001068 GrPaint&& paint,
Jim Van Verthc5903412016-11-17 15:27:09 -05001069 const SkMatrix& viewMatrix,
1070 const SkRRect& rrect,
1071 SkScalar blurRadius,
1072 const GrStyle& style) {
1073 ASSERT_SINGLE_OWNER
1074 RETURN_IF_ABANDONED
1075 SkDEBUGCODE(this->validate();)
1076 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawShadowRRect");
1077 if (rrect.isEmpty()) {
1078 return;
1079 }
1080
1081 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
1082
Robert Phillips72152832017-01-25 17:31:35 -05001083 AutoCheckFlush acf(this->drawingManager());
Jim Van Verthc5903412016-11-17 15:27:09 -05001084 const SkStrokeRec stroke = style.strokeRec();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001085 // TODO: add instancing support?
Jim Van Verthc5903412016-11-17 15:27:09 -05001086
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001087 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomon649a3412017-03-09 13:50:43 -05001088 std::unique_ptr<GrMeshDrawOp> op = GrShadowRRectOp::Make(paint.getColor(), viewMatrix, rrect,
1089 blurRadius, stroke, shaderCaps);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001090 if (op) {
Brian Salomon82f44312017-01-11 13:42:54 -05001091 GrPipelineBuilder pipelineBuilder(std::move(paint), GrAAType::kNone);
Brian Salomon649a3412017-03-09 13:50:43 -05001092 this->addMeshDrawOp(pipelineBuilder, clip, std::move(op));
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001093 return;
Jim Van Verthc5903412016-11-17 15:27:09 -05001094 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001095}
1096
1097///////////////////////////////////////////////////////////////////////////////
1098
Brian Osman11052242016-10-27 14:47:55 -04001099bool GrRenderTargetContext::drawFilledDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001100 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001101 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001102 const SkMatrix& viewMatrix,
1103 const SkRRect& origOuter,
1104 const SkRRect& origInner) {
robertphillips00095892016-02-29 13:50:40 -08001105 SkASSERT(!origInner.isEmpty());
1106 SkASSERT(!origOuter.isEmpty());
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001107 GrAAType aaType;
robertphillips00095892016-02-29 13:50:40 -08001108
csmartdaltone0d36292016-07-29 08:14:20 -07001109 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport()) {
Robert Phillipsf2361d22016-10-25 14:20:06 -04001110 InstancedRendering* ir = this->getOpList()->instancedRendering();
Brian Salomonf8334782017-01-03 09:42:58 -05001111 std::unique_ptr<GrDrawOp> op(ir->recordDRRect(origOuter, origInner, viewMatrix,
Brian Salomon82f44312017-01-11 13:42:54 -05001112 paint.getColor(), aa, fInstancedPipelineInfo,
1113 &aaType));
Brian Salomon42521e82016-12-07 16:44:58 -05001114 if (op) {
Brian Salomon82f44312017-01-11 13:42:54 -05001115 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
Brian Salomon467921e2017-03-06 16:17:12 -05001116 this->addDrawOp(pipelineBuilder, clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -07001117 return true;
1118 }
1119 }
1120
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001121 aaType = this->decideAAType(aa);
robertphillips00095892016-02-29 13:50:40 -08001122
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001123 GrPrimitiveEdgeType innerEdgeType, outerEdgeType;
1124 if (GrAAType::kCoverage == aaType) {
1125 innerEdgeType = kInverseFillAA_GrProcessorEdgeType;
1126 outerEdgeType = kFillAA_GrProcessorEdgeType;
1127 } else {
1128 innerEdgeType = kInverseFillBW_GrProcessorEdgeType;
1129 outerEdgeType = kFillBW_GrProcessorEdgeType;
1130 }
robertphillips00095892016-02-29 13:50:40 -08001131
1132 SkTCopyOnFirstWrite<SkRRect> inner(origInner), outer(origOuter);
1133 SkMatrix inverseVM;
1134 if (!viewMatrix.isIdentity()) {
1135 if (!origInner.transform(viewMatrix, inner.writable())) {
1136 return false;
1137 }
1138 if (!origOuter.transform(viewMatrix, outer.writable())) {
1139 return false;
1140 }
1141 if (!viewMatrix.invert(&inverseVM)) {
1142 return false;
1143 }
1144 } else {
1145 inverseVM.reset();
halcanary9d524f22016-03-29 09:03:52 -07001146 }
robertphillips00095892016-02-29 13:50:40 -08001147
robertphillips00095892016-02-29 13:50:40 -08001148 // TODO these need to be a geometry processors
bungeman06ca8ec2016-06-09 08:01:03 -07001149 sk_sp<GrFragmentProcessor> innerEffect(GrRRectEffect::Make(innerEdgeType, *inner));
robertphillips00095892016-02-29 13:50:40 -08001150 if (!innerEffect) {
1151 return false;
1152 }
1153
bungeman06ca8ec2016-06-09 08:01:03 -07001154 sk_sp<GrFragmentProcessor> outerEffect(GrRRectEffect::Make(outerEdgeType, *outer));
robertphillips00095892016-02-29 13:50:40 -08001155 if (!outerEffect) {
1156 return false;
1157 }
1158
Brian Salomon82f44312017-01-11 13:42:54 -05001159 paint.addCoverageFragmentProcessor(std::move(innerEffect));
1160 paint.addCoverageFragmentProcessor(std::move(outerEffect));
robertphillips00095892016-02-29 13:50:40 -08001161
1162 SkRect bounds = outer->getBounds();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001163 if (GrAAType::kCoverage == aaType) {
robertphillips00095892016-02-29 13:50:40 -08001164 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1165 }
halcanary9d524f22016-03-29 09:03:52 -07001166
Brian Salomon82f44312017-01-11 13:42:54 -05001167 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), bounds,
1168 inverseVM);
robertphillips00095892016-02-29 13:50:40 -08001169 return true;
1170}
1171
Brian Osman11052242016-10-27 14:47:55 -04001172void GrRenderTargetContext::drawDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001173 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001174 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001175 const SkMatrix& viewMatrix,
1176 const SkRRect& outer,
1177 const SkRRect& inner) {
robertphillips00095892016-02-29 13:50:40 -08001178 ASSERT_SINGLE_OWNER
1179 RETURN_IF_ABANDONED
1180 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -04001181 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawDRRect");
robertphillips00095892016-02-29 13:50:40 -08001182
1183 SkASSERT(!outer.isEmpty());
1184 SkASSERT(!inner.isEmpty());
1185
Robert Phillips72152832017-01-25 17:31:35 -05001186 AutoCheckFlush acf(this->drawingManager());
robertphillips00095892016-02-29 13:50:40 -08001187
Brian Salomon82f44312017-01-11 13:42:54 -05001188 if (this->drawFilledDRRect(clip, std::move(paint), aa, viewMatrix, outer, inner)) {
robertphillips00095892016-02-29 13:50:40 -08001189 return;
1190 }
1191
1192 SkPath path;
1193 path.setIsVolatile(true);
1194 path.addRRect(inner);
1195 path.addRRect(outer);
1196 path.setFillType(SkPath::kEvenOdd_FillType);
1197
Brian Salomon82f44312017-01-11 13:42:54 -05001198 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, GrStyle::SimpleFill());
robertphillips00095892016-02-29 13:50:40 -08001199}
1200
robertphillipsea461502015-05-26 11:38:03 -07001201///////////////////////////////////////////////////////////////////////////////
1202
msarettcc319b92016-08-25 18:07:18 -07001203static inline bool is_int(float x) {
1204 return x == (float) sk_float_round2int(x);
1205}
1206
Brian Osman11052242016-10-27 14:47:55 -04001207void GrRenderTargetContext::drawRegion(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001208 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001209 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001210 const SkMatrix& viewMatrix,
1211 const SkRegion& region,
1212 const GrStyle& style) {
msarettcc319b92016-08-25 18:07:18 -07001213 ASSERT_SINGLE_OWNER
1214 RETURN_IF_ABANDONED
1215 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -04001216 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawRegion");
msarettcc319b92016-08-25 18:07:18 -07001217
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001218 if (GrAA::kYes == aa) {
Brian Salomonfc527d22016-12-14 21:07:01 -05001219 // GrRegionOp performs no antialiasing but is much faster, so here we check the matrix
Brian Salomonc57c7c92016-12-06 14:47:34 -05001220 // to see whether aa is really required.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001221 if (!SkToBool(viewMatrix.getType() & ~(SkMatrix::kTranslate_Mask)) &&
1222 is_int(viewMatrix.getTranslateX()) &&
1223 is_int(viewMatrix.getTranslateY())) {
1224 aa = GrAA::kNo;
1225 }
Brian Salomonc57c7c92016-12-06 14:47:34 -05001226 }
msarettcc319b92016-08-25 18:07:18 -07001227 bool complexStyle = !style.isSimpleFill();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001228 if (complexStyle || GrAA::kYes == aa) {
msarettcc319b92016-08-25 18:07:18 -07001229 SkPath path;
1230 region.getBoundaryPath(&path);
Brian Salomon82f44312017-01-11 13:42:54 -05001231 return this->drawPath(clip, std::move(paint), aa, viewMatrix, path, style);
msarettcc319b92016-08-25 18:07:18 -07001232 }
1233
Brian Salomon649a3412017-03-09 13:50:43 -05001234 std::unique_ptr<GrMeshDrawOp> op = GrRegionOp::Make(paint.getColor(), viewMatrix, region);
Brian Salomon82f44312017-01-11 13:42:54 -05001235 GrPipelineBuilder pipelineBuilder(std::move(paint), GrAAType::kNone);
Brian Salomon649a3412017-03-09 13:50:43 -05001236 this->addMeshDrawOp(pipelineBuilder, clip, std::move(op));
msarettcc319b92016-08-25 18:07:18 -07001237}
1238
Brian Osman11052242016-10-27 14:47:55 -04001239void GrRenderTargetContext::drawOval(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001240 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001241 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001242 const SkMatrix& viewMatrix,
1243 const SkRect& oval,
1244 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001245 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001246 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001247 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -04001248 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawOval");
robertphillips2e1e51f2015-10-15 08:01:48 -07001249
robertphillipsea461502015-05-26 11:38:03 -07001250 if (oval.isEmpty()) {
1251 return;
1252 }
1253
bsalomon6663acf2016-05-10 09:14:17 -07001254 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
robertphillipsea461502015-05-26 11:38:03 -07001255
Robert Phillips72152832017-01-25 17:31:35 -05001256 AutoCheckFlush acf(this->drawingManager());
bsalomon6663acf2016-05-10 09:14:17 -07001257 const SkStrokeRec& stroke = style.strokeRec();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001258 GrAAType aaType;
csmartdaltona7f29642016-07-07 08:49:11 -07001259
csmartdaltone0d36292016-07-29 08:14:20 -07001260 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport() &&
1261 stroke.isFillStyle()) {
Robert Phillipsf2361d22016-10-25 14:20:06 -04001262 InstancedRendering* ir = this->getOpList()->instancedRendering();
Brian Salomonf8334782017-01-03 09:42:58 -05001263 std::unique_ptr<GrDrawOp> op(ir->recordOval(oval, viewMatrix, paint.getColor(), aa,
1264 fInstancedPipelineInfo, &aaType));
Brian Salomon42521e82016-12-07 16:44:58 -05001265 if (op) {
Brian Salomon82f44312017-01-11 13:42:54 -05001266 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
Brian Salomon467921e2017-03-06 16:17:12 -05001267 this->addDrawOp(pipelineBuilder, clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -07001268 return;
1269 }
1270 }
1271
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001272 aaType = this->decideAAType(aa);
1273 if (GrAAType::kCoverage == aaType) {
Brian Salomon94efbf52016-11-29 13:43:05 -05001274 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomon649a3412017-03-09 13:50:43 -05001275 std::unique_ptr<GrMeshDrawOp> op =
Brian Salomon289e3d82016-12-14 15:52:56 -05001276 GrOvalOpFactory::MakeOvalOp(paint.getColor(), viewMatrix, oval, stroke, shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -05001277 if (op) {
Brian Salomon82f44312017-01-11 13:42:54 -05001278 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
Brian Salomon649a3412017-03-09 13:50:43 -05001279 this->addMeshDrawOp(pipelineBuilder, clip, std::move(op));
robertphillipsb56f9272016-02-25 11:03:52 -08001280 return;
1281 }
robertphillipsea461502015-05-26 11:38:03 -07001282 }
robertphillipsb56f9272016-02-25 11:03:52 -08001283
1284 SkPath path;
1285 path.setIsVolatile(true);
1286 path.addOval(oval);
Brian Salomon82f44312017-01-11 13:42:54 -05001287 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, style);
robertphillipsea461502015-05-26 11:38:03 -07001288}
1289
Brian Osman11052242016-10-27 14:47:55 -04001290void GrRenderTargetContext::drawArc(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001291 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001292 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001293 const SkMatrix& viewMatrix,
1294 const SkRect& oval,
1295 SkScalar startAngle,
1296 SkScalar sweepAngle,
1297 bool useCenter,
1298 const GrStyle& style) {
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001299 ASSERT_SINGLE_OWNER
1300 RETURN_IF_ABANDONED
1301 SkDEBUGCODE(this->validate();)
1302 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawArc");
1303
1304 AutoCheckFlush acf(this->drawingManager());
1305
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001306 GrAAType aaType = this->decideAAType(aa);
1307 if (GrAAType::kCoverage == aaType) {
Brian Salomon94efbf52016-11-29 13:43:05 -05001308 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomon649a3412017-03-09 13:50:43 -05001309 std::unique_ptr<GrMeshDrawOp> op = GrOvalOpFactory::MakeArcOp(paint.getColor(),
1310 viewMatrix,
1311 oval,
1312 startAngle,
1313 sweepAngle,
1314 useCenter,
1315 style,
1316 shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -05001317 if (op) {
Brian Salomon82f44312017-01-11 13:42:54 -05001318 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
Brian Salomon649a3412017-03-09 13:50:43 -05001319 this->addMeshDrawOp(pipelineBuilder, clip, std::move(op));
bsalomon4f3a0ca2016-08-22 13:14:26 -07001320 return;
1321 }
1322 }
1323 SkPath path;
bsalomon21af9ca2016-08-25 12:29:23 -07001324 SkPathPriv::CreateDrawArcPath(&path, oval, startAngle, sweepAngle, useCenter,
1325 style.isSimpleFill());
Brian Salomon82f44312017-01-11 13:42:54 -05001326 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, style);
bsalomon4f3a0ca2016-08-22 13:14:26 -07001327}
1328
Brian Osman11052242016-10-27 14:47:55 -04001329void GrRenderTargetContext::drawImageLattice(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001330 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001331 const SkMatrix& viewMatrix,
1332 int imageWidth,
1333 int imageHeight,
1334 std::unique_ptr<SkLatticeIter> iter,
1335 const SkRect& dst) {
joshualitt1de610a2016-01-06 08:26:09 -08001336 ASSERT_SINGLE_OWNER
joshualitt33a5fce2015-11-18 13:28:51 -08001337 RETURN_IF_ABANDONED
1338 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -04001339 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawImageLattice");
joshualitt33a5fce2015-11-18 13:28:51 -08001340
Robert Phillips72152832017-01-25 17:31:35 -05001341 AutoCheckFlush acf(this->drawingManager());
joshualitt33a5fce2015-11-18 13:28:51 -08001342
Brian Salomon649a3412017-03-09 13:50:43 -05001343 std::unique_ptr<GrMeshDrawOp> op = GrLatticeOp::MakeNonAA(
1344 paint.getColor(), viewMatrix, imageWidth, imageHeight, std::move(iter), dst);
joshualitt33a5fce2015-11-18 13:28:51 -08001345
Brian Salomon82f44312017-01-11 13:42:54 -05001346 GrPipelineBuilder pipelineBuilder(std::move(paint), GrAAType::kNone);
Brian Salomon649a3412017-03-09 13:50:43 -05001347 this->addMeshDrawOp(pipelineBuilder, clip, std::move(op));
joshualitt33a5fce2015-11-18 13:28:51 -08001348}
1349
Brian Osman11052242016-10-27 14:47:55 -04001350void GrRenderTargetContext::prepareForExternalIO() {
robertphillips8c523e02016-07-26 07:41:00 -07001351 ASSERT_SINGLE_OWNER
1352 RETURN_IF_ABANDONED
1353 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -04001354 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::prepareForExternalIO");
robertphillips8c523e02016-07-26 07:41:00 -07001355
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001356 // Deferral of the VRAM resources must end in this instance anyway
1357 sk_sp<GrRenderTarget> rt(
Brian Osman32342f02017-03-04 08:12:46 -05001358 sk_ref_sp(fRenderTargetProxy->instantiate(fContext->resourceProvider())));
Robert Phillipse60ad622016-11-17 10:22:48 -05001359 if (!rt) {
1360 return;
1361 }
robertphillips8c523e02016-07-26 07:41:00 -07001362
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001363 ASSERT_OWNED_RESOURCE(rt);
1364
Robert Phillips72152832017-01-25 17:31:35 -05001365 this->drawingManager()->prepareSurfaceForExternalIO(rt.get());
robertphillips8c523e02016-07-26 07:41:00 -07001366}
joshualitt33a5fce2015-11-18 13:28:51 -08001367
Brian Osman11052242016-10-27 14:47:55 -04001368void GrRenderTargetContext::drawNonAAFilledRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001369 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001370 const SkMatrix& viewMatrix,
1371 const SkRect& rect,
1372 const SkRect* localRect,
1373 const SkMatrix* localMatrix,
1374 const GrUserStencilSettings* ss,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001375 GrAAType hwOrNoneAAType) {
1376 SkASSERT(GrAAType::kCoverage != hwOrNoneAAType);
1377 SkASSERT(hwOrNoneAAType == GrAAType::kNone || this->isStencilBufferMultisampled());
Brian Salomon649a3412017-03-09 13:50:43 -05001378 std::unique_ptr<GrMeshDrawOp> op = GrRectOpFactory::MakeNonAAFill(paint.getColor(), viewMatrix,
1379 rect, localRect, localMatrix);
Brian Salomon82f44312017-01-11 13:42:54 -05001380 GrPipelineBuilder pipelineBuilder(std::move(paint), hwOrNoneAAType);
robertphillips44302392016-07-08 14:43:03 -07001381 if (ss) {
1382 pipelineBuilder.setUserStencil(ss);
1383 }
Brian Salomon649a3412017-03-09 13:50:43 -05001384 this->addMeshDrawOp(pipelineBuilder, clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -07001385}
1386
robertphillipsea461502015-05-26 11:38:03 -07001387// Can 'path' be drawn as a pair of filled nested rectangles?
bsalomon6663acf2016-05-10 09:14:17 -07001388static bool fills_as_nested_rects(const SkMatrix& viewMatrix, const SkPath& path, SkRect rects[2]) {
robertphillipsea461502015-05-26 11:38:03 -07001389
1390 if (path.isInverseFillType()) {
1391 return false;
1392 }
1393
1394 // TODO: this restriction could be lifted if we were willing to apply
1395 // the matrix to all the points individually rather than just to the rect
robertphillips0e7029e2015-11-30 05:45:06 -08001396 if (!viewMatrix.rectStaysRect()) {
robertphillipsea461502015-05-26 11:38:03 -07001397 return false;
1398 }
1399
1400 SkPath::Direction dirs[2];
1401 if (!path.isNestedFillRects(rects, dirs)) {
1402 return false;
1403 }
1404
1405 if (SkPath::kWinding_FillType == path.getFillType() && dirs[0] == dirs[1]) {
1406 // The two rects need to be wound opposite to each other
1407 return false;
1408 }
1409
1410 // Right now, nested rects where the margin is not the same width
1411 // all around do not render correctly
1412 const SkScalar* outer = rects[0].asScalars();
1413 const SkScalar* inner = rects[1].asScalars();
1414
1415 bool allEq = true;
1416
1417 SkScalar margin = SkScalarAbs(outer[0] - inner[0]);
1418 bool allGoE1 = margin >= SK_Scalar1;
1419
1420 for (int i = 1; i < 4; ++i) {
1421 SkScalar temp = SkScalarAbs(outer[i] - inner[i]);
1422 if (temp < SK_Scalar1) {
1423 allGoE1 = false;
1424 }
1425 if (!SkScalarNearlyEqual(margin, temp)) {
1426 allEq = false;
1427 }
1428 }
1429
1430 return allEq || allGoE1;
1431}
1432
Brian Osman11052242016-10-27 14:47:55 -04001433void GrRenderTargetContext::drawPath(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001434 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001435 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001436 const SkMatrix& viewMatrix,
1437 const SkPath& path,
1438 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001439 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001440 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001441 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -04001442 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawPath");
robertphillips2e1e51f2015-10-15 08:01:48 -07001443
robertphillipsea461502015-05-26 11:38:03 -07001444 if (path.isEmpty()) {
1445 if (path.isInverseFillType()) {
Brian Salomon82f44312017-01-11 13:42:54 -05001446 this->drawPaint(clip, std::move(paint), viewMatrix);
robertphillipsea461502015-05-26 11:38:03 -07001447 }
1448 return;
1449 }
1450
Robert Phillips72152832017-01-25 17:31:35 -05001451 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -07001452
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001453 GrAAType aaType = this->decideAAType(aa);
1454 if (GrAAType::kCoverage == aaType && !style.pathEffect()) {
bsalomon6663acf2016-05-10 09:14:17 -07001455 if (style.isSimpleFill() && !path.isConvex()) {
robertphillipsea461502015-05-26 11:38:03 -07001456 // Concave AA paths are expensive - try to avoid them for special cases
1457 SkRect rects[2];
1458
bsalomon6663acf2016-05-10 09:14:17 -07001459 if (fills_as_nested_rects(viewMatrix, path, rects)) {
Brian Salomon649a3412017-03-09 13:50:43 -05001460 std::unique_ptr<GrMeshDrawOp> op =
Brian Salomon6a639042016-12-14 11:08:17 -05001461 GrRectOpFactory::MakeAAFillNestedRects(paint.getColor(), viewMatrix, rects);
Brian Salomon21aa35f2016-12-09 16:01:53 -05001462 if (op) {
Brian Salomon82f44312017-01-11 13:42:54 -05001463 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
Brian Salomon649a3412017-03-09 13:50:43 -05001464 this->addMeshDrawOp(pipelineBuilder, clip, std::move(op));
bsalomon40ef4852016-05-02 13:22:13 -07001465 }
robertphillipsea461502015-05-26 11:38:03 -07001466 return;
1467 }
1468 }
1469 SkRect ovalRect;
1470 bool isOval = path.isOval(&ovalRect);
1471
1472 if (isOval && !path.isInverseFillType()) {
Brian Salomon94efbf52016-11-29 13:43:05 -05001473 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomon649a3412017-03-09 13:50:43 -05001474 std::unique_ptr<GrMeshDrawOp> op = GrOvalOpFactory::MakeOvalOp(
Brian Salomon289e3d82016-12-14 15:52:56 -05001475 paint.getColor(), viewMatrix, ovalRect, style.strokeRec(), shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -05001476 if (op) {
Brian Salomon82f44312017-01-11 13:42:54 -05001477 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
Brian Salomon649a3412017-03-09 13:50:43 -05001478 this->addMeshDrawOp(pipelineBuilder, clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -07001479 return;
1480 }
1481 }
1482 }
robertphillips4bc31812016-03-01 12:22:49 -08001483
1484 // Note that internalDrawPath may sw-rasterize the path into a scratch texture.
1485 // Scratch textures can be recycled after they are returned to the texture
1486 // cache. This presents a potential hazard for buffered drawing. However,
1487 // the writePixels that uploads to the scratch will perform a flush so we're
1488 // OK.
Brian Salomon82f44312017-01-11 13:42:54 -05001489 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, style);
robertphillipsea461502015-05-26 11:38:03 -07001490}
1491
Brian Osman11052242016-10-27 14:47:55 -04001492bool GrRenderTargetContextPriv::drawAndStencilPath(const GrClip& clip,
1493 const GrUserStencilSettings* ss,
1494 SkRegion::Op op,
1495 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001496 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001497 const SkMatrix& viewMatrix,
1498 const SkPath& path) {
robertphillips391395d2016-03-02 09:26:36 -08001499 ASSERT_SINGLE_OWNER_PRIV
1500 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -04001501 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001502 GR_AUDIT_TRAIL_AUTO_FRAME(fRenderTargetContext->fAuditTrail,
1503 "GrRenderTargetContextPriv::drawAndStencilPath");
robertphillips391395d2016-03-02 09:26:36 -08001504
1505 if (path.isEmpty() && path.isInverseFillType()) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001506 this->drawAndStencilRect(clip, ss, op, invert, GrAA::kNo, SkMatrix::I(),
Brian Osman11052242016-10-27 14:47:55 -04001507 SkRect::MakeIWH(fRenderTargetContext->width(),
1508 fRenderTargetContext->height()));
robertphillips391395d2016-03-02 09:26:36 -08001509 return true;
1510 }
1511
Robert Phillips72152832017-01-25 17:31:35 -05001512 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -08001513
1514 // An Assumption here is that path renderer would use some form of tweaking
1515 // the src color (either the input alpha or in the frag shader) to implement
1516 // aa. If we have some future driver-mojo path AA that can do the right
1517 // thing WRT to the blend then we'll need some query on the PR.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001518 GrAAType aaType = fRenderTargetContext->decideAAType(aa);
robertphillips976f5f02016-06-03 10:59:20 -07001519 bool hasUserStencilSettings = !ss->isUnused();
robertphillips391395d2016-03-02 09:26:36 -08001520
bsalomon8acedde2016-06-24 10:42:16 -07001521 GrShape shape(path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -08001522 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Osman11052242016-10-27 14:47:55 -04001523 canDrawArgs.fShaderCaps =
Robert Phillips72152832017-01-25 17:31:35 -05001524 fRenderTargetContext->drawingManager()->getContext()->caps()->shaderCaps();
robertphillips391395d2016-03-02 09:26:36 -08001525 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07001526 canDrawArgs.fShape = &shape;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001527 canDrawArgs.fAAType = aaType;
cdalton93a379b2016-05-11 13:58:08 -07001528 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips391395d2016-03-02 09:26:36 -08001529
1530 // Don't allow the SW renderer
Robert Phillips72152832017-01-25 17:31:35 -05001531 GrPathRenderer* pr = fRenderTargetContext->drawingManager()->getPathRenderer(
Brian Salomon36aa1762016-12-10 13:24:02 -05001532 canDrawArgs, false, GrPathRendererChain::DrawType::kStencilAndColor);
robertphillips391395d2016-03-02 09:26:36 -08001533 if (!pr) {
1534 return false;
1535 }
1536
1537 GrPaint paint;
1538 paint.setCoverageSetOpXPFactory(op, invert);
1539
Brian Salomon82f44312017-01-11 13:42:54 -05001540 GrPathRenderer::DrawPathArgs args{
Robert Phillips256c37b2017-03-01 14:32:46 -05001541 fRenderTargetContext->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05001542 std::move(paint),
1543 ss,
1544 fRenderTargetContext,
1545 &clip,
1546 &viewMatrix,
1547 &shape,
1548 aaType,
1549 fRenderTargetContext->isGammaCorrect()};
robertphillips391395d2016-03-02 09:26:36 -08001550 pr->drawPath(args);
1551 return true;
1552}
1553
Brian Osman11052242016-10-27 14:47:55 -04001554SkBudgeted GrRenderTargetContextPriv::isBudgeted() const {
robertphillips714712b2016-08-04 06:20:45 -07001555 ASSERT_SINGLE_OWNER_PRIV
1556
Brian Osman11052242016-10-27 14:47:55 -04001557 if (fRenderTargetContext->wasAbandoned()) {
robertphillips714712b2016-08-04 06:20:45 -07001558 return SkBudgeted::kNo;
1559 }
1560
Brian Osman11052242016-10-27 14:47:55 -04001561 SkDEBUGCODE(fRenderTargetContext->validate();)
robertphillips714712b2016-08-04 06:20:45 -07001562
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001563 return fRenderTargetContext->fRenderTargetProxy->isBudgeted();
robertphillips714712b2016-08-04 06:20:45 -07001564}
1565
Brian Osman11052242016-10-27 14:47:55 -04001566void GrRenderTargetContext::internalDrawPath(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001567 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001568 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001569 const SkMatrix& viewMatrix,
1570 const SkPath& path,
1571 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001572 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001573 RETURN_IF_ABANDONED
bsalomon8acedde2016-06-24 10:42:16 -07001574 SkASSERT(!path.isEmpty());
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001575 GrShape shape;
robertphillipsea461502015-05-26 11:38:03 -07001576
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001577 GrAAType aaType = this->decideAAType(aa, /*allowMixedSamples*/ true);
1578 if (style.isSimpleHairline() && aaType == GrAAType::kMixedSamples) {
1579 // NVPR cannot handle hairlines, so this will would get picked up by a different stencil and
1580 // cover path renderer (i.e. default path renderer). The hairline renderer produces much
1581 // smoother hairlines than MSAA.
1582 aaType = GrAAType::kCoverage;
bsalomon0a0f67e2016-06-28 11:56:42 -07001583 }
robertphillips68737822015-10-29 12:12:21 -07001584 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Robert Phillips72152832017-01-25 17:31:35 -05001585 canDrawArgs.fShaderCaps = this->drawingManager()->getContext()->caps()->shaderCaps();
robertphillips68737822015-10-29 12:12:21 -07001586 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07001587 canDrawArgs.fShape = &shape;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001588 canDrawArgs.fHasUserStencilSettings = false;
robertphillips68737822015-10-29 12:12:21 -07001589
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001590 GrPathRenderer* pr;
Brian Salomon82125e92016-12-10 09:35:48 -05001591 static constexpr GrPathRendererChain::DrawType kType = GrPathRendererChain::DrawType::kColor;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001592 do {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001593 shape = GrShape(path, style);
bsalomon8acedde2016-06-24 10:42:16 -07001594 if (shape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001595 return;
1596 }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001597
1598 canDrawArgs.fAAType = aaType;
1599
1600 // Try a 1st time without applying any of the style to the geometry (and barring sw)
Robert Phillips72152832017-01-25 17:31:35 -05001601 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001602 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
1603
1604 if (!pr && shape.style().pathEffect()) {
1605 // It didn't work above, so try again with the path effect applied.
1606 shape = shape.applyStyle(GrStyle::Apply::kPathEffectOnly, styleScale);
bsalomon8acedde2016-06-24 10:42:16 -07001607 if (shape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001608 return;
1609 }
Robert Phillips72152832017-01-25 17:31:35 -05001610 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
bsalomon6663acf2016-05-10 09:14:17 -07001611 }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001612 if (!pr) {
1613 if (shape.style().applies()) {
1614 shape = shape.applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec, styleScale);
1615 if (shape.isEmpty()) {
1616 return;
1617 }
1618 }
1619 // This time, allow SW renderer
Robert Phillips72152832017-01-25 17:31:35 -05001620 pr = this->drawingManager()->getPathRenderer(canDrawArgs, true, kType);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001621 }
Brian Salomon0abc8b42016-12-13 10:22:54 -05001622 if (!pr && GrAATypeIsHW(aaType)) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001623 // There are exceptional cases where we may wind up falling back to coverage based AA
1624 // when the target is MSAA (e.g. through disabling path renderers via GrContextOptions).
1625 aaType = GrAAType::kCoverage;
1626 } else {
1627 break;
1628 }
1629 } while(true);
robertphillipsea461502015-05-26 11:38:03 -07001630
bsalomon8acedde2016-06-24 10:42:16 -07001631 if (!pr) {
robertphillipsea461502015-05-26 11:38:03 -07001632#ifdef SK_DEBUG
1633 SkDebugf("Unable to find path renderer compatible with path.\n");
1634#endif
1635 return;
1636 }
1637
Robert Phillips256c37b2017-03-01 14:32:46 -05001638 GrPathRenderer::DrawPathArgs args{this->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05001639 std::move(paint),
1640 &GrUserStencilSettings::kUnused,
1641 this,
1642 &clip,
1643 &viewMatrix,
1644 &shape,
1645 aaType,
1646 this->isGammaCorrect()};
bsalomon0aff2fa2015-07-31 06:48:27 -07001647 pr->drawPath(args);
robertphillipsea461502015-05-26 11:38:03 -07001648}
1649
Brian Salomon467921e2017-03-06 16:17:12 -05001650static void op_bounds(SkRect* bounds, const GrOp* op) {
1651 *bounds = op->bounds();
1652 if (op->hasZeroArea()) {
1653 if (op->hasAABloat()) {
1654 bounds->outset(0.5f, 0.5f);
1655 } else {
1656 // We don't know which way the particular GPU will snap lines or points at integer
1657 // coords. So we ensure that the bounds is large enough for either snap.
1658 SkRect before = *bounds;
1659 bounds->roundOut(bounds);
1660 if (bounds->fLeft == before.fLeft) {
1661 bounds->fLeft -= 1;
1662 }
1663 if (bounds->fTop == before.fTop) {
1664 bounds->fTop -= 1;
1665 }
1666 if (bounds->fRight == before.fRight) {
1667 bounds->fRight += 1;
1668 }
1669 if (bounds->fBottom == before.fBottom) {
1670 bounds->fBottom += 1;
1671 }
1672 }
1673 }
1674}
1675
Brian Salomon649a3412017-03-09 13:50:43 -05001676uint32_t GrRenderTargetContext::addMeshDrawOp(const GrPipelineBuilder& pipelineBuilder,
1677 const GrClip& clip,
1678 std::unique_ptr<GrMeshDrawOp> op) {
1679 return this->addDrawOp(pipelineBuilder, clip, std::move(op));
1680}
1681
Robert Phillipsc0138922017-03-08 11:50:55 -05001682uint32_t GrRenderTargetContext::addDrawOp(const GrPipelineBuilder& pipelineBuilder,
1683 const GrClip& clip,
1684 std::unique_ptr<GrDrawOp> op) {
joshualitt1de610a2016-01-06 08:26:09 -08001685 ASSERT_SINGLE_OWNER
Robert Phillipsc0138922017-03-08 11:50:55 -05001686 if (this->drawingManager()->wasAbandoned()) {
1687 return SK_InvalidUniqueID;
1688 }
robertphillips2e1e51f2015-10-15 08:01:48 -07001689 SkDEBUGCODE(this->validate();)
Brian Salomon42521e82016-12-07 16:44:58 -05001690 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::addDrawOp");
robertphillips2d70dcb2015-10-06 07:38:23 -07001691
Brian Salomon467921e2017-03-06 16:17:12 -05001692 // Setup clip
1693 SkRect bounds;
1694 op_bounds(&bounds, op.get());
1695 GrAppliedClip appliedClip(bounds);
1696 if (!clip.apply(fContext, this, pipelineBuilder.isHWAntialias(),
1697 pipelineBuilder.hasUserStencilSettings(), &appliedClip)) {
Robert Phillipsc0138922017-03-08 11:50:55 -05001698 return SK_InvalidUniqueID;
Brian Salomon467921e2017-03-06 16:17:12 -05001699 }
1700
1701 // This forces instantiation of the render target. Pipeline creation is moving to flush time
1702 // by which point instantiation must have occurred anyway.
1703 GrRenderTarget* rt = this->accessRenderTarget();
1704 if (!rt) {
Robert Phillipsc0138922017-03-08 11:50:55 -05001705 return SK_InvalidUniqueID;
Brian Salomon467921e2017-03-06 16:17:12 -05001706 }
1707
1708 GrResourceProvider* resourceProvider = fContext->resourceProvider();
1709 if (pipelineBuilder.hasUserStencilSettings() || appliedClip.hasStencilClip()) {
1710 if (!resourceProvider->attachStencilAttachment(this->accessRenderTarget())) {
1711 SkDebugf("ERROR creating stencil attachment. Draw skipped.\n");
Robert Phillipsc0138922017-03-08 11:50:55 -05001712 return SK_InvalidUniqueID;
Brian Salomon467921e2017-03-06 16:17:12 -05001713 }
1714 }
1715
1716 GrProcessorSet::FragmentProcessorAnalysis analysis;
1717 op->analyzeProcessors(&analysis, pipelineBuilder.processors(), appliedClip, *this->caps());
1718
1719 GrPipeline::InitArgs args;
1720 pipelineBuilder.getPipelineInitArgs(&args);
1721 args.fAppliedClip = &appliedClip;
1722 args.fRenderTarget = rt;
1723 args.fCaps = this->caps();
1724 args.fAnalysis = &analysis;
1725
1726 if (pipelineBuilder.willXPNeedDstTexture(*this->caps(), analysis)) {
1727 this->setupDstTexture(rt, clip, bounds, &args.fDstTexture);
1728 if (!args.fDstTexture.texture()) {
Robert Phillipsc0138922017-03-08 11:50:55 -05001729 return SK_InvalidUniqueID;
Brian Salomon467921e2017-03-06 16:17:12 -05001730 }
1731 }
1732 op->initPipeline(args);
1733 // TODO: We need to add pipeline dependencies on textures, etc before recording this op.
1734 op->setClippedBounds(appliedClip.clippedDrawBounds());
Robert Phillipsc0138922017-03-08 11:50:55 -05001735 return this->getOpList()->addOp(std::move(op), this);
Brian Salomon467921e2017-03-06 16:17:12 -05001736}
1737
1738void GrRenderTargetContext::setupDstTexture(GrRenderTarget* rt, const GrClip& clip,
1739 const SkRect& opBounds,
1740 GrXferProcessor::DstTexture* dstTexture) {
1741 if (this->caps()->textureBarrierSupport()) {
1742 if (GrTexture* rtTex = rt->asTexture()) {
1743 // The render target is a texture, so we can read from it directly in the shader. The XP
1744 // will be responsible to detect this situation and request a texture barrier.
1745 dstTexture->setTexture(sk_ref_sp(rtTex));
1746 dstTexture->setOffset(0, 0);
1747 return;
1748 }
1749 }
1750
1751 SkIRect copyRect;
1752 clip.getConservativeBounds(rt->width(), rt->height(), &copyRect);
1753
1754 SkIRect drawIBounds;
1755 opBounds.roundOut(&drawIBounds);
1756 if (!copyRect.intersect(drawIBounds)) {
1757#ifdef SK_DEBUG
1758 GrCapsDebugf(this->caps(), "Missed an early reject. "
1759 "Bailing on draw from setupDstTexture.\n");
1760#endif
1761 return;
1762 }
1763
1764 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
1765 // have per-sample dst values by making the copy multisampled.
1766 GrSurfaceDesc desc;
1767 if (!this->caps()->initDescForDstCopy(rt, &desc)) {
1768 desc.fOrigin = kDefault_GrSurfaceOrigin;
1769 desc.fFlags = kRenderTarget_GrSurfaceFlag;
1770 desc.fConfig = rt->config();
1771 }
1772
1773 desc.fWidth = copyRect.width();
1774 desc.fHeight = copyRect.height();
1775
1776 static const uint32_t kFlags = 0;
1777 sk_sp<GrTexture> copy(fContext->resourceProvider()->createApproxTexture(desc, kFlags));
1778
1779 if (!copy) {
1780 SkDebugf("Failed to create temporary copy of destination texture.\n");
1781 return;
1782 }
1783 SkIPoint dstPoint = {0, 0};
1784 this->getOpList()->copySurface(copy.get(), rt, copyRect, dstPoint);
1785 dstTexture->setTexture(std::move(copy));
1786 dstTexture->setOffset(copyRect.fLeft, copyRect.fTop);
robertphillips2334fb62015-06-17 05:43:33 -07001787}