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 
18 package android.filterfw.core;
19 
20 import android.filterfw.core.Filter;
21 import android.filterfw.core.Frame;
22 import android.filterfw.core.FrameManager;
23 import android.filterfw.core.GLEnvironment;
24 
25 import java.util.HashMap;
26 import java.util.HashSet;
27 import java.util.Set;
28 
29 /**
30  * @hide
31  */
32 public class FilterContext {
33 
34     private FrameManager mFrameManager;
35     private GLEnvironment mGLEnvironment;
36     private HashMap<String, Frame> mStoredFrames = new HashMap<String, Frame>();
37     private Set<FilterGraph> mGraphs = new HashSet<FilterGraph>();
38 
getFrameManager()39     public FrameManager getFrameManager() {
40         return mFrameManager;
41     }
42 
setFrameManager(FrameManager manager)43     public void setFrameManager(FrameManager manager) {
44         if (manager == null) {
45             throw new NullPointerException("Attempting to set null FrameManager!");
46         } else if (manager.getContext() != null) {
47             throw new IllegalArgumentException("Attempting to set FrameManager which is already "
48                 + "bound to another FilterContext!");
49         } else {
50             mFrameManager = manager;
51             mFrameManager.setContext(this);
52         }
53     }
54 
getGLEnvironment()55     public GLEnvironment getGLEnvironment() {
56         return mGLEnvironment;
57     }
58 
initGLEnvironment(GLEnvironment environment)59     public void initGLEnvironment(GLEnvironment environment) {
60         if (mGLEnvironment == null) {
61             mGLEnvironment = environment;
62         } else {
63             throw new RuntimeException("Attempting to re-initialize GL Environment for " +
64                 "FilterContext!");
65         }
66     }
67 
68     public interface OnFrameReceivedListener {
onFrameReceived(Filter filter, Frame frame, Object userData)69         public void onFrameReceived(Filter filter, Frame frame, Object userData);
70     }
71 
storeFrame(String key, Frame frame)72     public synchronized void storeFrame(String key, Frame frame) {
73         Frame storedFrame = fetchFrame(key);
74         if (storedFrame != null) {
75             storedFrame.release();
76         }
77         frame.onFrameStore();
78         mStoredFrames.put(key, frame.retain());
79     }
80 
fetchFrame(String key)81     public synchronized Frame fetchFrame(String key) {
82         Frame frame = mStoredFrames.get(key);
83         if (frame != null) {
84             frame.onFrameFetch();
85         }
86         return frame;
87     }
88 
removeFrame(String key)89     public synchronized void removeFrame(String key) {
90         Frame frame = mStoredFrames.get(key);
91         if (frame != null) {
92             mStoredFrames.remove(key);
93             frame.release();
94         }
95     }
96 
tearDown()97     public synchronized void tearDown() {
98         // Release stored frames
99         for (Frame frame : mStoredFrames.values()) {
100             frame.release();
101         }
102         mStoredFrames.clear();
103 
104         // Release graphs
105         for (FilterGraph graph : mGraphs) {
106             graph.tearDown(this);
107         }
108         mGraphs.clear();
109 
110         // Release frame manager
111         if (mFrameManager != null) {
112             mFrameManager.tearDown();
113             mFrameManager = null;
114         }
115 
116         // Release GL context
117         if (mGLEnvironment != null) {
118             mGLEnvironment.tearDown();
119             mGLEnvironment = null;
120         }
121     }
122 
addGraph(FilterGraph graph)123     final void addGraph(FilterGraph graph) {
124         mGraphs.add(graph);
125     }
126 }
127