| 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.Canvas; |
| 14 | import android.graphics.Rect; |
| 15 | import android.graphics.SurfaceTexture; |
| 16 |
| 17 | import androidx.annotation.Nullable; |
| 18 |
| 19 | import android.view.Surface; |
| 20 | import android.view.SurfaceHolder; |
| 21 |
| 22 | import javax.microedition.khronos.egl.EGL10; |
| 23 | import javax.microedition.khronos.egl.EGLConfig; |
| 24 | import javax.microedition.khronos.egl.EGLContext; |
| 25 | import javax.microedition.khronos.egl.EGLDisplay; |
| 26 | import javax.microedition.khronos.egl.EGLSurface; |
| 27 |
| 28 | /** |
| 29 | * Holds EGL state and utility methods for handling an egl 1.0 EGLContext, an EGLDisplay, |
| 30 | * and an EGLSurface. |
| 31 | */ |
| 32 | class EglBase10Impl implements EglBase10 { |
| 33 | private static final String TAG = "EglBase10Impl"; |
| 34 | // This constant is taken from EGL14.EGL_CONTEXT_CLIENT_VERSION. |
| 35 | private static final int EGL_CONTEXT_CLIENT_VERSION = 0x3098; |
| 36 |
| 37 | private final EGL10 egl; |
| 38 | private EGLContext eglContext; |
| 39 | @Nullable |
| 40 | private EGLConfig eglConfig; |
| 41 | private EGLDisplay eglDisplay; |
| 42 | private EGLSurface eglSurface = EGL10.EGL_NO_SURFACE; |
| 43 | private EGLSurface eglBackgroundSurface = EGL10.EGL_NO_SURFACE; |
| 44 |
| 45 | // EGL wrapper for an actual EGLContext. |
| 46 | private static class Context implements EglBase10.Context { |
| 47 | private final EGL10 egl; |
| 48 | private final EGLContext eglContext; |
| 49 | private final EGLConfig eglContextConfig; |
| 50 |
| 51 | @Override |
| 52 | public EGLContext getRawContext() { |
| 53 | return eglContext; |
| 54 | } |
| 55 |
| 56 | @Override |
| 57 | public long getNativeEglContext() { |
| 58 | EGLContext previousContext = egl.eglGetCurrentContext(); |
| 59 | EGLDisplay currentDisplay = egl.eglGetCurrentDisplay(); |
| 60 | EGLSurface previousDrawSurface = egl.eglGetCurrentSurface(EGL10.EGL_DRAW); |
| 61 | EGLSurface previousReadSurface = egl.eglGetCurrentSurface(EGL10.EGL_READ); |
| 62 | EGLSurface tempEglSurface = null; |
| 63 |
| 64 | if (currentDisplay == EGL10.EGL_NO_DISPLAY) { |
| 65 | currentDisplay = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY); |
| 66 | } |
| 67 |
| 68 | try { |
| 69 | if (previousContext != eglContext) { |
| 70 | int[] surfaceAttribs = {EGL10.EGL_WIDTH, 1, EGL10.EGL_HEIGHT, 1, EGL10.EGL_NONE}; |
| 71 | tempEglSurface = |
| 72 | egl.eglCreatePbufferSurface(currentDisplay, eglContextConfig, surfaceAttribs); |
| 73 | if (!egl.eglMakeCurrent(currentDisplay, tempEglSurface, tempEglSurface, eglContext)) { |
| 74 | throw new RuntimeException( |
| 75 | "Failed to make temporary EGL surface active: " + egl.eglGetError()); |
| 76 | } |
| 77 | } |
| 78 |
| 79 | return nativeGetCurrentNativeEGLContext(); |
| 80 | } finally { |
| 81 | if (tempEglSurface != null) { |
| 82 | egl.eglMakeCurrent( |
| 83 | currentDisplay, previousDrawSurface, previousReadSurface, previousContext); |
| 84 | egl.eglDestroySurface(currentDisplay, tempEglSurface); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 |
| 89 | public Context(EGL10 egl, EGLContext eglContext, EGLConfig eglContextConfig) { |
| 90 | this.egl = egl; |
| 91 | this.eglContext = eglContext; |
| 92 | this.eglContextConfig = eglContextConfig; |
| 93 | } |
| 94 | } |
| 95 |
| 96 | // Create a new context with the specified config type, sharing data with sharedContext. |
| 97 | public EglBase10Impl(EGLContext sharedContext, int[] configAttributes) { |
| 98 | this.egl = (EGL10) EGLContext.getEGL(); |
| 99 | eglDisplay = getEglDisplay(); |
| 100 | eglConfig = getEglConfig(egl, eglDisplay, configAttributes); |
| 101 | final int openGlesVersion = EglBase.getOpenGlesVersionFromConfig(configAttributes); |
| 102 | Logging.d(TAG, "Using OpenGL ES version " + openGlesVersion); |
| 103 | eglContext = createEglContext(sharedContext, eglDisplay, eglConfig, openGlesVersion); |
| 104 | } |
| 105 |
| 106 | @Override |
| 107 | public void createSurface(Surface surface) { |
| 108 | createSurfaceInternal(new FakeSurfaceHolder(surface), false); |
| 109 | } |
| 110 |
| 111 | // Create EGLSurface from the Android SurfaceTexture. |
| 112 | @Override |
| 113 | public void createSurface(SurfaceTexture surfaceTexture) { |
| 114 | createSurfaceInternal(surfaceTexture, false); |
| 115 | } |
| 116 |
| 117 | // Create EGLSurface from either a SurfaceHolder or a SurfaceTexture. |
| 118 | private void createSurfaceInternal(Object nativeWindow, boolean background) { |
| 119 | if (!(nativeWindow instanceof SurfaceHolder) && !(nativeWindow instanceof SurfaceTexture)) { |
| 120 | throw new IllegalStateException("Input must be either a SurfaceHolder or SurfaceTexture"); |
| 121 | } |
| 122 | checkIsNotReleased(); |
| 123 | if (background) { |
| 124 | if (eglBackgroundSurface != EGL10.EGL_NO_SURFACE) { |
| 125 | throw new RuntimeException("Already has an EGLSurface"); |
| 126 | } |
| 127 | int[] surfaceAttribs = {EGL10.EGL_NONE}; |
| 128 | eglBackgroundSurface = egl.eglCreateWindowSurface(eglDisplay, eglConfig, nativeWindow, surfaceAttribs); |
| 129 | if (eglBackgroundSurface == EGL10.EGL_NO_SURFACE) { |
| 130 | throw new RuntimeException( |
| 131 | "Failed to create window surface: 0x" + Integer.toHexString(egl.eglGetError())); |
| 132 | } |
| 133 | } else { |
| 134 | if (eglSurface != EGL10.EGL_NO_SURFACE) { |
| 135 | throw new RuntimeException("Already has an EGLSurface"); |
| 136 | } |
| 137 | int[] surfaceAttribs = {EGL10.EGL_NONE}; |
| 138 | eglSurface = egl.eglCreateWindowSurface(eglDisplay, eglConfig, nativeWindow, surfaceAttribs); |
| 139 | if (eglSurface == EGL10.EGL_NO_SURFACE) { |
| 140 | throw new RuntimeException( |
| 141 | "Failed to create window surface: 0x" + Integer.toHexString(egl.eglGetError())); |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 |
| 146 | // Create dummy 1x1 pixel buffer surface so the context can be made current. |
| 147 | @Override |
| 148 | public void createDummyPbufferSurface() { |
| 149 | createPbufferSurface(1, 1); |
| 150 | } |
| 151 |
| 152 | @Override |
| 153 | public void createPbufferSurface(int width, int height) { |
| 154 | checkIsNotReleased(); |
| 155 | if (eglSurface != EGL10.EGL_NO_SURFACE) { |
| 156 | throw new RuntimeException("Already has an EGLSurface"); |
| 157 | } |
| 158 | int[] surfaceAttribs = {EGL10.EGL_WIDTH, width, EGL10.EGL_HEIGHT, height, EGL10.EGL_NONE}; |
| 159 | eglSurface = egl.eglCreatePbufferSurface(eglDisplay, eglConfig, surfaceAttribs); |
| 160 | if (eglSurface == EGL10.EGL_NO_SURFACE) { |
| 161 | throw new RuntimeException("Failed to create pixel buffer surface with size " + width + "x" |
| 162 | + height + ": 0x" + Integer.toHexString(egl.eglGetError())); |
| 163 | } |
| 164 | } |
| 165 |
| 166 | @Override |
| 167 | public org.webrtc.EglBase.Context getEglBaseContext() { |
| 168 | return new Context(egl, eglContext, eglConfig); |
| 169 | } |
| 170 |
| 171 | @Override |
| 172 | public boolean hasSurface() { |
| 173 | return eglSurface != EGL10.EGL_NO_SURFACE; |
| 174 | } |
| 175 |
| 176 | @Override |
| 177 | public int surfaceWidth() { |
| 178 | final int widthArray[] = new int[1]; |
| 179 | egl.eglQuerySurface(eglDisplay, eglSurface, EGL10.EGL_WIDTH, widthArray); |
| 180 | return widthArray[0]; |
| 181 | } |
| 182 |
| 183 | @Override |
| 184 | public int surfaceHeight() { |
| 185 | final int heightArray[] = new int[1]; |
| 186 | egl.eglQuerySurface(eglDisplay, eglSurface, EGL10.EGL_HEIGHT, heightArray); |
| 187 | return heightArray[0]; |
| 188 | } |
| 189 |
| 190 | @Override |
| 191 | public void releaseSurface(boolean background) { |
| 192 | if (background) { |
| 193 | if (eglBackgroundSurface != EGL10.EGL_NO_SURFACE) { |
| 194 | egl.eglDestroySurface(eglDisplay, eglBackgroundSurface); |
| 195 | eglBackgroundSurface = EGL10.EGL_NO_SURFACE; |
| 196 | } |
| 197 | } else { |
| 198 | if (eglSurface != EGL10.EGL_NO_SURFACE) { |
| 199 | egl.eglDestroySurface(eglDisplay, eglSurface); |
| 200 | eglSurface = EGL10.EGL_NO_SURFACE; |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 |
| 205 | private void checkIsNotReleased() { |
| 206 | if (eglDisplay == EGL10.EGL_NO_DISPLAY || eglContext == EGL10.EGL_NO_CONTEXT |
| 207 | || eglConfig == null) { |
| 208 | throw new RuntimeException("This object has been released"); |
| 209 | } |
| 210 | } |
| 211 |
| 212 | @Override |
| 213 | public void release() { |
| 214 | checkIsNotReleased(); |
| 215 | releaseSurface(false); |
| 216 | releaseSurface(true); |
| 217 | detachCurrent(); |
| 218 | egl.eglDestroyContext(eglDisplay, eglContext); |
| 219 | egl.eglTerminate(eglDisplay); |
| 220 | eglContext = EGL10.EGL_NO_CONTEXT; |
| 221 | eglDisplay = EGL10.EGL_NO_DISPLAY; |
| 222 | eglConfig = null; |
| 223 | } |
| 224 |
| 225 | @Override |
| 226 | public void makeCurrent() { |
| 227 | checkIsNotReleased(); |
| 228 | if (eglSurface == EGL10.EGL_NO_SURFACE) { |
| 229 | throw new RuntimeException("No EGLSurface - can't make current"); |
| 230 | } |
| 231 | synchronized (EglBase.lock) { |
| 232 | if (!egl.eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext)) { |
| 233 | throw new RuntimeException( |
| 234 | "eglMakeCurrent failed: 0x" + Integer.toHexString(egl.eglGetError())); |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 |
| 239 | // Detach the current EGL context, so that it can be made current on another thread. |
| 240 | @Override |
| 241 | public void detachCurrent() { |
| 242 | synchronized (EglBase.lock) { |
| 243 | if (!egl.eglMakeCurrent( |
| 244 | eglDisplay, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_CONTEXT)) { |
| 245 | throw new RuntimeException( |
| 246 | "eglDetachCurrent failed: 0x" + Integer.toHexString(egl.eglGetError())); |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 |
| 251 | @Override |
| 252 | public void swapBuffers(boolean background) { |
| 253 | EGLSurface surface = background ? eglBackgroundSurface : eglSurface; |
| 254 | checkIsNotReleased(); |
| 255 | if (surface == EGL10.EGL_NO_SURFACE) { |
| 256 | throw new RuntimeException("No EGLSurface - can't swap buffers"); |
| 257 | } |
| 258 | synchronized (EglBase.lock) { |
| 259 | egl.eglSwapBuffers(eglDisplay, surface); |
| 260 | } |
| 261 | } |
| 262 |
| 263 | @Override |
| 264 | public void swapBuffers(long timeStampNs, boolean background) { |
| 265 | // Setting presentation time is not supported for EGL 1.0. |
| 266 | swapBuffers(background); |
| 267 | } |
| 268 |
| 269 | @Override |
| 270 | public void createBackgroundSurface(SurfaceTexture surface) { |
| 271 | createSurfaceInternal(surface, true); |
| 272 | } |
| 273 |
| 274 | @Override |
| 275 | public void makeBackgroundCurrent() { |
| 276 | checkIsNotReleased(); |
| 277 | if (eglBackgroundSurface == EGL10.EGL_NO_SURFACE) { |
| 278 | throw new RuntimeException("No EGLSurface - can't make current"); |
| 279 | } |
| 280 | synchronized (EglBase.lock) { |
| 281 | if (!egl.eglMakeCurrent(eglDisplay, eglBackgroundSurface, eglBackgroundSurface, eglContext)) { |
| 282 | throw new RuntimeException( |
| 283 | "eglMakeCurrent failed: 0x" + Integer.toHexString(egl.eglGetError())); |
| 284 | } |
| 285 | } |
| 286 | } |
| 287 |
| 288 | @Override |
| 289 | public boolean hasBackgroundSurface() { |
| 290 | return eglBackgroundSurface != EGL10.EGL_NO_SURFACE; |
| 291 | } |
| 292 |
| 293 | // Return an EGLDisplay, or die trying. |
| 294 | private EGLDisplay getEglDisplay() { |
| 295 | EGLDisplay eglDisplay = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY); |
| 296 | if (eglDisplay == EGL10.EGL_NO_DISPLAY) { |
| 297 | throw new RuntimeException( |
| 298 | "Unable to get EGL10 display: 0x" + Integer.toHexString(egl.eglGetError())); |
| 299 | } |
| 300 | int[] version = new int[2]; |
| 301 | if (!egl.eglInitialize(eglDisplay, version)) { |
| 302 | throw new RuntimeException( |
| 303 | "Unable to initialize EGL10: 0x" + Integer.toHexString(egl.eglGetError())); |
| 304 | } |
| 305 | return eglDisplay; |
| 306 | } |
| 307 |
| 308 | // Return an EGLConfig, or die trying. |
| 309 | private static EGLConfig getEglConfig(EGL10 egl, EGLDisplay eglDisplay, int[] configAttributes) { |
| 310 | EGLConfig[] configs = new EGLConfig[1]; |
| 311 | int[] numConfigs = new int[1]; |
| 312 | if (!egl.eglChooseConfig(eglDisplay, configAttributes, configs, configs.length, numConfigs)) { |
| 313 | throw new RuntimeException( |
| 314 | "eglChooseConfig failed: 0x" + Integer.toHexString(egl.eglGetError())); |
| 315 | } |
| 316 | if (numConfigs[0] <= 0) { |
| 317 | throw new RuntimeException("Unable to find any matching EGL config"); |
| 318 | } |
| 319 | final EGLConfig eglConfig = configs[0]; |
| 320 | if (eglConfig == null) { |
| 321 | throw new RuntimeException("eglChooseConfig returned null"); |
| 322 | } |
| 323 | return eglConfig; |
| 324 | } |
| 325 |
| 326 | // Return an EGLConfig, or die trying. |
| 327 | private EGLContext createEglContext(@Nullable EGLContext sharedContext, EGLDisplay eglDisplay, |
| 328 | EGLConfig eglConfig, int openGlesVersion) { |
| 329 | if (sharedContext != null && sharedContext == EGL10.EGL_NO_CONTEXT) { |
| 330 | throw new RuntimeException("Invalid sharedContext"); |
| 331 | } |
| 332 | int[] contextAttributes = {EGL_CONTEXT_CLIENT_VERSION, openGlesVersion, EGL10.EGL_NONE}; |
| 333 | EGLContext rootContext = sharedContext == null ? EGL10.EGL_NO_CONTEXT : sharedContext; |
| 334 | final EGLContext eglContext; |
| 335 | synchronized (EglBase.lock) { |
| 336 | eglContext = egl.eglCreateContext(eglDisplay, eglConfig, rootContext, contextAttributes); |
| 337 | } |
| 338 | if (eglContext == EGL10.EGL_NO_CONTEXT) { |
| 339 | throw new RuntimeException( |
| 340 | "Failed to create EGL context: 0x" + Integer.toHexString(egl.eglGetError())); |
| 341 | } |
| 342 | return eglContext; |
| 343 | } |
| 344 |
| 345 |
| 346 | /** |
| 347 | * We have to wrap Surface in a SurfaceHolder because for some reason eglCreateWindowSurface |
| 348 | * couldn't actually take a Surface object until API 17. Older versions fortunately just call |
| 349 | * SurfaceHolder.getSurface(), so we'll do that. No other methods are relevant. |
| 350 | */ |
| 351 | private class FakeSurfaceHolder implements SurfaceHolder { |
| 352 | private final Surface surface; |
| 353 |
| 354 | FakeSurfaceHolder(Surface surface) { |
| 355 | this.surface = surface; |
| 356 | } |
| 357 |
| 358 | @Override |
| 359 | public void addCallback(Callback callback) { |
| 360 | } |
| 361 |
| 362 | @Override |
| 363 | public void removeCallback(Callback callback) { |
| 364 | } |
| 365 |
| 366 | @Override |
| 367 | public boolean isCreating() { |
| 368 | return false; |
| 369 | } |
| 370 |
| 371 | @Deprecated |
| 372 | @Override |
| 373 | public void setType(int i) { |
| 374 | } |
| 375 |
| 376 | @Override |
| 377 | public void setFixedSize(int i, int i2) { |
| 378 | } |
| 379 |
| 380 | @Override |
| 381 | public void setSizeFromLayout() { |
| 382 | } |
| 383 |
| 384 | @Override |
| 385 | public void setFormat(int i) { |
| 386 | } |
| 387 |
| 388 | @Override |
| 389 | public void setKeepScreenOn(boolean b) { |
| 390 | } |
| 391 |
| 392 | @Nullable |
| 393 | @Override |
| 394 | public Canvas lockCanvas() { |
| 395 | return null; |
| 396 | } |
| 397 |
| 398 | @Nullable |
| 399 | @Override |
| 400 | public Canvas lockCanvas(Rect rect) { |
| 401 | return null; |
| 402 | } |
| 403 |
| 404 | @Override |
| 405 | public void unlockCanvasAndPost(Canvas canvas) { |
| 406 | } |
| 407 |
| 408 | @Nullable |
| 409 | @Override |
| 410 | public Rect getSurfaceFrame() { |
| 411 | return null; |
| 412 | } |
| 413 |
| 414 | @Override |
| 415 | public Surface getSurface() { |
| 416 | return surface; |
| 417 | } |
| 418 | } |
| 419 |
| 420 | private static native long nativeGetCurrentNativeEGLContext(); |
| 421 | } |
| 422 |