Skip to content

Commit

Permalink
Custom aspect ratio safeguards (#17168)
Browse files Browse the repository at this point in the history
  • Loading branch information
sonninnos authored Nov 6, 2024
1 parent c27075d commit 767be48
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 25 deletions.
9 changes: 6 additions & 3 deletions gfx/video_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
81 changes: 59 additions & 22 deletions menu/menu_setting.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
Expand All @@ -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)",
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 +
Expand All @@ -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) */
Expand Down

0 comments on commit 767be48

Please sign in to comment.