Split Linux platform target and move runtime/platform state

This commit is contained in:
2026-06-17 01:20:11 +02:00
parent 90a55b86fe
commit 3ce365fc15
16 changed files with 169 additions and 94 deletions

View File

@@ -155,16 +155,6 @@ public:
int idle_ms = 100;
pp::platform::PlatformServices* platform_services_ = nullptr;
#if defined(__IOS__) && defined(__OBJC__)
GameViewController* ios_view;
AppDelegate* ios_app;
#elif defined(__OSX__) && defined(__OBJC__)
View* osx_view;
AppOSX* osx_app;
#elif __LINUX__ || __WEB__
GLFWwindow* glfw_window;
#endif
#ifdef __ANDROID__
struct android_app* and_app;
struct engine* and_engine;

View File

@@ -15,6 +15,7 @@
#ifdef _WIN32
#include "platform_windows/windows_platform_services.h"
#endif
#include "platform_legacy/legacy_platform_state.h"
#include "platform_legacy/legacy_platform_services.h"
#include "renderer_gl/opengl_capabilities.h"
@@ -65,10 +66,11 @@ void App::set_platform_services(pp::platform::PlatformServices* services) noexce
if (services)
{
pp::platform::linux_desktop::set_fps_title_callback([this](std::string title) {
if (!glfw_window)
auto* const window = pp::platform::legacy::active_legacy_glfw_window_state().window;
if (!window)
return;
glfwSetWindowTitle(glfw_window, title.c_str());
glfwSetWindowTitle(window, title.c_str());
});
}
else

View File

