Move canvas async work into app runtime
This commit is contained in:
25
src/app.cpp
25
src/app.cpp
@@ -689,22 +689,31 @@ namespace
|
||||
if (plan.update_frame_label)
|
||||
app.update_rec_frames();
|
||||
}
|
||||
|
||||
template <typename CanvasDocument>
|
||||
bool process_recording_worker_iteration(App& app)
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(app.rec_mutex);
|
||||
app.rec_cv.wait(lock);
|
||||
|
||||
const auto iteration = make_recording_worker_iteration_context<CanvasDocument>(app);
|
||||
if (!iteration.plan.continue_running)
|
||||
return false;
|
||||
|
||||
if (iteration.plan.encode_frame && iteration.legacy_canvas && iteration.canvas_document && iteration.encoder)
|
||||
encode_recording_frame(app, iteration.plan, iteration.legacy_canvas, iteration.canvas_document, iteration.encoder);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
void App::rec_loop()
|
||||
{
|
||||
BT_SetTerminate();
|
||||
rec_running = true;
|
||||
while(rec_running)
|
||||
while (rec_running)
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(rec_mutex);
|
||||
rec_cv.wait(lock/*, [this] { return !(rec_frames.empty() && rec_running); }*/);
|
||||
const auto iteration = make_recording_worker_iteration_context<CanvasDocument>(*this);
|
||||
if (!iteration.plan.continue_running)
|
||||
if (!process_recording_worker_iteration<CanvasDocument>(*this))
|
||||
break;
|
||||
|
||||
if (iteration.plan.encode_frame && iteration.legacy_canvas && iteration.canvas_document && iteration.encoder)
|
||||
encode_recording_frame(*this, iteration.plan, iteration.legacy_canvas, iteration.canvas_document, iteration.encoder);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,11 +8,16 @@ AppRuntime::AppRuntime()
|
||||
{
|
||||
prepared_file_worker_main(stop_token);
|
||||
})
|
||||
, canvas_async_worker_([this](std::stop_token stop_token)
|
||||
{
|
||||
canvas_async_worker_main(stop_token);
|
||||
})
|
||||
{
|
||||
}
|
||||
|
||||
AppRuntime::~AppRuntime()
|
||||
{
|
||||
canvas_async_worker_stop();
|
||||
prepared_file_worker_stop();
|
||||
}
|
||||
|
||||
@@ -47,6 +52,17 @@ void AppRuntime::prepared_file_task(std::function<void()> task)
|
||||
prepared_file_cv_.notify_one();
|
||||
}
|
||||
|
||||
void AppRuntime::canvas_async_task(std::function<void()> task)
|
||||
{
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(canvas_async_task_mutex_);
|
||||
if (!canvas_async_running_)
|
||||
return;
|
||||
canvas_async_tasklist_.push_back(std::move(task));
|
||||
}
|
||||
canvas_async_cv_.notify_one();
|
||||
}
|
||||
|
||||
void AppRuntime::prepared_file_worker_main(std::stop_token stop_token)
|
||||
{
|
||||
for (;;)
|
||||
@@ -78,6 +94,37 @@ void AppRuntime::prepared_file_worker_main(std::stop_token stop_token)
|
||||
}
|
||||
}
|
||||
|
||||
void AppRuntime::canvas_async_worker_main(std::stop_token stop_token)
|
||||
{
|
||||
for (;;)
|
||||
{
|
||||
std::function<void()> task;
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(canvas_async_task_mutex_);
|
||||
canvas_async_cv_.wait(lock, [this, &stop_token]
|
||||
{
|
||||
return stop_token.stop_requested() || !canvas_async_running_ || !canvas_async_tasklist_.empty();
|
||||
});
|
||||
if ((stop_token.stop_requested() || !canvas_async_running_) && canvas_async_tasklist_.empty())
|
||||
break;
|
||||
task = std::move(canvas_async_tasklist_.front());
|
||||
canvas_async_tasklist_.pop_front();
|
||||
}
|
||||
|
||||
if (task)
|
||||
{
|
||||
try
|
||||
{
|
||||
task();
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
LOG("canvas async worker task failed");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void AppRuntime::prepared_file_worker_stop()
|
||||
{
|
||||
{
|
||||
@@ -92,6 +139,20 @@ void AppRuntime::prepared_file_worker_stop()
|
||||
}
|
||||
}
|
||||
|
||||
void AppRuntime::canvas_async_worker_stop()
|
||||
{
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(canvas_async_task_mutex_);
|
||||
canvas_async_running_ = false;
|
||||
}
|
||||
canvas_async_cv_.notify_all();
|
||||
if (canvas_async_worker_.joinable())
|
||||
{
|
||||
canvas_async_worker_.request_stop();
|
||||
canvas_async_worker_.join();
|
||||
}
|
||||
}
|
||||
|
||||
void AppRuntime::render_thread_tick(App& app)
|
||||
{
|
||||
static uint32_t count = 0;
|
||||
|
||||
@@ -45,6 +45,7 @@ public:
|
||||
void notify_render_worker() noexcept;
|
||||
void notify_ui_worker() noexcept;
|
||||
void prepared_file_task(std::function<void()> task);
|
||||
void canvas_async_task(std::function<void()> task);
|
||||
|
||||
void render_thread_tick(App& app);
|
||||
void render_thread_main(App& app, std::stop_token stop_token);
|
||||
@@ -205,6 +206,8 @@ public:
|
||||
private:
|
||||
void prepared_file_worker_main(std::stop_token stop_token);
|
||||
void prepared_file_worker_stop();
|
||||
void canvas_async_worker_main(std::stop_token stop_token);
|
||||
void canvas_async_worker_stop();
|
||||
|
||||
std::deque<std::function<void()>> prepared_file_tasklist_;
|
||||
std::mutex prepared_file_task_mutex_;
|
||||
@@ -212,6 +215,12 @@ private:
|
||||
std::jthread prepared_file_worker_;
|
||||
bool prepared_file_running_ = true;
|
||||
|
||||
std::deque<std::function<void()>> canvas_async_tasklist_;
|
||||
std::mutex canvas_async_task_mutex_;
|
||||
std::condition_variable canvas_async_cv_;
|
||||
std::jthread canvas_async_worker_;
|
||||
bool canvas_async_running_ = true;
|
||||
|
||||
std::deque<AppTask> render_tasklist_;
|
||||
std::mutex render_task_mutex_;
|
||||
std::condition_variable render_cv_;
|
||||
|
||||
104
src/canvas.cpp
104
src/canvas.cpp
@@ -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)
|
||||
|
||||
@@ -263,6 +263,19 @@ template <typename ModesT>
|
||||
};
|
||||
}
|
||||
|
||||
template <typename ModesT>
|
||||
[[nodiscard]] inline auto make_legacy_canvas_draw_merge_grid_modes_draw(
|
||||
ModesT* modes,
|
||||
const glm::mat4& ortho_proj,
|
||||
const glm::mat4& proj,
|
||||
const glm::mat4& camera)
|
||||
{
|
||||
return [modes, ortho_proj, proj, camera] {
|
||||
for (auto& mode : *modes)
|
||||
mode->on_Draw(ortho_proj, proj, camera);
|
||||
};
|
||||
}
|
||||
|
||||
struct LegacyCanvasDrawMergeSmaskFacesExecution {
|
||||
std::function<void()> set_active_texture_unit;
|
||||
std::function<void()> enable_blend;
|
||||
|
||||
@@ -765,10 +765,11 @@ void NodeCanvas::draw()
|
||||
},
|
||||
});
|
||||
},
|
||||
.draw_grid_modes = [&] {
|
||||
for (auto& mode : Canvas::modes[(int)kCanvasMode::Grid])
|
||||
mode->on_Draw(ortho_proj, proj, camera);
|
||||
},
|
||||
.draw_grid_modes = pp::panopainter::make_legacy_canvas_draw_merge_grid_modes_draw(
|
||||
&Canvas::modes[(int)kCanvasMode::Grid],
|
||||
ortho_proj,
|
||||
proj,
|
||||
camera),
|
||||
.draw_heightmap = pp::panopainter::make_legacy_canvas_draw_merge_heightmap_draw(App::I->grid, proj, camera),
|
||||
.draw_current_modes = pp::panopainter::make_legacy_canvas_draw_merge_current_modes_draw(
|
||||
m_canvas->m_mode,
|
||||
|
||||
Reference in New Issue
Block a user