OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkBitmapDevice.h" | 8 #include "SkBitmapDevice.h" |
9 #include "SkBitmapSource.h" | 9 #include "SkBitmapSource.h" |
10 #include "SkCanvas.h" | 10 #include "SkCanvas.h" |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 | 59 |
60 template<> struct SerializationUtils<SkRegion> { | 60 template<> struct SerializationUtils<SkRegion> { |
61 static void Write(SkWriteBuffer& writer, const SkRegion* region) { | 61 static void Write(SkWriteBuffer& writer, const SkRegion* region) { |
62 writer.writeRegion(*region); | 62 writer.writeRegion(*region); |
63 } | 63 } |
64 static void Read(SkValidatingReadBuffer& reader, SkRegion* region) { | 64 static void Read(SkValidatingReadBuffer& reader, SkRegion* region) { |
65 reader.readRegion(region); | 65 reader.readRegion(region); |
66 } | 66 } |
67 }; | 67 }; |
68 | 68 |
| 69 template<> struct SerializationUtils<SkString> { |
| 70 static void Write(SkWriteBuffer& writer, const SkString* string) { |
| 71 writer.writeString(string->c_str()); |
| 72 } |
| 73 static void Read(SkValidatingReadBuffer& reader, SkString* string) { |
| 74 reader.readString(string); |
| 75 } |
| 76 }; |
| 77 |
69 template<> struct SerializationUtils<unsigned char> { | 78 template<> struct SerializationUtils<unsigned char> { |
70 static void Write(SkWriteBuffer& writer, unsigned char* data, uint32_t array
Size) { | 79 static void Write(SkWriteBuffer& writer, unsigned char* data, uint32_t array
Size) { |
71 writer.writeByteArray(data, arraySize); | 80 writer.writeByteArray(data, arraySize); |
72 } | 81 } |
73 static bool Read(SkValidatingReadBuffer& reader, unsigned char* data, uint32
_t arraySize) { | 82 static bool Read(SkValidatingReadBuffer& reader, unsigned char* data, uint32
_t arraySize) { |
74 return reader.readByteArray(data, arraySize); | 83 return reader.readByteArray(data, arraySize); |
75 } | 84 } |
76 }; | 85 }; |
77 | 86 |
78 template<> struct SerializationUtils<SkColor> { | 87 template<> struct SerializationUtils<SkColor> { |
(...skipping 25 matching lines...) Expand all Loading... |
104 | 113 |
105 template<> struct SerializationUtils<SkScalar> { | 114 template<> struct SerializationUtils<SkScalar> { |
106 static void Write(SkWriteBuffer& writer, SkScalar* data, uint32_t arraySize)
{ | 115 static void Write(SkWriteBuffer& writer, SkScalar* data, uint32_t arraySize)
{ |
107 writer.writeScalarArray(data, arraySize); | 116 writer.writeScalarArray(data, arraySize); |
108 } | 117 } |
109 static bool Read(SkValidatingReadBuffer& reader, SkScalar* data, uint32_t ar
raySize) { | 118 static bool Read(SkValidatingReadBuffer& reader, SkScalar* data, uint32_t ar
raySize) { |
110 return reader.readScalarArray(data, arraySize); | 119 return reader.readScalarArray(data, arraySize); |
111 } | 120 } |
112 }; | 121 }; |
113 | 122 |
114 template<typename T> | 123 template<typename T, bool testInvalid> struct SerializationTestUtils { |
115 static void TestObjectSerialization(T* testObj, skiatest::Reporter* reporter) { | 124 static void InvalidateData(unsigned char* data) {} |
| 125 }; |
| 126 |
| 127 template<> struct SerializationTestUtils<SkString, true> { |
| 128 static void InvalidateData(unsigned char* data) { |
| 129 data[3] |= 0x80; // Reverse sign of 1st integer |
| 130 } |
| 131 }; |
| 132 |
| 133 template<typename T, bool testInvalid> |
| 134 static void TestObjectSerializationNoAlign(T* testObj, skiatest::Reporter* repor
ter) { |
116 SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag); | 135 SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag); |
117 SerializationUtils<T>::Write(writer, testObj); | 136 SerializationUtils<T>::Write(writer, testObj); |
118 size_t bytesWritten = writer.bytesWritten(); | 137 size_t bytesWritten = writer.bytesWritten(); |
119 REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten); | 138 REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten); |
120 | 139 |
121 unsigned char dataWritten[1024]; | 140 unsigned char dataWritten[1024]; |
122 writer.writeToMemory(dataWritten); | 141 writer.writeToMemory(dataWritten); |
123 | 142 |
| 143 SerializationTestUtils<T, testInvalid>::InvalidateData(dataWritten); |
| 144 |
124 // Make sure this fails when it should (test with smaller size, but still mu
ltiple of 4) | 145 // Make sure this fails when it should (test with smaller size, but still mu
ltiple of 4) |
125 SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 4); | 146 SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 4); |
126 T obj; | 147 T obj; |
127 SerializationUtils<T>::Read(buffer, &obj); | 148 SerializationUtils<T>::Read(buffer, &obj); |
128 REPORTER_ASSERT(reporter, !buffer.isValid()); | 149 REPORTER_ASSERT(reporter, !buffer.isValid()); |
129 | 150 |
130 // Make sure this succeeds when it should | 151 // Make sure this succeeds when it should |
131 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten); | 152 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten); |
132 const unsigned char* peekBefore = static_cast<const unsigned char*>(buffer2.
skip(0)); | 153 const unsigned char* peekBefore = static_cast<const unsigned char*>(buffer2.
skip(0)); |
133 T obj2; | 154 T obj2; |
134 SerializationUtils<T>::Read(buffer2, &obj2); | 155 SerializationUtils<T>::Read(buffer2, &obj2); |
135 const unsigned char* peekAfter = static_cast<const unsigned char*>(buffer2.s
kip(0)); | 156 const unsigned char* peekAfter = static_cast<const unsigned char*>(buffer2.s
kip(0)); |
136 // This should have succeeded, since there are enough bytes to read this | 157 // This should have succeeded, since there are enough bytes to read this |
137 REPORTER_ASSERT(reporter, buffer2.isValid()); | 158 REPORTER_ASSERT(reporter, buffer2.isValid() == !testInvalid); |
| 159 // Note: This following test should always succeed, regardless of whether th
e buffer is valid, |
| 160 // since if it is invalid, it will simply skip to the end, as if it had read
the whole buffer. |
138 REPORTER_ASSERT(reporter, static_cast<size_t>(peekAfter - peekBefore) == byt
esWritten); | 161 REPORTER_ASSERT(reporter, static_cast<size_t>(peekAfter - peekBefore) == byt
esWritten); |
| 162 } |
139 | 163 |
| 164 template<typename T> |
| 165 static void TestObjectSerialization(T* testObj, skiatest::Reporter* reporter) { |
| 166 TestObjectSerializationNoAlign<T, false>(testObj, reporter); |
140 TestAlignment(testObj, reporter); | 167 TestAlignment(testObj, reporter); |
141 } | 168 } |
142 | 169 |
143 template<typename T> | 170 template<typename T> |
144 static T* TestFlattenableSerialization(T* testObj, bool shouldSucceed, | 171 static T* TestFlattenableSerialization(T* testObj, bool shouldSucceed, |
145 skiatest::Reporter* reporter) { | 172 skiatest::Reporter* reporter) { |
146 SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag); | 173 SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag); |
147 SerializationUtils<T>::Write(writer, testObj); | 174 SerializationUtils<T>::Write(writer, testObj); |
148 size_t bytesWritten = writer.bytesWritten(); | 175 size_t bytesWritten = writer.bytesWritten(); |
149 REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten); | 176 REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten); |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
302 SkPath path; | 329 SkPath path; |
303 TestObjectSerialization(&path, reporter); | 330 TestObjectSerialization(&path, reporter); |
304 } | 331 } |
305 | 332 |
306 // Test region serialization | 333 // Test region serialization |
307 { | 334 { |
308 SkRegion region; | 335 SkRegion region; |
309 TestObjectSerialization(®ion, reporter); | 336 TestObjectSerialization(®ion, reporter); |
310 } | 337 } |
311 | 338 |
| 339 // Test string serialization |
| 340 { |
| 341 SkString string("string"); |
| 342 TestObjectSerializationNoAlign<SkString, false>(&string, reporter); |
| 343 TestObjectSerializationNoAlign<SkString, true>(&string, reporter); |
| 344 } |
| 345 |
312 // Test rrect serialization | 346 // Test rrect serialization |
313 { | 347 { |
314 // SkRRect does not initialize anything. | 348 // SkRRect does not initialize anything. |
315 // An uninitialized SkRRect can be serialized, | 349 // An uninitialized SkRRect can be serialized, |
316 // but will branch on uninitialized data when deserialized. | 350 // but will branch on uninitialized data when deserialized. |
317 SkRRect rrect; | 351 SkRRect rrect; |
318 SkRect rect = SkRect::MakeXYWH(1, 2, 20, 30); | 352 SkRect rect = SkRect::MakeXYWH(1, 2, 20, 30); |
319 SkVector corners[4] = { {1, 2}, {2, 3}, {3,4}, {4,5} }; | 353 SkVector corners[4] = { {1, 2}, {2, 3}, {3,4}, {4,5} }; |
320 rrect.setRectRadii(rect, corners); | 354 rrect.setRectRadii(rect, corners); |
321 TestAlignment(&rrect, reporter); | 355 TestAlignment(&rrect, reporter); |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
382 SkAutoTMalloc<unsigned char> data(size); | 416 SkAutoTMalloc<unsigned char> data(size); |
383 writer.writeToMemory(static_cast<void*>(data.get())); | 417 writer.writeToMemory(static_cast<void*>(data.get())); |
384 | 418 |
385 // Deserialize picture | 419 // Deserialize picture |
386 SkValidatingReadBuffer reader(static_cast<void*>(data.get()), size); | 420 SkValidatingReadBuffer reader(static_cast<void*>(data.get()), size); |
387 SkAutoTUnref<SkPicture> readPict( | 421 SkAutoTUnref<SkPicture> readPict( |
388 SkPicture::CreateFromBuffer(reader)); | 422 SkPicture::CreateFromBuffer(reader)); |
389 REPORTER_ASSERT(reporter, NULL != readPict.get()); | 423 REPORTER_ASSERT(reporter, NULL != readPict.get()); |
390 } | 424 } |
391 } | 425 } |
OLD | NEW |