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.
5 #include "ui/gl/android/surface_texture.h"
7 #include <android/native_window_jni.h>
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"
18 // TODO(boliu): Remove this method when when we move off ICS. See
19 // http://crbug.com/161864.
20 bool GlContextMethodsAvailable() {
21 bool available = base::android::BuildInfo::GetInstance()->sdk_int() >= 16;
23 LOG(WARNING) << "Running on unsupported device: rendering may not work";
29 SurfaceTexture::SurfaceTexture(int texture_id) {
30 JNIEnv* env = base::android::AttachCurrentThread();
31 j_surface_texture_.Reset(
32 Java_SurfaceTexturePlatformWrapper_create(env, texture_id));
35 SurfaceTexture::~SurfaceTexture() {
36 JNIEnv* env = base::android::AttachCurrentThread();
37 Java_SurfaceTexturePlatformWrapper_destroy(env, j_surface_texture_.obj());
40 void SurfaceTexture::SetFrameAvailableCallback(
41 const base::Closure& callback) {
42 JNIEnv* env = base::android::AttachCurrentThread();
43 Java_SurfaceTexturePlatformWrapper_setFrameAvailableCallback(
45 j_surface_texture_.obj(),
46 reinterpret_cast<int>(new SurfaceTextureListener(callback)));
49 void SurfaceTexture::UpdateTexImage() {
50 JNIEnv* env = base::android::AttachCurrentThread();
51 Java_SurfaceTexturePlatformWrapper_updateTexImage(env,
52 j_surface_texture_.obj());
55 void SurfaceTexture::GetTransformMatrix(float mtx[16]) {
56 JNIEnv* env = base::android::AttachCurrentThread();
58 base::android::ScopedJavaLocalRef<jfloatArray> jmatrix(
59 env, env->NewFloatArray(16));
60 Java_SurfaceTexturePlatformWrapper_getTransformMatrix(
61 env, j_surface_texture_.obj(), jmatrix.obj());
64 jfloat* elements = env->GetFloatArrayElements(jmatrix.obj(), &is_copy);
65 for (int i = 0; i < 16; ++i) {
66 mtx[i] = static_cast<float>(elements[i]);
68 env->ReleaseFloatArrayElements(jmatrix.obj(), elements, JNI_ABORT);
71 void SurfaceTexture::SetDefaultBufferSize(int width, int height) {
72 JNIEnv* env = base::android::AttachCurrentThread();
74 if (width > 0 && height > 0) {
75 Java_SurfaceTexturePlatformWrapper_setDefaultBufferSize(
76 env, j_surface_texture_.obj(), static_cast<jint>(width),
77 static_cast<jint>(height));
79 LOG(WARNING) << "Not setting surface texture buffer size - "
80 "width or height is 0";
84 void SurfaceTexture::AttachToGLContext() {
85 if (GlContextMethodsAvailable()) {
87 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texture_id);
89 JNIEnv* env = base::android::AttachCurrentThread();
90 Java_SurfaceTexturePlatformWrapper_attachToGLContext(
91 env, j_surface_texture_.obj(), texture_id);
95 void SurfaceTexture::DetachFromGLContext() {
96 if (GlContextMethodsAvailable()) {
97 JNIEnv* env = base::android::AttachCurrentThread();
98 Java_SurfaceTexturePlatformWrapper_detachFromGLContext(
99 env, j_surface_texture_.obj());
103 ANativeWindow* SurfaceTexture::CreateSurface() {
104 JNIEnv* env = base::android::AttachCurrentThread();
105 ScopedJavaSurface surface(this);
106 ANativeWindow* native_window = ANativeWindow_fromSurface(
107 env, surface.j_surface().obj());
108 return native_window;
112 bool SurfaceTexture::RegisterSurfaceTexture(JNIEnv* env) {
113 return RegisterNativesImpl(env);