blob: 7d4df08aea3ac569c4b45dbae2b69625b5fa715f [file] [log] [blame]
commit-bot@chromium.org02512882013-10-31 18:37:50 +00001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
egdaniel4132de72016-06-15 14:28:17 -07008#include "Resources.h"
dvonbeck8811e402016-06-16 12:39:25 -07009#include "SkAnnotationKeys.h"
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +000010#include "SkCanvas.h"
Khushal42f8bc42018-04-03 17:51:40 -070011#include "SkDashPathEffect.h"
bungeman41868fe2015-05-20 09:21:04 -070012#include "SkFixed.h"
13#include "SkFontDescriptor.h"
fmalita5598b632015-09-15 11:26:13 -070014#include "SkImage.h"
15#include "SkImageSource.h"
Robert Phillipsa8cdbd72018-07-17 12:30:40 -040016#include "SkLightingShader.h"
bungemanf93d7112016-09-16 06:24:20 -070017#include "SkMakeUnique.h"
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +000018#include "SkMallocPixelRef.h"
Cary Clark6d6d6032017-10-20 12:14:33 -040019#include "SkMatrixPriv.h"
Robert Phillipsa8cdbd72018-07-17 12:30:40 -040020#include "SkNormalSource.h"
caseq26337e92014-06-30 12:14:52 -070021#include "SkOSFile.h"
Mike Reedfadbfcd2017-12-06 16:09:20 -050022#include "SkReadBuffer.h"
Cary Clarkefd99cc2018-06-11 16:25:43 -040023#include "SkPicturePriv.h"
robertphillips@google.com770963f2014-04-18 18:04:41 +000024#include "SkPictureRecorder.h"
Florin Malita4aed1382017-05-25 10:38:07 -040025#include "SkShaderBase.h"
senorblanco91c395a2014-09-25 15:51:35 -070026#include "SkTableColorFilter.h"
commit-bot@chromium.org1e7ee992014-03-14 21:22:22 +000027#include "SkTemplates.h"
Khushal42f8bc42018-04-03 17:51:40 -070028#include "SkTextBlob.h"
caseq26337e92014-06-30 12:14:52 -070029#include "SkTypeface.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000030#include "SkWriteBuffer.h"
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +000031#include "SkXfermodeImageFilter.h"
dvonbeck8811e402016-06-16 12:39:25 -070032#include "sk_tool_utils.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000033#include "Test.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{
344 // Create a paint with the typeface.
caseq26337e92014-06-30 12:14:52 -0700345 SkPaint paint;
346 paint.setColor(SK_ColorGRAY);
347 paint.setTextSize(SkIntToScalar(30));
bungeman13b9c952016-05-12 10:09:30 -0700348 paint.setTypeface(std::move(typeface));
caseq26337e92014-06-30 12:14:52 -0700349
350 // Paint some text.
351 SkPictureRecorder recorder;
352 SkIRect canvasRect = SkIRect::MakeWH(kBitmapSize, kBitmapSize);
halcanary9d524f22016-03-29 09:03:52 -0700353 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(canvasRect.width()),
354 SkIntToScalar(canvasRect.height()),
halcanary96fcdcc2015-08-27 07:41:13 -0700355 nullptr, 0);
caseq26337e92014-06-30 12:14:52 -0700356 canvas->drawColor(SK_ColorWHITE);
bungeman41868fe2015-05-20 09:21:04 -0700357 canvas->drawText(text, 2, 24, 32, paint);
reedca2622b2016-03-18 07:25:55 -0700358 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
caseq26337e92014-06-30 12:14:52 -0700359
360 // Serlialize picture and create its clone from stream.
361 SkDynamicMemoryWStream stream;
362 picture->serialize(&stream);
Ben Wagner145dbcd2016-11-03 14:40:50 -0400363 std::unique_ptr<SkStream> inputStream(stream.detachAsStream());
reedca2622b2016-03-18 07:25:55 -0700364 sk_sp<SkPicture> loadedPicture(SkPicture::MakeFromStream(inputStream.get()));
caseq26337e92014-06-30 12:14:52 -0700365
366 // Draw both original and clone picture and compare bitmaps -- they should be identical.
367 SkBitmap origBitmap = draw_picture(*picture);
368 SkBitmap destBitmap = draw_picture(*loadedPicture);
369 compare_bitmaps(reporter, origBitmap, destBitmap);
370}
371
bungeman41868fe2015-05-20 09:21:04 -0700372static void TestPictureTypefaceSerialization(skiatest::Reporter* reporter) {
373 {
374 // Load typeface from file to test CreateFromFile with index.
Mike Reed271d1d92018-09-03 21:10:10 -0400375 auto typeface = MakeResourceAsTypeface("fonts/test.ttc", 1);
bungeman41868fe2015-05-20 09:21:04 -0700376 if (!typeface) {
halcanary7d571242016-02-24 17:59:16 -0800377 INFOF(reporter, "Could not run fontstream test because test.ttc not found.");
bungeman41868fe2015-05-20 09:21:04 -0700378 } else {
bungeman13b9c952016-05-12 10:09:30 -0700379 serialize_and_compare_typeface(std::move(typeface), "A!", reporter);
bungeman41868fe2015-05-20 09:21:04 -0700380 }
381 }
382
383 {
384 // Load typeface as stream to create with axis settings.
Mike Reed0933bc92017-12-09 01:27:41 +0000385 std::unique_ptr<SkStreamAsset> distortable(GetResourceAsStream("fonts/Distortable.ttf"));
bungeman41868fe2015-05-20 09:21:04 -0700386 if (!distortable) {
halcanary7d571242016-02-24 17:59:16 -0800387 INFOF(reporter, "Could not run fontstream test because Distortable.ttf not found.");
bungeman41868fe2015-05-20 09:21:04 -0700388 } else {
389 SkFixed axis = SK_FixedSqrt2;
bungeman13b9c952016-05-12 10:09:30 -0700390 sk_sp<SkTypeface> typeface(SkTypeface::MakeFromFontData(
bungemanf93d7112016-09-16 06:24:20 -0700391 skstd::make_unique<SkFontData>(std::move(distortable), 0, &axis, 1)));
bungeman41868fe2015-05-20 09:21:04 -0700392 if (!typeface) {
halcanary7d571242016-02-24 17:59:16 -0800393 INFOF(reporter, "Could not run fontstream test because Distortable.ttf not created.");
bungeman41868fe2015-05-20 09:21:04 -0700394 } else {
bungeman13b9c952016-05-12 10:09:30 -0700395 serialize_and_compare_typeface(std::move(typeface), "abc", reporter);
bungeman41868fe2015-05-20 09:21:04 -0700396 }
397 }
398 }
399}
400
reed84825042014-09-02 12:50:45 -0700401static void setup_bitmap_for_canvas(SkBitmap* bitmap) {
402 bitmap->allocN32Pixels(kBitmapSize, kBitmapSize);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000403}
404
reed84825042014-09-02 12:50:45 -0700405static void make_checkerboard_bitmap(SkBitmap& bitmap) {
406 setup_bitmap_for_canvas(&bitmap);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000407
408 SkCanvas canvas(bitmap);
409 canvas.clear(0x00000000);
410 SkPaint darkPaint;
411 darkPaint.setColor(0xFF804020);
412 SkPaint lightPaint;
413 lightPaint.setColor(0xFF244484);
414 const int i = kBitmapSize / 8;
415 const SkScalar f = SkIntToScalar(i);
416 for (int y = 0; y < kBitmapSize; y += i) {
417 for (int x = 0; x < kBitmapSize; x += i) {
418 canvas.save();
419 canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
420 canvas.drawRect(SkRect::MakeXYWH(0, 0, f, f), darkPaint);
421 canvas.drawRect(SkRect::MakeXYWH(f, 0, f, f), lightPaint);
422 canvas.drawRect(SkRect::MakeXYWH(0, f, f, f), lightPaint);
423 canvas.drawRect(SkRect::MakeXYWH(f, f, f, f), darkPaint);
424 canvas.restore();
425 }
426 }
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000427}
428
reed84825042014-09-02 12:50:45 -0700429static void draw_something(SkCanvas* canvas) {
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000430 SkPaint paint;
431 SkBitmap bitmap;
reed84825042014-09-02 12:50:45 -0700432 make_checkerboard_bitmap(bitmap);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000433
434 canvas->save();
435 canvas->scale(0.5f, 0.5f);
halcanary96fcdcc2015-08-27 07:41:13 -0700436 canvas->drawBitmap(bitmap, 0, 0, nullptr);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000437 canvas->restore();
438
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000439 paint.setAntiAlias(true);
440
441 paint.setColor(SK_ColorRED);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000442 canvas->drawCircle(SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/3), paint);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000443 paint.setColor(SK_ColorBLACK);
444 paint.setTextSize(SkIntToScalar(kBitmapSize/3));
Cary Clark2a475ea2017-04-28 15:35:12 -0400445 canvas->drawString("Picture", SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/4), paint);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000446}
447
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000448DEF_TEST(Serialization, reporter) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000449 // Test matrix serialization
450 {
451 SkMatrix matrix = SkMatrix::I();
452 TestObjectSerialization(&matrix, reporter);
caseq26337e92014-06-30 12:14:52 -0700453 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000454
Michael Ludwigea241402018-08-22 09:26:33 -0400455 // Test point3 serialization
456 {
457 SkPoint3 point;
458 TestObjectSerializationNoAlign<SkPoint3, false>(&point, reporter);
459 }
460
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000461 // Test path serialization
462 {
463 SkPath path;
464 TestObjectSerialization(&path, reporter);
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000465 }
sugoi@google.com305f78e2013-11-04 16:18:15 +0000466
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000467 // Test region serialization
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000468 {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000469 SkRegion region;
470 TestObjectSerialization(&region, reporter);
471 }
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000472
senorblanco91c395a2014-09-25 15:51:35 -0700473 // Test color filter serialization
474 {
475 TestColorFilterSerialization(reporter);
476 }
477
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000478 // Test string serialization
479 {
480 SkString string("string");
481 TestObjectSerializationNoAlign<SkString, false>(&string, reporter);
482 TestObjectSerializationNoAlign<SkString, true>(&string, reporter);
483 }
484
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000485 // Test rrect serialization
486 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000487 // SkRRect does not initialize anything.
488 // An uninitialized SkRRect can be serialized,
489 // but will branch on uninitialized data when deserialized.
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000490 SkRRect rrect;
rmistry@google.comd6bab022013-12-02 13:50:38 +0000491 SkRect rect = SkRect::MakeXYWH(1, 2, 20, 30);
492 SkVector corners[4] = { {1, 2}, {2, 3}, {3,4}, {4,5} };
493 rrect.setRectRadii(rect, corners);
Cary Clark6d6d6032017-10-20 12:14:33 -0400494 SerializationTest::TestAlignment(&rrect, reporter);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000495 }
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000496
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000497 // Test readByteArray
498 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000499 unsigned char data[kArraySize] = { 1, 2, 3 };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000500 TestArraySerialization(data, reporter);
501 }
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000502
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000503 // Test readColorArray
504 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000505 SkColor data[kArraySize] = { SK_ColorBLACK, SK_ColorWHITE, SK_ColorRED };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000506 TestArraySerialization(data, reporter);
507 }
508
brianosman97bbf822016-09-25 13:15:58 -0700509 // Test readColor4fArray
510 {
511 SkColor4f data[kArraySize] = {
512 SkColor4f::FromColor(SK_ColorBLACK),
513 SkColor4f::FromColor(SK_ColorWHITE),
514 SkColor4f::FromColor(SK_ColorRED),
515 { 1.f, 2.f, 4.f, 8.f }
516 };
517 TestArraySerialization(data, reporter);
518 }
519
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000520 // Test readIntArray
521 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000522 int32_t data[kArraySize] = { 1, 2, 4, 8 };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000523 TestArraySerialization(data, reporter);
524 }
525
526 // Test readPointArray
527 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000528 SkPoint data[kArraySize] = { {6, 7}, {42, 128} };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000529 TestArraySerialization(data, reporter);
530 }
531
532 // Test readScalarArray
533 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000534 SkScalar data[kArraySize] = { SK_Scalar1, SK_ScalarHalf, SK_ScalarMax };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000535 TestArraySerialization(data, reporter);
commit-bot@chromium.org02512882013-10-31 18:37:50 +0000536 }
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000537
538 // Test invalid deserializations
539 {
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000540 SkImageInfo info = SkImageInfo::MakeN32Premul(kBitmapSize, kBitmapSize);
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000541
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000542 SkBitmap validBitmap;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000543 validBitmap.setInfo(info);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000544
545 // Create a bitmap with a really large height
546 SkBitmap invalidBitmap;
reede5ea5002014-09-03 11:54:58 -0700547 invalidBitmap.setInfo(info.makeWH(info.width(), 1000000000));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000548
549 // The deserialization should succeed, and the rendering shouldn't crash,
550 // even when the device fails to initialize, due to its size
551 TestBitmapSerialization(validBitmap, invalidBitmap, true, reporter);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000552 }
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000553
554 // Test simple SkPicture serialization
555 {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000556 SkPictureRecorder recorder;
reed84825042014-09-02 12:50:45 -0700557 draw_something(recorder.beginRecording(SkIntToScalar(kBitmapSize),
558 SkIntToScalar(kBitmapSize),
halcanary96fcdcc2015-08-27 07:41:13 -0700559 nullptr, 0));
reedca2622b2016-03-18 07:25:55 -0700560 sk_sp<SkPicture> pict(recorder.finishRecordingAsPicture());
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000561
562 // Serialize picture
brianosmanfad98562016-05-04 11:06:28 -0700563 SkBinaryWriteBuffer writer;
Cary Clarkefd99cc2018-06-11 16:25:43 -0400564 SkPicturePriv::Flatten(pict, writer);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000565 size_t size = writer.bytesWritten();
commit-bot@chromium.org1e7ee992014-03-14 21:22:22 +0000566 SkAutoTMalloc<unsigned char> data(size);
567 writer.writeToMemory(static_cast<void*>(data.get()));
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000568
569 // Deserialize picture
Mike Reedfadbfcd2017-12-06 16:09:20 -0500570 SkReadBuffer reader(static_cast<void*>(data.get()), size);
Cary Clarkefd99cc2018-06-11 16:25:43 -0400571 sk_sp<SkPicture> readPict(SkPicturePriv::MakeFromBuffer(reader));
reedd921dbb2016-09-30 09:27:20 -0700572 REPORTER_ASSERT(reporter, reader.isValid());
bsalomon49f085d2014-09-05 13:34:00 -0700573 REPORTER_ASSERT(reporter, readPict.get());
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000574 }
caseq26337e92014-06-30 12:14:52 -0700575
576 TestPictureTypefaceSerialization(reporter);
Robert Phillipsa8cdbd72018-07-17 12:30:40 -0400577
578 // Test SkLightingShader/NormalMapSource serialization
579 {
580 const int kTexSize = 2;
581
582 SkLights::Builder builder;
583
584 builder.add(SkLights::Light::MakeDirectional(SkColor3f::Make(1.0f, 1.0f, 1.0f),
585 SkVector3::Make(1.0f, 0.0f, 0.0f)));
586 builder.setAmbientLightColor(SkColor3f::Make(0.2f, 0.2f, 0.2f));
587
588 sk_sp<SkLights> fLights = builder.finish();
589
590 SkBitmap diffuse = sk_tool_utils::create_checkerboard_bitmap(
591 kTexSize, kTexSize,
Mike Kleind46dce32018-08-16 10:17:03 -0400592 0x00000000,
Robert Phillipsa8cdbd72018-07-17 12:30:40 -0400593 sk_tool_utils::color_to_565(0xFF804020),
594 8);
595
596 SkRect bitmapBounds = SkRect::MakeIWH(diffuse.width(), diffuse.height());
597
598 SkMatrix matrix;
599 SkRect r = SkRect::MakeWH(SkIntToScalar(kTexSize), SkIntToScalar(kTexSize));
600 matrix.setRectToRect(bitmapBounds, r, SkMatrix::kFill_ScaleToFit);
601
602 SkMatrix ctm;
603 ctm.setRotate(45);
604 SkBitmap normals;
605 normals.allocN32Pixels(kTexSize, kTexSize);
606
607 sk_tool_utils::create_frustum_normal_map(&normals, SkIRect::MakeWH(kTexSize, kTexSize));
608 sk_sp<SkShader> normalMap = SkShader::MakeBitmapShader(normals, SkShader::kClamp_TileMode,
609 SkShader::kClamp_TileMode, &matrix);
610 sk_sp<SkNormalSource> normalSource = SkNormalSource::MakeFromNormalMap(std::move(normalMap),
611 ctm);
612 sk_sp<SkShader> diffuseShader = SkShader::MakeBitmapShader(diffuse,
613 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, &matrix);
614
615 sk_sp<SkShader> lightingShader = SkLightingShader::Make(diffuseShader,
616 normalSource,
617 fLights);
618 sk_sp<SkShader>(TestFlattenableSerialization(as_SB(lightingShader.get()), true, reporter));
619
620 lightingShader = SkLightingShader::Make(std::move(diffuseShader),
621 nullptr,
622 fLights);
623 sk_sp<SkShader>(TestFlattenableSerialization(as_SB(lightingShader.get()), true, reporter));
624
625 lightingShader = SkLightingShader::Make(nullptr,
626 std::move(normalSource),
627 fLights);
628 sk_sp<SkShader>(TestFlattenableSerialization(as_SB(lightingShader.get()), true, reporter));
629
630 lightingShader = SkLightingShader::Make(nullptr,
631 nullptr,
632 fLights);
633 sk_sp<SkShader>(TestFlattenableSerialization(as_SB(lightingShader.get()), true, reporter));
634 }
commit-bot@chromium.org02512882013-10-31 18:37:50 +0000635}
reedf70b5312016-03-04 16:36:20 -0800636
637///////////////////////////////////////////////////////////////////////////////////////////////////
638#include "SkAnnotation.h"
639
reedca2622b2016-03-18 07:25:55 -0700640static sk_sp<SkPicture> copy_picture_via_serialization(SkPicture* src) {
reedf70b5312016-03-04 16:36:20 -0800641 SkDynamicMemoryWStream wstream;
642 src->serialize(&wstream);
Ben Wagner145dbcd2016-11-03 14:40:50 -0400643 std::unique_ptr<SkStreamAsset> rstream(wstream.detachAsStream());
644 return SkPicture::MakeFromStream(rstream.get());
reedf70b5312016-03-04 16:36:20 -0800645}
646
647struct AnnotationRec {
648 const SkRect fRect;
649 const char* fKey;
bungeman38d909e2016-08-02 14:40:46 -0700650 sk_sp<SkData> fValue;
reedf70b5312016-03-04 16:36:20 -0800651};
652
653class TestAnnotationCanvas : public SkCanvas {
654 skiatest::Reporter* fReporter;
655 const AnnotationRec* fRec;
656 int fCount;
657 int fCurrIndex;
658
659public:
660 TestAnnotationCanvas(skiatest::Reporter* reporter, const AnnotationRec rec[], int count)
661 : SkCanvas(100, 100)
662 , fReporter(reporter)
663 , fRec(rec)
664 , fCount(count)
665 , fCurrIndex(0)
666 {}
667
668 ~TestAnnotationCanvas() {
669 REPORTER_ASSERT(fReporter, fCount == fCurrIndex);
670 }
671
672protected:
673 void onDrawAnnotation(const SkRect& rect, const char key[], SkData* value) {
674 REPORTER_ASSERT(fReporter, fCurrIndex < fCount);
675 REPORTER_ASSERT(fReporter, rect == fRec[fCurrIndex].fRect);
676 REPORTER_ASSERT(fReporter, !strcmp(key, fRec[fCurrIndex].fKey));
bungeman38d909e2016-08-02 14:40:46 -0700677 REPORTER_ASSERT(fReporter, value->equals(fRec[fCurrIndex].fValue.get()));
reedf70b5312016-03-04 16:36:20 -0800678 fCurrIndex += 1;
679 }
680};
681
682/*
683 * Test the 3 annotation types by recording them into a picture, serializing, and then playing
684 * them back into another canvas.
685 */
686DEF_TEST(Annotations, reporter) {
687 SkPictureRecorder recorder;
688 SkCanvas* recordingCanvas = recorder.beginRecording(SkRect::MakeWH(100, 100));
halcanary9d524f22016-03-29 09:03:52 -0700689
reedf70b5312016-03-04 16:36:20 -0800690 const char* str0 = "rect-with-url";
691 const SkRect r0 = SkRect::MakeWH(10, 10);
bungeman38d909e2016-08-02 14:40:46 -0700692 sk_sp<SkData> d0(SkData::MakeWithCString(str0));
693 SkAnnotateRectWithURL(recordingCanvas, r0, d0.get());
halcanary9d524f22016-03-29 09:03:52 -0700694
reedf70b5312016-03-04 16:36:20 -0800695 const char* str1 = "named-destination";
696 const SkRect r1 = SkRect::MakeXYWH(5, 5, 0, 0); // collapsed to a point
bungeman38d909e2016-08-02 14:40:46 -0700697 sk_sp<SkData> d1(SkData::MakeWithCString(str1));
698 SkAnnotateNamedDestination(recordingCanvas, {r1.x(), r1.y()}, d1.get());
halcanary9d524f22016-03-29 09:03:52 -0700699
reedf70b5312016-03-04 16:36:20 -0800700 const char* str2 = "link-to-destination";
701 const SkRect r2 = SkRect::MakeXYWH(20, 20, 5, 6);
bungeman38d909e2016-08-02 14:40:46 -0700702 sk_sp<SkData> d2(SkData::MakeWithCString(str2));
703 SkAnnotateLinkToDestination(recordingCanvas, r2, d2.get());
reedf70b5312016-03-04 16:36:20 -0800704
705 const AnnotationRec recs[] = {
bungeman38d909e2016-08-02 14:40:46 -0700706 { r0, SkAnnotationKeys::URL_Key(), std::move(d0) },
707 { r1, SkAnnotationKeys::Define_Named_Dest_Key(), std::move(d1) },
708 { r2, SkAnnotationKeys::Link_Named_Dest_Key(), std::move(d2) },
reedf70b5312016-03-04 16:36:20 -0800709 };
710
reedca2622b2016-03-18 07:25:55 -0700711 sk_sp<SkPicture> pict0(recorder.finishRecordingAsPicture());
712 sk_sp<SkPicture> pict1(copy_picture_via_serialization(pict0.get()));
reedf70b5312016-03-04 16:36:20 -0800713
714 TestAnnotationCanvas canvas(reporter, recs, SK_ARRAY_COUNT(recs));
715 canvas.drawPicture(pict1);
716}
Mike Reed25325842018-03-14 09:52:02 -0400717
718DEF_TEST(WriteBuffer_storage, reporter) {
719 enum {
720 kSize = 32
721 };
722 int32_t storage[kSize/4];
723 char src[kSize];
724 sk_bzero(src, kSize);
725
726 SkBinaryWriteBuffer writer(storage, kSize);
727 REPORTER_ASSERT(reporter, writer.usingInitialStorage());
728 REPORTER_ASSERT(reporter, writer.bytesWritten() == 0);
729 writer.write(src, kSize - 4);
730 REPORTER_ASSERT(reporter, writer.usingInitialStorage());
731 REPORTER_ASSERT(reporter, writer.bytesWritten() == kSize - 4);
732 writer.writeInt(0);
733 REPORTER_ASSERT(reporter, writer.usingInitialStorage());
734 REPORTER_ASSERT(reporter, writer.bytesWritten() == kSize);
735
736 writer.reset(storage, kSize-4);
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()); // this is the change
744 REPORTER_ASSERT(reporter, writer.bytesWritten() == kSize);
745}
Khushal42f8bc42018-04-03 17:51:40 -0700746
747DEF_TEST(WriteBuffer_external_memory_textblob, reporter) {
Mike Reed2ed78202018-11-21 15:10:08 -0500748 SkFont font;
Khushal42f8bc42018-04-03 17:51:40 -0700749 font.setTypeface(SkTypeface::MakeDefault());
750
751 SkTextBlobBuilder builder;
752 int glyph_count = 5;
753 const auto& run = builder.allocRun(font, glyph_count, 1.2f, 2.3f);
754 // allocRun() allocates only the glyph buffer.
755 std::fill(run.glyphs, run.glyphs + glyph_count, 0);
756 auto blob = builder.make();
757 SkSerialProcs procs;
758 SkAutoTMalloc<uint8_t> storage;
759 size_t blob_size = 0u;
760 size_t storage_size = 0u;
761
762 blob_size = SkAlign4(blob->serialize(procs)->size());
763 REPORTER_ASSERT(reporter, blob_size > 4u);
764 storage_size = blob_size - 4;
765 storage.realloc(storage_size);
766 REPORTER_ASSERT(reporter, blob->serialize(procs, storage.get(), storage_size) == 0u);
767 storage_size = blob_size;
768 storage.realloc(storage_size);
769 REPORTER_ASSERT(reporter, blob->serialize(procs, storage.get(), storage_size) != 0u);
770}
771
772DEF_TEST(WriteBuffer_external_memory_flattenable, reporter) {
773 SkScalar intervals[] = {1.f, 1.f};
774 auto path_effect = SkDashPathEffect::Make(intervals, 2, 0);
775 size_t path_size = SkAlign4(path_effect->serialize()->size());
776 REPORTER_ASSERT(reporter, path_size > 4u);
777 SkAutoTMalloc<uint8_t> storage;
778
779 size_t storage_size = path_size - 4;
780 storage.realloc(storage_size);
781 REPORTER_ASSERT(reporter, path_effect->serialize(storage.get(), storage_size) == 0u);
782
783 storage_size = path_size;
784 storage.realloc(storage_size);
785 REPORTER_ASSERT(reporter, path_effect->serialize(storage.get(), storage_size) != 0u);
786}