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