frame_dumper: Use jthread (#6855)

This commit is contained in:
GPUCode 2023-08-08 04:21:28 +03:00 committed by GitHub
parent ac78b74c45
commit f76165d848
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 16 deletions

View file

@ -5,6 +5,9 @@
#include <glad/glad.h> #include <glad/glad.h>
#include <utility> #include <utility>
#include "core/core.h"
#include "core/dumping/backend.h"
#include "core/frontend/emu_window.h" #include "core/frontend/emu_window.h"
#include "video_core/renderer_opengl/frame_dumper_opengl.h" #include "video_core/renderer_opengl/frame_dumper_opengl.h"
#include "video_core/renderer_opengl/gl_texture_mailbox.h" #include "video_core/renderer_opengl/gl_texture_mailbox.h"
@ -12,13 +15,9 @@
namespace OpenGL { namespace OpenGL {
FrameDumperOpenGL::FrameDumperOpenGL(Core::System& system_, Frontend::EmuWindow& emu_window) FrameDumperOpenGL::FrameDumperOpenGL(Core::System& system_, Frontend::EmuWindow& emu_window)
: system(system_), context(emu_window.CreateSharedContext()) {} : system{system_}, context{emu_window.CreateSharedContext()} {}
FrameDumperOpenGL::~FrameDumperOpenGL() { FrameDumperOpenGL::~FrameDumperOpenGL() = default;
if (present_thread.joinable()) {
present_thread.join();
}
}
bool FrameDumperOpenGL::IsDumping() const { bool FrameDumperOpenGL::IsDumping() const {
auto video_dumper = system.GetVideoDumper(); auto video_dumper = system.GetVideoDumper();
@ -35,19 +34,19 @@ void FrameDumperOpenGL::StartDumping() {
present_thread.join(); present_thread.join();
} }
present_thread = std::thread(&FrameDumperOpenGL::PresentLoop, this); present_thread = std::jthread([this](std::stop_token stop_token) { PresentLoop(stop_token); });
} }
void FrameDumperOpenGL::StopDumping() { void FrameDumperOpenGL::StopDumping() {
stop_requested.store(true, std::memory_order_relaxed); present_thread.request_stop();
} }
void FrameDumperOpenGL::PresentLoop() { void FrameDumperOpenGL::PresentLoop(std::stop_token stop_token) {
const auto scope = context->Acquire(); const auto scope = context->Acquire();
InitializeOpenGLObjects(); InitializeOpenGLObjects();
const auto& layout = GetLayout(); const auto& layout = GetLayout();
while (!stop_requested.exchange(false)) { while (!stop_token.stop_requested()) {
auto frame = mailbox->TryGetPresentFrame(200); auto frame = mailbox->TryGetPresentFrame(200);
if (!frame) { if (!frame) {
continue; continue;

View file

@ -6,9 +6,8 @@
#include <atomic> #include <atomic>
#include <memory> #include <memory>
#include <thread>
#include "core/core.h" #include "common/polyfill_thread.h"
#include "core/dumping/backend.h"
#include "core/frontend/framebuffer_layout.h" #include "core/frontend/framebuffer_layout.h"
#include "video_core/renderer_opengl/gl_resource_manager.h" #include "video_core/renderer_opengl/gl_resource_manager.h"
@ -18,6 +17,10 @@ class GraphicsContext;
class TextureMailbox; class TextureMailbox;
} // namespace Frontend } // namespace Frontend
namespace Core {
class System;
}
namespace OpenGL { namespace OpenGL {
class RendererOpenGL; class RendererOpenGL;
@ -42,12 +45,12 @@ public:
private: private:
void InitializeOpenGLObjects(); void InitializeOpenGLObjects();
void CleanupOpenGLObjects(); void CleanupOpenGLObjects();
void PresentLoop(); void PresentLoop(std::stop_token stop_token);
private:
Core::System& system; Core::System& system;
std::unique_ptr<Frontend::GraphicsContext> context; std::unique_ptr<Frontend::GraphicsContext> context;
std::thread present_thread; std::jthread present_thread;
std::atomic_bool stop_requested{false};
// PBOs used to dump frames faster // PBOs used to dump frames faster
std::array<OGLBuffer, 2> pbos; std::array<OGLBuffer, 2> pbos;