Calculate translations to apply to vertices in batch

BUG=skia:
GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1716443005

Review URL: https://codereview.chromium.org/1716443005
diff --git a/src/gpu/batches/GrAtlasTextBatch.cpp b/src/gpu/batches/GrAtlasTextBatch.cpp
index 6fea132..432322f 100644
--- a/src/gpu/batches/GrAtlasTextBatch.cpp
+++ b/src/gpu/batches/GrAtlasTextBatch.cpp
@@ -34,8 +34,8 @@
         str.appendf("%d: Color: 0x%08x Trans: %.2f,%.2f Runs: %d\n",
                     i,
                     fGeoData[i].fColor,
-                    fGeoData[i].fTransX,
-                    fGeoData[i].fTransY,
+                    fGeoData[i].fX,
+                    fGeoData[i].fY,
                     fGeoData[i].fBlob->runCount());
     }
 
@@ -148,10 +148,10 @@
         Blob* blob = args.fBlob;
         size_t byteCount;
         void* blobVertices;
-        int glyphCount;
+        int subRunGlyphCount;
         blob->regenInBatch(target, fFontCache, &helper, args.fRun, args.fSubRun, &cache,
-                           &typeface, &scaler, &desc, vertexStride, args.fColor, args.fTransX,
-                           args.fTransY, &blobVertices, &byteCount, &glyphCount);
+                           &typeface, &scaler, &desc, vertexStride, args.fViewMatrix, args.fX,
+                           args.fY, args.fColor, &blobVertices, &byteCount, &subRunGlyphCount);
 
         // now copy all vertices
         memcpy(currVertex, blobVertices, byteCount);
@@ -161,10 +161,10 @@
         SkRect rect;
         rect.setLargestInverted();
         SkPoint* vertex = (SkPoint*) ((char*)blobVertices);
-        rect.growToInclude(vertex, vertexStride, kVerticesPerGlyph * glyphCount);
+        rect.growToInclude(vertex, vertexStride, kVerticesPerGlyph * subRunGlyphCount);
 
         if (this->usesDistanceFields()) {
-            fBatch.fViewMatrix.mapRect(&rect);
+            args.fViewMatrix.mapRect(&rect);
         }
         SkASSERT(fBounds.contains(rect));
 #endif
diff --git a/src/gpu/batches/GrAtlasTextBatch.h b/src/gpu/batches/GrAtlasTextBatch.h
index d90aa19..435fb59 100644
--- a/src/gpu/batches/GrAtlasTextBatch.h
+++ b/src/gpu/batches/GrAtlasTextBatch.h
@@ -22,12 +22,13 @@
 
     typedef GrAtlasTextBlob Blob;
     struct Geometry {
+        SkMatrix fViewMatrix;
         Blob* fBlob;
+        SkScalar fX;
+        SkScalar fY;
         int fRun;
         int fSubRun;
         GrColor fColor;
-        SkScalar fTransX;
-        SkScalar fTransY;
     };
 
     static GrAtlasTextBatch* CreateBitmap(GrMaskFormat maskFormat, int glyphCount,
@@ -79,9 +80,9 @@
     void init() {
         const Geometry& geo = fGeoData[0];
         fBatch.fColor = geo.fColor;
-        fBatch.fViewMatrix = geo.fBlob->viewMatrix();
 
-        geo.fBlob->computeSubRunBounds(&fBounds, geo.fRun, geo.fSubRun);
+        geo.fBlob->computeSubRunBounds(&fBounds, geo.fRun, geo.fSubRun, geo.fViewMatrix, geo.fX,
+                                       geo.fY);
     }
 
     const char* name() const override { return "TextBatch"; }
@@ -141,7 +142,7 @@
     inline void flush(GrVertexBatch::Target* target, FlushInfo* flushInfo) const;
 
     GrColor color() const { return fBatch.fColor; }
-    const SkMatrix& viewMatrix() const { return fBatch.fViewMatrix; }
+    const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; }
     bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; }
     int numGlyphs() const { return fBatch.fNumGlyphs; }
 
