-
Notifications
You must be signed in to change notification settings - Fork 1
/
0028-egl-add-null-platform.patch
1482 lines (1459 loc) · 44.2 KB
/
0028-egl-add-null-platform.patch
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
From e1507dd0413aef1cea1d3caecba59a1966764177 Mon Sep 17 00:00:00 2001
From: Frank Binns <[email protected]>
Date: Sun, 5 Jun 2016 12:04:40 +0100
Subject: [PATCH] egl: add "null" platform
---
meson.build | 10 +-
meson_options.txt | 2 +-
src/egl/drivers/dri2/egl_dri2.c | 11 +-
src/egl/drivers/dri2/egl_dri2.h | 59 +-
src/egl/drivers/dri2/platform_null.c | 1179 ++++++++++++++++++++++++++
src/egl/main/eglapi.c | 5 +-
src/egl/main/egldisplay.c | 1 +
src/egl/main/egldisplay.h | 1 +
src/egl/meson.build | 5 +
9 files changed, 1264 insertions(+), 9 deletions(-)
create mode 100644 src/egl/drivers/dri2/platform_null.c
diff --git a/meson.build b/meson.build
index d9fbb1f..ce81c7d 100644
--- a/meson.build
+++ b/meson.build
@@ -335,7 +335,7 @@ endif
_platforms = get_option('platforms')
if _platforms.contains('auto')
if system_has_kms_drm
- _platforms = ['x11', 'wayland']
+ _platforms = ['x11', 'wayland', 'null']
elif ['darwin', 'cygwin'].contains(host_machine.system())
_platforms = ['x11']
elif ['haiku'].contains(host_machine.system())
@@ -353,6 +353,7 @@ with_platform_x11 = _platforms.contains('x11')
with_platform_wayland = _platforms.contains('wayland')
with_platform_haiku = _platforms.contains('haiku')
with_platform_windows = _platforms.contains('windows')
+with_platform_null = _platforms.contains('null')
with_glx = get_option('glx')
if with_glx == 'auto'
@@ -1014,6 +1015,10 @@ if with_platform_android
]
endif
+if with_platform_null
+ pre_args += '-DHAVE_NULL_PLATFORM'
+endif
+
prog_python = import('python').find_installation('python3')
has_mako = run_command(
prog_python, '-c',
@@ -1640,7 +1645,8 @@ with_gallium_drisw_kms = false
dep_libdrm = dependency(
'libdrm', version : '>=' + _drm_ver,
# GNU/Hurd includes egl_dri2, without drm.
- required : (with_dri2 and host_machine.system() != 'gnu') or with_dri3
+ required : (with_dri2 and host_machine.system() != 'gnu') or with_dri3 or
+ with_platform_null
)
if dep_libdrm.found()
pre_args += '-DHAVE_LIBDRM'
diff --git a/meson_options.txt b/meson_options.txt
index 3f401d3..d76cc4b 100644
--- a/meson_options.txt
+++ b/meson_options.txt
@@ -23,7 +23,7 @@ option(
type : 'array',
value : ['auto'],
choices : [
- 'auto', 'x11', 'wayland', 'haiku', 'android', 'windows',
+ 'auto', 'x11', 'wayland', 'haiku', 'android', 'windows', 'null',
],
description : 'window systems to support. If this is set to `auto`, all platforms applicable will be enabled.'
)
diff --git a/src/egl/drivers/dri2/egl_dri2.c b/src/egl/drivers/dri2/egl_dri2.c
index 635c445..68b8fa7 100644
--- a/src/egl/drivers/dri2/egl_dri2.c
+++ b/src/egl/drivers/dri2/egl_dri2.c
@@ -1261,6 +1261,9 @@ dri2_initialize(_EGLDisplay *disp)
case _EGL_PLATFORM_DEVICE:
ret = dri2_initialize_device(disp);
break;
+ case _EGL_PLATFORM_NULL:
+ ret = dri2_initialize_null(disp);
+ break;
case _EGL_PLATFORM_X11:
case _EGL_PLATFORM_XCB:
ret = dri2_initialize_x11(disp);
@@ -1321,8 +1324,6 @@ dri2_display_destroy(_EGLDisplay *disp)
dri2_dpy->vtbl->close_screen_notify(disp);
dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
}
- if (dri2_dpy->fd >= 0)
- close(dri2_dpy->fd);
/* Don't dlclose the driver when building with the address sanitizer, so you
* get good symbols from the leak reports.
@@ -1348,11 +1349,17 @@ dri2_display_destroy(_EGLDisplay *disp)
case _EGL_PLATFORM_WAYLAND:
dri2_teardown_wayland(dri2_dpy);
break;
+ case _EGL_PLATFORM_NULL:
+ dri2_teardown_null(dri2_dpy);
+ break;
default:
/* TODO: add teardown for other platforms */
break;
}
+ if (dri2_dpy->fd >= 0)
+ close(dri2_dpy->fd);
+
/* The drm platform does not create the screen/driver_configs but reuses
* the ones from the gbm device. As such the gbm itself is responsible
* for the cleanup.
diff --git a/src/egl/drivers/dri2/egl_dri2.h b/src/egl/drivers/dri2/egl_dri2.h
index 35f66ad..5de5f0f 100644
--- a/src/egl/drivers/dri2/egl_dri2.h
+++ b/src/egl/drivers/dri2/egl_dri2.h
@@ -77,6 +77,10 @@ struct zwp_linux_dmabuf_feedback_v1;
#endif /* HAVE_ANDROID_PLATFORM */
+#ifdef HAVE_NULL_PLATFORM
+#include <xf86drmMode.h>
+#endif
+
#include "eglconfig.h"
#include "eglcontext.h"
#include "egldevice.h"
@@ -94,6 +98,22 @@ struct zwp_linux_dmabuf_feedback_v1;
struct wl_buffer;
+#ifdef HAVE_NULL_PLATFORM
+struct display_output {
+ bool in_use;
+ uint32_t connector_id;
+ drmModePropertyRes **connector_prop_res;
+ uint32_t crtc_id;
+ drmModePropertyRes **crtc_prop_res;
+ uint32_t plane_id;
+ drmModePropertyRes **plane_prop_res;
+ drmModeModeInfo mode;
+ uint32_t mode_blob_id;
+ unsigned formats;
+ drmModeAtomicReq *atomic_state;
+};
+#endif
+
struct dri2_egl_display_vtbl {
/* mandatory on Wayland, unused otherwise */
int (*authenticate)(_EGLDisplay *disp, uint32_t id);
@@ -287,6 +307,11 @@ struct dri2_egl_display
char *device_name;
#endif
+#ifdef HAVE_NULL_PLATFORM
+ bool atomic_enabled;
+ struct display_output output;
+#endif
+
#ifdef HAVE_ANDROID_PLATFORM
const gralloc_module_t *gralloc;
/* gralloc vendor usage bit for front rendering */
@@ -331,11 +356,14 @@ struct dri2_egl_surface
struct zwp_linux_dmabuf_feedback_v1 *wl_dmabuf_feedback;
struct dmabuf_feedback dmabuf_feedback, pending_dmabuf_feedback;
bool compositor_using_another_device;
- int format;
bool resized;
bool received_dmabuf_feedback;
#endif
+#if defined(HAVE_WAYLAND_PLATFORM) || defined(HAVE_NULL_PLATFORM)
+ int format;
+#endif
+
#ifdef HAVE_DRM_PLATFORM
struct gbm_dri_surface *gbm_surf;
#endif
@@ -343,12 +371,15 @@ struct dri2_egl_surface
/* EGL-owned buffers */
__DRIbuffer *local_buffers[__DRI_BUFFER_COUNT];
-#if defined(HAVE_WAYLAND_PLATFORM) || defined(HAVE_DRM_PLATFORM)
+#if defined(HAVE_WAYLAND_PLATFORM) || defined(HAVE_DRM_PLATFORM) || \
+ defined(HAVE_NULL_PLATFORM)
struct {
+#if defined(HAVE_WAYLAND_PLATFORM) || defined(HAVE_NULL_PLATFORM)
+ __DRIimage *dri_image;
+#endif
#ifdef HAVE_WAYLAND_PLATFORM
struct wl_buffer *wl_buffer;
bool wl_release;
- __DRIimage *dri_image;
/* for is_different_gpu case. NULL else */
__DRIimage *linear_copy;
/* for swrast */
@@ -357,6 +388,9 @@ struct dri2_egl_surface
#endif
#ifdef HAVE_DRM_PLATFORM
struct gbm_bo *bo;
+#endif
+#ifdef HAVE_NULL_PLATFORM
+ uint32_t fb_id;
#endif
bool locked;
int age;
@@ -393,6 +427,10 @@ struct dri2_egl_surface
void *swrast_front;
#endif
+#ifdef HAVE_NULL_PLATFORM
+ uint32_t front_fb_id;
+#endif
+
int out_fence_fd;
EGLBoolean enable_out_fence;
@@ -571,6 +609,21 @@ dri2_initialize_android(_EGLDisplay *disp)
EGLBoolean
dri2_initialize_surfaceless(_EGLDisplay *disp);
+#ifdef HAVE_NULL_PLATFORM
+EGLBoolean
+dri2_initialize_null(_EGLDisplay *disp);
+void
+dri2_teardown_null(struct dri2_egl_display *dri2_dpy);
+#else
+static inline EGLBoolean
+dri2_initialize_null(_EGLDisplay *disp)
+{
+ return _eglError(EGL_NOT_INITIALIZED, "Null platform not built");
+}
+static inline void
+dri2_teardown_null(struct dri2_egl_display *dri2_dpy) {}
+#endif
+
EGLBoolean
dri2_initialize_device(_EGLDisplay *disp);
static inline void
diff --git a/src/egl/drivers/dri2/platform_null.c b/src/egl/drivers/dri2/platform_null.c
new file mode 100644
index 0000000..fb03ecc
--- /dev/null
+++ b/src/egl/drivers/dri2/platform_null.c
@@ -0,0 +1,1179 @@
+/*
+ * Copyright (c) Imagination Technologies Ltd.
+ *
+ * Parts based on platform_wayland, which has:
+ *
+ * Copyright © 2011-2012 Intel Corporation
+ * Copyright © 2012 Collabora, Ltd.
+ *
+ * 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 (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
+ * 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.
+ */
+
+#include <dlfcn.h>
+#include <drm_fourcc.h>
+#include <fcntl.h>
+#include <poll.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <xf86drm.h>
+
+#include "egl_dri2.h"
+#include "loader.h"
+
+#define NULL_CARD_MINOR_MAX 63U
+
+/*
+ * Need at least version 4 for __DRI_IMAGE_ATTRIB_WIDTH and
+ * __DRI_IMAGE_ATTRIB_HEIGHT
+ */
+#define NULL_IMAGE_EXTENSION_VERSION_MIN 4
+
+struct object_property {
+ uint32_t object_id;
+ uint32_t prop_id;
+ uint64_t prop_value;
+};
+
+#define object_property_set_named(output, object_type, prop_name, value) \
+ { \
+ .object_id = (output)->object_type##_id, \
+ .prop_id = property_id_get_for_name((output)->object_type##_prop_res, \
+ prop_name), \
+ .prop_value = value, \
+ }
+
+/*
+ * The index of entries in this table is used as a bitmask in
+ * dri2_dpy->formats, which tracks the formats supported by the display.
+ */
+static const struct dri2_null_format {
+ uint32_t drm_format;
+ int dri_image_format;
+ int rgba_shifts[4];
+ unsigned int rgba_sizes[4];
+} dri2_null_formats[] = {
+ {
+ .drm_format = DRM_FORMAT_XRGB8888,
+ .dri_image_format = __DRI_IMAGE_FORMAT_XRGB8888,
+ .rgba_shifts = { 16, 8, 0, -1 },
+ .rgba_sizes = { 8, 8, 8, 0 },
+ },
+ {
+ .drm_format = DRM_FORMAT_ARGB8888,
+ .dri_image_format = __DRI_IMAGE_FORMAT_ARGB8888,
+ .rgba_shifts = { 16, 8, 0, 24 },
+ .rgba_sizes = { 8, 8, 8, 8 },
+ },
+ {
+ .drm_format = DRM_FORMAT_RGB565,
+ .dri_image_format = __DRI_IMAGE_FORMAT_RGB565,
+ .rgba_shifts = { 11, 5, 0, -1 },
+ .rgba_sizes = { 5, 6, 5, 0 },
+ },
+};
+
+
+static int
+format_idx_get_from_config(struct dri2_egl_display *dri2_dpy,
+ const __DRIconfig *config)
+{
+ int shifts[4];
+ unsigned int sizes[4];
+
+ dri2_get_shifts_and_sizes(dri2_dpy->core, config, shifts, sizes);
+
+ for (unsigned int i = 0; i < ARRAY_SIZE(dri2_null_formats); i++) {
+ const struct dri2_null_format *format = &dri2_null_formats[i];
+
+ if (shifts[0] == format->rgba_shifts[0] &&
+ shifts[1] == format->rgba_shifts[1] &&
+ shifts[2] == format->rgba_shifts[2] &&
+ shifts[3] == format->rgba_shifts[3] &&
+ sizes[0] == format->rgba_sizes[0] &&
+ sizes[1] == format->rgba_sizes[1] &&
+ sizes[2] == format->rgba_sizes[2] &&
+ sizes[3] == format->rgba_sizes[3]) {
+ return i;
+ }
+ }
+
+ return -1;
+}
+
+static int
+format_idx_get_from_dri_image_format(uint32_t dri_image_format)
+{
+ for (unsigned int i = 0; i < ARRAY_SIZE(dri2_null_formats); i++)
+ if (dri2_null_formats[i].dri_image_format == dri_image_format)
+ return i;
+
+ return -1;
+}
+
+static int
+format_idx_get_from_drm_format(uint32_t drm_format)
+{
+ for (unsigned int i = 0; i < ARRAY_SIZE(dri2_null_formats); i++)
+ if (dri2_null_formats[i].drm_format == drm_format)
+ return i;
+
+ return -1;
+}
+
+static int
+atomic_state_add_object_properties(drmModeAtomicReq *atomic_state,
+ const struct object_property *props,
+ const unsigned prop_count)
+{
+ for (unsigned i = 0; i < prop_count; i++) {
+ int err;
+
+ if (props[i].prop_id == 0)
+ return -EINVAL;
+
+ err = drmModeAtomicAddProperty(atomic_state, props[i].object_id,
+ props[i].prop_id, props[i].prop_value);
+ if (err < 0)
+ return err;
+ }
+
+ return 0;
+}
+
+
+static uint32_t
+property_id_get_for_name(drmModePropertyRes **prop_res, const char *prop_name)
+{
+ if (prop_res)
+ for (unsigned i = 0; prop_res[i]; i++)
+ if (!strcmp(prop_res[i]->name, prop_name))
+ return prop_res[i]->prop_id;
+
+ return 0;
+}
+
+static void
+flip_handler(int fd, unsigned int sequence, unsigned int tv_sec,
+ unsigned int tv_usec, void *user_data)
+{
+ bool *plocked = user_data;
+
+ if (plocked)
+ *plocked = false;
+}
+
+static bool
+flip_process(int fd)
+{
+ static drmEventContext evctx =
+ {.version = 2, .page_flip_handler = flip_handler};
+ struct pollfd pfd = {.fd = fd, .events = POLLIN};
+ int ret;
+
+ do {
+ ret = poll(&pfd, 1, -1);
+ } while (ret > 0 && pfd.revents != pfd.events);
+
+ if (ret <= 0)
+ return false;
+
+ drmHandleEvent(fd, &evctx);
+
+ return true;
+}
+
+static drmModePropertyRes **
+object_get_property_resources(int fd, uint32_t object_id, uint32_t object_type)
+{
+ drmModeObjectProperties *props;
+ drmModePropertyRes **prop_res;
+
+ props = drmModeObjectGetProperties(fd, object_id, object_type);
+ if (!props)
+ return NULL;
+
+ prop_res = malloc((props->count_props + 1) * sizeof(*prop_res));
+ if (prop_res) {
+ prop_res[props->count_props] = NULL;
+
+ for (unsigned i = 0; i < props->count_props; i++) {
+ prop_res[i] = drmModeGetProperty(fd, props->props[i]);
+ if (!prop_res[i]) {
+ while (i--) {
+ drmModeFreeProperty(prop_res[i]);
+ free(prop_res);
+ prop_res = NULL;
+ }
+ break;
+ }
+ }
+ }
+
+ drmModeFreeObjectProperties(props);
+
+ return prop_res;
+}
+
+static void
+object_free_property_resources(int fd, drmModePropertyRes **prop_res)
+{
+ for (unsigned i = 0; prop_res[i]; i++)
+ drmModeFreeProperty(prop_res[i]);
+ free(prop_res);
+}
+
+static bool
+object_property_value_for_name(int fd, uint32_t object_id, uint32_t object_type,
+ const char *prop_name, uint64_t *value_out)
+{
+ drmModeObjectProperties *plane_props;
+ bool found = false;
+
+ plane_props = drmModeObjectGetProperties(fd, object_id, object_type);
+ if (!plane_props)
+ return false;
+
+ for (unsigned i = 0; i < plane_props->count_props; i++) {
+ drmModePropertyRes *prop;
+
+ prop = drmModeGetProperty(fd, plane_props->props[i]);
+ if (!prop)
+ continue;
+
+ found = !strcmp(prop->name, prop_name);
+ drmModeFreeProperty(prop);
+ if (found) {
+ *value_out = plane_props->prop_values[i];
+ break;
+ }
+ }
+
+ drmModeFreeObjectProperties(plane_props);
+
+ return found;
+}
+
+static int
+connector_choose_mode(drmModeConnector *connector)
+{
+ if (!connector->count_modes)
+ return -1;
+
+ for (unsigned i = 0; i < connector->count_modes; i++) {
+ if (connector->modes[i].flags & DRM_MODE_FLAG_INTERLACE)
+ continue;
+
+ if (connector->modes[i].type & DRM_MODE_TYPE_PREFERRED)
+ return i;
+ }
+
+ return 0;
+}
+
+static drmModeConnector *
+connector_get(int fd, drmModeRes *resources)
+{
+ /* Find the first connected connector */
+ for (unsigned i = 0; i < resources->count_connectors; i++) {
+ drmModeConnector *connector;
+
+ connector = drmModeGetConnector(fd, resources->connectors[i]);
+ if (!connector)
+ continue;
+
+ if (connector->connection == DRM_MODE_CONNECTED)
+ return connector;
+
+ drmModeFreeConnector(connector);
+ }
+
+ return NULL;
+}
+
+static drmModeCrtc *
+crtc_get_for_connector(int fd, drmModeRes *resources,
+ drmModeConnector *connector)
+{
+ for (unsigned i = 0; i < connector->count_encoders; i++) {
+ drmModeEncoder *encoder;
+
+ encoder = drmModeGetEncoder(fd, connector->encoders[i]);
+ if (!encoder)
+ continue;
+
+ for (unsigned j = 0; j < resources->count_crtcs; j++) {
+ if (encoder->possible_crtcs & (1 << j)) {
+ drmModeCrtc *crtc;
+
+ crtc = drmModeGetCrtc(fd, resources->crtcs[j]);
+ if (crtc) {
+ drmModeFreeEncoder(encoder);
+ return crtc;
+ }
+ }
+ }
+
+ drmModeFreeEncoder(encoder);
+ }
+
+ return NULL;
+}
+
+static drmModePlane *
+primary_plane_get_for_crtc(int fd, drmModeRes *resources, drmModeCrtc *crtc)
+{
+ drmModePlaneRes *plane_resources;
+ unsigned crtc_idx;
+
+ plane_resources = drmModeGetPlaneResources(fd);
+ if (!plane_resources)
+ return NULL;
+
+ for (crtc_idx = 0; crtc_idx < resources->count_crtcs; crtc_idx++)
+ if (resources->crtcs[crtc_idx] == crtc->crtc_id)
+ break;
+ assert(crtc_idx != resources->count_crtcs);
+
+ for (unsigned i = 0; i < plane_resources->count_planes; i++) {
+ const uint32_t crtc_bit = 1 << crtc_idx;
+ drmModePlane *plane;
+
+ plane = drmModeGetPlane(fd, plane_resources->planes[i]);
+ if (!plane)
+ continue;
+
+ if (plane->possible_crtcs & crtc_bit) {
+ uint64_t type;
+ bool res;
+
+ res = object_property_value_for_name(fd, plane->plane_id,
+ DRM_MODE_OBJECT_PLANE,
+ "type", &type);
+ if (res && type == DRM_PLANE_TYPE_PRIMARY) {
+ drmModeFreePlaneResources(plane_resources);
+ return plane;
+ }
+ }
+
+ drmModeFreePlane(plane);
+ }
+
+ drmModeFreePlaneResources(plane_resources);
+
+ return NULL;
+}
+
+static bool
+display_output_atomic_init(int fd, struct display_output *output)
+{
+ drmModePropertyRes **connector_prop_res;
+ drmModePropertyRes **crtc_prop_res;
+ drmModePropertyRes **plane_prop_res;
+ int err;
+
+ connector_prop_res = object_get_property_resources(fd, output->connector_id,
+ DRM_MODE_OBJECT_CONNECTOR);
+ if (!connector_prop_res)
+ return false;
+
+ crtc_prop_res = object_get_property_resources(fd, output->crtc_id,
+ DRM_MODE_OBJECT_CRTC);
+ if (!crtc_prop_res)
+ goto err_free_connector_prop_res;
+
+ plane_prop_res = object_get_property_resources(fd, output->plane_id,
+ DRM_MODE_OBJECT_PLANE);
+ if (!plane_prop_res)
+ goto err_free_crtc_prop_res;
+
+ err = drmModeCreatePropertyBlob(fd, &output->mode, sizeof(output->mode),
+ &output->mode_blob_id);
+ if (err)
+ goto err_free_plane_prop_res;
+
+ output->atomic_state = drmModeAtomicAlloc();
+ if (!output->atomic_state)
+ goto err_destroy_mode_prop_blob;
+
+ output->connector_prop_res = connector_prop_res;
+ output->crtc_prop_res = crtc_prop_res;
+ output->plane_prop_res = plane_prop_res;
+
+ return true;
+
+err_destroy_mode_prop_blob:
+ drmModeDestroyPropertyBlob(fd, output->mode_blob_id);
+err_free_plane_prop_res:
+ object_free_property_resources(fd, plane_prop_res);
+err_free_crtc_prop_res:
+ object_free_property_resources(fd, crtc_prop_res);
+err_free_connector_prop_res:
+ object_free_property_resources(fd, connector_prop_res);
+ return false;
+}
+
+static int
+display_output_atomic_flip(int fd, struct display_output *output, uint32_t fb_id,
+ uint32_t flags, void *flip_data)
+{
+ const struct object_property obj_props[] = {
+ object_property_set_named(output, plane, "FB_ID", fb_id),
+ };
+ int err;
+
+ /* Reset atomic state */
+ drmModeAtomicSetCursor(output->atomic_state, 0);
+
+ err = atomic_state_add_object_properties(output->atomic_state, obj_props,
+ ARRAY_SIZE(obj_props));
+ if (err)
+ return err;
+
+ /*
+ * Don't block - like drmModePageFlip, drmModeAtomicCommit will return
+ * -EBUSY if the commit can't be queued in the kernel.
+ */
+ flags |= DRM_MODE_ATOMIC_NONBLOCK;
+
+ return drmModeAtomicCommit(fd, output->atomic_state, flags, flip_data);
+}
+
+static int
+display_output_atomic_modeset(int fd, struct display_output *output, uint32_t fb_id)
+{
+ /* SRC_W and SRC_H in 16.16 fixed point */
+ const struct object_property obj_props[] = {
+ object_property_set_named(output, connector, "CRTC_ID", output->crtc_id),
+ object_property_set_named(output, crtc, "ACTIVE", 1),
+ object_property_set_named(output, crtc, "MODE_ID", output->mode_blob_id),
+ object_property_set_named(output, plane, "FB_ID", fb_id),
+ object_property_set_named(output, plane, "CRTC_ID", output->crtc_id),
+ object_property_set_named(output, plane, "CRTC_X", 0),
+ object_property_set_named(output, plane, "CRTC_Y", 0),
+ object_property_set_named(output, plane, "CRTC_W", output->mode.hdisplay),
+ object_property_set_named(output, plane, "CRTC_H", output->mode.vdisplay),
+ object_property_set_named(output, plane, "SRC_X", 0),
+ object_property_set_named(output, plane, "SRC_Y", 0),
+ object_property_set_named(output, plane, "SRC_W", output->mode.hdisplay << 16),
+ object_property_set_named(output, plane, "SRC_H", output->mode.vdisplay << 16),
+ };
+ int err;
+
+ /* Reset atomic state */
+ drmModeAtomicSetCursor(output->atomic_state, 0);
+
+ err = atomic_state_add_object_properties(output->atomic_state, obj_props,
+ ARRAY_SIZE(obj_props));
+ if (err)
+ return false;
+
+ return drmModeAtomicCommit(fd, output->atomic_state,
+ DRM_MODE_ATOMIC_ALLOW_MODESET, NULL);
+}
+
+static bool
+display_output_init(int fd, struct display_output *output, bool use_atomic)
+{
+ drmModeRes *resources;
+ drmModeConnector *connector;
+ drmModeCrtc *crtc;
+ drmModePlane *plane;
+ unsigned mode_idx;
+
+ resources = drmModeGetResources(fd);
+ if (!resources)
+ return false;
+
+ connector = connector_get(fd, resources);
+ if (!connector)
+ goto err_free_resources;
+
+ crtc = crtc_get_for_connector(fd, resources, connector);
+ if (!crtc)
+ goto err_free_connector;
+
+ plane = primary_plane_get_for_crtc(fd, resources, crtc);
+ if (!plane)
+ goto err_free_crtc;
+
+ mode_idx = connector_choose_mode(connector);
+ if (mode_idx < 0)
+ goto err_free_plane;
+ output->mode = connector->modes[mode_idx];
+
+ /* Record the display supported formats */
+ for (unsigned i = 0; i < plane->count_formats; i++) {
+ int format_idx;
+
+ format_idx = format_idx_get_from_drm_format(plane->formats[i]);
+ if (format_idx == -1)
+ continue;
+
+ output->formats |= (1 << format_idx);
+ }
+ if (!output->formats)
+ goto err_free_plane;
+
+ output->connector_id = connector->connector_id;
+ output->crtc_id = crtc->crtc_id;
+ output->plane_id = plane->plane_id;
+
+ drmModeFreePlane(plane);
+ drmModeFreeCrtc(crtc);
+ drmModeFreeConnector(connector);
+ drmModeFreeResources(resources);
+
+ if (use_atomic) {
+ if (!display_output_atomic_init(fd, output)) {
+ _eglLog(_EGL_DEBUG,
+ "failed to initialise atomic support (using legacy mode)");
+ }
+ }
+
+ return true;
+
+err_free_plane:
+ drmModeFreePlane(plane);
+err_free_crtc:
+ drmModeFreeCrtc(crtc);
+err_free_connector:
+ drmModeFreeConnector(connector);
+err_free_resources:
+ drmModeFreeResources(resources);
+ return false;
+}
+
+static int
+display_output_flip(int fd, struct display_output *output, uint32_t fb_id,
+ uint32_t flags, void *flip_data)
+{
+ if (output->atomic_state)
+ return display_output_atomic_flip(fd, output, fb_id, flags, flip_data);
+
+ return drmModePageFlip(fd, output->crtc_id, fb_id, flags, flip_data);
+}
+
+static int
+display_output_modeset(int fd, struct display_output *output, uint32_t fb_id)
+{
+ if (output->atomic_state)
+ return display_output_atomic_modeset(fd, output, fb_id);
+
+ return drmModeSetCrtc(fd, output->crtc_id, fb_id, 0, 0,
+ &output->connector_id, 1, &output->mode);
+}
+
+static bool
+add_fb_for_dri_image(struct dri2_egl_display *dri2_dpy, __DRIimage *image,
+ uint32_t *fb_id_out)
+{
+ uint32_t handles[4] = {0};
+ uint32_t pitches[4] = {0};
+ uint32_t offsets[4] = {0};
+ int handle, stride, width, height, format;
+ int format_idx;
+
+ dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_HANDLE, &handle);
+ dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_STRIDE, &stride);
+ dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_WIDTH, &width);
+ dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_HEIGHT, &height);
+ dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_FORMAT, &format);
+
+ handles[0] = (uint32_t) handle;
+ pitches[0] = (uint32_t) stride;
+
+ format_idx = format_idx_get_from_dri_image_format(format);
+ assert(format_idx != -1);
+
+ return !drmModeAddFB2(dri2_dpy->fd, width, height,
+ dri2_null_formats[format_idx].drm_format,
+ handles, pitches, offsets, fb_id_out, 0);
+}
+
+static bool
+get_front_bo(struct dri2_egl_surface *dri2_surf)
+{
+ struct dri2_egl_display *dri2_dpy =
+ dri2_egl_display(dri2_surf->base.Resource.Display);
+ unsigned int use = 0;
+
+ if (dri2_surf->base.Type == EGL_WINDOW_BIT)
+ use |= __DRI_IMAGE_USE_SCANOUT;
+
+ dri2_surf->front = dri2_dpy->image->createImage(dri2_dpy->dri_screen,
+ dri2_surf->base.Width,
+ dri2_surf->base.Height,
+ dri2_surf->format,
+ use,
+ NULL);
+ if (!dri2_surf->front)
+ return false;
+
+ if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
+ if (!add_fb_for_dri_image(dri2_dpy, dri2_surf->front,
+ &dri2_surf->front_fb_id)) {
+ dri2_dpy->image->destroyImage(dri2_surf->front);
+ dri2_surf->front = NULL;
+ return false;
+ }
+ }
+
+ return true;
+}
+
+static bool
+get_back_bo(struct dri2_egl_surface *dri2_surf)
+{
+ struct dri2_egl_display *dri2_dpy =
+ dri2_egl_display(dri2_surf->base.Resource.Display);
+
+ if (!dri2_surf->back) {
+ for (unsigned i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
+ if (!dri2_surf->color_buffers[i].locked) {
+ dri2_surf->back = &dri2_surf->color_buffers[i];
+ break;
+ }
+ }
+ if (!dri2_surf->back)
+ return false;
+ }
+
+ if (!dri2_surf->back->dri_image) {
+ dri2_surf->back->dri_image =
+ dri2_dpy->image->createImage(dri2_dpy->dri_screen,
+ dri2_surf->base.Width,
+ dri2_surf->base.Height,
+ dri2_surf->format,
+ __DRI_IMAGE_USE_SCANOUT,
+ NULL);
+ if (!dri2_surf->back->dri_image)
+ return false;
+ }
+
+ if (!dri2_surf->back->fb_id) {
+ if (!add_fb_for_dri_image(dri2_dpy, dri2_surf->back->dri_image,
+ &dri2_surf->back->fb_id)) {
+ return false;
+ }
+ }
+
+ dri2_surf->back->locked = 1;
+
+ return true;
+}
+
+static _EGLSurface *
+create_surface(_EGLDisplay *disp, _EGLConfig *config, EGLint type,
+ const EGLint *attrib_list)
+{
+ struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
+ struct dri2_egl_config *dri2_config = dri2_egl_config(config);
+ struct dri2_egl_surface *dri2_surf;
+ const __DRIconfig *dri_config;
+ _EGLSurface *surf;
+ int format_idx;
+
+ dri2_surf = calloc(1, sizeof(*dri2_surf));
+ if (!dri2_surf) {
+ _eglError(EGL_BAD_ALLOC, "failed to create surface");
+ return NULL;
+ }
+ surf = &dri2_surf->base;
+
+ if (!dri2_init_surface(surf, disp, type, config, attrib_list, false, NULL))
+ goto err_free_surface;
+
+ dri_config = dri2_get_dri_config(dri2_config, type,
+ dri2_surf->base.GLColorspace);
+ if (!dri_config) {
+ _eglError(EGL_BAD_MATCH, "Unsupported surfacetype/colorspace configuration");
+ goto err_free_surface;
+ }
+
+ dri2_surf->dri_drawable =
+ dri2_dpy->image_driver->createNewDrawable(dri2_dpy->dri_screen,
+ dri_config, dri2_surf);
+ if (!dri2_surf->dri_drawable) {
+ _eglError(EGL_BAD_ALLOC, "failed to create drawable");
+ goto err_free_surface;
+ }
+
+ format_idx = format_idx_get_from_config(dri2_dpy, dri_config);
+ assert(format_idx != -1);
+
+ dri2_surf->format = dri2_null_formats[format_idx].dri_image_format;
+
+ return surf;
+
+err_free_surface:
+ free(dri2_surf);
+ return NULL;
+}
+
+static void
+destroy_surface(_EGLSurface *surf)
+{
+ struct dri2_egl_display *dri2_dpy = dri2_egl_display(surf->Resource.Display);
+ struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
+
+ dri2_dpy->core->destroyDrawable(dri2_surf->dri_drawable);
+ dri2_fini_surface(surf);
+ free(surf);
+}
+
+static EGLBoolean
+dri2_null_destroy_surface(_EGLDisplay *disp, _EGLSurface *surf);
+
+static _EGLSurface *
+dri2_null_create_window_surface(_EGLDisplay *disp, _EGLConfig *config,
+ void *native_window, const EGLint *attrib_list)