A small cleanup of GrAtlasTextContext

BUG=skia:

Review URL: https://codereview.chromium.org/1502323002
diff --git a/src/gpu/GrAtlasTextBlob.cpp b/src/gpu/GrAtlasTextBlob.cpp
index 3fb7ac9..21f2975 100644
--- a/src/gpu/GrAtlasTextBlob.cpp
+++ b/src/gpu/GrAtlasTextBlob.cpp
@@ -7,6 +7,69 @@
 
 #include "GrAtlasTextBlob.h"
 
+void GrAtlasTextBlob::appendGlyph(Run* run,
+                                  Run::SubRunInfo* subRun,
+                                  const SkRect& positions, GrColor color,
+                                  size_t vertexStride, bool useVertexColor,
+                                  GrGlyph* glyph) {
+    run->fVertexBounds.joinNonEmptyArg(positions);
+    run->fColor = color;
+
+    intptr_t vertex = reinterpret_cast<intptr_t>(this->fVertices + subRun->vertexEndIndex());
+
+    if (useVertexColor) {
+        // V0
+        SkPoint* position = reinterpret_cast<SkPoint*>(vertex);
+        position->set(positions.fLeft, positions.fTop);
+        SkColor* colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
+        *colorPtr = color;
+        vertex += vertexStride;
+
+        // V1
+        position = reinterpret_cast<SkPoint*>(vertex);
+        position->set(positions.fLeft, positions.fBottom);
+        colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
+        *colorPtr = color;
+        vertex += vertexStride;
+
+        // V2
+        position = reinterpret_cast<SkPoint*>(vertex);
+        position->set(positions.fRight, positions.fBottom);
+        colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
+        *colorPtr = color;
+        vertex += vertexStride;
+
+        // V3
+        position = reinterpret_cast<SkPoint*>(vertex);
+        position->set(positions.fRight, positions.fTop);
+        colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
+        *colorPtr = color;
+    } else {
+        // V0
+        SkPoint* position = reinterpret_cast<SkPoint*>(vertex);
+        position->set(positions.fLeft, positions.fTop);
+        vertex += vertexStride;
+
+        // V1
+        position = reinterpret_cast<SkPoint*>(vertex);
+        position->set(positions.fLeft, positions.fBottom);
+        vertex += vertexStride;
+
+        // V2
+        position = reinterpret_cast<SkPoint*>(vertex);
+        position->set(positions.fRight, positions.fBottom);
+        vertex += vertexStride;
+
+        // V3
+        position = reinterpret_cast<SkPoint*>(vertex);
+        position->set(positions.fRight, positions.fTop);
+    }
+    subRun->appendVertices(vertexStride);
+    fGlyphs[subRun->glyphEndIndex()] = glyph;
+    subRun->glyphAppended();
+}
+
+// TODO get this code building again
 #ifdef CACHE_SANITY_CHECK
 void GrAtlasTextBlob::AssertEqual(const GrAtlasTextBlob& l, const GrAtlasTextBlob& r) {
     SkASSERT(l.fSize == r.fSize);
diff --git a/src/gpu/GrAtlasTextBlob.h b/src/gpu/GrAtlasTextBlob.h
index 09f9dc0..22e2298 100644
--- a/src/gpu/GrAtlasTextBlob.h
+++ b/src/gpu/GrAtlasTextBlob.h
@@ -93,6 +93,7 @@
                 , fUseLCDText(that.fUseLCDText) {
             }
 
+            // TODO when this object is more internal, drop the privacy
             void resetBulkUseToken() { fBulkUseToken.reset(); }
             GrBatchAtlas::BulkUseTokenUpdater* bulkUseToken() { return &fBulkUseToken; }
             void setStrike(GrBatchTextStrike* strike) { fStrike.reset(SkRef(strike)); }
@@ -102,23 +103,27 @@
             uint64_t atlasGeneration() const { return fAtlasGeneration; }
 
             size_t byteCount() const { return fVertexEndIndex - fVertexStartIndex; }
-            void setVertexStartIndex(size_t vertStartIndex) { fVertexStartIndex = vertStartIndex;}
             size_t vertexStartIndex() const { return fVertexStartIndex; }
-            void setVertexEndIndex(size_t vertEndIndex) { fVertexEndIndex = vertEndIndex; }
             size_t vertexEndIndex() const { return fVertexEndIndex; }
             void appendVertices(size_t vertexStride) {
                 fVertexEndIndex += vertexStride * kVerticesPerGlyph;
             }
 
             uint32_t glyphCount() const { return fGlyphEndIndex - fGlyphStartIndex; }
-            void setGlyphStartIndex(uint32_t glyphStartIndex) { fGlyphStartIndex = glyphStartIndex;}
             uint32_t glyphStartIndex() const { return fGlyphStartIndex; }
-            void setGlyphEndIndex(uint32_t glyphEndIndex) { fGlyphEndIndex = glyphEndIndex; }
             uint32_t glyphEndIndex() const { return fGlyphEndIndex; }
             void glyphAppended() { fGlyphEndIndex++; }
             void setMaskFormat(GrMaskFormat format) { fMaskFormat = format; }
             GrMaskFormat maskFormat() const { return fMaskFormat; }
 
+            void setAsSuccessor(const SubRunInfo& prev) {
+                fGlyphStartIndex = prev.glyphEndIndex();
+                fGlyphEndIndex = prev.glyphEndIndex();
+
+                fVertexStartIndex = prev.vertexEndIndex();
+                fVertexEndIndex = prev.vertexEndIndex();
+            }
+
             // df properties
             void setUseLCDText(bool useLCDText) { fUseLCDText = useLCDText; }
             bool hasUseLCDText() const { return fUseLCDText; }
@@ -141,13 +146,9 @@
         SubRunInfo& push_back() {
             // Forward glyph / vertex information to seed the new sub run
             SubRunInfo& newSubRun = fSubRunInfo.push_back();
-            SubRunInfo& prevSubRun = fSubRunInfo.fromBack(1);
+            const SubRunInfo& prevSubRun = fSubRunInfo.fromBack(1);
 
-            newSubRun.setGlyphStartIndex(prevSubRun.glyphEndIndex());
-            newSubRun.setGlyphEndIndex(prevSubRun.glyphEndIndex());
-
-            newSubRun.setVertexStartIndex(prevSubRun.vertexEndIndex());
-            newSubRun.setVertexEndIndex(prevSubRun.vertexEndIndex());
+            newSubRun.setAsSuccessor(prevSubRun);
             return newSubRun;
         }
         static const int kMinSubRuns = 1;
@@ -269,11 +270,22 @@
     bool hasBitmap() const { return SkToBool(fTextType & kHasBitmap_TextType); }
     void setHasDistanceField() { fTextType |= kHasDistanceField_TextType; }
     void setHasBitmap() { fTextType |= kHasBitmap_TextType; }
-    void appendGlyph(Run::SubRunInfo* subrun, GrGlyph* glyph) {
-        this->fGlyphs[subrun->glyphEndIndex()] = glyph;
-        subrun->glyphAppended();
+
+    void push_back_run(int currRun) {
+        SkASSERT(currRun < fRunCount);
+        if (currRun > 0) {
+            Run::SubRunInfo& newRun = fRuns[currRun].fSubRunInfo.back();
+            Run::SubRunInfo& lastRun = fRuns[currRun - 1].fSubRunInfo.back();
+            newRun.setAsSuccessor(lastRun);
+        }
     }
 
+    void appendGlyph(Run* run,
+                     Run::SubRunInfo* subRun,
+                     const SkRect& positions, GrColor color,
+                     size_t vertexStride, bool useVertexColor,
+                     GrGlyph* glyph);
+
     static const int kVerticesPerGlyph = 4;
 
 #ifdef CACHE_SANITY_CHECK
diff --git a/src/gpu/GrAtlasTextContext.cpp b/src/gpu/GrAtlasTextContext.cpp
index cac52fb..5023857 100644
--- a/src/gpu/GrAtlasTextContext.cpp
+++ b/src/gpu/GrAtlasTextContext.cpp
@@ -461,17 +461,7 @@
 
         runPaint.setFlags(FilterTextFlags(fSurfaceProps, runPaint));
 
-        // setup vertex / glyphIndex for the new run
-        if (run > 0) {
-            PerSubRunInfo& newRun = cacheBlob->fRuns[run].fSubRunInfo.back();
-            PerSubRunInfo& lastRun = cacheBlob->fRuns[run - 1].fSubRunInfo.back();
-
-            newRun.setVertexStartIndex(lastRun.vertexEndIndex());
-            newRun.setVertexEndIndex(lastRun.vertexEndIndex());
-
-            newRun.setGlyphStartIndex(lastRun.glyphEndIndex());
-            newRun.setGlyphEndIndex(lastRun.glyphEndIndex());
-        }
+        cacheBlob->push_back_run(run);
 
         if (this->canDrawAsDistanceFields(runPaint, viewMatrix)) {
             cacheBlob->setHasDistanceField();
@@ -1011,7 +1001,7 @@
         subRun = &run.push_back();
         subRun->setStrike(fCurrStrike);
     } else if (!run.fInitialized) {
-        subRun->setStrike(SkRef(fCurrStrike));
+        subRun->setStrike(fCurrStrike);
     }
 
     run.fInitialized = true;
@@ -1024,8 +1014,7 @@
     r.fRight = r.fLeft + SkIntToScalar(width);
     r.fBottom = r.fTop + SkIntToScalar(height);
     subRun->setMaskFormat(format);
-    this->appendGlyphCommon(blob, &run, subRun, r, color, vertexStride, kA8_GrMaskFormat == format,
-                            glyph);
+    blob->appendGlyph(&run, subRun, r, color, vertexStride, kA8_GrMaskFormat == format, glyph);
 }
 
 bool GrAtlasTextContext::dfAppendGlyph(GrAtlasTextBlob* blob, int runIndex,
@@ -1085,8 +1074,7 @@
                                                               subRun->hasUseLCDText());
 
     bool useColorVerts = !subRun->hasUseLCDText();
-    this->appendGlyphCommon(blob, &run, subRun, glyphRect, color, vertexStride, useColorVerts,
-                            glyph);
+    blob->appendGlyph(&run, subRun, glyphRect, color, vertexStride, useColorVerts, glyph);
     return true;
 }
 
