summaryrefslogtreecommitdiff
path: root/src/managers/animation/AnimationManager.cpp
blob: 687f5f2c86e348bbc303d1140f742388a0c1b010 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#include "AnimationManager.hpp"
#include "../../Compositor.hpp"
#include "../../config/ConfigManager.hpp"
#include "../../desktop/DesktopTypes.hpp"
#include "../../helpers/AnimatedVariable.hpp"
#include "../../macros.hpp"
#include "../../config/ConfigValue.hpp"
#include "../../desktop/view/Window.hpp"
#include "../../desktop/view/LayerSurface.hpp"
#include "../eventLoop/EventLoopManager.hpp"
#include "../../helpers/varlist/VarList.hpp"
#include "../../render/Renderer.hpp"
#include "../../event/EventBus.hpp"

#include <hyprgraphics/color/Color.hpp>
#include <hyprutils/animation/AnimatedVariable.hpp>
#include <hyprutils/animation/AnimationManager.hpp>

static int wlTick(SP<CEventLoopTimer> self, void* data) {
    if (g_pAnimationManager)
        g_pAnimationManager->frameTick();

    return 0;
}

CHyprAnimationManager::CHyprAnimationManager() {
    m_animationTimer = makeShared<CEventLoopTimer>(std::chrono::microseconds(500), wlTick, nullptr);
    if (g_pEventLoopManager) // null in --verify-config mode
        g_pEventLoopManager->addTimer(m_animationTimer);

    addBezierWithName("linear", Vector2D(0.0, 0.0), Vector2D(1.0, 1.0));
}

template <Animable VarType>
static void updateVariable(CAnimatedVariable<VarType>& av, const float POINTY, bool warp = false) {
    if (warp || av.value() == av.goal()) {
        av.warp(true, false);
        return;
    }

    const auto DELTA = av.goal() - av.begun();
    av.value()       = av.begun() + DELTA * POINTY;
}

static void updateColorVariable(CAnimatedVariable<CHyprColor>& av, const float POINTY, bool warp) {
    if (warp || av.value() == av.goal()) {
        av.warp(true, false);
        return;
    }

    // convert both to OkLab, then lerp that, and convert back.
    // This is not as fast as just lerping rgb, but it's WAY more precise...
    // Use the CHyprColor cache for OkLab

    const auto&                L1 = av.begun().asOkLab();
    const auto&                L2 = av.goal().asOkLab();

    static const auto          lerp = [](const float one, const float two, const float progress) -> float { return one + ((two - one) * progress); };

    const Hyprgraphics::CColor lerped = Hyprgraphics::CColor::SOkLab{
        .l = lerp(L1.l, L2.l, POINTY),
        .a = lerp(L1.a, L2.a, POINTY),
        .b = lerp(L1.b, L2.b, POINTY),
    };

    av.value() = {lerped, lerp(av.begun().a, av.goal().a, POINTY)};
}

static SAnimationContext& getContext(Hyprutils::Animation::CBaseAnimatedVariable* pAV) {
    switch (pAV->m_Type) {
        case AVARTYPE_FLOAT: return dc<CAnimatedVariable<float>*>(pAV)->m_Context;
        case AVARTYPE_VECTOR: return dc<CAnimatedVariable<Vector2D>*>(pAV)->m_Context;
        case AVARTYPE_COLOR: return dc<CAnimatedVariable<CHyprColor>*>(pAV)->m_Context;
        default: UNREACHABLE();
    }
}

static void damageWindowForPolicies(PHLWINDOW pWindow, bool entire, bool border, bool shadow) {
    if (entire)
        g_pHyprRenderer->damageWindow(pWindow); // damageWindow already damages all decorations
    else {
        if (border) {
            const auto PDECO = pWindow->getDecorationByType(DECORATION_BORDER);
            PDECO->damageEntire();
        }
        if (shadow) {
            const auto PDECO = pWindow->getDecorationByType(DECORATION_SHADOW);
            PDECO->damageEntire();
        }
    }
}

