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 | void* fPtr; |
| 16 | intptr_t fValue; |
| 17 | |
| 18 | TestingKey(intptr_t value) : fPtr(&gGlobalAddress), fValue(value) { |
| 19 | this->init(sizeof(fPtr) + sizeof(fValue)); |
| 20 | } |
| 21 | }; |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 22 | struct TestingRec : public SkResourceCache::Rec { |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame] | 23 | TestingRec(const TestingKey& key, uint32_t value) : fKey(key), fValue(value) {} |
reed | 0461713 | 2014-08-21 09:46:49 -0700 | [diff] [blame] | 24 | |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame] | 25 | TestingKey fKey; |
| 26 | intptr_t fValue; |
| 27 | |
| 28 | virtual const Key& getKey() const SK_OVERRIDE { return fKey; } |
| 29 | virtual size_t bytesUsed() const SK_OVERRIDE { return sizeof(fKey) + sizeof(fValue); } |
reed | c90e014 | 2014-09-15 11:39:44 -0700 | [diff] [blame^] | 30 | |
| 31 | static bool Visitor(const SkResourceCache::Rec& baseRec, void* context) { |
| 32 | const TestingRec& rec = static_cast<const TestingRec&>(baseRec); |
| 33 | intptr_t* result = (intptr_t*)context; |
| 34 | |
| 35 | *result = rec.fValue; |
| 36 | return true; |
| 37 | } |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame] | 38 | }; |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 39 | } |
| 40 | |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 41 | static const int COUNT = 10; |
| 42 | static const int DIM = 256; |
| 43 | |
reed | c90e014 | 2014-09-15 11:39:44 -0700 | [diff] [blame^] | 44 | static void test_cache(skiatest::Reporter* reporter, SkResourceCache& cache, bool testPurge) { |
reed@google.com | 772443a | 2013-12-11 15:30:24 +0000 | [diff] [blame] | 45 | for (int i = 0; i < COUNT; ++i) { |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame] | 46 | TestingKey key(i); |
reed | c90e014 | 2014-09-15 11:39:44 -0700 | [diff] [blame^] | 47 | intptr_t value = -1; |
tfarina@chromium.org | 4f3c305 | 2013-12-14 15:12:48 +0000 | [diff] [blame] | 48 | |
reed | c90e014 | 2014-09-15 11:39:44 -0700 | [diff] [blame^] | 49 | REPORTER_ASSERT(reporter, !cache.find(key, TestingRec::Visitor, &value)); |
| 50 | REPORTER_ASSERT(reporter, -1 == value); |
skia.committer@gmail.com | cf0803b | 2013-12-10 07:02:03 +0000 | [diff] [blame] | 51 | |
reed | c90e014 | 2014-09-15 11:39:44 -0700 | [diff] [blame^] | 52 | cache.add(SkNEW_ARGS(TestingRec, (key, i))); |
skia.committer@gmail.com | cf0803b | 2013-12-10 07:02:03 +0000 | [diff] [blame] | 53 | |
reed | c90e014 | 2014-09-15 11:39:44 -0700 | [diff] [blame^] | 54 | REPORTER_ASSERT(reporter, cache.find(key, TestingRec::Visitor, &value)); |
| 55 | REPORTER_ASSERT(reporter, i == value); |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 56 | } |
skia.committer@gmail.com | cf0803b | 2013-12-10 07:02:03 +0000 | [diff] [blame] | 57 | |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 58 | if (testPurge) { |
| 59 | // stress test, should trigger purges |
| 60 | for (size_t i = 0; i < COUNT * 100; ++i) { |
reed | 0461713 | 2014-08-21 09:46:49 -0700 | [diff] [blame] | 61 | TestingKey key(i); |
reed | c90e014 | 2014-09-15 11:39:44 -0700 | [diff] [blame^] | 62 | cache.add(SkNEW_ARGS(TestingRec, (key, i))); |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 63 | } |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 64 | } |
reed@google.com | 772443a | 2013-12-11 15:30:24 +0000 | [diff] [blame] | 65 | |
| 66 | // test the originals after all that purging |
| 67 | for (int i = 0; i < COUNT; ++i) { |
reed | c90e014 | 2014-09-15 11:39:44 -0700 | [diff] [blame^] | 68 | intptr_t value; |
| 69 | (void)cache.find(TestingKey(i), TestingRec::Visitor, &value); |
reed@google.com | 772443a | 2013-12-11 15:30:24 +0000 | [diff] [blame] | 70 | } |
| 71 | |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 72 | cache.setTotalByteLimit(0); |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 73 | } |
| 74 | |
reed@google.com | 772443a | 2013-12-11 15:30:24 +0000 | [diff] [blame] | 75 | #include "SkDiscardableMemoryPool.h" |
| 76 | |
| 77 | static SkDiscardableMemoryPool* gPool; |
| 78 | static SkDiscardableMemory* pool_factory(size_t bytes) { |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 79 | SkASSERT(gPool); |
reed@google.com | 772443a | 2013-12-11 15:30:24 +0000 | [diff] [blame] | 80 | return gPool->create(bytes); |
| 81 | } |
| 82 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 83 | DEF_TEST(ImageCache, reporter) { |
reed@google.com | 772443a | 2013-12-11 15:30:24 +0000 | [diff] [blame] | 84 | static const size_t defLimit = DIM * DIM * 4 * COUNT + 1024; // 1K slop |
| 85 | |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 86 | { |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 87 | SkResourceCache cache(defLimit); |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 88 | test_cache(reporter, cache, true); |
| 89 | } |
| 90 | { |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 91 | SkAutoTUnref<SkDiscardableMemoryPool> pool( |
| 92 | SkDiscardableMemoryPool::Create(defLimit, NULL)); |
| 93 | gPool = pool.get(); |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 94 | SkResourceCache cache(pool_factory); |
reed@google.com | 772443a | 2013-12-11 15:30:24 +0000 | [diff] [blame] | 95 | test_cache(reporter, cache, true); |
| 96 | } |
| 97 | { |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 98 | SkResourceCache cache(SkDiscardableMemory::Create); |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 99 | test_cache(reporter, cache, false); |
| 100 | } |
| 101 | } |
| 102 | |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 103 | DEF_TEST(ImageCache_doubleAdd, r) { |
| 104 | // Adding the same key twice should be safe. |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 105 | SkResourceCache cache(4096); |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 106 | |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame] | 107 | TestingKey key(1); |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 108 | |
reed | c90e014 | 2014-09-15 11:39:44 -0700 | [diff] [blame^] | 109 | cache.add(SkNEW_ARGS(TestingRec, (key, 2))); |
| 110 | cache.add(SkNEW_ARGS(TestingRec, (key, 3))); |
commit-bot@chromium.org | 5e4112b | 2014-03-05 13:44:18 +0000 | [diff] [blame] | 111 | |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame] | 112 | // Lookup can return either value. |
reed | c90e014 | 2014-09-15 11:39:44 -0700 | [diff] [blame^] | 113 | intptr_t value = -1; |
| 114 | REPORTER_ASSERT(r, cache.find(key, TestingRec::Visitor, &value)); |
| 115 | REPORTER_ASSERT(r, 2 == value || 3 == value); |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 116 | } |