OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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 "content/common/android/surface_texture_bridge.h" |
| 6 |
| 7 #include "base/android/jni_android.h" |
| 8 #include "base/logging.h" |
| 9 #include "content/common/android/surface_texture_listener.h" |
| 10 |
| 11 using base::android::AttachCurrentThread; |
| 12 using base::android::CheckException; |
| 13 using base::android::GetClass; |
| 14 using base::android::GetMethodID; |
| 15 using base::android::ScopedJavaLocalRef; |
| 16 |
| 17 namespace content { |
| 18 |
| 19 SurfaceTextureBridge::SurfaceTextureBridge(int texture_id) |
| 20 : texture_id_(texture_id) { |
| 21 JNIEnv* env = AttachCurrentThread(); |
| 22 CHECK(env); |
| 23 |
| 24 j_class_.Reset(GetClass(env, "android/graphics/SurfaceTexture")); |
| 25 jmethodID constructor = GetMethodID(env, j_class_, "<init>", "(I)V"); |
| 26 ScopedJavaLocalRef<jobject> tmp(env, |
| 27 env->NewObject(j_class_.obj(), constructor, texture_id)); |
| 28 DCHECK(!tmp.is_null()); |
| 29 j_surface_texture_.Reset(tmp); |
| 30 } |
| 31 |
| 32 SurfaceTextureBridge::~SurfaceTextureBridge() { |
| 33 JNIEnv* env = AttachCurrentThread(); |
| 34 CHECK(env); |
| 35 |
| 36 // Release the listener. |
| 37 const char* method_signature = |
| 38 "(Landroid/graphics/SurfaceTexture$OnFrameAvailableListener;)V"; |
| 39 jmethodID method = GetMethodID(env, j_class_, "setOnFrameAvailableListener", |
| 40 method_signature); |
| 41 env->CallVoidMethod(j_surface_texture_.obj(), method, NULL); |
| 42 CheckException(env); |
| 43 |
| 44 // Release graphics memory. |
| 45 jmethodID release = GetMethodID(env, j_class_, "release", "()V"); |
| 46 env->CallVoidMethod(j_surface_texture_.obj(), release); |
| 47 CheckException(env); |
| 48 } |
| 49 |
| 50 void SurfaceTextureBridge::SetFrameAvailableCallback( |
| 51 const base::Closure& callback) { |
| 52 JNIEnv* env = AttachCurrentThread(); |
| 53 CHECK(env); |
| 54 |
| 55 // Since the listener is owned by the Java SurfaceTexture object, setting |
| 56 // a new listener here will release an existing one at the same time. |
| 57 ScopedJavaLocalRef<jobject> j_listener(env, |
| 58 SurfaceTextureListener::CreateSurfaceTextureListener(env, callback)); |
| 59 DCHECK(!j_listener.is_null()); |
| 60 |
| 61 // Set it as the onFrameAvailableListener for our SurfaceTexture instance. |
| 62 const char* method_signature = |
| 63 "(Landroid/graphics/SurfaceTexture$OnFrameAvailableListener;)V"; |
| 64 jmethodID method = GetMethodID(env, j_class_, "setOnFrameAvailableListener", |
| 65 method_signature); |
| 66 env->CallVoidMethod(j_surface_texture_.obj(), method, j_listener.obj()); |
| 67 CheckException(env); |
| 68 } |
| 69 |
| 70 void SurfaceTextureBridge::UpdateTexImage() { |
| 71 JNIEnv* env = AttachCurrentThread(); |
| 72 CHECK(env); |
| 73 |
| 74 jmethodID method = GetMethodID(env, j_class_, "updateTexImage", "()V"); |
| 75 env->CallVoidMethod(j_surface_texture_.obj(), method); |
| 76 CheckException(env); |
| 77 } |
| 78 |
| 79 void SurfaceTextureBridge::GetTransformMatrix(float mtx[16]) { |
| 80 JNIEnv* env = AttachCurrentThread(); |
| 81 CHECK(env); |
| 82 |
| 83 ScopedJavaLocalRef<jfloatArray> jmatrix(env, env->NewFloatArray(16)); |
| 84 jmethodID method = GetMethodID(env, j_class_, "getTransformMatrix", "([F)V"); |
| 85 env->CallVoidMethod(j_surface_texture_.obj(), method, jmatrix.obj()); |
| 86 CheckException(env); |
| 87 |
| 88 jboolean is_copy; |
| 89 jfloat* elements = env->GetFloatArrayElements(jmatrix.obj(), &is_copy); |
| 90 for (int i = 0; i < 16; ++i) { |
| 91 mtx[i] = static_cast<float>(elements[i]); |
| 92 } |
| 93 env->ReleaseFloatArrayElements(jmatrix.obj(), elements, JNI_ABORT); |
| 94 } |
| 95 |
| 96 void SurfaceTextureBridge::SetDefaultBufferSize(int width, int height) { |
| 97 JNIEnv* env = AttachCurrentThread(); |
| 98 CHECK(env); |
| 99 |
| 100 jmethodID method = GetMethodID(env, j_class_, |
| 101 "setDefaultBufferSize", "(II)V"); |
| 102 env->CallVoidMethod(j_surface_texture_.obj(), method, |
| 103 static_cast<jint>(width), static_cast<jint>(height)); |
| 104 CheckException(env); |
| 105 } |
| 106 |
| 107 } // namespace content |
OLD | NEW |