blob: 94d09c07354d1a21a12cd18d1a0632ee11918e46 [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"
robertphillips@google.com770963f2014-04-18 18:04:41 +000012#include "SkPictureRecorder.h"
commit-bot@chromium.org1e7ee992014-03-14 21:22:22 +000013#include "SkTemplates.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000014#include "SkWriteBuffer.h"
commit-bot@chromium.org02512882013-10-31 18:37:50 +000015#include "SkValidatingReadBuffer.h"
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +000016#include "SkXfermodeImageFilter.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000017#include "Test.h"
commit-bot@chromium.org02512882013-10-31 18:37:50 +000018
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000019static const uint32_t kArraySize = 64;
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +000020static const int kBitmapSize = 256;
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000021
22template<typename T>
23static void TestAlignment(T* testObj, skiatest::Reporter* reporter) {
24 // Test memory read/write functions directly
25 unsigned char dataWritten[1024];
26 size_t bytesWrittenToMemory = testObj->writeToMemory(dataWritten);
27 REPORTER_ASSERT(reporter, SkAlign4(bytesWrittenToMemory) == bytesWrittenToMemory);
28 size_t bytesReadFromMemory = testObj->readFromMemory(dataWritten, bytesWrittenToMemory);
29 REPORTER_ASSERT(reporter, SkAlign4(bytesReadFromMemory) == bytesReadFromMemory);
30}
31
32template<typename T> struct SerializationUtils {
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +000033 // Generic case for flattenables
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000034 static void Write(SkWriteBuffer& writer, const T* flattenable) {
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +000035 writer.writeFlattenable(flattenable);
36 }
37 static void Read(SkValidatingReadBuffer& reader, T** flattenable) {
38 *flattenable = (T*)reader.readFlattenable(T::GetFlattenableType());
39 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000040};
41
42template<> struct SerializationUtils<SkMatrix> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000043 static void Write(SkWriteBuffer& writer, const SkMatrix* matrix) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000044 writer.writeMatrix(*matrix);
45 }
46 static void Read(SkValidatingReadBuffer& reader, SkMatrix* matrix) {
47 reader.readMatrix(matrix);
48 }
49};
50
51template<> struct SerializationUtils<SkPath> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000052 static void Write(SkWriteBuffer& writer, const SkPath* path) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000053 writer.writePath(*path);
54 }
55 static void Read(SkValidatingReadBuffer& reader, SkPath* path) {
56 reader.readPath(path);
57 }
58};
59
60template<> struct SerializationUtils<SkRegion> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000061 static void Write(SkWriteBuffer& writer, const SkRegion* region) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000062 writer.writeRegion(*region);
63 }
64 static void Read(SkValidatingReadBuffer& reader, SkRegion* region) {
65 reader.readRegion(region);
66 }
67};
68
69template<> struct SerializationUtils<unsigned char> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000070 static void Write(SkWriteBuffer& writer, unsigned char* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +000071 writer.writeByteArray(data, arraySize);
sugoi@google.com305f78e2013-11-04 16:18:15 +000072 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000073 static bool Read(SkValidatingReadBuffer& reader, unsigned char* data, uint32_t arraySize) {
74 return reader.readByteArray(data, arraySize);
75 }
76};
commit-bot@chromium.org02512882013-10-31 18:37:50 +000077
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000078template<> struct SerializationUtils<SkColor> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000079 static void Write(SkWriteBuffer& writer, SkColor* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +000080 writer.writeColorArray(data, arraySize);
sugoi@google.comb48a59a2013-11-04 20:28:23 +000081 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000082 static bool Read(SkValidatingReadBuffer& reader, SkColor* data, uint32_t arraySize) {
83 return reader.readColorArray(data, arraySize);
84 }
85};
sugoi@google.com305f78e2013-11-04 16:18:15 +000086
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000087template<> struct SerializationUtils<int32_t> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000088 static void Write(SkWriteBuffer& writer, int32_t* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +000089 writer.writeIntArray(data, arraySize);
sugoi@google.comb48a59a2013-11-04 20:28:23 +000090 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000091 static bool Read(SkValidatingReadBuffer& reader, int32_t* data, uint32_t arraySize) {
92 return reader.readIntArray(data, arraySize);
93 }
94};
sugoi@google.com305f78e2013-11-04 16:18:15 +000095
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000096template<> struct SerializationUtils<SkPoint> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000097 static void Write(SkWriteBuffer& writer, SkPoint* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +000098 writer.writePointArray(data, arraySize);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000099 }
100 static bool Read(SkValidatingReadBuffer& reader, SkPoint* data, uint32_t arraySize) {
101 return reader.readPointArray(data, arraySize);
102 }
103};
reed@google.com12a23862013-11-04 21:35:55 +0000104
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000105template<> struct SerializationUtils<SkScalar> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000106 static void Write(SkWriteBuffer& writer, SkScalar* data, uint32_t arraySize) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000107 writer.writeScalarArray(data, arraySize);
108 }
109 static bool Read(SkValidatingReadBuffer& reader, SkScalar* data, uint32_t arraySize) {
110 return reader.readScalarArray(data, arraySize);
111 }
112};
reed@google.com12a23862013-11-04 21:35:55 +0000113
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000114template<typename T>
115static void TestObjectSerialization(T* testObj, skiatest::Reporter* reporter) {
commit-bot@chromium.orga2bd2d12014-01-30 22:16:32 +0000116 SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000117 SerializationUtils<T>::Write(writer, testObj);
118 size_t bytesWritten = writer.bytesWritten();
119 REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten);
reed@google.com12a23862013-11-04 21:35:55 +0000120
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000121 unsigned char dataWritten[1024];
122 writer.writeToMemory(dataWritten);
123
124 // Make sure this fails when it should (test with smaller size, but still multiple of 4)
125 SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 4);
commit-bot@chromium.org8f457e32013-11-08 19:22:57 +0000126 T obj;
127 SerializationUtils<T>::Read(buffer, &obj);
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000128 REPORTER_ASSERT(reporter, !buffer.isValid());
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000129
130 // Make sure this succeeds when it should
131 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten);
commit-bot@chromium.org8f457e32013-11-08 19:22:57 +0000132 const unsigned char* peekBefore = static_cast<const unsigned char*>(buffer2.skip(0));
133 T obj2;
134 SerializationUtils<T>::Read(buffer2, &obj2);
135 const unsigned char* peekAfter = static_cast<const unsigned char*>(buffer2.skip(0));
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000136 // This should have succeeded, since there are enough bytes to read this
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000137 REPORTER_ASSERT(reporter, buffer2.isValid());
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000138 REPORTER_ASSERT(reporter, static_cast<size_t>(peekAfter - peekBefore) == bytesWritten);
139
140 TestAlignment(testObj, reporter);
141}
142
143template<typename T>
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000144static T* TestFlattenableSerialization(T* testObj, bool shouldSucceed,
145 skiatest::Reporter* reporter) {
commit-bot@chromium.orga2bd2d12014-01-30 22:16:32 +0000146 SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000147 SerializationUtils<T>::Write(writer, testObj);
148 size_t bytesWritten = writer.bytesWritten();
149 REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten);
150
151 unsigned char dataWritten[1024];
reed@google.combf790232013-12-13 19:45:58 +0000152 SkASSERT(bytesWritten <= sizeof(dataWritten));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000153 writer.writeToMemory(dataWritten);
154
155 // Make sure this fails when it should (test with smaller size, but still multiple of 4)
156 SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 4);
157 T* obj = NULL;
158 SerializationUtils<T>::Read(buffer, &obj);
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000159 REPORTER_ASSERT(reporter, !buffer.isValid());
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000160 REPORTER_ASSERT(reporter, NULL == obj);
161
162 // Make sure this succeeds when it should
163 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten);
164 const unsigned char* peekBefore = static_cast<const unsigned char*>(buffer2.skip(0));
165 T* obj2 = NULL;
166 SerializationUtils<T>::Read(buffer2, &obj2);
167 const unsigned char* peekAfter = static_cast<const unsigned char*>(buffer2.skip(0));
168 if (shouldSucceed) {
169 // This should have succeeded, since there are enough bytes to read this
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000170 REPORTER_ASSERT(reporter, buffer2.isValid());
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000171 REPORTER_ASSERT(reporter, static_cast<size_t>(peekAfter - peekBefore) == bytesWritten);
172 REPORTER_ASSERT(reporter, NULL != obj2);
173 } else {
174 // If the deserialization was supposed to fail, make sure it did
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000175 REPORTER_ASSERT(reporter, !buffer.isValid());
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000176 REPORTER_ASSERT(reporter, NULL == obj2);
177 }
178
179 return obj2; // Return object to perform further validity tests on it
180}
181
182template<typename T>
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000183static void TestArraySerialization(T* data, skiatest::Reporter* reporter) {
commit-bot@chromium.orga2bd2d12014-01-30 22:16:32 +0000184 SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000185 SerializationUtils<T>::Write(writer, data, kArraySize);
186 size_t bytesWritten = writer.bytesWritten();
187 // This should write the length (in 4 bytes) and the array
188 REPORTER_ASSERT(reporter, (4 + kArraySize * sizeof(T)) == bytesWritten);
189
190 unsigned char dataWritten[1024];
191 writer.writeToMemory(dataWritten);
192
193 // Make sure this fails when it should
194 SkValidatingReadBuffer buffer(dataWritten, bytesWritten);
195 T dataRead[kArraySize];
196 bool success = SerializationUtils<T>::Read(buffer, dataRead, kArraySize / 2);
197 // This should have failed, since the provided size was too small
198 REPORTER_ASSERT(reporter, !success);
199
200 // Make sure this succeeds when it should
201 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten);
202 success = SerializationUtils<T>::Read(buffer2, dataRead, kArraySize);
203 // This should have succeeded, since there are enough bytes to read this
204 REPORTER_ASSERT(reporter, success);
205}
206
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000207static void TestBitmapSerialization(const SkBitmap& validBitmap,
208 const SkBitmap& invalidBitmap,
209 bool shouldSucceed,
210 skiatest::Reporter* reporter) {
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000211 SkAutoTUnref<SkBitmapSource> validBitmapSource(SkBitmapSource::Create(validBitmap));
212 SkAutoTUnref<SkBitmapSource> invalidBitmapSource(SkBitmapSource::Create(invalidBitmap));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000213 SkAutoTUnref<SkXfermode> mode(SkXfermode::Create(SkXfermode::kSrcOver_Mode));
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000214 SkAutoTUnref<SkXfermodeImageFilter> xfermodeImageFilter(
215 SkXfermodeImageFilter::Create(mode, invalidBitmapSource, validBitmapSource));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000216
217 SkAutoTUnref<SkImageFilter> deserializedFilter(
218 TestFlattenableSerialization<SkImageFilter>(
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000219 xfermodeImageFilter, shouldSucceed, reporter));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000220
221 // Try to render a small bitmap using the invalid deserialized filter
222 // to make sure we don't crash while trying to render it
223 if (shouldSucceed) {
224 SkBitmap bitmap;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000225 bitmap.allocN32Pixels(24, 24);
226 SkCanvas canvas(bitmap);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000227 canvas.clear(0x00000000);
228 SkPaint paint;
229 paint.setImageFilter(deserializedFilter);
230 canvas.clipRect(SkRect::MakeXYWH(0, 0, SkIntToScalar(24), SkIntToScalar(24)));
231 canvas.drawBitmap(bitmap, 0, 0, &paint);
232 }
233}
234
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000235static bool setup_bitmap_for_canvas(SkBitmap* bitmap) {
236 SkImageInfo info = SkImageInfo::Make(
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000237 kBitmapSize, kBitmapSize, kN32_SkColorType, kPremul_SkAlphaType);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000238 return bitmap->allocPixels(info);
239}
240
241static bool make_checkerboard_bitmap(SkBitmap& bitmap) {
242 bool success = setup_bitmap_for_canvas(&bitmap);
243
244 SkCanvas canvas(bitmap);
245 canvas.clear(0x00000000);
246 SkPaint darkPaint;
247 darkPaint.setColor(0xFF804020);
248 SkPaint lightPaint;
249 lightPaint.setColor(0xFF244484);
250 const int i = kBitmapSize / 8;
251 const SkScalar f = SkIntToScalar(i);
252 for (int y = 0; y < kBitmapSize; y += i) {
253 for (int x = 0; x < kBitmapSize; x += i) {
254 canvas.save();
255 canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
256 canvas.drawRect(SkRect::MakeXYWH(0, 0, f, f), darkPaint);
257 canvas.drawRect(SkRect::MakeXYWH(f, 0, f, f), lightPaint);
258 canvas.drawRect(SkRect::MakeXYWH(0, f, f, f), lightPaint);
259 canvas.drawRect(SkRect::MakeXYWH(f, f, f, f), darkPaint);
260 canvas.restore();
261 }
262 }
263
264 return success;
265}
266
267static bool drawSomething(SkCanvas* canvas) {
268 SkPaint paint;
269 SkBitmap bitmap;
270 bool success = make_checkerboard_bitmap(bitmap);
271
272 canvas->save();
273 canvas->scale(0.5f, 0.5f);
274 canvas->drawBitmap(bitmap, 0, 0, NULL);
275 canvas->restore();
276
277 const char beforeStr[] = "before circle";
278 const char afterStr[] = "after circle";
279
280 paint.setAntiAlias(true);
281
282 paint.setColor(SK_ColorRED);
283 canvas->drawData(beforeStr, sizeof(beforeStr));
284 canvas->drawCircle(SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/3), paint);
285 canvas->drawData(afterStr, sizeof(afterStr));
286 paint.setColor(SK_ColorBLACK);
287 paint.setTextSize(SkIntToScalar(kBitmapSize/3));
288 canvas->drawText("Picture", 7, SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/4), paint);
289
290 return success;
291}
292
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000293DEF_TEST(Serialization, reporter) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000294 // Test matrix serialization
295 {
296 SkMatrix matrix = SkMatrix::I();
297 TestObjectSerialization(&matrix, reporter);
298 }
299
300 // Test path serialization
301 {
302 SkPath path;
303 TestObjectSerialization(&path, reporter);
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000304 }
sugoi@google.com305f78e2013-11-04 16:18:15 +0000305
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000306 // Test region serialization
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000307 {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000308 SkRegion region;
309 TestObjectSerialization(&region, reporter);
310 }
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000311
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000312 // Test rrect serialization
313 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000314 // SkRRect does not initialize anything.
315 // An uninitialized SkRRect can be serialized,
316 // but will branch on uninitialized data when deserialized.
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000317 SkRRect rrect;
rmistry@google.comd6bab022013-12-02 13:50:38 +0000318 SkRect rect = SkRect::MakeXYWH(1, 2, 20, 30);
319 SkVector corners[4] = { {1, 2}, {2, 3}, {3,4}, {4,5} };
320 rrect.setRectRadii(rect, corners);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000321 TestAlignment(&rrect, reporter);
322 }
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000323
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000324 // Test readByteArray
325 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000326 unsigned char data[kArraySize] = { 1, 2, 3 };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000327 TestArraySerialization(data, reporter);
328 }
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000329
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000330 // Test readColorArray
331 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000332 SkColor data[kArraySize] = { SK_ColorBLACK, SK_ColorWHITE, SK_ColorRED };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000333 TestArraySerialization(data, reporter);
334 }
335
336 // Test readIntArray
337 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000338 int32_t data[kArraySize] = { 1, 2, 4, 8 };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000339 TestArraySerialization(data, reporter);
340 }
341
342 // Test readPointArray
343 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000344 SkPoint data[kArraySize] = { {6, 7}, {42, 128} };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000345 TestArraySerialization(data, reporter);
346 }
347
348 // Test readScalarArray
349 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000350 SkScalar data[kArraySize] = { SK_Scalar1, SK_ScalarHalf, SK_ScalarMax };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000351 TestArraySerialization(data, reporter);
commit-bot@chromium.org02512882013-10-31 18:37:50 +0000352 }
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000353
354 // Test invalid deserializations
355 {
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000356 SkImageInfo info = SkImageInfo::MakeN32Premul(kBitmapSize, kBitmapSize);
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000357
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000358 SkBitmap validBitmap;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000359 validBitmap.setConfig(info);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000360
361 // Create a bitmap with a really large height
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000362 info.fHeight = 1000000000;
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000363 SkBitmap invalidBitmap;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000364 invalidBitmap.setConfig(info);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000365
366 // The deserialization should succeed, and the rendering shouldn't crash,
367 // even when the device fails to initialize, due to its size
368 TestBitmapSerialization(validBitmap, invalidBitmap, true, reporter);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000369 }
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000370
371 // Test simple SkPicture serialization
372 {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000373 SkPictureRecorder recorder;
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000374 bool didDraw = drawSomething(recorder.beginRecording(kBitmapSize, kBitmapSize, NULL, 0));
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000375 REPORTER_ASSERT(reporter, didDraw);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000376 SkAutoTUnref<SkPicture> pict(recorder.endRecording());
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000377
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}