Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(330)

Side by Side Diff: tests/ImageCacheTest.cpp

Issue 569353002: Change SkResourceCache to take a Visitor inside its find(). (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: remember to purge after the add Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/core/SkResourceCache.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 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 struct TestingRec : public SkResourceCache::Rec { 22 struct TestingRec : public SkResourceCache::Rec {
23 TestingRec(const TestingKey& key, uint32_t value) : fKey(key), fValue(value) {} 23 TestingRec(const TestingKey& key, uint32_t value) : fKey(key), fValue(value) {}
24 24
25 TestingKey fKey; 25 TestingKey fKey;
26 intptr_t fValue; 26 intptr_t fValue;
27 27
28 virtual const Key& getKey() const SK_OVERRIDE { return fKey; } 28 virtual const Key& getKey() const SK_OVERRIDE { return fKey; }
29 virtual size_t bytesUsed() const SK_OVERRIDE { return sizeof(fKey) + sizeof( fValue); } 29 virtual size_t bytesUsed() const SK_OVERRIDE { return sizeof(fKey) + sizeof( fValue); }
30
31 static bool Visitor(const SkResourceCache::Rec& baseRec, void* context) {
32 const TestingRec& rec = static_cast<const TestingRec&>(baseRec);
33 intptr_t* result = (intptr_t*)context;
34
35 *result = rec.fValue;
36 return true;
37 }
30 }; 38 };
31 } 39 }
32 40
33 static const int COUNT = 10; 41 static const int COUNT = 10;
34 static const int DIM = 256; 42 static const int DIM = 256;
35 43
36 static void test_cache(skiatest::Reporter* reporter, SkResourceCache& cache, 44 static void test_cache(skiatest::Reporter* reporter, SkResourceCache& cache, boo l testPurge) {
37 bool testPurge) {
38 SkResourceCache::ID id;
39
40 for (int i = 0; i < COUNT; ++i) { 45 for (int i = 0; i < COUNT; ++i) {
41 TestingKey key(i); 46 TestingKey key(i);
47 intptr_t value = -1;
42 48
43 const TestingRec* rec = (const TestingRec*)cache.findAndLock(key); 49 REPORTER_ASSERT(reporter, !cache.find(key, TestingRec::Visitor, &value)) ;
44 REPORTER_ASSERT(reporter, NULL == rec); 50 REPORTER_ASSERT(reporter, -1 == value);
45 51
46 TestingRec* newRec = SkNEW_ARGS(TestingRec, (key, i)); 52 cache.add(SkNEW_ARGS(TestingRec, (key, i)));
47 const TestingRec* addedRec = (const TestingRec*)cache.addAndLock(newRec) ;
48 REPORTER_ASSERT(reporter, addedRec);
49 53
50 const TestingRec* foundRec = (const TestingRec*)cache.findAndLock(key); 54 REPORTER_ASSERT(reporter, cache.find(key, TestingRec::Visitor, &value));
51 REPORTER_ASSERT(reporter, foundRec == addedRec); 55 REPORTER_ASSERT(reporter, i == value);
52 REPORTER_ASSERT(reporter, foundRec->fValue == i);
53 cache.unlock(foundRec);
54 cache.unlock(addedRec);
55 } 56 }
56 57
57 if (testPurge) { 58 if (testPurge) {
58 // stress test, should trigger purges 59 // stress test, should trigger purges
59 for (size_t i = 0; i < COUNT * 100; ++i) { 60 for (size_t i = 0; i < COUNT * 100; ++i) {
60 TestingKey key(i); 61 TestingKey key(i);
61 SkResourceCache::ID id = cache.addAndLock(SkNEW_ARGS(TestingRec, (ke y, i))); 62 cache.add(SkNEW_ARGS(TestingRec, (key, i)));
62 REPORTER_ASSERT(reporter, id);
63 cache.unlock(id);
64 } 63 }
65 } 64 }
66 65
67 // test the originals after all that purging 66 // test the originals after all that purging
68 for (int i = 0; i < COUNT; ++i) { 67 for (int i = 0; i < COUNT; ++i) {
69 id = cache.findAndLock(TestingKey(i)); 68 intptr_t value;
70 if (id) { 69 (void)cache.find(TestingKey(i), TestingRec::Visitor, &value);
71 cache.unlock(id);
72 }
73 } 70 }
74 71
75 cache.setTotalByteLimit(0); 72 cache.setTotalByteLimit(0);
76 } 73 }
77 74
78 #include "SkDiscardableMemoryPool.h" 75 #include "SkDiscardableMemoryPool.h"
79 76
80 static SkDiscardableMemoryPool* gPool; 77 static SkDiscardableMemoryPool* gPool;
81 static SkDiscardableMemory* pool_factory(size_t bytes) { 78 static SkDiscardableMemory* pool_factory(size_t bytes) {
82 SkASSERT(gPool); 79 SkASSERT(gPool);
(...skipping 19 matching lines...) Expand all
102 test_cache(reporter, cache, false); 99 test_cache(reporter, cache, false);
103 } 100 }
104 } 101 }
105 102
106 DEF_TEST(ImageCache_doubleAdd, r) { 103 DEF_TEST(ImageCache_doubleAdd, r) {
107 // Adding the same key twice should be safe. 104 // Adding the same key twice should be safe.
108 SkResourceCache cache(4096); 105 SkResourceCache cache(4096);
109 106
110 TestingKey key(1); 107 TestingKey key(1);
111 108
112 SkResourceCache::ID id1 = cache.addAndLock(SkNEW_ARGS(TestingRec, (key, 2))) ; 109 cache.add(SkNEW_ARGS(TestingRec, (key, 2)));
113 SkResourceCache::ID id2 = cache.addAndLock(SkNEW_ARGS(TestingRec, (key, 3))) ; 110 cache.add(SkNEW_ARGS(TestingRec, (key, 3)));
114 // We don't really care if id1 == id2 as long as unlocking both works.
115 cache.unlock(id1);
116 cache.unlock(id2);
117 111
118 // Lookup can return either value. 112 // Lookup can return either value.
119 const TestingRec* rec = (const TestingRec*)cache.findAndLock(key); 113 intptr_t value = -1;
120 REPORTER_ASSERT(r, rec); 114 REPORTER_ASSERT(r, cache.find(key, TestingRec::Visitor, &value));
121 REPORTER_ASSERT(r, 2 == rec->fValue || 3 == rec->fValue); 115 REPORTER_ASSERT(r, 2 == value || 3 == value);
122 cache.unlock(rec);
123 } 116 }
OLDNEW
« no previous file with comments | « src/core/SkResourceCache.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698