| 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 | 7 |
| 8 #include "Test.h" | 8 #include "Test.h" |
| 9 #include "TestClassDef.h" | 9 #include "TestClassDef.h" |
| 10 #include "SkBitmapDevice.h" | 10 #include "SkBitmapDevice.h" |
| 11 #include "SkCanvas.h" | 11 #include "SkCanvas.h" |
| 12 #include "SkColorPriv.h" | 12 #include "SkColorPriv.h" |
| 13 #include "SkData.h" | 13 #include "SkData.h" |
| 14 #include "SkDecodingImageGenerator.h" |
| 14 #include "SkError.h" | 15 #include "SkError.h" |
| 15 #include "SkPaint.h" | 16 #include "SkPaint.h" |
| 16 #include "SkPicture.h" | 17 #include "SkPicture.h" |
| 17 #include "SkPictureUtils.h" | 18 #include "SkPictureUtils.h" |
| 18 #include "SkRandom.h" | 19 #include "SkRandom.h" |
| 19 #include "SkRRect.h" | 20 #include "SkRRect.h" |
| 20 #include "SkShader.h" | 21 #include "SkShader.h" |
| 21 #include "SkStream.h" | 22 #include "SkStream.h" |
| 22 | 23 |
| 23 | 24 |
| (...skipping 306 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 330 SkPicture picture; | 331 SkPicture picture; |
| 331 SkCanvas* recordingCanvas = picture.beginRecording(100, 100); | 332 SkCanvas* recordingCanvas = picture.beginRecording(100, 100); |
| 332 recordingCanvas->drawBitmap(bm, 0, 0); | 333 recordingCanvas->drawBitmap(bm, 0, 0); |
| 333 picture.endRecording(); | 334 picture.endRecording(); |
| 334 | 335 |
| 335 SkCanvas canvas; | 336 SkCanvas canvas; |
| 336 canvas.drawPicture(picture); | 337 canvas.drawPicture(picture); |
| 337 } | 338 } |
| 338 #endif | 339 #endif |
| 339 | 340 |
| 340 #include "SkData.h" | |
| 341 #include "SkImageRef_GlobalPool.h" | |
| 342 // Class to test SkPixelRef::onRefEncodedData, since there are currently no impl
ementations in skia. | |
| 343 class SkDataImageRef : public SkImageRef_GlobalPool { | |
| 344 | |
| 345 public: | |
| 346 SkDataImageRef(SkMemoryStream* stream) | |
| 347 : SkImageRef_GlobalPool(stream, SkBitmap::kNo_Config) { | |
| 348 SkASSERT(stream != NULL); | |
| 349 fData = stream->copyToData(); | |
| 350 this->setImmutable(); | |
| 351 } | |
| 352 | |
| 353 ~SkDataImageRef() { | |
| 354 fData->unref(); | |
| 355 } | |
| 356 | |
| 357 virtual SkData* onRefEncodedData() SK_OVERRIDE { | |
| 358 fData->ref(); | |
| 359 return fData; | |
| 360 } | |
| 361 | |
| 362 private: | |
| 363 SkData* fData; | |
| 364 }; | |
| 365 | |
| 366 #include "SkImageEncoder.h" | 341 #include "SkImageEncoder.h" |
| 367 | 342 |
| 368 static SkData* encode_bitmap_to_data(size_t* offset, const SkBitmap& bm) { | 343 static SkData* encode_bitmap_to_data(size_t* offset, const SkBitmap& bm) { |
| 369 *offset = 0; | 344 *offset = 0; |
| 370 return SkImageEncoder::EncodeData(bm, SkImageEncoder::kPNG_Type, 100); | 345 return SkImageEncoder::EncodeData(bm, SkImageEncoder::kPNG_Type, 100); |
| 371 } | 346 } |
| 372 | 347 |
| 373 static SkData* serialized_picture_from_bitmap(const SkBitmap& bitmap) { | 348 static SkData* serialized_picture_from_bitmap(const SkBitmap& bitmap) { |
| 374 SkPicture picture; | 349 SkPicture picture; |
| 375 SkCanvas* canvas = picture.beginRecording(bitmap.width(), bitmap.height()); | 350 SkCanvas* canvas = picture.beginRecording(bitmap.width(), bitmap.height()); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 397 | 372 |
| 398 static void test_bitmap_with_encoded_data(skiatest::Reporter* reporter) { | 373 static void test_bitmap_with_encoded_data(skiatest::Reporter* reporter) { |
| 399 // Create a bitmap that will be encoded. | 374 // Create a bitmap that will be encoded. |
| 400 SkBitmap original; | 375 SkBitmap original; |
| 401 make_bm(&original, 100, 100, SK_ColorBLUE, true); | 376 make_bm(&original, 100, 100, SK_ColorBLUE, true); |
| 402 SkDynamicMemoryWStream wStream; | 377 SkDynamicMemoryWStream wStream; |
| 403 if (!SkImageEncoder::EncodeStream(&wStream, original, SkImageEncoder::kPNG_T
ype, 100)) { | 378 if (!SkImageEncoder::EncodeStream(&wStream, original, SkImageEncoder::kPNG_T
ype, 100)) { |
| 404 return; | 379 return; |
| 405 } | 380 } |
| 406 SkAutoDataUnref data(wStream.copyToData()); | 381 SkAutoDataUnref data(wStream.copyToData()); |
| 407 SkMemoryStream memStream; | |
| 408 memStream.setData(data); | |
| 409 | 382 |
| 410 // Use the encoded bitmap as the data for an image ref. | |
| 411 SkBitmap bm; | 383 SkBitmap bm; |
| 412 SkAutoTUnref<SkDataImageRef> imageRef(SkNEW_ARGS(SkDataImageRef, (&memStream
))); | 384 bool installSuccess = SkDecodingImageGenerator::Install(data, &bm); |
| 413 imageRef->getInfo(&bm); | 385 REPORTER_ASSERT(reporter, installSuccess); |
| 414 bm.setPixelRef(imageRef); | |
| 415 | 386 |
| 416 // Write both bitmaps to pictures, and ensure that the resulting data stream
s are the same. | 387 // Write both bitmaps to pictures, and ensure that the resulting data stream
s are the same. |
| 417 // Flattening original will follow the old path of performing an encode, whi
le flattening bm | 388 // Flattening original will follow the old path of performing an encode, whi
le flattening bm |
| 418 // will use the already encoded data. | 389 // will use the already encoded data. |
| 419 SkAutoDataUnref picture1(serialized_picture_from_bitmap(original)); | 390 SkAutoDataUnref picture1(serialized_picture_from_bitmap(original)); |
| 420 SkAutoDataUnref picture2(serialized_picture_from_bitmap(bm)); | 391 SkAutoDataUnref picture2(serialized_picture_from_bitmap(bm)); |
| 421 REPORTER_ASSERT(reporter, picture1->equals(picture2)); | 392 REPORTER_ASSERT(reporter, picture1->equals(picture2)); |
| 422 // Now test that a parse error was generated when trying to create a new SkP
icture without | 393 // Now test that a parse error was generated when trying to create a new SkP
icture without |
| 423 // providing a function to decode the bitmap. | 394 // providing a function to decode the bitmap. |
| 424 ErrorContext context; | 395 ErrorContext context; |
| (...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 661 test_bad_bitmap(); | 632 test_bad_bitmap(); |
| 662 #endif | 633 #endif |
| 663 test_peephole(); | 634 test_peephole(); |
| 664 test_gatherpixelrefs(reporter); | 635 test_gatherpixelrefs(reporter); |
| 665 test_bitmap_with_encoded_data(reporter); | 636 test_bitmap_with_encoded_data(reporter); |
| 666 test_clone_empty(reporter); | 637 test_clone_empty(reporter); |
| 667 test_clip_bound_opt(reporter); | 638 test_clip_bound_opt(reporter); |
| 668 test_clip_expansion(reporter); | 639 test_clip_expansion(reporter); |
| 669 test_hierarchical(reporter); | 640 test_hierarchical(reporter); |
| 670 } | 641 } |
| OLD | NEW |