make matrix serialization private

Moves readFromMemory, writeToMemory to private section.
No sign that these are called from google3, android,
chromium, but function names are common enough that
it's hard to know for sure.

These are used inside templates internally and for
testing, so it is not quite as simple as adding alternate
entry points in SkMatrixPriv.

R=reed@google.com
Bug: skia:6898
Change-Id: I1fac142f4bf0f38608ea93438c46f39147606c4d
Reviewed-on: https://skia-review.googlesource.com/62361
Reviewed-by: Mike Reed <reed@google.com>
Commit-Queue: Cary Clark <caryclark@google.com>
diff --git a/include/core/SkMatrix.h b/include/core/SkMatrix.h
index e22099c..49be97a 100644
--- a/include/core/SkMatrix.h
+++ b/include/core/SkMatrix.h
@@ -601,22 +601,6 @@
         return !(a == b);
     }
 
-    enum {
-        // writeTo/readFromMemory will never return a value larger than this
-        kMaxFlattenSize = 9 * sizeof(SkScalar) + sizeof(uint32_t),
-    };
-    // return the number of bytes written, whether or not buffer is null
-    size_t writeToMemory(void* buffer) const;
-    /**
-     * Reads data from the buffer parameter
-     *
-     * @param buffer Memory to read from
-     * @param length Amount of memory available in the buffer
-     * @return number of bytes read (must be a multiple of 4) or
-     *         0 if there was not enough memory available
-     */
-    size_t readFromMemory(const void* buffer, size_t length);
-
     void dump() const;
     void toString(SkString* str) const;
 
@@ -850,8 +834,22 @@
 
     static const MapPtsProc gMapPtsProcs[];
 
+    // return the number of bytes written, whether or not buffer is null
+    size_t writeToMemory(void* buffer) const;
+    /**
+     * Reads data from the buffer parameter
+     *
+     * @param buffer Memory to read from
+     * @param length Amount of memory available in the buffer
+     * @return number of bytes read (must be a multiple of 4) or
+     *         0 if there was not enough memory available
+     */
+    size_t readFromMemory(const void* buffer, size_t length);
+
     friend class SkPerspIter;
     friend class SkMatrixPriv;
+    friend class SkReader32;
+    friend class SerializationTest;
 };
 SK_END_REQUIRE_DENSE
 
diff --git a/include/core/SkWriter32.h b/include/core/SkWriter32.h
index beaaee1..e81178d 100644
--- a/include/core/SkWriter32.h
+++ b/include/core/SkWriter32.h
@@ -141,11 +141,7 @@
         path.writeToMemory(this->reserve(size));
     }
 
