blob: f6a8a7d5fc48d0b06f63fff33f512bb4c1630146 [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
caseq26337e92014-06-30 12:14:52 -07008#include "Resources.h"
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +00009#include "SkBitmapSource.h"
10#include "SkCanvas.h"
11#include "SkMallocPixelRef.h"
caseq26337e92014-06-30 12:14:52 -070012#include "SkOSFile.h"
robertphillips@google.com770963f2014-04-18 18:04:41 +000013#include "SkPictureRecorder.h"
senorblanco91c395a2014-09-25 15:51:35 -070014#include "SkTableColorFilter.h"
commit-bot@chromium.org1e7ee992014-03-14 21:22:22 +000015#include "SkTemplates.h"
caseq26337e92014-06-30 12:14:52 -070016#include "SkTypeface.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000017#include "SkWriteBuffer.h"
commit-bot@chromium.org02512882013-10-31 18:37:50 +000018#include "SkValidatingReadBuffer.h"
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +000019#include "SkXfermodeImageFilter.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000020#include "Test.h"
commit-bot@chromium.org02512882013-10-31 18:37:50 +000021
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000022static const uint32_t kArraySize = 64;
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +000023static const int kBitmapSize = 256;
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000024
25template<typename T>
26static void TestAlignment(T* testObj, skiatest::Reporter* reporter) {
27 // Test memory read/write functions directly
28 unsigned char dataWritten[1024];
29 size_t bytesWrittenToMemory = testObj->writeToMemory(dataWritten);
30 REPORTER_ASSERT(reporter, SkAlign4(bytesWrittenToMemory) == bytesWrittenToMemory);
31 size_t bytesReadFromMemory = testObj->readFromMemory(dataWritten, bytesWrittenToMemory);
32 REPORTER_ASSERT(reporter, SkAlign4(bytesReadFromMemory) == bytesReadFromMemory);
33}
34
35template<typename T> struct SerializationUtils {
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +000036 // Generic case for flattenables
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000037 static void Write(SkWriteBuffer& writer, const T* flattenable) {
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +000038 writer.writeFlattenable(flattenable);
39 }
40 static void Read(SkValidatingReadBuffer& reader, T** flattenable) {
41 *flattenable = (T*)reader.readFlattenable(T::GetFlattenableType());
42 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000043};
44
45template<> struct SerializationUtils<SkMatrix> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000046 static void Write(SkWriteBuffer& writer, const SkMatrix* matrix) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000047 writer.writeMatrix(*matrix);
48 }
49 static void Read(SkValidatingReadBuffer& reader, SkMatrix* matrix) {
50 reader.readMatrix(matrix);
51 }
52};
53
54template<> struct SerializationUtils<SkPath> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000055 static void Write(SkWriteBuffer& writer, const SkPath* path) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000056 writer.writePath(*path);
57 }
58 static void Read(SkValidatingReadBuffer& reader, SkPath* path) {
59 reader.readPath(path);
60 }
61};
62
63template<> struct SerializationUtils<SkRegion> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000064 static void Write(SkWriteBuffer& writer, const SkRegion* region) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000065 writer.writeRegion(*region);
66 }
67 static void Read(SkValidatingReadBuffer& reader, SkRegion* region) {
68 reader.readRegion(region);
69 }
70};
71
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +000072template<> struct SerializationUtils<SkString> {
73 static void Write(SkWriteBuffer& writer, const SkString* string) {
74 writer.writeString(string->c_str());
75 }
76 static void Read(SkValidatingReadBuffer& reader, SkString* string) {
77 reader.readString(string);
78 }
79};
80
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000081template<> struct SerializationUtils<unsigned char> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000082 static void Write(SkWriteBuffer& writer, unsigned char* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +000083 writer.writeByteArray(data, arraySize);
sugoi@google.com305f78e2013-11-04 16:18:15 +000084 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000085 static bool Read(SkValidatingReadBuffer& reader, unsigned char* data, uint32_t arraySize) {
86 return reader.readByteArray(data, arraySize);
87 }
88};
commit-bot@chromium.org02512882013-10-31 18:37:50 +000089
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000090template<> struct SerializationUtils<SkColor> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000091 static void Write(SkWriteBuffer& writer, SkColor* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +000092 writer.writeColorArray(data, arraySize);
sugoi@google.comb48a59a2013-11-04 20:28:23 +000093 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000094 static bool Read(SkValidatingReadBuffer& reader, SkColor* data, uint32_t arraySize) {
95 return reader.readColorArray(data, arraySize);
96 }
97};
sugoi@google.com305f78e2013-11-04 16:18:15 +000098
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000099template<> struct SerializationUtils<int32_t> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000100 static void Write(SkWriteBuffer& writer, int32_t* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +0000101 writer.writeIntArray(data, arraySize);
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000102 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000103 static bool Read(SkValidatingReadBuffer& reader, int32_t* data, uint32_t arraySize) {
104 return reader.readIntArray(data, arraySize);
105 }
106};
sugoi@google.com305f78e2013-11-04 16:18:15 +0000107
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000108template<> struct SerializationUtils<SkPoint> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000109 static void Write(SkWriteBuffer& writer, SkPoint* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +0000110 writer.writePointArray(data, arraySize);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000111 }
112 static bool Read(SkValidatingReadBuffer& reader, SkPoint* data, uint32_t arraySize) {
113 return reader.readPointArray(data, arraySize);
114 }
115};
reed@google.com12a23862013-11-04 21:35:55 +0000116
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000117template<> struct SerializationUtils<SkScalar> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000118 static void Write(SkWriteBuffer& writer, SkScalar* data, uint32_t arraySize) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000119 writer.writeScalarArray(data, arraySize);
120 }
121 static bool Read(SkValidatingReadBuffer& reader, SkScalar* data, uint32_t arraySize) {
122 return reader.readScalarArray(data, arraySize);
123 }
124};
reed@google.com12a23862013-11-04 21:35:55 +0000125
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000126template<typename T, bool testInvalid> struct SerializationTestUtils {
127 static void InvalidateData(unsigned char* data) {}
128};
129
130template<> struct SerializationTestUtils<SkString, true> {
131 static void InvalidateData(unsigned char* data) {
132 data[3] |= 0x80; // Reverse sign of 1st integer
133 }
134};
135
136template<typename T, bool testInvalid>
137static void TestObjectSerializationNoAlign(T* testObj, skiatest::Reporter* reporter) {
commit-bot@chromium.orga2bd2d12014-01-30 22:16:32 +0000138 SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000139 SerializationUtils<T>::Write(writer, testObj);
140 size_t bytesWritten = writer.bytesWritten();
141 REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten);
reed@google.com12a23862013-11-04 21:35:55 +0000142
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000143 unsigned char dataWritten[1024];
144 writer.writeToMemory(dataWritten);
145
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000146 SerializationTestUtils<T, testInvalid>::InvalidateData(dataWritten);
147
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000148 // Make sure this fails when it should (test with smaller size, but still multiple of 4)
149 SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 4);
commit-bot@chromium.org8f457e32013-11-08 19:22:57 +0000150 T obj;
151 SerializationUtils<T>::Read(buffer, &obj);
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000152 REPORTER_ASSERT(reporter, !buffer.isValid());
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000153
154 // Make sure this succeeds when it should
155 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten);
commit-bot@chromium.org8f457e32013-11-08 19:22:57 +0000156 const unsigned char* peekBefore = static_cast<const unsigned char*>(buffer2.skip(0));
157 T obj2;
158 SerializationUtils<T>::Read(buffer2, &obj2);
159 const unsigned char* peekAfter = static_cast<const unsigned char*>(buffer2.skip(0));
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000160 // This should have succeeded, since there are enough bytes to read this
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000161 REPORTER_ASSERT(reporter, buffer2.isValid() == !testInvalid);
162 // Note: This following test should always succeed, regardless of whether the buffer is valid,
163 // 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 +0000164 REPORTER_ASSERT(reporter, static_cast<size_t>(peekAfter - peekBefore) == bytesWritten);
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000165}
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000166
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000167template<typename T>
168static void TestObjectSerialization(T* testObj, skiatest::Reporter* reporter) {
169 TestObjectSerializationNoAlign<T, false>(testObj, reporter);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000170 TestAlignment(testObj, reporter);
171}
172
173template<typename T>
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000174static T* TestFlattenableSerialization(T* testObj, bool shouldSucceed,
175 skiatest::Reporter* reporter) {
commit-bot@chromium.orga2bd2d12014-01-30 22:16:32 +0000176 SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000177 SerializationUtils<T>::Write(writer, testObj);
178 size_t bytesWritten = writer.bytesWritten();
179 REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten);
180
senorblanco91c395a2014-09-25 15:51:35 -0700181 unsigned char dataWritten[4096];
reed@google.combf790232013-12-13 19:45:58 +0000182 SkASSERT(bytesWritten <= sizeof(dataWritten));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000183 writer.writeToMemory(dataWritten);
184
185 // Make sure this fails when it should (test with smaller size, but still multiple of 4)
186 SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 4);
187 T* obj = NULL;
188 SerializationUtils<T>::Read(buffer, &obj);
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000189 REPORTER_ASSERT(reporter, !buffer.isValid());
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000190 REPORTER_ASSERT(reporter, NULL == obj);
191
192 // Make sure this succeeds when it should
193 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten);
194 const unsigned char* peekBefore = static_cast<const unsigned char*>(buffer2.skip(0));
195 T* obj2 = NULL;
196 SerializationUtils<T>::Read(buffer2, &obj2);
197 const unsigned char* peekAfter = static_cast<const unsigned char*>(buffer2.skip(0));
198 if (shouldSucceed) {
199 // This should have succeeded, since there are enough bytes to read this
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000200 REPORTER_ASSERT(reporter, buffer2.isValid());
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000201 REPORTER_ASSERT(reporter, static_cast<size_t>(peekAfter - peekBefore) == bytesWritten);
bsalomon49f085d2014-09-05 13:34:00 -0700202 REPORTER_ASSERT(reporter, obj2);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000203 } else {
204 // If the deserialization was supposed to fail, make sure it did
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000205 REPORTER_ASSERT(reporter, !buffer.isValid());
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000206 REPORTER_ASSERT(reporter, NULL == obj2);
207 }
208
209 return obj2; // Return object to perform further validity tests on it
210}
211
212template<typename T>
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000213static void TestArraySerialization(T* data, skiatest::Reporter* reporter) {
commit-bot@chromium.orga2bd2d12014-01-30 22:16:32 +0000214 SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000215 SerializationUtils<T>::Write(writer, data, kArraySize);
216 size_t bytesWritten = writer.bytesWritten();
217 // This should write the length (in 4 bytes) and the array
218 REPORTER_ASSERT(reporter, (4 + kArraySize * sizeof(T)) == bytesWritten);
219
220 unsigned char dataWritten[1024];
221 writer.writeToMemory(dataWritten);
222
223 // Make sure this fails when it should
224 SkValidatingReadBuffer buffer(dataWritten, bytesWritten);
225 T dataRead[kArraySize];
226 bool success = SerializationUtils<T>::Read(buffer, dataRead, kArraySize / 2);
227 // This should have failed, since the provided size was too small
228 REPORTER_ASSERT(reporter, !success);
229
230 // Make sure this succeeds when it should
231 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten);
232 success = SerializationUtils<T>::Read(buffer2, dataRead, kArraySize);
233 // This should have succeeded, since there are enough bytes to read this
234 REPORTER_ASSERT(reporter, success);
235}
236
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000237static void TestBitmapSerialization(const SkBitmap& validBitmap,
238 const SkBitmap& invalidBitmap,
239 bool shouldSucceed,
240 skiatest::Reporter* reporter) {
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000241 SkAutoTUnref<SkBitmapSource> validBitmapSource(SkBitmapSource::Create(validBitmap));
242 SkAutoTUnref<SkBitmapSource> invalidBitmapSource(SkBitmapSource::Create(invalidBitmap));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000243 SkAutoTUnref<SkXfermode> mode(SkXfermode::Create(SkXfermode::kSrcOver_Mode));
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000244 SkAutoTUnref<SkXfermodeImageFilter> xfermodeImageFilter(
245 SkXfermodeImageFilter::Create(mode, invalidBitmapSource, validBitmapSource));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000246
247 SkAutoTUnref<SkImageFilter> deserializedFilter(
248 TestFlattenableSerialization<SkImageFilter>(
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000249 xfermodeImageFilter, shouldSucceed, reporter));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000250
251 // Try to render a small bitmap using the invalid deserialized filter
252 // to make sure we don't crash while trying to render it
253 if (shouldSucceed) {
254 SkBitmap bitmap;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000255 bitmap.allocN32Pixels(24, 24);
256 SkCanvas canvas(bitmap);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000257 canvas.clear(0x00000000);
258 SkPaint paint;
259 paint.setImageFilter(deserializedFilter);
260 canvas.clipRect(SkRect::MakeXYWH(0, 0, SkIntToScalar(24), SkIntToScalar(24)));
261 canvas.drawBitmap(bitmap, 0, 0, &paint);
262 }
263}
264
senorblanco0f7197b2014-09-24 11:09:38 -0700265static void TestXfermodeSerialization(skiatest::Reporter* reporter) {
266 for (size_t i = 0; i <= SkXfermode::kLastMode; ++i) {
267 if (i == SkXfermode::kSrcOver_Mode) {
268 // skip SrcOver, as it is allowed to return NULL from Create()
269 continue;
270 }
271 SkAutoTUnref<SkXfermode> mode(SkXfermode::Create(static_cast<SkXfermode::Mode>(i)));
272 REPORTER_ASSERT(reporter, mode.get());
273 SkAutoTUnref<SkXfermode> copy(
274 TestFlattenableSerialization<SkXfermode>(mode.get(), true, reporter));
275 }
276}
277
senorblanco91c395a2014-09-25 15:51:35 -0700278static void TestColorFilterSerialization(skiatest::Reporter* reporter) {
279 uint8_t table[256];
280 for (int i = 0; i < 256; ++i) {
281 table[i] = (i * 41) % 256;
282 }
283 SkAutoTUnref<SkColorFilter> colorFilter(SkTableColorFilter::Create(table));
284 SkAutoTUnref<SkColorFilter> copy(
285 TestFlattenableSerialization<SkColorFilter>(colorFilter.get(), true, reporter));
286}
287
caseq26337e92014-06-30 12:14:52 -0700288static SkBitmap draw_picture(SkPicture& picture) {
289 SkBitmap bitmap;
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700290 bitmap.allocN32Pixels(SkScalarCeilToInt(picture.cullRect().width()),
291 SkScalarCeilToInt(picture.cullRect().height()));
caseq26337e92014-06-30 12:14:52 -0700292 SkCanvas canvas(bitmap);
robertphillipsc5ba71d2014-09-04 08:42:50 -0700293 picture.playback(&canvas);
caseq26337e92014-06-30 12:14:52 -0700294 return bitmap;
295}
296
297static void compare_bitmaps(skiatest::Reporter* reporter,
298 const SkBitmap& b1, const SkBitmap& b2) {
299 REPORTER_ASSERT(reporter, b1.width() == b2.width());
300 REPORTER_ASSERT(reporter, b1.height() == b2.height());
301 SkAutoLockPixels autoLockPixels1(b1);
302 SkAutoLockPixels autoLockPixels2(b2);
303
304 if ((b1.width() != b2.width()) ||
305 (b1.height() != b2.height())) {
306 return;
307 }
308
309 int pixelErrors = 0;
310 for (int y = 0; y < b2.height(); ++y) {
311 for (int x = 0; x < b2.width(); ++x) {
312 if (b1.getColor(x, y) != b2.getColor(x, y))
313 ++pixelErrors;
314 }
315 }
316 REPORTER_ASSERT(reporter, 0 == pixelErrors);
317}
318
319static void TestPictureTypefaceSerialization(skiatest::Reporter* reporter) {
320 // Load typeface form file.
321 // This test cannot run if there is no resource path.
322 SkString resourcePath = GetResourcePath();
323 if (resourcePath.isEmpty()) {
324 SkDebugf("Could not run fontstream test because resourcePath not specified.");
325 return;
326 }
tfarinaa8e2e152014-07-28 19:26:58 -0700327 SkString filename = SkOSPath::Join(resourcePath.c_str(), "test.ttc");
bungemand71b7572014-09-18 10:55:32 -0700328 SkTypeface* typeface = SkTypeface::CreateFromFile(filename.c_str(), 1);
caseq26337e92014-06-30 12:14:52 -0700329 if (!typeface) {
330 SkDebugf("Could not run fontstream test because test.ttc not found.");
331 return;
332 }
333
334 // Create a paint with the typeface we loaded.
335 SkPaint paint;
336 paint.setColor(SK_ColorGRAY);
337 paint.setTextSize(SkIntToScalar(30));
338 SkSafeUnref(paint.setTypeface(typeface));
339
340 // Paint some text.
341 SkPictureRecorder recorder;
342 SkIRect canvasRect = SkIRect::MakeWH(kBitmapSize, kBitmapSize);
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700343 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(canvasRect.width()),
344 SkIntToScalar(canvasRect.height()),
345 NULL, 0);
caseq26337e92014-06-30 12:14:52 -0700346 canvas->drawColor(SK_ColorWHITE);
bungemand71b7572014-09-18 10:55:32 -0700347 canvas->drawText("A!", 2, 24, 32, paint);
caseq26337e92014-06-30 12:14:52 -0700348 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
349
350 // Serlialize picture and create its clone from stream.
351 SkDynamicMemoryWStream stream;
352 picture->serialize(&stream);
scroggoa1193e42015-01-21 12:09:53 -0800353 SkAutoTDelete<SkStream> inputStream(stream.detachAsStream());
caseq26337e92014-06-30 12:14:52 -0700354 SkAutoTUnref<SkPicture> loadedPicture(SkPicture::CreateFromStream(inputStream.get()));
355
356 // Draw both original and clone picture and compare bitmaps -- they should be identical.
357 SkBitmap origBitmap = draw_picture(*picture);
358 SkBitmap destBitmap = draw_picture(*loadedPicture);
359 compare_bitmaps(reporter, origBitmap, destBitmap);
360}
361
reed84825042014-09-02 12:50:45 -0700362static void setup_bitmap_for_canvas(SkBitmap* bitmap) {
363 bitmap->allocN32Pixels(kBitmapSize, kBitmapSize);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000364}
365
reed84825042014-09-02 12:50:45 -0700366static void make_checkerboard_bitmap(SkBitmap& bitmap) {
367 setup_bitmap_for_canvas(&bitmap);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000368
369 SkCanvas canvas(bitmap);
370 canvas.clear(0x00000000);
371 SkPaint darkPaint;
372 darkPaint.setColor(0xFF804020);
373 SkPaint lightPaint;
374 lightPaint.setColor(0xFF244484);
375 const int i = kBitmapSize / 8;
376 const SkScalar f = SkIntToScalar(i);
377 for (int y = 0; y < kBitmapSize; y += i) {
378 for (int x = 0; x < kBitmapSize; x += i) {
379 canvas.save();
380 canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
381 canvas.drawRect(SkRect::MakeXYWH(0, 0, f, f), darkPaint);
382 canvas.drawRect(SkRect::MakeXYWH(f, 0, f, f), lightPaint);
383 canvas.drawRect(SkRect::MakeXYWH(0, f, f, f), lightPaint);
384 canvas.drawRect(SkRect::MakeXYWH(f, f, f, f), darkPaint);
385 canvas.restore();
386 }
387 }
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000388}
389
reed84825042014-09-02 12:50:45 -0700390static void draw_something(SkCanvas* canvas) {
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000391 SkPaint paint;
392 SkBitmap bitmap;
reed84825042014-09-02 12:50:45 -0700393 make_checkerboard_bitmap(bitmap);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000394
395 canvas->save();
396 canvas->scale(0.5f, 0.5f);
397 canvas->drawBitmap(bitmap, 0, 0, NULL);
398 canvas->restore();
399
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000400 paint.setAntiAlias(true);
401
402 paint.setColor(SK_ColorRED);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000403 canvas->drawCircle(SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/3), paint);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000404 paint.setColor(SK_ColorBLACK);
405 paint.setTextSize(SkIntToScalar(kBitmapSize/3));
406 canvas->drawText("Picture", 7, SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/4), paint);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000407}
408
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000409DEF_TEST(Serialization, reporter) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000410 // Test matrix serialization
411 {
412 SkMatrix matrix = SkMatrix::I();
413 TestObjectSerialization(&matrix, reporter);
caseq26337e92014-06-30 12:14:52 -0700414 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000415
416 // Test path serialization
417 {
418 SkPath path;
419 TestObjectSerialization(&path, reporter);
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000420 }
sugoi@google.com305f78e2013-11-04 16:18:15 +0000421
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000422 // Test region serialization
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000423 {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000424 SkRegion region;
425 TestObjectSerialization(&region, reporter);
426 }
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000427
senorblanco0f7197b2014-09-24 11:09:38 -0700428 // Test xfermode serialization
429 {
430 TestXfermodeSerialization(reporter);
431 }
432
senorblanco91c395a2014-09-25 15:51:35 -0700433 // Test color filter serialization
434 {
435 TestColorFilterSerialization(reporter);
436 }
437
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000438 // Test string serialization
439 {
440 SkString string("string");
441 TestObjectSerializationNoAlign<SkString, false>(&string, reporter);
442 TestObjectSerializationNoAlign<SkString, true>(&string, reporter);
443 }
444
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000445 // Test rrect serialization
446 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000447 // SkRRect does not initialize anything.
448 // An uninitialized SkRRect can be serialized,
449 // but will branch on uninitialized data when deserialized.
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000450 SkRRect rrect;
rmistry@google.comd6bab022013-12-02 13:50:38 +0000451 SkRect rect = SkRect::MakeXYWH(1, 2, 20, 30);
452 SkVector corners[4] = { {1, 2}, {2, 3}, {3,4}, {4,5} };
453 rrect.setRectRadii(rect, corners);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000454 TestAlignment(&rrect, reporter);
455 }
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000456
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000457 // Test readByteArray
458 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000459 unsigned char data[kArraySize] = { 1, 2, 3 };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000460 TestArraySerialization(data, reporter);
461 }
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000462
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000463 // Test readColorArray
464 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000465 SkColor data[kArraySize] = { SK_ColorBLACK, SK_ColorWHITE, SK_ColorRED };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000466 TestArraySerialization(data, reporter);
467 }
468
469 // Test readIntArray
470 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000471 int32_t data[kArraySize] = { 1, 2, 4, 8 };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000472 TestArraySerialization(data, reporter);
473 }
474
475 // Test readPointArray
476 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000477 SkPoint data[kArraySize] = { {6, 7}, {42, 128} };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000478 TestArraySerialization(data, reporter);
479 }
480
481 // Test readScalarArray
482 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000483 SkScalar data[kArraySize] = { SK_Scalar1, SK_ScalarHalf, SK_ScalarMax };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000484 TestArraySerialization(data, reporter);
commit-bot@chromium.org02512882013-10-31 18:37:50 +0000485 }
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000486
487 // Test invalid deserializations
488 {
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000489 SkImageInfo info = SkImageInfo::MakeN32Premul(kBitmapSize, kBitmapSize);
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000490
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000491 SkBitmap validBitmap;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000492 validBitmap.setInfo(info);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000493
494 // Create a bitmap with a really large height
495 SkBitmap invalidBitmap;
reede5ea5002014-09-03 11:54:58 -0700496 invalidBitmap.setInfo(info.makeWH(info.width(), 1000000000));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000497
498 // The deserialization should succeed, and the rendering shouldn't crash,
499 // even when the device fails to initialize, due to its size
500 TestBitmapSerialization(validBitmap, invalidBitmap, true, reporter);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000501 }
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000502
503 // Test simple SkPicture serialization
504 {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000505 SkPictureRecorder recorder;
reed84825042014-09-02 12:50:45 -0700506 draw_something(recorder.beginRecording(SkIntToScalar(kBitmapSize),
507 SkIntToScalar(kBitmapSize),
508 NULL, 0));
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000509 SkAutoTUnref<SkPicture> pict(recorder.endRecording());
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000510
511 // Serialize picture
512 SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag);
513 pict->flatten(writer);
514 size_t size = writer.bytesWritten();
commit-bot@chromium.org1e7ee992014-03-14 21:22:22 +0000515 SkAutoTMalloc<unsigned char> data(size);
516 writer.writeToMemory(static_cast<void*>(data.get()));
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000517
518 // Deserialize picture
commit-bot@chromium.org1e7ee992014-03-14 21:22:22 +0000519 SkValidatingReadBuffer reader(static_cast<void*>(data.get()), size);
commit-bot@chromium.orge5eee512014-03-13 16:18:49 +0000520 SkAutoTUnref<SkPicture> readPict(
521 SkPicture::CreateFromBuffer(reader));
bsalomon49f085d2014-09-05 13:34:00 -0700522 REPORTER_ASSERT(reporter, readPict.get());
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000523 }
caseq26337e92014-06-30 12:14:52 -0700524
525 TestPictureTypefaceSerialization(reporter);
commit-bot@chromium.org02512882013-10-31 18:37:50 +0000526}