blob: 31dbbe8ad93c00af466b4c3f8a632c44121d5284 [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"
bungeman3489ee02015-05-14 14:18:02 -070011#include "SkFixed.h"
12#include "SkFontDescriptor.h"
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +000013#include "SkMallocPixelRef.h"
caseq26337e92014-06-30 12:14:52 -070014#include "SkOSFile.h"
robertphillips@google.com770963f2014-04-18 18:04:41 +000015#include "SkPictureRecorder.h"
senorblanco91c395a2014-09-25 15:51:35 -070016#include "SkTableColorFilter.h"
commit-bot@chromium.org1e7ee992014-03-14 21:22:22 +000017#include "SkTemplates.h"
caseq26337e92014-06-30 12:14:52 -070018#include "SkTypeface.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000019#include "SkWriteBuffer.h"
commit-bot@chromium.org02512882013-10-31 18:37:50 +000020#include "SkValidatingReadBuffer.h"
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +000021#include "SkXfermodeImageFilter.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000022#include "Test.h"
commit-bot@chromium.org02512882013-10-31 18:37:50 +000023
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000024static const uint32_t kArraySize = 64;
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +000025static const int kBitmapSize = 256;
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000026
27template<typename T>
28static void TestAlignment(T* testObj, skiatest::Reporter* reporter) {
29 // Test memory read/write functions directly
30 unsigned char dataWritten[1024];
31 size_t bytesWrittenToMemory = testObj->writeToMemory(dataWritten);
32 REPORTER_ASSERT(reporter, SkAlign4(bytesWrittenToMemory) == bytesWrittenToMemory);
33 size_t bytesReadFromMemory = testObj->readFromMemory(dataWritten, bytesWrittenToMemory);
34 REPORTER_ASSERT(reporter, SkAlign4(bytesReadFromMemory) == bytesReadFromMemory);
35}
36
37template<typename T> struct SerializationUtils {
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +000038 // Generic case for flattenables
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000039 static void Write(SkWriteBuffer& writer, const T* flattenable) {
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +000040 writer.writeFlattenable(flattenable);
41 }
42 static void Read(SkValidatingReadBuffer& reader, T** flattenable) {
43 *flattenable = (T*)reader.readFlattenable(T::GetFlattenableType());
44 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000045};
46
47template<> struct SerializationUtils<SkMatrix> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000048 static void Write(SkWriteBuffer& writer, const SkMatrix* matrix) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000049 writer.writeMatrix(*matrix);
50 }
51 static void Read(SkValidatingReadBuffer& reader, SkMatrix* matrix) {
52 reader.readMatrix(matrix);
53 }
54};
55
56template<> struct SerializationUtils<SkPath> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000057 static void Write(SkWriteBuffer& writer, const SkPath* path) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000058 writer.writePath(*path);
59 }
60 static void Read(SkValidatingReadBuffer& reader, SkPath* path) {
61 reader.readPath(path);
62 }
63};
64
65template<> struct SerializationUtils<SkRegion> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000066 static void Write(SkWriteBuffer& writer, const SkRegion* region) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000067 writer.writeRegion(*region);
68 }
69 static void Read(SkValidatingReadBuffer& reader, SkRegion* region) {
70 reader.readRegion(region);
71 }
72};
73
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +000074template<> struct SerializationUtils<SkString> {
75 static void Write(SkWriteBuffer& writer, const SkString* string) {
76 writer.writeString(string->c_str());
77 }
78 static void Read(SkValidatingReadBuffer& reader, SkString* string) {
79 reader.readString(string);
80 }
81};
82
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000083template<> struct SerializationUtils<unsigned char> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000084 static void Write(SkWriteBuffer& writer, unsigned char* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +000085 writer.writeByteArray(data, arraySize);
sugoi@google.com305f78e2013-11-04 16:18:15 +000086 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000087 static bool Read(SkValidatingReadBuffer& reader, unsigned char* data, uint32_t arraySize) {
88 return reader.readByteArray(data, arraySize);
89 }
90};
commit-bot@chromium.org02512882013-10-31 18:37:50 +000091
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000092template<> struct SerializationUtils<SkColor> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000093 static void Write(SkWriteBuffer& writer, SkColor* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +000094 writer.writeColorArray(data, arraySize);
sugoi@google.comb48a59a2013-11-04 20:28:23 +000095 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000096 static bool Read(SkValidatingReadBuffer& reader, SkColor* data, uint32_t arraySize) {
97 return reader.readColorArray(data, arraySize);
98 }
99};
sugoi@google.com305f78e2013-11-04 16:18:15 +0000100
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000101template<> struct SerializationUtils<int32_t> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000102 static void Write(SkWriteBuffer& writer, int32_t* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +0000103 writer.writeIntArray(data, arraySize);
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000104 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000105 static bool Read(SkValidatingReadBuffer& reader, int32_t* data, uint32_t arraySize) {
106 return reader.readIntArray(data, arraySize);
107 }
108};
sugoi@google.com305f78e2013-11-04 16:18:15 +0000109
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000110template<> struct SerializationUtils<SkPoint> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000111 static void Write(SkWriteBuffer& writer, SkPoint* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +0000112 writer.writePointArray(data, arraySize);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000113 }
114 static bool Read(SkValidatingReadBuffer& reader, SkPoint* data, uint32_t arraySize) {
115 return reader.readPointArray(data, arraySize);
116 }
117};
reed@google.com12a23862013-11-04 21:35:55 +0000118
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000119template<> struct SerializationUtils<SkScalar> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000120 static void Write(SkWriteBuffer& writer, SkScalar* data, uint32_t arraySize) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000121 writer.writeScalarArray(data, arraySize);
122 }
123 static bool Read(SkValidatingReadBuffer& reader, SkScalar* data, uint32_t arraySize) {
124 return reader.readScalarArray(data, arraySize);
125 }
126};
reed@google.com12a23862013-11-04 21:35:55 +0000127
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000128template<typename T, bool testInvalid> struct SerializationTestUtils {
129 static void InvalidateData(unsigned char* data) {}
130};
131
132template<> struct SerializationTestUtils<SkString, true> {
133 static void InvalidateData(unsigned char* data) {
134 data[3] |= 0x80; // Reverse sign of 1st integer
135 }
136};
137
138template<typename T, bool testInvalid>
139static void TestObjectSerializationNoAlign(T* testObj, skiatest::Reporter* reporter) {
commit-bot@chromium.orga2bd2d12014-01-30 22:16:32 +0000140 SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000141 SerializationUtils<T>::Write(writer, testObj);
142 size_t bytesWritten = writer.bytesWritten();
143 REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten);
reed@google.com12a23862013-11-04 21:35:55 +0000144
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000145 unsigned char dataWritten[1024];
146 writer.writeToMemory(dataWritten);
147
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000148 SerializationTestUtils<T, testInvalid>::InvalidateData(dataWritten);
149
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000150 // Make sure this fails when it should (test with smaller size, but still multiple of 4)
151 SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 4);
commit-bot@chromium.org8f457e32013-11-08 19:22:57 +0000152 T obj;
153 SerializationUtils<T>::Read(buffer, &obj);
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000154 REPORTER_ASSERT(reporter, !buffer.isValid());
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000155
156 // Make sure this succeeds when it should
157 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten);
commit-bot@chromium.org8f457e32013-11-08 19:22:57 +0000158 const unsigned char* peekBefore = static_cast<const unsigned char*>(buffer2.skip(0));
159 T obj2;
160 SerializationUtils<T>::Read(buffer2, &obj2);
161 const unsigned char* peekAfter = static_cast<const unsigned char*>(buffer2.skip(0));
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000162 // This should have succeeded, since there are enough bytes to read this
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000163 REPORTER_ASSERT(reporter, buffer2.isValid() == !testInvalid);
164 // Note: This following test should always succeed, regardless of whether the buffer is valid,
165 // 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 +0000166 REPORTER_ASSERT(reporter, static_cast<size_t>(peekAfter - peekBefore) == bytesWritten);
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000167}
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000168
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000169template<typename T>
170static void TestObjectSerialization(T* testObj, skiatest::Reporter* reporter) {
171 TestObjectSerializationNoAlign<T, false>(testObj, reporter);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000172 TestAlignment(testObj, reporter);
173}
174
175template<typename T>
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000176static T* TestFlattenableSerialization(T* testObj, bool shouldSucceed,
177 skiatest::Reporter* reporter) {
commit-bot@chromium.orga2bd2d12014-01-30 22:16:32 +0000178 SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000179 SerializationUtils<T>::Write(writer, testObj);
180 size_t bytesWritten = writer.bytesWritten();
181 REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten);
182
senorblanco91c395a2014-09-25 15:51:35 -0700183 unsigned char dataWritten[4096];
reed@google.combf790232013-12-13 19:45:58 +0000184 SkASSERT(bytesWritten <= sizeof(dataWritten));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000185 writer.writeToMemory(dataWritten);
186
187 // Make sure this fails when it should (test with smaller size, but still multiple of 4)
188 SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 4);
189 T* obj = NULL;
190 SerializationUtils<T>::Read(buffer, &obj);
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000191 REPORTER_ASSERT(reporter, !buffer.isValid());
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000192 REPORTER_ASSERT(reporter, NULL == obj);
193
194 // Make sure this succeeds when it should
195 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten);
196 const unsigned char* peekBefore = static_cast<const unsigned char*>(buffer2.skip(0));
197 T* obj2 = NULL;
198 SerializationUtils<T>::Read(buffer2, &obj2);
199 const unsigned char* peekAfter = static_cast<const unsigned char*>(buffer2.skip(0));
200 if (shouldSucceed) {
201 // This should have succeeded, since there are enough bytes to read this
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000202 REPORTER_ASSERT(reporter, buffer2.isValid());
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000203 REPORTER_ASSERT(reporter, static_cast<size_t>(peekAfter - peekBefore) == bytesWritten);
bsalomon49f085d2014-09-05 13:34:00 -0700204 REPORTER_ASSERT(reporter, obj2);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000205 } else {
206 // If the deserialization was supposed to fail, make sure it did
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000207 REPORTER_ASSERT(reporter, !buffer.isValid());
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000208 REPORTER_ASSERT(reporter, NULL == obj2);
209 }
210
211 return obj2; // Return object to perform further validity tests on it
212}
213
214template<typename T>
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000215static void TestArraySerialization(T* data, skiatest::Reporter* reporter) {
commit-bot@chromium.orga2bd2d12014-01-30 22:16:32 +0000216 SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000217 SerializationUtils<T>::Write(writer, data, kArraySize);
218 size_t bytesWritten = writer.bytesWritten();
219 // This should write the length (in 4 bytes) and the array
220 REPORTER_ASSERT(reporter, (4 + kArraySize * sizeof(T)) == bytesWritten);
221
222 unsigned char dataWritten[1024];
223 writer.writeToMemory(dataWritten);
224
225 // Make sure this fails when it should
226 SkValidatingReadBuffer buffer(dataWritten, bytesWritten);
227 T dataRead[kArraySize];
228 bool success = SerializationUtils<T>::Read(buffer, dataRead, kArraySize / 2);
229 // This should have failed, since the provided size was too small
230 REPORTER_ASSERT(reporter, !success);
231
232 // Make sure this succeeds when it should
233 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten);
234 success = SerializationUtils<T>::Read(buffer2, dataRead, kArraySize);
235 // This should have succeeded, since there are enough bytes to read this
236 REPORTER_ASSERT(reporter, success);
237}
238
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000239static void TestBitmapSerialization(const SkBitmap& validBitmap,
240 const SkBitmap& invalidBitmap,
241 bool shouldSucceed,
242 skiatest::Reporter* reporter) {
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000243 SkAutoTUnref<SkBitmapSource> validBitmapSource(SkBitmapSource::Create(validBitmap));
244 SkAutoTUnref<SkBitmapSource> invalidBitmapSource(SkBitmapSource::Create(invalidBitmap));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000245 SkAutoTUnref<SkXfermode> mode(SkXfermode::Create(SkXfermode::kSrcOver_Mode));
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000246 SkAutoTUnref<SkXfermodeImageFilter> xfermodeImageFilter(
247 SkXfermodeImageFilter::Create(mode, invalidBitmapSource, validBitmapSource));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000248
249 SkAutoTUnref<SkImageFilter> deserializedFilter(
250 TestFlattenableSerialization<SkImageFilter>(
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000251 xfermodeImageFilter, shouldSucceed, reporter));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000252
253 // Try to render a small bitmap using the invalid deserialized filter
254 // to make sure we don't crash while trying to render it
255 if (shouldSucceed) {
256 SkBitmap bitmap;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000257 bitmap.allocN32Pixels(24, 24);
258 SkCanvas canvas(bitmap);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000259 canvas.clear(0x00000000);
260 SkPaint paint;
261 paint.setImageFilter(deserializedFilter);
262 canvas.clipRect(SkRect::MakeXYWH(0, 0, SkIntToScalar(24), SkIntToScalar(24)));
263 canvas.drawBitmap(bitmap, 0, 0, &paint);
264 }
265}
266
senorblanco0f7197b2014-09-24 11:09:38 -0700267static void TestXfermodeSerialization(skiatest::Reporter* reporter) {
268 for (size_t i = 0; i <= SkXfermode::kLastMode; ++i) {
269 if (i == SkXfermode::kSrcOver_Mode) {
270 // skip SrcOver, as it is allowed to return NULL from Create()
271 continue;
272 }
273 SkAutoTUnref<SkXfermode> mode(SkXfermode::Create(static_cast<SkXfermode::Mode>(i)));
274 REPORTER_ASSERT(reporter, mode.get());
275 SkAutoTUnref<SkXfermode> copy(
276 TestFlattenableSerialization<SkXfermode>(mode.get(), true, reporter));
277 }
278}
279
senorblanco91c395a2014-09-25 15:51:35 -0700280static void TestColorFilterSerialization(skiatest::Reporter* reporter) {
281 uint8_t table[256];
282 for (int i = 0; i < 256; ++i) {
283 table[i] = (i * 41) % 256;
284 }
285 SkAutoTUnref<SkColorFilter> colorFilter(SkTableColorFilter::Create(table));
286 SkAutoTUnref<SkColorFilter> copy(
287 TestFlattenableSerialization<SkColorFilter>(colorFilter.get(), true, reporter));
288}
289
caseq26337e92014-06-30 12:14:52 -0700290static SkBitmap draw_picture(SkPicture& picture) {
291 SkBitmap bitmap;
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700292 bitmap.allocN32Pixels(SkScalarCeilToInt(picture.cullRect().width()),
293 SkScalarCeilToInt(picture.cullRect().height()));
caseq26337e92014-06-30 12:14:52 -0700294 SkCanvas canvas(bitmap);
robertphillipsc5ba71d2014-09-04 08:42:50 -0700295 picture.playback(&canvas);
caseq26337e92014-06-30 12:14:52 -0700296 return bitmap;
297}
298
299static void compare_bitmaps(skiatest::Reporter* reporter,
300 const SkBitmap& b1, const SkBitmap& b2) {
301 REPORTER_ASSERT(reporter, b1.width() == b2.width());
302 REPORTER_ASSERT(reporter, b1.height() == b2.height());
303 SkAutoLockPixels autoLockPixels1(b1);
304 SkAutoLockPixels autoLockPixels2(b2);
305
306 if ((b1.width() != b2.width()) ||
307 (b1.height() != b2.height())) {
308 return;
309 }
310
311 int pixelErrors = 0;
312 for (int y = 0; y < b2.height(); ++y) {
313 for (int x = 0; x < b2.width(); ++x) {
314 if (b1.getColor(x, y) != b2.getColor(x, y))
315 ++pixelErrors;
316 }
317 }
318 REPORTER_ASSERT(reporter, 0 == pixelErrors);
319}
bungeman3489ee02015-05-14 14:18:02 -0700320static void serialize_and_compare_typeface(SkTypeface* typeface, const char* text,
321 skiatest::Reporter* reporter)
322{
323 // Create a paint with the typeface.
caseq26337e92014-06-30 12:14:52 -0700324 SkPaint paint;
325 paint.setColor(SK_ColorGRAY);
326 paint.setTextSize(SkIntToScalar(30));
bungeman3489ee02015-05-14 14:18:02 -0700327 paint.setTypeface(typeface);
caseq26337e92014-06-30 12:14:52 -0700328
329 // Paint some text.
330 SkPictureRecorder recorder;
331 SkIRect canvasRect = SkIRect::MakeWH(kBitmapSize, kBitmapSize);
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700332 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(canvasRect.width()),
333 SkIntToScalar(canvasRect.height()),
334 NULL, 0);
caseq26337e92014-06-30 12:14:52 -0700335 canvas->drawColor(SK_ColorWHITE);
bungeman3489ee02015-05-14 14:18:02 -0700336 canvas->drawText(text, 2, 24, 32, paint);
caseq26337e92014-06-30 12:14:52 -0700337 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
338
339 // Serlialize picture and create its clone from stream.
340 SkDynamicMemoryWStream stream;
341 picture->serialize(&stream);
scroggoa1193e42015-01-21 12:09:53 -0800342 SkAutoTDelete<SkStream> inputStream(stream.detachAsStream());
caseq26337e92014-06-30 12:14:52 -0700343 SkAutoTUnref<SkPicture> loadedPicture(SkPicture::CreateFromStream(inputStream.get()));
344
345 // Draw both original and clone picture and compare bitmaps -- they should be identical.
346 SkBitmap origBitmap = draw_picture(*picture);
347 SkBitmap destBitmap = draw_picture(*loadedPicture);
348 compare_bitmaps(reporter, origBitmap, destBitmap);
349}
350
bungeman3489ee02015-05-14 14:18:02 -0700351static void TestPictureTypefaceSerialization(skiatest::Reporter* reporter) {
352 {
353 // Load typeface from file to test CreateFromFile with index.
354 SkString filename = GetResourcePath("/fonts/test.ttc");
355 SkAutoTUnref<SkTypeface> typeface(SkTypeface::CreateFromFile(filename.c_str(), 1));
356 if (!typeface) {
357 SkDebugf("Could not run fontstream test because test.ttc not found.");
358 } else {
359 serialize_and_compare_typeface(typeface, "A!", reporter);
360 }
361 }
362
363 {
364 // Load typeface as stream to create with axis settings.
365 SkAutoTDelete<SkStreamAsset> distortable(GetResourceAsStream("/fonts/Distortable.ttf"));
366 if (!distortable) {
367 SkDebugf("Could not run fontstream test because Distortable.ttf not found.");
368 } else {
369 SkFixed axis = SK_FixedSqrt2;
370 SkAutoTUnref<SkTypeface> typeface(SkTypeface::CreateFromFontData(
371 new SkFontData(distortable.detach(), 0, &axis, 1)));
372 if (!typeface) {
373 SkDebugf("Could not run fontstream test because Distortable.ttf not created.");
374 } else {
375 serialize_and_compare_typeface(typeface, "abc", reporter);
376 }
377 }
378 }
379}
380
reed84825042014-09-02 12:50:45 -0700381static void setup_bitmap_for_canvas(SkBitmap* bitmap) {
382 bitmap->allocN32Pixels(kBitmapSize, kBitmapSize);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000383}
384
reed84825042014-09-02 12:50:45 -0700385static void make_checkerboard_bitmap(SkBitmap& bitmap) {
386 setup_bitmap_for_canvas(&bitmap);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000387
388 SkCanvas canvas(bitmap);
389 canvas.clear(0x00000000);
390 SkPaint darkPaint;
391 darkPaint.setColor(0xFF804020);
392 SkPaint lightPaint;
393 lightPaint.setColor(0xFF244484);
394 const int i = kBitmapSize / 8;
395 const SkScalar f = SkIntToScalar(i);
396 for (int y = 0; y < kBitmapSize; y += i) {
397 for (int x = 0; x < kBitmapSize; x += i) {
398 canvas.save();
399 canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
400 canvas.drawRect(SkRect::MakeXYWH(0, 0, f, f), darkPaint);
401 canvas.drawRect(SkRect::MakeXYWH(f, 0, f, f), lightPaint);
402 canvas.drawRect(SkRect::MakeXYWH(0, f, f, f), lightPaint);
403 canvas.drawRect(SkRect::MakeXYWH(f, f, f, f), darkPaint);
404 canvas.restore();
405 }
406 }
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000407}
408
reed84825042014-09-02 12:50:45 -0700409static void draw_something(SkCanvas* canvas) {
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000410 SkPaint paint;
411 SkBitmap bitmap;
reed84825042014-09-02 12:50:45 -0700412 make_checkerboard_bitmap(bitmap);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000413
414 canvas->save();
415 canvas->scale(0.5f, 0.5f);
416 canvas->drawBitmap(bitmap, 0, 0, NULL);
417 canvas->restore();
418
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000419 paint.setAntiAlias(true);
420
421 paint.setColor(SK_ColorRED);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000422 canvas->drawCircle(SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/3), paint);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000423 paint.setColor(SK_ColorBLACK);
424 paint.setTextSize(SkIntToScalar(kBitmapSize/3));
425 canvas->drawText("Picture", 7, SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/4), paint);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000426}
427
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000428DEF_TEST(Serialization, reporter) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000429 // Test matrix serialization
430 {
431 SkMatrix matrix = SkMatrix::I();
432 TestObjectSerialization(&matrix, reporter);
caseq26337e92014-06-30 12:14:52 -0700433 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000434
435 // Test path serialization
436 {
437 SkPath path;
438 TestObjectSerialization(&path, reporter);
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000439 }
sugoi@google.com305f78e2013-11-04 16:18:15 +0000440
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000441 // Test region serialization
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000442 {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000443 SkRegion region;
444 TestObjectSerialization(&region, reporter);
445 }
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000446
senorblanco0f7197b2014-09-24 11:09:38 -0700447 // Test xfermode serialization
448 {
449 TestXfermodeSerialization(reporter);
450 }
451
senorblanco91c395a2014-09-25 15:51:35 -0700452 // Test color filter serialization
453 {
454 TestColorFilterSerialization(reporter);
455 }
456
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000457 // Test string serialization
458 {
459 SkString string("string");
460 TestObjectSerializationNoAlign<SkString, false>(&string, reporter);
461 TestObjectSerializationNoAlign<SkString, true>(&string, reporter);
462 }
463
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000464 // Test rrect serialization
465 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000466 // SkRRect does not initialize anything.
467 // An uninitialized SkRRect can be serialized,
468 // but will branch on uninitialized data when deserialized.
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000469 SkRRect rrect;
rmistry@google.comd6bab022013-12-02 13:50:38 +0000470 SkRect rect = SkRect::MakeXYWH(1, 2, 20, 30);
471 SkVector corners[4] = { {1, 2}, {2, 3}, {3,4}, {4,5} };
472 rrect.setRectRadii(rect, corners);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000473 TestAlignment(&rrect, reporter);
474 }
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000475
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000476 // Test readByteArray
477 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000478 unsigned char data[kArraySize] = { 1, 2, 3 };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000479 TestArraySerialization(data, reporter);
480 }
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000481
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000482 // Test readColorArray
483 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000484 SkColor data[kArraySize] = { SK_ColorBLACK, SK_ColorWHITE, SK_ColorRED };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000485 TestArraySerialization(data, reporter);
486 }
487
488 // Test readIntArray
489 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000490 int32_t data[kArraySize] = { 1, 2, 4, 8 };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000491 TestArraySerialization(data, reporter);
492 }
493
494 // Test readPointArray
495 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000496 SkPoint data[kArraySize] = { {6, 7}, {42, 128} };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000497 TestArraySerialization(data, reporter);
498 }
499
500 // Test readScalarArray
501 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000502 SkScalar data[kArraySize] = { SK_Scalar1, SK_ScalarHalf, SK_ScalarMax };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000503 TestArraySerialization(data, reporter);
commit-bot@chromium.org02512882013-10-31 18:37:50 +0000504 }
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000505
506 // Test invalid deserializations
507 {
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000508 SkImageInfo info = SkImageInfo::MakeN32Premul(kBitmapSize, kBitmapSize);
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000509
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000510 SkBitmap validBitmap;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000511 validBitmap.setInfo(info);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000512
513 // Create a bitmap with a really large height
514 SkBitmap invalidBitmap;
reede5ea5002014-09-03 11:54:58 -0700515 invalidBitmap.setInfo(info.makeWH(info.width(), 1000000000));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000516
517 // The deserialization should succeed, and the rendering shouldn't crash,
518 // even when the device fails to initialize, due to its size
519 TestBitmapSerialization(validBitmap, invalidBitmap, true, reporter);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000520 }
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000521
522 // Test simple SkPicture serialization
523 {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000524 SkPictureRecorder recorder;
reed84825042014-09-02 12:50:45 -0700525 draw_something(recorder.beginRecording(SkIntToScalar(kBitmapSize),
526 SkIntToScalar(kBitmapSize),
527 NULL, 0));
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000528 SkAutoTUnref<SkPicture> pict(recorder.endRecording());
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000529
530 // Serialize picture
531 SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag);
532 pict->flatten(writer);
533 size_t size = writer.bytesWritten();
commit-bot@chromium.org1e7ee992014-03-14 21:22:22 +0000534 SkAutoTMalloc<unsigned char> data(size);
535 writer.writeToMemory(static_cast<void*>(data.get()));
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000536
537 // Deserialize picture
commit-bot@chromium.org1e7ee992014-03-14 21:22:22 +0000538 SkValidatingReadBuffer reader(static_cast<void*>(data.get()), size);
commit-bot@chromium.orge5eee512014-03-13 16:18:49 +0000539 SkAutoTUnref<SkPicture> readPict(
540 SkPicture::CreateFromBuffer(reader));
bsalomon49f085d2014-09-05 13:34:00 -0700541 REPORTER_ASSERT(reporter, readPict.get());
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000542 }
caseq26337e92014-06-30 12:14:52 -0700543
544 TestPictureTypefaceSerialization(reporter);
commit-bot@chromium.org02512882013-10-31 18:37:50 +0000545}