summaryrefslogtreecommitdiff
path: root/src/common/framebuffer_layout.cpp
diff options
context:
space:
mode:
authorGravatar bunnei2016-11-15 19:57:08 -0500
committerGravatar GitHub2016-11-15 19:57:08 -0500
commit5a31552764dc8970253e642f4b829a8b785375c6 (patch)
tree8186a82c37ae04c17bd1d6250c524fc097f9671c /src/common/framebuffer_layout.cpp
parentMerge pull request #2171 from jroweboy/fix-mac-build (diff)
parentRound the rectangle size to prevent float to int casting issues (diff)
downloadyuzu-5a31552764dc8970253e642f4b829a8b785375c6.tar.gz
yuzu-5a31552764dc8970253e642f4b829a8b785375c6.tar.xz
yuzu-5a31552764dc8970253e642f4b829a8b785375c6.zip
Merge pull request #1753 from jroweboy/frame_layouts
Support additional screen layouts.
Diffstat (limited to 'src/common/framebuffer_layout.cpp')
-rw-r--r--src/common/framebuffer_layout.cpp138
1 files changed, 138 insertions, 0 deletions
diff --git a/src/common/framebuffer_layout.cpp b/src/common/framebuffer_layout.cpp
new file mode 100644
index 000000000..46c008d9c
--- /dev/null
+++ b/src/common/framebuffer_layout.cpp
@@ -0,0 +1,138 @@
1// Copyright 2016 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include <cmath>
6
7#include "common/assert.h"
8#include "common/framebuffer_layout.h"
9#include "video_core/video_core.h"
10
11namespace Layout {
12
13static const float TOP_SCREEN_ASPECT_RATIO =
14 static_cast<float>(VideoCore::kScreenTopHeight) / VideoCore::kScreenTopWidth;
15static const float BOT_SCREEN_ASPECT_RATIO =
16 static_cast<float>(VideoCore::kScreenBottomHeight) / VideoCore::kScreenBottomWidth;
17
18// Finds the largest size subrectangle contained in window area that is confined to the aspect ratio
19template <class T>
20static MathUtil::Rectangle<T> maxRectangle(MathUtil::Rectangle<T> window_area,
21 float screen_aspect_ratio) {
22 float scale = std::min(static_cast<float>(window_area.GetWidth()),
23 window_area.GetHeight() / screen_aspect_ratio);
24 return MathUtil::Rectangle<T>{0, 0, static_cast<T>(std::round(scale)),
25 static_cast<T>(std::round(scale * screen_aspect_ratio))};
26}
27
28FramebufferLayout DefaultFrameLayout(unsigned width, unsigned height, bool swapped) {
29 ASSERT(width > 0);
30 ASSERT(height > 0);
31
32 FramebufferLayout res{width, height, true, true, {}, {}};
33 // Default layout gives equal screen sizes to the top and bottom screen
34 MathUtil::Rectangle<unsigned> screen_window_area{0, 0, width, height / 2};
35 MathUtil::Rectangle<unsigned> top_screen =
36 maxRectangle(screen_window_area, TOP_SCREEN_ASPECT_RATIO);
37 MathUtil::Rectangle<unsigned> bot_screen =
38 maxRectangle(screen_window_area, BOT_SCREEN_ASPECT_RATIO);
39
40 float window_aspect_ratio = static_cast<float>(height) / width;
41 // both screens height are taken into account by multiplying by 2
42 float emulation_aspect_ratio = TOP_SCREEN_ASPECT_RATIO * 2;
43
44 if (window_aspect_ratio < emulation_aspect_ratio) {
45 // Apply borders to the left and right sides of the window.
46 top_screen =
47 top_screen.TranslateX((screen_window_area.GetWidth() - top_screen.GetWidth()) / 2);
48 bot_screen =
49 bot_screen.TranslateX((screen_window_area.GetWidth() - bot_screen.GetWidth()) / 2);
50 } else {
51 // Window is narrower than the emulation content => apply borders to the top and bottom
52 // Recalculate the bottom screen to account for the width difference between top and bottom
53 screen_window_area = {0, 0, width, top_screen.GetHeight()};
54 bot_screen = maxRectangle(screen_window_area, BOT_SCREEN_ASPECT_RATIO);
55 bot_screen = bot_screen.TranslateX((top_screen.GetWidth() - bot_screen.GetWidth()) / 2);
56 if (swapped) {
57 bot_screen = bot_screen.TranslateY(height / 2 - bot_screen.GetHeight());
58 } else {
59 top_screen = top_screen.TranslateY(height / 2 - top_screen.GetHeight());
60 }
61 }
62 // Move the top screen to the bottom if we are swapped.
63 res.top_screen = swapped ? top_screen.TranslateY(height / 2) : top_screen;
64 res.bottom_screen = swapped ? bot_screen : bot_screen.TranslateY(height / 2);
65 return res;
66}
67
68FramebufferLayout SingleFrameLayout(unsigned width, unsigned height, bool swapped) {
69 ASSERT(width > 0);
70 ASSERT(height > 0);
71 // The drawing code needs at least somewhat valid values for both screens
72 // so just calculate them both even if the other isn't showing.
73 FramebufferLayout res{width, height, !swapped, swapped, {}, {}};
74
75 MathUtil::Rectangle<unsigned> screen_window_area{0, 0, width, height};
76 MathUtil::Rectangle<unsigned> top_screen =
77 maxRectangle(screen_window_area, TOP_SCREEN_ASPECT_RATIO);
78 MathUtil::Rectangle<unsigned> bot_screen =
79 maxRectangle(screen_window_area, BOT_SCREEN_ASPECT_RATIO);
80
81 float window_aspect_ratio = static_cast<float>(height) / width;
82 float emulation_aspect_ratio = (swapped) ? BOT_SCREEN_ASPECT_RATIO : TOP_SCREEN_ASPECT_RATIO;
83
84 if (window_aspect_ratio < emulation_aspect_ratio) {
85 top_screen =
86 top_screen.TranslateX((screen_window_area.GetWidth() - top_screen.GetWidth()) / 2);
87 bot_screen =
88 bot_screen.TranslateX((screen_window_area.GetWidth() - bot_screen.GetWidth()) / 2);
89 } else {
90 top_screen = top_screen.TranslateY((height - top_screen.GetHeight()) / 2);
91 bot_screen = bot_screen.TranslateY((height - bot_screen.GetHeight()) / 2);
92 }
93 res.top_screen = top_screen;
94 res.bottom_screen = bot_screen;
95 return res;
96}
97
98FramebufferLayout LargeFrameLayout(unsigned width, unsigned height, bool swapped) {
99 ASSERT(width > 0);
100 ASSERT(height > 0);
101
102 FramebufferLayout res{width, height, true, true, {}, {}};
103 // Split the window into two parts. Give 4x width to the main screen and 1x width to the small
104 // To do that, find the total emulation box and maximize that based on window size
105 float window_aspect_ratio = static_cast<float>(height) / width;
106 float emulation_aspect_ratio =
107 swapped
108 ? VideoCore::kScreenBottomHeight * 4 /
109 (VideoCore::kScreenBottomWidth * 4.0f + VideoCore::kScreenTopWidth)
110 : VideoCore::kScreenTopHeight * 4 /
111 (VideoCore::kScreenTopWidth * 4.0f + VideoCore::kScreenBottomWidth);
112 float large_screen_aspect_ratio = swapped ? BOT_SCREEN_ASPECT_RATIO : TOP_SCREEN_ASPECT_RATIO;
113 float small_screen_aspect_ratio = swapped ? TOP_SCREEN_ASPECT_RATIO : BOT_SCREEN_ASPECT_RATIO;
114
115 MathUtil::Rectangle<unsigned> screen_window_area{0, 0, width, height};
116 MathUtil::Rectangle<unsigned> total_rect =
117 maxRectangle(screen_window_area, emulation_aspect_ratio);
118 MathUtil::Rectangle<unsigned> large_screen =
119 maxRectangle(total_rect, large_screen_aspect_ratio);
120 MathUtil::Rectangle<unsigned> fourth_size_rect = total_rect.Scale(.25f);
121 MathUtil::Rectangle<unsigned> small_screen =
122 maxRectangle(fourth_size_rect, small_screen_aspect_ratio);
123
124 if (window_aspect_ratio < emulation_aspect_ratio) {
125 large_screen =
126 large_screen.TranslateX((screen_window_area.GetWidth() - total_rect.GetWidth()) / 2);
127 } else {
128 large_screen = large_screen.TranslateY((height - total_rect.GetHeight()) / 2);
129 }
130 // Shift the small screen to the bottom right corner
131 small_screen =
132 small_screen.TranslateX(large_screen.right)
133 .TranslateY(large_screen.GetHeight() + large_screen.top - small_screen.GetHeight());
134 res.top_screen = swapped ? small_screen : large_screen;
135 res.bottom_screen = swapped ? large_screen : small_screen;
136 return res;
137}
138}