blob: c2cf2d999989f0e54288312a3f37dd65265c8247 [file] [log] [blame]
reed@google.com772443a2013-12-11 15:30:24 +00001 /*
reed@google.com602a1d72013-07-23 19:13:54 +00002 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
reed@google.come4eb1222013-12-09 22:29:30 +00008#include "SkDiscardableMemory.h"
reed011f39a2014-08-28 13:35:23 -07009#include "SkResourceCache.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000010#include "Test.h"
reed@google.com602a1d72013-07-23 19:13:54 +000011
reed04617132014-08-21 09:46:49 -070012namespace {
13static void* gGlobalAddress;
reed011f39a2014-08-28 13:35:23 -070014struct TestingKey : public SkResourceCache::Key {
reed04617132014-08-21 09:46:49 -070015 intptr_t fValue;
16
reed7eeba252015-02-24 13:54:23 -080017 TestingKey(intptr_t value, uint64_t sharedID = 0) : fValue(value) {
18 this->init(&gGlobalAddress, sharedID, sizeof(fValue));
reed04617132014-08-21 09:46:49 -070019 }
20};
reed011f39a2014-08-28 13:35:23 -070021struct TestingRec : public SkResourceCache::Rec {
reed680fb9e2014-08-26 09:08:04 -070022 TestingRec(const TestingKey& key, uint32_t value) : fKey(key), fValue(value) {}
reed04617132014-08-21 09:46:49 -070023
reed680fb9e2014-08-26 09:08:04 -070024 TestingKey fKey;
25 intptr_t fValue;
26
mtklein36352bf2015-03-25 18:17:31 -070027 const Key& getKey() const override { return fKey; }
28 size_t bytesUsed() const override { return sizeof(fKey) + sizeof(fValue); }
reed216b6432015-08-19 12:25:40 -070029 const char* getCategory() const override { return "test_cache"; }
halcanary96fcdcc2015-08-27 07:41:13 -070030 SkDiscardableMemory* diagnostic_only_getDiscardable() const override { return nullptr; }
reedc90e0142014-09-15 11:39:44 -070031
32 static bool Visitor(const SkResourceCache::Rec& baseRec, void* context) {
33 const TestingRec& rec = static_cast<const TestingRec&>(baseRec);
34 intptr_t* result = (intptr_t*)context;
halcanary9d524f22016-03-29 09:03:52 -070035
reedc90e0142014-09-15 11:39:44 -070036 *result = rec.fValue;
37 return true;
38 }
reed680fb9e2014-08-26 09:08:04 -070039};
reed@google.com602a1d72013-07-23 19:13:54 +000040}
41
reed@google.come4eb1222013-12-09 22:29:30 +000042static const int COUNT = 10;
43static const int DIM = 256;
44
reedc90e0142014-09-15 11:39:44 -070045static void test_cache(skiatest::Reporter* reporter, SkResourceCache& cache, bool testPurge) {
reed@google.com772443a2013-12-11 15:30:24 +000046 for (int i = 0; i < COUNT; ++i) {
reed680fb9e2014-08-26 09:08:04 -070047 TestingKey key(i);
reedc90e0142014-09-15 11:39:44 -070048 intptr_t value = -1;
tfarina@chromium.org4f3c3052013-12-14 15:12:48 +000049
reedc90e0142014-09-15 11:39:44 -070050 REPORTER_ASSERT(reporter, !cache.find(key, TestingRec::Visitor, &value));
51 REPORTER_ASSERT(reporter, -1 == value);
skia.committer@gmail.comcf0803b2013-12-10 07:02:03 +000052
halcanary385fe4d2015-08-26 13:07:48 -070053 cache.add(new TestingRec(key, i));
skia.committer@gmail.comcf0803b2013-12-10 07:02:03 +000054
reedc90e0142014-09-15 11:39:44 -070055 REPORTER_ASSERT(reporter, cache.find(key, TestingRec::Visitor, &value));
56 REPORTER_ASSERT(reporter, i == value);
reed@google.com602a1d72013-07-23 19:13:54 +000057 }
skia.committer@gmail.comcf0803b2013-12-10 07:02:03 +000058
reed@google.come4eb1222013-12-09 22:29:30 +000059 if (testPurge) {
60 // stress test, should trigger purges
bsalomon98806072014-12-12 15:11:17 -080061 for (int i = 0; i < COUNT * 100; ++i) {
reed04617132014-08-21 09:46:49 -070062 TestingKey key(i);
halcanary385fe4d2015-08-26 13:07:48 -070063 cache.add(new TestingRec(key, i));
reed@google.come4eb1222013-12-09 22:29:30 +000064 }
reed@google.com602a1d72013-07-23 19:13:54 +000065 }
reed@google.com772443a2013-12-11 15:30:24 +000066
67 // test the originals after all that purging
68 for (int i = 0; i < COUNT; ++i) {
reedc90e0142014-09-15 11:39:44 -070069 intptr_t value;
70 (void)cache.find(TestingKey(i), TestingRec::Visitor, &value);
reed@google.com772443a2013-12-11 15:30:24 +000071 }
72
halcanary805ef152014-07-17 06:58:01 -070073 cache.setTotalByteLimit(0);
reed@google.com602a1d72013-07-23 19:13:54 +000074}
75
reed7eeba252015-02-24 13:54:23 -080076static void test_cache_purge_shared_id(skiatest::Reporter* reporter, SkResourceCache& cache) {
77 for (int i = 0; i < COUNT; ++i) {
halcanary385fe4d2015-08-26 13:07:48 -070078 TestingKey key(i, i & 1); // every other key will have a 1 for its sharedID
79 cache.add(new TestingRec(key, i));
reed7eeba252015-02-24 13:54:23 -080080 }
81
82 // Ensure that everyone is present
83 for (int i = 0; i < COUNT; ++i) {
84 TestingKey key(i, i & 1); // every other key will have a 1 for its sharedID
85 intptr_t value = -1;
86
87 REPORTER_ASSERT(reporter, cache.find(key, TestingRec::Visitor, &value));
88 REPORTER_ASSERT(reporter, value == i);
89 }
90
91 // Now purge the ones that had a non-zero sharedID (the odd-indexed ones)
92 cache.purgeSharedID(1);
93
94 // Ensure that only the even ones are still present
95 for (int i = 0; i < COUNT; ++i) {
96 TestingKey key(i, i & 1); // every other key will have a 1 for its sharedID
97 intptr_t value = -1;
98
99 if (i & 1) {
100 REPORTER_ASSERT(reporter, !cache.find(key, TestingRec::Visitor, &value));
101 } else {
102 REPORTER_ASSERT(reporter, cache.find(key, TestingRec::Visitor, &value));
103 REPORTER_ASSERT(reporter, value == i);
104 }
105 }
106}
107
reed@google.com772443a2013-12-11 15:30:24 +0000108#include "SkDiscardableMemoryPool.h"
109
110static SkDiscardableMemoryPool* gPool;
111static SkDiscardableMemory* pool_factory(size_t bytes) {
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +0000112 SkASSERT(gPool);
reed@google.com772443a2013-12-11 15:30:24 +0000113 return gPool->create(bytes);
114}
115
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000116DEF_TEST(ImageCache, reporter) {
reed@google.com772443a2013-12-11 15:30:24 +0000117 static const size_t defLimit = DIM * DIM * 4 * COUNT + 1024; // 1K slop
118
reed@google.come4eb1222013-12-09 22:29:30 +0000119 {
reed011f39a2014-08-28 13:35:23 -0700120 SkResourceCache cache(defLimit);
reed@google.come4eb1222013-12-09 22:29:30 +0000121 test_cache(reporter, cache, true);
122 }
123 {
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +0000124 SkAutoTUnref<SkDiscardableMemoryPool> pool(
halcanary96fcdcc2015-08-27 07:41:13 -0700125 SkDiscardableMemoryPool::Create(defLimit, nullptr));
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +0000126 gPool = pool.get();
reed011f39a2014-08-28 13:35:23 -0700127 SkResourceCache cache(pool_factory);
reed@google.com772443a2013-12-11 15:30:24 +0000128 test_cache(reporter, cache, true);
129 }
130 {
reed011f39a2014-08-28 13:35:23 -0700131 SkResourceCache cache(SkDiscardableMemory::Create);
reed@google.come4eb1222013-12-09 22:29:30 +0000132 test_cache(reporter, cache, false);
133 }
reed7eeba252015-02-24 13:54:23 -0800134 {
135 SkResourceCache cache(defLimit);
136 test_cache_purge_shared_id(reporter, cache);
137 }
reed@google.come4eb1222013-12-09 22:29:30 +0000138}
139
rmistry@google.comd6bab022013-12-02 13:50:38 +0000140DEF_TEST(ImageCache_doubleAdd, r) {
141 // Adding the same key twice should be safe.
reed011f39a2014-08-28 13:35:23 -0700142 SkResourceCache cache(4096);
rmistry@google.comd6bab022013-12-02 13:50:38 +0000143
reed680fb9e2014-08-26 09:08:04 -0700144 TestingKey key(1);
rmistry@google.comd6bab022013-12-02 13:50:38 +0000145
halcanary385fe4d2015-08-26 13:07:48 -0700146 cache.add(new TestingRec(key, 2));
147 cache.add(new TestingRec(key, 3));
commit-bot@chromium.org5e4112b2014-03-05 13:44:18 +0000148
reed680fb9e2014-08-26 09:08:04 -0700149 // Lookup can return either value.
reedc90e0142014-09-15 11:39:44 -0700150 intptr_t value = -1;
151 REPORTER_ASSERT(r, cache.find(key, TestingRec::Visitor, &value));
152 REPORTER_ASSERT(r, 2 == value || 3 == value);
rmistry@google.comd6bab022013-12-02 13:50:38 +0000153}