reed@google.com | 772443a | 2013-12-11 15:30:24 +0000 | [diff] [blame] | 1 | /* |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 2 | * 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.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 8 | #include "SkDiscardableMemory.h" |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 9 | #include "SkResourceCache.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 10 | #include "Test.h" |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 11 | |
reed | 0461713 | 2014-08-21 09:46:49 -0700 | [diff] [blame] | 12 | namespace { |
| 13 | static void* gGlobalAddress; |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 14 | struct TestingKey : public SkResourceCache::Key { |
reed | 0461713 | 2014-08-21 09:46:49 -0700 | [diff] [blame] | 15 | intptr_t fValue; |
| 16 | |
reed | 7eeba25 | 2015-02-24 13:54:23 -0800 | [diff] [blame] | 17 | TestingKey(intptr_t value, uint64_t sharedID = 0) : fValue(value) { |
| 18 | this->init(&gGlobalAddress, sharedID, sizeof(fValue)); |
reed | 0461713 | 2014-08-21 09:46:49 -0700 | [diff] [blame] | 19 | } |
| 20 | }; |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 21 | struct TestingRec : public SkResourceCache::Rec { |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame] | 22 | TestingRec(const TestingKey& key, uint32_t value) : fKey(key), fValue(value) {} |
reed | 0461713 | 2014-08-21 09:46:49 -0700 | [diff] [blame] | 23 | |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame] | 24 | TestingKey fKey; |
| 25 | intptr_t fValue; |
| 26 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 27 | const Key& getKey() const override { return fKey; } |
| 28 | size_t bytesUsed() const override { return sizeof(fKey) + sizeof(fValue); } |
reed | 216b643 | 2015-08-19 12:25:40 -0700 | [diff] [blame] | 29 | const char* getCategory() const override { return "test_cache"; } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 30 | SkDiscardableMemory* diagnostic_only_getDiscardable() const override { return nullptr; } |
reed | c90e014 | 2014-09-15 11:39:44 -0700 | [diff] [blame] | 31 | |
| 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; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 35 | |
reed | c90e014 | 2014-09-15 11:39:44 -0700 | [diff] [blame] | 36 | *result = rec.fValue; |
| 37 | return true; |
| 38 | } |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame] | 39 | }; |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 40 | } |
| 41 | |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 42 | static const int COUNT = 10; |
| 43 | static const int DIM = 256; |
| 44 | |
reed | c90e014 | 2014-09-15 11:39:44 -0700 | [diff] [blame] | 45 | static void test_cache(skiatest::Reporter* reporter, SkResourceCache& cache, bool testPurge) { |
reed@google.com | 772443a | 2013-12-11 15:30:24 +0000 | [diff] [blame] | 46 | for (int i = 0; i < COUNT; ++i) { |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame] | 47 | TestingKey key(i); |
reed | c90e014 | 2014-09-15 11:39:44 -0700 | [diff] [blame] | 48 | intptr_t value = -1; |
tfarina@chromium.org | 4f3c305 | 2013-12-14 15:12:48 +0000 | [diff] [blame] | 49 | |
reed | c90e014 | 2014-09-15 11:39:44 -0700 | [diff] [blame] | 50 | REPORTER_ASSERT(reporter, !cache.find(key, TestingRec::Visitor, &value)); |
| 51 | REPORTER_ASSERT(reporter, -1 == value); |
skia.committer@gmail.com | cf0803b | 2013-12-10 07:02:03 +0000 | [diff] [blame] | 52 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 53 | cache.add(new TestingRec(key, i)); |
skia.committer@gmail.com | cf0803b | 2013-12-10 07:02:03 +0000 | [diff] [blame] | 54 | |
reed | c90e014 | 2014-09-15 11:39:44 -0700 | [diff] [blame] | 55 | REPORTER_ASSERT(reporter, cache.find(key, TestingRec::Visitor, &value)); |
| 56 | REPORTER_ASSERT(reporter, i == value); |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 57 | } |
skia.committer@gmail.com | cf0803b | 2013-12-10 07:02:03 +0000 | [diff] [blame] | 58 | |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 59 | if (testPurge) { |
| 60 | // stress test, should trigger purges |
bsalomon | 9880607 | 2014-12-12 15:11:17 -0800 | [diff] [blame] | 61 | for (int i = 0; i < COUNT * 100; ++i) { |
reed | 0461713 | 2014-08-21 09:46:49 -0700 | [diff] [blame] | 62 | TestingKey key(i); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 63 | cache.add(new TestingRec(key, i)); |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 64 | } |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 65 | } |
reed@google.com | 772443a | 2013-12-11 15:30:24 +0000 | [diff] [blame] | 66 | |
| 67 | // test the originals after all that purging |
| 68 | for (int i = 0; i < COUNT; ++i) { |
reed | c90e014 | 2014-09-15 11:39:44 -0700 | [diff] [blame] | 69 | intptr_t value; |
| 70 | (void)cache.find(TestingKey(i), TestingRec::Visitor, &value); |
reed@google.com | 772443a | 2013-12-11 15:30:24 +0000 | [diff] [blame] | 71 | } |
| 72 | |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 73 | cache.setTotalByteLimit(0); |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 74 | } |
| 75 | |
reed | 7eeba25 | 2015-02-24 13:54:23 -0800 | [diff] [blame] | 76 | static void test_cache_purge_shared_id(skiatest::Reporter* reporter, SkResourceCache& cache) { |
| 77 | for (int i = 0; i < COUNT; ++i) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 78 | TestingKey key(i, i & 1); // every other key will have a 1 for its sharedID |
| 79 | cache.add(new TestingRec(key, i)); |
reed | 7eeba25 | 2015-02-24 13:54:23 -0800 | [diff] [blame] | 80 | } |
| 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.com | 772443a | 2013-12-11 15:30:24 +0000 | [diff] [blame] | 108 | #include "SkDiscardableMemoryPool.h" |
| 109 | |
| 110 | static SkDiscardableMemoryPool* gPool; |
| 111 | static SkDiscardableMemory* pool_factory(size_t bytes) { |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 112 | SkASSERT(gPool); |
reed@google.com | 772443a | 2013-12-11 15:30:24 +0000 | [diff] [blame] | 113 | return gPool->create(bytes); |
| 114 | } |
| 115 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 116 | DEF_TEST(ImageCache, reporter) { |
reed@google.com | 772443a | 2013-12-11 15:30:24 +0000 | [diff] [blame] | 117 | static const size_t defLimit = DIM * DIM * 4 * COUNT + 1024; // 1K slop |
| 118 | |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 119 | { |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 120 | SkResourceCache cache(defLimit); |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 121 | test_cache(reporter, cache, true); |
| 122 | } |
| 123 | { |
Hal Canary | 788c3c4 | 2017-04-25 08:58:57 -0400 | [diff] [blame] | 124 | sk_sp<SkDiscardableMemoryPool> pool(SkDiscardableMemoryPool::Make(defLimit)); |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 125 | gPool = pool.get(); |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 126 | SkResourceCache cache(pool_factory); |
reed@google.com | 772443a | 2013-12-11 15:30:24 +0000 | [diff] [blame] | 127 | test_cache(reporter, cache, true); |
| 128 | } |
| 129 | { |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 130 | SkResourceCache cache(SkDiscardableMemory::Create); |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 131 | test_cache(reporter, cache, false); |
| 132 | } |
reed | 7eeba25 | 2015-02-24 13:54:23 -0800 | [diff] [blame] | 133 | { |
| 134 | SkResourceCache cache(defLimit); |
| 135 | test_cache_purge_shared_id(reporter, cache); |
| 136 | } |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 137 | } |
| 138 | |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 139 | DEF_TEST(ImageCache_doubleAdd, r) { |
| 140 | // Adding the same key twice should be safe. |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 141 | SkResourceCache cache(4096); |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 142 | |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame] | 143 | TestingKey key(1); |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 144 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 145 | cache.add(new TestingRec(key, 2)); |
| 146 | cache.add(new TestingRec(key, 3)); |
commit-bot@chromium.org | 5e4112b | 2014-03-05 13:44:18 +0000 | [diff] [blame] | 147 | |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame] | 148 | // Lookup can return either value. |
reed | c90e014 | 2014-09-15 11:39:44 -0700 | [diff] [blame] | 149 | intptr_t value = -1; |
| 150 | REPORTER_ASSERT(r, cache.find(key, TestingRec::Visitor, &value)); |
| 151 | REPORTER_ASSERT(r, 2 == value || 3 == value); |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 152 | } |