| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "Test.h" | 8 #include "Test.h" |
| 9 #include "SkDiscardableMemory.h" | 9 #include "SkDiscardableMemory.h" |
| 10 #include "SkScaledImageCache.h" | 10 #include "SkScaledImageCache.h" |
| 11 | 11 |
| 12 static void make_bm(SkBitmap* bm, int w, int h) { | 12 static void make_bm(SkBitmap* bm, int w, int h) { |
| 13 bm->setConfig(SkBitmap::kARGB_8888_Config, w, h); | 13 bm->setConfig(SkBitmap::kARGB_8888_Config, w, h); |
| 14 bm->allocPixels(); | 14 bm->allocPixels(); |
| 15 } | 15 } |
| 16 | 16 |
| 17 static const int COUNT = 10; | 17 static const int COUNT = 10; |
| 18 static const int DIM = 256; | 18 static const int DIM = 256; |
| 19 | 19 |
| 20 static void test_cache(skiatest::Reporter* reporter, SkScaledImageCache& cache, | 20 static void test_cache(skiatest::Reporter* reporter, SkScaledImageCache& cache, |
| 21 bool testPurge) { | 21 bool testPurge) { |
| 22 SkScaledImageCache::ID* id; | 22 SkScaledImageCache::ID* id; |
| 23 | 23 |
| 24 SkBitmap bm[COUNT]; | 24 SkBitmap bm[COUNT]; |
| 25 | 25 |
| 26 SkScalar scale = 2; | 26 const SkScalar scale = 2; |
| 27 for (int i = 0; i < COUNT; ++i) { |
| 28 make_bm(&bm[i], DIM, DIM); |
| 29 } |
| 30 |
| 27 for (int i = 0; i < COUNT; ++i) { | 31 for (int i = 0; i < COUNT; ++i) { |
| 28 SkBitmap tmp; | 32 SkBitmap tmp; |
| 29 | 33 |
| 30 make_bm(&bm[i], DIM, DIM); | 34 SkScaledImageCache::ID* id = cache.findAndLock(bm[i], scale, scale, &tmp
); |
| 31 id = cache.findAndLock(bm[i], scale, scale, &tmp); | |
| 32 REPORTER_ASSERT(reporter, NULL == id); | 35 REPORTER_ASSERT(reporter, NULL == id); |
| 33 | 36 |
| 34 make_bm(&tmp, DIM, DIM); | 37 make_bm(&tmp, DIM, DIM); |
| 35 id = cache.addAndLock(bm[i], scale, scale, tmp); | 38 id = cache.addAndLock(bm[i], scale, scale, tmp); |
| 36 REPORTER_ASSERT(reporter, NULL != id); | 39 REPORTER_ASSERT(reporter, NULL != id); |
| 37 | 40 |
| 38 SkBitmap tmp2; | 41 SkBitmap tmp2; |
| 39 SkScaledImageCache::ID* id2 = cache.findAndLock(bm[i], scale, scale, | 42 SkScaledImageCache::ID* id2 = cache.findAndLock(bm[i], scale, scale, |
| 40 &tmp2); | 43 &tmp2); |
| 41 REPORTER_ASSERT(reporter, id == id2); | 44 REPORTER_ASSERT(reporter, id == id2); |
| 42 REPORTER_ASSERT(reporter, tmp.pixelRef() == tmp2.pixelRef()); | 45 REPORTER_ASSERT(reporter, tmp.pixelRef() == tmp2.pixelRef()); |
| 43 REPORTER_ASSERT(reporter, tmp.width() == tmp2.width()); | 46 REPORTER_ASSERT(reporter, tmp.width() == tmp2.width()); |
| 44 REPORTER_ASSERT(reporter, tmp.height() == tmp2.height()); | 47 REPORTER_ASSERT(reporter, tmp.height() == tmp2.height()); |
| 45 cache.unlock(id2); | 48 cache.unlock(id2); |
| 46 | 49 |
| 47 cache.unlock(id); | 50 cache.unlock(id); |
| 48 } | 51 } |
| 49 | 52 |
| 50 if (testPurge) { | 53 if (testPurge) { |
| 51 // stress test, should trigger purges | 54 // stress test, should trigger purges |
| 55 float incScale = 2; |
| 52 for (size_t i = 0; i < COUNT * 100; ++i) { | 56 for (size_t i = 0; i < COUNT * 100; ++i) { |
| 53 scale += 1; | 57 incScale += 1; |
| 54 | 58 |
| 55 SkBitmap tmp; | 59 SkBitmap tmp; |
| 60 make_bm(&tmp, DIM, DIM); |
| 56 | 61 |
| 57 make_bm(&tmp, DIM, DIM); | 62 SkScaledImageCache::ID* id = cache.addAndLock(bm[0], incScale, |
| 58 id = cache.addAndLock(bm[0], scale, scale, tmp); | 63 incScale, tmp); |
| 59 REPORTER_ASSERT(reporter, NULL != id); | 64 REPORTER_ASSERT(reporter, NULL != id); |
| 60 cache.unlock(id); | 65 cache.unlock(id); |
| 61 } | 66 } |
| 62 } | 67 } |
| 68 |
| 69 // test the originals after all that purging |
| 70 for (int i = 0; i < COUNT; ++i) { |
| 71 SkBitmap tmp; |
| 72 id = cache.findAndLock(bm[i], scale, scale, &tmp); |
| 73 if (id) { |
| 74 cache.unlock(id); |
| 75 } |
| 76 } |
| 77 |
| 63 cache.setByteLimit(0); | 78 cache.setByteLimit(0); |
| 64 } | 79 } |
| 65 | 80 |
| 81 #include "SkDiscardableMemoryPool.h" |
| 82 |
| 83 static SkDiscardableMemoryPool* gPool; |
| 84 static SkDiscardableMemory* pool_factory(size_t bytes) { |
| 85 return gPool->create(bytes); |
| 86 } |
| 87 |
| 66 static void TestImageCache(skiatest::Reporter* reporter) { | 88 static void TestImageCache(skiatest::Reporter* reporter) { |
| 89 static const size_t defLimit = DIM * DIM * 4 * COUNT + 1024; // 1K slop |
| 90 |
| 67 { | 91 { |
| 68 static const size_t defLimit = DIM * DIM * 4 * COUNT + 1024; // 1K sl
op | |
| 69 SkScaledImageCache cache(defLimit); | 92 SkScaledImageCache cache(defLimit); |
| 70 test_cache(reporter, cache, true); | 93 test_cache(reporter, cache, true); |
| 71 } | 94 } |
| 72 { | 95 { |
| 96 SkDiscardableMemoryPool pool(defLimit); |
| 97 gPool = &pool; |
| 98 SkScaledImageCache cache(pool_factory); |
| 99 test_cache(reporter, cache, true); |
| 100 } |
| 101 { |
| 73 SkScaledImageCache cache(SkDiscardableMemory::Create); | 102 SkScaledImageCache cache(SkDiscardableMemory::Create); |
| 74 test_cache(reporter, cache, false); | 103 test_cache(reporter, cache, false); |
| 75 } | 104 } |
| 76 } | 105 } |
| 77 | 106 |
| 78 #include "TestClassDef.h" | 107 #include "TestClassDef.h" |
| 79 DEFINE_TESTCLASS("ImageCache", TestImageCacheClass, TestImageCache) | 108 DEFINE_TESTCLASS("ImageCache", TestImageCacheClass, TestImageCache) |
| 80 | 109 |
| 81 DEF_TEST(ImageCache_doubleAdd, r) { | 110 DEF_TEST(ImageCache_doubleAdd, r) { |
| 82 // Adding the same key twice should be safe. | 111 // Adding the same key twice should be safe. |
| 83 SkScaledImageCache cache(1024); | 112 SkScaledImageCache cache(1024); |
| 84 | 113 |
| 85 SkBitmap original; | 114 SkBitmap original; |
| 86 original.setConfig(SkBitmap::kARGB_8888_Config, 40, 40); | 115 original.setConfig(SkBitmap::kARGB_8888_Config, 40, 40); |
| 87 original.allocPixels(); | 116 original.allocPixels(); |
| 88 | 117 |
| 89 SkBitmap scaled; | 118 SkBitmap scaled; |
| 90 scaled.setConfig(SkBitmap::kARGB_8888_Config, 20, 20); | 119 scaled.setConfig(SkBitmap::kARGB_8888_Config, 20, 20); |
| 91 scaled.allocPixels(); | 120 scaled.allocPixels(); |
| 92 | 121 |
| 93 SkScaledImageCache::ID* id1 = cache.addAndLock(original, 0.5f, 0.5f, scaled)
; | 122 SkScaledImageCache::ID* id1 = cache.addAndLock(original, 0.5f, 0.5f, scaled)
; |
| 94 SkScaledImageCache::ID* id2 = cache.addAndLock(original, 0.5f, 0.5f, scaled)
; | 123 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. | 124 // We don't really care if id1 == id2 as long as unlocking both works. |
| 96 cache.unlock(id1); | 125 cache.unlock(id1); |
| 97 cache.unlock(id2); | 126 cache.unlock(id2); |
| 98 } | 127 } |
| OLD | NEW |