blob: f49abe9106b0569be5eb883b2c3f1e73c5242095 [file] [log] [blame]
commit-bot@chromium.org02512882013-10-31 18:37:50 +00001/*
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
8#include "SkOrderedWriteBuffer.h"
9#include "SkValidatingReadBuffer.h"
10#include "Test.h"
11
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000012static const uint32_t kArraySize = 64;
13
14template<typename T>
15static void TestAlignment(T* testObj, skiatest::Reporter* reporter) {
16 // Test memory read/write functions directly
17 unsigned char dataWritten[1024];
18 size_t bytesWrittenToMemory = testObj->writeToMemory(dataWritten);
19 REPORTER_ASSERT(reporter, SkAlign4(bytesWrittenToMemory) == bytesWrittenToMemory);
20 size_t bytesReadFromMemory = testObj->readFromMemory(dataWritten, bytesWrittenToMemory);
21 REPORTER_ASSERT(reporter, SkAlign4(bytesReadFromMemory) == bytesReadFromMemory);
22}
23
24template<typename T> struct SerializationUtils {
25};
26
27template<> struct SerializationUtils<SkMatrix> {
28 static void Write(SkOrderedWriteBuffer& writer, const SkMatrix* matrix) {
29 writer.writeMatrix(*matrix);
30 }
31 static void Read(SkValidatingReadBuffer& reader, SkMatrix* matrix) {
32 reader.readMatrix(matrix);
33 }
34};
35
36template<> struct SerializationUtils<SkPath> {
37 static void Write(SkOrderedWriteBuffer& writer, const SkPath* path) {
38 writer.writePath(*path);
39 }
40 static void Read(SkValidatingReadBuffer& reader, SkPath* path) {
41 reader.readPath(path);
42 }
43};
44
45template<> struct SerializationUtils<SkRegion> {
46 static void Write(SkOrderedWriteBuffer& writer, const SkRegion* region) {
47 writer.writeRegion(*region);
48 }
49 static void Read(SkValidatingReadBuffer& reader, SkRegion* region) {
50 reader.readRegion(region);
51 }
52};
53
54template<> struct SerializationUtils<unsigned char> {
55 static void Write(SkOrderedWriteBuffer& writer, unsigned char* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +000056 writer.writeByteArray(data, arraySize);
sugoi@google.com305f78e2013-11-04 16:18:15 +000057 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000058 static bool Read(SkValidatingReadBuffer& reader, unsigned char* data, uint32_t arraySize) {
59 return reader.readByteArray(data, arraySize);
60 }
61};
commit-bot@chromium.org02512882013-10-31 18:37:50 +000062
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000063template<> struct SerializationUtils<SkColor> {
64 static void Write(SkOrderedWriteBuffer& writer, SkColor* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +000065 writer.writeColorArray(data, arraySize);
sugoi@google.comb48a59a2013-11-04 20:28:23 +000066 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000067 static bool Read(SkValidatingReadBuffer& reader, SkColor* data, uint32_t arraySize) {
68 return reader.readColorArray(data, arraySize);
69 }
70};
sugoi@google.com305f78e2013-11-04 16:18:15 +000071
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000072template<> struct SerializationUtils<int32_t> {
73 static void Write(SkOrderedWriteBuffer& writer, int32_t* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +000074 writer.writeIntArray(data, arraySize);
sugoi@google.comb48a59a2013-11-04 20:28:23 +000075 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000076 static bool Read(SkValidatingReadBuffer& reader, int32_t* data, uint32_t arraySize) {
77 return reader.readIntArray(data, arraySize);
78 }
79};
sugoi@google.com305f78e2013-11-04 16:18:15 +000080
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000081template<> struct SerializationUtils<SkPoint> {
82 static void Write(SkOrderedWriteBuffer& writer, SkPoint* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +000083 writer.writePointArray(data, arraySize);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000084 }
85 static bool Read(SkValidatingReadBuffer& reader, SkPoint* data, uint32_t arraySize) {
86 return reader.readPointArray(data, arraySize);
87 }
88};
reed@google.com12a23862013-11-04 21:35:55 +000089
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000090template<> struct SerializationUtils<SkScalar> {
91 static void Write(SkOrderedWriteBuffer& writer, SkScalar* data, uint32_t arraySize) {
92 writer.writeScalarArray(data, arraySize);
93 }
94 static bool Read(SkValidatingReadBuffer& reader, SkScalar* data, uint32_t arraySize) {
95 return reader.readScalarArray(data, arraySize);
96 }
97};
reed@google.com12a23862013-11-04 21:35:55 +000098
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000099template<typename T>
100static void TestObjectSerialization(T* testObj, skiatest::Reporter* reporter) {
101 SkOrderedWriteBuffer writer(1024);
102 writer.setFlags(SkOrderedWriteBuffer::kValidation_Flag);
103 SerializationUtils<T>::Write(writer, testObj);
104 size_t bytesWritten = writer.bytesWritten();
105 REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten);
reed@google.com12a23862013-11-04 21:35:55 +0000106
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000107 unsigned char dataWritten[1024];
108 writer.writeToMemory(dataWritten);
109
110 // Make sure this fails when it should (test with smaller size, but still multiple of 4)
111 SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 4);
commit-bot@chromium.org8f457e32013-11-08 19:22:57 +0000112 T obj;
113 SerializationUtils<T>::Read(buffer, &obj);
114 REPORTER_ASSERT(reporter, !buffer.validate(true));
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000115
116 // Make sure this succeeds when it should
117 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten);
commit-bot@chromium.org8f457e32013-11-08 19:22:57 +0000118 const unsigned char* peekBefore = static_cast<const unsigned char*>(buffer2.skip(0));
119 T obj2;
120 SerializationUtils<T>::Read(buffer2, &obj2);
121 const unsigned char* peekAfter = static_cast<const unsigned char*>(buffer2.skip(0));
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000122 // This should have succeeded, since there are enough bytes to read this
commit-bot@chromium.org8f457e32013-11-08 19:22:57 +0000123 REPORTER_ASSERT(reporter, buffer2.validate(true));
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000124 REPORTER_ASSERT(reporter, static_cast<size_t>(peekAfter - peekBefore) == bytesWritten);
125
126 TestAlignment(testObj, reporter);
127}
128
129template<typename T>
130static void TestArraySerialization(T* data, skiatest::Reporter* reporter) {
131 SkOrderedWriteBuffer writer(1024);
132 writer.setFlags(SkOrderedWriteBuffer::kValidation_Flag);
133 SerializationUtils<T>::Write(writer, data, kArraySize);
134 size_t bytesWritten = writer.bytesWritten();
135 // This should write the length (in 4 bytes) and the array
136 REPORTER_ASSERT(reporter, (4 + kArraySize * sizeof(T)) == bytesWritten);
137
138 unsigned char dataWritten[1024];
139 writer.writeToMemory(dataWritten);
140
141 // Make sure this fails when it should
142 SkValidatingReadBuffer buffer(dataWritten, bytesWritten);
143 T dataRead[kArraySize];
144 bool success = SerializationUtils<T>::Read(buffer, dataRead, kArraySize / 2);
145 // This should have failed, since the provided size was too small
146 REPORTER_ASSERT(reporter, !success);
147
148 // Make sure this succeeds when it should
149 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten);
150 success = SerializationUtils<T>::Read(buffer2, dataRead, kArraySize);
151 // This should have succeeded, since there are enough bytes to read this
152 REPORTER_ASSERT(reporter, success);
153}
154
155static void Tests(skiatest::Reporter* reporter) {
156 // Test matrix serialization
157 {
158 SkMatrix matrix = SkMatrix::I();
159 TestObjectSerialization(&matrix, reporter);
160 }
161
162 // Test path serialization
163 {
164 SkPath path;
165 TestObjectSerialization(&path, reporter);
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000166 }
sugoi@google.com305f78e2013-11-04 16:18:15 +0000167
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000168 // Test region serialization
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000169 {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000170 SkRegion region;
171 TestObjectSerialization(&region, reporter);
172 }
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000173
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000174 // Test rrect serialization
175 {
176 SkRRect rrect;
177 TestAlignment(&rrect, reporter);
178 }
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000179
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000180 // Test readByteArray
181 {
182 unsigned char data[kArraySize] = {0};
183 TestArraySerialization(data, reporter);
184 }
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000185
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000186 // Test readColorArray
187 {
188 SkColor data[kArraySize];
189 TestArraySerialization(data, reporter);
190 }
191
192 // Test readIntArray
193 {
194 int32_t data[kArraySize];
195 TestArraySerialization(data, reporter);
196 }
197
198 // Test readPointArray
199 {
200 SkPoint data[kArraySize];
201 TestArraySerialization(data, reporter);
202 }
203
204 // Test readScalarArray
205 {
206 SkScalar data[kArraySize];
207 TestArraySerialization(data, reporter);
commit-bot@chromium.org02512882013-10-31 18:37:50 +0000208 }
209}
210
211#include "TestClassDef.h"
212DEFINE_TESTCLASS("Serialization", SerializationClass, Tests)