-
Notifications
You must be signed in to change notification settings - Fork 0
/
wsi-termux-x11-v4.patch
1016 lines (978 loc) · 35.7 KB
/
wsi-termux-x11-v4.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
diff --git a/src/freedreno/vulkan/tu_knl_kgsl.cc b/src/freedreno/vulkan/tu_knl_kgsl.cc
index ed0cbff44cd..accf032766c 100644
--- a/src/freedreno/vulkan/tu_knl_kgsl.cc
+++ b/src/freedreno/vulkan/tu_knl_kgsl.cc
@@ -11,7 +11,6 @@
#include <stdint.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
-
#include "msm_kgsl.h"
#include "vk_util.h"
@@ -25,6 +24,7 @@
#include "tu_device.h"
#include "tu_dynamic_rendering.h"
+
static int
safe_ioctl(int fd, unsigned long request, void *arg)
{
@@ -173,7 +173,6 @@ static int
kgsl_bo_export_dmabuf(struct tu_device *dev, struct tu_bo *bo)
{
tu_stub();
-
return -1;
}
diff --git a/src/freedreno/vulkan/tu_wsi.cc b/src/freedreno/vulkan/tu_wsi.cc
index 9929993bbe0..e2559c553e0 100644
--- a/src/freedreno/vulkan/tu_wsi.cc
+++ b/src/freedreno/vulkan/tu_wsi.cc
@@ -14,6 +14,18 @@
#include "tu_device.h"
+static void
+kgsl_get_info(VkPhysicalDevice _pdevice,
+ VkDeviceMemory _memory,
+ int *fd,
+ uint32_t *offset)
+{
+ TU_FROM_HANDLE(tu_physical_device, pdevice, _pdevice);
+ TU_FROM_HANDLE(tu_device_memory, memory, _memory);
+ *fd = pdevice->local_fd;
+ *offset = memory->bo->gem_handle << 12;
+}
+
static VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
tu_wsi_proc_addr(VkPhysicalDevice physicalDevice, const char *pName)
{
@@ -24,9 +36,7 @@ tu_wsi_proc_addr(VkPhysicalDevice physicalDevice, const char *pName)
static bool
tu_wsi_can_present_on_device(VkPhysicalDevice physicalDevice, int fd)
{
- TU_FROM_HANDLE(tu_physical_device, pdevice, physicalDevice);
-
- return wsi_common_drm_devices_equal(fd, pdevice->local_fd);
+ return true;
}
VkResult
@@ -45,6 +55,11 @@ tu_wsi_init(struct tu_physical_device *physical_device)
if (result != VK_SUCCESS)
return result;
+ if (strcmp(physical_device->instance->knl->name, "kgsl") == 0) {
+ physical_device->wsi_device.kgsl_get_info = kgsl_get_info;
+ physical_device->wsi_device.is_tu_kgsl = true;
+ }
+
physical_device->wsi_device.supports_modifiers = true;
physical_device->wsi_device.can_present_on_device =
tu_wsi_can_present_on_device;
diff --git a/src/util/libdrm.h b/src/util/libdrm.h
index 900f2f3b6bb..a290e685a9e 100644
--- a/src/util/libdrm.h
+++ b/src/util/libdrm.h
@@ -31,6 +31,11 @@
#include <xf86drm.h>
#else
+#ifdef __cplusplus
+#include <cstddef>
+#include <cstdint>
+#endif
+
#include <errno.h>
#define DRM_NODE_PRIMARY 0
diff --git a/src/vulkan/runtime/vk_image.c b/src/vulkan/runtime/vk_image.c
index 48e00dc8163..5e7a4546211 100644
--- a/src/vulkan/runtime/vk_image.c
+++ b/src/vulkan/runtime/vk_image.c
@@ -448,7 +448,7 @@ vk_image_view_init(struct vk_device *device,
assert(image_view->format == image->format);
if (!(image->create_flags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT))
- assert(image_view->format == image->format);
+ //assert(image_view->format == image->format);
/* Restrict the format to only the planes chosen.
*
diff --git a/src/vulkan/wsi/wsi_common.c b/src/vulkan/wsi/wsi_common.c
index 27fd4edeffa..5d7dd8f7156 100644
--- a/src/vulkan/wsi/wsi_common.c
+++ b/src/vulkan/wsi/wsi_common.c
@@ -39,6 +39,16 @@
#include "vk_sync_dummy.h"
#include "vk_util.h"
+#ifndef HAVE_LIBDRM
+
+#include "drm-uapi/drm_fourcc.h"
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <linux/dma-heap.h>
+#include <fcntl.h>
+
+#endif
+
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
@@ -49,6 +59,494 @@
uint64_t WSI_DEBUG;
+#ifndef HAVE_LIBDRM
+
+
+#define WSI_PRIME_LINEAR_STRIDE_ALIGN 256
+
+bool
+wsi_drm_image_needs_buffer_blit(const struct wsi_device *wsi,
+const struct wsi_drm_image_params *params)
+{
+ if (!params->same_gpu)
+ return true;
+ if (params->num_modifier_lists > 0 || wsi->supports_scanout)
+ return false;
+
+ return true;
+}
+
+static uint32_t
+prime_select_buffer_memory_type(const struct wsi_device *wsi,
+ uint32_t type_bits)
+{
+ return wsi_select_memory_type(wsi, 0 /* req_props */,
+ VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
+ type_bits);
+}
+
+static VkResult
+wsi_init_image_dmabuf_fd(const struct wsi_swapchain *chain,
+ struct wsi_image *image,
+ bool linear)
+{
+ const struct wsi_device *wsi = chain->wsi;
+ const VkMemoryGetFdInfoKHR memory_get_fd_info = {
+ .sType = VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR,
+ .pNext = NULL,
+ .memory = linear ? image->blit.memory : image->memory,
+ .handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
+ };
+
+ return wsi->GetMemoryFdKHR(chain->device, &memory_get_fd_info,
+ &image->dma_buf_fd);
+}
+
+static VkResult
+wsi_create_prime_image_mem(const struct wsi_swapchain *chain,
+ const struct wsi_image_info *info,
+ struct wsi_image *image)
+{
+ VkResult result = wsi_create_buffer_blit_context(chain, info, image,VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,true);
+ if (result != VK_SUCCESS)
+ return result;
+
+ result = wsi_init_image_dmabuf_fd(chain, image, true);
+ if (result != VK_SUCCESS)
+ return result;
+
+ image->drm_modifier = info->prime_use_linear_modifier ?
+ DRM_FORMAT_MOD_LINEAR : DRM_FORMAT_MOD_INVALID;
+
+ return VK_SUCCESS;
+}
+
+static VkResult
+wsi_configure_prime_image(UNUSED const struct wsi_swapchain *chain,
+ const VkSwapchainCreateInfoKHR *pCreateInfo,
+ bool use_modifier,
+ wsi_memory_type_select_cb select_buffer_memory_type,
+ struct wsi_image_info *info)
+{
+ VkResult result = wsi_configure_image(chain, pCreateInfo,
+ 0 /* handle_types */, info);
+ if (result != VK_SUCCESS)
+ return result;
+
+ wsi_configure_buffer_image(chain, pCreateInfo,
+ WSI_PRIME_LINEAR_STRIDE_ALIGN, 4096,
+ info);
+ info->prime_use_linear_modifier = use_modifier;
+
+ info->create_mem = wsi_create_prime_image_mem;
+ info->select_blit_dst_memory_type = select_buffer_memory_type;
+ info->select_image_memory_type = wsi_select_device_memory_type;
+
+ return VK_SUCCESS;
+}
+
+static const struct VkDrmFormatModifierPropertiesEXT *
+get_modifier_props(const struct wsi_image_info *info, uint64_t modifier)
+{
+ for (uint32_t i = 0; i < info->modifier_prop_count; i++) {
+ if (info->modifier_props[i].drmFormatModifier == modifier)
+ return &info->modifier_props[i];
+ }
+ return NULL;
+}
+
+static VkResult
+wsi_create_native_image_mem(const struct wsi_swapchain *chain,
+ const struct wsi_image_info *info,
+ struct wsi_image *image)
+{
+ const struct wsi_device *wsi = chain->wsi;
+ VkResult result;
+
+ VkMemoryRequirements reqs;
+ wsi->GetImageMemoryRequirements(chain->device, image->image, &reqs);
+
+ const struct wsi_memory_allocate_info memory_wsi_info = {
+ .sType = VK_STRUCTURE_TYPE_WSI_MEMORY_ALLOCATE_INFO_MESA,
+ .pNext = NULL,
+ .implicit_sync = true,
+ };
+ const VkExportMemoryAllocateInfo memory_export_info = {
+ .sType = VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO,
+ .pNext = &memory_wsi_info,
+ .handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
+ };
+ const VkMemoryDedicatedAllocateInfo memory_dedicated_info = {
+ .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO,
+ .pNext = &memory_export_info,
+ .image = image->image,
+ .buffer = VK_NULL_HANDLE,
+ };
+ const VkMemoryAllocateInfo memory_info = {
+ .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
+ .pNext = &memory_dedicated_info,
+ .allocationSize = reqs.size,
+ .memoryTypeIndex =
+ wsi_select_device_memory_type(wsi, reqs.memoryTypeBits),
+ };
+ result = wsi->AllocateMemory(chain->device, &memory_info,
+ &chain->alloc, &image->memory);
+ if (result != VK_SUCCESS)
+ return result;
+
+ result = wsi_init_image_dmabuf_fd(chain, image, false);
+ if (result != VK_SUCCESS)
+ return result;
+
+ if (info->drm_mod_list.drmFormatModifierCount > 0) {
+ VkImageDrmFormatModifierPropertiesEXT image_mod_props = {
+ .sType = VK_STRUCTURE_TYPE_IMAGE_DRM_FORMAT_MODIFIER_PROPERTIES_EXT,
+ };
+ result = wsi->GetImageDrmFormatModifierPropertiesEXT(chain->device,
+ image->image,
+ &image_mod_props);
+ if (result != VK_SUCCESS)
+ return result;
+
+ image->drm_modifier = image_mod_props.drmFormatModifier;
+ assert(image->drm_modifier != DRM_FORMAT_MOD_INVALID);
+
+ const struct VkDrmFormatModifierPropertiesEXT *mod_props =
+ get_modifier_props(info, image->drm_modifier);
+ image->num_planes = mod_props->drmFormatModifierPlaneCount;
+
+ for (uint32_t p = 0; p < image->num_planes; p++) {
+ const VkImageSubresource image_subresource = {
+ .aspectMask = VK_IMAGE_ASPECT_PLANE_0_BIT << p,
+ .mipLevel = 0,
+ .arrayLayer = 0,
+ };
+ VkSubresourceLayout image_layout;
+ wsi->GetImageSubresourceLayout(chain->device, image->image,
+ &image_subresource, &image_layout);
+ image->sizes[p] = image_layout.size;
+ image->row_pitches[p] = image_layout.rowPitch;
+ image->offsets[p] = image_layout.offset;
+ }
+ } else {
+ const VkImageSubresource image_subresource = {
+ .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
+ .mipLevel = 0,
+ .arrayLayer = 0,
+ };
+ VkSubresourceLayout image_layout;
+ wsi->GetImageSubresourceLayout(chain->device, image->image,
+ &image_subresource, &image_layout);
+
+ image->drm_modifier = DRM_FORMAT_MOD_INVALID;
+ image->num_planes = 1;
+ image->sizes[0] = reqs.size;
+ image->row_pitches[0] = image_layout.rowPitch;
+ image->offsets[0] = 0;
+ }
+
+ return VK_SUCCESS;
+}
+
+static int
+dma_heap_alloc(uint64_t size)
+{
+ int fd = -1, heap = open("/dev/dma_heap/system", O_RDONLY);
+ if (heap < 0)
+ goto fail_open;
+ struct dma_heap_allocation_data alloc_data = {.len = size, .fd_flags = O_RDWR | O_CLOEXEC};
+ if (ioctl(heap, DMA_HEAP_IOCTL_ALLOC, &alloc_data) < 0)
+ goto fail_alloc;
+ fd = alloc_data.fd;
+fail_alloc:
+ close(heap);
+fail_open:
+ return fd;
+}
+
+
+static int ion_alloc(uint64_t size) {
+ int fd = -1, ion_dev = open("/dev/ion", O_RDONLY);
+ if (ion_dev < 0)
+ goto fail_open;
+ struct ion_allocation_data {
+ __u64 len;
+ __u32 heap_id_mask;
+ __u32 flags;
+ __u32 fd;
+ __u32 unused;
+ } alloc_data = {
+ .len = size,
+ /* ION_HEAP_SYSTEM | ION_SYSTEM_HEAP_ID */
+ .heap_id_mask = (1U << 0) | (1U << 25),
+ .flags = 0, /* uncached */
+ };
+ if (ioctl(ion_dev, _IOWR('I', 0, struct ion_allocation_data), &alloc_data) <
+ 0)
+ goto fail_alloc;
+ fd = alloc_data.fd;
+fail_alloc:
+ close(ion_dev);
+fail_open:
+ return fd;
+};
+
+static VkResult
+wsi_create_kgsl_image_mem(const struct wsi_swapchain *chain,
+ const struct wsi_image_info *info,
+ struct wsi_image *image)
+{
+ const struct wsi_device *wsi = chain->wsi;
+ VkResult result;
+
+ VkMemoryRequirements reqs;
+ wsi->GetImageMemoryRequirements(chain->device, image->image, &reqs);
+
+ if (debug_get_bool_option("USE_HEAP", true)) {
+ image->dma_buf_fd = dma_heap_alloc(reqs.size);
+ if (image->dma_buf_fd < 0)
+ image->dma_buf_fd = ion_alloc(reqs.size);
+ }
+
+ const struct wsi_memory_allocate_info memory_wsi_info = {
+ .sType = VK_STRUCTURE_TYPE_WSI_MEMORY_ALLOCATE_INFO_MESA,
+ .pNext = NULL,
+ .implicit_sync = true,
+ };
+ const VkImportMemoryFdInfoKHR memory_import_info = {
+ .sType = VK_STRUCTURE_TYPE_IMPORT_MEMORY_FD_INFO_KHR,
+ .pNext = &memory_wsi_info,
+ .fd = os_dupfd_cloexec(image->dma_buf_fd),
+ .handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
+ };
+ const VkMemoryDedicatedAllocateInfo memory_dedicated_info = {
+ .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO,
+ .pNext = (image->dma_buf_fd < 0) ? &memory_wsi_info : &memory_import_info,
+ .image = image->image,
+ .buffer = VK_NULL_HANDLE,
+ };
+ const VkMemoryAllocateInfo memory_info = {
+ .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
+ .pNext = &memory_dedicated_info,
+ .allocationSize = reqs.size,
+ .memoryTypeIndex =
+ wsi_select_device_memory_type(wsi, reqs.memoryTypeBits),
+ };
+ result = wsi->AllocateMemory(chain->device, &memory_info,
+ &chain->alloc, &image->memory);
+ if (result != VK_SUCCESS)
+ return result;
+
+ uint32_t dma_buf_offset = 0;
+ if (image->dma_buf_fd == -1)
+ wsi->kgsl_get_info(wsi->pdevice, image->memory, &image->dma_buf_fd,
+ &dma_buf_offset);
+
+ image->cpu_map = mmap(0, reqs.size, PROT_READ | PROT_WRITE, MAP_SHARED,
+ image->dma_buf_fd, dma_buf_offset);
+
+ if (image->cpu_map == MAP_FAILED)
+ return VK_ERROR_OUT_OF_HOST_MEMORY;
+ munmap(image->cpu_map, reqs.size);
+
+ const VkImageSubresource image_subresource = {
+ .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
+ .mipLevel = 0,
+ .arrayLayer = 0,
+ };
+ VkSubresourceLayout image_layout;
+ wsi->GetImageSubresourceLayout(chain->device, image->image,
+ &image_subresource, &image_layout);
+
+ image->drm_modifier = 1274; /* termux-x11's RAW_MMAPPABLE_FD */
+ image->num_planes = 1;
+ image->sizes[0] = reqs.size;
+ image->row_pitches[0] = image_layout.rowPitch;
+ image->offsets[0] = dma_buf_offset;
+
+ return VK_SUCCESS;
+}
+
+
+
+
+static VkResult
+wsi_configure_native_image(const struct wsi_swapchain *chain,
+ const VkSwapchainCreateInfoKHR *pCreateInfo,
+ uint32_t num_modifier_lists,
+ const uint32_t *num_modifiers,
+ const uint64_t *const *modifiers,
+ struct wsi_image_info *info)
+{
+ const struct wsi_device *wsi = chain->wsi;
+
+ VkExternalMemoryHandleTypeFlags handle_type =
+ VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT;
+
+ VkResult result = wsi_configure_image(chain, pCreateInfo, handle_type, info);
+ if (result != VK_SUCCESS)
+ return result;
+
+ if (num_modifier_lists == 0) {
+ /* If we don't have modifiers, fall back to the legacy "scanout" flag */
+ info->wsi.scanout = true;
+ } else {
+ /* The winsys can't request modifiers if we don't support them. */
+ assert(wsi->supports_modifiers);
+ struct VkDrmFormatModifierPropertiesListEXT modifier_props_list = {
+ .sType = VK_STRUCTURE_TYPE_DRM_FORMAT_MODIFIER_PROPERTIES_LIST_EXT,
+ };
+ VkFormatProperties2 format_props = {
+ .sType = VK_STRUCTURE_TYPE_FORMAT_PROPERTIES_2,
+ .pNext = &modifier_props_list,
+ };
+ wsi->GetPhysicalDeviceFormatProperties2KHR(wsi->pdevice,
+ pCreateInfo->imageFormat,
+ &format_props);
+ assert(modifier_props_list.drmFormatModifierCount > 0);
+ info->modifier_props =
+ vk_alloc(&chain->alloc,
+ sizeof(*info->modifier_props) *
+ modifier_props_list.drmFormatModifierCount,
+ 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
+ if (info->modifier_props == NULL)
+ goto fail_oom;
+
+ modifier_props_list.pDrmFormatModifierProperties = info->modifier_props;
+ wsi->GetPhysicalDeviceFormatProperties2KHR(wsi->pdevice,
+ pCreateInfo->imageFormat,
+ &format_props);
+
+ /* Call GetImageFormatProperties with every modifier and filter the list
+ * down to those that we know work.
+ */
+ info->modifier_prop_count = 0;
+ for (uint32_t i = 0; i < modifier_props_list.drmFormatModifierCount; i++) {
+ VkPhysicalDeviceImageDrmFormatModifierInfoEXT mod_info = {
+ .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_DRM_FORMAT_MODIFIER_INFO_EXT,
+ .drmFormatModifier = info->modifier_props[i].drmFormatModifier,
+ .sharingMode = pCreateInfo->imageSharingMode,
+ .queueFamilyIndexCount = pCreateInfo->queueFamilyIndexCount,
+ .pQueueFamilyIndices = pCreateInfo->pQueueFamilyIndices,
+ };
+ VkPhysicalDeviceImageFormatInfo2 format_info = {
+ .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2,
+ .format = pCreateInfo->imageFormat,
+ .type = VK_IMAGE_TYPE_2D,
+ .tiling = VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT,
+ .usage = pCreateInfo->imageUsage,
+ .flags = info->create.flags,
+ };
+
+ VkImageFormatListCreateInfo format_list;
+ if (info->create.flags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT) {
+ format_list = info->format_list;
+ format_list.pNext = NULL;
+ __vk_append_struct(&format_info, &format_list);
+ }
+
+ struct wsi_image_create_info wsi_info = (struct wsi_image_create_info) {
+ .sType = VK_STRUCTURE_TYPE_WSI_IMAGE_CREATE_INFO_MESA,
+ .pNext = NULL,
+ };
+ __vk_append_struct(&format_info, &wsi_info);
+
+ VkImageFormatProperties2 format_props = {
+ .sType = VK_STRUCTURE_TYPE_IMAGE_FORMAT_PROPERTIES_2,
+ .pNext = NULL,
+ };
+ __vk_append_struct(&format_info, &mod_info);
+ result = wsi->GetPhysicalDeviceImageFormatProperties2(wsi->pdevice,
+ &format_info,
+ &format_props);
+ if (result == VK_SUCCESS &&
+ pCreateInfo->imageExtent.width <= format_props.imageFormatProperties.maxExtent.width &&
+ pCreateInfo->imageExtent.height <= format_props.imageFormatProperties.maxExtent.height)
+ info->modifier_props[info->modifier_prop_count++] = info->modifier_props[i];
+ }
+
+ uint32_t max_modifier_count = 0;
+ for (uint32_t l = 0; l < num_modifier_lists; l++)
+ max_modifier_count = MAX2(max_modifier_count, num_modifiers[l]);
+
+ uint64_t *image_modifiers =
+ vk_alloc(&chain->alloc, sizeof(*image_modifiers) * max_modifier_count,
+ 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
+ if (!image_modifiers)
+ goto fail_oom;
+
+ uint32_t image_modifier_count = 0;
+ for (uint32_t l = 0; l < num_modifier_lists; l++) {
+ /* Walk the modifier lists and construct a list of supported
+ * modifiers.
+ */
+ for (uint32_t i = 0; i < num_modifiers[l]; i++) {
+ if (get_modifier_props(info, modifiers[l][i]))
+ image_modifiers[image_modifier_count++] = modifiers[l][i];
+ }
+
+ /* We only want to take the modifiers from the first list */
+ if (image_modifier_count > 0)
+ break;
+ }
+
+ if (image_modifier_count > 0) {
+ info->create.tiling = VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT;
+ info->drm_mod_list = (VkImageDrmFormatModifierListCreateInfoEXT) {
+ .sType = VK_STRUCTURE_TYPE_IMAGE_DRM_FORMAT_MODIFIER_LIST_CREATE_INFO_EXT,
+ .drmFormatModifierCount = image_modifier_count,
+ .pDrmFormatModifiers = image_modifiers,
+ };
+ image_modifiers = NULL;
+ __vk_append_struct(&info->create, &info->drm_mod_list);
+ } else {
+ vk_free(&chain->alloc, image_modifiers);
+ /* TODO: Add a proper error here */
+ assert(!"Failed to find a supported modifier! This should never "
+ "happen because LINEAR should always be available");
+ goto fail_oom;
+ }
+ }
+
+ if (wsi->is_tu_kgsl)
+ info->create_mem = wsi_create_kgsl_image_mem;
+ else
+ info->create_mem = wsi_create_native_image_mem;
+
+ return VK_SUCCESS;
+
+fail_oom:
+ wsi_destroy_image_info(chain, info);
+ return VK_ERROR_OUT_OF_HOST_MEMORY;
+}
+
+
+VkResult
+wsi_drm_configure_image(const struct wsi_swapchain *chain,
+ const VkSwapchainCreateInfoKHR *pCreateInfo,
+ const struct wsi_drm_image_params *params,
+ struct wsi_image_info *info)
+{
+ assert(params->base.image_type == WSI_IMAGE_TYPE_DRM);
+
+ if (chain->blit.type == WSI_SWAPCHAIN_BUFFER_BLIT) {
+ bool use_modifier = params->num_modifier_lists > 0;
+ wsi_memory_type_select_cb select_buffer_memory_type =
+ params->same_gpu ? wsi_select_device_memory_type :
+ prime_select_buffer_memory_type;
+ return wsi_configure_prime_image(chain, pCreateInfo, use_modifier,
+ select_buffer_memory_type, info);
+ } else {
+ return wsi_configure_native_image(chain, pCreateInfo,
+ params->num_modifier_lists,
+ params->num_modifiers,
+ params->modifiers,
+ info);
+ }
+}
+
+#endif
+
static const struct debug_control debug_control[] = {
{ "buffer", WSI_DEBUG_BUFFER },
{ "sw", WSI_DEBUG_SW },
@@ -310,11 +808,13 @@ wsi_device_setup_syncobj_fd(struct wsi_device *wsi_device,
#endif
}
+
static enum wsi_swapchain_blit_type
get_blit_type(const struct wsi_device *wsi,
const struct wsi_base_image_params *params,
VkDevice device)
{
+
switch (params->image_type) {
case WSI_IMAGE_TYPE_CPU: {
const struct wsi_cpu_image_params *cpu_params =
@@ -322,14 +822,13 @@ get_blit_type(const struct wsi_device *wsi,
return wsi_cpu_image_needs_buffer_blit(wsi, cpu_params) ?
WSI_SWAPCHAIN_BUFFER_BLIT : WSI_SWAPCHAIN_NO_BLIT;
}
-#ifdef HAVE_LIBDRM
+
case WSI_IMAGE_TYPE_DRM: {
const struct wsi_drm_image_params *drm_params =
container_of(params, const struct wsi_drm_image_params, base);
return wsi_drm_image_needs_buffer_blit(wsi, drm_params) ?
WSI_SWAPCHAIN_BUFFER_BLIT : WSI_SWAPCHAIN_NO_BLIT;
}
-#endif
#ifdef _WIN32
case WSI_IMAGE_TYPE_DXGI: {
const struct wsi_dxgi_image_params *dxgi_params =
@@ -340,6 +839,7 @@ get_blit_type(const struct wsi_device *wsi,
default:
unreachable("Invalid image type");
}
+
}
static VkResult
@@ -354,13 +854,11 @@ configure_image(const struct wsi_swapchain *chain,
container_of(params, const struct wsi_cpu_image_params, base);
return wsi_configure_cpu_image(chain, pCreateInfo, cpu_params, info);
}
-#ifdef HAVE_LIBDRM
case WSI_IMAGE_TYPE_DRM: {
const struct wsi_drm_image_params *drm_params =
container_of(params, const struct wsi_drm_image_params, base);
return wsi_drm_configure_image(chain, pCreateInfo, drm_params, info);
}
-#endif
#ifdef _WIN32
case WSI_IMAGE_TYPE_DXGI: {
const struct wsi_dxgi_image_params *dxgi_params =
@@ -370,9 +868,12 @@ configure_image(const struct wsi_swapchain *chain,
#endif
default:
unreachable("Invalid image type");
+
}
}
+
+
#if defined(HAVE_PTHREAD) && !defined(_WIN32)
bool
wsi_init_pthread_cond_monotonic(pthread_cond_t *cond)
@@ -2075,7 +2576,7 @@ wsi_configure_cpu_image(const struct wsi_swapchain *chain,
const struct wsi_cpu_image_params *params,
struct wsi_image_info *info)
{
- assert(params->base.image_type == WSI_IMAGE_TYPE_CPU);
+// assert(params->base.image_type == WSI_IMAGE_TYPE_CPU);
assert(chain->blit.type == WSI_SWAPCHAIN_NO_BLIT ||
chain->blit.type == WSI_SWAPCHAIN_BUFFER_BLIT);
diff --git a/src/vulkan/wsi/wsi_common.h b/src/vulkan/wsi/wsi_common.h
index 8fe8fb9a748..2b2129c427b 100644
--- a/src/vulkan/wsi/wsi_common.h
+++ b/src/vulkan/wsi/wsi_common.h
@@ -171,6 +171,7 @@ struct wsi_device {
} win32;
bool sw;
+ bool is_tu_kgsl;
/* Set to true if the implementation is ok with linear WSI images. */
bool wants_linear;
@@ -224,6 +225,11 @@ struct wsi_device {
*/
VkQueue (*get_blit_queue)(VkDevice device);
+ void (*kgsl_get_info)(VkPhysicalDevice _pdevice,
+ VkDeviceMemory _memory,
+ int *fd,
+ uint32_t *offset);
+
#define WSI_CB(cb) PFN_vk##cb cb
WSI_CB(AllocateMemory);
WSI_CB(AllocateCommandBuffers);
diff --git a/src/vulkan/wsi/wsi_common_drm.c b/src/vulkan/wsi/wsi_common_drm.c
index b53d485ef3a..da6afac2376 100644
--- a/src/vulkan/wsi/wsi_common_drm.c
+++ b/src/vulkan/wsi/wsi_common_drm.c
@@ -39,6 +39,10 @@
#include <stdlib.h>
#include <stdio.h>
#include <xf86drm.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <linux/dma-heap.h>
static VkResult
wsi_dma_buf_export_sync_file(int dma_buf_fd, int *sync_file_fd)
@@ -54,7 +58,7 @@ wsi_dma_buf_export_sync_file(int dma_buf_fd, int *sync_file_fd)
};
int ret = drmIoctl(dma_buf_fd, DMA_BUF_IOCTL_EXPORT_SYNC_FILE, &export);
if (ret) {
- if (errno == ENOTTY || errno == EBADF || errno == ENOSYS) {
+ if (errno == ENOTTY || errno == EBADF || errno == ENOSYS || errno == ENODEV) {
no_dma_buf_sync_file = true;
return VK_ERROR_FEATURE_NOT_PRESENT;
} else {
@@ -82,7 +86,7 @@ wsi_dma_buf_import_sync_file(int dma_buf_fd, int sync_file_fd)
};
int ret = drmIoctl(dma_buf_fd, DMA_BUF_IOCTL_IMPORT_SYNC_FILE, &import);
if (ret) {
- if (errno == ENOTTY || errno == EBADF || errno == ENOSYS) {
+ if (errno == ENOTTY || errno == EBADF || errno == ENOSYS || errno == ENODEV) {
no_dma_buf_sync_file = true;
return VK_ERROR_FEATURE_NOT_PRESENT;
} else {
@@ -306,6 +310,11 @@ wsi_create_native_image_mem(const struct wsi_swapchain *chain,
const struct wsi_image_info *info,
struct wsi_image *image);
+static VkResult
+wsi_create_kgsl_image_mem(const struct wsi_swapchain *chain,
+ const struct wsi_image_info *info,
+ struct wsi_image *image);
+
static VkResult
wsi_configure_native_image(const struct wsi_swapchain *chain,
const VkSwapchainCreateInfoKHR *pCreateInfo,
@@ -444,7 +453,10 @@ wsi_configure_native_image(const struct wsi_swapchain *chain,
}
}
- info->create_mem = wsi_create_native_image_mem;
+ if (wsi->is_tu_kgsl)
+ info->create_mem = wsi_create_kgsl_image_mem;
+ else
+ info->create_mem = wsi_create_native_image_mem;
return VK_SUCCESS;
@@ -563,6 +575,124 @@ wsi_create_native_image_mem(const struct wsi_swapchain *chain,
return VK_SUCCESS;
}
+static int
+dma_heap_alloc(uint64_t size)
+{
+ int fd = -1, heap = open("/dev/dma_heap/system", O_RDONLY);
+ if (heap < 0)
+ goto fail_open;
+ struct dma_heap_allocation_data alloc_data = {.len = size, .fd_flags = O_RDWR | O_CLOEXEC};
+ if (ioctl(heap, DMA_HEAP_IOCTL_ALLOC, &alloc_data) < 0)
+ goto fail_alloc;
+ fd = alloc_data.fd;
+fail_alloc:
+ close(heap);
+fail_open:
+ return fd;
+}
+
+static int ion_alloc(uint64_t size) {
+ int fd = -1, ion_dev = open("/dev/ion", O_RDONLY);
+ if (ion_dev < 0)
+ goto fail_open;
+ struct ion_allocation_data {
+ __u64 len;
+ __u32 heap_id_mask;
+ __u32 flags;
+ __u32 fd;
+ __u32 unused;
+ } alloc_data = {
+ .len = size,
+ /* ION_HEAP_SYSTEM | ION_SYSTEM_HEAP_ID */
+ .heap_id_mask = (1U << 0) | (1U << 25),
+ .flags = 0, /* uncached */
+ };
+ if (ioctl(ion_dev, _IOWR('I', 0, struct ion_allocation_data), &alloc_data) <
+ 0)
+ goto fail_alloc;
+ fd = alloc_data.fd;
+fail_alloc:
+ close(ion_dev);
+fail_open:
+ return fd;
+};
+
+static VkResult
+wsi_create_kgsl_image_mem(const struct wsi_swapchain *chain,
+ const struct wsi_image_info *info,
+ struct wsi_image *image)
+{
+ const struct wsi_device *wsi = chain->wsi;
+ VkResult result;
+
+ VkMemoryRequirements reqs;
+ wsi->GetImageMemoryRequirements(chain->device, image->image, &reqs);
+
+ if (debug_get_bool_option("USE_HEAP", true)) {
+ image->dma_buf_fd = dma_heap_alloc(reqs.size);
+ if (image->dma_buf_fd < 0)
+ image->dma_buf_fd = ion_alloc(reqs.size);
+ }
+
+ const struct wsi_memory_allocate_info memory_wsi_info = {
+ .sType = VK_STRUCTURE_TYPE_WSI_MEMORY_ALLOCATE_INFO_MESA,
+ .pNext = NULL,
+ .implicit_sync = true,
+ };
+ const VkImportMemoryFdInfoKHR memory_import_info = {
+ .sType = VK_STRUCTURE_TYPE_IMPORT_MEMORY_FD_INFO_KHR,
+ .pNext = &memory_wsi_info,
+ .fd = os_dupfd_cloexec(image->dma_buf_fd),
+ .handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
+ };
+ const VkMemoryDedicatedAllocateInfo memory_dedicated_info = {
+ .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO,
+ .pNext = (image->dma_buf_fd < 0) ? &memory_wsi_info : &memory_import_info,
+ .image = image->image,
+ .buffer = VK_NULL_HANDLE,
+ };
+ const VkMemoryAllocateInfo memory_info = {
+ .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
+ .pNext = &memory_dedicated_info,
+ .allocationSize = reqs.size,
+ .memoryTypeIndex =
+ wsi_select_device_memory_type(wsi, reqs.memoryTypeBits),
+ };
+ result = wsi->AllocateMemory(chain->device, &memory_info,
+ &chain->alloc, &image->memory);
+ if (result != VK_SUCCESS)
+ return result;
+
+ uint32_t dma_buf_offset = 0;
+ if (image->dma_buf_fd == -1)
+ wsi->kgsl_get_info(wsi->pdevice, image->memory, &image->dma_buf_fd,
+ &dma_buf_offset);
+
+ image->cpu_map = mmap(0, reqs.size, PROT_READ | PROT_WRITE, MAP_SHARED,
+ image->dma_buf_fd, dma_buf_offset);
+
+ if (image->cpu_map == MAP_FAILED)
+ return VK_ERROR_OUT_OF_HOST_MEMORY;
+ munmap(image->cpu_map, reqs.size);
+
+ const VkImageSubresource image_subresource = {
+ .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
+ .mipLevel = 0,
+ .arrayLayer = 0,
+ };
+ VkSubresourceLayout image_layout;
+ wsi->GetImageSubresourceLayout(chain->device, image->image,
+ &image_subresource, &image_layout);
+
+ image->drm_modifier = 1274; /* termux-x11's RAW_MMAPPABLE_FD */
+ image->num_planes = 1;
+ image->sizes[0] = reqs.size;
+ image->row_pitches[0] = image_layout.rowPitch;
+ image->offsets[0] = dma_buf_offset;
+
+ return VK_SUCCESS;
+}
+
#define WSI_PRIME_LINEAR_STRIDE_ALIGN 256
static VkResult
diff --git a/src/vulkan/wsi/wsi_common_x11.c b/src/vulkan/wsi/wsi_common_x11.c
index cba1d1c5e7c..48ff1550dc0 100644
--- a/src/vulkan/wsi/wsi_common_x11.c
+++ b/src/vulkan/wsi/wsi_common_x11.c
@@ -43,7 +43,7 @@
#include <string.h>
#include <fcntl.h>
#include <poll.h>
-#include <xf86drm.h>
+#include "util/libdrm.h"
#include "drm-uapi/drm_fourcc.h"
#include "util/hash_table.h"
#include "util/os_file.h"
@@ -145,22 +145,7 @@ static bool
wsi_x11_check_dri3_compatible(const struct wsi_device *wsi_dev,
xcb_connection_t *conn)
{
- xcb_screen_iterator_t screen_iter =
- xcb_setup_roots_iterator(xcb_get_setup(conn));
- xcb_screen_t *screen = screen_iter.data;
-
- /* Open the DRI3 device from the X server. If we do not retrieve one we
- * assume our local device is compatible.
- */
- int dri3_fd = wsi_dri3_open(conn, screen->root, None);
- if (dri3_fd == -1)
- return true;
-
- bool match = wsi_device_matches_drm_fd(wsi_dev, dri3_fd);
-
- close(dri3_fd);
-
- return match;
+ return true;
}
static bool
@@ -1420,7 +1405,7 @@ x11_acquire_next_image_poll_find_index(struct x11_swapchain *chain, uint32_t *im
for (uint32_t i = 0; i < chain->base.image_count; i++) {
if (!chain->images[i].busy) {
/* We found a non-busy image */
- xshmfence_await(chain->images[i].shm_fence);
+ xcb_sync_await_fence(chain->conn, 1, &chain->images[i].sync_fence);
*image_index = i;
chain->images[i].busy = true;
chain->present_poll_acquire_count++;
@@ -1576,7 +1561,7 @@ x11_acquire_next_image_from_queue(struct x11_swapchain *chain,
}
assert(image_index < chain->base.image_count);
- xshmfence_await(chain->images[image_index].shm_fence);
+ xcb_sync_await_fence(chain->conn, 1, &chain->images[image_index].sync_fence);
*image_index_out = image_index;
@@ -1619,7 +1604,7 @@ x11_present_to_x11_dri3(struct x11_swapchain *chain, uint32_t image_index,
options |= XCB_PRESENT_OPTION_SUBOPTIMAL;
#endif
- xshmfence_reset(image->shm_fence);
+ xcb_sync_reset_fence(chain->conn, image->sync_fence);
++chain->sent_image_count;
assert(chain->sent_image_count <= chain->base.image_count);
@@ -2199,14 +2184,23 @@ x11_image_init(VkDevice device_h, struct x11_swapchain *chain,
return VK_ERROR_OUT_OF_HOST_MEMORY;
cookie =
- xcb_dri3_pixmap_from_buffer_checked(chain->conn,
- image->pixmap,
- chain->window,
- image->base.sizes[0],
- pCreateInfo->imageExtent.width,
- pCreateInfo->imageExtent.height,
- image->base.row_pitches[0],
- chain->depth, bpp, fd);
+ xcb_dri3_pixmap_from_buffers_checked(chain->conn,
+ image->pixmap,
+ chain->window,
+ 1,
+ pCreateInfo->imageExtent.width,
+ pCreateInfo->imageExtent.height,
+ image->base.row_pitches[0],
+ image->base.offsets[0],
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ chain->depth, bpp,
+ image->base.drm_modifier,
+ &fd);
}
error = xcb_request_check(chain->conn, cookie);
@@ -2215,6 +2209,12 @@ x11_image_init(VkDevice device_h, struct x11_swapchain *chain,
goto fail_image;
}
+ image->sync_fence = xcb_generate_id(chain->conn);