Skip to content

Display and control your Android device

Add EGL helper

Romain Vimont763d8fc7aff4
diff --git a/app/meson.build b/app/meson.build
index 413bba5a..61b5f32f 100644
--- a/app/meson.build
+++ b/app/meson.build
@@ -15,6 +15,7 @@ src = [
'src/demuxer.c',
'src/device_msg.c',
'src/disconnect.c',
+ 'src/egl.c',
'src/events.c',
'src/file_pusher.c',
'src/fps_counter.c',
diff --git a/app/src/egl.c b/app/src/egl.c
new file mode 100644
index 00000000..27f9739b
--- /dev/null
+++ b/app/src/egl.c
@@ -0,0 +1,120 @@
+#include "egl.h"
+
+#include <assert.h>
+#include <string.h>
+#include <SDL3/SDL.h>
+
+#include "util/log.h"
+
+bool
+sc_egl_init(struct sc_egl *egl) {
+ egl->display = (EGLDisplay) SDL_EGL_GetCurrentDisplay();
+ if (egl->display == EGL_NO_DISPLAY) {
+ LOGD("EGL_NO_DISPLAY");
+ return false;
+ }
+
+ egl->QueryString = (PFNEGLQUERYSTRINGPROC)
+ SDL_EGL_GetProcAddress("eglQueryString");
+ assert(egl->QueryString);
+
+ egl->GetError = (PFNEGLGETERRORPROC)
+ SDL_EGL_GetProcAddress("eglGetError");
+ assert(egl->GetError);
+
+ egl->extensions = egl->QueryString(egl->display, EGL_EXTENSIONS);
+ if (!egl->extensions) {
+ LOGE("EGL error: Could not get EGL extensions");
+ return false;
+ }
+
+ // eglGetProcAddress() may return a pointer even for unsupported functions,
+ // so only resolve the entry points of advertised extensions
+ egl->CreateImageKHR = NULL;
+ egl->DestroyImageKHR = NULL;
+ if (sc_egl_has_extension(egl, "EGL_KHR_image_base")
+ || sc_egl_has_extension(egl, "EGL_KHR_image")) {
+ egl->CreateImageKHR = (PFNEGLCREATEIMAGEKHRPROC)
+ SDL_EGL_GetProcAddress("eglCreateImageKHR");
+ assert(egl->CreateImageKHR);
+
+ egl->DestroyImageKHR = (PFNEGLDESTROYIMAGEKHRPROC)
+ SDL_EGL_GetProcAddress("eglDestroyImageKHR");
+ assert(egl->DestroyImageKHR);
+ }
+
+ egl->EGLImageTargetTexture2DOES = NULL;
+ if (SDL_GL_ExtensionSupported("GL_OES_EGL_image")) {
+ egl->EGLImageTargetTexture2DOES = (PFNGLEGLIMAGETARGETTEXTURE2DOESPROC)
+ SDL_GL_GetProcAddress("glEGLImageTargetTexture2DOES");
+ }
+
+ egl->has_dma_buf_import =
+ sc_egl_has_extension(egl, "EGL_EXT_image_dma_buf_import");
+ egl->has_dma_buf_import_modifiers =
+ sc_egl_has_extension(egl, "EGL_EXT_image_dma_buf_import_modifiers");
+
+ return true;
+}
+
+bool
+sc_egl_has_extension(struct sc_egl *egl, const char *extension) {
+ assert(egl->extensions);
+ assert(extension);
+ assert(!strchr(extension, ' '));
+
+ size_t len = strlen(extension);
+ const char *p = egl->extensions;
+ while ((p = strstr(p, extension))) {
+ if ((p == egl->extensions || p[-1] == ' ')
+ && (p[len] == '\0' || p[len] == ' ')) {
+ return true;
+ }
+ p += len;
+ }
+
+ return false;
+}
+
+EGLImageKHR
+sc_egl_create_image_dma_buf(struct sc_egl *egl, uint32_t drm_format,
+ int width, int height,
+ const struct sc_egl_dma_buf_plane *plane) {
+ assert(egl->CreateImageKHR);
+ assert(egl->has_dma_buf_import);
+
+ EGLint attrs[20];
+ unsigned n = 0;
+ attrs[n++] = EGL_WIDTH;
+ attrs[n++] = width;
+ attrs[n++] = EGL_HEIGHT;
+ attrs[n++] = height;
+ attrs[n++] = EGL_LINUX_DRM_FOURCC_EXT;
+ attrs[n++] = (EGLint) drm_format;
+ attrs[n++] = EGL_DMA_BUF_PLANE0_FD_EXT;
+ attrs[n++] = plane->fd;
+ attrs[n++] = EGL_DMA_BUF_PLANE0_OFFSET_EXT;
+ attrs[n++] = (EGLint) plane->offset;
+ attrs[n++] = EGL_DMA_BUF_PLANE0_PITCH_EXT;
+ attrs[n++] = (EGLint) plane->pitch;
+
+ if (plane->modifier != SC_DRM_FORMAT_MOD_INVALID
+ && egl->has_dma_buf_import_modifiers) {
+ attrs[n++] = EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT;
+ attrs[n++] = (EGLint) (plane->modifier & UINT32_MAX);
+ attrs[n++] = EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT;
+ attrs[n++] = (EGLint) (plane->modifier >> 32);
+ }
+
+ attrs[n++] = EGL_NONE;
+ assert(n <= ARRAY_LEN(attrs));
+
+ return egl->CreateImageKHR(egl->display, EGL_NO_CONTEXT,
+ EGL_LINUX_DMA_BUF_EXT, NULL, attrs);
+}
+
+void
+sc_egl_destroy_image(struct sc_egl *egl, EGLImageKHR image) {
+ assert(egl->DestroyImageKHR);
+ egl->DestroyImageKHR(egl->display, image);
+}
diff --git a/app/src/egl.h b/app/src/egl.h
new file mode 100644
index 00000000..dbb1b08e
--- /dev/null
+++ b/app/src/egl.h
@@ -0,0 +1,76 @@
+#ifndef SC_EGL_H
+#define SC_EGL_H
+
+#include "common.h"
+
+#include <stdbool.h>
+#include <stdint.h>
+
+// Use the EGL definitions bundled with SDL
+#define SDL_USE_BUILTIN_OPENGL_DEFINITIONS
+#include <SDL3/SDL_egl.h>
+
+#include "opengl.h"
+
+// From GL_OES_EGL_image, only defined by the OpenGL ES headers (which cannot be
+// included along with the OpenGL headers)
+typedef void (APIENTRYP PFNGLEGLIMAGETARGETTEXTURE2DOESPROC)
+ (GLenum target, GLeglImageOES image);
+
+struct sc_egl {
+ EGLDisplay display;
+ const char *extensions; // owned by EGL
+
+ PFNEGLQUERYSTRINGPROC QueryString;
+ PFNEGLGETERRORPROC GetError;
+
+ // EGL_KHR_image_base (NULL if unsupported)
+ PFNEGLCREATEIMAGEKHRPROC CreateImageKHR;
+ PFNEGLDESTROYIMAGEKHRPROC DestroyImageKHR;
+
+ // GL_OES_EGL_image, the OpenGL side of EGLImage (NULL if unsupported)
+ PFNGLEGLIMAGETARGETTEXTURE2DOESPROC EGLImageTargetTexture2DOES;
+
+ // EGL_EXT_image_dma_buf_import
+ bool has_dma_buf_import;
+ // EGL_EXT_image_dma_buf_import_modifiers
+ bool has_dma_buf_import_modifiers;
+};
+
+// Same value as DRM_FORMAT_MOD_INVALID from drm_fourcc.h, defined here to avoid
+// depending on the libdrm headers
+#define SC_DRM_FORMAT_MOD_INVALID UINT64_C(0x00FFFFFFFFFFFFFF)
+
+struct sc_egl_dma_buf_plane {
+ int fd;
+ uint32_t offset;
+ uint32_t pitch;
+
+ // The DRM format modifier of the buffer (SC_DRM_FORMAT_MOD_INVALID if
+ // unknown), only used if the EGL implementation supports modifiers
+ uint64_t modifier;
+};
+
+bool
+sc_egl_init(struct sc_egl *egl);
+
+bool
+sc_egl_has_extension(struct sc_egl *egl, const char *extension);
+
+/**
+ * Import a single-plane DMA-BUF as an EGLImage
+ *
+ * The DRM format is a FourCC (drm_fourcc.h) that describes the plane, for
+ * example DRM_FORMAT_R8 or DRM_FORMAT_GR88.
+ *
+ * Return EGL_NO_IMAGE_KHR on error (see egl->GetError()).
+ */
+EGLImageKHR
+sc_egl_create_image_dma_buf(struct sc_egl *egl, uint32_t drm_format,
+ int width, int height,
+ const struct sc_egl_dma_buf_plane *plane);
+
+void
+sc_egl_destroy_image(struct sc_egl *egl, EGLImageKHR image);
+
+#endif