Pointer to GrStrikeCache on GrTextBlob

This will allow all the glyph drawing closures to live on the
GrTextBlob.

Other cleanup
* Rename glyphCache things to grStrikeCache.

Change-Id: I03e0353a1434230086a08184221272f1e5751ae6
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/197244
Commit-Queue: Herb Derby <herb@google.com>
Reviewed-by: Mike Klein <mtklein@google.com>
diff --git a/src/atlastext/SkAtlasTextTarget.cpp b/src/atlastext/SkAtlasTextTarget.cpp
index 1ba6c12..ecdde4a 100644
--- a/src/atlastext/SkAtlasTextTarget.cpp
+++ b/src/atlastext/SkAtlasTextTarget.cpp
@@ -219,7 +219,7 @@
     FlushInfo flushInfo;
     SkExclusiveStrikePtr autoGlyphCache;
     auto& context = target->context()->internal();
-    auto glyphCache = context.grContext()->priv().getGlyphCache();
+    auto glyphCache = context.grContext()->priv().getGrStrikeCache();
     auto atlasManager = context.grContext()->priv().getAtlasManager();
     auto resourceProvider = context.grContext()->priv().resourceProvider();
 
diff --git a/src/atlastext/SkInternalAtlasTextContext.cpp b/src/atlastext/SkInternalAtlasTextContext.cpp
index ba2924e..9be17a7 100644
--- a/src/atlastext/SkInternalAtlasTextContext.cpp
+++ b/src/atlastext/SkInternalAtlasTextContext.cpp
@@ -50,7 +50,7 @@
 }
 
 GrStrikeCache* SkInternalAtlasTextContext::glyphCache() {
-    return fGrContext->priv().getGlyphCache();
+    return fGrContext->priv().getGrStrikeCache();
 }
 
 GrTextBlobCache* SkInternalAtlasTextContext::textBlobCache() {
diff --git a/src/core/SkGlyphRunPainter.cpp b/src/core/SkGlyphRunPainter.cpp
index d14b110..77ca79a 100644
--- a/src/core/SkGlyphRunPainter.cpp
+++ b/src/core/SkGlyphRunPainter.cpp
@@ -636,7 +636,7 @@
                                                  (mf && !as_MFB(mf)->asABlur(&blurRec)));
     SkScalerContextFlags scalerContextFlags = ComputeScalerContextFlags(target->colorSpaceInfo());
 
-    auto glyphCache = context->priv().getGlyphCache();
+    auto grStrikeCache = context->priv().getGrStrikeCache();
     GrTextBlobCache* textBlobCache = context->priv().getTextBlobCache();
 
     sk_sp<GrTextBlob> cacheBlob;
@@ -670,19 +670,21 @@
             // TODO we could probably get away reuse most of the time if the pointer is unique,
             // but we'd have to clear the subrun information
             textBlobCache->remove(cacheBlob.get());
-            cacheBlob = textBlobCache->makeCachedBlob(glyphRunList, key, blurRec, listPaint, color);
+            cacheBlob = textBlobCache->makeCachedBlob(
+                    glyphRunList, key, blurRec, listPaint, color, grStrikeCache);
             cacheBlob->generateFromGlyphRunList(
-                    glyphCache, *context->priv().caps()->shaderCaps(), fOptions,
+                    *context->priv().caps()->shaderCaps(), fOptions,
                     listPaint, scalerContextFlags, viewMatrix, props,
                     glyphRunList, target->glyphPainter());
         } else {
             textBlobCache->makeMRU(cacheBlob.get());
 
             if (CACHE_SANITY_CHECK) {
-                sk_sp<GrTextBlob> sanityBlob(textBlobCache->makeBlob(glyphRunList, color));
+                sk_sp<GrTextBlob> sanityBlob(textBlobCache->makeBlob(
+                        glyphRunList, color, grStrikeCache));
                 sanityBlob->setupKey(key, blurRec, listPaint);
                 cacheBlob->generateFromGlyphRunList(
-                        glyphCache, *context->priv().caps()->shaderCaps(), fOptions,
+                        *context->priv().caps()->shaderCaps(), fOptions,
                         listPaint, scalerContextFlags, viewMatrix, props, glyphRunList,
                         target->glyphPainter());
                 GrTextBlob::AssertEqual(*sanityBlob, *cacheBlob);
@@ -690,12 +692,13 @@
         }
     } else {
         if (canCache) {
-            cacheBlob = textBlobCache->makeCachedBlob(glyphRunList, key, blurRec, listPaint, color);
+            cacheBlob = textBlobCache->makeCachedBlob(
+                    glyphRunList, key, blurRec, listPaint, color, grStrikeCache);
         } else {
-            cacheBlob = textBlobCache->makeBlob(glyphRunList, color);
+            cacheBlob = textBlobCache->makeBlob(glyphRunList, color, grStrikeCache);
         }
         cacheBlob->generateFromGlyphRunList(
-                glyphCache, *context->priv().caps()->shaderCaps(), fOptions, listPaint,
+                *context->priv().caps()->shaderCaps(), fOptions, listPaint,
                 scalerContextFlags, viewMatrix, props, glyphRunList,
                 target->glyphPainter());
     }
