Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add OpenGL Shim #1503

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bin/mangohud.in
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fi
DISABLE_LD_PRELOAD="cs2.sh
"

MANGOHUD_LIB_NAME="@ld_libdir_mangohud@libMangoHud_opengl.so"
MANGOHUD_LIB_NAME="@ld_libdir_mangohud@libMangoHud_shim.so"

if [ "$1" = "--dlsym" ]; then
MANGOHUD_LIB_NAME="@ld_libdir_mangohud@libMangoHud_dlsym.so:${MANGOHUD_LIB_NAME}"
Expand Down
2 changes: 2 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,12 @@ install() {
/usr/bin/install -Dvm644 ./build/release/usr/lib/mangohud/lib64/libMangoHud.so /usr/lib/mangohud/lib64/libMangoHud.so
/usr/bin/install -Dvm644 ./build/release/usr/lib/mangohud/lib64/libMangoHud_dlsym.so /usr/lib/mangohud/lib64/libMangoHud_dlsym.so
/usr/bin/install -Dvm644 ./build/release/usr/lib/mangohud/lib64/libMangoHud_opengl.so /usr/lib/mangohud/lib64/libMangoHud_opengl.so
/usr/bin/install -Dvm644 ./build/release/usr/lib/mangohud/lib64/libMangoHud_shim.so /usr/lib/mangohud/lib64/libMangoHud_shim.so
if [ "$MACHINE" = "x86_64" ]; then
/usr/bin/install -Dvm644 ./build/release/usr/lib/mangohud/lib32/libMangoHud.so /usr/lib/mangohud/lib32/libMangoHud.so
/usr/bin/install -Dvm644 ./build/release/usr/lib/mangohud/lib32/libMangoHud_dlsym.so /usr/lib/mangohud/lib32/libMangoHud_dlsym.so
/usr/bin/install -Dvm644 ./build/release/usr/lib/mangohud/lib32/libMangoHud_opengl.so /usr/lib/mangohud/lib32/libMangoHud_opengl.so
/usr/bin/install -Dvm644 ./build/release/usr/lib/mangohud/lib32/libMangoHud_shim.so /usr/lib/mangohud/lib32/libMangoHud_shim.so
fi

/usr/bin/install -Dvm644 ./build/release/usr/share/vulkan/implicit_layer.d/MangoHud.x86_64.json /usr/share/vulkan/implicit_layer.d/MangoHud.x86_64.json
Expand Down
2 changes: 2 additions & 0 deletions src/gl/gl.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ int glXQueryDrawable(void *dpy, void* glxdraw, int attr, unsigned int * value);
int64_t glXSwapBuffersMscOML(void* dpy, void* drawable, int64_t target_msc, int64_t divisor, int64_t remainder);

unsigned int eglSwapBuffers( void*, void* );
void* eglGetPlatformDisplay( unsigned int, void*, const intptr_t*);
void* eglGetDisplay( void* );

#ifdef __cplusplus
}
Expand Down
128 changes: 128 additions & 0 deletions src/gl/shim.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
#include <dlfcn.h>
#include <stdio.h>
#include <stdint.h>
#include "gl.h"
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>

static void* handle = NULL;
static bool mangoHudLoaded = false;

// Load MangoHud after EGL/GLX functions have been intercepted
static void loadMangoHud(void);
static void loadMangoHud() {
if (mangoHudLoaded) return;

// allow user to load custom mangohud libs (useful for testing)
char *libs = getenv("MANGOHUD_OPENGL_LIBS");
char *lib = NULL;

if (libs)
{
lib = strtok(libs, ":");

// when user specifies only one path
if (!lib) lib = libs;

while (lib != NULL)
{
handle = dlopen(lib, RTLD_LAZY | RTLD_LOCAL | RTLD_DEEPBIND);

if (handle)
{
mangoHudLoaded = true;
break;
}
else fprintf(stderr, "shim: Failed to load from: %s\n", lib);

lib = strtok(NULL, ":");
}
}

if (!mangoHudLoaded)
{
handle = dlopen("${ORIGIN}/libMangoHud_opengl.so", RTLD_LAZY | RTLD_LOCAL | RTLD_DEEPBIND);
if (handle) mangoHudLoaded = true;
}
}

