summaryrefslogtreecommitdiffstats
path: root/src/core/frontend/framebuffer_layout.cpp
blob: 0832463d6ce3b6dec68020a0314303b1cde8440e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright 2018 yuzu emulator team
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

#include <cmath>

#include "common/assert.h"
#include "common/settings.h"
#include "core/frontend/framebuffer_layout.h"

namespace Layout {

// Finds the largest size subrectangle contained in window area that is confined to the aspect ratio
template <class T>
static Common::Rectangle<T> MaxRectangle(Common::Rectangle<T> window_area,
                                         float screen_aspect_ratio) {
    const float scale = std::min(static_cast<float>(window_area.GetWidth()),
                                 static_cast<float>(window_area.GetHeight()) / screen_aspect_ratio);
    return Common::Rectangle<T>{0, 0, static_cast<T>(std::round(scale)),
                                static_cast<T>(std::round(scale * screen_aspect_ratio))};
}

FramebufferLayout DefaultFrameLayout(u32 width, u32 height) {
    ASSERT(width > 0);
    ASSERT(height > 0);
    // The drawing code needs at least somewhat valid values for both screens
    // so just calculate them both even if the other isn't showing.
    FramebufferLayout res{width, height, false, {}};

    const float window_aspect_ratio = static_cast<float>(height) / static_cast<float>(width);
    const float emulation_aspect_ratio = EmulationAspectRatio(
        static_cast<AspectRatio>(Settings::values.aspect_ratio.GetValue()), window_aspect_ratio);

    const Common::Rectangle<u32> screen_window_area{0, 0, width, height};
    Common::Rectangle<u32> screen = MaxRectangle(screen_window_area, emulation_aspect_ratio);

    if (window_aspect_ratio < emulation_aspect_ratio) {
        screen = screen.TranslateX((screen_window_area.GetWidth() - screen.GetWidth()) / 2);
    } else {
        screen = screen.TranslateY((height - screen.GetHeight()) / 2);
    }

    res.screen = screen;
    return res;
}

FramebufferLayout FrameLayoutFromResolutionScale(u32 res_scale) {
    u32 width, height;

    if (Settings::values.use_docked_mode.GetValue()) {
        width = ScreenDocked::Width * res_scale;
        height = ScreenDocked::Height * res_scale;
    } else {
        width = ScreenUndocked::Width * res_scale;
        height = ScreenUndocked::Height * res_scale;
    }

    return DefaultFrameLayout(width, height);
}

float EmulationAspectRatio(AspectRatio aspect, float window_aspect_ratio) {
    switch (aspect) {
    case AspectRatio::Default:
        return static_cast<float>(ScreenUndocked::Height) / ScreenUndocked::Width;
    case AspectRatio::R4_3:
        return 3.0f / 4.0f;
    case AspectRatio::R21_9:
        return 9.0f / 21.0f;
    case AspectRatio::StretchToWindow:
        return window_aspect_ratio;
    default:
        return static_cast<float>(ScreenUndocked::Height) / ScreenUndocked::Width;
    }
}

} // namespace Layout