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 | |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 8 | #include "SkAnnotationKeys.h" |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 9 | #include "Resources.h" |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 10 | #include "SkCanvas.h" |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 11 | #include "SkFixed.h" |
| 12 | #include "SkFontDescriptor.h" |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 13 | #include "SkImage.h" |
| 14 | #include "SkImageSource.h" |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 15 | #include "SkMallocPixelRef.h" |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 16 | #include "SkOSFile.h" |
robertphillips@google.com | 770963f | 2014-04-18 18:04:41 +0000 | [diff] [blame] | 17 | #include "SkPictureRecorder.h" |
senorblanco | 91c395a | 2014-09-25 15:51:35 -0700 | [diff] [blame] | 18 | #include "SkTableColorFilter.h" |
commit-bot@chromium.org | 1e7ee99 | 2014-03-14 21:22:22 +0000 | [diff] [blame] | 19 | #include "SkTemplates.h" |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 20 | #include "SkTypeface.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 21 | #include "SkWriteBuffer.h" |
commit-bot@chromium.org | 0251288 | 2013-10-31 18:37:50 +0000 | [diff] [blame] | 22 | #include "SkValidatingReadBuffer.h" |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 23 | #include "SkXfermodeImageFilter.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 24 | #include "Test.h" |
commit-bot@chromium.org | 0251288 | 2013-10-31 18:37:50 +0000 | [diff] [blame] | 25 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 26 | static const uint32_t kArraySize = 64; |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 27 | static const int kBitmapSize = 256; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 28 | |
| 29 | template<typename T> |
| 30 | static void TestAlignment(T* testObj, skiatest::Reporter* reporter) { |
| 31 | // Test memory read/write functions directly |
| 32 | unsigned char dataWritten[1024]; |
| 33 | size_t bytesWrittenToMemory = testObj->writeToMemory(dataWritten); |
| 34 | REPORTER_ASSERT(reporter, SkAlign4(bytesWrittenToMemory) == bytesWrittenToMemory); |
| 35 | size_t bytesReadFromMemory = testObj->readFromMemory(dataWritten, bytesWrittenToMemory); |
| 36 | REPORTER_ASSERT(reporter, SkAlign4(bytesReadFromMemory) == bytesReadFromMemory); |
| 37 | } |
| 38 | |
| 39 | template<typename T> struct SerializationUtils { |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 40 | // Generic case for flattenables |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 41 | static void Write(SkWriteBuffer& writer, const T* flattenable) { |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 42 | writer.writeFlattenable(flattenable); |
| 43 | } |
| 44 | static void Read(SkValidatingReadBuffer& reader, T** flattenable) { |
mtklein | 3b37545 | 2016-04-04 14:57:19 -0700 | [diff] [blame] | 45 | *flattenable = (T*)reader.readFlattenable(T::GetFlattenableType()); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 46 | } |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | template<> struct SerializationUtils<SkMatrix> { |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 50 | static void Write(SkWriteBuffer& writer, const SkMatrix* matrix) { |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 51 | writer.writeMatrix(*matrix); |
| 52 | } |
| 53 | static void Read(SkValidatingReadBuffer& reader, SkMatrix* matrix) { |
| 54 | reader.readMatrix(matrix); |
| 55 | } |
| 56 | }; |
| 57 | |
| 58 | template<> struct SerializationUtils<SkPath> { |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 59 | static void Write(SkWriteBuffer& writer, const SkPath* path) { |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 60 | writer.writePath(*path); |
| 61 | } |
| 62 | static void Read(SkValidatingReadBuffer& reader, SkPath* path) { |
| 63 | reader.readPath(path); |
| 64 | } |
| 65 | }; |
| 66 | |
| 67 | template<> struct SerializationUtils<SkRegion> { |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 68 | static void Write(SkWriteBuffer& writer, const SkRegion* region) { |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 69 | writer.writeRegion(*region); |
| 70 | } |
| 71 | static void Read(SkValidatingReadBuffer& reader, SkRegion* region) { |
| 72 | reader.readRegion(region); |
| 73 | } |
| 74 | }; |
| 75 | |
commit-bot@chromium.org | 1ac99c8 | 2014-04-29 15:35:23 +0000 | [diff] [blame] | 76 | template<> struct SerializationUtils<SkString> { |
| 77 | static void Write(SkWriteBuffer& writer, const SkString* string) { |
| 78 | writer.writeString(string->c_str()); |
| 79 | } |
| 80 | static void Read(SkValidatingReadBuffer& reader, SkString* string) { |
| 81 | reader.readString(string); |
| 82 | } |
| 83 | }; |
| 84 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 85 | template<> struct SerializationUtils<unsigned char> { |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 86 | static void Write(SkWriteBuffer& writer, unsigned char* data, uint32_t arraySize) { |
reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame] | 87 | writer.writeByteArray(data, arraySize); |
sugoi@google.com | 305f78e | 2013-11-04 16:18:15 +0000 | [diff] [blame] | 88 | } |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 89 | static bool Read(SkValidatingReadBuffer& reader, unsigned char* data, uint32_t arraySize) { |
| 90 | return reader.readByteArray(data, arraySize); |
| 91 | } |
| 92 | }; |
commit-bot@chromium.org | 0251288 | 2013-10-31 18:37:50 +0000 | [diff] [blame] | 93 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 94 | template<> struct SerializationUtils<SkColor> { |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 95 | static void Write(SkWriteBuffer& writer, SkColor* data, uint32_t arraySize) { |
reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame] | 96 | writer.writeColorArray(data, arraySize); |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 97 | } |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 98 | static bool Read(SkValidatingReadBuffer& reader, SkColor* data, uint32_t arraySize) { |
| 99 | return reader.readColorArray(data, arraySize); |
| 100 | } |
| 101 | }; |
sugoi@google.com | 305f78e | 2013-11-04 16:18:15 +0000 | [diff] [blame] | 102 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 103 | template<> struct SerializationUtils<int32_t> { |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 104 | static void Write(SkWriteBuffer& writer, int32_t* data, uint32_t arraySize) { |
reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame] | 105 | writer.writeIntArray(data, arraySize); |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 106 | } |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 107 | static bool Read(SkValidatingReadBuffer& reader, int32_t* data, uint32_t arraySize) { |
| 108 | return reader.readIntArray(data, arraySize); |
| 109 | } |
| 110 | }; |
sugoi@google.com | 305f78e | 2013-11-04 16:18:15 +0000 | [diff] [blame] | 111 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 112 | template<> struct SerializationUtils<SkPoint> { |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 113 | static void Write(SkWriteBuffer& writer, SkPoint* data, uint32_t arraySize) { |
reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame] | 114 | writer.writePointArray(data, arraySize); |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 115 | } |
| 116 | static bool Read(SkValidatingReadBuffer& reader, SkPoint* data, uint32_t arraySize) { |
| 117 | return reader.readPointArray(data, arraySize); |
| 118 | } |
| 119 | }; |
reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame] | 120 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 121 | template<> struct SerializationUtils<SkScalar> { |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 122 | static void Write(SkWriteBuffer& writer, SkScalar* data, uint32_t arraySize) { |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 123 | writer.writeScalarArray(data, arraySize); |
| 124 | } |
| 125 | static bool Read(SkValidatingReadBuffer& reader, SkScalar* data, uint32_t arraySize) { |
| 126 | return reader.readScalarArray(data, arraySize); |
| 127 | } |
| 128 | }; |
reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame] | 129 | |
commit-bot@chromium.org | 1ac99c8 | 2014-04-29 15:35:23 +0000 | [diff] [blame] | 130 | template<typename T, bool testInvalid> struct SerializationTestUtils { |
| 131 | static void InvalidateData(unsigned char* data) {} |
| 132 | }; |
| 133 | |
| 134 | template<> struct SerializationTestUtils<SkString, true> { |
| 135 | static void InvalidateData(unsigned char* data) { |
| 136 | data[3] |= 0x80; // Reverse sign of 1st integer |
| 137 | } |
| 138 | }; |
| 139 | |
| 140 | template<typename T, bool testInvalid> |
| 141 | static void TestObjectSerializationNoAlign(T* testObj, skiatest::Reporter* reporter) { |
brianosman | fad9856 | 2016-05-04 11:06:28 -0700 | [diff] [blame] | 142 | SkBinaryWriteBuffer writer; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 143 | SerializationUtils<T>::Write(writer, testObj); |
| 144 | size_t bytesWritten = writer.bytesWritten(); |
| 145 | REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten); |
reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame] | 146 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 147 | unsigned char dataWritten[1024]; |
| 148 | writer.writeToMemory(dataWritten); |
| 149 | |
commit-bot@chromium.org | 1ac99c8 | 2014-04-29 15:35:23 +0000 | [diff] [blame] | 150 | SerializationTestUtils<T, testInvalid>::InvalidateData(dataWritten); |
| 151 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 152 | // Make sure this fails when it should (test with smaller size, but still multiple of 4) |
| 153 | SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 4); |
commit-bot@chromium.org | 8f457e3 | 2013-11-08 19:22:57 +0000 | [diff] [blame] | 154 | T obj; |
| 155 | SerializationUtils<T>::Read(buffer, &obj); |
commit-bot@chromium.org | c2e9db3 | 2013-12-06 20:14:46 +0000 | [diff] [blame] | 156 | REPORTER_ASSERT(reporter, !buffer.isValid()); |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 157 | |
| 158 | // Make sure this succeeds when it should |
| 159 | SkValidatingReadBuffer buffer2(dataWritten, bytesWritten); |
commit-bot@chromium.org | 8f457e3 | 2013-11-08 19:22:57 +0000 | [diff] [blame] | 160 | const unsigned char* peekBefore = static_cast<const unsigned char*>(buffer2.skip(0)); |
| 161 | T obj2; |
| 162 | SerializationUtils<T>::Read(buffer2, &obj2); |
| 163 | const unsigned char* peekAfter = static_cast<const unsigned char*>(buffer2.skip(0)); |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 164 | // 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] | 165 | REPORTER_ASSERT(reporter, buffer2.isValid() == !testInvalid); |
| 166 | // Note: This following test should always succeed, regardless of whether the buffer is valid, |
| 167 | // since if it is invalid, it will simply skip to the end, as if it had read the whole buffer. |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 168 | REPORTER_ASSERT(reporter, static_cast<size_t>(peekAfter - peekBefore) == bytesWritten); |
commit-bot@chromium.org | 1ac99c8 | 2014-04-29 15:35:23 +0000 | [diff] [blame] | 169 | } |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 170 | |
commit-bot@chromium.org | 1ac99c8 | 2014-04-29 15:35:23 +0000 | [diff] [blame] | 171 | template<typename T> |
| 172 | static void TestObjectSerialization(T* testObj, skiatest::Reporter* reporter) { |
| 173 | TestObjectSerializationNoAlign<T, false>(testObj, reporter); |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 174 | TestAlignment(testObj, reporter); |
| 175 | } |
| 176 | |
| 177 | template<typename T> |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 178 | static T* TestFlattenableSerialization(T* testObj, bool shouldSucceed, |
| 179 | skiatest::Reporter* reporter) { |
brianosman | fad9856 | 2016-05-04 11:06:28 -0700 | [diff] [blame] | 180 | SkBinaryWriteBuffer writer; |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 181 | SerializationUtils<T>::Write(writer, testObj); |
| 182 | size_t bytesWritten = writer.bytesWritten(); |
| 183 | REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten); |
| 184 | |
senorblanco | 91c395a | 2014-09-25 15:51:35 -0700 | [diff] [blame] | 185 | unsigned char dataWritten[4096]; |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 186 | SkASSERT(bytesWritten <= sizeof(dataWritten)); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 187 | writer.writeToMemory(dataWritten); |
| 188 | |
| 189 | // Make sure this fails when it should (test with smaller size, but still multiple of 4) |
| 190 | SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 4); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 191 | T* obj = nullptr; |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 192 | SerializationUtils<T>::Read(buffer, &obj); |
commit-bot@chromium.org | c2e9db3 | 2013-12-06 20:14:46 +0000 | [diff] [blame] | 193 | REPORTER_ASSERT(reporter, !buffer.isValid()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 194 | REPORTER_ASSERT(reporter, nullptr == obj); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 195 | |
| 196 | // Make sure this succeeds when it should |
| 197 | SkValidatingReadBuffer buffer2(dataWritten, bytesWritten); |
| 198 | const unsigned char* peekBefore = static_cast<const unsigned char*>(buffer2.skip(0)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 199 | T* obj2 = nullptr; |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 200 | SerializationUtils<T>::Read(buffer2, &obj2); |
| 201 | const unsigned char* peekAfter = static_cast<const unsigned char*>(buffer2.skip(0)); |
| 202 | if (shouldSucceed) { |
| 203 | // 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] | 204 | REPORTER_ASSERT(reporter, buffer2.isValid()); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 205 | REPORTER_ASSERT(reporter, static_cast<size_t>(peekAfter - peekBefore) == bytesWritten); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 206 | REPORTER_ASSERT(reporter, obj2); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 207 | } else { |
| 208 | // 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] | 209 | REPORTER_ASSERT(reporter, !buffer.isValid()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 210 | REPORTER_ASSERT(reporter, nullptr == obj2); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | return obj2; // Return object to perform further validity tests on it |
| 214 | } |
| 215 | |
| 216 | template<typename T> |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 217 | static void TestArraySerialization(T* data, skiatest::Reporter* reporter) { |
brianosman | fad9856 | 2016-05-04 11:06:28 -0700 | [diff] [blame] | 218 | SkBinaryWriteBuffer writer; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 219 | SerializationUtils<T>::Write(writer, data, kArraySize); |
| 220 | size_t bytesWritten = writer.bytesWritten(); |
| 221 | // This should write the length (in 4 bytes) and the array |
| 222 | REPORTER_ASSERT(reporter, (4 + kArraySize * sizeof(T)) == bytesWritten); |
| 223 | |
| 224 | unsigned char dataWritten[1024]; |
| 225 | writer.writeToMemory(dataWritten); |
| 226 | |
| 227 | // Make sure this fails when it should |
| 228 | SkValidatingReadBuffer buffer(dataWritten, bytesWritten); |
| 229 | T dataRead[kArraySize]; |
| 230 | bool success = SerializationUtils<T>::Read(buffer, dataRead, kArraySize / 2); |
| 231 | // This should have failed, since the provided size was too small |
| 232 | REPORTER_ASSERT(reporter, !success); |
| 233 | |
| 234 | // Make sure this succeeds when it should |
| 235 | SkValidatingReadBuffer buffer2(dataWritten, bytesWritten); |
| 236 | success = SerializationUtils<T>::Read(buffer2, dataRead, kArraySize); |
| 237 | // This should have succeeded, since there are enough bytes to read this |
| 238 | REPORTER_ASSERT(reporter, success); |
| 239 | } |
| 240 | |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 241 | static void TestBitmapSerialization(const SkBitmap& validBitmap, |
| 242 | const SkBitmap& invalidBitmap, |
| 243 | bool shouldSucceed, |
| 244 | skiatest::Reporter* reporter) { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 245 | sk_sp<SkImage> validImage(SkImage::MakeFromBitmap(validBitmap)); |
robertphillips | 549c899 | 2016-04-01 09:28:51 -0700 | [diff] [blame] | 246 | sk_sp<SkImageFilter> validBitmapSource(SkImageSource::Make(std::move(validImage))); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 247 | sk_sp<SkImage> invalidImage(SkImage::MakeFromBitmap(invalidBitmap)); |
robertphillips | 549c899 | 2016-04-01 09:28:51 -0700 | [diff] [blame] | 248 | sk_sp<SkImageFilter> invalidBitmapSource(SkImageSource::Make(std::move(invalidImage))); |
reed | cfb6bdf | 2016-03-29 11:32:50 -0700 | [diff] [blame] | 249 | sk_sp<SkImageFilter> xfermodeImageFilter( |
| 250 | SkXfermodeImageFilter::Make(SkXfermode::Make(SkXfermode::kSrcOver_Mode), |
robertphillips | 8c0326d | 2016-04-05 12:48:34 -0700 | [diff] [blame] | 251 | std::move(invalidBitmapSource), |
| 252 | std::move(validBitmapSource), nullptr)); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 253 | |
| 254 | SkAutoTUnref<SkImageFilter> deserializedFilter( |
| 255 | TestFlattenableSerialization<SkImageFilter>( |
reed | cfb6bdf | 2016-03-29 11:32:50 -0700 | [diff] [blame] | 256 | xfermodeImageFilter.get(), shouldSucceed, reporter)); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 257 | |
| 258 | // Try to render a small bitmap using the invalid deserialized filter |
| 259 | // to make sure we don't crash while trying to render it |
| 260 | if (shouldSucceed) { |
| 261 | SkBitmap bitmap; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 262 | bitmap.allocN32Pixels(24, 24); |
| 263 | SkCanvas canvas(bitmap); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 264 | canvas.clear(0x00000000); |
| 265 | SkPaint paint; |
| 266 | paint.setImageFilter(deserializedFilter); |
| 267 | canvas.clipRect(SkRect::MakeXYWH(0, 0, SkIntToScalar(24), SkIntToScalar(24))); |
| 268 | canvas.drawBitmap(bitmap, 0, 0, &paint); |
| 269 | } |
| 270 | } |
| 271 | |
senorblanco | 0f7197b | 2014-09-24 11:09:38 -0700 | [diff] [blame] | 272 | static void TestXfermodeSerialization(skiatest::Reporter* reporter) { |
| 273 | for (size_t i = 0; i <= SkXfermode::kLastMode; ++i) { |
| 274 | if (i == SkXfermode::kSrcOver_Mode) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 275 | // skip SrcOver, as it is allowed to return nullptr from Create() |
senorblanco | 0f7197b | 2014-09-24 11:09:38 -0700 | [diff] [blame] | 276 | continue; |
| 277 | } |
reed | cfb6bdf | 2016-03-29 11:32:50 -0700 | [diff] [blame] | 278 | auto mode(SkXfermode::Make(static_cast<SkXfermode::Mode>(i))); |
| 279 | REPORTER_ASSERT(reporter, mode); |
senorblanco | 0f7197b | 2014-09-24 11:09:38 -0700 | [diff] [blame] | 280 | SkAutoTUnref<SkXfermode> copy( |
| 281 | TestFlattenableSerialization<SkXfermode>(mode.get(), true, reporter)); |
| 282 | } |
| 283 | } |
| 284 | |
senorblanco | 91c395a | 2014-09-25 15:51:35 -0700 | [diff] [blame] | 285 | static void TestColorFilterSerialization(skiatest::Reporter* reporter) { |
| 286 | uint8_t table[256]; |
| 287 | for (int i = 0; i < 256; ++i) { |
| 288 | table[i] = (i * 41) % 256; |
| 289 | } |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 290 | auto colorFilter(SkTableColorFilter::Make(table)); |
senorblanco | 91c395a | 2014-09-25 15:51:35 -0700 | [diff] [blame] | 291 | SkAutoTUnref<SkColorFilter> copy( |
| 292 | TestFlattenableSerialization<SkColorFilter>(colorFilter.get(), true, reporter)); |
| 293 | } |
| 294 | |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 295 | static SkBitmap draw_picture(SkPicture& picture) { |
| 296 | SkBitmap bitmap; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 297 | bitmap.allocN32Pixels(SkScalarCeilToInt(picture.cullRect().width()), |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 298 | SkScalarCeilToInt(picture.cullRect().height())); |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 299 | SkCanvas canvas(bitmap); |
robertphillips | c5ba71d | 2014-09-04 08:42:50 -0700 | [diff] [blame] | 300 | picture.playback(&canvas); |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 301 | return bitmap; |
| 302 | } |
| 303 | |
| 304 | static void compare_bitmaps(skiatest::Reporter* reporter, |
| 305 | const SkBitmap& b1, const SkBitmap& b2) { |
| 306 | REPORTER_ASSERT(reporter, b1.width() == b2.width()); |
| 307 | REPORTER_ASSERT(reporter, b1.height() == b2.height()); |
| 308 | SkAutoLockPixels autoLockPixels1(b1); |
| 309 | SkAutoLockPixels autoLockPixels2(b2); |
| 310 | |
| 311 | if ((b1.width() != b2.width()) || |
| 312 | (b1.height() != b2.height())) { |
| 313 | return; |
| 314 | } |
| 315 | |
| 316 | int pixelErrors = 0; |
| 317 | for (int y = 0; y < b2.height(); ++y) { |
| 318 | for (int x = 0; x < b2.width(); ++x) { |
| 319 | if (b1.getColor(x, y) != b2.getColor(x, y)) |
| 320 | ++pixelErrors; |
| 321 | } |
| 322 | } |
| 323 | REPORTER_ASSERT(reporter, 0 == pixelErrors); |
| 324 | } |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 325 | static void serialize_and_compare_typeface(SkTypeface* typeface, const char* text, |
| 326 | skiatest::Reporter* reporter) |
| 327 | { |
| 328 | // Create a paint with the typeface. |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 329 | SkPaint paint; |
| 330 | paint.setColor(SK_ColorGRAY); |
| 331 | paint.setTextSize(SkIntToScalar(30)); |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 332 | paint.setTypeface(typeface); |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 333 | |
| 334 | // Paint some text. |
| 335 | SkPictureRecorder recorder; |
| 336 | SkIRect canvasRect = SkIRect::MakeWH(kBitmapSize, kBitmapSize); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 337 | SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(canvasRect.width()), |
| 338 | SkIntToScalar(canvasRect.height()), |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 339 | nullptr, 0); |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 340 | canvas->drawColor(SK_ColorWHITE); |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 341 | canvas->drawText(text, 2, 24, 32, paint); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 342 | sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture()); |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 343 | |
| 344 | // Serlialize picture and create its clone from stream. |
| 345 | SkDynamicMemoryWStream stream; |
| 346 | picture->serialize(&stream); |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 347 | SkAutoTDelete<SkStream> inputStream(stream.detachAsStream()); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 348 | sk_sp<SkPicture> loadedPicture(SkPicture::MakeFromStream(inputStream.get())); |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 349 | |
| 350 | // Draw both original and clone picture and compare bitmaps -- they should be identical. |
| 351 | SkBitmap origBitmap = draw_picture(*picture); |
| 352 | SkBitmap destBitmap = draw_picture(*loadedPicture); |
| 353 | compare_bitmaps(reporter, origBitmap, destBitmap); |
| 354 | } |
| 355 | |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 356 | static void TestPictureTypefaceSerialization(skiatest::Reporter* reporter) { |
| 357 | { |
| 358 | // Load typeface from file to test CreateFromFile with index. |
| 359 | SkString filename = GetResourcePath("/fonts/test.ttc"); |
| 360 | SkAutoTUnref<SkTypeface> typeface(SkTypeface::CreateFromFile(filename.c_str(), 1)); |
| 361 | if (!typeface) { |
halcanary | 7d57124 | 2016-02-24 17:59:16 -0800 | [diff] [blame] | 362 | INFOF(reporter, "Could not run fontstream test because test.ttc not found."); |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 363 | } else { |
| 364 | serialize_and_compare_typeface(typeface, "A!", reporter); |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | { |
| 369 | // Load typeface as stream to create with axis settings. |
| 370 | SkAutoTDelete<SkStreamAsset> distortable(GetResourceAsStream("/fonts/Distortable.ttf")); |
| 371 | if (!distortable) { |
halcanary | 7d57124 | 2016-02-24 17:59:16 -0800 | [diff] [blame] | 372 | INFOF(reporter, "Could not run fontstream test because Distortable.ttf not found."); |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 373 | } else { |
| 374 | SkFixed axis = SK_FixedSqrt2; |
| 375 | SkAutoTUnref<SkTypeface> typeface(SkTypeface::CreateFromFontData( |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 376 | new SkFontData(distortable.release(), 0, &axis, 1))); |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 377 | if (!typeface) { |
halcanary | 7d57124 | 2016-02-24 17:59:16 -0800 | [diff] [blame] | 378 | INFOF(reporter, "Could not run fontstream test because Distortable.ttf not created."); |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 379 | } else { |
| 380 | serialize_and_compare_typeface(typeface, "abc", reporter); |
| 381 | } |
| 382 | } |
| 383 | } |
| 384 | } |
| 385 | |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 386 | static void setup_bitmap_for_canvas(SkBitmap* bitmap) { |
| 387 | bitmap->allocN32Pixels(kBitmapSize, kBitmapSize); |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 388 | } |
| 389 | |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 390 | static void make_checkerboard_bitmap(SkBitmap& bitmap) { |
| 391 | setup_bitmap_for_canvas(&bitmap); |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 392 | |
| 393 | SkCanvas canvas(bitmap); |
| 394 | canvas.clear(0x00000000); |
| 395 | SkPaint darkPaint; |
| 396 | darkPaint.setColor(0xFF804020); |
| 397 | SkPaint lightPaint; |
| 398 | lightPaint.setColor(0xFF244484); |
| 399 | const int i = kBitmapSize / 8; |
| 400 | const SkScalar f = SkIntToScalar(i); |
| 401 | for (int y = 0; y < kBitmapSize; y += i) { |
| 402 | for (int x = 0; x < kBitmapSize; x += i) { |
| 403 | canvas.save(); |
| 404 | canvas.translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 405 | canvas.drawRect(SkRect::MakeXYWH(0, 0, f, f), darkPaint); |
| 406 | canvas.drawRect(SkRect::MakeXYWH(f, 0, f, f), lightPaint); |
| 407 | canvas.drawRect(SkRect::MakeXYWH(0, f, f, f), lightPaint); |
| 408 | canvas.drawRect(SkRect::MakeXYWH(f, f, f, f), darkPaint); |
| 409 | canvas.restore(); |
| 410 | } |
| 411 | } |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 412 | } |
| 413 | |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 414 | static void draw_something(SkCanvas* canvas) { |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 415 | SkPaint paint; |
| 416 | SkBitmap bitmap; |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 417 | make_checkerboard_bitmap(bitmap); |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 418 | |
| 419 | canvas->save(); |
| 420 | canvas->scale(0.5f, 0.5f); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 421 | canvas->drawBitmap(bitmap, 0, 0, nullptr); |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 422 | canvas->restore(); |
| 423 | |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 424 | paint.setAntiAlias(true); |
| 425 | |
| 426 | paint.setColor(SK_ColorRED); |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 427 | 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] | 428 | paint.setColor(SK_ColorBLACK); |
| 429 | paint.setTextSize(SkIntToScalar(kBitmapSize/3)); |
| 430 | canvas->drawText("Picture", 7, SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/4), paint); |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 431 | } |
| 432 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 433 | DEF_TEST(Serialization, reporter) { |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 434 | // Test matrix serialization |
| 435 | { |
| 436 | SkMatrix matrix = SkMatrix::I(); |
| 437 | TestObjectSerialization(&matrix, reporter); |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 438 | } |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 439 | |
| 440 | // Test path serialization |
| 441 | { |
| 442 | SkPath path; |
| 443 | TestObjectSerialization(&path, reporter); |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 444 | } |
sugoi@google.com | 305f78e | 2013-11-04 16:18:15 +0000 | [diff] [blame] | 445 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 446 | // Test region serialization |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 447 | { |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 448 | SkRegion region; |
| 449 | TestObjectSerialization(®ion, reporter); |
| 450 | } |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 451 | |
senorblanco | 0f7197b | 2014-09-24 11:09:38 -0700 | [diff] [blame] | 452 | // Test xfermode serialization |
| 453 | { |
| 454 | TestXfermodeSerialization(reporter); |
| 455 | } |
| 456 | |
senorblanco | 91c395a | 2014-09-25 15:51:35 -0700 | [diff] [blame] | 457 | // Test color filter serialization |
| 458 | { |
| 459 | TestColorFilterSerialization(reporter); |
| 460 | } |
| 461 | |
commit-bot@chromium.org | 1ac99c8 | 2014-04-29 15:35:23 +0000 | [diff] [blame] | 462 | // Test string serialization |
| 463 | { |
| 464 | SkString string("string"); |
| 465 | TestObjectSerializationNoAlign<SkString, false>(&string, reporter); |
| 466 | TestObjectSerializationNoAlign<SkString, true>(&string, reporter); |
| 467 | } |
| 468 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 469 | // Test rrect serialization |
| 470 | { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 471 | // SkRRect does not initialize anything. |
| 472 | // An uninitialized SkRRect can be serialized, |
| 473 | // but will branch on uninitialized data when deserialized. |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 474 | SkRRect rrect; |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 475 | SkRect rect = SkRect::MakeXYWH(1, 2, 20, 30); |
| 476 | SkVector corners[4] = { {1, 2}, {2, 3}, {3,4}, {4,5} }; |
| 477 | rrect.setRectRadii(rect, corners); |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 478 | TestAlignment(&rrect, reporter); |
| 479 | } |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 480 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 481 | // Test readByteArray |
| 482 | { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 483 | unsigned char data[kArraySize] = { 1, 2, 3 }; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 484 | TestArraySerialization(data, reporter); |
| 485 | } |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 486 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 487 | // Test readColorArray |
| 488 | { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 489 | SkColor data[kArraySize] = { SK_ColorBLACK, SK_ColorWHITE, SK_ColorRED }; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 490 | TestArraySerialization(data, reporter); |
| 491 | } |
| 492 | |
| 493 | // Test readIntArray |
| 494 | { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 495 | int32_t data[kArraySize] = { 1, 2, 4, 8 }; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 496 | TestArraySerialization(data, reporter); |
| 497 | } |
| 498 | |
| 499 | // Test readPointArray |
| 500 | { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 501 | SkPoint data[kArraySize] = { {6, 7}, {42, 128} }; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 502 | TestArraySerialization(data, reporter); |
| 503 | } |
| 504 | |
| 505 | // Test readScalarArray |
| 506 | { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 507 | SkScalar data[kArraySize] = { SK_Scalar1, SK_ScalarHalf, SK_ScalarMax }; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 508 | TestArraySerialization(data, reporter); |
commit-bot@chromium.org | 0251288 | 2013-10-31 18:37:50 +0000 | [diff] [blame] | 509 | } |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 510 | |
| 511 | // Test invalid deserializations |
| 512 | { |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 513 | SkImageInfo info = SkImageInfo::MakeN32Premul(kBitmapSize, kBitmapSize); |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 514 | |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 515 | SkBitmap validBitmap; |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 516 | validBitmap.setInfo(info); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 517 | |
| 518 | // Create a bitmap with a really large height |
| 519 | SkBitmap invalidBitmap; |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 520 | invalidBitmap.setInfo(info.makeWH(info.width(), 1000000000)); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 521 | |
| 522 | // The deserialization should succeed, and the rendering shouldn't crash, |
| 523 | // even when the device fails to initialize, due to its size |
| 524 | TestBitmapSerialization(validBitmap, invalidBitmap, true, reporter); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 525 | } |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 526 | |
| 527 | // Test simple SkPicture serialization |
| 528 | { |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 529 | SkPictureRecorder recorder; |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 530 | draw_something(recorder.beginRecording(SkIntToScalar(kBitmapSize), |
| 531 | SkIntToScalar(kBitmapSize), |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 532 | nullptr, 0)); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 533 | sk_sp<SkPicture> pict(recorder.finishRecordingAsPicture()); |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 534 | |
| 535 | // Serialize picture |
brianosman | fad9856 | 2016-05-04 11:06:28 -0700 | [diff] [blame] | 536 | SkBinaryWriteBuffer writer; |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 537 | pict->flatten(writer); |
| 538 | size_t size = writer.bytesWritten(); |
commit-bot@chromium.org | 1e7ee99 | 2014-03-14 21:22:22 +0000 | [diff] [blame] | 539 | SkAutoTMalloc<unsigned char> data(size); |
| 540 | writer.writeToMemory(static_cast<void*>(data.get())); |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 541 | |
| 542 | // Deserialize picture |
commit-bot@chromium.org | 1e7ee99 | 2014-03-14 21:22:22 +0000 | [diff] [blame] | 543 | SkValidatingReadBuffer reader(static_cast<void*>(data.get()), size); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 544 | sk_sp<SkPicture> readPict(SkPicture::MakeFromBuffer(reader)); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 545 | REPORTER_ASSERT(reporter, readPict.get()); |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 546 | } |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 547 | |
| 548 | TestPictureTypefaceSerialization(reporter); |
commit-bot@chromium.org | 0251288 | 2013-10-31 18:37:50 +0000 | [diff] [blame] | 549 | } |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 550 | |
| 551 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 552 | #include "SkAnnotation.h" |
| 553 | |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 554 | static sk_sp<SkPicture> copy_picture_via_serialization(SkPicture* src) { |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 555 | SkDynamicMemoryWStream wstream; |
| 556 | src->serialize(&wstream); |
| 557 | SkAutoTDelete<SkStreamAsset> rstream(wstream.detachAsStream()); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 558 | return SkPicture::MakeFromStream(rstream); |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | struct AnnotationRec { |
| 562 | const SkRect fRect; |
| 563 | const char* fKey; |
| 564 | SkData* fValue; |
| 565 | }; |
| 566 | |
| 567 | class TestAnnotationCanvas : public SkCanvas { |
| 568 | skiatest::Reporter* fReporter; |
| 569 | const AnnotationRec* fRec; |
| 570 | int fCount; |
| 571 | int fCurrIndex; |
| 572 | |
| 573 | public: |
| 574 | TestAnnotationCanvas(skiatest::Reporter* reporter, const AnnotationRec rec[], int count) |
| 575 | : SkCanvas(100, 100) |
| 576 | , fReporter(reporter) |
| 577 | , fRec(rec) |
| 578 | , fCount(count) |
| 579 | , fCurrIndex(0) |
| 580 | {} |
| 581 | |
| 582 | ~TestAnnotationCanvas() { |
| 583 | REPORTER_ASSERT(fReporter, fCount == fCurrIndex); |
| 584 | } |
| 585 | |
| 586 | protected: |
| 587 | void onDrawAnnotation(const SkRect& rect, const char key[], SkData* value) { |
| 588 | REPORTER_ASSERT(fReporter, fCurrIndex < fCount); |
| 589 | REPORTER_ASSERT(fReporter, rect == fRec[fCurrIndex].fRect); |
| 590 | REPORTER_ASSERT(fReporter, !strcmp(key, fRec[fCurrIndex].fKey)); |
| 591 | REPORTER_ASSERT(fReporter, value->equals(fRec[fCurrIndex].fValue)); |
| 592 | fCurrIndex += 1; |
| 593 | } |
| 594 | }; |
| 595 | |
| 596 | /* |
| 597 | * Test the 3 annotation types by recording them into a picture, serializing, and then playing |
| 598 | * them back into another canvas. |
| 599 | */ |
| 600 | DEF_TEST(Annotations, reporter) { |
| 601 | SkPictureRecorder recorder; |
| 602 | SkCanvas* recordingCanvas = recorder.beginRecording(SkRect::MakeWH(100, 100)); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 603 | |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 604 | const char* str0 = "rect-with-url"; |
| 605 | const SkRect r0 = SkRect::MakeWH(10, 10); |
| 606 | SkAutoTUnref<SkData> d0(SkData::NewWithCString(str0)); |
| 607 | SkAnnotateRectWithURL(recordingCanvas, r0, d0); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 608 | |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 609 | const char* str1 = "named-destination"; |
| 610 | const SkRect r1 = SkRect::MakeXYWH(5, 5, 0, 0); // collapsed to a point |
| 611 | SkAutoTUnref<SkData> d1(SkData::NewWithCString(str1)); |
| 612 | SkAnnotateNamedDestination(recordingCanvas, {r1.x(), r1.y()}, d1); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 613 | |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 614 | const char* str2 = "link-to-destination"; |
| 615 | const SkRect r2 = SkRect::MakeXYWH(20, 20, 5, 6); |
| 616 | SkAutoTUnref<SkData> d2(SkData::NewWithCString(str2)); |
| 617 | SkAnnotateLinkToDestination(recordingCanvas, r2, d2); |
| 618 | |
| 619 | const AnnotationRec recs[] = { |
| 620 | { r0, SkAnnotationKeys::URL_Key(), d0 }, |
| 621 | { r1, SkAnnotationKeys::Define_Named_Dest_Key(), d1 }, |
| 622 | { r2, SkAnnotationKeys::Link_Named_Dest_Key(), d2 }, |
| 623 | }; |
| 624 | |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 625 | sk_sp<SkPicture> pict0(recorder.finishRecordingAsPicture()); |
| 626 | sk_sp<SkPicture> pict1(copy_picture_via_serialization(pict0.get())); |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 627 | |
| 628 | TestAnnotationCanvas canvas(reporter, recs, SK_ARRAY_COUNT(recs)); |
| 629 | canvas.drawPicture(pict1); |
| 630 | } |