OSDN Git Service

Merge "Removed dependencies of native filter program implementations on MFF headers."
[android-x86/system-media.git] / mca / filterfw / java / android / filterfw / core / NativeProgram.java
1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17
18 package android.filterfw.core;
19
20 import android.filterfw.core.Frame;
21 import android.filterfw.core.Program;
22
23 /**
24  * @hide
25  */
26 public class NativeProgram extends Program {
27
28     private int nativeProgramId;
29     private boolean mHasInitFunction     = false;
30     private boolean mHasTeardownFunction = false;
31     private boolean mHasSetValueFunction = false;
32     private boolean mHasGetValueFunction = false;
33
34     public NativeProgram(String nativeLibName, String nativeFunctionPrefix) {
35         // Allocate the native instance
36         allocate();
37
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!");
43         }
44
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!");
51         }
52
53         String initFuncName = nativeFunctionPrefix + "_init";
54         mHasInitFunction = bindInitFunction(initFuncName);
55
56         String teardownFuncName = nativeFunctionPrefix + "_teardown";
57         mHasTeardownFunction = bindTeardownFunction(teardownFuncName);
58
59         String setValueFuncName = nativeFunctionPrefix + "_setvalue";
60         mHasSetValueFunction = bindSetValueFunction(setValueFuncName);
61
62         String getValueFuncName = nativeFunctionPrefix + "_getvalue";
63         mHasGetValueFunction = bindGetValueFunction(getValueFuncName);
64
65         // Initialize the native code
66         if (mHasInitFunction && !callNativeInit()) {
67             throw new RuntimeException("Could not initialize NativeFrame!");
68         }
69     }
70
71     @Override
72     protected void finalize() throws Throwable {
73         if (mHasTeardownFunction && !callNativeTeardown()) {
74             throw new RuntimeException("Could not tear down NativeFrame!");
75         }
76         deallocate();
77     }
78
79     @Override
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];
85             } else {
86                 throw new RuntimeException("NativeProgram got non-native frame as input "+ i +"!");
87             }
88         }
89
90         // Get the GL output frame
91         NativeFrame nativeOutput = null;
92         if (output == null || output instanceof NativeFrame) {
93             nativeOutput = (NativeFrame)output;
94         } else {
95             throw new RuntimeException("NativeProgram got non-native output frame!");
96         }
97
98         // Process!
99         if (!callNativeProcess(nativeInputs, nativeOutput)) {
100             throw new RuntimeException("Calling native process() caused error!");
101         }
102     }
103
104     @Override
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!");
109         }
110         if (!callNativeSetValue(variableName, value.toString())) {
111             throw new RuntimeException("Error setting native value for variable '" + variableName + "'!");
112         }
113     }
114
115     @Override
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!");
120         }
121         return callNativeGetValue(variableName);
122     }
123
124     static {
125         System.loadLibrary("filterfw");
126     }
127
128     private native boolean allocate();
129
130     private native boolean deallocate();
131
132     private native boolean nativeInit();
133
134     private native boolean openNativeLibrary(String libName);
135
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);
141
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();
147 }