Compare commits

...

12 Commits

Author SHA1 Message Date
7613065b4c
windows mono_time hotfix 2024-01-07 22:22:23 +01:00
be8ceb861c
1 sec cooldown for reduced fps mode 2024-01-07 22:20:40 +01:00
da0f59a3f5
try fix plugin rendering 2024-01-07 19:37:05 +01:00
000254320e
add ubuntu linux to cd 2024-01-07 18:54:18 +01:00
2fac7206d2
pull windows fixes 2024-01-07 18:05:36 +01:00
e8234f2a4a
properly seperate tick and render 2024-01-07 16:33:08 +01:00
a0ba0b39d8
add fps reduced mode 2024-01-06 18:23:06 +01:00
845967cf12
more main loop fiddling 2024-01-06 16:45:08 +01:00
92b58cbfa9
faster wakeup 2024-01-06 15:13:45 +01:00
5a0651eaf0
wip low fps mode for hidden states and powersave modes.
dont use yet, as all compute is still done in the render method
2024-01-06 14:38:21 +01:00
14fcaf1d7e
why is windows so bad 2024-01-05 16:32:37 +01:00
5a0252d8d0
start screen refactor 2024-01-05 14:47:08 +01:00
14 changed files with 301 additions and 85 deletions

View File

@ -10,6 +10,36 @@ env:
BUILD_TYPE: Release
jobs:
linux-ubuntu:
timeout-minutes: 10
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v3
with:
submodules: recursive
- name: Install Dependencies
run: sudo apt update && sudo apt -y install libsodium-dev
- name: Configure CMake
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}}
- name: Build
run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}} -j 4
- name: temp test
run: ${{github.workspace}}/build/bin/mono_time_test
- uses: actions/upload-artifact@v3
with:
name: ${{ github.event.repository.name }}-ubuntu20.04-x86_64
# TODO: do propper packing
path: |
${{github.workspace}}/build/bin/
windows:
timeout-minutes: 15

@ -1 +1 @@
Subproject commit 96bab0200f5b13671756abe7e3132ed78aaa2a40
Subproject commit 5ae3f07c3ac16c3255ed5b7792c17545c7667ac3

@ -1 +1 @@
Subproject commit 54084b5a53e1617ff9b0c225880b0f1d60fe65ea
Subproject commit 08b3d69c159ca93aaaf38b6ee06507aa0eefd403

View File

