-
Notifications
You must be signed in to change notification settings - Fork 5
/
drm_cursor.c
1304 lines (1037 loc) · 32.6 KB
/
drm_cursor.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
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
/*
* Copyright (c) 2021, Jeffy Chen <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <xf86drm.h>
#include <xf86drmMode.h>
#include <gbm.h>
#include "drm_common.h"
#include "drm_egl.h"
#define DRM_CURSOR_CONFIG_FILE "/etc/drm-cursor.conf"
#define OPT_DEBUG "debug="
#define OPT_LOG_FILE "log-file="
#define OPT_HIDE "hide="
#define OPT_ALLOW_OVERLAY "allow-overlay="
#define OPT_PREFER_AFBC "prefer-afbc="
#define OPT_PREFER_PLANE "prefer-plane="
#define OPT_PREFER_PLANES "prefer-planes="
#define OPT_CRTC_BLOCKLIST "crtc-blocklist="
#define OPT_NUM_SURFACES "num-surfaces="
#define OPT_MAX_FPS "max-fps="
#define OPT_ATOMIC "atomic="
#define OPT_SCALE "scale="
#define OPT_SCALE_FROM "scale-from="
#define DRM_MAX_CRTCS 8
typedef enum {
PLANE_PROP_type = 0,
PLANE_PROP_IN_FORMATS,
PLANE_PROP_zpos,
PLANE_PROP_ZPOS,
PLANE_PROP_ASYNC_COMMIT,
PLANE_PROP_CRTC_ID,
PLANE_PROP_FB_ID,
PLANE_PROP_SRC_X,
PLANE_PROP_SRC_Y,
PLANE_PROP_SRC_W,
PLANE_PROP_SRC_H,
PLANE_PROP_CRTC_X,
PLANE_PROP_CRTC_Y,
PLANE_PROP_CRTC_W,
PLANE_PROP_CRTC_H,
PLANE_PROP_MAX,
} drm_plane_prop;
static const char *drm_plane_prop_names[] = {
[PLANE_PROP_type] = "type",
[PLANE_PROP_IN_FORMATS] = "IN_FORMATS",
[PLANE_PROP_zpos] = "zpos",
[PLANE_PROP_ZPOS] = "ZPOS",
[PLANE_PROP_ASYNC_COMMIT] = "ASYNC_COMMIT",
[PLANE_PROP_CRTC_ID] = "CRTC_ID",
[PLANE_PROP_FB_ID] = "FB_ID",
[PLANE_PROP_SRC_X] = "SRC_X",
[PLANE_PROP_SRC_Y] = "SRC_Y",
[PLANE_PROP_SRC_W] = "SRC_W",
[PLANE_PROP_SRC_H] = "SRC_H",
[PLANE_PROP_CRTC_X] = "CRTC_X",
[PLANE_PROP_CRTC_Y] = "CRTC_Y",
[PLANE_PROP_CRTC_W] = "CRTC_W",
[PLANE_PROP_CRTC_H] = "CRTC_H",
};
typedef struct {
uint32_t plane_id;
int cursor_plane;
int can_afbc;
int can_linear;
drmModePlane *plane;
drmModeObjectProperties *props;
int prop_ids[PLANE_PROP_MAX];
} drm_plane;
#define REQ_SET_CURSOR (1 << 0)
#define REQ_MOVE_CURSOR (1 << 1)
typedef struct {
uint32_t handle;
uint32_t fb;
int width;
int height;
int scaled_w;
int scaled_h;
int x;
int y;
int scaled_x;
int scaled_y;
int off_x;
int off_y;
int hot_x;
int hot_y;
int request;
} drm_cursor_state;
typedef enum {
IDLE = 0,
FATAL_ERROR,
PENDING,
} drm_thread_state;
typedef struct {
uint32_t crtc_id;
uint32_t crtc_pipe;
int width;
int height;
drm_plane *plane;
uint32_t prefer_plane_id;
drm_cursor_state cursor_next;
drm_cursor_state cursor_curr;
pthread_t thread;
pthread_cond_t cond;
pthread_mutex_t mutex;
drm_thread_state state;
void *egl_ctx;
int verified;
int use_afbc_modifier;
int blocked;
int async_commit;
uint64_t last_update_time;
} drm_crtc;
typedef struct {
int fd;
drm_crtc crtcs[DRM_MAX_CRTCS];
int num_crtcs;
drmModePlaneResPtr pres;
drmModeRes *res;
int prefer_afbc_modifier;
int allow_overlay;
int num_surfaces;
int inited;
int atomic;
int hide;
uint64_t min_interval;
float scale_x, scale_y;
float scale_from;
char *configs;
} drm_ctx;
static drm_ctx g_drm_ctx = { 0, };
drm_private int g_drm_debug = 0;
drm_private FILE *g_log_fp = NULL;
static inline uint64_t drm_curr_time(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec + tv.tv_usec / 1000;
}
static int drm_plane_get_prop(drm_ctx *ctx, drm_plane *plane, drm_plane_prop p)
{
drmModePropertyPtr prop;
uint32_t i;
if (plane->prop_ids[p])
return plane->prop_ids[p];
for (i = 0; i < plane->props->count_props; i++) {
prop = drmModeGetProperty(ctx->fd, plane->props->props[i]);
if (prop && !strcmp(prop->name, drm_plane_prop_names[p])) {
drmModeFreeProperty(prop);
plane->prop_ids[p] = i;
return i;
}
drmModeFreeProperty(prop);
}
return -1;
}
static int drm_atomic_add_plane_prop(drm_ctx *ctx, drmModeAtomicReq *request,
drm_plane *plane, drm_plane_prop p,
uint64_t value)
{
int prop_idx = drm_plane_get_prop(ctx, plane, p);
if (prop_idx < 0)
return -1;
return drmModeAtomicAddProperty(request, plane->plane_id,
plane->props->props[prop_idx], value);
}
static int drm_set_plane(drm_ctx *ctx, drm_crtc *crtc, drm_plane *plane,
uint32_t fb, int x, int y, int w, int h)
{
drmModeAtomicReq *req;
int ret = 0;
if (plane->cursor_plane || crtc->async_commit || !ctx->atomic)
goto legacy;
req = drmModeAtomicAlloc();
if (!req)
goto legacy;
if (!fb) {
ret |= drm_atomic_add_plane_prop(ctx, req, plane, PLANE_PROP_CRTC_ID, 0);
ret |= drm_atomic_add_plane_prop(ctx, req, plane, PLANE_PROP_FB_ID, 0);
} else {
ret |= drm_atomic_add_plane_prop(ctx, req, plane,
PLANE_PROP_CRTC_ID, crtc->crtc_id);
ret |= drm_atomic_add_plane_prop(ctx, req, plane, PLANE_PROP_FB_ID, fb);
ret |= drm_atomic_add_plane_prop(ctx, req, plane, PLANE_PROP_SRC_X, 0);
ret |= drm_atomic_add_plane_prop(ctx, req, plane, PLANE_PROP_SRC_Y, 0);
ret |= drm_atomic_add_plane_prop(ctx, req, plane,
PLANE_PROP_SRC_W, w << 16);
ret |= drm_atomic_add_plane_prop(ctx, req,
plane, PLANE_PROP_SRC_H, h << 16);
ret |= drm_atomic_add_plane_prop(ctx, req, plane, PLANE_PROP_CRTC_X, x);
ret |= drm_atomic_add_plane_prop(ctx, req, plane, PLANE_PROP_CRTC_Y, y);
ret |= drm_atomic_add_plane_prop(ctx, req, plane, PLANE_PROP_CRTC_W, w);
ret |= drm_atomic_add_plane_prop(ctx, req, plane, PLANE_PROP_CRTC_H, h);
}
ret |= drmModeAtomicCommit(ctx->fd, req, DRM_MODE_ATOMIC_NONBLOCK, NULL);
drmModeAtomicFree(req);
if (ret >= 0)
return 0;
legacy:
if (ret < 0 && ctx->atomic) {
DRM_ERROR("CRTC[%d]: failed to do atomic commit (%d)\n",
crtc->crtc_id, errno);
ctx->atomic = 0;
}
return drmModeSetPlane(ctx->fd, plane->plane_id, crtc->crtc_id, fb, 0,
x, y, w, h, 0, 0, w << 16, h << 16);
}
static int drm_plane_get_prop_value(drm_ctx *ctx, drm_plane *plane,
drm_plane_prop p, uint64_t *value)
{
int prop_idx = drm_plane_get_prop(ctx, plane, p);
if (prop_idx < 0)
return -1;
*value = plane->props->prop_values[prop_idx];
return 0;
}
static int drm_plane_set_prop_max(drm_ctx *ctx, drm_plane *plane,
drm_plane_prop p)
{
drmModePropertyPtr prop;
int prop_idx = drm_plane_get_prop(ctx, plane, p);
if (prop_idx < 0)
return -1;
prop = drmModeGetProperty(ctx->fd, plane->props->props[prop_idx]);
drmModeObjectSetProperty (ctx->fd, plane->plane_id,
DRM_MODE_OBJECT_PLANE,
plane->props->props[prop_idx],
prop->values[prop->count_values - 1]);
DRM_DEBUG("set plane %d prop: %s to max: %"PRIu64"\n",
plane->plane_id, drm_plane_prop_names[p],
prop->values[prop->count_values - 1]);
drmModeFreeProperty(prop);
return 0;
}
static void drm_free_plane(drm_plane *plane)
{
drmModeFreeObjectProperties(plane->props);
drmModeFreePlane(plane->plane);
free(plane);
}
static void drm_plane_update_format(drm_ctx *ctx, drm_plane *plane)
{
drmModePropertyBlobPtr blob;
struct drm_format_modifier_blob *header;
struct drm_format_modifier *modifiers;
uint32_t *formats;
uint64_t value;
uint32_t i, j;
plane->can_afbc = plane->can_linear = 0;
/* Check formats */
for (i = 0; i < plane->plane->count_formats; i++) {
if (plane->plane->formats[i] == DRM_FORMAT_ARGB8888)
break;
}
if (i == plane->plane->count_formats)
return;
if (drm_plane_get_prop_value(ctx, plane, PLANE_PROP_IN_FORMATS, &value) < 0) {
/* No in_formats */
plane->can_linear = 1;
return;
}
blob = drmModeGetPropertyBlob(ctx->fd, value);
if (!blob)
return;
header = blob->data;
formats = (uint32_t *) ((char *) header + header->formats_offset);
modifiers = (struct drm_format_modifier *)
((char *) header + header->modifiers_offset);
/* Check in_formats */
for (i = 0; i < header->count_formats; i++) {
if (formats[i] == DRM_FORMAT_ARGB8888)
break;
}
if (i == header->count_formats)
goto out;
if (!header->count_modifiers) {
plane->can_linear = 1;
goto out;
}
/* Check modifiers */
for (j = 0; j < header->count_modifiers; j++) {
struct drm_format_modifier *mod = &modifiers[j];
if ((i < mod->offset) || (i > mod->offset + 63))
continue;
if (!(mod->formats & (1 << (i - mod->offset))))
continue;
if (mod->modifier == DRM_AFBC_MODIFIER)
plane->can_afbc = 1;
if (mod->modifier == DRM_FORMAT_MOD_LINEAR)
plane->can_linear = 1;
}
out:
drmModeFreePropertyBlob(blob);
}
static drm_plane *drm_get_plane(drm_ctx *ctx, uint32_t plane_id)
{
drm_plane *plane = calloc(1, sizeof(*plane));
if (!plane)
return NULL;
plane->plane_id = plane_id;
plane->plane = drmModeGetPlane(ctx->fd, plane_id);
if (!plane->plane)
goto err;
plane->props = drmModeObjectGetProperties(ctx->fd, plane_id,
DRM_MODE_OBJECT_PLANE);
if (!plane->props)
goto err;
drm_plane_update_format(ctx, plane);
return plane;
err:
drm_free_plane(plane);
return NULL;
}
static void drm_load_configs(drm_ctx *ctx)
{
struct stat st;
const char *file = DRM_CURSOR_CONFIG_FILE;
char *ptr, *tmp;
int fd;
if (stat(file, &st) < 0)
return;
fd = open(file, O_RDONLY);
if (fd < 0)
return;
ptr = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
if (ptr == MAP_FAILED)
goto out_close_fd;
ctx->configs = malloc(st.st_size + 1);
if (!ctx->configs)
goto out_unmap;
memcpy(ctx->configs, ptr, st.st_size);
ctx->configs[st.st_size] = '\0';
tmp = ctx->configs;
while ((tmp = strchr(tmp, '#'))) {
while (*tmp != '\n' && *tmp != '\0')
*tmp++ = '\n';
}
out_unmap:
munmap(ptr, st.st_size);
out_close_fd:
close(fd);
}
static const char *drm_get_config(drm_ctx *ctx, const char *name)
{
static char buf[4096];
const char *config;
if (!ctx->configs)
return NULL;
config = strstr(ctx->configs, name);
if (!config)
return NULL;
if (config[strlen(name)] == '\n' || config[strlen(name)] == '\r')
return NULL;
sscanf(config + strlen(name), "%4095s", buf);
return buf;
}
static int drm_get_config_int(drm_ctx *ctx, const char *name, int def)
{
const char *config = drm_get_config(ctx, name);
if (config)
return atoi(config);
return def;
}
static drm_ctx *drm_get_ctx(int fd)
{
drm_ctx *ctx = &g_drm_ctx;
uint32_t prefer_planes[DRM_MAX_CRTCS] = { 0, };
uint32_t prefer_plane = 0;
uint32_t i, max_fps, count_crtcs;
const char *config;
if (fd < 0)
return ctx;
if (ctx->inited) {
/* Make sure the ctx's fd is the same as the input fd */
int flags = fcntl(ctx->fd, F_GETFL, 0);
if (fcntl(fd, F_GETFL, 0) == flags) {
fcntl(ctx->fd, F_SETFL, flags ^ O_NONBLOCK);
if (fcntl(fd, F_GETFL, 0) != flags) {
fcntl(ctx->fd, F_SETFL, flags);
return ctx;
}
}
close(ctx->fd);
ctx->fd = dup(fd);
return ctx;
}
/* Failed already */
if (ctx->fd < 0)
return NULL;
ctx->fd = dup(fd);
if (ctx->fd < 0)
return NULL;
drm_load_configs(ctx);
g_drm_debug = drm_get_config_int(ctx, OPT_DEBUG, 0);
if (getenv("DRM_DEBUG") || !access("/tmp/.drm_cursor_debug", F_OK))
g_drm_debug = 1;
if (!(config = getenv("DRM_CURSOR_LOG_FILE")))
config = drm_get_config(ctx, OPT_LOG_FILE);
g_log_fp = fopen(config ? config : "/var/log/drm-cursor.log", "wb+");
ctx->atomic = drm_get_config_int(ctx, OPT_ATOMIC, 1);
DRM_INFO("atomic drm API %s\n", ctx->atomic ? "enabled" : "disabled");
ctx->hide = drm_get_config_int(ctx, OPT_HIDE, 0);
if (ctx->hide)
DRM_INFO("invisible cursors\n");
#ifdef PREFER_AFBC_MODIFIER
ctx->prefer_afbc_modifier = 1;
#endif
ctx->prefer_afbc_modifier =
drm_get_config_int(ctx, OPT_PREFER_AFBC, ctx->prefer_afbc_modifier);
if (ctx->prefer_afbc_modifier)
DRM_DEBUG("prefer ARM AFBC modifier\n");
ctx->allow_overlay = drm_get_config_int(ctx, OPT_ALLOW_OVERLAY, 0);
if (ctx->allow_overlay)
DRM_DEBUG("allow overlay planes\n");
drmSetClientCap(ctx->fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
ctx->num_surfaces = drm_get_config_int(ctx, OPT_NUM_SURFACES, 8);
max_fps = drm_get_config_int(ctx, OPT_MAX_FPS, 0);
if (max_fps <= 0)
max_fps = 60;
ctx->min_interval = 1000 / max_fps;
if (ctx->min_interval)
ctx->min_interval--;
DRM_INFO("max fps: %d\n", max_fps);
config = drm_get_config(ctx, OPT_SCALE_FROM);
if (config) {
int w, h, screen_w, screen_h;
if (config &&
sscanf(config, "%dx%d/%dx%d", &w, &h, &screen_w, &screen_h) == 4) {
ctx->scale_from = 1.0 * w * h / screen_w / screen_h;
DRM_INFO("scale from: %s\n", config);
}
} else {
config = drm_get_config(ctx, OPT_SCALE);
if (config && sscanf(config, "%fx%f", &ctx->scale_x, &ctx->scale_y) == 2)
DRM_INFO("scale: %s\n", config);
}
ctx->res = drmModeGetResources(ctx->fd);
if (!ctx->res)
goto err_free_configs;
ctx->pres = drmModeGetPlaneResources(ctx->fd);
if (!ctx->pres)
goto err_free_res;
count_crtcs = ctx->res->count_crtcs;
/* Allow specifying prefer plane */
if ((config = getenv("DRM_CURSOR_PREFER_PLANE")))
prefer_plane = atoi(config);
else
prefer_plane = drm_get_config_int(ctx, OPT_PREFER_PLANE, 0);
/* Allow specifying prefer planes */
if (!(config = getenv("DRM_CURSOR_PREFER_PLANES")))
config = drm_get_config(ctx, OPT_PREFER_PLANES);
for (i = 0; config && i < count_crtcs; i++) {
prefer_planes[i] = atoi(config);
config = strchr(config, ',');
if (config)
config++;
}
/* Fetch all CRTCs */
for (i = 0; i < count_crtcs; i++) {
drmModeCrtcPtr c = drmModeGetCrtc(ctx->fd, ctx->res->crtcs[i]);
drm_crtc *crtc = &ctx->crtcs[ctx->num_crtcs];
if (!c)
continue;
crtc->crtc_id = c->crtc_id;
crtc->crtc_pipe = i;
crtc->prefer_plane_id = prefer_planes[i] ? prefer_planes[i] : prefer_plane;
DRM_DEBUG("found %d CRTC: %d(%d) (%dx%d) prefer plane: %d\n",
ctx->num_crtcs, c->crtc_id, i, c->width, c->height,
crtc->prefer_plane_id);
ctx->num_crtcs++;
drmModeFreeCrtc(c);
}
DRM_DEBUG("found %d CRTCs\n", ctx->num_crtcs);
if (!ctx->num_crtcs)
goto err_free_pres;
config = drm_get_config(ctx, OPT_CRTC_BLOCKLIST);
for (i = 0; config && i < count_crtcs; i++) {
uint32_t crtc_id = atoi(config);
for (int j = 0; j < ctx->num_crtcs; j++) {
drm_crtc *crtc = &ctx->crtcs[j];
if (crtc->crtc_id != crtc_id)
continue;
DRM_DEBUG("CRTC: %d blocked\n", crtc_id);
crtc->blocked = 1;
}
config = strchr(config, ',');
if (config)
config++;
}
if (g_drm_debug) {
/* Dump planes for debugging */
for (i = 0; i < ctx->pres->count_planes; i++) {
drm_plane *plane = drm_get_plane(ctx, ctx->pres->planes[i]);
char *type;
uint64_t value = 0;
if (!plane)
continue;
drm_plane_get_prop_value(ctx, plane, PLANE_PROP_type, &value);
switch (value) {
case DRM_PLANE_TYPE_PRIMARY:
type = "primary";
break;
case DRM_PLANE_TYPE_OVERLAY:
type = "overlay";
break;
case DRM_PLANE_TYPE_CURSOR:
type = "cursor ";
break;
default:
type = "unknown";
break;
}
DRM_DEBUG("found plane: %d[%s] crtcs: 0x%x %s%s\n",
plane->plane_id, type, plane->plane->possible_crtcs,
plane->can_linear ? "(ARGB)" : "",
plane->can_afbc ? "(AFBC)" : "");
drm_free_plane(plane);
}
}
DRM_INFO("using libdrm-cursor (%s)\n", LIBDRM_CURSOR_VERSION);
ctx->inited = 1;
return ctx;
err_free_pres:
drmModeFreePlaneResources(ctx->pres);
err_free_res:
drmModeFreeResources(ctx->res);
err_free_configs:
free(ctx->configs);
close(ctx->fd);
ctx->fd = -1;
return NULL;
}
#define drm_crtc_bind_plane_force(ctx, crtc, plane) \
drm_crtc_bind_plane(ctx, crtc, plane, 1)
#define drm_crtc_bind_plane_cursor(ctx, crtc, plane) \
drm_crtc_bind_plane(ctx, crtc, plane, 0)
static int drm_crtc_bind_plane(drm_ctx *ctx, drm_crtc *crtc, uint32_t plane_id,
int allow_overlay)
{
drm_plane *plane;
uint64_t value;
int i;
/* CRTC already assigned */
if (crtc->plane)
return 1;
/* Plane already assigned */
for (i = 0; i < ctx->num_crtcs; i++) {
if (ctx->crtcs[i].plane && ctx->crtcs[i].plane->plane_id == plane_id)
return -1;
}
plane = drm_get_plane(ctx, plane_id);
if (!plane)
return -1;
/* Unable to use */
if (!plane->can_afbc && !plane->can_linear)
goto err;
/* Not for this CRTC */
if (!(plane->plane->possible_crtcs & (1 << crtc->crtc_pipe)))
goto err;
/* Not using primary planes */
if (drm_plane_get_prop_value(ctx, plane, PLANE_PROP_type, &value) < 0)
goto err;
if (value == DRM_PLANE_TYPE_PRIMARY)
goto err;
/* Check for overlay plane */
if (!allow_overlay && value == DRM_PLANE_TYPE_OVERLAY)
goto err;
plane->cursor_plane = value == DRM_PLANE_TYPE_CURSOR;
if (plane->cursor_plane)
DRM_INFO("CRTC[%d]: using cursor plane\n", crtc->crtc_id);
if (ctx->prefer_afbc_modifier && plane->can_afbc)
crtc->use_afbc_modifier = 1;
else if (!plane->can_linear)
crtc->use_afbc_modifier = 1;
DRM_DEBUG("CRTC[%d]: bind plane: %d%s\n", crtc->crtc_id, plane->plane_id,
crtc->use_afbc_modifier ? "(AFBC)" : "");
crtc->plane = plane;
return 0;
err:
drm_free_plane(plane);
return -1;
}
static int drm_crtc_valid(drm_crtc *crtc)
{
return (crtc->width > 0 && crtc->height > 0) ? 0 : -1;
}
static int drm_update_crtc(drm_ctx *ctx, drm_crtc *crtc)
{
drmModeCrtcPtr c;
int was_connected, connected;
c = drmModeGetCrtc(ctx->fd, crtc->crtc_id);
if (!c)
return -1;
was_connected = drm_crtc_valid(crtc) >= 0;
crtc->width = c->width;
crtc->height = c->height;
connected = drm_crtc_valid(crtc) >= 0;
drmModeFreeCrtc(c);
if (connected != was_connected)
DRM_DEBUG("CRTC[%d]: %s!\n", crtc->crtc_id, \
connected ? "connected" : "disconnected");
return drm_crtc_valid(crtc);
}
static int drm_crtc_update_offsets(drm_ctx *ctx, drm_crtc *crtc,
drm_cursor_state *cursor_state)
{
int x, y, off_x, off_y, width, height, area_w, area_h;
float scale_x, scale_y;
if (drm_update_crtc(ctx, crtc) < 0)
return -1;
width = cursor_state->width;
height = cursor_state->height;
if (ctx->scale_from) {
scale_x = scale_y =
ctx->scale_from * crtc->width * crtc->height / width / height;
} else {
scale_x = ctx->scale_x ? ctx->scale_x : 1.0;
scale_y = ctx->scale_y ? ctx->scale_y : 1.0;
}
width *= scale_x;
height *= scale_y;
x = cursor_state->x + cursor_state->hot_x - cursor_state->hot_x * scale_x;
y = cursor_state->y + cursor_state->hot_y - cursor_state->hot_y * scale_y;
area_w = crtc->width - width;
area_h = crtc->height - height;
off_x = off_y = 0;
if (x < 0)
off_x = x;
if (y < 0)
off_y = y;
if (x > area_w)
off_x = x - area_w;
if (y > area_h)
off_y = y - area_h;
cursor_state->scaled_x = x;
cursor_state->scaled_y = y;
cursor_state->off_x = off_x;
cursor_state->off_y = off_y;
cursor_state->scaled_w = width;
cursor_state->scaled_h = height;
return 0;
}
#define drm_crtc_disable_cursor(ctx, crtc) \
drm_crtc_update_cursor(ctx, crtc, NULL)
static int drm_crtc_update_cursor(drm_ctx *ctx, drm_crtc *crtc,
drm_cursor_state *cursor_state)
{
drm_plane *plane = crtc->plane;
uint32_t old_fb = crtc->cursor_curr.fb;
uint32_t fb;
int x, y, w, h, ret;
/* Disable */
if (!cursor_state) {
if (old_fb) {
DRM_DEBUG("CRTC[%d]: disabling cursor\n", crtc->crtc_id);
drm_set_plane(ctx, crtc, plane, 0, 0, 0, 0, 0);
drmModeRmFB(ctx->fd, old_fb);
}
memset(&crtc->cursor_curr, 0, sizeof(drm_cursor_state));
return 0;
}
/* Unchanged */
if (crtc->cursor_curr.fb == cursor_state->fb &&
crtc->cursor_curr.scaled_x == cursor_state->scaled_x &&
crtc->cursor_curr.scaled_y == cursor_state->scaled_y &&
crtc->cursor_curr.off_x == cursor_state->off_x &&
crtc->cursor_curr.off_y == cursor_state->off_y) {
crtc->cursor_curr = *cursor_state;
return 0;
}
fb = cursor_state->fb;
x = cursor_state->scaled_x - cursor_state->off_x;
y = cursor_state->scaled_y - cursor_state->off_y;
w = cursor_state->scaled_w;
h = cursor_state->scaled_h;
DRM_DEBUG("CRTC[%d]: setting fb: %d (%dx%d) on plane: %d at (%d,%d)\n",
crtc->crtc_id, fb, w, h, plane->plane_id, x, y);
ret = drm_set_plane(ctx, crtc, plane, fb, x, y, w, h);
if (ret)
DRM_ERROR("CRTC[%d]: failed to set plane (%d)\n", crtc->crtc_id, errno);
if (old_fb && old_fb != fb) {
DRM_DEBUG("CRTC[%d]: remove FB: %d\n", crtc->crtc_id, old_fb);
drmModeRmFB(ctx->fd, old_fb);
}
crtc->cursor_curr = *cursor_state;
return ret;
}
static int drm_crtc_create_fb(drm_ctx *ctx, drm_crtc *crtc,
drm_cursor_state *cursor_state)
{
uint32_t handle = cursor_state->handle;
int width = cursor_state->width;
int height = cursor_state->height;
int scaled_w = cursor_state->scaled_w;
int scaled_h = cursor_state->scaled_h;
int off_x = cursor_state->off_x;
int off_y = cursor_state->off_y;
DRM_DEBUG("CRTC[%d]: convert FB from %d (%dx%d) to (%dx%d) offset: (%d,%d)\n",
crtc->crtc_id, handle, width, height,
scaled_w, scaled_h, off_x, off_y);
if (!crtc->egl_ctx) {
uint64_t modifier;
int format;
if (crtc->use_afbc_modifier) {
/* Mali only support AFBC with BGR formats now */
format = GBM_FORMAT_ABGR8888;
modifier = DRM_AFBC_MODIFIER;
} else {
format = GBM_FORMAT_ARGB8888;
modifier = 0;
}
crtc->egl_ctx = egl_init_ctx(ctx->fd, ctx->num_surfaces, format, modifier);
if (!crtc->egl_ctx) {
DRM_ERROR("CRTC[%d]: failed to init egl ctx\n", crtc->crtc_id);
return -1;
}
}
cursor_state->fb =
egl_convert_fb(ctx->fd, crtc->egl_ctx, handle, width, height,
scaled_w, scaled_h, off_x, off_y);
if (!cursor_state->fb) {
DRM_ERROR("CRTC[%d]: failed to create FB\n", crtc->crtc_id);
return -1;
}
DRM_DEBUG("CRTC[%d]: created FB: %d\n", crtc->crtc_id, cursor_state->fb);
return 0;
}
static void *drm_crtc_thread_fn(void *data)
{
drm_ctx *ctx = drm_get_ctx(-1);
drm_crtc *crtc = data;
drm_plane *plane = crtc->plane;
drm_cursor_state cursor_state;
uint64_t duration;
char name[256];
DRM_DEBUG("CRTC[%d]: thread started\n", crtc->crtc_id);
/**
* The new DRM driver doesn't allow setting atomic cap for Xorg.
* Let's use a custom thread name to workaround that.
*/
snprintf(name, sizeof(name), "drm-cursor[%d]", crtc->crtc_id);
pthread_setname_np(crtc->thread, name);
if (!plane->cursor_plane) {
drmSetClientCap(ctx->fd, DRM_CLIENT_CAP_ATOMIC, 1);
/* Reflush props with atomic cap enabled */
drmModeFreeObjectProperties(plane->props);
plane->props = drmModeObjectGetProperties(ctx->fd, plane->plane_id,
DRM_MODE_OBJECT_PLANE);
if (!plane->props)
goto error;
/* Set maximum ZPOS */
drm_plane_set_prop_max(ctx, plane, PLANE_PROP_zpos);
drm_plane_set_prop_max(ctx, plane, PLANE_PROP_ZPOS);
/* Set async commit for Rockchip BSP kernel */
crtc->async_commit =
!drm_plane_set_prop_max(ctx, plane, PLANE_PROP_ASYNC_COMMIT);
if (crtc->async_commit)
DRM_INFO("CRTC[%d]: using async commit\n", crtc->crtc_id);
}
crtc->last_update_time = drm_curr_time();
while (1) {
/* Wait for new cursor state */
pthread_mutex_lock(&crtc->mutex);
while (crtc->state != PENDING)
pthread_cond_wait(&crtc->cond, &crtc->mutex);
cursor_state = crtc->cursor_next;
crtc->cursor_next.request = 0;
crtc->state = IDLE;
cursor_state.request |= crtc->cursor_curr.request; /* For retry */
pthread_mutex_unlock(&crtc->mutex);
/* For edge moving */
if (drm_crtc_update_offsets(ctx, crtc, &cursor_state) < 0) {
DRM_DEBUG("CRTC[%d]: unavailable!\n", crtc->crtc_id);
drm_crtc_disable_cursor(ctx, crtc);
goto retry;
}
if (cursor_state.request & REQ_SET_CURSOR) {
cursor_state.request = 0;
/* Handle set-cursor */
DRM_DEBUG("CRTC[%d]: set new cursor %d (%dx%d)\n",