-
Notifications
You must be signed in to change notification settings - Fork 59
/
drm-gbm.c
152 lines (130 loc) · 4.24 KB
/
drm-gbm.c
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
// gcc -o drm-gbm drm-gbm.c -ldrm -lgbm -lEGL -lGL -I/usr/include/libdrm
// general documentation: man drm
#include <xf86drm.h>
#include <xf86drmMode.h>
#include <gbm.h>
#include <EGL/egl.h>
#include <GL/gl.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#define EXIT(msg) { fputs (msg, stderr); exit (EXIT_FAILURE); }
static int device;
static drmModeConnector *find_connector (drmModeRes *resources) {
// iterate the connectors
int i;
for (i=0; i<resources->count_connectors; i++) {
drmModeConnector *connector = drmModeGetConnector (device, resources->connectors[i]);
// pick the first connected connector
if (connector->connection == DRM_MODE_CONNECTED) {
return connector;
}
drmModeFreeConnector (connector);
}
// no connector found
return NULL;
}
static drmModeEncoder *find_encoder (drmModeRes *resources, drmModeConnector *connector) {
if (connector->encoder_id) {
return drmModeGetEncoder (device, connector->encoder_id);
}
// no encoder found
return NULL;
}
static uint32_t connector_id;
static drmModeModeInfo mode_info;
static drmModeCrtc *crtc;
static void find_display_configuration () {
drmModeRes *resources = drmModeGetResources (device);
// find a connector
drmModeConnector *connector = find_connector (resources);
if (!connector) EXIT ("no connector found\n");
// save the connector_id
connector_id = connector->connector_id;
// save the first mode
mode_info = connector->modes[0];
printf ("resolution: %ix%i\n", mode_info.hdisplay, mode_info.vdisplay);
// find an encoder
drmModeEncoder *encoder = find_encoder (resources, connector);
if (!encoder) EXIT ("no encoder found\n");
// find a CRTC
if (encoder->crtc_id) {
crtc = drmModeGetCrtc (device, encoder->crtc_id);
}
drmModeFreeEncoder (encoder);
drmModeFreeConnector (connector);
drmModeFreeResources (resources);
}
static struct gbm_device *gbm_device;
static EGLDisplay display;
static EGLContext context;
static struct gbm_surface *gbm_surface;
static EGLSurface egl_surface;
static void setup_opengl () {
gbm_device = gbm_create_device (device);
display = eglGetDisplay (gbm_device);
eglInitialize (display, NULL, NULL);
// create an OpenGL context
eglBindAPI (EGL_OPENGL_API);
EGLint attributes[] = {
EGL_RED_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_BLUE_SIZE, 8,
EGL_NONE};
EGLConfig config;
EGLint num_config;
eglChooseConfig (display, attributes, &config, 1, &num_config);
context = eglCreateContext (display, config, EGL_NO_CONTEXT, NULL);
// create the GBM and EGL surface
gbm_surface = gbm_surface_create (gbm_device, mode_info.hdisplay, mode_info.vdisplay, GBM_BO_FORMAT_XRGB8888, GBM_BO_USE_SCANOUT|GBM_BO_USE_RENDERING);
egl_surface = eglCreateWindowSurface (display, config, gbm_surface, NULL);
eglMakeCurrent (display, egl_surface, egl_surface, context);
}
static struct gbm_bo *previous_bo = NULL;
static uint32_t previous_fb;
static void swap_buffers () {
eglSwapBuffers (display, egl_surface);
struct gbm_bo *bo = gbm_surface_lock_front_buffer (gbm_surface);
uint32_t handle = gbm_bo_get_handle (bo).u32;
uint32_t pitch = gbm_bo_get_stride (bo);
uint32_t fb;
drmModeAddFB (device, mode_info.hdisplay, mode_info.vdisplay, 24, 32, pitch, handle, &fb);
drmModeSetCrtc (device, crtc->crtc_id, fb, 0, 0, &connector_id, 1, &mode_info);
if (previous_bo) {
drmModeRmFB (device, previous_fb);
gbm_surface_release_buffer (gbm_surface, previous_bo);
}
previous_bo = bo;
previous_fb = fb;
}
static void draw (float progress) {
glClearColor (1.0f-progress, progress, 0.0, 1.0);
glClear (GL_COLOR_BUFFER_BIT);
swap_buffers ();
}
static void clean_up () {
// set the previous crtc
drmModeSetCrtc (device, crtc->crtc_id, crtc->buffer_id, crtc->x, crtc->y, &connector_id, 1, &crtc->mode);
drmModeFreeCrtc (crtc);
if (previous_bo) {
drmModeRmFB (device, previous_fb);
gbm_surface_release_buffer (gbm_surface, previous_bo);
}
eglDestroySurface (display, egl_surface);
gbm_surface_destroy (gbm_surface);
eglDestroyContext (display, context);
eglTerminate (display);
gbm_device_destroy (gbm_device);
}
int main () {
device = open ("/dev/dri/card0", O_RDWR|O_CLOEXEC);
find_display_configuration ();
setup_opengl ();
int i;
for (i = 0; i < 600; i++)
draw (i / 600.0f);
clean_up ();
close (device);
return 0;
}