Rollback text allocation changes.

This also addresses an inadvertantly introduced distance field bug.

BUG=399125
R=bsalomon@google.com

Author: jvanverth@google.com

Review URL: https://codereview.chromium.org/438683006
diff --git a/src/gpu/GrBitmapTextContext.cpp b/src/gpu/GrBitmapTextContext.cpp
index cc657c6..3ca98e4 100755
--- a/src/gpu/GrBitmapTextContext.cpp
+++ b/src/gpu/GrBitmapTextContext.cpp
@@ -54,10 +54,12 @@
                                        : GrTextContext(context, properties) {
     fStrike = NULL;
 
+    fCurrTexture = NULL;
     fCurrVertex = 0;
     fEffectTextureUniqueID = SK_InvalidUniqueID;
 
     fVertices = NULL;
+    fMaxVertices = 0;
 
     fVertexBounds.setLargestInverted();
 }
@@ -89,14 +91,13 @@
     if (fCurrVertex > 0) {
         // setup our sampler state for our text texture/atlas
         SkASSERT(SkIsAlign4(fCurrVertex));
+        SkASSERT(fCurrTexture);
         GrTextureParams params(SkShader::kRepeat_TileMode, GrTextureParams::kNone_FilterMode);
 
-        GrTexture* currTexture = fStrike->getTexture();
-        SkASSERT(currTexture);
-        uint32_t textureUniqueID = currTexture->getUniqueID();
+        uint32_t textureUniqueID = fCurrTexture->getUniqueID();
         
         if (textureUniqueID != fEffectTextureUniqueID) {
-            fCachedEffect.reset(GrCustomCoordsTextureEffect::Create(currTexture, params));
+            fCachedEffect.reset(GrCustomCoordsTextureEffect::Create(fCurrTexture, params));
             fEffectTextureUniqueID = textureUniqueID;
         }
 
@@ -151,12 +152,13 @@
                                           nGlyphs,
                                           4, 6, &fVertexBounds);
 
+        fDrawTarget->resetVertexSource();
+        fVertices = NULL;
+        fMaxVertices = 0;
         fCurrVertex = 0;
         fVertexBounds.setLargestInverted();
+        SkSafeSetNull(fCurrTexture);
     }
