summaryrefslogtreecommitdiff
path: root/src/helpers/sync/SyncTimeline.hpp
blob: ba65e004d0b5042a723229522389a062c37e0276 (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
#pragma once

#include <cstdint>
#include <optional>
#include <vector>
#include <functional>
#include <hyprutils/os/FileDescriptor.hpp>
#include "../memory/Memory.hpp"

/*
    Hyprland synchronization timelines are based on the wlroots' ones, which
    are based on Vk timeline semaphores: https://www.khronos.org/blog/vulkan-timeline-semaphores
*/

struct wl_event_source;

class CSyncTimeline {
  public:
    static SP<CSyncTimeline> create(int drmFD_);
    static SP<CSyncTimeline> create(int drmFD_, int drmSyncobjFD);
    ~CSyncTimeline();

    struct SWaiter {
        std::function<void()>          fn;
        wl_event_source*               source = nullptr;
        WP<CSyncTimeline>              timeline;
        Hyprutils::OS::CFileDescriptor eventFd;
    };

    // check if the timeline point has been signaled
    // flags: DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT or DRM_SYNCOBJ_WAIT_FLAGS_WAIT_AVAILABLE
    // std::nullopt on fail
    std::optional<bool>            check(uint64_t point, uint32_t flags);

    bool                           addWaiter(const std::function<void()>& waiter, uint64_t point, uint32_t flags);
    void                           removeWaiter(SWaiter*);
    Hyprutils::OS::CFileDescriptor exportAsSyncFileFD(uint64_t src);
    bool                           importFromSyncFileFD(uint64_t dst, Hyprutils::OS::CFileDescriptor& fd);
    bool                           transfer(SP<CSyncTimeline> from, uint64_t fromPoint, uint64_t toPoint);
    void                           signal(uint64_t point);

    int                            drmFD  = -1;
    uint32_t                       handle = 0;
    WP<CSyncTimeline>              self;

  private:
    CSyncTimeline() = default;

    std::vector<SP<SWaiter>> waiters;
};