scroggo@google.com | 9f686f3 | 2012-11-29 21:05:37 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame^] | 8 | #ifdef SK_DEBUG |
| 9 | |
scroggo@google.com | 9f686f3 | 2012-11-29 21:05:37 +0000 | [diff] [blame] | 10 | #include "SkBitmap.h" |
| 11 | #include "SkBitmapFactory.h" |
| 12 | #include "SkCanvas.h" |
| 13 | #include "SkColor.h" |
| 14 | #include "SkData.h" |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame^] | 15 | #include "SkImageDecoder.h" |
scroggo@google.com | 9f686f3 | 2012-11-29 21:05:37 +0000 | [diff] [blame] | 16 | #include "SkImageEncoder.h" |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame^] | 17 | #include "SkLazyPixelRef.h" |
| 18 | #include "SkLruImageCache.h" |
scroggo@google.com | 9f686f3 | 2012-11-29 21:05:37 +0000 | [diff] [blame] | 19 | #include "SkPaint.h" |
| 20 | #include "SkStream.h" |
| 21 | #include "SkTemplates.h" |
| 22 | #include "Test.h" |
| 23 | |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame^] | 24 | #ifdef SK_BUILD_FOR_ANDROID |
| 25 | #include "SkAshmemImageCache.h" |
| 26 | #endif |
| 27 | |
scroggo@google.com | 9f686f3 | 2012-11-29 21:05:37 +0000 | [diff] [blame] | 28 | static SkBitmap* create_bitmap() { |
| 29 | SkBitmap* bm = SkNEW(SkBitmap); |
| 30 | const int W = 100, H = 100; |
| 31 | bm->setConfig(SkBitmap::kARGB_8888_Config, W, H); |
| 32 | bm->allocPixels(); |
| 33 | bm->eraseColor(SK_ColorBLACK); |
| 34 | SkCanvas canvas(*bm); |
| 35 | SkPaint paint; |
| 36 | paint.setColor(SK_ColorBLUE); |
| 37 | canvas.drawRectCoords(0, 0, SkIntToScalar(W/2), SkIntToScalar(H/2), paint); |
| 38 | return bm; |
| 39 | } |
| 40 | |
| 41 | static SkData* create_data_from_bitmap(const SkBitmap& bm) { |
| 42 | SkDynamicMemoryWStream stream; |
| 43 | if (SkImageEncoder::EncodeStream(&stream, bm, SkImageEncoder::kPNG_Type, 100)) { |
| 44 | return stream.copyToData(); |
| 45 | } |
| 46 | return NULL; |
| 47 | } |
| 48 | |
| 49 | static void assert_bounds_equal(skiatest::Reporter* reporter, const SkBitmap& bm1, |
| 50 | const SkBitmap& bm2) { |
| 51 | REPORTER_ASSERT(reporter, bm1.width() == bm2.width()); |
| 52 | REPORTER_ASSERT(reporter, bm1.height() == bm2.height()); |
| 53 | } |
| 54 | |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame^] | 55 | static void test_cache(skiatest::Reporter* reporter, SkImageCache* cache, SkData* encodedData, |
| 56 | const SkBitmap& origBitmap) { |
| 57 | SkBitmapFactory factory(&SkImageDecoder::DecodeMemoryToTarget); |
| 58 | factory.setImageCache(cache); |
| 59 | SkAutoTDelete<SkBitmap> bitmapFromFactory(SkNEW(SkBitmap)); |
| 60 | bool success = factory.installPixelRef(encodedData, bitmapFromFactory.get()); |
| 61 | // This assumes that if the encoder worked, the decoder should also work, so the above call |
| 62 | // should not fail. |
| 63 | REPORTER_ASSERT(reporter, success); |
| 64 | assert_bounds_equal(reporter, origBitmap, *bitmapFromFactory.get()); |
| 65 | |
| 66 | SkPixelRef* pixelRef = bitmapFromFactory->pixelRef(); |
| 67 | REPORTER_ASSERT(reporter, pixelRef != NULL); |
| 68 | if (NULL == cache) { |
| 69 | // This assumes that installPixelRef called lockPixels. |
| 70 | REPORTER_ASSERT(reporter, bitmapFromFactory->readyToDraw()); |
| 71 | } else { |
| 72 | // Lazy decoding |
| 73 | REPORTER_ASSERT(reporter, !bitmapFromFactory->readyToDraw()); |
| 74 | SkLazyPixelRef* lazyRef = static_cast<SkLazyPixelRef*>(pixelRef); |
| 75 | int32_t cacheID = lazyRef->getCacheId(); |
| 76 | REPORTER_ASSERT(reporter, cache->getCacheStatus(cacheID) |
| 77 | != SkImageCache::kPinned_CacheStatus); |
| 78 | { |
| 79 | SkAutoLockPixels alp(*bitmapFromFactory.get()); |
| 80 | REPORTER_ASSERT(reporter, bitmapFromFactory->readyToDraw()); |
| 81 | cacheID = lazyRef->getCacheId(); |
| 82 | REPORTER_ASSERT(reporter, cache->getCacheStatus(cacheID) |
| 83 | == SkImageCache::kPinned_CacheStatus); |
| 84 | } |
| 85 | REPORTER_ASSERT(reporter, !bitmapFromFactory->readyToDraw()); |
| 86 | REPORTER_ASSERT(reporter, cache->getCacheStatus(cacheID) |
| 87 | != SkImageCache::kPinned_CacheStatus); |
| 88 | bitmapFromFactory.free(); |
| 89 | REPORTER_ASSERT(reporter, cache->getCacheStatus(cacheID) |
| 90 | == SkImageCache::kThrownAway_CacheStatus); |
| 91 | } |
| 92 | } |
| 93 | |
scroggo@google.com | 9f686f3 | 2012-11-29 21:05:37 +0000 | [diff] [blame] | 94 | static void TestBitmapFactory(skiatest::Reporter* reporter) { |
| 95 | SkAutoTDelete<SkBitmap> bitmap(create_bitmap()); |
| 96 | SkASSERT(bitmap.get() != NULL); |
| 97 | |
| 98 | SkAutoDataUnref encodedBitmap(create_data_from_bitmap(*bitmap.get())); |
| 99 | if (encodedBitmap.get() == NULL) { |
| 100 | // Encoding failed. |
| 101 | return; |
| 102 | } |
| 103 | |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame^] | 104 | SkAutoTUnref<SkLruImageCache> lruCache(SkNEW_ARGS(SkLruImageCache, (1024 * 1024))); |
| 105 | test_cache(reporter, lruCache, encodedBitmap, *bitmap.get()); |
| 106 | test_cache(reporter, NULL, encodedBitmap, *bitmap.get()); |
| 107 | #ifdef SK_BUILD_FOR_ANDROID |
| 108 | test_cache(reporter, SkAshmemImageCache::GetAshmemImageCache(), encodedBitmap, *bitmap.get()); |
| 109 | #endif |
scroggo@google.com | 9f686f3 | 2012-11-29 21:05:37 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | #include "TestClassDef.h" |
| 113 | DEFINE_TESTCLASS("BitmapFactory", TestBitmapFactoryClass, TestBitmapFactory) |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame^] | 114 | |
| 115 | #endif // SK_DEBUG |