| 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" |
| 11 #include "SkMallocPixelRef.h" | 11 #include "SkMallocPixelRef.h" |
| 12 #include "SkOrderedWriteBuffer.h" | 12 #include "SkWriteBuffer.h" |
| 13 #include "SkValidatingReadBuffer.h" | 13 #include "SkValidatingReadBuffer.h" |
| 14 #include "SkXfermodeImageFilter.h" | 14 #include "SkXfermodeImageFilter.h" |
| 15 #include "Test.h" | 15 #include "Test.h" |
| 16 | 16 |
| 17 static const uint32_t kArraySize = 64; | 17 static const uint32_t kArraySize = 64; |
| 18 | 18 |
| 19 template<typename T> | 19 template<typename T> |
| 20 static void TestAlignment(T* testObj, skiatest::Reporter* reporter) { | 20 static void TestAlignment(T* testObj, skiatest::Reporter* reporter) { |
| 21 // Test memory read/write functions directly | 21 // Test memory read/write functions directly |
| 22 unsigned char dataWritten[1024]; | 22 unsigned char dataWritten[1024]; |
| 23 size_t bytesWrittenToMemory = testObj->writeToMemory(dataWritten); | 23 size_t bytesWrittenToMemory = testObj->writeToMemory(dataWritten); |
| 24 REPORTER_ASSERT(reporter, SkAlign4(bytesWrittenToMemory) == bytesWrittenToMe
mory); | 24 REPORTER_ASSERT(reporter, SkAlign4(bytesWrittenToMemory) == bytesWrittenToMe
mory); |
| 25 size_t bytesReadFromMemory = testObj->readFromMemory(dataWritten, bytesWritt
enToMemory); | 25 size_t bytesReadFromMemory = testObj->readFromMemory(dataWritten, bytesWritt
enToMemory); |
| 26 REPORTER_ASSERT(reporter, SkAlign4(bytesReadFromMemory) == bytesReadFromMemo
ry); | 26 REPORTER_ASSERT(reporter, SkAlign4(bytesReadFromMemory) == bytesReadFromMemo
ry); |
| 27 } | 27 } |
| 28 | 28 |
| 29 template<typename T> struct SerializationUtils { | 29 template<typename T> struct SerializationUtils { |
| 30 // Generic case for flattenables | 30 // Generic case for flattenables |
| 31 static void Write(SkOrderedWriteBuffer& writer, const T* flattenable) { | 31 static void Write(SkWriteBuffer& writer, const T* flattenable) { |
| 32 writer.writeFlattenable(flattenable); | 32 writer.writeFlattenable(flattenable); |
| 33 } | 33 } |
| 34 static void Read(SkValidatingReadBuffer& reader, T** flattenable) { | 34 static void Read(SkValidatingReadBuffer& reader, T** flattenable) { |
| 35 *flattenable = (T*)reader.readFlattenable(T::GetFlattenableType()); | 35 *flattenable = (T*)reader.readFlattenable(T::GetFlattenableType()); |
| 36 } | 36 } |
| 37 }; | 37 }; |
| 38 | 38 |
| 39 template<> struct SerializationUtils<SkMatrix> { | 39 template<> struct SerializationUtils<SkMatrix> { |
| 40 static void Write(SkOrderedWriteBuffer& writer, const SkMatrix* matrix) { | 40 static void Write(SkWriteBuffer& writer, const SkMatrix* matrix) { |
| 41 writer.writeMatrix(*matrix); | 41 writer.writeMatrix(*matrix); |
| 42 } | 42 } |
| 43 static void Read(SkValidatingReadBuffer& reader, SkMatrix* matrix) { | 43 static void Read(SkValidatingReadBuffer& reader, SkMatrix* matrix) { |
| 44 reader.readMatrix(matrix); | 44 reader.readMatrix(matrix); |
| 45 } | 45 } |
| 46 }; | 46 }; |
| 47 | 47 |
| 48 template<> struct SerializationUtils<SkPath> { | 48 template<> struct SerializationUtils<SkPath> { |
| 49 static void Write(SkOrderedWriteBuffer& writer, const SkPath* path) { | 49 static void Write(SkWriteBuffer& writer, const SkPath* path) { |
| 50 writer.writePath(*path); | 50 writer.writePath(*path); |
| 51 } | 51 } |
| 52 static void Read(SkValidatingReadBuffer& reader, SkPath* path) { | 52 static void Read(SkValidatingReadBuffer& reader, SkPath* path) { |
| 53 reader.readPath(path); | 53 reader.readPath(path); |
| 54 } | 54 } |
| 55 }; | 55 }; |
| 56 | 56 |
| 57 template<> struct SerializationUtils<SkRegion> { | 57 template<> struct SerializationUtils<SkRegion> { |
| 58 static void Write(SkOrderedWriteBuffer& writer, const SkRegion* region) { | 58 static void Write(SkWriteBuffer& writer, const SkRegion* region) { |
| 59 writer.writeRegion(*region); | 59 writer.writeRegion(*region); |
| 60 } | 60 } |
| 61 static void Read(SkValidatingReadBuffer& reader, SkRegion* region) { | 61 static void Read(SkValidatingReadBuffer& reader, SkRegion* region) { |
| 62 reader.readRegion(region); | 62 reader.readRegion(region); |
| 63 } | 63 } |
| 64 }; | 64 }; |
| 65 | 65 |
| 66 template<> struct SerializationUtils<unsigned char> { | 66 template<> struct SerializationUtils<unsigned char> { |
| 67 static void Write(SkOrderedWriteBuffer& writer, unsigned char* data, uint32_
t arraySize) { | 67 static void Write(SkWriteBuffer& writer, unsigned char* data, uint32_t array
Size) { |
| 68 writer.writeByteArray(data, arraySize); | 68 writer.writeByteArray(data, arraySize); |
| 69 } | 69 } |
| 70 static bool Read(SkValidatingReadBuffer& reader, unsigned char* data, uint32
_t arraySize) { | 70 static bool Read(SkValidatingReadBuffer& reader, unsigned char* data, uint32
_t arraySize) { |
| 71 return reader.readByteArray(data, arraySize); | 71 return reader.readByteArray(data, arraySize); |
| 72 } | 72 } |
| 73 }; | 73 }; |
| 74 | 74 |
| 75 template<> struct SerializationUtils<SkColor> { | 75 template<> struct SerializationUtils<SkColor> { |
| 76 static void Write(SkOrderedWriteBuffer& writer, SkColor* data, uint32_t arra
ySize) { | 76 static void Write(SkWriteBuffer& writer, SkColor* data, uint32_t arraySize)
{ |
| 77 writer.writeColorArray(data, arraySize); | 77 writer.writeColorArray(data, arraySize); |
| 78 } | 78 } |
| 79 static bool Read(SkValidatingReadBuffer& reader, SkColor* data, uint32_t arr
aySize) { | 79 static bool Read(SkValidatingReadBuffer& reader, SkColor* data, uint32_t arr
aySize) { |
| 80 return reader.readColorArray(data, arraySize); | 80 return reader.readColorArray(data, arraySize); |
| 81 } | 81 } |
| 82 }; | 82 }; |
| 83 | 83 |
| 84 template<> struct SerializationUtils<int32_t> { | 84 template<> struct SerializationUtils<int32_t> { |
| 85 static void Write(SkOrderedWriteBuffer& writer, int32_t* data, uint32_t arra
ySize) { | 85 static void Write(SkWriteBuffer& writer, int32_t* data, uint32_t arraySize)
{ |
| 86 writer.writeIntArray(data, arraySize); | 86 writer.writeIntArray(data, arraySize); |
| 87 } | 87 } |
| 88 static bool Read(SkValidatingReadBuffer& reader, int32_t* data, uint32_t arr
aySize) { | 88 static bool Read(SkValidatingReadBuffer& reader, int32_t* data, uint32_t arr
aySize) { |
| 89 return reader.readIntArray(data, arraySize); | 89 return reader.readIntArray(data, arraySize); |
| 90 } | 90 } |
| 91 }; | 91 }; |
| 92 | 92 |
| 93 template<> struct SerializationUtils<SkPoint> { | 93 template<> struct SerializationUtils<SkPoint> { |
| 94 static void Write(SkOrderedWriteBuffer& writer, SkPoint* data, uint32_t arra
ySize) { | 94 static void Write(SkWriteBuffer& writer, SkPoint* data, uint32_t arraySize)
{ |
| 95 writer.writePointArray(data, arraySize); | 95 writer.writePointArray(data, arraySize); |
| 96 } | 96 } |
| 97 static bool Read(SkValidatingReadBuffer& reader, SkPoint* data, uint32_t arr
aySize) { | 97 static bool Read(SkValidatingReadBuffer& reader, SkPoint* data, uint32_t arr
aySize) { |
| 98 return reader.readPointArray(data, arraySize); | 98 return reader.readPointArray(data, arraySize); |
| 99 } | 99 } |
| 100 }; | 100 }; |
| 101 | 101 |
| 102 template<> struct SerializationUtils<SkScalar> { | 102 template<> struct SerializationUtils<SkScalar> { |
| 103 static void Write(SkOrderedWriteBuffer& writer, SkScalar* data, uint32_t arr
aySize) { | 103 static void Write(SkWriteBuffer& writer, SkScalar* data, uint32_t arraySize)
{ |
| 104 writer.writeScalarArray(data, arraySize); | 104 writer.writeScalarArray(data, arraySize); |
| 105 } | 105 } |
| 106 static bool Read(SkValidatingReadBuffer& reader, SkScalar* data, uint32_t ar
raySize) { | 106 static bool Read(SkValidatingReadBuffer& reader, SkScalar* data, uint32_t ar
raySize) { |
| 107 return reader.readScalarArray(data, arraySize); | 107 return reader.readScalarArray(data, arraySize); |
| 108 } | 108 } |
| 109 }; | 109 }; |
| 110 | 110 |
| 111 template<typename T> | 111 template<typename T> |
| 112 static void TestObjectSerialization(T* testObj, skiatest::Reporter* reporter) { | 112 static void TestObjectSerialization(T* testObj, skiatest::Reporter* reporter) { |
| 113 SkOrderedWriteBuffer writer; | 113 SkWriteBuffer writer; |
| 114 writer.setFlags(SkOrderedWriteBuffer::kValidation_Flag); | 114 writer.setFlags(SkWriteBuffer::kValidation_Flag); |
| 115 SerializationUtils<T>::Write(writer, testObj); | 115 SerializationUtils<T>::Write(writer, testObj); |
| 116 size_t bytesWritten = writer.bytesWritten(); | 116 size_t bytesWritten = writer.bytesWritten(); |
| 117 REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten); | 117 REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten); |
| 118 | 118 |
| 119 unsigned char dataWritten[1024]; | 119 unsigned char dataWritten[1024]; |
| 120 writer.writeToMemory(dataWritten); | 120 writer.writeToMemory(dataWritten); |
| 121 | 121 |
| 122 // Make sure this fails when it should (test with smaller size, but still mu
ltiple of 4) | 122 // Make sure this fails when it should (test with smaller size, but still mu
ltiple of 4) |
| 123 SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 4); | 123 SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 4); |
| 124 T obj; | 124 T obj; |
| 125 SerializationUtils<T>::Read(buffer, &obj); | 125 SerializationUtils<T>::Read(buffer, &obj); |
| 126 REPORTER_ASSERT(reporter, !buffer.isValid()); | 126 REPORTER_ASSERT(reporter, !buffer.isValid()); |
| 127 | 127 |
| 128 // Make sure this succeeds when it should | 128 // Make sure this succeeds when it should |
| 129 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten); | 129 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten); |
| 130 const unsigned char* peekBefore = static_cast<const unsigned char*>(buffer2.
skip(0)); | 130 const unsigned char* peekBefore = static_cast<const unsigned char*>(buffer2.
skip(0)); |
| 131 T obj2; | 131 T obj2; |
| 132 SerializationUtils<T>::Read(buffer2, &obj2); | 132 SerializationUtils<T>::Read(buffer2, &obj2); |
| 133 const unsigned char* peekAfter = static_cast<const unsigned char*>(buffer2.s
kip(0)); | 133 const unsigned char* peekAfter = static_cast<const unsigned char*>(buffer2.s
kip(0)); |
| 134 // This should have succeeded, since there are enough bytes to read this | 134 // This should have succeeded, since there are enough bytes to read this |
| 135 REPORTER_ASSERT(reporter, buffer2.isValid()); | 135 REPORTER_ASSERT(reporter, buffer2.isValid()); |
| 136 REPORTER_ASSERT(reporter, static_cast<size_t>(peekAfter - peekBefore) == byt
esWritten); | 136 REPORTER_ASSERT(reporter, static_cast<size_t>(peekAfter - peekBefore) == byt
esWritten); |
| 137 | 137 |
| 138 TestAlignment(testObj, reporter); | 138 TestAlignment(testObj, reporter); |
| 139 } | 139 } |
| 140 | 140 |
| 141 template<typename T> | 141 template<typename T> |
| 142 static T* TestFlattenableSerialization(T* testObj, bool shouldSucceed, | 142 static T* TestFlattenableSerialization(T* testObj, bool shouldSucceed, |
| 143 skiatest::Reporter* reporter) { | 143 skiatest::Reporter* reporter) { |
| 144 SkOrderedWriteBuffer writer; | 144 SkWriteBuffer writer; |
| 145 writer.setFlags(SkOrderedWriteBuffer::kValidation_Flag); | 145 writer.setFlags(SkWriteBuffer::kValidation_Flag); |
| 146 SerializationUtils<T>::Write(writer, testObj); | 146 SerializationUtils<T>::Write(writer, testObj); |
| 147 size_t bytesWritten = writer.bytesWritten(); | 147 size_t bytesWritten = writer.bytesWritten(); |
| 148 REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten); | 148 REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten); |
| 149 | 149 |
| 150 unsigned char dataWritten[1024]; | 150 unsigned char dataWritten[1024]; |
| 151 SkASSERT(bytesWritten <= sizeof(dataWritten)); | 151 SkASSERT(bytesWritten <= sizeof(dataWritten)); |
| 152 writer.writeToMemory(dataWritten); | 152 writer.writeToMemory(dataWritten); |
| 153 | 153 |
| 154 // Make sure this fails when it should (test with smaller size, but still mu
ltiple of 4) | 154 // Make sure this fails when it should (test with smaller size, but still mu
ltiple of 4) |
| 155 SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 4); | 155 SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 4); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 173 // If the deserialization was supposed to fail, make sure it did | 173 // If the deserialization was supposed to fail, make sure it did |
| 174 REPORTER_ASSERT(reporter, !buffer.isValid()); | 174 REPORTER_ASSERT(reporter, !buffer.isValid()); |
| 175 REPORTER_ASSERT(reporter, NULL == obj2); | 175 REPORTER_ASSERT(reporter, NULL == obj2); |
| 176 } | 176 } |
| 177 | 177 |
| 178 return obj2; // Return object to perform further validity tests on it | 178 return obj2; // Return object to perform further validity tests on it |
| 179 } | 179 } |
| 180 | 180 |
| 181 template<typename T> | 181 template<typename T> |
| 182 static void TestArraySerialization(T* data, skiatest::Reporter* reporter) { | 182 static void TestArraySerialization(T* data, skiatest::Reporter* reporter) { |
| 183 SkOrderedWriteBuffer writer; | 183 SkWriteBuffer writer; |
| 184 writer.setFlags(SkOrderedWriteBuffer::kValidation_Flag); | 184 writer.setFlags(SkWriteBuffer::kValidation_Flag); |
| 185 SerializationUtils<T>::Write(writer, data, kArraySize); | 185 SerializationUtils<T>::Write(writer, data, kArraySize); |
| 186 size_t bytesWritten = writer.bytesWritten(); | 186 size_t bytesWritten = writer.bytesWritten(); |
| 187 // This should write the length (in 4 bytes) and the array | 187 // This should write the length (in 4 bytes) and the array |
| 188 REPORTER_ASSERT(reporter, (4 + kArraySize * sizeof(T)) == bytesWritten); | 188 REPORTER_ASSERT(reporter, (4 + kArraySize * sizeof(T)) == bytesWritten); |
| 189 | 189 |
| 190 unsigned char dataWritten[1024]; | 190 unsigned char dataWritten[1024]; |
| 191 writer.writeToMemory(dataWritten); | 191 writer.writeToMemory(dataWritten); |
| 192 | 192 |
| 193 // Make sure this fails when it should | 193 // Make sure this fails when it should |
| 194 SkValidatingReadBuffer buffer(dataWritten, bytesWritten); | 194 SkValidatingReadBuffer buffer(dataWritten, bytesWritten); |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 326 info.fHeight = 1; | 326 info.fHeight = 1; |
| 327 | 327 |
| 328 invalidBitmap2.setPixelRef(SkMallocPixelRef::NewAllocate( | 328 invalidBitmap2.setPixelRef(SkMallocPixelRef::NewAllocate( |
| 329 info, invalidBitmap2.rowBytes(), NULL))->unref(); | 329 info, invalidBitmap2.rowBytes(), NULL))->unref(); |
| 330 | 330 |
| 331 // The deserialization should detect the pixel ref being too small and f
ail | 331 // The deserialization should detect the pixel ref being too small and f
ail |
| 332 TestBitmapSerialization(validBitmap, invalidBitmap2, false, reporter); | 332 TestBitmapSerialization(validBitmap, invalidBitmap2, false, reporter); |
| 333 #endif | 333 #endif |
| 334 } | 334 } |
| 335 } | 335 } |
| OLD | NEW |