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

Side by Side Diff: tests/ResourceCacheTest.cpp

Issue 1810323002: Cache render targets that render to wrapped textures Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright 2013 Google Inc. 2 * Copyright 2013 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 // Include 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 229 matching lines...) Expand 10 before | Expand all | Expand 10 after
240 240
241 class TestResource : public GrGpuResource { 241 class TestResource : public GrGpuResource {
242 enum ScratchConstructor { kScratchConstructor }; 242 enum ScratchConstructor { kScratchConstructor };
243 public: 243 public:
244 static const size_t kDefaultSize = 100; 244 static const size_t kDefaultSize = 100;
245 245
246 /** Property that distinctly categorizes the resource. 246 /** Property that distinctly categorizes the resource.
247 * For example, textures have width, height, ... */ 247 * For example, textures have width, height, ... */
248 enum SimulatedProperty { kA_SimulatedProperty, kB_SimulatedProperty }; 248 enum SimulatedProperty { kA_SimulatedProperty, kB_SimulatedProperty };
249 249
250 TestResource(GrGpu* gpu, size_t size, GrGpuResource::LifeCycle lifeCycle) 250 TestResource(GrGpu* gpu, size_t size, SkBudgeted budgeted)
251 : INHERITED(gpu, lifeCycle) 251 : INHERITED(gpu, budgeted)
252 , fToDelete(nullptr) 252 , fToDelete(nullptr)
253 , fSize(size) 253 , fSize(size)
254 , fProperty(kA_SimulatedProperty) { 254 , fProperty(kA_SimulatedProperty)
255 , fIsBorrowed(false) {
255 ++fNumAlive; 256 ++fNumAlive;
256 this->registerWithCache(); 257 this->registerWithCache();
257 } 258 }
258 259
259 TestResource(GrGpu* gpu, GrGpuResource::LifeCycle lifeCycle) 260 enum BorrowedConstructor { kBorrowedConstructor };
260 : INHERITED(gpu, lifeCycle) 261
262 TestResource(GrGpu* gpu, size_t size, SkBudgeted budgeted, BorrowedConstruct or)
263 : INHERITED(gpu, budgeted)
264 , fToDelete(nullptr)
265 , fSize(size)
266 , fProperty(kA_SimulatedProperty)
267 , fIsBorrowed(true) {
268 ++fNumAlive;
269 this->registerWithCache();
270 }
271
272 TestResource(GrGpu* gpu, SkBudgeted budgeted)
273 : INHERITED(gpu, budgeted)
261 , fToDelete(nullptr) 274 , fToDelete(nullptr)
262 , fSize(kDefaultSize) 275 , fSize(kDefaultSize)
263 , fProperty(kA_SimulatedProperty) { 276 , fProperty(kA_SimulatedProperty)
277 , fIsBorrowed(false) {
278 ++fNumAlive;
279 this->registerWithCache();
280 }
281
282 TestResource(GrGpu* gpu, SkBudgeted budgeted, BorrowedConstructor)
283 : INHERITED(gpu, budgeted)
284 , fToDelete(nullptr)
285 , fSize(kDefaultSize)
286 , fProperty(kA_SimulatedProperty)
287 , fIsBorrowed(true) {
264 ++fNumAlive; 288 ++fNumAlive;
265 this->registerWithCache(); 289 this->registerWithCache();
266 } 290 }
267 291
268 TestResource(GrGpu* gpu) 292 TestResource(GrGpu* gpu)
269 : INHERITED(gpu, kCached_LifeCycle) 293 : INHERITED(gpu, SkBudgeted::kYes)
270 , fToDelete(nullptr) 294 , fToDelete(nullptr)
271 , fSize(kDefaultSize) 295 , fSize(kDefaultSize)
272 , fProperty(kA_SimulatedProperty) { 296 , fProperty(kA_SimulatedProperty) {
273 ++fNumAlive; 297 ++fNumAlive;
274 this->registerWithCache(); 298 this->registerWithCache();
275 } 299 }
276 300
277 static TestResource* CreateScratch(GrGpu* gpu, SimulatedProperty property, b ool cached = true) { 301 static TestResource* CreateScratch(GrGpu* gpu, SimulatedProperty property,
302 SkBudgeted cached = SkBudgeted::kYes) {
278 return new TestResource(gpu, property, cached, kScratchConstructor); 303 return new TestResource(gpu, property, cached, kScratchConstructor);
279 } 304 }
280 305
281 ~TestResource() { 306 ~TestResource() {
282 --fNumAlive; 307 --fNumAlive;
283 SkSafeUnref(fToDelete); 308 SkSafeUnref(fToDelete);
284 } 309 }
285 310
286 void setSize(size_t size) { 311 void setSize(size_t size) {
287 fSize = size; 312 fSize = size;
(...skipping 11 matching lines...) Expand all
299 GrScratchKey::Builder builder(key, t, kScratchKeyFieldCnt); 324 GrScratchKey::Builder builder(key, t, kScratchKeyFieldCnt);
300 for (int i = 0; i < kScratchKeyFieldCnt; ++i) { 325 for (int i = 0; i < kScratchKeyFieldCnt; ++i) {
301 builder[i] = static_cast<uint32_t>(i + property); 326 builder[i] = static_cast<uint32_t>(i + property);
302 } 327 }
303 } 328 }
304 329
305 static size_t ExpectedScratchKeySize() { 330 static size_t ExpectedScratchKeySize() {
306 return sizeof(uint32_t) * (kScratchKeyFieldCnt + GrScratchKey::kMetaData Cnt); 331 return sizeof(uint32_t) * (kScratchKeyFieldCnt + GrScratchKey::kMetaData Cnt);
307 } 332 }
308 333
334 protected:
335 bool refsWrappedResources() const { return fIsBorrowed; }
336
309 private: 337 private:
310 static const int kScratchKeyFieldCnt = 6; 338 static const int kScratchKeyFieldCnt = 6;
311 339
312 TestResource(GrGpu* gpu, SimulatedProperty property, bool cached, ScratchCon structor) 340 TestResource(GrGpu* gpu, SimulatedProperty property, SkBudgeted budgeted, Sc ratchConstructor)
313 : INHERITED(gpu, cached ? kCached_LifeCycle : kUncached_LifeCycle) 341 : INHERITED(gpu, budgeted)
314 , fToDelete(nullptr) 342 , fToDelete(nullptr)
315 , fSize(kDefaultSize) 343 , fSize(kDefaultSize)
316 , fProperty(property) { 344 , fProperty(property)
345 , fIsBorrowed(false) {
317 GrScratchKey scratchKey; 346 GrScratchKey scratchKey;
318 ComputeScratchKey(fProperty, &scratchKey); 347 ComputeScratchKey(fProperty, &scratchKey);
319 this->setScratchKey(scratchKey); 348 this->setScratchKey(scratchKey);
320 ++fNumAlive; 349 ++fNumAlive;
321 this->registerWithCache(); 350 this->registerWithCache();
322 } 351 }
323 352
324 size_t onGpuMemorySize() const override { return fSize; } 353 size_t onGpuMemorySize() const override { return fSize; }
325 354
326 TestResource* fToDelete; 355 TestResource* fToDelete;
327 size_t fSize; 356 size_t fSize;
328 static int fNumAlive; 357 static int fNumAlive;
329 SimulatedProperty fProperty; 358 SimulatedProperty fProperty;
359 bool fIsBorrowed;
330 typedef GrGpuResource INHERITED; 360 typedef GrGpuResource INHERITED;
331 }; 361 };
332 int TestResource::fNumAlive = 0; 362 int TestResource::fNumAlive = 0;
333 363
334 class Mock { 364 class Mock {
335 public: 365 public:
336 Mock(int maxCnt, size_t maxBytes) { 366 Mock(int maxCnt, size_t maxBytes) {
337 fContext.reset(GrContext::CreateMockContext()); 367 fContext.reset(GrContext::CreateMockContext());
338 SkASSERT(fContext); 368 SkASSERT(fContext);
339 fContext->setResourceCacheLimits(maxCnt, maxBytes); 369 fContext->setResourceCacheLimits(maxCnt, maxBytes);
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
414 GrUniqueKey uniqueKey; 444 GrUniqueKey uniqueKey;
415 make_unique_key<0>(&uniqueKey, 0); 445 make_unique_key<0>(&uniqueKey, 0);
416 446
417 // Create a scratch, a unique, and a wrapped resource 447 // Create a scratch, a unique, and a wrapped resource
418 TestResource* scratch = 448 TestResource* scratch =
419 TestResource::CreateScratch(context->getGpu(), TestResource::kB_Simu latedProperty); 449 TestResource::CreateScratch(context->getGpu(), TestResource::kB_Simu latedProperty);
420 scratch->setSize(10); 450 scratch->setSize(10);
421 TestResource* unique = new TestResource(context->getGpu()); 451 TestResource* unique = new TestResource(context->getGpu());
422 unique->setSize(11); 452 unique->setSize(11);
423 unique->resourcePriv().setUniqueKey(uniqueKey); 453 unique->resourcePriv().setUniqueKey(uniqueKey);
424 TestResource* wrapped = new TestResource(context->getGpu(), GrGpuResource::k Borrowed_LifeCycle); 454 TestResource* wrapped = new TestResource(context->getGpu(), SkBudgeted::kNo,
455 TestResource::kBorrowedConstructor) ;
425 wrapped->setSize(12); 456 wrapped->setSize(12);
426 TestResource* unbudgeted = 457 TestResource* unbudgeted =
427 new TestResource(context->getGpu(), GrGpuResource::kUncached_LifeCyc le); 458 new TestResource(context->getGpu(), SkBudgeted::kNo);
428 unbudgeted->setSize(13); 459 unbudgeted->setSize(13);
429 460
430 // Make sure we can't add a unique key to the wrapped resource 461 // Make sure we can't add a unique key to the wrapped resource
431 GrUniqueKey uniqueKey2; 462 GrUniqueKey uniqueKey2;
432 make_unique_key<0>(&uniqueKey2, 1); 463 make_unique_key<0>(&uniqueKey2, 1);
433 wrapped->resourcePriv().setUniqueKey(uniqueKey2); 464 wrapped->resourcePriv().setUniqueKey(uniqueKey2);
434 REPORTER_ASSERT(reporter, nullptr == cache->findAndRefUniqueResource(uniqueK ey2)); 465 REPORTER_ASSERT(reporter, nullptr == cache->findAndRefUniqueResource(uniqueK ey2));
435 466
436 // Make sure sizes are as we expect 467 // Make sure sizes are as we expect
437 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount()); 468 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
(...skipping 14 matching lines...) Expand all
452 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() == 483 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
453 cache->getBudgetedResourceBytes()); 484 cache->getBudgetedResourceBytes());
454 485
455 // Unreffing the wrapped resource should free it right away. 486 // Unreffing the wrapped resource should free it right away.
456 wrapped->unref(); 487 wrapped->unref();
457 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount()); 488 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
458 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() + 489 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
459 unbudgeted->gpuMemorySize() == cache->getResourceB ytes()); 490 unbudgeted->gpuMemorySize() == cache->getResourceB ytes());
460 491
461 // Now try freeing the budgeted resources first 492 // Now try freeing the budgeted resources first
462 wrapped = new TestResource(context->getGpu(), GrGpuResource::kBorrowed_LifeC ycle); 493 wrapped = new TestResource(context->getGpu(), SkBudgeted::kNo,
494 TestResource::kBorrowedConstructor);
463 scratch->setSize(12); 495 scratch->setSize(12);
464 unique->unref(); 496 unique->unref();
465 cache->purgeAllUnlocked(); 497 cache->purgeAllUnlocked();
466 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount()); 498 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
467 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + wrapped->gpuMemorySize( ) + 499 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + wrapped->gpuMemorySize( ) +
468 unbudgeted->gpuMemorySize() == cache->getResourceB ytes()); 500 unbudgeted->gpuMemorySize() == cache->getResourceB ytes());
469 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount()); 501 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
470 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() == cache->getBudgetedReso urceBytes()); 502 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() == cache->getBudgetedReso urceBytes());
471 503
472 scratch->unref(); 504 scratch->unref();
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
515 unique = new TestResource(context->getGpu()); 547 unique = new TestResource(context->getGpu());
516 unique->setSize(11); 548 unique->setSize(11);
517 unique->resourcePriv().setUniqueKey(uniqueKey); 549 unique->resourcePriv().setUniqueKey(uniqueKey);
518 unique->unref(); 550 unique->unref();
519 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); 551 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
520 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes()); 552 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
521 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount()); 553 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
522 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes()); 554 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
523 555
524 size_t large = 2 * cache->getResourceBytes(); 556 size_t large = 2 * cache->getResourceBytes();
525 unbudgeted = new TestResource(context->getGpu(), large, GrGpuResource::kUnca ched_LifeCycle); 557 unbudgeted = new TestResource(context->getGpu(), large, SkBudgeted::kNo);
526 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount()); 558 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
527 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes()); 559 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
528 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount()); 560 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
529 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes()); 561 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
530 562
531 unbudgeted->unref(); 563 unbudgeted->unref();
532 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); 564 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
533 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes()); 565 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
534 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount()); 566 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
535 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes()); 567 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
536 568
537 wrapped = new TestResource(context->getGpu(), large, GrGpuResource::kBorrowe d_LifeCycle); 569 wrapped = new TestResource(context->getGpu(), large, SkBudgeted::kNo,
570 TestResource::kBorrowedConstructor);
538 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount()); 571 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
539 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes()); 572 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
540 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount()); 573 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
541 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes()); 574 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
542 575
543 wrapped->unref(); 576 wrapped->unref();
544 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); 577 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
545 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes()); 578 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
546 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount()); 579 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
547 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes()); 580 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
548 581
549 cache->purgeAllUnlocked(); 582 cache->purgeAllUnlocked();
550 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); 583 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
551 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes()); 584 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
552 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount()); 585 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
553 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes()); 586 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
554 } 587 }
555 588
556 // This method can't be static because it needs to friended in GrGpuResource::Ca cheAccess. 589 // This method can't be static because it needs to friended in GrGpuResource::Ca cheAccess.
557 void test_unbudgeted_to_scratch(skiatest::Reporter* reporter); 590 void test_unbudgeted_to_scratch(skiatest::Reporter* reporter);
558 /*static*/ void test_unbudgeted_to_scratch(skiatest::Reporter* reporter) { 591 /*static*/ void test_unbudgeted_to_scratch(skiatest::Reporter* reporter) {
559 Mock mock(10, 300); 592 Mock mock(10, 300);
560 GrContext* context = mock.context(); 593 GrContext* context = mock.context();
561 GrResourceCache* cache = mock.cache(); 594 GrResourceCache* cache = mock.cache();
562 595
563 TestResource* resource = 596 TestResource* resource = TestResource::CreateScratch(
564 TestResource::CreateScratch(context->getGpu(), TestResource::kA_Simulate dProperty, false); 597 context->getGpu(), TestResource::kA_SimulatedProperty, SkBudgeted::kNo);
565 GrScratchKey key; 598 GrScratchKey key;
566 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &key); 599 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &key);
567 600
568 size_t size = resource->gpuMemorySize(); 601 size_t size = resource->gpuMemorySize();
569 for (int i = 0; i < 2; ++i) { 602 for (int i = 0; i < 2; ++i) {
570 // Since this resource is unbudgeted, it should not be reachable as scra tch. 603 // Since this resource is unbudgeted, it should not be reachable as scra tch.
571 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == ke y); 604 REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == ke y);
572 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch()); 605 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
573 REPORTER_ASSERT(reporter, SkBudgeted::kNo == resource->resourcePriv().is Budgeted()); 606 REPORTER_ASSERT(reporter, SkBudgeted::kNo == resource->resourcePriv().is Budgeted());
574 REPORTER_ASSERT(reporter, nullptr == cache->findAndRefScratchResource(ke y, TestResource::kDefaultSize, 0)); 607 REPORTER_ASSERT(reporter, nullptr == cache->findAndRefScratchResource(ke y, TestResource::kDefaultSize, 0));
(...skipping 734 matching lines...) Expand 10 before | Expand all | Expand 10 after
1309 test_cache_chained_purge(reporter); 1342 test_cache_chained_purge(reporter);
1310 test_resource_size_changed(reporter); 1343 test_resource_size_changed(reporter);
1311 test_timestamp_wrap(reporter); 1344 test_timestamp_wrap(reporter);
1312 test_flush(reporter); 1345 test_flush(reporter);
1313 test_large_resource_count(reporter); 1346 test_large_resource_count(reporter);
1314 test_custom_data(reporter); 1347 test_custom_data(reporter);
1315 test_abandoned(reporter); 1348 test_abandoned(reporter);
1316 } 1349 }
1317 1350
1318 #endif 1351 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698