| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "cc/tiles/gpu_image_decode_controller.h" | 5 #include "cc/tiles/gpu_image_decode_controller.h" |
| 6 | 6 |
| 7 #include <inttypes.h> | 7 #include <inttypes.h> |
| 8 | 8 |
| 9 #include "base/memory/discardable_memory_allocator.h" | 9 #include "base/memory/discardable_memory_allocator.h" |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| (...skipping 346 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 357 | 357 |
| 358 // It is safe to unregister, even if we didn't register in the constructor. | 358 // It is safe to unregister, even if we didn't register in the constructor. |
| 359 base::trace_event::MemoryDumpManager::GetInstance()->UnregisterDumpProvider( | 359 base::trace_event::MemoryDumpManager::GetInstance()->UnregisterDumpProvider( |
| 360 this); | 360 this); |
| 361 } | 361 } |
| 362 | 362 |
| 363 bool GpuImageDecodeController::GetTaskForImageAndRef( | 363 bool GpuImageDecodeController::GetTaskForImageAndRef( |
| 364 const DrawImage& draw_image, | 364 const DrawImage& draw_image, |
| 365 const TracingInfo& tracing_info, | 365 const TracingInfo& tracing_info, |
| 366 scoped_refptr<TileTask>* task) { | 366 scoped_refptr<TileTask>* task) { |
| 367 TRACE_EVENT0("disabled-by-default-cc.debug", |
| 368 "GpuImageDecodeController::GetTaskForImageAndRef"); |
| 367 if (SkipImage(draw_image)) { | 369 if (SkipImage(draw_image)) { |
| 368 *task = nullptr; | 370 *task = nullptr; |
| 369 return false; | 371 return false; |
| 370 } | 372 } |
| 371 | 373 |
| 372 base::AutoLock lock(lock_); | 374 base::AutoLock lock(lock_); |
| 373 const auto image_id = draw_image.image()->uniqueID(); | 375 const auto image_id = draw_image.image()->uniqueID(); |
| 374 ImageData* image_data = GetImageDataForDrawImage(draw_image); | 376 ImageData* image_data = GetImageDataForDrawImage(draw_image); |
| 375 scoped_refptr<ImageData> new_data; | 377 scoped_refptr<ImageData> new_data; |
| 376 if (!image_data) { | 378 if (!image_data) { |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 417 tracing_info)); | 419 tracing_info)); |
| 418 image_data->upload.task = *task; | 420 image_data->upload.task = *task; |
| 419 | 421 |
| 420 // Ref the image again - this ref is owned by the caller, and it is their | 422 // Ref the image again - this ref is owned by the caller, and it is their |
| 421 // responsibility to release it by calling UnrefImage. | 423 // responsibility to release it by calling UnrefImage. |
| 422 RefImage(draw_image); | 424 RefImage(draw_image); |
| 423 return true; | 425 return true; |
| 424 } | 426 } |
| 425 | 427 |
| 426 void GpuImageDecodeController::UnrefImage(const DrawImage& draw_image) { | 428 void GpuImageDecodeController::UnrefImage(const DrawImage& draw_image) { |
| 429 TRACE_EVENT0("disabled-by-default-cc.debug", |
| 430 "GpuImageDecodeController::UnrefImage"); |
| 427 base::AutoLock lock(lock_); | 431 base::AutoLock lock(lock_); |
| 428 UnrefImageInternal(draw_image); | 432 UnrefImageInternal(draw_image); |
| 429 } | 433 } |
| 430 | 434 |
| 431 DecodedDrawImage GpuImageDecodeController::GetDecodedImageForDraw( | 435 DecodedDrawImage GpuImageDecodeController::GetDecodedImageForDraw( |
| 432 const DrawImage& draw_image) { | 436 const DrawImage& draw_image) { |
| 433 TRACE_EVENT0("cc", "GpuImageDecodeController::GetDecodedImageForDraw"); | 437 TRACE_EVENT0("cc", "GpuImageDecodeController::GetDecodedImageForDraw"); |
| 434 | 438 |
| 435 // We are being called during raster. The context lock must already be | 439 // We are being called during raster. The context lock must already be |
| 436 // acquired by the caller. | 440 // acquired by the caller. |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 493 base::AutoLock lock(lock_); | 497 base::AutoLock lock(lock_); |
| 494 UnrefImageInternal(draw_image); | 498 UnrefImageInternal(draw_image); |
| 495 | 499 |
| 496 // We are mid-draw and holding the context lock, ensure we clean up any | 500 // We are mid-draw and holding the context lock, ensure we clean up any |
| 497 // textures (especially at-raster), which may have just been marked for | 501 // textures (especially at-raster), which may have just been marked for |
| 498 // deletion by UnrefImage. | 502 // deletion by UnrefImage. |
| 499 DeletePendingImages(); | 503 DeletePendingImages(); |
| 500 } | 504 } |
| 501 | 505 |
| 502 void GpuImageDecodeController::ReduceCacheUsage() { | 506 void GpuImageDecodeController::ReduceCacheUsage() { |
| 507 TRACE_EVENT0("disabled-by-default-cc.debug", |
| 508 "GpuImageDecodeController::ReduceCacheUsage"); |
| 503 base::AutoLock lock(lock_); | 509 base::AutoLock lock(lock_); |
| 504 EnsureCapacity(0); | 510 EnsureCapacity(0); |
| 505 } | 511 } |
| 506 | 512 |
| 507 void GpuImageDecodeController::SetShouldAggressivelyFreeResources( | 513 void GpuImageDecodeController::SetShouldAggressivelyFreeResources( |
| 508 bool aggressively_free_resources) { | 514 bool aggressively_free_resources) { |
| 515 TRACE_EVENT1("disabled-by-default-cc.debug", |
| 516 "GpuImageDecodeController::SetShouldAggressivelyFreeResources", |
| 517 "agressive_free_resources", aggressively_free_resources); |
| 509 if (aggressively_free_resources) { | 518 if (aggressively_free_resources) { |
| 510 ContextProvider::ScopedContextLock context_lock(context_); | 519 ContextProvider::ScopedContextLock context_lock(context_); |
| 511 base::AutoLock lock(lock_); | 520 base::AutoLock lock(lock_); |
| 512 // We want to keep as little in our cache as possible. Set our memory limit | 521 // We want to keep as little in our cache as possible. Set our memory limit |
| 513 // to zero and EnsureCapacity to clean up memory. | 522 // to zero and EnsureCapacity to clean up memory. |
| 514 cached_bytes_limit_ = 0; | 523 cached_bytes_limit_ = 0; |
| 515 EnsureCapacity(0); | 524 EnsureCapacity(0); |
| 516 | 525 |
| 517 // We are holding the context lock, so finish cleaning up deleted images | 526 // We are holding the context lock, so finish cleaning up deleted images |
| 518 // now. | 527 // now. |
| 519 DeletePendingImages(); | 528 DeletePendingImages(); |
| 520 } else { | 529 } else { |
| 521 base::AutoLock lock(lock_); | 530 base::AutoLock lock(lock_); |
| 522 cached_bytes_limit_ = max_gpu_image_bytes_; | 531 cached_bytes_limit_ = max_gpu_image_bytes_; |
| 523 } | 532 } |
| 524 } | 533 } |
| 525 | 534 |
| 526 bool GpuImageDecodeController::OnMemoryDump( | 535 bool GpuImageDecodeController::OnMemoryDump( |
| 527 const base::trace_event::MemoryDumpArgs& args, | 536 const base::trace_event::MemoryDumpArgs& args, |
| 528 base::trace_event::ProcessMemoryDump* pmd) { | 537 base::trace_event::ProcessMemoryDump* pmd) { |
| 538 TRACE_EVENT0("disabled-by-default-cc.debug", |
| 539 "GpuImageDecodeController::OnMemoryDump"); |
| 529 for (const auto& image_pair : persistent_cache_) { | 540 for (const auto& image_pair : persistent_cache_) { |
| 530 const ImageData* image_data = image_pair.second.get(); | 541 const ImageData* image_data = image_pair.second.get(); |
| 531 const uint32_t image_id = image_pair.first; | 542 const uint32_t image_id = image_pair.first; |
| 532 | 543 |
| 533 // If we have discardable decoded data, dump this here. | 544 // If we have discardable decoded data, dump this here. |
| 534 if (image_data->decode.data()) { | 545 if (image_data->decode.data()) { |
| 535 std::string discardable_dump_name = base::StringPrintf( | 546 std::string discardable_dump_name = base::StringPrintf( |
| 536 "cc/image_memory/controller_0x%" PRIXPTR "/discardable/image_%d", | 547 "cc/image_memory/controller_0x%" PRIXPTR "/discardable/image_%d", |
| 537 reinterpret_cast<uintptr_t>(this), image_id); | 548 reinterpret_cast<uintptr_t>(this), image_id); |
| 538 base::trace_event::MemoryAllocatorDump* dump = | 549 base::trace_event::MemoryAllocatorDump* dump = |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 577 const int kImportance = 3; | 588 const int kImportance = 3; |
| 578 pmd->CreateSharedGlobalAllocatorDump(guid); | 589 pmd->CreateSharedGlobalAllocatorDump(guid); |
| 579 pmd->AddOwnershipEdge(dump->guid(), guid, kImportance); | 590 pmd->AddOwnershipEdge(dump->guid(), guid, kImportance); |
| 580 } | 591 } |
| 581 } | 592 } |
| 582 | 593 |
| 583 return true; | 594 return true; |
| 584 } | 595 } |
| 585 | 596 |
| 586 void GpuImageDecodeController::DecodeImage(const DrawImage& draw_image) { | 597 void GpuImageDecodeController::DecodeImage(const DrawImage& draw_image) { |
| 598 TRACE_EVENT0("disabled-by-default-cc.debug", |
| 599 "GpuImageDecodeController::DecodeImage"); |
| 587 base::AutoLock lock(lock_); | 600 base::AutoLock lock(lock_); |
| 588 ImageData* image_data = GetImageDataForDrawImage(draw_image); | 601 ImageData* image_data = GetImageDataForDrawImage(draw_image); |
| 589 DCHECK(image_data); | 602 DCHECK(image_data); |
| 590 DCHECK(!image_data->is_at_raster); | 603 DCHECK(!image_data->is_at_raster); |
| 591 DecodeImageIfNecessary(draw_image, image_data); | 604 DecodeImageIfNecessary(draw_image, image_data); |
| 592 } | 605 } |
| 593 | 606 |
| 594 void GpuImageDecodeController::UploadImage(const DrawImage& draw_image) { | 607 void GpuImageDecodeController::UploadImage(const DrawImage& draw_image) { |
| 608 TRACE_EVENT0("disabled-by-default-cc.debug", |
| 609 "GpuImageDecodeController::UploadImage"); |
| 595 ContextProvider::ScopedContextLock context_lock(context_); | 610 ContextProvider::ScopedContextLock context_lock(context_); |
| 596 base::AutoLock lock(lock_); | 611 base::AutoLock lock(lock_); |
| 597 ImageData* image_data = GetImageDataForDrawImage(draw_image); | 612 ImageData* image_data = GetImageDataForDrawImage(draw_image); |
| 598 DCHECK(image_data); | 613 DCHECK(image_data); |
| 599 DCHECK(!image_data->is_at_raster); | 614 DCHECK(!image_data->is_at_raster); |
| 600 UploadImageIfNecessary(draw_image, image_data); | 615 UploadImageIfNecessary(draw_image, image_data); |
| 601 } | 616 } |
| 602 | 617 |
| 603 void GpuImageDecodeController::OnImageDecodeTaskCompleted( | 618 void GpuImageDecodeController::OnImageDecodeTaskCompleted( |
| 604 const DrawImage& draw_image) { | 619 const DrawImage& draw_image) { |
| 620 TRACE_EVENT0("disabled-by-default-cc.debug", |
| 621 "GpuImageDecodeController::OnImageDecodeTaskCompleted"); |
| 605 base::AutoLock lock(lock_); | 622 base::AutoLock lock(lock_); |
| 606 // Decode task is complete, remove our reference to it. | 623 // Decode task is complete, remove our reference to it. |
| 607 ImageData* image_data = GetImageDataForDrawImage(draw_image); | 624 ImageData* image_data = GetImageDataForDrawImage(draw_image); |
| 608 DCHECK(image_data); | 625 DCHECK(image_data); |
| 609 DCHECK(image_data->decode.task); | 626 DCHECK(image_data->decode.task); |
| 610 image_data->decode.task = nullptr; | 627 image_data->decode.task = nullptr; |
| 611 | 628 |
| 612 // While the decode task is active, we keep a ref on the decoded data. | 629 // While the decode task is active, we keep a ref on the decoded data. |
| 613 // Release that ref now. | 630 // Release that ref now. |
| 614 UnrefImageDecode(draw_image); | 631 UnrefImageDecode(draw_image); |
| 615 } | 632 } |
| 616 | 633 |
| 617 void GpuImageDecodeController::OnImageUploadTaskCompleted( | 634 void GpuImageDecodeController::OnImageUploadTaskCompleted( |
| 618 const DrawImage& draw_image) { | 635 const DrawImage& draw_image) { |
| 636 TRACE_EVENT0("disabled-by-default-cc.debug", |
| 637 "GpuImageDecodeController::OnImageUploadTaskCompleted"); |
| 619 base::AutoLock lock(lock_); | 638 base::AutoLock lock(lock_); |
| 620 // Upload task is complete, remove our reference to it. | 639 // Upload task is complete, remove our reference to it. |
| 621 ImageData* image_data = GetImageDataForDrawImage(draw_image); | 640 ImageData* image_data = GetImageDataForDrawImage(draw_image); |
| 622 DCHECK(image_data); | 641 DCHECK(image_data); |
| 623 DCHECK(image_data->upload.task); | 642 DCHECK(image_data->upload.task); |
| 624 image_data->upload.task = nullptr; | 643 image_data->upload.task = nullptr; |
| 625 | 644 |
| 626 // While the upload task is active, we keep a ref on both the image it will be | 645 // While the upload task is active, we keep a ref on both the image it will be |
| 627 // populating, as well as the decode it needs to populate it. Release these | 646 // populating, as well as the decode it needs to populate it. Release these |
| 628 // refs now. | 647 // refs now. |
| 629 UnrefImageDecode(draw_image); | 648 UnrefImageDecode(draw_image); |
| 630 UnrefImageInternal(draw_image); | 649 UnrefImageInternal(draw_image); |
| 631 } | 650 } |
| 632 | 651 |
| 633 // Checks if an existing image decode exists. If not, returns a task to produce | 652 // Checks if an existing image decode exists. If not, returns a task to produce |
| 634 // the requested decode. | 653 // the requested decode. |
| 635 scoped_refptr<TileTask> GpuImageDecodeController::GetImageDecodeTaskAndRef( | 654 scoped_refptr<TileTask> GpuImageDecodeController::GetImageDecodeTaskAndRef( |
| 636 const DrawImage& draw_image, | 655 const DrawImage& draw_image, |
| 637 const TracingInfo& tracing_info) { | 656 const TracingInfo& tracing_info) { |
| 657 TRACE_EVENT0("disabled-by-default-cc.debug", |
| 658 "GpuImageDecodeController::GetImageDecodeTaskAndRef"); |
| 638 lock_.AssertAcquired(); | 659 lock_.AssertAcquired(); |
| 639 | 660 |
| 640 // This ref is kept alive while an upload task may need this decode. We | 661 // This ref is kept alive while an upload task may need this decode. We |
| 641 // release this ref in UploadTaskCompleted. | 662 // release this ref in UploadTaskCompleted. |
| 642 RefImageDecode(draw_image); | 663 RefImageDecode(draw_image); |
| 643 | 664 |
| 644 ImageData* image_data = GetImageDataForDrawImage(draw_image); | 665 ImageData* image_data = GetImageDataForDrawImage(draw_image); |
| 645 DCHECK(image_data); | 666 DCHECK(image_data); |
| 646 if (image_data->decode.is_locked()) { | 667 if (image_data->decode.is_locked()) { |
| 647 // We should never be creating a decode task for an at raster image. | 668 // We should never be creating a decode task for an at raster image. |
| 648 DCHECK(!image_data->is_at_raster); | 669 DCHECK(!image_data->is_at_raster); |
| 649 // We should never be creating a decode for an already-uploaded image. | 670 // We should never be creating a decode for an already-uploaded image. |
| 650 DCHECK(!image_data->upload.image()); | 671 DCHECK(!image_data->upload.image()); |
| 651 return nullptr; | 672 return nullptr; |
| 652 } | 673 } |
| 653 | 674 |
| 654 // We didn't have an existing locked image, create a task to lock or decode. | 675 // We didn't have an existing locked image, create a task to lock or decode. |
| 655 scoped_refptr<TileTask>& existing_task = image_data->decode.task; | 676 scoped_refptr<TileTask>& existing_task = image_data->decode.task; |
| 656 if (!existing_task) { | 677 if (!existing_task) { |
| 657 // Ref image decode and create a decode task. This ref will be released in | 678 // Ref image decode and create a decode task. This ref will be released in |
| 658 // DecodeTaskCompleted. | 679 // DecodeTaskCompleted. |
| 659 RefImageDecode(draw_image); | 680 RefImageDecode(draw_image); |
| 660 existing_task = make_scoped_refptr( | 681 existing_task = make_scoped_refptr( |
| 661 new ImageDecodeTaskImpl(this, draw_image, tracing_info)); | 682 new ImageDecodeTaskImpl(this, draw_image, tracing_info)); |
| 662 } | 683 } |
| 663 return existing_task; | 684 return existing_task; |
| 664 } | 685 } |
| 665 | 686 |
| 666 void GpuImageDecodeController::RefImageDecode(const DrawImage& draw_image) { | 687 void GpuImageDecodeController::RefImageDecode(const DrawImage& draw_image) { |
| 688 TRACE_EVENT0("disabled-by-default-cc.debug", |
| 689 "GpuImageDecodeController::RefImageDecode"); |
| 667 lock_.AssertAcquired(); | 690 lock_.AssertAcquired(); |
| 668 auto found = in_use_cache_.find(GenerateInUseCacheKey(draw_image)); | 691 auto found = in_use_cache_.find(GenerateInUseCacheKey(draw_image)); |
| 669 DCHECK(found != in_use_cache_.end()); | 692 DCHECK(found != in_use_cache_.end()); |
| 670 ++found->second.ref_count; | 693 ++found->second.ref_count; |
| 671 ++found->second.image_data->decode.ref_count; | 694 ++found->second.image_data->decode.ref_count; |
| 672 OwnershipChanged(found->second.image_data.get()); | 695 OwnershipChanged(found->second.image_data.get()); |
| 673 } | 696 } |
| 674 | 697 |
| 675 void GpuImageDecodeController::UnrefImageDecode(const DrawImage& draw_image) { | 698 void GpuImageDecodeController::UnrefImageDecode(const DrawImage& draw_image) { |
| 699 TRACE_EVENT0("disabled-by-default-cc.debug", |
| 700 "GpuImageDecodeController::UnrefImageDecode"); |
| 676 lock_.AssertAcquired(); | 701 lock_.AssertAcquired(); |
| 677 auto found = in_use_cache_.find(GenerateInUseCacheKey(draw_image)); | 702 auto found = in_use_cache_.find(GenerateInUseCacheKey(draw_image)); |
| 678 DCHECK(found != in_use_cache_.end()); | 703 DCHECK(found != in_use_cache_.end()); |
| 679 DCHECK_GT(found->second.image_data->decode.ref_count, 0u); | 704 DCHECK_GT(found->second.image_data->decode.ref_count, 0u); |
| 680 DCHECK_GT(found->second.ref_count, 0u); | 705 DCHECK_GT(found->second.ref_count, 0u); |
| 681 --found->second.ref_count; | 706 --found->second.ref_count; |
| 682 --found->second.image_data->decode.ref_count; | 707 --found->second.image_data->decode.ref_count; |
| 683 OwnershipChanged(found->second.image_data.get()); | 708 OwnershipChanged(found->second.image_data.get()); |
| 684 if (found->second.ref_count == 0u) { | 709 if (found->second.ref_count == 0u) { |
| 685 in_use_cache_.erase(found); | 710 in_use_cache_.erase(found); |
| 686 } | 711 } |
| 687 } | 712 } |
| 688 | 713 |
| 689 void GpuImageDecodeController::RefImage(const DrawImage& draw_image) { | 714 void GpuImageDecodeController::RefImage(const DrawImage& draw_image) { |
| 715 TRACE_EVENT0("disabled-by-default-cc.debug", |
| 716 "GpuImageDecodeController::RefImage"); |
| 690 lock_.AssertAcquired(); | 717 lock_.AssertAcquired(); |
| 691 InUseCacheKey key = GenerateInUseCacheKey(draw_image); | 718 InUseCacheKey key = GenerateInUseCacheKey(draw_image); |
| 692 auto found = in_use_cache_.find(key); | 719 auto found = in_use_cache_.find(key); |
| 693 | 720 |
| 694 // If no secondary cache entry was found for the given |draw_image|, then | 721 // If no secondary cache entry was found for the given |draw_image|, then |
| 695 // the draw_image only exists in the |persistent_cache_|. Create an in-use | 722 // the draw_image only exists in the |persistent_cache_|. Create an in-use |
| 696 // cache entry now. | 723 // cache entry now. |
| 697 if (found == in_use_cache_.end()) { | 724 if (found == in_use_cache_.end()) { |
| 698 auto found_image = persistent_cache_.Peek(draw_image.image()->uniqueID()); | 725 auto found_image = persistent_cache_.Peek(draw_image.image()->uniqueID()); |
| 699 DCHECK(found_image != persistent_cache_.end()); | 726 DCHECK(found_image != persistent_cache_.end()); |
| 700 DCHECK(found_image->second->upload_scale_mip_level <= | 727 DCHECK(found_image->second->upload_scale_mip_level <= |
| 701 CalculateUploadScaleMipLevel(draw_image)); | 728 CalculateUploadScaleMipLevel(draw_image)); |
| 702 found = in_use_cache_ | 729 found = in_use_cache_ |
| 703 .insert(InUseCache::value_type( | 730 .insert(InUseCache::value_type( |
| 704 key, InUseCacheEntry(found_image->second))) | 731 key, InUseCacheEntry(found_image->second))) |
| 705 .first; | 732 .first; |
| 706 } | 733 } |
| 707 | 734 |
| 708 DCHECK(found != in_use_cache_.end()); | 735 DCHECK(found != in_use_cache_.end()); |
| 709 ++found->second.ref_count; | 736 ++found->second.ref_count; |
| 710 ++found->second.image_data->upload.ref_count; | 737 ++found->second.image_data->upload.ref_count; |
| 711 OwnershipChanged(found->second.image_data.get()); | 738 OwnershipChanged(found->second.image_data.get()); |
| 712 } | 739 } |
| 713 | 740 |
| 714 void GpuImageDecodeController::UnrefImageInternal(const DrawImage& draw_image) { | 741 void GpuImageDecodeController::UnrefImageInternal(const DrawImage& draw_image) { |
| 742 TRACE_EVENT0("disabled-by-default-cc.debug", |
| 743 "GpuImageDecodeController::UnrefImageInternal"); |
| 715 lock_.AssertAcquired(); | 744 lock_.AssertAcquired(); |
| 716 auto found = in_use_cache_.find(GenerateInUseCacheKey(draw_image)); | 745 auto found = in_use_cache_.find(GenerateInUseCacheKey(draw_image)); |
| 717 DCHECK(found != in_use_cache_.end()); | 746 DCHECK(found != in_use_cache_.end()); |
| 718 DCHECK_GT(found->second.image_data->upload.ref_count, 0u); | 747 DCHECK_GT(found->second.image_data->upload.ref_count, 0u); |
| 719 DCHECK_GT(found->second.ref_count, 0u); | 748 DCHECK_GT(found->second.ref_count, 0u); |
| 720 --found->second.ref_count; | 749 --found->second.ref_count; |
| 721 --found->second.image_data->upload.ref_count; | 750 --found->second.image_data->upload.ref_count; |
| 722 OwnershipChanged(found->second.image_data.get()); | 751 OwnershipChanged(found->second.image_data.get()); |
| 723 if (found->second.ref_count == 0u) { | 752 if (found->second.ref_count == 0u) { |
| 724 in_use_cache_.erase(found); | 753 in_use_cache_.erase(found); |
| 725 } | 754 } |
| 726 } | 755 } |
| 727 | 756 |
| 728 // Called any time an image or decode ref count changes. Takes care of any | 757 // Called any time an image or decode ref count changes. Takes care of any |
| 729 // necessary memory budget book-keeping and cleanup. | 758 // necessary memory budget book-keeping and cleanup. |
| 730 void GpuImageDecodeController::OwnershipChanged(ImageData* image_data) { | 759 void GpuImageDecodeController::OwnershipChanged(ImageData* image_data) { |
| 760 TRACE_EVENT0("disabled-by-default-cc.debug", |
| 761 "GpuImageDecodeController::OwnershipChanged"); |
| 731 lock_.AssertAcquired(); | 762 lock_.AssertAcquired(); |
| 732 | 763 |
| 733 bool has_any_refs = | 764 bool has_any_refs = |
| 734 image_data->upload.ref_count > 0 || image_data->decode.ref_count > 0; | 765 image_data->upload.ref_count > 0 || image_data->decode.ref_count > 0; |
| 735 | 766 |
| 736 // Don't keep around orphaned images. | 767 // Don't keep around orphaned images. |
| 737 if (image_data->is_orphaned && !has_any_refs) { | 768 if (image_data->is_orphaned && !has_any_refs) { |
| 738 images_pending_deletion_.push_back(std::move(image_data->upload.image())); | 769 images_pending_deletion_.push_back(std::move(image_data->upload.image())); |
| 739 image_data->upload.SetImage(nullptr); | 770 image_data->upload.SetImage(nullptr); |
| 740 } | 771 } |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 807 } else { | 838 } else { |
| 808 DCHECK(!image_data->upload.budgeted || image_data->upload.ref_count > 0); | 839 DCHECK(!image_data->upload.budgeted || image_data->upload.ref_count > 0); |
| 809 } | 840 } |
| 810 #endif | 841 #endif |
| 811 } | 842 } |
| 812 | 843 |
| 813 // Ensures that we can fit a new image of size |required_size| in our cache. In | 844 // Ensures that we can fit a new image of size |required_size| in our cache. In |
| 814 // doing so, this function will free unreferenced image data as necessary to | 845 // doing so, this function will free unreferenced image data as necessary to |
| 815 // create rooom. | 846 // create rooom. |
| 816 bool GpuImageDecodeController::EnsureCapacity(size_t required_size) { | 847 bool GpuImageDecodeController::EnsureCapacity(size_t required_size) { |
| 848 TRACE_EVENT0("disabled-by-default-cc.debug", |
| 849 "GpuImageDecodeController::EnsureCapacity"); |
| 817 lock_.AssertAcquired(); | 850 lock_.AssertAcquired(); |
| 818 | 851 |
| 819 if (CanFitSize(required_size) && !ExceedsPreferredCount()) | 852 if (CanFitSize(required_size) && !ExceedsPreferredCount()) |
| 820 return true; | 853 return true; |
| 821 | 854 |
| 822 // While we are over memory or preferred item capacity, we iterate through | 855 // While we are over memory or preferred item capacity, we iterate through |
| 823 // our set of cached image data in LRU order. For each image, we can do two | 856 // our set of cached image data in LRU order. For each image, we can do two |
| 824 // things: 1) We can free the uploaded image, reducing the memory usage of | 857 // things: 1) We can free the uploaded image, reducing the memory usage of |
| 825 // the cache and 2) we can remove the entry entirely, reducing the count of | 858 // the cache and 2) we can remove the entry entirely, reducing the count of |
| 826 // elements in the cache. | 859 // elements in the cache. |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 858 if (CanFitSize(required_size) && !ExceedsPreferredCount()) | 891 if (CanFitSize(required_size) && !ExceedsPreferredCount()) |
| 859 return true; | 892 return true; |
| 860 } | 893 } |
| 861 | 894 |
| 862 // Preferred count is only used as a guideline when triming the cache. Allow | 895 // Preferred count is only used as a guideline when triming the cache. Allow |
| 863 // new elements to be added as long as we are below our size limit. | 896 // new elements to be added as long as we are below our size limit. |
| 864 return CanFitSize(required_size); | 897 return CanFitSize(required_size); |
| 865 } | 898 } |
| 866 | 899 |
| 867 bool GpuImageDecodeController::CanFitSize(size_t size) const { | 900 bool GpuImageDecodeController::CanFitSize(size_t size) const { |
| 901 TRACE_EVENT0("disabled-by-default-cc.debug", |
| 902 "GpuImageDecodeController::CanFitSize"); |
| 868 lock_.AssertAcquired(); | 903 lock_.AssertAcquired(); |
| 869 | 904 |
| 870 base::CheckedNumeric<uint32_t> new_size(bytes_used_); | 905 base::CheckedNumeric<uint32_t> new_size(bytes_used_); |
| 871 new_size += size; | 906 new_size += size; |
| 872 return new_size.IsValid() && new_size.ValueOrDie() <= cached_bytes_limit_; | 907 return new_size.IsValid() && new_size.ValueOrDie() <= cached_bytes_limit_; |
| 873 } | 908 } |
| 874 | 909 |
| 875 bool GpuImageDecodeController::ExceedsPreferredCount() const { | 910 bool GpuImageDecodeController::ExceedsPreferredCount() const { |
| 911 TRACE_EVENT0("disabled-by-default-cc.debug", |
| 912 "GpuImageDecodeController::ExceedsPreferredCount"); |
| 876 lock_.AssertAcquired(); | 913 lock_.AssertAcquired(); |
| 877 | 914 |
| 878 return persistent_cache_.size() > cached_items_limit_; | 915 return persistent_cache_.size() > cached_items_limit_; |
| 879 } | 916 } |
| 880 | 917 |
| 881 void GpuImageDecodeController::DecodeImageIfNecessary( | 918 void GpuImageDecodeController::DecodeImageIfNecessary( |
| 882 const DrawImage& draw_image, | 919 const DrawImage& draw_image, |
| 883 ImageData* image_data) { | 920 ImageData* image_data) { |
| 884 lock_.AssertAcquired(); | 921 lock_.AssertAcquired(); |
| 885 | 922 |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1011 DCHECK(uploaded_image); | 1048 DCHECK(uploaded_image); |
| 1012 | 1049 |
| 1013 // At-raster may have decoded this while we were unlocked. If so, ignore our | 1050 // At-raster may have decoded this while we were unlocked. If so, ignore our |
| 1014 // result. | 1051 // result. |
| 1015 if (!image_data->upload.image()) | 1052 if (!image_data->upload.image()) |
| 1016 image_data->upload.SetImage(std::move(uploaded_image)); | 1053 image_data->upload.SetImage(std::move(uploaded_image)); |
| 1017 } | 1054 } |
| 1018 | 1055 |
| 1019 scoped_refptr<GpuImageDecodeController::ImageData> | 1056 scoped_refptr<GpuImageDecodeController::ImageData> |
| 1020 GpuImageDecodeController::CreateImageData(const DrawImage& draw_image) { | 1057 GpuImageDecodeController::CreateImageData(const DrawImage& draw_image) { |
| 1058 TRACE_EVENT0("disabled-by-default-cc.debug", |
| 1059 "GpuImageDecodeController::CreateImageData"); |
| 1021 lock_.AssertAcquired(); | 1060 lock_.AssertAcquired(); |
| 1022 | 1061 |
| 1023 DecodedDataMode mode; | 1062 DecodedDataMode mode; |
| 1024 int upload_scale_mip_level = CalculateUploadScaleMipLevel(draw_image); | 1063 int upload_scale_mip_level = CalculateUploadScaleMipLevel(draw_image); |
| 1025 auto params = SkImage::DeferredTextureImageUsageParams( | 1064 auto params = SkImage::DeferredTextureImageUsageParams( |
| 1026 draw_image.matrix(), CalculateUploadScaleFilterQuality(draw_image), | 1065 draw_image.matrix(), CalculateUploadScaleFilterQuality(draw_image), |
| 1027 upload_scale_mip_level); | 1066 upload_scale_mip_level); |
| 1028 size_t data_size = draw_image.image()->getDeferredTextureImageData( | 1067 size_t data_size = draw_image.image()->getDeferredTextureImageData( |
| 1029 *context_threadsafe_proxy_.get(), ¶ms, 1, nullptr); | 1068 *context_threadsafe_proxy_.get(), ¶ms, 1, nullptr); |
| 1030 | 1069 |
| 1031 if (data_size == 0) { | 1070 if (data_size == 0) { |
| 1032 // Can't upload image, too large or other failure. Try to use SW fallback. | 1071 // Can't upload image, too large or other failure. Try to use SW fallback. |
| 1033 SkImageInfo image_info = | 1072 SkImageInfo image_info = |
| 1034 CreateImageInfoForDrawImage(draw_image, upload_scale_mip_level); | 1073 CreateImageInfoForDrawImage(draw_image, upload_scale_mip_level); |
| 1035 data_size = image_info.getSafeSize(image_info.minRowBytes()); | 1074 data_size = image_info.getSafeSize(image_info.minRowBytes()); |
| 1036 mode = DecodedDataMode::CPU; | 1075 mode = DecodedDataMode::CPU; |
| 1037 } else { | 1076 } else { |
| 1038 mode = DecodedDataMode::GPU; | 1077 mode = DecodedDataMode::GPU; |
| 1039 } | 1078 } |
| 1040 | 1079 |
| 1041 return make_scoped_refptr( | 1080 return make_scoped_refptr( |
| 1042 new ImageData(mode, data_size, upload_scale_mip_level, | 1081 new ImageData(mode, data_size, upload_scale_mip_level, |
| 1043 CalculateUploadScaleFilterQuality(draw_image))); | 1082 CalculateUploadScaleFilterQuality(draw_image))); |
| 1044 } | 1083 } |
| 1045 | 1084 |
| 1046 void GpuImageDecodeController::DeletePendingImages() { | 1085 void GpuImageDecodeController::DeletePendingImages() { |
| 1086 TRACE_EVENT0("disabled-by-default-cc.debug", |
| 1087 "GpuImageDecodeController::DeletePendingImages"); |
| 1047 context_->GetLock()->AssertAcquired(); | 1088 context_->GetLock()->AssertAcquired(); |
| 1048 lock_.AssertAcquired(); | 1089 lock_.AssertAcquired(); |
| 1049 images_pending_deletion_.clear(); | 1090 images_pending_deletion_.clear(); |
| 1050 } | 1091 } |
| 1051 | 1092 |
| 1052 SkImageInfo GpuImageDecodeController::CreateImageInfoForDrawImage( | 1093 SkImageInfo GpuImageDecodeController::CreateImageInfoForDrawImage( |
| 1053 const DrawImage& draw_image, | 1094 const DrawImage& draw_image, |
| 1054 int upload_scale_mip_level) const { | 1095 int upload_scale_mip_level) const { |
| 1096 TRACE_EVENT0("disabled-by-default-cc.debug", |
| 1097 "GpuImageDecodeController::CreateImageInfoForDrawImage"); |
| 1055 gfx::Size mip_size = | 1098 gfx::Size mip_size = |
| 1056 CalculateSizeForMipLevel(draw_image, upload_scale_mip_level); | 1099 CalculateSizeForMipLevel(draw_image, upload_scale_mip_level); |
| 1057 return SkImageInfo::Make(mip_size.width(), mip_size.height(), | 1100 return SkImageInfo::Make(mip_size.width(), mip_size.height(), |
| 1058 ResourceFormatToClosestSkColorType(format_), | 1101 ResourceFormatToClosestSkColorType(format_), |
| 1059 kPremul_SkAlphaType); | 1102 kPremul_SkAlphaType); |
| 1060 } | 1103 } |
| 1061 | 1104 |
| 1062 // Tries to find an ImageData that can be used to draw the provided | 1105 // Tries to find an ImageData that can be used to draw the provided |
| 1063 // |draw_image|. First looks for an exact entry in our |in_use_cache_|. If one | 1106 // |draw_image|. First looks for an exact entry in our |in_use_cache_|. If one |
| 1064 // cannot be found, it looks for a compatible entry in our |persistent_cache_|. | 1107 // cannot be found, it looks for a compatible entry in our |persistent_cache_|. |
| 1065 GpuImageDecodeController::ImageData* | 1108 GpuImageDecodeController::ImageData* |
| 1066 GpuImageDecodeController::GetImageDataForDrawImage( | 1109 GpuImageDecodeController::GetImageDataForDrawImage( |
| 1067 const DrawImage& draw_image) { | 1110 const DrawImage& draw_image) { |
| 1111 TRACE_EVENT0("disabled-by-default-cc.debug", |
| 1112 "GpuImageDecodeController::GetImageDataForDrawImage"); |
| 1068 lock_.AssertAcquired(); | 1113 lock_.AssertAcquired(); |
| 1069 auto found_in_use = in_use_cache_.find(GenerateInUseCacheKey(draw_image)); | 1114 auto found_in_use = in_use_cache_.find(GenerateInUseCacheKey(draw_image)); |
| 1070 if (found_in_use != in_use_cache_.end()) | 1115 if (found_in_use != in_use_cache_.end()) |
| 1071 return found_in_use->second.image_data.get(); | 1116 return found_in_use->second.image_data.get(); |
| 1072 | 1117 |
| 1073 auto found_persistent = persistent_cache_.Get(draw_image.image()->uniqueID()); | 1118 auto found_persistent = persistent_cache_.Get(draw_image.image()->uniqueID()); |
| 1074 if (found_persistent != persistent_cache_.end()) { | 1119 if (found_persistent != persistent_cache_.end()) { |
| 1075 ImageData* image_data = found_persistent->second.get(); | 1120 ImageData* image_data = found_persistent->second.get(); |
| 1076 if (IsCompatible(image_data, draw_image)) { | 1121 if (IsCompatible(image_data, draw_image)) { |
| 1077 return image_data; | 1122 return image_data; |
| 1078 } else { | 1123 } else { |
| 1079 found_persistent->second->is_orphaned = true; | 1124 found_persistent->second->is_orphaned = true; |
| 1080 // Call OwnershipChanged before erasing the orphaned task from the | 1125 // Call OwnershipChanged before erasing the orphaned task from the |
| 1081 // persistent cache. This ensures that if the orphaned task has 0 | 1126 // persistent cache. This ensures that if the orphaned task has 0 |
| 1082 // references, it is cleaned up safely before it is deleted. | 1127 // references, it is cleaned up safely before it is deleted. |
| 1083 OwnershipChanged(image_data); | 1128 OwnershipChanged(image_data); |
| 1084 persistent_cache_.Erase(found_persistent); | 1129 persistent_cache_.Erase(found_persistent); |
| 1085 } | 1130 } |
| 1086 } | 1131 } |
| 1087 | 1132 |
| 1088 return nullptr; | 1133 return nullptr; |
| 1089 } | 1134 } |
| 1090 | 1135 |
| 1091 // Determines if we can draw the provided |draw_image| using the provided | 1136 // Determines if we can draw the provided |draw_image| using the provided |
| 1092 // |image_data|. This is true if the |image_data| is not scaled, or if it | 1137 // |image_data|. This is true if the |image_data| is not scaled, or if it |
| 1093 // is scaled at an equal or larger scale and equal or larger quality to | 1138 // is scaled at an equal or larger scale and equal or larger quality to |
| 1094 // the provided |draw_image|. | 1139 // the provided |draw_image|. |
| 1095 bool GpuImageDecodeController::IsCompatible(const ImageData* image_data, | 1140 bool GpuImageDecodeController::IsCompatible(const ImageData* image_data, |
| 1096 const DrawImage& draw_image) const { | 1141 const DrawImage& draw_image) const { |
| 1142 TRACE_EVENT0("disabled-by-default-cc.debug", |
| 1143 "GpuImageDecodeController::IsCompatible"); |
| 1097 bool is_scaled = image_data->upload_scale_mip_level != 0; | 1144 bool is_scaled = image_data->upload_scale_mip_level != 0; |
| 1098 bool scale_is_compatible = CalculateUploadScaleMipLevel(draw_image) >= | 1145 bool scale_is_compatible = CalculateUploadScaleMipLevel(draw_image) >= |
| 1099 image_data->upload_scale_mip_level; | 1146 image_data->upload_scale_mip_level; |
| 1100 bool quality_is_compatible = CalculateUploadScaleFilterQuality(draw_image) <= | 1147 bool quality_is_compatible = CalculateUploadScaleFilterQuality(draw_image) <= |
| 1101 image_data->upload_scale_filter_quality; | 1148 image_data->upload_scale_filter_quality; |
| 1102 return !is_scaled || (scale_is_compatible && quality_is_compatible); | 1149 return !is_scaled || (scale_is_compatible && quality_is_compatible); |
| 1103 } | 1150 } |
| 1104 | 1151 |
| 1105 size_t GpuImageDecodeController::GetDrawImageSizeForTesting( | 1152 size_t GpuImageDecodeController::GetDrawImageSizeForTesting( |
| 1106 const DrawImage& image) { | 1153 const DrawImage& image) { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 1121 bool GpuImageDecodeController::DiscardableIsLockedForTesting( | 1168 bool GpuImageDecodeController::DiscardableIsLockedForTesting( |
| 1122 const DrawImage& image) { | 1169 const DrawImage& image) { |
| 1123 base::AutoLock lock(lock_); | 1170 base::AutoLock lock(lock_); |
| 1124 auto found = persistent_cache_.Peek(image.image()->uniqueID()); | 1171 auto found = persistent_cache_.Peek(image.image()->uniqueID()); |
| 1125 DCHECK(found != persistent_cache_.end()); | 1172 DCHECK(found != persistent_cache_.end()); |
| 1126 ImageData* image_data = found->second.get(); | 1173 ImageData* image_data = found->second.get(); |
| 1127 return image_data->decode.is_locked(); | 1174 return image_data->decode.is_locked(); |
| 1128 } | 1175 } |
| 1129 | 1176 |
| 1130 } // namespace cc | 1177 } // namespace cc |
| OLD | NEW |