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 | |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 8 | #include "Resources.h" |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 9 | #include "SkBitmapSource.h" |
| 10 | #include "SkCanvas.h" |
| 11 | #include "SkMallocPixelRef.h" |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 12 | #include "SkOSFile.h" |
robertphillips@google.com | 770963f | 2014-04-18 18:04:41 +0000 | [diff] [blame] | 13 | #include "SkPictureRecorder.h" |
senorblanco | 91c395a | 2014-09-25 15:51:35 -0700 | [diff] [blame] | 14 | #include "SkTableColorFilter.h" |
commit-bot@chromium.org | 1e7ee99 | 2014-03-14 21:22:22 +0000 | [diff] [blame] | 15 | #include "SkTemplates.h" |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 16 | #include "SkTypeface.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 17 | #include "SkWriteBuffer.h" |
commit-bot@chromium.org | 0251288 | 2013-10-31 18:37:50 +0000 | [diff] [blame] | 18 | #include "SkValidatingReadBuffer.h" |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 19 | #include "SkXfermodeImageFilter.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 20 | #include "Test.h" |
commit-bot@chromium.org | 0251288 | 2013-10-31 18:37:50 +0000 | [diff] [blame] | 21 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 22 | static const uint32_t kArraySize = 64; |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 23 | static const int kBitmapSize = 256; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 24 | |
| 25 | template<typename T> |
| 26 | static void TestAlignment(T* testObj, skiatest::Reporter* reporter) { |
| 27 | // Test memory read/write functions directly |
| 28 | unsigned char dataWritten[1024]; |
| 29 | size_t bytesWrittenToMemory = testObj->writeToMemory(dataWritten); |
| 30 | REPORTER_ASSERT(reporter, SkAlign4(bytesWrittenToMemory) == bytesWrittenToMemory); |
| 31 | size_t bytesReadFromMemory = testObj->readFromMemory(dataWritten, bytesWrittenToMemory); |
| 32 | REPORTER_ASSERT(reporter, SkAlign4(bytesReadFromMemory) == bytesReadFromMemory); |
| 33 | } |
| 34 | |
| 35 | template<typename T> struct SerializationUtils { |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 36 | // Generic case for flattenables |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 37 | static void Write(SkWriteBuffer& writer, const T* flattenable) { |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 38 | writer.writeFlattenable(flattenable); |
| 39 | } |
| 40 | static void Read(SkValidatingReadBuffer& reader, T** flattenable) { |
| 41 | *flattenable = (T*)reader.readFlattenable(T::GetFlattenableType()); |
| 42 | } |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | template<> struct SerializationUtils<SkMatrix> { |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 46 | static void Write(SkWriteBuffer& writer, const SkMatrix* matrix) { |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 47 | writer.writeMatrix(*matrix); |
| 48 | } |
| 49 | static void Read(SkValidatingReadBuffer& reader, SkMatrix* matrix) { |
| 50 | reader.readMatrix(matrix); |
| 51 | } |
| 52 | }; |
| 53 | |
| 54 | template<> struct SerializationUtils<SkPath> { |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 55 | static void Write(SkWriteBuffer& writer, const SkPath* path) { |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 56 | writer.writePath(*path); |
| 57 | } |
| 58 | static void Read(SkValidatingReadBuffer& reader, SkPath* path) { |
| 59 | reader.readPath(path); |
| 60 | } |
| 61 | }; |
| 62 | |
| 63 | template<> struct SerializationUtils<SkRegion> { |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 64 | static void Write(SkWriteBuffer& writer, const SkRegion* region) { |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 65 | writer.writeRegion(*region); |
| 66 | } |
| 67 | static void Read(SkValidatingReadBuffer& reader, SkRegion* region) { |
| 68 | reader.readRegion(region); |
| 69 | } |
| 70 | }; |
| 71 | |
commit-bot@chromium.org | 1ac99c8 | 2014-04-29 15:35:23 +0000 | [diff] [blame] | 72 | template<> struct SerializationUtils<SkString> { |
| 73 | static void Write(SkWriteBuffer& writer, const SkString* string) { |
| 74 | writer.writeString(string->c_str()); |
| 75 | } |
| 76 | static void Read(SkValidatingReadBuffer& reader, SkString* string) { |
| 77 | reader.readString(string); |
| 78 | } |
| 79 | }; |
| 80 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 81 | template<> struct SerializationUtils<unsigned char> { |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 82 | static void Write(SkWriteBuffer& writer, unsigned char* data, uint32_t arraySize) { |
reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame] | 83 | writer.writeByteArray(data, arraySize); |
sugoi@google.com | 305f78e | 2013-11-04 16:18:15 +0000 | [diff] [blame] | 84 | } |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 85 | static bool Read(SkValidatingReadBuffer& reader, unsigned char* data, uint32_t arraySize) { |
| 86 | return reader.readByteArray(data, arraySize); |
| 87 | } |
| 88 | }; |
commit-bot@chromium.org | 0251288 | 2013-10-31 18:37:50 +0000 | [diff] [blame] | 89 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 90 | template<> struct SerializationUtils<SkColor> { |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 91 | static void Write(SkWriteBuffer& writer, SkColor* data, uint32_t arraySize) { |
reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame] | 92 | writer.writeColorArray(data, arraySize); |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 93 | } |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 94 | static bool Read(SkValidatingReadBuffer& reader, SkColor* data, uint32_t arraySize) { |
| 95 | return reader.readColorArray(data, arraySize); |
| 96 | } |
| 97 | }; |
sugoi@google.com | 305f78e | 2013-11-04 16:18:15 +0000 | [diff] [blame] | 98 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 99 | template<> struct SerializationUtils<int32_t> { |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 100 | static void Write(SkWriteBuffer& writer, int32_t* data, uint32_t arraySize) { |
reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame] | 101 | writer.writeIntArray(data, arraySize); |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 102 | } |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 103 | static bool Read(SkValidatingReadBuffer& reader, int32_t* data, uint32_t arraySize) { |
| 104 | return reader.readIntArray(data, arraySize); |
| 105 | } |
| 106 | }; |
sugoi@google.com | 305f78e | 2013-11-04 16:18:15 +0000 | [diff] [blame] | 107 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 108 | template<> struct SerializationUtils<SkPoint> { |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 109 | static void Write(SkWriteBuffer& writer, SkPoint* data, uint32_t arraySize) { |
reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame] | 110 | writer.writePointArray(data, arraySize); |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 111 | } |
| 112 | static bool Read(SkValidatingReadBuffer& reader, SkPoint* data, uint32_t arraySize) { |
| 113 | return reader.readPointArray(data, arraySize); |
| 114 | } |
| 115 | }; |
reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame] | 116 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 117 | template<> struct SerializationUtils<SkScalar> { |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 118 | static void Write(SkWriteBuffer& writer, SkScalar* data, uint32_t arraySize) { |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 119 | writer.writeScalarArray(data, arraySize); |
| 120 | } |
| 121 | static bool Read(SkValidatingReadBuffer& reader, SkScalar* data, uint32_t arraySize) { |
| 122 | return reader.readScalarArray(data, arraySize); |
| 123 | } |
| 124 | }; |
reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame] | 125 | |
commit-bot@chromium.org | 1ac99c8 | 2014-04-29 15:35:23 +0000 | [diff] [blame] | 126 | template<typename T, bool testInvalid> struct SerializationTestUtils { |
| 127 | static void InvalidateData(unsigned char* data) {} |
| 128 | }; |
| 129 | |
| 130 | template<> struct SerializationTestUtils<SkString, true> { |
| 131 | static void InvalidateData(unsigned char* data) { |
| 132 | data[3] |= 0x80; // Reverse sign of 1st integer |
| 133 | } |
| 134 | }; |
| 135 | |
| 136 | template<typename T, bool testInvalid> |
| 137 | static void TestObjectSerializationNoAlign(T* testObj, skiatest::Reporter* reporter) { |
commit-bot@chromium.org | a2bd2d1 | 2014-01-30 22:16:32 +0000 | [diff] [blame] | 138 | SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag); |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 139 | SerializationUtils<T>::Write(writer, testObj); |
| 140 | size_t bytesWritten = writer.bytesWritten(); |
| 141 | REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten); |
reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame] | 142 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 143 | unsigned char dataWritten[1024]; |
| 144 | writer.writeToMemory(dataWritten); |
| 145 | |
commit-bot@chromium.org | 1ac99c8 | 2014-04-29 15:35:23 +0000 | [diff] [blame] | 146 | SerializationTestUtils<T, testInvalid>::InvalidateData(dataWritten); |
| 147 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 148 | // Make sure this fails when it should (test with smaller size, but still multiple of 4) |
| 149 | SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 4); |
commit-bot@chromium.org | 8f457e3 | 2013-11-08 19:22:57 +0000 | [diff] [blame] | 150 | T obj; |
| 151 | SerializationUtils<T>::Read(buffer, &obj); |
commit-bot@chromium.org | c2e9db3 | 2013-12-06 20:14:46 +0000 | [diff] [blame] | 152 | REPORTER_ASSERT(reporter, !buffer.isValid()); |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 153 | |
| 154 | // Make sure this succeeds when it should |
| 155 | SkValidatingReadBuffer buffer2(dataWritten, bytesWritten); |
commit-bot@chromium.org | 8f457e3 | 2013-11-08 19:22:57 +0000 | [diff] [blame] | 156 | const unsigned char* peekBefore = static_cast<const unsigned char*>(buffer2.skip(0)); |
| 157 | T obj2; |
| 158 | SerializationUtils<T>::Read(buffer2, &obj2); |
| 159 | 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] | 160 | // 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] | 161 | REPORTER_ASSERT(reporter, buffer2.isValid() == !testInvalid); |
| 162 | // Note: This following test should always succeed, regardless of whether the buffer is valid, |
| 163 | // 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] | 164 | REPORTER_ASSERT(reporter, static_cast<size_t>(peekAfter - peekBefore) == bytesWritten); |
commit-bot@chromium.org | 1ac99c8 | 2014-04-29 15:35:23 +0000 | [diff] [blame] | 165 | } |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 166 | |
commit-bot@chromium.org | 1ac99c8 | 2014-04-29 15:35:23 +0000 | [diff] [blame] | 167 | template<typename T> |
| 168 | static void TestObjectSerialization(T* testObj, skiatest::Reporter* reporter) { |
| 169 | TestObjectSerializationNoAlign<T, false>(testObj, reporter); |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 170 | TestAlignment(testObj, reporter); |
| 171 | } |
| 172 | |
| 173 | template<typename T> |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 174 | static T* TestFlattenableSerialization(T* testObj, bool shouldSucceed, |
| 175 | skiatest::Reporter* reporter) { |
commit-bot@chromium.org | a2bd2d1 | 2014-01-30 22:16:32 +0000 | [diff] [blame] | 176 | SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 177 | SerializationUtils<T>::Write(writer, testObj); |
| 178 | size_t bytesWritten = writer.bytesWritten(); |
| 179 | REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten); |
| 180 | |
senorblanco | 91c395a | 2014-09-25 15:51:35 -0700 | [diff] [blame] | 181 | unsigned char dataWritten[4096]; |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 182 | SkASSERT(bytesWritten <= sizeof(dataWritten)); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 183 | writer.writeToMemory(dataWritten); |
| 184 | |
| 185 | // Make sure this fails when it should (test with smaller size, but still multiple of 4) |
| 186 | SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 4); |
| 187 | T* obj = NULL; |
| 188 | SerializationUtils<T>::Read(buffer, &obj); |
commit-bot@chromium.org | c2e9db3 | 2013-12-06 20:14:46 +0000 | [diff] [blame] | 189 | REPORTER_ASSERT(reporter, !buffer.isValid()); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 190 | REPORTER_ASSERT(reporter, NULL == obj); |
| 191 | |
| 192 | // Make sure this succeeds when it should |
| 193 | SkValidatingReadBuffer buffer2(dataWritten, bytesWritten); |
| 194 | const unsigned char* peekBefore = static_cast<const unsigned char*>(buffer2.skip(0)); |
| 195 | T* obj2 = NULL; |
| 196 | SerializationUtils<T>::Read(buffer2, &obj2); |
| 197 | const unsigned char* peekAfter = static_cast<const unsigned char*>(buffer2.skip(0)); |
| 198 | if (shouldSucceed) { |
| 199 | // 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] | 200 | REPORTER_ASSERT(reporter, buffer2.isValid()); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 201 | REPORTER_ASSERT(reporter, static_cast<size_t>(peekAfter - peekBefore) == bytesWritten); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 202 | REPORTER_ASSERT(reporter, obj2); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 203 | } else { |
| 204 | // 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] | 205 | REPORTER_ASSERT(reporter, !buffer.isValid()); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 206 | REPORTER_ASSERT(reporter, NULL == obj2); |
| 207 | } |
| 208 | |
| 209 | return obj2; // Return object to perform further validity tests on it |
| 210 | } |
| 211 | |
| 212 | template<typename T> |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 213 | static void TestArraySerialization(T* data, skiatest::Reporter* reporter) { |
commit-bot@chromium.org | a2bd2d1 | 2014-01-30 22:16:32 +0000 | [diff] [blame] | 214 | SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag); |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 215 | SerializationUtils<T>::Write(writer, data, kArraySize); |
| 216 | size_t bytesWritten = writer.bytesWritten(); |
| 217 | // This should write the length (in 4 bytes) and the array |
| 218 | REPORTER_ASSERT(reporter, (4 + kArraySize * sizeof(T)) == bytesWritten); |
| 219 | |
| 220 | unsigned char dataWritten[1024]; |
| 221 | writer.writeToMemory(dataWritten); |
| 222 | |
| 223 | // Make sure this fails when it should |
| 224 | SkValidatingReadBuffer buffer(dataWritten, bytesWritten); |
| 225 | T dataRead[kArraySize]; |
| 226 | bool success = SerializationUtils<T>::Read(buffer, dataRead, kArraySize / 2); |
| 227 | // This should have failed, since the provided size was too small |
| 228 | REPORTER_ASSERT(reporter, !success); |
| 229 | |
| 230 | // Make sure this succeeds when it should |
| 231 | SkValidatingReadBuffer buffer2(dataWritten, bytesWritten); |
| 232 | success = SerializationUtils<T>::Read(buffer2, dataRead, kArraySize); |
| 233 | // This should have succeeded, since there are enough bytes to read this |
| 234 | REPORTER_ASSERT(reporter, success); |
| 235 | } |
| 236 | |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 237 | static void TestBitmapSerialization(const SkBitmap& validBitmap, |
| 238 | const SkBitmap& invalidBitmap, |
| 239 | bool shouldSucceed, |
| 240 | skiatest::Reporter* reporter) { |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame] | 241 | SkAutoTUnref<SkBitmapSource> validBitmapSource(SkBitmapSource::Create(validBitmap)); |
| 242 | SkAutoTUnref<SkBitmapSource> invalidBitmapSource(SkBitmapSource::Create(invalidBitmap)); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 243 | SkAutoTUnref<SkXfermode> mode(SkXfermode::Create(SkXfermode::kSrcOver_Mode)); |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame] | 244 | SkAutoTUnref<SkXfermodeImageFilter> xfermodeImageFilter( |
| 245 | SkXfermodeImageFilter::Create(mode, invalidBitmapSource, validBitmapSource)); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 246 | |
| 247 | SkAutoTUnref<SkImageFilter> deserializedFilter( |
| 248 | TestFlattenableSerialization<SkImageFilter>( |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame] | 249 | xfermodeImageFilter, shouldSucceed, reporter)); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 250 | |
| 251 | // Try to render a small bitmap using the invalid deserialized filter |
| 252 | // to make sure we don't crash while trying to render it |
| 253 | if (shouldSucceed) { |
| 254 | SkBitmap bitmap; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 255 | bitmap.allocN32Pixels(24, 24); |
| 256 | SkCanvas canvas(bitmap); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 257 | canvas.clear(0x00000000); |
| 258 | SkPaint paint; |
| 259 | paint.setImageFilter(deserializedFilter); |
| 260 | canvas.clipRect(SkRect::MakeXYWH(0, 0, SkIntToScalar(24), SkIntToScalar(24))); |
| 261 | canvas.drawBitmap(bitmap, 0, 0, &paint); |
| 262 | } |
| 263 | } |
| 264 | |
senorblanco | 0f7197b | 2014-09-24 11:09:38 -0700 | [diff] [blame] | 265 | static void TestXfermodeSerialization(skiatest::Reporter* reporter) { |
| 266 | for (size_t i = 0; i <= SkXfermode::kLastMode; ++i) { |
| 267 | if (i == SkXfermode::kSrcOver_Mode) { |
| 268 | // skip SrcOver, as it is allowed to return NULL from Create() |
| 269 | continue; |
| 270 | } |
| 271 | SkAutoTUnref<SkXfermode> mode(SkXfermode::Create(static_cast<SkXfermode::Mode>(i))); |
| 272 | REPORTER_ASSERT(reporter, mode.get()); |
| 273 | SkAutoTUnref<SkXfermode> copy( |
| 274 | TestFlattenableSerialization<SkXfermode>(mode.get(), true, reporter)); |
| 275 | } |
| 276 | } |
| 277 | |
senorblanco | 91c395a | 2014-09-25 15:51:35 -0700 | [diff] [blame] | 278 | static void TestColorFilterSerialization(skiatest::Reporter* reporter) { |
| 279 | uint8_t table[256]; |
| 280 | for (int i = 0; i < 256; ++i) { |
| 281 | table[i] = (i * 41) % 256; |
| 282 | } |
| 283 | SkAutoTUnref<SkColorFilter> colorFilter(SkTableColorFilter::Create(table)); |
| 284 | SkAutoTUnref<SkColorFilter> copy( |
| 285 | TestFlattenableSerialization<SkColorFilter>(colorFilter.get(), true, reporter)); |
| 286 | } |
| 287 | |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 288 | static SkBitmap draw_picture(SkPicture& picture) { |
| 289 | SkBitmap bitmap; |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 290 | bitmap.allocN32Pixels(SkScalarCeilToInt(picture.cullRect().width()), |
| 291 | SkScalarCeilToInt(picture.cullRect().height())); |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 292 | SkCanvas canvas(bitmap); |
robertphillips | c5ba71d | 2014-09-04 08:42:50 -0700 | [diff] [blame] | 293 | picture.playback(&canvas); |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 294 | return bitmap; |
| 295 | } |
| 296 | |
| 297 | static void compare_bitmaps(skiatest::Reporter* reporter, |
| 298 | const SkBitmap& b1, const SkBitmap& b2) { |
| 299 | REPORTER_ASSERT(reporter, b1.width() == b2.width()); |
| 300 | REPORTER_ASSERT(reporter, b1.height() == b2.height()); |
| 301 | SkAutoLockPixels autoLockPixels1(b1); |
| 302 | SkAutoLockPixels autoLockPixels2(b2); |
| 303 | |
| 304 | if ((b1.width() != b2.width()) || |
| 305 | (b1.height() != b2.height())) { |
| 306 | return; |
| 307 | } |
| 308 | |
| 309 | int pixelErrors = 0; |
| 310 | for (int y = 0; y < b2.height(); ++y) { |
| 311 | for (int x = 0; x < b2.width(); ++x) { |
| 312 | if (b1.getColor(x, y) != b2.getColor(x, y)) |
| 313 | ++pixelErrors; |
| 314 | } |
| 315 | } |
| 316 | REPORTER_ASSERT(reporter, 0 == pixelErrors); |
| 317 | } |
| 318 | |
| 319 | static void TestPictureTypefaceSerialization(skiatest::Reporter* reporter) { |
| 320 | // Load typeface form file. |
| 321 | // This test cannot run if there is no resource path. |
| 322 | SkString resourcePath = GetResourcePath(); |
| 323 | if (resourcePath.isEmpty()) { |
| 324 | SkDebugf("Could not run fontstream test because resourcePath not specified."); |
| 325 | return; |
| 326 | } |
tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 327 | SkString filename = SkOSPath::Join(resourcePath.c_str(), "test.ttc"); |
bungeman | d71b757 | 2014-09-18 10:55:32 -0700 | [diff] [blame] | 328 | SkTypeface* typeface = SkTypeface::CreateFromFile(filename.c_str(), 1); |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 329 | if (!typeface) { |
| 330 | SkDebugf("Could not run fontstream test because test.ttc not found."); |
| 331 | return; |
| 332 | } |
| 333 | |
| 334 | // Create a paint with the typeface we loaded. |
| 335 | SkPaint paint; |
| 336 | paint.setColor(SK_ColorGRAY); |
| 337 | paint.setTextSize(SkIntToScalar(30)); |
| 338 | SkSafeUnref(paint.setTypeface(typeface)); |
| 339 | |
| 340 | // Paint some text. |
| 341 | SkPictureRecorder recorder; |
| 342 | SkIRect canvasRect = SkIRect::MakeWH(kBitmapSize, kBitmapSize); |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 343 | SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(canvasRect.width()), |
| 344 | SkIntToScalar(canvasRect.height()), |
| 345 | NULL, 0); |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 346 | canvas->drawColor(SK_ColorWHITE); |
bungeman | d71b757 | 2014-09-18 10:55:32 -0700 | [diff] [blame] | 347 | canvas->drawText("A!", 2, 24, 32, paint); |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 348 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| 349 | |
| 350 | // Serlialize picture and create its clone from stream. |
| 351 | SkDynamicMemoryWStream stream; |
| 352 | picture->serialize(&stream); |
| 353 | SkAutoTUnref<SkStream> inputStream(stream.detachAsStream()); |
| 354 | SkAutoTUnref<SkPicture> loadedPicture(SkPicture::CreateFromStream(inputStream.get())); |
| 355 | |
| 356 | // Draw both original and clone picture and compare bitmaps -- they should be identical. |
| 357 | SkBitmap origBitmap = draw_picture(*picture); |
| 358 | SkBitmap destBitmap = draw_picture(*loadedPicture); |
| 359 | compare_bitmaps(reporter, origBitmap, destBitmap); |
| 360 | } |
| 361 | |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 362 | static void setup_bitmap_for_canvas(SkBitmap* bitmap) { |
| 363 | bitmap->allocN32Pixels(kBitmapSize, kBitmapSize); |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 364 | } |
| 365 | |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 366 | static void make_checkerboard_bitmap(SkBitmap& bitmap) { |
| 367 | setup_bitmap_for_canvas(&bitmap); |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 368 | |
| 369 | SkCanvas canvas(bitmap); |
| 370 | canvas.clear(0x00000000); |
| 371 | SkPaint darkPaint; |
| 372 | darkPaint.setColor(0xFF804020); |
| 373 | SkPaint lightPaint; |
| 374 | lightPaint.setColor(0xFF244484); |
| 375 | const int i = kBitmapSize / 8; |
| 376 | const SkScalar f = SkIntToScalar(i); |
| 377 | for (int y = 0; y < kBitmapSize; y += i) { |
| 378 | for (int x = 0; x < kBitmapSize; x += i) { |
| 379 | canvas.save(); |
| 380 | canvas.translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 381 | canvas.drawRect(SkRect::MakeXYWH(0, 0, f, f), darkPaint); |
| 382 | canvas.drawRect(SkRect::MakeXYWH(f, 0, f, f), lightPaint); |
| 383 | canvas.drawRect(SkRect::MakeXYWH(0, f, f, f), lightPaint); |
| 384 | canvas.drawRect(SkRect::MakeXYWH(f, f, f, f), darkPaint); |
| 385 | canvas.restore(); |
| 386 | } |
| 387 | } |
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 draw_something(SkCanvas* canvas) { |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 391 | SkPaint paint; |
| 392 | SkBitmap bitmap; |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 393 | make_checkerboard_bitmap(bitmap); |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 394 | |
| 395 | canvas->save(); |
| 396 | canvas->scale(0.5f, 0.5f); |
| 397 | canvas->drawBitmap(bitmap, 0, 0, NULL); |
| 398 | canvas->restore(); |
| 399 | |
| 400 | const char beforeStr[] = "before circle"; |
| 401 | const char afterStr[] = "after circle"; |
| 402 | |
| 403 | paint.setAntiAlias(true); |
| 404 | |
| 405 | paint.setColor(SK_ColorRED); |
| 406 | canvas->drawData(beforeStr, sizeof(beforeStr)); |
| 407 | canvas->drawCircle(SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/3), paint); |
| 408 | canvas->drawData(afterStr, sizeof(afterStr)); |
| 409 | paint.setColor(SK_ColorBLACK); |
| 410 | paint.setTextSize(SkIntToScalar(kBitmapSize/3)); |
| 411 | 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] | 412 | } |
| 413 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 414 | DEF_TEST(Serialization, reporter) { |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 415 | // Test matrix serialization |
| 416 | { |
| 417 | SkMatrix matrix = SkMatrix::I(); |
| 418 | TestObjectSerialization(&matrix, reporter); |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 419 | } |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 420 | |
| 421 | // Test path serialization |
| 422 | { |
| 423 | SkPath path; |
| 424 | TestObjectSerialization(&path, reporter); |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 425 | } |
sugoi@google.com | 305f78e | 2013-11-04 16:18:15 +0000 | [diff] [blame] | 426 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 427 | // Test region serialization |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 428 | { |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 429 | SkRegion region; |
| 430 | TestObjectSerialization(®ion, reporter); |
| 431 | } |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 432 | |
senorblanco | 0f7197b | 2014-09-24 11:09:38 -0700 | [diff] [blame] | 433 | // Test xfermode serialization |
| 434 | { |
| 435 | TestXfermodeSerialization(reporter); |
| 436 | } |
| 437 | |
senorblanco | 91c395a | 2014-09-25 15:51:35 -0700 | [diff] [blame] | 438 | // Test color filter serialization |
| 439 | { |
| 440 | TestColorFilterSerialization(reporter); |
| 441 | } |
| 442 | |
commit-bot@chromium.org | 1ac99c8 | 2014-04-29 15:35:23 +0000 | [diff] [blame] | 443 | // Test string serialization |
| 444 | { |
| 445 | SkString string("string"); |
| 446 | TestObjectSerializationNoAlign<SkString, false>(&string, reporter); |
| 447 | TestObjectSerializationNoAlign<SkString, true>(&string, reporter); |
| 448 | } |
| 449 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 450 | // Test rrect serialization |
| 451 | { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 452 | // SkRRect does not initialize anything. |
| 453 | // An uninitialized SkRRect can be serialized, |
| 454 | // but will branch on uninitialized data when deserialized. |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 455 | SkRRect rrect; |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 456 | SkRect rect = SkRect::MakeXYWH(1, 2, 20, 30); |
| 457 | SkVector corners[4] = { {1, 2}, {2, 3}, {3,4}, {4,5} }; |
| 458 | rrect.setRectRadii(rect, corners); |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 459 | TestAlignment(&rrect, reporter); |
| 460 | } |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 461 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 462 | // Test readByteArray |
| 463 | { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 464 | unsigned char data[kArraySize] = { 1, 2, 3 }; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 465 | TestArraySerialization(data, reporter); |
| 466 | } |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 467 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 468 | // Test readColorArray |
| 469 | { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 470 | SkColor data[kArraySize] = { SK_ColorBLACK, SK_ColorWHITE, SK_ColorRED }; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 471 | TestArraySerialization(data, reporter); |
| 472 | } |
| 473 | |
| 474 | // Test readIntArray |
| 475 | { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 476 | int32_t data[kArraySize] = { 1, 2, 4, 8 }; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 477 | TestArraySerialization(data, reporter); |
| 478 | } |
| 479 | |
| 480 | // Test readPointArray |
| 481 | { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 482 | SkPoint data[kArraySize] = { {6, 7}, {42, 128} }; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 483 | TestArraySerialization(data, reporter); |
| 484 | } |
| 485 | |
| 486 | // Test readScalarArray |
| 487 | { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 488 | SkScalar data[kArraySize] = { SK_Scalar1, SK_ScalarHalf, SK_ScalarMax }; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 489 | TestArraySerialization(data, reporter); |
commit-bot@chromium.org | 0251288 | 2013-10-31 18:37:50 +0000 | [diff] [blame] | 490 | } |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 491 | |
| 492 | // Test invalid deserializations |
| 493 | { |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 494 | SkImageInfo info = SkImageInfo::MakeN32Premul(kBitmapSize, kBitmapSize); |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 495 | |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 496 | SkBitmap validBitmap; |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 497 | validBitmap.setInfo(info); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 498 | |
| 499 | // Create a bitmap with a really large height |
| 500 | SkBitmap invalidBitmap; |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 501 | invalidBitmap.setInfo(info.makeWH(info.width(), 1000000000)); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 502 | |
| 503 | // The deserialization should succeed, and the rendering shouldn't crash, |
| 504 | // even when the device fails to initialize, due to its size |
| 505 | TestBitmapSerialization(validBitmap, invalidBitmap, true, reporter); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 506 | } |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 507 | |
| 508 | // Test simple SkPicture serialization |
| 509 | { |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 510 | SkPictureRecorder recorder; |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 511 | draw_something(recorder.beginRecording(SkIntToScalar(kBitmapSize), |
| 512 | SkIntToScalar(kBitmapSize), |
| 513 | NULL, 0)); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 514 | SkAutoTUnref<SkPicture> pict(recorder.endRecording()); |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 515 | |
| 516 | // Serialize picture |
| 517 | SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag); |
| 518 | pict->flatten(writer); |
| 519 | size_t size = writer.bytesWritten(); |
commit-bot@chromium.org | 1e7ee99 | 2014-03-14 21:22:22 +0000 | [diff] [blame] | 520 | SkAutoTMalloc<unsigned char> data(size); |
| 521 | writer.writeToMemory(static_cast<void*>(data.get())); |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 522 | |
| 523 | // Deserialize picture |
commit-bot@chromium.org | 1e7ee99 | 2014-03-14 21:22:22 +0000 | [diff] [blame] | 524 | SkValidatingReadBuffer reader(static_cast<void*>(data.get()), size); |
commit-bot@chromium.org | e5eee51 | 2014-03-13 16:18:49 +0000 | [diff] [blame] | 525 | SkAutoTUnref<SkPicture> readPict( |
| 526 | SkPicture::CreateFromBuffer(reader)); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 527 | REPORTER_ASSERT(reporter, readPict.get()); |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 528 | } |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 529 | |
| 530 | TestPictureTypefaceSerialization(reporter); |
commit-bot@chromium.org | 0251288 | 2013-10-31 18:37:50 +0000 | [diff] [blame] | 531 | } |