| 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.annotation.TargetApi; |
| 14 | import android.graphics.SurfaceTexture; |
| 15 | import android.opengl.EGL14; |
| 16 | import android.opengl.EGLConfig; |
| 17 | import android.opengl.EGLContext; |
| 18 | import android.opengl.EGLDisplay; |
| 19 | import android.opengl.EGLExt; |
| 20 | import android.opengl.EGLSurface; |
| 21 | import android.opengl.GLException; |
| 22 | import android.os.Build; |
| 23 | import android.view.Surface; |
| 24 | import androidx.annotation.Nullable; |
| 25 |
| 26 | /** |
| 27 | * Holds EGL state and utility methods for handling an EGL14 EGLContext, an EGLDisplay, |
| 28 | * and an EGLSurface. |
| 29 | */ |
| 30 | @SuppressWarnings("ReferenceEquality") // We want to compare to EGL14 constants. |
| 31 | @TargetApi(18) |
| 32 | class EglBase14Impl implements EglBase14 { |
| 33 | private static final String TAG = "EglBase14Impl"; |
| 34 | private static final int EGLExt_SDK_VERSION = Build.VERSION_CODES.JELLY_BEAN_MR2; |
| 35 | private static final int CURRENT_SDK_VERSION = Build.VERSION.SDK_INT; |
| 36 | private EGLContext eglContext; |
| 37 | @Nullable private EGLConfig eglConfig; |
| 38 | private EGLDisplay eglDisplay; |
| 39 | private EGLSurface eglSurface = EGL14.EGL_NO_SURFACE; |
| 40 | private EGLSurface eglSurfaceBackground = EGL14.EGL_NO_SURFACE; |
| 41 |
| 42 | // EGL 1.4 is supported from API 17. But EGLExt that is used for setting presentation |
| 43 | // time stamp on a surface is supported from 18 so we require 18. |
| 44 | public static boolean isEGL14Supported() { |
| 45 | Logging.d(TAG, |
| 46 | "SDK version: " + CURRENT_SDK_VERSION |
| 47 | + ". isEGL14Supported: " + (CURRENT_SDK_VERSION >= EGLExt_SDK_VERSION)); |
| 48 | return (CURRENT_SDK_VERSION >= EGLExt_SDK_VERSION); |
| 49 | } |
| 50 |
| 51 | public static class Context implements EglBase14.Context { |
| 52 | private final EGLContext egl14Context; |
| 53 |
| 54 | @Override |
| 55 | public EGLContext getRawContext() { |
| 56 | return egl14Context; |
| 57 | } |
| 58 |
| 59 | @Override |
| 60 | @SuppressWarnings("deprecation") |
| 61 | @TargetApi(Build.VERSION_CODES.LOLLIPOP) |
| 62 | public long getNativeEglContext() { |
| 63 | return CURRENT_SDK_VERSION >= Build.VERSION_CODES.LOLLIPOP ? egl14Context.getNativeHandle() |
| 64 | : egl14Context.getHandle(); |
| 65 | } |
| 66 |
| 67 | public Context(android.opengl.EGLContext eglContext) { |
| 68 | this.egl14Context = eglContext; |
| 69 | } |
| 70 | } |
| 71 |
| 72 | // Create a new context with the specified config type, sharing data with sharedContext. |
| 73 | // |sharedContext| may be null. |
| 74 | public EglBase14Impl(EGLContext sharedContext, int[] configAttributes) { |
| 75 | eglDisplay = getEglDisplay(); |
| 76 | eglConfig = getEglConfig(eglDisplay, configAttributes); |
| 77 | final int openGlesVersion = EglBase.getOpenGlesVersionFromConfig(configAttributes); |
| 78 | Logging.d(TAG, "Using OpenGL ES version " + openGlesVersion); |
| 79 | eglContext = createEglContext(sharedContext, eglDisplay, eglConfig, openGlesVersion); |
| 80 | } |
| 81 |
| 82 | // Create EGLSurface from the Android Surface. |
| 83 | @Override |
| 84 | public void createSurface(Surface surface) { |
| 85 | createSurfaceInternal(surface, false); |
| 86 | } |
| 87 |
| 88 | // Create EGLSurface from the Android Surface. |
| 89 | @Override |
| 90 | public void createBackgroundSurface(SurfaceTexture surface) { |
| 91 | createSurfaceInternal(surface, true); |
| 92 | } |
| 93 |
| 94 |
| 95 | // Create EGLSurface from the Android SurfaceTexture. |
| 96 | @Override |
| 97 | public void createSurface(SurfaceTexture surfaceTexture) { |
| 98 | createSurfaceInternal(surfaceTexture, false); |
| 99 | } |
| 100 |
| 101 | // Create EGLSurface from either Surface or SurfaceTexture. |
| 102 | private void createSurfaceInternal(Object surface, boolean background) { |
| 103 | if (!(surface instanceof Surface) && !(surface instanceof SurfaceTexture)) { |
| 104 | throw new IllegalStateException("Input must be either a Surface or SurfaceTexture"); |
| 105 | } |
| 106 | checkIsNotReleased(); |
| 107 | if (background) { |
| 108 | if (eglSurfaceBackground != EGL14.EGL_NO_SURFACE) { |
| 109 | throw new RuntimeException("Already has an EGLSurface"); |
| 110 | } |
| 111 | int[] surfaceAttribs = {EGL14.EGL_NONE}; |
| 112 | eglSurfaceBackground = EGL14.eglCreateWindowSurface(eglDisplay, eglConfig, surface, surfaceAttribs, 0); |
| 113 | if (eglSurfaceBackground == EGL14.EGL_NO_SURFACE) { |
| 114 | throw new RuntimeException( |
| 115 | "Failed to create window surface: 0x" + Integer.toHexString(EGL14.eglGetError())); |
| 116 | } |
| 117 | } else { |
| 118 | if (eglSurface != EGL14.EGL_NO_SURFACE) { |
| 119 | throw new RuntimeException("Already has an EGLSurface"); |
| 120 | } |
| 121 | int[] surfaceAttribs = {EGL14.EGL_NONE}; |
| 122 | eglSurface = EGL14.eglCreateWindowSurface(eglDisplay, eglConfig, surface, surfaceAttribs, 0); |
| 123 | if (eglSurface == EGL14.EGL_NO_SURFACE) { |
| 124 | throw new RuntimeException( |
| 125 | "Failed to create window surface: 0x" + Integer.toHexString(EGL14.eglGetError())); |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 |
| 130 | @Override |
| 131 | public void createDummyPbufferSurface() { |
| 132 | createPbufferSurface(1, 1); |
| 133 | } |
| 134 |
| 135 | @Override |
| 136 | public void createPbufferSurface(int width, int height) { |
| 137 | checkIsNotReleased(); |
| 138 | if (eglSurface != EGL14.EGL_NO_SURFACE) { |
| 139 | throw new RuntimeException("Already has an EGLSurface"); |
| 140 | } |
| 141 | int[] surfaceAttribs = {EGL14.EGL_WIDTH, width, EGL14.EGL_HEIGHT, height, EGL14.EGL_NONE}; |
| 142 | eglSurface = EGL14.eglCreatePbufferSurface(eglDisplay, eglConfig, surfaceAttribs, 0); |
| 143 | if (eglSurface == EGL14.EGL_NO_SURFACE) { |
| 144 | throw new RuntimeException("Failed to create pixel buffer surface with size " + width + "x" |
| 145 | + height + ": 0x" + Integer.toHexString(EGL14.eglGetError())); |
| 146 | } |
| 147 | } |
| 148 |
| 149 | @Override |
| 150 | public Context getEglBaseContext() { |
| 151 | return new Context(eglContext); |
| 152 | } |
| 153 |
| 154 | @Override |
| 155 | public boolean hasSurface() { |
| 156 | return eglSurface != EGL14.EGL_NO_SURFACE; |
| 157 | } |
| 158 |
| 159 | @Override |
| 160 | public int surfaceWidth() { |
| 161 | final int widthArray[] = new int[1]; |
| 162 | EGL14.eglQuerySurface(eglDisplay, eglSurface, EGL14.EGL_WIDTH, widthArray, 0); |
| 163 | return widthArray[0]; |
| 164 | } |
| 165 |
| 166 | @Override |
| 167 | public int surfaceHeight() { |
| 168 | final int heightArray[] = new int[1]; |
| 169 | EGL14.eglQuerySurface(eglDisplay, eglSurface, EGL14.EGL_HEIGHT, heightArray, 0); |
| 170 | return heightArray[0]; |
| 171 | } |
| 172 |
| 173 | @Override |
| 174 | public void releaseSurface(boolean background) { |
| 175 | if (background) { |
| 176 | if (eglSurfaceBackground != EGL14.EGL_NO_SURFACE) { |
| 177 | EGL14.eglDestroySurface(eglDisplay, eglSurfaceBackground); |
| 178 | eglSurfaceBackground = EGL14.EGL_NO_SURFACE; |
| 179 | } |
| 180 | } else { |
| 181 | if (eglSurface != EGL14.EGL_NO_SURFACE) { |
| 182 | EGL14.eglDestroySurface(eglDisplay, eglSurface); |
| 183 | eglSurface = EGL14.EGL_NO_SURFACE; |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 |
| 188 | private void checkIsNotReleased() { |
| 189 | if (eglDisplay == EGL14.EGL_NO_DISPLAY || eglContext == EGL14.EGL_NO_CONTEXT |
| 190 | || eglConfig == null) { |
| 191 | throw new RuntimeException("This object has been released"); |
| 192 | } |
| 193 | } |
| 194 |
| 195 | @Override |
| 196 | public void release() { |
| 197 | checkIsNotReleased(); |
| 198 | releaseSurface(false); |
| 199 | releaseSurface(true); |
| 200 | detachCurrent(); |
| 201 | synchronized (EglBase.lock) { |
| 202 | EGL14.eglDestroyContext(eglDisplay, eglContext); |
| 203 | } |
| 204 | EGL14.eglReleaseThread(); |
| 205 | EGL14.eglTerminate(eglDisplay); |
| 206 | eglContext = EGL14.EGL_NO_CONTEXT; |
| 207 | eglDisplay = EGL14.EGL_NO_DISPLAY; |
| 208 | eglConfig = null; |
| 209 | } |
| 210 |
| 211 | @Override |
| 212 | public void makeCurrent() { |
| 213 | checkIsNotReleased(); |
| 214 | if (eglSurface == EGL14.EGL_NO_SURFACE) { |
| 215 | throw new RuntimeException("No EGLSurface - can't make current"); |
| 216 | } |
| 217 | synchronized (EglBase.lock) { |
| 218 | if (!EGL14.eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext)) { |
| 219 | throw new RuntimeException( |
| 220 | "eglMakeCurrent failed: 0x" + Integer.toHexString(EGL14.eglGetError())); |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 |
| 225 | @Override |
| 226 | public void makeBackgroundCurrent() { |
| 227 | checkIsNotReleased(); |
| 228 | if (eglSurfaceBackground == EGL14.EGL_NO_SURFACE) { |
| 229 | throw new RuntimeException("No EGLSurface - can't make current"); |
| 230 | } |
| 231 | synchronized (EglBase.lock) { |
| 232 | if (!EGL14.eglMakeCurrent(eglDisplay, eglSurfaceBackground, eglSurfaceBackground, eglContext)) { |
| 233 | throw new RuntimeException( |
| 234 | "eglMakeCurrent failed: 0x" + Integer.toHexString(EGL14.eglGetError())); |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 |
| 239 | @Override |
| 240 | public boolean hasBackgroundSurface() { |
| 241 | return eglSurfaceBackground != EGL14.EGL_NO_SURFACE; |
| 242 | } |
| 243 |
| 244 | // Detach the current EGL context, so that it can be made current on another thread. |
| 245 | @Override |
| 246 | public void detachCurrent() { |
| 247 | synchronized (EglBase.lock) { |
| 248 | if (!EGL14.eglMakeCurrent( |
| 249 | eglDisplay, EGL14.EGL_NO_SURFACE, EGL14.EGL_NO_SURFACE, EGL14.EGL_NO_CONTEXT)) { |
| 250 | throw new RuntimeException( |
| 251 | "eglDetachCurrent failed: 0x" + Integer.toHexString(EGL14.eglGetError())); |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 |
| 256 | @Override |
| 257 | public void swapBuffers(boolean background) { |
| 258 | checkIsNotReleased(); |
| 259 | EGLSurface surface = background ? eglSurfaceBackground : eglSurface; |
| 260 | if (surface == EGL14.EGL_NO_SURFACE) { |
| 261 | throw new RuntimeException("No EGLSurface - can't swap buffers"); |
| 262 | } |
| 263 | synchronized (EglBase.lock) { |
| 264 | EGL14.eglSwapBuffers(eglDisplay, surface); |
| 265 | } |
| 266 | } |
| 267 |
| 268 | @Override |
| 269 | public void swapBuffers(long timeStampNs, boolean background) { |
| 270 | checkIsNotReleased(); |
| 271 | EGLSurface surface = background ? eglSurfaceBackground : eglSurface; |
| 272 | if (surface == EGL14.EGL_NO_SURFACE) { |
| 273 | throw new RuntimeException("No EGLSurface - can't swap buffers"); |
| 274 | } |
| 275 | synchronized (EglBase.lock) { |
| 276 | // See |
| 277 | // https://android.googlesource.com/platform/frameworks/native/+/tools_r22.2/opengl/specs/EGL_ANDROID_presentation_time.txt |
| 278 | EGLExt.eglPresentationTimeANDROID(eglDisplay, surface, timeStampNs); |
| 279 | EGL14.eglSwapBuffers(eglDisplay, surface); |
| 280 | } |
| 281 | } |
| 282 |
| 283 | // Return an EGLDisplay, or die trying. |
| 284 | private static EGLDisplay getEglDisplay() { |
| 285 | EGLDisplay eglDisplay = EGL14.eglGetDisplay(EGL14.EGL_DEFAULT_DISPLAY); |
| 286 | if (eglDisplay == EGL14.EGL_NO_DISPLAY) { |
| 287 | throw new GLException(EGL14.eglGetError(), |
| 288 | "Unable to get EGL14 display: 0x" + Integer.toHexString(EGL14.eglGetError())); |
| 289 | } |
| 290 | int[] version = new int[2]; |
| 291 | if (!EGL14.eglInitialize(eglDisplay, version, 0, version, 1)) { |
| 292 | throw new GLException(EGL14.eglGetError(), |
| 293 | "Unable to initialize EGL14: 0x" + Integer.toHexString(EGL14.eglGetError())); |
| 294 | } |
| 295 | return eglDisplay; |
| 296 | } |
| 297 |
| 298 | // Return an EGLConfig, or die trying. |
| 299 | private static EGLConfig getEglConfig(EGLDisplay eglDisplay, int[] configAttributes) { |
| 300 | EGLConfig[] configs = new EGLConfig[1]; |
| 301 | int[] numConfigs = new int[1]; |
| 302 | if (!EGL14.eglChooseConfig( |
| 303 | eglDisplay, configAttributes, 0, configs, 0, configs.length, numConfigs, 0)) { |
| 304 | throw new GLException(EGL14.eglGetError(), |
| 305 | "eglChooseConfig failed: 0x" + Integer.toHexString(EGL14.eglGetError())); |
| 306 | } |
| 307 | if (numConfigs[0] <= 0) { |
| 308 | throw new RuntimeException("Unable to find any matching EGL config"); |
| 309 | } |
| 310 | final EGLConfig eglConfig = configs[0]; |
| 311 | if (eglConfig == null) { |
| 312 | throw new RuntimeException("eglChooseConfig returned null"); |
| 313 | } |
| 314 | return eglConfig; |
| 315 | } |
| 316 |
| 317 | // Return an EGLConfig, or die trying. |
| 318 | private static EGLContext createEglContext(@Nullable EGLContext sharedContext, |
| 319 | EGLDisplay eglDisplay, EGLConfig eglConfig, int openGlesVersion) { |
| 320 | if (sharedContext != null && sharedContext == EGL14.EGL_NO_CONTEXT) { |
| 321 | throw new RuntimeException("Invalid sharedContext"); |
| 322 | } |
| 323 | int[] contextAttributes = {EGL14.EGL_CONTEXT_CLIENT_VERSION, openGlesVersion, EGL14.EGL_NONE}; |
| 324 | EGLContext rootContext = sharedContext == null ? EGL14.EGL_NO_CONTEXT : sharedContext; |
| 325 | final EGLContext eglContext; |
| 326 | synchronized (EglBase.lock) { |
| 327 | eglContext = EGL14.eglCreateContext(eglDisplay, eglConfig, rootContext, contextAttributes, 0); |
| 328 | } |
| 329 | if (eglContext == EGL14.EGL_NO_CONTEXT) { |
| 330 | throw new GLException(EGL14.eglGetError(), |
| 331 | "Failed to create EGL context: 0x" + Integer.toHexString(EGL14.eglGetError())); |
| 332 | } |
| 333 | return eglContext; |
| 334 | } |
| 335 | } |
| 336 |