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.compat.annotation.UnsupportedAppUsage; 21 import android.graphics.SurfaceTexture; 22 import android.media.MediaRecorder; 23 import android.os.Looper; 24 import android.util.Log; 25 import android.view.Surface; 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 69 @UnsupportedAppUsage isActive()70 public boolean isActive() { 71 return nativeIsActive(); 72 } 73 isContextActive()74 public boolean isContextActive() { 75 return nativeIsContextActive(); 76 } 77 isAnyContextActive()78 public static boolean isAnyContextActive() { 79 return nativeIsAnyContextActive(); 80 } 81 82 @UnsupportedAppUsage activate()83 public void activate() { 84 if (Looper.myLooper() != null && Looper.myLooper().equals(Looper.getMainLooper())) { 85 Log.e("FilterFramework", "Activating GL context in UI thread!"); 86 } 87 if (mManageContext && !nativeActivate()) { 88 throw new RuntimeException("Could not activate GLEnvironment!"); 89 } 90 } 91 92 @UnsupportedAppUsage deactivate()93 public void deactivate() { 94 if (mManageContext && !nativeDeactivate()) { 95 throw new RuntimeException("Could not deactivate GLEnvironment!"); 96 } 97 } 98 99 @UnsupportedAppUsage swapBuffers()100 public void swapBuffers() { 101 if (!nativeSwapBuffers()) { 102 throw new RuntimeException("Error swapping EGL buffers!"); 103 } 104 } 105 registerSurface(Surface surface)106 public int registerSurface(Surface surface) { 107 int result = nativeAddSurface(surface); 108 if (result < 0) { 109 throw new RuntimeException("Error registering surface " + surface + "!"); 110 } 111 return result; 112 } 113 registerSurfaceTexture(SurfaceTexture surfaceTexture, int width, int height)114 public int registerSurfaceTexture(SurfaceTexture surfaceTexture, int width, int height) { 115 Surface surface = new Surface(surfaceTexture); 116 int result = nativeAddSurfaceWidthHeight(surface, width, height); 117 surface.release(); 118 if (result < 0) { 119 throw new RuntimeException("Error registering surfaceTexture " + surfaceTexture + "!"); 120 } 121 return result; 122 } 123 124 @UnsupportedAppUsage registerSurfaceFromMediaRecorder(MediaRecorder mediaRecorder)125 public int registerSurfaceFromMediaRecorder(MediaRecorder mediaRecorder) { 126 int result = nativeAddSurfaceFromMediaRecorder(mediaRecorder); 127 if (result < 0) { 128 throw new RuntimeException("Error registering surface from " 129 + "MediaRecorder" + mediaRecorder + "!"); 130 } 131 return result; 132 } 133 134 @UnsupportedAppUsage activateSurfaceWithId(int surfaceId)135 public void activateSurfaceWithId(int surfaceId) { 136 if (!nativeActivateSurfaceId(surfaceId)) { 137 throw new RuntimeException("Could not activate surface " + surfaceId + "!"); 138 } 139 } 140 141 @UnsupportedAppUsage unregisterSurfaceId(int surfaceId)142 public void unregisterSurfaceId(int surfaceId) { 143 if (!nativeRemoveSurfaceId(surfaceId)) { 144 throw new RuntimeException("Could not unregister surface " + surfaceId + "!"); 145 } 146 } 147 148 @UnsupportedAppUsage setSurfaceTimestamp(long timestamp)149 public void setSurfaceTimestamp(long timestamp) { 150 if (!nativeSetSurfaceTimestamp(timestamp)) { 151 throw new RuntimeException("Could not set timestamp for current surface!"); 152 } 153 } 154 155 static { 156 System.loadLibrary("filterfw"); 157 } 158 nativeInitWithNewContext()159 private native boolean nativeInitWithNewContext(); 160 nativeInitWithCurrentContext()161 private native boolean nativeInitWithCurrentContext(); 162 nativeIsActive()163 private native boolean nativeIsActive(); 164 nativeIsContextActive()165 private native boolean nativeIsContextActive(); 166 nativeIsAnyContextActive()167 private static native boolean nativeIsAnyContextActive(); 168 nativeActivate()169 private native boolean nativeActivate(); 170 nativeDeactivate()171 private native boolean nativeDeactivate(); 172 nativeSwapBuffers()173 private native boolean nativeSwapBuffers(); 174 nativeAllocate()175 private native boolean nativeAllocate(); 176 nativeDeallocate()177 private native boolean nativeDeallocate(); 178 nativeAddSurface(Surface surface)179 private native int nativeAddSurface(Surface surface); 180 nativeAddSurfaceWidthHeight(Surface surface, int width, int height)181 private native int nativeAddSurfaceWidthHeight(Surface surface, int width, int height); 182 nativeAddSurfaceFromMediaRecorder(MediaRecorder mediaRecorder)183 private native int nativeAddSurfaceFromMediaRecorder(MediaRecorder mediaRecorder); 184 nativeDisconnectSurfaceMediaSource(MediaRecorder mediaRecorder)185 private native boolean nativeDisconnectSurfaceMediaSource(MediaRecorder mediaRecorder); 186 nativeActivateSurfaceId(int surfaceId)187 private native boolean nativeActivateSurfaceId(int surfaceId); 188 nativeRemoveSurfaceId(int surfaceId)189 private native boolean nativeRemoveSurfaceId(int surfaceId); 190 nativeSetSurfaceTimestamp(long timestamp)191 private native boolean nativeSetSurfaceTimestamp(long timestamp); 192 } 193