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" |
bungeman | f93d711 | 2016-09-16 06:24:20 -0700 | [diff] [blame] | 15 | #include "SkMakeUnique.h" |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 16 | #include "SkMallocPixelRef.h" |
Cary Clark | 6d6d603 | 2017-10-20 12:14:33 -0400 | [diff] [blame] | 17 | #include "SkMatrixPriv.h" |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 18 | #include "SkOSFile.h" |
Mike Reed | fadbfcd | 2017-12-06 16:09:20 -0500 | [diff] [blame] | 19 | #include "SkReadBuffer.h" |
robertphillips@google.com | 770963f | 2014-04-18 18:04:41 +0000 | [diff] [blame] | 20 | #include "SkPictureRecorder.h" |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 21 | #include "SkShaderBase.h" |
senorblanco | 91c395a | 2014-09-25 15:51:35 -0700 | [diff] [blame] | 22 | #include "SkTableColorFilter.h" |
commit-bot@chromium.org | 1e7ee99 | 2014-03-14 21:22:22 +0000 | [diff] [blame] | 23 | #include "SkTemplates.h" |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 24 | #include "SkTypeface.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 25 | #include "SkWriteBuffer.h" |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 26 | #include "SkXfermodeImageFilter.h" |
dvonbeck | 8811e40 | 2016-06-16 12:39:25 -0700 | [diff] [blame] | 27 | #include "sk_tool_utils.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 28 | #include "Test.h" |
commit-bot@chromium.org | 0251288 | 2013-10-31 18:37:50 +0000 | [diff] [blame] | 29 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 30 | static const uint32_t kArraySize = 64; |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 31 | static const int kBitmapSize = 256; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 32 | |
Cary Clark | 6d6d603 | 2017-10-20 12:14:33 -0400 | [diff] [blame] | 33 | class SerializationTest { |
| 34 | public: |
| 35 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 36 | template<typename T> |
| 37 | static void TestAlignment(T* testObj, skiatest::Reporter* reporter) { |
| 38 | // Test memory read/write functions directly |
| 39 | unsigned char dataWritten[1024]; |
| 40 | size_t bytesWrittenToMemory = testObj->writeToMemory(dataWritten); |
| 41 | REPORTER_ASSERT(reporter, SkAlign4(bytesWrittenToMemory) == bytesWrittenToMemory); |
| 42 | size_t bytesReadFromMemory = testObj->readFromMemory(dataWritten, bytesWrittenToMemory); |
| 43 | REPORTER_ASSERT(reporter, SkAlign4(bytesReadFromMemory) == bytesReadFromMemory); |
| 44 | } |
Cary Clark | 6d6d603 | 2017-10-20 12:14:33 -0400 | [diff] [blame] | 45 | }; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 46 | |
| 47 | template<typename T> struct SerializationUtils { |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 48 | // Generic case for flattenables |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 49 | static void Write(SkWriteBuffer& writer, const T* flattenable) { |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 50 | writer.writeFlattenable(flattenable); |
| 51 | } |
Mike Reed | fadbfcd | 2017-12-06 16:09:20 -0500 | [diff] [blame] | 52 | static void Read(SkReadBuffer& reader, T** flattenable) { |
mtklein | 3b37545 | 2016-04-04 14:57:19 -0700 | [diff] [blame] | 53 | *flattenable = (T*)reader.readFlattenable(T::GetFlattenableType()); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 54 | } |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | template<> struct SerializationUtils<SkMatrix> { |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 58 | static void Write(SkWriteBuffer& writer, const SkMatrix* matrix) { |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 59 | writer.writeMatrix(*matrix); |
| 60 | } |
Mike Reed | fadbfcd | 2017-12-06 16:09:20 -0500 | [diff] [blame] | 61 | static void Read(SkReadBuffer& reader, SkMatrix* matrix) { |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 62 | reader.readMatrix(matrix); |
| 63 | } |
| 64 | }; |
| 65 | |
| 66 | template<> struct SerializationUtils<SkPath> { |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 67 | static void Write(SkWriteBuffer& writer, const SkPath* path) { |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 68 | writer.writePath(*path); |
| 69 | } |
Mike Reed | fadbfcd | 2017-12-06 16:09:20 -0500 | [diff] [blame] | 70 | static void Read(SkReadBuffer& reader, SkPath* path) { |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 71 | reader.readPath(path); |
| 72 | } |
| 73 | }; |
| 74 | |
| 75 | template<> struct SerializationUtils<SkRegion> { |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 76 | static void Write(SkWriteBuffer& writer, const SkRegion* region) { |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 77 | writer.writeRegion(*region); |
| 78 | } |
Mike Reed | fadbfcd | 2017-12-06 16:09:20 -0500 | [diff] [blame] | 79 | static void Read(SkReadBuffer& reader, SkRegion* region) { |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 80 | reader.readRegion(region); |
| 81 | } |
| 82 | }; |
| 83 | |
commit-bot@chromium.org | 1ac99c8 | 2014-04-29 15:35:23 +0000 | [diff] [blame] | 84 | template<> struct SerializationUtils<SkString> { |
| 85 | static void Write(SkWriteBuffer& writer, const SkString* string) { |
| 86 | writer.writeString(string->c_str()); |
| 87 | } |
Mike Reed | fadbfcd | 2017-12-06 16:09:20 -0500 | [diff] [blame] | 88 | static void Read(SkReadBuffer& reader, SkString* string) { |
commit-bot@chromium.org | 1ac99c8 | 2014-04-29 15:35:23 +0000 | [diff] [blame] | 89 | reader.readString(string); |
| 90 | } |
| 91 | }; |
| 92 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 93 | template<> struct SerializationUtils<unsigned char> { |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 94 | static void Write(SkWriteBuffer& writer, unsigned char* data, uint32_t arraySize) { |
reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame] | 95 | writer.writeByteArray(data, arraySize); |
sugoi@google.com | 305f78e | 2013-11-04 16:18:15 +0000 | [diff] [blame] | 96 | } |
Mike Reed | fadbfcd | 2017-12-06 16:09:20 -0500 | [diff] [blame] | 97 | static bool Read(SkReadBuffer& reader, unsigned char* data, uint32_t arraySize) { |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 98 | return reader.readByteArray(data, arraySize); |
| 99 | } |
| 100 | }; |
commit-bot@chromium.org | 0251288 | 2013-10-31 18:37:50 +0000 | [diff] [blame] | 101 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 102 | template<> struct SerializationUtils<SkColor> { |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 103 | static void Write(SkWriteBuffer& writer, SkColor* data, uint32_t arraySize) { |
reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame] | 104 | writer.writeColorArray(data, arraySize); |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 105 | } |
Mike Reed | fadbfcd | 2017-12-06 16:09:20 -0500 | [diff] [blame] | 106 | static bool Read(SkReadBuffer& reader, SkColor* data, uint32_t arraySize) { |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 107 | return reader.readColorArray(data, arraySize); |
| 108 | } |
| 109 | }; |
sugoi@google.com | 305f78e | 2013-11-04 16:18:15 +0000 | [diff] [blame] | 110 | |
brianosman | 97bbf82 | 2016-09-25 13:15:58 -0700 | [diff] [blame] | 111 | template<> struct SerializationUtils<SkColor4f> { |
| 112 | static void Write(SkWriteBuffer& writer, SkColor4f* data, uint32_t arraySize) { |
| 113 | writer.writeColor4fArray(data, arraySize); |
| 114 | } |
Mike Reed | fadbfcd | 2017-12-06 16:09:20 -0500 | [diff] [blame] | 115 | static bool Read(SkReadBuffer& reader, SkColor4f* data, uint32_t arraySize) { |
brianosman | 97bbf82 | 2016-09-25 13:15:58 -0700 | [diff] [blame] | 116 | return reader.readColor4fArray(data, arraySize); |
| 117 | } |
| 118 | }; |
| 119 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 120 | template<> struct SerializationUtils<int32_t> { |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 121 | static void Write(SkWriteBuffer& writer, int32_t* data, uint32_t arraySize) { |
reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame] | 122 | writer.writeIntArray(data, arraySize); |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 123 | } |
Mike Reed | fadbfcd | 2017-12-06 16:09:20 -0500 | [diff] [blame] | 124 | static bool Read(SkReadBuffer& reader, int32_t* data, uint32_t arraySize) { |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 125 | return reader.readIntArray(data, arraySize); |
| 126 | } |
| 127 | }; |
sugoi@google.com | 305f78e | 2013-11-04 16:18:15 +0000 | [diff] [blame] | 128 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 129 | template<> struct SerializationUtils<SkPoint> { |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 130 | static void Write(SkWriteBuffer& writer, SkPoint* data, uint32_t arraySize) { |
reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame] | 131 | writer.writePointArray(data, arraySize); |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 132 | } |
Mike Reed | fadbfcd | 2017-12-06 16:09:20 -0500 | [diff] [blame] | 133 | static bool Read(SkReadBuffer& reader, SkPoint* data, uint32_t arraySize) { |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 134 | return reader.readPointArray(data, arraySize); |
| 135 | } |
| 136 | }; |
reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame] | 137 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 138 | template<> struct SerializationUtils<SkScalar> { |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 139 | static void Write(SkWriteBuffer& writer, SkScalar* data, uint32_t arraySize) { |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 140 | writer.writeScalarArray(data, arraySize); |
| 141 | } |
Mike Reed | fadbfcd | 2017-12-06 16:09:20 -0500 | [diff] [blame] | 142 | static bool Read(SkReadBuffer& reader, SkScalar* data, uint32_t arraySize) { |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 143 | return reader.readScalarArray(data, arraySize); |
| 144 | } |
| 145 | }; |
reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame] | 146 | |
commit-bot@chromium.org | 1ac99c8 | 2014-04-29 15:35:23 +0000 | [diff] [blame] | 147 | template<typename T, bool testInvalid> struct SerializationTestUtils { |
| 148 | static void InvalidateData(unsigned char* data) {} |
| 149 | }; |
| 150 | |
| 151 | template<> struct SerializationTestUtils<SkString, true> { |
| 152 | static void InvalidateData(unsigned char* data) { |
| 153 | data[3] |= 0x80; // Reverse sign of 1st integer |
| 154 | } |
| 155 | }; |
| 156 | |
| 157 | template<typename T, bool testInvalid> |
| 158 | static void TestObjectSerializationNoAlign(T* testObj, skiatest::Reporter* reporter) { |
brianosman | fad9856 | 2016-05-04 11:06:28 -0700 | [diff] [blame] | 159 | SkBinaryWriteBuffer writer; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 160 | SerializationUtils<T>::Write(writer, testObj); |
| 161 | size_t bytesWritten = writer.bytesWritten(); |
| 162 | REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten); |
reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame] | 163 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 164 | unsigned char dataWritten[1024]; |
| 165 | writer.writeToMemory(dataWritten); |
| 166 | |
commit-bot@chromium.org | 1ac99c8 | 2014-04-29 15:35:23 +0000 | [diff] [blame] | 167 | SerializationTestUtils<T, testInvalid>::InvalidateData(dataWritten); |
| 168 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 169 | // Make sure this fails when it should (test with smaller size, but still multiple of 4) |
Mike Reed | fadbfcd | 2017-12-06 16:09:20 -0500 | [diff] [blame] | 170 | SkReadBuffer buffer(dataWritten, bytesWritten - 4); |
commit-bot@chromium.org | 8f457e3 | 2013-11-08 19:22:57 +0000 | [diff] [blame] | 171 | T obj; |
| 172 | SerializationUtils<T>::Read(buffer, &obj); |
commit-bot@chromium.org | c2e9db3 | 2013-12-06 20:14:46 +0000 | [diff] [blame] | 173 | REPORTER_ASSERT(reporter, !buffer.isValid()); |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 174 | |
| 175 | // Make sure this succeeds when it should |
Mike Reed | fadbfcd | 2017-12-06 16:09:20 -0500 | [diff] [blame] | 176 | SkReadBuffer buffer2(dataWritten, bytesWritten); |
Robert Phillips | b252604 | 2016-09-26 09:00:36 -0400 | [diff] [blame] | 177 | size_t offsetBefore = buffer2.offset(); |
commit-bot@chromium.org | 8f457e3 | 2013-11-08 19:22:57 +0000 | [diff] [blame] | 178 | T obj2; |
| 179 | SerializationUtils<T>::Read(buffer2, &obj2); |
Robert Phillips | b252604 | 2016-09-26 09:00:36 -0400 | [diff] [blame] | 180 | size_t offsetAfter = buffer2.offset(); |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 181 | // 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] | 182 | REPORTER_ASSERT(reporter, buffer2.isValid() == !testInvalid); |
| 183 | // Note: This following test should always succeed, regardless of whether the buffer is valid, |
| 184 | // since if it is invalid, it will simply skip to the end, as if it had read the whole buffer. |
Robert Phillips | b252604 | 2016-09-26 09:00:36 -0400 | [diff] [blame] | 185 | REPORTER_ASSERT(reporter, offsetAfter - offsetBefore == bytesWritten); |
commit-bot@chromium.org | 1ac99c8 | 2014-04-29 15:35:23 +0000 | [diff] [blame] | 186 | } |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 187 | |
commit-bot@chromium.org | 1ac99c8 | 2014-04-29 15:35:23 +0000 | [diff] [blame] | 188 | template<typename T> |
| 189 | static void TestObjectSerialization(T* testObj, skiatest::Reporter* reporter) { |
| 190 | TestObjectSerializationNoAlign<T, false>(testObj, reporter); |
Cary Clark | 6d6d603 | 2017-10-20 12:14:33 -0400 | [diff] [blame] | 191 | SerializationTest::TestAlignment(testObj, reporter); |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | template<typename T> |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 195 | static T* TestFlattenableSerialization(T* testObj, bool shouldSucceed, |
| 196 | skiatest::Reporter* reporter) { |
brianosman | fad9856 | 2016-05-04 11:06:28 -0700 | [diff] [blame] | 197 | SkBinaryWriteBuffer writer; |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 198 | SerializationUtils<T>::Write(writer, testObj); |
| 199 | size_t bytesWritten = writer.bytesWritten(); |
| 200 | REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten); |
| 201 | |
dvonbeck | 8811e40 | 2016-06-16 12:39:25 -0700 | [diff] [blame] | 202 | SkASSERT(bytesWritten <= 4096); |
senorblanco | 91c395a | 2014-09-25 15:51:35 -0700 | [diff] [blame] | 203 | unsigned char dataWritten[4096]; |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 204 | writer.writeToMemory(dataWritten); |
| 205 | |
| 206 | // Make sure this fails when it should (test with smaller size, but still multiple of 4) |
Mike Reed | fadbfcd | 2017-12-06 16:09:20 -0500 | [diff] [blame] | 207 | SkReadBuffer buffer(dataWritten, bytesWritten - 4); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 208 | T* obj = nullptr; |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 209 | SerializationUtils<T>::Read(buffer, &obj); |
commit-bot@chromium.org | c2e9db3 | 2013-12-06 20:14:46 +0000 | [diff] [blame] | 210 | REPORTER_ASSERT(reporter, !buffer.isValid()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 211 | REPORTER_ASSERT(reporter, nullptr == obj); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 212 | |
| 213 | // Make sure this succeeds when it should |
Mike Reed | fadbfcd | 2017-12-06 16:09:20 -0500 | [diff] [blame] | 214 | SkReadBuffer buffer2(dataWritten, bytesWritten); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 215 | const unsigned char* peekBefore = static_cast<const unsigned char*>(buffer2.skip(0)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 216 | T* obj2 = nullptr; |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 217 | SerializationUtils<T>::Read(buffer2, &obj2); |
| 218 | const unsigned char* peekAfter = static_cast<const unsigned char*>(buffer2.skip(0)); |
| 219 | if (shouldSucceed) { |
| 220 | // 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] | 221 | REPORTER_ASSERT(reporter, buffer2.isValid()); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 222 | REPORTER_ASSERT(reporter, static_cast<size_t>(peekAfter - peekBefore) == bytesWritten); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 223 | REPORTER_ASSERT(reporter, obj2); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 224 | } else { |
| 225 | // 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] | 226 | REPORTER_ASSERT(reporter, !buffer.isValid()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 227 | REPORTER_ASSERT(reporter, nullptr == obj2); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | return obj2; // Return object to perform further validity tests on it |
| 231 | } |
| 232 | |
| 233 | template<typename T> |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 234 | static void TestArraySerialization(T* data, skiatest::Reporter* reporter) { |
brianosman | fad9856 | 2016-05-04 11:06:28 -0700 | [diff] [blame] | 235 | SkBinaryWriteBuffer writer; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 236 | SerializationUtils<T>::Write(writer, data, kArraySize); |
| 237 | size_t bytesWritten = writer.bytesWritten(); |
| 238 | // This should write the length (in 4 bytes) and the array |
| 239 | REPORTER_ASSERT(reporter, (4 + kArraySize * sizeof(T)) == bytesWritten); |
| 240 | |
brianosman | 97bbf82 | 2016-09-25 13:15:58 -0700 | [diff] [blame] | 241 | unsigned char dataWritten[2048]; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 242 | writer.writeToMemory(dataWritten); |
| 243 | |
| 244 | // Make sure this fails when it should |
Mike Reed | fadbfcd | 2017-12-06 16:09:20 -0500 | [diff] [blame] | 245 | SkReadBuffer buffer(dataWritten, bytesWritten); |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 246 | T dataRead[kArraySize]; |
| 247 | bool success = SerializationUtils<T>::Read(buffer, dataRead, kArraySize / 2); |
| 248 | // This should have failed, since the provided size was too small |
| 249 | REPORTER_ASSERT(reporter, !success); |
| 250 | |
| 251 | // Make sure this succeeds when it should |
Mike Reed | fadbfcd | 2017-12-06 16:09:20 -0500 | [diff] [blame] | 252 | SkReadBuffer buffer2(dataWritten, bytesWritten); |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 253 | success = SerializationUtils<T>::Read(buffer2, dataRead, kArraySize); |
| 254 | // This should have succeeded, since there are enough bytes to read this |
| 255 | REPORTER_ASSERT(reporter, success); |
| 256 | } |
| 257 | |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 258 | static void TestBitmapSerialization(const SkBitmap& validBitmap, |
| 259 | const SkBitmap& invalidBitmap, |
| 260 | bool shouldSucceed, |
| 261 | skiatest::Reporter* reporter) { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 262 | sk_sp<SkImage> validImage(SkImage::MakeFromBitmap(validBitmap)); |
robertphillips | 549c899 | 2016-04-01 09:28:51 -0700 | [diff] [blame] | 263 | sk_sp<SkImageFilter> validBitmapSource(SkImageSource::Make(std::move(validImage))); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 264 | sk_sp<SkImage> invalidImage(SkImage::MakeFromBitmap(invalidBitmap)); |
robertphillips | 549c899 | 2016-04-01 09:28:51 -0700 | [diff] [blame] | 265 | sk_sp<SkImageFilter> invalidBitmapSource(SkImageSource::Make(std::move(invalidImage))); |
reed | cfb6bdf | 2016-03-29 11:32:50 -0700 | [diff] [blame] | 266 | sk_sp<SkImageFilter> xfermodeImageFilter( |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 267 | SkXfermodeImageFilter::Make(SkBlendMode::kSrcOver, |
robertphillips | 8c0326d | 2016-04-05 12:48:34 -0700 | [diff] [blame] | 268 | std::move(invalidBitmapSource), |
| 269 | std::move(validBitmapSource), nullptr)); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 270 | |
Mike Reed | 5e25717 | 2016-11-01 11:22:05 -0400 | [diff] [blame] | 271 | sk_sp<SkImageFilter> deserializedFilter( |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 272 | TestFlattenableSerialization<SkImageFilter>( |
reed | cfb6bdf | 2016-03-29 11:32:50 -0700 | [diff] [blame] | 273 | xfermodeImageFilter.get(), shouldSucceed, reporter)); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 274 | |
| 275 | // Try to render a small bitmap using the invalid deserialized filter |
| 276 | // to make sure we don't crash while trying to render it |
| 277 | if (shouldSucceed) { |
| 278 | SkBitmap bitmap; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 279 | bitmap.allocN32Pixels(24, 24); |
| 280 | SkCanvas canvas(bitmap); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 281 | canvas.clear(0x00000000); |
| 282 | SkPaint paint; |
| 283 | paint.setImageFilter(deserializedFilter); |
| 284 | canvas.clipRect(SkRect::MakeXYWH(0, 0, SkIntToScalar(24), SkIntToScalar(24))); |
| 285 | canvas.drawBitmap(bitmap, 0, 0, &paint); |
| 286 | } |
| 287 | } |
| 288 | |
senorblanco | 91c395a | 2014-09-25 15:51:35 -0700 | [diff] [blame] | 289 | static void TestColorFilterSerialization(skiatest::Reporter* reporter) { |
| 290 | uint8_t table[256]; |
| 291 | for (int i = 0; i < 256; ++i) { |
| 292 | table[i] = (i * 41) % 256; |
| 293 | } |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 294 | auto colorFilter(SkTableColorFilter::Make(table)); |
Hal Canary | 342b7ac | 2016-11-04 11:49:42 -0400 | [diff] [blame] | 295 | sk_sp<SkColorFilter> copy( |
senorblanco | 91c395a | 2014-09-25 15:51:35 -0700 | [diff] [blame] | 296 | TestFlattenableSerialization<SkColorFilter>(colorFilter.get(), true, reporter)); |
| 297 | } |
| 298 | |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 299 | static SkBitmap draw_picture(SkPicture& picture) { |
| 300 | SkBitmap bitmap; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 301 | bitmap.allocN32Pixels(SkScalarCeilToInt(picture.cullRect().width()), |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 302 | SkScalarCeilToInt(picture.cullRect().height())); |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 303 | SkCanvas canvas(bitmap); |
robertphillips | c5ba71d | 2014-09-04 08:42:50 -0700 | [diff] [blame] | 304 | picture.playback(&canvas); |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 305 | return bitmap; |
| 306 | } |
| 307 | |
| 308 | static void compare_bitmaps(skiatest::Reporter* reporter, |
| 309 | const SkBitmap& b1, const SkBitmap& b2) { |
| 310 | REPORTER_ASSERT(reporter, b1.width() == b2.width()); |
| 311 | REPORTER_ASSERT(reporter, b1.height() == b2.height()); |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 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); |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 349 | std::unique_ptr<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. |
Mike Reed | 0933bc9 | 2017-12-09 01:27:41 +0000 | [diff] [blame] | 361 | auto data = GetResourceAsData("fonts/test.ttc"); |
| 362 | auto typeface = SkTypeface::MakeFromStream(new SkMemoryStream(std::move(data)), 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. |
Mike Reed | 0933bc9 | 2017-12-09 01:27:41 +0000 | [diff] [blame] | 372 | std::unique_ptr<SkStreamAsset> distortable(GetResourceAsStream("fonts/Distortable.ttf")); |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 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( |
bungeman | f93d711 | 2016-09-16 06:24:20 -0700 | [diff] [blame] | 378 | skstd::make_unique<SkFontData>(std::move(distortable), 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)); |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 432 | canvas->drawString("Picture", 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 | 91c395a | 2014-09-25 15:51:35 -0700 | [diff] [blame] | 454 | // Test color filter serialization |
| 455 | { |
| 456 | TestColorFilterSerialization(reporter); |
| 457 | } |
| 458 | |
commit-bot@chromium.org | 1ac99c8 | 2014-04-29 15:35:23 +0000 | [diff] [blame] | 459 | // Test string serialization |
| 460 | { |
| 461 | SkString string("string"); |
| 462 | TestObjectSerializationNoAlign<SkString, false>(&string, reporter); |
| 463 | TestObjectSerializationNoAlign<SkString, true>(&string, reporter); |
| 464 | } |
| 465 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 466 | // Test rrect serialization |
| 467 | { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 468 | // SkRRect does not initialize anything. |
| 469 | // An uninitialized SkRRect can be serialized, |
| 470 | // but will branch on uninitialized data when deserialized. |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 471 | SkRRect rrect; |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 472 | SkRect rect = SkRect::MakeXYWH(1, 2, 20, 30); |
| 473 | SkVector corners[4] = { {1, 2}, {2, 3}, {3,4}, {4,5} }; |
| 474 | rrect.setRectRadii(rect, corners); |
Cary Clark | 6d6d603 | 2017-10-20 12:14:33 -0400 | [diff] [blame] | 475 | SerializationTest::TestAlignment(&rrect, reporter); |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 476 | } |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 477 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 478 | // Test readByteArray |
| 479 | { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 480 | unsigned char data[kArraySize] = { 1, 2, 3 }; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 481 | TestArraySerialization(data, 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 readColorArray |
| 485 | { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 486 | SkColor data[kArraySize] = { SK_ColorBLACK, SK_ColorWHITE, SK_ColorRED }; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 487 | TestArraySerialization(data, reporter); |
| 488 | } |
| 489 | |
brianosman | 97bbf82 | 2016-09-25 13:15:58 -0700 | [diff] [blame] | 490 | // Test readColor4fArray |
| 491 | { |
| 492 | SkColor4f data[kArraySize] = { |
| 493 | SkColor4f::FromColor(SK_ColorBLACK), |
| 494 | SkColor4f::FromColor(SK_ColorWHITE), |
| 495 | SkColor4f::FromColor(SK_ColorRED), |
| 496 | { 1.f, 2.f, 4.f, 8.f } |
| 497 | }; |
| 498 | TestArraySerialization(data, reporter); |
| 499 | } |
| 500 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 501 | // Test readIntArray |
| 502 | { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 503 | int32_t data[kArraySize] = { 1, 2, 4, 8 }; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 504 | TestArraySerialization(data, reporter); |
| 505 | } |
| 506 | |
| 507 | // Test readPointArray |
| 508 | { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 509 | SkPoint data[kArraySize] = { {6, 7}, {42, 128} }; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 510 | TestArraySerialization(data, reporter); |
| 511 | } |
| 512 | |
| 513 | // Test readScalarArray |
| 514 | { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 515 | SkScalar data[kArraySize] = { SK_Scalar1, SK_ScalarHalf, SK_ScalarMax }; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 516 | TestArraySerialization(data, reporter); |
commit-bot@chromium.org | 0251288 | 2013-10-31 18:37:50 +0000 | [diff] [blame] | 517 | } |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 518 | |
| 519 | // Test invalid deserializations |
| 520 | { |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 521 | SkImageInfo info = SkImageInfo::MakeN32Premul(kBitmapSize, kBitmapSize); |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 522 | |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 523 | SkBitmap validBitmap; |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 524 | validBitmap.setInfo(info); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 525 | |
| 526 | // Create a bitmap with a really large height |
| 527 | SkBitmap invalidBitmap; |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 528 | invalidBitmap.setInfo(info.makeWH(info.width(), 1000000000)); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 529 | |
| 530 | // The deserialization should succeed, and the rendering shouldn't crash, |
| 531 | // even when the device fails to initialize, due to its size |
| 532 | TestBitmapSerialization(validBitmap, invalidBitmap, true, reporter); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 533 | } |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 534 | |
| 535 | // Test simple SkPicture serialization |
| 536 | { |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 537 | SkPictureRecorder recorder; |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 538 | draw_something(recorder.beginRecording(SkIntToScalar(kBitmapSize), |
| 539 | SkIntToScalar(kBitmapSize), |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 540 | nullptr, 0)); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 541 | sk_sp<SkPicture> pict(recorder.finishRecordingAsPicture()); |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 542 | |
| 543 | // Serialize picture |
brianosman | fad9856 | 2016-05-04 11:06:28 -0700 | [diff] [blame] | 544 | SkBinaryWriteBuffer writer; |
Mike Klein | 88d9071 | 2018-01-27 17:30:04 +0000 | [diff] [blame] | 545 | pict->flatten(writer); |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 546 | size_t size = writer.bytesWritten(); |
commit-bot@chromium.org | 1e7ee99 | 2014-03-14 21:22:22 +0000 | [diff] [blame] | 547 | SkAutoTMalloc<unsigned char> data(size); |
| 548 | writer.writeToMemory(static_cast<void*>(data.get())); |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 549 | |
| 550 | // Deserialize picture |
Mike Reed | fadbfcd | 2017-12-06 16:09:20 -0500 | [diff] [blame] | 551 | SkReadBuffer reader(static_cast<void*>(data.get()), size); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 552 | sk_sp<SkPicture> readPict(SkPicture::MakeFromBuffer(reader)); |
reed | d921dbb | 2016-09-30 09:27:20 -0700 | [diff] [blame] | 553 | REPORTER_ASSERT(reporter, reader.isValid()); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 554 | REPORTER_ASSERT(reporter, readPict.get()); |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 555 | } |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 556 | |
| 557 | TestPictureTypefaceSerialization(reporter); |
commit-bot@chromium.org | 0251288 | 2013-10-31 18:37:50 +0000 | [diff] [blame] | 558 | } |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 559 | |
| 560 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 561 | #include "SkAnnotation.h" |
| 562 | |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 563 | static sk_sp<SkPicture> copy_picture_via_serialization(SkPicture* src) { |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 564 | SkDynamicMemoryWStream wstream; |
| 565 | src->serialize(&wstream); |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 566 | std::unique_ptr<SkStreamAsset> rstream(wstream.detachAsStream()); |
| 567 | return SkPicture::MakeFromStream(rstream.get()); |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | struct AnnotationRec { |
| 571 | const SkRect fRect; |
| 572 | const char* fKey; |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 573 | sk_sp<SkData> fValue; |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 574 | }; |
| 575 | |
| 576 | class TestAnnotationCanvas : public SkCanvas { |
| 577 | skiatest::Reporter* fReporter; |
| 578 | const AnnotationRec* fRec; |
| 579 | int fCount; |
| 580 | int fCurrIndex; |
| 581 | |
| 582 | public: |
| 583 | TestAnnotationCanvas(skiatest::Reporter* reporter, const AnnotationRec rec[], int count) |
| 584 | : SkCanvas(100, 100) |
| 585 | , fReporter(reporter) |
| 586 | , fRec(rec) |
| 587 | , fCount(count) |
| 588 | , fCurrIndex(0) |
| 589 | {} |
| 590 | |
| 591 | ~TestAnnotationCanvas() { |
| 592 | REPORTER_ASSERT(fReporter, fCount == fCurrIndex); |
| 593 | } |
| 594 | |
| 595 | protected: |
| 596 | void onDrawAnnotation(const SkRect& rect, const char key[], SkData* value) { |
| 597 | REPORTER_ASSERT(fReporter, fCurrIndex < fCount); |
| 598 | REPORTER_ASSERT(fReporter, rect == fRec[fCurrIndex].fRect); |
| 599 | REPORTER_ASSERT(fReporter, !strcmp(key, fRec[fCurrIndex].fKey)); |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 600 | REPORTER_ASSERT(fReporter, value->equals(fRec[fCurrIndex].fValue.get())); |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 601 | fCurrIndex += 1; |
| 602 | } |
| 603 | }; |
| 604 | |
| 605 | /* |
| 606 | * Test the 3 annotation types by recording them into a picture, serializing, and then playing |
| 607 | * them back into another canvas. |
| 608 | */ |
| 609 | DEF_TEST(Annotations, reporter) { |
| 610 | SkPictureRecorder recorder; |
| 611 | SkCanvas* recordingCanvas = recorder.beginRecording(SkRect::MakeWH(100, 100)); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 612 | |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 613 | const char* str0 = "rect-with-url"; |
| 614 | const SkRect r0 = SkRect::MakeWH(10, 10); |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 615 | sk_sp<SkData> d0(SkData::MakeWithCString(str0)); |
| 616 | SkAnnotateRectWithURL(recordingCanvas, r0, d0.get()); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 617 | |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 618 | const char* str1 = "named-destination"; |
| 619 | const SkRect r1 = SkRect::MakeXYWH(5, 5, 0, 0); // collapsed to a point |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 620 | sk_sp<SkData> d1(SkData::MakeWithCString(str1)); |
| 621 | SkAnnotateNamedDestination(recordingCanvas, {r1.x(), r1.y()}, d1.get()); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 622 | |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 623 | const char* str2 = "link-to-destination"; |
| 624 | const SkRect r2 = SkRect::MakeXYWH(20, 20, 5, 6); |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 625 | sk_sp<SkData> d2(SkData::MakeWithCString(str2)); |
| 626 | SkAnnotateLinkToDestination(recordingCanvas, r2, d2.get()); |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 627 | |
| 628 | const AnnotationRec recs[] = { |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 629 | { r0, SkAnnotationKeys::URL_Key(), std::move(d0) }, |
| 630 | { r1, SkAnnotationKeys::Define_Named_Dest_Key(), std::move(d1) }, |
| 631 | { r2, SkAnnotationKeys::Link_Named_Dest_Key(), std::move(d2) }, |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 632 | }; |
| 633 | |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 634 | sk_sp<SkPicture> pict0(recorder.finishRecordingAsPicture()); |
| 635 | sk_sp<SkPicture> pict1(copy_picture_via_serialization(pict0.get())); |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 636 | |
| 637 | TestAnnotationCanvas canvas(reporter, recs, SK_ARRAY_COUNT(recs)); |
| 638 | canvas.drawPicture(pict1); |
| 639 | } |