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