From f6233c69be980cfe47f88595047dae42cbb4f7a4 Mon Sep 17 00:00:00 2001
From: Alyssa Rosenzweig <alyssa@collabora.com>
Date: Thu, 11 Aug 2022 16:00:54 +0000
Subject: [PATCH] Make it work on libmali surfaceless

Signed-off-by: Alyssa Rosenzweig <alyssa@collabora.com>
---
 includes/EGL/eglext.h             | 16 ++++++++++++++++
 tests-3d/test-triangle-smoothed.c |  2 +-
 tests-3d/test-util-3d.h           | 23 +++++++++++++++++++++++
 3 files changed, 40 insertions(+), 1 deletion(-)

diff --git a/includes/EGL/eglext.h b/includes/EGL/eglext.h
index 10d3ac7..ec7d562 100644
--- a/includes/EGL/eglext.h
+++ b/includes/EGL/eglext.h
@@ -248,6 +248,22 @@ typedef EGLBoolean (EGLAPIENTRYP PFNEGLPOSTSUBBUFFERNVPROC) (EGLDisplay dpy, EGL
 #  define EGL_GL_VIDEO_YUV_FLAGS_ROB			0x3336	/* eglCreateImageKHR attribute */
 #endif
 
+#ifndef EGL_EXT_platform_base
+#define EGL_EXT_platform_base 1
+typedef EGLDisplay (EGLAPIENTRYP PFNEGLGETPLATFORMDISPLAYEXTPROC) (EGLenum platform, void *native_display, const EGLint *attrib_list);
+typedef EGLSurface (EGLAPIENTRYP PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC) (EGLDisplay dpy, EGLConfig config, void *native_window, const EGLint *attrib_list);
+typedef EGLSurface (EGLAPIENTRYP PFNEGLCREATEPLATFORMPIXMAPSURFACEEXTPROC) (EGLDisplay dpy, EGLConfig config, void *native_pixmap, const EGLint *attrib_list);
+#ifdef EGL_EGLEXT_PROTOTYPES
+EGLAPI EGLDisplay EGLAPIENTRY eglGetPlatformDisplayEXT (EGLenum platform, void *native_display, const EGLint *attrib_list);
+EGLAPI EGLSurface EGLAPIENTRY eglCreatePlatformWindowSurfaceEXT (EGLDisplay dpy, EGLConfig config, void *native_window, const EGLint *attrib_list);
+EGLAPI EGLSurface EGLAPIENTRY eglCreatePlatformPixmapSurfaceEXT (EGLDisplay dpy, EGLConfig config, void *native_pixmap, const EGLint *attrib_list);
+#endif
+#endif /* EGL_EXT_platform_base */
+
+#ifndef EGL_KHR_platform_gbm
+#define EGL_KHR_platform_gbm 1
+#define EGL_PLATFORM_GBM_KHR              0x31D7
+#endif /* EGL_KHR_platform_gbm */
 
 #ifdef __cplusplus
 }
diff --git a/tests-3d/test-triangle-smoothed.c b/tests-3d/test-triangle-smoothed.c
index 582679b..e88a6bf 100644
--- a/tests-3d/test-triangle-smoothed.c
+++ b/tests-3d/test-triangle-smoothed.c
@@ -33,7 +33,7 @@ static EGLint const config_attribute_list[] = {
 	EGL_RED_SIZE, 8,
 	EGL_GREEN_SIZE, 8,
 	EGL_BLUE_SIZE, 8,
-	EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
+	EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
 	EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
 	EGL_DEPTH_SIZE, 8,
 	EGL_NONE
diff --git a/tests-3d/test-util-3d.h b/tests-3d/test-util-3d.h
index 774f18c..55f50ca 100644
--- a/tests-3d/test-util-3d.h
+++ b/tests-3d/test-util-3d.h
@@ -28,6 +28,7 @@
 #include "test-util-common.h"
 
 #include <EGL/egl.h>
+#include <EGL/eglext.h>
 #ifndef GL_ES_VERSION_2_0
 #include <GLES2/gl2.h>
 #include <GLES2/gl2ext.h>
@@ -432,10 +433,15 @@ glStrError(GLenum error)
 #  include <wayland-server.h>
 #  include <wayland-client-protocol.h>
 #  include <wayland-egl.h>
+#elif defined(SUPPORT_SL)
+#include <gbm.h>
 #endif
 
 #if defined(SUPPORT_WAYLAND)
 static struct wl_display *wl_display;
+#elif defined(SUPPORT_SL)
+struct gbm_device *gbm_dev;
+struct gbm_surface *gbm_surface;
 #else
 static EGLNativeDisplayType native_dpy;
 #endif
@@ -457,6 +463,20 @@ get_display(void)
 #elif defined(SUPPORT_WAYLAND)
 	wl_display = wl_display_connect(NULL);
 	display = eglGetDisplay((EGLNativeDisplayType) wl_display);
+#elif defined(SUPPORT_SL)
+	/* TODO */
+	int drm_fd = open("/dev/dri/card0", O_RDWR);
+
+	if (drm_fd < 0) {
+		ERROR_MSG("no DRM card found\n");
+		exit(-1);
+	}
+
+	gbm_dev = gbm_create_device(drm_fd);
+
+	PFNEGLGETPLATFORMDISPLAYEXTPROC func = NULL;
+	func = (void *) eglGetProcAddress("eglGetPlatformDisplayEXT");
+	display = func(EGL_PLATFORM_GBM_KHR, gbm_dev, NULL);
 #else
 	native_dpy = XOpenDisplay(NULL);
 	display = eglGetDisplay(native_dpy);
@@ -550,6 +570,9 @@ static EGLSurface make_window(EGLDisplay display, EGLConfig config, int width, i
 	}
 
 	surface = eglCreateWindowSurface(display, config, egl_window, NULL);
+#elif defined(SUPPORT_SL)
+	gbm_surface = gbm_surface_create(gbm_dev, width, height, GBM_FORMAT_XRGB8888, GBM_BO_USE_RENDERING);
+	surface = eglCreateWindowSurface(display, config, gbm_surface, NULL);
 #else
 	XVisualInfo *visInfo, visTemplate;
 	int num_visuals;
-- 
2.30.2

