Skip to content

Commit f711c33

Browse files
committed
Support Linux DRM backend
Implemented DRM-based framebuffer setup, including: - Opening DRM device - Creating dumb buffers and mapping them to framebuffers - Setting mode and handling CRTC for connected display output Currently, using the DRM legacy interface is suitable for Mado's backend if we only need basic window display without advanced features. The DRM legacy is simpler to implement. Close #60
1 parent 20d4707 commit f711c33

File tree

3 files changed

+313
-0
lines changed

3 files changed

+313
-0
lines changed

Makefile

+9
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,15 @@ libtwin.a_files-y += backend/fbdev.c
122122
libtwin.a_files-y += backend/linux_input.c
123123
endif
124124

125+
ifeq ($(CONFIG_BACKEND_DRM), y)
126+
BACKEND = drm
127+
TARGET_LIBS += -ludev
128+
libtwin.a_files-y += backend/drm.c
129+
libtwin.a_files-y += backend/linux_input.c
130+
libtwin.a_cflags-y += $(shell pkg-config --cflags libdrm)
131+
TARGET_LIBS += $(shell pkg-config --libs libdrm)
132+
endif
133+
125134
ifeq ($(CONFIG_BACKEND_VNC), y)
126135
BACKEND = vnc
127136
libtwin.a_files-y += backend/vnc.c

backend/drm.c

