turnip: Add driver skeleton (v2)
[mesa.git] / src / freedreno / vulkan / tu_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 <libsync.h>
28 #include <vulkan/vk_android_native_buffer.h>
29 #include <vulkan/vk_icd.h>
30
31 #include "tu_private.h"
32
33 static int
34 tu_hal_open(const struct hw_module_t *mod,
35 const char *id,
36 struct hw_device_t **dev);
37 static int
38 tu_hal_close(struct hw_device_t *dev);
39
40 static void UNUSED
41 static_asserts(void)
42 {
43 STATIC_ASSERT(HWVULKAN_DISPATCH_MAGIC == ICD_LOADER_MAGIC);
44 }
45
46 PUBLIC struct hwvulkan_module_t HAL_MODULE_INFO_SYM = {
47 .common =
48 {
49 .tag = HARDWARE_MODULE_TAG,
50 .module_api_version = HWVULKAN_MODULE_API_VERSION_0_1,
51 .hal_api_version = HARDWARE_MAKE_API_VERSION(1, 0),
52 .id = HWVULKAN_HARDWARE_MODULE_ID,
53 .name = "AMD Vulkan HAL",
54 .author = "Google",
55 .methods =
56 &(hw_module_methods_t){
57 .open = tu_hal_open,
58 },
59 },
60 };
61
62 /* If any bits in test_mask are set, then unset them and return true. */
63 static inline bool
64 unmask32(uint32_t *inout_mask, uint32_t test_mask)
65 {
66 uint32_t orig_mask = *inout_mask;
67 *inout_mask &= ~test_mask;
68 return *inout_mask != orig_mask;
69 }
70
71 static int
72 tu_hal_open(const struct hw_module_t *mod,
73 const char *id,
74 struct hw_device_t **dev)
75 {
76 assert(mod == &HAL_MODULE_INFO_SYM.common);
77 assert(strcmp(id, HWVULKAN_DEVICE_0) == 0);
78
79 hwvulkan_device_t *hal_dev = malloc(sizeof(*hal_dev));
80 if (!hal_dev)
81 return -1;
82
83 *hal_dev = (hwvulkan_device_t){
84 .common =
85 {
86 .tag = HARDWARE_DEVICE_TAG,
87 .version = HWVULKAN_DEVICE_API_VERSION_0_1,
88 .module = &HAL_MODULE_INFO_SYM.common,
89 .close = tu_hal_close,
90 },
91 .EnumerateInstanceExtensionProperties =
92 tu_EnumerateInstanceExtensionProperties,
93 .CreateInstance = tu_CreateInstance,
94 .GetInstanceProcAddr = tu_GetInstanceProcAddr,
95 };
96
97 *dev = &hal_dev->common;
98 return 0;
99 }
100
101 static int
102 tu_hal_close(struct hw_device_t *dev)
103 {
104 /* hwvulkan.h claims that hw_device_t::close() is never called. */
105 return -1;
106 }
107
108 VkResult
109 tu_image_from_gralloc(VkDevice device_h,
110 const VkImageCreateInfo *base_info,
111 const VkNativeBufferANDROID *gralloc_info,
112 const VkAllocationCallbacks *alloc,
113 VkImage *out_image_h)
114
115 {
116 TU_FROM_HANDLE(tu_device, device, device_h);
117 VkImage image_h = VK_NULL_HANDLE;
118 struct tu_image *image = NULL;
119 struct tu_bo *bo = NULL;
120 VkResult result;
121
122 result = tu_image_create(
123 device_h,
124 &(struct tu_image_create_info){
125 .vk_info = base_info, .scanout = true, .no_metadata_planes = true },
126 alloc,
127 &image_h);
128
129 if (result != VK_SUCCESS)
130 return result;
131
132 if (gralloc_info->handle->numFds != 1) {
133 return vk_errorf(device->instance,
134 VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR,
135 "VkNativeBufferANDROID::handle::numFds is %d, "
136 "expected 1",
137 gralloc_info->handle->numFds);
138 }
139
140 /* Do not close the gralloc handle's dma_buf. The lifetime of the dma_buf
141 * must exceed that of the gralloc handle, and we do not own the gralloc
142 * handle.
143 */
144 int dma_buf = gralloc_info->handle->data[0];
145
146 image = tu_image_from_handle(image_h);
147
148 VkDeviceMemory memory_h;
149
150 const VkMemoryDedicatedAllocateInfoKHR ded_alloc = {
151 .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO_KHR,
152 .pNext = NULL,
153 .buffer = VK_NULL_HANDLE,
154 .image = image_h
155 };
156
157 const VkImportMemoryFdInfoKHR import_info = {
158 .sType = VK_STRUCTURE_TYPE_IMPORT_MEMORY_FD_INFO_KHR,
159 .pNext = &ded_alloc,
160 .handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT_KHR,
161 .fd = dup(dma_buf),
162 };
163 /* Find the first VRAM memory type, or GART for PRIME images. */
164 int memory_type_index = -1;
165 for (int i = 0;
166 i < device->physical_device->memory_properties.memoryTypeCount;
167 ++i) {
168 bool is_local =
169 !!(device->physical_device->memory_properties.memoryTypes[i]
170 .propertyFlags &
171 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
172 if (is_local) {
173 memory_type_index = i;
174 break;
175 }
176 }
177
178 /* fallback */
179 if (memory_type_index == -1)
180 memory_type_index = 0;
181
182 result =
183 tu_AllocateMemory(device_h,
184 &(VkMemoryAllocateInfo){
185 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
186 .pNext = &import_info,
187 .allocationSize = image->size,
188 .memoryTypeIndex = memory_type_index,
189 },
190 alloc,
191 &memory_h);
192 if (result != VK_SUCCESS)
193 goto fail_create_image;
194
195 tu_BindImageMemory(device_h, image_h, memory_h, 0);
196
197 image->owned_memory = memory_h;
198 /* Don't clobber the out-parameter until success is certain. */
199 *out_image_h = image_h;
200
201 return VK_SUCCESS;
202
203 fail_create_image:
204 fail_size:
205 tu_DestroyImage(device_h, image_h, alloc);
206
207 return result;
208 }
209
210 VkResult
211 tu_GetSwapchainGrallocUsageANDROID(VkDevice device_h,
212 VkFormat format,
213 VkImageUsageFlags imageUsage,
214 int *grallocUsage)
215 {
216 TU_FROM_HANDLE(tu_device, device, device_h);
217 struct tu_physical_device *phys_dev = device->physical_device;
218 VkPhysicalDevice phys_dev_h = tu_physical_device_to_handle(phys_dev);
219 VkResult result;
220
221 *grallocUsage = 0;
222
223 /* WARNING: Android Nougat's libvulkan.so hardcodes the VkImageUsageFlags
224 * returned to applications via
225 * VkSurfaceCapabilitiesKHR::supportedUsageFlags.
226 * The relevant code in libvulkan/swapchain.cpp contains this fun comment:
227 *
228 * TODO(jessehall): I think these are right, but haven't thought hard
229 * about it. Do we need to query the driver for support of any of
230 * these?
231 *
232 * Any disagreement between this function and the hardcoded
233 * VkSurfaceCapabilitiesKHR:supportedUsageFlags causes tests
234 * dEQP-VK.wsi.android.swapchain.*.image_usage to fail.
235 */
236
237 const VkPhysicalDeviceImageFormatInfo2KHR image_format_info = {
238 .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2_KHR,
239 .format = format,
240 .type = VK_IMAGE_TYPE_2D,
241 .tiling = VK_IMAGE_TILING_OPTIMAL,
242 .usage = imageUsage,
243 };
244
245 VkImageFormatProperties2KHR image_format_props = {
246 .sType = VK_STRUCTURE_TYPE_IMAGE_FORMAT_PROPERTIES_2_KHR,
247 };
248
249 /* Check that requested format and usage are supported. */
250 result = tu_GetPhysicalDeviceImageFormatProperties2(
251 phys_dev_h, &image_format_info, &image_format_props);
252 if (result != VK_SUCCESS) {
253 return vk_errorf(device->instance,
254 result,
255 "tu_GetPhysicalDeviceImageFormatProperties2 failed "
256 "inside %s",
257 __func__);
258 }
259
260 if (unmask32(&imageUsage,
261 VK_IMAGE_USAGE_TRANSFER_DST_BIT |
262 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT))
263 *grallocUsage |= GRALLOC_USAGE_HW_RENDER;
264
265 if (unmask32(&imageUsage,
266 VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_SAMPLED_BIT |
267 VK_IMAGE_USAGE_STORAGE_BIT |
268 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT))
269 *grallocUsage |= GRALLOC_USAGE_HW_TEXTURE;
270
271 /* All VkImageUsageFlags not explicitly checked here are unsupported for
272 * gralloc swapchains.
273 */
274 if (imageUsage != 0) {
275 return vk_errorf(device->instance,
276 VK_ERROR_FORMAT_NOT_SUPPORTED,
277 "unsupported VkImageUsageFlags(0x%x) for gralloc "
278 "swapchain",
279 imageUsage);
280 }
281
282 /*
283 * FINISHME: Advertise all display-supported formats. Mostly
284 * DRM_FORMAT_ARGB2101010 and DRM_FORMAT_ABGR2101010, but need to check
285 * what we need for 30-bit colors.
286 */
287 if (format == VK_FORMAT_B8G8R8A8_UNORM ||
288 format == VK_FORMAT_B5G6R5_UNORM_PACK16) {
289 *grallocUsage |= GRALLOC_USAGE_HW_FB | GRALLOC_USAGE_HW_COMPOSER |
290 GRALLOC_USAGE_EXTERNAL_DISP;
291 }
292
293 if (*grallocUsage == 0)
294 return VK_ERROR_FORMAT_NOT_SUPPORTED;
295
296 return VK_SUCCESS;
297 }
298
299 VkResult
300 tu_AcquireImageANDROID(VkDevice device,
301 VkImage image_h,
302 int nativeFenceFd,
303 VkSemaphore semaphore,
304 VkFence fence)
305 {
306 VkResult semaphore_result = VK_SUCCESS, fence_result = VK_SUCCESS;
307
308 if (semaphore != VK_NULL_HANDLE) {
309 int semaphore_fd =
310 nativeFenceFd >= 0 ? dup(nativeFenceFd) : nativeFenceFd;
311 semaphore_result = tu_ImportSemaphoreFdKHR(
312 device,
313 &(VkImportSemaphoreFdInfoKHR){
314 .sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR,
315 .flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT_KHR,
316 .fd = semaphore_fd,
317 .semaphore = semaphore,
318 });
319 }
320
321 if (fence != VK_NULL_HANDLE) {
322 int fence_fd = nativeFenceFd >= 0 ? dup(nativeFenceFd) : nativeFenceFd;
323 fence_result = tu_ImportFenceFdKHR(
324 device,
325 &(VkImportFenceFdInfoKHR){
326 .sType = VK_STRUCTURE_TYPE_IMPORT_FENCE_FD_INFO_KHR,
327 .flags = VK_FENCE_IMPORT_TEMPORARY_BIT_KHR,
328 .fd = fence_fd,
329 .fence = fence,
330 });
331 }
332
333 close(nativeFenceFd);
334
335 if (semaphore_result != VK_SUCCESS)
336 return semaphore_result;
337 return fence_result;
338 }
339
340 VkResult
341 tu_QueueSignalReleaseImageANDROID(VkQueue _queue,
342 uint32_t waitSemaphoreCount,
343 const VkSemaphore *pWaitSemaphores,
344 VkImage image,
345 int *pNativeFenceFd)
346 {
347 TU_FROM_HANDLE(tu_queue, queue, _queue);
348 VkResult result = VK_SUCCESS;
349
350 if (waitSemaphoreCount == 0) {
351 if (pNativeFenceFd)
352 *pNativeFenceFd = -1;
353 return VK_SUCCESS;
354 }
355
356 int fd = -1;
357
358 for (uint32_t i = 0; i < waitSemaphoreCount; ++i) {
359 int tmp_fd;
360 result = tu_GetSemaphoreFdKHR(
361 tu_device_to_handle(queue->device),
362 &(VkSemaphoreGetFdInfoKHR){
363 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR,
364 .handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT_KHR,
365 .semaphore = pWaitSemaphores[i],
366 },
367 &tmp_fd);
368 if (result != VK_SUCCESS) {
369 if (fd >= 0)
370 close(fd);
371 return result;
372 }
373
374 if (fd < 0)
375 fd = tmp_fd;
376 else if (tmp_fd >= 0) {
377 sync_accumulate("tu", &fd, tmp_fd);
378 close(tmp_fd);
379 }
380 }
381
382 if (pNativeFenceFd) {
383 *pNativeFenceFd = fd;
384 } else if (fd >= 0) {
385 close(fd);
386 /* We still need to do the exports, to reset the semaphores, but
387 * otherwise we don't wait on them. */
388 }
389 return VK_SUCCESS;
390 }