Own canvas async work and thin NodeCanvas composite
This commit is contained in:
128
src/canvas.cpp
128
src/canvas.cpp
@@ -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)
|
||||
|
||||
@@ -175,6 +175,20 @@ struct LegacyCanvasDrawMergeFinalPlaneCompositeExecution {
|
||||
std::function<void()> unbind_merged_texture;
|
||||
};
|
||||
|
||||
struct LegacyCanvasDrawMergeCacheToScreenCompositeUniforms {
|
||||
LegacyCanvasDrawMergeCheckerboardUniforms checkerboard;
|
||||
LegacyCanvasDrawMergeTextureUniforms texture;
|
||||
};
|
||||
|
||||
struct LegacyCanvasDrawMergeCacheToScreenCompositeExecution {
|
||||
std::function<void()> enable_blend;
|
||||
std::function<void(const LegacyCanvasDrawMergeCheckerboardUniforms&, int)> draw_checkerboard_plane;
|
||||
std::function<void()> bind_sampler;
|
||||
std::function<void()> bind_cache_texture;
|
||||
std::function<void()> draw_cache_texture;
|
||||
std::function<void()> unbind_cache_texture;
|
||||
};
|
||||
|
||||
struct LegacyCanvasDrawMergeDisplayResolveUniforms {
|
||||
LegacyCanvasDrawMergeTextureUniforms texture;
|
||||
};
|
||||
@@ -447,6 +461,23 @@ inline void execute_legacy_canvas_draw_merge_final_plane_composite(
|
||||
execution.unbind_merged_texture();
|
||||
}
|
||||
|
||||
inline void execute_legacy_canvas_draw_merge_cache_to_screen_composite(
|
||||
const LegacyCanvasDrawMergeCacheToScreenCompositeUniforms& uniforms,
|
||||
const LegacyCanvasDrawMergeCacheToScreenCompositeExecution& execution)
|
||||
{
|
||||
execution.enable_blend();
|
||||
|
||||
for (int plane_index = 0; plane_index < 6; ++plane_index) {
|
||||
execution.draw_checkerboard_plane(uniforms.checkerboard, plane_index);
|
||||
}
|
||||
|
||||
execution.bind_sampler();
|
||||
execution.bind_cache_texture();
|
||||
setup_legacy_canvas_draw_merge_texture_shader(uniforms.texture);
|
||||
execution.draw_cache_texture();
|
||||
execution.unbind_cache_texture();
|
||||
}
|
||||
|
||||
inline void execute_legacy_canvas_draw_merge_display_resolve(
|
||||
const LegacyCanvasDrawMergeDisplayResolveUniforms& uniforms,
|
||||
const LegacyCanvasDrawMergeDisplayResolveExecution& execution)
|
||||
|
||||
@@ -7,10 +7,15 @@
|
||||
#include "paint_renderer/compositor.h"
|
||||
|
||||
#include <array>
|
||||
#include <condition_variable>
|
||||
#include <fstream>
|
||||
#include <deque>
|
||||
#include <functional>
|
||||
#include <mutex>
|
||||
#include <limits>
|
||||
#include <span>
|
||||
#include <string>
|
||||
#include <stop_token>
|
||||
#include <thread>
|
||||
|
||||
namespace pp::panopainter {
|
||||
@@ -30,6 +35,79 @@ struct LegacyDocumentExportSnapshotReports {
|
||||
pp::paint_renderer::DocumentFrameFacePngExportResult face_pngs;
|
||||
};
|
||||
|
||||
class LegacyDocumentVideoExportWorker final {
|
||||
public:
|
||||
LegacyDocumentVideoExportWorker()
|
||||
: worker_([this](std::stop_token stop_token) {
|
||||
run(stop_token);
|
||||
})
|
||||
{
|
||||
}
|
||||
|
||||
~LegacyDocumentVideoExportWorker()
|
||||
{
|
||||
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("document video export worker task failed");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::mutex mutex_;
|
||||
std::condition_variable cv_;
|
||||
std::deque<std::function<void()>> tasks_;
|
||||
bool stopping_ = false;
|
||||
std::jthread worker_;
|
||||
};
|
||||
|
||||
LegacyDocumentVideoExportWorker& document_video_export_worker()
|
||||
{
|
||||
static LegacyDocumentVideoExportWorker worker;
|
||||
return worker;
|
||||
}
|
||||
|
||||
pp::foundation::Status write_export_binary_file(std::string_view path, std::span<const std::byte> bytes)
|
||||
{
|
||||
if (path.empty()) {
|
||||
@@ -746,16 +824,18 @@ public:
|
||||
auto* app = &app_;
|
||||
auto path_string = std::string(path);
|
||||
if (asynchronous_) {
|
||||
std::thread([app, path_string] {
|
||||
document_video_export_worker().post([app, path_string = std::move(path_string)]() mutable {
|
||||
BT_SetTerminate();
|
||||
app->rec_export(path_string);
|
||||
show_export_success_dialog(
|
||||
*app,
|
||||
pp::app::plan_document_export_success_dialog(
|
||||
pp::app::DocumentExportSuccessKind::timelapse,
|
||||
pp::app::DocumentExportSuccessDestination::path,
|
||||
path_string));
|
||||
}).detach();
|
||||
app->ui_task([app, path_string = std::move(path_string)]() mutable {
|
||||
show_export_success_dialog(
|
||||
*app,
|
||||
pp::app::plan_document_export_success_dialog(
|
||||
pp::app::DocumentExportSuccessKind::timelapse,
|
||||
pp::app::DocumentExportSuccessDestination::path,
|
||||
path_string));
|
||||
});
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -676,40 +676,44 @@ void NodeCanvas::draw()
|
||||
apply_node_canvas_viewport(0, 0, m_rtt.getWidth(), m_rtt.getHeight());
|
||||
else
|
||||
apply_node_canvas_viewport(c.x + App::I->off_x, c.y + App::I->off_y, c.z, c.w);
|
||||
}
|
||||
|
||||
// draw the grid behind the layers using a temporary copy
|
||||
if (use_blend)
|
||||
{
|
||||
apply_node_canvas_capability(pp::renderer::gl::blend_state(), true);
|
||||
|
||||
//draw the grid
|
||||
for (int plane_index = 0; plane_index < 6; plane_index++)
|
||||
{
|
||||
auto plane_mvp = proj * camera *
|
||||
glm::scale(glm::vec3(m_canvas->m_layers.size() + 500.f)) *
|
||||
m_canvas->m_plane_transform[plane_index] *
|
||||
glm::translate(glm::vec3(0, 0, -1.f));
|
||||
|
||||
pp::panopainter::setup_legacy_canvas_draw_merge_checkerboard_shader(
|
||||
pp::panopainter::LegacyCanvasDrawMergeCheckerboardUniforms {
|
||||
.mvp = plane_mvp,
|
||||
pp::panopainter::execute_legacy_canvas_draw_merge_cache_to_screen_composite(
|
||||
pp::panopainter::LegacyCanvasDrawMergeCacheToScreenCompositeUniforms {
|
||||
.checkerboard = {
|
||||
.colorize = false,
|
||||
});
|
||||
m_face_plane.draw_fill();
|
||||
}
|
||||
},
|
||||
.texture = {
|
||||
.mvp = glm::ortho<float>(-1, 1, -1, 1),
|
||||
.texture_slot = 0,
|
||||
},
|
||||
},
|
||||
{
|
||||
.enable_blend = [&] {
|
||||
apply_node_canvas_capability(pp::renderer::gl::blend_state(), true);
|
||||
},
|
||||
.draw_checkerboard_plane = [&](const pp::panopainter::LegacyCanvasDrawMergeCheckerboardUniforms& uniforms, int plane_index) {
|
||||
auto checkerboard_uniforms = uniforms;
|
||||
checkerboard_uniforms.mvp = proj * camera *
|
||||
glm::scale(glm::vec3(m_canvas->m_layers.size() + 500.f)) *
|
||||
m_canvas->m_plane_transform[plane_index] *
|
||||
glm::translate(glm::vec3(0, 0, -1.f));
|
||||
|
||||
// draw the layers
|
||||
m_sampler.bind(0);
|
||||
set_active_texture_unit(0);
|
||||
m_cache_rtt.bindTexture();
|
||||
pp::panopainter::setup_legacy_canvas_draw_merge_texture_shader(
|
||||
pp::panopainter::LegacyCanvasDrawMergeTextureUniforms {
|
||||
.mvp = glm::ortho<float>(-1, 1, -1, 1),
|
||||
.texture_slot = 0,
|
||||
pp::panopainter::setup_legacy_canvas_draw_merge_checkerboard_shader(checkerboard_uniforms);
|
||||
m_face_plane.draw_fill();
|
||||
},
|
||||
.bind_sampler = [&] {
|
||||
m_sampler.bind(0);
|
||||
set_active_texture_unit(0);
|
||||
},
|
||||
.bind_cache_texture = [&] {
|
||||
m_cache_rtt.bindTexture();
|
||||
},
|
||||
.draw_cache_texture = [&] {
|
||||
m_face_plane.draw_fill();
|
||||
},
|
||||
.unbind_cache_texture = [&] {
|
||||
m_cache_rtt.unbindTexture();
|
||||
},
|
||||
});
|
||||
m_face_plane.draw_fill();
|
||||
m_cache_rtt.unbindTexture();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user