Some refactoring of the fade out of notifications:

1) Use different wxWidgets call to get current time on Windows vs Unix
   for efficiency reasons.
2) Don't call this function multiple times in a single function, it is
   both expensive and not correct: One shall work with the same timestamp.
3) Added missing unbind of the new timer.
This commit is contained in:
Vojtech Bubnik 2020-12-16 16:15:58 +01:00
parent 08c4f674f7
commit 1dae057f15
4 changed files with 47 additions and 41 deletions

View file

@ -1612,7 +1612,7 @@ void GLCanvas3D::render()
#endif // ENABLE_ENVIRONMENT_MAP #endif // ENABLE_ENVIRONMENT_MAP
m_render_timer.Stop(); m_render_timer.Stop();
m_extra_frame_requested_delayed = std::numeric_limits<size_t>::max(); m_extra_frame_requested_delayed = std::numeric_limits<int>::max();
const Size& cnv_size = get_canvas_size(); const Size& cnv_size = get_canvas_size();
// Probably due to different order of events on Linux/GTK2, when one switched from 3D scene // Probably due to different order of events on Linux/GTK2, when one switched from 3D scene
@ -2381,6 +2381,7 @@ void GLCanvas3D::unbind_event_handlers()
m_canvas->Unbind(wxEVT_KEY_UP, &GLCanvas3D::on_key, this); m_canvas->Unbind(wxEVT_KEY_UP, &GLCanvas3D::on_key, this);
m_canvas->Unbind(wxEVT_MOUSEWHEEL, &GLCanvas3D::on_mouse_wheel, this); m_canvas->Unbind(wxEVT_MOUSEWHEEL, &GLCanvas3D::on_mouse_wheel, this);
m_canvas->Unbind(wxEVT_TIMER, &GLCanvas3D::on_timer, this); m_canvas->Unbind(wxEVT_TIMER, &GLCanvas3D::on_timer, this);
m_canvas->Unbind(EVT_GLCANVAS_RENDER_TIMER, &GLCanvas3D::on_render_timer, this);
m_canvas->Unbind(wxEVT_LEFT_DOWN, &GLCanvas3D::on_mouse, this); m_canvas->Unbind(wxEVT_LEFT_DOWN, &GLCanvas3D::on_mouse, this);
m_canvas->Unbind(wxEVT_LEFT_UP, &GLCanvas3D::on_mouse, this); m_canvas->Unbind(wxEVT_LEFT_UP, &GLCanvas3D::on_mouse, this);
m_canvas->Unbind(wxEVT_MIDDLE_DOWN, &GLCanvas3D::on_mouse, this); m_canvas->Unbind(wxEVT_MIDDLE_DOWN, &GLCanvas3D::on_mouse, this);
@ -2994,23 +2995,22 @@ void GLCanvas3D::on_render_timer(wxTimerEvent& evt)
m_dirty = true; m_dirty = true;
} }
void GLCanvas3D::request_extra_frame_delayed(wxLongLong miliseconds) void GLCanvas3D::request_extra_frame_delayed(int miliseconds)
{ {
int64_t now = timestamp_now();
if (! m_render_timer.IsRunning()) { if (! m_render_timer.IsRunning()) {
m_extra_frame_requested_delayed = miliseconds; m_extra_frame_requested_delayed = miliseconds;
m_render_timer.StartOnce((int)miliseconds.ToLong()); m_render_timer.StartOnce(miliseconds);
m_render_timer_start = wxGetLocalTimeMillis(); m_render_timer_start = now;
} else { } else {
const wxLongLong remaining_time = m_extra_frame_requested_delayed - (wxGetLocalTimeMillis() - m_render_timer_start); const int64_t remaining_time = (m_render_timer_start + m_extra_frame_requested_delayed) - now;
if (miliseconds < remaining_time) { if (miliseconds < remaining_time) {
m_render_timer.Stop(); m_render_timer.Stop();
m_extra_frame_requested_delayed = miliseconds; m_extra_frame_requested_delayed = miliseconds;
m_render_timer.StartOnce((int)miliseconds.ToLong()); m_render_timer.StartOnce(miliseconds);
m_render_timer_start = wxGetLocalTimeMillis(); m_render_timer_start = now;
} }
} }
} }
#ifndef NDEBUG #ifndef NDEBUG

View file

