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 "Resources.h" |
8 #include "SkBitmapSource.h" | 9 #include "SkBitmapSource.h" |
9 #include "SkCanvas.h" | 10 #include "SkCanvas.h" |
10 #include "SkMallocPixelRef.h" | 11 #include "SkMallocPixelRef.h" |
| 12 #include "SkOSFile.h" |
11 #include "SkPictureRecorder.h" | 13 #include "SkPictureRecorder.h" |
12 #include "SkTemplates.h" | 14 #include "SkTemplates.h" |
| 15 #include "SkTypeface.h" |
13 #include "SkWriteBuffer.h" | 16 #include "SkWriteBuffer.h" |
14 #include "SkValidatingReadBuffer.h" | 17 #include "SkValidatingReadBuffer.h" |
15 #include "SkXfermodeImageFilter.h" | 18 #include "SkXfermodeImageFilter.h" |
16 #include "Test.h" | 19 #include "Test.h" |
17 | 20 |
18 static const uint32_t kArraySize = 64; | 21 static const uint32_t kArraySize = 64; |
19 static const int kBitmapSize = 256; | 22 static const int kBitmapSize = 256; |
20 | 23 |
21 template<typename T> | 24 template<typename T> |
22 static void TestAlignment(T* testObj, skiatest::Reporter* reporter) { | 25 static void TestAlignment(T* testObj, skiatest::Reporter* reporter) { |
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
251 bitmap.allocN32Pixels(24, 24); | 254 bitmap.allocN32Pixels(24, 24); |
252 SkCanvas canvas(bitmap); | 255 SkCanvas canvas(bitmap); |
253 canvas.clear(0x00000000); | 256 canvas.clear(0x00000000); |
254 SkPaint paint; | 257 SkPaint paint; |
255 paint.setImageFilter(deserializedFilter); | 258 paint.setImageFilter(deserializedFilter); |
256 canvas.clipRect(SkRect::MakeXYWH(0, 0, SkIntToScalar(24), SkIntToScalar(
24))); | 259 canvas.clipRect(SkRect::MakeXYWH(0, 0, SkIntToScalar(24), SkIntToScalar(
24))); |
257 canvas.drawBitmap(bitmap, 0, 0, &paint); | 260 canvas.drawBitmap(bitmap, 0, 0, &paint); |
258 } | 261 } |
259 } | 262 } |
260 | 263 |
| 264 static 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 |
| 272 static 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 |
| 294 static 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 specifi
ed."); |
| 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.he
ight(), 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(inputStrea
m.get())); |
| 328 |
| 329 // Draw both original and clone picture and compare bitmaps -- they should b
e identical. |
| 330 SkBitmap origBitmap = draw_picture(*picture); |
| 331 SkBitmap destBitmap = draw_picture(*loadedPicture); |
| 332 compare_bitmaps(reporter, origBitmap, destBitmap); |
| 333 } |
| 334 |
261 static bool setup_bitmap_for_canvas(SkBitmap* bitmap) { | 335 static bool setup_bitmap_for_canvas(SkBitmap* bitmap) { |
262 SkImageInfo info = SkImageInfo::Make( | 336 SkImageInfo info = SkImageInfo::Make( |
263 kBitmapSize, kBitmapSize, kN32_SkColorType, kPremul_SkAlphaType); | 337 kBitmapSize, kBitmapSize, kN32_SkColorType, kPremul_SkAlphaType); |
264 return bitmap->allocPixels(info); | 338 return bitmap->allocPixels(info); |
265 } | 339 } |
266 | 340 |
267 static bool make_checkerboard_bitmap(SkBitmap& bitmap) { | 341 static bool make_checkerboard_bitmap(SkBitmap& bitmap) { |
268 bool success = setup_bitmap_for_canvas(&bitmap); | 342 bool success = setup_bitmap_for_canvas(&bitmap); |
269 | 343 |
270 SkCanvas canvas(bitmap); | 344 SkCanvas canvas(bitmap); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
314 canvas->drawText("Picture", 7, SkIntToScalar(kBitmapSize/2), SkIntToScalar(k
BitmapSize/4), paint); | 388 canvas->drawText("Picture", 7, SkIntToScalar(kBitmapSize/2), SkIntToScalar(k
BitmapSize/4), paint); |
315 | 389 |
316 return success; | 390 return success; |
317 } | 391 } |
318 | 392 |
319 DEF_TEST(Serialization, reporter) { | 393 DEF_TEST(Serialization, reporter) { |
320 // Test matrix serialization | 394 // Test matrix serialization |
321 { | 395 { |
322 SkMatrix matrix = SkMatrix::I(); | 396 SkMatrix matrix = SkMatrix::I(); |
323 TestObjectSerialization(&matrix, reporter); | 397 TestObjectSerialization(&matrix, reporter); |
324 } | 398 } |
325 | 399 |
326 // Test path serialization | 400 // Test path serialization |
327 { | 401 { |
328 SkPath path; | 402 SkPath path; |
329 TestObjectSerialization(&path, reporter); | 403 TestObjectSerialization(&path, reporter); |
330 } | 404 } |
331 | 405 |
332 // Test region serialization | 406 // Test region serialization |
333 { | 407 { |
334 SkRegion region; | 408 SkRegion region; |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
414 size_t size = writer.bytesWritten(); | 488 size_t size = writer.bytesWritten(); |
415 SkAutoTMalloc<unsigned char> data(size); | 489 SkAutoTMalloc<unsigned char> data(size); |
416 writer.writeToMemory(static_cast<void*>(data.get())); | 490 writer.writeToMemory(static_cast<void*>(data.get())); |
417 | 491 |
418 // Deserialize picture | 492 // Deserialize picture |
419 SkValidatingReadBuffer reader(static_cast<void*>(data.get()), size); | 493 SkValidatingReadBuffer reader(static_cast<void*>(data.get()), size); |
420 SkAutoTUnref<SkPicture> readPict( | 494 SkAutoTUnref<SkPicture> readPict( |
421 SkPicture::CreateFromBuffer(reader)); | 495 SkPicture::CreateFromBuffer(reader)); |
422 REPORTER_ASSERT(reporter, NULL != readPict.get()); | 496 REPORTER_ASSERT(reporter, NULL != readPict.get()); |
423 } | 497 } |
| 498 |
| 499 TestPictureTypefaceSerialization(reporter); |
424 } | 500 } |
OLD | NEW |