2013-12-11 15:30:24 +00:00
|
|
|
/*
|
2013-07-23 19:13:54 +00:00
|
|
|
* Copyright 2013 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2013-12-09 22:29:30 +00:00
|
|
|
#include "SkDiscardableMemory.h"
|
2013-07-23 19:13:54 +00:00
|
|
|
#include "SkScaledImageCache.h"
|
2014-01-24 20:56:26 +00:00
|
|
|
#include "Test.h"
|
2013-07-23 19:13:54 +00:00
|
|
|
|
2014-08-21 16:46:49 +00:00
|
|
|
namespace {
|
|
|
|
static void* gGlobalAddress;
|
|
|
|
struct TestingKey : public SkScaledImageCache::Key {
|
|
|
|
void* fPtr;
|
|
|
|
intptr_t fValue;
|
|
|
|
|
|
|
|
TestingKey(intptr_t value) : fPtr(&gGlobalAddress), fValue(value) {
|
|
|
|
this->init(sizeof(fPtr) + sizeof(fValue));
|
|
|
|
}
|
|
|
|
};
|
2014-08-26 16:08:04 +00:00
|
|
|
struct TestingRec : public SkScaledImageCache::Rec {
|
|
|
|
TestingRec(const TestingKey& key, uint32_t value) : fKey(key), fValue(value) {}
|
|
|
|
|
|
|
|
TestingKey fKey;
|
|
|
|
intptr_t fValue;
|
2014-08-21 16:46:49 +00:00
|
|
|
|
2014-08-26 16:08:04 +00:00
|
|
|
virtual const Key& getKey() const SK_OVERRIDE { return fKey; }
|
|
|
|
virtual size_t bytesUsed() const SK_OVERRIDE { return sizeof(fKey) + sizeof(fValue); }
|
|
|
|
};
|
2013-07-23 19:13:54 +00:00
|
|
|
}
|
|
|
|
|
2013-12-09 22:29:30 +00:00
|
|
|
static const int COUNT = 10;
|
|
|
|
static const int DIM = 256;
|
2013-07-23 19:13:54 +00:00
|
|
|
|
2013-12-09 22:29:30 +00:00
|
|
|
static void test_cache(skiatest::Reporter* reporter, SkScaledImageCache& cache,
|
|
|
|
bool testPurge) {
|
2014-08-26 16:08:04 +00:00
|
|
|
SkScaledImageCache::ID id;
|
2013-12-10 07:02:03 +00:00
|
|
|
|
2013-12-11 15:30:24 +00:00
|
|
|
for (int i = 0; i < COUNT; ++i) {
|
2014-08-26 16:08:04 +00:00
|
|
|
TestingKey key(i);
|
2013-12-14 15:12:48 +00:00
|
|
|
|
2014-08-26 16:08:04 +00:00
|
|
|
const TestingRec* rec = (const TestingRec*)cache.findAndLock(key);
|
|
|
|
REPORTER_ASSERT(reporter, NULL == rec);
|
2013-12-10 07:02:03 +00:00
|
|
|
|
2014-08-26 16:08:04 +00:00
|
|
|
TestingRec* newRec = SkNEW_ARGS(TestingRec, (key, i));
|
|
|
|
const TestingRec* addedRec = (const TestingRec*)cache.addAndLock(newRec);
|
|
|
|
REPORTER_ASSERT(reporter, NULL != addedRec);
|
2013-12-10 07:02:03 +00:00
|
|
|
|
2014-08-26 16:08:04 +00:00
|
|
|
const TestingRec* foundRec = (const TestingRec*)cache.findAndLock(key);
|
|
|
|
REPORTER_ASSERT(reporter, foundRec == addedRec);
|
|
|
|
REPORTER_ASSERT(reporter, foundRec->fValue == i);
|
|
|
|
cache.unlock(foundRec);
|
|
|
|
cache.unlock(addedRec);
|
2013-07-23 19:13:54 +00:00
|
|
|
}
|
2013-12-10 07:02:03 +00:00
|
|
|
|
2013-12-09 22:29:30 +00:00
|
|
|
if (testPurge) {
|
|
|
|
// stress test, should trigger purges
|
|
|
|
for (size_t i = 0; i < COUNT * 100; ++i) {
|
2014-08-21 16:46:49 +00:00
|
|
|
TestingKey key(i);
|
2014-08-26 16:08:04 +00:00
|
|
|
SkScaledImageCache::ID id = cache.addAndLock(SkNEW_ARGS(TestingRec, (key, i)));
|
2013-12-09 22:29:30 +00:00
|
|
|
REPORTER_ASSERT(reporter, NULL != id);
|
|
|
|
cache.unlock(id);
|
|
|
|
}
|
2013-07-23 19:13:54 +00:00
|
|
|
}
|
2013-12-11 15:30:24 +00:00
|
|
|
|
|
|
|
// test the originals after all that purging
|
|
|
|
for (int i = 0; i < COUNT; ++i) {
|
2014-08-26 16:08:04 +00:00
|
|
|
id = cache.findAndLock(TestingKey(i));
|
2013-12-11 15:30:24 +00:00
|
|
|
if (id) {
|
|
|
|
cache.unlock(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-17 13:58:01 +00:00
|
|
|
cache.setTotalByteLimit(0);
|
2013-07-23 19:13:54 +00:00
|
|
|
}
|
|
|
|
|
2013-12-11 15:30:24 +00:00
|
|
|
#include "SkDiscardableMemoryPool.h"
|
|
|
|
|
|
|
|
static SkDiscardableMemoryPool* gPool;
|
|
|
|
static SkDiscardableMemory* pool_factory(size_t bytes) {
|
2014-04-04 16:43:38 +00:00
|
|
|
SkASSERT(gPool);
|
2013-12-11 15:30:24 +00:00
|
|
|
return gPool->create(bytes);
|
|
|
|
}
|
|
|
|
|
2013-12-12 21:11:12 +00:00
|
|
|
DEF_TEST(ImageCache, reporter) {
|
2013-12-11 15:30:24 +00:00
|
|
|
static const size_t defLimit = DIM * DIM * 4 * COUNT + 1024; // 1K slop
|
|
|
|
|
2013-12-09 22:29:30 +00:00
|
|
|
{
|
|
|
|
SkScaledImageCache cache(defLimit);
|
|
|
|
test_cache(reporter, cache, true);
|
|
|
|
}
|
2013-12-11 15:30:24 +00:00
|
|
|
{
|
2014-04-04 16:43:38 +00:00
|
|
|
SkAutoTUnref<SkDiscardableMemoryPool> pool(
|
|
|
|
SkDiscardableMemoryPool::Create(defLimit, NULL));
|
|
|
|
gPool = pool.get();
|
2013-12-11 15:30:24 +00:00
|
|
|
SkScaledImageCache cache(pool_factory);
|
|
|
|
test_cache(reporter, cache, true);
|
|
|
|
}
|
2013-12-09 22:29:30 +00:00
|
|
|
{
|
|
|
|
SkScaledImageCache cache(SkDiscardableMemory::Create);
|
|
|
|
test_cache(reporter, cache, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-02 13:50:38 +00:00
|
|
|
DEF_TEST(ImageCache_doubleAdd, r) {
|
|
|
|
// Adding the same key twice should be safe.
|
2014-03-05 13:44:18 +00:00
|
|
|
SkScaledImageCache cache(4096);
|
2013-12-02 13:50:38 +00:00
|
|
|
|
2014-08-26 16:08:04 +00:00
|
|
|
TestingKey key(1);
|
2014-08-21 16:46:49 +00:00
|
|
|
|
2014-08-26 16:08:04 +00:00
|
|
|
SkScaledImageCache::ID id1 = cache.addAndLock(SkNEW_ARGS(TestingRec, (key, 2)));
|
|
|
|
SkScaledImageCache::ID id2 = cache.addAndLock(SkNEW_ARGS(TestingRec, (key, 3)));
|
2013-12-02 13:50:38 +00:00
|
|
|
// We don't really care if id1 == id2 as long as unlocking both works.
|
|
|
|
cache.unlock(id1);
|
|
|
|
cache.unlock(id2);
|
2014-03-05 13:44:18 +00:00
|
|
|
|
2014-08-26 16:08:04 +00:00
|
|
|
// Lookup can return either value.
|
|
|
|
const TestingRec* rec = (const TestingRec*)cache.findAndLock(key);
|
|
|
|
REPORTER_ASSERT(r, NULL != rec);
|
|
|
|
REPORTER_ASSERT(r, 2 == rec->fValue || 3 == rec->fValue);
|
|
|
|
cache.unlock(rec);
|
2013-12-02 13:50:38 +00:00
|
|
|
}
|