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 | }; |
| 22 | } |
| 23 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 24 | static void make_bm(SkBitmap* bm, int w, int h) { |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 25 | bm->allocN32Pixels(w, h); |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 26 | } |
| 27 | |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 28 | static const int COUNT = 10; |
| 29 | static const int DIM = 256; |
| 30 | |
| 31 | static void test_cache(skiatest::Reporter* reporter, SkScaledImageCache& cache, |
| 32 | bool testPurge) { |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 33 | SkScaledImageCache::ID* id; |
skia.committer@gmail.com | cf0803b | 2013-12-10 07:02:03 +0000 | [diff] [blame] | 34 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 35 | SkBitmap bm[COUNT]; |
skia.committer@gmail.com | cf0803b | 2013-12-10 07:02:03 +0000 | [diff] [blame] | 36 | |
reed@google.com | 772443a | 2013-12-11 15:30:24 +0000 | [diff] [blame] | 37 | for (int i = 0; i < COUNT; ++i) { |
| 38 | make_bm(&bm[i], DIM, DIM); |
| 39 | } |
tfarina@chromium.org | 4f3c305 | 2013-12-14 15:12:48 +0000 | [diff] [blame] | 40 | |
reed@google.com | 0e66162 | 2013-07-23 19:27:48 +0000 | [diff] [blame] | 41 | for (int i = 0; i < COUNT; ++i) { |
reed | 0461713 | 2014-08-21 09:46:49 -0700 | [diff] [blame^] | 42 | TestingKey key(bm[i].getGenerationID()); |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 43 | SkBitmap tmp; |
skia.committer@gmail.com | cf0803b | 2013-12-10 07:02:03 +0000 | [diff] [blame] | 44 | |
reed | 0461713 | 2014-08-21 09:46:49 -0700 | [diff] [blame^] | 45 | SkScaledImageCache::ID* id = cache.findAndLock(key, &tmp); |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 46 | REPORTER_ASSERT(reporter, NULL == id); |
skia.committer@gmail.com | cf0803b | 2013-12-10 07:02:03 +0000 | [diff] [blame] | 47 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 48 | make_bm(&tmp, DIM, DIM); |
reed | 0461713 | 2014-08-21 09:46:49 -0700 | [diff] [blame^] | 49 | id = cache.addAndLock(key, tmp); |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 50 | REPORTER_ASSERT(reporter, NULL != id); |
skia.committer@gmail.com | cf0803b | 2013-12-10 07:02:03 +0000 | [diff] [blame] | 51 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 52 | SkBitmap tmp2; |
reed | 0461713 | 2014-08-21 09:46:49 -0700 | [diff] [blame^] | 53 | SkScaledImageCache::ID* id2 = cache.findAndLock(key, &tmp2); |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 54 | REPORTER_ASSERT(reporter, id == id2); |
| 55 | REPORTER_ASSERT(reporter, tmp.pixelRef() == tmp2.pixelRef()); |
| 56 | REPORTER_ASSERT(reporter, tmp.width() == tmp2.width()); |
| 57 | REPORTER_ASSERT(reporter, tmp.height() == tmp2.height()); |
| 58 | cache.unlock(id2); |
skia.committer@gmail.com | cf0803b | 2013-12-10 07:02:03 +0000 | [diff] [blame] | 59 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 60 | cache.unlock(id); |
| 61 | } |
skia.committer@gmail.com | cf0803b | 2013-12-10 07:02:03 +0000 | [diff] [blame] | 62 | |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 63 | if (testPurge) { |
| 64 | // stress test, should trigger purges |
| 65 | for (size_t i = 0; i < COUNT * 100; ++i) { |
reed | 0461713 | 2014-08-21 09:46:49 -0700 | [diff] [blame^] | 66 | TestingKey key(i); |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 67 | SkBitmap tmp; |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 68 | make_bm(&tmp, DIM, DIM); |
reed@google.com | 772443a | 2013-12-11 15:30:24 +0000 | [diff] [blame] | 69 | |
reed | 0461713 | 2014-08-21 09:46:49 -0700 | [diff] [blame^] | 70 | SkScaledImageCache::ID* id = cache.addAndLock(key, tmp); |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 71 | REPORTER_ASSERT(reporter, NULL != id); |
| 72 | cache.unlock(id); |
| 73 | } |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 74 | } |
reed@google.com | 772443a | 2013-12-11 15:30:24 +0000 | [diff] [blame] | 75 | |
| 76 | // test the originals after all that purging |
| 77 | for (int i = 0; i < COUNT; ++i) { |
reed | 0461713 | 2014-08-21 09:46:49 -0700 | [diff] [blame^] | 78 | TestingKey key(bm[i].getGenerationID()); |
reed@google.com | 772443a | 2013-12-11 15:30:24 +0000 | [diff] [blame] | 79 | SkBitmap tmp; |
reed | 0461713 | 2014-08-21 09:46:49 -0700 | [diff] [blame^] | 80 | id = cache.findAndLock(key, &tmp); |
reed@google.com | 772443a | 2013-12-11 15:30:24 +0000 | [diff] [blame] | 81 | if (id) { |
| 82 | cache.unlock(id); |
| 83 | } |
| 84 | } |
| 85 | |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 86 | cache.setTotalByteLimit(0); |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 87 | } |
| 88 | |
reed@google.com | 772443a | 2013-12-11 15:30:24 +0000 | [diff] [blame] | 89 | #include "SkDiscardableMemoryPool.h" |
| 90 | |
| 91 | static SkDiscardableMemoryPool* gPool; |
| 92 | static SkDiscardableMemory* pool_factory(size_t bytes) { |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 93 | SkASSERT(gPool); |
reed@google.com | 772443a | 2013-12-11 15:30:24 +0000 | [diff] [blame] | 94 | return gPool->create(bytes); |
| 95 | } |
| 96 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 97 | DEF_TEST(ImageCache, reporter) { |
reed@google.com | 772443a | 2013-12-11 15:30:24 +0000 | [diff] [blame] | 98 | static const size_t defLimit = DIM * DIM * 4 * COUNT + 1024; // 1K slop |
| 99 | |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 100 | { |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 101 | SkScaledImageCache cache(defLimit); |
| 102 | test_cache(reporter, cache, true); |
| 103 | } |
| 104 | { |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 105 | SkAutoTUnref<SkDiscardableMemoryPool> pool( |
| 106 | SkDiscardableMemoryPool::Create(defLimit, NULL)); |
| 107 | gPool = pool.get(); |
reed@google.com | 772443a | 2013-12-11 15:30:24 +0000 | [diff] [blame] | 108 | SkScaledImageCache cache(pool_factory); |
| 109 | test_cache(reporter, cache, true); |
| 110 | } |
| 111 | { |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 112 | SkScaledImageCache cache(SkDiscardableMemory::Create); |
| 113 | test_cache(reporter, cache, false); |
| 114 | } |
| 115 | } |
| 116 | |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 117 | DEF_TEST(ImageCache_doubleAdd, r) { |
| 118 | // Adding the same key twice should be safe. |
commit-bot@chromium.org | 5e4112b | 2014-03-05 13:44:18 +0000 | [diff] [blame] | 119 | SkScaledImageCache cache(4096); |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 120 | |
| 121 | SkBitmap original; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 122 | original.allocN32Pixels(40, 40); |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 123 | |
commit-bot@chromium.org | 5e4112b | 2014-03-05 13:44:18 +0000 | [diff] [blame] | 124 | SkBitmap scaled1; |
| 125 | scaled1.allocN32Pixels(20, 20); |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 126 | |
commit-bot@chromium.org | 5e4112b | 2014-03-05 13:44:18 +0000 | [diff] [blame] | 127 | SkBitmap scaled2; |
| 128 | scaled2.allocN32Pixels(20, 20); |
| 129 | |
reed | 0461713 | 2014-08-21 09:46:49 -0700 | [diff] [blame^] | 130 | TestingKey key(original.getGenerationID()); |
| 131 | |
| 132 | SkScaledImageCache::ID* id1 = cache.addAndLock(key, scaled1); |
| 133 | SkScaledImageCache::ID* id2 = cache.addAndLock(key, scaled2); |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 134 | // We don't really care if id1 == id2 as long as unlocking both works. |
| 135 | cache.unlock(id1); |
| 136 | cache.unlock(id2); |
commit-bot@chromium.org | 5e4112b | 2014-03-05 13:44:18 +0000 | [diff] [blame] | 137 | |
| 138 | SkBitmap tmp; |
| 139 | // Lookup should return the value that was added last. |
reed | 0461713 | 2014-08-21 09:46:49 -0700 | [diff] [blame^] | 140 | SkScaledImageCache::ID* id = cache.findAndLock(key, &tmp); |
commit-bot@chromium.org | 5e4112b | 2014-03-05 13:44:18 +0000 | [diff] [blame] | 141 | REPORTER_ASSERT(r, NULL != id); |
| 142 | REPORTER_ASSERT(r, tmp.getGenerationID() == scaled2.getGenerationID()); |
| 143 | cache.unlock(id); |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 144 | } |