Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / browser / android / browser_surface_texture_manager.cc
blob2311994db4492cd51bd6f51789a736199fd0538d
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/public/browser/browser_thread.h"
15 #include "ui/gl/android/scoped_java_surface.h"
16 #include "ui/gl/android/surface_texture.h"
18 namespace content {
20 // static
21 BrowserSurfaceTextureManager* BrowserSurfaceTextureManager::GetInstance() {
22 return base::Singleton<
23 BrowserSurfaceTextureManager,
24 base::LeakySingletonTraits<BrowserSurfaceTextureManager>>::get();
27 void BrowserSurfaceTextureManager::RegisterSurfaceTexture(
28 int surface_texture_id,
29 int client_id,
30 gfx::SurfaceTexture* surface_texture) {
31 content::CreateSurfaceTextureSurface(
32 surface_texture_id, client_id, surface_texture);
35 void BrowserSurfaceTextureManager::UnregisterSurfaceTexture(
36 int surface_texture_id,
37 int client_id) {
38 content::DestroySurfaceTextureSurface(surface_texture_id, client_id);
41 gfx::AcceleratedWidget
42 BrowserSurfaceTextureManager::AcquireNativeWidgetForSurfaceTexture(
43 int surface_texture_id) {
44 gfx::ScopedJavaSurface surface(
45 content::GetSurfaceTextureSurface(
46 surface_texture_id,
47 BrowserGpuChannelHostFactory::instance()->GetGpuChannelId()));
48 if (surface.j_surface().is_null())
49 return NULL;
51 JNIEnv* env = base::android::AttachCurrentThread();
52 // Note: This ensures that any local references used by
53 // ANativeWindow_fromSurface are released immediately. This is needed as a
54 // workaround for https://code.google.com/p/android/issues/detail?id=68174
55 base::android::ScopedJavaLocalFrame scoped_local_reference_frame(env);
56 ANativeWindow* native_window =
57 ANativeWindow_fromSurface(env, surface.j_surface().obj());
59 return native_window;
62 void BrowserSurfaceTextureManager::EstablishSurfaceTexturePeer(
63 base::ProcessHandle render_process_handle,
64 scoped_refptr<gfx::SurfaceTexture> surface_texture,
65 int render_frame_id,
66 int player_id) {
67 if (!surface_texture.get())
68 return;
70 BrowserThread::PostTask(
71 BrowserThread::UI, FROM_HERE,
72 base::Bind(&BrowserMediaPlayerManager::SetSurfacePeer, surface_texture,
73 render_process_handle, render_frame_id, player_id));
76 BrowserSurfaceTextureManager::BrowserSurfaceTextureManager() {
77 SurfaceTexturePeer::InitInstance(this);
80 BrowserSurfaceTextureManager::~BrowserSurfaceTextureManager() {
81 SurfaceTexturePeer::InitInstance(nullptr);
84 } // namespace content