Throw EGL errors to GLExceptions.

Bug: webrtc:13359
Change-Id: I1528fcd4cd0a5fc243baccd61fc4032cd0db4004
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/237141
Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org>
Reviewed-by: Xavier Lepaul‎ <xalep@webrtc.org>
Commit-Queue: Xavier Lepaul‎ <xalep@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#35313}
diff --git a/sdk/android/src/java/org/webrtc/EglBase10Impl.java b/sdk/android/src/java/org/webrtc/EglBase10Impl.java
index 14b9141..254a17c 100644
--- a/sdk/android/src/java/org/webrtc/EglBase10Impl.java
+++ b/sdk/android/src/java/org/webrtc/EglBase10Impl.java
@@ -13,6 +13,8 @@
 import android.graphics.Canvas;
 import android.graphics.Rect;
 import android.graphics.SurfaceTexture;
+import android.opengl.EGL14;
+import android.opengl.GLException;
 import android.view.Surface;
 import android.view.SurfaceHolder;
 import androidx.annotation.Nullable;
@@ -66,7 +68,7 @@
           tempEglSurface =
               egl.eglCreatePbufferSurface(currentDisplay, eglContextConfig, surfaceAttribs);
           if (!egl.eglMakeCurrent(currentDisplay, tempEglSurface, tempEglSurface, eglContext)) {
-            throw new RuntimeException(
+            throw new GLException(egl.eglGetError(),
                 "Failed to make temporary EGL surface active: " + egl.eglGetError());
           }
         }
@@ -187,7 +189,7 @@
     int[] surfaceAttribs = {EGL10.EGL_NONE};
     eglSurface = egl.eglCreateWindowSurface(eglDisplay, eglConfig, nativeWindow, surfaceAttribs);
     if (eglSurface == EGL10.EGL_NO_SURFACE) {
-      throw new RuntimeException(
+      throw new GLException(egl.eglGetError(),
           "Failed to create window surface: 0x" + Integer.toHexString(egl.eglGetError()));
     }
   }
@@ -207,8 +209,9 @@
     int[] surfaceAttribs = {EGL10.EGL_WIDTH, width, EGL10.EGL_HEIGHT, height, EGL10.EGL_NONE};
     eglSurface = egl.eglCreatePbufferSurface(eglDisplay, eglConfig, surfaceAttribs);
     if (eglSurface == EGL10.EGL_NO_SURFACE) {
-      throw new RuntimeException("Failed to create pixel buffer surface with size " + width + "x"
-          + height + ": 0x" + Integer.toHexString(egl.eglGetError()));
+      throw new GLException(egl.eglGetError(),
+          "Failed to create pixel buffer surface with size " + width + "x" + height + ": 0x"
+              + Integer.toHexString(egl.eglGetError()));
     }
   }
 
@@ -271,7 +274,7 @@
     }
     synchronized (EglBase.lock) {
       if (!egl.eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext)) {
-        throw new RuntimeException(
+        throw new GLException(egl.eglGetError(),
             "eglMakeCurrent failed: 0x" + Integer.toHexString(egl.eglGetError()));
       }
     }
