-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay_drm.c
225 lines (172 loc) · 6.41 KB
/
display_drm.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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include <stdio.h>
#include <fcntl.h>
#include <drm/drm.h>
#include <drm/drm_mode.h>
#include <string.h>
#include <sys/ioctl.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/select.h>
#include <unistd.h>
#include "display_drm.h"
int drm_ioctl(int fd, int cmd, void * data){
int ret = ioctl(fd, cmd, data);
if(ret < 0){
printf("drm ioctl error");
exit(0);
}
return ret;
}
void drm_get_resources(int fd, struct drm_mode_card_res * res){
memset(res, 0, sizeof(struct drm_mode_card_res));
drm_ioctl(fd, DRM_IOCTL_MODE_GETRESOURCES, res);
if(res->count_connectors){
res->connector_id_ptr = (uint64_t)malloc(res->count_connectors * sizeof(int));
}
if(res->count_encoders){
res->encoder_id_ptr = (uint64_t)malloc(res->count_encoders * sizeof(int));
}
if(res->count_crtcs){
res->crtc_id_ptr = (uint64_t)malloc(res->count_crtcs * sizeof(int));
}
drm_ioctl(fd, DRM_IOCTL_MODE_GETRESOURCES, res);
}
void drm_get_connector(int fd, int connector_id, struct drm_mode_get_connector * connector){
memset(connector, 0, sizeof(struct drm_mode_get_connector));
connector->connector_id = connector_id;
drm_ioctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, connector);
if(connector->count_modes){
connector->modes_ptr = (uint64_t)malloc(connector->count_modes * sizeof(struct drm_mode_modeinfo));
}
if(connector->count_props){
connector->props_ptr = (uint64_t)malloc(connector->count_props * sizeof(uint32_t));
connector->prop_values_ptr = (uint64_t)malloc(connector->count_props * sizeof(uint64_t));
}
if(connector->count_encoders){
connector->encoders_ptr = (uint64_t)malloc(connector->count_encoders * sizeof(int));
}
drm_ioctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, connector);
}
void drm_get_encoder(int fd, int encoder_id, struct drm_mode_get_encoder * enc){
memset(enc, 0, sizeof(struct drm_mode_get_encoder));
enc->encoder_id = encoder_id;
drm_ioctl(fd, DRM_IOCTL_MODE_GETENCODER, enc);
}
void create_framebuffer(int fd, int width, int height, struct framebuffer * fb){
struct drm_mode_create_dumb dumb;
struct drm_mode_map_dumb mdumb;
struct drm_mode_fb_cmd fbcmd;
memset(&dumb, 0, sizeof(dumb));
memset(&fbcmd, 0, sizeof(fbcmd));
memset(&mdumb, 0, sizeof(mdumb));
dumb.width = width;
dumb.height = height;
dumb.bpp = 32;
drm_ioctl(fd, DRM_IOCTL_MODE_CREATE_DUMB, &dumb);
fbcmd.width = dumb.width;
fbcmd.height = dumb.height;
fbcmd.depth = 24;
fbcmd.bpp = dumb.bpp;
fbcmd.pitch = dumb.pitch;
fbcmd.handle = dumb.handle;
drm_ioctl(fd, DRM_IOCTL_MODE_ADDFB, &fbcmd);
mdumb.handle = dumb.handle;
drm_ioctl(fd, DRM_IOCTL_MODE_MAP_DUMB, &mdumb);
fb->addr = mmap(0, dumb.size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, mdumb.offset);
fb->size = dumb.size;
fb->fb_id = fbcmd.fb_id;
fb->height = height;
fb->width = width;
}
void drm_set_crtc(struct display * display){
struct drm_mode_crtc crtc;
memset(&crtc, 0, sizeof(struct drm_mode_crtc));
crtc.mode = display->mode;
crtc.crtc_id = display->crtc_id;
crtc.count_connectors = 1;
crtc.x = 0;
crtc.y = 0;
crtc.mode_valid = 1;
crtc.set_connectors_ptr = display->ptr_connector_id;
crtc.fb_id = display->fbs[0]->fb_id;
drm_ioctl(display->fd, DRM_IOCTL_MODE_SETCRTC, &crtc);
}
void drm_page_flip(int fd, int fb_id, int crtc_id){
struct drm_mode_crtc_page_flip flip;
flip.fb_id = fb_id;
flip.crtc_id = crtc_id;
flip.flags = DRM_MODE_PAGE_FLIP_EVENT;
flip.reserved = 0;
ioctl(fd, DRM_IOCTL_MODE_PAGE_FLIP, &flip);
}
int open_dev(){
int fd = open("/dev/dri/card0", O_RDWR | O_CLOEXEC);
return fd;
}
void start_display(struct display * display){
drm_set_crtc(display);
drm_page_flip(display->fd, display->fbs[0]->fb_id, display->crtc_id);
}
void create_framebuffers(struct display * display){
struct framebuffer ** fbs = (struct framebuffer **) malloc(sizeof(struct framebuffer *) * 2);
for(int i=0;i<2;i++){
fbs[i] = (struct framebuffer *)malloc(sizeof(struct framebuffer));
}
display->fbs = fbs;
create_framebuffer(display->fd, (display->mode).hdisplay, (display->mode).vdisplay, fbs[0]);
create_framebuffer(display->fd, (display->mode).hdisplay, (display->mode).vdisplay, fbs[1]);
}
void show_modes(int fd){
struct drm_mode_card_res res;
struct drm_mode_get_connector connector;
drm_get_resources(fd, &res);
int user_width, user_height;
int no = 0;
for(int i=0;i<res.count_connectors;i++){
int connector_id = *((int *)((res.connector_id_ptr) + (i * 4)));
drm_get_connector(fd, connector_id, &connector);
if(connector.connection != connected){
continue;
}
struct drm_mode_modeinfo * modes = (struct drm_mode_modeinfo *)connector.modes_ptr;
for(int i=0;i<connector.count_modes;i++){
printf("%d x %d\n", modes[i].hdisplay, modes[i].vdisplay);
}
}
}
struct display * choose_mode(int fd, int height, int width){
struct drm_mode_card_res res;
struct drm_mode_get_encoder enc;
struct drm_mode_get_connector connector;
struct drm_mode_modeinfo mode;
struct display * display = NULL;
drm_get_resources(fd, &res);
int user_width = width, user_height = height;
for(int i=0;i<res.count_connectors;i++){
int connector_id = *((int *)((res.connector_id_ptr) + (i * 4)));
drm_get_connector(fd, connector_id, &connector);
if(connector.connection != connected){
continue;
}
struct drm_mode_modeinfo * modes = (struct drm_mode_modeinfo *)connector.modes_ptr;
for(int i=0;i<connector.count_modes;i++){
if((modes[i].vdisplay == user_height && modes[i].hdisplay == user_width)){
drm_get_encoder(fd, connector.encoder_id, &enc);
display = (struct display *)malloc(sizeof(struct display));
mode = modes[i];
display->connector_id = connector_id;
display->encoder_id = connector.encoder_id;
display->crtc_id = enc.crtc_id;
display->mode = mode;
display->fd = fd;
display->size = 4 * height * width;
display->height = height;
display->width = width;
display->ptr_connector_id = (uint64_t)&(display->connector_id);
return display;
}
}
}
return display;
}