| Index: tests/BitmapFactoryTest.cpp
|
| diff --git a/tests/BitmapFactoryTest.cpp b/tests/BitmapFactoryTest.cpp
|
| index 8bfbaf1c4f238170e45627c970be8fd4571cf16b..eca2c8e33b0396f24a7f7b6b9486e0ab2a8f9a5c 100644
|
| --- a/tests/BitmapFactoryTest.cpp
|
| +++ b/tests/BitmapFactoryTest.cpp
|
| @@ -25,9 +25,14 @@
|
| #include "SkAshmemImageCache.h"
|
| #endif
|
|
|
| +#ifdef SK_BUILD_FOR_MAC
|
| +#include "SkMacImageCache.h"
|
| +#endif
|
| +
|
| static SkBitmap* create_bitmap() {
|
| SkBitmap* bm = SkNEW(SkBitmap);
|
| - const int W = 100, H = 100;
|
| + // Use a large bitmap.
|
| + const int W = 1000, H = 1000;
|
| bm->setConfig(SkBitmap::kARGB_8888_Config, W, H);
|
| bm->allocPixels();
|
| bm->eraseColor(SK_ColorBLACK);
|
| @@ -52,7 +57,53 @@ static void assert_bounds_equal(skiatest::Reporter* reporter, const SkBitmap& bm
|
| REPORTER_ASSERT(reporter, bm1.height() == bm2.height());
|
| }
|
|
|
| -static void test_cache(skiatest::Reporter* reporter, SkImageCache* cache, SkData* encodedData,
|
| +static void test_cache(skiatest::Reporter* reporter, SkImageCache* cache) {
|
| + // Test the cache directly:
|
| + cache->purgeAllCaches();
|
| + intptr_t ID = SkImageCache::UNINITIALIZED_ID;
|
| + const size_t size = 1000;
|
| + char buffer[size];
|
| + sk_bzero((void*) buffer, size);
|
| + void* memory = cache->allocAndPinCache(size, &ID);
|
| + if (memory != NULL) {
|
| + memcpy(memory, (void*)buffer, size);
|
| + REPORTER_ASSERT(reporter, cache->getPinStatus(ID) == SkImageCache::kPinned_PinStatus);
|
| + cache->releaseCache(ID);
|
| + REPORTER_ASSERT(reporter, cache->getPinStatus(ID) != SkImageCache::kPinned_PinStatus);
|
| + SkImageCache::PurgeStatus purgeStatus;
|
| + memory = cache->pinCache(ID, &purgeStatus);
|
| + if (memory != NULL) {
|
| + REPORTER_ASSERT(reporter, cache->getPinStatus(ID)
|
| + == SkImageCache::kPinned_PinStatus);
|
| + if (SkImageCache::kNotPurged_PurgeStatus == purgeStatus) {
|
| + REPORTER_ASSERT(reporter, !memcmp(memory, (void*) buffer, size));
|
| + }
|
| + cache->releaseCache(ID);
|
| + REPORTER_ASSERT(reporter, cache->getPinStatus(ID)
|
| + != SkImageCache::kPinned_PinStatus);
|
| + cache->purgeAllCaches();
|
| + REPORTER_ASSERT(reporter, cache->getPinStatus(ID)
|
| + != SkImageCache::kPinned_PinStatus);
|
| + memory = cache->pinCache(ID, &purgeStatus);
|
| + if (memory != NULL) {
|
| + // Since the cache was thrown away, and ID was not pinned, it should have
|
| + // been purged.
|
| + REPORTER_ASSERT(reporter, SkImageCache::kPurged_PurgeStatus == purgeStatus);
|
| + cache->releaseCache(ID);
|
| + REPORTER_ASSERT(reporter, cache->getPinStatus(ID)
|
| + != SkImageCache::kPinned_PinStatus);
|
| + cache->throwAwayCache(ID);
|
| + REPORTER_ASSERT(reporter, cache->getPinStatus(ID)
|
| + == SkImageCache::kThrownAway_PinStatus);
|
| + } else {
|
| + REPORTER_ASSERT(reporter, cache->getPinStatus(ID)
|
| + == SkImageCache::kThrownAway_PinStatus);
|
| + }
|
| + }
|
| + }
|
| +}
|
| +
|
| +static void test_factory(skiatest::Reporter* reporter, SkImageCache* cache, SkData* encodedData,
|
| const SkBitmap& origBitmap) {
|
| SkBitmapFactory factory(&SkImageDecoder::DecodeMemoryToTarget);
|
| factory.setImageCache(cache);
|
| @@ -72,41 +123,80 @@ static void test_cache(skiatest::Reporter* reporter, SkImageCache* cache, SkData
|
| // Lazy decoding
|
| REPORTER_ASSERT(reporter, !bitmapFromFactory->readyToDraw());
|
| SkLazyPixelRef* lazyRef = static_cast<SkLazyPixelRef*>(pixelRef);
|
| - int32_t cacheID = lazyRef->getCacheId();
|
| - REPORTER_ASSERT(reporter, cache->getCacheStatus(cacheID)
|
| - != SkImageCache::kPinned_CacheStatus);
|
| + intptr_t cacheID = lazyRef->getCacheId();
|
| + REPORTER_ASSERT(reporter, cache->getPinStatus(cacheID)
|
| + != SkImageCache::kPinned_PinStatus);
|
| {
|
| SkAutoLockPixels alp(*bitmapFromFactory.get());
|
| REPORTER_ASSERT(reporter, bitmapFromFactory->readyToDraw());
|
| cacheID = lazyRef->getCacheId();
|
| - REPORTER_ASSERT(reporter, cache->getCacheStatus(cacheID)
|
| - == SkImageCache::kPinned_CacheStatus);
|
| + REPORTER_ASSERT(reporter, cache->getPinStatus(cacheID)
|
| + == SkImageCache::kPinned_PinStatus);
|
| }
|
| REPORTER_ASSERT(reporter, !bitmapFromFactory->readyToDraw());
|
| - REPORTER_ASSERT(reporter, cache->getCacheStatus(cacheID)
|
| - != SkImageCache::kPinned_CacheStatus);
|
| + REPORTER_ASSERT(reporter, cache->getPinStatus(cacheID)
|
| + != SkImageCache::kPinned_PinStatus);
|
| bitmapFromFactory.free();
|
| - REPORTER_ASSERT(reporter, cache->getCacheStatus(cacheID)
|
| - == SkImageCache::kThrownAway_CacheStatus);
|
| + REPORTER_ASSERT(reporter, cache->getPinStatus(cacheID)
|
| + == SkImageCache::kThrownAway_PinStatus);
|
| }
|
| }
|
|
|
| +class ImageCacheHolder : public SkNoncopyable {
|
| +
|
| +public:
|
| + ~ImageCacheHolder() {
|
| + fCaches.safeUnrefAll();
|
| + }
|
| +
|
| + void addImageCache(SkImageCache* cache) {
|
| + SkSafeRef(cache);
|
| + *fCaches.append() = cache;
|
| + }
|
| +
|
| + int count() const { return fCaches.count(); }
|
| +
|
| + SkImageCache* getAt(int i) {
|
| + if (i < 0 || i > fCaches.count()) {
|
| + return NULL;
|
| + }
|
| + return fCaches.getAt(i);
|
| + }
|
| +
|
| +private:
|
| + SkTDArray<SkImageCache*> fCaches;
|
| +};
|
| +
|
| static void TestBitmapFactory(skiatest::Reporter* reporter) {
|
| SkAutoTDelete<SkBitmap> bitmap(create_bitmap());
|
| SkASSERT(bitmap.get() != NULL);
|
|
|
| SkAutoDataUnref encodedBitmap(create_data_from_bitmap(*bitmap.get()));
|
| - if (encodedBitmap.get() == NULL) {
|
| - // Encoding failed.
|
| - return;
|
| - }
|
| + bool encodeSucceeded = encodedBitmap.get() != NULL;
|
| + SkASSERT(encodeSucceeded);
|
| +
|
| + ImageCacheHolder cacheHolder;
|
|
|
| SkAutoTUnref<SkLruImageCache> lruCache(SkNEW_ARGS(SkLruImageCache, (1024 * 1024)));
|
| - test_cache(reporter, lruCache, encodedBitmap, *bitmap.get());
|
| - test_cache(reporter, NULL, encodedBitmap, *bitmap.get());
|
| + cacheHolder.addImageCache(lruCache);
|
| +
|
| + cacheHolder.addImageCache(NULL);
|
| +
|
| #ifdef SK_BUILD_FOR_ANDROID
|
| - test_cache(reporter, SkAshmemImageCache::GetAshmemImageCache(), encodedBitmap, *bitmap.get());
|
| + cacheHolder.addImageCache(SkAshmemImageCache::GetAshmemImageCache());
|
| +#elif defined(SK_BUILD_FOR_MAC)
|
| + cacheHolder.addImageCache(SkMacImageCache::GetMacImageCache());
|
| #endif
|
| +
|
| + for (int i = 0; i < cacheHolder.count(); i++) {
|
| + SkImageCache* cache = cacheHolder.getAt(i);
|
| + if (cache != NULL) {
|
| + test_cache(reporter, cache);
|
| + }
|
| + if (encodeSucceeded) {
|
| + test_factory(reporter, cache, encodedBitmap, *bitmap.get());
|
| + }
|
| + }
|
| }
|
|
|
| #include "TestClassDef.h"
|
|
|