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@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 9 | #include "SkScaledImageCache.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; |
| 14 | struct TestingKey : public SkScaledImageCache::Key { |
| 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 | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame^] | 22 | struct TestingRec : public SkScaledImageCache::Rec { |
| 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); } |
| 30 | }; |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 31 | } |
| 32 | |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 33 | static const int COUNT = 10; |
| 34 | static const int DIM = 256; |
| 35 | |
| 36 | static void test_cache(skiatest::Reporter* reporter, SkScaledImageCache& cache, |
| 37 | bool testPurge) { |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame^] | 38 | SkScaledImageCache::ID id; |
skia.committer@gmail.com | cf0803b | 2013-12-10 07:02:03 +0000 | [diff] [blame] | 39 | |
reed@google.com | 772443a | 2013-12-11 15:30:24 +0000 | [diff] [blame] | 40 | for (int i = 0; i < COUNT; ++i) { |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame^] | 41 | TestingKey key(i); |
tfarina@chromium.org | 4f3c305 | 2013-12-14 15:12:48 +0000 | [diff] [blame] | 42 | |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame^] | 43 | const TestingRec* rec = (const TestingRec*)cache.findAndLock(key); |
| 44 | REPORTER_ASSERT(reporter, NULL == rec); |
skia.committer@gmail.com | cf0803b | 2013-12-10 07:02:03 +0000 | [diff] [blame] | 45 | |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame^] | 46 | TestingRec* newRec = SkNEW_ARGS(TestingRec, (key, i)); |
| 47 | const TestingRec* addedRec = (const TestingRec*)cache.addAndLock(newRec); |
| 48 | REPORTER_ASSERT(reporter, NULL != addedRec); |
skia.committer@gmail.com | cf0803b | 2013-12-10 07:02:03 +0000 | [diff] [blame] | 49 | |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame^] | 50 | const TestingRec* foundRec = (const TestingRec*)cache.findAndLock(key); |
| 51 | REPORTER_ASSERT(reporter, foundRec == addedRec); |
| 52 | REPORTER_ASSERT(reporter, foundRec->fValue == i); |
| 53 | cache.unlock(foundRec); |
| 54 | cache.unlock(addedRec); |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 55 | } |
skia.committer@gmail.com | cf0803b | 2013-12-10 07:02:03 +0000 | [diff] [blame] | 56 | |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 57 | if (testPurge) { |
| 58 | // stress test, should trigger purges |
| 59 | for (size_t i = 0; i < COUNT * 100; ++i) { |
reed | 0461713 | 2014-08-21 09:46:49 -0700 | [diff] [blame] | 60 | TestingKey key(i); |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame^] | 61 | SkScaledImageCache::ID id = cache.addAndLock(SkNEW_ARGS(TestingRec, (key, i))); |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 62 | REPORTER_ASSERT(reporter, NULL != id); |
| 63 | cache.unlock(id); |
| 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 | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame^] | 69 | id = cache.findAndLock(TestingKey(i)); |
reed@google.com | 772443a | 2013-12-11 15:30:24 +0000 | [diff] [blame] | 70 | if (id) { |
| 71 | cache.unlock(id); |
| 72 | } |
| 73 | } |
| 74 | |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 75 | cache.setTotalByteLimit(0); |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 76 | } |
| 77 | |
reed@google.com | 772443a | 2013-12-11 15:30:24 +0000 | [diff] [blame] | 78 | #include "SkDiscardableMemoryPool.h" |
| 79 | |
| 80 | static SkDiscardableMemoryPool* gPool; |
| 81 | static SkDiscardableMemory* pool_factory(size_t bytes) { |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 82 | SkASSERT(gPool); |
reed@google.com | 772443a | 2013-12-11 15:30:24 +0000 | [diff] [blame] | 83 | return gPool->create(bytes); |
| 84 | } |
| 85 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 86 | DEF_TEST(ImageCache, reporter) { |
reed@google.com | 772443a | 2013-12-11 15:30:24 +0000 | [diff] [blame] | 87 | static const size_t defLimit = DIM * DIM * 4 * COUNT + 1024; // 1K slop |
| 88 | |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 89 | { |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 90 | SkScaledImageCache cache(defLimit); |
| 91 | test_cache(reporter, cache, true); |
| 92 | } |
| 93 | { |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 94 | SkAutoTUnref<SkDiscardableMemoryPool> pool( |
| 95 | SkDiscardableMemoryPool::Create(defLimit, NULL)); |
| 96 | gPool = pool.get(); |
reed@google.com | 772443a | 2013-12-11 15:30:24 +0000 | [diff] [blame] | 97 | SkScaledImageCache cache(pool_factory); |
| 98 | test_cache(reporter, cache, true); |
| 99 | } |
| 100 | { |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 101 | SkScaledImageCache cache(SkDiscardableMemory::Create); |
| 102 | test_cache(reporter, cache, false); |
| 103 | } |
| 104 | } |
| 105 | |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 106 | DEF_TEST(ImageCache_doubleAdd, r) { |
| 107 | // Adding the same key twice should be safe. |
commit-bot@chromium.org | 5e4112b | 2014-03-05 13:44:18 +0000 | [diff] [blame] | 108 | SkScaledImageCache cache(4096); |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 109 | |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame^] | 110 | TestingKey key(1); |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 111 | |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame^] | 112 | SkScaledImageCache::ID id1 = cache.addAndLock(SkNEW_ARGS(TestingRec, (key, 2))); |
| 113 | SkScaledImageCache::ID id2 = cache.addAndLock(SkNEW_ARGS(TestingRec, (key, 3))); |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 114 | // We don't really care if id1 == id2 as long as unlocking both works. |
| 115 | cache.unlock(id1); |
| 116 | cache.unlock(id2); |
commit-bot@chromium.org | 5e4112b | 2014-03-05 13:44:18 +0000 | [diff] [blame] | 117 | |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame^] | 118 | // Lookup can return either value. |
| 119 | const TestingRec* rec = (const TestingRec*)cache.findAndLock(key); |
| 120 | REPORTER_ASSERT(r, NULL != rec); |
| 121 | REPORTER_ASSERT(r, 2 == rec->fValue || 3 == rec->fValue); |
| 122 | cache.unlock(rec); |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 123 | } |