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
|
/* SPDX-License-Identifier: MIT */
#include "drm++/gem.hpp"
#include "drm++/helper.hpp"
#include "drm++/ioctl.hpp"
#include <cassert>
#include <cerrno>
#include <stdexcept>
#include <system_error>
#include <utility>
#include <drm.h>
#include <drm_mode.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <unistd.h>
using namespace drm;
using namespace drm::gem;
/* GEM object class */
Object::Object(ObjectManager& manager, int dmabuf_fd, bool close) : m_manager(&manager) {
try {
drm_prime_handle args{
.fd = dmabuf_fd
};
ioctl::perform(this->m_manager->m_fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, args);
this->m_handle = args.handle;
this->m_manager->m_refcounts[this->m_handle]++;
} catch (...) {
if (close) {
::close(dmabuf_fd);
}
throw;
}
if (close) {
::close(dmabuf_fd);
}
}
int Object::exportFd(ExportFlags flags) const {
drm_prime_handle args{
.handle = this->m_handle,
.flags = static_cast<u32>(flags)
};
ioctl::perform(this->m_manager->m_fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, args);
return args.fd;
}
void Object::changeHandle(u32 handle) {
auto& refcounts{this->m_manager->m_refcounts};
if (refcounts[handle] > 0 || refcounts[this->m_handle] > 1) {
throw std::logic_error("GEM handle is still in use");
}
drm_gem_change_handle args{
.handle = this->m_handle,
.new_handle = handle
};
ioctl::perform(this->m_manager->m_fd, DRM_IOCTL_GEM_CHANGE_HANDLE, args);
std::exchange(refcounts[handle], refcounts[this->m_handle]);
this->m_handle = handle;
}
void Object::destruct() noexcept {
if (this->m_manager == nullptr) {
return;
}
auto& refcounts{this->m_manager->m_refcounts};
if (refcounts[this->m_handle] > 1) {
refcounts[this->m_handle]--;
this->m_manager = nullptr;
return;
}
drm_gem_close args{
.handle = this->m_handle
};
try {
ioctl::perform(this->m_manager->m_fd, DRM_IOCTL_GEM_CLOSE, args);
} catch(...) {
assert(false && "GEM close failed, memory leak likely");
}
this->m_manager = nullptr;
}
/* Dumb buffer class */
DumbBuffer::DumbBuffer(ObjectManager& manager, u32 width, u32 height, u32 bpp)
: Object(manager), m_width(width), m_height(height), m_bpp(bpp) {
drm_mode_create_dumb args{
.height = height,
.width = width,
.bpp = bpp
};
ioctl::perform(this->m_manager->m_fd, DRM_IOCTL_MODE_CREATE_DUMB, args);
this->m_handle = args.handle;
this->m_manager->m_refcounts[this->m_handle]++;
this->m_pitch = args.pitch;
this->m_size = args.size;
}
void* DumbBuffer::map() {
if (this->m_map != nullptr) {
return this->m_map;
}
drm_mode_map_dumb args{
.handle = this->m_handle
};
ioctl::perform(this->m_manager->m_fd, DRM_IOCTL_MODE_MAP_DUMB, args);
void* map{::mmap(nullptr, this->m_size, PROT_READ | PROT_WRITE, MAP_SHARED,
this->m_manager->m_fd, static_cast<off_t>(args.offset))};
if (map == MAP_FAILED) {
throw std::system_error(errno, std::generic_category(), "mmap failed");
}
this->m_map = map;
return this->m_map;
}
void DumbBuffer::destruct() noexcept {
if (this->m_manager == nullptr) {
return;
}
auto& refcounts{this->m_manager->m_refcounts};
if (refcounts[this->m_handle] > 1) {
refcounts[this->m_handle]--;
this->m_manager = nullptr;
return;
}
if (this->m_map != nullptr) {
::munmap(this->m_map, this->m_size);
this->m_map = nullptr;
}
drm_mode_destroy_dumb args{
.handle = this->m_handle
};
try {
ioctl::perform(this->m_manager->m_fd, DRM_IOCTL_MODE_DESTROY_DUMB, args);
} catch(...) {
assert(false && "Dumb buffer destroy failed, memory leak likely");
}
this->m_manager = nullptr;
}
|