blob: 3a443f3b9c376bc09e02dad7ea92482758bab133 [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;
266 bitmap.allocN32Pixels(picture.width(), picture.height());
267 SkCanvas canvas(bitmap);
268 picture.draw(&canvas);
269 return bitmap;
270}
271
272static void compare_bitmaps(skiatest::Reporter* reporter,
273 const SkBitmap& b1, const SkBitmap& b2) {
274 REPORTER_ASSERT(reporter, b1.width() == b2.width());
275 REPORTER_ASSERT(reporter, b1.height() == b2.height());
276 SkAutoLockPixels autoLockPixels1(b1);
277 SkAutoLockPixels autoLockPixels2(b2);
278
279 if ((b1.width() != b2.width()) ||
280 (b1.height() != b2.height())) {
281 return;
282 }
283
284 int pixelErrors = 0;
285 for (int y = 0; y < b2.height(); ++y) {
286 for (int x = 0; x < b2.width(); ++x) {
287 if (b1.getColor(x, y) != b2.getColor(x, y))
288 ++pixelErrors;
289 }
290 }
291 REPORTER_ASSERT(reporter, 0 == pixelErrors);
292}
293
294static void TestPictureTypefaceSerialization(skiatest::Reporter* reporter) {
295 // Load typeface form file.
296 // This test cannot run if there is no resource path.
297 SkString resourcePath = GetResourcePath();
298 if (resourcePath.isEmpty()) {
299 SkDebugf("Could not run fontstream test because resourcePath not specified.");
300 return;
301 }
302 SkString filename = SkOSPath::SkPathJoin(resourcePath.c_str(), "test.ttc");
303 SkTypeface* typeface = SkTypeface::CreateFromFile(filename.c_str());
304 if (!typeface) {
305 SkDebugf("Could not run fontstream test because test.ttc not found.");
306 return;
307 }
308
309 // Create a paint with the typeface we loaded.
310 SkPaint paint;
311 paint.setColor(SK_ColorGRAY);
312 paint.setTextSize(SkIntToScalar(30));
313 SkSafeUnref(paint.setTypeface(typeface));
314
315 // Paint some text.
316 SkPictureRecorder recorder;
317 SkIRect canvasRect = SkIRect::MakeWH(kBitmapSize, kBitmapSize);
318 SkCanvas* canvas = recorder.beginRecording(canvasRect.width(), canvasRect.height(), NULL, 0);
319 canvas->drawColor(SK_ColorWHITE);
320 canvas->drawText("A", 1, 24, 32, paint);
321 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
322
323 // Serlialize picture and create its clone from stream.
324 SkDynamicMemoryWStream stream;
325 picture->serialize(&stream);
326 SkAutoTUnref<SkStream> inputStream(stream.detachAsStream());
327 SkAutoTUnref<SkPicture> loadedPicture(SkPicture::CreateFromStream(inputStream.get()));
328
329 // Draw both original and clone picture and compare bitmaps -- they should be identical.
330 SkBitmap origBitmap = draw_picture(*picture);
331 SkBitmap destBitmap = draw_picture(*loadedPicture);
332 compare_bitmaps(reporter, origBitmap, destBitmap);
333}
334
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000335static bool setup_bitmap_for_canvas(SkBitmap* bitmap) {
336 SkImageInfo info = SkImageInfo::Make(
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000337 kBitmapSize, kBitmapSize, kN32_SkColorType, kPremul_SkAlphaType);
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000338 return bitmap->allocPixels(info);
339}
340
341static bool make_checkerboard_bitmap(SkBitmap& bitmap) {
342 bool success = setup_bitmap_for_canvas(&bitmap);
343
344 SkCanvas canvas(bitmap);
345 canvas.clear(0x00000000);
346 SkPaint darkPaint;
347 darkPaint.setColor(0xFF804020);
348 SkPaint lightPaint;
349 lightPaint.setColor(0xFF244484);
350 const int i = kBitmapSize / 8;
351 const SkScalar f = SkIntToScalar(i);
352 for (int y = 0; y < kBitmapSize; y += i) {
353 for (int x = 0; x < kBitmapSize; x += i) {
354 canvas.save();
355 canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
356 canvas.drawRect(SkRect::MakeXYWH(0, 0, f, f), darkPaint);
357 canvas.drawRect(SkRect::MakeXYWH(f, 0, f, f), lightPaint);
358 canvas.drawRect(SkRect::MakeXYWH(0, f, f, f), lightPaint);
359 canvas.drawRect(SkRect::MakeXYWH(f, f, f, f), darkPaint);
360 canvas.restore();
361 }
362 }
363
364 return success;
365}
366
367static bool drawSomething(SkCanvas* canvas) {
368 SkPaint paint;
369 SkBitmap bitmap;
370 bool success = make_checkerboard_bitmap(bitmap);
371
372 canvas->save();
373 canvas->scale(0.5f, 0.5f);
374 canvas->drawBitmap(bitmap, 0, 0, NULL);
375 canvas->restore();
376
377 const char beforeStr[] = "before circle";
378 const char afterStr[] = "after circle";
379
380 paint.setAntiAlias(true);
381
382 paint.setColor(SK_ColorRED);
383 canvas->drawData(beforeStr, sizeof(beforeStr));
384 canvas->drawCircle(SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/3), paint);
385 canvas->drawData(afterStr, sizeof(afterStr));
386 paint.setColor(SK_ColorBLACK);
387 paint.setTextSize(SkIntToScalar(kBitmapSize/3));
388 canvas->drawText("Picture", 7, SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/4), paint);
389
390 return success;
391}
392
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000393DEF_TEST(Serialization, reporter) {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000394 // Test matrix serialization
395 {
396 SkMatrix matrix = SkMatrix::I();
397 TestObjectSerialization(&matrix, reporter);
caseq26337e92014-06-30 12:14:52 -0700398 }
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000399
400 // Test path serialization
401 {
402 SkPath path;
403 TestObjectSerialization(&path, reporter);
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000404 }
sugoi@google.com305f78e2013-11-04 16:18:15 +0000405
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000406 // Test region serialization
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000407 {
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000408 SkRegion region;
409 TestObjectSerialization(&region, reporter);
410 }
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000411
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +0000412 // Test string serialization
413 {
414 SkString string("string");
415 TestObjectSerializationNoAlign<SkString, false>(&string, reporter);
416 TestObjectSerializationNoAlign<SkString, true>(&string, reporter);
417 }
418
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000419 // Test rrect serialization
420 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000421 // SkRRect does not initialize anything.
422 // An uninitialized SkRRect can be serialized,
423 // but will branch on uninitialized data when deserialized.
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000424 SkRRect rrect;
rmistry@google.comd6bab022013-12-02 13:50:38 +0000425 SkRect rect = SkRect::MakeXYWH(1, 2, 20, 30);
426 SkVector corners[4] = { {1, 2}, {2, 3}, {3,4}, {4,5} };
427 rrect.setRectRadii(rect, corners);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000428 TestAlignment(&rrect, reporter);
429 }
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000430
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000431 // Test readByteArray
432 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000433 unsigned char data[kArraySize] = { 1, 2, 3 };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000434 TestArraySerialization(data, reporter);
435 }
sugoi@google.comb48a59a2013-11-04 20:28:23 +0000436
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000437 // Test readColorArray
438 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000439 SkColor data[kArraySize] = { SK_ColorBLACK, SK_ColorWHITE, SK_ColorRED };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000440 TestArraySerialization(data, reporter);
441 }
442
443 // Test readIntArray
444 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000445 int32_t data[kArraySize] = { 1, 2, 4, 8 };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000446 TestArraySerialization(data, reporter);
447 }
448
449 // Test readPointArray
450 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000451 SkPoint data[kArraySize] = { {6, 7}, {42, 128} };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000452 TestArraySerialization(data, reporter);
453 }
454
455 // Test readScalarArray
456 {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000457 SkScalar data[kArraySize] = { SK_Scalar1, SK_ScalarHalf, SK_ScalarMax };
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000458 TestArraySerialization(data, reporter);
commit-bot@chromium.org02512882013-10-31 18:37:50 +0000459 }
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000460
461 // Test invalid deserializations
462 {
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000463 SkImageInfo info = SkImageInfo::MakeN32Premul(kBitmapSize, kBitmapSize);
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000464
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000465 SkBitmap validBitmap;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000466 validBitmap.setInfo(info);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000467
468 // Create a bitmap with a really large height
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000469 info.fHeight = 1000000000;
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000470 SkBitmap invalidBitmap;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000471 invalidBitmap.setInfo(info);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000472
473 // The deserialization should succeed, and the rendering shouldn't crash,
474 // even when the device fails to initialize, due to its size
475 TestBitmapSerialization(validBitmap, invalidBitmap, true, reporter);
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000476 }
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000477
478 // Test simple SkPicture serialization
479 {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000480 SkPictureRecorder recorder;
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000481 bool didDraw = drawSomething(recorder.beginRecording(kBitmapSize, kBitmapSize, NULL, 0));
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000482 REPORTER_ASSERT(reporter, didDraw);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000483 SkAutoTUnref<SkPicture> pict(recorder.endRecording());
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000484
485 // Serialize picture
486 SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag);
487 pict->flatten(writer);
488 size_t size = writer.bytesWritten();
commit-bot@chromium.org1e7ee992014-03-14 21:22:22 +0000489 SkAutoTMalloc<unsigned char> data(size);
490 writer.writeToMemory(static_cast<void*>(data.get()));
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000491
492 // Deserialize picture
commit-bot@chromium.org1e7ee992014-03-14 21:22:22 +0000493 SkValidatingReadBuffer reader(static_cast<void*>(data.get()), size);
commit-bot@chromium.orge5eee512014-03-13 16:18:49 +0000494 SkAutoTUnref<SkPicture> readPict(
495 SkPicture::CreateFromBuffer(reader));
496 REPORTER_ASSERT(reporter, NULL != readPict.get());
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000497 }
caseq26337e92014-06-30 12:14:52 -0700498
499 TestPictureTypefaceSerialization(reporter);
commit-bot@chromium.org02512882013-10-31 18:37:50 +0000500}