-
-    fDrawTarget->resetVertexSource();
-    fVertices = NULL;
 }
 
 inline void GrBitmapTextContext::init(const GrPaint& paint, const SkPaint& skPaint) {
@@ -164,9 +166,11 @@
 
     fStrike = NULL;
 
+    fCurrTexture = NULL;
     fCurrVertex = 0;
 
     fVertices = NULL;
+    fMaxVertices = 0;
 }
 
 inline void GrBitmapTextContext::finish() {
@@ -187,18 +191,11 @@
 
     this->init(paint, skPaint);
 
-    if (NULL == fDrawTarget) {
-        return;
-    }
-
     SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc();
 
     SkAutoGlyphCache    autoCache(fSkPaint, &fDeviceProperties, &fContext->getMatrix());
     SkGlyphCache*       cache = autoCache.getCache();
     GrFontScaler*       fontScaler = GetGrFontScaler(cache);
-    if (NULL == fStrike) {
-        fStrike = fContext->getFontCache()->getStrike(fontScaler, false);
-    }
 
     // transform our starting point
     {
@@ -227,23 +224,6 @@
 
     const char* stop = text + byteLength;
 
-    // allocate vertices
-    SkASSERT(NULL == fVertices);
-    bool useColorVerts = kA8_GrMaskFormat == fStrike->getMaskFormat();
-    if (useColorVerts) {
-        fDrawTarget->drawState()->setVertexAttribs<gTextVertexWithColorAttribs>(
-                                                    SK_ARRAY_COUNT(gTextVertexWithColorAttribs));
-    } else {
-        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);
-
     SkAutoKern autokern;
 
     SkFixed fxMask = ~0;
@@ -304,42 +284,17 @@
 
     this->init(paint, skPaint);
 
-    if (NULL == fDrawTarget) {
-        return;
-    }
-
     SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc();
 
     SkAutoGlyphCache    autoCache(fSkPaint, &fDeviceProperties, &fContext->getMatrix());
     SkGlyphCache*       cache = autoCache.getCache();
     GrFontScaler*       fontScaler = GetGrFontScaler(cache);
-    
-    if (NULL == fStrike) {
-        fStrike = fContext->getFontCache()->getStrike(fontScaler, false);
-    }
 
     // store original matrix before we reset, so we can use it to transform positions
     SkMatrix ctm = fContext->getMatrix();
     GrContext::AutoMatrix  autoMatrix;
     autoMatrix.setIdentity(fContext, &fPaint);
 
-    // allocate vertices
-    SkASSERT(NULL == fVertices);
-    bool useColorVerts = kA8_GrMaskFormat == fStrike->getMaskFormat();
-    if (useColorVerts) {
-        fDrawTarget->drawState()->setVertexAttribs<gTextVertexWithColorAttribs>(
-                                                    SK_ARRAY_COUNT(gTextVertexWithColorAttribs));
-    } else {
-        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);
-
     const char*        stop = text + byteLength;
     SkTextAlignProc    alignProc(fSkPaint.getTextAlign());
     SkTextMapStateProc tmsProc(ctm, constY, scalarsPerPosition);
@@ -471,6 +426,14 @@
 void GrBitmapTextContext::drawPackedGlyph(GrGlyph::PackedID packed,
                                           SkFixed vx, SkFixed vy,
                                           GrFontScaler* scaler) {
+    if (NULL == fDrawTarget) {
+        return;
+    }
+
+    if (NULL == fStrike) {
+        fStrike = fContext->getFontCache()->getStrike(scaler, false);
+    }
+
     GrGlyph* glyph = fStrike->getGlyph(packed, scaler);
     if (NULL == glyph || glyph->fBounds.isEmpty()) {
         return;
@@ -553,6 +516,55 @@
     GrTexture* texture = glyph->fPlot->texture();
     SkASSERT(texture);
 
+    if (fCurrTexture != texture || fCurrVertex + 4 > fMaxVertices) {
+        this->flushGlyphs();
+        fCurrTexture = texture;
+        fCurrTexture->ref();
+    }
+
+    bool useColorVerts = kA8_GrMaskFormat == fStrike->getMaskFormat();
+
+    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;
+        if (useColorVerts) {
+            fDrawTarget->drawState()->setVertexAttribs<gTextVertexWithColorAttribs>(
+                SK_ARRAY_COUNT(gTextVertexWithColorAttribs));
+        } else {
+            fDrawTarget->drawState()->setVertexAttribs<gTextVertexAttribs>(
+                SK_ARRAY_COUNT(gTextVertexAttribs));
+        }
+        bool flush = fDrawTarget->geometryHints(&fMaxVertices, NULL);
+        if (flush) {
+            this->flushGlyphs();
+            fContext->flush();
+            if (useColorVerts) {
+                fDrawTarget->drawState()->setVertexAttribs<gTextVertexWithColorAttribs>(
+                  SK_ARRAY_COUNT(gTextVertexWithColorAttribs));
+            } else {
+                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,
+                                                               &fVertices,
+                                                               NULL);
+        GrAlwaysAssert(success);
+    }
+
     SkFixed tx = SkIntToFixed(glyph->fAtlasLocation.fX);
     SkFixed ty = SkIntToFixed(glyph->fAtlasLocation.fY);
 
@@ -564,7 +576,6 @@
 
     fVertexBounds.growToInclude(r);
 
-    bool useColorVerts = kA8_GrMaskFormat == fStrike->getMaskFormat();
     size_t vertSize = useColorVerts ? (2 * sizeof(SkPoint) + sizeof(GrColor)) :
                                       (2 * sizeof(SkPoint));
 
diff --git a/src/gpu/GrBitmapTextContext.h b/src/gpu/GrBitmapTextContext.h
index 84b7f37..6d4ea6d 100644
--- a/src/gpu/GrBitmapTextContext.h
+++ b/src/gpu/GrBitmapTextContext.h
@@ -45,6 +45,8 @@
     };
 
     void*                       fVertices;
+    int32_t                     fMaxVertices;
+    GrTexture*                  fCurrTexture;
     SkAutoTUnref<GrEffect>      fCachedEffect;
     // Used to check whether fCachedEffect is still valid.
     uint32_t                    fEffectTextureUniqueID;
diff --git a/src/gpu/GrDistanceFieldTextContext.cpp b/src/gpu/GrDistanceFieldTextContext.cpp
index 034980d..4e2567d 100755
--- a/src/gpu/GrDistanceFieldTextContext.cpp
+++ b/src/gpu/GrDistanceFieldTextContext.cpp
@@ -65,12 +65,14 @@
     fStrike = NULL;
     fGammaTexture = NULL;
 
+    fCurrTexture = NULL;
     fCurrVertex = 0;
     fEffectTextureUniqueID = SK_InvalidUniqueID;
     fEffectColor = GrColor_ILLEGAL;
     fEffectFlags = 0;
 
     fVertices = NULL;
+    fMaxVertices = 0;
 
     fVertexBounds.setLargestInverted();
 }
@@ -120,9 +122,7 @@
     GrTextureParams params(SkShader::kRepeat_TileMode, GrTextureParams::kBilerp_FilterMode);
     GrTextureParams gammaParams(SkShader::kClamp_TileMode, GrTextureParams::kNone_FilterMode);
     
-    GrTexture* currTexture = fStrike->getTexture();
-    SkASSERT(currTexture);
-    uint32_t textureUniqueID = currTexture->getUniqueID();
+    uint32_t textureUniqueID = fCurrTexture->getUniqueID();
     
     // set up any flags
     uint32_t flags = 0;
@@ -140,7 +140,7 @@
         flags != fEffectFlags) {
         if (fUseLCDText) {
             GrColor colorNoPreMul = skcolor_to_grcolor_nopremultiply(filteredColor);
-            fCachedEffect.reset(GrDistanceFieldLCDTextureEffect::Create(currTexture,
+            fCachedEffect.reset(GrDistanceFieldLCDTextureEffect::Create(fCurrTexture,
                                                                         params,
                                                                         fGammaTexture,
                                                                         gammaParams,
@@ -150,7 +150,7 @@
 #ifdef SK_GAMMA_APPLY_TO_A8
             U8CPU lum = SkColorSpaceLuminance::computeLuminance(fDeviceProperties.fGamma,
                                                                 filteredColor);
-            fCachedEffect.reset(GrDistanceFieldTextureEffect::Create(currTexture,
+            fCachedEffect.reset(GrDistanceFieldTextureEffect::Create(fCurrTexture,
                                                                      params,
                                                                      fGammaTexture,
                                                                      gammaParams,
@@ -228,17 +228,26 @@
         fDrawTarget->drawIndexedInstances(kTriangles_GrPrimitiveType,
                                           nGlyphs,
                                           4, 6, &fVertexBounds);
+        fDrawTarget->resetVertexSource();
+        fVertices = NULL;
+        fMaxVertices = 0;
         fCurrVertex = 0;
+        SkSafeSetNull(fCurrTexture);
         fVertexBounds.setLargestInverted();
     }
-
-    fDrawTarget->resetVertexSource();
-    fVertices = NULL;
 }
 
 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;
@@ -321,6 +330,55 @@
     GrTexture* texture = glyph->fPlot->texture();
     SkASSERT(texture);
 
+    if (fCurrTexture != texture || fCurrVertex + 4 > fMaxVertices) {
+        this->flushGlyphs();
+        fCurrTexture = texture;
+        fCurrTexture->ref();
+    }
+
+    bool useColorVerts = !fUseLCDText;
+    
+    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;
+        if (useColorVerts) {
+            fDrawTarget->drawState()->setVertexAttribs<gTextVertexWithColorAttribs>(
+                                                    SK_ARRAY_COUNT(gTextVertexWithColorAttribs));
+        } else {
+            fDrawTarget->drawState()->setVertexAttribs<gTextVertexAttribs>(
+                                                    SK_ARRAY_COUNT(gTextVertexAttribs));
+        }
+        bool flush = fDrawTarget->geometryHints(&fMaxVertices, NULL);
+        if (flush) {
+            this->flushGlyphs();
+            fContext->flush();
+            if (useColorVerts) {
+                fDrawTarget->drawState()->setVertexAttribs<gTextVertexWithColorAttribs>(
+                                                    SK_ARRAY_COUNT(gTextVertexWithColorAttribs));
+            } else {
+                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,
+                                                               &fVertices,
+                                                               NULL);
+        GrAlwaysAssert(success);
+    }
+    
     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);
@@ -340,10 +398,10 @@
     SkFixed th = SkIntToFixed(glyph->fBounds.height() - 2*SK_DistanceFieldInset);
 
     SkRect r;
-    r.fLeft = SkFixedToFloat(sx);
-    r.fTop = SkFixedToFloat(sy);
-    r.fRight = SkFixedToFloat(sx + width);
-    r.fBottom = SkFixedToFloat(sy + height);
+    r.fLeft = sx;
+    r.fTop = sy;
+    r.fRight = sx + width;
+    r.fBottom = sy + height;
 
     fVertexBounds.growToInclude(r);
 
@@ -364,7 +422,7 @@
                               SkFixedToFloat(texture->normalizeFixedX(tx + tw)),
                               SkFixedToFloat(texture->normalizeFixedY(ty + th)),
                               vertSize);
-    if (!fUseLCDText) {
+    if (useColorVerts) {
         // color comes after position.
         GrColor* colors = reinterpret_cast<GrColor*>(positions + 1);
         for (int i = 0; i < 4; ++i) {
@@ -464,10 +522,6 @@
 
     this->init(paint, skPaint);
 
-    if (NULL == fDrawTarget) {
-        return;
-    }
-
     SkScalar sizeRatio = fTextRatio;
 
     SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc();
@@ -475,28 +529,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);
-    if (!fUseLCDText) {
-        fDrawTarget->drawState()->setVertexAttribs<gTextVertexWithColorAttribs>(
-                                                       SK_ARRAY_COUNT(gTextVertexWithColorAttribs));
-    } else {
-        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;
@@ -564,34 +599,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);
-    if (!fUseLCDText) {
-        fDrawTarget->drawState()->setVertexAttribs<gTextVertexWithColorAttribs>(
-                                                       SK_ARRAY_COUNT(gTextVertexWithColorAttribs));
-    } else {
-        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);
 
diff --git a/src/gpu/GrDistanceFieldTextContext.h b/src/gpu/GrDistanceFieldTextContext.h
index fa0a76b..75a13b3 100644
--- a/src/gpu/GrDistanceFieldTextContext.h
+++ b/src/gpu/GrDistanceFieldTextContext.h
@@ -55,6 +55,8 @@
     };
 
     void*                   fVertices;
+    int32_t                 fMaxVertices;
+    GrTexture*              fCurrTexture;
     int                     fCurrVertex;
     SkRect                  fVertexBounds;
 };