Commit 0c4b4860 by tofunmi Committed by Rohit Singh

Add context sharing capabilities to the default GlObjectsProvider

Creates a way for apps to provide their EGLContext to DefaultVideoFrameProcessor, so that we can attach their context to the one we create. See [the EGL docs for more information about how contexts are shared in GL](https://registry.khronos.org/EGL/sdk/docs/man/html/eglCreateContext.xhtml)

PiperOrigin-RevId: 525708652
parent e020e159
...@@ -31,16 +31,17 @@ import androidx.media3.common.util.UnstableApi; ...@@ -31,16 +31,17 @@ import androidx.media3.common.util.UnstableApi;
@UnstableApi @UnstableApi
public interface GlObjectsProvider { public interface GlObjectsProvider {
/** /**
* Provider for GL objects that configures a GL context with 8-bit RGB or 10-bit RGB attributes, * @deprecated Please use {@code DefaultGlObjectsProvider} in {@code androidx.media3.effect}.
* and no depth buffer or render buffers.
*/ */
@Deprecated
GlObjectsProvider DEFAULT = GlObjectsProvider DEFAULT =
new GlObjectsProvider() { new GlObjectsProvider() {
@Override @Override
@RequiresApi(17) @RequiresApi(17)
public EGLContext createEglContext( public EGLContext createEglContext(
EGLDisplay eglDisplay, int openGlVersion, int[] configAttributes) throws GlException { EGLDisplay eglDisplay, int openGlVersion, int[] configAttributes) throws GlException {
return GlUtil.createEglContext(eglDisplay, openGlVersion, configAttributes); return GlUtil.createEglContext(
EGL14.EGL_NO_CONTEXT, eglDisplay, openGlVersion, configAttributes);
} }
@Override @Override
......
...@@ -249,12 +249,14 @@ public final class GlUtil { ...@@ -249,12 +249,14 @@ public final class GlUtil {
*/ */
@RequiresApi(17) @RequiresApi(17)
public static EGLContext createEglContext(EGLDisplay eglDisplay) throws GlException { public static EGLContext createEglContext(EGLDisplay eglDisplay) throws GlException {
return createEglContext(eglDisplay, /* openGlVersion= */ 2, EGL_CONFIG_ATTRIBUTES_RGBA_8888); return createEglContext(
EGL14.EGL_NO_CONTEXT, eglDisplay, /* openGlVersion= */ 2, EGL_CONFIG_ATTRIBUTES_RGBA_8888);
} }
/** /**
* Creates a new {@link EGLContext} for the specified {@link EGLDisplay}. * Creates a new {@link EGLContext} for the specified {@link EGLDisplay}.
* *
* @param sharedContext The {@link EGLContext} with which to share data.
* @param eglDisplay The {@link EGLDisplay} to create an {@link EGLContext} for. * @param eglDisplay The {@link EGLDisplay} to create an {@link EGLContext} for.
* @param openGlVersion The version of OpenGL ES to configure. Accepts either {@code 2}, for * @param openGlVersion The version of OpenGL ES to configure. Accepts either {@code 2}, for
* OpenGL ES 2.0, or {@code 3}, for OpenGL ES 3.0. * OpenGL ES 2.0, or {@code 3}, for OpenGL ES 3.0.
...@@ -263,13 +265,16 @@ public final class GlUtil { ...@@ -263,13 +265,16 @@ public final class GlUtil {
*/ */
@RequiresApi(17) @RequiresApi(17)
public static EGLContext createEglContext( public static EGLContext createEglContext(
EGLDisplay eglDisplay, @IntRange(from = 2, to = 3) int openGlVersion, int[] configAttributes) EGLContext sharedContext,
EGLDisplay eglDisplay,
@IntRange(from = 2, to = 3) int openGlVersion,
int[] configAttributes)
throws GlException { throws GlException {
checkArgument( checkArgument(
Arrays.equals(configAttributes, EGL_CONFIG_ATTRIBUTES_RGBA_8888) Arrays.equals(configAttributes, EGL_CONFIG_ATTRIBUTES_RGBA_8888)
|| Arrays.equals(configAttributes, EGL_CONFIG_ATTRIBUTES_RGBA_1010102)); || Arrays.equals(configAttributes, EGL_CONFIG_ATTRIBUTES_RGBA_1010102));
checkArgument(openGlVersion == 2 || openGlVersion == 3); checkArgument(openGlVersion == 2 || openGlVersion == 3);
return Api17.createEglContext(eglDisplay, openGlVersion, configAttributes); return Api17.createEglContext(sharedContext, eglDisplay, openGlVersion, configAttributes);
} }
/** /**
...@@ -691,13 +696,14 @@ public final class GlUtil { ...@@ -691,13 +696,14 @@ public final class GlUtil {
@DoNotInline @DoNotInline
public static EGLContext createEglContext( public static EGLContext createEglContext(
EGLDisplay eglDisplay, int version, int[] configAttributes) throws GlException { EGLContext sharedContext, EGLDisplay eglDisplay, int version, int[] configAttributes)
throws GlException {
int[] contextAttributes = {EGL14.EGL_CONTEXT_CLIENT_VERSION, version, EGL14.EGL_NONE}; int[] contextAttributes = {EGL14.EGL_CONTEXT_CLIENT_VERSION, version, EGL14.EGL_NONE};
EGLContext eglContext = EGLContext eglContext =
EGL14.eglCreateContext( EGL14.eglCreateContext(
eglDisplay, eglDisplay,
getEglConfig(eglDisplay, configAttributes), getEglConfig(eglDisplay, configAttributes),
EGL14.EGL_NO_CONTEXT, sharedContext,
contextAttributes, contextAttributes,
/* offset= */ 0); /* offset= */ 0);
if (eglContext == null) { if (eglContext == null) {
......
/*
* Copyright 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package androidx.media3.effect;
import android.opengl.EGL14;
import android.opengl.EGLContext;
import android.opengl.EGLDisplay;
import android.opengl.EGLSurface;
import androidx.annotation.Nullable;
import androidx.media3.common.C;
import androidx.media3.common.GlObjectsProvider;
import androidx.media3.common.GlTextureInfo;
import androidx.media3.common.util.GlUtil;
import androidx.media3.common.util.UnstableApi;
// TODO(b/261820382): Add tests for sharing context.
/**
* Implementation of {@link GlObjectsProvider} that configures an {@link EGLContext} to share data
* with a preexisting {@code sharedEglContext}.
*
* <p>The created {@link EGLContext} is configured with 8-bit RGB or 10-bit RGB attributes and no
* depth buffer or render buffers.
*/
@UnstableApi
public final class DefaultGlObjectsProvider implements GlObjectsProvider {
private final EGLContext sharedEglContext;
/**
* Creates an instance.
*
* @param sharedEglContext The {@link EGLContext} with which to share data.
*/
public DefaultGlObjectsProvider(@Nullable EGLContext sharedEglContext) {
this.sharedEglContext = sharedEglContext != null ? sharedEglContext : EGL14.EGL_NO_CONTEXT;
}
@Override
public EGLContext createEglContext(
EGLDisplay eglDisplay, int openGlVersion, int[] configAttributes) throws GlUtil.GlException {
return GlUtil.createEglContext(sharedEglContext, eglDisplay, openGlVersion, configAttributes);
}
@Override
public EGLSurface createEglSurface(
EGLDisplay eglDisplay,
Object surface,
@C.ColorTransfer int colorTransfer,
boolean isEncoderInputSurface)
throws GlUtil.GlException {
return GlUtil.createEglSurface(eglDisplay, surface, colorTransfer, isEncoderInputSurface);
}
@Override
public EGLSurface createFocusedPlaceholderEglSurface(
EGLContext eglContext, EGLDisplay eglDisplay, int[] configAttributes)
throws GlUtil.GlException {
return GlUtil.createFocusedPlaceholderEglSurface(eglContext, eglDisplay, configAttributes);
}
@Override
public GlTextureInfo createBuffersForTexture(int texId, int width, int height)
throws GlUtil.GlException {
int fboId = GlUtil.createFboForTexture(texId);
return new GlTextureInfo(texId, fboId, /* rboId= */ C.INDEX_UNSET, width, height);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment