Derek Sollenberger | 08581f1 | 2009-09-08 18:36:29 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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 | #include <string.h> |
| 18 | #include <jni.h> |
| 19 | #include <JNIHelp.h> |
| 20 | #include <utils/Log.h> |
| 21 | |
| 22 | #include "PluginObject.h" |
| 23 | |
| 24 | #define EXPORT __attribute__((visibility("default"))) |
| 25 | |
| 26 | static SurfaceSubPlugin* getPluginObject(int npp) { |
| 27 | NPP instance = (NPP)npp; |
| 28 | PluginObject* obj = static_cast<PluginObject*>(instance->pdata); |
| 29 | if (obj && obj->activePlugin |
| 30 | && obj->activePlugin->supportsDrawingModel(kSurface_ANPDrawingModel)) { |
| 31 | return static_cast<SurfaceSubPlugin*>(obj->activePlugin); |
| 32 | } |
| 33 | return NULL; |
| 34 | } |
| 35 | |
Derek Sollenberger | 51cce58 | 2009-12-01 08:26:20 -0500 | [diff] [blame^] | 36 | static jboolean javaInit(JNIEnv* env, jobject thiz, jint npp) { |
| 37 | SurfaceSubPlugin* obj = getPluginObject(npp); |
| 38 | |
| 39 | if (obj) { |
| 40 | jobject globalObject = env->NewGlobalRef(thiz); |
| 41 | obj->setJavaInterface(globalObject); |
| 42 | return true; |
| 43 | } else { |
| 44 | return false; |
| 45 | } |
| 46 | } |
| 47 | |
Derek Sollenberger | 08581f1 | 2009-09-08 18:36:29 -0400 | [diff] [blame] | 48 | static void surfaceCreated(JNIEnv* env, jobject thiz, jint npp, jobject surface) { |
| 49 | SurfaceSubPlugin* obj = getPluginObject(npp); |
Derek Sollenberger | b8947ee | 2009-10-05 14:40:18 -0400 | [diff] [blame] | 50 | jobject globalSurface = env->NewGlobalRef(surface); |
| 51 | obj->surfaceCreated(globalSurface); |
Derek Sollenberger | 08581f1 | 2009-09-08 18:36:29 -0400 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | static void surfaceChanged(JNIEnv* env, jobject thiz, jint npp, jint format, jint width, jint height) { |
| 55 | SurfaceSubPlugin* obj = getPluginObject(npp); |
| 56 | obj->surfaceChanged(format, width, height); |
| 57 | } |
| 58 | |
| 59 | static void surfaceDestroyed(JNIEnv* env, jobject thiz, jint npp) { |
| 60 | SurfaceSubPlugin* obj = getPluginObject(npp); |
| 61 | if (obj) { |
| 62 | obj->surfaceDestroyed(); |
| 63 | } |
| 64 | } |
| 65 | |
Derek Sollenberger | bb660dd | 2009-10-13 11:43:48 -0400 | [diff] [blame] | 66 | static jint getSurfaceWidth(JNIEnv* env, jobject thiz, jint npp) { |
| 67 | SurfaceSubPlugin* obj = getPluginObject(npp); |
| 68 | return obj->getPluginWidth(); |
| 69 | } |
| 70 | |
| 71 | static jint getSurfaceHeight(JNIEnv* env, jobject thiz, jint npp) { |
| 72 | SurfaceSubPlugin* obj = getPluginObject(npp); |
| 73 | return obj->getPluginHeight(); |
| 74 | } |
| 75 | |
Derek Sollenberger | 08581f1 | 2009-09-08 18:36:29 -0400 | [diff] [blame] | 76 | static jboolean isFixedSurface(JNIEnv* env, jobject thiz, jint npp) { |
| 77 | SurfaceSubPlugin* obj = getPluginObject(npp); |
| 78 | return obj->isFixedSurface(); |
| 79 | } |
| 80 | |
| 81 | /* |
| 82 | * JNI registration. |
| 83 | */ |
Derek Sollenberger | 04b3e9a | 2009-11-24 14:50:55 -0500 | [diff] [blame] | 84 | static JNINativeMethod gJavaSamplePluginMethods[] = { |
Derek Sollenberger | 51cce58 | 2009-12-01 08:26:20 -0500 | [diff] [blame^] | 85 | { "nativeJavaInit", "(I)Z", (void*) javaInit }, |
Derek Sollenberger | 08581f1 | 2009-09-08 18:36:29 -0400 | [diff] [blame] | 86 | { "nativeSurfaceCreated", "(ILandroid/view/View;)V", (void*) surfaceCreated }, |
| 87 | { "nativeSurfaceChanged", "(IIII)V", (void*) surfaceChanged }, |
| 88 | { "nativeSurfaceDestroyed", "(I)V", (void*) surfaceDestroyed }, |
Derek Sollenberger | bb660dd | 2009-10-13 11:43:48 -0400 | [diff] [blame] | 89 | { "nativeGetSurfaceWidth", "(I)I", (void*) getSurfaceWidth }, |
| 90 | { "nativeGetSurfaceHeight", "(I)I", (void*) getSurfaceHeight }, |
Derek Sollenberger | 08581f1 | 2009-09-08 18:36:29 -0400 | [diff] [blame] | 91 | { "nativeIsFixedSurface", "(I)Z", (void*) isFixedSurface }, |
| 92 | }; |
| 93 | |
| 94 | EXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) { |
| 95 | |
| 96 | JNIEnv* env = NULL; |
| 97 | |
| 98 | if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) { |
| 99 | return -1; |
| 100 | } |
| 101 | |
Derek Sollenberger | 04b3e9a | 2009-11-24 14:50:55 -0500 | [diff] [blame] | 102 | jniRegisterNativeMethods(env, "com/android/sampleplugin/SamplePlugin", |
| 103 | gJavaSamplePluginMethods, NELEM(gJavaSamplePluginMethods)); |
Derek Sollenberger | 08581f1 | 2009-09-08 18:36:29 -0400 | [diff] [blame] | 104 | |
| 105 | return JNI_VERSION_1_4; |
| 106 | } |