diff --git a/gfx/drivers_context/android_ctx.c b/gfx/drivers_context/android_ctx.c index 1f797c8cc1..c73db9aa59 100644 --- a/gfx/drivers_context/android_ctx.c +++ b/gfx/drivers_context/android_ctx.c @@ -625,4 +625,5 @@ const gfx_ctx_driver_t gfx_ctx_android = { #else NULL #endif + ,NULL }; diff --git a/gfx/drivers_context/khr_display_ctx.c b/gfx/drivers_context/khr_display_ctx.c index b5b1dcbe16..a3fa2b4f58 100644 --- a/gfx/drivers_context/khr_display_ctx.c +++ b/gfx/drivers_context/khr_display_ctx.c @@ -258,5 +258,6 @@ const gfx_ctx_driver_t gfx_ctx_khr_display = { gfx_ctx_khr_display_set_flags, NULL, gfx_ctx_khr_display_get_context_data, + NULL }; diff --git a/gfx/drivers_context/wayland_ctx.c b/gfx/drivers_context/wayland_ctx.c index 4d5b872e3a..df8373e1a3 100644 --- a/gfx/drivers_context/wayland_ctx.c +++ b/gfx/drivers_context/wayland_ctx.c @@ -1177,4 +1177,5 @@ const gfx_ctx_driver_t gfx_ctx_wayland = { #else NULL #endif + ,NULL }; diff --git a/gfx/drivers_context/wgl_ctx.cpp b/gfx/drivers_context/wgl_ctx.cpp index 7eeef85e63..78847ef0b2 100644 --- a/gfx/drivers_context/wgl_ctx.cpp +++ b/gfx/drivers_context/wgl_ctx.cpp @@ -705,5 +705,6 @@ const gfx_ctx_driver_t gfx_ctx_wgl = { #else NULL, #endif + NULL }; diff --git a/gfx/drivers_context/x_ctx.c b/gfx/drivers_context/x_ctx.c index cdc1647411..28bb831099 100644 --- a/gfx/drivers_context/x_ctx.c +++ b/gfx/drivers_context/x_ctx.c @@ -69,6 +69,8 @@ static enum gfx_ctx_api x_api; static PFNGLXCREATECONTEXTATTRIBSARBPROC glx_create_context_attribs; #endif +static gfx_ctx_x_data_t *current_context_data = NULL; + static int x_nul_handler(Display *dpy, XErrorEvent *event) { (void)dpy; @@ -341,6 +343,8 @@ static void *gfx_ctx_x_init(void *data) if (!x) return NULL; + current_context_data = x; + XInitThreads(); if (!x11_connect()) @@ -913,6 +917,22 @@ static void gfx_ctx_x_set_flags(void *data, uint32_t flags) x->core_hw_context_enable = true; } +void gfx_ctx_x_make_current(bool release) +{ + if (!current_context_data) + return; + + if (release) + { + glXMakeContextCurrent(g_x11_dpy, None, None, NULL); + } + else + { + glXMakeContextCurrent(g_x11_dpy, + current_context_data->g_glx_win, current_context_data->g_glx_win, current_context_data->g_ctx); + } +} + const gfx_ctx_driver_t gfx_ctx_x = { gfx_ctx_x_init, gfx_ctx_x_destroy, @@ -947,5 +967,10 @@ const gfx_ctx_driver_t gfx_ctx_x = { #else NULL #endif +#ifdef HAVE_OPENGL + ,gfx_ctx_x_make_current +#else + ,NULL +#endif }; diff --git a/gfx/drivers_font/gl_raster_font.c b/gfx/drivers_font/gl_raster_font.c index ce787b7e78..a6c56f024a 100644 --- a/gfx/drivers_font/gl_raster_font.c +++ b/gfx/drivers_font/gl_raster_font.c @@ -17,6 +17,7 @@ #include "../common/gl_common.h" #include "../font_driver.h" #include "../video_shader_driver.h" +#include "../video_context_driver.h" #include /* TODO: Move viewport side effects to the caller: it's a source of bugs. */ @@ -142,6 +143,7 @@ static void *gl_raster_font_init_font(void *data, { const struct font_atlas *atlas = NULL; gl_raster_t *font = (gl_raster_t*)calloc(1, sizeof(*font)); + settings_t *settings = config_get_ptr(); if (!font) return NULL; @@ -156,6 +158,9 @@ static void *gl_raster_font_init_font(void *data, return NULL; } + if (settings->video.threaded) + video_context_driver_make_current(false); + glGenTextures(1, &font->tex); glBindTexture(GL_TEXTURE_2D, font->tex); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); @@ -185,12 +190,16 @@ error: static void gl_raster_font_free_font(void *data) { gl_raster_t *font = (gl_raster_t*)data; + settings_t *settings = config_get_ptr(); if (!font) return; if (font->font_driver && font->font_data) font->font_driver->free(font->font_data); + if (settings->video.threaded) + video_context_driver_make_current(false); + glDeleteTextures(1, &font->tex); free(font); } diff --git a/gfx/video_context_driver.c b/gfx/video_context_driver.c index 03a8304fae..fecce22b21 100644 --- a/gfx/video_context_driver.c +++ b/gfx/video_context_driver.c @@ -328,6 +328,14 @@ bool video_context_driver_bind_hw_render(bool *enable) return true; } +void video_context_driver_make_current(bool release) +{ + if (!current_video_context || !current_video_context->make_current) + return; + + current_video_context->make_current(release); +} + bool video_context_driver_set(const gfx_ctx_driver_t *data) { if (!data) diff --git a/gfx/video_context_driver.h b/gfx/video_context_driver.h index 002a1ad0f6..b05bd4332d 100644 --- a/gfx/video_context_driver.h +++ b/gfx/video_context_driver.h @@ -163,6 +163,10 @@ typedef struct gfx_ctx_driver * This is mostly relevant for graphics APIs such as Vulkan * which do not have global context state. */ void *(*get_context_data)(void *data); + + /* Optional. Makes driver context (only GLX right now) + * active for this thread. */ + void (*make_current)(bool release); } gfx_ctx_driver_t; typedef struct gfx_ctx_flags @@ -280,6 +284,8 @@ bool video_context_driver_get_video_output_next(void); bool video_context_driver_bind_hw_render(bool *enable); +void video_context_driver_make_current(bool restore); + bool video_context_driver_set(const gfx_ctx_driver_t *data); void video_context_driver_destroy(void);