blob: 457df911c1b9d32bb46c62dac25a6116573fe5a3 [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 {
Hal Canary788c3c42017-04-25 08:58:57 -0400124 sk_sp<SkDiscardableMemoryPool> pool(SkDiscardableMemoryPool::Make(defLimit));
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +0000125 gPool = pool.get();
reed011f39a2014-08-28 13:35:23 -0700126 SkResourceCache cache(pool_factory);
reed@google.com772443a2013-12-11 15:30:24 +0000127 test_cache(reporter, cache, true);
128 }
129 {
reed011f39a2014-08-28 13:35:23 -0700130 SkResourceCache cache(SkDiscardableMemory::Create);
reed@google.come4eb1222013-12-09 22:29:30 +0000131 test_cache(reporter, cache, false);
132 }
reed7eeba252015-02-24 13:54:23 -0800133 {
134 SkResourceCache cache(defLimit);
135 test_cache_purge_shared_id(reporter, cache);
136 }
reed@google.come4eb1222013-12-09 22:29:30 +0000137}
138
rmistry@google.comd6bab022013-12-02 13:50:38 +0000139DEF_TEST(ImageCache_doubleAdd, r) {
140 // Adding the same key twice should be safe.
reed011f39a2014-08-28 13:35:23 -0700141 SkResourceCache cache(4096);
rmistry@google.comd6bab022013-12-02 13:50:38 +0000142
reed680fb9e2014-08-26 09:08:04 -0700143 TestingKey key(1);
rmistry@google.comd6bab022013-12-02 13:50:38 +0000144
halcanary385fe4d2015-08-26 13:07:48 -0700145 cache.add(new TestingRec(key, 2));
146 cache.add(new TestingRec(key, 3));
commit-bot@chromium.org5e4112b2014-03-05 13:44:18 +0000147
reed680fb9e2014-08-26 09:08:04 -0700148 // Lookup can return either value.
reedc90e0142014-09-15 11:39:44 -0700149 intptr_t value = -1;
150 REPORTER_ASSERT(r, cache.find(key, TestingRec::Visitor, &value));
151 REPORTER_ASSERT(r, 2 == value || 3 == value);
rmistry@google.comd6bab022013-12-02 13:50:38 +0000152}