Compare commits

...

6 Commits

Author SHA1 Message Date
f784f91798 add tracy and perform some markup 2024-03-16 16:42:23 +01:00
bde0f2c7c3 imgui debug stuff 2024-03-15 13:02:49 +01:00
83bbac2cd1 proper cleanup procedure for main 2024-03-15 13:02:37 +01:00
d5dede5a61 properly cleanup in main 2024-03-12 18:43:36 +01:00
719400068a cache the localtime result in the message
localtime uses global state AND spams allocations like cazy
2024-03-12 18:09:42 +01:00
aaf8c6adc1 save cooldown for tox profile (10sec after last) and save on exit 2024-03-12 18:09:21 +01:00
12 changed files with 200 additions and 32 deletions

View File

@ -1,5 +1,7 @@
cmake_minimum_required(VERSION 3.9 FATAL_ERROR)
add_subdirectory(./tracy)
add_subdirectory(./entt)
add_subdirectory(./solanaceae_util)

View File

@ -171,9 +171,10 @@ configure_file(
target_include_directories(toxcore PRIVATE "${TOX_DIR}toxcore")
target_include_directories(toxcore PUBLIC "${TOX_DIR}")
target_compile_definitions(toxcore PUBLIC USE_IPV6=1)
#target_compile_definitions(toxcore PUBLIC USE_IPV6=1)
#target_compile_definitions(toxcore PUBLIC MIN_LOGGER_LEVEL=LOGGER_LEVEL_DEBUG)
target_compile_definitions(toxcore PUBLIC MIN_LOGGER_LEVEL=LOGGER_LEVEL_INFO)
#target_compile_definitions(toxcore PUBLIC MIN_LOGGER_LEVEL=LOGGER_LEVEL_INFO)
target_compile_definitions(toxcore PUBLIC MIN_LOGGER_LEVEL=LOGGER_LEVEL_TRACE)
find_package(unofficial-sodium CONFIG QUIET)
find_package(sodium QUIET)

12
external/tracy/CMakeLists.txt vendored Normal file
View File

@ -0,0 +1,12 @@
cmake_minimum_required(VERSION 3.24 FATAL_ERROR)
include(FetchContent)
if (NOT TARGET TracyClient)
FetchContent_Declare(tracy
GIT_REPOSITORY https://github.com/wolfpld/tracy.git
GIT_TAG v0.10
)
FetchContent_MakeAvailable(tracy)
endif()

View File

@ -12,6 +12,14 @@
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs { inherit system; };
# get deps (move to lockfile or something)
# TODO: use system provided instead
tracy-src = pkgs.fetchFromGitHub {
owner = "wolfpld"; repo = "tracy";
rev = "v0.10";
hash = "sha256-DN1ExvQ5wcIUyhMAfiakFbZkDsx+5l8VMtYGvSdboPA=";
};
in {
packages.default = pkgs.stdenv.mkDerivation {
pname = "tomato";
@ -56,8 +64,11 @@
] ++ self.packages.${system}.default.dlopenBuildInputs;
cmakeFlags = [
"TOMATO_ASAN=1"
"CMAKE_BUILD_TYPE=RelWithDebInfo"
"-DTOMATO_ASAN=OFF"
"-DCMAKE_BUILD_TYPE=RelWithDebInfo"
"-DTRACY_ENABLE=ON"
"-DFETCHCONTENT_SOURCE_DIR_TRACY=${tracy-src}"
];
# TODO: replace with install command

View File

@ -82,6 +82,8 @@ target_link_libraries(tomato PUBLIC
solanaceae_tox_contacts
solanaceae_tox_messages
TracyClient
SDL3::SDL3
imgui

View File

@ -2,6 +2,8 @@
#include "./file_selector.hpp"
#include <tracy/Tracy.hpp>
#include <solanaceae/message3/components.hpp>
#include <solanaceae/tox_messages/components.hpp>
#include <solanaceae/contact/components.hpp>
@ -35,6 +37,16 @@ namespace Components {
float fade {1.f};
};
struct ConvertedTimeCache {
// calling localtime is expensive af
int tm_year {0};
int tm_yday {0};
int tm_mon {0};
int tm_mday {0};
int tm_hour {0};
int tm_min {0};
};
} // Components
static constexpr float lerp(float a, float b, float t) {
@ -139,6 +151,7 @@ ChatGui4::~ChatGui4(void) {
}
float ChatGui4::render(float time_delta) {
ZoneScoped;
_fss.render();
_sip.render(time_delta);
@ -324,9 +337,11 @@ float ChatGui4::render(float time_delta) {
//tmp_view.use<Message::Components::Timestamp>();
//tmp_view.each([&](const Message3 e, Message::Components::ContactFrom& c_from, Message::Components::ContactTo& c_to, Message::Components::Timestamp ts
//) {
uint64_t prev_ts {0};
//uint64_t prev_ts {0};
Components::ConvertedTimeCache prev_time {};
auto tmp_view = msg_reg.view<Message::Components::Timestamp>();
for (auto view_it = tmp_view.rbegin(), view_last = tmp_view.rend(); view_it != view_last; view_it++) {
ZoneScoped;
const Message3 e = *view_it;
// manually filter ("reverse" iteration <.<)
@ -342,15 +357,12 @@ float ChatGui4::render(float time_delta) {
// TODO: why?
ImGui::TableNextRow(0, TEXT_BASE_HEIGHT);
{ // check if date changed
// TODO: find defined ways of casting to time_t
std::time_t prev = prev_ts / 1000;
std::time_t next = ts.ts / 1000;
std::tm prev_tm = *std::localtime(&prev);
std::tm next_tm = *std::localtime(&next);
if (msg_reg.all_of<Components::ConvertedTimeCache>(e)) { // check if date changed
// TODO: move conversion up?
const auto& next_time = msg_reg.get<Components::ConvertedTimeCache>(e);
if (
prev_tm.tm_yday != next_tm.tm_yday ||
prev_tm.tm_year != next_tm.tm_year // making sure
prev_time.tm_yday != next_time.tm_yday ||
prev_time.tm_year != next_time.tm_year // making sure
) {
// name
if (ImGui::TableNextColumn()) {
@ -359,14 +371,14 @@ float ChatGui4::render(float time_delta) {
// msg
if (ImGui::TableNextColumn()) {
ImGui::TextDisabled("DATE CHANGED from %d.%d.%d to %d.%d.%d",
1900+prev_tm.tm_year, 1+prev_tm.tm_mon, prev_tm.tm_mday,
1900+next_tm.tm_year, 1+next_tm.tm_mon, next_tm.tm_mday
1900+prev_time.tm_year, 1+prev_time.tm_mon, prev_time.tm_mday,
1900+next_time.tm_year, 1+next_time.tm_mon, next_time.tm_mday
);
}
ImGui::TableNextRow(0, TEXT_BASE_HEIGHT);
}
prev_ts = ts.ts;
prev_time = next_time;
}
@ -519,12 +531,24 @@ float ChatGui4::render(float time_delta) {
// ts
if (ImGui::TableNextColumn()) {
auto time = std::chrono::system_clock::to_time_t(
std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds>{std::chrono::milliseconds{ts.ts}}
);
auto localtime = std::localtime(&time);
if (!msg_reg.all_of<Components::ConvertedTimeCache>(e)) {
auto time = std::chrono::system_clock::to_time_t(
std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds>{std::chrono::milliseconds{ts.ts}}
);
auto localtime = std::localtime(&time);
msg_reg.emplace<Components::ConvertedTimeCache>(
e,
localtime->tm_year,
localtime->tm_yday,
localtime->tm_mon,
localtime->tm_mday,
localtime->tm_hour,
localtime->tm_min
);
}
const auto& ctc = msg_reg.get<Components::ConvertedTimeCache>(e);
ImGui::Text("%.2d:%.2d", localtime->tm_hour, localtime->tm_min);
ImGui::Text("%.2d:%.2d", ctc.tm_hour, ctc.tm_min);
}
// extra
@ -657,6 +681,7 @@ void ChatGui4::sendFilePath(const char* file_path) {
// has MessageText
void ChatGui4::renderMessageBodyText(Message3Registry& reg, const Message3 e) {
ZoneScoped;
const auto& msgtext = reg.get<Message::Components::MessageText>(e).text;
// TODO: set word wrap
@ -699,6 +724,7 @@ void ChatGui4::renderMessageBodyText(Message3Registry& reg, const Message3 e) {
}
void ChatGui4::renderMessageBodyFile(Message3Registry& reg, const Message3 e) {
ZoneScoped;
if (
!_show_chat_avatar_tf
&& (
@ -958,6 +984,7 @@ void ChatGui4::renderMessageExtra(Message3Registry& reg, const Message3 e) {
}
void ChatGui4::renderContactList(void) {
ZoneScoped;
if (ImGui::BeginChild("contacts", {TEXT_BASE_WIDTH*35, 0})) {
//for (const auto& c : _cm.getBigContacts()) {
for (const auto& c : _cr.view<Contact::Components::TagBig>()) {
@ -1118,6 +1145,7 @@ bool ChatGui4::renderSubContactListContact(const Contact3 c, const bool selected
}
void ChatGui4::pasteFile(const char* mime_type) {
ZoneScoped;
size_t data_size = 0;
void* data = SDL_GetClipboardData(mime_type, &data_size);

View File

@ -1,6 +1,8 @@
#include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>
#include <tracy/Tracy.hpp>
#include <imgui/imgui.h>
#include <imgui/backends/imgui_impl_sdl3.h>
#include <imgui/backends/imgui_impl_sdlrenderer3.h>
@ -10,7 +12,6 @@
#include "./start_screen.hpp"
#include <memory>
#include <future>
#include <iostream>
#include <thread>
#include <chrono>
@ -29,8 +30,6 @@ int main(int argc, char** argv) {
std::cerr << "SDL_Init failed (" << SDL_GetError() << ")\n";
return 1;
}
// me just messing with RAII cleanup
auto sdl_scope = std::async(std::launch::deferred, &SDL_Quit);
// more RAII
std::unique_ptr<SDL_Window, decltype(&SDL_DestroyWindow)> window {
@ -86,15 +85,14 @@ int main(int argc, char** argv) {
}
ImGui_ImplSDL3_InitForSDLRenderer(window.get(), renderer.get());
auto imgui_sdl_scope = std::async(std::launch::deferred, &ImGui_ImplSDL3_Shutdown);
ImGui_ImplSDLRenderer3_Init(renderer.get());
auto imgui_sdlrenderer_scope = std::async(std::launch::deferred, &ImGui_ImplSDLRenderer3_Shutdown);
std::unique_ptr<Screen> screen = std::make_unique<StartScreen>(renderer.get());
bool quit = false;
while (!quit) {
ZoneScopedN("tomato::mainloop");
auto new_time = std::chrono::steady_clock::now();
const float time_delta_tick = std::chrono::duration<float, std::chrono::seconds::period>(new_time - last_time_tick).count();
@ -104,7 +102,10 @@ int main(int argc, char** argv) {
bool render = time_delta_render >= screen->nextRender();
if (tick) {
ZoneScopedN("tomato::tick");
FrameMarkStart("tomato::tick");
Screen* ret_screen = screen->tick(time_delta_tick, quit);
FrameMarkEnd("tomato::tick");
if (ret_screen != nullptr) {
screen.reset(ret_screen);
}
@ -131,6 +132,8 @@ int main(int argc, char** argv) {
// can do both in the same loop
if (render) {
ZoneScopedN("tomato::render");
FrameMarkStart("tomato::render");
ImGui_ImplSDLRenderer3_NewFrame();
ImGui_ImplSDL3_NewFrame();
ImGui::NewFrame();
@ -146,9 +149,11 @@ int main(int argc, char** argv) {
ImGui_ImplSDLRenderer3_RenderDrawData(ImGui::GetDrawData());
SDL_RenderPresent(renderer.get());
FrameMark;
// clearing after present is (should) more performant, but first frame is a mess
SDL_SetRenderDrawColor(renderer.get(), 0x10, 0x10, 0x10, SDL_ALPHA_OPAQUE);
SDL_RenderClear(renderer.get());
FrameMarkEnd("tomato::render");
last_time_render = new_time;
}
@ -213,6 +218,19 @@ int main(int argc, char** argv) {
#endif
}
// TODO: use scope for the unique ptrs
screen.reset();
ImGui_ImplSDLRenderer3_Shutdown();
ImGui_ImplSDL3_Shutdown();
ImGui::DestroyContext();
renderer.reset();
window.reset();
SDL_Quit();
return 0;
}

View File

@ -1,5 +1,7 @@
#include "./main_screen.hpp"
#include <tracy/Tracy.hpp>
#include <solanaceae/contact/components.hpp>
#include <imgui/imgui.h>
@ -155,6 +157,7 @@ bool MainScreen::handleEvent(SDL_Event& e) {
}
Screen* MainScreen::render(float time_delta, bool&) {
ZoneScoped;
// HACK: render the tomato main window first, with proper flags set.
// flags need to be set the first time begin() is called.
// and plugins are run before the main cg is run.
@ -215,9 +218,24 @@ Screen* MainScreen::render(float time_delta, bool&) {
ImGui::EndMenu();
}
if (ImGui::BeginMenu("Settings")) {
if (ImGui::MenuItem("ImGui Style Editor")) {
ImGui::SeparatorText("ImGui");
if (ImGui::MenuItem("Style Editor")) {
_show_tool_style_editor = true;
}
if (ImGui::MenuItem("Metrics")) {
_show_tool_metrics = true;
}
if (ImGui::MenuItem("Debug Log")) {
_show_tool_debug_log = true;
}
if (ImGui::MenuItem("ID Stack Tool")) {
_show_tool_id_stack = true;
}
ImGui::EndMenu();
}
ImGui::EndMenuBar();
@ -234,6 +252,18 @@ Screen* MainScreen::render(float time_delta, bool&) {
ImGui::End();
}
if (_show_tool_metrics) {
ImGui::ShowMetricsWindow(&_show_tool_metrics);
}
if (_show_tool_debug_log) {
ImGui::ShowDebugLogWindow(&_show_tool_debug_log);
}
if (_show_tool_id_stack) {
ImGui::ShowIDStackToolWindow(&_show_tool_id_stack);
}
if constexpr (false) {
ImGui::ShowDemoWindow();
}
@ -376,7 +406,8 @@ Screen* MainScreen::render(float time_delta, bool&) {
}
Screen* MainScreen::tick(float time_delta, bool& quit) {
quit = !tc.iterate(); // compute
ZoneScoped;
quit = !tc.iterate(time_delta); // compute
tcm.iterate(time_delta); // compute

View File

@ -76,6 +76,9 @@ struct MainScreen final : public Screen {
ToxDHTCapHisto tdch;
bool _show_tool_style_editor {false};
bool _show_tool_metrics {false};
bool _show_tool_debug_log {false};
bool _show_tool_id_stack {false};
bool _window_hidden {false};
uint64_t _window_hidden_ts {0};

View File

@ -1,6 +1,13 @@
#include "./tox_client.hpp"
#include "toxcore/tox.h"
// experimental
#include <cstdint>
#include <toxcore/tox_private.h>
#include <toxcore/mem.h>
#include <tracy/Tracy.hpp>
// meh, change this
#include <exception>
#include <system_error>
@ -68,6 +75,52 @@ ToxClient::ToxClient(std::string_view save_path, std::string_view save_password)
}
}
#if 1
// experimental tracy alloc tracking
static Tox_System g_mysystem = tox_default_system();
static const Memory* g_systemmemory = os_memory();
static Memory_Funcs g_mymemoryfuncs {
+[](void* obj, uint32_t size) {
auto ret = g_systemmemory->funcs->malloc(g_systemmemory->obj, size);
TracyAllocNS(ret, size, 32, "tox");
return ret;
},
+[](void* obj, uint32_t nmemb, uint32_t size) {
auto ret = g_systemmemory->funcs->calloc(g_systemmemory->obj, nmemb, size);
TracyAllocNS(ret, nmemb*size, 32, "tox");
return ret;
},
+[](void* obj, void* ptr, uint32_t size) {
TracyFreeNS(ptr, 32, "tox");
auto ret = g_systemmemory->funcs->realloc(g_systemmemory->obj, ptr, size);
TracyAllocNS(ret, size, 32, "tox");
return ret;
},
+[](void* obj, void* ptr) {
TracyFreeNS(ptr, 32, "tox");
g_systemmemory->funcs->free(g_systemmemory->obj, ptr);
}
};
static Memory g_mymemory {
&g_mymemoryfuncs,
nullptr
};
g_mysystem.mem = &g_mymemory;
tox_options_set_operating_system(options, &g_mysystem);
tox_options_set_log_callback(options, +[](Tox*, Tox_Log_Level level, const char*, uint32_t, const char*, const char* message, void*) {
std::string tmp_str;
tmp_str += "[";
tmp_str += tox_log_level_to_string(level);
tmp_str += "] ";
tmp_str += message;
TracyMessageS(tmp_str.c_str(), tmp_str.size(), 32);
});
#endif
tox_options_set_experimental_groups_persistence(options, true);
TOX_ERR_NEW err_new;
@ -121,10 +174,14 @@ ToxClient::ToxClient(std::string_view save_path, std::string_view save_password)
}
ToxClient::~ToxClient(void) {
if (_tox_profile_dirty) {
saveToxProfile();
}
tox_kill(_tox);
}
bool ToxClient::iterate(void) {
bool ToxClient::iterate(float time_delta) {
ZoneScoped;
Tox_Err_Events_Iterate err_e_it = TOX_ERR_EVENTS_ITERATE_OK;
auto* events = tox_events_iterate(_tox, false, &err_e_it);
if (err_e_it == TOX_ERR_EVENTS_ITERATE_OK && events != nullptr) {
@ -136,7 +193,8 @@ bool ToxClient::iterate(void) {
tox_events_free(events);
if (_tox_profile_dirty) {
_save_heat -= time_delta;
if (_tox_profile_dirty && _save_heat <= 0.f) {
saveToxProfile();
}
@ -180,5 +238,6 @@ void ToxClient::saveToxProfile(void) {
}
_tox_profile_dirty = false;
_save_heat = 10.f;
}

View File

@ -22,6 +22,7 @@ class ToxClient : public ToxDefaultImpl, public ToxEventProviderBase {
std::string _tox_profile_path;
std::string _tox_profile_password;
bool _tox_profile_dirty {true}; // set in callbacks
float _save_heat {0.f};
public:
//ToxClient(/*const CommandLine& cl*/);
@ -34,7 +35,7 @@ class ToxClient : public ToxDefaultImpl, public ToxEventProviderBase {
void setDirty(void) { _tox_profile_dirty = true; }
// returns false when we shoul stop the program
bool iterate(void);
bool iterate(float time_delta);
void stop(void); // let it know it should exit
void setToxProfilePath(const std::string& new_path) { _tox_profile_path = new_path; }