@@ -154,7 +155,6 @@
 
     struct BatchTracker {
         GrColor fColor;
-        SkMatrix fViewMatrix;
         bool fUsesLocalCoords;
         bool fColorIgnored;
         bool fCoverageIgnored;
diff --git a/src/gpu/text/GrAtlasTextBlob.cpp b/src/gpu/text/GrAtlasTextBlob.cpp
index f2ca3d2..26f6a7c 100644
--- a/src/gpu/text/GrAtlasTextBlob.cpp
+++ b/src/gpu/text/GrAtlasTextBlob.cpp
@@ -165,8 +165,7 @@
     fBigGlyphs.push_back(GrAtlasTextBlob::BigGlyph(*glyph->fPath, x, y, scale, applyVM));
 }
 
-bool GrAtlasTextBlob::mustRegenerate(SkScalar* outTransX, SkScalar* outTransY,
-                                     const SkPaint& paint,
+bool GrAtlasTextBlob::mustRegenerate(const SkPaint& paint,
                                      GrColor color, const SkMaskFilter::BlurRec& blurRec,
                                      const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
     // If we have LCD text then our canonical color will be set to transparent, in this case we have
@@ -177,11 +176,11 @@
         return true;
     }
 
-    if (fViewMatrix.hasPerspective() != viewMatrix.hasPerspective()) {
+    if (fInitialViewMatrix.hasPerspective() != viewMatrix.hasPerspective()) {
         return true;
     }
 
-    if (fViewMatrix.hasPerspective() && !fViewMatrix.cheapEqualTo(viewMatrix)) {
+    if (fInitialViewMatrix.hasPerspective() && !fInitialViewMatrix.cheapEqualTo(viewMatrix)) {
         return true;
     }
 
@@ -205,17 +204,17 @@
     // for mixed blobs if this becomes an issue.
     if (this->hasBitmap() && this->hasDistanceField()) {
         // Identical viewmatrices and we can reuse in all cases
-        if (fViewMatrix.cheapEqualTo(viewMatrix) && x == fX && y == fY) {
+        if (fInitialViewMatrix.cheapEqualTo(viewMatrix) && x == fInitialX && y == fInitialY) {
             return false;
         }
         return true;
     }
 
     if (this->hasBitmap()) {
-        if (fViewMatrix.getScaleX() != viewMatrix.getScaleX() ||
-            fViewMatrix.getScaleY() != viewMatrix.getScaleY() ||
-            fViewMatrix.getSkewX() != viewMatrix.getSkewX() ||
-            fViewMatrix.getSkewY() != viewMatrix.getSkewY()) {
+        if (fInitialViewMatrix.getScaleX() != viewMatrix.getScaleX() ||
+            fInitialViewMatrix.getScaleY() != viewMatrix.getScaleY() ||
+            fInitialViewMatrix.getSkewX() != viewMatrix.getSkewX() ||
+            fInitialViewMatrix.getSkewY() != viewMatrix.getSkewY()) {
             return true;
         }
 
@@ -224,39 +223,27 @@
         // This cool bit of math will determine the necessary translation to apply to the already
         // generated vertex coordinates to move them to the correct position
         SkScalar transX = viewMatrix.getTranslateX() +
-                          viewMatrix.getScaleX() * (x - fX) +
-                          viewMatrix.getSkewX() * (y - fY) -
-                          fViewMatrix.getTranslateX();
+                          viewMatrix.getScaleX() * (x - fInitialX) +
+                          viewMatrix.getSkewX() * (y - fInitialY) -
+                          fInitialViewMatrix.getTranslateX();
         SkScalar transY = viewMatrix.getTranslateY() +
-                          viewMatrix.getSkewY() * (x - fX) +
-                          viewMatrix.getScaleY() * (y - fY) -
-                          fViewMatrix.getTranslateY();
-        if (!SkScalarIsInt(transX) || !SkScalarIsInt(transY) ) {
+                          viewMatrix.getSkewY() * (x - fInitialX) +
+                          viewMatrix.getScaleY() * (y - fInitialY) -
+                          fInitialViewMatrix.getTranslateY();
+        if (!SkScalarIsInt(transX) || !SkScalarIsInt(transY)) {
             return true;
         }
-
-        (*outTransX) = transX;
-        (*outTransY) = transY;
     } else if (this->hasDistanceField()) {
         // A scale outside of [blob.fMaxMinScale, blob.fMinMaxScale] would result in a different
         // distance field being generated, so we have to regenerate in those cases
         SkScalar newMaxScale = viewMatrix.getMaxScale();
-        SkScalar oldMaxScale = fViewMatrix.getMaxScale();
+        SkScalar oldMaxScale = fInitialViewMatrix.getMaxScale();
         SkScalar scaleAdjust = newMaxScale / oldMaxScale;
         if (scaleAdjust < fMaxMinScale || scaleAdjust > fMinMaxScale) {
             return true;
         }
-
-        (*outTransX) = x - fX;
-        (*outTransY) = y - fY;
     }
 
-    // If we can reuse the blob, then make sure we update the blob's viewmatrix, and x/y
-    // offsets.  Note, we offset the vertex bounds right before flushing
-    fViewMatrix = viewMatrix;
-    fX = x;
-    fY = y;
-
     // It is possible that a blob has neither distanceField nor bitmaptext.  This is in the case
     // when all of the runs inside the blob are drawn as paths.  In this case, we always regenerate
     // the blob anyways at flush time, so no need to regenerate explicitly
@@ -266,7 +253,8 @@
 inline GrDrawBatch* GrAtlasTextBlob::createBatch(
                                               const Run::SubRunInfo& info,
                                               int glyphCount, int run, int subRun,
-                                              GrColor color, SkScalar transX, SkScalar transY,
+                                              const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
+                                              GrColor color,
                                               const SkPaint& skPaint, const SkSurfaceProps& props,
                                               const GrDistanceFieldAdjustTable* distanceAdjustTable,
                                               GrBatchFontCache* cache) {
@@ -296,12 +284,13 @@
         batch = GrAtlasTextBatch::CreateBitmap(format, glyphCount, cache);
     }
     GrAtlasTextBatch::Geometry& geometry = batch->geometry();
+    geometry.fViewMatrix = viewMatrix;
     geometry.fBlob = SkRef(this);
     geometry.fRun = run;
     geometry.fSubRun = subRun;
     geometry.fColor = subRunColor;
-    geometry.fTransX = transX;
-    geometry.fTransY = transY;
+    geometry.fX = x;
+    geometry.fY = y;
     batch->init();
 
     return batch;
@@ -309,8 +298,8 @@
 
 inline
 void GrAtlasTextBlob::flushRun(GrDrawContext* dc, GrPipelineBuilder* pipelineBuilder,
-                               int run, GrColor color,
-                               SkScalar transX, SkScalar transY,
+                               int run, const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
+                               GrColor color,
                                const SkPaint& skPaint, const SkSurfaceProps& props,
                                const GrDistanceFieldAdjustTable* distanceAdjustTable,
                                GrBatchFontCache* cache) {
@@ -322,26 +311,48 @@
         }
 
         SkAutoTUnref<GrDrawBatch> batch(this->createBatch(info, glyphCount, run,
-                                                          subRun, color, transX, transY,
+                                                          subRun, viewMatrix, x, y, color,
                                                           skPaint, props,
                                                           distanceAdjustTable, cache));
         dc->drawBatch(pipelineBuilder, batch);
     }
 }
 
+static void calculate_translation(bool applyVM,
+                                  const SkMatrix& newViewMatrix, SkScalar newX, SkScalar newY,
+                                  const SkMatrix& currentViewMatrix, SkScalar currentX,
+                                  SkScalar currentY, SkScalar* transX, SkScalar* transY) {
+    if (applyVM) {
+        *transX = newViewMatrix.getTranslateX() +
+                  newViewMatrix.getScaleX() * (newX - currentX) +
+                  newViewMatrix.getSkewX() * (newY - currentY) -
+                  currentViewMatrix.getTranslateX();
+
+        *transY = newViewMatrix.getTranslateY() +
+                  newViewMatrix.getSkewY() * (newX - currentX) +
+                  newViewMatrix.getScaleY() * (newY - currentY) -
+                  currentViewMatrix.getTranslateY();
+    } else {
+        *transX = newX - currentX;
+        *transY = newY - currentY;
+    }
+}
+
+
 void GrAtlasTextBlob::flushBigGlyphs(GrContext* context, GrDrawContext* dc,
                                      const GrClip& clip, const SkPaint& skPaint,
-                                     SkScalar transX, SkScalar transY,
+                                     const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
                                      const SkIRect& clipBounds) {
+    SkScalar transX, transY;
     for (int i = 0; i < fBigGlyphs.count(); i++) {
         GrAtlasTextBlob::BigGlyph& bigGlyph = fBigGlyphs[i];
-        bigGlyph.fVx += transX;
-        bigGlyph.fVy += transY;
+        calculate_translation(bigGlyph.fApplyVM, viewMatrix, x, y,
+                              fInitialViewMatrix, fInitialX, fInitialY, &transX, &transY);
         SkMatrix ctm;
         ctm.setScale(bigGlyph.fScale, bigGlyph.fScale);
-        ctm.postTranslate(bigGlyph.fVx, bigGlyph.fVy);
+        ctm.postTranslate(bigGlyph.fX + transX, bigGlyph.fY + transY);
         if (bigGlyph.fApplyVM) {
-            ctm.postConcat(fViewMatrix);
+            ctm.postConcat(viewMatrix);
         }
 
         GrBlurUtils::drawPathWithMaskFilter(context, dc, clip, bigGlyph.fPath,
@@ -399,8 +410,7 @@
                                   const GrClip& clip,
                                   const SkMatrix& viewMatrix,
                                   const SkIRect& clipBounds,
-                                  SkScalar x, SkScalar y,
-                                  SkScalar transX, SkScalar transY) {
+                                  SkScalar x, SkScalar y) {
     // We loop through the runs of the blob, flushing each.  If any run is too large, then we flush
     // it as paths
     GrPipelineBuilder pipelineBuilder(grPaint, dc->accessRenderTarget(), clip);
@@ -414,13 +424,12 @@
                                   drawFilter, viewMatrix, clipBounds, x, y);
             continue;
         }
-        this->flushRun(dc, &pipelineBuilder, run, color,
-                       transX, transY, skPaint, props,
+        this->flushRun(dc, &pipelineBuilder, run, viewMatrix, x, y, color, skPaint, props,
                        distanceAdjustTable, context->getBatchFontCache());
     }
 
     // Now flush big glyphs
-    this->flushBigGlyphs(context, dc, clip, skPaint, transX, transY, clipBounds);
+    this->flushBigGlyphs(context, dc, clip, skPaint, viewMatrix, x, y, clipBounds);
 }
 
 void GrAtlasTextBlob::flushThrowaway(GrContext* context,
@@ -430,27 +439,30 @@
                                      const SkPaint& skPaint,
                                      const GrPaint& grPaint,
                                      const GrClip& clip,
-                                     const SkIRect& clipBounds) {
+                                     const SkMatrix& viewMatrix,
+                                     const SkIRect& clipBounds,
+                                     SkScalar x, SkScalar y) {
     GrPipelineBuilder pipelineBuilder(grPaint, dc->accessRenderTarget(), clip);
 
     GrColor color = grPaint.getColor();
     for (int run = 0; run < fRunCount; run++) {
-        this->flushRun(dc, &pipelineBuilder, run, color, 0, 0, skPaint, props,
+        this->flushRun(dc, &pipelineBuilder, run, viewMatrix, x, y, color, skPaint, props,
                        distanceAdjustTable, context->getBatchFontCache());
     }
 
     // Now flush big glyphs
-    this->flushBigGlyphs(context, dc, clip, skPaint, 0, 0, clipBounds);
+    this->flushBigGlyphs(context, dc, clip, skPaint, viewMatrix, x, y, clipBounds);
 }
 
 GrDrawBatch* GrAtlasTextBlob::test_createBatch(
                                               int glyphCount, int run, int subRun,
-                                              GrColor color, SkScalar transX, SkScalar transY,
+                                              const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
+                                              GrColor color,
                                               const SkPaint& skPaint, const SkSurfaceProps& props,
                                               const GrDistanceFieldAdjustTable* distanceAdjustTable,
                                               GrBatchFontCache* cache) {
     const GrAtlasTextBlob::Run::SubRunInfo& info = fRuns[run].fSubRunInfo[subRun];
-    return this->createBatch(info, glyphCount, run, subRun, color, transX, transY, skPaint,
+    return this->createBatch(info, glyphCount, run, subRun, viewMatrix, x, y, color, skPaint,
                              props, distanceAdjustTable, cache);
 }
 
@@ -476,7 +488,6 @@
     }
 
     SkASSERT_RELEASE(l.fKey == r.fKey);
-    SkASSERT_RELEASE(l.fViewMatrix.cheapEqualTo(r.fViewMatrix));
     //SkASSERT_RELEASE(l.fPaintColor == r.fPaintColor); // Colors might not actually be identical
     SkASSERT_RELEASE(l.fMaxMinScale == r.fMaxMinScale);
     SkASSERT_RELEASE(l.fMinMaxScale == r.fMinMaxScale);
@@ -540,3 +551,13 @@
         }
     }
 }
+
+void GrAtlasTextBlob::Run::SubRunInfo::computeTranslation(const SkMatrix& viewMatrix,
+                                                          SkScalar x, SkScalar y, SkScalar* transX,
+                                                          SkScalar* transY) {
+    calculate_translation(!this->drawAsDistanceFields(), viewMatrix, x, y,
+                          fCurrentViewMatrix, fX, fY, transX, transY);
+    fCurrentViewMatrix = viewMatrix;
+    fX = x;
+    fY = y;
+}
diff --git a/src/gpu/text/GrAtlasTextBlob.h b/src/gpu/text/GrAtlasTextBlob.h
index 8b175c8..a026693 100644
--- a/src/gpu/text/GrAtlasTextBlob.h
+++ b/src/gpu/text/GrAtlasTextBlob.h
@@ -173,8 +173,7 @@
         }
     }
 
