OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 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 #include "Test.h" | 7 #include "Test.h" |
8 #include "SkCanvas.h" | 8 #include "SkCanvas.h" |
9 #include "SkColorPriv.h" | 9 #include "SkColorPriv.h" |
10 #include "SkData.h" | 10 #include "SkData.h" |
| 11 #include "SkError.h" |
11 #include "SkPaint.h" | 12 #include "SkPaint.h" |
12 #include "SkPicture.h" | 13 #include "SkPicture.h" |
13 #include "SkRandom.h" | 14 #include "SkRandom.h" |
14 #include "SkRRect.h" | 15 #include "SkRRect.h" |
15 #include "SkShader.h" | 16 #include "SkShader.h" |
16 #include "SkStream.h" | 17 #include "SkStream.h" |
17 | 18 |
18 #include "SkPictureUtils.h" | 19 #include "SkPictureUtils.h" |
19 | 20 |
20 static void make_bm(SkBitmap* bm, int w, int h, SkColor color, bool immutable) { | 21 static void make_bm(SkBitmap* bm, int w, int h, SkColor color, bool immutable) { |
(...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
364 | 365 |
365 static SkData* serialized_picture_from_bitmap(const SkBitmap& bitmap) { | 366 static SkData* serialized_picture_from_bitmap(const SkBitmap& bitmap) { |
366 SkPicture picture; | 367 SkPicture picture; |
367 SkCanvas* canvas = picture.beginRecording(bitmap.width(), bitmap.height()); | 368 SkCanvas* canvas = picture.beginRecording(bitmap.width(), bitmap.height()); |
368 canvas->drawBitmap(bitmap, 0, 0); | 369 canvas->drawBitmap(bitmap, 0, 0); |
369 SkDynamicMemoryWStream wStream; | 370 SkDynamicMemoryWStream wStream; |
370 picture.serialize(&wStream, &PNGEncodeBitmapToStream); | 371 picture.serialize(&wStream, &PNGEncodeBitmapToStream); |
371 return wStream.copyToData(); | 372 return wStream.copyToData(); |
372 } | 373 } |
373 | 374 |
| 375 struct ErrorContext { |
| 376 int fErrors; |
| 377 skiatest::Reporter* fReporter; |
| 378 }; |
| 379 |
| 380 static void assert_one_parse_error_cb(SkError error, void* context) { |
| 381 ErrorContext* errorContext = static_cast<ErrorContext*>(context); |
| 382 errorContext->fErrors++; |
| 383 // This test only expects one error, and that is a kParseError. If there are
others, |
| 384 // there is some unknown problem. |
| 385 REPORTER_ASSERT_MESSAGE(errorContext->fReporter, 1 == errorContext->fErrors, |
| 386 "This threw more errors than expected."); |
| 387 REPORTER_ASSERT_MESSAGE(errorContext->fReporter, kParseError_SkError == erro
r, |
| 388 SkGetLastErrorString()); |
| 389 } |
| 390 |
374 static void test_bitmap_with_encoded_data(skiatest::Reporter* reporter) { | 391 static void test_bitmap_with_encoded_data(skiatest::Reporter* reporter) { |
375 // Create a bitmap that will be encoded. | 392 // Create a bitmap that will be encoded. |
376 SkBitmap original; | 393 SkBitmap original; |
377 make_bm(&original, 100, 100, SK_ColorBLUE, true); | 394 make_bm(&original, 100, 100, SK_ColorBLUE, true); |
378 SkDynamicMemoryWStream wStream; | 395 SkDynamicMemoryWStream wStream; |
379 if (!SkImageEncoder::EncodeStream(&wStream, original, SkImageEncoder::kPNG_T
ype, 100)) { | 396 if (!SkImageEncoder::EncodeStream(&wStream, original, SkImageEncoder::kPNG_T
ype, 100)) { |
380 return; | 397 return; |
381 } | 398 } |
382 SkAutoDataUnref data(wStream.copyToData()); | 399 SkAutoDataUnref data(wStream.copyToData()); |
383 SkMemoryStream memStream; | 400 SkMemoryStream memStream; |
384 memStream.setData(data); | 401 memStream.setData(data); |
385 | 402 |
386 // Use the encoded bitmap as the data for an image ref. | 403 // Use the encoded bitmap as the data for an image ref. |
387 SkBitmap bm; | 404 SkBitmap bm; |
388 SkAutoTUnref<SkDataImageRef> imageRef(SkNEW_ARGS(SkDataImageRef, (&memStream
))); | 405 SkAutoTUnref<SkDataImageRef> imageRef(SkNEW_ARGS(SkDataImageRef, (&memStream
))); |
389 imageRef->getInfo(&bm); | 406 imageRef->getInfo(&bm); |
390 bm.setPixelRef(imageRef); | 407 bm.setPixelRef(imageRef); |
391 | 408 |
392 // Write both bitmaps to pictures, and ensure that the resulting data stream
s are the same. | 409 // Write both bitmaps to pictures, and ensure that the resulting data stream
s are the same. |
393 // Flattening original will follow the old path of performing an encode, whi
le flattening bm | 410 // Flattening original will follow the old path of performing an encode, whi
le flattening bm |
394 // will use the already encoded data. | 411 // will use the already encoded data. |
395 SkAutoDataUnref picture1(serialized_picture_from_bitmap(original)); | 412 SkAutoDataUnref picture1(serialized_picture_from_bitmap(original)); |
396 SkAutoDataUnref picture2(serialized_picture_from_bitmap(bm)); | 413 SkAutoDataUnref picture2(serialized_picture_from_bitmap(bm)); |
397 REPORTER_ASSERT(reporter, picture1->equals(picture2)); | 414 REPORTER_ASSERT(reporter, picture1->equals(picture2)); |
| 415 // Now test that a parse error was generated when trying to create a new SkP
icture without |
| 416 // providing a function to decode the bitmap. |
| 417 ErrorContext context; |
| 418 context.fErrors = 0; |
| 419 context.fReporter = reporter; |
| 420 SkSetErrorCallback(assert_one_parse_error_cb, &context); |
| 421 SkMemoryStream pictureStream(picture1); |
| 422 bool success; |
| 423 SkClearLastError(); |
| 424 SkPicture pictureFromStream(&pictureStream, &success, NULL); |
| 425 REPORTER_ASSERT(reporter, success); |
| 426 SkClearLastError(); |
| 427 SkSetErrorCallback(NULL, NULL); |
398 } | 428 } |
399 | 429 |
400 static void test_clone_empty(skiatest::Reporter* reporter) { | 430 static void test_clone_empty(skiatest::Reporter* reporter) { |
401 // This is a regression test for crbug.com/172062 | 431 // This is a regression test for crbug.com/172062 |
402 // Before the fix, we used to crash accessing a null pointer when we | 432 // Before the fix, we used to crash accessing a null pointer when we |
403 // had a picture with no paints. This test passes by not crashing. | 433 // had a picture with no paints. This test passes by not crashing. |
404 { | 434 { |
405 SkPicture picture; | 435 SkPicture picture; |
406 picture.beginRecording(1, 1); | 436 picture.beginRecording(1, 1); |
407 picture.endRecording(); | 437 picture.endRecording(); |
(...skipping 19 matching lines...) Expand all Loading... |
427 test_bad_bitmap(); | 457 test_bad_bitmap(); |
428 #endif | 458 #endif |
429 test_peephole(); | 459 test_peephole(); |
430 test_gatherpixelrefs(reporter); | 460 test_gatherpixelrefs(reporter); |
431 test_bitmap_with_encoded_data(reporter); | 461 test_bitmap_with_encoded_data(reporter); |
432 test_clone_empty(reporter); | 462 test_clone_empty(reporter); |
433 } | 463 } |
434 | 464 |
435 #include "TestClassDef.h" | 465 #include "TestClassDef.h" |
436 DEFINE_TESTCLASS("Pictures", PictureTestClass, TestPicture) | 466 DEFINE_TESTCLASS("Pictures", PictureTestClass, TestPicture) |
OLD | NEW |