blob: ec3681dfcf631582101f6363f916c3cf82efac3f [file] [log] [blame]
Chris Dalton1a325d22017-07-14 15:17:41 -06001/*
2 * Copyright 2017 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
8#include "GrCoverageCountingPathRenderer.h"
9
10#include "GrCaps.h"
11#include "GrClip.h"
12#include "GrGpu.h"
13#include "GrGpuCommandBuffer.h"
Chris Dalton383a2ef2018-01-08 17:21:41 -050014#include "GrOpFlushState.h"
Robert Phillips777707b2018-01-17 11:40:14 -050015#include "GrProxyProvider.h"
Chris Dalton383a2ef2018-01-08 17:21:41 -050016#include "GrRenderTargetOpList.h"
17#include "GrStyle.h"
18#include "GrTexture.h"
Chris Dalton1a325d22017-07-14 15:17:41 -060019#include "SkMakeUnique.h"
20#include "SkMatrix.h"
Chris Daltona039d3b2017-09-28 11:16:36 -060021#include "SkPathOps.h"
Chris Dalton383a2ef2018-01-08 17:21:41 -050022#include "ccpr/GrCCClipProcessor.h"
Chris Dalton1a325d22017-07-14 15:17:41 -060023
Chris Daltona32a3c32017-12-05 10:05:21 -070024// Shorthand for keeping line lengths under control with nested classes...
25using CCPR = GrCoverageCountingPathRenderer;
26
27// If a path spans more pixels than this, we need to crop it or else analytic AA can run out of fp32
28// precision.
29static constexpr float kPathCropThreshold = 1 << 16;
30
31static void crop_path(const SkPath& path, const SkIRect& cropbox, SkPath* out) {
32 SkPath cropPath;
33 cropPath.addRect(SkRect::Make(cropbox));
34 if (!Op(cropPath, path, kIntersect_SkPathOp, out)) {
35 // This can fail if the PathOps encounter NaN or infinities.
36 out->reset();
37 }
38}
Chris Dalton1a325d22017-07-14 15:17:41 -060039
40bool GrCoverageCountingPathRenderer::IsSupported(const GrCaps& caps) {
41 const GrShaderCaps& shaderCaps = *caps.shaderCaps();
Chris Dalton383a2ef2018-01-08 17:21:41 -050042 return shaderCaps.integerSupport() && shaderCaps.flatInterpolationSupport() &&
43 caps.instanceAttribSupport() && GrCaps::kNone_MapFlags != caps.mapBufferFlags() &&
Chris Dalton1a325d22017-07-14 15:17:41 -060044 caps.isConfigTexturable(kAlpha_half_GrPixelConfig) &&
Brian Salomonbdecacf2018-02-02 20:32:49 -050045 caps.isConfigRenderable(kAlpha_half_GrPixelConfig) &&
Chris Daltone4679fa2017-09-29 13:58:26 -060046 !caps.blacklistCoverageCounting();
Chris Dalton1a325d22017-07-14 15:17:41 -060047}
48
Chris Dalton383a2ef2018-01-08 17:21:41 -050049sk_sp<GrCoverageCountingPathRenderer> GrCoverageCountingPathRenderer::CreateIfSupported(
50 const GrCaps& caps, bool drawCachablePaths) {
Chris Daltona2ac30d2017-10-17 10:40:01 -060051 auto ccpr = IsSupported(caps) ? new GrCoverageCountingPathRenderer(drawCachablePaths) : nullptr;
52 return sk_sp<GrCoverageCountingPathRenderer>(ccpr);
Chris Dalton1a325d22017-07-14 15:17:41 -060053}
54
Chris Dalton383a2ef2018-01-08 17:21:41 -050055GrPathRenderer::CanDrawPath GrCoverageCountingPathRenderer::onCanDrawPath(
56 const CanDrawPathArgs& args) const {
Chris Daltona2ac30d2017-10-17 10:40:01 -060057 if (args.fShape->hasUnstyledKey() && !fDrawCachablePaths) {
58 return CanDrawPath::kNo;
59 }
60
Chris Dalton383a2ef2018-01-08 17:21:41 -050061 if (!args.fShape->style().isSimpleFill() || args.fShape->inverseFilled() ||
62 args.fViewMatrix->hasPerspective() || GrAAType::kCoverage != args.fAAType) {
Chris Dalton5ed44232017-09-07 13:22:46 -060063 return CanDrawPath::kNo;
Chris Dalton1a325d22017-07-14 15:17:41 -060064 }
65
66 SkPath path;
67 args.fShape->asPath(&path);
Chris Dalton5ed44232017-09-07 13:22:46 -060068 if (SkPathPriv::ConicWeightCnt(path)) {
69 return CanDrawPath::kNo;
70 }
71
Chris Daltondb91c6e2017-09-08 16:25:08 -060072 SkRect devBounds;
73 SkIRect devIBounds;
74 args.fViewMatrix->mapRect(&devBounds, path.getBounds());
75 devBounds.roundOut(&devIBounds);
76 if (!devIBounds.intersect(*args.fClipConservativeBounds)) {
77 // Path is completely clipped away. Our code will eventually notice this before doing any
78 // real work.
79 return CanDrawPath::kYes;
80 }
81
82 if (devIBounds.height() * devIBounds.width() > 256 * 256) {
83 // Large paths can blow up the atlas fast. And they are not ideal for a two-pass rendering
84 // algorithm. Give the simpler direct renderers a chance before we commit to drawing it.
85 return CanDrawPath::kAsBackup;
86 }
87
88 if (args.fShape->hasUnstyledKey() && path.countVerbs() > 50) {
89 // Complex paths do better cached in an SDF, if the renderer will accept them.
90 return CanDrawPath::kAsBackup;
91 }
92
Chris Dalton5ed44232017-09-07 13:22:46 -060093 return CanDrawPath::kYes;
Chris Dalton1a325d22017-07-14 15:17:41 -060094}
95
96bool GrCoverageCountingPathRenderer::onDrawPath(const DrawPathArgs& args) {
97 SkASSERT(!fFlushing);
Chris Dalton1a325d22017-07-14 15:17:41 -060098 auto op = skstd::make_unique<DrawPathsOp>(this, args, args.fPaint.getColor());
99 args.fRenderTargetContext->addDrawOp(*args.fClip, std::move(op));
Chris Dalton1a325d22017-07-14 15:17:41 -0600100 return true;
101}
102
Chris Daltona32a3c32017-12-05 10:05:21 -0700103CCPR::DrawPathsOp::DrawPathsOp(GrCoverageCountingPathRenderer* ccpr, const DrawPathArgs& args,
104 GrColor color)
Chris Dalton1a325d22017-07-14 15:17:41 -0600105 : INHERITED(ClassID())
106 , fCCPR(ccpr)
107 , fSRGBFlags(GrPipeline::SRGBFlagsFromPaint(args.fPaint))
108 , fProcessors(std::move(args.fPaint))
109 , fTailDraw(&fHeadDraw)
Chris Daltona32a3c32017-12-05 10:05:21 -0700110 , fOwningRTPendingPaths(nullptr) {
Chris Dalton080baa42017-11-06 14:19:19 -0700111 SkDEBUGCODE(++fCCPR->fPendingDrawOpsCount);
Chris Dalton1a325d22017-07-14 15:17:41 -0600112 SkDEBUGCODE(fBaseInstance = -1);
Chris Dalton383a2ef2018-01-08 17:21:41 -0500113 SkDEBUGCODE(fInstanceCount = 1);
114 SkDEBUGCODE(fNumSkippedInstances = 0);
Chris Dalton1a325d22017-07-14 15:17:41 -0600115 GrRenderTargetContext* const rtc = args.fRenderTargetContext;
116
117 SkRect devBounds;
118 args.fViewMatrix->mapRect(&devBounds, args.fShape->bounds());
Chris Daltonc9c97b72017-11-27 15:34:26 -0700119 args.fClip->getConservativeBounds(rtc->width(), rtc->height(), &fHeadDraw.fClipIBounds,
120 nullptr);
Chris Daltona32a3c32017-12-05 10:05:21 -0700121 if (SkTMax(devBounds.height(), devBounds.width()) > kPathCropThreshold) {
122 // The path is too large. We need to crop it or analytic AA can run out of fp32 precision.
123 SkPath path;
Chris Daltona039d3b2017-09-28 11:16:36 -0600124 args.fShape->asPath(&path);
125 path.transform(*args.fViewMatrix);
126 fHeadDraw.fMatrix.setIdentity();
Chris Daltona32a3c32017-12-05 10:05:21 -0700127 crop_path(path, fHeadDraw.fClipIBounds, &fHeadDraw.fPath);
Chris Daltona039d3b2017-09-28 11:16:36 -0600128 devBounds = fHeadDraw.fPath.getBounds();
Chris Daltona039d3b2017-09-28 11:16:36 -0600129 } else {
130 fHeadDraw.fMatrix = *args.fViewMatrix;
131 args.fShape->asPath(&fHeadDraw.fPath);
Chris Daltona039d3b2017-09-28 11:16:36 -0600132 }
Chris Dalton383a2ef2018-01-08 17:21:41 -0500133 fHeadDraw.fColor = color; // Can't call args.fPaint.getColor() because it has been std::move'd.
Chris Dalton1a325d22017-07-14 15:17:41 -0600134
135 // FIXME: intersect with clip bounds to (hopefully) improve batching.
136 // (This is nontrivial due to assumptions in generating the octagon cover geometry.)
137 this->setBounds(devBounds, GrOp::HasAABloat::kYes, GrOp::IsZeroArea::kNo);
138}
139
Chris Daltona32a3c32017-12-05 10:05:21 -0700140CCPR::DrawPathsOp::~DrawPathsOp() {
141 if (fOwningRTPendingPaths) {
Chris Dalton080baa42017-11-06 14:19:19 -0700142 // Remove CCPR's dangling pointer to this Op before deleting it.
Chris Daltona32a3c32017-12-05 10:05:21 -0700143 fOwningRTPendingPaths->fDrawOps.remove(this);
Chris Dalton080baa42017-11-06 14:19:19 -0700144 }
145 SkDEBUGCODE(--fCCPR->fPendingDrawOpsCount);
146}
147
Chris Daltona32a3c32017-12-05 10:05:21 -0700148GrDrawOp::RequiresDstTexture CCPR::DrawPathsOp::finalize(const GrCaps& caps,
149 const GrAppliedClip* clip,
150 GrPixelConfigIsClamped dstIsClamped) {
Chris Dalton080baa42017-11-06 14:19:19 -0700151 SkASSERT(!fCCPR->fFlushing);
152 // There should only be one single path draw in this Op right now.
Chris Daltona32a3c32017-12-05 10:05:21 -0700153 SkASSERT(1 == fInstanceCount);
Chris Dalton080baa42017-11-06 14:19:19 -0700154 SkASSERT(&fHeadDraw == fTailDraw);
Chris Dalton383a2ef2018-01-08 17:21:41 -0500155 GrProcessorSet::Analysis analysis =
156 fProcessors.finalize(fHeadDraw.fColor, GrProcessorAnalysisCoverage::kSingleChannel,
157 clip, false, caps, dstIsClamped, &fHeadDraw.fColor);
Chris Dalton1a325d22017-07-14 15:17:41 -0600158 return analysis.requiresDstTexture() ? RequiresDstTexture::kYes : RequiresDstTexture::kNo;
159}
160
Chris Daltona32a3c32017-12-05 10:05:21 -0700161bool CCPR::DrawPathsOp::onCombineIfPossible(GrOp* op, const GrCaps& caps) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600162 DrawPathsOp* that = op->cast<DrawPathsOp>();
163 SkASSERT(fCCPR == that->fCCPR);
Chris Dalton080baa42017-11-06 14:19:19 -0700164 SkASSERT(!fCCPR->fFlushing);
Chris Daltona32a3c32017-12-05 10:05:21 -0700165 SkASSERT(fOwningRTPendingPaths);
166 SkASSERT(fInstanceCount);
167 SkASSERT(!that->fOwningRTPendingPaths || that->fOwningRTPendingPaths == fOwningRTPendingPaths);
168 SkASSERT(that->fInstanceCount);
Chris Dalton1a325d22017-07-14 15:17:41 -0600169
Chris Dalton383a2ef2018-01-08 17:21:41 -0500170 if (this->getFillType() != that->getFillType() || fSRGBFlags != that->fSRGBFlags ||
Chris Dalton1a325d22017-07-14 15:17:41 -0600171 fProcessors != that->fProcessors) {
172 return false;
173 }
174
Chris Daltona32a3c32017-12-05 10:05:21 -0700175 fTailDraw->fNext = &fOwningRTPendingPaths->fDrawsAllocator.push_back(that->fHeadDraw);
Chris Dalton080baa42017-11-06 14:19:19 -0700176 fTailDraw = (that->fTailDraw == &that->fHeadDraw) ? fTailDraw->fNext : that->fTailDraw;
Chris Dalton1a325d22017-07-14 15:17:41 -0600177
178 this->joinBounds(*that);
179
Chris Dalton383a2ef2018-01-08 17:21:41 -0500180 SkDEBUGCODE(fInstanceCount += that->fInstanceCount);
Chris Daltona32a3c32017-12-05 10:05:21 -0700181 SkDEBUGCODE(that->fInstanceCount = 0);
Chris Dalton1a325d22017-07-14 15:17:41 -0600182 return true;
183}
184
Chris Daltona32a3c32017-12-05 10:05:21 -0700185void CCPR::DrawPathsOp::wasRecorded(GrRenderTargetOpList* opList) {
Chris Dalton080baa42017-11-06 14:19:19 -0700186 SkASSERT(!fCCPR->fFlushing);
Chris Daltona32a3c32017-12-05 10:05:21 -0700187 SkASSERT(!fOwningRTPendingPaths);
188 fOwningRTPendingPaths = &fCCPR->fRTPendingPathsMap[opList->uniqueID()];
189 fOwningRTPendingPaths->fDrawOps.addToTail(this);
190}
191
192bool GrCoverageCountingPathRenderer::canMakeClipProcessor(const SkPath& deviceSpacePath) const {
193 if (!fDrawCachablePaths && !deviceSpacePath.isVolatile()) {
194 return false;
195 }
196
197 if (SkPathPriv::ConicWeightCnt(deviceSpacePath)) {
198 return false;
199 }
200
201 return true;
202}
203
Chris Dalton383a2ef2018-01-08 17:21:41 -0500204std::unique_ptr<GrFragmentProcessor> GrCoverageCountingPathRenderer::makeClipProcessor(
Robert Phillips777707b2018-01-17 11:40:14 -0500205 GrProxyProvider* proxyProvider,
206 uint32_t opListID, const SkPath& deviceSpacePath, const SkIRect& accessRect,
207 int rtWidth, int rtHeight) {
Chris Dalton383a2ef2018-01-08 17:21:41 -0500208 using MustCheckBounds = GrCCClipProcessor::MustCheckBounds;
Chris Daltona32a3c32017-12-05 10:05:21 -0700209
210 SkASSERT(!fFlushing);
211 SkASSERT(this->canMakeClipProcessor(deviceSpacePath));
212
213 ClipPath& clipPath = fRTPendingPathsMap[opListID].fClipPaths[deviceSpacePath.getGenerationID()];
214 if (clipPath.isUninitialized()) {
215 // This ClipPath was just created during lookup. Initialize it.
Robert Phillips777707b2018-01-17 11:40:14 -0500216 clipPath.init(proxyProvider, deviceSpacePath, accessRect, rtWidth, rtHeight);
Chris Daltona32a3c32017-12-05 10:05:21 -0700217 } else {
218 clipPath.addAccess(accessRect);
219 }
220
221 bool mustCheckBounds = !clipPath.pathDevIBounds().contains(accessRect);
Chris Dalton383a2ef2018-01-08 17:21:41 -0500222 return skstd::make_unique<GrCCClipProcessor>(&clipPath, MustCheckBounds(mustCheckBounds),
223 deviceSpacePath.getFillType());
Chris Daltona32a3c32017-12-05 10:05:21 -0700224}
225
Robert Phillips777707b2018-01-17 11:40:14 -0500226void CCPR::ClipPath::init(GrProxyProvider* proxyProvider,
227 const SkPath& deviceSpacePath, const SkIRect& accessRect,
228 int rtWidth, int rtHeight) {
Chris Daltona32a3c32017-12-05 10:05:21 -0700229 SkASSERT(this->isUninitialized());
230
Robert Phillips777707b2018-01-17 11:40:14 -0500231 fAtlasLazyProxy = proxyProvider->createFullyLazyProxy(
Robert Phillipsce5209a2018-02-13 11:13:51 -0500232 [this](GrResourceProvider* resourceProvider) {
Greg Daniel94a6ce82018-01-16 16:14:41 -0500233 if (!resourceProvider) {
234 return sk_sp<GrTexture>();
235 }
Chris Dalton383a2ef2018-01-08 17:21:41 -0500236 SkASSERT(fHasAtlas);
237 SkASSERT(!fHasAtlasTransform);
Chris Daltona32a3c32017-12-05 10:05:21 -0700238
Chris Dalton383a2ef2018-01-08 17:21:41 -0500239 GrTextureProxy* textureProxy = fAtlas ? fAtlas->textureProxy() : nullptr;
240 if (!textureProxy || !textureProxy->instantiate(resourceProvider)) {
241 fAtlasScale = fAtlasTranslate = {0, 0};
242 SkDEBUGCODE(fHasAtlasTransform = true);
243 return sk_sp<GrTexture>();
244 }
Chris Daltona32a3c32017-12-05 10:05:21 -0700245
Robert Phillipsce5209a2018-02-13 11:13:51 -0500246 SkASSERT(kTopLeft_GrSurfaceOrigin == textureProxy->origin());
247
Chris Dalton383a2ef2018-01-08 17:21:41 -0500248 fAtlasScale = {1.f / textureProxy->width(), 1.f / textureProxy->height()};
249 fAtlasTranslate = {fAtlasOffsetX * fAtlasScale.x(),
250 fAtlasOffsetY * fAtlasScale.y()};
Chris Dalton383a2ef2018-01-08 17:21:41 -0500251 SkDEBUGCODE(fHasAtlasTransform = true);
Chris Daltona32a3c32017-12-05 10:05:21 -0700252
Chris Dalton383a2ef2018-01-08 17:21:41 -0500253 return sk_ref_sp(textureProxy->priv().peekTexture());
254 },
Robert Phillipsce5209a2018-02-13 11:13:51 -0500255 GrProxyProvider::Renderable::kYes, kTopLeft_GrSurfaceOrigin, kAlpha_half_GrPixelConfig);
Chris Daltona32a3c32017-12-05 10:05:21 -0700256
257 const SkRect& pathDevBounds = deviceSpacePath.getBounds();
258 if (SkTMax(pathDevBounds.height(), pathDevBounds.width()) > kPathCropThreshold) {
259 // The path is too large. We need to crop it or analytic AA can run out of fp32 precision.
260 crop_path(deviceSpacePath, SkIRect::MakeWH(rtWidth, rtHeight), &fDeviceSpacePath);
261 } else {
262 fDeviceSpacePath = deviceSpacePath;
263 }
264 deviceSpacePath.getBounds().roundOut(&fPathDevIBounds);
265 fAccessRect = accessRect;
Chris Dalton1a325d22017-07-14 15:17:41 -0600266}
267
268void GrCoverageCountingPathRenderer::preFlush(GrOnFlushResourceProvider* onFlushRP,
269 const uint32_t* opListIDs, int numOpListIDs,
270 SkTArray<sk_sp<GrRenderTargetContext>>* results) {
Chris Dalton383a2ef2018-01-08 17:21:41 -0500271 using PathInstance = GrCCPathProcessor::Instance;
Chris Daltonc1e59632017-09-05 00:30:07 -0600272
Chris Daltona32a3c32017-12-05 10:05:21 -0700273 SkASSERT(!fFlushing);
Chris Daltonc1e59632017-09-05 00:30:07 -0600274 SkASSERT(!fPerFlushIndexBuffer);
275 SkASSERT(!fPerFlushVertexBuffer);
276 SkASSERT(!fPerFlushInstanceBuffer);
Chris Dalton9ca27842018-01-18 12:24:50 -0700277 SkASSERT(!fPerFlushPathParser);
Chris Daltonc1e59632017-09-05 00:30:07 -0600278 SkASSERT(fPerFlushAtlases.empty());
Chris Dalton383a2ef2018-01-08 17:21:41 -0500279 SkDEBUGCODE(fFlushing = true);
Chris Daltona32a3c32017-12-05 10:05:21 -0700280
281 if (fRTPendingPathsMap.empty()) {
Chris Dalton383a2ef2018-01-08 17:21:41 -0500282 return; // Nothing to draw.
Chris Daltona32a3c32017-12-05 10:05:21 -0700283 }
Chris Daltonc1e59632017-09-05 00:30:07 -0600284
285 fPerFlushResourcesAreValid = false;
286
Chris Daltona32a3c32017-12-05 10:05:21 -0700287 // Count the paths that are being flushed.
Chris Daltonc9c97b72017-11-27 15:34:26 -0700288 int maxTotalPaths = 0, maxPathPoints = 0, numSkPoints = 0, numSkVerbs = 0;
Chris Dalton383a2ef2018-01-08 17:21:41 -0500289 SkDEBUGCODE(int numClipPaths = 0);
Chris Dalton1a325d22017-07-14 15:17:41 -0600290 for (int i = 0; i < numOpListIDs; ++i) {
Chris Daltona32a3c32017-12-05 10:05:21 -0700291 auto it = fRTPendingPathsMap.find(opListIDs[i]);
292 if (fRTPendingPathsMap.end() == it) {
Chris Dalton080baa42017-11-06 14:19:19 -0700293 continue;
Chris Dalton1a325d22017-07-14 15:17:41 -0600294 }
Chris Daltona32a3c32017-12-05 10:05:21 -0700295 const RTPendingPaths& rtPendingPaths = it->second;
296
297 SkTInternalLList<DrawPathsOp>::Iter drawOpsIter;
298 drawOpsIter.init(rtPendingPaths.fDrawOps,
299 SkTInternalLList<DrawPathsOp>::Iter::kHead_IterStart);
300 while (DrawPathsOp* op = drawOpsIter.get()) {
301 for (const DrawPathsOp::SingleDraw* draw = op->head(); draw; draw = draw->fNext) {
Chris Dalton080baa42017-11-06 14:19:19 -0700302 ++maxTotalPaths;
Chris Daltonc9c97b72017-11-27 15:34:26 -0700303 maxPathPoints = SkTMax(draw->fPath.countPoints(), maxPathPoints);
Chris Dalton080baa42017-11-06 14:19:19 -0700304 numSkPoints += draw->fPath.countPoints();
305 numSkVerbs += draw->fPath.countVerbs();
306 }
Chris Daltona32a3c32017-12-05 10:05:21 -0700307 drawOpsIter.next();
Chris Dalton080baa42017-11-06 14:19:19 -0700308 }
Chris Daltona32a3c32017-12-05 10:05:21 -0700309
310 maxTotalPaths += rtPendingPaths.fClipPaths.size();
311 SkDEBUGCODE(numClipPaths += rtPendingPaths.fClipPaths.size());
312 for (const auto& clipsIter : rtPendingPaths.fClipPaths) {
313 const SkPath& path = clipsIter.second.deviceSpacePath();
314 maxPathPoints = SkTMax(path.countPoints(), maxPathPoints);
315 numSkPoints += path.countPoints();
316 numSkVerbs += path.countVerbs();
317 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600318 }
319
Chris Daltona32a3c32017-12-05 10:05:21 -0700320 if (!maxTotalPaths) {
Chris Dalton383a2ef2018-01-08 17:21:41 -0500321 return; // Nothing to draw.
Chris Dalton1a325d22017-07-14 15:17:41 -0600322 }
323
Chris Daltona32a3c32017-12-05 10:05:21 -0700324 // Allocate GPU buffers.
Chris Dalton383a2ef2018-01-08 17:21:41 -0500325 fPerFlushIndexBuffer = GrCCPathProcessor::FindIndexBuffer(onFlushRP);
Chris Dalton1a325d22017-07-14 15:17:41 -0600326 if (!fPerFlushIndexBuffer) {
327 SkDebugf("WARNING: failed to allocate ccpr path index buffer.\n");
328 return;
329 }
330
Chris Dalton383a2ef2018-01-08 17:21:41 -0500331 fPerFlushVertexBuffer = GrCCPathProcessor::FindVertexBuffer(onFlushRP);
Chris Dalton1a325d22017-07-14 15:17:41 -0600332 if (!fPerFlushVertexBuffer) {
333 SkDebugf("WARNING: failed to allocate ccpr path vertex buffer.\n");
334 return;
335 }
336
Chris Dalton383a2ef2018-01-08 17:21:41 -0500337 fPerFlushInstanceBuffer =
338 onFlushRP->makeBuffer(kVertex_GrBufferType, maxTotalPaths * sizeof(PathInstance));
Chris Dalton1a325d22017-07-14 15:17:41 -0600339 if (!fPerFlushInstanceBuffer) {
340 SkDebugf("WARNING: failed to allocate path instance buffer. No paths will be drawn.\n");
341 return;
342 }
343
344 PathInstance* pathInstanceData = static_cast<PathInstance*>(fPerFlushInstanceBuffer->map());
345 SkASSERT(pathInstanceData);
346 int pathInstanceIdx = 0;
347
Chris Dalton9ca27842018-01-18 12:24:50 -0700348 fPerFlushPathParser = sk_make_sp<GrCCPathParser>(maxTotalPaths, maxPathPoints, numSkPoints,
349 numSkVerbs);
Chris Dalton383a2ef2018-01-08 17:21:41 -0500350 SkDEBUGCODE(int skippedTotalPaths = 0);
Chris Dalton1a325d22017-07-14 15:17:41 -0600351
Chris Daltona32a3c32017-12-05 10:05:21 -0700352 // Allocate atlas(es) and fill out GPU instance buffers.
353 for (int i = 0; i < numOpListIDs; ++i) {
354 auto it = fRTPendingPathsMap.find(opListIDs[i]);
355 if (fRTPendingPathsMap.end() == it) {
356 continue;
357 }
358 RTPendingPaths& rtPendingPaths = it->second;
Chris Dalton1a325d22017-07-14 15:17:41 -0600359
Chris Daltona32a3c32017-12-05 10:05:21 -0700360 SkTInternalLList<DrawPathsOp>::Iter drawOpsIter;
361 drawOpsIter.init(rtPendingPaths.fDrawOps,
362 SkTInternalLList<DrawPathsOp>::Iter::kHead_IterStart);
363 while (DrawPathsOp* op = drawOpsIter.get()) {
Chris Dalton9ca27842018-01-18 12:24:50 -0700364 pathInstanceIdx = op->setupResources(onFlushRP, pathInstanceData, pathInstanceIdx);
Chris Daltona32a3c32017-12-05 10:05:21 -0700365 drawOpsIter.next();
Chris Dalton383a2ef2018-01-08 17:21:41 -0500366 SkDEBUGCODE(skippedTotalPaths += op->numSkippedInstances_debugOnly());
Chris Dalton1a325d22017-07-14 15:17:41 -0600367 }
368
Chris Daltona32a3c32017-12-05 10:05:21 -0700369 for (auto& clipsIter : rtPendingPaths.fClipPaths) {
Chris Dalton9ca27842018-01-18 12:24:50 -0700370 clipsIter.second.placePathInAtlas(this, onFlushRP, fPerFlushPathParser.get());
Chris Daltonc1e59632017-09-05 00:30:07 -0600371 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600372 }
373
Chris Dalton1a325d22017-07-14 15:17:41 -0600374 fPerFlushInstanceBuffer->unmap();
375
Chris Daltona32a3c32017-12-05 10:05:21 -0700376 SkASSERT(pathInstanceIdx == maxTotalPaths - skippedTotalPaths - numClipPaths);
377
378 if (!fPerFlushAtlases.empty()) {
Chris Dalton9ca27842018-01-18 12:24:50 -0700379 auto coverageCountBatchID = fPerFlushPathParser->closeCurrentBatch();
380 fPerFlushAtlases.back().setCoverageCountBatchID(coverageCountBatchID);
Chris Daltona32a3c32017-12-05 10:05:21 -0700381 }
382
Chris Dalton9ca27842018-01-18 12:24:50 -0700383 if (!fPerFlushPathParser->finalize(onFlushRP)) {
384 SkDebugf("WARNING: failed to allocate GPU buffers for CCPR. No paths will be drawn.\n");
Chris Daltonc1e59632017-09-05 00:30:07 -0600385 return;
Chris Dalton1a325d22017-07-14 15:17:41 -0600386 }
387
Chris Dalton9ca27842018-01-18 12:24:50 -0700388 // Draw the atlas(es).
Chris Dalton383a2ef2018-01-08 17:21:41 -0500389 GrTAllocator<GrCCAtlas>::Iter atlasIter(&fPerFlushAtlases);
Chris Dalton9ca27842018-01-18 12:24:50 -0700390 while (atlasIter.next()) {
391 if (auto rtc = atlasIter.get()->finalize(onFlushRP, fPerFlushPathParser)) {
Chris Daltonc1e59632017-09-05 00:30:07 -0600392 results->push_back(std::move(rtc));
393 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600394 }
Chris Daltonc1e59632017-09-05 00:30:07 -0600395
396 fPerFlushResourcesAreValid = true;
Chris Dalton1a325d22017-07-14 15:17:41 -0600397}
398
Chris Daltona32a3c32017-12-05 10:05:21 -0700399int CCPR::DrawPathsOp::setupResources(GrOnFlushResourceProvider* onFlushRP,
Chris Dalton383a2ef2018-01-08 17:21:41 -0500400 GrCCPathProcessor::Instance* pathInstanceData,
Chris Daltona32a3c32017-12-05 10:05:21 -0700401 int pathInstanceIdx) {
Chris Dalton9ca27842018-01-18 12:24:50 -0700402 GrCCPathParser* parser = fCCPR->fPerFlushPathParser.get();
Chris Dalton383a2ef2018-01-08 17:21:41 -0500403 const GrCCAtlas* currentAtlas = nullptr;
Chris Daltona32a3c32017-12-05 10:05:21 -0700404 SkASSERT(fInstanceCount > 0);
405 SkASSERT(-1 == fBaseInstance);
406 fBaseInstance = pathInstanceIdx;
407
408 for (const SingleDraw* draw = this->head(); draw; draw = draw->fNext) {
409 // parsePath gives us two tight bounding boxes: one in device space, as well as a second
410 // one rotated an additional 45 degrees. The path vertex shader uses these two bounding
411 // boxes to generate an octagon that circumscribes the path.
412 SkRect devBounds, devBounds45;
Chris Dalton9ca27842018-01-18 12:24:50 -0700413 parser->parsePath(draw->fMatrix, draw->fPath, &devBounds, &devBounds45);
Chris Daltona32a3c32017-12-05 10:05:21 -0700414
415 SkIRect devIBounds;
416 devBounds.roundOut(&devIBounds);
417
418 int16_t offsetX, offsetY;
Chris Dalton383a2ef2018-01-08 17:21:41 -0500419 GrCCAtlas* atlas = fCCPR->placeParsedPathInAtlas(onFlushRP, draw->fClipIBounds, devIBounds,
Chris Dalton9ca27842018-01-18 12:24:50 -0700420 &offsetX, &offsetY);
Chris Daltona32a3c32017-12-05 10:05:21 -0700421 if (!atlas) {
422 SkDEBUGCODE(++fNumSkippedInstances);
423 continue;
424 }
425 if (currentAtlas != atlas) {
426 if (currentAtlas) {
427 this->addAtlasBatch(currentAtlas, pathInstanceIdx);
428 }
429 currentAtlas = atlas;
430 }
431
432 const SkMatrix& m = draw->fMatrix;
433 pathInstanceData[pathInstanceIdx++] = {
Chris Dalton383a2ef2018-01-08 17:21:41 -0500434 devBounds,
435 devBounds45,
436 {{m.getScaleX(), m.getSkewY(), m.getSkewX(), m.getScaleY()}},
437 {{m.getTranslateX(), m.getTranslateY()}},
438 {{offsetX, offsetY}},
439 draw->fColor};
Chris Daltona32a3c32017-12-05 10:05:21 -0700440 }
441
442 SkASSERT(pathInstanceIdx == fBaseInstance + fInstanceCount - fNumSkippedInstances);
443 if (currentAtlas) {
444 this->addAtlasBatch(currentAtlas, pathInstanceIdx);
445 }
446
447 return pathInstanceIdx;
448}
449
450void CCPR::ClipPath::placePathInAtlas(GrCoverageCountingPathRenderer* ccpr,
451 GrOnFlushResourceProvider* onFlushRP,
Chris Dalton9ca27842018-01-18 12:24:50 -0700452 GrCCPathParser* parser) {
Chris Daltona32a3c32017-12-05 10:05:21 -0700453 SkASSERT(!this->isUninitialized());
454 SkASSERT(!fHasAtlas);
Chris Dalton9ca27842018-01-18 12:24:50 -0700455 parser->parseDeviceSpacePath(fDeviceSpacePath);
Chris Daltona32a3c32017-12-05 10:05:21 -0700456 fAtlas = ccpr->placeParsedPathInAtlas(onFlushRP, fAccessRect, fPathDevIBounds, &fAtlasOffsetX,
Chris Dalton9ca27842018-01-18 12:24:50 -0700457 &fAtlasOffsetY);
Chris Daltona32a3c32017-12-05 10:05:21 -0700458 SkDEBUGCODE(fHasAtlas = true);
459}
460
Chris Dalton383a2ef2018-01-08 17:21:41 -0500461GrCCAtlas* GrCoverageCountingPathRenderer::placeParsedPathInAtlas(
462 GrOnFlushResourceProvider* onFlushRP,
463 const SkIRect& clipIBounds,
464 const SkIRect& pathIBounds,
465 int16_t* atlasOffsetX,
Chris Dalton9ca27842018-01-18 12:24:50 -0700466 int16_t* atlasOffsetY) {
467 using ScissorMode = GrCCPathParser::ScissorMode;
Chris Daltona32a3c32017-12-05 10:05:21 -0700468
469 ScissorMode scissorMode;
470 SkIRect clippedPathIBounds;
471 if (clipIBounds.contains(pathIBounds)) {
472 clippedPathIBounds = pathIBounds;
473 scissorMode = ScissorMode::kNonScissored;
474 } else if (clippedPathIBounds.intersect(clipIBounds, pathIBounds)) {
475 scissorMode = ScissorMode::kScissored;
476 } else {
Chris Dalton9ca27842018-01-18 12:24:50 -0700477 fPerFlushPathParser->discardParsedPath();
Chris Daltona32a3c32017-12-05 10:05:21 -0700478 return nullptr;
479 }
480
481 SkIPoint16 atlasLocation;
Chris Dalton9ca27842018-01-18 12:24:50 -0700482 int h = clippedPathIBounds.height(), w = clippedPathIBounds.width();
Chris Daltona32a3c32017-12-05 10:05:21 -0700483 if (fPerFlushAtlases.empty() || !fPerFlushAtlases.back().addRect(w, h, &atlasLocation)) {
484 if (!fPerFlushAtlases.empty()) {
485 // The atlas is out of room and can't grow any bigger.
Chris Dalton9ca27842018-01-18 12:24:50 -0700486 auto coverageCountBatchID = fPerFlushPathParser->closeCurrentBatch();
487 fPerFlushAtlases.back().setCoverageCountBatchID(coverageCountBatchID);
Chris Daltona32a3c32017-12-05 10:05:21 -0700488 }
489 fPerFlushAtlases.emplace_back(*onFlushRP->caps(), w, h).addRect(w, h, &atlasLocation);
490 }
491
492 *atlasOffsetX = atlasLocation.x() - static_cast<int16_t>(clippedPathIBounds.left());
493 *atlasOffsetY = atlasLocation.y() - static_cast<int16_t>(clippedPathIBounds.top());
Chris Dalton9ca27842018-01-18 12:24:50 -0700494 fPerFlushPathParser->saveParsedPath(scissorMode, clippedPathIBounds, *atlasOffsetX,
495 *atlasOffsetY);
Chris Daltona32a3c32017-12-05 10:05:21 -0700496
497 return &fPerFlushAtlases.back();
498}
499
500void CCPR::DrawPathsOp::onExecute(GrOpFlushState* flushState) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600501 SkASSERT(fCCPR->fFlushing);
Greg Daniel500d58b2017-08-24 15:59:33 -0400502 SkASSERT(flushState->rtCommandBuffer());
Chris Dalton1a325d22017-07-14 15:17:41 -0600503
Chris Daltonc1e59632017-09-05 00:30:07 -0600504 if (!fCCPR->fPerFlushResourcesAreValid) {
Chris Dalton383a2ef2018-01-08 17:21:41 -0500505 return; // Setup failed.
Chris Dalton1a325d22017-07-14 15:17:41 -0600506 }
507
Chris Dalton383a2ef2018-01-08 17:21:41 -0500508 SkASSERT(fBaseInstance >= 0); // Make sure setupResources has been called.
Chris Dalton080baa42017-11-06 14:19:19 -0700509
Chris Daltond1513222017-10-06 08:30:46 -0600510 GrPipeline::InitArgs initArgs;
511 initArgs.fFlags = fSRGBFlags;
512 initArgs.fProxy = flushState->drawOpArgs().fProxy;
513 initArgs.fCaps = &flushState->caps();
514 initArgs.fResourceProvider = flushState->resourceProvider();
515 initArgs.fDstProxy = flushState->drawOpArgs().fDstProxy;
516 GrPipeline pipeline(initArgs, std::move(fProcessors), flushState->detachAppliedClip());
Chris Dalton1a325d22017-07-14 15:17:41 -0600517
518 int baseInstance = fBaseInstance;
519
520 for (int i = 0; i < fAtlasBatches.count(); baseInstance = fAtlasBatches[i++].fEndInstanceIdx) {
521 const AtlasBatch& batch = fAtlasBatches[i];
522 SkASSERT(batch.fEndInstanceIdx > baseInstance);
523
524 if (!batch.fAtlas->textureProxy()) {
Chris Dalton383a2ef2018-01-08 17:21:41 -0500525 continue; // Atlas failed to allocate.
Chris Dalton1a325d22017-07-14 15:17:41 -0600526 }
527
Chris Dalton27059d32018-01-23 14:06:50 -0700528 GrCCPathProcessor pathProc(flushState->resourceProvider(),
529 sk_ref_sp(batch.fAtlas->textureProxy()), this->getFillType());
Chris Dalton1a325d22017-07-14 15:17:41 -0600530
Chris Dalton27059d32018-01-23 14:06:50 -0700531 GrMesh mesh(GrCCPathProcessor::MeshPrimitiveType(flushState->caps()));
Chris Dalton1a325d22017-07-14 15:17:41 -0600532 mesh.setIndexedInstanced(fCCPR->fPerFlushIndexBuffer.get(),
Chris Dalton27059d32018-01-23 14:06:50 -0700533 GrCCPathProcessor::NumIndicesPerInstance(flushState->caps()),
Chris Dalton1a325d22017-07-14 15:17:41 -0600534 fCCPR->fPerFlushInstanceBuffer.get(),
535 batch.fEndInstanceIdx - baseInstance, baseInstance);
536 mesh.setVertexData(fCCPR->fPerFlushVertexBuffer.get());
537
Chris Dalton27059d32018-01-23 14:06:50 -0700538 flushState->rtCommandBuffer()->draw(pipeline, pathProc, &mesh, nullptr, 1, this->bounds());
Chris Dalton1a325d22017-07-14 15:17:41 -0600539 }
540
Chris Daltona32a3c32017-12-05 10:05:21 -0700541 SkASSERT(baseInstance == fBaseInstance + fInstanceCount - fNumSkippedInstances);
Chris Dalton1a325d22017-07-14 15:17:41 -0600542}
543
Chris Dalton3968ff92017-11-27 12:26:31 -0700544void GrCoverageCountingPathRenderer::postFlush(GrDeferredUploadToken, const uint32_t* opListIDs,
545 int numOpListIDs) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600546 SkASSERT(fFlushing);
547 fPerFlushAtlases.reset();
Chris Dalton9ca27842018-01-18 12:24:50 -0700548 fPerFlushPathParser.reset();
Chris Dalton1a325d22017-07-14 15:17:41 -0600549 fPerFlushInstanceBuffer.reset();
550 fPerFlushVertexBuffer.reset();
551 fPerFlushIndexBuffer.reset();
Chris Daltona32a3c32017-12-05 10:05:21 -0700552 // We wait to erase these until after flush, once Ops and FPs are done accessing their data.
553 for (int i = 0; i < numOpListIDs; ++i) {
554 fRTPendingPathsMap.erase(opListIDs[i]);
555 }
Chris Dalton383a2ef2018-01-08 17:21:41 -0500556 SkDEBUGCODE(fFlushing = false);
Chris Dalton1a325d22017-07-14 15:17:41 -0600557}