OSDN Git Service

Merge core filter framework libraries into one.
[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 import android.util.Log;
24
25 public class NativeProgram extends Program {
26
27     private int nativeProgramId;
28     private boolean mHasInitFunction     = false;
29     private boolean mHasTeardownFunction = false;
30     private boolean mHasSetValueFunction = false;
31     private boolean mHasGetValueFunction = false;
32
33     public NativeProgram(String nativeLibName, String nativeFunctionPrefix) {
34         // Allocate the native instance
35         allocate();
36
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!");
42         }
43
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!");
50         }
51
52         String initFuncName = nativeFunctionPrefix + "_init";
53         mHasInitFunction = bindInitFunction(initFuncName);
54
55         String teardownFuncName = nativeFunctionPrefix + "_teardown";
56         mHasTeardownFunction = bindTeardownFunction(teardownFuncName);
57
58         String setValueFuncName = nativeFunctionPrefix + "_setvalue";
59         mHasSetValueFunction = bindSetValueFunction(setValueFuncName);
60
61         String getValueFuncName = nativeFunctionPrefix + "_getvalue";
62         mHasGetValueFunction = bindGetValueFunction(getValueFuncName);
63
64         // Initialize the native code
65         if (mHasInitFunction && !callNativeInit()) {
66             throw new RuntimeException("Could not initialize NativeFrame!");
67         }
68     }
69
70     protected void finalize() throws Throwable {
71         if (mHasTeardownFunction && !callNativeTeardown()) {
72             throw new RuntimeException("Could not tear down NativeFrame!");
73         }
74         deallocate();
75     }
76
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];
82             } else {
83                 throw new RuntimeException("NativeProgram got non-native frame as input "+ i +"!");
84             }
85         }
86
87         // Get the GL output frame
88         NativeFrame nativeOutput = null;
89         if (output instanceof NativeFrame) {
90             nativeOutput = (NativeFrame)output;
91         } else {
92             throw new RuntimeException("NativeProgram got non-native output frame!");
93         }
94
95         // Process!
96         if (!callNativeProcess(nativeInputs, nativeOutput)) {
97             throw new RuntimeException("Calling native process() caused error!");
98         }
99     }
100
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!");
105         }
106         if (!callNativeSetValue(variableName, value)) {
107             throw new RuntimeException("Error setting native value for variable '" + variableName + "'!");
108         }
109     }
110
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!");
115         }
116         return callNativeGetValue(variableName);
117     }
118
119     static {
120         System.loadLibrary("filterfw");
121     }
122
123     private native boolean allocate();
124
125     private native boolean deallocate();
126
127     private native boolean nativeInit();
128
129     private native boolean openNativeLibrary(String libName);
130
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);
136
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();
142 }