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
|
/*
* Copyright (C) 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <malloc.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <linux/limits.h>
#include <sys/ioctl.h>
#include <adf/adf.h>
#define ADF_BASE_PATH "/dev/"
static ssize_t adf_find_nodes(const char *pattern, adf_id_t **ids)
{
DIR *dir;
struct dirent *dirent;
size_t n = 0;
ssize_t ret;
adf_id_t *ids_ret = NULL;
dir = opendir(ADF_BASE_PATH);
if (!dir)
return -errno;
errno = 0;
while ((dirent = readdir(dir))) {
adf_id_t id;
int matched = sscanf(dirent->d_name, pattern, &id);
if (matched < 0) {
ret = -errno;
goto done;
} else if (matched != 1) {
continue;
}
adf_id_t *new_ids = realloc(ids_ret, (n + 1) * sizeof(ids_ret[0]));
if (!new_ids) {
ret = -ENOMEM;
goto done;
}
ids_ret = new_ids;
ids_ret[n] = id;
n++;
}
if (errno)
ret = -errno;
else
ret = n;
done:
closedir(dir);
if (ret < 0)
free(ids_ret);
else
*ids = ids_ret;
return ret;
}
ssize_t adf_devices(adf_id_t **ids)
{
return adf_find_nodes("adf%u", ids);
}
int adf_device_open(adf_id_t id, int flags, struct adf_device *dev)
{
char filename[64];
int err;
dev->id = id;
snprintf(filename, sizeof(filename), ADF_BASE_PATH "adf%u", id);
dev->fd = open(filename, flags);
if (dev->fd < 0)
return -errno;
return 0;
}
void adf_device_close(struct adf_device *dev)
{
if (dev->fd >= 0)
close(dev->fd);
}
int adf_get_device_data(struct adf_device *dev, struct adf_device_data *data)
{
int err;
int ret = 0;
memset(data, 0, sizeof(*data));
err = ioctl(dev->fd, ADF_GET_DEVICE_DATA, data);
if (err < 0)
return -ENOMEM;
if (data->n_attachments) {
data->attachments = malloc(sizeof(data->attachments[0]) *
data->n_attachments);
if (!data->attachments)
return -ENOMEM;
}
if (data->n_allowed_attachments) {
data->allowed_attachments =
malloc(sizeof(data->allowed_attachments[0]) *
data->n_allowed_attachments);
if (!data->allowed_attachments) {
ret = -ENOMEM;
goto done;
}
}
if (data->custom_data_size) {
data->custom_data = malloc(data->custom_data_size);
if (!data->custom_data) {
ret = -ENOMEM;
goto done;
}
}
err = ioctl(dev->fd, ADF_GET_DEVICE_DATA, data);
if (err < 0)
ret = -errno;
done:
if (ret < 0)
adf_free_device_data(data);
return ret;
}
void adf_free_device_data(struct adf_device_data *data)
{
free(data->attachments);
free(data->allowed_attachments);
free(data->custom_data);
}
int adf_device_post(struct adf_device *dev,
adf_id_t *interfaces, size_t n_interfaces,
struct adf_buffer_config *bufs, size_t n_bufs,
void *custom_data, size_t custom_data_size)
{
int err;
struct adf_post_config data;
memset(&data, 0, sizeof(data));
data.interfaces = interfaces;
data.n_interfaces = n_interfaces;
data.bufs = bufs;
data.n_bufs = n_bufs;
data.custom_data = custom_data;
data.custom_data_size = custom_data_size;
err = ioctl(dev->fd, ADF_POST_CONFIG, &data);
if (err < 0)
return -errno;
return (int)data.complete_fence;
}
static int adf_device_attachment(struct adf_device *dev,
adf_id_t overlay_engine, adf_id_t interface, bool attach)
{
int err;
struct adf_attachment_config data;
memset(&data, 0, sizeof(data));
data.overlay_engine = overlay_engine;
data.interface = interface;
err = ioctl(dev->fd, attach ? ADF_ATTACH : ADF_DETACH, &data);
if (err < 0)
return -errno;
return 0;
}
int adf_device_attach(struct adf_device *dev, adf_id_t overlay_engine,
adf_id_t interface)
{
return adf_device_attachment(dev, overlay_engine, interface, true);
}
int adf_device_detach(struct adf_device *dev, adf_id_t overlay_engine,
adf_id_t interface)
{
return adf_device_attachment(dev, overlay_engine, interface, false);
}
ssize_t adf_interfaces(struct adf_device *dev, adf_id_t **interfaces)
{
char pattern[64];
snprintf(pattern, sizeof(pattern), "adf-interface%u.%%u", dev->id);
return adf_find_nodes(pattern, interfaces);
}
ssize_t adf_interfaces_for_overlay_engine(struct adf_device *dev,
adf_id_t overlay_engine, adf_id_t **interfaces)
{
struct adf_device_data data;
ssize_t n = 0;
ssize_t ret;
adf_id_t *ids_ret = NULL;
ret = adf_get_device_data(dev, &data);
if (ret < 0)
return ret;
size_t i;
for (i = 0; i < data.n_allowed_attachments; i++) {
if (data.allowed_attachments[i].overlay_engine != overlay_engine)
continue;
adf_id_t *new_ids = realloc(ids_ret, (n + 1) * sizeof(ids_ret[0]));
if (!new_ids) {
ret = -ENOMEM;
goto done;
}
ids_ret = new_ids;
ids_ret[n] = data.allowed_attachments[i].interface;
n++;
}
ret = n;
done:
adf_free_device_data(&data);
if (ret < 0)
free(ids_ret);
else
*interfaces = ids_ret;
return ret;
}
static ssize_t adf_interfaces_filter(struct adf_device *dev,
adf_id_t *in, size_t n_in, adf_id_t **out,
bool (*filter)(struct adf_interface_data *data, __u32 match),
__u32 match)
{
size_t n = 0;
ssize_t ret;
adf_id_t *ids_ret = NULL;
size_t i;
for (i = 0; i < n_in; i++) {
int fd = adf_interface_open(dev, in[i], O_RDONLY);
if (fd < 0) {
ret = fd;
goto done;
}
struct adf_interface_data data;
ret = adf_get_interface_data(fd, &data);
close(fd);
if (ret < 0)
goto done;
if (!filter(&data, match))
continue;
adf_id_t *new_ids = realloc(ids_ret, (n + 1) * sizeof(ids_ret[0]));
if (!new_ids) {
ret = -ENOMEM;
goto done;
}
ids_ret = new_ids;
ids_ret[n] = in[i];
n++;
}
ret = n;
done:
if (ret < 0)
free(ids_ret);
else
*out = ids_ret;
return ret;
}
static bool adf_interface_type_filter(struct adf_interface_data *data,
__u32 type)
{
return data->type == (enum adf_interface_type)type;
}
ssize_t adf_interfaces_filter_by_type(struct adf_device *dev,
enum adf_interface_type type,
adf_id_t *in, size_t n_in, adf_id_t **out)
{
return adf_interfaces_filter(dev, in, n_in, out, adf_interface_type_filter,
type);
}
static bool adf_interface_flags_filter(struct adf_interface_data *data,
__u32 flag)
{
return !!(data->flags & flag);
}
ssize_t adf_interfaces_filter_by_flag(struct adf_device *dev, __u32 flag,
adf_id_t *in, size_t n_in, adf_id_t **out)
{
return adf_interfaces_filter(dev, in, n_in, out, adf_interface_flags_filter,
flag);
}
int adf_interface_open(struct adf_device *dev, adf_id_t id, int flags)
{
char filename[64];
snprintf(filename, sizeof(filename), ADF_BASE_PATH "adf-interface%u.%u",
dev->id, id);
int fd = open(filename, flags);
if (fd < 0)
return -errno;
return fd;
}
int adf_get_interface_data(int fd, struct adf_interface_data *data)
{
int err;
int ret = 0;
memset(data, 0, sizeof(*data));
err = ioctl(fd, ADF_GET_INTERFACE_DATA, data);
if (err < 0)
return -errno;
if (data->n_available_modes) {
data->available_modes = malloc(sizeof(data->available_modes[0]) *
data->n_available_modes);
if (!data->available_modes)
return -ENOMEM;
}
if (data->custom_data_size) {
data->custom_data = malloc(data->custom_data_size);
if (!data->custom_data) {
ret = -ENOMEM;
goto done;
}
}
err = ioctl(fd, ADF_GET_INTERFACE_DATA, data);
if (err < 0)
ret = -errno;
done:
if (ret < 0)
adf_free_interface_data(data);
return ret;
}
void adf_free_interface_data(struct adf_interface_data *data)
{
free(data->available_modes);
free(data->custom_data);
}
int adf_interface_blank(int fd, __u8 mode)
{
int err = ioctl(fd, ADF_BLANK, mode);
if (err < 0)
return -errno;
return 0;
}
int adf_interface_set_mode(int fd, struct drm_mode_modeinfo *mode)
{
int err = ioctl(fd, ADF_SET_MODE, mode);
if (err < 0)
return -errno;
return 0;
}
int adf_interface_simple_buffer_alloc(int fd, __u32 w, __u32 h,
__u32 format, __u32 *offset, __u32 *pitch)
{
int err;
struct adf_simple_buffer_alloc data;
memset(&data, 0, sizeof(data));
data.w = w;
data.h = h;
data.format = format;
err = ioctl(fd, ADF_SIMPLE_BUFFER_ALLOC, &data);
if (err < 0)
return -errno;
*offset = data.offset;
*pitch = data.pitch;
return (int)data.fd;
}
int adf_interface_simple_post(int fd, __u32 overlay_engine,
__u32 w, __u32 h, __u32 format, int buf_fd, __u32 offset,
__u32 pitch, int acquire_fence)
{
int ret;
struct adf_simple_post_config data;
memset(&data, 0, sizeof(data));
data.buf.overlay_engine = overlay_engine;
data.buf.w = w;
data.buf.h = h;
data.buf.format = format;
data.buf.fd[0] = buf_fd;
data.buf.offset[0] = offset;
data.buf.pitch[0] = pitch;
data.buf.n_planes = 1;
data.buf.acquire_fence = acquire_fence;
ret = ioctl(fd, ADF_SIMPLE_POST_CONFIG, &data);
if (ret < 0)
return -errno;
return (int)data.complete_fence;
}
ssize_t adf_overlay_engines(struct adf_device *dev, adf_id_t **overlay_engines)
{
char pattern[64];
snprintf(pattern, sizeof(pattern), "adf-overlay-engine%u.%%u", dev->id);
return adf_find_nodes(pattern, overlay_engines);
}
ssize_t adf_overlay_engines_for_interface(struct adf_device *dev,
adf_id_t interface, adf_id_t **overlay_engines)
{
struct adf_device_data data;
ssize_t n = 0;
ssize_t ret;
adf_id_t *ids_ret = NULL;
ret = adf_get_device_data(dev, &data);
if (ret < 0)
return ret;
size_t i;
for (i = 0; i < data.n_allowed_attachments; i++) {
if (data.allowed_attachments[i].interface != interface)
continue;
adf_id_t *new_ids = realloc(ids_ret, (n + 1) * sizeof(ids_ret[0]));
if (!new_ids) {
ret = -ENOMEM;
goto done;
}
ids_ret = new_ids;
ids_ret[n] = data.allowed_attachments[i].overlay_engine;
n++;
}
ret = n;
done:
adf_free_device_data(&data);
if (ret < 0)
free(ids_ret);
else
*overlay_engines = ids_ret;
return ret;
}
static ssize_t adf_overlay_engines_filter(struct adf_device *dev,
adf_id_t *in, size_t n_in, adf_id_t **out,
bool (*filter)(struct adf_overlay_engine_data *data, void *cookie),
void *cookie)
{
size_t n = 0;
ssize_t ret;
adf_id_t *ids_ret = NULL;
size_t i;
for (i = 0; i < n_in; i++) {
int fd = adf_overlay_engine_open(dev, in[i], O_RDONLY);
if (fd < 0) {
ret = fd;
goto done;
}
struct adf_overlay_engine_data data;
ret = adf_get_overlay_engine_data(fd, &data);
close(fd);
if (ret < 0)
goto done;
if (!filter(&data, cookie))
continue;
adf_id_t *new_ids = realloc(ids_ret, (n + 1) * sizeof(ids_ret[0]));
if (!new_ids) {
ret = -ENOMEM;
goto done;
}
ids_ret = new_ids;
ids_ret[n] = in[i];
n++;
}
ret = n;
done:
if (ret < 0)
free(ids_ret);
else
*out = ids_ret;
return ret;
}
struct format_filter_cookie {
const __u32 *formats;
size_t n_formats;
};
static bool adf_overlay_engine_format_filter(
struct adf_overlay_engine_data *data, void *cookie)
{
struct format_filter_cookie *c = cookie;
size_t i;
for (i = 0; i < data->n_supported_formats; i++) {
size_t j;
for (j = 0; j < c->n_formats; j++)
if (data->supported_formats[i] == c->formats[j])
return true;
}
return false;
}
ssize_t adf_overlay_engines_filter_by_format(struct adf_device *dev,
const __u32 *formats, size_t n_formats, adf_id_t *in, size_t n_in,
adf_id_t **out)
{
struct format_filter_cookie cookie = { formats, n_formats };
return adf_overlay_engines_filter(dev, in, n_in, out,
adf_overlay_engine_format_filter, &cookie);
}
int adf_overlay_engine_open(struct adf_device *dev, adf_id_t id, int flags)
{
char filename[64];
snprintf(filename, sizeof(filename),
ADF_BASE_PATH "adf-overlay-engine%u.%u", dev->id, id);
int fd = open(filename, flags);
if (fd < 0)
return -errno;
return fd;
}
int adf_get_overlay_engine_data(int fd, struct adf_overlay_engine_data *data)
{
int err;
int ret = 0;
memset(data, 0, sizeof(*data));
err = ioctl(fd, ADF_GET_OVERLAY_ENGINE_DATA, data);
if (err < 0)
return -errno;
if (data->n_supported_formats) {
data->supported_formats = malloc(sizeof(data->supported_formats[0]) *
data->n_supported_formats);
if (!data->supported_formats)
return -ENOMEM;
}
if (data->custom_data_size) {
data->custom_data = malloc(data->custom_data_size);
if (!data->custom_data) {
ret = -ENOMEM;
goto done;
}
}
err = ioctl(fd, ADF_GET_OVERLAY_ENGINE_DATA, data);
if (err < 0)
ret = -errno;
done:
if (ret < 0)
adf_free_overlay_engine_data(data);
return ret;
}
void adf_free_overlay_engine_data(struct adf_overlay_engine_data *data)
{
free(data->supported_formats);
free(data->custom_data);
}
bool adf_overlay_engine_supports_format(int fd, __u32 format)
{
struct adf_overlay_engine_data data;
bool ret = false;
size_t i;
int err = adf_get_overlay_engine_data(fd, &data);
if (err < 0)
return false;
for (i = 0; i < data.n_supported_formats; i++) {
if (data.supported_formats[i] == format) {
ret = true;
break;
}
}
adf_free_overlay_engine_data(&data);
return ret;
}
int adf_set_event(int fd, enum adf_event_type type, bool enabled)
{
struct adf_set_event data;
data.type = type;
data.enabled = enabled;
int err = ioctl(fd, ADF_SET_EVENT, &data);
if (err < 0)
return -errno;
return 0;
}
int adf_read_event(int fd, struct adf_event **event)
{
struct adf_event header;
struct {
struct adf_event base;
uint8_t data[0];
} *event_ret;
size_t data_size;
int ret = 0;
int err = read(fd, &header, sizeof(header));
if (err < 0)
return -errno;
if ((size_t)err < sizeof(header))
return -EIO;
if (header.length < sizeof(header))
return -EIO;
event_ret = malloc(header.length);
if (!event_ret)
return -ENOMEM;
data_size = header.length - sizeof(header);
memcpy(event_ret, &header, sizeof(header));
ssize_t read_size = read(fd, &event_ret->data, data_size);
if (read_size < 0) {
ret = -errno;
goto done;
}
if ((size_t)read_size < data_size) {
ret = -EIO;
goto done;
}
*event = &event_ret->base;
done:
if (ret < 0)
free(event_ret);
return ret;
}
void adf_format_str(__u32 format, char buf[ADF_FORMAT_STR_SIZE])
{
buf[0] = format & 0xFF;
buf[1] = (format >> 8) & 0xFF;
buf[2] = (format >> 16) & 0xFF;
buf[3] = (format >> 24) & 0xFF;
buf[4] = '\0';
}
static bool adf_find_simple_post_overlay_engine(struct adf_device *dev,
const __u32 *formats, size_t n_formats,
adf_id_t interface, adf_id_t *overlay_engine)
{
adf_id_t *engs;
ssize_t n_engs = adf_overlay_engines_for_interface(dev, interface, &engs);
if (n_engs <= 0)
return false;
adf_id_t *filtered_engs;
ssize_t n_filtered_engs = adf_overlay_engines_filter_by_format(dev,
formats, n_formats, engs, n_engs, &filtered_engs);
free(engs);
if (n_filtered_engs <= 0)
return false;
*overlay_engine = filtered_engs[0];
free(filtered_engs);
return true;
}
static const __u32 any_rgb_format[] = {
DRM_FORMAT_C8,
DRM_FORMAT_RGB332,
DRM_FORMAT_BGR233,
DRM_FORMAT_XRGB1555,
DRM_FORMAT_XBGR1555,
DRM_FORMAT_RGBX5551,
DRM_FORMAT_BGRX5551,
DRM_FORMAT_ARGB1555,
DRM_FORMAT_ABGR1555,
DRM_FORMAT_RGBA5551,
DRM_FORMAT_BGRA5551,
DRM_FORMAT_RGB565,
DRM_FORMAT_BGR565,
DRM_FORMAT_RGB888,
DRM_FORMAT_BGR888,
DRM_FORMAT_XRGB8888,
DRM_FORMAT_XBGR8888,
DRM_FORMAT_RGBX8888,
DRM_FORMAT_BGRX8888,
DRM_FORMAT_XRGB2101010,
DRM_FORMAT_XBGR2101010,
DRM_FORMAT_RGBX1010102,
DRM_FORMAT_BGRX1010102,
DRM_FORMAT_ARGB2101010,
DRM_FORMAT_ABGR2101010,
DRM_FORMAT_RGBA1010102,
DRM_FORMAT_BGRA1010102,
DRM_FORMAT_ARGB8888,
DRM_FORMAT_ABGR8888,
DRM_FORMAT_RGBA8888,
DRM_FORMAT_BGRA8888,
};
int adf_find_simple_post_configuration(struct adf_device *dev,
const __u32 *formats, size_t n_formats,
adf_id_t *interface, adf_id_t *overlay_engine)
{
adf_id_t *intfs = NULL;
ssize_t n_intfs = adf_interfaces(dev, &intfs);
if (n_intfs < 0)
return n_intfs;
else if (!n_intfs)
return -ENODEV;
adf_id_t *primary_intfs;
ssize_t n_primary_intfs = adf_interfaces_filter_by_flag(dev,
ADF_INTF_FLAG_PRIMARY, intfs, n_intfs, &primary_intfs);
free(intfs);
if (n_primary_intfs < 0)
return n_primary_intfs;
else if (!n_primary_intfs)
return -ENODEV;
if (!formats) {
formats = any_rgb_format;
n_formats = sizeof(any_rgb_format) / sizeof(any_rgb_format[0]);
}
bool found = false;
ssize_t i = 0;
for (i = 0; i < n_primary_intfs; i++) {
found = adf_find_simple_post_overlay_engine(dev, formats, n_formats,
primary_intfs[i], overlay_engine);
if (found) {
*interface = primary_intfs[i];
break;
}
}
free(primary_intfs);
if (!found)
return -ENODEV;
return 0;
}
|