Revert "Remove attach and detach glyph cache."

This reverts commit 61d56b92a4076f8afd644d015913641d691e39c8.

Reason for revert: broke gldft text

Original change's description:
> 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>

TBR=bungeman@google.com,herb@google.com

Change-Id: Id5723a3abcd103ed20197bba2dab06e8074be633
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Bug: skia:7515
Reviewed-on: https://skia-review.googlesource.com/112745
Reviewed-by: Herb Derby <herb@google.com>
Commit-Queue: Herb Derby <herb@google.com>
diff --git a/src/gpu/text/GrAtlasTextBlob.cpp b/src/gpu/text/GrAtlasTextBlob.cpp
index dfe3312..b4404c3 100644
--- a/src/gpu/text/GrAtlasTextBlob.cpp
+++ b/src/gpu/text/GrAtlasTextBlob.cpp
@@ -55,11 +55,11 @@
     return cacheBlob;
 }
 
-SkExclusiveStrikePtr GrAtlasTextBlob::setupCache(int runIndex,
-                                                 const SkSurfaceProps& props,
-                                                 SkScalerContextFlags scalerContextFlags,
-                                                 const SkPaint& skPaint,
-                                                 const SkMatrix* viewMatrix) {
+SkGlyphCache* 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,8 +71,7 @@
     run->fTypeface = SkPaintPriv::RefTypefaceOrDefault(skPaint);
     run->fPathEffect = sk_ref_sp(effects.fPathEffect);
     run->fMaskFilter = sk_ref_sp(effects.fMaskFilter);
-    return SkGlyphCache::FindOrCreateStrikeExclusive(
-        *desc->getDesc(), effects, *run->fTypeface.get());
+    return SkGlyphCache::DetachCache(run->fTypeface.get(), effects, desc->getDesc());
 }
 
 void GrAtlasTextBlob::appendGlyph(int runIndex,
diff --git a/src/gpu/text/GrAtlasTextBlob.h b/src/gpu/text/GrAtlasTextBlob.h
index e5a931d..b4a11a4 100644
--- a/src/gpu/text/GrAtlasTextBlob.h
+++ b/src/gpu/text/GrAtlasTextBlob.h
@@ -170,11 +170,11 @@
         run.fOverrideDescriptor.reset(new SkAutoDescriptor);
     }
 
-    SkExclusiveStrikePtr setupCache(int runIndex,
-                                    const SkSurfaceProps& props,
-                                    SkScalerContextFlags scalerContextFlags,
-                                    const SkPaint& skPaint,
-                                    const SkMatrix* viewMatrix);
+    SkGlyphCache* 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;
-    SkExclusiveStrikePtr* fLazyCache;
+    SkAutoGlyphCache* fLazyCache;
     Run* fRun;
     Run::SubRunInfo* fSubRun;
     GrColor fColor;
diff --git a/src/gpu/text/GrAtlasTextBlobVertexRegenerator.cpp b/src/gpu/text/GrAtlasTextBlobVertexRegenerator.cpp
index 188f34c..296df22 100644
--- a/src/gpu/text/GrAtlasTextBlobVertexRegenerator.cpp
+++ b/src/gpu/text/GrAtlasTextBlobVertexRegenerator.cpp
@@ -244,8 +244,7 @@
             SkScalerContextEffects effects;
             effects.fPathEffect = fRun->fPathEffect.get();
             effects.fMaskFilter = fRun->fMaskFilter.get();
-            *fLazyCache =
-                SkGlyphCache::FindOrCreateStrikeExclusive(*desc, effects, *fRun->fTypeface.get());
+            fLazyCache->reset(SkGlyphCache::DetachCache(fRun->fTypeface.get(), effects, desc));
         }
 
         if (regenGlyphs) {
diff --git a/src/gpu/text/GrAtlasTextContext.cpp b/src/gpu/text/GrAtlasTextContext.cpp
index 72f5b5b..ad708d7 100644
--- a/src/gpu/text/GrAtlasTextContext.cpp
+++ b/src/gpu/text/GrAtlasTextContext.cpp
@@ -386,18 +386,20 @@
         return;
     }
 
-    sk_sp<GrTextStrike> currStrike = nullptr;
-    auto cache = blob->setupCache(runIndex, props, scalerContextFlags, paint, &viewMatrix);
+    sk_sp<GrTextStrike> currStrike;
+    SkGlyphCache* cache = blob->setupCache(runIndex, props, scalerContextFlags, paint, &viewMatrix);
     SkFindAndPlaceGlyph::ProcessText(paint.skPaint().getTextEncoding(), text, byteLength, {x, y},
-                                     viewMatrix, paint.skPaint().getTextAlign(), cache.get(),
+                                     viewMatrix, paint.skPaint().getTextAlign(), cache,
                                      [&](const SkGlyph& glyph, SkPoint position, SkPoint rounding) {
                                          position += rounding;
                                          BmpAppendGlyph(blob, runIndex, glyphCache, &currStrike,
                                                         glyph, SkScalarFloorToScalar(position.fX),
                                                         SkScalarFloorToScalar(position.fY),
-                                                        paint.filteredPremulColor(), cache.get(),
+                                                        paint.filteredPremulColor(), cache,
                                                         SK_Scalar1);
                                      });
