| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 |
| 11 | package org.webrtc; |
| 12 |
| 13 | import android.graphics.SurfaceTexture; |
| 14 | import androidx.annotation.Nullable; |
| 15 | import android.view.Surface; |
| 16 | import java.util.ArrayList; |
| 17 | import javax.microedition.khronos.egl.EGL10; |
| 18 |
| 19 | /** |
| 20 | * Holds EGL state and utility methods for handling an egl 1.0 EGLContext, an EGLDisplay, |
| 21 | * and an EGLSurface. |
| 22 | */ |
| 23 | public interface EglBase { |
| 24 |
| 25 | // EGL wrapper for an actual EGLContext. |
| 26 | public interface Context { |
| 27 | public final static long NO_CONTEXT = 0; |
| 28 |
| 29 | /** |
| 30 | * Returns an EGL context that can be used by native code. Returns NO_CONTEXT if the method is |
| 31 | * unsupported. |
| 32 | * |
| 33 | * @note This is currently only supported for EGL 1.4 and not for EGL 1.0. |
| 34 | */ |
| 35 | long getNativeEglContext(); |
| 36 | } |
| 37 |
| 38 | // According to the documentation, EGL can be used from multiple threads at the same time if each |
| 39 | // thread has its own EGLContext, but in practice it deadlocks on some devices when doing this. |
| 40 | // Therefore, synchronize on this global lock before calling dangerous EGL functions that might |
| 41 | // deadlock. See https://bugs.chromium.org/p/webrtc/issues/detail?id=5702 for more info. |
| 42 | public static final Object lock = new Object(); |
| 43 |
| 44 | // These constants are taken from EGL14.EGL_OPENGL_ES2_BIT and EGL14.EGL_CONTEXT_CLIENT_VERSION. |
| 45 | // https://android.googlesource.com/platform/frameworks/base/+/master/opengl/java/android/opengl/EGL14.java |
| 46 | // This is similar to how GlSurfaceView does: |
| 47 | // http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/android/opengl/GLSurfaceView.java#760 |
| 48 | public static final int EGL_OPENGL_ES2_BIT = 4; |
| 49 | public static final int EGL_OPENGL_ES3_BIT = 0x40; |
| 50 | // Android-specific extension. |
| 51 | public static final int EGL_RECORDABLE_ANDROID = 0x3142; |
| 52 |
| 53 | public static ConfigBuilder configBuilder() { |
| 54 | return new ConfigBuilder(); |
| 55 | } |
| 56 |
| 57 | public static class ConfigBuilder { |
| 58 | private int openGlesVersion = 2; |
| 59 | private boolean hasAlphaChannel; |
| 60 | private boolean supportsPixelBuffer; |
| 61 | private boolean isRecordable; |
| 62 |
| 63 | public ConfigBuilder setOpenGlesVersion(int version) { |
| 64 | if (version < 1 || version > 3) { |
| 65 | throw new IllegalArgumentException("OpenGL ES version " + version + " not supported"); |
| 66 | } |
| 67 | this.openGlesVersion = version; |
| 68 | return this; |
| 69 | } |
| 70 |
| 71 | public ConfigBuilder setHasAlphaChannel(boolean hasAlphaChannel) { |
| 72 | this.hasAlphaChannel = hasAlphaChannel; |
| 73 | return this; |
| 74 | } |
| 75 |
| 76 | public ConfigBuilder setSupportsPixelBuffer(boolean supportsPixelBuffer) { |
| 77 | this.supportsPixelBuffer = supportsPixelBuffer; |
| 78 | return this; |
| 79 | } |
| 80 |
| 81 | public ConfigBuilder setIsRecordable(boolean isRecordable) { |
| 82 | this.isRecordable = isRecordable; |
| 83 | return this; |
| 84 | } |
| 85 |
| 86 | public int[] createConfigAttributes() { |
| 87 | ArrayList<Integer> list = new ArrayList<>(); |
| 88 | list.add(EGL10.EGL_RED_SIZE); |
| 89 | list.add(8); |
| 90 | list.add(EGL10.EGL_GREEN_SIZE); |
| 91 | list.add(8); |
| 92 | list.add(EGL10.EGL_BLUE_SIZE); |
| 93 | list.add(8); |
| 94 | if (hasAlphaChannel) { |
| 95 | list.add(EGL10.EGL_ALPHA_SIZE); |
| 96 | list.add(8); |
| 97 | } |
| 98 | if (openGlesVersion == 2 || openGlesVersion == 3) { |
| 99 | list.add(EGL10.EGL_RENDERABLE_TYPE); |
| 100 | list.add(openGlesVersion == 3 ? EGL_OPENGL_ES3_BIT : EGL_OPENGL_ES2_BIT); |
| 101 | } |
| 102 | if (supportsPixelBuffer) { |
| 103 | list.add(EGL10.EGL_SURFACE_TYPE); |
| 104 | list.add(EGL10.EGL_PBUFFER_BIT); |
| 105 | } |
| 106 | if (isRecordable) { |
| 107 | list.add(EGL_RECORDABLE_ANDROID); |
| 108 | list.add(1); |
| 109 | } |
| 110 | list.add(EGL10.EGL_NONE); |
| 111 |
| 112 | final int[] res = new int[list.size()]; |
| 113 | for (int i = 0; i < list.size(); ++i) { |
| 114 | res[i] = list.get(i); |
| 115 | } |
| 116 | return res; |
| 117 | } |
| 118 | } |
| 119 |
| 120 | public static final int[] CONFIG_PLAIN = configBuilder().createConfigAttributes(); |
| 121 | public static final int[] CONFIG_RGBA = |
| 122 | configBuilder().setHasAlphaChannel(true).createConfigAttributes(); |
| 123 | public static final int[] CONFIG_PIXEL_BUFFER = |
| 124 | configBuilder().setSupportsPixelBuffer(true).createConfigAttributes(); |
| 125 | public static final int[] CONFIG_PIXEL_RGBA_BUFFER = configBuilder() |
| 126 | .setHasAlphaChannel(true) |
| 127 | .setSupportsPixelBuffer(true) |
| 128 | .createConfigAttributes(); |
| 129 | public static final int[] CONFIG_RECORDABLE = |
| 130 | configBuilder().setIsRecordable(true).createConfigAttributes(); |
| 131 |
| 132 | static int getOpenGlesVersionFromConfig(int[] configAttributes) { |
| 133 | for (int i = 0; i < configAttributes.length - 1; ++i) { |
| 134 | if (configAttributes[i] == EGL10.EGL_RENDERABLE_TYPE) { |
| 135 | switch (configAttributes[i + 1]) { |
| 136 | case EGL_OPENGL_ES2_BIT: |
| 137 | return 2; |
| 138 | case EGL_OPENGL_ES3_BIT: |
| 139 | return 3; |
| 140 | default: |
| 141 | return 1; |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | // Default to V1 if no renderable type is specified. |
| 146 | return 1; |
| 147 | } |
| 148 |
| 149 | /** |
| 150 | * Create a new context with the specified config attributes, sharing data with |sharedContext|. |
| 151 | * If |sharedContext| is null, a root context is created. This function will try to create an EGL |
| 152 | * 1.4 context if possible, and an EGL 1.0 context otherwise. |
| 153 | */ |
| 154 | public static EglBase create(@Nullable Context sharedContext, int[] configAttributes) { |
| 155 | if (sharedContext == null) { |
| 156 | return EglBase14Impl.isEGL14Supported() ? createEgl14(configAttributes) |
| 157 | : createEgl10(configAttributes); |
| 158 | } else if (sharedContext instanceof EglBase14.Context) { |
| 159 | return createEgl14((EglBase14.Context) sharedContext, configAttributes); |
| 160 | } else if (sharedContext instanceof EglBase10.Context) { |
| 161 | return createEgl10((EglBase10.Context) sharedContext, configAttributes); |
| 162 | } |
| 163 | throw new IllegalArgumentException("Unrecognized Context"); |
| 164 | } |
| 165 |
| 166 | /** |
| 167 | * Helper function for creating a plain root context. This function will try to create an EGL 1.4 |
| 168 | * context if possible, and an EGL 1.0 context otherwise. |
| 169 | */ |
| 170 | public static EglBase create() { |
| 171 | return create(null /* shaderContext */, CONFIG_PLAIN); |
| 172 | } |
| 173 |
| 174 | /** |
| 175 | * Helper function for creating a plain context, sharing data with `sharedContext`. This function |
| 176 | * will try to create an EGL 1.4 context if possible, and an EGL 1.0 context otherwise. |
| 177 | */ |
| 178 | public static EglBase create(Context sharedContext) { |
| 179 | return create(sharedContext, CONFIG_PLAIN); |
| 180 | } |
| 181 |
| 182 | /** Explicitly create a root EGl 1.0 context with the specified config attributes. */ |
| 183 | public static EglBase10 createEgl10(int[] configAttributes) { |
| 184 | return new EglBase10Impl(/* sharedContext= */ null, configAttributes); |
| 185 | } |
| 186 |
| 187 | /** |
| 188 | * Explicitly create a root EGl 1.0 context with the specified config attributes and shared |
| 189 | * context. |
| 190 | */ |
| 191 | public static EglBase10 createEgl10(EglBase10.Context sharedContext, int[] configAttributes) { |
| 192 | return new EglBase10Impl( |
| 193 | sharedContext == null ? null : sharedContext.getRawContext(), configAttributes); |
| 194 | } |
| 195 |
| 196 | /** |
| 197 | * Explicitly create a root EGl 1.0 context with the specified config attributes |
| 198 | * and shared context. |
| 199 | */ |
| 200 | public static EglBase10 createEgl10( |
| 201 | javax.microedition.khronos.egl.EGLContext sharedContext, int[] configAttributes) { |
| 202 | return new EglBase10Impl(sharedContext, configAttributes); |
| 203 | } |
| 204 |
| 205 | /** Explicitly create a root EGl 1.4 context with the specified config attributes. */ |
| 206 | public static EglBase14 createEgl14(int[] configAttributes) { |
| 207 | return new EglBase14Impl(/* sharedContext= */ null, configAttributes); |
| 208 | } |
| 209 |
| 210 | /** |
| 211 | * Explicitly create a root EGl 1.4 context with the specified config attributes and shared |
| 212 | * context. |
| 213 | */ |
| 214 | public static EglBase14 createEgl14(EglBase14.Context sharedContext, int[] configAttributes) { |
| 215 | return new EglBase14Impl( |
| 216 | sharedContext == null ? null : sharedContext.getRawContext(), configAttributes); |
| 217 | } |
| 218 |
| 219 | /** |
| 220 | * Explicitly create a root EGl 1.4 context with the specified config attributes |
| 221 | * and shared context. |
| 222 | */ |
| 223 | public static EglBase14 createEgl14( |
| 224 | android.opengl.EGLContext sharedContext, int[] configAttributes) { |
| 225 | return new EglBase14Impl(sharedContext, configAttributes); |
| 226 | } |
| 227 |
| 228 | void createSurface(Surface surface); |
| 229 |
| 230 | // Create EGLSurface from the Android SurfaceTexture. |
| 231 | void createSurface(SurfaceTexture surfaceTexture); |
| 232 |
| 233 | // Create dummy 1x1 pixel buffer surface so the context can be made current. |
| 234 | void createDummyPbufferSurface(); |
| 235 |
| 236 | void createPbufferSurface(int width, int height); |
| 237 |
| 238 | Context getEglBaseContext(); |
| 239 |
| 240 | boolean hasSurface(); |
| 241 |
| 242 | int surfaceWidth(); |
| 243 |
| 244 | int surfaceHeight(); |
| 245 |
| 246 | void releaseSurface(boolean background); |
| 247 |
| 248 | void release(); |
| 249 |
| 250 | void makeCurrent(); |
| 251 |
| 252 | // Detach the current EGL context, so that it can be made current on another thread. |
| 253 | void detachCurrent(); |
| 254 |
| 255 | void swapBuffers(boolean background); |
| 256 |
| 257 | void swapBuffers(long presentationTimeStampNs,boolean background); |
| 258 |
| 259 | // Create EGLSurface from the Android Surface. |
| 260 | void createBackgroundSurface(SurfaceTexture surface); |
| 261 |
| 262 | void makeBackgroundCurrent(); |
| 263 |
| 264 | boolean hasBackgroundSurface(); |
| 265 | } |
| 266 |