aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/helpers/sync/SyncTimeline.cpp
blob: 16b5bd92deb3b4593f9827a15b3b54595a745174 (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
#include "SyncTimeline.hpp"
#include "../../defines.hpp"
#include "../../managers/eventLoop/EventLoopManager.hpp"

#include <xf86drm.h>
#include <sys/eventfd.h>

SP<CSyncTimeline> CSyncTimeline::create(int drmFD_) {
    auto timeline   = SP<CSyncTimeline>(new CSyncTimeline);
    timeline->drmFD = drmFD_;
    timeline->self  = timeline;

    if (drmSyncobjCreate(drmFD_, 0, &timeline->handle)) {
        Debug::log(ERR, "CSyncTimeline: failed to create a drm syncobj??");
        return nullptr;
    }

    return timeline;
}

SP<CSyncTimeline> CSyncTimeline::create(int drmFD_, int drmSyncobjFD) {
    auto timeline   = SP<CSyncTimeline>(new CSyncTimeline);
    timeline->drmFD = drmFD_;
    timeline->self  = timeline;

    if (drmSyncobjFDToHandle(drmFD_, drmSyncobjFD, &timeline->handle)) {
        Debug::log(ERR, "CSyncTimeline: failed to create a drm syncobj from fd??");
        return nullptr;
    }

    return timeline;
}

CSyncTimeline::~CSyncTimeline() {
    if (handle == 0)
        return;

    drmSyncobjDestroy(drmFD, handle);
}

std::optional<bool> CSyncTimeline::check(uint64_t point, uint32_t flags) {
#ifdef __FreeBSD__
    constexpr int ETIME_ERR = ETIMEDOUT;
#else
    constexpr int ETIME_ERR = ETIME;
#endif

    uint32_t signaled = 0;
    int      ret      = drmSyncobjTimelineWait(drmFD, &handle, &point, 1, 0, flags, &signaled);
    if (ret != 0 && ret != -ETIME_ERR) {
        Debug::log(ERR, "CSyncTimeline::check: drmSyncobjTimelineWait failed");
        return std::nullopt;
    }

    return ret == 0;
}

static int handleWaiterFD(int fd, uint32_t mask, void* data) {
    auto waiter = (CSyncTimeline::SWaiter*)data;

    if (mask & (WL_EVENT_HANGUP | WL_EVENT_ERROR)) {
        Debug::log(ERR, "handleWaiterFD: eventfd error");
        return 0;
    }

    if (mask & WL_EVENT_READABLE) {
        uint64_t value = 0;
        if (read(fd, &value, sizeof(value)) <= 0)
            Debug::log(ERR, "handleWaiterFD: failed to read from eventfd");
    }

    wl_event_source_remove(waiter->source);
    waiter->source = nullptr;

    if (waiter->fn)
        waiter->fn();

    if (waiter->timeline)
        waiter->timeline->removeWaiter(waiter);

    return 0;
}

bool CSyncTimeline::addWaiter(const std::function<void()>& waiter, uint64_t point, uint32_t flags) {
    auto w      = makeShared<SWaiter>();
    w->fn       = waiter;
    w->timeline = self;

    int eventFD = eventfd(0, EFD_CLOEXEC);

    if (eventFD < 0) {
        Debug::log(ERR, "CSyncTimeline::addWaiter: failed to acquire an eventfd");
        return false;
    }

    drm_syncobj_eventfd syncobjEventFD = {
        .handle = handle,
        .flags  = flags,
        .point  = point,
        .fd     = eventFD,
    };

    if (drmIoctl(drmFD, DRM_IOCTL_SYNCOBJ_EVENTFD, &syncobjEventFD) != 0) {
        Debug::log(ERR, "CSyncTimeline::addWaiter: drmIoctl failed");
        close(eventFD);
        return false;
    }

    w->source = wl_event_loop_add_fd(g_pEventLoopManager->m_sWayland.loop, eventFD, WL_EVENT_READABLE, ::handleWaiterFD, w.get());
    if (!w->source) {
        Debug::log(ERR, "CSyncTimeline::addWaiter: wl_event_loop_add_fd failed");
        close(eventFD);
        return false;
    }

    waiters.emplace_back(w);

    return true;
}

void CSyncTimeline::removeWaiter(SWaiter* w) {
    if (w->source) {
        wl_event_source_remove(w->source);
        w->source = nullptr;
    }
    std::erase_if(waiters, [w](const auto& e) { return e.get() == w; });
}

int CSyncTimeline::exportAsSyncFileFD(uint64_t src) {
    int      sync = -1;

    uint32_t syncHandle = 0;
    if (drmSyncobjCreate(drmFD, 0, &syncHandle)) {
        Debug::log(ERR, "exportAsSyncFileFD: drmSyncobjCreate failed");
        return -1;
    }

    if (drmSyncobjTransfer(drmFD, syncHandle, 0, handle, src, 0)) {
        Debug::log(ERR, "exportAsSyncFileFD: drmSyncobjTransfer failed");
        drmSyncobjDestroy(drmFD, syncHandle);
        return -1;
    }

    if (drmSyncobjExportSyncFile(drmFD, syncHandle, &sync)) {
        Debug::log(ERR, "exportAsSyncFileFD: drmSyncobjExportSyncFile failed");
        drmSyncobjDestroy(drmFD, syncHandle);
        return -1;
    }

    drmSyncobjDestroy(drmFD, syncHandle);
    return sync;
}

bool CSyncTimeline::importFromSyncFileFD(uint64_t dst, int fd) {
    uint32_t syncHandle = 0;

    if (drmSyncobjCreate(drmFD, 0, &syncHandle)) {
        Debug::log(ERR, "importFromSyncFileFD: drmSyncobjCreate failed");
        return false;
    }

    if (drmSyncobjImportSyncFile(drmFD, syncHandle, fd)) {
        Debug::log(ERR, "importFromSyncFileFD: drmSyncobjImportSyncFile failed");
        drmSyncobjDestroy(drmFD, syncHandle);
        return false;
    }

    if (drmSyncobjTransfer(drmFD, handle, dst, syncHandle, 0, 0)) {
        Debug::log(ERR, "importFromSyncFileFD: drmSyncobjTransfer failed");
        drmSyncobjDestroy(drmFD, syncHandle);
        return false;
    }

    drmSyncobjDestroy(drmFD, syncHandle);
    return true;
}

bool CSyncTimeline::transfer(SP<CSyncTimeline> from, uint64_t fromPoint, uint64_t toPoint) {
    if (drmFD != from->drmFD) {
        Debug::log(ERR, "CSyncTimeline::transfer: cannot transfer timelines between gpus, {} -> {}", from->drmFD, drmFD);
        return false;
    }

    if (drmSyncobjTransfer(drmFD, handle, toPoint, from->handle, fromPoint, 0)) {
        Debug::log(ERR, "CSyncTimeline::transfer: drmSyncobjTransfer failed");
        return false;
    }

    return true;
}

void CSyncTimeline::signal(uint64_t point) {
    if (drmSyncobjTimelineSignal(drmFD, &handle, &point, 1))
        Debug::log(ERR, "CSyncTimeline::signal: drmSyncobjTimelineSignal failed");
}