Skip to content

Commit

Permalink
Allow the configs parameter for eglChooseConfig to be null in shadow
Browse files Browse the repository at this point in the history
Most of the time, the output parameters for EGL methods are
expected to be non-null. But the `configs` parameter of
`eglChooseConfig` is one of the exceptions. When `configs` is
not null, `eglChooseConfig` fills that with the first
`configSize` configs and fills `numConfig` with the number of
returned configs (no more than `configsSize`). But when `configs`
is_ `null` it fills `numConfigs` with the number of _matching_
configs, ignoring `configsSize`:
https://registry.khronos.org/EGL/sdk/docs/man/html/eglChooseConfig.xhtml

The Android code wrapping this API checks that `attrib_list` and
`num_configs` output arrays are not null, but allows `configs`
to be null (see frameworks/base/core/jni/android_opengl_EGL14.cpp).

PiperOrigin-RevId: 631820937
  • Loading branch information
sfreilich authored and Copybara-Service committed May 8, 2024
1 parent 123e31a commit 3cdc3f1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,27 @@ public void eglGetDisplay() {
}

@Test
public void eglChooseConfig() {
public void eglChooseConfig_retrieveConfigs() {
EGLDisplay display = EGL14.eglGetDisplay(EGL14.EGL_DEFAULT_DISPLAY);
// When the config array is not null, numConfig is filled with the number of configs returned
// (up to the size of the config array).
EGLConfig[] configs = new EGLConfig[1];
int[] numConfig = new int[1];
assertThat(EGL14.eglChooseConfig(display, new int[0], 0, configs, 0, 1, numConfig, 0)).isTrue();
assertThat(numConfig[0]).isGreaterThan(0);
assertThat(configs[0]).isNotNull();
}

@Test
public void eglChooseConfig_countMatching() {
EGLDisplay display = EGL14.eglGetDisplay(EGL14.EGL_DEFAULT_DISPLAY);
// When the config array is null, numConfig is filled with the total number of configs matched.
EGLConfig[] configs = null;
int[] numConfig = new int[1];
assertThat(EGL14.eglChooseConfig(display, new int[0], 0, configs, 0, 0, numConfig, 0)).isTrue();
assertThat(numConfig[0]).isGreaterThan(0);
}

@Test
public void eglCreateContext_v2() {
EGLDisplay display = EGL14.eglGetDisplay(EGL14.EGL_DEFAULT_DISPLAY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ protected static boolean eglChooseConfig(
int configSize,
int[] numConfig,
int numConfigOffset) {
configs[configsOffset] = createEglConfig();
// The configs array here can be null, in which case the numConfig output is supposed to be
// set to the number of matching configs instead of the number of returned configs.
if (configs != null) {
configs[configsOffset] = createEglConfig();
}
numConfig[numConfigOffset] = 1;
return true;
}
Expand Down

0 comments on commit 3cdc3f1

Please sign in to comment.