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 /* NOTE ON VK_ANDROID_NATIVE_BUFFER_SPEC_VERSION 6
31  *
32  * This version of the extension transitions from gralloc0 to gralloc1 usage
33  * flags (int -> 2x uint64_t). The WSI implementation will temporarily continue
34  * to fill out deprecated fields in VkNativeBufferANDROID, and will call the
35  * deprecated vkGetSwapchainGrallocUsageANDROID if the new
36  * vkGetSwapchainGrallocUsage2ANDROID is not supported. This transitionary
37  * backwards-compatibility support is temporary, and will likely be removed in
38  * (along with all gralloc0 support) in a future release.
39  */
40 /* NOTE ON VK_ANDROID_NATIVE_BUFFER_SPEC_VERSION 8
41  *
42  * This version of the extension doesn't introduce new types or structs, but is
43  * to accommodate the new struct VkBindImageMemorySwapchainInfoKHR added in
44  * VK_KHR_swapchain spec version 69. When VkBindImageMemorySwapchainInfoKHR is
45  * chained in the pNext chain of VkBindImageMemoryInfo, a VkNativeBufferANDROID
46  * that holds the correct gralloc handle according to the imageIndex specified
47  * in VkBindImageMemorySwapchainInfoKHR will be additionally chained to the
48  * pNext chain of VkBindImageMemoryInfo and passed down to the driver.
49  */
50 #define VK_ANDROID_NATIVE_BUFFER_SPEC_VERSION     8
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 
64 typedef struct {
65     uint64_t consumer;
66     uint64_t producer;
67 } VkNativeBufferUsage2ANDROID;
68 
69 typedef struct {
70     VkStructureType             sType; // must be VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID
71     const void*                 pNext;
72 
73     // Buffer handle and stride returned from gralloc alloc()
74     buffer_handle_t             handle;
75     int                         stride;
76 
77     // Gralloc format and usage requested when the buffer was allocated.
78     int                         format;
79     int                         usage; // DEPRECATED in SPEC_VERSION 6
80     // -- Added in SPEC_VERSION 6 --
81     VkNativeBufferUsage2ANDROID usage2;
82 } VkNativeBufferANDROID;
83 
84 typedef struct {
85     VkStructureType                        sType; // must be VK_STRUCTURE_TYPE_SWAPCHAIN_IMAGE_CREATE_INFO_ANDROID
86     const void*                            pNext;
87 
88     VkSwapchainImageUsageFlagsANDROID      usage;
89 } VkSwapchainImageCreateInfoANDROID;
90 
91 typedef struct {
92     VkStructureType                        sType; // must be VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENTATION_PROPERTIES_ANDROID
93     const void*                            pNext;
94 
95     VkBool32                               sharedImage;
96 } VkPhysicalDevicePresentationPropertiesANDROID;
97 
98 // -- DEPRECATED in SPEC_VERSION 6 --
99 typedef VkResult (VKAPI_PTR *PFN_vkGetSwapchainGrallocUsageANDROID)(VkDevice device, VkFormat format, VkImageUsageFlags imageUsage, int* grallocUsage);
100 // -- ADDED in SPEC_VERSION 6 --
101 typedef VkResult (VKAPI_PTR *PFN_vkGetSwapchainGrallocUsage2ANDROID)(VkDevice device, VkFormat format, VkImageUsageFlags imageUsage, VkSwapchainImageUsageFlagsANDROID swapchainImageUsage, uint64_t* grallocConsumerUsage, uint64_t* grallocProducerUsage);
102 typedef VkResult (VKAPI_PTR *PFN_vkAcquireImageANDROID)(VkDevice device, VkImage image, int nativeFenceFd, VkSemaphore semaphore, VkFence fence);
103 typedef VkResult (VKAPI_PTR *PFN_vkQueueSignalReleaseImageANDROID)(VkQueue queue, uint32_t waitSemaphoreCount, const VkSemaphore* pWaitSemaphores, VkImage image, int* pNativeFenceFd);
104 
105 #ifndef VK_NO_PROTOTYPES
106 
107 // -- DEPRECATED in SPEC_VERSION 6 --
108 VKAPI_ATTR VkResult VKAPI_CALL vkGetSwapchainGrallocUsageANDROID(
109     VkDevice            device,
110     VkFormat            format,
111     VkImageUsageFlags   imageUsage,
112     int*                grallocUsage
113 );
114 // -- ADDED in SPEC_VERSION 6 --
115 VKAPI_ATTR VkResult VKAPI_CALL vkGetSwapchainGrallocUsage2ANDROID(
116     VkDevice            device,
117     VkFormat            format,
118     VkImageUsageFlags   imageUsage,
119     VkSwapchainImageUsageFlagsANDROID swapchainImageUsage,
120     uint64_t*           grallocConsumerUsage,
121     uint64_t*           grallocProducerUsage
122 );
123 VKAPI_ATTR VkResult VKAPI_CALL vkAcquireImageANDROID(
124     VkDevice            device,
125     VkImage             image,
126     int                 nativeFenceFd,
127     VkSemaphore         semaphore,
128     VkFence             fence
129 );
130 VKAPI_ATTR VkResult VKAPI_CALL vkQueueSignalReleaseImageANDROID(
131     VkQueue             queue,
132     uint32_t            waitSemaphoreCount,
133     const VkSemaphore*  pWaitSemaphores,
134     VkImage             image,
135     int*                pNativeFenceFd
136 );
137 #endif
138 
139 #ifdef __cplusplus
140 }
141 #endif
142 
143 #endif // __VK_ANDROID_NATIVE_BUFFER_H__
144