OSDN Git Service

Add reset function to Native Program.
authorWei Hua <whua@google.com>
Tue, 23 Aug 2011 18:37:39 +0000 (11:37 -0700)
committerWei Hua <whua@google.com>
Tue, 23 Aug 2011 18:37:39 +0000 (11:37 -0700)
Bug: 5202663
Change-Id: I6d49e7cb765f776e193e8bf1fcde26678438e12e

mca/filterfw/java/android/filterfw/core/NativeProgram.java
mca/filterfw/java/android/filterfw/core/Program.java
mca/filterfw/jni/jni_native_program.cpp
mca/filterfw/jni/jni_native_program.h
mca/filterfw/native/core/native_program.cpp
mca/filterfw/native/core/native_program.h

index 916e641..791ab3c 100644 (file)
@@ -30,6 +30,7 @@ public class NativeProgram extends Program {
     private boolean mHasTeardownFunction = false;
     private boolean mHasSetValueFunction = false;
     private boolean mHasGetValueFunction = false;
+    private boolean mHasResetFunction = false;
     private boolean mTornDown = false;
 
     public NativeProgram(String nativeLibName, String nativeFunctionPrefix) {
@@ -63,13 +64,16 @@ public class NativeProgram extends Program {
         String getValueFuncName = nativeFunctionPrefix + "_getvalue";
         mHasGetValueFunction = bindGetValueFunction(getValueFuncName);
 
+        String resetFuncName = nativeFunctionPrefix + "_reset";
+        mHasResetFunction = bindResetFunction(resetFuncName);
+
         // Initialize the native code
         if (mHasInitFunction && !callNativeInit()) {
             throw new RuntimeException("Could not initialize NativeProgram!");
         }
     }
 
-    public  void tearDown() {
+    public void tearDown() {
         if (mTornDown) return;
         if (mHasTeardownFunction && !callNativeTeardown()) {
             throw new RuntimeException("Could not tear down NativeProgram!");
@@ -79,6 +83,13 @@ public class NativeProgram extends Program {
     }
 
     @Override
+    public void reset() {
+        if (mHasResetFunction && !callNativeReset()) {
+            throw new RuntimeException("Could not reset NativeProgram!");
+        }
+    }
+
+    @Override
     protected void finalize() throws Throwable {
         tearDown();
     }
@@ -153,11 +164,13 @@ public class NativeProgram extends Program {
     private native boolean bindSetValueFunction(String funcName);
     private native boolean bindGetValueFunction(String funcName);
     private native boolean bindProcessFunction(String funcName);
+    private native boolean bindResetFunction(String funcName);
     private native boolean bindTeardownFunction(String funcName);
 
     private native boolean callNativeInit();
     private native boolean callNativeSetValue(String key, String value);
     private native String  callNativeGetValue(String key);
     private native boolean callNativeProcess(NativeFrame[] inputs, NativeFrame output);
+    private native boolean callNativeReset();
     private native boolean callNativeTeardown();
 }
index 9f5ce1a..1930648 100644 (file)
@@ -35,4 +35,7 @@ public abstract class Program {
     public abstract void setHostValue(String variableName, Object value);
 
     public abstract Object getHostValue(String variableName);
+
+    public void reset() {
+    }
 }
index eba2e27..c53e67d 100644 (file)
@@ -79,6 +79,15 @@ jboolean Java_android_filterfw_core_NativeProgram_bindProcessFunction(JNIEnv* en
   return ToJBool(program && func_name && program->BindProcessFunction(ToCppString(env, func_name)));
 }
 
+jboolean Java_android_filterfw_core_NativeProgram_bindResetFunction(JNIEnv* env,
+                                                                    jobject thiz,
+                                                                    jstring func_name) {
+  NativeProgram* program = ConvertFromJava<NativeProgram>(env, thiz);
+  return ToJBool(program &&
+                 func_name &&
+                 program->BindResetFunction(ToCppString(env, func_name)));
+}
+
 jboolean Java_android_filterfw_core_NativeProgram_bindTeardownFunction(JNIEnv* env,
                                                                        jobject thiz,
                                                                        jstring func_name) {
@@ -167,6 +176,11 @@ jboolean Java_android_filterfw_core_NativeProgram_callNativeProcess(JNIEnv* env,
   return ToJBool(program->CallProcess(input_buffers, input_sizes, output_data, output_size));
 }
 
+jboolean Java_android_filterfw_core_NativeProgram_callNativeReset(JNIEnv* env, jobject thiz) {
+  NativeProgram* program = ConvertFromJava<NativeProgram>(env, thiz);
+  return ToJBool(program && program->CallReset());
+}
+
 jboolean Java_android_filterfw_core_NativeProgram_callNativeTeardown(JNIEnv* env, jobject thiz) {
   NativeProgram* program = ConvertFromJava<NativeProgram>(env, thiz);
   return ToJBool(program && program->CallTeardown());
index 5d21cc4..fa97c39 100644 (file)
@@ -58,6 +58,11 @@ Java_android_filterfw_core_NativeProgram_bindProcessFunction(JNIEnv* env,
                                                              jstring func_name);
 
 JNIEXPORT jboolean JNICALL
+Java_android_filterfw_core_NativeProgram_bindResetFunction(JNIEnv* env,
+                                                           jobject thiz,
+                                                           jstring func_name);
+
+JNIEXPORT jboolean JNICALL
 Java_android_filterfw_core_NativeProgram_bindTeardownFunction(JNIEnv* env,
                                                               jobject thiz,
                                                               jstring func_name);
@@ -83,6 +88,9 @@ Java_android_filterfw_core_NativeProgram_callNativeProcess(JNIEnv* env,
                                                            jobject output);
 
 JNIEXPORT jboolean JNICALL
+Java_android_filterfw_core_NativeProgram_callNativeReset(JNIEnv* env, jobject thiz);
+
+JNIEXPORT jboolean JNICALL
 Java_android_filterfw_core_NativeProgram_callNativeTeardown(JNIEnv* env, jobject thiz);
 
 #ifdef __cplusplus
index 5f464bb..53c9113 100644 (file)
@@ -32,6 +32,7 @@ NativeProgram::NativeProgram()
       setvalue_function_(NULL),
       getvalue_function_(NULL),
       process_function_(NULL),
+      reset_function_(NULL),
       teardown_function_(NULL),
       user_data_(NULL) {
 }
@@ -85,6 +86,13 @@ bool NativeProgram::BindGetValueFunction(const std::string& func_name) {
   return getvalue_function_ != NULL;
 }
 
+bool NativeProgram::BindResetFunction(const std::string& func_name) {
+  if (!lib_handle_)
+    return false;
+  reset_function_ = reinterpret_cast<ResetFunctionPtr>(dlsym(lib_handle_, func_name.c_str()));
+  return reset_function_ != NULL;
+}
+
 bool NativeProgram::BindTeardownFunction(const std::string& func_name) {
   if (!lib_handle_)
     return false;
@@ -134,6 +142,14 @@ std::string NativeProgram::CallGetValue(const std::string& key) {
   return std::string();
 }
 
+bool NativeProgram::CallReset() {
+  if (reset_function_) {
+    reset_function_(user_data_);
+    return true;
+  }
+  return false;
+}
+
 bool NativeProgram::CallTeardown() {
   if (teardown_function_) {
     teardown_function_(user_data_);
index 743f9e9..ce704af 100644 (file)
@@ -31,6 +31,7 @@ typedef void (*InitFunctionPtr)(void**);
 typedef void (*SetValueFunctionPtr)(const char*, const char*, void*);
 typedef void (*GetValueFunctionPtr)(const char*, char*, int, void*);
 typedef int (*ProcessFunctionPtr)(const char**, const int*, int, char*, int, void*);
+typedef void (*ResetFunctionPtr)(void*);
 typedef void (*TeardownFunctionPtr)(void*);
 
 class NativeProgram {
@@ -46,6 +47,7 @@ class NativeProgram {
     bool BindSetValueFunction(const std::string& func_name);
     bool BindGetValueFunction(const std::string& func_name);
     bool BindProcessFunction(const std::string& func_name);
+    bool BindResetFunction(const std::string& func_name);
     bool BindTeardownFunction(const std::string& func_name);
 
     bool CallInit();
@@ -55,6 +57,7 @@ class NativeProgram {
                      const std::vector<int>& input_sizes,
                      char* output,
                      int output_size);
+    bool CallReset();
     bool CallTeardown();
 
   private:
@@ -66,6 +69,7 @@ class NativeProgram {
     SetValueFunctionPtr setvalue_function_;
     GetValueFunctionPtr getvalue_function_;
     ProcessFunctionPtr process_function_;
+    ResetFunctionPtr reset_function_;
     TeardownFunctionPtr teardown_function_;
 
     // Pointer to user data