| 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 "SkAnnotationKeys.h" | 8 #include "SkAnnotationKeys.h" |
| 9 #include "Resources.h" | 9 #include "Resources.h" |
| 10 #include "SkCanvas.h" | 10 #include "SkCanvas.h" |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 }; | 132 }; |
| 133 | 133 |
| 134 template<> struct SerializationTestUtils<SkString, true> { | 134 template<> struct SerializationTestUtils<SkString, true> { |
| 135 static void InvalidateData(unsigned char* data) { | 135 static void InvalidateData(unsigned char* data) { |
| 136 data[3] |= 0x80; // Reverse sign of 1st integer | 136 data[3] |= 0x80; // Reverse sign of 1st integer |
| 137 } | 137 } |
| 138 }; | 138 }; |
| 139 | 139 |
| 140 template<typename T, bool testInvalid> | 140 template<typename T, bool testInvalid> |
| 141 static void TestObjectSerializationNoAlign(T* testObj, skiatest::Reporter* repor
ter) { | 141 static void TestObjectSerializationNoAlign(T* testObj, skiatest::Reporter* repor
ter) { |
| 142 SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag); | 142 SkWriteBuffer writer; |
| 143 SerializationUtils<T>::Write(writer, testObj); | 143 SerializationUtils<T>::Write(writer, testObj); |
| 144 size_t bytesWritten = writer.bytesWritten(); | 144 size_t bytesWritten = writer.bytesWritten(); |
| 145 REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten); | 145 REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten); |
| 146 | 146 |
| 147 unsigned char dataWritten[1024]; | 147 unsigned char dataWritten[1024]; |
| 148 writer.writeToMemory(dataWritten); | 148 writer.writeToMemory(dataWritten); |
| 149 | 149 |
| 150 SerializationTestUtils<T, testInvalid>::InvalidateData(dataWritten); | 150 SerializationTestUtils<T, testInvalid>::InvalidateData(dataWritten); |
| 151 | 151 |
| 152 // Make sure this fails when it should (test with smaller size, but still mu
ltiple of 4) | 152 // Make sure this fails when it should (test with smaller size, but still mu
ltiple of 4) |
| (...skipping 17 matching lines...) Expand all Loading... |
| 170 | 170 |
| 171 template<typename T> | 171 template<typename T> |
| 172 static void TestObjectSerialization(T* testObj, skiatest::Reporter* reporter) { | 172 static void TestObjectSerialization(T* testObj, skiatest::Reporter* reporter) { |
| 173 TestObjectSerializationNoAlign<T, false>(testObj, reporter); | 173 TestObjectSerializationNoAlign<T, false>(testObj, reporter); |
| 174 TestAlignment(testObj, reporter); | 174 TestAlignment(testObj, reporter); |
| 175 } | 175 } |
| 176 | 176 |
| 177 template<typename T> | 177 template<typename T> |
| 178 static T* TestFlattenableSerialization(T* testObj, bool shouldSucceed, | 178 static T* TestFlattenableSerialization(T* testObj, bool shouldSucceed, |
| 179 skiatest::Reporter* reporter) { | 179 skiatest::Reporter* reporter) { |
| 180 SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag); | 180 SkWriteBuffer writer; |
| 181 SerializationUtils<T>::Write(writer, testObj); | 181 SerializationUtils<T>::Write(writer, testObj); |
| 182 size_t bytesWritten = writer.bytesWritten(); | 182 size_t bytesWritten = writer.bytesWritten(); |
| 183 REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten); | 183 REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten); |
| 184 | 184 |
| 185 unsigned char dataWritten[4096]; | 185 unsigned char dataWritten[4096]; |
| 186 SkASSERT(bytesWritten <= sizeof(dataWritten)); | 186 SkASSERT(bytesWritten <= sizeof(dataWritten)); |
| 187 writer.writeToMemory(dataWritten); | 187 writer.writeToMemory(dataWritten); |
| 188 | 188 |
| 189 // Make sure this fails when it should (test with smaller size, but still mu
ltiple of 4) | 189 // Make sure this fails when it should (test with smaller size, but still mu
ltiple of 4) |
| 190 SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 4); | 190 SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 4); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 208 // If the deserialization was supposed to fail, make sure it did | 208 // If the deserialization was supposed to fail, make sure it did |
| 209 REPORTER_ASSERT(reporter, !buffer.isValid()); | 209 REPORTER_ASSERT(reporter, !buffer.isValid()); |
| 210 REPORTER_ASSERT(reporter, nullptr == obj2); | 210 REPORTER_ASSERT(reporter, nullptr == obj2); |
| 211 } | 211 } |
| 212 | 212 |
| 213 return obj2; // Return object to perform further validity tests on it | 213 return obj2; // Return object to perform further validity tests on it |
| 214 } | 214 } |
| 215 | 215 |
| 216 template<typename T> | 216 template<typename T> |
| 217 static void TestArraySerialization(T* data, skiatest::Reporter* reporter) { | 217 static void TestArraySerialization(T* data, skiatest::Reporter* reporter) { |
| 218 SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag); | 218 SkWriteBuffer writer; |
| 219 SerializationUtils<T>::Write(writer, data, kArraySize); | 219 SerializationUtils<T>::Write(writer, data, kArraySize); |
| 220 size_t bytesWritten = writer.bytesWritten(); | 220 size_t bytesWritten = writer.bytesWritten(); |
| 221 // This should write the length (in 4 bytes) and the array | 221 // This should write the length (in 4 bytes) and the array |
| 222 REPORTER_ASSERT(reporter, (4 + kArraySize * sizeof(T)) == bytesWritten); | 222 REPORTER_ASSERT(reporter, (4 + kArraySize * sizeof(T)) == bytesWritten); |
| 223 | 223 |
| 224 unsigned char dataWritten[1024]; | 224 unsigned char dataWritten[1024]; |
| 225 writer.writeToMemory(dataWritten); | 225 writer.writeToMemory(dataWritten); |
| 226 | 226 |
| 227 // Make sure this fails when it should | 227 // Make sure this fails when it should |
| 228 SkValidatingReadBuffer buffer(dataWritten, bytesWritten); | 228 SkValidatingReadBuffer buffer(dataWritten, bytesWritten); |
| (...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 526 | 526 |
| 527 // Test simple SkPicture serialization | 527 // Test simple SkPicture serialization |
| 528 { | 528 { |
| 529 SkPictureRecorder recorder; | 529 SkPictureRecorder recorder; |
| 530 draw_something(recorder.beginRecording(SkIntToScalar(kBitmapSize), | 530 draw_something(recorder.beginRecording(SkIntToScalar(kBitmapSize), |
| 531 SkIntToScalar(kBitmapSize), | 531 SkIntToScalar(kBitmapSize), |
| 532 nullptr, 0)); | 532 nullptr, 0)); |
| 533 sk_sp<SkPicture> pict(recorder.finishRecordingAsPicture()); | 533 sk_sp<SkPicture> pict(recorder.finishRecordingAsPicture()); |
| 534 | 534 |
| 535 // Serialize picture | 535 // Serialize picture |
| 536 SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag); | 536 SkWriteBuffer writer; |
| 537 pict->flatten(writer); | 537 pict->flatten(writer); |
| 538 size_t size = writer.bytesWritten(); | 538 size_t size = writer.bytesWritten(); |
| 539 SkAutoTMalloc<unsigned char> data(size); | 539 SkAutoTMalloc<unsigned char> data(size); |
| 540 writer.writeToMemory(static_cast<void*>(data.get())); | 540 writer.writeToMemory(static_cast<void*>(data.get())); |
| 541 | 541 |
| 542 // Deserialize picture | 542 // Deserialize picture |
| 543 SkValidatingReadBuffer reader(static_cast<void*>(data.get()), size); | 543 SkValidatingReadBuffer reader(static_cast<void*>(data.get()), size); |
| 544 sk_sp<SkPicture> readPict(SkPicture::MakeFromBuffer(reader)); | 544 sk_sp<SkPicture> readPict(SkPicture::MakeFromBuffer(reader)); |
| 545 REPORTER_ASSERT(reporter, readPict.get()); | 545 REPORTER_ASSERT(reporter, readPict.get()); |
| 546 } | 546 } |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 621 { r1, SkAnnotationKeys::Define_Named_Dest_Key(), d1 }, | 621 { r1, SkAnnotationKeys::Define_Named_Dest_Key(), d1 }, |
| 622 { r2, SkAnnotationKeys::Link_Named_Dest_Key(), d2 }, | 622 { r2, SkAnnotationKeys::Link_Named_Dest_Key(), d2 }, |
| 623 }; | 623 }; |
| 624 | 624 |
| 625 sk_sp<SkPicture> pict0(recorder.finishRecordingAsPicture()); | 625 sk_sp<SkPicture> pict0(recorder.finishRecordingAsPicture()); |
| 626 sk_sp<SkPicture> pict1(copy_picture_via_serialization(pict0.get())); | 626 sk_sp<SkPicture> pict1(copy_picture_via_serialization(pict0.get())); |
| 627 | 627 |
| 628 TestAnnotationCanvas canvas(reporter, recs, SK_ARRAY_COUNT(recs)); | 628 TestAnnotationCanvas canvas(reporter, recs, SK_ARRAY_COUNT(recs)); |
| 629 canvas.drawPicture(pict1); | 629 canvas.drawPicture(pict1); |
| 630 } | 630 } |
| OLD | NEW |