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