1 // Copyright (c) 2012 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/app/android/child_process_service.h"
7 #include <android/native_window_jni.h>
8 #include <cpu-features.h>
10 #include "base/android/jni_array.h"
11 #include "base/android/library_loader/library_loader_hooks.h"
12 #include "base/android/memory_pressure_listener_android.h"
13 #include "base/logging.h"
14 #include "base/posix/global_descriptors.h"
15 #include "content/child/child_thread.h"
16 #include "content/common/android/surface_texture_manager.h"
17 #include "content/common/android/surface_texture_peer.h"
18 #include "content/common/gpu/gpu_surface_lookup.h"
19 #include "content/public/app/android_library_loader_hooks.h"
20 #include "content/public/common/content_descriptors.h"
21 #include "ipc/ipc_descriptors.h"
22 #include "jni/ChildProcessService_jni.h"
23 #include "ui/gl/android/scoped_java_surface.h"
25 using base::android::AttachCurrentThread;
26 using base::android::CheckException;
27 using base::android::JavaIntArrayToIntVector;
33 // TODO(sievers): Use two different implementations of this depending on if
34 // we're in a renderer or gpu process.
35 class SurfaceTextureManagerImpl : public SurfaceTextureManager,
36 public SurfaceTexturePeer,
37 public GpuSurfaceLookup {
39 // |service| is the instance of
40 // org.chromium.content.app.ChildProcessService.
41 explicit SurfaceTextureManagerImpl(
42 const base::android::ScopedJavaLocalRef<jobject>& service)
44 SurfaceTexturePeer::InitInstance(this);
45 GpuSurfaceLookup::InitInstance(this);
47 virtual ~SurfaceTextureManagerImpl() {
48 SurfaceTexturePeer::InitInstance(NULL);
49 GpuSurfaceLookup::InitInstance(NULL);
52 // Overridden from SurfaceTextureManager:
53 virtual void RegisterSurfaceTexture(
54 int surface_texture_id,
56 gfx::SurfaceTexture* surface_texture) override {
57 JNIEnv* env = base::android::AttachCurrentThread();
58 Java_ChildProcessService_createSurfaceTextureSurface(
63 surface_texture->j_surface_texture().obj());
65 virtual void UnregisterSurfaceTexture(int surface_texture_id,
66 int client_id) override {
67 JNIEnv* env = base::android::AttachCurrentThread();
68 Java_ChildProcessService_destroySurfaceTextureSurface(
69 env, service_.obj(), surface_texture_id, client_id);
71 virtual gfx::AcceleratedWidget AcquireNativeWidgetForSurfaceTexture(
72 int surface_texture_id) override {
73 JNIEnv* env = base::android::AttachCurrentThread();
74 gfx::ScopedJavaSurface surface(
75 Java_ChildProcessService_getSurfaceTextureSurface(env, service_.obj(),
78 if (surface.j_surface().is_null())
81 // Note: This ensures that any local references used by
82 // ANativeWindow_fromSurface are released immediately. This is needed as a
83 // workaround for https://code.google.com/p/android/issues/detail?id=68174
84 base::android::ScopedJavaLocalFrame scoped_local_reference_frame(env);
85 ANativeWindow* native_window =
86 ANativeWindow_fromSurface(env, surface.j_surface().obj());
91 // Overridden from SurfaceTexturePeer:
92 virtual void EstablishSurfaceTexturePeer(
93 base::ProcessHandle pid,
94 scoped_refptr<gfx::SurfaceTexture> surface_texture,
96 int secondary_id) override {
97 JNIEnv* env = base::android::AttachCurrentThread();
98 content::Java_ChildProcessService_establishSurfaceTexturePeer(
102 surface_texture->j_surface_texture().obj(),
107 // Overridden from GpuSurfaceLookup:
108 virtual gfx::AcceleratedWidget AcquireNativeWidget(int surface_id) override {
109 JNIEnv* env = base::android::AttachCurrentThread();
110 gfx::ScopedJavaSurface surface(
111 content::Java_ChildProcessService_getViewSurface(
112 env, service_.obj(), surface_id));
114 if (surface.j_surface().is_null())
117 // Note: This ensures that any local references used by
118 // ANativeWindow_fromSurface are released immediately. This is needed as a
119 // workaround for https://code.google.com/p/android/issues/detail?id=68174
120 base::android::ScopedJavaLocalFrame scoped_local_reference_frame(env);
121 ANativeWindow* native_window =
122 ANativeWindow_fromSurface(env, surface.j_surface().obj());
124 return native_window;
128 // The instance of org.chromium.content.app.ChildProcessService.
129 base::android::ScopedJavaGlobalRef<jobject> service_;
131 DISALLOW_COPY_AND_ASSIGN(SurfaceTextureManagerImpl);
134 // Chrome actually uses the renderer code path for all of its child
135 // processes such as renderers, plugins, etc.
136 void InternalInitChildProcess(const std::vector<int>& file_ids,
137 const std::vector<int>& file_fds,
143 jlong cpu_features) {
144 base::android::ScopedJavaLocalRef<jobject> service(env, service_in);
146 // Set the CPU properties.
147 android_setCpu(cpu_count, cpu_features);
148 // Register the file descriptors.
149 // This includes the IPC channel, the crash dump signals and resource related
151 DCHECK(file_fds.size() == file_ids.size());
152 for (size_t i = 0; i < file_ids.size(); ++i)
153 base::GlobalDescriptors::GetInstance()->Set(file_ids[i], file_fds[i]);
155 SurfaceTextureManager::InitInstance(new SurfaceTextureManagerImpl(service));
157 base::android::MemoryPressureListenerAndroid::RegisterSystemCallback(env);
160 } // namespace <anonymous>
162 void InitChildProcess(JNIEnv* env,
166 jintArray j_file_ids,
167 jintArray j_file_fds,
169 jlong cpu_features) {
170 std::vector<int> file_ids;
171 std::vector<int> file_fds;
172 JavaIntArrayToIntVector(env, j_file_ids, &file_ids);
173 JavaIntArrayToIntVector(env, j_file_fds, &file_fds);
175 InternalInitChildProcess(
176 file_ids, file_fds, env, clazz, context, service,
177 cpu_count, cpu_features);
180 void ExitChildProcess(JNIEnv* env, jclass clazz) {
181 VLOG(0) << "ChildProcessService: Exiting child process.";
182 base::android::LibraryLoaderExitHook();
186 bool RegisterChildProcessService(JNIEnv* env) {
187 return RegisterNativesImpl(env);
190 void ShutdownMainThread(JNIEnv* env, jobject obj) {
191 ChildThread::ShutdownThread();
194 } // namespace content