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