1 | /* |
2 | * Copyright 2018 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 "src/gpu/ccpr/GrCCPerFlushResources.h" |
9 | |
10 | #include "include/private/GrRecordingContext.h" |
11 | #include "src/gpu/GrClip.h" |
12 | #include "src/gpu/GrMemoryPool.h" |
13 | #include "src/gpu/GrOnFlushResourceProvider.h" |
14 | #include "src/gpu/GrRecordingContextPriv.h" |
15 | #include "src/gpu/GrRenderTargetContext.h" |
16 | #include "src/gpu/GrSurfaceContextPriv.h" |
17 | #include "src/gpu/ccpr/GrCCPathCache.h" |
18 | #include "src/gpu/ccpr/GrGSCoverageProcessor.h" |
19 | #include "src/gpu/ccpr/GrSampleMaskProcessor.h" |
20 | #include "src/gpu/ccpr/GrVSCoverageProcessor.h" |
21 | #include "src/gpu/geometry/GrShape.h" |
22 | #include <algorithm> |
23 | |
24 | using CoverageType = GrCCAtlas::CoverageType; |
25 | using FillBatchID = GrCCFiller::BatchID; |
26 | using StrokeBatchID = GrCCStroker::BatchID; |
27 | using PathInstance = GrCCPathProcessor::Instance; |
28 | |
29 | static constexpr int kFillIdx = GrCCPerFlushResourceSpecs::kFillIdx; |
30 | static constexpr int kStrokeIdx = GrCCPerFlushResourceSpecs::kStrokeIdx; |
31 | |
32 | namespace { |
33 | |
34 | // Base class for an Op that renders a CCPR atlas. |
35 | class AtlasOp : public GrDrawOp { |
36 | public: |
37 | FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; } |
38 | GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*, |
39 | bool hasMixedSampledCoverage, GrClampType) override { |
40 | return GrProcessorSet::EmptySetAnalysis(); |
41 | } |
42 | CombineResult onCombineIfPossible(GrOp* other, GrRecordingContext::Arenas*, |
43 | const GrCaps&) override { |
44 | // We will only make multiple copy ops if they have different source proxies. |
45 | // TODO: make use of texture chaining. |
46 | return CombineResult::kCannotCombine; |
47 | } |
48 | |
49 | protected: |
50 | AtlasOp(uint32_t classID, sk_sp<const GrCCPerFlushResources> resources, |
51 | const SkISize& drawBounds) |
52 | : GrDrawOp(classID) |
53 | , fResources(std::move(resources)) { |
54 | this->setBounds(SkRect::MakeIWH(drawBounds.width(), drawBounds.height()), |
55 | GrOp::HasAABloat::kNo, GrOp::IsHairline::kNo); |
56 | } |
57 | |
58 | const sk_sp<const GrCCPerFlushResources> fResources; |
59 | |
60 | private: |
61 | void onPrePrepare(GrRecordingContext*, |
62 | const GrSurfaceProxyView* writeView, |
63 | GrAppliedClip*, |
64 | const GrXferProcessor::DstProxyView&) final {} |
65 | void onPrepare(GrOpFlushState*) final {} |
66 | }; |
67 | |
68 | // Copies paths from a cached coverage count or msaa atlas into an 8-bit literal-coverage atlas. |
69 | class CopyAtlasOp : public AtlasOp { |
70 | public: |
71 | DEFINE_OP_CLASS_ID |
72 | |
73 | static std::unique_ptr<GrDrawOp> Make( |
74 | GrRecordingContext* context, sk_sp<const GrCCPerFlushResources> resources, |
75 | sk_sp<GrTextureProxy> copyProxy, int baseInstance, int endInstance, |
76 | const SkISize& drawBounds) { |
77 | GrOpMemoryPool* pool = context->priv().opMemoryPool(); |
78 | |
79 | return pool->allocate<CopyAtlasOp>(std::move(resources), std::move(copyProxy), baseInstance, |
80 | endInstance, drawBounds); |
81 | } |
82 | |
83 | const char* name() const override { return "CopyAtlasOp (CCPR)" ; } |
84 | |
85 | void visitProxies(const VisitProxyFunc& fn) const override { |
86 | fn(fSrcProxy.get(), GrMipMapped::kNo); |
87 | } |
88 | |
89 | void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override { |
90 | SkASSERT(fSrcProxy); |
91 | SkASSERT(fSrcProxy->isInstantiated()); |
92 | |
93 | auto coverageMode = GrCCAtlas::CoverageTypeToPathCoverageMode( |
94 | fResources->renderedPathCoverageType()); |
95 | GrColorType ct = GrCCAtlas::CoverageTypeToColorType(fResources->renderedPathCoverageType()); |
96 | GrSwizzle swizzle = flushState->caps().getReadSwizzle(fSrcProxy->backendFormat(), ct); |
97 | GrCCPathProcessor pathProc(coverageMode, fSrcProxy->peekTexture(), swizzle, |
98 | GrCCAtlas::kTextureOrigin); |
99 | |
100 | GrPipeline pipeline(GrScissorTest::kDisabled, SkBlendMode::kSrc, |
101 | flushState->drawOpArgs().writeSwizzle()); |
102 | |
103 | pathProc.drawPaths(flushState, pipeline, *fSrcProxy, *fResources, fBaseInstance, |
104 | fEndInstance, this->bounds()); |
105 | } |
106 | |
107 | private: |
108 | friend class ::GrOpMemoryPool; // for ctor |
109 | |
110 | CopyAtlasOp(sk_sp<const GrCCPerFlushResources> resources, sk_sp<GrTextureProxy> srcProxy, |
111 | int baseInstance, int endInstance, const SkISize& drawBounds) |
112 | : AtlasOp(ClassID(), std::move(resources), drawBounds) |
113 | , fSrcProxy(srcProxy) |
114 | , fBaseInstance(baseInstance) |
115 | , fEndInstance(endInstance) { |
116 | } |
117 | sk_sp<GrTextureProxy> fSrcProxy; |
118 | const int fBaseInstance; |
119 | const int fEndInstance; |
120 | }; |
121 | |
122 | // Renders coverage counts to a CCPR atlas using the resources' pre-filled GrCCPathParser. |
123 | template<typename ProcessorType> class RenderAtlasOp : public AtlasOp { |
124 | public: |
125 | DEFINE_OP_CLASS_ID |
126 | |
127 | static std::unique_ptr<GrDrawOp> Make( |
128 | GrRecordingContext* context, sk_sp<const GrCCPerFlushResources> resources, |
129 | FillBatchID fillBatchID, StrokeBatchID strokeBatchID, const SkISize& drawBounds) { |
130 | GrOpMemoryPool* pool = context->priv().opMemoryPool(); |
131 | |
132 | return pool->allocate<RenderAtlasOp>( |
133 | std::move(resources), fillBatchID, strokeBatchID, drawBounds); |
134 | } |
135 | |
136 | // GrDrawOp interface. |
137 | const char* name() const override { return "RenderAtlasOp (CCPR)" ; } |
138 | |
139 | void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override { |
140 | ProcessorType proc; |
141 | GrPipeline pipeline(GrScissorTest::kEnabled, SkBlendMode::kPlus, |
142 | flushState->drawOpArgs().writeSwizzle()); |
143 | fResources->filler().drawFills(flushState, &proc, pipeline, fFillBatchID, fDrawBounds); |
144 | fResources->stroker().drawStrokes(flushState, &proc, fStrokeBatchID, fDrawBounds); |
145 | } |
146 | |
147 | private: |
148 | friend class ::GrOpMemoryPool; // for ctor |
149 | |
150 | RenderAtlasOp(sk_sp<const GrCCPerFlushResources> resources, FillBatchID fillBatchID, |
151 | StrokeBatchID strokeBatchID, const SkISize& drawBounds) |
152 | : AtlasOp(ClassID(), std::move(resources), drawBounds) |
153 | , fFillBatchID(fillBatchID) |
154 | , fStrokeBatchID(strokeBatchID) |
155 | , fDrawBounds(SkIRect::MakeWH(drawBounds.width(), drawBounds.height())) { |
156 | } |
157 | |
158 | const FillBatchID fFillBatchID; |
159 | const StrokeBatchID fStrokeBatchID; |
160 | const SkIRect fDrawBounds; |
161 | }; |
162 | |
163 | } |
164 | |
165 | static int inst_buffer_count(const GrCCPerFlushResourceSpecs& specs) { |
166 | return specs.fNumCachedPaths + |
167 | // Copies get two instances per draw: 1 copy + 1 draw. |
168 | (specs.fNumCopiedPaths[kFillIdx] + specs.fNumCopiedPaths[kStrokeIdx]) * 2 + |
169 | specs.fNumRenderedPaths[kFillIdx] + specs.fNumRenderedPaths[kStrokeIdx]; |
170 | // No clips in instance buffers. |
171 | } |
172 | |
173 | GrCCPerFlushResources::GrCCPerFlushResources( |
174 | GrOnFlushResourceProvider* onFlushRP, CoverageType coverageType, |
175 | const GrCCPerFlushResourceSpecs& specs) |
176 | // Overallocate by one point so we can call Sk4f::Store at the final SkPoint in the array. |
177 | // (See transform_path_pts below.) |
178 | // FIXME: instead use built-in instructions to write only the first two lanes of an Sk4f. |
179 | : fLocalDevPtsBuffer(std::max(specs.fRenderedPathStats[kFillIdx].fMaxPointsPerPath, |
180 | specs.fRenderedPathStats[kStrokeIdx].fMaxPointsPerPath) + 1) |
181 | , fFiller((CoverageType::kFP16_CoverageCount == coverageType) |
182 | ? GrCCFiller::Algorithm::kCoverageCount |
183 | : GrCCFiller::Algorithm::kStencilWindingCount, |
184 | specs.fNumRenderedPaths[kFillIdx] + specs.fNumClipPaths, |
185 | specs.fRenderedPathStats[kFillIdx].fNumTotalSkPoints, |
186 | specs.fRenderedPathStats[kFillIdx].fNumTotalSkVerbs, |
187 | specs.fRenderedPathStats[kFillIdx].fNumTotalConicWeights) |
188 | , fStroker(specs.fNumRenderedPaths[kStrokeIdx], |
189 | specs.fRenderedPathStats[kStrokeIdx].fNumTotalSkPoints, |
190 | specs.fRenderedPathStats[kStrokeIdx].fNumTotalSkVerbs) |
191 | , fCopyAtlasStack(CoverageType::kA8_LiteralCoverage, specs.fCopyAtlasSpecs, |
192 | onFlushRP->caps()) |
193 | , fRenderedAtlasStack(coverageType, specs.fRenderedAtlasSpecs, onFlushRP->caps()) |
194 | , fIndexBuffer(GrCCPathProcessor::FindIndexBuffer(onFlushRP)) |
195 | , fVertexBuffer(GrCCPathProcessor::FindVertexBuffer(onFlushRP)) |
196 | , fNextCopyInstanceIdx(0) |
197 | , fNextPathInstanceIdx( |
198 | specs.fNumCopiedPaths[kFillIdx] + specs.fNumCopiedPaths[kStrokeIdx]) { |
199 | if (!fIndexBuffer) { |
200 | SkDebugf("WARNING: failed to allocate CCPR index buffer. No paths will be drawn.\n" ); |
201 | return; |
202 | } |
203 | if (!fVertexBuffer) { |
204 | SkDebugf("WARNING: failed to allocate CCPR vertex buffer. No paths will be drawn.\n" ); |
205 | return; |
206 | } |
207 | fPathInstanceBuffer.resetAndMapBuffer(onFlushRP, |
208 | inst_buffer_count(specs) * sizeof(PathInstance)); |
209 | if (!fPathInstanceBuffer.gpuBuffer()) { |
210 | SkDebugf("WARNING: failed to allocate CCPR instance buffer. No paths will be drawn.\n" ); |
211 | return; |
212 | } |
213 | |
214 | if (CoverageType::kA8_Multisample == coverageType) { |
215 | int numRenderedPaths = |
216 | specs.fNumRenderedPaths[kFillIdx] + specs.fNumRenderedPaths[kStrokeIdx] + |
217 | specs.fNumClipPaths; |
218 | fStencilResolveBuffer.resetAndMapBuffer( |
219 | onFlushRP, numRenderedPaths * sizeof(GrStencilAtlasOp::ResolveRectInstance)); |
220 | if (!fStencilResolveBuffer.gpuBuffer()) { |
221 | SkDebugf("WARNING: failed to allocate CCPR stencil resolve buffer. " |
222 | "No paths will be drawn.\n" ); |
223 | return; |
224 | } |
225 | SkDEBUGCODE(fEndStencilResolveInstance = numRenderedPaths); |
226 | } |
227 | |
228 | SkDEBUGCODE(fEndCopyInstance = |
229 | specs.fNumCopiedPaths[kFillIdx] + specs.fNumCopiedPaths[kStrokeIdx]); |
230 | SkDEBUGCODE(fEndPathInstance = inst_buffer_count(specs)); |
231 | } |
232 | |
233 | void GrCCPerFlushResources::upgradeEntryToLiteralCoverageAtlas( |
234 | GrCCPathCache* pathCache, GrOnFlushResourceProvider* onFlushRP, GrCCPathCacheEntry* entry, |
235 | GrFillRule fillRule) { |
236 | using ReleaseAtlasResult = GrCCPathCacheEntry::ReleaseAtlasResult; |
237 | SkASSERT(this->isMapped()); |
238 | SkASSERT(fNextCopyInstanceIdx < fEndCopyInstance); |
239 | |
240 | const GrCCCachedAtlas* cachedAtlas = entry->cachedAtlas(); |
241 | SkASSERT(cachedAtlas); |
242 | SkASSERT(cachedAtlas->getOnFlushProxy()); |
243 | |
244 | if (CoverageType::kA8_LiteralCoverage == cachedAtlas->coverageType()) { |
245 | // This entry has already been upgraded to literal coverage. The path must have been drawn |
246 | // multiple times during the flush. |
247 | SkDEBUGCODE(--fEndCopyInstance); |
248 | return; |
249 | } |
250 | |
251 | SkIVector newAtlasOffset; |
252 | if (GrCCAtlas* retiredAtlas = fCopyAtlasStack.addRect(entry->devIBounds(), &newAtlasOffset)) { |
253 | // We did not fit in the previous copy atlas and it was retired. We will render the ranges |
254 | // up until fCopyPathRanges.count() into the retired atlas during finalize(). |
255 | retiredAtlas->setFillBatchID(fCopyPathRanges.count()); |
256 | fCurrCopyAtlasRangesIdx = fCopyPathRanges.count(); |
257 | } |
258 | |
259 | this->recordCopyPathInstance( |
260 | *entry, newAtlasOffset, fillRule, sk_ref_sp(cachedAtlas->getOnFlushProxy())); |
261 | |
262 | sk_sp<GrTexture> previousAtlasTexture = |
263 | sk_ref_sp(cachedAtlas->getOnFlushProxy()->peekTexture()); |
264 | GrCCAtlas* newAtlas = &fCopyAtlasStack.current(); |
265 | if (ReleaseAtlasResult::kDidInvalidateFromCache == |
266 | entry->upgradeToLiteralCoverageAtlas(pathCache, onFlushRP, newAtlas, newAtlasOffset)) { |
267 | // This texture just got booted out of the cache. Keep it around, in case we might be able |
268 | // to recycle it for a new atlas. We can recycle it because copying happens before rendering |
269 | // new paths, and every path from the atlas that we're planning to use this flush will be |
270 | // copied to a new atlas. We'll never copy some and leave others. |
271 | fRecyclableAtlasTextures.push_back(std::move(previousAtlasTexture)); |
272 | } |
273 | } |
274 | |
275 | void GrCCPerFlushResources::recordCopyPathInstance( |
276 | const GrCCPathCacheEntry& entry, const SkIVector& newAtlasOffset, GrFillRule fillRule, |
277 | sk_sp<GrTextureProxy> srcProxy) { |
278 | SkASSERT(fNextCopyInstanceIdx < fEndCopyInstance); |
279 | |
280 | // Write the instance at the back of the array. |
281 | int currentInstanceIdx = fNextCopyInstanceIdx++; |
282 | fPathInstanceBuffer[currentInstanceIdx].set(entry, newAtlasOffset, SK_PMColor4fWHITE, fillRule); |
283 | |
284 | // Percolate the instance forward until it's contiguous with other instances that share the same |
285 | // proxy. |
286 | for (int i = fCopyPathRanges.count() - 1; i >= fCurrCopyAtlasRangesIdx; --i) { |
287 | if (fCopyPathRanges[i].fSrcProxy == srcProxy) { |
288 | ++fCopyPathRanges[i].fCount; |
289 | return; |
290 | } |
291 | int rangeFirstInstanceIdx = currentInstanceIdx - fCopyPathRanges[i].fCount; |
292 | std::swap(fPathInstanceBuffer[rangeFirstInstanceIdx], |
293 | fPathInstanceBuffer[currentInstanceIdx]); |
294 | currentInstanceIdx = rangeFirstInstanceIdx; |
295 | } |
296 | |
297 | // An instance with this particular proxy did not yet exist in the array. Add a range for it, |
298 | // first moving any later ranges back to make space for it at fCurrCopyAtlasRangesIdx. |
299 | fCopyPathRanges.push_back(); |
300 | std::move_backward(fCopyPathRanges.begin() + fCurrCopyAtlasRangesIdx, |
301 | fCopyPathRanges.end() - 1, |
302 | fCopyPathRanges.end()); |
303 | fCopyPathRanges[fCurrCopyAtlasRangesIdx] = {std::move(srcProxy), 1}; |
304 | } |
305 | |
306 | static bool transform_path_pts( |
307 | const SkMatrix& m, const SkPath& path, const SkAutoSTArray<32, SkPoint>& outDevPts, |
308 | GrOctoBounds* octoBounds) { |
309 | const SkPoint* pts = SkPathPriv::PointData(path); |
310 | int numPts = path.countPoints(); |
311 | SkASSERT(numPts + 1 <= outDevPts.count()); |
312 | SkASSERT(numPts); |
313 | |
314 | // m45 transforms path points into "45 degree" device space. A bounding box in this space gives |
315 | // the circumscribing octagon's diagonals. We could use SK_ScalarRoot2Over2, but an orthonormal |
316 | // transform is not necessary as long as the shader uses the correct inverse. |
317 | SkMatrix m45; |
318 | m45.setSinCos(1, 1); |
319 | m45.preConcat(m); |
320 | |
321 | // X,Y,T are two parallel view matrices that accumulate two bounding boxes as they map points: |
322 | // device-space bounds and "45 degree" device-space bounds (| 1 -1 | * devCoords). |
323 | // | 1 1 | |
324 | Sk4f X = Sk4f(m.getScaleX(), m.getSkewY(), m45.getScaleX(), m45.getSkewY()); |
325 | Sk4f Y = Sk4f(m.getSkewX(), m.getScaleY(), m45.getSkewX(), m45.getScaleY()); |
326 | Sk4f T = Sk4f(m.getTranslateX(), m.getTranslateY(), m45.getTranslateX(), m45.getTranslateY()); |
327 | |
328 | // Map the path's points to device space and accumulate bounding boxes. |
329 | Sk4f devPt = SkNx_fma(Y, Sk4f(pts[0].y()), T); |
330 | devPt = SkNx_fma(X, Sk4f(pts[0].x()), devPt); |
331 | Sk4f topLeft = devPt; |
332 | Sk4f bottomRight = devPt; |
333 | |
334 | // Store all 4 values [dev.x, dev.y, dev45.x, dev45.y]. We are only interested in the first two, |
335 | // and will overwrite [dev45.x, dev45.y] with the next point. This is why the dst buffer must |
336 | // be at least one larger than the number of points. |
337 | devPt.store(&outDevPts[0]); |
338 | |
339 | for (int i = 1; i < numPts; ++i) { |
340 | devPt = SkNx_fma(Y, Sk4f(pts[i].y()), T); |
341 | devPt = SkNx_fma(X, Sk4f(pts[i].x()), devPt); |
342 | topLeft = Sk4f::Min(topLeft, devPt); |
343 | bottomRight = Sk4f::Max(bottomRight, devPt); |
344 | devPt.store(&outDevPts[i]); |
345 | } |
346 | |
347 | if (!(Sk4f(0) == topLeft*0).allTrue() || !(Sk4f(0) == bottomRight*0).allTrue()) { |
348 | // The bounds are infinite or NaN. |
349 | return false; |
350 | } |
351 | |
352 | SkPoint topLeftPts[2], bottomRightPts[2]; |
353 | topLeft.store(topLeftPts); |
354 | bottomRight.store(bottomRightPts); |
355 | |
356 | const SkRect& devBounds = SkRect::MakeLTRB( |
357 | topLeftPts[0].x(), topLeftPts[0].y(), bottomRightPts[0].x(), bottomRightPts[0].y()); |
358 | const SkRect& devBounds45 = SkRect::MakeLTRB( |
359 | topLeftPts[1].x(), topLeftPts[1].y(), bottomRightPts[1].x(), bottomRightPts[1].y()); |
360 | |
361 | octoBounds->set(devBounds, devBounds45); |
362 | return true; |
363 | } |
364 | |
365 | GrCCAtlas* GrCCPerFlushResources::renderShapeInAtlas( |
366 | const SkIRect& clipIBounds, const SkMatrix& m, const GrShape& shape, float strokeDevWidth, |
367 | GrOctoBounds* octoBounds, SkIRect* devIBounds, SkIVector* devToAtlasOffset) { |
368 | SkASSERT(this->isMapped()); |
369 | SkASSERT(fNextPathInstanceIdx < fEndPathInstance); |
370 | |
371 | SkPath path; |
372 | shape.asPath(&path); |
373 | if (path.isEmpty()) { |
374 | SkDEBUGCODE(--fEndPathInstance); |
375 | SkDEBUGCODE(--fEndStencilResolveInstance); |
376 | return nullptr; |
377 | } |
378 | if (!transform_path_pts(m, path, fLocalDevPtsBuffer, octoBounds)) { |
379 | // The transformed path had infinite or NaN bounds. |
380 | SkDEBUGCODE(--fEndPathInstance); |
381 | SkDEBUGCODE(--fEndStencilResolveInstance); |
382 | return nullptr; |
383 | } |
384 | |
385 | const SkStrokeRec& stroke = shape.style().strokeRec(); |
386 | if (!stroke.isFillStyle()) { |
387 | float r = SkStrokeRec::GetInflationRadius( |
388 | stroke.getJoin(), stroke.getMiter(), stroke.getCap(), strokeDevWidth); |
389 | octoBounds->outset(r); |
390 | } |
391 | |
392 | GrScissorTest enableScissorInAtlas; |
393 | if (clipIBounds.contains(octoBounds->bounds())) { |
394 | enableScissorInAtlas = GrScissorTest::kDisabled; |
395 | } else if (octoBounds->clip(clipIBounds)) { |
396 | enableScissorInAtlas = GrScissorTest::kEnabled; |
397 | } else { |
398 | // The clip and octo bounds do not intersect. Draw nothing. |
399 | SkDEBUGCODE(--fEndPathInstance); |
400 | SkDEBUGCODE(--fEndStencilResolveInstance); |
401 | return nullptr; |
402 | } |
403 | octoBounds->roundOut(devIBounds); |
404 | SkASSERT(clipIBounds.contains(*devIBounds)); |
405 | |
406 | this->placeRenderedPathInAtlas(*devIBounds, enableScissorInAtlas, devToAtlasOffset); |
407 | |
408 | GrFillRule fillRule; |
409 | if (stroke.isFillStyle()) { |
410 | SkASSERT(0 == strokeDevWidth); |
411 | fFiller.parseDeviceSpaceFill(path, fLocalDevPtsBuffer.begin(), enableScissorInAtlas, |
412 | *devIBounds, *devToAtlasOffset); |
413 | fillRule = GrFillRuleForSkPath(path); |
414 | } else { |
415 | // Stroke-and-fill is not yet supported. |
416 | SkASSERT(SkStrokeRec::kStroke_Style == stroke.getStyle() || stroke.isHairlineStyle()); |
417 | SkASSERT(!stroke.isHairlineStyle() || 1 == strokeDevWidth); |
418 | fStroker.parseDeviceSpaceStroke( |
419 | path, fLocalDevPtsBuffer.begin(), stroke, strokeDevWidth, enableScissorInAtlas, |
420 | *devIBounds, *devToAtlasOffset); |
421 | fillRule = GrFillRule::kNonzero; |
422 | } |
423 | |
424 | if (GrCCAtlas::CoverageType::kA8_Multisample == this->renderedPathCoverageType()) { |
425 | this->recordStencilResolveInstance(*devIBounds, *devToAtlasOffset, fillRule); |
426 | } |
427 | |
428 | return &fRenderedAtlasStack.current(); |
429 | } |
430 | |
431 | const GrCCAtlas* GrCCPerFlushResources::renderDeviceSpacePathInAtlas( |
432 | const SkIRect& clipIBounds, const SkPath& devPath, const SkIRect& devPathIBounds, |
433 | GrFillRule fillRule, SkIVector* devToAtlasOffset) { |
434 | SkASSERT(this->isMapped()); |
435 | |
436 | if (devPath.isEmpty()) { |
437 | SkDEBUGCODE(--fEndStencilResolveInstance); |
438 | return nullptr; |
439 | } |
440 | |
441 | GrScissorTest enableScissorInAtlas; |
442 | SkIRect clippedPathIBounds; |
443 | if (clipIBounds.contains(devPathIBounds)) { |
444 | clippedPathIBounds = devPathIBounds; |
445 | enableScissorInAtlas = GrScissorTest::kDisabled; |
446 | } else if (clippedPathIBounds.intersect(clipIBounds, devPathIBounds)) { |
447 | enableScissorInAtlas = GrScissorTest::kEnabled; |
448 | } else { |
449 | // The clip and path bounds do not intersect. Draw nothing. |
450 | SkDEBUGCODE(--fEndStencilResolveInstance); |
451 | return nullptr; |
452 | } |
453 | |
454 | this->placeRenderedPathInAtlas(clippedPathIBounds, enableScissorInAtlas, devToAtlasOffset); |
455 | fFiller.parseDeviceSpaceFill(devPath, SkPathPriv::PointData(devPath), enableScissorInAtlas, |
456 | clippedPathIBounds, *devToAtlasOffset); |
457 | |
458 | // In MSAA mode we also record an internal draw instance that will be used to resolve stencil |
459 | // winding values to coverage when the atlas is generated. |
460 | if (GrCCAtlas::CoverageType::kA8_Multisample == this->renderedPathCoverageType()) { |
461 | this->recordStencilResolveInstance(clippedPathIBounds, *devToAtlasOffset, fillRule); |
462 | } |
463 | |
464 | return &fRenderedAtlasStack.current(); |
465 | } |
466 | |
467 | void GrCCPerFlushResources::placeRenderedPathInAtlas( |
468 | const SkIRect& clippedPathIBounds, GrScissorTest scissorTest, SkIVector* devToAtlasOffset) { |
469 | if (GrCCAtlas* retiredAtlas = |
470 | fRenderedAtlasStack.addRect(clippedPathIBounds, devToAtlasOffset)) { |
471 | // We did not fit in the previous coverage count atlas and it was retired. Close the path |
472 | // parser's current batch (which does not yet include the path we just parsed). We will |
473 | // render this batch into the retired atlas during finalize(). |
474 | retiredAtlas->setFillBatchID(fFiller.closeCurrentBatch()); |
475 | retiredAtlas->setStrokeBatchID(fStroker.closeCurrentBatch()); |
476 | retiredAtlas->setEndStencilResolveInstance(fNextStencilResolveInstanceIdx); |
477 | } |
478 | } |
479 | |
480 | void GrCCPerFlushResources::recordStencilResolveInstance( |
481 | const SkIRect& clippedPathIBounds, const SkIVector& devToAtlasOffset, GrFillRule fillRule) { |
482 | SkASSERT(GrCCAtlas::CoverageType::kA8_Multisample == this->renderedPathCoverageType()); |
483 | SkASSERT(fNextStencilResolveInstanceIdx < fEndStencilResolveInstance); |
484 | |
485 | SkIRect atlasIBounds = clippedPathIBounds.makeOffset(devToAtlasOffset); |
486 | if (GrFillRule::kEvenOdd == fillRule) { |
487 | // Make even/odd fills counterclockwise. The resolve draw uses two-sided stencil, with |
488 | // "nonzero" settings in front and "even/odd" settings in back. |
489 | std::swap(atlasIBounds.fLeft, atlasIBounds.fRight); |
490 | } |
491 | fStencilResolveBuffer[fNextStencilResolveInstanceIdx++] = { |
492 | (int16_t)atlasIBounds.left(), (int16_t)atlasIBounds.top(), |
493 | (int16_t)atlasIBounds.right(), (int16_t)atlasIBounds.bottom()}; |
494 | } |
495 | |
496 | bool GrCCPerFlushResources::finalize(GrOnFlushResourceProvider* onFlushRP) { |
497 | SkASSERT(this->isMapped()); |
498 | SkASSERT(fNextPathInstanceIdx == fEndPathInstance); |
499 | SkASSERT(fNextCopyInstanceIdx == fEndCopyInstance); |
500 | SkASSERT(GrCCAtlas::CoverageType::kA8_Multisample != this->renderedPathCoverageType() || |
501 | fNextStencilResolveInstanceIdx == fEndStencilResolveInstance); |
502 | |
503 | fPathInstanceBuffer.unmapBuffer(); |
504 | |
505 | if (fStencilResolveBuffer.gpuBuffer()) { |
506 | fStencilResolveBuffer.unmapBuffer(); |
507 | } |
508 | |
509 | if (!fCopyAtlasStack.empty()) { |
510 | fCopyAtlasStack.current().setFillBatchID(fCopyPathRanges.count()); |
511 | fCurrCopyAtlasRangesIdx = fCopyPathRanges.count(); |
512 | } |
513 | if (!fRenderedAtlasStack.empty()) { |
514 | fRenderedAtlasStack.current().setFillBatchID(fFiller.closeCurrentBatch()); |
515 | fRenderedAtlasStack.current().setStrokeBatchID(fStroker.closeCurrentBatch()); |
516 | fRenderedAtlasStack.current().setEndStencilResolveInstance(fNextStencilResolveInstanceIdx); |
517 | } |
518 | |
519 | // Build the GPU buffers to render path coverage counts. (This must not happen until after the |
520 | // final calls to fFiller/fStroker.closeCurrentBatch().) |
521 | if (!fFiller.prepareToDraw(onFlushRP)) { |
522 | return false; |
523 | } |
524 | if (!fStroker.prepareToDraw(onFlushRP)) { |
525 | return false; |
526 | } |
527 | |
528 | // Draw the copies from coverage count or msaa atlas(es) into 8-bit cached atlas(es). |
529 | int copyRangeIdx = 0; |
530 | int baseCopyInstance = 0; |
531 | for (GrCCAtlas& atlas : fCopyAtlasStack.atlases()) { |
532 | int endCopyRange = atlas.getFillBatchID(); |
533 | SkASSERT(endCopyRange > copyRangeIdx); |
534 | |
535 | auto rtc = atlas.instantiate(onFlushRP); |
536 | for (; copyRangeIdx < endCopyRange; ++copyRangeIdx) { |
537 | const CopyPathRange& copyRange = fCopyPathRanges[copyRangeIdx]; |
538 | int endCopyInstance = baseCopyInstance + copyRange.fCount; |
539 | if (rtc) { |
540 | auto op = CopyAtlasOp::Make( |
541 | rtc->surfPriv().getContext(), sk_ref_sp(this), copyRange.fSrcProxy, |
542 | baseCopyInstance, endCopyInstance, atlas.drawBounds()); |
543 | rtc->addDrawOp(GrNoClip(), std::move(op)); |
544 | } |
545 | baseCopyInstance = endCopyInstance; |
546 | } |
547 | } |
548 | SkASSERT(fCopyPathRanges.count() == copyRangeIdx); |
549 | SkASSERT(fNextCopyInstanceIdx == baseCopyInstance); |
550 | SkASSERT(baseCopyInstance == fEndCopyInstance); |
551 | |
552 | // Render the coverage count atlas(es). |
553 | int baseStencilResolveInstance = 0; |
554 | for (GrCCAtlas& atlas : fRenderedAtlasStack.atlases()) { |
555 | // Copies will be finished by the time we get to rendering new atlases. See if we can |
556 | // recycle any previous invalidated atlas textures instead of creating new ones. |
557 | sk_sp<GrTexture> backingTexture; |
558 | for (sk_sp<GrTexture>& texture : fRecyclableAtlasTextures) { |
559 | if (texture && atlas.currentHeight() == texture->height() && |
560 | atlas.currentWidth() == texture->width()) { |
561 | backingTexture = skstd::exchange(texture, nullptr); |
562 | break; |
563 | } |
564 | } |
565 | |
566 | if (auto rtc = atlas.instantiate(onFlushRP, std::move(backingTexture))) { |
567 | std::unique_ptr<GrDrawOp> op; |
568 | if (CoverageType::kA8_Multisample == fRenderedAtlasStack.coverageType()) { |
569 | op = GrStencilAtlasOp::Make( |
570 | rtc->surfPriv().getContext(), sk_ref_sp(this), atlas.getFillBatchID(), |
571 | atlas.getStrokeBatchID(), baseStencilResolveInstance, |
572 | atlas.getEndStencilResolveInstance(), atlas.drawBounds()); |
573 | } else if (onFlushRP->caps()->shaderCaps()->geometryShaderSupport()) { |
574 | op = RenderAtlasOp<GrGSCoverageProcessor>::Make( |
575 | rtc->surfPriv().getContext(), sk_ref_sp(this), atlas.getFillBatchID(), |
576 | atlas.getStrokeBatchID(), atlas.drawBounds()); |
577 | } else { |
578 | op = RenderAtlasOp<GrVSCoverageProcessor>::Make( |
579 | rtc->surfPriv().getContext(), sk_ref_sp(this), atlas.getFillBatchID(), |
580 | atlas.getStrokeBatchID(), atlas.drawBounds()); |
581 | } |
582 | rtc->addDrawOp(GrNoClip(), std::move(op)); |
583 | if (rtc->asSurfaceProxy()->requiresManualMSAAResolve()) { |
584 | onFlushRP->addTextureResolveTask(sk_ref_sp(rtc->asTextureProxy()), |
585 | GrSurfaceProxy::ResolveFlags::kMSAA); |
586 | } |
587 | } |
588 | |
589 | SkASSERT(atlas.getEndStencilResolveInstance() >= baseStencilResolveInstance); |
590 | baseStencilResolveInstance = atlas.getEndStencilResolveInstance(); |
591 | } |
592 | SkASSERT(GrCCAtlas::CoverageType::kA8_Multisample != this->renderedPathCoverageType() || |
593 | baseStencilResolveInstance == fEndStencilResolveInstance); |
594 | |
595 | return true; |
596 | } |
597 | |
598 | void GrCCPerFlushResourceSpecs::cancelCopies() { |
599 | // Convert copies to cached draws. |
600 | fNumCachedPaths += fNumCopiedPaths[kFillIdx] + fNumCopiedPaths[kStrokeIdx]; |
601 | fNumCopiedPaths[kFillIdx] = fNumCopiedPaths[kStrokeIdx] = 0; |
602 | fCopyPathStats[kFillIdx] = fCopyPathStats[kStrokeIdx] = GrCCRenderedPathStats(); |
603 | fCopyAtlasSpecs = GrCCAtlas::Specs(); |
604 | } |
605 | |