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