Make GrSWMaskHelper take a matrix for each draw

Change-Id: I52659857174848696f360d64552a9690db24ed50
Reviewed-on: https://skia-review.googlesource.com/40883
Commit-Queue: Brian Salomon <bsalomon@google.com>
Reviewed-by: Brian Osman <brianosman@google.com>
diff --git a/src/gpu/GrClipStackClip.cpp b/src/gpu/GrClipStackClip.cpp
index 5e366a7..ef71941 100644
--- a/src/gpu/GrClipStackClip.cpp
+++ b/src/gpu/GrClipStackClip.cpp
@@ -444,7 +444,7 @@
     SkMatrix translate;
     translate.setTranslate(SkIntToScalar(-reducedClip.left()), SkIntToScalar(-reducedClip.top()));
 
-    if (!helper.init(maskSpaceIBounds, &translate)) {
+    if (!helper.init(maskSpaceIBounds)) {
         return nullptr;
     }
     helper.clear(InitialState::kAllIn == reducedClip.initialState() ? 0xFF : 0x00);
@@ -462,25 +462,25 @@
             if (kReverseDifference_SkClipOp == op) {
                 SkRect temp = SkRect::Make(reducedClip.ibounds());
                 // invert the entire scene
-                helper.drawRect(temp, SkRegion::kXOR_Op, GrAA::kNo, 0xFF);
+                helper.drawRect(temp, translate, SkRegion::kXOR_Op, GrAA::kNo, 0xFF);
             }
             SkPath clipPath;
             element->asDeviceSpacePath(&clipPath);
             clipPath.toggleInverseFillType();
             GrShape shape(clipPath, GrStyle::SimpleFill());
-            helper.drawShape(shape, SkRegion::kReplace_Op, aa, 0x00);
+            helper.drawShape(shape, translate, SkRegion::kReplace_Op, aa, 0x00);
             continue;
         }
 
         // The other ops (union, xor, diff) only affect pixels inside
         // the geometry so they can just be drawn normally
         if (Element::DeviceSpaceType::kRect == element->getDeviceSpaceType()) {
-            helper.drawRect(element->getDeviceSpaceRect(), (SkRegion::Op)op, aa, 0xFF);
+            helper.drawRect(element->getDeviceSpaceRect(), translate, (SkRegion::Op)op, aa, 0xFF);
         } else {
             SkPath path;
             element->asDeviceSpacePath(&path);
             GrShape shape(path, GrStyle::SimpleFill());
-            helper.drawShape(shape, (SkRegion::Op)op, aa, 0xFF);
+            helper.drawShape(shape, translate, (SkRegion::Op)op, aa, 0xFF);
         }
     }
 
diff --git a/src/gpu/GrSWMaskHelper.cpp b/src/gpu/GrSWMaskHelper.cpp
index af0d1bd..5d0c375 100644
--- a/src/gpu/GrSWMaskHelper.cpp
+++ b/src/gpu/GrSWMaskHelper.cpp
@@ -33,25 +33,35 @@
 /**
  * Draw a single rect element of the clip stack into the accumulation bitmap
  */
