2 * Copyright (C) 2011 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package android.filterfw.core;
20 import android.filterfw.core.Frame;
21 import android.filterfw.core.Program;
26 public class NativeProgram extends Program {
28 private int nativeProgramId;
29 private boolean mHasInitFunction = false;
30 private boolean mHasTeardownFunction = false;
31 private boolean mHasSetValueFunction = false;
32 private boolean mHasGetValueFunction = false;
34 public NativeProgram(String nativeLibName, String nativeFunctionPrefix) {
35 // Allocate the native instance
38 // Open the native library
39 String fullLibName = "lib" + nativeLibName + ".so";
40 if (!openNativeLibrary(fullLibName)) {
41 throw new RuntimeException("Could not find native library named '" + fullLibName + "' " +
42 "required for native program!");
45 // Bind the native functions
46 String processFuncName = nativeFunctionPrefix + "_process";
47 if (!bindProcessFunction(processFuncName)) {
48 throw new RuntimeException("Could not find native program function name " +
49 processFuncName + " in library " + fullLibName + "! " +
50 "This function is required!");
53 String initFuncName = nativeFunctionPrefix + "_init";
54 mHasInitFunction = bindInitFunction(initFuncName);
56 String teardownFuncName = nativeFunctionPrefix + "_teardown";
57 mHasTeardownFunction = bindTeardownFunction(teardownFuncName);
59 String setValueFuncName = nativeFunctionPrefix + "_setvalue";
60 mHasSetValueFunction = bindSetValueFunction(setValueFuncName);
62 String getValueFuncName = nativeFunctionPrefix + "_getvalue";
63 mHasGetValueFunction = bindGetValueFunction(getValueFuncName);
65 // Initialize the native code
66 if (mHasInitFunction && !callNativeInit()) {
67 throw new RuntimeException("Could not initialize NativeFrame!");
72 protected void finalize() throws Throwable {
73 if (mHasTeardownFunction && !callNativeTeardown()) {
74 throw new RuntimeException("Could not tear down NativeFrame!");
80 public void process(Frame[] inputs, Frame output) {
81 NativeFrame[] nativeInputs = new NativeFrame[inputs.length];
82 for (int i = 0; i < inputs.length; ++i) {
83 if (inputs[i] == null || inputs[i] instanceof NativeFrame) {
84 nativeInputs[i] = (NativeFrame)inputs[i];
86 throw new RuntimeException("NativeProgram got non-native frame as input "+ i +"!");
90 // Get the GL output frame
91 NativeFrame nativeOutput = null;
92 if (output == null || output instanceof NativeFrame) {
93 nativeOutput = (NativeFrame)output;
95 throw new RuntimeException("NativeProgram got non-native output frame!");
99 if (!callNativeProcess(nativeInputs, nativeOutput)) {
100 throw new RuntimeException("Calling native process() caused error!");
105 public void setHostValue(String variableName, Object value) {
106 if (!mHasSetValueFunction) {
107 throw new RuntimeException("Attempting to set native variable, but native code does not " +
108 "define native setvalue function!");
110 if (!callNativeSetValue(variableName, value.toString())) {
111 throw new RuntimeException("Error setting native value for variable '" + variableName + "'!");
116 public Object getHostValue(String variableName) {
117 if (!mHasGetValueFunction) {
118 throw new RuntimeException("Attempting to get native variable, but native code does not " +
119 "define native getvalue function!");
121 return callNativeGetValue(variableName);
125 System.loadLibrary("filterfw");
128 private native boolean allocate();
130 private native boolean deallocate();
132 private native boolean nativeInit();
134 private native boolean openNativeLibrary(String libName);
136 private native boolean bindInitFunction(String funcName);
137 private native boolean bindSetValueFunction(String funcName);
138 private native boolean bindGetValueFunction(String funcName);
139 private native boolean bindProcessFunction(String funcName);
140 private native boolean bindTeardownFunction(String funcName);
142 private native boolean callNativeInit();
143 private native boolean callNativeSetValue(String key, String value);
144 private native String callNativeGetValue(String key);
145 private native boolean callNativeProcess(NativeFrame[] inputs, NativeFrame output);
146 private native boolean callNativeTeardown();