• 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 "VulkanHandleMapping.h"
17 #include "VulkanDispatch.h"
18 
19 #include <vulkan/vulkan.h>
20 
21 #include <memory>
22 
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     void on_vkDestroyFence(android::base::BumpPool* pool,
604                            VkDevice device,
605                            VkFence fence,
606                            const VkAllocationCallbacks* pAllocator);
607 
608     // Descriptor update templates
609     VkResult on_vkCreateDescriptorUpdateTemplate(
610         android::base::BumpPool* pool,
611         VkDevice boxed_device,
612         const VkDescriptorUpdateTemplateCreateInfo* pCreateInfo,
613         const VkAllocationCallbacks* pAllocator,
614         VkDescriptorUpdateTemplate* pDescriptorUpdateTemplate);
615 
616     VkResult on_vkCreateDescriptorUpdateTemplateKHR(
617         android::base::BumpPool* pool,
618         VkDevice boxed_device,
619         const VkDescriptorUpdateTemplateCreateInfo* pCreateInfo,
620         const VkAllocationCallbacks* pAllocator,
621         VkDescriptorUpdateTemplate* pDescriptorUpdateTemplate);
622 
623     void on_vkDestroyDescriptorUpdateTemplate(
624         android::base::BumpPool* pool,
625         VkDevice boxed_device,
626         VkDescriptorUpdateTemplate descriptorUpdateTemplate,
627         const VkAllocationCallbacks* pAllocator);
628 
629     void on_vkDestroyDescriptorUpdateTemplateKHR(
630         android::base::BumpPool* pool,
631         VkDevice boxed_device,
632         VkDescriptorUpdateTemplate descriptorUpdateTemplate,
633         const VkAllocationCallbacks* pAllocator);
634 
635     void on_vkUpdateDescriptorSetWithTemplateSizedGOOGLE(
636         android::base::BumpPool* pool,
637         VkDevice boxed_device,
638         VkDescriptorSet descriptorSet,
639         VkDescriptorUpdateTemplate descriptorUpdateTemplate,
640         uint32_t imageInfoCount,
641         uint32_t bufferInfoCount,
642         uint32_t bufferViewCount,
643         const uint32_t* pImageInfoEntryIndices,
644         const uint32_t* pBufferInfoEntryIndices,
645         const uint32_t* pBufferViewEntryIndices,
646         const VkDescriptorImageInfo* pImageInfos,
647         const VkDescriptorBufferInfo* pBufferInfos,
648         const VkBufferView* pBufferViews);
649 
650     VkResult on_vkBeginCommandBuffer(
651             android::base::BumpPool* pool,
652             VkCommandBuffer commandBuffer,
653             const VkCommandBufferBeginInfo* pBeginInfo);
654     void on_vkBeginCommandBufferAsyncGOOGLE(
655         android::base::BumpPool* pool,
656         VkCommandBuffer commandBuffer,
657         const VkCommandBufferBeginInfo* pBeginInfo);
658     void on_vkEndCommandBufferAsyncGOOGLE(
659         android::base::BumpPool* pool,
660         VkCommandBuffer commandBuffer);
661     void on_vkResetCommandBufferAsyncGOOGLE(
662         android::base::BumpPool* pool,
663         VkCommandBuffer commandBuffer,
664         VkCommandBufferResetFlags flags);
665     void on_vkCommandBufferHostSyncGOOGLE(
666         android::base::BumpPool* pool,
667         VkCommandBuffer commandBuffer,
668         uint32_t needHostSync,
669         uint32_t sequenceNumber);
670 
671     VkResult on_vkCreateImageWithRequirementsGOOGLE(
672         android::base::BumpPool* pool,
673         VkDevice device,
674         const VkImageCreateInfo* pCreateInfo,
675         const VkAllocationCallbacks* pAllocator,
676         VkImage* pImage,
677         VkMemoryRequirements* pMemoryRequirements);
678     VkResult on_vkCreateBufferWithRequirementsGOOGLE(
679         android::base::BumpPool* pool,
680         VkDevice device,
681         const VkBufferCreateInfo* pCreateInfo,
682         const VkAllocationCallbacks* pAllocator,
683         VkBuffer* pBuffer,
684         VkMemoryRequirements* pMemoryRequirements);
685 
686     void on_vkCmdBindPipeline(android::base::BumpPool* pool,
687                               VkCommandBuffer commandBuffer,
688                               VkPipelineBindPoint pipelineBindPoint,
689                               VkPipeline pipeline);
690 
691     void on_vkCmdBindDescriptorSets(android::base::BumpPool* pool,
692                                     VkCommandBuffer commandBuffer,
693                                     VkPipelineBindPoint pipelineBindPoint,
694                                     VkPipelineLayout layout,
695                                     uint32_t firstSet,
696                                     uint32_t descriptorSetCount,
697                                     const VkDescriptorSet* pDescriptorSets,
698                                     uint32_t dynamicOffsetCount,
699                                     const uint32_t* pDynamicOffsets);
700 
701     VkResult on_vkCreateRenderPass(android::base::BumpPool* pool,
702                                    VkDevice device,
703                                    const VkRenderPassCreateInfo* pCreateInfo,
704                                    const VkAllocationCallbacks* pAllocator,
705                                    VkRenderPass* pRenderPass);
706 
707     // VK_GOOGLE_gfxstream
708     void on_vkQueueHostSyncGOOGLE(
709         android::base::BumpPool* pool,
710         VkQueue queue,
711         uint32_t needHostSync,
712         uint32_t sequenceNumber);
713     void on_vkQueueSubmitAsyncGOOGLE(
714         android::base::BumpPool* pool,
715         VkQueue queue,
716         uint32_t submitCount,
717         const VkSubmitInfo* pSubmits,
718         VkFence fence);
719     void on_vkQueueWaitIdleAsyncGOOGLE(
720         android::base::BumpPool* pool,
721         VkQueue queue);
722     void on_vkQueueBindSparseAsyncGOOGLE(
723         android::base::BumpPool* pool,
724         VkQueue queue,
725         uint32_t bindInfoCount,
726         const VkBindSparseInfo* pBindInfo, VkFence fence);
727 
728     // VK_GOOGLE_gfxstream
729     void on_vkGetLinearImageLayoutGOOGLE(
730         android::base::BumpPool* pool,
731         VkDevice device,
732         VkFormat format,
733         VkDeviceSize* pOffset,
734         VkDeviceSize* pRowPitchAlignment);
735 
736     // VK_GOOGLE_gfxstream
737     void on_vkQueueFlushCommandsGOOGLE(
738         android::base::BumpPool* pool,
739         VkQueue queue,
740         VkCommandBuffer commandBuffer,
741         VkDeviceSize dataSize,
742         const void* pData);
743     void on_vkQueueCommitDescriptorSetUpdatesGOOGLE(
744         android::base::BumpPool* pool,
745         VkQueue queue,
746         uint32_t descriptorPoolCount,
747         const VkDescriptorPool* pDescriptorPools,
748         uint32_t descriptorSetCount,
749         const VkDescriptorSetLayout* pDescriptorSetLayouts,
750         const uint64_t* pDescriptorSetPoolIds,
751         const uint32_t* pDescriptorSetWhichPool,
752         const uint32_t* pDescriptorSetPendingAllocation,
753         const uint32_t* pDescriptorWriteStartingIndices,
754         uint32_t pendingDescriptorWriteCount,
755         const VkWriteDescriptorSet* pPendingDescriptorWrites);
756     void on_vkCollectDescriptorPoolIdsGOOGLE(
757         android::base::BumpPool* pool,
758         VkDevice device,
759         VkDescriptorPool descriptorPool,
760         uint32_t* pPoolIdCount,
761         uint64_t* pPoolIds);
762 
763     // Fence waits
764     VkResult waitForFence(VkFence boxed_fence, uint64_t timeout);
765 
766     VkResult getFenceStatus(VkFence boxed_fence);
767 
768     // Transformations
769     void deviceMemoryTransform_tohost(
770         VkDeviceMemory* memory, uint32_t memoryCount,
771         VkDeviceSize* offset, uint32_t offsetCount,
772         VkDeviceSize* size, uint32_t sizeCount,
773         uint32_t* typeIndex, uint32_t typeIndexCount,
774         uint32_t* typeBits, uint32_t typeBitsCount);
775     void deviceMemoryTransform_fromhost(
776         VkDeviceMemory* memory, uint32_t memoryCount,
777         VkDeviceSize* offset, uint32_t offsetCount,
778         VkDeviceSize* size, uint32_t sizeCount,
779         uint32_t* typeIndex, uint32_t typeIndexCount,
780         uint32_t* typeBits, uint32_t typeBitsCount);
781 
782     // Snapshot access
783     VkDecoderSnapshot* snapshot();
784 
785 #define DEFINE_TRANSFORMED_TYPE_PROTOTYPE(type) \
786     void transformImpl_##type##_tohost(const type*, uint32_t); \
787     void transformImpl_##type##_fromhost(const type*, uint32_t); \
788 
789 LIST_TRANSFORMED_TYPES(DEFINE_TRANSFORMED_TYPE_PROTOTYPE)
790 
791     // boxed handles
792 #define DEFINE_BOXED_DISPATCHABLE_HANDLE_API_DECL(type) \
793     type new_boxed_##type(type underlying, VulkanDispatch* dispatch, bool ownDispatch); \
794     void delete_##type(type boxed); \
795     type unbox_##type(type boxed); \
796     type unboxed_to_boxed_##type(type boxed); \
797     VulkanDispatch* dispatch_##type(type boxed); \
798 
799 #define DEFINE_BOXED_NON_DISPATCHABLE_HANDLE_API_DECL(type) \
800     type new_boxed_non_dispatchable_##type(type underlying); \
801     void delete_##type(type boxed); \
802     type unbox_##type(type boxed); \
803     type unboxed_to_boxed_non_dispatchable_##type(type boxed); \
804 
805 GOLDFISH_VK_LIST_DISPATCHABLE_HANDLE_TYPES(DEFINE_BOXED_DISPATCHABLE_HANDLE_API_DECL)
806 GOLDFISH_VK_LIST_NON_DISPATCHABLE_HANDLE_TYPES(DEFINE_BOXED_NON_DISPATCHABLE_HANDLE_API_DECL)
807 
808 private:
809     class Impl;
810     std::unique_ptr<Impl> mImpl;
811 };
812 
813 #define MAKE_HANDLE_MAPPING_FOREACH(type_name, map_impl, map_to_u64_impl, map_from_u64_impl) \
814     void mapHandles_##type_name(type_name* handles, size_t count) override { \
815         for (size_t i = 0; i < count; ++i) { \
816             map_impl; \
817         } \
818     } \
819     void mapHandles_##type_name##_u64(const type_name* handles, uint64_t* handle_u64s, size_t count) override { \
820         for (size_t i = 0; i < count; ++i) { \
821             map_to_u64_impl; \
822         } \
823     } \
824     void mapHandles_u64_##type_name(const uint64_t* handle_u64s, type_name* handles, size_t count) override { \
825         for (size_t i = 0; i < count; ++i) { \
826             map_from_u64_impl; \
827         } \
828     } \
829 
830 #define BOXED_DISPATCHABLE_UNWRAP_IMPL(type_name) \
831     MAKE_HANDLE_MAPPING_FOREACH(type_name, \
832         if (handles[i]) { handles[i] = m_state->unbox_##type_name(handles[i]); } else { handles[i] = (type_name)nullptr; } ;, \
833         if (handles[i]) { handle_u64s[i] = (uint64_t)m_state->unbox_##type_name(handles[i]); } else { handle_u64s[i] = 0; }, \
834         if (handle_u64s[i]) { handles[i] = m_state->unbox_##type_name((type_name)(uintptr_t)handle_u64s[i]); } else { handles[i] = (type_name)nullptr; })
835 
836 #define BOXED_NON_DISPATCHABLE_UNWRAP_IMPL(type_name) \
837     MAKE_HANDLE_MAPPING_FOREACH(type_name, \
838         if (handles[i]) { handles[i] = m_state->unbox_##type_name(handles[i]); } else { handles[i] = (type_name)nullptr; } ;, \
839         if (handles[i]) { handle_u64s[i] = (uint64_t)m_state->unbox_##type_name(handles[i]); } else { handle_u64s[i] = 0; }, \
840         if (handle_u64s[i]) { handles[i] = m_state->unbox_##type_name((type_name)(uintptr_t)handle_u64s[i]); } else { handles[i] = (type_name)nullptr; })
841 
842 class BoxedHandleUnwrapMapping : public VulkanHandleMapping {
843 public:
BoxedHandleUnwrapMapping(VkDecoderGlobalState * state)844     BoxedHandleUnwrapMapping(VkDecoderGlobalState* state) : VulkanHandleMapping(state) { }
~BoxedHandleUnwrapMapping()845     virtual ~BoxedHandleUnwrapMapping() { }
846     GOLDFISH_VK_LIST_DISPATCHABLE_HANDLE_TYPES(BOXED_DISPATCHABLE_UNWRAP_IMPL)
847     GOLDFISH_VK_LIST_NON_DISPATCHABLE_HANDLE_TYPES(BOXED_NON_DISPATCHABLE_UNWRAP_IMPL)
848 };
849 
850 #define BOXED_DISPATCHABLE_WRAP_IMPL(type_name) \
851     MAKE_HANDLE_MAPPING_FOREACH(type_name, \
852         if (handles[i]) { handles[i] = m_state->unboxed_to_boxed_##type_name(handles[i]); } else { handles[i] = (type_name)nullptr; } ;, \
853         if (handles[i]) { handle_u64s[i] = (uint64_t)m_state->unboxed_to_boxed_##type_name(handles[i]); } else { handle_u64s[i] = 0; }, \
854         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; })
855 
856 #define BOXED_NON_DISPATCHABLE_WRAP_IMPL(type_name) \
857     MAKE_HANDLE_MAPPING_FOREACH(type_name, \
858         if (handles[i]) { handles[i] = m_state->unboxed_to_boxed_non_dispatchable_##type_name(handles[i]); } else { handles[i] = (type_name)nullptr; } ;, \
859         if (handles[i]) { handle_u64s[i] = (uint64_t)m_state->unboxed_to_boxed_non_dispatchable_##type_name(handles[i]); } else { handle_u64s[i] = 0; }, \
860         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; })
861 
862 class BoxedHandleWrapMapping : public VulkanHandleMapping {
863 public:
BoxedHandleWrapMapping(VkDecoderGlobalState * state)864     BoxedHandleWrapMapping(VkDecoderGlobalState* state) : VulkanHandleMapping(state) { }
~BoxedHandleWrapMapping()865     virtual ~BoxedHandleWrapMapping() { }
866     GOLDFISH_VK_LIST_DISPATCHABLE_HANDLE_TYPES(BOXED_DISPATCHABLE_WRAP_IMPL)
867     GOLDFISH_VK_LIST_NON_DISPATCHABLE_HANDLE_TYPES(BOXED_NON_DISPATCHABLE_WRAP_IMPL)
868 };
869 
870 // Not used, so we do not define.
871 #define BOXED_DISPATCHABLE_CREATE_IMPL(type_name) \
872     MAKE_HANDLE_MAPPING_FOREACH(type_name, \
873         (void)handles[i], \
874         (void)handle_u64s[i], \
875         (void)handles[i];)
876 
877 // Not used, so we do not define.
878 #define BOXED_DISPATCHABLE_DESTROY_IMPL(type_name) \
879     MAKE_HANDLE_MAPPING_FOREACH(type_name, \
880         (void)handles[i], \
881         (void)handle_u64s[i], \
882         (void)handles[i];)
883 
884 // We only use the create/destroy mappings for non dispatchable handles.
885 #define BOXED_NON_DISPATCHABLE_CREATE_IMPL(type_name) \
886     MAKE_HANDLE_MAPPING_FOREACH(type_name, \
887         handles[i] = new_boxed_non_dispatchable_##type_name(handles[i]);, \
888         handle_u64s[i] = (uint64_t)new_boxed_non_dispatchable_##type_name(handles[i]), \
889         handles[i] = (type_name)new_boxed_non_dispatchable_##type_name((type_name)(uintptr_t)handle_u64s[i]);)
890 
891 #define BOXED_NON_DISPATCHABLE_DESTROY_IMPL(type_name) \
892     MAKE_HANDLE_MAPPING_FOREACH(type_name, \
893         delete_##type_name(handles[i]), \
894         (void)handle_u64s[i]; delete_##type_name(handles[i]), \
895         (void)handles[i]; delete_##type_name((type_name)handle_u64s[i]))
896 
897 #define BOXED_NON_DISPATCHABLE_UNWRAP_AND_DELETE_IMPL(type_name) \
898     MAKE_HANDLE_MAPPING_FOREACH(type_name, \
899         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; } ;, \
900         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; }, \
901         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; })
902 
903 class BoxedHandleCreateMapping : public VulkanHandleMapping {
904 public:
BoxedHandleCreateMapping(VkDecoderGlobalState * state)905     BoxedHandleCreateMapping(VkDecoderGlobalState* state) : VulkanHandleMapping(state) { }
~BoxedHandleCreateMapping()906     virtual ~BoxedHandleCreateMapping() { }
907     GOLDFISH_VK_LIST_DISPATCHABLE_HANDLE_TYPES(BOXED_DISPATCHABLE_CREATE_IMPL)
908     GOLDFISH_VK_LIST_NON_DISPATCHABLE_HANDLE_TYPES(BOXED_NON_DISPATCHABLE_CREATE_IMPL)
909 };
910 
911 class BoxedHandleDestroyMapping : public VulkanHandleMapping {
912 public:
BoxedHandleDestroyMapping(VkDecoderGlobalState * state)913     BoxedHandleDestroyMapping(VkDecoderGlobalState* state) : VulkanHandleMapping(state) { }
~BoxedHandleDestroyMapping()914     virtual ~BoxedHandleDestroyMapping() { }
915     GOLDFISH_VK_LIST_DISPATCHABLE_HANDLE_TYPES(BOXED_DISPATCHABLE_DESTROY_IMPL)
916     GOLDFISH_VK_LIST_NON_DISPATCHABLE_HANDLE_TYPES(BOXED_NON_DISPATCHABLE_DESTROY_IMPL)
917 };
918 
919 class BoxedHandleUnwrapAndDeleteMapping : public VulkanHandleMapping {
920 public:
BoxedHandleUnwrapAndDeleteMapping(VkDecoderGlobalState * state)921     BoxedHandleUnwrapAndDeleteMapping(VkDecoderGlobalState* state) : VulkanHandleMapping(state) { }
~BoxedHandleUnwrapAndDeleteMapping()922     virtual ~BoxedHandleUnwrapAndDeleteMapping() { }
923     GOLDFISH_VK_LIST_DISPATCHABLE_HANDLE_TYPES(BOXED_DISPATCHABLE_DESTROY_IMPL)
924     GOLDFISH_VK_LIST_NON_DISPATCHABLE_HANDLE_TYPES(BOXED_NON_DISPATCHABLE_UNWRAP_AND_DELETE_IMPL)
925 };
926 
927 #define HANDLE_MAPPING_DECLS(type_name) \
928     void mapHandles_##type_name(type_name* handles, size_t count) override; \
929     void mapHandles_##type_name##_u64(const type_name* handles, uint64_t* handle_u64s, size_t count) override; \
930     void mapHandles_u64_##type_name(const uint64_t* handle_u64s, type_name* handles, size_t count) override; \
931 
932 class BoxedHandleUnwrapAndDeletePreserveBoxedMapping : public VulkanHandleMapping {
933 public:
BoxedHandleUnwrapAndDeletePreserveBoxedMapping(VkDecoderGlobalState * state)934     BoxedHandleUnwrapAndDeletePreserveBoxedMapping(VkDecoderGlobalState* state) : VulkanHandleMapping(state) { }
935     void setup(android::base::BumpPool* pool, uint64_t** bufPtr);
~BoxedHandleUnwrapAndDeletePreserveBoxedMapping()936     virtual ~BoxedHandleUnwrapAndDeletePreserveBoxedMapping() { }
937 
938     GOLDFISH_VK_LIST_DISPATCHABLE_HANDLE_TYPES(HANDLE_MAPPING_DECLS)
939     GOLDFISH_VK_LIST_NON_DISPATCHABLE_HANDLE_TYPES(HANDLE_MAPPING_DECLS)
940 
941 private:
942     void allocPreserve(size_t count);
943 
944     android::base::BumpPool* mPool = nullptr;
945     uint64_t** mPreserveBufPtr = nullptr;
946 };
947 
948 } // namespace goldfish_vk
949