@@ -63,6 +63,27 @@ void AppRuntime::canvas_async_task(std::function<void()> task)
canvas_async_cv_.notify_one();
}
void AppRuntime::main_thread_task(std::packaged_task<void()> task)
{
std::lock_guard<std::mutex> lock(main_thread_task_mutex_);
main_thread_tasklist_.push_back(std::move(task));
}
void AppRuntime::drain_main_thread_tasks()
{
std::deque<std::packaged_task<void()>> tasklist;
{
std::lock_guard<std::mutex> lock(main_thread_task_mutex_);
tasklist = std::move(main_thread_tasklist_);
}
while (!tasklist.empty())
{
tasklist.front()();
tasklist.pop_front();
}
}
void AppRuntime::prepared_file_worker_main(std::stop_token stop_token)
{
for (;;)

View File

@@ -46,6 +46,8 @@ public:
void notify_ui_worker() noexcept;
void prepared_file_task(std::function<void()> task);
void canvas_async_task(std::function<void()> task);
void main_thread_task(std::packaged_task<void()> task);
void drain_main_thread_tasks();
void render_thread_tick(App& app);
void render_thread_main(App& app, std::stop_token stop_token);
@@ -221,6 +223,9 @@ private:
std::jthread canvas_async_worker_;
bool canvas_async_running_ = true;
std::deque<std::packaged_task<void()>> main_thread_tasklist_;
std::mutex main_thread_task_mutex_;
std::deque<AppTask> render_tasklist_;
std::mutex render_task_mutex_;
std::condition_variable render_cv_;

View File

@@ -19,20 +19,7 @@ struct RetainedLegacyStoragePaths final {
#if defined(__LINUX__) || defined(__WEB__)
[[nodiscard]] RetainedLegacyGlfwWindowState& active_legacy_glfw_window_state()
{
static RetainedLegacyGlfwWindowState state = [] {
RetainedLegacyGlfwWindowState retained;
retained.window = App::I->glfw_window;
retained.hooks.acquire_render_context = [window = retained.window] {
glfwMakeContextCurrent(window);
};
retained.hooks.present_render_context = [window = retained.window] {
glfwSwapBuffers(window);
};
retained.hooks.request_app_close = [window = retained.window] {
glfwSetWindowShouldClose(window, GLFW_TRUE);
};
return retained;
}();
static RetainedLegacyGlfwWindowState state;
return state;
}
@@ -40,24 +27,53 @@ struct RetainedLegacyStoragePaths final {
{
return active_legacy_glfw_window_state().hooks;
}
void set_legacy_glfw_window(GLFWwindow* window)
{
auto& retained = active_legacy_glfw_window_state();
retained.window = window;
retained.hooks.acquire_render_context = [window] {
glfwMakeContextCurrent(window);
};
retained.hooks.present_render_context = [window] {
glfwSwapBuffers(window);
};
retained.hooks.request_app_close = [window] {
glfwSetWindowShouldClose(window, GLFW_TRUE);
};
}
#endif
#if defined(__IOS__) || defined(__OSX__)
[[nodiscard]] RetainedLegacyAppleState& active_legacy_apple_state()
{
static RetainedLegacyAppleState state = [] {
RetainedLegacyAppleState retained;
#ifdef __IOS__
retained.ios_view = App::I->ios_view;
retained.ios_app = App::I->ios_app;
#elif defined(__OSX__)
retained.osx_view = App::I->osx_view;
retained.osx_app = App::I->osx_app;
#endif
return retained;
}();
static RetainedLegacyAppleState state;
return state;
}
void set_legacy_apple_state(
#ifdef __IOS__
GameViewController* ios_view,
AppDelegate* ios_app
#elif defined(__OSX__)
View* osx_view,
AppOSX* osx_app
#endif
)
{
auto& retained = active_legacy_apple_state();
#ifdef __IOS__
if (ios_view)
retained.ios_view = ios_view;
if (ios_app)
retained.ios_app = ios_app;
#elif defined(__OSX__)
if (osx_view)
retained.osx_view = osx_view;
if (osx_app)
retained.osx_app = osx_app;
#endif
}
#endif
[[nodiscard]] const pp::platform::PlatformStoragePaths& active_legacy_storage_paths()

View File

@@ -32,6 +32,7 @@ struct RetainedLegacyGlfwWindowState final {
[[nodiscard]] RetainedLegacyGlfwWindowState& active_legacy_glfw_window_state();
[[nodiscard]] RetainedLegacyGlfwWindowHooks& active_legacy_glfw_window_hooks();
void set_legacy_glfw_window(GLFWwindow* window);
#endif
#if defined(__IOS__) || defined(__OSX__)
@@ -46,6 +47,15 @@ struct RetainedLegacyAppleState final {
};
[[nodiscard]] RetainedLegacyAppleState& active_legacy_apple_state();
void set_legacy_apple_state(
#ifdef __IOS__
GameViewController* ios_view,
AppDelegate* ios_app
#elif defined(__OSX__)
View* osx_view,
AppOSX* osx_app
#endif
);
#endif
[[nodiscard]] const pp::platform::PlatformStoragePaths& active_legacy_storage_paths();

View File

@@ -99,24 +99,6 @@ void win32_save_window_state()
namespace pp::platform::windows {
namespace {
struct RetainedMainTaskQueue final {
std::deque<std::packaged_task<void()>> tasklist;
std::mutex task_mutex;
};
[[nodiscard]] RetainedMainTaskQueue& retained_main_task_queue()
{
static RetainedMainTaskQueue queue;
return queue;
}
void enqueue_main_task(std::packaged_task<void()> task)
{
auto& queue = retained_main_task_queue();
std::lock_guard<std::mutex> lock(queue.task_mutex);
queue.tasklist.emplace_back(std::move(task));
}
struct RetainedWin32AsyncRenderContextState final {
HDC hdc{};
HGLRC hrc{};
@@ -189,23 +171,18 @@ void swap_async_render_context()
void enqueue_main_thread_task(std::packaged_task<void()> task)
{
enqueue_main_task(std::move(task));
if (!App::I)
{
task();
return;
}
App::I->runtime().main_thread_task(std::move(task));
}
void drain_main_thread_tasks()
{
std::deque<std::packaged_task<void()>> working_list;
{
auto& queue = retained_main_task_queue();
std::lock_guard<std::mutex> lock(queue.task_mutex);
working_list = std::move(queue.tasklist);
}
while (!working_list.empty())
{
working_list.front()();
working_list.pop_front();
}
if (App::I)
App::I->runtime().drain_main_thread_tasks();
}
} // namespace pp::platform::windows