| 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 "SkDiscardableMemory.h" | 8 #include "SkDiscardableMemory.h" |
| 9 #include "SkScaledImageCache.h" | 9 #include "SkScaledImageCache.h" |
| 10 #include "Test.h" | 10 #include "Test.h" |
| 11 | 11 |
| 12 namespace { |
| 13 static void* gGlobalAddress; |
| 14 struct TestingKey : public SkScaledImageCache::Key { |
| 15 void* fPtr; |
| 16 intptr_t fValue; |
| 17 |
| 18 TestingKey(intptr_t value) : fPtr(&gGlobalAddress), fValue(value) { |
| 19 this->init(sizeof(fPtr) + sizeof(fValue)); |
| 20 } |
| 21 }; |
| 22 } |
| 23 |
| 12 static void make_bm(SkBitmap* bm, int w, int h) { | 24 static void make_bm(SkBitmap* bm, int w, int h) { |
| 13 bm->allocN32Pixels(w, h); | 25 bm->allocN32Pixels(w, h); |
| 14 } | 26 } |
| 15 | 27 |
| 16 static const int COUNT = 10; | 28 static const int COUNT = 10; |
| 17 static const int DIM = 256; | 29 static const int DIM = 256; |
| 18 | 30 |
| 19 static void test_cache(skiatest::Reporter* reporter, SkScaledImageCache& cache, | 31 static void test_cache(skiatest::Reporter* reporter, SkScaledImageCache& cache, |
| 20 bool testPurge) { | 32 bool testPurge) { |
| 21 SkScaledImageCache::ID* id; | 33 SkScaledImageCache::ID* id; |
| 22 | 34 |
| 23 SkBitmap bm[COUNT]; | 35 SkBitmap bm[COUNT]; |
| 24 | 36 |
| 25 const SkScalar scale = 2; | |
| 26 for (int i = 0; i < COUNT; ++i) { | 37 for (int i = 0; i < COUNT; ++i) { |
| 27 make_bm(&bm[i], DIM, DIM); | 38 make_bm(&bm[i], DIM, DIM); |
| 28 } | 39 } |
| 29 | 40 |
| 30 for (int i = 0; i < COUNT; ++i) { | 41 for (int i = 0; i < COUNT; ++i) { |
| 42 TestingKey key(bm[i].getGenerationID()); |
| 31 SkBitmap tmp; | 43 SkBitmap tmp; |
| 32 | 44 |
| 33 SkScaledImageCache::ID* id = cache.findAndLock(bm[i], scale, scale, &tmp
); | 45 SkScaledImageCache::ID* id = cache.findAndLock(key, &tmp); |
| 34 REPORTER_ASSERT(reporter, NULL == id); | 46 REPORTER_ASSERT(reporter, NULL == id); |
| 35 | 47 |
| 36 make_bm(&tmp, DIM, DIM); | 48 make_bm(&tmp, DIM, DIM); |
| 37 id = cache.addAndLock(bm[i], scale, scale, tmp); | 49 id = cache.addAndLock(key, tmp); |
| 38 REPORTER_ASSERT(reporter, NULL != id); | 50 REPORTER_ASSERT(reporter, NULL != id); |
| 39 | 51 |
| 40 SkBitmap tmp2; | 52 SkBitmap tmp2; |
| 41 SkScaledImageCache::ID* id2 = cache.findAndLock(bm[i], scale, scale, | 53 SkScaledImageCache::ID* id2 = cache.findAndLock(key, &tmp2); |
| 42 &tmp2); | |
| 43 REPORTER_ASSERT(reporter, id == id2); | 54 REPORTER_ASSERT(reporter, id == id2); |
| 44 REPORTER_ASSERT(reporter, tmp.pixelRef() == tmp2.pixelRef()); | 55 REPORTER_ASSERT(reporter, tmp.pixelRef() == tmp2.pixelRef()); |
| 45 REPORTER_ASSERT(reporter, tmp.width() == tmp2.width()); | 56 REPORTER_ASSERT(reporter, tmp.width() == tmp2.width()); |
| 46 REPORTER_ASSERT(reporter, tmp.height() == tmp2.height()); | 57 REPORTER_ASSERT(reporter, tmp.height() == tmp2.height()); |
| 47 cache.unlock(id2); | 58 cache.unlock(id2); |
| 48 | 59 |
| 49 cache.unlock(id); | 60 cache.unlock(id); |
| 50 } | 61 } |
| 51 | 62 |
| 52 if (testPurge) { | 63 if (testPurge) { |
| 53 // stress test, should trigger purges | 64 // stress test, should trigger purges |
| 54 float incScale = 2; | |
| 55 for (size_t i = 0; i < COUNT * 100; ++i) { | 65 for (size_t i = 0; i < COUNT * 100; ++i) { |
| 56 incScale += 1; | 66 TestingKey key(i); |
| 57 | |
| 58 SkBitmap tmp; | 67 SkBitmap tmp; |
| 59 make_bm(&tmp, DIM, DIM); | 68 make_bm(&tmp, DIM, DIM); |
| 60 | 69 |
| 61 SkScaledImageCache::ID* id = cache.addAndLock(bm[0], incScale, | 70 SkScaledImageCache::ID* id = cache.addAndLock(key, tmp); |
| 62 incScale, tmp); | |
| 63 REPORTER_ASSERT(reporter, NULL != id); | 71 REPORTER_ASSERT(reporter, NULL != id); |
| 64 cache.unlock(id); | 72 cache.unlock(id); |
| 65 } | 73 } |
| 66 } | 74 } |
| 67 | 75 |
| 68 // test the originals after all that purging | 76 // test the originals after all that purging |
| 69 for (int i = 0; i < COUNT; ++i) { | 77 for (int i = 0; i < COUNT; ++i) { |
| 78 TestingKey key(bm[i].getGenerationID()); |
| 70 SkBitmap tmp; | 79 SkBitmap tmp; |
| 71 id = cache.findAndLock(bm[i], scale, scale, &tmp); | 80 id = cache.findAndLock(key, &tmp); |
| 72 if (id) { | 81 if (id) { |
| 73 cache.unlock(id); | 82 cache.unlock(id); |
| 74 } | 83 } |
| 75 } | 84 } |
| 76 | 85 |
| 77 cache.setTotalByteLimit(0); | 86 cache.setTotalByteLimit(0); |
| 78 } | 87 } |
| 79 | 88 |
| 80 #include "SkDiscardableMemoryPool.h" | 89 #include "SkDiscardableMemoryPool.h" |
| 81 | 90 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 111 | 120 |
| 112 SkBitmap original; | 121 SkBitmap original; |
| 113 original.allocN32Pixels(40, 40); | 122 original.allocN32Pixels(40, 40); |
| 114 | 123 |
| 115 SkBitmap scaled1; | 124 SkBitmap scaled1; |
| 116 scaled1.allocN32Pixels(20, 20); | 125 scaled1.allocN32Pixels(20, 20); |
| 117 | 126 |
| 118 SkBitmap scaled2; | 127 SkBitmap scaled2; |
| 119 scaled2.allocN32Pixels(20, 20); | 128 scaled2.allocN32Pixels(20, 20); |
| 120 | 129 |
| 121 SkScaledImageCache::ID* id1 = cache.addAndLock(original, 0.5f, 0.5f, scaled1
); | 130 TestingKey key(original.getGenerationID()); |
| 122 SkScaledImageCache::ID* id2 = cache.addAndLock(original, 0.5f, 0.5f, scaled2
); | 131 |
| 132 SkScaledImageCache::ID* id1 = cache.addAndLock(key, scaled1); |
| 133 SkScaledImageCache::ID* id2 = cache.addAndLock(key, scaled2); |
| 123 // We don't really care if id1 == id2 as long as unlocking both works. | 134 // We don't really care if id1 == id2 as long as unlocking both works. |
| 124 cache.unlock(id1); | 135 cache.unlock(id1); |
| 125 cache.unlock(id2); | 136 cache.unlock(id2); |
| 126 | 137 |
| 127 SkBitmap tmp; | 138 SkBitmap tmp; |
| 128 // Lookup should return the value that was added last. | 139 // Lookup should return the value that was added last. |
| 129 SkScaledImageCache::ID* id = cache.findAndLock(original, 0.5f, 0.5f, &tmp); | 140 SkScaledImageCache::ID* id = cache.findAndLock(key, &tmp); |
| 130 REPORTER_ASSERT(r, NULL != id); | 141 REPORTER_ASSERT(r, NULL != id); |
| 131 REPORTER_ASSERT(r, tmp.getGenerationID() == scaled2.getGenerationID()); | 142 REPORTER_ASSERT(r, tmp.getGenerationID() == scaled2.getGenerationID()); |
| 132 cache.unlock(id); | 143 cache.unlock(id); |
| 133 } | 144 } |
| OLD | NEW |