-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cc
executable file
·238 lines (183 loc) · 5.36 KB
/
main.cc
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
226
227
228
229
230
231
232
233
234
235
236
237
#include <iostream>
/*#include "demo.h"
int main(int argc, char **argv) {
sandbox::Demo app(argc, argv);
return app.run();
}*/
PEN_SOURCE 600
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <xf86drm.h>
#include <xf86drmMode.h>
#include <cstdlib>
static const char *dri_path = "/dev/dri/card0";
enum {
DEPTH = 24,
BPP = 32,
};
struct drm_dev_t {
uint32_t *buf;
uint32_t conn_id, enc_id, crtc_id, fb_id;
uint32_t width, height;
uint32_t pitch, size, handle;
drmModeModeInfo mode;
drmModeCrtc *saved_crtc;
struct drm_dev_t *next;
};
void fatal(char *str)
{
fprintf(stderr, "%s\n", str);
exit(EXIT_FAILURE);
}
void error(char *str)
{
perror(str);
exit(EXIT_FAILURE);
}
int eopen(const char *path, int flag)
{
int fd;
if ((fd = open(path, flag)) < 0) {
fprintf(stderr, "cannot open \"%s\"\n", path);
error("open");
}
return fd;
}
void *emmap(int addr, size_t len, int prot, int flag, int fd, off_t offset)
{
uint32_t *fp;
if ((fp = (uint32_t *) mmap(0, len, prot, flag, fd, offset)) == MAP_FAILED)
error("mmap");
return fp;
}
int drm_open(const char *path)
{
int fd, flags;
uint64_t has_dumb;
fd = eopen(path, O_RDWR);
/* set FD_CLOEXEC flag */
if ((flags = fcntl(fd, F_GETFD)) < 0
|| fcntl(fd, F_SETFD, flags | FD_CLOEXEC) < 0)
fatal("fcntl FD_CLOEXEC failed");
/* check capability */
if (drmGetCap(fd, DRM_CAP_DUMB_BUFFER, &has_dumb) < 0 || has_dumb == 0)
fatal("drmGetCap DRM_CAP_DUMB_BUFFER failed or doesn't have dumb buffer");
return fd;
}
struct drm_dev_t *drm_find_dev(int fd)
{
int i;
struct drm_dev_t *dev = NULL, *dev_head = NULL;
drmModeRes *res;
drmModeConnector *conn;
drmModeEncoder *enc;
if ((res = drmModeGetResources(fd)) == NULL)
fatal("drmModeGetResources() failed");
/* find all available connectors */
for (i = 0; i < res->count_connectors; i++) {
conn = drmModeGetConnector(fd, res->connectors[i]);
if (conn != NULL && conn->connection == DRM_MODE_CONNECTED && conn->count_modes > 0) {
dev = (struct drm_dev_t *) malloc(sizeof(struct drm_dev_t));
memset(dev, 0, sizeof(struct drm_dev_t));
dev->conn_id = conn->connector_id;
dev->enc_id = conn->encoder_id;
dev->next = NULL;
memcpy(&dev->mode, &conn->modes[0], sizeof(drmModeModeInfo));
dev->width = conn->modes[0].hdisplay;
dev->height = conn->modes[0].vdisplay;
/* FIXME: use default encoder/crtc pair */
if ((enc = drmModeGetEncoder(fd, dev->enc_id)) == NULL)
fatal("drmModeGetEncoder() faild");
dev->crtc_id = enc->crtc_id;
drmModeFreeEncoder(enc);
dev->saved_crtc = NULL;
/* create dev list */
dev->next = dev_head;
dev_head = dev;
}
drmModeFreeConnector(conn);
}
drmModeFreeResources(res);
return dev_head;
}
void drm_setup_fb(int fd, struct drm_dev_t *dev)
{
struct drm_mode_create_dumb creq;
struct drm_mode_map_dumb mreq;
memset(&creq, 0, sizeof(struct drm_mode_create_dumb));
creq.width = dev->width;
creq.height = dev->height;
creq.bpp = BPP; // hard conding
if (drmIoctl(fd, DRM_IOCTL_MODE_CREATE_DUMB, &creq) < 0)
fatal("drmIoctl DRM_IOCTL_MODE_CREATE_DUMB failed");
dev->pitch = creq.pitch;
dev->size = creq.size;
dev->handle = creq.handle;
if (drmModeAddFB(fd, dev->width, dev->height,
DEPTH, BPP, dev->pitch, dev->handle, &dev->fb_id))
fatal("drmModeAddFB failed");
memset(&mreq, 0, sizeof(struct drm_mode_map_dumb));
mreq.handle = dev->handle;
if (drmIoctl(fd, DRM_IOCTL_MODE_MAP_DUMB, &mreq))
fatal("drmIoctl DRM_IOCTL_MODE_MAP_DUMB failed");
dev->buf = (uint32_t *) emmap(0, dev->size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, mreq.offset);
dev->saved_crtc = drmModeGetCrtc(fd, dev->crtc_id); /* must store crtc data */
if (drmModeSetCrtc(fd, dev->crtc_id, dev->fb_id, 0, 0, &dev->conn_id, 1, &dev->mode))
fatal("drmModeSetCrtc() failed");
}
void drm_destroy(int fd, struct drm_dev_t *dev_head)
{
struct drm_dev_t *devp, *devp_tmp;
struct drm_mode_destroy_dumb dreq;
for (devp = dev_head; devp != NULL;) {
if (devp->saved_crtc)
drmModeSetCrtc(fd, devp->saved_crtc->crtc_id, devp->saved_crtc->buffer_id,
devp->saved_crtc->x, devp->saved_crtc->y, &devp->conn_id, 1, &devp->saved_crtc->mode);
drmModeFreeCrtc(devp->saved_crtc);
munmap(devp->buf, devp->size);
drmModeRmFB(fd, devp->fb_id);
memset(&dreq, 0, sizeof(dreq));
dreq.handle = devp->handle;
drmIoctl(fd, DRM_IOCTL_MODE_DESTROY_DUMB, &dreq);
devp_tmp = devp;
devp = devp->next;
free(devp_tmp);
}
close(fd);
}
int main()
{
int fd;
int i, j;
uint8_t color;
struct drm_dev_t *dev_head, *dev;
/* init */
fd = drm_open(dri_path);
dev_head = drm_find_dev(fd);
if (dev_head == NULL) {
fprintf(stderr, "available drm_dev not found\n");
return EXIT_FAILURE;
}
printf("available connector(s)\n\n");
for (dev = dev_head; dev != NULL; dev = dev->next) {
printf("connector id:%d\n", dev->conn_id);
printf("\tencoder id:%d crtc id:%d fb id:%d\n", dev->enc_id, dev->crtc_id, dev->fb_id);
printf("\twidth:%d height:%d\n", dev->width, dev->height);
}
/* FIXME: use first drm_dev */
dev = dev_head;
drm_setup_fb(fd, dev);
/* draw something */
for (i = 0; i < dev->height; i++)
for (j = 0; j < dev->width; j++) {
color = (double) (i * j) / (dev->height * dev->width) * 0xFF;
*(dev->buf + i * dev->width + j) = (uint32_t) 0xFFFFFF & (0x00 << 16 | color << 8 | color);
}
sleep(3);
/* destroy */
drm_destroy(fd, dev_head);
return 0;
}