| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "content/browser/compositor/delegated_frame_host.h" | 5 #include "content/browser/compositor/delegated_frame_host.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <utility> | 9 #include <utility> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 (preferred_color_type == kRGB_565_SkColorType) || | 144 (preferred_color_type == kRGB_565_SkColorType) || |
| 145 (preferred_color_type == kN32_SkColorType)); | 145 (preferred_color_type == kN32_SkColorType)); |
| 146 DCHECK(format_support); | 146 DCHECK(format_support); |
| 147 if (!CanCopyToBitmap()) { | 147 if (!CanCopyToBitmap()) { |
| 148 callback.Run(SkBitmap(), content::READBACK_SURFACE_UNAVAILABLE); | 148 callback.Run(SkBitmap(), content::READBACK_SURFACE_UNAVAILABLE); |
| 149 return; | 149 return; |
| 150 } | 150 } |
| 151 | 151 |
| 152 scoped_ptr<cc::CopyOutputRequest> request = | 152 scoped_ptr<cc::CopyOutputRequest> request = |
| 153 cc::CopyOutputRequest::CreateRequest( | 153 cc::CopyOutputRequest::CreateRequest( |
| 154 base::Bind(&DelegatedFrameHost::CopyFromCompositingSurfaceHasResult, | 154 base::Bind(&CopyFromCompositingSurfaceHasResult, output_size, |
| 155 output_size, preferred_color_type, callback)); | 155 preferred_color_type, callback)); |
| 156 if (!src_subrect.IsEmpty()) | 156 if (!src_subrect.IsEmpty()) |
| 157 request->set_area(src_subrect); | 157 request->set_area(src_subrect); |
| 158 RequestCopyOfOutput(std::move(request)); | 158 RequestCopyOfOutput(std::move(request)); |
| 159 } | 159 } |
| 160 | 160 |
| 161 void DelegatedFrameHost::CopyFromCompositingSurfaceToVideoFrame( | 161 void DelegatedFrameHost::CopyFromCompositingSurfaceToVideoFrame( |
| 162 const gfx::Rect& src_subrect, | 162 const gfx::Rect& src_subrect, |
| 163 const scoped_refptr<media::VideoFrame>& target, | 163 const scoped_refptr<media::VideoFrame>& target, |
| 164 const base::Callback<void(const gfx::Rect&, bool)>& callback) { | 164 const base::Callback<void(const gfx::Rect&, bool)>& callback) { |
| 165 if (!CanCopyToVideoFrame()) { | 165 if (!CanCopyToVideoFrame()) { |
| (...skipping 420 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 586 client_->DelegatedFrameHostGetLayer()->SetShowSolidColorContent(); | 586 client_->DelegatedFrameHostGetLayer()->SetShowSolidColorContent(); |
| 587 frame_provider_ = NULL; | 587 frame_provider_ = NULL; |
| 588 if (!surface_id_.is_null()) { | 588 if (!surface_id_.is_null()) { |
| 589 surface_factory_->Destroy(surface_id_); | 589 surface_factory_->Destroy(surface_id_); |
| 590 surface_id_ = cc::SurfaceId(); | 590 surface_id_ = cc::SurfaceId(); |
| 591 } | 591 } |
| 592 delegated_frame_evictor_->DiscardedFrame(); | 592 delegated_frame_evictor_->DiscardedFrame(); |
| 593 } | 593 } |
| 594 | 594 |
| 595 // static | 595 // static |
| 596 void DelegatedFrameHost::CopyFromCompositingSurfaceHasResult( | |
| 597 const gfx::Size& dst_size_in_pixel, | |
| 598 const SkColorType color_type, | |
| 599 const ReadbackRequestCallback& callback, | |
| 600 scoped_ptr<cc::CopyOutputResult> result) { | |
| 601 if (result->IsEmpty() || result->size().IsEmpty()) { | |
| 602 callback.Run(SkBitmap(), content::READBACK_FAILED); | |
| 603 return; | |
| 604 } | |
| 605 | |
| 606 gfx::Size output_size_in_pixel; | |
| 607 if (dst_size_in_pixel.IsEmpty()) | |
| 608 output_size_in_pixel = result->size(); | |
| 609 else | |
| 610 output_size_in_pixel = dst_size_in_pixel; | |
| 611 | |
| 612 if (result->HasTexture()) { | |
| 613 // GPU-accelerated path | |
| 614 PrepareTextureCopyOutputResult(output_size_in_pixel, color_type, callback, | |
| 615 std::move(result)); | |
| 616 return; | |
| 617 } | |
| 618 | |
| 619 DCHECK(result->HasBitmap()); | |
| 620 // Software path | |
| 621 PrepareBitmapCopyOutputResult(output_size_in_pixel, color_type, callback, | |
| 622 std::move(result)); | |
| 623 } | |
| 624 | |
| 625 static void CopyFromCompositingSurfaceFinished( | |
| 626 const ReadbackRequestCallback& callback, | |
| 627 scoped_ptr<cc::SingleReleaseCallback> release_callback, | |
| 628 scoped_ptr<SkBitmap> bitmap, | |
| 629 scoped_ptr<SkAutoLockPixels> bitmap_pixels_lock, | |
| 630 bool result) { | |
| 631 bitmap_pixels_lock.reset(); | |
| 632 | |
| 633 gpu::SyncToken sync_token; | |
| 634 if (result) { | |
| 635 GLHelper* gl_helper = ImageTransportFactory::GetInstance()->GetGLHelper(); | |
| 636 if (gl_helper) | |
| 637 gl_helper->GenerateSyncToken(&sync_token); | |
| 638 } | |
| 639 const bool lost_resource = !sync_token.HasData(); | |
| 640 release_callback->Run(sync_token, lost_resource); | |
| 641 | |
| 642 callback.Run(*bitmap, | |
| 643 result ? content::READBACK_SUCCESS : content::READBACK_FAILED); | |
| 644 } | |
| 645 | |
| 646 // static | |
| 647 void DelegatedFrameHost::PrepareTextureCopyOutputResult( | |
| 648 const gfx::Size& dst_size_in_pixel, | |
| 649 const SkColorType color_type, | |
| 650 const ReadbackRequestCallback& callback, | |
| 651 scoped_ptr<cc::CopyOutputResult> result) { | |
| 652 DCHECK(result->HasTexture()); | |
| 653 base::ScopedClosureRunner scoped_callback_runner( | |
| 654 base::Bind(callback, SkBitmap(), content::READBACK_FAILED)); | |
| 655 | |
| 656 // TODO(siva.gunturi): We should be able to validate the format here using | |
| 657 // GLHelper::IsReadbackConfigSupported before we processs the result. | |
| 658 // See crbug.com/415682 and crbug.com/415131. | |
| 659 scoped_ptr<SkBitmap> bitmap(new SkBitmap); | |
| 660 if (!bitmap->tryAllocPixels(SkImageInfo::Make( | |
| 661 dst_size_in_pixel.width(), dst_size_in_pixel.height(), color_type, | |
| 662 kOpaque_SkAlphaType))) { | |
| 663 scoped_callback_runner.Reset(base::Bind( | |
| 664 callback, SkBitmap(), content::READBACK_BITMAP_ALLOCATION_FAILURE)); | |
| 665 return; | |
| 666 } | |
| 667 | |
| 668 ImageTransportFactory* factory = ImageTransportFactory::GetInstance(); | |
| 669 GLHelper* gl_helper = factory->GetGLHelper(); | |
| 670 if (!gl_helper) | |
| 671 return; | |
| 672 | |
| 673 scoped_ptr<SkAutoLockPixels> bitmap_pixels_lock( | |
| 674 new SkAutoLockPixels(*bitmap)); | |
| 675 uint8_t* pixels = static_cast<uint8_t*>(bitmap->getPixels()); | |
| 676 | |
| 677 cc::TextureMailbox texture_mailbox; | |
| 678 scoped_ptr<cc::SingleReleaseCallback> release_callback; | |
| 679 result->TakeTexture(&texture_mailbox, &release_callback); | |
| 680 DCHECK(texture_mailbox.IsTexture()); | |
| 681 | |
| 682 ignore_result(scoped_callback_runner.Release()); | |
| 683 | |
| 684 gl_helper->CropScaleReadbackAndCleanMailbox( | |
| 685 texture_mailbox.mailbox(), texture_mailbox.sync_token(), result->size(), | |
| 686 gfx::Rect(result->size()), dst_size_in_pixel, pixels, color_type, | |
| 687 base::Bind(&CopyFromCompositingSurfaceFinished, callback, | |
| 688 base::Passed(&release_callback), base::Passed(&bitmap), | |
| 689 base::Passed(&bitmap_pixels_lock)), | |
| 690 GLHelper::SCALER_QUALITY_GOOD); | |
| 691 } | |
| 692 | |
| 693 // static | |
| 694 void DelegatedFrameHost::PrepareBitmapCopyOutputResult( | |
| 695 const gfx::Size& dst_size_in_pixel, | |
| 696 const SkColorType preferred_color_type, | |
| 697 const ReadbackRequestCallback& callback, | |
| 698 scoped_ptr<cc::CopyOutputResult> result) { | |
| 699 SkColorType color_type = preferred_color_type; | |
| 700 if (color_type != kN32_SkColorType && color_type != kAlpha_8_SkColorType) { | |
| 701 // Switch back to default colortype if format not supported. | |
| 702 color_type = kN32_SkColorType; | |
| 703 } | |
| 704 DCHECK(result->HasBitmap()); | |
| 705 scoped_ptr<SkBitmap> source = result->TakeBitmap(); | |
| 706 DCHECK(source); | |
| 707 SkBitmap scaled_bitmap; | |
| 708 if (source->width() != dst_size_in_pixel.width() || | |
| 709 source->height() != dst_size_in_pixel.height()) { | |
| 710 scaled_bitmap = | |
| 711 skia::ImageOperations::Resize(*source, | |
| 712 skia::ImageOperations::RESIZE_BEST, | |
| 713 dst_size_in_pixel.width(), | |
| 714 dst_size_in_pixel.height()); | |
| 715 } else { | |
| 716 scaled_bitmap = *source; | |
| 717 } | |
| 718 if (color_type == kN32_SkColorType) { | |
| 719 DCHECK_EQ(scaled_bitmap.colorType(), kN32_SkColorType); | |
| 720 callback.Run(scaled_bitmap, READBACK_SUCCESS); | |
| 721 return; | |
| 722 } | |
| 723 DCHECK_EQ(color_type, kAlpha_8_SkColorType); | |
| 724 // The software path currently always returns N32 bitmap regardless of the | |
| 725 // |color_type| we ask for. | |
| 726 DCHECK_EQ(scaled_bitmap.colorType(), kN32_SkColorType); | |
| 727 // Paint |scaledBitmap| to alpha-only |grayscale_bitmap|. | |
| 728 SkBitmap grayscale_bitmap; | |
| 729 bool success = grayscale_bitmap.tryAllocPixels( | |
| 730 SkImageInfo::MakeA8(scaled_bitmap.width(), scaled_bitmap.height())); | |
| 731 if (!success) { | |
| 732 callback.Run(SkBitmap(), content::READBACK_BITMAP_ALLOCATION_FAILURE); | |
| 733 return; | |
| 734 } | |
| 735 SkCanvas canvas(grayscale_bitmap); | |
| 736 SkPaint paint; | |
| 737 skia::RefPtr<SkColorFilter> filter = | |
| 738 skia::AdoptRef(SkLumaColorFilter::Create()); | |
| 739 paint.setColorFilter(filter.get()); | |
| 740 canvas.drawBitmap(scaled_bitmap, SkIntToScalar(0), SkIntToScalar(0), &paint); | |
| 741 callback.Run(grayscale_bitmap, READBACK_SUCCESS); | |
| 742 } | |
| 743 | |
| 744 // static | |
| 745 void DelegatedFrameHost::ReturnSubscriberTexture( | 596 void DelegatedFrameHost::ReturnSubscriberTexture( |
| 746 base::WeakPtr<DelegatedFrameHost> dfh, | 597 base::WeakPtr<DelegatedFrameHost> dfh, |
| 747 scoped_refptr<OwnedMailbox> subscriber_texture, | 598 scoped_refptr<OwnedMailbox> subscriber_texture, |
| 748 const gpu::SyncToken& sync_token) { | 599 const gpu::SyncToken& sync_token) { |
| 749 if (!subscriber_texture.get()) | 600 if (!subscriber_texture.get()) |
| 750 return; | 601 return; |
| 751 if (!dfh) | 602 if (!dfh) |
| 752 return; | 603 return; |
| 753 | 604 |
| 754 subscriber_texture->UpdateSyncToken(sync_token); | 605 subscriber_texture->UpdateSyncToken(sync_token); |
| (...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1040 vsync_interval_ = interval; | 891 vsync_interval_ = interval; |
| 1041 } | 892 } |
| 1042 | 893 |
| 1043 void DelegatedFrameHost::LockResources() { | 894 void DelegatedFrameHost::LockResources() { |
| 1044 DCHECK(frame_provider_.get() || !surface_id_.is_null()); | 895 DCHECK(frame_provider_.get() || !surface_id_.is_null()); |
| 1045 delegated_frame_evictor_->LockFrame(); | 896 delegated_frame_evictor_->LockFrame(); |
| 1046 } | 897 } |
| 1047 | 898 |
| 1048 void DelegatedFrameHost::RequestCopyOfOutput( | 899 void DelegatedFrameHost::RequestCopyOfOutput( |
| 1049 scoped_ptr<cc::CopyOutputRequest> request) { | 900 scoped_ptr<cc::CopyOutputRequest> request) { |
| 1050 if (!request_copy_of_output_callback_for_testing_.is_null()) | 901 if (!request_copy_of_output_callback_for_testing_.is_null()) { |
| 1051 request_copy_of_output_callback_for_testing_.Run(std::move(request)); | 902 request_copy_of_output_callback_for_testing_.Run(std::move(request)); |
| 1052 else | 903 } else { |
| 1053 client_->DelegatedFrameHostGetLayer()->RequestCopyOfOutput( | 904 client_->DelegatedFrameHostGetLayer()->RequestCopyOfOutput( |
| 1054 std::move(request)); | 905 std::move(request)); |
| 906 } |
| 1055 } | 907 } |
| 1056 | 908 |
| 1057 void DelegatedFrameHost::UnlockResources() { | 909 void DelegatedFrameHost::UnlockResources() { |
| 1058 DCHECK(frame_provider_.get() || !surface_id_.is_null()); | 910 DCHECK(frame_provider_.get() || !surface_id_.is_null()); |
| 1059 delegated_frame_evictor_->UnlockFrame(); | 911 delegated_frame_evictor_->UnlockFrame(); |
| 1060 } | 912 } |
| 1061 | 913 |
| 1062 //////////////////////////////////////////////////////////////////////////////// | 914 //////////////////////////////////////////////////////////////////////////////// |
| 1063 // DelegatedFrameHost, ui::LayerOwnerDelegate implementation: | 915 // DelegatedFrameHost, ui::LayerOwnerDelegate implementation: |
| 1064 | 916 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 1076 cc::SurfaceManager* manager = factory->GetSurfaceManager(); | 928 cc::SurfaceManager* manager = factory->GetSurfaceManager(); |
| 1077 new_layer->SetShowSurface( | 929 new_layer->SetShowSurface( |
| 1078 surface_id_, base::Bind(&SatisfyCallback, base::Unretained(manager)), | 930 surface_id_, base::Bind(&SatisfyCallback, base::Unretained(manager)), |
| 1079 base::Bind(&RequireCallback, base::Unretained(manager)), | 931 base::Bind(&RequireCallback, base::Unretained(manager)), |
| 1080 current_surface_size_, current_scale_factor_, | 932 current_surface_size_, current_scale_factor_, |
| 1081 current_frame_size_in_dip_); | 933 current_frame_size_in_dip_); |
| 1082 } | 934 } |
| 1083 } | 935 } |
| 1084 | 936 |
| 1085 } // namespace content | 937 } // namespace content |
| OLD | NEW |