| 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 // Include here to ensure SK_SUPPORT_GPU is set correctly before it is examined. | 8 // Include here to ensure SK_SUPPORT_GPU is set correctly before it is examined. |
| 9 #include "SkTypes.h" | 9 #include "SkTypes.h" |
| 10 | 10 |
| (...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 254 TestResource(GrGpu* gpu) | 254 TestResource(GrGpu* gpu) |
| 255 : INHERITED(gpu, kCached_LifeCycle) | 255 : INHERITED(gpu, kCached_LifeCycle) |
| 256 , fToDelete(NULL) | 256 , fToDelete(NULL) |
| 257 , fSize(kDefaultSize) | 257 , fSize(kDefaultSize) |
| 258 , fProperty(kA_SimulatedProperty) { | 258 , fProperty(kA_SimulatedProperty) { |
| 259 ++fNumAlive; | 259 ++fNumAlive; |
| 260 this->registerWithCache(); | 260 this->registerWithCache(); |
| 261 } | 261 } |
| 262 | 262 |
| 263 static TestResource* CreateScratch(GrGpu* gpu, SimulatedProperty property, b
ool cached = true) { | 263 static TestResource* CreateScratch(GrGpu* gpu, SimulatedProperty property, b
ool cached = true) { |
| 264 return SkNEW_ARGS(TestResource, (gpu, property, cached, kScratchConstruc
tor)); | 264 return new TestResource(gpu, property, cached, kScratchConstructor); |
| 265 } | 265 } |
| 266 | 266 |
| 267 ~TestResource() { | 267 ~TestResource() { |
| 268 --fNumAlive; | 268 --fNumAlive; |
| 269 SkSafeUnref(fToDelete); | 269 SkSafeUnref(fToDelete); |
| 270 } | 270 } |
| 271 | 271 |
| 272 void setSize(size_t size) { | 272 void setSize(size_t size) { |
| 273 fSize = size; | 273 fSize = size; |
| 274 this->didChangeGpuMemorySize(); | 274 this->didChangeGpuMemorySize(); |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 335 private: | 335 private: |
| 336 SkAutoTUnref<GrContext> fContext; | 336 SkAutoTUnref<GrContext> fContext; |
| 337 }; | 337 }; |
| 338 | 338 |
| 339 static void test_no_key(skiatest::Reporter* reporter) { | 339 static void test_no_key(skiatest::Reporter* reporter) { |
| 340 Mock mock(10, 30000); | 340 Mock mock(10, 30000); |
| 341 GrContext* context = mock.context(); | 341 GrContext* context = mock.context(); |
| 342 GrResourceCache* cache = mock.cache(); | 342 GrResourceCache* cache = mock.cache(); |
| 343 | 343 |
| 344 // Create a bunch of resources with no keys | 344 // Create a bunch of resources with no keys |
| 345 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu())); | 345 TestResource* a = new TestResource(context->getGpu()); |
| 346 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu())); | 346 TestResource* b = new TestResource(context->getGpu()); |
| 347 TestResource* c = SkNEW_ARGS(TestResource, (context->getGpu())); | 347 TestResource* c = new TestResource(context->getGpu()); |
| 348 TestResource* d = SkNEW_ARGS(TestResource, (context->getGpu())); | 348 TestResource* d = new TestResource(context->getGpu()); |
| 349 a->setSize(11); | 349 a->setSize(11); |
| 350 b->setSize(12); | 350 b->setSize(12); |
| 351 c->setSize(13); | 351 c->setSize(13); |
| 352 d->setSize(14); | 352 d->setSize(14); |
| 353 | 353 |
| 354 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive()); | 354 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive()); |
| 355 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount()); | 355 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount()); |
| 356 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMe
morySize() + | 356 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMe
morySize() + |
| 357 d->gpuMemorySize() == cache->getResourceBytes()); | 357 d->gpuMemorySize() == cache->getResourceBytes()); |
| 358 | 358 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 397 GrContext* context = mock.context(); | 397 GrContext* context = mock.context(); |
| 398 GrResourceCache* cache = mock.cache(); | 398 GrResourceCache* cache = mock.cache(); |
| 399 | 399 |
| 400 GrUniqueKey uniqueKey; | 400 GrUniqueKey uniqueKey; |
| 401 make_unique_key<0>(&uniqueKey, 0); | 401 make_unique_key<0>(&uniqueKey, 0); |
| 402 | 402 |
| 403 // Create a scratch, a unique, and a wrapped resource | 403 // Create a scratch, a unique, and a wrapped resource |
| 404 TestResource* scratch = | 404 TestResource* scratch = |
| 405 TestResource::CreateScratch(context->getGpu(), TestResource::kB_Simu
latedProperty); | 405 TestResource::CreateScratch(context->getGpu(), TestResource::kB_Simu
latedProperty); |
| 406 scratch->setSize(10); | 406 scratch->setSize(10); |
| 407 TestResource* unique = SkNEW_ARGS(TestResource, (context->getGpu())); | 407 TestResource* unique = new TestResource(context->getGpu()); |
| 408 unique->setSize(11); | 408 unique->setSize(11); |
| 409 unique->resourcePriv().setUniqueKey(uniqueKey); | 409 unique->resourcePriv().setUniqueKey(uniqueKey); |
| 410 TestResource* wrapped = SkNEW_ARGS(TestResource, | 410 TestResource* wrapped = new TestResource(context->getGpu(), GrGpuResource::k
Borrowed_LifeCycle); |
| 411 (context->getGpu(), GrGpuResource::kBorro
wed_LifeCycle)); | |
| 412 wrapped->setSize(12); | 411 wrapped->setSize(12); |
| 413 TestResource* unbudgeted = SkNEW_ARGS(TestResource, | 412 TestResource* unbudgeted = |
| 414 (context->getGpu(), GrGpuResource::kUn
cached_LifeCycle)); | 413 new TestResource(context->getGpu(), GrGpuResource::kUncached_LifeCyc
le); |
| 415 unbudgeted->setSize(13); | 414 unbudgeted->setSize(13); |
| 416 | 415 |
| 417 // Make sure we can't add a unique key to the wrapped resource | 416 // Make sure we can't add a unique key to the wrapped resource |
| 418 GrUniqueKey uniqueKey2; | 417 GrUniqueKey uniqueKey2; |
| 419 make_unique_key<0>(&uniqueKey2, 1); | 418 make_unique_key<0>(&uniqueKey2, 1); |
| 420 wrapped->resourcePriv().setUniqueKey(uniqueKey2); | 419 wrapped->resourcePriv().setUniqueKey(uniqueKey2); |
| 421 REPORTER_ASSERT(reporter, NULL == cache->findAndRefUniqueResource(uniqueKey2
)); | 420 REPORTER_ASSERT(reporter, NULL == cache->findAndRefUniqueResource(uniqueKey2
)); |
| 422 | 421 |
| 423 // Make sure sizes are as we expect | 422 // Make sure sizes are as we expect |
| 424 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount()); | 423 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount()); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 439 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize()
== | 438 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize()
== |
| 440 cache->getBudgetedResourceBytes()); | 439 cache->getBudgetedResourceBytes()); |
| 441 | 440 |
| 442 // Unreffing the wrapped resource should free it right away. | 441 // Unreffing the wrapped resource should free it right away. |
| 443 wrapped->unref(); | 442 wrapped->unref(); |
| 444 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount()); | 443 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount()); |
| 445 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize()
+ | 444 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize()
+ |
| 446 unbudgeted->gpuMemorySize() == cache->getResourceB
ytes()); | 445 unbudgeted->gpuMemorySize() == cache->getResourceB
ytes()); |
| 447 | 446 |
| 448 // Now try freeing the budgeted resources first | 447 // Now try freeing the budgeted resources first |
| 449 wrapped = SkNEW_ARGS(TestResource, (context->getGpu(), GrGpuResource::kBorro
wed_LifeCycle)); | 448 wrapped = new TestResource(context->getGpu(), GrGpuResource::kBorrowed_LifeC
ycle); |
| 450 scratch->setSize(12); | 449 scratch->setSize(12); |
| 451 unique->unref(); | 450 unique->unref(); |
| 452 cache->purgeAllUnlocked(); | 451 cache->purgeAllUnlocked(); |
| 453 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount()); | 452 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount()); |
| 454 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + wrapped->gpuMemorySize(
) + | 453 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + wrapped->gpuMemorySize(
) + |
| 455 unbudgeted->gpuMemorySize() == cache->getResourceB
ytes()); | 454 unbudgeted->gpuMemorySize() == cache->getResourceB
ytes()); |
| 456 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount()); | 455 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount()); |
| 457 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() == cache->getBudgetedReso
urceBytes()); | 456 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() == cache->getBudgetedReso
urceBytes()); |
| 458 | 457 |
| 459 scratch->unref(); | 458 scratch->unref(); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 492 | 491 |
| 493 // A large uncached or wrapped resource shouldn't evict anything. | 492 // A large uncached or wrapped resource shouldn't evict anything. |
| 494 scratch = TestResource::CreateScratch(context->getGpu(), TestResource::kB_Si
mulatedProperty); | 493 scratch = TestResource::CreateScratch(context->getGpu(), TestResource::kB_Si
mulatedProperty); |
| 495 scratch->setSize(10); | 494 scratch->setSize(10); |
| 496 scratch->unref(); | 495 scratch->unref(); |
| 497 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); | 496 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
| 498 REPORTER_ASSERT(reporter, 10 == cache->getResourceBytes()); | 497 REPORTER_ASSERT(reporter, 10 == cache->getResourceBytes()); |
| 499 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount()); | 498 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount()); |
| 500 REPORTER_ASSERT(reporter, 10 == cache->getBudgetedResourceBytes()); | 499 REPORTER_ASSERT(reporter, 10 == cache->getBudgetedResourceBytes()); |
| 501 | 500 |
| 502 unique = SkNEW_ARGS(TestResource, (context->getGpu())); | 501 unique = new TestResource(context->getGpu()); |
| 503 unique->setSize(11); | 502 unique->setSize(11); |
| 504 unique->resourcePriv().setUniqueKey(uniqueKey); | 503 unique->resourcePriv().setUniqueKey(uniqueKey); |
| 505 unique->unref(); | 504 unique->unref(); |
| 506 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); | 505 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
| 507 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes()); | 506 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes()); |
| 508 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount()); | 507 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount()); |
| 509 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes()); | 508 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes()); |
| 510 | 509 |
| 511 size_t large = 2 * cache->getResourceBytes(); | 510 size_t large = 2 * cache->getResourceBytes(); |
| 512 unbudgeted = SkNEW_ARGS(TestResource, | 511 unbudgeted = new TestResource(context->getGpu(), large, GrGpuResource::kUnca
ched_LifeCycle); |
| 513 (context->getGpu(), large, GrGpuResource::kUncached_
LifeCycle)); | |
| 514 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount()); | 512 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount()); |
| 515 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes()); | 513 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes()); |
| 516 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount()); | 514 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount()); |
| 517 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes()); | 515 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes()); |
| 518 | 516 |
| 519 unbudgeted->unref(); | 517 unbudgeted->unref(); |
| 520 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); | 518 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
| 521 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes()); | 519 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes()); |
| 522 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount()); | 520 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount()); |
| 523 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes()); | 521 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes()); |
| 524 | 522 |
| 525 wrapped = SkNEW_ARGS(TestResource, | 523 wrapped = new TestResource(context->getGpu(), large, GrGpuResource::kBorrowe
d_LifeCycle); |
| 526 (context->getGpu(), large, GrGpuResource::kBorrowed_Lif
eCycle)); | |
| 527 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount()); | 524 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount()); |
| 528 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes()); | 525 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes()); |
| 529 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount()); | 526 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount()); |
| 530 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes()); | 527 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes()); |
| 531 | 528 |
| 532 wrapped->unref(); | 529 wrapped->unref(); |
| 533 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); | 530 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
| 534 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes()); | 531 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes()); |
| 535 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount()); | 532 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount()); |
| 536 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes()); | 533 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes()); |
| (...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 768 | 765 |
| 769 static void test_duplicate_unique_key(skiatest::Reporter* reporter) { | 766 static void test_duplicate_unique_key(skiatest::Reporter* reporter) { |
| 770 Mock mock(5, 30000); | 767 Mock mock(5, 30000); |
| 771 GrContext* context = mock.context(); | 768 GrContext* context = mock.context(); |
| 772 GrResourceCache* cache = mock.cache(); | 769 GrResourceCache* cache = mock.cache(); |
| 773 | 770 |
| 774 GrUniqueKey key; | 771 GrUniqueKey key; |
| 775 make_unique_key<0>(&key, 0); | 772 make_unique_key<0>(&key, 0); |
| 776 | 773 |
| 777 // Create two resources that we will attempt to register with the same uniqu
e key. | 774 // Create two resources that we will attempt to register with the same uniqu
e key. |
| 778 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu())); | 775 TestResource* a = new TestResource(context->getGpu()); |
| 779 a->setSize(11); | 776 a->setSize(11); |
| 780 | 777 |
| 781 // Set key on resource a. | 778 // Set key on resource a. |
| 782 a->resourcePriv().setUniqueKey(key); | 779 a->resourcePriv().setUniqueKey(key); |
| 783 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key)); | 780 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key)); |
| 784 a->unref(); | 781 a->unref(); |
| 785 | 782 |
| 786 // Make sure that redundantly setting a's key works. | 783 // Make sure that redundantly setting a's key works. |
| 787 a->resourcePriv().setUniqueKey(key); | 784 a->resourcePriv().setUniqueKey(key); |
| 788 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key)); | 785 REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key)); |
| 789 a->unref(); | 786 a->unref(); |
| 790 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); | 787 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
| 791 REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getResourceBytes()); | 788 REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getResourceBytes()); |
| 792 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); | 789 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
| 793 | 790 |
| 794 // Create resource b and set the same key. It should replace a's unique key
cache entry. | 791 // Create resource b and set the same key. It should replace a's unique key
cache entry. |
| 795 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu())); | 792 TestResource* b = new TestResource(context->getGpu()); |
| 796 b->setSize(12); | 793 b->setSize(12); |
| 797 b->resourcePriv().setUniqueKey(key); | 794 b->resourcePriv().setUniqueKey(key); |
| 798 REPORTER_ASSERT(reporter, b == cache->findAndRefUniqueResource(key)); | 795 REPORTER_ASSERT(reporter, b == cache->findAndRefUniqueResource(key)); |
| 799 b->unref(); | 796 b->unref(); |
| 800 | 797 |
| 801 // Still have two resources because a is still reffed. | 798 // Still have two resources because a is still reffed. |
| 802 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); | 799 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
| 803 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == cache->
getResourceBytes()); | 800 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == cache->
getResourceBytes()); |
| 804 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); | 801 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
| 805 | 802 |
| 806 a->unref(); | 803 a->unref(); |
| 807 // Now a should be gone. | 804 // Now a should be gone. |
| 808 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); | 805 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
| 809 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes()); | 806 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes()); |
| 810 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); | 807 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
| 811 | 808 |
| 812 // Now replace b with c, but make sure c can start with one unique key and c
hange it to b's key. | 809 // Now replace b with c, but make sure c can start with one unique key and c
hange it to b's key. |
| 813 // Also make b be unreffed when replacement occurs. | 810 // Also make b be unreffed when replacement occurs. |
| 814 b->unref(); | 811 b->unref(); |
| 815 TestResource* c = SkNEW_ARGS(TestResource, (context->getGpu())); | 812 TestResource* c = new TestResource(context->getGpu()); |
| 816 GrUniqueKey differentKey; | 813 GrUniqueKey differentKey; |
| 817 make_unique_key<0>(&differentKey, 1); | 814 make_unique_key<0>(&differentKey, 1); |
| 818 c->setSize(13); | 815 c->setSize(13); |
| 819 c->resourcePriv().setUniqueKey(differentKey); | 816 c->resourcePriv().setUniqueKey(differentKey); |
| 820 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); | 817 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
| 821 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() == cache->
getResourceBytes()); | 818 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() == cache->
getResourceBytes()); |
| 822 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); | 819 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
| 823 // c replaces b and b should be immediately purged. | 820 // c replaces b and b should be immediately purged. |
| 824 c->resourcePriv().setUniqueKey(key); | 821 c->resourcePriv().setUniqueKey(key); |
| 825 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); | 822 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 842 REPORTER_ASSERT(reporter, c == cache->findAndRefUniqueResource(key)); | 839 REPORTER_ASSERT(reporter, c == cache->findAndRefUniqueResource(key)); |
| 843 c->resourcePriv().removeUniqueKey(); | 840 c->resourcePriv().removeUniqueKey(); |
| 844 c->unref(); | 841 c->unref(); |
| 845 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); | 842 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
| 846 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes()); | 843 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes()); |
| 847 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); | 844 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
| 848 | 845 |
| 849 { | 846 { |
| 850 GrUniqueKey key2; | 847 GrUniqueKey key2; |
| 851 make_unique_key<0>(&key2, 0); | 848 make_unique_key<0>(&key2, 0); |
| 852 SkAutoTUnref<TestResource> d(SkNEW_ARGS(TestResource, (context->getGpu()
))); | 849 SkAutoTUnref<TestResource> d(new TestResource(context->getGpu())); |
| 853 int foo = 4132; | 850 int foo = 4132; |
| 854 SkAutoTUnref<SkData> data(SkData::NewWithCopy(&foo, sizeof(foo))); | 851 SkAutoTUnref<SkData> data(SkData::NewWithCopy(&foo, sizeof(foo))); |
| 855 key2.setCustomData(data.get()); | 852 key2.setCustomData(data.get()); |
| 856 d->resourcePriv().setUniqueKey(key2); | 853 d->resourcePriv().setUniqueKey(key2); |
| 857 } | 854 } |
| 858 | 855 |
| 859 GrUniqueKey key3; | 856 GrUniqueKey key3; |
| 860 make_unique_key<0>(&key3, 0); | 857 make_unique_key<0>(&key3, 0); |
| 861 SkAutoTUnref<GrGpuResource> d2(cache->findAndRefUniqueResource(key3)); | 858 SkAutoTUnref<GrGpuResource> d2(cache->findAndRefUniqueResource(key3)); |
| 862 REPORTER_ASSERT(reporter, *(int*) d2->getUniqueKey().getCustomData()->data()
== 4132); | 859 REPORTER_ASSERT(reporter, *(int*) d2->getUniqueKey().getCustomData()->data()
== 4132); |
| 863 } | 860 } |
| 864 | 861 |
| 865 static void test_purge_invalidated(skiatest::Reporter* reporter) { | 862 static void test_purge_invalidated(skiatest::Reporter* reporter) { |
| 866 Mock mock(5, 30000); | 863 Mock mock(5, 30000); |
| 867 GrContext* context = mock.context(); | 864 GrContext* context = mock.context(); |
| 868 GrResourceCache* cache = mock.cache(); | 865 GrResourceCache* cache = mock.cache(); |
| 869 | 866 |
| 870 GrUniqueKey key1, key2, key3; | 867 GrUniqueKey key1, key2, key3; |
| 871 make_unique_key<0>(&key1, 1); | 868 make_unique_key<0>(&key1, 1); |
| 872 make_unique_key<0>(&key2, 2); | 869 make_unique_key<0>(&key2, 2); |
| 873 make_unique_key<0>(&key3, 3); | 870 make_unique_key<0>(&key3, 3); |
| 874 | 871 |
| 875 // Add three resources to the cache. Only c is usable as scratch. | 872 // Add three resources to the cache. Only c is usable as scratch. |
| 876 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu())); | 873 TestResource* a = new TestResource(context->getGpu()); |
| 877 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu())); | 874 TestResource* b = new TestResource(context->getGpu()); |
| 878 TestResource* c = TestResource::CreateScratch(context->getGpu(), | 875 TestResource* c = TestResource::CreateScratch(context->getGpu(), |
| 879 TestResource::kA_SimulatedProp
erty); | 876 TestResource::kA_SimulatedProp
erty); |
| 880 a->resourcePriv().setUniqueKey(key1); | 877 a->resourcePriv().setUniqueKey(key1); |
| 881 b->resourcePriv().setUniqueKey(key2); | 878 b->resourcePriv().setUniqueKey(key2); |
| 882 c->resourcePriv().setUniqueKey(key3); | 879 c->resourcePriv().setUniqueKey(key3); |
| 883 a->unref(); | 880 a->unref(); |
| 884 // hold b until *after* the message is sent. | 881 // hold b until *after* the message is sent. |
| 885 c->unref(); | 882 c->unref(); |
| 886 | 883 |
| 887 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1)); | 884 REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1)); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 932 | 929 |
| 933 static void test_cache_chained_purge(skiatest::Reporter* reporter) { | 930 static void test_cache_chained_purge(skiatest::Reporter* reporter) { |
| 934 Mock mock(3, 30000); | 931 Mock mock(3, 30000); |
| 935 GrContext* context = mock.context(); | 932 GrContext* context = mock.context(); |
| 936 GrResourceCache* cache = mock.cache(); | 933 GrResourceCache* cache = mock.cache(); |
| 937 | 934 |
| 938 GrUniqueKey key1, key2; | 935 GrUniqueKey key1, key2; |
| 939 make_unique_key<0>(&key1, 1); | 936 make_unique_key<0>(&key1, 1); |
| 940 make_unique_key<0>(&key2, 2); | 937 make_unique_key<0>(&key2, 2); |
| 941 | 938 |
| 942 | 939 TestResource* a = new TestResource(context->getGpu()); |
| 943 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu())); | 940 TestResource* b = new TestResource(context->getGpu()); |
| 944 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu())); | |
| 945 a->resourcePriv().setUniqueKey(key1); | 941 a->resourcePriv().setUniqueKey(key1); |
| 946 b->resourcePriv().setUniqueKey(key2); | 942 b->resourcePriv().setUniqueKey(key2); |
| 947 | 943 |
| 948 // Make a cycle | 944 // Make a cycle |
| 949 a->setUnrefWhenDestroyed(b); | 945 a->setUnrefWhenDestroyed(b); |
| 950 b->setUnrefWhenDestroyed(a); | 946 b->setUnrefWhenDestroyed(a); |
| 951 | 947 |
| 952 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); | 948 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
| 953 | 949 |
| 954 a->unref(); | 950 a->unref(); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 971 GrUniqueKey key1, key2; | 967 GrUniqueKey key1, key2; |
| 972 make_unique_key<0>(&key1, 1); | 968 make_unique_key<0>(&key1, 1); |
| 973 make_unique_key<0>(&key2, 2); | 969 make_unique_key<0>(&key2, 2); |
| 974 | 970 |
| 975 // Test changing resources sizes (both increase & decrease). | 971 // Test changing resources sizes (both increase & decrease). |
| 976 { | 972 { |
| 977 Mock mock(3, 30000); | 973 Mock mock(3, 30000); |
| 978 GrContext* context = mock.context(); | 974 GrContext* context = mock.context(); |
| 979 GrResourceCache* cache = mock.cache(); | 975 GrResourceCache* cache = mock.cache(); |
| 980 | 976 |
| 981 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu())); | 977 TestResource* a = new TestResource(context->getGpu()); |
| 982 a->resourcePriv().setUniqueKey(key1); | 978 a->resourcePriv().setUniqueKey(key1); |
| 983 a->unref(); | 979 a->unref(); |
| 984 | 980 |
| 985 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu())); | 981 TestResource* b = new TestResource(context->getGpu()); |
| 986 b->resourcePriv().setUniqueKey(key2); | 982 b->resourcePriv().setUniqueKey(key2); |
| 987 b->unref(); | 983 b->unref(); |
| 988 | 984 |
| 989 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes()); | 985 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes()); |
| 990 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); | 986 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
| 991 { | 987 { |
| 992 SkAutoTUnref<TestResource> find2( | 988 SkAutoTUnref<TestResource> find2( |
| 993 static_cast<TestResource*>(cache->findAndRefUniqueResource(key2)
)); | 989 static_cast<TestResource*>(cache->findAndRefUniqueResource(key2)
)); |
| 994 find2->setSize(200); | 990 find2->setSize(200); |
| 995 SkAutoTUnref<TestResource> find1( | 991 SkAutoTUnref<TestResource> find1( |
| 996 static_cast<TestResource*>(cache->findAndRefUniqueResource(key1)
)); | 992 static_cast<TestResource*>(cache->findAndRefUniqueResource(key1)
)); |
| 997 find1->setSize(50); | 993 find1->setSize(50); |
| 998 } | 994 } |
| 999 | 995 |
| 1000 REPORTER_ASSERT(reporter, 250 == cache->getResourceBytes()); | 996 REPORTER_ASSERT(reporter, 250 == cache->getResourceBytes()); |
| 1001 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); | 997 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
| 1002 } | 998 } |
| 1003 | 999 |
| 1004 // Test increasing a resources size beyond the cache budget. | 1000 // Test increasing a resources size beyond the cache budget. |
| 1005 { | 1001 { |
| 1006 Mock mock(2, 300); | 1002 Mock mock(2, 300); |
| 1007 GrContext* context = mock.context(); | 1003 GrContext* context = mock.context(); |
| 1008 GrResourceCache* cache = mock.cache(); | 1004 GrResourceCache* cache = mock.cache(); |
| 1009 | 1005 |
| 1010 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu())); | 1006 TestResource* a = new TestResource(context->getGpu()); |
| 1011 a->setSize(100); | 1007 a->setSize(100); |
| 1012 a->resourcePriv().setUniqueKey(key1); | 1008 a->resourcePriv().setUniqueKey(key1); |
| 1013 a->unref(); | 1009 a->unref(); |
| 1014 | 1010 |
| 1015 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu())); | 1011 TestResource* b = new TestResource(context->getGpu()); |
| 1016 b->setSize(100); | 1012 b->setSize(100); |
| 1017 b->resourcePriv().setUniqueKey(key2); | 1013 b->resourcePriv().setUniqueKey(key2); |
| 1018 b->unref(); | 1014 b->unref(); |
| 1019 | 1015 |
| 1020 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes()); | 1016 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes()); |
| 1021 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); | 1017 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
| 1022 | 1018 |
| 1023 { | 1019 { |
| 1024 SkAutoTUnref<TestResource> find2(static_cast<TestResource*>( | 1020 SkAutoTUnref<TestResource> find2(static_cast<TestResource*>( |
| 1025 cache->findAndRefUniqueResource(key2))); | 1021 cache->findAndRefUniqueResource(key2))); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 1054 SkTDArray<int> shouldPurgeIdxs; | 1050 SkTDArray<int> shouldPurgeIdxs; |
| 1055 int purgeableCnt = 0; | 1051 int purgeableCnt = 0; |
| 1056 SkTDArray<GrGpuResource*> resourcesToUnref; | 1052 SkTDArray<GrGpuResource*> resourcesToUnref; |
| 1057 | 1053 |
| 1058 // Add kCount resources, holding onto resources at random so we have a m
ix of purgeable and | 1054 // Add kCount resources, holding onto resources at random so we have a m
ix of purgeable and |
| 1059 // unpurgeable resources. | 1055 // unpurgeable resources. |
| 1060 for (int j = 0; j < kCount; ++j) { | 1056 for (int j = 0; j < kCount; ++j) { |
| 1061 GrUniqueKey key; | 1057 GrUniqueKey key; |
| 1062 make_unique_key<0>(&key, j); | 1058 make_unique_key<0>(&key, j); |
| 1063 | 1059 |
| 1064 TestResource* r = SkNEW_ARGS(TestResource, (context->getGpu())); | 1060 TestResource* r = new TestResource(context->getGpu()); |
| 1065 r->resourcePriv().setUniqueKey(key); | 1061 r->resourcePriv().setUniqueKey(key); |
| 1066 if (random.nextU() % kLockedFreq) { | 1062 if (random.nextU() % kLockedFreq) { |
| 1067 // Make this is purgeable. | 1063 // Make this is purgeable. |
| 1068 r->unref(); | 1064 r->unref(); |
| 1069 ++purgeableCnt; | 1065 ++purgeableCnt; |
| 1070 if (purgeableCnt <= kNumToPurge) { | 1066 if (purgeableCnt <= kNumToPurge) { |
| 1071 *shouldPurgeIdxs.append() = j; | 1067 *shouldPurgeIdxs.append() = j; |
| 1072 } | 1068 } |
| 1073 } else { | 1069 } else { |
| 1074 *resourcesToUnref.append() = r; | 1070 *resourcesToUnref.append() = r; |
| (...skipping 28 matching lines...) Expand all Loading... |
| 1103 GrResourceCache* cache = mock.cache(); | 1099 GrResourceCache* cache = mock.cache(); |
| 1104 | 1100 |
| 1105 // The current cache impl will round the max flush count to the next power o
f 2. So we choose a | 1101 // The current cache impl will round the max flush count to the next power o
f 2. So we choose a |
| 1106 // power of two here to keep things simpler. | 1102 // power of two here to keep things simpler. |
| 1107 static const int kFlushCount = 16; | 1103 static const int kFlushCount = 16; |
| 1108 cache->setLimits(1000000, 1000000, kFlushCount); | 1104 cache->setLimits(1000000, 1000000, kFlushCount); |
| 1109 | 1105 |
| 1110 { | 1106 { |
| 1111 // Insert a resource and send a flush notification kFlushCount times. | 1107 // Insert a resource and send a flush notification kFlushCount times. |
| 1112 for (int i = 0; i < kFlushCount; ++i) { | 1108 for (int i = 0; i < kFlushCount; ++i) { |
| 1113 TestResource* r = SkNEW_ARGS(TestResource, (context->getGpu())); | 1109 TestResource* r = new TestResource(context->getGpu()); |
| 1114 GrUniqueKey k; | 1110 GrUniqueKey k; |
| 1115 make_unique_key<1>(&k, i); | 1111 make_unique_key<1>(&k, i); |
| 1116 r->resourcePriv().setUniqueKey(k); | 1112 r->resourcePriv().setUniqueKey(k); |
| 1117 r->unref(); | 1113 r->unref(); |
| 1118 cache->notifyFlushOccurred(); | 1114 cache->notifyFlushOccurred(); |
| 1119 } | 1115 } |
| 1120 | 1116 |
| 1121 // Send flush notifications to the cache. Each flush should purge the ol
dest resource. | 1117 // Send flush notifications to the cache. Each flush should purge the ol
dest resource. |
| 1122 for (int i = 0; i < kFlushCount - 1; ++i) { | 1118 for (int i = 0; i < kFlushCount - 1; ++i) { |
| 1123 // The first resource was purged after the last flush in the initial
loop, hence the -1. | 1119 // The first resource was purged after the last flush in the initial
loop, hence the -1. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 1134 | 1130 |
| 1135 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); | 1131 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
| 1136 cache->purgeAllUnlocked(); | 1132 cache->purgeAllUnlocked(); |
| 1137 } | 1133 } |
| 1138 | 1134 |
| 1139 // Do a similar test but where we leave refs on some resources to prevent th
em from being | 1135 // Do a similar test but where we leave refs on some resources to prevent th
em from being |
| 1140 // purged. | 1136 // purged. |
| 1141 { | 1137 { |
| 1142 GrGpuResource* refedResources[kFlushCount >> 1]; | 1138 GrGpuResource* refedResources[kFlushCount >> 1]; |
| 1143 for (int i = 0; i < kFlushCount; ++i) { | 1139 for (int i = 0; i < kFlushCount; ++i) { |
| 1144 TestResource* r = SkNEW_ARGS(TestResource, (context->getGpu())); | 1140 TestResource* r = new TestResource(context->getGpu()); |
| 1145 GrUniqueKey k; | 1141 GrUniqueKey k; |
| 1146 make_unique_key<1>(&k, i); | 1142 make_unique_key<1>(&k, i); |
| 1147 r->resourcePriv().setUniqueKey(k); | 1143 r->resourcePriv().setUniqueKey(k); |
| 1148 // Leave a ref on every other resource, beginning with the first. | 1144 // Leave a ref on every other resource, beginning with the first. |
| 1149 if (SkToBool(i & 0x1)) { | 1145 if (SkToBool(i & 0x1)) { |
| 1150 refedResources[i/2] = r; | 1146 refedResources[i/2] = r; |
| 1151 } else { | 1147 } else { |
| 1152 r->unref(); | 1148 r->unref(); |
| 1153 } | 1149 } |
| 1154 cache->notifyFlushOccurred(); | 1150 cache->notifyFlushOccurred(); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1189 GrContext* context = mock.context(); | 1185 GrContext* context = mock.context(); |
| 1190 GrResourceCache* cache = mock.cache(); | 1186 GrResourceCache* cache = mock.cache(); |
| 1191 | 1187 |
| 1192 for (int i = 0; i < kResourceCnt; ++i) { | 1188 for (int i = 0; i < kResourceCnt; ++i) { |
| 1193 GrUniqueKey key1, key2; | 1189 GrUniqueKey key1, key2; |
| 1194 make_unique_key<1>(&key1, i); | 1190 make_unique_key<1>(&key1, i); |
| 1195 make_unique_key<2>(&key2, i); | 1191 make_unique_key<2>(&key2, i); |
| 1196 | 1192 |
| 1197 TestResource* resource; | 1193 TestResource* resource; |
| 1198 | 1194 |
| 1199 resource = SkNEW_ARGS(TestResource, (context->getGpu())); | 1195 resource = new TestResource(context->getGpu()); |
| 1200 resource->resourcePriv().setUniqueKey(key1); | 1196 resource->resourcePriv().setUniqueKey(key1); |
| 1201 resource->setSize(1); | 1197 resource->setSize(1); |
| 1202 resource->unref(); | 1198 resource->unref(); |
| 1203 | 1199 |
| 1204 resource = SkNEW_ARGS(TestResource, (context->getGpu())); | 1200 resource = new TestResource(context->getGpu()); |
| 1205 resource->resourcePriv().setUniqueKey(key2); | 1201 resource->resourcePriv().setUniqueKey(key2); |
| 1206 resource->setSize(1); | 1202 resource->setSize(1); |
| 1207 resource->unref(); | 1203 resource->unref(); |
| 1208 } | 1204 } |
| 1209 | 1205 |
| 1210 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 2 * kResourceCnt); | 1206 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 2 * kResourceCnt); |
| 1211 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 2 * kResource
Cnt); | 1207 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 2 * kResource
Cnt); |
| 1212 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 2 * kResource
Cnt); | 1208 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 2 * kResource
Cnt); |
| 1213 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 2 * kResourceCnt); | 1209 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 2 * kResourceCnt); |
| 1214 REPORTER_ASSERT(reporter, cache->getResourceCount() == 2 * kResourceCnt); | 1210 REPORTER_ASSERT(reporter, cache->getResourceCount() == 2 * kResourceCnt); |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1289 test_purge_invalidated(reporter); | 1285 test_purge_invalidated(reporter); |
| 1290 test_cache_chained_purge(reporter); | 1286 test_cache_chained_purge(reporter); |
| 1291 test_resource_size_changed(reporter); | 1287 test_resource_size_changed(reporter); |
| 1292 test_timestamp_wrap(reporter); | 1288 test_timestamp_wrap(reporter); |
| 1293 test_flush(reporter); | 1289 test_flush(reporter); |
| 1294 test_large_resource_count(reporter); | 1290 test_large_resource_count(reporter); |
| 1295 test_custom_data(reporter); | 1291 test_custom_data(reporter); |
| 1296 } | 1292 } |
| 1297 | 1293 |
| 1298 #endif | 1294 #endif |
| OLD | NEW |