framebuffer_layout.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright 2016 Citra Emulator Project
  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/framebuffer_layout.h"
  7. #include "video_core/video_core.h"
  8. namespace Layout {
  9. static const float TOP_SCREEN_ASPECT_RATIO =
  10. static_cast<float>(VideoCore::kScreenTopHeight) / VideoCore::kScreenTopWidth;
  11. static const float BOT_SCREEN_ASPECT_RATIO =
  12. static_cast<float>(VideoCore::kScreenBottomHeight) / VideoCore::kScreenBottomWidth;
  13. // Finds the largest size subrectangle contained in window area that is confined to the aspect ratio
  14. template <class T>
  15. static MathUtil::Rectangle<T> maxRectangle(MathUtil::Rectangle<T> window_area,
  16. float screen_aspect_ratio) {
  17. float scale = std::min(static_cast<float>(window_area.GetWidth()),
  18. window_area.GetHeight() / screen_aspect_ratio);
  19. return MathUtil::Rectangle<T>{0, 0, static_cast<T>(std::round(scale)),
  20. static_cast<T>(std::round(scale * screen_aspect_ratio))};
  21. }
  22. FramebufferLayout DefaultFrameLayout(unsigned width, unsigned height, bool swapped) {
  23. ASSERT(width > 0);
  24. ASSERT(height > 0);
  25. FramebufferLayout res{width, height, true, true, {}, {}};
  26. // Default layout gives equal screen sizes to the top and bottom screen
  27. MathUtil::Rectangle<unsigned> screen_window_area{0, 0, width, height / 2};
  28. MathUtil::Rectangle<unsigned> top_screen =
  29. maxRectangle(screen_window_area, TOP_SCREEN_ASPECT_RATIO);
  30. MathUtil::Rectangle<unsigned> bot_screen =
  31. maxRectangle(screen_window_area, BOT_SCREEN_ASPECT_RATIO);
  32. float window_aspect_ratio = static_cast<float>(height) / width;
  33. // both screens height are taken into account by multiplying by 2
  34. float emulation_aspect_ratio = TOP_SCREEN_ASPECT_RATIO * 2;
  35. if (window_aspect_ratio < emulation_aspect_ratio) {
  36. // Apply borders to the left and right sides of the window.
  37. top_screen =
  38. top_screen.TranslateX((screen_window_area.GetWidth() - top_screen.GetWidth()) / 2);
  39. bot_screen =
  40. bot_screen.TranslateX((screen_window_area.GetWidth() - bot_screen.GetWidth()) / 2);
  41. } else {
  42. // Window is narrower than the emulation content => apply borders to the top and bottom
  43. // Recalculate the bottom screen to account for the width difference between top and bottom
  44. screen_window_area = {0, 0, width, top_screen.GetHeight()};
  45. bot_screen = maxRectangle(screen_window_area, BOT_SCREEN_ASPECT_RATIO);
  46. bot_screen = bot_screen.TranslateX((top_screen.GetWidth() - bot_screen.GetWidth()) / 2);
  47. if (swapped) {
  48. bot_screen = bot_screen.TranslateY(height / 2 - bot_screen.GetHeight());
  49. } else {
  50. top_screen = top_screen.TranslateY(height / 2 - top_screen.GetHeight());
  51. }
  52. }
  53. // Move the top screen to the bottom if we are swapped.
  54. res.top_screen = swapped ? top_screen.TranslateY(height / 2) : top_screen;
  55. res.bottom_screen = swapped ? bot_screen : bot_screen.TranslateY(height / 2);
  56. return res;
  57. }
  58. FramebufferLayout SingleFrameLayout(unsigned width, unsigned height, bool swapped) {
  59. ASSERT(width > 0);
  60. ASSERT(height > 0);
  61. // The drawing code needs at least somewhat valid values for both screens
  62. // so just calculate them both even if the other isn't showing.
  63. FramebufferLayout res{width, height, !swapped, swapped, {}, {}};
  64. MathUtil::Rectangle<unsigned> screen_window_area{0, 0, width, height};
  65. MathUtil::Rectangle<unsigned> top_screen =
  66. maxRectangle(screen_window_area, TOP_SCREEN_ASPECT_RATIO);
  67. MathUtil::Rectangle<unsigned> bot_screen =
  68. maxRectangle(screen_window_area, BOT_SCREEN_ASPECT_RATIO);
  69. float window_aspect_ratio = static_cast<float>(height) / width;
  70. float emulation_aspect_ratio = (swapped) ? BOT_SCREEN_ASPECT_RATIO : TOP_SCREEN_ASPECT_RATIO;
  71. if (window_aspect_ratio < emulation_aspect_ratio) {
  72. top_screen =
  73. top_screen.TranslateX((screen_window_area.GetWidth() - top_screen.GetWidth()) / 2);
  74. bot_screen =
  75. bot_screen.TranslateX((screen_window_area.GetWidth() - bot_screen.GetWidth()) / 2);
  76. } else {
  77. top_screen = top_screen.TranslateY((height - top_screen.GetHeight()) / 2);
  78. bot_screen = bot_screen.TranslateY((height - bot_screen.GetHeight()) / 2);
  79. }
  80. res.top_screen = top_screen;
  81. res.bottom_screen = bot_screen;
  82. return res;
  83. }
  84. FramebufferLayout LargeFrameLayout(unsigned width, unsigned height, bool swapped) {
  85. ASSERT(width > 0);
  86. ASSERT(height > 0);
  87. FramebufferLayout res{width, height, true, true, {}, {}};
  88. // Split the window into two parts. Give 4x width to the main screen and 1x width to the small
  89. // To do that, find the total emulation box and maximize that based on window size
  90. float window_aspect_ratio = static_cast<float>(height) / width;
  91. float emulation_aspect_ratio =
  92. swapped
  93. ? VideoCore::kScreenBottomHeight * 4 /
  94. (VideoCore::kScreenBottomWidth * 4.0f + VideoCore::kScreenTopWidth)
  95. : VideoCore::kScreenTopHeight * 4 /
  96. (VideoCore::kScreenTopWidth * 4.0f + VideoCore::kScreenBottomWidth);
  97. float large_screen_aspect_ratio = swapped ? BOT_SCREEN_ASPECT_RATIO : TOP_SCREEN_ASPECT_RATIO;
  98. float small_screen_aspect_ratio = swapped ? TOP_SCREEN_ASPECT_RATIO : BOT_SCREEN_ASPECT_RATIO;
  99. MathUtil::Rectangle<unsigned> screen_window_area{0, 0, width, height};
  100. MathUtil::Rectangle<unsigned> total_rect =
  101. maxRectangle(screen_window_area, emulation_aspect_ratio);
  102. MathUtil::Rectangle<unsigned> large_screen =
  103. maxRectangle(total_rect, large_screen_aspect_ratio);
  104. MathUtil::Rectangle<unsigned> fourth_size_rect = total_rect.Scale(.25f);
  105. MathUtil::Rectangle<unsigned> small_screen =
  106. maxRectangle(fourth_size_rect, small_screen_aspect_ratio);
  107. if (window_aspect_ratio < emulation_aspect_ratio) {
  108. large_screen =
  109. large_screen.TranslateX((screen_window_area.GetWidth() - total_rect.GetWidth()) / 2);
  110. } else {
  111. large_screen = large_screen.TranslateY((height - total_rect.GetHeight()) / 2);
  112. }
  113. // Shift the small screen to the bottom right corner
  114. small_screen =
  115. small_screen.TranslateX(large_screen.right)
  116. .TranslateY(large_screen.GetHeight() + large_screen.top - small_screen.GetHeight());
  117. res.top_screen = swapped ? small_screen : large_screen;
  118. res.bottom_screen = swapped ? large_screen : small_screen;
  119. return res;
  120. }
  121. }