framebuffer_layout.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 "core/frontend/framebuffer_layout.h"
  7. #include "core/settings.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. float scale = std::min(static_cast<float>(window_area.GetWidth()),
  14. 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};
  24. const float window_aspect_ratio = static_cast<float>(height) / width;
  25. const float emulation_aspect_ratio = EmulationAspectRatio(
  26. static_cast<AspectRatio>(Settings::values.aspect_ratio), 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(u32 res_scale) {
  38. u32 width, height;
  39. if (Settings::values.use_docked_mode) {
  40. width = ScreenDocked::WidthDocked * res_scale;
  41. height = ScreenDocked::HeightDocked * res_scale;
  42. } else {
  43. width = ScreenUndocked::Width * res_scale;
  44. height = ScreenUndocked::Height * res_scale;
  45. }
  46. return DefaultFrameLayout(width, height);
  47. }
  48. float EmulationAspectRatio(AspectRatio aspect, float window_aspect_ratio) {
  49. switch (aspect) {
  50. case AspectRatio::Default:
  51. return static_cast<float>(ScreenUndocked::Height) / ScreenUndocked::Width;
  52. case AspectRatio::R4_3:
  53. return 3.0f / 4.0f;
  54. case AspectRatio::R21_9:
  55. return 9.0f / 21.0f;
  56. case AspectRatio::StretchToWindow:
  57. return window_aspect_ratio;
  58. default:
  59. return static_cast<float>(ScreenUndocked::Height) / ScreenUndocked::Width;
  60. }
  61. }
  62. } // namespace Layout