1 /*
2 * Copyright © 2017, Google Inc.
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "tu_android.h"
7
8 #include <android/hardware_buffer.h>
9 #include <hardware/hardware.h>
10 #include <hardware/hwvulkan.h>
11 #include <stdbool.h>
12
13 #include "util/u_gralloc/u_gralloc.h"
14 #include "vk_android.h"
15
16 #include "tu_device.h"
17
18 static int
19 tu_hal_open(const struct hw_module_t *mod,
20 const char *id,
21 struct hw_device_t **dev);
22 static int
23 tu_hal_close(struct hw_device_t *dev);
24
25 static_assert(HWVULKAN_DISPATCH_MAGIC == ICD_LOADER_MAGIC, "");
26
27 struct hw_module_methods_t HAL_MODULE_METHODS = {
28 .open = tu_hal_open,
29 };
30
31 PUBLIC struct hwvulkan_module_t HAL_MODULE_INFO_SYM = {
32 .common =
33 {
34 .tag = HARDWARE_MODULE_TAG,
35 .module_api_version = HWVULKAN_MODULE_API_VERSION_0_1,
36 .hal_api_version = HARDWARE_MAKE_API_VERSION(1, 0),
37 .id = HWVULKAN_HARDWARE_MODULE_ID,
38 .name = "Turnip Vulkan HAL",
39 .author = "Google",
40 .methods = &HAL_MODULE_METHODS,
41 },
42 };
43
44 static int
tu_hal_open(const struct hw_module_t * mod,const char * id,struct hw_device_t ** dev)45 tu_hal_open(const struct hw_module_t *mod,
46 const char *id,
47 struct hw_device_t **dev)
48 {
49 assert(mod == &HAL_MODULE_INFO_SYM.common);
50 assert(strcmp(id, HWVULKAN_DEVICE_0) == 0);
51
52 hwvulkan_device_t *hal_dev = (hwvulkan_device_t *) malloc(sizeof(*hal_dev));
53 if (!hal_dev)
54 return -1;
55
56 *hal_dev = (hwvulkan_device_t){
57 .common =
58 {
59 .tag = HARDWARE_DEVICE_TAG,
60 .version = HWVULKAN_DEVICE_API_VERSION_0_1,
61 .module = &HAL_MODULE_INFO_SYM.common,
62 .close = tu_hal_close,
63 },
64 .EnumerateInstanceExtensionProperties =
65 tu_EnumerateInstanceExtensionProperties,
66 .CreateInstance = tu_CreateInstance,
67 .GetInstanceProcAddr = tu_GetInstanceProcAddr,
68 };
69
70 vk_android_init_ugralloc();
71
72 *dev = &hal_dev->common;
73 return 0;
74 }
75
76 static int
tu_hal_close(struct hw_device_t * dev)77 tu_hal_close(struct hw_device_t *dev)
78 {
79 /* hwvulkan.h claims that hw_device_t::close() is never called. */
80 vk_android_destroy_ugralloc();
81 return -1;
82 }
83