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