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 | 1e7ee99 | 2014-03-14 21:22:22 +0000 | [diff] [blame] | 12 | #include "SkTemplates.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 13 | #include "SkWriteBuffer.h" |
commit-bot@chromium.org | 0251288 | 2013-10-31 18:37:50 +0000 | [diff] [blame] | 14 | #include "SkValidatingReadBuffer.h" |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 15 | #include "SkXfermodeImageFilter.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 16 | #include "Test.h" |
commit-bot@chromium.org | 0251288 | 2013-10-31 18:37:50 +0000 | [diff] [blame] | 17 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 18 | static const uint32_t kArraySize = 64; |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 19 | static const int kBitmapSize = 256; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 20 | |
| 21 | template<typename T> |
| 22 | static void TestAlignment(T* testObj, skiatest::Reporter* reporter) { |
| 23 | // Test memory read/write functions directly |
| 24 | unsigned char dataWritten[1024]; |
| 25 | size_t bytesWrittenToMemory = testObj->writeToMemory(dataWritten); |
| 26 | REPORTER_ASSERT(reporter, SkAlign4(bytesWrittenToMemory) == bytesWrittenToMemory); |
| 27 | size_t bytesReadFromMemory = testObj->readFromMemory(dataWritten, bytesWrittenToMemory); |
| 28 | REPORTER_ASSERT(reporter, SkAlign4(bytesReadFromMemory) == bytesReadFromMemory); |
| 29 | } |
| 30 | |
| 31 | template<typename T> struct SerializationUtils { |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 32 | // Generic case for flattenables |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 33 | static void Write(SkWriteBuffer& writer, const T* flattenable) { |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 34 | writer.writeFlattenable(flattenable); |
| 35 | } |
| 36 | static void Read(SkValidatingReadBuffer& reader, T** flattenable) { |
| 37 | *flattenable = (T*)reader.readFlattenable(T::GetFlattenableType()); |
| 38 | } |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | template<> struct SerializationUtils<SkMatrix> { |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 42 | static void Write(SkWriteBuffer& writer, const SkMatrix* matrix) { |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 43 | writer.writeMatrix(*matrix); |
| 44 | } |
| 45 | static void Read(SkValidatingReadBuffer& reader, SkMatrix* matrix) { |
| 46 | reader.readMatrix(matrix); |
| 47 | } |
| 48 | }; |
| 49 | |
| 50 | template<> struct SerializationUtils<SkPath> { |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 51 | static void Write(SkWriteBuffer& writer, const SkPath* path) { |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 52 | writer.writePath(*path); |
| 53 | } |
| 54 | static void Read(SkValidatingReadBuffer& reader, SkPath* path) { |
| 55 | reader.readPath(path); |
| 56 | } |
| 57 | }; |
| 58 | |
| 59 | template<> struct SerializationUtils<SkRegion> { |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 60 | static void Write(SkWriteBuffer& writer, const SkRegion* region) { |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 61 | writer.writeRegion(*region); |
| 62 | } |
| 63 | static void Read(SkValidatingReadBuffer& reader, SkRegion* region) { |
| 64 | reader.readRegion(region); |
| 65 | } |
| 66 | }; |
| 67 | |
| 68 | template<> struct SerializationUtils<unsigned char> { |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 69 | static void Write(SkWriteBuffer& writer, unsigned char* data, uint32_t arraySize) { |
reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame] | 70 | writer.writeByteArray(data, arraySize); |
sugoi@google.com | 305f78e | 2013-11-04 16:18:15 +0000 | [diff] [blame] | 71 | } |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 72 | static bool Read(SkValidatingReadBuffer& reader, unsigned char* data, uint32_t arraySize) { |
| 73 | return reader.readByteArray(data, arraySize); |
| 74 | } |
| 75 | }; |
commit-bot@chromium.org | 0251288 | 2013-10-31 18:37:50 +0000 | [diff] [blame] | 76 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 77 | template<> struct SerializationUtils<SkColor> { |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 78 | static void Write(SkWriteBuffer& writer, SkColor* data, uint32_t arraySize) { |
reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame] | 79 | writer.writeColorArray(data, arraySize); |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 80 | } |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 81 | static bool Read(SkValidatingReadBuffer& reader, SkColor* data, uint32_t arraySize) { |
| 82 | return reader.readColorArray(data, arraySize); |
| 83 | } |
| 84 | }; |
sugoi@google.com | 305f78e | 2013-11-04 16:18:15 +0000 | [diff] [blame] | 85 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 86 | template<> struct SerializationUtils<int32_t> { |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 87 | static void Write(SkWriteBuffer& writer, int32_t* data, uint32_t arraySize) { |
reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame] | 88 | writer.writeIntArray(data, arraySize); |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 89 | } |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 90 | static bool Read(SkValidatingReadBuffer& reader, int32_t* data, uint32_t arraySize) { |
| 91 | return reader.readIntArray(data, arraySize); |
| 92 | } |
| 93 | }; |
sugoi@google.com | 305f78e | 2013-11-04 16:18:15 +0000 | [diff] [blame] | 94 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 95 | template<> struct SerializationUtils<SkPoint> { |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 96 | static void Write(SkWriteBuffer& writer, SkPoint* data, uint32_t arraySize) { |
reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame] | 97 | writer.writePointArray(data, arraySize); |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 98 | } |
| 99 | static bool Read(SkValidatingReadBuffer& reader, SkPoint* data, uint32_t arraySize) { |
| 100 | return reader.readPointArray(data, arraySize); |
| 101 | } |
| 102 | }; |
reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame] | 103 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 104 | template<> struct SerializationUtils<SkScalar> { |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 105 | static void Write(SkWriteBuffer& writer, SkScalar* data, uint32_t arraySize) { |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 106 | writer.writeScalarArray(data, arraySize); |
| 107 | } |
| 108 | static bool Read(SkValidatingReadBuffer& reader, SkScalar* data, uint32_t arraySize) { |
| 109 | return reader.readScalarArray(data, arraySize); |
| 110 | } |
| 111 | }; |
reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame] | 112 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 113 | template<typename T> |
| 114 | static void TestObjectSerialization(T* testObj, skiatest::Reporter* reporter) { |
commit-bot@chromium.org | a2bd2d1 | 2014-01-30 22:16:32 +0000 | [diff] [blame] | 115 | SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag); |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 116 | SerializationUtils<T>::Write(writer, testObj); |
| 117 | size_t bytesWritten = writer.bytesWritten(); |
| 118 | REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten); |
reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame] | 119 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 120 | unsigned char dataWritten[1024]; |
| 121 | writer.writeToMemory(dataWritten); |
| 122 | |
| 123 | // Make sure this fails when it should (test with smaller size, but still multiple of 4) |
| 124 | SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 4); |
commit-bot@chromium.org | 8f457e3 | 2013-11-08 19:22:57 +0000 | [diff] [blame] | 125 | T obj; |
| 126 | SerializationUtils<T>::Read(buffer, &obj); |
commit-bot@chromium.org | c2e9db3 | 2013-12-06 20:14:46 +0000 | [diff] [blame] | 127 | REPORTER_ASSERT(reporter, !buffer.isValid()); |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 128 | |
| 129 | // Make sure this succeeds when it should |
| 130 | SkValidatingReadBuffer buffer2(dataWritten, bytesWritten); |
commit-bot@chromium.org | 8f457e3 | 2013-11-08 19:22:57 +0000 | [diff] [blame] | 131 | const unsigned char* peekBefore = static_cast<const unsigned char*>(buffer2.skip(0)); |
| 132 | T obj2; |
| 133 | SerializationUtils<T>::Read(buffer2, &obj2); |
| 134 | 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] | 135 | // 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] | 136 | REPORTER_ASSERT(reporter, buffer2.isValid()); |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 137 | REPORTER_ASSERT(reporter, static_cast<size_t>(peekAfter - peekBefore) == bytesWritten); |
| 138 | |
| 139 | TestAlignment(testObj, reporter); |
| 140 | } |
| 141 | |
| 142 | template<typename T> |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 143 | static T* TestFlattenableSerialization(T* testObj, bool shouldSucceed, |
| 144 | skiatest::Reporter* reporter) { |
commit-bot@chromium.org | a2bd2d1 | 2014-01-30 22:16:32 +0000 | [diff] [blame] | 145 | SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 146 | SerializationUtils<T>::Write(writer, testObj); |
| 147 | size_t bytesWritten = writer.bytesWritten(); |
| 148 | REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten); |
| 149 | |
| 150 | unsigned char dataWritten[1024]; |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 151 | SkASSERT(bytesWritten <= sizeof(dataWritten)); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 152 | writer.writeToMemory(dataWritten); |
| 153 | |
| 154 | // Make sure this fails when it should (test with smaller size, but still multiple of 4) |
| 155 | SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 4); |
| 156 | T* obj = NULL; |
| 157 | SerializationUtils<T>::Read(buffer, &obj); |
commit-bot@chromium.org | c2e9db3 | 2013-12-06 20:14:46 +0000 | [diff] [blame] | 158 | REPORTER_ASSERT(reporter, !buffer.isValid()); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 159 | REPORTER_ASSERT(reporter, NULL == obj); |
| 160 | |
| 161 | // Make sure this succeeds when it should |
| 162 | SkValidatingReadBuffer buffer2(dataWritten, bytesWritten); |
| 163 | const unsigned char* peekBefore = static_cast<const unsigned char*>(buffer2.skip(0)); |
| 164 | T* obj2 = NULL; |
| 165 | SerializationUtils<T>::Read(buffer2, &obj2); |
| 166 | const unsigned char* peekAfter = static_cast<const unsigned char*>(buffer2.skip(0)); |
| 167 | if (shouldSucceed) { |
| 168 | // 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] | 169 | REPORTER_ASSERT(reporter, buffer2.isValid()); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 170 | REPORTER_ASSERT(reporter, static_cast<size_t>(peekAfter - peekBefore) == bytesWritten); |
| 171 | REPORTER_ASSERT(reporter, NULL != obj2); |
| 172 | } else { |
| 173 | // 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] | 174 | REPORTER_ASSERT(reporter, !buffer.isValid()); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 175 | REPORTER_ASSERT(reporter, NULL == obj2); |
| 176 | } |
| 177 | |
| 178 | return obj2; // Return object to perform further validity tests on it |
| 179 | } |
| 180 | |
| 181 | template<typename T> |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 182 | static void TestArraySerialization(T* data, skiatest::Reporter* reporter) { |
commit-bot@chromium.org | a2bd2d1 | 2014-01-30 22:16:32 +0000 | [diff] [blame] | 183 | SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag); |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 184 | SerializationUtils<T>::Write(writer, data, kArraySize); |
| 185 | size_t bytesWritten = writer.bytesWritten(); |
| 186 | // This should write the length (in 4 bytes) and the array |
| 187 | REPORTER_ASSERT(reporter, (4 + kArraySize * sizeof(T)) == bytesWritten); |
| 188 | |
| 189 | unsigned char dataWritten[1024]; |
| 190 | writer.writeToMemory(dataWritten); |
| 191 | |
| 192 | // Make sure this fails when it should |
| 193 | SkValidatingReadBuffer buffer(dataWritten, bytesWritten); |
| 194 | T dataRead[kArraySize]; |
| 195 | bool success = SerializationUtils<T>::Read(buffer, dataRead, kArraySize / 2); |
| 196 | // This should have failed, since the provided size was too small |
| 197 | REPORTER_ASSERT(reporter, !success); |
| 198 | |
| 199 | // Make sure this succeeds when it should |
| 200 | SkValidatingReadBuffer buffer2(dataWritten, bytesWritten); |
| 201 | success = SerializationUtils<T>::Read(buffer2, dataRead, kArraySize); |
| 202 | // This should have succeeded, since there are enough bytes to read this |
| 203 | REPORTER_ASSERT(reporter, success); |
| 204 | } |
| 205 | |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 206 | static void TestBitmapSerialization(const SkBitmap& validBitmap, |
| 207 | const SkBitmap& invalidBitmap, |
| 208 | bool shouldSucceed, |
| 209 | skiatest::Reporter* reporter) { |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame] | 210 | SkAutoTUnref<SkBitmapSource> validBitmapSource(SkBitmapSource::Create(validBitmap)); |
| 211 | SkAutoTUnref<SkBitmapSource> invalidBitmapSource(SkBitmapSource::Create(invalidBitmap)); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 212 | SkAutoTUnref<SkXfermode> mode(SkXfermode::Create(SkXfermode::kSrcOver_Mode)); |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame] | 213 | SkAutoTUnref<SkXfermodeImageFilter> xfermodeImageFilter( |
| 214 | SkXfermodeImageFilter::Create(mode, invalidBitmapSource, validBitmapSource)); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 215 | |
| 216 | SkAutoTUnref<SkImageFilter> deserializedFilter( |
| 217 | TestFlattenableSerialization<SkImageFilter>( |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame] | 218 | xfermodeImageFilter, shouldSucceed, reporter)); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 219 | |
| 220 | // Try to render a small bitmap using the invalid deserialized filter |
| 221 | // to make sure we don't crash while trying to render it |
| 222 | if (shouldSucceed) { |
| 223 | SkBitmap bitmap; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 224 | bitmap.allocN32Pixels(24, 24); |
| 225 | SkCanvas canvas(bitmap); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 226 | canvas.clear(0x00000000); |
| 227 | SkPaint paint; |
| 228 | paint.setImageFilter(deserializedFilter); |
| 229 | canvas.clipRect(SkRect::MakeXYWH(0, 0, SkIntToScalar(24), SkIntToScalar(24))); |
| 230 | canvas.drawBitmap(bitmap, 0, 0, &paint); |
| 231 | } |
| 232 | } |
| 233 | |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 234 | static bool setup_bitmap_for_canvas(SkBitmap* bitmap) { |
| 235 | SkImageInfo info = SkImageInfo::Make( |
| 236 | kBitmapSize, kBitmapSize, kPMColor_SkColorType, kPremul_SkAlphaType); |
| 237 | return bitmap->allocPixels(info); |
| 238 | } |
| 239 | |
| 240 | static bool make_checkerboard_bitmap(SkBitmap& bitmap) { |
| 241 | bool success = setup_bitmap_for_canvas(&bitmap); |
| 242 | |
| 243 | SkCanvas canvas(bitmap); |
| 244 | canvas.clear(0x00000000); |
| 245 | SkPaint darkPaint; |
| 246 | darkPaint.setColor(0xFF804020); |
| 247 | SkPaint lightPaint; |
| 248 | lightPaint.setColor(0xFF244484); |
| 249 | const int i = kBitmapSize / 8; |
| 250 | const SkScalar f = SkIntToScalar(i); |
| 251 | for (int y = 0; y < kBitmapSize; y += i) { |
| 252 | for (int x = 0; x < kBitmapSize; x += i) { |
| 253 | canvas.save(); |
| 254 | canvas.translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 255 | canvas.drawRect(SkRect::MakeXYWH(0, 0, f, f), darkPaint); |
| 256 | canvas.drawRect(SkRect::MakeXYWH(f, 0, f, f), lightPaint); |
| 257 | canvas.drawRect(SkRect::MakeXYWH(0, f, f, f), lightPaint); |
| 258 | canvas.drawRect(SkRect::MakeXYWH(f, f, f, f), darkPaint); |
| 259 | canvas.restore(); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | return success; |
| 264 | } |
| 265 | |
| 266 | static bool drawSomething(SkCanvas* canvas) { |
| 267 | SkPaint paint; |
| 268 | SkBitmap bitmap; |
| 269 | bool success = make_checkerboard_bitmap(bitmap); |
| 270 | |
| 271 | canvas->save(); |
| 272 | canvas->scale(0.5f, 0.5f); |
| 273 | canvas->drawBitmap(bitmap, 0, 0, NULL); |
| 274 | canvas->restore(); |
| 275 | |
| 276 | const char beforeStr[] = "before circle"; |
| 277 | const char afterStr[] = "after circle"; |
| 278 | |
| 279 | paint.setAntiAlias(true); |
| 280 | |
| 281 | paint.setColor(SK_ColorRED); |
| 282 | canvas->drawData(beforeStr, sizeof(beforeStr)); |
| 283 | canvas->drawCircle(SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/3), paint); |
| 284 | canvas->drawData(afterStr, sizeof(afterStr)); |
| 285 | paint.setColor(SK_ColorBLACK); |
| 286 | paint.setTextSize(SkIntToScalar(kBitmapSize/3)); |
| 287 | canvas->drawText("Picture", 7, SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/4), paint); |
| 288 | |
| 289 | return success; |
| 290 | } |
| 291 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 292 | DEF_TEST(Serialization, reporter) { |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 293 | // Test matrix serialization |
| 294 | { |
| 295 | SkMatrix matrix = SkMatrix::I(); |
| 296 | TestObjectSerialization(&matrix, reporter); |
| 297 | } |
| 298 | |
| 299 | // Test path serialization |
| 300 | { |
| 301 | SkPath path; |
| 302 | TestObjectSerialization(&path, reporter); |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 303 | } |
sugoi@google.com | 305f78e | 2013-11-04 16:18:15 +0000 | [diff] [blame] | 304 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 305 | // Test region serialization |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 306 | { |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 307 | SkRegion region; |
| 308 | TestObjectSerialization(®ion, reporter); |
| 309 | } |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 310 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 311 | // Test rrect serialization |
| 312 | { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 313 | // SkRRect does not initialize anything. |
| 314 | // An uninitialized SkRRect can be serialized, |
| 315 | // but will branch on uninitialized data when deserialized. |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 316 | SkRRect rrect; |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 317 | SkRect rect = SkRect::MakeXYWH(1, 2, 20, 30); |
| 318 | SkVector corners[4] = { {1, 2}, {2, 3}, {3,4}, {4,5} }; |
| 319 | rrect.setRectRadii(rect, corners); |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 320 | TestAlignment(&rrect, reporter); |
| 321 | } |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 322 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 323 | // Test readByteArray |
| 324 | { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 325 | unsigned char data[kArraySize] = { 1, 2, 3 }; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 326 | TestArraySerialization(data, reporter); |
| 327 | } |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 328 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 329 | // Test readColorArray |
| 330 | { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 331 | SkColor data[kArraySize] = { SK_ColorBLACK, SK_ColorWHITE, SK_ColorRED }; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 332 | TestArraySerialization(data, reporter); |
| 333 | } |
| 334 | |
| 335 | // Test readIntArray |
| 336 | { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 337 | int32_t data[kArraySize] = { 1, 2, 4, 8 }; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 338 | TestArraySerialization(data, reporter); |
| 339 | } |
| 340 | |
| 341 | // Test readPointArray |
| 342 | { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 343 | SkPoint data[kArraySize] = { {6, 7}, {42, 128} }; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 344 | TestArraySerialization(data, reporter); |
| 345 | } |
| 346 | |
| 347 | // Test readScalarArray |
| 348 | { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 349 | SkScalar data[kArraySize] = { SK_Scalar1, SK_ScalarHalf, SK_ScalarMax }; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 350 | TestArraySerialization(data, reporter); |
commit-bot@chromium.org | 0251288 | 2013-10-31 18:37:50 +0000 | [diff] [blame] | 351 | } |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 352 | |
| 353 | // Test invalid deserializations |
| 354 | { |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 355 | SkImageInfo info = SkImageInfo::MakeN32Premul(kBitmapSize, kBitmapSize); |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 356 | |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 357 | SkBitmap validBitmap; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 358 | validBitmap.setConfig(info); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 359 | |
| 360 | // Create a bitmap with a really large height |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 361 | info.fHeight = 1000000000; |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 362 | SkBitmap invalidBitmap; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 363 | invalidBitmap.setConfig(info); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 364 | |
| 365 | // The deserialization should succeed, and the rendering shouldn't crash, |
| 366 | // even when the device fails to initialize, due to its size |
| 367 | TestBitmapSerialization(validBitmap, invalidBitmap, true, reporter); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 368 | } |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 369 | |
| 370 | // Test simple SkPicture serialization |
| 371 | { |
| 372 | SkPicture* pict = new SkPicture; |
| 373 | SkAutoUnref aur(pict); |
| 374 | bool didDraw = drawSomething(pict->beginRecording(kBitmapSize, kBitmapSize)); |
| 375 | REPORTER_ASSERT(reporter, didDraw); |
| 376 | pict->endRecording(); |
| 377 | |
| 378 | // Serialize picture |
| 379 | SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag); |
| 380 | pict->flatten(writer); |
| 381 | size_t size = writer.bytesWritten(); |
commit-bot@chromium.org | 1e7ee99 | 2014-03-14 21:22:22 +0000 | [diff] [blame] | 382 | SkAutoTMalloc<unsigned char> data(size); |
| 383 | writer.writeToMemory(static_cast<void*>(data.get())); |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 384 | |
| 385 | // Deserialize picture |
commit-bot@chromium.org | 1e7ee99 | 2014-03-14 21:22:22 +0000 | [diff] [blame] | 386 | SkValidatingReadBuffer reader(static_cast<void*>(data.get()), size); |
commit-bot@chromium.org | e5eee51 | 2014-03-13 16:18:49 +0000 | [diff] [blame] | 387 | SkAutoTUnref<SkPicture> readPict( |
| 388 | SkPicture::CreateFromBuffer(reader)); |
| 389 | REPORTER_ASSERT(reporter, NULL != readPict.get()); |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 390 | } |
commit-bot@chromium.org | 0251288 | 2013-10-31 18:37:50 +0000 | [diff] [blame] | 391 | } |