Add remoting and PPAPI tests to GN build
[chromium-blink-merge.git] / content / browser / android / browser_surface_texture_manager.cc
blob832036b1864ef4af1bbc42aaef48ae9ab8536a9b
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 BrowserSurfaceTextureManager::BrowserSurfaceTextureManager() {
77 SurfaceTexturePeer::InitInstance(this);
80 BrowserSurfaceTextureManager::~BrowserSurfaceTextureManager() {
81 SurfaceTexturePeer::InitInstance(NULL);
84 void BrowserSurfaceTextureManager::RegisterSurfaceTexture(
85 int surface_texture_id,
86 int client_id,
87 gfx::SurfaceTexture* surface_texture) {
88 content::CreateSurfaceTextureSurface(
89 surface_texture_id, client_id, surface_texture);
92 void BrowserSurfaceTextureManager::UnregisterSurfaceTexture(
93 int surface_texture_id,
94 int client_id) {
95 content::DestroySurfaceTextureSurface(surface_texture_id, client_id);
98 gfx::AcceleratedWidget
99 BrowserSurfaceTextureManager::AcquireNativeWidgetForSurfaceTexture(
100 int surface_texture_id) {
101 gfx::ScopedJavaSurface surface(
102 content::GetSurfaceTextureSurface(
103 surface_texture_id,
104 BrowserGpuChannelHostFactory::instance()->GetGpuChannelId()));
105 if (surface.j_surface().is_null())
106 return NULL;
108 JNIEnv* env = base::android::AttachCurrentThread();
109 // Note: This ensures that any local references used by
110 // ANativeWindow_fromSurface are released immediately. This is needed as a
111 // workaround for https://code.google.com/p/android/issues/detail?id=68174
112 base::android::ScopedJavaLocalFrame scoped_local_reference_frame(env);
113 ANativeWindow* native_window =
114 ANativeWindow_fromSurface(env, surface.j_surface().obj());
116 return native_window;
119 void BrowserSurfaceTextureManager::EstablishSurfaceTexturePeer(
120 base::ProcessHandle render_process_handle,
121 scoped_refptr<gfx::SurfaceTexture> surface_texture,
122 int render_frame_id,
123 int player_id) {
124 if (!surface_texture.get())
125 return;
127 BrowserThread::PostTask(BrowserThread::UI,
128 FROM_HERE,
129 base::Bind(&SetSurfacePeer,
130 surface_texture,
131 render_process_handle,
132 render_frame_id,
133 player_id));
136 } // namespace content