| 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) : fValue(value) { | 17 TestingKey(intptr_t value, uint64_t sharedID = 0) : fValue(value) { |
| 18 this->init(&gGlobalAddress, 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 SK_OVERRIDE { return fKey; } | 27 const Key& getKey() const SK_OVERRIDE { return fKey; } |
| 28 size_t bytesUsed() const SK_OVERRIDE { return sizeof(fKey) + sizeof(fValue);
} | 28 size_t bytesUsed() const SK_OVERRIDE { return sizeof(fKey) + sizeof(fValue);
} |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 | 64 |
| 65 // test the originals after all that purging | 65 // test the originals after all that purging |
| 66 for (int i = 0; i < COUNT; ++i) { | 66 for (int i = 0; i < COUNT; ++i) { |
| 67 intptr_t value; | 67 intptr_t value; |
| 68 (void)cache.find(TestingKey(i), TestingRec::Visitor, &value); | 68 (void)cache.find(TestingKey(i), TestingRec::Visitor, &value); |
| 69 } | 69 } |
| 70 | 70 |
| 71 cache.setTotalByteLimit(0); | 71 cache.setTotalByteLimit(0); |
| 72 } | 72 } |
| 73 | 73 |
| 74 static void test_cache_purge_shared_id(skiatest::Reporter* reporter, SkResourceC
ache& cache) { |
| 75 for (int i = 0; i < COUNT; ++i) { |
| 76 TestingKey key(i, i & 1); // every other key will have a 1 for its sha
redID |
| 77 cache.add(SkNEW_ARGS(TestingRec, (key, i))); |
| 78 } |
| 79 |
| 80 // Ensure that everyone is present |
| 81 for (int i = 0; i < COUNT; ++i) { |
| 82 TestingKey key(i, i & 1); // every other key will have a 1 for its sha
redID |
| 83 intptr_t value = -1; |
| 84 |
| 85 REPORTER_ASSERT(reporter, cache.find(key, TestingRec::Visitor, &value)); |
| 86 REPORTER_ASSERT(reporter, value == i); |
| 87 } |
| 88 |
| 89 // Now purge the ones that had a non-zero sharedID (the odd-indexed ones) |
| 90 cache.purgeSharedID(1); |
| 91 |
| 92 // Ensure that only the even ones are still present |
| 93 for (int i = 0; i < COUNT; ++i) { |
| 94 TestingKey key(i, i & 1); // every other key will have a 1 for its sha
redID |
| 95 intptr_t value = -1; |
| 96 |
| 97 if (i & 1) { |
| 98 REPORTER_ASSERT(reporter, !cache.find(key, TestingRec::Visitor, &val
ue)); |
| 99 } else { |
| 100 REPORTER_ASSERT(reporter, cache.find(key, TestingRec::Visitor, &valu
e)); |
| 101 REPORTER_ASSERT(reporter, value == i); |
| 102 } |
| 103 } |
| 104 } |
| 105 |
| 74 #include "SkDiscardableMemoryPool.h" | 106 #include "SkDiscardableMemoryPool.h" |
| 75 | 107 |
| 76 static SkDiscardableMemoryPool* gPool; | 108 static SkDiscardableMemoryPool* gPool; |
| 77 static SkDiscardableMemory* pool_factory(size_t bytes) { | 109 static SkDiscardableMemory* pool_factory(size_t bytes) { |
| 78 SkASSERT(gPool); | 110 SkASSERT(gPool); |
| 79 return gPool->create(bytes); | 111 return gPool->create(bytes); |
| 80 } | 112 } |
| 81 | 113 |
| 82 DEF_TEST(ImageCache, reporter) { | 114 DEF_TEST(ImageCache, reporter) { |
| 83 static const size_t defLimit = DIM * DIM * 4 * COUNT + 1024; // 1K slop | 115 static const size_t defLimit = DIM * DIM * 4 * COUNT + 1024; // 1K slop |
| 84 | 116 |
| 85 { | 117 { |
| 86 SkResourceCache cache(defLimit); | 118 SkResourceCache cache(defLimit); |
| 87 test_cache(reporter, cache, true); | 119 test_cache(reporter, cache, true); |
| 88 } | 120 } |
| 89 { | 121 { |
| 90 SkAutoTUnref<SkDiscardableMemoryPool> pool( | 122 SkAutoTUnref<SkDiscardableMemoryPool> pool( |
| 91 SkDiscardableMemoryPool::Create(defLimit, NULL)); | 123 SkDiscardableMemoryPool::Create(defLimit, NULL)); |
| 92 gPool = pool.get(); | 124 gPool = pool.get(); |
| 93 SkResourceCache cache(pool_factory); | 125 SkResourceCache cache(pool_factory); |
| 94 test_cache(reporter, cache, true); | 126 test_cache(reporter, cache, true); |
| 95 } | 127 } |
| 96 { | 128 { |
| 97 SkResourceCache cache(SkDiscardableMemory::Create); | 129 SkResourceCache cache(SkDiscardableMemory::Create); |
| 98 test_cache(reporter, cache, false); | 130 test_cache(reporter, cache, false); |
| 99 } | 131 } |
| 132 { |
| 133 SkResourceCache cache(defLimit); |
| 134 test_cache_purge_shared_id(reporter, cache); |
| 135 } |
| 100 } | 136 } |
| 101 | 137 |
| 102 DEF_TEST(ImageCache_doubleAdd, r) { | 138 DEF_TEST(ImageCache_doubleAdd, r) { |
| 103 // Adding the same key twice should be safe. | 139 // Adding the same key twice should be safe. |
| 104 SkResourceCache cache(4096); | 140 SkResourceCache cache(4096); |
| 105 | 141 |
| 106 TestingKey key(1); | 142 TestingKey key(1); |
| 107 | 143 |
| 108 cache.add(SkNEW_ARGS(TestingRec, (key, 2))); | 144 cache.add(SkNEW_ARGS(TestingRec, (key, 2))); |
| 109 cache.add(SkNEW_ARGS(TestingRec, (key, 3))); | 145 cache.add(SkNEW_ARGS(TestingRec, (key, 3))); |
| 110 | 146 |
| 111 // Lookup can return either value. | 147 // Lookup can return either value. |
| 112 intptr_t value = -1; | 148 intptr_t value = -1; |
| 113 REPORTER_ASSERT(r, cache.find(key, TestingRec::Visitor, &value)); | 149 REPORTER_ASSERT(r, cache.find(key, TestingRec::Visitor, &value)); |
| 114 REPORTER_ASSERT(r, 2 == value || 3 == value); | 150 REPORTER_ASSERT(r, 2 == value || 3 == value); |
| 115 } | 151 } |
| OLD | NEW |