-
Notifications
You must be signed in to change notification settings - Fork 12
/
custom_video_drmkms.cpp
executable file
·1328 lines (1140 loc) · 42.1 KB
/
custom_video_drmkms.cpp
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
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**************************************************************
custom_video_drmkms.cpp - Linux DRM/KMS video management layer
---------------------------------------------------------
Switchres Modeline generation engine for emulation
License GPL-2.0+
Copyright 2010-2021 Chris Kennedy, Antonio Giner,
Alexandre Wodarczyk, Gil Delescluse
**************************************************************/
#include <stdio.h>
#include <dlfcn.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <dirent.h>
#include "custom_video_drmkms.h"
#include "log.h"
#define drmGetVersion p_drmGetVersion
#define drmFreeVersion p_drmFreeVersion
#define drmModeGetResources p_drmModeGetResources
#define drmModeGetConnector p_drmModeGetConnector
#define drmModeGetConnectorCurrent p_drmModeGetConnectorCurrent
#define drmModeFreeConnector p_drmModeFreeConnector
#define drmModeFreeResources p_drmModeFreeResources
#define drmModeGetEncoder p_drmModeGetEncoder
#define drmModeFreeEncoder p_drmModeFreeEncoder
#define drmModeGetCrtc p_drmModeGetCrtc
#define drmModeSetCrtc p_drmModeSetCrtc
#define drmModeFreeCrtc p_drmModeFreeCrtc
#define drmModeAttachMode p_drmModeAttachMode
#define drmModeDetachMode p_drmModeDetachMode
#define drmModeAddFB p_drmModeAddFB
#define drmModeRmFB p_drmModeRmFB
#define drmModeGetFB p_drmModeGetFB
#define drmModeFreeFB p_drmModeFreeFB
#define drmPrimeHandleToFD p_drmPrimeHandleToFD
#define drmModeGetPlaneResources p_drmModeGetPlaneResources
#define drmModeFreePlaneResources p_drmModeFreePlaneResources
#define drmIoctl p_drmIoctl
#define drmGetCap p_drmGetCap
#define drmGetDevices2 p_drmGetDevices2
#define drmIsMaster p_drmIsMaster
#define drmSetMaster p_drmSetMaster
#define drmDropMaster p_drmDropMaster
# define MAX_CARD_ID 10
# define MAX_DRM_DEVICES 16
// To enable libdrmhook: make SR_WITH_DRMHOOK=1
#ifdef SR_WITH_DRMHOOK
#define hook_handle RTLD_DEFAULT
#define hook_log " (will attempt hook)"
#else
#define hook_handle mp_drm_handle
#define hook_log ""
#endif
//============================================================
// shared the privileges of the master fd
//============================================================
// If 2 displays use the same GPU but a different connector, let's share the
// FD indexed on the card ID
static int s_shared_fd[MAX_CARD_ID] = {};
// The active shares on a fd, per card id
static int s_shared_count[MAX_CARD_ID] = {};
// What we're missing here, is also a list of the connector ids associated with
// the screen number, otherwise SR will try to use (again) the first connector
// that has an monitor plugged to it
static unsigned int s_shared_conn[MAX_CARD_ID] = {};
//============================================================
// id for class object (static)
//============================================================
// This helps to trace counts of active displays accross vaious instances
// ++'ed at constructor, --'ed at destructor
// m_id will use the ++-ed value
static int static_id = 0;
//============================================================
// list connector types
//============================================================
const char *get_connector_name(int mode)
{
switch (mode)
{
case DRM_MODE_CONNECTOR_Unknown:
return "Unknown";
case DRM_MODE_CONNECTOR_VGA:
return "VGA-";
case DRM_MODE_CONNECTOR_DVII:
return "DVI-I-";
case DRM_MODE_CONNECTOR_DVID:
return "DVI-D-";
case DRM_MODE_CONNECTOR_DVIA:
return "DVI-A-";
case DRM_MODE_CONNECTOR_Composite:
return "Composite-";
case DRM_MODE_CONNECTOR_SVIDEO:
return "SVIDEO-";
case DRM_MODE_CONNECTOR_LVDS:
return "LVDS-";
case DRM_MODE_CONNECTOR_Component:
return "Component-";
case DRM_MODE_CONNECTOR_9PinDIN:
return "9PinDIN-";
case DRM_MODE_CONNECTOR_DisplayPort:
return "DisplayPort-";
case DRM_MODE_CONNECTOR_HDMIA:
return "HDMI-A-";
case DRM_MODE_CONNECTOR_HDMIB:
return "HDMI-B-";
case DRM_MODE_CONNECTOR_TV:
return "TV-";
case DRM_MODE_CONNECTOR_eDP:
return "eDP-";
case DRM_MODE_CONNECTOR_VIRTUAL:
return "VIRTUAL-";
case DRM_MODE_CONNECTOR_DSI:
return "DSI-";
case DRM_MODE_CONNECTOR_DPI:
return "DPI-";
default:
return "not_defined-";
}
}
//============================================================
// Check if a connector is not used on a previous display
//============================================================
bool connector_already_used(unsigned int conn_id)
{
// Don't remap to an already used connector
for (int c = 1 ; c < static_id ; c++)
{
if (s_shared_conn[c] == conn_id)
return true;
}
return false;
}
//============================================================
// Convert a SR modeline to a DRM modeline
//============================================================
void modeline_to_drm_modeline(int id, modeline *mode, drmModeModeInfo *drmmode)
{
// Clear struct
memset(drmmode, 0, sizeof(drmModeModeInfo));
// Create specific mode name
snprintf(drmmode->name, 32, "SR-%d_%dx%d@%.02f%s", id, mode->hactive, mode->vactive, mode->vfreq, mode->interlace ? "i" : "");
drmmode->clock = mode->pclock / 1000;
drmmode->hdisplay = mode->hactive;
drmmode->hsync_start = mode->hbegin;
drmmode->hsync_end = mode->hend;
drmmode->htotal = mode->htotal;
drmmode->vdisplay = mode->vactive;
drmmode->vsync_start = mode->vbegin;
drmmode->vsync_end = mode->vend;
drmmode->vtotal = mode->vtotal;
drmmode->flags = (mode->interlace ? DRM_MODE_FLAG_INTERLACE : 0) | (mode->doublescan ? DRM_MODE_FLAG_DBLSCAN : 0) | (mode->hsync ? DRM_MODE_FLAG_PHSYNC : DRM_MODE_FLAG_NHSYNC) | (mode->vsync ? DRM_MODE_FLAG_PVSYNC : DRM_MODE_FLAG_NVSYNC);
drmmode->hskew = 0;
drmmode->vscan = 0;
drmmode->vrefresh = mode->refresh; // Used only for human readable output
}
//============================================================
// drmkms_timing::test_kernel_user_modes
//============================================================
bool drmkms_timing::test_kernel_user_modes()
{
int ret = 0, first_modes_count = 0, second_modes_count = 0;
int fd;
drmModeModeInfo mode = {};
const char* my_name = "KMS Test mode";
drmModeConnector *conn;
// Make sure we are master, that is required for the IOCTL
fd = get_master_fd();
if (fd < 0)
{
log_verbose("DRM/KMS: <%d> (%s) Need master to test kernel user modes\n", m_id, __FUNCTION__);
return false;
}
// Create a dummy modeline with a pixel clock higher than 25MHz to avoid
// drivers checks rejecting the mode. Use a modeline that no one would
// ever use hopefully
strcpy(mode.name, my_name);
mode.clock = 25212;
mode.hdisplay = 1234;
mode.hsync_start = 1290;
mode.hsync_end = 1408;
mode.htotal = 1610;
mode.vdisplay = 234;
mode.vsync_start = 238;
mode.vsync_end = 241;
mode.vtotal = 261;
mode.flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC;
// Count the number of existing modes, so it should be +1 when attaching
// a new mode. Could also check the mode name, still better
conn = drmModeGetConnector(fd, m_desktop_output);
if (!conn)
{
log_verbose("DRM/KMS: <%d> (%s) Cannot get connector\n", m_id, __FUNCTION__);
m_kernel_user_modes = false;
return false;
}
first_modes_count = conn->count_modes;
ret = drmModeAttachMode(fd, m_desktop_output, &mode);
drmModeFreeConnector(conn);
// This case can only happen if we're not drmMaster. If the kernel doesn't
// support adding new modes, the IOCTL will still return 0, not an error
if (ret < 0)
{
// Let's fail, no need to go further
log_verbose("DRM/KMS: <%d> (%s) Cannot add new kernel user mode\n", m_id, __FUNCTION__);
m_kernel_user_modes = false;
return false;
}
// Not using drmModeGetConnectorCurrent here since we need to force a
// modelist connector refresh, so the kernel will probe the connector
conn = drmModeGetConnector(fd, m_desktop_output);
second_modes_count = conn->count_modes;
if (first_modes_count != second_modes_count)
{
log_verbose("DRM/KMS: <%d> (%s) Kernel supports user modes (%d vs %d)\n", m_id, __FUNCTION__, first_modes_count, second_modes_count);
m_kernel_user_modes = true;
drmModeDetachMode(fd, m_desktop_output, &mode);
if (fd != m_hook_fd)
drmDropMaster(fd);
}
else
log_verbose("DRM/KMS: <%d> (%s) Kernel doesn't supports user modes\n", m_id, __FUNCTION__);
drmModeFreeConnector(conn);
return m_kernel_user_modes;
}
//============================================================
// drmkms_timing::drmkms_timing
//============================================================
drmkms_timing::drmkms_timing(char *device_name, custom_video_settings *vs)
{
m_vs = *vs;
m_id = ++static_id;
log_verbose("DRM/KMS: <%d> (drmkms_timing) creation (%s)\n", m_id, device_name);
// Copy screen device name and limit size
if ((strlen(device_name) + 1) > 32)
{
strncpy(m_device_name, device_name, 31);
log_error("DRM/KMS: <%d> (drmkms_timing) [ERROR] the devine name is too long it has been trucated to %s\n", m_id, m_device_name);
}
else
strcpy(m_device_name, device_name);
}
//============================================================
// drmkms_timing::~drmkms_timing
//============================================================
drmkms_timing::~drmkms_timing()
{
// Remove kernel user modes
if (m_kernel_user_modes)
{
int i = 0, ret = 0;
int fd;
drmModeConnector *conn;
fd = get_master_fd();
if (fd >= 0)
{
conn = drmModeGetConnectorCurrent(fd, m_desktop_output);
drmSetMaster(fd);
for (i = 0; i < conn->count_modes; i++)
{
drmModeModeInfo *mode = &conn->modes[i];
log_verbose("DRM/KMS: <%d> (%s) Checking kernel mode: %s\n", m_id, __FUNCTION__, mode->name);
ret = strncmp(mode->name, "SR-", 3);
if (ret == 0)
{
log_verbose("DRM/KMS: <%d> (%s) Removing kernel user mode: %s\n", m_id, __FUNCTION__, mode->name);
drmModeDetachMode(fd, m_desktop_output, mode);
}
}
if (fd != m_hook_fd)
drmDropMaster(fd);
drmModeFreeConnector(conn);
if (fd != m_drm_fd and fd != m_hook_fd)
close(fd);
}
}
// Free the connector used
s_shared_conn[m_id] = -1;
// close DRM/KMS library
if (mp_drm_handle)
dlclose(mp_drm_handle);
if (m_drm_fd > 0)
{
if (!--s_shared_count[m_card_id])
close(m_drm_fd);
}
// Reset static data
static_id = 0;
memset(s_shared_fd, 0, sizeof(s_shared_fd));
memset(s_shared_count, 0, sizeof(s_shared_count));
memset(s_shared_conn, 0, sizeof(s_shared_conn));
}
//============================================================
// drmkms_timing::init
//============================================================
bool drmkms_timing::init()
{
log_verbose("DRM/KMS: <%d> (init) loading DRM/KMS library%s\n", m_id, hook_log);
mp_drm_handle = dlopen("libdrm.so", RTLD_NOW);
if (mp_drm_handle)
{
p_drmGetVersion = (__typeof__(drmGetVersion)) dlsym(mp_drm_handle, "drmGetVersion");
if (p_drmGetVersion == NULL)
{
log_error("DRM/KMS: <%d> (init) [ERROR] missing func %s in %s", m_id, "drmGetVersion", "DRM_LIBRARY");
return false;
}
p_drmFreeVersion = (__typeof__(drmFreeVersion)) dlsym(mp_drm_handle, "drmFreeVersion");
if (p_drmFreeVersion == NULL)
{
log_error("DRM/KMS: <%d> (init) [ERROR] missing func %s in %s", m_id, "drmFreeVersion", "DRM_LIBRARY");
return false;
}
p_drmModeGetResources = (__typeof__(drmModeGetResources)) dlsym(mp_drm_handle, "drmModeGetResources");
if (p_drmModeGetResources == NULL)
{
log_error("DRM/KMS: <%d> (init) [ERROR] missing func %s in %s", m_id, "drmModeGetResources", "DRM_LIBRARY");
return false;
}
p_drmModeGetConnector = (__typeof__(drmModeGetConnector)) dlsym(hook_handle, "drmModeGetConnector");
if (p_drmModeGetConnector == NULL)
{
log_error("DRM/KMS: <%d> (init) [ERROR] missing func %s in %s", m_id, "drmModeGetConnector", "DRM_LIBRARY");
return false;
}
p_drmModeGetConnectorCurrent = (__typeof__(drmModeGetConnectorCurrent)) dlsym(hook_handle, "drmModeGetConnectorCurrent");
if (p_drmModeGetConnectorCurrent == NULL)
{
log_error("DRM/KMS: <%d> (init) [ERROR] missing func %s in %s", m_id, "drmModeGetConnectorCurrent", "DRM_LIBRARY");
return false;
}
p_drmModeFreeConnector = (__typeof__(drmModeFreeConnector)) dlsym(hook_handle, "drmModeFreeConnector");
if (p_drmModeFreeConnector == NULL)
{
log_error("DRM/KMS: <%d> (init) [ERROR] missing func %s in %s", m_id, "drmModeFreeConnector", "DRM_LIBRARY");
return false;
}
p_drmModeFreeResources = (__typeof__(drmModeFreeResources)) dlsym(mp_drm_handle, "drmModeFreeResources");
if (p_drmModeFreeResources == NULL)
{
log_error("DRM/KMS: <%d> (init) [ERROR] missing func %s in %s", m_id, "drmModeFreeResources", "DRM_LIBRARY");
return false;
}
p_drmModeGetEncoder = (__typeof__(drmModeGetEncoder)) dlsym(mp_drm_handle, "drmModeGetEncoder");
if (p_drmModeGetEncoder == NULL)
{
log_error("DRM/KMS: <%d> (init) [ERROR] missing func %s in %s", m_id, "drmModeGetEncoder", "DRM_LIBRARY");
return false;
}
p_drmModeFreeEncoder = (__typeof__(drmModeFreeEncoder)) dlsym(mp_drm_handle, "drmModeFreeEncoder");
if (p_drmModeFreeEncoder == NULL)
{
log_error("DRM/KMS: <%d> (init) [ERROR] missing func %s in %s", m_id, "drmModeFreeEncoder", "DRM_LIBRARY");
return false;
}
p_drmModeGetCrtc = (__typeof__(drmModeGetCrtc)) dlsym(mp_drm_handle, "drmModeGetCrtc");
if (p_drmModeGetCrtc == NULL)
{
log_error("DRM/KMS: <%d> (init) [ERROR] missing func %s in %s", m_id, "drmModeGetCrtc", "DRM_LIBRARY");
return false;
}
p_drmModeSetCrtc = (__typeof__(drmModeSetCrtc)) dlsym(mp_drm_handle, "drmModeSetCrtc");
if (p_drmModeSetCrtc == NULL)
{
log_error("DRM/KMS: <%d> (init) [ERROR] missing func %s in %s", m_id, "drmModeSetCrtc", "DRM_LIBRARY");
return false;
}
p_drmModeFreeCrtc = (__typeof__(drmModeFreeCrtc)) dlsym(mp_drm_handle, "drmModeFreeCrtc");
if (p_drmModeFreeCrtc == NULL)
{
log_error("DRM/KMS: <%d> (init) [ERROR] missing func %s in %s", m_id, "drmModeFreeCrtc", "DRM_LIBRARY");
return false;
}
p_drmModeAttachMode = (__typeof__(drmModeAttachMode)) dlsym(mp_drm_handle, "drmModeAttachMode");
if (p_drmModeAttachMode == NULL)
{
log_error("DRM/KMS: <%d> (init) [ERROR] missing func %s in %s", m_id, "drmModeAttachMode", "DRM_LIBRARY");
return false;
}
p_drmModeDetachMode = (__typeof__(drmModeDetachMode)) dlsym(mp_drm_handle, "drmModeDetachMode");
if (p_drmModeDetachMode == NULL)
{
log_error("DRM/KMS: <%d> (init) [ERROR] missing func %s in %s", m_id, "drmModeDetachMode", "DRM_LIBRARY");
return false;
}
p_drmModeAddFB = (__typeof__(drmModeAddFB)) dlsym(mp_drm_handle, "drmModeAddFB");
if (p_drmModeAddFB == NULL)
{
log_error("DRM/KMS: <%d> (init) [ERROR] missing func %s in %s", m_id, "drmModeAddFB", "DRM_LIBRARY");
return false;
}
p_drmModeRmFB = (__typeof__(drmModeRmFB)) dlsym(mp_drm_handle, "drmModeRmFB");
if (p_drmModeRmFB == NULL)
{
log_error("DRM/KMS: <%d> (init) [ERROR] missing func %s in %s", m_id, "drmModeRmFB", "DRM_LIBRARY");
return false;
}
p_drmModeGetFB = (__typeof__(drmModeGetFB)) dlsym(mp_drm_handle, "drmModeGetFB");
if (p_drmModeGetFB == NULL)
{
log_error("DRM/KMS: <%d> (init) [ERROR] missing func %s in %s", m_id, "drmModeGetFB", "DRM_LIBRARY");
return false;
}
p_drmModeFreeFB = (__typeof__(drmModeFreeFB)) dlsym(mp_drm_handle, "drmModeFreeFB");
if (p_drmModeFreeFB == NULL)
{
log_error("DRM/KMS: <%d> (init) [ERROR] missing func %s in %s", m_id, "drmModeFreeFB", "DRM_LIBRARY");
return false;
}
p_drmPrimeHandleToFD = (__typeof__(drmPrimeHandleToFD)) dlsym(mp_drm_handle, "drmPrimeHandleToFD");
if (p_drmPrimeHandleToFD == NULL)
{
log_error("DRM/KMS: <%d> (init) [ERROR] missing func %s in %s", m_id, "drmPrimeHandleToFD", "DRM_LIBRARY");
return false;
}
p_drmModeGetPlaneResources = (__typeof__(drmModeGetPlaneResources)) dlsym(mp_drm_handle, "drmModeGetPlaneResources");
if (p_drmModeGetPlaneResources == NULL)
{
log_error("DRM/KMS: <%d> (init) [ERROR] missing func %s in %s", m_id, "drmModeGetPlaneResources", "DRM_LIBRARY");
return false;
}
p_drmModeFreePlaneResources = (__typeof__(drmModeFreePlaneResources)) dlsym(mp_drm_handle, "drmModeFreePlaneResources");
if (p_drmModeFreePlaneResources == NULL)
{
log_error("DRM/KMS: <%d> (init) [ERROR] missing func %s in %s", m_id, "drmModeFreePlaneResources", "DRM_LIBRARY");
return false;
}
p_drmIoctl = (__typeof__(drmIoctl)) dlsym(mp_drm_handle, "drmIoctl");
if (p_drmIoctl == NULL)
{
log_error("DRM/KMS: <%d> (init) [ERROR] missing func %s in %s", m_id, "drmIoctl", "DRM_LIBRARY");
return false;
}
p_drmGetCap = (__typeof__(drmGetCap)) dlsym(mp_drm_handle, "drmGetCap");
if (p_drmGetCap == NULL)
{
log_error("DRM/KMS: <%d> (init) [ERROR] missing func %s in %s", m_id, "drmGetCap", "DRM_LIBRARY");
return false;
}
p_drmGetDevices2 = (__typeof__(drmGetDevices2)) dlsym(mp_drm_handle, "drmGetDevices2");
if (p_drmGetDevices2 == NULL)
{
log_error("DRM/KMS: <%d> (init) [ERROR] missing func %s in %s", m_id, "drmGetDevices2", "DRM_LIBRARY");
return false;
}
p_drmIsMaster = (__typeof__(drmIsMaster)) dlsym(mp_drm_handle, "drmIsMaster");
if (p_drmIsMaster == NULL)
{
log_error("DRM/KMS: <%d> (init) [ERROR] missing func %s in %s", m_id, "drmIsMaster", "DRM_LIBRARY");
return false;
}
p_drmSetMaster = (__typeof__(drmSetMaster)) dlsym(mp_drm_handle, "drmSetMaster");
if (p_drmSetMaster == NULL)
{
log_error("DRM/KMS: <%d> (init) [ERROR] missing func %s in %s", m_id, "drmSetMaster", "DRM_LIBRARY");
return false;
}
p_drmDropMaster = (__typeof__(drmDropMaster)) dlsym(mp_drm_handle, "drmDropMaster");
if (p_drmDropMaster == NULL)
{
log_error("DRM/KMS: <%d> (init) [ERROR] missing func %s in %s", m_id, "drmDropMaster", "DRM_LIBRARY");
return false;
}
}
else
{
log_error("DRM/KMS: <%d> (init) [ERROR] missing %s library\n", m_id, "DRM/KMS_LIBRARY");
return false;
}
int screen_pos = -1;
// Handle the screen name, "auto", "screen[0-9]" and device name
if (strlen(m_device_name) == 7 && !strncmp(m_device_name, "screen", 6) && m_device_name[6] >= '0' && m_device_name[6] <= '9')
screen_pos = m_device_name[6] - '0';
else if (strlen(m_device_name) == 1 && m_device_name[0] >= '0' && m_device_name[0] <= '9')
screen_pos = m_device_name[0] - '0';
// Get an array of drm devices to check
drmDevicePtr devices[MAX_DRM_DEVICES];
int num_devices = drmGetDevices2(0, NULL, 0);
if (num_devices > MAX_DRM_DEVICES)
num_devices = MAX_DRM_DEVICES;
int ret = drmGetDevices2(0, devices, num_devices);
if (ret < 0)
{
log_error("DRM/KMS: drmGetDevices2() returned an error %d\n", ret);
return false;
}
char *drm_name;
drmModeRes *p_res;
drmModeConnector *p_connector;
int output_position = 0;
for (int num = 0; num < num_devices; num++)
{
// Skip non-primary nodes
if (devices[num]->available_nodes & (1 << DRM_NODE_PRIMARY))
drm_name = devices[num]->nodes[DRM_NODE_PRIMARY];
else continue;
if (!access(drm_name, F_OK) == 0)
{
log_error("DRM/KMS: <%d> (init) [ERROR] cannot open device %s\n", m_id, drm_name);
break;
}
m_drm_fd = open(drm_name, O_RDWR | O_CLOEXEC);
drmVersion *version = drmGetVersion(m_drm_fd);
log_verbose("DRM/KMS: <%d> (init) version %d.%d.%d type %s\n", m_id, version->version_major, version->version_minor, version->version_patchlevel, version->name);
drmFreeVersion(version);
uint64_t check_dumb = 0;
if (drmGetCap(m_drm_fd, DRM_CAP_DUMB_BUFFER, &check_dumb) < 0)
log_error("DRM/KMS: <%d> (init) [ERROR] ioctl DRM_CAP_DUMB_BUFFER\n", m_id);
if (!check_dumb)
{
log_error("DRM/KMS: <%d> (init) [ERROR] dumb buffer not supported\n", m_id);
continue;
}
p_res = drmModeGetResources(m_drm_fd);
for (int i = 0; i < p_res->count_connectors; i++)
{
p_connector = drmModeGetConnectorCurrent(m_drm_fd, p_res->connectors[i]);
if (!p_connector)
{
log_error("DRM/KMS: <%d> (init) [ERROR] card %d connector %d - %d\n", m_id, num, i, p_res->connectors[i]);
continue;
}
char connector_name[32];
snprintf(connector_name, 32, "%s%d", get_connector_name(p_connector->connector_type), p_connector->connector_type_id);
log_verbose("DRM/KMS: <%d> (init) card %d connector %d id %d name %s status %d - modes %d\n", m_id, num, i, p_connector->connector_id, connector_name, p_connector->connection, p_connector->count_modes);
// detect desktop connector
if (!m_desktop_output && p_connector->connection == DRM_MODE_CONNECTED)
{
if (!strcmp(m_device_name, "auto") || !strcmp(m_device_name, connector_name) || output_position == screen_pos)
{
// In a multihead setup, skip already used connectors
if (connector_already_used(p_connector->connector_id))
{
drmModeFreeConnector(p_connector);
continue;
}
m_desktop_output = p_connector->connector_id;
m_card_id = num;
strcpy(m_drm_name, drm_name);
log_verbose("DRM/KMS: <%d> (init) card %d connector %d id %d name %s selected as primary output\n", m_id, num, i, m_desktop_output, connector_name);
drmModeEncoder *p_encoder = drmModeGetEncoder(m_drm_fd, p_connector->encoder_id);
if (p_encoder)
{
for (int e = 0; e < p_res->count_crtcs; e++)
{
mp_crtc_desktop = drmModeGetCrtc(m_drm_fd, p_res->crtcs[e]);
if (mp_crtc_desktop->crtc_id == p_encoder->crtc_id)
{
log_verbose("DRM/KMS: <%d> (init) desktop mode name %s crtc %d fb %d valid %d\n", m_id, mp_crtc_desktop->mode.name, mp_crtc_desktop->crtc_id, mp_crtc_desktop->buffer_id, mp_crtc_desktop->mode_valid);
break;
}
drmModeFreeCrtc(mp_crtc_desktop);
}
}
if (!mp_crtc_desktop)
{
m_desktop_output = 0;
log_error("DRM/KMS: <%d> (init) [ERROR] no crtc found\n", m_id);
}
drmModeFreeEncoder(p_encoder);
}
output_position++;
}
drmModeFreeConnector(p_connector);
}
drmModeFreeResources(p_res);
if (!m_desktop_output)
close(m_drm_fd);
else
{
if (drmIsMaster(m_drm_fd))
{
// We've never called drmSetMaster before. This means we're the first app
// opening the device, so the kernel sets us as master by default.
// We drop master so other apps can become master
log_verbose("DRM/KMS: <%d> (%s) Already DRM master\n", m_id, __FUNCTION__);
s_shared_fd[m_card_id] = m_drm_fd;
s_shared_count[m_card_id] = 1;
drmDropMaster(m_drm_fd);
}
else
{
if (s_shared_count[m_card_id] > 0)
{
log_verbose("DRM/KMS: <%d> (%s : %d) The drm FD was substituted, expect the unexpected\n", m_id, __FUNCTION__, __LINE__);
close(m_drm_fd);
m_drm_fd = s_shared_fd[m_card_id];
s_shared_count[m_card_id]++;
}
else if (m_id == 1)
{
log_verbose("DRM/KMS: <%d> (%s) looking for the DRM master\n", m_id, __FUNCTION__);
int fd = get_master_fd();
if (fd >= 0)
{
close(m_drm_fd);
// This statement is dangerous, as drmIsMaster can return 1
// on m_drm_fd if there is no master left, but it doesn't
// check if m_drm_fd is a valid fd
m_drm_fd = fd;
s_shared_fd[m_card_id] = m_drm_fd;
// start at 2 to disable closing the fd
s_shared_count[m_card_id] = 2;
}
if (!drmIsMaster(m_drm_fd))
{
m_desktop_output = 0;
log_error("DRM/KMS: <%d> (%s) [ERROR] limited DRM rights on this screen\n", m_id, __FUNCTION__);
}
}
}
// If we're here and we have a valid output, we're done.
if (m_desktop_output) break;
}
}
// Handle no screen detected case
if (!m_desktop_output)
{
log_error("DRM/KMS: <%d> (init) [ERROR] no screen detected\n", m_id);
return false;
}
else
{
}
// Check if we have a libdrm hook
if (drmModeGetConnectorCurrent(-1, 0) != NULL)
{
log_verbose("DRM/KMS: libdrm hook found!\n");
m_caps |= CUSTOM_VIDEO_CAPS_UPDATE;
}
// Check if the kernel handles user modes
else if (test_kernel_user_modes())
m_caps |= CUSTOM_VIDEO_CAPS_ADD;
if (drmIsMaster(m_drm_fd) and m_drm_fd != m_hook_fd)
drmDropMaster(m_drm_fd);
return true;
}
//============================================================
// drmkms_timing::get_master_fd
//============================================================
// BACKGROUND
// This is written as of Linux 5.14, 5.15 is just out, not yet tested.
// There are a few unexpected behaviours so far in DRM:
// - drmSetMaster seems to always return -1 on 5.4, but ok on 5.14
// - drmIsMaster doesn't care if the FD exists and will always return 1
// if the there is no master on the DRI device
// That's why we can't trust drmIsMaster if we didn't make sure before that
// the FD does exist.
// get_master_fd will always return a valid master FD, or return -1 if it's
// impossible
int drmkms_timing::get_master_fd()
{
const size_t path_length = 20;
char procpath[path_length];
char fullpath[512];
char* actualpath;
struct stat st;
int fd;
// CASE 1: m_drm_fd is a valid FD
if (fstat(m_drm_fd, &st) == 0)
{
if (drmIsMaster(m_drm_fd))
return m_drm_fd;
if (drmSetMaster(m_drm_fd) == 0)
return m_drm_fd;
}
// CASE 2: m_drm_fd can't be master, find the master FD
if (m_card_id > MAX_CARD_ID - 1 or m_card_id < 0)
{
log_error("DRM/KMS: <%d> (%s) [ERROR] card id (%d) out of bounds (0 to %d)\n", m_id, __FUNCTION__, m_card_id, MAX_CARD_ID - 1);
return -1;
}
if (!access(m_drm_name, F_OK) == 0)
{
log_error("DRM/KMS: <%d> (%s) [ERROR] Device %s doesn't exist\n", m_id, __FUNCTION__, m_drm_name);
return -1;
}
sprintf(procpath, "/proc/%d/fd", getpid());
auto dir = opendir(procpath);
if (!dir)
return -1;
while (auto f = readdir(dir))
{
// Skip everything that starts with a dot
if (f->d_name[0] == '.')
continue;
// Only symlinks matter
if (f-> d_type != DT_LNK)
continue;
//log_verbose("File: %s\n", f->d_name);
sprintf(fullpath, "%s/%s", procpath, f->d_name);
if (stat(fullpath, &st))
continue;
if (!S_ISCHR(st.st_mode))
continue;
actualpath = realpath(fullpath, NULL);
// Only check the device we expect
if (strncmp(m_drm_name, actualpath, path_length) != 0)
{
free(actualpath);
continue;
}
fd = atoi(f->d_name);
//log_verbose("File: %s -> %s %d\n", fullpath, actualpath, fd);
free(actualpath);
if (drmIsMaster(fd))
{
log_verbose("DRM/KMS: <%d> (%s) DRM hook created on FD %d\n", m_id, __FUNCTION__, fd);
closedir(dir);
m_hook_fd = fd;
return fd;
}
}
closedir(dir);
// CASE 3: m_drm_fd is not a master (and probably not even a valid FD), the currend pid doesn't have master rights
// Or master is owned by a 3rd party app (like a frontend ...)
log_verbose("DRM/KMS: <%d> (%s) Couldn't find a master FD, opening default %s\n", m_id, __FUNCTION__, m_drm_name);
// mark our former hook as invalid
m_hook_fd = -1;
fd = open(m_drm_name, O_RDWR | O_CLOEXEC);
if (fd < 0)
{
// Oh, we're totally screwed here, worst possible scenario
log_error("DRM/KMS: <%d> (%s) Can't open %s, can't get master rights\n", m_id, __FUNCTION__, m_drm_name);
return -1;
}
// Hardly any chance we reach here. I don't even know when to close the FD ...
if (drmIsMaster(fd) or drmSetMaster(fd) == 0)
return fd;
// There is definitely no way we get master ...
close(fd);
log_error("DRM/KMS: <%d> (%s) No way to get master rights!\n", m_id, __FUNCTION__);
return -1;
}
//============================================================
// drmkms_timing::update_mode
//============================================================
bool drmkms_timing::update_mode(modeline *mode)
{
if (!mode)
return false;
if (!m_desktop_output)
{
log_error("DRM/KMS: <%d> (update_mode) [ERROR] no screen detected\n", m_id);
return false;
}
// Without libdrm hook, the update method isn't natively supported, so we must delete
// the mode and readd it with updated timings.
if (!(m_caps & CUSTOM_VIDEO_CAPS_UPDATE))
{
if (!delete_mode(mode))
{
log_error("DRM/KMS: <%d> (update_mode) [ERROR] delete operation not successful", m_id);
return false;
}
if (!add_mode(mode))
{
log_error("DRM/KMS: <%d> (update_mode) [ERROR] add operation not successful", m_id);
return false;
}
return true;
}
// libdrn hook case, we can update timings directly in the connector's data
drmModeConnector *conn = drmModeGetConnectorCurrent(m_drm_fd, m_desktop_output);
if (conn)
{
for (int i = 0; i < conn->count_modes; i++)
{
drmModeModeInfo *drmmode = &conn->modes[i];
if ((int)mode->platform_data == i)
{
int m_type = drmmode->type;
modeline_to_drm_modeline(m_id, mode, drmmode);
drmmode->type = m_type;
return true;
}
}
}
return false;
}
//============================================================
// drmkms_timing::add_mode
//============================================================
bool drmkms_timing::add_mode(modeline *mode)
{
if (!mode)
return false;
// Handle no screen detected case
if (!m_desktop_output)
{
log_error("DRM/KMS: <%d> (add_mode) [ERROR] no screen detected\n", m_id);
return false;
}
if (!mp_crtc_desktop)
{
log_error("DRM/KMS: <%d> (add_mode) [ERROR] no desktop crtc\n", m_id);
return false;
}
if (!mode)
return false;
if (m_kernel_user_modes)
{
int ret = 0, fd = m_drm_fd;
drmModeModeInfo drmmode;
if (!drmIsMaster(fd))
fd = get_master_fd();
if (!drmIsMaster(fd))
{
log_error("DRM/KMS: <%d> (%s) Need master to add a kernel mode (%d)\n", m_id, __FUNCTION__, ret);
return false;
}
modeline_to_drm_modeline(m_id, mode, &drmmode);
drmmode.type = DRM_MODE_TYPE_USERDEF;
log_verbose("DRM/KMS: <%d> (add_mode) [DEBUG] Adding a mode to the kernel: %dx%d %s\n", m_id, drmmode.hdisplay, drmmode.vdisplay, drmmode.name);
// Calling drmModeGetConnector forces a refresh of the connector modes, which is slow, so don't do it
ret = drmModeAttachMode(fd, m_desktop_output, &drmmode);
if (ret != 0)
{
// This case hardly has any chance to happen, since at this point
// we are drmMaster, and we have already checked that the kernel
// supports user modes. If any error, it's on the kernel side
log_verbose("DRM/KMS: <%d> (%s) Couldn't add mode (ret=%d)\n", m_id, __FUNCTION__, ret);
if (fd != m_hook_fd)
drmDropMaster(fd);
return false;
}
log_verbose("DRM/KMS: <%d> (%s) Mode added\n", m_id, __FUNCTION__);
if (fd != m_hook_fd)
drmDropMaster(fd);
}
return true;
}
//============================================================
// drmkms_timing::set_timing
//============================================================
bool drmkms_timing::set_timing(modeline *mode)
{
if (!mode)
return false;
// Handle no screen detected case
if (!m_desktop_output)
{
log_error("DRM/KMS: <%d> (set_timing) [ERROR] no screen detected\n", m_id);
return false;
}
if (!kms_has_mode(mode))
add_mode(mode);
// If we can't be master, no need to go further
drmSetMaster(m_drm_fd);
if (!drmIsMaster(m_drm_fd))
return false;
// Setup the DRM mode structure
drmModeModeInfo dmode = {};
// Create specific mode name
snprintf(dmode.name, 32, "SR-%d_%dx%d@%.02f%s", m_id, mode->hactive, mode->vactive, mode->vfreq, mode->interlace ? "i" : "");
dmode.clock = mode->pclock / 1000;
dmode.hdisplay = mode->hactive;
dmode.hsync_start = mode->hbegin;
dmode.hsync_end = mode->hend;
dmode.htotal = mode->htotal;
dmode.vdisplay = mode->vactive;
dmode.vsync_start = mode->vbegin;
dmode.vsync_end = mode->vend;
dmode.vtotal = mode->vtotal;