| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/gl/android/surface_texture_bridge.h" | 5 #include "ui/gl/android/surface_texture_bridge.h" |
| 6 | 6 |
| 7 #include <android/native_window_jni.h> | 7 #include <android/native_window_jni.h> |
| 8 | 8 |
| 9 // TODO(boliu): Remove this include when we move off ICS. | 9 // TODO(boliu): Remove this include when we move off ICS. |
| 10 #include "base/android/build_info.h" | 10 #include "base/android/build_info.h" |
| 11 #include "base/android/jni_android.h" | 11 #include "base/android/jni_android.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "jni/SurfaceTexture_jni.h" | 13 #include "jni/SurfaceTextureBridge_jni.h" |
| 14 #include "ui/gl/android/scoped_java_surface.h" | 14 #include "ui/gl/android/scoped_java_surface.h" |
| 15 #include "ui/gl/android/surface_texture_listener.h" | 15 #include "ui/gl/android/surface_texture_listener.h" |
| 16 #include "ui/gl/gl_bindings.h" | 16 #include "ui/gl/gl_bindings.h" |
| 17 | 17 |
| 18 using base::android::AttachCurrentThread; | |
| 19 using base::android::CheckException; | |
| 20 using base::android::GetClass; | |
| 21 using base::android::ScopedJavaLocalRef; | |
| 22 | |
| 23 namespace { | |
| 24 bool g_jni_initialized = false; | |
| 25 | |
| 26 void RegisterNativesIfNeeded(JNIEnv* env) { | |
| 27 if (!g_jni_initialized) { | |
| 28 JNI_SurfaceTexture::RegisterNativesImpl(env); | |
| 29 g_jni_initialized = true; | |
| 30 } | |
| 31 } | |
| 32 | |
| 33 // TODO(boliu): Remove this method when when we move off ICS. See | 18 // TODO(boliu): Remove this method when when we move off ICS. See |
| 34 // http://crbug.com/161864. | 19 // http://crbug.com/161864. |
| 35 bool GlContextMethodsAvailable() { | 20 bool GlContextMethodsAvailable() { |
| 36 bool available = base::android::BuildInfo::GetInstance()->sdk_int() >= 16; | 21 bool available = base::android::BuildInfo::GetInstance()->sdk_int() >= 16; |
| 37 if (!available) | 22 if (!available) |
| 38 LOG(WARNING) << "Running on unsupported device: rendering may not work"; | 23 LOG(WARNING) << "Running on unsupported device: rendering may not work"; |
| 39 return available; | 24 return available; |
| 40 } | 25 } |
| 41 | 26 |
| 42 } // namespace | |
| 43 | |
| 44 namespace gfx { | 27 namespace gfx { |
| 45 | 28 |
| 46 SurfaceTextureBridge::SurfaceTextureBridge(int texture_id) { | 29 SurfaceTextureBridge::SurfaceTextureBridge(int texture_id) { |
| 47 JNIEnv* env = AttachCurrentThread(); | 30 JNIEnv* env = base::android::AttachCurrentThread(); |
| 48 CHECK(env); | 31 j_surface_texture_.Reset(Java_SurfaceTextureBridge_create(env, texture_id)); |
| 49 RegisterNativesIfNeeded(env); | |
| 50 | |
| 51 ScopedJavaLocalRef<jobject> tmp( | |
| 52 JNI_SurfaceTexture::Java_SurfaceTexture_Constructor( | |
| 53 env, texture_id)); | |
| 54 DCHECK(!tmp.is_null()); | |
| 55 j_surface_texture_.Reset(tmp); | |
| 56 } | 32 } |
| 57 | 33 |
| 58 SurfaceTextureBridge::~SurfaceTextureBridge() { | 34 SurfaceTextureBridge::~SurfaceTextureBridge() { |
| 59 JNIEnv* env = AttachCurrentThread(); | 35 JNIEnv* env = base::android::AttachCurrentThread(); |
| 60 CHECK(env); | 36 Java_SurfaceTextureBridge_destroy(env, j_surface_texture_.obj()); |
| 61 | |
| 62 // Release the listener. | |
| 63 JNI_SurfaceTexture::Java_SurfaceTexture_setOnFrameAvailableListener( | |
| 64 env, j_surface_texture_.obj(), NULL); | |
| 65 | |
| 66 // Release graphics memory. | |
| 67 JNI_SurfaceTexture::Java_SurfaceTexture_release( | |
| 68 env, j_surface_texture_.obj()); | |
| 69 } | 37 } |
| 70 | 38 |
| 71 void SurfaceTextureBridge::SetFrameAvailableCallback( | 39 void SurfaceTextureBridge::SetFrameAvailableCallback( |
| 72 const base::Closure& callback) { | 40 const base::Closure& callback) { |
| 73 JNIEnv* env = AttachCurrentThread(); | 41 JNIEnv* env = base::android::AttachCurrentThread(); |
| 74 CHECK(env); | 42 Java_SurfaceTextureBridge_setFrameAvailableCallback( |
| 75 | |
| 76 // Since the listener is owned by the Java SurfaceTexture object, setting | |
| 77 // a new listener here will release an existing one at the same time. | |
| 78 ScopedJavaLocalRef<jobject> j_listener( | |
| 79 env, | 43 env, |
| 80 SurfaceTextureListener::CreateSurfaceTextureListener(env, callback)); | 44 j_surface_texture_.obj(), |
| 81 DCHECK(!j_listener.is_null()); | 45 reinterpret_cast<int>(new SurfaceTextureListener(callback))); |
| 82 | |
| 83 // Set it as the onFrameAvailableListener for our SurfaceTexture instance. | |
| 84 JNI_SurfaceTexture::Java_SurfaceTexture_setOnFrameAvailableListener( | |
| 85 env, j_surface_texture_.obj(), j_listener.obj()); | |
| 86 } | 46 } |
| 87 | 47 |
| 88 void SurfaceTextureBridge::UpdateTexImage() { | 48 void SurfaceTextureBridge::UpdateTexImage() { |
| 89 JNIEnv* env = AttachCurrentThread(); | 49 JNIEnv* env = base::android::AttachCurrentThread(); |
| 90 CHECK(env); | 50 Java_SurfaceTextureBridge_updateTexImage(env, j_surface_texture_.obj()); |
| 91 | |
| 92 JNI_SurfaceTexture::Java_SurfaceTexture_updateTexImage( | |
| 93 env, j_surface_texture_.obj()); | |
| 94 } | 51 } |
| 95 | 52 |
| 96 void SurfaceTextureBridge::GetTransformMatrix(float mtx[16]) { | 53 void SurfaceTextureBridge::GetTransformMatrix(float mtx[16]) { |
| 97 JNIEnv* env = AttachCurrentThread(); | 54 JNIEnv* env = base::android::AttachCurrentThread(); |
| 98 CHECK(env); | |
| 99 | 55 |
| 100 ScopedJavaLocalRef<jfloatArray> jmatrix(env, env->NewFloatArray(16)); | 56 base::android::ScopedJavaLocalRef<jfloatArray> jmatrix( |
| 101 JNI_SurfaceTexture::Java_SurfaceTexture_getTransformMatrix( | 57 env, env->NewFloatArray(16)); |
| 58 Java_SurfaceTextureBridge_getTransformMatrix( |
| 102 env, j_surface_texture_.obj(), jmatrix.obj()); | 59 env, j_surface_texture_.obj(), jmatrix.obj()); |
| 103 | 60 |
| 104 jboolean is_copy; | 61 jboolean is_copy; |
| 105 jfloat* elements = env->GetFloatArrayElements(jmatrix.obj(), &is_copy); | 62 jfloat* elements = env->GetFloatArrayElements(jmatrix.obj(), &is_copy); |
| 106 for (int i = 0; i < 16; ++i) { | 63 for (int i = 0; i < 16; ++i) { |
| 107 mtx[i] = static_cast<float>(elements[i]); | 64 mtx[i] = static_cast<float>(elements[i]); |
| 108 } | 65 } |
| 109 env->ReleaseFloatArrayElements(jmatrix.obj(), elements, JNI_ABORT); | 66 env->ReleaseFloatArrayElements(jmatrix.obj(), elements, JNI_ABORT); |
| 110 } | 67 } |
| 111 | 68 |
| 112 void SurfaceTextureBridge::SetDefaultBufferSize(int width, int height) { | 69 void SurfaceTextureBridge::SetDefaultBufferSize(int width, int height) { |
| 113 JNIEnv* env = AttachCurrentThread(); | 70 JNIEnv* env = base::android::AttachCurrentThread(); |
| 114 CHECK(env); | |
| 115 | 71 |
| 116 if (width > 0 && height > 0) { | 72 if (width > 0 && height > 0) { |
| 117 JNI_SurfaceTexture::Java_SurfaceTexture_setDefaultBufferSize( | 73 Java_SurfaceTextureBridge_setDefaultBufferSize( |
| 118 env, j_surface_texture_.obj(), static_cast<jint>(width), | 74 env, j_surface_texture_.obj(), static_cast<jint>(width), |
| 119 static_cast<jint>(height)); | 75 static_cast<jint>(height)); |
| 120 } else { | 76 } else { |
| 121 LOG(WARNING) << "Not setting surface texture buffer size - " | 77 LOG(WARNING) << "Not setting surface texture buffer size - " |
| 122 "width or height is 0"; | 78 "width or height is 0"; |
| 123 } | 79 } |
| 124 } | 80 } |
| 125 | 81 |
| 126 void SurfaceTextureBridge::AttachToGLContext() { | 82 void SurfaceTextureBridge::AttachToGLContext() { |
| 127 if (GlContextMethodsAvailable()) { | 83 if (GlContextMethodsAvailable()) { |
| 128 int texture_id; | 84 int texture_id; |
| 129 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texture_id); | 85 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texture_id); |
| 130 DCHECK(texture_id); | 86 DCHECK(texture_id); |
| 131 JNIEnv* env = AttachCurrentThread(); | 87 JNIEnv* env = base::android::AttachCurrentThread(); |
| 132 // Note: This method is only available on JB and greater. | 88 Java_SurfaceTextureBridge_attachToGLContext( |
| 133 JNI_SurfaceTexture::Java_SurfaceTexture_attachToGLContext( | |
| 134 env, j_surface_texture_.obj(), texture_id); | 89 env, j_surface_texture_.obj(), texture_id); |
| 135 } | 90 } |
| 136 } | 91 } |
| 137 | 92 |
| 138 void SurfaceTextureBridge::DetachFromGLContext() { | 93 void SurfaceTextureBridge::DetachFromGLContext() { |
| 139 if (GlContextMethodsAvailable()) { | 94 if (GlContextMethodsAvailable()) { |
| 140 JNIEnv* env = AttachCurrentThread(); | 95 JNIEnv* env = base::android::AttachCurrentThread(); |
| 141 // Note: This method is only available on JB and greater. | 96 Java_SurfaceTextureBridge_detachFromGLContext( |
| 142 JNI_SurfaceTexture::Java_SurfaceTexture_detachFromGLContext( | |
| 143 env, j_surface_texture_.obj()); | 97 env, j_surface_texture_.obj()); |
| 144 } | 98 } |
| 145 } | 99 } |
| 146 | 100 |
| 147 ANativeWindow* SurfaceTextureBridge::CreateSurface() { | 101 ANativeWindow* SurfaceTextureBridge::CreateSurface() { |
| 148 JNIEnv* env = AttachCurrentThread(); | 102 JNIEnv* env = base::android::AttachCurrentThread(); |
| 149 ScopedJavaSurface surface(this); | 103 ScopedJavaSurface surface(this); |
| 150 ANativeWindow* native_window = | 104 ANativeWindow* native_window = ANativeWindow_fromSurface( |
| 151 ANativeWindow_fromSurface(env, surface.j_surface().obj()); | 105 env, surface.j_surface().obj()); |
| 152 return native_window; | 106 return native_window; |
| 153 } | 107 } |
| 154 | 108 |
| 109 // static |
| 110 bool SurfaceTextureBridge::RegisterSurfaceTextureBridge(JNIEnv* env) { |
| 111 return RegisterNativesImpl(env); |
| 112 } |
| 113 |
| 155 } // namespace gfx | 114 } // namespace gfx |
| OLD | NEW |