blob: 979d7b12ca26abe2f4fc839e8fbe05eb0e4c1870 [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"
21#include "src/core/SkMakeUnique.h"
22#include "src/core/SkMatrixPriv.h"
23#include "src/core/SkNormalSource.h"
24#include "src/core/SkOSFile.h"
25#include "src/core/SkPicturePriv.h"
26#include "src/core/SkReadBuffer.h"
27#include "src/core/SkWriteBuffer.h"
28#include "src/shaders/SkLightingShader.h"
29#include "src/shaders/SkShaderBase.h"
30#include "tests/Test.h"
31#include "tools/Resources.h"
32#include "tools/ToolUtils.h"
commit-bot@chromium.org02512882013-10-31 18:37:50 +000033
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000034static const uint32_t kArraySize = 64;
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +000035static const int kBitmapSize = 256;
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000036
Cary Clark6d6d6032017-10-20 12:14:33 -040037class SerializationTest {
38public:
39
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000040template<typename T>
41static void TestAlignment(T* testObj, skiatest::Reporter* reporter) {
42 // Test memory read/write functions directly
43 unsigned char dataWritten[1024];
44 size_t bytesWrittenToMemory = testObj->writeToMemory(dataWritten);
45 REPORTER_ASSERT(reporter, SkAlign4(bytesWrittenToMemory) == bytesWrittenToMemory);
46 size_t bytesReadFromMemory = testObj->readFromMemory(dataWritten, bytesWrittenToMemory);
47 REPORTER_ASSERT(reporter, SkAlign4(bytesReadFromMemory) == bytesReadFromMemory);
48}
Cary Clark6d6d6032017-10-20 12:14:33 -040049};
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000050
51template<typename T> struct SerializationUtils {
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +000052 // Generic case for flattenables
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000053 static void Write(SkWriteBuffer& writer, const T* flattenable) {
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +000054 writer.writeFlattenable(flattenable);
55 }
Mike Reedfadbfcd2017-12-06 16:09:20 -050056 static void Read(SkReadBuffer& reader, T** flattenable) {
mtklein3b375452016-04-04 14:57:19 -070057 *flattenable = (T*)reader.readFlattenable(T::GetFlattenableType());
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +000058 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000059};
60
61template<> struct SerializationUtils<SkMatrix> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000062 static void Write(SkWriteBuffer& writer, const SkMatrix* matrix) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000063 writer.writeMatrix(*matrix);
64 }
Mike Reedfadbfcd2017-12-06 16:09:20 -050065 static void Read(SkReadBuffer& reader, SkMatrix* matrix) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000066 reader.readMatrix(matrix);
67 }
68};
69
70template<> struct SerializationUtils<SkPath> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000071 static void Write(SkWriteBuffer& writer, const SkPath* path) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000072 writer.writePath(*path);
73 }
Mike Reedfadbfcd2017-12-06 16:09:20 -050074 static void Read(SkReadBuffer& reader, SkPath* path) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000075 reader.readPath(path);
76 }
77};
78
79template<> struct SerializationUtils<SkRegion> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000080 static void Write(SkWriteBuffer& writer, const SkRegion* region) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000081 writer.writeRegion(*region);
82 }
Mike Reedfadbfcd2017-12-06 16:09:20 -050083 static void Read(SkReadBuffer& reader, SkRegion* region) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000084 reader.readRegion(region);
85 }
86};
87
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +000088template<> struct SerializationUtils<SkString> {
89 static void Write(SkWriteBuffer& writer, const SkString* string) {
90 writer.writeString(string->c_str());
91 }
Mike Reedfadbfcd2017-12-06 16:09:20 -050092 static void Read(SkReadBuffer& reader, SkString* string) {
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +000093 reader.readString(string);
94 }
95};
96
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000097template<> struct SerializationUtils<unsigned char> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000098 static void Write(SkWriteBuffer& writer, unsigned char* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +000099 writer.writeByteArray(data, arraySize);
sugoi@google.com305f78e2013-11-04 16:18:15 +0000100 }
Mike Reedfadbfcd2017-12-06 16:09:20 -0500101 static bool Read(SkReadBuffer& reader, unsigned char* data, uint32_t arraySize) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000102 return reader.readByteArray(data, arraySize);
103 }
104};
commit-bot@chromium.org02512882013-10-31 18:37:50 +0000105
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000106template<> struct SerializationUtils<SkColor> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000107 static void Write(SkWriteBuffer& writer, SkColor* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +0000108 writer.writeColorArray(data, arraySize);
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000109 }
Mike Reedfadbfcd2017-12-06 16:09:20 -0500110 static bool Read(SkReadBuffer& reader, SkColor* data, uint32_t arraySize) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000111 return reader.readColorArray(data, arraySize);
112 }
113};
sugoi@google.com305f78e2013-11-04 16:18:15 +0000114
brianosman97bbf822016-09-25 13:15:58 -0700115template<> struct SerializationUtils<SkColor4f> {
116 static void Write(SkWriteBuffer& writer, SkColor4f* data, uint32_t arraySize) {
117 writer.writeColor4fArray(data, arraySize);
118 }
Mike Reedfadbfcd2017-12-06 16:09:20 -0500119 static bool Read(SkReadBuffer& reader, SkColor4f* data, uint32_t arraySize) {
brianosman97bbf822016-09-25 13:15:58 -0700120 return reader.readColor4fArray(data, arraySize);
121 }
122};
123
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000124template<> struct SerializationUtils<int32_t> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000125 static void Write(SkWriteBuffer& writer, int32_t* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +0000126 writer.writeIntArray(data, arraySize);
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000127 }
Mike Reedfadbfcd2017-12-06 16:09:20 -0500128 static bool Read(SkReadBuffer& reader, int32_t* data, uint32_t arraySize) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000129 return reader.readIntArray(data, arraySize);
130 }
131};
sugoi@google.com305f78e2013-11-04 16:18:15 +0000132
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000133template<> struct SerializationUtils<SkPoint> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000134 static void Write(SkWriteBuffer& writer, SkPoint* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +0000135 writer.writePointArray(data, arraySize);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000136 }
Mike Reedfadbfcd2017-12-06 16:09:20 -0500137 static bool Read(SkReadBuffer& reader, SkPoint* data, uint32_t arraySize) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000138 return reader.readPointArray(data, arraySize);
139 }
140};
reed@google.com12a23862013-11-04 21:35:55 +0000141
Michael Ludwigea241402018-08-22 09:26:33 -0400142template<> struct SerializationUtils<SkPoint3> {
143 static void Write(SkWriteBuffer& writer, const SkPoint3* data) {
144 writer.writePoint3(*data);
145 }
146 static void Read(SkReadBuffer& reader, SkPoint3* data) {
147 reader.readPoint3(data);
148 }
149};
150
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000151template<> struct SerializationUtils<SkScalar> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000152 static void Write(SkWriteBuffer& writer, SkScalar* data, uint32_t arraySize) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000153 writer.writeScalarArray(data, arraySize);
154 }
Mike Reedfadbfcd2017-12-06 16:09:20 -0500155 static bool Read(SkReadBuffer& reader, SkScalar* data, uint32_t arraySize) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000156 return reader.readScalarArray(data, arraySize);
157 }
158};
reed@google.com12a23862013-11-04 21:35:55 +0000159
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000160template<typename T, bool testInvalid> struct SerializationTestUtils {
161 static void InvalidateData(unsigned char* data) {}
162};
163
164template<> struct SerializationTestUtils<SkString, true> {
165 static void InvalidateData(unsigned char* data) {
166 data[3] |= 0x80; // Reverse sign of 1st integer
167 }
168};
169
170template<typename T, bool testInvalid>
171static void TestObjectSerializationNoAlign(T* testObj, skiatest::Reporter* reporter) {
brianosmanfad98562016-05-04 11:06:28 -0700172 SkBinaryWriteBuffer writer;
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000173 SerializationUtils<T>::Write(writer, testObj);
174 size_t bytesWritten = writer.bytesWritten();
175 REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten);
reed@google.com12a23862013-11-04 21:35:55 +0000176
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000177 unsigned char dataWritten[1024];
178 writer.writeToMemory(dataWritten);
179
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000180 SerializationTestUtils<T, testInvalid>::InvalidateData(dataWritten);
181
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000182 // Make sure this fails when it should (test with smaller size, but still multiple of 4)
Mike Reedfadbfcd2017-12-06 16:09:20 -0500183 SkReadBuffer buffer(dataWritten, bytesWritten - 4);
commit-bot@chromium.org8f457e32013-11-08 19:22:57 +0000184 T obj;
185 SerializationUtils<T>::Read(buffer, &obj);
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000186 REPORTER_ASSERT(reporter, !buffer.isValid());
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000187
188 // Make sure this succeeds when it should
Mike Reedfadbfcd2017-12-06 16:09:20 -0500189 SkReadBuffer buffer2(dataWritten, bytesWritten);
Robert Phillipsb2526042016-09-26 09:00:36 -0400190 size_t offsetBefore = buffer2.offset();
commit-bot@chromium.org8f457e32013-11-08 19:22:57 +0000191 T obj2;
192 SerializationUtils<T>::Read(buffer2, &obj2);
Robert Phillipsb2526042016-09-26 09:00:36 -0400193 size_t offsetAfter = buffer2.offset();
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000194 // This should have succeeded, since there are enough bytes to read this
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000195 REPORTER_ASSERT(reporter, buffer2.isValid() == !testInvalid);
196 // Note: This following test should always succeed, regardless of whether the buffer is valid,
197 // 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 -0400198 REPORTER_ASSERT(reporter, offsetAfter - offsetBefore == bytesWritten);
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000199}
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000200
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000201template<typename T>
202static void TestObjectSerialization(T* testObj, skiatest::Reporter* reporter) {
203 TestObjectSerializationNoAlign<T, false>(testObj, reporter);
Cary Clark6d6d6032017-10-20 12:14:33 -0400204 SerializationTest::TestAlignment(testObj, reporter);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000205}
206
207template<typename T>
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000208static T* TestFlattenableSerialization(T* testObj, bool shouldSucceed,
209 skiatest::Reporter* reporter) {
brianosmanfad98562016-05-04 11:06:28 -0700210 SkBinaryWriteBuffer writer;
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000211 SerializationUtils<T>::Write(writer, testObj);
212 size_t bytesWritten = writer.bytesWritten();
213 REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten);
214
dvonbeck8811e402016-06-16 12:39:25 -0700215 SkASSERT(bytesWritten <= 4096);
senorblanco91c395a2014-09-25 15:51:35 -0700216 unsigned char dataWritten[4096];
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000217 writer.writeToMemory(dataWritten);
218
219 // Make sure this fails when it should (test with smaller size, but still multiple of 4)
Mike Reedfadbfcd2017-12-06 16:09:20 -0500220 SkReadBuffer buffer(dataWritten, bytesWritten - 4);
halcanary96fcdcc2015-08-27 07:41:13 -0700221 T* obj = nullptr;
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000222 SerializationUtils<T>::Read(buffer, &obj);
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000223 REPORTER_ASSERT(reporter, !buffer.isValid());
halcanary96fcdcc2015-08-27 07:41:13 -0700224 REPORTER_ASSERT(reporter, nullptr == obj);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000225
226 // Make sure this succeeds when it should
Mike Reedfadbfcd2017-12-06 16:09:20 -0500227 SkReadBuffer buffer2(dataWritten, bytesWritten);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000228 const unsigned char* peekBefore = static_cast<const unsigned char*>(buffer2.skip(0));
halcanary96fcdcc2015-08-27 07:41:13 -0700229 T* obj2 = nullptr;
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000230 SerializationUtils<T>::Read(buffer2, &obj2);
231 const unsigned char* peekAfter = static_cast<const unsigned char*>(buffer2.skip(0));
232 if (shouldSucceed) {
233 // This should have succeeded, since there are enough bytes to read this
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000234 REPORTER_ASSERT(reporter, buffer2.isValid());
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000235 REPORTER_ASSERT(reporter, static_cast<size_t>(peekAfter - peekBefore) == bytesWritten);
bsalomon49f085d2014-09-05 13:34:00 -0700236 REPORTER_ASSERT(reporter, obj2);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000237 } else {
238 // If the deserialization was supposed to fail, make sure it did
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000239 REPORTER_ASSERT(reporter, !buffer.isValid());
halcanary96fcdcc2015-08-27 07:41:13 -0700240 REPORTER_ASSERT(reporter, nullptr == obj2);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000241 }
242
243 return obj2; // Return object to perform further validity tests on it
244}
245
246template<typename T>
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000247static void TestArraySerialization(T* data, skiatest::Reporter* reporter) {
brianosmanfad98562016-05-04 11:06:28 -0700248 SkBinaryWriteBuffer writer;
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000249 SerializationUtils<T>::Write(writer, data, kArraySize);
250 size_t bytesWritten = writer.bytesWritten();
251 // This should write the length (in 4 bytes) and the array
252 REPORTER_ASSERT(reporter, (4 + kArraySize * sizeof(T)) == bytesWritten);
253
brianosman97bbf822016-09-25 13:15:58 -0700254 unsigned char dataWritten[2048];
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000255 writer.writeToMemory(dataWritten);
256
257 // Make sure this fails when it should
Mike Reedfadbfcd2017-12-06 16:09:20 -0500258 SkReadBuffer buffer(dataWritten, bytesWritten);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000259 T dataRead[kArraySize];
260 bool success = SerializationUtils<T>::Read(buffer, dataRead, kArraySize / 2);
261 // This should have failed, since the provided size was too small
262 REPORTER_ASSERT(reporter, !success);
263
264 // Make sure this succeeds when it should
Mike Reedfadbfcd2017-12-06 16:09:20 -0500265 SkReadBuffer buffer2(dataWritten, bytesWritten);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000266 success = SerializationUtils<T>::Read(buffer2, dataRead, kArraySize);
267 // This should have succeeded, since there are enough bytes to read this
268 REPORTER_ASSERT(reporter, success);
269}
270
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000271static void TestBitmapSerialization(const SkBitmap& validBitmap,
272 const SkBitmap& invalidBitmap,
273 bool shouldSucceed,
274 skiatest::Reporter* reporter) {
reed9ce9d672016-03-17 10:51:11 -0700275 sk_sp<SkImage> validImage(SkImage::MakeFromBitmap(validBitmap));
Michael Ludwig55edb502019-08-05 10:41:10 -0400276 sk_sp<SkImageFilter> validBitmapSource(SkImageFilters::Image(std::move(validImage)));
reed9ce9d672016-03-17 10:51:11 -0700277 sk_sp<SkImage> invalidImage(SkImage::MakeFromBitmap(invalidBitmap));
Michael Ludwig55edb502019-08-05 10:41:10 -0400278 sk_sp<SkImageFilter> invalidBitmapSource(SkImageFilters::Image(std::move(invalidImage)));
reedcfb6bdf2016-03-29 11:32:50 -0700279 sk_sp<SkImageFilter> xfermodeImageFilter(
Michael Ludwig55edb502019-08-05 10:41:10 -0400280 SkImageFilters::Xfermode(SkBlendMode::kSrcOver,
281 std::move(invalidBitmapSource),
282 std::move(validBitmapSource), nullptr));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000283
Mike Reed5e257172016-11-01 11:22:05 -0400284 sk_sp<SkImageFilter> deserializedFilter(
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000285 TestFlattenableSerialization<SkImageFilter>(
reedcfb6bdf2016-03-29 11:32:50 -0700286 xfermodeImageFilter.get(), shouldSucceed, reporter));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000287
288 // Try to render a small bitmap using the invalid deserialized filter
289 // to make sure we don't crash while trying to render it
290 if (shouldSucceed) {
291 SkBitmap bitmap;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000292 bitmap.allocN32Pixels(24, 24);
293 SkCanvas canvas(bitmap);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000294 canvas.clear(0x00000000);
295 SkPaint paint;
296 paint.setImageFilter(deserializedFilter);
297 canvas.clipRect(SkRect::MakeXYWH(0, 0, SkIntToScalar(24), SkIntToScalar(24)));
298 canvas.drawBitmap(bitmap, 0, 0, &paint);
299 }
300}
301
senorblanco91c395a2014-09-25 15:51:35 -0700302static void TestColorFilterSerialization(skiatest::Reporter* reporter) {
303 uint8_t table[256];
304 for (int i = 0; i < 256; ++i) {
305 table[i] = (i * 41) % 256;
306 }
reedd053ce92016-03-22 10:17:23 -0700307 auto colorFilter(SkTableColorFilter::Make(table));
Hal Canary342b7ac2016-11-04 11:49:42 -0400308 sk_sp<SkColorFilter> copy(
senorblanco91c395a2014-09-25 15:51:35 -0700309 TestFlattenableSerialization<SkColorFilter>(colorFilter.get(), true, reporter));
310}
311
caseq26337e92014-06-30 12:14:52 -0700312static SkBitmap draw_picture(SkPicture& picture) {
313 SkBitmap bitmap;
halcanary9d524f22016-03-29 09:03:52 -0700314 bitmap.allocN32Pixels(SkScalarCeilToInt(picture.cullRect().width()),
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700315 SkScalarCeilToInt(picture.cullRect().height()));
caseq26337e92014-06-30 12:14:52 -0700316 SkCanvas canvas(bitmap);
robertphillipsc5ba71d2014-09-04 08:42:50 -0700317 picture.playback(&canvas);
caseq26337e92014-06-30 12:14:52 -0700318 return bitmap;
319}
320
321static void compare_bitmaps(skiatest::Reporter* reporter,
322 const SkBitmap& b1, const SkBitmap& b2) {
323 REPORTER_ASSERT(reporter, b1.width() == b2.width());
324 REPORTER_ASSERT(reporter, b1.height() == b2.height());
caseq26337e92014-06-30 12:14:52 -0700325
326 if ((b1.width() != b2.width()) ||
327 (b1.height() != b2.height())) {
328 return;
329 }
330
331 int pixelErrors = 0;
332 for (int y = 0; y < b2.height(); ++y) {
333 for (int x = 0; x < b2.width(); ++x) {
334 if (b1.getColor(x, y) != b2.getColor(x, y))
335 ++pixelErrors;
336 }
337 }
338 REPORTER_ASSERT(reporter, 0 == pixelErrors);
339}
bungeman13b9c952016-05-12 10:09:30 -0700340static void serialize_and_compare_typeface(sk_sp<SkTypeface> typeface, const char* text,
bungeman41868fe2015-05-20 09:21:04 -0700341 skiatest::Reporter* reporter)
342{
Hal Canary3560ea72019-01-08 13:01:58 -0500343 // Create a font with the typeface.
caseq26337e92014-06-30 12:14:52 -0700344 SkPaint paint;
345 paint.setColor(SK_ColorGRAY);
Hal Canary3560ea72019-01-08 13:01:58 -0500346 SkFont font(std::move(typeface), 30);
caseq26337e92014-06-30 12:14:52 -0700347
348 // Paint some text.
349 SkPictureRecorder recorder;
350 SkIRect canvasRect = SkIRect::MakeWH(kBitmapSize, kBitmapSize);
halcanary9d524f22016-03-29 09:03:52 -0700351 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(canvasRect.width()),
352 SkIntToScalar(canvasRect.height()),
halcanary96fcdcc2015-08-27 07:41:13 -0700353 nullptr, 0);
caseq26337e92014-06-30 12:14:52 -0700354 canvas->drawColor(SK_ColorWHITE);
Hal Canary3560ea72019-01-08 13:01:58 -0500355 canvas->drawString(text, 24, 32, font, paint);
reedca2622b2016-03-18 07:25:55 -0700356 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
caseq26337e92014-06-30 12:14:52 -0700357
358 // Serlialize picture and create its clone from stream.
359 SkDynamicMemoryWStream stream;
360 picture->serialize(&stream);
Ben Wagner145dbcd2016-11-03 14:40:50 -0400361 std::unique_ptr<SkStream> inputStream(stream.detachAsStream());
reedca2622b2016-03-18 07:25:55 -0700362 sk_sp<SkPicture> loadedPicture(SkPicture::MakeFromStream(inputStream.get()));
caseq26337e92014-06-30 12:14:52 -0700363
364 // Draw both original and clone picture and compare bitmaps -- they should be identical.
365 SkBitmap origBitmap = draw_picture(*picture);
366 SkBitmap destBitmap = draw_picture(*loadedPicture);
367 compare_bitmaps(reporter, origBitmap, destBitmap);
368}
369
bungeman41868fe2015-05-20 09:21:04 -0700370static void TestPictureTypefaceSerialization(skiatest::Reporter* reporter) {
371 {
372 // Load typeface from file to test CreateFromFile with index.
Mike Reed271d1d92018-09-03 21:10:10 -0400373 auto typeface = MakeResourceAsTypeface("fonts/test.ttc", 1);
bungeman41868fe2015-05-20 09:21:04 -0700374 if (!typeface) {
halcanary7d571242016-02-24 17:59:16 -0800375 INFOF(reporter, "Could not run fontstream test because test.ttc not found.");
bungeman41868fe2015-05-20 09:21:04 -0700376 } else {
bungeman13b9c952016-05-12 10:09:30 -0700377 serialize_and_compare_typeface(std::move(typeface), "A!", reporter);
bungeman41868fe2015-05-20 09:21:04 -0700378 }
379 }
380
381 {
382 // Load typeface as stream to create with axis settings.
Mike Reed0933bc92017-12-09 01:27:41 +0000383 std::unique_ptr<SkStreamAsset> distortable(GetResourceAsStream("fonts/Distortable.ttf"));
bungeman41868fe2015-05-20 09:21:04 -0700384 if (!distortable) {
halcanary7d571242016-02-24 17:59:16 -0800385 INFOF(reporter, "Could not run fontstream test because Distortable.ttf not found.");
bungeman41868fe2015-05-20 09:21:04 -0700386 } else {
387 SkFixed axis = SK_FixedSqrt2;
bungeman13b9c952016-05-12 10:09:30 -0700388 sk_sp<SkTypeface> typeface(SkTypeface::MakeFromFontData(
bungemanf93d7112016-09-16 06:24:20 -0700389 skstd::make_unique<SkFontData>(std::move(distortable), 0, &axis, 1)));
bungeman41868fe2015-05-20 09:21:04 -0700390 if (!typeface) {
halcanary7d571242016-02-24 17:59:16 -0800391 INFOF(reporter, "Could not run fontstream test because Distortable.ttf not created.");
bungeman41868fe2015-05-20 09:21:04 -0700392 } else {
Hal Canary3560ea72019-01-08 13:01:58 -0500393 serialize_and_compare_typeface(std::move(typeface), "ab", reporter);
bungeman41868fe2015-05-20 09:21:04 -0700394 }
395 }
396 }
397}
398
reed84825042014-09-02 12:50:45 -0700399static void setup_bitmap_for_canvas(SkBitmap* bitmap) {
400 bitmap->allocN32Pixels(kBitmapSize, kBitmapSize);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000401}
402
reed84825042014-09-02 12:50:45 -0700403static void make_checkerboard_bitmap(SkBitmap& bitmap) {
404 setup_bitmap_for_canvas(&bitmap);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000405
406 SkCanvas canvas(bitmap);
407 canvas.clear(0x00000000);
408 SkPaint darkPaint;
409 darkPaint.setColor(0xFF804020);
410 SkPaint lightPaint;
411 lightPaint.setColor(0xFF244484);
412 const int i = kBitmapSize / 8;
413 const SkScalar f = SkIntToScalar(i);
414 for (int y = 0; y < kBitmapSize; y += i) {
415 for (int x = 0; x < kBitmapSize; x += i) {
416 canvas.save();
417 canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
418 canvas.drawRect(SkRect::MakeXYWH(0, 0, f, f), darkPaint);
419 canvas.drawRect(SkRect::MakeXYWH(f, 0, f, f), lightPaint);
420 canvas.drawRect(SkRect::MakeXYWH(0, f, f, f), lightPaint);
421 canvas.drawRect(SkRect::MakeXYWH(f, f, f, f), darkPaint);
422 canvas.restore();
423 }
424 }
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000425}
426
reed84825042014-09-02 12:50:45 -0700427static void draw_something(SkCanvas* canvas) {
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000428 SkPaint paint;
429 SkBitmap bitmap;
reed84825042014-09-02 12:50:45 -0700430 make_checkerboard_bitmap(bitmap);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000431
432 canvas->save();
433 canvas->scale(0.5f, 0.5f);
halcanary96fcdcc2015-08-27 07:41:13 -0700434 canvas->drawBitmap(bitmap, 0, 0, nullptr);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000435 canvas->restore();
436
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000437 paint.setAntiAlias(true);
438
439 paint.setColor(SK_ColorRED);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000440 canvas->drawCircle(SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/3), paint);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000441 paint.setColor(SK_ColorBLACK);
Mike Reed1af9b482019-01-07 11:01:57 -0500442
443 SkFont font;
444 font.setSize(kBitmapSize/3);
445 canvas->drawString("Picture", SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/4), font, paint);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000446}
447
Ben Wagner7825d492019-07-10 16:27:34 -0400448static sk_sp<SkImage> render(const SkPicture& p) {
449 auto surf = SkSurface::MakeRasterN32Premul(SkScalarRoundToInt(p.cullRect().width()),
450 SkScalarRoundToInt(p.cullRect().height()));
451 if (!surf) {
452 return nullptr; // bounds are empty?
453 }
454 surf->getCanvas()->clear(SK_ColorWHITE);
455 p.playback(surf->getCanvas());
456 return surf->makeImageSnapshot();
457}
458
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000459DEF_TEST(Serialization, reporter) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000460 // Test matrix serialization
461 {
462 SkMatrix matrix = SkMatrix::I();
463 TestObjectSerialization(&matrix, reporter);
caseq26337e92014-06-30 12:14:52 -0700464 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000465
Michael Ludwigea241402018-08-22 09:26:33 -0400466 // Test point3 serialization
467 {
468 SkPoint3 point;
469 TestObjectSerializationNoAlign<SkPoint3, false>(&point, reporter);
470 }
471
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000472 // Test path serialization
473 {
474 SkPath path;
475 TestObjectSerialization(&path, reporter);
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000476 }
sugoi@google.com305f78e2013-11-04 16:18:15 +0000477
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000478 // Test region serialization
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000479 {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000480 SkRegion region;
481 TestObjectSerialization(&region, reporter);
482 }
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000483
senorblanco91c395a2014-09-25 15:51:35 -0700484 // Test color filter serialization
485 {
486 TestColorFilterSerialization(reporter);
487 }
488
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000489 // Test string serialization
490 {
491 SkString string("string");
492 TestObjectSerializationNoAlign<SkString, false>(&string, reporter);
493 TestObjectSerializationNoAlign<SkString, true>(&string, reporter);
494 }
495
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000496 // Test rrect serialization
497 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000498 // SkRRect does not initialize anything.
499 // An uninitialized SkRRect can be serialized,
500 // but will branch on uninitialized data when deserialized.
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000501 SkRRect rrect;
rmistry@google.comd6bab022013-12-02 13:50:38 +0000502 SkRect rect = SkRect::MakeXYWH(1, 2, 20, 30);
503 SkVector corners[4] = { {1, 2}, {2, 3}, {3,4}, {4,5} };
504 rrect.setRectRadii(rect, corners);
Cary Clark6d6d6032017-10-20 12:14:33 -0400505 SerializationTest::TestAlignment(&rrect, reporter);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000506 }
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000507
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000508 // Test readByteArray
509 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000510 unsigned char data[kArraySize] = { 1, 2, 3 };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000511 TestArraySerialization(data, reporter);
512 }
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000513
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000514 // Test readColorArray
515 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000516 SkColor data[kArraySize] = { SK_ColorBLACK, SK_ColorWHITE, SK_ColorRED };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000517 TestArraySerialization(data, reporter);
518 }
519
brianosman97bbf822016-09-25 13:15:58 -0700520 // Test readColor4fArray
521 {
522 SkColor4f data[kArraySize] = {
523 SkColor4f::FromColor(SK_ColorBLACK),
524 SkColor4f::FromColor(SK_ColorWHITE),
525 SkColor4f::FromColor(SK_ColorRED),
526 { 1.f, 2.f, 4.f, 8.f }
527 };
528 TestArraySerialization(data, reporter);
529 }
530
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000531 // Test readIntArray
532 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000533 int32_t data[kArraySize] = { 1, 2, 4, 8 };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000534 TestArraySerialization(data, reporter);
535 }
536
537 // Test readPointArray
538 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000539 SkPoint data[kArraySize] = { {6, 7}, {42, 128} };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000540 TestArraySerialization(data, reporter);
541 }
542
543 // Test readScalarArray
544 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000545 SkScalar data[kArraySize] = { SK_Scalar1, SK_ScalarHalf, SK_ScalarMax };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000546 TestArraySerialization(data, reporter);
commit-bot@chromium.org02512882013-10-31 18:37:50 +0000547 }
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000548
549 // Test invalid deserializations
550 {
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000551 SkImageInfo info = SkImageInfo::MakeN32Premul(kBitmapSize, kBitmapSize);
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000552
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000553 SkBitmap validBitmap;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000554 validBitmap.setInfo(info);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000555
556 // Create a bitmap with a really large height
557 SkBitmap invalidBitmap;
reede5ea5002014-09-03 11:54:58 -0700558 invalidBitmap.setInfo(info.makeWH(info.width(), 1000000000));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000559
560 // The deserialization should succeed, and the rendering shouldn't crash,
561 // even when the device fails to initialize, due to its size
562 TestBitmapSerialization(validBitmap, invalidBitmap, true, reporter);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000563 }
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000564
565 // Test simple SkPicture serialization
566 {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000567 SkPictureRecorder recorder;
reed84825042014-09-02 12:50:45 -0700568 draw_something(recorder.beginRecording(SkIntToScalar(kBitmapSize),
569 SkIntToScalar(kBitmapSize),
halcanary96fcdcc2015-08-27 07:41:13 -0700570 nullptr, 0));
reedca2622b2016-03-18 07:25:55 -0700571 sk_sp<SkPicture> pict(recorder.finishRecordingAsPicture());
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000572
573 // Serialize picture
brianosmanfad98562016-05-04 11:06:28 -0700574 SkBinaryWriteBuffer writer;
Cary Clarkefd99cc2018-06-11 16:25:43 -0400575 SkPicturePriv::Flatten(pict, writer);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000576 size_t size = writer.bytesWritten();
commit-bot@chromium.org1e7ee992014-03-14 21:22:22 +0000577 SkAutoTMalloc<unsigned char> data(size);
578 writer.writeToMemory(static_cast<void*>(data.get()));
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000579
580 // Deserialize picture
Mike Reedfadbfcd2017-12-06 16:09:20 -0500581 SkReadBuffer reader(static_cast<void*>(data.get()), size);
Cary Clarkefd99cc2018-06-11 16:25:43 -0400582 sk_sp<SkPicture> readPict(SkPicturePriv::MakeFromBuffer(reader));
reedd921dbb2016-09-30 09:27:20 -0700583 REPORTER_ASSERT(reporter, reader.isValid());
bsalomon49f085d2014-09-05 13:34:00 -0700584 REPORTER_ASSERT(reporter, readPict.get());
Ben Wagner7825d492019-07-10 16:27:34 -0400585 sk_sp<SkImage> img0 = render(*pict);
586 sk_sp<SkImage> img1 = render(*readPict);
587 if (img0 && img1) {
588 REPORTER_ASSERT(reporter, ToolUtils::equal_pixels(img0.get(), img1.get()));
589 }
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000590 }
caseq26337e92014-06-30 12:14:52 -0700591
592 TestPictureTypefaceSerialization(reporter);
Robert Phillipsa8cdbd72018-07-17 12:30:40 -0400593
594 // Test SkLightingShader/NormalMapSource serialization
595 {
596 const int kTexSize = 2;
597
598 SkLights::Builder builder;
599
600 builder.add(SkLights::Light::MakeDirectional(SkColor3f::Make(1.0f, 1.0f, 1.0f),
601 SkVector3::Make(1.0f, 0.0f, 0.0f)));
602 builder.setAmbientLightColor(SkColor3f::Make(0.2f, 0.2f, 0.2f));
603
604 sk_sp<SkLights> fLights = builder.finish();
605
Mike Kleinea3f0142019-03-20 11:12:10 -0500606 SkBitmap diffuse = ToolUtils::create_checkerboard_bitmap(
607 kTexSize, kTexSize, 0x00000000, ToolUtils::color_to_565(0xFF804020), 8);
Robert Phillipsa8cdbd72018-07-17 12:30:40 -0400608
609 SkRect bitmapBounds = SkRect::MakeIWH(diffuse.width(), diffuse.height());
610
611 SkMatrix matrix;
612 SkRect r = SkRect::MakeWH(SkIntToScalar(kTexSize), SkIntToScalar(kTexSize));
613 matrix.setRectToRect(bitmapBounds, r, SkMatrix::kFill_ScaleToFit);
614
615 SkMatrix ctm;
616 ctm.setRotate(45);
617 SkBitmap normals;
618 normals.allocN32Pixels(kTexSize, kTexSize);
619
Mike Kleinea3f0142019-03-20 11:12:10 -0500620 ToolUtils::create_frustum_normal_map(&normals, SkIRect::MakeWH(kTexSize, kTexSize));
Mike Reed50acf8f2019-04-08 13:20:23 -0400621 sk_sp<SkShader> normalMap = normals.makeShader(&matrix);
Robert Phillipsa8cdbd72018-07-17 12:30:40 -0400622 sk_sp<SkNormalSource> normalSource = SkNormalSource::MakeFromNormalMap(std::move(normalMap),
623 ctm);
Mike Reed50acf8f2019-04-08 13:20:23 -0400624 sk_sp<SkShader> diffuseShader = diffuse.makeShader(&matrix);
Robert Phillipsa8cdbd72018-07-17 12:30:40 -0400625
626 sk_sp<SkShader> lightingShader = SkLightingShader::Make(diffuseShader,
627 normalSource,
628 fLights);
629 sk_sp<SkShader>(TestFlattenableSerialization(as_SB(lightingShader.get()), true, reporter));
630
631 lightingShader = SkLightingShader::Make(std::move(diffuseShader),
632 nullptr,
633 fLights);
634 sk_sp<SkShader>(TestFlattenableSerialization(as_SB(lightingShader.get()), true, reporter));
635
636 lightingShader = SkLightingShader::Make(nullptr,
637 std::move(normalSource),
638 fLights);
639 sk_sp<SkShader>(TestFlattenableSerialization(as_SB(lightingShader.get()), true, reporter));
640
641 lightingShader = SkLightingShader::Make(nullptr,
642 nullptr,
643 fLights);
644 sk_sp<SkShader>(TestFlattenableSerialization(as_SB(lightingShader.get()), true, reporter));
645 }
commit-bot@chromium.org02512882013-10-31 18:37:50 +0000646}
reedf70b5312016-03-04 16:36:20 -0800647
648///////////////////////////////////////////////////////////////////////////////////////////////////
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500649#include "include/core/SkAnnotation.h"
reedf70b5312016-03-04 16:36:20 -0800650
reedca2622b2016-03-18 07:25:55 -0700651static sk_sp<SkPicture> copy_picture_via_serialization(SkPicture* src) {
reedf70b5312016-03-04 16:36:20 -0800652 SkDynamicMemoryWStream wstream;
653 src->serialize(&wstream);
Ben Wagner145dbcd2016-11-03 14:40:50 -0400654 std::unique_ptr<SkStreamAsset> rstream(wstream.detachAsStream());
655 return SkPicture::MakeFromStream(rstream.get());
reedf70b5312016-03-04 16:36:20 -0800656}
657
658struct AnnotationRec {
659 const SkRect fRect;
660 const char* fKey;
bungeman38d909e2016-08-02 14:40:46 -0700661 sk_sp<SkData> fValue;
reedf70b5312016-03-04 16:36:20 -0800662};
663
664class TestAnnotationCanvas : public SkCanvas {
665 skiatest::Reporter* fReporter;
666 const AnnotationRec* fRec;
667 int fCount;
668 int fCurrIndex;
669
670public:
671 TestAnnotationCanvas(skiatest::Reporter* reporter, const AnnotationRec rec[], int count)
672 : SkCanvas(100, 100)
673 , fReporter(reporter)
674 , fRec(rec)
675 , fCount(count)
676 , fCurrIndex(0)
677 {}
678
679 ~TestAnnotationCanvas() {
680 REPORTER_ASSERT(fReporter, fCount == fCurrIndex);
681 }
682
683protected:
684 void onDrawAnnotation(const SkRect& rect, const char key[], SkData* value) {
685 REPORTER_ASSERT(fReporter, fCurrIndex < fCount);
686 REPORTER_ASSERT(fReporter, rect == fRec[fCurrIndex].fRect);
687 REPORTER_ASSERT(fReporter, !strcmp(key, fRec[fCurrIndex].fKey));
bungeman38d909e2016-08-02 14:40:46 -0700688 REPORTER_ASSERT(fReporter, value->equals(fRec[fCurrIndex].fValue.get()));
reedf70b5312016-03-04 16:36:20 -0800689 fCurrIndex += 1;
690 }
691};
692
693/*
694 * Test the 3 annotation types by recording them into a picture, serializing, and then playing
695 * them back into another canvas.
696 */
697DEF_TEST(Annotations, reporter) {
698 SkPictureRecorder recorder;
699 SkCanvas* recordingCanvas = recorder.beginRecording(SkRect::MakeWH(100, 100));
halcanary9d524f22016-03-29 09:03:52 -0700700
reedf70b5312016-03-04 16:36:20 -0800701 const char* str0 = "rect-with-url";
702 const SkRect r0 = SkRect::MakeWH(10, 10);
bungeman38d909e2016-08-02 14:40:46 -0700703 sk_sp<SkData> d0(SkData::MakeWithCString(str0));
704 SkAnnotateRectWithURL(recordingCanvas, r0, d0.get());
halcanary9d524f22016-03-29 09:03:52 -0700705
reedf70b5312016-03-04 16:36:20 -0800706 const char* str1 = "named-destination";
707 const SkRect r1 = SkRect::MakeXYWH(5, 5, 0, 0); // collapsed to a point
bungeman38d909e2016-08-02 14:40:46 -0700708 sk_sp<SkData> d1(SkData::MakeWithCString(str1));
709 SkAnnotateNamedDestination(recordingCanvas, {r1.x(), r1.y()}, d1.get());
halcanary9d524f22016-03-29 09:03:52 -0700710
reedf70b5312016-03-04 16:36:20 -0800711 const char* str2 = "link-to-destination";
712 const SkRect r2 = SkRect::MakeXYWH(20, 20, 5, 6);
bungeman38d909e2016-08-02 14:40:46 -0700713 sk_sp<SkData> d2(SkData::MakeWithCString(str2));
714 SkAnnotateLinkToDestination(recordingCanvas, r2, d2.get());
reedf70b5312016-03-04 16:36:20 -0800715
716 const AnnotationRec recs[] = {
bungeman38d909e2016-08-02 14:40:46 -0700717 { r0, SkAnnotationKeys::URL_Key(), std::move(d0) },
718 { r1, SkAnnotationKeys::Define_Named_Dest_Key(), std::move(d1) },
719 { r2, SkAnnotationKeys::Link_Named_Dest_Key(), std::move(d2) },
reedf70b5312016-03-04 16:36:20 -0800720 };
721
reedca2622b2016-03-18 07:25:55 -0700722 sk_sp<SkPicture> pict0(recorder.finishRecordingAsPicture());
723 sk_sp<SkPicture> pict1(copy_picture_via_serialization(pict0.get()));
reedf70b5312016-03-04 16:36:20 -0800724
725 TestAnnotationCanvas canvas(reporter, recs, SK_ARRAY_COUNT(recs));
726 canvas.drawPicture(pict1);
727}
Mike Reed25325842018-03-14 09:52:02 -0400728
729DEF_TEST(WriteBuffer_storage, reporter) {
730 enum {
731 kSize = 32
732 };
733 int32_t storage[kSize/4];
734 char src[kSize];
735 sk_bzero(src, kSize);
736
737 SkBinaryWriteBuffer writer(storage, kSize);
738 REPORTER_ASSERT(reporter, writer.usingInitialStorage());
739 REPORTER_ASSERT(reporter, writer.bytesWritten() == 0);
740 writer.write(src, kSize - 4);
741 REPORTER_ASSERT(reporter, writer.usingInitialStorage());
742 REPORTER_ASSERT(reporter, writer.bytesWritten() == kSize - 4);
743 writer.writeInt(0);
744 REPORTER_ASSERT(reporter, writer.usingInitialStorage());
745 REPORTER_ASSERT(reporter, writer.bytesWritten() == kSize);
746
747 writer.reset(storage, kSize-4);
748 REPORTER_ASSERT(reporter, writer.usingInitialStorage());
749 REPORTER_ASSERT(reporter, writer.bytesWritten() == 0);
750 writer.write(src, kSize - 4);
751 REPORTER_ASSERT(reporter, writer.usingInitialStorage());
752 REPORTER_ASSERT(reporter, writer.bytesWritten() == kSize - 4);
753 writer.writeInt(0);
754 REPORTER_ASSERT(reporter, !writer.usingInitialStorage()); // this is the change
755 REPORTER_ASSERT(reporter, writer.bytesWritten() == kSize);
756}
Khushal42f8bc42018-04-03 17:51:40 -0700757
758DEF_TEST(WriteBuffer_external_memory_textblob, reporter) {
Mike Reed2ed78202018-11-21 15:10:08 -0500759 SkFont font;
Khushal42f8bc42018-04-03 17:51:40 -0700760 font.setTypeface(SkTypeface::MakeDefault());
761
762 SkTextBlobBuilder builder;
763 int glyph_count = 5;
764 const auto& run = builder.allocRun(font, glyph_count, 1.2f, 2.3f);
765 // allocRun() allocates only the glyph buffer.
766 std::fill(run.glyphs, run.glyphs + glyph_count, 0);
767 auto blob = builder.make();
768 SkSerialProcs procs;
769 SkAutoTMalloc<uint8_t> storage;
770 size_t blob_size = 0u;
771 size_t storage_size = 0u;
772
773 blob_size = SkAlign4(blob->serialize(procs)->size());
774 REPORTER_ASSERT(reporter, blob_size > 4u);
775 storage_size = blob_size - 4;
776 storage.realloc(storage_size);
777 REPORTER_ASSERT(reporter, blob->serialize(procs, storage.get(), storage_size) == 0u);
778 storage_size = blob_size;
779 storage.realloc(storage_size);
780 REPORTER_ASSERT(reporter, blob->serialize(procs, storage.get(), storage_size) != 0u);
781}
782
783DEF_TEST(WriteBuffer_external_memory_flattenable, reporter) {
784 SkScalar intervals[] = {1.f, 1.f};
785 auto path_effect = SkDashPathEffect::Make(intervals, 2, 0);
786 size_t path_size = SkAlign4(path_effect->serialize()->size());
787 REPORTER_ASSERT(reporter, path_size > 4u);
788 SkAutoTMalloc<uint8_t> storage;
789
790 size_t storage_size = path_size - 4;
791 storage.realloc(storage_size);
792 REPORTER_ASSERT(reporter, path_effect->serialize(storage.get(), storage_size) == 0u);
793
794 storage_size = path_size;
795 storage.realloc(storage_size);
796 REPORTER_ASSERT(reporter, path_effect->serialize(storage.get(), storage_size) != 0u);
797}