dav1dplay: Ensure that SDL is shut down when the application quits

This commit is contained in:
Cameron Cawley
2024-09-04 11:24:47 +00:00
committed by Ronald S. Bultje
co-authored by Ronald S. Bultje
parent cc6eb3d53d
commit 4e1a8b4510
3 changed files with 33 additions and 22 deletions
+25 -20
View File
@@ -240,35 +240,37 @@ static Dav1dPlayRenderContext *dp_rd_ctx_create(int argc, char **argv)
return NULL;
}
// Parse and validate arguments
dav1d_default_settings(&rd_ctx->lib_settings);
memset(&rd_ctx->settings, 0, sizeof(rd_ctx->settings));
dp_rd_ctx_parse_args(rd_ctx, argc, argv);
// Init SDL2 library
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0) {
fprintf(stderr, "SDL_Init failed: %s\n", SDL_GetError());
goto fail;
}
// Register a custom event to notify our SDL main thread
// about new frames
rd_ctx->event_types = SDL_RegisterEvents(3);
if (rd_ctx->event_types == UINT32_MAX) {
fprintf(stderr, "Failure to create custom SDL event types!\n");
free(rd_ctx);
return NULL;
goto fail;
}
rd_ctx->fifo = dp_fifo_create(5);
if (rd_ctx->fifo == NULL) {
fprintf(stderr, "Failed to create FIFO for output pictures!\n");
free(rd_ctx);
return NULL;
goto fail;
}
rd_ctx->lock = SDL_CreateMutex();
if (rd_ctx->lock == NULL) {
fprintf(stderr, "SDL_CreateMutex failed: %s\n", SDL_GetError());
dp_fifo_destroy(rd_ctx->fifo);
free(rd_ctx);
return NULL;
goto fail;
}
// Parse and validate arguments
dav1d_default_settings(&rd_ctx->lib_settings);
memset(&rd_ctx->settings, 0, sizeof(rd_ctx->settings));
dp_rd_ctx_parse_args(rd_ctx, argc, argv);
// Select renderer
renderer_info = dp_get_renderer(rd_ctx->settings.renderer_name);
@@ -281,13 +283,19 @@ static Dav1dPlayRenderContext *dp_rd_ctx_create(int argc, char **argv)
rd_ctx->rd_priv = (renderer_info) ? renderer_info->create_renderer() : NULL;
if (rd_ctx->rd_priv == NULL) {
SDL_DestroyMutex(rd_ctx->lock);
dp_fifo_destroy(rd_ctx->fifo);
free(rd_ctx);
return NULL;
goto fail;
}
return rd_ctx;
fail:
if (rd_ctx->lock)
SDL_DestroyMutex(rd_ctx->lock);
if (rd_ctx->fifo)
dp_fifo_destroy(rd_ctx->fifo);
free(rd_ctx);
SDL_Quit();
return NULL;
}
/**
@@ -662,10 +670,6 @@ int main(int argc, char **argv)
return 1;
}
// Init SDL2 library
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0)
return 10;
// Create render context
Dav1dPlayRenderContext *rd_ctx = dp_rd_ctx_create(argc, argv);
if (rd_ctx == NULL) {
@@ -776,5 +780,6 @@ out:;
int decoder_ret = 0;
SDL_WaitThread(decoder_thread, &decoder_ret);
dp_rd_ctx_destroy(rd_ctx);
SDL_Quit();
return decoder_ret;
}
+6 -2
View File
@@ -64,6 +64,8 @@ typedef struct renderer_priv_ctx
#ifdef HAVE_PLACEBO_OPENGL
// Placebo OpenGL handle
pl_opengl gl;
// SDL OpenGL context
SDL_GLContext gl_context;
#endif
// Placebo GPU
pl_gpu gpu;
@@ -132,8 +134,8 @@ static void *placebo_renderer_create_gl(void)
return NULL;
sdlwin = rd_priv_ctx->win;
SDL_GLContext glcontext = SDL_GL_CreateContext(sdlwin);
SDL_GL_MakeCurrent(sdlwin, glcontext);
rd_priv_ctx->gl_context = SDL_GL_CreateContext(sdlwin);
SDL_GL_MakeCurrent(sdlwin, rd_priv_ctx->gl_context);
rd_priv_ctx->gl = pl_opengl_create(rd_priv_ctx->log, pl_opengl_params(
#ifndef NDEBUG
@@ -280,6 +282,8 @@ static void placebo_renderer_destroy(void *cookie)
#ifdef HAVE_PLACEBO_OPENGL
if (rd_priv_ctx->gl)
pl_opengl_destroy(&(rd_priv_ctx->gl));
if (rd_priv_ctx->gl_context)
SDL_GL_DeleteContext(rd_priv_ctx->gl_context);
#endif
SDL_DestroyWindow(rd_priv_ctx->win);
+2
View File
@@ -79,7 +79,9 @@ static void sdl_renderer_destroy(void *cookie)
Dav1dPlayRendererPrivateContext *rd_priv_ctx = cookie;
assert(rd_priv_ctx != NULL);
SDL_DestroyTexture(rd_priv_ctx->tex);
SDL_DestroyRenderer(rd_priv_ctx->renderer);
SDL_DestroyWindow(rd_priv_ctx->win);
SDL_DestroyMutex(rd_priv_ctx->lock);
free(rd_priv_ctx);
}