framebuffer_layout.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <cmath>
  5. #include "common/assert.h"
  6. #include "common/settings.h"
  7. #include "core/frontend/framebuffer_layout.h"
  8. namespace Layout {
  9. // Finds the largest size subrectangle contained in window area that is confined to the aspect ratio
  10. template <class T>
  11. static Common::Rectangle<T> MaxRectangle(Common::Rectangle<T> window_area,
  12. float screen_aspect_ratio) {
  13. const float scale = std::min(static_cast<float>(window_area.GetWidth()),
  14. static_cast<float>(window_area.GetHeight()) / screen_aspect_ratio);
  15. return Common::Rectangle<T>{0, 0, static_cast<T>(std::round(scale)),
  16. static_cast<T>(std::round(scale * screen_aspect_ratio))};
  17. }
  18. FramebufferLayout DefaultFrameLayout(u32 width, u32 height) {
  19. ASSERT(width > 0);
  20. ASSERT(height > 0);
  21. // The drawing code needs at least somewhat valid values for both screens
  22. // so just calculate them both even if the other isn't showing.
  23. FramebufferLayout res{
  24. .width = width,
  25. .height = height,
  26. .screen = {},
  27. .is_srgb = false,
  28. };
  29. const float window_aspect_ratio = static_cast<float>(height) / static_cast<float>(width);
  30. const float emulation_aspect_ratio = EmulationAspectRatio(
  31. static_cast<AspectRatio>(Settings::values.aspect_ratio.GetValue()), window_aspect_ratio);
  32. const Common::Rectangle<u32> screen_window_area{0, 0, width, height};
  33. Common::Rectangle<u32> screen = MaxRectangle(screen_window_area, emulation_aspect_ratio);
  34. if (window_aspect_ratio < emulation_aspect_ratio) {
  35. screen = screen.TranslateX((screen_window_area.GetWidth() - screen.GetWidth()) / 2);
  36. } else {
  37. screen = screen.TranslateY((height - screen.GetHeight()) / 2);
  38. }
  39. res.screen = screen;
  40. return res;
  41. }
  42. FramebufferLayout FrameLayoutFromResolutionScale(f32 res_scale) {
  43. const bool is_docked = Settings::values.use_docked_mode.GetValue();
  44. const u32 screen_width = is_docked ? ScreenDocked::Width : ScreenUndocked::Width;
  45. const u32 screen_height = is_docked ? ScreenDocked::Height : ScreenUndocked::Height;
  46. const u32 width = static_cast<u32>(static_cast<f32>(screen_width) * res_scale);
  47. const u32 height = static_cast<u32>(static_cast<f32>(screen_height) * res_scale);
  48. return DefaultFrameLayout(width, height);
  49. }
  50. float EmulationAspectRatio(AspectRatio aspect, float window_aspect_ratio) {
  51. switch (aspect) {
  52. case AspectRatio::Default:
  53. return static_cast<float>(ScreenUndocked::Height) / ScreenUndocked::Width;
  54. case AspectRatio::R4_3:
  55. return 3.0f / 4.0f;
  56. case AspectRatio::R21_9:
  57. return 9.0f / 21.0f;
  58. case AspectRatio::StretchToWindow:
  59. return window_aspect_ratio;
  60. default:
  61. return static_cast<float>(ScreenUndocked::Height) / ScreenUndocked::Width;
  62. }
  63. }
  64. } // namespace Layout