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"; |
| 38 | |
Jamie Gennis | 376590d | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 39 | struct fields_t { |
| 40 | jfieldID surfaceTexture; |
| 41 | jmethodID postEvent; |
Jamie Gennis | 6714efc | 2010-12-20 12:09:37 -0800 | [diff] [blame] | 42 | }; |
Jamie Gennis | 376590d | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 43 | static fields_t fields; |
Jamie Gennis | 6714efc | 2010-12-20 12:09:37 -0800 | [diff] [blame] | 44 | |
| 45 | // ---------------------------------------------------------------------------- |
| 46 | |
Jamie Gennis | 376590d | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 47 | static void SurfaceTexture_setSurfaceTexture(JNIEnv* env, jobject thiz, |
Jamie Gennis | 6714efc | 2010-12-20 12:09:37 -0800 | [diff] [blame] | 48 | const sp<SurfaceTexture>& surfaceTexture) |
| 49 | { |
| 50 | SurfaceTexture* const p = |
Jamie Gennis | 376590d | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 51 | (SurfaceTexture*)env->GetIntField(thiz, fields.surfaceTexture); |
Jamie Gennis | 6714efc | 2010-12-20 12:09:37 -0800 | [diff] [blame] | 52 | if (surfaceTexture.get()) { |
Jamie Gennis | 376590d | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 53 | surfaceTexture->incStrong(thiz); |
Jamie Gennis | 6714efc | 2010-12-20 12:09:37 -0800 | [diff] [blame] | 54 | } |
| 55 | if (p) { |
Jamie Gennis | 376590d | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 56 | p->decStrong(thiz); |
Jamie Gennis | 6714efc | 2010-12-20 12:09:37 -0800 | [diff] [blame] | 57 | } |
Jamie Gennis | 376590d | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 58 | env->SetIntField(thiz, fields.surfaceTexture, (int)surfaceTexture.get()); |
Jamie Gennis | 6714efc | 2010-12-20 12:09:37 -0800 | [diff] [blame] | 59 | } |
| 60 | |
Jamie Gennis | 376590d | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 61 | sp<SurfaceTexture> SurfaceTexture_getSurfaceTexture(JNIEnv* env, jobject thiz) |
Jamie Gennis | 6714efc | 2010-12-20 12:09:37 -0800 | [diff] [blame] | 62 | { |
| 63 | sp<SurfaceTexture> surfaceTexture( |
Jamie Gennis | 376590d | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 64 | (SurfaceTexture*)env->GetIntField(thiz, fields.surfaceTexture)); |
Jamie Gennis | 6714efc | 2010-12-20 12:09:37 -0800 | [diff] [blame] | 65 | return surfaceTexture; |
| 66 | } |
| 67 | |
Glenn Kasten | 846db33 | 2011-03-04 11:44:32 -0800 | [diff] [blame] | 68 | sp<ANativeWindow> android_SurfaceTexture_getNativeWindow( |
| 69 | JNIEnv* env, jobject thiz) |
| 70 | { |
| 71 | sp<SurfaceTexture> surfaceTexture(SurfaceTexture_getSurfaceTexture(env, thiz)); |
| 72 | sp<SurfaceTextureClient> surfaceTextureClient(surfaceTexture != NULL ? |
| 73 | new SurfaceTextureClient(surfaceTexture) : NULL); |
| 74 | return surfaceTextureClient; |
| 75 | } |
| 76 | |
Jamie Gennis | 6714efc | 2010-12-20 12:09:37 -0800 | [diff] [blame] | 77 | // ---------------------------------------------------------------------------- |
| 78 | |
Jamie Gennis | 376590d | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 79 | class JNISurfaceTextureContext : public SurfaceTexture::FrameAvailableListener |
Jamie Gennis | 6714efc | 2010-12-20 12:09:37 -0800 | [diff] [blame] | 80 | { |
Jamie Gennis | 376590d | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 81 | public: |
| 82 | JNISurfaceTextureContext(JNIEnv* env, jobject weakThiz, jclass clazz); |
| 83 | virtual ~JNISurfaceTextureContext(); |
| 84 | virtual void onFrameAvailable(); |
Jamie Gennis | 6714efc | 2010-12-20 12:09:37 -0800 | [diff] [blame] | 85 | |
Jamie Gennis | 376590d | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 86 | private: |
| 87 | jobject mWeakThiz; |
| 88 | jclass mClazz; |
| 89 | }; |
| 90 | |
| 91 | JNISurfaceTextureContext::JNISurfaceTextureContext(JNIEnv* env, |
| 92 | jobject weakThiz, jclass clazz) : |
| 93 | mWeakThiz(env->NewGlobalRef(weakThiz)), |
| 94 | mClazz((jclass)env->NewGlobalRef(clazz)) |
| 95 | {} |
| 96 | |
| 97 | JNISurfaceTextureContext::~JNISurfaceTextureContext() |
| 98 | { |
| 99 | JNIEnv *env = AndroidRuntime::getJNIEnv(); |
| 100 | env->DeleteGlobalRef(mWeakThiz); |
| 101 | env->DeleteGlobalRef(mClazz); |
Jamie Gennis | 6714efc | 2010-12-20 12:09:37 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Jamie Gennis | 376590d | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 104 | void JNISurfaceTextureContext::onFrameAvailable() |
Jamie Gennis | 6714efc | 2010-12-20 12:09:37 -0800 | [diff] [blame] | 105 | { |
Jamie Gennis | 376590d | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 106 | JNIEnv *env = AndroidRuntime::getJNIEnv(); |
| 107 | env->CallStaticVoidMethod(mClazz, fields.postEvent, mWeakThiz); |
| 108 | } |
| 109 | |
| 110 | // ---------------------------------------------------------------------------- |
| 111 | |
| 112 | static void SurfaceTexture_classInit(JNIEnv* env, jclass clazz) |
| 113 | { |
| 114 | fields.surfaceTexture = env->GetFieldID(clazz, |
| 115 | ANDROID_GRAPHICS_SURFACETEXTURE_JNI_ID, "I"); |
| 116 | if (fields.surfaceTexture == NULL) { |
| 117 | LOGE("can't find android/graphics/SurfaceTexture.%s", |
| 118 | ANDROID_GRAPHICS_SURFACETEXTURE_JNI_ID); |
| 119 | } |
| 120 | |
| 121 | fields.postEvent = env->GetStaticMethodID(clazz, "postEventFromNative", |
| 122 | "(Ljava/lang/Object;)V"); |
| 123 | if (fields.postEvent == NULL) { |
| 124 | LOGE("can't find android/graphics/SurfaceTexture.postEventFromNative"); |
| 125 | } |
| 126 | |
| 127 | } |
| 128 | |
| 129 | static void SurfaceTexture_init(JNIEnv* env, jobject thiz, jint texName, |
| 130 | jobject weakThiz) |
| 131 | { |
| 132 | sp<SurfaceTexture> surfaceTexture(new SurfaceTexture(texName)); |
| 133 | if (surfaceTexture == 0) { |
| 134 | jniThrowException(env, OutOfResourcesException, |
| 135 | "Unable to create native SurfaceTexture"); |
| 136 | return; |
| 137 | } |
| 138 | SurfaceTexture_setSurfaceTexture(env, thiz, surfaceTexture); |
| 139 | |
| 140 | jclass clazz = env->GetObjectClass(thiz); |
| 141 | if (clazz == NULL) { |
| 142 | jniThrowRuntimeException(env, |
| 143 | "Can't find android/graphics/SurfaceTexture"); |
| 144 | return; |
| 145 | } |
| 146 | |
| 147 | sp<JNISurfaceTextureContext> ctx(new JNISurfaceTextureContext(env, weakThiz, |
| 148 | clazz)); |
| 149 | surfaceTexture->setFrameAvailableListener(ctx); |
| 150 | } |
| 151 | |
| 152 | static void SurfaceTexture_finalize(JNIEnv* env, jobject thiz) |
| 153 | { |
| 154 | sp<SurfaceTexture> surfaceTexture(SurfaceTexture_getSurfaceTexture(env, thiz)); |
| 155 | surfaceTexture->setFrameAvailableListener(0); |
| 156 | SurfaceTexture_setSurfaceTexture(env, thiz, 0); |
| 157 | } |
| 158 | |
| 159 | static void SurfaceTexture_updateTexImage(JNIEnv* env, jobject thiz) |
| 160 | { |
| 161 | sp<SurfaceTexture> surfaceTexture(SurfaceTexture_getSurfaceTexture(env, thiz)); |
Jamie Gennis | 6714efc | 2010-12-20 12:09:37 -0800 | [diff] [blame] | 162 | surfaceTexture->updateTexImage(); |
| 163 | } |
| 164 | |
Jamie Gennis | 376590d | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 165 | static void SurfaceTexture_getTransformMatrix(JNIEnv* env, jobject thiz, |
Jamie Gennis | b0ba48c | 2011-01-09 18:22:05 -0800 | [diff] [blame] | 166 | jfloatArray jmtx) |
| 167 | { |
Jamie Gennis | 376590d | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 168 | sp<SurfaceTexture> surfaceTexture(SurfaceTexture_getSurfaceTexture(env, thiz)); |
Jamie Gennis | b0ba48c | 2011-01-09 18:22:05 -0800 | [diff] [blame] | 169 | float* mtx = env->GetFloatArrayElements(jmtx, NULL); |
| 170 | surfaceTexture->getTransformMatrix(mtx); |
| 171 | env->ReleaseFloatArrayElements(jmtx, mtx, 0); |
| 172 | } |
| 173 | |
Jamie Gennis | 6714efc | 2010-12-20 12:09:37 -0800 | [diff] [blame] | 174 | // ---------------------------------------------------------------------------- |
| 175 | |
| 176 | const char* const kSurfaceTextureClassPathName = "android/graphics/SurfaceTexture"; |
Jamie Gennis | 6714efc | 2010-12-20 12:09:37 -0800 | [diff] [blame] | 177 | |
| 178 | static JNINativeMethod gSurfaceTextureMethods[] = { |
Jamie Gennis | 376590d | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 179 | {"nativeClassInit", "()V", (void*)SurfaceTexture_classInit }, |
| 180 | {"nativeInit", "(ILjava/lang/Object;)V", (void*)SurfaceTexture_init }, |
| 181 | {"nativeFinalize", "()V", (void*)SurfaceTexture_finalize }, |
| 182 | {"nativeUpdateTexImage", "()V", (void*)SurfaceTexture_updateTexImage }, |
| 183 | {"nativeGetTransformMatrix", "([F)V", (void*)SurfaceTexture_getTransformMatrix }, |
Jamie Gennis | 6714efc | 2010-12-20 12:09:37 -0800 | [diff] [blame] | 184 | }; |
| 185 | |
Jamie Gennis | 6714efc | 2010-12-20 12:09:37 -0800 | [diff] [blame] | 186 | int register_android_graphics_SurfaceTexture(JNIEnv* env) |
| 187 | { |
| 188 | int err = 0; |
| 189 | err = AndroidRuntime::registerNativeMethods(env, kSurfaceTextureClassPathName, |
| 190 | gSurfaceTextureMethods, NELEM(gSurfaceTextureMethods)); |
| 191 | return err; |
| 192 | } |
| 193 | |
| 194 | } // namespace android |