-void GrSWMaskHelper::drawRect(const SkRect& rect, SkRegion::Op op, GrAA aa, uint8_t alpha) {
+void GrSWMaskHelper::drawRect(const SkRect& rect, const SkMatrix& matrix, SkRegion::Op op, GrAA aa,
+                              uint8_t alpha) {
     SkPaint paint;
-
     paint.setBlendMode(op_to_mode(op));
     paint.setAntiAlias(GrAA::kYes == aa);
     paint.setColor(SkColorSetARGB(alpha, alpha, alpha, alpha));
 
+    SkMatrix translatedMatrix = matrix;
+    translatedMatrix.postTranslate(fTranslate.fX, fTranslate.fY);
+    fDraw.fMatrix = &translatedMatrix;
+
     fDraw.drawRect(rect, paint);
+    SkDEBUGCODE(fDraw.fMatrix = (SkMatrix*)0xbbbbbbbb);
 }
 
 /**
  * Draw a single path element of the clip stack into the accumulation bitmap
  */
-void GrSWMaskHelper::drawShape(const GrShape& shape, SkRegion::Op op, GrAA aa, uint8_t alpha) {
+void GrSWMaskHelper::drawShape(const GrShape& shape, const SkMatrix& matrix, SkRegion::Op op,
+                               GrAA aa, uint8_t alpha) {
     SkPaint paint;
     paint.setPathEffect(shape.style().refPathEffect());
     shape.style().strokeRec().applyToPaint(&paint);
     paint.setAntiAlias(GrAA::kYes == aa);
 
+    SkMatrix translatedMatrix = matrix;
+    translatedMatrix.postTranslate(fTranslate.fX, fTranslate.fY);
+    fDraw.fMatrix = &translatedMatrix;
+
     SkPath path;
     shape.asPath(&path);
     if (SkRegion::kReplace_Op == op && 0xFF == alpha) {
@@ -62,17 +72,12 @@
         paint.setColor(SkColorSetARGB(alpha, alpha, alpha, alpha));
         fDraw.drawPath(path, paint);
     }
-}
+    SkDEBUGCODE(fDraw.fMatrix = (SkMatrix*)0xbbbbbbbb);
+};
 
-bool GrSWMaskHelper::init(const SkIRect& resultBounds, const SkMatrix* matrix) {
-    if (matrix) {
-        fMatrix = *matrix;
-    } else {
-        fMatrix.setIdentity();
-    }
-
-    // Now translate so the bound's UL corner is at the origin
-    fMatrix.postTranslate(-SkIntToScalar(resultBounds.fLeft), -SkIntToScalar(resultBounds.fTop));
+bool GrSWMaskHelper::init(const SkIRect& resultBounds) {
+    // We will need to translate draws so the bound's UL corner is at the origin
+    fTranslate = {-SkIntToScalar(resultBounds.fLeft), -SkIntToScalar(resultBounds.fTop)};
     SkIRect bounds = SkIRect::MakeWH(resultBounds.width(), resultBounds.height());
 
     const SkImageInfo bmImageInfo = SkImageInfo::MakeA8(bounds.width(), bounds.height());
@@ -85,7 +90,8 @@
     fDraw.fDst      = *fPixels;
     fRasterClip.setRect(bounds);
     fDraw.fRC       = &fRasterClip;
-    fDraw.fMatrix   = &fMatrix;
+    // Each draw must specify the matrix.
+    SkDEBUGCODE(fDraw.fMatrix = (SkMatrix*)0xbbbbbbbb);
     return true;
 }
 
diff --git a/src/gpu/GrSWMaskHelper.h b/src/gpu/GrSWMaskHelper.h
index 4e1363c..6590143 100644
--- a/src/gpu/GrSWMaskHelper.h
+++ b/src/gpu/GrSWMaskHelper.h
@@ -41,13 +41,13 @@
     // may be accumulated in the helper during creation, "resultBounds"
     // allows the caller to specify the region of interest - to limit the
     // amount of work.
-    bool init(const SkIRect& resultBounds, const SkMatrix* matrix);
+    bool init(const SkIRect& resultBounds);
 
     // Draw a single rect into the accumulation bitmap using the specified op
-    void drawRect(const SkRect& rect, SkRegion::Op op, GrAA, uint8_t alpha);
+    void drawRect(const SkRect& rect, const SkMatrix& matrix, SkRegion::Op op, GrAA, uint8_t alpha);
 
     // Draw a single path into the accumuation bitmap using the specified op
-    void drawShape(const GrShape&, SkRegion::Op op, GrAA, uint8_t alpha);
+    void drawShape(const GrShape&, const SkMatrix& matrix, SkRegion::Op op, GrAA, uint8_t alpha);
 
     sk_sp<GrTextureProxy> toTextureProxy(GrContext*, SkBackingFit fit);
 
@@ -57,7 +57,7 @@
     }
 
 private:
-    SkMatrix             fMatrix;
+    SkVector             fTranslate;
     SkAutoPixmapStorage* fPixels;
     SkAutoPixmapStorage  fPixelsStorage;
     SkDraw               fDraw;
diff --git a/src/gpu/GrSoftwarePathRenderer.cpp b/src/gpu/GrSoftwarePathRenderer.cpp
index e76af89..1363e1a 100644
--- a/src/gpu/GrSoftwarePathRenderer.cpp
+++ b/src/gpu/GrSoftwarePathRenderer.cpp
@@ -342,9 +342,9 @@
             auto drawAndUploadMask = [uploaderRaw] {
                 TRACE_EVENT0("skia", "Threaded SW Mask Render");
                 GrSWMaskHelper helper(uploaderRaw->getPixels());
-                if (helper.init(uploaderRaw->getMaskBounds(), uploaderRaw->getViewMatrix())) {
-                    helper.drawShape(uploaderRaw->getShape(), SkRegion::kReplace_Op,
-                                     uploaderRaw->getAA(), 0xFF);
+                if (helper.init(uploaderRaw->getMaskBounds())) {
+                    helper.drawShape(uploaderRaw->getShape(), *uploaderRaw->getViewMatrix(),
+                                     SkRegion::kReplace_Op, uploaderRaw->getAA(), 0xFF);
                 } else {
                     SkDEBUGFAIL("Unable to allocate SW mask.");
                 }
@@ -354,10 +354,10 @@
             args.fRenderTargetContext->getOpList()->addPrepareCallback(std::move(uploader));
         } else {
             GrSWMaskHelper helper;
-            if (!helper.init(*boundsForMask, args.fViewMatrix)) {
+            if (!helper.init(*boundsForMask)) {
                 return false;
             }
-            helper.drawShape(*args.fShape, SkRegion::kReplace_Op, aa, 0xFF);
+            helper.drawShape(*args.fShape, *args.fViewMatrix, SkRegion::kReplace_Op, aa, 0xFF);
             proxy = helper.toTextureProxy(args.fContext, fit);
         }