anv/android: add GetAndroidHardwareBufferPropertiesANDROID
[mesa.git] / src / intel / vulkan / anv_android.c
1 /*
2 * Copyright © 2017, Google Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include <hardware/gralloc.h>
25 #include <hardware/hardware.h>
26 #include <hardware/hwvulkan.h>
27 #include <vulkan/vk_android_native_buffer.h>
28 #include <vulkan/vk_icd.h>
29 #include <sync/sync.h>
30
31 #include "anv_private.h"
32 #include "vk_format_info.h"
33 #include "vk_util.h"
34
35 static int anv_hal_open(const struct hw_module_t* mod, const char* id, struct hw_device_t** dev);
36 static int anv_hal_close(struct hw_device_t *dev);
37
38 static void UNUSED
39 static_asserts(void)
40 {
41 STATIC_ASSERT(HWVULKAN_DISPATCH_MAGIC == ICD_LOADER_MAGIC);
42 }
43
44 PUBLIC struct hwvulkan_module_t HAL_MODULE_INFO_SYM = {
45 .common = {
46 .tag = HARDWARE_MODULE_TAG,
47 .module_api_version = HWVULKAN_MODULE_API_VERSION_0_1,
48 .hal_api_version = HARDWARE_MAKE_API_VERSION(1, 0),
49 .id = HWVULKAN_HARDWARE_MODULE_ID,
50 .name = "Intel Vulkan HAL",
51 .author = "Intel",
52 .methods = &(hw_module_methods_t) {
53 .open = anv_hal_open,
54 },
55 },
56 };
57
58 /* If any bits in test_mask are set, then unset them and return true. */
59 static inline bool
60 unmask32(uint32_t *inout_mask, uint32_t test_mask)
61 {
62 uint32_t orig_mask = *inout_mask;
63 *inout_mask &= ~test_mask;
64 return *inout_mask != orig_mask;
65 }
66
67 static int
68 anv_hal_open(const struct hw_module_t* mod, const char* id,
69 struct hw_device_t** dev)
70 {
71 assert(mod == &HAL_MODULE_INFO_SYM.common);
72 assert(strcmp(id, HWVULKAN_DEVICE_0) == 0);
73
74 hwvulkan_device_t *hal_dev = malloc(sizeof(*hal_dev));
75 if (!hal_dev)
76 return -1;
77
78 *hal_dev = (hwvulkan_device_t) {
79 .common = {
80 .tag = HARDWARE_DEVICE_TAG,
81 .version = HWVULKAN_DEVICE_API_VERSION_0_1,
82 .module = &HAL_MODULE_INFO_SYM.common,
83 .close = anv_hal_close,
84 },
85 .EnumerateInstanceExtensionProperties = anv_EnumerateInstanceExtensionProperties,
86 .CreateInstance = anv_CreateInstance,
87 .GetInstanceProcAddr = anv_GetInstanceProcAddr,
88 };
89
90 *dev = &hal_dev->common;
91 return 0;
92 }
93
94 static int
95 anv_hal_close(struct hw_device_t *dev)
96 {
97 /* hwvulkan.h claims that hw_device_t::close() is never called. */
98 return -1;
99 }
100
101 static VkResult
102 get_ahw_buffer_format_properties(
103 VkDevice device_h,
104 const struct AHardwareBuffer *buffer,
105 VkAndroidHardwareBufferFormatPropertiesANDROID *pProperties)
106 {
107 ANV_FROM_HANDLE(anv_device, device, device_h);
108
109 /* Get a description of buffer contents . */
110 AHardwareBuffer_Desc desc;
111 AHardwareBuffer_describe(buffer, &desc);
112
113 /* Verify description. */
114 uint64_t gpu_usage =
115 AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE |
116 AHARDWAREBUFFER_USAGE_GPU_COLOR_OUTPUT |
117 AHARDWAREBUFFER_USAGE_GPU_DATA_BUFFER;
118
119 /* "Buffer must be a valid Android hardware buffer object with at least
120 * one of the AHARDWAREBUFFER_USAGE_GPU_* usage flags."
121 */
122 if (!(desc.usage & (gpu_usage)))
123 return VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR;
124
125 /* Fill properties fields based on description. */
126 VkAndroidHardwareBufferFormatPropertiesANDROID *p = pProperties;
127
128 p->format = vk_format_from_android(desc.format);
129
130 const struct anv_format *anv_format = anv_get_format(p->format);
131 p->externalFormat = (uint64_t) (uintptr_t) anv_format;
132
133 /* Default to OPTIMAL tiling but set to linear in case
134 * of AHARDWAREBUFFER_USAGE_GPU_DATA_BUFFER usage.
135 */
136 VkImageTiling tiling = VK_IMAGE_TILING_OPTIMAL;
137
138 if (desc.usage & AHARDWAREBUFFER_USAGE_GPU_DATA_BUFFER)
139 tiling = VK_IMAGE_TILING_LINEAR;
140
141 p->formatFeatures =
142 anv_get_image_format_features(&device->info, p->format, anv_format,
143 tiling);
144
145 /* "Images can be created with an external format even if the Android hardware
146 * buffer has a format which has an equivalent Vulkan format to enable
147 * consistent handling of images from sources that might use either category
148 * of format. However, all images created with an external format are subject
149 * to the valid usage requirements associated with external formats, even if
150 * the Android hardware buffer’s format has a Vulkan equivalent."
151 *
152 * "The formatFeatures member *must* include
153 * VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT and at least one of
154 * VK_FORMAT_FEATURE_MIDPOINT_CHROMA_SAMPLES_BIT or
155 * VK_FORMAT_FEATURE_COSITED_CHROMA_SAMPLES_BIT"
156 */
157 p->formatFeatures |=
158 VK_FORMAT_FEATURE_MIDPOINT_CHROMA_SAMPLES_BIT;
159
160 /* "Implementations may not always be able to determine the color model,
161 * numerical range, or chroma offsets of the image contents, so the values
162 * in VkAndroidHardwareBufferFormatPropertiesANDROID are only suggestions.
163 * Applications should treat these values as sensible defaults to use in
164 * the absence of more reliable information obtained through some other
165 * means."
166 */
167 p->samplerYcbcrConversionComponents.r = VK_COMPONENT_SWIZZLE_IDENTITY;
168 p->samplerYcbcrConversionComponents.g = VK_COMPONENT_SWIZZLE_IDENTITY;
169 p->samplerYcbcrConversionComponents.b = VK_COMPONENT_SWIZZLE_IDENTITY;
170 p->samplerYcbcrConversionComponents.a = VK_COMPONENT_SWIZZLE_IDENTITY;
171
172 p->suggestedYcbcrModel = VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_601;
173 p->suggestedYcbcrRange = VK_SAMPLER_YCBCR_RANGE_ITU_FULL;
174
175 p->suggestedXChromaOffset = VK_CHROMA_LOCATION_MIDPOINT;
176 p->suggestedYChromaOffset = VK_CHROMA_LOCATION_MIDPOINT;
177
178 return VK_SUCCESS;
179 }
180
181 VkResult
182 anv_GetAndroidHardwareBufferPropertiesANDROID(
183 VkDevice device_h,
184 const struct AHardwareBuffer *buffer,
185 VkAndroidHardwareBufferPropertiesANDROID *pProperties)
186 {
187 ANV_FROM_HANDLE(anv_device, dev, device_h);
188 struct anv_physical_device *pdevice = &dev->instance->physicalDevice;
189
190 VkAndroidHardwareBufferFormatPropertiesANDROID *format_prop =
191 vk_find_struct(pProperties->pNext,
192 ANDROID_HARDWARE_BUFFER_FORMAT_PROPERTIES_ANDROID);
193
194 /* Fill format properties of an Android hardware buffer. */
195 if (format_prop)
196 get_ahw_buffer_format_properties(device_h, buffer, format_prop);
197
198 /* NOTE - We support buffers with only one handle but do not error on
199 * multiple handle case. Reason is that we want to support YUV formats
200 * where we have many logical planes but they all point to the same
201 * buffer, like is the case with VK_FORMAT_G8_B8R8_2PLANE_420_UNORM.
202 */
203 const native_handle_t *handle =
204 AHardwareBuffer_getNativeHandle(buffer);
205 int dma_buf = (handle && handle->numFds) ? handle->data[0] : -1;
206 if (dma_buf < 0)
207 return VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR;
208
209 /* All memory types. */
210 uint32_t memory_types = (1ull << pdevice->memory.type_count) - 1;
211
212 pProperties->allocationSize = lseek(dma_buf, 0, SEEK_END);
213 pProperties->memoryTypeBits = memory_types;
214
215 return VK_SUCCESS;
216 }
217
218 VkResult
219 anv_image_from_gralloc(VkDevice device_h,
220 const VkImageCreateInfo *base_info,
221 const VkNativeBufferANDROID *gralloc_info,
222 const VkAllocationCallbacks *alloc,
223 VkImage *out_image_h)
224
225 {
226 ANV_FROM_HANDLE(anv_device, device, device_h);
227 VkImage image_h = VK_NULL_HANDLE;
228 struct anv_image *image = NULL;
229 struct anv_bo *bo = NULL;
230 VkResult result;
231
232 struct anv_image_create_info anv_info = {
233 .vk_info = base_info,
234 .isl_extra_usage_flags = ISL_SURF_USAGE_DISABLE_AUX_BIT,
235 };
236
237 if (gralloc_info->handle->numFds != 1) {
238 return vk_errorf(device->instance, device,
239 VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR,
240 "VkNativeBufferANDROID::handle::numFds is %d, "
241 "expected 1", gralloc_info->handle->numFds);
242 }
243
244 /* Do not close the gralloc handle's dma_buf. The lifetime of the dma_buf
245 * must exceed that of the gralloc handle, and we do not own the gralloc
246 * handle.
247 */
248 int dma_buf = gralloc_info->handle->data[0];
249
250 uint64_t bo_flags = ANV_BO_EXTERNAL;
251 if (device->instance->physicalDevice.supports_48bit_addresses)
252 bo_flags |= EXEC_OBJECT_SUPPORTS_48B_ADDRESS;
253 if (device->instance->physicalDevice.use_softpin)
254 bo_flags |= EXEC_OBJECT_PINNED;
255
256 result = anv_bo_cache_import(device, &device->bo_cache, dma_buf, bo_flags, &bo);
257 if (result != VK_SUCCESS) {
258 return vk_errorf(device->instance, device, result,
259 "failed to import dma-buf from VkNativeBufferANDROID");
260 }
261
262 int i915_tiling = anv_gem_get_tiling(device, bo->gem_handle);
263 switch (i915_tiling) {
264 case I915_TILING_NONE:
265 anv_info.isl_tiling_flags = ISL_TILING_LINEAR_BIT;
266 break;
267 case I915_TILING_X:
268 anv_info.isl_tiling_flags = ISL_TILING_X_BIT;
269 break;
270 case I915_TILING_Y:
271 anv_info.isl_tiling_flags = ISL_TILING_Y0_BIT;
272 break;
273 case -1:
274 result = vk_errorf(device->instance, device,
275 VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR,
276 "DRM_IOCTL_I915_GEM_GET_TILING failed for "
277 "VkNativeBufferANDROID");
278 goto fail_tiling;
279 default:
280 result = vk_errorf(device->instance, device,
281 VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR,
282 "DRM_IOCTL_I915_GEM_GET_TILING returned unknown "
283 "tiling %d for VkNativeBufferANDROID", i915_tiling);
284 goto fail_tiling;
285 }
286
287 enum isl_format format = anv_get_isl_format(&device->info,
288 base_info->format,
289 VK_IMAGE_ASPECT_COLOR_BIT,
290 base_info->tiling);
291 assert(format != ISL_FORMAT_UNSUPPORTED);
292
293 anv_info.stride = gralloc_info->stride *
294 (isl_format_get_layout(format)->bpb / 8);
295
296 result = anv_image_create(device_h, &anv_info, alloc, &image_h);
297 image = anv_image_from_handle(image_h);
298 if (result != VK_SUCCESS)
299 goto fail_create;
300
301 if (bo->size < image->size) {
302 result = vk_errorf(device->instance, device,
303 VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR,
304 "dma-buf from VkNativeBufferANDROID is too small for "
305 "VkImage: %"PRIu64"B < %"PRIu64"B",
306 bo->size, image->size);
307 goto fail_size;
308 }
309
310 assert(image->n_planes == 1);
311 assert(image->planes[0].address.offset == 0);
312
313 image->planes[0].address.bo = bo;
314 image->planes[0].bo_is_owned = true;
315
316 /* We need to set the WRITE flag on window system buffers so that GEM will
317 * know we're writing to them and synchronize uses on other rings (for
318 * example, if the display server uses the blitter ring).
319 *
320 * If this function fails and if the imported bo was resident in the cache,
321 * we should avoid updating the bo's flags. Therefore, we defer updating
322 * the flags until success is certain.
323 *
324 */
325 bo->flags &= ~EXEC_OBJECT_ASYNC;
326 bo->flags |= EXEC_OBJECT_WRITE;
327
328 /* Don't clobber the out-parameter until success is certain. */
329 *out_image_h = image_h;
330
331 return VK_SUCCESS;
332
333 fail_size:
334 anv_DestroyImage(device_h, image_h, alloc);
335 fail_create:
336 fail_tiling:
337 anv_bo_cache_release(device, &device->bo_cache, bo);
338
339 return result;
340 }
341
342 VkResult anv_GetSwapchainGrallocUsageANDROID(
343 VkDevice device_h,
344 VkFormat format,
345 VkImageUsageFlags imageUsage,
346 int* grallocUsage)
347 {
348 ANV_FROM_HANDLE(anv_device, device, device_h);
349 struct anv_physical_device *phys_dev = &device->instance->physicalDevice;
350 VkPhysicalDevice phys_dev_h = anv_physical_device_to_handle(phys_dev);
351 VkResult result;
352
353 *grallocUsage = 0;
354 intel_logd("%s: format=%d, usage=0x%x", __func__, format, imageUsage);
355
356 /* WARNING: Android Nougat's libvulkan.so hardcodes the VkImageUsageFlags
357 * returned to applications via VkSurfaceCapabilitiesKHR::supportedUsageFlags.
358 * The relevant code in libvulkan/swapchain.cpp contains this fun comment:
359 *
360 * TODO(jessehall): I think these are right, but haven't thought hard
361 * about it. Do we need to query the driver for support of any of
362 * these?
363 *
364 * Any disagreement between this function and the hardcoded
365 * VkSurfaceCapabilitiesKHR:supportedUsageFlags causes tests
366 * dEQP-VK.wsi.android.swapchain.*.image_usage to fail.
367 */
368
369 const VkPhysicalDeviceImageFormatInfo2KHR image_format_info = {
370 .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2_KHR,
371 .format = format,
372 .type = VK_IMAGE_TYPE_2D,
373 .tiling = VK_IMAGE_TILING_OPTIMAL,
374 .usage = imageUsage,
375 };
376
377 VkImageFormatProperties2KHR image_format_props = {
378 .sType = VK_STRUCTURE_TYPE_IMAGE_FORMAT_PROPERTIES_2_KHR,
379 };
380
381 /* Check that requested format and usage are supported. */
382 result = anv_GetPhysicalDeviceImageFormatProperties2(phys_dev_h,
383 &image_format_info, &image_format_props);
384 if (result != VK_SUCCESS) {
385 return vk_errorf(device->instance, device, result,
386 "anv_GetPhysicalDeviceImageFormatProperties2 failed "
387 "inside %s", __func__);
388 }
389
390 /* Reject STORAGE here to avoid complexity elsewhere. */
391 if (imageUsage & VK_IMAGE_USAGE_STORAGE_BIT) {
392 return vk_errorf(device->instance, device, VK_ERROR_FORMAT_NOT_SUPPORTED,
393 "VK_IMAGE_USAGE_STORAGE_BIT unsupported for gralloc "
394 "swapchain");
395 }
396
397 if (unmask32(&imageUsage, VK_IMAGE_USAGE_TRANSFER_DST_BIT |
398 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT))
399 *grallocUsage |= GRALLOC_USAGE_HW_RENDER;
400
401 if (unmask32(&imageUsage, VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
402 VK_IMAGE_USAGE_SAMPLED_BIT |
403 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT))
404 *grallocUsage |= GRALLOC_USAGE_HW_TEXTURE;
405
406 /* All VkImageUsageFlags not explicitly checked here are unsupported for
407 * gralloc swapchains.
408 */
409 if (imageUsage != 0) {
410 return vk_errorf(device->instance, device, VK_ERROR_FORMAT_NOT_SUPPORTED,
411 "unsupported VkImageUsageFlags(0x%x) for gralloc "
412 "swapchain", imageUsage);
413 }
414
415 /* The below formats support GRALLOC_USAGE_HW_FB (that is, display
416 * scanout). This short list of formats is univserally supported on Intel
417 * but is incomplete. The full set of supported formats is dependent on
418 * kernel and hardware.
419 *
420 * FINISHME: Advertise all display-supported formats.
421 */
422 switch (format) {
423 case VK_FORMAT_B8G8R8A8_UNORM:
424 case VK_FORMAT_B5G6R5_UNORM_PACK16:
425 case VK_FORMAT_R8G8B8A8_UNORM:
426 case VK_FORMAT_R8G8B8A8_SRGB:
427 *grallocUsage |= GRALLOC_USAGE_HW_FB |
428 GRALLOC_USAGE_HW_COMPOSER |
429 GRALLOC_USAGE_EXTERNAL_DISP;
430 break;
431 default:
432 intel_logw("%s: unsupported format=%d", __func__, format);
433 }
434
435 if (*grallocUsage == 0)
436 return VK_ERROR_FORMAT_NOT_SUPPORTED;
437
438 return VK_SUCCESS;
439 }
440
441 VkResult
442 anv_AcquireImageANDROID(
443 VkDevice device_h,
444 VkImage image_h,
445 int nativeFenceFd,
446 VkSemaphore semaphore_h,
447 VkFence fence_h)
448 {
449 ANV_FROM_HANDLE(anv_device, device, device_h);
450 VkResult result = VK_SUCCESS;
451
452 if (nativeFenceFd != -1) {
453 /* As a simple, firstpass implementation of VK_ANDROID_native_buffer, we
454 * block on the nativeFenceFd. This may introduce latency and is
455 * definitiely inefficient, yet it's correct.
456 *
457 * FINISHME(chadv): Import the nativeFenceFd into the VkSemaphore and
458 * VkFence.
459 */
460 if (sync_wait(nativeFenceFd, /*timeout*/ -1) < 0) {
461 result = vk_errorf(device->instance, device, VK_ERROR_DEVICE_LOST,
462 "%s: failed to wait on nativeFenceFd=%d",
463 __func__, nativeFenceFd);
464 }
465
466 /* From VK_ANDROID_native_buffer's pseudo spec
467 * (https://source.android.com/devices/graphics/implement-vulkan):
468 *
469 * The driver takes ownership of the fence fd and is responsible for
470 * closing it [...] even if vkAcquireImageANDROID fails and returns
471 * an error.
472 */
473 close(nativeFenceFd);
474
475 if (result != VK_SUCCESS)
476 return result;
477 }
478
479 if (semaphore_h || fence_h) {
480 /* Thanks to implicit sync, the image is ready for GPU access. But we
481 * must still put the semaphore into the "submit" state; otherwise the
482 * client may get unexpected behavior if the client later uses it as
483 * a wait semaphore.
484 *
485 * Because we blocked above on the nativeFenceFd, the image is also
486 * ready for foreign-device access (including CPU access). But we must
487 * still signal the fence; otherwise the client may get unexpected
488 * behavior if the client later waits on it.
489 *
490 * For some values of anv_semaphore_type, we must submit the semaphore
491 * to execbuf in order to signal it. Likewise for anv_fence_type.
492 * Instead of open-coding here the signal operation for each
493 * anv_semaphore_type and anv_fence_type, we piggy-back on
494 * vkQueueSubmit.
495 */
496 const VkSubmitInfo submit = {
497 .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
498 .waitSemaphoreCount = 0,
499 .commandBufferCount = 0,
500 .signalSemaphoreCount = (semaphore_h ? 1 : 0),
501 .pSignalSemaphores = &semaphore_h,
502 };
503
504 result = anv_QueueSubmit(anv_queue_to_handle(&device->queue), 1,
505 &submit, fence_h);
506 if (result != VK_SUCCESS) {
507 return vk_errorf(device->instance, device, result,
508 "anv_QueueSubmit failed inside %s", __func__);
509 }
510 }
511
512 return VK_SUCCESS;
513 }
514
515 VkResult
516 anv_QueueSignalReleaseImageANDROID(
517 VkQueue queue,
518 uint32_t waitSemaphoreCount,
519 const VkSemaphore* pWaitSemaphores,
520 VkImage image,
521 int* pNativeFenceFd)
522 {
523 VkResult result;
524
525 if (waitSemaphoreCount == 0)
526 goto done;
527
528 result = anv_QueueSubmit(queue, 1,
529 &(VkSubmitInfo) {
530 .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
531 .waitSemaphoreCount = 1,
532 .pWaitSemaphores = pWaitSemaphores,
533 },
534 (VkFence) VK_NULL_HANDLE);
535 if (result != VK_SUCCESS)
536 return result;
537
538 done:
539 if (pNativeFenceFd) {
540 /* We can rely implicit on sync because above we submitted all
541 * semaphores to the queue.
542 */
543 *pNativeFenceFd = -1;
544 }
545
546 return VK_SUCCESS;
547 }