2014-07-17 13:58:01 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2014 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2014-09-16 17:39:55 +00:00
|
|
|
|
2014-07-17 13:58:01 +00:00
|
|
|
#include "Test.h"
|
2014-08-21 16:46:49 +00:00
|
|
|
#include "SkBitmapCache.h"
|
2014-09-16 17:39:55 +00:00
|
|
|
#include "SkCanvas.h"
|
2014-09-11 17:49:52 +00:00
|
|
|
#include "SkDiscardableMemoryPool.h"
|
2014-09-16 17:39:55 +00:00
|
|
|
#include "SkGraphics.h"
|
|
|
|
#include "SkResourceCache.h"
|
2014-12-10 15:24:28 +00:00
|
|
|
#include "SkSurface.h"
|
2014-07-17 13:58:01 +00:00
|
|
|
|
|
|
|
static const int kCanvasSize = 1;
|
|
|
|
static const int kBitmapSize = 16;
|
|
|
|
static const int kScale = 8;
|
|
|
|
|
2014-07-18 16:18:40 +00:00
|
|
|
static bool is_in_scaled_image_cache(const SkBitmap& orig,
|
|
|
|
SkScalar xScale,
|
|
|
|
SkScalar yScale) {
|
|
|
|
SkBitmap scaled;
|
2015-08-31 22:16:17 +00:00
|
|
|
int width = SkScalarRoundToInt(orig.width() * xScale);
|
|
|
|
int height = SkScalarRoundToInt(orig.height() * yScale);
|
2015-09-15 19:38:12 +00:00
|
|
|
return SkBitmapCache::FindWH(SkBitmapCacheDesc::Make(orig, width, height), &scaled);
|
2014-07-18 16:18:40 +00:00
|
|
|
}
|
|
|
|
|
2015-01-24 01:06:20 +00:00
|
|
|
// Draw a scaled bitmap, then return true if it has been cached.
|
|
|
|
static bool test_scaled_image_cache_usage() {
|
2014-12-10 15:24:28 +00:00
|
|
|
SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(kCanvasSize, kCanvasSize));
|
|
|
|
SkCanvas* canvas = surface->getCanvas();
|
2014-07-17 13:58:01 +00:00
|
|
|
SkBitmap bitmap;
|
2014-09-02 19:50:45 +00:00
|
|
|
bitmap.allocN32Pixels(kBitmapSize, kBitmapSize);
|
2014-07-17 17:17:28 +00:00
|
|
|
bitmap.eraseColor(0xFFFFFFFF);
|
2015-01-24 01:06:20 +00:00
|
|
|
SkScalar xScale = SkIntToScalar(kScale);
|
|
|
|
SkScalar yScale = xScale / 2;
|
|
|
|
SkScalar xScaledSize = SkIntToScalar(kBitmapSize) * xScale;
|
|
|
|
SkScalar yScaledSize = SkIntToScalar(kBitmapSize) * yScale;
|
|
|
|
canvas->clipRect(SkRect::MakeLTRB(0, 0, xScaledSize, yScaledSize));
|
2014-07-17 13:58:01 +00:00
|
|
|
SkPaint paint;
|
2015-03-16 17:08:34 +00:00
|
|
|
paint.setFilterQuality(kHigh_SkFilterQuality);
|
2014-07-18 16:18:40 +00:00
|
|
|
|
2014-07-17 13:58:01 +00:00
|
|
|
canvas->drawBitmapRect(bitmap,
|
2015-01-24 01:06:20 +00:00
|
|
|
SkRect::MakeLTRB(0, 0, xScaledSize, yScaledSize),
|
2014-07-17 13:58:01 +00:00
|
|
|
&paint);
|
2014-07-18 16:18:40 +00:00
|
|
|
|
2015-01-24 01:06:20 +00:00
|
|
|
return is_in_scaled_image_cache(bitmap, xScale, yScale);
|
2014-07-17 13:58:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// http://crbug.com/389439
|
2014-08-28 20:35:23 +00:00
|
|
|
DEF_TEST(ResourceCache_SingleAllocationByteLimit, reporter) {
|
|
|
|
size_t originalByteLimit = SkGraphics::GetResourceCacheTotalByteLimit();
|
2014-07-17 13:58:01 +00:00
|
|
|
size_t originalAllocationLimit =
|
2014-08-28 20:35:23 +00:00
|
|
|
SkGraphics::GetResourceCacheSingleAllocationByteLimit();
|
2014-07-17 13:58:01 +00:00
|
|
|
|
|
|
|
size_t size = kBitmapSize * kScale * kBitmapSize * kScale
|
|
|
|
* SkColorTypeBytesPerPixel(kN32_SkColorType);
|
|
|
|
|
2014-08-28 20:35:23 +00:00
|
|
|
SkGraphics::SetResourceCacheTotalByteLimit(0); // clear cache
|
|
|
|
SkGraphics::SetResourceCacheTotalByteLimit(2 * size);
|
|
|
|
SkGraphics::SetResourceCacheSingleAllocationByteLimit(0); // No limit
|
2014-07-17 13:58:01 +00:00
|
|
|
|
2015-01-24 01:06:20 +00:00
|
|
|
REPORTER_ASSERT(reporter, test_scaled_image_cache_usage());
|
2014-07-17 13:58:01 +00:00
|
|
|
|
2014-08-28 20:35:23 +00:00
|
|
|
SkGraphics::SetResourceCacheTotalByteLimit(0); // clear cache
|
|
|
|
SkGraphics::SetResourceCacheTotalByteLimit(2 * size);
|
|
|
|
SkGraphics::SetResourceCacheSingleAllocationByteLimit(size * 2); // big enough
|
2014-07-17 13:58:01 +00:00
|
|
|
|
2015-01-24 01:06:20 +00:00
|
|
|
REPORTER_ASSERT(reporter, test_scaled_image_cache_usage());
|
2014-07-17 13:58:01 +00:00
|
|
|
|
2014-08-28 20:35:23 +00:00
|
|
|
SkGraphics::SetResourceCacheTotalByteLimit(0); // clear cache
|
|
|
|
SkGraphics::SetResourceCacheTotalByteLimit(2 * size);
|
|
|
|
SkGraphics::SetResourceCacheSingleAllocationByteLimit(size / 2); // too small
|
2014-07-17 13:58:01 +00:00
|
|
|
|
2015-01-24 01:06:20 +00:00
|
|
|
REPORTER_ASSERT(reporter, !test_scaled_image_cache_usage());
|
2014-07-17 13:58:01 +00:00
|
|
|
|
2014-08-28 20:35:23 +00:00
|
|
|
SkGraphics::SetResourceCacheSingleAllocationByteLimit(originalAllocationLimit);
|
|
|
|
SkGraphics::SetResourceCacheTotalByteLimit(originalByteLimit);
|
2014-07-17 13:58:01 +00:00
|
|
|
}
|
2014-09-03 18:33:13 +00:00
|
|
|
|
2014-09-16 17:39:55 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////
|
2014-09-04 17:50:53 +00:00
|
|
|
|
2014-09-16 17:39:55 +00:00
|
|
|
static void make_bitmap(SkBitmap* bitmap, const SkImageInfo& info, SkBitmap::Allocator* allocator) {
|
2014-09-05 20:34:00 +00:00
|
|
|
if (allocator) {
|
2014-09-16 17:39:55 +00:00
|
|
|
bitmap->setInfo(info);
|
|
|
|
allocator->allocPixelRef(bitmap, 0);
|
2014-09-03 18:33:13 +00:00
|
|
|
} else {
|
2014-09-16 17:39:55 +00:00
|
|
|
bitmap->allocPixels(info);
|
2014-09-03 18:33:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// http://skbug.com/2894
|
|
|
|
DEF_TEST(BitmapCache_add_rect, reporter) {
|
2014-09-16 17:39:55 +00:00
|
|
|
SkResourceCache::DiscardableFactory factory = SkResourceCache::GetDiscardableFactory();
|
|
|
|
SkBitmap::Allocator* allocator = SkBitmapCache::GetAllocator();
|
|
|
|
|
|
|
|
SkAutoTDelete<SkResourceCache> cache;
|
|
|
|
if (factory) {
|
2015-08-26 20:07:48 +00:00
|
|
|
cache.reset(new SkResourceCache(factory));
|
2014-09-16 17:39:55 +00:00
|
|
|
} else {
|
|
|
|
const size_t byteLimit = 100 * 1024;
|
2015-08-26 20:07:48 +00:00
|
|
|
cache.reset(new SkResourceCache(byteLimit));
|
2014-09-16 17:39:55 +00:00
|
|
|
}
|
|
|
|
SkBitmap cachedBitmap;
|
|
|
|
make_bitmap(&cachedBitmap, SkImageInfo::MakeN32Premul(5, 5), allocator);
|
|
|
|
cachedBitmap.setImmutable();
|
|
|
|
|
2014-09-03 18:33:13 +00:00
|
|
|
SkBitmap bm;
|
|
|
|
SkIRect rect = SkIRect::MakeWH(5, 5);
|
2015-02-25 15:17:11 +00:00
|
|
|
uint32_t cachedID = cachedBitmap.getGenerationID();
|
|
|
|
SkPixelRef* cachedPR = cachedBitmap.pixelRef();
|
2014-09-03 18:33:13 +00:00
|
|
|
|
|
|
|
// Wrong subset size
|
2015-02-25 15:17:11 +00:00
|
|
|
REPORTER_ASSERT(reporter, !SkBitmapCache::Add(cachedPR, SkIRect::MakeWH(4, 6), cachedBitmap, cache));
|
|
|
|
REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedID, rect, &bm, cache));
|
2014-09-03 18:33:13 +00:00
|
|
|
// Wrong offset value
|
2015-02-25 15:17:11 +00:00
|
|
|
REPORTER_ASSERT(reporter, !SkBitmapCache::Add(cachedPR, SkIRect::MakeXYWH(-1, 0, 5, 5), cachedBitmap, cache));
|
|
|
|
REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedID, rect, &bm, cache));
|
2014-09-03 18:33:13 +00:00
|
|
|
|
|
|
|
// Should not be in the cache
|
2015-02-25 15:17:11 +00:00
|
|
|
REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedID, rect, &bm, cache));
|
2014-09-03 18:33:13 +00:00
|
|
|
|
2015-02-25 15:17:11 +00:00
|
|
|
REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedPR, rect, cachedBitmap, cache));
|
2014-09-03 18:33:13 +00:00
|
|
|
// Should be in the cache, we just added it
|
2015-02-25 15:17:11 +00:00
|
|
|
REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedID, rect, &bm, cache));
|
2014-09-03 18:33:13 +00:00
|
|
|
}
|
2014-09-11 17:49:52 +00:00
|
|
|
|
2014-10-08 12:17:12 +00:00
|
|
|
#include "SkMipMap.h"
|
|
|
|
|
|
|
|
enum LockedState {
|
|
|
|
kNotLocked,
|
|
|
|
kLocked,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum CachedState {
|
|
|
|
kNotInCache,
|
|
|
|
kInCache,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void check_data(skiatest::Reporter* reporter, const SkCachedData* data,
|
|
|
|
int refcnt, CachedState cacheState, LockedState lockedState) {
|
|
|
|
REPORTER_ASSERT(reporter, data->testing_only_getRefCnt() == refcnt);
|
|
|
|
REPORTER_ASSERT(reporter, data->testing_only_isInCache() == (kInCache == cacheState));
|
2015-08-27 14:41:13 +00:00
|
|
|
bool isLocked = (data->data() != nullptr);
|
2014-10-08 12:17:12 +00:00
|
|
|
REPORTER_ASSERT(reporter, isLocked == (lockedState == kLocked));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_mipmapcache(skiatest::Reporter* reporter, SkResourceCache* cache) {
|
|
|
|
cache->purgeAll();
|
|
|
|
|
|
|
|
SkBitmap src;
|
|
|
|
src.allocN32Pixels(5, 5);
|
|
|
|
src.setImmutable();
|
|
|
|
|
2015-09-15 19:38:12 +00:00
|
|
|
const SkMipMap* mipmap = SkMipMapCache::FindAndRef(SkBitmapCacheDesc::Make(src), cache);
|
2015-08-27 14:41:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, nullptr == mipmap);
|
2014-10-08 12:17:12 +00:00
|
|
|
|
|
|
|
mipmap = SkMipMapCache::AddAndRef(src, cache);
|
|
|
|
REPORTER_ASSERT(reporter, mipmap);
|
2015-02-24 21:54:23 +00:00
|
|
|
|
|
|
|
{
|
2015-09-15 19:38:12 +00:00
|
|
|
const SkMipMap* mm = SkMipMapCache::FindAndRef(SkBitmapCacheDesc::Make(src), cache);
|
2015-02-24 21:54:23 +00:00
|
|
|
REPORTER_ASSERT(reporter, mm);
|
|
|
|
REPORTER_ASSERT(reporter, mm == mipmap);
|
|
|
|
mm->unref();
|
|
|
|
}
|
|
|
|
|
2014-10-08 12:17:12 +00:00
|
|
|
check_data(reporter, mipmap, 2, kInCache, kLocked);
|
|
|
|
|
|
|
|
mipmap->unref();
|
|
|
|
// tricky, since technically after this I'm no longer an owner, but since the cache is
|
|
|
|
// local, I know it won't get purged behind my back
|
|
|
|
check_data(reporter, mipmap, 1, kInCache, kNotLocked);
|
|
|
|
|
|
|
|
// find us again
|
2015-09-15 19:38:12 +00:00
|
|
|
mipmap = SkMipMapCache::FindAndRef(SkBitmapCacheDesc::Make(src), cache);
|
2014-10-08 12:17:12 +00:00
|
|
|
check_data(reporter, mipmap, 2, kInCache, kLocked);
|
|
|
|
|
|
|
|
cache->purgeAll();
|
|
|
|
check_data(reporter, mipmap, 1, kNotInCache, kLocked);
|
|
|
|
|
|
|
|
mipmap->unref();
|
|
|
|
}
|
|
|
|
|
2015-02-24 21:54:23 +00:00
|
|
|
static void test_mipmap_notify(skiatest::Reporter* reporter, SkResourceCache* cache) {
|
|
|
|
const int N = 3;
|
|
|
|
SkBitmap src[N];
|
|
|
|
for (int i = 0; i < N; ++i) {
|
|
|
|
src[i].allocN32Pixels(5, 5);
|
|
|
|
src[i].setImmutable();
|
|
|
|
SkMipMapCache::AddAndRef(src[i], cache)->unref();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < N; ++i) {
|
2015-09-15 19:38:12 +00:00
|
|
|
const SkMipMap* mipmap = SkMipMapCache::FindAndRef(SkBitmapCacheDesc::Make(src[i]), cache);
|
2015-02-24 21:54:23 +00:00
|
|
|
if (cache) {
|
|
|
|
// if cache is null, we're working on the global cache, and other threads might purge
|
|
|
|
// it, making this check fragile.
|
|
|
|
REPORTER_ASSERT(reporter, mipmap);
|
|
|
|
}
|
|
|
|
SkSafeUnref(mipmap);
|
|
|
|
|
|
|
|
src[i].reset(); // delete the underlying pixelref, which *should* remove us from the cache
|
|
|
|
|
2015-09-15 19:38:12 +00:00
|
|
|
mipmap = SkMipMapCache::FindAndRef(SkBitmapCacheDesc::Make(src[i]), cache);
|
2015-02-24 21:54:23 +00:00
|
|
|
REPORTER_ASSERT(reporter, !mipmap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_bitmap_notify(skiatest::Reporter* reporter, SkResourceCache* cache) {
|
|
|
|
const SkIRect subset = SkIRect::MakeWH(5, 5);
|
|
|
|
const int N = 3;
|
|
|
|
SkBitmap src[N], dst[N];
|
|
|
|
for (int i = 0; i < N; ++i) {
|
|
|
|
src[i].allocN32Pixels(5, 5);
|
|
|
|
src[i].setImmutable();
|
|
|
|
dst[i].allocN32Pixels(5, 5);
|
|
|
|
dst[i].setImmutable();
|
2015-02-25 15:17:11 +00:00
|
|
|
SkBitmapCache::Add(src[i].pixelRef(), subset, dst[i], cache);
|
2015-02-24 21:54:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < N; ++i) {
|
|
|
|
const uint32_t genID = src[i].getGenerationID();
|
|
|
|
SkBitmap result;
|
|
|
|
bool found = SkBitmapCache::Find(genID, subset, &result, cache);
|
|
|
|
if (cache) {
|
|
|
|
// if cache is null, we're working on the global cache, and other threads might purge
|
|
|
|
// it, making this check fragile.
|
|
|
|
REPORTER_ASSERT(reporter, found);
|
|
|
|
}
|
|
|
|
|
|
|
|
src[i].reset(); // delete the underlying pixelref, which *should* remove us from the cache
|
|
|
|
|
|
|
|
found = SkBitmapCache::Find(genID, subset, &result, cache);
|
|
|
|
REPORTER_ASSERT(reporter, !found);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-11 17:49:52 +00:00
|
|
|
DEF_TEST(BitmapCache_discarded_bitmap, reporter) {
|
2014-09-16 17:39:55 +00:00
|
|
|
SkResourceCache::DiscardableFactory factory = SkResourceCache::GetDiscardableFactory();
|
|
|
|
SkBitmap::Allocator* allocator = SkBitmapCache::GetAllocator();
|
|
|
|
|
|
|
|
SkAutoTDelete<SkResourceCache> cache;
|
|
|
|
if (factory) {
|
2015-08-26 20:07:48 +00:00
|
|
|
cache.reset(new SkResourceCache(factory));
|
2014-09-16 17:39:55 +00:00
|
|
|
} else {
|
|
|
|
const size_t byteLimit = 100 * 1024;
|
2015-08-26 20:07:48 +00:00
|
|
|
cache.reset(new SkResourceCache(byteLimit));
|
2014-09-16 17:39:55 +00:00
|
|
|
}
|
|
|
|
SkBitmap cachedBitmap;
|
|
|
|
make_bitmap(&cachedBitmap, SkImageInfo::MakeN32Premul(5, 5), allocator);
|
2014-09-11 17:49:52 +00:00
|
|
|
cachedBitmap.setImmutable();
|
|
|
|
cachedBitmap.unlockPixels();
|
|
|
|
|
2014-09-16 17:39:55 +00:00
|
|
|
SkBitmap bm;
|
|
|
|
SkIRect rect = SkIRect::MakeWH(5, 5);
|
|
|
|
|
2014-09-11 17:49:52 +00:00
|
|
|
// Add a bitmap to the cache.
|
2015-02-25 15:17:11 +00:00
|
|
|
REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.pixelRef(), rect, cachedBitmap, cache));
|
2014-09-16 17:39:55 +00:00
|
|
|
REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm, cache));
|
2014-09-11 17:49:52 +00:00
|
|
|
|
|
|
|
// Finding more than once works fine.
|
2014-09-16 17:39:55 +00:00
|
|
|
REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm, cache));
|
2014-09-11 17:49:52 +00:00
|
|
|
bm.unlockPixels();
|
|
|
|
|
|
|
|
// Drop the pixels in the bitmap.
|
2014-09-16 17:39:55 +00:00
|
|
|
if (factory) {
|
|
|
|
REPORTER_ASSERT(reporter, SkGetGlobalDiscardableMemoryPool()->getRAMUsed() > 0);
|
|
|
|
SkGetGlobalDiscardableMemoryPool()->dumpPool();
|
|
|
|
REPORTER_ASSERT(reporter, SkGetGlobalDiscardableMemoryPool()->getRAMUsed() == 0);
|
2014-09-11 17:49:52 +00:00
|
|
|
|
2014-09-16 17:39:55 +00:00
|
|
|
// The bitmap is not in the cache since it has been dropped.
|
|
|
|
REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm, cache));
|
|
|
|
}
|
2014-09-11 17:49:52 +00:00
|
|
|
|
2014-09-16 17:39:55 +00:00
|
|
|
make_bitmap(&cachedBitmap, SkImageInfo::MakeN32Premul(5, 5), allocator);
|
2014-09-11 17:49:52 +00:00
|
|
|
cachedBitmap.setImmutable();
|
|
|
|
cachedBitmap.unlockPixels();
|
|
|
|
|
|
|
|
// We can add the bitmap back to the cache and find it again.
|
2015-02-25 15:17:11 +00:00
|
|
|
REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.pixelRef(), rect, cachedBitmap, cache));
|
2014-09-16 17:39:55 +00:00
|
|
|
REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm, cache));
|
2014-10-08 12:17:12 +00:00
|
|
|
|
|
|
|
test_mipmapcache(reporter, cache);
|
2015-02-24 21:54:23 +00:00
|
|
|
test_bitmap_notify(reporter, cache);
|
|
|
|
test_mipmap_notify(reporter, cache);
|
2014-09-11 17:49:52 +00:00
|
|
|
}
|