ccpr: Initialize the atlas size more intelligently

Rather than always starting the atlas at 1024 x 1024, begin with the
first pow2 dimensions whose area is theoretically large enough to
contain the pending paths.

Bug: skia:
Change-Id: I263e77ff6a697e865f6b3b62b9df7002225f9544
Reviewed-on: https://skia-review.googlesource.com/133660
Commit-Queue: Chris Dalton <csmartdalton@google.com>
Reviewed-by: Brian Salomon <bsalomon@google.com>
Reviewed-by: Robert Phillips <robertphillips@google.com>
diff --git a/src/gpu/ccpr/GrCCAtlas.h b/src/gpu/ccpr/GrCCAtlas.h
index 184022e..b30bdbe 100644
--- a/src/gpu/ccpr/GrCCAtlas.h
+++ b/src/gpu/ccpr/GrCCAtlas.h
@@ -27,8 +27,22 @@
 class GrCCAtlas {
 public:
     using CoverageCountBatchID = int;
+    static constexpr int kPadding = 1;  // Amount of padding below and to the right of each path.
 
-    GrCCAtlas(const GrCaps&, int minSize);
+    // This struct encapsulates the minimum and desired requirements for an atlas, as well as an
+    // approximate number of pixels to help select a good initial size.
+    struct Specs {
+        int fMaxPreferredTextureSize = 0;
+        int fMinTextureSize = 0;
+        int fMinWidth = 0;  // If there are 100 20x10 paths, this should be 20.
+        int fMinHeight = 0;  // If there are 100 20x10 paths, this should be 10.
+        int fApproxNumPixels = 0;
+
+        // Add space for a rect in the desired atlas specs.
+        void accountForSpace(int width, int height);
+    };
+
+    GrCCAtlas(const Specs&);
     ~GrCCAtlas();
 
     bool addRect(int devWidth, int devHeight, SkIPoint16* loc);
@@ -51,8 +65,7 @@
 
     bool internalPlaceRect(int w, int h, SkIPoint16* loc);
 
-    const int fMaxAtlasSize;
-
+    const int fMaxTextureSize;
     int fWidth, fHeight;
     std::unique_ptr<Node> fTopNode;
     SkISize fDrawBounds = {0, 0};
@@ -61,4 +74,10 @@
     sk_sp<GrTextureProxy> fTextureProxy;
 };
 
+inline void GrCCAtlas::Specs::accountForSpace(int width, int height) {
+    fMinWidth = SkTMax(width, fMinWidth);
+    fMinHeight = SkTMax(height, fMinHeight);
+    fApproxNumPixels += (width + kPadding) * (height + kPadding);
+}
+
 #endif