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