-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrm.c
956 lines (801 loc) · 23.5 KB
/
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
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
/*
* Copyright 2014 The ChromiumOS Authors
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <time.h>
#include <unistd.h>
#include "drm.h"
#include "input.h"
#include "util.h"
static drm_t* g_drm = NULL;
static int32_t atomic_set_prop(drm_t* drm, drmModeAtomicReqPtr pset, uint32_t id,
drmModeObjectPropertiesPtr props, const char *name, uint64_t value)
{
uint32_t u;
int32_t ret;
drmModePropertyPtr prop;
for (u = 0; u < props->count_props; u++) {
prop = drmModeGetProperty(drm->fd, props->props[u]);
if (!prop)
continue;
if (strcmp(prop->name, name)) {
drmModeFreeProperty(prop);
continue;
}
ret = drmModeAtomicAddProperty(pset, id, prop->prop_id, value);
if (ret < 0)
LOG(ERROR, "setting atomic property %s failed with %d\n", name, ret);
else
ret = 0;
drmModeFreeProperty(prop);
return ret;
}
LOG(ERROR, "could not find atomic property %s\n", name);
return -ENOENT;
}
static int32_t crtc_planes_num(drm_t* drm, int32_t crtc_index)
{
drmModePlanePtr plane;
int32_t planes_num = 0;
drmModePlaneResPtr plane_resources = drmModeGetPlaneResources(drm->fd);
if (!plane_resources)
return 1; /* Just pretend there is one plane. */
for (uint32_t p = 0; p < plane_resources->count_planes; p++) {
plane = drmModeGetPlane(drm->fd, plane_resources->planes[p]);
if (plane->possible_crtcs & (1 << crtc_index))
planes_num++;
drmModeFreePlane(plane);
}
drmModeFreePlaneResources(plane_resources);
return planes_num;
}
static bool get_connector_path(drm_t* drm, uint32_t connector_id, uint32_t* ret_encoder_id, uint32_t* ret_crtc_id)
{
drmModeConnector* connector = drmModeGetConnector(drm->fd, connector_id);
drmModeEncoder* encoder;
if (!connector)
return false; /* Error. */
if (ret_encoder_id)
*ret_encoder_id = connector->encoder_id;
if (!connector->encoder_id) {
drmModeFreeConnector(connector);
if (ret_crtc_id)
*ret_crtc_id = 0;
return true; /* Not connected. */
}
encoder = drmModeGetEncoder(drm->fd, connector->encoder_id);
if (!encoder) {
if (ret_crtc_id)
*ret_crtc_id = 0;
drmModeFreeConnector(connector);
return false; /* Error. */
}
if (ret_crtc_id)
*ret_crtc_id = encoder->crtc_id;
drmModeFreeEncoder(encoder);
drmModeFreeConnector(connector);
return true; /* Connected. */
}
/* Find CRTC with most planes for given connector_id. */
static bool find_crtc_for_connector(drm_t* drm, uint32_t connector_id, uint32_t* ret_crtc_id)
{
int enc;
int32_t crtc_id = -1;
int32_t max_crtc_planes = -1;
drmModeConnector* connector = drmModeGetConnector(drm->fd, connector_id);
if (!connector)
return false;
for (enc = 0; enc < connector->count_encoders; enc++) {
int crtc;
drmModeEncoder* encoder = drmModeGetEncoder(drm->fd, connector->encoders[enc]);
if (encoder) {
for (crtc = 0; crtc < drm->resources->count_crtcs; crtc++) {
int32_t crtc_planes;
if (!(encoder->possible_crtcs & (1 << crtc)))
continue;
crtc_planes = crtc_planes_num(drm, crtc);
if (max_crtc_planes < crtc_planes) {
crtc_id = drm->resources->crtcs[crtc];
max_crtc_planes = crtc_planes;
}
}
drmModeFreeEncoder(encoder);
if (crtc_id != -1) {
if (ret_crtc_id)
*ret_crtc_id = crtc_id;
drmModeFreeConnector(connector);
return true;
}
}
}
drmModeFreeConnector(connector);
return false;
}
static int drm_is_primary_plane(drm_t* drm, uint32_t plane_id)
{
uint32_t p;
bool found = false;
int ret = -1;
drmModeObjectPropertiesPtr props;
props = drmModeObjectGetProperties(drm->fd,
plane_id,
DRM_MODE_OBJECT_PLANE);
if (!props) {
LOG(ERROR, "Unable to get plane properties: %m");
return -1;
}
for (p = 0; p < props->count_props && !found; p++) {
drmModePropertyPtr prop;
prop = drmModeGetProperty(drm->fd, props->props[p]);
if (prop) {
if (strcmp("type", prop->name) == 0) {
found = true;
ret = (props->prop_values[p] == DRM_PLANE_TYPE_PRIMARY);
}
drmModeFreeProperty(prop);
}
}
drmModeFreeObjectProperties(props);
return ret;
}
/* Disable all planes except for primary on crtc we use. */
static void drm_disable_non_primary_planes(drm_t* drm, uint32_t console_crtc_id)
{
int ret;
if (!drm->plane_resources)
return;
for (uint32_t p = 0; p < drm->plane_resources->count_planes; p++) {
drmModePlanePtr plane;
plane = drmModeGetPlane(drm->fd,
drm->plane_resources->planes[p]);
if (plane) {
int primary = drm_is_primary_plane(drm, plane->plane_id);
if (!(plane->crtc_id == console_crtc_id && primary != 0)) {
ret = drmModeSetPlane(drm->fd, plane->plane_id, plane->crtc_id,
0, 0,
0, 0,
0, 0,
0, 0,
0, 0);
if (ret) {
LOG(WARNING, "Unable to disable plane:%d %m", plane->plane_id);
}
}
drmModeFreePlane(plane);
}
}
}
static bool drm_is_internal(unsigned type)
{
unsigned t;
unsigned kInternalConnectors[] = {
DRM_MODE_CONNECTOR_LVDS,
DRM_MODE_CONNECTOR_eDP,
DRM_MODE_CONNECTOR_DSI,
};
for (t = 0; t < ARRAY_SIZE(kInternalConnectors); t++)
if (type == kInternalConnectors[t])
return true;
return false;
}
static drmModeConnector* find_first_connected_connector(drm_t* drm, bool internal, bool external)
{
for (int i = 0; i < drm->resources->count_connectors; i++) {
drmModeConnector* connector;
connector = drmModeGetConnector(drm->fd, drm->resources->connectors[i]);
if (connector) {
bool is_internal = drm_is_internal(connector->connector_type);
if (!internal && is_internal)
continue;
if (!external && !is_internal)
continue;
if ((connector->count_modes > 0) &&
(connector->connection == DRM_MODE_CONNECTED))
return connector;
drmModeFreeConnector(connector);
}
}
return NULL;
}
static int find_panel_orientation(drm_t *drm)
{
uint32_t p;
bool found = false;
drmModeObjectPropertiesPtr props;
props = drmModeObjectGetProperties(drm->fd,
drm->console_connector_id,
DRM_MODE_OBJECT_CONNECTOR);
if (!props) {
LOG(ERROR, "Unable to get connector properties: %m");
return -1;
}
for (p = 0; p < props->count_props && !found; p++) {
drmModePropertyPtr prop;
prop = drmModeGetProperty(drm->fd, props->props[p]);
if (prop) {
if (strcmp("panel orientation", prop->name) == 0) {
found = true;
drm->panel_orientation = (int32_t)(props->prop_values[p]);
}
drmModeFreeProperty(prop);
}
}
drmModeFreeObjectProperties(props);
return 0;
}
static bool find_main_monitor(drm_t* drm)
{
int modes;
uint32_t console_crtc_id = 0;
int lid_state = input_check_lid_state();
drmModeConnector* main_monitor_connector = NULL;
drm->console_connector_id = 0;
/*
* Find the LVDS/eDP/DSI connectors. Those are the main screens.
*/
if (lid_state <= 0)
main_monitor_connector = find_first_connected_connector(drm, true, false);
/*
* Now try external connectors.
*/
if (!main_monitor_connector)
main_monitor_connector =
find_first_connected_connector(drm, false, true);
/*
* If we still didn't find a connector, give up and return.
*/
if (!main_monitor_connector)
return false;
if (!main_monitor_connector->count_modes)
return false;
drm->console_connector_id = main_monitor_connector->connector_id;
drm->console_connector_internal = drm_is_internal(main_monitor_connector->connector_type);
drm->console_mmWidth = main_monitor_connector->mmWidth;
drm->console_mmHeight = main_monitor_connector->mmHeight;
for (modes = 0; modes < main_monitor_connector->count_modes; modes++) {
if (main_monitor_connector->modes[modes].type &
DRM_MODE_TYPE_PREFERRED) {
drm->console_mode_info = main_monitor_connector->modes[modes];
break;
}
}
/* If there was no preferred mode use first one. */
if (modes == main_monitor_connector->count_modes)
drm->console_mode_info = main_monitor_connector->modes[0];
find_panel_orientation(drm);
drmModeFreeConnector(main_monitor_connector);
get_connector_path(drm, drm->console_connector_id, NULL, &console_crtc_id);
if (!console_crtc_id)
/* No existing path, find one. */
find_crtc_for_connector(drm, drm->console_connector_id, &console_crtc_id);
if (!console_crtc_id)
/* Cannot find CRTC for connector. We will not be able to use it. */
return false;
return true;
}
static void drm_clear_rmfb(drm_t* drm)
{
if (drm->delayed_rmfb_fb_id) {
drmModeRmFB(drm->fd, drm->delayed_rmfb_fb_id);
drm->delayed_rmfb_fb_id = 0;
}
}
static void drm_fini(drm_t* drm)
{
if (!drm)
return;
if (drm->fd >= 0) {
drm_clear_rmfb(drm);
if (drm->plane_resources) {
drmModeFreePlaneResources(drm->plane_resources);
drm->plane_resources = NULL;
}
if (drm->resources) {
drmModeFreeResources(drm->resources);
drm->resources = NULL;
}
drmClose(drm->fd);
drm->fd = -1;
}
free(drm);
}
static bool drm_equal(drm_t* l, drm_t* r)
{
if (!l && !r)
return true;
if ((!l && r) || (l && !r))
return false;
if (l->console_connector_id != r->console_connector_id)
return false;
return true;
}
static int drm_score(drm_t* drm)
{
drmVersionPtr version;
int score = 0;
if (!drm)
return -1000000000;
if (!drm->console_connector_id)
return -1000000000;
if (drm->console_connector_internal)
score++;
version = drmGetVersion(drm->fd);
if (version) {
/* We would rather use any driver besides UDL. */
if (strcmp("udl", version->name) == 0)
score--;
if (strcmp("evdi", version->name) == 0)
score--;
/* VGEM should be ignored because it has no displays, but lets make sure. */
if (strcmp("vgem", version->name) == 0)
score -= 1000000;
drmFreeVersion(version);
}
return score;
}
/*
* Scan and find best DRM object to display frecon on.
* This object should be created with DRM master, and we will keep master till
* first mode set or explicit drop master.
*/
drm_t* drm_scan(void)
{
unsigned i;
char* dev_name;
int ret;
drm_t *best_drm = NULL;
for (i = 0; i < DRM_MAX_MINOR; i++) {
uint64_t atomic = 0;
drm_t* drm = calloc(1, sizeof(drm_t));
if (!drm)
return NULL;
try_open_again:
ret = asprintf(&dev_name, DRM_DEV_NAME, DRM_DIR_NAME, i);
if (ret < 0) {
drm_fini(drm);
continue;
}
drm->fd = open(dev_name, O_RDWR | O_CLOEXEC, 0);
free(dev_name);
if (drm->fd < 0) {
drm_fini(drm);
continue;
}
/* if we have master this should succeed */
ret = drmSetMaster(drm->fd);
if (ret != 0) {
drmClose(drm->fd);
drm->fd = -1;
usleep(100*1000);
goto try_open_again;
}
/* Set universal planes cap if possible. Ignore any errors. */
drmSetClientCap(drm->fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
ret = drmGetCap(drm->fd, DRM_CLIENT_CAP_ATOMIC, &atomic);
if (!ret && atomic) {
drm->atomic = true;
ret = drmSetClientCap(drm->fd, DRM_CLIENT_CAP_ATOMIC, 1);
if (ret < 0) {
LOG(ERROR, "Failed to set atomic cap.");
drm->atomic = false;
}
}
drm->resources = drmModeGetResources(drm->fd);
if (!drm->resources) {
drm_fini(drm);
continue;
}
/* Expect at least one crtc so we do not try to run on VGEM. */
if (drm->resources->count_crtcs == 0 || drm->resources->count_connectors == 0) {
drm_fini(drm);
continue;
}
drm->plane_resources = drmModeGetPlaneResources(drm->fd);
if (!find_main_monitor(drm)) {
drm_fini(drm);
continue;
}
drm->refcount = 1;
if (drm_score(drm) > drm_score(best_drm)) {
drm_fini(best_drm);
best_drm = drm;
} else {
drm_fini(drm);
}
}
if (best_drm) {
drmVersionPtr version;
version = drmGetVersion(best_drm->fd);
if (version) {
LOG(INFO,
"Frecon using drm driver %s, version %d.%d, date(%s), desc(%s)%s",
version->name,
version->version_major,
version->version_minor,
version->date,
version->desc,
best_drm->atomic ? " using atomic" : "");
drmFreeVersion(version);
}
}
return best_drm;
}
void drm_set(drm_t* drm_)
{
if (g_drm) {
drm_delref(g_drm);
g_drm = NULL;
}
g_drm = drm_;
}
void drm_close(void)
{
if (g_drm) {
drm_delref(g_drm);
g_drm = NULL;
}
}
void drm_delref(drm_t* drm)
{
if (!drm)
return;
if (drm->refcount) {
drm->refcount--;
} else {
LOG(ERROR, "Imbalanced drm_close()");
}
if (drm->refcount) {
return;
}
drm_fini(drm);
}
drm_t* drm_addref(void)
{
if (g_drm) {
g_drm->refcount++;
return g_drm;
}
return NULL;
}
int drm_dropmaster(drm_t* drm)
{
int ret = 0;
if (!drm)
drm = g_drm;
if (drm)
ret = drmDropMaster(drm->fd);
return ret;
}
int drm_setmaster(drm_t* drm)
{
int ret = 0;
if (!drm)
drm = g_drm;
if (drm)
ret = drmSetMaster(drm->fd);
return ret;
}
/*
* Returns true if connector/crtc/driver have changed and framebuffer object have to be re-created.
*/
bool drm_rescan(void)
{
drm_t* ndrm;
/* In case we had master, drop master so the newly created object could have it. */
drm_dropmaster(g_drm);
ndrm = drm_scan();
if (ndrm) {
if (drm_equal(ndrm, g_drm)) {
drm_fini(ndrm);
/* Regain master we dropped. */
drm_setmaster(g_drm);
} else {
drm_delref(g_drm);
g_drm = ndrm;
return true;
}
} else {
if (g_drm) {
drm_delref(g_drm); /* No usable monitor/drm object. */
g_drm = NULL;
return true;
}
}
return false;
}
bool drm_valid(drm_t* drm) {
return drm && drm->fd >= 0 && drm->resources && drm->console_connector_id;
}
static bool is_crtc_possible(drm_t* drm, uint32_t crtc_id, uint32_t mask)
{
int32_t crtc;
for (crtc = 0; crtc < drm->resources->count_crtcs; crtc++)
if (drm->resources->crtcs[crtc] == crtc_id)
return !!(mask & (1u << crtc));
return false;
}
#define CHECK(fn) do { ret = fn; if (ret < 0) goto error_mode; } while (0)
static int32_t drm_setmode_atomic(drm_t* drm, uint32_t fb_id)
{
int32_t ret;
int32_t crtc, conn;
uint32_t plane;
uint32_t console_crtc_id = 0;
drmModeObjectPropertiesPtr crtc_props = NULL;
drmModeObjectPropertiesPtr plane_props = NULL;
drmModeObjectPropertiesPtr conn_props = NULL;
drmModePlaneResPtr plane_resources;
drmModeAtomicReqPtr pset = NULL;
uint32_t mode_id = 0;
plane_resources = drmModeGetPlaneResources(drm->fd);
if (!plane_resources)
return -ENOENT;
get_connector_path(drm, drm->console_connector_id, NULL, &console_crtc_id);
if (!console_crtc_id)
find_crtc_for_connector(drm, drm->console_connector_id, &console_crtc_id);
if (!console_crtc_id) {
LOG(ERROR, "Could not get console crtc for connector:%d in modeset.\n", drm->console_connector_id);
return -ENOENT;
}
pset = drmModeAtomicAlloc();
if (!pset) {
ret = -ENOMEM;
goto error_mode;
}
for (crtc = 0; crtc < drm->resources->count_crtcs; crtc++) {
uint32_t crtc_id = drm->resources->crtcs[crtc];
crtc_props = drmModeObjectGetProperties(drm->fd, crtc_id, DRM_MODE_OBJECT_CRTC);
if (!crtc_props) {
LOG(ERROR, "Could not query properties for crtc %d %m.", crtc_id);
if (crtc_id != console_crtc_id)
continue;
ret = -ENOENT;
goto error_mode;
}
if (crtc_id == console_crtc_id) {
CHECK(drmModeCreatePropertyBlob(drm->fd, &drm->console_mode_info,
sizeof(drm->console_mode_info),
&mode_id));
/* drm->crtc->mode has been set during init */
CHECK(atomic_set_prop(drm, pset, crtc_id, crtc_props, "MODE_ID", mode_id));
CHECK(atomic_set_prop(drm, pset, crtc_id, crtc_props, "ACTIVE", 1));
/* Reset color matrix to identity and gamma/degamma LUTs to pass through,
* ignore errors in case they are not supported. */
atomic_set_prop(drm, pset, crtc_id, crtc_props, "CTM", 0);
atomic_set_prop(drm, pset, crtc_id, crtc_props, "DEGAMMA_LUT", 0);
atomic_set_prop(drm, pset, crtc_id, crtc_props, "GAMMA_LUT", 0);
} else {
CHECK(atomic_set_prop(drm, pset, crtc_id, crtc_props, "MODE_ID", 0));
CHECK(atomic_set_prop(drm, pset, crtc_id, crtc_props, "ACTIVE", 0));
}
drmModeFreeObjectProperties(crtc_props);
crtc_props = NULL;
}
for (plane = 0; plane < plane_resources->count_planes; plane++) {
drmModePlanePtr planeobj;
uint32_t plane_id = plane_resources->planes[plane];
uint32_t possible_crtcs;
int primary;
planeobj = drmModeGetPlane(drm->fd, plane_id);
if (!planeobj) {
LOG(ERROR, "Could not query plane object for plane %d %m.", plane_id);
ret = -ENOENT;
goto error_mode;
}
possible_crtcs = planeobj->possible_crtcs;
drmModeFreePlane(planeobj);
primary = drm_is_primary_plane(drm, plane_id);
plane_props = drmModeObjectGetProperties(drm->fd, plane_id, DRM_MODE_OBJECT_PLANE);
if (!plane_props) {
LOG(ERROR, "Could not query properties for plane %d %m.", plane_id);
ret = -ENOENT;
goto error_mode;
}
if (is_crtc_possible(drm, console_crtc_id, possible_crtcs) && primary) {
CHECK(atomic_set_prop(drm, pset, plane_id, plane_props, "FB_ID", fb_id));
CHECK(atomic_set_prop(drm, pset, plane_id, plane_props, "CRTC_ID", console_crtc_id));
CHECK(atomic_set_prop(drm, pset, plane_id, plane_props, "CRTC_X", 0));
CHECK(atomic_set_prop(drm, pset, plane_id, plane_props, "CRTC_Y", 0));
CHECK(atomic_set_prop(drm, pset, plane_id, plane_props, "CRTC_W", drm->console_mode_info.hdisplay));
CHECK(atomic_set_prop(drm, pset, plane_id, plane_props, "CRTC_H", drm->console_mode_info.vdisplay));
CHECK(atomic_set_prop(drm, pset, plane_id, plane_props, "SRC_X", 0));
CHECK(atomic_set_prop(drm, pset, plane_id, plane_props, "SRC_Y", 0));
CHECK(atomic_set_prop(drm, pset, plane_id, plane_props, "SRC_W", drm->console_mode_info.hdisplay << 16));
CHECK(atomic_set_prop(drm, pset, plane_id, plane_props, "SRC_H", drm->console_mode_info.vdisplay << 16));
} else {
CHECK(atomic_set_prop(drm, pset, plane_id, plane_props, "FB_ID", 0));
CHECK(atomic_set_prop(drm, pset, plane_id, plane_props, "CRTC_ID", 0));
}
drmModeFreeObjectProperties(plane_props);
plane_props = NULL;
}
for (conn = 0; conn < drm->resources->count_connectors; conn++) {
uint32_t conn_id = drm->resources->connectors[conn];
conn_props = drmModeObjectGetProperties(drm->fd, conn_id, DRM_MODE_OBJECT_CONNECTOR);
if (!conn_props) {
LOG(ERROR, "Could not query properties for connector %d %m.", conn_id);
if (conn_id != drm->console_connector_id)
continue;
ret = -ENOENT;
goto error_mode;
}
if (conn_id == drm->console_connector_id)
CHECK(atomic_set_prop(drm, pset, conn_id, conn_props, "CRTC_ID", console_crtc_id));
else
CHECK(atomic_set_prop(drm, pset, conn_id, conn_props, "CRTC_ID", 0));
drmModeFreeObjectProperties(conn_props);
conn_props = NULL;
}
ret = drmModeAtomicCommit(drm->fd, pset,
DRM_MODE_ATOMIC_ALLOW_MODESET , NULL);
if (ret < 0) {
drm_clear_rmfb(drm);
/* LOG(INFO, "TIMING: Console switch atomic modeset finished."); */
} else {
ret = 0;
}
error_mode:
if (mode_id)
drmModeDestroyPropertyBlob(drm->fd, mode_id);
if (plane_resources)
drmModeFreePlaneResources(plane_resources);
if (crtc_props)
drmModeFreeObjectProperties(crtc_props);
if (conn_props)
drmModeFreeObjectProperties(conn_props);
if (plane_props)
drmModeFreeObjectProperties(plane_props);
drmModeAtomicFree(pset);
return ret;
}
#undef CHECK
static int remove_gamma_properties(drm_t* drm, uint32_t crtc_id) {
drmModeObjectPropertiesPtr crtc_props = NULL;
crtc_props = drmModeObjectGetProperties(drm->fd,
crtc_id,
DRM_MODE_OBJECT_CRTC);
if (!crtc_props) {
LOG(ERROR, "Could not query properties for crtc %d %m.", crtc_id);
return -ENOENT;
}
for (uint32_t i = 0; i < crtc_props->count_props; i++) {
drmModePropertyPtr prop;
prop = drmModeGetProperty(drm->fd, crtc_props->props[i]);
if (!prop)
continue;
// Remove the GAMMA_LUT and DEGAMMA_LUT properties.
if (!strcmp(prop->name, "GAMMA_LUT") ||
!strcmp(prop->name, "DEGAMMA_LUT")) {
// Ignore the return in case it is not supported.
if (drmModeObjectSetProperty(drm->fd, crtc_id,
DRM_MODE_OBJECT_CRTC,
crtc_props->props[i],
0)) {
LOG(ERROR, "Unable to remove %s from crtc:%d %m", prop->name, crtc_id);
}
}
drmModeFreeProperty(prop);
}
drmModeFreeObjectProperties(crtc_props);
return 0;
}
int32_t drm_setmode(drm_t* drm, uint32_t fb_id)
{
int conn;
int32_t ret;
uint32_t existing_console_crtc_id = 0;
if (drm->atomic)
if (drm_setmode_atomic(drm, fb_id) == 0)
return 0;
/* Fallback to legacy mode set. */
get_connector_path(drm, drm->console_connector_id, NULL, &existing_console_crtc_id);
/* Loop through all the connectors, disable ones that are configured and set video mode on console connector. */
for (conn = 0; conn < drm->resources->count_connectors; conn++) {
uint32_t connector_id = drm->resources->connectors[conn];
if (connector_id == drm->console_connector_id) {
uint32_t console_crtc_id = 0;
if (existing_console_crtc_id)
console_crtc_id = existing_console_crtc_id;
else {
find_crtc_for_connector(drm, connector_id, &console_crtc_id);
if (!console_crtc_id) {
LOG(ERROR, "Could not get console crtc for connector:%d in modeset.\n", drm->console_connector_id);
return -ENOENT;
}
}
ret = drmModeSetCrtc(drm->fd, console_crtc_id,
fb_id,
0, 0, // x,y
&drm->console_connector_id,
1, // connector_count
&drm->console_mode_info); // mode
if (ret) {
LOG(ERROR, "Unable to set crtc:%d connector:%d %m", console_crtc_id, drm->console_connector_id);
return ret;
}
ret = drmModeSetCursor(drm->fd, console_crtc_id,
0, 0, 0);
if (ret)
LOG(ERROR, "Unable to hide cursor on crtc:%d %m.", console_crtc_id);
ret = remove_gamma_properties(drm, console_crtc_id);
if (ret)
LOG(ERROR, "Unable to remove gamma LUT properties from crtc:%d %m.", console_crtc_id);
drm_disable_non_primary_planes(drm, console_crtc_id);
} else {
uint32_t crtc_id = 0;
get_connector_path(drm, connector_id, NULL, &crtc_id);
if (!crtc_id)
/* This connector is not configured, skip. */
continue;
if (existing_console_crtc_id && existing_console_crtc_id == crtc_id)
/* This connector is mirroring from the same CRTC as console. It will be turned off when console is set. */
continue;
ret = drmModeSetCrtc(drm->fd, crtc_id, 0, // buffer_id
0, 0, // x,y
NULL, // connectors
0, // connector_count
NULL); // mode
if (ret)
LOG(ERROR, "Unable to disable crtc %d: %m", crtc_id);
}
}
drm_clear_rmfb(drm);
/* LOG(INFO, "TIMING: Console switch modeset finished."); */
return ret;
}
/*
* Delayed rmfb(). We want to keep fb at least till after next modeset
* so our transitions are cleaner (e.g. when recreating term after exitin
* shell). Also it keeps fb around till Chrome starts.
*/
void drm_rmfb(drm_t* drm, uint32_t fb_id)
{
drm_clear_rmfb(drm);
drm->delayed_rmfb_fb_id = fb_id;
}
bool drm_read_edid(drm_t* drm)
{
drmModeConnector* console_connector;
if (drm->edid_found) {
return true;
}
console_connector = drmModeGetConnector(drm->fd, drm->console_connector_id);
if (!console_connector)
return false;
for (int i = 0; i < console_connector->count_props; i++) {
drmModePropertyPtr prop;
drmModePropertyBlobPtr blob_ptr;
prop = drmModeGetProperty(drm->fd, console_connector->props[i]);
if (prop) {
if (strcmp(prop->name, "EDID") == 0) {
blob_ptr = drmModeGetPropertyBlob(drm->fd,
console_connector->prop_values[i]);
if (blob_ptr) {
memcpy(&drm->edid, blob_ptr->data, EDID_SIZE);
drmModeFreePropertyBlob(blob_ptr);
drmModeFreeConnector(console_connector);
return (drm->edid_found = true);
}
}
}
}
drmModeFreeConnector(console_connector);
return false;
}
uint32_t drm_gethres(drm_t* drm)
{
return drm->console_mode_info.hdisplay;
}
uint32_t drm_getvres(drm_t* drm)
{
return drm->console_mode_info.vdisplay;
}