blob: 327a9444b0aae73837105e356b20379a0c023818 [file] [log] [blame]
Chris Dalton5ba36ba2018-05-09 01:08:38 -06001/*
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 "GrCCPerFlushResources.h"
9
Chris Dalton9414c962018-06-14 10:14:50 -060010#include "GrClip.h"
11#include "GrMemoryPool.h"
Chris Dalton5ba36ba2018-05-09 01:08:38 -060012#include "GrOnFlushResourceProvider.h"
Robert Phillipsbe9aff22019-02-15 11:33:22 -050013#include "GrRecordingContext.h"
14#include "GrRecordingContextPriv.h"
Chris Dalton5ba36ba2018-05-09 01:08:38 -060015#include "GrRenderTargetContext.h"
Chris Dalton09a7bb22018-08-31 19:53:15 +080016#include "GrShape.h"
Robert Phillipsbe9aff22019-02-15 11:33:22 -050017#include "GrSurfaceContextPriv.h"
Chris Dalton9414c962018-06-14 10:14:50 -060018#include "SkMakeUnique.h"
Chris Dalton4da70192018-06-18 09:51:36 -060019#include "ccpr/GrCCPathCache.h"
Chris Dalton5ba36ba2018-05-09 01:08:38 -060020
Chris Daltone1639692018-08-20 14:00:30 -060021using FillBatchID = GrCCFiller::BatchID;
Chris Dalton09a7bb22018-08-31 19:53:15 +080022using StrokeBatchID = GrCCStroker::BatchID;
Chris Dalton5ba36ba2018-05-09 01:08:38 -060023using PathInstance = GrCCPathProcessor::Instance;
24
Chris Dalton09a7bb22018-08-31 19:53:15 +080025static constexpr int kFillIdx = GrCCPerFlushResourceSpecs::kFillIdx;
26static constexpr int kStrokeIdx = GrCCPerFlushResourceSpecs::kStrokeIdx;
27
Chris Dalton9414c962018-06-14 10:14:50 -060028namespace {
29
Chris Dalton4da70192018-06-18 09:51:36 -060030// Base class for an Op that renders a CCPR atlas.
31class AtlasOp : public GrDrawOp {
32public:
33 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
Chris Dalton4b62aed2019-01-15 11:53:00 -070034 GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*) override {
35 return GrProcessorSet::EmptySetAnalysis();
Brian Osman532b3f92018-07-11 10:02:07 -040036 }
Brian Salomon7eae3e02018-08-07 14:02:38 +000037 CombineResult onCombineIfPossible(GrOp* other, const GrCaps&) override {
Chris Dalton351e80c2019-01-06 22:51:00 -070038 // We will only make multiple copy ops if they have different source proxies.
39 // TODO: make use of texture chaining.
40 return CombineResult::kCannotCombine;
Chris Dalton4da70192018-06-18 09:51:36 -060041 }
42 void onPrepare(GrOpFlushState*) override {}
43
44protected:
45 AtlasOp(uint32_t classID, sk_sp<const GrCCPerFlushResources> resources,
46 const SkISize& drawBounds)
47 : GrDrawOp(classID)
48 , fResources(std::move(resources)) {
49 this->setBounds(SkRect::MakeIWH(drawBounds.width(), drawBounds.height()),
50 GrOp::HasAABloat::kNo, GrOp::IsZeroArea::kNo);
51 }
52
53 const sk_sp<const GrCCPerFlushResources> fResources;
54};
55
Chris Dalton351e80c2019-01-06 22:51:00 -070056// Copies paths from a cached coverage count atlas into an 8-bit literal-coverage atlas.
Chris Dalton4da70192018-06-18 09:51:36 -060057class CopyAtlasOp : public AtlasOp {
58public:
59 DEFINE_OP_CLASS_ID
60
Robert Phillipsbe9aff22019-02-15 11:33:22 -050061 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Chris Dalton4da70192018-06-18 09:51:36 -060062 sk_sp<const GrCCPerFlushResources> resources,
63 sk_sp<GrTextureProxy> copyProxy, int baseInstance,
64 int endInstance, const SkISize& drawBounds) {
Robert Phillips9da87e02019-02-04 13:26:26 -050065 GrOpMemoryPool* pool = context->priv().opMemoryPool();
Robert Phillipsc994a932018-06-19 13:09:54 -040066
67 return pool->allocate<CopyAtlasOp>(std::move(resources), std::move(copyProxy),
68 baseInstance, endInstance, drawBounds);
Chris Dalton4da70192018-06-18 09:51:36 -060069 }
70
71 const char* name() const override { return "CopyAtlasOp (CCPR)"; }
Chris Dalton351e80c2019-01-06 22:51:00 -070072 void visitProxies(const VisitProxyFunc& fn, VisitorType) const override { fn(fSrcProxy.get()); }
Chris Dalton4da70192018-06-18 09:51:36 -060073
Brian Salomon588cec72018-11-14 13:56:37 -050074 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Chris Dalton351e80c2019-01-06 22:51:00 -070075 SkASSERT(fSrcProxy);
Brian Salomon7eae3e02018-08-07 14:02:38 +000076 GrPipeline::FixedDynamicState dynamicState;
Chris Dalton351e80c2019-01-06 22:51:00 -070077 auto srcProxy = fSrcProxy.get();
78 dynamicState.fPrimitiveProcessorTextures = &srcProxy;
Brian Salomon7eae3e02018-08-07 14:02:38 +000079
Robert Phillipsd0fe8752019-01-31 14:13:59 -050080 GrPipeline pipeline(GrScissorTest::kDisabled, SkBlendMode::kSrc);
Chris Dalton351e80c2019-01-06 22:51:00 -070081 GrCCPathProcessor pathProc(srcProxy);
Brian Salomon7eae3e02018-08-07 14:02:38 +000082 pathProc.drawPaths(flushState, pipeline, &dynamicState, *fResources, fBaseInstance,
83 fEndInstance, this->bounds());
Chris Dalton4da70192018-06-18 09:51:36 -060084 }
85
86private:
87 friend class ::GrOpMemoryPool; // for ctor
88
Chris Dalton351e80c2019-01-06 22:51:00 -070089 CopyAtlasOp(sk_sp<const GrCCPerFlushResources> resources, sk_sp<GrTextureProxy> srcProxy,
Chris Dalton4da70192018-06-18 09:51:36 -060090 int baseInstance, int endInstance, const SkISize& drawBounds)
91 : AtlasOp(ClassID(), std::move(resources), drawBounds)
Chris Dalton351e80c2019-01-06 22:51:00 -070092 , fSrcProxy(srcProxy)
Chris Dalton4da70192018-06-18 09:51:36 -060093 , fBaseInstance(baseInstance)
94 , fEndInstance(endInstance) {
95 }
Chris Dalton351e80c2019-01-06 22:51:00 -070096 sk_sp<GrTextureProxy> fSrcProxy;
Chris Dalton4da70192018-06-18 09:51:36 -060097 const int fBaseInstance;
98 const int fEndInstance;
99};
100
Chris Dalton9414c962018-06-14 10:14:50 -0600101// Renders coverage counts to a CCPR atlas using the resources' pre-filled GrCCPathParser.
Chris Dalton4da70192018-06-18 09:51:36 -0600102class RenderAtlasOp : public AtlasOp {
Chris Dalton9414c962018-06-14 10:14:50 -0600103public:
104 DEFINE_OP_CLASS_ID
105
Robert Phillipsbe9aff22019-02-15 11:33:22 -0500106 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Chris Dalton9414c962018-06-14 10:14:50 -0600107 sk_sp<const GrCCPerFlushResources> resources,
Chris Dalton09a7bb22018-08-31 19:53:15 +0800108 FillBatchID fillBatchID, StrokeBatchID strokeBatchID,
109 const SkISize& drawBounds) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500110 GrOpMemoryPool* pool = context->priv().opMemoryPool();
Robert Phillipsc994a932018-06-19 13:09:54 -0400111
Chris Dalton09a7bb22018-08-31 19:53:15 +0800112 return pool->allocate<RenderAtlasOp>(std::move(resources), fillBatchID, strokeBatchID,
113 drawBounds);
Chris Dalton9414c962018-06-14 10:14:50 -0600114 }
115
116 // GrDrawOp interface.
117 const char* name() const override { return "RenderAtlasOp (CCPR)"; }
Chris Dalton9414c962018-06-14 10:14:50 -0600118
Brian Salomon588cec72018-11-14 13:56:37 -0500119 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Chris Dalton09a7bb22018-08-31 19:53:15 +0800120 fResources->filler().drawFills(flushState, fFillBatchID, fDrawBounds);
121 fResources->stroker().drawStrokes(flushState, fStrokeBatchID, fDrawBounds);
Chris Dalton9414c962018-06-14 10:14:50 -0600122 }
123
124private:
125 friend class ::GrOpMemoryPool; // for ctor
126
Chris Dalton09a7bb22018-08-31 19:53:15 +0800127 RenderAtlasOp(sk_sp<const GrCCPerFlushResources> resources, FillBatchID fillBatchID,
128 StrokeBatchID strokeBatchID, const SkISize& drawBounds)
Chris Dalton4da70192018-06-18 09:51:36 -0600129 : AtlasOp(ClassID(), std::move(resources), drawBounds)
Chris Dalton09a7bb22018-08-31 19:53:15 +0800130 , fFillBatchID(fillBatchID)
131 , fStrokeBatchID(strokeBatchID)
Chris Dalton9414c962018-06-14 10:14:50 -0600132 , fDrawBounds(SkIRect::MakeWH(drawBounds.width(), drawBounds.height())) {
Chris Dalton9414c962018-06-14 10:14:50 -0600133 }
134
Chris Dalton09a7bb22018-08-31 19:53:15 +0800135 const FillBatchID fFillBatchID;
136 const StrokeBatchID fStrokeBatchID;
Chris Dalton9414c962018-06-14 10:14:50 -0600137 const SkIRect fDrawBounds;
138};
139
140}
141
Chris Dalton4da70192018-06-18 09:51:36 -0600142static int inst_buffer_count(const GrCCPerFlushResourceSpecs& specs) {
143 return specs.fNumCachedPaths +
Chris Dalton09a7bb22018-08-31 19:53:15 +0800144 // Copies get two instances per draw: 1 copy + 1 draw.
145 (specs.fNumCopiedPaths[kFillIdx] + specs.fNumCopiedPaths[kStrokeIdx]) * 2 +
146 specs.fNumRenderedPaths[kFillIdx] + specs.fNumRenderedPaths[kStrokeIdx];
147 // No clips in instance buffers.
Chris Dalton4da70192018-06-18 09:51:36 -0600148}
149
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600150GrCCPerFlushResources::GrCCPerFlushResources(GrOnFlushResourceProvider* onFlushRP,
Chris Dalton42c21152018-06-13 15:28:19 -0600151 const GrCCPerFlushResourceSpecs& specs)
Brian Salomonae64c192019-02-05 09:41:37 -0500152 // Overallocate by one point so we can call Sk4f::Store at the final SkPoint in the array.
153 // (See transform_path_pts below.)
154 // FIXME: instead use built-in instructions to write only the first two lanes of an Sk4f.
Chris Dalton09a7bb22018-08-31 19:53:15 +0800155 : fLocalDevPtsBuffer(SkTMax(specs.fRenderedPathStats[kFillIdx].fMaxPointsPerPath,
156 specs.fRenderedPathStats[kStrokeIdx].fMaxPointsPerPath) + 1)
157 , fFiller(specs.fNumRenderedPaths[kFillIdx] + specs.fNumClipPaths,
158 specs.fRenderedPathStats[kFillIdx].fNumTotalSkPoints,
159 specs.fRenderedPathStats[kFillIdx].fNumTotalSkVerbs,
160 specs.fRenderedPathStats[kFillIdx].fNumTotalConicWeights)
161 , fStroker(specs.fNumRenderedPaths[kStrokeIdx],
162 specs.fRenderedPathStats[kStrokeIdx].fNumTotalSkPoints,
163 specs.fRenderedPathStats[kStrokeIdx].fNumTotalSkVerbs)
Chris Dalton351e80c2019-01-06 22:51:00 -0700164 , fCopyAtlasStack(GrCCAtlas::CoverageType::kA8_LiteralCoverage, specs.fCopyAtlasSpecs,
165 onFlushRP->caps())
166 , fRenderedAtlasStack(GrCCAtlas::CoverageType::kFP16_CoverageCount,
167 specs.fRenderedAtlasSpecs, onFlushRP->caps())
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600168 , fIndexBuffer(GrCCPathProcessor::FindIndexBuffer(onFlushRP))
169 , fVertexBuffer(GrCCPathProcessor::FindVertexBuffer(onFlushRP))
Brian Salomonae64c192019-02-05 09:41:37 -0500170 , fInstanceBuffer(onFlushRP->makeBuffer(GrGpuBufferType::kVertex,
Chris Dalton4da70192018-06-18 09:51:36 -0600171 inst_buffer_count(specs) * sizeof(PathInstance)))
172 , fNextCopyInstanceIdx(0)
Chris Dalton09a7bb22018-08-31 19:53:15 +0800173 , fNextPathInstanceIdx(specs.fNumCopiedPaths[kFillIdx] +
174 specs.fNumCopiedPaths[kStrokeIdx]) {
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600175 if (!fIndexBuffer) {
176 SkDebugf("WARNING: failed to allocate CCPR index buffer. No paths will be drawn.\n");
177 return;
178 }
179 if (!fVertexBuffer) {
180 SkDebugf("WARNING: failed to allocate CCPR vertex buffer. No paths will be drawn.\n");
181 return;
182 }
183 if (!fInstanceBuffer) {
184 SkDebugf("WARNING: failed to allocate CCPR instance buffer. No paths will be drawn.\n");
185 return;
186 }
187 fPathInstanceData = static_cast<PathInstance*>(fInstanceBuffer->map());
188 SkASSERT(fPathInstanceData);
Chris Dalton09a7bb22018-08-31 19:53:15 +0800189 SkDEBUGCODE(fEndCopyInstance =
190 specs.fNumCopiedPaths[kFillIdx] + specs.fNumCopiedPaths[kStrokeIdx]);
Chris Dalton4da70192018-06-18 09:51:36 -0600191 SkDEBUGCODE(fEndPathInstance = inst_buffer_count(specs));
192}
193
Chris Dalton351e80c2019-01-06 22:51:00 -0700194void GrCCPerFlushResources::upgradeEntryToLiteralCoverageAtlas(
195 GrCCPathCache* pathCache, GrOnFlushResourceProvider* onFlushRP, GrCCPathCacheEntry* entry,
196 GrCCPathProcessor::DoEvenOddFill evenOdd) {
197 using ReleaseAtlasResult = GrCCPathCacheEntry::ReleaseAtlasResult;
Chris Dalton4da70192018-06-18 09:51:36 -0600198 SkASSERT(this->isMapped());
199 SkASSERT(fNextCopyInstanceIdx < fEndCopyInstance);
Chris Dalton4da70192018-06-18 09:51:36 -0600200
Chris Dalton351e80c2019-01-06 22:51:00 -0700201 const GrCCCachedAtlas* cachedAtlas = entry->cachedAtlas();
202 SkASSERT(cachedAtlas);
203 SkASSERT(cachedAtlas->getOnFlushProxy());
204
205 if (GrCCAtlas::CoverageType::kA8_LiteralCoverage == cachedAtlas->coverageType()) {
206 // This entry has already been upgraded to literal coverage. The path must have been drawn
207 // multiple times during the flush.
208 SkDEBUGCODE(--fEndCopyInstance);
209 return;
Chris Dalton4da70192018-06-18 09:51:36 -0600210 }
211
Chris Dalton351e80c2019-01-06 22:51:00 -0700212 SkIVector newAtlasOffset;
213 if (GrCCAtlas* retiredAtlas = fCopyAtlasStack.addRect(entry->devIBounds(), &newAtlasOffset)) {
214 // We did not fit in the previous copy atlas and it was retired. We will render the ranges
215 // up until fCopyPathRanges.count() into the retired atlas during finalize().
216 retiredAtlas->setFillBatchID(fCopyPathRanges.count());
217 fCurrCopyAtlasRangesIdx = fCopyPathRanges.count();
218 }
219
220 this->recordCopyPathInstance(*entry, newAtlasOffset, evenOdd,
221 sk_ref_sp(cachedAtlas->getOnFlushProxy()));
222
223 sk_sp<GrTexture> previousAtlasTexture =
224 sk_ref_sp(cachedAtlas->getOnFlushProxy()->peekTexture());
225 GrCCAtlas* newAtlas = &fCopyAtlasStack.current();
226 if (ReleaseAtlasResult::kDidInvalidateFromCache ==
227 entry->upgradeToLiteralCoverageAtlas(pathCache, onFlushRP, newAtlas, newAtlasOffset)) {
228 // This texture just got booted out of the cache. Keep it around, in case we might be able
229 // to recycle it for a new atlas. We can recycle it because copying happens before rendering
230 // new paths, and every path from the atlas that we're planning to use this flush will be
231 // copied to a new atlas. We'll never copy some and leave others.
232 fRecyclableAtlasTextures.push_back(std::move(previousAtlasTexture));
233 }
234}
235
236template<typename T, typename... Args>
237static void emplace_at_memcpy(SkTArray<T>* array, int idx, Args&&... args) {
238 if (int moveCount = array->count() - idx) {
239 array->push_back();
240 T* location = array->begin() + idx;
241 memcpy(location+1, location, moveCount * sizeof(T));
242 new (location) T(std::forward<Args>(args)...);
243 } else {
244 array->emplace_back(std::forward<Args>(args)...);
245 }
246}
247
248void GrCCPerFlushResources::recordCopyPathInstance(const GrCCPathCacheEntry& entry,
249 const SkIVector& newAtlasOffset,
250 GrCCPathProcessor::DoEvenOddFill evenOdd,
251 sk_sp<GrTextureProxy> srcProxy) {
252 SkASSERT(fNextCopyInstanceIdx < fEndCopyInstance);
253
254 // Write the instance at the back of the array.
255 int currentInstanceIdx = fNextCopyInstanceIdx++;
Brian Osmanc6444d22019-01-09 16:30:12 -0500256 constexpr uint64_t kWhite = (((uint64_t) SK_Half1) << 0) |
257 (((uint64_t) SK_Half1) << 16) |
258 (((uint64_t) SK_Half1) << 32) |
259 (((uint64_t) SK_Half1) << 48);
260 fPathInstanceData[currentInstanceIdx].set(entry, newAtlasOffset, kWhite, evenOdd);
Chris Dalton351e80c2019-01-06 22:51:00 -0700261
262 // Percolate the instance forward until it's contiguous with other instances that share the same
263 // proxy.
264 for (int i = fCopyPathRanges.count() - 1; i >= fCurrCopyAtlasRangesIdx; --i) {
265 if (fCopyPathRanges[i].fSrcProxy == srcProxy) {
266 ++fCopyPathRanges[i].fCount;
267 return;
268 }
269 int rangeFirstInstanceIdx = currentInstanceIdx - fCopyPathRanges[i].fCount;
270 std::swap(fPathInstanceData[rangeFirstInstanceIdx], fPathInstanceData[currentInstanceIdx]);
271 currentInstanceIdx = rangeFirstInstanceIdx;
272 }
273
274 // An instance with this particular proxy did not yet exist in the array. Add a range for it.
275 emplace_at_memcpy(&fCopyPathRanges, fCurrCopyAtlasRangesIdx, std::move(srcProxy), 1);
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600276}
277
Chris Daltonce038dc2018-09-14 14:14:49 -0600278static bool transform_path_pts(const SkMatrix& m, const SkPath& path,
Chris Daltone1639692018-08-20 14:00:30 -0600279 const SkAutoSTArray<32, SkPoint>& outDevPts, SkRect* devBounds,
280 SkRect* devBounds45) {
281 const SkPoint* pts = SkPathPriv::PointData(path);
282 int numPts = path.countPoints();
283 SkASSERT(numPts + 1 <= outDevPts.count());
284 SkASSERT(numPts);
285
286 // m45 transforms path points into "45 degree" device space. A bounding box in this space gives
287 // the circumscribing octagon's diagonals. We could use SK_ScalarRoot2Over2, but an orthonormal
288 // transform is not necessary as long as the shader uses the correct inverse.
289 SkMatrix m45;
290 m45.setSinCos(1, 1);
291 m45.preConcat(m);
292
293 // X,Y,T are two parallel view matrices that accumulate two bounding boxes as they map points:
294 // device-space bounds and "45 degree" device-space bounds (| 1 -1 | * devCoords).
295 // | 1 1 |
296 Sk4f X = Sk4f(m.getScaleX(), m.getSkewY(), m45.getScaleX(), m45.getSkewY());
297 Sk4f Y = Sk4f(m.getSkewX(), m.getScaleY(), m45.getSkewX(), m45.getScaleY());
298 Sk4f T = Sk4f(m.getTranslateX(), m.getTranslateY(), m45.getTranslateX(), m45.getTranslateY());
299
300 // Map the path's points to device space and accumulate bounding boxes.
301 Sk4f devPt = SkNx_fma(Y, Sk4f(pts[0].y()), T);
302 devPt = SkNx_fma(X, Sk4f(pts[0].x()), devPt);
303 Sk4f topLeft = devPt;
304 Sk4f bottomRight = devPt;
305
306 // Store all 4 values [dev.x, dev.y, dev45.x, dev45.y]. We are only interested in the first two,
307 // and will overwrite [dev45.x, dev45.y] with the next point. This is why the dst buffer must
308 // be at least one larger than the number of points.
309 devPt.store(&outDevPts[0]);
310
311 for (int i = 1; i < numPts; ++i) {
312 devPt = SkNx_fma(Y, Sk4f(pts[i].y()), T);
313 devPt = SkNx_fma(X, Sk4f(pts[i].x()), devPt);
314 topLeft = Sk4f::Min(topLeft, devPt);
315 bottomRight = Sk4f::Max(bottomRight, devPt);
316 devPt.store(&outDevPts[i]);
317 }
318
Chris Daltonce038dc2018-09-14 14:14:49 -0600319 if (!(Sk4f(0) == topLeft*0).allTrue() || !(Sk4f(0) == bottomRight*0).allTrue()) {
320 // The bounds are infinite or NaN.
321 return false;
322 }
323
Chris Daltone1639692018-08-20 14:00:30 -0600324 SkPoint topLeftPts[2], bottomRightPts[2];
325 topLeft.store(topLeftPts);
326 bottomRight.store(bottomRightPts);
327 devBounds->setLTRB(topLeftPts[0].x(), topLeftPts[0].y(), bottomRightPts[0].x(),
328 bottomRightPts[0].y());
329 devBounds45->setLTRB(topLeftPts[1].x(), topLeftPts[1].y(), bottomRightPts[1].x(),
330 bottomRightPts[1].y());
Chris Daltonce038dc2018-09-14 14:14:49 -0600331 return true;
Chris Daltone1639692018-08-20 14:00:30 -0600332}
333
Chris Dalton351e80c2019-01-06 22:51:00 -0700334GrCCAtlas* GrCCPerFlushResources::renderShapeInAtlas(
Chris Dalton09a7bb22018-08-31 19:53:15 +0800335 const SkIRect& clipIBounds, const SkMatrix& m, const GrShape& shape, float strokeDevWidth,
336 SkRect* devBounds, SkRect* devBounds45, SkIRect* devIBounds, SkIVector* devToAtlasOffset) {
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600337 SkASSERT(this->isMapped());
Chris Dalton9414c962018-06-14 10:14:50 -0600338 SkASSERT(fNextPathInstanceIdx < fEndPathInstance);
339
Chris Dalton09a7bb22018-08-31 19:53:15 +0800340 SkPath path;
341 shape.asPath(&path);
Chris Daltone1639692018-08-20 14:00:30 -0600342 if (path.isEmpty()) {
343 SkDEBUGCODE(--fEndPathInstance);
344 return nullptr;
345 }
Chris Daltonce038dc2018-09-14 14:14:49 -0600346 if (!transform_path_pts(m, path, fLocalDevPtsBuffer, devBounds, devBounds45)) {
347 // The transformed path had infinite or NaN bounds.
348 SkDEBUGCODE(--fEndPathInstance);
349 return nullptr;
350 }
Chris Dalton09a7bb22018-08-31 19:53:15 +0800351
352 const SkStrokeRec& stroke = shape.style().strokeRec();
353 if (!stroke.isFillStyle()) {
354 float r = SkStrokeRec::GetInflationRadius(stroke.getJoin(), stroke.getMiter(),
355 stroke.getCap(), strokeDevWidth);
356 devBounds->outset(r, r);
357 // devBounds45 is in (| 1 -1 | * devCoords) space.
358 // | 1 1 |
359 devBounds45->outset(r*SK_ScalarSqrt2, r*SK_ScalarSqrt2);
360 }
Chris Dalton4da70192018-06-18 09:51:36 -0600361 devBounds->roundOut(devIBounds);
Chris Dalton9414c962018-06-14 10:14:50 -0600362
Chris Daltone1639692018-08-20 14:00:30 -0600363 GrScissorTest scissorTest;
364 SkIRect clippedPathIBounds;
365 if (!this->placeRenderedPathInAtlas(clipIBounds, *devIBounds, &scissorTest, &clippedPathIBounds,
366 devToAtlasOffset)) {
Chris Dalton9414c962018-06-14 10:14:50 -0600367 SkDEBUGCODE(--fEndPathInstance);
368 return nullptr; // Path was degenerate or clipped away.
369 }
Chris Daltone1639692018-08-20 14:00:30 -0600370
Chris Dalton09a7bb22018-08-31 19:53:15 +0800371 if (stroke.isFillStyle()) {
372 SkASSERT(0 == strokeDevWidth);
373 fFiller.parseDeviceSpaceFill(path, fLocalDevPtsBuffer.begin(), scissorTest,
374 clippedPathIBounds, *devToAtlasOffset);
375 } else {
376 // Stroke-and-fill is not yet supported.
377 SkASSERT(SkStrokeRec::kStroke_Style == stroke.getStyle() || stroke.isHairlineStyle());
378 SkASSERT(!stroke.isHairlineStyle() || 1 == strokeDevWidth);
379 fStroker.parseDeviceSpaceStroke(path, fLocalDevPtsBuffer.begin(), stroke, strokeDevWidth,
380 scissorTest, clippedPathIBounds, *devToAtlasOffset);
381 }
Chris Dalton4da70192018-06-18 09:51:36 -0600382 return &fRenderedAtlasStack.current();
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600383}
384
Chris Dalton9414c962018-06-14 10:14:50 -0600385const GrCCAtlas* GrCCPerFlushResources::renderDeviceSpacePathInAtlas(
386 const SkIRect& clipIBounds, const SkPath& devPath, const SkIRect& devPathIBounds,
387 SkIVector* devToAtlasOffset) {
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600388 SkASSERT(this->isMapped());
Chris Daltone1639692018-08-20 14:00:30 -0600389
390 if (devPath.isEmpty()) {
Chris Dalton9414c962018-06-14 10:14:50 -0600391 return nullptr;
392 }
Chris Daltone1639692018-08-20 14:00:30 -0600393
394 GrScissorTest scissorTest;
395 SkIRect clippedPathIBounds;
396 if (!this->placeRenderedPathInAtlas(clipIBounds, devPathIBounds, &scissorTest,
397 &clippedPathIBounds, devToAtlasOffset)) {
398 return nullptr;
399 }
400
401 fFiller.parseDeviceSpaceFill(devPath, SkPathPriv::PointData(devPath), scissorTest,
402 clippedPathIBounds, *devToAtlasOffset);
Chris Dalton4da70192018-06-18 09:51:36 -0600403 return &fRenderedAtlasStack.current();
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600404}
405
Chris Daltone1639692018-08-20 14:00:30 -0600406bool GrCCPerFlushResources::placeRenderedPathInAtlas(const SkIRect& clipIBounds,
407 const SkIRect& pathIBounds,
408 GrScissorTest* scissorTest,
409 SkIRect* clippedPathIBounds,
410 SkIVector* devToAtlasOffset) {
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600411 if (clipIBounds.contains(pathIBounds)) {
Chris Daltone1639692018-08-20 14:00:30 -0600412 *clippedPathIBounds = pathIBounds;
413 *scissorTest = GrScissorTest::kDisabled;
414 } else if (clippedPathIBounds->intersect(clipIBounds, pathIBounds)) {
415 *scissorTest = GrScissorTest::kEnabled;
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600416 } else {
Chris Dalton9414c962018-06-14 10:14:50 -0600417 return false;
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600418 }
419
Chris Dalton4da70192018-06-18 09:51:36 -0600420 if (GrCCAtlas* retiredAtlas =
Chris Daltone1639692018-08-20 14:00:30 -0600421 fRenderedAtlasStack.addRect(*clippedPathIBounds, devToAtlasOffset)) {
Chris Dalton9414c962018-06-14 10:14:50 -0600422 // We did not fit in the previous coverage count atlas and it was retired. Close the path
423 // parser's current batch (which does not yet include the path we just parsed). We will
424 // render this batch into the retired atlas during finalize().
Chris Dalton09a7bb22018-08-31 19:53:15 +0800425 retiredAtlas->setFillBatchID(fFiller.closeCurrentBatch());
426 retiredAtlas->setStrokeBatchID(fStroker.closeCurrentBatch());
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600427 }
Chris Dalton9414c962018-06-14 10:14:50 -0600428 return true;
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600429}
430
431bool GrCCPerFlushResources::finalize(GrOnFlushResourceProvider* onFlushRP,
Chris Dalton9414c962018-06-14 10:14:50 -0600432 SkTArray<sk_sp<GrRenderTargetContext>>* out) {
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600433 SkASSERT(this->isMapped());
Chris Dalton9414c962018-06-14 10:14:50 -0600434 SkASSERT(fNextPathInstanceIdx == fEndPathInstance);
Chris Dalton351e80c2019-01-06 22:51:00 -0700435 SkASSERT(fNextCopyInstanceIdx == fEndCopyInstance);
Chris Dalton9414c962018-06-14 10:14:50 -0600436
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600437 fInstanceBuffer->unmap();
438 fPathInstanceData = nullptr;
439
Chris Dalton4da70192018-06-18 09:51:36 -0600440 if (!fCopyAtlasStack.empty()) {
Chris Dalton351e80c2019-01-06 22:51:00 -0700441 fCopyAtlasStack.current().setFillBatchID(fCopyPathRanges.count());
442 fCurrCopyAtlasRangesIdx = fCopyPathRanges.count();
Chris Dalton4da70192018-06-18 09:51:36 -0600443 }
444 if (!fRenderedAtlasStack.empty()) {
Chris Dalton09a7bb22018-08-31 19:53:15 +0800445 fRenderedAtlasStack.current().setFillBatchID(fFiller.closeCurrentBatch());
446 fRenderedAtlasStack.current().setStrokeBatchID(fStroker.closeCurrentBatch());
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600447 }
448
Chris Dalton9414c962018-06-14 10:14:50 -0600449 // Build the GPU buffers to render path coverage counts. (This must not happen until after the
Chris Dalton09a7bb22018-08-31 19:53:15 +0800450 // final calls to fFiller/fStroker.closeCurrentBatch().)
Chris Daltone1639692018-08-20 14:00:30 -0600451 if (!fFiller.prepareToDraw(onFlushRP)) {
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600452 return false;
453 }
Chris Dalton09a7bb22018-08-31 19:53:15 +0800454 if (!fStroker.prepareToDraw(onFlushRP)) {
455 return false;
456 }
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600457
Chris Dalton351e80c2019-01-06 22:51:00 -0700458 // Draw the copies from 16-bit literal coverage atlas(es) into 8-bit cached atlas(es).
459 int copyRangeIdx = 0;
Chris Dalton4da70192018-06-18 09:51:36 -0600460 int baseCopyInstance = 0;
461 for (GrCCAtlasStack::Iter atlas(fCopyAtlasStack); atlas.next();) {
Chris Dalton351e80c2019-01-06 22:51:00 -0700462 int endCopyRange = atlas->getFillBatchID();
463 SkASSERT(endCopyRange > copyRangeIdx);
464
465 sk_sp<GrRenderTargetContext> rtc = atlas->makeRenderTargetContext(onFlushRP);
466 for (; copyRangeIdx < endCopyRange; ++copyRangeIdx) {
467 const CopyPathRange& copyRange = fCopyPathRanges[copyRangeIdx];
468 int endCopyInstance = baseCopyInstance + copyRange.fCount;
469 if (rtc) {
470 auto op = CopyAtlasOp::Make(rtc->surfPriv().getContext(), sk_ref_sp(this),
471 copyRange.fSrcProxy, baseCopyInstance, endCopyInstance,
472 atlas->drawBounds());
473 rtc->addDrawOp(GrNoClip(), std::move(op));
474 }
475 baseCopyInstance = endCopyInstance;
Chris Dalton4da70192018-06-18 09:51:36 -0600476 }
Chris Dalton351e80c2019-01-06 22:51:00 -0700477 out->push_back(std::move(rtc));
Chris Dalton4da70192018-06-18 09:51:36 -0600478 }
Chris Dalton351e80c2019-01-06 22:51:00 -0700479 SkASSERT(fCopyPathRanges.count() == copyRangeIdx);
480 SkASSERT(fNextCopyInstanceIdx == baseCopyInstance);
481 SkASSERT(baseCopyInstance == fEndCopyInstance);
Chris Dalton4da70192018-06-18 09:51:36 -0600482
Chris Dalton4da70192018-06-18 09:51:36 -0600483 // Render the coverage count atlas(es).
484 for (GrCCAtlasStack::Iter atlas(fRenderedAtlasStack); atlas.next();) {
Chris Dalton351e80c2019-01-06 22:51:00 -0700485 // Copies will be finished by the time we get to rendering new atlases. See if we can
486 // recycle any previous invalidated atlas textures instead of creating new ones.
Chris Daltonafde18f2018-06-22 12:44:19 -0600487 sk_sp<GrTexture> backingTexture;
Chris Dalton351e80c2019-01-06 22:51:00 -0700488 for (sk_sp<GrTexture>& texture : fRecyclableAtlasTextures) {
489 if (texture && atlas->currentHeight() == texture->height() &&
490 atlas->currentWidth() == texture->width()) {
491 backingTexture = skstd::exchange(texture, nullptr);
492 break;
493 }
Chris Daltonafde18f2018-06-22 12:44:19 -0600494 }
495
496 if (auto rtc = atlas->makeRenderTargetContext(onFlushRP, std::move(backingTexture))) {
Chris Dalton9414c962018-06-14 10:14:50 -0600497 auto op = RenderAtlasOp::Make(rtc->surfPriv().getContext(), sk_ref_sp(this),
Chris Dalton09a7bb22018-08-31 19:53:15 +0800498 atlas->getFillBatchID(), atlas->getStrokeBatchID(),
499 atlas->drawBounds());
Chris Dalton9414c962018-06-14 10:14:50 -0600500 rtc->addDrawOp(GrNoClip(), std::move(op));
501 out->push_back(std::move(rtc));
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600502 }
503 }
504
505 return true;
506}
Chris Dalton4da70192018-06-18 09:51:36 -0600507
Chris Dalton351e80c2019-01-06 22:51:00 -0700508void GrCCPerFlushResourceSpecs::cancelCopies() {
509 // Convert copies to cached draws.
510 fNumCachedPaths += fNumCopiedPaths[kFillIdx] + fNumCopiedPaths[kStrokeIdx];
511 fNumCopiedPaths[kFillIdx] = fNumCopiedPaths[kStrokeIdx] = 0;
512 fCopyPathStats[kFillIdx] = fCopyPathStats[kStrokeIdx] = GrCCRenderedPathStats();
Chris Dalton4da70192018-06-18 09:51:36 -0600513 fCopyAtlasSpecs = GrCCAtlas::Specs();
Chris Dalton4da70192018-06-18 09:51:36 -0600514}