Skip to content

Commit da3a59e

Browse files
committed
big picture: fix image/video aspect ratios
1 parent ca5fca0 commit da3a59e

7 files changed

Lines changed: 91 additions & 37 deletions

rpcs3/Emu/RSX/Overlays/BigPicture/overlay_big_picture_game_details.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,12 @@ namespace rsx
8282

8383
if (icon_data)
8484
{
85+
m_icon.set_keep_aspect_ratio(true);
8586
m_icon.set_raw_image(icon_data);
8687
}
8788
else
8889
{
90+
m_icon.set_keep_aspect_ratio(false);
8991
m_icon.set_image_resource(resource_config::standard_image_resource::new_entry);
9092
}
9193

rpcs3/Emu/RSX/Overlays/BigPicture/overlay_big_picture_game_grid.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ namespace rsx
2727
// The renderer's texture cache is keyed by this object's address, which can be reused by an
2828
// unrelated image after the old one is freed - force a re-upload instead of trusting the cache.
2929
m_icon_data->dirty = true;
30+
static_cast<image_view*>(icon.get())->set_keep_aspect_ratio(true);
3031
static_cast<image_view*>(icon.get())->set_raw_image(m_icon_data.get());
3132
}
3233
else

rpcs3/Emu/RSX/Overlays/overlay_controls.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,6 +1041,60 @@ namespace rsx
10411041
return result;
10421042
}
10431043

1044+
void image_view::adjust_padding()
1045+
{
1046+
overlay_element::set_padding(m_original_padding_left, m_original_padding_right, m_original_padding_top, m_original_padding_bottom);
1047+
1048+
if (!m_keep_aspect_ratio || !external_ref || external_ref->w <= 0 || external_ref->h <= 0)
1049+
{
1050+
return;
1051+
}
1052+
1053+
// Adjust padding so that the image keeps its aspect ratio
1054+
1055+
const u16 p_left = m_original_padding_left;
1056+
const u16 p_right = m_original_padding_right;
1057+
const u16 p_top = m_original_padding_top;
1058+
const u16 p_bottom = m_original_padding_bottom;
1059+
1060+
const u16 target_width = w - (p_left + p_right);
1061+
const u16 target_height = h - (p_top + p_bottom);
1062+
const f32 target_ratio = target_width / static_cast<f32>(target_height);
1063+
const f32 image_ratio = external_ref->w / static_cast<f32>(external_ref->h);
1064+
const f32 convert_ratio = image_ratio / target_ratio;
1065+
1066+
if (convert_ratio > 1.0f)
1067+
{
1068+
const u16 new_padding = static_cast<u16>(target_height - target_height / convert_ratio) / 2;
1069+
overlay_element::set_padding(p_left, p_right, new_padding + p_top, new_padding + p_bottom);
1070+
}
1071+
else if (convert_ratio < 1.0f)
1072+
{
1073+
const u16 new_padding = static_cast<u16>(target_width - target_width * convert_ratio) / 2;
1074+
overlay_element::set_padding(p_left + new_padding, p_right + new_padding, p_top, p_bottom);
1075+
}
1076+
}
1077+
1078+
void image_view::set_padding(u16 left, u16 right, u16 top, u16 bottom)
1079+
{
1080+
m_original_padding_left = left;
1081+
m_original_padding_right = right;
1082+
m_original_padding_top = top;
1083+
m_original_padding_bottom = bottom;
1084+
1085+
adjust_padding();
1086+
}
1087+
1088+
void image_view::set_padding(u16 padding)
1089+
{
1090+
m_original_padding_left = padding;
1091+
m_original_padding_right = padding;
1092+
m_original_padding_top = padding;
1093+
m_original_padding_bottom = padding;
1094+
1095+
adjust_padding();
1096+
}
1097+
10441098
compiled_resource& image_view::get_compiled()
10451099
{
10461100
if (is_compiled())
@@ -1086,6 +1140,8 @@ namespace rsx
10861140
{
10871141
image_resource_ref = image_resource_id::raw_image;
10881142
external_ref = raw_image;
1143+
1144+
adjust_padding();
10891145
}
10901146

10911147
void image_view::clear_image()
@@ -1099,6 +1155,15 @@ namespace rsx
10991155
blur_strength = strength;
11001156
}
11011157