-    void writeMatrix(const SkMatrix& matrix) {
-        size_t size = matrix.writeToMemory(nullptr);
-        SkASSERT(SkAlign4(size) == size);
-        matrix.writeToMemory(this->reserve(size));
-    }
+    void writeMatrix(const SkMatrix& matrix);
 
     void writeRegion(const SkRegion& rgn) {
         size_t size = rgn.writeToMemory(nullptr);
diff --git a/src/core/SkMatrixPriv.h b/src/core/SkMatrixPriv.h
index b0df2ec..ed1a5f1 100644
--- a/src/core/SkMatrixPriv.h
+++ b/src/core/SkMatrixPriv.h
@@ -13,9 +13,23 @@
 
 class SkMatrixPriv {
 public:
+    enum {
+        // writeTo/readFromMemory will never return a value larger than this
+        kMaxFlattenSize = 9 * sizeof(SkScalar) + sizeof(uint32_t),
+    };
+
+    static size_t WriteToMemory(const SkMatrix& matrix, void* buffer) {
+        return matrix.writeToMemory(buffer);
+    }
+
+    static size_t ReadFromMemory(SkMatrix* matrix, const void* buffer, size_t length) {
+        return matrix->readFromMemory(buffer, length);
+    }
+
     typedef SkMatrix::MapXYProc MapXYProc;
     typedef SkMatrix::MapPtsProc MapPtsProc;
 
+
     static MapPtsProc GetMapPtsProc(const SkMatrix& matrix) {
         return SkMatrix::GetMapPtsProc(matrix.getType());
     }
diff --git a/src/core/SkPictureRecord.cpp b/src/core/SkPictureRecord.cpp
index 2ebe7bb..3700e3b 100644
--- a/src/core/SkPictureRecord.cpp
+++ b/src/core/SkPictureRecord.cpp
@@ -8,6 +8,7 @@
 #include "SkPictureRecord.h"
 #include "SkDrawShadowInfo.h"
 #include "SkImage_Base.h"
+#include "SkMatrixPriv.h"
 #include "SkPatchUtils.h"
 #include "SkPixelRef.h"
 #include "SkRRect.h"
@@ -111,7 +112,7 @@
     }
     if (rec.fClipMatrix) {
         flatFlags |= SAVELAYERREC_HAS_CLIPMATRIX;
-        size += rec.fClipMatrix->writeToMemory(nullptr);
+        size += SkMatrixPriv::WriteToMemory(*rec.fClipMatrix, nullptr);
     }
 
     const size_t initialOffset = this->addDraw(SAVE_LAYER_SAVELAYERREC, &size);
@@ -225,7 +226,7 @@
 void SkPictureRecord::recordConcat(const SkMatrix& matrix) {
     this->validate(fWriter.bytesWritten(), 0);
     // op + matrix
-    size_t size = kUInt32Size + matrix.writeToMemory(nullptr);
+    size_t size = kUInt32Size + SkMatrixPriv::WriteToMemory(matrix, nullptr);
     size_t initialOffset = this->addDraw(CONCAT, &size);
     this->addMatrix(matrix);
     this->validate(initialOffset, size);
@@ -234,7 +235,7 @@
 void SkPictureRecord::didSetMatrix(const SkMatrix& matrix) {
     this->validate(fWriter.bytesWritten(), 0);
     // op + matrix
-    size_t size = kUInt32Size + matrix.writeToMemory(nullptr);
+    size_t size = kUInt32Size + SkMatrixPriv::WriteToMemory(matrix, nullptr);
     size_t initialOffset = this->addDraw(SET_MATRIX, &size);
     this->addMatrix(matrix);
     this->validate(initialOffset, size);
@@ -623,7 +624,8 @@
                                        const SkMatrix* matrix, const SkPaint& paint) {
     // op + paint index + length + 'length' worth of data + path index + matrix
     const SkMatrix& m = matrix ? *matrix : SkMatrix::I();
-    size_t size = 3 * kUInt32Size + SkAlign4(byteLength) + kUInt32Size + m.writeToMemory(nullptr);
+    size_t size = 3 * kUInt32Size + SkAlign4(byteLength) + kUInt32Size +
+        SkMatrixPriv::WriteToMemory(m, nullptr);
     size_t initialOffset = this->addDraw(DRAW_TEXT_ON_PATH, &size);
     this->addPaint(paint);
     this->addText(text, byteLength);
@@ -682,7 +684,7 @@
         this->addPicture(picture);
     } else {
         const SkMatrix& m = matrix ? *matrix : SkMatrix::I();
-        size += m.writeToMemory(nullptr) + kUInt32Size;    // matrix + paint
+        size += SkMatrixPriv::WriteToMemory(m, nullptr) + kUInt32Size;    // matrix + paint
         initialOffset = this->addDraw(DRAW_PICTURE_MATRIX_PAINT, &size);
         this->addPaintPtr(paint);
         this->addMatrix(m);
@@ -700,7 +702,7 @@
         initialOffset = this->addDraw(DRAW_DRAWABLE, &size);
         this->addDrawable(drawable);
     } else {
-        size += matrix->writeToMemory(nullptr);    // matrix
+        size += SkMatrixPriv::WriteToMemory(*matrix, nullptr);    // matrix
         initialOffset = this->addDraw(DRAW_DRAWABLE_MATRIX, &size);
         this->addMatrix(*matrix);
         this->addDrawable(drawable);
diff --git a/src/core/SkValidatingReadBuffer.cpp b/src/core/SkValidatingReadBuffer.cpp
index 2da08c6..e13cde5 100644
--- a/src/core/SkValidatingReadBuffer.cpp
+++ b/src/core/SkValidatingReadBuffer.cpp
@@ -6,6 +6,7 @@
  */
 
 #include "SkBitmap.h"
+#include "SkMatrixPriv.h"
 #include "SkValidatingReadBuffer.h"
 #include "SkStream.h"
 #include "SkTypeface.h"
@@ -135,7 +136,7 @@
 void SkValidatingReadBuffer::readMatrix(SkMatrix* matrix) {
     size_t size = 0;
     if (!fError) {
-        size = matrix->readFromMemory(fReader.peek(), fReader.available());
+        size = SkMatrixPriv::ReadFromMemory(matrix, fReader.peek(), fReader.available());
         this->validate((SkAlign4(size) == size) && (0 != size));
     }
     if (!fError) {
diff --git a/src/core/SkWriter32.cpp b/src/core/SkWriter32.cpp
index d328644..3261aad 100644
--- a/src/core/SkWriter32.cpp
+++ b/src/core/SkWriter32.cpp
@@ -5,10 +5,17 @@
  * found in the LICENSE file.
  */
 
+#include "SkMatrixPriv.h"
 #include "SkReader32.h"
 #include "SkString.h"
 #include "SkWriter32.h"
 
+void SkWriter32::writeMatrix(const SkMatrix& matrix) {
+    size_t size = SkMatrixPriv::WriteToMemory(matrix, nullptr);
+    SkASSERT(SkAlign4(size) == size);
+    SkMatrixPriv::WriteToMemory(matrix, this->reserve(size));
+}
+
 /*
  *  Strings are stored as: length[4-bytes] + string_data + '\0' + pad_to_mul_4
  */
diff --git a/tests/MatrixTest.cpp b/tests/MatrixTest.cpp
index 578e8b0..18d4908 100644
--- a/tests/MatrixTest.cpp
+++ b/tests/MatrixTest.cpp
@@ -6,7 +6,7 @@
  */
 
 #include "SkMath.h"
-#include "SkMatrix.h"
+#include "SkMatrixPriv.h"
 #include "SkMatrixUtils.h"
 #include "SkPoint3.h"
 #include "SkRandom.h"
@@ -141,20 +141,20 @@
 
 static void test_flatten(skiatest::Reporter* reporter, const SkMatrix& m) {
     // add 100 in case we have a bug, I don't want to kill my stack in the test
-    static const size_t kBufferSize = SkMatrix::kMaxFlattenSize + 100;
+    static const size_t kBufferSize = SkMatrixPriv::kMaxFlattenSize + 100;
     char buffer[kBufferSize];
-    size_t size1 = m.writeToMemory(nullptr);
-    size_t size2 = m.writeToMemory(buffer);
+    size_t size1 = SkMatrixPriv::WriteToMemory(m, nullptr);
+    size_t size2 = SkMatrixPriv::WriteToMemory(m, buffer);
     REPORTER_ASSERT(reporter, size1 == size2);
-    REPORTER_ASSERT(reporter, size1 <= SkMatrix::kMaxFlattenSize);
+    REPORTER_ASSERT(reporter, size1 <= SkMatrixPriv::kMaxFlattenSize);
 
     SkMatrix m2;
-    size_t size3 = m2.readFromMemory(buffer, kBufferSize);
+    size_t size3 = SkMatrixPriv::ReadFromMemory(&m2, buffer, kBufferSize);
     REPORTER_ASSERT(reporter, size1 == size3);
     REPORTER_ASSERT(reporter, are_equal(reporter, m, m2));
 
     char buffer2[kBufferSize];
-    size3 = m2.writeToMemory(buffer2);
+    size3 = SkMatrixPriv::WriteToMemory(m2, buffer2);
     REPORTER_ASSERT(reporter, size1 == size3);
     REPORTER_ASSERT(reporter, memcmp(buffer, buffer2, size1) == 0);
 }
diff --git a/tests/SerializationTest.cpp b/tests/SerializationTest.cpp
index 898b12f..da3c47c 100644
--- a/tests/SerializationTest.cpp
+++ b/tests/SerializationTest.cpp
@@ -14,6 +14,7 @@
 #include "SkImageSource.h"
 #include "SkMakeUnique.h"
 #include "SkMallocPixelRef.h"
+#include "SkMatrixPriv.h"
 #include "SkOSFile.h"
 #include "SkPictureRecorder.h"
 #include "SkShaderBase.h"
@@ -29,6 +30,9 @@
 static const uint32_t kArraySize = 64;
 static const int kBitmapSize = 256;
 
+class SerializationTest {
+public:
+
 template<typename T>
 static void TestAlignment(T* testObj, skiatest::Reporter* reporter) {
     // Test memory read/write functions directly
@@ -38,6 +42,7 @@
     size_t bytesReadFromMemory = testObj->readFromMemory(dataWritten, bytesWrittenToMemory);
     REPORTER_ASSERT(reporter, SkAlign4(bytesReadFromMemory) == bytesReadFromMemory);
 }
+};
 
 template<typename T> struct SerializationUtils {
     // Generic case for flattenables
@@ -183,7 +188,7 @@
 template<typename T>
 static void TestObjectSerialization(T* testObj, skiatest::Reporter* reporter) {
     TestObjectSerializationNoAlign<T, false>(testObj, reporter);
-    TestAlignment(testObj, reporter);
+    SerializationTest::TestAlignment(testObj, reporter);
 }
 
 template<typename T>
@@ -467,7 +472,7 @@
         SkRect rect = SkRect::MakeXYWH(1, 2, 20, 30);
         SkVector corners[4] = { {1, 2}, {2, 3}, {3,4}, {4,5} };
         rrect.setRectRadii(rect, corners);
-        TestAlignment(&rrect, reporter);
+        SerializationTest::TestAlignment(&rrect, reporter);
     }
 
     // Test readByteArray