blob: e4e1a7acd7b0e0f1a408362ca846ccaadfac55ca [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"
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +000016#include "SkMallocPixelRef.h"
caseq26337e92014-06-30 12:14:52 -070017#include "SkOSFile.h"
robertphillips@google.com770963f2014-04-18 18:04:41 +000018#include "SkPictureRecorder.h"
senorblanco91c395a2014-09-25 15:51:35 -070019#include "SkTableColorFilter.h"
commit-bot@chromium.org1e7ee992014-03-14 21:22:22 +000020#include "SkTemplates.h"
caseq26337e92014-06-30 12:14:52 -070021#include "SkTypeface.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000022#include "SkWriteBuffer.h"
commit-bot@chromium.org02512882013-10-31 18:37:50 +000023#include "SkValidatingReadBuffer.h"
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +000024#include "SkXfermodeImageFilter.h"
dvonbeck8811e402016-06-16 12:39:25 -070025#include "sk_tool_utils.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000026#include "Test.h"
commit-bot@chromium.org02512882013-10-31 18:37:50 +000027
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000028static const uint32_t kArraySize = 64;
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +000029static const int kBitmapSize = 256;
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000030
31template<typename T>
32static void TestAlignment(T* testObj, skiatest::Reporter* reporter) {
33 // Test memory read/write functions directly
34 unsigned char dataWritten[1024];
35 size_t bytesWrittenToMemory = testObj->writeToMemory(dataWritten);
36 REPORTER_ASSERT(reporter, SkAlign4(bytesWrittenToMemory) == bytesWrittenToMemory);
37 size_t bytesReadFromMemory = testObj->readFromMemory(dataWritten, bytesWrittenToMemory);
38 REPORTER_ASSERT(reporter, SkAlign4(bytesReadFromMemory) == bytesReadFromMemory);
39}
40
41template<typename T> struct SerializationUtils {
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +000042 // Generic case for flattenables
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000043 static void Write(SkWriteBuffer& writer, const T* flattenable) {
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +000044 writer.writeFlattenable(flattenable);
45 }
46 static void Read(SkValidatingReadBuffer& reader, T** flattenable) {
mtklein3b375452016-04-04 14:57:19 -070047 *flattenable = (T*)reader.readFlattenable(T::GetFlattenableType());
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +000048 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000049};
50
51template<> struct SerializationUtils<SkMatrix> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000052 static void Write(SkWriteBuffer& writer, const SkMatrix* matrix) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000053 writer.writeMatrix(*matrix);
54 }
55 static void Read(SkValidatingReadBuffer& reader, SkMatrix* matrix) {
56 reader.readMatrix(matrix);
57 }
58};
59
60template<> struct SerializationUtils<SkPath> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000061 static void Write(SkWriteBuffer& writer, const SkPath* path) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000062 writer.writePath(*path);
63 }
64 static void Read(SkValidatingReadBuffer& reader, SkPath* path) {
65 reader.readPath(path);
66 }
67};
68
69template<> struct SerializationUtils<SkRegion> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000070 static void Write(SkWriteBuffer& writer, const SkRegion* region) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000071 writer.writeRegion(*region);
72 }
73 static void Read(SkValidatingReadBuffer& reader, SkRegion* region) {
74 reader.readRegion(region);
75 }
76};
77
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +000078template<> struct SerializationUtils<SkString> {
79 static void Write(SkWriteBuffer& writer, const SkString* string) {
80 writer.writeString(string->c_str());
81 }
82 static void Read(SkValidatingReadBuffer& reader, SkString* string) {
83 reader.readString(string);
84 }
85};
86
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000087template<> struct SerializationUtils<unsigned char> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000088 static void Write(SkWriteBuffer& writer, unsigned char* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +000089 writer.writeByteArray(data, arraySize);
sugoi@google.com305f78e2013-11-04 16:18:15 +000090 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000091 static bool Read(SkValidatingReadBuffer& reader, unsigned char* data, uint32_t arraySize) {
92 return reader.readByteArray(data, arraySize);
93 }
94};
commit-bot@chromium.org02512882013-10-31 18:37:50 +000095
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000096template<> struct SerializationUtils<SkColor> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000097 static void Write(SkWriteBuffer& writer, SkColor* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +000098 writer.writeColorArray(data, arraySize);
sugoi@google.comb48a59a2013-11-04 20:28:23 +000099 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000100 static bool Read(SkValidatingReadBuffer& reader, SkColor* data, uint32_t arraySize) {
101 return reader.readColorArray(data, arraySize);
102 }
103};
sugoi@google.com305f78e2013-11-04 16:18:15 +0000104
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000105template<> struct SerializationUtils<int32_t> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000106 static void Write(SkWriteBuffer& writer, int32_t* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +0000107 writer.writeIntArray(data, arraySize);
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000108 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000109 static bool Read(SkValidatingReadBuffer& reader, int32_t* data, uint32_t arraySize) {
110 return reader.readIntArray(data, arraySize);
111 }
112};
sugoi@google.com305f78e2013-11-04 16:18:15 +0000113
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000114template<> struct SerializationUtils<SkPoint> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000115 static void Write(SkWriteBuffer& writer, SkPoint* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +0000116 writer.writePointArray(data, arraySize);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000117 }
118 static bool Read(SkValidatingReadBuffer& reader, SkPoint* data, uint32_t arraySize) {
119 return reader.readPointArray(data, arraySize);
120 }
121};
reed@google.com12a23862013-11-04 21:35:55 +0000122
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000123template<> struct SerializationUtils<SkScalar> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000124 static void Write(SkWriteBuffer& writer, SkScalar* data, uint32_t arraySize) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000125 writer.writeScalarArray(data, arraySize);
126 }
127 static bool Read(SkValidatingReadBuffer& reader, SkScalar* data, uint32_t arraySize) {
128 return reader.readScalarArray(data, arraySize);
129 }
130};
reed@google.com12a23862013-11-04 21:35:55 +0000131
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000132template<typename T, bool testInvalid> struct SerializationTestUtils {
133 static void InvalidateData(unsigned char* data) {}
134};
135
136template<> struct SerializationTestUtils<SkString, true> {
137 static void InvalidateData(unsigned char* data) {
138 data[3] |= 0x80; // Reverse sign of 1st integer
139 }
140};
141
142template<typename T, bool testInvalid>
143static void TestObjectSerializationNoAlign(T* testObj, skiatest::Reporter* reporter) {
brianosmanfad98562016-05-04 11:06:28 -0700144 SkBinaryWriteBuffer writer;
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000145 SerializationUtils<T>::Write(writer, testObj);
146 size_t bytesWritten = writer.bytesWritten();
147 REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten);
reed@google.com12a23862013-11-04 21:35:55 +0000148
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000149 unsigned char dataWritten[1024];
150 writer.writeToMemory(dataWritten);
151
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000152 SerializationTestUtils<T, testInvalid>::InvalidateData(dataWritten);
153
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000154 // Make sure this fails when it should (test with smaller size, but still multiple of 4)
155 SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 4);
commit-bot@chromium.org8f457e32013-11-08 19:22:57 +0000156 T obj;
157 SerializationUtils<T>::Read(buffer, &obj);
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000158 REPORTER_ASSERT(reporter, !buffer.isValid());
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000159
160 // Make sure this succeeds when it should
161 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten);
commit-bot@chromium.org8f457e32013-11-08 19:22:57 +0000162 const unsigned char* peekBefore = static_cast<const unsigned char*>(buffer2.skip(0));
163 T obj2;
164 SerializationUtils<T>::Read(buffer2, &obj2);
165 const unsigned char* peekAfter = static_cast<const unsigned char*>(buffer2.skip(0));
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000166 // This should have succeeded, since there are enough bytes to read this
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000167 REPORTER_ASSERT(reporter, buffer2.isValid() == !testInvalid);
168 // Note: This following test should always succeed, regardless of whether the buffer is valid,
169 // 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 +0000170 REPORTER_ASSERT(reporter, static_cast<size_t>(peekAfter - peekBefore) == bytesWritten);
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000171}
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000172
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000173template<typename T>
174static void TestObjectSerialization(T* testObj, skiatest::Reporter* reporter) {
175 TestObjectSerializationNoAlign<T, false>(testObj, reporter);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000176 TestAlignment(testObj, reporter);
177}
178
179template<typename T>
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000180static T* TestFlattenableSerialization(T* testObj, bool shouldSucceed,
181 skiatest::Reporter* reporter) {
brianosmanfad98562016-05-04 11:06:28 -0700182 SkBinaryWriteBuffer writer;
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000183 SerializationUtils<T>::Write(writer, testObj);
184 size_t bytesWritten = writer.bytesWritten();
185 REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten);
186
dvonbeck8811e402016-06-16 12:39:25 -0700187 SkASSERT(bytesWritten <= 4096);
senorblanco91c395a2014-09-25 15:51:35 -0700188 unsigned char dataWritten[4096];
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000189 writer.writeToMemory(dataWritten);
190
191 // Make sure this fails when it should (test with smaller size, but still multiple of 4)
192 SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 4);
halcanary96fcdcc2015-08-27 07:41:13 -0700193 T* obj = nullptr;
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000194 SerializationUtils<T>::Read(buffer, &obj);
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000195 REPORTER_ASSERT(reporter, !buffer.isValid());
halcanary96fcdcc2015-08-27 07:41:13 -0700196 REPORTER_ASSERT(reporter, nullptr == obj);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000197
198 // Make sure this succeeds when it should
199 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten);
200 const unsigned char* peekBefore = static_cast<const unsigned char*>(buffer2.skip(0));
halcanary96fcdcc2015-08-27 07:41:13 -0700201 T* obj2 = nullptr;
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000202 SerializationUtils<T>::Read(buffer2, &obj2);
203 const unsigned char* peekAfter = static_cast<const unsigned char*>(buffer2.skip(0));
204 if (shouldSucceed) {
205 // This should have succeeded, since there are enough bytes to read this
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000206 REPORTER_ASSERT(reporter, buffer2.isValid());
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000207 REPORTER_ASSERT(reporter, static_cast<size_t>(peekAfter - peekBefore) == bytesWritten);
bsalomon49f085d2014-09-05 13:34:00 -0700208 REPORTER_ASSERT(reporter, obj2);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000209 } else {
210 // If the deserialization was supposed to fail, make sure it did
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000211 REPORTER_ASSERT(reporter, !buffer.isValid());
halcanary96fcdcc2015-08-27 07:41:13 -0700212 REPORTER_ASSERT(reporter, nullptr == obj2);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000213 }
214
215 return obj2; // Return object to perform further validity tests on it
216}
217
218template<typename T>
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000219static void TestArraySerialization(T* data, skiatest::Reporter* reporter) {
brianosmanfad98562016-05-04 11:06:28 -0700220 SkBinaryWriteBuffer writer;
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000221 SerializationUtils<T>::Write(writer, data, kArraySize);
222 size_t bytesWritten = writer.bytesWritten();
223 // This should write the length (in 4 bytes) and the array
224 REPORTER_ASSERT(reporter, (4 + kArraySize * sizeof(T)) == bytesWritten);
225
226 unsigned char dataWritten[1024];
227 writer.writeToMemory(dataWritten);
228
229 // Make sure this fails when it should
230 SkValidatingReadBuffer buffer(dataWritten, bytesWritten);
231 T dataRead[kArraySize];
232 bool success = SerializationUtils<T>::Read(buffer, dataRead, kArraySize / 2);
233 // This should have failed, since the provided size was too small
234 REPORTER_ASSERT(reporter, !success);
235
236 // Make sure this succeeds when it should
237 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten);
238 success = SerializationUtils<T>::Read(buffer2, dataRead, kArraySize);
239 // This should have succeeded, since there are enough bytes to read this
240 REPORTER_ASSERT(reporter, success);
241}
242
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000243static void TestBitmapSerialization(const SkBitmap& validBitmap,
244 const SkBitmap& invalidBitmap,
245 bool shouldSucceed,
246 skiatest::Reporter* reporter) {
reed9ce9d672016-03-17 10:51:11 -0700247 sk_sp<SkImage> validImage(SkImage::MakeFromBitmap(validBitmap));
robertphillips549c8992016-04-01 09:28:51 -0700248 sk_sp<SkImageFilter> validBitmapSource(SkImageSource::Make(std::move(validImage)));
reed9ce9d672016-03-17 10:51:11 -0700249 sk_sp<SkImage> invalidImage(SkImage::MakeFromBitmap(invalidBitmap));
robertphillips549c8992016-04-01 09:28:51 -0700250 sk_sp<SkImageFilter> invalidBitmapSource(SkImageSource::Make(std::move(invalidImage)));
reedcfb6bdf2016-03-29 11:32:50 -0700251 sk_sp<SkImageFilter> xfermodeImageFilter(
252 SkXfermodeImageFilter::Make(SkXfermode::Make(SkXfermode::kSrcOver_Mode),
robertphillips8c0326d2016-04-05 12:48:34 -0700253 std::move(invalidBitmapSource),
254 std::move(validBitmapSource), nullptr));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000255
256 SkAutoTUnref<SkImageFilter> deserializedFilter(
257 TestFlattenableSerialization<SkImageFilter>(
reedcfb6bdf2016-03-29 11:32:50 -0700258 xfermodeImageFilter.get(), shouldSucceed, reporter));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000259
260 // Try to render a small bitmap using the invalid deserialized filter
261 // to make sure we don't crash while trying to render it
262 if (shouldSucceed) {
263 SkBitmap bitmap;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000264 bitmap.allocN32Pixels(24, 24);
265 SkCanvas canvas(bitmap);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000266 canvas.clear(0x00000000);
267 SkPaint paint;
268 paint.setImageFilter(deserializedFilter);
269 canvas.clipRect(SkRect::MakeXYWH(0, 0, SkIntToScalar(24), SkIntToScalar(24)));
270 canvas.drawBitmap(bitmap, 0, 0, &paint);
271 }
272}
273
senorblanco0f7197b2014-09-24 11:09:38 -0700274static void TestXfermodeSerialization(skiatest::Reporter* reporter) {
275 for (size_t i = 0; i <= SkXfermode::kLastMode; ++i) {
276 if (i == SkXfermode::kSrcOver_Mode) {
halcanary96fcdcc2015-08-27 07:41:13 -0700277 // skip SrcOver, as it is allowed to return nullptr from Create()
senorblanco0f7197b2014-09-24 11:09:38 -0700278 continue;
279 }
reedcfb6bdf2016-03-29 11:32:50 -0700280 auto mode(SkXfermode::Make(static_cast<SkXfermode::Mode>(i)));
281 REPORTER_ASSERT(reporter, mode);
senorblanco0f7197b2014-09-24 11:09:38 -0700282 SkAutoTUnref<SkXfermode> copy(
283 TestFlattenableSerialization<SkXfermode>(mode.get(), true, reporter));
284 }
285}
286
senorblanco91c395a2014-09-25 15:51:35 -0700287static void TestColorFilterSerialization(skiatest::Reporter* reporter) {
288 uint8_t table[256];
289 for (int i = 0; i < 256; ++i) {
290 table[i] = (i * 41) % 256;
291 }
reedd053ce92016-03-22 10:17:23 -0700292 auto colorFilter(SkTableColorFilter::Make(table));
senorblanco91c395a2014-09-25 15:51:35 -0700293 SkAutoTUnref<SkColorFilter> copy(
294 TestFlattenableSerialization<SkColorFilter>(colorFilter.get(), true, reporter));
295}
296
caseq26337e92014-06-30 12:14:52 -0700297static SkBitmap draw_picture(SkPicture& picture) {
298 SkBitmap bitmap;
halcanary9d524f22016-03-29 09:03:52 -0700299 bitmap.allocN32Pixels(SkScalarCeilToInt(picture.cullRect().width()),
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700300 SkScalarCeilToInt(picture.cullRect().height()));
caseq26337e92014-06-30 12:14:52 -0700301 SkCanvas canvas(bitmap);
robertphillipsc5ba71d2014-09-04 08:42:50 -0700302 picture.playback(&canvas);
caseq26337e92014-06-30 12:14:52 -0700303 return bitmap;
304}
305
306static void compare_bitmaps(skiatest::Reporter* reporter,
307 const SkBitmap& b1, const SkBitmap& b2) {
308 REPORTER_ASSERT(reporter, b1.width() == b2.width());
309 REPORTER_ASSERT(reporter, b1.height() == b2.height());
310 SkAutoLockPixels autoLockPixels1(b1);
311 SkAutoLockPixels autoLockPixels2(b2);
312
313 if ((b1.width() != b2.width()) ||
314 (b1.height() != b2.height())) {
315 return;
316 }
317
318 int pixelErrors = 0;
319 for (int y = 0; y < b2.height(); ++y) {
320 for (int x = 0; x < b2.width(); ++x) {
321 if (b1.getColor(x, y) != b2.getColor(x, y))
322 ++pixelErrors;
323 }
324 }
325 REPORTER_ASSERT(reporter, 0 == pixelErrors);
326}
bungeman13b9c952016-05-12 10:09:30 -0700327static void serialize_and_compare_typeface(sk_sp<SkTypeface> typeface, const char* text,
bungeman41868fe2015-05-20 09:21:04 -0700328 skiatest::Reporter* reporter)
329{
330 // Create a paint with the typeface.
caseq26337e92014-06-30 12:14:52 -0700331 SkPaint paint;
332 paint.setColor(SK_ColorGRAY);
333 paint.setTextSize(SkIntToScalar(30));
bungeman13b9c952016-05-12 10:09:30 -0700334 paint.setTypeface(std::move(typeface));
caseq26337e92014-06-30 12:14:52 -0700335
336 // Paint some text.
337 SkPictureRecorder recorder;
338 SkIRect canvasRect = SkIRect::MakeWH(kBitmapSize, kBitmapSize);
halcanary9d524f22016-03-29 09:03:52 -0700339 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(canvasRect.width()),
340 SkIntToScalar(canvasRect.height()),
halcanary96fcdcc2015-08-27 07:41:13 -0700341 nullptr, 0);
caseq26337e92014-06-30 12:14:52 -0700342 canvas->drawColor(SK_ColorWHITE);
bungeman41868fe2015-05-20 09:21:04 -0700343 canvas->drawText(text, 2, 24, 32, paint);
reedca2622b2016-03-18 07:25:55 -0700344 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
caseq26337e92014-06-30 12:14:52 -0700345
346 // Serlialize picture and create its clone from stream.
347 SkDynamicMemoryWStream stream;
348 picture->serialize(&stream);
scroggoa1193e42015-01-21 12:09:53 -0800349 SkAutoTDelete<SkStream> inputStream(stream.detachAsStream());
reedca2622b2016-03-18 07:25:55 -0700350 sk_sp<SkPicture> loadedPicture(SkPicture::MakeFromStream(inputStream.get()));
caseq26337e92014-06-30 12:14:52 -0700351
352 // Draw both original and clone picture and compare bitmaps -- they should be identical.
353 SkBitmap origBitmap = draw_picture(*picture);
354 SkBitmap destBitmap = draw_picture(*loadedPicture);
355 compare_bitmaps(reporter, origBitmap, destBitmap);
356}
357
bungeman41868fe2015-05-20 09:21:04 -0700358static void TestPictureTypefaceSerialization(skiatest::Reporter* reporter) {
359 {
360 // Load typeface from file to test CreateFromFile with index.
361 SkString filename = GetResourcePath("/fonts/test.ttc");
bungeman13b9c952016-05-12 10:09:30 -0700362 sk_sp<SkTypeface> typeface(SkTypeface::MakeFromFile(filename.c_str(), 1));
bungeman41868fe2015-05-20 09:21:04 -0700363 if (!typeface) {
halcanary7d571242016-02-24 17:59:16 -0800364 INFOF(reporter, "Could not run fontstream test because test.ttc not found.");
bungeman41868fe2015-05-20 09:21:04 -0700365 } else {
bungeman13b9c952016-05-12 10:09:30 -0700366 serialize_and_compare_typeface(std::move(typeface), "A!", reporter);
bungeman41868fe2015-05-20 09:21:04 -0700367 }
368 }
369
370 {
371 // Load typeface as stream to create with axis settings.
372 SkAutoTDelete<SkStreamAsset> distortable(GetResourceAsStream("/fonts/Distortable.ttf"));
373 if (!distortable) {
halcanary7d571242016-02-24 17:59:16 -0800374 INFOF(reporter, "Could not run fontstream test because Distortable.ttf not found.");
bungeman41868fe2015-05-20 09:21:04 -0700375 } else {
376 SkFixed axis = SK_FixedSqrt2;
bungeman13b9c952016-05-12 10:09:30 -0700377 sk_sp<SkTypeface> typeface(SkTypeface::MakeFromFontData(
mtklein18300a32016-03-16 13:53:35 -0700378 new SkFontData(distortable.release(), 0, &axis, 1)));
bungeman41868fe2015-05-20 09:21:04 -0700379 if (!typeface) {
halcanary7d571242016-02-24 17:59:16 -0800380 INFOF(reporter, "Could not run fontstream test because Distortable.ttf not created.");
bungeman41868fe2015-05-20 09:21:04 -0700381 } else {
bungeman13b9c952016-05-12 10:09:30 -0700382 serialize_and_compare_typeface(std::move(typeface), "abc", reporter);
bungeman41868fe2015-05-20 09:21:04 -0700383 }
384 }
385 }
386}
387
reed84825042014-09-02 12:50:45 -0700388static void setup_bitmap_for_canvas(SkBitmap* bitmap) {
389 bitmap->allocN32Pixels(kBitmapSize, kBitmapSize);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000390}
391
reed84825042014-09-02 12:50:45 -0700392static void make_checkerboard_bitmap(SkBitmap& bitmap) {
393 setup_bitmap_for_canvas(&bitmap);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000394
395 SkCanvas canvas(bitmap);
396 canvas.clear(0x00000000);
397 SkPaint darkPaint;
398 darkPaint.setColor(0xFF804020);
399 SkPaint lightPaint;
400 lightPaint.setColor(0xFF244484);
401 const int i = kBitmapSize / 8;
402 const SkScalar f = SkIntToScalar(i);
403 for (int y = 0; y < kBitmapSize; y += i) {
404 for (int x = 0; x < kBitmapSize; x += i) {
405 canvas.save();
406 canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
407 canvas.drawRect(SkRect::MakeXYWH(0, 0, f, f), darkPaint);
408 canvas.drawRect(SkRect::MakeXYWH(f, 0, f, f), lightPaint);
409 canvas.drawRect(SkRect::MakeXYWH(0, f, f, f), lightPaint);
410 canvas.drawRect(SkRect::MakeXYWH(f, f, f, f), darkPaint);
411 canvas.restore();
412 }
413 }
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000414}
415
reed84825042014-09-02 12:50:45 -0700416static void draw_something(SkCanvas* canvas) {
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000417 SkPaint paint;
418 SkBitmap bitmap;
reed84825042014-09-02 12:50:45 -0700419 make_checkerboard_bitmap(bitmap);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000420
421 canvas->save();
422 canvas->scale(0.5f, 0.5f);
halcanary96fcdcc2015-08-27 07:41:13 -0700423 canvas->drawBitmap(bitmap, 0, 0, nullptr);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000424 canvas->restore();
425
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000426 paint.setAntiAlias(true);
427
428 paint.setColor(SK_ColorRED);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000429 canvas->drawCircle(SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/3), paint);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000430 paint.setColor(SK_ColorBLACK);
431 paint.setTextSize(SkIntToScalar(kBitmapSize/3));
432 canvas->drawText("Picture", 7, SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/4), paint);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000433}
434
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000435DEF_TEST(Serialization, reporter) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000436 // Test matrix serialization
437 {
438 SkMatrix matrix = SkMatrix::I();
439 TestObjectSerialization(&matrix, reporter);
caseq26337e92014-06-30 12:14:52 -0700440 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000441
442 // Test path serialization
443 {
444 SkPath path;
445 TestObjectSerialization(&path, reporter);
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000446 }
sugoi@google.com305f78e2013-11-04 16:18:15 +0000447
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000448 // Test region serialization
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000449 {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000450 SkRegion region;
451 TestObjectSerialization(&region, reporter);
452 }
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000453
senorblanco0f7197b2014-09-24 11:09:38 -0700454 // Test xfermode serialization
455 {
456 TestXfermodeSerialization(reporter);
457 }
458
senorblanco91c395a2014-09-25 15:51:35 -0700459 // Test color filter serialization
460 {
461 TestColorFilterSerialization(reporter);
462 }
463
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000464 // Test string serialization
465 {
466 SkString string("string");
467 TestObjectSerializationNoAlign<SkString, false>(&string, reporter);
468 TestObjectSerializationNoAlign<SkString, true>(&string, reporter);
469 }
470
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000471 // Test rrect serialization
472 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000473 // SkRRect does not initialize anything.
474 // An uninitialized SkRRect can be serialized,
475 // but will branch on uninitialized data when deserialized.
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000476 SkRRect rrect;
rmistry@google.comd6bab022013-12-02 13:50:38 +0000477 SkRect rect = SkRect::MakeXYWH(1, 2, 20, 30);
478 SkVector corners[4] = { {1, 2}, {2, 3}, {3,4}, {4,5} };
479 rrect.setRectRadii(rect, corners);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000480 TestAlignment(&rrect, reporter);
481 }
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000482
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000483 // Test readByteArray
484 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000485 unsigned char data[kArraySize] = { 1, 2, 3 };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000486 TestArraySerialization(data, reporter);
487 }
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000488
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000489 // Test readColorArray
490 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000491 SkColor data[kArraySize] = { SK_ColorBLACK, SK_ColorWHITE, SK_ColorRED };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000492 TestArraySerialization(data, reporter);
493 }
494
495 // Test readIntArray
496 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000497 int32_t data[kArraySize] = { 1, 2, 4, 8 };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000498 TestArraySerialization(data, reporter);
499 }
500
501 // Test readPointArray
502 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000503 SkPoint data[kArraySize] = { {6, 7}, {42, 128} };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000504 TestArraySerialization(data, reporter);
505 }
506
507 // Test readScalarArray
508 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000509 SkScalar data[kArraySize] = { SK_Scalar1, SK_ScalarHalf, SK_ScalarMax };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000510 TestArraySerialization(data, reporter);
commit-bot@chromium.org02512882013-10-31 18:37:50 +0000511 }
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000512
513 // Test invalid deserializations
514 {
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000515 SkImageInfo info = SkImageInfo::MakeN32Premul(kBitmapSize, kBitmapSize);
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000516
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000517 SkBitmap validBitmap;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000518 validBitmap.setInfo(info);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000519
520 // Create a bitmap with a really large height
521 SkBitmap invalidBitmap;
reede5ea5002014-09-03 11:54:58 -0700522 invalidBitmap.setInfo(info.makeWH(info.width(), 1000000000));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000523
524 // The deserialization should succeed, and the rendering shouldn't crash,
525 // even when the device fails to initialize, due to its size
526 TestBitmapSerialization(validBitmap, invalidBitmap, true, reporter);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000527 }
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000528
529 // Test simple SkPicture serialization
530 {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000531 SkPictureRecorder recorder;
reed84825042014-09-02 12:50:45 -0700532 draw_something(recorder.beginRecording(SkIntToScalar(kBitmapSize),
533 SkIntToScalar(kBitmapSize),
halcanary96fcdcc2015-08-27 07:41:13 -0700534 nullptr, 0));
reedca2622b2016-03-18 07:25:55 -0700535 sk_sp<SkPicture> pict(recorder.finishRecordingAsPicture());
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000536
537 // Serialize picture
brianosmanfad98562016-05-04 11:06:28 -0700538 SkBinaryWriteBuffer writer;
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000539 pict->flatten(writer);
540 size_t size = writer.bytesWritten();
commit-bot@chromium.org1e7ee992014-03-14 21:22:22 +0000541 SkAutoTMalloc<unsigned char> data(size);
542 writer.writeToMemory(static_cast<void*>(data.get()));
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000543
544 // Deserialize picture
commit-bot@chromium.org1e7ee992014-03-14 21:22:22 +0000545 SkValidatingReadBuffer reader(static_cast<void*>(data.get()), size);
reedca2622b2016-03-18 07:25:55 -0700546 sk_sp<SkPicture> readPict(SkPicture::MakeFromBuffer(reader));
bsalomon49f085d2014-09-05 13:34:00 -0700547 REPORTER_ASSERT(reporter, readPict.get());
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000548 }
caseq26337e92014-06-30 12:14:52 -0700549
550 TestPictureTypefaceSerialization(reporter);
dvonbeck8811e402016-06-16 12:39:25 -0700551
552 // Test SkLightingShader/NormalMapSource serialization
553 {
554 const int kTexSize = 2;
555
556 SkLights::Builder builder;
557
558 builder.add(SkLights::Light(SkColor3f::Make(1.0f, 1.0f, 1.0f),
559 SkVector3::Make(1.0f, 0.0f, 0.0f)));
560 builder.add(SkLights::Light(SkColor3f::Make(0.2f, 0.2f, 0.2f)));
561
562 sk_sp<SkLights> fLights = builder.finish();
563
564 SkBitmap diffuse = sk_tool_utils::create_checkerboard_bitmap(
565 kTexSize, kTexSize,
566 sk_tool_utils::color_to_565(0x0),
567 sk_tool_utils::color_to_565(0xFF804020),
568 8);
569
570 SkRect bitmapBounds = SkRect::MakeIWH(diffuse.width(), diffuse.height());
571
572 SkMatrix matrix;
573 SkRect r = SkRect::MakeWH(SkIntToScalar(kTexSize), SkIntToScalar(kTexSize));
574 matrix.setRectToRect(bitmapBounds, r, SkMatrix::kFill_ScaleToFit);
575
576 SkVector invNormRotation = { SkScalarSqrt(0.3f), SkScalarSqrt(0.7f) };
577 SkBitmap normals;
578 normals.allocN32Pixels(kTexSize, kTexSize);
579
580 sk_tool_utils::create_frustum_normal_map(&normals, SkIRect::MakeWH(kTexSize, kTexSize));
581 sk_sp<SkShader> lightingShader = SkLightingShader::Make(diffuse, normals, fLights,
582 invNormRotation, &matrix, &matrix);
583
584 SkAutoTUnref<SkShader>(TestFlattenableSerialization(lightingShader.get(), true, reporter));
585 // TODO test equality?
586
587 }
commit-bot@chromium.org02512882013-10-31 18:37:50 +0000588}
reedf70b5312016-03-04 16:36:20 -0800589
590///////////////////////////////////////////////////////////////////////////////////////////////////
591#include "SkAnnotation.h"
592
reedca2622b2016-03-18 07:25:55 -0700593static sk_sp<SkPicture> copy_picture_via_serialization(SkPicture* src) {
reedf70b5312016-03-04 16:36:20 -0800594 SkDynamicMemoryWStream wstream;
595 src->serialize(&wstream);
596 SkAutoTDelete<SkStreamAsset> rstream(wstream.detachAsStream());
reedca2622b2016-03-18 07:25:55 -0700597 return SkPicture::MakeFromStream(rstream);
reedf70b5312016-03-04 16:36:20 -0800598}
599
600struct AnnotationRec {
601 const SkRect fRect;
602 const char* fKey;
603 SkData* fValue;
604};
605
606class TestAnnotationCanvas : public SkCanvas {
607 skiatest::Reporter* fReporter;
608 const AnnotationRec* fRec;
609 int fCount;
610 int fCurrIndex;
611
612public:
613 TestAnnotationCanvas(skiatest::Reporter* reporter, const AnnotationRec rec[], int count)
614 : SkCanvas(100, 100)
615 , fReporter(reporter)
616 , fRec(rec)
617 , fCount(count)
618 , fCurrIndex(0)
619 {}
620
621 ~TestAnnotationCanvas() {
622 REPORTER_ASSERT(fReporter, fCount == fCurrIndex);
623 }
624
625protected:
626 void onDrawAnnotation(const SkRect& rect, const char key[], SkData* value) {
627 REPORTER_ASSERT(fReporter, fCurrIndex < fCount);
628 REPORTER_ASSERT(fReporter, rect == fRec[fCurrIndex].fRect);
629 REPORTER_ASSERT(fReporter, !strcmp(key, fRec[fCurrIndex].fKey));
630 REPORTER_ASSERT(fReporter, value->equals(fRec[fCurrIndex].fValue));
631 fCurrIndex += 1;
632 }
633};
634
635/*
636 * Test the 3 annotation types by recording them into a picture, serializing, and then playing
637 * them back into another canvas.
638 */
639DEF_TEST(Annotations, reporter) {
640 SkPictureRecorder recorder;
641 SkCanvas* recordingCanvas = recorder.beginRecording(SkRect::MakeWH(100, 100));
halcanary9d524f22016-03-29 09:03:52 -0700642
reedf70b5312016-03-04 16:36:20 -0800643 const char* str0 = "rect-with-url";
644 const SkRect r0 = SkRect::MakeWH(10, 10);
645 SkAutoTUnref<SkData> d0(SkData::NewWithCString(str0));
646 SkAnnotateRectWithURL(recordingCanvas, r0, d0);
halcanary9d524f22016-03-29 09:03:52 -0700647
reedf70b5312016-03-04 16:36:20 -0800648 const char* str1 = "named-destination";
649 const SkRect r1 = SkRect::MakeXYWH(5, 5, 0, 0); // collapsed to a point
650 SkAutoTUnref<SkData> d1(SkData::NewWithCString(str1));
651 SkAnnotateNamedDestination(recordingCanvas, {r1.x(), r1.y()}, d1);
halcanary9d524f22016-03-29 09:03:52 -0700652
reedf70b5312016-03-04 16:36:20 -0800653 const char* str2 = "link-to-destination";
654 const SkRect r2 = SkRect::MakeXYWH(20, 20, 5, 6);
655 SkAutoTUnref<SkData> d2(SkData::NewWithCString(str2));
656 SkAnnotateLinkToDestination(recordingCanvas, r2, d2);
657
658 const AnnotationRec recs[] = {
659 { r0, SkAnnotationKeys::URL_Key(), d0 },
660 { r1, SkAnnotationKeys::Define_Named_Dest_Key(), d1 },
661 { r2, SkAnnotationKeys::Link_Named_Dest_Key(), d2 },
662 };
663
reedca2622b2016-03-18 07:25:55 -0700664 sk_sp<SkPicture> pict0(recorder.finishRecordingAsPicture());
665 sk_sp<SkPicture> pict1(copy_picture_via_serialization(pict0.get()));
reedf70b5312016-03-04 16:36:20 -0800666
667 TestAnnotationCanvas canvas(reporter, recs, SK_ARRAY_COUNT(recs));
668 canvas.drawPicture(pict1);
669}