Create the EGLContext on the thread it will be used on.

Not doing so seems to have caused issues with creating window surfaces
on that context later on.

Bug: b/225229697
Change-Id: Id202c93c4e51d1661e79a4b37751d11fcd64c119
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/311462
Reviewed-by: Xavier Lepaul‎ <xalep@webrtc.org>
Commit-Queue: Linus Nilsson <lnilsson@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#40411}
diff --git a/sdk/android/api/org/webrtc/EglThread.java b/sdk/android/api/org/webrtc/EglThread.java
index 0912210..0227f2a 100644
--- a/sdk/android/api/org/webrtc/EglThread.java
+++ b/sdk/android/api/org/webrtc/EglThread.java
@@ -33,15 +33,19 @@
     renderThread.start();
     Handler handler = new Handler(renderThread.getLooper());
 
-    // If sharedContext is null, then texture frames are disabled. This is typically for old
-    // devices that might not be fully spec compliant, so force EGL 1.0 since EGL 1.4 has
-    // caused trouble on some weird devices.
-    EglConnection eglConnection;
-    if (sharedContext == null) {
-      eglConnection = EglConnection.createEgl10(configAttributes);
-    } else {
-      eglConnection = EglConnection.create(sharedContext, configAttributes);
-    }
+    // Not creating the EGLContext on the thread it will be used on seems to cause issues with
+    // creating window surfaces on certain devices. So keep the same legacy behavior as EglRenderer
+    // and create the context on the render thread.
+    EglConnection eglConnection = ThreadUtils.invokeAtFrontUninterruptibly(handler, () -> {
+      // If sharedContext is null, then texture frames are disabled. This is typically for old
+      // devices that might not be fully spec compliant, so force EGL 1.0 since EGL 1.4 has
+      // caused trouble on some weird devices.
+      if (sharedContext == null) {
+        return EglConnection.createEgl10(configAttributes);
+      } else {
+        return EglConnection.create(sharedContext, configAttributes);
+      }
+    });
 
     return new EglThread(
         releaseMonitor != null ? releaseMonitor : eglThread -> true, handler, eglConnection);