blob: af146006e7f1975ac20bb24a971dd36e61561650 [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
caseq26337e92014-06-30 12:14:52 -07008#include "Resources.h"
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +00009#include "SkBitmapSource.h"
10#include "SkCanvas.h"
11#include "SkMallocPixelRef.h"
caseq26337e92014-06-30 12:14:52 -070012#include "SkOSFile.h"
robertphillips@google.com770963f2014-04-18 18:04:41 +000013#include "SkPictureRecorder.h"
commit-bot@chromium.org1e7ee992014-03-14 21:22:22 +000014#include "SkTemplates.h"
caseq26337e92014-06-30 12:14:52 -070015#include "SkTypeface.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000016#include "SkWriteBuffer.h"
commit-bot@chromium.org02512882013-10-31 18:37:50 +000017#include "SkValidatingReadBuffer.h"
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +000018#include "SkXfermodeImageFilter.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000019#include "Test.h"
commit-bot@chromium.org02512882013-10-31 18:37:50 +000020
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000021static const uint32_t kArraySize = 64;
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +000022static const int kBitmapSize = 256;
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000023
24template<typename T>
25static void TestAlignment(T* testObj, skiatest::Reporter* reporter) {
26 // Test memory read/write functions directly
27 unsigned char dataWritten[1024];
28 size_t bytesWrittenToMemory = testObj->writeToMemory(dataWritten);
29 REPORTER_ASSERT(reporter, SkAlign4(bytesWrittenToMemory) == bytesWrittenToMemory);
30 size_t bytesReadFromMemory = testObj->readFromMemory(dataWritten, bytesWrittenToMemory);
31 REPORTER_ASSERT(reporter, SkAlign4(bytesReadFromMemory) == bytesReadFromMemory);
32}
33
34template<typename T> struct SerializationUtils {
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +000035 // Generic case for flattenables
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000036 static void Write(SkWriteBuffer& writer, const T* flattenable) {
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +000037 writer.writeFlattenable(flattenable);
38 }
39 static void Read(SkValidatingReadBuffer& reader, T** flattenable) {
40 *flattenable = (T*)reader.readFlattenable(T::GetFlattenableType());
41 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000042};
43
44template<> struct SerializationUtils<SkMatrix> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000045 static void Write(SkWriteBuffer& writer, const SkMatrix* matrix) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000046 writer.writeMatrix(*matrix);
47 }
48 static void Read(SkValidatingReadBuffer& reader, SkMatrix* matrix) {
49 reader.readMatrix(matrix);
50 }
51};
52
53template<> struct SerializationUtils<SkPath> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000054 static void Write(SkWriteBuffer& writer, const SkPath* path) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000055 writer.writePath(*path);
56 }
57 static void Read(SkValidatingReadBuffer& reader, SkPath* path) {
58 reader.readPath(path);
59 }
60};
61
62template<> struct SerializationUtils<SkRegion> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000063 static void Write(SkWriteBuffer& writer, const SkRegion* region) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000064 writer.writeRegion(*region);
65 }
66 static void Read(SkValidatingReadBuffer& reader, SkRegion* region) {
67 reader.readRegion(region);
68 }
69};
70
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +000071template<> struct SerializationUtils<SkString> {
72 static void Write(SkWriteBuffer& writer, const SkString* string) {
73 writer.writeString(string->c_str());
74 }
75 static void Read(SkValidatingReadBuffer& reader, SkString* string) {
76 reader.readString(string);
77 }
78};
79
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000080template<> struct SerializationUtils<unsigned char> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000081 static void Write(SkWriteBuffer& writer, unsigned char* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +000082 writer.writeByteArray(data, arraySize);
sugoi@google.com305f78e2013-11-04 16:18:15 +000083 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000084 static bool Read(SkValidatingReadBuffer& reader, unsigned char* data, uint32_t arraySize) {
85 return reader.readByteArray(data, arraySize);
86 }
87};
commit-bot@chromium.org02512882013-10-31 18:37:50 +000088
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000089template<> struct SerializationUtils<SkColor> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000090 static void Write(SkWriteBuffer& writer, SkColor* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +000091 writer.writeColorArray(data, arraySize);
sugoi@google.comb48a59a2013-11-04 20:28:23 +000092 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000093 static bool Read(SkValidatingReadBuffer& reader, SkColor* data, uint32_t arraySize) {
94 return reader.readColorArray(data, arraySize);
95 }
96};
sugoi@google.com305f78e2013-11-04 16:18:15 +000097
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +000098template<> struct SerializationUtils<int32_t> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000099 static void Write(SkWriteBuffer& writer, int32_t* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +0000100 writer.writeIntArray(data, arraySize);
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000101 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000102 static bool Read(SkValidatingReadBuffer& reader, int32_t* data, uint32_t arraySize) {
103 return reader.readIntArray(data, arraySize);
104 }
105};
sugoi@google.com305f78e2013-11-04 16:18:15 +0000106
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000107template<> struct SerializationUtils<SkPoint> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000108 static void Write(SkWriteBuffer& writer, SkPoint* data, uint32_t arraySize) {
reed@google.com12a23862013-11-04 21:35:55 +0000109 writer.writePointArray(data, arraySize);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000110 }
111 static bool Read(SkValidatingReadBuffer& reader, SkPoint* data, uint32_t arraySize) {
112 return reader.readPointArray(data, arraySize);
113 }
114};
reed@google.com12a23862013-11-04 21:35:55 +0000115
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000116template<> struct SerializationUtils<SkScalar> {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000117 static void Write(SkWriteBuffer& writer, SkScalar* data, uint32_t arraySize) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000118 writer.writeScalarArray(data, arraySize);
119 }
120 static bool Read(SkValidatingReadBuffer& reader, SkScalar* data, uint32_t arraySize) {
121 return reader.readScalarArray(data, arraySize);
122 }
123};
reed@google.com12a23862013-11-04 21:35:55 +0000124
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000125template<typename T, bool testInvalid> struct SerializationTestUtils {
126 static void InvalidateData(unsigned char* data) {}
127};
128
129template<> struct SerializationTestUtils<SkString, true> {
130 static void InvalidateData(unsigned char* data) {
131 data[3] |= 0x80; // Reverse sign of 1st integer
132 }
133};
134
135template<typename T, bool testInvalid>
136static void TestObjectSerializationNoAlign(T* testObj, skiatest::Reporter* reporter) {
commit-bot@chromium.orga2bd2d12014-01-30 22:16:32 +0000137 SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000138 SerializationUtils<T>::Write(writer, testObj);
139 size_t bytesWritten = writer.bytesWritten();
140 REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten);
reed@google.com12a23862013-11-04 21:35:55 +0000141
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000142 unsigned char dataWritten[1024];
143 writer.writeToMemory(dataWritten);
144
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000145 SerializationTestUtils<T, testInvalid>::InvalidateData(dataWritten);
146
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000147 // Make sure this fails when it should (test with smaller size, but still multiple of 4)
148 SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 4);
commit-bot@chromium.org8f457e32013-11-08 19:22:57 +0000149 T obj;
150 SerializationUtils<T>::Read(buffer, &obj);
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000151 REPORTER_ASSERT(reporter, !buffer.isValid());
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000152
153 // Make sure this succeeds when it should
154 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten);
commit-bot@chromium.org8f457e32013-11-08 19:22:57 +0000155 const unsigned char* peekBefore = static_cast<const unsigned char*>(buffer2.skip(0));
156 T obj2;
157 SerializationUtils<T>::Read(buffer2, &obj2);
158 const unsigned char* peekAfter = static_cast<const unsigned char*>(buffer2.skip(0));
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000159 // This should have succeeded, since there are enough bytes to read this
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000160 REPORTER_ASSERT(reporter, buffer2.isValid() == !testInvalid);
161 // Note: This following test should always succeed, regardless of whether the buffer is valid,
162 // since if it is invalid, it will simply skip to the end, as if it had read the whole buffer.
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000163 REPORTER_ASSERT(reporter, static_cast<size_t>(peekAfter - peekBefore) == bytesWritten);
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000164}
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000165
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000166template<typename T>
167static void TestObjectSerialization(T* testObj, skiatest::Reporter* reporter) {
168 TestObjectSerializationNoAlign<T, false>(testObj, reporter);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000169 TestAlignment(testObj, reporter);
170}
171
172template<typename T>
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000173static T* TestFlattenableSerialization(T* testObj, bool shouldSucceed,
174 skiatest::Reporter* reporter) {
commit-bot@chromium.orga2bd2d12014-01-30 22:16:32 +0000175 SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000176 SerializationUtils<T>::Write(writer, testObj);
177 size_t bytesWritten = writer.bytesWritten();
178 REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten);
179
180 unsigned char dataWritten[1024];
reed@google.combf790232013-12-13 19:45:58 +0000181 SkASSERT(bytesWritten <= sizeof(dataWritten));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000182 writer.writeToMemory(dataWritten);
183
184 // Make sure this fails when it should (test with smaller size, but still multiple of 4)
185 SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 4);
186 T* obj = NULL;
187 SerializationUtils<T>::Read(buffer, &obj);
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000188 REPORTER_ASSERT(reporter, !buffer.isValid());
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000189 REPORTER_ASSERT(reporter, NULL == obj);
190
191 // Make sure this succeeds when it should
192 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten);
193 const unsigned char* peekBefore = static_cast<const unsigned char*>(buffer2.skip(0));
194 T* obj2 = NULL;
195 SerializationUtils<T>::Read(buffer2, &obj2);
196 const unsigned char* peekAfter = static_cast<const unsigned char*>(buffer2.skip(0));
197 if (shouldSucceed) {
198 // This should have succeeded, since there are enough bytes to read this
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000199 REPORTER_ASSERT(reporter, buffer2.isValid());
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000200 REPORTER_ASSERT(reporter, static_cast<size_t>(peekAfter - peekBefore) == bytesWritten);
bsalomon49f085d2014-09-05 13:34:00 -0700201 REPORTER_ASSERT(reporter, obj2);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000202 } else {
203 // If the deserialization was supposed to fail, make sure it did
commit-bot@chromium.orgc2e9db32013-12-06 20:14:46 +0000204 REPORTER_ASSERT(reporter, !buffer.isValid());
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000205 REPORTER_ASSERT(reporter, NULL == obj2);
206 }
207
208 return obj2; // Return object to perform further validity tests on it
209}
210
211template<typename T>
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000212static void TestArraySerialization(T* data, skiatest::Reporter* reporter) {
commit-bot@chromium.orga2bd2d12014-01-30 22:16:32 +0000213 SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000214 SerializationUtils<T>::Write(writer, data, kArraySize);
215 size_t bytesWritten = writer.bytesWritten();
216 // This should write the length (in 4 bytes) and the array
217 REPORTER_ASSERT(reporter, (4 + kArraySize * sizeof(T)) == bytesWritten);
218
219 unsigned char dataWritten[1024];
220 writer.writeToMemory(dataWritten);
221
222 // Make sure this fails when it should
223 SkValidatingReadBuffer buffer(dataWritten, bytesWritten);
224 T dataRead[kArraySize];
225 bool success = SerializationUtils<T>::Read(buffer, dataRead, kArraySize / 2);
226 // This should have failed, since the provided size was too small
227 REPORTER_ASSERT(reporter, !success);
228
229 // Make sure this succeeds when it should
230 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten);
231 success = SerializationUtils<T>::Read(buffer2, dataRead, kArraySize);
232 // This should have succeeded, since there are enough bytes to read this
233 REPORTER_ASSERT(reporter, success);
234}
235
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000236static void TestBitmapSerialization(const SkBitmap& validBitmap,
237 const SkBitmap& invalidBitmap,
238 bool shouldSucceed,
239 skiatest::Reporter* reporter) {
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000240 SkAutoTUnref<SkBitmapSource> validBitmapSource(SkBitmapSource::Create(validBitmap));
241 SkAutoTUnref<SkBitmapSource> invalidBitmapSource(SkBitmapSource::Create(invalidBitmap));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000242 SkAutoTUnref<SkXfermode> mode(SkXfermode::Create(SkXfermode::kSrcOver_Mode));
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000243 SkAutoTUnref<SkXfermodeImageFilter> xfermodeImageFilter(
244 SkXfermodeImageFilter::Create(mode, invalidBitmapSource, validBitmapSource));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000245
246 SkAutoTUnref<SkImageFilter> deserializedFilter(
247 TestFlattenableSerialization<SkImageFilter>(
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000248 xfermodeImageFilter, shouldSucceed, reporter));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000249
250 // Try to render a small bitmap using the invalid deserialized filter
251 // to make sure we don't crash while trying to render it
252 if (shouldSucceed) {
253 SkBitmap bitmap;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000254 bitmap.allocN32Pixels(24, 24);
255 SkCanvas canvas(bitmap);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000256 canvas.clear(0x00000000);
257 SkPaint paint;
258 paint.setImageFilter(deserializedFilter);
259 canvas.clipRect(SkRect::MakeXYWH(0, 0, SkIntToScalar(24), SkIntToScalar(24)));
260 canvas.drawBitmap(bitmap, 0, 0, &paint);
261 }
262}
263
senorblanco0f7197b2014-09-24 11:09:38 -0700264static void TestXfermodeSerialization(skiatest::Reporter* reporter) {
265 for (size_t i = 0; i <= SkXfermode::kLastMode; ++i) {
266 if (i == SkXfermode::kSrcOver_Mode) {
267 // skip SrcOver, as it is allowed to return NULL from Create()
268 continue;
269 }
270 SkAutoTUnref<SkXfermode> mode(SkXfermode::Create(static_cast<SkXfermode::Mode>(i)));
271 REPORTER_ASSERT(reporter, mode.get());
272 SkAutoTUnref<SkXfermode> copy(
273 TestFlattenableSerialization<SkXfermode>(mode.get(), true, reporter));
274 }
275}
276
caseq26337e92014-06-30 12:14:52 -0700277static SkBitmap draw_picture(SkPicture& picture) {
278 SkBitmap bitmap;
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700279 bitmap.allocN32Pixels(SkScalarCeilToInt(picture.cullRect().width()),
280 SkScalarCeilToInt(picture.cullRect().height()));
caseq26337e92014-06-30 12:14:52 -0700281 SkCanvas canvas(bitmap);
robertphillipsc5ba71d2014-09-04 08:42:50 -0700282 picture.playback(&canvas);
caseq26337e92014-06-30 12:14:52 -0700283 return bitmap;
284}
285
286static void compare_bitmaps(skiatest::Reporter* reporter,
287 const SkBitmap& b1, const SkBitmap& b2) {
288 REPORTER_ASSERT(reporter, b1.width() == b2.width());
289 REPORTER_ASSERT(reporter, b1.height() == b2.height());
290 SkAutoLockPixels autoLockPixels1(b1);
291 SkAutoLockPixels autoLockPixels2(b2);
292
293 if ((b1.width() != b2.width()) ||
294 (b1.height() != b2.height())) {
295 return;
296 }
297
298 int pixelErrors = 0;
299 for (int y = 0; y < b2.height(); ++y) {
300 for (int x = 0; x < b2.width(); ++x) {
301 if (b1.getColor(x, y) != b2.getColor(x, y))
302 ++pixelErrors;
303 }
304 }
305 REPORTER_ASSERT(reporter, 0 == pixelErrors);
306}
307
308static void TestPictureTypefaceSerialization(skiatest::Reporter* reporter) {
309 // Load typeface form file.
310 // This test cannot run if there is no resource path.
311 SkString resourcePath = GetResourcePath();
312 if (resourcePath.isEmpty()) {
313 SkDebugf("Could not run fontstream test because resourcePath not specified.");
314 return;
315 }
tfarinaa8e2e152014-07-28 19:26:58 -0700316 SkString filename = SkOSPath::Join(resourcePath.c_str(), "test.ttc");
bungemand71b7572014-09-18 10:55:32 -0700317 SkTypeface* typeface = SkTypeface::CreateFromFile(filename.c_str(), 1);
caseq26337e92014-06-30 12:14:52 -0700318 if (!typeface) {
319 SkDebugf("Could not run fontstream test because test.ttc not found.");
320 return;
321 }
322
323 // Create a paint with the typeface we loaded.
324 SkPaint paint;
325 paint.setColor(SK_ColorGRAY);
326 paint.setTextSize(SkIntToScalar(30));
327 SkSafeUnref(paint.setTypeface(typeface));
328
329 // Paint some text.
330 SkPictureRecorder recorder;
331 SkIRect canvasRect = SkIRect::MakeWH(kBitmapSize, kBitmapSize);
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700332 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(canvasRect.width()),
333 SkIntToScalar(canvasRect.height()),
334 NULL, 0);
caseq26337e92014-06-30 12:14:52 -0700335 canvas->drawColor(SK_ColorWHITE);
bungemand71b7572014-09-18 10:55:32 -0700336 canvas->drawText("A!", 2, 24, 32, paint);
caseq26337e92014-06-30 12:14:52 -0700337 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
338
339 // Serlialize picture and create its clone from stream.
340 SkDynamicMemoryWStream stream;
341 picture->serialize(&stream);
342 SkAutoTUnref<SkStream> inputStream(stream.detachAsStream());
343 SkAutoTUnref<SkPicture> loadedPicture(SkPicture::CreateFromStream(inputStream.get()));
344
345 // Draw both original and clone picture and compare bitmaps -- they should be identical.
346 SkBitmap origBitmap = draw_picture(*picture);
347 SkBitmap destBitmap = draw_picture(*loadedPicture);
348 compare_bitmaps(reporter, origBitmap, destBitmap);
349}
350
reed84825042014-09-02 12:50:45 -0700351static void setup_bitmap_for_canvas(SkBitmap* bitmap) {
352 bitmap->allocN32Pixels(kBitmapSize, kBitmapSize);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000353}
354
reed84825042014-09-02 12:50:45 -0700355static void make_checkerboard_bitmap(SkBitmap& bitmap) {
356 setup_bitmap_for_canvas(&bitmap);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000357
358 SkCanvas canvas(bitmap);
359 canvas.clear(0x00000000);
360 SkPaint darkPaint;
361 darkPaint.setColor(0xFF804020);
362 SkPaint lightPaint;
363 lightPaint.setColor(0xFF244484);
364 const int i = kBitmapSize / 8;
365 const SkScalar f = SkIntToScalar(i);
366 for (int y = 0; y < kBitmapSize; y += i) {
367 for (int x = 0; x < kBitmapSize; x += i) {
368 canvas.save();
369 canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
370 canvas.drawRect(SkRect::MakeXYWH(0, 0, f, f), darkPaint);
371 canvas.drawRect(SkRect::MakeXYWH(f, 0, f, f), lightPaint);
372 canvas.drawRect(SkRect::MakeXYWH(0, f, f, f), lightPaint);
373 canvas.drawRect(SkRect::MakeXYWH(f, f, f, f), darkPaint);
374 canvas.restore();
375 }
376 }
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000377}
378
reed84825042014-09-02 12:50:45 -0700379static void draw_something(SkCanvas* canvas) {
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000380 SkPaint paint;
381 SkBitmap bitmap;
reed84825042014-09-02 12:50:45 -0700382 make_checkerboard_bitmap(bitmap);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000383
384 canvas->save();
385 canvas->scale(0.5f, 0.5f);
386 canvas->drawBitmap(bitmap, 0, 0, NULL);
387 canvas->restore();
388
389 const char beforeStr[] = "before circle";
390 const char afterStr[] = "after circle";
391
392 paint.setAntiAlias(true);
393
394 paint.setColor(SK_ColorRED);
395 canvas->drawData(beforeStr, sizeof(beforeStr));
396 canvas->drawCircle(SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/3), paint);
397 canvas->drawData(afterStr, sizeof(afterStr));
398 paint.setColor(SK_ColorBLACK);
399 paint.setTextSize(SkIntToScalar(kBitmapSize/3));
400 canvas->drawText("Picture", 7, SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/4), paint);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000401}
402
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000403DEF_TEST(Serialization, reporter) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000404 // Test matrix serialization
405 {
406 SkMatrix matrix = SkMatrix::I();
407 TestObjectSerialization(&matrix, reporter);
caseq26337e92014-06-30 12:14:52 -0700408 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000409
410 // Test path serialization
411 {
412 SkPath path;
413 TestObjectSerialization(&path, reporter);
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000414 }
sugoi@google.com305f78e2013-11-04 16:18:15 +0000415
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000416 // Test region serialization
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000417 {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000418 SkRegion region;
419 TestObjectSerialization(&region, reporter);
420 }
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000421
senorblanco0f7197b2014-09-24 11:09:38 -0700422 // Test xfermode serialization
423 {
424 TestXfermodeSerialization(reporter);
425 }
426
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000427 // Test string serialization
428 {
429 SkString string("string");
430 TestObjectSerializationNoAlign<SkString, false>(&string, reporter);
431 TestObjectSerializationNoAlign<SkString, true>(&string, reporter);
432 }
433
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000434 // Test rrect serialization
435 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000436 // SkRRect does not initialize anything.
437 // An uninitialized SkRRect can be serialized,
438 // but will branch on uninitialized data when deserialized.
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000439 SkRRect rrect;
rmistry@google.comd6bab022013-12-02 13:50:38 +0000440 SkRect rect = SkRect::MakeXYWH(1, 2, 20, 30);
441 SkVector corners[4] = { {1, 2}, {2, 3}, {3,4}, {4,5} };
442 rrect.setRectRadii(rect, corners);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000443 TestAlignment(&rrect, reporter);
444 }
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000445
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000446 // Test readByteArray
447 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000448 unsigned char data[kArraySize] = { 1, 2, 3 };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000449 TestArraySerialization(data, reporter);
450 }
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000451
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000452 // Test readColorArray
453 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000454 SkColor data[kArraySize] = { SK_ColorBLACK, SK_ColorWHITE, SK_ColorRED };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000455 TestArraySerialization(data, reporter);
456 }
457
458 // Test readIntArray
459 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000460 int32_t data[kArraySize] = { 1, 2, 4, 8 };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000461 TestArraySerialization(data, reporter);
462 }
463
464 // Test readPointArray
465 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000466 SkPoint data[kArraySize] = { {6, 7}, {42, 128} };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000467 TestArraySerialization(data, reporter);
468 }
469
470 // Test readScalarArray
471 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000472 SkScalar data[kArraySize] = { SK_Scalar1, SK_ScalarHalf, SK_ScalarMax };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000473 TestArraySerialization(data, reporter);
commit-bot@chromium.org02512882013-10-31 18:37:50 +0000474 }
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000475
476 // Test invalid deserializations
477 {
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000478 SkImageInfo info = SkImageInfo::MakeN32Premul(kBitmapSize, kBitmapSize);
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000479
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000480 SkBitmap validBitmap;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000481 validBitmap.setInfo(info);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000482
483 // Create a bitmap with a really large height
484 SkBitmap invalidBitmap;
reede5ea5002014-09-03 11:54:58 -0700485 invalidBitmap.setInfo(info.makeWH(info.width(), 1000000000));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000486
487 // The deserialization should succeed, and the rendering shouldn't crash,
488 // even when the device fails to initialize, due to its size
489 TestBitmapSerialization(validBitmap, invalidBitmap, true, reporter);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000490 }
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000491
492 // Test simple SkPicture serialization
493 {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000494 SkPictureRecorder recorder;
reed84825042014-09-02 12:50:45 -0700495 draw_something(recorder.beginRecording(SkIntToScalar(kBitmapSize),
496 SkIntToScalar(kBitmapSize),
497 NULL, 0));
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000498 SkAutoTUnref<SkPicture> pict(recorder.endRecording());
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000499
500 // Serialize picture
501 SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag);
502 pict->flatten(writer);
503 size_t size = writer.bytesWritten();
commit-bot@chromium.org1e7ee992014-03-14 21:22:22 +0000504 SkAutoTMalloc<unsigned char> data(size);
505 writer.writeToMemory(static_cast<void*>(data.get()));
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000506
507 // Deserialize picture
commit-bot@chromium.org1e7ee992014-03-14 21:22:22 +0000508 SkValidatingReadBuffer reader(static_cast<void*>(data.get()), size);
commit-bot@chromium.orge5eee512014-03-13 16:18:49 +0000509 SkAutoTUnref<SkPicture> readPict(
510 SkPicture::CreateFromBuffer(reader));
bsalomon49f085d2014-09-05 13:34:00 -0700511 REPORTER_ASSERT(reporter, readPict.get());
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000512 }
caseq26337e92014-06-30 12:14:52 -0700513
514 TestPictureTypefaceSerialization(reporter);
commit-bot@chromium.org02512882013-10-31 18:37:50 +0000515}