framebuffer_layout.cpp 7.3 KB

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