ccpr: Generalize GrCCAtlas to work for cached atlases as well

Converts atlas offsets to SkIVector, adds a GrCCAtlasStack class,
moves the Op that renders the atlases into GrCCPerFlushResources, etc.

Bug: skia:
Change-Id: I5110be8e74da709f3ce84bb6798ead572142d0fa
Reviewed-on: https://skia-review.googlesource.com/134701
Commit-Queue: Chris Dalton <csmartdalton@google.com>
Reviewed-by: Robert Phillips <robertphillips@google.com>
diff --git a/src/gpu/ccpr/GrCCAtlas.cpp b/src/gpu/ccpr/GrCCAtlas.cpp
index 551b2a0..cbf6993 100644
--- a/src/gpu/ccpr/GrCCAtlas.cpp
+++ b/src/gpu/ccpr/GrCCAtlas.cpp
@@ -7,19 +7,13 @@
 
 #include "GrCCAtlas.h"
 
-#include "GrClip.h"
-#include "GrMemoryPool.h"
+#include "GrCaps.h"
 #include "GrOnFlushResourceProvider.h"
-#include "GrSurfaceContextPriv.h"
 #include "GrRectanizer_skyline.h"
 #include "GrRenderTargetContext.h"
-#include "GrSurfaceContextPriv.h"
 #include "GrTextureProxy.h"
 #include "SkMakeUnique.h"
 #include "SkMathPriv.h"
-#include "ccpr/GrCCCoverageProcessor.h"
-#include "ccpr/GrCCPathParser.h"
-#include "ops/GrDrawOp.h"
 
 class GrCCAtlas::Node {
 public:
@@ -50,50 +44,6 @@
     GrRectanizerSkyline fRectanizer;
 };
 
