aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests
diff options
context:
space:
mode:
authorGravatar reed <reed@google.com>2014-10-02 13:47:08 -0700
committerGravatar Commit bot <commit-bot@chromium.org>2014-10-02 13:47:08 -0700
commit92561a0b99ad6c08ab7a11dd1872f028199392e9 (patch)
tree636cd4bbf4e131a384bdcf18d0172a86b20fde73 /tests
parentd51ce44d18cb28df568f817b05478d24ef21c1a7 (diff)
Add SkCachedData and use it for SkMipMap
Diffstat (limited to 'tests')
-rw-r--r--tests/CachedDataTest.cpp95
-rw-r--r--tests/MipMapTest.cpp2
-rw-r--r--tests/SkResourceCacheTest.cpp51
3 files changed, 147 insertions, 1 deletions
diff --git a/tests/CachedDataTest.cpp b/tests/CachedDataTest.cpp
new file mode 100644
index 0000000000..f65a46b770
--- /dev/null
+++ b/tests/CachedDataTest.cpp
@@ -0,0 +1,95 @@
+/*
+ * Copyright 2014 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "SkCachedData.h"
+#include "SkDiscardableMemoryPool.h"
+#include "Test.h"
+
+enum LockedState {
+ kUnlocked,
+ kLocked,
+};
+
+enum CachedState {
+ kNotInCache,
+ kInCache,
+};
+
+static void check_data(skiatest::Reporter* reporter, 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));
+ REPORTER_ASSERT(reporter, data->testing_only_isLocked() == (lockedState == kLocked));
+}
+
+static SkCachedData* make_data(size_t size, SkDiscardableMemoryPool* pool) {
+ if (pool) {
+ SkDiscardableMemory* dm = pool->create(size);
+ // the pool "can" return null, but it shouldn't in these controlled conditions
+ SK_ALWAYSBREAK(dm);
+ return SkNEW_ARGS(SkCachedData, (size, dm));
+ } else {
+ return SkNEW_ARGS(SkCachedData, (sk_malloc_throw(size), size));
+ }
+}
+
+// returns with the data locked by client and cache
+static SkCachedData* test_locking(skiatest::Reporter* reporter,
+ size_t size, SkDiscardableMemoryPool* pool) {
+ SkCachedData* data = make_data(size, pool);
+
+ memset(data->writable_data(), 0x80, size); // just to use writable_data()
+
+ check_data(reporter, data, 1, kNotInCache, kLocked);
+
+ data->ref();
+ check_data(reporter, data, 2, kNotInCache, kLocked);
+ data->unref();
+ check_data(reporter, data, 1, kNotInCache, kLocked);
+
+ data->attachToCacheAndRef();
+ check_data(reporter, data, 2, kInCache, kLocked);
+
+ data->unref();
+ check_data(reporter, data, 1, kInCache, kUnlocked);
+
+ data->ref();
+ check_data(reporter, data, 2, kInCache, kLocked);
+
+ return data;
+}
+
+/*
+ * SkCachedData behaves differently (regarding its locked/unlocked state) depending on
+ * when it is in the cache or not. Being in the cache is signaled by calling attachToCacheAndRef()
+ * instead of ref(). (and balanced by detachFromCacheAndUnref).
+ *
+ * Thus, among other things, we test the end-of-life behavior when the client is the last owner
+ * and when the cache is.
+ */
+DEF_TEST(CachedData, reporter) {
+ SkAutoTUnref<SkDiscardableMemoryPool> pool(SkDiscardableMemoryPool::Create(1000));
+
+ for (int useDiscardable = 0; useDiscardable <= 1; ++useDiscardable) {
+ const size_t size = 100;
+
+ // test with client as last owner
+ SkCachedData* data = test_locking(reporter, size, useDiscardable ? pool.get() : NULL);
+ check_data(reporter, data, 2, kInCache, kLocked);
+ data->detachFromCacheAndUnref();
+ check_data(reporter, data, 1, kNotInCache, kLocked);
+ data->unref();
+
+ // test with cache as last owner
+ data = test_locking(reporter, size, useDiscardable ? pool.get() : NULL);
+ check_data(reporter, data, 2, kInCache, kLocked);
+ data->unref();
+ check_data(reporter, data, 1, kInCache, kUnlocked);
+ data->detachFromCacheAndUnref();
+ }
+}
+
diff --git a/tests/MipMapTest.cpp b/tests/MipMapTest.cpp
index 33f467244b..c8396effa6 100644
--- a/tests/MipMapTest.cpp
+++ b/tests/MipMapTest.cpp
@@ -25,7 +25,7 @@ DEF_TEST(MipMap, reporter) {
for (int i = 0; i < 500; ++i) {
make_bitmap(&bm, rand);
- SkAutoTUnref<SkMipMap> mm(SkMipMap::Build(bm));
+ SkAutoTUnref<SkMipMap> mm(SkMipMap::Build(bm, NULL));
REPORTER_ASSERT(reporter, !mm->extractLevel(SK_Scalar1, NULL));
REPORTER_ASSERT(reporter, !mm->extractLevel(SK_Scalar1 * 2, NULL));
diff --git a/tests/SkResourceCacheTest.cpp b/tests/SkResourceCacheTest.cpp
index f13476a5a3..0e941758ee 100644
--- a/tests/SkResourceCacheTest.cpp
+++ b/tests/SkResourceCacheTest.cpp
@@ -121,6 +121,55 @@ DEF_TEST(BitmapCache_add_rect, reporter) {
REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm, cache));
}
+#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();
+}
+
DEF_TEST(BitmapCache_discarded_bitmap, reporter) {
SkResourceCache::DiscardableFactory factory = SkResourceCache::GetDiscardableFactory();
SkBitmap::Allocator* allocator = SkBitmapCache::GetAllocator();
@@ -165,4 +214,6 @@ DEF_TEST(BitmapCache_discarded_bitmap, reporter) {
// We can add the bitmap back to the cache and find it again.
REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.getGenerationID(), rect, cachedBitmap, cache));
REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm, cache));
+
+ test_mipmapcache(reporter, cache);
}