blob: 38e84bd4b431061cef586a9f98677adae038fff6 [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 "SkCanvas.h"
bungeman41868fe2015-05-20 09:21:04 -070010#include "SkFixed.h"
11#include "SkFontDescriptor.h"
fmalita5598b632015-09-15 11:26:13 -070012#include "SkImage.h"
13#include "SkImageSource.h"
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +000014#include "SkMallocPixelRef.h"
caseq26337e92014-06-30 12:14:52 -070015#include "SkOSFile.h"
robertphillips@google.com770963f2014-04-18 18:04:41 +000016#include "SkPictureRecorder.h"
senorblanco91c395a2014-09-25 15:51:35 -070017#include "SkTableColorFilter.h"
commit-bot@chromium.org1e7ee992014-03-14 21:22:22 +000018#include "SkTemplates.h"
caseq26337e92014-06-30 12:14:52 -070019#include "SkTypeface.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000020#include "SkWriteBuffer.h"
commit-bot@chromium.org02512882013-10-31 18:37:50 +000021#include "SkValidatingReadBuffer.h"
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +000022#include "SkXfermodeImageFilter.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000023#include "Test.h"
commit-bot@chromium.org02512882013-10-31 18:37:50 +000024
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000025static const uint32_t kArraySize = 64;
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +000026static const int kBitmapSize = 256;
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000027
28template<typename T>
29static void TestAlignment(T* testObj, skiatest::Reporter* reporter) {
30 // Test memory read/write functions directly
31 unsigned char dataWritten[1024];
32 size_t bytesWrittenToMemory = testObj->writeToMemory(dataWritten);
33 REPORTER_ASSERT(reporter, SkAlign4(bytesWrittenToMemory) == bytesWrittenToMemory);
34 size_t bytesReadFromMemory = testObj->readFromMemory(dataWritten, bytesWrittenToMemory);
35 REPORTER_ASSERT(reporter, SkAlign4(bytesReadFromMemory) == bytesReadFromMemory);
36}
37
38template<typename T> struct SerializationUtils {
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +000039 // Generic case for flattenables
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000040 static void Write(SkWriteBuffer& writer, const T* flattenable) {
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +000041 writer.writeFlattenable(flattenable);
42 }
43 static void Read(SkValidatingReadBuffer& reader, T** flattenable) {
44 *flattenable = (T*)reader.readFlattenable(T::GetFlattenableType());
45 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000046};
47
48template<> struct SerializationUtils<SkMatrix> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000049 static void Write(SkWriteBuffer& writer, const SkMatrix* matrix) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000050 writer.writeMatrix(*matrix);
51 }
52 static void Read(SkValidatingReadBuffer& reader, SkMatrix* matrix) {
53 reader.readMatrix(matrix);
54 }
55};
56
57template<> struct SerializationUtils<SkPath> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000058 static void Write(SkWriteBuffer& writer, const SkPath* path) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000059 writer.writePath(*path);
60 }
61 static void Read(SkValidatingReadBuffer& reader, SkPath* path) {
62 reader.readPath(path);
63 }
64};
65
66template<> struct SerializationUtils<SkRegion> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000067 static void Write(SkWriteBuffer& writer, const SkRegion* region) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000068 writer.writeRegion(*region);
69 }
70 static void Read(SkValidatingReadBuffer& reader, SkRegion* region) {
71 reader.readRegion(region);
72 }
73};
74
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +000075template<> struct SerializationUtils<SkString> {
76 static void Write(SkWriteBuffer& writer, const SkString* string) {
77 writer.writeString(string->c_str());
78 }
79 static void Read(SkValidatingReadBuffer& reader, SkString* string) {
80 reader.readString(string);
81 }
82};
83
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000084template<> struct SerializationUtils<unsigned char> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000085 static void Write(SkWriteBuffer& writer, unsigned char* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +000086 writer.writeByteArray(data, arraySize);
sugoi@google.com305f78e2013-11-04 16:18:15 +000087 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000088 static bool Read(SkValidatingReadBuffer& reader, unsigned char* data, uint32_t arraySize) {
89 return reader.readByteArray(data, arraySize);
90 }
91};
commit-bot@chromium.org02512882013-10-31 18:37:50 +000092
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000093template<> struct SerializationUtils<SkColor> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000094 static void Write(SkWriteBuffer& writer, SkColor* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +000095 writer.writeColorArray(data, arraySize);
sugoi@google.comb48a59a2013-11-04 20:28:23 +000096 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000097 static bool Read(SkValidatingReadBuffer& reader, SkColor* data, uint32_t arraySize) {
98 return reader.readColorArray(data, arraySize);
99 }
100};
sugoi@google.com305f78e2013-11-04 16:18:15 +0000101
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000102template<> struct SerializationUtils<int32_t> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000103 static void Write(SkWriteBuffer& writer, int32_t* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +0000104 writer.writeIntArray(data, arraySize);
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000105 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000106 static bool Read(SkValidatingReadBuffer& reader, int32_t* data, uint32_t arraySize) {
107 return reader.readIntArray(data, arraySize);
108 }
109};
sugoi@google.com305f78e2013-11-04 16:18:15 +0000110
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000111template<> struct SerializationUtils<SkPoint> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000112 static void Write(SkWriteBuffer& writer, SkPoint* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +0000113 writer.writePointArray(data, arraySize);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000114 }
115 static bool Read(SkValidatingReadBuffer& reader, SkPoint* data, uint32_t arraySize) {
116 return reader.readPointArray(data, arraySize);
117 }
118};
reed@google.com12a23862013-11-04 21:35:55 +0000119
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000120template<> struct SerializationUtils<SkScalar> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000121 static void Write(SkWriteBuffer& writer, SkScalar* data, uint32_t arraySize) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000122 writer.writeScalarArray(data, arraySize);
123 }
124 static bool Read(SkValidatingReadBuffer& reader, SkScalar* data, uint32_t arraySize) {
125 return reader.readScalarArray(data, arraySize);
126 }
127};
reed@google.com12a23862013-11-04 21:35:55 +0000128
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000129template<typename T, bool testInvalid> struct SerializationTestUtils {
130 static void InvalidateData(unsigned char* data) {}
131};
132
133template<> struct SerializationTestUtils<SkString, true> {
134 static void InvalidateData(unsigned char* data) {
135 data[3] |= 0x80; // Reverse sign of 1st integer
136 }
137};
138
139template<typename T, bool testInvalid>
140static void TestObjectSerializationNoAlign(T* testObj, skiatest::Reporter* reporter) {
commit-bot@chromium.orga2bd2d12014-01-30 22:16:32 +0000141 SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000142 SerializationUtils<T>::Write(writer, testObj);
143 size_t bytesWritten = writer.bytesWritten();
144 REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten);
reed@google.com12a23862013-11-04 21:35:55 +0000145
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000146 unsigned char dataWritten[1024];
147 writer.writeToMemory(dataWritten);
148
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000149 SerializationTestUtils<T, testInvalid>::InvalidateData(dataWritten);
150
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000151 // Make sure this fails when it should (test with smaller size, but still multiple of 4)
152 SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 4);
commit-bot@chromium.org8f457e32013-11-08 19:22:57 +0000153 T obj;
154 SerializationUtils<T>::Read(buffer, &obj);
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000155 REPORTER_ASSERT(reporter, !buffer.isValid());
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000156
157 // Make sure this succeeds when it should
158 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten);
commit-bot@chromium.org8f457e32013-11-08 19:22:57 +0000159 const unsigned char* peekBefore = static_cast<const unsigned char*>(buffer2.skip(0));
160 T obj2;
161 SerializationUtils<T>::Read(buffer2, &obj2);
162 const unsigned char* peekAfter = static_cast<const unsigned char*>(buffer2.skip(0));
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000163 // This should have succeeded, since there are enough bytes to read this
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000164 REPORTER_ASSERT(reporter, buffer2.isValid() == !testInvalid);
165 // Note: This following test should always succeed, regardless of whether the buffer is valid,
166 // 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 +0000167 REPORTER_ASSERT(reporter, static_cast<size_t>(peekAfter - peekBefore) == bytesWritten);
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000168}
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000169
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000170template<typename T>
171static void TestObjectSerialization(T* testObj, skiatest::Reporter* reporter) {
172 TestObjectSerializationNoAlign<T, false>(testObj, reporter);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000173 TestAlignment(testObj, reporter);
174}
175
176template<typename T>
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000177static T* TestFlattenableSerialization(T* testObj, bool shouldSucceed,
178 skiatest::Reporter* reporter) {
commit-bot@chromium.orga2bd2d12014-01-30 22:16:32 +0000179 SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000180 SerializationUtils<T>::Write(writer, testObj);
181 size_t bytesWritten = writer.bytesWritten();
182 REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten);
183
senorblanco91c395a2014-09-25 15:51:35 -0700184 unsigned char dataWritten[4096];
reed@google.combf790232013-12-13 19:45:58 +0000185 SkASSERT(bytesWritten <= sizeof(dataWritten));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000186 writer.writeToMemory(dataWritten);
187
188 // Make sure this fails when it should (test with smaller size, but still multiple of 4)
189 SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 4);
halcanary96fcdcc2015-08-27 07:41:13 -0700190 T* obj = nullptr;
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000191 SerializationUtils<T>::Read(buffer, &obj);
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000192 REPORTER_ASSERT(reporter, !buffer.isValid());
halcanary96fcdcc2015-08-27 07:41:13 -0700193 REPORTER_ASSERT(reporter, nullptr == obj);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000194
195 // Make sure this succeeds when it should
196 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten);
197 const unsigned char* peekBefore = static_cast<const unsigned char*>(buffer2.skip(0));
halcanary96fcdcc2015-08-27 07:41:13 -0700198 T* obj2 = nullptr;
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000199 SerializationUtils<T>::Read(buffer2, &obj2);
200 const unsigned char* peekAfter = static_cast<const unsigned char*>(buffer2.skip(0));
201 if (shouldSucceed) {
202 // This should have succeeded, since there are enough bytes to read this
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000203 REPORTER_ASSERT(reporter, buffer2.isValid());
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000204 REPORTER_ASSERT(reporter, static_cast<size_t>(peekAfter - peekBefore) == bytesWritten);
bsalomon49f085d2014-09-05 13:34:00 -0700205 REPORTER_ASSERT(reporter, obj2);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000206 } else {
207 // If the deserialization was supposed to fail, make sure it did
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000208 REPORTER_ASSERT(reporter, !buffer.isValid());
halcanary96fcdcc2015-08-27 07:41:13 -0700209 REPORTER_ASSERT(reporter, nullptr == obj2);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000210 }
211
212 return obj2; // Return object to perform further validity tests on it
213}
214
215template<typename T>
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000216static void TestArraySerialization(T* data, skiatest::Reporter* reporter) {
commit-bot@chromium.orga2bd2d12014-01-30 22:16:32 +0000217 SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000218 SerializationUtils<T>::Write(writer, data, kArraySize);
219 size_t bytesWritten = writer.bytesWritten();
220 // This should write the length (in 4 bytes) and the array
221 REPORTER_ASSERT(reporter, (4 + kArraySize * sizeof(T)) == bytesWritten);
222
223 unsigned char dataWritten[1024];
224 writer.writeToMemory(dataWritten);
225
226 // Make sure this fails when it should
227 SkValidatingReadBuffer buffer(dataWritten, bytesWritten);
228 T dataRead[kArraySize];
229 bool success = SerializationUtils<T>::Read(buffer, dataRead, kArraySize / 2);
230 // This should have failed, since the provided size was too small
231 REPORTER_ASSERT(reporter, !success);
232
233 // Make sure this succeeds when it should
234 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten);
235 success = SerializationUtils<T>::Read(buffer2, dataRead, kArraySize);
236 // This should have succeeded, since there are enough bytes to read this
237 REPORTER_ASSERT(reporter, success);
238}
239
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000240static void TestBitmapSerialization(const SkBitmap& validBitmap,
241 const SkBitmap& invalidBitmap,
242 bool shouldSucceed,
243 skiatest::Reporter* reporter) {
fmalita5598b632015-09-15 11:26:13 -0700244 SkAutoTUnref<SkImage> validImage(SkImage::NewFromBitmap(validBitmap));
245 SkAutoTUnref<SkImageFilter> validBitmapSource(SkImageSource::Create(validImage));
246 SkAutoTUnref<SkImage> invalidImage(SkImage::NewFromBitmap(invalidBitmap));
247 SkAutoTUnref<SkImageFilter> invalidBitmapSource(SkImageSource::Create(invalidImage));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000248 SkAutoTUnref<SkXfermode> mode(SkXfermode::Create(SkXfermode::kSrcOver_Mode));
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000249 SkAutoTUnref<SkXfermodeImageFilter> xfermodeImageFilter(
250 SkXfermodeImageFilter::Create(mode, invalidBitmapSource, validBitmapSource));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000251
252 SkAutoTUnref<SkImageFilter> deserializedFilter(
253 TestFlattenableSerialization<SkImageFilter>(
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000254 xfermodeImageFilter, shouldSucceed, reporter));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000255
256 // Try to render a small bitmap using the invalid deserialized filter
257 // to make sure we don't crash while trying to render it
258 if (shouldSucceed) {
259 SkBitmap bitmap;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000260 bitmap.allocN32Pixels(24, 24);
261 SkCanvas canvas(bitmap);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000262 canvas.clear(0x00000000);
263 SkPaint paint;
264 paint.setImageFilter(deserializedFilter);
265 canvas.clipRect(SkRect::MakeXYWH(0, 0, SkIntToScalar(24), SkIntToScalar(24)));
266 canvas.drawBitmap(bitmap, 0, 0, &paint);
267 }
268}
269
senorblanco0f7197b2014-09-24 11:09:38 -0700270static void TestXfermodeSerialization(skiatest::Reporter* reporter) {
271 for (size_t i = 0; i <= SkXfermode::kLastMode; ++i) {
272 if (i == SkXfermode::kSrcOver_Mode) {
halcanary96fcdcc2015-08-27 07:41:13 -0700273 // skip SrcOver, as it is allowed to return nullptr from Create()
senorblanco0f7197b2014-09-24 11:09:38 -0700274 continue;
275 }
276 SkAutoTUnref<SkXfermode> mode(SkXfermode::Create(static_cast<SkXfermode::Mode>(i)));
277 REPORTER_ASSERT(reporter, mode.get());
278 SkAutoTUnref<SkXfermode> copy(
279 TestFlattenableSerialization<SkXfermode>(mode.get(), true, reporter));
280 }
281}
282
senorblanco91c395a2014-09-25 15:51:35 -0700283static void TestColorFilterSerialization(skiatest::Reporter* reporter) {
284 uint8_t table[256];
285 for (int i = 0; i < 256; ++i) {
286 table[i] = (i * 41) % 256;
287 }
288 SkAutoTUnref<SkColorFilter> colorFilter(SkTableColorFilter::Create(table));
289 SkAutoTUnref<SkColorFilter> copy(
290 TestFlattenableSerialization<SkColorFilter>(colorFilter.get(), true, reporter));
291}
292
caseq26337e92014-06-30 12:14:52 -0700293static SkBitmap draw_picture(SkPicture& picture) {
294 SkBitmap bitmap;
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700295 bitmap.allocN32Pixels(SkScalarCeilToInt(picture.cullRect().width()),
296 SkScalarCeilToInt(picture.cullRect().height()));
caseq26337e92014-06-30 12:14:52 -0700297 SkCanvas canvas(bitmap);
robertphillipsc5ba71d2014-09-04 08:42:50 -0700298 picture.playback(&canvas);
caseq26337e92014-06-30 12:14:52 -0700299 return bitmap;
300}
301
302static void compare_bitmaps(skiatest::Reporter* reporter,
303 const SkBitmap& b1, const SkBitmap& b2) {
304 REPORTER_ASSERT(reporter, b1.width() == b2.width());
305 REPORTER_ASSERT(reporter, b1.height() == b2.height());
306 SkAutoLockPixels autoLockPixels1(b1);
307 SkAutoLockPixels autoLockPixels2(b2);
308
309 if ((b1.width() != b2.width()) ||
310 (b1.height() != b2.height())) {
311 return;
312 }
313
314 int pixelErrors = 0;
315 for (int y = 0; y < b2.height(); ++y) {
316 for (int x = 0; x < b2.width(); ++x) {
317 if (b1.getColor(x, y) != b2.getColor(x, y))
318 ++pixelErrors;
319 }
320 }
321 REPORTER_ASSERT(reporter, 0 == pixelErrors);
322}
bungeman41868fe2015-05-20 09:21:04 -0700323static void serialize_and_compare_typeface(SkTypeface* typeface, const char* text,
324 skiatest::Reporter* reporter)
325{
326 // Create a paint with the typeface.
caseq26337e92014-06-30 12:14:52 -0700327 SkPaint paint;
328 paint.setColor(SK_ColorGRAY);
329 paint.setTextSize(SkIntToScalar(30));
bungeman41868fe2015-05-20 09:21:04 -0700330 paint.setTypeface(typeface);
caseq26337e92014-06-30 12:14:52 -0700331
332 // Paint some text.
333 SkPictureRecorder recorder;
334 SkIRect canvasRect = SkIRect::MakeWH(kBitmapSize, kBitmapSize);
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700335 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(canvasRect.width()),
336 SkIntToScalar(canvasRect.height()),
halcanary96fcdcc2015-08-27 07:41:13 -0700337 nullptr, 0);
caseq26337e92014-06-30 12:14:52 -0700338 canvas->drawColor(SK_ColorWHITE);
bungeman41868fe2015-05-20 09:21:04 -0700339 canvas->drawText(text, 2, 24, 32, paint);
caseq26337e92014-06-30 12:14:52 -0700340 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
341
342 // Serlialize picture and create its clone from stream.
343 SkDynamicMemoryWStream stream;
344 picture->serialize(&stream);
scroggoa1193e42015-01-21 12:09:53 -0800345 SkAutoTDelete<SkStream> inputStream(stream.detachAsStream());
caseq26337e92014-06-30 12:14:52 -0700346 SkAutoTUnref<SkPicture> loadedPicture(SkPicture::CreateFromStream(inputStream.get()));
347
348 // Draw both original and clone picture and compare bitmaps -- they should be identical.
349 SkBitmap origBitmap = draw_picture(*picture);
350 SkBitmap destBitmap = draw_picture(*loadedPicture);
351 compare_bitmaps(reporter, origBitmap, destBitmap);
352}
353
bungeman41868fe2015-05-20 09:21:04 -0700354static void TestPictureTypefaceSerialization(skiatest::Reporter* reporter) {
355 {
356 // Load typeface from file to test CreateFromFile with index.
357 SkString filename = GetResourcePath("/fonts/test.ttc");
358 SkAutoTUnref<SkTypeface> typeface(SkTypeface::CreateFromFile(filename.c_str(), 1));
359 if (!typeface) {
360 SkDebugf("Could not run fontstream test because test.ttc not found.");
361 } else {
362 serialize_and_compare_typeface(typeface, "A!", reporter);
363 }
364 }
365
366 {
367 // Load typeface as stream to create with axis settings.
368 SkAutoTDelete<SkStreamAsset> distortable(GetResourceAsStream("/fonts/Distortable.ttf"));
369 if (!distortable) {
370 SkDebugf("Could not run fontstream test because Distortable.ttf not found.");
371 } else {
372 SkFixed axis = SK_FixedSqrt2;
373 SkAutoTUnref<SkTypeface> typeface(SkTypeface::CreateFromFontData(
374 new SkFontData(distortable.detach(), 0, &axis, 1)));
375 if (!typeface) {
376 SkDebugf("Could not run fontstream test because Distortable.ttf not created.");
377 } else {
378 serialize_and_compare_typeface(typeface, "abc", reporter);
379 }
380 }
381 }
382}
383
reed84825042014-09-02 12:50:45 -0700384static void setup_bitmap_for_canvas(SkBitmap* bitmap) {
385 bitmap->allocN32Pixels(kBitmapSize, kBitmapSize);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000386}
387
reed84825042014-09-02 12:50:45 -0700388static void make_checkerboard_bitmap(SkBitmap& bitmap) {
389 setup_bitmap_for_canvas(&bitmap);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000390
391 SkCanvas canvas(bitmap);
392 canvas.clear(0x00000000);
393 SkPaint darkPaint;
394 darkPaint.setColor(0xFF804020);
395 SkPaint lightPaint;
396 lightPaint.setColor(0xFF244484);
397 const int i = kBitmapSize / 8;
398 const SkScalar f = SkIntToScalar(i);
399 for (int y = 0; y < kBitmapSize; y += i) {
400 for (int x = 0; x < kBitmapSize; x += i) {
401 canvas.save();
402 canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
403 canvas.drawRect(SkRect::MakeXYWH(0, 0, f, f), darkPaint);
404 canvas.drawRect(SkRect::MakeXYWH(f, 0, f, f), lightPaint);
405 canvas.drawRect(SkRect::MakeXYWH(0, f, f, f), lightPaint);
406 canvas.drawRect(SkRect::MakeXYWH(f, f, f, f), darkPaint);
407 canvas.restore();
408 }
409 }
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000410}
411
reed84825042014-09-02 12:50:45 -0700412static void draw_something(SkCanvas* canvas) {
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000413 SkPaint paint;
414 SkBitmap bitmap;
reed84825042014-09-02 12:50:45 -0700415 make_checkerboard_bitmap(bitmap);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000416
417 canvas->save();
418 canvas->scale(0.5f, 0.5f);
halcanary96fcdcc2015-08-27 07:41:13 -0700419 canvas->drawBitmap(bitmap, 0, 0, nullptr);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000420 canvas->restore();
421
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000422 paint.setAntiAlias(true);
423
424 paint.setColor(SK_ColorRED);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000425 canvas->drawCircle(SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/3), paint);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000426 paint.setColor(SK_ColorBLACK);
427 paint.setTextSize(SkIntToScalar(kBitmapSize/3));
428 canvas->drawText("Picture", 7, SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/4), paint);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000429}
430
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000431DEF_TEST(Serialization, reporter) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000432 // Test matrix serialization
433 {
434 SkMatrix matrix = SkMatrix::I();
435 TestObjectSerialization(&matrix, reporter);
caseq26337e92014-06-30 12:14:52 -0700436 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000437
438 // Test path serialization
439 {
440 SkPath path;
441 TestObjectSerialization(&path, reporter);
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000442 }
sugoi@google.com305f78e2013-11-04 16:18:15 +0000443
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000444 // Test region serialization
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000445 {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000446 SkRegion region;
447 TestObjectSerialization(&region, reporter);
448 }
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000449
senorblanco0f7197b2014-09-24 11:09:38 -0700450 // Test xfermode serialization
451 {
452 TestXfermodeSerialization(reporter);
453 }
454
senorblanco91c395a2014-09-25 15:51:35 -0700455 // Test color filter serialization
456 {
457 TestColorFilterSerialization(reporter);
458 }
459
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000460 // Test string serialization
461 {
462 SkString string("string");
463 TestObjectSerializationNoAlign<SkString, false>(&string, reporter);
464 TestObjectSerializationNoAlign<SkString, true>(&string, reporter);
465 }
466
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000467 // Test rrect serialization
468 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000469 // SkRRect does not initialize anything.
470 // An uninitialized SkRRect can be serialized,
471 // but will branch on uninitialized data when deserialized.
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000472 SkRRect rrect;
rmistry@google.comd6bab022013-12-02 13:50:38 +0000473 SkRect rect = SkRect::MakeXYWH(1, 2, 20, 30);
474 SkVector corners[4] = { {1, 2}, {2, 3}, {3,4}, {4,5} };
475 rrect.setRectRadii(rect, corners);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000476 TestAlignment(&rrect, reporter);
477 }
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000478
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000479 // Test readByteArray
480 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000481 unsigned char data[kArraySize] = { 1, 2, 3 };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000482 TestArraySerialization(data, reporter);
483 }
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000484
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000485 // Test readColorArray
486 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000487 SkColor data[kArraySize] = { SK_ColorBLACK, SK_ColorWHITE, SK_ColorRED };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000488 TestArraySerialization(data, reporter);
489 }
490
491 // Test readIntArray
492 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000493 int32_t data[kArraySize] = { 1, 2, 4, 8 };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000494 TestArraySerialization(data, reporter);
495 }
496
497 // Test readPointArray
498 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000499 SkPoint data[kArraySize] = { {6, 7}, {42, 128} };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000500 TestArraySerialization(data, reporter);
501 }
502
503 // Test readScalarArray
504 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000505 SkScalar data[kArraySize] = { SK_Scalar1, SK_ScalarHalf, SK_ScalarMax };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000506 TestArraySerialization(data, reporter);
commit-bot@chromium.org02512882013-10-31 18:37:50 +0000507 }
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000508
509 // Test invalid deserializations
510 {
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000511 SkImageInfo info = SkImageInfo::MakeN32Premul(kBitmapSize, kBitmapSize);
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000512
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000513 SkBitmap validBitmap;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000514 validBitmap.setInfo(info);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000515
516 // Create a bitmap with a really large height
517 SkBitmap invalidBitmap;
reede5ea5002014-09-03 11:54:58 -0700518 invalidBitmap.setInfo(info.makeWH(info.width(), 1000000000));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000519
520 // The deserialization should succeed, and the rendering shouldn't crash,
521 // even when the device fails to initialize, due to its size
522 TestBitmapSerialization(validBitmap, invalidBitmap, true, reporter);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000523 }
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000524
525 // Test simple SkPicture serialization
526 {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000527 SkPictureRecorder recorder;
reed84825042014-09-02 12:50:45 -0700528 draw_something(recorder.beginRecording(SkIntToScalar(kBitmapSize),
529 SkIntToScalar(kBitmapSize),
halcanary96fcdcc2015-08-27 07:41:13 -0700530 nullptr, 0));
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000531 SkAutoTUnref<SkPicture> pict(recorder.endRecording());
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000532
533 // Serialize picture
534 SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag);
535 pict->flatten(writer);
536 size_t size = writer.bytesWritten();
commit-bot@chromium.org1e7ee992014-03-14 21:22:22 +0000537 SkAutoTMalloc<unsigned char> data(size);
538 writer.writeToMemory(static_cast<void*>(data.get()));
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000539
540 // Deserialize picture
commit-bot@chromium.org1e7ee992014-03-14 21:22:22 +0000541 SkValidatingReadBuffer reader(static_cast<void*>(data.get()), size);
commit-bot@chromium.orge5eee512014-03-13 16:18:49 +0000542 SkAutoTUnref<SkPicture> readPict(
543 SkPicture::CreateFromBuffer(reader));
bsalomon49f085d2014-09-05 13:34:00 -0700544 REPORTER_ASSERT(reporter, readPict.get());
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000545 }
caseq26337e92014-06-30 12:14:52 -0700546
547 TestPictureTypefaceSerialization(reporter);
commit-bot@chromium.org02512882013-10-31 18:37:50 +0000548}