Have GrGlyphCache and GrAtlasManager compute the atlas limits independently

DDL contexts will have a GrGlyphCache but not a GrAtlasManager. This CL
disentangles the computation of the atlas limits so the GrGlyphCache
can function independently.

Change-Id: Ia698c7d1ec625d1a0d1f0b5521b56731cfeafde9
Reviewed-on: https://skia-review.googlesource.com/112708
Commit-Queue: Robert Phillips <robertphillips@google.com>
Reviewed-by: Jim Van Verth <jvanverth@google.com>
diff --git a/src/gpu/GrContext.cpp b/src/gpu/GrContext.cpp
index d29a173..16dfab3 100644
--- a/src/gpu/GrContext.cpp
+++ b/src/gpu/GrContext.cpp
@@ -125,7 +125,7 @@
                                                allowMultitexturing);
         this->contextPriv().addOnFlushCallbackObject(fFullAtlasManager);
 
-        glyphCache->setGlyphSizeLimit(fFullAtlasManager->getGlyphSizeLimit());
+        SkASSERT(glyphCache->getGlyphSizeLimit() == fFullAtlasManager->getGlyphSizeLimit());
         return true;
     }
 
@@ -396,7 +396,7 @@
     fDrawingManager.reset(new GrDrawingManager(this, prcOptions, atlasTextContextOptions,
                                                &fSingleOwner, options.fSortRenderTargets));
 
-    fGlyphCache = new GrGlyphCache;
+    fGlyphCache = new GrGlyphCache(fCaps.get(), options.fGlyphCacheTextureMaximumBytes);
 
     fTextBlobCache.reset(new GrTextBlobCache(TextBlobCacheOverBudgetCB,
                                              this, this->uniqueID(), SkToBool(fGpu)));
diff --git a/src/gpu/text/GrAtlasManager.cpp b/src/gpu/text/GrAtlasManager.cpp
index b688c1c..208e707 100644
--- a/src/gpu/text/GrAtlasManager.cpp
+++ b/src/gpu/text/GrAtlasManager.cpp
@@ -12,28 +12,42 @@
 #include "GrGlyphCache.h"
 #include "GrProxyProvider.h"
 
+
+void GrRestrictedAtlasManager::ComputeAtlasLimits(const GrCaps* caps, float maxTextureBytes,
+                                                  int* maxDim, int* minDim,
+                                                  int* maxPlot, int* minPlot) {
+    SkASSERT(maxDim && minDim && maxPlot && minPlot);
+
+    // Calculate RGBA size. Must be between 512 x 256 and MaxTextureSize x MaxTextureSize / 2
+    int log2MaxTextureSize = SkPrevLog2(caps->maxTextureSize());
+    int log2MaxDim = 9;
+    for (; log2MaxDim <= log2MaxTextureSize; ++log2MaxDim) {
+        int maxDimTmp = 1 << log2MaxDim;
+        int minDimTmp = 1 << (log2MaxDim - 1);
+
+        if (maxDimTmp * minDimTmp * 4 >= maxTextureBytes) {
+            break;
+        }
+    }
+
+
+    int log2MinDim = log2MaxDim - 1;
+    *maxDim = 1 << log2MaxDim;
+    *minDim = 1 << log2MinDim;
+    // Plots are either 256 or 512.
+    *maxPlot = SkTMin(512, SkTMax(256, 1 << (log2MaxDim - 2)));
+    *minPlot = SkTMin(512, SkTMax(256, 1 << (log2MaxDim - 3)));
+}
+
 GrRestrictedAtlasManager::GrRestrictedAtlasManager(
                                         sk_sp<const GrCaps> caps,
                                         float maxTextureBytes,
                                         GrDrawOpAtlas::AllowMultitexturing allowMultitexturing)
             : fCaps(std::move(caps))
             , fAllowMultitexturing(allowMultitexturing) {
-    // Calculate RGBA size. Must be between 512 x 256 and MaxTextureSize x MaxTextureSize / 2
-    int log2MaxTextureSize = SkPrevLog2(fCaps->maxTextureSize());
-    int log2MaxDim = 9;
-    for (; log2MaxDim <= log2MaxTextureSize; ++log2MaxDim) {
-        int maxDim = 1 << log2MaxDim;
-        int minDim = 1 << (log2MaxDim - 1);
 
-        if (maxDim * minDim * 4 >= maxTextureBytes) break;
-    }
-
-    int log2MinDim = log2MaxDim - 1;
-    int maxDim = 1 << log2MaxDim;
-    int minDim = 1 << log2MinDim;
-    // Plots are either 256 or 512.
-    int maxPlot = SkTMin(512, SkTMax(256, 1 << (log2MaxDim - 2)));
-    int minPlot = SkTMin(512, SkTMax(256, 1 << (log2MaxDim - 3)));
+    int maxDim, minDim, maxPlot, minPlot;
+    ComputeAtlasLimits(fCaps.get(), maxTextureBytes, &maxDim, &minDim, &maxPlot, &minPlot);
 
     // Setup default atlas configs. The A8 atlas uses maxDim for both width and height, as the A8
     // format is already very compact.
diff --git a/src/gpu/text/GrAtlasManager.h b/src/gpu/text/GrAtlasManager.h
index 9241193..4b1c667 100644
--- a/src/gpu/text/GrAtlasManager.h
+++ b/src/gpu/text/GrAtlasManager.h
@@ -43,6 +43,9 @@
 
     SkScalar getGlyphSizeLimit() const { return fGlyphSizeLimit; }
 
+    static void ComputeAtlasLimits(const GrCaps* caps, float maxTextureBytes,
+                                   int* maxDim, int* minDim, int* maxPlot, int* minPlot);
+
 protected:
     // There is a 1:1 mapping between GrMaskFormats and atlas indices
     static int MaskFormatToAtlasIndex(GrMaskFormat format) {
diff --git a/src/gpu/text/GrGlyphCache.cpp b/src/gpu/text/GrGlyphCache.cpp
index 49e24b8..79f7748 100644
--- a/src/gpu/text/GrGlyphCache.cpp
+++ b/src/gpu/text/GrGlyphCache.cpp
@@ -12,9 +12,14 @@
 #include "SkAutoMalloc.h"
 #include "SkDistanceFieldGen.h"
 
-GrGlyphCache::GrGlyphCache()
+GrGlyphCache::GrGlyphCache(const GrCaps* caps, float maxTextureBytes)
         : fPreserveStrike(nullptr)
         , fGlyphSizeLimit(0) {
+
+    int maxDim, minDim, maxPlot, minPlot;
+    GrRestrictedAtlasManager::ComputeAtlasLimits(caps, maxTextureBytes,
+                                                 &maxDim, &minDim, &maxPlot, &minPlot);
+    fGlyphSizeLimit = minPlot;
 }
 
 GrGlyphCache::~GrGlyphCache() {
diff --git a/src/gpu/text/GrGlyphCache.h b/src/gpu/text/GrGlyphCache.h
index bbc199f..cff3095 100644
--- a/src/gpu/text/GrGlyphCache.h
+++ b/src/gpu/text/GrGlyphCache.h
@@ -108,10 +108,9 @@
  */
 class GrGlyphCache {
 public:
-    GrGlyphCache();
+    GrGlyphCache(const GrCaps* caps, float maxTextureBytes);
     ~GrGlyphCache();
 
-    void setGlyphSizeLimit(SkScalar sizeLimit) { fGlyphSizeLimit = sizeLimit; }
     SkScalar getGlyphSizeLimit() const { return fGlyphSizeLimit; }
 
     void setStrikeToPreserve(GrTextStrike* strike) { fPreserveStrike = strike; }