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.FilterContext;
21import android.filterfw.core.GLFrame;
22import android.filterfw.core.Frame;
23import android.filterfw.core.FrameFormat;
24import android.filterfw.core.FrameManager;
25import android.filterfw.format.ImageFormat;
26
27/**
28 * The FilterEffect class is the base class for all Effects based on Filters from the Mobile
29 * Filter Framework (MFF).
30 * @hide
31 */
32public abstract class FilterEffect extends Effect {
33
34    protected EffectContext mEffectContext;
35    private String mName;
36
37    /**
38     * Protected constructor as FilterEffects should be created by Factory.
39     */
40    protected FilterEffect(EffectContext context, String name) {
41        mEffectContext = context;
42        mName = name;
43    }
44
45    /**
46     * Get the effect name.
47     *
48     * Returns the unique name of the effect, which matches the name used for instantiating this
49     * effect by the EffectFactory.
50     *
51     * @return The name of the effect.
52     */
53    @Override
54    public String getName() {
55        return mName;
56    }
57
58    // Helper Methods for subclasses ///////////////////////////////////////////////////////////////
59    /**
60     * Call this before manipulating the GL context. Will assert that the GL environment is in a
61     * valid state, and save it.
62     */
63    protected void beginGLEffect() {
64        mEffectContext.assertValidGLState();
65        mEffectContext.saveGLState();
66    }
67
68    /**
69     * Call this after manipulating the GL context. Restores the previous GL state.
70     */
71    protected void endGLEffect() {
72        mEffectContext.restoreGLState();
73    }
74
75    /**
76     * Returns the active filter context for this effect.
77     */
78    protected FilterContext getFilterContext() {
79        return mEffectContext.mFilterContext;
80    }
81
82    /**
83     * Converts a texture into a Frame.
84     */
85    protected Frame frameFromTexture(int texId, int width, int height) {
86        FrameManager manager = getFilterContext().getFrameManager();
87        FrameFormat format = ImageFormat.create(width, height,
88                                                ImageFormat.COLORSPACE_RGBA,
89                                                FrameFormat.TARGET_GPU);
90        Frame frame = manager.newBoundFrame(format,
91                                            GLFrame.EXISTING_TEXTURE_BINDING,
92                                            texId);
93        frame.setTimestamp(Frame.TIMESTAMP_UNKNOWN);
94        return frame;
95    }
96
97}
98
99