• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.NativeAllocatorTag;
21 import android.graphics.SurfaceTexture;
22 import android.os.Looper;
23 import android.util.Log;
24 import android.view.Surface;
25 import android.media.MediaRecorder;
26 
27 /**
28  * @hide
29  */
30 public class GLEnvironment {
31 
32     private int glEnvId;
33 
34     private boolean mManageContext = true;
35 
GLEnvironment()36     public GLEnvironment() {
37         nativeAllocate();
38     }
39 
GLEnvironment(NativeAllocatorTag tag)40     private GLEnvironment(NativeAllocatorTag tag) {
41     }
42 
tearDown()43     public synchronized void tearDown() {
44         if (glEnvId != -1) {
45             nativeDeallocate();
46             glEnvId = -1;
47         }
48     }
49 
50     @Override
finalize()51     protected void finalize() throws Throwable {
52         tearDown();
53     }
54 
initWithNewContext()55     public void initWithNewContext() {
56         mManageContext = true;
57         if (!nativeInitWithNewContext()) {
58             throw new RuntimeException("Could not initialize GLEnvironment with new context!");
59         }
60     }
61 
initWithCurrentContext()62     public void initWithCurrentContext() {
63         mManageContext = false;
64         if (!nativeInitWithCurrentContext()) {
65             throw new RuntimeException("Could not initialize GLEnvironment with current context!");
66         }
67     }
68 
isActive()69     public boolean isActive() {
70         return nativeIsActive();
71     }
72 
isContextActive()73     public boolean isContextActive() {
74         return nativeIsContextActive();
75     }
76 
isAnyContextActive()77     public static boolean isAnyContextActive() {
78         return nativeIsAnyContextActive();
79     }
80 
activate()81     public void activate() {
82         if (Looper.myLooper() != null && Looper.myLooper().equals(Looper.getMainLooper())) {
83             Log.e("FilterFramework", "Activating GL context in UI thread!");
84         }
85         if (mManageContext && !nativeActivate()) {
86             throw new RuntimeException("Could not activate GLEnvironment!");
87         }
88     }
89 
deactivate()90     public void deactivate() {
91         if (mManageContext && !nativeDeactivate()) {
92             throw new RuntimeException("Could not deactivate GLEnvironment!");
93         }
94     }
95 
swapBuffers()96     public void swapBuffers() {
97         if (!nativeSwapBuffers()) {
98             throw new RuntimeException("Error swapping EGL buffers!");
99         }
100     }
101 
registerSurface(Surface surface)102     public int registerSurface(Surface surface) {
103         int result = nativeAddSurface(surface);
104         if (result < 0) {
105             throw new RuntimeException("Error registering surface " + surface + "!");
106         }
107         return result;
108     }
109 
registerSurfaceTexture(SurfaceTexture surfaceTexture, int width, int height)110     public int registerSurfaceTexture(SurfaceTexture surfaceTexture, int width, int height) {
111         Surface surface = new Surface(surfaceTexture);
112         int result = nativeAddSurfaceWidthHeight(surface, width, height);
113         surface.release();
114         if (result < 0) {
115             throw new RuntimeException("Error registering surfaceTexture " + surfaceTexture + "!");
116         }
117         return result;
118     }
119 
registerSurfaceFromMediaRecorder(MediaRecorder mediaRecorder)120     public int registerSurfaceFromMediaRecorder(MediaRecorder mediaRecorder) {
121         int result = nativeAddSurfaceFromMediaRecorder(mediaRecorder);
122         if (result < 0) {
123             throw new RuntimeException("Error registering surface from "
124                                     + "MediaRecorder" + mediaRecorder + "!");
125         }
126         return result;
127     }
128 
activateSurfaceWithId(int surfaceId)129     public void activateSurfaceWithId(int surfaceId) {
130         if (!nativeActivateSurfaceId(surfaceId)) {
131             throw new RuntimeException("Could not activate surface " + surfaceId + "!");
132         }
133     }
134 
unregisterSurfaceId(int surfaceId)135     public void unregisterSurfaceId(int surfaceId) {
136         if (!nativeRemoveSurfaceId(surfaceId)) {
137             throw new RuntimeException("Could not unregister surface " + surfaceId + "!");
138         }
139     }
140 
setSurfaceTimestamp(long timestamp)141     public void setSurfaceTimestamp(long timestamp) {
142         if (!nativeSetSurfaceTimestamp(timestamp)) {
143             throw new RuntimeException("Could not set timestamp for current surface!");
144         }
145     }
146 
147     static {
148         System.loadLibrary("filterfw");
149     }
150 
nativeInitWithNewContext()151     private native boolean nativeInitWithNewContext();
152 
nativeInitWithCurrentContext()153     private native boolean nativeInitWithCurrentContext();
154 
nativeIsActive()155     private native boolean nativeIsActive();
156 
nativeIsContextActive()157     private native boolean nativeIsContextActive();
158 
nativeIsAnyContextActive()159     private static native boolean nativeIsAnyContextActive();
160 
nativeActivate()161     private native boolean nativeActivate();
162 
nativeDeactivate()163     private native boolean nativeDeactivate();
164 
nativeSwapBuffers()165     private native boolean nativeSwapBuffers();
166 
nativeAllocate()167     private native boolean nativeAllocate();
168 
nativeDeallocate()169     private native boolean nativeDeallocate();
170 
nativeAddSurface(Surface surface)171     private native int nativeAddSurface(Surface surface);
172 
nativeAddSurfaceWidthHeight(Surface surface, int width, int height)173     private native int nativeAddSurfaceWidthHeight(Surface surface, int width, int height);
174 
nativeAddSurfaceFromMediaRecorder(MediaRecorder mediaRecorder)175     private native int nativeAddSurfaceFromMediaRecorder(MediaRecorder mediaRecorder);
176 
nativeDisconnectSurfaceMediaSource(MediaRecorder mediaRecorder)177     private native boolean  nativeDisconnectSurfaceMediaSource(MediaRecorder mediaRecorder);
178 
nativeActivateSurfaceId(int surfaceId)179     private native boolean nativeActivateSurfaceId(int surfaceId);
180 
nativeRemoveSurfaceId(int surfaceId)181     private native boolean nativeRemoveSurfaceId(int surfaceId);
182 
nativeSetSurfaceTimestamp(long timestamp)183     private native boolean nativeSetSurfaceTimestamp(long timestamp);
184 }
185