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