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.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/core/SkDiscardableMemory.h"
|
|
|
|
#include "src/core/SkResourceCache.h"
|
|
|
|
#include "tests/Test.h"
|
2013-07-23 19:13:54 +00:00
|
|
|
|
2014-08-21 16:46:49 +00:00
|
|
|
namespace {
|
|
|
|
static void* gGlobalAddress;
|
2014-08-28 20:35:23 +00:00
|
|
|
struct TestingKey : public SkResourceCache::Key {
|
2014-08-21 16:46:49 +00:00
|
|
|
intptr_t fValue;
|
|
|
|
|
2015-02-24 21:54:23 +00:00
|
|
|
TestingKey(intptr_t value, uint64_t sharedID = 0) : fValue(value) {
|
|
|
|
this->init(&gGlobalAddress, sharedID, sizeof(fValue));
|
2014-08-21 16:46:49 +00:00
|
|
|
}
|
|
|
|
};
|
2014-08-28 20:35:23 +00:00
|
|
|
struct TestingRec : public SkResourceCache::Rec {
|
2014-08-26 16:08:04 +00:00
|
|
|
TestingRec(const TestingKey& key, uint32_t value) : fKey(key), fValue(value) {}
|
|
|
|
|
|
|
|
TestingKey fKey;
|
|
|
|
intptr_t fValue;
|
2014-08-21 16:46:49 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
const Key& getKey() const override { return fKey; }
|
|
|
|
size_t bytesUsed() const override { return sizeof(fKey) + sizeof(fValue); }
|
2015-08-19 19:25:40 +00:00
|
|
|
const char* getCategory() const override { return "test_cache"; }
|
2015-08-27 14:41:13 +00:00
|
|
|
SkDiscardableMemory* diagnostic_only_getDiscardable() const override { return nullptr; }
|
2014-09-15 18:39:44 +00:00
|
|
|
|
|
|
|
static bool Visitor(const SkResourceCache::Rec& baseRec, void* context) {
|
|
|
|
const TestingRec& rec = static_cast<const TestingRec&>(baseRec);
|
|
|
|
intptr_t* result = (intptr_t*)context;
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2014-09-15 18:39:44 +00:00
|
|
|
*result = rec.fValue;
|
|
|
|
return true;
|
|
|
|
}
|
2014-08-26 16:08:04 +00:00
|
|
|
};
|
2020-08-06 18:11:56 +00:00
|
|
|
} // namespace
|
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
|
|
|
|
2014-09-15 18:39:44 +00:00
|
|
|
static void test_cache(skiatest::Reporter* reporter, SkResourceCache& cache, bool testPurge) {
|
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);
|
2014-09-15 18:39:44 +00:00
|
|
|
intptr_t value = -1;
|
2013-12-14 15:12:48 +00:00
|
|
|
|
2014-09-15 18:39:44 +00:00
|
|
|
REPORTER_ASSERT(reporter, !cache.find(key, TestingRec::Visitor, &value));
|
|
|
|
REPORTER_ASSERT(reporter, -1 == value);
|
2013-12-10 07:02:03 +00:00
|
|
|
|
2015-08-26 20:07:48 +00:00
|
|
|
cache.add(new TestingRec(key, i));
|
2013-12-10 07:02:03 +00:00
|
|
|
|
2014-09-15 18:39:44 +00:00
|
|
|
REPORTER_ASSERT(reporter, cache.find(key, TestingRec::Visitor, &value));
|
|
|
|
REPORTER_ASSERT(reporter, i == value);
|
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
|
2014-12-12 23:11:17 +00:00
|
|
|
for (int i = 0; i < COUNT * 100; ++i) {
|
2014-08-21 16:46:49 +00:00
|
|
|
TestingKey key(i);
|
2015-08-26 20:07:48 +00:00
|
|
|
cache.add(new TestingRec(key, i));
|
2013-12-09 22:29:30 +00:00
|
|
|
}
|
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-09-15 18:39:44 +00:00
|
|
|
intptr_t value;
|
|
|
|
(void)cache.find(TestingKey(i), TestingRec::Visitor, &value);
|
2013-12-11 15:30:24 +00:00
|
|
|
}
|
|
|
|
|
2014-07-17 13:58:01 +00:00
|
|
|
cache.setTotalByteLimit(0);
|
2013-07-23 19:13:54 +00:00
|
|
|
}
|
|
|
|
|
2015-02-24 21:54:23 +00:00
|
|
|
static void test_cache_purge_shared_id(skiatest::Reporter* reporter, SkResourceCache& cache) {
|
|
|
|
for (int i = 0; i < COUNT; ++i) {
|
2015-08-26 20:07:48 +00:00
|
|
|
TestingKey key(i, i & 1); // every other key will have a 1 for its sharedID
|
|
|
|
cache.add(new TestingRec(key, i));
|
2015-02-24 21:54:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that everyone is present
|
|
|
|
for (int i = 0; i < COUNT; ++i) {
|
|
|
|
TestingKey key(i, i & 1); // every other key will have a 1 for its sharedID
|
|
|
|
intptr_t value = -1;
|
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, cache.find(key, TestingRec::Visitor, &value));
|
|
|
|
REPORTER_ASSERT(reporter, value == i);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now purge the ones that had a non-zero sharedID (the odd-indexed ones)
|
|
|
|
cache.purgeSharedID(1);
|
|
|
|
|
|
|
|
// Ensure that only the even ones are still present
|
|
|
|
for (int i = 0; i < COUNT; ++i) {
|
|
|
|
TestingKey key(i, i & 1); // every other key will have a 1 for its sharedID
|
|
|
|
intptr_t value = -1;
|
|
|
|
|
|
|
|
if (i & 1) {
|
|
|
|
REPORTER_ASSERT(reporter, !cache.find(key, TestingRec::Visitor, &value));
|
|
|
|
} else {
|
|
|
|
REPORTER_ASSERT(reporter, cache.find(key, TestingRec::Visitor, &value));
|
|
|
|
REPORTER_ASSERT(reporter, value == i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/lazy/SkDiscardableMemoryPool.h"
|
2013-12-11 15:30:24 +00:00
|
|
|
|
|
|
|
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
|
|
|
{
|
2014-08-28 20:35:23 +00:00
|
|
|
SkResourceCache cache(defLimit);
|
2013-12-09 22:29:30 +00:00
|
|
|
test_cache(reporter, cache, true);
|
|
|
|
}
|
2013-12-11 15:30:24 +00:00
|
|
|
{
|
2017-04-25 12:58:57 +00:00
|
|
|
sk_sp<SkDiscardableMemoryPool> pool(SkDiscardableMemoryPool::Make(defLimit));
|
2014-04-04 16:43:38 +00:00
|
|
|
gPool = pool.get();
|
2014-08-28 20:35:23 +00:00
|
|
|
SkResourceCache cache(pool_factory);
|
2013-12-11 15:30:24 +00:00
|
|
|
test_cache(reporter, cache, true);
|
|
|
|
}
|
2013-12-09 22:29:30 +00:00
|
|
|
{
|
2014-08-28 20:35:23 +00:00
|
|
|
SkResourceCache cache(SkDiscardableMemory::Create);
|
2013-12-09 22:29:30 +00:00
|
|
|
test_cache(reporter, cache, false);
|
|
|
|
}
|
2015-02-24 21:54:23 +00:00
|
|
|
{
|
|
|
|
SkResourceCache cache(defLimit);
|
|
|
|
test_cache_purge_shared_id(reporter, cache);
|
|
|
|
}
|
2013-12-09 22:29:30 +00:00
|
|
|
}
|
|
|
|
|
2013-12-02 13:50:38 +00:00
|
|
|
DEF_TEST(ImageCache_doubleAdd, r) {
|
|
|
|
// Adding the same key twice should be safe.
|
2014-08-28 20:35:23 +00:00
|
|
|
SkResourceCache 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
|
|
|
|
2015-08-26 20:07:48 +00:00
|
|
|
cache.add(new TestingRec(key, 2));
|
|
|
|
cache.add(new TestingRec(key, 3));
|
2014-03-05 13:44:18 +00:00
|
|
|
|
2014-08-26 16:08:04 +00:00
|
|
|
// Lookup can return either value.
|
2014-09-15 18:39:44 +00:00
|
|
|
intptr_t value = -1;
|
|
|
|
REPORTER_ASSERT(r, cache.find(key, TestingRec::Visitor, &value));
|
|
|
|
REPORTER_ASSERT(r, 2 == value || 3 == value);
|
2013-12-02 13:50:38 +00:00
|
|
|
}
|