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