blob: 95067298d03c05c8f1616211bcc32e31f12268c4 [file] [log] [blame]
commit-bot@chromium.org02512882013-10-31 18:37:50 +00001/*
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.orgcd3b15c2013-12-04 17:06:49 +00008#include "SkBitmapDevice.h"
9#include "SkBitmapSource.h"
10#include "SkCanvas.h"
11#include "SkMallocPixelRef.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000012#include "SkWriteBuffer.h"
commit-bot@chromium.org02512882013-10-31 18:37:50 +000013#include "SkValidatingReadBuffer.h"
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +000014#include "SkXfermodeImageFilter.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000015#include "Test.h"
commit-bot@chromium.org02512882013-10-31 18:37:50 +000016
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000017static const uint32_t kArraySize = 64;
18
19template<typename T>
20static void TestAlignment(T* testObj, skiatest::Reporter* reporter) {
21 // Test memory read/write functions directly
22 unsigned char dataWritten[1024];
23 size_t bytesWrittenToMemory = testObj->writeToMemory(dataWritten);
24 REPORTER_ASSERT(reporter, SkAlign4(bytesWrittenToMemory) == bytesWrittenToMemory);
25 size_t bytesReadFromMemory = testObj->readFromMemory(dataWritten, bytesWrittenToMemory);
26 REPORTER_ASSERT(reporter, SkAlign4(bytesReadFromMemory) == bytesReadFromMemory);
27}
28
29template<typename T> struct SerializationUtils {
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +000030 // Generic case for flattenables
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000031 static void Write(SkWriteBuffer& writer, const T* flattenable) {
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +000032 writer.writeFlattenable(flattenable);
33 }
34 static void Read(SkValidatingReadBuffer& reader, T** flattenable) {
35 *flattenable = (T*)reader.readFlattenable(T::GetFlattenableType());
36 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000037};
38
39template<> struct SerializationUtils<SkMatrix> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000040 static void Write(SkWriteBuffer& writer, const SkMatrix* matrix) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000041 writer.writeMatrix(*matrix);
42 }
43 static void Read(SkValidatingReadBuffer& reader, SkMatrix* matrix) {
44 reader.readMatrix(matrix);
45 }
46};
47
48template<> struct SerializationUtils<SkPath> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000049 static void Write(SkWriteBuffer& writer, const SkPath* path) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000050 writer.writePath(*path);
51 }
52 static void Read(SkValidatingReadBuffer& reader, SkPath* path) {
53 reader.readPath(path);
54 }
55};
56
57template<> struct SerializationUtils<SkRegion> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000058 static void Write(SkWriteBuffer& writer, const SkRegion* region) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000059 writer.writeRegion(*region);
60 }
61 static void Read(SkValidatingReadBuffer& reader, SkRegion* region) {
62 reader.readRegion(region);
63 }
64};
65
66template<> struct SerializationUtils<unsigned char> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000067 static void Write(SkWriteBuffer& writer, unsigned char* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +000068 writer.writeByteArray(data, arraySize);
sugoi@google.com305f78e2013-11-04 16:18:15 +000069 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000070 static bool Read(SkValidatingReadBuffer& reader, unsigned char* data, uint32_t arraySize) {
71 return reader.readByteArray(data, arraySize);
72 }
73};
commit-bot@chromium.org02512882013-10-31 18:37:50 +000074
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000075template<> struct SerializationUtils<SkColor> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000076 static void Write(SkWriteBuffer& writer, SkColor* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +000077 writer.writeColorArray(data, arraySize);
sugoi@google.comb48a59a2013-11-04 20:28:23 +000078 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000079 static bool Read(SkValidatingReadBuffer& reader, SkColor* data, uint32_t arraySize) {
80 return reader.readColorArray(data, arraySize);
81 }
82};
sugoi@google.com305f78e2013-11-04 16:18:15 +000083
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000084template<> struct SerializationUtils<int32_t> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000085 static void Write(SkWriteBuffer& writer, int32_t* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +000086 writer.writeIntArray(data, arraySize);
sugoi@google.comb48a59a2013-11-04 20:28:23 +000087 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000088 static bool Read(SkValidatingReadBuffer& reader, int32_t* data, uint32_t arraySize) {
89 return reader.readIntArray(data, arraySize);
90 }
91};
sugoi@google.com305f78e2013-11-04 16:18:15 +000092
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000093template<> struct SerializationUtils<SkPoint> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000094 static void Write(SkWriteBuffer& writer, SkPoint* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +000095 writer.writePointArray(data, arraySize);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000096 }
97 static bool Read(SkValidatingReadBuffer& reader, SkPoint* data, uint32_t arraySize) {
98 return reader.readPointArray(data, arraySize);
99 }
100};
reed@google.com12a23862013-11-04 21:35:55 +0000101
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000102template<> struct SerializationUtils<SkScalar> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000103 static void Write(SkWriteBuffer& writer, SkScalar* data, uint32_t arraySize) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000104 writer.writeScalarArray(data, arraySize);
105 }
106 static bool Read(SkValidatingReadBuffer& reader, SkScalar* data, uint32_t arraySize) {
107 return reader.readScalarArray(data, arraySize);
108 }
109};
reed@google.com12a23862013-11-04 21:35:55 +0000110
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000111template<typename T>
112static void TestObjectSerialization(T* testObj, skiatest::Reporter* reporter) {
commit-bot@chromium.orga2bd2d12014-01-30 22:16:32 +0000113 SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000114 SerializationUtils<T>::Write(writer, testObj);
115 size_t bytesWritten = writer.bytesWritten();
116 REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten);
reed@google.com12a23862013-11-04 21:35:55 +0000117
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000118 unsigned char dataWritten[1024];
119 writer.writeToMemory(dataWritten);
120
121 // Make sure this fails when it should (test with smaller size, but still multiple of 4)
122 SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 4);
commit-bot@chromium.org8f457e32013-11-08 19:22:57 +0000123 T obj;
124 SerializationUtils<T>::Read(buffer, &obj);
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000125 REPORTER_ASSERT(reporter, !buffer.isValid());
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000126
127 // Make sure this succeeds when it should
128 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten);
commit-bot@chromium.org8f457e32013-11-08 19:22:57 +0000129 const unsigned char* peekBefore = static_cast<const unsigned char*>(buffer2.skip(0));
130 T obj2;
131 SerializationUtils<T>::Read(buffer2, &obj2);
132 const unsigned char* peekAfter = static_cast<const unsigned char*>(buffer2.skip(0));
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000133 // This should have succeeded, since there are enough bytes to read this
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000134 REPORTER_ASSERT(reporter, buffer2.isValid());
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000135 REPORTER_ASSERT(reporter, static_cast<size_t>(peekAfter - peekBefore) == bytesWritten);
136
137 TestAlignment(testObj, reporter);
138}
139
140template<typename T>
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000141static T* TestFlattenableSerialization(T* testObj, bool shouldSucceed,
142 skiatest::Reporter* reporter) {
commit-bot@chromium.orga2bd2d12014-01-30 22:16:32 +0000143 SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000144 SerializationUtils<T>::Write(writer, testObj);
145 size_t bytesWritten = writer.bytesWritten();
146 REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten);
147
148 unsigned char dataWritten[1024];
reed@google.combf790232013-12-13 19:45:58 +0000149 SkASSERT(bytesWritten <= sizeof(dataWritten));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000150 writer.writeToMemory(dataWritten);
151
152 // Make sure this fails when it should (test with smaller size, but still multiple of 4)
153 SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 4);
154 T* obj = NULL;
155 SerializationUtils<T>::Read(buffer, &obj);
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000156 REPORTER_ASSERT(reporter, !buffer.isValid());
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000157 REPORTER_ASSERT(reporter, NULL == obj);
158
159 // Make sure this succeeds when it should
160 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten);
161 const unsigned char* peekBefore = static_cast<const unsigned char*>(buffer2.skip(0));
162 T* obj2 = NULL;
163 SerializationUtils<T>::Read(buffer2, &obj2);
164 const unsigned char* peekAfter = static_cast<const unsigned char*>(buffer2.skip(0));
165 if (shouldSucceed) {
166 // This should have succeeded, since there are enough bytes to read this
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000167 REPORTER_ASSERT(reporter, buffer2.isValid());
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000168 REPORTER_ASSERT(reporter, static_cast<size_t>(peekAfter - peekBefore) == bytesWritten);
169 REPORTER_ASSERT(reporter, NULL != obj2);
170 } else {
171 // If the deserialization was supposed to fail, make sure it did
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000172 REPORTER_ASSERT(reporter, !buffer.isValid());
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000173 REPORTER_ASSERT(reporter, NULL == obj2);
174 }
175
176 return obj2; // Return object to perform further validity tests on it
177}
178
179template<typename T>
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000180static void TestArraySerialization(T* data, skiatest::Reporter* reporter) {
commit-bot@chromium.orga2bd2d12014-01-30 22:16:32 +0000181 SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000182 SerializationUtils<T>::Write(writer, data, kArraySize);
183 size_t bytesWritten = writer.bytesWritten();
184 // This should write the length (in 4 bytes) and the array
185 REPORTER_ASSERT(reporter, (4 + kArraySize * sizeof(T)) == bytesWritten);
186
187 unsigned char dataWritten[1024];
188 writer.writeToMemory(dataWritten);
189
190 // Make sure this fails when it should
191 SkValidatingReadBuffer buffer(dataWritten, bytesWritten);
192 T dataRead[kArraySize];
193 bool success = SerializationUtils<T>::Read(buffer, dataRead, kArraySize / 2);
194 // This should have failed, since the provided size was too small
195 REPORTER_ASSERT(reporter, !success);
196
197 // Make sure this succeeds when it should
198 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten);
199 success = SerializationUtils<T>::Read(buffer2, dataRead, kArraySize);
200 // This should have succeeded, since there are enough bytes to read this
201 REPORTER_ASSERT(reporter, success);
202}
203
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000204static void TestBitmapSerialization(const SkBitmap& validBitmap,
205 const SkBitmap& invalidBitmap,
206 bool shouldSucceed,
207 skiatest::Reporter* reporter) {
208 SkBitmapSource validBitmapSource(validBitmap);
209 SkBitmapSource invalidBitmapSource(invalidBitmap);
210 SkAutoTUnref<SkXfermode> mode(SkXfermode::Create(SkXfermode::kSrcOver_Mode));
211 SkXfermodeImageFilter xfermodeImageFilter(mode, &invalidBitmapSource, &validBitmapSource);
212
213 SkAutoTUnref<SkImageFilter> deserializedFilter(
214 TestFlattenableSerialization<SkImageFilter>(
215 &xfermodeImageFilter, shouldSucceed, reporter));
216
217 // Try to render a small bitmap using the invalid deserialized filter
218 // to make sure we don't crash while trying to render it
219 if (shouldSucceed) {
220 SkBitmap bitmap;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000221 bitmap.allocN32Pixels(24, 24);
222 SkCanvas canvas(bitmap);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000223 canvas.clear(0x00000000);
224 SkPaint paint;
225 paint.setImageFilter(deserializedFilter);
226 canvas.clipRect(SkRect::MakeXYWH(0, 0, SkIntToScalar(24), SkIntToScalar(24)));
227 canvas.drawBitmap(bitmap, 0, 0, &paint);
228 }
229}
230
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000231DEF_TEST(Serialization, reporter) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000232 // Test matrix serialization
233 {
234 SkMatrix matrix = SkMatrix::I();
235 TestObjectSerialization(&matrix, reporter);
236 }
237
238 // Test path serialization
239 {
240 SkPath path;
241 TestObjectSerialization(&path, reporter);
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000242 }
sugoi@google.com305f78e2013-11-04 16:18:15 +0000243
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000244 // Test region serialization
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000245 {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000246 SkRegion region;
247 TestObjectSerialization(&region, reporter);
248 }
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000249
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000250 // Test rrect serialization
251 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000252 // SkRRect does not initialize anything.
253 // An uninitialized SkRRect can be serialized,
254 // but will branch on uninitialized data when deserialized.
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000255 SkRRect rrect;
rmistry@google.comd6bab022013-12-02 13:50:38 +0000256 SkRect rect = SkRect::MakeXYWH(1, 2, 20, 30);
257 SkVector corners[4] = { {1, 2}, {2, 3}, {3,4}, {4,5} };
258 rrect.setRectRadii(rect, corners);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000259 TestAlignment(&rrect, reporter);
260 }
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000261
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000262 // Test readByteArray
263 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000264 unsigned char data[kArraySize] = { 1, 2, 3 };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000265 TestArraySerialization(data, reporter);
266 }
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000267
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000268 // Test readColorArray
269 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000270 SkColor data[kArraySize] = { SK_ColorBLACK, SK_ColorWHITE, SK_ColorRED };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000271 TestArraySerialization(data, reporter);
272 }
273
274 // Test readIntArray
275 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000276 int32_t data[kArraySize] = { 1, 2, 4, 8 };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000277 TestArraySerialization(data, reporter);
278 }
279
280 // Test readPointArray
281 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000282 SkPoint data[kArraySize] = { {6, 7}, {42, 128} };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000283 TestArraySerialization(data, reporter);
284 }
285
286 // Test readScalarArray
287 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000288 SkScalar data[kArraySize] = { SK_Scalar1, SK_ScalarHalf, SK_ScalarMax };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000289 TestArraySerialization(data, reporter);
commit-bot@chromium.org02512882013-10-31 18:37:50 +0000290 }
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000291
292 // Test invalid deserializations
293 {
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000294 SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256);
295
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000296 SkBitmap validBitmap;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000297 validBitmap.setConfig(info);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000298
299 // Create a bitmap with a really large height
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000300 info.fHeight = 1000000000;
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000301 SkBitmap invalidBitmap;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000302 invalidBitmap.setConfig(info);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000303
304 // The deserialization should succeed, and the rendering shouldn't crash,
305 // even when the device fails to initialize, due to its size
306 TestBitmapSerialization(validBitmap, invalidBitmap, true, reporter);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000307 }
commit-bot@chromium.org02512882013-10-31 18:37:50 +0000308}