1 /* 2 * Copyright 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef __VK_ANDROID_NATIVE_BUFFER_H__ 18 #define __VK_ANDROID_NATIVE_BUFFER_H__ 19 20 /* MESA: A hack to avoid #ifdefs in driver code. */ 21 #ifdef ANDROID 22 #include <cutils/native_handle.h> 23 #include <vulkan/vulkan.h> 24 25 #if ANDROID_API_LEVEL < 28 26 /* buffer_handle_t was defined in the deprecated system/window.h */ 27 typedef const native_handle_t *buffer_handle_t; 28 #endif 29 30 #else 31 typedef void *buffer_handle_t; 32 #endif 33 34 #ifdef __cplusplus 35 extern "C" { 36 #endif 37 38 #define VK_ANDROID_native_buffer 1 39 40 #define VK_ANDROID_NATIVE_BUFFER_EXTENSION_NUMBER 11 41 /* 42 * NOTE ON VK_ANDROID_NATIVE_BUFFER_SPEC_VERSION 6 43 * 44 * This version of the extension transitions from gralloc0 to gralloc1 usage 45 * flags (int -> 2x uint64_t). The WSI implementation will temporarily continue 46 * to fill out deprecated fields in VkNativeBufferANDROID, and will call the 47 * deprecated vkGetSwapchainGrallocUsageANDROID if the new 48 * vkGetSwapchainGrallocUsage2ANDROID is not supported. This transitionary 49 * backwards-compatibility support is temporary, and will likely be removed 50 * (along with all gralloc0 support) in a future release. 51 */ 52 /* 53 * NOTE ON VK_ANDROID_NATIVE_BUFFER_SPEC_VERSION 8 54 * 55 * This version of the extension doesn't introduce new types or structs, but is 56 * to accommodate the new struct VkBindImageMemorySwapchainInfoKHR added in 57 * VK_KHR_swapchain spec version 69. When VkBindImageMemorySwapchainInfoKHR is 58 * chained in the pNext chain of VkBindImageMemoryInfo, a VkNativeBufferANDROID 59 * that holds the correct gralloc handle according to the imageIndex specified 60 * in VkBindImageMemorySwapchainInfoKHR will be additionally chained to the 61 * pNext chain of VkBindImageMemoryInfo and passed down to the driver. 62 */ 63 /* 64 * NOTE ON VK_ANDROID_NATIVE_BUFFER_SPEC_VERSION 9 65 * 66 * This version of the extension is largely designed to clean up the mix of 67 * GrallocUsage and GrallocUsage2 68 */ 69 #define VK_ANDROID_NATIVE_BUFFER_SPEC_VERSION 9 70 #define VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME "VK_ANDROID_native_buffer" 71 72 #define VK_ANDROID_NATIVE_BUFFER_ENUM(type, id) \ 73 ((type)(1000000000 + \ 74 (1000 * (VK_ANDROID_NATIVE_BUFFER_EXTENSION_NUMBER - 1)) + (id))) 75 #define VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID \ 76 VK_ANDROID_NATIVE_BUFFER_ENUM(VkStructureType, 0) 77 #define VK_STRUCTURE_TYPE_SWAPCHAIN_IMAGE_CREATE_INFO_ANDROID \ 78 VK_ANDROID_NATIVE_BUFFER_ENUM(VkStructureType, 1) 79 #define VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENTATION_PROPERTIES_ANDROID \ 80 VK_ANDROID_NATIVE_BUFFER_ENUM(VkStructureType, 2) 81 #define VK_STRUCTURE_TYPE_GRALLOC_USAGE_INFO_ANDROID \ 82 VK_ANDROID_NATIVE_BUFFER_ENUM(VkStructureType, 3) 83 84 /* clang-format off */ 85 typedef enum VkSwapchainImageUsageFlagBitsANDROID { 86 VK_SWAPCHAIN_IMAGE_USAGE_SHARED_BIT_ANDROID = 0x00000001, 87 VK_SWAPCHAIN_IMAGE_USAGE_FLAG_BITS_MAX_ENUM = 0x7FFFFFFF 88 } VkSwapchainImageUsageFlagBitsANDROID; 89 typedef VkFlags VkSwapchainImageUsageFlagsANDROID; 90 91 /* 92 * struct VkNativeBufferUsage2ANDROID 93 * 94 * consumer: gralloc1 consumer usage flag 95 * producer: gralloc1 producer usage flag 96 */ 97 typedef struct { 98 uint64_t consumer; 99 uint64_t producer; 100 } VkNativeBufferUsage2ANDROID; 101 102 /* 103 * struct VkNativeBufferANDROID 104 * 105 * sType: VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID 106 * pNext: NULL or a pointer to a structure extending this structure 107 * handle: buffer handle returned from gralloc alloc() 108 * stride: stride returned from gralloc alloc() 109 * format: gralloc format requested when the buffer was allocated 110 * usage: gralloc usage requested when the buffer was allocated 111 * usage2: gralloc usage requested when the buffer was allocated 112 * usage3: gralloc usage requested when the buffer was allocated 113 */ 114 typedef struct { 115 VkStructureType sType; 116 const void* pNext; 117 buffer_handle_t handle; 118 int stride; 119 int format; 120 int usage; /* DEPRECATED in SPEC_VERSION 6 */ 121 VkNativeBufferUsage2ANDROID usage2; /* DEPRECATED in SPEC_VERSION 9 */ 122 uint64_t usage3; /* ADDED in SPEC_VERSION 9 */ 123 } VkNativeBufferANDROID; 124 125 /* 126 * struct VkSwapchainImageCreateInfoANDROID 127 * 128 * sType: VK_STRUCTURE_TYPE_SWAPCHAIN_IMAGE_CREATE_INFO_ANDROID 129 * pNext: NULL or a pointer to a structure extending this structure 130 * usage: is a bitmask of VkSwapchainImageUsageFlagsANDROID 131 */ 132 typedef struct { 133 VkStructureType sType; 134 const void* pNext; 135 VkSwapchainImageUsageFlagsANDROID usage; 136 } VkSwapchainImageCreateInfoANDROID; 137 138 /* 139 * struct VkPhysicalDevicePresentationPropertiesANDROID 140 * 141 * sType: VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENTATION_PROPERTIES_ANDROID 142 * pNext: NULL or a pointer to a structure extending this structure 143 * sharedImage: specifies if the image can be shared with the display system 144 */ 145 typedef struct { 146 VkStructureType sType; 147 const void* pNext; 148 VkBool32 sharedImage; 149 } VkPhysicalDevicePresentationPropertiesANDROID; 150 151 /* 152 * struct VkGrallocUsageInfoANDROID 153 * 154 * sType: VK_STRUCTURE_TYPE_GRALLOC_USAGE_INFO_ANDROID 155 * pNext: NULL or a pointer to a structure extending this structure 156 * format: value specifying the format the image will be created with 157 * imageUsage: bitmask of VkImageUsageFlagBits describing intended usage 158 */ 159 typedef struct { 160 VkStructureType sType; 161 const void* pNext; 162 VkFormat format; 163 VkImageUsageFlags imageUsage; 164 } VkGrallocUsageInfoANDROID; 165 166 /* DEPRECATED in SPEC_VERSION 6 */ 167 typedef VkResult (VKAPI_PTR *PFN_vkGetSwapchainGrallocUsageANDROID)( 168 VkDevice device, 169 VkFormat format, 170 VkImageUsageFlags imageUsage, 171 int* grallocUsage); 172 173 /* DEPRECATED in SPEC_VERSION 9 */ 174 typedef VkResult (VKAPI_PTR *PFN_vkGetSwapchainGrallocUsage2ANDROID)( 175 VkDevice device, 176 VkFormat format, 177 VkImageUsageFlags imageUsage, 178 VkSwapchainImageUsageFlagsANDROID swapchainImageUsage, 179 uint64_t* grallocConsumerUsage, 180 uint64_t* grallocProducerUsage); 181 182 /* ADDED in SPEC_VERSION 9 */ 183 typedef VkResult (VKAPI_PTR *PFN_vkGetSwapchainGrallocUsage3ANDROID)( 184 VkDevice device, 185 const VkGrallocUsageInfoANDROID* grallocUsageInfo, 186 uint64_t* grallocUsage); 187 188 typedef VkResult (VKAPI_PTR *PFN_vkAcquireImageANDROID)( 189 VkDevice device, 190 VkImage image, 191 int nativeFenceFd, 192 VkSemaphore semaphore, 193 VkFence fence); 194 195 typedef VkResult (VKAPI_PTR *PFN_vkQueueSignalReleaseImageANDROID)( 196 VkQueue queue, 197 uint32_t waitSemaphoreCount, 198 const VkSemaphore* pWaitSemaphores, 199 VkImage image, 200 int* pNativeFenceFd); 201 202 #ifndef VK_NO_PROTOTYPES 203 204 /* DEPRECATED in SPEC_VERSION 6 */ 205 VKAPI_ATTR VkResult VKAPI_CALL vkGetSwapchainGrallocUsageANDROID( 206 VkDevice device, 207 VkFormat format, 208 VkImageUsageFlags imageUsage, 209 int* grallocUsage 210 ); 211 212 /* DEPRECATED in SPEC_VERSION 9 */ 213 VKAPI_ATTR VkResult VKAPI_CALL vkGetSwapchainGrallocUsage2ANDROID( 214 VkDevice device, 215 VkFormat format, 216 VkImageUsageFlags imageUsage, 217 VkSwapchainImageUsageFlagsANDROID swapchainImageUsage, 218 uint64_t* grallocConsumerUsage, 219 uint64_t* grallocProducerUsage 220 ); 221 222 /* ADDED in SPEC_VERSION 9 */ 223 VKAPI_ATTR VkResult VKAPI_CALL vkGetSwapchainGrallocUsage3ANDROID( 224 VkDevice device, 225 const VkGrallocUsageInfoANDROID* grallocUsageInfo, 226 uint64_t* grallocUsage 227 ); 228 229 VKAPI_ATTR VkResult VKAPI_CALL vkAcquireImageANDROID( 230 VkDevice device, 231 VkImage image, 232 int nativeFenceFd, 233 VkSemaphore semaphore, 234 VkFence fence 235 ); 236 237 VKAPI_ATTR VkResult VKAPI_CALL vkQueueSignalReleaseImageANDROID( 238 VkQueue queue, 239 uint32_t waitSemaphoreCount, 240 const VkSemaphore* pWaitSemaphores, 241 VkImage image, 242 int* pNativeFenceFd 243 ); 244 245 #endif 246 /* clang-format on */ 247 248 #ifdef __cplusplus 249 } 250 #endif 251 252 #endif /* __VK_ANDROID_NATIVE_BUFFER_H__ */ 253