forked from zillevdr/vdr-plugin-softhddevice-drm
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvideo_drm.c
3385 lines (2874 loc) · 97.4 KB
/
video_drm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
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
///
/// @file video.c @brief Video module
///
/// Copyright (c) 2009 - 2015 by Johns. All Rights Reserved.
/// Copyright (c) 2018 by zille. All Rights Reserved.
///
/// Contributor(s):
///
/// License: AGPLv3
///
/// This program is free software: you can redistribute it and/or modify
/// it under the terms of the GNU Affero General Public License as
/// published by the Free Software Foundation, either version 3 of the
/// License.
///
/// 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 Affero General Public License for more details.
///
//////////////////////////////////////////////////////////////////////////////
///
/// @defgroup Video The video module.
///
/// This module contains all video rendering functions.
///
#ifndef __USE_GNU
#define __USE_GNU
#endif
#include <stdbool.h>
#include <unistd.h>
#include <inttypes.h>
#include <libintl.h>
#define _(str) gettext(str) ///< gettext shortcut
#define _N(str) str ///< gettext_noop shortcut
#ifdef USE_GLES
#include <assert.h>
#endif
#include <pthread.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <sys/mman.h>
#include <drm_fourcc.h>
#include <libavcodec/avcodec.h>
#include <libavutil/hwcontext_drm.h>
#include <libavutil/pixdesc.h>
#include <libavfilter/buffersink.h>
#include <libavfilter/buffersrc.h>
#include <libavutil/opt.h>
#ifdef USE_GLES
#include <gbm.h>
#include "gles_private.h"
#endif
#include "misc.h"
#include "video.h"
#include "audio.h"
#include "codec.h"
#include "drm.h"
#include "buf2rgb.h"
//----------------------------------------------------------------------------
// Variables
//----------------------------------------------------------------------------
int VideoAudioDelay;
static int VideoDisplayWidth = 0;
static int VideoDisplayHeight = 0;
static uint32_t VideoDisplayRefresh = 0;
static pthread_cond_t WaitCleanCondition;
static pthread_mutex_t WaitCleanMutex;
static pthread_mutex_t TrickSpeedMutex;
static pthread_mutex_t PlaybackMutex;
static pthread_mutex_t VideoClockMutex;
static pthread_t DecodeThread; ///< video decode thread
static pthread_t FilterThread;
static pthread_t GrabbingThread;
static pthread_t DisplayThread;
static pthread_mutex_t DisplayQueue;
//----------------------------------------------------------------------------
// Helper functions
//----------------------------------------------------------------------------
static void ReleaseFrame( __attribute__ ((unused)) void *opaque, uint8_t *data)
{
AVDRMFrameDescriptor *primedata = (AVDRMFrameDescriptor *)data;
av_free(primedata);
}
static void ThreadExitHandler( __attribute__ ((unused)) void * arg)
{
FilterThread = 0;
}
int GetPropertyValue(int fd_drm, uint32_t objectID,
uint32_t objectType, const char *propName, uint64_t *value)
{
uint32_t i;
int found = 0;
drmModePropertyPtr Prop;
drmModeObjectPropertiesPtr objectProps =
drmModeObjectGetProperties(fd_drm, objectID, objectType);
for (i = 0; i < objectProps->count_props; i++) {
if ((Prop = drmModeGetProperty(fd_drm, objectProps->props[i])) == NULL)
Debug2(L_DRM, "GetPropertyValue: Unable to query property.");
if (strcmp(propName, Prop->name) == 0) {
*value = objectProps->prop_values[i];
found = 1;
}
drmModeFreeProperty(Prop);
if (found)
break;
}
drmModeFreeObjectProperties(objectProps);
if (!found) {
Debug2(L_DRM, "GetPropertyValue: Unable to find value for property \'%s\'.",
propName);
return -1;
}
return 0;
}
static int SetPlanePropertyRequest(drmModeAtomicReqPtr ModeReq, uint32_t objectID, const char *propName, uint64_t value)
{
VideoRender *render = (VideoRender *)GetVideoRender();
if (!render) {
Fatal("failed to get VideoRender");
}
struct plane *obj = NULL;
if (objectID == render->planes[VIDEO_PLANE]->plane_id)
obj = render->planes[VIDEO_PLANE];
else if (objectID == render->planes[OSD_PLANE]->plane_id)
obj = render->planes[OSD_PLANE];
if (!obj) {
Error("SetPlanePropertyRequest: Unable to find plane with id %d", objectID);
return -EINVAL;
}
uint32_t i;
int id = -1;
for (i = 0; i < obj->props->count_props; i++) {
if (strcmp(obj->props_info[i]->name, propName) == 0) {
id = obj->props_info[i]->prop_id;
break;
}
}
if (id < 0) {
Error("SetPlanePropertyRequest: Unable to find value for property \'%s\'.",
propName);
return -EINVAL;
}
return drmModeAtomicAddProperty(ModeReq, objectID, id, value);
}
static int SetPropertyRequest(drmModeAtomicReqPtr ModeReq, int fd_drm,
uint32_t objectID, uint32_t objectType,
const char *propName, uint64_t value)
{
uint32_t i;
uint64_t id = 0;
drmModePropertyPtr Prop;
drmModeObjectPropertiesPtr objectProps =
drmModeObjectGetProperties(fd_drm, objectID, objectType);
for (i = 0; i < objectProps->count_props; i++) {
if ((Prop = drmModeGetProperty(fd_drm, objectProps->props[i])) == NULL)
Debug2(L_DRM, "SetPropertyRequest: Unable to query property.");
if (strcmp(propName, Prop->name) == 0) {
id = Prop->prop_id;
drmModeFreeProperty(Prop);
break;
}
drmModeFreeProperty(Prop);
}
drmModeFreeObjectProperties(objectProps);
if (id == 0)
Debug2(L_DRM, "SetPropertyRequest: Unable to find value for property \'%s\'.",
propName);
return drmModeAtomicAddProperty(ModeReq, objectID, id, value);
}
void SetPlaneZpos(drmModeAtomicReqPtr ModeReq, struct plane *plane)
{
SetPlanePropertyRequest(ModeReq, plane->plane_id, "zpos", plane->properties.zpos);
}
void SetPlane(drmModeAtomicReqPtr ModeReq, struct plane *plane)
{
SetPlanePropertyRequest(ModeReq, plane->plane_id, "CRTC_ID", plane->properties.crtc_id);
SetPlanePropertyRequest(ModeReq, plane->plane_id, "FB_ID", plane->properties.fb_id);
SetPlanePropertyRequest(ModeReq, plane->plane_id, "CRTC_X", plane->properties.crtc_x);
SetPlanePropertyRequest(ModeReq, plane->plane_id, "CRTC_Y", plane->properties.crtc_y);
SetPlanePropertyRequest(ModeReq, plane->plane_id, "CRTC_W", plane->properties.crtc_w);
SetPlanePropertyRequest(ModeReq, plane->plane_id, "CRTC_H", plane->properties.crtc_h);
SetPlanePropertyRequest(ModeReq, plane->plane_id, "SRC_X", plane->properties.src_x);
SetPlanePropertyRequest(ModeReq, plane->plane_id, "SRC_Y", plane->properties.src_y);
SetPlanePropertyRequest(ModeReq, plane->plane_id, "SRC_W", plane->properties.src_w << 16);
SetPlanePropertyRequest(ModeReq, plane->plane_id, "SRC_H", plane->properties.src_h << 16);
}
void DumpPlaneProperties(struct plane *plane)
{
Info("DumpPlaneProperties (plane_id = %d):", plane->plane_id);
Info(" CRTC ID: %"PRIu64"", plane->properties.crtc_id);
Info(" FB ID : %"PRIu64"", plane->properties.fb_id);
Info(" CRTC X : %"PRIu64"", plane->properties.crtc_x);
Info(" CRTC Y : %"PRIu64"", plane->properties.crtc_y);
Info(" CRTC W : %"PRIu64"", plane->properties.crtc_w);
Info(" CRTC H : %"PRIu64"", plane->properties.crtc_h);
Info(" SRC X : %"PRIu64"", plane->properties.src_x);
Info(" SRC Y : %"PRIu64"", plane->properties.src_y);
Info(" SRC W : %"PRIu64"", plane->properties.src_w);
Info(" SRC H : %"PRIu64"", plane->properties.src_h);
Info(" ZPOS : %"PRIu64"", plane->properties.zpos);
}
size_t ReadLineFromFile(char *buf, size_t size, char * file)
{
FILE *fd = NULL;
size_t character;
fd = fopen(file, "r");
if (fd == NULL) {
Error("Can't open %s", file);
return 0;
}
character = getline(&buf, &size, fd);
fclose(fd);
return character;
}
void ReadHWPlatform(VideoRender * render)
{
char *txt_buf;
char *read_ptr;
size_t bufsize = 128;
size_t read_size;
txt_buf = (char *) calloc(bufsize, sizeof(char));
render->NoHwDeint = 0;
read_size = ReadLineFromFile(txt_buf, bufsize, "/sys/firmware/devicetree/base/compatible");
if (!read_size) {
free((void *)txt_buf);
return;
}
read_ptr = txt_buf;
while(read_size) {
if (strstr(read_ptr, "bcm2711")) {
Debug2(L_DRM, "ReadHWPlatform: bcm2711 found");
break;
}
if (strstr(read_ptr, "amlogic")) {
Debug2(L_DRM, "ReadHWPlatform: amlogic found, disable HW deinterlacer");
render->NoHwDeint = 1;
break;
}
read_size -= (strlen(read_ptr) + 1);
read_ptr = (char *)&read_ptr[(strlen(read_ptr) + 1)];
}
free((void *)txt_buf);
}
static int TestCaps(int fd)
{
uint64_t test;
if (drmGetCap(fd, DRM_CAP_DUMB_BUFFER, &test) < 0 || test == 0)
return 1;
if (drmSetClientCap(fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1) != 0)
return 1;
if (drmSetClientCap(fd, DRM_CLIENT_CAP_ATOMIC, 1) != 0)
return 1;
if (drmGetCap(fd, DRM_CAP_PRIME, &test) < 0)
return 1;
if (drmGetCap(fd, DRM_PRIME_CAP_EXPORT, &test) < 0)
return 1;
if (drmGetCap(fd, DRM_PRIME_CAP_IMPORT, &test) < 0)
return 1;
return 0;
}
static int CheckZpos(VideoRender * render, struct plane *plane, uint64_t zpos)
{
drmModeAtomicReqPtr ModeReq;
const uint32_t flags = DRM_MODE_ATOMIC_ALLOW_MODESET;
if (!(ModeReq = drmModeAtomicAlloc()))
Error("CheckZpos: cannot allocate atomic request (%d): %m", errno);
plane->properties.zpos = zpos;
SetPlaneZpos(ModeReq, plane);
if (drmModeAtomicCommit(render->fd_drm, ModeReq, flags, NULL) != 0) {
Debug2(L_DRM, "CheckZpos: cannot set atomic mode (%d), don't use zpos change: %m", errno);
render->use_zpos = 0;
drmModeAtomicFree(ModeReq);
return 1;
}
drmModeAtomicFree(ModeReq);
return 0;
}
#ifdef USE_GLES
static const EGLint context_attribute_list[] =
{
EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_NONE
};
EGLConfig get_config(void)
{
VideoRender *render = (VideoRender *)GetVideoRender();
if (!render) {
Fatal("failed to get VideoRender");
}
EGLint config_attribute_list[] = {
EGL_BUFFER_SIZE, 32,
EGL_STENCIL_SIZE, EGL_DONT_CARE,
EGL_DEPTH_SIZE, EGL_DONT_CARE,
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_NONE
};
EGLConfig *configs;
EGLint matched;
EGLint count;
EGL_CHECK(eglGetConfigs(render->eglDisplay, NULL, 0, &count));
if (count < 1) {
Fatal("no EGL configs to choose from");
}
Debug2(L_OPENGL, "%d EGL configs found", count);
configs = malloc(count * sizeof(*configs));
if (!configs)
Fatal("can't allocate space for EGL configs");
EGL_CHECK(eglChooseConfig(render->eglDisplay, config_attribute_list, configs, count, &matched));
if (!matched) {
Fatal("no EGL configs with appropriate attributes");
}
Debug2(L_OPENGL, "%d appropriate EGL configs found, which match attributes", matched);
for (int i = 0; i < matched; ++i) {
EGLint gbm_format;
EGL_CHECK(eglGetConfigAttrib(render->eglDisplay, configs[i], EGL_NATIVE_VISUAL_ID, &gbm_format));
if (gbm_format == GBM_FORMAT_ARGB8888)
return configs[i];
}
Fatal("no matching gbm config found");
}
PFNEGLGETPLATFORMDISPLAYEXTPROC get_platform_display = NULL;
PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC get_platform_surface = NULL;
#endif
static void get_properties(int fd, int plane_id, struct plane *plane)
{
uint32_t i;
plane->props = drmModeObjectGetProperties(fd, plane_id, DRM_MODE_OBJECT_PLANE);
if (!plane->props) {
Error("could not get %u properties: %s",
plane_id, strerror(errno));
return;
}
plane->props_info = calloc(plane->props->count_props, sizeof(*plane->props_info)); \
for (i = 0; i < plane->props->count_props; i++) {
plane->props_info[i] = drmModeGetProperty(fd, plane->props->props[i]);
}
}
static void free_properties(struct plane *plane)
{
if (!plane->props)
return;
if (!plane->props_info)
return;
for (uint32_t i = 0; i < plane->props->count_props; i++) {
if (plane->props_info[i]) {
drmModeFreeProperty(plane->props_info[i]);
}
}
drmModeFreeObjectProperties(plane->props);
free(plane->props_info);
}
void VideoSetDisplay(const char* resolution)
{
sscanf(resolution, "%dx%d@%d", &VideoDisplayWidth, &VideoDisplayHeight, &VideoDisplayRefresh);
}
static drmModeConnector *find_drm_connector(int fd, drmModeRes *resources)
{
drmModeConnector *connector = NULL;
int i;
for (i = 0; i < resources->count_connectors; i++) {
connector = drmModeGetConnector(fd, resources->connectors[i]);
if (connector && connector->connection == DRM_MODE_CONNECTED) {
break;
}
drmModeFreeConnector(connector);
connector = NULL;
}
return connector;
}
static int32_t find_crtc_for_encoder(const drmModeRes *resources, const drmModeEncoder *encoder)
{
int i;
for (i = 0; i < resources->count_crtcs; i++) {
const uint32_t crtc_mask = 1 << i;
const uint32_t crtc_id = resources->crtcs[i];
if (encoder->possible_crtcs & crtc_mask) {
return crtc_id;
}
}
return -1;
}
static int get_resources(int fd, drmModeRes **resources)
{
*resources = drmModeGetResources(fd);
if (*resources == NULL) {
Error("FindDevice: cannot retrieve DRM resources (%d): %m", errno);
return -1;
}
return 0;
}
#define MAX_DRM_DEVICES 64
static int find_drm_device(drmModeRes **resources)
{
drmDevicePtr devices[MAX_DRM_DEVICES] = { NULL };
int num_devices, fd = -1;
num_devices = drmGetDevices2(0, devices,MAX_DRM_DEVICES);
if (num_devices < 0) {
Error("FindDevice: drmGetDevices2 failed: %s", strerror(-num_devices));
return fd;
}
for (int i = 0; i < num_devices && fd < 0; i++) {
drmDevicePtr device = devices[i];
int ret;
if (!(device->available_nodes & (1 << DRM_NODE_PRIMARY)))
continue;
fd = open(device->nodes[DRM_NODE_PRIMARY], O_RDWR);
if (fd < 0)
continue;
if (TestCaps(fd)) {
close(fd);
fd = -1;
continue;
}
ret = get_resources(fd, resources);
if (!ret)
break;
close(fd);
fd = -1;
}
drmFreeDevices(devices, num_devices);
if (fd < 0)
Error("FindDevice: no drm device found!");
return fd;
}
static int32_t find_crtc_for_connector(VideoRender *render, const drmModeRes *resources, const drmModeConnector *connector)
{
int i;
for (i = 0; i < connector->count_encoders; i++) {
const uint32_t encoder_id = connector->encoders[i];
drmModeEncoder *encoder = drmModeGetEncoder(render->fd_drm, encoder_id);
if (encoder) {
const int32_t crtc_id = find_crtc_for_encoder(resources, encoder);
drmModeFreeEncoder(encoder);
if (crtc_id != 0) {
return crtc_id;
}
}
}
return -1;
}
static int init_gbm(VideoRender *render, int w, int h, uint32_t format, uint64_t modifier)
{
render->gbm_device = gbm_create_device(render->fd_drm);
if (!render->gbm_device) {
Error("FindDevice: failed to create gbm device!");
return -1;
}
render->gbm_surface = gbm_surface_create(render->gbm_device, w, h, format, modifier);
if (!render->gbm_surface) {
Error("FindDevice: failed to create %d x %d surface bo", w, h);
return -1;
}
return 0;
}
#ifdef USE_GLES
static int init_egl(VideoRender *render)
{
EGLint iMajorVersion, iMinorVersion;
PFNEGLGETPLATFORMDISPLAYEXTPROC get_platform_display = (PFNEGLGETPLATFORMDISPLAYEXTPROC)eglGetProcAddress("eglGetPlatformDisplayEXT");
assert(get_platform_display != NULL);
PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC get_platform_surface = (PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC)eglGetProcAddress("eglCreatePlatformWindowSurfaceEXT");
assert(get_platform_surface != NULL);
EGL_CHECK(render->eglDisplay = get_platform_display(EGL_PLATFORM_GBM_KHR, render->gbm_device, NULL));
if (!render->eglDisplay) {
Error("FindDevice: failed to get eglDisplay");
return -1;
}
if (!eglInitialize(render->eglDisplay, &iMajorVersion, &iMinorVersion)) {
Error("FindDevice: eglInitialize failed");
return -1;
}
Debug2(L_OPENGL, "FindDevice: Using display %p with EGL version %d.%d", render->eglDisplay, iMajorVersion, iMinorVersion);
EGL_CHECK(Debug2(L_OPENGL, " EGL Version: \"%s\"", eglQueryString(render->eglDisplay, EGL_VERSION)));
EGL_CHECK(Debug2(L_OPENGL, " EGL Vendor: \"%s\"", eglQueryString(render->eglDisplay, EGL_VENDOR)));
EGL_CHECK(Debug2(L_OPENGL, " EGL Extensions: \"%s\"", eglQueryString(render->eglDisplay, EGL_EXTENSIONS)));
EGL_CHECK(Debug2(L_OPENGL, " EGL APIs: \"%s\"", eglQueryString(render->eglDisplay, EGL_CLIENT_APIS)));
EGLConfig eglConfig = get_config();
EGL_CHECK(eglBindAPI(EGL_OPENGL_ES_API));
EGL_CHECK(render->eglContext = eglCreateContext(render->eglDisplay, eglConfig, EGL_NO_CONTEXT, context_attribute_list));
if (!render->eglContext) {
Error("FindDevice: failed to create eglContext");
return -1;
}
EGL_CHECK(render->eglSurface = get_platform_surface(render->eglDisplay, eglConfig, render->gbm_surface, NULL));
if (render->eglSurface == EGL_NO_SURFACE) {
Error("FindDevice: failed to create eglSurface");
return -1;
}
EGLint s_width, s_height;
EGL_CHECK(eglQuerySurface(render->eglDisplay, render->eglSurface, EGL_WIDTH, &s_width));
EGL_CHECK(eglQuerySurface(render->eglDisplay, render->eglSurface, EGL_HEIGHT, &s_height));
Debug2(L_OPENGL, "FindDevice: EGLSurface %p on EGLDisplay %p for %d x %d BO created", render->eglSurface, render->eglDisplay, s_width, s_height);
render->GlInit = 1;
Info("EGL context initialized");
return 0;
}
#endif
static int FindDevice(VideoRender * render)
{
drmModeRes *resources;
drmModeConnector *connector;
drmModeEncoder *encoder = NULL;
drmModeModeInfo *mode = NULL;
drmModePlane *plane;
drmModePlaneRes *plane_res;
int i;
uint32_t j, k;
// find a drm device
render->fd_drm = find_drm_device(&resources);
if (render->fd_drm < 0) {
Error("FindDevice: Could not open device!");
return -1;
}
Debug2(L_DRM, "FindDevice: DRM have %i connectors, %i crtcs, %i encoders",
resources->count_connectors, resources->count_crtcs,
resources->count_encoders);
// find a connected connector
connector = find_drm_connector(render->fd_drm, resources);
if (!connector) {
Error("FindDevice: cannot retrieve DRM connector (%d): %m", errno);
return -errno;
}
render->connector_id = connector->connector_id;
// find a user requested mode
if (VideoDisplayWidth) {
for (i = 0; i < connector->count_modes; i++) {
drmModeModeInfo *current_mode = &connector->modes[i];
if(current_mode->hdisplay == VideoDisplayWidth && current_mode->vdisplay == VideoDisplayHeight &&
current_mode->vrefresh == VideoDisplayRefresh && !(current_mode->flags & DRM_MODE_FLAG_INTERLACE)) {
mode = current_mode;
Debug2(L_DRM, "FindDevice: Use user requested mode: %dx%d@%d", mode->hdisplay, mode->vdisplay, mode->vrefresh);
break;
}
}
if (!mode)
Warning("FindDevice: User requested mode not found, try default modes");
}
uint32_t preferred_hz[3] = {50, 60, 0};
// find the highest resolution mode with 50, 60 or any refresh rate
if (!mode) {
j = 0;
int area;
find_mode:
for (i = 0, area = 0; i < connector->count_modes; i++) {
drmModeModeInfo *current_mode = &connector->modes[i];
if (preferred_hz[j] && current_mode->vrefresh != preferred_hz[j])
continue;
int current_area = current_mode->hdisplay * current_mode->vdisplay;
if (current_area > area) {
mode = current_mode;
area = current_area;
}
}
if (!mode && preferred_hz[j]) {
j++;
goto find_mode;
}
if (mode)
Debug2(L_DRM, "FindDevice: Use mode with the highest resolution: %dx%d@%d",
mode->hdisplay, mode->vdisplay, mode->vrefresh);
}
if (!mode) {
Error("FindDevice: No monitor mode found! Give up!");
return -1;
}
memcpy(&render->mode, mode, sizeof(drmModeModeInfo));
// find encoder
for (i = 0; i < resources->count_encoders; i++) {
encoder = drmModeGetEncoder(render->fd_drm, resources->encoders[i]);
if (encoder->encoder_id == connector->encoder_id)
break;
drmModeFreeEncoder(encoder);
encoder = NULL;
}
if (encoder) {
render->crtc_id = encoder->crtc_id;
Debug2(L_DRM, "FindDevice: have encoder, render->crtc_id %d", render->crtc_id);
} else {
int32_t crtc_id = find_crtc_for_connector(render, resources, connector);
if (crtc_id == -1) {
Error("FindDevice: No crtc found!");
return -errno;
}
render->crtc_id = crtc_id;
Debug2(L_DRM, "FindDevice: have no encoder, render->crtc_id %d", render->crtc_id);
}
for (i = 0; i < resources->count_crtcs; i++) {
if (resources->crtcs[i] == render->crtc_id) {
render->crtc_index = i;
break;
}
}
Info("FindDevice: Using Monitor Mode %dx%d@%d, crtc_id %d crtc_idx %d",
render->mode.hdisplay, render->mode.vdisplay, render->mode.vrefresh, render->crtc_id, render->crtc_index);
drmModeFreeConnector(connector);
// find planes
if ((plane_res = drmModeGetPlaneResources(render->fd_drm)) == NULL) {
Error("FindDevice: cannot retrieve PlaneResources (%d): %m", errno);
return -1;
}
// allocate local plane structs
for (i = 0; i < MAX_PLANES; i++) {
render->planes[i] = calloc(1, sizeof(struct plane));
}
// test and list the local plane structs
struct plane best_primary_video_plane = { .plane_id = 0}; // is the NV12 capable primary plane with the lowest plane_id
struct plane best_overlay_video_plane = { .plane_id = 0}; // is the NV12 capable overlay plane with the lowest plane_id
struct plane best_primary_osd_plane = { .plane_id = 0}; // is the AR24 capable primary plane with the highest plane_id
struct plane best_overlay_osd_plane = { .plane_id = 0}; // is the AR24 capable overlay plane with the highest plane_id
for (j = 0; j < plane_res->count_planes; j++) {
plane = drmModeGetPlane(render->fd_drm, plane_res->planes[j]);
if (plane == NULL) {
Error("FindDevice: cannot query DRM-KMS plane %d", j);
continue;
}
uint64_t type;
uint64_t zpos;
char pixelformats[256];
if (plane->possible_crtcs & (1 << render->crtc_index)) {
if (GetPropertyValue(render->fd_drm, plane_res->planes[j],
DRM_MODE_OBJECT_PLANE, "type", &type)) {
Debug2(L_DRM, "FindDevice: Failed to get property 'type'");
}
if (GetPropertyValue(render->fd_drm, plane_res->planes[j],
DRM_MODE_OBJECT_PLANE, "zpos", &zpos)) {
Debug2(L_DRM, "FindDevice: Failed to get property 'zpos'");
} else {
render->use_zpos = 1;
}
Debug2(L_DRM, "FindDevice: %s: id %i possible_crtcs %i",
(type == DRM_PLANE_TYPE_PRIMARY) ? "PRIMARY " :
(type == DRM_PLANE_TYPE_OVERLAY) ? "OVERLAY " :
(type == DRM_PLANE_TYPE_CURSOR) ? "CURSOR " : "UNKNOWN",
plane->plane_id, plane->possible_crtcs);
strcpy(pixelformats, " ");
// test pixel format and plane caps
for (k = 0; k < plane->count_formats; k++) {
if (encoder->possible_crtcs & plane->possible_crtcs) {
char tmp[10];
switch (plane->formats[k]) {
case DRM_FORMAT_NV12:
snprintf(tmp, sizeof(tmp), " %4.4s", (char *)&plane->formats[k]);
strcat(pixelformats, tmp);
if (type == DRM_PLANE_TYPE_PRIMARY && !best_primary_video_plane.plane_id) {
best_primary_video_plane.plane_id = plane->plane_id;
best_primary_video_plane.type = type;
best_primary_video_plane.properties.zpos = zpos;
strcat(pixelformats, "! ");
}
if (type == DRM_PLANE_TYPE_OVERLAY && !best_overlay_video_plane.plane_id) {
best_overlay_video_plane.plane_id = plane->plane_id;
best_overlay_video_plane.type = type;
best_overlay_video_plane.properties.zpos = zpos;
strcat(pixelformats, "! ");
}
break;
case DRM_FORMAT_ARGB8888:
snprintf(tmp, sizeof(tmp), " %4.4s", (char *)&plane->formats[k]);
strcat(pixelformats, tmp);
if (type == DRM_PLANE_TYPE_PRIMARY) {
best_primary_osd_plane.plane_id = plane->plane_id;
best_primary_osd_plane.type = type;
best_primary_osd_plane.properties.zpos = zpos;
strcat(pixelformats, "! ");
}
if (type == DRM_PLANE_TYPE_OVERLAY) {
best_overlay_osd_plane.plane_id = plane->plane_id;
best_overlay_osd_plane.type = type;
best_overlay_osd_plane.properties.zpos = zpos;
strcat(pixelformats, "! ");
}
break;
default:
break;
}
}
}
Debug2(L_DRM, "%s", pixelformats);
}
drmModeFreePlane(plane);
}
// debug output
if (best_primary_video_plane.plane_id) {
Debug2(L_DRM, "FindDevice: best_primary_video_plane: plane_id %d, type %s, zpos %"PRIu64"",
best_primary_video_plane.plane_id, best_primary_video_plane.type == DRM_PLANE_TYPE_PRIMARY ? "PRIMARY" : "OVERLAY", best_primary_video_plane.properties.zpos);
}
if (best_overlay_video_plane.plane_id) {
Debug2(L_DRM, "FindDevice: best_overlay_video_plane: plane_id %d, type %s, zpos %"PRIu64"",
best_overlay_video_plane.plane_id, best_overlay_video_plane.type == DRM_PLANE_TYPE_PRIMARY ? "PRIMARY" : "OVERLAY", best_overlay_video_plane.properties.zpos);
}
if (best_primary_osd_plane.plane_id) {
Debug2(L_DRM, "FindDevice: best_primary_osd_plane: plane_id %d, type %s, zpos %"PRIu64"",
best_primary_osd_plane.plane_id, best_primary_osd_plane.type == DRM_PLANE_TYPE_PRIMARY ? "PRIMARY" : "OVERLAY", best_primary_osd_plane.properties.zpos);
}
if (best_overlay_osd_plane.plane_id) {
Debug2(L_DRM, "FindDevice: best_overlay_osd_plane: plane_id %d, type %s, zpos %"PRIu64"",
best_overlay_osd_plane.plane_id, best_overlay_osd_plane.type == DRM_PLANE_TYPE_PRIMARY ? "PRIMARY" : "OVERLAY", best_overlay_osd_plane.properties.zpos);
}
// See which planes we should use
if (best_primary_video_plane.plane_id && best_overlay_osd_plane.plane_id) {
render->planes[VIDEO_PLANE]->plane_id = best_primary_video_plane.plane_id;
render->planes[VIDEO_PLANE]->type = best_primary_video_plane.type;
render->planes[VIDEO_PLANE]->properties.zpos = render->zpos_primary = best_primary_video_plane.properties.zpos;
render->planes[OSD_PLANE]->plane_id = best_overlay_osd_plane.plane_id;
render->planes[OSD_PLANE]->type = best_overlay_osd_plane.type;
render->planes[OSD_PLANE]->properties.zpos = render->zpos_overlay = best_overlay_osd_plane.properties.zpos;
} else if (best_overlay_video_plane.plane_id && best_primary_osd_plane.plane_id) {
render->planes[VIDEO_PLANE]->plane_id = best_overlay_video_plane.plane_id;
render->planes[VIDEO_PLANE]->type = best_overlay_video_plane.type;
render->planes[VIDEO_PLANE]->properties.zpos = render->zpos_overlay = best_overlay_video_plane.properties.zpos;
render->planes[OSD_PLANE]->plane_id = best_primary_osd_plane.plane_id;
render->planes[OSD_PLANE]->type = best_primary_osd_plane.type;
render->planes[OSD_PLANE]->properties.zpos = render->zpos_primary = best_primary_osd_plane.properties.zpos;
render->use_zpos = 1;
} else {
Error("FindDevice: No suitable planes found!");
return -1;
}
// fill the plane's properties to speed up SetPropertyRequest later
get_properties(render->fd_drm, render->planes[VIDEO_PLANE]->plane_id, render->planes[VIDEO_PLANE]);
get_properties(render->fd_drm, render->planes[OSD_PLANE]->plane_id, render->planes[OSD_PLANE]);
// Check, if we can set z-order (meson and rpi have fixed z-order, which cannot be changed)
if (render->use_zpos && CheckZpos(render, render->planes[VIDEO_PLANE], render->planes[VIDEO_PLANE]->properties.zpos)) {
render->use_zpos = 0;
}
if (render->use_zpos && CheckZpos(render, render->planes[OSD_PLANE], render->planes[OSD_PLANE]->properties.zpos)) {
render->use_zpos = 0;
}
// render->use_zpos was set, if Video is on OVERLAY, and Osd is on PRIMARY
// Check if the OVERLAY plane really got a higher zpos than the PRIMARY plane
// If not, change their zpos values or hardcode them to
// 1 OVERLAY (Video)
// 0 PRIMARY (Osd)
if (render->use_zpos && render->zpos_overlay <= render->zpos_primary) {
char str_zpos[256];
strcpy(str_zpos, "FindDevice: zpos values are wrong, so ");
if (render->zpos_overlay == render->zpos_primary) {
// is this possible?
strcat(str_zpos, "hardcode them to 0 and 1, because they are equal");
render->zpos_primary = 0;
render->zpos_overlay = 1;
} else {
strcat(str_zpos, "switch them");
uint64_t zpos_tmp = render->zpos_primary;
render->zpos_primary = render->zpos_overlay;
render->zpos_overlay = zpos_tmp;
}
Debug2(L_DRM, "%s", str_zpos);
}
drmModeFreePlaneResources(plane_res);
drmModeFreeEncoder(encoder);
drmModeFreeResources(resources);
Info("FindDevice: DRM setup - CRTC: %i video_plane: %i (%s %"PRIu64") osd_plane: %i (%s %"PRIu64") use_zpos: %d",
render->crtc_id, render->planes[VIDEO_PLANE]->plane_id,
render->planes[VIDEO_PLANE]->type == DRM_PLANE_TYPE_PRIMARY ? "PRIMARY" : "OVERLAY",
render->planes[VIDEO_PLANE]->properties.zpos,
render->planes[OSD_PLANE]->plane_id,
render->planes[OSD_PLANE]->type == DRM_PLANE_TYPE_PRIMARY ? "PRIMARY" : "OVERLAY",
render->planes[OSD_PLANE]->properties.zpos,
render->use_zpos);
#ifdef USE_GLES
if (DisableOglOsd)
return 0;
// init gbm
int w, h;
double pixel_aspect;
GetScreenSize(&w, &h, &pixel_aspect);
if (init_gbm(render, w, h, DRM_FORMAT_ARGB8888, GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING)) {
Error("FindDevice: failed to init gbm device and surface!");
return -1;
}
// init egl
if (init_egl(render)) {
Error("FindDevice: failed to init egl!");
return -1;
}
#endif
return 0;
}
#ifdef USE_GLES
static void drm_fb_destroy_callback(struct gbm_bo *bo, void *data)
{
int drm_fd = gbm_device_get_fd(gbm_bo_get_device(bo));
struct drm_buf *buf = data;
if (buf->fb_id)
drmModeRmFB(drm_fd, buf->fb_id);
free(buf);
}
__attribute__ ((weak)) union gbm_bo_handle
gbm_bo_get_handle_for_plane(struct gbm_bo *bo, int plane);
__attribute__ ((weak)) int
gbm_bo_get_fd(struct gbm_bo *bo);
__attribute__ ((weak)) uint64_t
gbm_bo_get_modifier(struct gbm_bo *bo);
__attribute__ ((weak)) int
gbm_bo_get_plane_count(struct gbm_bo *bo);
__attribute__ ((weak)) uint32_t
gbm_bo_get_stride_for_plane(struct gbm_bo *bo, int plane);
__attribute__ ((weak)) uint32_t
gbm_bo_get_offset(struct gbm_bo *bo, int plane);
struct drm_buf *drm_get_buf_from_bo(VideoRender *render, struct gbm_bo *bo)
{
struct drm_buf *buf = gbm_bo_get_user_data(bo);
uint32_t mod_flags = 0;
int ret = -1;
// the buffer was already allocated
if (buf)
return buf;
buf = calloc(1, sizeof *buf);
buf->bo = bo;
buf->width = gbm_bo_get_width(bo);
buf->height = gbm_bo_get_height(bo);
buf->pix_fmt = gbm_bo_get_format(bo);
if (gbm_bo_get_handle_for_plane && gbm_bo_get_modifier &&
gbm_bo_get_plane_count && gbm_bo_get_stride_for_plane &&
gbm_bo_get_offset) {
uint64_t modifiers[4] = {0};
modifiers[0] = gbm_bo_get_modifier(bo);
const int num_planes = gbm_bo_get_plane_count(bo);
buf->num_planes = num_planes;
for (int i = 0; i < num_planes; i++) {
buf->handle[i] = gbm_bo_get_handle_for_plane(bo, i).u32;