165953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn/* 265953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn * Copyright (C) 2011 The Android Open Source Project 365953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn * 465953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn * Licensed under the Apache License, Version 2.0 (the "License"); 565953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn * you may not use this file except in compliance with the License. 665953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn * You may obtain a copy of the License at 765953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn * 865953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn * http://www.apache.org/licenses/LICENSE-2.0 965953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn * 1065953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn * Unless required by applicable law or agreed to in writing, software 1165953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn * distributed under the License is distributed on an "AS IS" BASIS, 1265953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1365953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn * See the License for the specific language governing permissions and 1465953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn * limitations under the License. 1565953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn */ 1665953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn 1765953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn 1865953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Rennpackage android.filterfw; 1965953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn 2065953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Rennimport android.filterfw.core.Filter; 2165953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Rennimport android.filterfw.core.FilterFactory; 2265953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Rennimport android.filterfw.core.FilterFunction; 2365953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Rennimport android.filterfw.core.FrameManager; 2465953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn 2565953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn/** 2665953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn * A FilterFunctionEnvironment provides a simple functional front-end to manually executing 2765953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn * filters. Use this environment if a graph-based approach is not convenient for your case. 2865953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn * Typically, a FilterFunctionEnvironment is used as follows: 2965953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn * 1. Instantiate a new FilterFunctionEnvironment instance. 3065953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn * 2. Perform any configuration, such as setting a GL environment. 3165953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn * 3. Wrap Filters into FilterFunctions by calling createFunction(). 3265953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn * 4. Execute FilterFunctions individually and use the results for further processing. 3365953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn * Additionally, there is a convenience method to execute a number of filters in sequence. 3465953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn * @hide 3565953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn */ 3665953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Rennpublic class FilterFunctionEnvironment extends MffEnvironment { 3765953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn 3865953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn /** 3965953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn * Create a new FilterFunctionEnvironment with the default components. 4065953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn */ 4165953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn public FilterFunctionEnvironment() { 4265953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn super(null); 4365953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn } 4465953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn 4565953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn /** 4665953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn * Create a new FilterFunctionEnvironment with a custom FrameManager. Pass null to auto-create 4765953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn * a FrameManager. 4865953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn * 4965953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn * @param frameManager The FrameManager to use, or null to auto-create one. 5065953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn */ 5165953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn public FilterFunctionEnvironment(FrameManager frameManager) { 5265953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn super(frameManager); 5365953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn } 5465953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn 5565953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn /** 5665953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn * Create a new FilterFunction from a specific filter class. The function is initialized with 5765953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn * the given key-value list of parameters. Note, that this function uses the default shared 5865953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn * FilterFactory to create the filter instance. 5965953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn * 6065953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn * @param filterClass The class of the filter to wrap. This must be a Filter subclass. 6165953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn * @param parameters An argument list of alternating key-value filter parameters. 6265953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn * @return A new FilterFunction instance. 6365953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn */ 6465953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn public FilterFunction createFunction(Class filterClass, Object... parameters) { 6565953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn String filterName = "FilterFunction(" + filterClass.getSimpleName() + ")"; 6665953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn Filter filter = FilterFactory.sharedFactory().createFilterByClass(filterClass, filterName); 6765953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn filter.initWithAssignmentList(parameters); 6865953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn return new FilterFunction(getContext(), filter); 6965953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn } 7065953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn 7165953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn /** 7265953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn * Convenience method to execute a sequence of filter functions. Note that every function in 7365953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn * the list MUST have one input and one output port, except the first filter (which must not 7465953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn * have any input ports) and the last filter (which may not have any output ports). 7565953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn * 7665953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn * @param functions A list of filter functions. The first filter must be a source filter. 7765953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn * @return The result of the last filter executed, or null if the last filter did not 7865953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn produce any output. 7965953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn * 8065953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn public Frame executeSequence(FilterFunction[] functions) { 8165953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn Frame oldFrame = null; 8265953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn Frame newFrame = null; 8365953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn for (FilterFunction filterFunction : functions) { 8465953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn if (oldFrame == null) { 8565953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn newFrame = filterFunction.executeWithArgList(); 8665953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn } else { 8765953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn newFrame = filterFunction.executeWithArgList(oldFrame); 8865953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn oldFrame.release(); 8965953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn } 9065953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn oldFrame = newFrame; 9165953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn } 9265953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn if (oldFrame != null) { 9365953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn oldFrame.release(); 9465953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn } 9565953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn return newFrame; 9665953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn }*/ 9765953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn 9865953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn} 99