-    bool mustRegenerate(SkScalar* outTransX, SkScalar* outTransY, const SkPaint& paint,
-                        GrColor color, const SkMaskFilter::BlurRec& blurRec,
+    bool mustRegenerate(const SkPaint& paint, GrColor color, const SkMaskFilter::BlurRec& blurRec,
                         const SkMatrix& viewMatrix, SkScalar x, SkScalar y);
 
     // flush a GrAtlasTextBlob associated with a SkTextBlob
@@ -189,8 +188,7 @@
                      const GrClip& clip,
                      const SkMatrix& viewMatrix,
                      const SkIRect& clipBounds,
-                     SkScalar x, SkScalar y,
-                     SkScalar transX, SkScalar transY);
+                     SkScalar x, SkScalar y);
 
     // flush a throwaway GrAtlasTextBlob *not* associated with an SkTextBlob
     void flushThrowaway(GrContext* context,
@@ -200,9 +198,12 @@
                         const SkPaint& skPaint,
                         const GrPaint& grPaint,
                         const GrClip& clip,
-                        const SkIRect& clipBounds);
+                        const SkMatrix& viewMatrix,
+                        const SkIRect& clipBounds,
+                        SkScalar x, SkScalar y);
 
-    void computeSubRunBounds(SkRect* outBounds, int runIndex, int subRunIndex) {
+    void computeSubRunBounds(SkRect* outBounds, int runIndex, int subRunIndex,
+                             const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
         // We don't yet position distance field text on the cpu, so we have to map the vertex bounds
         // into device space.
         // We handle vertex bounds differently for distance field text and bitmap text because
@@ -214,8 +215,8 @@
         if (subRun.drawAsDistanceFields()) {
             // Distance field text is positioned with the (X,Y) as part of the glyph position,
             // and currently the view matrix is applied on the GPU
-            outBounds->offset(fX - fInitialX, fY - fInitialY);
-            fViewMatrix.mapRect(outBounds);
+            outBounds->offset(x - fInitialX, y - fInitialY);
+            viewMatrix.mapRect(outBounds);
         } else {
             // Bitmap text is fully positioned on the CPU, and offset by an (X,Y) translate in
             // device space.
@@ -223,9 +224,9 @@
 
             boundsMatrix.postTranslate(-fInitialX, -fInitialY);
 
-            boundsMatrix.postTranslate(fX, fY);
+            boundsMatrix.postTranslate(x, y);
 
-            boundsMatrix.postConcat(fViewMatrix);
+            boundsMatrix.postConcat(viewMatrix);
             boundsMatrix.mapRect(outBounds);
 
             // Due to floating point numerical inaccuracies, we have to round out here
@@ -233,9 +234,6 @@
         }
     }
 
-    const SkMatrix& viewMatrix() const { return fViewMatrix; }
-
-
     // position + local coord
     static const size_t kColorTextVASize = sizeof(SkPoint) + sizeof(SkIPoint16);
     static const size_t kGrayTextVASize = sizeof(SkPoint) + sizeof(GrColor) + sizeof(SkIPoint16);
@@ -261,7 +259,8 @@
                       GrBlobRegenHelper *helper, int run, int subRun, SkGlyphCache** cache,
                       SkTypeface** typeface, GrFontScaler** scaler,
                       const SkDescriptor** desc, size_t vertexStride,
-                      GrColor color, SkScalar transX, SkScalar transY,
+                      const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
+                      GrColor color,
                       void** vertices, size_t* byteCount, int* glyphCount);
 
     const Key& key() const { return fKey; }
