-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwsi-termux-x11-v3.patch
338 lines (314 loc) · 12.4 KB
/
wsi-termux-x11-v3.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
diff --git a/src/freedreno/vulkan/tu_wsi.cc b/src/freedreno/vulkan/tu_wsi.cc
index 9929993bbe0..85c744585a8 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)
{
@@ -45,6 +57,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/vulkan/wsi/wsi_common.c b/src/vulkan/wsi/wsi_common.c
index 726862560e8..3539a585d22 100644
--- a/src/vulkan/wsi/wsi_common.c
+++ b/src/vulkan/wsi/wsi_common.c
@@ -1424,7 +1424,7 @@ wsi_common_queue_present(const struct wsi_device *wsi,
assert(!has_signal_dma_buf);
#endif
- if (wsi->sw)
+ if (wsi->sw || (wsi->is_tu_kgsl && !has_signal_dma_buf))
wsi->WaitForFences(device, 1, &swapchain->fences[image_index],
true, ~0ull);
diff --git a/src/vulkan/wsi/wsi_common.h b/src/vulkan/wsi/wsi_common.h
index 346025602b9..fbf22a736c1 100644
--- a/src/vulkan/wsi/wsi_common.h
+++ b/src/vulkan/wsi/wsi_common.h
@@ -168,6 +168,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;
@@ -221,6 +222,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 2dff27afa64..1ba21660bab 100644
--- a/src/vulkan/wsi/wsi_common_x11.c
+++ b/src/vulkan/wsi/wsi_common_x11.c
@@ -1422,7 +1422,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++;
@@ -1578,7 +1578,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;
@@ -1617,7 +1617,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);
@@ -2185,14 +2185,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);
@@ -2201,6 +2210,12 @@ x11_image_init(VkDevice device_h, struct x11_swapchain *chain,
goto fail_image;
}
+ image->sync_fence = xcb_generate_id(chain->conn);
+ xcb_sync_create_fence(chain->conn, image->pixmap, image->sync_fence, false);
+ xcb_sync_trigger_fence(chain->conn, image->sync_fence);
+ image->busy = false;
+ return VK_SUCCESS;
+
out_fence:
fence_fd = xshmfence_alloc_shm();
if (fence_fd < 0)
@@ -2245,7 +2260,6 @@ x11_image_finish(struct x11_swapchain *chain,
if (!chain->base.wsi->sw || chain->has_mit_shm) {
cookie = xcb_sync_destroy_fence(chain->conn, image->sync_fence);
xcb_discard_reply(chain->conn, cookie.sequence);
- xshmfence_unmap_shm(image->shm_fence);
cookie = xcb_free_pixmap(chain->conn, image->pixmap);
xcb_discard_reply(chain->conn, cookie.sequence);