Files
Renderive/render_2D/renderable/Renderable.cpp
T
2026-08-12 09:45:18 +08:00

101 lines
2.9 KiB
C++

#include "Renderable.h"
#include "../plot/Plot_Core.h"
#include "../render/Blend2D_Cache.h"
#include <renderive/scene/base/Scene_Render_Context.hpp>
namespace renderive {
Renderable::Renderable(Plot_Core& plot, bool cache_enabled)
: ::Renderable_Base(plot.kernel_scene(), {.cache_enabled = cache_enabled}), plot_(plot) {}
Renderable::~Renderable() = default;
Renderable_Cache_Mode Renderable::get_cache_mode() const noexcept {
return configuration().cache_enabled
? Renderable_Cache_Mode::Local_Pixel
: Renderable_Cache_Mode::Direct;
}
void Renderable::set_cache_mode(Renderable_Cache_Mode mode) {
plot_.set_renderable_cache(*this, mode);
}
std::string Renderable::object_name() const {
std::lock_guard lock(metadata_mutex_);
return object_name_;
}
void Renderable::set_object_name(std::string name) {
std::lock_guard lock(metadata_mutex_);
object_name_ = std::move(name);
}
bool Renderable::is_visible() const noexcept {
return visible_.load(std::memory_order_acquire);
}
void Renderable::set_visible(bool visible) {
if (visible_.exchange(visible, std::memory_order_acq_rel) != visible)
changed();
}
void Renderable::render(const ::Scene_Render_Context& context) {
if (!is_visible() || context.color_cache == nullptr)
return;
auto* cache = dynamic_cast<detail::Blend2D_Color_Cache*>(context.color_cache);
if (!cache)
return;
detail::Painter painter(*cache, viewport_size());
if (painter) {
const auto state = render_state_view();
paint(painter, state);
}
}
void Renderable::build_task_graph(Renderable_Task_Graph& graph) {
build_paint_task_graph(graph);
}
void Renderable::build_paint_task_graph(Renderable_Task_Graph& graph) {
add_paint_task(graph, "paint", [this](detail::Painter& painter,
const Render_State_View& state) {
paint(painter, state);
});
}
Renderable_Task_Graph::Task Renderable::add_paint_task(
Renderable_Task_Graph& graph,
std::string name,
Paint_Task_Function function) {
return graph.emplace(
[this, function = std::move(function)](const Scene_Render_Context& context) {
if (!is_visible() || context.color_cache == nullptr)
return;
auto* cache = dynamic_cast<detail::Blend2D_Color_Cache*>(context.color_cache);
if (!cache)
return;
detail::Painter painter(*cache, viewport_size());
if (painter)
function(painter, render_state_view());
},
std::move(name));
}
void Renderable::changed() noexcept {
invalidate_cache();
plot_.notify_model_dirty();
}
void Renderable::task_graph_changed() noexcept {
rebuild_task_graph();
plot_.notify_model_dirty();
}
Size Renderable::viewport_size() const noexcept {
return plot_.viewport_size();
}
} // namespace renderive