Merge pull request #4976 from comex/poll-events

Overhaul EmuWindow::PollEvents to fix yuzu-cmd calling SDL_PollEvents off main thread
master
Rodrigo Locatti 2020-11-25 20:44:53 +07:00 committed by GitHub
commit 0e15c68f54
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 68 additions and 73 deletions

@ -102,8 +102,8 @@ public:
float render_surface_scale = 1.0f; float render_surface_scale = 1.0f;
}; };
/// Polls window events /// Called from GPU thread when a frame is displayed.
virtual void PollEvents() = 0; virtual void OnFrameDisplayed() {}
/** /**
* Returns a GraphicsContext that the frontend provides to be used for rendering. * Returns a GraphicsContext that the frontend provides to be used for rendering.

@ -151,8 +151,8 @@ void RendererOpenGL::SwapBuffers(const Tegra::FramebufferConfig* framebuffer) {
rasterizer->TickFrame(); rasterizer->TickFrame();
render_window.PollEvents();
context->SwapBuffers(); context->SwapBuffers();
render_window.OnFrameDisplayed();
} }
void RendererOpenGL::PrepareRendertarget(const Tegra::FramebufferConfig* framebuffer) { void RendererOpenGL::PrepareRendertarget(const Tegra::FramebufferConfig* framebuffer) {

@ -252,8 +252,6 @@ RendererVulkan::~RendererVulkan() {
} }
void RendererVulkan::SwapBuffers(const Tegra::FramebufferConfig* framebuffer) { void RendererVulkan::SwapBuffers(const Tegra::FramebufferConfig* framebuffer) {
render_window.PollEvents();
if (!framebuffer) { if (!framebuffer) {
return; return;
} }
@ -283,7 +281,7 @@ void RendererVulkan::SwapBuffers(const Tegra::FramebufferConfig* framebuffer) {
rasterizer->TickFrame(); rasterizer->TickFrame();
} }
render_window.PollEvents(); render_window.OnFrameDisplayed();
} }
bool RendererVulkan::Init() { bool RendererVulkan::Init() {

@ -314,7 +314,7 @@ GRenderWindow::~GRenderWindow() {
input_subsystem->Shutdown(); input_subsystem->Shutdown();
} }
void GRenderWindow::PollEvents() { void GRenderWindow::OnFrameDisplayed() {
if (!first_frame) { if (!first_frame) {
first_frame = true; first_frame = true;
emit FirstFrameDisplayed(); emit FirstFrameDisplayed();

@ -131,7 +131,7 @@ public:
~GRenderWindow() override; ~GRenderWindow() override;
// EmuWindow implementation. // EmuWindow implementation.
void PollEvents() override; void OnFrameDisplayed() override;
bool IsShown() const override; bool IsShown() const override;
std::unique_ptr<Core::Frontend::GraphicsContext> CreateSharedContext() const override; std::unique_ptr<Core::Frontend::GraphicsContext> CreateSharedContext() const override;

@ -121,11 +121,15 @@ void EmuWindow_SDL2::Fullscreen() {
SDL_MaximizeWindow(render_window); SDL_MaximizeWindow(render_window);
} }
void EmuWindow_SDL2::PollEvents() { void EmuWindow_SDL2::WaitEvent() {
// Called on main thread
SDL_Event event; SDL_Event event;
// SDL_PollEvent returns 0 when there are no more events in the event queue if (!SDL_WaitEvent(&event)) {
while (SDL_PollEvent(&event)) { LOG_CRITICAL(Frontend, "SDL_WaitEvent failed: {}", SDL_GetError());
exit(1);
}
switch (event.type) { switch (event.type) {
case SDL_WINDOWEVENT: case SDL_WINDOWEVENT:
switch (event.window.event) { switch (event.window.event) {
@ -158,8 +162,7 @@ void EmuWindow_SDL2::PollEvents() {
case SDL_MOUSEBUTTONUP: case SDL_MOUSEBUTTONUP:
// ignore if it came from touch // ignore if it came from touch
if (event.button.which != SDL_TOUCH_MOUSEID) { if (event.button.which != SDL_TOUCH_MOUSEID) {
OnMouseButton(event.button.button, event.button.state, event.button.x, OnMouseButton(event.button.button, event.button.state, event.button.x, event.button.y);
event.button.y);
} }
break; break;
case SDL_FINGERDOWN: case SDL_FINGERDOWN:
@ -177,7 +180,6 @@ void EmuWindow_SDL2::PollEvents() {
default: default:
break; break;
} }
}
const u32 current_time = SDL_GetTicks(); const u32 current_time = SDL_GetTicks();
if (current_time > last_time + 2000) { if (current_time > last_time + 2000) {

@ -23,38 +23,38 @@ public:
explicit EmuWindow_SDL2(InputCommon::InputSubsystem* input_subsystem); explicit EmuWindow_SDL2(InputCommon::InputSubsystem* input_subsystem);
~EmuWindow_SDL2(); ~EmuWindow_SDL2();
/// Polls window events
void PollEvents() override;
/// Whether the window is still open, and a close request hasn't yet been sent /// Whether the window is still open, and a close request hasn't yet been sent
bool IsOpen() const; bool IsOpen() const;
/// Returns if window is shown (not minimized) /// Returns if window is shown (not minimized)
bool IsShown() const override; bool IsShown() const override;
/// Wait for the next event on the main thread.
void WaitEvent();
protected: protected:
/// Called by PollEvents when a key is pressed or released. /// Called by WaitEvent when a key is pressed or released.
void OnKeyEvent(int key, u8 state); void OnKeyEvent(int key, u8 state);
/// Called by PollEvents when the mouse moves. /// Called by WaitEvent when the mouse moves.
void OnMouseMotion(s32 x, s32 y); void OnMouseMotion(s32 x, s32 y);
/// Called by PollEvents when a mouse button is pressed or released /// Called by WaitEvent when a mouse button is pressed or released
void OnMouseButton(u32 button, u8 state, s32 x, s32 y); void OnMouseButton(u32 button, u8 state, s32 x, s32 y);
/// Translates pixel position (0..1) to pixel positions /// Translates pixel position (0..1) to pixel positions
std::pair<unsigned, unsigned> TouchToPixelPos(float touch_x, float touch_y) const; std::pair<unsigned, unsigned> TouchToPixelPos(float touch_x, float touch_y) const;
/// Called by PollEvents when a finger starts touching the touchscreen /// Called by WaitEvent when a finger starts touching the touchscreen
void OnFingerDown(float x, float y); void OnFingerDown(float x, float y);
/// Called by PollEvents when a finger moves while touching the touchscreen /// Called by WaitEvent when a finger moves while touching the touchscreen
void OnFingerMotion(float x, float y); void OnFingerMotion(float x, float y);
/// Called by PollEvents when a finger stops touching the touchscreen /// Called by WaitEvent when a finger stops touching the touchscreen
void OnFingerUp(); void OnFingerUp();
/// Called by PollEvents when any event that may cause the window to be resized occurs /// Called by WaitEvent when any event that may cause the window to be resized occurs
void OnResize(); void OnResize();
/// Called when user passes the fullscreen parameter flag /// Called when user passes the fullscreen parameter flag

@ -242,7 +242,7 @@ int main(int argc, char** argv) {
void(system.Run()); void(system.Run());
while (emu_window->IsOpen()) { while (emu_window->IsOpen()) {
std::this_thread::sleep_for(std::chrono::milliseconds(1)); emu_window->WaitEvent();
} }
void(system.Pause()); void(system.Pause());
system.Shutdown(); system.Shutdown();

@ -109,8 +109,6 @@ EmuWindow_SDL2_Hide::~EmuWindow_SDL2_Hide() {
SDL_Quit(); SDL_Quit();
} }
void EmuWindow_SDL2_Hide::PollEvents() {}
bool EmuWindow_SDL2_Hide::IsShown() const { bool EmuWindow_SDL2_Hide::IsShown() const {
return false; return false;
} }

@ -17,9 +17,6 @@ public:
explicit EmuWindow_SDL2_Hide(); explicit EmuWindow_SDL2_Hide();
~EmuWindow_SDL2_Hide(); ~EmuWindow_SDL2_Hide();
/// Polls window events
void PollEvents() override;
/// Whether the screen is being shown or not. /// Whether the screen is being shown or not.
bool IsShown() const override; bool IsShown() const override;