@ -349,7 +349,7 @@ class GLCanvas3D
public: public:
void add_frame(int64_t frame) { void add_frame(int64_t frame) {
int64_t now = wxGetLocalTimeMillis().GetValue(); int64_t now = GLCanvas3D::timestamp_now();
if (!m_frames.empty() && now - m_frames.front().first > 1000) { if (!m_frames.empty() && now - m_frames.front().first > 1000) {
m_curr_total -= m_frames.front().second; m_curr_total -= m_frames.front().second;
m_frames.pop(); m_frames.pop();
@ -454,14 +454,14 @@ private:
// when true renders an extra frame by not resetting m_dirty to false // when true renders an extra frame by not resetting m_dirty to false
// see request_extra_frame() // see request_extra_frame()
bool m_extra_frame_requested; bool m_extra_frame_requested;
wxLongLong m_extra_frame_requested_delayed { std::numeric_limits<wxLongLong>::max() }; int m_extra_frame_requested_delayed { std::numeric_limits<int>::max() };
bool m_event_handlers_bound{ false }; bool m_event_handlers_bound{ false };
mutable GLVolumeCollection m_volumes; mutable GLVolumeCollection m_volumes;
GCodeViewer m_gcode_viewer; GCodeViewer m_gcode_viewer;
RenderTimer m_render_timer; RenderTimer m_render_timer;
wxLongLong m_render_timer_start; int64_t m_render_timer_start;
Selection m_selection; Selection m_selection;
const DynamicPrintConfig* m_config; const DynamicPrintConfig* m_config;
@ -742,7 +742,7 @@ public:
void msw_rescale(); void msw_rescale();
void request_extra_frame() { m_extra_frame_requested = true; } void request_extra_frame() { m_extra_frame_requested = true; }
void request_extra_frame_delayed(wxLongLong miliseconds); void request_extra_frame_delayed(int miliseconds);
int get_main_toolbar_item_id(const std::string& name) const { return m_main_toolbar.get_item_id(name); } int get_main_toolbar_item_id(const std::string& name) const { return m_main_toolbar.get_item_id(name); }
void force_main_toolbar_left_action(int item_id) { m_main_toolbar.force_left_action(item_id, *this); } void force_main_toolbar_left_action(int item_id) { m_main_toolbar.force_left_action(item_id, *this); }
@ -773,10 +773,16 @@ public:
return ret; return ret;
} }
// Timestamp for FPS calculation and notification fade-outs.
static int64_t timestamp_now() {
#ifdef _WIN32
// Cheaper on Windows, calls GetSystemTimeAsFileTime()
return wxGetUTCTimeMillis().GetValue();
#else
// calls clock()
return wxGetLocalTimeMillis().GetValue();
#endif
}
private: private:
bool _is_shown_on_screen() const; bool _is_shown_on_screen() const;

View file

@ -139,7 +139,6 @@ NotificationManager::PopNotification::PopNotification(const NotificationData &n,
, m_hypertext (n.hypertext) , m_hypertext (n.hypertext)
, m_text2 (n.text2) , m_text2 (n.text2)
, m_evt_handler (evt_handler) , m_evt_handler (evt_handler)
// , m_notification_start (wxGetLocalTimeMillis())
{ {
//init(); //init();
} }
@ -152,7 +151,7 @@ void NotificationManager::PopNotification::render(GLCanvas3D& canvas, float init
} }
if (m_fading_out) if (m_fading_out)
m_last_render_fading = wxGetLocalTimeMillis(); m_last_render_fading = GLCanvas3D::timestamp_now();
Size cnv_size = canvas.get_canvas_size(); Size cnv_size = canvas.get_canvas_size();
ImGuiWrapper& imgui = *wxGetApp().imgui(); ImGuiWrapper& imgui = *wxGetApp().imgui();
@ -754,7 +753,7 @@ void NotificationManager::PopNotification::update_state()
if (!m_initialized) if (!m_initialized)
init(); init();
m_next_render = std::numeric_limits<wxLongLong>::max(); m_next_render = std::numeric_limits<int64_t>::max();
if (m_hidden) { if (m_hidden) {
m_state = EState::Hidden; m_state = EState::Hidden;
@ -766,21 +765,23 @@ void NotificationManager::PopNotification::update_state()
m_fading_out = false; m_fading_out = false;
m_current_fade_opacity = 1.0f; m_current_fade_opacity = 1.0f;
m_remaining_time = m_data.duration; m_remaining_time = m_data.duration;
// m_notification_start = wxGetLocalTimeMillis(); // m_notification_start = GLCanvas3D::timestamp_now();
// BOOST_LOG_TRIVIAL(error) << "hover"; // BOOST_LOG_TRIVIAL(error) << "hover";
} }
int64_t now = GLCanvas3D::timestamp_now();
if (m_counting_down) { if (m_counting_down) {
//wxMilliClock_t up_time = wxGetLocalTimeMillis() - m_notification_start; //int64_t up_time = GLCanvas3D::timestamp_now() - m_notification_start;
if (m_fading_out && m_current_fade_opacity <= 0.0f) if (m_fading_out && m_current_fade_opacity <= 0.0f)
m_finished = true; m_finished = true;
else if (!m_fading_out && m_remaining_time <=0/*up_time >= m_data.duration * 1000*/) { else if (!m_fading_out && m_remaining_time <=0/*up_time >= m_data.duration * 1000*/) {
m_fading_out = true; m_fading_out = true;
m_fading_start = wxGetLocalTimeMillis(); m_fading_start = now;
m_last_render_fading = wxGetLocalTimeMillis(); m_last_render_fading = now;
} else if (!m_fading_out) { } else if (!m_fading_out) {
m_next_render = std::min<wxLongLong>(/*m_data.duration * 1000 - up_time*/m_remaining_time * 1000, MAX_TIMEOUT_MILISECONDS); m_next_render = std::min<int64_t>(/*m_data.duration * 1000 - up_time*/m_remaining_time * 1000, MAX_TIMEOUT_MILISECONDS);
//BOOST_LOG_TRIVIAL(error) << (boost::format("up time %1% next render %2%") % up_time % m_next_render); //BOOST_LOG_TRIVIAL(error) << (boost::format("up time %1% next render %2%") % up_time % m_next_render);
} }
@ -802,12 +803,12 @@ void NotificationManager::PopNotification::update_state()
if (m_fading_out) { if (m_fading_out) {
if (!m_paused) { if (!m_paused) {
m_state = EState::FadingOutStatic; m_state = EState::FadingOutStatic;
wxMilliClock_t curr_time = wxGetLocalTimeMillis() - m_fading_start; int64_t curr_time = now - m_fading_start;
wxMilliClock_t no_render_time = wxGetLocalTimeMillis() - m_last_render_fading; int64_t no_render_time = now - m_last_render_fading;
m_current_fade_opacity = std::clamp(1.0f - 0.001f * static_cast<float>(curr_time.GetValue()) / FADING_OUT_DURATION, 0.0f, 1.0f); m_current_fade_opacity = std::clamp(1.0f - 0.001f * static_cast<float>(curr_time) / FADING_OUT_DURATION, 0.0f, 1.0f);
auto next_render = FADING_OUT_TIMEOUT - no_render_time; auto next_render = FADING_OUT_TIMEOUT - no_render_time;
if (next_render <= 0) { if (next_render <= 0) {
//m_last_render_fading = wxGetLocalTimeMillis(); //m_last_render_fading = GLCanvas3D::timestamp_now();
m_state = EState::FadingOutRender; m_state = EState::FadingOutRender;
m_next_render = 0; m_next_render = 0;
} else } else
@ -1425,16 +1426,16 @@ void NotificationManager::update_notifications()
m_requires_update = true; m_requires_update = true;
//request frames //request frames
wxLongLong next_render = std::numeric_limits<wxLongLong>::max(); int64_t next_render = std::numeric_limits<int64_t>::max();
const wxLongLong max = std::numeric_limits<wxLongLong>::max(); const int64_t max = std::numeric_limits<int64_t>::max();
for (const std::unique_ptr<PopNotification>& notification : m_pop_notifications) { for (const std::unique_ptr<PopNotification>& notification : m_pop_notifications) {
next_render = std::min<wxLongLong>(next_render, notification->next_render()); next_render = std::min<int64_t>(next_render, notification->next_render());
} }
if (next_render == 0) if (next_render == 0)
wxGetApp().plater()->get_current_canvas3D()->request_extra_frame(); wxGetApp().plater()->get_current_canvas3D()->request_extra_frame();
else if (next_render < max) else if (next_render < max)
wxGetApp().plater()->get_current_canvas3D()->request_extra_frame_delayed(next_render); wxGetApp().plater()->get_current_canvas3D()->request_extra_frame_delayed(int(next_render));
// actualizate timers // actualizate timers
wxWindow* p = dynamic_cast<wxWindow*>(wxGetApp().plater()); wxWindow* p = dynamic_cast<wxWindow*>(wxGetApp().plater());

View file

@ -242,8 +242,7 @@ private:
bool requires_render() const { return m_state == EState::FadingOutRender || m_state == EState::ClosePending || m_state == EState::Finished; } bool requires_render() const { return m_state == EState::FadingOutRender || m_state == EState::ClosePending || m_state == EState::Finished; }
bool requires_update() const { return m_state != EState::Hidden; } bool requires_update() const { return m_state != EState::Hidden; }
EState get_state() const { return m_state; } EState get_state() const { return m_state; }
wxLongLong next_render() const { return m_next_render; } int64_t next_render() const { return m_next_render; }
// void reset_start_time() { m_notification_start = wxGetLocalTimeMillis(); }
#endif // ENABLE_NEW_NOTIFICATIONS_FADE_OUT #endif // ENABLE_NEW_NOTIFICATIONS_FADE_OUT
protected: protected:
@ -299,13 +298,13 @@ private:
int m_countdown_frame { 0 }; int m_countdown_frame { 0 };
bool m_fading_out { false }; bool m_fading_out { false };
#if ENABLE_NEW_NOTIFICATIONS_FADE_OUT #if ENABLE_NEW_NOTIFICATIONS_FADE_OUT
wxLongLong m_fading_start { 0LL }; int64_t m_fading_start { 0LL };
// time of last done render when fading // time of last done render when fading
wxLongLong m_last_render_fading { 0LL }; int64_t m_last_render_fading { 0LL };
// first appereance of notification or last hover; // first appereance of notification or last hover;
// wxLongLong m_notification_start; // int64_t m_notification_start;
// time to next must-do render // time to next must-do render
wxLongLong m_next_render { std::numeric_limits<wxLongLong>::max() }; int64_t m_next_render { std::numeric_limits<int64_t>::max() };
#else #else
// total time left when fading beggins // total time left when fading beggins
float m_fading_time{ 0.0f }; float m_fading_time{ 0.0f };