framebuffer_layout.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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{width, height, false, {}};
  24. const float window_aspect_ratio = static_cast<float>(height) / static_cast<float>(width);
  25. const float emulation_aspect_ratio = EmulationAspectRatio(
  26. static_cast<AspectRatio>(Settings::values.aspect_ratio.GetValue()), window_aspect_ratio);
  27. const Common::Rectangle<u32> screen_window_area{0, 0, width, height};
  28. Common::Rectangle<u32> screen = MaxRectangle(screen_window_area, emulation_aspect_ratio);
  29. if (window_aspect_ratio < emulation_aspect_ratio) {
  30. screen = screen.TranslateX((screen_window_area.GetWidth() - screen.GetWidth()) / 2);
  31. } else {
  32. screen = screen.TranslateY((height - screen.GetHeight()) / 2);
  33. }
  34. res.screen = screen;
  35. return res;
  36. }
  37. FramebufferLayout FrameLayoutFromResolutionScale(f32 res_scale) {
  38. const bool is_docked = Settings::values.use_docked_mode.GetValue();
  39. const u32 screen_width = is_docked ? ScreenDocked::Width : ScreenUndocked::Width;
  40. const u32 screen_height = is_docked ? ScreenDocked::Height : ScreenUndocked::Height;
  41. const u32 width = static_cast<u32>(static_cast<f32>(screen_width) * res_scale);
  42. const u32 height = static_cast<u32>(static_cast<f32>(screen_height) * res_scale);
  43. return DefaultFrameLayout(width, height);
  44. }
  45. float EmulationAspectRatio(AspectRatio aspect, float window_aspect_ratio) {
  46. switch (aspect) {
  47. case AspectRatio::Default:
  48. return static_cast<float>(ScreenUndocked::Height) / ScreenUndocked::Width;
  49. case AspectRatio::R4_3:
  50. return 3.0f / 4.0f;
  51. case AspectRatio::R21_9:
  52. return 9.0f / 21.0f;
  53. case AspectRatio::StretchToWindow:
  54. return window_aspect_ratio;
  55. default:
  56. return static_cast<float>(ScreenUndocked::Height) / ScreenUndocked::Width;
  57. }
  58. }
  59. } // namespace Layout