| 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 | |
| commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 8 | #include "SkBitmapDevice.h" |
| 9 | #include "SkBitmapSource.h" |
| 10 | #include "SkCanvas.h" |
| 11 | #include "SkMallocPixelRef.h" |
| commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 12 | #include "SkWriteBuffer.h" |
| commit-bot@chromium.org | 0251288 | 2013-10-31 18:37:50 +0000 | [diff] [blame] | 13 | #include "SkValidatingReadBuffer.h" |
| commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 14 | #include "SkXfermodeImageFilter.h" |
| tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 15 | #include "Test.h" |
| commit-bot@chromium.org | 0251288 | 2013-10-31 18:37:50 +0000 | [diff] [blame] | 16 | |
| commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 17 | static const uint32_t kArraySize = 64; |
| 18 | |
| 19 | template<typename T> |
| 20 | static void TestAlignment(T* testObj, skiatest::Reporter* reporter) { |
| 21 | // Test memory read/write functions directly |
| 22 | unsigned char dataWritten[1024]; |
| 23 | size_t bytesWrittenToMemory = testObj->writeToMemory(dataWritten); |
| 24 | REPORTER_ASSERT(reporter, SkAlign4(bytesWrittenToMemory) == bytesWrittenToMemory); |
| 25 | size_t bytesReadFromMemory = testObj->readFromMemory(dataWritten, bytesWrittenToMemory); |
| 26 | REPORTER_ASSERT(reporter, SkAlign4(bytesReadFromMemory) == bytesReadFromMemory); |
| 27 | } |
| 28 | |
| 29 | template<typename T> struct SerializationUtils { |
| commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 30 | // Generic case for flattenables |
| commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 31 | static void Write(SkWriteBuffer& writer, const T* flattenable) { |
| commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 32 | writer.writeFlattenable(flattenable); |
| 33 | } |
| 34 | static void Read(SkValidatingReadBuffer& reader, T** flattenable) { |
| 35 | *flattenable = (T*)reader.readFlattenable(T::GetFlattenableType()); |
| 36 | } |
| commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | template<> struct SerializationUtils<SkMatrix> { |
| commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 40 | static void Write(SkWriteBuffer& writer, const SkMatrix* matrix) { |
| commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 41 | writer.writeMatrix(*matrix); |
| 42 | } |
| 43 | static void Read(SkValidatingReadBuffer& reader, SkMatrix* matrix) { |
| 44 | reader.readMatrix(matrix); |
| 45 | } |
| 46 | }; |
| 47 | |
| 48 | template<> struct SerializationUtils<SkPath> { |
| commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 49 | static void Write(SkWriteBuffer& writer, const SkPath* path) { |
| commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 50 | writer.writePath(*path); |
| 51 | } |
| 52 | static void Read(SkValidatingReadBuffer& reader, SkPath* path) { |
| 53 | reader.readPath(path); |
| 54 | } |
| 55 | }; |
| 56 | |
| 57 | template<> struct SerializationUtils<SkRegion> { |
| commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 58 | static void Write(SkWriteBuffer& writer, const SkRegion* region) { |
| commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 59 | writer.writeRegion(*region); |
| 60 | } |
| 61 | static void Read(SkValidatingReadBuffer& reader, SkRegion* region) { |
| 62 | reader.readRegion(region); |
| 63 | } |
| 64 | }; |
| 65 | |
| 66 | template<> struct SerializationUtils<unsigned char> { |
| commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 67 | static void Write(SkWriteBuffer& writer, unsigned char* data, uint32_t arraySize) { |
| reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame] | 68 | writer.writeByteArray(data, arraySize); |
| sugoi@google.com | 305f78e | 2013-11-04 16:18:15 +0000 | [diff] [blame] | 69 | } |
| commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 70 | static bool Read(SkValidatingReadBuffer& reader, unsigned char* data, uint32_t arraySize) { |
| 71 | return reader.readByteArray(data, arraySize); |
| 72 | } |
| 73 | }; |
| commit-bot@chromium.org | 0251288 | 2013-10-31 18:37:50 +0000 | [diff] [blame] | 74 | |
| commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 75 | template<> struct SerializationUtils<SkColor> { |
| commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 76 | static void Write(SkWriteBuffer& writer, SkColor* data, uint32_t arraySize) { |
| reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame] | 77 | writer.writeColorArray(data, arraySize); |
| sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 78 | } |
| commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 79 | static bool Read(SkValidatingReadBuffer& reader, SkColor* data, uint32_t arraySize) { |
| 80 | return reader.readColorArray(data, arraySize); |
| 81 | } |
| 82 | }; |
| sugoi@google.com | 305f78e | 2013-11-04 16:18:15 +0000 | [diff] [blame] | 83 | |
| commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 84 | template<> struct SerializationUtils<int32_t> { |
| commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 85 | static void Write(SkWriteBuffer& writer, int32_t* data, uint32_t arraySize) { |
| reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame] | 86 | writer.writeIntArray(data, arraySize); |
| sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 87 | } |
| commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 88 | static bool Read(SkValidatingReadBuffer& reader, int32_t* data, uint32_t arraySize) { |
| 89 | return reader.readIntArray(data, arraySize); |
| 90 | } |
| 91 | }; |
| sugoi@google.com | 305f78e | 2013-11-04 16:18:15 +0000 | [diff] [blame] | 92 | |
| commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 93 | template<> struct SerializationUtils<SkPoint> { |
| commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 94 | static void Write(SkWriteBuffer& writer, SkPoint* data, uint32_t arraySize) { |
| reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame] | 95 | writer.writePointArray(data, arraySize); |
| commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 96 | } |
| 97 | static bool Read(SkValidatingReadBuffer& reader, SkPoint* data, uint32_t arraySize) { |
| 98 | return reader.readPointArray(data, arraySize); |
| 99 | } |
| 100 | }; |
| reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame] | 101 | |
| commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 102 | template<> struct SerializationUtils<SkScalar> { |
| commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 103 | static void Write(SkWriteBuffer& writer, SkScalar* data, uint32_t arraySize) { |
| commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 104 | writer.writeScalarArray(data, arraySize); |
| 105 | } |
| 106 | static bool Read(SkValidatingReadBuffer& reader, SkScalar* data, uint32_t arraySize) { |
| 107 | return reader.readScalarArray(data, arraySize); |
| 108 | } |
| 109 | }; |
| reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame] | 110 | |
| commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 111 | template<typename T> |
| 112 | static void TestObjectSerialization(T* testObj, skiatest::Reporter* reporter) { |
| commit-bot@chromium.org | a2bd2d1 | 2014-01-30 22:16:32 +0000 | [diff] [blame^] | 113 | SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag); |
| commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 114 | SerializationUtils<T>::Write(writer, testObj); |
| 115 | size_t bytesWritten = writer.bytesWritten(); |
| 116 | REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten); |
| reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame] | 117 | |
| commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 118 | unsigned char dataWritten[1024]; |
| 119 | writer.writeToMemory(dataWritten); |
| 120 | |
| 121 | // Make sure this fails when it should (test with smaller size, but still multiple of 4) |
| 122 | SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 4); |
| commit-bot@chromium.org | 8f457e3 | 2013-11-08 19:22:57 +0000 | [diff] [blame] | 123 | T obj; |
| 124 | SerializationUtils<T>::Read(buffer, &obj); |
| commit-bot@chromium.org | c2e9db3 | 2013-12-06 20:14:46 +0000 | [diff] [blame] | 125 | REPORTER_ASSERT(reporter, !buffer.isValid()); |
| commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 126 | |
| 127 | // Make sure this succeeds when it should |
| 128 | SkValidatingReadBuffer buffer2(dataWritten, bytesWritten); |
| commit-bot@chromium.org | 8f457e3 | 2013-11-08 19:22:57 +0000 | [diff] [blame] | 129 | const unsigned char* peekBefore = static_cast<const unsigned char*>(buffer2.skip(0)); |
| 130 | T obj2; |
| 131 | SerializationUtils<T>::Read(buffer2, &obj2); |
| 132 | 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] | 133 | // 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] | 134 | REPORTER_ASSERT(reporter, buffer2.isValid()); |
| commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 135 | REPORTER_ASSERT(reporter, static_cast<size_t>(peekAfter - peekBefore) == bytesWritten); |
| 136 | |
| 137 | TestAlignment(testObj, reporter); |
| 138 | } |
| 139 | |
| 140 | template<typename T> |
| commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 141 | static T* TestFlattenableSerialization(T* testObj, bool shouldSucceed, |
| 142 | skiatest::Reporter* reporter) { |
| commit-bot@chromium.org | a2bd2d1 | 2014-01-30 22:16:32 +0000 | [diff] [blame^] | 143 | SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag); |
| commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 144 | SerializationUtils<T>::Write(writer, testObj); |
| 145 | size_t bytesWritten = writer.bytesWritten(); |
| 146 | REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten); |
| 147 | |
| 148 | unsigned char dataWritten[1024]; |
| reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 149 | SkASSERT(bytesWritten <= sizeof(dataWritten)); |
| commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 150 | writer.writeToMemory(dataWritten); |
| 151 | |
| 152 | // Make sure this fails when it should (test with smaller size, but still multiple of 4) |
| 153 | SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 4); |
| 154 | T* obj = NULL; |
| 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 | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 157 | REPORTER_ASSERT(reporter, NULL == obj); |
| 158 | |
| 159 | // Make sure this succeeds when it should |
| 160 | SkValidatingReadBuffer buffer2(dataWritten, bytesWritten); |
| 161 | const unsigned char* peekBefore = static_cast<const unsigned char*>(buffer2.skip(0)); |
| 162 | T* obj2 = NULL; |
| 163 | SerializationUtils<T>::Read(buffer2, &obj2); |
| 164 | const unsigned char* peekAfter = static_cast<const unsigned char*>(buffer2.skip(0)); |
| 165 | if (shouldSucceed) { |
| 166 | // 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] | 167 | REPORTER_ASSERT(reporter, buffer2.isValid()); |
| commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 168 | REPORTER_ASSERT(reporter, static_cast<size_t>(peekAfter - peekBefore) == bytesWritten); |
| 169 | REPORTER_ASSERT(reporter, NULL != obj2); |
| 170 | } else { |
| 171 | // 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] | 172 | REPORTER_ASSERT(reporter, !buffer.isValid()); |
| commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 173 | REPORTER_ASSERT(reporter, NULL == obj2); |
| 174 | } |
| 175 | |
| 176 | return obj2; // Return object to perform further validity tests on it |
| 177 | } |
| 178 | |
| 179 | template<typename T> |
| commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 180 | static void TestArraySerialization(T* data, skiatest::Reporter* reporter) { |
| commit-bot@chromium.org | a2bd2d1 | 2014-01-30 22:16:32 +0000 | [diff] [blame^] | 181 | SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag); |
| commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 182 | SerializationUtils<T>::Write(writer, data, kArraySize); |
| 183 | size_t bytesWritten = writer.bytesWritten(); |
| 184 | // This should write the length (in 4 bytes) and the array |
| 185 | REPORTER_ASSERT(reporter, (4 + kArraySize * sizeof(T)) == bytesWritten); |
| 186 | |
| 187 | unsigned char dataWritten[1024]; |
| 188 | writer.writeToMemory(dataWritten); |
| 189 | |
| 190 | // Make sure this fails when it should |
| 191 | SkValidatingReadBuffer buffer(dataWritten, bytesWritten); |
| 192 | T dataRead[kArraySize]; |
| 193 | bool success = SerializationUtils<T>::Read(buffer, dataRead, kArraySize / 2); |
| 194 | // This should have failed, since the provided size was too small |
| 195 | REPORTER_ASSERT(reporter, !success); |
| 196 | |
| 197 | // Make sure this succeeds when it should |
| 198 | SkValidatingReadBuffer buffer2(dataWritten, bytesWritten); |
| 199 | success = SerializationUtils<T>::Read(buffer2, dataRead, kArraySize); |
| 200 | // This should have succeeded, since there are enough bytes to read this |
| 201 | REPORTER_ASSERT(reporter, success); |
| 202 | } |
| 203 | |
| commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 204 | static void TestBitmapSerialization(const SkBitmap& validBitmap, |
| 205 | const SkBitmap& invalidBitmap, |
| 206 | bool shouldSucceed, |
| 207 | skiatest::Reporter* reporter) { |
| 208 | SkBitmapSource validBitmapSource(validBitmap); |
| 209 | SkBitmapSource invalidBitmapSource(invalidBitmap); |
| 210 | SkAutoTUnref<SkXfermode> mode(SkXfermode::Create(SkXfermode::kSrcOver_Mode)); |
| 211 | SkXfermodeImageFilter xfermodeImageFilter(mode, &invalidBitmapSource, &validBitmapSource); |
| 212 | |
| 213 | SkAutoTUnref<SkImageFilter> deserializedFilter( |
| 214 | TestFlattenableSerialization<SkImageFilter>( |
| 215 | &xfermodeImageFilter, shouldSucceed, reporter)); |
| 216 | |
| 217 | // Try to render a small bitmap using the invalid deserialized filter |
| 218 | // to make sure we don't crash while trying to render it |
| 219 | if (shouldSucceed) { |
| 220 | SkBitmap bitmap; |
| 221 | bitmap.setConfig(SkBitmap::kARGB_8888_Config, 24, 24); |
| 222 | bitmap.allocPixels(); |
| 223 | SkBitmapDevice device(bitmap); |
| 224 | SkCanvas canvas(&device); |
| 225 | canvas.clear(0x00000000); |
| 226 | SkPaint paint; |
| 227 | paint.setImageFilter(deserializedFilter); |
| 228 | canvas.clipRect(SkRect::MakeXYWH(0, 0, SkIntToScalar(24), SkIntToScalar(24))); |
| 229 | canvas.drawBitmap(bitmap, 0, 0, &paint); |
| 230 | } |
| 231 | } |
| 232 | |
| tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 233 | DEF_TEST(Serialization, reporter) { |
| commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 234 | // Test matrix serialization |
| 235 | { |
| 236 | SkMatrix matrix = SkMatrix::I(); |
| 237 | TestObjectSerialization(&matrix, reporter); |
| 238 | } |
| 239 | |
| 240 | // Test path serialization |
| 241 | { |
| 242 | SkPath path; |
| 243 | TestObjectSerialization(&path, reporter); |
| sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 244 | } |
| sugoi@google.com | 305f78e | 2013-11-04 16:18:15 +0000 | [diff] [blame] | 245 | |
| commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 246 | // Test region serialization |
| sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 247 | { |
| commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 248 | SkRegion region; |
| 249 | TestObjectSerialization(®ion, reporter); |
| 250 | } |
| sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 251 | |
| commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 252 | // Test rrect serialization |
| 253 | { |
| rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 254 | // SkRRect does not initialize anything. |
| 255 | // An uninitialized SkRRect can be serialized, |
| 256 | // but will branch on uninitialized data when deserialized. |
| commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 257 | SkRRect rrect; |
| rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 258 | SkRect rect = SkRect::MakeXYWH(1, 2, 20, 30); |
| 259 | SkVector corners[4] = { {1, 2}, {2, 3}, {3,4}, {4,5} }; |
| 260 | rrect.setRectRadii(rect, corners); |
| commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 261 | TestAlignment(&rrect, reporter); |
| 262 | } |
| sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 263 | |
| commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 264 | // Test readByteArray |
| 265 | { |
| rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 266 | unsigned char data[kArraySize] = { 1, 2, 3 }; |
| commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 267 | TestArraySerialization(data, reporter); |
| 268 | } |
| sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 269 | |
| commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 270 | // Test readColorArray |
| 271 | { |
| rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 272 | SkColor data[kArraySize] = { SK_ColorBLACK, SK_ColorWHITE, SK_ColorRED }; |
| commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 273 | TestArraySerialization(data, reporter); |
| 274 | } |
| 275 | |
| 276 | // Test readIntArray |
| 277 | { |
| rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 278 | int32_t data[kArraySize] = { 1, 2, 4, 8 }; |
| commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 279 | TestArraySerialization(data, reporter); |
| 280 | } |
| 281 | |
| 282 | // Test readPointArray |
| 283 | { |
| rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 284 | SkPoint data[kArraySize] = { {6, 7}, {42, 128} }; |
| commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 285 | TestArraySerialization(data, reporter); |
| 286 | } |
| 287 | |
| 288 | // Test readScalarArray |
| 289 | { |
| rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 290 | SkScalar data[kArraySize] = { SK_Scalar1, SK_ScalarHalf, SK_ScalarMax }; |
| commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 291 | TestArraySerialization(data, reporter); |
| commit-bot@chromium.org | 0251288 | 2013-10-31 18:37:50 +0000 | [diff] [blame] | 292 | } |
| commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 293 | |
| 294 | // Test invalid deserializations |
| 295 | { |
| 296 | SkBitmap validBitmap; |
| 297 | validBitmap.setConfig(SkBitmap::kARGB_8888_Config, 256, 256); |
| 298 | |
| 299 | // Create a bitmap with a really large height |
| 300 | SkBitmap invalidBitmap; |
| 301 | invalidBitmap.setConfig(SkBitmap::kARGB_8888_Config, 256, 1000000000); |
| 302 | |
| 303 | // The deserialization should succeed, and the rendering shouldn't crash, |
| 304 | // even when the device fails to initialize, due to its size |
| 305 | TestBitmapSerialization(validBitmap, invalidBitmap, true, reporter); |
| 306 | |
| reed@google.com | dcea530 | 2014-01-03 13:43:01 +0000 | [diff] [blame] | 307 | // we assert if the pixelref doesn't agree with the config, so skip this |
| 308 | // test (at least for now) |
| 309 | #if 0 |
| commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 310 | // Create a bitmap with a pixel ref too small |
| reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 311 | SkImageInfo info; |
| 312 | info.fWidth = 256; |
| 313 | info.fHeight = 256; |
| 314 | info.fColorType = kPMColor_SkColorType; |
| 315 | info.fAlphaType = kPremul_SkAlphaType; |
| 316 | |
| commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 317 | SkBitmap invalidBitmap2; |
| reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 318 | invalidBitmap2.setConfig(info); |
| skia.committer@gmail.com | 96f5fa0 | 2013-12-16 07:01:40 +0000 | [diff] [blame] | 319 | |
| reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 320 | // Hack to force invalid, by making the pixelref smaller than its |
| 321 | // owning bitmap. |
| 322 | info.fWidth = 32; |
| 323 | info.fHeight = 1; |
| skia.committer@gmail.com | 96f5fa0 | 2013-12-16 07:01:40 +0000 | [diff] [blame] | 324 | |
| reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 325 | invalidBitmap2.setPixelRef(SkMallocPixelRef::NewAllocate( |
| 326 | info, invalidBitmap2.rowBytes(), NULL))->unref(); |
| skia.committer@gmail.com | 8491d24 | 2013-12-05 07:02:16 +0000 | [diff] [blame] | 327 | |
| commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 328 | // The deserialization should detect the pixel ref being too small and fail |
| 329 | TestBitmapSerialization(validBitmap, invalidBitmap2, false, reporter); |
| reed@google.com | dcea530 | 2014-01-03 13:43:01 +0000 | [diff] [blame] | 330 | #endif |
| commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 331 | } |
| commit-bot@chromium.org | 0251288 | 2013-10-31 18:37:50 +0000 | [diff] [blame] | 332 | } |