From be8707125795af849f670fc2e48a861fcaf0058a Mon Sep 17 00:00:00 2001 From: sonninnos Date: Wed, 6 Nov 2024 20:41:46 +0200 Subject: [PATCH] Custom aspect ratio safeguards --- gfx/video_driver.c | 9 +++-- menu/menu_setting.c | 81 +++++++++++++++++++++++++++++++++------------ 2 files changed, 65 insertions(+), 25 deletions(-) diff --git a/gfx/video_driver.c b/gfx/video_driver.c index b235844fd4e..3500df1c5b1 100644 --- a/gfx/video_driver.c +++ b/gfx/video_driver.c @@ -2081,10 +2081,12 @@ void video_viewport_get_scaled_aspect2(struct video_viewport *vp, unsigned viewp video_viewport_t *custom_vp = &settings->video_viewport_custom; int padding_x = 0; int padding_y = 0; + x = custom_vp->x; y = custom_vp->y; + if (!ydown) - y = vp->full_height - (y + custom_vp->height); + y = vp->full_height - (y + custom_vp->height); padding_x += (viewport_width - custom_vp->width); if (padding_x < 0) padding_x *= 2; @@ -2372,8 +2374,8 @@ void video_viewport_get_scaled_integer(struct video_viewport *vp, } #endif - content_width = (content_width == 4) ? video_st->av_info.geometry.base_width : content_width; - content_height = (content_height == 4) ? video_st->av_info.geometry.base_height : content_height; + content_width = (content_width <= 4) ? video_st->av_info.geometry.base_width : content_width; + content_height = (content_height <= 4) ? video_st->av_info.geometry.base_height : content_height; if (!ydown) viewport_bias_y = 1.0 - viewport_bias_y; @@ -2409,6 +2411,7 @@ void video_viewport_get_scaled_integer(struct video_viewport *vp, { x = custom_vp->x; y = custom_vp->y; + if (!ydown) y = vp->height - (y + custom_vp->height); padding_x = width - custom_vp->width; diff --git a/menu/menu_setting.c b/menu/menu_setting.c index 54d532bd854..b029fe5d9a0 100644 --- a/menu/menu_setting.c +++ b/menu/menu_setting.c @@ -4958,8 +4958,10 @@ static void setting_get_string_representation_uint_custom_viewport_width(rarch_s return; geom = (struct retro_game_geometry*)&av_info->geometry; - _len = snprintf(s, len, "%u", - *setting->value.target.unsigned_integer); + _len = snprintf(s, len, "%u", *setting->value.target.unsigned_integer); + + if (!geom->base_width || !geom->base_height) + return; if (!(rotation % 2) && (*setting->value.target.unsigned_integer % geom->base_width == 0)) snprintf(s + _len, len - _len, " (%ux)", @@ -4981,8 +4983,10 @@ static void setting_get_string_representation_uint_custom_viewport_height(rarch_ return; geom = (struct retro_game_geometry*)&av_info->geometry; - _len = snprintf(s, len, "%u", - *setting->value.target.unsigned_integer); + _len = snprintf(s, len, "%u", *setting->value.target.unsigned_integer); + + if (!geom->base_width || !geom->base_height) + return; if (!(rotation % 2) && (*setting->value.target.unsigned_integer % geom->base_height == 0)) snprintf(s + _len, len - _len, " (%ux)", @@ -5836,11 +5840,17 @@ static int setting_uint_action_left_custom_viewport_width( { if (custom->width > geom->base_height) custom->width -= geom->base_height; + + if (custom->width < geom->base_height) + custom->width = geom->base_height; } else { if (custom->width > geom->base_width) custom->width -= geom->base_width; + + if (custom->width < geom->base_width) + custom->width = geom->base_width; } } else @@ -5877,11 +5887,17 @@ static int setting_uint_action_left_custom_viewport_height( { if (custom->height > geom->base_width) custom->height -= geom->base_width; + + if (custom->height < geom->base_width) + custom->height = geom->base_width; } else { if (custom->height > geom->base_height) custom->height -= geom->base_height; + + if (custom->height < geom->base_height) + custom->height = geom->base_height; } } else @@ -8517,25 +8533,35 @@ static void general_write_handler(rarch_setting_t *setting) if (*setting->value.target.boolean) { - unsigned int rotation = retroarch_get_rotation(); - struct retro_game_geometry *geom = (struct retro_game_geometry*) - &av_info->geometry; + unsigned rotation = retroarch_get_rotation(); + unsigned base_width = 0; + unsigned base_height = 0; + struct retro_game_geometry *geom = (struct retro_game_geometry*)&av_info->geometry; - custom_vp->x = 0; - custom_vp->y = 0; + custom_vp->x = 0; + custom_vp->y = 0; + + base_width = (geom->base_width) ? geom->base_width : video_st->frame_cache_width; + base_height = (geom->base_height) ? geom->base_height : video_st->frame_cache_height; + + if (base_width <= 4 || base_height <= 4) + { + base_width = (rotation % 2) ? 240 : 320; + base_height = (rotation % 2) ? 320 : 240; + } if (rotation % 2) { - custom_vp->width = ((custom_vp->width + geom->base_height - 1) / geom->base_height) * geom->base_height; - custom_vp->height = ((custom_vp->height + geom->base_width - 1) / geom->base_width) * geom->base_width; + custom_vp->width = ((custom_vp->width + base_height - 1) / base_height) * base_height; + custom_vp->height = ((custom_vp->height + base_width - 1) / base_width) * base_width; } else { - custom_vp->width = ((custom_vp->width + geom->base_width - 1) / geom->base_width) * geom->base_width; - custom_vp->height = ((custom_vp->height + geom->base_height - 1) / geom->base_height) * geom->base_height; + custom_vp->width = ((custom_vp->width + base_width - 1) / base_width) * base_width; + custom_vp->height = ((custom_vp->height + base_height - 1) / base_height) * base_height; } - aspectratio_lut[ASPECT_RATIO_CUSTOM].value = - (float)custom_vp->width / custom_vp->height; + + aspectratio_lut[ASPECT_RATIO_CUSTOM].value = (float)custom_vp->width / custom_vp->height; } } break; @@ -8845,9 +8871,10 @@ static void general_write_handler(rarch_setting_t *setting) if (sys_info) { - unsigned int rotation = retroarch_get_rotation(); - struct retro_game_geometry *geom = (struct retro_game_geometry*) - &av_info->geometry; + unsigned rotation = retroarch_get_rotation(); + unsigned base_width = 0; + unsigned base_height = 0; + struct retro_game_geometry *geom = (struct retro_game_geometry*)&av_info->geometry; video_driver_set_rotation( (*setting->value.target.unsigned_integer + @@ -8858,18 +8885,28 @@ static void general_write_handler(rarch_setting_t *setting) custom_vp->x = 0; custom_vp->y = 0; + base_width = (geom->base_width) ? geom->base_width : video_st->frame_cache_width; + base_height = (geom->base_height) ? geom->base_height : video_st->frame_cache_height; + + if (base_width <= 4 || base_height <= 4) + { + base_width = (rotation % 2) ? 240 : 320; + base_height = (rotation % 2) ? 320 : 240; + } + /* Round down when rotation is "horizontal", round up when rotation is "vertical" to avoid expanding viewport each time user rotates */ if (rotation % 2) { - custom_vp->width = MAX(1, (custom_vp->width / geom->base_height)) * geom->base_height; - custom_vp->height = MAX(1, (custom_vp->height / geom->base_width )) * geom->base_width; + custom_vp->width = MAX(1, (custom_vp->width / base_height)) * base_height; + custom_vp->height = MAX(1, (custom_vp->height / base_width )) * base_width; } else { - custom_vp->width = ((custom_vp->width + geom->base_width - 1) / geom->base_width) * geom->base_width; - custom_vp->height = ((custom_vp->height + geom->base_height - 1) / geom->base_height) * geom->base_height; + custom_vp->width = ((custom_vp->width + base_width - 1) / base_width) * base_width; + custom_vp->height = ((custom_vp->height + base_height - 1) / base_height) * base_height; } + aspectratio_lut[ASPECT_RATIO_CUSTOM].value = (float)custom_vp->width / custom_vp->height; /* Update Aspect Ratio (only useful for 1:1 PAR) */