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