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 public class NativeProgram extends Program {
25 private int nativeProgramId;
26 private boolean mHasInitFunction = false;
27 private boolean mHasTeardownFunction = false;
28 private boolean mHasSetValueFunction = false;
29 private boolean mHasGetValueFunction = false;
31 public NativeProgram(String nativeLibName, String nativeFunctionPrefix) {
32 // Allocate the native instance
35 // Open the native library
36 String fullLibName = "lib" + nativeLibName + ".so";
37 if (!openNativeLibrary(fullLibName)) {
38 throw new RuntimeException("Could not find native library named '" + fullLibName + "' " +
39 "required for native program!");
42 // Bind the native functions
43 String processFuncName = nativeFunctionPrefix + "_process";
44 if (!bindProcessFunction(processFuncName)) {
45 throw new RuntimeException("Could not find native program function name " +
46 processFuncName + " in library " + fullLibName + "! " +
47 "This function is required!");
50 String initFuncName = nativeFunctionPrefix + "_init";
51 mHasInitFunction = bindInitFunction(initFuncName);
53 String teardownFuncName = nativeFunctionPrefix + "_teardown";
54 mHasTeardownFunction = bindTeardownFunction(teardownFuncName);
56 String setValueFuncName = nativeFunctionPrefix + "_setvalue";
57 mHasSetValueFunction = bindSetValueFunction(setValueFuncName);
59 String getValueFuncName = nativeFunctionPrefix + "_getvalue";
60 mHasGetValueFunction = bindGetValueFunction(getValueFuncName);
62 // Initialize the native code
63 if (mHasInitFunction && !callNativeInit()) {
64 throw new RuntimeException("Could not initialize NativeFrame!");
69 protected void finalize() throws Throwable {
70 if (mHasTeardownFunction && !callNativeTeardown()) {
71 throw new RuntimeException("Could not tear down NativeFrame!");
76 public void process(Frame[] inputs, Frame output) {
77 NativeFrame[] nativeInputs = new NativeFrame[inputs.length];
78 for (int i = 0; i < inputs.length; ++i) {
79 if (inputs[i] instanceof NativeFrame) {
80 nativeInputs[i] = (NativeFrame)inputs[i];
82 throw new RuntimeException("NativeProgram got non-native frame as input "+ i +"!");
86 // Get the GL output frame
87 NativeFrame nativeOutput = null;
88 if (output instanceof NativeFrame) {
89 nativeOutput = (NativeFrame)output;
91 throw new RuntimeException("NativeProgram got non-native output frame!");
95 if (!callNativeProcess(nativeInputs, nativeOutput)) {
96 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 + "'!");
112 public Object getHostValue(String variableName) {
113 if (!mHasGetValueFunction) {
114 throw new RuntimeException("Attempting to get native variable, but native code does not " +
115 "define native getvalue function!");
117 return callNativeGetValue(variableName);
121 System.loadLibrary("filterfw");
124 private native boolean allocate();
126 private native boolean deallocate();
128 private native boolean nativeInit();
130 private native boolean openNativeLibrary(String libName);
132 private native boolean bindInitFunction(String funcName);
133 private native boolean bindSetValueFunction(String funcName);
134 private native boolean bindGetValueFunction(String funcName);
135 private native boolean bindProcessFunction(String funcName);
136 private native boolean bindTeardownFunction(String funcName);
138 private native boolean callNativeInit();
139 private native boolean callNativeSetValue(String key, Object value);
140 private native Object callNativeGetValue(String key);
141 private native boolean callNativeProcess(NativeFrame[] inputs, NativeFrame output);
142 private native boolean callNativeTeardown();