chrome/browser/extensions: Remove use of MessageLoopProxy and deprecated MessageLoop...
[chromium-blink-merge.git] / content / browser / android / browser_surface_texture_manager.cc
blob93c10f13ff2e70ad97fca1eb9ae4b41f8babbe08
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
3 // found in the LICENSE file.
5 #include "content/browser/android/browser_surface_texture_manager.h"
7 #include <android/native_window_jni.h>
9 #include "base/android/jni_android.h"
10 #include "content/browser/android/child_process_launcher_android.h"
11 #include "content/browser/frame_host/render_frame_host_impl.h"
12 #include "content/browser/gpu/browser_gpu_channel_host_factory.h"
13 #include "content/browser/media/android/browser_media_player_manager.h"
14 #include "content/browser/media/media_web_contents_observer.h"
15 #include "content/browser/web_contents/web_contents_impl.h"
16 #include "content/public/browser/browser_thread.h"
17 #include "content/public/browser/render_process_host.h"
18 #include "media/base/android/media_player_android.h"
19 #include "ui/gl/android/scoped_java_surface.h"
20 #include "ui/gl/android/surface_texture.h"
22 namespace content {
23 namespace {
25 // Pass a java surface object to the MediaPlayerAndroid object
26 // identified by render process handle, render frame ID and player ID.
27 static void SetSurfacePeer(scoped_refptr<gfx::SurfaceTexture> surface_texture,
28 base::ProcessHandle render_process_handle,
29 int render_frame_id,
30 int player_id) {
31 int render_process_id = 0;
32 RenderProcessHost::iterator it = RenderProcessHost::AllHostsIterator();
33 while (!it.IsAtEnd()) {
34 if (it.GetCurrentValue()->GetHandle() == render_process_handle) {
35 render_process_id = it.GetCurrentValue()->GetID();
36 break;
38 it.Advance();
40 if (!render_process_id) {
41 DVLOG(1) << "Cannot find render process for render_process_handle "
42 << render_process_handle;
43 return;
46 RenderFrameHostImpl* frame =
47 RenderFrameHostImpl::FromID(render_process_id, render_frame_id);
48 if (!frame) {
49 DVLOG(1) << "Cannot find frame for render_frame_id " << render_frame_id;
50 return;
53 WebContentsImpl* web_contents =
54 static_cast<WebContentsImpl*>(WebContents::FromRenderFrameHost(frame));
55 BrowserMediaPlayerManager* player_manager =
56 web_contents->media_web_contents_observer()->GetMediaPlayerManager(frame);
57 if (!player_manager) {
58 DVLOG(1) << "Cannot find the media player manager for frame " << frame;
59 return;
62 media::MediaPlayerAndroid* player = player_manager->GetPlayer(player_id);
63 if (!player) {
64 DVLOG(1) << "Cannot find media player for player_id " << player_id;
65 return;
68 if (player != player_manager->GetFullscreenPlayer()) {
69 gfx::ScopedJavaSurface scoped_surface(surface_texture.get());
70 player->SetVideoSurface(scoped_surface.Pass());
74 } // namespace
76 // static
77 BrowserSurfaceTextureManager* BrowserSurfaceTextureManager::GetInstance() {
78 return Singleton<BrowserSurfaceTextureManager,
79 LeakySingletonTraits<BrowserSurfaceTextureManager>>::get();
82 void BrowserSurfaceTextureManager::RegisterSurfaceTexture(
83 int surface_texture_id,
84 int client_id,
85 gfx::SurfaceTexture* surface_texture) {
86 content::CreateSurfaceTextureSurface(
87 surface_texture_id, client_id, surface_texture);
90 void BrowserSurfaceTextureManager::UnregisterSurfaceTexture(
91 int surface_texture_id,
92 int client_id) {
93 content::DestroySurfaceTextureSurface(surface_texture_id, client_id);
96 gfx::AcceleratedWidget
97 BrowserSurfaceTextureManager::AcquireNativeWidgetForSurfaceTexture(
98 int surface_texture_id) {
99 gfx::ScopedJavaSurface surface(
100 content::GetSurfaceTextureSurface(
101 surface_texture_id,
102 BrowserGpuChannelHostFactory::instance()->GetGpuChannelId()));
103 if (surface.j_surface().is_null())
104 return NULL;
106 JNIEnv* env = base::android::AttachCurrentThread();
107 // Note: This ensures that any local references used by
108 // ANativeWindow_fromSurface are released immediately. This is needed as a
109 // workaround for https://code.google.com/p/android/issues/detail?id=68174
110 base::android::ScopedJavaLocalFrame scoped_local_reference_frame(env);
111 ANativeWindow* native_window =
112 ANativeWindow_fromSurface(env, surface.j_surface().obj());
114 return native_window;
117 void BrowserSurfaceTextureManager::EstablishSurfaceTexturePeer(
118 base::ProcessHandle render_process_handle,
119 scoped_refptr<gfx::SurfaceTexture> surface_texture,
120 int render_frame_id,
121 int player_id) {
122 if (!surface_texture.get())
123 return;
125 BrowserThread::PostTask(BrowserThread::UI,
126 FROM_HERE,
127 base::Bind(&SetSurfacePeer,
128 surface_texture,
129 render_process_handle,
130 render_frame_id,
131 player_id));
134 BrowserSurfaceTextureManager::BrowserSurfaceTextureManager() {
135 SurfaceTexturePeer::InitInstance(this);
138 BrowserSurfaceTextureManager::~BrowserSurfaceTextureManager() {
139 SurfaceTexturePeer::InitInstance(nullptr);
142 } // namespace content