blob: b5582dccfc4201b376187a9d411f0687afe4b57b [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);
201 REPORTER_ASSERT(reporter, NULL != obj2);
202 } 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
caseq26337e92014-06-30 12:14:52 -0700264static SkBitmap draw_picture(SkPicture& picture) {
265 SkBitmap bitmap;
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700266 bitmap.allocN32Pixels(SkScalarCeilToInt(picture.cullRect().width()),
267 SkScalarCeilToInt(picture.cullRect().height()));
caseq26337e92014-06-30 12:14:52 -0700268 SkCanvas canvas(bitmap);
robertphillipsc5ba71d2014-09-04 08:42:50 -0700269 picture.playback(&canvas);
caseq26337e92014-06-30 12:14:52 -0700270 return bitmap;
271}
272
273static void compare_bitmaps(skiatest::Reporter* reporter,
274 const SkBitmap& b1, const SkBitmap& b2) {
275 REPORTER_ASSERT(reporter, b1.width() == b2.width());
276 REPORTER_ASSERT(reporter, b1.height() == b2.height());
277 SkAutoLockPixels autoLockPixels1(b1);
278 SkAutoLockPixels autoLockPixels2(b2);
279
280 if ((b1.width() != b2.width()) ||
281 (b1.height() != b2.height())) {
282 return;
283 }
284
285 int pixelErrors = 0;
286 for (int y = 0; y < b2.height(); ++y) {
287 for (int x = 0; x < b2.width(); ++x) {
288 if (b1.getColor(x, y) != b2.getColor(x, y))
289 ++pixelErrors;
290 }
291 }
292 REPORTER_ASSERT(reporter, 0 == pixelErrors);
293}
294
295static void TestPictureTypefaceSerialization(skiatest::Reporter* reporter) {
296 // Load typeface form file.
297 // This test cannot run if there is no resource path.
298 SkString resourcePath = GetResourcePath();
299 if (resourcePath.isEmpty()) {
300 SkDebugf("Could not run fontstream test because resourcePath not specified.");
301 return;
302 }
tfarinaa8e2e152014-07-28 19:26:58 -0700303 SkString filename = SkOSPath::Join(resourcePath.c_str(), "test.ttc");
caseq26337e92014-06-30 12:14:52 -0700304 SkTypeface* typeface = SkTypeface::CreateFromFile(filename.c_str());
305 if (!typeface) {
306 SkDebugf("Could not run fontstream test because test.ttc not found.");
307 return;
308 }
309
310 // Create a paint with the typeface we loaded.
311 SkPaint paint;
312 paint.setColor(SK_ColorGRAY);
313 paint.setTextSize(SkIntToScalar(30));
314 SkSafeUnref(paint.setTypeface(typeface));
315
316 // Paint some text.
317 SkPictureRecorder recorder;
318 SkIRect canvasRect = SkIRect::MakeWH(kBitmapSize, kBitmapSize);
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700319 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(canvasRect.width()),
320 SkIntToScalar(canvasRect.height()),
321 NULL, 0);
caseq26337e92014-06-30 12:14:52 -0700322 canvas->drawColor(SK_ColorWHITE);
323 canvas->drawText("A", 1, 24, 32, paint);
324 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
325
326 // Serlialize picture and create its clone from stream.
327 SkDynamicMemoryWStream stream;
328 picture->serialize(&stream);
329 SkAutoTUnref<SkStream> inputStream(stream.detachAsStream());
330 SkAutoTUnref<SkPicture> loadedPicture(SkPicture::CreateFromStream(inputStream.get()));
331
332 // Draw both original and clone picture and compare bitmaps -- they should be identical.
333 SkBitmap origBitmap = draw_picture(*picture);
334 SkBitmap destBitmap = draw_picture(*loadedPicture);
335 compare_bitmaps(reporter, origBitmap, destBitmap);
336}
337
reed84825042014-09-02 12:50:45 -0700338static void setup_bitmap_for_canvas(SkBitmap* bitmap) {
339 bitmap->allocN32Pixels(kBitmapSize, kBitmapSize);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000340}
341
reed84825042014-09-02 12:50:45 -0700342static void make_checkerboard_bitmap(SkBitmap& bitmap) {
343 setup_bitmap_for_canvas(&bitmap);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000344
345 SkCanvas canvas(bitmap);
346 canvas.clear(0x00000000);
347 SkPaint darkPaint;
348 darkPaint.setColor(0xFF804020);
349 SkPaint lightPaint;
350 lightPaint.setColor(0xFF244484);
351 const int i = kBitmapSize / 8;
352 const SkScalar f = SkIntToScalar(i);
353 for (int y = 0; y < kBitmapSize; y += i) {
354 for (int x = 0; x < kBitmapSize; x += i) {
355 canvas.save();
356 canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
357 canvas.drawRect(SkRect::MakeXYWH(0, 0, f, f), darkPaint);
358 canvas.drawRect(SkRect::MakeXYWH(f, 0, f, f), lightPaint);
359 canvas.drawRect(SkRect::MakeXYWH(0, f, f, f), lightPaint);
360 canvas.drawRect(SkRect::MakeXYWH(f, f, f, f), darkPaint);
361 canvas.restore();
362 }
363 }
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000364}
365
reed84825042014-09-02 12:50:45 -0700366static void draw_something(SkCanvas* canvas) {
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000367 SkPaint paint;
368 SkBitmap bitmap;
reed84825042014-09-02 12:50:45 -0700369 make_checkerboard_bitmap(bitmap);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000370
371 canvas->save();
372 canvas->scale(0.5f, 0.5f);
373 canvas->drawBitmap(bitmap, 0, 0, NULL);
374 canvas->restore();
375
376 const char beforeStr[] = "before circle";
377 const char afterStr[] = "after circle";
378
379 paint.setAntiAlias(true);
380
381 paint.setColor(SK_ColorRED);
382 canvas->drawData(beforeStr, sizeof(beforeStr));
383 canvas->drawCircle(SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/3), paint);
384 canvas->drawData(afterStr, sizeof(afterStr));
385 paint.setColor(SK_ColorBLACK);
386 paint.setTextSize(SkIntToScalar(kBitmapSize/3));
387 canvas->drawText("Picture", 7, SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/4), paint);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000388}
389
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000390DEF_TEST(Serialization, reporter) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000391 // Test matrix serialization
392 {
393 SkMatrix matrix = SkMatrix::I();
394 TestObjectSerialization(&matrix, reporter);
caseq26337e92014-06-30 12:14:52 -0700395 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000396
397 // Test path serialization
398 {
399 SkPath path;
400 TestObjectSerialization(&path, reporter);
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000401 }
sugoi@google.com305f78e2013-11-04 16:18:15 +0000402
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000403 // Test region serialization
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000404 {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000405 SkRegion region;
406 TestObjectSerialization(&region, reporter);
407 }
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000408
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000409 // Test string serialization
410 {
411 SkString string("string");
412 TestObjectSerializationNoAlign<SkString, false>(&string, reporter);
413 TestObjectSerializationNoAlign<SkString, true>(&string, reporter);
414 }
415
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000416 // Test rrect serialization
417 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000418 // SkRRect does not initialize anything.
419 // An uninitialized SkRRect can be serialized,
420 // but will branch on uninitialized data when deserialized.
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000421 SkRRect rrect;
rmistry@google.comd6bab022013-12-02 13:50:38 +0000422 SkRect rect = SkRect::MakeXYWH(1, 2, 20, 30);
423 SkVector corners[4] = { {1, 2}, {2, 3}, {3,4}, {4,5} };
424 rrect.setRectRadii(rect, corners);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000425 TestAlignment(&rrect, reporter);
426 }
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000427
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000428 // Test readByteArray
429 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000430 unsigned char data[kArraySize] = { 1, 2, 3 };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000431 TestArraySerialization(data, reporter);
432 }
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000433
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000434 // Test readColorArray
435 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000436 SkColor data[kArraySize] = { SK_ColorBLACK, SK_ColorWHITE, SK_ColorRED };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000437 TestArraySerialization(data, reporter);
438 }
439
440 // Test readIntArray
441 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000442 int32_t data[kArraySize] = { 1, 2, 4, 8 };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000443 TestArraySerialization(data, reporter);
444 }
445
446 // Test readPointArray
447 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000448 SkPoint data[kArraySize] = { {6, 7}, {42, 128} };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000449 TestArraySerialization(data, reporter);
450 }
451
452 // Test readScalarArray
453 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000454 SkScalar data[kArraySize] = { SK_Scalar1, SK_ScalarHalf, SK_ScalarMax };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000455 TestArraySerialization(data, reporter);
commit-bot@chromium.org02512882013-10-31 18:37:50 +0000456 }
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000457
458 // Test invalid deserializations
459 {
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000460 SkImageInfo info = SkImageInfo::MakeN32Premul(kBitmapSize, kBitmapSize);
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000461
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000462 SkBitmap validBitmap;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000463 validBitmap.setInfo(info);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000464
465 // Create a bitmap with a really large height
466 SkBitmap invalidBitmap;
reede5ea5002014-09-03 11:54:58 -0700467 invalidBitmap.setInfo(info.makeWH(info.width(), 1000000000));
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000468
469 // The deserialization should succeed, and the rendering shouldn't crash,
470 // even when the device fails to initialize, due to its size
471 TestBitmapSerialization(validBitmap, invalidBitmap, true, reporter);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000472 }
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000473
474 // Test simple SkPicture serialization
475 {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000476 SkPictureRecorder recorder;
reed84825042014-09-02 12:50:45 -0700477 draw_something(recorder.beginRecording(SkIntToScalar(kBitmapSize),
478 SkIntToScalar(kBitmapSize),
479 NULL, 0));
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000480 SkAutoTUnref<SkPicture> pict(recorder.endRecording());
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000481
482 // Serialize picture
483 SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag);
484 pict->flatten(writer);
485 size_t size = writer.bytesWritten();
commit-bot@chromium.org1e7ee992014-03-14 21:22:22 +0000486 SkAutoTMalloc<unsigned char> data(size);
487 writer.writeToMemory(static_cast<void*>(data.get()));
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000488
489 // Deserialize picture
commit-bot@chromium.org1e7ee992014-03-14 21:22:22 +0000490 SkValidatingReadBuffer reader(static_cast<void*>(data.get()), size);
commit-bot@chromium.orge5eee512014-03-13 16:18:49 +0000491 SkAutoTUnref<SkPicture> readPict(
492 SkPicture::CreateFromBuffer(reader));
493 REPORTER_ASSERT(reporter, NULL != readPict.get());
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000494 }
caseq26337e92014-06-30 12:14:52 -0700495
496 TestPictureTypefaceSerialization(reporter);
commit-bot@chromium.org02512882013-10-31 18:37:50 +0000497}