blob: db6cd9dababc29e341fa03d4d802acf0bd137046 [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"
Michael Ludwig55edb502019-08-05 10:41:10 -040015#include "include/effects/SkImageFilters.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "include/effects/SkTableColorFilter.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "include/private/SkFixed.h"
18#include "include/private/SkTemplates.h"
19#include "src/core/SkAnnotationKeys.h"
20#include "src/core/SkFontDescriptor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/core/SkMatrixPriv.h"
22#include "src/core/SkNormalSource.h"
23#include "src/core/SkOSFile.h"
24#include "src/core/SkPicturePriv.h"
25#include "src/core/SkReadBuffer.h"
26#include "src/core/SkWriteBuffer.h"
27#include "src/shaders/SkLightingShader.h"
28#include "src/shaders/SkShaderBase.h"
29#include "tests/Test.h"
30#include "tools/Resources.h"
31#include "tools/ToolUtils.h"
commit-bot@chromium.org02512882013-10-31 18:37:50 +000032
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000033static const uint32_t kArraySize = 64;
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +000034static const int kBitmapSize = 256;
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000035
Cary Clark6d6d6032017-10-20 12:14:33 -040036class SerializationTest {
37public:
38
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000039template<typename T>
40static void TestAlignment(T* testObj, skiatest::Reporter* reporter) {
41 // Test memory read/write functions directly
42 unsigned char dataWritten[1024];
43 size_t bytesWrittenToMemory = testObj->writeToMemory(dataWritten);
44 REPORTER_ASSERT(reporter, SkAlign4(bytesWrittenToMemory) == bytesWrittenToMemory);
45 size_t bytesReadFromMemory = testObj->readFromMemory(dataWritten, bytesWrittenToMemory);
46 REPORTER_ASSERT(reporter, SkAlign4(bytesReadFromMemory) == bytesReadFromMemory);
47}
Cary Clark6d6d6032017-10-20 12:14:33 -040048};
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000049
50template<typename T> struct SerializationUtils {
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +000051 // Generic case for flattenables
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000052 static void Write(SkWriteBuffer& writer, const T* flattenable) {
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +000053 writer.writeFlattenable(flattenable);
54 }
Mike Reedfadbfcd2017-12-06 16:09:20 -050055 static void Read(SkReadBuffer& reader, T** flattenable) {
mtklein3b375452016-04-04 14:57:19 -070056 *flattenable = (T*)reader.readFlattenable(T::GetFlattenableType());
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +000057 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000058};
59
60template<> struct SerializationUtils<SkMatrix> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000061 static void Write(SkWriteBuffer& writer, const SkMatrix* matrix) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000062 writer.writeMatrix(*matrix);
63 }
Mike Reedfadbfcd2017-12-06 16:09:20 -050064 static void Read(SkReadBuffer& reader, SkMatrix* matrix) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000065 reader.readMatrix(matrix);
66 }
67};
68
69template<> struct SerializationUtils<SkPath> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000070 static void Write(SkWriteBuffer& writer, const SkPath* path) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000071 writer.writePath(*path);
72 }
Mike Reedfadbfcd2017-12-06 16:09:20 -050073 static void Read(SkReadBuffer& reader, SkPath* path) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000074 reader.readPath(path);
75 }
76};
77
78template<> struct SerializationUtils<SkRegion> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000079 static void Write(SkWriteBuffer& writer, const SkRegion* region) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000080 writer.writeRegion(*region);
81 }
Mike Reedfadbfcd2017-12-06 16:09:20 -050082 static void Read(SkReadBuffer& reader, SkRegion* region) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000083 reader.readRegion(region);
84 }
85};
86
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +000087template<> struct SerializationUtils<SkString> {
88 static void Write(SkWriteBuffer& writer, const SkString* string) {
89 writer.writeString(string->c_str());
90 }
Mike Reedfadbfcd2017-12-06 16:09:20 -050091 static void Read(SkReadBuffer& reader, SkString* string) {
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +000092 reader.readString(string);
93 }
94};
95
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000096template<> struct SerializationUtils<unsigned char> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000097 static void Write(SkWriteBuffer& writer, unsigned char* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +000098 writer.writeByteArray(data, arraySize);
sugoi@google.com305f78e2013-11-04 16:18:15 +000099 }
Mike Reedfadbfcd2017-12-06 16:09:20 -0500100 static bool Read(SkReadBuffer& reader, unsigned char* data, uint32_t arraySize) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000101 return reader.readByteArray(data, arraySize);
102 }
103};
commit-bot@chromium.org02512882013-10-31 18:37:50 +0000104
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000105template<> struct SerializationUtils<SkColor> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000106 static void Write(SkWriteBuffer& writer, SkColor* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +0000107 writer.writeColorArray(data, arraySize);
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000108 }
Mike Reedfadbfcd2017-12-06 16:09:20 -0500109 static bool Read(SkReadBuffer& reader, SkColor* data, uint32_t arraySize) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000110 return reader.readColorArray(data, arraySize);
111 }
112};
sugoi@google.com305f78e2013-11-04 16:18:15 +0000113
brianosman97bbf822016-09-25 13:15:58 -0700114template<> struct SerializationUtils<SkColor4f> {
115 static void Write(SkWriteBuffer& writer, SkColor4f* data, uint32_t arraySize) {
116 writer.writeColor4fArray(data, arraySize);
117 }
Mike Reedfadbfcd2017-12-06 16:09:20 -0500118 static bool Read(SkReadBuffer& reader, SkColor4f* data, uint32_t arraySize) {
brianosman97bbf822016-09-25 13:15:58 -0700119 return reader.readColor4fArray(data, arraySize);
120 }
121};
122
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000123template<> struct SerializationUtils<int32_t> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000124 static void Write(SkWriteBuffer& writer, int32_t* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +0000125 writer.writeIntArray(data, arraySize);
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000126 }
Mike Reedfadbfcd2017-12-06 16:09:20 -0500127 static bool Read(SkReadBuffer& reader, int32_t* data, uint32_t arraySize) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000128 return reader.readIntArray(data, arraySize);
129 }
130};
sugoi@google.com305f78e2013-11-04 16:18:15 +0000131
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000132template<> struct SerializationUtils<SkPoint> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000133 static void Write(SkWriteBuffer& writer, SkPoint* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +0000134 writer.writePointArray(data, arraySize);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000135 }
Mike Reedfadbfcd2017-12-06 16:09:20 -0500136 static bool Read(SkReadBuffer& reader, SkPoint* data, uint32_t arraySize) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000137 return reader.readPointArray(data, arraySize);
138 }
139};
reed@google.com12a23862013-11-04 21:35:55 +0000140
Michael Ludwigea241402018-08-22 09:26:33 -0400141template<> struct SerializationUtils<SkPoint3> {
142 static void Write(SkWriteBuffer& writer, const SkPoint3* data) {
143 writer.writePoint3(*data);
144 }
145 static void Read(SkReadBuffer& reader, SkPoint3* data) {
146 reader.readPoint3(data);
147 }
148};
149
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000150template<> struct SerializationUtils<SkScalar> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000151 static void Write(SkWriteBuffer& writer, SkScalar* data, uint32_t arraySize) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000152 writer.writeScalarArray(data, arraySize);
153 }
Mike Reedfadbfcd2017-12-06 16:09:20 -0500154 static bool Read(SkReadBuffer& reader, SkScalar* data, uint32_t arraySize) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000155 return reader.readScalarArray(data, arraySize);
156 }
157};
reed@google.com12a23862013-11-04 21:35:55 +0000158
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000159template<typename T, bool testInvalid> struct SerializationTestUtils {
160 static void InvalidateData(unsigned char* data) {}
161};
162
163template<> struct SerializationTestUtils<SkString, true> {
164 static void InvalidateData(unsigned char* data) {
165 data[3] |= 0x80; // Reverse sign of 1st integer
166 }
167};
168
169template<typename T, bool testInvalid>
170static void TestObjectSerializationNoAlign(T* testObj, skiatest::Reporter* reporter) {
brianosmanfad98562016-05-04 11:06:28 -0700171 SkBinaryWriteBuffer writer;
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000172 SerializationUtils<T>::Write(writer, testObj);
173 size_t bytesWritten = writer.bytesWritten();
174 REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten);
reed@google.com12a23862013-11-04 21:35:55 +0000175
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000176 unsigned char dataWritten[1024];
177 writer.writeToMemory(dataWritten);
178
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000179 SerializationTestUtils<T, testInvalid>::InvalidateData(dataWritten);
180
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000181 // Make sure this fails when it should (test with smaller size, but still multiple of 4)
Mike Reedfadbfcd2017-12-06 16:09:20 -0500182 SkReadBuffer buffer(dataWritten, bytesWritten - 4);
commit-bot@chromium.org8f457e32013-11-08 19:22:57 +0000183 T obj;
184 SerializationUtils<T>::Read(buffer, &obj);
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000185 REPORTER_ASSERT(reporter, !buffer.isValid());
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000186
187 // Make sure this succeeds when it should
Mike Reedfadbfcd2017-12-06 16:09:20 -0500188 SkReadBuffer buffer2(dataWritten, bytesWritten);
Robert Phillipsb2526042016-09-26 09:00:36 -0400189 size_t offsetBefore = buffer2.offset();
commit-bot@chromium.org8f457e32013-11-08 19:22:57 +0000190 T obj2;
191 SerializationUtils<T>::Read(buffer2, &obj2);
Robert Phillipsb2526042016-09-26 09:00:36 -0400192 size_t offsetAfter = buffer2.offset();
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000193 // This should have succeeded, since there are enough bytes to read this
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000194 REPORTER_ASSERT(reporter, buffer2.isValid() == !testInvalid);
195 // Note: This following test should always succeed, regardless of whether the buffer is valid,
196 // 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 -0400197 REPORTER_ASSERT(reporter, offsetAfter - offsetBefore == bytesWritten);
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000198}
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000199
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000200template<typename T>
201static void TestObjectSerialization(T* testObj, skiatest::Reporter* reporter) {
202 TestObjectSerializationNoAlign<T, false>(testObj, reporter);
Cary Clark6d6d6032017-10-20 12:14:33 -0400203 SerializationTest::TestAlignment(testObj, reporter);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000204}
205
206template<typename T>
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000207static T* TestFlattenableSerialization(T* testObj, bool shouldSucceed,
208 skiatest::Reporter* reporter) {
brianosmanfad98562016-05-04 11:06:28 -0700209 SkBinaryWriteBuffer writer;
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000210 SerializationUtils<T>::Write(writer, testObj);
211 size_t bytesWritten = writer.bytesWritten();
212 REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten);
213
dvonbeck8811e402016-06-16 12:39:25 -0700214 SkASSERT(bytesWritten <= 4096);
senorblanco91c395a2014-09-25 15:51:35 -0700215 unsigned char dataWritten[4096];
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000216 writer.writeToMemory(dataWritten);
217
218 // Make sure this fails when it should (test with smaller size, but still multiple of 4)
Mike Reedfadbfcd2017-12-06 16:09:20 -0500219 SkReadBuffer buffer(dataWritten, bytesWritten - 4);
halcanary96fcdcc2015-08-27 07:41:13 -0700220 T* obj = nullptr;
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000221 SerializationUtils<T>::Read(buffer, &obj);
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000222 REPORTER_ASSERT(reporter, !buffer.isValid());
halcanary96fcdcc2015-08-27 07:41:13 -0700223 REPORTER_ASSERT(reporter, nullptr == obj);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000224
225 // Make sure this succeeds when it should
Mike Reedfadbfcd2017-12-06 16:09:20 -0500226 SkReadBuffer buffer2(dataWritten, bytesWritten);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000227 const unsigned char* peekBefore = static_cast<const unsigned char*>(buffer2.skip(0));
halcanary96fcdcc2015-08-27 07:41:13 -0700228 T* obj2 = nullptr;
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000229 SerializationUtils<T>::Read(buffer2, &obj2);
230 const unsigned char* peekAfter = static_cast<const unsigned char*>(buffer2.skip(0));
231 if (shouldSucceed) {
232 // This should have succeeded, since there are enough bytes to read this
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000233 REPORTER_ASSERT(reporter, buffer2.isValid());
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000234 REPORTER_ASSERT(reporter, static_cast<size_t>(peekAfter - peekBefore) == bytesWritten);
bsalomon49f085d2014-09-05 13:34:00 -0700235 REPORTER_ASSERT(reporter, obj2);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000236 } else {
237 // If the deserialization was supposed to fail, make sure it did
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000238 REPORTER_ASSERT(reporter, !buffer.isValid());
halcanary96fcdcc2015-08-27 07:41:13 -0700239 REPORTER_ASSERT(reporter, nullptr == obj2);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000240 }
241
242 return obj2; // Return object to perform further validity tests on it
243}
244
245template<typename T>
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000246static void TestArraySerialization(T* data, skiatest::Reporter* reporter) {
brianosmanfad98562016-05-04 11:06:28 -0700247 SkBinaryWriteBuffer writer;
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000248 SerializationUtils<T>::Write(writer, data, kArraySize);
249 size_t bytesWritten = writer.bytesWritten();
250 // This should write the length (in 4 bytes) and the array
251 REPORTER_ASSERT(reporter, (4 + kArraySize * sizeof(T)) == bytesWritten);
252
brianosman97bbf822016-09-25 13:15:58 -0700253 unsigned char dataWritten[2048];
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000254 writer.writeToMemory(dataWritten);
255
256 // Make sure this fails when it should
Mike Reedfadbfcd2017-12-06 16:09:20 -0500257 SkReadBuffer buffer(dataWritten, bytesWritten);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000258 T dataRead[kArraySize];
259 bool success = SerializationUtils<T>::Read(buffer, dataRead, kArraySize / 2);
260 // This should have failed, since the provided size was too small
261 REPORTER_ASSERT(reporter, !success);
262
263 // Make sure this succeeds when it should
Mike Reedfadbfcd2017-12-06 16:09:20 -0500264 SkReadBuffer buffer2(dataWritten, bytesWritten);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000265 success = SerializationUtils<T>::Read(buffer2, dataRead, kArraySize);
266 // This should have succeeded, since there are enough bytes to read this
267 REPORTER_ASSERT(reporter, success);
268}
269
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000270static void TestBitmapSerialization(const SkBitmap& validBitmap,
271 const SkBitmap& invalidBitmap,
272 bool shouldSucceed,
273 skiatest::Reporter* reporter) {
reed9ce9d672016-03-17 10:51:11 -0700274 sk_sp<SkImage> validImage(SkImage::MakeFromBitmap(validBitmap));
Michael Ludwig55edb502019-08-05 10:41:10 -0400275 sk_sp<SkImageFilter> validBitmapSource(SkImageFilters::Image(std::move(validImage)));
reed9ce9d672016-03-17 10:51:11 -0700276 sk_sp<SkImage> invalidImage(SkImage::MakeFromBitmap(invalidBitmap));
Michael Ludwig55edb502019-08-05 10:41:10 -0400277 sk_sp<SkImageFilter> invalidBitmapSource(SkImageFilters::Image(std::move(invalidImage)));
reedcfb6bdf2016-03-29 11:32:50 -0700278 sk_sp<SkImageFilter> xfermodeImageFilter(
Michael Ludwig55edb502019-08-05 10:41:10 -0400279 SkImageFilters::Xfermode(SkBlendMode::kSrcOver,
280 std::move(invalidBitmapSource),
281 std::move(validBitmapSource), nullptr));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000282
Mike Reed5e257172016-11-01 11:22:05 -0400283 sk_sp<SkImageFilter> deserializedFilter(
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000284 TestFlattenableSerialization<SkImageFilter>(
reedcfb6bdf2016-03-29 11:32:50 -0700285 xfermodeImageFilter.get(), shouldSucceed, reporter));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000286
287 // Try to render a small bitmap using the invalid deserialized filter
288 // to make sure we don't crash while trying to render it
289 if (shouldSucceed) {
290 SkBitmap bitmap;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000291 bitmap.allocN32Pixels(24, 24);
292 SkCanvas canvas(bitmap);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000293 canvas.clear(0x00000000);
294 SkPaint paint;
295 paint.setImageFilter(deserializedFilter);
296 canvas.clipRect(SkRect::MakeXYWH(0, 0, SkIntToScalar(24), SkIntToScalar(24)));
297 canvas.drawBitmap(bitmap, 0, 0, &paint);
298 }
299}
300
senorblanco91c395a2014-09-25 15:51:35 -0700301static void TestColorFilterSerialization(skiatest::Reporter* reporter) {
302 uint8_t table[256];
303 for (int i = 0; i < 256; ++i) {
304 table[i] = (i * 41) % 256;
305 }
reedd053ce92016-03-22 10:17:23 -0700306 auto colorFilter(SkTableColorFilter::Make(table));
Hal Canary342b7ac2016-11-04 11:49:42 -0400307 sk_sp<SkColorFilter> copy(
senorblanco91c395a2014-09-25 15:51:35 -0700308 TestFlattenableSerialization<SkColorFilter>(colorFilter.get(), true, reporter));
309}
310
caseq26337e92014-06-30 12:14:52 -0700311static SkBitmap draw_picture(SkPicture& picture) {
312 SkBitmap bitmap;
halcanary9d524f22016-03-29 09:03:52 -0700313 bitmap.allocN32Pixels(SkScalarCeilToInt(picture.cullRect().width()),
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700314 SkScalarCeilToInt(picture.cullRect().height()));
caseq26337e92014-06-30 12:14:52 -0700315 SkCanvas canvas(bitmap);
robertphillipsc5ba71d2014-09-04 08:42:50 -0700316 picture.playback(&canvas);
caseq26337e92014-06-30 12:14:52 -0700317 return bitmap;
318}
319
320static void compare_bitmaps(skiatest::Reporter* reporter,
321 const SkBitmap& b1, const SkBitmap& b2) {
322 REPORTER_ASSERT(reporter, b1.width() == b2.width());
323 REPORTER_ASSERT(reporter, b1.height() == b2.height());
caseq26337e92014-06-30 12:14:52 -0700324
325 if ((b1.width() != b2.width()) ||
326 (b1.height() != b2.height())) {
327 return;
328 }
329
330 int pixelErrors = 0;
331 for (int y = 0; y < b2.height(); ++y) {
332 for (int x = 0; x < b2.width(); ++x) {
333 if (b1.getColor(x, y) != b2.getColor(x, y))
334 ++pixelErrors;
335 }
336 }
337 REPORTER_ASSERT(reporter, 0 == pixelErrors);
338}
bungeman13b9c952016-05-12 10:09:30 -0700339static void serialize_and_compare_typeface(sk_sp<SkTypeface> typeface, const char* text,
bungeman41868fe2015-05-20 09:21:04 -0700340 skiatest::Reporter* reporter)
341{
Hal Canary3560ea72019-01-08 13:01:58 -0500342 // Create a font with the typeface.
caseq26337e92014-06-30 12:14:52 -0700343 SkPaint paint;
344 paint.setColor(SK_ColorGRAY);
Hal Canary3560ea72019-01-08 13:01:58 -0500345 SkFont font(std::move(typeface), 30);
caseq26337e92014-06-30 12:14:52 -0700346
347 // Paint some text.
348 SkPictureRecorder recorder;
349 SkIRect canvasRect = SkIRect::MakeWH(kBitmapSize, kBitmapSize);
halcanary9d524f22016-03-29 09:03:52 -0700350 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(canvasRect.width()),
351 SkIntToScalar(canvasRect.height()),
halcanary96fcdcc2015-08-27 07:41:13 -0700352 nullptr, 0);
caseq26337e92014-06-30 12:14:52 -0700353 canvas->drawColor(SK_ColorWHITE);
Hal Canary3560ea72019-01-08 13:01:58 -0500354 canvas->drawString(text, 24, 32, font, paint);
reedca2622b2016-03-18 07:25:55 -0700355 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
caseq26337e92014-06-30 12:14:52 -0700356
357 // Serlialize picture and create its clone from stream.
358 SkDynamicMemoryWStream stream;
359 picture->serialize(&stream);
Ben Wagner145dbcd2016-11-03 14:40:50 -0400360 std::unique_ptr<SkStream> inputStream(stream.detachAsStream());
reedca2622b2016-03-18 07:25:55 -0700361 sk_sp<SkPicture> loadedPicture(SkPicture::MakeFromStream(inputStream.get()));
caseq26337e92014-06-30 12:14:52 -0700362
363 // Draw both original and clone picture and compare bitmaps -- they should be identical.
364 SkBitmap origBitmap = draw_picture(*picture);
365 SkBitmap destBitmap = draw_picture(*loadedPicture);
366 compare_bitmaps(reporter, origBitmap, destBitmap);
367}
368
bungeman41868fe2015-05-20 09:21:04 -0700369static void TestPictureTypefaceSerialization(skiatest::Reporter* reporter) {
370 {
371 // Load typeface from file to test CreateFromFile with index.
Mike Reed271d1d92018-09-03 21:10:10 -0400372 auto typeface = MakeResourceAsTypeface("fonts/test.ttc", 1);
bungeman41868fe2015-05-20 09:21:04 -0700373 if (!typeface) {
halcanary7d571242016-02-24 17:59:16 -0800374 INFOF(reporter, "Could not run fontstream test because test.ttc not found.");
bungeman41868fe2015-05-20 09:21:04 -0700375 } else {
bungeman13b9c952016-05-12 10:09:30 -0700376 serialize_and_compare_typeface(std::move(typeface), "A!", reporter);
bungeman41868fe2015-05-20 09:21:04 -0700377 }
378 }
379
380 {
381 // Load typeface as stream to create with axis settings.
Mike Reed0933bc92017-12-09 01:27:41 +0000382 std::unique_ptr<SkStreamAsset> distortable(GetResourceAsStream("fonts/Distortable.ttf"));
bungeman41868fe2015-05-20 09:21:04 -0700383 if (!distortable) {
halcanary7d571242016-02-24 17:59:16 -0800384 INFOF(reporter, "Could not run fontstream test because Distortable.ttf not found.");
bungeman41868fe2015-05-20 09:21:04 -0700385 } else {
386 SkFixed axis = SK_FixedSqrt2;
bungeman13b9c952016-05-12 10:09:30 -0700387 sk_sp<SkTypeface> typeface(SkTypeface::MakeFromFontData(
Mike Kleinf46d5ca2019-12-11 10:45:01 -0500388 std::make_unique<SkFontData>(std::move(distortable), 0, &axis, 1)));
bungeman41868fe2015-05-20 09:21:04 -0700389 if (!typeface) {
halcanary7d571242016-02-24 17:59:16 -0800390 INFOF(reporter, "Could not run fontstream test because Distortable.ttf not created.");
bungeman41868fe2015-05-20 09:21:04 -0700391 } else {
Hal Canary3560ea72019-01-08 13:01:58 -0500392 serialize_and_compare_typeface(std::move(typeface), "ab", reporter);
bungeman41868fe2015-05-20 09:21:04 -0700393 }
394 }
395 }
396}
397
reed84825042014-09-02 12:50:45 -0700398static void setup_bitmap_for_canvas(SkBitmap* bitmap) {
399 bitmap->allocN32Pixels(kBitmapSize, kBitmapSize);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000400}
401
reed84825042014-09-02 12:50:45 -0700402static void make_checkerboard_bitmap(SkBitmap& bitmap) {
403 setup_bitmap_for_canvas(&bitmap);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000404
405 SkCanvas canvas(bitmap);
406 canvas.clear(0x00000000);
407 SkPaint darkPaint;
408 darkPaint.setColor(0xFF804020);
409 SkPaint lightPaint;
410 lightPaint.setColor(0xFF244484);
411 const int i = kBitmapSize / 8;
412 const SkScalar f = SkIntToScalar(i);
413 for (int y = 0; y < kBitmapSize; y += i) {
414 for (int x = 0; x < kBitmapSize; x += i) {
415 canvas.save();
416 canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
417 canvas.drawRect(SkRect::MakeXYWH(0, 0, f, f), darkPaint);
418 canvas.drawRect(SkRect::MakeXYWH(f, 0, f, f), lightPaint);
419 canvas.drawRect(SkRect::MakeXYWH(0, f, f, f), lightPaint);
420 canvas.drawRect(SkRect::MakeXYWH(f, f, f, f), darkPaint);
421 canvas.restore();
422 }
423 }
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000424}
425
reed84825042014-09-02 12:50:45 -0700426static void draw_something(SkCanvas* canvas) {
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000427 SkPaint paint;
428 SkBitmap bitmap;
reed84825042014-09-02 12:50:45 -0700429 make_checkerboard_bitmap(bitmap);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000430
431 canvas->save();
432 canvas->scale(0.5f, 0.5f);
halcanary96fcdcc2015-08-27 07:41:13 -0700433 canvas->drawBitmap(bitmap, 0, 0, nullptr);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000434 canvas->restore();
435
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000436 paint.setAntiAlias(true);
437
438 paint.setColor(SK_ColorRED);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000439 canvas->drawCircle(SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/3), paint);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000440 paint.setColor(SK_ColorBLACK);
Mike Reed1af9b482019-01-07 11:01:57 -0500441
442 SkFont font;
443 font.setSize(kBitmapSize/3);
444 canvas->drawString("Picture", SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/4), font, paint);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000445}
446
Ben Wagner7825d492019-07-10 16:27:34 -0400447static sk_sp<SkImage> render(const SkPicture& p) {
448 auto surf = SkSurface::MakeRasterN32Premul(SkScalarRoundToInt(p.cullRect().width()),
449 SkScalarRoundToInt(p.cullRect().height()));
450 if (!surf) {
451 return nullptr; // bounds are empty?
452 }
453 surf->getCanvas()->clear(SK_ColorWHITE);
454 p.playback(surf->getCanvas());
455 return surf->makeImageSnapshot();
456}
457
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000458DEF_TEST(Serialization, reporter) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000459 // Test matrix serialization
460 {
461 SkMatrix matrix = SkMatrix::I();
462 TestObjectSerialization(&matrix, reporter);
caseq26337e92014-06-30 12:14:52 -0700463 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000464
Michael Ludwigea241402018-08-22 09:26:33 -0400465 // Test point3 serialization
466 {
467 SkPoint3 point;
468 TestObjectSerializationNoAlign<SkPoint3, false>(&point, reporter);
469 }
470
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000471 // Test path serialization
472 {
473 SkPath path;
474 TestObjectSerialization(&path, reporter);
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000475 }
sugoi@google.com305f78e2013-11-04 16:18:15 +0000476
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000477 // Test region serialization
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000478 {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000479 SkRegion region;
480 TestObjectSerialization(&region, reporter);
481 }
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000482
senorblanco91c395a2014-09-25 15:51:35 -0700483 // Test color filter serialization
484 {
485 TestColorFilterSerialization(reporter);
486 }
487
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000488 // Test string serialization
489 {
490 SkString string("string");
491 TestObjectSerializationNoAlign<SkString, false>(&string, reporter);
492 TestObjectSerializationNoAlign<SkString, true>(&string, reporter);
493 }
494
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000495 // Test rrect serialization
496 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000497 // SkRRect does not initialize anything.
498 // An uninitialized SkRRect can be serialized,
499 // but will branch on uninitialized data when deserialized.
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000500 SkRRect rrect;
rmistry@google.comd6bab022013-12-02 13:50:38 +0000501 SkRect rect = SkRect::MakeXYWH(1, 2, 20, 30);
502 SkVector corners[4] = { {1, 2}, {2, 3}, {3,4}, {4,5} };
503 rrect.setRectRadii(rect, corners);
Cary Clark6d6d6032017-10-20 12:14:33 -0400504 SerializationTest::TestAlignment(&rrect, reporter);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000505 }
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000506
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000507 // Test readByteArray
508 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000509 unsigned char data[kArraySize] = { 1, 2, 3 };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000510 TestArraySerialization(data, reporter);
511 }
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000512
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000513 // Test readColorArray
514 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000515 SkColor data[kArraySize] = { SK_ColorBLACK, SK_ColorWHITE, SK_ColorRED };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000516 TestArraySerialization(data, reporter);
517 }
518
brianosman97bbf822016-09-25 13:15:58 -0700519 // Test readColor4fArray
520 {
521 SkColor4f data[kArraySize] = {
522 SkColor4f::FromColor(SK_ColorBLACK),
523 SkColor4f::FromColor(SK_ColorWHITE),
524 SkColor4f::FromColor(SK_ColorRED),
525 { 1.f, 2.f, 4.f, 8.f }
526 };
527 TestArraySerialization(data, reporter);
528 }
529
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000530 // Test readIntArray
531 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000532 int32_t data[kArraySize] = { 1, 2, 4, 8 };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000533 TestArraySerialization(data, reporter);
534 }
535
536 // Test readPointArray
537 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000538 SkPoint data[kArraySize] = { {6, 7}, {42, 128} };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000539 TestArraySerialization(data, reporter);
540 }
541
542 // Test readScalarArray
543 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000544 SkScalar data[kArraySize] = { SK_Scalar1, SK_ScalarHalf, SK_ScalarMax };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000545 TestArraySerialization(data, reporter);
commit-bot@chromium.org02512882013-10-31 18:37:50 +0000546 }
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000547
548 // Test invalid deserializations
549 {
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000550 SkImageInfo info = SkImageInfo::MakeN32Premul(kBitmapSize, kBitmapSize);
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000551
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000552 SkBitmap validBitmap;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000553 validBitmap.setInfo(info);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000554
555 // Create a bitmap with a really large height
556 SkBitmap invalidBitmap;
reede5ea5002014-09-03 11:54:58 -0700557 invalidBitmap.setInfo(info.makeWH(info.width(), 1000000000));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000558
559 // The deserialization should succeed, and the rendering shouldn't crash,
560 // even when the device fails to initialize, due to its size
561 TestBitmapSerialization(validBitmap, invalidBitmap, true, reporter);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000562 }
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000563
564 // Test simple SkPicture serialization
565 {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000566 SkPictureRecorder recorder;
reed84825042014-09-02 12:50:45 -0700567 draw_something(recorder.beginRecording(SkIntToScalar(kBitmapSize),
568 SkIntToScalar(kBitmapSize),
halcanary96fcdcc2015-08-27 07:41:13 -0700569 nullptr, 0));
reedca2622b2016-03-18 07:25:55 -0700570 sk_sp<SkPicture> pict(recorder.finishRecordingAsPicture());
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000571
572 // Serialize picture
brianosmanfad98562016-05-04 11:06:28 -0700573 SkBinaryWriteBuffer writer;
Cary Clarkefd99cc2018-06-11 16:25:43 -0400574 SkPicturePriv::Flatten(pict, writer);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000575 size_t size = writer.bytesWritten();
commit-bot@chromium.org1e7ee992014-03-14 21:22:22 +0000576 SkAutoTMalloc<unsigned char> data(size);
577 writer.writeToMemory(static_cast<void*>(data.get()));
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000578
579 // Deserialize picture
Mike Reedfadbfcd2017-12-06 16:09:20 -0500580 SkReadBuffer reader(static_cast<void*>(data.get()), size);
Cary Clarkefd99cc2018-06-11 16:25:43 -0400581 sk_sp<SkPicture> readPict(SkPicturePriv::MakeFromBuffer(reader));
reedd921dbb2016-09-30 09:27:20 -0700582 REPORTER_ASSERT(reporter, reader.isValid());
bsalomon49f085d2014-09-05 13:34:00 -0700583 REPORTER_ASSERT(reporter, readPict.get());
Ben Wagner7825d492019-07-10 16:27:34 -0400584 sk_sp<SkImage> img0 = render(*pict);
585 sk_sp<SkImage> img1 = render(*readPict);
586 if (img0 && img1) {
587 REPORTER_ASSERT(reporter, ToolUtils::equal_pixels(img0.get(), img1.get()));
588 }
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000589 }
caseq26337e92014-06-30 12:14:52 -0700590
591 TestPictureTypefaceSerialization(reporter);
Robert Phillipsa8cdbd72018-07-17 12:30:40 -0400592
593 // Test SkLightingShader/NormalMapSource serialization
594 {
595 const int kTexSize = 2;
596
597 SkLights::Builder builder;
598
599 builder.add(SkLights::Light::MakeDirectional(SkColor3f::Make(1.0f, 1.0f, 1.0f),
600 SkVector3::Make(1.0f, 0.0f, 0.0f)));
601 builder.setAmbientLightColor(SkColor3f::Make(0.2f, 0.2f, 0.2f));
602
603 sk_sp<SkLights> fLights = builder.finish();
604
Mike Kleinea3f0142019-03-20 11:12:10 -0500605 SkBitmap diffuse = ToolUtils::create_checkerboard_bitmap(
606 kTexSize, kTexSize, 0x00000000, ToolUtils::color_to_565(0xFF804020), 8);
Robert Phillipsa8cdbd72018-07-17 12:30:40 -0400607
608 SkRect bitmapBounds = SkRect::MakeIWH(diffuse.width(), diffuse.height());
609
610 SkMatrix matrix;
611 SkRect r = SkRect::MakeWH(SkIntToScalar(kTexSize), SkIntToScalar(kTexSize));
612 matrix.setRectToRect(bitmapBounds, r, SkMatrix::kFill_ScaleToFit);
613
614 SkMatrix ctm;
615 ctm.setRotate(45);
616 SkBitmap normals;
617 normals.allocN32Pixels(kTexSize, kTexSize);
618
Mike Kleinea3f0142019-03-20 11:12:10 -0500619 ToolUtils::create_frustum_normal_map(&normals, SkIRect::MakeWH(kTexSize, kTexSize));
Mike Reed50acf8f2019-04-08 13:20:23 -0400620 sk_sp<SkShader> normalMap = normals.makeShader(&matrix);
Robert Phillipsa8cdbd72018-07-17 12:30:40 -0400621 sk_sp<SkNormalSource> normalSource = SkNormalSource::MakeFromNormalMap(std::move(normalMap),
622 ctm);
Mike Reed50acf8f2019-04-08 13:20:23 -0400623 sk_sp<SkShader> diffuseShader = diffuse.makeShader(&matrix);
Robert Phillipsa8cdbd72018-07-17 12:30:40 -0400624
625 sk_sp<SkShader> lightingShader = SkLightingShader::Make(diffuseShader,
626 normalSource,
627 fLights);
628 sk_sp<SkShader>(TestFlattenableSerialization(as_SB(lightingShader.get()), true, reporter));
629
630 lightingShader = SkLightingShader::Make(std::move(diffuseShader),
631 nullptr,
632 fLights);
633 sk_sp<SkShader>(TestFlattenableSerialization(as_SB(lightingShader.get()), true, reporter));
634
635 lightingShader = SkLightingShader::Make(nullptr,
636 std::move(normalSource),
637 fLights);
638 sk_sp<SkShader>(TestFlattenableSerialization(as_SB(lightingShader.get()), true, reporter));
639
640 lightingShader = SkLightingShader::Make(nullptr,
641 nullptr,
642 fLights);
643 sk_sp<SkShader>(TestFlattenableSerialization(as_SB(lightingShader.get()), true, reporter));
644 }
commit-bot@chromium.org02512882013-10-31 18:37:50 +0000645}
reedf70b5312016-03-04 16:36:20 -0800646
647///////////////////////////////////////////////////////////////////////////////////////////////////
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500648#include "include/core/SkAnnotation.h"
reedf70b5312016-03-04 16:36:20 -0800649
reedca2622b2016-03-18 07:25:55 -0700650static sk_sp<SkPicture> copy_picture_via_serialization(SkPicture* src) {
reedf70b5312016-03-04 16:36:20 -0800651 SkDynamicMemoryWStream wstream;
652 src->serialize(&wstream);
Ben Wagner145dbcd2016-11-03 14:40:50 -0400653 std::unique_ptr<SkStreamAsset> rstream(wstream.detachAsStream());
654 return SkPicture::MakeFromStream(rstream.get());
reedf70b5312016-03-04 16:36:20 -0800655}
656
657struct AnnotationRec {
658 const SkRect fRect;
659 const char* fKey;
bungeman38d909e2016-08-02 14:40:46 -0700660 sk_sp<SkData> fValue;
reedf70b5312016-03-04 16:36:20 -0800661};
662
663class TestAnnotationCanvas : public SkCanvas {
664 skiatest::Reporter* fReporter;
665 const AnnotationRec* fRec;
666 int fCount;
667 int fCurrIndex;
668
669public:
670 TestAnnotationCanvas(skiatest::Reporter* reporter, const AnnotationRec rec[], int count)
671 : SkCanvas(100, 100)
672 , fReporter(reporter)
673 , fRec(rec)
674 , fCount(count)
675 , fCurrIndex(0)
676 {}
677
678 ~TestAnnotationCanvas() {
679 REPORTER_ASSERT(fReporter, fCount == fCurrIndex);
680 }
681
682protected:
683 void onDrawAnnotation(const SkRect& rect, const char key[], SkData* value) {
684 REPORTER_ASSERT(fReporter, fCurrIndex < fCount);
685 REPORTER_ASSERT(fReporter, rect == fRec[fCurrIndex].fRect);
686 REPORTER_ASSERT(fReporter, !strcmp(key, fRec[fCurrIndex].fKey));
bungeman38d909e2016-08-02 14:40:46 -0700687 REPORTER_ASSERT(fReporter, value->equals(fRec[fCurrIndex].fValue.get()));
reedf70b5312016-03-04 16:36:20 -0800688 fCurrIndex += 1;
689 }
690};
691
692/*
693 * Test the 3 annotation types by recording them into a picture, serializing, and then playing
694 * them back into another canvas.
695 */
696DEF_TEST(Annotations, reporter) {
697 SkPictureRecorder recorder;
698 SkCanvas* recordingCanvas = recorder.beginRecording(SkRect::MakeWH(100, 100));
halcanary9d524f22016-03-29 09:03:52 -0700699
reedf70b5312016-03-04 16:36:20 -0800700 const char* str0 = "rect-with-url";
701 const SkRect r0 = SkRect::MakeWH(10, 10);
bungeman38d909e2016-08-02 14:40:46 -0700702 sk_sp<SkData> d0(SkData::MakeWithCString(str0));
703 SkAnnotateRectWithURL(recordingCanvas, r0, d0.get());
halcanary9d524f22016-03-29 09:03:52 -0700704
reedf70b5312016-03-04 16:36:20 -0800705 const char* str1 = "named-destination";
706 const SkRect r1 = SkRect::MakeXYWH(5, 5, 0, 0); // collapsed to a point
bungeman38d909e2016-08-02 14:40:46 -0700707 sk_sp<SkData> d1(SkData::MakeWithCString(str1));
708 SkAnnotateNamedDestination(recordingCanvas, {r1.x(), r1.y()}, d1.get());
halcanary9d524f22016-03-29 09:03:52 -0700709
reedf70b5312016-03-04 16:36:20 -0800710 const char* str2 = "link-to-destination";
711 const SkRect r2 = SkRect::MakeXYWH(20, 20, 5, 6);
bungeman38d909e2016-08-02 14:40:46 -0700712 sk_sp<SkData> d2(SkData::MakeWithCString(str2));
713 SkAnnotateLinkToDestination(recordingCanvas, r2, d2.get());
reedf70b5312016-03-04 16:36:20 -0800714
715 const AnnotationRec recs[] = {
bungeman38d909e2016-08-02 14:40:46 -0700716 { r0, SkAnnotationKeys::URL_Key(), std::move(d0) },
717 { r1, SkAnnotationKeys::Define_Named_Dest_Key(), std::move(d1) },
718 { r2, SkAnnotationKeys::Link_Named_Dest_Key(), std::move(d2) },
reedf70b5312016-03-04 16:36:20 -0800719 };
720
reedca2622b2016-03-18 07:25:55 -0700721 sk_sp<SkPicture> pict0(recorder.finishRecordingAsPicture());
722 sk_sp<SkPicture> pict1(copy_picture_via_serialization(pict0.get()));
reedf70b5312016-03-04 16:36:20 -0800723
724 TestAnnotationCanvas canvas(reporter, recs, SK_ARRAY_COUNT(recs));
725 canvas.drawPicture(pict1);
726}
Mike Reed25325842018-03-14 09:52:02 -0400727
728DEF_TEST(WriteBuffer_storage, reporter) {
729 enum {
730 kSize = 32
731 };
732 int32_t storage[kSize/4];
733 char src[kSize];
734 sk_bzero(src, kSize);
735
736 SkBinaryWriteBuffer writer(storage, kSize);
737 REPORTER_ASSERT(reporter, writer.usingInitialStorage());
738 REPORTER_ASSERT(reporter, writer.bytesWritten() == 0);
739 writer.write(src, kSize - 4);
740 REPORTER_ASSERT(reporter, writer.usingInitialStorage());
741 REPORTER_ASSERT(reporter, writer.bytesWritten() == kSize - 4);
742 writer.writeInt(0);
743 REPORTER_ASSERT(reporter, writer.usingInitialStorage());
744 REPORTER_ASSERT(reporter, writer.bytesWritten() == kSize);
745
746 writer.reset(storage, kSize-4);
747 REPORTER_ASSERT(reporter, writer.usingInitialStorage());
748 REPORTER_ASSERT(reporter, writer.bytesWritten() == 0);
749 writer.write(src, kSize - 4);
750 REPORTER_ASSERT(reporter, writer.usingInitialStorage());
751 REPORTER_ASSERT(reporter, writer.bytesWritten() == kSize - 4);
752 writer.writeInt(0);
753 REPORTER_ASSERT(reporter, !writer.usingInitialStorage()); // this is the change
754 REPORTER_ASSERT(reporter, writer.bytesWritten() == kSize);
755}
Khushal42f8bc42018-04-03 17:51:40 -0700756
757DEF_TEST(WriteBuffer_external_memory_textblob, reporter) {
Mike Reed2ed78202018-11-21 15:10:08 -0500758 SkFont font;
Khushal42f8bc42018-04-03 17:51:40 -0700759 font.setTypeface(SkTypeface::MakeDefault());
760
761 SkTextBlobBuilder builder;
762 int glyph_count = 5;
763 const auto& run = builder.allocRun(font, glyph_count, 1.2f, 2.3f);
764 // allocRun() allocates only the glyph buffer.
765 std::fill(run.glyphs, run.glyphs + glyph_count, 0);
766 auto blob = builder.make();
767 SkSerialProcs procs;
768 SkAutoTMalloc<uint8_t> storage;
769 size_t blob_size = 0u;
770 size_t storage_size = 0u;
771
772 blob_size = SkAlign4(blob->serialize(procs)->size());
773 REPORTER_ASSERT(reporter, blob_size > 4u);
774 storage_size = blob_size - 4;
775 storage.realloc(storage_size);
776 REPORTER_ASSERT(reporter, blob->serialize(procs, storage.get(), storage_size) == 0u);
777 storage_size = blob_size;
778 storage.realloc(storage_size);
779 REPORTER_ASSERT(reporter, blob->serialize(procs, storage.get(), storage_size) != 0u);
780}
781
782DEF_TEST(WriteBuffer_external_memory_flattenable, reporter) {
783 SkScalar intervals[] = {1.f, 1.f};
784 auto path_effect = SkDashPathEffect::Make(intervals, 2, 0);
785 size_t path_size = SkAlign4(path_effect->serialize()->size());
786 REPORTER_ASSERT(reporter, path_size > 4u);
787 SkAutoTMalloc<uint8_t> storage;
788
789 size_t storage_size = path_size - 4;
790 storage.realloc(storage_size);
791 REPORTER_ASSERT(reporter, path_effect->serialize(storage.get(), storage_size) == 0u);
792
793 storage_size = path_size;
794 storage.realloc(storage_size);
795 REPORTER_ASSERT(reporter, path_effect->serialize(storage.get(), storage_size) != 0u);
796}