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