@@ -791,8 +794,7 @@
     }
 }
 
-void GrTextBlob::generateFromGlyphRunList(GrStrikeCache* glyphCache,
-                                          const GrShaderCaps& shaderCaps,
+void GrTextBlob::generateFromGlyphRunList(const GrShaderCaps& shaderCaps,
                                           const GrTextContext::Options& options,
                                           const SkPaint& paint,
                                           SkScalerContextFlags scalerContextFlags,
@@ -824,14 +826,14 @@
                     hasWCoord);
 
             auto processMasks =
-                    [run, glyphCache, blob{this}]
+                    [run, blob{this}]
                             (SkSpan<const SkGlyphRunListPainter::GlyphAndPos> masks,
                              SkStrikeInterface* strike, SkScalar textScale, SkScalar minScale,
                              SkScalar maxScale) {
                         blob->setMinAndMaxScale(minScale, maxScale);
                         run->setupFont(strike->strikeSpec());
                         sk_sp<GrTextStrike> currStrike =
-                                glyphCache->getStrike(strike->getDescriptor());
+                                blob->fStrikeCache->getStrike(strike->getDescriptor());
                         for (const auto& mask : masks) {
                             run->appendSourceSpaceGlyph(
                                     currStrike, *mask.glyph, mask.position, textScale);
@@ -850,11 +852,12 @@
                     }
                 };
 
-            auto argbFallbackDevice = [blob{this}, run, glyphCache]
+            auto argbFallbackDevice = [blob{this}, run]
                     (SkSpan<const SkGlyphRunListPainter::GlyphAndPos> masks,
                      SkStrikeInterface* strike) {
                 blob->setHasBitmap();
-                sk_sp<GrTextStrike> grStrike = glyphCache->getStrike(strike->getDescriptor());
+                sk_sp<GrTextStrike> grStrike =
+                        blob->fStrikeCache->getStrike(strike->getDescriptor());
                 auto subRun = run->initARGBFallback();
                 run->setupFont(strike->strikeSpec());
                 subRun->setStrike(grStrike);
@@ -863,13 +866,14 @@
                 }
             };
 
-            auto argbFallbackSource = [blob{this}, run, glyphCache]
+            auto argbFallbackSource = [blob{this}, run]
                     (SkSpan<const SkGlyphRunListPainter::GlyphAndPos> masks,
                      SkStrikeInterface* strike,
                      SkScalar strikeToSourceRatio,
                      bool hasW) {
                 blob->setHasBitmap();
-                sk_sp<GrTextStrike> grStrike = glyphCache->getStrike(strike->getDescriptor());
+                sk_sp<GrTextStrike> grStrike =
+                        blob->fStrikeCache->getStrike(strike->getDescriptor());
                 auto subRun = run->initARGBFallback();
                 run->setupFont(strike->strikeSpec());
                 subRun->setStrike(grStrike);
@@ -904,11 +908,12 @@
                     }
                 };
 
-            auto argbFallbackDevice = [blob{this}, run, glyphCache]
+            auto argbFallbackDevice = [blob{this}, run]
                     (SkSpan<const SkGlyphRunListPainter::GlyphAndPos> masks,
                      SkStrikeInterface* strike) {
                 blob->setHasBitmap();
-                sk_sp<GrTextStrike> grStrike = glyphCache->getStrike(strike->getDescriptor());
+                sk_sp<GrTextStrike> grStrike =
+                        blob->fStrikeCache->getStrike(strike->getDescriptor());
                 auto subRun = run->initARGBFallback();
                 run->setupFont(strike->strikeSpec());
                 subRun->setStrike(grStrike);
@@ -917,13 +922,14 @@
                 }
             };
 
