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
25static inline const VkNativeBufferANDROID *
26vn_android_find_native_buffer(const VkImageCreateInfo *create_info)
27{
28   return vk_find_struct_const(create_info->pNext, NATIVE_BUFFER_ANDROID);
29}
30
31VkResult
32vn_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
38bool
39vn_android_get_drm_format_modifier_info(
40   const VkPhysicalDeviceImageFormatInfo2 *format_info,
41   VkPhysicalDeviceImageDrmFormatModifierInfoEXT *out_info);
42
43uint64_t
44vn_android_get_ahb_usage(const VkImageUsageFlags usage,
45                         const VkImageCreateFlags flags);
46
47VkResult
48vn_android_image_from_ahb(struct vn_device *dev,
49                          const VkImageCreateInfo *create_info,
50                          const VkAllocationCallbacks *alloc,
51                          struct vn_image **out_img);
52
53VkResult
54vn_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
60VkResult
61vn_android_device_allocate_ahb(struct vn_device *dev,
62                               struct vn_device_memory *mem,
63                               const VkMemoryAllocateInfo *alloc_info,
64                               const VkAllocationCallbacks *alloc);
65
66void
67vn_android_release_ahb(struct AHardwareBuffer *ahb);
68
69VkFormat
70vn_android_drm_format_to_vk_format(uint32_t format);
71
72VkResult
73vn_android_buffer_from_ahb(struct vn_device *dev,
74                           const VkBufferCreateInfo *create_info,
75                           const VkAllocationCallbacks *alloc,
76                           struct vn_buffer **out_buf);
77
78VkResult
79vn_android_init_ahb_buffer_memory_type_bits(struct vn_device *dev);
80
81#else
82
83static inline const VkNativeBufferANDROID *
84vn_android_find_native_buffer(UNUSED const VkImageCreateInfo *create_info)
85{
86   return NULL;
87}
88
89static inline VkResult
90vn_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
99static inline bool
100vn_android_get_drm_format_modifier_info(
101   UNUSED const VkPhysicalDeviceImageFormatInfo2 *format_info,
102   UNUSED VkPhysicalDeviceImageDrmFormatModifierInfoEXT *out_info)
103{
104   return false;
105}
106
107static inline uint64_t
108vn_android_get_ahb_usage(UNUSED const VkImageUsageFlags usage,
109                         UNUSED const VkImageCreateFlags flags)
110{
111   return 0;
112}
113
114static inline VkResult
115vn_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
123static inline VkResult
124vn_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
133static inline VkResult
134vn_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
142static inline void
143vn_android_release_ahb(UNUSED struct AHardwareBuffer *ahb)
144{
145   return;
146}
147
148static inline VkFormat
149vn_android_drm_format_to_vk_format(UNUSED uint32_t format)
150{
151   return VK_FORMAT_UNDEFINED;
152}
153
154static inline VkResult
155vn_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
163static inline VkResult
164vn_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