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 <vulkan/vulkan.h> 18 19 #include <atomic> 20 #include <functional> 21 #include <memory> 22 #include <optional> 23 #include <set> 24 #include <unordered_map> 25 26 #include "CommandBufferStagingStream.h" 27 #include "HostVisibleMemoryVirtualization.h" 28 #include "VirtGpu.h" 29 #include "VulkanHandleMapping.h" 30 #include "VulkanHandles.h" 31 #include "aemu/base/Optional.h" 32 #include "aemu/base/Tracing.h" 33 #include "aemu/base/synchronization/AndroidLock.h" 34 #include "aemu/base/threads/AndroidWorkPool.h" 35 #include "goldfish_vk_transform_guest.h" 36 37 using gfxstream::guest::AutoLock; 38 using gfxstream::guest::Lock; 39 using gfxstream::guest::Optional; 40 using gfxstream::guest::RecursiveLock; 41 using gfxstream::guest::WorkPool; 42 43 /// Use installed headers or locally defined Fuchsia-specific bits 44 #ifdef VK_USE_PLATFORM_FUCHSIA 45 46 #include <cutils/native_handle.h> 47 #include <fidl/fuchsia.hardware.goldfish/cpp/wire.h> 48 #include <fidl/fuchsia.sysmem/cpp/wire.h> 49 #include <lib/zx/channel.h> 50 #include <lib/zx/vmo.h> 51 #include <zircon/errors.h> 52 #include <zircon/process.h> 53 #include <zircon/rights.h> 54 #include <zircon/syscalls.h> 55 #include <zircon/syscalls/object.h> 56 57 #include <optional> 58 59 #include "services/service_connector.h" 60 61 #ifndef FUCHSIA_NO_TRACE 62 #include <lib/trace/event.h> 63 #endif 64 65 #define GET_STATUS_SAFE(result, member) ((result).ok() ? ((result)->member) : ZX_OK) 66 67 struct SetBufferCollectionImageConstraintsResult { 68 VkResult result; 69 fuchsia_sysmem::wire::BufferCollectionConstraints constraints; 70 std::vector<uint32_t> createInfoIndex; 71 }; 72 73 struct SetBufferCollectionBufferConstraintsResult { 74 VkResult result; 75 fuchsia_sysmem::wire::BufferCollectionConstraints constraints; 76 }; 77 78 #else 79 80 typedef uint32_t zx_handle_t; 81 typedef uint64_t zx_koid_t; 82 #define ZX_HANDLE_INVALID ((zx_handle_t)0) 83 #define ZX_KOID_INVALID ((zx_koid_t)0) 84 #endif // VK_USE_PLATFORM_FUCHSIA 85 86 /// Use installed headers or locally defined Android-specific bits 87 #ifdef VK_USE_PLATFORM_ANDROID_KHR 88 #include "AndroidHardwareBuffer.h" 89 #endif 90 91 #if defined(__linux__) || defined(__Fuchsia__) 92 #include <android/hardware_buffer.h> 93 #endif 94 95 #if GFXSTREAM_ENABLE_GUEST_GOLDFISH 96 /// Goldfish sync only used for AEMU -- should replace in virtio-gpu when possibe 97 #include "../egl/goldfish_sync.h" 98 #endif 99 100 struct EmulatorFeatureInfo; 101 102 class HostConnection; 103 104 namespace gfxstream { 105 namespace vk { 106 107 class VkEncoder; 108 109 class ResourceTracker { 110 public: 111 ResourceTracker(); 112 ~ResourceTracker(); 113 static ResourceTracker* get(); 114 115 VulkanHandleMapping* createMapping(); 116 VulkanHandleMapping* destroyMapping(); 117 118 using HostConnectionGetFunc = HostConnection* (*)(); 119 using VkEncoderGetFunc = VkEncoder* (*)(HostConnection*); 120 using CleanupCallback = std::function<void()>; 121 122 struct ThreadingCallbacks { 123 HostConnectionGetFunc hostConnectionGetFunc = nullptr; 124 VkEncoderGetFunc vkEncoderGetFunc = nullptr; 125 }; 126 127 static uint32_t streamFeatureBits; 128 static ThreadingCallbacks threadingCallbacks; 129 130 #define HANDLE_REGISTER_DECL(type) \ 131 void register_##type(type); \ 132 void unregister_##type(type); 133 134 GOLDFISH_VK_LIST_HANDLE_TYPES(HANDLE_REGISTER_DECL) 135 136 VkResult on_vkEnumerateInstanceExtensionProperties(void* context, VkResult input_result, 137 const char* pLayerName, 138 uint32_t* pPropertyCount, 139 VkExtensionProperties* pProperties); 140 141 VkResult on_vkEnumerateDeviceExtensionProperties(void* context, VkResult input_result, 142 VkPhysicalDevice physicalDevice, 143 const char* pLayerName, 144 uint32_t* pPropertyCount, 145 VkExtensionProperties* pProperties); 146 147 VkResult on_vkEnumeratePhysicalDevices(void* context, VkResult input_result, 148 VkInstance instance, uint32_t* pPhysicalDeviceCount, 149 VkPhysicalDevice* pPhysicalDevices); 150 151 void on_vkGetPhysicalDeviceFeatures2(void* context, VkPhysicalDevice physicalDevice, 152 VkPhysicalDeviceFeatures2* pFeatures); 153 void on_vkGetPhysicalDeviceFeatures2KHR(void* context, VkPhysicalDevice physicalDevice, 154 VkPhysicalDeviceFeatures2* pFeatures); 155 void on_vkGetPhysicalDeviceProperties(void* context, VkPhysicalDevice physicalDevice, 156 VkPhysicalDeviceProperties* pProperties); 157 void on_vkGetPhysicalDeviceProperties2(void* context, VkPhysicalDevice physicalDevice, 158 VkPhysicalDeviceProperties2* pProperties); 159 void on_vkGetPhysicalDeviceProperties2KHR(void* context, VkPhysicalDevice physicalDevice, 160 VkPhysicalDeviceProperties2* pProperties); 161 162 void on_vkGetPhysicalDeviceMemoryProperties( 163 void* context, VkPhysicalDevice physicalDevice, 164 VkPhysicalDeviceMemoryProperties* pMemoryProperties); 165 void on_vkGetPhysicalDeviceMemoryProperties2( 166 void* context, VkPhysicalDevice physicalDevice, 167 VkPhysicalDeviceMemoryProperties2* pMemoryProperties); 168 void on_vkGetPhysicalDeviceMemoryProperties2KHR( 169 void* context, VkPhysicalDevice physicalDevice, 170 VkPhysicalDeviceMemoryProperties2* pMemoryProperties); 171 void on_vkGetDeviceQueue(void* context, VkDevice device, uint32_t queueFamilyIndex, 172 uint32_t queueIndex, VkQueue* pQueue); 173 void on_vkGetDeviceQueue2(void* context, VkDevice device, const VkDeviceQueueInfo2* pQueueInfo, 174 VkQueue* pQueue); 175 176 VkResult on_vkCreateInstance(void* context, VkResult input_result, 177 const VkInstanceCreateInfo* createInfo, 178 const VkAllocationCallbacks* pAllocator, VkInstance* pInstance); 179 VkResult on_vkCreateDevice(void* context, VkResult input_result, 180 VkPhysicalDevice physicalDevice, 181 const VkDeviceCreateInfo* pCreateInfo, 182 const VkAllocationCallbacks* pAllocator, VkDevice* pDevice); 183 void on_vkDestroyDevice_pre(void* context, VkDevice device, 184 const VkAllocationCallbacks* pAllocator); 185 186 VkResult on_vkAllocateMemory(void* context, VkResult input_result, VkDevice device, 187 const VkMemoryAllocateInfo* pAllocateInfo, 188 const VkAllocationCallbacks* pAllocator, VkDeviceMemory* pMemory); 189 void on_vkFreeMemory(void* context, VkDevice device, VkDeviceMemory memory, 190 const VkAllocationCallbacks* pAllocator); 191 192 VkResult on_vkMapMemory(void* context, VkResult input_result, VkDevice device, 193 VkDeviceMemory memory, VkDeviceSize offset, VkDeviceSize size, 194 VkMemoryMapFlags, void** ppData); 195 196 void on_vkUnmapMemory(void* context, VkDevice device, VkDeviceMemory memory); 197 198 VkResult on_vkCreateImage(void* context, VkResult input_result, VkDevice device, 199 const VkImageCreateInfo* pCreateInfo, 200 const VkAllocationCallbacks* pAllocator, VkImage* pImage); 201 void on_vkDestroyImage(void* context, VkDevice device, VkImage image, 202 const VkAllocationCallbacks* pAllocator); 203 204 void on_vkGetImageMemoryRequirements(void* context, VkDevice device, VkImage image, 205 VkMemoryRequirements* pMemoryRequirements); 206 void on_vkGetImageMemoryRequirements2(void* context, VkDevice device, 207 const VkImageMemoryRequirementsInfo2* pInfo, 208 VkMemoryRequirements2* pMemoryRequirements); 209 void on_vkGetImageMemoryRequirements2KHR(void* context, VkDevice device, 210 const VkImageMemoryRequirementsInfo2* pInfo, 211 VkMemoryRequirements2* pMemoryRequirements); 212 void on_vkGetImageSubresourceLayout(void* context, VkDevice device, VkImage image, 213 const VkImageSubresource* pSubresource, 214 VkSubresourceLayout* pLayout); 215 216 VkResult on_vkBindImageMemory(void* context, VkResult input_result, VkDevice device, 217 VkImage image, VkDeviceMemory memory, VkDeviceSize memoryOffset); 218 VkResult on_vkBindImageMemory2(void* context, VkResult input_result, VkDevice device, 219 uint32_t bindingCount, const VkBindImageMemoryInfo* pBindInfos); 220 VkResult on_vkBindImageMemory2KHR(void* context, VkResult input_result, VkDevice device, 221 uint32_t bindingCount, 222 const VkBindImageMemoryInfo* pBindInfos); 223 224 VkResult on_vkCreateBuffer(void* context, VkResult input_result, VkDevice device, 225 const VkBufferCreateInfo* pCreateInfo, 226 const VkAllocationCallbacks* pAllocator, VkBuffer* pBuffer); 227 void on_vkDestroyBuffer(void* context, VkDevice device, VkBuffer buffer, 228 const VkAllocationCallbacks* pAllocator); 229 230 void on_vkGetBufferMemoryRequirements(void* context, VkDevice device, VkBuffer buffer, 231 VkMemoryRequirements* pMemoryRequirements); 232 void on_vkGetBufferMemoryRequirements2(void* context, VkDevice device, 233 const VkBufferMemoryRequirementsInfo2* pInfo, 234 VkMemoryRequirements2* pMemoryRequirements); 235 void on_vkGetBufferMemoryRequirements2KHR(void* context, VkDevice device, 236 const VkBufferMemoryRequirementsInfo2* pInfo, 237 VkMemoryRequirements2* pMemoryRequirements); 238 239 VkResult on_vkBindBufferMemory(void* context, VkResult input_result, VkDevice device, 240 VkBuffer buffer, VkDeviceMemory memory, 241 VkDeviceSize memoryOffset); 242 VkResult on_vkBindBufferMemory2(void* context, VkResult input_result, VkDevice device, 243 uint32_t bindInfoCount, 244 const VkBindBufferMemoryInfo* pBindInfos); 245 VkResult on_vkBindBufferMemory2KHR(void* context, VkResult input_result, VkDevice device, 246 uint32_t bindInfoCount, 247 const VkBindBufferMemoryInfo* pBindInfos); 248 249 VkResult on_vkCreateSemaphore(void* context, VkResult, VkDevice device, 250 const VkSemaphoreCreateInfo* pCreateInfo, 251 const VkAllocationCallbacks* pAllocator, VkSemaphore* pSemaphore); 252 void on_vkDestroySemaphore(void* context, VkDevice device, VkSemaphore semaphore, 253 const VkAllocationCallbacks* pAllocator); 254 VkResult on_vkGetSemaphoreFdKHR(void* context, VkResult, VkDevice device, 255 const VkSemaphoreGetFdInfoKHR* pGetFdInfo, int* pFd); 256 VkResult on_vkImportSemaphoreFdKHR(void* context, VkResult, VkDevice device, 257 const VkImportSemaphoreFdInfoKHR* pImportSemaphoreFdInfo); 258 259 VkResult on_vkQueueSubmit(void* context, VkResult input_result, VkQueue queue, 260 uint32_t submitCount, const VkSubmitInfo* pSubmits, VkFence fence); 261 262 VkResult on_vkQueueSubmit2(void* context, VkResult input_result, VkQueue queue, 263 uint32_t submitCount, const VkSubmitInfo2* pSubmits, VkFence fence); 264 265 VkResult on_vkQueueWaitIdle(void* context, VkResult input_result, VkQueue queue); 266 267 void unwrap_vkCreateImage_pCreateInfo(const VkImageCreateInfo* pCreateInfo, 268 VkImageCreateInfo* local_pCreateInfo); 269 270 void unwrap_vkAcquireImageANDROID_nativeFenceFd(int fd, int* fd_out); 271 272 void unwrap_VkBindImageMemory2_pBindInfos(uint32_t bindInfoCount, 273 const VkBindImageMemoryInfo* inputBindInfos, 274 VkBindImageMemoryInfo* outputBindInfos); 275 276 VkResult on_vkGetMemoryFdKHR(void* context, VkResult input_result, VkDevice device, 277 const VkMemoryGetFdInfoKHR* pGetFdInfo, int* pFd); 278 VkResult on_vkGetMemoryFdPropertiesKHR(void* context, VkResult input_result, VkDevice device, 279 VkExternalMemoryHandleTypeFlagBits handleType, int fd, 280 VkMemoryFdPropertiesKHR* pMemoryFdProperties); 281 282 #ifdef VK_USE_PLATFORM_FUCHSIA 283 VkResult on_vkGetMemoryZirconHandleFUCHSIA(void* context, VkResult input_result, 284 VkDevice device, 285 const VkMemoryGetZirconHandleInfoFUCHSIA* pInfo, 286 uint32_t* pHandle); 287 VkResult on_vkGetMemoryZirconHandlePropertiesFUCHSIA( 288 void* context, VkResult input_result, VkDevice device, 289 VkExternalMemoryHandleTypeFlagBits handleType, uint32_t handle, 290 VkMemoryZirconHandlePropertiesFUCHSIA* pProperties); 291 VkResult on_vkGetSemaphoreZirconHandleFUCHSIA( 292 void* context, VkResult input_result, VkDevice device, 293 const VkSemaphoreGetZirconHandleInfoFUCHSIA* pInfo, uint32_t* pHandle); 294 VkResult on_vkImportSemaphoreZirconHandleFUCHSIA( 295 void* context, VkResult input_result, VkDevice device, 296 const VkImportSemaphoreZirconHandleInfoFUCHSIA* pInfo); 297 VkResult on_vkCreateBufferCollectionFUCHSIA(void* context, VkResult input_result, 298 VkDevice device, 299 const VkBufferCollectionCreateInfoFUCHSIA* pInfo, 300 const VkAllocationCallbacks* pAllocator, 301 VkBufferCollectionFUCHSIA* pCollection); 302 void on_vkDestroyBufferCollectionFUCHSIA(void* context, VkResult input_result, VkDevice device, 303 VkBufferCollectionFUCHSIA collection, 304 const VkAllocationCallbacks* pAllocator); 305 VkResult on_vkSetBufferCollectionBufferConstraintsFUCHSIA( 306 void* context, VkResult input_result, VkDevice device, VkBufferCollectionFUCHSIA collection, 307 const VkBufferConstraintsInfoFUCHSIA* pBufferConstraintsInfo); 308 VkResult on_vkSetBufferCollectionImageConstraintsFUCHSIA( 309 void* context, VkResult input_result, VkDevice device, VkBufferCollectionFUCHSIA collection, 310 const VkImageConstraintsInfoFUCHSIA* pImageConstraintsInfo); 311 VkResult on_vkGetBufferCollectionPropertiesFUCHSIA( 312 void* context, VkResult input_result, VkDevice device, VkBufferCollectionFUCHSIA collection, 313 VkBufferCollectionPropertiesFUCHSIA* pProperties); 314 315 VkResult setBufferCollectionImageConstraintsFUCHSIA( 316 VkEncoder* enc, VkDevice device, 317 fidl::WireSyncClient<fuchsia_sysmem::BufferCollection>* pCollection, 318 const VkImageConstraintsInfoFUCHSIA* pImageConstraintsInfo); 319 320 VkResult setBufferCollectionBufferConstraintsFUCHSIA( 321 fidl::WireSyncClient<fuchsia_sysmem::BufferCollection>* pCollection, 322 const VkBufferConstraintsInfoFUCHSIA* pBufferConstraintsInfo); 323 324 #endif 325 326 #ifdef VK_USE_PLATFORM_ANDROID_KHR 327 VkResult on_vkGetAndroidHardwareBufferPropertiesANDROID( 328 void* context, VkResult input_result, VkDevice device, const AHardwareBuffer* buffer, 329 VkAndroidHardwareBufferPropertiesANDROID* pProperties); 330 VkResult on_vkGetMemoryAndroidHardwareBufferANDROID( 331 void* context, VkResult input_result, VkDevice device, 332 const VkMemoryGetAndroidHardwareBufferInfoANDROID* pInfo, struct AHardwareBuffer** pBuffer); 333 #endif 334 335 VkResult on_vkCreateSamplerYcbcrConversion( 336 void* context, VkResult input_result, VkDevice device, 337 const VkSamplerYcbcrConversionCreateInfo* pCreateInfo, 338 const VkAllocationCallbacks* pAllocator, VkSamplerYcbcrConversion* pYcbcrConversion); 339 void on_vkDestroySamplerYcbcrConversion(void* context, VkDevice device, 340 VkSamplerYcbcrConversion ycbcrConversion, 341 const VkAllocationCallbacks* pAllocator); 342 VkResult on_vkCreateSamplerYcbcrConversionKHR( 343 void* context, VkResult input_result, VkDevice device, 344 const VkSamplerYcbcrConversionCreateInfo* pCreateInfo, 345 const VkAllocationCallbacks* pAllocator, VkSamplerYcbcrConversion* pYcbcrConversion); 346 void on_vkDestroySamplerYcbcrConversionKHR(void* context, VkDevice device, 347 VkSamplerYcbcrConversion ycbcrConversion, 348 const VkAllocationCallbacks* pAllocator); 349 350 VkResult on_vkCreateSampler(void* context, VkResult input_result, VkDevice device, 351 const VkSamplerCreateInfo* pCreateInfo, 352 const VkAllocationCallbacks* pAllocator, VkSampler* pSampler); 353 354 void on_vkGetPhysicalDeviceExternalFenceProperties( 355 void* context, VkPhysicalDevice physicalDevice, 356 const VkPhysicalDeviceExternalFenceInfo* pExternalFenceInfo, 357 VkExternalFenceProperties* pExternalFenceProperties); 358 359 void on_vkGetPhysicalDeviceExternalFencePropertiesKHR( 360 void* context, VkPhysicalDevice physicalDevice, 361 const VkPhysicalDeviceExternalFenceInfo* pExternalFenceInfo, 362 VkExternalFenceProperties* pExternalFenceProperties); 363 364 VkResult on_vkCreateFence(void* context, VkResult input_result, VkDevice device, 365 const VkFenceCreateInfo* pCreateInfo, 366 const VkAllocationCallbacks* pAllocator, VkFence* pFence); 367 368 void on_vkDestroyFence(void* context, VkDevice device, VkFence fence, 369 const VkAllocationCallbacks* pAllocator); 370 371 VkResult on_vkResetFences(void* context, VkResult input_result, VkDevice device, 372 uint32_t fenceCount, const VkFence* pFences); 373 374 VkResult on_vkImportFenceFdKHR(void* context, VkResult input_result, VkDevice device, 375 const VkImportFenceFdInfoKHR* pImportFenceFdInfo); 376 377 VkResult on_vkGetFenceFdKHR(void* context, VkResult input_result, VkDevice device, 378 const VkFenceGetFdInfoKHR* pGetFdInfo, int* pFd); 379 380 VkResult on_vkWaitForFences(void* context, VkResult input_result, VkDevice device, 381 uint32_t fenceCount, const VkFence* pFences, VkBool32 waitAll, 382 uint64_t timeout); 383 384 VkResult on_vkCreateDescriptorPool(void* context, VkResult input_result, VkDevice device, 385 const VkDescriptorPoolCreateInfo* pCreateInfo, 386 const VkAllocationCallbacks* pAllocator, 387 VkDescriptorPool* pDescriptorPool); 388 389 void on_vkDestroyDescriptorPool(void* context, VkDevice device, VkDescriptorPool descriptorPool, 390 const VkAllocationCallbacks* pAllocator); 391 392 VkResult on_vkResetDescriptorPool(void* context, VkResult input_result, VkDevice device, 393 VkDescriptorPool descriptorPool, 394 VkDescriptorPoolResetFlags flags); 395 396 VkResult on_vkAllocateDescriptorSets(void* context, VkResult input_result, VkDevice device, 397 const VkDescriptorSetAllocateInfo* pAllocateInfo, 398 VkDescriptorSet* pDescriptorSets); 399 400 VkResult on_vkFreeDescriptorSets(void* context, VkResult input_result, VkDevice device, 401 VkDescriptorPool descriptorPool, uint32_t descriptorSetCount, 402 const VkDescriptorSet* pDescriptorSets); 403 404 VkResult on_vkCreateDescriptorSetLayout(void* context, VkResult input_result, VkDevice device, 405 const VkDescriptorSetLayoutCreateInfo* pCreateInfo, 406 const VkAllocationCallbacks* pAllocator, 407 VkDescriptorSetLayout* pSetLayout); 408 409 void on_vkUpdateDescriptorSets(void* context, VkDevice device, uint32_t descriptorWriteCount, 410 const VkWriteDescriptorSet* pDescriptorWrites, 411 uint32_t descriptorCopyCount, 412 const VkCopyDescriptorSet* pDescriptorCopies); 413 414 VkResult on_vkMapMemoryIntoAddressSpaceGOOGLE_pre(void* context, VkResult input_result, 415 VkDevice device, VkDeviceMemory memory, 416 uint64_t* pAddress); 417 VkResult on_vkMapMemoryIntoAddressSpaceGOOGLE(void* context, VkResult input_result, 418 VkDevice device, VkDeviceMemory memory, 419 uint64_t* pAddress); 420 421 VkResult on_vkCreateDescriptorUpdateTemplate( 422 void* context, VkResult input_result, VkDevice device, 423 const VkDescriptorUpdateTemplateCreateInfo* pCreateInfo, 424 const VkAllocationCallbacks* pAllocator, 425 VkDescriptorUpdateTemplate* pDescriptorUpdateTemplate); 426 427 VkResult on_vkCreateDescriptorUpdateTemplateKHR( 428 void* context, VkResult input_result, VkDevice device, 429 const VkDescriptorUpdateTemplateCreateInfo* pCreateInfo, 430 const VkAllocationCallbacks* pAllocator, 431 VkDescriptorUpdateTemplate* pDescriptorUpdateTemplate); 432 433 void on_vkUpdateDescriptorSetWithTemplate(void* context, VkDevice device, 434 VkDescriptorSet descriptorSet, 435 VkDescriptorUpdateTemplate descriptorUpdateTemplate, 436 const void* pData); 437 438 void on_vkUpdateDescriptorSetWithTemplateKHR(void* context, VkDevice device, 439 VkDescriptorSet descriptorSet, 440 VkDescriptorUpdateTemplate descriptorUpdateTemplate, 441 const void* pData); 442 443 VkResult on_vkGetPhysicalDeviceImageFormatProperties2( 444 void* context, VkResult input_result, VkPhysicalDevice physicalDevice, 445 const VkPhysicalDeviceImageFormatInfo2* pImageFormatInfo, 446 VkImageFormatProperties2* pImageFormatProperties); 447 448 VkResult on_vkGetPhysicalDeviceImageFormatProperties2KHR( 449 void* context, VkResult input_result, VkPhysicalDevice physicalDevice, 450 const VkPhysicalDeviceImageFormatInfo2* pImageFormatInfo, 451 VkImageFormatProperties2* pImageFormatProperties); 452 453 void on_vkGetPhysicalDeviceExternalBufferProperties( 454 void* context, VkPhysicalDevice physicalDevice, 455 const VkPhysicalDeviceExternalBufferInfo* pExternalBufferInfo, 456 VkExternalBufferProperties* pExternalBufferProperties); 457 458 void on_vkGetPhysicalDeviceExternalBufferPropertiesKHR( 459 void* context, VkPhysicalDevice physicalDevice, 460 const VkPhysicalDeviceExternalBufferInfoKHR* pExternalBufferInfo, 461 VkExternalBufferPropertiesKHR* pExternalBufferProperties); 462 463 void on_vkGetPhysicalDeviceExternalSemaphoreProperties( 464 void* context, VkPhysicalDevice physicalDevice, 465 const VkPhysicalDeviceExternalSemaphoreInfo* pExternalSemaphoreInfo, 466 VkExternalSemaphoreProperties* pExternalSemaphoreProperties); 467 468 void on_vkGetPhysicalDeviceExternalSemaphorePropertiesKHR( 469 void* context, VkPhysicalDevice physicalDevice, 470 const VkPhysicalDeviceExternalSemaphoreInfo* pExternalSemaphoreInfo, 471 VkExternalSemaphoreProperties* pExternalSemaphoreProperties); 472 473 void registerEncoderCleanupCallback(const VkEncoder* encoder, void* handle, 474 CleanupCallback callback); 475 void unregisterEncoderCleanupCallback(const VkEncoder* encoder, void* handle); 476 void onEncoderDeleted(const VkEncoder* encoder); 477 478 uint32_t syncEncodersForCommandBuffer(VkCommandBuffer commandBuffer, VkEncoder* current); 479 uint32_t syncEncodersForQueue(VkQueue queue, VkEncoder* currentEncoder); 480 481 CommandBufferStagingStream::Alloc getAlloc(); 482 CommandBufferStagingStream::Free getFree(); 483 484 VkResult on_vkBeginCommandBuffer(void* context, VkResult input_result, 485 VkCommandBuffer commandBuffer, 486 const VkCommandBufferBeginInfo* pBeginInfo); 487 VkResult on_vkEndCommandBuffer(void* context, VkResult input_result, 488 VkCommandBuffer commandBuffer); 489 VkResult on_vkResetCommandBuffer(void* context, VkResult input_result, 490 VkCommandBuffer commandBuffer, 491 VkCommandBufferResetFlags flags); 492 493 VkResult on_vkCreateImageView(void* context, VkResult input_result, VkDevice device, 494 const VkImageViewCreateInfo* pCreateInfo, 495 const VkAllocationCallbacks* pAllocator, VkImageView* pView); 496 497 void on_vkCmdExecuteCommands(void* context, VkCommandBuffer commandBuffer, 498 uint32_t commandBufferCount, 499 const VkCommandBuffer* pCommandBuffers); 500 501 void on_vkCmdBindDescriptorSets(void* context, VkCommandBuffer commandBuffer, 502 VkPipelineBindPoint pipelineBindPoint, VkPipelineLayout layout, 503 uint32_t firstSet, uint32_t descriptorSetCount, 504 const VkDescriptorSet* pDescriptorSets, 505 uint32_t dynamicOffsetCount, const uint32_t* pDynamicOffsets); 506 507 void on_vkCmdPipelineBarrier( 508 void* context, VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask, 509 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags, 510 uint32_t memoryBarrierCount, const VkMemoryBarrier* pMemoryBarriers, 511 uint32_t bufferMemoryBarrierCount, const VkBufferMemoryBarrier* pBufferMemoryBarriers, 512 uint32_t imageMemoryBarrierCount, const VkImageMemoryBarrier* pImageMemoryBarriers); 513 514 void on_vkDestroyDescriptorSetLayout(void* context, VkDevice device, 515 VkDescriptorSetLayout descriptorSetLayout, 516 const VkAllocationCallbacks* pAllocator); 517 518 VkResult on_vkAllocateCommandBuffers(void* context, VkResult input_result, VkDevice device, 519 const VkCommandBufferAllocateInfo* pAllocateInfo, 520 VkCommandBuffer* pCommandBuffers); 521 522 VkResult on_vkQueueSignalReleaseImageANDROID(void* context, VkResult input_result, 523 VkQueue queue, uint32_t waitSemaphoreCount, 524 const VkSemaphore* pWaitSemaphores, VkImage image, 525 int* pNativeFenceFd); 526 527 VkResult on_vkCreateGraphicsPipelines(void* context, VkResult input_result, VkDevice device, 528 VkPipelineCache pipelineCache, uint32_t createInfoCount, 529 const VkGraphicsPipelineCreateInfo* pCreateInfos, 530 const VkAllocationCallbacks* pAllocator, 531 VkPipeline* pPipelines); 532 533 uint8_t* getMappedPointer(VkDeviceMemory memory); 534 VkDeviceSize getMappedSize(VkDeviceMemory memory); 535 VkDeviceSize getNonCoherentExtendedSize(VkDevice device, VkDeviceSize basicSize) const; 536 bool isValidMemoryRange(const VkMappedMemoryRange& range) const; 537 538 void setupFeatures(const EmulatorFeatureInfo* features); 539 void setupCaps(uint32_t& noRenderControlEnc); 540 541 void setThreadingCallbacks(const ThreadingCallbacks& callbacks); 542 bool hostSupportsVulkan() const; 543 bool usingDirectMapping() const; 544 uint32_t getStreamFeatures() const; 545 uint32_t getApiVersionFromInstance(VkInstance instance) const; 546 uint32_t getApiVersionFromDevice(VkDevice device) const; 547 bool hasInstanceExtension(VkInstance instance, const std::string& name) const; 548 bool hasDeviceExtension(VkDevice instance, const std::string& name) const; 549 VkDevice getDevice(VkCommandBuffer commandBuffer) const; 550 void addToCommandPool(VkCommandPool commandPool, uint32_t commandBufferCount, 551 VkCommandBuffer* pCommandBuffers); 552 void resetCommandPoolStagingInfo(VkCommandPool commandPool); 553 554 #ifdef __GNUC__ 555 #define ALWAYS_INLINE_GFXSTREAM 556 #elif 557 #define ALWAYS_INLINE_GFXSTREAM __attribute__((always_inline)) 558 #endif 559 560 static VkEncoder* getCommandBufferEncoder(VkCommandBuffer commandBuffer); 561 static VkEncoder* getQueueEncoder(VkQueue queue); 562 static VkEncoder* getThreadLocalEncoder(); 563 564 static void setSeqnoPtr(uint32_t* seqnoptr); 565 static ALWAYS_INLINE_GFXSTREAM uint32_t nextSeqno(); 566 static ALWAYS_INLINE_GFXSTREAM uint32_t getSeqno(); 567 568 // Transforms 569 void deviceMemoryTransform_tohost(VkDeviceMemory* memory, uint32_t memoryCount, 570 VkDeviceSize* offset, uint32_t offsetCount, 571 VkDeviceSize* size, uint32_t sizeCount, uint32_t* typeIndex, 572 uint32_t typeIndexCount, uint32_t* typeBits, 573 uint32_t typeBitsCount); 574 void deviceMemoryTransform_fromhost(VkDeviceMemory* memory, uint32_t memoryCount, 575 VkDeviceSize* offset, uint32_t offsetCount, 576 VkDeviceSize* size, uint32_t sizeCount, uint32_t* typeIndex, 577 uint32_t typeIndexCount, uint32_t* typeBits, 578 uint32_t typeBitsCount); 579 580 void transformImpl_VkExternalMemoryProperties_fromhost(VkExternalMemoryProperties* pProperties, 581 uint32_t); 582 void transformImpl_VkExternalMemoryProperties_tohost(VkExternalMemoryProperties* pProperties, 583 uint32_t); 584 void transformImpl_VkImageCreateInfo_fromhost(const VkImageCreateInfo*, uint32_t); 585 void transformImpl_VkImageCreateInfo_tohost(const VkImageCreateInfo*, uint32_t); 586 587 #define DEFINE_TRANSFORMED_TYPE_PROTOTYPE(type) \ 588 void transformImpl_##type##_tohost(type*, uint32_t); \ 589 void transformImpl_##type##_fromhost(type*, uint32_t); 590 591 LIST_TRIVIAL_TRANSFORMED_TYPES(DEFINE_TRANSFORMED_TYPE_PROTOTYPE) 592 593 private: 594 VulkanHandleMapping* mCreateMapping = nullptr; 595 VulkanHandleMapping* mDestroyMapping = nullptr; 596 597 uint32_t getColorBufferMemoryIndex(void* context, VkDevice device); 598 const VkPhysicalDeviceMemoryProperties& getPhysicalDeviceMemoryProperties( 599 void* context, VkDevice device, VkPhysicalDevice physicalDevice); 600 601 VkResult on_vkGetPhysicalDeviceImageFormatProperties2_common( 602 bool isKhr, void* context, VkResult input_result, VkPhysicalDevice physicalDevice, 603 const VkPhysicalDeviceImageFormatInfo2* pImageFormatInfo, 604 VkImageFormatProperties2* pImageFormatProperties); 605 606 void on_vkGetPhysicalDeviceExternalBufferProperties_common( 607 bool isKhr, void* context, VkPhysicalDevice physicalDevice, 608 const VkPhysicalDeviceExternalBufferInfo* pExternalBufferInfo, 609 VkExternalBufferProperties* pExternalBufferProperties); 610 611 template <typename VkSubmitInfoType> 612 VkResult on_vkQueueSubmitTemplate(void* context, VkResult input_result, VkQueue queue, 613 uint32_t submitCount, const VkSubmitInfoType* pSubmits, 614 VkFence fence); 615 616 void freeDescriptorSetsIfHostAllocated(VkEncoder* enc, VkDevice device, 617 uint32_t descriptorSetCount, 618 const VkDescriptorSet* sets); 619 void clearDescriptorPoolAndUnregisterDescriptorSets(void* context, VkDevice device, 620 VkDescriptorPool pool); 621 622 void setDeviceInfo(VkDevice device, VkPhysicalDevice physdev, VkPhysicalDeviceProperties props, 623 VkPhysicalDeviceMemoryProperties memProps, uint32_t enabledExtensionCount, 624 const char* const* ppEnabledExtensionNames, const void* pNext); 625 626 void setDeviceMemoryInfo(VkDevice device, VkDeviceMemory memory, VkDeviceSize allocationSize, 627 uint8_t* ptr, uint32_t memoryTypeIndex, AHardwareBuffer* ahw, 628 bool imported, zx_handle_t vmoHandle, VirtGpuResourcePtr blobPtr); 629 630 void setImageInfo(VkImage image, VkDevice device, const VkImageCreateInfo* pCreateInfo); 631 632 bool supportsDeferredCommands() const; 633 bool supportsAsyncQueueSubmit() const; 634 bool supportsCreateResourcesWithRequirements() const; 635 636 int getHostInstanceExtensionIndex(const std::string& extName) const; 637 int getHostDeviceExtensionIndex(const std::string& extName) const; 638 639 CoherentMemoryPtr createCoherentMemory(VkDevice device, VkDeviceMemory mem, 640 const VkMemoryAllocateInfo& hostAllocationInfo, 641 VkEncoder* enc, VkResult& res); 642 VkResult allocateCoherentMemory(VkDevice device, const VkMemoryAllocateInfo* pAllocateInfo, 643 VkEncoder* enc, VkDeviceMemory* pMemory); 644 645 VkResult getCoherentMemory(const VkMemoryAllocateInfo* pAllocateInfo, VkEncoder* enc, 646 VkDevice device, VkDeviceMemory* pMemory); 647 648 void transformImageMemoryRequirements2ForGuest(VkImage image, VkMemoryRequirements2* reqs2); 649 650 void transformBufferMemoryRequirements2ForGuest(VkBuffer buffer, VkMemoryRequirements2* reqs2); 651 652 void flushCommandBufferPendingCommandsBottomUp(void* context, VkQueue queue, 653 const std::vector<VkCommandBuffer>& workingSet); 654 655 template <class VkSubmitInfoType> 656 void flushStagingStreams(void* context, VkQueue queue, uint32_t submitCount, 657 const VkSubmitInfoType* pSubmits); 658 659 VkResult vkQueueSubmitEnc(VkEncoder* enc, VkQueue queue, uint32_t submitCount, 660 const VkSubmitInfo* pSubmits, VkFence fence); 661 662 VkResult vkQueueSubmitEnc(VkEncoder* enc, VkQueue queue, uint32_t submitCount, 663 const VkSubmitInfo2* pSubmits, VkFence fence); 664 665 VkResult initDescriptorUpdateTemplateBuffers( 666 const VkDescriptorUpdateTemplateCreateInfo* pCreateInfo, 667 VkDescriptorUpdateTemplate descriptorUpdateTemplate); 668 669 #if defined(VK_USE_PLATFORM_ANDROID_KHR) 670 VkResult exportSyncFdForQSRILocked(VkImage image, int* fd); 671 #endif 672 673 void setInstanceInfo(VkInstance instance, uint32_t enabledExtensionCount, 674 const char* const* ppEnabledExtensionNames, uint32_t apiVersion); 675 676 void resetCommandBufferStagingInfo(VkCommandBuffer commandBuffer, bool alsoResetPrimaries, 677 bool alsoClearPendingDescriptorSets); 678 679 void resetCommandBufferPendingTopology(VkCommandBuffer commandBuffer); 680 681 void clearCommandPool(VkCommandPool commandPool); 682 683 void ensureSyncDeviceFd(void); 684 685 #if defined(VK_USE_PLATFORM_ANDROID_KHR) 686 void unwrap_VkNativeBufferANDROID(const VkNativeBufferANDROID* inputNativeInfo, 687 VkNativeBufferANDROID* outputNativeInfo); 688 689 void unwrap_VkBindImageMemorySwapchainInfoKHR( 690 const VkBindImageMemorySwapchainInfoKHR* inputBimsi, 691 VkBindImageMemorySwapchainInfoKHR* outputBimsi); 692 #endif 693 694 #if defined(VK_USE_PLATFORM_FUCHSIA) 695 VkResult getBufferCollectionImageCreateInfoIndexLocked( 696 VkBufferCollectionFUCHSIA collection, fuchsia_sysmem::wire::BufferCollectionInfo2& info, 697 uint32_t* outCreateInfoIndex); 698 699 SetBufferCollectionImageConstraintsResult setBufferCollectionImageConstraintsImpl( 700 VkEncoder* enc, VkDevice device, 701 fidl::WireSyncClient<fuchsia_sysmem::BufferCollection>* pCollection, 702 const VkImageConstraintsInfoFUCHSIA* pImageConstraintsInfo); 703 704 VkResult setBufferCollectionConstraintsFUCHSIA( 705 VkEncoder* enc, VkDevice device, 706 fidl::WireSyncClient<fuchsia_sysmem::BufferCollection>* collection, 707 const VkImageCreateInfo* pImageInfo); 708 #endif 709 710 void unregister_VkDescriptorSet_locked(VkDescriptorSet set); 711 712 #define HANDLE_DEFINE_TRIVIAL_INFO_STRUCT(type) \ 713 struct type##_Info { \ 714 uint32_t unused; \ 715 }; 716 GOLDFISH_VK_LIST_TRIVIAL_HANDLE_TYPES(HANDLE_DEFINE_TRIVIAL_INFO_STRUCT) 717 718 struct VkInstance_Info { 719 uint32_t highestApiVersion; 720 std::set<std::string> enabledExtensions; 721 // Fodder for vkEnumeratePhysicalDevices. 722 std::vector<VkPhysicalDevice> physicalDevices; 723 }; 724 725 struct VkDevice_Info { 726 VkPhysicalDevice physdev; 727 VkPhysicalDeviceProperties props; 728 VkPhysicalDeviceMemoryProperties memProps; 729 uint32_t apiVersion; 730 std::set<std::string> enabledExtensions; 731 std::vector<std::pair<PFN_vkDeviceMemoryReportCallbackEXT, void*>> 732 deviceMemoryReportCallbacks; 733 }; 734 735 struct VkDeviceMemory_Info { 736 bool dedicated = false; 737 bool imported = false; 738 739 #ifdef VK_USE_PLATFORM_ANDROID_KHR 740 AHardwareBuffer* ahw = nullptr; 741 #endif 742 zx_handle_t vmoHandle = ZX_HANDLE_INVALID; 743 VkDevice device; 744 745 uint8_t* ptr = nullptr; 746 747 uint64_t blobId = 0; 748 uint64_t allocationSize = 0; 749 uint32_t memoryTypeIndex = 0; 750 uint64_t coherentMemorySize = 0; 751 uint64_t coherentMemoryOffset = 0; 752 753 #if defined(__ANDROID__) 754 GoldfishAddressSpaceBlockPtr goldfishBlock = nullptr; 755 #endif // defined(__ANDROID__) 756 CoherentMemoryPtr coherentMemory = nullptr; 757 VirtGpuResourcePtr blobPtr = nullptr; 758 }; 759 760 struct VkCommandBuffer_Info { 761 uint32_t placeholder; 762 }; 763 764 struct VkQueue_Info { 765 VkDevice device; 766 }; 767 768 // custom guest-side structs for images/buffers because of AHardwareBuffer :(( 769 struct VkImage_Info { 770 VkDevice device; 771 VkImageCreateInfo createInfo; 772 bool external = false; 773 VkExternalMemoryImageCreateInfo externalCreateInfo; 774 VkDeviceMemory currentBacking = VK_NULL_HANDLE; 775 VkDeviceSize currentBackingOffset = 0; 776 VkDeviceSize currentBackingSize = 0; 777 bool baseRequirementsKnown = false; 778 VkMemoryRequirements baseRequirements; 779 #ifdef VK_USE_PLATFORM_ANDROID_KHR 780 bool hasExternalFormat = false; 781 unsigned externalFourccFormat = 0; 782 std::vector<int> pendingQsriSyncFds; 783 #endif 784 #ifdef VK_USE_PLATFORM_FUCHSIA 785 bool isSysmemBackedMemory = false; 786 #endif 787 #ifdef LINUX_GUEST_BUILD 788 bool isDmaBufImage = false; 789 VkImage linearPeerImage = VK_NULL_HANDLE; 790 #endif 791 }; 792 793 struct VkBuffer_Info { 794 VkDevice device; 795 VkBufferCreateInfo createInfo; 796 bool external = false; 797 VkExternalMemoryBufferCreateInfo externalCreateInfo; 798 VkDeviceMemory currentBacking = VK_NULL_HANDLE; 799 VkDeviceSize currentBackingOffset = 0; 800 VkDeviceSize currentBackingSize = 0; 801 bool baseRequirementsKnown = false; 802 VkMemoryRequirements baseRequirements; 803 #ifdef VK_USE_PLATFORM_FUCHSIA 804 bool isSysmemBackedMemory = false; 805 #endif 806 }; 807 808 struct VkSemaphore_Info { 809 VkDevice device; 810 zx_handle_t eventHandle = ZX_HANDLE_INVALID; 811 zx_koid_t eventKoid = ZX_KOID_INVALID; 812 std::optional<int> syncFd = {}; 813 }; 814 815 struct VkDescriptorUpdateTemplate_Info { 816 uint32_t templateEntryCount = 0; 817 VkDescriptorUpdateTemplateEntry* templateEntries; 818 819 uint32_t imageInfoCount = 0; 820 uint32_t bufferInfoCount = 0; 821 uint32_t bufferViewCount = 0; 822 uint32_t inlineUniformBlockCount = 0; 823 uint32_t* imageInfoIndices; 824 uint32_t* bufferInfoIndices; 825 uint32_t* bufferViewIndices; 826 VkDescriptorImageInfo* imageInfos; 827 VkDescriptorBufferInfo* bufferInfos; 828 VkBufferView* bufferViews; 829 std::vector<uint8_t> inlineUniformBlockBuffer; 830 std::vector<uint32_t> inlineUniformBlockBytesPerBlocks; // bytes per uniform block 831 }; 832 833 struct VkFence_Info { 834 VkDevice device; 835 bool external = false; 836 VkExportFenceCreateInfo exportFenceCreateInfo; 837 #if defined(VK_USE_PLATFORM_ANDROID_KHR) || defined(__linux__) 838 int syncFd = -1; 839 #endif 840 }; 841 842 struct VkDescriptorPool_Info { 843 uint32_t unused; 844 }; 845 846 struct VkDescriptorSet_Info { 847 uint32_t unused; 848 }; 849 850 struct VkDescriptorSetLayout_Info { 851 uint32_t unused; 852 }; 853 854 struct VkCommandPool_Info { 855 uint32_t unused; 856 }; 857 858 struct VkSampler_Info { 859 uint32_t unused; 860 }; 861 862 struct VkBufferCollectionFUCHSIA_Info { 863 #ifdef VK_USE_PLATFORM_FUCHSIA 864 gfxstream::guest::Optional<fuchsia_sysmem::wire::BufferCollectionConstraints> constraints; 865 gfxstream::guest::Optional<VkBufferCollectionPropertiesFUCHSIA> properties; 866 867 // the index of corresponding createInfo for each image format 868 // constraints in |constraints|. 869 std::vector<uint32_t> createInfoIndex; 870 #endif // VK_USE_PLATFORM_FUCHSIA 871 }; 872 873 VkDescriptorImageInfo filterNonexistentSampler(const VkDescriptorImageInfo& inputInfo); 874 875 void emitDeviceMemoryReport(VkDevice_Info info, VkDeviceMemoryReportEventTypeEXT type, 876 uint64_t memoryObjectId, VkDeviceSize size, VkObjectType objectType, 877 uint64_t objectHandle, uint32_t heapIndex = 0); 878 879 void transformImageMemoryRequirementsForGuestLocked(VkImage image, VkMemoryRequirements* reqs); 880 CoherentMemoryPtr freeCoherentMemoryLocked(VkDeviceMemory memory, VkDeviceMemory_Info& info); 881 882 mutable RecursiveLock mLock; 883 884 std::optional<const VkPhysicalDeviceMemoryProperties> mCachedPhysicalDeviceMemoryProps; 885 std::unique_ptr<EmulatorFeatureInfo> mFeatureInfo; 886 #if defined(__ANDROID__) 887 std::unique_ptr<GoldfishAddressSpaceBlockProvider> mGoldfishAddressSpaceBlockProvider; 888 #endif // defined(__ANDROID__) 889 890 struct VirtGpuCaps mCaps; 891 std::vector<VkExtensionProperties> mHostInstanceExtensions; 892 std::vector<VkExtensionProperties> mHostDeviceExtensions; 893 894 // 32 bits only for now, upper bits may be used later. 895 std::atomic<uint32_t> mBlobId = 0; 896 #if defined(VK_USE_PLATFORM_ANDROID_KHR) || defined(__linux__) 897 int mSyncDeviceFd = -1; 898 #endif 899 900 #ifdef VK_USE_PLATFORM_FUCHSIA 901 fidl::WireSyncClient<fuchsia_hardware_goldfish::ControlDevice> mControlDevice; 902 fidl::WireSyncClient<fuchsia_sysmem::Allocator> mSysmemAllocator; 903 #endif 904 905 #define HANDLE_REGISTER_DECLARATION(type) std::unordered_map<type, type##_Info> info_##type; 906 GOLDFISH_VK_LIST_HANDLE_TYPES(HANDLE_REGISTER_DECLARATION)907 GOLDFISH_VK_LIST_HANDLE_TYPES(HANDLE_REGISTER_DECLARATION) 908 909 WorkPool mWorkPool{4}; 910 std::unordered_map<VkQueue, std::vector<WorkPool::WaitGroupHandle>> 911 mQueueSensitiveWorkPoolItems; 912 913 std::unordered_map<const VkEncoder*, std::unordered_map<void*, CleanupCallback>> 914 mEncoderCleanupCallbacks; 915 }; 916 917 } // namespace vk 918 } // namespace gfxstream 919