Preserve presence/absence of optional matrices in CanvasKit

With the change to stop eliding Identity local matrices in:
 https://skia-review.googlesource.com/c/skia/+/842376
It becomes important to preserve the presence or absence of the local matrices in CanvasKit.

Change-Id: Ia445e8a63653b64b02ee269b784161846848596c
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/845996
Reviewed-by: Kaylee Lubick <kjlubick@google.com>
Commit-Queue: Robert Phillips <robertphillips@google.com>
diff --git a/modules/canvaskit/canvaskit_bindings.cpp b/modules/canvaskit/canvaskit_bindings.cpp
index fede659..95fc8fe 100644
--- a/modules/canvaskit/canvaskit_bindings.cpp
+++ b/modules/canvaskit/canvaskit_bindings.cpp
@@ -1688,13 +1688,16 @@
                                  SkTileMode tx, SkTileMode ty,
                                  float B, float C, // See SkSamplingOptions.h for docs.
                                  WASMPointerF32 mPtr)->sk_sp<SkShader> {
-            return self->makeShader(tx, ty, SkSamplingOptions({B, C}), OptionalMatrix(mPtr));
+            OptionalMatrix localMatrix(mPtr);
+            return self->makeShader(tx, ty, SkSamplingOptions({B, C}), mPtr ? &localMatrix
+                                                                            : nullptr);
         }), allow_raw_pointers())
         .function("_makeShaderOptions", optional_override([](sk_sp<SkImage> self,
                                  SkTileMode tx, SkTileMode ty,
                                  SkFilterMode filter, SkMipmapMode mipmap,
                                  WASMPointerF32 mPtr)->sk_sp<SkShader> {
-            return self->makeShader(tx, ty, {filter, mipmap}, OptionalMatrix(mPtr));
+            OptionalMatrix localMatrix(mPtr);
+            return self->makeShader(tx, ty, {filter, mipmap}, mPtr ? &localMatrix : nullptr);
         }), allow_raw_pointers())
 #if defined(ENABLE_GPU)
         .function("_readPixels", optional_override([](sk_sp<SkImage> self,
@@ -2038,7 +2041,7 @@
                                  WASMPointerF32 mPtr, WASMPointerF32 rPtr) -> sk_sp<SkShader> {
             OptionalMatrix localMatrix(mPtr);
             SkRect* tileRect = reinterpret_cast<SkRect*>(rPtr);
-            return self.makeShader(tmx, tmy, mode, &localMatrix, tileRect);
+            return self.makeShader(tmx, tmy, mode, mPtr ? &localMatrix : nullptr, tileRect);
         }), allow_raw_pointers())
         .function("_cullRect", optional_override([](SkPicture& self,
                                                      WASMPointerF32 fPtr)->void {
@@ -2104,11 +2107,13 @@
              if (colorType == SkColorType::kRGBA_F32_SkColorType) {
                  const SkColor4f* colors  = reinterpret_cast<const SkColor4f*>(cPtr);
                  return SkGradientShader::MakeLinear(points, colors, colorSpace, positions, count,
-                                                     mode, flags, &localMatrix);
+                                                     mode, flags,
+                                                     mPtr ? &localMatrix : nullptr);
              } else if (colorType == SkColorType::kRGBA_8888_SkColorType) {
                  const SkColor* colors  = reinterpret_cast<const SkColor*>(cPtr);
                  return SkGradientShader::MakeLinear(points, colors, positions, count,
-                                                     mode, flags, &localMatrix);
+                                                     mode, flags,
+                                                     mPtr ? &localMatrix : nullptr);
              }
              SkDebugf("%d is not an accepted colorType\n", colorType);
              return nullptr;
@@ -2125,11 +2130,13 @@
             if (colorType == SkColorType::kRGBA_F32_SkColorType) {
                const SkColor4f* colors  = reinterpret_cast<const SkColor4f*>(cPtr);
                return SkGradientShader::MakeRadial({cx, cy}, radius, colors, colorSpace,
-                                                   positions, count, mode, flags, &localMatrix);
+                                                   positions, count, mode, flags,
+                                                   mPtr ? &localMatrix : nullptr);
             } else if (colorType == SkColorType::kRGBA_8888_SkColorType) {
                const SkColor* colors  = reinterpret_cast<const SkColor*>(cPtr);
                return SkGradientShader::MakeRadial({cx, cy}, radius, colors, positions,
-                                                   count, mode, flags, &localMatrix);
+                                                   count, mode, flags,
+                                                   mPtr ? &localMatrix : nullptr);
             }
             SkDebugf("%d is not an accepted colorType\n", colorType);
             return nullptr;
@@ -2148,12 +2155,12 @@
                const SkColor4f* colors  = reinterpret_cast<const SkColor4f*>(cPtr);
                return SkGradientShader::MakeSweep(cx, cy, colors, colorSpace, positions, count,
                                                   mode, startAngle, endAngle, flags,
-                                                  &localMatrix);
+                                                  mPtr ? &localMatrix : nullptr);
             } else if (colorType == SkColorType::kRGBA_8888_SkColorType) {
                const SkColor* colors  = reinterpret_cast<const SkColor*>(cPtr);
                return SkGradientShader::MakeSweep(cx, cy, colors, positions, count,
                                                   mode, startAngle, endAngle, flags,
-                                                  &localMatrix);
+                                                  mPtr ? &localMatrix : nullptr);
             }
             SkDebugf("%d is not an accepted colorType\n", colorType);
             return nullptr;
@@ -2183,7 +2190,8 @@
                return SkGradientShader::MakeTwoPointConical(startAndEnd[0], startRadius,
                                                             startAndEnd[1], endRadius,
                                                             colors, colorSpace, positions, count, mode,
-                                                            flags, &localMatrix);
+                                                            flags,
+                                                            mPtr ? &localMatrix : nullptr);
             } else if (colorType == SkColorType::kRGBA_8888_SkColorType) {
                 const SkColor* colors = reinterpret_cast<const SkColor*>(cPtr);
                 return SkGradientShader::MakeTwoPointConical(startAndEnd[0],
@@ -2195,7 +2203,7 @@
                                                              count,
                                                              mode,
                                                              flags,
-                                                             &localMatrix);
+                                                             mPtr ? &localMatrix : nullptr);
             }
             SkDebugf("%d is not an accepted colorType\n", colorType);
             return nullptr;
@@ -2260,7 +2268,7 @@
             }
 
             OptionalMatrix localMatrix(mPtr);
-            return self.makeShader(uniforms, nullptr, 0, &localMatrix);
+            return self.makeShader(uniforms, nullptr, 0, mPtr ? &localMatrix : nullptr);
         }))
         .function("_makeShaderWithChildren", optional_override([](SkRuntimeEffect& self,
                                                                   WASMPointerF32 fPtr,
@@ -2286,7 +2294,7 @@
                 children[i] = sk_ref_sp<SkShader>(childrenPtrs[i]);
             }
             OptionalMatrix localMatrix(mPtr);
-            auto s = self.makeShader(uniforms, children, cLen, &localMatrix);
+            auto s = self.makeShader(uniforms, children, cLen, mPtr ? &localMatrix : nullptr);
             delete[] children;
             return s;
         }))