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 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 | 192 |
193 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive()); | 193 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive()); |
194 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount()); | 194 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount()); |
195 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMe
morySize() + | 195 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMe
morySize() + |
196 d->gpuMemorySize() == cache->getResourceBytes()); | 196 d->gpuMemorySize() == cache->getResourceBytes()); |
197 | 197 |
198 // Should be safe to purge without deleting the resources since we still hav
e refs. | 198 // Should be safe to purge without deleting the resources since we still hav
e refs. |
199 cache->purgeAllUnlocked(); | 199 cache->purgeAllUnlocked(); |
200 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive()); | 200 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive()); |
201 | 201 |
202 // Since the resources have neither content nor scratch keys, delete immedia
tely upon unref. | 202 // Since the resources have neither unique nor scratch keys, delete immediat
ely upon unref. |
203 | 203 |
204 a->unref(); | 204 a->unref(); |
205 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive()); | 205 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive()); |
206 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount()); | 206 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount()); |
207 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() + d->gpuMe
morySize() == | 207 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() + d->gpuMe
morySize() == |
208 cache->getResourceBytes()); | 208 cache->getResourceBytes()); |
209 | 209 |
210 c->unref(); | 210 c->unref(); |
211 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); | 211 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
212 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); | 212 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
213 REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() == | 213 REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() == |
214 cache->getResourceBytes()); | 214 cache->getResourceBytes()); |
215 | 215 |
216 d->unref(); | 216 d->unref(); |
217 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); | 217 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
218 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); | 218 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
219 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes()); | 219 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes()); |
220 | 220 |
221 b->unref(); | 221 b->unref(); |
222 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); | 222 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
223 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); | 223 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
224 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes()); | 224 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes()); |
225 } | 225 } |
226 | 226 |
227 // Each integer passed as a template param creates a new domain. | 227 // Each integer passed as a template param creates a new domain. |
228 template <int> static void make_content_key(GrContentKey* key, int data) { | 228 template <int> static void make_unique_key(GrUniqueKey* key, int data) { |
229 static GrContentKey::Domain d = GrContentKey::GenerateDomain(); | 229 static GrUniqueKey::Domain d = GrUniqueKey::GenerateDomain(); |
230 GrContentKey::Builder builder(key, d, 1); | 230 GrUniqueKey::Builder builder(key, d, 1); |
231 builder[0] = data; | 231 builder[0] = data; |
232 } | 232 } |
233 | 233 |
234 static void test_budgeting(skiatest::Reporter* reporter) { | 234 static void test_budgeting(skiatest::Reporter* reporter) { |
235 Mock mock(10, 300); | 235 Mock mock(10, 300); |
236 GrContext* context = mock.context(); | 236 GrContext* context = mock.context(); |
237 GrResourceCache* cache = mock.cache(); | 237 GrResourceCache* cache = mock.cache(); |
238 | 238 |
239 GrContentKey contentKey; | 239 GrUniqueKey uniqueKey; |
240 make_content_key<0>(&contentKey, 0); | 240 make_unique_key<0>(&uniqueKey, 0); |
241 | 241 |
242 // Create a scratch, a content, and a wrapped resource | 242 // Create a scratch, a unique, and a wrapped resource |
243 TestResource* scratch = | 243 TestResource* scratch = |
244 TestResource::CreateScratch(context->getGpu(), TestResource::kB_Simu
latedProperty); | 244 TestResource::CreateScratch(context->getGpu(), TestResource::kB_Simu
latedProperty); |
245 scratch->setSize(10); | 245 scratch->setSize(10); |
246 TestResource* content = SkNEW_ARGS(TestResource, (context->getGpu())); | 246 TestResource* unique = SkNEW_ARGS(TestResource, (context->getGpu())); |
247 content->setSize(11); | 247 unique->setSize(11); |
248 REPORTER_ASSERT(reporter, content->resourcePriv().setContentKey(contentKey))
; | 248 REPORTER_ASSERT(reporter, unique->resourcePriv().setUniqueKey(uniqueKey)); |
249 TestResource* wrapped = SkNEW_ARGS(TestResource, | 249 TestResource* wrapped = SkNEW_ARGS(TestResource, |
250 (context->getGpu(), GrGpuResource::kWrapp
ed_LifeCycle)); | 250 (context->getGpu(), GrGpuResource::kWrapp
ed_LifeCycle)); |
251 wrapped->setSize(12); | 251 wrapped->setSize(12); |
252 TestResource* unbudgeted = SkNEW_ARGS(TestResource, | 252 TestResource* unbudgeted = SkNEW_ARGS(TestResource, |
253 (context->getGpu(), GrGpuResource::kUn
cached_LifeCycle)); | 253 (context->getGpu(), GrGpuResource::kUn
cached_LifeCycle)); |
254 unbudgeted->setSize(13); | 254 unbudgeted->setSize(13); |
255 | 255 |
256 // Make sure we can't add a content key to the wrapped resource | 256 // Make sure we can't add a unique key to the wrapped resource |
257 GrContentKey contentKey2; | 257 GrUniqueKey uniqueKey2; |
258 make_content_key<0>(&contentKey2, 1); | 258 make_unique_key<0>(&uniqueKey2, 1); |
259 REPORTER_ASSERT(reporter, !wrapped->resourcePriv().setContentKey(contentKey2
)); | 259 REPORTER_ASSERT(reporter, !wrapped->resourcePriv().setUniqueKey(uniqueKey2))
; |
260 REPORTER_ASSERT(reporter, NULL == cache->findAndRefContentResource(contentKe
y2)); | 260 REPORTER_ASSERT(reporter, NULL == cache->findAndRefUniqueResource(uniqueKey2
)); |
261 | 261 |
262 // Make sure sizes are as we expect | 262 // Make sure sizes are as we expect |
263 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount()); | 263 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount()); |
264 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize(
) + | 264 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize()
+ |
265 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySi
ze() == | 265 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySi
ze() == |
266 cache->getResourceBytes()); | 266 cache->getResourceBytes()); |
267 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount()); | 267 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount()); |
268 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize(
) == | 268 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize()
== |
269 cache->getBudgetedResourceBytes()); | 269 cache->getBudgetedResourceBytes()); |
270 | 270 |
271 // Our refs mean that the resources are non purgeable. | 271 // Our refs mean that the resources are non purgeable. |
272 cache->purgeAllUnlocked(); | 272 cache->purgeAllUnlocked(); |
273 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount()); | 273 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount()); |
274 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize(
) + | 274 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize()
+ |
275 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySi
ze() == | 275 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySi
ze() == |
276 cache->getResourceBytes()); | 276 cache->getResourceBytes()); |
277 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount()); | 277 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount()); |
278 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize(
) == | 278 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize()
== |
279 cache->getBudgetedResourceBytes()); | 279 cache->getBudgetedResourceBytes()); |
280 | 280 |
281 // Unreffing the wrapped resource should free it right away. | 281 // Unreffing the wrapped resource should free it right away. |
282 wrapped->unref(); | 282 wrapped->unref(); |
283 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount()); | 283 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount()); |
284 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize(
) + | 284 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize()
+ |
285 unbudgeted->gpuMemorySize() == cache->getResourceB
ytes()); | 285 unbudgeted->gpuMemorySize() == cache->getResourceB
ytes()); |
286 | 286 |
287 // Now try freeing the budgeted resources first | 287 // Now try freeing the budgeted resources first |
288 wrapped = SkNEW_ARGS(TestResource, (context->getGpu(), GrGpuResource::kWrapp
ed_LifeCycle)); | 288 wrapped = SkNEW_ARGS(TestResource, (context->getGpu(), GrGpuResource::kWrapp
ed_LifeCycle)); |
289 scratch->setSize(12); | 289 scratch->setSize(12); |
290 content->unref(); | 290 unique->unref(); |
291 cache->purgeAllUnlocked(); | 291 cache->purgeAllUnlocked(); |
292 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount()); | 292 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount()); |
293 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + wrapped->gpuMemorySize(
) + | 293 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + wrapped->gpuMemorySize(
) + |
294 unbudgeted->gpuMemorySize() == cache->getResourceB
ytes()); | 294 unbudgeted->gpuMemorySize() == cache->getResourceB
ytes()); |
295 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount()); | 295 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount()); |
296 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() == cache->getBudgetedReso
urceBytes()); | 296 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() == cache->getBudgetedReso
urceBytes()); |
297 | 297 |
298 scratch->unref(); | 298 scratch->unref(); |
299 cache->purgeAllUnlocked(); | 299 cache->purgeAllUnlocked(); |
300 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); | 300 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
(...skipping 13 matching lines...) Expand all Loading... |
314 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes()); | 314 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes()); |
315 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount()); | 315 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount()); |
316 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes()); | 316 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes()); |
317 } | 317 } |
318 | 318 |
319 static void test_unbudgeted(skiatest::Reporter* reporter) { | 319 static void test_unbudgeted(skiatest::Reporter* reporter) { |
320 Mock mock(10, 30000); | 320 Mock mock(10, 30000); |
321 GrContext* context = mock.context(); | 321 GrContext* context = mock.context(); |
322 GrResourceCache* cache = mock.cache(); | 322 GrResourceCache* cache = mock.cache(); |
323 | 323 |
324 GrContentKey contentKey; | 324 GrUniqueKey uniqueKey; |
325 make_content_key<0>(&contentKey, 0); | 325 make_unique_key<0>(&uniqueKey, 0); |
326 | 326 |
327 TestResource* scratch; | 327 TestResource* scratch; |
328 TestResource* content; | 328 TestResource* unique; |
329 TestResource* wrapped; | 329 TestResource* wrapped; |
330 TestResource* unbudgeted; | 330 TestResource* unbudgeted; |
331 | 331 |
332 // A large uncached or wrapped resource shouldn't evict anything. | 332 // A large uncached or wrapped resource shouldn't evict anything. |
333 scratch = TestResource::CreateScratch(context->getGpu(), TestResource::kB_Si
mulatedProperty); | 333 scratch = TestResource::CreateScratch(context->getGpu(), TestResource::kB_Si
mulatedProperty); |
334 scratch->setSize(10); | 334 scratch->setSize(10); |
335 scratch->unref(); | 335 scratch->unref(); |
336 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); | 336 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
337 REPORTER_ASSERT(reporter, 10 == cache->getResourceBytes()); | 337 REPORTER_ASSERT(reporter, 10 == cache->getResourceBytes()); |
338 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount()); | 338 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount()); |
339 REPORTER_ASSERT(reporter, 10 == cache->getBudgetedResourceBytes()); | 339 REPORTER_ASSERT(reporter, 10 == cache->getBudgetedResourceBytes()); |
340 | 340 |
341 content = SkNEW_ARGS(TestResource, (context->getGpu())); | 341 unique = SkNEW_ARGS(TestResource, (context->getGpu())); |
342 content->setSize(11); | 342 unique->setSize(11); |
343 REPORTER_ASSERT(reporter, content->resourcePriv().setContentKey(contentKey))
; | 343 REPORTER_ASSERT(reporter, unique->resourcePriv().setUniqueKey(uniqueKey)); |
344 content->unref(); | 344 unique->unref(); |
345 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); | 345 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
346 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes()); | 346 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes()); |
347 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount()); | 347 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount()); |
348 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes()); | 348 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes()); |
349 | 349 |
350 size_t large = 2 * cache->getResourceBytes(); | 350 size_t large = 2 * cache->getResourceBytes(); |
351 unbudgeted = SkNEW_ARGS(TestResource, | 351 unbudgeted = SkNEW_ARGS(TestResource, |
352 (context->getGpu(), large, GrGpuResource::kUncached_
LifeCycle)); | 352 (context->getGpu(), large, GrGpuResource::kUncached_
LifeCycle)); |
353 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount()); | 353 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount()); |
354 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes()); | 354 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes()); |
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
599 REPORTER_ASSERT(reporter, find == a || find == b); | 599 REPORTER_ASSERT(reporter, find == a || find == b); |
600 | 600 |
601 GrGpuResource* find2 = cache->findAndRefScratchResource(scratchKey2); | 601 GrGpuResource* find2 = cache->findAndRefScratchResource(scratchKey2); |
602 REPORTER_ASSERT(reporter, find2 != NULL); | 602 REPORTER_ASSERT(reporter, find2 != NULL); |
603 REPORTER_ASSERT(reporter, find2 == a || find2 == b); | 603 REPORTER_ASSERT(reporter, find2 == a || find2 == b); |
604 REPORTER_ASSERT(reporter, find2 != find); | 604 REPORTER_ASSERT(reporter, find2 != find); |
605 find2->unref(); | 605 find2->unref(); |
606 find->unref(); | 606 find->unref(); |
607 } | 607 } |
608 | 608 |
609 static void test_duplicate_content_key(skiatest::Reporter* reporter) { | 609 static void test_duplicate_unique_key(skiatest::Reporter* reporter) { |
610 Mock mock(5, 30000); | 610 Mock mock(5, 30000); |
611 GrContext* context = mock.context(); | 611 GrContext* context = mock.context(); |
612 GrResourceCache* cache = mock.cache(); | 612 GrResourceCache* cache = mock.cache(); |
613 | 613 |
614 GrContentKey key; | 614 GrUniqueKey key; |
615 make_content_key<0>(&key, 0); | 615 make_unique_key<0>(&key, 0); |
616 | 616 |
617 // Create two resources that we will attempt to register with the same conte
nt key. | 617 // Create two resources that we will attempt to register with the same uniqu
e key. |
618 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu())); | 618 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu())); |
619 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu())); | 619 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu())); |
620 a->setSize(11); | 620 a->setSize(11); |
621 b->setSize(12); | 621 b->setSize(12); |
622 | 622 |
623 // Can't set the same content key on two resources. | 623 // Can't set the same unique key on two resources. |
624 REPORTER_ASSERT(reporter, a->resourcePriv().setContentKey(key)); | 624 REPORTER_ASSERT(reporter, a->resourcePriv().setUniqueKey(key)); |
625 REPORTER_ASSERT(reporter, !b->resourcePriv().setContentKey(key)); | 625 REPORTER_ASSERT(reporter, !b->resourcePriv().setUniqueKey(key)); |
626 | 626 |
627 // Still have two resources because b is still reffed. | 627 // Still have two resources because b is still reffed. |
628 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); | 628 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
629 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == | 629 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == |
630 cache->getResourceBytes()); | 630 cache->getResourceBytes()); |
631 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); | 631 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
632 | 632 |
633 b->unref(); | 633 b->unref(); |
634 // Now b should be gone. | 634 // Now b should be gone. |
635 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); | 635 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
(...skipping 15 matching lines...) Expand all Loading... |
651 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); | 651 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
652 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes()); | 652 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes()); |
653 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); | 653 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
654 } | 654 } |
655 | 655 |
656 static void test_purge_invalidated(skiatest::Reporter* reporter) { | 656 static void test_purge_invalidated(skiatest::Reporter* reporter) { |
657 Mock mock(5, 30000); | 657 Mock mock(5, 30000); |
658 GrContext* context = mock.context(); | 658 GrContext* context = mock.context(); |
659 GrResourceCache* cache = mock.cache(); | 659 GrResourceCache* cache = mock.cache(); |
660 | 660 |
661 GrContentKey key1, key2, key3; | 661 GrUniqueKey key1, key2, key3; |
662 make_content_key<0>(&key1, 1); | 662 make_unique_key<0>(&key1, 1); |
663 make_content_key<0>(&key2, 2); | 663 make_unique_key<0>(&key2, 2); |
664 make_content_key<0>(&key3, 3); | 664 make_unique_key<0>(&key3, 3); |
665 | 665 |
666 // Add three resources to the cache. Only c is usable as scratch. | 666 // Add three resources to the cache. Only c is usable as scratch. |
667 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu())); | 667 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu())); |
668 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu())); | 668 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu())); |
669 TestResource* c = TestResource::CreateScratch(context->getGpu(), | 669 TestResource* c = TestResource::CreateScratch(context->getGpu(), |
670 TestResource::kA_SimulatedProp
erty); | 670 TestResource::kA_SimulatedProp
erty); |
671 a->resourcePriv().setContentKey(key1); | 671 a->resourcePriv().setUniqueKey(key1); |
672 b->resourcePriv().setContentKey(key2); | 672 b->resourcePriv().setUniqueKey(key2); |
673 c->resourcePriv().setContentKey(key3); | 673 c->resourcePriv().setUniqueKey(key3); |
674 a->unref(); | 674 a->unref(); |
675 // hold b until *after* the message is sent. | 675 // hold b until *after* the message is sent. |
676 c->unref(); | 676 c->unref(); |
677 | 677 |
678 REPORTER_ASSERT(reporter, cache->hasContentKey(key1)); | 678 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1)); |
679 REPORTER_ASSERT(reporter, cache->hasContentKey(key2)); | 679 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2)); |
680 REPORTER_ASSERT(reporter, cache->hasContentKey(key3)); | 680 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3)); |
681 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive()); | 681 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive()); |
682 | 682 |
683 typedef GrContentKeyInvalidatedMessage Msg; | 683 typedef GrUniqueKeyInvalidatedMessage Msg; |
684 typedef SkMessageBus<GrContentKeyInvalidatedMessage> Bus; | 684 typedef SkMessageBus<GrUniqueKeyInvalidatedMessage> Bus; |
685 | 685 |
686 // Invalidate two of the three, they should be purged and no longer accessib
le via their keys. | 686 // Invalidate two of the three, they should be purged and no longer accessib
le via their keys. |
687 Bus::Post(Msg(key1)); | 687 Bus::Post(Msg(key1)); |
688 Bus::Post(Msg(key2)); | 688 Bus::Post(Msg(key2)); |
689 cache->purgeAsNeeded(); | 689 cache->purgeAsNeeded(); |
690 // a should be deleted now, but we still have a ref on b. | 690 // a should be deleted now, but we still have a ref on b. |
691 REPORTER_ASSERT(reporter, !cache->hasContentKey(key1)); | 691 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1)); |
692 REPORTER_ASSERT(reporter, !cache->hasContentKey(key2)); | 692 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2)); |
693 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); | 693 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
694 REPORTER_ASSERT(reporter, cache->hasContentKey(key3)); | 694 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3)); |
695 | 695 |
696 // Invalidate the third. | 696 // Invalidate the third. |
697 Bus::Post(Msg(key3)); | 697 Bus::Post(Msg(key3)); |
698 cache->purgeAsNeeded(); | 698 cache->purgeAsNeeded(); |
699 // we still have a ref on b, c should be recycled as scratch. | 699 // we still have a ref on b, c should be recycled as scratch. |
700 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); | 700 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
701 REPORTER_ASSERT(reporter, !cache->hasContentKey(key3)); | 701 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key3)); |
702 | 702 |
703 // make b purgeable. It should be immediately deleted since it has no key. | 703 // make b purgeable. It should be immediately deleted since it has no key. |
704 b->unref(); | 704 b->unref(); |
705 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); | 705 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
706 | 706 |
707 // Make sure we actually get to c via it's scratch key, before we say goodby
e. | 707 // Make sure we actually get to c via it's scratch key, before we say goodby
e. |
708 GrScratchKey scratchKey; | 708 GrScratchKey scratchKey; |
709 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratch
Key); | 709 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratch
Key); |
710 GrGpuResource* scratch = cache->findAndRefScratchResource(scratchKey); | 710 GrGpuResource* scratch = cache->findAndRefScratchResource(scratchKey); |
711 REPORTER_ASSERT(reporter, scratch == c); | 711 REPORTER_ASSERT(reporter, scratch == c); |
712 SkSafeUnref(scratch); | 712 SkSafeUnref(scratch); |
713 | 713 |
714 // Get rid of c. | 714 // Get rid of c. |
715 cache->purgeAllUnlocked(); | 715 cache->purgeAllUnlocked(); |
716 scratch = cache->findAndRefScratchResource(scratchKey); | 716 scratch = cache->findAndRefScratchResource(scratchKey); |
717 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); | 717 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
718 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); | 718 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
719 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes()); | 719 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes()); |
720 REPORTER_ASSERT(reporter, !scratch); | 720 REPORTER_ASSERT(reporter, !scratch); |
721 SkSafeUnref(scratch); | 721 SkSafeUnref(scratch); |
722 } | 722 } |
723 | 723 |
724 static void test_cache_chained_purge(skiatest::Reporter* reporter) { | 724 static void test_cache_chained_purge(skiatest::Reporter* reporter) { |
725 Mock mock(3, 30000); | 725 Mock mock(3, 30000); |
726 GrContext* context = mock.context(); | 726 GrContext* context = mock.context(); |
727 GrResourceCache* cache = mock.cache(); | 727 GrResourceCache* cache = mock.cache(); |
728 | 728 |
729 GrContentKey key1, key2; | 729 GrUniqueKey key1, key2; |
730 make_content_key<0>(&key1, 1); | 730 make_unique_key<0>(&key1, 1); |
731 make_content_key<0>(&key2, 2); | 731 make_unique_key<0>(&key2, 2); |
732 | 732 |
733 | 733 |
734 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu())); | 734 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu())); |
735 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu())); | 735 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu())); |
736 a->resourcePriv().setContentKey(key1); | 736 a->resourcePriv().setUniqueKey(key1); |
737 b->resourcePriv().setContentKey(key2); | 737 b->resourcePriv().setUniqueKey(key2); |
738 | 738 |
739 // Make a cycle | 739 // Make a cycle |
740 a->setUnrefWhenDestroyed(b); | 740 a->setUnrefWhenDestroyed(b); |
741 b->setUnrefWhenDestroyed(a); | 741 b->setUnrefWhenDestroyed(a); |
742 | 742 |
743 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); | 743 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
744 | 744 |
745 a->unref(); | 745 a->unref(); |
746 b->unref(); | 746 b->unref(); |
747 | 747 |
748 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); | 748 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
749 | 749 |
750 cache->purgeAllUnlocked(); | 750 cache->purgeAllUnlocked(); |
751 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); | 751 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
752 | 752 |
753 // Break the cycle | 753 // Break the cycle |
754 a->setUnrefWhenDestroyed(NULL); | 754 a->setUnrefWhenDestroyed(NULL); |
755 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); | 755 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
756 | 756 |
757 cache->purgeAllUnlocked(); | 757 cache->purgeAllUnlocked(); |
758 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); | 758 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
759 } | 759 } |
760 | 760 |
761 static void test_resource_size_changed(skiatest::Reporter* reporter) { | 761 static void test_resource_size_changed(skiatest::Reporter* reporter) { |
762 GrContentKey key1, key2; | 762 GrUniqueKey key1, key2; |
763 make_content_key<0>(&key1, 1); | 763 make_unique_key<0>(&key1, 1); |
764 make_content_key<0>(&key2, 2); | 764 make_unique_key<0>(&key2, 2); |
765 | 765 |
766 // Test changing resources sizes (both increase & decrease). | 766 // Test changing resources sizes (both increase & decrease). |
767 { | 767 { |
768 Mock mock(3, 30000); | 768 Mock mock(3, 30000); |
769 GrContext* context = mock.context(); | 769 GrContext* context = mock.context(); |
770 GrResourceCache* cache = mock.cache(); | 770 GrResourceCache* cache = mock.cache(); |
771 | 771 |
772 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu())); | 772 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu())); |
773 a->resourcePriv().setContentKey(key1); | 773 a->resourcePriv().setUniqueKey(key1); |
774 a->unref(); | 774 a->unref(); |
775 | 775 |
776 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu())); | 776 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu())); |
777 b->resourcePriv().setContentKey(key2); | 777 b->resourcePriv().setUniqueKey(key2); |
778 b->unref(); | 778 b->unref(); |
779 | 779 |
780 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes()); | 780 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes()); |
781 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); | 781 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
782 { | 782 { |
783 SkAutoTUnref<TestResource> find2(static_cast<TestResource*>(cache->f
indAndRefContentResource(key2))); | 783 SkAutoTUnref<TestResource> find2( |
| 784 static_cast<TestResource*>(cache->findAndRefUniqueResource(key2)
)); |
784 find2->setSize(200); | 785 find2->setSize(200); |
785 SkAutoTUnref<TestResource> find1(static_cast<TestResource*>(cache->f
indAndRefContentResource(key1))); | 786 SkAutoTUnref<TestResource> find1( |
| 787 static_cast<TestResource*>(cache->findAndRefUniqueResource(key1)
)); |
786 find1->setSize(50); | 788 find1->setSize(50); |
787 } | 789 } |
788 | 790 |
789 REPORTER_ASSERT(reporter, 250 == cache->getResourceBytes()); | 791 REPORTER_ASSERT(reporter, 250 == cache->getResourceBytes()); |
790 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); | 792 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
791 } | 793 } |
792 | 794 |
793 // Test increasing a resources size beyond the cache budget. | 795 // Test increasing a resources size beyond the cache budget. |
794 { | 796 { |
795 Mock mock(2, 300); | 797 Mock mock(2, 300); |
796 GrContext* context = mock.context(); | 798 GrContext* context = mock.context(); |
797 GrResourceCache* cache = mock.cache(); | 799 GrResourceCache* cache = mock.cache(); |
798 | 800 |
799 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu())); | 801 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu())); |
800 a->setSize(100); | 802 a->setSize(100); |
801 a->resourcePriv().setContentKey(key1); | 803 a->resourcePriv().setUniqueKey(key1); |
802 a->unref(); | 804 a->unref(); |
803 | 805 |
804 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu())); | 806 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu())); |
805 b->setSize(100); | 807 b->setSize(100); |
806 b->resourcePriv().setContentKey(key2); | 808 b->resourcePriv().setUniqueKey(key2); |
807 b->unref(); | 809 b->unref(); |
808 | 810 |
809 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes()); | 811 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes()); |
810 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); | 812 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
811 | 813 |
812 { | 814 { |
813 SkAutoTUnref<TestResource> find2(static_cast<TestResource*>(cache->f
indAndRefContentResource(key2))); | 815 SkAutoTUnref<TestResource> find2(static_cast<TestResource*>( |
| 816 cache->findAndRefUniqueResource(key2))); |
814 find2->setSize(201); | 817 find2->setSize(201); |
815 } | 818 } |
816 REPORTER_ASSERT(reporter, !cache->hasContentKey(key1)); | 819 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1)); |
817 | 820 |
818 REPORTER_ASSERT(reporter, 201 == cache->getResourceBytes()); | 821 REPORTER_ASSERT(reporter, 201 == cache->getResourceBytes()); |
819 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); | 822 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
820 } | 823 } |
821 } | 824 } |
822 | 825 |
823 static void test_large_resource_count(skiatest::Reporter* reporter) { | 826 static void test_large_resource_count(skiatest::Reporter* reporter) { |
824 // Set the cache size to double the resource count because we're going to cr
eate 2x that number | 827 // Set the cache size to double the resource count because we're going to cr
eate 2x that number |
825 // resources, using two different key domains. Add a little slop to the byte
s because we resize | 828 // resources, using two different key domains. Add a little slop to the byte
s because we resize |
826 // down to 1 byte after creating the resource. | 829 // down to 1 byte after creating the resource. |
827 static const int kResourceCnt = 2000; | 830 static const int kResourceCnt = 2000; |
828 | 831 |
829 Mock mock(2 * kResourceCnt, 2 * kResourceCnt + 1000); | 832 Mock mock(2 * kResourceCnt, 2 * kResourceCnt + 1000); |
830 GrContext* context = mock.context(); | 833 GrContext* context = mock.context(); |
831 GrResourceCache* cache = mock.cache(); | 834 GrResourceCache* cache = mock.cache(); |
832 | 835 |
833 for (int i = 0; i < kResourceCnt; ++i) { | 836 for (int i = 0; i < kResourceCnt; ++i) { |
834 GrContentKey key1, key2; | 837 GrUniqueKey key1, key2; |
835 make_content_key<1>(&key1, i); | 838 make_unique_key<1>(&key1, i); |
836 make_content_key<2>(&key2, i); | 839 make_unique_key<2>(&key2, i); |
837 | 840 |
838 TestResource* resource; | 841 TestResource* resource; |
839 | 842 |
840 resource = SkNEW_ARGS(TestResource, (context->getGpu())); | 843 resource = SkNEW_ARGS(TestResource, (context->getGpu())); |
841 resource->resourcePriv().setContentKey(key1); | 844 resource->resourcePriv().setUniqueKey(key1); |
842 resource->setSize(1); | 845 resource->setSize(1); |
843 resource->unref(); | 846 resource->unref(); |
844 | 847 |
845 resource = SkNEW_ARGS(TestResource, (context->getGpu())); | 848 resource = SkNEW_ARGS(TestResource, (context->getGpu())); |
846 resource->resourcePriv().setContentKey(key2); | 849 resource->resourcePriv().setUniqueKey(key2); |
847 resource->setSize(1); | 850 resource->setSize(1); |
848 resource->unref(); | 851 resource->unref(); |
849 } | 852 } |
850 | 853 |
851 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 2 * kResourceCnt); | 854 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 2 * kResourceCnt); |
852 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 2 * kResource
Cnt); | 855 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 2 * kResource
Cnt); |
853 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 2 * kResource
Cnt); | 856 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 2 * kResource
Cnt); |
854 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 2 * kResourceCnt); | 857 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 2 * kResourceCnt); |
855 REPORTER_ASSERT(reporter, cache->getResourceCount() == 2 * kResourceCnt); | 858 REPORTER_ASSERT(reporter, cache->getResourceCount() == 2 * kResourceCnt); |
856 for (int i = 0; i < kResourceCnt; ++i) { | 859 for (int i = 0; i < kResourceCnt; ++i) { |
857 GrContentKey key1, key2; | 860 GrUniqueKey key1, key2; |
858 make_content_key<1>(&key1, i); | 861 make_unique_key<1>(&key1, i); |
859 make_content_key<2>(&key2, i); | 862 make_unique_key<2>(&key2, i); |
860 | 863 |
861 REPORTER_ASSERT(reporter, cache->hasContentKey(key1)); | 864 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1)); |
862 REPORTER_ASSERT(reporter, cache->hasContentKey(key2)); | 865 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2)); |
863 } | 866 } |
864 | 867 |
865 cache->purgeAllUnlocked(); | 868 cache->purgeAllUnlocked(); |
866 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 0); | 869 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 0); |
867 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 0); | 870 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 0); |
868 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 0); | 871 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 0); |
869 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 0); | 872 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 0); |
870 REPORTER_ASSERT(reporter, cache->getResourceCount() == 0); | 873 REPORTER_ASSERT(reporter, cache->getResourceCount() == 0); |
871 | 874 |
872 for (int i = 0; i < kResourceCnt; ++i) { | 875 for (int i = 0; i < kResourceCnt; ++i) { |
873 GrContentKey key1, key2; | 876 GrUniqueKey key1, key2; |
874 make_content_key<1>(&key1, i); | 877 make_unique_key<1>(&key1, i); |
875 make_content_key<2>(&key2, i); | 878 make_unique_key<2>(&key2, i); |
876 | 879 |
877 REPORTER_ASSERT(reporter, !cache->hasContentKey(key1)); | 880 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1)); |
878 REPORTER_ASSERT(reporter, !cache->hasContentKey(key2)); | 881 REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2)); |
879 } | 882 } |
880 } | 883 } |
881 | 884 |
882 | 885 |
883 //////////////////////////////////////////////////////////////////////////////// | 886 //////////////////////////////////////////////////////////////////////////////// |
884 DEF_GPUTEST(ResourceCache, reporter, factory) { | 887 DEF_GPUTEST(ResourceCache, reporter, factory) { |
885 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) { | 888 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) { |
886 GrContextFactory::GLContextType glType = static_cast<GrContextFactory::G
LContextType>(type); | 889 GrContextFactory::GLContextType glType = static_cast<GrContextFactory::G
LContextType>(type); |
887 if (!GrContextFactory::IsRenderingGLContext(glType)) { | 890 if (!GrContextFactory::IsRenderingGLContext(glType)) { |
888 continue; | 891 continue; |
(...skipping 11 matching lines...) Expand all Loading... |
900 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(context, | 903 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(context, |
901 SkSurface::kN
o_Budgeted, info)); | 904 SkSurface::kN
o_Budgeted, info)); |
902 test_cache(reporter, context, surface->getCanvas()); | 905 test_cache(reporter, context, surface->getCanvas()); |
903 } | 906 } |
904 | 907 |
905 // The below tests create their own mock contexts. | 908 // The below tests create their own mock contexts. |
906 test_no_key(reporter); | 909 test_no_key(reporter); |
907 test_budgeting(reporter); | 910 test_budgeting(reporter); |
908 test_unbudgeted(reporter); | 911 test_unbudgeted(reporter); |
909 test_unbudgeted_to_scratch(reporter); | 912 test_unbudgeted_to_scratch(reporter); |
910 test_duplicate_content_key(reporter); | 913 test_duplicate_unique_key(reporter); |
911 test_duplicate_scratch_key(reporter); | 914 test_duplicate_scratch_key(reporter); |
912 test_remove_scratch_key(reporter); | 915 test_remove_scratch_key(reporter); |
913 test_scratch_key_consistency(reporter); | 916 test_scratch_key_consistency(reporter); |
914 test_purge_invalidated(reporter); | 917 test_purge_invalidated(reporter); |
915 test_cache_chained_purge(reporter); | 918 test_cache_chained_purge(reporter); |
916 test_resource_size_changed(reporter); | 919 test_resource_size_changed(reporter); |
917 test_large_resource_count(reporter); | 920 test_large_resource_count(reporter); |
918 } | 921 } |
919 | 922 |
920 #endif | 923 #endif |
OLD | NEW |