Revert of Move vertex buffer setup out of drawPackedGlyph(). (https://codereview.chromium.org/414573002/)

Reason for revert:
Breaking Ubuntu and Windows bots.

Original issue's description:
> Move vertex buffer setup out of drawPackedGlyph().
>
> This gets a very welcome ~10% speedup on my Mac.
>
> Committed: https://skia.googlesource.com/skia/+/1d38619389cc1100bd516b9acbf17b7bd42d33ce

R=bsalomon@google.com
TBR=bsalomon@google.com
NOTREECHECKS=true
NOTRY=true

Author: jvanverth@google.com

Review URL: https://codereview.chromium.org/419943002
diff --git a/src/gpu/GrDistanceFieldTextContext.cpp b/src/gpu/GrDistanceFieldTextContext.cpp
index f7d4f6f..fe6e50f 100755
--- a/src/gpu/GrDistanceFieldTextContext.cpp
+++ b/src/gpu/GrDistanceFieldTextContext.cpp
@@ -52,6 +52,7 @@
     fCurrVertex = 0;
 
     fVertices = NULL;
+    fMaxVertices = 0;
 }
 
 GrDistanceFieldTextContext::~GrDistanceFieldTextContext() {
@@ -177,6 +178,7 @@
                                           4, 6);
         fDrawTarget->resetVertexSource();
         fVertices = NULL;
+        fMaxVertices = 0;
         fCurrVertex = 0;
         SkSafeSetNull(fCurrTexture);
     }