@@ -283,7 +286,7 @@
     synchronized (EglBase.lock) {
       if (!egl.eglMakeCurrent(
               eglDisplay, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_CONTEXT)) {
-        throw new RuntimeException(
+        throw new GLException(egl.eglGetError(),
             "eglDetachCurrent failed: 0x" + Integer.toHexString(egl.eglGetError()));
       }
     }
@@ -310,12 +313,12 @@
   private EGLDisplay getEglDisplay() {
     EGLDisplay eglDisplay = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
     if (eglDisplay == EGL10.EGL_NO_DISPLAY) {
-      throw new RuntimeException(
+      throw new GLException(egl.eglGetError(),
           "Unable to get EGL10 display: 0x" + Integer.toHexString(egl.eglGetError()));
     }
     int[] version = new int[2];
     if (!egl.eglInitialize(eglDisplay, version)) {
-      throw new RuntimeException(
+      throw new GLException(egl.eglGetError(),
           "Unable to initialize EGL10: 0x" + Integer.toHexString(egl.eglGetError()));
     }
     return eglDisplay;
@@ -326,8 +329,8 @@
     EGLConfig[] configs = new EGLConfig[1];
     int[] numConfigs = new int[1];
     if (!egl.eglChooseConfig(eglDisplay, configAttributes, configs, configs.length, numConfigs)) {
-      throw new RuntimeException(
-          "eglChooseConfig failed: 0x" + Integer.toHexString(egl.eglGetError()));
+      throw new GLException(
+          egl.eglGetError(), "eglChooseConfig failed: 0x" + Integer.toHexString(egl.eglGetError()));
     }
     if (numConfigs[0] <= 0) {
       throw new RuntimeException("Unable to find any matching EGL config");
@@ -352,7 +355,7 @@
       eglContext = egl.eglCreateContext(eglDisplay, eglConfig, rootContext, contextAttributes);
     }
     if (eglContext == EGL10.EGL_NO_CONTEXT) {
-      throw new RuntimeException(
+      throw new GLException(egl.eglGetError(),
           "Failed to create EGL context: 0x" + Integer.toHexString(egl.eglGetError()));
     }
     return eglContext;
diff --git a/sdk/android/src/java/org/webrtc/EglBase14Impl.java b/sdk/android/src/java/org/webrtc/EglBase14Impl.java
index 3e319ba..aa27cf8 100644
--- a/sdk/android/src/java/org/webrtc/EglBase14Impl.java
+++ b/sdk/android/src/java/org/webrtc/EglBase14Impl.java
@@ -18,6 +18,7 @@
 import android.opengl.EGLDisplay;
 import android.opengl.EGLExt;
 import android.opengl.EGLSurface;
+import android.opengl.GLException;
 import android.os.Build;
 import android.view.Surface;
 import androidx.annotation.Nullable;
@@ -102,7 +103,7 @@
     int[] surfaceAttribs = {EGL14.EGL_NONE};
     eglSurface = EGL14.eglCreateWindowSurface(eglDisplay, eglConfig, surface, surfaceAttribs, 0);
     if (eglSurface == EGL14.EGL_NO_SURFACE) {
-      throw new RuntimeException(
+      throw new GLException(EGL14.eglGetError(),
           "Failed to create window surface: 0x" + Integer.toHexString(EGL14.eglGetError()));
     }
   }
@@ -121,8 +122,9 @@
     int[] surfaceAttribs = {EGL14.EGL_WIDTH, width, EGL14.EGL_HEIGHT, height, EGL14.EGL_NONE};
     eglSurface = EGL14.eglCreatePbufferSurface(eglDisplay, eglConfig, surfaceAttribs, 0);
     if (eglSurface == EGL14.EGL_NO_SURFACE) {
-      throw new RuntimeException("Failed to create pixel buffer surface with size " + width + "x"
-          + height + ": 0x" + Integer.toHexString(EGL14.eglGetError()));
+      throw new GLException(EGL14.eglGetError(),
+          "Failed to create pixel buffer surface with size " + width + "x" + height + ": 0x"
+              + Integer.toHexString(EGL14.eglGetError()));
     }
   }
 
@@ -188,7 +190,7 @@
     }
     synchronized (EglBase.lock) {
       if (!EGL14.eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext)) {
-        throw new RuntimeException(
+        throw new GLException(EGL14.eglGetError(),
             "eglMakeCurrent failed: 0x" + Integer.toHexString(EGL14.eglGetError()));
       }
     }
@@ -200,7 +202,7 @@
     synchronized (EglBase.lock) {
       if (!EGL14.eglMakeCurrent(
               eglDisplay, EGL14.EGL_NO_SURFACE, EGL14.EGL_NO_SURFACE, EGL14.EGL_NO_CONTEXT)) {
-        throw new RuntimeException(
+        throw new GLException(EGL14.eglGetError(),
             "eglDetachCurrent failed: 0x" + Integer.toHexString(EGL14.eglGetError()));
       }
     }
@@ -235,12 +237,12 @@
   private static EGLDisplay getEglDisplay() {
     EGLDisplay eglDisplay = EGL14.eglGetDisplay(EGL14.EGL_DEFAULT_DISPLAY);
     if (eglDisplay == EGL14.EGL_NO_DISPLAY) {
-      throw new RuntimeException(
+      throw new GLException(EGL14.eglGetError(),
           "Unable to get EGL14 display: 0x" + Integer.toHexString(EGL14.eglGetError()));
     }
     int[] version = new int[2];
     if (!EGL14.eglInitialize(eglDisplay, version, 0, version, 1)) {
-      throw new RuntimeException(
+      throw new GLException(EGL14.eglGetError(),
           "Unable to initialize EGL14: 0x" + Integer.toHexString(EGL14.eglGetError()));
     }
     return eglDisplay;
@@ -252,7 +254,7 @@
     int[] numConfigs = new int[1];
     if (!EGL14.eglChooseConfig(
             eglDisplay, configAttributes, 0, configs, 0, configs.length, numConfigs, 0)) {
-      throw new RuntimeException(
+      throw new GLException(EGL14.eglGetError(),
           "eglChooseConfig failed: 0x" + Integer.toHexString(EGL14.eglGetError()));
     }
     if (numConfigs[0] <= 0) {
@@ -278,7 +280,7 @@
       eglContext = EGL14.eglCreateContext(eglDisplay, eglConfig, rootContext, contextAttributes, 0);
     }
     if (eglContext == EGL14.EGL_NO_CONTEXT) {
-      throw new RuntimeException(
+      throw new GLException(EGL14.eglGetError(),
           "Failed to create EGL context: 0x" + Integer.toHexString(EGL14.eglGetError()));
     }
     return eglContext;