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;
23 import android.util.Log;
25 public class NativeProgram extends Program {
27 private int nativeProgramId;
28 private boolean mHasInitFunction = false;
29 private boolean mHasTeardownFunction = false;
30 private boolean mHasSetValueFunction = false;
31 private boolean mHasGetValueFunction = false;
33 public NativeProgram(String nativeLibName, String nativeFunctionPrefix) {
34 // Allocate the native instance
37 // Open the native library
38 String fullLibName = "lib" + nativeLibName + ".so";
39 if (!openNativeLibrary(fullLibName)) {
40 throw new RuntimeException("Could not find native library named '" + fullLibName + "' " +
41 "required for native program!");
44 // Bind the native functions
45 String processFuncName = nativeFunctionPrefix + "_process";
46 if (!bindProcessFunction(processFuncName)) {
47 throw new RuntimeException("Could not find native program function name " +
48 processFuncName + " in library " + fullLibName + "! " +
49 "This function is required!");
52 String initFuncName = nativeFunctionPrefix + "_init";
53 mHasInitFunction = bindInitFunction(initFuncName);
55 String teardownFuncName = nativeFunctionPrefix + "_teardown";
56 mHasTeardownFunction = bindTeardownFunction(teardownFuncName);
58 String setValueFuncName = nativeFunctionPrefix + "_setvalue";
59 mHasSetValueFunction = bindSetValueFunction(setValueFuncName);
61 String getValueFuncName = nativeFunctionPrefix + "_getvalue";
62 mHasGetValueFunction = bindGetValueFunction(getValueFuncName);
64 // Initialize the native code
65 if (mHasInitFunction && !callNativeInit()) {
66 throw new RuntimeException("Could not initialize NativeFrame!");
70 protected void finalize() throws Throwable {
71 if (mHasTeardownFunction && !callNativeTeardown()) {
72 throw new RuntimeException("Could not tear down NativeFrame!");
77 public void process(Frame[] inputs, Frame output) {
78 NativeFrame[] nativeInputs = new NativeFrame[inputs.length];
79 for (int i = 0; i < inputs.length; ++i) {
80 if (inputs[i] instanceof NativeFrame) {
81 nativeInputs[i] = (NativeFrame)inputs[i];
83 throw new RuntimeException("NativeProgram got non-native frame as input "+ i +"!");
87 // Get the GL output frame
88 NativeFrame nativeOutput = null;
89 if (output instanceof NativeFrame) {
90 nativeOutput = (NativeFrame)output;
92 throw new RuntimeException("NativeProgram got non-native output frame!");
96 if (!callNativeProcess(nativeInputs, nativeOutput)) {
97 throw new RuntimeException("Calling native process() caused error!");
101 public void setHostValue(String variableName, Object value) {
102 if (!mHasSetValueFunction) {
103 throw new RuntimeException("Attempting to set native variable, but native code does not " +
104 "define native setvalue function!");
106 if (!callNativeSetValue(variableName, value)) {
107 throw new RuntimeException("Error setting native value for variable '" + variableName + "'!");
111 public Object getHostValue(String variableName) {
112 if (!mHasGetValueFunction) {
113 throw new RuntimeException("Attempting to get native variable, but native code does not " +
114 "define native getvalue function!");
116 return callNativeGetValue(variableName);
120 System.loadLibrary("filterfw");
123 private native boolean allocate();
125 private native boolean deallocate();
127 private native boolean nativeInit();
129 private native boolean openNativeLibrary(String libName);
131 private native boolean bindInitFunction(String funcName);
132 private native boolean bindSetValueFunction(String funcName);
133 private native boolean bindGetValueFunction(String funcName);
134 private native boolean bindProcessFunction(String funcName);
135 private native boolean bindTeardownFunction(String funcName);
137 private native boolean callNativeInit();
138 private native boolean callNativeSetValue(String key, Object value);
139 private native Object callNativeGetValue(String key);
140 private native boolean callNativeProcess(NativeFrame[] inputs, NativeFrame output);
141 private native boolean callNativeTeardown();