1 // Copyright (C) 2018 The Android Open Source Project 2 // Copyright (C) 2018 Google Inc. 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 #pragma once 16 17 #include "android/base/Tracing.h" 18 19 #include <vulkan/vulkan.h> 20 21 #include "VulkanHandleMapping.h" 22 #include "VulkanHandles.h" 23 #include <memory> 24 25 #include "goldfish_vk_transform_guest.h" 26 27 struct EmulatorFeatureInfo; 28 29 namespace goldfish_vk { 30 31 class ResourceTracker { 32 public: 33 ResourceTracker(); 34 virtual ~ResourceTracker(); 35 static ResourceTracker* get(); 36 VulkanHandleMapping* createMapping(); 37 VulkanHandleMapping* unwrapMapping(); 38 VulkanHandleMapping* destroyMapping(); 39 VulkanHandleMapping* defaultMapping(); 40 41 #define HANDLE_REGISTER_DECL(type) \ 42 void register_##type(type); \ 43 void unregister_##type(type); \ 44 45 GOLDFISH_VK_LIST_HANDLE_TYPES(HANDLE_REGISTER_DECL) 46 47 VkResult on_vkEnumerateInstanceExtensionProperties( 48 void* context, 49 VkResult input_result, 50 const char* pLayerName, 51 uint32_t* pPropertyCount, 52 VkExtensionProperties* pProperties); 53 54 VkResult on_vkEnumerateDeviceExtensionProperties( 55 void* context, 56 VkResult input_result, 57 VkPhysicalDevice physicalDevice, 58 const char* pLayerName, 59 uint32_t* pPropertyCount, 60 VkExtensionProperties* pProperties); 61 62 VkResult on_vkEnumeratePhysicalDevices( 63 void* context, VkResult input_result, 64 VkInstance instance, uint32_t* pPhysicalDeviceCount, 65 VkPhysicalDevice* pPhysicalDevices); 66 67 void on_vkGetPhysicalDeviceMemoryProperties( 68 void* context, 69 VkPhysicalDevice physicalDevice, 70 VkPhysicalDeviceMemoryProperties* pMemoryProperties); 71 void on_vkGetPhysicalDeviceMemoryProperties2( 72 void* context, 73 VkPhysicalDevice physicalDevice, 74 VkPhysicalDeviceMemoryProperties2* pMemoryProperties); 75 void on_vkGetPhysicalDeviceMemoryProperties2KHR( 76 void* context, 77 VkPhysicalDevice physicalDevice, 78 VkPhysicalDeviceMemoryProperties2* pMemoryProperties); 79 80 VkResult on_vkCreateInstance( 81 void* context, 82 VkResult input_result, 83 const VkInstanceCreateInfo* createInfo, 84 const VkAllocationCallbacks* pAllocator, 85 VkInstance* pInstance); 86 VkResult on_vkCreateDevice( 87 void* context, 88 VkResult input_result, 89 VkPhysicalDevice physicalDevice, 90 const VkDeviceCreateInfo* pCreateInfo, 91 const VkAllocationCallbacks* pAllocator, 92 VkDevice* pDevice); 93 void on_vkDestroyDevice_pre( 94 void* context, 95 VkDevice device, 96 const VkAllocationCallbacks* pAllocator); 97 98 VkResult on_vkAllocateMemory( 99 void* context, 100 VkResult input_result, 101 VkDevice device, 102 const VkMemoryAllocateInfo* pAllocateInfo, 103 const VkAllocationCallbacks* pAllocator, 104 VkDeviceMemory* pMemory); 105 void on_vkFreeMemory( 106 void* context, 107 VkDevice device, 108 VkDeviceMemory memory, 109 const VkAllocationCallbacks* pAllocator); 110 111 VkResult on_vkMapMemory( 112 void* context, 113 VkResult input_result, 114 VkDevice device, 115 VkDeviceMemory memory, 116 VkDeviceSize offset, 117 VkDeviceSize size, 118 VkMemoryMapFlags, 119 void** ppData); 120 121 void on_vkUnmapMemory( 122 void* context, 123 VkDevice device, 124 VkDeviceMemory memory); 125 126 VkResult on_vkCreateImage( 127 void* context, VkResult input_result, 128 VkDevice device, const VkImageCreateInfo *pCreateInfo, 129 const VkAllocationCallbacks *pAllocator, 130 VkImage *pImage); 131 void on_vkDestroyImage( 132 void* context, 133 VkDevice device, VkImage image, const VkAllocationCallbacks *pAllocator); 134 135 void on_vkGetImageMemoryRequirements( 136 void *context, VkDevice device, VkImage image, 137 VkMemoryRequirements *pMemoryRequirements); 138 void on_vkGetImageMemoryRequirements2( 139 void *context, VkDevice device, const VkImageMemoryRequirementsInfo2 *pInfo, 140 VkMemoryRequirements2 *pMemoryRequirements); 141 void on_vkGetImageMemoryRequirements2KHR( 142 void *context, VkDevice device, const VkImageMemoryRequirementsInfo2 *pInfo, 143 VkMemoryRequirements2 *pMemoryRequirements); 144 145 VkResult on_vkBindImageMemory( 146 void* context, VkResult input_result, 147 VkDevice device, VkImage image, VkDeviceMemory memory, 148 VkDeviceSize memoryOffset); 149 VkResult on_vkBindImageMemory2( 150 void* context, VkResult input_result, 151 VkDevice device, uint32_t bindingCount, const VkBindImageMemoryInfo* pBindInfos); 152 VkResult on_vkBindImageMemory2KHR( 153 void* context, VkResult input_result, 154 VkDevice device, uint32_t bindingCount, const VkBindImageMemoryInfo* pBindInfos); 155 156 VkResult on_vkCreateBuffer( 157 void* context, VkResult input_result, 158 VkDevice device, const VkBufferCreateInfo *pCreateInfo, 159 const VkAllocationCallbacks *pAllocator, 160 VkBuffer *pBuffer); 161 void on_vkDestroyBuffer( 162 void* context, 163 VkDevice device, VkBuffer buffer, const VkAllocationCallbacks *pAllocator); 164 165 void on_vkGetBufferMemoryRequirements( 166 void* context, VkDevice device, VkBuffer buffer, VkMemoryRequirements *pMemoryRequirements); 167 void on_vkGetBufferMemoryRequirements2( 168 void* context, VkDevice device, const VkBufferMemoryRequirementsInfo2* pInfo, 169 VkMemoryRequirements2* pMemoryRequirements); 170 void on_vkGetBufferMemoryRequirements2KHR( 171 void* context, VkDevice device, const VkBufferMemoryRequirementsInfo2* pInfo, 172 VkMemoryRequirements2* pMemoryRequirements); 173 174 VkResult on_vkBindBufferMemory( 175 void* context, VkResult input_result, 176 VkDevice device, VkBuffer buffer, VkDeviceMemory memory, VkDeviceSize memoryOffset); 177 VkResult on_vkBindBufferMemory2( 178 void* context, VkResult input_result, 179 VkDevice device, uint32_t bindInfoCount, const VkBindBufferMemoryInfo *pBindInfos); 180 VkResult on_vkBindBufferMemory2KHR( 181 void* context, VkResult input_result, 182 VkDevice device, uint32_t bindInfoCount, const VkBindBufferMemoryInfo *pBindInfos); 183 184 VkResult on_vkCreateSemaphore( 185 void* context, VkResult, 186 VkDevice device, const VkSemaphoreCreateInfo* pCreateInfo, 187 const VkAllocationCallbacks* pAllocator, 188 VkSemaphore* pSemaphore); 189 void on_vkDestroySemaphore( 190 void* context, 191 VkDevice device, VkSemaphore semaphore, const VkAllocationCallbacks *pAllocator); 192 VkResult on_vkGetSemaphoreFdKHR( 193 void* context, VkResult, 194 VkDevice device, 195 const VkSemaphoreGetFdInfoKHR* pGetFdInfo, 196 int* pFd); 197 VkResult on_vkImportSemaphoreFdKHR( 198 void* context, VkResult, 199 VkDevice device, 200 const VkImportSemaphoreFdInfoKHR* pImportSemaphoreFdInfo); 201 202 VkResult on_vkQueueSubmit( 203 void* context, VkResult input_result, 204 VkQueue queue, uint32_t submitCount, const VkSubmitInfo* pSubmits, VkFence fence); 205 206 void unwrap_VkNativeBufferANDROID( 207 const VkImageCreateInfo* pCreateInfo, 208 VkImageCreateInfo* local_pCreateInfo); 209 void unwrap_vkAcquireImageANDROID_nativeFenceFd(int fd, int* fd_out); 210 211 #ifdef VK_USE_PLATFORM_FUCHSIA 212 VkResult on_vkGetMemoryZirconHandleFUCHSIA( 213 void* context, VkResult input_result, 214 VkDevice device, 215 const VkMemoryGetZirconHandleInfoFUCHSIA* pInfo, 216 uint32_t* pHandle); 217 VkResult on_vkGetMemoryZirconHandlePropertiesFUCHSIA( 218 void* context, VkResult input_result, 219 VkDevice device, 220 VkExternalMemoryHandleTypeFlagBits handleType, 221 uint32_t handle, 222 VkMemoryZirconHandlePropertiesFUCHSIA* pProperties); 223 VkResult on_vkGetSemaphoreZirconHandleFUCHSIA( 224 void* context, VkResult input_result, 225 VkDevice device, 226 const VkSemaphoreGetZirconHandleInfoFUCHSIA* pInfo, 227 uint32_t* pHandle); 228 VkResult on_vkImportSemaphoreZirconHandleFUCHSIA( 229 void* context, VkResult input_result, 230 VkDevice device, 231 const VkImportSemaphoreZirconHandleInfoFUCHSIA* pInfo); 232 VkResult on_vkCreateBufferCollectionFUCHSIA( 233 void* context, VkResult input_result, 234 VkDevice device, 235 const VkBufferCollectionCreateInfoFUCHSIA* pInfo, 236 const VkAllocationCallbacks* pAllocator, 237 VkBufferCollectionFUCHSIA* pCollection); 238 void on_vkDestroyBufferCollectionFUCHSIA( 239 void* context, VkResult input_result, 240 VkDevice device, 241 VkBufferCollectionFUCHSIA collection, 242 const VkAllocationCallbacks* pAllocator); 243 VkResult on_vkSetBufferCollectionConstraintsFUCHSIA( 244 void* context, VkResult input_result, 245 VkDevice device, 246 VkBufferCollectionFUCHSIA collection, 247 const VkImageCreateInfo* pImageInfo); 248 VkResult on_vkGetBufferCollectionPropertiesFUCHSIA( 249 void* context, VkResult input_result, 250 VkDevice device, 251 VkBufferCollectionFUCHSIA collection, 252 VkBufferCollectionPropertiesFUCHSIA* pProperties); 253 #endif 254 255 VkResult on_vkGetAndroidHardwareBufferPropertiesANDROID( 256 void* context, VkResult input_result, 257 VkDevice device, 258 const AHardwareBuffer* buffer, 259 VkAndroidHardwareBufferPropertiesANDROID* pProperties); 260 VkResult on_vkGetMemoryAndroidHardwareBufferANDROID( 261 void* context, VkResult input_result, 262 VkDevice device, 263 const VkMemoryGetAndroidHardwareBufferInfoANDROID *pInfo, 264 struct AHardwareBuffer** pBuffer); 265 266 VkResult on_vkCreateSamplerYcbcrConversion( 267 void* context, VkResult input_result, 268 VkDevice device, 269 const VkSamplerYcbcrConversionCreateInfo* pCreateInfo, 270 const VkAllocationCallbacks* pAllocator, 271 VkSamplerYcbcrConversion* pYcbcrConversion); 272 VkResult on_vkCreateSamplerYcbcrConversionKHR( 273 void* context, VkResult input_result, 274 VkDevice device, 275 const VkSamplerYcbcrConversionCreateInfo* pCreateInfo, 276 const VkAllocationCallbacks* pAllocator, 277 VkSamplerYcbcrConversion* pYcbcrConversion); 278 279 VkResult on_vkMapMemoryIntoAddressSpaceGOOGLE_pre( 280 void* context, 281 VkResult input_result, 282 VkDevice device, 283 VkDeviceMemory memory, 284 uint64_t* pAddress); 285 VkResult on_vkMapMemoryIntoAddressSpaceGOOGLE( 286 void* context, 287 VkResult input_result, 288 VkDevice device, 289 VkDeviceMemory memory, 290 uint64_t* pAddress); 291 292 VkResult on_vkCreateDescriptorUpdateTemplate( 293 void* context, VkResult input_result, 294 VkDevice device, 295 const VkDescriptorUpdateTemplateCreateInfo* pCreateInfo, 296 const VkAllocationCallbacks* pAllocator, 297 VkDescriptorUpdateTemplate* pDescriptorUpdateTemplate); 298 299 VkResult on_vkCreateDescriptorUpdateTemplateKHR( 300 void* context, VkResult input_result, 301 VkDevice device, 302 const VkDescriptorUpdateTemplateCreateInfo* pCreateInfo, 303 const VkAllocationCallbacks* pAllocator, 304 VkDescriptorUpdateTemplate* pDescriptorUpdateTemplate); 305 306 void on_vkUpdateDescriptorSetWithTemplate( 307 void* context, 308 VkDevice device, 309 VkDescriptorSet descriptorSet, 310 VkDescriptorUpdateTemplate descriptorUpdateTemplate, 311 const void* pData); 312 313 VkResult on_vkGetPhysicalDeviceImageFormatProperties2( 314 void* context, VkResult input_result, 315 VkPhysicalDevice physicalDevice, 316 const VkPhysicalDeviceImageFormatInfo2* pImageFormatInfo, 317 VkImageFormatProperties2* pImageFormatProperties); 318 319 VkResult on_vkGetPhysicalDeviceImageFormatProperties2KHR( 320 void* context, VkResult input_result, 321 VkPhysicalDevice physicalDevice, 322 const VkPhysicalDeviceImageFormatInfo2* pImageFormatInfo, 323 VkImageFormatProperties2* pImageFormatProperties); 324 325 VkResult on_vkBeginCommandBuffer( 326 void* context, VkResult input_result, 327 VkCommandBuffer commandBuffer, 328 const VkCommandBufferBeginInfo* pBeginInfo); 329 VkResult on_vkEndCommandBuffer( 330 void* context, VkResult input_result, 331 VkCommandBuffer commandBuffer); 332 VkResult on_vkResetCommandBuffer( 333 void* context, VkResult input_result, 334 VkCommandBuffer commandBuffer, 335 VkCommandBufferResetFlags flags); 336 337 VkResult on_vkCreateImageView( 338 void* context, VkResult input_result, 339 VkDevice device, 340 const VkImageViewCreateInfo* pCreateInfo, 341 const VkAllocationCallbacks* pAllocator, 342 VkImageView* pView); 343 344 bool isMemoryTypeHostVisible(VkDevice device, uint32_t typeIndex) const; 345 uint8_t* getMappedPointer(VkDeviceMemory memory); 346 VkDeviceSize getMappedSize(VkDeviceMemory memory); 347 VkDeviceSize getNonCoherentExtendedSize(VkDevice device, VkDeviceSize basicSize) const; 348 bool isValidMemoryRange(const VkMappedMemoryRange& range) const; 349 void setupFeatures(const EmulatorFeatureInfo* features); 350 bool hostSupportsVulkan() const; 351 bool usingDirectMapping() const; 352 uint32_t getApiVersionFromInstance(VkInstance instance) const; 353 uint32_t getApiVersionFromDevice(VkDevice device) const; 354 bool hasInstanceExtension(VkInstance instance, const std::string& name) const; 355 bool hasDeviceExtension(VkDevice instance, const std::string& name) const; 356 357 // Transforms 358 void deviceMemoryTransform_tohost( 359 VkDeviceMemory* memory, uint32_t memoryCount, 360 VkDeviceSize* offset, uint32_t offsetCount, 361 VkDeviceSize* size, uint32_t sizeCount, 362 uint32_t* typeIndex, uint32_t typeIndexCount, 363 uint32_t* typeBits, uint32_t typeBitsCount); 364 void deviceMemoryTransform_fromhost( 365 VkDeviceMemory* memory, uint32_t memoryCount, 366 VkDeviceSize* offset, uint32_t offsetCount, 367 VkDeviceSize* size, uint32_t sizeCount, 368 uint32_t* typeIndex, uint32_t typeIndexCount, 369 uint32_t* typeBits, uint32_t typeBitsCount); 370 371 #define DEFINE_TRANSFORMED_TYPE_PROTOTYPE(type) \ 372 void transformImpl_##type##_tohost(const type*, uint32_t); \ 373 void transformImpl_##type##_fromhost(const type*, uint32_t); \ 374 375 LIST_TRANSFORMED_TYPES(DEFINE_TRANSFORMED_TYPE_PROTOTYPE) 376 377 private: 378 class Impl; 379 std::unique_ptr<Impl> mImpl; 380 }; 381 382 } // namespace goldfish_vk 383