@@ -1105,67 +1093,6 @@
     blob->fBigGlyphs.push_back(GrAtlasTextBlob::BigGlyph(*glyph->fPath, x, y, scale, applyVM));
 }
 
-inline void GrAtlasTextContext::appendGlyphCommon(GrAtlasTextBlob* blob, Run* run,
-                                                  Run::SubRunInfo* subRun,
-                                                  const SkRect& positions, GrColor color,
-                                                  size_t vertexStride, bool useVertexColor,
-                                                  GrGlyph* glyph) {
-    blob->appendGlyph(subRun, glyph);
-    run->fVertexBounds.joinNonEmptyArg(positions);
-    run->fColor = color;
-
-    intptr_t vertex = reinterpret_cast<intptr_t>(blob->fVertices + subRun->vertexEndIndex());
-
-    if (useVertexColor) {
-        // V0
-        SkPoint* position = reinterpret_cast<SkPoint*>(vertex);
-        position->set(positions.fLeft, positions.fTop);
-        SkColor* colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
-        *colorPtr = color;
-        vertex += vertexStride;
-
-        // V1
-        position = reinterpret_cast<SkPoint*>(vertex);
-        position->set(positions.fLeft, positions.fBottom);
-        colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
-        *colorPtr = color;
-        vertex += vertexStride;
-
-        // V2
-        position = reinterpret_cast<SkPoint*>(vertex);
-        position->set(positions.fRight, positions.fBottom);
-        colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
-        *colorPtr = color;
-        vertex += vertexStride;
-
-        // V3
-        position = reinterpret_cast<SkPoint*>(vertex);
-        position->set(positions.fRight, positions.fTop);
-        colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
-        *colorPtr = color;
-    } else {
-        // V0
-        SkPoint* position = reinterpret_cast<SkPoint*>(vertex);
-        position->set(positions.fLeft, positions.fTop);
-        vertex += vertexStride;
-
-        // V1
-        position = reinterpret_cast<SkPoint*>(vertex);
-        position->set(positions.fLeft, positions.fBottom);
-        vertex += vertexStride;
-
-        // V2
-        position = reinterpret_cast<SkPoint*>(vertex);
-        position->set(positions.fRight, positions.fBottom);
-        vertex += vertexStride;
-
-        // V3
-        position = reinterpret_cast<SkPoint*>(vertex);
-        position->set(positions.fRight, positions.fTop);
-    }
-    subRun->appendVertices(vertexStride);
-}
-
 void GrAtlasTextContext::flushRunAsPaths(GrDrawContext* dc,
                                          const SkTextBlobRunIterator& it,
                                          const GrClip& clip, const SkPaint& skPaint,
diff --git a/src/gpu/GrAtlasTextContext.h b/src/gpu/GrAtlasTextContext.h
index 70eabd2..04f3968 100644
--- a/src/gpu/GrAtlasTextContext.h
+++ b/src/gpu/GrAtlasTextContext.h
@@ -65,10 +65,6 @@
     inline void appendGlyphPath(GrAtlasTextBlob*, GrGlyph*, GrFontScaler*, const SkGlyph&,
                                 SkScalar x, SkScalar y, SkScalar scale = 1.0f,
                                 bool applyVM = false);
-    inline void appendGlyphCommon(GrAtlasTextBlob*, Run*, Run::SubRunInfo*,
-                                  const SkRect& positions, GrColor color,
-                                  size_t vertexStride, bool useVertexColor,
-                                  GrGlyph*);
 
     inline void flushRunAsPaths(GrDrawContext*,
                                 const SkTextBlobRunIterator&, const GrClip& clip,