blob: 3e2f5542b0154250b2476d0d11ad4304ff1475e0 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkCanvas.h"
9#include "include/core/SkImage.h"
10#include "include/core/SkMallocPixelRef.h"
11#include "include/core/SkPictureRecorder.h"
12#include "include/core/SkTextBlob.h"
13#include "include/core/SkTypeface.h"
14#include "include/effects/SkDashPathEffect.h"
15#include "include/effects/SkImageSource.h"
16#include "include/effects/SkTableColorFilter.h"
17#include "include/effects/SkXfermodeImageFilter.h"
18#include "include/private/SkFixed.h"
19#include "include/private/SkTemplates.h"
20#include "src/core/SkAnnotationKeys.h"
21#include "src/core/SkFontDescriptor.h"
22#include "src/core/SkMakeUnique.h"
23#include "src/core/SkMatrixPriv.h"
24#include "src/core/SkNormalSource.h"
25#include "src/core/SkOSFile.h"
26#include "src/core/SkPicturePriv.h"
27#include "src/core/SkReadBuffer.h"
28#include "src/core/SkWriteBuffer.h"
29#include "src/shaders/SkLightingShader.h"
30#include "src/shaders/SkShaderBase.h"
31#include "tests/Test.h"
32#include "tools/Resources.h"
33#include "tools/ToolUtils.h"
commit-bot@chromium.org02512882013-10-31 18:37:50 +000034
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000035static const uint32_t kArraySize = 64;
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +000036static const int kBitmapSize = 256;
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000037
Cary Clark6d6d6032017-10-20 12:14:33 -040038class SerializationTest {
39public:
40
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000041template<typename T>
42static void TestAlignment(T* testObj, skiatest::Reporter* reporter) {
43 // Test memory read/write functions directly
44 unsigned char dataWritten[1024];
45 size_t bytesWrittenToMemory = testObj->writeToMemory(dataWritten);
46 REPORTER_ASSERT(reporter, SkAlign4(bytesWrittenToMemory) == bytesWrittenToMemory);
47 size_t bytesReadFromMemory = testObj->readFromMemory(dataWritten, bytesWrittenToMemory);
48 REPORTER_ASSERT(reporter, SkAlign4(bytesReadFromMemory) == bytesReadFromMemory);
49}
Cary Clark6d6d6032017-10-20 12:14:33 -040050};
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000051
52template<typename T> struct SerializationUtils {
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +000053 // Generic case for flattenables
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000054 static void Write(SkWriteBuffer& writer, const T* flattenable) {
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +000055 writer.writeFlattenable(flattenable);
56 }
Mike Reedfadbfcd2017-12-06 16:09:20 -050057 static void Read(SkReadBuffer& reader, T** flattenable) {
mtklein3b375452016-04-04 14:57:19 -070058 *flattenable = (T*)reader.readFlattenable(T::GetFlattenableType());
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +000059 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000060};
61
62template<> struct SerializationUtils<SkMatrix> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000063 static void Write(SkWriteBuffer& writer, const SkMatrix* matrix) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000064 writer.writeMatrix(*matrix);
65 }
Mike Reedfadbfcd2017-12-06 16:09:20 -050066 static void Read(SkReadBuffer& reader, SkMatrix* matrix) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000067 reader.readMatrix(matrix);
68 }
69};
70
71template<> struct SerializationUtils<SkPath> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000072 static void Write(SkWriteBuffer& writer, const SkPath* path) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000073 writer.writePath(*path);
74 }
Mike Reedfadbfcd2017-12-06 16:09:20 -050075 static void Read(SkReadBuffer& reader, SkPath* path) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000076 reader.readPath(path);
77 }
78};
79
80template<> struct SerializationUtils<SkRegion> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000081 static void Write(SkWriteBuffer& writer, const SkRegion* region) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000082 writer.writeRegion(*region);
83 }
Mike Reedfadbfcd2017-12-06 16:09:20 -050084 static void Read(SkReadBuffer& reader, SkRegion* region) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000085 reader.readRegion(region);
86 }
87};
88
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +000089template<> struct SerializationUtils<SkString> {
90 static void Write(SkWriteBuffer& writer, const SkString* string) {
91 writer.writeString(string->c_str());
92 }
Mike Reedfadbfcd2017-12-06 16:09:20 -050093 static void Read(SkReadBuffer& reader, SkString* string) {
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +000094 reader.readString(string);
95 }
96};
97
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000098template<> struct SerializationUtils<unsigned char> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000099 static void Write(SkWriteBuffer& writer, unsigned char* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +0000100 writer.writeByteArray(data, arraySize);
sugoi@google.com305f78e2013-11-04 16:18:15 +0000101 }
Mike Reedfadbfcd2017-12-06 16:09:20 -0500102 static bool Read(SkReadBuffer& reader, unsigned char* data, uint32_t arraySize) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000103 return reader.readByteArray(data, arraySize);
104 }
105};
commit-bot@chromium.org02512882013-10-31 18:37:50 +0000106
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000107template<> struct SerializationUtils<SkColor> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000108 static void Write(SkWriteBuffer& writer, SkColor* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +0000109 writer.writeColorArray(data, arraySize);
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000110 }
Mike Reedfadbfcd2017-12-06 16:09:20 -0500111 static bool Read(SkReadBuffer& reader, SkColor* data, uint32_t arraySize) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000112 return reader.readColorArray(data, arraySize);
113 }
114};
sugoi@google.com305f78e2013-11-04 16:18:15 +0000115
brianosman97bbf822016-09-25 13:15:58 -0700116template<> struct SerializationUtils<SkColor4f> {
117 static void Write(SkWriteBuffer& writer, SkColor4f* data, uint32_t arraySize) {
118 writer.writeColor4fArray(data, arraySize);
119 }
Mike Reedfadbfcd2017-12-06 16:09:20 -0500120 static bool Read(SkReadBuffer& reader, SkColor4f* data, uint32_t arraySize) {
brianosman97bbf822016-09-25 13:15:58 -0700121 return reader.readColor4fArray(data, arraySize);
122 }
123};
124
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000125template<> struct SerializationUtils<int32_t> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000126 static void Write(SkWriteBuffer& writer, int32_t* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +0000127 writer.writeIntArray(data, arraySize);
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000128 }
Mike Reedfadbfcd2017-12-06 16:09:20 -0500129 static bool Read(SkReadBuffer& reader, int32_t* data, uint32_t arraySize) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000130 return reader.readIntArray(data, arraySize);
131 }
132};
sugoi@google.com305f78e2013-11-04 16:18:15 +0000133
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000134template<> struct SerializationUtils<SkPoint> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000135 static void Write(SkWriteBuffer& writer, SkPoint* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +0000136 writer.writePointArray(data, arraySize);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000137 }
Mike Reedfadbfcd2017-12-06 16:09:20 -0500138 static bool Read(SkReadBuffer& reader, SkPoint* data, uint32_t arraySize) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000139 return reader.readPointArray(data, arraySize);
140 }
141};
reed@google.com12a23862013-11-04 21:35:55 +0000142
Michael Ludwigea241402018-08-22 09:26:33 -0400143template<> struct SerializationUtils<SkPoint3> {
144 static void Write(SkWriteBuffer& writer, const SkPoint3* data) {
145 writer.writePoint3(*data);
146 }
147 static void Read(SkReadBuffer& reader, SkPoint3* data) {
148 reader.readPoint3(data);
149 }
150};
151
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000152template<> struct SerializationUtils<SkScalar> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000153 static void Write(SkWriteBuffer& writer, SkScalar* data, uint32_t arraySize) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000154 writer.writeScalarArray(data, arraySize);
155 }
Mike Reedfadbfcd2017-12-06 16:09:20 -0500156 static bool Read(SkReadBuffer& reader, SkScalar* data, uint32_t arraySize) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000157 return reader.readScalarArray(data, arraySize);
158 }
159};
reed@google.com12a23862013-11-04 21:35:55 +0000160
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000161template<typename T, bool testInvalid> struct SerializationTestUtils {
162 static void InvalidateData(unsigned char* data) {}
163};
164
165template<> struct SerializationTestUtils<SkString, true> {
166 static void InvalidateData(unsigned char* data) {
167 data[3] |= 0x80; // Reverse sign of 1st integer
168 }
169};
170
171template<typename T, bool testInvalid>
172static void TestObjectSerializationNoAlign(T* testObj, skiatest::Reporter* reporter) {
brianosmanfad98562016-05-04 11:06:28 -0700173 SkBinaryWriteBuffer writer;
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000174 SerializationUtils<T>::Write(writer, testObj);
175 size_t bytesWritten = writer.bytesWritten();
176 REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten);
reed@google.com12a23862013-11-04 21:35:55 +0000177
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000178 unsigned char dataWritten[1024];
179 writer.writeToMemory(dataWritten);
180
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000181 SerializationTestUtils<T, testInvalid>::InvalidateData(dataWritten);
182
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000183 // Make sure this fails when it should (test with smaller size, but still multiple of 4)
Mike Reedfadbfcd2017-12-06 16:09:20 -0500184 SkReadBuffer buffer(dataWritten, bytesWritten - 4);
commit-bot@chromium.org8f457e32013-11-08 19:22:57 +0000185 T obj;
186 SerializationUtils<T>::Read(buffer, &obj);
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000187 REPORTER_ASSERT(reporter, !buffer.isValid());
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000188
189 // Make sure this succeeds when it should
Mike Reedfadbfcd2017-12-06 16:09:20 -0500190 SkReadBuffer buffer2(dataWritten, bytesWritten);
Robert Phillipsb2526042016-09-26 09:00:36 -0400191 size_t offsetBefore = buffer2.offset();
commit-bot@chromium.org8f457e32013-11-08 19:22:57 +0000192 T obj2;
193 SerializationUtils<T>::Read(buffer2, &obj2);
Robert Phillipsb2526042016-09-26 09:00:36 -0400194 size_t offsetAfter = buffer2.offset();
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000195 // This should have succeeded, since there are enough bytes to read this
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000196 REPORTER_ASSERT(reporter, buffer2.isValid() == !testInvalid);
197 // Note: This following test should always succeed, regardless of whether the buffer is valid,
198 // 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 -0400199 REPORTER_ASSERT(reporter, offsetAfter - offsetBefore == bytesWritten);
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000200}
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000201
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000202template<typename T>
203static void TestObjectSerialization(T* testObj, skiatest::Reporter* reporter) {
204 TestObjectSerializationNoAlign<T, false>(testObj, reporter);
Cary Clark6d6d6032017-10-20 12:14:33 -0400205 SerializationTest::TestAlignment(testObj, reporter);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000206}
207
208template<typename T>
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000209static T* TestFlattenableSerialization(T* testObj, bool shouldSucceed,
210 skiatest::Reporter* reporter) {
brianosmanfad98562016-05-04 11:06:28 -0700211 SkBinaryWriteBuffer writer;
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000212 SerializationUtils<T>::Write(writer, testObj);
213 size_t bytesWritten = writer.bytesWritten();
214 REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten);
215
dvonbeck8811e402016-06-16 12:39:25 -0700216 SkASSERT(bytesWritten <= 4096);
senorblanco91c395a2014-09-25 15:51:35 -0700217 unsigned char dataWritten[4096];
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000218 writer.writeToMemory(dataWritten);
219
220 // Make sure this fails when it should (test with smaller size, but still multiple of 4)
Mike Reedfadbfcd2017-12-06 16:09:20 -0500221 SkReadBuffer buffer(dataWritten, bytesWritten - 4);
halcanary96fcdcc2015-08-27 07:41:13 -0700222 T* obj = nullptr;
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000223 SerializationUtils<T>::Read(buffer, &obj);
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000224 REPORTER_ASSERT(reporter, !buffer.isValid());
halcanary96fcdcc2015-08-27 07:41:13 -0700225 REPORTER_ASSERT(reporter, nullptr == obj);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000226
227 // Make sure this succeeds when it should
Mike Reedfadbfcd2017-12-06 16:09:20 -0500228 SkReadBuffer buffer2(dataWritten, bytesWritten);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000229 const unsigned char* peekBefore = static_cast<const unsigned char*>(buffer2.skip(0));
halcanary96fcdcc2015-08-27 07:41:13 -0700230 T* obj2 = nullptr;
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000231 SerializationUtils<T>::Read(buffer2, &obj2);
232 const unsigned char* peekAfter = static_cast<const unsigned char*>(buffer2.skip(0));
233 if (shouldSucceed) {
234 // This should have succeeded, since there are enough bytes to read this
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000235 REPORTER_ASSERT(reporter, buffer2.isValid());
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000236 REPORTER_ASSERT(reporter, static_cast<size_t>(peekAfter - peekBefore) == bytesWritten);
bsalomon49f085d2014-09-05 13:34:00 -0700237 REPORTER_ASSERT(reporter, obj2);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000238 } else {
239 // If the deserialization was supposed to fail, make sure it did
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000240 REPORTER_ASSERT(reporter, !buffer.isValid());
halcanary96fcdcc2015-08-27 07:41:13 -0700241 REPORTER_ASSERT(reporter, nullptr == obj2);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000242 }
243
244 return obj2; // Return object to perform further validity tests on it
245}
246
247template<typename T>
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000248static void TestArraySerialization(T* data, skiatest::Reporter* reporter) {
brianosmanfad98562016-05-04 11:06:28 -0700249 SkBinaryWriteBuffer writer;
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000250 SerializationUtils<T>::Write(writer, data, kArraySize);
251 size_t bytesWritten = writer.bytesWritten();
252 // This should write the length (in 4 bytes) and the array
253 REPORTER_ASSERT(reporter, (4 + kArraySize * sizeof(T)) == bytesWritten);
254
brianosman97bbf822016-09-25 13:15:58 -0700255 unsigned char dataWritten[2048];
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000256 writer.writeToMemory(dataWritten);
257
258 // Make sure this fails when it should
Mike Reedfadbfcd2017-12-06 16:09:20 -0500259 SkReadBuffer buffer(dataWritten, bytesWritten);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000260 T dataRead[kArraySize];
261 bool success = SerializationUtils<T>::Read(buffer, dataRead, kArraySize / 2);
262 // This should have failed, since the provided size was too small
263 REPORTER_ASSERT(reporter, !success);
264
265 // Make sure this succeeds when it should
Mike Reedfadbfcd2017-12-06 16:09:20 -0500266 SkReadBuffer buffer2(dataWritten, bytesWritten);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000267 success = SerializationUtils<T>::Read(buffer2, dataRead, kArraySize);
268 // This should have succeeded, since there are enough bytes to read this
269 REPORTER_ASSERT(reporter, success);
270}
271
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000272static void TestBitmapSerialization(const SkBitmap& validBitmap,
273 const SkBitmap& invalidBitmap,
274 bool shouldSucceed,
275 skiatest::Reporter* reporter) {
reed9ce9d672016-03-17 10:51:11 -0700276 sk_sp<SkImage> validImage(SkImage::MakeFromBitmap(validBitmap));
robertphillips549c8992016-04-01 09:28:51 -0700277 sk_sp<SkImageFilter> validBitmapSource(SkImageSource::Make(std::move(validImage)));
reed9ce9d672016-03-17 10:51:11 -0700278 sk_sp<SkImage> invalidImage(SkImage::MakeFromBitmap(invalidBitmap));
robertphillips549c8992016-04-01 09:28:51 -0700279 sk_sp<SkImageFilter> invalidBitmapSource(SkImageSource::Make(std::move(invalidImage)));
reedcfb6bdf2016-03-29 11:32:50 -0700280 sk_sp<SkImageFilter> xfermodeImageFilter(
reed374772b2016-10-05 17:33:02 -0700281 SkXfermodeImageFilter::Make(SkBlendMode::kSrcOver,
robertphillips8c0326d2016-04-05 12:48:34 -0700282 std::move(invalidBitmapSource),
283 std::move(validBitmapSource), nullptr));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000284
Mike Reed5e257172016-11-01 11:22:05 -0400285 sk_sp<SkImageFilter> deserializedFilter(
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000286 TestFlattenableSerialization<SkImageFilter>(
reedcfb6bdf2016-03-29 11:32:50 -0700287 xfermodeImageFilter.get(), shouldSucceed, reporter));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000288
289 // Try to render a small bitmap using the invalid deserialized filter
290 // to make sure we don't crash while trying to render it
291 if (shouldSucceed) {
292 SkBitmap bitmap;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000293 bitmap.allocN32Pixels(24, 24);
294 SkCanvas canvas(bitmap);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000295 canvas.clear(0x00000000);
296 SkPaint paint;
297 paint.setImageFilter(deserializedFilter);
298 canvas.clipRect(SkRect::MakeXYWH(0, 0, SkIntToScalar(24), SkIntToScalar(24)));
299 canvas.drawBitmap(bitmap, 0, 0, &paint);
300 }
301}
302
senorblanco91c395a2014-09-25 15:51:35 -0700303static void TestColorFilterSerialization(skiatest::Reporter* reporter) {
304 uint8_t table[256];
305 for (int i = 0; i < 256; ++i) {
306 table[i] = (i * 41) % 256;
307 }
reedd053ce92016-03-22 10:17:23 -0700308 auto colorFilter(SkTableColorFilter::Make(table));
Hal Canary342b7ac2016-11-04 11:49:42 -0400309 sk_sp<SkColorFilter> copy(
senorblanco91c395a2014-09-25 15:51:35 -0700310 TestFlattenableSerialization<SkColorFilter>(colorFilter.get(), true, reporter));
311}
312
caseq26337e92014-06-30 12:14:52 -0700313static SkBitmap draw_picture(SkPicture& picture) {
314 SkBitmap bitmap;
halcanary9d524f22016-03-29 09:03:52 -0700315 bitmap.allocN32Pixels(SkScalarCeilToInt(picture.cullRect().width()),
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700316 SkScalarCeilToInt(picture.cullRect().height()));
caseq26337e92014-06-30 12:14:52 -0700317 SkCanvas canvas(bitmap);
robertphillipsc5ba71d2014-09-04 08:42:50 -0700318 picture.playback(&canvas);
caseq26337e92014-06-30 12:14:52 -0700319 return bitmap;
320}
321
322static void compare_bitmaps(skiatest::Reporter* reporter,
323 const SkBitmap& b1, const SkBitmap& b2) {
324 REPORTER_ASSERT(reporter, b1.width() == b2.width());
325 REPORTER_ASSERT(reporter, b1.height() == b2.height());
caseq26337e92014-06-30 12:14:52 -0700326
327 if ((b1.width() != b2.width()) ||
328 (b1.height() != b2.height())) {
329 return;
330 }
331
332 int pixelErrors = 0;
333 for (int y = 0; y < b2.height(); ++y) {
334 for (int x = 0; x < b2.width(); ++x) {
335 if (b1.getColor(x, y) != b2.getColor(x, y))
336 ++pixelErrors;
337 }
338 }
339 REPORTER_ASSERT(reporter, 0 == pixelErrors);
340}
bungeman13b9c952016-05-12 10:09:30 -0700341static void serialize_and_compare_typeface(sk_sp<SkTypeface> typeface, const char* text,
bungeman41868fe2015-05-20 09:21:04 -0700342 skiatest::Reporter* reporter)
343{
Hal Canary3560ea72019-01-08 13:01:58 -0500344 // Create a font with the typeface.
caseq26337e92014-06-30 12:14:52 -0700345 SkPaint paint;
346 paint.setColor(SK_ColorGRAY);
Hal Canary3560ea72019-01-08 13:01:58 -0500347 SkFont font(std::move(typeface), 30);
caseq26337e92014-06-30 12:14:52 -0700348
349 // Paint some text.
350 SkPictureRecorder recorder;
351 SkIRect canvasRect = SkIRect::MakeWH(kBitmapSize, kBitmapSize);
halcanary9d524f22016-03-29 09:03:52 -0700352 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(canvasRect.width()),
353 SkIntToScalar(canvasRect.height()),
halcanary96fcdcc2015-08-27 07:41:13 -0700354 nullptr, 0);
caseq26337e92014-06-30 12:14:52 -0700355 canvas->drawColor(SK_ColorWHITE);
Hal Canary3560ea72019-01-08 13:01:58 -0500356 canvas->drawString(text, 24, 32, font, paint);
reedca2622b2016-03-18 07:25:55 -0700357 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
caseq26337e92014-06-30 12:14:52 -0700358
359 // Serlialize picture and create its clone from stream.
360 SkDynamicMemoryWStream stream;
361 picture->serialize(&stream);
Ben Wagner145dbcd2016-11-03 14:40:50 -0400362 std::unique_ptr<SkStream> inputStream(stream.detachAsStream());
reedca2622b2016-03-18 07:25:55 -0700363 sk_sp<SkPicture> loadedPicture(SkPicture::MakeFromStream(inputStream.get()));
caseq26337e92014-06-30 12:14:52 -0700364
365 // Draw both original and clone picture and compare bitmaps -- they should be identical.
366 SkBitmap origBitmap = draw_picture(*picture);
367 SkBitmap destBitmap = draw_picture(*loadedPicture);
368 compare_bitmaps(reporter, origBitmap, destBitmap);
369}
370
bungeman41868fe2015-05-20 09:21:04 -0700371static void TestPictureTypefaceSerialization(skiatest::Reporter* reporter) {
372 {
373 // Load typeface from file to test CreateFromFile with index.
Mike Reed271d1d92018-09-03 21:10:10 -0400374 auto typeface = MakeResourceAsTypeface("fonts/test.ttc", 1);
bungeman41868fe2015-05-20 09:21:04 -0700375 if (!typeface) {
halcanary7d571242016-02-24 17:59:16 -0800376 INFOF(reporter, "Could not run fontstream test because test.ttc not found.");
bungeman41868fe2015-05-20 09:21:04 -0700377 } else {
bungeman13b9c952016-05-12 10:09:30 -0700378 serialize_and_compare_typeface(std::move(typeface), "A!", reporter);
bungeman41868fe2015-05-20 09:21:04 -0700379 }
380 }
381
382 {
383 // Load typeface as stream to create with axis settings.
Mike Reed0933bc92017-12-09 01:27:41 +0000384 std::unique_ptr<SkStreamAsset> distortable(GetResourceAsStream("fonts/Distortable.ttf"));
bungeman41868fe2015-05-20 09:21:04 -0700385 if (!distortable) {
halcanary7d571242016-02-24 17:59:16 -0800386 INFOF(reporter, "Could not run fontstream test because Distortable.ttf not found.");
bungeman41868fe2015-05-20 09:21:04 -0700387 } else {
388 SkFixed axis = SK_FixedSqrt2;
bungeman13b9c952016-05-12 10:09:30 -0700389 sk_sp<SkTypeface> typeface(SkTypeface::MakeFromFontData(
bungemanf93d7112016-09-16 06:24:20 -0700390 skstd::make_unique<SkFontData>(std::move(distortable), 0, &axis, 1)));
bungeman41868fe2015-05-20 09:21:04 -0700391 if (!typeface) {
halcanary7d571242016-02-24 17:59:16 -0800392 INFOF(reporter, "Could not run fontstream test because Distortable.ttf not created.");
bungeman41868fe2015-05-20 09:21:04 -0700393 } else {
Hal Canary3560ea72019-01-08 13:01:58 -0500394 serialize_and_compare_typeface(std::move(typeface), "ab", reporter);
bungeman41868fe2015-05-20 09:21:04 -0700395 }
396 }
397 }
398}
399
reed84825042014-09-02 12:50:45 -0700400static void setup_bitmap_for_canvas(SkBitmap* bitmap) {
401 bitmap->allocN32Pixels(kBitmapSize, kBitmapSize);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000402}
403
reed84825042014-09-02 12:50:45 -0700404static void make_checkerboard_bitmap(SkBitmap& bitmap) {
405 setup_bitmap_for_canvas(&bitmap);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000406
407 SkCanvas canvas(bitmap);
408 canvas.clear(0x00000000);
409 SkPaint darkPaint;
410 darkPaint.setColor(0xFF804020);
411 SkPaint lightPaint;
412 lightPaint.setColor(0xFF244484);
413 const int i = kBitmapSize / 8;
414 const SkScalar f = SkIntToScalar(i);
415 for (int y = 0; y < kBitmapSize; y += i) {
416 for (int x = 0; x < kBitmapSize; x += i) {
417 canvas.save();
418 canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
419 canvas.drawRect(SkRect::MakeXYWH(0, 0, f, f), darkPaint);
420 canvas.drawRect(SkRect::MakeXYWH(f, 0, f, f), lightPaint);
421 canvas.drawRect(SkRect::MakeXYWH(0, f, f, f), lightPaint);
422 canvas.drawRect(SkRect::MakeXYWH(f, f, f, f), darkPaint);
423 canvas.restore();
424 }
425 }
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000426}
427
reed84825042014-09-02 12:50:45 -0700428static void draw_something(SkCanvas* canvas) {
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000429 SkPaint paint;
430 SkBitmap bitmap;
reed84825042014-09-02 12:50:45 -0700431 make_checkerboard_bitmap(bitmap);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000432
433 canvas->save();
434 canvas->scale(0.5f, 0.5f);
halcanary96fcdcc2015-08-27 07:41:13 -0700435 canvas->drawBitmap(bitmap, 0, 0, nullptr);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000436 canvas->restore();
437
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000438 paint.setAntiAlias(true);
439
440 paint.setColor(SK_ColorRED);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000441 canvas->drawCircle(SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/3), paint);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000442 paint.setColor(SK_ColorBLACK);
Mike Reed1af9b482019-01-07 11:01:57 -0500443
444 SkFont font;
445 font.setSize(kBitmapSize/3);
446 canvas->drawString("Picture", SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/4), font, paint);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000447}
448
Ben Wagner7825d492019-07-10 16:27:34 -0400449static sk_sp<SkImage> render(const SkPicture& p) {
450 auto surf = SkSurface::MakeRasterN32Premul(SkScalarRoundToInt(p.cullRect().width()),
451 SkScalarRoundToInt(p.cullRect().height()));
452 if (!surf) {
453 return nullptr; // bounds are empty?
454 }
455 surf->getCanvas()->clear(SK_ColorWHITE);
456 p.playback(surf->getCanvas());
457 return surf->makeImageSnapshot();
458}
459
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000460DEF_TEST(Serialization, reporter) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000461 // Test matrix serialization
462 {
463 SkMatrix matrix = SkMatrix::I();
464 TestObjectSerialization(&matrix, reporter);
caseq26337e92014-06-30 12:14:52 -0700465 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000466
Michael Ludwigea241402018-08-22 09:26:33 -0400467 // Test point3 serialization
468 {
469 SkPoint3 point;
470 TestObjectSerializationNoAlign<SkPoint3, false>(&point, reporter);
471 }
472
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000473 // Test path serialization
474 {
475 SkPath path;
476 TestObjectSerialization(&path, reporter);
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000477 }
sugoi@google.com305f78e2013-11-04 16:18:15 +0000478
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000479 // Test region serialization
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000480 {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000481 SkRegion region;
482 TestObjectSerialization(&region, reporter);
483 }
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000484
senorblanco91c395a2014-09-25 15:51:35 -0700485 // Test color filter serialization
486 {
487 TestColorFilterSerialization(reporter);
488 }
489
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000490 // Test string serialization
491 {
492 SkString string("string");
493 TestObjectSerializationNoAlign<SkString, false>(&string, reporter);
494 TestObjectSerializationNoAlign<SkString, true>(&string, reporter);
495 }
496
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000497 // Test rrect serialization
498 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000499 // SkRRect does not initialize anything.
500 // An uninitialized SkRRect can be serialized,
501 // but will branch on uninitialized data when deserialized.
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000502 SkRRect rrect;
rmistry@google.comd6bab022013-12-02 13:50:38 +0000503 SkRect rect = SkRect::MakeXYWH(1, 2, 20, 30);
504 SkVector corners[4] = { {1, 2}, {2, 3}, {3,4}, {4,5} };
505 rrect.setRectRadii(rect, corners);
Cary Clark6d6d6032017-10-20 12:14:33 -0400506 SerializationTest::TestAlignment(&rrect, reporter);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000507 }
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000508
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000509 // Test readByteArray
510 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000511 unsigned char data[kArraySize] = { 1, 2, 3 };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000512 TestArraySerialization(data, reporter);
513 }
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000514
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000515 // Test readColorArray
516 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000517 SkColor data[kArraySize] = { SK_ColorBLACK, SK_ColorWHITE, SK_ColorRED };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000518 TestArraySerialization(data, reporter);
519 }
520
brianosman97bbf822016-09-25 13:15:58 -0700521 // Test readColor4fArray
522 {
523 SkColor4f data[kArraySize] = {
524 SkColor4f::FromColor(SK_ColorBLACK),
525 SkColor4f::FromColor(SK_ColorWHITE),
526 SkColor4f::FromColor(SK_ColorRED),
527 { 1.f, 2.f, 4.f, 8.f }
528 };
529 TestArraySerialization(data, reporter);
530 }
531
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000532 // Test readIntArray
533 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000534 int32_t data[kArraySize] = { 1, 2, 4, 8 };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000535 TestArraySerialization(data, reporter);
536 }
537
538 // Test readPointArray
539 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000540 SkPoint data[kArraySize] = { {6, 7}, {42, 128} };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000541 TestArraySerialization(data, reporter);
542 }
543
544 // Test readScalarArray
545 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000546 SkScalar data[kArraySize] = { SK_Scalar1, SK_ScalarHalf, SK_ScalarMax };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000547 TestArraySerialization(data, reporter);
commit-bot@chromium.org02512882013-10-31 18:37:50 +0000548 }
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000549
550 // Test invalid deserializations
551 {
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000552 SkImageInfo info = SkImageInfo::MakeN32Premul(kBitmapSize, kBitmapSize);
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000553
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000554 SkBitmap validBitmap;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000555 validBitmap.setInfo(info);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000556
557 // Create a bitmap with a really large height
558 SkBitmap invalidBitmap;
reede5ea5002014-09-03 11:54:58 -0700559 invalidBitmap.setInfo(info.makeWH(info.width(), 1000000000));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000560
561 // The deserialization should succeed, and the rendering shouldn't crash,
562 // even when the device fails to initialize, due to its size
563 TestBitmapSerialization(validBitmap, invalidBitmap, true, reporter);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000564 }
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000565
566 // Test simple SkPicture serialization
567 {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000568 SkPictureRecorder recorder;
reed84825042014-09-02 12:50:45 -0700569 draw_something(recorder.beginRecording(SkIntToScalar(kBitmapSize),
570 SkIntToScalar(kBitmapSize),
halcanary96fcdcc2015-08-27 07:41:13 -0700571 nullptr, 0));
reedca2622b2016-03-18 07:25:55 -0700572 sk_sp<SkPicture> pict(recorder.finishRecordingAsPicture());
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000573
574 // Serialize picture
brianosmanfad98562016-05-04 11:06:28 -0700575 SkBinaryWriteBuffer writer;
Cary Clarkefd99cc2018-06-11 16:25:43 -0400576 SkPicturePriv::Flatten(pict, writer);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000577 size_t size = writer.bytesWritten();
commit-bot@chromium.org1e7ee992014-03-14 21:22:22 +0000578 SkAutoTMalloc<unsigned char> data(size);
579 writer.writeToMemory(static_cast<void*>(data.get()));
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000580
581 // Deserialize picture
Mike Reedfadbfcd2017-12-06 16:09:20 -0500582 SkReadBuffer reader(static_cast<void*>(data.get()), size);
Cary Clarkefd99cc2018-06-11 16:25:43 -0400583 sk_sp<SkPicture> readPict(SkPicturePriv::MakeFromBuffer(reader));
reedd921dbb2016-09-30 09:27:20 -0700584 REPORTER_ASSERT(reporter, reader.isValid());
bsalomon49f085d2014-09-05 13:34:00 -0700585 REPORTER_ASSERT(reporter, readPict.get());
Ben Wagner7825d492019-07-10 16:27:34 -0400586 sk_sp<SkImage> img0 = render(*pict);
587 sk_sp<SkImage> img1 = render(*readPict);
588 if (img0 && img1) {
589 REPORTER_ASSERT(reporter, ToolUtils::equal_pixels(img0.get(), img1.get()));
590 }
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000591 }
caseq26337e92014-06-30 12:14:52 -0700592
593 TestPictureTypefaceSerialization(reporter);
Robert Phillipsa8cdbd72018-07-17 12:30:40 -0400594
595 // Test SkLightingShader/NormalMapSource serialization
596 {
597 const int kTexSize = 2;
598
599 SkLights::Builder builder;
600
601 builder.add(SkLights::Light::MakeDirectional(SkColor3f::Make(1.0f, 1.0f, 1.0f),
602 SkVector3::Make(1.0f, 0.0f, 0.0f)));
603 builder.setAmbientLightColor(SkColor3f::Make(0.2f, 0.2f, 0.2f));
604
605 sk_sp<SkLights> fLights = builder.finish();
606
Mike Kleinea3f0142019-03-20 11:12:10 -0500607 SkBitmap diffuse = ToolUtils::create_checkerboard_bitmap(
608 kTexSize, kTexSize, 0x00000000, ToolUtils::color_to_565(0xFF804020), 8);
Robert Phillipsa8cdbd72018-07-17 12:30:40 -0400609
610 SkRect bitmapBounds = SkRect::MakeIWH(diffuse.width(), diffuse.height());
611
612 SkMatrix matrix;
613 SkRect r = SkRect::MakeWH(SkIntToScalar(kTexSize), SkIntToScalar(kTexSize));
614 matrix.setRectToRect(bitmapBounds, r, SkMatrix::kFill_ScaleToFit);
615
616 SkMatrix ctm;
617 ctm.setRotate(45);
618 SkBitmap normals;
619 normals.allocN32Pixels(kTexSize, kTexSize);
620
Mike Kleinea3f0142019-03-20 11:12:10 -0500621 ToolUtils::create_frustum_normal_map(&normals, SkIRect::MakeWH(kTexSize, kTexSize));
Mike Reed50acf8f2019-04-08 13:20:23 -0400622 sk_sp<SkShader> normalMap = normals.makeShader(&matrix);
Robert Phillipsa8cdbd72018-07-17 12:30:40 -0400623 sk_sp<SkNormalSource> normalSource = SkNormalSource::MakeFromNormalMap(std::move(normalMap),
624 ctm);
Mike Reed50acf8f2019-04-08 13:20:23 -0400625 sk_sp<SkShader> diffuseShader = diffuse.makeShader(&matrix);
Robert Phillipsa8cdbd72018-07-17 12:30:40 -0400626
627 sk_sp<SkShader> lightingShader = SkLightingShader::Make(diffuseShader,
628 normalSource,
629 fLights);
630 sk_sp<SkShader>(TestFlattenableSerialization(as_SB(lightingShader.get()), true, reporter));
631
632 lightingShader = SkLightingShader::Make(std::move(diffuseShader),
633 nullptr,
634 fLights);
635 sk_sp<SkShader>(TestFlattenableSerialization(as_SB(lightingShader.get()), true, reporter));
636
637 lightingShader = SkLightingShader::Make(nullptr,
638 std::move(normalSource),
639 fLights);
640 sk_sp<SkShader>(TestFlattenableSerialization(as_SB(lightingShader.get()), true, reporter));
641
642 lightingShader = SkLightingShader::Make(nullptr,
643 nullptr,
644 fLights);
645 sk_sp<SkShader>(TestFlattenableSerialization(as_SB(lightingShader.get()), true, reporter));
646 }
commit-bot@chromium.org02512882013-10-31 18:37:50 +0000647}
reedf70b5312016-03-04 16:36:20 -0800648
649///////////////////////////////////////////////////////////////////////////////////////////////////
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500650#include "include/core/SkAnnotation.h"
reedf70b5312016-03-04 16:36:20 -0800651
reedca2622b2016-03-18 07:25:55 -0700652static sk_sp<SkPicture> copy_picture_via_serialization(SkPicture* src) {
reedf70b5312016-03-04 16:36:20 -0800653 SkDynamicMemoryWStream wstream;
654 src->serialize(&wstream);
Ben Wagner145dbcd2016-11-03 14:40:50 -0400655 std::unique_ptr<SkStreamAsset> rstream(wstream.detachAsStream());
656 return SkPicture::MakeFromStream(rstream.get());
reedf70b5312016-03-04 16:36:20 -0800657}
658
659struct AnnotationRec {
660 const SkRect fRect;
661 const char* fKey;
bungeman38d909e2016-08-02 14:40:46 -0700662 sk_sp<SkData> fValue;
reedf70b5312016-03-04 16:36:20 -0800663};
664
665class TestAnnotationCanvas : public SkCanvas {
666 skiatest::Reporter* fReporter;
667 const AnnotationRec* fRec;
668 int fCount;
669 int fCurrIndex;
670
671public:
672 TestAnnotationCanvas(skiatest::Reporter* reporter, const AnnotationRec rec[], int count)
673 : SkCanvas(100, 100)
674 , fReporter(reporter)
675 , fRec(rec)
676 , fCount(count)
677 , fCurrIndex(0)
678 {}
679
680 ~TestAnnotationCanvas() {
681 REPORTER_ASSERT(fReporter, fCount == fCurrIndex);
682 }
683
684protected:
685 void onDrawAnnotation(const SkRect& rect, const char key[], SkData* value) {
686 REPORTER_ASSERT(fReporter, fCurrIndex < fCount);
687 REPORTER_ASSERT(fReporter, rect == fRec[fCurrIndex].fRect);
688 REPORTER_ASSERT(fReporter, !strcmp(key, fRec[fCurrIndex].fKey));
bungeman38d909e2016-08-02 14:40:46 -0700689 REPORTER_ASSERT(fReporter, value->equals(fRec[fCurrIndex].fValue.get()));
reedf70b5312016-03-04 16:36:20 -0800690 fCurrIndex += 1;
691 }
692};
693
694/*
695 * Test the 3 annotation types by recording them into a picture, serializing, and then playing
696 * them back into another canvas.
697 */
698DEF_TEST(Annotations, reporter) {
699 SkPictureRecorder recorder;
700 SkCanvas* recordingCanvas = recorder.beginRecording(SkRect::MakeWH(100, 100));
halcanary9d524f22016-03-29 09:03:52 -0700701
reedf70b5312016-03-04 16:36:20 -0800702 const char* str0 = "rect-with-url";
703 const SkRect r0 = SkRect::MakeWH(10, 10);
bungeman38d909e2016-08-02 14:40:46 -0700704 sk_sp<SkData> d0(SkData::MakeWithCString(str0));
705 SkAnnotateRectWithURL(recordingCanvas, r0, d0.get());
halcanary9d524f22016-03-29 09:03:52 -0700706
reedf70b5312016-03-04 16:36:20 -0800707 const char* str1 = "named-destination";
708 const SkRect r1 = SkRect::MakeXYWH(5, 5, 0, 0); // collapsed to a point
bungeman38d909e2016-08-02 14:40:46 -0700709 sk_sp<SkData> d1(SkData::MakeWithCString(str1));
710 SkAnnotateNamedDestination(recordingCanvas, {r1.x(), r1.y()}, d1.get());
halcanary9d524f22016-03-29 09:03:52 -0700711
reedf70b5312016-03-04 16:36:20 -0800712 const char* str2 = "link-to-destination";
713 const SkRect r2 = SkRect::MakeXYWH(20, 20, 5, 6);
bungeman38d909e2016-08-02 14:40:46 -0700714 sk_sp<SkData> d2(SkData::MakeWithCString(str2));
715 SkAnnotateLinkToDestination(recordingCanvas, r2, d2.get());
reedf70b5312016-03-04 16:36:20 -0800716
717 const AnnotationRec recs[] = {
bungeman38d909e2016-08-02 14:40:46 -0700718 { r0, SkAnnotationKeys::URL_Key(), std::move(d0) },
719 { r1, SkAnnotationKeys::Define_Named_Dest_Key(), std::move(d1) },
720 { r2, SkAnnotationKeys::Link_Named_Dest_Key(), std::move(d2) },
reedf70b5312016-03-04 16:36:20 -0800721 };
722
reedca2622b2016-03-18 07:25:55 -0700723 sk_sp<SkPicture> pict0(recorder.finishRecordingAsPicture());
724 sk_sp<SkPicture> pict1(copy_picture_via_serialization(pict0.get()));
reedf70b5312016-03-04 16:36:20 -0800725
726 TestAnnotationCanvas canvas(reporter, recs, SK_ARRAY_COUNT(recs));
727 canvas.drawPicture(pict1);
728}
Mike Reed25325842018-03-14 09:52:02 -0400729
730DEF_TEST(WriteBuffer_storage, reporter) {
731 enum {
732 kSize = 32
733 };
734 int32_t storage[kSize/4];
735 char src[kSize];
736 sk_bzero(src, kSize);
737
738 SkBinaryWriteBuffer writer(storage, kSize);
739 REPORTER_ASSERT(reporter, writer.usingInitialStorage());
740 REPORTER_ASSERT(reporter, writer.bytesWritten() == 0);
741 writer.write(src, kSize - 4);
742 REPORTER_ASSERT(reporter, writer.usingInitialStorage());
743 REPORTER_ASSERT(reporter, writer.bytesWritten() == kSize - 4);
744 writer.writeInt(0);
745 REPORTER_ASSERT(reporter, writer.usingInitialStorage());
746 REPORTER_ASSERT(reporter, writer.bytesWritten() == kSize);
747
748 writer.reset(storage, kSize-4);
749 REPORTER_ASSERT(reporter, writer.usingInitialStorage());
750 REPORTER_ASSERT(reporter, writer.bytesWritten() == 0);
751 writer.write(src, kSize - 4);
752 REPORTER_ASSERT(reporter, writer.usingInitialStorage());
753 REPORTER_ASSERT(reporter, writer.bytesWritten() == kSize - 4);
754 writer.writeInt(0);
755 REPORTER_ASSERT(reporter, !writer.usingInitialStorage()); // this is the change
756 REPORTER_ASSERT(reporter, writer.bytesWritten() == kSize);
757}
Khushal42f8bc42018-04-03 17:51:40 -0700758
759DEF_TEST(WriteBuffer_external_memory_textblob, reporter) {
Mike Reed2ed78202018-11-21 15:10:08 -0500760 SkFont font;
Khushal42f8bc42018-04-03 17:51:40 -0700761 font.setTypeface(SkTypeface::MakeDefault());
762
763 SkTextBlobBuilder builder;
764 int glyph_count = 5;
765 const auto& run = builder.allocRun(font, glyph_count, 1.2f, 2.3f);
766 // allocRun() allocates only the glyph buffer.
767 std::fill(run.glyphs, run.glyphs + glyph_count, 0);
768 auto blob = builder.make();
769 SkSerialProcs procs;
770 SkAutoTMalloc<uint8_t> storage;
771 size_t blob_size = 0u;
772 size_t storage_size = 0u;
773
774 blob_size = SkAlign4(blob->serialize(procs)->size());
775 REPORTER_ASSERT(reporter, blob_size > 4u);
776 storage_size = blob_size - 4;
777 storage.realloc(storage_size);
778 REPORTER_ASSERT(reporter, blob->serialize(procs, storage.get(), storage_size) == 0u);
779 storage_size = blob_size;
780 storage.realloc(storage_size);
781 REPORTER_ASSERT(reporter, blob->serialize(procs, storage.get(), storage_size) != 0u);
782}
783
784DEF_TEST(WriteBuffer_external_memory_flattenable, reporter) {
785 SkScalar intervals[] = {1.f, 1.f};
786 auto path_effect = SkDashPathEffect::Make(intervals, 2, 0);
787 size_t path_size = SkAlign4(path_effect->serialize()->size());
788 REPORTER_ASSERT(reporter, path_size > 4u);
789 SkAutoTMalloc<uint8_t> storage;
790
791 size_t storage_size = path_size - 4;
792 storage.realloc(storage_size);
793 REPORTER_ASSERT(reporter, path_effect->serialize(storage.get(), storage_size) == 0u);
794
795 storage_size = path_size;
796 storage.realloc(storage_size);
797 REPORTER_ASSERT(reporter, path_effect->serialize(storage.get(), storage_size) != 0u);
798}