@ -162,7 +162,7 @@ Mono_Time *mono_time_new(const Memory *mem, mono_time_current_time_cb *current_t
#endif
mono_time->cur_time = 0;
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
#if defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) || defined(OS_WIN32)
// Maximum reproducibility. Never return time = 0.
mono_time->base_time = 1;
#else

View File

@ -51,7 +51,7 @@ ChatGui4::ChatGui4(
) : _conf(conf), _rmm(rmm), _cr(cr), _tal(_cr), _contact_tc(_tal, tu), _msg_tc(_mil, tu), _sip(tu) {
}
void ChatGui4::render(void) {
void ChatGui4::render(float time_delta) {
if (!_cr.storage<Contact::Components::TagAvatarInvalidate>().empty()) { // handle force-reloads for avatars
std::vector<Contact3> to_purge;
_cr.view<Contact::Components::TagAvatarInvalidate>().each([&to_purge](const Contact3 c) {
@ -66,7 +66,7 @@ void ChatGui4::render(void) {
_msg_tc.update();
_fss.render();
_sip.render();
_sip.render(time_delta);
const ImGuiViewport* viewport = ImGui::GetMainViewport();
ImGui::SetNextWindowPos(viewport->WorkPos);

View File

@ -46,7 +46,7 @@ class ChatGui4 {
);
public:
void render(void);
void render(float time_delta);
public:
bool any_unread {false};

View File

@ -21,7 +21,8 @@ int main(int argc, char** argv) {
std::cerr << "Failed to set '" << SDL_HINT_VIDEO_ALLOW_SCREENSAVER << "' to 1\n";
}
auto last_time = std::chrono::steady_clock::now();
auto last_time_render = std::chrono::steady_clock::now();
auto last_time_tick = std::chrono::steady_clock::now();
// actual setup
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
@ -68,9 +69,22 @@ int main(int argc, char** argv) {
bool quit = false;
while (!quit) {
auto new_time = std::chrono::steady_clock::now();
//const float time_delta {std::chrono::duration<float, std::chrono::seconds::period>(new_time - last_time).count()};
last_time = new_time;
const float time_delta_tick = std::chrono::duration<float, std::chrono::seconds::period>(new_time - last_time_tick).count();
const float time_delta_render = std::chrono::duration<float, std::chrono::seconds::period>(new_time - last_time_render).count();
bool tick = time_delta_tick >= screen->nextTick();
bool render = time_delta_render >= screen->nextRender();
if (tick) {
Screen* ret_screen = screen->tick(time_delta_tick, quit);
if (ret_screen != nullptr) {
screen.reset(ret_screen);
}
last_time_tick = new_time;
}
// do events outside of tick/render, so they can influence reported intervals
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_EVENT_QUIT) {
@ -88,34 +102,84 @@ int main(int argc, char** argv) {
break;
}
//float fps_target = 60.f;
//if (SDL_GetWindowFlags(window.get()) & (SDL_WINDOW_HIDDEN | SDL_WINDOW_MINIMIZED)) {
//fps_target = 30.f;
//}
// can do both in the same loop
if (render) {
ImGui_ImplSDLRenderer3_NewFrame();
ImGui_ImplSDL3_NewFrame();
ImGui::NewFrame();
ImGui_ImplSDLRenderer3_NewFrame();
ImGui_ImplSDL3_NewFrame();
ImGui::NewFrame();
{ // render
Screen* ret_screen = screen->render(time_delta_render, quit);
if (ret_screen != nullptr) {
screen.reset(ret_screen);
}
}
//ImGui::ShowDemoWindow();
ImGui::Render();
ImGui_ImplSDLRenderer3_RenderDrawData(ImGui::GetDrawData());
Screen* ret_screen = screen->poll(quit);
if (ret_screen != nullptr) {
screen.reset(ret_screen);
SDL_RenderPresent(renderer.get());
// 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());
last_time_render = new_time;
}
ImGui::Render();
ImGui_ImplSDLRenderer3_RenderDrawData(ImGui::GetDrawData());
//// TODO: seperate out render and tick
//const float time_to_next_loop = std::min<float>(screen->nextRender(), screen->nextTick());
SDL_RenderPresent(renderer.get());
// 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());
//std::this_thread::sleep_for( // time left to get to 60fps
//std::chrono::duration<float, std::chrono::seconds::period>(time_to_next_loop)
//- std::chrono::duration<float, std::chrono::seconds::period>(std::chrono::steady_clock::now() - new_time) // time used for rendering
//);
std::this_thread::sleep_for( // time left to get to 60fps
std::chrono::duration<float, std::chrono::seconds::period>(0.0166f) // 60fps frame duration
- std::chrono::duration<float, std::chrono::seconds::period>(std::chrono::steady_clock::now() - new_time) // time used for rendering
);
#if 1
if (render || tick) {
// why is windows like this
//std::this_thread::sleep_for(std::chrono::milliseconds(1)); // yield for 1ms
SDL_Delay(1); // yield for 1ms
} else {
#if 0
// pretty hacky and spins if close to next update
// if next loop >= 1ms away, wait 1ms
if (time_delta_tick+0.001f < screen->nextTick() && time_delta_render+0.001f < screen->nextRender()) {
std::this_thread::sleep_for(std::chrono::milliseconds(1)); // yield for 1ms
}
#else
// dynamic sleep, sleeps the reminder till next update
//std::this_thread::sleep_for(std::chrono::duration<float, std::chrono::seconds::period>(
//std::min<float>(
//screen->nextTick() - time_delta_tick,
//screen->nextRender() - time_delta_render
//)
//));
const float min_delay = std::min<float>(
screen->nextTick() - time_delta_tick,
screen->nextRender() - time_delta_render
) * 1000.f;
if (min_delay > 0.f) {
SDL_Delay(uint32_t(min_delay));
}
// better in theory, but consumes more cpu on linux for some reason
//SDL_WaitEventTimeout(nullptr, int32_t(
//std::min<float>(
//screen->nextTick() - time_delta_tick,
//screen->nextRender() - time_delta_render
//) * 1000.f
//));
#endif
}
#else
// why is windows like this
//std::this_thread::sleep_for(std::chrono::milliseconds(1)); // yield for 1ms
SDL_Delay(1); // yield for 1ms
#endif
}
return 0;

View File

@ -75,23 +75,78 @@ bool MainScreen::handleEvent(SDL_Event& e) {
if (e.type == SDL_EVENT_DROP_FILE) {
std::cout << "DROP FILE: " << e.drop.file << "\n";
cg.sendFilePath(e.drop.file);
_render_interval = 1.f/60.f; // TODO: magic
_time_since_event = 0.f;
return true; // TODO: forward return succ from sendFilePath()
}
if (
e.type == SDL_EVENT_WINDOW_MINIMIZED ||
e.type == SDL_EVENT_WINDOW_HIDDEN ||
e.type == SDL_EVENT_WINDOW_OCCLUDED // does this trigger on partial occlusion?
) {
auto* window = SDL_GetWindowFromID(e.window.windowID);
auto* event_renderer = SDL_GetRenderer(window);
if (event_renderer != nullptr && event_renderer == renderer) {
// our window is now obstructed
if (_window_hidden_ts < e.window.timestamp) {
_window_hidden_ts = e.window.timestamp;
_window_hidden = true;
//std::cout << "TOMAT: window hidden " << e.window.timestamp << "\n";
}
}
return true; // forward?
}
if (
e.type == SDL_EVENT_WINDOW_SHOWN ||
e.type == SDL_EVENT_WINDOW_RESTORED ||
e.type == SDL_EVENT_WINDOW_EXPOSED
) {
auto* window = SDL_GetWindowFromID(e.window.windowID);
auto* event_renderer = SDL_GetRenderer(window);
if (event_renderer != nullptr && event_renderer == renderer) {
if (_window_hidden_ts <= e.window.timestamp) {
_window_hidden_ts = e.window.timestamp;
if (_window_hidden) {
// if window was previously hidden, we shorten the wait for the next frame
_render_interval = 1.f/60.f;
}
_window_hidden = false;
//std::cout << "TOMAT: window shown " << e.window.timestamp << "\n";
}
}
_render_interval = 1.f/60.f; // TODO: magic
_time_since_event = 0.f;
return true; // forward?
}
if (
_fps_perf_mode <= 1 && (
// those are all the events imgui polls
e.type == SDL_EVENT_MOUSE_MOTION ||
e.type == SDL_EVENT_MOUSE_WHEEL ||
e.type == SDL_EVENT_MOUSE_BUTTON_DOWN ||
e.type == SDL_EVENT_MOUSE_BUTTON_UP ||
e.type == SDL_EVENT_TEXT_INPUT ||
e.type == SDL_EVENT_KEY_DOWN ||
e.type == SDL_EVENT_KEY_UP ||
e.type == SDL_EVENT_WINDOW_MOUSE_ENTER ||
e.type == SDL_EVENT_WINDOW_MOUSE_LEAVE ||
e.type == SDL_EVENT_WINDOW_FOCUS_GAINED ||
e.type == SDL_EVENT_WINDOW_FOCUS_LOST
)
) {
_render_interval = 1.f/60.f; // TODO: magic
_time_since_event = 0.f;
}
return false;
}
Screen* MainScreen::poll(bool& quit) {
auto new_time = std::chrono::high_resolution_clock::now();
const float time_delta {std::chrono::duration<float, std::chrono::seconds::period>(new_time - last_time).count()};
last_time = new_time;
quit = !tc.iterate();
tcm.iterate(time_delta);
tam.iterate();
Screen* MainScreen::render(float time_delta, bool&) {
// 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.
@ -109,48 +164,87 @@ Screen* MainScreen::poll(bool& quit) {
ImGui::End();
}
pm.tick(time_delta);
tdch.tick(time_delta);
const float pm_interval = pm.render(time_delta); // render
mts.iterate();
cg.render(time_delta); // render
sw.render(); // render
tuiu.render(); // render
tdch.render(); // render
{ // main window menubar injection
if (ImGui::Begin("tomato")) {
if (ImGui::BeginMenuBar()) {
// ImGui::Separator(); // why do we not need this????
if (ImGui::BeginMenu("Performance")) {
{ // fps
const auto targets = "normal\0reduced\0powersave\0";
ImGui::SetNextItemWidth(ImGui::GetFontSize()*10);
ImGui::Combo("fps mode", &_fps_perf_mode, targets, 4);
}
{ // compute
const auto targets = "normal\0powersave\0";
ImGui::SetNextItemWidth(ImGui::GetFontSize()*10);
ImGui::Combo("compute mode", &_compute_perf_mode, targets, 4);
ImGui::SetItemTooltip("Limiting compute can slow down things like filetransfers!");
}
ImGui::EndMenu();
}
ImGui::EndMenuBar();
}
}
ImGui::End();
}
cg.render();
sw.render();
tuiu.render();
tdch.render();
if constexpr (false) {
ImGui::ShowDemoWindow();
}
#if 0
{ // texture tests
const size_t width = 8;
const size_t height = 8;
#define W 0xff, 0xff, 0xff, 0xff
#define B 0x00, 0x00, 0x00, 0xff
#define P 0xff, 0x00, 0xff, 0xff
static uint8_t raw_pixel[width*height*4] {
P, W, W, W, W, W, W, P,
W, W, W, W, W, W, W, W,
W, W, W, W, W, W, W, W,
W, W, W, B, B, W, W, W,
W, W, W, B, B, W, W, W,
W, W, W, W, W, W, W, W,
W, W, W, W, W, W, W, W,
P, W, W, W, W, W, W, P,
};
static uint64_t texture = sdlrtu.uploadRGBA(raw_pixel, width, height);
if (ImGui::Begin("test texture")) {
ImGui::Text("test texture windoajsdf");
ImGui::Image(reinterpret_cast<void*>(texture), {width*10, height*10});
}
ImGui::End();
if (
_fps_perf_mode > 1 // TODO: magic
) {
// powersave forces 250ms
_render_interval = 1.f/4.f;
} else if (
_time_since_event > 1.f && ( // 1sec cool down
_fps_perf_mode == 1 || // TODO: magic
_window_hidden
)
) {
_render_interval = std::min<float>(1.f/4.f, pm_interval);
} else {
_render_interval = std::min<float>(1.f/60.f, pm_interval);
}
#endif
_time_since_event += time_delta;
return nullptr;
}
Screen* MainScreen::tick(float time_delta, bool& quit) {
quit = !tc.iterate(); // compute
tcm.iterate(time_delta); // compute
tam.iterate(); // compute
const float pm_interval = pm.tick(time_delta); // compute
tdch.tick(time_delta); // compute
mts.iterate(); // compute
_min_tick_interval = std::max<float>(
std::min<float>(
tc.toxIterationInterval()/1000.f,
pm_interval
//0.03f // HACK: 30ms upper bound, should be the same as tox but will change
),
(_compute_perf_mode == 0 ? 0.001f : 0.1f) // in powersave fix the lowerbound to 100ms
);
return nullptr;
}

View File

@ -38,8 +38,6 @@ extern "C" {
struct MainScreen final : public Screen {
SDL_Renderer* renderer;
std::chrono::high_resolution_clock::time_point last_time = std::chrono::high_resolution_clock::now();
SimpleConfigModel conf;
Contact3Registry cr;
RegistryMessageModel rmm;
@ -66,6 +64,10 @@ struct MainScreen final : public Screen {
ToxUIUtils tuiu;
ToxDHTCapHisto tdch;
bool _window_hidden {false};
bool _window_hidden_ts {0};
float _time_since_event {0.f};
MainScreen(SDL_Renderer* renderer_, std::string save_path, std::string save_password, std::vector<std::string> plugins);
~MainScreen(void);
@ -73,6 +75,18 @@ struct MainScreen final : public Screen {
// return nullptr if not next
// sets bool quit to true if exit
Screen* poll(bool&) override;
Screen* render(float time_delta, bool&) override;
Screen* tick(float time_delta, bool&) override;
// 0 - normal
// 1 - power save
int _fps_perf_mode {0};
int _compute_perf_mode {0};
float _render_interval {1.f/60.f};
float _min_tick_interval {0.f};
float nextRender(void) override { return _render_interval; }
float nextTick(void) override { return _min_tick_interval; }
};

View File

@ -2,14 +2,21 @@
#include <SDL3/SDL.h>
// all time values are in seconds
struct Screen {
virtual ~Screen(void) = default;
// return true if handled
virtual bool handleEvent(SDL_Event& e) { return false; }
virtual bool handleEvent(SDL_Event&) { return false; }
// return nullptr if not next
// sets bool quit to true if exit
virtual Screen* poll(bool& quit) = 0;
// both render and tick get called in the selfreported intervals
virtual Screen* render(float time_delta, bool& quit) = 0; // pure since this is a graphical app
virtual Screen* tick(float time_delta, bool& quit) = 0;
// TODO: const?
virtual float nextRender(void) { return 1.f/60.f; }
virtual float nextTick(void) { return 0.03f; }
};

View File

@ -158,7 +158,7 @@ void SendImagePopup::sendMemory(
}
void SendImagePopup::render(void) {
void SendImagePopup::render(float time_delta) {
if (_open_popup) {
_open_popup = false;
ImGui::OpenPopup("send image##SendImagePopup");
@ -171,7 +171,7 @@ void SendImagePopup::render(void) {
preview_image.doAnimation(getNowMS());
time += 1.f/60.f; // TODO: actual delay
time += time_delta;
time = fmod(time, 1.f); // fract()
//ImGui::Text("send file....\n......");

View File

@ -71,6 +71,6 @@ struct SendImagePopup {
// from file_path
// call this each frame
void render(void);
void render(float time_delta);
};

View File

@ -11,7 +11,7 @@
StartScreen::StartScreen(SDL_Renderer* renderer) : _renderer(renderer) {
}
Screen* StartScreen::poll(bool&) {
Screen* StartScreen::render(float, bool&) {
// TODO: imgui tox profile selector?
@ -137,3 +137,7 @@ Screen* StartScreen::poll(bool&) {
return nullptr;
}
Screen* StartScreen::tick(float, bool&) {
return nullptr;
}

View File

@ -30,6 +30,9 @@ struct StartScreen final : public Screen {
// return nullptr if not next
// sets bool quit to true if exit
Screen* poll(bool&) override;
Screen* render(float, bool&) override;
Screen* tick(float, bool&) override;
// use default nextRender and nextTick
};