1 // Copyright 2013 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.
4
5 #include "ui/gl/android/surface_texture.h"
6
7 #include <android/native_window_jni.h>
8
9 // TODO(boliu): Remove this include when we move off ICS.
10 #include "base/android/build_info.h"
11 #include "base/android/jni_android.h"
12 #include "base/logging.h"
13 #include "jni/SurfaceTexturePlatformWrapper_jni.h"
14 #include "ui/gl/android/scoped_java_surface.h"
15 #include "ui/gl/android/surface_texture_listener.h"
16 #include "ui/gl/gl_bindings.h"
17
18 // TODO(boliu): Remove this method when when we move off ICS. See
19 // http://crbug.com/161864.
GlContextMethodsAvailable()20 bool GlContextMethodsAvailable() {
21 bool available = base::android::BuildInfo::GetInstance()->sdk_int() >= 16;
22 if (!available)
23 LOG(WARNING) << "Running on unsupported device: rendering may not work";
24 return available;
25 }
26
27 namespace gfx {
28
Create(int texture_id)29 scoped_refptr<SurfaceTexture> SurfaceTexture::Create(int texture_id) {
30 JNIEnv* env = base::android::AttachCurrentThread();
31 return new SurfaceTexture(
32 Java_SurfaceTexturePlatformWrapper_create(env, texture_id));
33 }
34
CreateSingleBuffered(int texture_id)35 scoped_refptr<SurfaceTexture> SurfaceTexture::CreateSingleBuffered(
36 int texture_id) {
37 DCHECK(IsSingleBufferModeSupported());
38 JNIEnv* env = base::android::AttachCurrentThread();
39 return new SurfaceTexture(
40 Java_SurfaceTexturePlatformWrapper_createSingleBuffered(env, texture_id));
41 }
42
SurfaceTexture(const base::android::ScopedJavaLocalRef<jobject> & j_surface_texture)43 SurfaceTexture::SurfaceTexture(
44 const base::android::ScopedJavaLocalRef<jobject>& j_surface_texture) {
45 j_surface_texture_.Reset(j_surface_texture);
46 }
47
~SurfaceTexture()48 SurfaceTexture::~SurfaceTexture() {
49 JNIEnv* env = base::android::AttachCurrentThread();
50 Java_SurfaceTexturePlatformWrapper_destroy(env, j_surface_texture_.obj());
51 }
52
SetFrameAvailableCallback(const base::Closure & callback)53 void SurfaceTexture::SetFrameAvailableCallback(
54 const base::Closure& callback) {
55 JNIEnv* env = base::android::AttachCurrentThread();
56 Java_SurfaceTexturePlatformWrapper_setFrameAvailableCallback(
57 env,
58 j_surface_texture_.obj(),
59 reinterpret_cast<intptr_t>(new SurfaceTextureListener(callback)));
60 }
61
UpdateTexImage()62 void SurfaceTexture::UpdateTexImage() {
63 JNIEnv* env = base::android::AttachCurrentThread();
64 Java_SurfaceTexturePlatformWrapper_updateTexImage(env,
65 j_surface_texture_.obj());
66 }
67
ReleaseTexImage()68 void SurfaceTexture::ReleaseTexImage() {
69 DCHECK(IsSingleBufferModeSupported());
70 JNIEnv* env = base::android::AttachCurrentThread();
71 Java_SurfaceTexturePlatformWrapper_releaseTexImage(env,
72 j_surface_texture_.obj());
73 }
74
GetTransformMatrix(float mtx[16])75 void SurfaceTexture::GetTransformMatrix(float mtx[16]) {
76 JNIEnv* env = base::android::AttachCurrentThread();
77
78 base::android::ScopedJavaLocalRef<jfloatArray> jmatrix(
79 env, env->NewFloatArray(16));
80 Java_SurfaceTexturePlatformWrapper_getTransformMatrix(
81 env, j_surface_texture_.obj(), jmatrix.obj());
82
83 jboolean is_copy;
84 jfloat* elements = env->GetFloatArrayElements(jmatrix.obj(), &is_copy);
85 for (int i = 0; i < 16; ++i) {
86 mtx[i] = static_cast<float>(elements[i]);
87 }
88 env->ReleaseFloatArrayElements(jmatrix.obj(), elements, JNI_ABORT);
89 }
90
SetDefaultBufferSize(int width,int height)91 void SurfaceTexture::SetDefaultBufferSize(int width, int height) {
92 JNIEnv* env = base::android::AttachCurrentThread();
93
94 if (width > 0 && height > 0) {
95 Java_SurfaceTexturePlatformWrapper_setDefaultBufferSize(
96 env, j_surface_texture_.obj(), static_cast<jint>(width),
97 static_cast<jint>(height));
98 } else {
99 LOG(WARNING) << "Not setting surface texture buffer size - "
100 "width or height is 0";
101 }
102 }
103
AttachToGLContext()104 void SurfaceTexture::AttachToGLContext() {
105 if (GlContextMethodsAvailable()) {
106 int texture_id;
107 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texture_id);
108 DCHECK(texture_id);
109 JNIEnv* env = base::android::AttachCurrentThread();
110 Java_SurfaceTexturePlatformWrapper_attachToGLContext(
111 env, j_surface_texture_.obj(), texture_id);
112 }
113 }
114
DetachFromGLContext()115 void SurfaceTexture::DetachFromGLContext() {
116 if (GlContextMethodsAvailable()) {
117 JNIEnv* env = base::android::AttachCurrentThread();
118 Java_SurfaceTexturePlatformWrapper_detachFromGLContext(
119 env, j_surface_texture_.obj());
120 }
121 }
122
CreateSurface()123 ANativeWindow* SurfaceTexture::CreateSurface() {
124 JNIEnv* env = base::android::AttachCurrentThread();
125 ScopedJavaSurface surface(this);
126 // Note: This ensures that any local references used by
127 // ANativeWindow_fromSurface are released immediately. This is needed as a
128 // workaround for https://code.google.com/p/android/issues/detail?id=68174
129 base::android::ScopedJavaLocalFrame scoped_local_reference_frame(env);
130 ANativeWindow* native_window = ANativeWindow_fromSurface(
131 env, surface.j_surface().obj());
132 return native_window;
133 }
134
135 // static
IsSingleBufferModeSupported()136 bool SurfaceTexture::IsSingleBufferModeSupported() {
137 return base::android::BuildInfo::GetInstance()->sdk_int() >= 19;
138 }
139
RegisterSurfaceTexture(JNIEnv * env)140 bool SurfaceTexture::RegisterSurfaceTexture(JNIEnv* env) {
141 return RegisterNativesImpl(env);
142 }
143
144 } // namespace gfx
145