Delete GrCCAtlasStack

Change-Id: I88d86f98044233364ae26c228e80ad51b6020e7f
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/387376
Commit-Queue: Chris Dalton <csmartdalton@google.com>
Reviewed-by: Adlai Holler <adlai@google.com>
diff --git a/src/gpu/ccpr/GrCCAtlas.cpp b/src/gpu/ccpr/GrCCAtlas.cpp
index a08f86c..82eeeec 100644
--- a/src/gpu/ccpr/GrCCAtlas.cpp
+++ b/src/gpu/ccpr/GrCCAtlas.cpp
@@ -38,31 +38,3 @@
 
 GrCCAtlas::~GrCCAtlas() {
 }
-
-void GrCCAtlas::setFillBatchID(int id) {
-    // This can't be called anymore once makeRenderTargetContext() has been called.
-    SkASSERT(!this->isInstantiated());
-    fFillBatchID = id;
-}
-
-void GrCCAtlas::setEndStencilResolveInstance(int idx) {
-    // This can't be called anymore once makeRenderTargetContext() has been called.
-    SkASSERT(!this->isInstantiated());
-    fEndStencilResolveInstance = idx;
-}
-
-GrCCAtlas* GrCCAtlasStack::addRect(const SkIRect& devIBounds, SkIVector* devToAtlasOffset) {
-    GrCCAtlas* retiredAtlas = nullptr;
-    SkIPoint16 location;
-    if (fAtlases.empty() ||
-        !fAtlases.back().addRect(devIBounds.width(), devIBounds.height(), &location)) {
-        // The retired atlas is out of room and can't grow any bigger.
-        retiredAtlas = !fAtlases.empty() ? &fAtlases.back() : nullptr;
-        fAtlases.emplace_back(fSpecs, *fCaps);
-        SkASSERT(devIBounds.width() <= fSpecs.fMinWidth);
-        SkASSERT(devIBounds.height() <= fSpecs.fMinHeight);
-        SkAssertResult(fAtlases.back().addRect(devIBounds.width(), devIBounds.height(), &location));
-    }
-    devToAtlasOffset->set(location.x() - devIBounds.left(), location.y() - devIBounds.top());
-    return retiredAtlas;
-}
diff --git a/src/gpu/ccpr/GrCCAtlas.h b/src/gpu/ccpr/GrCCAtlas.h
index 5740b95..5691812 100644
--- a/src/gpu/ccpr/GrCCAtlas.h
+++ b/src/gpu/ccpr/GrCCAtlas.h
@@ -43,51 +43,6 @@
 
     GrCCAtlas(const Specs&, const GrCaps&);
     ~GrCCAtlas() override;
-
-    // This is an optional space for the caller to jot down user-defined instance data to use when
-    // rendering atlas content.
-    void setFillBatchID(int id);
-    int getFillBatchID() const { return fFillBatchID; }
-    void setEndStencilResolveInstance(int idx);
-    int getEndStencilResolveInstance() const { return fEndStencilResolveInstance; }
-
-private:
-    int fFillBatchID;
-    int fEndStencilResolveInstance;
-};
-
-/**
- * This class implements an unbounded stack of atlases. When the current atlas reaches the
- * implementation-dependent max texture size, a new one is pushed to the back and we continue on.
- */
-class GrCCAtlasStack {
-public:
-    using CCAtlasAllocator = GrTBlockList<GrCCAtlas, 4>;
-
-    GrCCAtlasStack(const GrCCAtlas::Specs& specs, const GrCaps* caps)
-            : fSpecs(specs), fCaps(caps) {}
-
-    bool empty() const { return fAtlases.empty(); }
-    const GrCCAtlas& front() const { SkASSERT(!this->empty()); return fAtlases.front(); }
-    GrCCAtlas& front() { SkASSERT(!this->empty()); return fAtlases.front(); }
-    GrCCAtlas& current() { SkASSERT(!this->empty()); return fAtlases.back(); }
-
-    CCAtlasAllocator::Iter atlases() { return fAtlases.items(); }
-    CCAtlasAllocator::CIter atlases() const { return fAtlases.items(); }
-
-    // Adds a rect to the current atlas and returns the offset from device space to atlas space.
-    // Call current() to get the atlas it was added to.
-    //
-    // If the return value is non-null, it means the given rect did not fit in the then-current
-    // atlas, so it was retired and a new one was added to the stack. The return value is the
-    // newly-retired atlas. The caller should call setUserBatchID() on the retired atlas before
-    // moving on.
-    GrCCAtlas* addRect(const SkIRect& devIBounds, SkIVector* devToAtlasOffset);
-
-private:
-    const GrCCAtlas::Specs fSpecs;
-    const GrCaps* const fCaps;
-    CCAtlasAllocator fAtlases;
 };
 
 inline void GrCCAtlas::Specs::accountForSpace(int width, int height) {
diff --git a/src/gpu/ccpr/GrCCClipPath.cpp b/src/gpu/ccpr/GrCCClipPath.cpp
index d69e0d5..9a63779 100644
--- a/src/gpu/ccpr/GrCCClipPath.cpp
+++ b/src/gpu/ccpr/GrCCClipPath.cpp
@@ -37,10 +37,10 @@
     }
 }
 
