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

Author: jvanverth@google.com

Review URL: https://codereview.chromium.org/414573002
diff --git a/src/gpu/GrBitmapTextContext.cpp b/src/gpu/GrBitmapTextContext.cpp
index 3ca98e4..cc657c6 100755
--- a/src/gpu/GrBitmapTextContext.cpp
+++ b/src/gpu/GrBitmapTextContext.cpp
@@ -54,12 +54,10 @@
                                        : GrTextContext(context, properties) {
     fStrike = NULL;
 
-    fCurrTexture = NULL;
     fCurrVertex = 0;
     fEffectTextureUniqueID = SK_InvalidUniqueID;
 
     fVertices = NULL;
-    fMaxVertices = 0;
 
     fVertexBounds.setLargestInverted();
 }
@@ -91,13 +89,14 @@
     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);
 
-        uint32_t textureUniqueID = fCurrTexture->getUniqueID();
+        GrTexture* currTexture = fStrike->getTexture();
+        SkASSERT(currTexture);
+        uint32_t textureUniqueID = currTexture->getUniqueID();
         
         if (textureUniqueID != fEffectTextureUniqueID) {
-            fCachedEffect.reset(GrCustomCoordsTextureEffect::Create(fCurrTexture, params));
+            fCachedEffect.reset(GrCustomCoordsTextureEffect::Create(currTexture, params));
             fEffectTextureUniqueID = textureUniqueID;
         }
 
@@ -152,13 +151,12 @@
                                           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) {
@@ -166,11 +164,9 @@
 
     fStrike = NULL;
 
-    fCurrTexture = NULL;
     fCurrVertex = 0;
 
     fVertices = NULL;
-    fMaxVertices = 0;
 }
 
 inline void GrBitmapTextContext::finish() {
@@ -191,11 +187,18 @@
 
     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
     {
@@ -224,6 +227,23 @@
 
     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;
@@ -284,17 +304,42 @@
 
     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);
@@ -426,14 +471,6 @@
 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;
@@ -516,55 +553,6 @@
     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);
 
@@ -576,6 +564,7 @@
 
     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 6d4ea6d..84b7f37 100644
--- a/src/gpu/GrBitmapTextContext.h
+++ b/src/gpu/GrBitmapTextContext.h
@@ -45,8 +45,6 @@
     };
 
     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 fe6e50f..8827382 100755
--- a/src/gpu/GrDistanceFieldTextContext.cpp
+++ b/src/gpu/GrDistanceFieldTextContext.cpp
@@ -48,11 +48,9 @@
     fStrike = NULL;
     fGammaTexture = NULL;
 
-    fCurrTexture = NULL;
     fCurrVertex = 0;
 
     fVertices = NULL;
