• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <cutils/native_handle.h>
21 #include <vulkan/vulkan.h>
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 #define VK_ANDROID_native_buffer 1
28 
29 #define VK_ANDROID_NATIVE_BUFFER_EXTENSION_NUMBER 11
30 /*
31  * NOTE ON VK_ANDROID_NATIVE_BUFFER_SPEC_VERSION 6
32  *
33  * This version of the extension transitions from gralloc0 to gralloc1 usage
34  * flags (int -> 2x uint64_t). The WSI implementation will temporarily continue
35  * to fill out deprecated fields in VkNativeBufferANDROID, and will call the
36  * deprecated vkGetSwapchainGrallocUsageANDROID if the new
37  * vkGetSwapchainGrallocUsage2ANDROID is not supported. This transitionary
38  * backwards-compatibility support is temporary, and will likely be removed
39  * (along with all gralloc0 support) in a future release.
40  */
41 /*
42  * NOTE ON VK_ANDROID_NATIVE_BUFFER_SPEC_VERSION 8
43  *
44  * This version of the extension doesn't introduce new types or structs, but is
45  * to accommodate the new struct VkBindImageMemorySwapchainInfoKHR added in
46  * VK_KHR_swapchain spec version 69. When VkBindImageMemorySwapchainInfoKHR is
47  * chained in the pNext chain of VkBindImageMemoryInfo, a VkNativeBufferANDROID
48  * that holds the correct gralloc handle according to the imageIndex specified
49  * in VkBindImageMemorySwapchainInfoKHR will be additionally chained to the
50  * pNext chain of VkBindImageMemoryInfo and passed down to the driver.
51  */
52 /*
53  * NOTE ON VK_ANDROID_NATIVE_BUFFER_SPEC_VERSION 9
54  *
55  * This version of the extension is largely designed to clean up the mix of
56  * GrallocUsage and GrallocUsage2
57  */
58 /*
59  * NOTE ON VK_ANDROID_NATIVE_BUFFER_SPEC_VERSION 10
60  *
61  * This version of the extension cleans up a bug introduced in version 9
62  */
63 /*
64  * NOTE ON VK_ANDROID_NATIVE_BUFFER_SPEC_VERSION 11
65  *
66  * This version of the extension deprecates the last of grallocusage and
67  * extends VkNativeBufferANDROID to support passing AHardwareBuffer*
68  */
69 #define VK_ANDROID_NATIVE_BUFFER_SPEC_VERSION 11
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 #define VK_STRUCTURE_TYPE_GRALLOC_USAGE_INFO_2_ANDROID \
84     VK_ANDROID_NATIVE_BUFFER_ENUM(VkStructureType, 4)
85 
86 /* clang-format off */
87 typedef enum VkSwapchainImageUsageFlagBitsANDROID {
88     VK_SWAPCHAIN_IMAGE_USAGE_SHARED_BIT_ANDROID = 0x00000001,
89     VK_SWAPCHAIN_IMAGE_USAGE_FLAG_BITS_MAX_ENUM = 0x7FFFFFFF
90 } VkSwapchainImageUsageFlagBitsANDROID;
91 typedef VkFlags VkSwapchainImageUsageFlagsANDROID;
92 
93 /*
94  * struct VkNativeBufferUsage2ANDROID
95  *
96  * consumer: gralloc1 consumer usage flag
97  * producer: gralloc1 producer usage flag
98  */
99 typedef struct {
100     uint64_t                          consumer;
101     uint64_t                          producer;
102 } VkNativeBufferUsage2ANDROID;
103 
104 /*
105  * struct VkNativeBufferANDROID
106  *
107  * sType: VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID
108  * pNext: NULL or a pointer to a structure extending this structure
109  * handle: buffer handle returned from gralloc alloc()
110  * stride: stride returned from gralloc alloc()
111  * format: gralloc format requested when the buffer was allocated
112  * usage: gralloc usage requested when the buffer was allocated
113  * usage2: gralloc usage requested when the buffer was allocated
114  * usage3: gralloc usage requested when the buffer was allocated
115  * ahb: The AHardwareBuffer* from the actual ANativeWindowBuffer. Caller
116  *      maintains ownership of resource. AHardwareBuffer pointer is only valid
117  *      for the duration of the function call
118  */
119 typedef struct {
120     VkStructureType                   sType;
121     const void*                       pNext;
122     buffer_handle_t                   handle;
123     int                               stride;
124     int                               format;
125     int                               usage; /* DEPRECATED in SPEC_VERSION 6 */
126     VkNativeBufferUsage2ANDROID       usage2; /* DEPRECATED in SPEC_VERSION 9 */
127     uint64_t                          usage3; /* ADDED in SPEC_VERSION 9 */
128     struct AHardwareBuffer*           ahb; /* ADDED in SPEC_VERSION 11 */
129 } VkNativeBufferANDROID;
130 
131 /*
132  * struct VkSwapchainImageCreateInfoANDROID
133  *
134  * sType: VK_STRUCTURE_TYPE_SWAPCHAIN_IMAGE_CREATE_INFO_ANDROID
135  * pNext: NULL or a pointer to a structure extending this structure
136  * usage: is a bitmask of VkSwapchainImageUsageFlagsANDROID
137  */
138 typedef struct {
139     VkStructureType                   sType;
140     const void*                       pNext;
141     VkSwapchainImageUsageFlagsANDROID usage;
142 } VkSwapchainImageCreateInfoANDROID;
143 
144 /*
145  * struct VkPhysicalDevicePresentationPropertiesANDROID
146  *
147  * sType: VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENTATION_PROPERTIES_ANDROID
148  * pNext: NULL or a pointer to a structure extending this structure
149  * sharedImage: specifies if the image can be shared with the display system
150  */
151 typedef struct {
152     VkStructureType                   sType;
153     const void*                       pNext;
154     VkBool32                          sharedImage;
155 } VkPhysicalDevicePresentationPropertiesANDROID;
156 
157 /*
158  * struct VkGrallocUsageInfoANDROID
159  *
160  * sType: VK_STRUCTURE_TYPE_GRALLOC_USAGE_INFO_ANDROID
161  * pNext: NULL or a pointer to a structure extending this structure
162  * format: value specifying the format the image will be created with
163  * imageUsage: bitmask of VkImageUsageFlagBits describing intended usage
164  *
165  * DEPRECATED in SPEC_VERSION 10
166  */
167 typedef struct {
168     VkStructureType                   sType;
169     const void*                       pNext;
170     VkFormat                          format;
171     VkImageUsageFlags                 imageUsage;
172 } VkGrallocUsageInfoANDROID;
173 
174 /*
175  * struct VkGrallocUsageInfo2ANDROID
176  *
177  * sType: VK_STRUCTURE_TYPE_GRALLOC_USAGE_INFO_2_ANDROID
178  * pNext: NULL or a pointer to a structure extending this structure
179  * format: value specifying the format the image will be created with
180  * imageUsage: bitmask of VkImageUsageFlagBits describing intended usage
181  * swapchainImageUsage: is a bitmask of VkSwapchainImageUsageFlagsANDROID
182  *
183  * DEPRECATED in SPEC_VERSION 11
184  */
185 typedef struct {
186     VkStructureType                   sType;
187     const void*                       pNext;
188     VkFormat                          format;
189     VkImageUsageFlags                 imageUsage;
190     VkSwapchainImageUsageFlagsANDROID swapchainImageUsage;
191 } VkGrallocUsageInfo2ANDROID;
192 
193 /* DEPRECATED in SPEC_VERSION 6 */
194 typedef VkResult (VKAPI_PTR *PFN_vkGetSwapchainGrallocUsageANDROID)(
195     VkDevice                          device,
196     VkFormat                          format,
197     VkImageUsageFlags                 imageUsage,
198     int*                              grallocUsage);
199 
200 /* DEPRECATED in SPEC_VERSION 9 */
201 typedef VkResult (VKAPI_PTR *PFN_vkGetSwapchainGrallocUsage2ANDROID)(
202     VkDevice                          device,
203     VkFormat                          format,
204     VkImageUsageFlags                 imageUsage,
205     VkSwapchainImageUsageFlagsANDROID swapchainImageUsage,
206     uint64_t*                         grallocConsumerUsage,
207     uint64_t*                         grallocProducerUsage);
208 
209 /* DEPRECATED in SPEC_VERSION 10 */
210 typedef VkResult (VKAPI_PTR *PFN_vkGetSwapchainGrallocUsage3ANDROID)(
211     VkDevice                          device,
212     const VkGrallocUsageInfoANDROID*  grallocUsageInfo,
213     uint64_t*                         grallocUsage);
214 
215 /* DEPRECATED in SPEC_VERSION 11 */
216 typedef VkResult (VKAPI_PTR *PFN_vkGetSwapchainGrallocUsage4ANDROID)(
217     VkDevice                          device,
218     const VkGrallocUsageInfo2ANDROID* grallocUsageInfo,
219     uint64_t*                         grallocUsage);
220 
221 typedef VkResult (VKAPI_PTR *PFN_vkAcquireImageANDROID)(
222     VkDevice                          device,
223     VkImage                           image,
224     int                               nativeFenceFd,
225     VkSemaphore                       semaphore,
226     VkFence                           fence);
227 
228 typedef VkResult (VKAPI_PTR *PFN_vkQueueSignalReleaseImageANDROID)(
229     VkQueue                           queue,
230     uint32_t                          waitSemaphoreCount,
231     const VkSemaphore*                pWaitSemaphores,
232     VkImage                           image,
233     int*                              pNativeFenceFd);
234 
235 #ifndef VK_NO_PROTOTYPES
236 
237 /* DEPRECATED in SPEC_VERSION 6 */
238 VKAPI_ATTR VkResult VKAPI_CALL vkGetSwapchainGrallocUsageANDROID(
239     VkDevice                          device,
240     VkFormat                          format,
241     VkImageUsageFlags                 imageUsage,
242     int*                              grallocUsage
243 );
244 
245 /* DEPRECATED in SPEC_VERSION 9 */
246 VKAPI_ATTR VkResult VKAPI_CALL vkGetSwapchainGrallocUsage2ANDROID(
247     VkDevice                          device,
248     VkFormat                          format,
249     VkImageUsageFlags                 imageUsage,
250     VkSwapchainImageUsageFlagsANDROID swapchainImageUsage,
251     uint64_t*                         grallocConsumerUsage,
252     uint64_t*                         grallocProducerUsage
253 );
254 
255 /* DEPRECATED in SPEC_VERSION 10 */
256 VKAPI_ATTR VkResult VKAPI_CALL vkGetSwapchainGrallocUsage3ANDROID(
257     VkDevice                          device,
258     const VkGrallocUsageInfoANDROID*  grallocUsageInfo,
259     uint64_t*                         grallocUsage
260 );
261 
262 /* DEPRECATED in SPEC_VERSION 11 */
263 VKAPI_ATTR VkResult VKAPI_CALL vkGetSwapchainGrallocUsage4ANDROID(
264     VkDevice                          device,
265     const VkGrallocUsageInfo2ANDROID* grallocUsageInfo,
266     uint64_t*                         grallocUsage
267 );
268 
269 VKAPI_ATTR VkResult VKAPI_CALL vkAcquireImageANDROID(
270     VkDevice                          device,
271     VkImage                           image,
272     int                               nativeFenceFd,
273     VkSemaphore                       semaphore,
274     VkFence                           fence
275 );
276 
277 VKAPI_ATTR VkResult VKAPI_CALL vkQueueSignalReleaseImageANDROID(
278     VkQueue                           queue,
279     uint32_t                          waitSemaphoreCount,
280     const VkSemaphore*                pWaitSemaphores,
281     VkImage                           image,
282     int*                              pNativeFenceFd
283 );
284 
285 #endif
286 /* clang-format on */
287 
288 #ifdef __cplusplus
289 }
290 #endif
291 
292 #endif /* __VK_ANDROID_NATIVE_BUFFER_H__ */
293