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