FileDocCategorySizeDatePackage
NativeProgram.javaAPI DocAndroid 5.1 API6534Thu Mar 12 22:22:30 GMT 2015android.filterfw.core

NativeProgram

public class NativeProgram extends android.filterfw.core.Program
hide

Fields Summary
private int
nativeProgramId
private boolean
mHasInitFunction
private boolean
mHasTeardownFunction
private boolean
mHasSetValueFunction
private boolean
mHasGetValueFunction
private boolean
mHasResetFunction
private boolean
mTornDown
Constructors Summary
public NativeProgram(String nativeLibName, String nativeFunctionPrefix)


         
        // Allocate the native instance
        allocate();

        // Open the native library
        String fullLibName = "lib" + nativeLibName + ".so";
        if (!openNativeLibrary(fullLibName)) {
            throw new RuntimeException("Could not find native library named '" + fullLibName + "' " +
                                       "required for native program!");
        }

        // Bind the native functions
        String processFuncName = nativeFunctionPrefix + "_process";
        if (!bindProcessFunction(processFuncName)) {
            throw new RuntimeException("Could not find native program function name " +
                                       processFuncName + " in library " + fullLibName + "! " +
                                       "This function is required!");
        }

        String initFuncName = nativeFunctionPrefix + "_init";
        mHasInitFunction = bindInitFunction(initFuncName);

        String teardownFuncName = nativeFunctionPrefix + "_teardown";
        mHasTeardownFunction = bindTeardownFunction(teardownFuncName);

        String setValueFuncName = nativeFunctionPrefix + "_setvalue";
        mHasSetValueFunction = bindSetValueFunction(setValueFuncName);

        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!");
        }
    
Methods Summary
private native booleanallocate()

private native booleanbindGetValueFunction(java.lang.String funcName)

private native booleanbindInitFunction(java.lang.String funcName)

private native booleanbindProcessFunction(java.lang.String funcName)

private native booleanbindResetFunction(java.lang.String funcName)

private native booleanbindSetValueFunction(java.lang.String funcName)

private native booleanbindTeardownFunction(java.lang.String funcName)

private native java.lang.StringcallNativeGetValue(java.lang.String key)

private native booleancallNativeInit()

private native booleancallNativeProcess(NativeFrame[] inputs, NativeFrame output)

private native booleancallNativeReset()

private native booleancallNativeSetValue(java.lang.String key, java.lang.String value)

private native booleancallNativeTeardown()

private native booleandeallocate()

protected voidfinalize()

        tearDown();
    
public java.lang.ObjectgetHostValue(java.lang.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!");
        }
        return callNativeGetValue(variableName);
    
private native booleannativeInit()

private native booleanopenNativeLibrary(java.lang.String libName)

public voidprocess(android.filterfw.core.Frame[] inputs, android.filterfw.core.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] == null || inputs[i] instanceof NativeFrame) {
                nativeInputs[i] = (NativeFrame)inputs[i];
            } else {
                throw new RuntimeException("NativeProgram got non-native frame as input "+ i +"!");
            }
        }

        // Get the native output frame
        NativeFrame nativeOutput = null;
        if (output == null || output instanceof NativeFrame) {
            nativeOutput = (NativeFrame)output;
        } else {
            throw new RuntimeException("NativeProgram got non-native output frame!");
        }

        // Process!
        if (!callNativeProcess(nativeInputs, nativeOutput)) {
            throw new RuntimeException("Calling native process() caused error!");
        }
    
public voidreset()

        if (mHasResetFunction && !callNativeReset()) {
            throw new RuntimeException("Could not reset NativeProgram!");
        }
    
public voidsetHostValue(java.lang.String variableName, java.lang.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.toString())) {
            throw new RuntimeException("Error setting native value for variable '" + variableName + "'!");
        }
    
public voidtearDown()

        if (mTornDown) return;
        if (mHasTeardownFunction && !callNativeTeardown()) {
            throw new RuntimeException("Could not tear down NativeProgram!");
        }
        deallocate();
        mTornDown = true;