Debian Codegraph @

src/panfrost/vulkan/panvk_android.c

raw
1 /*
2 * Copyright 2025 Google LLC
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "panvk_android.h"
7
8 #include "panvk_device.h"
9 #include "panvk_image.h"
10
11 #include "vndk/hardware_buffer.h"
12 #include "vulkan/vk_android_native_buffer.h"
13
14 #include "vk_alloc.h"
15 #include "vk_android.h"
16 #include "vk_device_memory.h"
17 #include "vk_util.h"
18
19 bool
20 panvk_android_is_gralloc_image(const VkImageCreateInfo *pCreateInfo)
21 {
22 vk_foreach_struct_const(ext, pCreateInfo->pNext) {
23 switch ((uint32_t)ext->sType) {
24 case VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID:
25 return true;
26 case VK_STRUCTURE_TYPE_IMAGE_SWAPCHAIN_CREATE_INFO_KHR: {
27 const VkImageSwapchainCreateInfoKHR *swapchain_info = (void *)ext;
28 if (swapchain_info->swapchain != VK_NULL_HANDLE)
29 return true;
30 break;
31 }
32 case VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO: {
33 const VkExternalMemoryImageCreateInfo *external_info = (void *)ext;
34 if (external_info->handleTypes &
35 VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID)
36 return true;
37 break;
38 }
39 default:
40 break;
41 }
42 }
43 return false;
44 }
45
46 struct panvk_android_deferred_image {
47 struct panvk_image base;
48
49 VkImageCreateInfo *create_info;
50 bool initialized;
51 };
52
53 static VkResult
54 panvk_android_create_deferred_image(VkDevice device,
55 const VkImageCreateInfo *pCreateInfo,
56 const VkAllocationCallbacks *pAllocator,
57 VkImage *pImage)
58 {
59 VK_FROM_HANDLE(panvk_device, dev, device);
60
61 /* collect all dynamic array infos */
62 uint32_t queue_family_count = 0;
63 uint32_t view_format_count = 0;
64
65 if (pCreateInfo->sharingMode == VK_SHARING_MODE_CONCURRENT)
66 queue_family_count = pCreateInfo->queueFamilyIndexCount;
67
68 const VkImageFormatListCreateInfo *raw_list =
69 vk_find_struct_const(pCreateInfo->pNext, IMAGE_FORMAT_LIST_CREATE_INFO);
70 if (raw_list)
71 view_format_count = raw_list->viewFormatCount;
72
73 /* Extend below when panvk supports more extensions that interact with ANB or
74 * AHB. e.g. VK_EXT_image_compression_control
75 */
76 VK_MULTIALLOC(ma);
77 VK_MULTIALLOC_DECL(&ma, struct panvk_android_deferred_image, deferred, 1);
78 VK_MULTIALLOC_DECL(&ma, VkImageCreateInfo, create_info, 1);
79 VK_MULTIALLOC_DECL(&ma, VkImageFormatListCreateInfo, list_info, 1);
80 VK_MULTIALLOC_DECL(&ma, VkImageStencilUsageCreateInfo, stencil_info, 1);
81 VK_MULTIALLOC_DECL(&ma, uint32_t, queue_families, queue_family_count);
82 VK_MULTIALLOC_DECL(&ma, uint32_t, view_formats, view_format_count);
83
84 if (!vk_multialloc_zalloc2(&ma, &dev->vk.alloc, pAllocator,
85 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT))
86 return panvk_error(dev, VK_ERROR_OUT_OF_HOST_MEMORY);
87
88 vk_image_init(&dev->vk, &deferred->base.vk, pCreateInfo);
89
90 /* prepare the deferred VkImageCreateInfo chain */
91 *create_info = *pCreateInfo;
92 create_info->pNext = NULL;
93 /* Assign resolved AHB external format */
94 create_info->format = deferred->base.vk.format;
95 create_info->tiling = deferred->base.vk.tiling =
96 VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT;
97 if (pCreateInfo->sharingMode == VK_SHARING_MODE_CONCURRENT) {
98 typed_memcpy(queue_families, pCreateInfo->pQueueFamilyIndices,
99 pCreateInfo->queueFamilyIndexCount);
100 create_info->pQueueFamilyIndices = queue_families;
101 }
102
103 /* Per spec section 12.3. Images
104 *
105 * - If tiling is VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT and flags contains
106 * VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT, then the pNext chain must include a
107 * VkImageFormatListCreateInfo structure with non-zero viewFormatCount.
108 *
109 * ANB and aliased ANB always chain proper format list for mutable swapchain
110 * image support, but AHB is allowed to mutate without an explicit format
111 * list due to legacy spec issue. So we chain a view format of the create
112 * format itself to satisfy VK_EXT_image_drm_format_modifier VUs.
113 */
114 if (view_format_count ||
115 deferred->base.vk.create_flags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT) {
116 if (view_format_count) {
117 typed_memcpy(view_formats, raw_list->pViewFormats, view_format_count);
118 } else {
119 view_format_count = 1;
120 view_formats = &create_info->format;
121 }
122 *list_info = (VkImageFormatListCreateInfo){
123 .sType = VK_STRUCTURE_TYPE_IMAGE_FORMAT_LIST_CREATE_INFO,
124 .viewFormatCount = view_format_count,
125 .pViewFormats = view_formats,
126 };
127 __vk_append_struct(create_info, list_info);
128 }
129
130 if (deferred->base.vk.stencil_usage) {
131 *stencil_info = (VkImageStencilUsageCreateInfo){
132 .sType = VK_STRUCTURE_TYPE_IMAGE_STENCIL_USAGE_CREATE_INFO,
133 .stencilUsage = deferred->base.vk.stencil_usage,
134 };
135 __vk_append_struct(create_info, stencil_info);
136 }
137
138 deferred->create_info = create_info;
139 *pImage = panvk_image_to_handle(&deferred->base);
140
141 return VK_SUCCESS;
142 }
143
144 static inline uint32_t
145 panvk_android_get_fd_mem_type_bits(VkDevice dev_handle, int dma_buf_fd)
146 {
147 VK_FROM_HANDLE(vk_device, dev, dev_handle);
148
149 VkMemoryFdPropertiesKHR fd_props = {
150 .sType = VK_STRUCTURE_TYPE_MEMORY_FD_PROPERTIES_KHR,
151 };
152 VkResult result = dev->dispatch_table.GetMemoryFdPropertiesKHR(
153 dev_handle, VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT, dma_buf_fd,
154 &fd_props);
155 return result == VK_SUCCESS ? fd_props.memoryTypeBits : 0;
156 }
157
158 static VkResult
159 panvk_android_get_image_mem_reqs(VkDevice dev_handle, VkImage img_handle,
160 int dma_buf_fd,
161 VkMemoryRequirements *out_mem_reqs)
162 {
163 VK_FROM_HANDLE(vk_device, dev, dev_handle);
164 VkMemoryRequirements mem_reqs;
165
166 dev->dispatch_table.GetImageMemoryRequirements(dev_handle, img_handle,
167 &mem_reqs);
168
169 const uint32_t fd_mem_type_bits =
170 panvk_android_get_fd_mem_type_bits(dev_handle, dma_buf_fd);
171
172 if (!(mem_reqs.memoryTypeBits & fd_mem_type_bits)) {
173 return panvk_errorf(dev_handle, VK_ERROR_INVALID_EXTERNAL_HANDLE,
174 "No compatible mem type: img req (%u), fd req (%u)",
175 mem_reqs.memoryTypeBits, fd_mem_type_bits);
176 }
177
178 mem_reqs.memoryTypeBits &= fd_mem_type_bits;
179 *out_mem_reqs = mem_reqs;
180
181 return VK_SUCCESS;
182 }
183
184 static VkResult
185 panvk_android_get_buffer_mem_reqs(VkDevice dev_handle, VkBuffer buf_handle,
186 int dma_buf_fd,
187 VkMemoryRequirements *out_mem_reqs)
188 {
189 VK_FROM_HANDLE(vk_device, dev, dev_handle);
190 VkMemoryRequirements mem_reqs;
191
192 dev->dispatch_table.GetBufferMemoryRequirements(dev_handle, buf_handle,
193 &mem_reqs);
194
195 const uint32_t fd_mem_type_bits =
196 panvk_android_get_fd_mem_type_bits(dev_handle, dma_buf_fd);
197
198 if (!(mem_reqs.memoryTypeBits & fd_mem_type_bits)) {
199 return panvk_errorf(dev_handle, VK_ERROR_INVALID_EXTERNAL_HANDLE,
200 "No compatible mem type: buf req (%u), fd req (%u)",
201 mem_reqs.memoryTypeBits, fd_mem_type_bits);
202 }
203
204 mem_reqs.memoryTypeBits &= fd_mem_type_bits;
205 *out_mem_reqs = mem_reqs;
206
207 return VK_SUCCESS;
208 }
209
210 static VkResult
211 panvk_android_import_anb_memory(VkDevice dev_handle, VkImage img_handle,
212 const VkNativeBufferANDROID *anb,
213 const VkAllocationCallbacks *alloc)
214 {
215 VK_FROM_HANDLE(vk_device, dev, dev_handle);
216 VK_FROM_HANDLE(panvk_image, img, img_handle);
217 VkMemoryRequirements mem_reqs;
218 VkResult result;
219
220 assert(anb && anb->handle && anb->handle->numFds > 0);
221
222 int dma_buf_fd = anb->handle->data[0];
223 result = panvk_android_get_image_mem_reqs(dev_handle, img_handle, dma_buf_fd,
224 &mem_reqs);
225 if (result != VK_SUCCESS)
226 return result;
227
228 int dup_fd = os_dupfd_cloexec(dma_buf_fd);
229 if (dup_fd < 0) {
230 return (errno == EMFILE) ? VK_ERROR_TOO_MANY_OBJECTS
231 : VK_ERROR_OUT_OF_HOST_MEMORY;
232 }
233
234 const VkMemoryDedicatedAllocateInfo dedicated_info = {
235 .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO,
236 .image = img_handle,
237 };
238 const VkImportMemoryFdInfoKHR fd_info = {
239 .sType = VK_STRUCTURE_TYPE_IMPORT_MEMORY_FD_INFO_KHR,
240 .pNext = &dedicated_info,
241 .handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
242 .fd = dup_fd,
243 };
244 const VkMemoryAllocateInfo alloc_info = {
245 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
246 .pNext = &fd_info,
247 .allocationSize = mem_reqs.size,
248 .memoryTypeIndex = ffs(mem_reqs.memoryTypeBits) - 1,
249 };
250 result = dev->dispatch_table.AllocateMemory(dev_handle, &alloc_info, alloc,
251 &img->vk.anb_memory);
252 if (result != VK_SUCCESS) {
253 close(dup_fd);
254 return result;
255 }
256
257 return VK_SUCCESS;
258 }
259
260 static VkResult
261 panvk_android_anb_init(struct panvk_device *dev, VkImageCreateInfo *create_info,
262 const VkNativeBufferANDROID *anb,
263 const VkAllocationCallbacks *alloc,
264 struct panvk_image *img)
265 {
266 VkResult result;
267
268 VkImageDrmFormatModifierExplicitCreateInfoEXT mod_info;
269 VkSubresourceLayout layouts[PANVK_MAX_PLANES];
270 assert(vk_find_struct_const(create_info->pNext, NATIVE_BUFFER_ANDROID));
271 result = vk_android_get_anb_layout(create_info, &mod_info, layouts,
272 PANVK_MAX_PLANES);
273 if (result != VK_SUCCESS)
274 return result;
275
276 mod_info.pNext = create_info->pNext;
277 const VkExternalMemoryImageCreateInfo external_info = {
278 .sType = VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO,
279 .pNext = &mod_info,
280 .handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
281 };
282
283 /* create_info is a already local copy from the caller */
284 create_info->pNext = &external_info;
285 result = panvk_image_init(img, create_info);
286 if (result != VK_SUCCESS)
287 return result;
288
289 result = panvk_android_import_anb_memory(
290 panvk_device_to_handle(dev), panvk_image_to_handle(img), anb, alloc);
291 if (result != VK_SUCCESS)
292 return result;
293
294 return VK_SUCCESS;
295 }
296
297 VkResult
298 panvk_android_create_gralloc_image(VkDevice device,
299 const VkImageCreateInfo *pCreateInfo,
300 const VkAllocationCallbacks *pAllocator,
301 VkImage *pImage)
302 {
303 VK_FROM_HANDLE(panvk_device, dev, device);
304 VkResult result;
305
306 const VkNativeBufferANDROID *anb =
307 vk_find_struct_const(pCreateInfo->pNext, NATIVE_BUFFER_ANDROID);
308 if (!anb) {
309 return panvk_android_create_deferred_image(device, pCreateInfo,
310 pAllocator, pImage);
311 }
312
313 struct panvk_image *img =
314 vk_image_create(&dev->vk, pCreateInfo, pAllocator, sizeof(*img));
315 if (!img)
316 return panvk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
317
318 VkImageCreateInfo create_info = *pCreateInfo;
319 create_info.tiling = img->vk.tiling =
320 VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT;
321
322 result = panvk_android_anb_init(dev, &create_info, anb, pAllocator, img);
323 if (result != VK_SUCCESS) {
324 vk_image_destroy(&dev->vk, pAllocator, &img->vk);
325 return panvk_error(device, result);
326 }
327
328 VkImage img_handle = panvk_image_to_handle(img);
329 result = dev->vk.dispatch_table.BindImageMemory(device, img_handle,
330 img->vk.anb_memory, 0);
331 if (result != VK_SUCCESS) {
332 dev->vk.dispatch_table.DestroyImage(device, img_handle, pAllocator);
333 return panvk_error(device, result);
334 }
335
336 *pImage = img_handle;
337
338 return VK_SUCCESS;
339 }
340
341 VkResult
342 panvk_android_get_wsi_memory(struct panvk_device *dev,
343 const VkBindImageMemoryInfo *bind_info,
344 VkDeviceMemory *out_mem_handle)
345 {
346 VK_FROM_HANDLE(panvk_image, img, bind_info->image);
347 VkResult result;
348
349 struct panvk_android_deferred_image *deferred =
350 container_of(img, struct panvk_android_deferred_image, base);
351 assert(deferred->create_info && !deferred->initialized);
352
353 const VkNativeBufferANDROID *anb =
354 vk_find_struct_const(bind_info->pNext, NATIVE_BUFFER_ANDROID);
355
356 /* Inject ANB into the deferred pNext chain to leverage the existing common
357 * Android helper vk_android_get_anb_layout, which could be refactored to
358 * take ANB directly instead.
359 */
360 VkNativeBufferANDROID local_anb = *anb;
361 local_anb.pNext = deferred->create_info->pNext;
362 deferred->create_info->pNext = &local_anb;
363 result = panvk_android_anb_init(dev, deferred->create_info, anb,
364 &dev->vk.alloc, img);
365 if (result != VK_SUCCESS)
366 return result;
367
368 deferred->initialized = true;
369 *out_mem_handle = img->vk.anb_memory;
370
371 return VK_SUCCESS;
372 }
373
374 static VkResult
375 panvk_android_ahb_image_init(struct AHardwareBuffer *ahb,
376 struct panvk_image *img)
377 {
378 VkResult result;
379
380 struct panvk_android_deferred_image *deferred =
381 container_of(img, struct panvk_android_deferred_image, base);
382 assert(deferred->create_info && !deferred->initialized);
383
384 VkImageDrmFormatModifierExplicitCreateInfoEXT mod_info;
385 VkSubresourceLayout layouts[PANVK_MAX_PLANES];
386 result =
387 vk_android_get_ahb_layout(ahb, &mod_info, layouts, PANVK_MAX_PLANES);
388 if (result != VK_SUCCESS)
389 return result;
390 __vk_append_struct(deferred->create_info, &mod_info);
391
392 VkExternalMemoryImageCreateInfo external_info = {
393 .sType = VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO,
394 .handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
395 };
396 __vk_append_struct(deferred->create_info, &external_info);
397
398 result = panvk_image_init(img, deferred->create_info);
399 if (result != VK_SUCCESS)
400 return result;
401
402 deferred->initialized = true;
403
404 return VK_SUCCESS;
405 }
406
407 static VkResult
408 panvk_android_import_ahb_memory(VkDevice device,
409 const VkMemoryAllocateInfo *pAllocateInfo,
410 struct AHardwareBuffer *ahb,
411 const VkAllocationCallbacks *pAllocator,
412 VkDeviceMemory *pMemory)
413 {
414 VK_FROM_HANDLE(vk_device, dev, device);
415 const native_handle_t *handle = AHardwareBuffer_getNativeHandle(ahb);
416 assert(handle && handle->numFds > 0);
417 int dma_buf_fd = handle->data[0];
418 VkResult result;
419
420 VkImage img_handle = VK_NULL_HANDLE;
421 VkBuffer buf_handle = VK_NULL_HANDLE;
422 VkMemoryRequirements mem_reqs;
423
424 /* Fix allocationSize and memoryTypeIndex. */
425 const VkMemoryDedicatedAllocateInfo *dedicated_info = vk_find_struct_const(
426 pAllocateInfo->pNext, MEMORY_DEDICATED_ALLOCATE_INFO);
427 if (dedicated_info && dedicated_info->image != VK_NULL_HANDLE) {
428 img_handle = dedicated_info->image;
429 VK_FROM_HANDLE(panvk_image, img, img_handle);
430 result = panvk_android_ahb_image_init(ahb, img);
431 if (result == VK_SUCCESS) {
432 result = panvk_android_get_image_mem_reqs(device, img_handle,
433 dma_buf_fd, &mem_reqs);
434 }
435 } else if (dedicated_info && dedicated_info->buffer != VK_NULL_HANDLE) {
436 buf_handle = dedicated_info->buffer;
437 result = panvk_android_get_buffer_mem_reqs(device, buf_handle, dma_buf_fd,
438 &mem_reqs);
439 if (result == VK_SUCCESS &&
440 pAllocateInfo->allocationSize > mem_reqs.size) {
441 /* For AHB VkBuffer import, the allocationSize comes from the raw
442 * external AHB props query and it can be larger than the underlying
443 * buffer memory requirement. So we must respect the allocationSize
444 * for the actual mem import to support mapping the whole AHB size,
445 * and the dedicated buffer info has to be stripped to obey the spec.
446 */
447 mem_reqs.size = pAllocateInfo->allocationSize;
448 buf_handle = VK_NULL_HANDLE;
449 }
450 } else {
451 mem_reqs.size = pAllocateInfo->allocationSize;
452 mem_reqs.memoryTypeBits =
453 panvk_android_get_fd_mem_type_bits(device, dma_buf_fd);
454 result = mem_reqs.memoryTypeBits ? VK_SUCCESS
455 : VK_ERROR_INVALID_EXTERNAL_HANDLE;
456 }
457 if (result != VK_SUCCESS)
458 return result;
459
460 /* Override to a compatible memory type if needed. */
461 uint32_t mem_type_index = pAllocateInfo->memoryTypeIndex;
462 if (!((1 << mem_type_index) & mem_reqs.memoryTypeBits))
463 mem_type_index = ffs(mem_reqs.memoryTypeBits) - 1;
464
465 int dup_fd = os_dupfd_cloexec(dma_buf_fd);
466 if (dup_fd < 0) {
467 return (errno == EMFILE) ? VK_ERROR_TOO_MANY_OBJECTS
468 : VK_ERROR_OUT_OF_HOST_MEMORY;
469 }
470
471 /* Always chain dedicated info for simplicity, since the spec allows both
472 * image and buffer to be VK_NULL_HANDLE.
473 */
474 const VkMemoryDedicatedAllocateInfo local_dedicated_info = {
475 .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO,
476 .image = img_handle,
477 .buffer = buf_handle,
478 };
479 const VkImportMemoryFdInfoKHR fd_info = {
480 .sType = VK_STRUCTURE_TYPE_IMPORT_MEMORY_FD_INFO_KHR,
481 .pNext = &local_dedicated_info,
482 .handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
483 .fd = dup_fd,
484 };
485 const VkMemoryAllocateInfo alloc_info = {
486 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
487 .pNext = &fd_info,
488 .allocationSize = mem_reqs.size,
489 .memoryTypeIndex = mem_type_index,
490 };
491 result = dev->dispatch_table.AllocateMemory(device, &alloc_info, pAllocator,
492 pMemory);
493 if (result != VK_SUCCESS)
494 close(dup_fd);
495
496 return result;
497 }
498
499 bool
500 panvk_android_is_ahb_memory(const VkMemoryAllocateInfo *pAllocateInfo)
501 {
502 vk_foreach_struct_const(ext, pAllocateInfo->pNext) {
503 switch (ext->sType) {
504 case VK_STRUCTURE_TYPE_IMPORT_ANDROID_HARDWARE_BUFFER_INFO_ANDROID:
505 return true;
506 case VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO:
507 return ((const VkExportMemoryAllocateInfo *)ext)->handleTypes ==
508 VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID;
509 default:
510 break;
511 }
512 }
513 return false;
514 }
515
516 VkResult
517 panvk_android_allocate_ahb_memory(VkDevice device,
518 const VkMemoryAllocateInfo *pAllocateInfo,
519 const VkAllocationCallbacks *pAllocator,
520 VkDeviceMemory *pMemory)
521 {
522 struct AHardwareBuffer *ahb;
523 VkResult result;
524
525 const VkImportAndroidHardwareBufferInfoANDROID *ahb_info =
526 vk_find_struct_const(pAllocateInfo->pNext,
527 IMPORT_ANDROID_HARDWARE_BUFFER_INFO_ANDROID);
528 if (ahb_info) {
529 ahb = ahb_info->buffer;
530 AHardwareBuffer_acquire(ahb);
531 } else {
532 ahb = vk_alloc_ahardware_buffer(pAllocateInfo);
533 if (!ahb)
534 return panvk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
535 }
536
537 result = panvk_android_import_ahb_memory(device, pAllocateInfo, ahb,
538 pAllocator, pMemory);
539 if (result != VK_SUCCESS) {
540 AHardwareBuffer_release(ahb);
541 return panvk_error(device, result);
542 }
543
544 VK_FROM_HANDLE(vk_device_memory, mem, *pMemory);
545 assert(!mem->ahardware_buffer);
546 mem->ahardware_buffer = ahb;
547
548 return VK_SUCCESS;
549 }