static void preDamageWorkspace(PHLWORKSPACE pWorkspace, PHLMONITOR pMonitor) {
    // don't damage the whole monitor on workspace change, unless it's a special workspace, because dim/blur etc
    if (pWorkspace->m_isSpecialWorkspace)
        g_pHyprRenderer->damageMonitor(pMonitor);

    // TODO: just make this into a damn callback already vax...
    for (auto const& w : g_pCompositor->m_windows) {
        if (!w->m_isMapped || w->isHidden() || w->m_workspace != pWorkspace)
            continue;

        if (w->m_isFloating && !w->m_pinned) {
            // still doing the full damage hack for floating because sometimes when the window
            // goes through multiple monitors the last rendered frame is missing damage somehow??
            const CBox windowBoxNoOffset = w->getFullWindowBoundingBox();
            const CBox monitorBox        = {pMonitor->m_position, pMonitor->m_size};
            if (windowBoxNoOffset.intersection(monitorBox) != windowBoxNoOffset) // on edges between multiple monitors
                g_pHyprRenderer->damageWindow(w, true);
        }

        if (pWorkspace->m_isSpecialWorkspace)
            g_pHyprRenderer->damageWindow(w, true); // hack for special too because it can cross multiple monitors
    }

    // damage any workspace window that is on any monitor
    for (auto const& w : g_pCompositor->m_windows) {
        if (!validMapped(w) || w->m_workspace != pWorkspace || w->m_pinned)
            continue;

        g_pHyprRenderer->damageWindow(w);
    }
}

template <Animable VarType>
static void handleUpdate(CAnimatedVariable<VarType>& av, bool warp) {
    bool animationsDisabled = warp;

    if (auto w = av.m_Context.pWindow.lock()) {
        if (!w->m_monitor.lock())
            return;
        animationsDisabled = w->m_ruleApplicator->noAnim().valueOr(animationsDisabled);
    } else if (auto ws = av.m_Context.pWorkspace.lock()) {
        if (!ws->m_monitor.lock())
            return;
    } else if (auto ls = av.m_Context.pLayer.lock()) {
        if (!g_pCompositor->getMonitorFromVector(ls->m_realPosition->goal() + ls->m_realSize->goal() / 2.F))
            return;
        animationsDisabled = animationsDisabled || ls->m_ruleApplicator->noanim().valueOrDefault();
    }

    const auto SPENT   = av.getPercent();
    const auto PBEZIER = g_pAnimationManager->getBezier(av.getBezierName());
    const auto POINTY  = PBEZIER->getYForPoint(SPENT);
    const bool WARP    = animationsDisabled || SPENT >= 1.f;

    if constexpr (std::same_as<VarType, CHyprColor>)
        updateColorVariable(av, POINTY, WARP);
    else
        updateVariable<VarType>(av, POINTY, WARP);

    av.onUpdate();
}

