From 696dae6e70c8e7a51bd38b952a79a46e7ee3bf81 Mon Sep 17 00:00:00 2001 From: libretroadmin Date: Wed, 31 May 2023 01:09:38 +0200 Subject: [PATCH] Refactor away some video driver functions --- gfx/common/vulkan_common.c | 5 ++-- gfx/common/win32_common.c | 3 ++- gfx/drivers/d3d10.c | 3 ++- gfx/drivers/d3d11.c | 5 ++-- gfx/drivers/vulkan.c | 6 ++--- gfx/drivers_context/wgl_ctx.c | 3 ++- gfx/drivers_context/x_ctx.c | 4 ++- gfx/video_driver.c | 47 +++++------------------------------ gfx/video_driver.h | 13 ---------- menu/drivers/rgui.c | 19 +++++++++++--- menu/menu_driver.c | 4 ++- tasks/task_screenshot.c | 33 ++++++++++++------------ tasks/task_translation.c | 10 ++++++-- 13 files changed, 66 insertions(+), 89 deletions(-) diff --git a/gfx/common/vulkan_common.c b/gfx/common/vulkan_common.c index 37bf3dbf05..bce6c8059f 100644 --- a/gfx/common/vulkan_common.c +++ b/gfx/common/vulkan_common.c @@ -2599,7 +2599,8 @@ void vulkan_present(gfx_ctx_vulkan_data_t *vk, unsigned index) void vulkan_context_destroy(gfx_ctx_vulkan_data_t *vk, bool destroy_surface) { - uint32_t video_st_flags = 0; + video_driver_state_t *video_st = video_state_get_ptr(); + uint32_t video_st_flags = 0; if (!vk->context.instance) return; @@ -2620,7 +2621,7 @@ void vulkan_context_destroy(gfx_ctx_vulkan_data_t *vk, vkDestroyDebugUtilsMessengerEXT(vk->context.instance, vk->context.debug_callback, NULL); #endif - video_st_flags = video_driver_get_st_flags(); + video_st_flags = video_st->flags; if (video_st_flags & VIDEO_FLAG_CACHE_CONTEXT) { diff --git a/gfx/common/win32_common.c b/gfx/common/win32_common.c index 751970321d..8aaa4a587f 100644 --- a/gfx/common/win32_common.c +++ b/gfx/common/win32_common.c @@ -860,7 +860,8 @@ static void win32_save_position(void) if (window_save_positions) { - uint32_t video_st_flags = video_driver_get_st_flags(); + video_driver_state_t *video_st = video_state_get_ptr(); + uint32_t video_st_flags = video_st->flags; bool video_fullscreen = settings->bools.video_fullscreen; if ( !video_fullscreen diff --git a/gfx/drivers/d3d10.c b/gfx/drivers/d3d10.c index 49fe7fe758..3ab7d51a0e 100644 --- a/gfx/drivers/d3d10.c +++ b/gfx/drivers/d3d10.c @@ -563,6 +563,7 @@ static void d3d10_gfx_free(void* data) { int i; #if 0 + video_driver_state_t *video_st = video_state_get_ptr(); uint32_t video_st_flags; #endif d3d10_video_t* d3d10 = (d3d10_video_t*)data; @@ -611,7 +612,7 @@ static void d3d10_gfx_free(void* data) font_driver_free_osd(); #if 0 - video_st_flags = video_driver_get_st_flags(); + video_st_flags = video_st->flags; if (video_st_flags & VIDEO_FLAG_CACHE_CONTEXT) { cached_device_d3d10 = d3d10->device; diff --git a/gfx/drivers/d3d11.c b/gfx/drivers/d3d11.c index 69249b3f54..e7942edb27 100644 --- a/gfx/drivers/d3d11.c +++ b/gfx/drivers/d3d11.c @@ -762,7 +762,8 @@ static void d3d11_gfx_free(void* data) { int i; uint32_t video_st_flags; - d3d11_video_t* d3d11 = (d3d11_video_t*)data; + d3d11_video_t* d3d11 = (d3d11_video_t*)data; + video_driver_state_t *video_st = video_state_get_ptr(); if (!d3d11) return; @@ -818,7 +819,7 @@ static void d3d11_gfx_free(void* data) Release(d3d11->scissor_disabled); Release(d3d11->swapChain); - video_st_flags = video_driver_get_st_flags(); + video_st_flags = video_st->flags; if (video_st_flags & VIDEO_FLAG_CACHE_CONTEXT) { cached_device_d3d11 = d3d11->device; diff --git a/gfx/drivers/vulkan.c b/gfx/drivers/vulkan.c index e9bf397cdb..a22151547f 100644 --- a/gfx/drivers/vulkan.c +++ b/gfx/drivers/vulkan.c @@ -868,13 +868,11 @@ static void vulkan_init_textures(vk_t *vk) static void vulkan_deinit_textures(vk_t *vk) { int i; - const void* cached_frame; video_driver_state_t *video_st = video_state_get_ptr(); - /* Avoid memcpying from a destroyed/unmapped texture later on. */ - video_driver_cached_frame_get(&cached_frame, NULL, NULL, NULL); + const void *cached_frame = video_st->frame_cache_data; if (vulkan_is_mapped_swapchain_texture_ptr(vk, cached_frame)) - video_st->frame_cache_data = NULL; + video_st->frame_cache_data = NULL; vkDestroySampler(vk->context->device, vk->samplers.nearest, NULL); vkDestroySampler(vk->context->device, vk->samplers.linear, NULL); diff --git a/gfx/drivers_context/wgl_ctx.c b/gfx/drivers_context/wgl_ctx.c index 13c9f552d6..4332821904 100644 --- a/gfx/drivers_context/wgl_ctx.c +++ b/gfx/drivers_context/wgl_ctx.c @@ -523,10 +523,11 @@ static void gfx_ctx_wgl_destroy(void *data) if (win32_hrc) { uint32_t video_st_flags; + video_driver_state_t *video_st = video_state_get_ptr(); gl_finish(); wglMakeCurrent(NULL, NULL); - video_st_flags = video_driver_get_st_flags(); + video_st_flags = video_st->flags; if (!(video_st_flags & VIDEO_FLAG_CACHE_CONTEXT)) { if (win32_hw_hrc) diff --git a/gfx/drivers_context/x_ctx.c b/gfx/drivers_context/x_ctx.c index 7449f1291c..52fea21377 100644 --- a/gfx/drivers_context/x_ctx.c +++ b/gfx/drivers_context/x_ctx.c @@ -175,11 +175,13 @@ static void gfx_ctx_x_destroy_resources(gfx_ctx_x_data_t *x) if (x->ctx) { uint32_t video_st_flags; + video_driver_state_t *video_st = video_state_get_ptr(); + glXSwapBuffers(g_x11_dpy, x->glx_win); gl_finish(); glXMakeContextCurrent(g_x11_dpy, None, None, NULL); - video_st_flags = video_driver_get_st_flags(); + video_st_flags = video_st->flags; if (!(video_st_flags & VIDEO_FLAG_CACHE_CONTEXT)) { if (x->hw_ctx) diff --git a/gfx/video_driver.c b/gfx/video_driver.c index 489e52db30..164fa72196 100644 --- a/gfx/video_driver.c +++ b/gfx/video_driver.c @@ -59,6 +59,8 @@ #define TIME_TO_FPS(last_time, new_time, frames) ((1000000.0f * (frames)) / ((new_time) - (last_time))) +#define FRAME_DELAY_AUTO_DEBUG 0 + typedef struct { struct string_list *list; @@ -1656,25 +1658,6 @@ bool video_driver_get_video_output_size(unsigned *width, unsigned *height, char return true; } -void video_driver_set_texture_enable(bool enable, bool fullscreen) -{ - video_driver_state_t *video_st = &video_driver_st; - if (video_st->poke && video_st->poke->set_texture_enable) - video_st->poke->set_texture_enable(video_st->data, - enable, fullscreen); -} - -void video_driver_set_texture_frame(const void *frame, bool rgb32, - unsigned width, unsigned height, float alpha) -{ - video_driver_state_t *video_st = &video_driver_st; - if ( video_st->poke - && video_st->poke->set_texture_frame) - video_st->poke->set_texture_frame(video_st->data, - frame, rgb32, width, height, alpha); -} - - void *video_driver_read_frame_raw(unsigned *width, unsigned *height, size_t *pitch) { @@ -1698,20 +1681,6 @@ void video_driver_set_filtering(unsigned index, index, smooth, ctx_scaling); } -void video_driver_cached_frame_get(const void **data, unsigned *width, - unsigned *height, size_t *pitch) -{ - video_driver_state_t *video_st= &video_driver_st; - if (data) - *data = video_st->frame_cache_data; - if (width) - *width = video_st->frame_cache_width; - if (height) - *height = video_st->frame_cache_height; - if (pitch) - *pitch = video_st->frame_cache_pitch; -} - void video_driver_get_size(unsigned *width, unsigned *height) { video_driver_state_t *video_st = &video_driver_st; @@ -3278,7 +3247,10 @@ bool video_driver_init_internal(bool *video_is_threaded, bool verbosity_enabled) } #if defined(PSP) - video_driver_set_texture_frame(&dummy_pixels, false, 1, 1, 1.0f); + if ( video_st->poke + && video_st->poke->set_texture_frame) + video_st->poke->set_texture_frame(video_st->data, + &dummy_pixels, false, 1, 1, 1.0f); #endif video_context_driver_reset(); @@ -3968,13 +3940,6 @@ void video_driver_reinit(int flags) video_st->flags &= ~VIDEO_FLAG_CACHE_CONTEXT; } -uint32_t video_driver_get_st_flags(void) -{ - video_driver_state_t *video_st = &video_driver_st; - return video_st->flags; -} - -#define FRAME_DELAY_AUTO_DEBUG 0 void video_frame_delay_auto(video_driver_state_t *video_st, video_frame_delay_auto_t *vfda) { int i; diff --git a/gfx/video_driver.h b/gfx/video_driver.h index cd0dc7d6fa..ee97c96893 100644 --- a/gfx/video_driver.h +++ b/gfx/video_driver.h @@ -967,11 +967,6 @@ bool video_driver_set_video_mode(unsigned width, bool video_driver_get_video_output_size( unsigned *width, unsigned *height, char *desc, size_t desc_len); -void video_driver_set_texture_enable(bool enable, bool full_screen); - -void video_driver_set_texture_frame(const void *frame, bool rgb32, - unsigned width, unsigned height, float alpha); - void * video_driver_read_frame_raw(unsigned *width, unsigned *height, size_t *pitch); @@ -988,12 +983,6 @@ void video_driver_set_size(unsigned width, unsigned height); float video_driver_get_aspect_ratio(void); -void video_driver_cached_frame_set(const void *data, unsigned width, - unsigned height, size_t pitch); - -void video_driver_cached_frame_get(const void **data, unsigned *width, - unsigned *height, size_t *pitch); - void video_driver_menu_settings(void **list_data, void *list_info_data, void *group_data, void *subgroup_data, const char *parent_group); @@ -1282,8 +1271,6 @@ void video_driver_set_viewport_config( void video_driver_set_viewport_square_pixel(struct retro_game_geometry *geom); -uint32_t video_driver_get_st_flags(void); - bool video_driver_init_internal(bool *video_is_threaded, bool verbosity_enabled); /** diff --git a/menu/drivers/rgui.c b/menu/drivers/rgui.c index c2c2b60f2b..d38c5ebecf 100644 --- a/menu/drivers/rgui.c +++ b/menu/drivers/rgui.c @@ -6570,9 +6570,20 @@ static void rgui_free(void *data) rgui_thumbnail_free(&rgui->mini_left_thumbnail); } +static void rgui_set_texture_frame(video_driver_state_t *video_st, + const void *frame, bool rgb32, + unsigned width, unsigned height, float alpha) +{ + if ( video_st->poke + && video_st->poke->set_texture_frame) + video_st->poke->set_texture_frame(video_st->data, + frame, rgb32, width, height, alpha); +} + static void rgui_set_texture(void *data) { unsigned fb_width, fb_height; + video_driver_state_t *video_st = video_state_get_ptr(); settings_t *settings = config_get_ptr(); gfx_display_t *p_disp = disp_get_ptr(); #if defined(DINGUX) @@ -6592,7 +6603,7 @@ static void rgui_set_texture(void *data) p_disp->flags &= ~GFX_DISP_FLAG_FB_DIRTY; if (internal_upscale_level == RGUI_UPSCALE_NONE) - video_driver_set_texture_frame(rgui->frame_buf.data, + rgui_set_texture_frame(video_st, rgui->frame_buf.data, false, fb_width, fb_height, 1.0f); else { @@ -6604,7 +6615,7 @@ static void rgui_set_texture(void *data) /* If viewport is currently the same size (or smaller) * than the menu framebuffer, no scaling is required */ if ((vp.width <= fb_width) && (vp.height <= fb_height)) - video_driver_set_texture_frame(rgui->frame_buf.data, + rgui_set_texture_frame(video_st, rgui->frame_buf.data, false, fb_width, fb_height, 1.0f); else { @@ -6651,7 +6662,7 @@ static void rgui_set_texture(void *data) configuration_set_uint(settings, settings->uints.menu_rgui_internal_upscale_level, RGUI_UPSCALE_NONE); - video_driver_set_texture_frame(frame_buf->data, + rgui_set_texture_frame(video_st, frame_buf->data, false, fb_width, fb_height, 1.0f); return; } @@ -6674,7 +6685,7 @@ static void rgui_set_texture(void *data) } /* Draw upscaled texture */ - video_driver_set_texture_frame(upscale_buf->data, + rgui_set_texture_frame(video_st, upscale_buf->data, false, out_width, out_height, 1.0f); } } diff --git a/menu/menu_driver.c b/menu/menu_driver.c index 267a7dee3c..7ac920a501 100644 --- a/menu/menu_driver.c +++ b/menu/menu_driver.c @@ -6380,7 +6380,9 @@ void retroarch_menu_running_finished(bool quit) menu_st->driver_ctx->environ_cb(MENU_ENVIRON_DISABLE_SCREENSAVER, NULL, menu_st->userdata); } - video_driver_set_texture_enable(false, false); + if (video_st->poke && video_st->poke->set_texture_enable) + video_st->poke->set_texture_enable(video_st->data, + false, false); #ifdef HAVE_OVERLAY if (!quit) if (settings && settings->bools.input_overlay_hide_in_menu) diff --git a/tasks/task_screenshot.c b/tasks/task_screenshot.c index 64b5d6185e..7f6845764f 100644 --- a/tasks/task_screenshot.c +++ b/tasks/task_screenshot.c @@ -456,15 +456,17 @@ error: return false; } -static bool take_screenshot_raw(const char *screenshot_dir, +static bool take_screenshot_raw( + video_driver_state_t *video_st, + const char *screenshot_dir, const char *name_base, void *userbuf, bool savestate, uint32_t runloop_flags, bool fullpath, bool use_thread, unsigned pixel_format_type) { - size_t pitch; - unsigned width, height; - const void *data = NULL; - video_driver_cached_frame_get(&data, &width, &height, &pitch); + const void *data = video_st->frame_cache_data; + unsigned width = video_st->frame_cache_width; + unsigned height = video_st->frame_cache_height; + size_t pitch = video_st->frame_cache_pitch; /* Negative pitch is needed as screenshot takes bottom-up, * but we use top-down. */ @@ -500,7 +502,9 @@ static bool take_screenshot_choice( if (supports_viewport_read) { /* Avoid taking screenshot of GUI overlays. */ - video_driver_set_texture_enable(false, false); + if (video_st->poke && video_st->poke->set_texture_enable) + video_st->poke->set_texture_enable(video_st->data, + false, false); if (!(runloop_flags & RUNLOOP_FLAG_IDLE)) video_driver_cached_frame(); return take_screenshot_viewport(screenshot_dir, @@ -509,20 +513,17 @@ static bool take_screenshot_choice( } if (!has_valid_framebuffer) - return take_screenshot_raw(screenshot_dir, + return take_screenshot_raw(video_st, screenshot_dir, name_base, NULL, savestate, runloop_flags, fullpath, use_thread, pixel_format_type); if (supports_read_frame_raw) { - size_t old_pitch; - unsigned old_width, old_height; - void *frame_data = NULL; - const void* old_data = NULL; - video_driver_cached_frame_get(&old_data, &old_width, &old_height, - &old_pitch); - - frame_data = video_driver_read_frame_raw( + const void *old_data = video_st->frame_cache_data; + unsigned old_width = video_st->frame_cache_width; + unsigned old_height = video_st->frame_cache_height; + size_t old_pitch = video_st->frame_cache_pitch; + void *frame_data = video_driver_read_frame_raw( &old_width, &old_height, &old_pitch); video_st->frame_cache_data = old_data; @@ -533,7 +534,7 @@ static bool take_screenshot_choice( if (frame_data) { video_st->frame_cache_data = frame_data; - return take_screenshot_raw(screenshot_dir, + return take_screenshot_raw(video_st, screenshot_dir, name_base, frame_data, savestate, runloop_flags, fullpath, use_thread, pixel_format_type); } diff --git a/tasks/task_translation.c b/tasks/task_translation.c index c3b18bd880..cb37c3892a 100644 --- a/tasks/task_translation.c +++ b/tasks/task_translation.c @@ -295,7 +295,10 @@ static void handle_translation_cb( if (raw_image_file_data) { /* Get the video frame dimensions reference */ - video_driver_cached_frame_get(&dummy_data, &width, &height, &pitch); + dummy_data = video_st->frame_cache_data; + width = video_st->frame_cache_width; + height = video_st->frame_cache_height; + pitch = video_st->frame_cache_pitch; /* try two different modes for text display * * In the first mode, we use display widget overlays, but they require @@ -850,7 +853,10 @@ bool run_translation_service(settings_t *settings, bool paused) if (!scaler) goto finish; - video_driver_cached_frame_get(&data, &width, &height, &pitch); + data = video_st->frame_cache_data; + width = video_st->frame_cache_width; + height = video_st->frame_cache_height; + pitch = video_st->frame_cache_pitch; if (!data) goto finish;