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