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