blob: 937fd4824b588623619a2b553ec68383f1c94ac8 [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"
14#include "SkMakeUnique.h"
15#include "SkMatrix.h"
Chris Daltona039d3b2017-09-28 11:16:36 -060016#include "SkPathOps.h"
Chris Dalton1a325d22017-07-14 15:17:41 -060017#include "GrOpFlushState.h"
18#include "GrRenderTargetOpList.h"
Chris Daltona32a3c32017-12-05 10:05:21 -070019#include "GrTexture.h"
Chris Dalton1a325d22017-07-14 15:17:41 -060020#include "GrStyle.h"
Chris Daltona32a3c32017-12-05 10:05:21 -070021#include "ccpr/GrCCPRClipProcessor.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();
41 return shaderCaps.geometryShaderSupport() &&
Chris Dalton1a325d22017-07-14 15:17:41 -060042 shaderCaps.integerSupport() &&
43 shaderCaps.flatInterpolationSupport() &&
Chris Dalton1a325d22017-07-14 15:17:41 -060044 caps.instanceAttribSupport() &&
Chris Daltonfddb6c02017-11-04 15:22:22 -060045 GrCaps::kNone_MapFlags != caps.mapBufferFlags() &&
Chris Dalton1a325d22017-07-14 15:17:41 -060046 caps.isConfigTexturable(kAlpha_half_GrPixelConfig) &&
Chris Daltone4679fa2017-09-29 13:58:26 -060047 caps.isConfigRenderable(kAlpha_half_GrPixelConfig, /*withMSAA=*/false) &&
Brian Salomon7f56d3d2017-10-09 13:02:49 -040048 GrCaps::kNone_MapFlags != caps.mapBufferFlags() &&
Chris Daltone4679fa2017-09-29 13:58:26 -060049 !caps.blacklistCoverageCounting();
Chris Dalton1a325d22017-07-14 15:17:41 -060050}
51
52sk_sp<GrCoverageCountingPathRenderer>
Chris Daltona2ac30d2017-10-17 10:40:01 -060053GrCoverageCountingPathRenderer::CreateIfSupported(const GrCaps& caps, bool drawCachablePaths) {
54 auto ccpr = IsSupported(caps) ? new GrCoverageCountingPathRenderer(drawCachablePaths) : nullptr;
55 return sk_sp<GrCoverageCountingPathRenderer>(ccpr);
Chris Dalton1a325d22017-07-14 15:17:41 -060056}
57
Chris Dalton5ed44232017-09-07 13:22:46 -060058GrPathRenderer::CanDrawPath
59GrCoverageCountingPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const {
Chris Daltona2ac30d2017-10-17 10:40:01 -060060 if (args.fShape->hasUnstyledKey() && !fDrawCachablePaths) {
61 return CanDrawPath::kNo;
62 }
63
Chris Dalton1a325d22017-07-14 15:17:41 -060064 if (!args.fShape->style().isSimpleFill() ||
65 args.fShape->inverseFilled() ||
66 args.fViewMatrix->hasPerspective() ||
67 GrAAType::kCoverage != args.fAAType) {
Chris Dalton5ed44232017-09-07 13:22:46 -060068 return CanDrawPath::kNo;
Chris Dalton1a325d22017-07-14 15:17:41 -060069 }
70
71 SkPath path;
72 args.fShape->asPath(&path);
Chris Dalton5ed44232017-09-07 13:22:46 -060073 if (SkPathPriv::ConicWeightCnt(path)) {
74 return CanDrawPath::kNo;
75 }
76
Chris Daltondb91c6e2017-09-08 16:25:08 -060077 SkRect devBounds;
78 SkIRect devIBounds;
79 args.fViewMatrix->mapRect(&devBounds, path.getBounds());
80 devBounds.roundOut(&devIBounds);
81 if (!devIBounds.intersect(*args.fClipConservativeBounds)) {
82 // Path is completely clipped away. Our code will eventually notice this before doing any
83 // real work.
84 return CanDrawPath::kYes;
85 }
86
87 if (devIBounds.height() * devIBounds.width() > 256 * 256) {
88 // Large paths can blow up the atlas fast. And they are not ideal for a two-pass rendering
89 // algorithm. Give the simpler direct renderers a chance before we commit to drawing it.
90 return CanDrawPath::kAsBackup;
91 }
92
93 if (args.fShape->hasUnstyledKey() && path.countVerbs() > 50) {
94 // Complex paths do better cached in an SDF, if the renderer will accept them.
95 return CanDrawPath::kAsBackup;
96 }
97
Chris Dalton5ed44232017-09-07 13:22:46 -060098 return CanDrawPath::kYes;
Chris Dalton1a325d22017-07-14 15:17:41 -060099}
100
101bool GrCoverageCountingPathRenderer::onDrawPath(const DrawPathArgs& args) {
102 SkASSERT(!fFlushing);
Chris Dalton1a325d22017-07-14 15:17:41 -0600103 auto op = skstd::make_unique<DrawPathsOp>(this, args, args.fPaint.getColor());
104 args.fRenderTargetContext->addDrawOp(*args.fClip, std::move(op));
Chris Dalton1a325d22017-07-14 15:17:41 -0600105 return true;
106}
107
Chris Daltona32a3c32017-12-05 10:05:21 -0700108CCPR::DrawPathsOp::DrawPathsOp(GrCoverageCountingPathRenderer* ccpr, const DrawPathArgs& args,
109 GrColor color)
Chris Dalton1a325d22017-07-14 15:17:41 -0600110 : INHERITED(ClassID())
111 , fCCPR(ccpr)
112 , fSRGBFlags(GrPipeline::SRGBFlagsFromPaint(args.fPaint))
113 , fProcessors(std::move(args.fPaint))
114 , fTailDraw(&fHeadDraw)
Chris Daltona32a3c32017-12-05 10:05:21 -0700115 , fOwningRTPendingPaths(nullptr) {
Chris Dalton080baa42017-11-06 14:19:19 -0700116 SkDEBUGCODE(++fCCPR->fPendingDrawOpsCount);
Chris Dalton1a325d22017-07-14 15:17:41 -0600117 SkDEBUGCODE(fBaseInstance = -1);
Chris Daltona32a3c32017-12-05 10:05:21 -0700118 SkDEBUGCODE(fInstanceCount = 1;)
119 SkDEBUGCODE(fNumSkippedInstances = 0;)
Chris Dalton1a325d22017-07-14 15:17:41 -0600120 GrRenderTargetContext* const rtc = args.fRenderTargetContext;
121
122 SkRect devBounds;
123 args.fViewMatrix->mapRect(&devBounds, args.fShape->bounds());
Chris Daltonc9c97b72017-11-27 15:34:26 -0700124 args.fClip->getConservativeBounds(rtc->width(), rtc->height(), &fHeadDraw.fClipIBounds,
125 nullptr);
Chris Daltona32a3c32017-12-05 10:05:21 -0700126 if (SkTMax(devBounds.height(), devBounds.width()) > kPathCropThreshold) {
127 // The path is too large. We need to crop it or analytic AA can run out of fp32 precision.
128 SkPath path;
Chris Daltona039d3b2017-09-28 11:16:36 -0600129 args.fShape->asPath(&path);
130 path.transform(*args.fViewMatrix);
131 fHeadDraw.fMatrix.setIdentity();
Chris Daltona32a3c32017-12-05 10:05:21 -0700132 crop_path(path, fHeadDraw.fClipIBounds, &fHeadDraw.fPath);
Chris Daltona039d3b2017-09-28 11:16:36 -0600133 devBounds = fHeadDraw.fPath.getBounds();
Chris Daltona039d3b2017-09-28 11:16:36 -0600134 } else {
135 fHeadDraw.fMatrix = *args.fViewMatrix;
136 args.fShape->asPath(&fHeadDraw.fPath);
Chris Daltona039d3b2017-09-28 11:16:36 -0600137 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600138 fHeadDraw.fColor = color; // Can't call args.fPaint.getColor() because it has been std::move'd.
139
140 // FIXME: intersect with clip bounds to (hopefully) improve batching.
141 // (This is nontrivial due to assumptions in generating the octagon cover geometry.)
142 this->setBounds(devBounds, GrOp::HasAABloat::kYes, GrOp::IsZeroArea::kNo);
143}
144
Chris Daltona32a3c32017-12-05 10:05:21 -0700145CCPR::DrawPathsOp::~DrawPathsOp() {
146 if (fOwningRTPendingPaths) {
Chris Dalton080baa42017-11-06 14:19:19 -0700147 // Remove CCPR's dangling pointer to this Op before deleting it.
Chris Daltona32a3c32017-12-05 10:05:21 -0700148 fOwningRTPendingPaths->fDrawOps.remove(this);
Chris Dalton080baa42017-11-06 14:19:19 -0700149 }
150 SkDEBUGCODE(--fCCPR->fPendingDrawOpsCount);
151}
152
Chris Daltona32a3c32017-12-05 10:05:21 -0700153GrDrawOp::RequiresDstTexture CCPR::DrawPathsOp::finalize(const GrCaps& caps,
154 const GrAppliedClip* clip,
155 GrPixelConfigIsClamped dstIsClamped) {
Chris Dalton080baa42017-11-06 14:19:19 -0700156 SkASSERT(!fCCPR->fFlushing);
157 // There should only be one single path draw in this Op right now.
Chris Daltona32a3c32017-12-05 10:05:21 -0700158 SkASSERT(1 == fInstanceCount);
Chris Dalton080baa42017-11-06 14:19:19 -0700159 SkASSERT(&fHeadDraw == fTailDraw);
Brian Osman9a725dd2017-09-20 09:53:22 -0400160 GrProcessorSet::Analysis analysis = fProcessors.finalize(
Chris Dalton080baa42017-11-06 14:19:19 -0700161 fHeadDraw.fColor, GrProcessorAnalysisCoverage::kSingleChannel, clip, false, caps,
162 dstIsClamped, &fHeadDraw.fColor);
Chris Dalton1a325d22017-07-14 15:17:41 -0600163 return analysis.requiresDstTexture() ? RequiresDstTexture::kYes : RequiresDstTexture::kNo;
164}
165
Chris Daltona32a3c32017-12-05 10:05:21 -0700166bool CCPR::DrawPathsOp::onCombineIfPossible(GrOp* op, const GrCaps& caps) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600167 DrawPathsOp* that = op->cast<DrawPathsOp>();
168 SkASSERT(fCCPR == that->fCCPR);
Chris Dalton080baa42017-11-06 14:19:19 -0700169 SkASSERT(!fCCPR->fFlushing);
Chris Daltona32a3c32017-12-05 10:05:21 -0700170 SkASSERT(fOwningRTPendingPaths);
171 SkASSERT(fInstanceCount);
172 SkASSERT(!that->fOwningRTPendingPaths || that->fOwningRTPendingPaths == fOwningRTPendingPaths);
173 SkASSERT(that->fInstanceCount);
Chris Dalton1a325d22017-07-14 15:17:41 -0600174
175 if (this->getFillType() != that->getFillType() ||
176 fSRGBFlags != that->fSRGBFlags ||
177 fProcessors != that->fProcessors) {
178 return false;
179 }
180
Chris Daltona32a3c32017-12-05 10:05:21 -0700181 fTailDraw->fNext = &fOwningRTPendingPaths->fDrawsAllocator.push_back(that->fHeadDraw);
Chris Dalton080baa42017-11-06 14:19:19 -0700182 fTailDraw = (that->fTailDraw == &that->fHeadDraw) ? fTailDraw->fNext : that->fTailDraw;
Chris Dalton1a325d22017-07-14 15:17:41 -0600183
184 this->joinBounds(*that);
185
Chris Daltona32a3c32017-12-05 10:05:21 -0700186 SkDEBUGCODE(fInstanceCount += that->fInstanceCount;)
187 SkDEBUGCODE(that->fInstanceCount = 0);
Chris Dalton1a325d22017-07-14 15:17:41 -0600188 return true;
189}
190
Chris Daltona32a3c32017-12-05 10:05:21 -0700191void CCPR::DrawPathsOp::wasRecorded(GrRenderTargetOpList* opList) {
Chris Dalton080baa42017-11-06 14:19:19 -0700192 SkASSERT(!fCCPR->fFlushing);
Chris Daltona32a3c32017-12-05 10:05:21 -0700193 SkASSERT(!fOwningRTPendingPaths);
194 fOwningRTPendingPaths = &fCCPR->fRTPendingPathsMap[opList->uniqueID()];
195 fOwningRTPendingPaths->fDrawOps.addToTail(this);
196}
197
198bool GrCoverageCountingPathRenderer::canMakeClipProcessor(const SkPath& deviceSpacePath) const {
199 if (!fDrawCachablePaths && !deviceSpacePath.isVolatile()) {
200 return false;
201 }
202
203 if (SkPathPriv::ConicWeightCnt(deviceSpacePath)) {
204 return false;
205 }
206
207 return true;
208}
209
210std::unique_ptr<GrFragmentProcessor>
211GrCoverageCountingPathRenderer::makeClipProcessor(uint32_t opListID, const SkPath& deviceSpacePath,
212 const SkIRect& accessRect, int rtWidth,
213 int rtHeight) {
214 using MustCheckBounds = GrCCPRClipProcessor::MustCheckBounds;
215
216 SkASSERT(!fFlushing);
217 SkASSERT(this->canMakeClipProcessor(deviceSpacePath));
218
219 ClipPath& clipPath = fRTPendingPathsMap[opListID].fClipPaths[deviceSpacePath.getGenerationID()];
220 if (clipPath.isUninitialized()) {
221 // This ClipPath was just created during lookup. Initialize it.
222 clipPath.init(deviceSpacePath, accessRect, rtWidth, rtHeight);
223 } else {
224 clipPath.addAccess(accessRect);
225 }
226
227 bool mustCheckBounds = !clipPath.pathDevIBounds().contains(accessRect);
228 return skstd::make_unique<GrCCPRClipProcessor>(&clipPath, MustCheckBounds(mustCheckBounds),
229 deviceSpacePath.getFillType());
230}
231
232void CCPR::ClipPath::init(const SkPath& deviceSpacePath, const SkIRect& accessRect, int rtWidth,
233 int rtHeight) {
234 SkASSERT(this->isUninitialized());
235
236 fAtlasLazyProxy = GrSurfaceProxy::MakeLazy([this](GrResourceProvider* resourceProvider,
237 GrSurfaceOrigin* outOrigin) {
238 SkASSERT(fHasAtlas);
239 SkASSERT(!fHasAtlasTransform);
240
241 GrTextureProxy* textureProxy = fAtlas ? fAtlas->textureProxy() : nullptr;
242 if (!textureProxy || !textureProxy->instantiate(resourceProvider)) {
243 fAtlasScale = fAtlasTranslate = {0, 0};
244 SkDEBUGCODE(fHasAtlasTransform = true);
245 return sk_sp<GrTexture>();
246 }
247
248 fAtlasScale = {1.f / textureProxy->width(), 1.f / textureProxy->height()};
249 fAtlasTranslate = {fAtlasOffsetX * fAtlasScale.x(), fAtlasOffsetY * fAtlasScale.y()};
250 if (kBottomLeft_GrSurfaceOrigin == textureProxy->origin()) {
251 fAtlasScale.fY = -fAtlasScale.y();
252 fAtlasTranslate.fY = 1 - fAtlasTranslate.y();
253 }
254 SkDEBUGCODE(fHasAtlasTransform = true);
255
256 *outOrigin = textureProxy->origin();
257 return sk_ref_sp(textureProxy->priv().peekTexture());
258 }, GrSurfaceProxy::Renderable::kYes, kAlpha_half_GrPixelConfig);
259
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 Daltonc1e59632017-09-05 00:30:07 -0600274 using PathInstance = GrCCPRPathProcessor::Instance;
275
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 Daltona32a3c32017-12-05 10:05:21 -0700281 SkDEBUGCODE(fFlushing = true;)
282
283 if (fRTPendingPathsMap.empty()) {
284 return; // Nothing to draw.
285 }
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 Daltona32a3c32017-12-05 10:05:21 -0700291 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 Daltonc1e59632017-09-05 00:30:07 -0600323 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 Dalton1a325d22017-07-14 15:17:41 -0600327 fPerFlushIndexBuffer = GrCCPRPathProcessor::FindOrMakeIndexBuffer(onFlushRP);
328 if (!fPerFlushIndexBuffer) {
329 SkDebugf("WARNING: failed to allocate ccpr path index buffer.\n");
330 return;
331 }
332
333 fPerFlushVertexBuffer = GrCCPRPathProcessor::FindOrMakeVertexBuffer(onFlushRP);
334 if (!fPerFlushVertexBuffer) {
335 SkDebugf("WARNING: failed to allocate ccpr path vertex buffer.\n");
336 return;
337 }
338
Chris Dalton1a325d22017-07-14 15:17:41 -0600339 fPerFlushInstanceBuffer = onFlushRP->makeBuffer(kVertex_GrBufferType,
Chris Daltona32a3c32017-12-05 10:05:21 -0700340 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 Daltonc9c97b72017-11-27 15:34:26 -0700350 GrCCPRCoverageOpsBuilder atlasOpsBuilder(maxTotalPaths, maxPathPoints, numSkPoints, numSkVerbs);
Chris Daltonc1e59632017-09-05 00:30:07 -0600351 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();
368 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 Daltonc1e59632017-09-05 00:30:07 -0600384 SkSTArray<4, std::unique_ptr<GrCCPRCoverageOp>> atlasOps(fPerFlushAtlases.count());
385 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 Daltonc1e59632017-09-05 00:30:07 -0600392 GrTAllocator<GrCCPRAtlas>::Iter atlasIter(&fPerFlushAtlases);
393 for (std::unique_ptr<GrCCPRCoverageOp>& atlasOp : atlasOps) {
394 SkAssertResult(atlasIter.next());
395 GrCCPRAtlas* atlas = atlasIter.get();
396 SkASSERT(atlasOp->bounds() == SkRect::MakeIWH(atlas->drawBounds().width(),
397 atlas->drawBounds().height()));
398 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,
408 GrCCPRCoverageOpsBuilder* atlasOpsBuilder,
409 GrCCPRPathProcessor::Instance* pathInstanceData,
410 int pathInstanceIdx) {
411 const GrCCPRAtlas* currentAtlas = nullptr;
412 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;
427 GrCCPRAtlas* atlas = fCCPR->placeParsedPathInAtlas(onFlushRP, draw->fClipIBounds,
428 devIBounds, &offsetX, &offsetY,
429 atlasOpsBuilder);
430 if (!atlas) {
431 SkDEBUGCODE(++fNumSkippedInstances);
432 continue;
433 }
434 if (currentAtlas != atlas) {
435 if (currentAtlas) {
436 this->addAtlasBatch(currentAtlas, pathInstanceIdx);
437 }
438 currentAtlas = atlas;
439 }
440
441 const SkMatrix& m = draw->fMatrix;
442 pathInstanceData[pathInstanceIdx++] = {
443 devBounds,
444 devBounds45,
445 {{m.getScaleX(), m.getSkewY(), m.getSkewX(), m.getScaleY()}},
446 {{m.getTranslateX(), m.getTranslateY()}},
447 {{offsetX, offsetY}},
448 draw->fColor
449 };
450 }
451
452 SkASSERT(pathInstanceIdx == fBaseInstance + fInstanceCount - fNumSkippedInstances);
453 if (currentAtlas) {
454 this->addAtlasBatch(currentAtlas, pathInstanceIdx);
455 }
456
457 return pathInstanceIdx;
458}
459
460void CCPR::ClipPath::placePathInAtlas(GrCoverageCountingPathRenderer* ccpr,
461 GrOnFlushResourceProvider* onFlushRP,
462 GrCCPRCoverageOpsBuilder* atlasOpsBuilder) {
463 SkASSERT(!this->isUninitialized());
464 SkASSERT(!fHasAtlas);
465 atlasOpsBuilder->parseDeviceSpacePath(fDeviceSpacePath);
466 fAtlas = ccpr->placeParsedPathInAtlas(onFlushRP, fAccessRect, fPathDevIBounds, &fAtlasOffsetX,
467 &fAtlasOffsetY, atlasOpsBuilder);
468 SkDEBUGCODE(fHasAtlas = true);
469}
470
471GrCCPRAtlas*
472GrCoverageCountingPathRenderer::placeParsedPathInAtlas(GrOnFlushResourceProvider* onFlushRP,
473 const SkIRect& clipIBounds,
474 const SkIRect& pathIBounds,
475 int16_t* atlasOffsetX,
476 int16_t* atlasOffsetY,
477 GrCCPRCoverageOpsBuilder* atlasOpsBuilder) {
478 using ScissorMode = GrCCPRCoverageOpsBuilder::ScissorMode;
479
480 ScissorMode scissorMode;
481 SkIRect clippedPathIBounds;
482 if (clipIBounds.contains(pathIBounds)) {
483 clippedPathIBounds = pathIBounds;
484 scissorMode = ScissorMode::kNonScissored;
485 } else if (clippedPathIBounds.intersect(clipIBounds, pathIBounds)) {
486 scissorMode = ScissorMode::kScissored;
487 } else {
488 atlasOpsBuilder->discardParsedPath();
489 return nullptr;
490 }
491
492 SkIPoint16 atlasLocation;
493 const int h = clippedPathIBounds.height(), w = clippedPathIBounds.width();
494 if (fPerFlushAtlases.empty() || !fPerFlushAtlases.back().addRect(w, h, &atlasLocation)) {
495 if (!fPerFlushAtlases.empty()) {
496 // The atlas is out of room and can't grow any bigger.
497 atlasOpsBuilder->emitOp(fPerFlushAtlases.back().drawBounds());
498 }
499 fPerFlushAtlases.emplace_back(*onFlushRP->caps(), w, h).addRect(w, h, &atlasLocation);
500 }
501
502 *atlasOffsetX = atlasLocation.x() - static_cast<int16_t>(clippedPathIBounds.left());
503 *atlasOffsetY = atlasLocation.y() - static_cast<int16_t>(clippedPathIBounds.top());
504 atlasOpsBuilder->saveParsedPath(scissorMode, clippedPathIBounds, *atlasOffsetX, *atlasOffsetY);
505
506 return &fPerFlushAtlases.back();
507}
508
509void CCPR::DrawPathsOp::onExecute(GrOpFlushState* flushState) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600510 SkASSERT(fCCPR->fFlushing);
Greg Daniel500d58b2017-08-24 15:59:33 -0400511 SkASSERT(flushState->rtCommandBuffer());
Chris Dalton1a325d22017-07-14 15:17:41 -0600512
Chris Daltonc1e59632017-09-05 00:30:07 -0600513 if (!fCCPR->fPerFlushResourcesAreValid) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600514 return; // Setup failed.
515 }
516
Chris Daltona32a3c32017-12-05 10:05:21 -0700517 SkASSERT(fBaseInstance >= 0); // Make sure setupResources has been called.
Chris Dalton080baa42017-11-06 14:19:19 -0700518
Chris Daltond1513222017-10-06 08:30:46 -0600519 GrPipeline::InitArgs initArgs;
520 initArgs.fFlags = fSRGBFlags;
521 initArgs.fProxy = flushState->drawOpArgs().fProxy;
522 initArgs.fCaps = &flushState->caps();
523 initArgs.fResourceProvider = flushState->resourceProvider();
524 initArgs.fDstProxy = flushState->drawOpArgs().fDstProxy;
525 GrPipeline pipeline(initArgs, std::move(fProcessors), flushState->detachAppliedClip());
Chris Dalton1a325d22017-07-14 15:17:41 -0600526
527 int baseInstance = fBaseInstance;
528
529 for (int i = 0; i < fAtlasBatches.count(); baseInstance = fAtlasBatches[i++].fEndInstanceIdx) {
530 const AtlasBatch& batch = fAtlasBatches[i];
531 SkASSERT(batch.fEndInstanceIdx > baseInstance);
532
533 if (!batch.fAtlas->textureProxy()) {
534 continue; // Atlas failed to allocate.
535 }
536
Chris Daltona32a3c32017-12-05 10:05:21 -0700537 GrCCPRPathProcessor coverProc(flushState->resourceProvider(),
538 sk_ref_sp(batch.fAtlas->textureProxy()), this->getFillType(),
539 *flushState->gpu()->caps()->shaderCaps());
Chris Dalton1a325d22017-07-14 15:17:41 -0600540
541 GrMesh mesh(GrPrimitiveType::kTriangles);
542 mesh.setIndexedInstanced(fCCPR->fPerFlushIndexBuffer.get(),
543 GrCCPRPathProcessor::kPerInstanceIndexCount,
544 fCCPR->fPerFlushInstanceBuffer.get(),
545 batch.fEndInstanceIdx - baseInstance, baseInstance);
546 mesh.setVertexData(fCCPR->fPerFlushVertexBuffer.get());
547
Greg Daniel500d58b2017-08-24 15:59:33 -0400548 flushState->rtCommandBuffer()->draw(pipeline, coverProc, &mesh, nullptr, 1, this->bounds());
Chris Dalton1a325d22017-07-14 15:17:41 -0600549 }
550
Chris Daltona32a3c32017-12-05 10:05:21 -0700551 SkASSERT(baseInstance == fBaseInstance + fInstanceCount - fNumSkippedInstances);
Chris Dalton1a325d22017-07-14 15:17:41 -0600552}
553
Chris Dalton3968ff92017-11-27 12:26:31 -0700554void GrCoverageCountingPathRenderer::postFlush(GrDeferredUploadToken, const uint32_t* opListIDs,
555 int numOpListIDs) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600556 SkASSERT(fFlushing);
557 fPerFlushAtlases.reset();
558 fPerFlushInstanceBuffer.reset();
559 fPerFlushVertexBuffer.reset();
560 fPerFlushIndexBuffer.reset();
Chris Daltona32a3c32017-12-05 10:05:21 -0700561 // We wait to erase these until after flush, once Ops and FPs are done accessing their data.
562 for (int i = 0; i < numOpListIDs; ++i) {
563 fRTPendingPathsMap.erase(opListIDs[i]);
564 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600565 SkDEBUGCODE(fFlushing = false;)
566}