@@ -275,7 +274,7 @@
     ////////////////////////////////////////////////////////////////////////////////////////////////
     // Internal test methods
     GrDrawBatch* test_createBatch(int glyphCount, int run, int subRun,
-                                  GrColor color, SkScalar transX, SkScalar transY,
+                                  const SkMatrix& viewMatrix, SkScalar x, SkScalar y, GrColor color,
                                   const SkPaint& skPaint, const SkSurfaceProps& props,
                                   const GrDistanceFieldAdjustTable* distanceAdjustTable,
                                   GrBatchFontCache* cache);
@@ -290,15 +289,14 @@
                           SkScalar x, SkScalar y, SkScalar scale, bool applyVM);
 
     inline void flushRun(GrDrawContext* dc, GrPipelineBuilder* pipelineBuilder,
-                         int run, GrColor color,
-                         SkScalar transX, SkScalar transY,
+                         int run, const SkMatrix& viewMatrix, SkScalar x, SkScalar y, GrColor color,
                          const SkPaint& skPaint, const SkSurfaceProps& props,
                          const GrDistanceFieldAdjustTable* distanceAdjustTable,
                          GrBatchFontCache* cache);
 
     void flushBigGlyphs(GrContext* context, GrDrawContext* dc,
                         const GrClip& clip, const SkPaint& skPaint,
-                        SkScalar transX, SkScalar transY,
+                        const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
                         const SkIRect& clipBounds);
 
     void flushRunAsPaths(GrContext* context,
@@ -309,18 +307,23 @@
                          SkDrawFilter* drawFilter, const SkMatrix& viewMatrix,
                          const SkIRect& clipBounds, SkScalar x, SkScalar y);
 
