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 #if SK_SUPPORT_GPU | 8 #if SK_SUPPORT_GPU |
9 | 9 |
10 #include "GrContext.h" | 10 #include "GrContext.h" |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 } | 58 } |
59 | 59 |
60 context->setResourceCacheLimits(oldMaxNum, oldMaxBytes); | 60 context->setResourceCacheLimits(oldMaxNum, oldMaxBytes); |
61 } | 61 } |
62 | 62 |
63 class TestResource : public GrGpuResource { | 63 class TestResource : public GrGpuResource { |
64 static const size_t kDefaultSize = 100; | 64 static const size_t kDefaultSize = 100; |
65 | 65 |
66 public: | 66 public: |
67 SK_DECLARE_INST_COUNT(TestResource); | 67 SK_DECLARE_INST_COUNT(TestResource); |
68 TestResource(GrGpu* gpu, size_t size = kDefaultSize) | 68 TestResource(GrGpu* gpu) |
69 : INHERITED(gpu, false) | 69 : INHERITED(gpu, false) |
70 , fCache(NULL) | 70 , fCache(NULL) |
71 , fToDelete(NULL) | 71 , fToDelete(NULL) |
72 , fSize(size) { | 72 , fSize(kDefaultSize) { |
73 ++fNumAlive; | 73 ++fNumAlive; |
74 this->registerWithCache(); | 74 this->registerWithCache(); |
75 } | 75 } |
| 76 |
| 77 TestResource(GrGpu* gpu, const GrResourceKey& scratchKey) |
| 78 : INHERITED(gpu, false) |
| 79 , fCache(NULL) |
| 80 , fToDelete(NULL) |
| 81 , fSize(kDefaultSize) { |
| 82 this->setScratchKey(scratchKey); |
| 83 ++fNumAlive; |
| 84 this->registerWithCache(); |
| 85 } |
76 | 86 |
77 ~TestResource() { | 87 ~TestResource() { |
78 --fNumAlive; | 88 --fNumAlive; |
79 if (fToDelete) { | 89 if (fToDelete) { |
80 // Breaks our little 2-element cycle below. | 90 // Breaks our little 2-element cycle below. |
81 fToDelete->setDeleteWhenDestroyed(NULL, NULL); | 91 fToDelete->setDeleteWhenDestroyed(NULL, NULL); |
82 fCache->deleteResource(fToDelete->getCacheEntry()); | 92 fCache->deleteResource(fToDelete->getCacheEntry()); |
83 } | 93 } |
84 this->release(); | 94 this->release(); |
85 } | 95 } |
(...skipping 15 matching lines...) Expand all Loading... |
101 private: | 111 private: |
102 GrResourceCache* fCache; | 112 GrResourceCache* fCache; |
103 TestResource* fToDelete; | 113 TestResource* fToDelete; |
104 size_t fSize; | 114 size_t fSize; |
105 static int fNumAlive; | 115 static int fNumAlive; |
106 | 116 |
107 typedef GrGpuResource INHERITED; | 117 typedef GrGpuResource INHERITED; |
108 }; | 118 }; |
109 int TestResource::fNumAlive = 0; | 119 int TestResource::fNumAlive = 0; |
110 | 120 |
111 static void test_purge_invalidated(skiatest::Reporter* reporter, GrContext* cont
ext) { | 121 static void test_duplicate_scratch_key(skiatest::Reporter* reporter) { |
112 GrCacheID::Domain domain = GrCacheID::GenerateDomain(); | 122 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); |
| 123 REPORTER_ASSERT(reporter, SkToBool(context)); |
| 124 if (NULL == context) { |
| 125 return; |
| 126 } |
| 127 context->setResourceCacheLimits(5, 30000); |
| 128 GrResourceCache* cache = context->getResourceCache(); |
| 129 SkDEBUGCODE(GrResourceCache2* cache2 = context->getResourceCache2();) |
| 130 cache->purgeAllUnlocked(); |
| 131 SkASSERT(0 == cache->getCachedResourceCount() && 0 == cache->getCachedResour
ceBytes()); |
| 132 |
113 GrCacheID::Key keyData; | 133 GrCacheID::Key keyData; |
114 keyData.fData64[0] = 5; | 134 GrCacheID::Domain domain = GrResourceKey::ScratchDomain(); |
115 keyData.fData64[1] = 18; | |
116 GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType(); | 135 GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType(); |
117 GrResourceKey key(GrCacheID(domain, keyData), t, 0); | 136 GrResourceKey scratchKey(GrCacheID(domain, keyData), t, 0); |
118 | 137 |
| 138 // Create two resources that have the same scratch key. |
| 139 TestResource* a = new TestResource(context->getGpu(), scratchKey); |
| 140 TestResource* b = new TestResource(context->getGpu(), scratchKey); |
| 141 a->setSize(11); |
| 142 b->setSize(12); |
| 143 // Scratch resources are registered with GrResourceCache2 just by existing.
There are 2. |
| 144 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache2->countScratchEntriesForKey
(scratchKey));) |
| 145 |
| 146 REPORTER_ASSERT(reporter, cache->addResource(scratchKey, a)); |
| 147 |
| 148 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache2->countScratchEntriesForKey
(scratchKey));) |
| 149 |
| 150 // Can't add the same resource twice. |
| 151 REPORTER_ASSERT(reporter, !cache->addResource(scratchKey, a)); |
| 152 REPORTER_ASSERT(reporter, 1 == cache->getCachedResourceCount()); |
| 153 REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getCachedResourceByte
s()); |
| 154 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache2->countScratchEntriesForKey
(scratchKey));) |
| 155 |
| 156 // Add a second with the same key. |
| 157 REPORTER_ASSERT(reporter, cache->addResource(scratchKey, b)); |
| 158 REPORTER_ASSERT(reporter, 2 == cache->getCachedResourceCount()); |
| 159 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == |
| 160 cache->getCachedResourceBytes()); |
| 161 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
| 162 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache2->countScratchEntriesForKey
(scratchKey));) |
| 163 |
| 164 // Our refs mean that the resources are non purgable. |
| 165 cache->purgeAllUnlocked(); |
| 166 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
| 167 REPORTER_ASSERT(reporter, 2 == cache->getCachedResourceCount()); |
| 168 |
| 169 // Unref but don't purge |
| 170 a->unref(); |
| 171 b->unref(); |
| 172 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
| 173 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache2->countScratchEntriesForKey
(scratchKey));) |
| 174 |
| 175 // Purge again. This time resources should be purgable. |
| 176 cache->purgeAllUnlocked(); |
| 177 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
| 178 REPORTER_ASSERT(reporter, 0 == cache->getCachedResourceCount()); |
| 179 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache2->countScratchEntriesForKey
(scratchKey));) |
| 180 } |
| 181 |
| 182 static void test_duplicate_content_key(skiatest::Reporter* reporter) { |
| 183 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); |
| 184 REPORTER_ASSERT(reporter, SkToBool(context)); |
| 185 if (NULL == context) { |
| 186 return; |
| 187 } |
119 context->setResourceCacheLimits(5, 30000); | 188 context->setResourceCacheLimits(5, 30000); |
120 GrResourceCache* cache = context->getResourceCache(); | 189 GrResourceCache* cache = context->getResourceCache(); |
121 cache->purgeAllUnlocked(); | 190 cache->purgeAllUnlocked(); |
122 SkASSERT(0 == cache->getCachedResourceCount() && 0 == cache->getCachedResour
ceBytes()); | 191 SkASSERT(0 == cache->getCachedResourceCount() && 0 == cache->getCachedResour
ceBytes()); |
123 | 192 |
124 // Add two resources with the same key that delete each other from the cache
when destroyed. | 193 GrCacheID::Domain domain = GrCacheID::GenerateDomain(); |
| 194 GrCacheID::Key keyData; |
| 195 memset(&keyData, 0, sizeof(keyData)); |
| 196 GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType(); |
| 197 GrResourceKey key(GrCacheID(domain, keyData), t, 0); |
| 198 |
| 199 |
| 200 // Create two resources that we will attempt to register with the same conte
nt key. |
125 TestResource* a = new TestResource(context->getGpu()); | 201 TestResource* a = new TestResource(context->getGpu()); |
126 TestResource* b = new TestResource(context->getGpu()); | 202 TestResource* b = new TestResource(context->getGpu()); |
127 cache->addResource(key, a); | 203 a->setSize(11); |
128 cache->addResource(key, b); | 204 b->setSize(12); |
129 // Circle back. | 205 REPORTER_ASSERT(reporter, cache->addResource(key, a)); |
130 a->setDeleteWhenDestroyed(cache, b); | 206 // Can't add the same or another resource with the same key. |
131 b->setDeleteWhenDestroyed(cache, a); | 207 REPORTER_ASSERT(reporter, !cache->addResource(key, a)); |
| 208 REPORTER_ASSERT(reporter, !cache->addResource(key, b)); |
| 209 REPORTER_ASSERT(reporter, 1 == cache->getCachedResourceCount()); |
| 210 REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getCachedResourceByte
s()); |
| 211 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
| 212 |
| 213 b->unref(); |
| 214 cache->purgeAllUnlocked(); |
| 215 a->setSize(10); |
| 216 REPORTER_ASSERT(reporter, 1 == cache->getCachedResourceCount()); |
| 217 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
| 218 |
132 a->unref(); | 219 a->unref(); |
133 b->unref(); | 220 cache->purgeAllUnlocked(); |
134 | 221 REPORTER_ASSERT(reporter, 0 == cache->getCachedResourceCount()); |
135 // Add a third independent resource also with the same key. | 222 REPORTER_ASSERT(reporter, 0 == cache->getCachedResourceBytes()); |
136 GrGpuResource* r = new TestResource(context->getGpu()); | |
137 cache->addResource(key, r); | |
138 r->unref(); | |
139 | |
140 // Invalidate all three, all three should be purged and destroyed. | |
141 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive()); | |
142 const GrResourceInvalidatedMessage msg = { key }; | |
143 SkMessageBus<GrResourceInvalidatedMessage>::Post(msg); | |
144 cache->purgeAsNeeded(); | |
145 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); | 223 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
146 } | 224 } |
147 | 225 |
148 static void test_cache_delete_on_destruction(skiatest::Reporter* reporter, | 226 static void test_purge_invalidated(skiatest::Reporter* reporter) { |
149 GrContext* context) { | 227 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); |
| 228 REPORTER_ASSERT(reporter, SkToBool(context)); |
| 229 if (NULL == context) { |
| 230 return; |
| 231 } |
| 232 |
150 GrCacheID::Domain domain = GrCacheID::GenerateDomain(); | 233 GrCacheID::Domain domain = GrCacheID::GenerateDomain(); |
151 GrCacheID::Key keyData; | 234 GrCacheID::Key keyData; |
152 keyData.fData64[0] = 5; | 235 memset(&keyData, 0, sizeof(keyData)); |
153 keyData.fData64[1] = 0; | 236 |
154 GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType(); | 237 GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType(); |
155 | 238 |
156 GrResourceKey key(GrCacheID(domain, keyData), t, 0); | 239 keyData.fData64[0] = 1; |
| 240 GrResourceKey key1(GrCacheID(domain, keyData), t, 0); |
| 241 keyData.fData64[0] = 2; |
| 242 GrResourceKey key2(GrCacheID(domain, keyData), t, 0); |
| 243 keyData.fData64[0] = 3; |
| 244 GrResourceKey key3(GrCacheID(domain, keyData), t, 0); |
| 245 |
| 246 context->setResourceCacheLimits(5, 30000); |
| 247 GrResourceCache* cache = context->getResourceCache(); |
| 248 GrResourceCache2* cache2 = context->getResourceCache2(); |
| 249 cache->purgeAllUnlocked(); |
| 250 SkASSERT(0 == cache->getCachedResourceCount() && 0 == cache->getCachedResour
ceBytes()); |
| 251 |
| 252 // Add three resources to the cache. |
| 253 TestResource* a = new TestResource(context->getGpu()); |
| 254 TestResource* b = new TestResource(context->getGpu()); |
| 255 TestResource* c = new TestResource(context->getGpu()); |
| 256 cache->addResource(key1, a); |
| 257 cache->addResource(key2, b); |
| 258 cache->addResource(key3, c); |
| 259 a->unref(); |
| 260 b->unref(); |
| 261 c->unref(); |
| 262 |
| 263 REPORTER_ASSERT(reporter, cache2->hasContentKey(key1)); |
| 264 REPORTER_ASSERT(reporter, cache2->hasContentKey(key2)); |
| 265 REPORTER_ASSERT(reporter, cache2->hasContentKey(key3)); |
| 266 |
| 267 // Invalidate two of the three, they should be purged and destroyed. |
| 268 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive()); |
| 269 const GrResourceInvalidatedMessage msg1 = { key1 }; |
| 270 SkMessageBus<GrResourceInvalidatedMessage>::Post(msg1); |
| 271 const GrResourceInvalidatedMessage msg2 = { key2 }; |
| 272 SkMessageBus<GrResourceInvalidatedMessage>::Post(msg2); |
| 273 cache->purgeAsNeeded(); |
| 274 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
| 275 REPORTER_ASSERT(reporter, !cache2->hasContentKey(key1)); |
| 276 REPORTER_ASSERT(reporter, !cache2->hasContentKey(key2)); |
| 277 REPORTER_ASSERT(reporter, cache2->hasContentKey(key3)); |
| 278 |
| 279 // Invalidate the third. |
| 280 const GrResourceInvalidatedMessage msg3 = { key3 }; |
| 281 SkMessageBus<GrResourceInvalidatedMessage>::Post(msg3); |
| 282 cache->purgeAsNeeded(); |
| 283 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
| 284 REPORTER_ASSERT(reporter, !cache2->hasContentKey(key3)); |
| 285 } |
| 286 |
| 287 static void test_cache_delete_on_destruction(skiatest::Reporter* reporter) { |
| 288 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); |
| 289 REPORTER_ASSERT(reporter, SkToBool(context)); |
| 290 if (NULL == context) { |
| 291 return; |
| 292 } |
| 293 |
| 294 GrCacheID::Domain domain = GrCacheID::GenerateDomain(); |
| 295 GrCacheID::Key keyData; |
| 296 memset(&keyData, 0, sizeof(keyData)); |
| 297 GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType(); |
| 298 |
| 299 keyData.fData64[0] = 1; |
| 300 GrResourceKey key1(GrCacheID(domain, keyData), t, 0); |
| 301 |
| 302 keyData.fData64[0] = 2; |
| 303 GrResourceKey key2(GrCacheID(domain, keyData), t, 0); |
157 | 304 |
158 { | 305 { |
159 context->setResourceCacheLimits(3, 30000); | 306 context->setResourceCacheLimits(3, 30000); |
160 GrResourceCache* cache = context->getResourceCache(); | 307 GrResourceCache* cache = context->getResourceCache(); |
161 cache->purgeAllUnlocked(); | 308 cache->purgeAllUnlocked(); |
162 SkASSERT(0 == cache->getCachedResourceCount() && 0 == cache->getCachedRe
sourceBytes()); | 309 SkASSERT(0 == cache->getCachedResourceCount() && 0 == cache->getCachedRe
sourceBytes()); |
163 | 310 |
164 TestResource* a = new TestResource(context->getGpu()); | 311 TestResource* a = new TestResource(context->getGpu()); |
165 TestResource* b = new TestResource(context->getGpu()); | 312 TestResource* b = new TestResource(context->getGpu()); |
166 cache->addResource(key, a); | 313 cache->addResource(key1, a); |
167 cache->addResource(key, b); | 314 cache->addResource(key2, b); |
168 | 315 |
169 a->setDeleteWhenDestroyed(cache, b); | 316 a->setDeleteWhenDestroyed(cache, b); |
170 b->setDeleteWhenDestroyed(cache, a); | 317 b->setDeleteWhenDestroyed(cache, a); |
171 | 318 |
172 a->unref(); | 319 a->unref(); |
173 b->unref(); | 320 b->unref(); |
| 321 |
174 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); | 322 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
| 323 |
175 cache->purgeAllUnlocked(); | 324 cache->purgeAllUnlocked(); |
176 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); | 325 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
177 } | 326 } |
178 { | 327 { |
179 context->setResourceCacheLimits(3, 30000); | 328 context->setResourceCacheLimits(3, 30000); |
180 GrResourceCache* cache = context->getResourceCache(); | 329 GrResourceCache* cache = context->getResourceCache(); |
181 cache->purgeAllUnlocked(); | 330 cache->purgeAllUnlocked(); |
182 SkASSERT(0 == cache->getCachedResourceCount() && 0 == cache->getCachedRe
sourceBytes()); | 331 SkASSERT(0 == cache->getCachedResourceCount() && 0 == cache->getCachedRe
sourceBytes()); |
| 332 |
183 TestResource* a = new TestResource(context->getGpu()); | 333 TestResource* a = new TestResource(context->getGpu()); |
184 TestResource* b = new TestResource(context->getGpu()); | 334 TestResource* b = new TestResource(context->getGpu()); |
185 cache->addResource(key, a); | 335 cache->addResource(key1, a); |
186 cache->addResource(key, b); | 336 cache->addResource(key2, b); |
187 | 337 |
188 a->setDeleteWhenDestroyed(cache, b); | 338 a->setDeleteWhenDestroyed(cache, b); |
189 b->setDeleteWhenDestroyed(cache, a); | 339 b->setDeleteWhenDestroyed(cache, a); |
190 | 340 |
191 a->unref(); | 341 a->unref(); |
192 b->unref(); | 342 b->unref(); |
193 | 343 |
194 cache->deleteResource(a->getCacheEntry()); | 344 cache->deleteResource(a->getCacheEntry()); |
195 | |
196 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); | 345 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
197 } | 346 } |
198 } | 347 } |
199 | 348 |
200 static void test_resource_size_changed(skiatest::Reporter* reporter, | 349 static void test_resource_size_changed(skiatest::Reporter* reporter) { |
201 GrContext* context) { | 350 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); |
| 351 REPORTER_ASSERT(reporter, SkToBool(context)); |
| 352 if (NULL == context) { |
| 353 return; |
| 354 } |
| 355 |
202 GrCacheID::Domain domain = GrCacheID::GenerateDomain(); | 356 GrCacheID::Domain domain = GrCacheID::GenerateDomain(); |
203 GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType(); | 357 GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType(); |
204 | 358 |
205 GrCacheID::Key key1Data; | 359 GrCacheID::Key key1Data; |
206 key1Data.fData64[0] = 0; | 360 key1Data.fData64[0] = 0; |
207 key1Data.fData64[1] = 0; | 361 key1Data.fData64[1] = 0; |
208 GrResourceKey key1(GrCacheID(domain, key1Data), t, 0); | 362 GrResourceKey key1(GrCacheID(domain, key1Data), t, 0); |
209 | 363 |
210 GrCacheID::Key key2Data; | 364 GrCacheID::Key key2Data; |
211 key2Data.fData64[0] = 1; | 365 key2Data.fData64[0] = 1; |
212 key2Data.fData64[1] = 0; | 366 key2Data.fData64[1] = 0; |
213 GrResourceKey key2(GrCacheID(domain, key2Data), t, 0); | 367 GrResourceKey key2(GrCacheID(domain, key2Data), t, 0); |
214 | 368 |
215 // Test changing resources sizes (both increase & decrease). | 369 // Test changing resources sizes (both increase & decrease). |
216 { | 370 { |
217 context->setResourceCacheLimits(3, 30000); | 371 context->setResourceCacheLimits(3, 30000); |
218 GrResourceCache* cache = context->getResourceCache(); | 372 GrResourceCache* cache = context->getResourceCache(); |
| 373 GrResourceCache2* cache2 = context->getResourceCache2(); |
219 cache->purgeAllUnlocked(); | 374 cache->purgeAllUnlocked(); |
220 SkASSERT(0 == cache->getCachedResourceCount() && 0 == cache->getCachedRe
sourceBytes()); | 375 SkASSERT(0 == cache->getCachedResourceCount() && 0 == cache->getCachedRe
sourceBytes()); |
221 | 376 |
222 TestResource* a = new TestResource(context->getGpu()); | 377 TestResource* a = new TestResource(context->getGpu()); |
223 a->setSize(100); // Test didChangeGpuMemorySize() when not in the cache. | 378 a->setSize(100); // Test didChangeGpuMemorySize() when not in the cache. |
224 cache->addResource(key1, a); | 379 cache->addResource(key1, a); |
225 a->unref(); | 380 a->unref(); |
226 | 381 |
227 TestResource* b = new TestResource(context->getGpu()); | 382 TestResource* b = new TestResource(context->getGpu()); |
228 b->setSize(100); | 383 b->setSize(100); |
229 cache->addResource(key2, b); | 384 cache->addResource(key2, b); |
230 b->unref(); | 385 b->unref(); |
231 | 386 |
232 REPORTER_ASSERT(reporter, 200 == cache->getCachedResourceBytes()); | 387 REPORTER_ASSERT(reporter, 200 == cache->getCachedResourceBytes()); |
233 REPORTER_ASSERT(reporter, 2 == cache->getCachedResourceCount()); | 388 REPORTER_ASSERT(reporter, 2 == cache->getCachedResourceCount()); |
234 | 389 { |
235 static_cast<TestResource*>(cache->find(key2))->setSize(200); | 390 SkAutoTUnref<TestResource> find2(static_cast<TestResource*>(cache2->
findAndRefContentResource(key2))); |
236 static_cast<TestResource*>(cache->find(key1))->setSize(50); | 391 find2->setSize(200); |
| 392 SkAutoTUnref<TestResource> find1(static_cast<TestResource*>(cache2->
findAndRefContentResource(key1))); |
| 393 find1->setSize(50); |
| 394 } |
237 | 395 |
238 REPORTER_ASSERT(reporter, 250 == cache->getCachedResourceBytes()); | 396 REPORTER_ASSERT(reporter, 250 == cache->getCachedResourceBytes()); |
239 REPORTER_ASSERT(reporter, 2 == cache->getCachedResourceCount()); | 397 REPORTER_ASSERT(reporter, 2 == cache->getCachedResourceCount()); |
240 } | 398 } |
241 | 399 |
242 // Test increasing a resources size beyond the cache budget. | 400 // Test increasing a resources size beyond the cache budget. |
243 { | 401 { |
244 context->setResourceCacheLimits(2, 300); | 402 context->setResourceCacheLimits(2, 300); |
245 GrResourceCache* cache = context->getResourceCache(); | 403 GrResourceCache* cache = context->getResourceCache(); |
| 404 GrResourceCache2* cache2 = context->getResourceCache2(); |
246 cache->purgeAllUnlocked(); | 405 cache->purgeAllUnlocked(); |
247 SkASSERT(0 == cache->getCachedResourceCount() && 0 == cache->getCachedRe
sourceBytes()); | 406 SkASSERT(0 == cache->getCachedResourceCount() && 0 == cache->getCachedRe
sourceBytes()); |
248 | 407 |
249 TestResource* a = new TestResource(context->getGpu(), 100); | 408 TestResource* a = new TestResource(context->getGpu()); |
| 409 a->setSize(100); |
250 cache->addResource(key1, a); | 410 cache->addResource(key1, a); |
251 a->unref(); | 411 a->unref(); |
252 | 412 |
253 TestResource* b = new TestResource(context->getGpu(), 100); | 413 TestResource* b = new TestResource(context->getGpu()); |
| 414 b->setSize(100); |
254 cache->addResource(key2, b); | 415 cache->addResource(key2, b); |
255 b->unref(); | 416 b->unref(); |
256 | 417 |
257 REPORTER_ASSERT(reporter, 200 == cache->getCachedResourceBytes()); | 418 REPORTER_ASSERT(reporter, 200 == cache->getCachedResourceBytes()); |
258 REPORTER_ASSERT(reporter, 2 == cache->getCachedResourceCount()); | 419 REPORTER_ASSERT(reporter, 2 == cache->getCachedResourceCount()); |
259 | 420 |
260 static_cast<TestResource*>(cache->find(key2))->setSize(201); | 421 { |
261 REPORTER_ASSERT(reporter, !cache->hasKey(key1)); | 422 SkAutoTUnref<TestResource> find2(static_cast<TestResource*>(cache2->
findAndRefContentResource(key2))); |
| 423 find2->setSize(201); |
| 424 } |
| 425 REPORTER_ASSERT(reporter, !cache2->hasContentKey(key1)); |
262 | 426 |
263 REPORTER_ASSERT(reporter, 201 == cache->getCachedResourceBytes()); | 427 REPORTER_ASSERT(reporter, 201 == cache->getCachedResourceBytes()); |
264 REPORTER_ASSERT(reporter, 1 == cache->getCachedResourceCount()); | 428 REPORTER_ASSERT(reporter, 1 == cache->getCachedResourceCount()); |
265 } | 429 } |
266 } | 430 } |
267 | 431 |
268 //////////////////////////////////////////////////////////////////////////////// | 432 //////////////////////////////////////////////////////////////////////////////// |
269 DEF_GPUTEST(ResourceCache, reporter, factory) { | 433 DEF_GPUTEST(ResourceCache, reporter, factory) { |
270 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) { | 434 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) { |
271 GrContextFactory::GLContextType glType = static_cast<GrContextFactory::G
LContextType>(type); | 435 GrContextFactory::GLContextType glType = static_cast<GrContextFactory::G
LContextType>(type); |
272 if (!GrContextFactory::IsRenderingGLContext(glType)) { | 436 if (!GrContextFactory::IsRenderingGLContext(glType)) { |
273 continue; | 437 continue; |
274 } | 438 } |
275 GrContext* context = factory->get(glType); | 439 GrContext* context = factory->get(glType); |
276 if (NULL == context) { | 440 if (NULL == context) { |
277 continue; | 441 continue; |
278 } | 442 } |
279 GrSurfaceDesc desc; | 443 GrSurfaceDesc desc; |
280 desc.fConfig = kSkia8888_GrPixelConfig; | 444 desc.fConfig = kSkia8888_GrPixelConfig; |
281 desc.fFlags = kRenderTarget_GrSurfaceFlag; | 445 desc.fFlags = kRenderTarget_GrSurfaceFlag; |
282 desc.fWidth = gWidth; | 446 desc.fWidth = gWidth; |
283 desc.fHeight = gHeight; | 447 desc.fHeight = gHeight; |
284 SkImageInfo info = SkImageInfo::MakeN32Premul(gWidth, gHeight); | 448 SkImageInfo info = SkImageInfo::MakeN32Premul(gWidth, gHeight); |
285 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(context, info
)); | 449 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(context, info
)); |
286 test_cache(reporter, context, surface->getCanvas()); | 450 test_cache(reporter, context, surface->getCanvas()); |
287 } | 451 } |
288 | 452 |
289 // The below tests use a mock context. | 453 // The below tests create their own mock contexts. |
290 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); | 454 test_duplicate_content_key(reporter); |
291 REPORTER_ASSERT(reporter, SkToBool(context)); | 455 test_duplicate_scratch_key(reporter); |
292 if (NULL == context) { | 456 test_purge_invalidated(reporter); |
293 return; | 457 test_cache_delete_on_destruction(reporter); |
294 } | 458 test_resource_size_changed(reporter); |
295 | |
296 test_purge_invalidated(reporter, context); | |
297 test_cache_delete_on_destruction(reporter, context); | |
298 test_resource_size_changed(reporter, context); | |
299 } | 459 } |
300 | 460 |
301 #endif | 461 #endif |
OLD | NEW |