1- /*
2- * Copyright 2015 The WebRTC project authors. All Rights Reserved.
3- *
4- * Use of this source code is governed by a BSD-style license
5- * that can be found in the LICENSE file in the root of the source
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-
111package org .webrtc ;
122
133import android .graphics .SurfaceTexture ;
@@ -34,6 +24,38 @@ public interface Context {
3424 long getNativeEglContext ();
3525 }
3626
27+ /**
28+ * Wraps the objects needed to interact with EGL that are independent of a particular EGLSurface.
29+ * In practice this means EGLContext, EGLDisplay and EGLConfig objects. Separating them out in a
30+ * standalone object allows for multiple EglBase instances to use the same underlying EGLContext,
31+ * while still operating on their own EGLSurface.
32+ */
33+ public interface EglConnection extends RefCounted {
34+ /** Analogous to corresponding EglBase#create below. */
35+ public static EglConnection create (@ Nullable Context sharedContext , int [] configAttributes ) {
36+ if (sharedContext == null ) {
37+ return EglConnection .createEgl14 (configAttributes );
38+ } else if (sharedContext instanceof EglBase14 .Context ) {
39+ return new EglBase14Impl .EglConnection (
40+ ((EglBase14 .Context ) sharedContext ).getRawContext (), configAttributes );
41+ } else if (sharedContext instanceof EglBase10 .Context ) {
42+ return new EglBase10Impl .EglConnection (
43+ ((EglBase10 .Context ) sharedContext ).getRawContext (), configAttributes );
44+ }
45+ throw new IllegalArgumentException ("Unrecognized Context" );
46+ }
47+
48+ /** Analogous to corresponding EglBase#createEgl10 below. */
49+ public static EglConnection createEgl10 (int [] configAttributes ) {
50+ return new EglBase10Impl .EglConnection (/* sharedContext= */ null , configAttributes );
51+ }
52+
53+ /** Analogous to corresponding EglBase#createEgl14 below. */
54+ public static EglConnection createEgl14 (int [] configAttributes ) {
55+ return new EglBase14Impl .EglConnection (/* sharedContext= */ null , configAttributes );
56+ }
57+ }
58+
3759 // According to the documentation, EGL can be used from multiple threads at the same time if each
3860 // thread has its own EGLContext, but in practice it deadlocks on some devices when doing this.
3961 // Therefore, synchronize on this global lock before calling dangerous EGL functions that might
@@ -118,15 +140,15 @@ public int[] createConfigAttributes() {
118140
119141 public static final int [] CONFIG_PLAIN = configBuilder ().createConfigAttributes ();
120142 public static final int [] CONFIG_RGBA =
121- configBuilder ().setHasAlphaChannel (true ).createConfigAttributes ();
143+ configBuilder ().setHasAlphaChannel (true ).createConfigAttributes ();
122144 public static final int [] CONFIG_PIXEL_BUFFER =
123- configBuilder ().setSupportsPixelBuffer (true ).createConfigAttributes ();
145+ configBuilder ().setSupportsPixelBuffer (true ).createConfigAttributes ();
124146 public static final int [] CONFIG_PIXEL_RGBA_BUFFER = configBuilder ()
125- .setHasAlphaChannel (true )
126- .setSupportsPixelBuffer (true )
127- .createConfigAttributes ();
147+ .setHasAlphaChannel (true )
148+ .setSupportsPixelBuffer (true )
149+ .createConfigAttributes ();
128150 public static final int [] CONFIG_RECORDABLE =
129- configBuilder ().setIsRecordable (true ).createConfigAttributes ();
151+ configBuilder ().setIsRecordable (true ).createConfigAttributes ();
130152
131153 static int getOpenGlesVersionFromConfig (int [] configAttributes ) {
132154 for (int i = 0 ; i < configAttributes .length - 1 ; ++i ) {
@@ -145,6 +167,24 @@ static int getOpenGlesVersionFromConfig(int[] configAttributes) {
145167 return 1 ;
146168 }
147169
170+ /**
171+ * Creates a new EglBase with a shared EglConnection. EglBase instances sharing the same
172+ * EglConnection should be used on the same thread to avoid the underlying EGLContext being made
173+ * current on multiple threads. It is up to the client of EglBase to ensure that instances with a
174+ * shared EglConnection are current on that thread before each use since other EglBase instances
175+ * may have used the same EGLContext since the last interaction.
176+ */
177+ public static EglBase create (EglConnection eglConnection ) {
178+ if (eglConnection == null ) {
179+ return create ();
180+ } else if (eglConnection instanceof EglBase14Impl .EglConnection ) {
181+ return new EglBase14Impl ((EglBase14Impl .EglConnection ) eglConnection );
182+ } else if (eglConnection instanceof EglBase10Impl .EglConnection ) {
183+ return new EglBase10Impl ((EglBase10Impl .EglConnection ) eglConnection );
184+ }
185+ throw new IllegalArgumentException ("Unrecognized EglConnection" );
186+ }
187+
148188 /**
149189 * Create a new context with the specified config attributes, sharing data with `sharedContext`.
150190 * If `sharedContext` is null, a root EGL 1.4 context is created.
@@ -187,15 +227,15 @@ public static EglBase10 createEgl10(int[] configAttributes) {
187227 */
188228 public static EglBase10 createEgl10 (EglBase10 .Context sharedContext , int [] configAttributes ) {
189229 return new EglBase10Impl (
190- sharedContext == null ? null : sharedContext .getRawContext (), configAttributes );
230+ sharedContext == null ? null : sharedContext .getRawContext (), configAttributes );
191231 }
192232
193233 /**
194234 * Explicitly create a root EGl 1.0 context with the specified config attributes
195235 * and shared context.
196236 */
197237 public static EglBase10 createEgl10 (
198- javax .microedition .khronos .egl .EGLContext sharedContext , int [] configAttributes ) {
238+ javax .microedition .khronos .egl .EGLContext sharedContext , int [] configAttributes ) {
199239 return new EglBase10Impl (sharedContext , configAttributes );
200240 }
201241
@@ -210,15 +250,15 @@ public static EglBase14 createEgl14(int[] configAttributes) {
210250 */
211251 public static EglBase14 createEgl14 (EglBase14 .Context sharedContext , int [] configAttributes ) {
212252 return new EglBase14Impl (
213- sharedContext == null ? null : sharedContext .getRawContext (), configAttributes );
253+ sharedContext == null ? null : sharedContext .getRawContext (), configAttributes );
214254 }
215255
216256 /**
217257 * Explicitly create a root EGl 1.4 context with the specified config attributes
218258 * and shared context.
219259 */
220260 public static EglBase14 createEgl14 (
221- android .opengl .EGLContext sharedContext , int [] configAttributes ) {
261+ android .opengl .EGLContext sharedContext , int [] configAttributes ) {
222262 return new EglBase14Impl (sharedContext , configAttributes );
223263 }
224264
@@ -252,4 +292,4 @@ public static EglBase14 createEgl14(
252292 void swapBuffers ();
253293
254294 void swapBuffers (long presentationTimeStampNs );
255- }
295+ }
0 commit comments