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