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

Side by Side Diff: tests/ImageCacheTest.cpp

Issue 569303002: Revert of Change SkResourceCache to take a Visitor inside its find(). (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: 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 }
38 }; 30 };
39 } 31 }
40 32
41 static const int COUNT = 10; 33 static const int COUNT = 10;
42 static const int DIM = 256; 34 static const int DIM = 256;
43 35
44 static void test_cache(skiatest::Reporter* reporter, SkResourceCache& cache, boo l testPurge) { 36 static void test_cache(skiatest::Reporter* reporter, SkResourceCache& cache,
37 bool testPurge) {
38 SkResourceCache::ID id;
39
45 for (int i = 0; i < COUNT; ++i) { 40 for (int i = 0; i < COUNT; ++i) {
46 TestingKey key(i); 41 TestingKey key(i);
47 intptr_t value = -1;
48 42
49 REPORTER_ASSERT(reporter, !cache.find(key, TestingRec::Visitor, &value)) ; 43 const TestingRec* rec = (const TestingRec*)cache.findAndLock(key);
50 REPORTER_ASSERT(reporter, -1 == value); 44 REPORTER_ASSERT(reporter, NULL == rec);
51 45
52 cache.add(SkNEW_ARGS(TestingRec, (key, i))); 46 TestingRec* newRec = SkNEW_ARGS(TestingRec, (key, i));
47 const TestingRec* addedRec = (const TestingRec*)cache.addAndLock(newRec) ;
48 REPORTER_ASSERT(reporter, addedRec);
53 49
54 REPORTER_ASSERT(reporter, cache.find(key, TestingRec::Visitor, &value)); 50 const TestingRec* foundRec = (const TestingRec*)cache.findAndLock(key);
55 REPORTER_ASSERT(reporter, i == value); 51 REPORTER_ASSERT(reporter, foundRec == addedRec);
52 REPORTER_ASSERT(reporter, foundRec->fValue == i);
53 cache.unlock(foundRec);
54 cache.unlock(addedRec);
56 } 55 }
57 56
58 if (testPurge) { 57 if (testPurge) {
59 // stress test, should trigger purges 58 // stress test, should trigger purges
60 for (size_t i = 0; i < COUNT * 100; ++i) { 59 for (size_t i = 0; i < COUNT * 100; ++i) {
61 TestingKey key(i); 60 TestingKey key(i);
62 cache.add(SkNEW_ARGS(TestingRec, (key, i))); 61 SkResourceCache::ID id = cache.addAndLock(SkNEW_ARGS(TestingRec, (ke y, i)));
62 REPORTER_ASSERT(reporter, id);
63 cache.unlock(id);
63 } 64 }
64 } 65 }
65 66
66 // test the originals after all that purging 67 // test the originals after all that purging
67 for (int i = 0; i < COUNT; ++i) { 68 for (int i = 0; i < COUNT; ++i) {
68 intptr_t value; 69 id = cache.findAndLock(TestingKey(i));
69 (void)cache.find(TestingKey(i), TestingRec::Visitor, &value); 70 if (id) {
71 cache.unlock(id);
72 }
70 } 73 }
71 74
72 cache.setTotalByteLimit(0); 75 cache.setTotalByteLimit(0);
73 } 76 }
74 77
75 #include "SkDiscardableMemoryPool.h" 78 #include "SkDiscardableMemoryPool.h"
76 79
77 static SkDiscardableMemoryPool* gPool; 80 static SkDiscardableMemoryPool* gPool;
78 static SkDiscardableMemory* pool_factory(size_t bytes) { 81 static SkDiscardableMemory* pool_factory(size_t bytes) {
79 SkASSERT(gPool); 82 SkASSERT(gPool);
(...skipping 19 matching lines...) Expand all
99 test_cache(reporter, cache, false); 102 test_cache(reporter, cache, false);
100 } 103 }
101 } 104 }
102 105
103 DEF_TEST(ImageCache_doubleAdd, r) { 106 DEF_TEST(ImageCache_doubleAdd, r) {
104 // Adding the same key twice should be safe. 107 // Adding the same key twice should be safe.
105 SkResourceCache cache(4096); 108 SkResourceCache cache(4096);
106 109
107 TestingKey key(1); 110 TestingKey key(1);
108 111
109 cache.add(SkNEW_ARGS(TestingRec, (key, 2))); 112 SkResourceCache::ID id1 = cache.addAndLock(SkNEW_ARGS(TestingRec, (key, 2))) ;
110 cache.add(SkNEW_ARGS(TestingRec, (key, 3))); 113 SkResourceCache::ID id2 = cache.addAndLock(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);
111 117
112 // Lookup can return either value. 118 // Lookup can return either value.
113 intptr_t value = -1; 119 const TestingRec* rec = (const TestingRec*)cache.findAndLock(key);
114 REPORTER_ASSERT(r, cache.find(key, TestingRec::Visitor, &value)); 120 REPORTER_ASSERT(r, rec);
115 REPORTER_ASSERT(r, 2 == value || 3 == value); 121 REPORTER_ASSERT(r, 2 == rec->fValue || 3 == rec->fValue);
122 cache.unlock(rec);
116 } 123 }
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