Skip to content

Commit

Permalink
Optimize timer coder that is used for hiding mouse pointer and toolba…
Browse files Browse the repository at this point in the history
…rs. Use lower timer resolution. Set timer for hiding toolbars only when a delay is applied, because UpdateToolbarsVisibility() is already called when moving the mouse, so no timer is needed in that situation.
  • Loading branch information
clsid2 committed Nov 22, 2021
1 parent 5705b1e commit f6c0e5b
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 21 deletions.
6 changes: 3 additions & 3 deletions src/mpc-hc/MainFrm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ void CMainFrame::EventCallback(MpcEvent ev)
// CMainFrame construction/destruction

CMainFrame::CMainFrame()
: m_timer32Hz(this, TIMER_32HZ, 32)
: m_timerHider(this, TIMER_HIDER, 200)
, m_timerOneTime(this, TIMER_ONETIME_START, TIMER_ONETIME_END - TIMER_ONETIME_START + 1)
, m_bUsingDXVA(false)
, m_HWAccelType(nullptr)
Expand Down Expand Up @@ -2413,8 +2413,8 @@ void CMainFrame::OnTimer(UINT_PTR nIDEvent)
}
}
break;
case TIMER_32HZ:
m_timer32Hz.NotifySubscribers();
case TIMER_HIDER:
m_timerHider.NotifySubscribers();
break;
case TIMER_DELAYEDSEEK:
KillTimer(TIMER_DELAYEDSEEK);
Expand Down
6 changes: 3 additions & 3 deletions src/mpc-hc/MainFrm.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,12 @@ class CMainFrame : public CFrameWnd, public CDropClient

DpiHelper m_dpi;

enum class Timer32HzSubscriber {
enum class TimerHiderSubscriber {
TOOLBARS_HIDER,
CURSOR_HIDER,
CURSOR_HIDER_D3DFS,
};
OnDemandTimer<Timer32HzSubscriber> m_timer32Hz;
OnDemandTimer<TimerHiderSubscriber> m_timerHider;

enum class TimerOneTimeSubscriber {
TOOLBARS_DELAY_NOTLOADED,
Expand All @@ -215,7 +215,7 @@ class CMainFrame : public CFrameWnd, public CDropClient
TIMER_STREAMPOSPOLLER2,
TIMER_STATS,
TIMER_UNLOAD_UNUSED_EXTERNAL_OBJECTS,
TIMER_32HZ,
TIMER_HIDER,
TIMER_WINDOW_FULLSCREEN,
TIMER_DELAYEDSEEK,
TIMER_ONETIME_START,
Expand Down
18 changes: 10 additions & 8 deletions src/mpc-hc/MainFrmControls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ CMainFrameControls::~CMainFrameControls()
{
DelayShowNotLoaded(false);
if (!m_zoneHideTicks.empty()) {
m_pMainFrame->m_timer32Hz.Unsubscribe(CMainFrame::Timer32HzSubscriber::TOOLBARS_HIDER);
m_pMainFrame->m_timerHider.Unsubscribe(CMainFrame::TimerHiderSubscriber::TOOLBARS_HIDER);
}
}

Expand Down Expand Up @@ -653,12 +653,14 @@ void CMainFrameControls::UpdateToolbarsVisibility()
autohideZone(DOCK_RIGHT);
}

const bool bNeedTimer = !m_zoneHideTicks.empty();
if (bNoTimer && bNeedTimer) {
m_pMainFrame->m_timer32Hz.Subscribe(CMainFrame::Timer32HzSubscriber::TOOLBARS_HIDER,
std::bind(&CMainFrameControls::UpdateToolbarsVisibility, this));
} else if (!bNoTimer && !bNeedTimer) {
m_pMainFrame->m_timer32Hz.Unsubscribe(CMainFrame::Timer32HzSubscriber::TOOLBARS_HIDER);
if (uTimeout > 0) {
const bool bNeedTimer = !m_zoneHideTicks.empty();
if (bNoTimer && bNeedTimer) {
m_pMainFrame->m_timerHider.Subscribe(CMainFrame::TimerHiderSubscriber::TOOLBARS_HIDER,
std::bind(&CMainFrameControls::UpdateToolbarsVisibility, this));
} else if (!bNoTimer && !bNeedTimer) {
m_pMainFrame->m_timerHider.Unsubscribe(CMainFrame::TimerHiderSubscriber::TOOLBARS_HIDER);
}
}

if (bRecalcLayout) {
Expand Down Expand Up @@ -799,6 +801,6 @@ void CMainFrameControls::LockHideZone(DockZone zone)
} else {
m_zoneHideTicks[zone] = dwNewHideTick;
}
m_pMainFrame->m_timer32Hz.Subscribe(CMainFrame::Timer32HzSubscriber::TOOLBARS_HIDER,
m_pMainFrame->m_timerHider.Subscribe(CMainFrame::TimerHiderSubscriber::TOOLBARS_HIDER,
std::bind(&CMainFrameControls::UpdateToolbarsVisibility, this));
}
8 changes: 4 additions & 4 deletions src/mpc-hc/MouseTouch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,16 +128,16 @@ void CMouse::StartMouseHider(const CPoint& screenPoint)
m_mouseHiderStartScreenPoint = screenPoint;
if (!m_bMouseHiderStarted) {
// periodic timer is used here intentionally, recreating timer after each mouse move is more expensive
auto t = m_bD3DFS ? CMainFrame::Timer32HzSubscriber::CURSOR_HIDER_D3DFS : CMainFrame::Timer32HzSubscriber::CURSOR_HIDER;
m_pMainFrame->m_timer32Hz.Subscribe(t, std::bind(&CMouse::MouseHiderCallback, this));
auto t = m_bD3DFS ? CMainFrame::TimerHiderSubscriber::CURSOR_HIDER_D3DFS : CMainFrame::TimerHiderSubscriber::CURSOR_HIDER;
m_pMainFrame->m_timerHider.Subscribe(t, std::bind(&CMouse::MouseHiderCallback, this));
m_bMouseHiderStarted = true;
}
m_dwMouseHiderStartTick = GetTickCount64();
}
void CMouse::StopMouseHider()
{
auto t = m_bD3DFS ? CMainFrame::Timer32HzSubscriber::CURSOR_HIDER_D3DFS : CMainFrame::Timer32HzSubscriber::CURSOR_HIDER;
m_pMainFrame->m_timer32Hz.Unsubscribe(t);
auto t = m_bD3DFS ? CMainFrame::TimerHiderSubscriber::CURSOR_HIDER_D3DFS : CMainFrame::TimerHiderSubscriber::CURSOR_HIDER;
m_pMainFrame->m_timerHider.Unsubscribe(t);
m_bMouseHiderStarted = false;
}
void CMouse::MouseHiderCallback()
Expand Down
10 changes: 7 additions & 3 deletions src/mpc-hc/TimerWrappers.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,13 @@ class OnDemandTimer
}

void NotifySubscribers() {
auto subscribers = m_subscribers;
for (const auto& kv : subscribers) {
kv.second();
if (m_pWnd->m_hWnd) {
auto subscribers = m_subscribers;
for (const auto& kv : subscribers) {
kv.second();
}
} else {
ASSERT(FALSE);
}
}

Expand Down

0 comments on commit f6c0e5b

Please sign in to comment.