blob: 91074cb714f34668e277f7c133c1e9d95f3f2578 [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) &&
Chris Daltone4679fa2017-09-29 13:58:26 -060045 caps.isConfigRenderable(kAlpha_half_GrPixelConfig, /*withMSAA=*/false) &&
46 !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(
Chris Dalton383a2ef2018-01-08 17:21:41 -0500232 [this](GrResourceProvider* resourceProvider, GrSurfaceOrigin* outOrigin) {
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
Chris Dalton383a2ef2018-01-08 17:21:41 -0500246 fAtlasScale = {1.f / textureProxy->width(), 1.f / textureProxy->height()};
247 fAtlasTranslate = {fAtlasOffsetX * fAtlasScale.x(),
248 fAtlasOffsetY * fAtlasScale.y()};
249 if (kBottomLeft_GrSurfaceOrigin == textureProxy->origin()) {
250 fAtlasScale.fY = -fAtlasScale.y();
251 fAtlasTranslate.fY = 1 - fAtlasTranslate.y();
252 }
253 SkDEBUGCODE(fHasAtlasTransform = true);
Chris Daltona32a3c32017-12-05 10:05:21 -0700254
Chris Dalton383a2ef2018-01-08 17:21:41 -0500255 *outOrigin = textureProxy->origin();
256 return sk_ref_sp(textureProxy->priv().peekTexture());
257 },
Robert Phillips777707b2018-01-17 11:40:14 -0500258 GrProxyProvider::Renderable::kYes, kAlpha_half_GrPixelConfig);
Chris Daltona32a3c32017-12-05 10:05:21 -0700259
260 const SkRect& pathDevBounds = deviceSpacePath.getBounds();
261 if (SkTMax(pathDevBounds.height(), pathDevBounds.width()) > kPathCropThreshold) {
262 // The path is too large. We need to crop it or analytic AA can run out of fp32 precision.
263 crop_path(deviceSpacePath, SkIRect::MakeWH(rtWidth, rtHeight), &fDeviceSpacePath);
264 } else {
265 fDeviceSpacePath = deviceSpacePath;
266 }
267 deviceSpacePath.getBounds().roundOut(&fPathDevIBounds);
268 fAccessRect = accessRect;
Chris Dalton1a325d22017-07-14 15:17:41 -0600269}
270
271void GrCoverageCountingPathRenderer::preFlush(GrOnFlushResourceProvider* onFlushRP,
272 const uint32_t* opListIDs, int numOpListIDs,
273 SkTArray<sk_sp<GrRenderTargetContext>>* results) {
Chris Dalton383a2ef2018-01-08 17:21:41 -0500274 using PathInstance = GrCCPathProcessor::Instance;
Chris Daltonc1e59632017-09-05 00:30:07 -0600275
Chris Daltona32a3c32017-12-05 10:05:21 -0700276 SkASSERT(!fFlushing);
Chris Daltonc1e59632017-09-05 00:30:07 -0600277 SkASSERT(!fPerFlushIndexBuffer);
278 SkASSERT(!fPerFlushVertexBuffer);
279 SkASSERT(!fPerFlushInstanceBuffer);
280 SkASSERT(fPerFlushAtlases.empty());
Chris Dalton383a2ef2018-01-08 17:21:41 -0500281 SkDEBUGCODE(fFlushing = true);
Chris Daltona32a3c32017-12-05 10:05:21 -0700282
283 if (fRTPendingPathsMap.empty()) {
Chris Dalton383a2ef2018-01-08 17:21:41 -0500284 return; // Nothing to draw.
Chris Daltona32a3c32017-12-05 10:05:21 -0700285 }
Chris Daltonc1e59632017-09-05 00:30:07 -0600286
287 fPerFlushResourcesAreValid = false;
288
Chris Daltona32a3c32017-12-05 10:05:21 -0700289 // Count the paths that are being flushed.
Chris Daltonc9c97b72017-11-27 15:34:26 -0700290 int maxTotalPaths = 0, maxPathPoints = 0, numSkPoints = 0, numSkVerbs = 0;
Chris Dalton383a2ef2018-01-08 17:21:41 -0500291 SkDEBUGCODE(int numClipPaths = 0);
Chris Dalton1a325d22017-07-14 15:17:41 -0600292 for (int i = 0; i < numOpListIDs; ++i) {
Chris Daltona32a3c32017-12-05 10:05:21 -0700293 auto it = fRTPendingPathsMap.find(opListIDs[i]);
294 if (fRTPendingPathsMap.end() == it) {
Chris Dalton080baa42017-11-06 14:19:19 -0700295 continue;
Chris Dalton1a325d22017-07-14 15:17:41 -0600296 }
Chris Daltona32a3c32017-12-05 10:05:21 -0700297 const RTPendingPaths& rtPendingPaths = it->second;
298
299 SkTInternalLList<DrawPathsOp>::Iter drawOpsIter;
300 drawOpsIter.init(rtPendingPaths.fDrawOps,
301 SkTInternalLList<DrawPathsOp>::Iter::kHead_IterStart);
302 while (DrawPathsOp* op = drawOpsIter.get()) {
303 for (const DrawPathsOp::SingleDraw* draw = op->head(); draw; draw = draw->fNext) {
Chris Dalton080baa42017-11-06 14:19:19 -0700304 ++maxTotalPaths;
Chris Daltonc9c97b72017-11-27 15:34:26 -0700305 maxPathPoints = SkTMax(draw->fPath.countPoints(), maxPathPoints);
Chris Dalton080baa42017-11-06 14:19:19 -0700306 numSkPoints += draw->fPath.countPoints();
307 numSkVerbs += draw->fPath.countVerbs();
308 }
Chris Daltona32a3c32017-12-05 10:05:21 -0700309 drawOpsIter.next();
Chris Dalton080baa42017-11-06 14:19:19 -0700310 }
Chris Daltona32a3c32017-12-05 10:05:21 -0700311
312 maxTotalPaths += rtPendingPaths.fClipPaths.size();
313 SkDEBUGCODE(numClipPaths += rtPendingPaths.fClipPaths.size());
314 for (const auto& clipsIter : rtPendingPaths.fClipPaths) {
315 const SkPath& path = clipsIter.second.deviceSpacePath();
316 maxPathPoints = SkTMax(path.countPoints(), maxPathPoints);
317 numSkPoints += path.countPoints();
318 numSkVerbs += path.countVerbs();
319 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600320 }
321
Chris Daltona32a3c32017-12-05 10:05:21 -0700322 if (!maxTotalPaths) {
Chris Dalton383a2ef2018-01-08 17:21:41 -0500323 return; // Nothing to draw.
Chris Dalton1a325d22017-07-14 15:17:41 -0600324 }
325
Chris Daltona32a3c32017-12-05 10:05:21 -0700326 // Allocate GPU buffers.
Chris Dalton383a2ef2018-01-08 17:21:41 -0500327 fPerFlushIndexBuffer = GrCCPathProcessor::FindIndexBuffer(onFlushRP);
Chris Dalton1a325d22017-07-14 15:17:41 -0600328 if (!fPerFlushIndexBuffer) {
329 SkDebugf("WARNING: failed to allocate ccpr path index buffer.\n");
330 return;
331 }
332
Chris Dalton383a2ef2018-01-08 17:21:41 -0500333 fPerFlushVertexBuffer = GrCCPathProcessor::FindVertexBuffer(onFlushRP);
Chris Dalton1a325d22017-07-14 15:17:41 -0600334 if (!fPerFlushVertexBuffer) {
335 SkDebugf("WARNING: failed to allocate ccpr path vertex buffer.\n");
336 return;
337 }
338
Chris Dalton383a2ef2018-01-08 17:21:41 -0500339 fPerFlushInstanceBuffer =
340 onFlushRP->makeBuffer(kVertex_GrBufferType, maxTotalPaths * sizeof(PathInstance));
Chris Dalton1a325d22017-07-14 15:17:41 -0600341 if (!fPerFlushInstanceBuffer) {
342 SkDebugf("WARNING: failed to allocate path instance buffer. No paths will be drawn.\n");
343 return;
344 }
345
346 PathInstance* pathInstanceData = static_cast<PathInstance*>(fPerFlushInstanceBuffer->map());
347 SkASSERT(pathInstanceData);
348 int pathInstanceIdx = 0;
349
Chris Dalton383a2ef2018-01-08 17:21:41 -0500350 GrCCCoverageOpsBuilder atlasOpsBuilder(maxTotalPaths, maxPathPoints, numSkPoints, numSkVerbs);
351 SkDEBUGCODE(int skippedTotalPaths = 0);
Chris Dalton1a325d22017-07-14 15:17:41 -0600352
Chris Daltona32a3c32017-12-05 10:05:21 -0700353 // Allocate atlas(es) and fill out GPU instance buffers.
354 for (int i = 0; i < numOpListIDs; ++i) {
355 auto it = fRTPendingPathsMap.find(opListIDs[i]);
356 if (fRTPendingPathsMap.end() == it) {
357 continue;
358 }
359 RTPendingPaths& rtPendingPaths = it->second;
Chris Dalton1a325d22017-07-14 15:17:41 -0600360
Chris Daltona32a3c32017-12-05 10:05:21 -0700361 SkTInternalLList<DrawPathsOp>::Iter drawOpsIter;
362 drawOpsIter.init(rtPendingPaths.fDrawOps,
363 SkTInternalLList<DrawPathsOp>::Iter::kHead_IterStart);
364 while (DrawPathsOp* op = drawOpsIter.get()) {
365 pathInstanceIdx = op->setupResources(onFlushRP, &atlasOpsBuilder, pathInstanceData,
366 pathInstanceIdx);
367 drawOpsIter.next();
Chris Dalton383a2ef2018-01-08 17:21:41 -0500368 SkDEBUGCODE(skippedTotalPaths += op->numSkippedInstances_debugOnly());
Chris Dalton1a325d22017-07-14 15:17:41 -0600369 }
370
Chris Daltona32a3c32017-12-05 10:05:21 -0700371 for (auto& clipsIter : rtPendingPaths.fClipPaths) {
372 clipsIter.second.placePathInAtlas(this, onFlushRP, &atlasOpsBuilder);
Chris Daltonc1e59632017-09-05 00:30:07 -0600373 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600374 }
375
Chris Dalton1a325d22017-07-14 15:17:41 -0600376 fPerFlushInstanceBuffer->unmap();
377
Chris Daltona32a3c32017-12-05 10:05:21 -0700378 SkASSERT(pathInstanceIdx == maxTotalPaths - skippedTotalPaths - numClipPaths);
379
380 if (!fPerFlushAtlases.empty()) {
381 atlasOpsBuilder.emitOp(fPerFlushAtlases.back().drawBounds());
382 }
383
Chris Dalton383a2ef2018-01-08 17:21:41 -0500384 SkSTArray<4, std::unique_ptr<GrCCCoverageOp>> atlasOps(fPerFlushAtlases.count());
Chris Daltonc1e59632017-09-05 00:30:07 -0600385 if (!atlasOpsBuilder.finalize(onFlushRP, &atlasOps)) {
386 SkDebugf("WARNING: failed to allocate ccpr atlas buffers. No paths will be drawn.\n");
387 return;
Chris Dalton1a325d22017-07-14 15:17:41 -0600388 }
Chris Daltonc1e59632017-09-05 00:30:07 -0600389 SkASSERT(atlasOps.count() == fPerFlushAtlases.count());
Chris Dalton1a325d22017-07-14 15:17:41 -0600390
Chris Daltona32a3c32017-12-05 10:05:21 -0700391 // Draw the coverage ops into their respective atlases.
Chris Dalton383a2ef2018-01-08 17:21:41 -0500392 GrTAllocator<GrCCAtlas>::Iter atlasIter(&fPerFlushAtlases);
393 for (std::unique_ptr<GrCCCoverageOp>& atlasOp : atlasOps) {
Chris Daltonc1e59632017-09-05 00:30:07 -0600394 SkAssertResult(atlasIter.next());
Chris Dalton383a2ef2018-01-08 17:21:41 -0500395 GrCCAtlas* atlas = atlasIter.get();
396 SkASSERT(atlasOp->bounds() ==
397 SkRect::MakeIWH(atlas->drawBounds().width(), atlas->drawBounds().height()));
Chris Daltonc1e59632017-09-05 00:30:07 -0600398 if (auto rtc = atlas->finalize(onFlushRP, std::move(atlasOp))) {
399 results->push_back(std::move(rtc));
400 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600401 }
Chris Daltonc1e59632017-09-05 00:30:07 -0600402 SkASSERT(!atlasIter.next());
403
404 fPerFlushResourcesAreValid = true;
Chris Dalton1a325d22017-07-14 15:17:41 -0600405}
406
Chris Daltona32a3c32017-12-05 10:05:21 -0700407int CCPR::DrawPathsOp::setupResources(GrOnFlushResourceProvider* onFlushRP,
Chris Dalton383a2ef2018-01-08 17:21:41 -0500408 GrCCCoverageOpsBuilder* atlasOpsBuilder,
409 GrCCPathProcessor::Instance* pathInstanceData,
Chris Daltona32a3c32017-12-05 10:05:21 -0700410 int pathInstanceIdx) {
Chris Dalton383a2ef2018-01-08 17:21:41 -0500411 const GrCCAtlas* currentAtlas = nullptr;
Chris Daltona32a3c32017-12-05 10:05:21 -0700412 SkASSERT(fInstanceCount > 0);
413 SkASSERT(-1 == fBaseInstance);
414 fBaseInstance = pathInstanceIdx;
415
416 for (const SingleDraw* draw = this->head(); draw; draw = draw->fNext) {
417 // parsePath gives us two tight bounding boxes: one in device space, as well as a second
418 // one rotated an additional 45 degrees. The path vertex shader uses these two bounding
419 // boxes to generate an octagon that circumscribes the path.
420 SkRect devBounds, devBounds45;
421 atlasOpsBuilder->parsePath(draw->fMatrix, draw->fPath, &devBounds, &devBounds45);
422
423 SkIRect devIBounds;
424 devBounds.roundOut(&devIBounds);
425
426 int16_t offsetX, offsetY;
Chris Dalton383a2ef2018-01-08 17:21:41 -0500427 GrCCAtlas* atlas = fCCPR->placeParsedPathInAtlas(onFlushRP, draw->fClipIBounds, devIBounds,
428 &offsetX, &offsetY, atlasOpsBuilder);
Chris Daltona32a3c32017-12-05 10:05:21 -0700429 if (!atlas) {
430 SkDEBUGCODE(++fNumSkippedInstances);
431 continue;
432 }
433 if (currentAtlas != atlas) {
434 if (currentAtlas) {
435 this->addAtlasBatch(currentAtlas, pathInstanceIdx);
436 }
437 currentAtlas = atlas;
438 }
439
440 const SkMatrix& m = draw->fMatrix;
441 pathInstanceData[pathInstanceIdx++] = {
Chris Dalton383a2ef2018-01-08 17:21:41 -0500442 devBounds,
443 devBounds45,
444 {{m.getScaleX(), m.getSkewY(), m.getSkewX(), m.getScaleY()}},
445 {{m.getTranslateX(), m.getTranslateY()}},
446 {{offsetX, offsetY}},
447 draw->fColor};
Chris Daltona32a3c32017-12-05 10:05:21 -0700448 }
449
450 SkASSERT(pathInstanceIdx == fBaseInstance + fInstanceCount - fNumSkippedInstances);
451 if (currentAtlas) {
452 this->addAtlasBatch(currentAtlas, pathInstanceIdx);
453 }
454
455 return pathInstanceIdx;
456}
457
458void CCPR::ClipPath::placePathInAtlas(GrCoverageCountingPathRenderer* ccpr,
459 GrOnFlushResourceProvider* onFlushRP,
Chris Dalton383a2ef2018-01-08 17:21:41 -0500460 GrCCCoverageOpsBuilder* atlasOpsBuilder) {
Chris Daltona32a3c32017-12-05 10:05:21 -0700461 SkASSERT(!this->isUninitialized());
462 SkASSERT(!fHasAtlas);
463 atlasOpsBuilder->parseDeviceSpacePath(fDeviceSpacePath);
464 fAtlas = ccpr->placeParsedPathInAtlas(onFlushRP, fAccessRect, fPathDevIBounds, &fAtlasOffsetX,
465 &fAtlasOffsetY, atlasOpsBuilder);
466 SkDEBUGCODE(fHasAtlas = true);
467}
468
Chris Dalton383a2ef2018-01-08 17:21:41 -0500469GrCCAtlas* GrCoverageCountingPathRenderer::placeParsedPathInAtlas(
470 GrOnFlushResourceProvider* onFlushRP,
471 const SkIRect& clipIBounds,
472 const SkIRect& pathIBounds,
473 int16_t* atlasOffsetX,
474 int16_t* atlasOffsetY,
475 GrCCCoverageOpsBuilder* atlasOpsBuilder) {
476 using ScissorMode = GrCCCoverageOpsBuilder::ScissorMode;
Chris Daltona32a3c32017-12-05 10:05:21 -0700477
478 ScissorMode scissorMode;
479 SkIRect clippedPathIBounds;
480 if (clipIBounds.contains(pathIBounds)) {
481 clippedPathIBounds = pathIBounds;
482 scissorMode = ScissorMode::kNonScissored;
483 } else if (clippedPathIBounds.intersect(clipIBounds, pathIBounds)) {
484 scissorMode = ScissorMode::kScissored;
485 } else {
486 atlasOpsBuilder->discardParsedPath();
487 return nullptr;
488 }
489
490 SkIPoint16 atlasLocation;
491 const int h = clippedPathIBounds.height(), w = clippedPathIBounds.width();
492 if (fPerFlushAtlases.empty() || !fPerFlushAtlases.back().addRect(w, h, &atlasLocation)) {
493 if (!fPerFlushAtlases.empty()) {
494 // The atlas is out of room and can't grow any bigger.
495 atlasOpsBuilder->emitOp(fPerFlushAtlases.back().drawBounds());
496 }
497 fPerFlushAtlases.emplace_back(*onFlushRP->caps(), w, h).addRect(w, h, &atlasLocation);
498 }
499
500 *atlasOffsetX = atlasLocation.x() - static_cast<int16_t>(clippedPathIBounds.left());
501 *atlasOffsetY = atlasLocation.y() - static_cast<int16_t>(clippedPathIBounds.top());
502 atlasOpsBuilder->saveParsedPath(scissorMode, clippedPathIBounds, *atlasOffsetX, *atlasOffsetY);
503
504 return &fPerFlushAtlases.back();
505}
506
507void CCPR::DrawPathsOp::onExecute(GrOpFlushState* flushState) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600508 SkASSERT(fCCPR->fFlushing);
Greg Daniel500d58b2017-08-24 15:59:33 -0400509 SkASSERT(flushState->rtCommandBuffer());
Chris Dalton1a325d22017-07-14 15:17:41 -0600510
Chris Daltonc1e59632017-09-05 00:30:07 -0600511 if (!fCCPR->fPerFlushResourcesAreValid) {
Chris Dalton383a2ef2018-01-08 17:21:41 -0500512 return; // Setup failed.
Chris Dalton1a325d22017-07-14 15:17:41 -0600513 }
514
Chris Dalton383a2ef2018-01-08 17:21:41 -0500515 SkASSERT(fBaseInstance >= 0); // Make sure setupResources has been called.
Chris Dalton080baa42017-11-06 14:19:19 -0700516
Chris Daltond1513222017-10-06 08:30:46 -0600517 GrPipeline::InitArgs initArgs;
518 initArgs.fFlags = fSRGBFlags;
519 initArgs.fProxy = flushState->drawOpArgs().fProxy;
520 initArgs.fCaps = &flushState->caps();
521 initArgs.fResourceProvider = flushState->resourceProvider();
522 initArgs.fDstProxy = flushState->drawOpArgs().fDstProxy;
523 GrPipeline pipeline(initArgs, std::move(fProcessors), flushState->detachAppliedClip());
Chris Dalton1a325d22017-07-14 15:17:41 -0600524
525 int baseInstance = fBaseInstance;
526
527 for (int i = 0; i < fAtlasBatches.count(); baseInstance = fAtlasBatches[i++].fEndInstanceIdx) {
528 const AtlasBatch& batch = fAtlasBatches[i];
529 SkASSERT(batch.fEndInstanceIdx > baseInstance);
530
531 if (!batch.fAtlas->textureProxy()) {
Chris Dalton383a2ef2018-01-08 17:21:41 -0500532 continue; // Atlas failed to allocate.
Chris Dalton1a325d22017-07-14 15:17:41 -0600533 }
534
Chris Dalton383a2ef2018-01-08 17:21:41 -0500535 GrCCPathProcessor coverProc(flushState->resourceProvider(),
536 sk_ref_sp(batch.fAtlas->textureProxy()), this->getFillType(),
537 *flushState->gpu()->caps()->shaderCaps());
Chris Dalton1a325d22017-07-14 15:17:41 -0600538
539 GrMesh mesh(GrPrimitiveType::kTriangles);
540 mesh.setIndexedInstanced(fCCPR->fPerFlushIndexBuffer.get(),
Chris Dalton383a2ef2018-01-08 17:21:41 -0500541 GrCCPathProcessor::kPerInstanceIndexCount,
Chris Dalton1a325d22017-07-14 15:17:41 -0600542 fCCPR->fPerFlushInstanceBuffer.get(),
543 batch.fEndInstanceIdx - baseInstance, baseInstance);
544 mesh.setVertexData(fCCPR->fPerFlushVertexBuffer.get());
545
Greg Daniel500d58b2017-08-24 15:59:33 -0400546 flushState->rtCommandBuffer()->draw(pipeline, coverProc, &mesh, nullptr, 1, this->bounds());
Chris Dalton1a325d22017-07-14 15:17:41 -0600547 }
548
Chris Daltona32a3c32017-12-05 10:05:21 -0700549 SkASSERT(baseInstance == fBaseInstance + fInstanceCount - fNumSkippedInstances);
Chris Dalton1a325d22017-07-14 15:17:41 -0600550}
551
Chris Dalton3968ff92017-11-27 12:26:31 -0700552void GrCoverageCountingPathRenderer::postFlush(GrDeferredUploadToken, const uint32_t* opListIDs,
553 int numOpListIDs) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600554 SkASSERT(fFlushing);
555 fPerFlushAtlases.reset();
556 fPerFlushInstanceBuffer.reset();
557 fPerFlushVertexBuffer.reset();
558 fPerFlushIndexBuffer.reset();
Chris Daltona32a3c32017-12-05 10:05:21 -0700559 // We wait to erase these until after flush, once Ops and FPs are done accessing their data.
560 for (int i = 0; i < numOpListIDs; ++i) {
561 fRTPendingPathsMap.erase(opListIDs[i]);
562 }
Chris Dalton383a2ef2018-01-08 17:21:41 -0500563 SkDEBUGCODE(fFlushing = false);
Chris Dalton1a325d22017-07-14 15:17:41 -0600564}