blob: 3aefde0dc5085df8cfb7e8de62361ca037d31ec6 [file] [log] [blame]
robertphillipsea461502015-05-26 11:38:03 -07001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Brian Osman11052242016-10-27 14:47:55 -04008#include "GrRenderTargetContext.h"
Brian Salomon467921e2017-03-06 16:17:12 -05009#include "GrAppliedClip.h"
Brian Salomon5ec9def2016-12-20 15:34:05 -050010#include "GrColor.h"
Robert Phillipse78b7252017-04-06 07:59:41 -040011#include "GrContextPriv.h"
robertphillips77a2e522015-10-17 07:43:27 -070012#include "GrDrawingManager.h"
csmartdalton02fa32c2016-08-19 13:29:27 -070013#include "GrFixedClip.h"
robertphillips714712b2016-08-04 06:20:45 -070014#include "GrGpuResourcePriv.h"
robertphillipsea461502015-05-26 11:38:03 -070015#include "GrPathRenderer.h"
robertphillips5fa7f302016-07-21 09:21:04 -070016#include "GrPipelineBuilder.h"
robertphillips2334fb62015-06-17 05:43:33 -070017#include "GrRenderTarget.h"
Brian Salomon5ec9def2016-12-20 15:34:05 -050018#include "GrRenderTargetContextPriv.h"
robertphillips2334fb62015-06-17 05:43:33 -070019#include "GrRenderTargetPriv.h"
bsalomon473addf2015-10-02 07:49:05 -070020#include "GrResourceProvider.h"
Brian Salomon467921e2017-03-06 16:17:12 -050021#include "GrStencilAttachment.h"
22#include "SkLatticeIter.h"
23#include "SkMatrixPriv.h"
robertphillips2d70dcb2015-10-06 07:38:23 -070024#include "SkSurfacePriv.h"
Brian Salomon467921e2017-03-06 16:17:12 -050025#include "effects/GrRRectEffect.h"
26#include "instanced/InstancedRendering.h"
Brian Salomon89527432016-12-16 09:52:16 -050027#include "ops/GrClearOp.h"
Robert Phillipsb9a02a12017-04-06 11:08:40 -040028#include "ops/GrClearStencilClipOp.h"
Brian Salomon467921e2017-03-06 16:17:12 -050029#include "ops/GrDrawOp.h"
Brian Salomon89527432016-12-16 09:52:16 -050030#include "ops/GrDrawAtlasOp.h"
31#include "ops/GrDrawVerticesOp.h"
32#include "ops/GrLatticeOp.h"
33#include "ops/GrOp.h"
34#include "ops/GrOvalOpFactory.h"
35#include "ops/GrRectOpFactory.h"
36#include "ops/GrRegionOp.h"
37#include "ops/GrShadowRRectOp.h"
Brian Salomon467921e2017-03-06 16:17:12 -050038#include "ops/GrStencilPathOp.h"
joshualitte8042922015-12-11 06:11:21 -080039#include "text/GrAtlasTextContext.h"
40#include "text/GrStencilAndCoverTextContext.h"
joshualittbc907352016-01-13 06:45:40 -080041#include "../private/GrAuditTrail.h"
42
Robert Phillips72152832017-01-25 17:31:35 -050043#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this->drawingManager()->getContext())
joshualitt1de610a2016-01-06 08:26:09 -080044#define ASSERT_SINGLE_OWNER \
Robert Phillipsa90aa2b2017-04-10 08:19:26 -040045 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(this->singleOwner());)
robertphillips391395d2016-03-02 09:26:36 -080046#define ASSERT_SINGLE_OWNER_PRIV \
Robert Phillipsa90aa2b2017-04-10 08:19:26 -040047 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fRenderTargetContext->singleOwner());)
Robert Phillips72152832017-01-25 17:31:35 -050048#define RETURN_IF_ABANDONED if (this->drawingManager()->wasAbandoned()) { return; }
49#define RETURN_IF_ABANDONED_PRIV if (fRenderTargetContext->drawingManager()->wasAbandoned()) { return; }
50#define RETURN_FALSE_IF_ABANDONED if (this->drawingManager()->wasAbandoned()) { return false; }
51#define RETURN_FALSE_IF_ABANDONED_PRIV if (fRenderTargetContext->drawingManager()->wasAbandoned()) { return false; }
52#define RETURN_NULL_IF_ABANDONED if (this->drawingManager()->wasAbandoned()) { return nullptr; }
robertphillipsea461502015-05-26 11:38:03 -070053
csmartdaltona7f29642016-07-07 08:49:11 -070054using gr_instanced::InstancedRendering;
55
robertphillipsea461502015-05-26 11:38:03 -070056class AutoCheckFlush {
57public:
halcanary9d524f22016-03-29 09:03:52 -070058 AutoCheckFlush(GrDrawingManager* drawingManager) : fDrawingManager(drawingManager) {
robertphillips77a2e522015-10-17 07:43:27 -070059 SkASSERT(fDrawingManager);
60 }
bsalomonb77a9072016-09-07 10:02:04 -070061 ~AutoCheckFlush() { fDrawingManager->flushIfNecessary(); }
robertphillipsea461502015-05-26 11:38:03 -070062
63private:
robertphillips77a2e522015-10-17 07:43:27 -070064 GrDrawingManager* fDrawingManager;
robertphillipsea461502015-05-26 11:38:03 -070065};
66
Brian Osman11052242016-10-27 14:47:55 -040067bool GrRenderTargetContext::wasAbandoned() const {
Robert Phillips72152832017-01-25 17:31:35 -050068 return this->drawingManager()->wasAbandoned();
robertphillips7761d612016-05-16 09:14:53 -070069}
70
Robert Phillipsf2361d22016-10-25 14:20:06 -040071// In MDB mode the reffing of the 'getLastOpList' call's result allows in-progress
Brian Osman11052242016-10-27 14:47:55 -040072// GrOpLists to be picked up and added to by renderTargetContexts lower in the call
Robert Phillipsf2361d22016-10-25 14:20:06 -040073// stack. When this occurs with a closed GrOpList, a new one will be allocated
Brian Osman11052242016-10-27 14:47:55 -040074// when the renderTargetContext attempts to use it (via getOpList).
75GrRenderTargetContext::GrRenderTargetContext(GrContext* context,
76 GrDrawingManager* drawingMgr,
Robert Phillipsc7635fa2016-10-28 13:25:24 -040077 sk_sp<GrRenderTargetProxy> rtp,
Brian Osman11052242016-10-27 14:47:55 -040078 sk_sp<SkColorSpace> colorSpace,
79 const SkSurfaceProps* surfaceProps,
80 GrAuditTrail* auditTrail,
81 GrSingleOwner* singleOwner)
Robert Phillips72152832017-01-25 17:31:35 -050082 : GrSurfaceContext(context, drawingMgr, std::move(colorSpace), auditTrail, singleOwner)
Robert Phillipsc7635fa2016-10-28 13:25:24 -040083 , fRenderTargetProxy(std::move(rtp))
84 , fOpList(SkSafeRef(fRenderTargetProxy->getLastRenderTargetOpList()))
Robert Phillipsc7635fa2016-10-28 13:25:24 -040085 , fInstancedPipelineInfo(fRenderTargetProxy.get())
brianosman5a7ae7e2016-09-12 12:07:25 -070086 , fColorXformFromSRGB(nullptr)
Robert Phillips2c862492017-01-18 10:08:39 -050087 , fSurfaceProps(SkSurfacePropsCopyOrDefault(surfaceProps)) {
brianosman5a7ae7e2016-09-12 12:07:25 -070088 if (fColorSpace) {
89 // sRGB sources are very common (SkColor, etc...), so we cache that gamut transformation
Matt Sarett77a7a1b2017-02-07 13:56:11 -050090 auto srgbColorSpace = SkColorSpace::MakeSRGB();
msarettc71a9b72016-09-16 11:01:27 -070091 fColorXformFromSRGB = GrColorSpaceXform::Make(srgbColorSpace.get(), fColorSpace.get());
brianosman5a7ae7e2016-09-12 12:07:25 -070092 }
robertphillips2e1e51f2015-10-15 08:01:48 -070093 SkDEBUGCODE(this->validate();)
robertphillipsea461502015-05-26 11:38:03 -070094}
95
robertphillips2e1e51f2015-10-15 08:01:48 -070096#ifdef SK_DEBUG
Brian Osman11052242016-10-27 14:47:55 -040097void GrRenderTargetContext::validate() const {
Robert Phillipsc7635fa2016-10-28 13:25:24 -040098 SkASSERT(fRenderTargetProxy);
99 fRenderTargetProxy->validate(fContext);
robertphillipsa106c622015-10-16 09:07:06 -0700100
Robert Phillipsf2361d22016-10-25 14:20:06 -0400101 if (fOpList && !fOpList->isClosed()) {
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400102 SkASSERT(fRenderTargetProxy->getLastOpList() == fOpList);
robertphillipsa106c622015-10-16 09:07:06 -0700103 }
robertphillips2e1e51f2015-10-15 08:01:48 -0700104}
105#endif
106
Brian Osman11052242016-10-27 14:47:55 -0400107GrRenderTargetContext::~GrRenderTargetContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800108 ASSERT_SINGLE_OWNER
Robert Phillipsf2361d22016-10-25 14:20:06 -0400109 SkSafeUnref(fOpList);
robertphillipsa106c622015-10-16 09:07:06 -0700110}
111
Robert Phillipsf200a902017-01-30 13:27:37 -0500112GrTextureProxy* GrRenderTargetContext::asTextureProxy() {
Robert Phillipseaa86252016-11-08 13:49:39 +0000113 return fRenderTargetProxy->asTextureProxy();
114}
115
Robert Phillipsf200a902017-01-30 13:27:37 -0500116sk_sp<GrTextureProxy> GrRenderTargetContext::asTextureProxyRef() {
117 return sk_ref_sp(fRenderTargetProxy->asTextureProxy());
118}
119
Brian Osman11052242016-10-27 14:47:55 -0400120GrRenderTargetOpList* GrRenderTargetContext::getOpList() {
joshualitt1de610a2016-01-06 08:26:09 -0800121 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700122 SkDEBUGCODE(this->validate();)
123
Robert Phillipsf2361d22016-10-25 14:20:06 -0400124 if (!fOpList || fOpList->isClosed()) {
Robert Phillips72152832017-01-25 17:31:35 -0500125 fOpList = this->drawingManager()->newOpList(fRenderTargetProxy.get());
robertphillipsa106c622015-10-16 09:07:06 -0700126 }
127
Robert Phillipsf2361d22016-10-25 14:20:06 -0400128 return fOpList;
robertphillipsa106c622015-10-16 09:07:06 -0700129}
130
Robert Phillipse2f7d182016-12-15 09:23:05 -0500131// TODO: move this (and GrTextContext::copy) to GrSurfaceContext?
132bool GrRenderTargetContext::onCopy(GrSurfaceProxy* srcProxy,
133 const SkIRect& srcRect,
134 const SkIPoint& dstPoint) {
joshualitt1de610a2016-01-06 08:26:09 -0800135 ASSERT_SINGLE_OWNER
bsalomonb8fea972016-02-16 07:34:17 -0800136 RETURN_FALSE_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700137 SkDEBUGCODE(this->validate();)
Robert Phillipsbf25d432017-04-07 10:08:53 -0400138 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::onCopy");
Robert Phillipse2f7d182016-12-15 09:23:05 -0500139
Robert Phillipsbf25d432017-04-07 10:08:53 -0400140 return this->getOpList()->copySurface(fContext->resourceProvider(),
141 fRenderTargetProxy.get(), srcProxy, srcRect, dstPoint);
robertphillipsea461502015-05-26 11:38:03 -0700142}
143
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500144void GrRenderTargetContext::drawText(const GrClip& clip, const SkPaint& skPaint,
Brian Salomon82f44312017-01-11 13:42:54 -0500145 const SkMatrix& viewMatrix, const char text[],
146 size_t byteLength, SkScalar x, SkScalar y,
147 const SkIRect& clipBounds) {
joshualitt1de610a2016-01-06 08:26:09 -0800148 ASSERT_SINGLE_OWNER
robertphillips2d70dcb2015-10-06 07:38:23 -0700149 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700150 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400151 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawText");
robertphillips2d70dcb2015-10-06 07:38:23 -0700152
Robert Phillips72152832017-01-25 17:31:35 -0500153 GrAtlasTextContext* atlasTextContext = this->drawingManager()->getAtlasTextContext();
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500154 atlasTextContext->drawText(fContext, this, clip, skPaint, viewMatrix, fSurfaceProps, text,
155 byteLength, x, y, clipBounds);
robertphillips2334fb62015-06-17 05:43:33 -0700156}
robertphillipscaef3452015-11-11 13:18:11 -0800157
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500158void GrRenderTargetContext::drawPosText(const GrClip& clip, const SkPaint& paint,
159 const SkMatrix& viewMatrix, const char text[],
160 size_t byteLength, const SkScalar pos[],
Brian Salomon82f44312017-01-11 13:42:54 -0500161 int scalarsPerPosition, const SkPoint& offset,
162 const SkIRect& clipBounds) {
joshualitt1de610a2016-01-06 08:26:09 -0800163 ASSERT_SINGLE_OWNER
robertphillips2d70dcb2015-10-06 07:38:23 -0700164 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700165 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400166 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawPosText");
robertphillips2d70dcb2015-10-06 07:38:23 -0700167
Robert Phillips72152832017-01-25 17:31:35 -0500168 GrAtlasTextContext* atlasTextContext = this->drawingManager()->getAtlasTextContext();
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500169 atlasTextContext->drawPosText(fContext, this, clip, paint, viewMatrix, fSurfaceProps, text,
170 byteLength, pos, scalarsPerPosition, offset, clipBounds);
robertphillips2334fb62015-06-17 05:43:33 -0700171}
robertphillipscaef3452015-11-11 13:18:11 -0800172
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500173void GrRenderTargetContext::drawTextBlob(const GrClip& clip, const SkPaint& paint,
Brian Osman11052242016-10-27 14:47:55 -0400174 const SkMatrix& viewMatrix, const SkTextBlob* blob,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500175 SkScalar x, SkScalar y, SkDrawFilter* filter,
176 const SkIRect& clipBounds) {
joshualitt1de610a2016-01-06 08:26:09 -0800177 ASSERT_SINGLE_OWNER
robertphillips2d70dcb2015-10-06 07:38:23 -0700178 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700179 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400180 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawTextBlob");
robertphillips2d70dcb2015-10-06 07:38:23 -0700181
Robert Phillips72152832017-01-25 17:31:35 -0500182 GrAtlasTextContext* atlasTextContext = this->drawingManager()->getAtlasTextContext();
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500183 atlasTextContext->drawTextBlob(fContext, this, clip, paint, viewMatrix, fSurfaceProps, blob, x,
184 y, filter, clipBounds);
robertphillipsea461502015-05-26 11:38:03 -0700185}
186
Brian Osman11052242016-10-27 14:47:55 -0400187void GrRenderTargetContext::discard() {
joshualitt1de610a2016-01-06 08:26:09 -0800188 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700189 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700190 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400191 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::discard");
robertphillips2e1e51f2015-10-15 08:01:48 -0700192
Robert Phillips72152832017-01-25 17:31:35 -0500193 AutoCheckFlush acf(this->drawingManager());
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400194
Brian Salomon69868af2016-12-22 15:42:51 -0500195 // TODO: This needs to be fixed up since it ends the deferral of the GrRenderTarget.
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400196 sk_sp<GrRenderTarget> rt(
Brian Osman32342f02017-03-04 08:12:46 -0500197 sk_ref_sp(fRenderTargetProxy->instantiate(fContext->resourceProvider())));
Robert Phillipse60ad622016-11-17 10:22:48 -0500198 if (!rt) {
199 return;
200 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400201
Brian Salomon69868af2016-12-22 15:42:51 -0500202 this->getOpList()->discard(this);
robertphillipsea461502015-05-26 11:38:03 -0700203}
204
Brian Osman11052242016-10-27 14:47:55 -0400205void GrRenderTargetContext::clear(const SkIRect* rect,
206 const GrColor color,
207 bool canIgnoreRect) {
joshualitt1de610a2016-01-06 08:26:09 -0800208 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700209 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700210 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400211 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::clear");
robertphillipsea461502015-05-26 11:38:03 -0700212
Robert Phillips72152832017-01-25 17:31:35 -0500213 AutoCheckFlush acf(this->drawingManager());
csmartdalton29df7602016-08-31 11:55:52 -0700214 this->internalClear(rect ? GrFixedClip(*rect) : GrFixedClip::Disabled(), color, canIgnoreRect);
215}
robertphillips9199a9f2016-07-13 07:48:43 -0700216
Robert Phillips784b7bf2016-12-09 13:35:02 -0500217void GrRenderTargetContextPriv::absClear(const SkIRect* clearRect, const GrColor color) {
218 ASSERT_SINGLE_OWNER_PRIV
219 RETURN_IF_ABANDONED_PRIV
220 SkDEBUGCODE(fRenderTargetContext->validate();)
221 GR_AUDIT_TRAIL_AUTO_FRAME(fRenderTargetContext->fAuditTrail,
222 "GrRenderTargetContext::absClear");
223
Robert Phillips72152832017-01-25 17:31:35 -0500224 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500225
226 SkIRect rtRect = SkIRect::MakeWH(fRenderTargetContext->fRenderTargetProxy->worstCaseWidth(
227 *fRenderTargetContext->caps()),
228 fRenderTargetContext->fRenderTargetProxy->worstCaseHeight(
229 *fRenderTargetContext->caps()));
230
231 if (clearRect) {
232 if (clearRect->contains(rtRect)) {
233 clearRect = nullptr; // full screen
234 } else {
235 if (!rtRect.intersect(*clearRect)) {
236 return;
237 }
238 }
239 }
240
241 // TODO: in a post-MDB world this should be handled at the OpList level.
242 // An op-list that is initially cleared and has no other ops should receive an
243 // extra draw.
244 if (fRenderTargetContext->fContext->caps()->useDrawInsteadOfClear()) {
245 // This works around a driver bug with clear by drawing a rect instead.
246 // The driver will ignore a clear if it is the only thing rendered to a
247 // target before the target is read.
248 GrPaint paint;
249 paint.setColor4f(GrColor4f::FromGrColor(color));
Brian Salomona1633922017-01-09 11:46:10 -0500250 paint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
Robert Phillips784b7bf2016-12-09 13:35:02 -0500251
252 // We don't call drawRect() here to avoid the cropping to the, possibly smaller,
253 // RenderTargetProxy bounds
Brian Salomon82f44312017-01-11 13:42:54 -0500254 fRenderTargetContext->drawNonAAFilledRect(GrNoClip(), std::move(paint), SkMatrix::I(),
255 SkRect::Make(rtRect), nullptr, nullptr, nullptr,
256 GrAAType::kNone);
Robert Phillips784b7bf2016-12-09 13:35:02 -0500257
258 } else {
Robert Phillips784b7bf2016-12-09 13:35:02 -0500259 // This path doesn't handle coalescing of full screen clears b.c. it
260 // has to clear the entire render target - not just the content area.
261 // It could be done but will take more finagling.
Robert Phillipsf7a72612017-03-31 10:03:45 -0400262 std::unique_ptr<GrOp> op(GrClearOp::Make(rtRect, color, fRenderTargetContext, !clearRect));
Brian Salomonfc527d22016-12-14 21:07:01 -0500263 if (!op) {
Robert Phillips784b7bf2016-12-09 13:35:02 -0500264 return;
265 }
Brian Salomon69868af2016-12-22 15:42:51 -0500266 fRenderTargetContext->getOpList()->addOp(std::move(op), fRenderTargetContext);
Robert Phillips784b7bf2016-12-09 13:35:02 -0500267 }
268}
269
Brian Osman11052242016-10-27 14:47:55 -0400270void GrRenderTargetContextPriv::clear(const GrFixedClip& clip,
271 const GrColor color,
272 bool canIgnoreClip) {
csmartdalton29df7602016-08-31 11:55:52 -0700273 ASSERT_SINGLE_OWNER_PRIV
274 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400275 SkDEBUGCODE(fRenderTargetContext->validate();)
276 GR_AUDIT_TRAIL_AUTO_FRAME(fRenderTargetContext->fAuditTrail,
277 "GrRenderTargetContextPriv::clear");
csmartdalton29df7602016-08-31 11:55:52 -0700278
Robert Phillips72152832017-01-25 17:31:35 -0500279 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Brian Osman11052242016-10-27 14:47:55 -0400280 fRenderTargetContext->internalClear(clip, color, canIgnoreClip);
csmartdalton29df7602016-08-31 11:55:52 -0700281}
282
Brian Osman11052242016-10-27 14:47:55 -0400283void GrRenderTargetContext::internalClear(const GrFixedClip& clip,
284 const GrColor color,
285 bool canIgnoreClip) {
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700286 bool isFull = false;
287 if (!clip.hasWindowRectangles()) {
288 isFull = !clip.scissorEnabled() ||
289 (canIgnoreClip && fContext->caps()->fullClearIsFree()) ||
290 clip.scissorRect().contains(SkIRect::MakeWH(this->width(), this->height()));
291 }
robertphillips9199a9f2016-07-13 07:48:43 -0700292
293 if (fContext->caps()->useDrawInsteadOfClear()) {
294 // This works around a driver bug with clear by drawing a rect instead.
295 // The driver will ignore a clear if it is the only thing rendered to a
296 // target before the target is read.
Robert Phillips784b7bf2016-12-09 13:35:02 -0500297 SkIRect clearRect = SkIRect::MakeWH(this->width(), this->height());
csmartdalton29df7602016-08-31 11:55:52 -0700298 if (isFull) {
robertphillips9199a9f2016-07-13 07:48:43 -0700299 this->discard();
Robert Phillips93f16332016-11-23 19:37:13 -0500300 } else if (!clearRect.intersect(clip.scissorRect())) {
csmartdalton29df7602016-08-31 11:55:52 -0700301 return;
robertphillips9199a9f2016-07-13 07:48:43 -0700302 }
303
304 GrPaint paint;
305 paint.setColor4f(GrColor4f::FromGrColor(color));
Brian Salomona1633922017-01-09 11:46:10 -0500306 paint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
robertphillips9199a9f2016-07-13 07:48:43 -0700307
Brian Salomon82f44312017-01-11 13:42:54 -0500308 this->drawRect(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), SkRect::Make(clearRect));
bsalomon9f129de2016-08-10 16:31:05 -0700309 } else if (isFull) {
Robert Phillipsf7a72612017-03-31 10:03:45 -0400310 this->getOpList()->fullClear(this, color);
robertphillips9199a9f2016-07-13 07:48:43 -0700311 } else {
Robert Phillipsf7a72612017-03-31 10:03:45 -0400312 std::unique_ptr<GrOp> op(GrClearOp::Make(clip, color, this));
Brian Salomon42521e82016-12-07 16:44:58 -0500313 if (!op) {
csmartdalton29df7602016-08-31 11:55:52 -0700314 return;
315 }
Brian Salomon69868af2016-12-22 15:42:51 -0500316 this->getOpList()->addOp(std::move(op), this);
robertphillips9199a9f2016-07-13 07:48:43 -0700317 }
robertphillipsea461502015-05-26 11:38:03 -0700318}
319
Brian Osman11052242016-10-27 14:47:55 -0400320void GrRenderTargetContext::drawPaint(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500321 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400322 const SkMatrix& viewMatrix) {
joshualitt1de610a2016-01-06 08:26:09 -0800323 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700324 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700325 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400326 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawPaint");
robertphillips2e1e51f2015-10-15 08:01:48 -0700327
robertphillipsea461502015-05-26 11:38:03 -0700328 // set rect to be big enough to fill the space, but not super-huge, so we
329 // don't overflow fixed-point implementations
robertphillips13a7eee2016-08-31 15:06:24 -0700330
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400331 SkRect r = fRenderTargetProxy->getBoundsRect();
robertphillipsea461502015-05-26 11:38:03 -0700332
bsalomoncb31e512016-08-26 10:48:19 -0700333 SkRRect rrect;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500334 GrAA aa;
bsalomoncb31e512016-08-26 10:48:19 -0700335 // Check if we can replace a clipRRect()/drawPaint() with a drawRRect(). We only do the
336 // transformation for non-rect rrects. Rects caused a performance regression on an Android
337 // test that needs investigation. We also skip cases where there are fragment processors
338 // because they may depend on having correct local coords and this path draws in device space
339 // without a local matrix.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500340 if (!paint.numTotalFragmentProcessors() && clip.isRRect(r, &rrect, &aa) && !rrect.isRect()) {
Brian Salomon82f44312017-01-11 13:42:54 -0500341 this->drawRRect(GrNoClip(), std::move(paint), aa, SkMatrix::I(), rrect,
342 GrStyle::SimpleFill());
bsalomoncb31e512016-08-26 10:48:19 -0700343 return;
344 }
345
robertphillipsea461502015-05-26 11:38:03 -0700346
347 bool isPerspective = viewMatrix.hasPerspective();
348
349 // We attempt to map r by the inverse matrix and draw that. mapRect will
350 // map the four corners and bound them with a new rect. This will not
351 // produce a correct result for some perspective matrices.
352 if (!isPerspective) {
reeda39667c2016-08-22 06:39:49 -0700353 if (!SkMatrixPriv::InverseMapRect(viewMatrix, &r, r)) {
robertphillipsea461502015-05-26 11:38:03 -0700354 SkDebugf("Could not invert matrix\n");
355 return;
356 }
Brian Salomon82f44312017-01-11 13:42:54 -0500357 this->drawRect(clip, std::move(paint), GrAA::kNo, viewMatrix, r);
robertphillipsea461502015-05-26 11:38:03 -0700358 } else {
359 SkMatrix localMatrix;
360 if (!viewMatrix.invert(&localMatrix)) {
361 SkDebugf("Could not invert matrix\n");
362 return;
363 }
364
Robert Phillips72152832017-01-25 17:31:35 -0500365 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700366
Brian Salomon82f44312017-01-11 13:42:54 -0500367 this->drawNonAAFilledRect(clip, std::move(paint), SkMatrix::I(), r, nullptr, &localMatrix,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500368 nullptr, GrAAType::kNone);
robertphillipsea461502015-05-26 11:38:03 -0700369 }
370}
371
robertphillipsea461502015-05-26 11:38:03 -0700372static inline bool rect_contains_inclusive(const SkRect& rect, const SkPoint& point) {
373 return point.fX >= rect.fLeft && point.fX <= rect.fRight &&
374 point.fY >= rect.fTop && point.fY <= rect.fBottom;
375}
376
bsalomonc55271f2015-11-09 11:55:57 -0800377static bool view_matrix_ok_for_aa_fill_rect(const SkMatrix& viewMatrix) {
378 return viewMatrix.preservesRightAngles();
379}
380
csmartdalton97f6cd52016-07-13 13:37:08 -0700381// Attempts to crop a rect and optional local rect to the clip boundaries.
382// Returns false if the draw can be skipped entirely.
robertphillips13a7eee2016-08-31 15:06:24 -0700383static bool crop_filled_rect(int width, int height, const GrClip& clip,
csmartdalton97f6cd52016-07-13 13:37:08 -0700384 const SkMatrix& viewMatrix, SkRect* rect,
385 SkRect* localRect = nullptr) {
386 if (!viewMatrix.rectStaysRect()) {
387 return true;
388 }
389
csmartdalton97f6cd52016-07-13 13:37:08 -0700390 SkIRect clipDevBounds;
391 SkRect clipBounds;
csmartdalton97f6cd52016-07-13 13:37:08 -0700392
robertphillips13a7eee2016-08-31 15:06:24 -0700393 clip.getConservativeBounds(width, height, &clipDevBounds);
reeda39667c2016-08-22 06:39:49 -0700394 if (!SkMatrixPriv::InverseMapRect(viewMatrix, &clipBounds, SkRect::Make(clipDevBounds))) {
395 return false;
396 }
csmartdalton97f6cd52016-07-13 13:37:08 -0700397
398 if (localRect) {
399 if (!rect->intersects(clipBounds)) {
400 return false;
401 }
402 const SkScalar dx = localRect->width() / rect->width();
403 const SkScalar dy = localRect->height() / rect->height();
404 if (clipBounds.fLeft > rect->fLeft) {
405 localRect->fLeft += (clipBounds.fLeft - rect->fLeft) * dx;
406 rect->fLeft = clipBounds.fLeft;
407 }
408 if (clipBounds.fTop > rect->fTop) {
409 localRect->fTop += (clipBounds.fTop - rect->fTop) * dy;
410 rect->fTop = clipBounds.fTop;
411 }
412 if (clipBounds.fRight < rect->fRight) {
413 localRect->fRight -= (rect->fRight - clipBounds.fRight) * dx;
414 rect->fRight = clipBounds.fRight;
415 }
416 if (clipBounds.fBottom < rect->fBottom) {
417 localRect->fBottom -= (rect->fBottom - clipBounds.fBottom) * dy;
418 rect->fBottom = clipBounds.fBottom;
419 }
420 return true;
421 }
422
423 return rect->intersect(clipBounds);
424}
425
Brian Osman11052242016-10-27 14:47:55 -0400426bool GrRenderTargetContext::drawFilledRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500427 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500428 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400429 const SkMatrix& viewMatrix,
430 const SkRect& rect,
431 const GrUserStencilSettings* ss) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700432 SkRect croppedRect = rect;
Robert Phillips784b7bf2016-12-09 13:35:02 -0500433 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix, &croppedRect)) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700434 return true;
435 }
robertphillips44302392016-07-08 14:43:03 -0700436
Brian Salomon54d212e2017-03-21 14:22:38 -0400437 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport() &&
438 (!ss || ss->isDisabled(false))) {
Robert Phillipsf2361d22016-10-25 14:20:06 -0400439 InstancedRendering* ir = this->getOpList()->instancedRendering();
Brian Salomon54d212e2017-03-21 14:22:38 -0400440 std::unique_ptr<GrDrawOp> op = ir->recordRect(croppedRect, viewMatrix, std::move(paint), aa,
441 fInstancedPipelineInfo);
Brian Salomon42521e82016-12-07 16:44:58 -0500442 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -0400443 this->addDrawOp(clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700444 return true;
csmartdaltona7f29642016-07-07 08:49:11 -0700445 }
446 }
Brian Salomon54d212e2017-03-21 14:22:38 -0400447 GrAAType aaType = this->decideAAType(aa);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500448 if (GrAAType::kCoverage == aaType) {
robertphillips391395d2016-03-02 09:26:36 -0800449 // The fill path can handle rotation but not skew.
450 if (view_matrix_ok_for_aa_fill_rect(viewMatrix)) {
451 SkRect devBoundRect;
csmartdalton97f6cd52016-07-13 13:37:08 -0700452 viewMatrix.mapRect(&devBoundRect, croppedRect);
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400453 std::unique_ptr<GrLegacyMeshDrawOp> op =
Brian Salomon649a3412017-03-09 13:50:43 -0500454 GrRectOpFactory::MakeAAFill(paint, viewMatrix, rect, croppedRect, devBoundRect);
Brian Salomon42521e82016-12-07 16:44:58 -0500455 if (op) {
Brian Salomon82f44312017-01-11 13:42:54 -0500456 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
robertphillips44302392016-07-08 14:43:03 -0700457 if (ss) {
458 pipelineBuilder.setUserStencil(ss);
459 }
Brian Salomone14bd802017-04-04 15:13:25 -0400460 this->addLegacyMeshDrawOp(std::move(pipelineBuilder), clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700461 return true;
462 }
robertphillips391395d2016-03-02 09:26:36 -0800463 }
464 } else {
Brian Salomon82f44312017-01-11 13:42:54 -0500465 this->drawNonAAFilledRect(clip, std::move(paint), viewMatrix, croppedRect, nullptr, nullptr,
466 ss, aaType);
robertphillips44302392016-07-08 14:43:03 -0700467 return true;
robertphillips391395d2016-03-02 09:26:36 -0800468 }
469
robertphillips44302392016-07-08 14:43:03 -0700470 return false;
robertphillips391395d2016-03-02 09:26:36 -0800471}
472
Brian Osman11052242016-10-27 14:47:55 -0400473void GrRenderTargetContext::drawRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500474 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500475 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400476 const SkMatrix& viewMatrix,
477 const SkRect& rect,
478 const GrStyle* style) {
bsalomon6663acf2016-05-10 09:14:17 -0700479 if (!style) {
480 style = &GrStyle::SimpleFill();
481 }
joshualitt1de610a2016-01-06 08:26:09 -0800482 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700483 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700484 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400485 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawRect");
robertphillips2e1e51f2015-10-15 08:01:48 -0700486
bsalomon6663acf2016-05-10 09:14:17 -0700487 // Path effects should've been devolved to a path in SkGpuDevice
488 SkASSERT(!style->pathEffect());
robertphillipsea461502015-05-26 11:38:03 -0700489
Robert Phillips72152832017-01-25 17:31:35 -0500490 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700491
bsalomon6663acf2016-05-10 09:14:17 -0700492 const SkStrokeRec& stroke = style->strokeRec();
robertphillips3ab14ca2016-07-10 11:49:39 -0700493 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
494
495 if (!fContext->caps()->useDrawInsteadOfClear()) {
496 // Check if this is a full RT draw and can be replaced with a clear. We don't bother
497 // checking cases where the RT is fully inside a stroke.
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400498 SkRect rtRect = fRenderTargetProxy->getBoundsRect();
robertphillips3ab14ca2016-07-10 11:49:39 -0700499 // Does the clip contain the entire RT?
500 if (clip.quickContains(rtRect)) {
501 SkMatrix invM;
502 if (!viewMatrix.invert(&invM)) {
robertphillipsea461502015-05-26 11:38:03 -0700503 return;
504 }
robertphillips3ab14ca2016-07-10 11:49:39 -0700505 // Does the rect bound the RT?
506 SkPoint srcSpaceRTQuad[4];
507 invM.mapRectToQuad(srcSpaceRTQuad, rtRect);
508 if (rect_contains_inclusive(rect, srcSpaceRTQuad[0]) &&
509 rect_contains_inclusive(rect, srcSpaceRTQuad[1]) &&
510 rect_contains_inclusive(rect, srcSpaceRTQuad[2]) &&
511 rect_contains_inclusive(rect, srcSpaceRTQuad[3])) {
512 // Will it blend?
513 GrColor clearColor;
514 if (paint.isConstantBlendedColor(&clearColor)) {
robertphillips9199a9f2016-07-13 07:48:43 -0700515 this->clear(nullptr, clearColor, true);
robertphillips3ab14ca2016-07-10 11:49:39 -0700516 return;
517 }
518 }
robertphillipsea461502015-05-26 11:38:03 -0700519 }
520 }
robertphillips44302392016-07-08 14:43:03 -0700521
Brian Salomon82f44312017-01-11 13:42:54 -0500522 if (this->drawFilledRect(clip, std::move(paint), aa, viewMatrix, rect, nullptr)) {
robertphillips44302392016-07-08 14:43:03 -0700523 return;
524 }
bsalomona7d85ba2016-07-06 11:54:59 -0700525 } else if (stroke.getStyle() == SkStrokeRec::kStroke_Style ||
526 stroke.getStyle() == SkStrokeRec::kHairline_Style) {
527 if ((!rect.width() || !rect.height()) &&
528 SkStrokeRec::kHairline_Style != stroke.getStyle()) {
529 SkScalar r = stroke.getWidth() / 2;
530 // TODO: Move these stroke->fill fallbacks to GrShape?
531 switch (stroke.getJoin()) {
532 case SkPaint::kMiter_Join:
Brian Salomon82f44312017-01-11 13:42:54 -0500533 this->drawRect(
534 clip, std::move(paint), aa, viewMatrix,
535 {rect.fLeft - r, rect.fTop - r, rect.fRight + r, rect.fBottom + r},
536 &GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700537 return;
538 case SkPaint::kRound_Join:
539 // Raster draws nothing when both dimensions are empty.
540 if (rect.width() || rect.height()){
541 SkRRect rrect = SkRRect::MakeRectXY(rect.makeOutset(r, r), r, r);
Brian Salomon82f44312017-01-11 13:42:54 -0500542 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect,
543 GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700544 return;
545 }
546 case SkPaint::kBevel_Join:
547 if (!rect.width()) {
Brian Salomon82f44312017-01-11 13:42:54 -0500548 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700549 {rect.fLeft - r, rect.fTop, rect.fRight + r, rect.fBottom},
550 &GrStyle::SimpleFill());
551 } else {
Brian Salomon82f44312017-01-11 13:42:54 -0500552 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700553 {rect.fLeft, rect.fTop - r, rect.fRight, rect.fBottom + r},
554 &GrStyle::SimpleFill());
555 }
556 return;
557 }
558 }
robertphillips44302392016-07-08 14:43:03 -0700559
robertphillips44302392016-07-08 14:43:03 -0700560 bool snapToPixelCenters = false;
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400561 std::unique_ptr<GrLegacyMeshDrawOp> op;
robertphillips44302392016-07-08 14:43:03 -0700562
robertphillips391395d2016-03-02 09:26:36 -0800563 GrColor color = paint.getColor();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500564 GrAAType aaType = this->decideAAType(aa);
565 if (GrAAType::kCoverage == aaType) {
cdaltonbb539482016-01-04 09:48:25 -0800566 // The stroke path needs the rect to remain axis aligned (no rotation or skew).
567 if (viewMatrix.rectStaysRect()) {
Brian Salomon6a639042016-12-14 11:08:17 -0500568 op = GrRectOpFactory::MakeAAStroke(color, viewMatrix, rect, stroke);
cdaltonbb539482016-01-04 09:48:25 -0800569 }
robertphillipsea461502015-05-26 11:38:03 -0700570 } else {
robertphillips391395d2016-03-02 09:26:36 -0800571 // Depending on sub-pixel coordinates and the particular GPU, we may lose a corner of
halcanary9d524f22016-03-29 09:03:52 -0700572 // hairline rects. We jam all the vertices to pixel centers to avoid this, but not
robertphillips391395d2016-03-02 09:26:36 -0800573 // when MSAA is enabled because it can cause ugly artifacts.
bsalomona7d85ba2016-07-06 11:54:59 -0700574 snapToPixelCenters = stroke.getStyle() == SkStrokeRec::kHairline_Style &&
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400575 !fRenderTargetProxy->isUnifiedMultisampled();
Brian Salomon6a639042016-12-14 11:08:17 -0500576 op = GrRectOpFactory::MakeNonAAStroke(color, viewMatrix, rect, stroke,
577 snapToPixelCenters);
robertphillips391395d2016-03-02 09:26:36 -0800578 }
robertphillips4bc31812016-03-01 12:22:49 -0800579
Brian Salomon42521e82016-12-07 16:44:58 -0500580 if (op) {
Brian Salomon82f44312017-01-11 13:42:54 -0500581 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
Brian Salomon189098e72017-01-19 09:55:19 -0500582 pipelineBuilder.setSnapVerticesToPixelCenters(snapToPixelCenters);
Brian Salomone14bd802017-04-04 15:13:25 -0400583 this->addLegacyMeshDrawOp(std::move(pipelineBuilder), clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700584 return;
robertphillips4bc31812016-03-01 12:22:49 -0800585 }
robertphillips4bc31812016-03-01 12:22:49 -0800586 }
halcanary9d524f22016-03-29 09:03:52 -0700587
robertphillips4bc31812016-03-01 12:22:49 -0800588 SkPath path;
589 path.setIsVolatile(true);
590 path.addRect(rect);
Brian Salomon82f44312017-01-11 13:42:54 -0500591 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, *style);
robertphillipsea461502015-05-26 11:38:03 -0700592}
593
Robert Phillipsec2249f2016-11-09 08:54:35 -0500594int GrRenderTargetContextPriv::maxWindowRectangles() const {
595 return fRenderTargetContext->fRenderTargetProxy->maxWindowRectangles(
596 *fRenderTargetContext->fContext->caps());
597}
598
Brian Osman11052242016-10-27 14:47:55 -0400599void GrRenderTargetContextPriv::clearStencilClip(const GrFixedClip& clip, bool insideStencilMask) {
robertphillips976f5f02016-06-03 10:59:20 -0700600 ASSERT_SINGLE_OWNER_PRIV
601 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400602 SkDEBUGCODE(fRenderTargetContext->validate();)
603 GR_AUDIT_TRAIL_AUTO_FRAME(fRenderTargetContext->fAuditTrail,
604 "GrRenderTargetContextPriv::clearStencilClip");
robertphillips976f5f02016-06-03 10:59:20 -0700605
Robert Phillips72152832017-01-25 17:31:35 -0500606 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400607
608 std::unique_ptr<GrOp> op(GrClearStencilClipOp::Make(clip, insideStencilMask,
609 fRenderTargetContext));
610 if (!op) {
Robert Phillipse60ad622016-11-17 10:22:48 -0500611 return;
612 }
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400613 fRenderTargetContext->getOpList()->addOp(std::move(op), fRenderTargetContext);
robertphillips976f5f02016-06-03 10:59:20 -0700614}
615
Brian Osman11052242016-10-27 14:47:55 -0400616void GrRenderTargetContextPriv::stencilPath(const GrClip& clip,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500617 GrAAType aaType,
Brian Osman11052242016-10-27 14:47:55 -0400618 const SkMatrix& viewMatrix,
619 const GrPath* path) {
Brian Salomon467921e2017-03-06 16:17:12 -0500620 ASSERT_SINGLE_OWNER_PRIV
621 RETURN_IF_ABANDONED_PRIV
622 SkDEBUGCODE(fRenderTargetContext->validate();)
623 GR_AUDIT_TRAIL_AUTO_FRAME(fRenderTargetContext->fAuditTrail,
624 "GrRenderTargetContext::stencilPath");
625
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500626 SkASSERT(aaType != GrAAType::kCoverage);
Brian Salomon467921e2017-03-06 16:17:12 -0500627
628 bool useHWAA = GrAATypeIsHW(aaType);
629 // TODO: extract portions of checkDraw that are relevant to path stenciling.
630 SkASSERT(path);
631 SkASSERT(fRenderTargetContext->caps()->shaderCaps()->pathRenderingSupport());
632
633 // FIXME: Use path bounds instead of this WAR once
634 // https://bugs.chromium.org/p/skia/issues/detail?id=5640 is resolved.
635 SkRect bounds = SkRect::MakeIWH(fRenderTargetContext->width(), fRenderTargetContext->height());
636
637 // Setup clip
Brian Salomon97180af2017-03-14 13:42:58 -0400638 GrAppliedClip appliedClip;
Brian Salomon467921e2017-03-06 16:17:12 -0500639 if (!clip.apply(fRenderTargetContext->fContext, fRenderTargetContext, useHWAA, true,
Brian Salomon97180af2017-03-14 13:42:58 -0400640 &appliedClip, &bounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -0500641 return;
642 }
643
644 // Coverage AA does not make sense when rendering to the stencil buffer. The caller should never
645 // attempt this in a situation that would require coverage AA.
646 SkASSERT(!appliedClip.clipCoverageFragmentProcessor());
647
648 GrRenderTarget* rt = fRenderTargetContext->accessRenderTarget();
649 if (!rt) {
650 return;
651 }
652 GrStencilAttachment* stencilAttachment =
653 fRenderTargetContext->fContext->resourceProvider()->attachStencilAttachment(rt);
654 if (!stencilAttachment) {
655 SkDebugf("ERROR creating stencil attachment. Draw skipped.\n");
656 return;
657 }
658
659 std::unique_ptr<GrOp> op = GrStencilPathOp::Make(viewMatrix,
660 useHWAA,
661 path->getFillType(),
662 appliedClip.hasStencilClip(),
663 stencilAttachment->bits(),
664 appliedClip.scissorState(),
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400665 fRenderTargetContext,
Brian Salomon467921e2017-03-06 16:17:12 -0500666 path);
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400667 if (!op) {
668 return;
669 }
Brian Salomon97180af2017-03-14 13:42:58 -0400670 op->setClippedBounds(bounds);
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400671 fRenderTargetContext->getOpList()->addOp(std::move(op), fRenderTargetContext);
robertphillips976f5f02016-06-03 10:59:20 -0700672}
673
Brian Osman11052242016-10-27 14:47:55 -0400674void GrRenderTargetContextPriv::stencilRect(const GrClip& clip,
675 const GrUserStencilSettings* ss,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500676 GrAAType aaType,
Brian Osman11052242016-10-27 14:47:55 -0400677 const SkMatrix& viewMatrix,
678 const SkRect& rect) {
robertphillips976f5f02016-06-03 10:59:20 -0700679 ASSERT_SINGLE_OWNER_PRIV
680 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400681 SkDEBUGCODE(fRenderTargetContext->validate();)
682 GR_AUDIT_TRAIL_AUTO_FRAME(fRenderTargetContext->fAuditTrail,
683 "GrRenderTargetContext::stencilRect");
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500684 SkASSERT(GrAAType::kCoverage != aaType);
Robert Phillips72152832017-01-25 17:31:35 -0500685 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips976f5f02016-06-03 10:59:20 -0700686
687 GrPaint paint;
Brian Salomona1633922017-01-09 11:46:10 -0500688 paint.setXPFactory(GrDisableColorXPFactory::Get());
robertphillips976f5f02016-06-03 10:59:20 -0700689
Brian Salomon82f44312017-01-11 13:42:54 -0500690 fRenderTargetContext->drawNonAAFilledRect(clip, std::move(paint), viewMatrix, rect, nullptr,
691 nullptr, ss, aaType);
robertphillips976f5f02016-06-03 10:59:20 -0700692}
693
Brian Osman11052242016-10-27 14:47:55 -0400694bool GrRenderTargetContextPriv::drawAndStencilRect(const GrClip& clip,
695 const GrUserStencilSettings* ss,
696 SkRegion::Op op,
697 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500698 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400699 const SkMatrix& viewMatrix,
700 const SkRect& rect) {
robertphillips391395d2016-03-02 09:26:36 -0800701 ASSERT_SINGLE_OWNER_PRIV
702 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400703 SkDEBUGCODE(fRenderTargetContext->validate();)
704 GR_AUDIT_TRAIL_AUTO_FRAME(fRenderTargetContext->fAuditTrail,
705 "GrRenderTargetContext::drawAndStencilRect");
robertphillips391395d2016-03-02 09:26:36 -0800706
Robert Phillips72152832017-01-25 17:31:35 -0500707 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -0800708
709 GrPaint paint;
robertphillips391395d2016-03-02 09:26:36 -0800710 paint.setCoverageSetOpXPFactory(op, invert);
711
Brian Salomon82f44312017-01-11 13:42:54 -0500712 if (fRenderTargetContext->drawFilledRect(clip, std::move(paint), aa, viewMatrix, rect, ss)) {
robertphillips391395d2016-03-02 09:26:36 -0800713 return true;
714 }
robertphillips391395d2016-03-02 09:26:36 -0800715 SkPath path;
716 path.setIsVolatile(true);
717 path.addRect(rect);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500718 return this->drawAndStencilPath(clip, ss, op, invert, aa, viewMatrix, path);
robertphillips391395d2016-03-02 09:26:36 -0800719}
720
Brian Osman11052242016-10-27 14:47:55 -0400721void GrRenderTargetContext::fillRectToRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500722 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500723 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400724 const SkMatrix& viewMatrix,
725 const SkRect& rectToDraw,
726 const SkRect& localRect) {
joshualitt1de610a2016-01-06 08:26:09 -0800727 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700728 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700729 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400730 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::fillRectToRect");
robertphillipsea461502015-05-26 11:38:03 -0700731
csmartdalton97f6cd52016-07-13 13:37:08 -0700732 SkRect croppedRect = rectToDraw;
733 SkRect croppedLocalRect = localRect;
robertphillips13a7eee2016-08-31 15:06:24 -0700734 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix,
735 &croppedRect, &croppedLocalRect)) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700736 return;
737 }
738
Robert Phillips72152832017-01-25 17:31:35 -0500739 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -0700740
csmartdaltone0d36292016-07-29 08:14:20 -0700741 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport()) {
Robert Phillipsf2361d22016-10-25 14:20:06 -0400742 InstancedRendering* ir = this->getOpList()->instancedRendering();
Brian Salomon54d212e2017-03-21 14:22:38 -0400743 std::unique_ptr<GrDrawOp> op(ir->recordRect(croppedRect, viewMatrix, std::move(paint),
744 croppedLocalRect, aa, fInstancedPipelineInfo));
Brian Salomon42521e82016-12-07 16:44:58 -0500745 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -0400746 this->addDrawOp(clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -0700747 return;
748 }
749 }
750
Brian Salomon54d212e2017-03-21 14:22:38 -0400751 GrAAType aaType = this->decideAAType(aa);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500752 if (GrAAType::kCoverage != aaType) {
Brian Salomon82f44312017-01-11 13:42:54 -0500753 this->drawNonAAFilledRect(clip, std::move(paint), viewMatrix, croppedRect,
754 &croppedLocalRect, nullptr, nullptr, aaType);
csmartdaltonfc49d562016-07-26 17:05:47 -0700755 return;
joshualitt04194f32016-01-13 10:08:27 -0800756 }
bsalomonbb243832016-07-22 07:10:19 -0700757
csmartdaltonfc49d562016-07-26 17:05:47 -0700758 if (view_matrix_ok_for_aa_fill_rect(viewMatrix)) {
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400759 std::unique_ptr<GrLegacyMeshDrawOp> op = GrAAFillRectOp::MakeWithLocalRect(
Brian Salomonf8334782017-01-03 09:42:58 -0500760 paint.getColor(), viewMatrix, croppedRect, croppedLocalRect);
Brian Salomon82f44312017-01-11 13:42:54 -0500761 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
Brian Salomone14bd802017-04-04 15:13:25 -0400762 this->addLegacyMeshDrawOp(std::move(pipelineBuilder), clip, std::move(op));
csmartdaltonfc49d562016-07-26 17:05:47 -0700763 return;
764 }
765
766 SkMatrix viewAndUnLocalMatrix;
767 if (!viewAndUnLocalMatrix.setRectToRect(localRect, rectToDraw, SkMatrix::kFill_ScaleToFit)) {
768 SkDebugf("fillRectToRect called with empty local matrix.\n");
769 return;
770 }
771 viewAndUnLocalMatrix.postConcat(viewMatrix);
772
773 SkPath path;
774 path.setIsVolatile(true);
775 path.addRect(localRect);
Brian Salomon82f44312017-01-11 13:42:54 -0500776 this->internalDrawPath(clip, std::move(paint), aa, viewAndUnLocalMatrix, path, GrStyle());
joshualittb6b513b2015-08-21 10:25:18 -0700777}
778
Brian Osman11052242016-10-27 14:47:55 -0400779void GrRenderTargetContext::fillRectWithLocalMatrix(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500780 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500781 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400782 const SkMatrix& viewMatrix,
783 const SkRect& rectToDraw,
784 const SkMatrix& localMatrix) {
joshualitt1de610a2016-01-06 08:26:09 -0800785 ASSERT_SINGLE_OWNER
joshualittb6b513b2015-08-21 10:25:18 -0700786 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700787 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400788 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::fillRectWithLocalMatrix");
joshualittb6b513b2015-08-21 10:25:18 -0700789
csmartdalton97f6cd52016-07-13 13:37:08 -0700790 SkRect croppedRect = rectToDraw;
robertphillips13a7eee2016-08-31 15:06:24 -0700791 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix, &croppedRect)) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700792 return;
793 }
794
Robert Phillips72152832017-01-25 17:31:35 -0500795 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -0700796
csmartdaltone0d36292016-07-29 08:14:20 -0700797 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport()) {
Robert Phillipsf2361d22016-10-25 14:20:06 -0400798 InstancedRendering* ir = this->getOpList()->instancedRendering();
Brian Salomon54d212e2017-03-21 14:22:38 -0400799 std::unique_ptr<GrDrawOp> op(ir->recordRect(croppedRect, viewMatrix, std::move(paint),
800 localMatrix, aa, fInstancedPipelineInfo));
Brian Salomon42521e82016-12-07 16:44:58 -0500801 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -0400802 this->addDrawOp(clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -0700803 return;
804 }
805 }
806
Brian Salomon54d212e2017-03-21 14:22:38 -0400807 GrAAType aaType = this->decideAAType(aa);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500808 if (GrAAType::kCoverage != aaType) {
Brian Salomon82f44312017-01-11 13:42:54 -0500809 this->drawNonAAFilledRect(clip, std::move(paint), viewMatrix, croppedRect, nullptr,
810 &localMatrix, nullptr, aaType);
csmartdaltonfc49d562016-07-26 17:05:47 -0700811 return;
bsalomonc55271f2015-11-09 11:55:57 -0800812 }
robertphillips4bc31812016-03-01 12:22:49 -0800813
csmartdaltonfc49d562016-07-26 17:05:47 -0700814 if (view_matrix_ok_for_aa_fill_rect(viewMatrix)) {
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400815 std::unique_ptr<GrLegacyMeshDrawOp> op =
Brian Salomon6a639042016-12-14 11:08:17 -0500816 GrAAFillRectOp::Make(paint.getColor(), viewMatrix, localMatrix, croppedRect);
Brian Salomon82f44312017-01-11 13:42:54 -0500817 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
Brian Salomone14bd802017-04-04 15:13:25 -0400818 this->addLegacyMeshDrawOp(std::move(pipelineBuilder), clip, std::move(op));
csmartdaltonfc49d562016-07-26 17:05:47 -0700819 return;
820 }
821
822 SkMatrix viewAndUnLocalMatrix;
823 if (!localMatrix.invert(&viewAndUnLocalMatrix)) {
824 SkDebugf("fillRectWithLocalMatrix called with degenerate local matrix.\n");
825 return;
826 }
827 viewAndUnLocalMatrix.postConcat(viewMatrix);
828
829 SkPath path;
830 path.setIsVolatile(true);
831 path.addRect(rectToDraw);
832 path.transform(localMatrix);
Brian Salomon82f44312017-01-11 13:42:54 -0500833 this->internalDrawPath(clip, std::move(paint), aa, viewAndUnLocalMatrix, path, GrStyle());
robertphillipsea461502015-05-26 11:38:03 -0700834}
835
Brian Osman11052242016-10-27 14:47:55 -0400836void GrRenderTargetContext::drawVertices(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500837 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400838 const SkMatrix& viewMatrix,
839 GrPrimitiveType primitiveType,
840 int vertexCount,
841 const SkPoint positions[],
842 const SkPoint texCoords[],
Brian Salomon3de0aee2017-01-29 09:34:17 -0500843 const uint32_t colors[],
Brian Osman11052242016-10-27 14:47:55 -0400844 const uint16_t indices[],
Brian Salomon3de0aee2017-01-29 09:34:17 -0500845 int indexCount,
846 ColorArrayType colorArrayType) {
joshualitt1de610a2016-01-06 08:26:09 -0800847 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700848 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700849 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400850 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawVertices");
robertphillipsea461502015-05-26 11:38:03 -0700851
Robert Phillips72152832017-01-25 17:31:35 -0500852 AutoCheckFlush acf(this->drawingManager());
robertphillips2e1e51f2015-10-15 08:01:48 -0700853
robertphillipsea461502015-05-26 11:38:03 -0700854 // TODO clients should give us bounds
855 SkRect bounds;
856 if (!bounds.setBoundsCheck(positions, vertexCount)) {
857 SkDebugf("drawVertices call empty bounds\n");
858 return;
859 }
860
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400861 std::unique_ptr<GrLegacyMeshDrawOp> op = GrDrawVerticesOp::Make(
Brian Salomon3de0aee2017-01-29 09:34:17 -0500862 paint.getColor(), primitiveType, viewMatrix, positions, vertexCount, indices,
863 indexCount, colors, texCoords, bounds, colorArrayType);
Brian Salomon199fb872017-02-06 09:41:10 -0500864 if (!op) {
865 return;
866 }
867 GrPipelineBuilder pipelineBuilder(std::move(paint), GrAAType::kNone);
Brian Salomone14bd802017-04-04 15:13:25 -0400868 this->addLegacyMeshDrawOp(std::move(pipelineBuilder), clip, std::move(op));
Brian Salomon199fb872017-02-06 09:41:10 -0500869}
robertphillipsea461502015-05-26 11:38:03 -0700870
Brian Salomon199fb872017-02-06 09:41:10 -0500871void GrRenderTargetContext::drawVertices(const GrClip& clip,
872 GrPaint&& paint,
873 const SkMatrix& viewMatrix,
Mike Reed5fa66452017-03-16 09:06:34 -0400874 sk_sp<SkVertices> vertices) {
Brian Salomon199fb872017-02-06 09:41:10 -0500875 ASSERT_SINGLE_OWNER
876 RETURN_IF_ABANDONED
877 SkDEBUGCODE(this->validate();)
878 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawVertices");
879
880 AutoCheckFlush acf(this->drawingManager());
881
882 SkASSERT(vertices);
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400883 std::unique_ptr<GrLegacyMeshDrawOp> op =
Mike Reed5fa66452017-03-16 09:06:34 -0400884 GrDrawVerticesOp::Make(paint.getColor(), std::move(vertices), viewMatrix);
Brian Salomon199fb872017-02-06 09:41:10 -0500885 if (!op) {
886 return;
887 }
Brian Salomon82f44312017-01-11 13:42:54 -0500888 GrPipelineBuilder pipelineBuilder(std::move(paint), GrAAType::kNone);
Brian Salomone14bd802017-04-04 15:13:25 -0400889 this->addLegacyMeshDrawOp(std::move(pipelineBuilder), clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -0700890}
891
892///////////////////////////////////////////////////////////////////////////////
893
Brian Osman11052242016-10-27 14:47:55 -0400894void GrRenderTargetContext::drawAtlas(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500895 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400896 const SkMatrix& viewMatrix,
897 int spriteCount,
898 const SkRSXform xform[],
899 const SkRect texRect[],
900 const SkColor colors[]) {
joshualitt1de610a2016-01-06 08:26:09 -0800901 ASSERT_SINGLE_OWNER
jvanverth31ff7622015-08-07 10:09:28 -0700902 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700903 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400904 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawAtlas");
robertphillips2e1e51f2015-10-15 08:01:48 -0700905
Robert Phillips72152832017-01-25 17:31:35 -0500906 AutoCheckFlush acf(this->drawingManager());
halcanary9d524f22016-03-29 09:03:52 -0700907
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400908 std::unique_ptr<GrLegacyMeshDrawOp> op =
Brian Salomonfc527d22016-12-14 21:07:01 -0500909 GrDrawAtlasOp::Make(paint.getColor(), viewMatrix, spriteCount, xform, texRect, colors);
Brian Salomon82f44312017-01-11 13:42:54 -0500910 GrPipelineBuilder pipelineBuilder(std::move(paint), GrAAType::kNone);
Brian Salomone14bd802017-04-04 15:13:25 -0400911 this->addLegacyMeshDrawOp(std::move(pipelineBuilder), clip, std::move(op));
jvanverth31ff7622015-08-07 10:09:28 -0700912}
913
914///////////////////////////////////////////////////////////////////////////////
915
Brian Osman11052242016-10-27 14:47:55 -0400916void GrRenderTargetContext::drawRRect(const GrClip& origClip,
Brian Salomon82f44312017-01-11 13:42:54 -0500917 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500918 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400919 const SkMatrix& viewMatrix,
920 const SkRRect& rrect,
921 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -0800922 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700923 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700924 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -0400925 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawRRect");
robertphillipsea461502015-05-26 11:38:03 -0700926 if (rrect.isEmpty()) {
927 return;
928 }
929
bsalomon7f0d9f32016-08-15 14:49:10 -0700930 GrNoClip noclip;
931 const GrClip* clip = &origClip;
932#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
933 // The Android framework frequently clips rrects to themselves where the clip is non-aa and the
Brian Salomon42521e82016-12-07 16:44:58 -0500934 // draw is aa. Since our lower level clip code works from op bounds, which are SkRects, it
bsalomon7f0d9f32016-08-15 14:49:10 -0700935 // doesn't detect that the clip can be ignored (modulo antialiasing). The following test
936 // attempts to mitigate the stencil clip cost but will only help when the entire clip stack
937 // can be ignored. We'd prefer to fix this in the framework by removing the clips calls.
938 SkRRect devRRect;
939 if (rrect.transform(viewMatrix, &devRRect) && clip->quickContains(devRRect)) {
940 clip = &noclip;
941 }
942#endif
bsalomon6663acf2016-05-10 09:14:17 -0700943 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
ksakamotoec7f2ac2016-07-05 03:54:53 -0700944
Robert Phillips72152832017-01-25 17:31:35 -0500945 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -0700946 const SkStrokeRec stroke = style.strokeRec();
csmartdaltona7f29642016-07-07 08:49:11 -0700947
csmartdaltone0d36292016-07-29 08:14:20 -0700948 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport() &&
949 stroke.isFillStyle()) {
Robert Phillipsf2361d22016-10-25 14:20:06 -0400950 InstancedRendering* ir = this->getOpList()->instancedRendering();
Brian Salomon54d212e2017-03-21 14:22:38 -0400951 std::unique_ptr<GrDrawOp> op(
952 ir->recordRRect(rrect, viewMatrix, std::move(paint), aa, fInstancedPipelineInfo));
Brian Salomon42521e82016-12-07 16:44:58 -0500953 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -0400954 this->addDrawOp(*clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -0700955 return;
956 }
957 }
958
Brian Salomon54d212e2017-03-21 14:22:38 -0400959 GrAAType aaType = this->decideAAType(aa);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500960 if (GrAAType::kCoverage == aaType) {
Brian Salomon94efbf52016-11-29 13:43:05 -0500961 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400962 std::unique_ptr<GrLegacyMeshDrawOp> op =
Brian Salomon649a3412017-03-09 13:50:43 -0500963 GrOvalOpFactory::MakeRRectOp(paint.getColor(),
964 paint.usesDistanceVectorField(),
965 viewMatrix,
966 rrect,
967 stroke,
968 shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -0500969 if (op) {
Brian Salomon82f44312017-01-11 13:42:54 -0500970 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
Brian Salomone14bd802017-04-04 15:13:25 -0400971 this->addLegacyMeshDrawOp(std::move(pipelineBuilder), *clip, std::move(op));
robertphillipsb56f9272016-02-25 11:03:52 -0800972 return;
973 }
robertphillipsea461502015-05-26 11:38:03 -0700974 }
robertphillipsb56f9272016-02-25 11:03:52 -0800975
976 SkPath path;
977 path.setIsVolatile(true);
978 path.addRRect(rrect);
Brian Salomon82f44312017-01-11 13:42:54 -0500979 this->internalDrawPath(*clip, std::move(paint), aa, viewMatrix, path, style);
robertphillipsea461502015-05-26 11:38:03 -0700980}
981
Jim Van Verthc5903412016-11-17 15:27:09 -0500982///////////////////////////////////////////////////////////////////////////////
983
984void GrRenderTargetContext::drawShadowRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500985 GrPaint&& paint,
Jim Van Verthc5903412016-11-17 15:27:09 -0500986 const SkMatrix& viewMatrix,
987 const SkRRect& rrect,
988 SkScalar blurRadius,
989 const GrStyle& style) {
990 ASSERT_SINGLE_OWNER
991 RETURN_IF_ABANDONED
992 SkDEBUGCODE(this->validate();)
993 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawShadowRRect");
994 if (rrect.isEmpty()) {
995 return;
996 }
997
998 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
999
Robert Phillips72152832017-01-25 17:31:35 -05001000 AutoCheckFlush acf(this->drawingManager());
Jim Van Verthc5903412016-11-17 15:27:09 -05001001 const SkStrokeRec stroke = style.strokeRec();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001002 // TODO: add instancing support?
Jim Van Verthc5903412016-11-17 15:27:09 -05001003
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001004 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomond3ccb0a2017-04-03 10:38:00 -04001005 std::unique_ptr<GrLegacyMeshDrawOp> op = GrShadowRRectOp::Make(
1006 paint.getColor(), viewMatrix, rrect, blurRadius, stroke, shaderCaps);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001007 if (op) {
Brian Salomon82f44312017-01-11 13:42:54 -05001008 GrPipelineBuilder pipelineBuilder(std::move(paint), GrAAType::kNone);
Brian Salomone14bd802017-04-04 15:13:25 -04001009 this->addLegacyMeshDrawOp(std::move(pipelineBuilder), clip, std::move(op));
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001010 return;
Jim Van Verthc5903412016-11-17 15:27:09 -05001011 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001012}
1013
1014///////////////////////////////////////////////////////////////////////////////
1015
Brian Osman11052242016-10-27 14:47:55 -04001016bool GrRenderTargetContext::drawFilledDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001017 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001018 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001019 const SkMatrix& viewMatrix,
1020 const SkRRect& origOuter,
1021 const SkRRect& origInner) {
robertphillips00095892016-02-29 13:50:40 -08001022 SkASSERT(!origInner.isEmpty());
1023 SkASSERT(!origOuter.isEmpty());
1024
csmartdaltone0d36292016-07-29 08:14:20 -07001025 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport()) {
Robert Phillipsf2361d22016-10-25 14:20:06 -04001026 InstancedRendering* ir = this->getOpList()->instancedRendering();
Brian Salomon54d212e2017-03-21 14:22:38 -04001027 std::unique_ptr<GrDrawOp> op(ir->recordDRRect(
1028 origOuter, origInner, viewMatrix, std::move(paint), aa, fInstancedPipelineInfo));
Brian Salomon42521e82016-12-07 16:44:58 -05001029 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -04001030 this->addDrawOp(clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -07001031 return true;
1032 }
1033 }
1034
Brian Salomon54d212e2017-03-21 14:22:38 -04001035 GrAAType aaType = this->decideAAType(aa);
robertphillips00095892016-02-29 13:50:40 -08001036
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001037 GrPrimitiveEdgeType innerEdgeType, outerEdgeType;
1038 if (GrAAType::kCoverage == aaType) {
1039 innerEdgeType = kInverseFillAA_GrProcessorEdgeType;
1040 outerEdgeType = kFillAA_GrProcessorEdgeType;
1041 } else {
1042 innerEdgeType = kInverseFillBW_GrProcessorEdgeType;
1043 outerEdgeType = kFillBW_GrProcessorEdgeType;
1044 }
robertphillips00095892016-02-29 13:50:40 -08001045
1046 SkTCopyOnFirstWrite<SkRRect> inner(origInner), outer(origOuter);
1047 SkMatrix inverseVM;
1048 if (!viewMatrix.isIdentity()) {
1049 if (!origInner.transform(viewMatrix, inner.writable())) {
1050 return false;
1051 }
1052 if (!origOuter.transform(viewMatrix, outer.writable())) {
1053 return false;
1054 }
1055 if (!viewMatrix.invert(&inverseVM)) {
1056 return false;
1057 }
1058 } else {
1059 inverseVM.reset();
halcanary9d524f22016-03-29 09:03:52 -07001060 }
robertphillips00095892016-02-29 13:50:40 -08001061
robertphillips00095892016-02-29 13:50:40 -08001062 // TODO these need to be a geometry processors
bungeman06ca8ec2016-06-09 08:01:03 -07001063 sk_sp<GrFragmentProcessor> innerEffect(GrRRectEffect::Make(innerEdgeType, *inner));
robertphillips00095892016-02-29 13:50:40 -08001064 if (!innerEffect) {
1065 return false;
1066 }
1067
bungeman06ca8ec2016-06-09 08:01:03 -07001068 sk_sp<GrFragmentProcessor> outerEffect(GrRRectEffect::Make(outerEdgeType, *outer));
robertphillips00095892016-02-29 13:50:40 -08001069 if (!outerEffect) {
1070 return false;
1071 }
1072
Brian Salomon82f44312017-01-11 13:42:54 -05001073 paint.addCoverageFragmentProcessor(std::move(innerEffect));
1074 paint.addCoverageFragmentProcessor(std::move(outerEffect));
robertphillips00095892016-02-29 13:50:40 -08001075
1076 SkRect bounds = outer->getBounds();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001077 if (GrAAType::kCoverage == aaType) {
robertphillips00095892016-02-29 13:50:40 -08001078 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1079 }
halcanary9d524f22016-03-29 09:03:52 -07001080
Brian Salomon82f44312017-01-11 13:42:54 -05001081 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), bounds,
1082 inverseVM);
robertphillips00095892016-02-29 13:50:40 -08001083 return true;
1084}
1085
Brian Osman11052242016-10-27 14:47:55 -04001086void GrRenderTargetContext::drawDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001087 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001088 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001089 const SkMatrix& viewMatrix,
1090 const SkRRect& outer,
1091 const SkRRect& inner) {
robertphillips00095892016-02-29 13:50:40 -08001092 ASSERT_SINGLE_OWNER
1093 RETURN_IF_ABANDONED
1094 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -04001095 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawDRRect");
robertphillips00095892016-02-29 13:50:40 -08001096
1097 SkASSERT(!outer.isEmpty());
1098 SkASSERT(!inner.isEmpty());
1099
Robert Phillips72152832017-01-25 17:31:35 -05001100 AutoCheckFlush acf(this->drawingManager());
robertphillips00095892016-02-29 13:50:40 -08001101
Brian Salomon82f44312017-01-11 13:42:54 -05001102 if (this->drawFilledDRRect(clip, std::move(paint), aa, viewMatrix, outer, inner)) {
robertphillips00095892016-02-29 13:50:40 -08001103 return;
1104 }
1105
1106 SkPath path;
1107 path.setIsVolatile(true);
1108 path.addRRect(inner);
1109 path.addRRect(outer);
1110 path.setFillType(SkPath::kEvenOdd_FillType);
1111
Brian Salomon82f44312017-01-11 13:42:54 -05001112 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, GrStyle::SimpleFill());
robertphillips00095892016-02-29 13:50:40 -08001113}
1114
robertphillipsea461502015-05-26 11:38:03 -07001115///////////////////////////////////////////////////////////////////////////////
1116
msarettcc319b92016-08-25 18:07:18 -07001117static inline bool is_int(float x) {
1118 return x == (float) sk_float_round2int(x);
1119}
1120
Brian Osman11052242016-10-27 14:47:55 -04001121void GrRenderTargetContext::drawRegion(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001122 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001123 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001124 const SkMatrix& viewMatrix,
1125 const SkRegion& region,
1126 const GrStyle& style) {
msarettcc319b92016-08-25 18:07:18 -07001127 ASSERT_SINGLE_OWNER
1128 RETURN_IF_ABANDONED
1129 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -04001130 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawRegion");
msarettcc319b92016-08-25 18:07:18 -07001131
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001132 if (GrAA::kYes == aa) {
Brian Salomonfc527d22016-12-14 21:07:01 -05001133 // GrRegionOp performs no antialiasing but is much faster, so here we check the matrix
Brian Salomonc57c7c92016-12-06 14:47:34 -05001134 // to see whether aa is really required.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001135 if (!SkToBool(viewMatrix.getType() & ~(SkMatrix::kTranslate_Mask)) &&
1136 is_int(viewMatrix.getTranslateX()) &&
1137 is_int(viewMatrix.getTranslateY())) {
1138 aa = GrAA::kNo;
1139 }
Brian Salomonc57c7c92016-12-06 14:47:34 -05001140 }
msarettcc319b92016-08-25 18:07:18 -07001141 bool complexStyle = !style.isSimpleFill();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001142 if (complexStyle || GrAA::kYes == aa) {
msarettcc319b92016-08-25 18:07:18 -07001143 SkPath path;
1144 region.getBoundaryPath(&path);
Brian Salomon82f44312017-01-11 13:42:54 -05001145 return this->drawPath(clip, std::move(paint), aa, viewMatrix, path, style);
msarettcc319b92016-08-25 18:07:18 -07001146 }
1147
Brian Salomond3ccb0a2017-04-03 10:38:00 -04001148 std::unique_ptr<GrLegacyMeshDrawOp> op = GrRegionOp::Make(paint.getColor(), viewMatrix, region);
Brian Salomon82f44312017-01-11 13:42:54 -05001149 GrPipelineBuilder pipelineBuilder(std::move(paint), GrAAType::kNone);
Brian Salomone14bd802017-04-04 15:13:25 -04001150 this->addLegacyMeshDrawOp(std::move(pipelineBuilder), clip, std::move(op));
msarettcc319b92016-08-25 18:07:18 -07001151}
1152
Brian Osman11052242016-10-27 14:47:55 -04001153void GrRenderTargetContext::drawOval(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001154 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001155 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001156 const SkMatrix& viewMatrix,
1157 const SkRect& oval,
1158 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001159 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001160 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001161 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -04001162 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawOval");
robertphillips2e1e51f2015-10-15 08:01:48 -07001163
robertphillipsea461502015-05-26 11:38:03 -07001164 if (oval.isEmpty()) {
1165 return;
1166 }
1167
bsalomon6663acf2016-05-10 09:14:17 -07001168 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
robertphillipsea461502015-05-26 11:38:03 -07001169
Robert Phillips72152832017-01-25 17:31:35 -05001170 AutoCheckFlush acf(this->drawingManager());
bsalomon6663acf2016-05-10 09:14:17 -07001171 const SkStrokeRec& stroke = style.strokeRec();
csmartdaltona7f29642016-07-07 08:49:11 -07001172
csmartdaltone0d36292016-07-29 08:14:20 -07001173 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport() &&
1174 stroke.isFillStyle()) {
Robert Phillipsf2361d22016-10-25 14:20:06 -04001175 InstancedRendering* ir = this->getOpList()->instancedRendering();
Brian Salomon54d212e2017-03-21 14:22:38 -04001176 std::unique_ptr<GrDrawOp> op(
1177 ir->recordOval(oval, viewMatrix, std::move(paint), aa, fInstancedPipelineInfo));
Brian Salomon42521e82016-12-07 16:44:58 -05001178 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -04001179 this->addDrawOp(clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -07001180 return;
1181 }
1182 }
1183
Brian Salomon54d212e2017-03-21 14:22:38 -04001184 GrAAType aaType = this->decideAAType(aa);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001185 if (GrAAType::kCoverage == aaType) {
Brian Salomon94efbf52016-11-29 13:43:05 -05001186 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomond3ccb0a2017-04-03 10:38:00 -04001187 std::unique_ptr<GrLegacyMeshDrawOp> op =
Brian Salomon289e3d82016-12-14 15:52:56 -05001188 GrOvalOpFactory::MakeOvalOp(paint.getColor(), viewMatrix, oval, stroke, shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -05001189 if (op) {
Brian Salomon82f44312017-01-11 13:42:54 -05001190 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
Brian Salomone14bd802017-04-04 15:13:25 -04001191 this->addLegacyMeshDrawOp(std::move(pipelineBuilder), clip, std::move(op));
robertphillipsb56f9272016-02-25 11:03:52 -08001192 return;
1193 }
robertphillipsea461502015-05-26 11:38:03 -07001194 }
robertphillipsb56f9272016-02-25 11:03:52 -08001195
1196 SkPath path;
1197 path.setIsVolatile(true);
1198 path.addOval(oval);
Brian Salomon82f44312017-01-11 13:42:54 -05001199 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, style);
robertphillipsea461502015-05-26 11:38:03 -07001200}
1201
Brian Osman11052242016-10-27 14:47:55 -04001202void GrRenderTargetContext::drawArc(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001203 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001204 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001205 const SkMatrix& viewMatrix,
1206 const SkRect& oval,
1207 SkScalar startAngle,
1208 SkScalar sweepAngle,
1209 bool useCenter,
1210 const GrStyle& style) {
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001211 ASSERT_SINGLE_OWNER
1212 RETURN_IF_ABANDONED
1213 SkDEBUGCODE(this->validate();)
1214 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawArc");
1215
1216 AutoCheckFlush acf(this->drawingManager());
1217
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001218 GrAAType aaType = this->decideAAType(aa);
1219 if (GrAAType::kCoverage == aaType) {
Brian Salomon94efbf52016-11-29 13:43:05 -05001220 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomond3ccb0a2017-04-03 10:38:00 -04001221 std::unique_ptr<GrLegacyMeshDrawOp> op = GrOvalOpFactory::MakeArcOp(paint.getColor(),
1222 viewMatrix,
1223 oval,
1224 startAngle,
1225 sweepAngle,
1226 useCenter,
1227 style,
1228 shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -05001229 if (op) {
Brian Salomon82f44312017-01-11 13:42:54 -05001230 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
Brian Salomone14bd802017-04-04 15:13:25 -04001231 this->addLegacyMeshDrawOp(std::move(pipelineBuilder), clip, std::move(op));
bsalomon4f3a0ca2016-08-22 13:14:26 -07001232 return;
1233 }
1234 }
1235 SkPath path;
bsalomon21af9ca2016-08-25 12:29:23 -07001236 SkPathPriv::CreateDrawArcPath(&path, oval, startAngle, sweepAngle, useCenter,
1237 style.isSimpleFill());
Brian Salomon82f44312017-01-11 13:42:54 -05001238 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, style);
bsalomon4f3a0ca2016-08-22 13:14:26 -07001239}
1240
Brian Osman11052242016-10-27 14:47:55 -04001241void GrRenderTargetContext::drawImageLattice(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001242 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001243 const SkMatrix& viewMatrix,
1244 int imageWidth,
1245 int imageHeight,
1246 std::unique_ptr<SkLatticeIter> iter,
1247 const SkRect& dst) {
joshualitt1de610a2016-01-06 08:26:09 -08001248 ASSERT_SINGLE_OWNER
joshualitt33a5fce2015-11-18 13:28:51 -08001249 RETURN_IF_ABANDONED
1250 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -04001251 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawImageLattice");
joshualitt33a5fce2015-11-18 13:28:51 -08001252
Robert Phillips72152832017-01-25 17:31:35 -05001253 AutoCheckFlush acf(this->drawingManager());
joshualitt33a5fce2015-11-18 13:28:51 -08001254
Brian Salomond3ccb0a2017-04-03 10:38:00 -04001255 std::unique_ptr<GrLegacyMeshDrawOp> op = GrLatticeOp::MakeNonAA(
Brian Salomon649a3412017-03-09 13:50:43 -05001256 paint.getColor(), viewMatrix, imageWidth, imageHeight, std::move(iter), dst);
joshualitt33a5fce2015-11-18 13:28:51 -08001257
Brian Salomon82f44312017-01-11 13:42:54 -05001258 GrPipelineBuilder pipelineBuilder(std::move(paint), GrAAType::kNone);
Brian Salomone14bd802017-04-04 15:13:25 -04001259 this->addLegacyMeshDrawOp(std::move(pipelineBuilder), clip, std::move(op));
joshualitt33a5fce2015-11-18 13:28:51 -08001260}
1261
Brian Osman11052242016-10-27 14:47:55 -04001262void GrRenderTargetContext::prepareForExternalIO() {
robertphillips8c523e02016-07-26 07:41:00 -07001263 ASSERT_SINGLE_OWNER
1264 RETURN_IF_ABANDONED
1265 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -04001266 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::prepareForExternalIO");
robertphillips8c523e02016-07-26 07:41:00 -07001267
Robert Phillips7ee385e2017-03-30 08:02:11 -04001268 this->drawingManager()->prepareSurfaceForExternalIO(fRenderTargetProxy.get());
robertphillips8c523e02016-07-26 07:41:00 -07001269}
joshualitt33a5fce2015-11-18 13:28:51 -08001270
Brian Osman11052242016-10-27 14:47:55 -04001271void GrRenderTargetContext::drawNonAAFilledRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001272 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001273 const SkMatrix& viewMatrix,
1274 const SkRect& rect,
1275 const SkRect* localRect,
1276 const SkMatrix* localMatrix,
1277 const GrUserStencilSettings* ss,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001278 GrAAType hwOrNoneAAType) {
1279 SkASSERT(GrAAType::kCoverage != hwOrNoneAAType);
1280 SkASSERT(hwOrNoneAAType == GrAAType::kNone || this->isStencilBufferMultisampled());
Brian Salomond3ccb0a2017-04-03 10:38:00 -04001281 std::unique_ptr<GrLegacyMeshDrawOp> op = GrRectOpFactory::MakeNonAAFill(
1282 paint.getColor(), viewMatrix, rect, localRect, localMatrix);
Brian Salomon82f44312017-01-11 13:42:54 -05001283 GrPipelineBuilder pipelineBuilder(std::move(paint), hwOrNoneAAType);
robertphillips44302392016-07-08 14:43:03 -07001284 if (ss) {
1285 pipelineBuilder.setUserStencil(ss);
1286 }
Brian Salomone14bd802017-04-04 15:13:25 -04001287 this->addLegacyMeshDrawOp(std::move(pipelineBuilder), clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -07001288}
1289
robertphillipsea461502015-05-26 11:38:03 -07001290// Can 'path' be drawn as a pair of filled nested rectangles?
bsalomon6663acf2016-05-10 09:14:17 -07001291static bool fills_as_nested_rects(const SkMatrix& viewMatrix, const SkPath& path, SkRect rects[2]) {
robertphillipsea461502015-05-26 11:38:03 -07001292
1293 if (path.isInverseFillType()) {
1294 return false;
1295 }
1296
1297 // TODO: this restriction could be lifted if we were willing to apply
1298 // the matrix to all the points individually rather than just to the rect
robertphillips0e7029e2015-11-30 05:45:06 -08001299 if (!viewMatrix.rectStaysRect()) {
robertphillipsea461502015-05-26 11:38:03 -07001300 return false;
1301 }
1302
1303 SkPath::Direction dirs[2];
1304 if (!path.isNestedFillRects(rects, dirs)) {
1305 return false;
1306 }
1307
1308 if (SkPath::kWinding_FillType == path.getFillType() && dirs[0] == dirs[1]) {
1309 // The two rects need to be wound opposite to each other
1310 return false;
1311 }
1312
1313 // Right now, nested rects where the margin is not the same width
1314 // all around do not render correctly
1315 const SkScalar* outer = rects[0].asScalars();
1316 const SkScalar* inner = rects[1].asScalars();
1317
1318 bool allEq = true;
1319
1320 SkScalar margin = SkScalarAbs(outer[0] - inner[0]);
1321 bool allGoE1 = margin >= SK_Scalar1;
1322
1323 for (int i = 1; i < 4; ++i) {
1324 SkScalar temp = SkScalarAbs(outer[i] - inner[i]);
1325 if (temp < SK_Scalar1) {
1326 allGoE1 = false;
1327 }
1328 if (!SkScalarNearlyEqual(margin, temp)) {
1329 allEq = false;
1330 }
1331 }
1332
1333 return allEq || allGoE1;
1334}
1335
Brian Osman11052242016-10-27 14:47:55 -04001336void GrRenderTargetContext::drawPath(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001337 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001338 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001339 const SkMatrix& viewMatrix,
1340 const SkPath& path,
1341 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001342 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001343 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001344 SkDEBUGCODE(this->validate();)
Brian Osman11052242016-10-27 14:47:55 -04001345 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawPath");
robertphillips2e1e51f2015-10-15 08:01:48 -07001346
robertphillipsea461502015-05-26 11:38:03 -07001347 if (path.isEmpty()) {
1348 if (path.isInverseFillType()) {
Brian Salomon82f44312017-01-11 13:42:54 -05001349 this->drawPaint(clip, std::move(paint), viewMatrix);
robertphillipsea461502015-05-26 11:38:03 -07001350 }
1351 return;
1352 }
1353
Robert Phillips72152832017-01-25 17:31:35 -05001354 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -07001355
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001356 GrAAType aaType = this->decideAAType(aa);
1357 if (GrAAType::kCoverage == aaType && !style.pathEffect()) {
bsalomon6663acf2016-05-10 09:14:17 -07001358 if (style.isSimpleFill() && !path.isConvex()) {
robertphillipsea461502015-05-26 11:38:03 -07001359 // Concave AA paths are expensive - try to avoid them for special cases
1360 SkRect rects[2];
1361
bsalomon6663acf2016-05-10 09:14:17 -07001362 if (fills_as_nested_rects(viewMatrix, path, rects)) {
Brian Salomond3ccb0a2017-04-03 10:38:00 -04001363 std::unique_ptr<GrLegacyMeshDrawOp> op =
Brian Salomon6a639042016-12-14 11:08:17 -05001364 GrRectOpFactory::MakeAAFillNestedRects(paint.getColor(), viewMatrix, rects);
Brian Salomon21aa35f2016-12-09 16:01:53 -05001365 if (op) {
Brian Salomon82f44312017-01-11 13:42:54 -05001366 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
Brian Salomone14bd802017-04-04 15:13:25 -04001367 this->addLegacyMeshDrawOp(std::move(pipelineBuilder), clip, std::move(op));
bsalomon40ef4852016-05-02 13:22:13 -07001368 }
robertphillipsea461502015-05-26 11:38:03 -07001369 return;
1370 }
1371 }
1372 SkRect ovalRect;
1373 bool isOval = path.isOval(&ovalRect);
1374
1375 if (isOval && !path.isInverseFillType()) {
Brian Salomon94efbf52016-11-29 13:43:05 -05001376 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomond3ccb0a2017-04-03 10:38:00 -04001377 std::unique_ptr<GrLegacyMeshDrawOp> op = GrOvalOpFactory::MakeOvalOp(
Brian Salomon289e3d82016-12-14 15:52:56 -05001378 paint.getColor(), viewMatrix, ovalRect, style.strokeRec(), shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -05001379 if (op) {
Brian Salomon82f44312017-01-11 13:42:54 -05001380 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
Brian Salomone14bd802017-04-04 15:13:25 -04001381 this->addLegacyMeshDrawOp(std::move(pipelineBuilder), clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -07001382 return;
1383 }
1384 }
1385 }
robertphillips4bc31812016-03-01 12:22:49 -08001386
1387 // Note that internalDrawPath may sw-rasterize the path into a scratch texture.
1388 // Scratch textures can be recycled after they are returned to the texture
1389 // cache. This presents a potential hazard for buffered drawing. However,
1390 // the writePixels that uploads to the scratch will perform a flush so we're
1391 // OK.
Brian Salomon82f44312017-01-11 13:42:54 -05001392 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, style);
robertphillipsea461502015-05-26 11:38:03 -07001393}
1394
Brian Osman11052242016-10-27 14:47:55 -04001395bool GrRenderTargetContextPriv::drawAndStencilPath(const GrClip& clip,
1396 const GrUserStencilSettings* ss,
1397 SkRegion::Op op,
1398 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001399 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001400 const SkMatrix& viewMatrix,
1401 const SkPath& path) {
robertphillips391395d2016-03-02 09:26:36 -08001402 ASSERT_SINGLE_OWNER_PRIV
1403 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -04001404 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001405 GR_AUDIT_TRAIL_AUTO_FRAME(fRenderTargetContext->fAuditTrail,
1406 "GrRenderTargetContextPriv::drawAndStencilPath");
robertphillips391395d2016-03-02 09:26:36 -08001407
1408 if (path.isEmpty() && path.isInverseFillType()) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001409 this->drawAndStencilRect(clip, ss, op, invert, GrAA::kNo, SkMatrix::I(),
Brian Osman11052242016-10-27 14:47:55 -04001410 SkRect::MakeIWH(fRenderTargetContext->width(),
1411 fRenderTargetContext->height()));
robertphillips391395d2016-03-02 09:26:36 -08001412 return true;
1413 }
1414
Robert Phillips72152832017-01-25 17:31:35 -05001415 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -08001416
1417 // An Assumption here is that path renderer would use some form of tweaking
1418 // the src color (either the input alpha or in the frag shader) to implement
1419 // aa. If we have some future driver-mojo path AA that can do the right
1420 // thing WRT to the blend then we'll need some query on the PR.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001421 GrAAType aaType = fRenderTargetContext->decideAAType(aa);
robertphillips976f5f02016-06-03 10:59:20 -07001422 bool hasUserStencilSettings = !ss->isUnused();
robertphillips391395d2016-03-02 09:26:36 -08001423
bsalomon8acedde2016-06-24 10:42:16 -07001424 GrShape shape(path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -08001425 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Osman11052242016-10-27 14:47:55 -04001426 canDrawArgs.fShaderCaps =
Robert Phillips72152832017-01-25 17:31:35 -05001427 fRenderTargetContext->drawingManager()->getContext()->caps()->shaderCaps();
robertphillips391395d2016-03-02 09:26:36 -08001428 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07001429 canDrawArgs.fShape = &shape;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001430 canDrawArgs.fAAType = aaType;
cdalton93a379b2016-05-11 13:58:08 -07001431 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips391395d2016-03-02 09:26:36 -08001432
1433 // Don't allow the SW renderer
Robert Phillips72152832017-01-25 17:31:35 -05001434 GrPathRenderer* pr = fRenderTargetContext->drawingManager()->getPathRenderer(
Brian Salomon36aa1762016-12-10 13:24:02 -05001435 canDrawArgs, false, GrPathRendererChain::DrawType::kStencilAndColor);
robertphillips391395d2016-03-02 09:26:36 -08001436 if (!pr) {
1437 return false;
1438 }
1439
1440 GrPaint paint;
1441 paint.setCoverageSetOpXPFactory(op, invert);
1442
Brian Salomon82f44312017-01-11 13:42:54 -05001443 GrPathRenderer::DrawPathArgs args{
Robert Phillips256c37b2017-03-01 14:32:46 -05001444 fRenderTargetContext->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05001445 std::move(paint),
1446 ss,
1447 fRenderTargetContext,
1448 &clip,
1449 &viewMatrix,
1450 &shape,
1451 aaType,
1452 fRenderTargetContext->isGammaCorrect()};
robertphillips391395d2016-03-02 09:26:36 -08001453 pr->drawPath(args);
1454 return true;
1455}
1456
Brian Osman11052242016-10-27 14:47:55 -04001457SkBudgeted GrRenderTargetContextPriv::isBudgeted() const {
robertphillips714712b2016-08-04 06:20:45 -07001458 ASSERT_SINGLE_OWNER_PRIV
1459
Brian Osman11052242016-10-27 14:47:55 -04001460 if (fRenderTargetContext->wasAbandoned()) {
robertphillips714712b2016-08-04 06:20:45 -07001461 return SkBudgeted::kNo;
1462 }
1463
Brian Osman11052242016-10-27 14:47:55 -04001464 SkDEBUGCODE(fRenderTargetContext->validate();)
robertphillips714712b2016-08-04 06:20:45 -07001465
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001466 return fRenderTargetContext->fRenderTargetProxy->isBudgeted();
robertphillips714712b2016-08-04 06:20:45 -07001467}
1468
Brian Osman11052242016-10-27 14:47:55 -04001469void GrRenderTargetContext::internalDrawPath(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001470 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001471 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001472 const SkMatrix& viewMatrix,
1473 const SkPath& path,
1474 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001475 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001476 RETURN_IF_ABANDONED
bsalomon8acedde2016-06-24 10:42:16 -07001477 SkASSERT(!path.isEmpty());
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001478 GrShape shape;
robertphillipsea461502015-05-26 11:38:03 -07001479
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001480 GrAAType aaType = this->decideAAType(aa, /*allowMixedSamples*/ true);
1481 if (style.isSimpleHairline() && aaType == GrAAType::kMixedSamples) {
1482 // NVPR cannot handle hairlines, so this will would get picked up by a different stencil and
1483 // cover path renderer (i.e. default path renderer). The hairline renderer produces much
1484 // smoother hairlines than MSAA.
1485 aaType = GrAAType::kCoverage;
bsalomon0a0f67e2016-06-28 11:56:42 -07001486 }
robertphillips68737822015-10-29 12:12:21 -07001487 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Robert Phillips72152832017-01-25 17:31:35 -05001488 canDrawArgs.fShaderCaps = this->drawingManager()->getContext()->caps()->shaderCaps();
robertphillips68737822015-10-29 12:12:21 -07001489 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07001490 canDrawArgs.fShape = &shape;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001491 canDrawArgs.fHasUserStencilSettings = false;
robertphillips68737822015-10-29 12:12:21 -07001492
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001493 GrPathRenderer* pr;
Brian Salomon82125e92016-12-10 09:35:48 -05001494 static constexpr GrPathRendererChain::DrawType kType = GrPathRendererChain::DrawType::kColor;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001495 do {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001496 shape = GrShape(path, style);
bsalomon8acedde2016-06-24 10:42:16 -07001497 if (shape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001498 return;
1499 }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001500
1501 canDrawArgs.fAAType = aaType;
1502
1503 // Try a 1st time without applying any of the style to the geometry (and barring sw)
Robert Phillips72152832017-01-25 17:31:35 -05001504 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001505 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
1506
1507 if (!pr && shape.style().pathEffect()) {
1508 // It didn't work above, so try again with the path effect applied.
1509 shape = shape.applyStyle(GrStyle::Apply::kPathEffectOnly, styleScale);
bsalomon8acedde2016-06-24 10:42:16 -07001510 if (shape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001511 return;
1512 }
Robert Phillips72152832017-01-25 17:31:35 -05001513 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
bsalomon6663acf2016-05-10 09:14:17 -07001514 }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001515 if (!pr) {
1516 if (shape.style().applies()) {
1517 shape = shape.applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec, styleScale);
1518 if (shape.isEmpty()) {
1519 return;
1520 }
1521 }
1522 // This time, allow SW renderer
Robert Phillips72152832017-01-25 17:31:35 -05001523 pr = this->drawingManager()->getPathRenderer(canDrawArgs, true, kType);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001524 }
Brian Salomon0abc8b42016-12-13 10:22:54 -05001525 if (!pr && GrAATypeIsHW(aaType)) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001526 // There are exceptional cases where we may wind up falling back to coverage based AA
1527 // when the target is MSAA (e.g. through disabling path renderers via GrContextOptions).
1528 aaType = GrAAType::kCoverage;
1529 } else {
1530 break;
1531 }
1532 } while(true);
robertphillipsea461502015-05-26 11:38:03 -07001533
bsalomon8acedde2016-06-24 10:42:16 -07001534 if (!pr) {
robertphillipsea461502015-05-26 11:38:03 -07001535#ifdef SK_DEBUG
1536 SkDebugf("Unable to find path renderer compatible with path.\n");
1537#endif
1538 return;
1539 }
1540
Robert Phillips256c37b2017-03-01 14:32:46 -05001541 GrPathRenderer::DrawPathArgs args{this->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05001542 std::move(paint),
1543 &GrUserStencilSettings::kUnused,
1544 this,
1545 &clip,
1546 &viewMatrix,
1547 &shape,
1548 aaType,
1549 this->isGammaCorrect()};
bsalomon0aff2fa2015-07-31 06:48:27 -07001550 pr->drawPath(args);
robertphillipsea461502015-05-26 11:38:03 -07001551}
1552
Brian Salomon467921e2017-03-06 16:17:12 -05001553static void op_bounds(SkRect* bounds, const GrOp* op) {
1554 *bounds = op->bounds();
1555 if (op->hasZeroArea()) {
1556 if (op->hasAABloat()) {
1557 bounds->outset(0.5f, 0.5f);
1558 } else {
1559 // We don't know which way the particular GPU will snap lines or points at integer
1560 // coords. So we ensure that the bounds is large enough for either snap.
1561 SkRect before = *bounds;
1562 bounds->roundOut(bounds);
1563 if (bounds->fLeft == before.fLeft) {
1564 bounds->fLeft -= 1;
1565 }
1566 if (bounds->fTop == before.fTop) {
1567 bounds->fTop -= 1;
1568 }
1569 if (bounds->fRight == before.fRight) {
1570 bounds->fRight += 1;
1571 }
1572 if (bounds->fBottom == before.fBottom) {
1573 bounds->fBottom += 1;
1574 }
1575 }
1576 }
1577}
1578
Brian Salomon54d212e2017-03-21 14:22:38 -04001579uint32_t GrRenderTargetContext::addDrawOp(const GrClip& clip, std::unique_ptr<GrDrawOp> op) {
joshualitt1de610a2016-01-06 08:26:09 -08001580 ASSERT_SINGLE_OWNER
Robert Phillipsc0138922017-03-08 11:50:55 -05001581 if (this->drawingManager()->wasAbandoned()) {
1582 return SK_InvalidUniqueID;
1583 }
robertphillips2e1e51f2015-10-15 08:01:48 -07001584 SkDEBUGCODE(this->validate();)
Brian Salomon42521e82016-12-07 16:44:58 -05001585 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::addDrawOp");
robertphillips2d70dcb2015-10-06 07:38:23 -07001586
Brian Salomon467921e2017-03-06 16:17:12 -05001587 // Setup clip
1588 SkRect bounds;
1589 op_bounds(&bounds, op.get());
Brian Salomon97180af2017-03-14 13:42:58 -04001590 GrAppliedClip appliedClip;
Brian Salomon54d212e2017-03-21 14:22:38 -04001591 GrDrawOp::FixedFunctionFlags fixedFunctionFlags = op->fixedFunctionFlags();
1592 if (!clip.apply(fContext, this, fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesHWAA,
1593 fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil, &appliedClip,
1594 &bounds)) {
1595 return SK_InvalidUniqueID;
1596 }
1597
1598 // This forces instantiation of the render target.
1599 GrRenderTarget* rt = this->accessRenderTarget();
1600 if (!rt) {
1601 return SK_InvalidUniqueID;
1602 }
1603
1604 if (fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil ||
1605 appliedClip.hasStencilClip()) {
1606 if (!fContext->resourceProvider()->attachStencilAttachment(rt)) {
1607 SkDebugf("ERROR creating stencil attachment. Draw skipped.\n");
1608 return SK_InvalidUniqueID;
1609 }
1610 }
1611
1612 GrXferProcessor::DstTexture dstTexture;
1613 if (op->xpRequiresDstTexture(*this->caps(), &appliedClip)) {
Robert Phillipsbf25d432017-04-07 10:08:53 -04001614 if (!this->setupDstTexture(fRenderTargetProxy.get(), clip, op->bounds(), &dstTexture)) {
Brian Salomon54d212e2017-03-21 14:22:38 -04001615 return SK_InvalidUniqueID;
1616 }
1617 }
1618
1619 op->setClippedBounds(bounds);
1620 return this->getOpList()->addOp(std::move(op), this, std::move(appliedClip), dstTexture);
1621}
1622
Brian Salomone14bd802017-04-04 15:13:25 -04001623uint32_t GrRenderTargetContext::addLegacyMeshDrawOp(GrPipelineBuilder&& pipelineBuilder,
Brian Salomond3ccb0a2017-04-03 10:38:00 -04001624 const GrClip& clip,
1625 std::unique_ptr<GrLegacyMeshDrawOp> op) {
Brian Salomon54d212e2017-03-21 14:22:38 -04001626 ASSERT_SINGLE_OWNER
1627 if (this->drawingManager()->wasAbandoned()) {
1628 return SK_InvalidUniqueID;
1629 }
1630 SkDEBUGCODE(this->validate();)
Brian Salomond3ccb0a2017-04-03 10:38:00 -04001631 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::addLegacyMeshDrawOp");
Brian Salomon54d212e2017-03-21 14:22:38 -04001632
1633 // Setup clip
1634 SkRect bounds;
1635 op_bounds(&bounds, op.get());
1636 GrAppliedClip appliedClip;
Brian Salomon467921e2017-03-06 16:17:12 -05001637 if (!clip.apply(fContext, this, pipelineBuilder.isHWAntialias(),
Brian Salomon97180af2017-03-14 13:42:58 -04001638 pipelineBuilder.hasUserStencilSettings(), &appliedClip, &bounds)) {
Robert Phillipsc0138922017-03-08 11:50:55 -05001639 return SK_InvalidUniqueID;
Brian Salomon467921e2017-03-06 16:17:12 -05001640 }
1641
1642 // This forces instantiation of the render target. Pipeline creation is moving to flush time
1643 // by which point instantiation must have occurred anyway.
1644 GrRenderTarget* rt = this->accessRenderTarget();
1645 if (!rt) {
Robert Phillipsc0138922017-03-08 11:50:55 -05001646 return SK_InvalidUniqueID;
Brian Salomon467921e2017-03-06 16:17:12 -05001647 }
1648
1649 GrResourceProvider* resourceProvider = fContext->resourceProvider();
Brian Salomon48d1b4c2017-04-08 07:38:53 -04001650 bool usesStencil = pipelineBuilder.hasUserStencilSettings() || appliedClip.hasStencilClip();
1651 if (usesStencil) {
Brian Salomon467921e2017-03-06 16:17:12 -05001652 if (!resourceProvider->attachStencilAttachment(this->accessRenderTarget())) {
1653 SkDebugf("ERROR creating stencil attachment. Draw skipped.\n");
Robert Phillipsc0138922017-03-08 11:50:55 -05001654 return SK_InvalidUniqueID;
Brian Salomon467921e2017-03-06 16:17:12 -05001655 }
1656 }
1657
Brian Salomon48d1b4c2017-04-08 07:38:53 -04001658 bool isMixedSamples = fRenderTargetProxy->isMixedSampled() &&
1659 (pipelineBuilder.isHWAntialias() || usesStencil);
1660
1661 GrColor overrideColor;
1662 GrProcessorSet::Analysis analysis = op->analyzeUpdateAndRecordProcessors(
1663 &pipelineBuilder, &appliedClip, isMixedSamples, *this->caps(), &overrideColor);
Brian Salomon467921e2017-03-06 16:17:12 -05001664
1665 GrPipeline::InitArgs args;
1666 pipelineBuilder.getPipelineInitArgs(&args);
1667 args.fAppliedClip = &appliedClip;
1668 args.fRenderTarget = rt;
1669 args.fCaps = this->caps();
Brian Salomon467921e2017-03-06 16:17:12 -05001670
Brian Salomon31853842017-03-28 16:32:05 -04001671 if (analysis.requiresDstTexture()) {
Robert Phillipsbf25d432017-04-07 10:08:53 -04001672 if (!this->setupDstTexture(fRenderTargetProxy.get(), clip, bounds, &args.fDstTexture)) {
Robert Phillipsc0138922017-03-08 11:50:55 -05001673 return SK_InvalidUniqueID;
Brian Salomon467921e2017-03-06 16:17:12 -05001674 }
1675 }
Brian Salomon48d1b4c2017-04-08 07:38:53 -04001676 op->initPipeline(args, analysis, overrideColor);
Brian Salomon467921e2017-03-06 16:17:12 -05001677 // TODO: We need to add pipeline dependencies on textures, etc before recording this op.
Brian Salomon97180af2017-03-14 13:42:58 -04001678 op->setClippedBounds(bounds);
Robert Phillipsc0138922017-03-08 11:50:55 -05001679 return this->getOpList()->addOp(std::move(op), this);
Brian Salomon467921e2017-03-06 16:17:12 -05001680}
1681
Robert Phillipsbf25d432017-04-07 10:08:53 -04001682bool GrRenderTargetContext::setupDstTexture(GrRenderTargetProxy* rtProxy, const GrClip& clip,
Brian Salomon467921e2017-03-06 16:17:12 -05001683 const SkRect& opBounds,
1684 GrXferProcessor::DstTexture* dstTexture) {
1685 if (this->caps()->textureBarrierSupport()) {
Robert Phillipsbf25d432017-04-07 10:08:53 -04001686 if (GrTextureProxy* texProxy = rtProxy->asTextureProxy()) {
1687 // MDB TODO: remove this instantiation. Blocked on making DstTexture be proxy-based
1688 sk_sp<GrTexture> tex(sk_ref_sp(texProxy->instantiate(fContext->resourceProvider())));
1689 if (!tex) {
1690 SkDebugf("setupDstTexture: instantiation of src texture failed.\n");
1691 return false; // We have bigger problems now
1692 }
1693
Brian Salomon467921e2017-03-06 16:17:12 -05001694 // The render target is a texture, so we can read from it directly in the shader. The XP
1695 // will be responsible to detect this situation and request a texture barrier.
Robert Phillipsbf25d432017-04-07 10:08:53 -04001696 dstTexture->setTexture(std::move(tex));
Brian Salomon467921e2017-03-06 16:17:12 -05001697 dstTexture->setOffset(0, 0);
Robert Phillipsbf25d432017-04-07 10:08:53 -04001698 return true;
Brian Salomon467921e2017-03-06 16:17:12 -05001699 }
1700 }
1701
Robert Phillipsbf25d432017-04-07 10:08:53 -04001702 SkIRect copyRect = SkIRect::MakeWH(rtProxy->width(), rtProxy->height());
Brian Salomon467921e2017-03-06 16:17:12 -05001703
Eric Karl74480882017-04-03 14:49:05 -07001704 SkIRect clippedRect;
Robert Phillipsbf25d432017-04-07 10:08:53 -04001705 clip.getConservativeBounds(rtProxy->width(), rtProxy->height(), &clippedRect);
Eric Karl72e551e2017-04-04 13:42:10 -07001706 SkIRect drawIBounds;
Brian Salomon467921e2017-03-06 16:17:12 -05001707 opBounds.roundOut(&drawIBounds);
Brian Salomon859621f2017-03-16 09:21:54 -04001708 // Cover up for any precision issues by outsetting the op bounds a pixel in each direction.
1709 drawIBounds.outset(1, 1);
Eric Karl72e551e2017-04-04 13:42:10 -07001710 if (!clippedRect.intersect(drawIBounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -05001711#ifdef SK_DEBUG
Robert Phillipsbf25d432017-04-07 10:08:53 -04001712 GrCapsDebugf(this->caps(), "setupDstTexture: Missed an early reject bailing on draw.");
Brian Salomon467921e2017-03-06 16:17:12 -05001713#endif
Robert Phillipsbf25d432017-04-07 10:08:53 -04001714 return false;
Brian Salomon467921e2017-03-06 16:17:12 -05001715 }
1716
1717 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
1718 // have per-sample dst values by making the copy multisampled.
1719 GrSurfaceDesc desc;
Eric Karl74480882017-04-03 14:49:05 -07001720 bool rectsMustMatch = false;
1721 bool disallowSubrect = false;
Robert Phillipsbf25d432017-04-07 10:08:53 -04001722 if (!this->caps()->initDescForDstCopy(rtProxy, &desc, &rectsMustMatch, &disallowSubrect)) {
1723 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
Brian Salomon467921e2017-03-06 16:17:12 -05001724 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillipsbf25d432017-04-07 10:08:53 -04001725 desc.fConfig = rtProxy->config();
Brian Salomon467921e2017-03-06 16:17:12 -05001726 }
1727
Eric Karl74480882017-04-03 14:49:05 -07001728 if (!disallowSubrect) {
1729 copyRect = clippedRect;
1730 }
Brian Salomon467921e2017-03-06 16:17:12 -05001731
Robert Phillipsbf25d432017-04-07 10:08:53 -04001732 SkIPoint dstPoint, dstOffset;
1733 SkBackingFit fit;
Eric Karl74480882017-04-03 14:49:05 -07001734 if (rectsMustMatch) {
Robert Phillipsbf25d432017-04-07 10:08:53 -04001735 SkASSERT(desc.fOrigin == rtProxy->origin());
1736 desc.fWidth = rtProxy->width();
1737 desc.fHeight = rtProxy->height();
Eric Karl74480882017-04-03 14:49:05 -07001738 dstPoint = {copyRect.fLeft, copyRect.fTop};
1739 dstOffset = {0, 0};
Robert Phillipsbf25d432017-04-07 10:08:53 -04001740 fit = SkBackingFit::kExact;
Eric Karl74480882017-04-03 14:49:05 -07001741 } else {
1742 desc.fWidth = copyRect.width();
1743 desc.fHeight = copyRect.height();
1744 dstPoint = {0, 0};
1745 dstOffset = {copyRect.fLeft, copyRect.fTop};
Robert Phillipsbf25d432017-04-07 10:08:53 -04001746 fit = SkBackingFit::kApprox;
Eric Karl74480882017-04-03 14:49:05 -07001747 }
Brian Salomon467921e2017-03-06 16:17:12 -05001748
Robert Phillipsbf25d432017-04-07 10:08:53 -04001749 sk_sp<GrSurfaceContext> sContext = fContext->contextPriv().makeDeferredSurfaceContext(
1750 desc,
1751 fit,
1752 SkBudgeted::kYes);
1753 if (!sContext) {
1754 SkDebugf("setupDstTexture: surfaceContext creation failed.\n");
1755 return false;
1756 }
1757
1758 if (!sContext->copy(rtProxy, copyRect, dstPoint)) {
1759 SkDebugf("setupDstTexture: copy failed.\n");
1760 return false;
1761 }
1762
1763 GrTextureProxy* copyProxy = sContext->asTextureProxy();
1764 // MDB TODO: remove this instantiation once DstTexture is proxy-backed
1765 sk_sp<GrTexture> copy(sk_ref_sp(copyProxy->instantiate(fContext->resourceProvider())));
Brian Salomon467921e2017-03-06 16:17:12 -05001766 if (!copy) {
Robert Phillipsbf25d432017-04-07 10:08:53 -04001767 SkDebugf("setupDstTexture: instantiation of copied texture failed.\n");
1768 return false;
Brian Salomon467921e2017-03-06 16:17:12 -05001769 }
Eric Karl74480882017-04-03 14:49:05 -07001770
Brian Salomon467921e2017-03-06 16:17:12 -05001771 dstTexture->setTexture(std::move(copy));
Eric Karl74480882017-04-03 14:49:05 -07001772 dstTexture->setOffset(dstOffset);
Robert Phillipsbf25d432017-04-07 10:08:53 -04001773 return true;
robertphillips2334fb62015-06-17 05:43:33 -07001774}