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