blob: b1aa3fabbb9748e0c9e3c84c2335d8b947c10ffb [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"
Chris Dalton9414c962018-06-14 10:14:50 -060013#include "GrSurfaceContextPriv.h"
Chris Dalton5ba36ba2018-05-09 01:08:38 -060014#include "GrRenderTargetContext.h"
Chris Dalton09a7bb22018-08-31 19:53:15 +080015#include "GrShape.h"
Chris Dalton9414c962018-06-14 10:14:50 -060016#include "SkMakeUnique.h"
Chris Dalton4da70192018-06-18 09:51:36 -060017#include "ccpr/GrCCPathCache.h"
Chris Dalton5ba36ba2018-05-09 01:08:38 -060018
Chris Daltone1639692018-08-20 14:00:30 -060019using FillBatchID = GrCCFiller::BatchID;
Chris Dalton09a7bb22018-08-31 19:53:15 +080020using StrokeBatchID = GrCCStroker::BatchID;
Chris Dalton5ba36ba2018-05-09 01:08:38 -060021using PathInstance = GrCCPathProcessor::Instance;
22
Chris Dalton09a7bb22018-08-31 19:53:15 +080023static constexpr int kFillIdx = GrCCPerFlushResourceSpecs::kFillIdx;
24static constexpr int kStrokeIdx = GrCCPerFlushResourceSpecs::kStrokeIdx;
25
Chris Dalton9414c962018-06-14 10:14:50 -060026namespace {
27
Chris Dalton4da70192018-06-18 09:51:36 -060028// Base class for an Op that renders a CCPR atlas.
29class AtlasOp : public GrDrawOp {
30public:
31 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
Chris Dalton4b62aed2019-01-15 11:53:00 -070032 GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*) override {
33 return GrProcessorSet::EmptySetAnalysis();
Brian Osman532b3f92018-07-11 10:02:07 -040034 }
Brian Salomon7eae3e02018-08-07 14:02:38 +000035 CombineResult onCombineIfPossible(GrOp* other, const GrCaps&) override {
Chris Dalton351e80c2019-01-06 22:51:00 -070036 // We will only make multiple copy ops if they have different source proxies.
37 // TODO: make use of texture chaining.
38 return CombineResult::kCannotCombine;
Chris Dalton4da70192018-06-18 09:51:36 -060039 }
40 void onPrepare(GrOpFlushState*) override {}
41
42protected:
43 AtlasOp(uint32_t classID, sk_sp<const GrCCPerFlushResources> resources,
44 const SkISize& drawBounds)
45 : GrDrawOp(classID)
46 , fResources(std::move(resources)) {
47 this->setBounds(SkRect::MakeIWH(drawBounds.width(), drawBounds.height()),
48 GrOp::HasAABloat::kNo, GrOp::IsZeroArea::kNo);
49 }
50
51 const sk_sp<const GrCCPerFlushResources> fResources;
52};
53
Chris Dalton351e80c2019-01-06 22:51:00 -070054// Copies paths from a cached coverage count atlas into an 8-bit literal-coverage atlas.
Chris Dalton4da70192018-06-18 09:51:36 -060055class CopyAtlasOp : public AtlasOp {
56public:
57 DEFINE_OP_CLASS_ID
58
59 static std::unique_ptr<GrDrawOp> Make(GrContext* context,
60 sk_sp<const GrCCPerFlushResources> resources,
61 sk_sp<GrTextureProxy> copyProxy, int baseInstance,
62 int endInstance, const SkISize& drawBounds) {
Robert Phillips9da87e02019-02-04 13:26:26 -050063 GrOpMemoryPool* pool = context->priv().opMemoryPool();
Robert Phillipsc994a932018-06-19 13:09:54 -040064
65 return pool->allocate<CopyAtlasOp>(std::move(resources), std::move(copyProxy),
66 baseInstance, endInstance, drawBounds);
Chris Dalton4da70192018-06-18 09:51:36 -060067 }
68
69 const char* name() const override { return "CopyAtlasOp (CCPR)"; }
Chris Dalton351e80c2019-01-06 22:51:00 -070070 void visitProxies(const VisitProxyFunc& fn, VisitorType) const override { fn(fSrcProxy.get()); }
Chris Dalton4da70192018-06-18 09:51:36 -060071
Brian Salomon588cec72018-11-14 13:56:37 -050072 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Chris Dalton351e80c2019-01-06 22:51:00 -070073 SkASSERT(fSrcProxy);
Brian Salomon7eae3e02018-08-07 14:02:38 +000074 GrPipeline::FixedDynamicState dynamicState;
Chris Dalton351e80c2019-01-06 22:51:00 -070075 auto srcProxy = fSrcProxy.get();
76 dynamicState.fPrimitiveProcessorTextures = &srcProxy;
Brian Salomon7eae3e02018-08-07 14:02:38 +000077
Robert Phillipsd0fe8752019-01-31 14:13:59 -050078 GrPipeline pipeline(GrScissorTest::kDisabled, SkBlendMode::kSrc);
Chris Dalton351e80c2019-01-06 22:51:00 -070079 GrCCPathProcessor pathProc(srcProxy);
Brian Salomon7eae3e02018-08-07 14:02:38 +000080 pathProc.drawPaths(flushState, pipeline, &dynamicState, *fResources, fBaseInstance,
81 fEndInstance, this->bounds());
Chris Dalton4da70192018-06-18 09:51:36 -060082 }
83
84private:
85 friend class ::GrOpMemoryPool; // for ctor
86
Chris Dalton351e80c2019-01-06 22:51:00 -070087 CopyAtlasOp(sk_sp<const GrCCPerFlushResources> resources, sk_sp<GrTextureProxy> srcProxy,
Chris Dalton4da70192018-06-18 09:51:36 -060088 int baseInstance, int endInstance, const SkISize& drawBounds)
89 : AtlasOp(ClassID(), std::move(resources), drawBounds)
Chris Dalton351e80c2019-01-06 22:51:00 -070090 , fSrcProxy(srcProxy)
Chris Dalton4da70192018-06-18 09:51:36 -060091 , fBaseInstance(baseInstance)
92 , fEndInstance(endInstance) {
93 }
Chris Dalton351e80c2019-01-06 22:51:00 -070094 sk_sp<GrTextureProxy> fSrcProxy;
Chris Dalton4da70192018-06-18 09:51:36 -060095 const int fBaseInstance;
96 const int fEndInstance;
97};
98
Chris Dalton9414c962018-06-14 10:14:50 -060099// Renders coverage counts to a CCPR atlas using the resources' pre-filled GrCCPathParser.
Chris Dalton4da70192018-06-18 09:51:36 -0600100class RenderAtlasOp : public AtlasOp {
Chris Dalton9414c962018-06-14 10:14:50 -0600101public:
102 DEFINE_OP_CLASS_ID
103
104 static std::unique_ptr<GrDrawOp> Make(GrContext* context,
105 sk_sp<const GrCCPerFlushResources> resources,
Chris Dalton09a7bb22018-08-31 19:53:15 +0800106 FillBatchID fillBatchID, StrokeBatchID strokeBatchID,
107 const SkISize& drawBounds) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500108 GrOpMemoryPool* pool = context->priv().opMemoryPool();
Robert Phillipsc994a932018-06-19 13:09:54 -0400109
Chris Dalton09a7bb22018-08-31 19:53:15 +0800110 return pool->allocate<RenderAtlasOp>(std::move(resources), fillBatchID, strokeBatchID,
111 drawBounds);
Chris Dalton9414c962018-06-14 10:14:50 -0600112 }
113
114 // GrDrawOp interface.
115 const char* name() const override { return "RenderAtlasOp (CCPR)"; }
Chris Dalton9414c962018-06-14 10:14:50 -0600116
Brian Salomon588cec72018-11-14 13:56:37 -0500117 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Chris Dalton09a7bb22018-08-31 19:53:15 +0800118 fResources->filler().drawFills(flushState, fFillBatchID, fDrawBounds);
119 fResources->stroker().drawStrokes(flushState, fStrokeBatchID, fDrawBounds);
Chris Dalton9414c962018-06-14 10:14:50 -0600120 }
121
122private:
123 friend class ::GrOpMemoryPool; // for ctor
124
Chris Dalton09a7bb22018-08-31 19:53:15 +0800125 RenderAtlasOp(sk_sp<const GrCCPerFlushResources> resources, FillBatchID fillBatchID,
126 StrokeBatchID strokeBatchID, const SkISize& drawBounds)
Chris Dalton4da70192018-06-18 09:51:36 -0600127 : AtlasOp(ClassID(), std::move(resources), drawBounds)
Chris Dalton09a7bb22018-08-31 19:53:15 +0800128 , fFillBatchID(fillBatchID)
129 , fStrokeBatchID(strokeBatchID)
Chris Dalton9414c962018-06-14 10:14:50 -0600130 , fDrawBounds(SkIRect::MakeWH(drawBounds.width(), drawBounds.height())) {
Chris Dalton9414c962018-06-14 10:14:50 -0600131 }
132
Chris Dalton09a7bb22018-08-31 19:53:15 +0800133 const FillBatchID fFillBatchID;
134 const StrokeBatchID fStrokeBatchID;
Chris Dalton9414c962018-06-14 10:14:50 -0600135 const SkIRect fDrawBounds;
136};
137
138}
139
Chris Dalton4da70192018-06-18 09:51:36 -0600140static int inst_buffer_count(const GrCCPerFlushResourceSpecs& specs) {
141 return specs.fNumCachedPaths +
Chris Dalton09a7bb22018-08-31 19:53:15 +0800142 // Copies get two instances per draw: 1 copy + 1 draw.
143 (specs.fNumCopiedPaths[kFillIdx] + specs.fNumCopiedPaths[kStrokeIdx]) * 2 +
144 specs.fNumRenderedPaths[kFillIdx] + specs.fNumRenderedPaths[kStrokeIdx];
145 // No clips in instance buffers.
Chris Dalton4da70192018-06-18 09:51:36 -0600146}
147
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600148GrCCPerFlushResources::GrCCPerFlushResources(GrOnFlushResourceProvider* onFlushRP,
Chris Dalton42c21152018-06-13 15:28:19 -0600149 const GrCCPerFlushResourceSpecs& specs)
Brian Salomonae64c192019-02-05 09:41:37 -0500150 // Overallocate by one point so we can call Sk4f::Store at the final SkPoint in the array.
151 // (See transform_path_pts below.)
152 // FIXME: instead use built-in instructions to write only the first two lanes of an Sk4f.
Chris Dalton09a7bb22018-08-31 19:53:15 +0800153 : fLocalDevPtsBuffer(SkTMax(specs.fRenderedPathStats[kFillIdx].fMaxPointsPerPath,
154 specs.fRenderedPathStats[kStrokeIdx].fMaxPointsPerPath) + 1)
155 , fFiller(specs.fNumRenderedPaths[kFillIdx] + specs.fNumClipPaths,
156 specs.fRenderedPathStats[kFillIdx].fNumTotalSkPoints,
157 specs.fRenderedPathStats[kFillIdx].fNumTotalSkVerbs,
158 specs.fRenderedPathStats[kFillIdx].fNumTotalConicWeights)
159 , fStroker(specs.fNumRenderedPaths[kStrokeIdx],
160 specs.fRenderedPathStats[kStrokeIdx].fNumTotalSkPoints,
161 specs.fRenderedPathStats[kStrokeIdx].fNumTotalSkVerbs)
Chris Dalton351e80c2019-01-06 22:51:00 -0700162 , fCopyAtlasStack(GrCCAtlas::CoverageType::kA8_LiteralCoverage, specs.fCopyAtlasSpecs,
163 onFlushRP->caps())
164 , fRenderedAtlasStack(GrCCAtlas::CoverageType::kFP16_CoverageCount,
165 specs.fRenderedAtlasSpecs, onFlushRP->caps())
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600166 , fIndexBuffer(GrCCPathProcessor::FindIndexBuffer(onFlushRP))
167 , fVertexBuffer(GrCCPathProcessor::FindVertexBuffer(onFlushRP))
Brian Salomonae64c192019-02-05 09:41:37 -0500168 , fInstanceBuffer(onFlushRP->makeBuffer(GrGpuBufferType::kVertex,
Chris Dalton4da70192018-06-18 09:51:36 -0600169 inst_buffer_count(specs) * sizeof(PathInstance)))
170 , fNextCopyInstanceIdx(0)
Chris Dalton09a7bb22018-08-31 19:53:15 +0800171 , fNextPathInstanceIdx(specs.fNumCopiedPaths[kFillIdx] +
172 specs.fNumCopiedPaths[kStrokeIdx]) {
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600173 if (!fIndexBuffer) {
174 SkDebugf("WARNING: failed to allocate CCPR index buffer. No paths will be drawn.\n");
175 return;
176 }
177 if (!fVertexBuffer) {
178 SkDebugf("WARNING: failed to allocate CCPR vertex buffer. No paths will be drawn.\n");
179 return;
180 }
181 if (!fInstanceBuffer) {
182 SkDebugf("WARNING: failed to allocate CCPR instance buffer. No paths will be drawn.\n");
183 return;
184 }
185 fPathInstanceData = static_cast<PathInstance*>(fInstanceBuffer->map());
186 SkASSERT(fPathInstanceData);
Chris Dalton09a7bb22018-08-31 19:53:15 +0800187 SkDEBUGCODE(fEndCopyInstance =
188 specs.fNumCopiedPaths[kFillIdx] + specs.fNumCopiedPaths[kStrokeIdx]);
Chris Dalton4da70192018-06-18 09:51:36 -0600189 SkDEBUGCODE(fEndPathInstance = inst_buffer_count(specs));
190}
191
Chris Dalton351e80c2019-01-06 22:51:00 -0700192void GrCCPerFlushResources::upgradeEntryToLiteralCoverageAtlas(
193 GrCCPathCache* pathCache, GrOnFlushResourceProvider* onFlushRP, GrCCPathCacheEntry* entry,
194 GrCCPathProcessor::DoEvenOddFill evenOdd) {
195 using ReleaseAtlasResult = GrCCPathCacheEntry::ReleaseAtlasResult;
Chris Dalton4da70192018-06-18 09:51:36 -0600196 SkASSERT(this->isMapped());
197 SkASSERT(fNextCopyInstanceIdx < fEndCopyInstance);
Chris Dalton4da70192018-06-18 09:51:36 -0600198
Chris Dalton351e80c2019-01-06 22:51:00 -0700199 const GrCCCachedAtlas* cachedAtlas = entry->cachedAtlas();
200 SkASSERT(cachedAtlas);
201 SkASSERT(cachedAtlas->getOnFlushProxy());
202
203 if (GrCCAtlas::CoverageType::kA8_LiteralCoverage == cachedAtlas->coverageType()) {
204 // This entry has already been upgraded to literal coverage. The path must have been drawn
205 // multiple times during the flush.
206 SkDEBUGCODE(--fEndCopyInstance);
207 return;
Chris Dalton4da70192018-06-18 09:51:36 -0600208 }
209
Chris Dalton351e80c2019-01-06 22:51:00 -0700210 SkIVector newAtlasOffset;
211 if (GrCCAtlas* retiredAtlas = fCopyAtlasStack.addRect(entry->devIBounds(), &newAtlasOffset)) {
212 // We did not fit in the previous copy atlas and it was retired. We will render the ranges
213 // up until fCopyPathRanges.count() into the retired atlas during finalize().
214 retiredAtlas->setFillBatchID(fCopyPathRanges.count());
215 fCurrCopyAtlasRangesIdx = fCopyPathRanges.count();
216 }
217
218 this->recordCopyPathInstance(*entry, newAtlasOffset, evenOdd,
219 sk_ref_sp(cachedAtlas->getOnFlushProxy()));
220
221 sk_sp<GrTexture> previousAtlasTexture =
222 sk_ref_sp(cachedAtlas->getOnFlushProxy()->peekTexture());
223 GrCCAtlas* newAtlas = &fCopyAtlasStack.current();
224 if (ReleaseAtlasResult::kDidInvalidateFromCache ==
225 entry->upgradeToLiteralCoverageAtlas(pathCache, onFlushRP, newAtlas, newAtlasOffset)) {
226 // This texture just got booted out of the cache. Keep it around, in case we might be able
227 // to recycle it for a new atlas. We can recycle it because copying happens before rendering
228 // new paths, and every path from the atlas that we're planning to use this flush will be
229 // copied to a new atlas. We'll never copy some and leave others.
230 fRecyclableAtlasTextures.push_back(std::move(previousAtlasTexture));
231 }
232}
233
234template<typename T, typename... Args>
235static void emplace_at_memcpy(SkTArray<T>* array, int idx, Args&&... args) {
236 if (int moveCount = array->count() - idx) {
237 array->push_back();
238 T* location = array->begin() + idx;
239 memcpy(location+1, location, moveCount * sizeof(T));
240 new (location) T(std::forward<Args>(args)...);
241 } else {
242 array->emplace_back(std::forward<Args>(args)...);
243 }
244}
245
246void GrCCPerFlushResources::recordCopyPathInstance(const GrCCPathCacheEntry& entry,
247 const SkIVector& newAtlasOffset,
248 GrCCPathProcessor::DoEvenOddFill evenOdd,
249 sk_sp<GrTextureProxy> srcProxy) {
250 SkASSERT(fNextCopyInstanceIdx < fEndCopyInstance);
251
252 // Write the instance at the back of the array.
253 int currentInstanceIdx = fNextCopyInstanceIdx++;
Brian Osmanc6444d22019-01-09 16:30:12 -0500254 constexpr uint64_t kWhite = (((uint64_t) SK_Half1) << 0) |
255 (((uint64_t) SK_Half1) << 16) |
256 (((uint64_t) SK_Half1) << 32) |
257 (((uint64_t) SK_Half1) << 48);
258 fPathInstanceData[currentInstanceIdx].set(entry, newAtlasOffset, kWhite, evenOdd);
Chris Dalton351e80c2019-01-06 22:51:00 -0700259
260 // Percolate the instance forward until it's contiguous with other instances that share the same
261 // proxy.
262 for (int i = fCopyPathRanges.count() - 1; i >= fCurrCopyAtlasRangesIdx; --i) {
263 if (fCopyPathRanges[i].fSrcProxy == srcProxy) {
264 ++fCopyPathRanges[i].fCount;
265 return;
266 }
267 int rangeFirstInstanceIdx = currentInstanceIdx - fCopyPathRanges[i].fCount;
268 std::swap(fPathInstanceData[rangeFirstInstanceIdx], fPathInstanceData[currentInstanceIdx]);
269 currentInstanceIdx = rangeFirstInstanceIdx;
270 }
271
272 // An instance with this particular proxy did not yet exist in the array. Add a range for it.
273 emplace_at_memcpy(&fCopyPathRanges, fCurrCopyAtlasRangesIdx, std::move(srcProxy), 1);
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600274}
275
Chris Daltonce038dc2018-09-14 14:14:49 -0600276static bool transform_path_pts(const SkMatrix& m, const SkPath& path,
Chris Daltone1639692018-08-20 14:00:30 -0600277 const SkAutoSTArray<32, SkPoint>& outDevPts, SkRect* devBounds,
278 SkRect* devBounds45) {
279 const SkPoint* pts = SkPathPriv::PointData(path);
280 int numPts = path.countPoints();
281 SkASSERT(numPts + 1 <= outDevPts.count());
282 SkASSERT(numPts);
283
284 // m45 transforms path points into "45 degree" device space. A bounding box in this space gives
285 // the circumscribing octagon's diagonals. We could use SK_ScalarRoot2Over2, but an orthonormal
286 // transform is not necessary as long as the shader uses the correct inverse.
287 SkMatrix m45;
288 m45.setSinCos(1, 1);
289 m45.preConcat(m);
290
291 // X,Y,T are two parallel view matrices that accumulate two bounding boxes as they map points:
292 // device-space bounds and "45 degree" device-space bounds (| 1 -1 | * devCoords).
293 // | 1 1 |
294 Sk4f X = Sk4f(m.getScaleX(), m.getSkewY(), m45.getScaleX(), m45.getSkewY());
295 Sk4f Y = Sk4f(m.getSkewX(), m.getScaleY(), m45.getSkewX(), m45.getScaleY());
296 Sk4f T = Sk4f(m.getTranslateX(), m.getTranslateY(), m45.getTranslateX(), m45.getTranslateY());
297
298 // Map the path's points to device space and accumulate bounding boxes.
299 Sk4f devPt = SkNx_fma(Y, Sk4f(pts[0].y()), T);
300 devPt = SkNx_fma(X, Sk4f(pts[0].x()), devPt);
301 Sk4f topLeft = devPt;
302 Sk4f bottomRight = devPt;
303
304 // Store all 4 values [dev.x, dev.y, dev45.x, dev45.y]. We are only interested in the first two,
305 // and will overwrite [dev45.x, dev45.y] with the next point. This is why the dst buffer must
306 // be at least one larger than the number of points.
307 devPt.store(&outDevPts[0]);
308
309 for (int i = 1; i < numPts; ++i) {
310 devPt = SkNx_fma(Y, Sk4f(pts[i].y()), T);
311 devPt = SkNx_fma(X, Sk4f(pts[i].x()), devPt);
312 topLeft = Sk4f::Min(topLeft, devPt);
313 bottomRight = Sk4f::Max(bottomRight, devPt);
314 devPt.store(&outDevPts[i]);
315 }
316
Chris Daltonce038dc2018-09-14 14:14:49 -0600317 if (!(Sk4f(0) == topLeft*0).allTrue() || !(Sk4f(0) == bottomRight*0).allTrue()) {
318 // The bounds are infinite or NaN.
319 return false;
320 }
321
Chris Daltone1639692018-08-20 14:00:30 -0600322 SkPoint topLeftPts[2], bottomRightPts[2];
323 topLeft.store(topLeftPts);
324 bottomRight.store(bottomRightPts);
325 devBounds->setLTRB(topLeftPts[0].x(), topLeftPts[0].y(), bottomRightPts[0].x(),
326 bottomRightPts[0].y());
327 devBounds45->setLTRB(topLeftPts[1].x(), topLeftPts[1].y(), bottomRightPts[1].x(),
328 bottomRightPts[1].y());
Chris Daltonce038dc2018-09-14 14:14:49 -0600329 return true;
Chris Daltone1639692018-08-20 14:00:30 -0600330}
331
Chris Dalton351e80c2019-01-06 22:51:00 -0700332GrCCAtlas* GrCCPerFlushResources::renderShapeInAtlas(
Chris Dalton09a7bb22018-08-31 19:53:15 +0800333 const SkIRect& clipIBounds, const SkMatrix& m, const GrShape& shape, float strokeDevWidth,
334 SkRect* devBounds, SkRect* devBounds45, SkIRect* devIBounds, SkIVector* devToAtlasOffset) {
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600335 SkASSERT(this->isMapped());
Chris Dalton9414c962018-06-14 10:14:50 -0600336 SkASSERT(fNextPathInstanceIdx < fEndPathInstance);
337
Chris Dalton09a7bb22018-08-31 19:53:15 +0800338 SkPath path;
339 shape.asPath(&path);
Chris Daltone1639692018-08-20 14:00:30 -0600340 if (path.isEmpty()) {
341 SkDEBUGCODE(--fEndPathInstance);
342 return nullptr;
343 }
Chris Daltonce038dc2018-09-14 14:14:49 -0600344 if (!transform_path_pts(m, path, fLocalDevPtsBuffer, devBounds, devBounds45)) {
345 // The transformed path had infinite or NaN bounds.
346 SkDEBUGCODE(--fEndPathInstance);
347 return nullptr;
348 }
Chris Dalton09a7bb22018-08-31 19:53:15 +0800349
350 const SkStrokeRec& stroke = shape.style().strokeRec();
351 if (!stroke.isFillStyle()) {
352 float r = SkStrokeRec::GetInflationRadius(stroke.getJoin(), stroke.getMiter(),
353 stroke.getCap(), strokeDevWidth);
354 devBounds->outset(r, r);
355 // devBounds45 is in (| 1 -1 | * devCoords) space.
356 // | 1 1 |
357 devBounds45->outset(r*SK_ScalarSqrt2, r*SK_ScalarSqrt2);
358 }
Chris Dalton4da70192018-06-18 09:51:36 -0600359 devBounds->roundOut(devIBounds);
Chris Dalton9414c962018-06-14 10:14:50 -0600360
Chris Daltone1639692018-08-20 14:00:30 -0600361 GrScissorTest scissorTest;
362 SkIRect clippedPathIBounds;
363 if (!this->placeRenderedPathInAtlas(clipIBounds, *devIBounds, &scissorTest, &clippedPathIBounds,
364 devToAtlasOffset)) {
Chris Dalton9414c962018-06-14 10:14:50 -0600365 SkDEBUGCODE(--fEndPathInstance);
366 return nullptr; // Path was degenerate or clipped away.
367 }
Chris Daltone1639692018-08-20 14:00:30 -0600368
Chris Dalton09a7bb22018-08-31 19:53:15 +0800369 if (stroke.isFillStyle()) {
370 SkASSERT(0 == strokeDevWidth);
371 fFiller.parseDeviceSpaceFill(path, fLocalDevPtsBuffer.begin(), scissorTest,
372 clippedPathIBounds, *devToAtlasOffset);
373 } else {
374 // Stroke-and-fill is not yet supported.
375 SkASSERT(SkStrokeRec::kStroke_Style == stroke.getStyle() || stroke.isHairlineStyle());
376 SkASSERT(!stroke.isHairlineStyle() || 1 == strokeDevWidth);
377 fStroker.parseDeviceSpaceStroke(path, fLocalDevPtsBuffer.begin(), stroke, strokeDevWidth,
378 scissorTest, clippedPathIBounds, *devToAtlasOffset);
379 }
Chris Dalton4da70192018-06-18 09:51:36 -0600380 return &fRenderedAtlasStack.current();
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600381}
382
Chris Dalton9414c962018-06-14 10:14:50 -0600383const GrCCAtlas* GrCCPerFlushResources::renderDeviceSpacePathInAtlas(
384 const SkIRect& clipIBounds, const SkPath& devPath, const SkIRect& devPathIBounds,
385 SkIVector* devToAtlasOffset) {
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600386 SkASSERT(this->isMapped());
Chris Daltone1639692018-08-20 14:00:30 -0600387
388 if (devPath.isEmpty()) {
Chris Dalton9414c962018-06-14 10:14:50 -0600389 return nullptr;
390 }
Chris Daltone1639692018-08-20 14:00:30 -0600391
392 GrScissorTest scissorTest;
393 SkIRect clippedPathIBounds;
394 if (!this->placeRenderedPathInAtlas(clipIBounds, devPathIBounds, &scissorTest,
395 &clippedPathIBounds, devToAtlasOffset)) {
396 return nullptr;
397 }
398
399 fFiller.parseDeviceSpaceFill(devPath, SkPathPriv::PointData(devPath), scissorTest,
400 clippedPathIBounds, *devToAtlasOffset);
Chris Dalton4da70192018-06-18 09:51:36 -0600401 return &fRenderedAtlasStack.current();
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600402}
403
Chris Daltone1639692018-08-20 14:00:30 -0600404bool GrCCPerFlushResources::placeRenderedPathInAtlas(const SkIRect& clipIBounds,
405 const SkIRect& pathIBounds,
406 GrScissorTest* scissorTest,
407 SkIRect* clippedPathIBounds,
408 SkIVector* devToAtlasOffset) {
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600409 if (clipIBounds.contains(pathIBounds)) {
Chris Daltone1639692018-08-20 14:00:30 -0600410 *clippedPathIBounds = pathIBounds;
411 *scissorTest = GrScissorTest::kDisabled;
412 } else if (clippedPathIBounds->intersect(clipIBounds, pathIBounds)) {
413 *scissorTest = GrScissorTest::kEnabled;
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600414 } else {
Chris Dalton9414c962018-06-14 10:14:50 -0600415 return false;
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600416 }
417
Chris Dalton4da70192018-06-18 09:51:36 -0600418 if (GrCCAtlas* retiredAtlas =
Chris Daltone1639692018-08-20 14:00:30 -0600419 fRenderedAtlasStack.addRect(*clippedPathIBounds, devToAtlasOffset)) {
Chris Dalton9414c962018-06-14 10:14:50 -0600420 // We did not fit in the previous coverage count atlas and it was retired. Close the path
421 // parser's current batch (which does not yet include the path we just parsed). We will
422 // render this batch into the retired atlas during finalize().
Chris Dalton09a7bb22018-08-31 19:53:15 +0800423 retiredAtlas->setFillBatchID(fFiller.closeCurrentBatch());
424 retiredAtlas->setStrokeBatchID(fStroker.closeCurrentBatch());
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600425 }
Chris Dalton9414c962018-06-14 10:14:50 -0600426 return true;
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600427}
428
429bool GrCCPerFlushResources::finalize(GrOnFlushResourceProvider* onFlushRP,
Chris Dalton9414c962018-06-14 10:14:50 -0600430 SkTArray<sk_sp<GrRenderTargetContext>>* out) {
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600431 SkASSERT(this->isMapped());
Chris Dalton9414c962018-06-14 10:14:50 -0600432 SkASSERT(fNextPathInstanceIdx == fEndPathInstance);
Chris Dalton351e80c2019-01-06 22:51:00 -0700433 SkASSERT(fNextCopyInstanceIdx == fEndCopyInstance);
Chris Dalton9414c962018-06-14 10:14:50 -0600434
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600435 fInstanceBuffer->unmap();
436 fPathInstanceData = nullptr;
437
Chris Dalton4da70192018-06-18 09:51:36 -0600438 if (!fCopyAtlasStack.empty()) {
Chris Dalton351e80c2019-01-06 22:51:00 -0700439 fCopyAtlasStack.current().setFillBatchID(fCopyPathRanges.count());
440 fCurrCopyAtlasRangesIdx = fCopyPathRanges.count();
Chris Dalton4da70192018-06-18 09:51:36 -0600441 }
442 if (!fRenderedAtlasStack.empty()) {
Chris Dalton09a7bb22018-08-31 19:53:15 +0800443 fRenderedAtlasStack.current().setFillBatchID(fFiller.closeCurrentBatch());
444 fRenderedAtlasStack.current().setStrokeBatchID(fStroker.closeCurrentBatch());
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600445 }
446
Chris Dalton9414c962018-06-14 10:14:50 -0600447 // Build the GPU buffers to render path coverage counts. (This must not happen until after the
Chris Dalton09a7bb22018-08-31 19:53:15 +0800448 // final calls to fFiller/fStroker.closeCurrentBatch().)
Chris Daltone1639692018-08-20 14:00:30 -0600449 if (!fFiller.prepareToDraw(onFlushRP)) {
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600450 return false;
451 }
Chris Dalton09a7bb22018-08-31 19:53:15 +0800452 if (!fStroker.prepareToDraw(onFlushRP)) {
453 return false;
454 }
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600455
Chris Dalton351e80c2019-01-06 22:51:00 -0700456 // Draw the copies from 16-bit literal coverage atlas(es) into 8-bit cached atlas(es).
457 int copyRangeIdx = 0;
Chris Dalton4da70192018-06-18 09:51:36 -0600458 int baseCopyInstance = 0;
459 for (GrCCAtlasStack::Iter atlas(fCopyAtlasStack); atlas.next();) {
Chris Dalton351e80c2019-01-06 22:51:00 -0700460 int endCopyRange = atlas->getFillBatchID();
461 SkASSERT(endCopyRange > copyRangeIdx);
462
463 sk_sp<GrRenderTargetContext> rtc = atlas->makeRenderTargetContext(onFlushRP);
464 for (; copyRangeIdx < endCopyRange; ++copyRangeIdx) {
465 const CopyPathRange& copyRange = fCopyPathRanges[copyRangeIdx];
466 int endCopyInstance = baseCopyInstance + copyRange.fCount;
467 if (rtc) {
468 auto op = CopyAtlasOp::Make(rtc->surfPriv().getContext(), sk_ref_sp(this),
469 copyRange.fSrcProxy, baseCopyInstance, endCopyInstance,
470 atlas->drawBounds());
471 rtc->addDrawOp(GrNoClip(), std::move(op));
472 }
473 baseCopyInstance = endCopyInstance;
Chris Dalton4da70192018-06-18 09:51:36 -0600474 }
Chris Dalton351e80c2019-01-06 22:51:00 -0700475 out->push_back(std::move(rtc));
Chris Dalton4da70192018-06-18 09:51:36 -0600476 }
Chris Dalton351e80c2019-01-06 22:51:00 -0700477 SkASSERT(fCopyPathRanges.count() == copyRangeIdx);
478 SkASSERT(fNextCopyInstanceIdx == baseCopyInstance);
479 SkASSERT(baseCopyInstance == fEndCopyInstance);
Chris Dalton4da70192018-06-18 09:51:36 -0600480
Chris Dalton4da70192018-06-18 09:51:36 -0600481 // Render the coverage count atlas(es).
482 for (GrCCAtlasStack::Iter atlas(fRenderedAtlasStack); atlas.next();) {
Chris Dalton351e80c2019-01-06 22:51:00 -0700483 // Copies will be finished by the time we get to rendering new atlases. See if we can
484 // recycle any previous invalidated atlas textures instead of creating new ones.
Chris Daltonafde18f2018-06-22 12:44:19 -0600485 sk_sp<GrTexture> backingTexture;
Chris Dalton351e80c2019-01-06 22:51:00 -0700486 for (sk_sp<GrTexture>& texture : fRecyclableAtlasTextures) {
487 if (texture && atlas->currentHeight() == texture->height() &&
488 atlas->currentWidth() == texture->width()) {
489 backingTexture = skstd::exchange(texture, nullptr);
490 break;
491 }
Chris Daltonafde18f2018-06-22 12:44:19 -0600492 }
493
494 if (auto rtc = atlas->makeRenderTargetContext(onFlushRP, std::move(backingTexture))) {
Chris Dalton9414c962018-06-14 10:14:50 -0600495 auto op = RenderAtlasOp::Make(rtc->surfPriv().getContext(), sk_ref_sp(this),
Chris Dalton09a7bb22018-08-31 19:53:15 +0800496 atlas->getFillBatchID(), atlas->getStrokeBatchID(),
497 atlas->drawBounds());
Chris Dalton9414c962018-06-14 10:14:50 -0600498 rtc->addDrawOp(GrNoClip(), std::move(op));
499 out->push_back(std::move(rtc));
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600500 }
501 }
502
503 return true;
504}
Chris Dalton4da70192018-06-18 09:51:36 -0600505
Chris Dalton351e80c2019-01-06 22:51:00 -0700506void GrCCPerFlushResourceSpecs::cancelCopies() {
507 // Convert copies to cached draws.
508 fNumCachedPaths += fNumCopiedPaths[kFillIdx] + fNumCopiedPaths[kStrokeIdx];
509 fNumCopiedPaths[kFillIdx] = fNumCopiedPaths[kStrokeIdx] = 0;
510 fCopyPathStats[kFillIdx] = fCopyPathStats[kStrokeIdx] = GrCCRenderedPathStats();
Chris Dalton4da70192018-06-18 09:51:36 -0600511 fCopyAtlasSpecs = GrCCAtlas::Specs();
Chris Dalton4da70192018-06-18 09:51:36 -0600512}