framebuffer_layout.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. float emulation_aspect_ratio = EmulationAspectRatio(
  26. static_cast<Aspect>(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(Aspect aspect, float window_aspect_ratio) {
  49. switch (aspect) {
  50. case Aspect::Default:
  51. return static_cast<float>(ScreenUndocked::Height) / ScreenUndocked::Width;
  52. case Aspect::Aspect21by9:
  53. return 9.0f / 21.0f;
  54. case Aspect::StretchToWindow:
  55. return window_aspect_ratio;
  56. default:
  57. return static_cast<float>(ScreenUndocked::Height) / ScreenUndocked::Width;
  58. }
  59. }
  60. } // namespace Layout