• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #pragma once
15 
16 #include <vulkan/vulkan.h>
17 
18 #include <memory>
19 #include <optional>
20 #include <unordered_map>
21 #include <utility>
22 #include <vector>
23 
24 #include "VkDecoderContext.h"
25 #include "VkQsriTimeline.h"
26 #include "VulkanDispatch.h"
27 #include "VulkanHandleMapping.h"
28 #include "aemu/base/AsyncResult.h"
29 #include "aemu/base/HealthMonitor.h"
30 #include "aemu/base/synchronization/Lock.h"
31 #include "cereal/common/goldfish_vk_private_defs.h"
32 #include "cereal/common/goldfish_vk_transform.h"
33 #include "host-common/GfxstreamFatalError.h"
34 #include "utils/GfxApiLogger.h"
35 #include "vk_util.h"
36 
37 using android::base::AutoLock;
38 using android::base::Lock;
39 using emugl::ABORT_REASON_OTHER;
40 using emugl::FatalError;
41 using emugl::HealthMonitor;
42 
43 class VkDecoderSnapshot;
44 
45 namespace android {
46 namespace base {
47 class BumpPool;
48 class Stream;
49 }  // namespace base
50 }  // namespace android
51 
52 namespace gfxstream {
53 namespace vk {
54 
55 // Class for tracking host-side state. Currently we only care about
56 // tracking VkDeviceMemory to make it easier to pass the right data
57 // from mapped pointers to the guest, but this could get more stuff
58 // added to it if for instance, we want to only expose a certain set
59 // of physical device capabilities, or do snapshots.
60 
61 // This class may be autogenerated in the future.
62 // Currently, it works by interfacing with VkDecoder calling on_<apicall>
63 // functions.
64 class VkDecoderGlobalState {
65    public:
66     VkDecoderGlobalState();
67     ~VkDecoderGlobalState();
68 
69     // There should only be one instance of VkDecoderGlobalState per process
70     // Note: currently not thread-safe
71     static VkDecoderGlobalState* get();
72 
73     // For testing only - destroys the global instance of VkDecoderGlobalState.
74     static void reset();
75 
76     // Snapshot save/load
77     bool snapshotsEnabled() const;
78 
79     // Whether to clean up VK instance.
80     // bug 149997534
81     bool vkCleanupEnabled() const;
82 
83     void save(android::base::Stream* stream);
84     void load(android::base::Stream* stream, emugl::GfxApiLogger& gfxLogger,
85               emugl::HealthMonitor<>* healthMonitor);
86 
87     // Lock/unlock of global state to serve as a global lock
88     void lock();
89     void unlock();
90 
91     // Sets the current created handles for snapshot load
92     // which will override the effect of any new_boxed_*** calls.
93     // They are consumed in sequence.
94     // Returns number of bytes of |buffer|
95     // that have been consumed for this purpose.
96     size_t setCreatedHandlesForSnapshotLoad(const unsigned char* buffer);
97     void clearCreatedHandlesForSnapshotLoad();
98 
99     VkResult on_vkEnumerateInstanceVersion(android::base::BumpPool* pool, uint32_t* pApiVersion);
100 
101     // Fast way to get dispatch tables associated with a Vulkan object.
102     // VkInstance
103     // VkPhysicalDevice
104     // VkDevice
105     // VkQueue
106     // VkCommandBuffer
107 
108     VkResult on_vkCreateInstance(android::base::BumpPool* pool,
109                                  const VkInstanceCreateInfo* pCreateInfo,
110                                  const VkAllocationCallbacks* pAllocator, VkInstance* pInstance);
111 
112     void on_vkDestroyInstance(android::base::BumpPool* pool, VkInstance instance,
113                               const VkAllocationCallbacks* pAllocator);
114 
115     VkResult on_vkEnumeratePhysicalDevices(android::base::BumpPool* pool, VkInstance instance,
116                                            uint32_t* physicalDeviceCount,
117                                            VkPhysicalDevice* physicalDevices);
118 
119     // Override features
120     void on_vkGetPhysicalDeviceFeatures(android::base::BumpPool* pool,
121                                         VkPhysicalDevice physicalDevice,
122                                         VkPhysicalDeviceFeatures* pFeatures);
123     void on_vkGetPhysicalDeviceFeatures2(android::base::BumpPool* pool,
124                                          VkPhysicalDevice physicalDevice,
125                                          VkPhysicalDeviceFeatures2* pFeatures);
126     void on_vkGetPhysicalDeviceFeatures2KHR(android::base::BumpPool* pool,
127                                             VkPhysicalDevice physicalDevice,
128                                             VkPhysicalDeviceFeatures2* pFeatures);
129 
130     // Override image format properties
131     VkResult on_vkGetPhysicalDeviceImageFormatProperties(
132         android::base::BumpPool* pool, VkPhysicalDevice physicalDevice, VkFormat format,
133         VkImageType type, VkImageTiling tiling, VkImageUsageFlags usage, VkImageCreateFlags flags,
134         VkImageFormatProperties* pImageFormatProperties);
135     VkResult on_vkGetPhysicalDeviceImageFormatProperties2(
136         android::base::BumpPool* pool, VkPhysicalDevice physicalDevice,
137         const VkPhysicalDeviceImageFormatInfo2* pImageFormatInfo,
138         VkImageFormatProperties2* pImageFormatProperties);
139     VkResult on_vkGetPhysicalDeviceImageFormatProperties2KHR(
140         android::base::BumpPool* pool, VkPhysicalDevice physicalDevice,
141         const VkPhysicalDeviceImageFormatInfo2* pImageFormatInfo,
142         VkImageFormatProperties2* pImageFormatProperties);
143 
144     // Override format features
145     void on_vkGetPhysicalDeviceFormatProperties(android::base::BumpPool* pool,
146                                                 VkPhysicalDevice physicalDevice, VkFormat format,
147                                                 VkFormatProperties* pFormatProperties);
148     void on_vkGetPhysicalDeviceFormatProperties2(android::base::BumpPool* pool,
149                                                  VkPhysicalDevice physicalDevice, VkFormat format,
150                                                  VkFormatProperties2* pFormatProperties);
151     void on_vkGetPhysicalDeviceFormatProperties2KHR(android::base::BumpPool* pool,
152                                                     VkPhysicalDevice physicalDevice,
153                                                     VkFormat format,
154                                                     VkFormatProperties2* pFormatProperties);
155 
156     // Override API version
157     void on_vkGetPhysicalDeviceProperties(android::base::BumpPool* pool,
158                                           VkPhysicalDevice physicalDevice,
159                                           VkPhysicalDeviceProperties* pProperties);
160     void on_vkGetPhysicalDeviceProperties2(android::base::BumpPool* pool,
161                                            VkPhysicalDevice physicalDevice,
162                                            VkPhysicalDeviceProperties2* pProperties);
163     void on_vkGetPhysicalDeviceProperties2KHR(android::base::BumpPool* pool,
164                                               VkPhysicalDevice physicalDevice,
165                                               VkPhysicalDeviceProperties2* pProperties);
166 
167     // Override memory types advertised from host
168     //
169     void on_vkGetPhysicalDeviceMemoryProperties(
170         android::base::BumpPool* pool, VkPhysicalDevice physicalDevice,
171         VkPhysicalDeviceMemoryProperties* pMemoryProperties);
172     void on_vkGetPhysicalDeviceMemoryProperties2(
173         android::base::BumpPool* pool, VkPhysicalDevice physicalDevice,
174         VkPhysicalDeviceMemoryProperties2* pMemoryProperties);
175     void on_vkGetPhysicalDeviceMemoryProperties2KHR(
176         android::base::BumpPool* pool, VkPhysicalDevice physicalDevice,
177         VkPhysicalDeviceMemoryProperties2* pMemoryProperties);
178 
179     // Override supported device extension lists
180     VkResult on_vkEnumerateDeviceExtensionProperties(android::base::BumpPool* pool,
181                                                      VkPhysicalDevice physicalDevice,
182                                                      const char* pLayerName,
183                                                      uint32_t* pPropertyCount,
184                                                      VkExtensionProperties* pProperties);
185 
186     VkResult on_vkCreateDevice(android::base::BumpPool* pool, VkPhysicalDevice physicalDevice,
187                                const VkDeviceCreateInfo* pCreateInfo,
188                                const VkAllocationCallbacks* pAllocator, VkDevice* pDevice);
189 
190     void on_vkGetDeviceQueue(android::base::BumpPool* pool, VkDevice device,
191                              uint32_t queueFamilyIndex, uint32_t queueIndex, VkQueue* pQueue);
192 
193     void on_vkGetDeviceQueue2(android::base::BumpPool* pool, VkDevice device,
194                               const VkDeviceQueueInfo2* pQueueInfo, VkQueue* pQueue);
195 
196     void on_vkDestroyDevice(android::base::BumpPool* pool, VkDevice device,
197                             const VkAllocationCallbacks* pAllocator);
198 
199     VkResult on_vkCreateBuffer(android::base::BumpPool* pool, VkDevice device,
200                                const VkBufferCreateInfo* pCreateInfo,
201                                const VkAllocationCallbacks* pAllocator, VkBuffer* pBuffer);
202 
203     void on_vkDestroyBuffer(android::base::BumpPool* pool, VkDevice device, VkBuffer buffer,
204                             const VkAllocationCallbacks* pAllocator);
205 
206     VkResult on_vkBindBufferMemory(android::base::BumpPool* pool, VkDevice device, VkBuffer buffer,
207                                    VkDeviceMemory memory, VkDeviceSize memoryOffset);
208     VkResult on_vkBindBufferMemory2(android::base::BumpPool* pool, VkDevice device,
209                                     uint32_t bindInfoCount,
210                                     const VkBindBufferMemoryInfo* pBindInfos);
211     VkResult on_vkBindBufferMemory2KHR(android::base::BumpPool* pool, VkDevice device,
212                                        uint32_t bindInfoCount,
213                                        const VkBindBufferMemoryInfo* pBindInfos);
214 
215     VkResult on_vkCreateImage(android::base::BumpPool* pool, VkDevice device,
216                               const VkImageCreateInfo* pCreateInfo,
217                               const VkAllocationCallbacks* pAllocator, VkImage* pImage);
218 
219     void on_vkDestroyImage(android::base::BumpPool* pool, VkDevice device, VkImage image,
220                            const VkAllocationCallbacks* pAllocator);
221 
222     VkResult on_vkBindImageMemory(android::base::BumpPool* pool, VkDevice device, VkImage image,
223                                   VkDeviceMemory memory, VkDeviceSize memoryOffset);
224     VkResult on_vkBindImageMemory2(android::base::BumpPool* pool, VkDevice device,
225                                    uint32_t bindInfoCount, const VkBindImageMemoryInfo* pBindInfos);
226     VkResult on_vkBindImageMemory2KHR(android::base::BumpPool* pool, VkDevice device,
227                                       uint32_t bindInfoCount,
228                                       const VkBindImageMemoryInfo* pBindInfos);
229 
230     VkResult on_vkCreateImageView(android::base::BumpPool* pool, VkDevice device,
231                                   const VkImageViewCreateInfo* pCreateInfo,
232                                   const VkAllocationCallbacks* pAllocator, VkImageView* pView);
233 
234     void on_vkDestroyImageView(android::base::BumpPool* pool, VkDevice device,
235                                VkImageView imageView, const VkAllocationCallbacks* pAllocator);
236 
237     VkResult on_vkCreateSampler(android::base::BumpPool* pool, VkDevice device,
238                                 const VkSamplerCreateInfo* pCreateInfo,
239                                 const VkAllocationCallbacks* pAllocator, VkSampler* pSampler);
240 
241     void on_vkDestroySampler(android::base::BumpPool* pool, VkDevice device, VkSampler sampler,
242                              const VkAllocationCallbacks* pAllocator);
243 
244     VkResult on_vkCreateDescriptorSetLayout(android::base::BumpPool* pool, VkDevice device,
245                                             const VkDescriptorSetLayoutCreateInfo* pCreateInfo,
246                                             const VkAllocationCallbacks* pAllocator,
247                                             VkDescriptorSetLayout* pSetLayout);
248 
249     void on_vkDestroyDescriptorSetLayout(android::base::BumpPool* pool, VkDevice device,
250                                          VkDescriptorSetLayout descriptorSetLayout,
251                                          const VkAllocationCallbacks* pAllocator);
252 
253     VkResult on_vkCreateDescriptorPool(android::base::BumpPool* pool, VkDevice device,
254                                        const VkDescriptorPoolCreateInfo* pCreateInfo,
255                                        const VkAllocationCallbacks* pAllocator,
256                                        VkDescriptorPool* pDescriptorPool);
257 
258     void on_vkDestroyDescriptorPool(android::base::BumpPool* pool, VkDevice device,
259                                     VkDescriptorPool descriptorPool,
260                                     const VkAllocationCallbacks* pAllocator);
261 
262     VkResult on_vkResetDescriptorPool(android::base::BumpPool* pool, VkDevice device,
263                                       VkDescriptorPool descriptorPool,
264                                       VkDescriptorPoolResetFlags flags);
265 
266     VkResult on_vkAllocateDescriptorSets(android::base::BumpPool* pool, VkDevice device,
267                                          const VkDescriptorSetAllocateInfo* pAllocateInfo,
268                                          VkDescriptorSet* pDescriptorSets);
269 
270     VkResult on_vkFreeDescriptorSets(android::base::BumpPool* pool, VkDevice device,
271                                      VkDescriptorPool descriptorPool, uint32_t descriptorSetCount,
272                                      const VkDescriptorSet* pDescriptorSets);
273 
274     void on_vkUpdateDescriptorSets(android::base::BumpPool* pool, VkDevice device,
275                                    uint32_t descriptorWriteCount,
276                                    const VkWriteDescriptorSet* pDescriptorWrites,
277                                    uint32_t descriptorCopyCount,
278                                    const VkCopyDescriptorSet* pDescriptorCopies);
279 
280     VkResult on_vkCreateShaderModule(android::base::BumpPool* pool, VkDevice device,
281                                      const VkShaderModuleCreateInfo* pCreateInfo,
282                                      const VkAllocationCallbacks* pAllocator,
283                                      VkShaderModule* pShaderModule);
284 
285     void on_vkDestroyShaderModule(android::base::BumpPool* pool, VkDevice device,
286                                   VkShaderModule shaderModule,
287                                   const VkAllocationCallbacks* pAllocator);
288 
289     VkResult on_vkCreatePipelineCache(android::base::BumpPool* pool, VkDevice device,
290                                       const VkPipelineCacheCreateInfo* pCreateInfo,
291                                       const VkAllocationCallbacks* pAllocator,
292                                       VkPipelineCache* pPipelineCache);
293 
294     void on_vkDestroyPipelineCache(android::base::BumpPool* pool, VkDevice device,
295                                    VkPipelineCache pipelineCache,
296                                    const VkAllocationCallbacks* pAllocator);
297 
298     VkResult on_vkCreateGraphicsPipelines(android::base::BumpPool* pool, VkDevice device,
299                                           VkPipelineCache pipelineCache, uint32_t createInfoCount,
300                                           const VkGraphicsPipelineCreateInfo* pCreateInfos,
301                                           const VkAllocationCallbacks* pAllocator,
302                                           VkPipeline* pPipelines);
303 
304     void on_vkDestroyPipeline(android::base::BumpPool* pool, VkDevice device, VkPipeline pipeline,
305                               const VkAllocationCallbacks* pAllocator);
306 
307     void on_vkCmdCopyBufferToImage(android::base::BumpPool* pool, VkCommandBuffer commandBuffer,
308                                    VkBuffer srcBuffer, VkImage dstImage,
309                                    VkImageLayout dstImageLayout, uint32_t regionCount,
310                                    const VkBufferImageCopy* pRegions,
311                                    const VkDecoderContext& context);
312 
313     void on_vkCmdCopyImage(android::base::BumpPool* pool, VkCommandBuffer commandBuffer,
314                            VkImage srcImage, VkImageLayout srcImageLayout, VkImage dstImage,
315                            VkImageLayout dstImageLayout, uint32_t regionCount,
316                            const VkImageCopy* pRegions);
317     void on_vkCmdCopyImageToBuffer(android::base::BumpPool* pool, VkCommandBuffer commandBuffer,
318                                    VkImage srcImage, VkImageLayout srcImageLayout,
319                                    VkBuffer dstBuffer, uint32_t regionCount,
320                                    const VkBufferImageCopy* pRegions);
321 
322     void on_vkGetImageMemoryRequirements(android::base::BumpPool* pool, VkDevice device,
323                                          VkImage image, VkMemoryRequirements* pMemoryRequirements);
324 
325     void on_vkGetImageMemoryRequirements2(android::base::BumpPool* pool, VkDevice device,
326                                           const VkImageMemoryRequirementsInfo2* pInfo,
327                                           VkMemoryRequirements2* pMemoryRequirements);
328 
329     void on_vkGetImageMemoryRequirements2KHR(android::base::BumpPool* pool, VkDevice device,
330                                              const VkImageMemoryRequirementsInfo2* pInfo,
331                                              VkMemoryRequirements2* pMemoryRequirements);
332 
333     void on_vkGetBufferMemoryRequirements(android::base::BumpPool* pool, VkDevice device,
334                                           VkBuffer buffer,
335                                           VkMemoryRequirements* pMemoryRequirements);
336 
337     void on_vkGetBufferMemoryRequirements2(android::base::BumpPool* pool, VkDevice device,
338                                            const VkBufferMemoryRequirementsInfo2* pInfo,
339                                            VkMemoryRequirements2* pMemoryRequirements);
340 
341     void on_vkGetBufferMemoryRequirements2KHR(android::base::BumpPool* pool, VkDevice device,
342                                               const VkBufferMemoryRequirementsInfo2* pInfo,
343                                               VkMemoryRequirements2* pMemoryRequirements);
344 
345     void on_vkCmdPipelineBarrier(android::base::BumpPool* pool, VkCommandBuffer commandBuffer,
346                                  VkPipelineStageFlags srcStageMask,
347                                  VkPipelineStageFlags dstStageMask,
348                                  VkDependencyFlags dependencyFlags, uint32_t memoryBarrierCount,
349                                  const VkMemoryBarrier* pMemoryBarriers,
350                                  uint32_t bufferMemoryBarrierCount,
351                                  const VkBufferMemoryBarrier* pBufferMemoryBarriers,
352                                  uint32_t imageMemoryBarrierCount,
353                                  const VkImageMemoryBarrier* pImageMemoryBarriers);
354 
355     // Do we need to wrap vk(Create|Destroy)Instance to
356     // update our maps of VkDevices? Spec suggests no:
357     // https://www.khronos.org/registry/vulkan/specs/1.1-extensions/man/html/vkDestroyInstance.html
358     // Valid Usage
359     // All child objects created using instance
360     // must have been destroyed prior to destroying instance
361     //
362     // This suggests that we should emulate the invalid behavior by
363     // not destroying our own VkDevice maps on instance destruction.
364 
365     VkResult on_vkAllocateMemory(android::base::BumpPool* pool, VkDevice device,
366                                  const VkMemoryAllocateInfo* pAllocateInfo,
367                                  const VkAllocationCallbacks* pAllocator, VkDeviceMemory* pMemory);
368 
369     void on_vkFreeMemory(android::base::BumpPool* pool, VkDevice device, VkDeviceMemory memory,
370                          const VkAllocationCallbacks* pAllocator);
371 
372     VkResult on_vkMapMemory(android::base::BumpPool* pool, VkDevice device, VkDeviceMemory memory,
373                             VkDeviceSize offset, VkDeviceSize size, VkMemoryMapFlags flags,
374                             void** ppData);
375 
376     void on_vkUnmapMemory(android::base::BumpPool* pool, VkDevice device, VkDeviceMemory memory);
377 
378     uint8_t* getMappedHostPointer(VkDeviceMemory memory);
379     VkDeviceSize getDeviceMemorySize(VkDeviceMemory memory);
380     bool usingDirectMapping() const;
381 
382     struct HostFeatureSupport {
383         bool supportsVulkan = false;
384         bool supportsVulkan1_1 = false;
385         bool supportsExternalMemory = false;
386         bool useDeferredCommands = false;
387         bool useCreateResourcesWithRequirements = false;
388         uint32_t apiVersion = 0;
389         uint32_t driverVersion = 0;
390         uint32_t deviceID = 0;
391         uint32_t vendorID = 0;
392     };
393 
394     HostFeatureSupport getHostFeatureSupport() const;
395 
396     // VK_ANDROID_native_buffer
397     VkResult on_vkGetSwapchainGrallocUsageANDROID(android::base::BumpPool* pool, VkDevice device,
398                                                   VkFormat format, VkImageUsageFlags imageUsage,
399                                                   int* grallocUsage);
400     VkResult on_vkGetSwapchainGrallocUsage2ANDROID(
401         android::base::BumpPool* pool, VkDevice device, VkFormat format,
402         VkImageUsageFlags imageUsage, VkSwapchainImageUsageFlagsANDROID swapchainImageUsage,
403         uint64_t* grallocConsumerUsage, uint64_t* grallocProducerUsage);
404     VkResult on_vkAcquireImageANDROID(android::base::BumpPool* pool, VkDevice device, VkImage image,
405                                       int nativeFenceFd, VkSemaphore semaphore, VkFence fence);
406     VkResult on_vkQueueSignalReleaseImageANDROID(android::base::BumpPool* pool, VkQueue queue,
407                                                  uint32_t waitSemaphoreCount,
408                                                  const VkSemaphore* pWaitSemaphores, VkImage image,
409                                                  int* pNativeFenceFd);
410 
411     // VK_GOOGLE_gfxstream
412     VkResult on_vkMapMemoryIntoAddressSpaceGOOGLE(android::base::BumpPool* pool, VkDevice device,
413                                                   VkDeviceMemory memory, uint64_t* pAddress);
414     VkResult on_vkGetMemoryHostAddressInfoGOOGLE(android::base::BumpPool* pool, VkDevice device,
415                                                  VkDeviceMemory memory, uint64_t* pAddress,
416                                                  uint64_t* pSize, uint64_t* pHostmemId);
417     VkResult on_vkGetBlobGOOGLE(android::base::BumpPool* pool, VkDevice device,
418                                 VkDeviceMemory memory);
419 
420     VkResult on_vkFreeMemorySyncGOOGLE(android::base::BumpPool* pool, VkDevice device,
421                                        VkDeviceMemory memory,
422                                        const VkAllocationCallbacks* pAllocator);
423 
424     VkResult on_vkAllocateCommandBuffers(android::base::BumpPool* pool, VkDevice device,
425                                          const VkCommandBufferAllocateInfo* pAllocateInfo,
426                                          VkCommandBuffer* pCommandBuffers);
427 
428     void on_vkCmdExecuteCommands(android::base::BumpPool* pool, VkCommandBuffer commandBuffer,
429                                  uint32_t commandBufferCount,
430                                  const VkCommandBuffer* pCommandBuffers);
431 
432     VkResult on_vkQueueSubmit(android::base::BumpPool* pool, VkQueue queue, uint32_t submitCount,
433                               const VkSubmitInfo* pSubmits, VkFence fence);
434 
435     VkResult on_vkQueueWaitIdle(android::base::BumpPool* pool, VkQueue queue);
436 
437     VkResult on_vkResetCommandBuffer(android::base::BumpPool* pool, VkCommandBuffer commandBuffer,
438                                      VkCommandBufferResetFlags flags);
439 
440     void on_vkFreeCommandBuffers(android::base::BumpPool* pool, VkDevice device,
441                                  VkCommandPool commandPool, uint32_t commandBufferCount,
442                                  const VkCommandBuffer* pCommandBuffers);
443 
444     VkResult on_vkCreateCommandPool(android::base::BumpPool* pool, VkDevice device,
445                                     const VkCommandPoolCreateInfo* pCreateInfo,
446                                     const VkAllocationCallbacks* pAllocator,
447                                     VkCommandPool* pCommandPool);
448 
449     void on_vkDestroyCommandPool(android::base::BumpPool* pool, VkDevice device,
450                                  VkCommandPool commandPool,
451                                  const VkAllocationCallbacks* pAllocator);
452 
453     VkResult on_vkResetCommandPool(android::base::BumpPool* pool, VkDevice device,
454                                    VkCommandPool commandPool, VkCommandPoolResetFlags flags);
455 
456     void on_vkGetPhysicalDeviceExternalSemaphoreProperties(
457         android::base::BumpPool* pool, VkPhysicalDevice physicalDevice,
458         const VkPhysicalDeviceExternalSemaphoreInfo* pExternalSemaphoreInfo,
459         VkExternalSemaphoreProperties* pExternalSemaphoreProperties);
460 
461     void on_vkGetPhysicalDeviceExternalSemaphorePropertiesKHR(
462         android::base::BumpPool* pool, VkPhysicalDevice physicalDevice,
463         const VkPhysicalDeviceExternalSemaphoreInfo* pExternalSemaphoreInfo,
464         VkExternalSemaphoreProperties* pExternalSemaphoreProperties);
465 
466     VkResult on_vkCreateSemaphore(android::base::BumpPool* pool, VkDevice device,
467                                   const VkSemaphoreCreateInfo* pCreateInfo,
468                                   const VkAllocationCallbacks* pAllocator, VkSemaphore* pSemaphore);
469     VkResult on_vkImportSemaphoreFdKHR(android::base::BumpPool* pool, VkDevice device,
470                                        const VkImportSemaphoreFdInfoKHR* pImportSemaphoreFdInfo);
471     VkResult on_vkGetSemaphoreFdKHR(android::base::BumpPool* pool, VkDevice boxed_device,
472                                     const VkSemaphoreGetFdInfoKHR* pGetFdInfo, int* pFd);
473     void on_vkDestroySemaphore(android::base::BumpPool* pool, VkDevice boxed_device,
474                                VkSemaphore semaphore, const VkAllocationCallbacks* pAllocator);
475 
476     VkResult on_vkCreateFence(android::base::BumpPool* pool, VkDevice device,
477                               const VkFenceCreateInfo* pCreateInfo,
478                               const VkAllocationCallbacks* pAllocator, VkFence* pFence);
479     VkResult on_vkResetFences(android::base::BumpPool* pool, VkDevice device, uint32_t fenceCount,
480                               const VkFence* pFences);
481     void on_vkDestroyFence(android::base::BumpPool* pool, VkDevice device, VkFence fence,
482                            const VkAllocationCallbacks* pAllocator);
483 
484     // Descriptor update templates
485     VkResult on_vkCreateDescriptorUpdateTemplate(
486         android::base::BumpPool* pool, VkDevice boxed_device,
487         const VkDescriptorUpdateTemplateCreateInfo* pCreateInfo,
488         const VkAllocationCallbacks* pAllocator,
489         VkDescriptorUpdateTemplate* pDescriptorUpdateTemplate);
490 
491     VkResult on_vkCreateDescriptorUpdateTemplateKHR(
492         android::base::BumpPool* pool, VkDevice boxed_device,
493         const VkDescriptorUpdateTemplateCreateInfo* pCreateInfo,
494         const VkAllocationCallbacks* pAllocator,
495         VkDescriptorUpdateTemplate* pDescriptorUpdateTemplate);
496 
497     void on_vkDestroyDescriptorUpdateTemplate(android::base::BumpPool* pool, VkDevice boxed_device,
498                                               VkDescriptorUpdateTemplate descriptorUpdateTemplate,
499                                               const VkAllocationCallbacks* pAllocator);
500 
501     void on_vkDestroyDescriptorUpdateTemplateKHR(
502         android::base::BumpPool* pool, VkDevice boxed_device,
503         VkDescriptorUpdateTemplate descriptorUpdateTemplate,
504         const VkAllocationCallbacks* pAllocator);
505 
506     void on_vkUpdateDescriptorSetWithTemplateSizedGOOGLE(
507         android::base::BumpPool* pool, VkDevice boxed_device, VkDescriptorSet descriptorSet,
508         VkDescriptorUpdateTemplate descriptorUpdateTemplate, uint32_t imageInfoCount,
509         uint32_t bufferInfoCount, uint32_t bufferViewCount, const uint32_t* pImageInfoEntryIndices,
510         const uint32_t* pBufferInfoEntryIndices, const uint32_t* pBufferViewEntryIndices,
511         const VkDescriptorImageInfo* pImageInfos, const VkDescriptorBufferInfo* pBufferInfos,
512         const VkBufferView* pBufferViews);
513 
514     void on_vkUpdateDescriptorSetWithTemplateSized2GOOGLE(
515         android::base::BumpPool* pool, VkDevice boxed_device, VkDescriptorSet descriptorSet,
516         VkDescriptorUpdateTemplate descriptorUpdateTemplate, uint32_t imageInfoCount,
517         uint32_t bufferInfoCount, uint32_t bufferViewCount, uint32_t inlineUniformBlockCount,
518         const uint32_t* pImageInfoEntryIndices, const uint32_t* pBufferInfoEntryIndices,
519         const uint32_t* pBufferViewEntryIndices, const VkDescriptorImageInfo* pImageInfos,
520         const VkDescriptorBufferInfo* pBufferInfos, const VkBufferView* pBufferViews,
521         const uint8_t* pInlineUniformBlockData);
522 
523     VkResult on_vkBeginCommandBuffer(android::base::BumpPool* pool, VkCommandBuffer commandBuffer,
524                                      const VkCommandBufferBeginInfo* pBeginInfo,
525                                      const VkDecoderContext& context);
526     void on_vkBeginCommandBufferAsyncGOOGLE(android::base::BumpPool* pool,
527                                             VkCommandBuffer commandBuffer,
528                                             const VkCommandBufferBeginInfo* pBeginInfo,
529                                             const VkDecoderContext& context);
530     VkResult on_vkEndCommandBuffer(android::base::BumpPool* pool, VkCommandBuffer commandBuffer,
531                                    const VkDecoderContext& context);
532     void on_vkEndCommandBufferAsyncGOOGLE(android::base::BumpPool* pool,
533                                           VkCommandBuffer commandBuffer,
534                                           const VkDecoderContext& context);
535     void on_vkResetCommandBufferAsyncGOOGLE(android::base::BumpPool* pool,
536                                             VkCommandBuffer commandBuffer,
537                                             VkCommandBufferResetFlags flags);
538     void on_vkCommandBufferHostSyncGOOGLE(android::base::BumpPool* pool,
539                                           VkCommandBuffer commandBuffer, uint32_t needHostSync,
540                                           uint32_t sequenceNumber);
541 
542     VkResult on_vkCreateImageWithRequirementsGOOGLE(android::base::BumpPool* pool, VkDevice device,
543                                                     const VkImageCreateInfo* pCreateInfo,
544                                                     const VkAllocationCallbacks* pAllocator,
545                                                     VkImage* pImage,
546                                                     VkMemoryRequirements* pMemoryRequirements);
547     VkResult on_vkCreateBufferWithRequirementsGOOGLE(android::base::BumpPool* pool, VkDevice device,
548                                                      const VkBufferCreateInfo* pCreateInfo,
549                                                      const VkAllocationCallbacks* pAllocator,
550                                                      VkBuffer* pBuffer,
551                                                      VkMemoryRequirements* pMemoryRequirements);
552 
553     void on_vkCmdBindPipeline(android::base::BumpPool* pool, VkCommandBuffer commandBuffer,
554                               VkPipelineBindPoint pipelineBindPoint, VkPipeline pipeline);
555 
556     void on_vkCmdBindDescriptorSets(android::base::BumpPool* pool, VkCommandBuffer commandBuffer,
557                                     VkPipelineBindPoint pipelineBindPoint, VkPipelineLayout layout,
558                                     uint32_t firstSet, uint32_t descriptorSetCount,
559                                     const VkDescriptorSet* pDescriptorSets,
560                                     uint32_t dynamicOffsetCount, const uint32_t* pDynamicOffsets);
561 
562     VkResult on_vkCreateRenderPass(android::base::BumpPool* pool, VkDevice device,
563                                    const VkRenderPassCreateInfo* pCreateInfo,
564                                    const VkAllocationCallbacks* pAllocator,
565                                    VkRenderPass* pRenderPass);
566     VkResult on_vkCreateRenderPass2(android::base::BumpPool* pool, VkDevice device,
567                                     const VkRenderPassCreateInfo2* pCreateInfo,
568                                     const VkAllocationCallbacks* pAllocator,
569                                     VkRenderPass* pRenderPass);
570     VkResult on_vkCreateRenderPass2KHR(android::base::BumpPool* pool, VkDevice device,
571                                        const VkRenderPassCreateInfo2KHR* pCreateInfo,
572                                        const VkAllocationCallbacks* pAllocator,
573                                        VkRenderPass* pRenderPass);
574     void on_vkDestroyRenderPass(android::base::BumpPool* pool, VkDevice device,
575                                 VkRenderPass renderPass, const VkAllocationCallbacks* pAllocator);
576     VkResult on_vkCreateFramebuffer(android::base::BumpPool* pool, VkDevice device,
577                                     const VkFramebufferCreateInfo* pCreateInfo,
578                                     const VkAllocationCallbacks* pAllocator,
579                                     VkFramebuffer* pFramebuffer);
580     void on_vkDestroyFramebuffer(android::base::BumpPool* pool, VkDevice device,
581                                  VkFramebuffer framebuffer,
582                                  const VkAllocationCallbacks* pAllocator);
583 
584     void on_vkCmdCopyQueryPoolResults(android::base::BumpPool* pool, VkCommandBuffer commandBuffer,
585                                       VkQueryPool queryPool, uint32_t firstQuery,
586                                       uint32_t queryCount, VkBuffer dstBuffer,
587                                       VkDeviceSize dstOffset, VkDeviceSize stride,
588                                       VkQueryResultFlags flags);
589 
590     // VK_GOOGLE_gfxstream
591     void on_vkQueueHostSyncGOOGLE(android::base::BumpPool* pool, VkQueue queue,
592                                   uint32_t needHostSync, uint32_t sequenceNumber);
593     void on_vkQueueSubmitAsyncGOOGLE(android::base::BumpPool* pool, VkQueue queue,
594                                      uint32_t submitCount, const VkSubmitInfo* pSubmits,
595                                      VkFence fence);
596     void on_vkQueueWaitIdleAsyncGOOGLE(android::base::BumpPool* pool, VkQueue queue);
597     void on_vkQueueBindSparseAsyncGOOGLE(android::base::BumpPool* pool, VkQueue queue,
598                                          uint32_t bindInfoCount, const VkBindSparseInfo* pBindInfo,
599                                          VkFence fence);
600 
601     // VK_GOOGLE_gfxstream
602     void on_vkGetLinearImageLayoutGOOGLE(android::base::BumpPool* pool, VkDevice device,
603                                          VkFormat format, VkDeviceSize* pOffset,
604                                          VkDeviceSize* pRowPitchAlignment);
605     void on_vkGetLinearImageLayout2GOOGLE(android::base::BumpPool* pool, VkDevice device,
606                                           const VkImageCreateInfo* pCreateInfo,
607                                           VkDeviceSize* pOffset, VkDeviceSize* pRowPitchAlignment);
608 
609     // VK_GOOGLE_gfxstream
610     void on_vkQueueFlushCommandsGOOGLE(android::base::BumpPool* pool, VkQueue queue,
611                                        VkCommandBuffer commandBuffer, VkDeviceSize dataSize,
612                                        const void* pData, const VkDecoderContext& context);
613     void on_vkQueueFlushCommandsFromAuxMemoryGOOGLE(android::base::BumpPool* pool, VkQueue queue,
614                                                     VkCommandBuffer commandBuffer,
615                                                     VkDeviceMemory deviceMemory,
616                                                     VkDeviceSize dataOffset, VkDeviceSize dataSize,
617                                                     const VkDecoderContext& context);
618     void on_vkQueueCommitDescriptorSetUpdatesGOOGLE(
619         android::base::BumpPool* pool, VkQueue queue, uint32_t descriptorPoolCount,
620         const VkDescriptorPool* pDescriptorPools, uint32_t descriptorSetCount,
621         const VkDescriptorSetLayout* pDescriptorSetLayouts, const uint64_t* pDescriptorSetPoolIds,
622         const uint32_t* pDescriptorSetWhichPool, const uint32_t* pDescriptorSetPendingAllocation,
623         const uint32_t* pDescriptorWriteStartingIndices, uint32_t pendingDescriptorWriteCount,
624         const VkWriteDescriptorSet* pPendingDescriptorWrites);
625     void on_vkCollectDescriptorPoolIdsGOOGLE(android::base::BumpPool* pool, VkDevice device,
626                                              VkDescriptorPool descriptorPool,
627                                              uint32_t* pPoolIdCount, uint64_t* pPoolIds);
628     VkResult on_vkQueueBindSparse(android::base::BumpPool* pool, VkQueue queue,
629                                   uint32_t bindInfoCount, const VkBindSparseInfo* pBindInfo,
630                                   VkFence fence);
631     void on_vkQueueSignalReleaseImageANDROIDAsyncGOOGLE(android::base::BumpPool* pool,
632                                                         VkQueue queue, uint32_t waitSemaphoreCount,
633                                                         const VkSemaphore* pWaitSemaphores,
634                                                         VkImage image);
635 
636     VkResult on_vkCreateSamplerYcbcrConversion(
637         android::base::BumpPool* pool, VkDevice device,
638         const VkSamplerYcbcrConversionCreateInfo* pCreateInfo,
639         const VkAllocationCallbacks* pAllocator, VkSamplerYcbcrConversion* pYcbcrConversion);
640     VkResult on_vkCreateSamplerYcbcrConversionKHR(
641         android::base::BumpPool* pool, VkDevice device,
642         const VkSamplerYcbcrConversionCreateInfo* pCreateInfo,
643         const VkAllocationCallbacks* pAllocator, VkSamplerYcbcrConversion* pYcbcrConversion);
644     void on_vkDestroySamplerYcbcrConversion(android::base::BumpPool* pool, VkDevice device,
645                                             VkSamplerYcbcrConversion ycbcrConversion,
646                                             const VkAllocationCallbacks* pAllocator);
647     void on_vkDestroySamplerYcbcrConversionKHR(android::base::BumpPool* pool, VkDevice device,
648                                                VkSamplerYcbcrConversion ycbcrConversion,
649                                                const VkAllocationCallbacks* pAllocator);
650 
651     void on_DeviceLost();
652 
653     void DeviceLostHandler();
654 
655     void on_CheckOutOfMemory(VkResult result, uint32_t opCode, const VkDecoderContext& context,
656                              std::optional<uint64_t> allocationSize = std::nullopt);
657 
658     // Fence waits
659     VkResult waitForFence(VkFence boxed_fence, uint64_t timeout);
660 
661     VkResult getFenceStatus(VkFence boxed_fence);
662 
663     // Wait for present (vkQueueSignalReleaseImageANDROID). This explicitly
664     // requires the image to be presented again versus how many times it's been
665     // presented so far, so it ends up incrementing a "target present count"
666     // for this image, and then waiting for the image to get vkQSRI'ed at least
667     // that many times.
668     AsyncResult registerQsriCallback(VkImage boxed_image, VkQsriTimeline::Callback callback);
669 
670     // Transformations
671     void deviceMemoryTransform_tohost(VkDeviceMemory* memory, uint32_t memoryCount,
672                                       VkDeviceSize* offset, uint32_t offsetCount,
673                                       VkDeviceSize* size, uint32_t sizeCount, uint32_t* typeIndex,
674                                       uint32_t typeIndexCount, uint32_t* typeBits,
675                                       uint32_t typeBitsCount);
676     void deviceMemoryTransform_fromhost(VkDeviceMemory* memory, uint32_t memoryCount,
677                                         VkDeviceSize* offset, uint32_t offsetCount,
678                                         VkDeviceSize* size, uint32_t sizeCount, uint32_t* typeIndex,
679                                         uint32_t typeIndexCount, uint32_t* typeBits,
680                                         uint32_t typeBitsCount);
681 
682     // Snapshot access
683     VkDecoderSnapshot* snapshot();
684 
685 #define DEFINE_TRANSFORMED_TYPE_PROTOTYPE(type)                \
686     void transformImpl_##type##_tohost(const type*, uint32_t); \
687     void transformImpl_##type##_fromhost(const type*, uint32_t);
688 
689     LIST_TRANSFORMED_TYPES(DEFINE_TRANSFORMED_TYPE_PROTOTYPE)
690 
691     // boxed handles
692 #define DEFINE_BOXED_DISPATCHABLE_HANDLE_API_DECL(type)                                 \
693     type new_boxed_##type(type underlying, VulkanDispatch* dispatch, bool ownDispatch); \
694     void delete_##type(type boxed);                                                     \
695     type unbox_##type(type boxed);                                                      \
696     type unboxed_to_boxed_##type(type boxed);                                           \
697     VulkanDispatch* dispatch_##type(type boxed);
698 
699 #define DEFINE_BOXED_NON_DISPATCHABLE_HANDLE_API_DECL(type)  \
700     type new_boxed_non_dispatchable_##type(type underlying); \
701     void delete_##type(type boxed);                          \
702     type unbox_##type(type boxed);                           \
703     type unboxed_to_boxed_non_dispatchable_##type(type boxed);
704 
705     GOLDFISH_VK_LIST_DISPATCHABLE_HANDLE_TYPES(DEFINE_BOXED_DISPATCHABLE_HANDLE_API_DECL)
706     GOLDFISH_VK_LIST_NON_DISPATCHABLE_HANDLE_TYPES(DEFINE_BOXED_NON_DISPATCHABLE_HANDLE_API_DECL)
707 
708    private:
709     class Impl;
710     std::unique_ptr<Impl> mImpl;
711 };
712 
713 #define MAKE_HANDLE_MAPPING_FOREACH(type_name, map_impl, map_to_u64_impl, map_from_u64_impl)       \
714     void mapHandles_##type_name(type_name* handles, size_t count) override {                       \
715         for (size_t i = 0; i < count; ++i) {                                                       \
716             map_impl;                                                                              \
717         }                                                                                          \
718     }                                                                                              \
719     void mapHandles_##type_name##_u64(const type_name* handles, uint64_t* handle_u64s,             \
720                                       size_t count) override {                                     \
721         for (size_t i = 0; i < count; ++i) {                                                       \
722             map_to_u64_impl;                                                                       \
723         }                                                                                          \
724     }                                                                                              \
725     void mapHandles_u64_##type_name(const uint64_t* handle_u64s, type_name* handles, size_t count) \
726         override {                                                                                 \
727         for (size_t i = 0; i < count; ++i) {                                                       \
728             map_from_u64_impl;                                                                     \
729         }                                                                                          \
730     }
731 
732 #define BOXED_DISPATCHABLE_UNWRAP_IMPL(type_name)                                                  \
733     MAKE_HANDLE_MAPPING_FOREACH(                                                                   \
734         type_name, if (handles[i]) { handles[i] = m_state->unbox_##type_name(handles[i]); } else { \
735             handles[i] = (type_name) nullptr;                                                      \
736         };                                                                                         \
737         ,                                                                                          \
738         if (handles[i]) {                                                                          \
739             handle_u64s[i] = (uint64_t)m_state->unbox_##type_name(handles[i]);                     \
740         } else { handle_u64s[i] = 0; },                                                            \
741         if (handle_u64s[i]) {                                                                      \
742             handles[i] = m_state->unbox_##type_name((type_name)(uintptr_t)handle_u64s[i]);         \
743         } else { handles[i] = (type_name) nullptr; })
744 
745 #define BOXED_NON_DISPATCHABLE_UNWRAP_IMPL(type_name)                                              \
746     MAKE_HANDLE_MAPPING_FOREACH(                                                                   \
747         type_name, if (handles[i]) { handles[i] = m_state->unbox_##type_name(handles[i]); } else { \
748             handles[i] = (type_name) nullptr;                                                      \
749         };                                                                                         \
750         ,                                                                                          \
751         if (handles[i]) {                                                                          \
752             handle_u64s[i] = (uint64_t)m_state->unbox_##type_name(handles[i]);                     \
753         } else { handle_u64s[i] = 0; },                                                            \
754         if (handle_u64s[i]) {                                                                      \
755             handles[i] = m_state->unbox_##type_name((type_name)(uintptr_t)handle_u64s[i]);         \
756         } else { handles[i] = (type_name) nullptr; })
757 
758 class BoxedHandleUnwrapMapping : public VulkanHandleMapping {
759    public:
BoxedHandleUnwrapMapping(VkDecoderGlobalState * state)760     BoxedHandleUnwrapMapping(VkDecoderGlobalState* state) : VulkanHandleMapping(state) {}
~BoxedHandleUnwrapMapping()761     virtual ~BoxedHandleUnwrapMapping() {}
762     GOLDFISH_VK_LIST_DISPATCHABLE_HANDLE_TYPES(BOXED_DISPATCHABLE_UNWRAP_IMPL)
763     GOLDFISH_VK_LIST_NON_DISPATCHABLE_HANDLE_TYPES(BOXED_NON_DISPATCHABLE_UNWRAP_IMPL)
764 };
765 
766 #define BOXED_DISPATCHABLE_WRAP_IMPL(type_name)                                                    \
767     MAKE_HANDLE_MAPPING_FOREACH(                                                                   \
768         type_name,                                                                                 \
769         if (handles[i]) { handles[i] = m_state->unboxed_to_boxed_##type_name(handles[i]); } else { \
770             handles[i] = (type_name) nullptr;                                                      \
771         };                                                                                         \
772         ,                                                                                          \
773         if (handles[i]) {                                                                          \
774             handle_u64s[i] = (uint64_t)m_state->unboxed_to_boxed_##type_name(handles[i]);          \
775         } else { handle_u64s[i] = 0; },                                                            \
776         if (handle_u64s[i]) {                                                                      \
777             handles[i] =                                                                           \
778                 m_state->unboxed_to_boxed_##type_name((type_name)(uintptr_t)handle_u64s[i]);       \
779         } else { handles[i] = (type_name) nullptr; })
780 
781 #define BOXED_NON_DISPATCHABLE_WRAP_IMPL(type_name)                                           \
782     MAKE_HANDLE_MAPPING_FOREACH(                                                              \
783         type_name,                                                                            \
784         if (handles[i]) {                                                                     \
785             handles[i] = m_state->unboxed_to_boxed_non_dispatchable_##type_name(handles[i]);  \
786         } else { handles[i] = (type_name) nullptr; };                                         \
787         ,                                                                                     \
788         if (handles[i]) {                                                                     \
789             handle_u64s[i] =                                                                  \
790                 (uint64_t)m_state->unboxed_to_boxed_non_dispatchable_##type_name(handles[i]); \
791         } else { handle_u64s[i] = 0; },                                                       \
792         if (handle_u64s[i]) {                                                                 \
793             handles[i] = m_state->unboxed_to_boxed_non_dispatchable_##type_name(              \
794                 (type_name)(uintptr_t)handle_u64s[i]);                                        \
795         } else { handles[i] = (type_name) nullptr; })
796 
797 class BoxedHandleWrapMapping : public VulkanHandleMapping {
798    public:
BoxedHandleWrapMapping(VkDecoderGlobalState * state)799     BoxedHandleWrapMapping(VkDecoderGlobalState* state) : VulkanHandleMapping(state) {}
~BoxedHandleWrapMapping()800     virtual ~BoxedHandleWrapMapping() {}
801     GOLDFISH_VK_LIST_DISPATCHABLE_HANDLE_TYPES(BOXED_DISPATCHABLE_WRAP_IMPL)
802     GOLDFISH_VK_LIST_NON_DISPATCHABLE_HANDLE_TYPES(BOXED_NON_DISPATCHABLE_WRAP_IMPL)
803 };
804 
805 // Not used, so we do not define.
806 #define BOXED_DISPATCHABLE_CREATE_IMPL(type_name)                                  \
807     MAKE_HANDLE_MAPPING_FOREACH(type_name, (void)handles[i], (void)handle_u64s[i], \
808                                 (void)handles[i];)
809 
810 // Not used, so we do not define.
811 #define BOXED_DISPATCHABLE_DESTROY_IMPL(type_name)                                 \
812     MAKE_HANDLE_MAPPING_FOREACH(type_name, (void)handles[i], (void)handle_u64s[i], \
813                                 (void)handles[i];)
814 
815 // We only use the create/destroy mappings for non dispatchable handles.
816 #define BOXED_NON_DISPATCHABLE_CREATE_IMPL(type_name)                                    \
817     MAKE_HANDLE_MAPPING_FOREACH(                                                         \
818         type_name, handles[i] = new_boxed_non_dispatchable_##type_name(handles[i]);      \
819         , handle_u64s[i] = (uint64_t)new_boxed_non_dispatchable_##type_name(handles[i]), \
820         handles[i] = (type_name)new_boxed_non_dispatchable_##type_name(                  \
821             (type_name)(uintptr_t)handle_u64s[i]);)
822 
823 #define BOXED_NON_DISPATCHABLE_DESTROY_IMPL(type_name)                                           \
824     MAKE_HANDLE_MAPPING_FOREACH(type_name, delete_##type_name(handles[i]), (void)handle_u64s[i]; \
825                                 delete_##type_name(handles[i]), (void)handles[i];                \
826                                 delete_##type_name((type_name)handle_u64s[i]))
827 
828 #define BOXED_NON_DISPATCHABLE_UNWRAP_AND_DELETE_IMPL(type_name)                           \
829     MAKE_HANDLE_MAPPING_FOREACH(                                                           \
830         type_name,                                                                         \
831         if (handles[i]) {                                                                  \
832             auto boxed = handles[i];                                                       \
833             handles[i] = m_state->unbox_##type_name(handles[i]);                           \
834             delete_##type_name(boxed);                                                     \
835         } else { handles[i] = (type_name) nullptr; };                                      \
836         ,                                                                                  \
837         if (handles[i]) {                                                                  \
838             auto boxed = handles[i];                                                       \
839             handle_u64s[i] = (uint64_t)m_state->unbox_##type_name(handles[i]);             \
840             delete_##type_name(boxed);                                                     \
841         } else { handle_u64s[i] = 0; },                                                    \
842         if (handle_u64s[i]) {                                                              \
843             auto boxed = (type_name)(uintptr_t)handle_u64s[i];                             \
844             handles[i] = m_state->unbox_##type_name((type_name)(uintptr_t)handle_u64s[i]); \
845             delete_##type_name(boxed);                                                     \
846         } else { handles[i] = (type_name) nullptr; })
847 
848 class BoxedHandleCreateMapping : public VulkanHandleMapping {
849    public:
BoxedHandleCreateMapping(VkDecoderGlobalState * state)850     BoxedHandleCreateMapping(VkDecoderGlobalState* state) : VulkanHandleMapping(state) {}
~BoxedHandleCreateMapping()851     virtual ~BoxedHandleCreateMapping() {}
852     GOLDFISH_VK_LIST_DISPATCHABLE_HANDLE_TYPES(BOXED_DISPATCHABLE_CREATE_IMPL)
853     GOLDFISH_VK_LIST_NON_DISPATCHABLE_HANDLE_TYPES(BOXED_NON_DISPATCHABLE_CREATE_IMPL)
854 };
855 
856 class BoxedHandleDestroyMapping : public VulkanHandleMapping {
857    public:
BoxedHandleDestroyMapping(VkDecoderGlobalState * state)858     BoxedHandleDestroyMapping(VkDecoderGlobalState* state) : VulkanHandleMapping(state) {}
~BoxedHandleDestroyMapping()859     virtual ~BoxedHandleDestroyMapping() {}
860     GOLDFISH_VK_LIST_DISPATCHABLE_HANDLE_TYPES(BOXED_DISPATCHABLE_DESTROY_IMPL)
861     GOLDFISH_VK_LIST_NON_DISPATCHABLE_HANDLE_TYPES(BOXED_NON_DISPATCHABLE_DESTROY_IMPL)
862 };
863 
864 class BoxedHandleUnwrapAndDeleteMapping : public VulkanHandleMapping {
865    public:
BoxedHandleUnwrapAndDeleteMapping(VkDecoderGlobalState * state)866     BoxedHandleUnwrapAndDeleteMapping(VkDecoderGlobalState* state) : VulkanHandleMapping(state) {}
~BoxedHandleUnwrapAndDeleteMapping()867     virtual ~BoxedHandleUnwrapAndDeleteMapping() {}
868     GOLDFISH_VK_LIST_DISPATCHABLE_HANDLE_TYPES(BOXED_DISPATCHABLE_DESTROY_IMPL)
869     GOLDFISH_VK_LIST_NON_DISPATCHABLE_HANDLE_TYPES(BOXED_NON_DISPATCHABLE_UNWRAP_AND_DELETE_IMPL)
870 };
871 
872 #define HANDLE_MAPPING_DECLS(type_name)                                                            \
873     void mapHandles_##type_name(type_name* handles, size_t count) override;                        \
874     void mapHandles_##type_name##_u64(const type_name* handles, uint64_t* handle_u64s,             \
875                                       size_t count) override;                                      \
876     void mapHandles_u64_##type_name(const uint64_t* handle_u64s, type_name* handles, size_t count) \
877         override;
878 
879 class BoxedHandleUnwrapAndDeletePreserveBoxedMapping : public VulkanHandleMapping {
880    public:
BoxedHandleUnwrapAndDeletePreserveBoxedMapping(VkDecoderGlobalState * state)881     BoxedHandleUnwrapAndDeletePreserveBoxedMapping(VkDecoderGlobalState* state)
882         : VulkanHandleMapping(state) {}
883     void setup(android::base::BumpPool* pool, uint64_t** bufPtr);
~BoxedHandleUnwrapAndDeletePreserveBoxedMapping()884     virtual ~BoxedHandleUnwrapAndDeletePreserveBoxedMapping() {}
885 
886     GOLDFISH_VK_LIST_DISPATCHABLE_HANDLE_TYPES(HANDLE_MAPPING_DECLS)
887     GOLDFISH_VK_LIST_NON_DISPATCHABLE_HANDLE_TYPES(HANDLE_MAPPING_DECLS)
888 
889    private:
890     void allocPreserve(size_t count);
891 
892     android::base::BumpPool* mPool = nullptr;
893     uint64_t** mPreserveBufPtr = nullptr;
894 };
895 
896 template <class TDispatch>
897 class ExternalFencePool {
898    public:
ExternalFencePool(TDispatch * dispatch,VkDevice device)899     ExternalFencePool(TDispatch* dispatch, VkDevice device)
900         : m_vk(dispatch), mDevice(device), mMaxSize(5) {}
901 
~ExternalFencePool()902     ~ExternalFencePool() {
903         if (!mPool.empty()) {
904             GFXSTREAM_ABORT(FatalError(ABORT_REASON_OTHER))
905                 << "External fence pool for device " << static_cast<void*>(mDevice)
906                 << " destroyed but " << mPool.size() << " fences still not destroyed.";
907         }
908     }
909 
add(VkFence fence)910     void add(VkFence fence) {
911         AutoLock lock(mLock);
912         mPool.push_back(fence);
913         if (mPool.size() > mMaxSize) {
914             INFO("External fence pool for %p has increased to size %d", mDevice, mPool.size());
915             mMaxSize = mPool.size();
916         }
917     }
918 
pop(const VkFenceCreateInfo * pCreateInfo)919     VkFence pop(const VkFenceCreateInfo* pCreateInfo) {
920         VkFence fence = VK_NULL_HANDLE;
921         {
922             AutoLock lock(mLock);
923             auto it = std::find_if(mPool.begin(), mPool.end(), [this](const VkFence& fence) {
924                 VkResult status = m_vk->vkGetFenceStatus(mDevice, fence);
925                 if (status != VK_SUCCESS) {
926                     if (status != VK_NOT_READY) {
927                         VK_CHECK(status);
928                     }
929 
930                     // Status is valid, but fence is not yet signaled
931                     return false;
932                 }
933                 return true;
934             });
935             if (it == mPool.end()) {
936                 return VK_NULL_HANDLE;
937             }
938 
939             fence = *it;
940             mPool.erase(it);
941         }
942 
943         if (!(pCreateInfo->flags & VK_FENCE_CREATE_SIGNALED_BIT)) {
944             VK_CHECK(m_vk->vkResetFences(mDevice, 1, &fence));
945         }
946 
947         return fence;
948     }
949 
popAll()950     std::vector<VkFence> popAll() {
951         AutoLock lock(mLock);
952         std::vector<VkFence> popped = mPool;
953         mPool.clear();
954         return popped;
955     }
956 
957    private:
958     TDispatch* m_vk;
959     VkDevice mDevice;
960     Lock mLock;
961     std::vector<VkFence> mPool;
962     int mMaxSize;
963 };
964 
965 }  // namespace vk
966 }  // namespace gfxstream
967