From 87cf910396c008035b752e3b9c14014f60bc57ff Mon Sep 17 00:00:00 2001 From: twinaphex Date: Sun, 15 Apr 2018 17:12:28 +0200 Subject: [PATCH] (CRT switch) Cleanups --- gfx/video_crt_switch.c | 380 +++++++++++++++++++---------------------- gfx/video_crt_switch.h | 29 +++- gfx/video_driver.c | 60 +++---- gfx/video_driver.h | 6 - 4 files changed, 226 insertions(+), 249 deletions(-) diff --git a/gfx/video_crt_switch.c b/gfx/video_crt_switch.c index d9bded15f9..ef9d4b169b 100644 --- a/gfx/video_crt_switch.c +++ b/gfx/video_crt_switch.c @@ -24,271 +24,241 @@ #include #endif - #include "video_driver.h" #include "video_crt_switch.h" -static float ra_tmp_core_hz; -static int ra_core_width; -static int ra_core_height; -static int ra_tmp_width; -static int ra_tmp_height; -static float fly_aspect; -static float ra_core_hz; -static int ra_set_core_hz; -static int orig_width; -static int orig_height; -static int first_run; +static int ra_core_width = 0; +static int ra_core_height = 0; +static int ra_tmp_width = 0; +static int ra_tmp_height = 0; +static int ra_set_core_hz = 0; +static int orig_width = 0; +static int orig_height = 0; +static int first_run = 0; + +static float ra_tmp_core_hz = 0.0f; +static float fly_aspect = 0.0f; +static float ra_core_hz = 0.0f; void crt_switch_res_core(int width, int height, float hz) { - /* ben_core_hz float passed from with in void video_driver_monitor_adjust_system_rates(void) */ - ra_core_width = width; + /* ra_core_hz float passed from within + * void video_driver_monitor_adjust_system_rates(void) */ + ra_core_width = width; ra_core_height = height; - ra_core_hz = hz; + ra_core_hz = hz; + crt_check_first_run(); - - if (ra_tmp_height != ra_core_height || ra_core_width != ra_tmp_width) - { /* detect resolution change and switch */ + + /* Detect resolution change and switch */ + if ( + (ra_tmp_height != ra_core_height) || + (ra_core_width != ra_tmp_width) + ) crt_screen_setup_aspect(width,height); - } - ra_tmp_height = ra_core_height; - ra_tmp_width = ra_core_width; - + ra_tmp_height = ra_core_height; + ra_tmp_width = ra_core_width; + + /* Check if aspect is correct, if notchange */ if (video_driver_get_aspect_ratio() != fly_aspect) - { /* check aspect is correct else change */ + { video_driver_set_aspect_ratio_value((float)fly_aspect); - crt_poke_video(); + video_driver_apply_state_changes(); } } -void crt_check_first_run() -{ /* ruin of first boot to get current display resolution */ + +void crt_check_first_run(void) +{ if (first_run != 1) { - #if defined(_WIN32) - orig_height = GetSystemMetrics(SM_CYSCREEN); - orig_width = GetSystemMetrics(SM_CXSCREEN); - #endif - + /* Run of first boot to get current display resolution */ +#if defined(_WIN32) + orig_height = GetSystemMetrics(SM_CYSCREEN); + orig_width = GetSystemMetrics(SM_CXSCREEN); +#endif } + first_run = 1; } +/* Create correct aspect to fit video if resolution does not exist */ void crt_screen_setup_aspect(int width, int height) -{ /* create correct aspect to fit video if resolution does not exist */ +{ switch_crt_hz(); - /* get original resolution of core */ - - + + /* get original resolution of core */ if (height == 4) - { + { + /* detect menu only */ if (width < 1920) - { width = 640; - }/* detect menu only */ - height = 480; - crt_aspect_ratio_switch(width,height); - } - if (height < 191 && height != 144) - { - crt_aspect_ratio_switch(width,height); - height = 200; - } - if (height > 191) - { - crt_aspect_ratio_switch(width,height); - } - if (height == 144 && ra_set_core_hz == 50) - { - height = 288; - crt_aspect_ratio_switch(width,height); - } - if (height > 200 && height < 224) - { - crt_aspect_ratio_switch(width,height); - height = 224; - } - if (height > 224 && height < 240) - { - crt_aspect_ratio_switch(width,height); - height = 240; - } - if (height > 240 && height < 255 ) - { - crt_aspect_ratio_switch(width,height); - height = 254; - } - if (height == 528 && ra_set_core_hz == 60) - { - crt_aspect_ratio_switch(width,height); - height = 480; - } - if (height >= 240 && height < 255 && ra_set_core_hz == 55) - { - crt_aspect_ratio_switch(width,height); - height = 254; - } - + height = 480; + crt_aspect_ratio_switch(width,height); + } + + if (height < 191 && height != 144) + { + crt_aspect_ratio_switch(width,height); + height = 200; + } + + if (height > 191) + crt_aspect_ratio_switch(width,height); + + if (height == 144 && ra_set_core_hz == 50) + { + height = 288; + crt_aspect_ratio_switch(width,height); + } + + if (height > 200 && height < 224) + { + crt_aspect_ratio_switch(width,height); + height = 224; + } + + if (height > 224 && height < 240) + { + crt_aspect_ratio_switch(width,height); + height = 240; + } + + if (height > 240 && height < 255) + { + crt_aspect_ratio_switch(width,height); + height = 254; + } + + if (height == 528 && ra_set_core_hz == 60) + { + crt_aspect_ratio_switch(width,height); + height = 480; + } + + if (height >= 240 && height < 255 && ra_set_core_hz == 55) + { + crt_aspect_ratio_switch(width,height); + height = 254; + } + switch_res_crt(width, height); } -void switch_res_crt(int width, int height){ - - if ( height > 100) +void switch_res_crt(int width, int height) +{ + if (height > 100) { - crt_switch_res(width, height,0,ra_set_core_hz); - crt_poke_video(); + video_driver_apply_state_changes(); } } -void crt_aspect_ratio_switch(int width,int height) -{ /* send aspect float to videeo_driver */ - fly_aspect = (float)width/height; +void crt_aspect_ratio_switch(int width, int height) +{ + /* send aspect float to videeo_driver */ + fly_aspect = (float)width / height; video_driver_set_aspect_ratio_value((float)fly_aspect); } -void switch_crt_hz() -{ /* set hz float an int for windows switching */ - - if (ra_core_hz != ra_tmp_core_hz) - { - if (ra_core_hz < 53 ) - { +void switch_crt_hz(void) +{ + if (ra_core_hz == ra_tmp_core_hz) + return; + + /* set hz float an int for windows switching */ + if (ra_core_hz < 53) ra_set_core_hz = 50; - } if (ra_core_hz >= 53 && ra_core_hz < 57) - { ra_set_core_hz = 55; - } - if (ra_core_hz >= 57 ) - { + if (ra_core_hz >= 57) ra_set_core_hz = 60; - } video_monitor_set_refresh_rate(ra_set_core_hz); ra_tmp_core_hz = ra_core_hz; - } } -void crt_video_restore() +void crt_video_restore(void) { - crt_switch_res(orig_width, orig_height,0,60); - } void crt_switch_res(int width, int height, int f_restore,int ra_hz) -{ /* windows function to swith resolutions */ +{ + /* Windows function to switch resolutions */ - #if defined(_WIN32) - DEVMODE curDevmode; - DEVMODE devmode; - DWORD flags = 0; - - int iModeNum; - int depth = 0; - int freq; - LONG res; +#if defined(_WIN32) + LONG res; + DEVMODE curDevmode; + DEVMODE devmode; - if (f_restore == 0) - { - freq = ra_hz; - } - else - { - freq = 0; - } - - EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &curDevmode); - - if (width == curDevmode.dmPelsWidth) - { - width = 0; /* used to stop superresolution bug */ - } - if (width == 0) - { - width = curDevmode.dmPelsWidth; - } - if (height == 0) - { - height = curDevmode.dmPelsHeight; - } - if (depth == 0) - { - depth = curDevmode.dmBitsPerPel; - } - if (freq == 0) - { - freq = curDevmode.dmDisplayFrequency; - } + int iModeNum; + int freq; + DWORD flags = 0; + int depth = 0; - for (iModeNum = 0; ; iModeNum++) + if (f_restore == 0) + freq = ra_hz; + else + freq = 0; + + EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &curDevmode); + + /* used to stop superresolution bug */ + if (width == curDevmode.dmPelsWidth) + width = 0; + if (width == 0) + width = curDevmode.dmPelsWidth; + if (height == 0) + height = curDevmode.dmPelsHeight; + if (depth == 0) + depth = curDevmode.dmBitsPerPel; + if (freq == 0) + freq = curDevmode.dmDisplayFrequency; + + for (iModeNum = 0; ; iModeNum++) + { + if (!EnumDisplaySettings(NULL, iModeNum, &devmode)) + break; + + if (devmode.dmPelsWidth != width) + continue; + + if (devmode.dmPelsHeight != height) + continue; + + if (devmode.dmBitsPerPel != depth) + continue; + + if (devmode.dmDisplayFrequency != freq) + continue; + + devmode.dmFields |= DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL | DM_DISPLAYFREQUENCY; + res = ChangeDisplaySettings(&devmode, CDS_TEST); + + switch (res) { - if (EnumDisplaySettings(NULL, iModeNum, &devmode)) - { - - if (devmode.dmPelsWidth != width) - { - continue; - } - - if (devmode.dmPelsHeight != height) - { - continue; - } - - if (devmode.dmBitsPerPel != depth) - { - continue; - } - - if (devmode.dmDisplayFrequency != freq) - { - continue; - } - - devmode.dmFields |= DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL | DM_DISPLAYFREQUENCY; - res = ChangeDisplaySettings(&devmode, CDS_TEST); - + case DISP_CHANGE_SUCCESSFUL: + res = ChangeDisplaySettings(&devmode, flags); switch (res) { - case DISP_CHANGE_SUCCESSFUL: - res = ChangeDisplaySettings(&devmode, flags); - switch (res) - { - case DISP_CHANGE_SUCCESSFUL: - return; - - case DISP_CHANGE_NOTUPDATED: - return; - - default: - - break; + case DISP_CHANGE_SUCCESSFUL: + return; + case DISP_CHANGE_NOTUPDATED: + return; + default: + break; } - break; - - case DISP_CHANGE_RESTART: - - break; - - default: - - break; - } - } - else - { break; - } + case DISP_CHANGE_RESTART: + break; + default: + break; } - #endif + } +#elif defined(linux) - #if defined(linux) - - #endif +#endif } diff --git a/gfx/video_crt_switch.h b/gfx/video_crt_switch.h index c93d9dea0e..dffb34ad4e 100644 --- a/gfx/video_crt_switch.h +++ b/gfx/video_crt_switch.h @@ -17,11 +17,32 @@ * If not, see . */ -void crt_check_first_run(); +#ifndef __VIDEO_CRT_SWITCH_H__ +#define __VIDEO_CRT_SWITCH_H__ + +#include + +#include +#include + +RETRO_BEGIN_DECLS + +void crt_check_first_run(void); + void crt_switch_res_core(int width, int height, float hz); + void crt_screen_setup_aspect(int width, int height); + void switch_res_crt(int width, int height); + void crt_aspect_ratio_switch(int width,int height); -void switch_crt_hz(); -void crt_video_restore(); -void crt_switch_res(int width, int height, int f_restore,int ra_hz); + +void switch_crt_hz(void); + +void crt_video_restore(void); + +void crt_switch_res(int width, int height, int f_restore, int ra_hz); + +RETRO_END_DECLS + +#endif diff --git a/gfx/video_driver.c b/gfx/video_driver.c index 5838962696..10bb453aa7 100644 --- a/gfx/video_driver.c +++ b/gfx/video_driver.c @@ -119,9 +119,6 @@ #define video_driver_context_unlock() ((void)0) #endif -static bool crt_switching_active; -static float video_driver_core_hz; - typedef struct video_pixel_scaler { @@ -146,10 +143,12 @@ static rarch_softfilter_t *video_driver_state_filter = NULL; static void *video_driver_state_buffer = NULL; static unsigned video_driver_state_scale = 0; static unsigned video_driver_state_out_bpp = 0; -static bool video_driver_state_out_rgb32 = false; +static bool video_driver_state_out_rgb32 = false; +static bool video_driver_crt_switching_active = false; static struct retro_system_av_info video_driver_av_info; + static enum retro_pixel_format video_driver_pix_fmt = RETRO_PIXEL_FORMAT_0RGB1555; static const void *frame_cache_data = NULL; @@ -158,6 +157,7 @@ static unsigned frame_cache_height = 0; static size_t frame_cache_pitch = 0; static bool video_driver_threaded = false; +static float video_driver_core_hz = 0.0f; static float video_driver_aspect_ratio = 0.0f; static unsigned video_driver_width = 0; static unsigned video_driver_height = 0; @@ -1425,14 +1425,11 @@ void video_driver_monitor_adjust_system_rates(void) if (!info || info->fps <= 0.0) return; - if (crt_switching_active == true) - { + if (video_driver_crt_switching_active) timing_skew = fabs(1.0f - info->fps / video_driver_core_hz); - } else - { timing_skew = fabs(1.0f - info->fps / video_refresh_rate); - } + /* We don't want to adjust pitch too much. If we have extreme cases, * just don't readjust at all. */ if (timing_skew <= settings->floats.audio_max_timing_skew) @@ -1442,15 +1439,17 @@ void video_driver_monitor_adjust_system_rates(void) video_refresh_rate, (float)info->fps); - if (crt_switching_active == true){ - if (info->fps <= video_driver_core_hz) - return; + if (video_driver_crt_switching_active) + { + if (info->fps <= video_driver_core_hz) + return; } else { - if (info->fps <= video_refresh_rate) - return; + if (info->fps <= video_refresh_rate) + return; } + /* We won't be able to do VSync reliably when game FPS > monitor FPS. */ rarch_ctl(RARCH_CTL_SET_NONBLOCK_FORCED, NULL); RARCH_LOG("[Video]: Game FPS > Monitor FPS. Cannot rely on VSync.\n"); @@ -2604,29 +2603,22 @@ void video_driver_frame(const void *data, unsigned width, runloop_msg_queue_push(video_info.fps_text, 2, 1, true); /* trigger set resolution*/ - if (video_info.crt_switch_resolution == true){ - crt_switching_active = true; + if (video_info.crt_switch_resolution) + { + video_driver_crt_switching_active = true; - if (video_info.crt_switch_resolution_super == 2560){ - width = 2560; - } - if (video_info.crt_switch_resolution_super == 3840){ - width = 3840; - } - if (video_info.crt_switch_resolution_super == 1920){ - width = 1920; - } - crt_switch_res_core(width, height, video_driver_core_hz); - } else if (video_info.crt_switch_resolution == false){ - crt_switching_active = false; - } + if (video_info.crt_switch_resolution_super == 2560) + width = 2560; + if (video_info.crt_switch_resolution_super == 3840) + width = 3840; + if (video_info.crt_switch_resolution_super == 1920) + width = 1920; + crt_switch_res_core(width, height, video_driver_core_hz); + } + else if (!video_info.crt_switch_resolution) + video_driver_crt_switching_active = false; /* trigger set resolution*/ - -} - -void crt_poke_video(){ - video_driver_poke->apply_state_changes(video_driver_data); } void video_driver_display_type_set(enum rarch_display_type type) diff --git a/gfx/video_driver.h b/gfx/video_driver.h index a0cf6be1d2..d1a2551986 100644 --- a/gfx/video_driver.h +++ b/gfx/video_driver.h @@ -71,12 +71,6 @@ RETRO_BEGIN_DECLS #define MAX_VARIABLES 64 -/* Added for resolution wswitching */ -void crt_poke_video(); - -/* Added for resolution wswitching */ - - enum { TEXTURES = 8,