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