-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDrmDisplay.cppm
More file actions
196 lines (170 loc) · 6.37 KB
/
DrmDisplay.cppm
File metadata and controls
196 lines (170 loc) · 6.37 KB
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
196
// Copyright (C) 2026 mxreal64
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://gnu.org>.
module;
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <xf86drm.h>
#include <xf86drmMode.h>
export module DrmDisplay;
import std;
export class DrmScreen {
private:
int drm_fd = -1;
uint32_t width = 0;
uint32_t height = 0;
uint32_t pitch = 0;
uint32_t fb_id = 0;
uint32_t handle = 0;
uint32_t crtc_id = 0;
uint32_t* pixel_buffer = nullptr;
size_t buffer_size = 0;
drmModeCrtc* saved_crtc = nullptr;
uint32_t connector_id = 0;
drmModeModeInfo mode{};
void cleanup() noexcept {
if (pixel_buffer && pixel_buffer != MAP_FAILED) {
::munmap(pixel_buffer, buffer_size);
pixel_buffer = nullptr;
}
if (drm_fd >= 0) {
if (saved_crtc) {
::drmModeSetCrtc(drm_fd, crtc_id, saved_crtc->buffer_id, saved_crtc->x, saved_crtc->y, &connector_id, 1, &saved_crtc->mode);
::drmModeFreeCrtc(saved_crtc);
saved_crtc = nullptr;
}
if (handle) {
struct drm_mode_destroy_dumb destroy_req { .handle = handle };
::drmIoctl(drm_fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_req);
handle = 0;
}
if (fb_id) {
::drmModeRmFB(drm_fd, fb_id);
fb_id = 0;
}
::close(drm_fd);
drm_fd = -1;
}
}
public:
DrmScreen() = default;
~DrmScreen() {
cleanup();
}
DrmScreen(const DrmScreen&) = delete;
DrmScreen& operator=(const DrmScreen&) = delete;
DrmScreen(DrmScreen&& other) noexcept { *this = std::move(other); }
DrmScreen& operator=(DrmScreen&& other) noexcept {
if (this != &other) {
cleanup();
drm_fd = std::exchange(other.drm_fd, -1);
width = std::exchange(other.width, 0);
height = std::exchange(other.height, 0);
pitch = std::exchange(other.pitch, 0);
fb_id = std::exchange(other.fb_id, 0);
handle = std::exchange(other.handle, 0);
crtc_id = std::exchange(other.crtc_id, 0);
pixel_buffer = std::exchange(other.pixel_buffer, nullptr);
buffer_size = std::exchange(other.buffer_size, 0);
saved_crtc = std::exchange(other.saved_crtc, nullptr);
connector_id = std::exchange(other.connector_id, 0);
mode = other.mode;
other.mode = {};
}
return *this;
}
bool init() {
drm_fd = ::open("/dev/dri/card0", O_RDWR | O_CLOEXEC);
if (drm_fd < 0) return false;
auto* resources = ::drmModeGetResources(drm_fd);
if (!resources) { ::close(drm_fd); drm_fd = -1; return false; }
drmModeConnector* connector = nullptr;
for (int i = 0; i < resources->count_connectors; ++i) {
connector = ::drmModeGetConnector(drm_fd, resources->connectors[i]);
if (connector->connection == DRM_MODE_CONNECTED && connector->count_modes > 0) {
break;
}
::drmModeFreeConnector(connector);
connector = nullptr;
}
if (!connector) {
::drmModeFreeResources(resources);
::close(drm_fd);
drm_fd = -1;
return false;
}
mode = connector->modes[0];
width = mode.hdisplay;
height = mode.vdisplay;
connector_id = connector->connector_id;
struct drm_mode_create_dumb create_req {};
create_req.width = width;
create_req.height = height;
create_req.bpp = 32;
if (::drmIoctl(drm_fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_req) < 0) {
::drmModeFreeConnector(connector);
::drmModeFreeResources(resources);
cleanup();
return false;
}
handle = create_req.handle;
buffer_size = create_req.size;
pitch = create_req.pitch;
if (::drmModeAddFB(drm_fd, width, height, 24, 32, pitch, handle, &fb_id) < 0) {
::drmModeFreeConnector(connector);
::drmModeFreeResources(resources);
cleanup();
return false;
}
struct drm_mode_map_dumb map_req {};
map_req.handle = handle;
if (::drmIoctl(drm_fd, DRM_IOCTL_MODE_MAP_DUMB, &map_req) < 0) {
::drmModeFreeConnector(connector);
::drmModeFreeResources(resources);
cleanup();
return false;
}
pixel_buffer = static_cast<uint32_t*>(::mmap(0, buffer_size, PROT_READ | PROT_WRITE, MAP_SHARED, drm_fd, map_req.offset));
if (pixel_buffer == MAP_FAILED) {
::drmModeFreeConnector(connector);
::drmModeFreeResources(resources);
cleanup();
return false;
}
crtc_id = resources->crtcs[0];
saved_crtc = ::drmModeGetCrtc(drm_fd, crtc_id);
if (::drmModeSetCrtc(drm_fd, crtc_id, fb_id, 0, 0, &connector_id, 1, &mode) < 0) {
::drmModeFreeConnector(connector);
::drmModeFreeResources(resources);
cleanup();
return false;
}
::drmModeFreeConnector(connector);
::drmModeFreeResources(resources);
return true;
}
void clear(uint32_t hex_color) {
if (!pixel_buffer) return;
std::fill_n(pixel_buffer, buffer_size / 4, hex_color);
}
void set_pixel(uint32_t x, uint32_t y, uint32_t hex_color) {
if (!pixel_buffer || x >= width || y >= height) return;
uint32_t pixel_index = (y * (pitch / 4)) + x;
pixel_buffer[pixel_index] = hex_color;
}
std::pair<uint32_t, uint32_t> get_resolution() const {
return {width, height};
}
};