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