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.filterfw;
19
20import android.filterfw.core.CachedFrameManager;
21import android.filterfw.core.FilterContext;
22import android.filterfw.core.FrameManager;
23import android.filterfw.core.GLEnvironment;
24
25/**
26 * Base class for mobile filter framework (MFF) frontend environments. These convenience classes
27 * allow using the filter framework without the requirement of performing manual setup of its
28 * required components.
29 *
30 * @hide
31 */
32public class MffEnvironment {
33
34    private FilterContext mContext;
35
36    /**
37     * Protected constructor to initialize the environment's essential components. These are the
38     * frame-manager and the filter-context. Passing in null for the frame-manager causes this
39     * to be auto-created.
40     *
41     * @param frameManager The FrameManager to use or null to auto-create one.
42     */
43    protected MffEnvironment(FrameManager frameManager) {
44        // Get or create the frame manager
45        if (frameManager == null) {
46            frameManager = new CachedFrameManager();
47        }
48
49        // Setup the environment
50        mContext = new FilterContext();
51        mContext.setFrameManager(frameManager);
52
53    }
54
55    /**
56     * Returns the environment's filter-context.
57     */
58    public FilterContext getContext() {
59        return mContext;
60    }
61
62    /**
63     * Set the environment's GL environment to the specified environment. This does not activate
64     * the environment.
65     */
66    public void setGLEnvironment(GLEnvironment glEnvironment) {
67        mContext.initGLEnvironment(glEnvironment);
68    }
69
70    /**
71     * Create and activate a new GL environment for use in this filter context.
72     */
73    public void createGLEnvironment() {
74        GLEnvironment glEnvironment = new GLEnvironment();
75        glEnvironment.initWithNewContext();
76        setGLEnvironment(glEnvironment);
77    }
78
79    /**
80     * Activate the GL environment for use in the current thread. A GL environment must have been
81     * previously set or created using setGLEnvironment() or createGLEnvironment()! Call this after
82     * having switched to a new thread for GL filter execution.
83     */
84    public void activateGLEnvironment() {
85        GLEnvironment glEnv = mContext.getGLEnvironment();
86        if (glEnv != null) {
87            mContext.getGLEnvironment().activate();
88        } else {
89            throw new NullPointerException("No GLEnvironment in place to activate!");
90        }
91    }
92
93    /**
94     * Deactivate the GL environment from use in the current thread. A GL environment must have been
95     * previously set or created using setGLEnvironment() or createGLEnvironment()! Call this before
96     * running GL filters in another thread.
97     */
98    public void deactivateGLEnvironment() {
99        GLEnvironment glEnv = mContext.getGLEnvironment();
100        if (glEnv != null) {
101            mContext.getGLEnvironment().deactivate();
102        } else {
103            throw new NullPointerException("No GLEnvironment in place to deactivate!");
104        }
105    }
106}
107