Merge pull request #236 from ascent12/gl-loader

Add GL/EGL extension loader generator
This commit is contained in:
Drew DeVault 2017-10-08 10:04:33 -04:00 committed by GitHub
commit 1908d88fab
9 changed files with 143 additions and 77 deletions

View File

@ -14,6 +14,7 @@
#include <wlr/render/gles2.h> #include <wlr/render/gles2.h>
#include <wlr/render.h> #include <wlr/render.h>
#include "backend/drm/drm.h" #include "backend/drm/drm.h"
#include "render/glapi.h"
bool wlr_drm_renderer_init(struct wlr_drm_backend *drm, bool wlr_drm_renderer_init(struct wlr_drm_backend *drm,
struct wlr_drm_renderer *renderer) { struct wlr_drm_renderer *renderer) {
@ -191,7 +192,7 @@ static struct wlr_texture *get_tex_for_bo(struct wlr_drm_renderer *renderer, str
EGL_NONE, EGL_NONE,
}; };
tex->img = renderer->egl.eglCreateImageKHR(renderer->egl.display, EGL_NO_CONTEXT, tex->img = eglCreateImageKHR(renderer->egl.display, EGL_NO_CONTEXT,
EGL_LINUX_DMA_BUF_EXT, NULL, attribs); EGL_LINUX_DMA_BUF_EXT, NULL, attribs);
if (!tex->img) { if (!tex->img) {
wlr_log(L_ERROR, "Failed to create EGL image: %s", egl_error()); wlr_log(L_ERROR, "Failed to create EGL image: %s", egl_error());

View File

@ -38,5 +38,5 @@ lib_wlr_backend = static_library(
'wlr_backend', 'wlr_backend',
backend_files, backend_files,
include_directories: wlr_inc, include_directories: wlr_inc,
dependencies: [wayland_server, egl, gbm, libinput, systemd, elogind, wlr_protos], dependencies: [wayland_server, egl, gbm, libinput, systemd, elogind, wlr_render, wlr_protos],
) )

91
glgen.sh Executable file
View File

@ -0,0 +1,91 @@
#!/bin/sh
# Generates a simple GL/EGL extension function loader.
#
# The input is a .txt file, with each function to load on its own line.
# If a line starts with a -, it is optional, and will not cause the loader
# to fail if it can't load the function. You'll need to check if that function
# is NULL before using it.
if [ $# -ne 2 ]; then
exit 1
fi
SPEC=$1
OUT=$2
BASE=$(basename "$SPEC" .txt)
INCLUDE_GUARD=$(printf %s "$SPEC" | tr -c [:alnum:] _ | tr [:lower:] [:upper:])
DECL=""
DEFN=""
LOADER=""
DECL_FMT='extern %s %s;'
DEFN_FMT='%s %s;'
LOADER_FMT='%s = (%s)eglGetProcAddress("%s");'
CHECK_FMT='if (!%s) {
wlr_log(L_ERROR, "Unable to load %s");
return false;
}'
while read -r COMMAND; do
[ ${COMMAND::1} = "-" ]
OPTIONAL=$?
COMMAND=${COMMAND#-}
if [ ${COMMAND: -2} = "WL" ]; then
FUNC_PTR_FMT='PFN%s'
else
FUNC_PTR_FMT='PFN%sPROC'
fi
FUNC_PTR=$(printf "$FUNC_PTR_FMT" "$COMMAND" | tr [:lower:] [:upper:])
DECL="$DECL$(printf "\n$DECL_FMT" "$FUNC_PTR" "$COMMAND")"
DEFN="$DEFN$(printf "\n$DEFN_FMT" "$FUNC_PTR" "$COMMAND")"
LOADER="$LOADER$(printf "\n$LOADER_FMT" "$COMMAND" "$FUNC_PTR" "$COMMAND")"
if [ $OPTIONAL -ne 0 ]; then
LOADER="$LOADER$(printf "\n$CHECK_FMT" "$COMMAND" "$COMMAND")"
fi
done < $SPEC
if [ ${OUT: -2} = '.h' ]; then
cat > $OUT << EOF
#ifndef $INCLUDE_GUARD
#define $INCLUDE_GUARD
#include <stdbool.h>
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <EGL/eglmesaext.h>
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
bool load_$BASE(void);
$DECL
#endif
EOF
elif [ ${OUT: -2} = '.c' ]; then
cat > $OUT << EOF
#include <wlr/util/log.h>
#include "$BASE.h"
$DEFN
bool load_$BASE(void) {
static bool done = false;
if (done) {
return true;
}
$LOADER
done = true;
return true;
}
EOF
else
exit 1
fi

View File

@ -10,15 +10,6 @@ struct wlr_egl {
EGLConfig config; EGLConfig config;
EGLContext context; EGLContext context;
PFNEGLGETPLATFORMDISPLAYEXTPROC get_platform_display;
PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC create_platform_window_surface;
PFNEGLCREATEIMAGEKHRPROC eglCreateImageKHR;
PFNEGLDESTROYIMAGEKHRPROC eglDestroyImageKHR;
PFNEGLQUERYWAYLANDBUFFERWL eglQueryWaylandBufferWL;
PFNEGLBINDWAYLANDDISPLAYWL eglBindWaylandDisplayWL;
PFNEGLUNBINDWAYLANDDISPLAYWL eglUnbindWaylandDisplayWL;
const char *egl_exts; const char *egl_exts;
const char *gl_exts; const char *gl_exts;

View File

@ -14,6 +14,10 @@ add_project_arguments(
'-DWLR_SRC_DIR="@0@"'.format(meson.source_root()), '-DWLR_SRC_DIR="@0@"'.format(meson.source_root()),
language: 'c', language: 'c',
) )
add_project_arguments(
'-I@0@'.format(meson.build_root()),
language: 'c',
)
add_project_link_arguments( add_project_link_arguments(
'-Wl,-rpath,@0@'.format(meson.build_root()), '-Wl,-rpath,@0@'.format(meson.build_root()),
language: 'c', language: 'c',
@ -69,8 +73,8 @@ if elogind.found() and get_option('enable_elogind')
endif endif
subdir('protocol') subdir('protocol')
subdir('backend')
subdir('render') subdir('render')
subdir('backend')
subdir('types') subdir('types')
subdir('util') subdir('util')
subdir('xcursor') subdir('xcursor')

View File

@ -4,6 +4,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <wlr/util/log.h> #include <wlr/util/log.h>
#include <wlr/egl.h> #include <wlr/egl.h>
#include "render/glapi.h"
// Extension documentation // Extension documentation
// https://www.khronos.org/registry/EGL/extensions/KHR/EGL_KHR_image_base.txt. // https://www.khronos.org/registry/EGL/extensions/KHR/EGL_KHR_image_base.txt.
@ -46,27 +47,6 @@ const char *egl_error(void) {
} }
} }
static bool egl_exts(struct wlr_egl *egl) {
egl->get_platform_display = (PFNEGLGETPLATFORMDISPLAYEXTPROC)
eglGetProcAddress("eglGetPlatformDisplayEXT");
if (!egl->get_platform_display) {
wlr_log(L_ERROR, "Failed to load EGL extension 'eglGetPlatformDisplayEXT'");
return false;
}
egl->create_platform_window_surface = (PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC)
eglGetProcAddress("eglCreatePlatformWindowSurfaceEXT");
if (!egl->create_platform_window_surface) {
wlr_log(L_ERROR,
"Failed to load EGL extension 'eglCreatePlatformWindowSurfaceEXT'");
return false;
}
return true;
}
static bool egl_get_config(EGLDisplay disp, EGLConfig *out, EGLint visual_id) { static bool egl_get_config(EGLDisplay disp, EGLConfig *out, EGLint visual_id) {
EGLint count = 0, matched = 0, ret; EGLint count = 0, matched = 0, ret;
@ -103,7 +83,7 @@ static bool egl_get_config(EGLDisplay disp, EGLConfig *out, EGLint visual_id) {
bool wlr_egl_init(struct wlr_egl *egl, EGLenum platform, EGLint visual_id, bool wlr_egl_init(struct wlr_egl *egl, EGLenum platform, EGLint visual_id,
void *remote_display) { void *remote_display) {
if (!egl_exts(egl)) { if (!load_glapi()) {
return false; return false;
} }
@ -112,7 +92,7 @@ bool wlr_egl_init(struct wlr_egl *egl, EGLenum platform, EGLint visual_id,
goto error; goto error;
} }
egl->display = egl->get_platform_display(platform, remote_display, NULL); egl->display = eglGetPlatformDisplayEXT(platform, remote_display, NULL);
if (egl->display == EGL_NO_DISPLAY) { if (egl->display == EGL_NO_DISPLAY) {
wlr_log(L_ERROR, "Failed to create EGL display: %s", egl_error()); wlr_log(L_ERROR, "Failed to create EGL display: %s", egl_error());
goto error; goto error;
@ -147,17 +127,6 @@ bool wlr_egl_init(struct wlr_egl *egl, EGLenum platform, EGLint visual_id,
goto error; goto error;
} }
egl->eglCreateImageKHR = (PFNEGLCREATEIMAGEKHRPROC)
eglGetProcAddress("eglCreateImageKHR");
egl->eglDestroyImageKHR = (PFNEGLDESTROYIMAGEKHRPROC)
eglGetProcAddress("eglDestroyImageKHR");
egl->eglQueryWaylandBufferWL = (PFNEGLQUERYWAYLANDBUFFERWL)
(void*) eglGetProcAddress("eglQueryWaylandBufferWL");
egl->eglBindWaylandDisplayWL = (PFNEGLBINDWAYLANDDISPLAYWL)
(void*) eglGetProcAddress("eglBindWaylandDisplayWL");
egl->eglUnbindWaylandDisplayWL = (PFNEGLUNBINDWAYLANDDISPLAYWL)
(void*) eglGetProcAddress("eglUnbindWaylandDisplayWL");
egl->gl_exts = (const char*) glGetString(GL_EXTENSIONS); egl->gl_exts = (const char*) glGetString(GL_EXTENSIONS);
wlr_log(L_INFO, "Using EGL %d.%d", (int)major, (int)minor); wlr_log(L_INFO, "Using EGL %d.%d", (int)major, (int)minor);
wlr_log(L_INFO, "Supported EGL extensions: %s", egl->egl_exts); wlr_log(L_INFO, "Supported EGL extensions: %s", egl->egl_exts);
@ -174,8 +143,8 @@ error:
void wlr_egl_free(struct wlr_egl *egl) { void wlr_egl_free(struct wlr_egl *egl) {
eglMakeCurrent(EGL_NO_DISPLAY, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); eglMakeCurrent(EGL_NO_DISPLAY, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
if (egl->wl_display && egl->eglUnbindWaylandDisplayWL) { if (egl->wl_display && eglUnbindWaylandDisplayWL) {
egl->eglUnbindWaylandDisplayWL(egl->display, egl->wl_display); eglUnbindWaylandDisplayWL(egl->display, egl->wl_display);
} }
eglDestroyContext(egl->display, egl->context); eglDestroyContext(egl->display, egl->context);
@ -184,11 +153,11 @@ void wlr_egl_free(struct wlr_egl *egl) {
} }
bool wlr_egl_bind_display(struct wlr_egl *egl, struct wl_display *local_display) { bool wlr_egl_bind_display(struct wlr_egl *egl, struct wl_display *local_display) {
if (!egl->eglBindWaylandDisplayWL) { if (!eglBindWaylandDisplayWL) {
return false; return false;
} }
if (egl->eglBindWaylandDisplayWL(egl->display, local_display)) { if (eglBindWaylandDisplayWL(egl->display, local_display)) {
egl->wl_display = local_display; egl->wl_display = local_display;
return true; return true;
} }
@ -198,33 +167,33 @@ bool wlr_egl_bind_display(struct wlr_egl *egl, struct wl_display *local_display)
bool wlr_egl_query_buffer(struct wlr_egl *egl, struct wl_resource *buf, bool wlr_egl_query_buffer(struct wlr_egl *egl, struct wl_resource *buf,
int attrib, int *value) { int attrib, int *value) {
if (!egl->eglQueryWaylandBufferWL) { if (!eglQueryWaylandBufferWL) {
return false; return false;
} }
return egl->eglQueryWaylandBufferWL(egl->display, buf, attrib, value); return eglQueryWaylandBufferWL(egl->display, buf, attrib, value);
} }
EGLImage wlr_egl_create_image(struct wlr_egl *egl, EGLenum target, EGLImage wlr_egl_create_image(struct wlr_egl *egl, EGLenum target,
EGLClientBuffer buffer, const EGLint *attribs) { EGLClientBuffer buffer, const EGLint *attribs) {
if (!egl->eglCreateImageKHR) { if (!eglCreateImageKHR) {
return false; return false;
} }
return egl->eglCreateImageKHR(egl->display, egl->context, target, return eglCreateImageKHR(egl->display, egl->context, target,
buffer, attribs); buffer, attribs);
} }
bool wlr_egl_destroy_image(struct wlr_egl *egl, EGLImage image) { bool wlr_egl_destroy_image(struct wlr_egl *egl, EGLImage image) {
if (!egl->eglDestroyImageKHR) { if (!eglDestroyImageKHR) {
return false; return false;
} }
egl->eglDestroyImageKHR(egl->display, image); eglDestroyImageKHR(egl->display, image);
return true; return true;
} }
EGLSurface wlr_egl_create_surface(struct wlr_egl *egl, void *window) { EGLSurface wlr_egl_create_surface(struct wlr_egl *egl, void *window) {
EGLSurface surf = egl->create_platform_window_surface(egl->display, egl->config, EGLSurface surf = eglCreatePlatformWindowSurfaceEXT(egl->display, egl->config,
window, NULL); window, NULL);
if (surf == EGL_NO_SURFACE) { if (surf == EGL_NO_SURFACE) {
wlr_log(L_ERROR, "Failed to create EGL surface: %s", egl_error()); wlr_log(L_ERROR, "Failed to create EGL surface: %s", egl_error());

8
render/glapi.txt Normal file
View File

@ -0,0 +1,8 @@
eglGetPlatformDisplayEXT
eglCreatePlatformWindowSurfaceEXT
-eglCreateImageKHR
-eglDestroyImageKHR
-eglQueryWaylandBufferWL
-eglBindWaylandDisplayWL
-eglUnbindWaylandDisplayWL
-glEGLImageTargetTexture2DOES

View File

@ -12,8 +12,8 @@
#include <wlr/render/matrix.h> #include <wlr/render/matrix.h>
#include <wlr/util/log.h> #include <wlr/util/log.h>
#include "render/gles2.h" #include "render/gles2.h"
#include "render/glapi.h"
PFNGLEGLIMAGETARGETTEXTURE2DOESPROC glEGLImageTargetTexture2DOES = NULL;
struct shaders shaders; struct shaders shaders;
static bool compile_shader(GLuint type, const GLchar *src, GLuint *shader) { static bool compile_shader(GLuint type, const GLchar *src, GLuint *shader) {
@ -101,25 +101,7 @@ error:
wlr_log(L_ERROR, "Failed to set up default shaders!"); wlr_log(L_ERROR, "Failed to set up default shaders!");
} }
static void init_image_ext() {
if (glEGLImageTargetTexture2DOES) {
return;
}
const char *exts = (const char*) glGetString(GL_EXTENSIONS);
if (strstr(exts, "GL_OES_EGL_image_external")) {
glEGLImageTargetTexture2DOES = (PFNGLEGLIMAGETARGETTEXTURE2DOESPROC)
eglGetProcAddress("glEGLImageTargetTexture2DOES");
}
if (!glEGLImageTargetTexture2DOES) {
wlr_log(L_INFO, "Failed to load glEGLImageTargetTexture2DOES "
"Will not be able to attach drm buffers");
}
}
static void init_globals() { static void init_globals() {
init_image_ext();
init_default_shaders(); init_default_shaders();
} }

View File

@ -1,3 +1,16 @@
glgen = find_program('../glgen.sh')
glapi_c = custom_target('glapi.c',
input: 'glapi.txt',
output: '@BASENAME@.c',
command: [glgen, '@INPUT@', '@OUTPUT@'],
)
glapi_h = custom_target('glapi.h',
input: 'glapi.txt',
output: '@BASENAME@.h',
command: [glgen, '@INPUT@', '@OUTPUT@'],
)
lib_wlr_render = static_library( lib_wlr_render = static_library(
'wlr_render', 'wlr_render',
files( files(
@ -11,6 +24,13 @@ lib_wlr_render = static_library(
'wlr_renderer.c', 'wlr_renderer.c',
'wlr_texture.c', 'wlr_texture.c',
), ),
glapi_c,
glapi_h,
include_directories: wlr_inc, include_directories: wlr_inc,
dependencies: [glesv2, egl], dependencies: [glesv2, egl],
) )
wlr_render = declare_dependency(
link_with: lib_wlr_render,
sources: glapi_h,
)