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"
|
2015-09-18 15:07:31 +00:00
|
|
|
#include "SkPicture.h"
|
|
|
|
#include "SkPictureRecorder.h"
|
2014-09-16 17:39:55 +00:00
|
|
|
#include "SkResourceCache.h"
|
2014-12-10 15:24:28 +00:00
|
|
|
#include "SkSurface.h"
|
2015-11-10 12:55:15 +00:00
|
|
|
#include "SkTypes.h"
|
2014-07-17 13:58:01 +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) {
|
2015-11-10 12:55:15 +00:00
|
|
|
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) {
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-07 13:29:00 +00:00
|
|
|
// https://bug.skia.org/2894
|
2014-09-03 18:33:13 +00:00
|
|
|
DEF_TEST(BitmapCache_add_rect, reporter) {
|
2014-09-16 17:39:55 +00:00
|
|
|
SkResourceCache::DiscardableFactory factory = SkResourceCache::GetDiscardableFactory();
|
|
|
|
SkBitmap::Allocator* allocator = SkBitmapCache::GetAllocator();
|
|
|
|
|
2016-11-03 18:40:50 +00:00
|
|
|
std::unique_ptr<SkResourceCache> cache;
|
2014-09-16 17:39:55 +00:00
|
|
|
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
|
2016-11-03 18:40:50 +00:00
|
|
|
REPORTER_ASSERT(reporter, !SkBitmapCache::Add(cachedPR, SkIRect::MakeWH(4, 6), cachedBitmap, cache.get()));
|
|
|
|
REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedID, rect, &bm, cache.get()));
|
2014-09-03 18:33:13 +00:00
|
|
|
// Wrong offset value
|
2016-11-03 18:40:50 +00:00
|
|
|
REPORTER_ASSERT(reporter, !SkBitmapCache::Add(cachedPR, SkIRect::MakeXYWH(-1, 0, 5, 5), cachedBitmap, cache.get()));
|
|
|
|
REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedID, rect, &bm, cache.get()));
|
2014-09-03 18:33:13 +00:00
|
|
|
|
|
|
|
// Should not be in the cache
|
2016-11-03 18:40:50 +00:00
|
|
|
REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedID, rect, &bm, cache.get()));
|
2014-09-03 18:33:13 +00:00
|
|
|
|
2016-11-03 18:40:50 +00:00
|
|
|
REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedPR, rect, cachedBitmap, cache.get()));
|
2014-09-03 18:33:13 +00:00
|
|
|
// Should be in the cache, we just added it
|
2016-11-03 18:40:50 +00:00
|
|
|
REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedID, rect, &bm, cache.get()));
|
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();
|
|
|
|
|
2016-06-10 18:41:47 +00:00
|
|
|
const SkSourceGammaTreatment treatment = SkSourceGammaTreatment::kIgnore;
|
|
|
|
|
|
|
|
const SkMipMap* mipmap = SkMipMapCache::FindAndRef(SkBitmapCacheDesc::Make(src), treatment,
|
|
|
|
cache);
|
2015-08-27 14:41:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, nullptr == mipmap);
|
2014-10-08 12:17:12 +00:00
|
|
|
|
2016-06-10 18:41:47 +00:00
|
|
|
mipmap = SkMipMapCache::AddAndRef(src, treatment, cache);
|
2014-10-08 12:17:12 +00:00
|
|
|
REPORTER_ASSERT(reporter, mipmap);
|
2015-02-24 21:54:23 +00:00
|
|
|
|
|
|
|
{
|
2016-06-10 18:41:47 +00:00
|
|
|
const SkMipMap* mm = SkMipMapCache::FindAndRef(SkBitmapCacheDesc::Make(src), treatment,
|
|
|
|
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
|
2016-06-10 18:41:47 +00:00
|
|
|
mipmap = SkMipMapCache::FindAndRef(SkBitmapCacheDesc::Make(src), treatment, 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) {
|
2016-06-10 18:41:47 +00:00
|
|
|
const SkSourceGammaTreatment treatment = SkSourceGammaTreatment::kIgnore;
|
2015-02-24 21:54:23 +00:00
|
|
|
const int N = 3;
|
2016-06-10 18:41:47 +00:00
|
|
|
|
2015-02-24 21:54:23 +00:00
|
|
|
SkBitmap src[N];
|
|
|
|
for (int i = 0; i < N; ++i) {
|
|
|
|
src[i].allocN32Pixels(5, 5);
|
|
|
|
src[i].setImmutable();
|
2016-06-10 18:41:47 +00:00
|
|
|
SkMipMapCache::AddAndRef(src[i], treatment, cache)->unref();
|
2015-02-24 21:54:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < N; ++i) {
|
2016-06-10 18:41:47 +00:00
|
|
|
const SkMipMap* mipmap = SkMipMapCache::FindAndRef(SkBitmapCacheDesc::Make(src[i]),
|
|
|
|
treatment, 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
|
|
|
|
|
2016-06-10 18:41:47 +00:00
|
|
|
mipmap = SkMipMapCache::FindAndRef(SkBitmapCacheDesc::Make(src[i]), treatment, 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-10 12:55:15 +00:00
|
|
|
#include "SkDiscardableMemoryPool.h"
|
2014-09-16 17:39:55 +00:00
|
|
|
|
2015-11-10 12:55:15 +00:00
|
|
|
static SkDiscardableMemoryPool* gPool = 0;
|
|
|
|
static SkDiscardableMemory* pool_factory(size_t bytes) {
|
|
|
|
SkASSERT(gPool);
|
|
|
|
return gPool->create(bytes);
|
|
|
|
}
|
2014-09-11 17:49:52 +00:00
|
|
|
|
2015-11-10 12:55:15 +00:00
|
|
|
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));
|
|
|
|
}
|
2014-09-11 17:49:52 +00:00
|
|
|
|
2015-11-10 12:55:15 +00:00
|
|
|
make_bitmap(&cachedBitmap, SkImageInfo::Make(5, 5, testType, kPremul_SkAlphaType),
|
|
|
|
allocator);
|
|
|
|
cachedBitmap.setImmutable();
|
|
|
|
cachedBitmap.unlockPixels();
|
2014-09-11 17:49:52 +00:00
|
|
|
|
2015-11-10 12:55:15 +00:00
|
|
|
// 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));
|
2014-09-16 17:39:55 +00:00
|
|
|
}
|
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
|
|
|
}
|
2015-09-18 15:07:31 +00:00
|
|
|
|
2015-11-10 12:55:15 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-18 15:07:31 +00:00
|
|
|
static void test_discarded_image(skiatest::Reporter* reporter, const SkMatrix& transform,
|
2016-03-17 17:51:11 +00:00
|
|
|
sk_sp<SkImage> (*buildImage)()) {
|
2016-03-24 01:59:25 +00:00
|
|
|
auto surface(SkSurface::MakeRasterN32Premul(10, 10));
|
2015-09-18 15:07:31 +00:00
|
|
|
SkCanvas* canvas = surface->getCanvas();
|
|
|
|
|
|
|
|
// SkBitmapCache is global, so other threads could be evicting our bitmaps. Loop a few times
|
|
|
|
// to mitigate this risk.
|
|
|
|
const unsigned kRepeatCount = 42;
|
|
|
|
for (unsigned i = 0; i < kRepeatCount; ++i) {
|
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
|
|
|
|
2016-03-17 17:51:11 +00:00
|
|
|
sk_sp<SkImage> image(buildImage());
|
2015-09-18 15:07:31 +00:00
|
|
|
|
|
|
|
// always use high quality to ensure caching when scaled
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setFilterQuality(kHigh_SkFilterQuality);
|
|
|
|
|
|
|
|
// draw the image (with a transform, to tickle different code paths) to ensure
|
|
|
|
// any associated resources get cached
|
|
|
|
canvas->concat(transform);
|
|
|
|
canvas->drawImage(image, 0, 0, &paint);
|
|
|
|
|
|
|
|
auto imageId = image->uniqueID();
|
|
|
|
|
|
|
|
// delete the image
|
|
|
|
image.reset(nullptr);
|
|
|
|
|
|
|
|
// all resources should have been purged
|
|
|
|
SkBitmap result;
|
|
|
|
REPORTER_ASSERT(reporter, !SkBitmapCache::Find(imageId, &result));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Verify that associated bitmap cache entries are purged on SkImage destruction.
|
|
|
|
DEF_TEST(BitmapCache_discarded_image, reporter) {
|
|
|
|
// Cache entries associated with SkImages fall into two categories:
|
|
|
|
//
|
|
|
|
// 1) generated image bitmaps (managed by the image cacherator)
|
|
|
|
// 2) scaled/resampled bitmaps (cached when HQ filters are used)
|
|
|
|
//
|
|
|
|
// To exercise the first cache type, we use generated/picture-backed SkImages.
|
|
|
|
// To exercise the latter, we draw scaled bitmap images using HQ filters.
|
|
|
|
|
|
|
|
const SkMatrix xforms[] = {
|
|
|
|
SkMatrix::MakeScale(1, 1),
|
|
|
|
SkMatrix::MakeScale(1.7f, 0.5f),
|
|
|
|
};
|
|
|
|
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(xforms); ++i) {
|
|
|
|
test_discarded_image(reporter, xforms[i], []() {
|
2016-03-24 01:59:25 +00:00
|
|
|
auto surface(SkSurface::MakeRasterN32Premul(10, 10));
|
2015-09-18 15:07:31 +00:00
|
|
|
surface->getCanvas()->clear(SK_ColorCYAN);
|
2016-03-17 17:51:11 +00:00
|
|
|
return surface->makeImageSnapshot();
|
2015-09-18 15:07:31 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test_discarded_image(reporter, xforms[i], []() {
|
|
|
|
SkPictureRecorder recorder;
|
|
|
|
SkCanvas* canvas = recorder.beginRecording(10, 10);
|
|
|
|
canvas->clear(SK_ColorCYAN);
|
2016-03-18 14:25:55 +00:00
|
|
|
return SkImage::MakeFromPicture(recorder.finishRecordingAsPicture(),
|
|
|
|
SkISize::Make(10, 10), nullptr, nullptr);
|
2015-09-18 15:07:31 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|