SDL2 driver: Use threaded model approach for easier debugging.
This still retains the previous fork() based design if GFX_SDL_USE_FORK is enabled.
This commit is contained in:
parent
47d9826a0a
commit
a3bd2d25ce
2 changed files with 53 additions and 27 deletions
|
@ -19,6 +19,9 @@
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <semaphore.h>
|
#include <semaphore.h>
|
||||||
#include <SDL.h>
|
#include <SDL.h>
|
||||||
|
#if GFX_SDL_USE_FORK
|
||||||
|
#include <assert.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
#define GDISP_DRIVER_VMT GDISPVMT_SDL
|
#define GDISP_DRIVER_VMT GDISPVMT_SDL
|
||||||
#include "gdisp_lld_config.h"
|
#include "gdisp_lld_config.h"
|
||||||
|
@ -177,7 +180,6 @@ static sem_t *input_event;
|
||||||
#define CTX_MUTEX_NAME "ugfx_ctx_mutex"
|
#define CTX_MUTEX_NAME "ugfx_ctx_mutex"
|
||||||
#define INPUT_EVENT_NAME "ugfx_input_event"
|
#define INPUT_EVENT_NAME "ugfx_input_event"
|
||||||
|
|
||||||
|
|
||||||
static int SDL_loop (void) {
|
static int SDL_loop (void) {
|
||||||
SDL_Window *window = SDL_CreateWindow("uGFX", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, GDISP_SCREEN_WIDTH, GDISP_SCREEN_HEIGHT, 0);
|
SDL_Window *window = SDL_CreateWindow("uGFX", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, GDISP_SCREEN_WIDTH, GDISP_SCREEN_HEIGHT, 0);
|
||||||
SDL_Renderer *render = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
|
SDL_Renderer *render = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
|
||||||
|
@ -291,6 +293,40 @@ static int SDL_loop (void) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void* SDL_start(void* arg)
|
||||||
|
{
|
||||||
|
// Get the PID (if supposed to)
|
||||||
|
#if GFX_SDL_USE_FORK
|
||||||
|
int status;
|
||||||
|
pid_t gui_pid = (pid_t)arg;
|
||||||
|
assert(gui_pid != 0);
|
||||||
|
#else
|
||||||
|
(void)arg;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Main proccess. It's for host UI and SDL
|
||||||
|
memset (context,0,sizeof (*context));
|
||||||
|
context->need_redraw = 1;
|
||||||
|
context->maxx = GDISP_SCREEN_WIDTH-1;
|
||||||
|
context->maxy = GDISP_SCREEN_HEIGHT-1;
|
||||||
|
context->minx = 0;
|
||||||
|
context->miny = 0;
|
||||||
|
SDL_loop();
|
||||||
|
|
||||||
|
// cleanup
|
||||||
|
#if GFX_SDL_USE_FORK
|
||||||
|
kill(gui_pid, SIGKILL);
|
||||||
|
waitpid(gui_pid, &status, 0);
|
||||||
|
#endif
|
||||||
|
SDL_Quit();
|
||||||
|
munmap(context,sizeof (*context));
|
||||||
|
sem_close(ctx_mutex);
|
||||||
|
sem_unlink(CTX_MUTEX_NAME);
|
||||||
|
sem_close(input_event);
|
||||||
|
sem_unlink(INPUT_EVENT_NAME);
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
static void *SDL_input_event_loop (void *arg) {
|
static void *SDL_input_event_loop (void *arg) {
|
||||||
(void)arg;
|
(void)arg;
|
||||||
for (;;) {
|
for (;;) {
|
||||||
|
@ -334,35 +370,22 @@ void sdl_driver_init (void) {
|
||||||
perror("Failed init semaphore");
|
perror("Failed init semaphore");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
pid_t gui_pid = fork ();
|
|
||||||
|
|
||||||
if (gui_pid) {
|
#if GFX_SDL_USE_FORK
|
||||||
// Main proccess. It's for host UI and SDL
|
pid_t gui_pid = fork();
|
||||||
int status;
|
if (gui_pid)
|
||||||
memset (context,0,sizeof (*context));
|
SDL_start((void*)gui_pid);
|
||||||
context->need_redraw = 1;
|
#else
|
||||||
context->maxx = GDISP_SCREEN_WIDTH-1;
|
pthread_t thread_sdl;
|
||||||
context->maxy = GDISP_SCREEN_HEIGHT-1;
|
pthread_create(&thread_sdl, NULL, SDL_start, NULL);
|
||||||
context->minx = 0;
|
pthread_detach(thread_sdl);
|
||||||
context->miny = 0;
|
#endif
|
||||||
SDL_loop ();
|
|
||||||
// cleanup
|
|
||||||
kill(gui_pid,SIGKILL);
|
|
||||||
waitpid(gui_pid, &status, 0);
|
|
||||||
SDL_Quit ();
|
|
||||||
munmap (context,sizeof (*context));
|
|
||||||
sem_close (ctx_mutex);
|
|
||||||
sem_unlink (CTX_MUTEX_NAME);
|
|
||||||
sem_close (input_event);
|
|
||||||
sem_unlink (INPUT_EVENT_NAME);
|
|
||||||
exit (0);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create thread for input events processing
|
// Create thread for input events processing
|
||||||
pthread_t thread;
|
pthread_t thread;
|
||||||
pthread_create(&thread, NULL, SDL_input_event_loop, NULL);
|
pthread_create(&thread, NULL, SDL_input_event_loop, NULL);
|
||||||
pthread_detach (thread);
|
pthread_detach (thread);
|
||||||
// Continue execution of ugfx UI in forked process
|
// Continue execution of ugfx UI in forked process (or corresponding thread_sdl)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -12,6 +12,9 @@
|
||||||
/* Driver hardware support. */
|
/* Driver hardware support. */
|
||||||
/*===========================================================================*/
|
/*===========================================================================*/
|
||||||
|
|
||||||
|
// Whether to use separate processes or multi-threading approach for SDL management.
|
||||||
|
#define GFX_SDL_USE_FORK GFXOFF
|
||||||
|
|
||||||
#define GDISP_HARDWARE_DRAWPIXEL GFXON
|
#define GDISP_HARDWARE_DRAWPIXEL GFXON
|
||||||
#define GDISP_HARDWARE_FILLS GFXON
|
#define GDISP_HARDWARE_FILLS GFXON
|
||||||
#define GDISP_HARDWARE_BITFILLS GFXOFF
|
#define GDISP_HARDWARE_BITFILLS GFXOFF
|
||||||
|
|
Loading…
Add table
Reference in a new issue