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.media.effect;
19
20import android.filterfw.core.Filter;
21import android.filterfw.core.FilterFactory;
22import android.filterfw.core.FilterFunction;
23import android.filterfw.core.Frame;
24import android.media.effect.Effect;
25import android.media.effect.EffectContext;
26
27import android.util.Log;
28
29/**
30 * Effect subclass for effects based on a single Filter. Subclasses need only invoke the
31 * constructor with the correct arguments to obtain an Effect implementation.
32 *
33 * @hide
34 */
35public class SingleFilterEffect extends FilterEffect {
36
37    protected FilterFunction mFunction;
38    protected String mInputName;
39    protected String mOutputName;
40
41    /**
42     * Constructs a new FilterFunctionEffect.
43     *
44     * @param name The name of this effect (used to create it in the EffectFactory).
45     * @param filterClass The class of the filter to wrap.
46     * @param inputName The name of the input image port.
47     * @param outputName The name of the output image port.
48     * @param finalParameters Key-value pairs of final input port assignments.
49     */
50    public SingleFilterEffect(EffectContext context,
51                              String name,
52                              Class filterClass,
53                              String inputName,
54                              String outputName,
55                              Object... finalParameters) {
56        super(context, name);
57
58        mInputName = inputName;
59        mOutputName = outputName;
60
61        String filterName = filterClass.getSimpleName();
62        FilterFactory factory = FilterFactory.sharedFactory();
63        Filter filter = factory.createFilterByClass(filterClass, filterName);
64        filter.initWithAssignmentList(finalParameters);
65
66        mFunction = new FilterFunction(getFilterContext(), filter);
67    }
68
69    @Override
70    public void apply(int inputTexId, int width, int height, int outputTexId) {
71        beginGLEffect();
72
73        Frame inputFrame = frameFromTexture(inputTexId, width, height);
74        Frame outputFrame = frameFromTexture(outputTexId, width, height);
75
76        Frame resultFrame = mFunction.executeWithArgList(mInputName, inputFrame);
77
78        outputFrame.setDataFromFrame(resultFrame);
79
80        inputFrame.release();
81        outputFrame.release();
82        resultFrame.release();
83
84        endGLEffect();
85    }
86
87    @Override
88    public void setParameter(String parameterKey, Object value) {
89        mFunction.setInputValue(parameterKey, value);
90    }
91
92    @Override
93    public void release() {
94        mFunction.tearDown();
95        mFunction = null;
96    }
97}
98
99