1158+
void image_view::set_keep_aspect_ratio(bool enabled)
1159+
{
1160+
if (m_keep_aspect_ratio != enabled)
1161+
{
1162+
m_keep_aspect_ratio = enabled;
1163+
adjust_padding();
1164+
}
1165+
}
1166+
11021167
image_button::image_button()
11031168
{
11041169
// Do not clip text to region extents

rpcs3/Emu/RSX/Overlays/overlay_controls.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,20 +368,34 @@ namespace rsx
368368
{
369369
protected:
370370
u8 image_resource_ref = image_resource_id::none;
371-
const void* external_ref = nullptr;
371+
const image_info_base* external_ref = nullptr;
372+
373+
// Original padding of inherited class. Helps us to keep the image aspect ratio when padding is adjusted.
374+
u16 m_original_padding_left = 0;
375+
u16 m_original_padding_right = 0;
376+
u16 m_original_padding_top = 0;
377+
u16 m_original_padding_bottom = 0;
378+
379+
bool m_keep_aspect_ratio = false;
372380

373381
// Strength of blur effect
374382
u8 blur_strength = 0;
375383

384+
void adjust_padding();
385+
376386
public:
377387
using overlay_element::overlay_element;
378388

389+
void set_padding(u16 left, u16 right, u16 top, u16 bottom) override;
390+
void set_padding(u16 padding) override;
391+
379392
compiled_resource& get_compiled() override;
380393

381394
void set_image_resource(u8 resource_id);
382395
void set_raw_image(const image_info_base* raw_image);
383396
void clear_image();
384397
void set_blur_strength(u8 strength);
398+
void set_keep_aspect_ratio(bool enabled);
385399
};
386400

387401
struct image_button : public image_view

rpcs3/Emu/RSX/Overlays/overlay_media_list_dialog.cpp

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -55,27 +55,8 @@ namespace rsx
5555
if (fs::exists(entry.info.path))
5656
{
5757
// Fit the new image into the available space
58-
if (entry.info.width > 0 && entry.info.height > 0)
59-
{
60-
const u16 target_width = image->w - (image->padding_left + image->padding_right);
61-
const u16 target_height = image->h - (image->padding_top + image->padding_bottom);
62-
const f32 target_ratio = target_width / static_cast<f32>(target_height);
63-
const f32 image_ratio = entry.info.width / static_cast<f32>(entry.info.height);
64-
const f32 convert_ratio = image_ratio / target_ratio;
65-
66-
if (convert_ratio > 1.0f)
67-
{
68-
const u16 new_padding = static_cast<u16>(target_height - target_height / convert_ratio) / 2;
69-
image->set_padding(image->padding_left, image->padding_right, new_padding + image->padding_top, new_padding + image->padding_bottom);
70-
}
71-
else if (convert_ratio < 1.0f)
72-
{
73-
const u16 new_padding = static_cast<u16>(target_width - target_width * convert_ratio) / 2;
74-
image->set_padding(image->padding_left + new_padding, image->padding_right + new_padding, image->padding_top, image->padding_bottom);
75-
}
76-
}
77-
7858
icon_data = std::make_unique<image_info>(entry.info.path);
59+
static_cast<image_view*>(image.get())->set_keep_aspect_ratio(true);
7960
static_cast<image_view*>(image.get())->set_raw_image(icon_data.get());
8061
}
8162
else

rpcs3/Emu/RSX/Overlays/overlay_message_dialog.cpp

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -391,25 +391,10 @@ namespace rsx
391391
background.back_color.a = 0.f;
392392

393393
background_poster.set_size(virtual_width, virtual_height);
394+
background_poster.set_keep_aspect_ratio(true);
394395
background_poster.set_raw_image(background_image.get());
395396
background_poster.set_blur_strength(static_cast<u8>(background_blur_strength));
396397

397-
ensure(background_image->w > 0);
398-
ensure(background_image->h > 0);
399-
ensure(background_poster.h > 0);
400-
401-
// Set padding in order to keep the aspect ratio
402-
if ((background_image->w / static_cast<double>(background_image->h)) > (background_poster.w / static_cast<double>(background_poster.h)))
403-
{
404-
const int padding = (background_poster.h - static_cast<int>(background_image->h * (background_poster.w / static_cast<double>(background_image->w)))) / 2;
405-
background_poster.set_padding(0, 0, padding, padding);
406-
}
407-
else
408-
{
409-
const int padding = (background_poster.w - static_cast<int>(background_image->w * (background_poster.h / static_cast<double>(background_image->h)))) / 2;
410-
background_poster.set_padding(padding, padding, 0, 0);
411-
}
412-
413398
if (background_overlay_image && background_overlay_image->get_data())
414399
{
415400
constexpr f32 reference_factor = 2.0f / 3.0f;

rpcs3/Emu/RSX/Overlays/overlay_video.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ namespace rsx
1414
if (!thumbnail_path.empty())
1515
{
1616
m_thumbnail_info = std::make_unique<image_info>(thumbnail_path);
17+
set_keep_aspect_ratio(true);
1718
set_raw_image(m_thumbnail_info.get());
1819
}
1920
}
@@ -25,6 +26,7 @@ namespace rsx
2526
if (!thumbnail_buf.empty())
2627
{
2728
m_thumbnail_info = std::make_unique<image_info>(thumbnail_buf);
29+
set_keep_aspect_ratio(true);
2830
set_raw_image(m_thumbnail_info.get());
2931
}
3032
}
@@ -48,6 +50,7 @@ namespace rsx
4850
if (auto img = image_info::load_icon(info.icon_path, info.icon_in_archive ? info.path : ""))
4951
{
5052
m_thumbnail_info = std::move(img);
53+
set_keep_aspect_ratio(true);
5154
set_raw_image(m_thumbnail_info.get());
5255
}
5356
}
@@ -102,20 +105,23 @@ namespace rsx
102105
m_video_source->get_image(info->data, info->w, info->h, info->channels, info->bpp);
103106
info->dirty = true;
104107

108+
set_keep_aspect_ratio(true);
105109
set_raw_image(info.get());
106110
m_is_compiled = false;
107111
return;
108112
}
109113

110114
if (m_thumbnail_info && m_thumbnail_info.get() != external_ref)
111115
{
116+
set_keep_aspect_ratio(true);
112117
set_raw_image(m_thumbnail_info.get());
113118
m_is_compiled = false;
114119
return;
115120
}
116121

117122
if (m_thumbnail_id != image_resource_id::none && m_thumbnail_id != image_resource_ref)
118123
{
124+
set_keep_aspect_ratio(false);
119125
set_image_resource(m_thumbnail_id);
120126
m_is_compiled = false;
121127
return;

0 commit comments

Comments
 (0)