Skip to content

Commit 22481db

Browse files
committed
sdl2: add shared context by implementing sdl_ctx_bind_hw_render
1 parent 11d4e46 commit 22481db

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

gfx/drivers_context/sdl_gl_ctx.c

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ typedef struct gfx_ctx_sdl_data
4242
#ifdef HAVE_SDL2
4343
SDL_Window *win;
4444
SDL_GLContext ctx;
45+
SDL_GLContext shared_ctx;
4546
#else
4647
SDL_Surface *win;
4748
#endif
@@ -67,10 +68,14 @@ static void sdl_ctx_destroy_resources(gfx_ctx_sdl_data_t *sdl)
6768
if (sdl->ctx)
6869
SDL_GL_DeleteContext(sdl->ctx);
6970

71+
if (sdl->shared_ctx)
72+
SDL_GL_DeleteContext(sdl->shared_ctx);
73+
7074
if (sdl->win)
7175
SDL_DestroyWindow(sdl->win);
7276

7377
sdl->ctx = NULL;
78+
sdl->shared_ctx = NULL;
7479
#else
7580
if (sdl->win)
7681
SDL_FreeSurface(sdl->win);
@@ -423,6 +428,35 @@ static uint32_t sdl_ctx_get_flags(void *data)
423428
static bool sdl_ctx_suppress_screensaver(void *data, bool enable) { return false; }
424429
static void sdl_ctx_set_flags(void *data, uint32_t flags) { }
425430

431+
static void sdl_ctx_bind_hw_render(void *data, bool enable)
432+
{
433+
#ifdef HAVE_SDL2
434+
gfx_ctx_sdl_data_t *sdl = (gfx_ctx_sdl_data_t*)data;
435+
if (!sdl || !sdl->win || !sdl->ctx)
436+
return;
437+
438+
if (enable)
439+
{
440+
if (!sdl->shared_ctx)
441+
{
442+
SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1);
443+
SDL_GL_MakeCurrent(sdl->win, sdl->ctx);
444+
sdl->shared_ctx = SDL_GL_CreateContext(sdl->win);
445+
if (!sdl->shared_ctx)
446+
{
447+
RARCH_ERR("[SDL_GL]: Failed to create shared GL context: %s\n", SDL_GetError());
448+
return;
449+
}
450+
}
451+
SDL_GL_MakeCurrent(sdl->win, sdl->shared_ctx);
452+
}
453+
else
454+
{
455+
SDL_GL_MakeCurrent(sdl->win, sdl->ctx);
456+
}
457+
#endif
458+
}
459+
426460
const gfx_ctx_driver_t gfx_ctx_sdl_gl =
427461
{
428462
sdl_ctx_init,
@@ -453,7 +487,7 @@ const gfx_ctx_driver_t gfx_ctx_sdl_gl =
453487
"gl_sdl",
454488
sdl_ctx_get_flags,
455489
sdl_ctx_set_flags,
456-
NULL, /* bind_hw_render */
490+
sdl_ctx_bind_hw_render,
457491
NULL,
458492
NULL
459493
};

0 commit comments

Comments
 (0)