-    // This function will only be called when we are regenerating a blob from scratch. We record the
+    // This function will only be called when we are generating a blob from scratch. We record the
     // initial view matrix and initial offsets(x,y), because we record vertex bounds relative to
     // these numbers.  When blobs are reused with new matrices, we need to return to model space so
     // we can update the vertex bounds appropriately.
     void setupViewMatrix(const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
-        fViewMatrix = viewMatrix;
+        fInitialViewMatrix = viewMatrix;
         if (!viewMatrix.invert(&fInitialViewMatrixInverse)) {
             fInitialViewMatrixInverse = SkMatrix::I();
             SkDebugf("Could not invert viewmatrix\n");
         }
-        fX = fInitialX = x;
-        fY = fInitialY = y;
+        fInitialX = x;
+        fInitialY = y;
+
+        // make sure all initial subruns have the correct VM and X/Y applied
+        for (int i = 0; i < fRunCount; i++) {
+            fRuns[i].fSubRunInfo[0].init(fInitialViewMatrix, x, y);
+        }
     }
 
     /*
@@ -369,12 +372,15 @@
             SubRunInfo(const SubRunInfo& that)
                 : fBulkUseToken(that.fBulkUseToken)
                 , fStrike(SkSafeRef(that.fStrike.get()))
+                , fCurrentViewMatrix(that.fCurrentViewMatrix)
                 , fVertexBounds(that.fVertexBounds)
                 , fAtlasGeneration(that.fAtlasGeneration)
                 , fVertexStartIndex(that.fVertexStartIndex)
                 , fVertexEndIndex(that.fVertexEndIndex)
                 , fGlyphStartIndex(that.fGlyphStartIndex)
                 , fGlyphEndIndex(that.fGlyphEndIndex)
+                , fX(that.fX)
+                , fY(that.fY)
                 , fColor(that.fColor)
                 , fMaskFormat(that.fMaskFormat)
                 , fDrawAsDistanceFields(that.fDrawAsDistanceFields)
@@ -412,6 +418,9 @@
 
                 fVertexStartIndex = prev.vertexEndIndex();
                 fVertexEndIndex = prev.vertexEndIndex();
+
+                // copy over viewmatrix settings
+                this->init(prev.fCurrentViewMatrix, prev.fX, prev.fY);
             }
 
             const SkRect& vertexBounds() const { return fVertexBounds; }
@@ -419,6 +428,16 @@
                 fVertexBounds.joinNonEmptyArg(glyphBounds);
             }
 
+            void init(const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
+                fCurrentViewMatrix = viewMatrix;
+                fX = x;
+                fY = y;
+            }
+
+            // This function assumes the translation will be applied before it is called again
+            void computeTranslation(const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
+                                    SkScalar*transX, SkScalar* transY);
+
             // df properties
             void setUseLCDText(bool useLCDText) { fUseLCDText = useLCDText; }
             bool hasUseLCDText() const { return fUseLCDText; }
@@ -428,12 +447,15 @@
         private:
             GrBatchAtlas::BulkUseTokenUpdater fBulkUseToken;
             SkAutoTUnref<GrBatchTextStrike> fStrike;
+            SkMatrix fCurrentViewMatrix;
             SkRect fVertexBounds;
             uint64_t fAtlasGeneration;
             size_t fVertexStartIndex;
             size_t fVertexEndIndex;
             uint32_t fGlyphStartIndex;
             uint32_t fGlyphEndIndex;
+            SkScalar fX;
+            SkScalar fY;
             GrColor fColor;
             GrMaskFormat fMaskFormat;
             bool fDrawAsDistanceFields; // df property
@@ -475,7 +497,8 @@
 
     inline GrDrawBatch* createBatch(const Run::SubRunInfo& info,
                                     int glyphCount, int run, int subRun,
-                                    GrColor color, SkScalar transX, SkScalar transY,
+                                    const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
+                                    GrColor color,
                                     const SkPaint& skPaint, const SkSurfaceProps& props,
                                     const GrDistanceFieldAdjustTable* distanceAdjustTable,
                                     GrBatchFontCache* cache);
@@ -483,14 +506,14 @@
     struct BigGlyph {
         BigGlyph(const SkPath& path, SkScalar vx, SkScalar vy, SkScalar scale, bool applyVM)
             : fPath(path)
-            , fVx(vx)
-            , fVy(vy)
             , fScale(scale)
+            , fX(vx)
+            , fY(vy)
             , fApplyVM(applyVM) {}
         SkPath fPath;
-        SkScalar fVx;
-        SkScalar fVy;
         SkScalar fScale;
+        SkScalar fX;
+        SkScalar fY;
         bool fApplyVM;
     };
 
@@ -514,14 +537,12 @@
     StrokeInfo fStrokeInfo;
     SkTArray<BigGlyph> fBigGlyphs;
     Key fKey;
-    SkMatrix fViewMatrix;
+    SkMatrix fInitialViewMatrix;
     SkMatrix fInitialViewMatrixInverse;
     size_t fSize;
     GrColor fPaintColor;
     SkScalar fInitialX;
     SkScalar fInitialY;
-    SkScalar fX;
-    SkScalar fY;
 
     // We can reuse distance field text, but only if the new viewmatrix would not result in
     // a mip change.  Because there can be multiple runs in a blob, we track the overall
diff --git a/src/gpu/text/GrAtlasTextBlob_regenInBatch.cpp b/src/gpu/text/GrAtlasTextBlob_regenInBatch.cpp
index 14bf4a5..00a7364 100644
--- a/src/gpu/text/GrAtlasTextBlob_regenInBatch.cpp
+++ b/src/gpu/text/GrAtlasTextBlob_regenInBatch.cpp
@@ -235,14 +235,18 @@
                                    int runIndex, int subRunIndex, SkGlyphCache** cache,
                                    SkTypeface** typeface, GrFontScaler** scaler,
                                    const SkDescriptor** desc, size_t vertexStride,
-                                   GrColor color, SkScalar transX,
-                                   SkScalar transY,
+                                   const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
+                                   GrColor color,
                                    void** vertices, size_t* byteCount, int* glyphCount) {
     Run& run = fRuns[runIndex];
     Run::SubRunInfo& info = run.fSubRunInfo[subRunIndex];
 
     uint64_t currentAtlasGen = fontCache->atlasGeneration(info.maskFormat());
 
+    // Compute translation if any
+    SkScalar transX, transY;
+    info.computeTranslation(viewMatrix, x, y, &transX, &transY);
+
     // Because the GrBatchFontCache may evict the strike a blob depends on using for
     // generating its texture coords, we have to track whether or not the strike has
     // been abandoned.  If it hasn't been abandoned, then we can use the GrGlyph*s as is
diff --git a/src/gpu/text/GrAtlasTextContext.cpp b/src/gpu/text/GrAtlasTextContext.cpp
index 0f36fe3..1934a8d 100644
--- a/src/gpu/text/GrAtlasTextContext.cpp
+++ b/src/gpu/text/GrAtlasTextContext.cpp
@@ -109,9 +109,6 @@
         cacheBlob.reset(SkSafeRef(cache->find(key)));
     }
 
-    SkScalar transX = 0.f;
-    SkScalar transY = 0.f;
-
     // Though for the time being runs in the textblob can override the paint, they only touch font
     // info.
     GrPaint grPaint;
@@ -120,8 +117,7 @@
     }
 
     if (cacheBlob) {
-        if (cacheBlob->mustRegenerate(&transX, &transY, skPaint, grPaint.getColor(), blurRec,
-                                      viewMatrix, x, y)) {
+        if (cacheBlob->mustRegenerate(skPaint, grPaint.getColor(), blurRec, viewMatrix, x, y)) {
             // We have to remake the blob because changes may invalidate our masks.
             // TODO we could probably get away reuse most of the time if the pointer is unique,
             // but we'd have to clear the subrun information
@@ -160,7 +156,7 @@
     }
 
     cacheBlob->flushCached(context, dc, blob, props, fDistanceAdjustTable, skPaint,
-                           grPaint, drawFilter, clip, viewMatrix, clipBounds, x, y, transX, transY);
+                           grPaint, drawFilter, clip, viewMatrix, clipBounds, x, y);
 }
 
 void GrAtlasTextContext::RegenerateTextBlob(GrAtlasTextBlob* cacheBlob,
@@ -321,7 +317,7 @@
                                viewMatrix, props,
                                text, byteLength, x, y));
         blob->flushThrowaway(context, dc, props, fDistanceAdjustTable, skPaint, paint,
-                             clip, regionClipBounds);
+                             clip, viewMatrix, regionClipBounds, x, y);
         return;
     }
 
@@ -351,7 +347,7 @@
                                   pos, scalarsPerPosition,
                                   offset));
         blob->flushThrowaway(context, dc, props, fDistanceAdjustTable, skPaint, paint,
-                             clip, regionClipBounds);
+                             clip, viewMatrix, regionClipBounds, offset.fX, offset.fY);
         return;
     }
 
@@ -408,9 +404,9 @@
 
     // We'd like to be able to test this with random translations, but currently the vertex
     // bounds and vertices will get out of sync
-    SkScalar transX = 0.f;//SkIntToScalar(random->nextU());
-    SkScalar transY = 0.f;//SkIntToScalar(random->nextU());
-    return blob->test_createBatch(textLen, 0, 0, color, transX, transY, skPaint,
+    SkScalar x = 0.f;//SkIntToScalar(random->nextU());
+    SkScalar y = 0.f;//SkIntToScalar(random->nextU());
+    return blob->test_createBatch(textLen, 0, 0, viewMatrix, x, y, color, skPaint,
                                   gSurfaceProps, gTextContext->dfAdjustTable(),
                                   context->getBatchFontCache());
 }