-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathkms-glsl.h
2802 lines (2312 loc) · 76.3 KB
/
kms-glsl.h
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
// https://qiita.com/Pctg-x8/items/52c7e018556ec5c867de
// drm-common.h
/*
* Copyright (c) 2017 Rob Clark <rclark@redhat.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, sub license,
* 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 (including the
* next paragraph) 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 NON-INFRINGEMENT. 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.
*/
#ifndef _DRM_COMMON_H
#define _DRM_COMMON_H
#include <xf86drm.h>
#include <xf86drmMode.h>
struct gbm;
struct egl;
struct plane {
drmModePlane *plane;
drmModeObjectProperties *props;
drmModePropertyRes **props_info;
};
struct crtc {
drmModeCrtc *crtc;
drmModeObjectProperties *props;
drmModePropertyRes **props_info;
};
struct connector {
drmModeConnector *connector;
drmModeObjectProperties *props;
drmModePropertyRes **props_info;
};
struct drm {
int fd;
/* only used for atomic: */
struct plane *plane;
struct crtc *crtc;
drmModeCrtc *orig_crtc;
struct connector *connector;
int crtc_index;
int kms_in_fence_fd;
int kms_out_fence_fd;
drmModeModeInfo *mode;
uint32_t crtc_id;
uint32_t connector_id;
/* number of frames to run for: */
unsigned int count;
int (*run)(const struct gbm *gbm, const struct egl *egl);
int (*draw)(const struct gbm *gbm, const struct egl *egl);
};
struct drm_fb {
struct gbm_bo *bo;
uint32_t fb_id;
};
struct drm_fb * drm_fb_get_from_bo(struct gbm_bo *bo);
int init_drm(struct drm *drm, const char *device, const char *mode_str, unsigned int vrefresh, unsigned int count);
const struct drm * init_drm_legacy(const char *device, const char *mode_str, unsigned int vrefresh, unsigned int count);
const struct drm * init_drm_atomic(const char *device, const char *mode_str, unsigned int vrefresh, unsigned int count);
#endif /* _DRM_COMMON_H */
// common.h
/*
* Copyright (c) 2017 Rob Clark <rclark@redhat.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, sub license,
* 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 (including the
* next paragraph) 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 NON-INFRINGEMENT. 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.
*/
#ifndef _COMMON_H
#define _COMMON_H
#ifndef GL_ES_VERSION_2_0
#include <GLES2/gl2.h>
#endif
#include <GLES2/gl2ext.h>
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <gbm.h>
#include <drm_fourcc.h>
#include <stdbool.h>
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
/* from mesa's util/macros.h: */
#define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
#define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
#define MIN3( A, B, C ) ((A) < (B) ? MIN2(A, C) : MIN2(B, C))
#define MAX3( A, B, C ) ((A) > (B) ? MAX2(A, C) : MAX2(B, C))
static inline unsigned
u_minify(unsigned value, unsigned levels)
{
return MAX2(1, value >> levels);
}
#ifndef DRM_FORMAT_MOD_LINEAR
#define DRM_FORMAT_MOD_LINEAR 0
#endif
#ifndef DRM_FORMAT_MOD_INVALID
#define DRM_FORMAT_MOD_INVALID ((((__u64)0) << 56) | ((1ULL << 56) - 1))
#endif
#ifndef EGL_KHR_platform_gbm
#define EGL_KHR_platform_gbm 1
#define EGL_PLATFORM_GBM_KHR 0x31D7
#endif /* EGL_KHR_platform_gbm */
#ifndef EGL_EXT_platform_base
#define EGL_EXT_platform_base 1
typedef EGLDisplay (EGLAPIENTRYP PFNEGLGETPLATFORMDISPLAYEXTPROC) (EGLenum platform, void *native_display, const EGLint *attrib_list);
typedef EGLSurface (EGLAPIENTRYP PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC) (EGLDisplay dpy, EGLConfig config, void *native_window, const EGLint *attrib_list);
typedef EGLSurface (EGLAPIENTRYP PFNEGLCREATEPLATFORMPIXMAPSURFACEEXTPROC) (EGLDisplay dpy, EGLConfig config, void *native_pixmap, const EGLint *attrib_list);
#ifdef EGL_EGLEXT_PROTOTYPES
EGLAPI EGLDisplay EGLAPIENTRY eglGetPlatformDisplayEXT (EGLenum platform, void *native_display, const EGLint *attrib_list);
EGLAPI EGLSurface EGLAPIENTRY eglCreatePlatformWindowSurfaceEXT (EGLDisplay dpy, EGLConfig config, void *native_window, const EGLint *attrib_list);
EGLAPI EGLSurface EGLAPIENTRY eglCreatePlatformPixmapSurfaceEXT (EGLDisplay dpy, EGLConfig config, void *native_pixmap, const EGLint *attrib_list);
#endif
#endif /* EGL_EXT_platform_base */
#ifndef EGL_VERSION_1_5
#define EGLImage EGLImageKHR
#endif /* EGL_VERSION_1_5 */
#define WEAK __attribute__((weak))
/* Define tokens from EGL_EXT_image_dma_buf_import_modifiers */
#ifndef EGL_EXT_image_dma_buf_import_modifiers
#define EGL_EXT_image_dma_buf_import_modifiers 1
#define EGL_DMA_BUF_PLANE3_FD_EXT 0x3440
#define EGL_DMA_BUF_PLANE3_OFFSET_EXT 0x3441
#define EGL_DMA_BUF_PLANE3_PITCH_EXT 0x3442
#define EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT 0x3443
#define EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT 0x3444
#define EGL_DMA_BUF_PLANE1_MODIFIER_LO_EXT 0x3445
#define EGL_DMA_BUF_PLANE1_MODIFIER_HI_EXT 0x3446
#define EGL_DMA_BUF_PLANE2_MODIFIER_LO_EXT 0x3447
#define EGL_DMA_BUF_PLANE2_MODIFIER_HI_EXT 0x3448
#define EGL_DMA_BUF_PLANE3_MODIFIER_LO_EXT 0x3449
#define EGL_DMA_BUF_PLANE3_MODIFIER_HI_EXT 0x344A
#endif
#define NUM_BUFFERS 2
struct gbm {
struct gbm_device *dev;
struct gbm_surface *surface;
struct gbm_bo *bos[NUM_BUFFERS]; /* for the surfaceless case */
uint32_t format;
int width, height;
};
const struct gbm * init_gbm(int drm_fd, int w, int h, uint32_t format, uint64_t modifier, bool surfaceless);
struct framebuffer {
EGLImageKHR image;
GLuint tex;
GLuint fb;
};
struct egl {
EGLDisplay display;
EGLConfig config;
EGLContext context;
EGLSurface surface;
struct framebuffer fbs[NUM_BUFFERS]; /* for the surfaceless case */
PFNEGLGETPLATFORMDISPLAYEXTPROC eglGetPlatformDisplayEXT;
PFNEGLCREATEIMAGEKHRPROC eglCreateImageKHR;
PFNEGLDESTROYIMAGEKHRPROC eglDestroyImageKHR;
PFNGLEGLIMAGETARGETTEXTURE2DOESPROC glEGLImageTargetTexture2DOES;
PFNEGLCREATESYNCKHRPROC eglCreateSyncKHR;
PFNEGLDESTROYSYNCKHRPROC eglDestroySyncKHR;
PFNEGLWAITSYNCKHRPROC eglWaitSyncKHR;
PFNEGLCLIENTWAITSYNCKHRPROC eglClientWaitSyncKHR;
PFNEGLDUPNATIVEFENCEFDANDROIDPROC eglDupNativeFenceFDANDROID;
/* AMD_performance_monitor */
PFNGLGETPERFMONITORGROUPSAMDPROC glGetPerfMonitorGroupsAMD;
PFNGLGETPERFMONITORCOUNTERSAMDPROC glGetPerfMonitorCountersAMD;
PFNGLGETPERFMONITORGROUPSTRINGAMDPROC glGetPerfMonitorGroupStringAMD;
PFNGLGETPERFMONITORCOUNTERSTRINGAMDPROC glGetPerfMonitorCounterStringAMD;
PFNGLGETPERFMONITORCOUNTERINFOAMDPROC glGetPerfMonitorCounterInfoAMD;
PFNGLGENPERFMONITORSAMDPROC glGenPerfMonitorsAMD;
PFNGLDELETEPERFMONITORSAMDPROC glDeletePerfMonitorsAMD;
PFNGLSELECTPERFMONITORCOUNTERSAMDPROC glSelectPerfMonitorCountersAMD;
PFNGLBEGINPERFMONITORAMDPROC glBeginPerfMonitorAMD;
PFNGLENDPERFMONITORAMDPROC glEndPerfMonitorAMD;
PFNGLGETPERFMONITORCOUNTERDATAAMDPROC glGetPerfMonitorCounterDataAMD;
bool modifiers_supported;
void (*draw)(uint64_t start_time, unsigned frame);
};
static inline int __egl_check(void *ptr, const char *name)
{
if (!ptr) {
printf("no %s\n", name);
return -1;
}
return 0;
}
#define egl_check(egl, name) __egl_check((egl)->name, #name)
const struct egl * init_egl(const struct gbm *gbm);
int create_program(const char *vs_src, const char *fs_src);
int link_program(unsigned program);
int init_shadertoy(const struct gbm *gbm, struct egl *egl, const char *shadertoy);
void init_perfcntrs(const struct egl *egl, const char *perfcntrs);
void start_perfcntrs(void);
void end_perfcntrs(void);
void finish_perfcntrs(void);
void dump_perfcntrs(unsigned nframes, uint64_t elapsed_time_ns);
#define NSEC_PER_SEC (INT64_C(1000) * USEC_PER_SEC)
#define USEC_PER_SEC (INT64_C(1000) * MSEC_PER_SEC)
#define MSEC_PER_SEC INT64_C(1000)
uint64_t get_time_ns(void);
#endif /* _COMMON_H */
// drm-atomic.c
/*
* Copyright (c) 2017 Rob Clark <rclark@redhat.com>
* Copyright (c) 2020 Antonin Stefanutti <antonin.stefanutti@gmail.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, sub license,
* 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 (including the
* next paragraph) 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 NON-INFRINGEMENT. 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.
*/
#include <assert.h>
#include <errno.h>
#include <poll.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
// #include "common.h"
// #include "drm-common.h"
#define VOID2U64(x) ((uint64_t)(unsigned long)(x))
static struct drm _drm = {
.kms_out_fence_fd = -1,
};
static int add_connector_property(drmModeAtomicReq *req, uint32_t obj_id,
const char *name, uint64_t value)
{
struct connector *obj = _drm.connector;
unsigned int i;
int prop_id = 0;
for (i = 0 ; i < obj->props->count_props ; i++) {
if (strcmp(obj->props_info[i]->name, name) == 0) {
prop_id = obj->props_info[i]->prop_id;
break;
}
}
if (prop_id < 0) {
printf("no connector property: %s\n", name);
return -EINVAL;
}
return drmModeAtomicAddProperty(req, obj_id, prop_id, value);
}
static int add_crtc_property(drmModeAtomicReq *req, uint32_t obj_id,
const char *name, uint64_t value)
{
struct crtc *obj = _drm.crtc;
unsigned int i;
int prop_id = -1;
for (i = 0 ; i < obj->props->count_props ; i++) {
if (strcmp(obj->props_info[i]->name, name) == 0) {
prop_id = obj->props_info[i]->prop_id;
break;
}
}
if (prop_id < 0) {
printf("no crtc property: %s\n", name);
return -EINVAL;
}
return drmModeAtomicAddProperty(req, obj_id, prop_id, value);
}
static int add_plane_property(drmModeAtomicReq *req, uint32_t obj_id,
const char *name, uint64_t value)
{
struct plane *obj = _drm.plane;
unsigned int i;
int prop_id = -1;
for (i = 0 ; i < obj->props->count_props ; i++) {
if (strcmp(obj->props_info[i]->name, name) == 0) {
prop_id = obj->props_info[i]->prop_id;
break;
}
}
if (prop_id < 0) {
printf("no plane property: %s\n", name);
return -EINVAL;
}
return drmModeAtomicAddProperty(req, obj_id, prop_id, value);
}
static int drm_atomic_commit(uint32_t fb_id, uint32_t flags)
{
drmModeAtomicReq *req;
uint32_t plane_id = _drm.plane->plane->plane_id;
uint32_t blob_id;
int ret;
req = drmModeAtomicAlloc();
if (flags & DRM_MODE_ATOMIC_ALLOW_MODESET) {
if (add_connector_property(req, _drm.connector_id, "CRTC_ID",
_drm.crtc_id) < 0)
return -1;
if (drmModeCreatePropertyBlob(_drm.fd, _drm.mode, sizeof(*_drm.mode),
&blob_id) != 0)
return -1;
if (add_crtc_property(req, _drm.crtc_id, "MODE_ID", blob_id) < 0)
return -1;
if (add_crtc_property(req, _drm.crtc_id, "ACTIVE", 1) < 0)
return -1;
}
add_plane_property(req, plane_id, "FB_ID", fb_id);
add_plane_property(req, plane_id, "CRTC_ID", _drm.crtc_id);
add_plane_property(req, plane_id, "SRC_X", 0);
add_plane_property(req, plane_id, "SRC_Y", 0);
add_plane_property(req, plane_id, "SRC_W", _drm.mode->hdisplay << 16);
add_plane_property(req, plane_id, "SRC_H", _drm.mode->vdisplay << 16);
add_plane_property(req, plane_id, "CRTC_X", 0);
add_plane_property(req, plane_id, "CRTC_Y", 0);
add_plane_property(req, plane_id, "CRTC_W", _drm.mode->hdisplay);
add_plane_property(req, plane_id, "CRTC_H", _drm.mode->vdisplay);
if (_drm.kms_in_fence_fd != -1) {
add_crtc_property(req, _drm.crtc_id, "OUT_FENCE_PTR",
VOID2U64(&_drm.kms_out_fence_fd));
add_plane_property(req, plane_id, "IN_FENCE_FD", _drm.kms_in_fence_fd);
}
ret = drmModeAtomicCommit(_drm.fd, req, flags, NULL);
if (ret)
goto out;
if (_drm.kms_in_fence_fd != -1) {
close(_drm.kms_in_fence_fd);
_drm.kms_in_fence_fd = -1;
}
out:
drmModeAtomicFree(req);
return ret;
}
static EGLSyncKHR create_fence(const struct egl *egl, int fd)
{
EGLint attrib_list[] = {
EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fd,
EGL_NONE,
};
EGLSyncKHR fence = egl->eglCreateSyncKHR(egl->display,
EGL_SYNC_NATIVE_FENCE_ANDROID, attrib_list);
assert(fence);
return fence;
}
static int atomic_run(const struct gbm *gbm, const struct egl *egl)
{
struct gbm_bo *bo = NULL;
struct drm_fb *fb;
uint32_t i = 0;
uint32_t flags = DRM_MODE_ATOMIC_NONBLOCK;
uint64_t start_time, report_time, cur_time;
int ret;
if (egl_check(egl, eglDupNativeFenceFDANDROID) ||
egl_check(egl, eglCreateSyncKHR) ||
egl_check(egl, eglDestroySyncKHR) ||
egl_check(egl, eglWaitSyncKHR) ||
egl_check(egl, eglClientWaitSyncKHR))
return -1;
/* Allow a modeset change for the first commit only. */
flags |= DRM_MODE_ATOMIC_ALLOW_MODESET;
start_time = report_time = get_time_ns();
while (i < _drm.count) {
unsigned frame = i;
struct gbm_bo *next_bo;
EGLSyncKHR gpu_fence = NULL; /* out-fence from gpu, in-fence to kms */
EGLSyncKHR kms_fence = NULL; /* in-fence to gpu, out-fence from kms */
if (_drm.kms_out_fence_fd != -1) {
kms_fence = create_fence(egl, _drm.kms_out_fence_fd);
assert(kms_fence);
/* driver now has ownership of the fence fd: */
_drm.kms_out_fence_fd = -1;
/* wait "on the gpu" (ie. this won't necessarily block, but
* will block the rendering until fence is signaled), until
* the previous pageflip completes so we don't render into
* the buffer that is still on screen.
*/
egl->eglWaitSyncKHR(egl->display, kms_fence, 0);
}
/* Start fps measuring on second frame, to remove the time spent
* compiling shader, etc, from the fps:
*/
if (i == 1) {
start_time = report_time = get_time_ns();
}
if (!gbm->surface) {
glBindFramebuffer(GL_FRAMEBUFFER, egl->fbs[frame % NUM_BUFFERS].fb);
}
egl->draw(start_time, i++);
/* insert fence to be singled in cmdstream.. this fence will be
* signaled when gpu rendering done
*/
gpu_fence = create_fence(egl, EGL_NO_NATIVE_FENCE_FD_ANDROID);
assert(gpu_fence);
if (gbm->surface) {
eglSwapBuffers(egl->display, egl->surface);
}
/* after swapbuffers, gpu_fence should be flushed, so safe
* to get fd:
*/
_drm.kms_in_fence_fd = egl->eglDupNativeFenceFDANDROID(egl->display, gpu_fence);
egl->eglDestroySyncKHR(egl->display, gpu_fence);
assert(_drm.kms_in_fence_fd != -1);
if (gbm->surface) {
next_bo = gbm_surface_lock_front_buffer(gbm->surface);
} else {
next_bo = gbm->bos[frame % NUM_BUFFERS];
}
if (!next_bo) {
printf("Failed to lock frontbuffer\n");
return -1;
}
fb = drm_fb_get_from_bo(next_bo);
if (!fb) {
printf("Failed to get a new framebuffer BO\n");
return -1;
}
if (kms_fence) {
EGLint status;
/* Wait on the CPU side for the _previous_ commit to
* complete before we post the flip through KMS, as
* atomic will reject the commit if we post a new one
* whilst the previous one is still pending.
*/
do {
status = egl->eglClientWaitSyncKHR(egl->display,
kms_fence,
0,
EGL_FOREVER_KHR);
} while (status != EGL_CONDITION_SATISFIED_KHR);
egl->eglDestroySyncKHR(egl->display, kms_fence);
}
cur_time = get_time_ns();
if (cur_time > (report_time + 2 * NSEC_PER_SEC)) {
double elapsed_time = cur_time - start_time;
double secs = elapsed_time / (double)NSEC_PER_SEC;
unsigned frames = i - 1; /* first frame ignored */
printf("Rendered %u frames in %f sec (%f fps)\n",
frames, secs, (double)frames/secs);
report_time = cur_time;
}
/* Check for user input: */
struct pollfd fdset[] = { {
.fd = STDIN_FILENO,
.events = POLLIN,
} };
ret = poll(fdset, ARRAY_SIZE(fdset), 0);
if (ret > 0) {
printf("user interrupted!\n");
return 0;
}
/*
* Here you could also update drm plane layers if you want
* hw composition
*/
ret = drm_atomic_commit(fb->fb_id, flags);
if (ret) {
printf("failed to commit: %s\n", strerror(errno));
return -1;
}
/* release last buffer to render on again: */
if (bo && gbm->surface)
gbm_surface_release_buffer(gbm->surface, bo);
bo = next_bo;
/* Allow a modeset change for the first commit only. */
flags &= ~(DRM_MODE_ATOMIC_ALLOW_MODESET);
}
finish_perfcntrs();
cur_time = get_time_ns();
double elapsed_time = cur_time - start_time;
double secs = elapsed_time / (double)NSEC_PER_SEC;
unsigned frames = i - 1; /* first frame ignored */
printf("Rendered %u frames in %f sec (%f fps)\n",
frames, secs, (double)frames/secs);
dump_perfcntrs(frames, elapsed_time);
return ret;
}
static int atomic_draw(const struct gbm *gbm, const struct egl *egl)
{
static struct gbm_bo *bo = NULL;
struct drm_fb *fb;
static uint32_t i = 0;
static uint32_t flags = DRM_MODE_ATOMIC_NONBLOCK;
static uint64_t start_time = 0;
static uint64_t report_time, cur_time;
int ret;
if (!start_time) {
if (egl_check(egl, eglDupNativeFenceFDANDROID) ||
egl_check(egl, eglCreateSyncKHR) ||
egl_check(egl, eglDestroySyncKHR) ||
egl_check(egl, eglWaitSyncKHR) ||
egl_check(egl, eglClientWaitSyncKHR))
return -1;
/* Allow a modeset change for the first commit only. */
flags |= DRM_MODE_ATOMIC_ALLOW_MODESET;
start_time = report_time = get_time_ns();
}
unsigned frame = i;
struct gbm_bo *next_bo;
EGLSyncKHR gpu_fence = NULL; /* out-fence from gpu, in-fence to kms */
EGLSyncKHR kms_fence = NULL; /* in-fence to gpu, out-fence from kms */
if (_drm.kms_out_fence_fd != -1) {
kms_fence = create_fence(egl, _drm.kms_out_fence_fd);
assert(kms_fence);
/* driver now has ownership of the fence fd: */
_drm.kms_out_fence_fd = -1;
/* wait "on the gpu" (ie. this won't necessarily block, but
* will block the rendering until fence is signaled), until
* the previous pageflip completes so we don't render into
* the buffer that is still on screen.
*/
egl->eglWaitSyncKHR(egl->display, kms_fence, 0);
}
/* Start fps measuring on second frame, to remove the time spent
* compiling shader, etc, from the fps:
*/
if (i == 1) {
start_time = report_time = get_time_ns();
}
if (!gbm->surface) {
glBindFramebuffer(GL_FRAMEBUFFER, egl->fbs[frame % NUM_BUFFERS].fb);
}
egl->draw(start_time, i++);
/* insert fence to be singled in cmdstream.. this fence will be
* signaled when gpu rendering done
*/
gpu_fence = create_fence(egl, EGL_NO_NATIVE_FENCE_FD_ANDROID);
assert(gpu_fence);
if (gbm->surface) {
eglSwapBuffers(egl->display, egl->surface);
}
/* after swapbuffers, gpu_fence should be flushed, so safe
* to get fd:
*/
_drm.kms_in_fence_fd = egl->eglDupNativeFenceFDANDROID(egl->display, gpu_fence);
egl->eglDestroySyncKHR(egl->display, gpu_fence);
assert(_drm.kms_in_fence_fd != -1);
if (gbm->surface) {
next_bo = gbm_surface_lock_front_buffer(gbm->surface);
} else {
next_bo = gbm->bos[frame % NUM_BUFFERS];
}
if (!next_bo) {
printf("Failed to lock frontbuffer\n");
return -1;
}
fb = drm_fb_get_from_bo(next_bo);
if (!fb) {
printf("Failed to get a new framebuffer BO\n");
return -1;
}
if (kms_fence) {
EGLint status;
/* Wait on the CPU side for the _previous_ commit to
* complete before we post the flip through KMS, as
* atomic will reject the commit if we post a new one
* whilst the previous one is still pending.
*/
do {
status = egl->eglClientWaitSyncKHR(egl->display,
kms_fence,
0,
EGL_FOREVER_KHR);
} while (status != EGL_CONDITION_SATISFIED_KHR);
egl->eglDestroySyncKHR(egl->display, kms_fence);
}
#if 0
cur_time = get_time_ns();
if (cur_time > (report_time + 2 * NSEC_PER_SEC)) {
double elapsed_time = cur_time - start_time;
double secs = elapsed_time / (double)NSEC_PER_SEC;
unsigned frames = i - 1; /* first frame ignored */
printf("Rendered %u frames in %f sec (%f fps)\n",
frames, secs, (double)frames/secs);
report_time = cur_time;
}
#endif
/* Check for user input: */
/* struct pollfd fdset[] = { {
.fd = STDIN_FILENO,
.events = POLLIN,
} };
ret = poll(fdset, ARRAY_SIZE(fdset), 0);
if (ret > 0) {
printf("user interrupted!\n");
return 0;
}*/
/*
* Here you could also update drm plane layers if you want
* hw composition
*/
ret = drm_atomic_commit(fb->fb_id, flags);
if (ret) {
printf("failed to commit: %s\n", strerror(errno));
return -1;
}
/* release last buffer to render on again: */
if (bo && gbm->surface)
gbm_surface_release_buffer(gbm->surface, bo);
bo = next_bo;
/* Allow a modeset change for the first commit only. */
flags &= ~(DRM_MODE_ATOMIC_ALLOW_MODESET);
return ret;
}
/* Pick a plane.. something that at a minimum can be connected to
* the chosen crtc, but prefer primary plane.
*
* Seems like there is some room for a drmModeObjectGetNamedProperty()
* type helper in lib_drm..
*/
static int get_plane_id(void)
{
drmModePlaneResPtr plane_resources;
uint32_t i, j;
int ret = -EINVAL;
int found_primary = 0;
plane_resources = drmModeGetPlaneResources(_drm.fd);
if (!plane_resources) {
printf("drmModeGetPlaneResources failed: %s\n", strerror(errno));
return -1;
}
for (i = 0; (i < plane_resources->count_planes) && !found_primary; i++) {
uint32_t id = plane_resources->planes[i];
drmModePlanePtr plane = drmModeGetPlane(_drm.fd, id);
if (!plane) {
printf("drmModeGetPlane(%u) failed: %s\n", id, strerror(errno));
continue;
}
if (plane->possible_crtcs & (1 << _drm.crtc_index)) {
drmModeObjectPropertiesPtr props =
drmModeObjectGetProperties(_drm.fd, id, DRM_MODE_OBJECT_PLANE);
/* primary or not, this plane is good enough to use: */
ret = id;
for (j = 0; j < props->count_props; j++) {
drmModePropertyPtr p =
drmModeGetProperty(_drm.fd, props->props[j]);
if ((strcmp(p->name, "type") == 0) &&
(props->prop_values[j] == DRM_PLANE_TYPE_PRIMARY)) {
/* found our primary plane, lets use that: */
found_primary = 1;
}
drmModeFreeProperty(p);
}
drmModeFreeObjectProperties(props);
}
drmModeFreePlane(plane);
}
drmModeFreePlaneResources(plane_resources);
return ret;
}
const struct drm * init_drm_atomic(const char *device, const char *mode_str,
unsigned int vrefresh, unsigned int count)
{
uint32_t plane_id;
int ret;
ret = init_drm(&_drm, device, mode_str, vrefresh, count);
if (ret)
return NULL;
ret = drmSetClientCap(_drm.fd, DRM_CLIENT_CAP_ATOMIC, 1);
if (ret) {
printf("no atomic modesetting support: %s\n", strerror(errno));
return NULL;
}
ret = get_plane_id();
if (!ret) {
printf("could not find a suitable plane\n");
return NULL;
} else {
plane_id = ret;
}
/* We only do single plane to single crtc to single connector, no
* fancy multi-monitor or multi-plane stuff. So just grab the
* plane/crtc/connector property info for one of each:
*/
_drm.plane = calloc(1, sizeof(*_drm.plane));
_drm.crtc = calloc(1, sizeof(*_drm.crtc));
_drm.connector = calloc(1, sizeof(*_drm.connector));
#define get_resource(type, Type, id) do { \
_drm.type->type = drmModeGet##Type(_drm.fd, id); \
if (!_drm.type->type) { \
printf("could not get %s %i: %s\n", \
#type, id, strerror(errno)); \
return NULL; \
} \
} while (0)
get_resource(plane, Plane, plane_id);
get_resource(crtc, Crtc, _drm.crtc_id);
get_resource(connector, Connector, _drm.connector_id);
#define get_properties(type, TYPE, id) do { \
uint32_t i; \
_drm.type->props = drmModeObjectGetProperties(_drm.fd, \
id, DRM_MODE_OBJECT_##TYPE); \
if (!_drm.type->props) { \
printf("could not get %s %u properties: %s\n", \
#type, id, strerror(errno)); \
return NULL; \
} \
_drm.type->props_info = calloc(_drm.type->props->count_props, \
sizeof(*_drm.type->props_info)); \
for (i = 0; i < _drm.type->props->count_props; i++) { \
_drm.type->props_info[i] = drmModeGetProperty(_drm.fd, \
_drm.type->props->props[i]); \
} \
} while (0)
get_properties(plane, PLANE, plane_id);
get_properties(crtc, CRTC, _drm.crtc_id);
get_properties(connector, CONNECTOR, _drm.connector_id);
_drm.run = atomic_run;
_drm.draw = atomic_draw;
return &_drm;
}
// common.c
/*
* Copyright (c) 2017 Rob Clark <rclark@redhat.com>
* Copyright © 2013 Intel Corporation
*
* 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, sub license,
* 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 (including the
* next paragraph) 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 NON-INFRINGEMENT. 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.
*/
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
// #include "common.h"
static struct gbm kg_gbm;
static struct egl kg_egl;
WEAK struct gbm_surface *
gbm_surface_create_with_modifiers(struct gbm_device *gbm,
uint32_t width, uint32_t height,
uint32_t format,
const uint64_t *modifiers,
const unsigned int count);
WEAK struct gbm_bo *
gbm_bo_create_with_modifiers(struct gbm_device *gbm,
uint32_t width, uint32_t height,
uint32_t format,
const uint64_t *modifiers,
const unsigned int count);
static struct gbm_bo * init_bo(uint64_t modifier)
{
struct gbm_bo *bo = NULL;
if (gbm_bo_create_with_modifiers) {
bo = gbm_bo_create_with_modifiers(kg_gbm.dev,
kg_gbm.width, kg_gbm.height,
kg_gbm.format,
&modifier, 1);
}
if (!bo) {
if (modifier != DRM_FORMAT_MOD_LINEAR) {
fprintf(stderr, "Modifiers requested but support isn't available\n");
return NULL;
}
bo = gbm_bo_create(kg_gbm.dev,
kg_gbm.width, kg_gbm.height,
kg_gbm.format,
GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING);
}
if (!bo) {
printf("failed to create gbm bo\n");
return NULL;
}
return bo;
}
static struct gbm * init_surfaceless(uint64_t modifier)
{
for (unsigned i = 0; i < ARRAY_SIZE(kg_gbm.bos); i++) {
kg_gbm.bos[i] = init_bo(modifier);
if (!kg_gbm.bos[i])
return NULL;
}
return &kg_gbm;
}
static struct gbm * init_surface(uint64_t modifier)
{
if (gbm_surface_create_with_modifiers) {
kg_gbm.surface = gbm_surface_create_with_modifiers(kg_gbm.dev,
kg_gbm.width, kg_gbm.height,
kg_gbm.format,
&modifier, 1);
}
if (!kg_gbm.surface) {
if (modifier != DRM_FORMAT_MOD_LINEAR) {
fprintf(stderr, "Modifiers requested but support isn't available\n");