blob: 3c24c558efd5c9c3ab1182b28ff733d200c946ad [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
egdaniel4132de72016-06-15 14:28:17 -07008#include "Resources.h"
dvonbeck8811e402016-06-16 12:39:25 -07009#include "SkAnnotationKeys.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"
dvonbeck8811e402016-06-16 12:39:25 -070015#include "SkLightingShader.h"
bungemanf93d7112016-09-16 06:24:20 -070016#include "SkMakeUnique.h"
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +000017#include "SkMallocPixelRef.h"
dvonbeck5b794fa2016-07-06 13:58:36 -070018#include "SkNormalSource.h"
caseq26337e92014-06-30 12:14:52 -070019#include "SkOSFile.h"
robertphillips@google.com770963f2014-04-18 18:04:41 +000020#include "SkPictureRecorder.h"
Florin Malita4aed1382017-05-25 10:38:07 -040021#include "SkShaderBase.h"
senorblanco91c395a2014-09-25 15:51:35 -070022#include "SkTableColorFilter.h"
commit-bot@chromium.org1e7ee992014-03-14 21:22:22 +000023#include "SkTemplates.h"
caseq26337e92014-06-30 12:14:52 -070024#include "SkTypeface.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000025#include "SkWriteBuffer.h"
commit-bot@chromium.org02512882013-10-31 18:37:50 +000026#include "SkValidatingReadBuffer.h"
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +000027#include "SkXfermodeImageFilter.h"
dvonbeck8811e402016-06-16 12:39:25 -070028#include "sk_tool_utils.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000029#include "Test.h"
commit-bot@chromium.org02512882013-10-31 18:37:50 +000030
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000031static const uint32_t kArraySize = 64;
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +000032static const int kBitmapSize = 256;
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000033
34template<typename T>
35static void TestAlignment(T* testObj, skiatest::Reporter* reporter) {
36 // Test memory read/write functions directly
37 unsigned char dataWritten[1024];
38 size_t bytesWrittenToMemory = testObj->writeToMemory(dataWritten);
39 REPORTER_ASSERT(reporter, SkAlign4(bytesWrittenToMemory) == bytesWrittenToMemory);
40 size_t bytesReadFromMemory = testObj->readFromMemory(dataWritten, bytesWrittenToMemory);
41 REPORTER_ASSERT(reporter, SkAlign4(bytesReadFromMemory) == bytesReadFromMemory);
42}
43
44template<typename T> struct SerializationUtils {
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +000045 // Generic case for flattenables
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000046 static void Write(SkWriteBuffer& writer, const T* flattenable) {
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +000047 writer.writeFlattenable(flattenable);
48 }
49 static void Read(SkValidatingReadBuffer& reader, T** flattenable) {
mtklein3b375452016-04-04 14:57:19 -070050 *flattenable = (T*)reader.readFlattenable(T::GetFlattenableType());
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +000051 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000052};
53
54template<> struct SerializationUtils<SkMatrix> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000055 static void Write(SkWriteBuffer& writer, const SkMatrix* matrix) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000056 writer.writeMatrix(*matrix);
57 }
58 static void Read(SkValidatingReadBuffer& reader, SkMatrix* matrix) {
59 reader.readMatrix(matrix);
60 }
61};
62
63template<> struct SerializationUtils<SkPath> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000064 static void Write(SkWriteBuffer& writer, const SkPath* path) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000065 writer.writePath(*path);
66 }
67 static void Read(SkValidatingReadBuffer& reader, SkPath* path) {
68 reader.readPath(path);
69 }
70};
71
72template<> struct SerializationUtils<SkRegion> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000073 static void Write(SkWriteBuffer& writer, const SkRegion* region) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000074 writer.writeRegion(*region);
75 }
76 static void Read(SkValidatingReadBuffer& reader, SkRegion* region) {
77 reader.readRegion(region);
78 }
79};
80
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +000081template<> struct SerializationUtils<SkString> {
82 static void Write(SkWriteBuffer& writer, const SkString* string) {
83 writer.writeString(string->c_str());
84 }
85 static void Read(SkValidatingReadBuffer& reader, SkString* string) {
86 reader.readString(string);
87 }
88};
89
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000090template<> struct SerializationUtils<unsigned char> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000091 static void Write(SkWriteBuffer& writer, unsigned char* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +000092 writer.writeByteArray(data, arraySize);
sugoi@google.com305f78e2013-11-04 16:18:15 +000093 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000094 static bool Read(SkValidatingReadBuffer& reader, unsigned char* data, uint32_t arraySize) {
95 return reader.readByteArray(data, arraySize);
96 }
97};
commit-bot@chromium.org02512882013-10-31 18:37:50 +000098
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000099template<> struct SerializationUtils<SkColor> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000100 static void Write(SkWriteBuffer& writer, SkColor* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +0000101 writer.writeColorArray(data, arraySize);
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000102 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000103 static bool Read(SkValidatingReadBuffer& reader, SkColor* data, uint32_t arraySize) {
104 return reader.readColorArray(data, arraySize);
105 }
106};
sugoi@google.com305f78e2013-11-04 16:18:15 +0000107
brianosman97bbf822016-09-25 13:15:58 -0700108template<> struct SerializationUtils<SkColor4f> {
109 static void Write(SkWriteBuffer& writer, SkColor4f* data, uint32_t arraySize) {
110 writer.writeColor4fArray(data, arraySize);
111 }
112 static bool Read(SkValidatingReadBuffer& reader, SkColor4f* data, uint32_t arraySize) {
113 return reader.readColor4fArray(data, arraySize);
114 }
115};
116
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000117template<> struct SerializationUtils<int32_t> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000118 static void Write(SkWriteBuffer& writer, int32_t* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +0000119 writer.writeIntArray(data, arraySize);
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000120 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000121 static bool Read(SkValidatingReadBuffer& reader, int32_t* data, uint32_t arraySize) {
122 return reader.readIntArray(data, arraySize);
123 }
124};
sugoi@google.com305f78e2013-11-04 16:18:15 +0000125
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000126template<> struct SerializationUtils<SkPoint> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000127 static void Write(SkWriteBuffer& writer, SkPoint* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +0000128 writer.writePointArray(data, arraySize);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000129 }
130 static bool Read(SkValidatingReadBuffer& reader, SkPoint* data, uint32_t arraySize) {
131 return reader.readPointArray(data, arraySize);
132 }
133};
reed@google.com12a23862013-11-04 21:35:55 +0000134
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000135template<> struct SerializationUtils<SkScalar> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000136 static void Write(SkWriteBuffer& writer, SkScalar* data, uint32_t arraySize) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000137 writer.writeScalarArray(data, arraySize);
138 }
139 static bool Read(SkValidatingReadBuffer& reader, SkScalar* data, uint32_t arraySize) {
140 return reader.readScalarArray(data, arraySize);
141 }
142};
reed@google.com12a23862013-11-04 21:35:55 +0000143
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000144template<typename T, bool testInvalid> struct SerializationTestUtils {
145 static void InvalidateData(unsigned char* data) {}
146};
147
148template<> struct SerializationTestUtils<SkString, true> {
149 static void InvalidateData(unsigned char* data) {
150 data[3] |= 0x80; // Reverse sign of 1st integer
151 }
152};
153
154template<typename T, bool testInvalid>
155static void TestObjectSerializationNoAlign(T* testObj, skiatest::Reporter* reporter) {
brianosmanfad98562016-05-04 11:06:28 -0700156 SkBinaryWriteBuffer writer;
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000157 SerializationUtils<T>::Write(writer, testObj);
158 size_t bytesWritten = writer.bytesWritten();
159 REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten);
reed@google.com12a23862013-11-04 21:35:55 +0000160
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000161 unsigned char dataWritten[1024];
162 writer.writeToMemory(dataWritten);
163
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000164 SerializationTestUtils<T, testInvalid>::InvalidateData(dataWritten);
165
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000166 // Make sure this fails when it should (test with smaller size, but still multiple of 4)
167 SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 4);
commit-bot@chromium.org8f457e32013-11-08 19:22:57 +0000168 T obj;
169 SerializationUtils<T>::Read(buffer, &obj);
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000170 REPORTER_ASSERT(reporter, !buffer.isValid());
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000171
172 // Make sure this succeeds when it should
173 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten);
Robert Phillipsb2526042016-09-26 09:00:36 -0400174 size_t offsetBefore = buffer2.offset();
commit-bot@chromium.org8f457e32013-11-08 19:22:57 +0000175 T obj2;
176 SerializationUtils<T>::Read(buffer2, &obj2);
Robert Phillipsb2526042016-09-26 09:00:36 -0400177 size_t offsetAfter = buffer2.offset();
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000178 // This should have succeeded, since there are enough bytes to read this
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000179 REPORTER_ASSERT(reporter, buffer2.isValid() == !testInvalid);
180 // Note: This following test should always succeed, regardless of whether the buffer is valid,
181 // since if it is invalid, it will simply skip to the end, as if it had read the whole buffer.
Robert Phillipsb2526042016-09-26 09:00:36 -0400182 REPORTER_ASSERT(reporter, offsetAfter - offsetBefore == bytesWritten);
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000183}
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000184
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000185template<typename T>
186static void TestObjectSerialization(T* testObj, skiatest::Reporter* reporter) {
187 TestObjectSerializationNoAlign<T, false>(testObj, reporter);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000188 TestAlignment(testObj, reporter);
189}
190
191template<typename T>
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000192static T* TestFlattenableSerialization(T* testObj, bool shouldSucceed,
193 skiatest::Reporter* reporter) {
brianosmanfad98562016-05-04 11:06:28 -0700194 SkBinaryWriteBuffer writer;
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000195 SerializationUtils<T>::Write(writer, testObj);
196 size_t bytesWritten = writer.bytesWritten();
197 REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten);
198
dvonbeck8811e402016-06-16 12:39:25 -0700199 SkASSERT(bytesWritten <= 4096);
senorblanco91c395a2014-09-25 15:51:35 -0700200 unsigned char dataWritten[4096];
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000201 writer.writeToMemory(dataWritten);
202
203 // Make sure this fails when it should (test with smaller size, but still multiple of 4)
204 SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 4);
halcanary96fcdcc2015-08-27 07:41:13 -0700205 T* obj = nullptr;
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000206 SerializationUtils<T>::Read(buffer, &obj);
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000207 REPORTER_ASSERT(reporter, !buffer.isValid());
halcanary96fcdcc2015-08-27 07:41:13 -0700208 REPORTER_ASSERT(reporter, nullptr == obj);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000209
210 // Make sure this succeeds when it should
211 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten);
212 const unsigned char* peekBefore = static_cast<const unsigned char*>(buffer2.skip(0));
halcanary96fcdcc2015-08-27 07:41:13 -0700213 T* obj2 = nullptr;
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000214 SerializationUtils<T>::Read(buffer2, &obj2);
215 const unsigned char* peekAfter = static_cast<const unsigned char*>(buffer2.skip(0));
216 if (shouldSucceed) {
217 // This should have succeeded, since there are enough bytes to read this
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000218 REPORTER_ASSERT(reporter, buffer2.isValid());
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000219 REPORTER_ASSERT(reporter, static_cast<size_t>(peekAfter - peekBefore) == bytesWritten);
bsalomon49f085d2014-09-05 13:34:00 -0700220 REPORTER_ASSERT(reporter, obj2);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000221 } else {
222 // If the deserialization was supposed to fail, make sure it did
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000223 REPORTER_ASSERT(reporter, !buffer.isValid());
halcanary96fcdcc2015-08-27 07:41:13 -0700224 REPORTER_ASSERT(reporter, nullptr == obj2);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000225 }
226
227 return obj2; // Return object to perform further validity tests on it
228}
229
230template<typename T>
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000231static void TestArraySerialization(T* data, skiatest::Reporter* reporter) {
brianosmanfad98562016-05-04 11:06:28 -0700232 SkBinaryWriteBuffer writer;
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000233 SerializationUtils<T>::Write(writer, data, kArraySize);
234 size_t bytesWritten = writer.bytesWritten();
235 // This should write the length (in 4 bytes) and the array
236 REPORTER_ASSERT(reporter, (4 + kArraySize * sizeof(T)) == bytesWritten);
237
brianosman97bbf822016-09-25 13:15:58 -0700238 unsigned char dataWritten[2048];
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000239 writer.writeToMemory(dataWritten);
240
241 // Make sure this fails when it should
242 SkValidatingReadBuffer buffer(dataWritten, bytesWritten);
243 T dataRead[kArraySize];
244 bool success = SerializationUtils<T>::Read(buffer, dataRead, kArraySize / 2);
245 // This should have failed, since the provided size was too small
246 REPORTER_ASSERT(reporter, !success);
247
248 // Make sure this succeeds when it should
249 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten);
250 success = SerializationUtils<T>::Read(buffer2, dataRead, kArraySize);
251 // This should have succeeded, since there are enough bytes to read this
252 REPORTER_ASSERT(reporter, success);
253}
254
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000255static void TestBitmapSerialization(const SkBitmap& validBitmap,
256 const SkBitmap& invalidBitmap,
257 bool shouldSucceed,
258 skiatest::Reporter* reporter) {
reed9ce9d672016-03-17 10:51:11 -0700259 sk_sp<SkImage> validImage(SkImage::MakeFromBitmap(validBitmap));
robertphillips549c8992016-04-01 09:28:51 -0700260 sk_sp<SkImageFilter> validBitmapSource(SkImageSource::Make(std::move(validImage)));
reed9ce9d672016-03-17 10:51:11 -0700261 sk_sp<SkImage> invalidImage(SkImage::MakeFromBitmap(invalidBitmap));
robertphillips549c8992016-04-01 09:28:51 -0700262 sk_sp<SkImageFilter> invalidBitmapSource(SkImageSource::Make(std::move(invalidImage)));
reedcfb6bdf2016-03-29 11:32:50 -0700263 sk_sp<SkImageFilter> xfermodeImageFilter(
reed374772b2016-10-05 17:33:02 -0700264 SkXfermodeImageFilter::Make(SkBlendMode::kSrcOver,
robertphillips8c0326d2016-04-05 12:48:34 -0700265 std::move(invalidBitmapSource),
266 std::move(validBitmapSource), nullptr));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000267
Mike Reed5e257172016-11-01 11:22:05 -0400268 sk_sp<SkImageFilter> deserializedFilter(
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000269 TestFlattenableSerialization<SkImageFilter>(
reedcfb6bdf2016-03-29 11:32:50 -0700270 xfermodeImageFilter.get(), shouldSucceed, reporter));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000271
272 // Try to render a small bitmap using the invalid deserialized filter
273 // to make sure we don't crash while trying to render it
274 if (shouldSucceed) {
275 SkBitmap bitmap;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000276 bitmap.allocN32Pixels(24, 24);
277 SkCanvas canvas(bitmap);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000278 canvas.clear(0x00000000);
279 SkPaint paint;
280 paint.setImageFilter(deserializedFilter);
281 canvas.clipRect(SkRect::MakeXYWH(0, 0, SkIntToScalar(24), SkIntToScalar(24)));
282 canvas.drawBitmap(bitmap, 0, 0, &paint);
283 }
284}
285
senorblanco91c395a2014-09-25 15:51:35 -0700286static void TestColorFilterSerialization(skiatest::Reporter* reporter) {
287 uint8_t table[256];
288 for (int i = 0; i < 256; ++i) {
289 table[i] = (i * 41) % 256;
290 }
reedd053ce92016-03-22 10:17:23 -0700291 auto colorFilter(SkTableColorFilter::Make(table));
Hal Canary342b7ac2016-11-04 11:49:42 -0400292 sk_sp<SkColorFilter> copy(
senorblanco91c395a2014-09-25 15:51:35 -0700293 TestFlattenableSerialization<SkColorFilter>(colorFilter.get(), true, reporter));
294}
295
caseq26337e92014-06-30 12:14:52 -0700296static SkBitmap draw_picture(SkPicture& picture) {
297 SkBitmap bitmap;
halcanary9d524f22016-03-29 09:03:52 -0700298 bitmap.allocN32Pixels(SkScalarCeilToInt(picture.cullRect().width()),
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700299 SkScalarCeilToInt(picture.cullRect().height()));
caseq26337e92014-06-30 12:14:52 -0700300 SkCanvas canvas(bitmap);
robertphillipsc5ba71d2014-09-04 08:42:50 -0700301 picture.playback(&canvas);
caseq26337e92014-06-30 12:14:52 -0700302 return bitmap;
303}
304
305static void compare_bitmaps(skiatest::Reporter* reporter,
306 const SkBitmap& b1, const SkBitmap& b2) {
307 REPORTER_ASSERT(reporter, b1.width() == b2.width());
308 REPORTER_ASSERT(reporter, b1.height() == b2.height());
caseq26337e92014-06-30 12:14:52 -0700309
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}
bungeman13b9c952016-05-12 10:09:30 -0700324static void serialize_and_compare_typeface(sk_sp<SkTypeface> typeface, const char* text,
bungeman41868fe2015-05-20 09:21:04 -0700325 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));
bungeman13b9c952016-05-12 10:09:30 -0700331 paint.setTypeface(std::move(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);
Ben Wagner145dbcd2016-11-03 14:40:50 -0400346 std::unique_ptr<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");
bungeman13b9c952016-05-12 10:09:30 -0700359 sk_sp<SkTypeface> typeface(SkTypeface::MakeFromFile(filename.c_str(), 1));
bungeman41868fe2015-05-20 09:21:04 -0700360 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 {
bungeman13b9c952016-05-12 10:09:30 -0700363 serialize_and_compare_typeface(std::move(typeface), "A!", reporter);
bungeman41868fe2015-05-20 09:21:04 -0700364 }
365 }
366
367 {
368 // Load typeface as stream to create with axis settings.
bungemanf93d7112016-09-16 06:24:20 -0700369 std::unique_ptr<SkStreamAsset> distortable(GetResourceAsStream("/fonts/Distortable.ttf"));
bungeman41868fe2015-05-20 09:21:04 -0700370 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;
bungeman13b9c952016-05-12 10:09:30 -0700374 sk_sp<SkTypeface> typeface(SkTypeface::MakeFromFontData(
bungemanf93d7112016-09-16 06:24:20 -0700375 skstd::make_unique<SkFontData>(std::move(distortable), 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 {
bungeman13b9c952016-05-12 10:09:30 -0700379 serialize_and_compare_typeface(std::move(typeface), "abc", reporter);
bungeman41868fe2015-05-20 09:21:04 -0700380 }
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));
Cary Clark2a475ea2017-04-28 15:35:12 -0400429 canvas->drawString("Picture", 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
senorblanco91c395a2014-09-25 15:51:35 -0700451 // Test color filter serialization
452 {
453 TestColorFilterSerialization(reporter);
454 }
455
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000456 // Test string serialization
457 {
458 SkString string("string");
459 TestObjectSerializationNoAlign<SkString, false>(&string, reporter);
460 TestObjectSerializationNoAlign<SkString, true>(&string, reporter);
461 }
462
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000463 // Test rrect serialization
464 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000465 // SkRRect does not initialize anything.
466 // An uninitialized SkRRect can be serialized,
467 // but will branch on uninitialized data when deserialized.
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000468 SkRRect rrect;
rmistry@google.comd6bab022013-12-02 13:50:38 +0000469 SkRect rect = SkRect::MakeXYWH(1, 2, 20, 30);
470 SkVector corners[4] = { {1, 2}, {2, 3}, {3,4}, {4,5} };
471 rrect.setRectRadii(rect, corners);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000472 TestAlignment(&rrect, reporter);
473 }
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000474
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000475 // Test readByteArray
476 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000477 unsigned char data[kArraySize] = { 1, 2, 3 };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000478 TestArraySerialization(data, reporter);
479 }
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000480
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000481 // Test readColorArray
482 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000483 SkColor data[kArraySize] = { SK_ColorBLACK, SK_ColorWHITE, SK_ColorRED };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000484 TestArraySerialization(data, reporter);
485 }
486
brianosman97bbf822016-09-25 13:15:58 -0700487 // Test readColor4fArray
488 {
489 SkColor4f data[kArraySize] = {
490 SkColor4f::FromColor(SK_ColorBLACK),
491 SkColor4f::FromColor(SK_ColorWHITE),
492 SkColor4f::FromColor(SK_ColorRED),
493 { 1.f, 2.f, 4.f, 8.f }
494 };
495 TestArraySerialization(data, reporter);
496 }
497
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000498 // Test readIntArray
499 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000500 int32_t data[kArraySize] = { 1, 2, 4, 8 };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000501 TestArraySerialization(data, reporter);
502 }
503
504 // Test readPointArray
505 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000506 SkPoint data[kArraySize] = { {6, 7}, {42, 128} };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000507 TestArraySerialization(data, reporter);
508 }
509
510 // Test readScalarArray
511 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000512 SkScalar data[kArraySize] = { SK_Scalar1, SK_ScalarHalf, SK_ScalarMax };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000513 TestArraySerialization(data, reporter);
commit-bot@chromium.org02512882013-10-31 18:37:50 +0000514 }
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000515
516 // Test invalid deserializations
517 {
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000518 SkImageInfo info = SkImageInfo::MakeN32Premul(kBitmapSize, kBitmapSize);
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000519
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000520 SkBitmap validBitmap;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000521 validBitmap.setInfo(info);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000522
523 // Create a bitmap with a really large height
524 SkBitmap invalidBitmap;
reede5ea5002014-09-03 11:54:58 -0700525 invalidBitmap.setInfo(info.makeWH(info.width(), 1000000000));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000526
527 // The deserialization should succeed, and the rendering shouldn't crash,
528 // even when the device fails to initialize, due to its size
529 TestBitmapSerialization(validBitmap, invalidBitmap, true, reporter);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000530 }
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000531
532 // Test simple SkPicture serialization
533 {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000534 SkPictureRecorder recorder;
reed84825042014-09-02 12:50:45 -0700535 draw_something(recorder.beginRecording(SkIntToScalar(kBitmapSize),
536 SkIntToScalar(kBitmapSize),
halcanary96fcdcc2015-08-27 07:41:13 -0700537 nullptr, 0));
reedca2622b2016-03-18 07:25:55 -0700538 sk_sp<SkPicture> pict(recorder.finishRecordingAsPicture());
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000539
540 // Serialize picture
brianosmanfad98562016-05-04 11:06:28 -0700541 SkBinaryWriteBuffer writer;
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000542 pict->flatten(writer);
543 size_t size = writer.bytesWritten();
commit-bot@chromium.org1e7ee992014-03-14 21:22:22 +0000544 SkAutoTMalloc<unsigned char> data(size);
545 writer.writeToMemory(static_cast<void*>(data.get()));
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000546
547 // Deserialize picture
commit-bot@chromium.org1e7ee992014-03-14 21:22:22 +0000548 SkValidatingReadBuffer reader(static_cast<void*>(data.get()), size);
reedca2622b2016-03-18 07:25:55 -0700549 sk_sp<SkPicture> readPict(SkPicture::MakeFromBuffer(reader));
reedd921dbb2016-09-30 09:27:20 -0700550 REPORTER_ASSERT(reporter, reader.isValid());
bsalomon49f085d2014-09-05 13:34:00 -0700551 REPORTER_ASSERT(reporter, readPict.get());
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000552 }
caseq26337e92014-06-30 12:14:52 -0700553
554 TestPictureTypefaceSerialization(reporter);
dvonbeck8811e402016-06-16 12:39:25 -0700555
556 // Test SkLightingShader/NormalMapSource serialization
557 {
558 const int kTexSize = 2;
559
560 SkLights::Builder builder;
561
vjiaoblack772b5ee2016-08-12 11:38:47 -0700562 builder.add(SkLights::Light::MakeDirectional(SkColor3f::Make(1.0f, 1.0f, 1.0f),
563 SkVector3::Make(1.0f, 0.0f, 0.0f)));
vjiaoblacka8eabc42016-08-29 10:22:09 -0700564 builder.setAmbientLightColor(SkColor3f::Make(0.2f, 0.2f, 0.2f));
dvonbeck8811e402016-06-16 12:39:25 -0700565
566 sk_sp<SkLights> fLights = builder.finish();
567
568 SkBitmap diffuse = sk_tool_utils::create_checkerboard_bitmap(
569 kTexSize, kTexSize,
570 sk_tool_utils::color_to_565(0x0),
571 sk_tool_utils::color_to_565(0xFF804020),
572 8);
573
574 SkRect bitmapBounds = SkRect::MakeIWH(diffuse.width(), diffuse.height());
575
576 SkMatrix matrix;
577 SkRect r = SkRect::MakeWH(SkIntToScalar(kTexSize), SkIntToScalar(kTexSize));
578 matrix.setRectToRect(bitmapBounds, r, SkMatrix::kFill_ScaleToFit);
579
dvonbeck5b794fa2016-07-06 13:58:36 -0700580 SkMatrix ctm;
581 ctm.setRotate(45);
dvonbeck8811e402016-06-16 12:39:25 -0700582 SkBitmap normals;
583 normals.allocN32Pixels(kTexSize, kTexSize);
584
585 sk_tool_utils::create_frustum_normal_map(&normals, SkIRect::MakeWH(kTexSize, kTexSize));
reed1ec04d92016-08-05 12:07:41 -0700586 sk_sp<SkShader> normalMap = SkShader::MakeBitmapShader(normals, SkShader::kClamp_TileMode,
587 SkShader::kClamp_TileMode, &matrix);
dvonbeck5b794fa2016-07-06 13:58:36 -0700588 sk_sp<SkNormalSource> normalSource = SkNormalSource::MakeFromNormalMap(std::move(normalMap),
589 ctm);
reed1ec04d92016-08-05 12:07:41 -0700590 sk_sp<SkShader> diffuseShader = SkShader::MakeBitmapShader(diffuse,
591 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, &matrix);
dvonbeckc526da92016-07-20 11:20:30 -0700592
593 sk_sp<SkShader> lightingShader = SkLightingShader::Make(diffuseShader,
594 normalSource,
dvonbeck6af677f2016-07-10 18:38:33 -0700595 fLights);
Florin Malita4aed1382017-05-25 10:38:07 -0400596 sk_sp<SkShader>(TestFlattenableSerialization(as_SB(lightingShader.get()), true, reporter));
dvonbeck8811e402016-06-16 12:39:25 -0700597
dvonbeckc526da92016-07-20 11:20:30 -0700598 lightingShader = SkLightingShader::Make(std::move(diffuseShader),
599 nullptr,
600 fLights);
Florin Malita4aed1382017-05-25 10:38:07 -0400601 sk_sp<SkShader>(TestFlattenableSerialization(as_SB(lightingShader.get()), true, reporter));
dvonbeckc526da92016-07-20 11:20:30 -0700602
603 lightingShader = SkLightingShader::Make(nullptr,
604 std::move(normalSource),
605 fLights);
Florin Malita4aed1382017-05-25 10:38:07 -0400606 sk_sp<SkShader>(TestFlattenableSerialization(as_SB(lightingShader.get()), true, reporter));
dvonbeckc526da92016-07-20 11:20:30 -0700607
608 lightingShader = SkLightingShader::Make(nullptr,
609 nullptr,
610 fLights);
Florin Malita4aed1382017-05-25 10:38:07 -0400611 sk_sp<SkShader>(TestFlattenableSerialization(as_SB(lightingShader.get()), true, reporter));
dvonbeck8811e402016-06-16 12:39:25 -0700612 }
commit-bot@chromium.org02512882013-10-31 18:37:50 +0000613}
reedf70b5312016-03-04 16:36:20 -0800614
615///////////////////////////////////////////////////////////////////////////////////////////////////
616#include "SkAnnotation.h"
617
reedca2622b2016-03-18 07:25:55 -0700618static sk_sp<SkPicture> copy_picture_via_serialization(SkPicture* src) {
reedf70b5312016-03-04 16:36:20 -0800619 SkDynamicMemoryWStream wstream;
620 src->serialize(&wstream);
Ben Wagner145dbcd2016-11-03 14:40:50 -0400621 std::unique_ptr<SkStreamAsset> rstream(wstream.detachAsStream());
622 return SkPicture::MakeFromStream(rstream.get());
reedf70b5312016-03-04 16:36:20 -0800623}
624
625struct AnnotationRec {
626 const SkRect fRect;
627 const char* fKey;
bungeman38d909e2016-08-02 14:40:46 -0700628 sk_sp<SkData> fValue;
reedf70b5312016-03-04 16:36:20 -0800629};
630
631class TestAnnotationCanvas : public SkCanvas {
632 skiatest::Reporter* fReporter;
633 const AnnotationRec* fRec;
634 int fCount;
635 int fCurrIndex;
636
637public:
638 TestAnnotationCanvas(skiatest::Reporter* reporter, const AnnotationRec rec[], int count)
639 : SkCanvas(100, 100)
640 , fReporter(reporter)
641 , fRec(rec)
642 , fCount(count)
643 , fCurrIndex(0)
644 {}
645
646 ~TestAnnotationCanvas() {
647 REPORTER_ASSERT(fReporter, fCount == fCurrIndex);
648 }
649
650protected:
651 void onDrawAnnotation(const SkRect& rect, const char key[], SkData* value) {
652 REPORTER_ASSERT(fReporter, fCurrIndex < fCount);
653 REPORTER_ASSERT(fReporter, rect == fRec[fCurrIndex].fRect);
654 REPORTER_ASSERT(fReporter, !strcmp(key, fRec[fCurrIndex].fKey));
bungeman38d909e2016-08-02 14:40:46 -0700655 REPORTER_ASSERT(fReporter, value->equals(fRec[fCurrIndex].fValue.get()));
reedf70b5312016-03-04 16:36:20 -0800656 fCurrIndex += 1;
657 }
658};
659
660/*
661 * Test the 3 annotation types by recording them into a picture, serializing, and then playing
662 * them back into another canvas.
663 */
664DEF_TEST(Annotations, reporter) {
665 SkPictureRecorder recorder;
666 SkCanvas* recordingCanvas = recorder.beginRecording(SkRect::MakeWH(100, 100));
halcanary9d524f22016-03-29 09:03:52 -0700667
reedf70b5312016-03-04 16:36:20 -0800668 const char* str0 = "rect-with-url";
669 const SkRect r0 = SkRect::MakeWH(10, 10);
bungeman38d909e2016-08-02 14:40:46 -0700670 sk_sp<SkData> d0(SkData::MakeWithCString(str0));
671 SkAnnotateRectWithURL(recordingCanvas, r0, d0.get());
halcanary9d524f22016-03-29 09:03:52 -0700672
reedf70b5312016-03-04 16:36:20 -0800673 const char* str1 = "named-destination";
674 const SkRect r1 = SkRect::MakeXYWH(5, 5, 0, 0); // collapsed to a point
bungeman38d909e2016-08-02 14:40:46 -0700675 sk_sp<SkData> d1(SkData::MakeWithCString(str1));
676 SkAnnotateNamedDestination(recordingCanvas, {r1.x(), r1.y()}, d1.get());
halcanary9d524f22016-03-29 09:03:52 -0700677
reedf70b5312016-03-04 16:36:20 -0800678 const char* str2 = "link-to-destination";
679 const SkRect r2 = SkRect::MakeXYWH(20, 20, 5, 6);
bungeman38d909e2016-08-02 14:40:46 -0700680 sk_sp<SkData> d2(SkData::MakeWithCString(str2));
681 SkAnnotateLinkToDestination(recordingCanvas, r2, d2.get());
reedf70b5312016-03-04 16:36:20 -0800682
683 const AnnotationRec recs[] = {
bungeman38d909e2016-08-02 14:40:46 -0700684 { r0, SkAnnotationKeys::URL_Key(), std::move(d0) },
685 { r1, SkAnnotationKeys::Define_Named_Dest_Key(), std::move(d1) },
686 { r2, SkAnnotationKeys::Link_Named_Dest_Key(), std::move(d2) },
reedf70b5312016-03-04 16:36:20 -0800687 };
688
reedca2622b2016-03-18 07:25:55 -0700689 sk_sp<SkPicture> pict0(recorder.finishRecordingAsPicture());
690 sk_sp<SkPicture> pict1(copy_picture_via_serialization(pict0.get()));
reedf70b5312016-03-04 16:36:20 -0800691
692 TestAnnotationCanvas canvas(reporter, recs, SK_ARRAY_COUNT(recs));
693 canvas.drawPicture(pict1);
694}