blob: 4750bbe25d436a13de76f62ede826487c719efb4 [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
reedf70b5312016-03-04 16:36:20 -08008#include "SkAnnotationKeys.h"
caseq26337e92014-06-30 12:14:52 -07009#include "Resources.h"
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +000010#include "SkCanvas.h"
bungeman41868fe2015-05-20 09:21:04 -070011#include "SkFixed.h"
12#include "SkFontDescriptor.h"
fmalita5598b632015-09-15 11:26:13 -070013#include "SkImage.h"
14#include "SkImageSource.h"
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +000015#include "SkMallocPixelRef.h"
caseq26337e92014-06-30 12:14:52 -070016#include "SkOSFile.h"
robertphillips@google.com770963f2014-04-18 18:04:41 +000017#include "SkPictureRecorder.h"
senorblanco91c395a2014-09-25 15:51:35 -070018#include "SkTableColorFilter.h"
commit-bot@chromium.org1e7ee992014-03-14 21:22:22 +000019#include "SkTemplates.h"
caseq26337e92014-06-30 12:14:52 -070020#include "SkTypeface.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000021#include "SkWriteBuffer.h"
commit-bot@chromium.org02512882013-10-31 18:37:50 +000022#include "SkValidatingReadBuffer.h"
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +000023#include "SkXfermodeImageFilter.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000024#include "Test.h"
commit-bot@chromium.org02512882013-10-31 18:37:50 +000025
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000026static const uint32_t kArraySize = 64;
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +000027static const int kBitmapSize = 256;
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000028
29template<typename T>
30static void TestAlignment(T* testObj, skiatest::Reporter* reporter) {
31 // Test memory read/write functions directly
32 unsigned char dataWritten[1024];
33 size_t bytesWrittenToMemory = testObj->writeToMemory(dataWritten);
34 REPORTER_ASSERT(reporter, SkAlign4(bytesWrittenToMemory) == bytesWrittenToMemory);
35 size_t bytesReadFromMemory = testObj->readFromMemory(dataWritten, bytesWrittenToMemory);
36 REPORTER_ASSERT(reporter, SkAlign4(bytesReadFromMemory) == bytesReadFromMemory);
37}
38
39template<typename T> struct SerializationUtils {
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +000040 // Generic case for flattenables
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000041 static void Write(SkWriteBuffer& writer, const T* flattenable) {
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +000042 writer.writeFlattenable(flattenable);
43 }
44 static void Read(SkValidatingReadBuffer& reader, T** flattenable) {
mtklein3b375452016-04-04 14:57:19 -070045 *flattenable = (T*)reader.readFlattenable(T::GetFlattenableType());
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +000046 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000047};
48
49template<> struct SerializationUtils<SkMatrix> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000050 static void Write(SkWriteBuffer& writer, const SkMatrix* matrix) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000051 writer.writeMatrix(*matrix);
52 }
53 static void Read(SkValidatingReadBuffer& reader, SkMatrix* matrix) {
54 reader.readMatrix(matrix);
55 }
56};
57
58template<> struct SerializationUtils<SkPath> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000059 static void Write(SkWriteBuffer& writer, const SkPath* path) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000060 writer.writePath(*path);
61 }
62 static void Read(SkValidatingReadBuffer& reader, SkPath* path) {
63 reader.readPath(path);
64 }
65};
66
67template<> struct SerializationUtils<SkRegion> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000068 static void Write(SkWriteBuffer& writer, const SkRegion* region) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000069 writer.writeRegion(*region);
70 }
71 static void Read(SkValidatingReadBuffer& reader, SkRegion* region) {
72 reader.readRegion(region);
73 }
74};
75
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +000076template<> struct SerializationUtils<SkString> {
77 static void Write(SkWriteBuffer& writer, const SkString* string) {
78 writer.writeString(string->c_str());
79 }
80 static void Read(SkValidatingReadBuffer& reader, SkString* string) {
81 reader.readString(string);
82 }
83};
84
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000085template<> struct SerializationUtils<unsigned char> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000086 static void Write(SkWriteBuffer& writer, unsigned char* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +000087 writer.writeByteArray(data, arraySize);
sugoi@google.com305f78e2013-11-04 16:18:15 +000088 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000089 static bool Read(SkValidatingReadBuffer& reader, unsigned char* data, uint32_t arraySize) {
90 return reader.readByteArray(data, arraySize);
91 }
92};
commit-bot@chromium.org02512882013-10-31 18:37:50 +000093
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000094template<> struct SerializationUtils<SkColor> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000095 static void Write(SkWriteBuffer& writer, SkColor* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +000096 writer.writeColorArray(data, arraySize);
sugoi@google.comb48a59a2013-11-04 20:28:23 +000097 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000098 static bool Read(SkValidatingReadBuffer& reader, SkColor* data, uint32_t arraySize) {
99 return reader.readColorArray(data, arraySize);
100 }
101};
sugoi@google.com305f78e2013-11-04 16:18:15 +0000102
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000103template<> struct SerializationUtils<int32_t> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000104 static void Write(SkWriteBuffer& writer, int32_t* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +0000105 writer.writeIntArray(data, arraySize);
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000106 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000107 static bool Read(SkValidatingReadBuffer& reader, int32_t* data, uint32_t arraySize) {
108 return reader.readIntArray(data, arraySize);
109 }
110};
sugoi@google.com305f78e2013-11-04 16:18:15 +0000111
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000112template<> struct SerializationUtils<SkPoint> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000113 static void Write(SkWriteBuffer& writer, SkPoint* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +0000114 writer.writePointArray(data, arraySize);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000115 }
116 static bool Read(SkValidatingReadBuffer& reader, SkPoint* data, uint32_t arraySize) {
117 return reader.readPointArray(data, arraySize);
118 }
119};
reed@google.com12a23862013-11-04 21:35:55 +0000120
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000121template<> struct SerializationUtils<SkScalar> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000122 static void Write(SkWriteBuffer& writer, SkScalar* data, uint32_t arraySize) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000123 writer.writeScalarArray(data, arraySize);
124 }
125 static bool Read(SkValidatingReadBuffer& reader, SkScalar* data, uint32_t arraySize) {
126 return reader.readScalarArray(data, arraySize);
127 }
128};
reed@google.com12a23862013-11-04 21:35:55 +0000129
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000130template<typename T, bool testInvalid> struct SerializationTestUtils {
131 static void InvalidateData(unsigned char* data) {}
132};
133
134template<> struct SerializationTestUtils<SkString, true> {
135 static void InvalidateData(unsigned char* data) {
136 data[3] |= 0x80; // Reverse sign of 1st integer
137 }
138};
139
140template<typename T, bool testInvalid>
141static void TestObjectSerializationNoAlign(T* testObj, skiatest::Reporter* reporter) {
brianosmanfad98562016-05-04 11:06:28 -0700142 SkBinaryWriteBuffer writer;
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000143 SerializationUtils<T>::Write(writer, testObj);
144 size_t bytesWritten = writer.bytesWritten();
145 REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten);
reed@google.com12a23862013-11-04 21:35:55 +0000146
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000147 unsigned char dataWritten[1024];
148 writer.writeToMemory(dataWritten);
149
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000150 SerializationTestUtils<T, testInvalid>::InvalidateData(dataWritten);
151
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000152 // Make sure this fails when it should (test with smaller size, but still multiple of 4)
153 SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 4);
commit-bot@chromium.org8f457e32013-11-08 19:22:57 +0000154 T obj;
155 SerializationUtils<T>::Read(buffer, &obj);
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000156 REPORTER_ASSERT(reporter, !buffer.isValid());
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000157
158 // Make sure this succeeds when it should
159 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten);
commit-bot@chromium.org8f457e32013-11-08 19:22:57 +0000160 const unsigned char* peekBefore = static_cast<const unsigned char*>(buffer2.skip(0));
161 T obj2;
162 SerializationUtils<T>::Read(buffer2, &obj2);
163 const unsigned char* peekAfter = static_cast<const unsigned char*>(buffer2.skip(0));
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000164 // This should have succeeded, since there are enough bytes to read this
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000165 REPORTER_ASSERT(reporter, buffer2.isValid() == !testInvalid);
166 // Note: This following test should always succeed, regardless of whether the buffer is valid,
167 // 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 +0000168 REPORTER_ASSERT(reporter, static_cast<size_t>(peekAfter - peekBefore) == bytesWritten);
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000169}
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000170
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000171template<typename T>
172static void TestObjectSerialization(T* testObj, skiatest::Reporter* reporter) {
173 TestObjectSerializationNoAlign<T, false>(testObj, reporter);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000174 TestAlignment(testObj, reporter);
175}
176
177template<typename T>
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000178static T* TestFlattenableSerialization(T* testObj, bool shouldSucceed,
179 skiatest::Reporter* reporter) {
brianosmanfad98562016-05-04 11:06:28 -0700180 SkBinaryWriteBuffer writer;
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000181 SerializationUtils<T>::Write(writer, testObj);
182 size_t bytesWritten = writer.bytesWritten();
183 REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten);
184
senorblanco91c395a2014-09-25 15:51:35 -0700185 unsigned char dataWritten[4096];
reed@google.combf790232013-12-13 19:45:58 +0000186 SkASSERT(bytesWritten <= sizeof(dataWritten));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000187 writer.writeToMemory(dataWritten);
188
189 // Make sure this fails when it should (test with smaller size, but still multiple of 4)
190 SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 4);
halcanary96fcdcc2015-08-27 07:41:13 -0700191 T* obj = nullptr;
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000192 SerializationUtils<T>::Read(buffer, &obj);
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000193 REPORTER_ASSERT(reporter, !buffer.isValid());
halcanary96fcdcc2015-08-27 07:41:13 -0700194 REPORTER_ASSERT(reporter, nullptr == obj);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000195
196 // Make sure this succeeds when it should
197 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten);
198 const unsigned char* peekBefore = static_cast<const unsigned char*>(buffer2.skip(0));
halcanary96fcdcc2015-08-27 07:41:13 -0700199 T* obj2 = nullptr;
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000200 SerializationUtils<T>::Read(buffer2, &obj2);
201 const unsigned char* peekAfter = static_cast<const unsigned char*>(buffer2.skip(0));
202 if (shouldSucceed) {
203 // This should have succeeded, since there are enough bytes to read this
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000204 REPORTER_ASSERT(reporter, buffer2.isValid());
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000205 REPORTER_ASSERT(reporter, static_cast<size_t>(peekAfter - peekBefore) == bytesWritten);
bsalomon49f085d2014-09-05 13:34:00 -0700206 REPORTER_ASSERT(reporter, obj2);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000207 } else {
208 // If the deserialization was supposed to fail, make sure it did
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000209 REPORTER_ASSERT(reporter, !buffer.isValid());
halcanary96fcdcc2015-08-27 07:41:13 -0700210 REPORTER_ASSERT(reporter, nullptr == obj2);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000211 }
212
213 return obj2; // Return object to perform further validity tests on it
214}
215
216template<typename T>
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000217static void TestArraySerialization(T* data, skiatest::Reporter* reporter) {
brianosmanfad98562016-05-04 11:06:28 -0700218 SkBinaryWriteBuffer writer;
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000219 SerializationUtils<T>::Write(writer, data, kArraySize);
220 size_t bytesWritten = writer.bytesWritten();
221 // This should write the length (in 4 bytes) and the array
222 REPORTER_ASSERT(reporter, (4 + kArraySize * sizeof(T)) == bytesWritten);
223
224 unsigned char dataWritten[1024];
225 writer.writeToMemory(dataWritten);
226
227 // Make sure this fails when it should
228 SkValidatingReadBuffer buffer(dataWritten, bytesWritten);
229 T dataRead[kArraySize];
230 bool success = SerializationUtils<T>::Read(buffer, dataRead, kArraySize / 2);
231 // This should have failed, since the provided size was too small
232 REPORTER_ASSERT(reporter, !success);
233
234 // Make sure this succeeds when it should
235 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten);
236 success = SerializationUtils<T>::Read(buffer2, dataRead, kArraySize);
237 // This should have succeeded, since there are enough bytes to read this
238 REPORTER_ASSERT(reporter, success);
239}
240
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000241static void TestBitmapSerialization(const SkBitmap& validBitmap,
242 const SkBitmap& invalidBitmap,
243 bool shouldSucceed,
244 skiatest::Reporter* reporter) {
reed9ce9d672016-03-17 10:51:11 -0700245 sk_sp<SkImage> validImage(SkImage::MakeFromBitmap(validBitmap));
robertphillips549c8992016-04-01 09:28:51 -0700246 sk_sp<SkImageFilter> validBitmapSource(SkImageSource::Make(std::move(validImage)));
reed9ce9d672016-03-17 10:51:11 -0700247 sk_sp<SkImage> invalidImage(SkImage::MakeFromBitmap(invalidBitmap));
robertphillips549c8992016-04-01 09:28:51 -0700248 sk_sp<SkImageFilter> invalidBitmapSource(SkImageSource::Make(std::move(invalidImage)));
reedcfb6bdf2016-03-29 11:32:50 -0700249 sk_sp<SkImageFilter> xfermodeImageFilter(
250 SkXfermodeImageFilter::Make(SkXfermode::Make(SkXfermode::kSrcOver_Mode),
robertphillips8c0326d2016-04-05 12:48:34 -0700251 std::move(invalidBitmapSource),
252 std::move(validBitmapSource), nullptr));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000253
254 SkAutoTUnref<SkImageFilter> deserializedFilter(
255 TestFlattenableSerialization<SkImageFilter>(
reedcfb6bdf2016-03-29 11:32:50 -0700256 xfermodeImageFilter.get(), shouldSucceed, reporter));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000257
258 // Try to render a small bitmap using the invalid deserialized filter
259 // to make sure we don't crash while trying to render it
260 if (shouldSucceed) {
261 SkBitmap bitmap;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000262 bitmap.allocN32Pixels(24, 24);
263 SkCanvas canvas(bitmap);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000264 canvas.clear(0x00000000);
265 SkPaint paint;
266 paint.setImageFilter(deserializedFilter);
267 canvas.clipRect(SkRect::MakeXYWH(0, 0, SkIntToScalar(24), SkIntToScalar(24)));
268 canvas.drawBitmap(bitmap, 0, 0, &paint);
269 }
270}
271
senorblanco0f7197b2014-09-24 11:09:38 -0700272static void TestXfermodeSerialization(skiatest::Reporter* reporter) {
273 for (size_t i = 0; i <= SkXfermode::kLastMode; ++i) {
274 if (i == SkXfermode::kSrcOver_Mode) {
halcanary96fcdcc2015-08-27 07:41:13 -0700275 // skip SrcOver, as it is allowed to return nullptr from Create()
senorblanco0f7197b2014-09-24 11:09:38 -0700276 continue;
277 }
reedcfb6bdf2016-03-29 11:32:50 -0700278 auto mode(SkXfermode::Make(static_cast<SkXfermode::Mode>(i)));
279 REPORTER_ASSERT(reporter, mode);
senorblanco0f7197b2014-09-24 11:09:38 -0700280 SkAutoTUnref<SkXfermode> copy(
281 TestFlattenableSerialization<SkXfermode>(mode.get(), true, reporter));
282 }
283}
284
senorblanco91c395a2014-09-25 15:51:35 -0700285static void TestColorFilterSerialization(skiatest::Reporter* reporter) {
286 uint8_t table[256];
287 for (int i = 0; i < 256; ++i) {
288 table[i] = (i * 41) % 256;
289 }
reedd053ce92016-03-22 10:17:23 -0700290 auto colorFilter(SkTableColorFilter::Make(table));
senorblanco91c395a2014-09-25 15:51:35 -0700291 SkAutoTUnref<SkColorFilter> copy(
292 TestFlattenableSerialization<SkColorFilter>(colorFilter.get(), true, reporter));
293}
294
caseq26337e92014-06-30 12:14:52 -0700295static SkBitmap draw_picture(SkPicture& picture) {
296 SkBitmap bitmap;
halcanary9d524f22016-03-29 09:03:52 -0700297 bitmap.allocN32Pixels(SkScalarCeilToInt(picture.cullRect().width()),
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700298 SkScalarCeilToInt(picture.cullRect().height()));
caseq26337e92014-06-30 12:14:52 -0700299 SkCanvas canvas(bitmap);
robertphillipsc5ba71d2014-09-04 08:42:50 -0700300 picture.playback(&canvas);
caseq26337e92014-06-30 12:14:52 -0700301 return bitmap;
302}
303
304static void compare_bitmaps(skiatest::Reporter* reporter,
305 const SkBitmap& b1, const SkBitmap& b2) {
306 REPORTER_ASSERT(reporter, b1.width() == b2.width());
307 REPORTER_ASSERT(reporter, b1.height() == b2.height());
308 SkAutoLockPixels autoLockPixels1(b1);
309 SkAutoLockPixels autoLockPixels2(b2);
310
311 if ((b1.width() != b2.width()) ||
312 (b1.height() != b2.height())) {
313 return;
314 }
315
316 int pixelErrors = 0;
317 for (int y = 0; y < b2.height(); ++y) {
318 for (int x = 0; x < b2.width(); ++x) {
319 if (b1.getColor(x, y) != b2.getColor(x, y))
320 ++pixelErrors;
321 }
322 }
323 REPORTER_ASSERT(reporter, 0 == pixelErrors);
324}
bungeman41868fe2015-05-20 09:21:04 -0700325static void serialize_and_compare_typeface(SkTypeface* typeface, const char* text,
326 skiatest::Reporter* reporter)
327{
328 // Create a paint with the typeface.
caseq26337e92014-06-30 12:14:52 -0700329 SkPaint paint;
330 paint.setColor(SK_ColorGRAY);
331 paint.setTextSize(SkIntToScalar(30));
bungeman41868fe2015-05-20 09:21:04 -0700332 paint.setTypeface(typeface);
caseq26337e92014-06-30 12:14:52 -0700333
334 // Paint some text.
335 SkPictureRecorder recorder;
336 SkIRect canvasRect = SkIRect::MakeWH(kBitmapSize, kBitmapSize);
halcanary9d524f22016-03-29 09:03:52 -0700337 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(canvasRect.width()),
338 SkIntToScalar(canvasRect.height()),
halcanary96fcdcc2015-08-27 07:41:13 -0700339 nullptr, 0);
caseq26337e92014-06-30 12:14:52 -0700340 canvas->drawColor(SK_ColorWHITE);
bungeman41868fe2015-05-20 09:21:04 -0700341 canvas->drawText(text, 2, 24, 32, paint);
reedca2622b2016-03-18 07:25:55 -0700342 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
caseq26337e92014-06-30 12:14:52 -0700343
344 // Serlialize picture and create its clone from stream.
345 SkDynamicMemoryWStream stream;
346 picture->serialize(&stream);
scroggoa1193e42015-01-21 12:09:53 -0800347 SkAutoTDelete<SkStream> inputStream(stream.detachAsStream());
reedca2622b2016-03-18 07:25:55 -0700348 sk_sp<SkPicture> loadedPicture(SkPicture::MakeFromStream(inputStream.get()));
caseq26337e92014-06-30 12:14:52 -0700349
350 // Draw both original and clone picture and compare bitmaps -- they should be identical.
351 SkBitmap origBitmap = draw_picture(*picture);
352 SkBitmap destBitmap = draw_picture(*loadedPicture);
353 compare_bitmaps(reporter, origBitmap, destBitmap);
354}
355
bungeman41868fe2015-05-20 09:21:04 -0700356static void TestPictureTypefaceSerialization(skiatest::Reporter* reporter) {
357 {
358 // Load typeface from file to test CreateFromFile with index.
359 SkString filename = GetResourcePath("/fonts/test.ttc");
360 SkAutoTUnref<SkTypeface> typeface(SkTypeface::CreateFromFile(filename.c_str(), 1));
361 if (!typeface) {
halcanary7d571242016-02-24 17:59:16 -0800362 INFOF(reporter, "Could not run fontstream test because test.ttc not found.");
bungeman41868fe2015-05-20 09:21:04 -0700363 } else {
364 serialize_and_compare_typeface(typeface, "A!", reporter);
365 }
366 }
367
368 {
369 // Load typeface as stream to create with axis settings.
370 SkAutoTDelete<SkStreamAsset> distortable(GetResourceAsStream("/fonts/Distortable.ttf"));
371 if (!distortable) {
halcanary7d571242016-02-24 17:59:16 -0800372 INFOF(reporter, "Could not run fontstream test because Distortable.ttf not found.");
bungeman41868fe2015-05-20 09:21:04 -0700373 } else {
374 SkFixed axis = SK_FixedSqrt2;
375 SkAutoTUnref<SkTypeface> typeface(SkTypeface::CreateFromFontData(
mtklein18300a32016-03-16 13:53:35 -0700376 new SkFontData(distortable.release(), 0, &axis, 1)));
bungeman41868fe2015-05-20 09:21:04 -0700377 if (!typeface) {
halcanary7d571242016-02-24 17:59:16 -0800378 INFOF(reporter, "Could not run fontstream test because Distortable.ttf not created.");
bungeman41868fe2015-05-20 09:21:04 -0700379 } else {
380 serialize_and_compare_typeface(typeface, "abc", reporter);
381 }
382 }
383 }
384}
385
reed84825042014-09-02 12:50:45 -0700386static void setup_bitmap_for_canvas(SkBitmap* bitmap) {
387 bitmap->allocN32Pixels(kBitmapSize, kBitmapSize);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000388}
389
reed84825042014-09-02 12:50:45 -0700390static void make_checkerboard_bitmap(SkBitmap& bitmap) {
391 setup_bitmap_for_canvas(&bitmap);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000392
393 SkCanvas canvas(bitmap);
394 canvas.clear(0x00000000);
395 SkPaint darkPaint;
396 darkPaint.setColor(0xFF804020);
397 SkPaint lightPaint;
398 lightPaint.setColor(0xFF244484);
399 const int i = kBitmapSize / 8;
400 const SkScalar f = SkIntToScalar(i);
401 for (int y = 0; y < kBitmapSize; y += i) {
402 for (int x = 0; x < kBitmapSize; x += i) {
403 canvas.save();
404 canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
405 canvas.drawRect(SkRect::MakeXYWH(0, 0, f, f), darkPaint);
406 canvas.drawRect(SkRect::MakeXYWH(f, 0, f, f), lightPaint);
407 canvas.drawRect(SkRect::MakeXYWH(0, f, f, f), lightPaint);
408 canvas.drawRect(SkRect::MakeXYWH(f, f, f, f), darkPaint);
409 canvas.restore();
410 }
411 }
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000412}
413
reed84825042014-09-02 12:50:45 -0700414static void draw_something(SkCanvas* canvas) {
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000415 SkPaint paint;
416 SkBitmap bitmap;
reed84825042014-09-02 12:50:45 -0700417 make_checkerboard_bitmap(bitmap);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000418
419 canvas->save();
420 canvas->scale(0.5f, 0.5f);
halcanary96fcdcc2015-08-27 07:41:13 -0700421 canvas->drawBitmap(bitmap, 0, 0, nullptr);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000422 canvas->restore();
423
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000424 paint.setAntiAlias(true);
425
426 paint.setColor(SK_ColorRED);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000427 canvas->drawCircle(SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/3), paint);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000428 paint.setColor(SK_ColorBLACK);
429 paint.setTextSize(SkIntToScalar(kBitmapSize/3));
430 canvas->drawText("Picture", 7, SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/4), paint);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000431}
432
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000433DEF_TEST(Serialization, reporter) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000434 // Test matrix serialization
435 {
436 SkMatrix matrix = SkMatrix::I();
437 TestObjectSerialization(&matrix, reporter);
caseq26337e92014-06-30 12:14:52 -0700438 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000439
440 // Test path serialization
441 {
442 SkPath path;
443 TestObjectSerialization(&path, reporter);
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000444 }
sugoi@google.com305f78e2013-11-04 16:18:15 +0000445
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000446 // Test region serialization
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000447 {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000448 SkRegion region;
449 TestObjectSerialization(&region, reporter);
450 }
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000451
senorblanco0f7197b2014-09-24 11:09:38 -0700452 // Test xfermode serialization
453 {
454 TestXfermodeSerialization(reporter);
455 }
456
senorblanco91c395a2014-09-25 15:51:35 -0700457 // Test color filter serialization
458 {
459 TestColorFilterSerialization(reporter);
460 }
461
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000462 // Test string serialization
463 {
464 SkString string("string");
465 TestObjectSerializationNoAlign<SkString, false>(&string, reporter);
466 TestObjectSerializationNoAlign<SkString, true>(&string, reporter);
467 }
468
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000469 // Test rrect serialization
470 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000471 // SkRRect does not initialize anything.
472 // An uninitialized SkRRect can be serialized,
473 // but will branch on uninitialized data when deserialized.
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000474 SkRRect rrect;
rmistry@google.comd6bab022013-12-02 13:50:38 +0000475 SkRect rect = SkRect::MakeXYWH(1, 2, 20, 30);
476 SkVector corners[4] = { {1, 2}, {2, 3}, {3,4}, {4,5} };
477 rrect.setRectRadii(rect, corners);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000478 TestAlignment(&rrect, reporter);
479 }
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000480
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000481 // Test readByteArray
482 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000483 unsigned char data[kArraySize] = { 1, 2, 3 };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000484 TestArraySerialization(data, reporter);
485 }
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000486
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000487 // Test readColorArray
488 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000489 SkColor data[kArraySize] = { SK_ColorBLACK, SK_ColorWHITE, SK_ColorRED };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000490 TestArraySerialization(data, reporter);
491 }
492
493 // Test readIntArray
494 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000495 int32_t data[kArraySize] = { 1, 2, 4, 8 };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000496 TestArraySerialization(data, reporter);
497 }
498
499 // Test readPointArray
500 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000501 SkPoint data[kArraySize] = { {6, 7}, {42, 128} };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000502 TestArraySerialization(data, reporter);
503 }
504
505 // Test readScalarArray
506 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000507 SkScalar data[kArraySize] = { SK_Scalar1, SK_ScalarHalf, SK_ScalarMax };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000508 TestArraySerialization(data, reporter);
commit-bot@chromium.org02512882013-10-31 18:37:50 +0000509 }
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000510
511 // Test invalid deserializations
512 {
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000513 SkImageInfo info = SkImageInfo::MakeN32Premul(kBitmapSize, kBitmapSize);
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000514
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000515 SkBitmap validBitmap;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000516 validBitmap.setInfo(info);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000517
518 // Create a bitmap with a really large height
519 SkBitmap invalidBitmap;
reede5ea5002014-09-03 11:54:58 -0700520 invalidBitmap.setInfo(info.makeWH(info.width(), 1000000000));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000521
522 // The deserialization should succeed, and the rendering shouldn't crash,
523 // even when the device fails to initialize, due to its size
524 TestBitmapSerialization(validBitmap, invalidBitmap, true, reporter);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000525 }
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000526
527 // Test simple SkPicture serialization
528 {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000529 SkPictureRecorder recorder;
reed84825042014-09-02 12:50:45 -0700530 draw_something(recorder.beginRecording(SkIntToScalar(kBitmapSize),
531 SkIntToScalar(kBitmapSize),
halcanary96fcdcc2015-08-27 07:41:13 -0700532 nullptr, 0));
reedca2622b2016-03-18 07:25:55 -0700533 sk_sp<SkPicture> pict(recorder.finishRecordingAsPicture());
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000534
535 // Serialize picture
brianosmanfad98562016-05-04 11:06:28 -0700536 SkBinaryWriteBuffer writer;
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000537 pict->flatten(writer);
538 size_t size = writer.bytesWritten();
commit-bot@chromium.org1e7ee992014-03-14 21:22:22 +0000539 SkAutoTMalloc<unsigned char> data(size);
540 writer.writeToMemory(static_cast<void*>(data.get()));
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000541
542 // Deserialize picture
commit-bot@chromium.org1e7ee992014-03-14 21:22:22 +0000543 SkValidatingReadBuffer reader(static_cast<void*>(data.get()), size);
reedca2622b2016-03-18 07:25:55 -0700544 sk_sp<SkPicture> readPict(SkPicture::MakeFromBuffer(reader));
bsalomon49f085d2014-09-05 13:34:00 -0700545 REPORTER_ASSERT(reporter, readPict.get());
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000546 }
caseq26337e92014-06-30 12:14:52 -0700547
548 TestPictureTypefaceSerialization(reporter);
commit-bot@chromium.org02512882013-10-31 18:37:50 +0000549}
reedf70b5312016-03-04 16:36:20 -0800550
551///////////////////////////////////////////////////////////////////////////////////////////////////
552#include "SkAnnotation.h"
553
reedca2622b2016-03-18 07:25:55 -0700554static sk_sp<SkPicture> copy_picture_via_serialization(SkPicture* src) {
reedf70b5312016-03-04 16:36:20 -0800555 SkDynamicMemoryWStream wstream;
556 src->serialize(&wstream);
557 SkAutoTDelete<SkStreamAsset> rstream(wstream.detachAsStream());
reedca2622b2016-03-18 07:25:55 -0700558 return SkPicture::MakeFromStream(rstream);
reedf70b5312016-03-04 16:36:20 -0800559}
560
561struct AnnotationRec {
562 const SkRect fRect;
563 const char* fKey;
564 SkData* fValue;
565};
566
567class TestAnnotationCanvas : public SkCanvas {
568 skiatest::Reporter* fReporter;
569 const AnnotationRec* fRec;
570 int fCount;
571 int fCurrIndex;
572
573public:
574 TestAnnotationCanvas(skiatest::Reporter* reporter, const AnnotationRec rec[], int count)
575 : SkCanvas(100, 100)
576 , fReporter(reporter)
577 , fRec(rec)
578 , fCount(count)
579 , fCurrIndex(0)
580 {}
581
582 ~TestAnnotationCanvas() {
583 REPORTER_ASSERT(fReporter, fCount == fCurrIndex);
584 }
585
586protected:
587 void onDrawAnnotation(const SkRect& rect, const char key[], SkData* value) {
588 REPORTER_ASSERT(fReporter, fCurrIndex < fCount);
589 REPORTER_ASSERT(fReporter, rect == fRec[fCurrIndex].fRect);
590 REPORTER_ASSERT(fReporter, !strcmp(key, fRec[fCurrIndex].fKey));
591 REPORTER_ASSERT(fReporter, value->equals(fRec[fCurrIndex].fValue));
592 fCurrIndex += 1;
593 }
594};
595
596/*
597 * Test the 3 annotation types by recording them into a picture, serializing, and then playing
598 * them back into another canvas.
599 */
600DEF_TEST(Annotations, reporter) {
601 SkPictureRecorder recorder;
602 SkCanvas* recordingCanvas = recorder.beginRecording(SkRect::MakeWH(100, 100));
halcanary9d524f22016-03-29 09:03:52 -0700603
reedf70b5312016-03-04 16:36:20 -0800604 const char* str0 = "rect-with-url";
605 const SkRect r0 = SkRect::MakeWH(10, 10);
606 SkAutoTUnref<SkData> d0(SkData::NewWithCString(str0));
607 SkAnnotateRectWithURL(recordingCanvas, r0, d0);
halcanary9d524f22016-03-29 09:03:52 -0700608
reedf70b5312016-03-04 16:36:20 -0800609 const char* str1 = "named-destination";
610 const SkRect r1 = SkRect::MakeXYWH(5, 5, 0, 0); // collapsed to a point
611 SkAutoTUnref<SkData> d1(SkData::NewWithCString(str1));
612 SkAnnotateNamedDestination(recordingCanvas, {r1.x(), r1.y()}, d1);
halcanary9d524f22016-03-29 09:03:52 -0700613
reedf70b5312016-03-04 16:36:20 -0800614 const char* str2 = "link-to-destination";
615 const SkRect r2 = SkRect::MakeXYWH(20, 20, 5, 6);
616 SkAutoTUnref<SkData> d2(SkData::NewWithCString(str2));
617 SkAnnotateLinkToDestination(recordingCanvas, r2, d2);
618
619 const AnnotationRec recs[] = {
620 { r0, SkAnnotationKeys::URL_Key(), d0 },
621 { r1, SkAnnotationKeys::Define_Named_Dest_Key(), d1 },
622 { r2, SkAnnotationKeys::Link_Named_Dest_Key(), d2 },
623 };
624
reedca2622b2016-03-18 07:25:55 -0700625 sk_sp<SkPicture> pict0(recorder.finishRecordingAsPicture());
626 sk_sp<SkPicture> pict1(copy_picture_via_serialization(pict0.get()));
reedf70b5312016-03-04 16:36:20 -0800627
628 TestAnnotationCanvas canvas(reporter, recs, SK_ARRAY_COUNT(recs));
629 canvas.drawPicture(pict1);
630}