blob: f3dafc821c1ef900b770653cb944c1b1c49d8cd6 [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;
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +000018static const int kBitmapSize = 256;
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000019
20template<typename T>
21static void TestAlignment(T* testObj, skiatest::Reporter* reporter) {
22 // Test memory read/write functions directly
23 unsigned char dataWritten[1024];
24 size_t bytesWrittenToMemory = testObj->writeToMemory(dataWritten);
25 REPORTER_ASSERT(reporter, SkAlign4(bytesWrittenToMemory) == bytesWrittenToMemory);
26 size_t bytesReadFromMemory = testObj->readFromMemory(dataWritten, bytesWrittenToMemory);
27 REPORTER_ASSERT(reporter, SkAlign4(bytesReadFromMemory) == bytesReadFromMemory);
28}
29
30template<typename T> struct SerializationUtils {
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +000031 // Generic case for flattenables
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000032 static void Write(SkWriteBuffer& writer, const T* flattenable) {
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +000033 writer.writeFlattenable(flattenable);
34 }
35 static void Read(SkValidatingReadBuffer& reader, T** flattenable) {
36 *flattenable = (T*)reader.readFlattenable(T::GetFlattenableType());
37 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000038};
39
40template<> struct SerializationUtils<SkMatrix> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000041 static void Write(SkWriteBuffer& writer, const SkMatrix* matrix) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000042 writer.writeMatrix(*matrix);
43 }
44 static void Read(SkValidatingReadBuffer& reader, SkMatrix* matrix) {
45 reader.readMatrix(matrix);
46 }
47};
48
49template<> struct SerializationUtils<SkPath> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000050 static void Write(SkWriteBuffer& writer, const SkPath* path) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000051 writer.writePath(*path);
52 }
53 static void Read(SkValidatingReadBuffer& reader, SkPath* path) {
54 reader.readPath(path);
55 }
56};
57
58template<> struct SerializationUtils<SkRegion> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000059 static void Write(SkWriteBuffer& writer, const SkRegion* region) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000060 writer.writeRegion(*region);
61 }
62 static void Read(SkValidatingReadBuffer& reader, SkRegion* region) {
63 reader.readRegion(region);
64 }
65};
66
67template<> struct SerializationUtils<unsigned char> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000068 static void Write(SkWriteBuffer& writer, unsigned char* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +000069 writer.writeByteArray(data, arraySize);
sugoi@google.com305f78e2013-11-04 16:18:15 +000070 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000071 static bool Read(SkValidatingReadBuffer& reader, unsigned char* data, uint32_t arraySize) {
72 return reader.readByteArray(data, arraySize);
73 }
74};
commit-bot@chromium.org02512882013-10-31 18:37:50 +000075
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000076template<> struct SerializationUtils<SkColor> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000077 static void Write(SkWriteBuffer& writer, SkColor* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +000078 writer.writeColorArray(data, arraySize);
sugoi@google.comb48a59a2013-11-04 20:28:23 +000079 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000080 static bool Read(SkValidatingReadBuffer& reader, SkColor* data, uint32_t arraySize) {
81 return reader.readColorArray(data, arraySize);
82 }
83};
sugoi@google.com305f78e2013-11-04 16:18:15 +000084
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000085template<> struct SerializationUtils<int32_t> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000086 static void Write(SkWriteBuffer& writer, int32_t* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +000087 writer.writeIntArray(data, arraySize);
sugoi@google.comb48a59a2013-11-04 20:28:23 +000088 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000089 static bool Read(SkValidatingReadBuffer& reader, int32_t* data, uint32_t arraySize) {
90 return reader.readIntArray(data, arraySize);
91 }
92};
sugoi@google.com305f78e2013-11-04 16:18:15 +000093
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000094template<> struct SerializationUtils<SkPoint> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000095 static void Write(SkWriteBuffer& writer, SkPoint* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +000096 writer.writePointArray(data, arraySize);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000097 }
98 static bool Read(SkValidatingReadBuffer& reader, SkPoint* data, uint32_t arraySize) {
99 return reader.readPointArray(data, arraySize);
100 }
101};
reed@google.com12a23862013-11-04 21:35:55 +0000102
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000103template<> struct SerializationUtils<SkScalar> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000104 static void Write(SkWriteBuffer& writer, SkScalar* data, uint32_t arraySize) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000105 writer.writeScalarArray(data, arraySize);
106 }
107 static bool Read(SkValidatingReadBuffer& reader, SkScalar* data, uint32_t arraySize) {
108 return reader.readScalarArray(data, arraySize);
109 }
110};
reed@google.com12a23862013-11-04 21:35:55 +0000111
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000112template<typename T>
113static void TestObjectSerialization(T* testObj, skiatest::Reporter* reporter) {
commit-bot@chromium.orga2bd2d12014-01-30 22:16:32 +0000114 SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000115 SerializationUtils<T>::Write(writer, testObj);
116 size_t bytesWritten = writer.bytesWritten();
117 REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten);
reed@google.com12a23862013-11-04 21:35:55 +0000118
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000119 unsigned char dataWritten[1024];
120 writer.writeToMemory(dataWritten);
121
122 // Make sure this fails when it should (test with smaller size, but still multiple of 4)
123 SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 4);
commit-bot@chromium.org8f457e32013-11-08 19:22:57 +0000124 T obj;
125 SerializationUtils<T>::Read(buffer, &obj);
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000126 REPORTER_ASSERT(reporter, !buffer.isValid());
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000127
128 // Make sure this succeeds when it should
129 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten);
commit-bot@chromium.org8f457e32013-11-08 19:22:57 +0000130 const unsigned char* peekBefore = static_cast<const unsigned char*>(buffer2.skip(0));
131 T obj2;
132 SerializationUtils<T>::Read(buffer2, &obj2);
133 const unsigned char* peekAfter = static_cast<const unsigned char*>(buffer2.skip(0));
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000134 // This should have succeeded, since there are enough bytes to read this
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000135 REPORTER_ASSERT(reporter, buffer2.isValid());
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000136 REPORTER_ASSERT(reporter, static_cast<size_t>(peekAfter - peekBefore) == bytesWritten);
137
138 TestAlignment(testObj, reporter);
139}
140
141template<typename T>
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000142static T* TestFlattenableSerialization(T* testObj, bool shouldSucceed,
143 skiatest::Reporter* reporter) {
commit-bot@chromium.orga2bd2d12014-01-30 22:16:32 +0000144 SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000145 SerializationUtils<T>::Write(writer, testObj);
146 size_t bytesWritten = writer.bytesWritten();
147 REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten);
148
149 unsigned char dataWritten[1024];
reed@google.combf790232013-12-13 19:45:58 +0000150 SkASSERT(bytesWritten <= sizeof(dataWritten));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000151 writer.writeToMemory(dataWritten);
152
153 // Make sure this fails when it should (test with smaller size, but still multiple of 4)
154 SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 4);
155 T* obj = NULL;
156 SerializationUtils<T>::Read(buffer, &obj);
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000157 REPORTER_ASSERT(reporter, !buffer.isValid());
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000158 REPORTER_ASSERT(reporter, NULL == obj);
159
160 // Make sure this succeeds when it should
161 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten);
162 const unsigned char* peekBefore = static_cast<const unsigned char*>(buffer2.skip(0));
163 T* obj2 = NULL;
164 SerializationUtils<T>::Read(buffer2, &obj2);
165 const unsigned char* peekAfter = static_cast<const unsigned char*>(buffer2.skip(0));
166 if (shouldSucceed) {
167 // This should have succeeded, since there are enough bytes to read this
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000168 REPORTER_ASSERT(reporter, buffer2.isValid());
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000169 REPORTER_ASSERT(reporter, static_cast<size_t>(peekAfter - peekBefore) == bytesWritten);
170 REPORTER_ASSERT(reporter, NULL != obj2);
171 } else {
172 // If the deserialization was supposed to fail, make sure it did
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000173 REPORTER_ASSERT(reporter, !buffer.isValid());
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000174 REPORTER_ASSERT(reporter, NULL == obj2);
175 }
176
177 return obj2; // Return object to perform further validity tests on it
178}
179
180template<typename T>
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000181static void TestArraySerialization(T* data, skiatest::Reporter* reporter) {
commit-bot@chromium.orga2bd2d12014-01-30 22:16:32 +0000182 SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000183 SerializationUtils<T>::Write(writer, data, kArraySize);
184 size_t bytesWritten = writer.bytesWritten();
185 // This should write the length (in 4 bytes) and the array
186 REPORTER_ASSERT(reporter, (4 + kArraySize * sizeof(T)) == bytesWritten);
187
188 unsigned char dataWritten[1024];
189 writer.writeToMemory(dataWritten);
190
191 // Make sure this fails when it should
192 SkValidatingReadBuffer buffer(dataWritten, bytesWritten);
193 T dataRead[kArraySize];
194 bool success = SerializationUtils<T>::Read(buffer, dataRead, kArraySize / 2);
195 // This should have failed, since the provided size was too small
196 REPORTER_ASSERT(reporter, !success);
197
198 // Make sure this succeeds when it should
199 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten);
200 success = SerializationUtils<T>::Read(buffer2, dataRead, kArraySize);
201 // This should have succeeded, since there are enough bytes to read this
202 REPORTER_ASSERT(reporter, success);
203}
204
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000205static void TestBitmapSerialization(const SkBitmap& validBitmap,
206 const SkBitmap& invalidBitmap,
207 bool shouldSucceed,
208 skiatest::Reporter* reporter) {
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000209 SkAutoTUnref<SkBitmapSource> validBitmapSource(SkBitmapSource::Create(validBitmap));
210 SkAutoTUnref<SkBitmapSource> invalidBitmapSource(SkBitmapSource::Create(invalidBitmap));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000211 SkAutoTUnref<SkXfermode> mode(SkXfermode::Create(SkXfermode::kSrcOver_Mode));
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000212 SkAutoTUnref<SkXfermodeImageFilter> xfermodeImageFilter(
213 SkXfermodeImageFilter::Create(mode, invalidBitmapSource, validBitmapSource));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000214
215 SkAutoTUnref<SkImageFilter> deserializedFilter(
216 TestFlattenableSerialization<SkImageFilter>(
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000217 xfermodeImageFilter, shouldSucceed, reporter));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000218
219 // Try to render a small bitmap using the invalid deserialized filter
220 // to make sure we don't crash while trying to render it
221 if (shouldSucceed) {
222 SkBitmap bitmap;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000223 bitmap.allocN32Pixels(24, 24);
224 SkCanvas canvas(bitmap);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000225 canvas.clear(0x00000000);
226 SkPaint paint;
227 paint.setImageFilter(deserializedFilter);
228 canvas.clipRect(SkRect::MakeXYWH(0, 0, SkIntToScalar(24), SkIntToScalar(24)));
229 canvas.drawBitmap(bitmap, 0, 0, &paint);
230 }
231}
232
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000233static bool setup_bitmap_for_canvas(SkBitmap* bitmap) {
234 SkImageInfo info = SkImageInfo::Make(
235 kBitmapSize, kBitmapSize, kPMColor_SkColorType, kPremul_SkAlphaType);
236 return bitmap->allocPixels(info);
237}
238
239static bool make_checkerboard_bitmap(SkBitmap& bitmap) {
240 bool success = setup_bitmap_for_canvas(&bitmap);
241
242 SkCanvas canvas(bitmap);
243 canvas.clear(0x00000000);
244 SkPaint darkPaint;
245 darkPaint.setColor(0xFF804020);
246 SkPaint lightPaint;
247 lightPaint.setColor(0xFF244484);
248 const int i = kBitmapSize / 8;
249 const SkScalar f = SkIntToScalar(i);
250 for (int y = 0; y < kBitmapSize; y += i) {
251 for (int x = 0; x < kBitmapSize; x += i) {
252 canvas.save();
253 canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
254 canvas.drawRect(SkRect::MakeXYWH(0, 0, f, f), darkPaint);
255 canvas.drawRect(SkRect::MakeXYWH(f, 0, f, f), lightPaint);
256 canvas.drawRect(SkRect::MakeXYWH(0, f, f, f), lightPaint);
257 canvas.drawRect(SkRect::MakeXYWH(f, f, f, f), darkPaint);
258 canvas.restore();
259 }
260 }
261
262 return success;
263}
264
265static bool drawSomething(SkCanvas* canvas) {
266 SkPaint paint;
267 SkBitmap bitmap;
268 bool success = make_checkerboard_bitmap(bitmap);
269
270 canvas->save();
271 canvas->scale(0.5f, 0.5f);
272 canvas->drawBitmap(bitmap, 0, 0, NULL);
273 canvas->restore();
274
275 const char beforeStr[] = "before circle";
276 const char afterStr[] = "after circle";
277
278 paint.setAntiAlias(true);
279
280 paint.setColor(SK_ColorRED);
281 canvas->drawData(beforeStr, sizeof(beforeStr));
282 canvas->drawCircle(SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/3), paint);
283 canvas->drawData(afterStr, sizeof(afterStr));
284 paint.setColor(SK_ColorBLACK);
285 paint.setTextSize(SkIntToScalar(kBitmapSize/3));
286 canvas->drawText("Picture", 7, SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/4), paint);
287
288 return success;
289}
290
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000291DEF_TEST(Serialization, reporter) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000292 // Test matrix serialization
293 {
294 SkMatrix matrix = SkMatrix::I();
295 TestObjectSerialization(&matrix, reporter);
296 }
297
298 // Test path serialization
299 {
300 SkPath path;
301 TestObjectSerialization(&path, reporter);
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000302 }
sugoi@google.com305f78e2013-11-04 16:18:15 +0000303
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000304 // Test region serialization
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000305 {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000306 SkRegion region;
307 TestObjectSerialization(&region, reporter);
308 }
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000309
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000310 // Test rrect serialization
311 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000312 // SkRRect does not initialize anything.
313 // An uninitialized SkRRect can be serialized,
314 // but will branch on uninitialized data when deserialized.
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000315 SkRRect rrect;
rmistry@google.comd6bab022013-12-02 13:50:38 +0000316 SkRect rect = SkRect::MakeXYWH(1, 2, 20, 30);
317 SkVector corners[4] = { {1, 2}, {2, 3}, {3,4}, {4,5} };
318 rrect.setRectRadii(rect, corners);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000319 TestAlignment(&rrect, reporter);
320 }
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000321
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000322 // Test readByteArray
323 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000324 unsigned char data[kArraySize] = { 1, 2, 3 };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000325 TestArraySerialization(data, reporter);
326 }
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000327
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000328 // Test readColorArray
329 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000330 SkColor data[kArraySize] = { SK_ColorBLACK, SK_ColorWHITE, SK_ColorRED };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000331 TestArraySerialization(data, reporter);
332 }
333
334 // Test readIntArray
335 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000336 int32_t data[kArraySize] = { 1, 2, 4, 8 };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000337 TestArraySerialization(data, reporter);
338 }
339
340 // Test readPointArray
341 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000342 SkPoint data[kArraySize] = { {6, 7}, {42, 128} };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000343 TestArraySerialization(data, reporter);
344 }
345
346 // Test readScalarArray
347 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000348 SkScalar data[kArraySize] = { SK_Scalar1, SK_ScalarHalf, SK_ScalarMax };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000349 TestArraySerialization(data, reporter);
commit-bot@chromium.org02512882013-10-31 18:37:50 +0000350 }
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000351
352 // Test invalid deserializations
353 {
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000354 SkImageInfo info = SkImageInfo::MakeN32Premul(kBitmapSize, kBitmapSize);
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000355
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000356 SkBitmap validBitmap;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000357 validBitmap.setConfig(info);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000358
359 // Create a bitmap with a really large height
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000360 info.fHeight = 1000000000;
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000361 SkBitmap invalidBitmap;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000362 invalidBitmap.setConfig(info);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000363
364 // The deserialization should succeed, and the rendering shouldn't crash,
365 // even when the device fails to initialize, due to its size
366 TestBitmapSerialization(validBitmap, invalidBitmap, true, reporter);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000367 }
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000368
369 // Test simple SkPicture serialization
370 {
371 SkPicture* pict = new SkPicture;
372 SkAutoUnref aur(pict);
373 bool didDraw = drawSomething(pict->beginRecording(kBitmapSize, kBitmapSize));
374 REPORTER_ASSERT(reporter, didDraw);
375 pict->endRecording();
376
377 // Serialize picture
378 SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag);
379 pict->flatten(writer);
380 size_t size = writer.bytesWritten();
381 void* data = sk_malloc_throw(size);
382 writer.writeToMemory(data);
383
384 // Deserialize picture
385 SkValidatingReadBuffer reader(data, size);
386 SkPicture* readPict(SkPicture::CreateFromBuffer(reader));
387 REPORTER_ASSERT(reporter, NULL != readPict);
388 }
commit-bot@chromium.org02512882013-10-31 18:37:50 +0000389}