blob: 005887b8157e20f1c2d5210efee186f698b1249f [file] [log] [blame]
Lingfeng Yang71b596b2018-11-07 18:03:25 -08001// 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.
Lingfeng Yang71b596b2018-11-07 18:03:25 -080015#pragma once
16
17#include <vulkan/vulkan.h>
18
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -070019#include <atomic>
Lingfeng Yang256f9252020-07-14 14:27:33 -070020#include <functional>
Lingfeng Yang71b596b2018-11-07 18:03:25 -080021#include <memory>
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -070022#include <optional>
Jason Macnakeea882a2023-11-15 17:29:11 -080023#include <set>
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -070024#include <unordered_map>
Lingfeng Yang71b596b2018-11-07 18:03:25 -080025
Shalini Sdb704c92023-01-27 21:35:33 +000026#include "CommandBufferStagingStream.h"
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -070027#include "HostVisibleMemoryVirtualization.h"
28#include "VirtGpu.h"
Shalini Sdb704c92023-01-27 21:35:33 +000029#include "VulkanHandleMapping.h"
30#include "VulkanHandles.h"
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -070031#include "aemu/base/Optional.h"
Shalini Sdb704c92023-01-27 21:35:33 +000032#include "aemu/base/Tracing.h"
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -070033#include "aemu/base/synchronization/AndroidLock.h"
34#include "aemu/base/threads/AndroidWorkPool.h"
Lingfeng Yang2b1b8cf2019-02-08 09:53:36 -080035#include "goldfish_vk_transform_guest.h"
36
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -070037using gfxstream::guest::AutoLock;
38using gfxstream::guest::Lock;
39using gfxstream::guest::Optional;
40using gfxstream::guest::RecursiveLock;
41using 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
C Stout5a3a4222023-11-14 16:31:56 -080067struct SetBufferCollectionImageConstraintsResult {
68 VkResult result;
69 fuchsia_sysmem::wire::BufferCollectionConstraints constraints;
70 std::vector<uint32_t> createInfoIndex;
71};
72
73struct SetBufferCollectionBufferConstraintsResult {
74 VkResult result;
75 fuchsia_sysmem::wire::BufferCollectionConstraints constraints;
76};
77
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -070078#else
79
80typedef uint32_t zx_handle_t;
81typedef 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
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -070088#include "AndroidHardwareBuffer.h"
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -070089#endif
90
C Stout8a64ca82024-01-31 17:24:24 -080091#if defined(__linux__) || defined(__Fuchsia__)
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -070092#include <android/hardware_buffer.h>
Gurchetan Singhb7feebd2024-01-23 14:12:36 -080093#endif
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -070094
Gurchetan Singhb7feebd2024-01-23 14:12:36 -080095#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
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -070099
Lingfeng Yang31754632018-12-21 18:24:55 -0800100struct EmulatorFeatureInfo;
101
Lingfeng Yang34b5cae2019-08-21 14:12:19 -0700102class HostConnection;
103
Jason Macnak3d664002023-03-30 16:00:50 -0700104namespace gfxstream {
105namespace vk {
Lingfeng Yang71b596b2018-11-07 18:03:25 -0800106
Lingfeng Yang39a276e2019-06-17 13:27:22 -0700107class VkEncoder;
108
Lingfeng Yang97a06702018-12-24 17:02:43 -0800109class ResourceTracker {
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700110 public:
Lingfeng Yang71b596b2018-11-07 18:03:25 -0800111 ResourceTracker();
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700112 ~ResourceTracker();
Lingfeng Yang71b596b2018-11-07 18:03:25 -0800113 static ResourceTracker* get();
Lingfeng Yang40329112021-01-22 18:01:43 -0800114
Lingfeng Yang71b596b2018-11-07 18:03:25 -0800115 VulkanHandleMapping* createMapping();
Lingfeng Yang71b596b2018-11-07 18:03:25 -0800116 VulkanHandleMapping* destroyMapping();
Lingfeng Yang6ab1b0d2018-11-27 23:36:03 -0800117
Lingfeng Yang34b5cae2019-08-21 14:12:19 -0700118 using HostConnectionGetFunc = HostConnection* (*)();
Lingfeng Yangcd2d8fe2019-08-16 12:21:50 -0700119 using VkEncoderGetFunc = VkEncoder* (*)(HostConnection*);
Lingfeng Yang256f9252020-07-14 14:27:33 -0700120 using CleanupCallback = std::function<void()>;
Lingfeng Yang34b5cae2019-08-21 14:12:19 -0700121
122 struct ThreadingCallbacks {
Gurchetan Singh86725912023-09-06 14:22:46 -0700123 HostConnectionGetFunc hostConnectionGetFunc = nullptr;
124 VkEncoderGetFunc vkEncoderGetFunc = nullptr;
Lingfeng Yang34b5cae2019-08-21 14:12:19 -0700125 };
126
Lingfeng Yang40329112021-01-22 18:01:43 -0800127 static uint32_t streamFeatureBits;
128 static ThreadingCallbacks threadingCallbacks;
129
Lingfeng Yang6ab1b0d2018-11-27 23:36:03 -0800130#define HANDLE_REGISTER_DECL(type) \
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700131 void register_##type(type); \
132 void unregister_##type(type);
Lingfeng Yang6ab1b0d2018-11-27 23:36:03 -0800133
134 GOLDFISH_VK_LIST_HANDLE_TYPES(HANDLE_REGISTER_DECL)
135
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700136 VkResult on_vkEnumerateInstanceExtensionProperties(void* context, VkResult input_result,
137 const char* pLayerName,
138 uint32_t* pPropertyCount,
139 VkExtensionProperties* pProperties);
Lingfeng Yang154a33c2019-01-29 19:06:23 -0800140
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700141 VkResult on_vkEnumerateDeviceExtensionProperties(void* context, VkResult input_result,
142 VkPhysicalDevice physicalDevice,
143 const char* pLayerName,
144 uint32_t* pPropertyCount,
145 VkExtensionProperties* pProperties);
Lingfeng Yang154a33c2019-01-29 19:06:23 -0800146
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700147 VkResult on_vkEnumeratePhysicalDevices(void* context, VkResult input_result,
148 VkInstance instance, uint32_t* pPhysicalDeviceCount,
149 VkPhysicalDevice* pPhysicalDevices);
Lingfeng Yangb64ca452019-02-14 22:04:28 -0800150
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700151 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);
Yilong Lif3369082020-06-25 03:39:24 -0700161
Lingfeng Yang97a06702018-12-24 17:02:43 -0800162 void on_vkGetPhysicalDeviceMemoryProperties(
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700163 void* context, VkPhysicalDevice physicalDevice,
Lingfeng Yang97a06702018-12-24 17:02:43 -0800164 VkPhysicalDeviceMemoryProperties* pMemoryProperties);
Lingfeng Yang154a33c2019-01-29 19:06:23 -0800165 void on_vkGetPhysicalDeviceMemoryProperties2(
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700166 void* context, VkPhysicalDevice physicalDevice,
Lingfeng Yang154a33c2019-01-29 19:06:23 -0800167 VkPhysicalDeviceMemoryProperties2* pMemoryProperties);
168 void on_vkGetPhysicalDeviceMemoryProperties2KHR(
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700169 void* context, VkPhysicalDevice physicalDevice,
Lingfeng Yang154a33c2019-01-29 19:06:23 -0800170 VkPhysicalDeviceMemoryProperties2* pMemoryProperties);
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700171 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,
Yilong Li366c3662021-04-25 03:38:14 -0700174 VkQueue* pQueue);
Lingfeng Yangdef88ba2018-12-13 12:43:17 -0800175
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700176 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);
Lingfeng Yang131d5a42018-11-30 12:00:33 -0800185
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700186 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);
Lingfeng Yang131d5a42018-11-30 12:00:33 -0800191
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700192 VkResult on_vkMapMemory(void* context, VkResult input_result, VkDevice device,
193 VkDeviceMemory memory, VkDeviceSize offset, VkDeviceSize size,
194 VkMemoryMapFlags, void** ppData);
Lingfeng Yangdef88ba2018-12-13 12:43:17 -0800195
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700196 void on_vkUnmapMemory(void* context, VkDevice device, VkDeviceMemory memory);
Lingfeng Yangdef88ba2018-12-13 12:43:17 -0800197
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700198 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);
Lingfeng Yang4af5f322019-02-14 08:10:28 -0800203
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700204 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);
Aaron Rubya8140f42024-05-10 15:50:44 -0400212 void on_vkGetImageSubresourceLayout(void* context, VkDevice device, VkImage image,
213 const VkImageSubresource* pSubresource,
214 VkSubresourceLayout* pLayout);
Lingfeng Yang4af5f322019-02-14 08:10:28 -0800215
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700216 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);
Lingfeng Yang4af5f322019-02-14 08:10:28 -0800223
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700224 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);
Lingfeng Yang4af5f322019-02-14 08:10:28 -0800229
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700230 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);
Lingfeng Yang4af5f322019-02-14 08:10:28 -0800238
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700239 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);
Lingfeng Yang4af5f322019-02-14 08:10:28 -0800248
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700249 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);
David Reveman32b110e2019-02-21 13:20:54 -0500258
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700259 VkResult on_vkQueueSubmit(void* context, VkResult input_result, VkQueue queue,
260 uint32_t submitCount, const VkSubmitInfo* pSubmits, VkFence fence);
David Reveman24710222019-02-25 02:21:42 -0500261
Yahan Zhoue4a84382023-09-13 16:27:47 -0700262 VkResult on_vkQueueSubmit2(void* context, VkResult input_result, VkQueue queue,
263 uint32_t submitCount, const VkSubmitInfo2* pSubmits, VkFence fence);
264
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700265 VkResult on_vkQueueWaitIdle(void* context, VkResult input_result, VkQueue queue);
Lingfeng Yangcd2d8fe2019-08-16 12:21:50 -0700266
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700267 void unwrap_vkCreateImage_pCreateInfo(const VkImageCreateInfo* pCreateInfo,
268 VkImageCreateInfo* local_pCreateInfo);
Jason Macnake0ac2882023-06-07 09:13:51 -0700269
Lingfeng Yangdef88ba2018-12-13 12:43:17 -0800270 void unwrap_vkAcquireImageANDROID_nativeFenceFd(int fd, int* fd_out);
271
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700272 void unwrap_VkBindImageMemory2_pBindInfos(uint32_t bindInfoCount,
273 const VkBindImageMemoryInfo* inputBindInfos,
274 VkBindImageMemoryInfo* outputBindInfos);
Jason Macnake0ac2882023-06-07 09:13:51 -0700275
Gurchetan Singhc4444b82023-09-19 08:06:20 -0700276 VkResult on_vkGetMemoryFdKHR(void* context, VkResult input_result, VkDevice device,
277 const VkMemoryGetFdInfoKHR* pGetFdInfo, int* pFd);
Aaron Ruby16b349b2024-04-26 13:52:40 -0400278 VkResult on_vkGetMemoryFdPropertiesKHR(void* context, VkResult input_result, VkDevice device,
279 VkExternalMemoryHandleTypeFlagBits handleType, int fd,
280 VkMemoryFdPropertiesKHR* pMemoryFdProperties);
Gurchetan Singhc4444b82023-09-19 08:06:20 -0700281
David Reveman5b7c5842019-02-20 01:06:48 -0500282#ifdef VK_USE_PLATFORM_FUCHSIA
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700283 VkResult on_vkGetMemoryZirconHandleFUCHSIA(void* context, VkResult input_result,
284 VkDevice device,
285 const VkMemoryGetZirconHandleInfoFUCHSIA* pInfo,
286 uint32_t* pHandle);
David Reveman488704f2019-03-27 01:29:08 -0400287 VkResult on_vkGetMemoryZirconHandlePropertiesFUCHSIA(
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700288 void* context, VkResult input_result, VkDevice device,
289 VkExternalMemoryHandleTypeFlagBits handleType, uint32_t handle,
David Reveman488704f2019-03-27 01:29:08 -0400290 VkMemoryZirconHandlePropertiesFUCHSIA* pProperties);
David Revemanba51ee22019-03-26 11:22:21 -0400291 VkResult on_vkGetSemaphoreZirconHandleFUCHSIA(
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700292 void* context, VkResult input_result, VkDevice device,
293 const VkSemaphoreGetZirconHandleInfoFUCHSIA* pInfo, uint32_t* pHandle);
David Revemanba51ee22019-03-26 11:22:21 -0400294 VkResult on_vkImportSemaphoreZirconHandleFUCHSIA(
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700295 void* context, VkResult input_result, VkDevice device,
David Revemanba51ee22019-03-26 11:22:21 -0400296 const VkImportSemaphoreZirconHandleInfoFUCHSIA* pInfo);
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700297 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);
Yilong Lie12328f2022-01-06 03:32:13 -0800305 VkResult on_vkSetBufferCollectionBufferConstraintsFUCHSIA(
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700306 void* context, VkResult input_result, VkDevice device, VkBufferCollectionFUCHSIA collection,
Yilong Lie12328f2022-01-06 03:32:13 -0800307 const VkBufferConstraintsInfoFUCHSIA* pBufferConstraintsInfo);
308 VkResult on_vkSetBufferCollectionImageConstraintsFUCHSIA(
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700309 void* context, VkResult input_result, VkDevice device, VkBufferCollectionFUCHSIA collection,
Yilong Lie12328f2022-01-06 03:32:13 -0800310 const VkImageConstraintsInfoFUCHSIA* pImageConstraintsInfo);
311 VkResult on_vkGetBufferCollectionPropertiesFUCHSIA(
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700312 void* context, VkResult input_result, VkDevice device, VkBufferCollectionFUCHSIA collection,
Yilong Lie12328f2022-01-06 03:32:13 -0800313 VkBufferCollectionPropertiesFUCHSIA* pProperties);
C Stout5a3a4222023-11-14 16:31:56 -0800314
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
David Reveman5b7c5842019-02-20 01:06:48 -0500324#endif
325
Kaiyi Li6a76b332022-08-23 08:10:59 -0700326#ifdef VK_USE_PLATFORM_ANDROID_KHR
Lingfeng Yang4f3f91f2019-02-28 11:37:21 -0800327 VkResult on_vkGetAndroidHardwareBufferPropertiesANDROID(
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700328 void* context, VkResult input_result, VkDevice device, const AHardwareBuffer* buffer,
Lingfeng Yang4f3f91f2019-02-28 11:37:21 -0800329 VkAndroidHardwareBufferPropertiesANDROID* pProperties);
330 VkResult on_vkGetMemoryAndroidHardwareBufferANDROID(
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700331 void* context, VkResult input_result, VkDevice device,
332 const VkMemoryGetAndroidHardwareBufferInfoANDROID* pInfo, struct AHardwareBuffer** pBuffer);
Kaiyi Li6a76b332022-08-23 08:10:59 -0700333#endif
Lingfeng Yang4f3f91f2019-02-28 11:37:21 -0800334
Lingfeng Yang5c701122019-03-05 08:34:46 -0800335 VkResult on_vkCreateSamplerYcbcrConversion(
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700336 void* context, VkResult input_result, VkDevice device,
Lingfeng Yang5c701122019-03-05 08:34:46 -0800337 const VkSamplerYcbcrConversionCreateInfo* pCreateInfo,
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700338 const VkAllocationCallbacks* pAllocator, VkSamplerYcbcrConversion* pYcbcrConversion);
339 void on_vkDestroySamplerYcbcrConversion(void* context, VkDevice device,
340 VkSamplerYcbcrConversion ycbcrConversion,
341 const VkAllocationCallbacks* pAllocator);
Lingfeng Yang5c701122019-03-05 08:34:46 -0800342 VkResult on_vkCreateSamplerYcbcrConversionKHR(
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700343 void* context, VkResult input_result, VkDevice device,
Lingfeng Yang5c701122019-03-05 08:34:46 -0800344 const VkSamplerYcbcrConversionCreateInfo* pCreateInfo,
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700345 const VkAllocationCallbacks* pAllocator, VkSamplerYcbcrConversion* pYcbcrConversion);
346 void on_vkDestroySamplerYcbcrConversionKHR(void* context, VkDevice device,
347 VkSamplerYcbcrConversion ycbcrConversion,
348 const VkAllocationCallbacks* pAllocator);
Roman Kiryanov004f48c2019-06-28 14:59:32 -0700349
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700350 VkResult on_vkCreateSampler(void* context, VkResult input_result, VkDevice device,
351 const VkSamplerCreateInfo* pCreateInfo,
352 const VkAllocationCallbacks* pAllocator, VkSampler* pSampler);
Lingfeng Yang5c701122019-03-05 08:34:46 -0800353
Lingfeng Yangcd2d8fe2019-08-16 12:21:50 -0700354 void on_vkGetPhysicalDeviceExternalFenceProperties(
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700355 void* context, VkPhysicalDevice physicalDevice,
Lingfeng Yangcd2d8fe2019-08-16 12:21:50 -0700356 const VkPhysicalDeviceExternalFenceInfo* pExternalFenceInfo,
357 VkExternalFenceProperties* pExternalFenceProperties);
358
359 void on_vkGetPhysicalDeviceExternalFencePropertiesKHR(
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700360 void* context, VkPhysicalDevice physicalDevice,
Lingfeng Yangcd2d8fe2019-08-16 12:21:50 -0700361 const VkPhysicalDeviceExternalFenceInfo* pExternalFenceInfo,
362 VkExternalFenceProperties* pExternalFenceProperties);
363
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700364 VkResult on_vkCreateFence(void* context, VkResult input_result, VkDevice device,
365 const VkFenceCreateInfo* pCreateInfo,
366 const VkAllocationCallbacks* pAllocator, VkFence* pFence);
Lingfeng Yangcd2d8fe2019-08-16 12:21:50 -0700367
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700368 void on_vkDestroyFence(void* context, VkDevice device, VkFence fence,
369 const VkAllocationCallbacks* pAllocator);
Lingfeng Yangcd2d8fe2019-08-16 12:21:50 -0700370
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700371 VkResult on_vkResetFences(void* context, VkResult input_result, VkDevice device,
372 uint32_t fenceCount, const VkFence* pFences);
Lingfeng Yangcd2d8fe2019-08-16 12:21:50 -0700373
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700374 VkResult on_vkImportFenceFdKHR(void* context, VkResult input_result, VkDevice device,
375 const VkImportFenceFdInfoKHR* pImportFenceFdInfo);
Lingfeng Yangcd2d8fe2019-08-16 12:21:50 -0700376
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700377 VkResult on_vkGetFenceFdKHR(void* context, VkResult input_result, VkDevice device,
378 const VkFenceGetFdInfoKHR* pGetFdInfo, int* pFd);
Lingfeng Yangcd2d8fe2019-08-16 12:21:50 -0700379
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700380 VkResult on_vkWaitForFences(void* context, VkResult input_result, VkDevice device,
381 uint32_t fenceCount, const VkFence* pFences, VkBool32 waitAll,
382 uint64_t timeout);
Lingfeng Yangcd2d8fe2019-08-16 12:21:50 -0700383
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700384 VkResult on_vkCreateDescriptorPool(void* context, VkResult input_result, VkDevice device,
385 const VkDescriptorPoolCreateInfo* pCreateInfo,
386 const VkAllocationCallbacks* pAllocator,
387 VkDescriptorPool* pDescriptorPool);
Lingfeng Yange9e77d52020-03-25 14:01:58 -0700388
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700389 void on_vkDestroyDescriptorPool(void* context, VkDevice device, VkDescriptorPool descriptorPool,
390 const VkAllocationCallbacks* pAllocator);
Lingfeng Yange9e77d52020-03-25 14:01:58 -0700391
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700392 VkResult on_vkResetDescriptorPool(void* context, VkResult input_result, VkDevice device,
393 VkDescriptorPool descriptorPool,
394 VkDescriptorPoolResetFlags flags);
Lingfeng Yange9e77d52020-03-25 14:01:58 -0700395
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700396 VkResult on_vkAllocateDescriptorSets(void* context, VkResult input_result, VkDevice device,
397 const VkDescriptorSetAllocateInfo* pAllocateInfo,
398 VkDescriptorSet* pDescriptorSets);
Lingfeng Yange9e77d52020-03-25 14:01:58 -0700399
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700400 VkResult on_vkFreeDescriptorSets(void* context, VkResult input_result, VkDevice device,
401 VkDescriptorPool descriptorPool, uint32_t descriptorSetCount,
402 const VkDescriptorSet* pDescriptorSets);
Lingfeng Yange9e77d52020-03-25 14:01:58 -0700403
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700404 VkResult on_vkCreateDescriptorSetLayout(void* context, VkResult input_result, VkDevice device,
405 const VkDescriptorSetLayoutCreateInfo* pCreateInfo,
406 const VkAllocationCallbacks* pAllocator,
407 VkDescriptorSetLayout* pSetLayout);
Lingfeng Yang03354c72020-03-26 13:00:51 -0700408
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700409 void on_vkUpdateDescriptorSets(void* context, VkDevice device, uint32_t descriptorWriteCount,
410 const VkWriteDescriptorSet* pDescriptorWrites,
411 uint32_t descriptorCopyCount,
412 const VkCopyDescriptorSet* pDescriptorCopies);
Lingfeng Yang03354c72020-03-26 13:00:51 -0700413
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700414 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);
Lingfeng Yang236abc92018-12-21 20:19:33 -0800420
Lingfeng Yang05d5ea32019-03-23 00:12:39 -0700421 VkResult on_vkCreateDescriptorUpdateTemplate(
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700422 void* context, VkResult input_result, VkDevice device,
Lingfeng Yang05d5ea32019-03-23 00:12:39 -0700423 const VkDescriptorUpdateTemplateCreateInfo* pCreateInfo,
424 const VkAllocationCallbacks* pAllocator,
425 VkDescriptorUpdateTemplate* pDescriptorUpdateTemplate);
426
427 VkResult on_vkCreateDescriptorUpdateTemplateKHR(
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700428 void* context, VkResult input_result, VkDevice device,
Lingfeng Yang05d5ea32019-03-23 00:12:39 -0700429 const VkDescriptorUpdateTemplateCreateInfo* pCreateInfo,
430 const VkAllocationCallbacks* pAllocator,
431 VkDescriptorUpdateTemplate* pDescriptorUpdateTemplate);
432
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700433 void on_vkUpdateDescriptorSetWithTemplate(void* context, VkDevice device,
434 VkDescriptorSet descriptorSet,
435 VkDescriptorUpdateTemplate descriptorUpdateTemplate,
436 const void* pData);
Lingfeng Yang05d5ea32019-03-23 00:12:39 -0700437
Jean-Francois Thibertf884afd2024-01-31 10:15:04 -0500438 void on_vkUpdateDescriptorSetWithTemplateKHR(void* context, VkDevice device,
439 VkDescriptorSet descriptorSet,
440 VkDescriptorUpdateTemplate descriptorUpdateTemplate,
441 const void* pData);
442
Lingfeng Yange517d1e2019-03-25 18:02:49 -0700443 VkResult on_vkGetPhysicalDeviceImageFormatProperties2(
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700444 void* context, VkResult input_result, VkPhysicalDevice physicalDevice,
Lingfeng Yange517d1e2019-03-25 18:02:49 -0700445 const VkPhysicalDeviceImageFormatInfo2* pImageFormatInfo,
446 VkImageFormatProperties2* pImageFormatProperties);
447
448 VkResult on_vkGetPhysicalDeviceImageFormatProperties2KHR(
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700449 void* context, VkResult input_result, VkPhysicalDevice physicalDevice,
Lingfeng Yange517d1e2019-03-25 18:02:49 -0700450 const VkPhysicalDeviceImageFormatInfo2* pImageFormatInfo,
451 VkImageFormatProperties2* pImageFormatProperties);
452
Andrew Woloszyn416d0a12023-10-04 17:02:19 -0400453 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
Yilong Lid1c69192021-02-05 02:06:07 -0800463 void on_vkGetPhysicalDeviceExternalSemaphoreProperties(
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700464 void* context, VkPhysicalDevice physicalDevice,
Yilong Lid1c69192021-02-05 02:06:07 -0800465 const VkPhysicalDeviceExternalSemaphoreInfo* pExternalSemaphoreInfo,
466 VkExternalSemaphoreProperties* pExternalSemaphoreProperties);
467
468 void on_vkGetPhysicalDeviceExternalSemaphorePropertiesKHR(
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700469 void* context, VkPhysicalDevice physicalDevice,
Yilong Lid1c69192021-02-05 02:06:07 -0800470 const VkPhysicalDeviceExternalSemaphoreInfo* pExternalSemaphoreInfo,
471 VkExternalSemaphoreProperties* pExternalSemaphoreProperties);
472
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700473 void registerEncoderCleanupCallback(const VkEncoder* encoder, void* handle,
474 CleanupCallback callback);
Lingfeng Yang256f9252020-07-14 14:27:33 -0700475 void unregisterEncoderCleanupCallback(const VkEncoder* encoder, void* handle);
476 void onEncoderDeleted(const VkEncoder* encoder);
477
Lingfeng Yang39a276e2019-06-17 13:27:22 -0700478 uint32_t syncEncodersForCommandBuffer(VkCommandBuffer commandBuffer, VkEncoder* current);
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700479 uint32_t syncEncodersForQueue(VkQueue queue, VkEncoder* currentEncoder);
Lingfeng Yang39a276e2019-06-17 13:27:22 -0700480
Shalini Sdb704c92023-01-27 21:35:33 +0000481 CommandBufferStagingStream::Alloc getAlloc();
482 CommandBufferStagingStream::Free getFree();
483
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700484 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);
Lingfeng Yangc53e7472019-03-27 08:50:55 -0700492
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700493 VkResult on_vkCreateImageView(void* context, VkResult input_result, VkDevice device,
494 const VkImageViewCreateInfo* pCreateInfo,
495 const VkAllocationCallbacks* pAllocator, VkImageView* pView);
Roman Kiryanov6db11e52019-04-26 14:18:14 -0700496
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700497 void on_vkCmdExecuteCommands(void* context, VkCommandBuffer commandBuffer,
498 uint32_t commandBufferCount,
499 const VkCommandBuffer* pCommandBuffers);
Lingfeng Yang967f9af2021-01-22 17:56:24 -0800500
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700501 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);
Lingfeng Yang55676e02021-02-08 08:39:45 -0800506
Jason Macnake6704de2022-05-27 09:41:20 -0700507 void on_vkCmdPipelineBarrier(
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700508 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);
Jason Macnake6704de2022-05-27 09:41:20 -0700513
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700514 void on_vkDestroyDescriptorSetLayout(void* context, VkDevice device,
515 VkDescriptorSetLayout descriptorSetLayout,
516 const VkAllocationCallbacks* pAllocator);
Lingfeng Yang55676e02021-02-08 08:39:45 -0800517
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700518 VkResult on_vkAllocateCommandBuffers(void* context, VkResult input_result, VkDevice device,
519 const VkCommandBufferAllocateInfo* pAllocateInfo,
520 VkCommandBuffer* pCommandBuffers);
Lingfeng Yang97f51af2021-04-22 12:24:11 -0700521
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700522 VkResult on_vkQueueSignalReleaseImageANDROID(void* context, VkResult input_result,
523 VkQueue queue, uint32_t waitSemaphoreCount,
524 const VkSemaphore* pWaitSemaphores, VkImage image,
525 int* pNativeFenceFd);
Lingfeng Yang084e5342021-07-15 13:06:43 -0700526
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700527 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);
Yahan Zhou483ac022022-06-13 15:41:11 -0700532
Lingfeng Yangdef88ba2018-12-13 12:43:17 -0800533 uint8_t* getMappedPointer(VkDeviceMemory memory);
534 VkDeviceSize getMappedSize(VkDeviceMemory memory);
Lingfeng Yang6ab1b0d2018-11-27 23:36:03 -0800535 VkDeviceSize getNonCoherentExtendedSize(VkDevice device, VkDeviceSize basicSize) const;
Lingfeng Yangdef88ba2018-12-13 12:43:17 -0800536 bool isValidMemoryRange(const VkMappedMemoryRange& range) const;
Gurchetan Singh5bb6c602023-04-04 02:53:32 +0000537
Lingfeng Yang31754632018-12-21 18:24:55 -0800538 void setupFeatures(const EmulatorFeatureInfo* features);
Gurchetan Singh99288832023-09-07 09:40:10 -0700539 void setupCaps(uint32_t& noRenderControlEnc);
Gurchetan Singh5bb6c602023-04-04 02:53:32 +0000540
Lingfeng Yang34b5cae2019-08-21 14:12:19 -0700541 void setThreadingCallbacks(const ThreadingCallbacks& callbacks);
Lingfeng Yangb8a38c72019-02-02 20:27:54 -0800542 bool hostSupportsVulkan() const;
Lingfeng Yang236abc92018-12-21 20:19:33 -0800543 bool usingDirectMapping() const;
Lingfeng Yangb55ed1c2019-06-20 15:57:08 -0700544 uint32_t getStreamFeatures() const;
Lingfeng Yang2b1b8cf2019-02-08 09:53:36 -0800545 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;
Yahan Zhou97f37fa2022-04-28 21:57:17 +0000549 VkDevice getDevice(VkCommandBuffer commandBuffer) const;
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700550 void addToCommandPool(VkCommandPool commandPool, uint32_t commandBufferCount,
Lingfeng Yang967f9af2021-01-22 17:56:24 -0800551 VkCommandBuffer* pCommandBuffers);
552 void resetCommandPoolStagingInfo(VkCommandPool commandPool);
553
Gurchetan Singhb7be3162021-11-04 09:36:15 -0700554#ifdef __GNUC__
Gurchetan Singhc4444b82023-09-19 08:06:20 -0700555#define ALWAYS_INLINE_GFXSTREAM
Gurchetan Singhb7be3162021-11-04 09:36:15 -0700556#elif
Gurchetan Singhc4444b82023-09-19 08:06:20 -0700557#define ALWAYS_INLINE_GFXSTREAM __attribute__((always_inline))
Gurchetan Singhb7be3162021-11-04 09:36:15 -0700558#endif
Lingfeng Yang967f9af2021-01-22 17:56:24 -0800559
560 static VkEncoder* getCommandBufferEncoder(VkCommandBuffer commandBuffer);
561 static VkEncoder* getQueueEncoder(VkQueue queue);
562 static VkEncoder* getThreadLocalEncoder();
Lingfeng Yang2b1b8cf2019-02-08 09:53:36 -0800563
Lingfeng Yangdb616552021-01-22 17:58:02 -0800564 static void setSeqnoPtr(uint32_t* seqnoptr);
Gurchetan Singhc4444b82023-09-19 08:06:20 -0700565 static ALWAYS_INLINE_GFXSTREAM uint32_t nextSeqno();
566 static ALWAYS_INLINE_GFXSTREAM uint32_t getSeqno();
Lingfeng Yangdb616552021-01-22 17:58:02 -0800567
Lingfeng Yang2b1b8cf2019-02-08 09:53:36 -0800568 // Transforms
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700569 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);
Lingfeng Yangdef88ba2018-12-13 12:43:17 -0800579
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700580 void transformImpl_VkExternalMemoryProperties_fromhost(VkExternalMemoryProperties* pProperties,
581 uint32_t);
582 void transformImpl_VkExternalMemoryProperties_tohost(VkExternalMemoryProperties* pProperties,
583 uint32_t);
Yilong Li353409a2022-01-04 02:37:56 -0800584 void transformImpl_VkImageCreateInfo_fromhost(const VkImageCreateInfo*, uint32_t);
585 void transformImpl_VkImageCreateInfo_tohost(const VkImageCreateInfo*, uint32_t);
Lingfeng Yang4af5f322019-02-14 08:10:28 -0800586
Yilong Lidbc16d52021-02-04 03:15:21 -0800587#define DEFINE_TRANSFORMED_TYPE_PROTOTYPE(type) \
588 void transformImpl_##type##_tohost(type*, uint32_t); \
589 void transformImpl_##type##_fromhost(type*, uint32_t);
Lingfeng Yangf0654ff2019-02-02 12:21:24 -0800590
Yilong Lid1c69192021-02-05 02:06:07 -0800591 LIST_TRIVIAL_TRANSFORMED_TYPES(DEFINE_TRANSFORMED_TYPE_PROTOTYPE)
Yilong Lidbc16d52021-02-04 03:15:21 -0800592
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700593 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
Andrew Woloszyn416d0a12023-10-04 17:02:19 -0400606 void on_vkGetPhysicalDeviceExternalBufferProperties_common(
607 bool isKhr, void* context, VkPhysicalDevice physicalDevice,
608 const VkPhysicalDeviceExternalBufferInfo* pExternalBufferInfo,
609 VkExternalBufferProperties* pExternalBufferProperties);
610
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700611 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,
Jason Macnak6d3d7b22024-04-01 16:48:53 -0700628 bool imported, zx_handle_t vmoHandle, VirtGpuResourcePtr blobPtr);
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700629
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
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700639 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
C Stout5a3a4222023-11-14 16:31:56 -0800694#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
Jason Macnakeea882a2023-11-15 17:29:11 -0800709
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;
Gurchetan Singhc4444b82023-09-19 08:06:20 -0700731 std::vector<std::pair<PFN_vkDeviceMemoryReportCallbackEXT, void*>>
732 deviceMemoryReportCallbacks;
Jason Macnakeea882a2023-11-15 17:29:11 -0800733 };
734
735 struct VkDeviceMemory_Info {
736 bool dedicated = false;
737 bool imported = false;
738
Gurchetan Singhc4444b82023-09-19 08:06:20 -0700739#ifdef VK_USE_PLATFORM_ANDROID_KHR
Jason Macnakeea882a2023-11-15 17:29:11 -0800740 AHardwareBuffer* ahw = nullptr;
Gurchetan Singhc4444b82023-09-19 08:06:20 -0700741#endif
Jason Macnakeea882a2023-11-15 17:29:11 -0800742 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
Gurchetan Singhc4444b82023-09-19 08:06:20 -0700753#if defined(__ANDROID__)
Jason Macnakeea882a2023-11-15 17:29:11 -0800754 GoldfishAddressSpaceBlockPtr goldfishBlock = nullptr;
Gurchetan Singhc4444b82023-09-19 08:06:20 -0700755#endif // defined(__ANDROID__)
Jason Macnakeea882a2023-11-15 17:29:11 -0800756 CoherentMemoryPtr coherentMemory = nullptr;
Jason Macnak6d3d7b22024-04-01 16:48:53 -0700757 VirtGpuResourcePtr blobPtr = nullptr;
Jason Macnakeea882a2023-11-15 17:29:11 -0800758 };
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;
Gurchetan Singhc4444b82023-09-19 08:06:20 -0700779#ifdef VK_USE_PLATFORM_ANDROID_KHR
Jason Macnakeea882a2023-11-15 17:29:11 -0800780 bool hasExternalFormat = false;
Sergiuad918472024-05-21 16:28:45 +0100781 unsigned externalFourccFormat = 0;
Jason Macnakeea882a2023-11-15 17:29:11 -0800782 std::vector<int> pendingQsriSyncFds;
Gurchetan Singhc4444b82023-09-19 08:06:20 -0700783#endif
784#ifdef VK_USE_PLATFORM_FUCHSIA
Jason Macnakeea882a2023-11-15 17:29:11 -0800785 bool isSysmemBackedMemory = false;
Gurchetan Singhc4444b82023-09-19 08:06:20 -0700786#endif
Aaron Ruby42a8b752024-05-27 15:37:53 -0400787#ifdef LINUX_GUEST_BUILD
788 bool isDmaBufImage = false;
Aaron Rubya8140f42024-05-10 15:50:44 -0400789 VkImage linearPeerImage = VK_NULL_HANDLE;
Aaron Ruby42a8b752024-05-27 15:37:53 -0400790#endif
Jason Macnakeea882a2023-11-15 17:29:11 -0800791 };
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;
Gurchetan Singhc4444b82023-09-19 08:06:20 -0700803#ifdef VK_USE_PLATFORM_FUCHSIA
Jason Macnakeea882a2023-11-15 17:29:11 -0800804 bool isSysmemBackedMemory = false;
Gurchetan Singhc4444b82023-09-19 08:06:20 -0700805#endif
Jason Macnakeea882a2023-11-15 17:29:11 -0800806 };
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;
Gurchetan Singhc4444b82023-09-19 08:06:20 -0700837#if defined(VK_USE_PLATFORM_ANDROID_KHR) || defined(__linux__)
Jason Macnakeea882a2023-11-15 17:29:11 -0800838 int syncFd = -1;
Gurchetan Singhc4444b82023-09-19 08:06:20 -0700839#endif
Jason Macnakeea882a2023-11-15 17:29:11 -0800840 };
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 {
Gurchetan Singhc4444b82023-09-19 08:06:20 -0700863#ifdef VK_USE_PLATFORM_FUCHSIA
Jason Macnakeea882a2023-11-15 17:29:11 -0800864 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;
Gurchetan Singhc4444b82023-09-19 08:06:20 -0700870#endif // VK_USE_PLATFORM_FUCHSIA
Jason Macnakeea882a2023-11-15 17:29:11 -0800871 };
872
873 VkDescriptorImageInfo filterNonexistentSampler(const VkDescriptorImageInfo& inputInfo);
874
Gurchetan Singhc4444b82023-09-19 08:06:20 -0700875 void emitDeviceMemoryReport(VkDevice_Info info, VkDeviceMemoryReportEventTypeEXT type,
876 uint64_t memoryObjectId, VkDeviceSize size, VkObjectType objectType,
877 uint64_t objectHandle, uint32_t heapIndex = 0);
Jason Macnakeea882a2023-11-15 17:29:11 -0800878
Gurchetan Singhc4444b82023-09-19 08:06:20 -0700879 void transformImageMemoryRequirementsForGuestLocked(VkImage image, VkMemoryRequirements* reqs);
Jason Macnakeea882a2023-11-15 17:29:11 -0800880 CoherentMemoryPtr freeCoherentMemoryLocked(VkDeviceMemory memory, VkDeviceMemory_Info& info);
881
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700882 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
Gurchetan Singhc4444b82023-09-19 08:06:20 -0700905#define HANDLE_REGISTER_DECLARATION(type) std::unordered_map<type, type##_Info> info_##type;
Jason Macnakeea882a2023-11-15 17:29:11 -0800906
Gurchetan Singhc4444b82023-09-19 08:06:20 -0700907 GOLDFISH_VK_LIST_HANDLE_TYPES(HANDLE_REGISTER_DECLARATION)
Jason Macnakeea882a2023-11-15 17:29:11 -0800908
Gurchetan Singhfa4dfda2023-09-18 17:11:24 -0700909 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;
Lingfeng Yang71b596b2018-11-07 18:03:25 -0800915};
Lingfeng Yang71b596b2018-11-07 18:03:25 -0800916
Jason Macnak3d664002023-03-30 16:00:50 -0700917} // namespace vk
918} // namespace gfxstream