Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(100)

Unified Diff: tests/SkResourceCacheTest.cpp

Issue 1426753006: SkResourceCache::GetAllocator() index8 and other color types handling (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « tests/CachedDecodingPixelRefTest.cpp ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tests/SkResourceCacheTest.cpp
diff --git a/tests/SkResourceCacheTest.cpp b/tests/SkResourceCacheTest.cpp
index 9faddd016da3f593882f0af86573217407a86c17..be3db08d321a067185f99387648c89fcc53086c8 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));
« no previous file with comments | « tests/CachedDecodingPixelRefTest.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698