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