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 #ifndef __VK_ANDROID_NATIVE_BUFFER_H__
17 #define __VK_ANDROID_NATIVE_BUFFER_H__
18 
19 /* MESA: A hack to avoid #ifdefs in driver code. */
20 #ifdef ANDROID
21 #include <cutils/native_handle.h>
22 #include <vulkan/vulkan.h>
23 
24 #if ANDROID_API_LEVEL < 28
25 /* buffer_handle_t was defined in the deprecated system/window.h */
26 typedef const native_handle_t *buffer_handle_t;
27 #endif
28 
29 #else
30 typedef void *buffer_handle_t;
31 #endif
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
37 #define VK_ANDROID_native_buffer 1
38 #define VK_ANDROID_NATIVE_BUFFER_EXTENSION_NUMBER 11
39 
40 /* NOTE ON VK_ANDROID_NATIVE_BUFFER_SPEC_VERSION 6
41  *
42  * This version of the extension transitions from gralloc0 to gralloc1 usage
43  * flags (int -> 2x uint64_t). The WSI implementation will temporarily continue
44  * to fill out deprecated fields in VkNativeBufferANDROID, and will call the
45  * deprecated vkGetSwapchainGrallocUsageANDROID if the new
46  * vkGetSwapchainGrallocUsage2ANDROID is not supported. This transitionary
47  * backwards-compatibility support is temporary, and will likely be removed in
48  * (along with all gralloc0 support) in a future release.
49  */
50 #define VK_ANDROID_NATIVE_BUFFER_SPEC_VERSION     7
51 #define VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME   "VK_ANDROID_native_buffer"
52 
53 #define VK_ANDROID_NATIVE_BUFFER_ENUM(type,id)    ((type)(1000000000 + (1000 * (VK_ANDROID_NATIVE_BUFFER_EXTENSION_NUMBER - 1)) + (id)))
54 #define VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID   VK_ANDROID_NATIVE_BUFFER_ENUM(VkStructureType, 0)
55 #define VK_STRUCTURE_TYPE_SWAPCHAIN_IMAGE_CREATE_INFO_ANDROID VK_ANDROID_NATIVE_BUFFER_ENUM(VkStructureType, 1)
56 #define VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENTATION_PROPERTIES_ANDROID VK_ANDROID_NATIVE_BUFFER_ENUM(VkStructureType, 2)
57 
58 typedef enum VkSwapchainImageUsageFlagBitsANDROID {
59     VK_SWAPCHAIN_IMAGE_USAGE_SHARED_BIT_ANDROID = 0x00000001,
60     VK_SWAPCHAIN_IMAGE_USAGE_FLAG_BITS_MAX_ENUM = 0x7FFFFFFF
61 } VkSwapchainImageUsageFlagBitsANDROID;
62 typedef VkFlags VkSwapchainImageUsageFlagsANDROID;
63 typedef struct {
64     VkStructureType             sType; // must be VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID
65     const void*                 pNext;
66 
67     // Buffer handle and stride returned from gralloc alloc()
68     buffer_handle_t             handle;
69     int                         stride;
70 
71     // Gralloc format and usage requested when the buffer was allocated.
72     int                         format;
73     int                         usage; // DEPRECATED in SPEC_VERSION 6
74 
75     // -- Added in SPEC_VERSION 6 --
76     struct {
77         uint64_t                consumer;
78         uint64_t                producer;
79     } usage2;
80 } VkNativeBufferANDROID;
81 
82 typedef struct {
83     VkStructureType                        sType; // must be VK_STRUCTURE_TYPE_SWAPCHAIN_IMAGE_CREATE_INFO_ANDROID
84     const void*                            pNext;
85 
86     VkSwapchainImageUsageFlagsANDROID      usage;
87 } VkSwapchainImageCreateInfoANDROID;
88 
89 typedef struct {
90     VkStructureType                        sType; // must be VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENTATION_PROPERTIES_ANDROID
91     const void*                            pNext;
92 
93     VkBool32                               sharedImage;
94 } VkPhysicalDevicePresentationPropertiesANDROID;
95 
96 // -- DEPRECATED in SPEC_VERSION 6 --
97 typedef VkResult (VKAPI_PTR *PFN_vkGetSwapchainGrallocUsageANDROID)(VkDevice device, VkFormat format, VkImageUsageFlags imageUsage, int* grallocUsage);
98 // -- ADDED in SPEC_VERSION 6 --
99 typedef VkResult (VKAPI_PTR *PFN_vkGetSwapchainGrallocUsage2ANDROID)(VkDevice device, VkFormat format, VkImageUsageFlags imageUsage, VkSwapchainImageUsageFlagsANDROID swapchainImageUsage, uint64_t* grallocConsumerUsage, uint64_t* grallocProducerUsage);
100 typedef VkResult (VKAPI_PTR *PFN_vkAcquireImageANDROID)(VkDevice device, VkImage image, int nativeFenceFd, VkSemaphore semaphore, VkFence fence);
101 typedef VkResult (VKAPI_PTR *PFN_vkQueueSignalReleaseImageANDROID)(VkQueue queue, uint32_t waitSemaphoreCount, const VkSemaphore* pWaitSemaphores, VkImage image, int* pNativeFenceFd);
102 
103 #ifndef VK_NO_PROTOTYPES
104 // -- DEPRECATED in SPEC_VERSION 6 --
105 VKAPI_ATTR VkResult VKAPI_CALL vkGetSwapchainGrallocUsageANDROID(
106     VkDevice            device,
107     VkFormat            format,
108     VkImageUsageFlags   imageUsage,
109     int*                grallocUsage
110 );
111 // -- ADDED in SPEC_VERSION 6 --
112 VKAPI_ATTR VkResult VKAPI_CALL vkGetSwapchainGrallocUsage2ANDROID(
113     VkDevice            device,
114     VkFormat            format,
115     VkImageUsageFlags   imageUsage,
116     VkSwapchainImageUsageFlagsANDROID swapchainImageUsage,
117     uint64_t*           grallocConsumerUsage,
118     uint64_t*           grallocProducerUsage
119 );
120 VKAPI_ATTR VkResult VKAPI_CALL vkAcquireImageANDROID(
121     VkDevice            device,
122     VkImage             image,
123     int                 nativeFenceFd,
124     VkSemaphore         semaphore,
125     VkFence             fence
126 );
127 VKAPI_ATTR VkResult VKAPI_CALL vkQueueSignalReleaseImageANDROID(
128     VkQueue             queue,
129     uint32_t            waitSemaphoreCount,
130     const VkSemaphore*  pWaitSemaphores,
131     VkImage             image,
132     int*                pNativeFenceFd
133 );
134 #endif
135 
136 #ifdef __cplusplus
137 }
138 #endif
139 
140 #endif // __VK_ANDROID_NATIVE_BUFFER_H__
141