| 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 "content/browser/media/android/browser_surface_view_manager.h" | 5 #include "content/browser/media/android/browser_surface_view_manager.h" |
| 6 | 6 |
| 7 #include "base/android/build_info.h" | 7 #include "base/android/build_info.h" |
| 8 #include "base/trace_event/trace_event.h" | 8 #include "base/trace_event/trace_event.h" |
| 9 #include "content/browser/android/content_view_core_impl.h" | |
| 10 #include "content/browser/gpu/gpu_process_host.h" | 9 #include "content/browser/gpu/gpu_process_host.h" |
| 11 #include "content/browser/web_contents/web_contents_impl.h" | 10 #include "content/browser/web_contents/web_contents_impl.h" |
| 12 #include "content/common/media/surface_view_manager_messages_android.h" | 11 #include "content/common/media/surface_view_manager_messages_android.h" |
| 13 #include "content/public/browser/render_frame_host.h" | 12 #include "content/public/browser/render_frame_host.h" |
| 14 #include "content/public/browser/web_contents_delegate.h" | 13 #include "content/public/browser/web_contents_delegate.h" |
| 15 #include "gpu/ipc/common/gpu_surface_tracker.h" | 14 #include "gpu/ipc/common/gpu_surface_tracker.h" |
| 16 #include "media/base/surface_manager.h" | 15 #include "media/base/surface_manager.h" |
| 17 #include "ui/gfx/geometry/size.h" | 16 #include "ui/gfx/geometry/size.h" |
| 18 | 17 |
| 19 namespace content { | 18 namespace content { |
| 20 namespace { | 19 namespace { |
| 21 void SendDestroyingVideoSurfaceOnIO(int surface_id, | 20 void SendDestroyingVideoSurfaceOnIO(int surface_id, |
| 22 const base::Closure& done_cb) { | 21 const base::Closure& done_cb) { |
| 23 GpuProcessHost* host = GpuProcessHost::Get( | 22 GpuProcessHost* host = GpuProcessHost::Get( |
| 24 GpuProcessHost::GPU_PROCESS_KIND_SANDBOXED, false /* force_create */); | 23 GpuProcessHost::GPU_PROCESS_KIND_SANDBOXED, false /* force_create */); |
| 25 if (host) | 24 if (host) |
| 26 host->SendDestroyingVideoSurface(surface_id, done_cb); | 25 host->SendDestroyingVideoSurface(surface_id, done_cb); |
| 27 else | 26 else |
| 28 done_cb.Run(); | 27 done_cb.Run(); |
| 29 } | 28 } |
| 30 } // namespace | 29 } // namespace |
| 31 | 30 |
| 32 BrowserSurfaceViewManager::BrowserSurfaceViewManager( | 31 BrowserSurfaceViewManager::BrowserSurfaceViewManager( |
| 33 RenderFrameHost* render_frame_host) | 32 RenderFrameHost* render_frame_host) |
| 34 : render_frame_host_(render_frame_host), | 33 : render_frame_host_(render_frame_host), |
| 35 surface_id_(media::SurfaceManager::kNoSurfaceID) {} | 34 surface_id_(media::SurfaceManager::kNoSurfaceID) { |
| 35 video_surface_manager_ = VideoSurfaceManager::GetInstance(); |
| 36 } |
| 36 | 37 |
| 37 BrowserSurfaceViewManager::~BrowserSurfaceViewManager() {} | 38 BrowserSurfaceViewManager::~BrowserSurfaceViewManager() {} |
| 38 | 39 |
| 39 void BrowserSurfaceViewManager::SetVideoSurface(gl::ScopedJavaSurface surface) { | 40 void BrowserSurfaceViewManager::SetVideoSurface(gl::ScopedJavaSurface surface) { |
| 40 TRACE_EVENT0("media", "BrowserSurfaceViewManager::SetVideoSurface"); | 41 TRACE_EVENT0("media", "BrowserSurfaceViewManager::SetVideoSurface"); |
| 41 if (surface.IsEmpty()) { | 42 if (surface.IsEmpty()) { |
| 42 DCHECK_NE(surface_id_, media::SurfaceManager::kNoSurfaceID); | 43 DCHECK_NE(surface_id_, media::SurfaceManager::kNoSurfaceID); |
| 43 gpu::GpuSurfaceTracker::Get()->RemoveSurface(surface_id_); | 44 gpu::GpuSurfaceTracker::Get()->RemoveSurface(surface_id_); |
| 44 gpu::GpuSurfaceTracker::Get()->UnregisterViewSurface(surface_id_); | 45 gpu::GpuSurfaceTracker::Get()->UnregisterViewSurface(surface_id_); |
| 45 SendDestroyingVideoSurface(surface_id_); | 46 SendDestroyingVideoSurface(surface_id_); |
| 46 surface_id_ = media::SurfaceManager::kNoSurfaceID; | 47 surface_id_ = media::SurfaceManager::kNoSurfaceID; |
| 47 } else { | 48 } else { |
| 48 // We just use the surface tracker to allocate a surface id for us. The | 49 // We just use the surface tracker to allocate a surface id for us. The |
| 49 // lookup will go through the Android specific path and get the java | 50 // lookup will go through the Android specific path and get the java |
| 50 // surface directly, so there's no need to add a valid native widget here. | 51 // surface directly, so there's no need to add a valid native widget here. |
| 51 surface_id_ = gpu::GpuSurfaceTracker::Get()->AddSurfaceForNativeWidget( | 52 surface_id_ = gpu::GpuSurfaceTracker::Get()->AddSurfaceForNativeWidget( |
| 52 gfx::kNullAcceleratedWidget); | 53 gfx::kNullAcceleratedWidget); |
| 53 gpu::GpuSurfaceTracker::GetInstance()->RegisterViewSurface( | 54 gpu::GpuSurfaceTracker::GetInstance()->RegisterViewSurface( |
| 54 surface_id_, surface.j_surface().obj()); | 55 surface_id_, surface.j_surface().obj()); |
| 55 SendSurfaceID(surface_id_); | 56 SendSurfaceID(surface_id_); |
| 56 } | 57 } |
| 57 } | 58 } |
| 58 | 59 |
| 59 void BrowserSurfaceViewManager::DidExitFullscreen(bool release_media_player) { | 60 void BrowserSurfaceViewManager::DidExitFullscreen(bool release_media_player) { |
| 60 DVLOG(3) << __func__; | 61 DVLOG(3) << __func__; |
| 61 content_video_view_.reset(); | 62 video_surface_provider_ = nullptr; |
| 62 } | 63 } |
| 63 | 64 |
| 64 void BrowserSurfaceViewManager::OnCreateFullscreenSurface( | 65 void BrowserSurfaceViewManager::OnCreateFullscreenSurface( |
| 65 const gfx::Size& video_natural_size) { | 66 const gfx::Size& video_natural_size) { |
| 67 |
| 66 // It's valid to get this call if we already own the fullscreen view. We just | 68 // It's valid to get this call if we already own the fullscreen view. We just |
| 67 // return the existing surface id. | 69 // return the existing surface id. |
| 68 if (content_video_view_) { | 70 if (video_surface_provider_ != nullptr) { |
| 69 // Send the surface now if we have it. Otherwise it will be returned by | 71 // Send the surface now if we have it. Otherwise it will be returned by |
| 70 // |SetVideoSurface|. | 72 // |SetVideoSurface|. |
| 71 if (surface_id_ != media::SurfaceManager::kNoSurfaceID) { | 73 if (surface_id_ != media::SurfaceManager::kNoSurfaceID) { |
| 72 SendSurfaceID(surface_id_); | 74 SendSurfaceID(surface_id_); |
| 73 OnNaturalSizeChanged(video_natural_size); | 75 OnNaturalSizeChanged(video_natural_size); |
| 74 return; | 76 return; |
| 75 } | 77 } |
| 76 } | 78 } |
| 77 | 79 |
| 78 // If we don't own the fullscreen view, but one exists, it means another | |
| 79 // WebContents has it. Ignore this request and return a null surface id. | |
| 80 if (ContentVideoView::GetInstance()) { | |
| 81 SendSurfaceID(media::SurfaceManager::kNoSurfaceID); | |
| 82 return; | |
| 83 } | |
| 84 | |
| 85 WebContents* web_contents = | 80 WebContents* web_contents = |
| 86 WebContents::FromRenderFrameHost(render_frame_host_); | 81 WebContents::FromRenderFrameHost(render_frame_host_); |
| 87 if (!web_contents->GetDelegate()) | 82 if (!web_contents->GetDelegate()) |
| 88 return; | 83 return; |
| 89 ContentViewCore* cvc = ContentViewCore::FromWebContents(web_contents); | 84 |
| 90 content_video_view_.reset( | 85 video_surface_provider_ = video_surface_manager_->GetVideoSurfaceProvider( |
| 91 new ContentVideoView(this, cvc, | 86 web_contents); |
| 92 web_contents->GetDelegate()->GetContentVideoViewEmbedder(), | 87 // If we don't own the fullscreen view and we get back null from the surface |
| 93 video_natural_size)); | 88 // manager, assume another WebContents has it. Ignore this request and return |
| 89 // a null surface id. |
| 90 if (video_surface_provider_ == nullptr) { |
| 91 SendSurfaceID(media::SurfaceManager::kNoSurfaceID); |
| 92 return; |
| 93 } |
| 94 |
| 95 video_surface_provider_->CreateVideoSurface(this, video_natural_size); |
| 94 } | 96 } |
| 95 | 97 |
| 96 void BrowserSurfaceViewManager::OnNaturalSizeChanged(const gfx::Size& size) { | 98 void BrowserSurfaceViewManager::OnNaturalSizeChanged(const gfx::Size& size) { |
| 97 if (content_video_view_) | 99 if (video_surface_provider_ != nullptr) |
| 98 content_video_view_->OnVideoSizeChanged(size.width(), size.height()); | 100 video_surface_provider_->OnVideoSizeChanged(size.width(), size.height()); |
| 99 } | 101 } |
| 100 | 102 |
| 101 bool BrowserSurfaceViewManager::SendSurfaceID(int surface_id) { | 103 bool BrowserSurfaceViewManager::SendSurfaceID(int surface_id) { |
| 102 return render_frame_host_->Send( | 104 return render_frame_host_->Send( |
| 103 new SurfaceViewManagerMsg_FullscreenSurfaceCreated( | 105 new SurfaceViewManagerMsg_FullscreenSurfaceCreated( |
| 104 render_frame_host_->GetRoutingID(), surface_id)); | 106 render_frame_host_->GetRoutingID(), surface_id)); |
| 105 } | 107 } |
| 106 | 108 |
| 107 void BrowserSurfaceViewManager::SendDestroyingVideoSurface(int surface_id) { | 109 void BrowserSurfaceViewManager::SendDestroyingVideoSurface(int surface_id) { |
| 108 base::WaitableEvent waiter(base::WaitableEvent::ResetPolicy::AUTOMATIC, | 110 base::WaitableEvent waiter(base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| 109 base::WaitableEvent::InitialState::NOT_SIGNALED); | 111 base::WaitableEvent::InitialState::NOT_SIGNALED); |
| 110 // Unretained is okay because we're waiting on the callback. | 112 // Unretained is okay because we're waiting on the callback. |
| 111 if (BrowserThread::PostTask( | 113 if (BrowserThread::PostTask( |
| 112 BrowserThread::IO, FROM_HERE, | 114 BrowserThread::IO, FROM_HERE, |
| 113 base::Bind(&SendDestroyingVideoSurfaceOnIO, surface_id, | 115 base::Bind(&SendDestroyingVideoSurfaceOnIO, surface_id, |
| 114 base::Bind(&base::WaitableEvent::Signal, | 116 base::Bind(&base::WaitableEvent::Signal, |
| 115 base::Unretained(&waiter))))) { | 117 base::Unretained(&waiter))))) { |
| 116 base::ThreadRestrictions::ScopedAllowWait allow_wait; | 118 base::ThreadRestrictions::ScopedAllowWait allow_wait; |
| 117 waiter.Wait(); | 119 waiter.Wait(); |
| 118 } | 120 } |
| 119 } | 121 } |
| 120 | 122 |
| 121 } // namespace content | 123 } // namespace content |
| OLD | NEW |