+300
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,300 @@
1+
/*
2+
* Twin - A Tiny Window System
3+
* Copyright (c) 2024 National Cheng Kung University, Taiwan
4+
* All rights reserved.
5+
*/
6+
7+
#include <fcntl.h>
8+
#include <libdrm/drm.h>
9+
#include <libdrm/drm_mode.h>
10+
#include <stdlib.h>
11+
#include <sys/ioctl.h>
12+
#include <sys/mman.h>
13+
#include <twin.h>
14+
#include <unistd.h>
15+
#include <xf86drm.h>
16+
#include <xf86drmMode.h>
17+
18+
#include "linux_input.h"
19+
#include "linux_vt.h"
20+
#include "twin_backend.h"
21+
22+
#define DRM_DRI_NAME "DRI_CARD"
23+
#define DRM_DRI_DEFAULT "/dev/dri/card0"
24+
#define SCREEN(x) ((twin_context_t *) x)->screen
25+
#define RES(x) ((twin_drm_t *) x)->res
26+
#define CONN(x) ((twin_drm_t *) x)->conn
27+
#define CRTC(x) ((twin_drm_t *) x)->crtc
28+
#define PRIV(x) ((twin_drm_t *) ((twin_context_t *) x)->priv)
29+
30+
typedef struct {
31+
twin_screen_t *screen;
32+
33+
/* Linux input system */
34+
void *input;
35+
36+
/* Linux virtual terminal (VT) */
37+
int vt_fd;
38+
39+
/* DRM driver */
40+
int width, height;
41+
int drm_dri_fd;
42+
drmModeResPtr res;
43+
drmModeConnectorPtr conn;
44+
drmModeCrtcPtr crtc;
45+
uint32_t fb_id;
46+
uint8_t *fb_base;
47+
size_t fb_len;
48+
} twin_drm_t;
49+
50+
static void _twin_drm_put_span(twin_coord_t left,
51+
twin_coord_t top,
52+
twin_coord_t right,
53+
twin_argb32_t *pixels,
54+
void *closure)
55+
{
56+
twin_drm_t *tx = PRIV(closure);
57+
58+
if (tx->fb_base == MAP_FAILED)
59+
return;
60+
61+
twin_coord_t width = right - left;
62+
off_t off = tx->width * top + left;
63+
uint32_t *dest =
64+
(uint32_t *) ((uintptr_t) tx->fb_base + (off * sizeof(*dest)));
65+
memcpy(dest, pixels, width * sizeof(*dest));
66+
}
67+
68+
static void twin_drm_get_screen_size(twin_drm_t *tx, int *width, int *height)
69+
{
70+
*width = CONN(tx)->modes[0].hdisplay;
71+
*height = CONN(tx)->modes[0].vdisplay;
72+
}
73+
74+
static bool twin_drm_work(void *closure)
75+
{
76+
twin_screen_t *screen = SCREEN(closure);
77+
78+
if (twin_screen_damaged(screen))
79+
twin_screen_update(screen);
80+
81+
return true;
82+
}
83+
84+
static bool get_resources(int fd, drmModeResPtr *resources)
85+
{
86+
*resources = drmModeGetResources(fd);
87+
if (*resources == NULL) {
88+
log_error("Failed to get resources");
89+
return false;
90+
}
91+
return true;
92+
}
93+
94+
static drmModeConnectorPtr find_drm_connector(int fd, drmModeResPtr resources)
95+
{
96+
drmModeConnectorPtr connector_ptr = NULL;
97+
98+
for (int i = 0; i < resources->count_connectors; i++) {
99+
connector_ptr = drmModeGetConnector(fd, resources->connectors[i]);
100+
if (connector_ptr && connector_ptr->connection == DRM_MODE_CONNECTED) {
101+
/* it's connected, let's use this! */
102+
break;
103+
}
104+
drmModeFreeConnector(connector_ptr);
105+
connector_ptr = NULL;
106+
}
107+
108+
return connector_ptr;
109+
}
110+
111+
static bool create_fb(twin_drm_t *tx)
112+
{
113+
/* Create dumb buffer */
114+
struct drm_mode_create_dumb create_req = {
115+
.width = tx->width,
116+
.height = tx->height,
117+
.bpp = 32,
118+
};
119+
if (ioctl(tx->drm_dri_fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_req) < 0) {
120+
log_error("Cannot create dumb buffer");
121+
return false;
122+
}
123+
tx->fb_len = create_req.size;
124+
125+
/* Create framebuffer object for the dumb-buffer */
126+
if (drmModeAddFB(tx->drm_dri_fd, tx->width, tx->height, 24, 32,
127+
create_req.pitch, create_req.handle, &tx->fb_id) != 0) {
128+
log_error("Cannot create framebubber");
129+
return false;
130+
}
131+
132+
/* Prepare buffer for memory mapping */
133+
struct drm_mode_map_dumb map_req = {
134+
.handle = create_req.handle,
135+
};
136+
if (ioctl(tx->drm_dri_fd, DRM_IOCTL_MODE_MAP_DUMB, &map_req) < 0) {
137+
log_error("Cannot map dumb buffer");
138+
goto bail_fb;
139+
}
140+
141+
/* Perform actual memory mapping */
142+
tx->fb_base = mmap(NULL, tx->fb_len, PROT_READ | PROT_WRITE, MAP_SHARED,
143+
tx->drm_dri_fd, map_req.offset);
144+
if (tx->fb_base == MAP_FAILED) {
145+
log_error("Failed to mmap framebuffer");
146+
goto bail_fb;
147+
}
148+
149+
return true;
150+
151+
bail_fb:
152+
drmModeRmFB(tx->drm_dri_fd, tx->fb_id);
153+
return false;
154+
}
155+
156+
static bool twin_drm_apply_config(twin_drm_t *tx)
157+
{
158+
/* Retrieve resources */
159+
if (!get_resources(tx->drm_dri_fd, &RES(tx)))
160+
return false;
161+
162+
/* Find a connected connector */
163+
CONN(tx) = find_drm_connector(tx->drm_dri_fd, RES(tx));
164+
if (!CONN(tx))
165+
goto bail_res;
166+
167+
/* Get the mode information */
168+
drmModeModeInfo mode = CONN(tx)->modes[0];
169+
tx->width = mode.hdisplay;
170+
tx->height = mode.vdisplay;
171+
172+
/* Create the framebuffer */
173+
if (!create_fb(tx))
174+
goto bail_conn;
175+
176+
/* Set the mode on a CRTC */
177+
CRTC(tx) = drmModeGetCrtc(tx->drm_dri_fd, RES(tx)->crtcs[0]);
178+
if (!CRTC(tx))
179+
goto bail_mmap;
180+
181+
if (drmModeSetCrtc(tx->drm_dri_fd, CRTC(tx)->crtc_id, tx->fb_id, 0, 0,
182+
&CONN(tx)->connector_id, 1, &mode) != 0) {
183+
log_error("Failed to set CRTC");
184+
goto bail_crtc;
185+
}
186+
187+
return true;
188+
189+
bail_crtc:
190+
drmModeFreeCrtc(CRTC(tx));
191+
bail_mmap:
192+
munmap(tx->fb_base, tx->fb_len);
193+
drmModeRmFB(tx->drm_dri_fd, tx->fb_id);
194+
bail_conn:
195+
drmModeFreeConnector(CONN(tx));
196+
bail_res:
197+
drmModeFreeResources(RES(tx));
198+
return false;
199+
}
200+
201+
twin_context_t *twin_drm_init(int width, int height)
202+
{
203+
/* Get environment variable to execute */
204+
char *drm_dri_path = getenv(DRM_DRI_NAME);
205+
if (!drm_dri_path) {
206+
log_info("Environment variable $DRI_CARD not set, use %s by default",
207+
DRM_DRI_DEFAULT);
208+
drm_dri_path = DRM_DRI_DEFAULT;
209+
}
210+
211+
twin_context_t *ctx = calloc(1, sizeof(twin_context_t));
212+
if (!ctx)
213+
return NULL;
214+
215+
ctx->priv = calloc(1, sizeof(twin_drm_t));
216+
if (!ctx->priv)
217+
return NULL;
218+
219+
twin_drm_t *tx = ctx->priv;
220+
221+
/* Open the DRM driver */
222+
tx->drm_dri_fd = open(drm_dri_path, O_RDWR);
223+
if (tx->drm_dri_fd < 0) {
224+
log_error("Failed to open %s", drm_dri_path);
225+
goto bail;
226+
}
227+
228+
/* Set up virtual terminal environment */
229+
if (!twin_vt_setup(&tx->vt_fd)) {
230+
goto bail_dri_card_fd;
231+
}
232+
233+
/* Apply configurations to the DRM driver*/
234+
if (!twin_drm_apply_config(tx)) {
235+
log_error("Failed to apply configurations to the DRM driver");
236+
goto bail_vt_fd;
237+
}
238+
239+
/* Create TWIN screen */
240+
ctx->screen =
241+
twin_screen_create(width, height, NULL, _twin_drm_put_span, ctx);
242+
243+
/* Create Linux input system object */
244+
tx->input = twin_linux_input_create(ctx->screen);
245+
if (!tx->input) {
246+
log_error("Failed to create Linux input system object");
247+
goto bail_screen;
248+
}
249+
250+
/* Setup file handler and work functions */
251+
twin_set_work(twin_drm_work, TWIN_WORK_REDISPLAY, ctx);
252+
253+
return ctx;
254+
255+
bail_screen:
256+
twin_screen_destroy(ctx->screen);
257+
bail_vt_fd:
258+
close(tx->vt_fd);
259+
bail_dri_card_fd:
260+
close(tx->drm_dri_fd);
261+
bail:
262+
free(ctx->priv);
263+
free(ctx);
264+
return NULL;
265+
}
266+
267+
static void twin_drm_configure(twin_context_t *ctx)
268+
{
269+
int width, height;
270+
twin_drm_t *tx = PRIV(ctx);
271+
twin_drm_get_screen_size(tx, &width, &height);
272+
twin_screen_resize(SCREEN(ctx), width, height);
273+
}
274+
275+
static void twin_drm_exit(twin_context_t *ctx)
276+
{
277+
if (!ctx)
278+
return;
279+
280+
twin_drm_t *tx = PRIV(ctx);
281+
twin_vt_mode(tx->vt_fd, KD_TEXT);
282+
drmModeFreeCrtc(CRTC(tx));
283+
munmap(tx->fb_base, tx->fb_len);
284+
drmModeRmFB(tx->drm_dri_fd, tx->fb_id);
285+
drmModeFreeConnector(CONN(tx));
286+
drmModeFreeResources(RES(tx));
287+
twin_linux_input_destroy(tx->input);
288+
close(tx->vt_fd);
289+
close(tx->drm_dri_fd);
290+
free(ctx->priv);
291+
free(ctx);
292+
}
293+
294+
/* Register the Linux DRM backend */
295+
296+
const twin_backend_t g_twin_backend = {
297+
.init = twin_drm_init,
298+
.configure = twin_drm_configure,
299+
.exit = twin_drm_exit,
300+
};

configs/Kconfig

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ config BACKEND_FBDEV
1212
bool "Linux framebuffer support"
1313
select CURSOR
1414

15+
config BACKEND_DRM
16+
bool "Linux DRM support"
17+
select CURSOR
18+
1519
config BACKEND_SDL
1620
bool "SDL video output support"
1721

0 commit comments

Comments
 (0)