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