• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1  /*
2  * Copyright 2013 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "SkDiscardableMemory.h"
9 #include "SkResourceCache.h"
10 #include "Test.h"
11 
12 namespace {
13 static void* gGlobalAddress;
14 struct TestingKey : public SkResourceCache::Key {
15     intptr_t    fValue;
16 
TestingKey__anon050bcc320111::TestingKey17     TestingKey(intptr_t value, uint64_t sharedID = 0) : fValue(value) {
18         this->init(&gGlobalAddress, sharedID, sizeof(fValue));
19     }
20 };
21 struct TestingRec : public SkResourceCache::Rec {
TestingRec__anon050bcc320111::TestingRec22     TestingRec(const TestingKey& key, uint32_t value) : fKey(key), fValue(value) {}
23 
24     TestingKey  fKey;
25     intptr_t    fValue;
26 
getKey__anon050bcc320111::TestingRec27     const Key& getKey() const override { return fKey; }
bytesUsed__anon050bcc320111::TestingRec28     size_t bytesUsed() const override { return sizeof(fKey) + sizeof(fValue); }
29 
Visitor__anon050bcc320111::TestingRec30     static bool Visitor(const SkResourceCache::Rec& baseRec, void* context) {
31         const TestingRec& rec = static_cast<const TestingRec&>(baseRec);
32         intptr_t* result = (intptr_t*)context;
33 
34         *result = rec.fValue;
35         return true;
36     }
37 };
38 }
39 
40 static const int COUNT = 10;
41 static const int DIM = 256;
42 
test_cache(skiatest::Reporter * reporter,SkResourceCache & cache,bool testPurge)43 static void test_cache(skiatest::Reporter* reporter, SkResourceCache& cache, bool testPurge) {
44     for (int i = 0; i < COUNT; ++i) {
45         TestingKey key(i);
46         intptr_t value = -1;
47 
48         REPORTER_ASSERT(reporter, !cache.find(key, TestingRec::Visitor, &value));
49         REPORTER_ASSERT(reporter, -1 == value);
50 
51         cache.add(SkNEW_ARGS(TestingRec, (key, i)));
52 
53         REPORTER_ASSERT(reporter, cache.find(key, TestingRec::Visitor, &value));
54         REPORTER_ASSERT(reporter, i == value);
55     }
56 
57     if (testPurge) {
58         // stress test, should trigger purges
59         for (int i = 0; i < COUNT * 100; ++i) {
60             TestingKey key(i);
61             cache.add(SkNEW_ARGS(TestingRec, (key, i)));
62         }
63     }
64 
65     // test the originals after all that purging
66     for (int i = 0; i < COUNT; ++i) {
67         intptr_t value;
68         (void)cache.find(TestingKey(i), TestingRec::Visitor, &value);
69     }
70 
71     cache.setTotalByteLimit(0);
72 }
73 
test_cache_purge_shared_id(skiatest::Reporter * reporter,SkResourceCache & cache)74 static void test_cache_purge_shared_id(skiatest::Reporter* reporter, SkResourceCache& cache) {
75     for (int i = 0; i < COUNT; ++i) {
76         TestingKey key(i, i & 1);   // every other key will have a 1 for its sharedID
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 sharedID
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 sharedID
95         intptr_t value = -1;
96 
97         if (i & 1) {
98             REPORTER_ASSERT(reporter, !cache.find(key, TestingRec::Visitor, &value));
99         } else {
100             REPORTER_ASSERT(reporter, cache.find(key, TestingRec::Visitor, &value));
101             REPORTER_ASSERT(reporter, value == i);
102         }
103     }
104 }
105 
106 #include "SkDiscardableMemoryPool.h"
107 
108 static SkDiscardableMemoryPool* gPool;
pool_factory(size_t bytes)109 static SkDiscardableMemory* pool_factory(size_t bytes) {
110     SkASSERT(gPool);
111     return gPool->create(bytes);
112 }
113 
DEF_TEST(ImageCache,reporter)114 DEF_TEST(ImageCache, reporter) {
115     static const size_t defLimit = DIM * DIM * 4 * COUNT + 1024;    // 1K slop
116 
117     {
118         SkResourceCache cache(defLimit);
119         test_cache(reporter, cache, true);
120     }
121     {
122         SkAutoTUnref<SkDiscardableMemoryPool> pool(
123                 SkDiscardableMemoryPool::Create(defLimit, NULL));
124         gPool = pool.get();
125         SkResourceCache cache(pool_factory);
126         test_cache(reporter, cache, true);
127     }
128     {
129         SkResourceCache cache(SkDiscardableMemory::Create);
130         test_cache(reporter, cache, false);
131     }
132     {
133         SkResourceCache cache(defLimit);
134         test_cache_purge_shared_id(reporter, cache);
135     }
136 }
137 
DEF_TEST(ImageCache_doubleAdd,r)138 DEF_TEST(ImageCache_doubleAdd, r) {
139     // Adding the same key twice should be safe.
140     SkResourceCache cache(4096);
141 
142     TestingKey key(1);
143 
144     cache.add(SkNEW_ARGS(TestingRec, (key, 2)));
145     cache.add(SkNEW_ARGS(TestingRec, (key, 3)));
146 
147     // Lookup can return either value.
148     intptr_t value = -1;
149     REPORTER_ASSERT(r, cache.find(key, TestingRec::Visitor, &value));
150     REPORTER_ASSERT(r, 2 == value || 3 == value);
151 }
152