void glXSwapBuffers(void* dpy, void* drawable) {
loadMangoHud();

void (*pglXSwapBuffers)(void*, void*) = dlsym(handle, "glXSwapBuffers");
if (pglXSwapBuffers) {
pglXSwapBuffers(dpy, drawable);
} else {
pglXSwapBuffers = dlsym(RTLD_NEXT, "glXSwapBuffers");
if (pglXSwapBuffers)
pglXSwapBuffers(dpy, drawable);
}
}

void* eglGetDisplay(void *native_dpy) {
loadMangoHud();

void* (*peglGetDisplay)(void*) = dlsym(handle, "eglGetDisplay");
if (peglGetDisplay) {
return peglGetDisplay(native_dpy);
} else {
peglGetDisplay = dlsym(RTLD_NEXT, "eglGetDisplay");
if (peglGetDisplay)
return peglGetDisplay(native_dpy);
}

return NULL;
}

void* eglGetPlatformDisplay(unsigned int platform, void* native_display, const intptr_t* attrib_list)
{
loadMangoHud();

void* (*peglGetPlatformDisplay)(unsigned int, void*, const intptr_t*) = dlsym(handle, "eglGetPlatformDisplay");
if (peglGetPlatformDisplay) {
return peglGetPlatformDisplay(platform, native_display, attrib_list);
} else {
peglGetPlatformDisplay = dlsym(RTLD_NEXT, "eglGetPlatformDisplay");
if (peglGetPlatformDisplay)
return peglGetPlatformDisplay(platform, native_display, attrib_list);
}

return NULL;
}

unsigned int eglSwapBuffers(void* dpy, void* surf) {
loadMangoHud();

// Get the hooked eglSwapBuffers function from the loaded library if available
unsigned int (*peglSwapBuffers)(void*, void*) = dlsym(handle, "eglSwapBuffers");
if (peglSwapBuffers) {
return peglSwapBuffers(dpy, surf);
} else {
// Fall back to the original eglSwapBuffers function
peglSwapBuffers = dlsym(RTLD_NEXT, "eglSwapBuffers");
if (peglSwapBuffers) {
return peglSwapBuffers(dpy, surf);
}
}

return 0;
}

int64_t glXSwapBuffersMscOML(void* dpy, void* drawable, int64_t target_msc, int64_t divisor, int64_t remainder) {
loadMangoHud();

// Get the hooked glXSwapBuffersMscOML function from the loaded library if available
int64_t (*pglXSwapBuffersMscOML)(void*, void*, int64_t, int64_t, int64_t) = dlsym(handle, "glXSwapBuffersMscOML");
if (pglXSwapBuffersMscOML) {
return pglXSwapBuffersMscOML(dpy, drawable, target_msc, divisor, remainder);
} else {
// Fall back to the original glXSwapBuffersMscOML function
pglXSwapBuffersMscOML = dlsym(RTLD_NEXT, "glXSwapBuffersMscOML");
if (pglXSwapBuffersMscOML) {
return pglXSwapBuffersMscOML(dpy, drawable, target_msc, divisor, remainder);
}
}

return -1;
}
13 changes: 13 additions & 0 deletions src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,19 @@ mangoapp = executable(
install : true
)
endif
if is_unixy
mangohud_shim = shared_library(
'MangoHud_shim',
files(
'gl/shim.c',
),
dependencies : [
dep_dl
],
install_dir : libdir_mangohud,
install: true
)
endif

configure_file(input : 'mangohud.json.in',
output : '@0@.@[email protected]'.format(meson.project_name(), host_machine.cpu_family()),
Expand Down
Loading