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_peer.h"
17 #include "content/common/gpu/gpu_surface_lookup.h"
18 #include "content/public/app/android_library_loader_hooks.h"
19 #include "content/public/common/content_descriptors.h"
20 #include "ipc/ipc_descriptors.h"
21 #include "jni/ChildProcessService_jni.h"
22 #include "ui/gl/android/scoped_java_surface.h"
24 using base::android::AttachCurrentThread;
25 using base::android::CheckException;
26 using base::android::JavaIntArrayToIntVector;
32 class SurfaceTexturePeerChildImpl : public content::SurfaceTexturePeer,
33 public content::GpuSurfaceLookup {
35 // |service| is the instance of
36 // org.chromium.content.app.ChildProcessService.
37 explicit SurfaceTexturePeerChildImpl(
38 const base::android::ScopedJavaLocalRef<jobject>& service)
40 GpuSurfaceLookup::InitInstance(this);
43 virtual ~SurfaceTexturePeerChildImpl() {
44 GpuSurfaceLookup::InitInstance(NULL);
47 virtual void EstablishSurfaceTexturePeer(
48 base::ProcessHandle pid,
49 scoped_refptr<gfx::SurfaceTexture> surface_texture,
51 int secondary_id) OVERRIDE {
52 JNIEnv* env = base::android::AttachCurrentThread();
53 content::Java_ChildProcessService_establishSurfaceTexturePeer(
54 env, service_.obj(), pid,
55 surface_texture->j_surface_texture().obj(), primary_id,
60 virtual gfx::AcceleratedWidget AcquireNativeWidget(int surface_id) OVERRIDE {
61 JNIEnv* env = base::android::AttachCurrentThread();
62 gfx::ScopedJavaSurface surface(
63 content::Java_ChildProcessService_getViewSurface(
64 env, service_.obj(), surface_id));
66 if (surface.j_surface().is_null())
69 ANativeWindow* native_window = ANativeWindow_fromSurface(
70 env, surface.j_surface().obj());
76 // The instance of org.chromium.content.app.ChildProcessService.
77 base::android::ScopedJavaGlobalRef<jobject> service_;
79 DISALLOW_COPY_AND_ASSIGN(SurfaceTexturePeerChildImpl);
82 // Chrome actually uses the renderer code path for all of its child
83 // processes such as renderers, plugins, etc.
84 void InternalInitChildProcess(const std::vector<int>& file_ids,
85 const std::vector<int>& file_fds,
92 base::android::ScopedJavaLocalRef<jobject> service(env, service_in);
94 // Set the CPU properties.
95 android_setCpu(cpu_count, cpu_features);
96 // Register the file descriptors.
97 // This includes the IPC channel, the crash dump signals and resource related
99 DCHECK(file_fds.size() == file_ids.size());
100 for (size_t i = 0; i < file_ids.size(); ++i)
101 base::GlobalDescriptors::GetInstance()->Set(file_ids[i], file_fds[i]);
103 content::SurfaceTexturePeer::InitInstance(
104 new SurfaceTexturePeerChildImpl(service));
106 base::android::MemoryPressureListenerAndroid::RegisterSystemCallback(env);
109 } // namespace <anonymous>
111 void InitChildProcess(JNIEnv* env,
115 jintArray j_file_ids,
116 jintArray j_file_fds,
118 jlong cpu_features) {
119 std::vector<int> file_ids;
120 std::vector<int> file_fds;
121 JavaIntArrayToIntVector(env, j_file_ids, &file_ids);
122 JavaIntArrayToIntVector(env, j_file_fds, &file_fds);
124 InternalInitChildProcess(
125 file_ids, file_fds, env, clazz, context, service,
126 cpu_count, cpu_features);
129 void ExitChildProcess(JNIEnv* env, jclass clazz) {
130 VLOG(0) << "ChildProcessService: Exiting child process.";
131 base::android::LibraryLoaderExitHook();
135 bool RegisterChildProcessService(JNIEnv* env) {
136 return RegisterNativesImpl(env);
139 void ShutdownMainThread(JNIEnv* env, jobject obj) {
140 ChildThread::ShutdownThread();
143 } // namespace content