*/
#include "SkDiscardableMemory.h"
-#include "SkScaledImageCache.h"
+#include "SkResourceCache.h"
#include "Test.h"
-static void make_bm(SkBitmap* bm, int w, int h) {
- bm->allocN32Pixels(w, h);
+namespace {
+static void* gGlobalAddress;
+struct TestingKey : public SkResourceCache::Key {
+ void* fPtr;
+ intptr_t fValue;
+
+ TestingKey(intptr_t value) : fPtr(&gGlobalAddress), fValue(value) {
+ this->init(sizeof(fPtr) + sizeof(fValue));
+ }
+};
+struct TestingRec : public SkResourceCache::Rec {
+ TestingRec(const TestingKey& key, uint32_t value) : fKey(key), fValue(value) {}
+
+ TestingKey fKey;
+ intptr_t fValue;
+
+ virtual const Key& getKey() const SK_OVERRIDE { return fKey; }
+ virtual size_t bytesUsed() const SK_OVERRIDE { return sizeof(fKey) + sizeof(fValue); }
+
+ static bool Visitor(const SkResourceCache::Rec& baseRec, void* context) {
+ const TestingRec& rec = static_cast<const TestingRec&>(baseRec);
+ intptr_t* result = (intptr_t*)context;
+
+ *result = rec.fValue;
+ return true;
+ }
+};
}
static const int COUNT = 10;
static const int DIM = 256;
-static void test_cache(skiatest::Reporter* reporter, SkScaledImageCache& cache,
- bool testPurge) {
- SkScaledImageCache::ID* id;
-
- SkBitmap bm[COUNT];
-
- const SkScalar scale = 2;
- for (int i = 0; i < COUNT; ++i) {
- make_bm(&bm[i], DIM, DIM);
- }
-
+static void test_cache(skiatest::Reporter* reporter, SkResourceCache& cache, bool testPurge) {
for (int i = 0; i < COUNT; ++i) {
- SkBitmap tmp;
-
- SkScaledImageCache::ID* id = cache.findAndLock(bm[i], scale, scale, &tmp);
- REPORTER_ASSERT(reporter, NULL == id);
+ TestingKey key(i);
+ intptr_t value = -1;
- make_bm(&tmp, DIM, DIM);
- id = cache.addAndLock(bm[i], scale, scale, tmp);
- REPORTER_ASSERT(reporter, NULL != id);
+ REPORTER_ASSERT(reporter, !cache.find(key, TestingRec::Visitor, &value));
+ REPORTER_ASSERT(reporter, -1 == value);
- SkBitmap tmp2;
- SkScaledImageCache::ID* id2 = cache.findAndLock(bm[i], scale, scale,
- &tmp2);
- REPORTER_ASSERT(reporter, id == id2);
- REPORTER_ASSERT(reporter, tmp.pixelRef() == tmp2.pixelRef());
- REPORTER_ASSERT(reporter, tmp.width() == tmp2.width());
- REPORTER_ASSERT(reporter, tmp.height() == tmp2.height());
- cache.unlock(id2);
+ cache.add(SkNEW_ARGS(TestingRec, (key, i)));
- cache.unlock(id);
+ REPORTER_ASSERT(reporter, cache.find(key, TestingRec::Visitor, &value));
+ REPORTER_ASSERT(reporter, i == value);
}
if (testPurge) {
// stress test, should trigger purges
- float incScale = 2;
for (size_t i = 0; i < COUNT * 100; ++i) {
- incScale += 1;
-
- SkBitmap tmp;
- make_bm(&tmp, DIM, DIM);
-
- SkScaledImageCache::ID* id = cache.addAndLock(bm[0], incScale,
- incScale, tmp);
- REPORTER_ASSERT(reporter, NULL != id);
- cache.unlock(id);
+ TestingKey key(i);
+ cache.add(SkNEW_ARGS(TestingRec, (key, i)));
}
}
// test the originals after all that purging
for (int i = 0; i < COUNT; ++i) {
- SkBitmap tmp;
- id = cache.findAndLock(bm[i], scale, scale, &tmp);
- if (id) {
- cache.unlock(id);
- }
+ intptr_t value;
+ (void)cache.find(TestingKey(i), TestingRec::Visitor, &value);
}
cache.setTotalByteLimit(0);
static const size_t defLimit = DIM * DIM * 4 * COUNT + 1024; // 1K slop
{
- SkScaledImageCache cache(defLimit);
+ SkResourceCache cache(defLimit);
test_cache(reporter, cache, true);
}
{
SkAutoTUnref<SkDiscardableMemoryPool> pool(
SkDiscardableMemoryPool::Create(defLimit, NULL));
gPool = pool.get();
- SkScaledImageCache cache(pool_factory);
+ SkResourceCache cache(pool_factory);
test_cache(reporter, cache, true);
}
{
- SkScaledImageCache cache(SkDiscardableMemory::Create);
+ SkResourceCache cache(SkDiscardableMemory::Create);
test_cache(reporter, cache, false);
}
}
DEF_TEST(ImageCache_doubleAdd, r) {
// Adding the same key twice should be safe.
- SkScaledImageCache cache(4096);
-
- SkBitmap original;
- original.allocN32Pixels(40, 40);
-
- SkBitmap scaled1;
- scaled1.allocN32Pixels(20, 20);
+ SkResourceCache cache(4096);
- SkBitmap scaled2;
- scaled2.allocN32Pixels(20, 20);
+ TestingKey key(1);
- SkScaledImageCache::ID* id1 = cache.addAndLock(original, 0.5f, 0.5f, scaled1);
- SkScaledImageCache::ID* id2 = cache.addAndLock(original, 0.5f, 0.5f, scaled2);
- // We don't really care if id1 == id2 as long as unlocking both works.
- cache.unlock(id1);
- cache.unlock(id2);
+ cache.add(SkNEW_ARGS(TestingRec, (key, 2)));
+ cache.add(SkNEW_ARGS(TestingRec, (key, 3)));
- SkBitmap tmp;
- // Lookup should return the value that was added last.
- SkScaledImageCache::ID* id = cache.findAndLock(original, 0.5f, 0.5f, &tmp);
- REPORTER_ASSERT(r, NULL != id);
- REPORTER_ASSERT(r, tmp.getGenerationID() == scaled2.getGenerationID());
- cache.unlock(id);
+ // Lookup can return either value.
+ intptr_t value = -1;
+ REPORTER_ASSERT(r, cache.find(key, TestingRec::Visitor, &value));
+ REPORTER_ASSERT(r, 2 == value || 3 == value);
}