animationmgr: avoid redundant ticks

This commit is contained in:
Vaxry 2023-08-05 23:29:33 +02:00
parent b925f1b497
commit a077b7a92e
5 changed files with 52 additions and 9 deletions

View file

@ -82,8 +82,11 @@ float CAnimatedVariable::getCurveValue() {
}
void CAnimatedVariable::connectToActive() {
g_pAnimationManager->scheduleTick(); // otherwise the animation manager will never pick this up
if (!m_bIsConnectedToActive)
g_pAnimationManager->m_vActiveAnimatedVariables.push_back(this);
m_bIsConnectedToActive = true;
}

View file

@ -15,3 +15,7 @@ int CTimer::getMillis() {
float CTimer::getSeconds() {
return std::chrono::duration_cast<std::chrono::milliseconds>(getDuration()).count() / 1000.f;
}
const std::chrono::system_clock::time_point& CTimer::chrono() const {
return m_tpLastReset;
}

View file

@ -4,9 +4,10 @@
class CTimer {
public:
void reset();
float getSeconds();
int getMillis();
void reset();
float getSeconds();
int getMillis();
const std::chrono::system_clock::time_point& chrono() const;
private:
std::chrono::system_clock::time_point m_tpLastReset;

View file

@ -4,16 +4,15 @@
int wlTick(void* data) {
float refreshRate = g_pHyprRenderer->m_pMostHzMonitor ? g_pHyprRenderer->m_pMostHzMonitor->refreshRate : 60.f;
wl_event_source_timer_update(g_pAnimationManager->m_pAnimationTick, 1000 / refreshRate);
if (g_pCompositor->m_bSessionActive && g_pAnimationManager && g_pHookSystem &&
std::ranges::any_of(g_pCompositor->m_vMonitors, [](const auto& mon) { return mon->m_bEnabled && mon->output; })) {
g_pAnimationManager->tick();
EMIT_HOOK_EVENT("tick", nullptr);
}
if (g_pAnimationManager && g_pAnimationManager->shouldTickForNext())
g_pAnimationManager->scheduleTick();
return 0;
}
@ -40,10 +39,15 @@ void CAnimationManager::addBezierWithName(std::string name, const Vector2D& p1,
void CAnimationManager::tick() {
m_bTickScheduled = false;
static std::chrono::time_point lastTick = std::chrono::high_resolution_clock::now();
m_fLastTickTime = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now() - lastTick).count() / 1000.0;
lastTick = std::chrono::high_resolution_clock::now();
if (m_vActiveAnimatedVariables.empty())
return;
bool animGlobalDisabled = false;
static auto* const PANIMENABLED = &g_pConfigManager->getConfigValuePtr("animations:enabled")->intValue;
@ -244,8 +248,8 @@ void CAnimationManager::tick() {
const auto EXTENTS = PDECO->getWindowDecorationExtents();
wlr_box dmg = {PWINDOW->m_vRealPosition.vec().x - EXTENTS.topLeft.x, PWINDOW->m_vRealPosition.vec().y - EXTENTS.topLeft.y,
PWINDOW->m_vRealSize.vec().x + EXTENTS.topLeft.x + EXTENTS.bottomRight.x,
PWINDOW->m_vRealSize.vec().y + EXTENTS.topLeft.y + EXTENTS.bottomRight.y};
PWINDOW->m_vRealSize.vec().x + EXTENTS.topLeft.x + EXTENTS.bottomRight.x,
PWINDOW->m_vRealSize.vec().y + EXTENTS.topLeft.y + EXTENTS.bottomRight.y};
if (!*PSHADOWIGNOREWINDOW) {
// easy, damage the entire box
@ -504,3 +508,30 @@ CBezierCurve* CAnimationManager::getBezier(const std::string& name) {
std::unordered_map<std::string, CBezierCurve> CAnimationManager::getAllBeziers() {
return m_mBezierCurves;
}
bool CAnimationManager::shouldTickForNext() {
return !m_vActiveAnimatedVariables.empty();
}
void CAnimationManager::scheduleTick() {
if (m_bTickScheduled)
return;
m_bTickScheduled = true;
const auto PMOSTHZ = g_pHyprRenderer->m_pMostHzMonitor;
float refreshRate = PMOSTHZ ? PMOSTHZ->refreshRate : 60.f;
float refreshDelayMs = std::floor(1000.0 / refreshRate);
if (!PMOSTHZ) {
wl_event_source_timer_update(m_pAnimationTick, refreshDelayMs);
return;
}
const float SINCEPRES = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now() - PMOSTHZ->lastPresentationTimer.chrono()).count() / 1000.0;
const auto TOPRES = std::clamp(refreshDelayMs - SINCEPRES, 1.1f, 1000.f); // we can't send 0, that will disarm it
wl_event_source_timer_update(m_pAnimationTick, std::floor(TOPRES));
}

View file

@ -13,6 +13,8 @@ class CAnimationManager {
CAnimationManager();
void tick();
bool shouldTickForNext();
void scheduleTick();
void addBezierWithName(std::string, const Vector2D&, const Vector2D&);
void removeAllBeziers();
@ -42,6 +44,8 @@ class CAnimationManager {
std::unordered_map<std::string, CBezierCurve> m_mBezierCurves;
bool m_bTickScheduled = false;
// Anim stuff
void animationPopin(CWindow*, bool close = false, float minPerc = 0.f);
void animationSlide(CWindow*, std::string force = "", bool close = false);