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 "SkResourceCache.h" | 9 #include "SkResourceCache.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 SkResourceCache::Key { | 14 struct TestingKey : public SkResourceCache::Key { |
15 intptr_t fValue; | 15 intptr_t fValue; |
16 | 16 |
17 TestingKey(intptr_t value, uint64_t sharedID = 0) : fValue(value) { | 17 TestingKey(intptr_t value, uint64_t sharedID = 0) : fValue(value) { |
18 this->init(&gGlobalAddress, sharedID, sizeof(fValue)); | 18 this->init(&gGlobalAddress, sharedID, sizeof(fValue)); |
19 } | 19 } |
20 }; | 20 }; |
21 struct TestingRec : public SkResourceCache::Rec { | 21 struct TestingRec : public SkResourceCache::Rec { |
22 TestingRec(const TestingKey& key, uint32_t value) : fKey(key), fValue(value)
{} | 22 TestingRec(const TestingKey& key, uint32_t value) : fKey(key), fValue(value)
{} |
23 | 23 |
24 TestingKey fKey; | 24 TestingKey fKey; |
25 intptr_t fValue; | 25 intptr_t fValue; |
26 | 26 |
27 const Key& getKey() const override { return fKey; } | 27 const Key& getKey() const override { return fKey; } |
28 size_t bytesUsed() const override { return sizeof(fKey) + sizeof(fValue); } | 28 size_t bytesUsed() const override { return sizeof(fKey) + sizeof(fValue); } |
29 const char* getCategory() const override { return "test_cache"; } | 29 const char* getCategory() const override { return "test_cache"; } |
30 SkDiscardableMemory* diagnostic_only_getDiscardable() const override { retur
n NULL; } | 30 SkDiscardableMemory* diagnostic_only_getDiscardable() const override { retur
n nullptr; } |
31 | 31 |
32 static bool Visitor(const SkResourceCache::Rec& baseRec, void* context) { | 32 static bool Visitor(const SkResourceCache::Rec& baseRec, void* context) { |
33 const TestingRec& rec = static_cast<const TestingRec&>(baseRec); | 33 const TestingRec& rec = static_cast<const TestingRec&>(baseRec); |
34 intptr_t* result = (intptr_t*)context; | 34 intptr_t* result = (intptr_t*)context; |
35 | 35 |
36 *result = rec.fValue; | 36 *result = rec.fValue; |
37 return true; | 37 return true; |
38 } | 38 } |
39 }; | 39 }; |
40 } | 40 } |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 | 115 |
116 DEF_TEST(ImageCache, reporter) { | 116 DEF_TEST(ImageCache, reporter) { |
117 static const size_t defLimit = DIM * DIM * 4 * COUNT + 1024; // 1K slop | 117 static const size_t defLimit = DIM * DIM * 4 * COUNT + 1024; // 1K slop |
118 | 118 |
119 { | 119 { |
120 SkResourceCache cache(defLimit); | 120 SkResourceCache cache(defLimit); |
121 test_cache(reporter, cache, true); | 121 test_cache(reporter, cache, true); |
122 } | 122 } |
123 { | 123 { |
124 SkAutoTUnref<SkDiscardableMemoryPool> pool( | 124 SkAutoTUnref<SkDiscardableMemoryPool> pool( |
125 SkDiscardableMemoryPool::Create(defLimit, NULL)); | 125 SkDiscardableMemoryPool::Create(defLimit, nullptr)); |
126 gPool = pool.get(); | 126 gPool = pool.get(); |
127 SkResourceCache cache(pool_factory); | 127 SkResourceCache cache(pool_factory); |
128 test_cache(reporter, cache, true); | 128 test_cache(reporter, cache, true); |
129 } | 129 } |
130 { | 130 { |
131 SkResourceCache cache(SkDiscardableMemory::Create); | 131 SkResourceCache cache(SkDiscardableMemory::Create); |
132 test_cache(reporter, cache, false); | 132 test_cache(reporter, cache, false); |
133 } | 133 } |
134 { | 134 { |
135 SkResourceCache cache(defLimit); | 135 SkResourceCache cache(defLimit); |
136 test_cache_purge_shared_id(reporter, cache); | 136 test_cache_purge_shared_id(reporter, cache); |
137 } | 137 } |
138 } | 138 } |
139 | 139 |
140 DEF_TEST(ImageCache_doubleAdd, r) { | 140 DEF_TEST(ImageCache_doubleAdd, r) { |
141 // Adding the same key twice should be safe. | 141 // Adding the same key twice should be safe. |
142 SkResourceCache cache(4096); | 142 SkResourceCache cache(4096); |
143 | 143 |
144 TestingKey key(1); | 144 TestingKey key(1); |
145 | 145 |
146 cache.add(new TestingRec(key, 2)); | 146 cache.add(new TestingRec(key, 2)); |
147 cache.add(new TestingRec(key, 3)); | 147 cache.add(new TestingRec(key, 3)); |
148 | 148 |
149 // Lookup can return either value. | 149 // Lookup can return either value. |
150 intptr_t value = -1; | 150 intptr_t value = -1; |
151 REPORTER_ASSERT(r, cache.find(key, TestingRec::Visitor, &value)); | 151 REPORTER_ASSERT(r, cache.find(key, TestingRec::Visitor, &value)); |
152 REPORTER_ASSERT(r, 2 == value || 3 == value); | 152 REPORTER_ASSERT(r, 2 == value || 3 == value); |
153 } | 153 } |
OLD | NEW |