blob: 15b73827ae2780470940a565100f8fe6ecab9380 [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) {
45 *flattenable = (T*)reader.readFlattenable(T::GetFlattenableType());
46 }
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) {
commit-bot@chromium.orga2bd2d12014-01-30 22:16:32 +0000142 SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag);
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) {
commit-bot@chromium.orga2bd2d12014-01-30 22:16:32 +0000180 SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag);
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) {
commit-bot@chromium.orga2bd2d12014-01-30 22:16:32 +0000218 SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag);
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),
robertphillips549c8992016-04-01 09:28:51 -0700251 invalidBitmapSource.get(), validBitmapSource.get(), nullptr));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000252
253 SkAutoTUnref<SkImageFilter> deserializedFilter(
254 TestFlattenableSerialization<SkImageFilter>(
reedcfb6bdf2016-03-29 11:32:50 -0700255 xfermodeImageFilter.get(), shouldSucceed, reporter));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000256
257 // Try to render a small bitmap using the invalid deserialized filter
258 // to make sure we don't crash while trying to render it
259 if (shouldSucceed) {
260 SkBitmap bitmap;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000261 bitmap.allocN32Pixels(24, 24);
262 SkCanvas canvas(bitmap);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000263 canvas.clear(0x00000000);
264 SkPaint paint;
265 paint.setImageFilter(deserializedFilter);
266 canvas.clipRect(SkRect::MakeXYWH(0, 0, SkIntToScalar(24), SkIntToScalar(24)));
267 canvas.drawBitmap(bitmap, 0, 0, &paint);
268 }
269}
270
senorblanco0f7197b2014-09-24 11:09:38 -0700271static void TestXfermodeSerialization(skiatest::Reporter* reporter) {
272 for (size_t i = 0; i <= SkXfermode::kLastMode; ++i) {
273 if (i == SkXfermode::kSrcOver_Mode) {
halcanary96fcdcc2015-08-27 07:41:13 -0700274 // skip SrcOver, as it is allowed to return nullptr from Create()
senorblanco0f7197b2014-09-24 11:09:38 -0700275 continue;
276 }
reedcfb6bdf2016-03-29 11:32:50 -0700277 auto mode(SkXfermode::Make(static_cast<SkXfermode::Mode>(i)));
278 REPORTER_ASSERT(reporter, mode);
senorblanco0f7197b2014-09-24 11:09:38 -0700279 SkAutoTUnref<SkXfermode> copy(
280 TestFlattenableSerialization<SkXfermode>(mode.get(), true, reporter));
281 }
282}
283
senorblanco91c395a2014-09-25 15:51:35 -0700284static void TestColorFilterSerialization(skiatest::Reporter* reporter) {
285 uint8_t table[256];
286 for (int i = 0; i < 256; ++i) {
287 table[i] = (i * 41) % 256;
288 }
reedd053ce92016-03-22 10:17:23 -0700289 auto colorFilter(SkTableColorFilter::Make(table));
senorblanco91c395a2014-09-25 15:51:35 -0700290 SkAutoTUnref<SkColorFilter> copy(
291 TestFlattenableSerialization<SkColorFilter>(colorFilter.get(), true, reporter));
292}
293
caseq26337e92014-06-30 12:14:52 -0700294static SkBitmap draw_picture(SkPicture& picture) {
295 SkBitmap bitmap;
halcanary9d524f22016-03-29 09:03:52 -0700296 bitmap.allocN32Pixels(SkScalarCeilToInt(picture.cullRect().width()),
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700297 SkScalarCeilToInt(picture.cullRect().height()));
caseq26337e92014-06-30 12:14:52 -0700298 SkCanvas canvas(bitmap);
robertphillipsc5ba71d2014-09-04 08:42:50 -0700299 picture.playback(&canvas);
caseq26337e92014-06-30 12:14:52 -0700300 return bitmap;
301}
302
303static void compare_bitmaps(skiatest::Reporter* reporter,
304 const SkBitmap& b1, const SkBitmap& b2) {
305 REPORTER_ASSERT(reporter, b1.width() == b2.width());
306 REPORTER_ASSERT(reporter, b1.height() == b2.height());
307 SkAutoLockPixels autoLockPixels1(b1);
308 SkAutoLockPixels autoLockPixels2(b2);
309
310 if ((b1.width() != b2.width()) ||
311 (b1.height() != b2.height())) {
312 return;
313 }
314
315 int pixelErrors = 0;
316 for (int y = 0; y < b2.height(); ++y) {
317 for (int x = 0; x < b2.width(); ++x) {
318 if (b1.getColor(x, y) != b2.getColor(x, y))
319 ++pixelErrors;
320 }
321 }
322 REPORTER_ASSERT(reporter, 0 == pixelErrors);
323}
bungeman41868fe2015-05-20 09:21:04 -0700324static void serialize_and_compare_typeface(SkTypeface* typeface, const char* text,
325 skiatest::Reporter* reporter)
326{
327 // Create a paint with the typeface.
caseq26337e92014-06-30 12:14:52 -0700328 SkPaint paint;
329 paint.setColor(SK_ColorGRAY);
330 paint.setTextSize(SkIntToScalar(30));
bungeman41868fe2015-05-20 09:21:04 -0700331 paint.setTypeface(typeface);
caseq26337e92014-06-30 12:14:52 -0700332
333 // Paint some text.
334 SkPictureRecorder recorder;
335 SkIRect canvasRect = SkIRect::MakeWH(kBitmapSize, kBitmapSize);
halcanary9d524f22016-03-29 09:03:52 -0700336 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(canvasRect.width()),
337 SkIntToScalar(canvasRect.height()),
halcanary96fcdcc2015-08-27 07:41:13 -0700338 nullptr, 0);
caseq26337e92014-06-30 12:14:52 -0700339 canvas->drawColor(SK_ColorWHITE);
bungeman41868fe2015-05-20 09:21:04 -0700340 canvas->drawText(text, 2, 24, 32, paint);
reedca2622b2016-03-18 07:25:55 -0700341 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
caseq26337e92014-06-30 12:14:52 -0700342
343 // Serlialize picture and create its clone from stream.
344 SkDynamicMemoryWStream stream;
345 picture->serialize(&stream);
scroggoa1193e42015-01-21 12:09:53 -0800346 SkAutoTDelete<SkStream> inputStream(stream.detachAsStream());
reedca2622b2016-03-18 07:25:55 -0700347 sk_sp<SkPicture> loadedPicture(SkPicture::MakeFromStream(inputStream.get()));
caseq26337e92014-06-30 12:14:52 -0700348
349 // Draw both original and clone picture and compare bitmaps -- they should be identical.
350 SkBitmap origBitmap = draw_picture(*picture);
351 SkBitmap destBitmap = draw_picture(*loadedPicture);
352 compare_bitmaps(reporter, origBitmap, destBitmap);
353}
354
bungeman41868fe2015-05-20 09:21:04 -0700355static void TestPictureTypefaceSerialization(skiatest::Reporter* reporter) {
356 {
357 // Load typeface from file to test CreateFromFile with index.
358 SkString filename = GetResourcePath("/fonts/test.ttc");
359 SkAutoTUnref<SkTypeface> typeface(SkTypeface::CreateFromFile(filename.c_str(), 1));
360 if (!typeface) {
halcanary7d571242016-02-24 17:59:16 -0800361 INFOF(reporter, "Could not run fontstream test because test.ttc not found.");
bungeman41868fe2015-05-20 09:21:04 -0700362 } else {
363 serialize_and_compare_typeface(typeface, "A!", reporter);
364 }
365 }
366
367 {
368 // Load typeface as stream to create with axis settings.
369 SkAutoTDelete<SkStreamAsset> distortable(GetResourceAsStream("/fonts/Distortable.ttf"));
370 if (!distortable) {
halcanary7d571242016-02-24 17:59:16 -0800371 INFOF(reporter, "Could not run fontstream test because Distortable.ttf not found.");
bungeman41868fe2015-05-20 09:21:04 -0700372 } else {
373 SkFixed axis = SK_FixedSqrt2;
374 SkAutoTUnref<SkTypeface> typeface(SkTypeface::CreateFromFontData(
mtklein18300a32016-03-16 13:53:35 -0700375 new SkFontData(distortable.release(), 0, &axis, 1)));
bungeman41868fe2015-05-20 09:21:04 -0700376 if (!typeface) {
halcanary7d571242016-02-24 17:59:16 -0800377 INFOF(reporter, "Could not run fontstream test because Distortable.ttf not created.");
bungeman41868fe2015-05-20 09:21:04 -0700378 } else {
379 serialize_and_compare_typeface(typeface, "abc", reporter);
380 }
381 }
382 }
383}
384
reed84825042014-09-02 12:50:45 -0700385static void setup_bitmap_for_canvas(SkBitmap* bitmap) {
386 bitmap->allocN32Pixels(kBitmapSize, kBitmapSize);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000387}
388
reed84825042014-09-02 12:50:45 -0700389static void make_checkerboard_bitmap(SkBitmap& bitmap) {
390 setup_bitmap_for_canvas(&bitmap);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000391
392 SkCanvas canvas(bitmap);
393 canvas.clear(0x00000000);
394 SkPaint darkPaint;
395 darkPaint.setColor(0xFF804020);
396 SkPaint lightPaint;
397 lightPaint.setColor(0xFF244484);
398 const int i = kBitmapSize / 8;
399 const SkScalar f = SkIntToScalar(i);
400 for (int y = 0; y < kBitmapSize; y += i) {
401 for (int x = 0; x < kBitmapSize; x += i) {
402 canvas.save();
403 canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
404 canvas.drawRect(SkRect::MakeXYWH(0, 0, f, f), darkPaint);
405 canvas.drawRect(SkRect::MakeXYWH(f, 0, f, f), lightPaint);
406 canvas.drawRect(SkRect::MakeXYWH(0, f, f, f), lightPaint);
407 canvas.drawRect(SkRect::MakeXYWH(f, f, f, f), darkPaint);
408 canvas.restore();
409 }
410 }
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000411}
412
reed84825042014-09-02 12:50:45 -0700413static void draw_something(SkCanvas* canvas) {
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000414 SkPaint paint;
415 SkBitmap bitmap;
reed84825042014-09-02 12:50:45 -0700416 make_checkerboard_bitmap(bitmap);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000417
418 canvas->save();
419 canvas->scale(0.5f, 0.5f);
halcanary96fcdcc2015-08-27 07:41:13 -0700420 canvas->drawBitmap(bitmap, 0, 0, nullptr);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000421 canvas->restore();
422
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000423 paint.setAntiAlias(true);
424
425 paint.setColor(SK_ColorRED);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000426 canvas->drawCircle(SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/3), paint);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000427 paint.setColor(SK_ColorBLACK);
428 paint.setTextSize(SkIntToScalar(kBitmapSize/3));
429 canvas->drawText("Picture", 7, SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/4), paint);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000430}
431
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000432DEF_TEST(Serialization, reporter) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000433 // Test matrix serialization
434 {
435 SkMatrix matrix = SkMatrix::I();
436 TestObjectSerialization(&matrix, reporter);
caseq26337e92014-06-30 12:14:52 -0700437 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000438
439 // Test path serialization
440 {
441 SkPath path;
442 TestObjectSerialization(&path, reporter);
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000443 }
sugoi@google.com305f78e2013-11-04 16:18:15 +0000444
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000445 // Test region serialization
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000446 {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000447 SkRegion region;
448 TestObjectSerialization(&region, reporter);
449 }
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000450
senorblanco0f7197b2014-09-24 11:09:38 -0700451 // Test xfermode serialization
452 {
453 TestXfermodeSerialization(reporter);
454 }
455
senorblanco91c395a2014-09-25 15:51:35 -0700456 // Test color filter serialization
457 {
458 TestColorFilterSerialization(reporter);
459 }
460
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000461 // Test string serialization
462 {
463 SkString string("string");
464 TestObjectSerializationNoAlign<SkString, false>(&string, reporter);
465 TestObjectSerializationNoAlign<SkString, true>(&string, reporter);
466 }
467
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000468 // Test rrect serialization
469 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000470 // SkRRect does not initialize anything.
471 // An uninitialized SkRRect can be serialized,
472 // but will branch on uninitialized data when deserialized.
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000473 SkRRect rrect;
rmistry@google.comd6bab022013-12-02 13:50:38 +0000474 SkRect rect = SkRect::MakeXYWH(1, 2, 20, 30);
475 SkVector corners[4] = { {1, 2}, {2, 3}, {3,4}, {4,5} };
476 rrect.setRectRadii(rect, corners);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000477 TestAlignment(&rrect, reporter);
478 }
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000479
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000480 // Test readByteArray
481 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000482 unsigned char data[kArraySize] = { 1, 2, 3 };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000483 TestArraySerialization(data, reporter);
484 }
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000485
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000486 // Test readColorArray
487 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000488 SkColor data[kArraySize] = { SK_ColorBLACK, SK_ColorWHITE, SK_ColorRED };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000489 TestArraySerialization(data, reporter);
490 }
491
492 // Test readIntArray
493 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000494 int32_t data[kArraySize] = { 1, 2, 4, 8 };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000495 TestArraySerialization(data, reporter);
496 }
497
498 // Test readPointArray
499 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000500 SkPoint data[kArraySize] = { {6, 7}, {42, 128} };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000501 TestArraySerialization(data, reporter);
502 }
503
504 // Test readScalarArray
505 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000506 SkScalar data[kArraySize] = { SK_Scalar1, SK_ScalarHalf, SK_ScalarMax };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000507 TestArraySerialization(data, reporter);
commit-bot@chromium.org02512882013-10-31 18:37:50 +0000508 }
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000509
510 // Test invalid deserializations
511 {
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000512 SkImageInfo info = SkImageInfo::MakeN32Premul(kBitmapSize, kBitmapSize);
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000513
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000514 SkBitmap validBitmap;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000515 validBitmap.setInfo(info);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000516
517 // Create a bitmap with a really large height
518 SkBitmap invalidBitmap;
reede5ea5002014-09-03 11:54:58 -0700519 invalidBitmap.setInfo(info.makeWH(info.width(), 1000000000));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000520
521 // The deserialization should succeed, and the rendering shouldn't crash,
522 // even when the device fails to initialize, due to its size
523 TestBitmapSerialization(validBitmap, invalidBitmap, true, reporter);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000524 }
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000525
526 // Test simple SkPicture serialization
527 {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000528 SkPictureRecorder recorder;
reed84825042014-09-02 12:50:45 -0700529 draw_something(recorder.beginRecording(SkIntToScalar(kBitmapSize),
530 SkIntToScalar(kBitmapSize),
halcanary96fcdcc2015-08-27 07:41:13 -0700531 nullptr, 0));
reedca2622b2016-03-18 07:25:55 -0700532 sk_sp<SkPicture> pict(recorder.finishRecordingAsPicture());
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000533
534 // Serialize picture
535 SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag);
536 pict->flatten(writer);
537 size_t size = writer.bytesWritten();
commit-bot@chromium.org1e7ee992014-03-14 21:22:22 +0000538 SkAutoTMalloc<unsigned char> data(size);
539 writer.writeToMemory(static_cast<void*>(data.get()));
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000540
541 // Deserialize picture
commit-bot@chromium.org1e7ee992014-03-14 21:22:22 +0000542 SkValidatingReadBuffer reader(static_cast<void*>(data.get()), size);
reedca2622b2016-03-18 07:25:55 -0700543 sk_sp<SkPicture> readPict(SkPicture::MakeFromBuffer(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}
reedf70b5312016-03-04 16:36:20 -0800549
550///////////////////////////////////////////////////////////////////////////////////////////////////
551#include "SkAnnotation.h"
552
reedca2622b2016-03-18 07:25:55 -0700553static sk_sp<SkPicture> copy_picture_via_serialization(SkPicture* src) {
reedf70b5312016-03-04 16:36:20 -0800554 SkDynamicMemoryWStream wstream;
555 src->serialize(&wstream);
556 SkAutoTDelete<SkStreamAsset> rstream(wstream.detachAsStream());
reedca2622b2016-03-18 07:25:55 -0700557 return SkPicture::MakeFromStream(rstream);
reedf70b5312016-03-04 16:36:20 -0800558}
559
560struct AnnotationRec {
561 const SkRect fRect;
562 const char* fKey;
563 SkData* fValue;
564};
565
566class TestAnnotationCanvas : public SkCanvas {
567 skiatest::Reporter* fReporter;
568 const AnnotationRec* fRec;
569 int fCount;
570 int fCurrIndex;
571
572public:
573 TestAnnotationCanvas(skiatest::Reporter* reporter, const AnnotationRec rec[], int count)
574 : SkCanvas(100, 100)
575 , fReporter(reporter)
576 , fRec(rec)
577 , fCount(count)
578 , fCurrIndex(0)
579 {}
580
581 ~TestAnnotationCanvas() {
582 REPORTER_ASSERT(fReporter, fCount == fCurrIndex);
583 }
584
585protected:
586 void onDrawAnnotation(const SkRect& rect, const char key[], SkData* value) {
587 REPORTER_ASSERT(fReporter, fCurrIndex < fCount);
588 REPORTER_ASSERT(fReporter, rect == fRec[fCurrIndex].fRect);
589 REPORTER_ASSERT(fReporter, !strcmp(key, fRec[fCurrIndex].fKey));
590 REPORTER_ASSERT(fReporter, value->equals(fRec[fCurrIndex].fValue));
591 fCurrIndex += 1;
592 }
593};
594
595/*
596 * Test the 3 annotation types by recording them into a picture, serializing, and then playing
597 * them back into another canvas.
598 */
599DEF_TEST(Annotations, reporter) {
600 SkPictureRecorder recorder;
601 SkCanvas* recordingCanvas = recorder.beginRecording(SkRect::MakeWH(100, 100));
halcanary9d524f22016-03-29 09:03:52 -0700602
reedf70b5312016-03-04 16:36:20 -0800603 const char* str0 = "rect-with-url";
604 const SkRect r0 = SkRect::MakeWH(10, 10);
605 SkAutoTUnref<SkData> d0(SkData::NewWithCString(str0));
606 SkAnnotateRectWithURL(recordingCanvas, r0, d0);
halcanary9d524f22016-03-29 09:03:52 -0700607
reedf70b5312016-03-04 16:36:20 -0800608 const char* str1 = "named-destination";
609 const SkRect r1 = SkRect::MakeXYWH(5, 5, 0, 0); // collapsed to a point
610 SkAutoTUnref<SkData> d1(SkData::NewWithCString(str1));
611 SkAnnotateNamedDestination(recordingCanvas, {r1.x(), r1.y()}, d1);
halcanary9d524f22016-03-29 09:03:52 -0700612
reedf70b5312016-03-04 16:36:20 -0800613 const char* str2 = "link-to-destination";
614 const SkRect r2 = SkRect::MakeXYWH(20, 20, 5, 6);
615 SkAutoTUnref<SkData> d2(SkData::NewWithCString(str2));
616 SkAnnotateLinkToDestination(recordingCanvas, r2, d2);
617
618 const AnnotationRec recs[] = {
619 { r0, SkAnnotationKeys::URL_Key(), d0 },
620 { r1, SkAnnotationKeys::Define_Named_Dest_Key(), d1 },
621 { r2, SkAnnotationKeys::Link_Named_Dest_Key(), d2 },
622 };
623
reedca2622b2016-03-18 07:25:55 -0700624 sk_sp<SkPicture> pict0(recorder.finishRecordingAsPicture());
625 sk_sp<SkPicture> pict1(copy_picture_via_serialization(pict0.get()));
reedf70b5312016-03-04 16:36:20 -0800626
627 TestAnnotationCanvas canvas(reporter, recs, SK_ARRAY_COUNT(recs));
628 canvas.drawPicture(pict1);
629}