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); |
| 201 | REPORTER_ASSERT(reporter, NULL != obj2); |
| 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 | |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 264 | static SkBitmap draw_picture(SkPicture& picture) { |
| 265 | SkBitmap bitmap; |
| 266 | bitmap.allocN32Pixels(picture.width(), picture.height()); |
| 267 | SkCanvas canvas(bitmap); |
| 268 | picture.draw(&canvas); |
| 269 | return bitmap; |
| 270 | } |
| 271 | |
| 272 | static void compare_bitmaps(skiatest::Reporter* reporter, |
| 273 | const SkBitmap& b1, const SkBitmap& b2) { |
| 274 | REPORTER_ASSERT(reporter, b1.width() == b2.width()); |
| 275 | REPORTER_ASSERT(reporter, b1.height() == b2.height()); |
| 276 | SkAutoLockPixels autoLockPixels1(b1); |
| 277 | SkAutoLockPixels autoLockPixels2(b2); |
| 278 | |
| 279 | if ((b1.width() != b2.width()) || |
| 280 | (b1.height() != b2.height())) { |
| 281 | return; |
| 282 | } |
| 283 | |
| 284 | int pixelErrors = 0; |
| 285 | for (int y = 0; y < b2.height(); ++y) { |
| 286 | for (int x = 0; x < b2.width(); ++x) { |
| 287 | if (b1.getColor(x, y) != b2.getColor(x, y)) |
| 288 | ++pixelErrors; |
| 289 | } |
| 290 | } |
| 291 | REPORTER_ASSERT(reporter, 0 == pixelErrors); |
| 292 | } |
| 293 | |
| 294 | static void TestPictureTypefaceSerialization(skiatest::Reporter* reporter) { |
| 295 | // Load typeface form file. |
| 296 | // This test cannot run if there is no resource path. |
| 297 | SkString resourcePath = GetResourcePath(); |
| 298 | if (resourcePath.isEmpty()) { |
| 299 | SkDebugf("Could not run fontstream test because resourcePath not specified."); |
| 300 | return; |
| 301 | } |
| 302 | SkString filename = SkOSPath::SkPathJoin(resourcePath.c_str(), "test.ttc"); |
| 303 | SkTypeface* typeface = SkTypeface::CreateFromFile(filename.c_str()); |
| 304 | if (!typeface) { |
| 305 | SkDebugf("Could not run fontstream test because test.ttc not found."); |
| 306 | return; |
| 307 | } |
| 308 | |
| 309 | // Create a paint with the typeface we loaded. |
| 310 | SkPaint paint; |
| 311 | paint.setColor(SK_ColorGRAY); |
| 312 | paint.setTextSize(SkIntToScalar(30)); |
| 313 | SkSafeUnref(paint.setTypeface(typeface)); |
| 314 | |
| 315 | // Paint some text. |
| 316 | SkPictureRecorder recorder; |
| 317 | SkIRect canvasRect = SkIRect::MakeWH(kBitmapSize, kBitmapSize); |
| 318 | SkCanvas* canvas = recorder.beginRecording(canvasRect.width(), canvasRect.height(), NULL, 0); |
| 319 | canvas->drawColor(SK_ColorWHITE); |
| 320 | canvas->drawText("A", 1, 24, 32, paint); |
| 321 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| 322 | |
| 323 | // Serlialize picture and create its clone from stream. |
| 324 | SkDynamicMemoryWStream stream; |
| 325 | picture->serialize(&stream); |
| 326 | SkAutoTUnref<SkStream> inputStream(stream.detachAsStream()); |
| 327 | SkAutoTUnref<SkPicture> loadedPicture(SkPicture::CreateFromStream(inputStream.get())); |
| 328 | |
| 329 | // Draw both original and clone picture and compare bitmaps -- they should be identical. |
| 330 | SkBitmap origBitmap = draw_picture(*picture); |
| 331 | SkBitmap destBitmap = draw_picture(*loadedPicture); |
| 332 | compare_bitmaps(reporter, origBitmap, destBitmap); |
| 333 | } |
| 334 | |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 335 | static bool setup_bitmap_for_canvas(SkBitmap* bitmap) { |
| 336 | SkImageInfo info = SkImageInfo::Make( |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 337 | kBitmapSize, kBitmapSize, kN32_SkColorType, kPremul_SkAlphaType); |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 338 | return bitmap->allocPixels(info); |
| 339 | } |
| 340 | |
| 341 | static bool make_checkerboard_bitmap(SkBitmap& bitmap) { |
| 342 | bool success = setup_bitmap_for_canvas(&bitmap); |
| 343 | |
| 344 | SkCanvas canvas(bitmap); |
| 345 | canvas.clear(0x00000000); |
| 346 | SkPaint darkPaint; |
| 347 | darkPaint.setColor(0xFF804020); |
| 348 | SkPaint lightPaint; |
| 349 | lightPaint.setColor(0xFF244484); |
| 350 | const int i = kBitmapSize / 8; |
| 351 | const SkScalar f = SkIntToScalar(i); |
| 352 | for (int y = 0; y < kBitmapSize; y += i) { |
| 353 | for (int x = 0; x < kBitmapSize; x += i) { |
| 354 | canvas.save(); |
| 355 | canvas.translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 356 | canvas.drawRect(SkRect::MakeXYWH(0, 0, f, f), darkPaint); |
| 357 | canvas.drawRect(SkRect::MakeXYWH(f, 0, f, f), lightPaint); |
| 358 | canvas.drawRect(SkRect::MakeXYWH(0, f, f, f), lightPaint); |
| 359 | canvas.drawRect(SkRect::MakeXYWH(f, f, f, f), darkPaint); |
| 360 | canvas.restore(); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | return success; |
| 365 | } |
| 366 | |
| 367 | static bool drawSomething(SkCanvas* canvas) { |
| 368 | SkPaint paint; |
| 369 | SkBitmap bitmap; |
| 370 | bool success = make_checkerboard_bitmap(bitmap); |
| 371 | |
| 372 | canvas->save(); |
| 373 | canvas->scale(0.5f, 0.5f); |
| 374 | canvas->drawBitmap(bitmap, 0, 0, NULL); |
| 375 | canvas->restore(); |
| 376 | |
| 377 | const char beforeStr[] = "before circle"; |
| 378 | const char afterStr[] = "after circle"; |
| 379 | |
| 380 | paint.setAntiAlias(true); |
| 381 | |
| 382 | paint.setColor(SK_ColorRED); |
| 383 | canvas->drawData(beforeStr, sizeof(beforeStr)); |
| 384 | canvas->drawCircle(SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/3), paint); |
| 385 | canvas->drawData(afterStr, sizeof(afterStr)); |
| 386 | paint.setColor(SK_ColorBLACK); |
| 387 | paint.setTextSize(SkIntToScalar(kBitmapSize/3)); |
| 388 | canvas->drawText("Picture", 7, SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/4), paint); |
| 389 | |
| 390 | return success; |
| 391 | } |
| 392 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 393 | DEF_TEST(Serialization, reporter) { |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 394 | // Test matrix serialization |
| 395 | { |
| 396 | SkMatrix matrix = SkMatrix::I(); |
| 397 | TestObjectSerialization(&matrix, reporter); |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 398 | } |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 399 | |
| 400 | // Test path serialization |
| 401 | { |
| 402 | SkPath path; |
| 403 | TestObjectSerialization(&path, reporter); |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 404 | } |
sugoi@google.com | 305f78e | 2013-11-04 16:18:15 +0000 | [diff] [blame] | 405 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 406 | // Test region serialization |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 407 | { |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 408 | SkRegion region; |
| 409 | TestObjectSerialization(®ion, reporter); |
| 410 | } |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 411 | |
commit-bot@chromium.org | 1ac99c8 | 2014-04-29 15:35:23 +0000 | [diff] [blame] | 412 | // Test string serialization |
| 413 | { |
| 414 | SkString string("string"); |
| 415 | TestObjectSerializationNoAlign<SkString, false>(&string, reporter); |
| 416 | TestObjectSerializationNoAlign<SkString, true>(&string, reporter); |
| 417 | } |
| 418 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 419 | // Test rrect serialization |
| 420 | { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 421 | // SkRRect does not initialize anything. |
| 422 | // An uninitialized SkRRect can be serialized, |
| 423 | // but will branch on uninitialized data when deserialized. |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 424 | SkRRect rrect; |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 425 | SkRect rect = SkRect::MakeXYWH(1, 2, 20, 30); |
| 426 | SkVector corners[4] = { {1, 2}, {2, 3}, {3,4}, {4,5} }; |
| 427 | rrect.setRectRadii(rect, corners); |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 428 | TestAlignment(&rrect, reporter); |
| 429 | } |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 430 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 431 | // Test readByteArray |
| 432 | { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 433 | unsigned char data[kArraySize] = { 1, 2, 3 }; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 434 | TestArraySerialization(data, reporter); |
| 435 | } |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 436 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 437 | // Test readColorArray |
| 438 | { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 439 | SkColor data[kArraySize] = { SK_ColorBLACK, SK_ColorWHITE, SK_ColorRED }; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 440 | TestArraySerialization(data, reporter); |
| 441 | } |
| 442 | |
| 443 | // Test readIntArray |
| 444 | { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 445 | int32_t data[kArraySize] = { 1, 2, 4, 8 }; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 446 | TestArraySerialization(data, reporter); |
| 447 | } |
| 448 | |
| 449 | // Test readPointArray |
| 450 | { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 451 | SkPoint data[kArraySize] = { {6, 7}, {42, 128} }; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 452 | TestArraySerialization(data, reporter); |
| 453 | } |
| 454 | |
| 455 | // Test readScalarArray |
| 456 | { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 457 | SkScalar data[kArraySize] = { SK_Scalar1, SK_ScalarHalf, SK_ScalarMax }; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 458 | TestArraySerialization(data, reporter); |
commit-bot@chromium.org | 0251288 | 2013-10-31 18:37:50 +0000 | [diff] [blame] | 459 | } |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 460 | |
| 461 | // Test invalid deserializations |
| 462 | { |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 463 | SkImageInfo info = SkImageInfo::MakeN32Premul(kBitmapSize, kBitmapSize); |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 464 | |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 465 | SkBitmap validBitmap; |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 466 | validBitmap.setInfo(info); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 467 | |
| 468 | // Create a bitmap with a really large height |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 469 | info.fHeight = 1000000000; |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 470 | SkBitmap invalidBitmap; |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 471 | invalidBitmap.setInfo(info); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 472 | |
| 473 | // The deserialization should succeed, and the rendering shouldn't crash, |
| 474 | // even when the device fails to initialize, due to its size |
| 475 | TestBitmapSerialization(validBitmap, invalidBitmap, true, reporter); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 476 | } |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 477 | |
| 478 | // Test simple SkPicture serialization |
| 479 | { |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 480 | SkPictureRecorder recorder; |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 481 | bool didDraw = drawSomething(recorder.beginRecording(kBitmapSize, kBitmapSize, NULL, 0)); |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 482 | REPORTER_ASSERT(reporter, didDraw); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 483 | SkAutoTUnref<SkPicture> pict(recorder.endRecording()); |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 484 | |
| 485 | // Serialize picture |
| 486 | SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag); |
| 487 | pict->flatten(writer); |
| 488 | size_t size = writer.bytesWritten(); |
commit-bot@chromium.org | 1e7ee99 | 2014-03-14 21:22:22 +0000 | [diff] [blame] | 489 | SkAutoTMalloc<unsigned char> data(size); |
| 490 | writer.writeToMemory(static_cast<void*>(data.get())); |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 491 | |
| 492 | // Deserialize picture |
commit-bot@chromium.org | 1e7ee99 | 2014-03-14 21:22:22 +0000 | [diff] [blame] | 493 | SkValidatingReadBuffer reader(static_cast<void*>(data.get()), size); |
commit-bot@chromium.org | e5eee51 | 2014-03-13 16:18:49 +0000 | [diff] [blame] | 494 | SkAutoTUnref<SkPicture> readPict( |
| 495 | SkPicture::CreateFromBuffer(reader)); |
| 496 | REPORTER_ASSERT(reporter, NULL != readPict.get()); |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 497 | } |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 498 | |
| 499 | TestPictureTypefaceSerialization(reporter); |
commit-bot@chromium.org | 0251288 | 2013-10-31 18:37:50 +0000 | [diff] [blame] | 500 | } |