sk_sp-ify GrGlyphCache

Hopefully, this makes it clearer that the subRuns of the GrAtlasTextBlobs carry a ref on the GrAtlasTextStrikes

Change-Id: I3d612074d98bc26240465f717711b7a2bcecb6ed
Reviewed-on: https://skia-review.googlesource.com/110981
Commit-Queue: Robert Phillips <robertphillips@google.com>
Reviewed-by: Jim Van Verth <jvanverth@google.com>
diff --git a/src/gpu/text/GrAtlasManager.cpp b/src/gpu/text/GrAtlasManager.cpp
index 6e227a9..c6a6056 100644
--- a/src/gpu/text/GrAtlasManager.cpp
+++ b/src/gpu/text/GrAtlasManager.cpp
@@ -96,7 +96,7 @@
 // add to texture atlas that matches this format
 bool GrAtlasManager::addToAtlas(GrResourceProvider* resourceProvider,
                                 GrGlyphCache* glyphCache,
-                                GrAtlasTextStrike* strike, GrDrawOpAtlas::AtlasID* id,
+                                GrTextStrike* strike, GrDrawOpAtlas::AtlasID* id,
                                 GrDeferredUploadTarget* target, GrMaskFormat format,
                                 int width, int height, const void* image, SkIPoint16* loc) {
     glyphCache->setStrikeToPreserve(strike);
diff --git a/src/gpu/text/GrAtlasManager.h b/src/gpu/text/GrAtlasManager.h
index 4629cb1..eb9c113 100644
--- a/src/gpu/text/GrAtlasManager.h
+++ b/src/gpu/text/GrAtlasManager.h
@@ -12,7 +12,7 @@
 #include "GrOnFlushResourceProvider.h"
 
 class GrAtlasGlypCache;
-class GrAtlasTextStrike;
+class GrTextStrike;
 struct GrGlyph;
 
  /** The GrAtlasManager classes manage the lifetime of and access to GrDrawOpAtlases.
@@ -100,7 +100,7 @@
     }
 
     // add to texture atlas that matches this format
-    bool addToAtlas(GrResourceProvider*, GrGlyphCache*, GrAtlasTextStrike*,
+    bool addToAtlas(GrResourceProvider*, GrGlyphCache*, GrTextStrike*,
                     GrDrawOpAtlas::AtlasID*, GrDeferredUploadTarget*, GrMaskFormat,
                     int width, int height, const void* image, SkIPoint16* loc);
 
diff --git a/src/gpu/text/GrAtlasTextBlob.cpp b/src/gpu/text/GrAtlasTextBlob.cpp
index 35e7830..8470370 100644
--- a/src/gpu/text/GrAtlasTextBlob.cpp
+++ b/src/gpu/text/GrAtlasTextBlob.cpp
@@ -76,7 +76,7 @@
 void GrAtlasTextBlob::appendGlyph(int runIndex,
                                   const SkRect& positions,
                                   GrColor color,
-                                  GrAtlasTextStrike* strike,
+                                  sk_sp<GrTextStrike> strike,
                                   GrGlyph* glyph,
                                   SkGlyphCache* cache, const SkGlyph& skGlyph,
                                   SkScalar x, SkScalar y, SkScalar scale, bool preTransformed) {
@@ -104,9 +104,9 @@
     Run::SubRunInfo* subRun = &run.fSubRunInfo.back();
     if (run.fInitialized && subRun->maskFormat() != format) {
         subRun = &run.push_back();
-        subRun->setStrike(strike);
+        subRun->setStrike(std::move(strike));
     } else if (!run.fInitialized) {
-        subRun->setStrike(strike);
+        subRun->setStrike(std::move(strike));
     }
 
     run.fInitialized = true;
@@ -461,8 +461,8 @@
 
             if (lSubRun.strike()) {
                 SkASSERT_RELEASE(rSubRun.strike());
-                SkASSERT_RELEASE(GrAtlasTextStrike::GetKey(*lSubRun.strike()) ==
-                                 GrAtlasTextStrike::GetKey(*rSubRun.strike()));
+                SkASSERT_RELEASE(GrTextStrike::GetKey(*lSubRun.strike()) ==
+                                 GrTextStrike::GetKey(*rSubRun.strike()));
 
             } else {
                 SkASSERT_RELEASE(!rSubRun.strike());
diff --git a/src/gpu/text/GrAtlasTextBlob.h b/src/gpu/text/GrAtlasTextBlob.h
index 9f91cae..52771c7 100644
--- a/src/gpu/text/GrAtlasTextBlob.h
+++ b/src/gpu/text/GrAtlasTextBlob.h
@@ -181,7 +181,7 @@
     void appendGlyph(int runIndex,
                      const SkRect& positions,
                      GrColor color,
-                     GrAtlasTextStrike* strike,
+                     sk_sp<GrTextStrike> strike,
                      GrGlyph* glyph,
                      SkGlyphCache*, const SkGlyph& skGlyph,
                      SkScalar x, SkScalar y, SkScalar scale, bool preTransformed);
@@ -371,8 +371,9 @@
             // TODO when this object is more internal, drop the privacy
             void resetBulkUseToken() { fBulkUseToken.reset(); }
             GrDrawOpAtlas::BulkUseTokenUpdater* bulkUseToken() { return &fBulkUseToken; }
-            void setStrike(GrAtlasTextStrike* strike) { fStrike.reset(SkRef(strike)); }
-            GrAtlasTextStrike* strike() const { return fStrike.get(); }
+            void setStrike(sk_sp<GrTextStrike> strike) { fStrike = std::move(strike); }
+            GrTextStrike* strike() const { return fStrike.get(); }
+            sk_sp<GrTextStrike> refStrike() const { return fStrike; }
 
             void setAtlasGeneration(uint64_t atlasGeneration) { fAtlasGeneration = atlasGeneration;}
             uint64_t atlasGeneration() const { return fAtlasGeneration; }
@@ -444,7 +445,7 @@
             };
 
             GrDrawOpAtlas::BulkUseTokenUpdater fBulkUseToken;
-            sk_sp<GrAtlasTextStrike> fStrike;
+            sk_sp<GrTextStrike> fStrike;
             SkMatrix fCurrentViewMatrix;
             SkRect fVertexBounds;
             uint64_t fAtlasGeneration;
diff --git a/src/gpu/text/GrAtlasTextBlobVertexRegenerator.cpp b/src/gpu/text/GrAtlasTextBlobVertexRegenerator.cpp
index 54001c2..f574d6f 100644
--- a/src/gpu/text/GrAtlasTextBlobVertexRegenerator.cpp
+++ b/src/gpu/text/GrAtlasTextBlobVertexRegenerator.cpp
@@ -232,7 +232,7 @@
 template <bool regenPos, bool regenCol, bool regenTexCoords, bool regenGlyphs>
 Regenerator::Result Regenerator::doRegen() {
     static_assert(!regenGlyphs || regenTexCoords, "must regenTexCoords along regenGlyphs");
-    GrAtlasTextStrike* strike = nullptr;
+    sk_sp<GrTextStrike> strike;
     if (regenTexCoords) {
         fSubRun->resetBulkUseToken();
 
@@ -250,7 +250,7 @@
         if (regenGlyphs) {
             strike = fGlyphCache->getStrike(fLazyCache->get());
         } else {
-            strike = fSubRun->strike();
+            strike = fSubRun->refStrike();
         }
     }
 
@@ -302,7 +302,7 @@
     fSubRun->setColor(fColor);
     if (regenTexCoords) {
         if (regenGlyphs) {
-            fSubRun->setStrike(strike);
+            fSubRun->setStrike(std::move(strike));
         }
         fSubRun->setAtlasGeneration(fBrokenRun
                                     ? GrDrawOpAtlas::kInvalidAtlasGeneration
diff --git a/src/gpu/text/GrAtlasTextContext.cpp b/src/gpu/text/GrAtlasTextContext.cpp
index aadc2dc..ce1b52e 100644
--- a/src/gpu/text/GrAtlasTextContext.cpp
+++ b/src/gpu/text/GrAtlasTextContext.cpp
@@ -384,7 +384,8 @@
                            text, byteLength, x, y);
         return;
     }
-    GrAtlasTextStrike* currStrike = nullptr;
+
+    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,
@@ -424,8 +425,7 @@
         return;
     }
 
-    GrAtlasTextStrike* currStrike = nullptr;
-
+    sk_sp<GrTextStrike> currStrike;
     SkGlyphCache* cache = blob->setupCache(runIndex, props, scalerContextFlags, paint, &viewMatrix);
     SkFindAndPlaceGlyph::ProcessPosText(
             paint.skPaint().getTextEncoding(), text, byteLength, offset, viewMatrix, pos,
@@ -541,7 +541,8 @@
 }
 
 void GrAtlasTextContext::BmpAppendGlyph(GrAtlasTextBlob* blob, int runIndex,
-                                        GrGlyphCache* grGlyphCache, GrAtlasTextStrike** strike,
+                                        GrGlyphCache* grGlyphCache,
+                                        sk_sp<GrTextStrike>* strike,
                                         const SkGlyph& skGlyph, SkScalar sx, SkScalar sy,
                                         GrColor color, SkGlyphCache* skGlyphCache,
                                         SkScalar textRatio) {
@@ -777,7 +778,7 @@
                                           glyphCache->getGlyphSizeLimit(),
                                           textRatio);
 
-    GrAtlasTextStrike* currStrike = nullptr;
+    sk_sp<GrTextStrike> currStrike;
 
     // 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).
@@ -820,7 +821,7 @@
 
 // TODO: merge with BmpAppendGlyph
 void GrAtlasTextContext::DfAppendGlyph(GrAtlasTextBlob* blob, int runIndex,
-                                       GrGlyphCache* grGlyphCache, GrAtlasTextStrike** strike,
+                                       GrGlyphCache* grGlyphCache, sk_sp<GrTextStrike>* strike,
                                        const SkGlyph& skGlyph, SkScalar sx, SkScalar sy,
                                        GrColor color, SkGlyphCache* skGlyphCache,
                                        SkScalar textRatio) {
@@ -905,7 +906,7 @@
                                      &fViewMatrix);
         }
 
-        GrAtlasTextStrike* currStrike = nullptr;
+        sk_sp<GrTextStrike> currStrike;
         const char* text = fFallbackTxt.begin();
         const char* stop = text + fFallbackTxt.count();
         SkPoint* glyphPos = fFallbackPos.begin();
diff --git a/src/gpu/text/GrAtlasTextContext.h b/src/gpu/text/GrAtlasTextContext.h
index 37b7de5..bcdc9fd 100644
--- a/src/gpu/text/GrAtlasTextContext.h
+++ b/src/gpu/text/GrAtlasTextContext.h
@@ -174,11 +174,11 @@
                                 const SkMatrix& viewMatrix) const;
 
     static void BmpAppendGlyph(GrAtlasTextBlob*, int runIndex, GrGlyphCache*,
-                               GrAtlasTextStrike**, const SkGlyph&, SkScalar sx, SkScalar sy,
+                               sk_sp<GrTextStrike>*, const SkGlyph&, SkScalar sx, SkScalar sy,
                                GrColor color, SkGlyphCache*, SkScalar textRatio);
 
     static void DfAppendGlyph(GrAtlasTextBlob*, int runIndex, GrGlyphCache*,
-                              GrAtlasTextStrike**, const SkGlyph&, SkScalar sx, SkScalar sy,
+                              sk_sp<GrTextStrike>*, const SkGlyph&, SkScalar sx, SkScalar sy,
                               GrColor color, SkGlyphCache* cache, SkScalar textRatio);
 
     const GrDistanceFieldAdjustTable* dfAdjustTable() const { return fDistanceAdjustTable.get(); }
diff --git a/src/gpu/text/GrGlyphCache.cpp b/src/gpu/text/GrGlyphCache.cpp
index d444089..7d89907 100644
--- a/src/gpu/text/GrGlyphCache.cpp
+++ b/src/gpu/text/GrGlyphCache.cpp
@@ -41,13 +41,13 @@
 
     StrikeHash::Iter iter(&glyphCache->fCache);
     for (; !iter.done(); ++iter) {
-        GrAtlasTextStrike* strike = &*iter;
+        GrTextStrike* strike = &*iter;
         strike->removeID(id);
 
         // clear out any empty strikes.  We will preserve the strike whose call to addToAtlas
         // triggered the eviction
         if (strike != glyphCache->fPreserveStrike && 0 == strike->fAtlasedGlyphs) {
-            glyphCache->fCache.remove(GrAtlasTextStrike::GetKey(*strike));
+            glyphCache->fCache.remove(GrTextStrike::GetKey(*strike));
             strike->fIsAbandoned = true;
             strike->unref();
         }
@@ -246,13 +246,13 @@
     atlas and a position within that texture.
  */
 
-GrAtlasTextStrike::GrAtlasTextStrike(const SkDescriptor& key)
+GrTextStrike::GrTextStrike(const SkDescriptor& key)
     : fFontScalerKey(key)
     , fPool(9/*start allocations at 512 bytes*/)
     , fAtlasedGlyphs(0)
     , fIsAbandoned(false) {}
 
-GrAtlasTextStrike::~GrAtlasTextStrike() {
+GrTextStrike::~GrTextStrike() {
     SkTDynamicHash<GrGlyph, GrGlyph::PackedID>::Iter iter(&fCache);
     while (!iter.done()) {
         (*iter).reset();
@@ -260,8 +260,8 @@
     }
 }
 
-GrGlyph* GrAtlasTextStrike::generateGlyph(const SkGlyph& skGlyph, GrGlyph::PackedID packed,
-                                          SkGlyphCache* cache) {
+GrGlyph* GrTextStrike::generateGlyph(const SkGlyph& skGlyph, GrGlyph::PackedID packed,
+                                     SkGlyphCache* cache) {
     SkIRect bounds;
     if (GrGlyph::kDistance_MaskStyle == GrGlyph::UnpackMaskStyle(packed)) {
         if (!get_packed_glyph_df_bounds(cache, skGlyph, &bounds)) {
@@ -280,7 +280,7 @@
     return glyph;
 }
 
-void GrAtlasTextStrike::removeID(GrDrawOpAtlas::AtlasID id) {
+void GrTextStrike::removeID(GrDrawOpAtlas::AtlasID id) {
     SkTDynamicHash<GrGlyph, GrGlyph::PackedID>::Iter iter(&fCache);
     while (!iter.done()) {
         if (id == (*iter).fID) {
@@ -292,13 +292,13 @@
     }
 }
 
-bool GrAtlasTextStrike::addGlyphToAtlas(GrResourceProvider* resourceProvider,
-                                        GrDeferredUploadTarget* target,
-                                        GrGlyphCache* glyphCache,
-                                        GrAtlasManager* fullAtlasManager,
-                                        GrGlyph* glyph,
-                                        SkGlyphCache* cache,
-                                        GrMaskFormat expectedMaskFormat) {
+bool GrTextStrike::addGlyphToAtlas(GrResourceProvider* resourceProvider,
+                                   GrDeferredUploadTarget* target,
+                                   GrGlyphCache* glyphCache,
+                                   GrAtlasManager* fullAtlasManager,
+                                   GrGlyph* glyph,
+                                   SkGlyphCache* cache,
+                                   GrMaskFormat expectedMaskFormat) {
     SkASSERT(glyph);
     SkASSERT(cache);
     SkASSERT(fCache.find(glyph->fPackedID));
diff --git a/src/gpu/text/GrGlyphCache.h b/src/gpu/text/GrGlyphCache.h
index 169e4b3..8279814 100644
--- a/src/gpu/text/GrGlyphCache.h
+++ b/src/gpu/text/GrGlyphCache.h
@@ -19,21 +19,21 @@
 class GrGpu;
 
 /**
- *  The GrAtlasTextStrike manages a pool of CPU backing memory for GrGlyphs. This backing memory
+ *  The GrTextStrike manages a pool of CPU backing memory for GrGlyphs. This backing memory
  *  is indexed by a PackedID and SkGlyphCache. The SkGlyphCache is what actually creates the mask.
- *  The GrAtlasTextStrike may outlive the generating SkGlyphCache. However, it retains a copy
- *  of it's SkDescriptor as a key to access (or regenerate) the SkGlyphCache. GrAtlasTextStrike are
+ *  The GrTextStrike may outlive the generating SkGlyphCache. However, it retains a copy
+ *  of it's SkDescriptor as a key to access (or regenerate) the SkGlyphCache. GrTextStrikes are
  *  created by and owned by a GrGlyphCache.
  */
-class GrAtlasTextStrike : public SkNVRefCnt<GrAtlasTextStrike> {
+class GrTextStrike : public SkNVRefCnt<GrTextStrike> {
 public:
-    GrAtlasTextStrike(const SkDescriptor& fontScalerKey);
-    ~GrAtlasTextStrike();
+    GrTextStrike(const SkDescriptor& fontScalerKey);
+    ~GrTextStrike();
 
     inline GrGlyph* getGlyph(const SkGlyph& skGlyph, GrGlyph::PackedID packed,
                              SkGlyphCache* cache) {
         GrGlyph* glyph = fCache.find(packed);
-        if (nullptr == glyph) {
+        if (!glyph) {
             glyph = this->generateGlyph(skGlyph, packed, cache);
         }
         return glyph;
@@ -47,7 +47,7 @@
                              GrMaskFormat expectedMaskFormat,
                              SkGlyphCache* cache) {
         GrGlyph* glyph = fCache.find(packed);
-        if (nullptr == glyph) {
+        if (!glyph) {
             // We could return this to the caller, but in practice it adds code complexity for
             // potentially little benefit(ie, if the glyph is not in our font cache, then its not
             // in the atlas and we're going to be doing a texture upload anyways).
@@ -76,8 +76,8 @@
     // If a TextStrike is abandoned by the cache, then the caller must get a new strike
     bool isAbandoned() const { return fIsAbandoned; }
 
-    static const SkDescriptor& GetKey(const GrAtlasTextStrike& ts) {
-        return *ts.fFontScalerKey.getDesc();
+    static const SkDescriptor& GetKey(const GrTextStrike& strike) {
+        return *strike.fFontScalerKey.getDesc();
     }
 
     static uint32_t Hash(const SkDescriptor& desc) { return desc.getChecksum(); }
@@ -113,15 +113,15 @@
     void setGlyphSizeLimit(SkScalar sizeLimit) { fGlyphSizeLimit = sizeLimit; }
     SkScalar getGlyphSizeLimit() const { return fGlyphSizeLimit; }
 
-    void setStrikeToPreserve(GrAtlasTextStrike* strike) { fPreserveStrike = strike; }
+    void setStrikeToPreserve(GrTextStrike* strike) { fPreserveStrike = strike; }
 
     // The user of the cache may hold a long-lived ref to the returned strike. However, actions by
     // another client of the cache may cause the strike to be purged while it is still reffed.
-    // Therefore, the caller must check GrAtlasTextStrike::isAbandoned() if there are other
+    // Therefore, the caller must check GrTextStrike::isAbandoned() if there are other
     // interactions with the cache since the strike was received.
-    inline GrAtlasTextStrike* getStrike(const SkGlyphCache* cache) {
-        GrAtlasTextStrike* strike = fCache.find(cache->getDescriptor());
-        if (nullptr == strike) {
+    inline sk_sp<GrTextStrike> getStrike(const SkGlyphCache* cache) {
+        sk_sp<GrTextStrike> strike = sk_ref_sp(fCache.find(cache->getDescriptor()));
+        if (!strike) {
             strike = this->generateStrike(cache);
         }
         return strike;
@@ -132,16 +132,17 @@
     static void HandleEviction(GrDrawOpAtlas::AtlasID, void*);
 
 private:
-    GrAtlasTextStrike* generateStrike(const SkGlyphCache* cache) {
-        GrAtlasTextStrike* strike = new GrAtlasTextStrike(cache->getDescriptor());
-        fCache.add(strike);
+    sk_sp<GrTextStrike> generateStrike(const SkGlyphCache* cache) {
+        // 'fCache' get the construction ref
+        sk_sp<GrTextStrike> strike = sk_ref_sp(new GrTextStrike(cache->getDescriptor()));
+        fCache.add(strike.get());
         return strike;
     }
 
-    using StrikeHash = SkTDynamicHash<GrAtlasTextStrike, SkDescriptor>;
+    using StrikeHash = SkTDynamicHash<GrTextStrike, SkDescriptor>;
 
     StrikeHash fCache;
-    GrAtlasTextStrike* fPreserveStrike;
+    GrTextStrike* fPreserveStrike;
     SkScalar fGlyphSizeLimit;
 };
 
diff --git a/src/gpu/text/GrTextUtils.h b/src/gpu/text/GrTextUtils.h
index 69421c2..ec26a86 100644
--- a/src/gpu/text/GrTextUtils.h
+++ b/src/gpu/text/GrTextUtils.h
@@ -18,7 +18,7 @@
 
 class GrAtlasTextBlob;
 class GrAtlasTextOp;
-class GrAtlasTextStrike;
+class GrTextStrike;
 class GrClip;
 class GrColorSpaceXform;
 class GrContext;