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) { |
msarett | 99d9231 | 2016-04-04 13:53:02 -0700 | [diff] [blame^] | 45 | *flattenable = (T*)reader.readFlattenable(); |
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) { |
commit-bot@chromium.org | a2bd2d1 | 2014-01-30 22:16:32 +0000 | [diff] [blame] | 142 | SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag); |
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) { |
commit-bot@chromium.org | a2bd2d1 | 2014-01-30 22:16:32 +0000 | [diff] [blame] | 180 | SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag); |
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) { |
commit-bot@chromium.org | a2bd2d1 | 2014-01-30 22:16:32 +0000 | [diff] [blame] | 218 | SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag); |
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 | b591c77 | 2016-04-04 08:06:17 -0700 | [diff] [blame] | 251 | invalidBitmapSource.get(), validBitmapSource.get(), nullptr)); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 252 | |
| 253 | SkAutoTUnref<SkImageFilter> deserializedFilter( |
| 254 | TestFlattenableSerialization<SkImageFilter>( |
reed | cfb6bdf | 2016-03-29 11:32:50 -0700 | [diff] [blame] | 255 | xfermodeImageFilter.get(), shouldSucceed, reporter)); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 256 | |
| 257 | // Try to render a small bitmap using the invalid deserialized filter |
| 258 | // to make sure we don't crash while trying to render it |
| 259 | if (shouldSucceed) { |
| 260 | SkBitmap bitmap; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 261 | bitmap.allocN32Pixels(24, 24); |
| 262 | SkCanvas canvas(bitmap); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 263 | canvas.clear(0x00000000); |
| 264 | SkPaint paint; |
| 265 | paint.setImageFilter(deserializedFilter); |
| 266 | canvas.clipRect(SkRect::MakeXYWH(0, 0, SkIntToScalar(24), SkIntToScalar(24))); |
| 267 | canvas.drawBitmap(bitmap, 0, 0, &paint); |
| 268 | } |
| 269 | } |
| 270 | |
senorblanco | 0f7197b | 2014-09-24 11:09:38 -0700 | [diff] [blame] | 271 | static void TestXfermodeSerialization(skiatest::Reporter* reporter) { |
| 272 | for (size_t i = 0; i <= SkXfermode::kLastMode; ++i) { |
| 273 | if (i == SkXfermode::kSrcOver_Mode) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 274 | // skip SrcOver, as it is allowed to return nullptr from Create() |
senorblanco | 0f7197b | 2014-09-24 11:09:38 -0700 | [diff] [blame] | 275 | continue; |
| 276 | } |
reed | cfb6bdf | 2016-03-29 11:32:50 -0700 | [diff] [blame] | 277 | auto mode(SkXfermode::Make(static_cast<SkXfermode::Mode>(i))); |
| 278 | REPORTER_ASSERT(reporter, mode); |
senorblanco | 0f7197b | 2014-09-24 11:09:38 -0700 | [diff] [blame] | 279 | SkAutoTUnref<SkXfermode> copy( |
| 280 | TestFlattenableSerialization<SkXfermode>(mode.get(), true, reporter)); |
| 281 | } |
| 282 | } |
| 283 | |
senorblanco | 91c395a | 2014-09-25 15:51:35 -0700 | [diff] [blame] | 284 | static void TestColorFilterSerialization(skiatest::Reporter* reporter) { |
| 285 | uint8_t table[256]; |
| 286 | for (int i = 0; i < 256; ++i) { |
| 287 | table[i] = (i * 41) % 256; |
| 288 | } |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 289 | auto colorFilter(SkTableColorFilter::Make(table)); |
senorblanco | 91c395a | 2014-09-25 15:51:35 -0700 | [diff] [blame] | 290 | SkAutoTUnref<SkColorFilter> copy( |
| 291 | TestFlattenableSerialization<SkColorFilter>(colorFilter.get(), true, reporter)); |
| 292 | } |
| 293 | |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 294 | static SkBitmap draw_picture(SkPicture& picture) { |
| 295 | SkBitmap bitmap; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 296 | bitmap.allocN32Pixels(SkScalarCeilToInt(picture.cullRect().width()), |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 297 | SkScalarCeilToInt(picture.cullRect().height())); |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 298 | SkCanvas canvas(bitmap); |
robertphillips | c5ba71d | 2014-09-04 08:42:50 -0700 | [diff] [blame] | 299 | picture.playback(&canvas); |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 300 | return bitmap; |
| 301 | } |
| 302 | |
| 303 | static void compare_bitmaps(skiatest::Reporter* reporter, |
| 304 | const SkBitmap& b1, const SkBitmap& b2) { |
| 305 | REPORTER_ASSERT(reporter, b1.width() == b2.width()); |
| 306 | REPORTER_ASSERT(reporter, b1.height() == b2.height()); |
| 307 | SkAutoLockPixels autoLockPixels1(b1); |
| 308 | SkAutoLockPixels autoLockPixels2(b2); |
| 309 | |
| 310 | if ((b1.width() != b2.width()) || |
| 311 | (b1.height() != b2.height())) { |
| 312 | return; |
| 313 | } |
| 314 | |
| 315 | int pixelErrors = 0; |
| 316 | for (int y = 0; y < b2.height(); ++y) { |
| 317 | for (int x = 0; x < b2.width(); ++x) { |
| 318 | if (b1.getColor(x, y) != b2.getColor(x, y)) |
| 319 | ++pixelErrors; |
| 320 | } |
| 321 | } |
| 322 | REPORTER_ASSERT(reporter, 0 == pixelErrors); |
| 323 | } |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 324 | static void serialize_and_compare_typeface(SkTypeface* typeface, const char* text, |
| 325 | skiatest::Reporter* reporter) |
| 326 | { |
| 327 | // Create a paint with the typeface. |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 328 | SkPaint paint; |
| 329 | paint.setColor(SK_ColorGRAY); |
| 330 | paint.setTextSize(SkIntToScalar(30)); |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 331 | paint.setTypeface(typeface); |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 332 | |
| 333 | // Paint some text. |
| 334 | SkPictureRecorder recorder; |
| 335 | SkIRect canvasRect = SkIRect::MakeWH(kBitmapSize, kBitmapSize); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 336 | SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(canvasRect.width()), |
| 337 | SkIntToScalar(canvasRect.height()), |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 338 | nullptr, 0); |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 339 | canvas->drawColor(SK_ColorWHITE); |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 340 | canvas->drawText(text, 2, 24, 32, paint); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 341 | sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture()); |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 342 | |
| 343 | // Serlialize picture and create its clone from stream. |
| 344 | SkDynamicMemoryWStream stream; |
| 345 | picture->serialize(&stream); |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 346 | SkAutoTDelete<SkStream> inputStream(stream.detachAsStream()); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 347 | sk_sp<SkPicture> loadedPicture(SkPicture::MakeFromStream(inputStream.get())); |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 348 | |
| 349 | // Draw both original and clone picture and compare bitmaps -- they should be identical. |
| 350 | SkBitmap origBitmap = draw_picture(*picture); |
| 351 | SkBitmap destBitmap = draw_picture(*loadedPicture); |
| 352 | compare_bitmaps(reporter, origBitmap, destBitmap); |
| 353 | } |
| 354 | |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 355 | static void TestPictureTypefaceSerialization(skiatest::Reporter* reporter) { |
| 356 | { |
| 357 | // Load typeface from file to test CreateFromFile with index. |
| 358 | SkString filename = GetResourcePath("/fonts/test.ttc"); |
| 359 | SkAutoTUnref<SkTypeface> typeface(SkTypeface::CreateFromFile(filename.c_str(), 1)); |
| 360 | if (!typeface) { |
halcanary | 7d57124 | 2016-02-24 17:59:16 -0800 | [diff] [blame] | 361 | INFOF(reporter, "Could not run fontstream test because test.ttc not found."); |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 362 | } else { |
| 363 | serialize_and_compare_typeface(typeface, "A!", reporter); |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | { |
| 368 | // Load typeface as stream to create with axis settings. |
| 369 | SkAutoTDelete<SkStreamAsset> distortable(GetResourceAsStream("/fonts/Distortable.ttf")); |
| 370 | if (!distortable) { |
halcanary | 7d57124 | 2016-02-24 17:59:16 -0800 | [diff] [blame] | 371 | INFOF(reporter, "Could not run fontstream test because Distortable.ttf not found."); |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 372 | } else { |
| 373 | SkFixed axis = SK_FixedSqrt2; |
| 374 | SkAutoTUnref<SkTypeface> typeface(SkTypeface::CreateFromFontData( |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 375 | new SkFontData(distortable.release(), 0, &axis, 1))); |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 376 | if (!typeface) { |
halcanary | 7d57124 | 2016-02-24 17:59:16 -0800 | [diff] [blame] | 377 | INFOF(reporter, "Could not run fontstream test because Distortable.ttf not created."); |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 378 | } else { |
| 379 | serialize_and_compare_typeface(typeface, "abc", reporter); |
| 380 | } |
| 381 | } |
| 382 | } |
| 383 | } |
| 384 | |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 385 | static void setup_bitmap_for_canvas(SkBitmap* bitmap) { |
| 386 | bitmap->allocN32Pixels(kBitmapSize, kBitmapSize); |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 387 | } |
| 388 | |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 389 | static void make_checkerboard_bitmap(SkBitmap& bitmap) { |
| 390 | setup_bitmap_for_canvas(&bitmap); |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 391 | |
| 392 | SkCanvas canvas(bitmap); |
| 393 | canvas.clear(0x00000000); |
| 394 | SkPaint darkPaint; |
| 395 | darkPaint.setColor(0xFF804020); |
| 396 | SkPaint lightPaint; |
| 397 | lightPaint.setColor(0xFF244484); |
| 398 | const int i = kBitmapSize / 8; |
| 399 | const SkScalar f = SkIntToScalar(i); |
| 400 | for (int y = 0; y < kBitmapSize; y += i) { |
| 401 | for (int x = 0; x < kBitmapSize; x += i) { |
| 402 | canvas.save(); |
| 403 | canvas.translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 404 | canvas.drawRect(SkRect::MakeXYWH(0, 0, f, f), darkPaint); |
| 405 | canvas.drawRect(SkRect::MakeXYWH(f, 0, f, f), lightPaint); |
| 406 | canvas.drawRect(SkRect::MakeXYWH(0, f, f, f), lightPaint); |
| 407 | canvas.drawRect(SkRect::MakeXYWH(f, f, f, f), darkPaint); |
| 408 | canvas.restore(); |
| 409 | } |
| 410 | } |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 411 | } |
| 412 | |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 413 | static void draw_something(SkCanvas* canvas) { |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 414 | SkPaint paint; |
| 415 | SkBitmap bitmap; |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 416 | make_checkerboard_bitmap(bitmap); |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 417 | |
| 418 | canvas->save(); |
| 419 | canvas->scale(0.5f, 0.5f); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 420 | canvas->drawBitmap(bitmap, 0, 0, nullptr); |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 421 | canvas->restore(); |
| 422 | |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 423 | paint.setAntiAlias(true); |
| 424 | |
| 425 | paint.setColor(SK_ColorRED); |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 426 | 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] | 427 | paint.setColor(SK_ColorBLACK); |
| 428 | paint.setTextSize(SkIntToScalar(kBitmapSize/3)); |
| 429 | 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] | 430 | } |
| 431 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 432 | DEF_TEST(Serialization, reporter) { |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 433 | // Test matrix serialization |
| 434 | { |
| 435 | SkMatrix matrix = SkMatrix::I(); |
| 436 | TestObjectSerialization(&matrix, reporter); |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 437 | } |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 438 | |
| 439 | // Test path serialization |
| 440 | { |
| 441 | SkPath path; |
| 442 | TestObjectSerialization(&path, reporter); |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 443 | } |
sugoi@google.com | 305f78e | 2013-11-04 16:18:15 +0000 | [diff] [blame] | 444 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 445 | // Test region serialization |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 446 | { |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 447 | SkRegion region; |
| 448 | TestObjectSerialization(®ion, reporter); |
| 449 | } |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 450 | |
senorblanco | 0f7197b | 2014-09-24 11:09:38 -0700 | [diff] [blame] | 451 | // Test xfermode serialization |
| 452 | { |
| 453 | TestXfermodeSerialization(reporter); |
| 454 | } |
| 455 | |
senorblanco | 91c395a | 2014-09-25 15:51:35 -0700 | [diff] [blame] | 456 | // Test color filter serialization |
| 457 | { |
| 458 | TestColorFilterSerialization(reporter); |
| 459 | } |
| 460 | |
commit-bot@chromium.org | 1ac99c8 | 2014-04-29 15:35:23 +0000 | [diff] [blame] | 461 | // Test string serialization |
| 462 | { |
| 463 | SkString string("string"); |
| 464 | TestObjectSerializationNoAlign<SkString, false>(&string, reporter); |
| 465 | TestObjectSerializationNoAlign<SkString, true>(&string, reporter); |
| 466 | } |
| 467 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 468 | // Test rrect serialization |
| 469 | { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 470 | // SkRRect does not initialize anything. |
| 471 | // An uninitialized SkRRect can be serialized, |
| 472 | // but will branch on uninitialized data when deserialized. |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 473 | SkRRect rrect; |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 474 | SkRect rect = SkRect::MakeXYWH(1, 2, 20, 30); |
| 475 | SkVector corners[4] = { {1, 2}, {2, 3}, {3,4}, {4,5} }; |
| 476 | rrect.setRectRadii(rect, corners); |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 477 | TestAlignment(&rrect, reporter); |
| 478 | } |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 479 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 480 | // Test readByteArray |
| 481 | { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 482 | unsigned char data[kArraySize] = { 1, 2, 3 }; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 483 | TestArraySerialization(data, reporter); |
| 484 | } |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 485 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 486 | // Test readColorArray |
| 487 | { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 488 | SkColor data[kArraySize] = { SK_ColorBLACK, SK_ColorWHITE, SK_ColorRED }; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 489 | TestArraySerialization(data, reporter); |
| 490 | } |
| 491 | |
| 492 | // Test readIntArray |
| 493 | { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 494 | int32_t data[kArraySize] = { 1, 2, 4, 8 }; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 495 | TestArraySerialization(data, reporter); |
| 496 | } |
| 497 | |
| 498 | // Test readPointArray |
| 499 | { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 500 | SkPoint data[kArraySize] = { {6, 7}, {42, 128} }; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 501 | TestArraySerialization(data, reporter); |
| 502 | } |
| 503 | |
| 504 | // Test readScalarArray |
| 505 | { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 506 | SkScalar data[kArraySize] = { SK_Scalar1, SK_ScalarHalf, SK_ScalarMax }; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 507 | TestArraySerialization(data, reporter); |
commit-bot@chromium.org | 0251288 | 2013-10-31 18:37:50 +0000 | [diff] [blame] | 508 | } |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 509 | |
| 510 | // Test invalid deserializations |
| 511 | { |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 512 | SkImageInfo info = SkImageInfo::MakeN32Premul(kBitmapSize, kBitmapSize); |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 513 | |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 514 | SkBitmap validBitmap; |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 515 | validBitmap.setInfo(info); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 516 | |
| 517 | // Create a bitmap with a really large height |
| 518 | SkBitmap invalidBitmap; |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 519 | invalidBitmap.setInfo(info.makeWH(info.width(), 1000000000)); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 520 | |
| 521 | // The deserialization should succeed, and the rendering shouldn't crash, |
| 522 | // even when the device fails to initialize, due to its size |
| 523 | TestBitmapSerialization(validBitmap, invalidBitmap, true, reporter); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 524 | } |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 525 | |
| 526 | // Test simple SkPicture serialization |
| 527 | { |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 528 | SkPictureRecorder recorder; |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 529 | draw_something(recorder.beginRecording(SkIntToScalar(kBitmapSize), |
| 530 | SkIntToScalar(kBitmapSize), |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 531 | nullptr, 0)); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 532 | sk_sp<SkPicture> pict(recorder.finishRecordingAsPicture()); |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 533 | |
| 534 | // Serialize picture |
| 535 | SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag); |
| 536 | pict->flatten(writer); |
| 537 | size_t size = writer.bytesWritten(); |
commit-bot@chromium.org | 1e7ee99 | 2014-03-14 21:22:22 +0000 | [diff] [blame] | 538 | SkAutoTMalloc<unsigned char> data(size); |
| 539 | writer.writeToMemory(static_cast<void*>(data.get())); |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 540 | |
| 541 | // Deserialize picture |
commit-bot@chromium.org | 1e7ee99 | 2014-03-14 21:22:22 +0000 | [diff] [blame] | 542 | SkValidatingReadBuffer reader(static_cast<void*>(data.get()), size); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 543 | sk_sp<SkPicture> readPict(SkPicture::MakeFromBuffer(reader)); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 544 | REPORTER_ASSERT(reporter, readPict.get()); |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 545 | } |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 546 | |
| 547 | TestPictureTypefaceSerialization(reporter); |
commit-bot@chromium.org | 0251288 | 2013-10-31 18:37:50 +0000 | [diff] [blame] | 548 | } |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 549 | |
| 550 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 551 | #include "SkAnnotation.h" |
| 552 | |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 553 | static sk_sp<SkPicture> copy_picture_via_serialization(SkPicture* src) { |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 554 | SkDynamicMemoryWStream wstream; |
| 555 | src->serialize(&wstream); |
| 556 | SkAutoTDelete<SkStreamAsset> rstream(wstream.detachAsStream()); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 557 | return SkPicture::MakeFromStream(rstream); |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | struct AnnotationRec { |
| 561 | const SkRect fRect; |
| 562 | const char* fKey; |
| 563 | SkData* fValue; |
| 564 | }; |
| 565 | |
| 566 | class TestAnnotationCanvas : public SkCanvas { |
| 567 | skiatest::Reporter* fReporter; |
| 568 | const AnnotationRec* fRec; |
| 569 | int fCount; |
| 570 | int fCurrIndex; |
| 571 | |
| 572 | public: |
| 573 | TestAnnotationCanvas(skiatest::Reporter* reporter, const AnnotationRec rec[], int count) |
| 574 | : SkCanvas(100, 100) |
| 575 | , fReporter(reporter) |
| 576 | , fRec(rec) |
| 577 | , fCount(count) |
| 578 | , fCurrIndex(0) |
| 579 | {} |
| 580 | |
| 581 | ~TestAnnotationCanvas() { |
| 582 | REPORTER_ASSERT(fReporter, fCount == fCurrIndex); |
| 583 | } |
| 584 | |
| 585 | protected: |
| 586 | void onDrawAnnotation(const SkRect& rect, const char key[], SkData* value) { |
| 587 | REPORTER_ASSERT(fReporter, fCurrIndex < fCount); |
| 588 | REPORTER_ASSERT(fReporter, rect == fRec[fCurrIndex].fRect); |
| 589 | REPORTER_ASSERT(fReporter, !strcmp(key, fRec[fCurrIndex].fKey)); |
| 590 | REPORTER_ASSERT(fReporter, value->equals(fRec[fCurrIndex].fValue)); |
| 591 | fCurrIndex += 1; |
| 592 | } |
| 593 | }; |
| 594 | |
| 595 | /* |
| 596 | * Test the 3 annotation types by recording them into a picture, serializing, and then playing |
| 597 | * them back into another canvas. |
| 598 | */ |
| 599 | DEF_TEST(Annotations, reporter) { |
| 600 | SkPictureRecorder recorder; |
| 601 | SkCanvas* recordingCanvas = recorder.beginRecording(SkRect::MakeWH(100, 100)); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 602 | |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 603 | const char* str0 = "rect-with-url"; |
| 604 | const SkRect r0 = SkRect::MakeWH(10, 10); |
| 605 | SkAutoTUnref<SkData> d0(SkData::NewWithCString(str0)); |
| 606 | SkAnnotateRectWithURL(recordingCanvas, r0, d0); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 607 | |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 608 | const char* str1 = "named-destination"; |
| 609 | const SkRect r1 = SkRect::MakeXYWH(5, 5, 0, 0); // collapsed to a point |
| 610 | SkAutoTUnref<SkData> d1(SkData::NewWithCString(str1)); |
| 611 | SkAnnotateNamedDestination(recordingCanvas, {r1.x(), r1.y()}, d1); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 612 | |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 613 | const char* str2 = "link-to-destination"; |
| 614 | const SkRect r2 = SkRect::MakeXYWH(20, 20, 5, 6); |
| 615 | SkAutoTUnref<SkData> d2(SkData::NewWithCString(str2)); |
| 616 | SkAnnotateLinkToDestination(recordingCanvas, r2, d2); |
| 617 | |
| 618 | const AnnotationRec recs[] = { |
| 619 | { r0, SkAnnotationKeys::URL_Key(), d0 }, |
| 620 | { r1, SkAnnotationKeys::Define_Named_Dest_Key(), d1 }, |
| 621 | { r2, SkAnnotationKeys::Link_Named_Dest_Key(), d2 }, |
| 622 | }; |
| 623 | |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 624 | sk_sp<SkPicture> pict0(recorder.finishRecordingAsPicture()); |
| 625 | sk_sp<SkPicture> pict1(copy_picture_via_serialization(pict0.get())); |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 626 | |
| 627 | TestAnnotationCanvas canvas(reporter, recs, SK_ARRAY_COUNT(recs)); |
| 628 | canvas.drawPicture(pict1); |
| 629 | } |