-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathatomictest.c
More file actions
1636 lines (1377 loc) · 47.6 KB
/
atomictest.c
File metadata and controls
1636 lines (1377 loc) · 47.6 KB
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 2017 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
/*
* This file performs some sanity checks on the DRM atomic API. To run a test, please run the
* following command:
*
* atomictest <testname>
*
* To get a list of possible tests, run:
*
* atomictest
*/
#include <getopt.h>
#include <pthread.h>
#include <sync/sync.h>
#include "bs_drm.h"
#define CHECK(cond) \
do { \
if (!(cond)) { \
bs_debug_error("check %s failed", #cond); \
return -1; \
} \
} while (0)
#define CHECK_RESULT(ret) \
do { \
if ((ret) < 0) { \
bs_debug_error("failed with error: %d", ret); \
return -1; \
} \
} while (0)
#define CURSOR_SIZE 64
// TODO(dcastagna): Remove these declarations once they're exported in a libsync header.
int sw_sync_timeline_create(void);
int sw_sync_timeline_inc(int fd, unsigned count);
int sw_sync_fence_create(int fd, const char *name, unsigned value);
// TODO(gsingh) This is defined in upstream libdrm -- remove when CROS libdrm is updated.
#ifndef DRM_ROTATE_0
#define DRM_ROTATE_0 (1UL << 0)
#endif
#ifndef DRM_REFLECT_Y
#define DRM_REFLECT_Y (1UL << 5)
#endif
#define TEST_COMMIT_FAIL 1
#define GAMMA_MAX_VALUE ((1 << 16) - 1)
static const uint32_t yuv_formats[] = {
DRM_FORMAT_NV12,
DRM_FORMAT_YVU420,
};
/*
* The blob for the CTM propery is a drm_color_ctm.
* drm_color_ctm contains a 3x3 u64 matrix. Every element is represented as
* sign and U31.32. The sign is the MSB.
*/
// clang-format off
static int64_t identity_ctm[9] = {
0x100000000, 0x0, 0x0,
0x0, 0x100000000, 0x0,
0x0, 0x0, 0x100000000
};
static int64_t red_shift_ctm[9] = {
0x140000000, 0x0, 0x0,
0x0, 0xC0000000, 0x0,
0x0, 0x0, 0xC0000000
};
// clang-format on
static bool automatic = false;
static struct gbm_device *gbm = NULL;
static void page_flip_handler(int fd, unsigned int sequence, unsigned int tv_sec,
unsigned int tv_usec, void *user_data)
{
// Nothing to do.
}
struct atomictest_property {
uint32_t pid;
uint64_t value;
};
struct atomictest_plane {
drmModePlane drm_plane;
struct gbm_bo *bo;
uint32_t format_idx;
/* Properties. */
struct atomictest_property crtc_id;
struct atomictest_property crtc_x;
struct atomictest_property crtc_y;
struct atomictest_property crtc_w;
struct atomictest_property crtc_h;
struct atomictest_property fb_id;
struct atomictest_property src_x;
struct atomictest_property src_y;
struct atomictest_property src_w;
struct atomictest_property src_h;
struct atomictest_property type;
struct atomictest_property in_fence_fd;
struct atomictest_property rotation;
struct atomictest_property ctm;
};
struct atomictest_connector {
uint32_t connector_id;
struct atomictest_property crtc_id;
struct atomictest_property edid;
struct atomictest_property dpms;
};
struct atomictest_crtc {
uint32_t crtc_id;
uint32_t width;
uint32_t height;
uint32_t *primary_idx;
uint32_t *cursor_idx;
uint32_t *overlay_idx;
uint32_t num_primary;
uint32_t num_cursor;
uint32_t num_overlay;
struct atomictest_plane *planes;
struct atomictest_property mode_id;
struct atomictest_property active;
struct atomictest_property out_fence_ptr;
struct atomictest_property ctm;
struct atomictest_property gamma_lut;
struct atomictest_property gamma_lut_size;
};
struct atomictest_mode {
uint32_t height;
uint32_t width;
uint32_t id;
};
struct atomictest_context {
int fd;
uint32_t num_crtcs;
uint32_t num_connectors;
uint32_t num_modes;
struct atomictest_connector *connectors;
struct atomictest_crtc *crtcs;
struct atomictest_mode *modes;
drmModeAtomicReqPtr pset;
drmEventContext drm_event_ctx;
struct bs_mapper *mapper;
};
typedef int (*test_function)(struct atomictest_context *ctx, struct atomictest_crtc *crtc);
struct atomictest_testcase {
const char *name;
test_function test_func;
};
// clang-format off
enum draw_format_type {
DRAW_NONE = 0,
DRAW_STRIPE = 1,
DRAW_TRANSPARENT_HOLE = 2,
DRAW_ELLIPSE = 3,
DRAW_CURSOR = 4,
DRAW_LINES = 5,
};
// clang-format on
static int drmModeCreatePropertyBlob64(int fd, const void *data, size_t length, uint64_t *id)
{
uint32_t ctm_blob_id = 0;
int ret = drmModeCreatePropertyBlob(fd, data, length, &ctm_blob_id);
*id = ctm_blob_id;
return ret;
}
static int drmModeDestroyPropertyBlob64(int fd, uint64_t id)
{
CHECK(id < (1ull << 32));
return drmModeDestroyPropertyBlob(fd, (uint32_t)id);
}
static int32_t get_format_idx(struct atomictest_plane *plane, uint32_t format)
{
for (int32_t i = 0; i < plane->drm_plane.count_formats; i++)
if (plane->drm_plane.formats[i] == format)
return i;
return -1;
}
static void copy_drm_plane(drmModePlane *dest, drmModePlane *src)
{
memcpy(dest, src, sizeof(drmModePlane));
dest->formats = calloc(src->count_formats, sizeof(uint32_t));
memcpy(dest->formats, src->formats, src->count_formats * sizeof(uint32_t));
}
static struct atomictest_plane *get_plane(struct atomictest_crtc *crtc, uint32_t idx, uint64_t type)
{
uint32_t index;
switch (type) {
case DRM_PLANE_TYPE_OVERLAY:
index = crtc->overlay_idx[idx];
break;
case DRM_PLANE_TYPE_PRIMARY:
index = crtc->primary_idx[idx];
break;
case DRM_PLANE_TYPE_CURSOR:
index = crtc->cursor_idx[idx];
break;
default:
bs_debug_error("invalid plane type returned");
return NULL;
}
return &crtc->planes[index];
}
static int draw_to_plane(struct bs_mapper *mapper, struct atomictest_plane *plane,
enum draw_format_type pattern)
{
struct gbm_bo *bo = plane->bo;
uint32_t format = gbm_bo_get_format(bo);
const struct bs_draw_format *draw_format = bs_get_draw_format(format);
if (draw_format && pattern) {
switch (pattern) {
case DRAW_STRIPE:
CHECK(bs_draw_stripe(mapper, bo, draw_format));
break;
case DRAW_TRANSPARENT_HOLE:
CHECK(bs_draw_transparent_hole(mapper, bo, draw_format));
break;
case DRAW_ELLIPSE:
CHECK(bs_draw_ellipse(mapper, bo, draw_format, 0));
break;
case DRAW_CURSOR:
CHECK(bs_draw_cursor(mapper, bo, draw_format));
break;
case DRAW_LINES:
CHECK(bs_draw_lines(mapper, bo, draw_format));
break;
default:
bs_debug_error("invalid draw type");
return -1;
}
} else {
// DRM_FORMAT_RGB565 --> red, DRM_FORMAT_BGR565 --> blue,
// everything else --> something
void *map_data;
uint16_t value = 0xF800;
uint32_t stride;
void *addr = bs_mapper_map(mapper, bo, 0, &map_data, &stride);
uint32_t num_shorts = stride * gbm_bo_get_height(bo) / sizeof(uint16_t);
uint16_t *pixel = (uint16_t *)addr;
CHECK(addr);
for (uint32_t i = 0; i < num_shorts; i++)
pixel[i] = value;
bs_mapper_unmap(mapper, bo, map_data);
}
return 0;
}
static int get_prop(int fd, drmModeObjectPropertiesPtr props, const char *name,
struct atomictest_property *bs_prop)
{
/* Property ID should always be > 0. */
bs_prop->pid = 0;
drmModePropertyPtr prop;
for (uint32_t i = 0; i < props->count_props; i++) {
if (bs_prop->pid)
break;
prop = drmModeGetProperty(fd, props->props[i]);
if (prop) {
if (!strcmp(prop->name, name)) {
bs_prop->pid = prop->prop_id;
bs_prop->value = props->prop_values[i];
}
drmModeFreeProperty(prop);
}
}
return (bs_prop->pid == 0) ? -1 : 0;
}
static int get_connector_props(int fd, struct atomictest_connector *connector,
drmModeObjectPropertiesPtr props)
{
CHECK_RESULT(get_prop(fd, props, "CRTC_ID", &connector->crtc_id));
CHECK_RESULT(get_prop(fd, props, "EDID", &connector->edid));
CHECK_RESULT(get_prop(fd, props, "DPMS", &connector->dpms));
return 0;
}
static int get_crtc_props(int fd, struct atomictest_crtc *crtc, drmModeObjectPropertiesPtr props)
{
CHECK_RESULT(get_prop(fd, props, "MODE_ID", &crtc->mode_id));
CHECK_RESULT(get_prop(fd, props, "ACTIVE", &crtc->active));
CHECK_RESULT(get_prop(fd, props, "OUT_FENCE_PTR", &crtc->out_fence_ptr));
/*
* The atomic API makes no guarantee a property is present in object. This test
* requires the above common properties since a plane is undefined without them.
* Other properties (i.e: ctm) are optional.
*/
get_prop(fd, props, "CTM", &crtc->ctm);
get_prop(fd, props, "GAMMA_LUT", &crtc->gamma_lut);
get_prop(fd, props, "GAMMA_LUT_SIZE", &crtc->gamma_lut_size);
return 0;
}
static int get_plane_props(int fd, struct atomictest_plane *plane, drmModeObjectPropertiesPtr props)
{
CHECK_RESULT(get_prop(fd, props, "CRTC_ID", &plane->crtc_id));
CHECK_RESULT(get_prop(fd, props, "FB_ID", &plane->fb_id));
CHECK_RESULT(get_prop(fd, props, "CRTC_X", &plane->crtc_x));
CHECK_RESULT(get_prop(fd, props, "CRTC_Y", &plane->crtc_y));
CHECK_RESULT(get_prop(fd, props, "CRTC_W", &plane->crtc_w));
CHECK_RESULT(get_prop(fd, props, "CRTC_H", &plane->crtc_h));
CHECK_RESULT(get_prop(fd, props, "SRC_X", &plane->src_x));
CHECK_RESULT(get_prop(fd, props, "SRC_Y", &plane->src_y));
CHECK_RESULT(get_prop(fd, props, "SRC_W", &plane->src_w));
CHECK_RESULT(get_prop(fd, props, "SRC_H", &plane->src_h));
CHECK_RESULT(get_prop(fd, props, "type", &plane->type));
CHECK_RESULT(get_prop(fd, props, "IN_FENCE_FD", &plane->in_fence_fd));
/*
* The atomic API makes no guarantee a property is present in object. This test
* requires the above common properties since a plane is undefined without them.
* Other properties (i.e: rotation and ctm) are optional.
*/
get_prop(fd, props, "rotation", &plane->rotation);
get_prop(fd, props, "PLANE_CTM", &plane->ctm);
return 0;
}
int set_connector_props(struct atomictest_connector *conn, drmModeAtomicReqPtr pset)
{
uint32_t id = conn->connector_id;
CHECK_RESULT(drmModeAtomicAddProperty(pset, id, conn->crtc_id.pid, conn->crtc_id.value));
return 0;
}
int set_crtc_props(struct atomictest_crtc *crtc, drmModeAtomicReqPtr pset)
{
uint32_t id = crtc->crtc_id;
CHECK_RESULT(drmModeAtomicAddProperty(pset, id, crtc->mode_id.pid, crtc->mode_id.value));
CHECK_RESULT(drmModeAtomicAddProperty(pset, id, crtc->active.pid, crtc->active.value));
if (crtc->out_fence_ptr.value)
CHECK_RESULT(drmModeAtomicAddProperty(pset, id, crtc->out_fence_ptr.pid,
crtc->out_fence_ptr.value));
if (crtc->ctm.pid)
CHECK_RESULT(drmModeAtomicAddProperty(pset, id, crtc->ctm.pid, crtc->ctm.value));
if (crtc->gamma_lut.pid)
CHECK_RESULT(
drmModeAtomicAddProperty(pset, id, crtc->gamma_lut.pid, crtc->gamma_lut.value));
return 0;
}
int set_plane_props(struct atomictest_plane *plane, drmModeAtomicReqPtr pset)
{
uint32_t id = plane->drm_plane.plane_id;
CHECK_RESULT(drmModeAtomicAddProperty(pset, id, plane->crtc_id.pid, plane->crtc_id.value));
CHECK_RESULT(drmModeAtomicAddProperty(pset, id, plane->fb_id.pid, plane->fb_id.value));
CHECK_RESULT(drmModeAtomicAddProperty(pset, id, plane->crtc_x.pid, plane->crtc_x.value));
CHECK_RESULT(drmModeAtomicAddProperty(pset, id, plane->crtc_y.pid, plane->crtc_y.value));
CHECK_RESULT(drmModeAtomicAddProperty(pset, id, plane->crtc_w.pid, plane->crtc_w.value));
CHECK_RESULT(drmModeAtomicAddProperty(pset, id, plane->crtc_h.pid, plane->crtc_h.value));
CHECK_RESULT(drmModeAtomicAddProperty(pset, id, plane->src_x.pid, plane->src_x.value));
CHECK_RESULT(drmModeAtomicAddProperty(pset, id, plane->src_y.pid, plane->src_y.value));
CHECK_RESULT(drmModeAtomicAddProperty(pset, id, plane->src_w.pid, plane->src_w.value));
CHECK_RESULT(drmModeAtomicAddProperty(pset, id, plane->src_h.pid, plane->src_h.value));
CHECK_RESULT(
drmModeAtomicAddProperty(pset, id, plane->in_fence_fd.pid, plane->in_fence_fd.value));
if (plane->rotation.pid)
CHECK_RESULT(
drmModeAtomicAddProperty(pset, id, plane->rotation.pid, plane->rotation.value));
if (plane->ctm.pid)
CHECK_RESULT(drmModeAtomicAddProperty(pset, id, plane->ctm.pid, plane->ctm.value));
return 0;
}
static int remove_plane_fb(struct atomictest_context *ctx, struct atomictest_plane *plane)
{
if (plane->bo && plane->fb_id.value) {
CHECK_RESULT(drmModeRmFB(ctx->fd, plane->fb_id.value));
gbm_bo_destroy(plane->bo);
plane->bo = NULL;
plane->fb_id.value = 0;
}
return 0;
}
static int add_plane_fb(struct atomictest_context *ctx, struct atomictest_plane *plane)
{
if (plane->format_idx < plane->drm_plane.count_formats) {
CHECK_RESULT(remove_plane_fb(ctx, plane));
uint32_t flags = (plane->type.value == DRM_PLANE_TYPE_CURSOR) ? GBM_BO_USE_CURSOR
: GBM_BO_USE_SCANOUT;
flags |= GBM_BO_USE_SW_WRITE_RARELY;
/* TODO(gsingh): add create with modifiers option. */
plane->bo = gbm_bo_create(gbm, plane->crtc_w.value, plane->crtc_h.value,
plane->drm_plane.formats[plane->format_idx], flags);
CHECK(plane->bo);
plane->fb_id.value = bs_drm_fb_create_gbm(plane->bo);
CHECK(plane->fb_id.value);
CHECK_RESULT(set_plane_props(plane, ctx->pset));
}
return 0;
}
static int init_plane(struct atomictest_context *ctx, struct atomictest_plane *plane,
uint32_t format, uint32_t x, uint32_t y, uint32_t w, uint32_t h,
uint32_t crtc_id)
{
int32_t idx = get_format_idx(plane, format);
if (idx < 0)
return -EINVAL;
plane->format_idx = idx;
plane->crtc_x.value = x;
plane->crtc_y.value = y;
plane->crtc_w.value = w;
plane->crtc_h.value = h;
plane->src_w.value = plane->crtc_w.value << 16;
plane->src_h.value = plane->crtc_h.value << 16;
plane->crtc_id.value = crtc_id;
CHECK_RESULT(add_plane_fb(ctx, plane));
return 0;
}
static int init_plane_any_format(struct atomictest_context *ctx, struct atomictest_plane *plane,
uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint32_t crtc_id,
bool yuv)
{
if (yuv) {
uint32_t i;
for (i = 0; i < BS_ARRAY_LEN(yuv_formats); i++)
if (!init_plane(ctx, plane, yuv_formats[i], x, y, w, h, crtc_id))
return 0;
} else {
// XRGB888 works well with our draw code, so try that first.
if (!init_plane(ctx, plane, DRM_FORMAT_XRGB8888, x, y, w, h, crtc_id))
return 0;
for (uint32_t format_idx = 0; format_idx < plane->drm_plane.count_formats;
format_idx++) {
if (!gbm_device_is_format_supported(
gbm, plane->drm_plane.formats[format_idx], GBM_BO_USE_SCANOUT))
continue;
if (!init_plane(ctx, plane, plane->drm_plane.formats[format_idx], x, y, w,
h, crtc_id))
return 0;
}
}
return -EINVAL;
}
static int disable_plane(struct atomictest_context *ctx, struct atomictest_plane *plane)
{
plane->format_idx = 0;
plane->crtc_x.value = 0;
plane->crtc_y.value = 0;
plane->crtc_w.value = 0;
plane->crtc_h.value = 0;
plane->src_w.value = 0;
plane->src_h.value = 0;
plane->crtc_id.value = 0;
if (plane->rotation.pid)
plane->rotation.value = DRM_ROTATE_0;
if (plane->ctm.pid)
plane->ctm.value = 0;
CHECK_RESULT(remove_plane_fb(ctx, plane));
CHECK_RESULT(set_plane_props(plane, ctx->pset));
return 0;
}
static int move_plane(struct atomictest_context *ctx, struct atomictest_crtc *crtc,
struct atomictest_plane *plane, uint32_t dx, uint32_t dy)
{
if (plane->crtc_x.value < (crtc->width - plane->crtc_w.value) &&
plane->crtc_y.value < (crtc->height - plane->crtc_h.value)) {
plane->crtc_x.value += dx;
plane->crtc_y.value += dy;
CHECK_RESULT(set_plane_props(plane, ctx->pset));
return 0;
}
return -1;
}
static int scale_plane(struct atomictest_context *ctx, struct atomictest_crtc *crtc,
struct atomictest_plane *plane, float dw, float dh)
{
int32_t plane_w = (int32_t)plane->crtc_w.value + dw * plane->crtc_w.value;
int32_t plane_h = (int32_t)plane->crtc_h.value + dh * plane->crtc_h.value;
if (plane_w > 0 && plane_h > 0 && (plane->crtc_x.value + plane_w < crtc->width) &&
(plane->crtc_h.value + plane_h < crtc->height)) {
plane->crtc_w.value = BS_ALIGN((uint32_t)plane_w, 2);
plane->crtc_h.value = BS_ALIGN((uint32_t)plane_h, 2);
CHECK_RESULT(set_plane_props(plane, ctx->pset));
return 0;
}
return -1;
}
static void log(struct atomictest_context *ctx)
{
printf("Committing the following configuration: \n");
for (uint32_t i = 0; i < ctx->num_crtcs; i++) {
struct atomictest_plane *plane;
struct atomictest_crtc *crtc = &ctx->crtcs[i];
uint32_t num_planes = crtc->num_primary + crtc->num_cursor + crtc->num_overlay;
if (!crtc->active.value)
continue;
printf("----- [CRTC: %u] -----\n", crtc->crtc_id);
for (uint32_t j = 0; j < num_planes; j++) {
plane = &crtc->planes[j];
if (plane->crtc_id.value == crtc->crtc_id && plane->fb_id.value) {
uint32_t format = gbm_bo_get_format(plane->bo);
char *fourcc = (char *)&format;
printf("\t{Plane ID: %u, ", plane->drm_plane.plane_id);
printf("Plane format: %c%c%c%c, ", fourcc[0], fourcc[1], fourcc[2],
fourcc[3]);
printf("Plane type: ");
switch (plane->type.value) {
case DRM_PLANE_TYPE_OVERLAY:
printf("overlay, ");
break;
case DRM_PLANE_TYPE_PRIMARY:
printf("primary, ");
break;
case DRM_PLANE_TYPE_CURSOR:
printf("cursor, ");
break;
}
printf("CRTC_X: %u, CRTC_Y: %u, CRTC_W: %u, CRTC_H: %u}\n",
plane->crtc_x.value, plane->crtc_y.value,
plane->crtc_w.value, plane->crtc_h.value);
}
}
}
}
static int test_commit(struct atomictest_context *ctx)
{
return drmModeAtomicCommit(ctx->fd, ctx->pset,
DRM_MODE_ATOMIC_ALLOW_MODESET | DRM_MODE_ATOMIC_TEST_ONLY, NULL);
}
static int commit(struct atomictest_context *ctx)
{
int ret;
fd_set fds;
FD_ZERO(&fds);
FD_SET(ctx->fd, &fds);
log(ctx);
ret = drmModeAtomicCommit(ctx->fd, ctx->pset,
DRM_MODE_PAGE_FLIP_EVENT | DRM_MODE_ATOMIC_ALLOW_MODESET, NULL);
CHECK_RESULT(ret);
do {
ret = select(ctx->fd + 1, &fds, NULL, NULL, NULL);
} while (ret == -1 && errno == EINTR);
CHECK_RESULT(ret);
if (FD_ISSET(ctx->fd, &fds))
drmHandleEvent(ctx->fd, &ctx->drm_event_ctx);
return 0;
}
static int test_and_commit(struct atomictest_context *ctx, uint32_t sleep_micro_secs)
{
sleep_micro_secs = automatic ? 0 : sleep_micro_secs;
if (!test_commit(ctx)) {
CHECK_RESULT(commit(ctx));
usleep(sleep_micro_secs);
} else {
return TEST_COMMIT_FAIL;
}
return 0;
}
static int pageflip_formats(struct atomictest_context *ctx, struct atomictest_crtc *crtc,
struct atomictest_plane *plane)
{
int ret = 0;
uint32_t flags;
for (uint32_t i = 0; i < plane->drm_plane.count_formats; i++) {
flags = (plane->type.value == DRM_PLANE_TYPE_CURSOR) ? GBM_BO_USE_CURSOR
: GBM_BO_USE_SCANOUT;
if (!gbm_device_is_format_supported(gbm, plane->drm_plane.formats[i], flags))
continue;
CHECK_RESULT(init_plane(ctx, plane, plane->drm_plane.formats[i], 0, 0, crtc->width,
crtc->height, crtc->crtc_id));
CHECK_RESULT(draw_to_plane(ctx->mapper, plane, DRAW_ELLIPSE));
ret |= test_and_commit(ctx, 1e6);
// disable, but don't commit, since we can't have an active CRTC without any planes.
CHECK_RESULT(disable_plane(ctx, plane));
}
return ret;
}
static uint32_t get_connection(struct atomictest_crtc *crtc, uint32_t crtc_index)
{
uint32_t connector_id = 0;
uint32_t crtc_mask = 1u << crtc_index;
struct bs_drm_pipe pipe = { 0 };
struct bs_drm_pipe_plumber *plumber = bs_drm_pipe_plumber_new();
bs_drm_pipe_plumber_crtc_mask(plumber, crtc_mask);
if (bs_drm_pipe_plumber_make(plumber, &pipe))
connector_id = pipe.connector_id;
bs_drm_pipe_plumber_destroy(&plumber);
return connector_id;
}
static int enable_crtc(struct atomictest_context *ctx, struct atomictest_crtc *crtc)
{
drmModeAtomicSetCursor(ctx->pset, 0);
for (uint32_t i = 0; i < ctx->num_connectors; i++) {
ctx->connectors[i].crtc_id.value = 0;
set_connector_props(&ctx->connectors[i], ctx->pset);
}
for (uint32_t i = 0; i < ctx->num_crtcs; i++) {
if (&ctx->crtcs[i] == crtc) {
uint32_t connector_id = get_connection(crtc, i);
CHECK(connector_id);
for (uint32_t j = 0; j < ctx->num_connectors; j++) {
if (connector_id == ctx->connectors[j].connector_id) {
ctx->connectors[j].crtc_id.value = crtc->crtc_id;
set_connector_props(&ctx->connectors[j], ctx->pset);
break;
}
}
break;
}
}
int ret = -EINVAL;
int cursor = drmModeAtomicGetCursor(ctx->pset);
for (uint32_t i = 0; i < ctx->num_modes; i++) {
struct atomictest_mode *mode = &ctx->modes[i];
drmModeAtomicSetCursor(ctx->pset, cursor);
crtc->mode_id.value = mode->id;
crtc->active.value = 1;
crtc->width = mode->width;
crtc->height = mode->height;
set_crtc_props(crtc, ctx->pset);
ret = drmModeAtomicCommit(ctx->fd, ctx->pset,
DRM_MODE_ATOMIC_TEST_ONLY | DRM_MODE_ATOMIC_ALLOW_MODESET,
NULL);
if (!ret)
return 0;
}
bs_debug_error("[CRTC:%d]: failed to find mode", crtc->crtc_id);
return ret;
}
static int disable_crtc(struct atomictest_context *ctx, struct atomictest_crtc *crtc)
{
for (uint32_t i = 0; i < ctx->num_connectors; i++) {
ctx->connectors[i].crtc_id.value = 0;
set_connector_props(&ctx->connectors[i], ctx->pset);
}
crtc->mode_id.value = 0;
crtc->active.value = 0;
if (crtc->ctm.pid)
crtc->ctm.value = 0;
if (crtc->gamma_lut.pid)
crtc->gamma_lut.value = 0;
set_crtc_props(crtc, ctx->pset);
int ret = drmModeAtomicCommit(ctx->fd, ctx->pset, DRM_MODE_ATOMIC_ALLOW_MODESET, NULL);
CHECK_RESULT(ret);
return ret;
}
static struct atomictest_context *new_context(uint32_t num_connectors, uint32_t num_crtcs,
uint32_t num_planes)
{
struct atomictest_context *ctx = calloc(1, sizeof(*ctx));
ctx->mapper = bs_mapper_gem_new();
if (ctx->mapper == NULL) {
bs_debug_error("failed to create mapper object");
free(ctx);
return NULL;
}
ctx->connectors = calloc(num_connectors, sizeof(*ctx->connectors));
ctx->crtcs = calloc(num_crtcs, sizeof(*ctx->crtcs));
for (uint32_t i = 0; i < num_crtcs; i++) {
ctx->crtcs[i].planes = calloc(num_planes, sizeof(*ctx->crtcs[i].planes));
ctx->crtcs[i].overlay_idx = calloc(num_planes, sizeof(uint32_t));
ctx->crtcs[i].primary_idx = calloc(num_planes, sizeof(uint32_t));
ctx->crtcs[i].cursor_idx = calloc(num_planes, sizeof(uint32_t));
}
ctx->num_connectors = num_connectors;
ctx->num_crtcs = num_crtcs;
ctx->num_modes = 0;
ctx->modes = NULL;
ctx->pset = drmModeAtomicAlloc();
ctx->drm_event_ctx.version = DRM_EVENT_CONTEXT_VERSION;
ctx->drm_event_ctx.page_flip_handler = page_flip_handler;
return ctx;
}
static void free_context(struct atomictest_context *ctx)
{
for (uint32_t i = 0; i < ctx->num_crtcs; i++) {
uint32_t num_planes = ctx->crtcs[i].num_primary + ctx->crtcs[i].num_cursor +
ctx->crtcs[i].num_overlay;
for (uint32_t j = 0; j < num_planes; j++) {
remove_plane_fb(ctx, &ctx->crtcs[i].planes[j]);
free(ctx->crtcs[i].planes[j].drm_plane.formats);
}
free(ctx->crtcs[i].planes);
free(ctx->crtcs[i].overlay_idx);
free(ctx->crtcs[i].cursor_idx);
free(ctx->crtcs[i].primary_idx);
}
drmModeAtomicFree(ctx->pset);
free(ctx->modes);
free(ctx->crtcs);
free(ctx->connectors);
bs_mapper_destroy(ctx->mapper);
free(ctx);
}
static struct atomictest_context *query_kms(int fd)
{
drmModeRes *res = drmModeGetResources(fd);
if (res == NULL) {
bs_debug_error("failed to get drm resources");
return false;
}
drmModePlaneRes *plane_res = drmModeGetPlaneResources(fd);
if (plane_res == NULL) {
bs_debug_error("failed to get plane resources");
drmModeFreeResources(res);
return NULL;
}
struct atomictest_context *ctx =
new_context(res->count_connectors, res->count_crtcs, plane_res->count_planes);
if (ctx == NULL) {
bs_debug_error("failed to allocate atomic context");
drmModeFreePlaneResources(plane_res);
drmModeFreeResources(res);
return NULL;
}
ctx->fd = fd;
drmModeObjectPropertiesPtr props = NULL;
for (uint32_t conn_index = 0; conn_index < res->count_connectors; conn_index++) {
uint32_t conn_id = res->connectors[conn_index];
ctx->connectors[conn_index].connector_id = conn_id;
props = drmModeObjectGetProperties(fd, conn_id, DRM_MODE_OBJECT_CONNECTOR);
get_connector_props(fd, &ctx->connectors[conn_index], props);
drmModeConnector *connector = drmModeGetConnector(fd, conn_id);
for (uint32_t mode_index = 0; mode_index < connector->count_modes; mode_index++) {
ctx->modes =
realloc(ctx->modes, (ctx->num_modes + 1) * sizeof(*ctx->modes));
drmModeCreatePropertyBlob(fd, &connector->modes[mode_index],
sizeof(drmModeModeInfo),
&ctx->modes[ctx->num_modes].id);
ctx->modes[ctx->num_modes].width = connector->modes[mode_index].hdisplay;
ctx->modes[ctx->num_modes].height = connector->modes[mode_index].vdisplay;
ctx->num_modes++;
}
drmModeFreeConnector(connector);
drmModeFreeObjectProperties(props);
props = NULL;
}
uint32_t crtc_index;
for (crtc_index = 0; crtc_index < res->count_crtcs; crtc_index++) {
ctx->crtcs[crtc_index].crtc_id = res->crtcs[crtc_index];
props =
drmModeObjectGetProperties(fd, res->crtcs[crtc_index], DRM_MODE_OBJECT_CRTC);
get_crtc_props(fd, &ctx->crtcs[crtc_index], props);
drmModeFreeObjectProperties(props);
props = NULL;
}
uint32_t overlay_idx, primary_idx, cursor_idx, idx;
for (uint32_t plane_index = 0; plane_index < plane_res->count_planes; plane_index++) {
drmModePlane *plane = drmModeGetPlane(fd, plane_res->planes[plane_index]);
if (plane == NULL) {
bs_debug_error("failed to get plane id %u", plane_res->planes[plane_index]);
continue;
}
uint32_t crtc_mask = 0;
drmModeObjectPropertiesPtr props = drmModeObjectGetProperties(
fd, plane_res->planes[plane_index], DRM_MODE_OBJECT_PLANE);
for (crtc_index = 0; crtc_index < res->count_crtcs; crtc_index++) {
crtc_mask = (1 << crtc_index);
if (plane->possible_crtcs & crtc_mask) {
struct atomictest_crtc *crtc = &ctx->crtcs[crtc_index];
cursor_idx = crtc->num_cursor;
primary_idx = crtc->num_primary;
overlay_idx = crtc->num_overlay;
idx = cursor_idx + primary_idx + overlay_idx;
copy_drm_plane(&crtc->planes[idx].drm_plane, plane);
get_plane_props(fd, &crtc->planes[idx], props);
switch (crtc->planes[idx].type.value) {
case DRM_PLANE_TYPE_OVERLAY:
crtc->overlay_idx[overlay_idx] = idx;
crtc->num_overlay++;
break;
case DRM_PLANE_TYPE_PRIMARY:
crtc->primary_idx[primary_idx] = idx;
crtc->num_primary++;
break;
case DRM_PLANE_TYPE_CURSOR:
crtc->cursor_idx[cursor_idx] = idx;
crtc->num_cursor++;
break;
default:
bs_debug_error("invalid plane type returned");
return NULL;
}
/*
* The DRM UAPI states that cursor and overlay framebuffers may be
* present after a CRTC disable, so zero this out so we can get a
* clean slate.
*/
crtc->planes[idx].fb_id.value = 0;
}
}
drmModeFreePlane(plane);
drmModeFreeObjectProperties(props);
props = NULL;
}
drmModeFreePlaneResources(plane_res);
drmModeFreeResources(res);
return ctx;
}
static int test_multiple_planes(struct atomictest_context *ctx, struct atomictest_crtc *crtc)
{
int ret = 0;
struct atomictest_plane *primary, *overlay, *cursor;
for (uint32_t i = 0; i < crtc->num_primary; i++) {
bool video = true;
uint32_t x, y;
for (uint32_t j = 0; j < crtc->num_overlay; j++) {
overlay = get_plane(crtc, j, DRM_PLANE_TYPE_OVERLAY);
x = crtc->width >> (j + 2);
y = crtc->height >> (j + 2);
// drmModeAddFB2 requires the height and width are even for sub-sampled YUV
// formats.
x = BS_ALIGN(x, 2);
y = BS_ALIGN(y, 2);
if (video &&
!init_plane_any_format(ctx, overlay, x, y, x, y, crtc->crtc_id, true)) {
CHECK_RESULT(draw_to_plane(ctx->mapper, overlay, DRAW_STRIPE));
video = false;
} else {
CHECK_RESULT(init_plane_any_format(ctx, overlay, x, y, x, y,
crtc->crtc_id, false));
CHECK_RESULT(draw_to_plane(ctx->mapper, overlay, DRAW_LINES));
}
}
for (uint32_t j = 0; j < crtc->num_cursor; j++) {
x = crtc->width >> (j + 2);
y = crtc->height >> (j + 2);
cursor = get_plane(crtc, j, DRM_PLANE_TYPE_CURSOR);
CHECK_RESULT(init_plane(ctx, cursor, DRM_FORMAT_ARGB8888, x, y, CURSOR_SIZE,
CURSOR_SIZE, crtc->crtc_id));
CHECK_RESULT(draw_to_plane(ctx->mapper, cursor, DRAW_CURSOR));
}
primary = get_plane(crtc, i, DRM_PLANE_TYPE_PRIMARY);
CHECK_RESULT(init_plane_any_format(ctx, primary, 0, 0, crtc->width, crtc->height,
crtc->crtc_id, false));
CHECK_RESULT(draw_to_plane(ctx->mapper, primary, DRAW_ELLIPSE));
uint32_t num_planes = crtc->num_primary + crtc->num_cursor + crtc->num_overlay;
int done = 0;
struct atomictest_plane *plane;
while (!done) {
done = 1;
for (uint32_t j = 0; j < num_planes; j++) {
plane = &crtc->planes[j];
if (plane->type.value != DRM_PLANE_TYPE_PRIMARY)
done &= move_plane(ctx, crtc, plane, 40, 40);
}
ret |= test_and_commit(ctx, 1e6 / 60);
}
ret |= test_and_commit(ctx, 1e6);
/* Disable primary plane and verify overlays show up. */
CHECK_RESULT(disable_plane(ctx, primary));
ret |= test_and_commit(ctx, 1e6);
}
return ret;
}
static int test_video_overlay(struct atomictest_context *ctx, struct atomictest_crtc *crtc)
{
int ret = 0;
struct atomictest_plane *overlay;
for (uint32_t i = 0; i < crtc->num_overlay; i++) {
overlay = get_plane(crtc, i, DRM_PLANE_TYPE_OVERLAY);
if (init_plane_any_format(ctx, overlay, 0, 0, 800, 800, crtc->crtc_id, true))
continue;
CHECK_RESULT(draw_to_plane(ctx->mapper, overlay, DRAW_STRIPE));
while (!move_plane(ctx, crtc, overlay, 40, 40))
ret |= test_and_commit(ctx, 1e6 / 60);
}
return ret;
}
static int test_orientation(struct atomictest_context *ctx, struct atomictest_crtc *crtc)
{
int ret = 0;
struct atomictest_plane *primary, *overlay;
for (uint32_t i = 0; i < crtc->num_overlay; i++) {
overlay = get_plane(crtc, i, DRM_PLANE_TYPE_OVERLAY);
if (!overlay->rotation.pid)
continue;
CHECK_RESULT(init_plane_any_format(ctx, overlay, 0, 0, crtc->width, crtc->height,
crtc->crtc_id, false));
overlay->rotation.value = DRM_ROTATE_0;
set_plane_props(overlay, ctx->pset);
CHECK_RESULT(draw_to_plane(ctx->mapper, overlay, DRAW_LINES));
ret |= test_and_commit(ctx, 1e6);