+
+    SkGlyphCache::AttachCache(cache);
 }
 
 void GrAtlasTextContext::DrawBmpPosText(GrAtlasTextBlob* blob, int runIndex,
@@ -424,19 +426,20 @@
         return;
     }
 
-    sk_sp<GrTextStrike> currStrike = nullptr;
-
-    auto cache = blob->setupCache(runIndex, props, scalerContextFlags, paint, &viewMatrix);
+    sk_sp<GrTextStrike> currStrike;
+    SkGlyphCache* cache = blob->setupCache(runIndex, props, scalerContextFlags, paint, &viewMatrix);
     SkFindAndPlaceGlyph::ProcessPosText(
             paint.skPaint().getTextEncoding(), text, byteLength, offset, viewMatrix, pos,
-            scalarsPerPosition, paint.skPaint().getTextAlign(), cache.get(),
+            scalarsPerPosition, paint.skPaint().getTextAlign(), cache,
             [&](const SkGlyph& glyph, SkPoint position, SkPoint rounding) {
                 position += rounding;
                 BmpAppendGlyph(blob, runIndex, glyphCache, &currStrike, glyph,
                                SkScalarFloorToScalar(position.fX),
                                SkScalarFloorToScalar(position.fY),
-                               paint.filteredPremulColor(), cache.get(), SK_Scalar1);
+                               paint.filteredPremulColor(), cache, SK_Scalar1);
             });
+
+    SkGlyphCache::AttachCache(cache);
 }
 
 void GrAtlasTextContext::DrawBmpTextAsPaths(GrAtlasTextBlob* blob, int runIndex,
@@ -505,7 +508,7 @@
                                                                         pathPaint.isDevKernText(),
                                                                         true);
     SkAutoGlyphCache           autoCache(pathPaint, &props, nullptr);
-    SkGlyphCache*              cache = autoCache.get();
+    SkGlyphCache*              cache = autoCache.getCache();
 
     const char*        stop = text + byteLength;
     const char*        lastText = text;
@@ -690,49 +693,42 @@
     // 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;
-    {
-        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);
+    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, &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;
@@ -782,40 +778,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).
-    {
-        auto cache =
+    SkGlyphCache* 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.get(), &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, &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.get(), textRatio);
-                } else {
-                    // can't append color glyph to SDF batch, send to fallback
-                    fallbackTextHelper.appendText(glyph, SkToInt(text - lastText), lastText,
-                                                  glyphPos);
-                }
-                pos += scalarsPerPosition;
+            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);
             }
         }
+        pos += scalarsPerPosition;
     }
+
+    SkGlyphCache::AttachCache(cache);
+
     fallbackTextHelper.drawText(blob, runIndex, glyphCache, props, paint, scalerContextFlags);
 }
 
@@ -887,7 +883,7 @@
     if (fFallbackTxt.count()) {
         blob->initOverride(runIndex);
         blob->setHasBitmap();
-        SkExclusiveStrikePtr cache;
+        SkGlyphCache* cache = nullptr;
         const SkPaint& skPaint = paint.skPaint();
         SkPaint::GlyphCacheProc glyphCacheProc =
             SkPaint::GetGlyphCacheProc(skPaint.getTextEncoding(),
@@ -917,12 +913,14 @@
         const char* stop = text + fFallbackTxt.count();
         SkPoint* glyphPos = fFallbackPos.begin();
         while (text < stop) {
-            const SkGlyph& glyph = glyphCacheProc(cache.get(), &text);
+            const SkGlyph& glyph = glyphCacheProc(cache, &text);
             GrAtlasTextContext::BmpAppendGlyph(blob, runIndex, glyphCache, &currStrike, glyph,
                                                glyphPos->fX, glyphPos->fY, textColor,
-                                               cache.get(), textRatio);
+                                               cache, textRatio);
             glyphPos++;
         }
+
+        SkGlyphCache::AttachCache(cache);
     }
 }
 
diff --git a/src/gpu/text/GrTextUtils.cpp b/src/gpu/text/GrTextUtils.cpp
index 7d693d1..680e262 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.get(), &fText);
+        const SkGlyph& glyph = fGlyphCacheProc(fCache, &fText);
 
         fXPos += (fPrevAdvance + fAutoKern.adjust(glyph)) * fScale;
         SkASSERT(0 == fXYIndex || 1 == fXYIndex);