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