diff --git a/src/main.cpp b/src/main.cpp index ccffd9e5..6d956d14 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -17,7 +17,6 @@ #include "../resource.h" #include -#include #include "wacom.h" #include "abr.h" @@ -30,68 +29,17 @@ namespace pp::platform::windows { RetainedState& retained_state(); } using pp::platform::windows::retained_state; -namespace { - -struct RetainedMainTaskQueue final -{ - std::deque> tasklist; - std::mutex task_mutex; -}; - -RetainedMainTaskQueue& retained_main_task_queue() -{ - static RetainedMainTaskQueue queue; - return queue; -} - -template -void enqueue_main_task(Callable&& task) -{ - auto& queue = retained_main_task_queue(); - std::lock_guard lock(queue.task_mutex); - queue.tasklist.emplace_back(std::forward(task)); -} - -void enqueue_main_task_bridge(std::packaged_task task) -{ - auto& queue = retained_main_task_queue(); - std::lock_guard lock(queue.task_mutex); - queue.tasklist.emplace_back(std::move(task)); -} - -void drain_main_tasks() -{ - std::deque> working_list; - { - auto& queue = retained_main_task_queue(); - std::lock_guard lock(queue.task_mutex); - working_list = std::move(queue.tasklist); - } - - while (!working_list.empty()) - { - working_list.front()(); - working_list.pop_front(); - } -} - -} HWND pp_windows_main_window_handle() { return retained_state().hWnd; } -void pp_windows_enqueue_main_task(std::packaged_task task) -{ - enqueue_main_task_bridge(std::move(task)); -} - void destroy_window() { - enqueue_main_task([hWnd = retained_state().hWnd] { + pp::platform::windows::enqueue_main_thread_task(std::packaged_task([hWnd = retained_state().hWnd] { pp::platform::windows::request_window_close(hWnd); - }); + })); } void async_lock() @@ -122,9 +70,9 @@ void win32_update_stylus(float dt) void win32_update_fps(int frames) { auto& state = retained_state(); - enqueue_main_task([hWnd = state.hWnd, window_title = state.window_title, &vr = state.vr, frames] { + pp::platform::windows::enqueue_main_thread_task(std::packaged_task([hWnd = state.hWnd, window_title = state.window_title, &vr = state.vr, frames] { pp::platform::windows::update_window_fps(hWnd, window_title, vr, frames); - }); + })); } int read_WMI_info() @@ -487,7 +435,7 @@ int main(int argc, char** argv) } // list of tasks for the main thread - drain_main_tasks(); + pp::platform::windows::drain_main_thread_tasks(); } // Clean up WacomTablet::I.terminate(); diff --git a/src/platform_windows/windows_platform_services.cpp b/src/platform_windows/windows_platform_services.cpp index b278f98d..6f98bccd 100644 --- a/src/platform_windows/windows_platform_services.cpp +++ b/src/platform_windows/windows_platform_services.cpp @@ -22,11 +22,28 @@ void win32_save_window_state(); bool win32_vr_start(); void win32_vr_stop(); HWND pp_windows_main_window_handle(); -void pp_windows_enqueue_main_task(std::packaged_task task); namespace pp::platform::windows { namespace { +struct RetainedMainTaskQueue final { + std::deque> tasklist; + std::mutex task_mutex; +}; + +[[nodiscard]] RetainedMainTaskQueue& retained_main_task_queue() +{ + static RetainedMainTaskQueue queue; + return queue; +} + +void enqueue_main_task(std::packaged_task task) +{ + auto& queue = retained_main_task_queue(); + std::lock_guard lock(queue.task_mutex); + queue.tasklist.emplace_back(std::move(task)); +} + struct RetainedWin32AsyncRenderContextState final { HDC hdc{}; HGLRC hrc{}; @@ -96,6 +113,27 @@ void swap_async_render_context() { SwapBuffers(retained_win32_async_render_context_state().hdc); } + +void enqueue_main_thread_task(std::packaged_task task) +{ + enqueue_main_task(std::move(task)); +} + +void drain_main_thread_tasks() +{ + std::deque> working_list; + { + auto& queue = retained_main_task_queue(); + std::lock_guard lock(queue.task_mutex); + working_list = std::move(queue.tasklist); + } + + while (!working_list.empty()) + { + working_list.front()(); + working_list.pop_front(); + } +} } // namespace pp::platform::windows namespace { @@ -159,7 +197,7 @@ void handle_gl_callback( void show_cursor(bool visible) { - pp_windows_enqueue_main_task(std::packaged_task([=] { + pp::platform::windows::enqueue_main_thread_task(std::packaged_task([=] { if (visible) while (ShowCursor(true) < 0); else diff --git a/src/platform_windows/windows_platform_services.h b/src/platform_windows/windows_platform_services.h index 0dda5a70..f705726e 100644 --- a/src/platform_windows/windows_platform_services.h +++ b/src/platform_windows/windows_platform_services.h @@ -2,8 +2,12 @@ #include "platform_api/platform_services.h" +#include + namespace pp::platform::windows { [[nodiscard]] PlatformServices& platform_services(); +void enqueue_main_thread_task(std::packaged_task task); +void drain_main_thread_tasks(); }