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"
21 BrowserSurfaceTextureManager
* BrowserSurfaceTextureManager::GetInstance() {
22 return Singleton
<BrowserSurfaceTextureManager
,
23 LeakySingletonTraits
<BrowserSurfaceTextureManager
>>::get();
26 void BrowserSurfaceTextureManager::RegisterSurfaceTexture(
27 int surface_texture_id
,
29 gfx::SurfaceTexture
* surface_texture
) {
30 content::CreateSurfaceTextureSurface(
31 surface_texture_id
, client_id
, surface_texture
);
34 void BrowserSurfaceTextureManager::UnregisterSurfaceTexture(
35 int surface_texture_id
,
37 content::DestroySurfaceTextureSurface(surface_texture_id
, client_id
);
40 gfx::AcceleratedWidget
41 BrowserSurfaceTextureManager::AcquireNativeWidgetForSurfaceTexture(
42 int surface_texture_id
) {
43 gfx::ScopedJavaSurface
surface(
44 content::GetSurfaceTextureSurface(
46 BrowserGpuChannelHostFactory::instance()->GetGpuChannelId()));
47 if (surface
.j_surface().is_null())
50 JNIEnv
* env
= base::android::AttachCurrentThread();
51 // Note: This ensures that any local references used by
52 // ANativeWindow_fromSurface are released immediately. This is needed as a
53 // workaround for https://code.google.com/p/android/issues/detail?id=68174
54 base::android::ScopedJavaLocalFrame
scoped_local_reference_frame(env
);
55 ANativeWindow
* native_window
=
56 ANativeWindow_fromSurface(env
, surface
.j_surface().obj());
61 void BrowserSurfaceTextureManager::EstablishSurfaceTexturePeer(
62 base::ProcessHandle render_process_handle
,
63 scoped_refptr
<gfx::SurfaceTexture
> surface_texture
,
66 if (!surface_texture
.get())
69 BrowserThread::PostTask(
70 BrowserThread::UI
, FROM_HERE
,
71 base::Bind(&BrowserMediaPlayerManager::SetSurfacePeer
, surface_texture
,
72 render_process_handle
, render_frame_id
, player_id
));
75 BrowserSurfaceTextureManager::BrowserSurfaceTextureManager() {
76 SurfaceTexturePeer::InitInstance(this);
79 BrowserSurfaceTextureManager::~BrowserSurfaceTextureManager() {
80 SurfaceTexturePeer::InitInstance(nullptr);
83 } // namespace content