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 #if defined(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 + (1000 * (VK_ANDROID_NATIVE_BUFFER_EXTENSION_NUMBER - 1)) + (id)))
74 #define VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID VK_ANDROID_NATIVE_BUFFER_ENUM(VkStructureType, 0)
75 #define VK_STRUCTURE_TYPE_SWAPCHAIN_IMAGE_CREATE_INFO_ANDROID \
76     VK_ANDROID_NATIVE_BUFFER_ENUM(VkStructureType, 1)
77 #define VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENTATION_PROPERTIES_ANDROID \
78     VK_ANDROID_NATIVE_BUFFER_ENUM(VkStructureType, 2)
79 #define VK_STRUCTURE_TYPE_GRALLOC_USAGE_INFO_ANDROID \
80     VK_ANDROID_NATIVE_BUFFER_ENUM(VkStructureType, 3)
81 
82 /* clang-format off */
83 typedef enum VkSwapchainImageUsageFlagBitsANDROID {
84     VK_SWAPCHAIN_IMAGE_USAGE_SHARED_BIT_ANDROID = 0x00000001,
85     VK_SWAPCHAIN_IMAGE_USAGE_FLAG_BITS_MAX_ENUM = 0x7FFFFFFF
86 } VkSwapchainImageUsageFlagBitsANDROID;
87 typedef VkFlags VkSwapchainImageUsageFlagsANDROID;
88 
89 /*
90  * struct VkNativeBufferUsage2ANDROID
91  *
92  * consumer: gralloc1 consumer usage flag
93  * producer: gralloc1 producer usage flag
94  */
95 typedef struct {
96     uint64_t                          consumer;
97     uint64_t                          producer;
98 } VkNativeBufferUsage2ANDROID;
99 
100 /*
101  * struct VkNativeBufferANDROID
102  *
103  * sType: VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID
104  * pNext: NULL or a pointer to a structure extending this structure
105  * handle: buffer handle returned from gralloc alloc()
106  * stride: stride returned from gralloc alloc()
107  * format: gralloc format requested when the buffer was allocated
108  * usage: gralloc usage requested when the buffer was allocated
109  * usage2: gralloc usage requested when the buffer was allocated
110  * usage3: gralloc usage requested when the buffer was allocated
111  */
112 typedef struct {
113     VkStructureType                   sType;
114     const void*                       pNext;
115     const uint32_t*                   handle;
116     int                               stride;
117     int                               format;
118     int                               usage; /* DEPRECATED in SPEC_VERSION 6 */
119     VkNativeBufferUsage2ANDROID       usage2; /* DEPRECATED in SPEC_VERSION 9 */
120     uint64_t                          usage3; /* ADDED in SPEC_VERSION 9 */
121 } VkNativeBufferANDROID;
122 
123 /*
124  * struct VkSwapchainImageCreateInfoANDROID
125  *
126  * sType: VK_STRUCTURE_TYPE_SWAPCHAIN_IMAGE_CREATE_INFO_ANDROID
127  * pNext: NULL or a pointer to a structure extending this structure
128  * usage: is a bitmask of VkSwapchainImageUsageFlagsANDROID
129  */
130 typedef struct {
131     VkStructureType                   sType;
132     const void*                       pNext;
133     VkSwapchainImageUsageFlagsANDROID usage;
134 } VkSwapchainImageCreateInfoANDROID;
135 
136 /*
137  * struct VkPhysicalDevicePresentationPropertiesANDROID
138  *
139  * sType: VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENTATION_PROPERTIES_ANDROID
140  * pNext: NULL or a pointer to a structure extending this structure
141  * sharedImage: specifies if the image can be shared with the display system
142  */
143 typedef struct {
144     VkStructureType                   sType;
145     const void*                       pNext;
146     VkBool32                          sharedImage;
147 } VkPhysicalDevicePresentationPropertiesANDROID;
148 
149 /*
150  * struct VkGrallocUsageInfoANDROID
151  *
152  * sType: VK_STRUCTURE_TYPE_GRALLOC_USAGE_INFO_ANDROID
153  * pNext: NULL or a pointer to a structure extending this structure
154  * format: value specifying the format the image will be created with
155  * imageUsage: bitmask of VkImageUsageFlagBits describing intended usage
156  */
157 typedef struct {
158     VkStructureType                   sType;
159     const void*                       pNext;
160     VkFormat                          format;
161     VkImageUsageFlags                 imageUsage;
162 } VkGrallocUsageInfoANDROID;
163 
164 /* DEPRECATED in SPEC_VERSION 6 */
165 typedef VkResult (VKAPI_PTR *PFN_vkGetSwapchainGrallocUsageANDROID)(
166     VkDevice                          device,
167     VkFormat                          format,
168     VkImageUsageFlags                 imageUsage,
169     int*                              grallocUsage);
170 
171 /* DEPRECATED in SPEC_VERSION 9 */
172 typedef VkResult (VKAPI_PTR *PFN_vkGetSwapchainGrallocUsage2ANDROID)(
173     VkDevice                          device,
174     VkFormat                          format,
175     VkImageUsageFlags                 imageUsage,
176     VkSwapchainImageUsageFlagsANDROID swapchainImageUsage,
177     uint64_t*                         grallocConsumerUsage,
178     uint64_t*                         grallocProducerUsage);
179 
180 /* ADDED in SPEC_VERSION 9 */
181 typedef VkResult (VKAPI_PTR *PFN_vkGetSwapchainGrallocUsage3ANDROID)(
182     VkDevice                          device,
183     const VkGrallocUsageInfoANDROID*  grallocUsageInfo,
184     uint64_t*                         grallocUsage);
185 
186 typedef VkResult (VKAPI_PTR *PFN_vkAcquireImageANDROID)(
187     VkDevice                          device,
188     VkImage                           image,
189     int                               nativeFenceFd,
190     VkSemaphore                       semaphore,
191     VkFence                           fence);
192 
193 typedef VkResult (VKAPI_PTR *PFN_vkQueueSignalReleaseImageANDROID)(
194     VkQueue                           queue,
195     uint32_t                          waitSemaphoreCount,
196     const VkSemaphore*                pWaitSemaphores,
197     VkImage                           image,
198     int*                              pNativeFenceFd);
199 
200 #ifndef VK_NO_PROTOTYPES
201 
202 /* DEPRECATED in SPEC_VERSION 6 */
203 VKAPI_ATTR VkResult VKAPI_CALL vkGetSwapchainGrallocUsageANDROID(
204     VkDevice                          device,
205     VkFormat                          format,
206     VkImageUsageFlags                 imageUsage,
207     int*                              grallocUsage
208 );
209 
210 /* DEPRECATED in SPEC_VERSION 9 */
211 VKAPI_ATTR VkResult VKAPI_CALL vkGetSwapchainGrallocUsage2ANDROID(
212     VkDevice                          device,
213     VkFormat                          format,
214     VkImageUsageFlags                 imageUsage,
215     VkSwapchainImageUsageFlagsANDROID swapchainImageUsage,
216     uint64_t*                         grallocConsumerUsage,
217     uint64_t*                         grallocProducerUsage
218 );
219 
220 /* ADDED in SPEC_VERSION 9 */
221 VKAPI_ATTR VkResult VKAPI_CALL vkGetSwapchainGrallocUsage3ANDROID(
222     VkDevice                          device,
223     const VkGrallocUsageInfoANDROID*  grallocUsageInfo,
224     uint64_t*                         grallocUsage
225 );
226 
227 VKAPI_ATTR VkResult VKAPI_CALL vkAcquireImageANDROID(
228     VkDevice                          device,
229     VkImage                           image,
230     int                               nativeFenceFd,
231     VkSemaphore                       semaphore,
232     VkFence                           fence
233 );
234 
235 VKAPI_ATTR VkResult VKAPI_CALL vkQueueSignalReleaseImageANDROID(
236     VkQueue                           queue,
237     uint32_t                          waitSemaphoreCount,
238     const VkSemaphore*                pWaitSemaphores,
239     VkImage                           image,
240     int*                              pNativeFenceFd
241 );
242 
243 #endif
244 /* clang-format on */
245 
246 #ifdef __cplusplus
247 }
248 #endif
249 
250 #endif /* __VK_ANDROID_NATIVE_BUFFER_H__ */
251