import android.filterfw.core.Frame;
import android.filterfw.core.Program;
+/**
+ * @hide
+ */
public class NativeProgram extends Program {
private int nativeProgramId;
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
// Initialize the native code
if (mHasInitFunction && !callNativeInit()) {
- throw new RuntimeException("Could not initialize NativeFrame!");
+ throw new RuntimeException("Could not initialize NativeProgram!");
}
}
- @Override
- 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 +"!");
// 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!");
@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!");
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();
}