blob: ccc907ccc61d9d770ce434a8a4aadf102de5bab5 [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
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000143template<> struct SerializationUtils<SkScalar> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000144 static void Write(SkWriteBuffer& writer, SkScalar* data, uint32_t arraySize) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000145 writer.writeScalarArray(data, arraySize);
146 }
Mike Reedfadbfcd2017-12-06 16:09:20 -0500147 static bool Read(SkReadBuffer& reader, SkScalar* data, uint32_t arraySize) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000148 return reader.readScalarArray(data, arraySize);
149 }
150};
reed@google.com12a23862013-11-04 21:35:55 +0000151
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000152template<typename T, bool testInvalid> struct SerializationTestUtils {
153 static void InvalidateData(unsigned char* data) {}
154};
155
156template<> struct SerializationTestUtils<SkString, true> {
157 static void InvalidateData(unsigned char* data) {
158 data[3] |= 0x80; // Reverse sign of 1st integer
159 }
160};
161
162template<typename T, bool testInvalid>
163static void TestObjectSerializationNoAlign(T* testObj, skiatest::Reporter* reporter) {
brianosmanfad98562016-05-04 11:06:28 -0700164 SkBinaryWriteBuffer writer;
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000165 SerializationUtils<T>::Write(writer, testObj);
166 size_t bytesWritten = writer.bytesWritten();
167 REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten);
reed@google.com12a23862013-11-04 21:35:55 +0000168
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000169 unsigned char dataWritten[1024];
170 writer.writeToMemory(dataWritten);
171
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000172 SerializationTestUtils<T, testInvalid>::InvalidateData(dataWritten);
173
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000174 // Make sure this fails when it should (test with smaller size, but still multiple of 4)
Mike Reedfadbfcd2017-12-06 16:09:20 -0500175 SkReadBuffer buffer(dataWritten, bytesWritten - 4);
commit-bot@chromium.org8f457e32013-11-08 19:22:57 +0000176 T obj;
177 SerializationUtils<T>::Read(buffer, &obj);
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000178 REPORTER_ASSERT(reporter, !buffer.isValid());
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000179
180 // Make sure this succeeds when it should
Mike Reedfadbfcd2017-12-06 16:09:20 -0500181 SkReadBuffer buffer2(dataWritten, bytesWritten);
Robert Phillipsb2526042016-09-26 09:00:36 -0400182 size_t offsetBefore = buffer2.offset();
commit-bot@chromium.org8f457e32013-11-08 19:22:57 +0000183 T obj2;
184 SerializationUtils<T>::Read(buffer2, &obj2);
Robert Phillipsb2526042016-09-26 09:00:36 -0400185 size_t offsetAfter = buffer2.offset();
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000186 // This should have succeeded, since there are enough bytes to read this
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000187 REPORTER_ASSERT(reporter, buffer2.isValid() == !testInvalid);
188 // Note: This following test should always succeed, regardless of whether the buffer is valid,
189 // 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 -0400190 REPORTER_ASSERT(reporter, offsetAfter - offsetBefore == bytesWritten);
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000191}
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000192
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000193template<typename T>
194static void TestObjectSerialization(T* testObj, skiatest::Reporter* reporter) {
195 TestObjectSerializationNoAlign<T, false>(testObj, reporter);
Cary Clark6d6d6032017-10-20 12:14:33 -0400196 SerializationTest::TestAlignment(testObj, reporter);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000197}
198
199template<typename T>
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000200static T* TestFlattenableSerialization(T* testObj, bool shouldSucceed,
201 skiatest::Reporter* reporter) {
brianosmanfad98562016-05-04 11:06:28 -0700202 SkBinaryWriteBuffer writer;
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000203 SerializationUtils<T>::Write(writer, testObj);
204 size_t bytesWritten = writer.bytesWritten();
205 REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten);
206
dvonbeck8811e402016-06-16 12:39:25 -0700207 SkASSERT(bytesWritten <= 4096);
senorblanco91c395a2014-09-25 15:51:35 -0700208 unsigned char dataWritten[4096];
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000209 writer.writeToMemory(dataWritten);
210
211 // Make sure this fails when it should (test with smaller size, but still multiple of 4)
Mike Reedfadbfcd2017-12-06 16:09:20 -0500212 SkReadBuffer buffer(dataWritten, bytesWritten - 4);
halcanary96fcdcc2015-08-27 07:41:13 -0700213 T* obj = nullptr;
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000214 SerializationUtils<T>::Read(buffer, &obj);
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000215 REPORTER_ASSERT(reporter, !buffer.isValid());
halcanary96fcdcc2015-08-27 07:41:13 -0700216 REPORTER_ASSERT(reporter, nullptr == obj);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000217
218 // Make sure this succeeds when it should
Mike Reedfadbfcd2017-12-06 16:09:20 -0500219 SkReadBuffer buffer2(dataWritten, bytesWritten);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000220 const unsigned char* peekBefore = static_cast<const unsigned char*>(buffer2.skip(0));
halcanary96fcdcc2015-08-27 07:41:13 -0700221 T* obj2 = nullptr;
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000222 SerializationUtils<T>::Read(buffer2, &obj2);
223 const unsigned char* peekAfter = static_cast<const unsigned char*>(buffer2.skip(0));
224 if (shouldSucceed) {
225 // This should have succeeded, since there are enough bytes to read this
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000226 REPORTER_ASSERT(reporter, buffer2.isValid());
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000227 REPORTER_ASSERT(reporter, static_cast<size_t>(peekAfter - peekBefore) == bytesWritten);
bsalomon49f085d2014-09-05 13:34:00 -0700228 REPORTER_ASSERT(reporter, obj2);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000229 } else {
230 // If the deserialization was supposed to fail, make sure it did
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000231 REPORTER_ASSERT(reporter, !buffer.isValid());
halcanary96fcdcc2015-08-27 07:41:13 -0700232 REPORTER_ASSERT(reporter, nullptr == obj2);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000233 }
234
235 return obj2; // Return object to perform further validity tests on it
236}
237
238template<typename T>
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000239static void TestArraySerialization(T* data, skiatest::Reporter* reporter) {
brianosmanfad98562016-05-04 11:06:28 -0700240 SkBinaryWriteBuffer writer;
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000241 SerializationUtils<T>::Write(writer, data, kArraySize);
242 size_t bytesWritten = writer.bytesWritten();
243 // This should write the length (in 4 bytes) and the array
244 REPORTER_ASSERT(reporter, (4 + kArraySize * sizeof(T)) == bytesWritten);
245
brianosman97bbf822016-09-25 13:15:58 -0700246 unsigned char dataWritten[2048];
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000247 writer.writeToMemory(dataWritten);
248
249 // Make sure this fails when it should
Mike Reedfadbfcd2017-12-06 16:09:20 -0500250 SkReadBuffer buffer(dataWritten, bytesWritten);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000251 T dataRead[kArraySize];
252 bool success = SerializationUtils<T>::Read(buffer, dataRead, kArraySize / 2);
253 // This should have failed, since the provided size was too small
254 REPORTER_ASSERT(reporter, !success);
255
256 // Make sure this succeeds when it should
Mike Reedfadbfcd2017-12-06 16:09:20 -0500257 SkReadBuffer buffer2(dataWritten, bytesWritten);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000258 success = SerializationUtils<T>::Read(buffer2, dataRead, kArraySize);
259 // This should have succeeded, since there are enough bytes to read this
260 REPORTER_ASSERT(reporter, success);
261}
262
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000263static void TestBitmapSerialization(const SkBitmap& validBitmap,
264 const SkBitmap& invalidBitmap,
265 bool shouldSucceed,
266 skiatest::Reporter* reporter) {
reed9ce9d672016-03-17 10:51:11 -0700267 sk_sp<SkImage> validImage(SkImage::MakeFromBitmap(validBitmap));
robertphillips549c8992016-04-01 09:28:51 -0700268 sk_sp<SkImageFilter> validBitmapSource(SkImageSource::Make(std::move(validImage)));
reed9ce9d672016-03-17 10:51:11 -0700269 sk_sp<SkImage> invalidImage(SkImage::MakeFromBitmap(invalidBitmap));
robertphillips549c8992016-04-01 09:28:51 -0700270 sk_sp<SkImageFilter> invalidBitmapSource(SkImageSource::Make(std::move(invalidImage)));
reedcfb6bdf2016-03-29 11:32:50 -0700271 sk_sp<SkImageFilter> xfermodeImageFilter(
reed374772b2016-10-05 17:33:02 -0700272 SkXfermodeImageFilter::Make(SkBlendMode::kSrcOver,
robertphillips8c0326d2016-04-05 12:48:34 -0700273 std::move(invalidBitmapSource),
274 std::move(validBitmapSource), nullptr));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000275
Mike Reed5e257172016-11-01 11:22:05 -0400276 sk_sp<SkImageFilter> deserializedFilter(
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000277 TestFlattenableSerialization<SkImageFilter>(
reedcfb6bdf2016-03-29 11:32:50 -0700278 xfermodeImageFilter.get(), shouldSucceed, reporter));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000279
280 // Try to render a small bitmap using the invalid deserialized filter
281 // to make sure we don't crash while trying to render it
282 if (shouldSucceed) {
283 SkBitmap bitmap;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000284 bitmap.allocN32Pixels(24, 24);
285 SkCanvas canvas(bitmap);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000286 canvas.clear(0x00000000);
287 SkPaint paint;
288 paint.setImageFilter(deserializedFilter);
289 canvas.clipRect(SkRect::MakeXYWH(0, 0, SkIntToScalar(24), SkIntToScalar(24)));
290 canvas.drawBitmap(bitmap, 0, 0, &paint);
291 }
292}
293
senorblanco91c395a2014-09-25 15:51:35 -0700294static void TestColorFilterSerialization(skiatest::Reporter* reporter) {
295 uint8_t table[256];
296 for (int i = 0; i < 256; ++i) {
297 table[i] = (i * 41) % 256;
298 }
reedd053ce92016-03-22 10:17:23 -0700299 auto colorFilter(SkTableColorFilter::Make(table));
Hal Canary342b7ac2016-11-04 11:49:42 -0400300 sk_sp<SkColorFilter> copy(
senorblanco91c395a2014-09-25 15:51:35 -0700301 TestFlattenableSerialization<SkColorFilter>(colorFilter.get(), true, reporter));
302}
303
caseq26337e92014-06-30 12:14:52 -0700304static SkBitmap draw_picture(SkPicture& picture) {
305 SkBitmap bitmap;
halcanary9d524f22016-03-29 09:03:52 -0700306 bitmap.allocN32Pixels(SkScalarCeilToInt(picture.cullRect().width()),
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700307 SkScalarCeilToInt(picture.cullRect().height()));
caseq26337e92014-06-30 12:14:52 -0700308 SkCanvas canvas(bitmap);
robertphillipsc5ba71d2014-09-04 08:42:50 -0700309 picture.playback(&canvas);
caseq26337e92014-06-30 12:14:52 -0700310 return bitmap;
311}
312
313static void compare_bitmaps(skiatest::Reporter* reporter,
314 const SkBitmap& b1, const SkBitmap& b2) {
315 REPORTER_ASSERT(reporter, b1.width() == b2.width());
316 REPORTER_ASSERT(reporter, b1.height() == b2.height());
caseq26337e92014-06-30 12:14:52 -0700317
318 if ((b1.width() != b2.width()) ||
319 (b1.height() != b2.height())) {
320 return;
321 }
322
323 int pixelErrors = 0;
324 for (int y = 0; y < b2.height(); ++y) {
325 for (int x = 0; x < b2.width(); ++x) {
326 if (b1.getColor(x, y) != b2.getColor(x, y))
327 ++pixelErrors;
328 }
329 }
330 REPORTER_ASSERT(reporter, 0 == pixelErrors);
331}
bungeman13b9c952016-05-12 10:09:30 -0700332static void serialize_and_compare_typeface(sk_sp<SkTypeface> typeface, const char* text,
bungeman41868fe2015-05-20 09:21:04 -0700333 skiatest::Reporter* reporter)
334{
335 // Create a paint with the typeface.
caseq26337e92014-06-30 12:14:52 -0700336 SkPaint paint;
337 paint.setColor(SK_ColorGRAY);
338 paint.setTextSize(SkIntToScalar(30));
bungeman13b9c952016-05-12 10:09:30 -0700339 paint.setTypeface(std::move(typeface));
caseq26337e92014-06-30 12:14:52 -0700340
341 // Paint some text.
342 SkPictureRecorder recorder;
343 SkIRect canvasRect = SkIRect::MakeWH(kBitmapSize, kBitmapSize);
halcanary9d524f22016-03-29 09:03:52 -0700344 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(canvasRect.width()),
345 SkIntToScalar(canvasRect.height()),
halcanary96fcdcc2015-08-27 07:41:13 -0700346 nullptr, 0);
caseq26337e92014-06-30 12:14:52 -0700347 canvas->drawColor(SK_ColorWHITE);
bungeman41868fe2015-05-20 09:21:04 -0700348 canvas->drawText(text, 2, 24, 32, paint);
reedca2622b2016-03-18 07:25:55 -0700349 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
caseq26337e92014-06-30 12:14:52 -0700350
351 // Serlialize picture and create its clone from stream.
352 SkDynamicMemoryWStream stream;
353 picture->serialize(&stream);
Ben Wagner145dbcd2016-11-03 14:40:50 -0400354 std::unique_ptr<SkStream> inputStream(stream.detachAsStream());
reedca2622b2016-03-18 07:25:55 -0700355 sk_sp<SkPicture> loadedPicture(SkPicture::MakeFromStream(inputStream.get()));
caseq26337e92014-06-30 12:14:52 -0700356
357 // Draw both original and clone picture and compare bitmaps -- they should be identical.
358 SkBitmap origBitmap = draw_picture(*picture);
359 SkBitmap destBitmap = draw_picture(*loadedPicture);
360 compare_bitmaps(reporter, origBitmap, destBitmap);
361}
362
bungeman41868fe2015-05-20 09:21:04 -0700363static void TestPictureTypefaceSerialization(skiatest::Reporter* reporter) {
364 {
365 // Load typeface from file to test CreateFromFile with index.
Mike Reed271d1d92018-09-03 21:10:10 -0400366 auto typeface = MakeResourceAsTypeface("fonts/test.ttc", 1);
bungeman41868fe2015-05-20 09:21:04 -0700367 if (!typeface) {
halcanary7d571242016-02-24 17:59:16 -0800368 INFOF(reporter, "Could not run fontstream test because test.ttc not found.");
bungeman41868fe2015-05-20 09:21:04 -0700369 } else {
bungeman13b9c952016-05-12 10:09:30 -0700370 serialize_and_compare_typeface(std::move(typeface), "A!", reporter);
bungeman41868fe2015-05-20 09:21:04 -0700371 }
372 }
373
374 {
375 // Load typeface as stream to create with axis settings.
Mike Reed0933bc92017-12-09 01:27:41 +0000376 std::unique_ptr<SkStreamAsset> distortable(GetResourceAsStream("fonts/Distortable.ttf"));
bungeman41868fe2015-05-20 09:21:04 -0700377 if (!distortable) {
halcanary7d571242016-02-24 17:59:16 -0800378 INFOF(reporter, "Could not run fontstream test because Distortable.ttf not found.");
bungeman41868fe2015-05-20 09:21:04 -0700379 } else {
380 SkFixed axis = SK_FixedSqrt2;
bungeman13b9c952016-05-12 10:09:30 -0700381 sk_sp<SkTypeface> typeface(SkTypeface::MakeFromFontData(
bungemanf93d7112016-09-16 06:24:20 -0700382 skstd::make_unique<SkFontData>(std::move(distortable), 0, &axis, 1)));
bungeman41868fe2015-05-20 09:21:04 -0700383 if (!typeface) {
halcanary7d571242016-02-24 17:59:16 -0800384 INFOF(reporter, "Could not run fontstream test because Distortable.ttf not created.");
bungeman41868fe2015-05-20 09:21:04 -0700385 } else {
bungeman13b9c952016-05-12 10:09:30 -0700386 serialize_and_compare_typeface(std::move(typeface), "abc", reporter);
bungeman41868fe2015-05-20 09:21:04 -0700387 }
388 }
389 }
390}
391
reed84825042014-09-02 12:50:45 -0700392static void setup_bitmap_for_canvas(SkBitmap* bitmap) {
393 bitmap->allocN32Pixels(kBitmapSize, kBitmapSize);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000394}
395
reed84825042014-09-02 12:50:45 -0700396static void make_checkerboard_bitmap(SkBitmap& bitmap) {
397 setup_bitmap_for_canvas(&bitmap);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000398
399 SkCanvas canvas(bitmap);
400 canvas.clear(0x00000000);
401 SkPaint darkPaint;
402 darkPaint.setColor(0xFF804020);
403 SkPaint lightPaint;
404 lightPaint.setColor(0xFF244484);
405 const int i = kBitmapSize / 8;
406 const SkScalar f = SkIntToScalar(i);
407 for (int y = 0; y < kBitmapSize; y += i) {
408 for (int x = 0; x < kBitmapSize; x += i) {
409 canvas.save();
410 canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
411 canvas.drawRect(SkRect::MakeXYWH(0, 0, f, f), darkPaint);
412 canvas.drawRect(SkRect::MakeXYWH(f, 0, f, f), lightPaint);
413 canvas.drawRect(SkRect::MakeXYWH(0, f, f, f), lightPaint);
414 canvas.drawRect(SkRect::MakeXYWH(f, f, f, f), darkPaint);
415 canvas.restore();
416 }
417 }
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000418}
419
reed84825042014-09-02 12:50:45 -0700420static void draw_something(SkCanvas* canvas) {
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000421 SkPaint paint;
422 SkBitmap bitmap;
reed84825042014-09-02 12:50:45 -0700423 make_checkerboard_bitmap(bitmap);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000424
425 canvas->save();
426 canvas->scale(0.5f, 0.5f);
halcanary96fcdcc2015-08-27 07:41:13 -0700427 canvas->drawBitmap(bitmap, 0, 0, nullptr);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000428 canvas->restore();
429
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000430 paint.setAntiAlias(true);
431
432 paint.setColor(SK_ColorRED);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000433 canvas->drawCircle(SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/3), paint);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000434 paint.setColor(SK_ColorBLACK);
435 paint.setTextSize(SkIntToScalar(kBitmapSize/3));
Cary Clark2a475ea2017-04-28 15:35:12 -0400436 canvas->drawString("Picture", SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/4), paint);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000437}
438
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000439DEF_TEST(Serialization, reporter) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000440 // Test matrix serialization
441 {
442 SkMatrix matrix = SkMatrix::I();
443 TestObjectSerialization(&matrix, reporter);
caseq26337e92014-06-30 12:14:52 -0700444 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000445
446 // Test path serialization
447 {
448 SkPath path;
449 TestObjectSerialization(&path, reporter);
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000450 }
sugoi@google.com305f78e2013-11-04 16:18:15 +0000451
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000452 // Test region serialization
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000453 {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000454 SkRegion region;
455 TestObjectSerialization(&region, reporter);
456 }
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000457
senorblanco91c395a2014-09-25 15:51:35 -0700458 // Test color filter serialization
459 {
460 TestColorFilterSerialization(reporter);
461 }
462
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000463 // Test string serialization
464 {
465 SkString string("string");
466 TestObjectSerializationNoAlign<SkString, false>(&string, reporter);
467 TestObjectSerializationNoAlign<SkString, true>(&string, reporter);
468 }
469
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000470 // Test rrect serialization
471 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000472 // SkRRect does not initialize anything.
473 // An uninitialized SkRRect can be serialized,
474 // but will branch on uninitialized data when deserialized.
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000475 SkRRect rrect;
rmistry@google.comd6bab022013-12-02 13:50:38 +0000476 SkRect rect = SkRect::MakeXYWH(1, 2, 20, 30);
477 SkVector corners[4] = { {1, 2}, {2, 3}, {3,4}, {4,5} };
478 rrect.setRectRadii(rect, corners);
Cary Clark6d6d6032017-10-20 12:14:33 -0400479 SerializationTest::TestAlignment(&rrect, reporter);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000480 }
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000481
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000482 // Test readByteArray
483 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000484 unsigned char data[kArraySize] = { 1, 2, 3 };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000485 TestArraySerialization(data, reporter);
486 }
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000487
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000488 // Test readColorArray
489 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000490 SkColor data[kArraySize] = { SK_ColorBLACK, SK_ColorWHITE, SK_ColorRED };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000491 TestArraySerialization(data, reporter);
492 }
493
brianosman97bbf822016-09-25 13:15:58 -0700494 // Test readColor4fArray
495 {
496 SkColor4f data[kArraySize] = {
497 SkColor4f::FromColor(SK_ColorBLACK),
498 SkColor4f::FromColor(SK_ColorWHITE),
499 SkColor4f::FromColor(SK_ColorRED),
500 { 1.f, 2.f, 4.f, 8.f }
501 };
502 TestArraySerialization(data, reporter);
503 }
504
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000505 // Test readIntArray
506 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000507 int32_t data[kArraySize] = { 1, 2, 4, 8 };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000508 TestArraySerialization(data, reporter);
509 }
510
511 // Test readPointArray
512 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000513 SkPoint data[kArraySize] = { {6, 7}, {42, 128} };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000514 TestArraySerialization(data, reporter);
515 }
516
517 // Test readScalarArray
518 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000519 SkScalar data[kArraySize] = { SK_Scalar1, SK_ScalarHalf, SK_ScalarMax };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000520 TestArraySerialization(data, reporter);
commit-bot@chromium.org02512882013-10-31 18:37:50 +0000521 }
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000522
523 // Test invalid deserializations
524 {
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000525 SkImageInfo info = SkImageInfo::MakeN32Premul(kBitmapSize, kBitmapSize);
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000526
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000527 SkBitmap validBitmap;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000528 validBitmap.setInfo(info);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000529
530 // Create a bitmap with a really large height
531 SkBitmap invalidBitmap;
reede5ea5002014-09-03 11:54:58 -0700532 invalidBitmap.setInfo(info.makeWH(info.width(), 1000000000));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000533
534 // The deserialization should succeed, and the rendering shouldn't crash,
535 // even when the device fails to initialize, due to its size
536 TestBitmapSerialization(validBitmap, invalidBitmap, true, reporter);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000537 }
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000538
539 // Test simple SkPicture serialization
540 {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000541 SkPictureRecorder recorder;
reed84825042014-09-02 12:50:45 -0700542 draw_something(recorder.beginRecording(SkIntToScalar(kBitmapSize),
543 SkIntToScalar(kBitmapSize),
halcanary96fcdcc2015-08-27 07:41:13 -0700544 nullptr, 0));
reedca2622b2016-03-18 07:25:55 -0700545 sk_sp<SkPicture> pict(recorder.finishRecordingAsPicture());
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000546
547 // Serialize picture
brianosmanfad98562016-05-04 11:06:28 -0700548 SkBinaryWriteBuffer writer;
Cary Clarkefd99cc2018-06-11 16:25:43 -0400549 SkPicturePriv::Flatten(pict, writer);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000550 size_t size = writer.bytesWritten();
commit-bot@chromium.org1e7ee992014-03-14 21:22:22 +0000551 SkAutoTMalloc<unsigned char> data(size);
552 writer.writeToMemory(static_cast<void*>(data.get()));
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000553
554 // Deserialize picture
Mike Reedfadbfcd2017-12-06 16:09:20 -0500555 SkReadBuffer reader(static_cast<void*>(data.get()), size);
Cary Clarkefd99cc2018-06-11 16:25:43 -0400556 sk_sp<SkPicture> readPict(SkPicturePriv::MakeFromBuffer(reader));
reedd921dbb2016-09-30 09:27:20 -0700557 REPORTER_ASSERT(reporter, reader.isValid());
bsalomon49f085d2014-09-05 13:34:00 -0700558 REPORTER_ASSERT(reporter, readPict.get());
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000559 }
caseq26337e92014-06-30 12:14:52 -0700560
561 TestPictureTypefaceSerialization(reporter);
Robert Phillipsa8cdbd72018-07-17 12:30:40 -0400562
563 // Test SkLightingShader/NormalMapSource serialization
564 {
565 const int kTexSize = 2;
566
567 SkLights::Builder builder;
568
569 builder.add(SkLights::Light::MakeDirectional(SkColor3f::Make(1.0f, 1.0f, 1.0f),
570 SkVector3::Make(1.0f, 0.0f, 0.0f)));
571 builder.setAmbientLightColor(SkColor3f::Make(0.2f, 0.2f, 0.2f));
572
573 sk_sp<SkLights> fLights = builder.finish();
574
575 SkBitmap diffuse = sk_tool_utils::create_checkerboard_bitmap(
576 kTexSize, kTexSize,
Mike Kleind46dce32018-08-16 10:17:03 -0400577 0x00000000,
Robert Phillipsa8cdbd72018-07-17 12:30:40 -0400578 sk_tool_utils::color_to_565(0xFF804020),
579 8);
580
581 SkRect bitmapBounds = SkRect::MakeIWH(diffuse.width(), diffuse.height());
582
583 SkMatrix matrix;
584 SkRect r = SkRect::MakeWH(SkIntToScalar(kTexSize), SkIntToScalar(kTexSize));
585 matrix.setRectToRect(bitmapBounds, r, SkMatrix::kFill_ScaleToFit);
586
587 SkMatrix ctm;
588 ctm.setRotate(45);
589 SkBitmap normals;
590 normals.allocN32Pixels(kTexSize, kTexSize);
591
592 sk_tool_utils::create_frustum_normal_map(&normals, SkIRect::MakeWH(kTexSize, kTexSize));
593 sk_sp<SkShader> normalMap = SkShader::MakeBitmapShader(normals, SkShader::kClamp_TileMode,
594 SkShader::kClamp_TileMode, &matrix);
595 sk_sp<SkNormalSource> normalSource = SkNormalSource::MakeFromNormalMap(std::move(normalMap),
596 ctm);
597 sk_sp<SkShader> diffuseShader = SkShader::MakeBitmapShader(diffuse,
598 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, &matrix);
599
600 sk_sp<SkShader> lightingShader = SkLightingShader::Make(diffuseShader,
601 normalSource,
602 fLights);
603 sk_sp<SkShader>(TestFlattenableSerialization(as_SB(lightingShader.get()), true, reporter));
604
605 lightingShader = SkLightingShader::Make(std::move(diffuseShader),
606 nullptr,
607 fLights);
608 sk_sp<SkShader>(TestFlattenableSerialization(as_SB(lightingShader.get()), true, reporter));
609
610 lightingShader = SkLightingShader::Make(nullptr,
611 std::move(normalSource),
612 fLights);
613 sk_sp<SkShader>(TestFlattenableSerialization(as_SB(lightingShader.get()), true, reporter));
614
615 lightingShader = SkLightingShader::Make(nullptr,
616 nullptr,
617 fLights);
618 sk_sp<SkShader>(TestFlattenableSerialization(as_SB(lightingShader.get()), true, reporter));
619 }
commit-bot@chromium.org02512882013-10-31 18:37:50 +0000620}
reedf70b5312016-03-04 16:36:20 -0800621
622///////////////////////////////////////////////////////////////////////////////////////////////////
623#include "SkAnnotation.h"
624
reedca2622b2016-03-18 07:25:55 -0700625static sk_sp<SkPicture> copy_picture_via_serialization(SkPicture* src) {
reedf70b5312016-03-04 16:36:20 -0800626 SkDynamicMemoryWStream wstream;
627 src->serialize(&wstream);
Ben Wagner145dbcd2016-11-03 14:40:50 -0400628 std::unique_ptr<SkStreamAsset> rstream(wstream.detachAsStream());
629 return SkPicture::MakeFromStream(rstream.get());
reedf70b5312016-03-04 16:36:20 -0800630}
631
632struct AnnotationRec {
633 const SkRect fRect;
634 const char* fKey;
bungeman38d909e2016-08-02 14:40:46 -0700635 sk_sp<SkData> fValue;
reedf70b5312016-03-04 16:36:20 -0800636};
637
638class TestAnnotationCanvas : public SkCanvas {
639 skiatest::Reporter* fReporter;
640 const AnnotationRec* fRec;
641 int fCount;
642 int fCurrIndex;
643
644public:
645 TestAnnotationCanvas(skiatest::Reporter* reporter, const AnnotationRec rec[], int count)
646 : SkCanvas(100, 100)
647 , fReporter(reporter)
648 , fRec(rec)
649 , fCount(count)
650 , fCurrIndex(0)
651 {}
652
653 ~TestAnnotationCanvas() {
654 REPORTER_ASSERT(fReporter, fCount == fCurrIndex);
655 }
656
657protected:
658 void onDrawAnnotation(const SkRect& rect, const char key[], SkData* value) {
659 REPORTER_ASSERT(fReporter, fCurrIndex < fCount);
660 REPORTER_ASSERT(fReporter, rect == fRec[fCurrIndex].fRect);
661 REPORTER_ASSERT(fReporter, !strcmp(key, fRec[fCurrIndex].fKey));
bungeman38d909e2016-08-02 14:40:46 -0700662 REPORTER_ASSERT(fReporter, value->equals(fRec[fCurrIndex].fValue.get()));
reedf70b5312016-03-04 16:36:20 -0800663 fCurrIndex += 1;
664 }
665};
666
667/*
668 * Test the 3 annotation types by recording them into a picture, serializing, and then playing
669 * them back into another canvas.
670 */
671DEF_TEST(Annotations, reporter) {
672 SkPictureRecorder recorder;
673 SkCanvas* recordingCanvas = recorder.beginRecording(SkRect::MakeWH(100, 100));
halcanary9d524f22016-03-29 09:03:52 -0700674
reedf70b5312016-03-04 16:36:20 -0800675 const char* str0 = "rect-with-url";
676 const SkRect r0 = SkRect::MakeWH(10, 10);
bungeman38d909e2016-08-02 14:40:46 -0700677 sk_sp<SkData> d0(SkData::MakeWithCString(str0));
678 SkAnnotateRectWithURL(recordingCanvas, r0, d0.get());
halcanary9d524f22016-03-29 09:03:52 -0700679
reedf70b5312016-03-04 16:36:20 -0800680 const char* str1 = "named-destination";
681 const SkRect r1 = SkRect::MakeXYWH(5, 5, 0, 0); // collapsed to a point
bungeman38d909e2016-08-02 14:40:46 -0700682 sk_sp<SkData> d1(SkData::MakeWithCString(str1));
683 SkAnnotateNamedDestination(recordingCanvas, {r1.x(), r1.y()}, d1.get());
halcanary9d524f22016-03-29 09:03:52 -0700684
reedf70b5312016-03-04 16:36:20 -0800685 const char* str2 = "link-to-destination";
686 const SkRect r2 = SkRect::MakeXYWH(20, 20, 5, 6);
bungeman38d909e2016-08-02 14:40:46 -0700687 sk_sp<SkData> d2(SkData::MakeWithCString(str2));
688 SkAnnotateLinkToDestination(recordingCanvas, r2, d2.get());
reedf70b5312016-03-04 16:36:20 -0800689
690 const AnnotationRec recs[] = {
bungeman38d909e2016-08-02 14:40:46 -0700691 { r0, SkAnnotationKeys::URL_Key(), std::move(d0) },
692 { r1, SkAnnotationKeys::Define_Named_Dest_Key(), std::move(d1) },
693 { r2, SkAnnotationKeys::Link_Named_Dest_Key(), std::move(d2) },
reedf70b5312016-03-04 16:36:20 -0800694 };
695
reedca2622b2016-03-18 07:25:55 -0700696 sk_sp<SkPicture> pict0(recorder.finishRecordingAsPicture());
697 sk_sp<SkPicture> pict1(copy_picture_via_serialization(pict0.get()));
reedf70b5312016-03-04 16:36:20 -0800698
699 TestAnnotationCanvas canvas(reporter, recs, SK_ARRAY_COUNT(recs));
700 canvas.drawPicture(pict1);
701}
Mike Reed25325842018-03-14 09:52:02 -0400702
703DEF_TEST(WriteBuffer_storage, reporter) {
704 enum {
705 kSize = 32
706 };
707 int32_t storage[kSize/4];
708 char src[kSize];
709 sk_bzero(src, kSize);
710
711 SkBinaryWriteBuffer writer(storage, kSize);
712 REPORTER_ASSERT(reporter, writer.usingInitialStorage());
713 REPORTER_ASSERT(reporter, writer.bytesWritten() == 0);
714 writer.write(src, kSize - 4);
715 REPORTER_ASSERT(reporter, writer.usingInitialStorage());
716 REPORTER_ASSERT(reporter, writer.bytesWritten() == kSize - 4);
717 writer.writeInt(0);
718 REPORTER_ASSERT(reporter, writer.usingInitialStorage());
719 REPORTER_ASSERT(reporter, writer.bytesWritten() == kSize);
720
721 writer.reset(storage, kSize-4);
722 REPORTER_ASSERT(reporter, writer.usingInitialStorage());
723 REPORTER_ASSERT(reporter, writer.bytesWritten() == 0);
724 writer.write(src, kSize - 4);
725 REPORTER_ASSERT(reporter, writer.usingInitialStorage());
726 REPORTER_ASSERT(reporter, writer.bytesWritten() == kSize - 4);
727 writer.writeInt(0);
728 REPORTER_ASSERT(reporter, !writer.usingInitialStorage()); // this is the change
729 REPORTER_ASSERT(reporter, writer.bytesWritten() == kSize);
730}
Khushal42f8bc42018-04-03 17:51:40 -0700731
732DEF_TEST(WriteBuffer_external_memory_textblob, reporter) {
733 SkPaint font;
734 font.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
735 font.setTypeface(SkTypeface::MakeDefault());
736
737 SkTextBlobBuilder builder;
738 int glyph_count = 5;
739 const auto& run = builder.allocRun(font, glyph_count, 1.2f, 2.3f);
740 // allocRun() allocates only the glyph buffer.
741 std::fill(run.glyphs, run.glyphs + glyph_count, 0);
742 auto blob = builder.make();
743 SkSerialProcs procs;
744 SkAutoTMalloc<uint8_t> storage;
745 size_t blob_size = 0u;
746 size_t storage_size = 0u;
747
748 blob_size = SkAlign4(blob->serialize(procs)->size());
749 REPORTER_ASSERT(reporter, blob_size > 4u);
750 storage_size = blob_size - 4;
751 storage.realloc(storage_size);
752 REPORTER_ASSERT(reporter, blob->serialize(procs, storage.get(), storage_size) == 0u);
753 storage_size = blob_size;
754 storage.realloc(storage_size);
755 REPORTER_ASSERT(reporter, blob->serialize(procs, storage.get(), storage_size) != 0u);
756}
757
758DEF_TEST(WriteBuffer_external_memory_flattenable, reporter) {
759 SkScalar intervals[] = {1.f, 1.f};
760 auto path_effect = SkDashPathEffect::Make(intervals, 2, 0);
761 size_t path_size = SkAlign4(path_effect->serialize()->size());
762 REPORTER_ASSERT(reporter, path_size > 4u);
763 SkAutoTMalloc<uint8_t> storage;
764
765 size_t storage_size = path_size - 4;
766 storage.realloc(storage_size);
767 REPORTER_ASSERT(reporter, path_effect->serialize(storage.get(), storage_size) == 0u);
768
769 storage_size = path_size;
770 storage.realloc(storage_size);
771 REPORTER_ASSERT(reporter, path_effect->serialize(storage.get(), storage_size) != 0u);
772}