summaryrefslogtreecommitdiff
path: root/src/syncobject.cpp
blob: 59c06fdfa991e64c7bbd2e669fae5c063f80d87d (plain) (blame)
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
/* SPDX-License-Identifier: MIT */

#include "syncobject.hpp"
#include "priv/ioctl.hpp"

#include <cstdint>
#include <optional>
#include <vector>

#include <unistd.h>

using namespace drm;

SyncObject::SyncObject(int fd, bool signal) : m_fd(fd) {
    drm_syncobj_create args{
        .flags = signal ? DRM_SYNCOBJ_CREATE_SIGNALED : 0U
    };
    ioctl::perform(this->m_fd, DRM_IOCTL_SYNCOBJ_CREATE, args);

    this->m_handle = args.handle;
}

SyncObject::SyncObject(int fd, int syncobj_fd, bool close) : m_fd(fd) {
    try {
        drm_syncobj_handle args{
            .fd = syncobj_fd
        };
        ioctl::perform(this->m_fd, DRM_IOCTL_SYNCOBJ_FD_TO_HANDLE, args);

        this->m_handle = args.handle;
    } catch (...) {
        if (close) {
            ::close(syncobj_fd);
        }

        throw;
    }

    if (close) {
        ::close(syncobj_fd);
    }
}

int SyncObject::exportFd() const {
    drm_syncobj_handle args{
        .handle = this->m_handle,
    };
    ioctl::perform(this->m_fd, DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD, args);

    return args.fd;
}

void SyncObject::importSyncFile(int syncfile_fd, bool close) const {
    try {
        drm_syncobj_handle args{
            .handle = this->m_handle,
            .flags = DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE,
            .fd = syncfile_fd
        };
        ioctl::perform(this->m_fd, DRM_IOCTL_SYNCOBJ_FD_TO_HANDLE, args);
    } catch (...) {
        if (close) {
            ::close(syncfile_fd);
        }

        throw;
    }

    if (close) {
        ::close(syncfile_fd);
    }
}

int SyncObject::exportSyncFile() const {
    drm_syncobj_handle args{
        .handle = this->m_handle,
        .flags = DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE
    };
    ioctl::perform(this->m_fd, DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD, args);

    return args.fd;
}

void SyncObject::signal(int fd, const std::vector<uint32_t>& objects) {
    if (objects.empty()) {
        return;
    }

    drm_syncobj_array args{
        .handles = reinterpret_cast<uint64_t>(objects.data()),
        .count_handles = static_cast<uint32_t>(objects.size())
    };
    ioctl::perform(fd, DRM_IOCTL_SYNCOBJ_SIGNAL, args);
}

void SyncObject::reset(int fd, const std::vector<uint32_t>& objects) {
    if (objects.empty()) {
        return;
    }

    drm_syncobj_array args{
        .handles = reinterpret_cast<uint64_t>(objects.data()),
        .count_handles = static_cast<uint32_t>(objects.size())
    };
    ioctl::perform(fd, DRM_IOCTL_SYNCOBJ_RESET, args);
}

uint32_t SyncObject::wait(
    int fd,
    const std::vector<uint32_t>& objects,
    int64_t timeout,
    bool waitAll,
    bool waitEmpty,
    bool waitAvailable,
    std::optional<uint64_t> deadlineHint
) {
    if (objects.empty()) {
        return 0;
    }

    if (waitAvailable) {
        waitEmpty = false;
    }

    drm_syncobj_wait args{
        .handles = reinterpret_cast<uint64_t>(objects.data()),
        .timeout_nsec = timeout,
        .count_handles = static_cast<uint32_t>(objects.size()),
        .flags =
            (waitAll   ? DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL : 0U) |
            (waitEmpty ? DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT : 0U) |
            (waitAvailable ? DRM_SYNCOBJ_WAIT_FLAGS_WAIT_AVAILABLE : 0U) |
            (deadlineHint.has_value() ? DRM_SYNCOBJ_WAIT_FLAGS_WAIT_DEADLINE : 0U),
        .deadline_nsec = deadlineHint.value_or(0)
    };
    ioctl::perform(fd, DRM_IOCTL_SYNCOBJ_WAIT, args);

    return args.first_signaled;
}

void SyncObject::destruct() noexcept {
    if (this->m_fd < 0) {
        return;
    }

    drm_syncobj_destroy args{
        .handle = this->m_handle
    };
    try {
        ioctl::perform(this->m_fd, DRM_IOCTL_SYNCOBJ_DESTROY, args);
    } catch(...) {
        (void) 0; // not much we can do about the leak
    }

    this->m_fd = -1;
}