Remove attach and detach glyph cache.

BUG=skia:7515

Change-Id: Ib978e3cf4cfffdefe6453feb520e5e73684abf2e
Reviewed-on: https://skia-review.googlesource.com/112560
Reviewed-by: Ben Wagner <bungeman@google.com>
Commit-Queue: Herb Derby <herb@google.com>
diff --git a/bench/PathTextBench.cpp b/bench/PathTextBench.cpp
index 8920223..4e83d33 100644
--- a/bench/PathTextBench.cpp
+++ b/bench/PathTextBench.cpp
@@ -47,7 +47,7 @@
     void onDelayedSetup() override {
         SkPaint defaultPaint;
         SkAutoGlyphCache agc(defaultPaint, nullptr, &SkMatrix::I());
-        SkGlyphCache* cache = agc.getCache();
+        SkGlyphCache* cache = agc.get();
         for (int i = 0; i < kNumGlyphs; ++i) {
             SkGlyphID id = cache->unicharToGlyph(kGlyphs[i]);
             cache->getScalerContext()->getPath(SkPackedGlyphID(id), &fGlyphs[i]);
diff --git a/bench/SkGlyphCacheBench.cpp b/bench/SkGlyphCacheBench.cpp
index 1cc257f..c760eb5 100644
--- a/bench/SkGlyphCacheBench.cpp
+++ b/bench/SkGlyphCacheBench.cpp
@@ -21,7 +21,7 @@
     for (SkScalar i = 8; i < 64; i++) {
         paint->setTextSize(i);
         SkAutoGlyphCacheNoGamma autoCache(*paint, nullptr, nullptr);
-        SkGlyphCache* cache = autoCache.getCache();
+        SkGlyphCache* cache = autoCache.get();
         uint16_t glyphs['z'];
         for (int c = ' '; c < 'z'; c++) {
             glyphs[c] = cache->unicharToGlyph(c);
diff --git a/samplecode/SamplePathText.cpp b/samplecode/SamplePathText.cpp
index b6a730f..c6c07ee 100644
--- a/samplecode/SamplePathText.cpp
+++ b/samplecode/SamplePathText.cpp
@@ -25,7 +25,7 @@
     PathText() {
         SkPaint defaultPaint;
         SkAutoGlyphCache agc(defaultPaint, nullptr, &SkMatrix::I());
-        SkGlyphCache* cache = agc.getCache();
+        SkGlyphCache* cache = agc.get();
         SkPath glyphPaths[52];
         for (int i = 0; i < 52; ++i) {
             // I and l are rects on OS X ...
diff --git a/src/core/SkGlyphCache.cpp b/src/core/SkGlyphCache.cpp
index 9bb4360..668f0e4 100644
--- a/src/core/SkGlyphCache.cpp
+++ b/src/core/SkGlyphCache.cpp
@@ -505,6 +505,23 @@
     return FindOrCreateStrikeExclusive(desc, creator);
 }
 
+SkExclusiveStrikePtr SkGlyphCache::FindOrCreateStrikeExclusive(
+    const SkPaint& paint,
+    const SkSurfaceProps* surfaceProps,
+    SkScalerContextFlags scalerContextFlags,
+    const SkMatrix* deviceMatrix)
+{
+    SkAutoDescriptor ad;
+    SkScalerContextEffects effects;
+
+    auto desc = SkScalerContext::CreateDescriptorAndEffectsUsingPaint(
+        paint, surfaceProps, scalerContextFlags, deviceMatrix, &ad, &effects);
+
+    auto tf = SkPaintPriv::GetTypefaceOrDefault(paint);
+
+    return FindOrCreateStrikeExclusive(*desc, effects, *tf);
+}
+
 void SkGlyphCache::AttachCache(SkGlyphCache* cache) {
     SkGlyphCache_Globals::AttachCache(cache);
 }
@@ -786,22 +803,3 @@
 size_t SkGraphics::GetTLSFontCacheLimit() { return 0; }
 void SkGraphics::SetTLSFontCacheLimit(size_t bytes) { }
 
-SkGlyphCache* SkGlyphCache::DetachCache(
-    SkTypeface* typeface, const SkScalerContextEffects& effects, const SkDescriptor* desc)
-{
-    auto cache = FindOrCreateStrikeExclusive(*desc, effects, *typeface);
-    return cache.release();
-}
-
-SkGlyphCache* SkGlyphCache::DetachCacheUsingPaint(const SkPaint& paint,
-                                                  const SkSurfaceProps* surfaceProps,
-                                                  SkScalerContextFlags scalerContextFlags,
-                                                  const SkMatrix* deviceMatrix) {
-    SkAutoDescriptor ad;
-    SkScalerContextEffects effects;
-
-    auto desc = SkScalerContext::CreateDescriptorAndEffectsUsingPaint(
-        paint, surfaceProps, scalerContextFlags, deviceMatrix, &ad, &effects);
-
-    return SkGlyphCache::DetachCache(SkPaintPriv::GetTypefaceOrDefault(paint), effects, desc);
-}
diff --git a/src/core/SkGlyphCache.h b/src/core/SkGlyphCache.h
index 8c335af..a161f6f 100644
--- a/src/core/SkGlyphCache.h
+++ b/src/core/SkGlyphCache.h
@@ -148,6 +148,12 @@
         const SkScalerContextEffects& effects,
         const SkTypeface& typeface);
 
+    static SkExclusiveStrikePtr FindOrCreateStrikeExclusive(
+        const SkPaint& paint,
+        const SkSurfaceProps* surfaceProps,
+        SkScalerContextFlags scalerContextFlags,
+        const SkMatrix* deviceMatrix);
+
     template <typename ScalerContextCreator>
     static SkExclusiveStrikePtr CreateStrikeExclusive(
         const SkDescriptor& desc, ScalerContextCreator creator)
@@ -167,22 +173,6 @@
         return SkExclusiveStrikePtr(new SkGlyphCache(desc, std::move(context)));
     }
 
-    /** Detach a strike from the global cache matching the specified descriptor. Once detached,
-        it can be queried/modified by the current thread, and when finished, be reattached to the
-        global cache with AttachCache(). While detached, if another request is made with the same
-        descriptor, a different strike will be generated. This is fine. It does mean we can have
-        more than 1 strike for the same descriptor, but that will eventually get purged, and the
-        win is that different thread will never block each other while a strike is being used.
-        DEPRECATED
-    */
-    static SkGlyphCache* DetachCache(
-        SkTypeface* typeface, const SkScalerContextEffects& effects, const SkDescriptor* desc);
-
-    static SkGlyphCache* DetachCacheUsingPaint(const SkPaint& paint,
-                                               const SkSurfaceProps* surfaceProps,
-                                               SkScalerContextFlags scalerContextFlags,
-                                               const SkMatrix* deviceMatrix);
-
     static void Dump();
 
     /** Dump memory usage statistics of all the attaches caches in the process using the
@@ -296,20 +286,17 @@
 
 class SkAutoGlyphCache : public SkExclusiveStrikePtr {
 public:
-    /** deprecated: use get() */
-    SkGlyphCache* getCache() const { return this->get(); }
     SkAutoGlyphCache() = default;
     SkAutoGlyphCache(SkGlyphCache* cache) : INHERITED(cache) {}
     SkAutoGlyphCache(SkTypeface* typeface, const SkScalerContextEffects& effects,
                      const SkDescriptor* desc)
-        : INHERITED(SkGlyphCache::DetachCache(typeface, effects, desc))
-    {}
+        : INHERITED(SkGlyphCache::FindOrCreateStrikeExclusive(*desc, effects, *typeface)) {}
     /** deprecated: always enables fake gamma */
     SkAutoGlyphCache(const SkPaint& paint,
                      const SkSurfaceProps* surfaceProps,
                      const SkMatrix* matrix)
         : INHERITED(
-        SkGlyphCache::DetachCacheUsingPaint(
+        SkGlyphCache::FindOrCreateStrikeExclusive(
             paint, surfaceProps,
             SkScalerContextFlags::kFakeGammaAndBoostContrast, matrix))
     {}
@@ -318,8 +305,8 @@
                      SkScalerContextFlags scalerContextFlags,
                      const SkMatrix* matrix)
         : INHERITED(
-            SkGlyphCache::DetachCacheUsingPaint(paint, surfaceProps, scalerContextFlags, matrix))
-    {}
+            SkGlyphCache::FindOrCreateStrikeExclusive(
+                paint, surfaceProps, scalerContextFlags, matrix)) {}
 private:
     using INHERITED = SkExclusiveStrikePtr;
 };
diff --git a/src/core/SkPaint.cpp b/src/core/SkPaint.cpp
index e368071..210b8e5 100644
--- a/src/core/SkPaint.cpp
+++ b/src/core/SkPaint.cpp
@@ -431,7 +431,7 @@
     }
 
     SkAutoGlyphCache autoCache(*this, nullptr, nullptr);
