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