Android: Expose underlying EGL context in API

This CL adds a way to extract the underlying android.opengl.EGLContext
and javax.microedition.khronos.egl.EGLContext for EglBase14 and
EglBase10 respectively. The reason is that clients can't be expected to
use only WebRTC's OpenGL code and might need to integrate with their
own GL code.

Bug: None
Change-Id: Ie00a564de45a090683542a52005da7e43c586ced
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/127888
Commit-Queue: Magnus Jedvert <magjed@webrtc.org>
Reviewed-by: Sami Kalliomäki <sakal@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#27205}
This commit is contained in:
Magnus Jedvert
2019-03-20 11:17:02 +01:00
committed by Commit Bot
parent d09bc55d3b
commit 98d85fe9a4
11 changed files with 111 additions and 41 deletions

View File

@ -26,7 +26,7 @@ import javax.microedition.khronos.egl.EGLSurface;
* Holds EGL state and utility methods for handling an egl 1.0 EGLContext, an EGLDisplay,
* and an EGLSurface.
*/
class EglBase10 implements EglBase {
class EglBase10Impl implements EglBase10 {
// This constant is taken from EGL14.EGL_CONTEXT_CLIENT_VERSION.
private static final int EGL_CONTEXT_CLIENT_VERSION = 0x3098;
@ -37,9 +37,14 @@ class EglBase10 implements EglBase {
private EGLSurface eglSurface = EGL10.EGL_NO_SURFACE;
// EGL wrapper for an actual EGLContext.
public static class Context implements EglBase.Context {
private static class Context implements EglBase10.Context {
private final EGLContext eglContext;
@Override
public EGLContext getRawContext() {
return eglContext;
}
@Override
public long getNativeEglContext() {
// TODO(magjed): Implement. There is no easy way of getting the native context for EGL 1.0. We
@ -55,7 +60,7 @@ class EglBase10 implements EglBase {
}
// Create a new context with the specified config type, sharing data with sharedContext.
public EglBase10(Context sharedContext, int[] configAttributes) {
public EglBase10Impl(EGLContext sharedContext, int[] configAttributes) {
this.egl = (EGL10) EGLContext.getEGL();
eglDisplay = getEglDisplay();
eglConfig = getEglConfig(eglDisplay, configAttributes);
@ -178,7 +183,7 @@ class EglBase10 implements EglBase {
@Override
public org.webrtc.EglBase.Context getEglBaseContext() {
return new EglBase10.Context(eglContext);
return new Context(eglContext);
}
@Override
@ -305,13 +310,12 @@ class EglBase10 implements EglBase {
// Return an EGLConfig, or die trying.
private EGLContext createEglContext(
@Nullable Context sharedContext, EGLDisplay eglDisplay, EGLConfig eglConfig) {
if (sharedContext != null && sharedContext.eglContext == EGL10.EGL_NO_CONTEXT) {
@Nullable EGLContext sharedContext, EGLDisplay eglDisplay, EGLConfig eglConfig) {
if (sharedContext != null && sharedContext == EGL10.EGL_NO_CONTEXT) {
throw new RuntimeException("Invalid sharedContext");
}
int[] contextAttributes = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL10.EGL_NONE};
EGLContext rootContext =
sharedContext == null ? EGL10.EGL_NO_CONTEXT : sharedContext.eglContext;
EGLContext rootContext = sharedContext == null ? EGL10.EGL_NO_CONTEXT : sharedContext;
final EGLContext eglContext;
synchronized (EglBase.lock) {
eglContext = egl.eglCreateContext(eglDisplay, eglConfig, rootContext, contextAttributes);

View File

@ -29,10 +29,10 @@ import org.webrtc.EglBase;
*/
@SuppressWarnings("ReferenceEquality") // We want to compare to EGL14 constants.
@TargetApi(18)
class EglBase14 implements EglBase {
class EglBase14Impl implements EglBase14 {
private static final String TAG = "EglBase14";
private static final int EGLExt_SDK_VERSION = android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
private static final int CURRENT_SDK_VERSION = android.os.Build.VERSION.SDK_INT;
private static final int EGLExt_SDK_VERSION = Build.VERSION_CODES.JELLY_BEAN_MR2;
private static final int CURRENT_SDK_VERSION = Build.VERSION.SDK_INT;
private EGLContext eglContext;
@Nullable private EGLConfig eglConfig;
private EGLDisplay eglDisplay;
@ -47,8 +47,13 @@ class EglBase14 implements EglBase {
return (CURRENT_SDK_VERSION >= EGLExt_SDK_VERSION);
}
public static class Context implements EglBase.Context {
private final android.opengl.EGLContext egl14Context;
public static class Context implements EglBase14.Context {
private final EGLContext egl14Context;
@Override
public EGLContext getRawContext() {
return egl14Context;
}
@Override
@SuppressWarnings("deprecation")
@ -65,7 +70,7 @@ class EglBase14 implements EglBase {
// Create a new context with the specified config type, sharing data with sharedContext.
// |sharedContext| may be null.
public EglBase14(EglBase14.Context sharedContext, int[] configAttributes) {
public EglBase14Impl(EGLContext sharedContext, int[] configAttributes) {
eglDisplay = getEglDisplay();
eglConfig = getEglConfig(eglDisplay, configAttributes);
eglContext = createEglContext(sharedContext, eglDisplay, eglConfig);
@ -121,7 +126,7 @@ class EglBase14 implements EglBase {
@Override
public Context getEglBaseContext() {
return new EglBase14.Context(eglContext);
return new Context(eglContext);
}
@Override
@ -258,13 +263,12 @@ class EglBase14 implements EglBase {
// Return an EGLConfig, or die trying.
private static EGLContext createEglContext(
@Nullable EglBase14.Context sharedContext, EGLDisplay eglDisplay, EGLConfig eglConfig) {
if (sharedContext != null && sharedContext.egl14Context == EGL14.EGL_NO_CONTEXT) {
@Nullable EGLContext sharedContext, EGLDisplay eglDisplay, EGLConfig eglConfig) {
if (sharedContext != null && sharedContext == EGL14.EGL_NO_CONTEXT) {
throw new RuntimeException("Invalid sharedContext");
}
int[] contextAttributes = {EGL14.EGL_CONTEXT_CLIENT_VERSION, 2, EGL14.EGL_NONE};
EGLContext rootContext =
sharedContext == null ? EGL14.EGL_NO_CONTEXT : sharedContext.egl14Context;
EGLContext rootContext = sharedContext == null ? EGL14.EGL_NO_CONTEXT : sharedContext;
final EGLContext eglContext;
synchronized (EglBase.lock) {
eglContext = EGL14.eglCreateContext(eglDisplay, eglConfig, rootContext, contextAttributes, 0);

View File

@ -215,7 +215,7 @@ class HardwareVideoEncoder implements VideoEncoder {
format, null /* surface */, null /* crypto */, MediaCodec.CONFIGURE_FLAG_ENCODE);
if (useSurfaceMode) {
textureEglBase = new EglBase14(sharedContext, EglBase.CONFIG_RECORDABLE);
textureEglBase = EglBase.createEgl14(sharedContext, EglBase.CONFIG_RECORDABLE);
textureInputSurface = codec.createInputSurface();
textureEglBase.createSurface(textureInputSurface);
textureEglBase.makeCurrent();