Skip to content

Commit

Permalink
karm-ui: Cleanup the debug facility.
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepy-monax committed Jan 2, 2025
1 parent 7485ecd commit 90818e8
Show file tree
Hide file tree
Showing 17 changed files with 193 additions and 439 deletions.
69 changes: 2 additions & 67 deletions src/libs/karm-ui/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,84 +8,19 @@
#include "_embed.h"
#include "app.h"
#include "host.h"
#include "input.h"
#include "layout.h"

namespace Karm::Ui {

Child inspector(Child child) {
return hflow(
child | Ui::grow(),
separator(),
vflow(
4,
icon(Mdi::FLASK, 24, GRAY600) | Ui::insets(6) | Ui::center(),
separator(),
button(
[](auto &n) {
debugShowLayoutBounds = !debugShowLayoutBounds;
Ui::shouldLayout(n);
},
ButtonStyle::subtle(),
Mdi::RULER_SQUARE
),
button(
[](auto &n) {
debugShowRepaintBounds = !debugShowRepaintBounds;
Ui::shouldLayout(n);
},
ButtonStyle::subtle(),
Mdi::BRUSH
),
button(
[](auto &n) {
debugShowEmptyBounds = !debugShowEmptyBounds;
Ui::shouldLayout(n);
},
ButtonStyle::subtle(),
Mdi::BORDER_NONE_VARIANT
),
button(
[](auto &n) {
debugShowScrollBounds = !debugShowScrollBounds;
Ui::shouldLayout(n);
},
ButtonStyle::subtle(),
Mdi::ARROW_UP_DOWN
),
button(
[](auto &n) {
debugShowPerfGraph = !debugShowPerfGraph;
Ui::shouldLayout(n);
},
ButtonStyle::subtle(),
Mdi::CHART_HISTOGRAM
)
) |
Ui::insets(4) |
Ui::box({
.backgroundFill = GRAY800,
})
);
}

Res<> runApp(Sys::Context &ctx, Child root) {
auto &args = useArgs(ctx);
if (args.has("--debug"))
root = root | inspector;
Res<> runApp(Sys::Context &, Child root) {
return try$(_Embed::makeHost(root))->run();
}

void mountApp(Cli::Command &cmd, Slot rootSlot) {
Cli::Flag debugArg = Cli::flag(NONE, "debug"s, "Show debug inspector."s);
Cli::Flag mobileArg = Cli::flag(NONE, "mobile"s, "Show mobile layout."s);

cmd.option(debugArg);
cmd.option(mobileArg);
cmd.callbackAsync = [rootSlot = std::move(rootSlot), debugArg](Sys::Context &) -> Async::Task<> {
cmd.callbackAsync = [rootSlot = std::move(rootSlot)](Sys::Context &) -> Async::Task<> {
auto root = rootSlot();
if (debugArg)
root = root | inspector;
co_return co_try$(_Embed::makeHost(root))->run();
};
}
Expand Down
47 changes: 5 additions & 42 deletions src/libs/karm-ui/host.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@
#include <karm-app/host.h>
#include <karm-base/ring.h>
#include <karm-gfx/cpu/canvas.h>
#include <karm-sys/time.h>
#include <karm-text/loader.h>

#include "node.h"
#include "perf.h"

namespace Karm::Ui {

static constexpr auto FRAME_RATE = 60;
static constexpr auto FRAME_TIME = 1.0 / FRAME_RATE;

struct Host : public Node {
Child _root;
Opt<Res<>> _res;
Gfx::CpuCanvas _g;
Vec<Math::Recti> _dirty;
PerfGraph _perf;

bool _shouldLayout{};
bool _shouldAnimate{};
Expand All @@ -30,9 +32,7 @@ struct Host : public Node {

virtual Gfx::MutPixels mutPixels() = 0;

Gfx::Pixels pixels() {
return mutPixels();
}
Gfx::Pixels pixels() { return mutPixels(); }

virtual void flip(Slice<Math::Recti> regions) = 0;

Expand All @@ -54,38 +54,15 @@ struct Host : public Node {

_root->paint(g, r);

if (debugShowRepaintBounds) {
static auto hue = 0.0;
g.strokeStyle(Gfx::stroke(Gfx::hsvToRgb({hue, 1, 1}).withOpacity(0.5)).withWidth(4).withAlign(Gfx::INSIDE_ALIGN));
hue += 1;
if (hue > 360)
hue = 0;
g.stroke(r.cast<f64>());
}

if (debugShowPerfGraph)
_perf.paint(_g);

g.pop();
}

void paint() {
if (debugShowPerfGraph)
_dirty.pushBack({0, 0, 256, 100});

_g.begin(mutPixels());

_perf.record(PerfEvent::PAINT);
for (auto &d : _dirty) {
paint(_g, d);
}
auto elapsed = _perf.end();

static auto threshold = TimeSpan::fromMSecs(15);
if (elapsed > threshold) {
logWarn("Stutter detected, paint took {} for {} nodes, threshold raised", elapsed, debugNodeCount);
threshold = elapsed;
}

_g.end();

Expand All @@ -94,25 +71,11 @@ struct Host : public Node {
}

void layout(Math::Recti r) override {
_perf.record(PerfEvent::LAYOUT);
_root->layout(r);
auto elapsed = _perf.end();
static auto threshold = TimeSpan::fromMSecs(1);
if (elapsed > threshold) {
logWarn("Stutter detected, layout took {} for {} nodes, threshold raised", elapsed, debugNodeCount);
threshold = elapsed;
}
}

void event(App::Event &event) override {
_perf.record(PerfEvent::INPUT);
_root->event(event);
auto elapsed = _perf.end();
static auto threshold = TimeSpan::fromMSecs(1);
if (elapsed > threshold) {
logWarn("Stutter detected, event took {} for {} nodes, threshold raised", elapsed, debugNodeCount);
threshold = elapsed;
}
}

void bubble(App::Event &event) override {
Expand Down
4 changes: 0 additions & 4 deletions src/libs/karm-ui/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,6 @@ struct Input : public View<Input> {
text.paint(g);

g.pop();
if (debugShowLayoutBounds)
g.plot(bound(), Gfx::CYAN);
}

void event(App::Event &e) override {
Expand Down Expand Up @@ -391,8 +389,6 @@ struct SimpleInput : public View<SimpleInput> {
text.paint(g);

g.pop();
if (debugShowLayoutBounds)
g.plot(bound(), Gfx::CYAN);
}

void event(App::Event &e) override {
Expand Down
14 changes: 1 addition & 13 deletions src/libs/karm-ui/layout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,7 @@ struct Empty : public View<Empty> {
return _size;
}

void paint(Gfx::Canvas &g, Math::Recti) override {
if (debugShowEmptyBounds) {
auto b = bound();
g.plot(b, Gfx::WHITE.withOpacity(0.2));
g.plot(Math::Edgei{b.topStart(), b.bottomEnd()}, Gfx::WHITE.withOpacity(0.2));
g.plot(Math::Edgei{b.topEnd(), b.bottomStart()}, Gfx::WHITE.withOpacity(0.2));
}
}
void paint(Gfx::Canvas &, Math::Recti) override {}
};

Child empty(Math::Vec2i size) {
Expand Down Expand Up @@ -310,9 +303,6 @@ struct Insets : public ProxyNode<Insets> {

void paint(Gfx::Canvas &g, Math::Recti r) override {
child().paint(g, r);
if (debugShowLayoutBounds) {
g.plot(child().bound(), Gfx::LIME);
}
}

void layout(Math::Recti rect) override {
Expand Down Expand Up @@ -347,8 +337,6 @@ struct AspectRatio : public ProxyNode<AspectRatio> {

void paint(Gfx::Canvas &g, Math::Recti r) override {
child().paint(g, r);
if (debugShowLayoutBounds)
g.plot(child().bound(), Gfx::INDIGO);
}

Math::Vec2i size(Math::Vec2i s, Hint) override {
Expand Down
12 changes: 0 additions & 12 deletions src/libs/karm-ui/node.cpp

This file was deleted.

15 changes: 0 additions & 15 deletions src/libs/karm-ui/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,6 @@ enum struct Hint {
MAX,
};

extern bool debugShowLayoutBounds;
extern bool debugShowRepaintBounds;
extern bool debugShowEmptyBounds;
extern bool debugShowScrollBounds;
extern bool debugShowPerfGraph;
extern int debugNodeCount;

struct Node;

using Child = Strong<Node>;
Expand All @@ -51,14 +44,6 @@ struct Node : public App::Dispatch {
f64 dt;
};

Node() {
debugNodeCount++;
}

virtual ~Node() {
debugNodeCount--;
}

Key key() const {
return _key;
}
Expand Down
108 changes: 0 additions & 108 deletions src/libs/karm-ui/perf.h

This file was deleted.

Loading

0 comments on commit 90818e8

Please sign in to comment.