void CHyprAnimationManager::tick() {
    static std::chrono::time_point lastTick = std::chrono::high_resolution_clock::now();
    m_lastTickTimeMs                        = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now() - lastTick).count() / 1000.0;
    lastTick                                = std::chrono::high_resolution_clock::now();

    static auto PANIMENABLED = CConfigValue<Config::INTEGER>("animations:enabled");

    if (m_vActiveAnimatedVariables.empty()) {
        tickDone();
        return;
    }

    const auto CPY = m_vActiveAnimatedVariables;

    // batch damage per owner to avoid redundant damage calls, otherwise
    // we could be damaging many many times too much
    struct SDamageOwner {
        PHLWINDOW    window;
        PHLWORKSPACE workspace;
        PHLLS        layer;
        PHLMONITOR   monitor;
        bool         entire = false;
        bool         border = false;
        bool         shadow = false;
    };

    std::vector<SDamageOwner> owners;
    owners.reserve(4);

    // Collect per-owner damage policies
    for (const auto& PAV : CPY) {
        if (!PAV)
            continue;

        const auto&   ctx = getContext(PAV.get());

        SDamageOwner* owner = nullptr;

        if (auto w = ctx.pWindow.lock()) {
            for (auto& o : owners) {
                if (o.window == w) {
                    owner = &o;
                    break;
                }
            }
            if (!owner) {
                auto monitor = w->m_monitor.lock();
                if (!monitor)
                    continue;
                owners.emplace_back(SDamageOwner{.window = w, .monitor = monitor});
                owner = &owners.back();
            }
        } else if (auto ws = ctx.pWorkspace.lock()) {
            for (auto& o : owners) {
                if (o.workspace == ws) {
                    owner = &o;
                    break;
                }
            }
            if (!owner) {
                auto monitor = ws->m_monitor.lock();
                if (!monitor)
                    continue;
                owners.emplace_back(SDamageOwner{.workspace = ws, .monitor = monitor});
                owner = &owners.back();
            }
        } else if (auto ls = ctx.pLayer.lock()) {
            for (auto& o : owners) {
                if (o.layer == ls) {
                    owner = &o;
                    break;
                }
            }
            if (!owner) {
                auto monitor = g_pCompositor->getMonitorFromVector(ls->m_realPosition->goal() + ls->m_realSize->goal() / 2.F);
                if (!monitor)
                    continue;
                owners.emplace_back(SDamageOwner{.layer = ls, .monitor = monitor});
                owner = &owners.back();
            }
        } else
            continue;

        switch (ctx.eDamagePolicy) {
            case AVARDAMAGE_ENTIRE: owner->entire = true; break;
            case AVARDAMAGE_BORDER: owner->border = true; break;
            case AVARDAMAGE_SHADOW: owner->shadow = true; break;
            default: break;
        }
    }

    // pre-damage each owner once (old state)
    for (const auto& owner : owners) {
        if (owner.window)
            damageWindowForPolicies(owner.window, owner.entire, owner.border, owner.shadow);
        else if (owner.workspace)
            preDamageWorkspace(owner.workspace, owner.monitor);
        else if (owner.layer) {
            CBox expandBox = CBox{owner.layer->m_realPosition->value(), owner.layer->m_realSize->value()};
            expandBox.expand(5);
            g_pHyprRenderer->damageBox(expandBox);
        }
    }

    // update all variable values
    for (const auto& PAV : CPY) {
        if (!PAV)
            continue;

        const auto LOCK = PAV.lock();
        bool       warp = !*PANIMENABLED || !PAV->enabled();

        switch (PAV->m_Type) {
            case AVARTYPE_FLOAT: {
                auto pTypedAV = dc<CAnimatedVariable<float>*>(PAV.get());
                RASSERT(pTypedAV, "Failed to upcast animated float");
                handleUpdate(*pTypedAV, warp);
            } break;
            case AVARTYPE_VECTOR: {
                auto pTypedAV = dc<CAnimatedVariable<Vector2D>*>(PAV.get());
                RASSERT(pTypedAV, "Failed to upcast animated Vector2D");
                handleUpdate(*pTypedAV, warp);
            } break;
            case AVARTYPE_COLOR: {
                auto pTypedAV = dc<CAnimatedVariable<CHyprColor>*>(PAV.get());
                RASSERT(pTypedAV, "Failed to upcast animated CHyprColor");
                handleUpdate(*pTypedAV, warp);
            } break;
            default: UNREACHABLE();
        }
    }

    // post-damage each owner once (new state) + schedule frames
    for (const auto& owner : owners) {
        if (owner.window)
            damageWindowForPolicies(owner.window, owner.entire, owner.border, owner.shadow);
        else if (owner.workspace) {
            if (owner.entire) {
                for (auto const& w : g_pCompositor->m_windows) {
                    if (!validMapped(w) || w->m_workspace != owner.workspace || w->m_pinned)
                        continue;

                    g_pHyprRenderer->damageWindow(w);
                }
            }
        } else if (owner.layer) {
            if (owner.entire) {
                if (owner.layer->m_layer <= 1)
                    owner.monitor->m_blurFBDirty = true;

                CBox expandBox = CBox{owner.layer->m_realPosition->value(), owner.layer->m_realSize->value()};
                expandBox.expand(5);
                g_pHyprRenderer->damageBox(expandBox);
            }
        }

        if (!owner.monitor->inFullscreenMode())
            g_pCompositor->scheduleFrameForMonitor(owner.monitor, Aquamarine::IOutput::AQ_SCHEDULE_ANIMATION);
    }

    tickDone();
}

