Marius Renn | 65953da | 2012-03-27 10:44:45 -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 | #include <string> |
| 18 | #include <vector> |
| 19 | |
| 20 | #include "jni/jni_native_program.h" |
| 21 | #include "jni/jni_util.h" |
| 22 | |
| 23 | #include "native/base/logging.h" |
| 24 | #include "native/core/native_frame.h" |
| 25 | #include "native/core/native_program.h" |
| 26 | |
| 27 | using android::filterfw::NativeFrame; |
| 28 | using android::filterfw::NativeProgram; |
| 29 | |
| 30 | jboolean Java_android_filterfw_core_NativeProgram_allocate(JNIEnv* env, jobject thiz) { |
| 31 | return ToJBool(WrapObjectInJava(new NativeProgram(), env, thiz, true)); |
| 32 | } |
| 33 | |
| 34 | jboolean Java_android_filterfw_core_NativeProgram_deallocate(JNIEnv* env, jobject thiz) { |
| 35 | return ToJBool(DeleteNativeObject<NativeProgram>(env, thiz)); |
| 36 | } |
| 37 | |
| 38 | jboolean Java_android_filterfw_core_NativeProgram_nativeInit(JNIEnv* env, jobject thiz) { |
| 39 | NativeProgram* program = ConvertFromJava<NativeProgram>(env, thiz); |
| 40 | return ToJBool(program && program->CallInit()); |
| 41 | } |
| 42 | |
| 43 | jboolean Java_android_filterfw_core_NativeProgram_openNativeLibrary(JNIEnv* env, |
| 44 | jobject thiz, |
| 45 | jstring lib_name) { |
| 46 | NativeProgram* program = ConvertFromJava<NativeProgram>(env, thiz); |
| 47 | return ToJBool(program && lib_name && program->OpenLibrary(ToCppString(env, lib_name))); |
| 48 | } |
| 49 | |
| 50 | jboolean Java_android_filterfw_core_NativeProgram_bindInitFunction(JNIEnv* env, |
| 51 | jobject thiz, |
| 52 | jstring func_name) { |
| 53 | NativeProgram* program = ConvertFromJava<NativeProgram>(env, thiz); |
| 54 | return ToJBool(program && func_name && program->BindInitFunction(ToCppString(env, func_name))); |
| 55 | } |
| 56 | |
| 57 | jboolean Java_android_filterfw_core_NativeProgram_bindSetValueFunction(JNIEnv* env, |
| 58 | jobject thiz, |
| 59 | jstring func_name) { |
| 60 | NativeProgram* program = ConvertFromJava<NativeProgram>(env, thiz); |
| 61 | return ToJBool(program && |
| 62 | func_name && |
| 63 | program->BindSetValueFunction(ToCppString(env, func_name))); |
| 64 | } |
| 65 | |
| 66 | jboolean Java_android_filterfw_core_NativeProgram_bindGetValueFunction(JNIEnv* env, |
| 67 | jobject thiz, |
| 68 | jstring func_name) { |
| 69 | NativeProgram* program = ConvertFromJava<NativeProgram>(env, thiz); |
| 70 | return ToJBool(program && |
| 71 | func_name && |
| 72 | program->BindGetValueFunction(ToCppString(env, func_name))); |
| 73 | } |
| 74 | |
| 75 | jboolean Java_android_filterfw_core_NativeProgram_bindProcessFunction(JNIEnv* env, |
| 76 | jobject thiz, |
| 77 | jstring func_name) { |
| 78 | NativeProgram* program = ConvertFromJava<NativeProgram>(env, thiz); |
| 79 | return ToJBool(program && func_name && program->BindProcessFunction(ToCppString(env, func_name))); |
| 80 | } |
| 81 | |
| 82 | jboolean Java_android_filterfw_core_NativeProgram_bindResetFunction(JNIEnv* env, |
| 83 | jobject thiz, |
| 84 | jstring func_name) { |
| 85 | NativeProgram* program = ConvertFromJava<NativeProgram>(env, thiz); |
| 86 | return ToJBool(program && |
| 87 | func_name && |
| 88 | program->BindResetFunction(ToCppString(env, func_name))); |
| 89 | } |
| 90 | |
| 91 | jboolean Java_android_filterfw_core_NativeProgram_bindTeardownFunction(JNIEnv* env, |
| 92 | jobject thiz, |
| 93 | jstring func_name) { |
| 94 | NativeProgram* program = ConvertFromJava<NativeProgram>(env, thiz); |
| 95 | return ToJBool(program && |
| 96 | func_name && |
| 97 | program->BindTeardownFunction(ToCppString(env, func_name))); |
| 98 | } |
| 99 | |
| 100 | jboolean Java_android_filterfw_core_NativeProgram_callNativeInit(JNIEnv* env, jobject thiz) { |
| 101 | NativeProgram* program = ConvertFromJava<NativeProgram>(env, thiz); |
| 102 | return ToJBool(program && program->CallInit()); |
| 103 | } |
| 104 | |
| 105 | jboolean Java_android_filterfw_core_NativeProgram_callNativeSetValue(JNIEnv* env, |
| 106 | jobject thiz, |
| 107 | jstring key, |
| 108 | jstring value) { |
| 109 | if (!value) { |
| 110 | ALOGE("Native Program: Attempting to set null value for key %s!", |
| 111 | ToCppString(env, key).c_str()); |
| 112 | } |
| 113 | NativeProgram* program = ConvertFromJava<NativeProgram>(env, thiz); |
| 114 | const std::string c_value = ToCppString(env, value); |
| 115 | const std::string c_key = ToCppString(env, key); |
| 116 | return ToJBool(program && program->CallSetValue(c_key, c_value)); |
| 117 | } |
| 118 | |
| 119 | jstring Java_android_filterfw_core_NativeProgram_callNativeGetValue(JNIEnv* env, |
| 120 | jobject thiz, |
| 121 | jstring key) { |
| 122 | NativeProgram* program = ConvertFromJava<NativeProgram>(env, thiz); |
| 123 | const std::string c_key = ToCppString(env, key); |
| 124 | if (program) { |
| 125 | return ToJString(env, program->CallGetValue(c_key)); |
| 126 | } |
| 127 | return JNI_FALSE; |
| 128 | } |
| 129 | |
| 130 | jboolean Java_android_filterfw_core_NativeProgram_callNativeProcess(JNIEnv* env, |
| 131 | jobject thiz, |
| 132 | jobjectArray inputs, |
| 133 | jobject output) { |
| 134 | NativeProgram* program = ConvertFromJava<NativeProgram>(env, thiz); |
| 135 | |
| 136 | // Sanity checks |
| 137 | if (!program || !inputs) { |
| 138 | return JNI_FALSE; |
| 139 | } |
| 140 | |
| 141 | // Get the input buffers |
| 142 | const int input_count = env->GetArrayLength(inputs); |
| 143 | std::vector<const char*> input_buffers(input_count, NULL); |
| 144 | std::vector<int> input_sizes(input_count, 0); |
| 145 | for (int i = 0 ; i < input_count; ++i) { |
| 146 | const char* input_data = NULL; |
| 147 | int input_size = 0; |
| 148 | jobject input = env->GetObjectArrayElement(inputs, i); |
| 149 | if (input) { |
| 150 | NativeFrame* native_frame = ConvertFromJava<NativeFrame>(env, input); |
| 151 | if (!native_frame) { |
| 152 | ALOGE("NativeProgram: Could not grab NativeFrame input %d!", i); |
| 153 | return JNI_FALSE; |
| 154 | } |
| 155 | input_data = reinterpret_cast<const char*>(native_frame->Data()); |
| 156 | input_size = native_frame->Size(); |
| 157 | } |
| 158 | input_buffers[i] = input_data; |
| 159 | input_sizes[i] = input_size; |
| 160 | } |
| 161 | |
| 162 | // Get the output buffer |
| 163 | char* output_data = NULL; |
| 164 | int output_size = 0; |
| 165 | if (output) { |
| 166 | NativeFrame* output_frame = ConvertFromJava<NativeFrame>(env, output); |
| 167 | if (!output_frame) { |
| 168 | ALOGE("NativeProgram: Could not grab NativeFrame output!"); |
| 169 | return JNI_FALSE; |
| 170 | } |
| 171 | output_data = reinterpret_cast<char*>(output_frame->MutableData()); |
| 172 | output_size = output_frame->Size(); |
| 173 | } |
| 174 | |
| 175 | // Process the frames! |
| 176 | return ToJBool(program->CallProcess(input_buffers, input_sizes, output_data, output_size)); |
| 177 | } |
| 178 | |
| 179 | jboolean Java_android_filterfw_core_NativeProgram_callNativeReset(JNIEnv* env, jobject thiz) { |
| 180 | NativeProgram* program = ConvertFromJava<NativeProgram>(env, thiz); |
| 181 | return ToJBool(program && program->CallReset()); |
| 182 | } |
| 183 | |
| 184 | jboolean Java_android_filterfw_core_NativeProgram_callNativeTeardown(JNIEnv* env, jobject thiz) { |
| 185 | NativeProgram* program = ConvertFromJava<NativeProgram>(env, thiz); |
| 186 | return ToJBool(program && program->CallTeardown()); |
| 187 | } |