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;
|
2014-08-28 21:27:42 +00:00
|
|
|
float roundedImageWidth = SkScalarRoundToScalar(orig.width() * xScale);
|
|
|
|
float roundedImageHeight = SkScalarRoundToScalar(orig.height() * xScale);
|
|
|
|
return SkBitmapCache::Find(orig, roundedImageWidth, roundedImageHeight, &scaled);
|
2014-07-18 16:18:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Draw a scaled bitmap, then return true iff it has been cached.
|
|
|
|
static bool test_scaled_image_cache_useage() {
|
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);
|
2014-07-18 16:18:40 +00:00
|
|
|
SkScalar scale = SkIntToScalar(kScale);
|
|
|
|
SkScalar scaledSize = SkIntToScalar(kBitmapSize) * scale;
|
2014-07-17 13:58:01 +00:00
|
|
|
canvas->clipRect(SkRect::MakeLTRB(0, 0, scaledSize, scaledSize));
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setFilterLevel(SkPaint::kHigh_FilterLevel);
|
2014-07-18 16:18:40 +00:00
|
|
|
|
2014-07-17 13:58:01 +00:00
|
|
|
canvas->drawBitmapRect(bitmap,
|
|
|
|
SkRect::MakeLTRB(0, 0, scaledSize, scaledSize),
|
|
|
|
&paint);
|
2014-07-18 16:18:40 +00:00
|
|
|
|
|
|
|
return is_in_scaled_image_cache(bitmap, scale, scale);
|
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
|
|
|
|
2014-07-18 16:18:40 +00:00
|
|
|
REPORTER_ASSERT(reporter, test_scaled_image_cache_useage());
|
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
|
|
|
|
2014-07-18 16:18:40 +00:00
|
|
|
REPORTER_ASSERT(reporter, test_scaled_image_cache_useage());
|
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
|
|
|
|
2014-07-18 16:18:40 +00:00
|
|
|
REPORTER_ASSERT(reporter, !test_scaled_image_cache_useage());
|
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) {
|
|
|
|
cache.reset(SkNEW_ARGS(SkResourceCache, (factory)));
|
|
|
|
} else {
|
|
|
|
const size_t byteLimit = 100 * 1024;
|
|
|
|
cache.reset(SkNEW_ARGS(SkResourceCache, (byteLimit)));
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
|
|
|
|
// Wrong subset size
|
2014-09-16 17:39:55 +00:00
|
|
|
REPORTER_ASSERT(reporter, !SkBitmapCache::Add(cachedBitmap.getGenerationID(), SkIRect::MakeWH(4, 6), cachedBitmap, cache));
|
|
|
|
REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm, cache));
|
2014-09-03 18:33:13 +00:00
|
|
|
// Wrong offset value
|
2014-09-16 17:39:55 +00:00
|
|
|
REPORTER_ASSERT(reporter, !SkBitmapCache::Add(cachedBitmap.getGenerationID(), SkIRect::MakeXYWH(-1, 0, 5, 5), cachedBitmap, cache));
|
|
|
|
REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm, cache));
|
2014-09-03 18:33:13 +00:00
|
|
|
|
|
|
|
// Should not be in the cache
|
2014-09-16 17:39:55 +00:00
|
|
|
REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm, cache));
|
2014-09-03 18:33:13 +00:00
|
|
|
|
2014-09-16 17:39:55 +00:00
|
|
|
REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.getGenerationID(), rect, cachedBitmap, cache));
|
2014-09-03 18:33:13 +00:00
|
|
|
// Should be in the cache, we just added it
|
2014-09-16 17:39:55 +00:00
|
|
|
REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID(), 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));
|
|
|
|
bool isLocked = (data->data() != NULL);
|
|
|
|
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();
|
|
|
|
|
|
|
|
const SkMipMap* mipmap = SkMipMapCache::FindAndRef(src, cache);
|
|
|
|
REPORTER_ASSERT(reporter, NULL == mipmap);
|
|
|
|
|
|
|
|
mipmap = SkMipMapCache::AddAndRef(src, cache);
|
|
|
|
REPORTER_ASSERT(reporter, mipmap);
|
|
|
|
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
|
|
|
|
mipmap = SkMipMapCache::FindAndRef(src, cache);
|
|
|
|
check_data(reporter, mipmap, 2, kInCache, kLocked);
|
|
|
|
|
|
|
|
cache->purgeAll();
|
|
|
|
check_data(reporter, mipmap, 1, kNotInCache, kLocked);
|
|
|
|
|
|
|
|
mipmap->unref();
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
cache.reset(SkNEW_ARGS(SkResourceCache, (factory)));
|
|
|
|
} else {
|
|
|
|
const size_t byteLimit = 100 * 1024;
|
|
|
|
cache.reset(SkNEW_ARGS(SkResourceCache, (byteLimit)));
|
|
|
|
}
|
|
|
|
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.
|
2014-09-16 17:39:55 +00:00
|
|
|
REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.getGenerationID(), rect, cachedBitmap, cache));
|
|
|
|
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.
|
2014-09-16 17:39:55 +00:00
|
|
|
REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.getGenerationID(), rect, cachedBitmap, cache));
|
|
|
|
REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm, cache));
|
2014-10-08 12:17:12 +00:00
|
|
|
|
|
|
|
test_mipmapcache(reporter, cache);
|
2014-09-11 17:49:52 +00:00
|
|
|
}
|