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;
19 
20 import android.filterfw.core.Filter;
21 import android.filterfw.core.FilterFactory;
22 import android.filterfw.core.FilterFunction;
23 import android.filterfw.core.FrameManager;
24 
25 /**
26  * A FilterFunctionEnvironment provides a simple functional front-end to manually executing
27  * filters. Use this environment if a graph-based approach is not convenient for your case.
28  * Typically, a FilterFunctionEnvironment is used as follows:
29  *   1. Instantiate a new FilterFunctionEnvironment instance.
30  *   2. Perform any configuration, such as setting a GL environment.
31  *   3. Wrap Filters into FilterFunctions by calling createFunction().
32  *   4. Execute FilterFunctions individually and use the results for further processing.
33  * Additionally, there is a convenience method to execute a number of filters in sequence.
34  * @hide
35  */
36 public class FilterFunctionEnvironment extends MffEnvironment {
37 
38     /**
39      * Create a new FilterFunctionEnvironment with the default components.
40      */
FilterFunctionEnvironment()41     public FilterFunctionEnvironment() {
42         super(null);
43     }
44 
45     /**
46      * Create a new FilterFunctionEnvironment with a custom FrameManager. Pass null to auto-create
47      * a FrameManager.
48      *
49      * @param frameManager The FrameManager to use, or null to auto-create one.
50      */
FilterFunctionEnvironment(FrameManager frameManager)51     public FilterFunctionEnvironment(FrameManager frameManager) {
52         super(frameManager);
53     }
54 
55     /**
56      * Create a new FilterFunction from a specific filter class. The function is initialized with
57      * the given key-value list of parameters. Note, that this function uses the default shared
58      * FilterFactory to create the filter instance.
59      *
60      * @param filterClass   The class of the filter to wrap. This must be a Filter subclass.
61      * @param parameters    An argument list of alternating key-value filter parameters.
62      * @return             A new FilterFunction instance.
63      */
createFunction(Class filterClass, Object... parameters)64     public FilterFunction createFunction(Class filterClass, Object... parameters) {
65         String filterName = "FilterFunction(" + filterClass.getSimpleName() + ")";
66         Filter filter = FilterFactory.sharedFactory().createFilterByClass(filterClass, filterName);
67         filter.initWithAssignmentList(parameters);
68         return new FilterFunction(getContext(), filter);
69     }
70 
71     /**
72      * Convenience method to execute a sequence of filter functions. Note that every function in
73      * the list MUST have one input and one output port, except the first filter (which must not
74      * have any input ports) and the last filter (which may not have any output ports).
75      *
76      * @param functions A list of filter functions. The first filter must be a source filter.
77      * @return         The result of the last filter executed, or null if the last filter did not
78                         produce any output.
79      *
80     public Frame executeSequence(FilterFunction[] functions) {
81         Frame oldFrame = null;
82         Frame newFrame = null;
83         for (FilterFunction filterFunction : functions) {
84             if (oldFrame == null) {
85                 newFrame = filterFunction.executeWithArgList();
86             } else {
87                 newFrame = filterFunction.executeWithArgList(oldFrame);
88                 oldFrame.release();
89             }
90             oldFrame = newFrame;
91         }
92         if (oldFrame != null) {
93             oldFrame.release();
94         }
95         return newFrame;
96     }*/
97 
98 }
99