OSDN Git Service

Graph and NativeProgram tearDown.
[android-x86/system-media.git] / mca / filterfw / java / android / filterfw / core / NativeProgram.java
index 95bd05e..fe5911b 100644 (file)
@@ -20,8 +20,9 @@ package android.filterfw.core;
 import android.filterfw.core.Frame;
 import android.filterfw.core.Program;
 
-import android.util.Log;
-
+/**
+ * @hide
+ */
 public class NativeProgram extends Program {
 
     private int nativeProgramId;
@@ -29,6 +30,7 @@ public class NativeProgram extends Program {
     private boolean mHasTeardownFunction = false;
     private boolean mHasSetValueFunction = false;
     private boolean mHasGetValueFunction = false;
+    private boolean mTornDown = false;
 
     public NativeProgram(String nativeLibName, String nativeFunctionPrefix) {
         // Allocate the native instance
@@ -63,21 +65,32 @@ public class NativeProgram extends Program {
 
         // Initialize the native code
         if (mHasInitFunction && !callNativeInit()) {
-            throw new RuntimeException("Could not initialize NativeFrame!");
+            throw new RuntimeException("Could not initialize NativeProgram!");
         }
     }
 
-    protected void finalize() throws Throwable {
+    public  void tearDown() {
+        if (mTornDown) return;
         if (mHasTeardownFunction && !callNativeTeardown()) {
-            throw new RuntimeException("Could not tear down NativeFrame!");
+            throw new RuntimeException("Could not tear down NativeProgram!");
         }
         deallocate();
+        mTornDown = true;
     }
 
+    @Override
+    protected void finalize() throws Throwable {
+        tearDown();
+    }
+
+    @Override
     public void process(Frame[] inputs, Frame output) {
+        if (mTornDown) {
+            throw new RuntimeException("NativeProgram already torn down!");
+        }
         NativeFrame[] nativeInputs = new NativeFrame[inputs.length];
         for (int i = 0; i < inputs.length; ++i) {
-            if (inputs[i] instanceof NativeFrame) {
+            if (inputs[i] == null || inputs[i] instanceof NativeFrame) {
                 nativeInputs[i] = (NativeFrame)inputs[i];
             } else {
                 throw new RuntimeException("NativeProgram got non-native frame as input "+ i +"!");
@@ -86,7 +99,7 @@ public class NativeProgram extends Program {
 
         // Get the GL output frame
         NativeFrame nativeOutput = null;
-        if (output instanceof NativeFrame) {
+        if (output == null || output instanceof NativeFrame) {
             nativeOutput = (NativeFrame)output;
         } else {
             throw new RuntimeException("NativeProgram got non-native output frame!");
@@ -98,17 +111,25 @@ public class NativeProgram extends Program {
         }
     }
 
+    @Override
     public void setHostValue(String variableName, Object value) {
+        if (mTornDown) {
+            throw new RuntimeException("NativeProgram already torn down!");
+        }
         if (!mHasSetValueFunction) {
             throw new RuntimeException("Attempting to set native variable, but native code does not " +
                                        "define native setvalue function!");
         }
-        if (!callNativeSetValue(variableName, value)) {
+        if (!callNativeSetValue(variableName, value.toString())) {
             throw new RuntimeException("Error setting native value for variable '" + variableName + "'!");
         }
     }
 
+    @Override
     public Object getHostValue(String variableName) {
+        if (mTornDown) {
+            throw new RuntimeException("NativeProgram already torn down!");
+        }
         if (!mHasGetValueFunction) {
             throw new RuntimeException("Attempting to get native variable, but native code does not " +
                                        "define native getvalue function!");
@@ -117,7 +138,7 @@ public class NativeProgram extends Program {
     }
 
     static {
-        System.loadLibrary("filterfw2-jni");
+        System.loadLibrary("filterfw");
     }
 
     private native boolean allocate();
@@ -135,8 +156,8 @@ public class NativeProgram extends Program {
     private native boolean bindTeardownFunction(String funcName);
 
     private native boolean callNativeInit();
-    private native boolean callNativeSetValue(String key, Object value);
-    private native Object  callNativeGetValue(String key);
+    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 callNativeTeardown();
 }