Own canvas async work and thin NodeCanvas composite

This commit is contained in:
2026-06-16 07:10:09 +02:00
parent 75f57213ca
commit 4d7a23a1fd
7 changed files with 279 additions and 78 deletions

View File

@@ -20,6 +20,10 @@
#include "renderer_gl/opengl_capabilities.h"
#include "util.h"
#include <array>
#include <condition_variable>
#include <deque>
#include <mutex>
#include <stop_token>
#include <thread>
#include <algorithm>
#include <cstdint>
@@ -31,6 +35,79 @@
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_;
};
LegacyCanvasAsyncWorker& canvas_async_worker()
{
static LegacyCanvasAsyncWorker worker;
return worker;
}
GLint current_canvas_stroke_internal_format()
{
const auto renderer_features = ShaderManager::render_device_features();
@@ -2769,11 +2846,10 @@ void Canvas::import_equirectangular(std::string file_path, std::shared_ptr<Layer
{
if (App::I->check_license())
{
std::thread t([=] {
canvas_async_worker().post([this, file_path = std::move(file_path), layer = std::move(layer)] {
BT_SetTerminate();
import_equirectangular_thread(file_path, layer);
});
t.detach();
}
}
@@ -2862,13 +2938,12 @@ void Canvas::export_equirectangular(std::string file_path, std::function<void()>
{
if (App::I->check_license())
{
std::thread t([=] {
canvas_async_worker().post([this, file_path = std::move(file_path), on_complete = std::move(on_complete)]() mutable {
BT_SetTerminate();
export_equirectangular_thread(file_path);
if (on_complete)
on_complete();
App::I->ui_task([on_complete = std::move(on_complete)]() mutable { on_complete(); });
});
t.detach();
}
}
@@ -2949,13 +3024,12 @@ void Canvas::export_depth(std::string file_name, std::function<void()> on_comple
{
if (App::I->check_license())
{
std::thread t([=] {
canvas_async_worker().post([this, file_name = std::move(file_name), on_complete = std::move(on_complete)]() mutable {
BT_SetTerminate();
export_depth_thread(file_name);
if (on_complete)
on_complete();
App::I->ui_task([on_complete = std::move(on_complete)]() mutable { on_complete(); });
});
t.detach();
}
}
@@ -3057,13 +3131,12 @@ void Canvas::export_layers(std::string path, std::function<void()> on_complete)
{
if (App::I->check_license())
{
std::thread t([=] {
canvas_async_worker().post([this, path = std::move(path), on_complete = std::move(on_complete)]() mutable {
BT_SetTerminate();
export_layers_thread(path);
if (on_complete)
on_complete();
App::I->ui_task([on_complete = std::move(on_complete)]() mutable { on_complete(); });
});
t.detach();
}
}
@@ -3084,13 +3157,12 @@ void Canvas::export_anim_frames(std::string path, std::function<void()> on_compl
{
if (App::I->check_license())
{
std::thread t([=] {
canvas_async_worker().post([this, path = std::move(path), on_complete = std::move(on_complete)]() mutable {
BT_SetTerminate();
export_anim_frames_thread(path);
if (on_complete)
on_complete();
App::I->ui_task([on_complete = std::move(on_complete)]() mutable { on_complete(); });
});
t.detach();
}
}
@@ -3110,13 +3182,12 @@ void Canvas::export_anim_mp4(std::string path, std::function<void()> on_complete
{
if (App::I->check_license())
{
std::thread t([=] {
canvas_async_worker().post([this, path = std::move(path), on_complete = std::move(on_complete)]() mutable {
BT_SetTerminate();
export_anim_mp4_thread(path);
if (on_complete)
on_complete();
App::I->ui_task([on_complete = std::move(on_complete)]() mutable { on_complete(); });
});
t.detach();
}
}
@@ -3149,13 +3220,12 @@ void Canvas::export_cube_faces(std::string file_name, std::function<void()> on_c
{
if (App::I->check_license())
{
std::thread t([=] {
canvas_async_worker().post([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)
on_complete();
App::I->ui_task([on_complete = std::move(on_complete)]() mutable { on_complete(); });
});
t.detach();
}
}
@@ -3202,13 +3272,13 @@ void Canvas::project_save(std::function<void(bool)> on_complete)
{
if (App::I->check_license())
{
std::thread t([=] {
const auto file_path = App::I->doc_path;
canvas_async_worker().post([this, file_path, on_complete = std::move(on_complete)]() mutable {
BT_SetTerminate();
bool ret = project_save_thread(App::I->doc_path, true);
bool ret = project_save_thread(file_path, true);
if (on_complete)
on_complete(ret);
App::I->ui_task([on_complete = std::move(on_complete), ret]() mutable { on_complete(ret); });
});
t.detach();
}
}
@@ -3217,13 +3287,12 @@ 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())
{
std::thread t([=] {
canvas_async_worker().post([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)
on_complete(ret);
App::I->ui_task([on_complete = std::move(on_complete), ret]() mutable { on_complete(ret); });
});
t.detach();
}
else
{
@@ -3499,13 +3568,12 @@ 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)
{
std::thread t([=] {
canvas_async_worker().post([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)
on_complete(result);
App::I->ui_task([on_complete = std::move(on_complete), result]() mutable { on_complete(result); });
});
t.detach();
}
bool Canvas::project_open_thread(std::string file_path)