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