summaryrefslogtreecommitdiff
path: root/chromium/content/app/android/child_process_service.cc
blob: e44974535dd7fd920a9aa95584ce92bbc0f630dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/app/android/child_process_service.h"

#include <android/native_window_jni.h>
#include <cpu-features.h>

#include "base/android/jni_array.h"
#include "base/android/library_loader/library_loader_hooks.h"
#include "base/android/memory_pressure_listener_android.h"
#include "base/logging.h"
#include "base/macros.h"
#include "base/posix/global_descriptors.h"
#include "content/child/child_thread_impl.h"
#include "content/public/common/content_descriptors.h"
#include "gpu/ipc/common/android/surface_texture_manager.h"
#include "gpu/ipc/common/android/surface_texture_peer.h"
#include "gpu/ipc/common/gpu_surface_lookup.h"
#include "ipc/ipc_descriptors.h"
#include "jni/ChildProcessService_jni.h"
#include "ui/gl/android/scoped_java_surface.h"

using base::android::AttachCurrentThread;
using base::android::CheckException;
using base::android::JavaIntArrayToIntVector;

namespace content {

namespace {

// TODO(sievers): Use two different implementations of this depending on if
// we're in a renderer or gpu process.
class SurfaceTextureManagerImpl : public gpu::SurfaceTextureManager,
                                  public gpu::SurfaceTexturePeer,
                                  public gpu::GpuSurfaceLookup {
 public:
  // |service| is the instance of
  // org.chromium.content.app.ChildProcessService.
  explicit SurfaceTextureManagerImpl(
      const base::android::JavaRef<jobject>& service)
      : service_(service) {
    SurfaceTexturePeer::InitInstance(this);
    gpu::GpuSurfaceLookup::InitInstance(this);
  }
  ~SurfaceTextureManagerImpl() override {
    SurfaceTexturePeer::InitInstance(NULL);
    gpu::GpuSurfaceLookup::InitInstance(NULL);
  }

  // Overridden from SurfaceTextureManager:
  void RegisterSurfaceTexture(int surface_texture_id,
                              int client_id,
                              gfx::SurfaceTexture* surface_texture) override {
    JNIEnv* env = base::android::AttachCurrentThread();
    Java_ChildProcessService_createSurfaceTextureSurface(
        env,
        service_.obj(),
        surface_texture_id,
        client_id,
        surface_texture->j_surface_texture().obj());
  }
  void UnregisterSurfaceTexture(int surface_texture_id,
                                int client_id) override {
    JNIEnv* env = base::android::AttachCurrentThread();
    Java_ChildProcessService_destroySurfaceTextureSurface(
        env, service_.obj(), surface_texture_id, client_id);
  }
  gfx::AcceleratedWidget AcquireNativeWidgetForSurfaceTexture(
      int surface_texture_id) override {
    JNIEnv* env = base::android::AttachCurrentThread();
    gfx::ScopedJavaSurface surface(
        Java_ChildProcessService_getSurfaceTextureSurface(env, service_.obj(),
                                                          surface_texture_id));

    if (surface.j_surface().is_null())
      return NULL;

    // Note: This ensures that any local references used by
    // ANativeWindow_fromSurface are released immediately. This is needed as a
    // workaround for https://code.google.com/p/android/issues/detail?id=68174
    base::android::ScopedJavaLocalFrame scoped_local_reference_frame(env);
    ANativeWindow* native_window =
        ANativeWindow_fromSurface(env, surface.j_surface().obj());

    return native_window;
  }

  // Overridden from SurfaceTexturePeer:
  void EstablishSurfaceTexturePeer(
      base::ProcessHandle pid,
      scoped_refptr<gfx::SurfaceTexture> surface_texture,
      int primary_id,
      int secondary_id) override {
    JNIEnv* env = base::android::AttachCurrentThread();
    content::Java_ChildProcessService_establishSurfaceTexturePeer(
        env,
        service_.obj(),
        pid,
        surface_texture->j_surface_texture().obj(),
        primary_id,
        secondary_id);
  }

  // Overridden from GpuSurfaceLookup:
  gfx::AcceleratedWidget AcquireNativeWidget(int surface_id) override {
    JNIEnv* env = base::android::AttachCurrentThread();
    gfx::ScopedJavaSurface surface(
        content::Java_ChildProcessService_getViewSurface(
            env, service_.obj(), surface_id));

    if (surface.j_surface().is_null())
      return NULL;

    // Note: This ensures that any local references used by
    // ANativeWindow_fromSurface are released immediately. This is needed as a
    // workaround for https://code.google.com/p/android/issues/detail?id=68174
    base::android::ScopedJavaLocalFrame scoped_local_reference_frame(env);
    ANativeWindow* native_window =
        ANativeWindow_fromSurface(env, surface.j_surface().obj());

    return native_window;
  }

  // Overridden from GpuSurfaceLookup:
  gfx::ScopedJavaSurface AcquireJavaSurface(int surface_id) override {
    JNIEnv* env = base::android::AttachCurrentThread();
    return gfx::ScopedJavaSurface(
        content::Java_ChildProcessService_getViewSurface(env, service_.obj(),
                                                         surface_id));
  }

 private:
  // The instance of org.chromium.content.app.ChildProcessService.
  base::android::ScopedJavaGlobalRef<jobject> service_;

  DISALLOW_COPY_AND_ASSIGN(SurfaceTextureManagerImpl);
};

// Chrome actually uses the renderer code path for all of its child
// processes such as renderers, plugins, etc.
void InternalInitChildProcess(JNIEnv* env,
                              const JavaParamRef<jobject>& service,
                              jint cpu_count,
                              jlong cpu_features) {
  // Set the CPU properties.
  android_setCpu(cpu_count, cpu_features);
  gpu::SurfaceTextureManager::SetInstance(
      new SurfaceTextureManagerImpl(service));

  base::android::MemoryPressureListenerAndroid::RegisterSystemCallback(env);
}

}  // namespace <anonymous>

void RegisterGlobalFileDescriptor(JNIEnv* env,
                                  const JavaParamRef<jclass>& clazz,
                                  jint id,
                                  jint fd,
                                  jlong offset,
                                  jlong size) {
  base::MemoryMappedFile::Region region = {offset, size};
  base::GlobalDescriptors::GetInstance()->Set(id, fd, region);
}

void InitChildProcess(JNIEnv* env,
                      const JavaParamRef<jclass>& clazz,
                      const JavaParamRef<jobject>& service,
                      jint cpu_count,
                      jlong cpu_features) {
  InternalInitChildProcess(env, service, cpu_count, cpu_features);
}

void ExitChildProcess(JNIEnv* env, const JavaParamRef<jclass>& clazz) {
  VLOG(0) << "ChildProcessService: Exiting child process.";
  base::android::LibraryLoaderExitHook();
  _exit(0);
}

bool RegisterChildProcessService(JNIEnv* env) {
  return RegisterNativesImpl(env);
}

void ShutdownMainThread(JNIEnv* env, const JavaParamRef<jobject>& obj) {
  ChildThreadImpl::ShutdownThread();
}

}  // namespace content