blob: dfc7afa4fc4ef3c4365129dc4414edbf89c3e18a [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.org1e7ee992014-03-14 21:22:22 +000012#include "SkTemplates.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000013#include "SkWriteBuffer.h"
commit-bot@chromium.org02512882013-10-31 18:37:50 +000014#include "SkValidatingReadBuffer.h"
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +000015#include "SkXfermodeImageFilter.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000016#include "Test.h"
commit-bot@chromium.org02512882013-10-31 18:37:50 +000017
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000018static const uint32_t kArraySize = 64;
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +000019static const int kBitmapSize = 256;
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000020
21template<typename T>
22static 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
31template<typename T> struct SerializationUtils {
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +000032 // Generic case for flattenables
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000033 static void Write(SkWriteBuffer& writer, const T* flattenable) {
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +000034 writer.writeFlattenable(flattenable);
35 }
36 static void Read(SkValidatingReadBuffer& reader, T** flattenable) {
37 *flattenable = (T*)reader.readFlattenable(T::GetFlattenableType());
38 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000039};
40
41template<> struct SerializationUtils<SkMatrix> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000042 static void Write(SkWriteBuffer& writer, const SkMatrix* matrix) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000043 writer.writeMatrix(*matrix);
44 }
45 static void Read(SkValidatingReadBuffer& reader, SkMatrix* matrix) {
46 reader.readMatrix(matrix);
47 }
48};
49
50template<> struct SerializationUtils<SkPath> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000051 static void Write(SkWriteBuffer& writer, const SkPath* path) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000052 writer.writePath(*path);
53 }
54 static void Read(SkValidatingReadBuffer& reader, SkPath* path) {
55 reader.readPath(path);
56 }
57};
58
59template<> struct SerializationUtils<SkRegion> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000060 static void Write(SkWriteBuffer& writer, const SkRegion* region) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000061 writer.writeRegion(*region);
62 }
63 static void Read(SkValidatingReadBuffer& reader, SkRegion* region) {
64 reader.readRegion(region);
65 }
66};
67
68template<> struct SerializationUtils<unsigned char> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000069 static void Write(SkWriteBuffer& writer, unsigned char* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +000070 writer.writeByteArray(data, arraySize);
sugoi@google.com305f78e2013-11-04 16:18:15 +000071 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000072 static bool Read(SkValidatingReadBuffer& reader, unsigned char* data, uint32_t arraySize) {
73 return reader.readByteArray(data, arraySize);
74 }
75};
commit-bot@chromium.org02512882013-10-31 18:37:50 +000076
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000077template<> struct SerializationUtils<SkColor> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000078 static void Write(SkWriteBuffer& writer, SkColor* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +000079 writer.writeColorArray(data, arraySize);
sugoi@google.comb48a59a2013-11-04 20:28:23 +000080 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000081 static bool Read(SkValidatingReadBuffer& reader, SkColor* data, uint32_t arraySize) {
82 return reader.readColorArray(data, arraySize);
83 }
84};
sugoi@google.com305f78e2013-11-04 16:18:15 +000085
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000086template<> struct SerializationUtils<int32_t> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000087 static void Write(SkWriteBuffer& writer, int32_t* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +000088 writer.writeIntArray(data, arraySize);
sugoi@google.comb48a59a2013-11-04 20:28:23 +000089 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000090 static bool Read(SkValidatingReadBuffer& reader, int32_t* data, uint32_t arraySize) {
91 return reader.readIntArray(data, arraySize);
92 }
93};
sugoi@google.com305f78e2013-11-04 16:18:15 +000094
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000095template<> struct SerializationUtils<SkPoint> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000096 static void Write(SkWriteBuffer& writer, SkPoint* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +000097 writer.writePointArray(data, arraySize);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000098 }
99 static bool Read(SkValidatingReadBuffer& reader, SkPoint* data, uint32_t arraySize) {
100 return reader.readPointArray(data, arraySize);
101 }
102};
reed@google.com12a23862013-11-04 21:35:55 +0000103
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000104template<> struct SerializationUtils<SkScalar> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000105 static void Write(SkWriteBuffer& writer, SkScalar* data, uint32_t arraySize) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000106 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.com12a23862013-11-04 21:35:55 +0000112
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000113template<typename T>
114static void TestObjectSerialization(T* testObj, skiatest::Reporter* reporter) {
commit-bot@chromium.orga2bd2d12014-01-30 22:16:32 +0000115 SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000116 SerializationUtils<T>::Write(writer, testObj);
117 size_t bytesWritten = writer.bytesWritten();
118 REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten);
reed@google.com12a23862013-11-04 21:35:55 +0000119
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000120 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.org8f457e32013-11-08 19:22:57 +0000125 T obj;
126 SerializationUtils<T>::Read(buffer, &obj);
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000127 REPORTER_ASSERT(reporter, !buffer.isValid());
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000128
129 // Make sure this succeeds when it should
130 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten);
commit-bot@chromium.org8f457e32013-11-08 19:22:57 +0000131 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.org4faa8692013-11-05 15:46:56 +0000135 // This should have succeeded, since there are enough bytes to read this
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000136 REPORTER_ASSERT(reporter, buffer2.isValid());
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000137 REPORTER_ASSERT(reporter, static_cast<size_t>(peekAfter - peekBefore) == bytesWritten);
138
139 TestAlignment(testObj, reporter);
140}
141
142template<typename T>
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000143static T* TestFlattenableSerialization(T* testObj, bool shouldSucceed,
144 skiatest::Reporter* reporter) {
commit-bot@chromium.orga2bd2d12014-01-30 22:16:32 +0000145 SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000146 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.combf790232013-12-13 19:45:58 +0000151 SkASSERT(bytesWritten <= sizeof(dataWritten));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000152 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.orgc2e9db32013-12-06 20:14:46 +0000158 REPORTER_ASSERT(reporter, !buffer.isValid());
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000159 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.orgc2e9db32013-12-06 20:14:46 +0000169 REPORTER_ASSERT(reporter, buffer2.isValid());
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000170 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.orgc2e9db32013-12-06 20:14:46 +0000174 REPORTER_ASSERT(reporter, !buffer.isValid());
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000175 REPORTER_ASSERT(reporter, NULL == obj2);
176 }
177
178 return obj2; // Return object to perform further validity tests on it
179}
180
181template<typename T>
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000182static void TestArraySerialization(T* data, skiatest::Reporter* reporter) {
commit-bot@chromium.orga2bd2d12014-01-30 22:16:32 +0000183 SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000184 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.orgcd3b15c2013-12-04 17:06:49 +0000206static void TestBitmapSerialization(const SkBitmap& validBitmap,
207 const SkBitmap& invalidBitmap,
208 bool shouldSucceed,
209 skiatest::Reporter* reporter) {
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000210 SkAutoTUnref<SkBitmapSource> validBitmapSource(SkBitmapSource::Create(validBitmap));
211 SkAutoTUnref<SkBitmapSource> invalidBitmapSource(SkBitmapSource::Create(invalidBitmap));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000212 SkAutoTUnref<SkXfermode> mode(SkXfermode::Create(SkXfermode::kSrcOver_Mode));
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000213 SkAutoTUnref<SkXfermodeImageFilter> xfermodeImageFilter(
214 SkXfermodeImageFilter::Create(mode, invalidBitmapSource, validBitmapSource));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000215
216 SkAutoTUnref<SkImageFilter> deserializedFilter(
217 TestFlattenableSerialization<SkImageFilter>(
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000218 xfermodeImageFilter, shouldSucceed, reporter));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000219
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.orgdeee4962014-02-13 14:41:43 +0000224 bitmap.allocN32Pixels(24, 24);
225 SkCanvas canvas(bitmap);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000226 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.org9e5f85e2014-03-12 14:46:41 +0000234static 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
240static 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
266static 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.orge4fafb12013-12-12 21:11:12 +0000292DEF_TEST(Serialization, reporter) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000293 // 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.comb48a59a2013-11-04 20:28:23 +0000303 }
sugoi@google.com305f78e2013-11-04 16:18:15 +0000304
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000305 // Test region serialization
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000306 {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000307 SkRegion region;
308 TestObjectSerialization(&region, reporter);
309 }
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000310
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000311 // Test rrect serialization
312 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000313 // SkRRect does not initialize anything.
314 // An uninitialized SkRRect can be serialized,
315 // but will branch on uninitialized data when deserialized.
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000316 SkRRect rrect;
rmistry@google.comd6bab022013-12-02 13:50:38 +0000317 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.org4faa8692013-11-05 15:46:56 +0000320 TestAlignment(&rrect, reporter);
321 }
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000322
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000323 // Test readByteArray
324 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000325 unsigned char data[kArraySize] = { 1, 2, 3 };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000326 TestArraySerialization(data, reporter);
327 }
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000328
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000329 // Test readColorArray
330 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000331 SkColor data[kArraySize] = { SK_ColorBLACK, SK_ColorWHITE, SK_ColorRED };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000332 TestArraySerialization(data, reporter);
333 }
334
335 // Test readIntArray
336 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000337 int32_t data[kArraySize] = { 1, 2, 4, 8 };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000338 TestArraySerialization(data, reporter);
339 }
340
341 // Test readPointArray
342 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000343 SkPoint data[kArraySize] = { {6, 7}, {42, 128} };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000344 TestArraySerialization(data, reporter);
345 }
346
347 // Test readScalarArray
348 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000349 SkScalar data[kArraySize] = { SK_Scalar1, SK_ScalarHalf, SK_ScalarMax };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000350 TestArraySerialization(data, reporter);
commit-bot@chromium.org02512882013-10-31 18:37:50 +0000351 }
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000352
353 // Test invalid deserializations
354 {
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000355 SkImageInfo info = SkImageInfo::MakeN32Premul(kBitmapSize, kBitmapSize);
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000356
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000357 SkBitmap validBitmap;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000358 validBitmap.setConfig(info);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000359
360 // Create a bitmap with a really large height
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000361 info.fHeight = 1000000000;
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000362 SkBitmap invalidBitmap;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000363 invalidBitmap.setConfig(info);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000364
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.orgcd3b15c2013-12-04 17:06:49 +0000368 }
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000369
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.org1e7ee992014-03-14 21:22:22 +0000382 SkAutoTMalloc<unsigned char> data(size);
383 writer.writeToMemory(static_cast<void*>(data.get()));
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000384
385 // Deserialize picture
commit-bot@chromium.org1e7ee992014-03-14 21:22:22 +0000386 SkValidatingReadBuffer reader(static_cast<void*>(data.get()), size);
commit-bot@chromium.orge5eee512014-03-13 16:18:49 +0000387 SkAutoTUnref<SkPicture> readPict(
388 SkPicture::CreateFromBuffer(reader));
389 REPORTER_ASSERT(reporter, NULL != readPict.get());
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000390 }
commit-bot@chromium.org02512882013-10-31 18:37:50 +0000391}