blob: 62d3a062fb89adec643e41df60d39bb362940e47 [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"
15#include "GrRenderTargetOpList.h"
16#include "GrStyle.h"
17#include "GrTexture.h"
Chris Dalton1a325d22017-07-14 15:17:41 -060018#include "SkMakeUnique.h"
19#include "SkMatrix.h"
Chris Daltona039d3b2017-09-28 11:16:36 -060020#include "SkPathOps.h"
Chris Dalton383a2ef2018-01-08 17:21:41 -050021#include "ccpr/GrCCClipProcessor.h"
Chris Dalton1a325d22017-07-14 15:17:41 -060022
Chris Daltona32a3c32017-12-05 10:05:21 -070023// Shorthand for keeping line lengths under control with nested classes...
24using CCPR = GrCoverageCountingPathRenderer;
25
26// If a path spans more pixels than this, we need to crop it or else analytic AA can run out of fp32
27// precision.
28static constexpr float kPathCropThreshold = 1 << 16;
29
30static void crop_path(const SkPath& path, const SkIRect& cropbox, SkPath* out) {
31 SkPath cropPath;
32 cropPath.addRect(SkRect::Make(cropbox));
33 if (!Op(cropPath, path, kIntersect_SkPathOp, out)) {
34 // This can fail if the PathOps encounter NaN or infinities.
35 out->reset();
36 }
37}
Chris Dalton1a325d22017-07-14 15:17:41 -060038
39bool GrCoverageCountingPathRenderer::IsSupported(const GrCaps& caps) {
40 const GrShaderCaps& shaderCaps = *caps.shaderCaps();
Chris Dalton383a2ef2018-01-08 17:21:41 -050041 return shaderCaps.integerSupport() && shaderCaps.flatInterpolationSupport() &&
42 caps.instanceAttribSupport() && GrCaps::kNone_MapFlags != caps.mapBufferFlags() &&
Chris Dalton1a325d22017-07-14 15:17:41 -060043 caps.isConfigTexturable(kAlpha_half_GrPixelConfig) &&
Chris Daltone4679fa2017-09-29 13:58:26 -060044 caps.isConfigRenderable(kAlpha_half_GrPixelConfig, /*withMSAA=*/false) &&
45 !caps.blacklistCoverageCounting();
Chris Dalton1a325d22017-07-14 15:17:41 -060046}
47
Chris Dalton383a2ef2018-01-08 17:21:41 -050048sk_sp<GrCoverageCountingPathRenderer> GrCoverageCountingPathRenderer::CreateIfSupported(
49 const GrCaps& caps, bool drawCachablePaths) {
Chris Daltona2ac30d2017-10-17 10:40:01 -060050 auto ccpr = IsSupported(caps) ? new GrCoverageCountingPathRenderer(drawCachablePaths) : nullptr;
51 return sk_sp<GrCoverageCountingPathRenderer>(ccpr);
Chris Dalton1a325d22017-07-14 15:17:41 -060052}
53
Chris Dalton383a2ef2018-01-08 17:21:41 -050054GrPathRenderer::CanDrawPath GrCoverageCountingPathRenderer::onCanDrawPath(
55 const CanDrawPathArgs& args) const {
Chris Daltona2ac30d2017-10-17 10:40:01 -060056 if (args.fShape->hasUnstyledKey() && !fDrawCachablePaths) {
57 return CanDrawPath::kNo;
58 }
59
Chris Dalton383a2ef2018-01-08 17:21:41 -050060 if (!args.fShape->style().isSimpleFill() || args.fShape->inverseFilled() ||
61 args.fViewMatrix->hasPerspective() || GrAAType::kCoverage != args.fAAType) {
Chris Dalton5ed44232017-09-07 13:22:46 -060062 return CanDrawPath::kNo;
Chris Dalton1a325d22017-07-14 15:17:41 -060063 }
64
65 SkPath path;
66 args.fShape->asPath(&path);
Chris Dalton5ed44232017-09-07 13:22:46 -060067 if (SkPathPriv::ConicWeightCnt(path)) {
68 return CanDrawPath::kNo;
69 }
70
Chris Daltondb91c6e2017-09-08 16:25:08 -060071 SkRect devBounds;
72 SkIRect devIBounds;
73 args.fViewMatrix->mapRect(&devBounds, path.getBounds());
74 devBounds.roundOut(&devIBounds);
75 if (!devIBounds.intersect(*args.fClipConservativeBounds)) {
76 // Path is completely clipped away. Our code will eventually notice this before doing any
77 // real work.
78 return CanDrawPath::kYes;
79 }
80
81 if (devIBounds.height() * devIBounds.width() > 256 * 256) {
82 // Large paths can blow up the atlas fast. And they are not ideal for a two-pass rendering
83 // algorithm. Give the simpler direct renderers a chance before we commit to drawing it.
84 return CanDrawPath::kAsBackup;
85 }
86
87 if (args.fShape->hasUnstyledKey() && path.countVerbs() > 50) {
88 // Complex paths do better cached in an SDF, if the renderer will accept them.
89 return CanDrawPath::kAsBackup;
90 }
91
Chris Dalton5ed44232017-09-07 13:22:46 -060092 return CanDrawPath::kYes;
Chris Dalton1a325d22017-07-14 15:17:41 -060093}
94
95bool GrCoverageCountingPathRenderer::onDrawPath(const DrawPathArgs& args) {
96 SkASSERT(!fFlushing);
Chris Dalton1a325d22017-07-14 15:17:41 -060097 auto op = skstd::make_unique<DrawPathsOp>(this, args, args.fPaint.getColor());
98 args.fRenderTargetContext->addDrawOp(*args.fClip, std::move(op));
Chris Dalton1a325d22017-07-14 15:17:41 -060099 return true;
100}
101
Chris Daltona32a3c32017-12-05 10:05:21 -0700102CCPR::DrawPathsOp::DrawPathsOp(GrCoverageCountingPathRenderer* ccpr, const DrawPathArgs& args,
103 GrColor color)
Chris Dalton1a325d22017-07-14 15:17:41 -0600104 : INHERITED(ClassID())
105 , fCCPR(ccpr)
106 , fSRGBFlags(GrPipeline::SRGBFlagsFromPaint(args.fPaint))
107 , fProcessors(std::move(args.fPaint))
108 , fTailDraw(&fHeadDraw)
Chris Daltona32a3c32017-12-05 10:05:21 -0700109 , fOwningRTPendingPaths(nullptr) {
Chris Dalton080baa42017-11-06 14:19:19 -0700110 SkDEBUGCODE(++fCCPR->fPendingDrawOpsCount);
Chris Dalton1a325d22017-07-14 15:17:41 -0600111 SkDEBUGCODE(fBaseInstance = -1);
Chris Dalton383a2ef2018-01-08 17:21:41 -0500112 SkDEBUGCODE(fInstanceCount = 1);
113 SkDEBUGCODE(fNumSkippedInstances = 0);
Chris Dalton1a325d22017-07-14 15:17:41 -0600114 GrRenderTargetContext* const rtc = args.fRenderTargetContext;
115
116 SkRect devBounds;
117 args.fViewMatrix->mapRect(&devBounds, args.fShape->bounds());
Chris Daltonc9c97b72017-11-27 15:34:26 -0700118 args.fClip->getConservativeBounds(rtc->width(), rtc->height(), &fHeadDraw.fClipIBounds,
119 nullptr);
Chris Daltona32a3c32017-12-05 10:05:21 -0700120 if (SkTMax(devBounds.height(), devBounds.width()) > kPathCropThreshold) {
121 // The path is too large. We need to crop it or analytic AA can run out of fp32 precision.
122 SkPath path;
Chris Daltona039d3b2017-09-28 11:16:36 -0600123 args.fShape->asPath(&path);
124 path.transform(*args.fViewMatrix);
125 fHeadDraw.fMatrix.setIdentity();
Chris Daltona32a3c32017-12-05 10:05:21 -0700126 crop_path(path, fHeadDraw.fClipIBounds, &fHeadDraw.fPath);
Chris Daltona039d3b2017-09-28 11:16:36 -0600127 devBounds = fHeadDraw.fPath.getBounds();
Chris Daltona039d3b2017-09-28 11:16:36 -0600128 } else {
129 fHeadDraw.fMatrix = *args.fViewMatrix;
130 args.fShape->asPath(&fHeadDraw.fPath);
Chris Daltona039d3b2017-09-28 11:16:36 -0600131 }
Chris Dalton383a2ef2018-01-08 17:21:41 -0500132 fHeadDraw.fColor = color; // Can't call args.fPaint.getColor() because it has been std::move'd.
Chris Dalton1a325d22017-07-14 15:17:41 -0600133
134 // FIXME: intersect with clip bounds to (hopefully) improve batching.
135 // (This is nontrivial due to assumptions in generating the octagon cover geometry.)
136 this->setBounds(devBounds, GrOp::HasAABloat::kYes, GrOp::IsZeroArea::kNo);
137}
138
Chris Daltona32a3c32017-12-05 10:05:21 -0700139CCPR::DrawPathsOp::~DrawPathsOp() {
140 if (fOwningRTPendingPaths) {
Chris Dalton080baa42017-11-06 14:19:19 -0700141 // Remove CCPR's dangling pointer to this Op before deleting it.
Chris Daltona32a3c32017-12-05 10:05:21 -0700142 fOwningRTPendingPaths->fDrawOps.remove(this);
Chris Dalton080baa42017-11-06 14:19:19 -0700143 }
144 SkDEBUGCODE(--fCCPR->fPendingDrawOpsCount);
145}
146
Chris Daltona32a3c32017-12-05 10:05:21 -0700147GrDrawOp::RequiresDstTexture CCPR::DrawPathsOp::finalize(const GrCaps& caps,
148 const GrAppliedClip* clip,
149 GrPixelConfigIsClamped dstIsClamped) {
Chris Dalton080baa42017-11-06 14:19:19 -0700150 SkASSERT(!fCCPR->fFlushing);
151 // There should only be one single path draw in this Op right now.
Chris Daltona32a3c32017-12-05 10:05:21 -0700152 SkASSERT(1 == fInstanceCount);
Chris Dalton080baa42017-11-06 14:19:19 -0700153 SkASSERT(&fHeadDraw == fTailDraw);
Chris Dalton383a2ef2018-01-08 17:21:41 -0500154 GrProcessorSet::Analysis analysis =
155 fProcessors.finalize(fHeadDraw.fColor, GrProcessorAnalysisCoverage::kSingleChannel,
156 clip, false, caps, dstIsClamped, &fHeadDraw.fColor);
Chris Dalton1a325d22017-07-14 15:17:41 -0600157 return analysis.requiresDstTexture() ? RequiresDstTexture::kYes : RequiresDstTexture::kNo;
158}
159
Chris Daltona32a3c32017-12-05 10:05:21 -0700160bool CCPR::DrawPathsOp::onCombineIfPossible(GrOp* op, const GrCaps& caps) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600161 DrawPathsOp* that = op->cast<DrawPathsOp>();
162 SkASSERT(fCCPR == that->fCCPR);
Chris Dalton080baa42017-11-06 14:19:19 -0700163 SkASSERT(!fCCPR->fFlushing);
Chris Daltona32a3c32017-12-05 10:05:21 -0700164 SkASSERT(fOwningRTPendingPaths);
165 SkASSERT(fInstanceCount);
166 SkASSERT(!that->fOwningRTPendingPaths || that->fOwningRTPendingPaths == fOwningRTPendingPaths);
167 SkASSERT(that->fInstanceCount);
Chris Dalton1a325d22017-07-14 15:17:41 -0600168
Chris Dalton383a2ef2018-01-08 17:21:41 -0500169 if (this->getFillType() != that->getFillType() || fSRGBFlags != that->fSRGBFlags ||
Chris Dalton1a325d22017-07-14 15:17:41 -0600170 fProcessors != that->fProcessors) {
171 return false;
172 }
173
Chris Daltona32a3c32017-12-05 10:05:21 -0700174 fTailDraw->fNext = &fOwningRTPendingPaths->fDrawsAllocator.push_back(that->fHeadDraw);
Chris Dalton080baa42017-11-06 14:19:19 -0700175 fTailDraw = (that->fTailDraw == &that->fHeadDraw) ? fTailDraw->fNext : that->fTailDraw;
Chris Dalton1a325d22017-07-14 15:17:41 -0600176
177 this->joinBounds(*that);
178
Chris Dalton383a2ef2018-01-08 17:21:41 -0500179 SkDEBUGCODE(fInstanceCount += that->fInstanceCount);
Chris Daltona32a3c32017-12-05 10:05:21 -0700180 SkDEBUGCODE(that->fInstanceCount = 0);
Chris Dalton1a325d22017-07-14 15:17:41 -0600181 return true;
182}
183
Chris Daltona32a3c32017-12-05 10:05:21 -0700184void CCPR::DrawPathsOp::wasRecorded(GrRenderTargetOpList* opList) {
Chris Dalton080baa42017-11-06 14:19:19 -0700185 SkASSERT(!fCCPR->fFlushing);
Chris Daltona32a3c32017-12-05 10:05:21 -0700186 SkASSERT(!fOwningRTPendingPaths);
187 fOwningRTPendingPaths = &fCCPR->fRTPendingPathsMap[opList->uniqueID()];
188 fOwningRTPendingPaths->fDrawOps.addToTail(this);
189}
190
191bool GrCoverageCountingPathRenderer::canMakeClipProcessor(const SkPath& deviceSpacePath) const {
192 if (!fDrawCachablePaths && !deviceSpacePath.isVolatile()) {
193 return false;
194 }
195
196 if (SkPathPriv::ConicWeightCnt(deviceSpacePath)) {
197 return false;
198 }
199
200 return true;
201}
202
Chris Dalton383a2ef2018-01-08 17:21:41 -0500203std::unique_ptr<GrFragmentProcessor> GrCoverageCountingPathRenderer::makeClipProcessor(
204 uint32_t opListID, const SkPath& deviceSpacePath, const SkIRect& accessRect, int rtWidth,
205 int rtHeight) {
206 using MustCheckBounds = GrCCClipProcessor::MustCheckBounds;
Chris Daltona32a3c32017-12-05 10:05:21 -0700207
208 SkASSERT(!fFlushing);
209 SkASSERT(this->canMakeClipProcessor(deviceSpacePath));
210
211 ClipPath& clipPath = fRTPendingPathsMap[opListID].fClipPaths[deviceSpacePath.getGenerationID()];
212 if (clipPath.isUninitialized()) {
213 // This ClipPath was just created during lookup. Initialize it.
214 clipPath.init(deviceSpacePath, accessRect, rtWidth, rtHeight);
215 } else {
216 clipPath.addAccess(accessRect);
217 }
218
219 bool mustCheckBounds = !clipPath.pathDevIBounds().contains(accessRect);
Chris Dalton383a2ef2018-01-08 17:21:41 -0500220 return skstd::make_unique<GrCCClipProcessor>(&clipPath, MustCheckBounds(mustCheckBounds),
221 deviceSpacePath.getFillType());
Chris Daltona32a3c32017-12-05 10:05:21 -0700222}
223
224void CCPR::ClipPath::init(const SkPath& deviceSpacePath, const SkIRect& accessRect, int rtWidth,
225 int rtHeight) {
226 SkASSERT(this->isUninitialized());
227
Chris Dalton383a2ef2018-01-08 17:21:41 -0500228 fAtlasLazyProxy = GrSurfaceProxy::MakeLazy(
229 [this](GrResourceProvider* resourceProvider, GrSurfaceOrigin* outOrigin) {
230 SkASSERT(fHasAtlas);
231 SkASSERT(!fHasAtlasTransform);
Chris Daltona32a3c32017-12-05 10:05:21 -0700232
Chris Dalton383a2ef2018-01-08 17:21:41 -0500233 GrTextureProxy* textureProxy = fAtlas ? fAtlas->textureProxy() : nullptr;
234 if (!textureProxy || !textureProxy->instantiate(resourceProvider)) {
235 fAtlasScale = fAtlasTranslate = {0, 0};
236 SkDEBUGCODE(fHasAtlasTransform = true);
237 return sk_sp<GrTexture>();
238 }
Chris Daltona32a3c32017-12-05 10:05:21 -0700239
Chris Dalton383a2ef2018-01-08 17:21:41 -0500240 fAtlasScale = {1.f / textureProxy->width(), 1.f / textureProxy->height()};
241 fAtlasTranslate = {fAtlasOffsetX * fAtlasScale.x(),
242 fAtlasOffsetY * fAtlasScale.y()};
243 if (kBottomLeft_GrSurfaceOrigin == textureProxy->origin()) {
244 fAtlasScale.fY = -fAtlasScale.y();
245 fAtlasTranslate.fY = 1 - fAtlasTranslate.y();
246 }
247 SkDEBUGCODE(fHasAtlasTransform = true);
Chris Daltona32a3c32017-12-05 10:05:21 -0700248
Chris Dalton383a2ef2018-01-08 17:21:41 -0500249 *outOrigin = textureProxy->origin();
250 return sk_ref_sp(textureProxy->priv().peekTexture());
251 },
252 GrSurfaceProxy::Renderable::kYes, kAlpha_half_GrPixelConfig);
Chris Daltona32a3c32017-12-05 10:05:21 -0700253
254 const SkRect& pathDevBounds = deviceSpacePath.getBounds();
255 if (SkTMax(pathDevBounds.height(), pathDevBounds.width()) > kPathCropThreshold) {
256 // The path is too large. We need to crop it or analytic AA can run out of fp32 precision.
257 crop_path(deviceSpacePath, SkIRect::MakeWH(rtWidth, rtHeight), &fDeviceSpacePath);
258 } else {
259 fDeviceSpacePath = deviceSpacePath;
260 }
261 deviceSpacePath.getBounds().roundOut(&fPathDevIBounds);
262 fAccessRect = accessRect;
Chris Dalton1a325d22017-07-14 15:17:41 -0600263}
264
265void GrCoverageCountingPathRenderer::preFlush(GrOnFlushResourceProvider* onFlushRP,
266 const uint32_t* opListIDs, int numOpListIDs,
267 SkTArray<sk_sp<GrRenderTargetContext>>* results) {
Chris Dalton383a2ef2018-01-08 17:21:41 -0500268 using PathInstance = GrCCPathProcessor::Instance;
Chris Daltonc1e59632017-09-05 00:30:07 -0600269
Chris Daltona32a3c32017-12-05 10:05:21 -0700270 SkASSERT(!fFlushing);
Chris Daltonc1e59632017-09-05 00:30:07 -0600271 SkASSERT(!fPerFlushIndexBuffer);
272 SkASSERT(!fPerFlushVertexBuffer);
273 SkASSERT(!fPerFlushInstanceBuffer);
274 SkASSERT(fPerFlushAtlases.empty());
Chris Dalton383a2ef2018-01-08 17:21:41 -0500275 SkDEBUGCODE(fFlushing = true);
Chris Daltona32a3c32017-12-05 10:05:21 -0700276
277 if (fRTPendingPathsMap.empty()) {
Chris Dalton383a2ef2018-01-08 17:21:41 -0500278 return; // Nothing to draw.
Chris Daltona32a3c32017-12-05 10:05:21 -0700279 }
Chris Daltonc1e59632017-09-05 00:30:07 -0600280
281 fPerFlushResourcesAreValid = false;
282
Chris Daltona32a3c32017-12-05 10:05:21 -0700283 // Count the paths that are being flushed.
Chris Daltonc9c97b72017-11-27 15:34:26 -0700284 int maxTotalPaths = 0, maxPathPoints = 0, numSkPoints = 0, numSkVerbs = 0;
Chris Dalton383a2ef2018-01-08 17:21:41 -0500285 SkDEBUGCODE(int numClipPaths = 0);
Chris Dalton1a325d22017-07-14 15:17:41 -0600286 for (int i = 0; i < numOpListIDs; ++i) {
Chris Daltona32a3c32017-12-05 10:05:21 -0700287 auto it = fRTPendingPathsMap.find(opListIDs[i]);
288 if (fRTPendingPathsMap.end() == it) {
Chris Dalton080baa42017-11-06 14:19:19 -0700289 continue;
Chris Dalton1a325d22017-07-14 15:17:41 -0600290 }
Chris Daltona32a3c32017-12-05 10:05:21 -0700291 const RTPendingPaths& rtPendingPaths = it->second;
292
293 SkTInternalLList<DrawPathsOp>::Iter drawOpsIter;
294 drawOpsIter.init(rtPendingPaths.fDrawOps,
295 SkTInternalLList<DrawPathsOp>::Iter::kHead_IterStart);
296 while (DrawPathsOp* op = drawOpsIter.get()) {
297 for (const DrawPathsOp::SingleDraw* draw = op->head(); draw; draw = draw->fNext) {
Chris Dalton080baa42017-11-06 14:19:19 -0700298 ++maxTotalPaths;
Chris Daltonc9c97b72017-11-27 15:34:26 -0700299 maxPathPoints = SkTMax(draw->fPath.countPoints(), maxPathPoints);
Chris Dalton080baa42017-11-06 14:19:19 -0700300 numSkPoints += draw->fPath.countPoints();
301 numSkVerbs += draw->fPath.countVerbs();
302 }
Chris Daltona32a3c32017-12-05 10:05:21 -0700303 drawOpsIter.next();
Chris Dalton080baa42017-11-06 14:19:19 -0700304 }
Chris Daltona32a3c32017-12-05 10:05:21 -0700305
306 maxTotalPaths += rtPendingPaths.fClipPaths.size();
307 SkDEBUGCODE(numClipPaths += rtPendingPaths.fClipPaths.size());
308 for (const auto& clipsIter : rtPendingPaths.fClipPaths) {
309 const SkPath& path = clipsIter.second.deviceSpacePath();
310 maxPathPoints = SkTMax(path.countPoints(), maxPathPoints);
311 numSkPoints += path.countPoints();
312 numSkVerbs += path.countVerbs();
313 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600314 }
315
Chris Daltona32a3c32017-12-05 10:05:21 -0700316 if (!maxTotalPaths) {
Chris Dalton383a2ef2018-01-08 17:21:41 -0500317 return; // Nothing to draw.
Chris Dalton1a325d22017-07-14 15:17:41 -0600318 }
319
Chris Daltona32a3c32017-12-05 10:05:21 -0700320 // Allocate GPU buffers.
Chris Dalton383a2ef2018-01-08 17:21:41 -0500321 fPerFlushIndexBuffer = GrCCPathProcessor::FindIndexBuffer(onFlushRP);
Chris Dalton1a325d22017-07-14 15:17:41 -0600322 if (!fPerFlushIndexBuffer) {
323 SkDebugf("WARNING: failed to allocate ccpr path index buffer.\n");
324 return;
325 }
326
Chris Dalton383a2ef2018-01-08 17:21:41 -0500327 fPerFlushVertexBuffer = GrCCPathProcessor::FindVertexBuffer(onFlushRP);
Chris Dalton1a325d22017-07-14 15:17:41 -0600328 if (!fPerFlushVertexBuffer) {
329 SkDebugf("WARNING: failed to allocate ccpr path vertex buffer.\n");
330 return;
331 }
332
Chris Dalton383a2ef2018-01-08 17:21:41 -0500333 fPerFlushInstanceBuffer =
334 onFlushRP->makeBuffer(kVertex_GrBufferType, maxTotalPaths * sizeof(PathInstance));
Chris Dalton1a325d22017-07-14 15:17:41 -0600335 if (!fPerFlushInstanceBuffer) {
336 SkDebugf("WARNING: failed to allocate path instance buffer. No paths will be drawn.\n");
337 return;
338 }
339
340 PathInstance* pathInstanceData = static_cast<PathInstance*>(fPerFlushInstanceBuffer->map());
341 SkASSERT(pathInstanceData);
342 int pathInstanceIdx = 0;
343
Chris Dalton383a2ef2018-01-08 17:21:41 -0500344 GrCCCoverageOpsBuilder atlasOpsBuilder(maxTotalPaths, maxPathPoints, numSkPoints, numSkVerbs);
345 SkDEBUGCODE(int skippedTotalPaths = 0);
Chris Dalton1a325d22017-07-14 15:17:41 -0600346
Chris Daltona32a3c32017-12-05 10:05:21 -0700347 // Allocate atlas(es) and fill out GPU instance buffers.
348 for (int i = 0; i < numOpListIDs; ++i) {
349 auto it = fRTPendingPathsMap.find(opListIDs[i]);
350 if (fRTPendingPathsMap.end() == it) {
351 continue;
352 }
353 RTPendingPaths& rtPendingPaths = it->second;
Chris Dalton1a325d22017-07-14 15:17:41 -0600354
Chris Daltona32a3c32017-12-05 10:05:21 -0700355 SkTInternalLList<DrawPathsOp>::Iter drawOpsIter;
356 drawOpsIter.init(rtPendingPaths.fDrawOps,
357 SkTInternalLList<DrawPathsOp>::Iter::kHead_IterStart);
358 while (DrawPathsOp* op = drawOpsIter.get()) {
359 pathInstanceIdx = op->setupResources(onFlushRP, &atlasOpsBuilder, pathInstanceData,
360 pathInstanceIdx);
361 drawOpsIter.next();
Chris Dalton383a2ef2018-01-08 17:21:41 -0500362 SkDEBUGCODE(skippedTotalPaths += op->numSkippedInstances_debugOnly());
Chris Dalton1a325d22017-07-14 15:17:41 -0600363 }
364
Chris Daltona32a3c32017-12-05 10:05:21 -0700365 for (auto& clipsIter : rtPendingPaths.fClipPaths) {
366 clipsIter.second.placePathInAtlas(this, onFlushRP, &atlasOpsBuilder);
Chris Daltonc1e59632017-09-05 00:30:07 -0600367 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600368 }
369
Chris Dalton1a325d22017-07-14 15:17:41 -0600370 fPerFlushInstanceBuffer->unmap();
371
Chris Daltona32a3c32017-12-05 10:05:21 -0700372 SkASSERT(pathInstanceIdx == maxTotalPaths - skippedTotalPaths - numClipPaths);
373
374 if (!fPerFlushAtlases.empty()) {
375 atlasOpsBuilder.emitOp(fPerFlushAtlases.back().drawBounds());
376 }
377
Chris Dalton383a2ef2018-01-08 17:21:41 -0500378 SkSTArray<4, std::unique_ptr<GrCCCoverageOp>> atlasOps(fPerFlushAtlases.count());
Chris Daltonc1e59632017-09-05 00:30:07 -0600379 if (!atlasOpsBuilder.finalize(onFlushRP, &atlasOps)) {
380 SkDebugf("WARNING: failed to allocate ccpr atlas buffers. No paths will be drawn.\n");
381 return;
Chris Dalton1a325d22017-07-14 15:17:41 -0600382 }
Chris Daltonc1e59632017-09-05 00:30:07 -0600383 SkASSERT(atlasOps.count() == fPerFlushAtlases.count());
Chris Dalton1a325d22017-07-14 15:17:41 -0600384
Chris Daltona32a3c32017-12-05 10:05:21 -0700385 // Draw the coverage ops into their respective atlases.
Chris Dalton383a2ef2018-01-08 17:21:41 -0500386 GrTAllocator<GrCCAtlas>::Iter atlasIter(&fPerFlushAtlases);
387 for (std::unique_ptr<GrCCCoverageOp>& atlasOp : atlasOps) {
Chris Daltonc1e59632017-09-05 00:30:07 -0600388 SkAssertResult(atlasIter.next());
Chris Dalton383a2ef2018-01-08 17:21:41 -0500389 GrCCAtlas* atlas = atlasIter.get();
390 SkASSERT(atlasOp->bounds() ==
391 SkRect::MakeIWH(atlas->drawBounds().width(), atlas->drawBounds().height()));
Chris Daltonc1e59632017-09-05 00:30:07 -0600392 if (auto rtc = atlas->finalize(onFlushRP, std::move(atlasOp))) {
393 results->push_back(std::move(rtc));
394 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600395 }
Chris Daltonc1e59632017-09-05 00:30:07 -0600396 SkASSERT(!atlasIter.next());
397
398 fPerFlushResourcesAreValid = true;
Chris Dalton1a325d22017-07-14 15:17:41 -0600399}
400
Chris Daltona32a3c32017-12-05 10:05:21 -0700401int CCPR::DrawPathsOp::setupResources(GrOnFlushResourceProvider* onFlushRP,
Chris Dalton383a2ef2018-01-08 17:21:41 -0500402 GrCCCoverageOpsBuilder* atlasOpsBuilder,
403 GrCCPathProcessor::Instance* pathInstanceData,
Chris Daltona32a3c32017-12-05 10:05:21 -0700404 int pathInstanceIdx) {
Chris Dalton383a2ef2018-01-08 17:21:41 -0500405 const GrCCAtlas* currentAtlas = nullptr;
Chris Daltona32a3c32017-12-05 10:05:21 -0700406 SkASSERT(fInstanceCount > 0);
407 SkASSERT(-1 == fBaseInstance);
408 fBaseInstance = pathInstanceIdx;
409
410 for (const SingleDraw* draw = this->head(); draw; draw = draw->fNext) {
411 // parsePath gives us two tight bounding boxes: one in device space, as well as a second
412 // one rotated an additional 45 degrees. The path vertex shader uses these two bounding
413 // boxes to generate an octagon that circumscribes the path.
414 SkRect devBounds, devBounds45;
415 atlasOpsBuilder->parsePath(draw->fMatrix, draw->fPath, &devBounds, &devBounds45);
416
417 SkIRect devIBounds;
418 devBounds.roundOut(&devIBounds);
419
420 int16_t offsetX, offsetY;
Chris Dalton383a2ef2018-01-08 17:21:41 -0500421 GrCCAtlas* atlas = fCCPR->placeParsedPathInAtlas(onFlushRP, draw->fClipIBounds, devIBounds,
422 &offsetX, &offsetY, atlasOpsBuilder);
Chris Daltona32a3c32017-12-05 10:05:21 -0700423 if (!atlas) {
424 SkDEBUGCODE(++fNumSkippedInstances);
425 continue;
426 }
427 if (currentAtlas != atlas) {
428 if (currentAtlas) {
429 this->addAtlasBatch(currentAtlas, pathInstanceIdx);
430 }
431 currentAtlas = atlas;
432 }
433
434 const SkMatrix& m = draw->fMatrix;
435 pathInstanceData[pathInstanceIdx++] = {
Chris Dalton383a2ef2018-01-08 17:21:41 -0500436 devBounds,
437 devBounds45,
438 {{m.getScaleX(), m.getSkewY(), m.getSkewX(), m.getScaleY()}},
439 {{m.getTranslateX(), m.getTranslateY()}},
440 {{offsetX, offsetY}},
441 draw->fColor};
Chris Daltona32a3c32017-12-05 10:05:21 -0700442 }
443
444 SkASSERT(pathInstanceIdx == fBaseInstance + fInstanceCount - fNumSkippedInstances);
445 if (currentAtlas) {
446 this->addAtlasBatch(currentAtlas, pathInstanceIdx);
447 }
448
449 return pathInstanceIdx;
450}
451
452void CCPR::ClipPath::placePathInAtlas(GrCoverageCountingPathRenderer* ccpr,
453 GrOnFlushResourceProvider* onFlushRP,
Chris Dalton383a2ef2018-01-08 17:21:41 -0500454 GrCCCoverageOpsBuilder* atlasOpsBuilder) {
Chris Daltona32a3c32017-12-05 10:05:21 -0700455 SkASSERT(!this->isUninitialized());
456 SkASSERT(!fHasAtlas);
457 atlasOpsBuilder->parseDeviceSpacePath(fDeviceSpacePath);
458 fAtlas = ccpr->placeParsedPathInAtlas(onFlushRP, fAccessRect, fPathDevIBounds, &fAtlasOffsetX,
459 &fAtlasOffsetY, atlasOpsBuilder);
460 SkDEBUGCODE(fHasAtlas = true);
461}
462
Chris Dalton383a2ef2018-01-08 17:21:41 -0500463GrCCAtlas* GrCoverageCountingPathRenderer::placeParsedPathInAtlas(
464 GrOnFlushResourceProvider* onFlushRP,
465 const SkIRect& clipIBounds,
466 const SkIRect& pathIBounds,
467 int16_t* atlasOffsetX,
468 int16_t* atlasOffsetY,
469 GrCCCoverageOpsBuilder* atlasOpsBuilder) {
470 using ScissorMode = GrCCCoverageOpsBuilder::ScissorMode;
Chris Daltona32a3c32017-12-05 10:05:21 -0700471
472 ScissorMode scissorMode;
473 SkIRect clippedPathIBounds;
474 if (clipIBounds.contains(pathIBounds)) {
475 clippedPathIBounds = pathIBounds;
476 scissorMode = ScissorMode::kNonScissored;
477 } else if (clippedPathIBounds.intersect(clipIBounds, pathIBounds)) {
478 scissorMode = ScissorMode::kScissored;
479 } else {
480 atlasOpsBuilder->discardParsedPath();
481 return nullptr;
482 }
483
484 SkIPoint16 atlasLocation;
485 const int h = clippedPathIBounds.height(), w = clippedPathIBounds.width();
486 if (fPerFlushAtlases.empty() || !fPerFlushAtlases.back().addRect(w, h, &atlasLocation)) {
487 if (!fPerFlushAtlases.empty()) {
488 // The atlas is out of room and can't grow any bigger.
489 atlasOpsBuilder->emitOp(fPerFlushAtlases.back().drawBounds());
490 }
491 fPerFlushAtlases.emplace_back(*onFlushRP->caps(), w, h).addRect(w, h, &atlasLocation);
492 }
493
494 *atlasOffsetX = atlasLocation.x() - static_cast<int16_t>(clippedPathIBounds.left());
495 *atlasOffsetY = atlasLocation.y() - static_cast<int16_t>(clippedPathIBounds.top());
496 atlasOpsBuilder->saveParsedPath(scissorMode, clippedPathIBounds, *atlasOffsetX, *atlasOffsetY);
497
498 return &fPerFlushAtlases.back();
499}
500
501void CCPR::DrawPathsOp::onExecute(GrOpFlushState* flushState) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600502 SkASSERT(fCCPR->fFlushing);
Greg Daniel500d58b2017-08-24 15:59:33 -0400503 SkASSERT(flushState->rtCommandBuffer());
Chris Dalton1a325d22017-07-14 15:17:41 -0600504
Chris Daltonc1e59632017-09-05 00:30:07 -0600505 if (!fCCPR->fPerFlushResourcesAreValid) {
Chris Dalton383a2ef2018-01-08 17:21:41 -0500506 return; // Setup failed.
Chris Dalton1a325d22017-07-14 15:17:41 -0600507 }
508
Chris Dalton383a2ef2018-01-08 17:21:41 -0500509 SkASSERT(fBaseInstance >= 0); // Make sure setupResources has been called.
Chris Dalton080baa42017-11-06 14:19:19 -0700510
Chris Daltond1513222017-10-06 08:30:46 -0600511 GrPipeline::InitArgs initArgs;
512 initArgs.fFlags = fSRGBFlags;
513 initArgs.fProxy = flushState->drawOpArgs().fProxy;
514 initArgs.fCaps = &flushState->caps();
515 initArgs.fResourceProvider = flushState->resourceProvider();
516 initArgs.fDstProxy = flushState->drawOpArgs().fDstProxy;
517 GrPipeline pipeline(initArgs, std::move(fProcessors), flushState->detachAppliedClip());
Chris Dalton1a325d22017-07-14 15:17:41 -0600518
519 int baseInstance = fBaseInstance;
520
521 for (int i = 0; i < fAtlasBatches.count(); baseInstance = fAtlasBatches[i++].fEndInstanceIdx) {
522 const AtlasBatch& batch = fAtlasBatches[i];
523 SkASSERT(batch.fEndInstanceIdx > baseInstance);
524
525 if (!batch.fAtlas->textureProxy()) {
Chris Dalton383a2ef2018-01-08 17:21:41 -0500526 continue; // Atlas failed to allocate.
Chris Dalton1a325d22017-07-14 15:17:41 -0600527 }
528
Chris Dalton383a2ef2018-01-08 17:21:41 -0500529 GrCCPathProcessor coverProc(flushState->resourceProvider(),
530 sk_ref_sp(batch.fAtlas->textureProxy()), this->getFillType(),
531 *flushState->gpu()->caps()->shaderCaps());
Chris Dalton1a325d22017-07-14 15:17:41 -0600532
533 GrMesh mesh(GrPrimitiveType::kTriangles);
534 mesh.setIndexedInstanced(fCCPR->fPerFlushIndexBuffer.get(),
Chris Dalton383a2ef2018-01-08 17:21:41 -0500535 GrCCPathProcessor::kPerInstanceIndexCount,
Chris Dalton1a325d22017-07-14 15:17:41 -0600536 fCCPR->fPerFlushInstanceBuffer.get(),
537 batch.fEndInstanceIdx - baseInstance, baseInstance);
538 mesh.setVertexData(fCCPR->fPerFlushVertexBuffer.get());
539
Greg Daniel500d58b2017-08-24 15:59:33 -0400540 flushState->rtCommandBuffer()->draw(pipeline, coverProc, &mesh, nullptr, 1, this->bounds());
Chris Dalton1a325d22017-07-14 15:17:41 -0600541 }
542
Chris Daltona32a3c32017-12-05 10:05:21 -0700543 SkASSERT(baseInstance == fBaseInstance + fInstanceCount - fNumSkippedInstances);
Chris Dalton1a325d22017-07-14 15:17:41 -0600544}
545
Chris Dalton3968ff92017-11-27 12:26:31 -0700546void GrCoverageCountingPathRenderer::postFlush(GrDeferredUploadToken, const uint32_t* opListIDs,
547 int numOpListIDs) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600548 SkASSERT(fFlushing);
549 fPerFlushAtlases.reset();
550 fPerFlushInstanceBuffer.reset();
551 fPerFlushVertexBuffer.reset();
552 fPerFlushIndexBuffer.reset();
Chris Daltona32a3c32017-12-05 10:05:21 -0700553 // We wait to erase these until after flush, once Ops and FPs are done accessing their data.
554 for (int i = 0; i < numOpListIDs; ++i) {
555 fRTPendingPathsMap.erase(opListIDs[i]);
556 }
Chris Dalton383a2ef2018-01-08 17:21:41 -0500557 SkDEBUGCODE(fFlushing = false);
Chris Dalton1a325d22017-07-14 15:17:41 -0600558}