@@ -195,6 +197,13 @@
 void GrDistanceFieldTextContext::drawPackedGlyph(GrGlyph::PackedID packed,
                                                  SkFixed vx, SkFixed vy,
                                                  GrFontScaler* scaler) {
+    if (NULL == fDrawTarget) {
+        return;
+    }
+    if (NULL == fStrike) {
+        fStrike = fContext->getFontCache()->getStrike(scaler, true);
+    }
+
     GrGlyph* glyph = fStrike->getGlyph(packed, scaler);
     if (NULL == glyph || glyph->fBounds.isEmpty()) {
         return;
@@ -277,12 +286,44 @@
     GrTexture* texture = glyph->fPlot->texture();
     SkASSERT(texture);
 
-    if (fCurrTexture != texture) {
+    if (fCurrTexture != texture || fCurrVertex + 4 > fMaxVertices) {
         this->flushGlyphs();
         fCurrTexture = texture;
         fCurrTexture->ref();
     }
 
+    if (NULL == fVertices) {
+       // If we need to reserve vertices allow the draw target to suggest
+        // a number of verts to reserve and whether to perform a flush.
+        fMaxVertices = kMinRequestedVerts;
+        fDrawTarget->drawState()->setVertexAttribs<gTextVertexAttribs>(
+            SK_ARRAY_COUNT(gTextVertexAttribs));
+        bool flush = fDrawTarget->geometryHints(&fMaxVertices, NULL);
+        if (flush) {
+            this->flushGlyphs();
+            fContext->flush();
+            fDrawTarget->drawState()->setVertexAttribs<gTextVertexAttribs>(
+                SK_ARRAY_COUNT(gTextVertexAttribs));
+        }
+        fMaxVertices = kDefaultRequestedVerts;
+        // ignore return, no point in flushing again.
+        fDrawTarget->geometryHints(&fMaxVertices, NULL);
+
+        int maxQuadVertices = 4 * fContext->getQuadIndexBuffer()->maxQuads();
+        if (fMaxVertices < kMinRequestedVerts) {
+            fMaxVertices = kDefaultRequestedVerts;
+        } else if (fMaxVertices > maxQuadVertices) {
+            // don't exceed the limit of the index buffer
+            fMaxVertices = maxQuadVertices;
+        }
+        bool success = fDrawTarget->reserveVertexAndIndexSpace(fMaxVertices,
+                                                               0,
+                                                               GrTCast<void**>(&fVertices),
+                                                               NULL);
+        GrAlwaysAssert(success);
+        SkASSERT(2*sizeof(SkPoint) == fDrawTarget->getDrawState().getVertexSize());
+    }
+
     SkScalar dx = SkIntToScalar(glyph->fBounds.fLeft + SK_DistanceFieldInset);
     SkScalar dy = SkIntToScalar(glyph->fBounds.fTop + SK_DistanceFieldInset);
     SkScalar width = SkIntToScalar(glyph->fBounds.width() - 2*SK_DistanceFieldInset);
@@ -302,16 +343,12 @@
     SkFixed th = SkIntToFixed(glyph->fBounds.height() - 2*SK_DistanceFieldInset);
 
     static const size_t kVertexSize = 2 * sizeof(SkPoint);
-    SkPoint* positions = reinterpret_cast<SkPoint*>(
-                                reinterpret_cast<intptr_t>(fVertices) + kVertexSize * fCurrVertex);
-    positions->setRectFan(sx,
-                          sy,
-                          sx + width,
-                          sy + height,
-                          kVertexSize);
-    SkPoint* textureCoords = reinterpret_cast<SkPoint*>(
-                            reinterpret_cast<intptr_t>(positions) + kVertexSize  - sizeof(SkPoint));
-    textureCoords->setRectFan(SkFixedToFloat(texture->normalizeFixedX(tx)),
+    fVertices[2*fCurrVertex].setRectFan(sx,
+                                        sy,
+                                        sx + width,
+                                        sy + height,
+                                        kVertexSize);
+    fVertices[2*fCurrVertex+1].setRectFan(SkFixedToFloat(texture->normalizeFixedX(tx)),
                                           SkFixedToFloat(texture->normalizeFixedY(ty)),
                                           SkFixedToFloat(texture->normalizeFixedX(tx + tw)),
                                           SkFixedToFloat(texture->normalizeFixedY(ty + th)),
@@ -328,6 +365,7 @@
     fCurrVertex = 0;
 
     fVertices = NULL;
+    fMaxVertices = 0;
 
     if (fSkPaint.getTextSize() <= kSmallDFFontLimit) {
         fTextRatio = fSkPaint.getTextSize()/kSmallDFFontSize;
@@ -408,10 +446,6 @@
 
     this->init(paint, skPaint);
 
-    if (NULL == fDrawTarget) {
-        return;
-    }
-
     SkScalar sizeRatio = fTextRatio;
 
     SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc();
@@ -419,23 +453,9 @@
     SkAutoGlyphCacheNoGamma    autoCache(fSkPaint, &fDeviceProperties, NULL);
     SkGlyphCache*              cache = autoCache.getCache();
     GrFontScaler*              fontScaler = GetGrFontScaler(cache);
-    if (NULL == fStrike) {
-        fStrike = fContext->getFontCache()->getStrike(fontScaler, true);
-    }
 
     setup_gamma_texture(fContext, cache, fDeviceProperties, &fGammaTexture);
 
-    // allocate vertices
-    SkASSERT(NULL == fVertices);
-    fDrawTarget->drawState()->setVertexAttribs<gTextVertexAttribs>(
-                                                                SK_ARRAY_COUNT(gTextVertexAttribs));
-    int numGlyphs = fSkPaint.textToGlyphs(text, byteLength, NULL);
-    bool success = fDrawTarget->reserveVertexAndIndexSpace(4*numGlyphs,
-                                                           0,
-                                                           &fVertices,
-                                                           NULL);
-    GrAlwaysAssert(success);
-
     // need to measure first
     // TODO - generate positions and pre-load cache as well?
     const char* stop = text + byteLength;
@@ -503,29 +523,11 @@
 
     this->init(paint, skPaint);
 
-    if (NULL == fDrawTarget) {
-        return;
-    }
-
     SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc();
 
     SkAutoGlyphCacheNoGamma    autoCache(fSkPaint, &fDeviceProperties, NULL);
     SkGlyphCache*              cache = autoCache.getCache();
     GrFontScaler*              fontScaler = GetGrFontScaler(cache);
-    if (NULL == fStrike) {
-        fStrike = fContext->getFontCache()->getStrike(fontScaler, true);
-    }
-
-    // allocate vertices
-    SkASSERT(NULL == fVertices);
-    fDrawTarget->drawState()->setVertexAttribs<gTextVertexAttribs>(
-                                                                SK_ARRAY_COUNT(gTextVertexAttribs));
-    int numGlyphs = fSkPaint.textToGlyphs(text, byteLength, NULL);
-    bool success = fDrawTarget->reserveVertexAndIndexSpace(4*numGlyphs,
-                                                           0,
-                                                           &fVertices,
-                                                           NULL);
-    GrAlwaysAssert(success);
 
     setup_gamma_texture(fContext, cache, fDeviceProperties, &fGammaTexture);