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 GLEnvironment()34 public GLEnvironment() { 35 nativeAllocate(); 36 } 37 GLEnvironment(NativeAllocatorTag tag)38 private GLEnvironment(NativeAllocatorTag tag) { 39 } 40 tearDown()41 public synchronized void tearDown() { 42 if (glEnvId != -1) { 43 nativeDeallocate(); 44 glEnvId = -1; 45 } 46 } 47 48 @Override finalize()49 protected void finalize() throws Throwable { 50 tearDown(); 51 } 52 initWithNewContext()53 public void initWithNewContext() { 54 if (!nativeInitWithNewContext()) { 55 throw new RuntimeException("Could not initialize GLEnvironment with new context!"); 56 } 57 } 58 initWithCurrentContext()59 public void initWithCurrentContext() { 60 if (!nativeInitWithCurrentContext()) { 61 throw new RuntimeException("Could not initialize GLEnvironment with current context!"); 62 } 63 } 64 isActive()65 public boolean isActive() { 66 return nativeIsActive(); 67 } 68 isContextActive()69 public boolean isContextActive() { 70 return nativeIsContextActive(); 71 } 72 isAnyContextActive()73 public static boolean isAnyContextActive() { 74 return nativeIsAnyContextActive(); 75 } 76 activate()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 deactivate()86 public void deactivate() { 87 if (!nativeDeactivate()) { 88 throw new RuntimeException("Could not deactivate GLEnvironment!"); 89 } 90 } 91 swapBuffers()92 public void swapBuffers() { 93 if (!nativeSwapBuffers()) { 94 throw new RuntimeException("Error swapping EGL buffers!"); 95 } 96 } 97 registerSurface(Surface surface)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 registerSurfaceTexture(SurfaceTexture surfaceTexture, int width, int height)106 public int registerSurfaceTexture(SurfaceTexture surfaceTexture, int width, int height) { 107 int result = nativeAddSurfaceTexture(surfaceTexture, width, height); 108 if (result < 0) { 109 throw new RuntimeException("Error registering surfaceTexture " + surfaceTexture + "!"); 110 } 111 return result; 112 } 113 registerSurfaceFromMediaRecorder(MediaRecorder mediaRecorder)114 public int registerSurfaceFromMediaRecorder(MediaRecorder mediaRecorder) { 115 int result = nativeAddSurfaceFromMediaRecorder(mediaRecorder); 116 if (result < 0) { 117 throw new RuntimeException("Error registering surface from " 118 + "MediaRecorder" + mediaRecorder + "!"); 119 } 120 return result; 121 } 122 activateSurfaceWithId(int surfaceId)123 public void activateSurfaceWithId(int surfaceId) { 124 if (!nativeActivateSurfaceId(surfaceId)) { 125 throw new RuntimeException("Could not activate surface " + surfaceId + "!"); 126 } 127 } 128 unregisterSurfaceId(int surfaceId)129 public void unregisterSurfaceId(int surfaceId) { 130 if (!nativeRemoveSurfaceId(surfaceId)) { 131 throw new RuntimeException("Could not unregister surface " + surfaceId + "!"); 132 } 133 } 134 setSurfaceTimestamp(long timestamp)135 public void setSurfaceTimestamp(long timestamp) { 136 if (!nativeSetSurfaceTimestamp(timestamp)) { 137 throw new RuntimeException("Could not set timestamp for current surface!"); 138 } 139 } 140 141 static { 142 System.loadLibrary("filterfw"); 143 } 144 nativeInitWithNewContext()145 private native boolean nativeInitWithNewContext(); 146 nativeInitWithCurrentContext()147 private native boolean nativeInitWithCurrentContext(); 148 nativeIsActive()149 private native boolean nativeIsActive(); 150 nativeIsContextActive()151 private native boolean nativeIsContextActive(); 152 nativeIsAnyContextActive()153 private static native boolean nativeIsAnyContextActive(); 154 nativeActivate()155 private native boolean nativeActivate(); 156 nativeDeactivate()157 private native boolean nativeDeactivate(); 158 nativeSwapBuffers()159 private native boolean nativeSwapBuffers(); 160 nativeAllocate()161 private native boolean nativeAllocate(); 162 nativeDeallocate()163 private native boolean nativeDeallocate(); 164 nativeAddSurface(Surface surface)165 private native int nativeAddSurface(Surface surface); 166 nativeAddSurfaceTexture(SurfaceTexture surface, int width, int height)167 private native int nativeAddSurfaceTexture(SurfaceTexture surface, int width, int height); 168 nativeAddSurfaceFromMediaRecorder(MediaRecorder mediaRecorder)169 private native int nativeAddSurfaceFromMediaRecorder(MediaRecorder mediaRecorder); 170 nativeDisconnectSurfaceMediaSource(MediaRecorder mediaRecorder)171 private native boolean nativeDisconnectSurfaceMediaSource(MediaRecorder mediaRecorder); 172 nativeActivateSurfaceId(int surfaceId)173 private native boolean nativeActivateSurfaceId(int surfaceId); 174 nativeRemoveSurfaceId(int surfaceId)175 private native boolean nativeRemoveSurfaceId(int surfaceId); 176 nativeSetSurfaceTimestamp(long timestamp)177 private native boolean nativeSetSurfaceTimestamp(long timestamp); 178 } 179