-            auto argbFallbackSource = [blob{this}, run, glyphCache]
+            auto argbFallbackSource = [blob{this}, run]
                     (SkSpan<const SkGlyphRunListPainter::GlyphAndPos> masks,
                      SkStrikeInterface* strike,
                      SkScalar strikeToSourceRatio,
                      bool hasW) {
                 blob->setHasBitmap();
-                sk_sp<GrTextStrike> grStrike = glyphCache->getStrike(strike->getDescriptor());
+                sk_sp<GrTextStrike> grStrike =
+                        blob->fStrikeCache->getStrike(strike->getDescriptor());
                 auto subRun = run->initARGBFallback();
                 run->setupFont(strike->strikeSpec());
                 subRun->setStrike(grStrike);
@@ -944,11 +950,12 @@
             this->setHasBitmap();
 
             auto processMasks =
-                [run, glyphCache]
+                [run, blob{this}]
                 (SkSpan<const SkGlyphRunListPainter::GlyphAndPos> masks,
                         SkStrikeInterface* strike) {
                     run->setupFont(strike->strikeSpec());
-                    sk_sp<GrTextStrike> currStrike = glyphCache->getStrike(strike->getDescriptor());
+                    sk_sp<GrTextStrike> currStrike =
+                            blob->fStrikeCache->getStrike(strike->getDescriptor());
                     for (const auto& mask : masks) {
                         SkPoint pt{SkScalarFloorToScalar(mask.position.fX),
                                    SkScalarFloorToScalar(mask.position.fY)};
@@ -996,7 +1003,7 @@
         return nullptr;
     }
 
-    auto glyphCache = direct->priv().getGlyphCache();
+    auto strikeCache = direct->priv().getGrStrikeCache();
 
     static SkSurfaceProps surfaceProps(SkSurfaceProps::kLegacyFontHost_InitType);
 
@@ -1012,12 +1019,12 @@
     auto glyphRunList = builder.useGlyphRunList();
     sk_sp<GrTextBlob> blob;
     if (!glyphRunList.empty()) {
-        blob = direct->priv().getTextBlobCache()->makeBlob(glyphRunList, color);
+        blob = direct->priv().getTextBlobCache()->makeBlob(glyphRunList, color, strikeCache);
         // Use the text and textLen below, because we don't want to mess with the paint.
         SkScalerContextFlags scalerContextFlags =
                 ComputeScalerContextFlags(rtc->colorSpaceInfo());
         blob->generateFromGlyphRunList(
-                glyphCache, *context->priv().caps()->shaderCaps(), textContext->fOptions,
+                *context->priv().caps()->shaderCaps(), textContext->fOptions,
                 skPaint, scalerContextFlags, viewMatrix, surfaceProps,
                 glyphRunList, rtc->textTarget()->glyphPainter());
     }
diff --git a/src/gpu/GrContext.cpp b/src/gpu/GrContext.cpp
index 4b5f1bb..05b0239 100644
--- a/src/gpu/GrContext.cpp
+++ b/src/gpu/GrContext.cpp
@@ -72,7 +72,7 @@
 
     SkASSERT(this->drawingManager());
     SkASSERT(this->caps());
-    SkASSERT(this->getGlyphCache());
+    SkASSERT(this->getGrStrikeCache());
     SkASSERT(this->getTextBlobCache());
 
     if (fGpu) {
@@ -160,7 +160,7 @@
 
     // TODO: the glyph cache doesn't hold any GpuResources so this call should not be needed here.
     // Some slack in the GrTextBlob's implementation requires it though. That could be fixed.
-    this->getGlyphCache()->freeAll();
+    this->getGrStrikeCache()->freeAll();
 
     this->drawingManager()->freeGpuResources();
 
diff --git a/src/gpu/GrContextPriv.h b/src/gpu/GrContextPriv.h
index 24f3e2a..374e049 100644
--- a/src/gpu/GrContextPriv.h
+++ b/src/gpu/GrContextPriv.h
@@ -66,7 +66,7 @@
     sk_sp<GrOpMemoryPool> refOpMemoryPool();
     GrOpMemoryPool* opMemoryPool() { return fContext->opMemoryPool(); }
 
-    GrStrikeCache* getGlyphCache() { return fContext->getGlyphCache(); }
+    GrStrikeCache* getGrStrikeCache() { return fContext->getGrStrikeCache(); }
     GrTextBlobCache* getTextBlobCache() { return fContext->getTextBlobCache(); }
 
     /**
diff --git a/src/gpu/GrLegacyDirectContext.cpp b/src/gpu/GrLegacyDirectContext.cpp
index 490b62b..e580ac8 100644
--- a/src/gpu/GrLegacyDirectContext.cpp
+++ b/src/gpu/GrLegacyDirectContext.cpp
@@ -85,7 +85,7 @@
             allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kYes;
         }
 
-        GrStrikeCache* glyphCache = this->priv().getGlyphCache();
+        GrStrikeCache* glyphCache = this->priv().getGrStrikeCache();
         GrProxyProvider* proxyProvider = this->priv().proxyProvider();
 
         fAtlasManager = new GrAtlasManager(proxyProvider, glyphCache,
diff --git a/src/gpu/GrOpFlushState.cpp b/src/gpu/GrOpFlushState.cpp
index a02b2f4..7a6aa18 100644
--- a/src/gpu/GrOpFlushState.cpp
+++ b/src/gpu/GrOpFlushState.cpp
@@ -178,7 +178,7 @@
 }
 
 GrStrikeCache* GrOpFlushState::glyphCache() const {
-    return fGpu->getContext()->priv().getGlyphCache();
+    return fGpu->getContext()->priv().getGrStrikeCache();
 }
 
 GrAtlasManager* GrOpFlushState::atlasManager() const {
diff --git a/src/gpu/GrRecordingContext.cpp b/src/gpu/GrRecordingContext.cpp
index 553fc7d..23f25cd 100644
--- a/src/gpu/GrRecordingContext.cpp
+++ b/src/gpu/GrRecordingContext.cpp
@@ -55,7 +55,7 @@
         return false;
     }
 
-    fGlyphCache.reset(new GrStrikeCache(this->caps(),
+    fStrikeCache.reset(new GrStrikeCache(this->caps(),
                                         this->options().fGlyphCacheTextureMaximumBytes));
 
     fTextBlobCache.reset(new GrTextBlobCache(textblobcache_overbudget_CB, this,
@@ -102,7 +102,7 @@
 void GrRecordingContext::abandonContext() {
     INHERITED::abandonContext();
 
-    fGlyphCache->freeAll();
+    fStrikeCache->freeAll();
     fTextBlobCache->freeAll();
 }
 
diff --git a/src/gpu/GrRecordingContextPriv.h b/src/gpu/GrRecordingContextPriv.h
index 879b56e..46f13f2 100644
--- a/src/gpu/GrRecordingContextPriv.h
+++ b/src/gpu/GrRecordingContextPriv.h
@@ -50,7 +50,7 @@
     sk_sp<GrOpMemoryPool> refOpMemoryPool();
     GrOpMemoryPool* opMemoryPool() { return fContext->opMemoryPool(); }
 
-    GrStrikeCache* getGlyphCache() { return fContext->getGlyphCache(); }
+    GrStrikeCache* getGrStrikeCache() { return fContext->getGrStrikeCache(); }
     GrTextBlobCache* getTextBlobCache() { return fContext->getTextBlobCache(); }
 
     /**
diff --git a/src/gpu/text/GrTextBlob.cpp b/src/gpu/text/GrTextBlob.cpp
index 8feaa1a..0ab8f6d 100644
--- a/src/gpu/text/GrTextBlob.cpp
+++ b/src/gpu/text/GrTextBlob.cpp
@@ -23,7 +23,10 @@
     return ((s + (N-1)) / N) * N;
 }
 
-sk_sp<GrTextBlob> GrTextBlob::Make(int glyphCount, int runCount, GrColor color) {
+sk_sp<GrTextBlob> GrTextBlob::Make(int glyphCount,
+                                   int runCount,
+                                   GrColor color,
+                                   GrStrikeCache* strikeCache) {
     // We allocate size for the GrTextBlob itself, plus size for the vertices array,
     // and size for the glyphIds array.
     size_t verticesCount = glyphCount * kVerticesPerGlyph * kMaxVASize;
@@ -40,7 +43,7 @@
         sk_bzero(allocation, size);
     }
 
-    sk_sp<GrTextBlob> blob{new (allocation) GrTextBlob{}};
+    sk_sp<GrTextBlob> blob{new (allocation) GrTextBlob{strikeCache}};
     blob->fSize = size;
 
     // setup offsets for vertices / glyphs
diff --git a/src/gpu/text/GrTextBlob.h b/src/gpu/text/GrTextBlob.h
index 7861439..36ec39e 100644
--- a/src/gpu/text/GrTextBlob.h
+++ b/src/gpu/text/GrTextBlob.h
@@ -55,8 +55,7 @@
 
     class VertexRegenerator;
 
-    void generateFromGlyphRunList(GrStrikeCache* glyphCache,
-                                  const GrShaderCaps& shaderCaps,
+    void generateFromGlyphRunList(const GrShaderCaps& shaderCaps,
                                   const GrTextContext::Options& options,
                                   const SkPaint& paint,
                                   SkScalerContextFlags scalerContextFlags,
@@ -65,7 +64,11 @@
                                   const SkGlyphRunList& glyphRunList,
                                   SkGlyphRunListPainter* glyphPainter);
 
-    static sk_sp<GrTextBlob> Make(int glyphCount, int runCount, GrColor color);
+    static sk_sp<GrTextBlob> Make(
+            int glyphCount,
+            int runCount,
+            GrColor color,
+            GrStrikeCache* strikeCache);
 
     /**
      * We currently force regeneration of a blob if old or new matrix differ in having perspective.
@@ -250,10 +253,7 @@
                                           GrTextTarget*);
 
 private:
-    GrTextBlob()
-        : fMaxMinScale(-SK_ScalarMax)
-        , fMinMaxScale(SK_ScalarMax)
-        , fTextType(0) {}
+    GrTextBlob(GrStrikeCache* strikeCache) : fStrikeCache{strikeCache} { }
 
     // This function will only be called when we are generating a blob from scratch. We record the
     // initial view matrix and initial offsets(x,y), because we record vertex bounds relative to
@@ -532,6 +532,10 @@
     char* fVertices;
     GrGlyph** fGlyphs;
     Run* fRuns;
+
+    // Lifetime: The GrStrikeCache is owned by and has the same lifetime as the GrRecordingContext.
+    // The GrRecordingContext also owns the GrTextBlob cache which owns this GrTextBlob.
+    GrStrikeCache* const fStrikeCache;
     SkMaskFilterBase::BlurRec fBlurRec;
     StrokeInfo fStrokeInfo;
     Key fKey;
@@ -545,11 +549,11 @@
     // We can reuse distance field text, but only if the new viewmatrix would not result in
     // a mip change.  Because there can be multiple runs in a blob, we track the overall
     // maximum minimum scale, and minimum maximum scale, we can support before we need to regen
-    SkScalar fMaxMinScale;
-    SkScalar fMinMaxScale;
+    SkScalar fMaxMinScale{-SK_ScalarMax};
+    SkScalar fMinMaxScale{SK_ScalarMax};
     int fRunCount{0};
     int fRunCountLimit;
-    uint8_t fTextType;
+    uint8_t fTextType{0};
 };
 
 /**
diff --git a/src/gpu/text/GrTextBlobCache.h b/src/gpu/text/GrTextBlobCache.h
index 4e9b5ce..03d48ad 100644
--- a/src/gpu/text/GrTextBlobCache.h
+++ b/src/gpu/text/GrTextBlobCache.h
@@ -33,16 +33,20 @@
     }
     ~GrTextBlobCache();
 
-    sk_sp<GrTextBlob> makeBlob(const SkGlyphRunList& glyphRunList, GrColor color) {
-        return GrTextBlob::Make(glyphRunList.totalGlyphCount(), glyphRunList.size(), color);
+    sk_sp<GrTextBlob> makeBlob(const SkGlyphRunList& glyphRunList,
+                               GrColor color,
+                               GrStrikeCache* strikeCache) {
+        return GrTextBlob::Make(
+                glyphRunList.totalGlyphCount(), glyphRunList.size(), color, strikeCache);
     }
 
     sk_sp<GrTextBlob> makeCachedBlob(const SkGlyphRunList& glyphRunList,
                                      const GrTextBlob::Key& key,
                                      const SkMaskFilterBase::BlurRec& blurRec,
                                      const SkPaint& paint,
-                                     GrColor color) {
-        sk_sp<GrTextBlob> cacheBlob(makeBlob(glyphRunList, color));
+                                     GrColor color,
+                                     GrStrikeCache* strikeCache) {
+        sk_sp<GrTextBlob> cacheBlob(makeBlob(glyphRunList, color, strikeCache));
         cacheBlob->setupKey(key, blurRec, paint);
         this->add(cacheBlob);
         glyphRunList.temporaryShuntBlobNotifyAddedToCache(fUniqueID);