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