1 /*
2  * Copyright 2021 Google LLC
3  * SPDX-License-Identifier: MIT
4  *
5  * based in part on anv and radv which are:
6  * Copyright © 2015 Intel Corporation
7  * Copyright © 2016 Red Hat
8  * Copyright © 2016 Bas Nieuwenhuizen
9  */
10 
11 #ifndef VN_ANDROID_H
12 #define VN_ANDROID_H
13 
14 #include "vn_common.h"
15 
16 #include <vulkan/vk_android_native_buffer.h>
17 #include <vulkan/vulkan.h>
18 #include <vulkan/vulkan_android.h>
19 
20 /* venus implements VK_ANDROID_native_buffer up to spec version 7 */
21 #define VN_ANDROID_NATIVE_BUFFER_SPEC_VERSION 7
22 
23 #ifdef ANDROID
24 
25 static inline const VkNativeBufferANDROID *
vn_android_find_native_buffer(const VkImageCreateInfo * create_info)26 vn_android_find_native_buffer(const VkImageCreateInfo *create_info)
27 {
28    return vk_find_struct_const(create_info->pNext, NATIVE_BUFFER_ANDROID);
29 }
30 
31 VkResult
32 vn_android_image_from_anb(struct vn_device *dev,
33                           const VkImageCreateInfo *image_info,
34                           const VkNativeBufferANDROID *anb_info,
35                           const VkAllocationCallbacks *alloc,
36                           struct vn_image **out_img);
37 
38 bool
39 vn_android_get_drm_format_modifier_info(
40    const VkPhysicalDeviceImageFormatInfo2 *format_info,
41    VkPhysicalDeviceImageDrmFormatModifierInfoEXT *out_info);
42 
43 uint64_t
44 vn_android_get_ahb_usage(const VkImageUsageFlags usage,
45                          const VkImageCreateFlags flags);
46 
47 VkResult
48 vn_android_image_from_ahb(struct vn_device *dev,
49                           const VkImageCreateInfo *create_info,
50                           const VkAllocationCallbacks *alloc,
51                           struct vn_image **out_img);
52 
53 VkResult
54 vn_android_device_import_ahb(struct vn_device *dev,
55                              struct vn_device_memory *mem,
56                              const VkMemoryAllocateInfo *alloc_info,
57                              const VkAllocationCallbacks *alloc,
58                              struct AHardwareBuffer *ahb);
59 
60 VkResult
61 vn_android_device_allocate_ahb(struct vn_device *dev,
62                                struct vn_device_memory *mem,
63                                const VkMemoryAllocateInfo *alloc_info,
64                                const VkAllocationCallbacks *alloc);
65 
66 void
67 vn_android_release_ahb(struct AHardwareBuffer *ahb);
68 
69 VkFormat
70 vn_android_drm_format_to_vk_format(uint32_t format);
71 
72 VkResult
73 vn_android_buffer_from_ahb(struct vn_device *dev,
74                            const VkBufferCreateInfo *create_info,
75                            const VkAllocationCallbacks *alloc,
76                            struct vn_buffer **out_buf);
77 
78 VkResult
79 vn_android_init_ahb_buffer_memory_type_bits(struct vn_device *dev);
80 
81 #else
82 
83 static inline const VkNativeBufferANDROID *
vn_android_find_native_buffer(UNUSED const VkImageCreateInfo * create_info)84 vn_android_find_native_buffer(UNUSED const VkImageCreateInfo *create_info)
85 {
86    return NULL;
87 }
88 
89 static inline VkResult
vn_android_image_from_anb(UNUSED struct vn_device * dev,UNUSED const VkImageCreateInfo * image_info,UNUSED const VkNativeBufferANDROID * anb_info,UNUSED const VkAllocationCallbacks * alloc,UNUSED struct vn_image ** out_img)90 vn_android_image_from_anb(UNUSED struct vn_device *dev,
91                           UNUSED const VkImageCreateInfo *image_info,
92                           UNUSED const VkNativeBufferANDROID *anb_info,
93                           UNUSED const VkAllocationCallbacks *alloc,
94                           UNUSED struct vn_image **out_img)
95 {
96    return VK_ERROR_OUT_OF_HOST_MEMORY;
97 }
98 
99 static inline bool
vn_android_get_drm_format_modifier_info(UNUSED const VkPhysicalDeviceImageFormatInfo2 * format_info,UNUSED VkPhysicalDeviceImageDrmFormatModifierInfoEXT * out_info)100 vn_android_get_drm_format_modifier_info(
101    UNUSED const VkPhysicalDeviceImageFormatInfo2 *format_info,
102    UNUSED VkPhysicalDeviceImageDrmFormatModifierInfoEXT *out_info)
103 {
104    return false;
105 }
106 
107 static inline uint64_t
vn_android_get_ahb_usage(UNUSED const VkImageUsageFlags usage,UNUSED const VkImageCreateFlags flags)108 vn_android_get_ahb_usage(UNUSED const VkImageUsageFlags usage,
109                          UNUSED const VkImageCreateFlags flags)
110 {
111    return 0;
112 }
113 
114 static inline VkResult
vn_android_image_from_ahb(UNUSED struct vn_device * dev,UNUSED const VkImageCreateInfo * create_info,UNUSED const VkAllocationCallbacks * alloc,UNUSED struct vn_image ** out_img)115 vn_android_image_from_ahb(UNUSED struct vn_device *dev,
116                           UNUSED const VkImageCreateInfo *create_info,
117                           UNUSED const VkAllocationCallbacks *alloc,
118                           UNUSED struct vn_image **out_img)
119 {
120    return VK_ERROR_OUT_OF_HOST_MEMORY;
121 }
122 
123 static inline VkResult
vn_android_device_import_ahb(UNUSED struct vn_device * dev,UNUSED struct vn_device_memory * mem,UNUSED const VkMemoryAllocateInfo * alloc_info,UNUSED const VkAllocationCallbacks * alloc,UNUSED struct AHardwareBuffer * ahb)124 vn_android_device_import_ahb(UNUSED struct vn_device *dev,
125                              UNUSED struct vn_device_memory *mem,
126                              UNUSED const VkMemoryAllocateInfo *alloc_info,
127                              UNUSED const VkAllocationCallbacks *alloc,
128                              UNUSED struct AHardwareBuffer *ahb)
129 {
130    return VK_ERROR_OUT_OF_HOST_MEMORY;
131 }
132 
133 static inline VkResult
vn_android_device_allocate_ahb(UNUSED struct vn_device * dev,UNUSED struct vn_device_memory * mem,UNUSED const VkMemoryAllocateInfo * alloc_info,UNUSED const VkAllocationCallbacks * alloc)134 vn_android_device_allocate_ahb(UNUSED struct vn_device *dev,
135                                UNUSED struct vn_device_memory *mem,
136                                UNUSED const VkMemoryAllocateInfo *alloc_info,
137                                UNUSED const VkAllocationCallbacks *alloc)
138 {
139    return VK_ERROR_OUT_OF_HOST_MEMORY;
140 }
141 
142 static inline void
vn_android_release_ahb(UNUSED struct AHardwareBuffer * ahb)143 vn_android_release_ahb(UNUSED struct AHardwareBuffer *ahb)
144 {
145    return;
146 }
147 
148 static inline VkFormat
vn_android_drm_format_to_vk_format(UNUSED uint32_t format)149 vn_android_drm_format_to_vk_format(UNUSED uint32_t format)
150 {
151    return VK_FORMAT_UNDEFINED;
152 }
153 
154 static inline VkResult
vn_android_buffer_from_ahb(UNUSED struct vn_device * dev,UNUSED const VkBufferCreateInfo * create_info,UNUSED const VkAllocationCallbacks * alloc,UNUSED struct vn_buffer ** out_buf)155 vn_android_buffer_from_ahb(UNUSED struct vn_device *dev,
156                            UNUSED const VkBufferCreateInfo *create_info,
157                            UNUSED const VkAllocationCallbacks *alloc,
158                            UNUSED struct vn_buffer **out_buf)
159 {
160    return VK_ERROR_OUT_OF_HOST_MEMORY;
161 }
162 
163 static inline VkResult
vn_android_init_ahb_buffer_memory_type_bits(UNUSED struct vn_device * dev)164 vn_android_init_ahb_buffer_memory_type_bits(UNUSED struct vn_device *dev)
165 {
166    return VK_ERROR_FEATURE_NOT_PRESENT;
167 }
168 
169 #endif /* ANDROID */
170 
171 #endif /* VN_ANDROID_H */
172