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) { |
| 84 | return gPool->create(bytes); |
| 85 | } |
| 86 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 87 | DEF_TEST(ImageCache, reporter) { |
reed@google.com | 772443a | 2013-12-11 15:30:24 +0000 | [diff] [blame] | 88 | static const size_t defLimit = DIM * DIM * 4 * COUNT + 1024; // 1K slop |
| 89 | |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 90 | { |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 91 | SkScaledImageCache cache(defLimit); |
| 92 | test_cache(reporter, cache, true); |
| 93 | } |
| 94 | { |
reed@google.com | 772443a | 2013-12-11 15:30:24 +0000 | [diff] [blame] | 95 | SkDiscardableMemoryPool pool(defLimit); |
| 96 | gPool = &pool; |
| 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 | |
| 110 | SkBitmap original; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 111 | original.allocN32Pixels(40, 40); |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 112 | |
commit-bot@chromium.org | 5e4112b | 2014-03-05 13:44:18 +0000 | [diff] [blame^] | 113 | SkBitmap scaled1; |
| 114 | scaled1.allocN32Pixels(20, 20); |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 115 | |
commit-bot@chromium.org | 5e4112b | 2014-03-05 13:44:18 +0000 | [diff] [blame^] | 116 | SkBitmap scaled2; |
| 117 | scaled2.allocN32Pixels(20, 20); |
| 118 | |
| 119 | SkScaledImageCache::ID* id1 = cache.addAndLock(original, 0.5f, 0.5f, scaled1); |
| 120 | SkScaledImageCache::ID* id2 = cache.addAndLock(original, 0.5f, 0.5f, scaled2); |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 121 | // We don't really care if id1 == id2 as long as unlocking both works. |
| 122 | cache.unlock(id1); |
| 123 | cache.unlock(id2); |
commit-bot@chromium.org | 5e4112b | 2014-03-05 13:44:18 +0000 | [diff] [blame^] | 124 | |
| 125 | SkBitmap tmp; |
| 126 | // Lookup should return the value that was added last. |
| 127 | SkScaledImageCache::ID* id = cache.findAndLock(original, 0.5f, 0.5f, &tmp); |
| 128 | REPORTER_ASSERT(r, NULL != id); |
| 129 | REPORTER_ASSERT(r, tmp.getGenerationID() == scaled2.getGenerationID()); |
| 130 | cache.unlock(id); |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 131 | } |