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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
|
/*
* DRM based mode setting test program
* Copyright 2008 Tungsten Graphics
* Jakob Bornecrantz <jakob@tungstengraphics.com>
* Copyright 2008 Intel Corporation
* Jesse Barnes <jesse.barnes@intel.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <sys/ioctl.h>
#include "drm.h"
#include "drm_fourcc.h"
#include "libdrm_macros.h"
#include "xf86drm.h"
#include "buffers.h"
#ifdef HAVE_CAIRO
#include <math.h>
#include <cairo.h>
#endif
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
struct bo
{
int fd;
void *ptr;
size_t size;
size_t offset;
size_t pitch;
unsigned handle;
};
/* -----------------------------------------------------------------------------
* Formats
*/
struct color_component {
unsigned int length;
unsigned int offset;
};
struct rgb_info {
struct color_component red;
struct color_component green;
struct color_component blue;
struct color_component alpha;
};
enum yuv_order {
YUV_YCbCr = 1,
YUV_YCrCb = 2,
YUV_YC = 4,
YUV_CY = 8,
};
struct yuv_info {
enum yuv_order order;
unsigned int xsub;
unsigned int ysub;
unsigned int chroma_stride;
};
struct format_info {
unsigned int format;
const char *name;
const struct rgb_info rgb;
const struct yuv_info yuv;
};
#define MAKE_RGB_INFO(rl, ro, gl, go, bl, bo, al, ao) \
.rgb = { { (rl), (ro) }, { (gl), (go) }, { (bl), (bo) }, { (al), (ao) } }
#define MAKE_YUV_INFO(order, xsub, ysub, chroma_stride) \
.yuv = { (order), (xsub), (ysub), (chroma_stride) }
static const struct format_info format_info[] = {
/* YUV packed */
{ DRM_FORMAT_UYVY, "UYVY", MAKE_YUV_INFO(YUV_YCbCr | YUV_CY, 2, 2, 2) },
{ DRM_FORMAT_VYUY, "VYUY", MAKE_YUV_INFO(YUV_YCrCb | YUV_CY, 2, 2, 2) },
{ DRM_FORMAT_YUYV, "YUYV", MAKE_YUV_INFO(YUV_YCbCr | YUV_YC, 2, 2, 2) },
{ DRM_FORMAT_YVYU, "YVYU", MAKE_YUV_INFO(YUV_YCrCb | YUV_YC, 2, 2, 2) },
/* YUV semi-planar */
{ DRM_FORMAT_NV12, "NV12", MAKE_YUV_INFO(YUV_YCbCr, 2, 2, 2) },
{ DRM_FORMAT_NV21, "NV21", MAKE_YUV_INFO(YUV_YCrCb, 2, 2, 2) },
{ DRM_FORMAT_NV16, "NV16", MAKE_YUV_INFO(YUV_YCbCr, 2, 1, 2) },
{ DRM_FORMAT_NV61, "NV61", MAKE_YUV_INFO(YUV_YCrCb, 2, 1, 2) },
/* YUV planar */
{ DRM_FORMAT_YUV420, "YU12", MAKE_YUV_INFO(YUV_YCbCr, 2, 2, 1) },
{ DRM_FORMAT_YVU420, "YV12", MAKE_YUV_INFO(YUV_YCrCb, 2, 2, 1) },
/* RGB16 */
{ DRM_FORMAT_ARGB4444, "AR12", MAKE_RGB_INFO(4, 8, 4, 4, 4, 0, 4, 12) },
{ DRM_FORMAT_XRGB4444, "XR12", MAKE_RGB_INFO(4, 8, 4, 4, 4, 0, 0, 0) },
{ DRM_FORMAT_ABGR4444, "AB12", MAKE_RGB_INFO(4, 0, 4, 4, 4, 8, 4, 12) },
{ DRM_FORMAT_XBGR4444, "XB12", MAKE_RGB_INFO(4, 0, 4, 4, 4, 8, 0, 0) },
{ DRM_FORMAT_RGBA4444, "RA12", MAKE_RGB_INFO(4, 12, 4, 8, 4, 4, 4, 0) },
{ DRM_FORMAT_RGBX4444, "RX12", MAKE_RGB_INFO(4, 12, 4, 8, 4, 4, 0, 0) },
{ DRM_FORMAT_BGRA4444, "BA12", MAKE_RGB_INFO(4, 4, 4, 8, 4, 12, 4, 0) },
{ DRM_FORMAT_BGRX4444, "BX12", MAKE_RGB_INFO(4, 4, 4, 8, 4, 12, 0, 0) },
{ DRM_FORMAT_ARGB1555, "AR15", MAKE_RGB_INFO(5, 10, 5, 5, 5, 0, 1, 15) },
{ DRM_FORMAT_XRGB1555, "XR15", MAKE_RGB_INFO(5, 10, 5, 5, 5, 0, 0, 0) },
{ DRM_FORMAT_ABGR1555, "AB15", MAKE_RGB_INFO(5, 0, 5, 5, 5, 10, 1, 15) },
{ DRM_FORMAT_XBGR1555, "XB15", MAKE_RGB_INFO(5, 0, 5, 5, 5, 10, 0, 0) },
{ DRM_FORMAT_RGBA5551, "RA15", MAKE_RGB_INFO(5, 11, 5, 6, 5, 1, 1, 0) },
{ DRM_FORMAT_RGBX5551, "RX15", MAKE_RGB_INFO(5, 11, 5, 6, 5, 1, 0, 0) },
{ DRM_FORMAT_BGRA5551, "BA15", MAKE_RGB_INFO(5, 1, 5, 6, 5, 11, 1, 0) },
{ DRM_FORMAT_BGRX5551, "BX15", MAKE_RGB_INFO(5, 1, 5, 6, 5, 11, 0, 0) },
{ DRM_FORMAT_RGB565, "RG16", MAKE_RGB_INFO(5, 11, 6, 5, 5, 0, 0, 0) },
{ DRM_FORMAT_BGR565, "BG16", MAKE_RGB_INFO(5, 0, 6, 5, 5, 11, 0, 0) },
/* RGB24 */
{ DRM_FORMAT_BGR888, "BG24", MAKE_RGB_INFO(8, 0, 8, 8, 8, 16, 0, 0) },
{ DRM_FORMAT_RGB888, "RG24", MAKE_RGB_INFO(8, 16, 8, 8, 8, 0, 0, 0) },
/* RGB32 */
{ DRM_FORMAT_ARGB8888, "AR24", MAKE_RGB_INFO(8, 16, 8, 8, 8, 0, 8, 24) },
{ DRM_FORMAT_XRGB8888, "XR24", MAKE_RGB_INFO(8, 16, 8, 8, 8, 0, 0, 0) },
{ DRM_FORMAT_ABGR8888, "AB24", MAKE_RGB_INFO(8, 0, 8, 8, 8, 16, 8, 24) },
{ DRM_FORMAT_XBGR8888, "XB24", MAKE_RGB_INFO(8, 0, 8, 8, 8, 16, 0, 0) },
{ DRM_FORMAT_RGBA8888, "RA24", MAKE_RGB_INFO(8, 24, 8, 16, 8, 8, 8, 0) },
{ DRM_FORMAT_RGBX8888, "RX24", MAKE_RGB_INFO(8, 24, 8, 16, 8, 8, 0, 0) },
{ DRM_FORMAT_BGRA8888, "BA24", MAKE_RGB_INFO(8, 8, 8, 16, 8, 24, 8, 0) },
{ DRM_FORMAT_BGRX8888, "BX24", MAKE_RGB_INFO(8, 8, 8, 16, 8, 24, 0, 0) },
{ DRM_FORMAT_ARGB2101010, "AR30", MAKE_RGB_INFO(10, 20, 10, 10, 10, 0, 2, 30) },
{ DRM_FORMAT_XRGB2101010, "XR30", MAKE_RGB_INFO(10, 20, 10, 10, 10, 0, 0, 0) },
{ DRM_FORMAT_ABGR2101010, "AB30", MAKE_RGB_INFO(10, 0, 10, 10, 10, 20, 2, 30) },
{ DRM_FORMAT_XBGR2101010, "XB30", MAKE_RGB_INFO(10, 0, 10, 10, 10, 20, 0, 0) },
{ DRM_FORMAT_RGBA1010102, "RA30", MAKE_RGB_INFO(10, 22, 10, 12, 10, 2, 2, 0) },
{ DRM_FORMAT_RGBX1010102, "RX30", MAKE_RGB_INFO(10, 22, 10, 12, 10, 2, 0, 0) },
{ DRM_FORMAT_BGRA1010102, "BA30", MAKE_RGB_INFO(10, 2, 10, 12, 10, 22, 2, 0) },
{ DRM_FORMAT_BGRX1010102, "BX30", MAKE_RGB_INFO(10, 2, 10, 12, 10, 22, 0, 0) },
};
unsigned int format_fourcc(const char *name)
{
unsigned int i;
for (i = 0; i < ARRAY_SIZE(format_info); i++) {
if (!strcmp(format_info[i].name, name))
return format_info[i].format;
}
return 0;
}
/* -----------------------------------------------------------------------------
* Test patterns
*/
struct color_rgb24 {
unsigned int value:24;
} __attribute__((__packed__));
struct color_yuv {
unsigned char y;
unsigned char u;
unsigned char v;
};
#define MAKE_YUV_601_Y(r, g, b) \
((( 66 * (r) + 129 * (g) + 25 * (b) + 128) >> 8) + 16)
#define MAKE_YUV_601_U(r, g, b) \
(((-38 * (r) - 74 * (g) + 112 * (b) + 128) >> 8) + 128)
#define MAKE_YUV_601_V(r, g, b) \
(((112 * (r) - 94 * (g) - 18 * (b) + 128) >> 8) + 128)
#define MAKE_YUV_601(r, g, b) \
{ .y = MAKE_YUV_601_Y(r, g, b), \
.u = MAKE_YUV_601_U(r, g, b), \
.v = MAKE_YUV_601_V(r, g, b) }
#define MAKE_RGBA(rgb, r, g, b, a) \
((((r) >> (8 - (rgb)->red.length)) << (rgb)->red.offset) | \
(((g) >> (8 - (rgb)->green.length)) << (rgb)->green.offset) | \
(((b) >> (8 - (rgb)->blue.length)) << (rgb)->blue.offset) | \
(((a) >> (8 - (rgb)->alpha.length)) << (rgb)->alpha.offset))
#define MAKE_RGB24(rgb, r, g, b) \
{ .value = MAKE_RGBA(rgb, r, g, b, 0) }
static void
fill_smpte_yuv_planar(const struct yuv_info *yuv,
unsigned char *y_mem, unsigned char *u_mem,
unsigned char *v_mem, unsigned int width,
unsigned int height, unsigned int stride)
{
const struct color_yuv colors_top[] = {
MAKE_YUV_601(191, 192, 192), /* grey */
MAKE_YUV_601(192, 192, 0), /* yellow */
MAKE_YUV_601(0, 192, 192), /* cyan */
MAKE_YUV_601(0, 192, 0), /* green */
MAKE_YUV_601(192, 0, 192), /* magenta */
MAKE_YUV_601(192, 0, 0), /* red */
MAKE_YUV_601(0, 0, 192), /* blue */
};
const struct color_yuv colors_middle[] = {
MAKE_YUV_601(0, 0, 192), /* blue */
MAKE_YUV_601(19, 19, 19), /* black */
MAKE_YUV_601(192, 0, 192), /* magenta */
MAKE_YUV_601(19, 19, 19), /* black */
MAKE_YUV_601(0, 192, 192), /* cyan */
MAKE_YUV_601(19, 19, 19), /* black */
MAKE_YUV_601(192, 192, 192), /* grey */
};
const struct color_yuv colors_bottom[] = {
MAKE_YUV_601(0, 33, 76), /* in-phase */
MAKE_YUV_601(255, 255, 255), /* super white */
MAKE_YUV_601(50, 0, 106), /* quadrature */
MAKE_YUV_601(19, 19, 19), /* black */
MAKE_YUV_601(9, 9, 9), /* 3.5% */
MAKE_YUV_601(19, 19, 19), /* 7.5% */
MAKE_YUV_601(29, 29, 29), /* 11.5% */
MAKE_YUV_601(19, 19, 19), /* black */
};
unsigned int cs = yuv->chroma_stride;
unsigned int xsub = yuv->xsub;
unsigned int ysub = yuv->ysub;
unsigned int x;
unsigned int y;
/* Luma */
for (y = 0; y < height * 6 / 9; ++y) {
for (x = 0; x < width; ++x)
y_mem[x] = colors_top[x * 7 / width].y;
y_mem += stride;
}
for (; y < height * 7 / 9; ++y) {
for (x = 0; x < width; ++x)
y_mem[x] = colors_middle[x * 7 / width].y;
y_mem += stride;
}
for (; y < height; ++y) {
for (x = 0; x < width * 5 / 7; ++x)
y_mem[x] = colors_bottom[x * 4 / (width * 5 / 7)].y;
for (; x < width * 6 / 7; ++x)
y_mem[x] = colors_bottom[(x - width * 5 / 7) * 3
/ (width / 7) + 4].y;
for (; x < width; ++x)
y_mem[x] = colors_bottom[7].y;
y_mem += stride;
}
/* Chroma */
for (y = 0; y < height / ysub * 6 / 9; ++y) {
for (x = 0; x < width; x += xsub) {
u_mem[x*cs/xsub] = colors_top[x * 7 / width].u;
v_mem[x*cs/xsub] = colors_top[x * 7 / width].v;
}
u_mem += stride * cs / xsub;
v_mem += stride * cs / xsub;
}
for (; y < height / ysub * 7 / 9; ++y) {
for (x = 0; x < width; x += xsub) {
u_mem[x*cs/xsub] = colors_middle[x * 7 / width].u;
v_mem[x*cs/xsub] = colors_middle[x * 7 / width].v;
}
u_mem += stride * cs / xsub;
v_mem += stride * cs / xsub;
}
for (; y < height / ysub; ++y) {
for (x = 0; x < width * 5 / 7; x += xsub) {
u_mem[x*cs/xsub] =
colors_bottom[x * 4 / (width * 5 / 7)].u;
v_mem[x*cs/xsub] =
colors_bottom[x * 4 / (width * 5 / 7)].v;
}
for (; x < width * 6 / 7; x += xsub) {
u_mem[x*cs/xsub] = colors_bottom[(x - width * 5 / 7) *
3 / (width / 7) + 4].u;
v_mem[x*cs/xsub] = colors_bottom[(x - width * 5 / 7) *
3 / (width / 7) + 4].v;
}
for (; x < width; x += xsub) {
u_mem[x*cs/xsub] = colors_bottom[7].u;
v_mem[x*cs/xsub] = colors_bottom[7].v;
}
u_mem += stride * cs / xsub;
v_mem += stride * cs / xsub;
}
}
static void
fill_smpte_yuv_packed(const struct yuv_info *yuv, unsigned char *mem,
unsigned int width, unsigned int height,
unsigned int stride)
{
const struct color_yuv colors_top[] = {
MAKE_YUV_601(191, 192, 192), /* grey */
MAKE_YUV_601(192, 192, 0), /* yellow */
MAKE_YUV_601(0, 192, 192), /* cyan */
MAKE_YUV_601(0, 192, 0), /* green */
MAKE_YUV_601(192, 0, 192), /* magenta */
MAKE_YUV_601(192, 0, 0), /* red */
MAKE_YUV_601(0, 0, 192), /* blue */
};
const struct color_yuv colors_middle[] = {
MAKE_YUV_601(0, 0, 192), /* blue */
MAKE_YUV_601(19, 19, 19), /* black */
MAKE_YUV_601(192, 0, 192), /* magenta */
MAKE_YUV_601(19, 19, 19), /* black */
MAKE_YUV_601(0, 192, 192), /* cyan */
MAKE_YUV_601(19, 19, 19), /* black */
MAKE_YUV_601(192, 192, 192), /* grey */
};
const struct color_yuv colors_bottom[] = {
MAKE_YUV_601(0, 33, 76), /* in-phase */
MAKE_YUV_601(255, 255, 255), /* super white */
MAKE_YUV_601(50, 0, 106), /* quadrature */
MAKE_YUV_601(19, 19, 19), /* black */
MAKE_YUV_601(9, 9, 9), /* 3.5% */
MAKE_YUV_601(19, 19, 19), /* 7.5% */
MAKE_YUV_601(29, 29, 29), /* 11.5% */
MAKE_YUV_601(19, 19, 19), /* black */
};
unsigned char *y_mem = (yuv->order & YUV_YC) ? mem : mem + 1;
unsigned char *c_mem = (yuv->order & YUV_CY) ? mem : mem + 1;
unsigned int u = (yuv->order & YUV_YCrCb) ? 2 : 0;
unsigned int v = (yuv->order & YUV_YCbCr) ? 2 : 0;
unsigned int x;
unsigned int y;
/* Luma */
for (y = 0; y < height * 6 / 9; ++y) {
for (x = 0; x < width; ++x)
y_mem[2*x] = colors_top[x * 7 / width].y;
y_mem += stride;
}
for (; y < height * 7 / 9; ++y) {
for (x = 0; x < width; ++x)
y_mem[2*x] = colors_middle[x * 7 / width].y;
y_mem += stride;
}
for (; y < height; ++y) {
for (x = 0; x < width * 5 / 7; ++x)
y_mem[2*x] = colors_bottom[x * 4 / (width * 5 / 7)].y;
for (; x < width * 6 / 7; ++x)
y_mem[2*x] = colors_bottom[(x - width * 5 / 7) * 3
/ (width / 7) + 4].y;
for (; x < width; ++x)
y_mem[2*x] = colors_bottom[7].y;
y_mem += stride;
}
/* Chroma */
for (y = 0; y < height * 6 / 9; ++y) {
for (x = 0; x < width; x += 2) {
c_mem[2*x+u] = colors_top[x * 7 / width].u;
c_mem[2*x+v] = colors_top[x * 7 / width].v;
}
c_mem += stride;
}
for (; y < height * 7 / 9; ++y) {
for (x = 0; x < width; x += 2) {
c_mem[2*x+u] = colors_middle[x * 7 / width].u;
c_mem[2*x+v] = colors_middle[x * 7 / width].v;
}
c_mem += stride;
}
for (; y < height; ++y) {
for (x = 0; x < width * 5 / 7; x += 2) {
c_mem[2*x+u] = colors_bottom[x * 4 / (width * 5 / 7)].u;
c_mem[2*x+v] = colors_bottom[x * 4 / (width * 5 / 7)].v;
}
for (; x < width * 6 / 7; x += 2) {
c_mem[2*x+u] = colors_bottom[(x - width * 5 / 7) *
3 / (width / 7) + 4].u;
c_mem[2*x+v] = colors_bottom[(x - width * 5 / 7) *
3 / (width / 7) + 4].v;
}
for (; x < width; x += 2) {
c_mem[2*x+u] = colors_bottom[7].u;
c_mem[2*x+v] = colors_bottom[7].v;
}
c_mem += stride;
}
}
static void
fill_smpte_rgb16(const struct rgb_info *rgb, unsigned char *mem,
unsigned int width, unsigned int height, unsigned int stride)
{
const uint16_t colors_top[] = {
MAKE_RGBA(rgb, 192, 192, 192, 255), /* grey */
MAKE_RGBA(rgb, 192, 192, 0, 255), /* yellow */
MAKE_RGBA(rgb, 0, 192, 192, 255), /* cyan */
MAKE_RGBA(rgb, 0, 192, 0, 255), /* green */
MAKE_RGBA(rgb, 192, 0, 192, 255), /* magenta */
MAKE_RGBA(rgb, 192, 0, 0, 255), /* red */
MAKE_RGBA(rgb, 0, 0, 192, 255), /* blue */
};
const uint16_t colors_middle[] = {
MAKE_RGBA(rgb, 0, 0, 192, 127), /* blue */
MAKE_RGBA(rgb, 19, 19, 19, 127), /* black */
MAKE_RGBA(rgb, 192, 0, 192, 127), /* magenta */
MAKE_RGBA(rgb, 19, 19, 19, 127), /* black */
MAKE_RGBA(rgb, 0, 192, 192, 127), /* cyan */
MAKE_RGBA(rgb, 19, 19, 19, 127), /* black */
MAKE_RGBA(rgb, 192, 192, 192, 127), /* grey */
};
const uint16_t colors_bottom[] = {
MAKE_RGBA(rgb, 0, 33, 76, 255), /* in-phase */
MAKE_RGBA(rgb, 255, 255, 255, 255), /* super white */
MAKE_RGBA(rgb, 50, 0, 106, 255), /* quadrature */
MAKE_RGBA(rgb, 19, 19, 19, 255), /* black */
MAKE_RGBA(rgb, 9, 9, 9, 255), /* 3.5% */
MAKE_RGBA(rgb, 19, 19, 19, 255), /* 7.5% */
MAKE_RGBA(rgb, 29, 29, 29, 255), /* 11.5% */
MAKE_RGBA(rgb, 19, 19, 19, 255), /* black */
};
unsigned int x;
unsigned int y;
for (y = 0; y < height * 6 / 9; ++y) {
for (x = 0; x < width; ++x)
((uint16_t *)mem)[x] = colors_top[x * 7 / width];
mem += stride;
}
for (; y < height * 7 / 9; ++y) {
for (x = 0; x < width; ++x)
((uint16_t *)mem)[x] = colors_middle[x * 7 / width];
mem += stride;
}
for (; y < height; ++y) {
for (x = 0; x < width * 5 / 7; ++x)
((uint16_t *)mem)[x] =
colors_bottom[x * 4 / (width * 5 / 7)];
for (; x < width * 6 / 7; ++x)
((uint16_t *)mem)[x] =
colors_bottom[(x - width * 5 / 7) * 3
/ (width / 7) + 4];
for (; x < width; ++x)
((uint16_t *)mem)[x] = colors_bottom[7];
mem += stride;
}
}
static void
fill_smpte_rgb24(const struct rgb_info *rgb, void *mem,
unsigned int width, unsigned int height, unsigned int stride)
{
const struct color_rgb24 colors_top[] = {
MAKE_RGB24(rgb, 192, 192, 192), /* grey */
MAKE_RGB24(rgb, 192, 192, 0), /* yellow */
MAKE_RGB24(rgb, 0, 192, 192), /* cyan */
MAKE_RGB24(rgb, 0, 192, 0), /* green */
MAKE_RGB24(rgb, 192, 0, 192), /* magenta */
MAKE_RGB24(rgb, 192, 0, 0), /* red */
MAKE_RGB24(rgb, 0, 0, 192), /* blue */
};
const struct color_rgb24 colors_middle[] = {
MAKE_RGB24(rgb, 0, 0, 192), /* blue */
MAKE_RGB24(rgb, 19, 19, 19), /* black */
MAKE_RGB24(rgb, 192, 0, 192), /* magenta */
MAKE_RGB24(rgb, 19, 19, 19), /* black */
MAKE_RGB24(rgb, 0, 192, 192), /* cyan */
MAKE_RGB24(rgb, 19, 19, 19), /* black */
MAKE_RGB24(rgb, 192, 192, 192), /* grey */
};
const struct color_rgb24 colors_bottom[] = {
MAKE_RGB24(rgb, 0, 33, 76), /* in-phase */
MAKE_RGB24(rgb, 255, 255, 255), /* super white */
MAKE_RGB24(rgb, 50, 0, 106), /* quadrature */
MAKE_RGB24(rgb, 19, 19, 19), /* black */
MAKE_RGB24(rgb, 9, 9, 9), /* 3.5% */
MAKE_RGB24(rgb, 19, 19, 19), /* 7.5% */
MAKE_RGB24(rgb, 29, 29, 29), /* 11.5% */
MAKE_RGB24(rgb, 19, 19, 19), /* black */
};
unsigned int x;
unsigned int y;
for (y = 0; y < height * 6 / 9; ++y) {
for (x = 0; x < width; ++x)
((struct color_rgb24 *)mem)[x] =
colors_top[x * 7 / width];
mem += stride;
}
for (; y < height * 7 / 9; ++y) {
for (x = 0; x < width; ++x)
((struct color_rgb24 *)mem)[x] =
colors_middle[x * 7 / width];
mem += stride;
}
for (; y < height; ++y) {
for (x = 0; x < width * 5 / 7; ++x)
((struct color_rgb24 *)mem)[x] =
colors_bottom[x * 4 / (width * 5 / 7)];
for (; x < width * 6 / 7; ++x)
((struct color_rgb24 *)mem)[x] =
colors_bottom[(x - width * 5 / 7) * 3
/ (width / 7) + 4];
for (; x < width; ++x)
((struct color_rgb24 *)mem)[x] = colors_bottom[7];
mem += stride;
}
}
static void
fill_smpte_rgb32(const struct rgb_info *rgb, unsigned char *mem,
unsigned int width, unsigned int height, unsigned int stride)
{
const uint32_t colors_top[] = {
MAKE_RGBA(rgb, 192, 192, 192, 255), /* grey */
MAKE_RGBA(rgb, 192, 192, 0, 255), /* yellow */
MAKE_RGBA(rgb, 0, 192, 192, 255), /* cyan */
MAKE_RGBA(rgb, 0, 192, 0, 255), /* green */
MAKE_RGBA(rgb, 192, 0, 192, 255), /* magenta */
MAKE_RGBA(rgb, 192, 0, 0, 255), /* red */
MAKE_RGBA(rgb, 0, 0, 192, 255), /* blue */
};
const uint32_t colors_middle[] = {
MAKE_RGBA(rgb, 0, 0, 192, 127), /* blue */
MAKE_RGBA(rgb, 19, 19, 19, 127), /* black */
MAKE_RGBA(rgb, 192, 0, 192, 127), /* magenta */
MAKE_RGBA(rgb, 19, 19, 19, 127), /* black */
MAKE_RGBA(rgb, 0, 192, 192, 127), /* cyan */
MAKE_RGBA(rgb, 19, 19, 19, 127), /* black */
MAKE_RGBA(rgb, 192, 192, 192, 127), /* grey */
};
const uint32_t colors_bottom[] = {
MAKE_RGBA(rgb, 0, 33, 76, 255), /* in-phase */
MAKE_RGBA(rgb, 255, 255, 255, 255), /* super white */
MAKE_RGBA(rgb, 50, 0, 106, 255), /* quadrature */
MAKE_RGBA(rgb, 19, 19, 19, 255), /* black */
MAKE_RGBA(rgb, 9, 9, 9, 255), /* 3.5% */
MAKE_RGBA(rgb, 19, 19, 19, 255), /* 7.5% */
MAKE_RGBA(rgb, 29, 29, 29, 255), /* 11.5% */
MAKE_RGBA(rgb, 19, 19, 19, 255), /* black */
};
unsigned int x;
unsigned int y;
for (y = 0; y < height * 6 / 9; ++y) {
for (x = 0; x < width; ++x)
((uint32_t *)mem)[x] = colors_top[x * 7 / width];
mem += stride;
}
for (; y < height * 7 / 9; ++y) {
for (x = 0; x < width; ++x)
((uint32_t *)mem)[x] = colors_middle[x * 7 / width];
mem += stride;
}
for (; y < height; ++y) {
for (x = 0; x < width * 5 / 7; ++x)
((uint32_t *)mem)[x] =
colors_bottom[x * 4 / (width * 5 / 7)];
for (; x < width * 6 / 7; ++x)
((uint32_t *)mem)[x] =
colors_bottom[(x - width * 5 / 7) * 3
/ (width / 7) + 4];
for (; x < width; ++x)
((uint32_t *)mem)[x] = colors_bottom[7];
mem += stride;
}
}
static void
fill_smpte(const struct format_info *info, void *planes[3], unsigned int width,
unsigned int height, unsigned int stride)
{
unsigned char *u, *v;
switch (info->format) {
case DRM_FORMAT_UYVY:
case DRM_FORMAT_VYUY:
case DRM_FORMAT_YUYV:
case DRM_FORMAT_YVYU:
return fill_smpte_yuv_packed(&info->yuv, planes[0], width,
height, stride);
case DRM_FORMAT_NV12:
case DRM_FORMAT_NV21:
case DRM_FORMAT_NV16:
case DRM_FORMAT_NV61:
u = info->yuv.order & YUV_YCbCr ? planes[1] : planes[1] + 1;
v = info->yuv.order & YUV_YCrCb ? planes[1] : planes[1] + 1;
return fill_smpte_yuv_planar(&info->yuv, planes[0], u, v,
width, height, stride);
case DRM_FORMAT_YUV420:
return fill_smpte_yuv_planar(&info->yuv, planes[0], planes[1],
planes[2], width, height, stride);
case DRM_FORMAT_YVU420:
return fill_smpte_yuv_planar(&info->yuv, planes[0], planes[2],
planes[1], width, height, stride);
case DRM_FORMAT_ARGB4444:
case DRM_FORMAT_XRGB4444:
case DRM_FORMAT_ABGR4444:
case DRM_FORMAT_XBGR4444:
case DRM_FORMAT_RGBA4444:
case DRM_FORMAT_RGBX4444:
case DRM_FORMAT_BGRA4444:
case DRM_FORMAT_BGRX4444:
case DRM_FORMAT_RGB565:
case DRM_FORMAT_BGR565:
case DRM_FORMAT_ARGB1555:
case DRM_FORMAT_XRGB1555:
case DRM_FORMAT_ABGR1555:
case DRM_FORMAT_XBGR1555:
case DRM_FORMAT_RGBA5551:
case DRM_FORMAT_RGBX5551:
case DRM_FORMAT_BGRA5551:
case DRM_FORMAT_BGRX5551:
return fill_smpte_rgb16(&info->rgb, planes[0],
width, height, stride);
case DRM_FORMAT_BGR888:
case DRM_FORMAT_RGB888:
return fill_smpte_rgb24(&info->rgb, planes[0],
width, height, stride);
case DRM_FORMAT_ARGB8888:
case DRM_FORMAT_XRGB8888:
case DRM_FORMAT_ABGR8888:
case DRM_FORMAT_XBGR8888:
case DRM_FORMAT_RGBA8888:
case DRM_FORMAT_RGBX8888:
case DRM_FORMAT_BGRA8888:
case DRM_FORMAT_BGRX8888:
case DRM_FORMAT_ARGB2101010:
case DRM_FORMAT_XRGB2101010:
case DRM_FORMAT_ABGR2101010:
case DRM_FORMAT_XBGR2101010:
case DRM_FORMAT_RGBA1010102:
case DRM_FORMAT_RGBX1010102:
case DRM_FORMAT_BGRA1010102:
case DRM_FORMAT_BGRX1010102:
return fill_smpte_rgb32(&info->rgb, planes[0],
width, height, stride);
}
}
/* swap these for big endian.. */
#define RED 2
#define GREEN 1
#define BLUE 0
static void
make_pwetty(void *data, int width, int height, int stride, uint32_t format)
{
#ifdef HAVE_CAIRO
cairo_surface_t *surface;
cairo_t *cr;
int x, y;
cairo_format_t cairo_format;
/* we can ignore the order of R,G,B channels */
switch (format) {
case DRM_FORMAT_XRGB8888:
case DRM_FORMAT_ARGB8888:
case DRM_FORMAT_XBGR8888:
case DRM_FORMAT_ABGR8888:
cairo_format = CAIRO_FORMAT_ARGB32;
break;
case DRM_FORMAT_RGB565:
case DRM_FORMAT_BGR565:
cairo_format = CAIRO_FORMAT_RGB16_565;
break;
default:
return;
}
surface = cairo_image_surface_create_for_data(data,
cairo_format,
width, height,
stride);
cr = cairo_create(surface);
cairo_surface_destroy(surface);
cairo_set_line_cap(cr, CAIRO_LINE_CAP_SQUARE);
for (x = 0; x < width; x += 250)
for (y = 0; y < height; y += 250) {
char buf[64];
cairo_move_to(cr, x, y - 20);
cairo_line_to(cr, x, y + 20);
cairo_move_to(cr, x - 20, y);
cairo_line_to(cr, x + 20, y);
cairo_new_sub_path(cr);
cairo_arc(cr, x, y, 10, 0, M_PI * 2);
cairo_set_line_width(cr, 4);
cairo_set_source_rgb(cr, 0, 0, 0);
cairo_stroke_preserve(cr);
cairo_set_source_rgb(cr, 1, 1, 1);
cairo_set_line_width(cr, 2);
cairo_stroke(cr);
snprintf(buf, sizeof buf, "%d, %d", x, y);
cairo_move_to(cr, x + 20, y + 20);
cairo_text_path(cr, buf);
cairo_set_source_rgb(cr, 0, 0, 0);
cairo_stroke_preserve(cr);
cairo_set_source_rgb(cr, 1, 1, 1);
cairo_fill(cr);
}
cairo_destroy(cr);
#endif
}
static void
fill_tiles_yuv_planar(const struct format_info *info,
unsigned char *y_mem, unsigned char *u_mem,
unsigned char *v_mem, unsigned int width,
unsigned int height, unsigned int stride)
{
const struct yuv_info *yuv = &info->yuv;
unsigned int cs = yuv->chroma_stride;
unsigned int xsub = yuv->xsub;
unsigned int ysub = yuv->ysub;
unsigned int x;
unsigned int y;
for (y = 0; y < height; ++y) {
for (x = 0; x < width; ++x) {
div_t d = div(x+y, width);
uint32_t rgb32 = 0x00130502 * (d.quot >> 6)
+ 0x000a1120 * (d.rem >> 6);
struct color_yuv color =
MAKE_YUV_601((rgb32 >> 16) & 0xff,
(rgb32 >> 8) & 0xff, rgb32 & 0xff);
y_mem[x] = color.y;
u_mem[x/xsub*cs] = color.u;
v_mem[x/xsub*cs] = color.v;
}
y_mem += stride;
if ((y + 1) % ysub == 0) {
u_mem += stride * cs / xsub;
v_mem += stride * cs / xsub;
}
}
}
static void
fill_tiles_yuv_packed(const struct format_info *info, unsigned char *mem,
unsigned int width, unsigned int height,
unsigned int stride)
{
const struct yuv_info *yuv = &info->yuv;
unsigned char *y_mem = (yuv->order & YUV_YC) ? mem : mem + 1;
unsigned char *c_mem = (yuv->order & YUV_CY) ? mem : mem + 1;
unsigned int u = (yuv->order & YUV_YCrCb) ? 2 : 0;
unsigned int v = (yuv->order & YUV_YCbCr) ? 2 : 0;
unsigned int x;
unsigned int y;
for (y = 0; y < height; ++y) {
for (x = 0; x < width; x += 2) {
div_t d = div(x+y, width);
uint32_t rgb32 = 0x00130502 * (d.quot >> 6)
+ 0x000a1120 * (d.rem >> 6);
struct color_yuv color =
MAKE_YUV_601((rgb32 >> 16) & 0xff,
(rgb32 >> 8) & 0xff, rgb32 & 0xff);
y_mem[2*x] = color.y;
c_mem[2*x+u] = color.u;
y_mem[2*x+2] = color.y;
c_mem[2*x+v] = color.v;
}
y_mem += stride;
c_mem += stride;
}
}
static void
fill_tiles_rgb16(const struct format_info *info, unsigned char *mem,
unsigned int width, unsigned int height, unsigned int stride)
{
const struct rgb_info *rgb = &info->rgb;
unsigned char *mem_base = mem;
unsigned int x, y;
for (y = 0; y < height; ++y) {
for (x = 0; x < width; ++x) {
div_t d = div(x+y, width);
uint32_t rgb32 = 0x00130502 * (d.quot >> 6)
+ 0x000a1120 * (d.rem >> 6);
uint16_t color =
MAKE_RGBA(rgb, (rgb32 >> 16) & 0xff,
(rgb32 >> 8) & 0xff, rgb32 & 0xff,
255);
((uint16_t *)mem)[x] = color;
}
mem += stride;
}
make_pwetty(mem_base, width, height, stride, info->format);
}
static void
fill_tiles_rgb24(const struct format_info *info, unsigned char *mem,
unsigned int width, unsigned int height, unsigned int stride)
{
const struct rgb_info *rgb = &info->rgb;
unsigned int x, y;
for (y = 0; y < height; ++y) {
for (x = 0; x < width; ++x) {
div_t d = div(x+y, width);
uint32_t rgb32 = 0x00130502 * (d.quot >> 6)
+ 0x000a1120 * (d.rem >> 6);
struct color_rgb24 color =
MAKE_RGB24(rgb, (rgb32 >> 16) & 0xff,
(rgb32 >> 8) & 0xff, rgb32 & 0xff);
((struct color_rgb24 *)mem)[x] = color;
}
mem += stride;
}
}
static void
fill_tiles_rgb32(const struct format_info *info, unsigned char *mem,
unsigned int width, unsigned int height, unsigned int stride)
{
const struct rgb_info *rgb = &info->rgb;
unsigned char *mem_base = mem;
unsigned int x, y;
for (y = 0; y < height; ++y) {
for (x = 0; x < width; ++x) {
div_t d = div(x+y, width);
uint32_t rgb32 = 0x00130502 * (d.quot >> 6)
+ 0x000a1120 * (d.rem >> 6);
uint32_t alpha = ((y < height/2) && (x < width/2)) ? 127 : 255;
uint32_t color =
MAKE_RGBA(rgb, (rgb32 >> 16) & 0xff,
(rgb32 >> 8) & 0xff, rgb32 & 0xff,
alpha);
((uint32_t *)mem)[x] = color;
}
mem += stride;
}
make_pwetty(mem_base, width, height, stride, info->format);
}
static void
fill_tiles(const struct format_info *info, void *planes[3], unsigned int width,
unsigned int height, unsigned int stride)
{
unsigned char *u, *v;
switch (info->format) {
case DRM_FORMAT_UYVY:
case DRM_FORMAT_VYUY:
case DRM_FORMAT_YUYV:
case DRM_FORMAT_YVYU:
return fill_tiles_yuv_packed(info, planes[0],
width, height, stride);
case DRM_FORMAT_NV12:
case DRM_FORMAT_NV21:
case DRM_FORMAT_NV16:
case DRM_FORMAT_NV61:
u = info->yuv.order & YUV_YCbCr ? planes[1] : planes[1] + 1;
v = info->yuv.order & YUV_YCrCb ? planes[1] : planes[1] + 1;
return fill_tiles_yuv_planar(info, planes[0], u, v,
width, height, stride);
case DRM_FORMAT_YUV420:
return fill_tiles_yuv_planar(info, planes[0], planes[1],
planes[2], width, height, stride);
case DRM_FORMAT_YVU420:
return fill_tiles_yuv_planar(info, planes[0], planes[2],
planes[1], width, height, stride);
case DRM_FORMAT_ARGB4444:
case DRM_FORMAT_XRGB4444:
case DRM_FORMAT_ABGR4444:
case DRM_FORMAT_XBGR4444:
case DRM_FORMAT_RGBA4444:
case DRM_FORMAT_RGBX4444:
case DRM_FORMAT_BGRA4444:
case DRM_FORMAT_BGRX4444:
case DRM_FORMAT_RGB565:
case DRM_FORMAT_BGR565:
case DRM_FORMAT_ARGB1555:
case DRM_FORMAT_XRGB1555:
case DRM_FORMAT_ABGR1555:
case DRM_FORMAT_XBGR1555:
case DRM_FORMAT_RGBA5551:
case DRM_FORMAT_RGBX5551:
case DRM_FORMAT_BGRA5551:
case DRM_FORMAT_BGRX5551:
return fill_tiles_rgb16(info, planes[0],
width, height, stride);
case DRM_FORMAT_BGR888:
case DRM_FORMAT_RGB888:
return fill_tiles_rgb24(info, planes[0],
width, height, stride);
case DRM_FORMAT_ARGB8888:
case DRM_FORMAT_XRGB8888:
case DRM_FORMAT_ABGR8888:
case DRM_FORMAT_XBGR8888:
case DRM_FORMAT_RGBA8888:
case DRM_FORMAT_RGBX8888:
case DRM_FORMAT_BGRA8888:
case DRM_FORMAT_BGRX8888:
case DRM_FORMAT_ARGB2101010:
case DRM_FORMAT_XRGB2101010:
case DRM_FORMAT_ABGR2101010:
case DRM_FORMAT_XBGR2101010:
case DRM_FORMAT_RGBA1010102:
case DRM_FORMAT_RGBX1010102:
case DRM_FORMAT_BGRA1010102:
case DRM_FORMAT_BGRX1010102:
return fill_tiles_rgb32(info, planes[0],
width, height, stride);
}
}
static void
fill_plain(const struct format_info *info, void *planes[3], unsigned int width,
unsigned int height, unsigned int stride)
{
memset(planes[0], 0x77, stride * height);
}
/*
* fill_pattern - Fill a buffer with a test pattern
* @format: Pixel format
* @pattern: Test pattern
* @buffer: Buffer memory
* @width: Width in pixels
* @height: Height in pixels
* @stride: Line stride (pitch) in bytes
*
* Fill the buffer with the test pattern specified by the pattern parameter.
* Supported formats vary depending on the selected pattern.
*/
static void
fill_pattern(unsigned int format, enum fill_pattern pattern, void *planes[3],
unsigned int width, unsigned int height, unsigned int stride)
{
const struct format_info *info = NULL;
unsigned int i;
for (i = 0; i < ARRAY_SIZE(format_info); ++i) {
if (format_info[i].format == format) {
info = &format_info[i];
break;
}
}
if (info == NULL)
return;
switch (pattern) {
case PATTERN_TILES:
return fill_tiles(info, planes, width, height, stride);
case PATTERN_SMPTE:
return fill_smpte(info, planes, width, height, stride);
case PATTERN_PLAIN:
return fill_plain(info, planes, width, height, stride);
default:
printf("Error: unsupported test pattern %u.\n", pattern);
break;
}
}
/* -----------------------------------------------------------------------------
* Buffers management
*/
static struct bo *
bo_create_dumb(int fd, unsigned int width, unsigned int height, unsigned int bpp)
{
struct drm_mode_create_dumb arg;
struct bo *bo;
int ret;
bo = calloc(1, sizeof(*bo));
if (bo == NULL) {
fprintf(stderr, "failed to allocate buffer object\n");
return NULL;
}
memset(&arg, 0, sizeof(arg));
arg.bpp = bpp;
arg.width = width;
arg.height = height;
ret = drmIoctl(fd, DRM_IOCTL_MODE_CREATE_DUMB, &arg);
if (ret) {
fprintf(stderr, "failed to create dumb buffer: %s\n",
strerror(errno));
free(bo);
return NULL;
}
bo->fd = fd;
bo->handle = arg.handle;
bo->size = arg.size;
bo->pitch = arg.pitch;
return bo;
}
static int bo_map(struct bo *bo, void **out)
{
struct drm_mode_map_dumb arg;
void *map;
int ret;
memset(&arg, 0, sizeof(arg));
arg.handle = bo->handle;
ret = drmIoctl(bo->fd, DRM_IOCTL_MODE_MAP_DUMB, &arg);
if (ret)
return ret;
map = drm_mmap(0, bo->size, PROT_READ | PROT_WRITE, MAP_SHARED,
bo->fd, arg.offset);
if (map == MAP_FAILED)
return -EINVAL;
bo->ptr = map;
*out = map;
return 0;
}
static void bo_unmap(struct bo *bo)
{
if (!bo->ptr)
return;
drm_munmap(bo->ptr, bo->size);
bo->ptr = NULL;
}
struct bo *
bo_create(int fd, unsigned int format,
unsigned int width, unsigned int height,
unsigned int handles[4], unsigned int pitches[4],
unsigned int offsets[4], enum fill_pattern pattern)
{
unsigned int virtual_height;
struct bo *bo;
unsigned int bpp;
void *planes[3] = { 0, };
void *virtual;
int ret;
switch (format) {
case DRM_FORMAT_NV12:
case DRM_FORMAT_NV21:
case DRM_FORMAT_NV16:
case DRM_FORMAT_NV61:
case DRM_FORMAT_YUV420:
case DRM_FORMAT_YVU420:
bpp = 8;
break;
case DRM_FORMAT_ARGB4444:
case DRM_FORMAT_XRGB4444:
case DRM_FORMAT_ABGR4444:
case DRM_FORMAT_XBGR4444:
case DRM_FORMAT_RGBA4444:
case DRM_FORMAT_RGBX4444:
case DRM_FORMAT_BGRA4444:
case DRM_FORMAT_BGRX4444:
case DRM_FORMAT_ARGB1555:
case DRM_FORMAT_XRGB1555:
case DRM_FORMAT_ABGR1555:
case DRM_FORMAT_XBGR1555:
case DRM_FORMAT_RGBA5551:
case DRM_FORMAT_RGBX5551:
case DRM_FORMAT_BGRA5551:
case DRM_FORMAT_BGRX5551:
case DRM_FORMAT_RGB565:
case DRM_FORMAT_BGR565:
case DRM_FORMAT_UYVY:
case DRM_FORMAT_VYUY:
case DRM_FORMAT_YUYV:
case DRM_FORMAT_YVYU:
bpp = 16;
break;
case DRM_FORMAT_BGR888:
case DRM_FORMAT_RGB888:
bpp = 24;
break;
case DRM_FORMAT_ARGB8888:
case DRM_FORMAT_XRGB8888:
case DRM_FORMAT_ABGR8888:
case DRM_FORMAT_XBGR8888:
case DRM_FORMAT_RGBA8888:
case DRM_FORMAT_RGBX8888:
case DRM_FORMAT_BGRA8888:
case DRM_FORMAT_BGRX8888:
case DRM_FORMAT_ARGB2101010:
case DRM_FORMAT_XRGB2101010:
case DRM_FORMAT_ABGR2101010:
case DRM_FORMAT_XBGR2101010:
case DRM_FORMAT_RGBA1010102:
case DRM_FORMAT_RGBX1010102:
case DRM_FORMAT_BGRA1010102:
case DRM_FORMAT_BGRX1010102:
bpp = 32;
break;
default:
fprintf(stderr, "unsupported format 0x%08x\n", format);
return NULL;
}
switch (format) {
case DRM_FORMAT_NV12:
case DRM_FORMAT_NV21:
case DRM_FORMAT_YUV420:
case DRM_FORMAT_YVU420:
virtual_height = height * 3 / 2;
break;
case DRM_FORMAT_NV16:
case DRM_FORMAT_NV61:
virtual_height = height * 2;
break;
default:
virtual_height = height;
break;
}
bo = bo_create_dumb(fd, width, virtual_height, bpp);
if (!bo)
return NULL;
ret = bo_map(bo, &virtual);
if (ret) {
fprintf(stderr, "failed to map buffer: %s\n",
strerror(-errno));
bo_destroy(bo);
return NULL;
}
/* just testing a limited # of formats to test single
* and multi-planar path.. would be nice to add more..
*/
switch (format) {
case DRM_FORMAT_UYVY:
case DRM_FORMAT_VYUY:
case DRM_FORMAT_YUYV:
case DRM_FORMAT_YVYU:
offsets[0] = 0;
handles[0] = bo->handle;
pitches[0] = bo->pitch;
planes[0] = virtual;
break;
case DRM_FORMAT_NV12:
case DRM_FORMAT_NV21:
case DRM_FORMAT_NV16:
case DRM_FORMAT_NV61:
offsets[0] = 0;
handles[0] = bo->handle;
pitches[0] = bo->pitch;
pitches[1] = pitches[0];
offsets[1] = pitches[0] * height;
handles[1] = bo->handle;
planes[0] = virtual;
planes[1] = virtual + offsets[1];
break;
case DRM_FORMAT_YUV420:
case DRM_FORMAT_YVU420:
offsets[0] = 0;
handles[0] = bo->handle;
pitches[0] = bo->pitch;
pitches[1] = pitches[0] / 2;
offsets[1] = pitches[0] * height;
handles[1] = bo->handle;
pitches[2] = pitches[1];
offsets[2] = offsets[1] + pitches[1] * height / 2;
handles[2] = bo->handle;
planes[0] = virtual;
planes[1] = virtual + offsets[1];
planes[2] = virtual + offsets[2];
break;
case DRM_FORMAT_ARGB4444:
case DRM_FORMAT_XRGB4444:
case DRM_FORMAT_ABGR4444:
case DRM_FORMAT_XBGR4444:
case DRM_FORMAT_RGBA4444:
case DRM_FORMAT_RGBX4444:
case DRM_FORMAT_BGRA4444:
case DRM_FORMAT_BGRX4444:
case DRM_FORMAT_ARGB1555:
case DRM_FORMAT_XRGB1555:
case DRM_FORMAT_ABGR1555:
case DRM_FORMAT_XBGR1555:
case DRM_FORMAT_RGBA5551:
case DRM_FORMAT_RGBX5551:
case DRM_FORMAT_BGRA5551:
case DRM_FORMAT_BGRX5551:
case DRM_FORMAT_RGB565:
case DRM_FORMAT_BGR565:
case DRM_FORMAT_BGR888:
case DRM_FORMAT_RGB888:
case DRM_FORMAT_ARGB8888:
case DRM_FORMAT_XRGB8888:
case DRM_FORMAT_ABGR8888:
case DRM_FORMAT_XBGR8888:
case DRM_FORMAT_RGBA8888:
case DRM_FORMAT_RGBX8888:
case DRM_FORMAT_BGRA8888:
case DRM_FORMAT_BGRX8888:
case DRM_FORMAT_ARGB2101010:
case DRM_FORMAT_XRGB2101010:
case DRM_FORMAT_ABGR2101010:
case DRM_FORMAT_XBGR2101010:
case DRM_FORMAT_RGBA1010102:
case DRM_FORMAT_RGBX1010102:
case DRM_FORMAT_BGRA1010102:
case DRM_FORMAT_BGRX1010102:
offsets[0] = 0;
handles[0] = bo->handle;
pitches[0] = bo->pitch;
planes[0] = virtual;
break;
}
fill_pattern(format, pattern, planes, width, height, pitches[0]);
bo_unmap(bo);
return bo;
}
void bo_destroy(struct bo *bo)
{
struct drm_mode_destroy_dumb arg;
int ret;
memset(&arg, 0, sizeof(arg));
arg.handle = bo->handle;
ret = drmIoctl(bo->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &arg);
if (ret)
fprintf(stderr, "failed to destroy dumb buffer: %s\n",
strerror(errno));
free(bo);
}
|