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