void CHyprAnimationManager::frameTick() {
    onTicked();

    if (!shouldTickForNext())
        return;

    if UNLIKELY (!g_pCompositor->m_sessionActive || g_pCompositor->m_unsafeState ||
                 !std::ranges::any_of(g_pCompositor->m_monitors, [](const auto& mon) { return mon->m_enabled && mon->m_output; }))
        return;

    if (!m_lastTickValid || m_lastTickTimer.getMillis() >= 1.0f) {
        m_lastTickTimer.reset();
        m_lastTickValid = true;

        tick();
        Event::bus()->m_events.tick.emit();
    }

    if (shouldTickForNext())
        scheduleTick();
}

void CHyprAnimationManager::scheduleTick() {
    if (m_tickScheduled)
        return;

    m_tickScheduled = true;

    if (!m_animationTimer || !g_pEventLoopManager) {
        m_tickScheduled = false;
        return;
    }

    m_animationTimer->updateTimeout(std::chrono::milliseconds(1));
}

void CHyprAnimationManager::onTicked() {
    m_tickScheduled = false;
}

void CHyprAnimationManager::resetTickState() {
    m_lastTickValid = false;
    m_tickScheduled = false;
}

std::string CHyprAnimationManager::styleValidInConfigVar(const std::string& config, const std::string& style) {
    if (config.starts_with("window")) {
        if (style.starts_with("slide") || style == "gnome" || style == "gnomed")
            return "";
        else if (style.starts_with("popin")) {
            // try parsing
            float minPerc = 0.f;
            if (style.find('%') != std::string::npos) {
                try {
                    auto percstr = style.substr(style.find_last_of(' '));
                    minPerc      = std::stoi(percstr.substr(0, percstr.length() - 1));
                } catch (std::exception& e) { return "invalid minperc"; }

                return "";
            }

            minPerc; // fix warning

            return "";
        }

        return "unknown style";
    } else if (config.starts_with("workspaces") || config.starts_with("specialWorkspace")) {
        if (style == "slide" || style == "slidevert" || style == "fade")
            return "";
        else if (style.starts_with("slide")) {
            // try parsing
            float movePerc = 0.f;
            if (style.find('%') != std::string::npos) {
                try {
                    auto percstr = style.substr(style.find_last_of(' ') + 1);
                    movePerc     = std::stoi(percstr.substr(0, percstr.length() - 1));
                } catch (std::exception& e) { return "invalid movePerc"; }

                return "";
            }

            movePerc; // fix warning

            return "";
        }

        return "unknown style";
    } else if (config == "borderangle") {
        if (style == "loop" || style == "once")
            return "";
        return "unknown style";
    } else if (config.starts_with("layers")) {
        if (style.empty() || style == "fade" || style == "slide")
            return "";
        else if (style.starts_with("popin")) {
            // try parsing
            float minPerc = 0.f;
            if (style.find('%') != std::string::npos) {
                try {
                    auto percstr = style.substr(style.find_last_of(' '));
                    minPerc      = std::stoi(percstr.substr(0, percstr.length() - 1));
                } catch (std::exception& e) { return "invalid minperc"; }

                return "";
            }

            minPerc; // fix warning

            return "";
        }
        return "";
        return "unknown style";
    } else {
        return "animation has no styles";
    }

    return "";
}