aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests
diff options
context:
space:
mode:
authorGravatar reed <reed@chromium.org>2015-02-24 13:54:23 -0800
committerGravatar Commit bot <commit-bot@chromium.org>2015-02-24 13:54:23 -0800
commit7eeba2587760a0802fd2b90765b4fd0e5e895375 (patch)
treedad9caa7dde038c52728034710c3d6d4926bfff2 /tests
parent4b583ac54b92ad6e7685bdfa2b68e7dd11ba0d28 (diff)
Notify resource caches when pixelref genID goes stale
patch from issue 954443002 at patchset 40001 (http://crrev.com/954443002#ps40001) BUG=skia: Review URL: https://codereview.chromium.org/950363002
Diffstat (limited to 'tests')
-rw-r--r--tests/ImageCacheTest.cpp40
-rw-r--r--tests/SkResourceCacheTest.cpp64
2 files changed, 102 insertions, 2 deletions
diff --git a/tests/ImageCacheTest.cpp b/tests/ImageCacheTest.cpp
index a4cfa73989..45dee92a2f 100644
--- a/tests/ImageCacheTest.cpp
+++ b/tests/ImageCacheTest.cpp
@@ -14,8 +14,8 @@ static void* gGlobalAddress;
struct TestingKey : public SkResourceCache::Key {
intptr_t fValue;
- TestingKey(intptr_t value) : fValue(value) {
- this->init(&gGlobalAddress, sizeof(fValue));
+ TestingKey(intptr_t value, uint64_t sharedID = 0) : fValue(value) {
+ this->init(&gGlobalAddress, sharedID, sizeof(fValue));
}
};
struct TestingRec : public SkResourceCache::Rec {
@@ -71,6 +71,38 @@ static void test_cache(skiatest::Reporter* reporter, SkResourceCache& cache, boo
cache.setTotalByteLimit(0);
}
+static void test_cache_purge_shared_id(skiatest::Reporter* reporter, SkResourceCache& cache) {
+ for (int i = 0; i < COUNT; ++i) {
+ TestingKey key(i, i & 1); // every other key will have a 1 for its sharedID
+ cache.add(SkNEW_ARGS(TestingRec, (key, i)));
+ }
+
+ // 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);
+ }
+ }
+}
+
#include "SkDiscardableMemoryPool.h"
static SkDiscardableMemoryPool* gPool;
@@ -97,6 +129,10 @@ DEF_TEST(ImageCache, reporter) {
SkResourceCache cache(SkDiscardableMemory::Create);
test_cache(reporter, cache, false);
}
+ {
+ SkResourceCache cache(defLimit);
+ test_cache_purge_shared_id(reporter, cache);
+ }
}
DEF_TEST(ImageCache_doubleAdd, r) {
diff --git a/tests/SkResourceCacheTest.cpp b/tests/SkResourceCacheTest.cpp
index 179e771ed8..626c837361 100644
--- a/tests/SkResourceCacheTest.cpp
+++ b/tests/SkResourceCacheTest.cpp
@@ -156,6 +156,14 @@ static void test_mipmapcache(skiatest::Reporter* reporter, SkResourceCache* cach
mipmap = SkMipMapCache::AddAndRef(src, cache);
REPORTER_ASSERT(reporter, mipmap);
+
+ {
+ const SkMipMap* mm = SkMipMapCache::FindAndRef(src, cache);
+ REPORTER_ASSERT(reporter, mm);
+ REPORTER_ASSERT(reporter, mm == mipmap);
+ mm->unref();
+ }
+
check_data(reporter, mipmap, 2, kInCache, kLocked);
mipmap->unref();
@@ -173,6 +181,60 @@ static void test_mipmapcache(skiatest::Reporter* reporter, SkResourceCache* cach
mipmap->unref();
}
+static void test_mipmap_notify(skiatest::Reporter* reporter, SkResourceCache* cache) {
+ const int N = 3;
+ SkBitmap src[N];
+ for (int i = 0; i < N; ++i) {
+ src[i].allocN32Pixels(5, 5);
+ src[i].setImmutable();
+ SkMipMapCache::AddAndRef(src[i], cache)->unref();
+ }
+
+ for (int i = 0; i < N; ++i) {
+ const SkMipMap* mipmap = SkMipMapCache::FindAndRef(src[i], cache);
+ if (cache) {
+ // if cache is null, we're working on the global cache, and other threads might purge
+ // it, making this check fragile.
+ REPORTER_ASSERT(reporter, mipmap);
+ }
+ SkSafeUnref(mipmap);
+
+ src[i].reset(); // delete the underlying pixelref, which *should* remove us from the cache
+
+ mipmap = SkMipMapCache::FindAndRef(src[i], cache);
+ REPORTER_ASSERT(reporter, !mipmap);
+ }
+}
+
+static void test_bitmap_notify(skiatest::Reporter* reporter, SkResourceCache* cache) {
+ const SkIRect subset = SkIRect::MakeWH(5, 5);
+ const int N = 3;
+ SkBitmap src[N], dst[N];
+ for (int i = 0; i < N; ++i) {
+ src[i].allocN32Pixels(5, 5);
+ src[i].setImmutable();
+ dst[i].allocN32Pixels(5, 5);
+ dst[i].setImmutable();
+ SkBitmapCache::Add(src[i].getGenerationID(), subset, dst[i], cache);
+ }
+
+ for (int i = 0; i < N; ++i) {
+ const uint32_t genID = src[i].getGenerationID();
+ SkBitmap result;
+ bool found = SkBitmapCache::Find(genID, subset, &result, cache);
+ if (cache) {
+ // if cache is null, we're working on the global cache, and other threads might purge
+ // it, making this check fragile.
+ REPORTER_ASSERT(reporter, found);
+ }
+
+ src[i].reset(); // delete the underlying pixelref, which *should* remove us from the cache
+
+ found = SkBitmapCache::Find(genID, subset, &result, cache);
+ REPORTER_ASSERT(reporter, !found);
+ }
+}
+
DEF_TEST(BitmapCache_discarded_bitmap, reporter) {
SkResourceCache::DiscardableFactory factory = SkResourceCache::GetDiscardableFactory();
SkBitmap::Allocator* allocator = SkBitmapCache::GetAllocator();
@@ -219,4 +281,6 @@ DEF_TEST(BitmapCache_discarded_bitmap, reporter) {
REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm, cache));
test_mipmapcache(reporter, cache);
+ test_bitmap_notify(reporter, cache);
+ test_mipmap_notify(reporter, cache);
}