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