blob: c8f8e4926e050b98816ba97fcdcb90327da1b0cf [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
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +000069template<> struct SerializationUtils<SkString> {
70 static void Write(SkWriteBuffer& writer, const SkString* string) {
71 writer.writeString(string->c_str());
72 }
73 static void Read(SkValidatingReadBuffer& reader, SkString* string) {
74 reader.readString(string);
75 }
76};
77
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000078template<> struct SerializationUtils<unsigned char> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000079 static void Write(SkWriteBuffer& writer, unsigned char* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +000080 writer.writeByteArray(data, arraySize);
sugoi@google.com305f78e2013-11-04 16:18:15 +000081 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000082 static bool Read(SkValidatingReadBuffer& reader, unsigned char* data, uint32_t arraySize) {
83 return reader.readByteArray(data, arraySize);
84 }
85};
commit-bot@chromium.org02512882013-10-31 18:37:50 +000086
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000087template<> struct SerializationUtils<SkColor> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000088 static void Write(SkWriteBuffer& writer, SkColor* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +000089 writer.writeColorArray(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, SkColor* data, uint32_t arraySize) {
92 return reader.readColorArray(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<int32_t> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000097 static void Write(SkWriteBuffer& writer, int32_t* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +000098 writer.writeIntArray(data, arraySize);
sugoi@google.comb48a59a2013-11-04 20:28:23 +000099 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000100 static bool Read(SkValidatingReadBuffer& reader, int32_t* data, uint32_t arraySize) {
101 return reader.readIntArray(data, arraySize);
102 }
103};
sugoi@google.com305f78e2013-11-04 16:18:15 +0000104
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000105template<> struct SerializationUtils<SkPoint> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000106 static void Write(SkWriteBuffer& writer, SkPoint* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +0000107 writer.writePointArray(data, arraySize);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000108 }
109 static bool Read(SkValidatingReadBuffer& reader, SkPoint* data, uint32_t arraySize) {
110 return reader.readPointArray(data, arraySize);
111 }
112};
reed@google.com12a23862013-11-04 21:35:55 +0000113
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000114template<> struct SerializationUtils<SkScalar> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000115 static void Write(SkWriteBuffer& writer, SkScalar* data, uint32_t arraySize) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000116 writer.writeScalarArray(data, arraySize);
117 }
118 static bool Read(SkValidatingReadBuffer& reader, SkScalar* data, uint32_t arraySize) {
119 return reader.readScalarArray(data, arraySize);
120 }
121};
reed@google.com12a23862013-11-04 21:35:55 +0000122
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000123template<typename T, bool testInvalid> struct SerializationTestUtils {
124 static void InvalidateData(unsigned char* data) {}
125};
126
127template<> struct SerializationTestUtils<SkString, true> {
128 static void InvalidateData(unsigned char* data) {
129 data[3] |= 0x80; // Reverse sign of 1st integer
130 }
131};
132
133template<typename T, bool testInvalid>
134static void TestObjectSerializationNoAlign(T* testObj, skiatest::Reporter* reporter) {
commit-bot@chromium.orga2bd2d12014-01-30 22:16:32 +0000135 SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000136 SerializationUtils<T>::Write(writer, testObj);
137 size_t bytesWritten = writer.bytesWritten();
138 REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten);
reed@google.com12a23862013-11-04 21:35:55 +0000139
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000140 unsigned char dataWritten[1024];
141 writer.writeToMemory(dataWritten);
142
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000143 SerializationTestUtils<T, testInvalid>::InvalidateData(dataWritten);
144
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000145 // Make sure this fails when it should (test with smaller size, but still multiple of 4)
146 SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 4);
commit-bot@chromium.org8f457e32013-11-08 19:22:57 +0000147 T obj;
148 SerializationUtils<T>::Read(buffer, &obj);
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000149 REPORTER_ASSERT(reporter, !buffer.isValid());
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000150
151 // Make sure this succeeds when it should
152 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten);
commit-bot@chromium.org8f457e32013-11-08 19:22:57 +0000153 const unsigned char* peekBefore = static_cast<const unsigned char*>(buffer2.skip(0));
154 T obj2;
155 SerializationUtils<T>::Read(buffer2, &obj2);
156 const unsigned char* peekAfter = static_cast<const unsigned char*>(buffer2.skip(0));
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000157 // This should have succeeded, since there are enough bytes to read this
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000158 REPORTER_ASSERT(reporter, buffer2.isValid() == !testInvalid);
159 // Note: This following test should always succeed, regardless of whether the buffer is valid,
160 // since if it is invalid, it will simply skip to the end, as if it had read the whole buffer.
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000161 REPORTER_ASSERT(reporter, static_cast<size_t>(peekAfter - peekBefore) == bytesWritten);
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000162}
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000163
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000164template<typename T>
165static void TestObjectSerialization(T* testObj, skiatest::Reporter* reporter) {
166 TestObjectSerializationNoAlign<T, false>(testObj, reporter);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000167 TestAlignment(testObj, reporter);
168}
169
170template<typename T>
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000171static T* TestFlattenableSerialization(T* testObj, bool shouldSucceed,
172 skiatest::Reporter* reporter) {
commit-bot@chromium.orga2bd2d12014-01-30 22:16:32 +0000173 SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000174 SerializationUtils<T>::Write(writer, testObj);
175 size_t bytesWritten = writer.bytesWritten();
176 REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten);
177
178 unsigned char dataWritten[1024];
reed@google.combf790232013-12-13 19:45:58 +0000179 SkASSERT(bytesWritten <= sizeof(dataWritten));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000180 writer.writeToMemory(dataWritten);
181
182 // Make sure this fails when it should (test with smaller size, but still multiple of 4)
183 SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 4);
184 T* obj = NULL;
185 SerializationUtils<T>::Read(buffer, &obj);
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000186 REPORTER_ASSERT(reporter, !buffer.isValid());
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000187 REPORTER_ASSERT(reporter, NULL == obj);
188
189 // Make sure this succeeds when it should
190 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten);
191 const unsigned char* peekBefore = static_cast<const unsigned char*>(buffer2.skip(0));
192 T* obj2 = NULL;
193 SerializationUtils<T>::Read(buffer2, &obj2);
194 const unsigned char* peekAfter = static_cast<const unsigned char*>(buffer2.skip(0));
195 if (shouldSucceed) {
196 // This should have succeeded, since there are enough bytes to read this
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000197 REPORTER_ASSERT(reporter, buffer2.isValid());
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000198 REPORTER_ASSERT(reporter, static_cast<size_t>(peekAfter - peekBefore) == bytesWritten);
199 REPORTER_ASSERT(reporter, NULL != obj2);
200 } else {
201 // If the deserialization was supposed to fail, make sure it did
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000202 REPORTER_ASSERT(reporter, !buffer.isValid());
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000203 REPORTER_ASSERT(reporter, NULL == obj2);
204 }
205
206 return obj2; // Return object to perform further validity tests on it
207}
208
209template<typename T>
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000210static void TestArraySerialization(T* data, skiatest::Reporter* reporter) {
commit-bot@chromium.orga2bd2d12014-01-30 22:16:32 +0000211 SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000212 SerializationUtils<T>::Write(writer, data, kArraySize);
213 size_t bytesWritten = writer.bytesWritten();
214 // This should write the length (in 4 bytes) and the array
215 REPORTER_ASSERT(reporter, (4 + kArraySize * sizeof(T)) == bytesWritten);
216
217 unsigned char dataWritten[1024];
218 writer.writeToMemory(dataWritten);
219
220 // Make sure this fails when it should
221 SkValidatingReadBuffer buffer(dataWritten, bytesWritten);
222 T dataRead[kArraySize];
223 bool success = SerializationUtils<T>::Read(buffer, dataRead, kArraySize / 2);
224 // This should have failed, since the provided size was too small
225 REPORTER_ASSERT(reporter, !success);
226
227 // Make sure this succeeds when it should
228 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten);
229 success = SerializationUtils<T>::Read(buffer2, dataRead, kArraySize);
230 // This should have succeeded, since there are enough bytes to read this
231 REPORTER_ASSERT(reporter, success);
232}
233
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000234static void TestBitmapSerialization(const SkBitmap& validBitmap,
235 const SkBitmap& invalidBitmap,
236 bool shouldSucceed,
237 skiatest::Reporter* reporter) {
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000238 SkAutoTUnref<SkBitmapSource> validBitmapSource(SkBitmapSource::Create(validBitmap));
239 SkAutoTUnref<SkBitmapSource> invalidBitmapSource(SkBitmapSource::Create(invalidBitmap));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000240 SkAutoTUnref<SkXfermode> mode(SkXfermode::Create(SkXfermode::kSrcOver_Mode));
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000241 SkAutoTUnref<SkXfermodeImageFilter> xfermodeImageFilter(
242 SkXfermodeImageFilter::Create(mode, invalidBitmapSource, validBitmapSource));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000243
244 SkAutoTUnref<SkImageFilter> deserializedFilter(
245 TestFlattenableSerialization<SkImageFilter>(
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000246 xfermodeImageFilter, shouldSucceed, reporter));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000247
248 // Try to render a small bitmap using the invalid deserialized filter
249 // to make sure we don't crash while trying to render it
250 if (shouldSucceed) {
251 SkBitmap bitmap;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000252 bitmap.allocN32Pixels(24, 24);
253 SkCanvas canvas(bitmap);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000254 canvas.clear(0x00000000);
255 SkPaint paint;
256 paint.setImageFilter(deserializedFilter);
257 canvas.clipRect(SkRect::MakeXYWH(0, 0, SkIntToScalar(24), SkIntToScalar(24)));
258 canvas.drawBitmap(bitmap, 0, 0, &paint);
259 }
260}
261
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000262static bool setup_bitmap_for_canvas(SkBitmap* bitmap) {
263 SkImageInfo info = SkImageInfo::Make(
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000264 kBitmapSize, kBitmapSize, kN32_SkColorType, kPremul_SkAlphaType);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000265 return bitmap->allocPixels(info);
266}
267
268static bool make_checkerboard_bitmap(SkBitmap& bitmap) {
269 bool success = setup_bitmap_for_canvas(&bitmap);
270
271 SkCanvas canvas(bitmap);
272 canvas.clear(0x00000000);
273 SkPaint darkPaint;
274 darkPaint.setColor(0xFF804020);
275 SkPaint lightPaint;
276 lightPaint.setColor(0xFF244484);
277 const int i = kBitmapSize / 8;
278 const SkScalar f = SkIntToScalar(i);
279 for (int y = 0; y < kBitmapSize; y += i) {
280 for (int x = 0; x < kBitmapSize; x += i) {
281 canvas.save();
282 canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
283 canvas.drawRect(SkRect::MakeXYWH(0, 0, f, f), darkPaint);
284 canvas.drawRect(SkRect::MakeXYWH(f, 0, f, f), lightPaint);
285 canvas.drawRect(SkRect::MakeXYWH(0, f, f, f), lightPaint);
286 canvas.drawRect(SkRect::MakeXYWH(f, f, f, f), darkPaint);
287 canvas.restore();
288 }
289 }
290
291 return success;
292}
293
294static bool drawSomething(SkCanvas* canvas) {
295 SkPaint paint;
296 SkBitmap bitmap;
297 bool success = make_checkerboard_bitmap(bitmap);
298
299 canvas->save();
300 canvas->scale(0.5f, 0.5f);
301 canvas->drawBitmap(bitmap, 0, 0, NULL);
302 canvas->restore();
303
304 const char beforeStr[] = "before circle";
305 const char afterStr[] = "after circle";
306
307 paint.setAntiAlias(true);
308
309 paint.setColor(SK_ColorRED);
310 canvas->drawData(beforeStr, sizeof(beforeStr));
311 canvas->drawCircle(SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/3), paint);
312 canvas->drawData(afterStr, sizeof(afterStr));
313 paint.setColor(SK_ColorBLACK);
314 paint.setTextSize(SkIntToScalar(kBitmapSize/3));
315 canvas->drawText("Picture", 7, SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/4), paint);
316
317 return success;
318}
319
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000320DEF_TEST(Serialization, reporter) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000321 // Test matrix serialization
322 {
323 SkMatrix matrix = SkMatrix::I();
324 TestObjectSerialization(&matrix, reporter);
325 }
326
327 // Test path serialization
328 {
329 SkPath path;
330 TestObjectSerialization(&path, reporter);
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000331 }
sugoi@google.com305f78e2013-11-04 16:18:15 +0000332
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000333 // Test region serialization
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000334 {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000335 SkRegion region;
336 TestObjectSerialization(&region, reporter);
337 }
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000338
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000339 // Test string serialization
340 {
341 SkString string("string");
342 TestObjectSerializationNoAlign<SkString, false>(&string, reporter);
343 TestObjectSerializationNoAlign<SkString, true>(&string, reporter);
344 }
345
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000346 // Test rrect serialization
347 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000348 // SkRRect does not initialize anything.
349 // An uninitialized SkRRect can be serialized,
350 // but will branch on uninitialized data when deserialized.
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000351 SkRRect rrect;
rmistry@google.comd6bab022013-12-02 13:50:38 +0000352 SkRect rect = SkRect::MakeXYWH(1, 2, 20, 30);
353 SkVector corners[4] = { {1, 2}, {2, 3}, {3,4}, {4,5} };
354 rrect.setRectRadii(rect, corners);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000355 TestAlignment(&rrect, reporter);
356 }
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000357
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000358 // Test readByteArray
359 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000360 unsigned char data[kArraySize] = { 1, 2, 3 };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000361 TestArraySerialization(data, reporter);
362 }
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000363
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000364 // Test readColorArray
365 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000366 SkColor data[kArraySize] = { SK_ColorBLACK, SK_ColorWHITE, SK_ColorRED };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000367 TestArraySerialization(data, reporter);
368 }
369
370 // Test readIntArray
371 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000372 int32_t data[kArraySize] = { 1, 2, 4, 8 };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000373 TestArraySerialization(data, reporter);
374 }
375
376 // Test readPointArray
377 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000378 SkPoint data[kArraySize] = { {6, 7}, {42, 128} };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000379 TestArraySerialization(data, reporter);
380 }
381
382 // Test readScalarArray
383 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000384 SkScalar data[kArraySize] = { SK_Scalar1, SK_ScalarHalf, SK_ScalarMax };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000385 TestArraySerialization(data, reporter);
commit-bot@chromium.org02512882013-10-31 18:37:50 +0000386 }
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000387
388 // Test invalid deserializations
389 {
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000390 SkImageInfo info = SkImageInfo::MakeN32Premul(kBitmapSize, kBitmapSize);
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000391
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000392 SkBitmap validBitmap;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000393 validBitmap.setConfig(info);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000394
395 // Create a bitmap with a really large height
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000396 info.fHeight = 1000000000;
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000397 SkBitmap invalidBitmap;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000398 invalidBitmap.setConfig(info);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000399
400 // The deserialization should succeed, and the rendering shouldn't crash,
401 // even when the device fails to initialize, due to its size
402 TestBitmapSerialization(validBitmap, invalidBitmap, true, reporter);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000403 }
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000404
405 // Test simple SkPicture serialization
406 {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000407 SkPictureRecorder recorder;
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000408 bool didDraw = drawSomething(recorder.beginRecording(kBitmapSize, kBitmapSize, NULL, 0));
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000409 REPORTER_ASSERT(reporter, didDraw);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000410 SkAutoTUnref<SkPicture> pict(recorder.endRecording());
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000411
412 // Serialize picture
413 SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag);
414 pict->flatten(writer);
415 size_t size = writer.bytesWritten();
commit-bot@chromium.org1e7ee992014-03-14 21:22:22 +0000416 SkAutoTMalloc<unsigned char> data(size);
417 writer.writeToMemory(static_cast<void*>(data.get()));
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000418
419 // Deserialize picture
commit-bot@chromium.org1e7ee992014-03-14 21:22:22 +0000420 SkValidatingReadBuffer reader(static_cast<void*>(data.get()), size);
commit-bot@chromium.orge5eee512014-03-13 16:18:49 +0000421 SkAutoTUnref<SkPicture> readPict(
422 SkPicture::CreateFromBuffer(reader));
423 REPORTER_ASSERT(reporter, NULL != readPict.get());
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000424 }
commit-bot@chromium.org02512882013-10-31 18:37:50 +0000425}