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 static void error_callback(SkError, void*) { | |
376 // Do nothing. | |
377 } | |
378 | |
374 static void test_bitmap_with_encoded_data(skiatest::Reporter* reporter) { | 379 static void test_bitmap_with_encoded_data(skiatest::Reporter* reporter) { |
375 // Create a bitmap that will be encoded. | 380 // Create a bitmap that will be encoded. |
376 SkBitmap original; | 381 SkBitmap original; |
377 make_bm(&original, 100, 100, SK_ColorBLUE, true); | 382 make_bm(&original, 100, 100, SK_ColorBLUE, true); |
378 SkDynamicMemoryWStream wStream; | 383 SkDynamicMemoryWStream wStream; |
379 if (!SkImageEncoder::EncodeStream(&wStream, original, SkImageEncoder::kPNG_T ype, 100)) { | 384 if (!SkImageEncoder::EncodeStream(&wStream, original, SkImageEncoder::kPNG_T ype, 100)) { |
380 return; | 385 return; |
381 } | 386 } |
382 SkAutoDataUnref data(wStream.copyToData()); | 387 SkAutoDataUnref data(wStream.copyToData()); |
383 SkMemoryStream memStream; | 388 SkMemoryStream memStream; |
384 memStream.setData(data); | 389 memStream.setData(data); |
385 | 390 |
386 // Use the encoded bitmap as the data for an image ref. | 391 // Use the encoded bitmap as the data for an image ref. |
387 SkBitmap bm; | 392 SkBitmap bm; |
388 SkAutoTUnref<SkDataImageRef> imageRef(SkNEW_ARGS(SkDataImageRef, (&memStream ))); | 393 SkAutoTUnref<SkDataImageRef> imageRef(SkNEW_ARGS(SkDataImageRef, (&memStream ))); |
389 imageRef->getInfo(&bm); | 394 imageRef->getInfo(&bm); |
390 bm.setPixelRef(imageRef); | 395 bm.setPixelRef(imageRef); |
391 | 396 |
392 // Write both bitmaps to pictures, and ensure that the resulting data stream s are the same. | 397 // 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 | 398 // Flattening original will follow the old path of performing an encode, whi le flattening bm |
394 // will use the already encoded data. | 399 // will use the already encoded data. |
395 SkAutoDataUnref picture1(serialized_picture_from_bitmap(original)); | 400 SkAutoDataUnref picture1(serialized_picture_from_bitmap(original)); |
396 SkAutoDataUnref picture2(serialized_picture_from_bitmap(bm)); | 401 SkAutoDataUnref picture2(serialized_picture_from_bitmap(bm)); |
397 REPORTER_ASSERT(reporter, picture1->equals(picture2)); | 402 REPORTER_ASSERT(reporter, picture1->equals(picture2)); |
403 // Now test that a parse error was generated when trying to create a new SkP icture without | |
404 // providing a function to decode the bitmap. | |
405 SkSetErrorCallback(error_callback, NULL); | |
humper
2013/04/25 17:07:08
Is the idea here to silence the built-in default c
| |
406 SkMemoryStream pictureStream(picture1); | |
407 bool success; | |
408 SkClearLastError(); | |
409 SkPicture pictureFromStream(&pictureStream, &success, NULL); | |
410 REPORTER_ASSERT(reporter, success); | |
411 REPORTER_ASSERT(reporter, SkGetLastError() == kParseError_SkError); | |
412 SkClearLastError(); | |
398 } | 413 } |
399 | 414 |
400 static void test_clone_empty(skiatest::Reporter* reporter) { | 415 static void test_clone_empty(skiatest::Reporter* reporter) { |
401 // This is a regression test for crbug.com/172062 | 416 // This is a regression test for crbug.com/172062 |
402 // Before the fix, we used to crash accessing a null pointer when we | 417 // 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. | 418 // had a picture with no paints. This test passes by not crashing. |
404 { | 419 { |
405 SkPicture picture; | 420 SkPicture picture; |
406 picture.beginRecording(1, 1); | 421 picture.beginRecording(1, 1); |
407 picture.endRecording(); | 422 picture.endRecording(); |
(...skipping 19 matching lines...) Expand all Loading... | |
427 test_bad_bitmap(); | 442 test_bad_bitmap(); |
428 #endif | 443 #endif |
429 test_peephole(); | 444 test_peephole(); |
430 test_gatherpixelrefs(reporter); | 445 test_gatherpixelrefs(reporter); |
431 test_bitmap_with_encoded_data(reporter); | 446 test_bitmap_with_encoded_data(reporter); |
432 test_clone_empty(reporter); | 447 test_clone_empty(reporter); |
433 } | 448 } |
434 | 449 |
435 #include "TestClassDef.h" | 450 #include "TestClassDef.h" |
436 DEFINE_TESTCLASS("Pictures", PictureTestClass, TestPicture) | 451 DEFINE_TESTCLASS("Pictures", PictureTestClass, TestPicture) |
OLD | NEW |