reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 8 | #include "Test.h" |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 9 | #include "SkDiscardableMemory.h" |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 10 | #include "SkScaledImageCache.h" |
| 11 | |
| 12 | static void make_bm(SkBitmap* bm, int w, int h) { |
| 13 | bm->setConfig(SkBitmap::kARGB_8888_Config, w, h); |
| 14 | bm->allocPixels(); |
| 15 | } |
| 16 | |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 17 | static const int COUNT = 10; |
| 18 | static const int DIM = 256; |
| 19 | |
| 20 | static void test_cache(skiatest::Reporter* reporter, SkScaledImageCache& cache, |
| 21 | bool testPurge) { |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 22 | SkScaledImageCache::ID* id; |
skia.committer@gmail.com | cf0803b | 2013-12-10 07:02:03 +0000 | [diff] [blame^] | 23 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 24 | SkBitmap bm[COUNT]; |
skia.committer@gmail.com | cf0803b | 2013-12-10 07:02:03 +0000 | [diff] [blame^] | 25 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 26 | SkScalar scale = 2; |
reed@google.com | 0e66162 | 2013-07-23 19:27:48 +0000 | [diff] [blame] | 27 | for (int i = 0; i < COUNT; ++i) { |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 28 | SkBitmap tmp; |
skia.committer@gmail.com | cf0803b | 2013-12-10 07:02:03 +0000 | [diff] [blame^] | 29 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 30 | make_bm(&bm[i], DIM, DIM); |
| 31 | id = cache.findAndLock(bm[i], scale, scale, &tmp); |
| 32 | REPORTER_ASSERT(reporter, NULL == id); |
skia.committer@gmail.com | cf0803b | 2013-12-10 07:02:03 +0000 | [diff] [blame^] | 33 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 34 | make_bm(&tmp, DIM, DIM); |
| 35 | id = cache.addAndLock(bm[i], scale, scale, tmp); |
| 36 | REPORTER_ASSERT(reporter, NULL != id); |
skia.committer@gmail.com | cf0803b | 2013-12-10 07:02:03 +0000 | [diff] [blame^] | 37 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 38 | SkBitmap tmp2; |
| 39 | SkScaledImageCache::ID* id2 = cache.findAndLock(bm[i], scale, scale, |
| 40 | &tmp2); |
| 41 | REPORTER_ASSERT(reporter, id == id2); |
| 42 | REPORTER_ASSERT(reporter, tmp.pixelRef() == tmp2.pixelRef()); |
| 43 | REPORTER_ASSERT(reporter, tmp.width() == tmp2.width()); |
| 44 | REPORTER_ASSERT(reporter, tmp.height() == tmp2.height()); |
| 45 | cache.unlock(id2); |
skia.committer@gmail.com | cf0803b | 2013-12-10 07:02:03 +0000 | [diff] [blame^] | 46 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 47 | cache.unlock(id); |
| 48 | } |
skia.committer@gmail.com | cf0803b | 2013-12-10 07:02:03 +0000 | [diff] [blame^] | 49 | |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 50 | if (testPurge) { |
| 51 | // stress test, should trigger purges |
| 52 | for (size_t i = 0; i < COUNT * 100; ++i) { |
| 53 | scale += 1; |
skia.committer@gmail.com | cf0803b | 2013-12-10 07:02:03 +0000 | [diff] [blame^] | 54 | |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 55 | SkBitmap tmp; |
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 | make_bm(&tmp, DIM, DIM); |
| 58 | id = cache.addAndLock(bm[0], scale, scale, tmp); |
| 59 | REPORTER_ASSERT(reporter, NULL != id); |
| 60 | cache.unlock(id); |
| 61 | } |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 62 | } |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 63 | cache.setByteLimit(0); |
| 64 | } |
| 65 | |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 66 | static void TestImageCache(skiatest::Reporter* reporter) { |
| 67 | { |
| 68 | static const size_t defLimit = DIM * DIM * 4 * COUNT + 1024; // 1K slop |
| 69 | SkScaledImageCache cache(defLimit); |
| 70 | test_cache(reporter, cache, true); |
| 71 | } |
| 72 | { |
| 73 | SkScaledImageCache cache(SkDiscardableMemory::Create); |
| 74 | test_cache(reporter, cache, false); |
| 75 | } |
| 76 | } |
| 77 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 78 | #include "TestClassDef.h" |
| 79 | DEFINE_TESTCLASS("ImageCache", TestImageCacheClass, TestImageCache) |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 80 | |
| 81 | DEF_TEST(ImageCache_doubleAdd, r) { |
| 82 | // Adding the same key twice should be safe. |
| 83 | SkScaledImageCache cache(1024); |
| 84 | |
| 85 | SkBitmap original; |
| 86 | original.setConfig(SkBitmap::kARGB_8888_Config, 40, 40); |
| 87 | original.allocPixels(); |
| 88 | |
| 89 | SkBitmap scaled; |
| 90 | scaled.setConfig(SkBitmap::kARGB_8888_Config, 20, 20); |
| 91 | scaled.allocPixels(); |
| 92 | |
| 93 | SkScaledImageCache::ID* id1 = cache.addAndLock(original, 0.5f, 0.5f, scaled); |
| 94 | SkScaledImageCache::ID* id2 = cache.addAndLock(original, 0.5f, 0.5f, scaled); |
| 95 | // We don't really care if id1 == id2 as long as unlocking both works. |
| 96 | cache.unlock(id1); |
| 97 | cache.unlock(id2); |
| 98 | } |