-    SkGlyphCache*    cache = autoCache.getCache();
+    SkGlyphCache*    cache = autoCache.get();
 
     const char* text = (const char*)textData;
     const char* stop = text + byteLength;
@@ -489,7 +489,7 @@
     }
 
     SkAutoGlyphCache autoCache(*this, nullptr, nullptr);
-    SkGlyphCache*    cache = autoCache.getCache();
+    SkGlyphCache*    cache = autoCache.get();
 
     switch (this->getTextEncoding()) {
         case SkPaint::kUTF8_TextEncoding: {
@@ -539,7 +539,7 @@
 
     SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
     SkAutoGlyphCache autoCache(*this, &props, nullptr);
-    SkGlyphCache*    cache = autoCache.getCache();
+    SkGlyphCache*    cache = autoCache.get();
 
     for (int index = 0; index < count; index++) {
         textData[index] = cache->glyphToUnichar(glyphs[index]);
@@ -805,7 +805,7 @@
     SkScalar scale = canon.getScale();
 
     SkAutoGlyphCache    autoCache(paint, nullptr, nullptr);
-    SkGlyphCache*       cache = autoCache.getCache();
+    SkGlyphCache*       cache = autoCache.get();
 
     SkScalar width = 0;
 
@@ -859,7 +859,7 @@
     }
 
     SkAutoGlyphCache    autoCache(paint, nullptr, nullptr);
-    SkGlyphCache*       cache = autoCache.getCache();
+    SkGlyphCache*       cache = autoCache.get();
 
     GlyphCacheProc   glyphCacheProc = SkPaint::GetGlyphCacheProc(paint.getTextEncoding(),
                                                                  paint.isDevKernText(),
@@ -965,7 +965,7 @@
     SkScalar scale = canon.getScale();
 
     SkAutoGlyphCache    autoCache(paint, nullptr, nullptr);
-    SkGlyphCache*       cache = autoCache.getCache();
+    SkGlyphCache*       cache = autoCache.get();
     GlyphCacheProc      glyphCacheProc = SkPaint::GetGlyphCacheProc(paint.getTextEncoding(),
                                                                     paint.isDevKernText(),
                                                                     nullptr != bounds);
@@ -1748,9 +1748,8 @@
     }
 
     // SRGBTODO: Is this correct?
-    fCache = SkGlyphCache::DetachCacheUsingPaint(fPaint, nullptr,
-                                                 SkScalerContextFlags::kFakeGammaAndBoostContrast,
-                                                 nullptr);
+    fCache = SkGlyphCache::FindOrCreateStrikeExclusive(
+        fPaint, nullptr, SkScalerContextFlags::kFakeGammaAndBoostContrast, nullptr);
 
     SkPaint::Style  style = SkPaint::kFill_Style;
     sk_sp<SkPathEffect> pe;
@@ -1768,7 +1767,7 @@
     SkScalar xOffset = 0;
     if (paint.getTextAlign() != SkPaint::kLeft_Align) { // need to measure first
         int      count;
-        SkScalar width = fPaint.measure_text(fCache, text, length, &count, nullptr) * fScale;
+        SkScalar width = fPaint.measure_text(fCache.get(), text, length, &count, nullptr) * fScale;
         if (paint.getTextAlign() == SkPaint::kCenter_Align) {
             width = SkScalarHalf(width);
         }
@@ -1783,13 +1782,9 @@
     fXYIndex = paint.isVerticalText() ? 1 : 0;
 }
 
-SkTextBaseIter::~SkTextBaseIter() {
-    SkGlyphCache::AttachCache(fCache);
-}
-
 bool SkTextToPathIter::next(const SkPath** path, SkScalar* xpos) {
     if (fText < fStop) {
-        const SkGlyph& glyph = fGlyphCacheProc(fCache, &fText);
+        const SkGlyph& glyph = fGlyphCacheProc(fCache.get(), &fText);
 
         fXPos += (fPrevAdvance + fAutoKern.adjust(glyph)) * fScale;
         fPrevAdvance = advance(glyph, fXYIndex);   // + fPaint.getTextTracking();
@@ -1812,7 +1807,7 @@
 }
 
 bool SkTextInterceptsIter::next(SkScalar* array, int* count) {
-    const SkGlyph& glyph = fGlyphCacheProc(fCache, &fText);
+    const SkGlyph& glyph = fGlyphCacheProc(fCache.get(), &fText);
     fXPos += (fPrevAdvance + fAutoKern.adjust(glyph)) * fScale;
     fPrevAdvance = advance(glyph, fXYIndex);   // + fPaint.getTextTracking();
     if (fCache->findPath(glyph)) {
diff --git a/src/core/SkTextToPathIter.h b/src/core/SkTextToPathIter.h
index 6de12a8..8bcbc21 100644
--- a/src/core/SkTextToPathIter.h
+++ b/src/core/SkTextToPathIter.h
@@ -9,22 +9,21 @@
 #define SkTextToPathIter_DEFINED
 
 #include "SkAutoKern.h"
+#include "SkGlyphCache.h"
 #include "SkPaint.h"
 
-class SkGlyphCache;
 
 class SkTextBaseIter {
 protected:
     SkTextBaseIter(const char text[], size_t length, const SkPaint& paint,
                    bool applyStrokeAndPathEffects);
-    ~SkTextBaseIter();
 
-    SkGlyphCache*   fCache;
-    SkPaint         fPaint;
-    SkScalar        fScale;
-    SkScalar        fPrevAdvance;
-    const char*     fText;
-    const char*     fStop;
+    SkExclusiveStrikePtr fCache;
+    SkPaint              fPaint;
+    SkScalar             fScale;
+    SkScalar             fPrevAdvance;
+    const char*          fText;
+    const char*          fStop;
     SkPaint::GlyphCacheProc fGlyphCacheProc;
 
     SkScalar        fXPos;      // accumulated xpos, returned in next
@@ -74,7 +73,7 @@
         if (TextType::kPosText == fTextType
                 && fPaint.getTextAlign() != SkPaint::kLeft_Align) { // need to measure first
             const char* text = fText;
-            const SkGlyph& glyph = fGlyphCacheProc(fCache, &text);
+            const SkGlyph& glyph = fGlyphCacheProc(fCache.get(), &text);
             SkScalar width = (&glyph.fAdvanceX)[0] * fScale;
             if (fPaint.getTextAlign() == SkPaint::kCenter_Align) {
                 width = SkScalarHalf(width);
diff --git a/src/gpu/text/GrAtlasTextBlob.cpp b/src/gpu/text/GrAtlasTextBlob.cpp
index b4404c3..dfe3312 100644
--- a/src/gpu/text/GrAtlasTextBlob.cpp
+++ b/src/gpu/text/GrAtlasTextBlob.cpp
@@ -55,11 +55,11 @@
     return cacheBlob;
 }
 
-SkGlyphCache* GrAtlasTextBlob::setupCache(int runIndex,
-                                          const SkSurfaceProps& props,
-                                          SkScalerContextFlags scalerContextFlags,
-                                          const SkPaint& skPaint,
-                                          const SkMatrix* viewMatrix) {
+SkExclusiveStrikePtr GrAtlasTextBlob::setupCache(int runIndex,
+                                                 const SkSurfaceProps& props,
+                                                 SkScalerContextFlags scalerContextFlags,
+                                                 const SkPaint& skPaint,
+                                                 const SkMatrix* viewMatrix) {
     GrAtlasTextBlob::Run* run = &fRuns[runIndex];
 
     // if we have an override descriptor for the run, then we should use that
@@ -71,7 +71,8 @@
     run->fTypeface = SkPaintPriv::RefTypefaceOrDefault(skPaint);
     run->fPathEffect = sk_ref_sp(effects.fPathEffect);
     run->fMaskFilter = sk_ref_sp(effects.fMaskFilter);
-    return SkGlyphCache::DetachCache(run->fTypeface.get(), effects, desc->getDesc());
+    return SkGlyphCache::FindOrCreateStrikeExclusive(
+        *desc->getDesc(), effects, *run->fTypeface.get());
 }
 
 void GrAtlasTextBlob::appendGlyph(int runIndex,
diff --git a/src/gpu/text/GrAtlasTextBlob.h b/src/gpu/text/GrAtlasTextBlob.h
index b4a11a4..e5a931d 100644
--- a/src/gpu/text/GrAtlasTextBlob.h
+++ b/src/gpu/text/GrAtlasTextBlob.h
@@ -170,11 +170,11 @@
         run.fOverrideDescriptor.reset(new SkAutoDescriptor);
     }
 
-    SkGlyphCache* setupCache(int runIndex,
-                             const SkSurfaceProps& props,
-                             SkScalerContextFlags scalerContextFlags,
-                             const SkPaint& skPaint,
-                             const SkMatrix* viewMatrix);
+    SkExclusiveStrikePtr setupCache(int runIndex,
+                                    const SkSurfaceProps& props,
+                                    SkScalerContextFlags scalerContextFlags,
+                                    const SkPaint& skPaint,
+                                    const SkMatrix* viewMatrix);
 
     // Appends a glyph to the blob.  If the glyph is too large, the glyph will be appended
     // as a path.
@@ -607,7 +607,7 @@
     GrDeferredUploadTarget* fUploadTarget;
     GrGlyphCache* fGlyphCache;
     GrAtlasManager* fFullAtlasManager;
-    SkAutoGlyphCache* fLazyCache;
+    SkExclusiveStrikePtr* fLazyCache;
     Run* fRun;
     Run::SubRunInfo* fSubRun;
     GrColor fColor;
diff --git a/src/gpu/text/GrAtlasTextBlobVertexRegenerator.cpp b/src/gpu/text/GrAtlasTextBlobVertexRegenerator.cpp
index 296df22..188f34c 100644
--- a/src/gpu/text/GrAtlasTextBlobVertexRegenerator.cpp
+++ b/src/gpu/text/GrAtlasTextBlobVertexRegenerator.cpp
@@ -244,7 +244,8 @@
             SkScalerContextEffects effects;
             effects.fPathEffect = fRun->fPathEffect.get();
             effects.fMaskFilter = fRun->fMaskFilter.get();
-            fLazyCache->reset(SkGlyphCache::DetachCache(fRun->fTypeface.get(), effects, desc));
+            *fLazyCache =
+                SkGlyphCache::FindOrCreateStrikeExclusive(*desc, effects, *fRun->fTypeface.get());
         }
 
         if (regenGlyphs) {
diff --git a/src/gpu/text/GrAtlasTextContext.cpp b/src/gpu/text/GrAtlasTextContext.cpp
index ad708d7..72f5b5b 100644
--- a/src/gpu/text/GrAtlasTextContext.cpp
+++ b/src/gpu/text/GrAtlasTextContext.cpp
@@ -386,20 +386,18 @@
         return;
     }
 
-    sk_sp<GrTextStrike> currStrike;
-    SkGlyphCache* cache = blob->setupCache(runIndex, props, scalerContextFlags, paint, &viewMatrix);
+    sk_sp<GrTextStrike> currStrike = nullptr;
+    auto cache = blob->setupCache(runIndex, props, scalerContextFlags, paint, &viewMatrix);
     SkFindAndPlaceGlyph::ProcessText(paint.skPaint().getTextEncoding(), text, byteLength, {x, y},
-                                     viewMatrix, paint.skPaint().getTextAlign(), cache,
+                                     viewMatrix, paint.skPaint().getTextAlign(), cache.get(),
                                      [&](const SkGlyph& glyph, SkPoint position, SkPoint rounding) {
                                          position += rounding;
                                          BmpAppendGlyph(blob, runIndex, glyphCache, &currStrike,
                                                         glyph, SkScalarFloorToScalar(position.fX),
                                                         SkScalarFloorToScalar(position.fY),
-                                                        paint.filteredPremulColor(), cache,
+                                                        paint.filteredPremulColor(), cache.get(),
                                                         SK_Scalar1);
                                      });
-
-    SkGlyphCache::AttachCache(cache);
 }
 
 void GrAtlasTextContext::DrawBmpPosText(GrAtlasTextBlob* blob, int runIndex,
@@ -426,20 +424,19 @@
         return;
     }
 
-    sk_sp<GrTextStrike> currStrike;
-    SkGlyphCache* cache = blob->setupCache(runIndex, props, scalerContextFlags, paint, &viewMatrix);
+    sk_sp<GrTextStrike> currStrike = nullptr;
+
+    auto cache = blob->setupCache(runIndex, props, scalerContextFlags, paint, &viewMatrix);
     SkFindAndPlaceGlyph::ProcessPosText(
             paint.skPaint().getTextEncoding(), text, byteLength, offset, viewMatrix, pos,
-            scalarsPerPosition, paint.skPaint().getTextAlign(), cache,
+            scalarsPerPosition, paint.skPaint().getTextAlign(), cache.get(),
             [&](const SkGlyph& glyph, SkPoint position, SkPoint rounding) {
                 position += rounding;
                 BmpAppendGlyph(blob, runIndex, glyphCache, &currStrike, glyph,
                                SkScalarFloorToScalar(position.fX),
                                SkScalarFloorToScalar(position.fY),
-                               paint.filteredPremulColor(), cache, SK_Scalar1);
+                               paint.filteredPremulColor(), cache.get(), SK_Scalar1);
             });
-
-    SkGlyphCache::AttachCache(cache);
 }
 
 void GrAtlasTextContext::DrawBmpTextAsPaths(GrAtlasTextBlob* blob, int runIndex,
@@ -508,7 +505,7 @@
                                                                         pathPaint.isDevKernText(),
                                                                         true);
     SkAutoGlyphCache           autoCache(pathPaint, &props, nullptr);
-    SkGlyphCache*              cache = autoCache.getCache();
+    SkGlyphCache*              cache = autoCache.get();
 
     const char*        stop = text + byteLength;
     const char*        lastText = text;
@@ -693,42 +690,49 @@
     // passed-in scaler context flags. (It's only used when we fall-back to bitmap text).
     SkScalerContext::CreateDescriptorAndEffectsUsingPaint(
         skPaint, &props, SkScalerContextFlags::kNone, nullptr, &desc, &effects);
-    auto typeface = SkPaintPriv::GetTypefaceOrDefault(skPaint);
-    SkGlyphCache* origPaintCache =
-            SkGlyphCache::DetachCache(typeface, effects, desc.getDesc());
 
     SkTArray<SkScalar> positions;
-
-    const char* textPtr = text;
     SkScalar stopX = 0;
     SkScalar stopY = 0;
-    SkScalar origin = 0;
-    switch (skPaint.getTextAlign()) {
-        case SkPaint::kRight_Align: origin = SK_Scalar1; break;
-        case SkPaint::kCenter_Align: origin = SK_ScalarHalf; break;
-        case SkPaint::kLeft_Align: origin = 0; break;
+    {
+        auto tf = SkPaintPriv::GetTypefaceOrDefault(skPaint);
+        auto origPaintCache = SkGlyphCache::FindOrCreateStrikeExclusive(
+            *desc.getDesc(), effects, *tf);
+
+
+        const char* textPtr = text;
+        SkScalar origin = 0;
+        switch (skPaint.getTextAlign()) {
+            case SkPaint::kRight_Align:
+                origin = SK_Scalar1;
+                break;
+            case SkPaint::kCenter_Align:
+                origin = SK_ScalarHalf;
+                break;
+            case SkPaint::kLeft_Align:
+                origin = 0;
+                break;
+        }
+
+        SkAutoKern autokern;
+        const char* stop = text + byteLength;
+        while (textPtr < stop) {
+            // don't need x, y here, since all subpixel variants will have the
+            // same advance
+            const SkGlyph& glyph = glyphCacheProc(origPaintCache.get(), &textPtr);
+
+            SkScalar width = SkFloatToScalar(glyph.fAdvanceX) + autokern.adjust(glyph);
+            positions.push_back(stopX + origin * width);
+
+            SkScalar height = SkFloatToScalar(glyph.fAdvanceY);
+            positions.push_back(stopY + origin * height);
+
+            stopX += width;
+            stopY += height;
+        }
+        SkASSERT(textPtr == stop);
     }
 
-    SkAutoKern autokern;
-    const char* stop = text + byteLength;
-    while (textPtr < stop) {
-        // don't need x, y here, since all subpixel variants will have the
-        // same advance
-        const SkGlyph& glyph = glyphCacheProc(origPaintCache, &textPtr);
-
-        SkScalar width = SkFloatToScalar(glyph.fAdvanceX) + autokern.adjust(glyph);
-        positions.push_back(stopX + origin * width);
-
-        SkScalar height = SkFloatToScalar(glyph.fAdvanceY);
-        positions.push_back(stopY + origin * height);
-
-        stopX += width;
-        stopY += height;
-    }
-    SkASSERT(textPtr == stop);
-
-    SkGlyphCache::AttachCache(origPaintCache);
-
     // now adjust starting point depending on alignment
     SkScalar alignX = stopX;
     SkScalar alignY = stopY;
@@ -778,40 +782,40 @@
 
     // We apply the fake-gamma by altering the distance in the shader, so we ignore the
     // passed-in scaler context flags. (It's only used when we fall-back to bitmap text).
-    SkGlyphCache* cache =
+    {
+        auto cache =
             blob->setupCache(runIndex, props, SkScalerContextFlags::kNone, dfPaint, nullptr);
-    SkPaint::GlyphCacheProc glyphCacheProc =
+        SkPaint::GlyphCacheProc glyphCacheProc =
             SkPaint::GetGlyphCacheProc(dfPaint.getTextEncoding(), dfPaint.isDevKernText(), true);
 
-    const char* stop = text + byteLength;
+        const char* stop = text + byteLength;
 
-    SkPaint::Align align = dfPaint.getTextAlign();
-    SkScalar alignMul = SkPaint::kCenter_Align == align ? SK_ScalarHalf :
-                        (SkPaint::kRight_Align == align ? SK_Scalar1 : 0);
-    while (text < stop) {
-        const char* lastText = text;
-        // the last 2 parameters are ignored
-        const SkGlyph& glyph = glyphCacheProc(cache, &text);
+        SkPaint::Align align = dfPaint.getTextAlign();
+        SkScalar alignMul = SkPaint::kCenter_Align == align ? SK_ScalarHalf :
+                            (SkPaint::kRight_Align == align ? SK_Scalar1 : 0);
+        while (text < stop) {
+            const char* lastText = text;
+            // the last 2 parameters are ignored
+            const SkGlyph& glyph = glyphCacheProc(cache.get(), &text);
 
-        if (glyph.fWidth) {
-            SkPoint glyphPos(offset);
-            glyphPos.fX += pos[0] - SkFloatToScalar(glyph.fAdvanceX) * alignMul * textRatio;
-            glyphPos.fY += (2 == scalarsPerPosition ? pos[1] : 0) -
-                           SkFloatToScalar(glyph.fAdvanceY) * alignMul * textRatio;
+            if (glyph.fWidth) {
+                SkPoint glyphPos(offset);
+                glyphPos.fX += pos[0] - SkFloatToScalar(glyph.fAdvanceX) * alignMul * textRatio;
+                glyphPos.fY += (2 == scalarsPerPosition ? pos[1] : 0) -
+                               SkFloatToScalar(glyph.fAdvanceY) * alignMul * textRatio;
 
-            if (glyph.fMaskFormat != SkMask::kARGB32_Format) {
-                DfAppendGlyph(blob, runIndex, glyphCache, &currStrike, glyph, glyphPos.fX,
-                              glyphPos.fY, paint.filteredPremulColor(), cache, textRatio);
-            } else {
-                // can't append color glyph to SDF batch, send to fallback
-                fallbackTextHelper.appendText(glyph, SkToInt(text - lastText), lastText, glyphPos);
+                if (glyph.fMaskFormat != SkMask::kARGB32_Format) {
+                    DfAppendGlyph(blob, runIndex, glyphCache, &currStrike, glyph, glyphPos.fX,
+                                  glyphPos.fY, paint.filteredPremulColor(), cache.get(), textRatio);
+                } else {
+                    // can't append color glyph to SDF batch, send to fallback
+                    fallbackTextHelper.appendText(glyph, SkToInt(text - lastText), lastText,
+                                                  glyphPos);
+                }
+                pos += scalarsPerPosition;
             }
         }
-        pos += scalarsPerPosition;
     }
-
-    SkGlyphCache::AttachCache(cache);
-
     fallbackTextHelper.drawText(blob, runIndex, glyphCache, props, paint, scalerContextFlags);
 }
 
@@ -883,7 +887,7 @@
     if (fFallbackTxt.count()) {
         blob->initOverride(runIndex);
         blob->setHasBitmap();
-        SkGlyphCache* cache = nullptr;
+        SkExclusiveStrikePtr cache;
         const SkPaint& skPaint = paint.skPaint();
         SkPaint::GlyphCacheProc glyphCacheProc =
             SkPaint::GetGlyphCacheProc(skPaint.getTextEncoding(),
@@ -913,14 +917,12 @@
         const char* stop = text + fFallbackTxt.count();
         SkPoint* glyphPos = fFallbackPos.begin();
         while (text < stop) {
-            const SkGlyph& glyph = glyphCacheProc(cache, &text);
+            const SkGlyph& glyph = glyphCacheProc(cache.get(), &text);
             GrAtlasTextContext::BmpAppendGlyph(blob, runIndex, glyphCache, &currStrike, glyph,
                                                glyphPos->fX, glyphPos->fY, textColor,
-                                               cache, textRatio);
+                                               cache.get(), textRatio);
             glyphPos++;
         }
-
-        SkGlyphCache::AttachCache(cache);
     }
 }
 
diff --git a/src/gpu/text/GrTextUtils.cpp b/src/gpu/text/GrTextUtils.cpp
index 680e262..7d693d1 100644
--- a/src/gpu/text/GrTextUtils.cpp
+++ b/src/gpu/text/GrTextUtils.cpp
@@ -81,7 +81,7 @@
     SkASSERT(path);
     SkASSERT(xpos);
     if (fText < fStop) {
-        const SkGlyph& glyph = fGlyphCacheProc(fCache, &fText);
+        const SkGlyph& glyph = fGlyphCacheProc(fCache.get(), &fText);
 
         fXPos += (fPrevAdvance + fAutoKern.adjust(glyph)) * fScale;
         SkASSERT(0 == fXYIndex || 1 == fXYIndex);
diff --git a/src/xps/SkXPSDevice.cpp b/src/xps/SkXPSDevice.cpp
index 9f1a960..b8387b9 100644
--- a/src/xps/SkXPSDevice.cpp
+++ b/src/xps/SkXPSDevice.cpp
@@ -1877,7 +1877,7 @@
     newTypefaceUse.xpsFont = xpsFontResource.release();
 
     SkAutoGlyphCache agc(paint, &this->surfaceProps(), &SkMatrix::I());
-    SkGlyphCache* glyphCache = agc.getCache();
+    SkGlyphCache* glyphCache = agc.get();
     unsigned int glyphCount = glyphCache->getGlyphCount();
     newTypefaceUse.glyphsUsed = new SkBitSet(glyphCount);
 
@@ -2064,7 +2064,7 @@
     const SkMatrix& matrix = SkMatrix::I();
 
     SkAutoGlyphCache    autoCache(paint, &this->surfaceProps(), &matrix);
-    SkGlyphCache*       cache = autoCache.getCache();
+    SkGlyphCache*       cache = autoCache.get();
 
     // Advance width and offsets for glyphs measured in hundredths of the font em size
     // (XPS Spec 5.1.3).
@@ -2122,7 +2122,7 @@
     const SkMatrix& matrix = SkMatrix::I();
 
     SkAutoGlyphCache    autoCache(paint, &this->surfaceProps(), &matrix);
-    SkGlyphCache*       cache = autoCache.getCache();
+    SkGlyphCache*       cache = autoCache.get();
 
     // Advance width and offsets for glyphs measured in hundredths of the font em size
     // (XPS Spec 5.1.3).