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
|
#include "DMABuffer.hpp"
#include "WLBuffer.hpp"
#include "../../desktop/view/LayerSurface.hpp"
#include "../../render/Renderer.hpp"
#include "../../helpers/Format.hpp"
#include <hyprgraphics/egl/Egl.hpp>
#if defined(__linux__)
#include <linux/dma-buf.h>
#include <linux/sync_file.h>
#endif
#include <sys/ioctl.h>
using namespace Hyprutils::OS;
using namespace Hyprgraphics::Egl;
CDMABuffer::CDMABuffer(uint32_t id, wl_client* client, Aquamarine::SDMABUFAttrs const& attrs_) : m_attrs(attrs_) {
m_listeners.resourceDestroy = events.destroy.listen([this] {
closeFDs();
m_listeners.resourceDestroy.reset();
});
size = m_attrs.size;
m_resource = CWLBufferResource::create(makeShared<CWlBuffer>(client, 1, id));
m_opaque = isDrmFormatOpaque(m_attrs.format);
m_texture = g_pHyprRenderer->createTexture(m_attrs, m_opaque); // texture takes ownership of the eglImage
if UNLIKELY (!m_texture) {
Log::logger->log(Log::ERR, "CDMABuffer: failed to import EGLImage, retrying as implicit");
m_attrs.modifier = DRM_FORMAT_MOD_INVALID;
m_texture = g_pHyprRenderer->createTexture(m_attrs, m_opaque);
if UNLIKELY (!m_texture) {
Log::logger->log(Log::ERR, "CDMABuffer: failed to import EGLImage");
return;
}
}
m_success = m_texture->ok();
if UNLIKELY (!m_success)
Log::logger->log(Log::ERR, "Failed to create a dmabuf: texture is null");
}
CDMABuffer::~CDMABuffer() {
if (m_resource)
m_resource->sendRelease();
closeFDs();
}
Aquamarine::eBufferCapability CDMABuffer::caps() {
return Aquamarine::eBufferCapability::BUFFER_CAPABILITY_DATAPTR;
}
Aquamarine::eBufferType CDMABuffer::type() {
return Aquamarine::eBufferType::BUFFER_TYPE_DMABUF;
}
void CDMABuffer::update(const CRegion& damage) {
;
}
bool CDMABuffer::isSynchronous() {
return false;
}
Aquamarine::SDMABUFAttrs CDMABuffer::dmabuf() {
return m_attrs;
}
std::tuple<uint8_t*, uint32_t, size_t> CDMABuffer::beginDataPtr(uint32_t flags) {
// FIXME:
return {nullptr, 0, 0};
}
void CDMABuffer::endDataPtr() {
// FIXME:
}
bool CDMABuffer::good() {
return m_success;
}
void CDMABuffer::closeFDs() {
for (int i = 0; i < m_attrs.planes; ++i) {
if (m_attrs.fds[i] == -1)
continue;
close(m_attrs.fds[i]);
m_attrs.fds[i] = -1;
}
m_attrs.planes = 0;
}
static int doIoctl(int fd, unsigned long request, void* arg) {
int ret;
do {
ret = ioctl(fd, request, arg);
} while (ret == -1 && (errno == EINTR || errno == EAGAIN));
return ret;
}
// https://www.kernel.org/doc/html/latest/driver-api/dma-buf.html#c.dma_buf_export_sync_file
// returns a sync file that will be signalled when dmabuf is ready to be read
CFileDescriptor CDMABuffer::exportSyncFile() {
if (!good())
return {};
#if !defined(__linux__)
return {};
#else
std::vector<CFileDescriptor> syncFds;
syncFds.reserve(m_attrs.fds.size());
for (const auto& fd : m_attrs.fds) {
if (fd == -1)
continue;
// buffer readability checks are rather slow on some Intel laptops
// see https://gitlab.freedesktop.org/drm/intel/-/issues/9415
if (g_pHyprRenderer && !g_pHyprRenderer->isIntel()) {
if (CFileDescriptor::isReadable(fd))
continue;
}
dma_buf_export_sync_file request{
.flags = DMA_BUF_SYNC_READ,
.fd = -1,
};
if (doIoctl(fd, DMA_BUF_IOCTL_EXPORT_SYNC_FILE, &request) == 0)
syncFds.emplace_back(request.fd);
}
if (syncFds.empty())
return {};
CFileDescriptor syncFd;
for (auto& fd : syncFds) {
if (!syncFd.isValid()) {
syncFd = std::move(fd);
continue;
}
const std::string name = "merged release fence";
struct sync_merge_data data{
.name = {}, // zero-initialize name[]
.fd2 = fd.get(),
.fence = -1,
};
std::ranges::copy_n(name.c_str(), std::min(name.size() + 1, sizeof(data.name)), data.name);
if (doIoctl(syncFd.get(), SYNC_IOC_MERGE, &data) == 0)
syncFd = CFileDescriptor(data.fence);
else
syncFd = {};
}
return syncFd;
#endif
}
|