Skip to content

Commit

Permalink
canvas: cache layers
Browse files Browse the repository at this point in the history
keeping a reference to the layer provider around and using it after a
call to update() isn't a good idea since the object handed to update()
may be already gone when rendering

so cache the layers for later use
  • Loading branch information
carrotIndustries committed Jul 28, 2023
1 parent d3039e1 commit bd91a8d
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 19 deletions.
1 change: 1 addition & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ src_common_ui = files(

src_canvas = files(
'src/canvas/canvas.cpp',
'src/canvas/canvas_layer_provider.cpp',
'src/canvas/appearance.cpp',
'src/canvas/render.cpp',
'src/canvas/draw.cpp',
Expand Down
16 changes: 8 additions & 8 deletions src/canvas/canvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ void Canvas::add_triangle(int layer, const Coordf &p0, const Coordf &p1, const C
void Canvas::update(const Symbol &sym, const Placement &tr, SymbolMode mode)
{
clear();
layer_provider = &sym;
layer_provider.update(sym);
transform = tr;
render(sym, mode);
request_push();
Expand All @@ -210,7 +210,7 @@ void Canvas::update(const Symbol &sym, const Placement &tr, SymbolMode mode)
void Canvas::update(const Sheet &sheet)
{
clear();
layer_provider = &sheet;
layer_provider.update(sheet);
update_markers();
render(sheet);
request_push();
Expand All @@ -219,46 +219,46 @@ void Canvas::update(const Sheet &sheet)
void Canvas::update(const Padstack &padstack, bool edit)
{
clear();
layer_provider = &padstack;
layer_provider.update(padstack);
render(padstack, edit);
request_push();
}

void Canvas::update(const Package &pkg, bool edit)
{
clear();
layer_provider = &pkg;
layer_provider.update(pkg);
render(pkg, edit);
request_push();
}

void Canvas::update(const Board &brd, PanelMode mode)
{
clear();
layer_provider = &brd;
layer_provider.update(brd);
render(brd, true, mode);
request_push();
}
void Canvas::update(const class Frame &fr, bool edit)
{
clear();
layer_provider = &fr;
layer_provider.update(fr);
render(fr, !edit);
request_push();
}

void Canvas::update(const class Decal &dec, bool edit)
{
clear();
layer_provider = &dec;
layer_provider.update(dec);
render(dec, edit);
request_push();
}

void Canvas::update(const class BlockSymbol &sym, bool edit)
{
clear();
layer_provider = &sym;
layer_provider.update(sym);
render(sym, !edit);
request_push();
}
Expand Down
3 changes: 2 additions & 1 deletion src/canvas/canvas.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "util/vector_pair.hpp"
#include "text_renderer.hpp"
#include "show_via_span.hpp"
#include "canvas_layer_provider.hpp"

namespace horizon {
class Canvas {
Expand Down Expand Up @@ -244,7 +245,7 @@ class Canvas {
std::vector<Target> targets;
Target target_current;

const class LayerProvider *layer_provider = nullptr;
CanvasLayerProvider layer_provider;
std::map<int, Color> layer_colors;
Color get_layer_color(int layer) const;
int work_layer = 0;
Expand Down
16 changes: 16 additions & 0 deletions src/canvas/canvas_layer_provider.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "canvas_layer_provider.hpp"

namespace horizon {

void CanvasLayerProvider::update(const LayerProvider &other)
{
layers = other.get_layers();
}

const std::map<int, Layer> &CanvasLayerProvider::get_layers() const
{
return layers;
}


} // namespace horizon
13 changes: 13 additions & 0 deletions src/canvas/canvas_layer_provider.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once
#include "common/layer_provider.hpp"

namespace horizon {
class CanvasLayerProvider : public LayerProvider {
public:
void update(const LayerProvider &other);
const std::map<int, Layer> &get_layers() const override;

private:
std::map<int, Layer> layers;
};
} // namespace horizon
2 changes: 1 addition & 1 deletion src/canvas/render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ void Canvas::render(const SchematicSymbol &sym)

void Canvas::render(const Text &text, bool interactive, ColorP co)
{
const bool rev = layer_provider->get_layers().at(text.layer).reverse;
const bool rev = layer_provider.get_layers().at(text.layer).reverse;
img_patch_type(PatchType::TEXT);
triangle_type_current = TriangleInfo::Type::TEXT;
img_text(&text);
Expand Down
14 changes: 5 additions & 9 deletions src/canvas/triangle_renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ void TriangleRenderer::render_layer_batch(int layer, HighlightMode highlight_mod
if (ca.appearance.colors.count(k))
buf.colors[i] = apply_highlight(ca.appearance.colors.at(k), highlight_mode, layer);
}
auto lc = ca.get_layer_color(ca.layer_provider ? ca.layer_provider->get_color_layer(layer) : layer);
auto lc = ca.get_layer_color(ca.layer_provider.get_color_layer(layer));
buf.colors[static_cast<int>(ColorP::AIRWIRE_ROUTER)] =
gl_array_from_color(ca.appearance.colors.at(ColorP::AIRWIRE_ROUTER));
buf.colors[static_cast<int>(ColorP::FROM_LAYER)] = apply_highlight(lc, highlight_mode, layer);
Expand Down Expand Up @@ -371,14 +371,10 @@ void TriangleRenderer::render()
layers.push_back(it.first);
}

if (ca.layer_provider) {
std::sort(layers.begin(), layers.end(), [this](auto la, auto lb) {
return ca.layer_provider->get_layer_position(la) < ca.layer_provider->get_layer_position(lb);
});
}
else {
std::sort(layers.begin(), layers.end());
}
std::sort(layers.begin(), layers.end(), [this](auto la, auto lb) {
return ca.layer_provider.get_layer_position(la) < ca.layer_provider.get_layer_position(lb);
});

if (ca.work_layer < 0) {
std::reverse(layers.begin(), layers.end());
}
Expand Down

0 comments on commit bd91a8d

Please sign in to comment.