commit-bot@chromium.org | 0251288 | 2013-10-31 18:37:50 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkCanvas.h" |
Ben Wagner | e80a595 | 2020-07-14 17:57:13 -0400 | [diff] [blame] | 9 | #include "include/core/SkFontMgr.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkImage.h" |
| 11 | #include "include/core/SkMallocPixelRef.h" |
| 12 | #include "include/core/SkPictureRecorder.h" |
| 13 | #include "include/core/SkTextBlob.h" |
| 14 | #include "include/core/SkTypeface.h" |
| 15 | #include "include/effects/SkDashPathEffect.h" |
Michael Ludwig | 55edb50 | 2019-08-05 10:41:10 -0400 | [diff] [blame] | 16 | #include "include/effects/SkImageFilters.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "include/effects/SkTableColorFilter.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 18 | #include "include/private/SkFixed.h" |
| 19 | #include "include/private/SkTemplates.h" |
| 20 | #include "src/core/SkAnnotationKeys.h" |
Brian Osman | 9e4e4c7 | 2020-06-10 07:19:34 -0400 | [diff] [blame] | 21 | #include "src/core/SkAutoMalloc.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 22 | #include "src/core/SkMatrixPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 23 | #include "src/core/SkOSFile.h" |
| 24 | #include "src/core/SkPicturePriv.h" |
| 25 | #include "src/core/SkReadBuffer.h" |
| 26 | #include "src/core/SkWriteBuffer.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 27 | #include "src/shaders/SkShaderBase.h" |
| 28 | #include "tests/Test.h" |
| 29 | #include "tools/Resources.h" |
| 30 | #include "tools/ToolUtils.h" |
commit-bot@chromium.org | 0251288 | 2013-10-31 18:37:50 +0000 | [diff] [blame] | 31 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 32 | static const uint32_t kArraySize = 64; |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 33 | static const int kBitmapSize = 256; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 34 | |
Cary Clark | 6d6d603 | 2017-10-20 12:14:33 -0400 | [diff] [blame] | 35 | class SerializationTest { |
| 36 | public: |
| 37 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 38 | template<typename T> |
| 39 | static void TestAlignment(T* testObj, skiatest::Reporter* reporter) { |
| 40 | // Test memory read/write functions directly |
| 41 | unsigned char dataWritten[1024]; |
| 42 | size_t bytesWrittenToMemory = testObj->writeToMemory(dataWritten); |
| 43 | REPORTER_ASSERT(reporter, SkAlign4(bytesWrittenToMemory) == bytesWrittenToMemory); |
| 44 | size_t bytesReadFromMemory = testObj->readFromMemory(dataWritten, bytesWrittenToMemory); |
| 45 | REPORTER_ASSERT(reporter, SkAlign4(bytesReadFromMemory) == bytesReadFromMemory); |
| 46 | } |
Cary Clark | 6d6d603 | 2017-10-20 12:14:33 -0400 | [diff] [blame] | 47 | }; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 48 | |
| 49 | template<typename T> struct SerializationUtils { |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 50 | // Generic case for flattenables |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 51 | static void Write(SkWriteBuffer& writer, const T* flattenable) { |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 52 | writer.writeFlattenable(flattenable); |
| 53 | } |
Mike Reed | fadbfcd | 2017-12-06 16:09:20 -0500 | [diff] [blame] | 54 | static void Read(SkReadBuffer& reader, T** flattenable) { |
mtklein | 3b37545 | 2016-04-04 14:57:19 -0700 | [diff] [blame] | 55 | *flattenable = (T*)reader.readFlattenable(T::GetFlattenableType()); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 56 | } |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | template<> struct SerializationUtils<SkMatrix> { |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 60 | static void Write(SkWriteBuffer& writer, const SkMatrix* matrix) { |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 61 | writer.writeMatrix(*matrix); |
| 62 | } |
Mike Reed | fadbfcd | 2017-12-06 16:09:20 -0500 | [diff] [blame] | 63 | static void Read(SkReadBuffer& reader, SkMatrix* matrix) { |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 64 | reader.readMatrix(matrix); |
| 65 | } |
| 66 | }; |
| 67 | |
| 68 | template<> struct SerializationUtils<SkPath> { |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 69 | static void Write(SkWriteBuffer& writer, const SkPath* path) { |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 70 | writer.writePath(*path); |
| 71 | } |
Mike Reed | fadbfcd | 2017-12-06 16:09:20 -0500 | [diff] [blame] | 72 | static void Read(SkReadBuffer& reader, SkPath* path) { |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 73 | reader.readPath(path); |
| 74 | } |
| 75 | }; |
| 76 | |
| 77 | template<> struct SerializationUtils<SkRegion> { |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 78 | static void Write(SkWriteBuffer& writer, const SkRegion* region) { |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 79 | writer.writeRegion(*region); |
| 80 | } |
Mike Reed | fadbfcd | 2017-12-06 16:09:20 -0500 | [diff] [blame] | 81 | static void Read(SkReadBuffer& reader, SkRegion* region) { |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 82 | reader.readRegion(region); |
| 83 | } |
| 84 | }; |
| 85 | |
commit-bot@chromium.org | 1ac99c8 | 2014-04-29 15:35:23 +0000 | [diff] [blame] | 86 | template<> struct SerializationUtils<SkString> { |
| 87 | static void Write(SkWriteBuffer& writer, const SkString* string) { |
| 88 | writer.writeString(string->c_str()); |
| 89 | } |
Mike Reed | fadbfcd | 2017-12-06 16:09:20 -0500 | [diff] [blame] | 90 | static void Read(SkReadBuffer& reader, SkString* string) { |
commit-bot@chromium.org | 1ac99c8 | 2014-04-29 15:35:23 +0000 | [diff] [blame] | 91 | reader.readString(string); |
| 92 | } |
| 93 | }; |
| 94 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 95 | template<> struct SerializationUtils<unsigned char> { |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 96 | static void Write(SkWriteBuffer& writer, unsigned char* data, uint32_t arraySize) { |
reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame] | 97 | writer.writeByteArray(data, arraySize); |
sugoi@google.com | 305f78e | 2013-11-04 16:18:15 +0000 | [diff] [blame] | 98 | } |
Mike Reed | fadbfcd | 2017-12-06 16:09:20 -0500 | [diff] [blame] | 99 | static bool Read(SkReadBuffer& reader, unsigned char* data, uint32_t arraySize) { |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 100 | return reader.readByteArray(data, arraySize); |
| 101 | } |
| 102 | }; |
commit-bot@chromium.org | 0251288 | 2013-10-31 18:37:50 +0000 | [diff] [blame] | 103 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 104 | template<> struct SerializationUtils<SkColor> { |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 105 | static void Write(SkWriteBuffer& writer, SkColor* data, uint32_t arraySize) { |
reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame] | 106 | writer.writeColorArray(data, arraySize); |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 107 | } |
Mike Reed | fadbfcd | 2017-12-06 16:09:20 -0500 | [diff] [blame] | 108 | static bool Read(SkReadBuffer& reader, SkColor* data, uint32_t arraySize) { |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 109 | return reader.readColorArray(data, arraySize); |
| 110 | } |
| 111 | }; |
sugoi@google.com | 305f78e | 2013-11-04 16:18:15 +0000 | [diff] [blame] | 112 | |
brianosman | 97bbf82 | 2016-09-25 13:15:58 -0700 | [diff] [blame] | 113 | template<> struct SerializationUtils<SkColor4f> { |
| 114 | static void Write(SkWriteBuffer& writer, SkColor4f* data, uint32_t arraySize) { |
| 115 | writer.writeColor4fArray(data, arraySize); |
| 116 | } |
Mike Reed | fadbfcd | 2017-12-06 16:09:20 -0500 | [diff] [blame] | 117 | static bool Read(SkReadBuffer& reader, SkColor4f* data, uint32_t arraySize) { |
brianosman | 97bbf82 | 2016-09-25 13:15:58 -0700 | [diff] [blame] | 118 | return reader.readColor4fArray(data, arraySize); |
| 119 | } |
| 120 | }; |
| 121 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 122 | template<> struct SerializationUtils<int32_t> { |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 123 | static void Write(SkWriteBuffer& writer, int32_t* data, uint32_t arraySize) { |
reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame] | 124 | writer.writeIntArray(data, arraySize); |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 125 | } |
Mike Reed | fadbfcd | 2017-12-06 16:09:20 -0500 | [diff] [blame] | 126 | static bool Read(SkReadBuffer& reader, int32_t* data, uint32_t arraySize) { |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 127 | return reader.readIntArray(data, arraySize); |
| 128 | } |
| 129 | }; |
sugoi@google.com | 305f78e | 2013-11-04 16:18:15 +0000 | [diff] [blame] | 130 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 131 | template<> struct SerializationUtils<SkPoint> { |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 132 | static void Write(SkWriteBuffer& writer, SkPoint* data, uint32_t arraySize) { |
reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame] | 133 | writer.writePointArray(data, arraySize); |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 134 | } |
Mike Reed | fadbfcd | 2017-12-06 16:09:20 -0500 | [diff] [blame] | 135 | static bool Read(SkReadBuffer& reader, SkPoint* data, uint32_t arraySize) { |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 136 | return reader.readPointArray(data, arraySize); |
| 137 | } |
| 138 | }; |
reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame] | 139 | |
Michael Ludwig | ea24140 | 2018-08-22 09:26:33 -0400 | [diff] [blame] | 140 | template<> struct SerializationUtils<SkPoint3> { |
| 141 | static void Write(SkWriteBuffer& writer, const SkPoint3* data) { |
| 142 | writer.writePoint3(*data); |
| 143 | } |
| 144 | static void Read(SkReadBuffer& reader, SkPoint3* data) { |
| 145 | reader.readPoint3(data); |
| 146 | } |
| 147 | }; |
| 148 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 149 | template<> struct SerializationUtils<SkScalar> { |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 150 | static void Write(SkWriteBuffer& writer, SkScalar* data, uint32_t arraySize) { |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 151 | writer.writeScalarArray(data, arraySize); |
| 152 | } |
Mike Reed | fadbfcd | 2017-12-06 16:09:20 -0500 | [diff] [blame] | 153 | static bool Read(SkReadBuffer& reader, SkScalar* data, uint32_t arraySize) { |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 154 | return reader.readScalarArray(data, arraySize); |
| 155 | } |
| 156 | }; |
reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame] | 157 | |
commit-bot@chromium.org | 1ac99c8 | 2014-04-29 15:35:23 +0000 | [diff] [blame] | 158 | template<typename T, bool testInvalid> struct SerializationTestUtils { |
| 159 | static void InvalidateData(unsigned char* data) {} |
| 160 | }; |
| 161 | |
| 162 | template<> struct SerializationTestUtils<SkString, true> { |
| 163 | static void InvalidateData(unsigned char* data) { |
| 164 | data[3] |= 0x80; // Reverse sign of 1st integer |
| 165 | } |
| 166 | }; |
| 167 | |
| 168 | template<typename T, bool testInvalid> |
| 169 | static void TestObjectSerializationNoAlign(T* testObj, skiatest::Reporter* reporter) { |
brianosman | fad9856 | 2016-05-04 11:06:28 -0700 | [diff] [blame] | 170 | SkBinaryWriteBuffer writer; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 171 | SerializationUtils<T>::Write(writer, testObj); |
| 172 | size_t bytesWritten = writer.bytesWritten(); |
| 173 | REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten); |
reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame] | 174 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 175 | unsigned char dataWritten[1024]; |
| 176 | writer.writeToMemory(dataWritten); |
| 177 | |
commit-bot@chromium.org | 1ac99c8 | 2014-04-29 15:35:23 +0000 | [diff] [blame] | 178 | SerializationTestUtils<T, testInvalid>::InvalidateData(dataWritten); |
| 179 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 180 | // Make sure this fails when it should (test with smaller size, but still multiple of 4) |
Mike Reed | fadbfcd | 2017-12-06 16:09:20 -0500 | [diff] [blame] | 181 | SkReadBuffer buffer(dataWritten, bytesWritten - 4); |
commit-bot@chromium.org | 8f457e3 | 2013-11-08 19:22:57 +0000 | [diff] [blame] | 182 | T obj; |
| 183 | SerializationUtils<T>::Read(buffer, &obj); |
commit-bot@chromium.org | c2e9db3 | 2013-12-06 20:14:46 +0000 | [diff] [blame] | 184 | REPORTER_ASSERT(reporter, !buffer.isValid()); |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 185 | |
| 186 | // Make sure this succeeds when it should |
Mike Reed | fadbfcd | 2017-12-06 16:09:20 -0500 | [diff] [blame] | 187 | SkReadBuffer buffer2(dataWritten, bytesWritten); |
Robert Phillips | b252604 | 2016-09-26 09:00:36 -0400 | [diff] [blame] | 188 | size_t offsetBefore = buffer2.offset(); |
commit-bot@chromium.org | 8f457e3 | 2013-11-08 19:22:57 +0000 | [diff] [blame] | 189 | T obj2; |
| 190 | SerializationUtils<T>::Read(buffer2, &obj2); |
Robert Phillips | b252604 | 2016-09-26 09:00:36 -0400 | [diff] [blame] | 191 | size_t offsetAfter = buffer2.offset(); |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 192 | // This should have succeeded, since there are enough bytes to read this |
commit-bot@chromium.org | 1ac99c8 | 2014-04-29 15:35:23 +0000 | [diff] [blame] | 193 | REPORTER_ASSERT(reporter, buffer2.isValid() == !testInvalid); |
| 194 | // Note: This following test should always succeed, regardless of whether the buffer is valid, |
| 195 | // since if it is invalid, it will simply skip to the end, as if it had read the whole buffer. |
Robert Phillips | b252604 | 2016-09-26 09:00:36 -0400 | [diff] [blame] | 196 | REPORTER_ASSERT(reporter, offsetAfter - offsetBefore == bytesWritten); |
commit-bot@chromium.org | 1ac99c8 | 2014-04-29 15:35:23 +0000 | [diff] [blame] | 197 | } |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 198 | |
commit-bot@chromium.org | 1ac99c8 | 2014-04-29 15:35:23 +0000 | [diff] [blame] | 199 | template<typename T> |
| 200 | static void TestObjectSerialization(T* testObj, skiatest::Reporter* reporter) { |
| 201 | TestObjectSerializationNoAlign<T, false>(testObj, reporter); |
Cary Clark | 6d6d603 | 2017-10-20 12:14:33 -0400 | [diff] [blame] | 202 | SerializationTest::TestAlignment(testObj, reporter); |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | template<typename T> |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 206 | static T* TestFlattenableSerialization(T* testObj, bool shouldSucceed, |
| 207 | skiatest::Reporter* reporter) { |
brianosman | fad9856 | 2016-05-04 11:06:28 -0700 | [diff] [blame] | 208 | SkBinaryWriteBuffer writer; |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 209 | SerializationUtils<T>::Write(writer, testObj); |
| 210 | size_t bytesWritten = writer.bytesWritten(); |
| 211 | REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten); |
| 212 | |
dvonbeck | 8811e40 | 2016-06-16 12:39:25 -0700 | [diff] [blame] | 213 | SkASSERT(bytesWritten <= 4096); |
senorblanco | 91c395a | 2014-09-25 15:51:35 -0700 | [diff] [blame] | 214 | unsigned char dataWritten[4096]; |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 215 | writer.writeToMemory(dataWritten); |
| 216 | |
| 217 | // Make sure this fails when it should (test with smaller size, but still multiple of 4) |
Mike Reed | fadbfcd | 2017-12-06 16:09:20 -0500 | [diff] [blame] | 218 | SkReadBuffer buffer(dataWritten, bytesWritten - 4); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 219 | T* obj = nullptr; |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 220 | SerializationUtils<T>::Read(buffer, &obj); |
commit-bot@chromium.org | c2e9db3 | 2013-12-06 20:14:46 +0000 | [diff] [blame] | 221 | REPORTER_ASSERT(reporter, !buffer.isValid()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 222 | REPORTER_ASSERT(reporter, nullptr == obj); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 223 | |
| 224 | // Make sure this succeeds when it should |
Mike Reed | fadbfcd | 2017-12-06 16:09:20 -0500 | [diff] [blame] | 225 | SkReadBuffer buffer2(dataWritten, bytesWritten); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 226 | const unsigned char* peekBefore = static_cast<const unsigned char*>(buffer2.skip(0)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 227 | T* obj2 = nullptr; |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 228 | SerializationUtils<T>::Read(buffer2, &obj2); |
| 229 | const unsigned char* peekAfter = static_cast<const unsigned char*>(buffer2.skip(0)); |
| 230 | if (shouldSucceed) { |
| 231 | // This should have succeeded, since there are enough bytes to read this |
commit-bot@chromium.org | c2e9db3 | 2013-12-06 20:14:46 +0000 | [diff] [blame] | 232 | REPORTER_ASSERT(reporter, buffer2.isValid()); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 233 | REPORTER_ASSERT(reporter, static_cast<size_t>(peekAfter - peekBefore) == bytesWritten); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 234 | REPORTER_ASSERT(reporter, obj2); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 235 | } else { |
| 236 | // If the deserialization was supposed to fail, make sure it did |
commit-bot@chromium.org | c2e9db3 | 2013-12-06 20:14:46 +0000 | [diff] [blame] | 237 | REPORTER_ASSERT(reporter, !buffer.isValid()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 238 | REPORTER_ASSERT(reporter, nullptr == obj2); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | return obj2; // Return object to perform further validity tests on it |
| 242 | } |
| 243 | |
| 244 | template<typename T> |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 245 | static void TestArraySerialization(T* data, skiatest::Reporter* reporter) { |
brianosman | fad9856 | 2016-05-04 11:06:28 -0700 | [diff] [blame] | 246 | SkBinaryWriteBuffer writer; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 247 | SerializationUtils<T>::Write(writer, data, kArraySize); |
| 248 | size_t bytesWritten = writer.bytesWritten(); |
| 249 | // This should write the length (in 4 bytes) and the array |
| 250 | REPORTER_ASSERT(reporter, (4 + kArraySize * sizeof(T)) == bytesWritten); |
| 251 | |
brianosman | 97bbf82 | 2016-09-25 13:15:58 -0700 | [diff] [blame] | 252 | unsigned char dataWritten[2048]; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 253 | writer.writeToMemory(dataWritten); |
| 254 | |
| 255 | // Make sure this fails when it should |
Mike Reed | fadbfcd | 2017-12-06 16:09:20 -0500 | [diff] [blame] | 256 | SkReadBuffer buffer(dataWritten, bytesWritten); |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 257 | T dataRead[kArraySize]; |
| 258 | bool success = SerializationUtils<T>::Read(buffer, dataRead, kArraySize / 2); |
| 259 | // This should have failed, since the provided size was too small |
| 260 | REPORTER_ASSERT(reporter, !success); |
| 261 | |
| 262 | // Make sure this succeeds when it should |
Mike Reed | fadbfcd | 2017-12-06 16:09:20 -0500 | [diff] [blame] | 263 | SkReadBuffer buffer2(dataWritten, bytesWritten); |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 264 | success = SerializationUtils<T>::Read(buffer2, dataRead, kArraySize); |
| 265 | // This should have succeeded, since there are enough bytes to read this |
| 266 | REPORTER_ASSERT(reporter, success); |
| 267 | } |
| 268 | |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 269 | static void TestBitmapSerialization(const SkBitmap& validBitmap, |
| 270 | const SkBitmap& invalidBitmap, |
| 271 | bool shouldSucceed, |
| 272 | skiatest::Reporter* reporter) { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 273 | sk_sp<SkImage> validImage(SkImage::MakeFromBitmap(validBitmap)); |
Michael Ludwig | 55edb50 | 2019-08-05 10:41:10 -0400 | [diff] [blame] | 274 | sk_sp<SkImageFilter> validBitmapSource(SkImageFilters::Image(std::move(validImage))); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 275 | sk_sp<SkImage> invalidImage(SkImage::MakeFromBitmap(invalidBitmap)); |
Michael Ludwig | 55edb50 | 2019-08-05 10:41:10 -0400 | [diff] [blame] | 276 | sk_sp<SkImageFilter> invalidBitmapSource(SkImageFilters::Image(std::move(invalidImage))); |
reed | cfb6bdf | 2016-03-29 11:32:50 -0700 | [diff] [blame] | 277 | sk_sp<SkImageFilter> xfermodeImageFilter( |
Michael Ludwig | 55edb50 | 2019-08-05 10:41:10 -0400 | [diff] [blame] | 278 | SkImageFilters::Xfermode(SkBlendMode::kSrcOver, |
| 279 | std::move(invalidBitmapSource), |
| 280 | std::move(validBitmapSource), nullptr)); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 281 | |
Mike Reed | 5e25717 | 2016-11-01 11:22:05 -0400 | [diff] [blame] | 282 | sk_sp<SkImageFilter> deserializedFilter( |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 283 | TestFlattenableSerialization<SkImageFilter>( |
reed | cfb6bdf | 2016-03-29 11:32:50 -0700 | [diff] [blame] | 284 | xfermodeImageFilter.get(), shouldSucceed, reporter)); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 285 | |
| 286 | // Try to render a small bitmap using the invalid deserialized filter |
| 287 | // to make sure we don't crash while trying to render it |
| 288 | if (shouldSucceed) { |
| 289 | SkBitmap bitmap; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 290 | bitmap.allocN32Pixels(24, 24); |
| 291 | SkCanvas canvas(bitmap); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 292 | canvas.clear(0x00000000); |
| 293 | SkPaint paint; |
| 294 | paint.setImageFilter(deserializedFilter); |
| 295 | canvas.clipRect(SkRect::MakeXYWH(0, 0, SkIntToScalar(24), SkIntToScalar(24))); |
| 296 | canvas.drawBitmap(bitmap, 0, 0, &paint); |
| 297 | } |
| 298 | } |
| 299 | |
senorblanco | 91c395a | 2014-09-25 15:51:35 -0700 | [diff] [blame] | 300 | static void TestColorFilterSerialization(skiatest::Reporter* reporter) { |
| 301 | uint8_t table[256]; |
| 302 | for (int i = 0; i < 256; ++i) { |
| 303 | table[i] = (i * 41) % 256; |
| 304 | } |
Mike Reed | 7991594 | 2020-06-26 09:05:10 -0400 | [diff] [blame] | 305 | auto filter = SkTableColorFilter::Make(table); |
Michael Ludwig | a693a47 | 2020-06-25 21:41:09 +0000 | [diff] [blame] | 306 | sk_sp<SkColorFilter> copy( |
Mike Reed | 7991594 | 2020-06-26 09:05:10 -0400 | [diff] [blame] | 307 | TestFlattenableSerialization(as_CFB(filter.get()), true, reporter)); |
senorblanco | 91c395a | 2014-09-25 15:51:35 -0700 | [diff] [blame] | 308 | } |
| 309 | |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 310 | static SkBitmap draw_picture(SkPicture& picture) { |
| 311 | SkBitmap bitmap; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 312 | bitmap.allocN32Pixels(SkScalarCeilToInt(picture.cullRect().width()), |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 313 | SkScalarCeilToInt(picture.cullRect().height())); |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 314 | SkCanvas canvas(bitmap); |
robertphillips | c5ba71d | 2014-09-04 08:42:50 -0700 | [diff] [blame] | 315 | picture.playback(&canvas); |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 316 | return bitmap; |
| 317 | } |
| 318 | |
| 319 | static void compare_bitmaps(skiatest::Reporter* reporter, |
| 320 | const SkBitmap& b1, const SkBitmap& b2) { |
| 321 | REPORTER_ASSERT(reporter, b1.width() == b2.width()); |
| 322 | REPORTER_ASSERT(reporter, b1.height() == b2.height()); |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 323 | |
| 324 | if ((b1.width() != b2.width()) || |
| 325 | (b1.height() != b2.height())) { |
| 326 | return; |
| 327 | } |
| 328 | |
| 329 | int pixelErrors = 0; |
| 330 | for (int y = 0; y < b2.height(); ++y) { |
| 331 | for (int x = 0; x < b2.width(); ++x) { |
| 332 | if (b1.getColor(x, y) != b2.getColor(x, y)) |
| 333 | ++pixelErrors; |
| 334 | } |
| 335 | } |
| 336 | REPORTER_ASSERT(reporter, 0 == pixelErrors); |
| 337 | } |
Alan Screen | 3cf3d92 | 2020-06-19 10:43:50 -0700 | [diff] [blame] | 338 | |
| 339 | static sk_sp<SkData> serialize_typeface_proc(SkTypeface* typeface, void* ctx) { |
| 340 | // Write out typeface ID followed by entire typeface. |
| 341 | SkDynamicMemoryWStream stream; |
| 342 | sk_sp<SkData> data(typeface->serialize(SkTypeface::SerializeBehavior::kDoIncludeData)); |
| 343 | uint32_t typeface_id = typeface->uniqueID(); |
| 344 | stream.write(&typeface_id, sizeof(typeface_id)); |
| 345 | stream.write(data->data(), data->size()); |
| 346 | return stream.detachAsData(); |
| 347 | } |
| 348 | |
| 349 | static sk_sp<SkTypeface> deserialize_typeface_proc(const void* data, size_t length, void* ctx) { |
| 350 | SkStream* stream; |
| 351 | if (length < sizeof(stream)) { |
| 352 | return nullptr; |
| 353 | } |
| 354 | memcpy(&stream, data, sizeof(stream)); |
| 355 | |
| 356 | SkFontID id; |
| 357 | if (!stream->read(&id, sizeof(id))) { |
| 358 | return nullptr; |
| 359 | } |
| 360 | |
| 361 | sk_sp<SkTypeface> typeface = SkTypeface::MakeDeserialize(stream); |
| 362 | return typeface; |
| 363 | } |
| 364 | |
| 365 | static void serialize_and_compare_typeface(sk_sp<SkTypeface> typeface, |
| 366 | const char* text, |
| 367 | const SkSerialProcs* serial_procs, |
| 368 | const SkDeserialProcs* deserial_procs, |
| 369 | skiatest::Reporter* reporter) { |
Hal Canary | 3560ea7 | 2019-01-08 13:01:58 -0500 | [diff] [blame] | 370 | // Create a font with the typeface. |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 371 | SkPaint paint; |
| 372 | paint.setColor(SK_ColorGRAY); |
Hal Canary | 3560ea7 | 2019-01-08 13:01:58 -0500 | [diff] [blame] | 373 | SkFont font(std::move(typeface), 30); |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 374 | |
| 375 | // Paint some text. |
| 376 | SkPictureRecorder recorder; |
| 377 | SkIRect canvasRect = SkIRect::MakeWH(kBitmapSize, kBitmapSize); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 378 | SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(canvasRect.width()), |
| 379 | SkIntToScalar(canvasRect.height()), |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 380 | nullptr, 0); |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 381 | canvas->drawColor(SK_ColorWHITE); |
Hal Canary | 3560ea7 | 2019-01-08 13:01:58 -0500 | [diff] [blame] | 382 | canvas->drawString(text, 24, 32, font, paint); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 383 | sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture()); |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 384 | |
| 385 | // Serlialize picture and create its clone from stream. |
| 386 | SkDynamicMemoryWStream stream; |
Alan Screen | 3cf3d92 | 2020-06-19 10:43:50 -0700 | [diff] [blame] | 387 | picture->serialize(&stream, serial_procs); |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 388 | std::unique_ptr<SkStream> inputStream(stream.detachAsStream()); |
Alan Screen | 3cf3d92 | 2020-06-19 10:43:50 -0700 | [diff] [blame] | 389 | sk_sp<SkPicture> loadedPicture(SkPicture::MakeFromStream(inputStream.get(), deserial_procs)); |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 390 | |
| 391 | // Draw both original and clone picture and compare bitmaps -- they should be identical. |
| 392 | SkBitmap origBitmap = draw_picture(*picture); |
| 393 | SkBitmap destBitmap = draw_picture(*loadedPicture); |
| 394 | compare_bitmaps(reporter, origBitmap, destBitmap); |
| 395 | } |
| 396 | |
Ben Wagner | e80a595 | 2020-07-14 17:57:13 -0400 | [diff] [blame] | 397 | static sk_sp<SkTypeface> makeDistortableWithNonDefaultAxes(skiatest::Reporter* reporter) { |
| 398 | std::unique_ptr<SkStreamAsset> distortable(GetResourceAsStream("fonts/Distortable.ttf")); |
| 399 | if (!distortable) { |
| 400 | REPORT_FAILURE(reporter, "distortable", SkString()); |
| 401 | return nullptr; |
| 402 | } |
| 403 | |
| 404 | const SkFontArguments::VariationPosition::Coordinate position[] = { |
| 405 | { SkSetFourByteTag('w','g','h','t'), SK_ScalarSqrt2 }, |
| 406 | }; |
| 407 | SkFontArguments params; |
| 408 | params.setVariationDesignPosition({position, SK_ARRAY_COUNT(position)}); |
| 409 | |
| 410 | sk_sp<SkFontMgr> fm = SkFontMgr::RefDefault(); |
| 411 | |
| 412 | sk_sp<SkTypeface> typeface = fm->makeFromStream(std::move(distortable), params); |
| 413 | if (!typeface) { |
| 414 | return nullptr; // Not all SkFontMgr can makeFromStream(). |
| 415 | } |
| 416 | |
| 417 | int count = typeface->getVariationDesignPosition(nullptr, 0); |
| 418 | if (count == -1) { |
| 419 | return nullptr; // The number of axes is unknown. |
| 420 | } |
| 421 | |
| 422 | return typeface; |
| 423 | } |
| 424 | |
Alan Screen | 3cf3d92 | 2020-06-19 10:43:50 -0700 | [diff] [blame] | 425 | static void TestPictureTypefaceSerialization(const SkSerialProcs* serial_procs, |
| 426 | const SkDeserialProcs* deserial_procs, |
| 427 | skiatest::Reporter* reporter) { |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 428 | { |
| 429 | // Load typeface from file to test CreateFromFile with index. |
Mike Reed | 271d1d9 | 2018-09-03 21:10:10 -0400 | [diff] [blame] | 430 | auto typeface = MakeResourceAsTypeface("fonts/test.ttc", 1); |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 431 | if (!typeface) { |
halcanary | 7d57124 | 2016-02-24 17:59:16 -0800 | [diff] [blame] | 432 | INFOF(reporter, "Could not run fontstream test because test.ttc not found."); |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 433 | } else { |
Alan Screen | 3cf3d92 | 2020-06-19 10:43:50 -0700 | [diff] [blame] | 434 | serialize_and_compare_typeface(std::move(typeface), "A!", serial_procs, deserial_procs, |
| 435 | reporter); |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 436 | } |
| 437 | } |
| 438 | |
| 439 | { |
| 440 | // Load typeface as stream to create with axis settings. |
Ben Wagner | e80a595 | 2020-07-14 17:57:13 -0400 | [diff] [blame] | 441 | auto typeface = makeDistortableWithNonDefaultAxes(reporter); |
| 442 | if (!typeface) { |
| 443 | INFOF(reporter, "Could not run fontstream test because Distortable.ttf not created."); |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 444 | } else { |
Ben Wagner | e80a595 | 2020-07-14 17:57:13 -0400 | [diff] [blame] | 445 | serialize_and_compare_typeface(std::move(typeface), "ab", serial_procs, |
| 446 | deserial_procs, reporter); |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 447 | } |
| 448 | } |
| 449 | } |
| 450 | |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 451 | static void setup_bitmap_for_canvas(SkBitmap* bitmap) { |
| 452 | bitmap->allocN32Pixels(kBitmapSize, kBitmapSize); |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 453 | } |
| 454 | |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 455 | static void make_checkerboard_bitmap(SkBitmap& bitmap) { |
| 456 | setup_bitmap_for_canvas(&bitmap); |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 457 | |
| 458 | SkCanvas canvas(bitmap); |
| 459 | canvas.clear(0x00000000); |
| 460 | SkPaint darkPaint; |
| 461 | darkPaint.setColor(0xFF804020); |
| 462 | SkPaint lightPaint; |
| 463 | lightPaint.setColor(0xFF244484); |
| 464 | const int i = kBitmapSize / 8; |
| 465 | const SkScalar f = SkIntToScalar(i); |
| 466 | for (int y = 0; y < kBitmapSize; y += i) { |
| 467 | for (int x = 0; x < kBitmapSize; x += i) { |
| 468 | canvas.save(); |
| 469 | canvas.translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 470 | canvas.drawRect(SkRect::MakeXYWH(0, 0, f, f), darkPaint); |
| 471 | canvas.drawRect(SkRect::MakeXYWH(f, 0, f, f), lightPaint); |
| 472 | canvas.drawRect(SkRect::MakeXYWH(0, f, f, f), lightPaint); |
| 473 | canvas.drawRect(SkRect::MakeXYWH(f, f, f, f), darkPaint); |
| 474 | canvas.restore(); |
| 475 | } |
| 476 | } |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 477 | } |
| 478 | |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 479 | static void draw_something(SkCanvas* canvas) { |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 480 | SkPaint paint; |
| 481 | SkBitmap bitmap; |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 482 | make_checkerboard_bitmap(bitmap); |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 483 | |
| 484 | canvas->save(); |
| 485 | canvas->scale(0.5f, 0.5f); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 486 | canvas->drawBitmap(bitmap, 0, 0, nullptr); |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 487 | canvas->restore(); |
| 488 | |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 489 | paint.setAntiAlias(true); |
| 490 | |
| 491 | paint.setColor(SK_ColorRED); |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 492 | canvas->drawCircle(SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/3), paint); |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 493 | paint.setColor(SK_ColorBLACK); |
Mike Reed | 1af9b48 | 2019-01-07 11:01:57 -0500 | [diff] [blame] | 494 | |
| 495 | SkFont font; |
| 496 | font.setSize(kBitmapSize/3); |
| 497 | canvas->drawString("Picture", SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/4), font, paint); |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 498 | } |
| 499 | |
Ben Wagner | 7825d49 | 2019-07-10 16:27:34 -0400 | [diff] [blame] | 500 | static sk_sp<SkImage> render(const SkPicture& p) { |
| 501 | auto surf = SkSurface::MakeRasterN32Premul(SkScalarRoundToInt(p.cullRect().width()), |
| 502 | SkScalarRoundToInt(p.cullRect().height())); |
| 503 | if (!surf) { |
| 504 | return nullptr; // bounds are empty? |
| 505 | } |
| 506 | surf->getCanvas()->clear(SK_ColorWHITE); |
| 507 | p.playback(surf->getCanvas()); |
| 508 | return surf->makeImageSnapshot(); |
| 509 | } |
| 510 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 511 | DEF_TEST(Serialization, reporter) { |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 512 | // Test matrix serialization |
| 513 | { |
| 514 | SkMatrix matrix = SkMatrix::I(); |
| 515 | TestObjectSerialization(&matrix, reporter); |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 516 | } |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 517 | |
Michael Ludwig | ea24140 | 2018-08-22 09:26:33 -0400 | [diff] [blame] | 518 | // Test point3 serialization |
| 519 | { |
| 520 | SkPoint3 point; |
| 521 | TestObjectSerializationNoAlign<SkPoint3, false>(&point, reporter); |
| 522 | } |
| 523 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 524 | // Test path serialization |
| 525 | { |
| 526 | SkPath path; |
| 527 | TestObjectSerialization(&path, reporter); |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 528 | } |
sugoi@google.com | 305f78e | 2013-11-04 16:18:15 +0000 | [diff] [blame] | 529 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 530 | // Test region serialization |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 531 | { |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 532 | SkRegion region; |
| 533 | TestObjectSerialization(®ion, reporter); |
| 534 | } |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 535 | |
senorblanco | 91c395a | 2014-09-25 15:51:35 -0700 | [diff] [blame] | 536 | // Test color filter serialization |
| 537 | { |
| 538 | TestColorFilterSerialization(reporter); |
| 539 | } |
| 540 | |
commit-bot@chromium.org | 1ac99c8 | 2014-04-29 15:35:23 +0000 | [diff] [blame] | 541 | // Test string serialization |
| 542 | { |
| 543 | SkString string("string"); |
| 544 | TestObjectSerializationNoAlign<SkString, false>(&string, reporter); |
| 545 | TestObjectSerializationNoAlign<SkString, true>(&string, reporter); |
| 546 | } |
| 547 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 548 | // Test rrect serialization |
| 549 | { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 550 | // SkRRect does not initialize anything. |
| 551 | // An uninitialized SkRRect can be serialized, |
| 552 | // but will branch on uninitialized data when deserialized. |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 553 | SkRRect rrect; |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 554 | SkRect rect = SkRect::MakeXYWH(1, 2, 20, 30); |
| 555 | SkVector corners[4] = { {1, 2}, {2, 3}, {3,4}, {4,5} }; |
| 556 | rrect.setRectRadii(rect, corners); |
Cary Clark | 6d6d603 | 2017-10-20 12:14:33 -0400 | [diff] [blame] | 557 | SerializationTest::TestAlignment(&rrect, reporter); |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 558 | } |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 559 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 560 | // Test readByteArray |
| 561 | { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 562 | unsigned char data[kArraySize] = { 1, 2, 3 }; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 563 | TestArraySerialization(data, reporter); |
| 564 | } |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 565 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 566 | // Test readColorArray |
| 567 | { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 568 | SkColor data[kArraySize] = { SK_ColorBLACK, SK_ColorWHITE, SK_ColorRED }; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 569 | TestArraySerialization(data, reporter); |
| 570 | } |
| 571 | |
brianosman | 97bbf82 | 2016-09-25 13:15:58 -0700 | [diff] [blame] | 572 | // Test readColor4fArray |
| 573 | { |
| 574 | SkColor4f data[kArraySize] = { |
| 575 | SkColor4f::FromColor(SK_ColorBLACK), |
| 576 | SkColor4f::FromColor(SK_ColorWHITE), |
| 577 | SkColor4f::FromColor(SK_ColorRED), |
| 578 | { 1.f, 2.f, 4.f, 8.f } |
| 579 | }; |
| 580 | TestArraySerialization(data, reporter); |
| 581 | } |
| 582 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 583 | // Test readIntArray |
| 584 | { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 585 | int32_t data[kArraySize] = { 1, 2, 4, 8 }; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 586 | TestArraySerialization(data, reporter); |
| 587 | } |
| 588 | |
| 589 | // Test readPointArray |
| 590 | { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 591 | SkPoint data[kArraySize] = { {6, 7}, {42, 128} }; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 592 | TestArraySerialization(data, reporter); |
| 593 | } |
| 594 | |
| 595 | // Test readScalarArray |
| 596 | { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 597 | SkScalar data[kArraySize] = { SK_Scalar1, SK_ScalarHalf, SK_ScalarMax }; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 598 | TestArraySerialization(data, reporter); |
commit-bot@chromium.org | 0251288 | 2013-10-31 18:37:50 +0000 | [diff] [blame] | 599 | } |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 600 | |
Brian Osman | 9e4e4c7 | 2020-06-10 07:19:34 -0400 | [diff] [blame] | 601 | // Test skipByteArray |
| 602 | { |
| 603 | // Valid case with non-empty array: |
| 604 | { |
| 605 | unsigned char data[kArraySize] = { 1, 2, 3 }; |
| 606 | SkBinaryWriteBuffer writer; |
| 607 | writer.writeByteArray(data, kArraySize); |
| 608 | SkAutoMalloc buf(writer.bytesWritten()); |
| 609 | writer.writeToMemory(buf.get()); |
| 610 | |
| 611 | SkReadBuffer reader(buf.get(), writer.bytesWritten()); |
| 612 | size_t len = ~0; |
| 613 | const void* arr = reader.skipByteArray(&len); |
| 614 | REPORTER_ASSERT(reporter, arr); |
| 615 | REPORTER_ASSERT(reporter, len == kArraySize); |
| 616 | REPORTER_ASSERT(reporter, memcmp(arr, data, len) == 0); |
| 617 | } |
| 618 | |
| 619 | // Writing a zero length array (can be detected as valid by non-nullptr return): |
| 620 | { |
| 621 | SkBinaryWriteBuffer writer; |
| 622 | writer.writeByteArray(nullptr, 0); |
| 623 | SkAutoMalloc buf(writer.bytesWritten()); |
| 624 | writer.writeToMemory(buf.get()); |
| 625 | |
| 626 | SkReadBuffer reader(buf.get(), writer.bytesWritten()); |
| 627 | size_t len = ~0; |
| 628 | const void* arr = reader.skipByteArray(&len); |
| 629 | REPORTER_ASSERT(reporter, arr); |
| 630 | REPORTER_ASSERT(reporter, len == 0); |
| 631 | } |
| 632 | |
| 633 | // If the array can't be safely read, should return nullptr: |
| 634 | { |
| 635 | SkBinaryWriteBuffer writer; |
| 636 | writer.writeUInt(kArraySize); |
| 637 | SkAutoMalloc buf(writer.bytesWritten()); |
| 638 | writer.writeToMemory(buf.get()); |
| 639 | |
| 640 | SkReadBuffer reader(buf.get(), writer.bytesWritten()); |
| 641 | size_t len = ~0; |
| 642 | const void* arr = reader.skipByteArray(&len); |
| 643 | REPORTER_ASSERT(reporter, !arr); |
| 644 | REPORTER_ASSERT(reporter, len == 0); |
| 645 | } |
| 646 | } |
| 647 | |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 648 | // Test invalid deserializations |
| 649 | { |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 650 | SkImageInfo info = SkImageInfo::MakeN32Premul(kBitmapSize, kBitmapSize); |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 651 | |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 652 | SkBitmap validBitmap; |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 653 | validBitmap.setInfo(info); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 654 | |
| 655 | // Create a bitmap with a really large height |
| 656 | SkBitmap invalidBitmap; |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 657 | invalidBitmap.setInfo(info.makeWH(info.width(), 1000000000)); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 658 | |
| 659 | // The deserialization should succeed, and the rendering shouldn't crash, |
| 660 | // even when the device fails to initialize, due to its size |
| 661 | TestBitmapSerialization(validBitmap, invalidBitmap, true, reporter); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 662 | } |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 663 | |
| 664 | // Test simple SkPicture serialization |
| 665 | { |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 666 | SkPictureRecorder recorder; |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 667 | draw_something(recorder.beginRecording(SkIntToScalar(kBitmapSize), |
| 668 | SkIntToScalar(kBitmapSize), |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 669 | nullptr, 0)); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 670 | sk_sp<SkPicture> pict(recorder.finishRecordingAsPicture()); |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 671 | |
| 672 | // Serialize picture |
brianosman | fad9856 | 2016-05-04 11:06:28 -0700 | [diff] [blame] | 673 | SkBinaryWriteBuffer writer; |
Cary Clark | efd99cc | 2018-06-11 16:25:43 -0400 | [diff] [blame] | 674 | SkPicturePriv::Flatten(pict, writer); |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 675 | size_t size = writer.bytesWritten(); |
commit-bot@chromium.org | 1e7ee99 | 2014-03-14 21:22:22 +0000 | [diff] [blame] | 676 | SkAutoTMalloc<unsigned char> data(size); |
| 677 | writer.writeToMemory(static_cast<void*>(data.get())); |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 678 | |
| 679 | // Deserialize picture |
Mike Reed | fadbfcd | 2017-12-06 16:09:20 -0500 | [diff] [blame] | 680 | SkReadBuffer reader(static_cast<void*>(data.get()), size); |
Cary Clark | efd99cc | 2018-06-11 16:25:43 -0400 | [diff] [blame] | 681 | sk_sp<SkPicture> readPict(SkPicturePriv::MakeFromBuffer(reader)); |
reed | d921dbb | 2016-09-30 09:27:20 -0700 | [diff] [blame] | 682 | REPORTER_ASSERT(reporter, reader.isValid()); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 683 | REPORTER_ASSERT(reporter, readPict.get()); |
Ben Wagner | 7825d49 | 2019-07-10 16:27:34 -0400 | [diff] [blame] | 684 | sk_sp<SkImage> img0 = render(*pict); |
| 685 | sk_sp<SkImage> img1 = render(*readPict); |
| 686 | if (img0 && img1) { |
| 687 | REPORTER_ASSERT(reporter, ToolUtils::equal_pixels(img0.get(), img1.get())); |
| 688 | } |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 689 | } |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 690 | |
Alan Screen | 3cf3d92 | 2020-06-19 10:43:50 -0700 | [diff] [blame] | 691 | TestPictureTypefaceSerialization(nullptr, nullptr, reporter); |
| 692 | |
| 693 | SkSerialProcs serial_procs; |
| 694 | serial_procs.fTypefaceProc = serialize_typeface_proc; |
| 695 | SkDeserialProcs deserial_procs; |
| 696 | deserial_procs.fTypefaceProc = deserialize_typeface_proc; |
| 697 | TestPictureTypefaceSerialization(&serial_procs, &deserial_procs, reporter); |
commit-bot@chromium.org | 0251288 | 2013-10-31 18:37:50 +0000 | [diff] [blame] | 698 | } |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 699 | |
| 700 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 701 | #include "include/core/SkAnnotation.h" |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 702 | |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 703 | static sk_sp<SkPicture> copy_picture_via_serialization(SkPicture* src) { |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 704 | SkDynamicMemoryWStream wstream; |
| 705 | src->serialize(&wstream); |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 706 | std::unique_ptr<SkStreamAsset> rstream(wstream.detachAsStream()); |
| 707 | return SkPicture::MakeFromStream(rstream.get()); |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | struct AnnotationRec { |
| 711 | const SkRect fRect; |
| 712 | const char* fKey; |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 713 | sk_sp<SkData> fValue; |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 714 | }; |
| 715 | |
| 716 | class TestAnnotationCanvas : public SkCanvas { |
| 717 | skiatest::Reporter* fReporter; |
| 718 | const AnnotationRec* fRec; |
| 719 | int fCount; |
| 720 | int fCurrIndex; |
| 721 | |
| 722 | public: |
| 723 | TestAnnotationCanvas(skiatest::Reporter* reporter, const AnnotationRec rec[], int count) |
| 724 | : SkCanvas(100, 100) |
| 725 | , fReporter(reporter) |
| 726 | , fRec(rec) |
| 727 | , fCount(count) |
| 728 | , fCurrIndex(0) |
| 729 | {} |
| 730 | |
| 731 | ~TestAnnotationCanvas() { |
| 732 | REPORTER_ASSERT(fReporter, fCount == fCurrIndex); |
| 733 | } |
| 734 | |
| 735 | protected: |
| 736 | void onDrawAnnotation(const SkRect& rect, const char key[], SkData* value) { |
| 737 | REPORTER_ASSERT(fReporter, fCurrIndex < fCount); |
| 738 | REPORTER_ASSERT(fReporter, rect == fRec[fCurrIndex].fRect); |
| 739 | REPORTER_ASSERT(fReporter, !strcmp(key, fRec[fCurrIndex].fKey)); |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 740 | REPORTER_ASSERT(fReporter, value->equals(fRec[fCurrIndex].fValue.get())); |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 741 | fCurrIndex += 1; |
| 742 | } |
| 743 | }; |
| 744 | |
| 745 | /* |
| 746 | * Test the 3 annotation types by recording them into a picture, serializing, and then playing |
| 747 | * them back into another canvas. |
| 748 | */ |
| 749 | DEF_TEST(Annotations, reporter) { |
| 750 | SkPictureRecorder recorder; |
| 751 | SkCanvas* recordingCanvas = recorder.beginRecording(SkRect::MakeWH(100, 100)); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 752 | |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 753 | const char* str0 = "rect-with-url"; |
| 754 | const SkRect r0 = SkRect::MakeWH(10, 10); |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 755 | sk_sp<SkData> d0(SkData::MakeWithCString(str0)); |
| 756 | SkAnnotateRectWithURL(recordingCanvas, r0, d0.get()); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 757 | |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 758 | const char* str1 = "named-destination"; |
| 759 | const SkRect r1 = SkRect::MakeXYWH(5, 5, 0, 0); // collapsed to a point |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 760 | sk_sp<SkData> d1(SkData::MakeWithCString(str1)); |
| 761 | SkAnnotateNamedDestination(recordingCanvas, {r1.x(), r1.y()}, d1.get()); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 762 | |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 763 | const char* str2 = "link-to-destination"; |
| 764 | const SkRect r2 = SkRect::MakeXYWH(20, 20, 5, 6); |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 765 | sk_sp<SkData> d2(SkData::MakeWithCString(str2)); |
| 766 | SkAnnotateLinkToDestination(recordingCanvas, r2, d2.get()); |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 767 | |
| 768 | const AnnotationRec recs[] = { |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 769 | { r0, SkAnnotationKeys::URL_Key(), std::move(d0) }, |
| 770 | { r1, SkAnnotationKeys::Define_Named_Dest_Key(), std::move(d1) }, |
| 771 | { r2, SkAnnotationKeys::Link_Named_Dest_Key(), std::move(d2) }, |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 772 | }; |
| 773 | |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 774 | sk_sp<SkPicture> pict0(recorder.finishRecordingAsPicture()); |
| 775 | sk_sp<SkPicture> pict1(copy_picture_via_serialization(pict0.get())); |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 776 | |
| 777 | TestAnnotationCanvas canvas(reporter, recs, SK_ARRAY_COUNT(recs)); |
| 778 | canvas.drawPicture(pict1); |
| 779 | } |
Mike Reed | 2532584 | 2018-03-14 09:52:02 -0400 | [diff] [blame] | 780 | |
| 781 | DEF_TEST(WriteBuffer_storage, reporter) { |
| 782 | enum { |
| 783 | kSize = 32 |
| 784 | }; |
| 785 | int32_t storage[kSize/4]; |
| 786 | char src[kSize]; |
| 787 | sk_bzero(src, kSize); |
| 788 | |
| 789 | SkBinaryWriteBuffer writer(storage, kSize); |
| 790 | REPORTER_ASSERT(reporter, writer.usingInitialStorage()); |
| 791 | REPORTER_ASSERT(reporter, writer.bytesWritten() == 0); |
| 792 | writer.write(src, kSize - 4); |
| 793 | REPORTER_ASSERT(reporter, writer.usingInitialStorage()); |
| 794 | REPORTER_ASSERT(reporter, writer.bytesWritten() == kSize - 4); |
| 795 | writer.writeInt(0); |
| 796 | REPORTER_ASSERT(reporter, writer.usingInitialStorage()); |
| 797 | REPORTER_ASSERT(reporter, writer.bytesWritten() == kSize); |
| 798 | |
| 799 | writer.reset(storage, kSize-4); |
| 800 | REPORTER_ASSERT(reporter, writer.usingInitialStorage()); |
| 801 | REPORTER_ASSERT(reporter, writer.bytesWritten() == 0); |
| 802 | writer.write(src, kSize - 4); |
| 803 | REPORTER_ASSERT(reporter, writer.usingInitialStorage()); |
| 804 | REPORTER_ASSERT(reporter, writer.bytesWritten() == kSize - 4); |
| 805 | writer.writeInt(0); |
| 806 | REPORTER_ASSERT(reporter, !writer.usingInitialStorage()); // this is the change |
| 807 | REPORTER_ASSERT(reporter, writer.bytesWritten() == kSize); |
| 808 | } |
Khushal | 42f8bc4 | 2018-04-03 17:51:40 -0700 | [diff] [blame] | 809 | |
| 810 | DEF_TEST(WriteBuffer_external_memory_textblob, reporter) { |
Mike Reed | 2ed7820 | 2018-11-21 15:10:08 -0500 | [diff] [blame] | 811 | SkFont font; |
Khushal | 42f8bc4 | 2018-04-03 17:51:40 -0700 | [diff] [blame] | 812 | font.setTypeface(SkTypeface::MakeDefault()); |
| 813 | |
| 814 | SkTextBlobBuilder builder; |
| 815 | int glyph_count = 5; |
| 816 | const auto& run = builder.allocRun(font, glyph_count, 1.2f, 2.3f); |
| 817 | // allocRun() allocates only the glyph buffer. |
| 818 | std::fill(run.glyphs, run.glyphs + glyph_count, 0); |
| 819 | auto blob = builder.make(); |
| 820 | SkSerialProcs procs; |
| 821 | SkAutoTMalloc<uint8_t> storage; |
| 822 | size_t blob_size = 0u; |
| 823 | size_t storage_size = 0u; |
| 824 | |
| 825 | blob_size = SkAlign4(blob->serialize(procs)->size()); |
| 826 | REPORTER_ASSERT(reporter, blob_size > 4u); |
| 827 | storage_size = blob_size - 4; |
| 828 | storage.realloc(storage_size); |
| 829 | REPORTER_ASSERT(reporter, blob->serialize(procs, storage.get(), storage_size) == 0u); |
| 830 | storage_size = blob_size; |
| 831 | storage.realloc(storage_size); |
| 832 | REPORTER_ASSERT(reporter, blob->serialize(procs, storage.get(), storage_size) != 0u); |
| 833 | } |
| 834 | |
| 835 | DEF_TEST(WriteBuffer_external_memory_flattenable, reporter) { |
| 836 | SkScalar intervals[] = {1.f, 1.f}; |
| 837 | auto path_effect = SkDashPathEffect::Make(intervals, 2, 0); |
| 838 | size_t path_size = SkAlign4(path_effect->serialize()->size()); |
| 839 | REPORTER_ASSERT(reporter, path_size > 4u); |
| 840 | SkAutoTMalloc<uint8_t> storage; |
| 841 | |
| 842 | size_t storage_size = path_size - 4; |
| 843 | storage.realloc(storage_size); |
| 844 | REPORTER_ASSERT(reporter, path_effect->serialize(storage.get(), storage_size) == 0u); |
| 845 | |
| 846 | storage_size = path_size; |
| 847 | storage.realloc(storage_size); |
| 848 | REPORTER_ASSERT(reporter, path_effect->serialize(storage.get(), storage_size) != 0u); |
| 849 | } |
Mike Reed | 33b1ecc | 2020-02-18 22:22:49 -0500 | [diff] [blame] | 850 | |
Mike Reed | 33b1ecc | 2020-02-18 22:22:49 -0500 | [diff] [blame] | 851 | DEF_TEST(ReadBuffer_empty, reporter) { |
| 852 | SkBinaryWriteBuffer writer; |
| 853 | writer.writeInt(123); |
| 854 | writer.writeDataAsByteArray(SkData::MakeEmpty().get()); |
| 855 | writer.writeInt(321); |
| 856 | |
| 857 | size_t size = writer.bytesWritten(); |
| 858 | SkAutoMalloc storage(size); |
| 859 | writer.writeToMemory(storage.get()); |
| 860 | |
| 861 | SkReadBuffer reader(storage.get(), size); |
| 862 | REPORTER_ASSERT(reporter, reader.readInt() == 123); |
| 863 | auto data = reader.readByteArrayAsData(); |
| 864 | REPORTER_ASSERT(reporter, data->size() == 0); |
| 865 | REPORTER_ASSERT(reporter, reader.readInt() == 321); |
| 866 | } |