libretro_window.cpp 6.26 KB
Newer Older
James's avatar
James committed
1
2
3
4
5
6
// Copyright 2017 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

#include <glad/glad.h>

7
8
#include "citra_libretro/citra_libretro.h"
#include "citra_libretro/environment.h"
9
10
11
12
#include "citra_libretro/input/input_factory.h"
#include "core/3ds.h"
#include "core/settings.h"
#include "libretro.h"
James's avatar
James committed
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

/// LibRetro expects a "default" GL state.
void ResetGLState() {
    // Clean up state.
    glLogicOp(GL_COPY);

    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);
    glDepthMask(GL_TRUE);

    glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);

    glDisable(GL_STENCIL_TEST);
    glStencilFunc(GL_ALWAYS, 0, 0xFFFFFFFF);

    glEnable(GL_BLEND);
    glBlendFunc(GL_ONE, GL_ZERO);
    glBlendEquation(GL_FUNC_ADD);
    glBlendFuncSeparate(GL_ONE, GL_ZERO, GL_ONE, GL_ZERO);
    glBlendColor(0, 0, 0, 0);

    glDisable(GL_COLOR_LOGIC_OP);

    glDisable(GL_DITHER);

    glDisable(GL_CULL_FACE);
    glCullFace(GL_BACK);

    glActiveTexture(GL_TEXTURE0);
}

EmuWindow_LibRetro::EmuWindow_LibRetro() {}

EmuWindow_LibRetro::~EmuWindow_LibRetro() {}

void EmuWindow_LibRetro::SwapBuffers() {
    submittedFrame = true;

    ResetGLState();
52

53
54
55
    if (enableEmulatedPointer) {
        tracker.Render(width, height);
    }
56

57
58
    LibRetro::UploadVideoFrame(RETRO_HW_FRAME_BUFFER_VALID, static_cast<unsigned>(width),
                               static_cast<unsigned>(height), 0);
James's avatar
James committed
59
60
    glUseProgram(0);
    ResetGLState();
61
62
63
64

    glBindFramebuffer(GL_FRAMEBUFFER, 0);
    glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
    glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
James's avatar
James committed
65
66
67
68
}

void EmuWindow_LibRetro::SetupFramebuffer() {
    ResetGLState();
69
70
71
72
73
74
75
76
77

    glBindFramebuffer(GL_DRAW_FRAMEBUFFER, framebuffer);

    // glClear can be a slow path - skip clearing if we don't need to.
    if (doCleanFrame) {
        glClear(GL_COLOR_BUFFER_BIT);

        doCleanFrame = false;
    }
James's avatar
James committed
78
79
80
81
}

void EmuWindow_LibRetro::PollEvents() {
    LibRetro::PollInput();
James's avatar
James committed
82

83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
    // TODO: Poll for right click for motion emu

    if (enableEmulatedPointer) {
        tracker.Update(width, height, GetFramebufferLayout().bottom_screen);

        if (tracker.IsPressed()) {
            auto mousePos = tracker.GetPressedPosition();

            if (hasTouched) {
                TouchMoved(mousePos.first, mousePos.second);
            } else {
                TouchPressed(mousePos.first, mousePos.second);
                hasTouched = true;
            }
        } else if (hasTouched) {
            hasTouched = false;
            TouchReleased();
James's avatar
James committed
100
101
        }
    }
James's avatar
James committed
102
103
104
105
106
107
108
109
110
111
}

void EmuWindow_LibRetro::MakeCurrent() {
    // They don't get any say in the matter - GL context is always current!
}

void EmuWindow_LibRetro::DoneCurrent() {
    // They don't get any say in the matter - GL context is always current!
}

112
113
void EmuWindow_LibRetro::OnMinimalClientAreaChangeRequest(
    const std::pair<unsigned, unsigned> &minimal_size) {
James's avatar
James committed
114
115
116
117
118
119
    width = minimal_size.first;
    height = minimal_size.second;
}

