blob: cd2d099738d5579ad63fc41e0834253e89807695 [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 Salomon6d4b65e2017-05-03 17:06:09 -04009#include "../private/GrAuditTrail.h"
Jim Van Verth3af1af92017-05-18 15:06:54 -040010#include "../private/SkShadowFlags.h"
Brian Salomon467921e2017-03-06 16:17:12 -050011#include "GrAppliedClip.h"
Greg Daniela5cb7812017-06-16 09:45:32 -040012#include "GrBackendSemaphore.h"
Brian Salomon5ec9def2016-12-20 15:34:05 -050013#include "GrColor.h"
Robert Phillipse78b7252017-04-06 07:59:41 -040014#include "GrContextPriv.h"
robertphillips77a2e522015-10-17 07:43:27 -070015#include "GrDrawingManager.h"
csmartdalton02fa32c2016-08-19 13:29:27 -070016#include "GrFixedClip.h"
robertphillips714712b2016-08-04 06:20:45 -070017#include "GrGpuResourcePriv.h"
Robert Phillips9d6c64f2017-09-14 10:56:45 -040018#include "GrOpList.h"
robertphillipsea461502015-05-26 11:38:03 -070019#include "GrPathRenderer.h"
robertphillips2334fb62015-06-17 05:43:33 -070020#include "GrRenderTarget.h"
Brian Salomon5ec9def2016-12-20 15:34:05 -050021#include "GrRenderTargetContextPriv.h"
bsalomon473addf2015-10-02 07:49:05 -070022#include "GrResourceProvider.h"
Brian Salomon467921e2017-03-06 16:17:12 -050023#include "GrStencilAttachment.h"
Brian Salomondcbb9d92017-07-19 10:53:20 -040024#include "GrTracing.h"
Jim Van Verth1af03d42017-07-31 09:34:58 -040025#include "SkDrawShadowInfo.h"
Brian Salomon467921e2017-03-06 16:17:12 -050026#include "SkLatticeIter.h"
27#include "SkMatrixPriv.h"
Jim Van Verth34d6e4b2017-06-09 11:09:03 -040028#include "SkShadowUtils.h"
robertphillips2d70dcb2015-10-06 07:38:23 -070029#include "SkSurfacePriv.h"
Brian Salomon467921e2017-03-06 16:17:12 -050030#include "effects/GrRRectEffect.h"
31#include "instanced/InstancedRendering.h"
Brian Salomon89527432016-12-16 09:52:16 -050032#include "ops/GrClearOp.h"
Robert Phillipsb9a02a12017-04-06 11:08:40 -040033#include "ops/GrClearStencilClipOp.h"
Robert Phillips65a88fa2017-08-08 08:36:22 -040034#include "ops/GrDebugMarkerOp.h"
Brian Salomon0f353322017-05-03 20:58:59 +000035#include "ops/GrDrawAtlasOp.h"
Brian Salomon6d4b65e2017-05-03 17:06:09 -040036#include "ops/GrDrawOp.h"
Brian Salomon89527432016-12-16 09:52:16 -050037#include "ops/GrDrawVerticesOp.h"
38#include "ops/GrLatticeOp.h"
39#include "ops/GrOp.h"
40#include "ops/GrOvalOpFactory.h"
41#include "ops/GrRectOpFactory.h"
42#include "ops/GrRegionOp.h"
Greg Daniela5cb7812017-06-16 09:45:32 -040043#include "ops/GrSemaphoreOp.h"
Brian Salomon89527432016-12-16 09:52:16 -050044#include "ops/GrShadowRRectOp.h"
Brian Salomon467921e2017-03-06 16:17:12 -050045#include "ops/GrStencilPathOp.h"
Brian Salomon34169692017-08-28 15:32:01 -040046#include "ops/GrTextureOp.h"
joshualitte8042922015-12-11 06:11:21 -080047#include "text/GrAtlasTextContext.h"
48#include "text/GrStencilAndCoverTextContext.h"
joshualittbc907352016-01-13 06:45:40 -080049
Robert Phillips72152832017-01-25 17:31:35 -050050#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this->drawingManager()->getContext())
joshualitt1de610a2016-01-06 08:26:09 -080051#define ASSERT_SINGLE_OWNER \
Robert Phillipsa90aa2b2017-04-10 08:19:26 -040052 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(this->singleOwner());)
robertphillips391395d2016-03-02 09:26:36 -080053#define ASSERT_SINGLE_OWNER_PRIV \
Robert Phillipsa90aa2b2017-04-10 08:19:26 -040054 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fRenderTargetContext->singleOwner());)
Robert Phillips72152832017-01-25 17:31:35 -050055#define RETURN_IF_ABANDONED if (this->drawingManager()->wasAbandoned()) { return; }
56#define RETURN_IF_ABANDONED_PRIV if (fRenderTargetContext->drawingManager()->wasAbandoned()) { return; }
57#define RETURN_FALSE_IF_ABANDONED if (this->drawingManager()->wasAbandoned()) { return false; }
58#define RETURN_FALSE_IF_ABANDONED_PRIV if (fRenderTargetContext->drawingManager()->wasAbandoned()) { return false; }
59#define RETURN_NULL_IF_ABANDONED if (this->drawingManager()->wasAbandoned()) { return nullptr; }
robertphillipsea461502015-05-26 11:38:03 -070060
Brian Salomone225b562017-06-14 13:00:03 -040061//////////////////////////////////////////////////////////////////////////////
62
63GrAAType GrChooseAAType(GrAA aa, GrFSAAType fsaaType, GrAllowMixedSamples allowMixedSamples,
64 const GrCaps& caps) {
65 if (GrAA::kNo == aa) {
66 // On some devices we cannot disable MSAA if it is enabled so we make the AA type reflect
67 // that.
68 if (fsaaType == GrFSAAType::kUnifiedMSAA && !caps.multisampleDisableSupport()) {
69 return GrAAType::kMSAA;
70 }
71 return GrAAType::kNone;
72 }
73 switch (fsaaType) {
74 case GrFSAAType::kNone:
75 return GrAAType::kCoverage;
76 case GrFSAAType::kUnifiedMSAA:
77 return GrAAType::kMSAA;
78 case GrFSAAType::kMixedSamples:
79 return GrAllowMixedSamples::kYes == allowMixedSamples ? GrAAType::kMixedSamples
80 : GrAAType::kCoverage;
81 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -040082 SK_ABORT("Unexpected fsaa type");
Brian Salomone225b562017-06-14 13:00:03 -040083 return GrAAType::kNone;
84}
85
86//////////////////////////////////////////////////////////////////////////////
87
robertphillipsea461502015-05-26 11:38:03 -070088class AutoCheckFlush {
89public:
halcanary9d524f22016-03-29 09:03:52 -070090 AutoCheckFlush(GrDrawingManager* drawingManager) : fDrawingManager(drawingManager) {
robertphillips77a2e522015-10-17 07:43:27 -070091 SkASSERT(fDrawingManager);
92 }
bsalomonb77a9072016-09-07 10:02:04 -070093 ~AutoCheckFlush() { fDrawingManager->flushIfNecessary(); }
robertphillipsea461502015-05-26 11:38:03 -070094
95private:
robertphillips77a2e522015-10-17 07:43:27 -070096 GrDrawingManager* fDrawingManager;
robertphillipsea461502015-05-26 11:38:03 -070097};
98
Brian Osman11052242016-10-27 14:47:55 -040099bool GrRenderTargetContext::wasAbandoned() const {
Robert Phillips72152832017-01-25 17:31:35 -0500100 return this->drawingManager()->wasAbandoned();
robertphillips7761d612016-05-16 09:14:53 -0700101}
102
Robert Phillipsf2361d22016-10-25 14:20:06 -0400103// In MDB mode the reffing of the 'getLastOpList' call's result allows in-progress
Brian Osman11052242016-10-27 14:47:55 -0400104// GrOpLists to be picked up and added to by renderTargetContexts lower in the call
Robert Phillipsf2361d22016-10-25 14:20:06 -0400105// stack. When this occurs with a closed GrOpList, a new one will be allocated
Brian Osman11052242016-10-27 14:47:55 -0400106// when the renderTargetContext attempts to use it (via getOpList).
107GrRenderTargetContext::GrRenderTargetContext(GrContext* context,
108 GrDrawingManager* drawingMgr,
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400109 sk_sp<GrRenderTargetProxy> rtp,
Brian Osman11052242016-10-27 14:47:55 -0400110 sk_sp<SkColorSpace> colorSpace,
111 const SkSurfaceProps* surfaceProps,
112 GrAuditTrail* auditTrail,
Robert Phillips941d1442017-06-14 16:37:02 -0400113 GrSingleOwner* singleOwner,
114 bool managedOpList)
Robert Phillips72152832017-01-25 17:31:35 -0500115 : GrSurfaceContext(context, drawingMgr, std::move(colorSpace), auditTrail, singleOwner)
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400116 , fRenderTargetProxy(std::move(rtp))
Robert Phillipsdc83b892017-04-13 12:23:54 -0400117 , fOpList(sk_ref_sp(fRenderTargetProxy->getLastRenderTargetOpList()))
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400118 , fInstancedPipelineInfo(fRenderTargetProxy.get())
brianosman5a7ae7e2016-09-12 12:07:25 -0700119 , fColorXformFromSRGB(nullptr)
Robert Phillips941d1442017-06-14 16:37:02 -0400120 , fSurfaceProps(SkSurfacePropsCopyOrDefault(surfaceProps))
121 , fManagedOpList(managedOpList) {
brianosman5a7ae7e2016-09-12 12:07:25 -0700122 if (fColorSpace) {
123 // sRGB sources are very common (SkColor, etc...), so we cache that gamut transformation
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500124 auto srgbColorSpace = SkColorSpace::MakeSRGB();
msarettc71a9b72016-09-16 11:01:27 -0700125 fColorXformFromSRGB = GrColorSpaceXform::Make(srgbColorSpace.get(), fColorSpace.get());
brianosman5a7ae7e2016-09-12 12:07:25 -0700126 }
Robert Phillipsd9914862017-06-14 15:16:59 -0400127
Robert Phillips09dfc472017-09-13 15:25:47 -0400128#ifndef MDB_ALLOC_RESOURCES
Robert Phillipsd9914862017-06-14 15:16:59 -0400129 // MDB TODO: to ensure all resources still get allocated in the correct order in the hybrid
130 // world we need to get the correct opList here so that it, in turn, can grab and hold
131 // its rendertarget.
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400132 this->getRTOpList();
Robert Phillips09dfc472017-09-13 15:25:47 -0400133#endif
robertphillips2e1e51f2015-10-15 08:01:48 -0700134 SkDEBUGCODE(this->validate();)
robertphillipsea461502015-05-26 11:38:03 -0700135}
136
robertphillips2e1e51f2015-10-15 08:01:48 -0700137#ifdef SK_DEBUG
Brian Osman11052242016-10-27 14:47:55 -0400138void GrRenderTargetContext::validate() const {
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400139 SkASSERT(fRenderTargetProxy);
140 fRenderTargetProxy->validate(fContext);
robertphillipsa106c622015-10-16 09:07:06 -0700141
Robert Phillipsf2361d22016-10-25 14:20:06 -0400142 if (fOpList && !fOpList->isClosed()) {
Robert Phillipsdc83b892017-04-13 12:23:54 -0400143 SkASSERT(fRenderTargetProxy->getLastOpList() == fOpList.get());
robertphillipsa106c622015-10-16 09:07:06 -0700144 }
robertphillips2e1e51f2015-10-15 08:01:48 -0700145}
146#endif
147
Brian Osman11052242016-10-27 14:47:55 -0400148GrRenderTargetContext::~GrRenderTargetContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800149 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700150}
151
Robert Phillipsf200a902017-01-30 13:27:37 -0500152GrTextureProxy* GrRenderTargetContext::asTextureProxy() {
Robert Phillipseaa86252016-11-08 13:49:39 +0000153 return fRenderTargetProxy->asTextureProxy();
154}
155
Robert Phillipsf200a902017-01-30 13:27:37 -0500156sk_sp<GrTextureProxy> GrRenderTargetContext::asTextureProxyRef() {
157 return sk_ref_sp(fRenderTargetProxy->asTextureProxy());
158}
159
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400160GrRenderTargetOpList* GrRenderTargetContext::getRTOpList() {
joshualitt1de610a2016-01-06 08:26:09 -0800161 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700162 SkDEBUGCODE(this->validate();)
163
Robert Phillipsf2361d22016-10-25 14:20:06 -0400164 if (!fOpList || fOpList->isClosed()) {
Robert Phillips941d1442017-06-14 16:37:02 -0400165 fOpList = this->drawingManager()->newRTOpList(fRenderTargetProxy.get(), fManagedOpList);
robertphillipsa106c622015-10-16 09:07:06 -0700166 }
167
Robert Phillipsdc83b892017-04-13 12:23:54 -0400168 return fOpList.get();
robertphillipsa106c622015-10-16 09:07:06 -0700169}
170
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400171GrOpList* GrRenderTargetContext::getOpList() {
172 return this->getRTOpList();
robertphillipsea461502015-05-26 11:38:03 -0700173}
174
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500175void GrRenderTargetContext::drawText(const GrClip& clip, const SkPaint& skPaint,
Brian Salomon82f44312017-01-11 13:42:54 -0500176 const SkMatrix& viewMatrix, const char text[],
177 size_t byteLength, SkScalar x, SkScalar y,
178 const SkIRect& clipBounds) {
joshualitt1de610a2016-01-06 08:26:09 -0800179 ASSERT_SINGLE_OWNER
robertphillips2d70dcb2015-10-06 07:38:23 -0700180 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700181 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400182 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawText", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -0700183
Robert Phillips72152832017-01-25 17:31:35 -0500184 GrAtlasTextContext* atlasTextContext = this->drawingManager()->getAtlasTextContext();
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500185 atlasTextContext->drawText(fContext, this, clip, skPaint, viewMatrix, fSurfaceProps, text,
186 byteLength, x, y, clipBounds);
robertphillips2334fb62015-06-17 05:43:33 -0700187}
robertphillipscaef3452015-11-11 13:18:11 -0800188
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500189void GrRenderTargetContext::drawPosText(const GrClip& clip, const SkPaint& paint,
190 const SkMatrix& viewMatrix, const char text[],
191 size_t byteLength, const SkScalar pos[],
Brian Salomon82f44312017-01-11 13:42:54 -0500192 int scalarsPerPosition, const SkPoint& offset,
193 const SkIRect& clipBounds) {
joshualitt1de610a2016-01-06 08:26:09 -0800194 ASSERT_SINGLE_OWNER
robertphillips2d70dcb2015-10-06 07:38:23 -0700195 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700196 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400197 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawPosText", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -0700198
Robert Phillips72152832017-01-25 17:31:35 -0500199 GrAtlasTextContext* atlasTextContext = this->drawingManager()->getAtlasTextContext();
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500200 atlasTextContext->drawPosText(fContext, this, clip, paint, viewMatrix, fSurfaceProps, text,
201 byteLength, pos, scalarsPerPosition, offset, clipBounds);
robertphillips2334fb62015-06-17 05:43:33 -0700202}
robertphillipscaef3452015-11-11 13:18:11 -0800203
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500204void GrRenderTargetContext::drawTextBlob(const GrClip& clip, const SkPaint& paint,
Brian Osman11052242016-10-27 14:47:55 -0400205 const SkMatrix& viewMatrix, const SkTextBlob* blob,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500206 SkScalar x, SkScalar y, SkDrawFilter* filter,
207 const SkIRect& clipBounds) {
joshualitt1de610a2016-01-06 08:26:09 -0800208 ASSERT_SINGLE_OWNER
robertphillips2d70dcb2015-10-06 07:38:23 -0700209 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700210 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400211 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTextBlob", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -0700212
Robert Phillips72152832017-01-25 17:31:35 -0500213 GrAtlasTextContext* atlasTextContext = this->drawingManager()->getAtlasTextContext();
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500214 atlasTextContext->drawTextBlob(fContext, this, clip, paint, viewMatrix, fSurfaceProps, blob, x,
215 y, filter, clipBounds);
robertphillipsea461502015-05-26 11:38:03 -0700216}
217
Brian Osman11052242016-10-27 14:47:55 -0400218void GrRenderTargetContext::discard() {
joshualitt1de610a2016-01-06 08:26:09 -0800219 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700220 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700221 SkDEBUGCODE(this->validate();)
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400222 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "discard", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700223
Robert Phillips72152832017-01-25 17:31:35 -0500224 AutoCheckFlush acf(this->drawingManager());
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400225
Robert Phillips380b90c2017-08-30 07:41:07 -0400226 this->getRTOpList()->discard();
robertphillipsea461502015-05-26 11:38:03 -0700227}
228
Brian Osman11052242016-10-27 14:47:55 -0400229void GrRenderTargetContext::clear(const SkIRect* rect,
230 const GrColor color,
231 bool canIgnoreRect) {
joshualitt1de610a2016-01-06 08:26:09 -0800232 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700233 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700234 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400235 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "clear", fContext);
robertphillipsea461502015-05-26 11:38:03 -0700236
Robert Phillips72152832017-01-25 17:31:35 -0500237 AutoCheckFlush acf(this->drawingManager());
csmartdalton29df7602016-08-31 11:55:52 -0700238 this->internalClear(rect ? GrFixedClip(*rect) : GrFixedClip::Disabled(), color, canIgnoreRect);
239}
robertphillips9199a9f2016-07-13 07:48:43 -0700240
Robert Phillips784b7bf2016-12-09 13:35:02 -0500241void GrRenderTargetContextPriv::absClear(const SkIRect* clearRect, const GrColor color) {
242 ASSERT_SINGLE_OWNER_PRIV
243 RETURN_IF_ABANDONED_PRIV
244 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400245 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "absClear",
246 fRenderTargetContext->fContext);
Robert Phillips784b7bf2016-12-09 13:35:02 -0500247
Robert Phillips72152832017-01-25 17:31:35 -0500248 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500249
Brian Salomonbb5711a2017-05-17 13:49:59 -0400250 SkIRect rtRect = SkIRect::MakeWH(fRenderTargetContext->fRenderTargetProxy->worstCaseWidth(),
251 fRenderTargetContext->fRenderTargetProxy->worstCaseHeight());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500252
253 if (clearRect) {
254 if (clearRect->contains(rtRect)) {
255 clearRect = nullptr; // full screen
256 } else {
257 if (!rtRect.intersect(*clearRect)) {
258 return;
259 }
260 }
261 }
262
263 // TODO: in a post-MDB world this should be handled at the OpList level.
264 // An op-list that is initially cleared and has no other ops should receive an
265 // extra draw.
266 if (fRenderTargetContext->fContext->caps()->useDrawInsteadOfClear()) {
267 // This works around a driver bug with clear by drawing a rect instead.
268 // The driver will ignore a clear if it is the only thing rendered to a
269 // target before the target is read.
270 GrPaint paint;
271 paint.setColor4f(GrColor4f::FromGrColor(color));
Brian Salomona1633922017-01-09 11:46:10 -0500272 paint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
Robert Phillips784b7bf2016-12-09 13:35:02 -0500273
274 // We don't call drawRect() here to avoid the cropping to the, possibly smaller,
275 // RenderTargetProxy bounds
Brian Salomonbaaf4392017-06-15 09:59:23 -0400276 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeNonAAFill(
277 std::move(paint), SkMatrix::I(), SkRect::Make(rtRect), GrAAType::kNone);
278 fRenderTargetContext->addDrawOp(GrNoClip(), std::move(op));
Robert Phillips784b7bf2016-12-09 13:35:02 -0500279 } else {
Robert Phillips784b7bf2016-12-09 13:35:02 -0500280 // This path doesn't handle coalescing of full screen clears b.c. it
281 // has to clear the entire render target - not just the content area.
282 // It could be done but will take more finagling.
Robert Phillips5efd5ea2017-05-30 13:47:32 -0400283 std::unique_ptr<GrOp> op(GrClearOp::Make(rtRect, color, !clearRect));
Brian Salomonfc527d22016-12-14 21:07:01 -0500284 if (!op) {
Robert Phillips784b7bf2016-12-09 13:35:02 -0500285 return;
286 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400287 fRenderTargetContext->getRTOpList()->addOp(std::move(op), *fRenderTargetContext->caps());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500288 }
289}
290
Brian Osman11052242016-10-27 14:47:55 -0400291void GrRenderTargetContextPriv::clear(const GrFixedClip& clip,
292 const GrColor color,
293 bool canIgnoreClip) {
csmartdalton29df7602016-08-31 11:55:52 -0700294 ASSERT_SINGLE_OWNER_PRIV
295 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400296 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400297 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "clear",
298 fRenderTargetContext->fContext);
csmartdalton29df7602016-08-31 11:55:52 -0700299
Robert Phillips72152832017-01-25 17:31:35 -0500300 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Brian Osman11052242016-10-27 14:47:55 -0400301 fRenderTargetContext->internalClear(clip, color, canIgnoreClip);
csmartdalton29df7602016-08-31 11:55:52 -0700302}
303
Brian Osman11052242016-10-27 14:47:55 -0400304void GrRenderTargetContext::internalClear(const GrFixedClip& clip,
305 const GrColor color,
306 bool canIgnoreClip) {
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700307 bool isFull = false;
308 if (!clip.hasWindowRectangles()) {
309 isFull = !clip.scissorEnabled() ||
310 (canIgnoreClip && fContext->caps()->fullClearIsFree()) ||
311 clip.scissorRect().contains(SkIRect::MakeWH(this->width(), this->height()));
312 }
robertphillips9199a9f2016-07-13 07:48:43 -0700313
314 if (fContext->caps()->useDrawInsteadOfClear()) {
315 // This works around a driver bug with clear by drawing a rect instead.
316 // The driver will ignore a clear if it is the only thing rendered to a
317 // target before the target is read.
Robert Phillips784b7bf2016-12-09 13:35:02 -0500318 SkIRect clearRect = SkIRect::MakeWH(this->width(), this->height());
csmartdalton29df7602016-08-31 11:55:52 -0700319 if (isFull) {
robertphillips9199a9f2016-07-13 07:48:43 -0700320 this->discard();
Robert Phillips93f16332016-11-23 19:37:13 -0500321 } else if (!clearRect.intersect(clip.scissorRect())) {
csmartdalton29df7602016-08-31 11:55:52 -0700322 return;
robertphillips9199a9f2016-07-13 07:48:43 -0700323 }
324
325 GrPaint paint;
326 paint.setColor4f(GrColor4f::FromGrColor(color));
Brian Salomona1633922017-01-09 11:46:10 -0500327 paint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
robertphillips9199a9f2016-07-13 07:48:43 -0700328
Brian Salomon82f44312017-01-11 13:42:54 -0500329 this->drawRect(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), SkRect::Make(clearRect));
bsalomon9f129de2016-08-10 16:31:05 -0700330 } else if (isFull) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400331 this->getRTOpList()->fullClear(*this->caps(), color);
robertphillips9199a9f2016-07-13 07:48:43 -0700332 } else {
Robert Phillips5efd5ea2017-05-30 13:47:32 -0400333 std::unique_ptr<GrOp> op(GrClearOp::Make(clip, color, this->asSurfaceProxy()));
Brian Salomon42521e82016-12-07 16:44:58 -0500334 if (!op) {
csmartdalton29df7602016-08-31 11:55:52 -0700335 return;
336 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400337 this->getRTOpList()->addOp(std::move(op), *this->caps());
robertphillips9199a9f2016-07-13 07:48:43 -0700338 }
robertphillipsea461502015-05-26 11:38:03 -0700339}
340
Brian Osman11052242016-10-27 14:47:55 -0400341void GrRenderTargetContext::drawPaint(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500342 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400343 const SkMatrix& viewMatrix) {
joshualitt1de610a2016-01-06 08:26:09 -0800344 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700345 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700346 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400347 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawPaint", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700348
robertphillipsea461502015-05-26 11:38:03 -0700349 // set rect to be big enough to fill the space, but not super-huge, so we
350 // don't overflow fixed-point implementations
robertphillips13a7eee2016-08-31 15:06:24 -0700351
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400352 SkRect r = fRenderTargetProxy->getBoundsRect();
robertphillipsea461502015-05-26 11:38:03 -0700353
bsalomoncb31e512016-08-26 10:48:19 -0700354 SkRRect rrect;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500355 GrAA aa;
bsalomoncb31e512016-08-26 10:48:19 -0700356 // Check if we can replace a clipRRect()/drawPaint() with a drawRRect(). We only do the
357 // transformation for non-rect rrects. Rects caused a performance regression on an Android
358 // test that needs investigation. We also skip cases where there are fragment processors
359 // because they may depend on having correct local coords and this path draws in device space
360 // without a local matrix.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500361 if (!paint.numTotalFragmentProcessors() && clip.isRRect(r, &rrect, &aa) && !rrect.isRect()) {
Brian Salomon82f44312017-01-11 13:42:54 -0500362 this->drawRRect(GrNoClip(), std::move(paint), aa, SkMatrix::I(), rrect,
363 GrStyle::SimpleFill());
bsalomoncb31e512016-08-26 10:48:19 -0700364 return;
365 }
366
robertphillipsea461502015-05-26 11:38:03 -0700367
368 bool isPerspective = viewMatrix.hasPerspective();
369
370 // We attempt to map r by the inverse matrix and draw that. mapRect will
371 // map the four corners and bound them with a new rect. This will not
372 // produce a correct result for some perspective matrices.
373 if (!isPerspective) {
reeda39667c2016-08-22 06:39:49 -0700374 if (!SkMatrixPriv::InverseMapRect(viewMatrix, &r, r)) {
robertphillipsea461502015-05-26 11:38:03 -0700375 SkDebugf("Could not invert matrix\n");
376 return;
377 }
Brian Salomon82f44312017-01-11 13:42:54 -0500378 this->drawRect(clip, std::move(paint), GrAA::kNo, viewMatrix, r);
robertphillipsea461502015-05-26 11:38:03 -0700379 } else {
380 SkMatrix localMatrix;
381 if (!viewMatrix.invert(&localMatrix)) {
382 SkDebugf("Could not invert matrix\n");
383 return;
384 }
385
Robert Phillips72152832017-01-25 17:31:35 -0500386 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700387
Brian Salomonbaaf4392017-06-15 09:59:23 -0400388 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeNonAAFillWithLocalMatrix(
389 std::move(paint), SkMatrix::I(), localMatrix, r, GrAAType::kNone);
390 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -0700391 }
392}
393
robertphillipsea461502015-05-26 11:38:03 -0700394static inline bool rect_contains_inclusive(const SkRect& rect, const SkPoint& point) {
395 return point.fX >= rect.fLeft && point.fX <= rect.fRight &&
396 point.fY >= rect.fTop && point.fY <= rect.fBottom;
397}
398
csmartdalton97f6cd52016-07-13 13:37:08 -0700399// Attempts to crop a rect and optional local rect to the clip boundaries.
400// Returns false if the draw can be skipped entirely.
robertphillips13a7eee2016-08-31 15:06:24 -0700401static bool crop_filled_rect(int width, int height, const GrClip& clip,
csmartdalton97f6cd52016-07-13 13:37:08 -0700402 const SkMatrix& viewMatrix, SkRect* rect,
403 SkRect* localRect = nullptr) {
404 if (!viewMatrix.rectStaysRect()) {
405 return true;
406 }
407
csmartdalton97f6cd52016-07-13 13:37:08 -0700408 SkIRect clipDevBounds;
409 SkRect clipBounds;
csmartdalton97f6cd52016-07-13 13:37:08 -0700410
robertphillips13a7eee2016-08-31 15:06:24 -0700411 clip.getConservativeBounds(width, height, &clipDevBounds);
reeda39667c2016-08-22 06:39:49 -0700412 if (!SkMatrixPriv::InverseMapRect(viewMatrix, &clipBounds, SkRect::Make(clipDevBounds))) {
413 return false;
414 }
csmartdalton97f6cd52016-07-13 13:37:08 -0700415
416 if (localRect) {
417 if (!rect->intersects(clipBounds)) {
418 return false;
419 }
420 const SkScalar dx = localRect->width() / rect->width();
421 const SkScalar dy = localRect->height() / rect->height();
422 if (clipBounds.fLeft > rect->fLeft) {
423 localRect->fLeft += (clipBounds.fLeft - rect->fLeft) * dx;
424 rect->fLeft = clipBounds.fLeft;
425 }
426 if (clipBounds.fTop > rect->fTop) {
427 localRect->fTop += (clipBounds.fTop - rect->fTop) * dy;
428 rect->fTop = clipBounds.fTop;
429 }
430 if (clipBounds.fRight < rect->fRight) {
431 localRect->fRight -= (rect->fRight - clipBounds.fRight) * dx;
432 rect->fRight = clipBounds.fRight;
433 }
434 if (clipBounds.fBottom < rect->fBottom) {
435 localRect->fBottom -= (rect->fBottom - clipBounds.fBottom) * dy;
436 rect->fBottom = clipBounds.fBottom;
437 }
438 return true;
439 }
440
441 return rect->intersect(clipBounds);
442}
443
Brian Osman11052242016-10-27 14:47:55 -0400444bool GrRenderTargetContext::drawFilledRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500445 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500446 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400447 const SkMatrix& viewMatrix,
448 const SkRect& rect,
449 const GrUserStencilSettings* ss) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700450 SkRect croppedRect = rect;
Robert Phillips784b7bf2016-12-09 13:35:02 -0500451 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix, &croppedRect)) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700452 return true;
453 }
robertphillips44302392016-07-08 14:43:03 -0700454
Brian Salomon54d212e2017-03-21 14:22:38 -0400455 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport() &&
456 (!ss || ss->isDisabled(false))) {
Robert Phillipse3302df2017-04-24 07:31:02 -0400457 gr_instanced::OpAllocator* oa = this->drawingManager()->instancingAllocator();
458 std::unique_ptr<GrDrawOp> op = oa->recordRect(croppedRect, viewMatrix, std::move(paint),
459 aa, fInstancedPipelineInfo);
Brian Salomon42521e82016-12-07 16:44:58 -0500460 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -0400461 this->addDrawOp(clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700462 return true;
csmartdaltona7f29642016-07-07 08:49:11 -0700463 }
464 }
Brian Salomon7c8460e2017-05-12 11:36:10 -0400465 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400466 std::unique_ptr<GrDrawOp> op;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500467 if (GrAAType::kCoverage == aaType) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400468 op = GrRectOpFactory::MakeAAFill(std::move(paint), viewMatrix, croppedRect, ss);
robertphillips391395d2016-03-02 09:26:36 -0800469 } else {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400470 op = GrRectOpFactory::MakeNonAAFill(std::move(paint), viewMatrix, croppedRect, aaType, ss);
robertphillips391395d2016-03-02 09:26:36 -0800471 }
Brian Salomonbaaf4392017-06-15 09:59:23 -0400472 if (!op) {
473 return false;
474 }
475 this->addDrawOp(clip, std::move(op));
476 return true;
robertphillips391395d2016-03-02 09:26:36 -0800477}
478
Brian Osman11052242016-10-27 14:47:55 -0400479void GrRenderTargetContext::drawRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500480 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500481 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400482 const SkMatrix& viewMatrix,
483 const SkRect& rect,
484 const GrStyle* style) {
bsalomon6663acf2016-05-10 09:14:17 -0700485 if (!style) {
486 style = &GrStyle::SimpleFill();
487 }
joshualitt1de610a2016-01-06 08:26:09 -0800488 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700489 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700490 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400491 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRect", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700492
bsalomon6663acf2016-05-10 09:14:17 -0700493 // Path effects should've been devolved to a path in SkGpuDevice
494 SkASSERT(!style->pathEffect());
robertphillipsea461502015-05-26 11:38:03 -0700495
Robert Phillips72152832017-01-25 17:31:35 -0500496 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700497
bsalomon6663acf2016-05-10 09:14:17 -0700498 const SkStrokeRec& stroke = style->strokeRec();
robertphillips3ab14ca2016-07-10 11:49:39 -0700499 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
Greg Daniela5cb7812017-06-16 09:45:32 -0400500
robertphillips3ab14ca2016-07-10 11:49:39 -0700501 if (!fContext->caps()->useDrawInsteadOfClear()) {
502 // Check if this is a full RT draw and can be replaced with a clear. We don't bother
503 // checking cases where the RT is fully inside a stroke.
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400504 SkRect rtRect = fRenderTargetProxy->getBoundsRect();
robertphillips3ab14ca2016-07-10 11:49:39 -0700505 // Does the clip contain the entire RT?
506 if (clip.quickContains(rtRect)) {
507 SkMatrix invM;
508 if (!viewMatrix.invert(&invM)) {
robertphillipsea461502015-05-26 11:38:03 -0700509 return;
510 }
robertphillips3ab14ca2016-07-10 11:49:39 -0700511 // Does the rect bound the RT?
512 SkPoint srcSpaceRTQuad[4];
513 invM.mapRectToQuad(srcSpaceRTQuad, rtRect);
514 if (rect_contains_inclusive(rect, srcSpaceRTQuad[0]) &&
515 rect_contains_inclusive(rect, srcSpaceRTQuad[1]) &&
516 rect_contains_inclusive(rect, srcSpaceRTQuad[2]) &&
517 rect_contains_inclusive(rect, srcSpaceRTQuad[3])) {
518 // Will it blend?
519 GrColor clearColor;
520 if (paint.isConstantBlendedColor(&clearColor)) {
robertphillips9199a9f2016-07-13 07:48:43 -0700521 this->clear(nullptr, clearColor, true);
robertphillips3ab14ca2016-07-10 11:49:39 -0700522 return;
523 }
524 }
robertphillipsea461502015-05-26 11:38:03 -0700525 }
526 }
robertphillips44302392016-07-08 14:43:03 -0700527
Brian Salomon82f44312017-01-11 13:42:54 -0500528 if (this->drawFilledRect(clip, std::move(paint), aa, viewMatrix, rect, nullptr)) {
robertphillips44302392016-07-08 14:43:03 -0700529 return;
530 }
bsalomona7d85ba2016-07-06 11:54:59 -0700531 } else if (stroke.getStyle() == SkStrokeRec::kStroke_Style ||
532 stroke.getStyle() == SkStrokeRec::kHairline_Style) {
533 if ((!rect.width() || !rect.height()) &&
534 SkStrokeRec::kHairline_Style != stroke.getStyle()) {
535 SkScalar r = stroke.getWidth() / 2;
536 // TODO: Move these stroke->fill fallbacks to GrShape?
537 switch (stroke.getJoin()) {
538 case SkPaint::kMiter_Join:
Brian Salomon82f44312017-01-11 13:42:54 -0500539 this->drawRect(
540 clip, std::move(paint), aa, viewMatrix,
541 {rect.fLeft - r, rect.fTop - r, rect.fRight + r, rect.fBottom + r},
542 &GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700543 return;
544 case SkPaint::kRound_Join:
545 // Raster draws nothing when both dimensions are empty.
546 if (rect.width() || rect.height()){
547 SkRRect rrect = SkRRect::MakeRectXY(rect.makeOutset(r, r), r, r);
Brian Salomon82f44312017-01-11 13:42:54 -0500548 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect,
549 GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700550 return;
551 }
552 case SkPaint::kBevel_Join:
553 if (!rect.width()) {
Brian Salomon82f44312017-01-11 13:42:54 -0500554 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700555 {rect.fLeft - r, rect.fTop, rect.fRight + r, rect.fBottom},
556 &GrStyle::SimpleFill());
557 } else {
Brian Salomon82f44312017-01-11 13:42:54 -0500558 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700559 {rect.fLeft, rect.fTop - r, rect.fRight, rect.fBottom + r},
560 &GrStyle::SimpleFill());
561 }
562 return;
563 }
564 }
robertphillips44302392016-07-08 14:43:03 -0700565
Brian Salomonbaaf4392017-06-15 09:59:23 -0400566 std::unique_ptr<GrDrawOp> op;
robertphillips44302392016-07-08 14:43:03 -0700567
Brian Salomon7c8460e2017-05-12 11:36:10 -0400568 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500569 if (GrAAType::kCoverage == aaType) {
cdaltonbb539482016-01-04 09:48:25 -0800570 // The stroke path needs the rect to remain axis aligned (no rotation or skew).
571 if (viewMatrix.rectStaysRect()) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400572 op = GrRectOpFactory::MakeAAStroke(std::move(paint), viewMatrix, rect, stroke);
cdaltonbb539482016-01-04 09:48:25 -0800573 }
robertphillipsea461502015-05-26 11:38:03 -0700574 } else {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400575 op = GrRectOpFactory::MakeNonAAStroke(std::move(paint), viewMatrix, rect, stroke,
576 aaType);
robertphillips391395d2016-03-02 09:26:36 -0800577 }
robertphillips4bc31812016-03-01 12:22:49 -0800578
Brian Salomon42521e82016-12-07 16:44:58 -0500579 if (op) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400580 this->addDrawOp(clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700581 return;
robertphillips4bc31812016-03-01 12:22:49 -0800582 }
robertphillips4bc31812016-03-01 12:22:49 -0800583 }
halcanary9d524f22016-03-29 09:03:52 -0700584
robertphillips4bc31812016-03-01 12:22:49 -0800585 SkPath path;
586 path.setIsVolatile(true);
587 path.addRect(rect);
Brian Salomon82f44312017-01-11 13:42:54 -0500588 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, *style);
robertphillipsea461502015-05-26 11:38:03 -0700589}
590
Robert Phillipsec2249f2016-11-09 08:54:35 -0500591int GrRenderTargetContextPriv::maxWindowRectangles() const {
592 return fRenderTargetContext->fRenderTargetProxy->maxWindowRectangles(
593 *fRenderTargetContext->fContext->caps());
594}
595
Brian Osman11052242016-10-27 14:47:55 -0400596void GrRenderTargetContextPriv::clearStencilClip(const GrFixedClip& clip, bool insideStencilMask) {
robertphillips976f5f02016-06-03 10:59:20 -0700597 ASSERT_SINGLE_OWNER_PRIV
598 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400599 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400600 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "clearStencilClip",
601 fRenderTargetContext->fContext);
robertphillips976f5f02016-06-03 10:59:20 -0700602
Robert Phillips72152832017-01-25 17:31:35 -0500603 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400604
Robert Phillips2f4ddf62017-06-01 08:48:19 -0400605 std::unique_ptr<GrOp> op(GrClearStencilClipOp::Make(
606 clip, insideStencilMask,
607 fRenderTargetContext->fRenderTargetProxy.get()));
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400608 if (!op) {
Robert Phillipse60ad622016-11-17 10:22:48 -0500609 return;
610 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400611 fRenderTargetContext->getRTOpList()->addOp(std::move(op), *fRenderTargetContext->caps());
robertphillips976f5f02016-06-03 10:59:20 -0700612}
613
Brian Osman11052242016-10-27 14:47:55 -0400614void GrRenderTargetContextPriv::stencilPath(const GrClip& clip,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500615 GrAAType aaType,
Brian Osman11052242016-10-27 14:47:55 -0400616 const SkMatrix& viewMatrix,
617 const GrPath* path) {
Brian Salomon467921e2017-03-06 16:17:12 -0500618 ASSERT_SINGLE_OWNER_PRIV
619 RETURN_IF_ABANDONED_PRIV
620 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400621 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "stencilPath",
622 fRenderTargetContext->fContext);
Brian Salomon467921e2017-03-06 16:17:12 -0500623
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500624 SkASSERT(aaType != GrAAType::kCoverage);
Brian Salomon467921e2017-03-06 16:17:12 -0500625
626 bool useHWAA = GrAATypeIsHW(aaType);
627 // TODO: extract portions of checkDraw that are relevant to path stenciling.
628 SkASSERT(path);
629 SkASSERT(fRenderTargetContext->caps()->shaderCaps()->pathRenderingSupport());
630
631 // FIXME: Use path bounds instead of this WAR once
632 // https://bugs.chromium.org/p/skia/issues/detail?id=5640 is resolved.
633 SkRect bounds = SkRect::MakeIWH(fRenderTargetContext->width(), fRenderTargetContext->height());
634
635 // Setup clip
Brian Salomon97180af2017-03-14 13:42:58 -0400636 GrAppliedClip appliedClip;
Brian Salomon467921e2017-03-06 16:17:12 -0500637 if (!clip.apply(fRenderTargetContext->fContext, fRenderTargetContext, useHWAA, true,
Brian Salomon97180af2017-03-14 13:42:58 -0400638 &appliedClip, &bounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -0500639 return;
640 }
641
642 // Coverage AA does not make sense when rendering to the stencil buffer. The caller should never
643 // attempt this in a situation that would require coverage AA.
644 SkASSERT(!appliedClip.clipCoverageFragmentProcessor());
645
Robert Phillips65048132017-08-10 08:44:49 -0400646 fRenderTargetContext->setNeedsStencil();
Brian Salomon467921e2017-03-06 16:17:12 -0500647
648 std::unique_ptr<GrOp> op = GrStencilPathOp::Make(viewMatrix,
649 useHWAA,
650 path->getFillType(),
651 appliedClip.hasStencilClip(),
Brian Salomon467921e2017-03-06 16:17:12 -0500652 appliedClip.scissorState(),
Brian Salomon467921e2017-03-06 16:17:12 -0500653 path);
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400654 if (!op) {
655 return;
656 }
Brian Salomon97180af2017-03-14 13:42:58 -0400657 op->setClippedBounds(bounds);
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400658 fRenderTargetContext->getRTOpList()->addOp(std::move(op), *fRenderTargetContext->caps());
robertphillips976f5f02016-06-03 10:59:20 -0700659}
660
Brian Osman11052242016-10-27 14:47:55 -0400661void GrRenderTargetContextPriv::stencilRect(const GrClip& clip,
662 const GrUserStencilSettings* ss,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500663 GrAAType aaType,
Brian Osman11052242016-10-27 14:47:55 -0400664 const SkMatrix& viewMatrix,
665 const SkRect& rect) {
robertphillips976f5f02016-06-03 10:59:20 -0700666 ASSERT_SINGLE_OWNER_PRIV
667 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400668 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400669 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "stencilRect",
670 fRenderTargetContext->fContext);
671
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500672 SkASSERT(GrAAType::kCoverage != aaType);
Robert Phillips72152832017-01-25 17:31:35 -0500673 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips976f5f02016-06-03 10:59:20 -0700674
675 GrPaint paint;
Brian Salomona1633922017-01-09 11:46:10 -0500676 paint.setXPFactory(GrDisableColorXPFactory::Get());
Brian Salomonbaaf4392017-06-15 09:59:23 -0400677 std::unique_ptr<GrDrawOp> op =
678 GrRectOpFactory::MakeNonAAFill(std::move(paint), viewMatrix, rect, aaType, ss);
679 fRenderTargetContext->addDrawOp(clip, std::move(op));
robertphillips976f5f02016-06-03 10:59:20 -0700680}
681
Brian Osman11052242016-10-27 14:47:55 -0400682bool GrRenderTargetContextPriv::drawAndStencilRect(const GrClip& clip,
683 const GrUserStencilSettings* ss,
684 SkRegion::Op op,
685 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500686 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400687 const SkMatrix& viewMatrix,
688 const SkRect& rect) {
robertphillips391395d2016-03-02 09:26:36 -0800689 ASSERT_SINGLE_OWNER_PRIV
690 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400691 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400692 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawAndStencilRect",
693 fRenderTargetContext->fContext);
robertphillips391395d2016-03-02 09:26:36 -0800694
Robert Phillips72152832017-01-25 17:31:35 -0500695 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -0800696
697 GrPaint paint;
robertphillips391395d2016-03-02 09:26:36 -0800698 paint.setCoverageSetOpXPFactory(op, invert);
699
Brian Salomon82f44312017-01-11 13:42:54 -0500700 if (fRenderTargetContext->drawFilledRect(clip, std::move(paint), aa, viewMatrix, rect, ss)) {
robertphillips391395d2016-03-02 09:26:36 -0800701 return true;
702 }
robertphillips391395d2016-03-02 09:26:36 -0800703 SkPath path;
704 path.setIsVolatile(true);
705 path.addRect(rect);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500706 return this->drawAndStencilPath(clip, ss, op, invert, aa, viewMatrix, path);
robertphillips391395d2016-03-02 09:26:36 -0800707}
708
Brian Osman11052242016-10-27 14:47:55 -0400709void GrRenderTargetContext::fillRectToRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500710 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500711 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400712 const SkMatrix& viewMatrix,
713 const SkRect& rectToDraw,
714 const SkRect& localRect) {
joshualitt1de610a2016-01-06 08:26:09 -0800715 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700716 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700717 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400718 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "fillRectToRect", fContext);
robertphillipsea461502015-05-26 11:38:03 -0700719
csmartdalton97f6cd52016-07-13 13:37:08 -0700720 SkRect croppedRect = rectToDraw;
721 SkRect croppedLocalRect = localRect;
robertphillips13a7eee2016-08-31 15:06:24 -0700722 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix,
723 &croppedRect, &croppedLocalRect)) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700724 return;
725 }
726
Robert Phillips72152832017-01-25 17:31:35 -0500727 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -0700728
csmartdaltone0d36292016-07-29 08:14:20 -0700729 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport()) {
Robert Phillipse3302df2017-04-24 07:31:02 -0400730 gr_instanced::OpAllocator* oa = this->drawingManager()->instancingAllocator();
731 std::unique_ptr<GrDrawOp> op(oa->recordRect(croppedRect, viewMatrix, std::move(paint),
Brian Salomon54d212e2017-03-21 14:22:38 -0400732 croppedLocalRect, aa, fInstancedPipelineInfo));
Brian Salomon42521e82016-12-07 16:44:58 -0500733 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -0400734 this->addDrawOp(clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -0700735 return;
736 }
737 }
738
Brian Salomon7c8460e2017-05-12 11:36:10 -0400739 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500740 if (GrAAType::kCoverage != aaType) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400741 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeNonAAFillWithLocalRect(
742 std::move(paint), viewMatrix, croppedRect, croppedLocalRect, aaType);
743 this->addDrawOp(clip, std::move(op));
csmartdaltonfc49d562016-07-26 17:05:47 -0700744 return;
joshualitt04194f32016-01-13 10:08:27 -0800745 }
bsalomonbb243832016-07-22 07:10:19 -0700746
Brian Salomonbaaf4392017-06-15 09:59:23 -0400747 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeAAFillWithLocalRect(
748 std::move(paint), viewMatrix, croppedRect, croppedLocalRect);
749 if (op) {
750 this->addDrawOp(clip, std::move(op));
csmartdaltonfc49d562016-07-26 17:05:47 -0700751 return;
752 }
753
754 SkMatrix viewAndUnLocalMatrix;
755 if (!viewAndUnLocalMatrix.setRectToRect(localRect, rectToDraw, SkMatrix::kFill_ScaleToFit)) {
756 SkDebugf("fillRectToRect called with empty local matrix.\n");
757 return;
758 }
759 viewAndUnLocalMatrix.postConcat(viewMatrix);
760
761 SkPath path;
762 path.setIsVolatile(true);
763 path.addRect(localRect);
Brian Salomon82f44312017-01-11 13:42:54 -0500764 this->internalDrawPath(clip, std::move(paint), aa, viewAndUnLocalMatrix, path, GrStyle());
joshualittb6b513b2015-08-21 10:25:18 -0700765}
766
Brian Salomon34169692017-08-28 15:32:01 -0400767static bool must_filter(const SkRect& src, const SkRect& dst, const SkMatrix& ctm) {
768 // We don't currently look for 90 degree rotations, mirroring, or downscales that sample at
769 // texel centers.
770 if (!ctm.isTranslate()) {
771 return true;
772 }
773 if (src.width() != dst.width() || src.height() != dst.height()) {
774 return true;
775 }
776 // Check that the device space rectangle's fractional offset is the same as the src rectangle,
777 // and that therefore integers in the src image fall on integers in device space.
778 SkScalar x = ctm.getTranslateX(), y = ctm.getTranslateY();
779 x += dst.fLeft; y += dst.fTop;
780 x -= src.fLeft; y -= src.fTop;
781 return !SkScalarIsInt(x) || !SkScalarIsInt(y);
782}
783
784void GrRenderTargetContext::drawTextureAffine(const GrClip& clip, sk_sp<GrTextureProxy> proxy,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400785 GrSamplerState::Filter filter, GrColor color,
Brian Salomon34169692017-08-28 15:32:01 -0400786 const SkRect& srcRect, const SkRect& dstRect,
787 const SkMatrix& viewMatrix,
788 sk_sp<GrColorSpaceXform> colorSpaceXform) {
789 ASSERT_SINGLE_OWNER
790 RETURN_IF_ABANDONED
791 SkDEBUGCODE(this->validate();)
792 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTextureAffine", fContext);
793 SkASSERT(!viewMatrix.hasPerspective());
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400794 if (filter != GrSamplerState::Filter::kNearest && !must_filter(srcRect, dstRect, viewMatrix)) {
795 filter = GrSamplerState::Filter::kNearest;
Brian Salomon34169692017-08-28 15:32:01 -0400796 }
Brian Salomonff9d6d32017-08-30 10:27:49 -0400797 SkRect clippedDstRect = dstRect;
798 SkRect clippedSrcRect = srcRect;
799 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix, &clippedDstRect,
800 &clippedSrcRect)) {
801 return;
802 }
803
Brian Salomon34169692017-08-28 15:32:01 -0400804 bool allowSRGB = SkToBool(this->getColorSpace());
Brian Salomonff9d6d32017-08-30 10:27:49 -0400805 this->addDrawOp(clip, GrTextureOp::Make(std::move(proxy), filter, color, clippedSrcRect,
806 clippedDstRect, viewMatrix, std::move(colorSpaceXform),
807 allowSRGB));
Brian Salomon34169692017-08-28 15:32:01 -0400808}
809
Brian Osman11052242016-10-27 14:47:55 -0400810void GrRenderTargetContext::fillRectWithLocalMatrix(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500811 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500812 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400813 const SkMatrix& viewMatrix,
814 const SkRect& rectToDraw,
815 const SkMatrix& localMatrix) {
joshualitt1de610a2016-01-06 08:26:09 -0800816 ASSERT_SINGLE_OWNER
joshualittb6b513b2015-08-21 10:25:18 -0700817 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700818 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400819 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "fillRectWithLocalMatrix", fContext);
joshualittb6b513b2015-08-21 10:25:18 -0700820
csmartdalton97f6cd52016-07-13 13:37:08 -0700821 SkRect croppedRect = rectToDraw;
robertphillips13a7eee2016-08-31 15:06:24 -0700822 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix, &croppedRect)) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700823 return;
824 }
825
Robert Phillips72152832017-01-25 17:31:35 -0500826 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -0700827
csmartdaltone0d36292016-07-29 08:14:20 -0700828 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport()) {
Robert Phillipse3302df2017-04-24 07:31:02 -0400829 gr_instanced::OpAllocator* oa = this->drawingManager()->instancingAllocator();
830 std::unique_ptr<GrDrawOp> op(oa->recordRect(croppedRect, viewMatrix, std::move(paint),
Brian Salomon54d212e2017-03-21 14:22:38 -0400831 localMatrix, aa, fInstancedPipelineInfo));
Brian Salomon42521e82016-12-07 16:44:58 -0500832 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -0400833 this->addDrawOp(clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -0700834 return;
835 }
836 }
837
Brian Salomon7c8460e2017-05-12 11:36:10 -0400838 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500839 if (GrAAType::kCoverage != aaType) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400840 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeNonAAFillWithLocalMatrix(
841 std::move(paint), viewMatrix, localMatrix, croppedRect, aaType);
842 this->addDrawOp(clip, std::move(op));
csmartdaltonfc49d562016-07-26 17:05:47 -0700843 return;
bsalomonc55271f2015-11-09 11:55:57 -0800844 }
robertphillips4bc31812016-03-01 12:22:49 -0800845
Brian Salomonbaaf4392017-06-15 09:59:23 -0400846 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeAAFillWithLocalMatrix(
847 std::move(paint), viewMatrix, localMatrix, croppedRect);
848 if (op) {
849 this->addDrawOp(clip, std::move(op));
csmartdaltonfc49d562016-07-26 17:05:47 -0700850 return;
851 }
852
853 SkMatrix viewAndUnLocalMatrix;
854 if (!localMatrix.invert(&viewAndUnLocalMatrix)) {
855 SkDebugf("fillRectWithLocalMatrix called with degenerate local matrix.\n");
856 return;
857 }
858 viewAndUnLocalMatrix.postConcat(viewMatrix);
859
860 SkPath path;
861 path.setIsVolatile(true);
862 path.addRect(rectToDraw);
863 path.transform(localMatrix);
Brian Salomon82f44312017-01-11 13:42:54 -0500864 this->internalDrawPath(clip, std::move(paint), aa, viewAndUnLocalMatrix, path, GrStyle());
robertphillipsea461502015-05-26 11:38:03 -0700865}
866
Brian Osman11052242016-10-27 14:47:55 -0400867void GrRenderTargetContext::drawVertices(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500868 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400869 const SkMatrix& viewMatrix,
Brian Osmanae0c50c2017-05-25 16:56:34 -0400870 sk_sp<SkVertices> vertices,
871 GrPrimitiveType* overridePrimType) {
Brian Salomon199fb872017-02-06 09:41:10 -0500872 ASSERT_SINGLE_OWNER
873 RETURN_IF_ABANDONED
874 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400875 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawVertices", fContext);
Brian Salomon199fb872017-02-06 09:41:10 -0500876
877 AutoCheckFlush acf(this->drawingManager());
878
879 SkASSERT(vertices);
Brian Salomonc2f42542017-07-12 14:11:22 -0400880 GrAAType aaType = this->chooseAAType(GrAA::kNo, GrAllowMixedSamples::kNo);
881 std::unique_ptr<GrDrawOp> op =
882 GrDrawVerticesOp::Make(std::move(paint), std::move(vertices), viewMatrix, aaType,
883 this->isGammaCorrect(), fColorXformFromSRGB, overridePrimType);
884 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -0700885}
886
887///////////////////////////////////////////////////////////////////////////////
888
Brian Osman11052242016-10-27 14:47:55 -0400889void GrRenderTargetContext::drawAtlas(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500890 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400891 const SkMatrix& viewMatrix,
892 int spriteCount,
893 const SkRSXform xform[],
894 const SkRect texRect[],
895 const SkColor colors[]) {
joshualitt1de610a2016-01-06 08:26:09 -0800896 ASSERT_SINGLE_OWNER
jvanverth31ff7622015-08-07 10:09:28 -0700897 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700898 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400899 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawAtlas", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700900
Robert Phillips72152832017-01-25 17:31:35 -0500901 AutoCheckFlush acf(this->drawingManager());
halcanary9d524f22016-03-29 09:03:52 -0700902
Brian Salomon0088f942017-07-12 11:51:27 -0400903 GrAAType aaType = this->chooseAAType(GrAA::kNo, GrAllowMixedSamples::kNo);
904 std::unique_ptr<GrDrawOp> op = GrDrawAtlasOp::Make(std::move(paint), viewMatrix, aaType,
905 spriteCount, xform, texRect, colors);
906 this->addDrawOp(clip, std::move(op));
jvanverth31ff7622015-08-07 10:09:28 -0700907}
908
909///////////////////////////////////////////////////////////////////////////////
910
Brian Osman11052242016-10-27 14:47:55 -0400911void GrRenderTargetContext::drawRRect(const GrClip& origClip,
Brian Salomon82f44312017-01-11 13:42:54 -0500912 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500913 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400914 const SkMatrix& viewMatrix,
915 const SkRRect& rrect,
916 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -0800917 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700918 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700919 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400920 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRRect", fContext);
robertphillipsea461502015-05-26 11:38:03 -0700921 if (rrect.isEmpty()) {
922 return;
923 }
924
bsalomon7f0d9f32016-08-15 14:49:10 -0700925 GrNoClip noclip;
926 const GrClip* clip = &origClip;
927#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
928 // The Android framework frequently clips rrects to themselves where the clip is non-aa and the
Brian Salomon42521e82016-12-07 16:44:58 -0500929 // draw is aa. Since our lower level clip code works from op bounds, which are SkRects, it
bsalomon7f0d9f32016-08-15 14:49:10 -0700930 // doesn't detect that the clip can be ignored (modulo antialiasing). The following test
931 // attempts to mitigate the stencil clip cost but will only help when the entire clip stack
932 // can be ignored. We'd prefer to fix this in the framework by removing the clips calls.
933 SkRRect devRRect;
934 if (rrect.transform(viewMatrix, &devRRect) && clip->quickContains(devRRect)) {
935 clip = &noclip;
936 }
937#endif
bsalomon6663acf2016-05-10 09:14:17 -0700938 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
ksakamotoec7f2ac2016-07-05 03:54:53 -0700939
Robert Phillips72152832017-01-25 17:31:35 -0500940 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -0700941 const SkStrokeRec stroke = style.strokeRec();
csmartdaltona7f29642016-07-07 08:49:11 -0700942
csmartdaltone0d36292016-07-29 08:14:20 -0700943 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport() &&
944 stroke.isFillStyle()) {
Robert Phillipse3302df2017-04-24 07:31:02 -0400945 gr_instanced::OpAllocator* oa = this->drawingManager()->instancingAllocator();
Brian Salomon54d212e2017-03-21 14:22:38 -0400946 std::unique_ptr<GrDrawOp> op(
Robert Phillipse3302df2017-04-24 07:31:02 -0400947 oa->recordRRect(rrect, viewMatrix, std::move(paint), aa, fInstancedPipelineInfo));
Brian Salomon42521e82016-12-07 16:44:58 -0500948 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -0400949 this->addDrawOp(*clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -0700950 return;
951 }
952 }
953
Brian Salomon7c8460e2017-05-12 11:36:10 -0400954 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500955 if (GrAAType::kCoverage == aaType) {
Brian Salomon94efbf52016-11-29 13:43:05 -0500956 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomon05441c42017-05-15 16:45:49 -0400957 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeRRectOp(std::move(paint),
Brian Salomon05441c42017-05-15 16:45:49 -0400958 viewMatrix,
959 rrect,
960 stroke,
961 shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -0500962 if (op) {
Brian Salomon05441c42017-05-15 16:45:49 -0400963 this->addDrawOp(*clip, std::move(op));
robertphillipsb56f9272016-02-25 11:03:52 -0800964 return;
965 }
robertphillipsea461502015-05-26 11:38:03 -0700966 }
robertphillipsb56f9272016-02-25 11:03:52 -0800967
968 SkPath path;
969 path.setIsVolatile(true);
970 path.addRRect(rrect);
Brian Salomon82f44312017-01-11 13:42:54 -0500971 this->internalDrawPath(*clip, std::move(paint), aa, viewMatrix, path, style);
robertphillipsea461502015-05-26 11:38:03 -0700972}
973
Jim Van Verthc5903412016-11-17 15:27:09 -0500974///////////////////////////////////////////////////////////////////////////////
975
Jim Van Verth3af1af92017-05-18 15:06:54 -0400976static SkPoint3 map(const SkMatrix& m, const SkPoint3& pt) {
977 SkPoint3 result;
978 m.mapXY(pt.fX, pt.fY, (SkPoint*)&result.fX);
979 result.fZ = pt.fZ;
980 return result;
981}
982
983bool GrRenderTargetContext::drawFastShadow(const GrClip& clip,
Brian Salomon05969092017-07-13 11:20:51 -0400984 GrColor color4ub,
Jim Van Verth3af1af92017-05-18 15:06:54 -0400985 const SkMatrix& viewMatrix,
986 const SkPath& path,
987 const SkDrawShadowRec& rec) {
Jim Van Verthc5903412016-11-17 15:27:09 -0500988 ASSERT_SINGLE_OWNER
Jim Van Verth3af1af92017-05-18 15:06:54 -0400989 if (this->drawingManager()->wasAbandoned()) {
990 return true;
991 }
Jim Van Verthc5903412016-11-17 15:27:09 -0500992 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400993 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFastShadow", fContext);
Jim Van Verth3af1af92017-05-18 15:06:54 -0400994
995 // check z plane
996 bool tiltZPlane = SkToBool(!SkScalarNearlyZero(rec.fZPlaneParams.fX) ||
997 !SkScalarNearlyZero(rec.fZPlaneParams.fY));
998 bool skipAnalytic = SkToBool(rec.fFlags & SkShadowFlags::kGeometricOnly_ShadowFlag);
999 if (tiltZPlane || skipAnalytic || !viewMatrix.rectStaysRect() || !viewMatrix.isSimilarity()) {
1000 return false;
1001 }
1002
1003 SkRRect rrect;
1004 SkRect rect;
1005 // we can only handle rects, circles, and rrects with circular corners
1006 bool isRRect = path.isRRect(&rrect) && rrect.isSimpleCircular() &&
1007 rrect.radii(SkRRect::kUpperLeft_Corner).fX > SK_ScalarNearlyZero;
1008 if (!isRRect &&
1009 path.isOval(&rect) && SkScalarNearlyEqual(rect.width(), rect.height()) &&
1010 rect.width() > SK_ScalarNearlyZero) {
1011 rrect.setOval(rect);
1012 isRRect = true;
1013 }
1014 if (!isRRect && path.isRect(&rect)) {
1015 rrect.setRect(rect);
1016 isRRect = true;
1017 }
1018
1019 if (!isRRect) {
1020 return false;
1021 }
1022
Jim Van Verthc5903412016-11-17 15:27:09 -05001023 if (rrect.isEmpty()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001024 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001025 }
1026
Robert Phillips72152832017-01-25 17:31:35 -05001027 AutoCheckFlush acf(this->drawingManager());
Jim Van Verthc5903412016-11-17 15:27:09 -05001028
Jim Van Verth3af1af92017-05-18 15:06:54 -04001029 // transform light
1030 SkPoint3 devLightPos = map(viewMatrix, rec.fLightPos);
1031
1032 // 1/scale
1033 SkScalar devToSrcScale = viewMatrix.isScaleTranslate() ?
1034 SkScalarInvert(viewMatrix[SkMatrix::kMScaleX]) :
1035 sk_float_rsqrt(viewMatrix[SkMatrix::kMScaleX] * viewMatrix[SkMatrix::kMScaleX] +
1036 viewMatrix[SkMatrix::kMSkewX] * viewMatrix[SkMatrix::kMSkewX]);
1037
1038 SkScalar occluderHeight = rec.fZPlaneParams.fZ;
Brian Salomon05969092017-07-13 11:20:51 -04001039 GrColor4f color = GrColor4f::FromGrColor(color4ub);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001040 bool transparent = SkToBool(rec.fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag);
Jim Van Verth34d6e4b2017-06-09 11:09:03 -04001041 bool tonalColor = SkToBool(rec.fFlags & SkShadowFlags::kTonalColor_ShadowFlag);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001042
1043 if (rec.fAmbientAlpha > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001044 SkScalar devSpaceInsetWidth = SkDrawShadowMetrics::AmbientBlurRadius(occluderHeight);
1045 const SkScalar umbraRecipAlpha = SkDrawShadowMetrics::AmbientRecipAlpha(occluderHeight);
1046 const SkScalar devSpaceAmbientBlur = devSpaceInsetWidth * umbraRecipAlpha;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001047
1048 // Outset the shadow rrect to the border of the penumbra
1049 SkScalar ambientPathOutset = devSpaceInsetWidth * devToSrcScale;
1050 SkRRect ambientRRect;
1051 SkRect outsetRect = rrect.rect().makeOutset(ambientPathOutset, ambientPathOutset);
1052 // If the rrect was an oval then its outset will also be one.
1053 // We set it explicitly to avoid errors.
1054 if (rrect.isOval()) {
1055 ambientRRect = SkRRect::MakeOval(outsetRect);
1056 } else {
1057 SkScalar outsetRad = rrect.getSimpleRadii().fX + ambientPathOutset;
1058 ambientRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1059 }
1060
Jim Van Verth34d6e4b2017-06-09 11:09:03 -04001061 GrColor ambientColor;
1062 if (tonalColor) {
1063 // with tonal color, the color only applies to the spot shadow
1064 ambientColor = GrColorPackRGBA(0, 0, 0, 255.999f*rec.fAmbientAlpha);
1065 } else {
1066 ambientColor = color.mulByScalar(rec.fAmbientAlpha).toGrColor();
1067 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001068 if (transparent) {
1069 // set a large inset to force a fill
1070 devSpaceInsetWidth = ambientRRect.width();
1071 }
1072 // the fraction of the blur we want to apply is devSpaceInsetWidth/devSpaceAmbientBlur,
Jim Van Verth1af03d42017-07-31 09:34:58 -04001073 // which is just 1/umbraRecipAlpha.
1074 SkScalar blurClamp = SkScalarInvert(umbraRecipAlpha);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001075
Brian Salomon05969092017-07-13 11:20:51 -04001076 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(ambientColor, viewMatrix,
1077 ambientRRect,
1078 devSpaceAmbientBlur,
1079 devSpaceInsetWidth,
1080 blurClamp);
1081 SkASSERT(op);
1082 this->addDrawOp(clip, std::move(op));
Jim Van Verthc5903412016-11-17 15:27:09 -05001083 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001084
1085 if (rec.fSpotAlpha > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001086 SkScalar devSpaceSpotBlur;
1087 SkScalar spotScale;
1088 SkVector spotOffset;
1089 SkDrawShadowMetrics::GetSpotParams(occluderHeight, devLightPos.fX, devLightPos.fY,
1090 devLightPos.fZ, rec.fLightRadius,
1091 &devSpaceSpotBlur, &spotScale, &spotOffset);
1092 // handle scale of radius due to CTM
Jim Van Verth3af1af92017-05-18 15:06:54 -04001093 const SkScalar srcSpaceSpotBlur = devSpaceSpotBlur * devToSrcScale;
1094
Jim Van Verth3af1af92017-05-18 15:06:54 -04001095 // Adjust translate for the effect of the scale.
1096 spotOffset.fX += spotScale*viewMatrix[SkMatrix::kMTransX];
1097 spotOffset.fY += spotScale*viewMatrix[SkMatrix::kMTransY];
1098 // This offset is in dev space, need to transform it into source space.
1099 SkMatrix ctmInverse;
1100 if (viewMatrix.invert(&ctmInverse)) {
1101 ctmInverse.mapPoints(&spotOffset, 1);
1102 } else {
1103 // Since the matrix is a similarity, this should never happen, but just in case...
1104 SkDebugf("Matrix is degenerate. Will not render spot shadow correctly!\n");
1105 SkASSERT(false);
1106 }
1107
1108 // Compute the transformed shadow rrect
1109 SkRRect spotShadowRRect;
1110 SkMatrix shadowTransform;
1111 shadowTransform.setScaleTranslate(spotScale, spotScale, spotOffset.fX, spotOffset.fY);
1112 rrect.transform(shadowTransform, &spotShadowRRect);
1113 SkScalar spotRadius = spotShadowRRect.getSimpleRadii().fX;
1114
1115 // Compute the insetWidth
Jim Van Verth1af03d42017-07-31 09:34:58 -04001116 SkScalar blurOutset = srcSpaceSpotBlur;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001117 SkScalar insetWidth = blurOutset;
1118 if (transparent) {
1119 // If transparent, just do a fill
1120 insetWidth += spotShadowRRect.width();
1121 } else {
1122 // For shadows, instead of using a stroke we specify an inset from the penumbra
1123 // border. We want to extend this inset area so that it meets up with the caster
1124 // geometry. The inset geometry will by default already be inset by the blur width.
1125 //
1126 // We compare the min and max corners inset by the radius between the original
1127 // rrect and the shadow rrect. The distance between the two plus the difference
1128 // between the scaled radius and the original radius gives the distance from the
1129 // transformed shadow shape to the original shape in that corner. The max
1130 // of these gives the maximum distance we need to cover.
1131 //
1132 // Since we are outsetting by 1/2 the blur distance, we just add the maxOffset to
1133 // that to get the full insetWidth.
1134 SkScalar maxOffset;
1135 if (rrect.isRect()) {
1136 // Manhattan distance works better for rects
1137 maxOffset = SkTMax(SkTMax(SkTAbs(spotShadowRRect.rect().fLeft -
1138 rrect.rect().fLeft),
1139 SkTAbs(spotShadowRRect.rect().fTop -
1140 rrect.rect().fTop)),
1141 SkTMax(SkTAbs(spotShadowRRect.rect().fRight -
1142 rrect.rect().fRight),
1143 SkTAbs(spotShadowRRect.rect().fBottom -
1144 rrect.rect().fBottom)));
1145 } else {
1146 SkScalar dr = spotRadius - rrect.getSimpleRadii().fX;
1147 SkPoint upperLeftOffset = SkPoint::Make(spotShadowRRect.rect().fLeft -
1148 rrect.rect().fLeft + dr,
1149 spotShadowRRect.rect().fTop -
1150 rrect.rect().fTop + dr);
1151 SkPoint lowerRightOffset = SkPoint::Make(spotShadowRRect.rect().fRight -
1152 rrect.rect().fRight - dr,
1153 spotShadowRRect.rect().fBottom -
1154 rrect.rect().fBottom - dr);
1155 maxOffset = SkScalarSqrt(SkTMax(upperLeftOffset.lengthSqd(),
1156 lowerRightOffset.lengthSqd())) + dr;
1157 }
1158 insetWidth += maxOffset;
1159 }
1160
1161 // Outset the shadow rrect to the border of the penumbra
1162 SkRect outsetRect = spotShadowRRect.rect().makeOutset(blurOutset, blurOutset);
1163 if (spotShadowRRect.isOval()) {
1164 spotShadowRRect = SkRRect::MakeOval(outsetRect);
1165 } else {
1166 SkScalar outsetRad = spotRadius + blurOutset;
1167 spotShadowRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1168 }
1169
Jim Van Verth34d6e4b2017-06-09 11:09:03 -04001170 GrColor spotColor;
1171 if (tonalColor) {
1172 SkScalar colorScale;
1173 SkScalar tonalAlpha;
1174 SkShadowUtils::ComputeTonalColorParams(color.fRGBA[0], color.fRGBA[1],
1175 color.fRGBA[2], rec.fSpotAlpha,
1176 &colorScale, &tonalAlpha);
1177 color.fRGBA[0] *= colorScale;
1178 color.fRGBA[1] *= colorScale;
1179 color.fRGBA[2] *= colorScale;
1180 color.fRGBA[3] = tonalAlpha;
1181 spotColor = color.toGrColor();
1182 } else {
1183 spotColor = color.mulByScalar(rec.fSpotAlpha).toGrColor();
1184 }
1185
Brian Salomon05969092017-07-13 11:20:51 -04001186 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(spotColor, viewMatrix,
1187 spotShadowRRect,
Jim Van Verth1af03d42017-07-31 09:34:58 -04001188 2.0f * devSpaceSpotBlur,
Brian Salomon05969092017-07-13 11:20:51 -04001189 insetWidth);
1190 SkASSERT(op);
1191 this->addDrawOp(clip, std::move(op));
Jim Van Verth3af1af92017-05-18 15:06:54 -04001192 }
1193
1194 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001195}
1196
1197///////////////////////////////////////////////////////////////////////////////
1198
Brian Osman11052242016-10-27 14:47:55 -04001199bool GrRenderTargetContext::drawFilledDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001200 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001201 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001202 const SkMatrix& viewMatrix,
1203 const SkRRect& origOuter,
1204 const SkRRect& origInner) {
robertphillips00095892016-02-29 13:50:40 -08001205 SkASSERT(!origInner.isEmpty());
1206 SkASSERT(!origOuter.isEmpty());
1207
csmartdaltone0d36292016-07-29 08:14:20 -07001208 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport()) {
Robert Phillipse3302df2017-04-24 07:31:02 -04001209 gr_instanced::OpAllocator* oa = this->drawingManager()->instancingAllocator();
1210 std::unique_ptr<GrDrawOp> op(oa->recordDRRect(
Brian Salomon54d212e2017-03-21 14:22:38 -04001211 origOuter, origInner, viewMatrix, std::move(paint), aa, fInstancedPipelineInfo));
Brian Salomon42521e82016-12-07 16:44:58 -05001212 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -04001213 this->addDrawOp(clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -07001214 return true;
1215 }
1216 }
1217
Brian Salomon7c8460e2017-05-12 11:36:10 -04001218 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
robertphillips00095892016-02-29 13:50:40 -08001219
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001220 GrPrimitiveEdgeType innerEdgeType, outerEdgeType;
1221 if (GrAAType::kCoverage == aaType) {
1222 innerEdgeType = kInverseFillAA_GrProcessorEdgeType;
1223 outerEdgeType = kFillAA_GrProcessorEdgeType;
1224 } else {
1225 innerEdgeType = kInverseFillBW_GrProcessorEdgeType;
1226 outerEdgeType = kFillBW_GrProcessorEdgeType;
1227 }
robertphillips00095892016-02-29 13:50:40 -08001228
1229 SkTCopyOnFirstWrite<SkRRect> inner(origInner), outer(origOuter);
1230 SkMatrix inverseVM;
1231 if (!viewMatrix.isIdentity()) {
1232 if (!origInner.transform(viewMatrix, inner.writable())) {
1233 return false;
1234 }
1235 if (!origOuter.transform(viewMatrix, outer.writable())) {
1236 return false;
1237 }
1238 if (!viewMatrix.invert(&inverseVM)) {
1239 return false;
1240 }
1241 } else {
1242 inverseVM.reset();
halcanary9d524f22016-03-29 09:03:52 -07001243 }
robertphillips00095892016-02-29 13:50:40 -08001244
robertphillips00095892016-02-29 13:50:40 -08001245 // TODO these need to be a geometry processors
Brian Salomonaff329b2017-08-11 09:40:37 -04001246 std::unique_ptr<GrFragmentProcessor> innerEffect(GrRRectEffect::Make(innerEdgeType, *inner));
robertphillips00095892016-02-29 13:50:40 -08001247 if (!innerEffect) {
1248 return false;
1249 }
1250
Brian Salomonaff329b2017-08-11 09:40:37 -04001251 std::unique_ptr<GrFragmentProcessor> outerEffect(GrRRectEffect::Make(outerEdgeType, *outer));
robertphillips00095892016-02-29 13:50:40 -08001252 if (!outerEffect) {
1253 return false;
1254 }
1255
Brian Salomon82f44312017-01-11 13:42:54 -05001256 paint.addCoverageFragmentProcessor(std::move(innerEffect));
1257 paint.addCoverageFragmentProcessor(std::move(outerEffect));
robertphillips00095892016-02-29 13:50:40 -08001258
1259 SkRect bounds = outer->getBounds();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001260 if (GrAAType::kCoverage == aaType) {
robertphillips00095892016-02-29 13:50:40 -08001261 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1262 }
halcanary9d524f22016-03-29 09:03:52 -07001263
Brian Salomon82f44312017-01-11 13:42:54 -05001264 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), bounds,
1265 inverseVM);
robertphillips00095892016-02-29 13:50:40 -08001266 return true;
1267}
1268
Brian Osman11052242016-10-27 14:47:55 -04001269void GrRenderTargetContext::drawDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001270 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001271 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001272 const SkMatrix& viewMatrix,
1273 const SkRRect& outer,
1274 const SkRRect& inner) {
robertphillips00095892016-02-29 13:50:40 -08001275 ASSERT_SINGLE_OWNER
1276 RETURN_IF_ABANDONED
1277 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001278 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawDRRect", fContext);
robertphillips00095892016-02-29 13:50:40 -08001279
1280 SkASSERT(!outer.isEmpty());
1281 SkASSERT(!inner.isEmpty());
1282
Robert Phillips72152832017-01-25 17:31:35 -05001283 AutoCheckFlush acf(this->drawingManager());
robertphillips00095892016-02-29 13:50:40 -08001284
Brian Salomon82f44312017-01-11 13:42:54 -05001285 if (this->drawFilledDRRect(clip, std::move(paint), aa, viewMatrix, outer, inner)) {
robertphillips00095892016-02-29 13:50:40 -08001286 return;
1287 }
1288
1289 SkPath path;
1290 path.setIsVolatile(true);
1291 path.addRRect(inner);
1292 path.addRRect(outer);
1293 path.setFillType(SkPath::kEvenOdd_FillType);
1294
Brian Salomon82f44312017-01-11 13:42:54 -05001295 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, GrStyle::SimpleFill());
robertphillips00095892016-02-29 13:50:40 -08001296}
1297
robertphillipsea461502015-05-26 11:38:03 -07001298///////////////////////////////////////////////////////////////////////////////
1299
Brian Osman11052242016-10-27 14:47:55 -04001300void GrRenderTargetContext::drawRegion(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001301 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001302 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001303 const SkMatrix& viewMatrix,
1304 const SkRegion& region,
Stan Iliev73d8fd92017-08-02 15:36:24 -04001305 const GrStyle& style,
1306 const GrUserStencilSettings* ss) {
msarettcc319b92016-08-25 18:07:18 -07001307 ASSERT_SINGLE_OWNER
1308 RETURN_IF_ABANDONED
1309 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001310 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRegion", fContext);
msarettcc319b92016-08-25 18:07:18 -07001311
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001312 if (GrAA::kYes == aa) {
Brian Salomonfc527d22016-12-14 21:07:01 -05001313 // GrRegionOp performs no antialiasing but is much faster, so here we check the matrix
Brian Salomonc57c7c92016-12-06 14:47:34 -05001314 // to see whether aa is really required.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001315 if (!SkToBool(viewMatrix.getType() & ~(SkMatrix::kTranslate_Mask)) &&
Brian Salomon34169692017-08-28 15:32:01 -04001316 SkScalarIsInt(viewMatrix.getTranslateX()) &&
1317 SkScalarIsInt(viewMatrix.getTranslateY())) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001318 aa = GrAA::kNo;
1319 }
Brian Salomonc57c7c92016-12-06 14:47:34 -05001320 }
msarettcc319b92016-08-25 18:07:18 -07001321 bool complexStyle = !style.isSimpleFill();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001322 if (complexStyle || GrAA::kYes == aa) {
msarettcc319b92016-08-25 18:07:18 -07001323 SkPath path;
1324 region.getBoundaryPath(&path);
Brian Salomon82f44312017-01-11 13:42:54 -05001325 return this->drawPath(clip, std::move(paint), aa, viewMatrix, path, style);
msarettcc319b92016-08-25 18:07:18 -07001326 }
1327
Brian Salomonf0366322017-07-11 15:53:05 -04001328 GrAAType aaType = this->chooseAAType(GrAA::kNo, GrAllowMixedSamples::kNo);
Stan Iliev73d8fd92017-08-02 15:36:24 -04001329 std::unique_ptr<GrDrawOp> op = GrRegionOp::Make(std::move(paint), viewMatrix, region, aaType,
1330 ss);
Brian Salomonf0366322017-07-11 15:53:05 -04001331 this->addDrawOp(clip, std::move(op));
msarettcc319b92016-08-25 18:07:18 -07001332}
1333
Brian Osman11052242016-10-27 14:47:55 -04001334void GrRenderTargetContext::drawOval(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001335 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001336 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001337 const SkMatrix& viewMatrix,
1338 const SkRect& oval,
1339 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001340 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001341 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001342 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001343 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawOval", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -07001344
robertphillipsea461502015-05-26 11:38:03 -07001345 if (oval.isEmpty()) {
1346 return;
1347 }
1348
bsalomon6663acf2016-05-10 09:14:17 -07001349 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
robertphillipsea461502015-05-26 11:38:03 -07001350
Robert Phillips72152832017-01-25 17:31:35 -05001351 AutoCheckFlush acf(this->drawingManager());
bsalomon6663acf2016-05-10 09:14:17 -07001352 const SkStrokeRec& stroke = style.strokeRec();
csmartdaltona7f29642016-07-07 08:49:11 -07001353
csmartdaltone0d36292016-07-29 08:14:20 -07001354 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport() &&
1355 stroke.isFillStyle()) {
Robert Phillipse3302df2017-04-24 07:31:02 -04001356 gr_instanced::OpAllocator* oa = this->drawingManager()->instancingAllocator();
Brian Salomon54d212e2017-03-21 14:22:38 -04001357 std::unique_ptr<GrDrawOp> op(
Robert Phillipse3302df2017-04-24 07:31:02 -04001358 oa->recordOval(oval, viewMatrix, std::move(paint), aa, fInstancedPipelineInfo));
Brian Salomon42521e82016-12-07 16:44:58 -05001359 if (op) {
Brian Salomon54d212e2017-03-21 14:22:38 -04001360 this->addDrawOp(clip, std::move(op));
csmartdaltona7f29642016-07-07 08:49:11 -07001361 return;
1362 }
1363 }
1364
Brian Salomon7c8460e2017-05-12 11:36:10 -04001365 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001366 if (GrAAType::kCoverage == aaType) {
Brian Salomon94efbf52016-11-29 13:43:05 -05001367 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomon05441c42017-05-15 16:45:49 -04001368 std::unique_ptr<GrDrawOp> op =
1369 GrOvalOpFactory::MakeOvalOp(std::move(paint), viewMatrix, oval, stroke, shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -05001370 if (op) {
Brian Salomon05441c42017-05-15 16:45:49 -04001371 this->addDrawOp(clip, std::move(op));
robertphillipsb56f9272016-02-25 11:03:52 -08001372 return;
1373 }
robertphillipsea461502015-05-26 11:38:03 -07001374 }
robertphillipsb56f9272016-02-25 11:03:52 -08001375
1376 SkPath path;
1377 path.setIsVolatile(true);
1378 path.addOval(oval);
Brian Salomon82f44312017-01-11 13:42:54 -05001379 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, style);
robertphillipsea461502015-05-26 11:38:03 -07001380}
1381
Brian Osman11052242016-10-27 14:47:55 -04001382void GrRenderTargetContext::drawArc(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001383 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001384 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001385 const SkMatrix& viewMatrix,
1386 const SkRect& oval,
1387 SkScalar startAngle,
1388 SkScalar sweepAngle,
1389 bool useCenter,
1390 const GrStyle& style) {
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001391 ASSERT_SINGLE_OWNER
1392 RETURN_IF_ABANDONED
1393 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001394 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawArc", fContext);
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001395
1396 AutoCheckFlush acf(this->drawingManager());
1397
Brian Salomon7c8460e2017-05-12 11:36:10 -04001398 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001399 if (GrAAType::kCoverage == aaType) {
Brian Salomon94efbf52016-11-29 13:43:05 -05001400 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomon05441c42017-05-15 16:45:49 -04001401 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeArcOp(std::move(paint),
1402 viewMatrix,
1403 oval,
1404 startAngle,
1405 sweepAngle,
1406 useCenter,
1407 style,
1408 shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -05001409 if (op) {
Brian Salomon05441c42017-05-15 16:45:49 -04001410 this->addDrawOp(clip, std::move(op));
bsalomon4f3a0ca2016-08-22 13:14:26 -07001411 return;
1412 }
1413 }
1414 SkPath path;
bsalomon21af9ca2016-08-25 12:29:23 -07001415 SkPathPriv::CreateDrawArcPath(&path, oval, startAngle, sweepAngle, useCenter,
1416 style.isSimpleFill());
Brian Salomon82f44312017-01-11 13:42:54 -05001417 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, style);
bsalomon4f3a0ca2016-08-22 13:14:26 -07001418}
1419
Brian Osman11052242016-10-27 14:47:55 -04001420void GrRenderTargetContext::drawImageLattice(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001421 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001422 const SkMatrix& viewMatrix,
1423 int imageWidth,
1424 int imageHeight,
1425 std::unique_ptr<SkLatticeIter> iter,
1426 const SkRect& dst) {
joshualitt1de610a2016-01-06 08:26:09 -08001427 ASSERT_SINGLE_OWNER
joshualitt33a5fce2015-11-18 13:28:51 -08001428 RETURN_IF_ABANDONED
1429 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001430 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawImageLattice", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001431
Robert Phillips72152832017-01-25 17:31:35 -05001432 AutoCheckFlush acf(this->drawingManager());
joshualitt33a5fce2015-11-18 13:28:51 -08001433
Brian Salomon815486c2017-07-11 08:52:13 -04001434 std::unique_ptr<GrDrawOp> op = GrLatticeOp::MakeNonAA(std::move(paint), viewMatrix, imageWidth,
1435 imageHeight, std::move(iter), dst);
1436 this->addDrawOp(clip, std::move(op));
joshualitt33a5fce2015-11-18 13:28:51 -08001437}
1438
Greg Daniel51316782017-08-02 15:10:09 +00001439GrSemaphoresSubmitted GrRenderTargetContext::prepareForExternalIO(
1440 int numSemaphores, GrBackendSemaphore backendSemaphores[]) {
robertphillips8c523e02016-07-26 07:41:00 -07001441 ASSERT_SINGLE_OWNER
Greg Daniel51316782017-08-02 15:10:09 +00001442 if (this->drawingManager()->wasAbandoned()) { return GrSemaphoresSubmitted::kNo; }
robertphillips8c523e02016-07-26 07:41:00 -07001443 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001444 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "prepareForExternalIO", fContext);
robertphillips8c523e02016-07-26 07:41:00 -07001445
Greg Daniel51316782017-08-02 15:10:09 +00001446 return this->drawingManager()->prepareSurfaceForExternalIO(fRenderTargetProxy.get(),
1447 numSemaphores,
1448 backendSemaphores);
Greg Daniela5cb7812017-06-16 09:45:32 -04001449}
1450
Greg Danielc64ee462017-06-15 16:59:49 -04001451bool GrRenderTargetContext::waitOnSemaphores(int numSemaphores,
Greg Daniela5cb7812017-06-16 09:45:32 -04001452 const GrBackendSemaphore* waitSemaphores) {
1453 ASSERT_SINGLE_OWNER
Greg Danielc64ee462017-06-15 16:59:49 -04001454 RETURN_FALSE_IF_ABANDONED
Greg Daniela5cb7812017-06-16 09:45:32 -04001455 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001456 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "waitOnSemaphores", fContext);
Greg Daniela5cb7812017-06-16 09:45:32 -04001457
1458 AutoCheckFlush acf(this->drawingManager());
1459
Greg Danielc64ee462017-06-15 16:59:49 -04001460 if (numSemaphores && !this->caps()->fenceSyncSupport()) {
1461 return false;
1462 }
1463
Greg Daniela5cb7812017-06-16 09:45:32 -04001464 SkTArray<sk_sp<GrSemaphore>> semaphores(numSemaphores);
1465 for (int i = 0; i < numSemaphores; ++i) {
1466 sk_sp<GrSemaphore> sema = fContext->resourceProvider()->wrapBackendSemaphore(
1467 waitSemaphores[i], kAdopt_GrWrapOwnership);
1468 std::unique_ptr<GrOp> waitOp(GrSemaphoreOp::MakeWait(sema, fRenderTargetProxy.get()));
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001469 this->getRTOpList()->addOp(std::move(waitOp), *this->caps());
Greg Daniela5cb7812017-06-16 09:45:32 -04001470 }
Greg Danielc64ee462017-06-15 16:59:49 -04001471 return true;
robertphillips8c523e02016-07-26 07:41:00 -07001472}
joshualitt33a5fce2015-11-18 13:28:51 -08001473
Robert Phillips65a88fa2017-08-08 08:36:22 -04001474void GrRenderTargetContext::insertEventMarker(const SkString& str) {
1475 std::unique_ptr<GrOp> op(GrDebugMarkerOp::Make(fRenderTargetProxy.get(), str));
1476 this->getRTOpList()->addOp(std::move(op), *this->caps());
1477}
1478
1479
robertphillipsea461502015-05-26 11:38:03 -07001480// Can 'path' be drawn as a pair of filled nested rectangles?
bsalomon6663acf2016-05-10 09:14:17 -07001481static bool fills_as_nested_rects(const SkMatrix& viewMatrix, const SkPath& path, SkRect rects[2]) {
robertphillipsea461502015-05-26 11:38:03 -07001482
1483 if (path.isInverseFillType()) {
1484 return false;
1485 }
1486
1487 // TODO: this restriction could be lifted if we were willing to apply
1488 // the matrix to all the points individually rather than just to the rect
robertphillips0e7029e2015-11-30 05:45:06 -08001489 if (!viewMatrix.rectStaysRect()) {
robertphillipsea461502015-05-26 11:38:03 -07001490 return false;
1491 }
1492
1493 SkPath::Direction dirs[2];
1494 if (!path.isNestedFillRects(rects, dirs)) {
1495 return false;
1496 }
1497
1498 if (SkPath::kWinding_FillType == path.getFillType() && dirs[0] == dirs[1]) {
1499 // The two rects need to be wound opposite to each other
1500 return false;
1501 }
1502
1503 // Right now, nested rects where the margin is not the same width
1504 // all around do not render correctly
1505 const SkScalar* outer = rects[0].asScalars();
1506 const SkScalar* inner = rects[1].asScalars();
1507
1508 bool allEq = true;
1509
1510 SkScalar margin = SkScalarAbs(outer[0] - inner[0]);
1511 bool allGoE1 = margin >= SK_Scalar1;
1512
1513 for (int i = 1; i < 4; ++i) {
1514 SkScalar temp = SkScalarAbs(outer[i] - inner[i]);
1515 if (temp < SK_Scalar1) {
1516 allGoE1 = false;
1517 }
1518 if (!SkScalarNearlyEqual(margin, temp)) {
1519 allEq = false;
1520 }
1521 }
1522
1523 return allEq || allGoE1;
1524}
1525
Brian Osman11052242016-10-27 14:47:55 -04001526void GrRenderTargetContext::drawPath(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001527 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001528 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001529 const SkMatrix& viewMatrix,
1530 const SkPath& path,
1531 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001532 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001533 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001534 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001535 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawPath", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -07001536
robertphillipsea461502015-05-26 11:38:03 -07001537 if (path.isEmpty()) {
1538 if (path.isInverseFillType()) {
Brian Salomon82f44312017-01-11 13:42:54 -05001539 this->drawPaint(clip, std::move(paint), viewMatrix);
robertphillipsea461502015-05-26 11:38:03 -07001540 }
1541 return;
1542 }
1543
Robert Phillips72152832017-01-25 17:31:35 -05001544 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -07001545
Brian Salomon7c8460e2017-05-12 11:36:10 -04001546 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001547 if (GrAAType::kCoverage == aaType && !style.pathEffect()) {
bsalomon6663acf2016-05-10 09:14:17 -07001548 if (style.isSimpleFill() && !path.isConvex()) {
robertphillipsea461502015-05-26 11:38:03 -07001549 // Concave AA paths are expensive - try to avoid them for special cases
1550 SkRect rects[2];
1551
bsalomon6663acf2016-05-10 09:14:17 -07001552 if (fills_as_nested_rects(viewMatrix, path, rects)) {
Brian Salomonbaaf4392017-06-15 09:59:23 -04001553 std::unique_ptr<GrDrawOp> op =
1554 GrRectOpFactory::MakeAAFillNestedRects(std::move(paint), viewMatrix, rects);
Brian Salomon5f970fe2017-06-16 17:30:59 -04001555 if (op) {
1556 this->addDrawOp(clip, std::move(op));
bsalomon40ef4852016-05-02 13:22:13 -07001557 }
Brian Salomon5f970fe2017-06-16 17:30:59 -04001558 // A null return indicates that there is nothing to draw in this case.
1559 return;
robertphillipsea461502015-05-26 11:38:03 -07001560 }
1561 }
1562 SkRect ovalRect;
1563 bool isOval = path.isOval(&ovalRect);
1564
1565 if (isOval && !path.isInverseFillType()) {
Brian Salomon94efbf52016-11-29 13:43:05 -05001566 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
Brian Salomon05441c42017-05-15 16:45:49 -04001567 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeOvalOp(
1568 std::move(paint), viewMatrix, ovalRect, style.strokeRec(), shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -05001569 if (op) {
Brian Salomon05441c42017-05-15 16:45:49 -04001570 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -07001571 return;
1572 }
1573 }
1574 }
robertphillips4bc31812016-03-01 12:22:49 -08001575
1576 // Note that internalDrawPath may sw-rasterize the path into a scratch texture.
1577 // Scratch textures can be recycled after they are returned to the texture
1578 // cache. This presents a potential hazard for buffered drawing. However,
1579 // the writePixels that uploads to the scratch will perform a flush so we're
1580 // OK.
Brian Salomon82f44312017-01-11 13:42:54 -05001581 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, style);
robertphillipsea461502015-05-26 11:38:03 -07001582}
1583
Brian Osman11052242016-10-27 14:47:55 -04001584bool GrRenderTargetContextPriv::drawAndStencilPath(const GrClip& clip,
1585 const GrUserStencilSettings* ss,
1586 SkRegion::Op op,
1587 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001588 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001589 const SkMatrix& viewMatrix,
1590 const SkPath& path) {
robertphillips391395d2016-03-02 09:26:36 -08001591 ASSERT_SINGLE_OWNER_PRIV
1592 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -04001593 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001594 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawAndStencilPath",
1595 fRenderTargetContext->fContext);
robertphillips391395d2016-03-02 09:26:36 -08001596
1597 if (path.isEmpty() && path.isInverseFillType()) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001598 this->drawAndStencilRect(clip, ss, op, invert, GrAA::kNo, SkMatrix::I(),
Brian Osman11052242016-10-27 14:47:55 -04001599 SkRect::MakeIWH(fRenderTargetContext->width(),
1600 fRenderTargetContext->height()));
robertphillips391395d2016-03-02 09:26:36 -08001601 return true;
1602 }
1603
Robert Phillips72152832017-01-25 17:31:35 -05001604 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -08001605
1606 // An Assumption here is that path renderer would use some form of tweaking
1607 // the src color (either the input alpha or in the frag shader) to implement
1608 // aa. If we have some future driver-mojo path AA that can do the right
1609 // thing WRT to the blend then we'll need some query on the PR.
Brian Salomon7c8460e2017-05-12 11:36:10 -04001610 GrAAType aaType = fRenderTargetContext->chooseAAType(aa, GrAllowMixedSamples::kNo);
robertphillips976f5f02016-06-03 10:59:20 -07001611 bool hasUserStencilSettings = !ss->isUnused();
robertphillips391395d2016-03-02 09:26:36 -08001612
Chris Daltondb91c6e2017-09-08 16:25:08 -06001613 SkIRect clipConservativeBounds;
1614 clip.getConservativeBounds(fRenderTargetContext->width(), fRenderTargetContext->height(),
1615 &clipConservativeBounds, nullptr);
1616
bsalomon8acedde2016-06-24 10:42:16 -07001617 GrShape shape(path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -08001618 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Eric Karl5c779752017-05-08 12:02:07 -07001619 canDrawArgs.fCaps = fRenderTargetContext->drawingManager()->getContext()->caps();
robertphillips391395d2016-03-02 09:26:36 -08001620 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07001621 canDrawArgs.fShape = &shape;
Chris Daltondb91c6e2017-09-08 16:25:08 -06001622 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001623 canDrawArgs.fAAType = aaType;
cdalton93a379b2016-05-11 13:58:08 -07001624 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips391395d2016-03-02 09:26:36 -08001625
1626 // Don't allow the SW renderer
Robert Phillips72152832017-01-25 17:31:35 -05001627 GrPathRenderer* pr = fRenderTargetContext->drawingManager()->getPathRenderer(
Brian Salomon36aa1762016-12-10 13:24:02 -05001628 canDrawArgs, false, GrPathRendererChain::DrawType::kStencilAndColor);
robertphillips391395d2016-03-02 09:26:36 -08001629 if (!pr) {
1630 return false;
1631 }
1632
1633 GrPaint paint;
1634 paint.setCoverageSetOpXPFactory(op, invert);
1635
Brian Salomon82f44312017-01-11 13:42:54 -05001636 GrPathRenderer::DrawPathArgs args{
Robert Phillips256c37b2017-03-01 14:32:46 -05001637 fRenderTargetContext->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05001638 std::move(paint),
1639 ss,
1640 fRenderTargetContext,
1641 &clip,
Chris Daltondb91c6e2017-09-08 16:25:08 -06001642 &clipConservativeBounds,
Brian Salomon82f44312017-01-11 13:42:54 -05001643 &viewMatrix,
1644 &shape,
1645 aaType,
1646 fRenderTargetContext->isGammaCorrect()};
robertphillips391395d2016-03-02 09:26:36 -08001647 pr->drawPath(args);
1648 return true;
1649}
1650
Brian Osman11052242016-10-27 14:47:55 -04001651SkBudgeted GrRenderTargetContextPriv::isBudgeted() const {
robertphillips714712b2016-08-04 06:20:45 -07001652 ASSERT_SINGLE_OWNER_PRIV
1653
Brian Osman11052242016-10-27 14:47:55 -04001654 if (fRenderTargetContext->wasAbandoned()) {
robertphillips714712b2016-08-04 06:20:45 -07001655 return SkBudgeted::kNo;
1656 }
1657
Brian Osman11052242016-10-27 14:47:55 -04001658 SkDEBUGCODE(fRenderTargetContext->validate();)
robertphillips714712b2016-08-04 06:20:45 -07001659
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001660 return fRenderTargetContext->fRenderTargetProxy->isBudgeted();
robertphillips714712b2016-08-04 06:20:45 -07001661}
1662
Brian Osman11052242016-10-27 14:47:55 -04001663void GrRenderTargetContext::internalDrawPath(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001664 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001665 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001666 const SkMatrix& viewMatrix,
1667 const SkPath& path,
1668 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001669 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001670 RETURN_IF_ABANDONED
Brian Salomondcbb9d92017-07-19 10:53:20 -04001671 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "internalDrawPath", fContext);
1672
Chris Daltondb91c6e2017-09-08 16:25:08 -06001673 SkIRect clipConservativeBounds;
1674 clip.getConservativeBounds(this->width(), this->height(), &clipConservativeBounds, nullptr);
1675
bsalomon8acedde2016-06-24 10:42:16 -07001676 SkASSERT(!path.isEmpty());
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001677 GrShape shape;
Brian Salomon7c8460e2017-05-12 11:36:10 -04001678 // NVPR cannot handle hairlines, so this would get picked up by a different stencil and
1679 // cover path renderer (i.e. default path renderer). The hairline renderer produces much
1680 // smoother hairlines than MSAA.
1681 GrAllowMixedSamples allowMixedSamples =
1682 style.isSimpleHairline() ? GrAllowMixedSamples::kNo : GrAllowMixedSamples::kYes;
1683 GrAAType aaType = this->chooseAAType(aa, allowMixedSamples);
robertphillips68737822015-10-29 12:12:21 -07001684 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Eric Karl5c779752017-05-08 12:02:07 -07001685 canDrawArgs.fCaps = this->drawingManager()->getContext()->caps();
robertphillips68737822015-10-29 12:12:21 -07001686 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07001687 canDrawArgs.fShape = &shape;
Chris Daltondb91c6e2017-09-08 16:25:08 -06001688 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001689 canDrawArgs.fHasUserStencilSettings = false;
robertphillips68737822015-10-29 12:12:21 -07001690
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001691 GrPathRenderer* pr;
Brian Salomon82125e92016-12-10 09:35:48 -05001692 static constexpr GrPathRendererChain::DrawType kType = GrPathRendererChain::DrawType::kColor;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001693 do {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001694 shape = GrShape(path, style);
Brian Salomon085c0862017-08-31 15:44:51 -04001695 if (shape.isEmpty() && !shape.inverseFilled()) {
robertphillipsea461502015-05-26 11:38:03 -07001696 return;
1697 }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001698
1699 canDrawArgs.fAAType = aaType;
1700
1701 // Try a 1st time without applying any of the style to the geometry (and barring sw)
Robert Phillips72152832017-01-25 17:31:35 -05001702 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001703 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
1704
1705 if (!pr && shape.style().pathEffect()) {
1706 // It didn't work above, so try again with the path effect applied.
1707 shape = shape.applyStyle(GrStyle::Apply::kPathEffectOnly, styleScale);
bsalomon8acedde2016-06-24 10:42:16 -07001708 if (shape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001709 return;
1710 }
Robert Phillips72152832017-01-25 17:31:35 -05001711 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
bsalomon6663acf2016-05-10 09:14:17 -07001712 }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001713 if (!pr) {
1714 if (shape.style().applies()) {
1715 shape = shape.applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec, styleScale);
1716 if (shape.isEmpty()) {
1717 return;
1718 }
1719 }
1720 // This time, allow SW renderer
Robert Phillips72152832017-01-25 17:31:35 -05001721 pr = this->drawingManager()->getPathRenderer(canDrawArgs, true, kType);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001722 }
Brian Salomon0abc8b42016-12-13 10:22:54 -05001723 if (!pr && GrAATypeIsHW(aaType)) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001724 // There are exceptional cases where we may wind up falling back to coverage based AA
1725 // when the target is MSAA (e.g. through disabling path renderers via GrContextOptions).
1726 aaType = GrAAType::kCoverage;
1727 } else {
1728 break;
1729 }
1730 } while(true);
robertphillipsea461502015-05-26 11:38:03 -07001731
bsalomon8acedde2016-06-24 10:42:16 -07001732 if (!pr) {
robertphillipsea461502015-05-26 11:38:03 -07001733#ifdef SK_DEBUG
1734 SkDebugf("Unable to find path renderer compatible with path.\n");
1735#endif
1736 return;
1737 }
1738
Robert Phillips256c37b2017-03-01 14:32:46 -05001739 GrPathRenderer::DrawPathArgs args{this->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05001740 std::move(paint),
1741 &GrUserStencilSettings::kUnused,
1742 this,
1743 &clip,
Chris Daltondb91c6e2017-09-08 16:25:08 -06001744 &clipConservativeBounds,
Brian Salomon82f44312017-01-11 13:42:54 -05001745 &viewMatrix,
1746 &shape,
1747 aaType,
1748 this->isGammaCorrect()};
bsalomon0aff2fa2015-07-31 06:48:27 -07001749 pr->drawPath(args);
robertphillipsea461502015-05-26 11:38:03 -07001750}
1751
Brian Salomon467921e2017-03-06 16:17:12 -05001752static void op_bounds(SkRect* bounds, const GrOp* op) {
1753 *bounds = op->bounds();
1754 if (op->hasZeroArea()) {
1755 if (op->hasAABloat()) {
1756 bounds->outset(0.5f, 0.5f);
1757 } else {
1758 // We don't know which way the particular GPU will snap lines or points at integer
1759 // coords. So we ensure that the bounds is large enough for either snap.
1760 SkRect before = *bounds;
1761 bounds->roundOut(bounds);
1762 if (bounds->fLeft == before.fLeft) {
1763 bounds->fLeft -= 1;
1764 }
1765 if (bounds->fTop == before.fTop) {
1766 bounds->fTop -= 1;
1767 }
1768 if (bounds->fRight == before.fRight) {
1769 bounds->fRight += 1;
1770 }
1771 if (bounds->fBottom == before.fBottom) {
1772 bounds->fBottom += 1;
1773 }
1774 }
1775 }
1776}
1777
Brian Salomon54d212e2017-03-21 14:22:38 -04001778uint32_t GrRenderTargetContext::addDrawOp(const GrClip& clip, std::unique_ptr<GrDrawOp> op) {
joshualitt1de610a2016-01-06 08:26:09 -08001779 ASSERT_SINGLE_OWNER
Robert Phillipsc0138922017-03-08 11:50:55 -05001780 if (this->drawingManager()->wasAbandoned()) {
1781 return SK_InvalidUniqueID;
1782 }
robertphillips2e1e51f2015-10-15 08:01:48 -07001783 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001784 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "addDrawOp", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -07001785
Brian Salomon467921e2017-03-06 16:17:12 -05001786 // Setup clip
1787 SkRect bounds;
1788 op_bounds(&bounds, op.get());
Brian Salomon97180af2017-03-14 13:42:58 -04001789 GrAppliedClip appliedClip;
Brian Salomon54d212e2017-03-21 14:22:38 -04001790 GrDrawOp::FixedFunctionFlags fixedFunctionFlags = op->fixedFunctionFlags();
1791 if (!clip.apply(fContext, this, fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesHWAA,
1792 fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil, &appliedClip,
1793 &bounds)) {
1794 return SK_InvalidUniqueID;
1795 }
1796
Brian Salomon54d212e2017-03-21 14:22:38 -04001797 if (fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil ||
1798 appliedClip.hasStencilClip()) {
Robert Phillips6b47c7d2017-08-29 07:24:09 -04001799 this->getOpList()->setStencilLoadOp(GrLoadOp::kClear);
Robert Phillips95214472017-08-08 18:00:03 -04001800
Robert Phillips65048132017-08-10 08:44:49 -04001801 this->setNeedsStencil();
Brian Salomon54d212e2017-03-21 14:22:38 -04001802 }
1803
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001804 GrXferProcessor::DstProxy dstProxy;
Brian Salomonf86d37b2017-06-16 10:04:34 -04001805 if (op->finalize(*this->caps(), &appliedClip) == GrDrawOp::RequiresDstTexture::kYes) {
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001806 if (!this->setupDstProxy(this->asRenderTargetProxy(), clip, op->bounds(), &dstProxy)) {
Brian Salomon54d212e2017-03-21 14:22:38 -04001807 return SK_InvalidUniqueID;
1808 }
1809 }
1810
1811 op->setClippedBounds(bounds);
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001812 return this->getRTOpList()->addOp(std::move(op), *this->caps(),
1813 std::move(appliedClip), dstProxy);
Brian Salomon54d212e2017-03-21 14:22:38 -04001814}
1815
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001816bool GrRenderTargetContext::setupDstProxy(GrRenderTargetProxy* rtProxy, const GrClip& clip,
Robert Phillips16d8ec62017-07-27 16:16:25 -04001817 const SkRect& opBounds,
1818 GrXferProcessor::DstProxy* dstProxy) {
Brian Salomon467921e2017-03-06 16:17:12 -05001819 if (this->caps()->textureBarrierSupport()) {
Robert Phillipsbf25d432017-04-07 10:08:53 -04001820 if (GrTextureProxy* texProxy = rtProxy->asTextureProxy()) {
Brian Salomon467921e2017-03-06 16:17:12 -05001821 // The render target is a texture, so we can read from it directly in the shader. The XP
1822 // will be responsible to detect this situation and request a texture barrier.
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001823 dstProxy->setProxy(sk_ref_sp(texProxy));
1824 dstProxy->setOffset(0, 0);
Robert Phillipsbf25d432017-04-07 10:08:53 -04001825 return true;
Brian Salomon467921e2017-03-06 16:17:12 -05001826 }
1827 }
1828
Robert Phillipsbf25d432017-04-07 10:08:53 -04001829 SkIRect copyRect = SkIRect::MakeWH(rtProxy->width(), rtProxy->height());
Brian Salomon467921e2017-03-06 16:17:12 -05001830
Eric Karl74480882017-04-03 14:49:05 -07001831 SkIRect clippedRect;
Robert Phillipsbf25d432017-04-07 10:08:53 -04001832 clip.getConservativeBounds(rtProxy->width(), rtProxy->height(), &clippedRect);
Eric Karl72e551e2017-04-04 13:42:10 -07001833 SkIRect drawIBounds;
Brian Salomon467921e2017-03-06 16:17:12 -05001834 opBounds.roundOut(&drawIBounds);
Brian Salomon859621f2017-03-16 09:21:54 -04001835 // Cover up for any precision issues by outsetting the op bounds a pixel in each direction.
1836 drawIBounds.outset(1, 1);
Eric Karl72e551e2017-04-04 13:42:10 -07001837 if (!clippedRect.intersect(drawIBounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -05001838#ifdef SK_DEBUG
Robert Phillipsbf25d432017-04-07 10:08:53 -04001839 GrCapsDebugf(this->caps(), "setupDstTexture: Missed an early reject bailing on draw.");
Brian Salomon467921e2017-03-06 16:17:12 -05001840#endif
Robert Phillipsbf25d432017-04-07 10:08:53 -04001841 return false;
Brian Salomon467921e2017-03-06 16:17:12 -05001842 }
1843
1844 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
1845 // have per-sample dst values by making the copy multisampled.
1846 GrSurfaceDesc desc;
Eric Karl74480882017-04-03 14:49:05 -07001847 bool rectsMustMatch = false;
1848 bool disallowSubrect = false;
Robert Phillipsbf25d432017-04-07 10:08:53 -04001849 if (!this->caps()->initDescForDstCopy(rtProxy, &desc, &rectsMustMatch, &disallowSubrect)) {
Brian Salomon467921e2017-03-06 16:17:12 -05001850 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillips16d8ec62017-07-27 16:16:25 -04001851 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
Robert Phillipsbf25d432017-04-07 10:08:53 -04001852 desc.fConfig = rtProxy->config();
Brian Salomon467921e2017-03-06 16:17:12 -05001853 }
1854
Eric Karl74480882017-04-03 14:49:05 -07001855 if (!disallowSubrect) {
1856 copyRect = clippedRect;
1857 }
Brian Salomon467921e2017-03-06 16:17:12 -05001858
Robert Phillipsbf25d432017-04-07 10:08:53 -04001859 SkIPoint dstPoint, dstOffset;
1860 SkBackingFit fit;
Eric Karl74480882017-04-03 14:49:05 -07001861 if (rectsMustMatch) {
Robert Phillipsbf25d432017-04-07 10:08:53 -04001862 SkASSERT(desc.fOrigin == rtProxy->origin());
1863 desc.fWidth = rtProxy->width();
1864 desc.fHeight = rtProxy->height();
Eric Karl74480882017-04-03 14:49:05 -07001865 dstPoint = {copyRect.fLeft, copyRect.fTop};
1866 dstOffset = {0, 0};
Robert Phillipsbf25d432017-04-07 10:08:53 -04001867 fit = SkBackingFit::kExact;
Eric Karl74480882017-04-03 14:49:05 -07001868 } else {
1869 desc.fWidth = copyRect.width();
1870 desc.fHeight = copyRect.height();
1871 dstPoint = {0, 0};
1872 dstOffset = {copyRect.fLeft, copyRect.fTop};
Robert Phillipsbf25d432017-04-07 10:08:53 -04001873 fit = SkBackingFit::kApprox;
Eric Karl74480882017-04-03 14:49:05 -07001874 }
Brian Salomon467921e2017-03-06 16:17:12 -05001875
Robert Phillipsbf25d432017-04-07 10:08:53 -04001876 sk_sp<GrSurfaceContext> sContext = fContext->contextPriv().makeDeferredSurfaceContext(
1877 desc,
1878 fit,
1879 SkBudgeted::kYes);
1880 if (!sContext) {
1881 SkDebugf("setupDstTexture: surfaceContext creation failed.\n");
1882 return false;
1883 }
1884
1885 if (!sContext->copy(rtProxy, copyRect, dstPoint)) {
1886 SkDebugf("setupDstTexture: copy failed.\n");
1887 return false;
1888 }
1889
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001890 dstProxy->setProxy(sContext->asTextureProxyRef());
1891 dstProxy->setOffset(dstOffset);
Robert Phillipsbf25d432017-04-07 10:08:53 -04001892 return true;
robertphillips2334fb62015-06-17 05:43:33 -07001893}