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