-class GrCCAtlas::DrawCoverageCountOp : public GrDrawOp {
-public:
-    DEFINE_OP_CLASS_ID
-
-    static std::unique_ptr<GrDrawOp> Make(GrContext* context,
-                                          sk_sp<const GrCCPathParser> parser,
-                                          CoverageCountBatchID batchID,
-                                          const SkISize& drawBounds) {
-        return std::unique_ptr<GrDrawOp>(new DrawCoverageCountOp(std::move(parser),
-                                                                 batchID, drawBounds));
-    }
-
-    // GrDrawOp interface.
-    const char* name() const override { return "GrCCAtlas::DrawCoverageCountOp"; }
-    FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
-    RequiresDstTexture finalize(const GrCaps&, const GrAppliedClip*,
-                                GrPixelConfigIsClamped) override { return RequiresDstTexture::kNo; }
-    bool onCombineIfPossible(GrOp* other, const GrCaps& caps) override { return false; }
-    void onPrepare(GrOpFlushState*) override {}
-    void onExecute(GrOpFlushState* flushState) override {
-        fParser->drawCoverageCount(flushState, fBatchID,
-                                   SkIRect::MakeWH(fDrawBounds.width(), fDrawBounds.height()));
-    }
-
-private:
-    friend class GrOpMemoryPool; // for ctor
-
-    DrawCoverageCountOp(sk_sp<const GrCCPathParser> parser, CoverageCountBatchID batchID,
-                        const SkISize& drawBounds)
-            : INHERITED(ClassID())
-            , fParser(std::move(parser))
-            , fBatchID(batchID)
-            , fDrawBounds(drawBounds) {
-        this->setBounds(SkRect::MakeIWH(fDrawBounds.width(), fDrawBounds.height()),
-                        GrOp::HasAABloat::kNo, GrOp::IsZeroArea::kNo);
-    }
-
-    const sk_sp<const GrCCPathParser> fParser;
-    const CoverageCountBatchID fBatchID;
-    const SkISize fDrawBounds;
-
-    typedef GrDrawOp INHERITED;
-};
-
 GrCCAtlas::GrCCAtlas(const Specs& specs)
         : fMaxTextureSize(SkTMax(SkTMax(specs.fMinHeight, specs.fMinWidth),
                                  specs.fMaxPreferredTextureSize)) {
@@ -121,17 +71,18 @@
 GrCCAtlas::~GrCCAtlas() {
 }
 
-bool GrCCAtlas::addRect(int w, int h, SkIPoint16* loc) {
-    // This can't be called anymore once setCoverageCountBatchID() has been called.
-    SkASSERT(!fCoverageCountBatchID);
+bool GrCCAtlas::addRect(const SkIRect& devIBounds, SkIVector* offset) {
+    // This can't be called anymore once makeClearedTextureProxy() has been called.
     SkASSERT(!fTextureProxy);
 
-    if (!this->internalPlaceRect(w, h, loc)) {
+    SkIPoint16 location;
+    if (!this->internalPlaceRect(devIBounds.width(), devIBounds.height(), &location)) {
         return false;
     }
+    offset->set(location.x() - devIBounds.left(), location.y() - devIBounds.top());
 
-    fDrawBounds.fWidth = SkTMax(fDrawBounds.width(), loc->x() + w);
-    fDrawBounds.fHeight = SkTMax(fDrawBounds.height(), loc->y() + h);
+    fDrawBounds.fWidth = SkTMax(fDrawBounds.width(), location.x() + devIBounds.width());
+    fDrawBounds.fHeight = SkTMax(fDrawBounds.height(), location.y() + devIBounds.height());
     return true;
 }
 
@@ -161,21 +112,21 @@
     return true;
 }
 
-sk_sp<GrRenderTargetContext> GrCCAtlas::finalize(GrOnFlushResourceProvider* onFlushRP,
-                                                 sk_sp<const GrCCPathParser> parser) {
-    SkASSERT(fCoverageCountBatchID);
+sk_sp<GrRenderTargetContext> GrCCAtlas::initInternalTextureProxy(
+        GrOnFlushResourceProvider* onFlushRP, GrPixelConfig config) {
     SkASSERT(!fTextureProxy);
     // Caller should have cropped any paths to the destination render target instead of asking for
     // an atlas larger than maxRenderTargetSize.
+    SkASSERT(SkTMax(fHeight, fWidth) <= fMaxTextureSize);
     SkASSERT(fMaxTextureSize <= onFlushRP->caps()->maxRenderTargetSize());
 
     GrSurfaceDesc desc;
     desc.fFlags = kRenderTarget_GrSurfaceFlag;
     desc.fWidth = fWidth;
     desc.fHeight = fHeight;
-    desc.fConfig = kAlpha_half_GrPixelConfig;
+    desc.fConfig = config;
     sk_sp<GrRenderTargetContext> rtc =
-            onFlushRP->makeRenderTargetContext(desc, kTopLeft_GrSurfaceOrigin, nullptr, nullptr);
+            onFlushRP->makeRenderTargetContext(desc, kTextureOrigin, nullptr, nullptr);
     if (!rtc) {
         SkDebugf("WARNING: failed to allocate a %ix%i atlas. Some paths will not be drawn.\n",
                  fWidth, fHeight);
@@ -185,14 +136,19 @@
     SkIRect clearRect = SkIRect::MakeSize(fDrawBounds);
     rtc->clear(&clearRect, 0, GrRenderTargetContext::CanClearFullscreen::kYes);
 
-    GrContext* context = rtc->surfPriv().getContext();
-
-    std::unique_ptr<GrDrawOp> op = DrawCoverageCountOp::Make(context,
-                                                             std::move(parser),
-                                                             fCoverageCountBatchID,
-                                                             fDrawBounds);
-    rtc->addDrawOp(GrNoClip(), std::move(op));
-
     fTextureProxy = sk_ref_sp(rtc->asTextureProxy());
     return rtc;
 }
+
+GrCCAtlas* GrCCAtlasStack::addRect(const SkIRect& devIBounds, SkIVector* offset) {
+    GrCCAtlas* retiredAtlas = nullptr;
+    if (fAtlases.empty() || !fAtlases.back().addRect(devIBounds, offset)) {
+        // The retired atlas is out of room and can't grow any bigger.
+        retiredAtlas = !fAtlases.empty() ? &fAtlases.back() : nullptr;
+        fAtlases.emplace_back(fSpecs);
+        SkASSERT(devIBounds.width() <= fSpecs.fMinWidth);
+        SkASSERT(devIBounds.height() <= fSpecs.fMinHeight);
+        SkAssertResult(fAtlases.back().addRect(devIBounds, offset));
+    }
+    return retiredAtlas;
+}