blob: c48b974fe758602803cd51f56c067dcfe807c08d [file] [log] [blame]
Jamie Gennis6714efc2010-12-20 12:09:37 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "SurfaceTexture"
18
19#include <stdio.h>
20
21#include <gui/SurfaceTexture.h>
Glenn Kasten846db332011-03-04 11:44:32 -080022#include <gui/SurfaceTextureClient.h>
Jamie Gennis6714efc2010-12-20 12:09:37 -080023
24#include <android_runtime/AndroidRuntime.h>
25
26#include <utils/Log.h>
27#include <utils/misc.h>
28
Jamie Gennis6714efc2010-12-20 12:09:37 -080029#include "jni.h"
Jamie Gennis376590d2011-01-13 14:43:36 -080030#include "JNIHelp.h"
Jamie Gennis6714efc2010-12-20 12:09:37 -080031
32// ----------------------------------------------------------------------------
33
34namespace android {
35
36static const char* const OutOfResourcesException =
37 "android/graphics/SurfaceTexture$OutOfResourcesException";
Jamie Gennis2b4bfa52012-04-13 14:48:22 -070038static const char* const IllegalStateException = "java/lang/IllegalStateException";
tedbo05031612011-06-06 16:02:47 -070039const char* const kSurfaceTextureClassPathName = "android/graphics/SurfaceTexture";
Jamie Gennis6714efc2010-12-20 12:09:37 -080040
Jamie Gennis376590d2011-01-13 14:43:36 -080041struct fields_t {
42 jfieldID surfaceTexture;
43 jmethodID postEvent;
Jamie Gennis6714efc2010-12-20 12:09:37 -080044};
Jamie Gennis376590d2011-01-13 14:43:36 -080045static fields_t fields;
Jamie Gennis6714efc2010-12-20 12:09:37 -080046
47// ----------------------------------------------------------------------------
48
Jamie Gennis376590d2011-01-13 14:43:36 -080049static void SurfaceTexture_setSurfaceTexture(JNIEnv* env, jobject thiz,
Jamie Gennis6714efc2010-12-20 12:09:37 -080050 const sp<SurfaceTexture>& surfaceTexture)
51{
52 SurfaceTexture* const p =
Jamie Gennis376590d2011-01-13 14:43:36 -080053 (SurfaceTexture*)env->GetIntField(thiz, fields.surfaceTexture);
Jamie Gennis6714efc2010-12-20 12:09:37 -080054 if (surfaceTexture.get()) {
Jamie Gennis376590d2011-01-13 14:43:36 -080055 surfaceTexture->incStrong(thiz);
Jamie Gennis6714efc2010-12-20 12:09:37 -080056 }
57 if (p) {
Jamie Gennis376590d2011-01-13 14:43:36 -080058 p->decStrong(thiz);
Jamie Gennis6714efc2010-12-20 12:09:37 -080059 }
Jamie Gennis376590d2011-01-13 14:43:36 -080060 env->SetIntField(thiz, fields.surfaceTexture, (int)surfaceTexture.get());
Jamie Gennis6714efc2010-12-20 12:09:37 -080061}
62
Jamie Gennis376590d2011-01-13 14:43:36 -080063sp<SurfaceTexture> SurfaceTexture_getSurfaceTexture(JNIEnv* env, jobject thiz)
Jamie Gennis6714efc2010-12-20 12:09:37 -080064{
65 sp<SurfaceTexture> surfaceTexture(
Jamie Gennis376590d2011-01-13 14:43:36 -080066 (SurfaceTexture*)env->GetIntField(thiz, fields.surfaceTexture));
Jamie Gennis6714efc2010-12-20 12:09:37 -080067 return surfaceTexture;
68}
69
Glenn Kasten846db332011-03-04 11:44:32 -080070sp<ANativeWindow> android_SurfaceTexture_getNativeWindow(
71 JNIEnv* env, jobject thiz)
72{
73 sp<SurfaceTexture> surfaceTexture(SurfaceTexture_getSurfaceTexture(env, thiz));
74 sp<SurfaceTextureClient> surfaceTextureClient(surfaceTexture != NULL ?
75 new SurfaceTextureClient(surfaceTexture) : NULL);
76 return surfaceTextureClient;
77}
78
tedbo05031612011-06-06 16:02:47 -070079bool android_SurfaceTexture_isInstanceOf(JNIEnv* env, jobject thiz)
80{
81 jclass surfaceTextureClass = env->FindClass(kSurfaceTextureClassPathName);
82 return env->IsInstanceOf(thiz, surfaceTextureClass);
83}
84
Jamie Gennis6714efc2010-12-20 12:09:37 -080085// ----------------------------------------------------------------------------
86
Jamie Gennis376590d2011-01-13 14:43:36 -080087class JNISurfaceTextureContext : public SurfaceTexture::FrameAvailableListener
Jamie Gennis6714efc2010-12-20 12:09:37 -080088{
Jamie Gennis376590d2011-01-13 14:43:36 -080089public:
90 JNISurfaceTextureContext(JNIEnv* env, jobject weakThiz, jclass clazz);
91 virtual ~JNISurfaceTextureContext();
92 virtual void onFrameAvailable();
Jamie Gennis6714efc2010-12-20 12:09:37 -080093
Jamie Gennis376590d2011-01-13 14:43:36 -080094private:
Jamie Gennis0a8fd9b2011-06-20 18:39:35 -070095 static JNIEnv* getJNIEnv(bool* needsDetach);
96 static void detachJNI();
Jamie Gennis84293fb2011-06-17 16:35:35 -070097
Jamie Gennis376590d2011-01-13 14:43:36 -080098 jobject mWeakThiz;
99 jclass mClazz;
100};
101
102JNISurfaceTextureContext::JNISurfaceTextureContext(JNIEnv* env,
103 jobject weakThiz, jclass clazz) :
104 mWeakThiz(env->NewGlobalRef(weakThiz)),
105 mClazz((jclass)env->NewGlobalRef(clazz))
106{}
107
Jamie Gennis0a8fd9b2011-06-20 18:39:35 -0700108JNIEnv* JNISurfaceTextureContext::getJNIEnv(bool* needsDetach) {
109 *needsDetach = false;
110 JNIEnv* env = AndroidRuntime::getJNIEnv();
111 if (env == NULL) {
112 JavaVMAttachArgs args = {JNI_VERSION_1_4, NULL, NULL};
113 JavaVM* vm = AndroidRuntime::getJavaVM();
114 int result = vm->AttachCurrentThread(&env, (void*) &args);
115 if (result != JNI_OK) {
Steve Block3762c312012-01-06 19:20:56 +0000116 ALOGE("thread attach failed: %#x", result);
Jamie Gennis0a8fd9b2011-06-20 18:39:35 -0700117 return NULL;
118 }
119 *needsDetach = true;
Jamie Gennis84293fb2011-06-17 16:35:35 -0700120 }
121 return env;
122}
123
Jamie Gennis0a8fd9b2011-06-20 18:39:35 -0700124void JNISurfaceTextureContext::detachJNI() {
125 JavaVM* vm = AndroidRuntime::getJavaVM();
126 int result = vm->DetachCurrentThread();
127 if (result != JNI_OK) {
Steve Block3762c312012-01-06 19:20:56 +0000128 ALOGE("thread detach failed: %#x", result);
Jamie Gennis0a8fd9b2011-06-20 18:39:35 -0700129 }
130}
131
Jamie Gennis376590d2011-01-13 14:43:36 -0800132JNISurfaceTextureContext::~JNISurfaceTextureContext()
133{
Jamie Gennis0a8fd9b2011-06-20 18:39:35 -0700134 bool needsDetach = false;
135 JNIEnv* env = getJNIEnv(&needsDetach);
Jamie Gennis84293fb2011-06-17 16:35:35 -0700136 if (env != NULL) {
137 env->DeleteGlobalRef(mWeakThiz);
138 env->DeleteGlobalRef(mClazz);
139 } else {
Steve Block8564c8d2012-01-05 23:22:43 +0000140 ALOGW("leaking JNI object references");
Jamie Gennis84293fb2011-06-17 16:35:35 -0700141 }
Jamie Gennis0a8fd9b2011-06-20 18:39:35 -0700142 if (needsDetach) {
143 detachJNI();
144 }
Jamie Gennis6714efc2010-12-20 12:09:37 -0800145}
146
Jamie Gennis376590d2011-01-13 14:43:36 -0800147void JNISurfaceTextureContext::onFrameAvailable()
Jamie Gennis6714efc2010-12-20 12:09:37 -0800148{
Jamie Gennis0a8fd9b2011-06-20 18:39:35 -0700149 bool needsDetach = false;
150 JNIEnv* env = getJNIEnv(&needsDetach);
Jamie Gennis84293fb2011-06-17 16:35:35 -0700151 if (env != NULL) {
152 env->CallStaticVoidMethod(mClazz, fields.postEvent, mWeakThiz);
153 } else {
Steve Block8564c8d2012-01-05 23:22:43 +0000154 ALOGW("onFrameAvailable event will not posted");
Jamie Gennis84293fb2011-06-17 16:35:35 -0700155 }
Jamie Gennis0a8fd9b2011-06-20 18:39:35 -0700156 if (needsDetach) {
157 detachJNI();
158 }
Jamie Gennis376590d2011-01-13 14:43:36 -0800159}
160
161// ----------------------------------------------------------------------------
162
163static void SurfaceTexture_classInit(JNIEnv* env, jclass clazz)
164{
165 fields.surfaceTexture = env->GetFieldID(clazz,
166 ANDROID_GRAPHICS_SURFACETEXTURE_JNI_ID, "I");
167 if (fields.surfaceTexture == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000168 ALOGE("can't find android/graphics/SurfaceTexture.%s",
Jamie Gennis376590d2011-01-13 14:43:36 -0800169 ANDROID_GRAPHICS_SURFACETEXTURE_JNI_ID);
170 }
171
172 fields.postEvent = env->GetStaticMethodID(clazz, "postEventFromNative",
173 "(Ljava/lang/Object;)V");
174 if (fields.postEvent == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000175 ALOGE("can't find android/graphics/SurfaceTexture.postEventFromNative");
Jamie Gennis376590d2011-01-13 14:43:36 -0800176 }
Jamie Gennis376590d2011-01-13 14:43:36 -0800177}
178
179static void SurfaceTexture_init(JNIEnv* env, jobject thiz, jint texName,
Grace Kloba0904d0a2011-06-23 21:21:47 -0700180 jobject weakThiz, jboolean allowSynchronous)
Jamie Gennis376590d2011-01-13 14:43:36 -0800181{
Grace Kloba0904d0a2011-06-23 21:21:47 -0700182 sp<SurfaceTexture> surfaceTexture(new SurfaceTexture(texName, allowSynchronous));
Jamie Gennis376590d2011-01-13 14:43:36 -0800183 if (surfaceTexture == 0) {
184 jniThrowException(env, OutOfResourcesException,
185 "Unable to create native SurfaceTexture");
186 return;
187 }
188 SurfaceTexture_setSurfaceTexture(env, thiz, surfaceTexture);
189
190 jclass clazz = env->GetObjectClass(thiz);
191 if (clazz == NULL) {
192 jniThrowRuntimeException(env,
193 "Can't find android/graphics/SurfaceTexture");
194 return;
195 }
196
197 sp<JNISurfaceTextureContext> ctx(new JNISurfaceTextureContext(env, weakThiz,
198 clazz));
199 surfaceTexture->setFrameAvailableListener(ctx);
200}
201
202static void SurfaceTexture_finalize(JNIEnv* env, jobject thiz)
203{
204 sp<SurfaceTexture> surfaceTexture(SurfaceTexture_getSurfaceTexture(env, thiz));
205 surfaceTexture->setFrameAvailableListener(0);
206 SurfaceTexture_setSurfaceTexture(env, thiz, 0);
207}
208
tedbo05031612011-06-06 16:02:47 -0700209static void SurfaceTexture_setDefaultBufferSize(
210 JNIEnv* env, jobject thiz, jint width, jint height)
211{
212 sp<SurfaceTexture> surfaceTexture(SurfaceTexture_getSurfaceTexture(env, thiz));
213 surfaceTexture->setDefaultBufferSize(width, height);
214}
215
Jamie Gennis2b4bfa52012-04-13 14:48:22 -0700216static void SurfaceTexture_updateTexImage(JNIEnv* env, jobject thiz)
Jamie Gennis376590d2011-01-13 14:43:36 -0800217{
218 sp<SurfaceTexture> surfaceTexture(SurfaceTexture_getSurfaceTexture(env, thiz));
Jamie Gennis2b4bfa52012-04-13 14:48:22 -0700219 status_t err = surfaceTexture->updateTexImage();
220 if (err == INVALID_OPERATION) {
221 jniThrowException(env, IllegalStateException, "Unable to update texture contents (see "
222 "logcat for details)");
Jamie Gennis721bfaa62012-04-13 19:14:37 -0700223 } else if (err < 0) {
Jamie Gennis2b4bfa52012-04-13 14:48:22 -0700224 jniThrowRuntimeException(env, "Error during updateTexImage (see logcat for details)");
225 }
Jamie Gennis6714efc2010-12-20 12:09:37 -0800226}
227
Jamie Gennisc6d99302012-04-05 11:34:02 -0700228static jint SurfaceTexture_detachFromGLContext(JNIEnv* env, jobject thiz)
229{
230 sp<SurfaceTexture> surfaceTexture(SurfaceTexture_getSurfaceTexture(env, thiz));
231 return surfaceTexture->detachFromContext();
232}
233
234static jint SurfaceTexture_attachToGLContext(JNIEnv* env, jobject thiz, jint tex)
235{
236 sp<SurfaceTexture> surfaceTexture(SurfaceTexture_getSurfaceTexture(env, thiz));
237 return surfaceTexture->attachToContext((GLuint)tex);
238}
239
Jamie Gennis376590d2011-01-13 14:43:36 -0800240static void SurfaceTexture_getTransformMatrix(JNIEnv* env, jobject thiz,
Jamie Gennisb0ba48c2011-01-09 18:22:05 -0800241 jfloatArray jmtx)
242{
Jamie Gennis376590d2011-01-13 14:43:36 -0800243 sp<SurfaceTexture> surfaceTexture(SurfaceTexture_getSurfaceTexture(env, thiz));
Jamie Gennisb0ba48c2011-01-09 18:22:05 -0800244 float* mtx = env->GetFloatArrayElements(jmtx, NULL);
245 surfaceTexture->getTransformMatrix(mtx);
246 env->ReleaseFloatArrayElements(jmtx, mtx, 0);
247}
248
Eino-Ville Talvalac5f94d82011-02-18 11:02:42 -0800249static jlong SurfaceTexture_getTimestamp(JNIEnv* env, jobject thiz)
250{
251 sp<SurfaceTexture> surfaceTexture(SurfaceTexture_getSurfaceTexture(env, thiz));
252 return surfaceTexture->getTimestamp();
253}
254
Mathias Agopianec46b4e2011-08-03 15:18:36 -0700255static void SurfaceTexture_release(JNIEnv* env, jobject thiz)
256{
257 sp<SurfaceTexture> surfaceTexture(SurfaceTexture_getSurfaceTexture(env, thiz));
258 surfaceTexture->abandon();
259}
260
Jamie Gennis6714efc2010-12-20 12:09:37 -0800261// ----------------------------------------------------------------------------
262
Jamie Gennis6714efc2010-12-20 12:09:37 -0800263static JNINativeMethod gSurfaceTextureMethods[] = {
Jamie Gennisc6d99302012-04-05 11:34:02 -0700264 {"nativeClassInit", "()V", (void*)SurfaceTexture_classInit },
265 {"nativeInit", "(ILjava/lang/Object;Z)V", (void*)SurfaceTexture_init },
266 {"nativeFinalize", "()V", (void*)SurfaceTexture_finalize },
tedbo05031612011-06-06 16:02:47 -0700267 {"nativeSetDefaultBufferSize", "(II)V", (void*)SurfaceTexture_setDefaultBufferSize },
Jamie Gennis2b4bfa52012-04-13 14:48:22 -0700268 {"nativeUpdateTexImage", "()V", (void*)SurfaceTexture_updateTexImage },
Jamie Gennisc6d99302012-04-05 11:34:02 -0700269 {"nativeDetachFromGLContext", "()I", (void*)SurfaceTexture_detachFromGLContext },
270 {"nativeAttachToGLContext", "(I)I", (void*)SurfaceTexture_attachToGLContext },
271 {"nativeGetTransformMatrix", "([F)V", (void*)SurfaceTexture_getTransformMatrix },
272 {"nativeGetTimestamp", "()J", (void*)SurfaceTexture_getTimestamp },
273 {"nativeRelease", "()V", (void*)SurfaceTexture_release },
Jamie Gennis6714efc2010-12-20 12:09:37 -0800274};
275
Jamie Gennis6714efc2010-12-20 12:09:37 -0800276int register_android_graphics_SurfaceTexture(JNIEnv* env)
277{
278 int err = 0;
279 err = AndroidRuntime::registerNativeMethods(env, kSurfaceTextureClassPathName,
280 gSurfaceTextureMethods, NELEM(gSurfaceTextureMethods));
281 return err;
282}
283
284} // namespace android