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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
|
/*
* Some defines to define the behavior of the program
*/
#define CLEAN_FBDEV
#undef DEMO_CLONE
#define SIZE_X 2048
#define SIZE_Y 2048
/* Pitch needs to be power of two */
#define PITCH 2048
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <string.h>
#ifdef CLEAN_FBDEV
#include <errno.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/fb.h>
#endif
#include <signal.h>
#include "xf86drm.h"
#include "xf86drmMode.h"
/* old functions to be replaced */
drmModeFBPtr createFB(int fd, drmModeResPtr res);
void testCursor(int fd, uint32_t crtc);
void prettyColors(int fd, unsigned int handle);
void prettyCursor(int fd, unsigned int handle, unsigned int color);
#ifdef CLEAN_FBDEV
struct fb_var_screeninfo var;
struct fb_fix_screeninfo fix;
#endif
/* structs for the demo_driver */
struct demo_driver;
struct demo_screen
{
/* drm stuff */
drmBO buffer;
drmModeFBPtr fb;
drmModeCrtcPtr crtc;
size_t num_outputs;
uint32_t outputs_id[8];
drmModeOutputPtr outputs[8];
struct drm_mode_modeinfo *mode;
/* virtual buffer */
uint32_t virt_x;
uint32_t virt_y;
uint32_t pitch;
/* parent */
struct demo_driver *driver;
};
#define DEMO_MAX_SCREENS 4
#define MAX_FIND_OUTPUTS 8
struct demo_driver
{
/* drm stuff */
int fd;
drmModeResPtr res;
/* screens */
size_t numScreens;
struct demo_screen screens[DEMO_MAX_SCREENS];
};
struct demo_driver* demoCreateDriver(void);
void demoUpdateRes(struct demo_driver *driver);
int demoCreateScreens(struct demo_driver *driver);
int demoCreateScreenCloned(struct demo_driver *driver);
void demoTakeDownScreen(struct demo_screen *screen);
int demoFindConnectedOutputs(struct demo_driver *driver, drmModeOutputPtr *out, size_t max_out);
drmModeCrtcPtr demoFindFreeCrtc(struct demo_driver *driver, drmModeOutputPtr output);
void demoPanScreen(struct demo_screen *screen, uint16_t x, uint16_t y);
/* yet to be implemented */
void demoMouseActivate(struct demo_screen *screen);
void demoMouseMove(struct demo_screen *screen, uint16_t x, uint16_t y);
static struct drm_mode_modeinfo mode = {
.name = "Test mode",
.clock = 25200,
.hdisplay = 640,
.hsync_start = 656,
.hsync_end = 752,
.htotal = 800,
.hskew = 0,
.vdisplay = 480,
.vsync_start = 490,
.vsync_end = 492,
.vtotal = 525,
.vscan = 0,
.vrefresh = 60000, /* vertical refresh * 1000 */
.flags = 10,
};
int main(int argc, char **argv)
{
struct demo_driver *driver;
int num;
int i;
#ifdef CLEAN_FBDEV
int fbdev_fd;
fbdev_fd = open("/dev/fb0", O_RDWR);
memset(&var, 0, sizeof(struct fb_var_screeninfo));
memset(&fix, 0, sizeof(struct fb_fix_screeninfo));
if (ioctl(fbdev_fd, FBIOGET_VSCREENINFO, &var))
printf("var %s\n", strerror(errno));
if (ioctl(fbdev_fd, FBIOGET_FSCREENINFO, &fix))
printf("fix %s\n", strerror(errno));
#endif
printf("starting demo\n");
driver = demoCreateDriver();
if (!driver) {
printf("failed to create driver\n");
return 1;
}
#ifndef DEMO_CLONE
num = demoCreateScreens(driver);
#else
num = demoCreateScreenCloned(driver);
#endif
if (num < 1) {
printf("no screens attached or an error occured\n");
return 1;
}
printf("created %i screens\n", num);
for (i = 0; i < num; i++) {
prettyColors(driver->fd, driver->screens[i].fb->handle);
}
sleep(1);
for (i = 0; i < num; i++) {
printf("%i: 100 0\n", i);
demoPanScreen(&driver->screens[i], 100, 0);
sleep(1);
printf("%i: 0 100\n", i);
demoPanScreen(&driver->screens[i], 0, 100);
sleep(1);
printf("%i: 100 100\n", i);
demoPanScreen(&driver->screens[i], 100, 100);
sleep(1);
printf("%i: 0 0\n", i);
demoPanScreen(&driver->screens[i], 0, 1);
sleep(1);
testCursor(driver->fd, driver->screens[i].crtc->crtc_id);
}
sleep(2);
printf("taking down screens\n");
for (i = 0; i < num; i++) {
demoTakeDownScreen(&driver->screens[i]);
}
#ifdef CLEAN_FBDEV
if (ioctl(fbdev_fd, FBIOPUT_VSCREENINFO, &var))
printf("var %s\n", strerror(errno));
close(fbdev_fd);
#endif
printf("ok\n");
return 0;
}
int demoCreateScreens(struct demo_driver *driver)
{
drmModeOutputPtr out[MAX_FIND_OUTPUTS];
int num;
int num_screens = 0;
struct demo_screen *screen;
int ret = 0;
int i;
num = demoFindConnectedOutputs(driver, out, MAX_FIND_OUTPUTS);
if (num < 0)
return 0;
printf("found %i connected outputs\n", num);
for (i = 0; i < num; i++) {
screen = &driver->screens[i];
screen->crtc = demoFindFreeCrtc(driver, out[i]);
if (!screen->crtc) {
printf("found no free crtc for output\n");
drmModeFreeOutput(out[i]);
continue;
}
screen->fb = createFB(driver->fd, driver->res);
if (!screen->fb) {
drmModeFreeOutput(out[i]);
drmModeFreeCrtc(screen->crtc);
screen->crtc = 0;
printf("could not create framebuffer\n");
continue;
}
screen->virt_x = SIZE_X;
screen->virt_y = SIZE_Y;
screen->pitch = PITCH;
screen->outputs[0] = out[i];
screen->outputs_id[0] = out[i]->output_id;
screen->num_outputs = 1;
screen->mode = &mode;
screen->driver = driver;
ret = drmModeSetCrtc(
driver->fd,
screen->crtc->crtc_id,
screen->fb->buffer_id,
0, 0,
screen->outputs_id, screen->num_outputs,
screen->mode);
if (ret) {
printf("failed to set mode\n");
demoTakeDownScreen(screen);
} else {
num_screens++;
}
demoUpdateRes(driver);
}
return num_screens;
}
int demoCreateScreenCloned(struct demo_driver *driver)
{
drmModeOutputPtr out[MAX_FIND_OUTPUTS];
int num;
struct demo_screen *screen;
int ret = 0;
int i;
num = demoFindConnectedOutputs(driver, out, MAX_FIND_OUTPUTS);
if (num < 0)
return 0;
printf("found %i connected outputs\n", num);
screen = &driver->screens[0];
screen->fb = createFB(driver->fd, driver->res);
if (!screen->fb) {
printf("could not create framebuffer\n");
return 0;
}
screen->mode = &mode;
screen->driver = driver;
screen->virt_x = SIZE_X;
screen->virt_y = SIZE_Y;
screen->pitch = PITCH;
screen->num_outputs = 0;
for (i = 0; i < num; i++) {
screen->crtc = demoFindFreeCrtc(driver, out[i]);
if (!screen->crtc) {
printf("found no free crtc for output\n");
drmModeFreeOutput(out[i]);
continue;
}
screen->outputs[screen->num_outputs] = out[i];
screen->outputs_id[screen->num_outputs] = out[i]->output_id;
screen->num_outputs++;
printf("%u, %u\n", out[i]->output_id, screen->num_outputs);
}
ret = drmModeSetCrtc(
driver->fd,
screen->crtc->crtc_id,
screen->fb->buffer_id,
0, 0,
screen->outputs_id, screen->num_outputs,
screen->mode);
if (ret) {
printf("failed to set mode\n");
demoTakeDownScreen(screen);
return 0;
}
demoUpdateRes(driver);
return 1;
}
void demoTakeDownScreen(struct demo_screen *screen)
{
int fd = screen->driver->fd;
int i;
drmBO bo;
#if 0
/* This can bust the fbdev arrangement as it basically unhooks
* the outputs and the fbdev backend doesn't know how to put things
* back on track. Realistically, it's up to the crtc owner to restore
* things.....
*
* So if you are mixing API's make sure the modesetting owner puts
* back the original CRTC arrangement so fbdev can continue...
*
* Ho-hum..
*/
if (screen->crtc)
drmModeSetCrtc(fd, screen->crtc->crtc_id, 0, 0, 0, 0, 0, 0);
#endif
if (screen->fb)
drmModeRmFB(fd, screen->fb->buffer_id);
/* maybe we should keep a pointer to the bo on the screen */
if (screen->fb && !drmBOReference(fd, screen->fb->handle, &bo)) {
drmBOUnreference(fd, &bo);
drmBOUnreference(fd, &bo);
} else {
printf("bo error\n");
}
for (i = 0; i < screen->num_outputs; i++) {
drmModeFreeOutput(screen->outputs[i]);
screen->outputs[i] = NULL;
}
drmModeFreeCrtc(screen->crtc);
drmModeFreeFB(screen->fb);
screen->crtc = NULL;
screen->fb = NULL;
}
drmModeCrtcPtr demoFindFreeCrtc(struct demo_driver *driver, drmModeOutputPtr output)
{
drmModeCrtcPtr crtc;
int i, j, used = 0;
drmModeResPtr res = driver->res;
for (i = 0; i < res->count_crtcs; i++) {
used = 0;
for (j = 0; j < DEMO_MAX_SCREENS; j++) {
crtc = driver->screens[j].crtc;
if (crtc && crtc->crtc_id == res->crtcs[i])
used = 1;
}
if (!used) {
crtc = drmModeGetCrtc(driver->fd, res->crtcs[i]);
break;
} else {
crtc = 0;
}
}
return crtc;
}
static int driverfd;
static void
hotplugSIGNAL(int sig, siginfo_t *si, void *d)
{
union drm_wait_hotplug hw;
int ret;
printf("GOT HOTPLUG EVENT!\n");
/* ask for another hotplug event ! */
memset(&hw, 0, sizeof(hw));
hw.request.type = _DRM_HOTPLUG_SIGNAL;
hw.request.signal = SIGUSR1;
ret = ioctl(driverfd, DRM_IOCTL_WAIT_HOTPLUG, &hw);
}
struct demo_driver* demoCreateDriver(void)
{
struct demo_driver* driver = malloc(sizeof(struct demo_driver));
union drm_wait_hotplug hw;
int ret = 0;
memset(driver, 0, sizeof(struct demo_driver));
driver->fd = drmOpen("i915",NULL);
if (driver->fd < 0) {
printf("Failed to open the card fb\n");
goto err_driver;
}
#if 0
/* ioctl wait for hotplug */
do {
memset(&hw, 0, sizeof(hw));
ret = ioctl(driver->fd, DRM_IOCTL_WAIT_HOTPLUG, &hw);
printf("HOTPLUG %d %d %d\n",ret,errno,hw.reply.counter);
} while (ret && errno == EBUSY);
#else
/* signal for hotplug */
{
struct sigaction sa;
struct sigaction osa;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_SIGINFO;
sa.sa_sigaction = hotplugSIGNAL;
sigaction(SIGUSR1, &sa, &osa);
driverfd = driver->fd;
memset(&hw, 0, sizeof(hw));
hw.request.type = _DRM_HOTPLUG_SIGNAL;
hw.request.signal = SIGUSR1;
ret = ioctl(driver->fd, DRM_IOCTL_WAIT_HOTPLUG, &hw);
}
#endif
demoUpdateRes(driver);
if (!driver->res) {
printf("could not retrive resources\n");
goto err_res;
}
return driver;
err_res:
drmClose(driver->fd);
err_driver:
free(driver);
return NULL;
}
void demoUpdateRes(struct demo_driver *driver)
{
if (driver->res)
drmModeFreeResources(driver->res);
driver->res = drmModeGetResources(driver->fd);
if (!driver->res)
printf("failed to get resources from kernel\n");
}
int demoFindConnectedOutputs(struct demo_driver *driver, drmModeOutputPtr *out, size_t max_out)
{
int count = 0;
int i,j;
int fd = driver->fd;
drmModeResPtr res = driver->res;
drmModeOutputPtr output;
for (i = 0; i < res->count_outputs && count < max_out; i++) {
output = drmModeGetOutput(fd, res->outputs[i]);
if (!output)
continue;
if (output->connection == DRM_MODE_DISCONNECTED) {
drmModeFreeOutput(output);
continue;
}
for (j = 0; j < output->count_props; j++) {
drmModePropertyPtr prop;
prop = drmModeGetProperty(fd, output->props[j]);
printf("Property: %s\n",prop->name);
if (prop->count_enums)
printf("%s\n",prop->enums[output->prop_values[j]].name);
}
out[count++] = output;
}
return count;
}
void demoPanScreen(struct demo_screen *screen, uint16_t x, uint16_t y)
{
drmModeSetCrtc(
screen->driver->fd,
screen->crtc->crtc_id,
screen->fb->buffer_id,
x, y,
screen->outputs_id, screen->num_outputs,
screen->mode);
}
drmModeFBPtr createFB(int fd, drmModeResPtr res)
{
drmModeFBPtr frame;
unsigned int fb = 0;
int ret = 0;
drmBO bo;
ret = drmBOCreate(fd, SIZE_X * SIZE_Y * 4, 0, 0,
DRM_BO_FLAG_READ |
DRM_BO_FLAG_WRITE |
DRM_BO_FLAG_MEM_TT |
DRM_BO_FLAG_MEM_VRAM |
DRM_BO_FLAG_NO_EVICT,
DRM_BO_HINT_DONT_FENCE, &bo);
if (ret) {
printf("failed to create framebuffer (ret %d)\n",ret);
goto err;
}
ret = drmModeAddFB(fd, SIZE_X, SIZE_Y, 32, 32, PITCH * 4, bo.handle, &fb);
if (ret)
goto err_bo;
frame = drmModeGetFB(fd, fb);
if (!frame)
goto err_bo;
return frame;
err_bo:
drmBOUnreference(fd, &bo);
err:
return 0;
}
void draw(unsigned int x, unsigned int y, unsigned int w, unsigned int h, unsigned int v, unsigned int *ptr)
{
int i, j;
for (i = x; i < x + w; i++)
for(j = y; j < y + h; j++)
ptr[(i * PITCH) + j] = v;
}
void prettyColors(int fd, unsigned int handle)
{
drmBO bo;
unsigned int *ptr;
int i;
drmBOReference(fd, handle, &bo);
drmBOMap(fd, &bo, DRM_BO_FLAG_READ | DRM_BO_FLAG_WRITE, 0, (void**)&ptr);
for (i = 0; i < (SIZE_X*SIZE_Y); i++)
ptr[i] = 0xFFFFFFFF;
for (i = 0; i < 8; i++)
draw(i * 40, i * 40, 40, 40, 0, ptr);
draw(200, 100, 40, 40, 0xff00ff, ptr);
draw(100, 200, 40, 40, 0xff00ff, ptr);
drmBOUnmap(fd, &bo);
}
void testCursor(int fd, uint32_t crtc)
{
drmBO bo;
int ret;
ret = drmBOCreate(fd, 64 * 64 * 4, 0, 0,
DRM_BO_FLAG_READ |
DRM_BO_FLAG_WRITE |
DRM_BO_FLAG_MEM_VRAM |
DRM_BO_FLAG_NO_EVICT,
DRM_BO_HINT_DONT_FENCE, &bo);
prettyCursor(fd, bo.handle, 0xFFFF00FF);
printf("set cursor\n");
drmModeSetCursor(fd, crtc, bo.handle, 64, 64);
printf("move cursor 0, 0\n");
drmModeMoveCursor(fd, crtc, 0, 0);
sleep(1);
prettyCursor(fd, bo.handle, 0xFFFF0000);
printf("move cursor 40, 40\n");
drmModeMoveCursor(fd, crtc, 40, 40);
sleep(1);
printf("move cursor 100, 100\n");
drmModeMoveCursor(fd, crtc, 100, 100);
sleep(1);
drmModeSetCursor(fd, crtc, 0, 0, 0);
}
void prettyCursor(int fd, unsigned int handle, unsigned int color)
{
drmBO bo;
unsigned int *ptr;
int i;
drmBOReference(fd, handle, &bo);
drmBOMap(fd, &bo, DRM_BO_FLAG_READ | DRM_BO_FLAG_WRITE, 0, (void**)&ptr);
for (i = 0; i < (64 * 64); i++)
ptr[i] = color;
drmBOUnmap(fd, &bo);
drmBOUnreference(fd, &bo);
}
|