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