-    fMaxVertices = 0;
 }
 
 GrDistanceFieldTextContext::~GrDistanceFieldTextContext() {
@@ -108,7 +106,8 @@
     if (fCurrVertex > 0) {
         // setup our sampler state for our text texture/atlas
         SkASSERT(SkIsAlign4(fCurrVertex));
-        SkASSERT(fCurrTexture);
+        GrTexture* currTexture = fStrike->getTexture();
+        SkASSERT(currTexture);
         GrTextureParams params(SkShader::kRepeat_TileMode, GrTextureParams::kBilerp_FilterMode);
         GrTextureParams gammaParams(SkShader::kClamp_TileMode, GrTextureParams::kNone_FilterMode);
 
@@ -125,7 +124,7 @@
             bool useBGR = SkDeviceProperties::Geometry::kBGR_Layout ==
                                                             fDeviceProperties.fGeometry.getLayout();
             drawState->addCoverageEffect(GrDistanceFieldLCDTextureEffect::Create(
-                                                            fCurrTexture,
+                                                            currTexture,
                                                             params,
                                                             fGammaTexture,
                                                             gammaParams,
@@ -155,14 +154,14 @@
             U8CPU lum = SkColorSpaceLuminance::computeLuminance(fDeviceProperties.fGamma,
                                                                 filteredColor);
             drawState->addCoverageEffect(GrDistanceFieldTextureEffect::Create(
-                                                              fCurrTexture, params,
+                                                              currTexture, params,
                                                               fGammaTexture, gammaParams,
                                                               lum/255.f,
                                                               fContext->getMatrix().isSimilarity()),
                                          kGlyphCoordsAttributeIndex)->unref();
 #else
             drawState->addCoverageEffect(GrDistanceFieldTextureEffect::Create(
-                                                              fCurrTexture, params,
+                                                              currTexture, params,
                                                               fContext->getMatrix().isSimilarity()),
                                          kGlyphCoordsAttributeIndex)->unref();
 #endif
@@ -176,12 +175,10 @@
         fDrawTarget->drawIndexedInstances(kTriangles_GrPrimitiveType,
                                           nGlyphs,
                                           4, 6);
-        fDrawTarget->resetVertexSource();
-        fVertices = NULL;
-        fMaxVertices = 0;
         fCurrVertex = 0;
-        SkSafeSetNull(fCurrTexture);
     }
+    fDrawTarget->resetVertexSource();
+    fVertices = NULL;
 }
 
 namespace {
@@ -197,13 +194,6 @@
 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;
@@ -286,44 +276,6 @@
     GrTexture* texture = glyph->fPlot->texture();
     SkASSERT(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);
@@ -343,12 +295,16 @@
     SkFixed th = SkIntToFixed(glyph->fBounds.height() - 2*SK_DistanceFieldInset);
 
     static const size_t kVertexSize = 2 * sizeof(SkPoint);
-    fVertices[2*fCurrVertex].setRectFan(sx,
-                                        sy,
-                                        sx + width,
-                                        sy + height,
-                                        kVertexSize);
-    fVertices[2*fCurrVertex+1].setRectFan(SkFixedToFloat(texture->normalizeFixedX(tx)),
+    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)),
                                           SkFixedToFloat(texture->normalizeFixedY(ty)),
                                           SkFixedToFloat(texture->normalizeFixedX(tx + tw)),
                                           SkFixedToFloat(texture->normalizeFixedY(ty + th)),
@@ -361,11 +317,9 @@
 
     fStrike = NULL;
 
-    fCurrTexture = NULL;
     fCurrVertex = 0;
 
     fVertices = NULL;
-    fMaxVertices = 0;
 
     if (fSkPaint.getTextSize() <= kSmallDFFontLimit) {
         fTextRatio = fSkPaint.getTextSize()/kSmallDFFontSize;
@@ -446,6 +400,10 @@
 
     this->init(paint, skPaint);
 
+    if (NULL == fDrawTarget) {
+        return;
+    }
+
     SkScalar sizeRatio = fTextRatio;
 
     SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc();
@@ -453,9 +411,23 @@
     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;
@@ -523,11 +495,29 @@
 
     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);
 
diff --git a/src/gpu/GrDistanceFieldTextContext.h b/src/gpu/GrDistanceFieldTextContext.h
index 3a602f0..7676fc1 100644
--- a/src/gpu/GrDistanceFieldTextContext.h
+++ b/src/gpu/GrDistanceFieldTextContext.h
@@ -48,9 +48,7 @@
         kDefaultRequestedVerts   = kDefaultRequestedGlyphs * 4,
     };
 
-    SkPoint*                fVertices;
-    int32_t                 fMaxVertices;
-    GrTexture*              fCurrTexture;
+    void*                   fVertices;
     int                     fCurrVertex;
 };
 
diff --git a/src/gpu/GrTextStrike.h b/src/gpu/GrTextStrike.h
index 89b0542..a5bb6ae 100644
--- a/src/gpu/GrTextStrike.h
+++ b/src/gpu/GrTextStrike.h
@@ -34,6 +34,7 @@
     const GrFontDescKey* getFontScalerKey() const { return fFontScalerKey; }
     GrFontCache* getFontCache() const { return fFontCache; }
     GrMaskFormat getMaskFormat() const { return fMaskFormat; }
+    GrTexture*   getTexture() const { return fAtlas->getTexture(); }
 
     inline GrGlyph* getGlyph(GrGlyph::PackedID, GrFontScaler*);
     bool addGlyphToAtlas(GrGlyph*, GrFontScaler*);