Move canvas async work into app runtime

This commit is contained in:
2026-06-16 08:33:16 +02:00
parent 3e4eb89499
commit 52f0d32612
9 changed files with 151 additions and 119 deletions

View File

@@ -35,90 +35,6 @@
namespace {
class LegacyCanvasAsyncWorker final {
public:
LegacyCanvasAsyncWorker()
: worker_([this](std::stop_token stop_token) {
run(stop_token);
})
{
}
~LegacyCanvasAsyncWorker()
{
shutdown();
}
void post(std::function<void()> task)
{
{
std::lock_guard<std::mutex> lock(mutex_);
if (stopping_)
return;
tasks_.push_back(std::move(task));
}
cv_.notify_one();
}
private:
void shutdown()
{
{
std::lock_guard<std::mutex> lock(mutex_);
stopping_ = true;
}
cv_.notify_all();
}
void run(std::stop_token stop_token)
{
for (;;) {
std::function<void()> task;
{
std::unique_lock<std::mutex> lock(mutex_);
cv_.wait(lock, [&] {
return stopping_ || stop_token.stop_requested() || !tasks_.empty();
});
if ((stopping_ || stop_token.stop_requested()) && tasks_.empty())
break;
task = std::move(tasks_.front());
tasks_.pop_front();
}
if (task) {
try {
task();
} catch (...) {
LOG("canvas async worker task failed");
}
}
}
}
std::mutex mutex_;
std::condition_variable cv_;
std::deque<std::function<void()>> tasks_;
bool stopping_ = false;
std::jthread worker_;
};
class RetainedCanvasAsyncWorkerState final {
public:
void post(std::function<void()> task)
{
worker_.post(std::move(task));
}
private:
LegacyCanvasAsyncWorker worker_;
};
RetainedCanvasAsyncWorkerState& canvas_async_worker_state()
{
static RetainedCanvasAsyncWorkerState state;
return state;
}
GLint current_canvas_stroke_internal_format()
{
const auto renderer_features = ShaderManager::render_device_features();
@@ -2857,7 +2773,7 @@ void Canvas::import_equirectangular(std::string file_path, std::shared_ptr<Layer
{
if (App::I->check_license())
{
canvas_async_worker_state().post([this, file_path = std::move(file_path), layer = std::move(layer)] {
App::I->runtime().canvas_async_task([this, file_path = std::move(file_path), layer = std::move(layer)] {
BT_SetTerminate();
import_equirectangular_thread(file_path, layer);
});
@@ -2949,7 +2865,7 @@ void Canvas::export_equirectangular(std::string file_path, std::function<void()>
{
if (App::I->check_license())
{
canvas_async_worker_state().post([this, file_path = std::move(file_path), on_complete = std::move(on_complete)]() mutable {
App::I->runtime().canvas_async_task([this, file_path = std::move(file_path), on_complete = std::move(on_complete)]() mutable {
BT_SetTerminate();
export_equirectangular_thread(file_path);
if (on_complete)
@@ -3035,7 +2951,7 @@ void Canvas::export_depth(std::string file_name, std::function<void()> on_comple
{
if (App::I->check_license())
{
canvas_async_worker_state().post([this, file_name = std::move(file_name), on_complete = std::move(on_complete)]() mutable {
App::I->runtime().canvas_async_task([this, file_name = std::move(file_name), on_complete = std::move(on_complete)]() mutable {
BT_SetTerminate();
export_depth_thread(file_name);
if (on_complete)
@@ -3142,7 +3058,7 @@ void Canvas::export_layers(std::string path, std::function<void()> on_complete)
{
if (App::I->check_license())
{
canvas_async_worker_state().post([this, path = std::move(path), on_complete = std::move(on_complete)]() mutable {
App::I->runtime().canvas_async_task([this, path = std::move(path), on_complete = std::move(on_complete)]() mutable {
BT_SetTerminate();
export_layers_thread(path);
if (on_complete)
@@ -3168,7 +3084,7 @@ void Canvas::export_anim_frames(std::string path, std::function<void()> on_compl
{
if (App::I->check_license())
{
canvas_async_worker_state().post([this, path = std::move(path), on_complete = std::move(on_complete)]() mutable {
App::I->runtime().canvas_async_task([this, path = std::move(path), on_complete = std::move(on_complete)]() mutable {
BT_SetTerminate();
export_anim_frames_thread(path);
if (on_complete)
@@ -3193,7 +3109,7 @@ void Canvas::export_anim_mp4(std::string path, std::function<void()> on_complete
{
if (App::I->check_license())
{
canvas_async_worker_state().post([this, path = std::move(path), on_complete = std::move(on_complete)]() mutable {
App::I->runtime().canvas_async_task([this, path = std::move(path), on_complete = std::move(on_complete)]() mutable {
BT_SetTerminate();
export_anim_mp4_thread(path);
if (on_complete)
@@ -3231,7 +3147,7 @@ void Canvas::export_cube_faces(std::string file_name, std::function<void()> on_c
{
if (App::I->check_license())
{
canvas_async_worker_state().post([this, file_name = std::move(file_name), on_complete = std::move(on_complete)]() mutable {
App::I->runtime().canvas_async_task([this, file_name = std::move(file_name), on_complete = std::move(on_complete)]() mutable {
BT_SetTerminate();
export_cube_faces_thread(file_name);
if (on_complete)
@@ -3284,7 +3200,7 @@ void Canvas::project_save(std::function<void(bool)> on_complete)
if (App::I->check_license())
{
const auto file_path = App::I->doc_path;
canvas_async_worker_state().post([this, file_path, on_complete = std::move(on_complete)]() mutable {
App::I->runtime().canvas_async_task([this, file_path, on_complete = std::move(on_complete)]() mutable {
BT_SetTerminate();
bool ret = project_save_thread(file_path, true);
if (on_complete)
@@ -3298,7 +3214,7 @@ void Canvas::project_save(std::string file_path, std::function<void(bool)> on_co
LOG("saving %s", file_path.c_str());
if (App::I->check_license())
{
canvas_async_worker_state().post([this, file_path = std::move(file_path), on_complete = std::move(on_complete)]() mutable {
App::I->runtime().canvas_async_task([this, file_path = std::move(file_path), on_complete = std::move(on_complete)]() mutable {
BT_SetTerminate();
bool ret = project_save_thread(file_path, true);
if (on_complete)
@@ -3579,7 +3495,7 @@ bool Canvas::project_save_thread(std::string file_path, bool show_progress)
void Canvas::project_open(std::string file_path, std::function<void(bool)> on_complete)
{
canvas_async_worker_state().post([this, file_path = std::move(file_path), on_complete = std::move(on_complete)]() mutable {
App::I->runtime().canvas_async_task([this, file_path = std::move(file_path), on_complete = std::move(on_complete)]() mutable {
BT_SetTerminate();
bool result = project_open_thread(file_path);
if (on_complete)