Remove one use of AttachCache, and a paint variant of FindOrCreate...

This is a point change to see if it causes bad draws using config
gldft. Checked with skdiff on that config.

Change-Id: Ibee4630df21456ba9dccf28480bded82d1d9bf47
Reviewed-on: https://skia-review.googlesource.com/113204
Reviewed-by: Ben Wagner <bungeman@google.com>
Commit-Queue: Herb Derby <herb@google.com>
diff --git a/src/core/SkGlyphCache.cpp b/src/core/SkGlyphCache.cpp
index 9bb4360..f16de64 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);
 }
@@ -793,15 +810,3 @@
     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..aad1fa4 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)
@@ -178,11 +184,6 @@
     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
@@ -302,14 +303,13 @@
     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 +318,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 1fd3bf5..f059fc3 100644
--- a/src/core/SkPaint.cpp
+++ b/src/core/SkPaint.cpp
@@ -1748,9 +1748,9 @@
     }
 
     // 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 +1768,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 +1783,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 +1808,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..2198334 100644
--- a/src/core/SkTextToPathIter.h
+++ b/src/core/SkTextToPathIter.h
@@ -9,22 +9,20 @@
 #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 +72,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/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);