Jamie Gennis | 6714efc | 2010-12-20 12:09:37 -0800 | [diff] [blame] | 1 | /* |
| 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 Kasten | 846db33 | 2011-03-04 11:44:32 -0800 | [diff] [blame] | 22 | #include <gui/SurfaceTextureClient.h> |
Jamie Gennis | 6714efc | 2010-12-20 12:09:37 -0800 | [diff] [blame] | 23 | |
| 24 | #include <android_runtime/AndroidRuntime.h> |
| 25 | |
| 26 | #include <utils/Log.h> |
| 27 | #include <utils/misc.h> |
| 28 | |
Jamie Gennis | 6714efc | 2010-12-20 12:09:37 -0800 | [diff] [blame] | 29 | #include "jni.h" |
Jamie Gennis | 376590d | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 30 | #include "JNIHelp.h" |
Jamie Gennis | 6714efc | 2010-12-20 12:09:37 -0800 | [diff] [blame] | 31 | |
| 32 | // ---------------------------------------------------------------------------- |
| 33 | |
| 34 | namespace android { |
| 35 | |
| 36 | static const char* const OutOfResourcesException = |
| 37 | "android/graphics/SurfaceTexture$OutOfResourcesException"; |
Jamie Gennis | 2b4bfa5 | 2012-04-13 14:48:22 -0700 | [diff] [blame] | 38 | static const char* const IllegalStateException = "java/lang/IllegalStateException"; |
tedbo | 0503161 | 2011-06-06 16:02:47 -0700 | [diff] [blame] | 39 | const char* const kSurfaceTextureClassPathName = "android/graphics/SurfaceTexture"; |
Jamie Gennis | 6714efc | 2010-12-20 12:09:37 -0800 | [diff] [blame] | 40 | |
Jamie Gennis | 376590d | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 41 | struct fields_t { |
| 42 | jfieldID surfaceTexture; |
| 43 | jmethodID postEvent; |
Jamie Gennis | 6714efc | 2010-12-20 12:09:37 -0800 | [diff] [blame] | 44 | }; |
Jamie Gennis | 376590d | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 45 | static fields_t fields; |
Jamie Gennis | 6714efc | 2010-12-20 12:09:37 -0800 | [diff] [blame] | 46 | |
| 47 | // ---------------------------------------------------------------------------- |
| 48 | |
Jamie Gennis | 376590d | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 49 | static void SurfaceTexture_setSurfaceTexture(JNIEnv* env, jobject thiz, |
Jamie Gennis | 6714efc | 2010-12-20 12:09:37 -0800 | [diff] [blame] | 50 | const sp<SurfaceTexture>& surfaceTexture) |
| 51 | { |
| 52 | SurfaceTexture* const p = |
Jamie Gennis | 376590d | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 53 | (SurfaceTexture*)env->GetIntField(thiz, fields.surfaceTexture); |
Jamie Gennis | 6714efc | 2010-12-20 12:09:37 -0800 | [diff] [blame] | 54 | if (surfaceTexture.get()) { |
Jamie Gennis | 376590d | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 55 | surfaceTexture->incStrong(thiz); |
Jamie Gennis | 6714efc | 2010-12-20 12:09:37 -0800 | [diff] [blame] | 56 | } |
| 57 | if (p) { |
Jamie Gennis | 376590d | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 58 | p->decStrong(thiz); |
Jamie Gennis | 6714efc | 2010-12-20 12:09:37 -0800 | [diff] [blame] | 59 | } |
Jamie Gennis | 376590d | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 60 | env->SetIntField(thiz, fields.surfaceTexture, (int)surfaceTexture.get()); |
Jamie Gennis | 6714efc | 2010-12-20 12:09:37 -0800 | [diff] [blame] | 61 | } |
| 62 | |
Jamie Gennis | 376590d | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 63 | sp<SurfaceTexture> SurfaceTexture_getSurfaceTexture(JNIEnv* env, jobject thiz) |
Jamie Gennis | 6714efc | 2010-12-20 12:09:37 -0800 | [diff] [blame] | 64 | { |
| 65 | sp<SurfaceTexture> surfaceTexture( |
Jamie Gennis | 376590d | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 66 | (SurfaceTexture*)env->GetIntField(thiz, fields.surfaceTexture)); |
Jamie Gennis | 6714efc | 2010-12-20 12:09:37 -0800 | [diff] [blame] | 67 | return surfaceTexture; |
| 68 | } |
| 69 | |
Glenn Kasten | 846db33 | 2011-03-04 11:44:32 -0800 | [diff] [blame] | 70 | sp<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 | |
tedbo | 0503161 | 2011-06-06 16:02:47 -0700 | [diff] [blame] | 79 | bool android_SurfaceTexture_isInstanceOf(JNIEnv* env, jobject thiz) |
| 80 | { |
| 81 | jclass surfaceTextureClass = env->FindClass(kSurfaceTextureClassPathName); |
| 82 | return env->IsInstanceOf(thiz, surfaceTextureClass); |
| 83 | } |
| 84 | |
Jamie Gennis | 6714efc | 2010-12-20 12:09:37 -0800 | [diff] [blame] | 85 | // ---------------------------------------------------------------------------- |
| 86 | |
Jamie Gennis | 376590d | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 87 | class JNISurfaceTextureContext : public SurfaceTexture::FrameAvailableListener |
Jamie Gennis | 6714efc | 2010-12-20 12:09:37 -0800 | [diff] [blame] | 88 | { |
Jamie Gennis | 376590d | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 89 | public: |
| 90 | JNISurfaceTextureContext(JNIEnv* env, jobject weakThiz, jclass clazz); |
| 91 | virtual ~JNISurfaceTextureContext(); |
| 92 | virtual void onFrameAvailable(); |
Jamie Gennis | 6714efc | 2010-12-20 12:09:37 -0800 | [diff] [blame] | 93 | |
Jamie Gennis | 376590d | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 94 | private: |
Jamie Gennis | 0a8fd9b | 2011-06-20 18:39:35 -0700 | [diff] [blame] | 95 | static JNIEnv* getJNIEnv(bool* needsDetach); |
| 96 | static void detachJNI(); |
Jamie Gennis | 84293fb | 2011-06-17 16:35:35 -0700 | [diff] [blame] | 97 | |
Jamie Gennis | 376590d | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 98 | jobject mWeakThiz; |
| 99 | jclass mClazz; |
| 100 | }; |
| 101 | |
| 102 | JNISurfaceTextureContext::JNISurfaceTextureContext(JNIEnv* env, |
| 103 | jobject weakThiz, jclass clazz) : |
| 104 | mWeakThiz(env->NewGlobalRef(weakThiz)), |
| 105 | mClazz((jclass)env->NewGlobalRef(clazz)) |
| 106 | {} |
| 107 | |
Jamie Gennis | 0a8fd9b | 2011-06-20 18:39:35 -0700 | [diff] [blame] | 108 | JNIEnv* 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 Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 116 | ALOGE("thread attach failed: %#x", result); |
Jamie Gennis | 0a8fd9b | 2011-06-20 18:39:35 -0700 | [diff] [blame] | 117 | return NULL; |
| 118 | } |
| 119 | *needsDetach = true; |
Jamie Gennis | 84293fb | 2011-06-17 16:35:35 -0700 | [diff] [blame] | 120 | } |
| 121 | return env; |
| 122 | } |
| 123 | |
Jamie Gennis | 0a8fd9b | 2011-06-20 18:39:35 -0700 | [diff] [blame] | 124 | void JNISurfaceTextureContext::detachJNI() { |
| 125 | JavaVM* vm = AndroidRuntime::getJavaVM(); |
| 126 | int result = vm->DetachCurrentThread(); |
| 127 | if (result != JNI_OK) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 128 | ALOGE("thread detach failed: %#x", result); |
Jamie Gennis | 0a8fd9b | 2011-06-20 18:39:35 -0700 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | |
Jamie Gennis | 376590d | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 132 | JNISurfaceTextureContext::~JNISurfaceTextureContext() |
| 133 | { |
Jamie Gennis | 0a8fd9b | 2011-06-20 18:39:35 -0700 | [diff] [blame] | 134 | bool needsDetach = false; |
| 135 | JNIEnv* env = getJNIEnv(&needsDetach); |
Jamie Gennis | 84293fb | 2011-06-17 16:35:35 -0700 | [diff] [blame] | 136 | if (env != NULL) { |
| 137 | env->DeleteGlobalRef(mWeakThiz); |
| 138 | env->DeleteGlobalRef(mClazz); |
| 139 | } else { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 140 | ALOGW("leaking JNI object references"); |
Jamie Gennis | 84293fb | 2011-06-17 16:35:35 -0700 | [diff] [blame] | 141 | } |
Jamie Gennis | 0a8fd9b | 2011-06-20 18:39:35 -0700 | [diff] [blame] | 142 | if (needsDetach) { |
| 143 | detachJNI(); |
| 144 | } |
Jamie Gennis | 6714efc | 2010-12-20 12:09:37 -0800 | [diff] [blame] | 145 | } |
| 146 | |
Jamie Gennis | 376590d | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 147 | void JNISurfaceTextureContext::onFrameAvailable() |
Jamie Gennis | 6714efc | 2010-12-20 12:09:37 -0800 | [diff] [blame] | 148 | { |
Jamie Gennis | 0a8fd9b | 2011-06-20 18:39:35 -0700 | [diff] [blame] | 149 | bool needsDetach = false; |
| 150 | JNIEnv* env = getJNIEnv(&needsDetach); |
Jamie Gennis | 84293fb | 2011-06-17 16:35:35 -0700 | [diff] [blame] | 151 | if (env != NULL) { |
| 152 | env->CallStaticVoidMethod(mClazz, fields.postEvent, mWeakThiz); |
| 153 | } else { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 154 | ALOGW("onFrameAvailable event will not posted"); |
Jamie Gennis | 84293fb | 2011-06-17 16:35:35 -0700 | [diff] [blame] | 155 | } |
Jamie Gennis | 0a8fd9b | 2011-06-20 18:39:35 -0700 | [diff] [blame] | 156 | if (needsDetach) { |
| 157 | detachJNI(); |
| 158 | } |
Jamie Gennis | 376590d | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | // ---------------------------------------------------------------------------- |
| 162 | |
| 163 | static 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 Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 168 | ALOGE("can't find android/graphics/SurfaceTexture.%s", |
Jamie Gennis | 376590d | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 169 | 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 Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 175 | ALOGE("can't find android/graphics/SurfaceTexture.postEventFromNative"); |
Jamie Gennis | 376590d | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 176 | } |
Jamie Gennis | 376590d | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | static void SurfaceTexture_init(JNIEnv* env, jobject thiz, jint texName, |
Grace Kloba | 0904d0a | 2011-06-23 21:21:47 -0700 | [diff] [blame] | 180 | jobject weakThiz, jboolean allowSynchronous) |
Jamie Gennis | 376590d | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 181 | { |
Grace Kloba | 0904d0a | 2011-06-23 21:21:47 -0700 | [diff] [blame] | 182 | sp<SurfaceTexture> surfaceTexture(new SurfaceTexture(texName, allowSynchronous)); |
Jamie Gennis | 376590d | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 183 | 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 | |
| 202 | static 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 | |
tedbo | 0503161 | 2011-06-06 16:02:47 -0700 | [diff] [blame] | 209 | static 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 Gennis | 2b4bfa5 | 2012-04-13 14:48:22 -0700 | [diff] [blame] | 216 | static void SurfaceTexture_updateTexImage(JNIEnv* env, jobject thiz) |
Jamie Gennis | 376590d | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 217 | { |
| 218 | sp<SurfaceTexture> surfaceTexture(SurfaceTexture_getSurfaceTexture(env, thiz)); |
Jamie Gennis | 2b4bfa5 | 2012-04-13 14:48:22 -0700 | [diff] [blame] | 219 | 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 Gennis | 721bfaa6 | 2012-04-13 19:14:37 -0700 | [diff] [blame^] | 223 | } else if (err < 0) { |
Jamie Gennis | 2b4bfa5 | 2012-04-13 14:48:22 -0700 | [diff] [blame] | 224 | jniThrowRuntimeException(env, "Error during updateTexImage (see logcat for details)"); |
| 225 | } |
Jamie Gennis | 6714efc | 2010-12-20 12:09:37 -0800 | [diff] [blame] | 226 | } |
| 227 | |
Jamie Gennis | c6d9930 | 2012-04-05 11:34:02 -0700 | [diff] [blame] | 228 | static jint SurfaceTexture_detachFromGLContext(JNIEnv* env, jobject thiz) |
| 229 | { |
| 230 | sp<SurfaceTexture> surfaceTexture(SurfaceTexture_getSurfaceTexture(env, thiz)); |
| 231 | return surfaceTexture->detachFromContext(); |
| 232 | } |
| 233 | |
| 234 | static 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 Gennis | 376590d | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 240 | static void SurfaceTexture_getTransformMatrix(JNIEnv* env, jobject thiz, |
Jamie Gennis | b0ba48c | 2011-01-09 18:22:05 -0800 | [diff] [blame] | 241 | jfloatArray jmtx) |
| 242 | { |
Jamie Gennis | 376590d | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 243 | sp<SurfaceTexture> surfaceTexture(SurfaceTexture_getSurfaceTexture(env, thiz)); |
Jamie Gennis | b0ba48c | 2011-01-09 18:22:05 -0800 | [diff] [blame] | 244 | float* mtx = env->GetFloatArrayElements(jmtx, NULL); |
| 245 | surfaceTexture->getTransformMatrix(mtx); |
| 246 | env->ReleaseFloatArrayElements(jmtx, mtx, 0); |
| 247 | } |
| 248 | |
Eino-Ville Talvala | c5f94d8 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 249 | static jlong SurfaceTexture_getTimestamp(JNIEnv* env, jobject thiz) |
| 250 | { |
| 251 | sp<SurfaceTexture> surfaceTexture(SurfaceTexture_getSurfaceTexture(env, thiz)); |
| 252 | return surfaceTexture->getTimestamp(); |
| 253 | } |
| 254 | |
Mathias Agopian | ec46b4e | 2011-08-03 15:18:36 -0700 | [diff] [blame] | 255 | static void SurfaceTexture_release(JNIEnv* env, jobject thiz) |
| 256 | { |
| 257 | sp<SurfaceTexture> surfaceTexture(SurfaceTexture_getSurfaceTexture(env, thiz)); |
| 258 | surfaceTexture->abandon(); |
| 259 | } |
| 260 | |
Jamie Gennis | 6714efc | 2010-12-20 12:09:37 -0800 | [diff] [blame] | 261 | // ---------------------------------------------------------------------------- |
| 262 | |
Jamie Gennis | 6714efc | 2010-12-20 12:09:37 -0800 | [diff] [blame] | 263 | static JNINativeMethod gSurfaceTextureMethods[] = { |
Jamie Gennis | c6d9930 | 2012-04-05 11:34:02 -0700 | [diff] [blame] | 264 | {"nativeClassInit", "()V", (void*)SurfaceTexture_classInit }, |
| 265 | {"nativeInit", "(ILjava/lang/Object;Z)V", (void*)SurfaceTexture_init }, |
| 266 | {"nativeFinalize", "()V", (void*)SurfaceTexture_finalize }, |
tedbo | 0503161 | 2011-06-06 16:02:47 -0700 | [diff] [blame] | 267 | {"nativeSetDefaultBufferSize", "(II)V", (void*)SurfaceTexture_setDefaultBufferSize }, |
Jamie Gennis | 2b4bfa5 | 2012-04-13 14:48:22 -0700 | [diff] [blame] | 268 | {"nativeUpdateTexImage", "()V", (void*)SurfaceTexture_updateTexImage }, |
Jamie Gennis | c6d9930 | 2012-04-05 11:34:02 -0700 | [diff] [blame] | 269 | {"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 Gennis | 6714efc | 2010-12-20 12:09:37 -0800 | [diff] [blame] | 274 | }; |
| 275 | |
Jamie Gennis | 6714efc | 2010-12-20 12:09:37 -0800 | [diff] [blame] | 276 | int 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 |