void EmuWindow_LibRetro::Prepare(bool hasOGL) {
    // TODO: Handle custom layouts
James's avatar
James committed
120
121
    unsigned baseX;
    unsigned baseY;
James's avatar
James committed
122
123
124
125
126

    float scaling = Settings::values.resolution_factor;

    bool swapped = Settings::values.swap_screen;

127
128
    enableEmulatedPointer = true;

James's avatar
James committed
129
    switch(Settings::values.layout_option) {
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
    case Settings::LayoutOption::SingleScreen:
        if (swapped) { // Bottom screen visible
            baseX = Core::kScreenBottomWidth;
            baseY = Core::kScreenBottomHeight;
        } else {  // Top screen visible
            baseX = Core::kScreenTopWidth;
            baseY = Core::kScreenTopHeight;
            enableEmulatedPointer = false;
        }
        baseX *= scaling;
        baseY *= scaling;
        break;
    case Settings::LayoutOption::LargeScreen:
        if (swapped) { // Bottom screen biggest
            baseX = Core::kScreenBottomWidth + Core::kScreenTopWidth / 4;
            baseY = Core::kScreenBottomHeight;
        } else { // Top screen biggest
            baseX = Core::kScreenTopWidth + Core::kScreenBottomWidth / 4;
            baseY = Core::kScreenTopHeight;
        }
James's avatar
James committed
150

151
152
153
154
155
156
        if (scaling < 4) {
            // Unfortunately, to get this aspect ratio correct (and have non-blurry 1x scaling),
            //  we have to have a pretty large buffer for the minimum ratio.
            baseX *= 4;
            baseY *= 4;
        } else {
James's avatar
James committed
157
158
            baseX *= scaling;
            baseY *= scaling;
159
160
        }
        break;
161
162
163
164
165
166
    case Settings::LayoutOption::SideScreen:
        baseX = Core::kScreenBottomWidth + Core::kScreenTopWidth;
        baseY = Core::kScreenTopHeight;
        baseX *= scaling;
        baseY *= scaling;
        break;
167
168
169
170
171
172
173
174
175
176
177
    case Settings::LayoutOption::Default:
    default:
        if (swapped) { // Bottom screen on top
            baseX = Core::kScreenBottomWidth;
        } else { // Top screen on top
            baseX = Core::kScreenTopWidth;
        }
        baseY = Core::kScreenTopHeight + Core::kScreenBottomHeight;
        baseX *= scaling;
        baseY *= scaling;
        break;
James's avatar
James committed
178
179
180
    }

    // Update Libretro with our status
181
    struct retro_system_av_info info{};
James's avatar
James committed
182
183
    info.timing.fps = 60.0;
    info.timing.sample_rate = 41000;
184
    info.geometry.aspect_ratio = (float)baseX / (float)baseY;
James's avatar
James committed
185
186
187
188
    info.geometry.base_width = baseX;
    info.geometry.base_height = baseY;
    info.geometry.max_width = baseX;
    info.geometry.max_height = baseY;
James's avatar
James committed
189
190
191
192
    if (!LibRetro::SetGeometry(&info)) {
        LOG_CRITICAL(Frontend, "Failed to update 3DS layout in frontend!");
    }

James's avatar
James committed
193
194
195
    NotifyClientAreaSizeChanged(std::pair<unsigned, unsigned>(baseX, baseY));
    OnMinimalClientAreaChangeRequest(std::pair<unsigned, unsigned>(baseX, baseY));
    UpdateCurrentFramebufferLayout(baseX, baseY);
James's avatar
James committed
196
197

    if (hasOGL) {
198
        framebuffer = static_cast<GLuint>(LibRetro::GetFramebuffer());
199

200
201
202
203
        if (enableEmulatedPointer) {
            // TODO: This is potentially leaking OpenGL resources!
            tracker.InitOpenGL();
        }
204

205
        doCleanFrame = true;
James's avatar
James committed
206
207
208
209
210
211
212
213
    }
}

bool EmuWindow_LibRetro::ShouldDeferRendererInit() const {
    // load_game doesn't always provide a GL context.
    return true;
}

214
215
216
217
218
bool EmuWindow_LibRetro::NeedsClearing() const {
    // We manage this ourselves.
    return false;
}

James's avatar
James committed
219
220
221
222
223
bool EmuWindow_LibRetro::HasSubmittedFrame() {
    bool state = submittedFrame;
    submittedFrame = false;
    return state;
}