aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/SkResourceCacheTest.cpp
diff options
context:
space:
mode:
authorGravatar aleksandar.stojiljkovic <aleksandar.stojiljkovic@intel.com>2015-11-10 04:55:15 -0800
committerGravatar Commit bot <commit-bot@chromium.org>2015-11-10 04:55:15 -0800
commit07e2692da3971960f8ae8adcd78ccb4bae7ab446 (patch)
tree7f109e777adb635d8548e8f2e9a591049255a79e /tests/SkResourceCacheTest.cpp
parentd9ffaed6d21f85c717117904da26a2bac2295607 (diff)
SkResourceCache::GetAllocator() index8 and other color types handling
Unit tests added to check all color types in SkOneShotDiscardablePixelref SkResourceCacheTest seems to have been allways using default Heap Allocator. Fixed so that it uses private (not global that is) SkDiscardableMemory. BUG=skia:4355 Review URL: https://codereview.chromium.org/1426753006
Diffstat (limited to 'tests/SkResourceCacheTest.cpp')
-rw-r--r--tests/SkResourceCacheTest.cpp120
1 files changed, 80 insertions, 40 deletions
diff --git a/tests/SkResourceCacheTest.cpp b/tests/SkResourceCacheTest.cpp
index 6e8ae40986..f9a0e6d505 100644
--- a/tests/SkResourceCacheTest.cpp
+++ b/tests/SkResourceCacheTest.cpp
@@ -14,11 +14,18 @@
#include "SkPictureRecorder.h"
#include "SkResourceCache.h"
#include "SkSurface.h"
+#include "SkTypes.h"
////////////////////////////////////////////////////////////////////////////////////////
static void make_bitmap(SkBitmap* bitmap, const SkImageInfo& info, SkBitmap::Allocator* allocator) {
- if (allocator) {
+ if (info.colorType() == kIndex_8_SkColorType) {
+ bitmap->setInfo(info);
+ SkPMColor ctStorage[256];
+ memset(ctStorage, 0xFF, sizeof(ctStorage)); // init with opaque-white for the moment
+ SkAutoTUnref<SkColorTable> ctable(new SkColorTable(ctStorage, 256));
+ bitmap->allocPixels(allocator, ctable);
+ } else if (allocator) {
bitmap->setInfo(info);
allocator->allocPixelRef(bitmap, 0);
} else {
@@ -173,55 +180,88 @@ static void test_bitmap_notify(skiatest::Reporter* reporter, SkResourceCache* ca
}
}
-DEF_TEST(BitmapCache_discarded_bitmap, reporter) {
- SkResourceCache::DiscardableFactory factory = SkResourceCache::GetDiscardableFactory();
- SkBitmap::Allocator* allocator = SkBitmapCache::GetAllocator();
-
- SkAutoTDelete<SkResourceCache> cache;
- if (factory) {
- cache.reset(new SkResourceCache(factory));
- } else {
- const size_t byteLimit = 100 * 1024;
- cache.reset(new SkResourceCache(byteLimit));
- }
- SkBitmap cachedBitmap;
- make_bitmap(&cachedBitmap, SkImageInfo::MakeN32Premul(5, 5), allocator);
- cachedBitmap.setImmutable();
- cachedBitmap.unlockPixels();
-
- SkBitmap bm;
- SkIRect rect = SkIRect::MakeWH(5, 5);
+#include "SkDiscardableMemoryPool.h"
- // Add a bitmap to the cache.
- REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.pixelRef(), rect, cachedBitmap, cache));
- REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm, cache));
+static SkDiscardableMemoryPool* gPool = 0;
+static SkDiscardableMemory* pool_factory(size_t bytes) {
+ SkASSERT(gPool);
+ return gPool->create(bytes);
+}
- // Finding more than once works fine.
- REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm, cache));
- bm.unlockPixels();
+static void testBitmapCache_discarded_bitmap(skiatest::Reporter* reporter, SkResourceCache* cache,
+ SkResourceCache::DiscardableFactory factory) {
+ SkBitmap::Allocator* allocator = cache->allocator();
+ const SkColorType testTypes[] = {
+ kAlpha_8_SkColorType,
+ kRGB_565_SkColorType,
+ kRGBA_8888_SkColorType,
+ kBGRA_8888_SkColorType,
+ kIndex_8_SkColorType,
+ kGray_8_SkColorType
+ };
+ for (const SkColorType testType : testTypes) {
+ SkBitmap cachedBitmap;
+ make_bitmap(&cachedBitmap, SkImageInfo::Make(5, 5, testType, kPremul_SkAlphaType),
+ allocator);
+ cachedBitmap.setImmutable();
+ cachedBitmap.unlockPixels();
+
+ SkBitmap bm;
+ SkIRect rect = SkIRect::MakeWH(5, 5);
+
+ // Add a bitmap to the cache.
+ REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.pixelRef(), rect, cachedBitmap,
+ cache));
+ REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm,
+ cache));
+
+ // Finding more than once works fine.
+ REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm,
+ cache));
+ bm.unlockPixels();
+
+ // Drop the pixels in the bitmap.
+ if (factory) {
+ REPORTER_ASSERT(reporter, gPool->getRAMUsed() > 0);
+ gPool->dumpPool();
+
+ // The bitmap is not in the cache since it has been dropped.
+ REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect,
+ &bm, cache));
+ }
- // Drop the pixels in the bitmap.
- if (factory) {
- REPORTER_ASSERT(reporter, SkGetGlobalDiscardableMemoryPool()->getRAMUsed() > 0);
- SkGetGlobalDiscardableMemoryPool()->dumpPool();
+ make_bitmap(&cachedBitmap, SkImageInfo::Make(5, 5, testType, kPremul_SkAlphaType),
+ allocator);
+ cachedBitmap.setImmutable();
+ cachedBitmap.unlockPixels();
- // The bitmap is not in the cache since it has been dropped.
- REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm, cache));
+ // We can add the bitmap back to the cache and find it again.
+ REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.pixelRef(), rect, cachedBitmap,
+ cache));
+ REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm,
+ cache));
}
-
- make_bitmap(&cachedBitmap, SkImageInfo::MakeN32Premul(5, 5), allocator);
- cachedBitmap.setImmutable();
- cachedBitmap.unlockPixels();
-
- // We can add the bitmap back to the cache and find it again.
- REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.pixelRef(), rect, cachedBitmap, cache));
- REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm, cache));
-
test_mipmapcache(reporter, cache);
test_bitmap_notify(reporter, cache);
test_mipmap_notify(reporter, cache);
}
+DEF_TEST(BitmapCache_discarded_bitmap, reporter) {
+ const size_t byteLimit = 100 * 1024;
+ {
+ SkResourceCache cache(byteLimit);
+ testBitmapCache_discarded_bitmap(reporter, &cache, nullptr);
+ }
+ {
+ SkAutoTUnref<SkDiscardableMemoryPool> pool(
+ SkDiscardableMemoryPool::Create(byteLimit, nullptr));
+ gPool = pool.get();
+ SkResourceCache::DiscardableFactory factory = pool_factory;
+ SkResourceCache cache(factory);
+ testBitmapCache_discarded_bitmap(reporter, &cache, factory);
+ }
+}
+
static void test_discarded_image(skiatest::Reporter* reporter, const SkMatrix& transform,
SkImage* (*buildImage)()) {
SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(10, 10));