FilterGraphEffect.java revision 6090995951c6e2e4dcf38102f01793f8a94166e1
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
17package android.media.effect;
18
19import android.filterfw.core.Filter;
20import android.filterfw.core.FilterGraph;
21import android.filterfw.core.GraphRunner;
22import android.filterfw.core.SyncRunner;
23import android.media.effect.FilterEffect;
24import android.media.effect.EffectContext;
25import android.filterfw.io.GraphIOException;
26import android.filterfw.io.GraphReader;
27import android.filterfw.io.TextGraphReader;
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 FilterGraphEffect extends FilterEffect {
36
37    private static final String TAG = "FilterGraphEffect";
38
39    protected String mInputName;
40    protected String mOutputName;
41    protected GraphRunner mRunner;
42    protected FilterGraph mGraph;
43    protected Class mSchedulerClass;
44
45    /**
46     * Constructs a new FilterGraphEffect.
47     *
48     * @param name The name of this effect (used to create it in the EffectFactory).
49     * @param graphString The graph string to create the graph.
50     * @param inputName The name of the input GLTextureSource filter.
51     * @param outputName The name of the output GLTextureSource filter.
52     */
53    public FilterGraphEffect(EffectContext context,
54                              String name,
55                              String graphString,
56                              String inputName,
57                              String outputName,
58                              Class scheduler) {
59        super(context, name);
60
61        mInputName = inputName;
62        mOutputName = outputName;
63        mSchedulerClass = scheduler;
64        createGraph(graphString);
65
66    }
67
68    private void createGraph(String graphString) {
69        GraphReader reader = new TextGraphReader();
70        try {
71            mGraph = reader.readGraphString(graphString);
72        } catch (GraphIOException e) {
73            throw new RuntimeException("Could not setup effect", e);
74        }
75
76        if (mGraph == null) {
77            throw new RuntimeException("Could not setup effect");
78        }
79        mRunner = new SyncRunner(getFilterContext(), mGraph, mSchedulerClass);
80    }
81
82    @Override
83    public void apply(int inputTexId, int width, int height, int outputTexId) {
84        beginGLEffect();
85        Filter src = mGraph.getFilter(mInputName);
86        if (src != null) {
87            src.setInputValue("texId", inputTexId);
88            src.setInputValue("width", width);
89            src.setInputValue("height", height);
90        } else {
91            throw new RuntimeException("Internal error applying effect");
92        }
93        Filter dest  = mGraph.getFilter(mOutputName);
94        if (dest != null) {
95            dest.setInputValue("texId", outputTexId);
96        } else {
97            throw new RuntimeException("Internal error applying effect");
98        }
99        try {
100            mRunner.run();
101        } catch (RuntimeException e) {
102            throw new RuntimeException("Internal error applying effect: ", e);
103        }
104        endGLEffect();
105    }
106
107    @Override
108    public void setParameter(String parameterKey, Object value) {
109    }
110
111    @Override
112    public void release() {
113         mGraph.tearDown(getFilterContext());
114         mGraph = null;
115    }
116}
117