| 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" |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 static const int DIM = 256; | 43 static const int DIM = 256; |
| 44 | 44 |
| 45 static void test_cache(skiatest::Reporter* reporter, SkResourceCache& cache, boo
l testPurge) { | 45 static void test_cache(skiatest::Reporter* reporter, SkResourceCache& cache, boo
l testPurge) { |
| 46 for (int i = 0; i < COUNT; ++i) { | 46 for (int i = 0; i < COUNT; ++i) { |
| 47 TestingKey key(i); | 47 TestingKey key(i); |
| 48 intptr_t value = -1; | 48 intptr_t value = -1; |
| 49 | 49 |
| 50 REPORTER_ASSERT(reporter, !cache.find(key, TestingRec::Visitor, &value))
; | 50 REPORTER_ASSERT(reporter, !cache.find(key, TestingRec::Visitor, &value))
; |
| 51 REPORTER_ASSERT(reporter, -1 == value); | 51 REPORTER_ASSERT(reporter, -1 == value); |
| 52 | 52 |
| 53 cache.add(SkNEW_ARGS(TestingRec, (key, i))); | 53 cache.add(new TestingRec(key, i)); |
| 54 | 54 |
| 55 REPORTER_ASSERT(reporter, cache.find(key, TestingRec::Visitor, &value)); | 55 REPORTER_ASSERT(reporter, cache.find(key, TestingRec::Visitor, &value)); |
| 56 REPORTER_ASSERT(reporter, i == value); | 56 REPORTER_ASSERT(reporter, i == value); |
| 57 } | 57 } |
| 58 | 58 |
| 59 if (testPurge) { | 59 if (testPurge) { |
| 60 // stress test, should trigger purges | 60 // stress test, should trigger purges |
| 61 for (int i = 0; i < COUNT * 100; ++i) { | 61 for (int i = 0; i < COUNT * 100; ++i) { |
| 62 TestingKey key(i); | 62 TestingKey key(i); |
| 63 cache.add(SkNEW_ARGS(TestingRec, (key, i))); | 63 cache.add(new TestingRec(key, i)); |
| 64 } | 64 } |
| 65 } | 65 } |
| 66 | 66 |
| 67 // test the originals after all that purging | 67 // test the originals after all that purging |
| 68 for (int i = 0; i < COUNT; ++i) { | 68 for (int i = 0; i < COUNT; ++i) { |
| 69 intptr_t value; | 69 intptr_t value; |
| 70 (void)cache.find(TestingKey(i), TestingRec::Visitor, &value); | 70 (void)cache.find(TestingKey(i), TestingRec::Visitor, &value); |
| 71 } | 71 } |
| 72 | 72 |
| 73 cache.setTotalByteLimit(0); | 73 cache.setTotalByteLimit(0); |
| 74 } | 74 } |
| 75 | 75 |
| 76 static void test_cache_purge_shared_id(skiatest::Reporter* reporter, SkResourceC
ache& cache) { | 76 static void test_cache_purge_shared_id(skiatest::Reporter* reporter, SkResourceC
ache& cache) { |
| 77 for (int i = 0; i < COUNT; ++i) { | 77 for (int i = 0; i < COUNT; ++i) { |
| 78 TestingKey key(i, i & 1); // every other key will have a 1 for its sha
redID | 78 TestingKey key(i, i & 1); // every other key will have a 1 for its sha
redID |
| 79 cache.add(SkNEW_ARGS(TestingRec, (key, i))); | 79 cache.add(new TestingRec(key, i)); |
| 80 } | 80 } |
| 81 | 81 |
| 82 // Ensure that everyone is present | 82 // Ensure that everyone is present |
| 83 for (int i = 0; i < COUNT; ++i) { | 83 for (int i = 0; i < COUNT; ++i) { |
| 84 TestingKey key(i, i & 1); // every other key will have a 1 for its sha
redID | 84 TestingKey key(i, i & 1); // every other key will have a 1 for its sha
redID |
| 85 intptr_t value = -1; | 85 intptr_t value = -1; |
| 86 | 86 |
| 87 REPORTER_ASSERT(reporter, cache.find(key, TestingRec::Visitor, &value)); | 87 REPORTER_ASSERT(reporter, cache.find(key, TestingRec::Visitor, &value)); |
| 88 REPORTER_ASSERT(reporter, value == i); | 88 REPORTER_ASSERT(reporter, value == i); |
| 89 } | 89 } |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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(SkNEW_ARGS(TestingRec, (key, 2))); | 146 cache.add(new TestingRec(key, 2)); |
| 147 cache.add(SkNEW_ARGS(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 |