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