-const GrCCAtlas* GrCCClipPath::renderPathInAtlas(GrCCPerFlushResources* resources,
-                                                 GrOnFlushResourceProvider* onFlushRP) {
+std::unique_ptr<GrCCAtlas> GrCCClipPath::renderPathInAtlas(GrCCPerFlushResources* resources,
+                                                           GrOnFlushResourceProvider* onFlushRP) {
     SkASSERT(!fHasAtlas);
-    const GrCCAtlas* retiredAtlas = resources->renderDeviceSpacePathInAtlas(
+    auto retiredAtlas = resources->renderDeviceSpacePathInAtlas(
             onFlushRP, fAccessRect, fDeviceSpacePath, fPathDevIBounds,
             GrFillRuleForSkPath(fDeviceSpacePath), &fDevToAtlasOffset);
     SkDEBUGCODE(fHasAtlas = true);
diff --git a/src/gpu/ccpr/GrCCClipPath.h b/src/gpu/ccpr/GrCCClipPath.h
index a2e251e..4ccfc57 100644
--- a/src/gpu/ccpr/GrCCClipPath.h
+++ b/src/gpu/ccpr/GrCCClipPath.h
@@ -41,7 +41,8 @@
     // atlas, so it was retired and a new one was added to the stack. The return value is the
     // newly-retired atlas. (*NOT* the atlas this path will reside in.) The caller must call
     // assignAtlasTexture on all prior GrCCClipPaths that will use the retired atlas.
-    const GrCCAtlas* renderPathInAtlas(GrCCPerFlushResources*, GrOnFlushResourceProvider*);
+    std::unique_ptr<GrCCAtlas> renderPathInAtlas(GrCCPerFlushResources*,
+                                                 GrOnFlushResourceProvider*);
 
     const SkIVector& atlasTranslate() const {
         SkASSERT(fHasAtlas);
diff --git a/src/gpu/ccpr/GrCCPerFlushResources.cpp b/src/gpu/ccpr/GrCCPerFlushResources.cpp
index efbda63..0b16247 100644
--- a/src/gpu/ccpr/GrCCPerFlushResources.cpp
+++ b/src/gpu/ccpr/GrCCPerFlushResources.cpp
@@ -18,10 +18,10 @@
 
 GrCCPerFlushResources::GrCCPerFlushResources(GrOnFlushResourceProvider* onFlushRP,
                                              const GrCCAtlas::Specs& specs)
-        : fRenderedAtlasStack(specs, onFlushRP->caps()) {
+        : fAtlasSpecs(specs) {
 }
 
-const GrCCAtlas* GrCCPerFlushResources::renderDeviceSpacePathInAtlas(
+std::unique_ptr<GrCCAtlas> GrCCPerFlushResources::renderDeviceSpacePathInAtlas(
         GrOnFlushResourceProvider* onFlushRP, const SkIRect& clipIBounds, const SkPath& devPath,
         const SkIRect& devPathIBounds, GrFillRule fillRule, SkIVector* devToAtlasOffset) {
     SkASSERT(!devPath.isEmpty());
@@ -35,9 +35,8 @@
         enableScissorInAtlas = GrScissorTest::kEnabled;
     }
 
-    const GrCCAtlas* retiredAtlas = this->placeRenderedPathInAtlas(onFlushRP, clippedPathIBounds,
-                                                                   enableScissorInAtlas,
-                                                                   devToAtlasOffset);
+    auto retiredAtlas = this->placeRenderedPathInAtlas(onFlushRP, clippedPathIBounds,
+                                                       enableScissorInAtlas, devToAtlasOffset);
 
     SkMatrix atlasMatrix = SkMatrix::Translate(devToAtlasOffset->fX, devToAtlasOffset->fY);
     this->enqueueRenderedPath(devPath, fillRule, clippedPathIBounds, atlasMatrix,
@@ -46,15 +45,26 @@
     return retiredAtlas;
 }
 
-const GrCCAtlas* GrCCPerFlushResources::placeRenderedPathInAtlas(
+std::unique_ptr<GrCCAtlas> GrCCPerFlushResources::placeRenderedPathInAtlas(
         GrOnFlushResourceProvider* onFlushRP, const SkIRect& clippedPathIBounds,
         GrScissorTest scissorTest, SkIVector* devToAtlasOffset) {
-    GrCCAtlas* retiredAtlas = fRenderedAtlasStack.addRect(clippedPathIBounds, devToAtlasOffset);
-    if (retiredAtlas) {
-        // We did not fit in the previous coverage count atlas and it was retired. Render the
-        // retired atlas.
-        this->flushRenderedPaths(onFlushRP, retiredAtlas);
+    std::unique_ptr<GrCCAtlas> retiredAtlas;
+    SkIPoint16 location;
+    if (!fAtlas ||
+        !fAtlas->addRect(clippedPathIBounds.width(), clippedPathIBounds.height(), &location)) {
+        // The retired atlas is out of room and can't grow any bigger.
+        if (fAtlas) {
+            this->flushRenderedPaths(onFlushRP);
+            retiredAtlas = std::move(fAtlas);
+        }
+        fAtlas = std::make_unique<GrCCAtlas>(fAtlasSpecs, *onFlushRP->caps());
+        SkASSERT(clippedPathIBounds.width() <= fAtlasSpecs.fMinWidth);
+        SkASSERT(clippedPathIBounds.height() <= fAtlasSpecs.fMinHeight);
+        SkAssertResult(fAtlas->addRect(clippedPathIBounds.width(), clippedPathIBounds.height(),
+                                       &location));
     }
+    devToAtlasOffset->set(location.x() - clippedPathIBounds.left(),
+                          location.y() - clippedPathIBounds.top());
     return retiredAtlas;
 }
 
@@ -133,9 +143,9 @@
     surfaceDrawContext->addDrawOp(nullptr, std::move(coverOp));
 }
 
-void GrCCPerFlushResources::flushRenderedPaths(GrOnFlushResourceProvider* onFlushRP,
-                                               GrCCAtlas* atlas) {
-    auto surfaceDrawContext = atlas->instantiate(onFlushRP);
+void GrCCPerFlushResources::flushRenderedPaths(GrOnFlushResourceProvider* onFlushRP) {
+    SkASSERT(fAtlas);
+    auto surfaceDrawContext = fAtlas->instantiate(onFlushRP);
     if (!surfaceDrawContext) {
         for (int i = 0; i < (int)SK_ARRAY_COUNT(fAtlasPaths); ++i) {
             fAtlasPaths[i].fUberPath.reset();
@@ -165,7 +175,7 @@
     }
 
     draw_stencil_to_coverage(onFlushRP, surfaceDrawContext.get(),
-                             SkRect::MakeSize(SkSize::Make(atlas->drawBounds())));
+                             SkRect::MakeSize(SkSize::Make(fAtlas->drawBounds())));
 
     if (surfaceDrawContext->asSurfaceProxy()->requiresManualMSAAResolve()) {
         onFlushRP->addTextureResolveTask(sk_ref_sp(surfaceDrawContext->asTextureProxy()),
@@ -173,9 +183,9 @@
     }
 }
 
-const GrCCAtlas* GrCCPerFlushResources::finalize(GrOnFlushResourceProvider* onFlushRP) {
-    if (!fRenderedAtlasStack.empty()) {
-        this->flushRenderedPaths(onFlushRP, &fRenderedAtlasStack.current());
+std::unique_ptr<GrCCAtlas> GrCCPerFlushResources::finalize(GrOnFlushResourceProvider* onFlushRP) {
+    if (fAtlas) {
+        this->flushRenderedPaths(onFlushRP);
     }
 #ifdef SK_DEBUG
     // These paths should have been rendered and reset to empty by this point.
@@ -184,5 +194,5 @@
         SkASSERT(fAtlasPaths[i].fScissoredPaths.empty());
     }
 #endif
-    return &fRenderedAtlasStack.current();
+    return std::move(fAtlas);
 }
diff --git a/src/gpu/ccpr/GrCCPerFlushResources.h b/src/gpu/ccpr/GrCCPerFlushResources.h
index 2d520a4..3b3ec23 100644
--- a/src/gpu/ccpr/GrCCPerFlushResources.h
+++ b/src/gpu/ccpr/GrCCPerFlushResources.h
@@ -28,19 +28,19 @@
     // atlas, so it was retired and a new one was added to the stack. The return value is the
     // newly-retired atlas. (*NOT* the atlas the path was just drawn into.) The caller must call
     // assignAtlasTexture on all GrCCClipPaths that will use the retired atlas.
-    const GrCCAtlas* renderDeviceSpacePathInAtlas(
+    std::unique_ptr<GrCCAtlas> renderDeviceSpacePathInAtlas(
             GrOnFlushResourceProvider*, const SkIRect& clipIBounds, const SkPath& devPath,
             const SkIRect& devPathIBounds, GrFillRule fillRule, SkIVector* devToAtlasOffset);
 
     // Finishes off the GPU buffers and renders the atlas(es).
-    const GrCCAtlas* finalize(GrOnFlushResourceProvider*);
+    std::unique_ptr<GrCCAtlas> finalize(GrOnFlushResourceProvider*);
 
 private:
     // If the return value is non-null, it means the given path did not fit in the then-current
     // atlas, so it was retired and a new one was added to the stack. The return value is the
     // newly-retired atlas. (*NOT* the atlas the path was just drawn into.) The caller must call
     // assignAtlasTexture on all GrCCClipPaths that will use the retired atlas.
-    const GrCCAtlas* placeRenderedPathInAtlas(
+    std::unique_ptr<GrCCAtlas> placeRenderedPathInAtlas(
             GrOnFlushResourceProvider*, const SkIRect& clippedPathIBounds, GrScissorTest,
             SkIVector* devToAtlasOffset);
 
@@ -50,9 +50,9 @@
                              SkIVector devToAtlasOffset);
 
     // Renders all enqueued paths into the given atlas and clears our path queue.
-    void flushRenderedPaths(GrOnFlushResourceProvider*, GrCCAtlas*);
+    void flushRenderedPaths(GrOnFlushResourceProvider*);
 
-    GrCCAtlasStack fRenderedAtlasStack;
+    const GrCCAtlas::Specs fAtlasSpecs;
 
     // Paths to be rendered in the atlas we are currently building.
     struct AtlasPaths {
@@ -62,6 +62,8 @@
     static_assert((int)GrFillRule::kNonzero == 0);
     static_assert((int)GrFillRule::kEvenOdd == 1);
     AtlasPaths fAtlasPaths[2];  // One for "nonzero" fill rule and one for "even-odd".
+
+    std::unique_ptr<GrCCAtlas> fAtlas;
 };
 
 #endif
diff --git a/src/gpu/ccpr/GrCoverageCountingPathRenderer.cpp b/src/gpu/ccpr/GrCoverageCountingPathRenderer.cpp
index ae1be7e..21e158c 100644
--- a/src/gpu/ccpr/GrCoverageCountingPathRenderer.cpp
+++ b/src/gpu/ccpr/GrCoverageCountingPathRenderer.cpp
@@ -181,7 +181,7 @@
     ClipMapsIter end(flushingPaths.end());
     ClipMapsIter nextPathToAssign = it;  // The next GrCCClipPath to call assignAtlasTexture on.
     for (; it != end; ++it) {
-        if (const GrCCAtlas* retiredAtlas = it->renderPathInAtlas(&perFlushResources, onFlushRP)) {
+        if (auto retiredAtlas = it->renderPathInAtlas(&perFlushResources, onFlushRP)) {
             assign_atlas_textures(retiredAtlas->textureProxy()->peekTexture(), nextPathToAssign,
                                   it);
             nextPathToAssign = it;
@@ -189,7 +189,7 @@
     }
 
     // Allocate resources and then render the atlas(es).
-    const GrCCAtlas* atlas = perFlushResources.finalize(onFlushRP);
+    auto atlas = perFlushResources.finalize(onFlushRP);
     assign_atlas_textures(atlas->textureProxy()->peekTexture(), nextPathToAssign, end);
 }