tedbo | 0503161 | 2011-06-06 16:02:47 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 "ParcelSurfaceTexture" |
| 18 | |
| 19 | #include <gui/SurfaceTextureClient.h> |
| 20 | |
| 21 | #include <android_runtime/AndroidRuntime.h> |
tedbo | cc5278a | 2011-06-10 13:05:32 -0700 | [diff] [blame] | 22 | #include <android_runtime/android_graphics_SurfaceTexture.h> |
tedbo | 0503161 | 2011-06-06 16:02:47 -0700 | [diff] [blame] | 23 | |
| 24 | #include <utils/Log.h> |
| 25 | |
| 26 | #include <binder/Parcel.h> |
| 27 | |
| 28 | #include "android_util_Binder.h" |
| 29 | #include "jni.h" |
| 30 | #include "JNIHelp.h" |
tedbo | 0503161 | 2011-06-06 16:02:47 -0700 | [diff] [blame] | 31 | |
| 32 | // ---------------------------------------------------------------------------- |
| 33 | |
| 34 | namespace android { |
| 35 | |
| 36 | const char* const kParcelSurfaceTextureClassPathName = |
| 37 | "android/graphics/ParcelSurfaceTexture"; |
| 38 | |
| 39 | struct fields_t { |
| 40 | jfieldID iSurfaceTexture; |
| 41 | }; |
| 42 | static fields_t fields; |
| 43 | |
| 44 | #define ANDROID_GRAPHICS_ISURFACETEXTURE_JNI_ID "mISurfaceTexture" |
| 45 | |
| 46 | // ---------------------------------------------------------------------------- |
| 47 | |
| 48 | static void ParcelSurfaceTexture_setISurfaceTexture( |
| 49 | JNIEnv* env, jobject thiz, const sp<ISurfaceTexture>& iSurfaceTexture) |
| 50 | { |
| 51 | ISurfaceTexture* const p = |
| 52 | (ISurfaceTexture*)env->GetIntField(thiz, fields.iSurfaceTexture); |
| 53 | if (iSurfaceTexture.get()) { |
| 54 | iSurfaceTexture->incStrong(thiz); |
| 55 | } |
| 56 | if (p) { |
| 57 | p->decStrong(thiz); |
| 58 | } |
| 59 | env->SetIntField(thiz, fields.iSurfaceTexture, (int)iSurfaceTexture.get()); |
| 60 | } |
| 61 | |
tedbo | cc5278a | 2011-06-10 13:05:32 -0700 | [diff] [blame] | 62 | sp<ISurfaceTexture> ParcelSurfaceTexture_getISurfaceTexture( |
tedbo | 0503161 | 2011-06-06 16:02:47 -0700 | [diff] [blame] | 63 | JNIEnv* env, jobject thiz) |
| 64 | { |
| 65 | sp<ISurfaceTexture> iSurfaceTexture( |
| 66 | (ISurfaceTexture*)env->GetIntField(thiz, fields.iSurfaceTexture)); |
| 67 | return iSurfaceTexture; |
| 68 | } |
| 69 | |
| 70 | sp<ANativeWindow> android_ParcelSurfaceTexture_getNativeWindow( |
| 71 | JNIEnv* env, jobject thiz) |
| 72 | { |
| 73 | sp<ISurfaceTexture> iSurfaceTexture( |
| 74 | ParcelSurfaceTexture_getISurfaceTexture(env, thiz)); |
| 75 | sp<SurfaceTextureClient> surfaceTextureClient(iSurfaceTexture != NULL ? |
| 76 | new SurfaceTextureClient(iSurfaceTexture) : NULL); |
| 77 | return surfaceTextureClient; |
| 78 | } |
| 79 | |
| 80 | bool android_ParcelSurfaceTexture_isInstanceOf(JNIEnv* env, jobject thiz) |
| 81 | { |
| 82 | jclass parcelSurfaceTextureClass = env->FindClass( |
| 83 | kParcelSurfaceTextureClassPathName); |
| 84 | return env->IsInstanceOf(thiz, parcelSurfaceTextureClass); |
| 85 | } |
| 86 | |
| 87 | // ---------------------------------------------------------------------------- |
| 88 | |
| 89 | static void ParcelSurfaceTexture_classInit(JNIEnv* env, jclass clazz) |
| 90 | { |
| 91 | fields.iSurfaceTexture = |
| 92 | env->GetFieldID(clazz, ANDROID_GRAPHICS_ISURFACETEXTURE_JNI_ID, "I"); |
| 93 | if (fields.iSurfaceTexture == NULL) { |
| 94 | LOGE("can't find android/graphics/ParcelSurfaceTexture.%s", |
| 95 | ANDROID_GRAPHICS_ISURFACETEXTURE_JNI_ID); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | static void ParcelSurfaceTexture_init(JNIEnv* env, jobject thiz, jobject jSurfaceTexture) |
| 100 | { |
| 101 | sp<ISurfaceTexture> iSurfaceTexture( |
| 102 | SurfaceTexture_getSurfaceTexture(env, jSurfaceTexture)); |
| 103 | ParcelSurfaceTexture_setISurfaceTexture(env, thiz, iSurfaceTexture); |
| 104 | } |
| 105 | |
| 106 | static void ParcelSurfaceTexture_finalize(JNIEnv* env, jobject thiz) |
| 107 | { |
| 108 | ParcelSurfaceTexture_setISurfaceTexture(env, thiz, 0); |
| 109 | } |
| 110 | |
| 111 | static void ParcelSurfaceTexture_writeToParcel( |
| 112 | JNIEnv* env, jobject thiz, jobject jParcel, jint flags) |
| 113 | { |
| 114 | Parcel* parcel = parcelForJavaObject(env, jParcel); |
| 115 | sp<ISurfaceTexture> iSurfaceTexture( |
| 116 | ParcelSurfaceTexture_getISurfaceTexture(env, thiz)); |
| 117 | sp<IBinder> b(iSurfaceTexture->asBinder()); |
| 118 | parcel->writeStrongBinder(b); |
| 119 | } |
| 120 | |
| 121 | static void ParcelSurfaceTexture_readFromParcel( |
| 122 | JNIEnv* env, jobject thiz, jobject jParcel) |
| 123 | { |
| 124 | Parcel* parcel = parcelForJavaObject(env, jParcel); |
| 125 | sp<ISurfaceTexture> iSurfaceTexture( |
| 126 | interface_cast<ISurfaceTexture>(parcel->readStrongBinder())); |
| 127 | ParcelSurfaceTexture_setISurfaceTexture(env, thiz, iSurfaceTexture); |
| 128 | } |
| 129 | |
| 130 | // ---------------------------------------------------------------------------- |
| 131 | |
| 132 | static JNINativeMethod gParcelSurfaceTextureMethods[] = { |
| 133 | {"nativeClassInit", "()V", (void*)ParcelSurfaceTexture_classInit }, |
| 134 | {"nativeInit", "(Landroid/graphics/SurfaceTexture;)V", |
| 135 | (void *)ParcelSurfaceTexture_init }, |
| 136 | { "nativeFinalize", "()V", (void *)ParcelSurfaceTexture_finalize }, |
| 137 | { "nativeWriteToParcel", "(Landroid/os/Parcel;I)V", |
| 138 | (void *)ParcelSurfaceTexture_writeToParcel }, |
| 139 | { "nativeReadFromParcel", "(Landroid/os/Parcel;)V", |
| 140 | (void *)ParcelSurfaceTexture_readFromParcel }, |
| 141 | }; |
| 142 | |
| 143 | |
| 144 | int register_android_graphics_ParcelSurfaceTexture(JNIEnv* env) |
| 145 | { |
| 146 | int err = 0; |
| 147 | err = AndroidRuntime::registerNativeMethods(env, kParcelSurfaceTextureClassPathName, |
| 148 | gParcelSurfaceTextureMethods, NELEM(gParcelSurfaceTextureMethods)); |
| 149 | return err; |
| 150 | } |
| 151 | |
| 152 | } // namespace android |