commit-bot@chromium.org | 0251288 | 2013-10-31 18:37:50 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
egdaniel | 4132de7 | 2016-06-15 14:28:17 -0700 | [diff] [blame] | 8 | #include "Resources.h" |
dvonbeck | 8811e40 | 2016-06-16 12:39:25 -0700 | [diff] [blame] | 9 | #include "SkAnnotationKeys.h" |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 10 | #include "SkCanvas.h" |
Khushal | 42f8bc4 | 2018-04-03 17:51:40 -0700 | [diff] [blame] | 11 | #include "SkDashPathEffect.h" |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 12 | #include "SkFixed.h" |
| 13 | #include "SkFontDescriptor.h" |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 14 | #include "SkImage.h" |
| 15 | #include "SkImageSource.h" |
bungeman | f93d711 | 2016-09-16 06:24:20 -0700 | [diff] [blame] | 16 | #include "SkMakeUnique.h" |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 17 | #include "SkMallocPixelRef.h" |
Cary Clark | 6d6d603 | 2017-10-20 12:14:33 -0400 | [diff] [blame] | 18 | #include "SkMatrixPriv.h" |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 19 | #include "SkOSFile.h" |
Mike Reed | fadbfcd | 2017-12-06 16:09:20 -0500 | [diff] [blame] | 20 | #include "SkReadBuffer.h" |
Cary Clark | efd99cc | 2018-06-11 16:25:43 -0400 | [diff] [blame] | 21 | #include "SkPicturePriv.h" |
robertphillips@google.com | 770963f | 2014-04-18 18:04:41 +0000 | [diff] [blame] | 22 | #include "SkPictureRecorder.h" |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 23 | #include "SkShaderBase.h" |
senorblanco | 91c395a | 2014-09-25 15:51:35 -0700 | [diff] [blame] | 24 | #include "SkTableColorFilter.h" |
commit-bot@chromium.org | 1e7ee99 | 2014-03-14 21:22:22 +0000 | [diff] [blame] | 25 | #include "SkTemplates.h" |
Khushal | 42f8bc4 | 2018-04-03 17:51:40 -0700 | [diff] [blame] | 26 | #include "SkTextBlob.h" |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 27 | #include "SkTypeface.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 28 | #include "SkWriteBuffer.h" |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 29 | #include "SkXfermodeImageFilter.h" |
dvonbeck | 8811e40 | 2016-06-16 12:39:25 -0700 | [diff] [blame] | 30 | #include "sk_tool_utils.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 31 | #include "Test.h" |
commit-bot@chromium.org | 0251288 | 2013-10-31 18:37:50 +0000 | [diff] [blame] | 32 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 33 | static const uint32_t kArraySize = 64; |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 34 | static const int kBitmapSize = 256; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 35 | |
Cary Clark | 6d6d603 | 2017-10-20 12:14:33 -0400 | [diff] [blame] | 36 | class SerializationTest { |
| 37 | public: |
| 38 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 39 | template<typename T> |
| 40 | static void TestAlignment(T* testObj, skiatest::Reporter* reporter) { |
| 41 | // Test memory read/write functions directly |
| 42 | unsigned char dataWritten[1024]; |
| 43 | size_t bytesWrittenToMemory = testObj->writeToMemory(dataWritten); |
| 44 | REPORTER_ASSERT(reporter, SkAlign4(bytesWrittenToMemory) == bytesWrittenToMemory); |
| 45 | size_t bytesReadFromMemory = testObj->readFromMemory(dataWritten, bytesWrittenToMemory); |
| 46 | REPORTER_ASSERT(reporter, SkAlign4(bytesReadFromMemory) == bytesReadFromMemory); |
| 47 | } |
Cary Clark | 6d6d603 | 2017-10-20 12:14:33 -0400 | [diff] [blame] | 48 | }; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 49 | |
| 50 | template<typename T> struct SerializationUtils { |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 51 | // Generic case for flattenables |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 52 | static void Write(SkWriteBuffer& writer, const T* flattenable) { |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 53 | writer.writeFlattenable(flattenable); |
| 54 | } |
Mike Reed | fadbfcd | 2017-12-06 16:09:20 -0500 | [diff] [blame] | 55 | static void Read(SkReadBuffer& reader, T** flattenable) { |
mtklein | 3b37545 | 2016-04-04 14:57:19 -0700 | [diff] [blame] | 56 | *flattenable = (T*)reader.readFlattenable(T::GetFlattenableType()); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 57 | } |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | template<> struct SerializationUtils<SkMatrix> { |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 61 | static void Write(SkWriteBuffer& writer, const SkMatrix* matrix) { |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 62 | writer.writeMatrix(*matrix); |
| 63 | } |
Mike Reed | fadbfcd | 2017-12-06 16:09:20 -0500 | [diff] [blame] | 64 | static void Read(SkReadBuffer& reader, SkMatrix* matrix) { |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 65 | reader.readMatrix(matrix); |
| 66 | } |
| 67 | }; |
| 68 | |
| 69 | template<> struct SerializationUtils<SkPath> { |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 70 | static void Write(SkWriteBuffer& writer, const SkPath* path) { |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 71 | writer.writePath(*path); |
| 72 | } |
Mike Reed | fadbfcd | 2017-12-06 16:09:20 -0500 | [diff] [blame] | 73 | static void Read(SkReadBuffer& reader, SkPath* path) { |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 74 | reader.readPath(path); |
| 75 | } |
| 76 | }; |
| 77 | |
| 78 | template<> struct SerializationUtils<SkRegion> { |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 79 | static void Write(SkWriteBuffer& writer, const SkRegion* region) { |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 80 | writer.writeRegion(*region); |
| 81 | } |
Mike Reed | fadbfcd | 2017-12-06 16:09:20 -0500 | [diff] [blame] | 82 | static void Read(SkReadBuffer& reader, SkRegion* region) { |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 83 | reader.readRegion(region); |
| 84 | } |
| 85 | }; |
| 86 | |
commit-bot@chromium.org | 1ac99c8 | 2014-04-29 15:35:23 +0000 | [diff] [blame] | 87 | template<> struct SerializationUtils<SkString> { |
| 88 | static void Write(SkWriteBuffer& writer, const SkString* string) { |
| 89 | writer.writeString(string->c_str()); |
| 90 | } |
Mike Reed | fadbfcd | 2017-12-06 16:09:20 -0500 | [diff] [blame] | 91 | static void Read(SkReadBuffer& reader, SkString* string) { |
commit-bot@chromium.org | 1ac99c8 | 2014-04-29 15:35:23 +0000 | [diff] [blame] | 92 | reader.readString(string); |
| 93 | } |
| 94 | }; |
| 95 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 96 | template<> struct SerializationUtils<unsigned char> { |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 97 | static void Write(SkWriteBuffer& writer, unsigned char* data, uint32_t arraySize) { |
reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame] | 98 | writer.writeByteArray(data, arraySize); |
sugoi@google.com | 305f78e | 2013-11-04 16:18:15 +0000 | [diff] [blame] | 99 | } |
Mike Reed | fadbfcd | 2017-12-06 16:09:20 -0500 | [diff] [blame] | 100 | static bool Read(SkReadBuffer& reader, unsigned char* data, uint32_t arraySize) { |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 101 | return reader.readByteArray(data, arraySize); |
| 102 | } |
| 103 | }; |
commit-bot@chromium.org | 0251288 | 2013-10-31 18:37:50 +0000 | [diff] [blame] | 104 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 105 | template<> struct SerializationUtils<SkColor> { |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 106 | static void Write(SkWriteBuffer& writer, SkColor* data, uint32_t arraySize) { |
reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame] | 107 | writer.writeColorArray(data, arraySize); |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 108 | } |
Mike Reed | fadbfcd | 2017-12-06 16:09:20 -0500 | [diff] [blame] | 109 | static bool Read(SkReadBuffer& reader, SkColor* data, uint32_t arraySize) { |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 110 | return reader.readColorArray(data, arraySize); |
| 111 | } |
| 112 | }; |
sugoi@google.com | 305f78e | 2013-11-04 16:18:15 +0000 | [diff] [blame] | 113 | |
brianosman | 97bbf82 | 2016-09-25 13:15:58 -0700 | [diff] [blame] | 114 | template<> struct SerializationUtils<SkColor4f> { |
| 115 | static void Write(SkWriteBuffer& writer, SkColor4f* data, uint32_t arraySize) { |
| 116 | writer.writeColor4fArray(data, arraySize); |
| 117 | } |
Mike Reed | fadbfcd | 2017-12-06 16:09:20 -0500 | [diff] [blame] | 118 | static bool Read(SkReadBuffer& reader, SkColor4f* data, uint32_t arraySize) { |
brianosman | 97bbf82 | 2016-09-25 13:15:58 -0700 | [diff] [blame] | 119 | return reader.readColor4fArray(data, arraySize); |
| 120 | } |
| 121 | }; |
| 122 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 123 | template<> struct SerializationUtils<int32_t> { |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 124 | static void Write(SkWriteBuffer& writer, int32_t* data, uint32_t arraySize) { |
reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame] | 125 | writer.writeIntArray(data, arraySize); |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 126 | } |
Mike Reed | fadbfcd | 2017-12-06 16:09:20 -0500 | [diff] [blame] | 127 | static bool Read(SkReadBuffer& reader, int32_t* data, uint32_t arraySize) { |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 128 | return reader.readIntArray(data, arraySize); |
| 129 | } |
| 130 | }; |
sugoi@google.com | 305f78e | 2013-11-04 16:18:15 +0000 | [diff] [blame] | 131 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 132 | template<> struct SerializationUtils<SkPoint> { |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 133 | static void Write(SkWriteBuffer& writer, SkPoint* data, uint32_t arraySize) { |
reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame] | 134 | writer.writePointArray(data, arraySize); |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 135 | } |
Mike Reed | fadbfcd | 2017-12-06 16:09:20 -0500 | [diff] [blame] | 136 | static bool Read(SkReadBuffer& reader, SkPoint* data, uint32_t arraySize) { |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 137 | return reader.readPointArray(data, arraySize); |
| 138 | } |
| 139 | }; |
reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame] | 140 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 141 | template<> struct SerializationUtils<SkScalar> { |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 142 | static void Write(SkWriteBuffer& writer, SkScalar* data, uint32_t arraySize) { |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 143 | writer.writeScalarArray(data, arraySize); |
| 144 | } |
Mike Reed | fadbfcd | 2017-12-06 16:09:20 -0500 | [diff] [blame] | 145 | static bool Read(SkReadBuffer& reader, SkScalar* data, uint32_t arraySize) { |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 146 | return reader.readScalarArray(data, arraySize); |
| 147 | } |
| 148 | }; |
reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame] | 149 | |
commit-bot@chromium.org | 1ac99c8 | 2014-04-29 15:35:23 +0000 | [diff] [blame] | 150 | template<typename T, bool testInvalid> struct SerializationTestUtils { |
| 151 | static void InvalidateData(unsigned char* data) {} |
| 152 | }; |
| 153 | |
| 154 | template<> struct SerializationTestUtils<SkString, true> { |
| 155 | static void InvalidateData(unsigned char* data) { |
| 156 | data[3] |= 0x80; // Reverse sign of 1st integer |
| 157 | } |
| 158 | }; |
| 159 | |
| 160 | template<typename T, bool testInvalid> |
| 161 | static void TestObjectSerializationNoAlign(T* testObj, skiatest::Reporter* reporter) { |
brianosman | fad9856 | 2016-05-04 11:06:28 -0700 | [diff] [blame] | 162 | SkBinaryWriteBuffer writer; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 163 | SerializationUtils<T>::Write(writer, testObj); |
| 164 | size_t bytesWritten = writer.bytesWritten(); |
| 165 | REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten); |
reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame] | 166 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 167 | unsigned char dataWritten[1024]; |
| 168 | writer.writeToMemory(dataWritten); |
| 169 | |
commit-bot@chromium.org | 1ac99c8 | 2014-04-29 15:35:23 +0000 | [diff] [blame] | 170 | SerializationTestUtils<T, testInvalid>::InvalidateData(dataWritten); |
| 171 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 172 | // Make sure this fails when it should (test with smaller size, but still multiple of 4) |
Mike Reed | fadbfcd | 2017-12-06 16:09:20 -0500 | [diff] [blame] | 173 | SkReadBuffer buffer(dataWritten, bytesWritten - 4); |
commit-bot@chromium.org | 8f457e3 | 2013-11-08 19:22:57 +0000 | [diff] [blame] | 174 | T obj; |
| 175 | SerializationUtils<T>::Read(buffer, &obj); |
commit-bot@chromium.org | c2e9db3 | 2013-12-06 20:14:46 +0000 | [diff] [blame] | 176 | REPORTER_ASSERT(reporter, !buffer.isValid()); |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 177 | |
| 178 | // Make sure this succeeds when it should |
Mike Reed | fadbfcd | 2017-12-06 16:09:20 -0500 | [diff] [blame] | 179 | SkReadBuffer buffer2(dataWritten, bytesWritten); |
Robert Phillips | b252604 | 2016-09-26 09:00:36 -0400 | [diff] [blame] | 180 | size_t offsetBefore = buffer2.offset(); |
commit-bot@chromium.org | 8f457e3 | 2013-11-08 19:22:57 +0000 | [diff] [blame] | 181 | T obj2; |
| 182 | SerializationUtils<T>::Read(buffer2, &obj2); |
Robert Phillips | b252604 | 2016-09-26 09:00:36 -0400 | [diff] [blame] | 183 | size_t offsetAfter = buffer2.offset(); |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 184 | // This should have succeeded, since there are enough bytes to read this |
commit-bot@chromium.org | 1ac99c8 | 2014-04-29 15:35:23 +0000 | [diff] [blame] | 185 | REPORTER_ASSERT(reporter, buffer2.isValid() == !testInvalid); |
| 186 | // Note: This following test should always succeed, regardless of whether the buffer is valid, |
| 187 | // since if it is invalid, it will simply skip to the end, as if it had read the whole buffer. |
Robert Phillips | b252604 | 2016-09-26 09:00:36 -0400 | [diff] [blame] | 188 | REPORTER_ASSERT(reporter, offsetAfter - offsetBefore == bytesWritten); |
commit-bot@chromium.org | 1ac99c8 | 2014-04-29 15:35:23 +0000 | [diff] [blame] | 189 | } |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 190 | |
commit-bot@chromium.org | 1ac99c8 | 2014-04-29 15:35:23 +0000 | [diff] [blame] | 191 | template<typename T> |
| 192 | static void TestObjectSerialization(T* testObj, skiatest::Reporter* reporter) { |
| 193 | TestObjectSerializationNoAlign<T, false>(testObj, reporter); |
Cary Clark | 6d6d603 | 2017-10-20 12:14:33 -0400 | [diff] [blame] | 194 | SerializationTest::TestAlignment(testObj, reporter); |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | template<typename T> |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 198 | static T* TestFlattenableSerialization(T* testObj, bool shouldSucceed, |
| 199 | skiatest::Reporter* reporter) { |
brianosman | fad9856 | 2016-05-04 11:06:28 -0700 | [diff] [blame] | 200 | SkBinaryWriteBuffer writer; |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 201 | SerializationUtils<T>::Write(writer, testObj); |
| 202 | size_t bytesWritten = writer.bytesWritten(); |
| 203 | REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten); |
| 204 | |
dvonbeck | 8811e40 | 2016-06-16 12:39:25 -0700 | [diff] [blame] | 205 | SkASSERT(bytesWritten <= 4096); |
senorblanco | 91c395a | 2014-09-25 15:51:35 -0700 | [diff] [blame] | 206 | unsigned char dataWritten[4096]; |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 207 | writer.writeToMemory(dataWritten); |
| 208 | |
| 209 | // Make sure this fails when it should (test with smaller size, but still multiple of 4) |
Mike Reed | fadbfcd | 2017-12-06 16:09:20 -0500 | [diff] [blame] | 210 | SkReadBuffer buffer(dataWritten, bytesWritten - 4); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 211 | T* obj = nullptr; |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 212 | SerializationUtils<T>::Read(buffer, &obj); |
commit-bot@chromium.org | c2e9db3 | 2013-12-06 20:14:46 +0000 | [diff] [blame] | 213 | REPORTER_ASSERT(reporter, !buffer.isValid()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 214 | REPORTER_ASSERT(reporter, nullptr == obj); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 215 | |
| 216 | // Make sure this succeeds when it should |
Mike Reed | fadbfcd | 2017-12-06 16:09:20 -0500 | [diff] [blame] | 217 | SkReadBuffer buffer2(dataWritten, bytesWritten); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 218 | const unsigned char* peekBefore = static_cast<const unsigned char*>(buffer2.skip(0)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 219 | T* obj2 = nullptr; |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 220 | SerializationUtils<T>::Read(buffer2, &obj2); |
| 221 | const unsigned char* peekAfter = static_cast<const unsigned char*>(buffer2.skip(0)); |
| 222 | if (shouldSucceed) { |
| 223 | // This should have succeeded, since there are enough bytes to read this |
commit-bot@chromium.org | c2e9db3 | 2013-12-06 20:14:46 +0000 | [diff] [blame] | 224 | REPORTER_ASSERT(reporter, buffer2.isValid()); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 225 | REPORTER_ASSERT(reporter, static_cast<size_t>(peekAfter - peekBefore) == bytesWritten); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 226 | REPORTER_ASSERT(reporter, obj2); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 227 | } else { |
| 228 | // If the deserialization was supposed to fail, make sure it did |
commit-bot@chromium.org | c2e9db3 | 2013-12-06 20:14:46 +0000 | [diff] [blame] | 229 | REPORTER_ASSERT(reporter, !buffer.isValid()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 230 | REPORTER_ASSERT(reporter, nullptr == obj2); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | return obj2; // Return object to perform further validity tests on it |
| 234 | } |
| 235 | |
| 236 | template<typename T> |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 237 | static void TestArraySerialization(T* data, skiatest::Reporter* reporter) { |
brianosman | fad9856 | 2016-05-04 11:06:28 -0700 | [diff] [blame] | 238 | SkBinaryWriteBuffer writer; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 239 | SerializationUtils<T>::Write(writer, data, kArraySize); |
| 240 | size_t bytesWritten = writer.bytesWritten(); |
| 241 | // This should write the length (in 4 bytes) and the array |
| 242 | REPORTER_ASSERT(reporter, (4 + kArraySize * sizeof(T)) == bytesWritten); |
| 243 | |
brianosman | 97bbf82 | 2016-09-25 13:15:58 -0700 | [diff] [blame] | 244 | unsigned char dataWritten[2048]; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 245 | writer.writeToMemory(dataWritten); |
| 246 | |
| 247 | // Make sure this fails when it should |
Mike Reed | fadbfcd | 2017-12-06 16:09:20 -0500 | [diff] [blame] | 248 | SkReadBuffer buffer(dataWritten, bytesWritten); |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 249 | T dataRead[kArraySize]; |
| 250 | bool success = SerializationUtils<T>::Read(buffer, dataRead, kArraySize / 2); |
| 251 | // This should have failed, since the provided size was too small |
| 252 | REPORTER_ASSERT(reporter, !success); |
| 253 | |
| 254 | // Make sure this succeeds when it should |
Mike Reed | fadbfcd | 2017-12-06 16:09:20 -0500 | [diff] [blame] | 255 | SkReadBuffer buffer2(dataWritten, bytesWritten); |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 256 | success = SerializationUtils<T>::Read(buffer2, dataRead, kArraySize); |
| 257 | // This should have succeeded, since there are enough bytes to read this |
| 258 | REPORTER_ASSERT(reporter, success); |
| 259 | } |
| 260 | |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 261 | static void TestBitmapSerialization(const SkBitmap& validBitmap, |
| 262 | const SkBitmap& invalidBitmap, |
| 263 | bool shouldSucceed, |
| 264 | skiatest::Reporter* reporter) { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 265 | sk_sp<SkImage> validImage(SkImage::MakeFromBitmap(validBitmap)); |
robertphillips | 549c899 | 2016-04-01 09:28:51 -0700 | [diff] [blame] | 266 | sk_sp<SkImageFilter> validBitmapSource(SkImageSource::Make(std::move(validImage))); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 267 | sk_sp<SkImage> invalidImage(SkImage::MakeFromBitmap(invalidBitmap)); |
robertphillips | 549c899 | 2016-04-01 09:28:51 -0700 | [diff] [blame] | 268 | sk_sp<SkImageFilter> invalidBitmapSource(SkImageSource::Make(std::move(invalidImage))); |
reed | cfb6bdf | 2016-03-29 11:32:50 -0700 | [diff] [blame] | 269 | sk_sp<SkImageFilter> xfermodeImageFilter( |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 270 | SkXfermodeImageFilter::Make(SkBlendMode::kSrcOver, |
robertphillips | 8c0326d | 2016-04-05 12:48:34 -0700 | [diff] [blame] | 271 | std::move(invalidBitmapSource), |
| 272 | std::move(validBitmapSource), nullptr)); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 273 | |
Mike Reed | 5e25717 | 2016-11-01 11:22:05 -0400 | [diff] [blame] | 274 | sk_sp<SkImageFilter> deserializedFilter( |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 275 | TestFlattenableSerialization<SkImageFilter>( |
reed | cfb6bdf | 2016-03-29 11:32:50 -0700 | [diff] [blame] | 276 | xfermodeImageFilter.get(), shouldSucceed, reporter)); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 277 | |
| 278 | // Try to render a small bitmap using the invalid deserialized filter |
| 279 | // to make sure we don't crash while trying to render it |
| 280 | if (shouldSucceed) { |
| 281 | SkBitmap bitmap; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 282 | bitmap.allocN32Pixels(24, 24); |
| 283 | SkCanvas canvas(bitmap); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 284 | canvas.clear(0x00000000); |
| 285 | SkPaint paint; |
| 286 | paint.setImageFilter(deserializedFilter); |
| 287 | canvas.clipRect(SkRect::MakeXYWH(0, 0, SkIntToScalar(24), SkIntToScalar(24))); |
| 288 | canvas.drawBitmap(bitmap, 0, 0, &paint); |
| 289 | } |
| 290 | } |
| 291 | |
senorblanco | 91c395a | 2014-09-25 15:51:35 -0700 | [diff] [blame] | 292 | static void TestColorFilterSerialization(skiatest::Reporter* reporter) { |
| 293 | uint8_t table[256]; |
| 294 | for (int i = 0; i < 256; ++i) { |
| 295 | table[i] = (i * 41) % 256; |
| 296 | } |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 297 | auto colorFilter(SkTableColorFilter::Make(table)); |
Hal Canary | 342b7ac | 2016-11-04 11:49:42 -0400 | [diff] [blame] | 298 | sk_sp<SkColorFilter> copy( |
senorblanco | 91c395a | 2014-09-25 15:51:35 -0700 | [diff] [blame] | 299 | TestFlattenableSerialization<SkColorFilter>(colorFilter.get(), true, reporter)); |
| 300 | } |
| 301 | |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 302 | static SkBitmap draw_picture(SkPicture& picture) { |
| 303 | SkBitmap bitmap; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 304 | bitmap.allocN32Pixels(SkScalarCeilToInt(picture.cullRect().width()), |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 305 | SkScalarCeilToInt(picture.cullRect().height())); |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 306 | SkCanvas canvas(bitmap); |
robertphillips | c5ba71d | 2014-09-04 08:42:50 -0700 | [diff] [blame] | 307 | picture.playback(&canvas); |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 308 | return bitmap; |
| 309 | } |
| 310 | |
| 311 | static void compare_bitmaps(skiatest::Reporter* reporter, |
| 312 | const SkBitmap& b1, const SkBitmap& b2) { |
| 313 | REPORTER_ASSERT(reporter, b1.width() == b2.width()); |
| 314 | REPORTER_ASSERT(reporter, b1.height() == b2.height()); |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 315 | |
| 316 | if ((b1.width() != b2.width()) || |
| 317 | (b1.height() != b2.height())) { |
| 318 | return; |
| 319 | } |
| 320 | |
| 321 | int pixelErrors = 0; |
| 322 | for (int y = 0; y < b2.height(); ++y) { |
| 323 | for (int x = 0; x < b2.width(); ++x) { |
| 324 | if (b1.getColor(x, y) != b2.getColor(x, y)) |
| 325 | ++pixelErrors; |
| 326 | } |
| 327 | } |
| 328 | REPORTER_ASSERT(reporter, 0 == pixelErrors); |
| 329 | } |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 330 | static void serialize_and_compare_typeface(sk_sp<SkTypeface> typeface, const char* text, |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 331 | skiatest::Reporter* reporter) |
| 332 | { |
| 333 | // Create a paint with the typeface. |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 334 | SkPaint paint; |
| 335 | paint.setColor(SK_ColorGRAY); |
| 336 | paint.setTextSize(SkIntToScalar(30)); |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 337 | paint.setTypeface(std::move(typeface)); |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 338 | |
| 339 | // Paint some text. |
| 340 | SkPictureRecorder recorder; |
| 341 | SkIRect canvasRect = SkIRect::MakeWH(kBitmapSize, kBitmapSize); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 342 | SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(canvasRect.width()), |
| 343 | SkIntToScalar(canvasRect.height()), |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 344 | nullptr, 0); |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 345 | canvas->drawColor(SK_ColorWHITE); |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 346 | canvas->drawText(text, 2, 24, 32, paint); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 347 | sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture()); |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 348 | |
| 349 | // Serlialize picture and create its clone from stream. |
| 350 | SkDynamicMemoryWStream stream; |
| 351 | picture->serialize(&stream); |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 352 | std::unique_ptr<SkStream> inputStream(stream.detachAsStream()); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 353 | sk_sp<SkPicture> loadedPicture(SkPicture::MakeFromStream(inputStream.get())); |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 354 | |
| 355 | // Draw both original and clone picture and compare bitmaps -- they should be identical. |
| 356 | SkBitmap origBitmap = draw_picture(*picture); |
| 357 | SkBitmap destBitmap = draw_picture(*loadedPicture); |
| 358 | compare_bitmaps(reporter, origBitmap, destBitmap); |
| 359 | } |
| 360 | |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 361 | static void TestPictureTypefaceSerialization(skiatest::Reporter* reporter) { |
| 362 | { |
| 363 | // Load typeface from file to test CreateFromFile with index. |
Mike Reed | 0933bc9 | 2017-12-09 01:27:41 +0000 | [diff] [blame] | 364 | auto data = GetResourceAsData("fonts/test.ttc"); |
| 365 | auto typeface = SkTypeface::MakeFromStream(new SkMemoryStream(std::move(data)), 1); |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 366 | if (!typeface) { |
halcanary | 7d57124 | 2016-02-24 17:59:16 -0800 | [diff] [blame] | 367 | INFOF(reporter, "Could not run fontstream test because test.ttc not found."); |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 368 | } else { |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 369 | serialize_and_compare_typeface(std::move(typeface), "A!", reporter); |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 370 | } |
| 371 | } |
| 372 | |
| 373 | { |
| 374 | // Load typeface as stream to create with axis settings. |
Mike Reed | 0933bc9 | 2017-12-09 01:27:41 +0000 | [diff] [blame] | 375 | std::unique_ptr<SkStreamAsset> distortable(GetResourceAsStream("fonts/Distortable.ttf")); |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 376 | if (!distortable) { |
halcanary | 7d57124 | 2016-02-24 17:59:16 -0800 | [diff] [blame] | 377 | INFOF(reporter, "Could not run fontstream test because Distortable.ttf not found."); |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 378 | } else { |
| 379 | SkFixed axis = SK_FixedSqrt2; |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 380 | sk_sp<SkTypeface> typeface(SkTypeface::MakeFromFontData( |
bungeman | f93d711 | 2016-09-16 06:24:20 -0700 | [diff] [blame] | 381 | skstd::make_unique<SkFontData>(std::move(distortable), 0, &axis, 1))); |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 382 | if (!typeface) { |
halcanary | 7d57124 | 2016-02-24 17:59:16 -0800 | [diff] [blame] | 383 | INFOF(reporter, "Could not run fontstream test because Distortable.ttf not created."); |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 384 | } else { |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 385 | serialize_and_compare_typeface(std::move(typeface), "abc", reporter); |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 386 | } |
| 387 | } |
| 388 | } |
| 389 | } |
| 390 | |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 391 | static void setup_bitmap_for_canvas(SkBitmap* bitmap) { |
| 392 | bitmap->allocN32Pixels(kBitmapSize, kBitmapSize); |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 393 | } |
| 394 | |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 395 | static void make_checkerboard_bitmap(SkBitmap& bitmap) { |
| 396 | setup_bitmap_for_canvas(&bitmap); |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 397 | |
| 398 | SkCanvas canvas(bitmap); |
| 399 | canvas.clear(0x00000000); |
| 400 | SkPaint darkPaint; |
| 401 | darkPaint.setColor(0xFF804020); |
| 402 | SkPaint lightPaint; |
| 403 | lightPaint.setColor(0xFF244484); |
| 404 | const int i = kBitmapSize / 8; |
| 405 | const SkScalar f = SkIntToScalar(i); |
| 406 | for (int y = 0; y < kBitmapSize; y += i) { |
| 407 | for (int x = 0; x < kBitmapSize; x += i) { |
| 408 | canvas.save(); |
| 409 | canvas.translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 410 | canvas.drawRect(SkRect::MakeXYWH(0, 0, f, f), darkPaint); |
| 411 | canvas.drawRect(SkRect::MakeXYWH(f, 0, f, f), lightPaint); |
| 412 | canvas.drawRect(SkRect::MakeXYWH(0, f, f, f), lightPaint); |
| 413 | canvas.drawRect(SkRect::MakeXYWH(f, f, f, f), darkPaint); |
| 414 | canvas.restore(); |
| 415 | } |
| 416 | } |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 417 | } |
| 418 | |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 419 | static void draw_something(SkCanvas* canvas) { |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 420 | SkPaint paint; |
| 421 | SkBitmap bitmap; |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 422 | make_checkerboard_bitmap(bitmap); |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 423 | |
| 424 | canvas->save(); |
| 425 | canvas->scale(0.5f, 0.5f); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 426 | canvas->drawBitmap(bitmap, 0, 0, nullptr); |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 427 | canvas->restore(); |
| 428 | |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 429 | paint.setAntiAlias(true); |
| 430 | |
| 431 | paint.setColor(SK_ColorRED); |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 432 | canvas->drawCircle(SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/3), paint); |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 433 | paint.setColor(SK_ColorBLACK); |
| 434 | paint.setTextSize(SkIntToScalar(kBitmapSize/3)); |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 435 | canvas->drawString("Picture", SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/4), paint); |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 436 | } |
| 437 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 438 | DEF_TEST(Serialization, reporter) { |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 439 | // Test matrix serialization |
| 440 | { |
| 441 | SkMatrix matrix = SkMatrix::I(); |
| 442 | TestObjectSerialization(&matrix, reporter); |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 443 | } |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 444 | |
| 445 | // Test path serialization |
| 446 | { |
| 447 | SkPath path; |
| 448 | TestObjectSerialization(&path, reporter); |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 449 | } |
sugoi@google.com | 305f78e | 2013-11-04 16:18:15 +0000 | [diff] [blame] | 450 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 451 | // Test region serialization |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 452 | { |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 453 | SkRegion region; |
| 454 | TestObjectSerialization(®ion, reporter); |
| 455 | } |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 456 | |
senorblanco | 91c395a | 2014-09-25 15:51:35 -0700 | [diff] [blame] | 457 | // Test color filter serialization |
| 458 | { |
| 459 | TestColorFilterSerialization(reporter); |
| 460 | } |
| 461 | |
commit-bot@chromium.org | 1ac99c8 | 2014-04-29 15:35:23 +0000 | [diff] [blame] | 462 | // Test string serialization |
| 463 | { |
| 464 | SkString string("string"); |
| 465 | TestObjectSerializationNoAlign<SkString, false>(&string, reporter); |
| 466 | TestObjectSerializationNoAlign<SkString, true>(&string, reporter); |
| 467 | } |
| 468 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 469 | // Test rrect serialization |
| 470 | { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 471 | // SkRRect does not initialize anything. |
| 472 | // An uninitialized SkRRect can be serialized, |
| 473 | // but will branch on uninitialized data when deserialized. |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 474 | SkRRect rrect; |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 475 | SkRect rect = SkRect::MakeXYWH(1, 2, 20, 30); |
| 476 | SkVector corners[4] = { {1, 2}, {2, 3}, {3,4}, {4,5} }; |
| 477 | rrect.setRectRadii(rect, corners); |
Cary Clark | 6d6d603 | 2017-10-20 12:14:33 -0400 | [diff] [blame] | 478 | SerializationTest::TestAlignment(&rrect, reporter); |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 479 | } |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 480 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 481 | // Test readByteArray |
| 482 | { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 483 | unsigned char data[kArraySize] = { 1, 2, 3 }; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 484 | TestArraySerialization(data, reporter); |
| 485 | } |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 486 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 487 | // Test readColorArray |
| 488 | { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 489 | SkColor data[kArraySize] = { SK_ColorBLACK, SK_ColorWHITE, SK_ColorRED }; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 490 | TestArraySerialization(data, reporter); |
| 491 | } |
| 492 | |
brianosman | 97bbf82 | 2016-09-25 13:15:58 -0700 | [diff] [blame] | 493 | // Test readColor4fArray |
| 494 | { |
| 495 | SkColor4f data[kArraySize] = { |
| 496 | SkColor4f::FromColor(SK_ColorBLACK), |
| 497 | SkColor4f::FromColor(SK_ColorWHITE), |
| 498 | SkColor4f::FromColor(SK_ColorRED), |
| 499 | { 1.f, 2.f, 4.f, 8.f } |
| 500 | }; |
| 501 | TestArraySerialization(data, reporter); |
| 502 | } |
| 503 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 504 | // Test readIntArray |
| 505 | { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 506 | int32_t data[kArraySize] = { 1, 2, 4, 8 }; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 507 | TestArraySerialization(data, reporter); |
| 508 | } |
| 509 | |
| 510 | // Test readPointArray |
| 511 | { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 512 | SkPoint data[kArraySize] = { {6, 7}, {42, 128} }; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 513 | TestArraySerialization(data, reporter); |
| 514 | } |
| 515 | |
| 516 | // Test readScalarArray |
| 517 | { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 518 | SkScalar data[kArraySize] = { SK_Scalar1, SK_ScalarHalf, SK_ScalarMax }; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 519 | TestArraySerialization(data, reporter); |
commit-bot@chromium.org | 0251288 | 2013-10-31 18:37:50 +0000 | [diff] [blame] | 520 | } |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 521 | |
| 522 | // Test invalid deserializations |
| 523 | { |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 524 | SkImageInfo info = SkImageInfo::MakeN32Premul(kBitmapSize, kBitmapSize); |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 525 | |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 526 | SkBitmap validBitmap; |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 527 | validBitmap.setInfo(info); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 528 | |
| 529 | // Create a bitmap with a really large height |
| 530 | SkBitmap invalidBitmap; |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 531 | invalidBitmap.setInfo(info.makeWH(info.width(), 1000000000)); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 532 | |
| 533 | // The deserialization should succeed, and the rendering shouldn't crash, |
| 534 | // even when the device fails to initialize, due to its size |
| 535 | TestBitmapSerialization(validBitmap, invalidBitmap, true, reporter); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 536 | } |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 537 | |
| 538 | // Test simple SkPicture serialization |
| 539 | { |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 540 | SkPictureRecorder recorder; |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 541 | draw_something(recorder.beginRecording(SkIntToScalar(kBitmapSize), |
| 542 | SkIntToScalar(kBitmapSize), |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 543 | nullptr, 0)); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 544 | sk_sp<SkPicture> pict(recorder.finishRecordingAsPicture()); |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 545 | |
| 546 | // Serialize picture |
brianosman | fad9856 | 2016-05-04 11:06:28 -0700 | [diff] [blame] | 547 | SkBinaryWriteBuffer writer; |
Cary Clark | efd99cc | 2018-06-11 16:25:43 -0400 | [diff] [blame] | 548 | SkPicturePriv::Flatten(pict, writer); |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 549 | size_t size = writer.bytesWritten(); |
commit-bot@chromium.org | 1e7ee99 | 2014-03-14 21:22:22 +0000 | [diff] [blame] | 550 | SkAutoTMalloc<unsigned char> data(size); |
| 551 | writer.writeToMemory(static_cast<void*>(data.get())); |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 552 | |
| 553 | // Deserialize picture |
Mike Reed | fadbfcd | 2017-12-06 16:09:20 -0500 | [diff] [blame] | 554 | SkReadBuffer reader(static_cast<void*>(data.get()), size); |
Cary Clark | efd99cc | 2018-06-11 16:25:43 -0400 | [diff] [blame] | 555 | sk_sp<SkPicture> readPict(SkPicturePriv::MakeFromBuffer(reader)); |
reed | d921dbb | 2016-09-30 09:27:20 -0700 | [diff] [blame] | 556 | REPORTER_ASSERT(reporter, reader.isValid()); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 557 | REPORTER_ASSERT(reporter, readPict.get()); |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 558 | } |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 559 | |
| 560 | TestPictureTypefaceSerialization(reporter); |
commit-bot@chromium.org | 0251288 | 2013-10-31 18:37:50 +0000 | [diff] [blame] | 561 | } |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 562 | |
| 563 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 564 | #include "SkAnnotation.h" |
| 565 | |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 566 | static sk_sp<SkPicture> copy_picture_via_serialization(SkPicture* src) { |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 567 | SkDynamicMemoryWStream wstream; |
| 568 | src->serialize(&wstream); |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 569 | std::unique_ptr<SkStreamAsset> rstream(wstream.detachAsStream()); |
| 570 | return SkPicture::MakeFromStream(rstream.get()); |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | struct AnnotationRec { |
| 574 | const SkRect fRect; |
| 575 | const char* fKey; |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 576 | sk_sp<SkData> fValue; |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 577 | }; |
| 578 | |
| 579 | class TestAnnotationCanvas : public SkCanvas { |
| 580 | skiatest::Reporter* fReporter; |
| 581 | const AnnotationRec* fRec; |
| 582 | int fCount; |
| 583 | int fCurrIndex; |
| 584 | |
| 585 | public: |
| 586 | TestAnnotationCanvas(skiatest::Reporter* reporter, const AnnotationRec rec[], int count) |
| 587 | : SkCanvas(100, 100) |
| 588 | , fReporter(reporter) |
| 589 | , fRec(rec) |
| 590 | , fCount(count) |
| 591 | , fCurrIndex(0) |
| 592 | {} |
| 593 | |
| 594 | ~TestAnnotationCanvas() { |
| 595 | REPORTER_ASSERT(fReporter, fCount == fCurrIndex); |
| 596 | } |
| 597 | |
| 598 | protected: |
| 599 | void onDrawAnnotation(const SkRect& rect, const char key[], SkData* value) { |
| 600 | REPORTER_ASSERT(fReporter, fCurrIndex < fCount); |
| 601 | REPORTER_ASSERT(fReporter, rect == fRec[fCurrIndex].fRect); |
| 602 | REPORTER_ASSERT(fReporter, !strcmp(key, fRec[fCurrIndex].fKey)); |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 603 | REPORTER_ASSERT(fReporter, value->equals(fRec[fCurrIndex].fValue.get())); |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 604 | fCurrIndex += 1; |
| 605 | } |
| 606 | }; |
| 607 | |
| 608 | /* |
| 609 | * Test the 3 annotation types by recording them into a picture, serializing, and then playing |
| 610 | * them back into another canvas. |
| 611 | */ |
| 612 | DEF_TEST(Annotations, reporter) { |
| 613 | SkPictureRecorder recorder; |
| 614 | SkCanvas* recordingCanvas = recorder.beginRecording(SkRect::MakeWH(100, 100)); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 615 | |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 616 | const char* str0 = "rect-with-url"; |
| 617 | const SkRect r0 = SkRect::MakeWH(10, 10); |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 618 | sk_sp<SkData> d0(SkData::MakeWithCString(str0)); |
| 619 | SkAnnotateRectWithURL(recordingCanvas, r0, d0.get()); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 620 | |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 621 | const char* str1 = "named-destination"; |
| 622 | const SkRect r1 = SkRect::MakeXYWH(5, 5, 0, 0); // collapsed to a point |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 623 | sk_sp<SkData> d1(SkData::MakeWithCString(str1)); |
| 624 | SkAnnotateNamedDestination(recordingCanvas, {r1.x(), r1.y()}, d1.get()); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 625 | |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 626 | const char* str2 = "link-to-destination"; |
| 627 | const SkRect r2 = SkRect::MakeXYWH(20, 20, 5, 6); |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 628 | sk_sp<SkData> d2(SkData::MakeWithCString(str2)); |
| 629 | SkAnnotateLinkToDestination(recordingCanvas, r2, d2.get()); |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 630 | |
| 631 | const AnnotationRec recs[] = { |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 632 | { r0, SkAnnotationKeys::URL_Key(), std::move(d0) }, |
| 633 | { r1, SkAnnotationKeys::Define_Named_Dest_Key(), std::move(d1) }, |
| 634 | { r2, SkAnnotationKeys::Link_Named_Dest_Key(), std::move(d2) }, |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 635 | }; |
| 636 | |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 637 | sk_sp<SkPicture> pict0(recorder.finishRecordingAsPicture()); |
| 638 | sk_sp<SkPicture> pict1(copy_picture_via_serialization(pict0.get())); |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 639 | |
| 640 | TestAnnotationCanvas canvas(reporter, recs, SK_ARRAY_COUNT(recs)); |
| 641 | canvas.drawPicture(pict1); |
| 642 | } |
Mike Reed | 2532584 | 2018-03-14 09:52:02 -0400 | [diff] [blame] | 643 | |
| 644 | DEF_TEST(WriteBuffer_storage, reporter) { |
| 645 | enum { |
| 646 | kSize = 32 |
| 647 | }; |
| 648 | int32_t storage[kSize/4]; |
| 649 | char src[kSize]; |
| 650 | sk_bzero(src, kSize); |
| 651 | |
| 652 | SkBinaryWriteBuffer writer(storage, kSize); |
| 653 | REPORTER_ASSERT(reporter, writer.usingInitialStorage()); |
| 654 | REPORTER_ASSERT(reporter, writer.bytesWritten() == 0); |
| 655 | writer.write(src, kSize - 4); |
| 656 | REPORTER_ASSERT(reporter, writer.usingInitialStorage()); |
| 657 | REPORTER_ASSERT(reporter, writer.bytesWritten() == kSize - 4); |
| 658 | writer.writeInt(0); |
| 659 | REPORTER_ASSERT(reporter, writer.usingInitialStorage()); |
| 660 | REPORTER_ASSERT(reporter, writer.bytesWritten() == kSize); |
| 661 | |
| 662 | writer.reset(storage, kSize-4); |
| 663 | REPORTER_ASSERT(reporter, writer.usingInitialStorage()); |
| 664 | REPORTER_ASSERT(reporter, writer.bytesWritten() == 0); |
| 665 | writer.write(src, kSize - 4); |
| 666 | REPORTER_ASSERT(reporter, writer.usingInitialStorage()); |
| 667 | REPORTER_ASSERT(reporter, writer.bytesWritten() == kSize - 4); |
| 668 | writer.writeInt(0); |
| 669 | REPORTER_ASSERT(reporter, !writer.usingInitialStorage()); // this is the change |
| 670 | REPORTER_ASSERT(reporter, writer.bytesWritten() == kSize); |
| 671 | } |
Khushal | 42f8bc4 | 2018-04-03 17:51:40 -0700 | [diff] [blame] | 672 | |
| 673 | DEF_TEST(WriteBuffer_external_memory_textblob, reporter) { |
| 674 | SkPaint font; |
| 675 | font.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
| 676 | font.setTypeface(SkTypeface::MakeDefault()); |
| 677 | |
| 678 | SkTextBlobBuilder builder; |
| 679 | int glyph_count = 5; |
| 680 | const auto& run = builder.allocRun(font, glyph_count, 1.2f, 2.3f); |
| 681 | // allocRun() allocates only the glyph buffer. |
| 682 | std::fill(run.glyphs, run.glyphs + glyph_count, 0); |
| 683 | auto blob = builder.make(); |
| 684 | SkSerialProcs procs; |
| 685 | SkAutoTMalloc<uint8_t> storage; |
| 686 | size_t blob_size = 0u; |
| 687 | size_t storage_size = 0u; |
| 688 | |
| 689 | blob_size = SkAlign4(blob->serialize(procs)->size()); |
| 690 | REPORTER_ASSERT(reporter, blob_size > 4u); |
| 691 | storage_size = blob_size - 4; |
| 692 | storage.realloc(storage_size); |
| 693 | REPORTER_ASSERT(reporter, blob->serialize(procs, storage.get(), storage_size) == 0u); |
| 694 | storage_size = blob_size; |
| 695 | storage.realloc(storage_size); |
| 696 | REPORTER_ASSERT(reporter, blob->serialize(procs, storage.get(), storage_size) != 0u); |
| 697 | } |
| 698 | |
| 699 | DEF_TEST(WriteBuffer_external_memory_flattenable, reporter) { |
| 700 | SkScalar intervals[] = {1.f, 1.f}; |
| 701 | auto path_effect = SkDashPathEffect::Make(intervals, 2, 0); |
| 702 | size_t path_size = SkAlign4(path_effect->serialize()->size()); |
| 703 | REPORTER_ASSERT(reporter, path_size > 4u); |
| 704 | SkAutoTMalloc<uint8_t> storage; |
| 705 | |
| 706 | size_t storage_size = path_size - 4; |
| 707 | storage.realloc(storage_size); |
| 708 | REPORTER_ASSERT(reporter, path_effect->serialize(storage.get(), storage_size) == 0u); |
| 709 | |
| 710 | storage_size = path_size; |
| 711 | storage.realloc(storage_size); |
| 712 | REPORTER_ASSERT(reporter, path_effect->serialize(storage.get(), storage_size) != 0u); |
| 713 | } |