Infobar material design refresh: bg color
[chromium-blink-merge.git] / content / app / android / child_process_service.cc
blobd3e6a1bf473883a57a4e69800e0b5721999096b5
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_impl.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/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;
28 namespace content {
30 namespace {
32 // TODO(sievers): Use two different implementations of this depending on if
33 // we're in a renderer or gpu process.
34 class SurfaceTextureManagerImpl : public SurfaceTextureManager,
35 public SurfaceTexturePeer,
36 public GpuSurfaceLookup {
37 public:
38 // |service| is the instance of
39 // org.chromium.content.app.ChildProcessService.
40 explicit SurfaceTextureManagerImpl(
41 const base::android::ScopedJavaLocalRef<jobject>& service)
42 : service_(service) {
43 SurfaceTexturePeer::InitInstance(this);
44 GpuSurfaceLookup::InitInstance(this);
46 ~SurfaceTextureManagerImpl() override {
47 SurfaceTexturePeer::InitInstance(NULL);
48 GpuSurfaceLookup::InitInstance(NULL);
51 // Overridden from SurfaceTextureManager:
52 void RegisterSurfaceTexture(int surface_texture_id,
53 int client_id,
54 gfx::SurfaceTexture* surface_texture) override {
55 JNIEnv* env = base::android::AttachCurrentThread();
56 Java_ChildProcessService_createSurfaceTextureSurface(
57 env,
58 service_.obj(),
59 surface_texture_id,
60 client_id,
61 surface_texture->j_surface_texture().obj());
63 void UnregisterSurfaceTexture(int surface_texture_id,
64 int client_id) override {
65 JNIEnv* env = base::android::AttachCurrentThread();
66 Java_ChildProcessService_destroySurfaceTextureSurface(
67 env, service_.obj(), surface_texture_id, client_id);
69 gfx::AcceleratedWidget AcquireNativeWidgetForSurfaceTexture(
70 int surface_texture_id) override {
71 JNIEnv* env = base::android::AttachCurrentThread();
72 gfx::ScopedJavaSurface surface(
73 Java_ChildProcessService_getSurfaceTextureSurface(env, service_.obj(),
74 surface_texture_id));
76 if (surface.j_surface().is_null())
77 return NULL;
79 // Note: This ensures that any local references used by
80 // ANativeWindow_fromSurface are released immediately. This is needed as a
81 // workaround for https://code.google.com/p/android/issues/detail?id=68174
82 base::android::ScopedJavaLocalFrame scoped_local_reference_frame(env);
83 ANativeWindow* native_window =
84 ANativeWindow_fromSurface(env, surface.j_surface().obj());
86 return native_window;
89 // Overridden from SurfaceTexturePeer:
90 void EstablishSurfaceTexturePeer(
91 base::ProcessHandle pid,
92 scoped_refptr<gfx::SurfaceTexture> surface_texture,
93 int primary_id,
94 int secondary_id) override {
95 JNIEnv* env = base::android::AttachCurrentThread();
96 content::Java_ChildProcessService_establishSurfaceTexturePeer(
97 env,
98 service_.obj(),
99 pid,
100 surface_texture->j_surface_texture().obj(),
101 primary_id,
102 secondary_id);
105 // Overridden from GpuSurfaceLookup:
106 gfx::AcceleratedWidget AcquireNativeWidget(int surface_id) override {
107 JNIEnv* env = base::android::AttachCurrentThread();
108 gfx::ScopedJavaSurface surface(
109 content::Java_ChildProcessService_getViewSurface(
110 env, service_.obj(), surface_id));
112 if (surface.j_surface().is_null())
113 return NULL;
115 // Note: This ensures that any local references used by
116 // ANativeWindow_fromSurface are released immediately. This is needed as a
117 // workaround for https://code.google.com/p/android/issues/detail?id=68174
118 base::android::ScopedJavaLocalFrame scoped_local_reference_frame(env);
119 ANativeWindow* native_window =
120 ANativeWindow_fromSurface(env, surface.j_surface().obj());
122 return native_window;
125 private:
126 // The instance of org.chromium.content.app.ChildProcessService.
127 base::android::ScopedJavaGlobalRef<jobject> service_;
129 DISALLOW_COPY_AND_ASSIGN(SurfaceTextureManagerImpl);
132 // Chrome actually uses the renderer code path for all of its child
133 // processes such as renderers, plugins, etc.
134 void InternalInitChildProcess(JNIEnv* env,
135 jclass clazz,
136 jobject service_in,
137 jint cpu_count,
138 jlong cpu_features) {
139 base::android::ScopedJavaLocalRef<jobject> service(
140 env, env->NewLocalRef(service_in));
142 // Set the CPU properties.
143 android_setCpu(cpu_count, cpu_features);
144 SurfaceTextureManager::SetInstance(new SurfaceTextureManagerImpl(service));
146 base::android::MemoryPressureListenerAndroid::RegisterSystemCallback(env);
149 } // namespace <anonymous>
151 void RegisterGlobalFileDescriptor(JNIEnv* env,
152 jclass clazz,
153 jint id,
154 jint fd,
155 jlong offset,
156 jlong size) {
157 base::MemoryMappedFile::Region region = {offset, size};
158 base::GlobalDescriptors::GetInstance()->Set(id, fd, region);
161 void InitChildProcess(JNIEnv* env,
162 jclass clazz,
163 jobject service,
164 jint cpu_count,
165 jlong cpu_features) {
166 InternalInitChildProcess(env, clazz, service, cpu_count, cpu_features);
169 void ExitChildProcess(JNIEnv* env, jclass clazz) {
170 VLOG(0) << "ChildProcessService: Exiting child process.";
171 base::android::LibraryLoaderExitHook();
172 _exit(0);
175 bool RegisterChildProcessService(JNIEnv* env) {
176 return RegisterNativesImpl(env);
179 void ShutdownMainThread(JNIEnv* env, jobject obj) {
180 ChildThreadImpl::ShutdownThread();
183 } // namespace content