1 // Copyright (C) 2018 The Android Open Source Project
2 // Copyright (C) 2018 Google Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 #pragma once
16 
17 #include "android/base/Tracing.h"
18 
19 #include <vulkan/vulkan.h>
20 
21 #include "VulkanHandleMapping.h"
22 #include "VulkanHandles.h"
23 #include <memory>
24 
25 #include "goldfish_vk_transform_guest.h"
26 
27 struct EmulatorFeatureInfo;
28 
29 class HostConnection;
30 
31 namespace goldfish_vk {
32 
33 class VkEncoder;
34 
35 class ResourceTracker {
36 public:
37     ResourceTracker();
38     virtual ~ResourceTracker();
39     static ResourceTracker* get();
40     VulkanHandleMapping* createMapping();
41     VulkanHandleMapping* unwrapMapping();
42     VulkanHandleMapping* destroyMapping();
43     VulkanHandleMapping* defaultMapping();
44 
45     using HostConnectionGetFunc = HostConnection* (*)();
46     using VkEncoderGetFunc = VkEncoder* (*)(HostConnection*);
47 
48     struct ThreadingCallbacks {
49         HostConnectionGetFunc hostConnectionGetFunc = 0;
50         VkEncoderGetFunc vkEncoderGetFunc = 0;
51     };
52 
53 #define HANDLE_REGISTER_DECL(type) \
54     void register_##type(type); \
55     void unregister_##type(type); \
56 
57     GOLDFISH_VK_LIST_HANDLE_TYPES(HANDLE_REGISTER_DECL)
58 
59     VkResult on_vkEnumerateInstanceExtensionProperties(
60         void* context,
61         VkResult input_result,
62         const char* pLayerName,
63         uint32_t* pPropertyCount,
64         VkExtensionProperties* pProperties);
65 
66     VkResult on_vkEnumerateDeviceExtensionProperties(
67         void* context,
68         VkResult input_result,
69         VkPhysicalDevice physicalDevice,
70         const char* pLayerName,
71         uint32_t* pPropertyCount,
72         VkExtensionProperties* pProperties);
73 
74     VkResult on_vkEnumeratePhysicalDevices(
75         void* context, VkResult input_result,
76         VkInstance instance, uint32_t* pPhysicalDeviceCount,
77         VkPhysicalDevice* pPhysicalDevices);
78 
79     void on_vkGetPhysicalDeviceMemoryProperties(
80         void* context,
81         VkPhysicalDevice physicalDevice,
82         VkPhysicalDeviceMemoryProperties* pMemoryProperties);
83     void on_vkGetPhysicalDeviceMemoryProperties2(
84         void* context,
85         VkPhysicalDevice physicalDevice,
86         VkPhysicalDeviceMemoryProperties2* pMemoryProperties);
87     void on_vkGetPhysicalDeviceMemoryProperties2KHR(
88         void* context,
89         VkPhysicalDevice physicalDevice,
90         VkPhysicalDeviceMemoryProperties2* pMemoryProperties);
91 
92     VkResult on_vkCreateInstance(
93         void* context,
94         VkResult input_result,
95         const VkInstanceCreateInfo* createInfo,
96         const VkAllocationCallbacks* pAllocator,
97         VkInstance* pInstance);
98     VkResult on_vkCreateDevice(
99         void* context,
100         VkResult input_result,
101         VkPhysicalDevice physicalDevice,
102         const VkDeviceCreateInfo* pCreateInfo,
103         const VkAllocationCallbacks* pAllocator,
104         VkDevice* pDevice);
105     void on_vkDestroyDevice_pre(
106         void* context,
107         VkDevice device,
108         const VkAllocationCallbacks* pAllocator);
109 
110     VkResult on_vkAllocateMemory(
111         void* context,
112         VkResult input_result,
113         VkDevice device,
114         const VkMemoryAllocateInfo* pAllocateInfo,
115         const VkAllocationCallbacks* pAllocator,
116         VkDeviceMemory* pMemory);
117     void on_vkFreeMemory(
118         void* context,
119         VkDevice device,
120         VkDeviceMemory memory,
121         const VkAllocationCallbacks* pAllocator);
122 
123     VkResult on_vkMapMemory(
124         void* context,
125         VkResult input_result,
126         VkDevice device,
127         VkDeviceMemory memory,
128         VkDeviceSize offset,
129         VkDeviceSize size,
130         VkMemoryMapFlags,
131         void** ppData);
132 
133     void on_vkUnmapMemory(
134         void* context,
135         VkDevice device,
136         VkDeviceMemory memory);
137 
138     VkResult on_vkCreateImage(
139         void* context, VkResult input_result,
140         VkDevice device, const VkImageCreateInfo *pCreateInfo,
141         const VkAllocationCallbacks *pAllocator,
142         VkImage *pImage);
143     void on_vkDestroyImage(
144         void* context,
145         VkDevice device, VkImage image, const VkAllocationCallbacks *pAllocator);
146 
147     void on_vkGetImageMemoryRequirements(
148         void *context, VkDevice device, VkImage image,
149         VkMemoryRequirements *pMemoryRequirements);
150     void on_vkGetImageMemoryRequirements2(
151         void *context, VkDevice device, const VkImageMemoryRequirementsInfo2 *pInfo,
152         VkMemoryRequirements2 *pMemoryRequirements);
153     void on_vkGetImageMemoryRequirements2KHR(
154         void *context, VkDevice device, const VkImageMemoryRequirementsInfo2 *pInfo,
155         VkMemoryRequirements2 *pMemoryRequirements);
156 
157     VkResult on_vkBindImageMemory(
158         void* context, VkResult input_result,
159         VkDevice device, VkImage image, VkDeviceMemory memory,
160         VkDeviceSize memoryOffset);
161     VkResult on_vkBindImageMemory2(
162         void* context, VkResult input_result,
163         VkDevice device, uint32_t bindingCount, const VkBindImageMemoryInfo* pBindInfos);
164     VkResult on_vkBindImageMemory2KHR(
165         void* context, VkResult input_result,
166         VkDevice device, uint32_t bindingCount, const VkBindImageMemoryInfo* pBindInfos);
167 
168     VkResult on_vkCreateBuffer(
169         void* context, VkResult input_result,
170         VkDevice device, const VkBufferCreateInfo *pCreateInfo,
171         const VkAllocationCallbacks *pAllocator,
172         VkBuffer *pBuffer);
173     void on_vkDestroyBuffer(
174         void* context,
175         VkDevice device, VkBuffer buffer, const VkAllocationCallbacks *pAllocator);
176 
177     void on_vkGetBufferMemoryRequirements(
178         void* context, VkDevice device, VkBuffer buffer, VkMemoryRequirements *pMemoryRequirements);
179     void on_vkGetBufferMemoryRequirements2(
180         void* context, VkDevice device, const VkBufferMemoryRequirementsInfo2* pInfo,
181         VkMemoryRequirements2* pMemoryRequirements);
182     void on_vkGetBufferMemoryRequirements2KHR(
183         void* context, VkDevice device, const VkBufferMemoryRequirementsInfo2* pInfo,
184         VkMemoryRequirements2* pMemoryRequirements);
185 
186     VkResult on_vkBindBufferMemory(
187         void* context, VkResult input_result,
188         VkDevice device, VkBuffer buffer, VkDeviceMemory memory, VkDeviceSize memoryOffset);
189     VkResult on_vkBindBufferMemory2(
190         void* context, VkResult input_result,
191         VkDevice device, uint32_t bindInfoCount, const VkBindBufferMemoryInfo *pBindInfos);
192     VkResult on_vkBindBufferMemory2KHR(
193         void* context, VkResult input_result,
194         VkDevice device, uint32_t bindInfoCount, const VkBindBufferMemoryInfo *pBindInfos);
195 
196     VkResult on_vkCreateSemaphore(
197         void* context, VkResult,
198         VkDevice device, const VkSemaphoreCreateInfo* pCreateInfo,
199         const VkAllocationCallbacks* pAllocator,
200         VkSemaphore* pSemaphore);
201     void on_vkDestroySemaphore(
202         void* context,
203         VkDevice device, VkSemaphore semaphore, const VkAllocationCallbacks *pAllocator);
204     VkResult on_vkGetSemaphoreFdKHR(
205         void* context, VkResult,
206         VkDevice device,
207         const VkSemaphoreGetFdInfoKHR* pGetFdInfo,
208         int* pFd);
209     VkResult on_vkImportSemaphoreFdKHR(
210         void* context, VkResult,
211         VkDevice device,
212         const VkImportSemaphoreFdInfoKHR* pImportSemaphoreFdInfo);
213 
214     VkResult on_vkQueueSubmit(
215         void* context, VkResult input_result,
216         VkQueue queue, uint32_t submitCount, const VkSubmitInfo* pSubmits, VkFence fence);
217 
218     VkResult on_vkQueueWaitIdle(
219         void* context, VkResult input_result,
220         VkQueue queue);
221 
222     void unwrap_VkNativeBufferANDROID(
223         const VkImageCreateInfo* pCreateInfo,
224         VkImageCreateInfo* local_pCreateInfo);
225     void unwrap_vkAcquireImageANDROID_nativeFenceFd(int fd, int* fd_out);
226 
227 #ifdef VK_USE_PLATFORM_FUCHSIA
228     VkResult on_vkGetMemoryZirconHandleFUCHSIA(
229         void* context, VkResult input_result,
230         VkDevice device,
231         const VkMemoryGetZirconHandleInfoFUCHSIA* pInfo,
232         uint32_t* pHandle);
233     VkResult on_vkGetMemoryZirconHandlePropertiesFUCHSIA(
234         void* context, VkResult input_result,
235         VkDevice device,
236         VkExternalMemoryHandleTypeFlagBits handleType,
237         uint32_t handle,
238         VkMemoryZirconHandlePropertiesFUCHSIA* pProperties);
239     VkResult on_vkGetSemaphoreZirconHandleFUCHSIA(
240         void* context, VkResult input_result,
241         VkDevice device,
242         const VkSemaphoreGetZirconHandleInfoFUCHSIA* pInfo,
243         uint32_t* pHandle);
244     VkResult on_vkImportSemaphoreZirconHandleFUCHSIA(
245         void* context, VkResult input_result,
246         VkDevice device,
247         const VkImportSemaphoreZirconHandleInfoFUCHSIA* pInfo);
248     VkResult on_vkCreateBufferCollectionFUCHSIA(
249         void* context, VkResult input_result,
250         VkDevice device,
251         const VkBufferCollectionCreateInfoFUCHSIA* pInfo,
252         const VkAllocationCallbacks* pAllocator,
253         VkBufferCollectionFUCHSIA* pCollection);
254     void on_vkDestroyBufferCollectionFUCHSIA(
255         void* context, VkResult input_result,
256         VkDevice device,
257         VkBufferCollectionFUCHSIA collection,
258         const VkAllocationCallbacks* pAllocator);
259     VkResult on_vkSetBufferCollectionConstraintsFUCHSIA(
260         void* context, VkResult input_result,
261         VkDevice device,
262         VkBufferCollectionFUCHSIA collection,
263         const VkImageCreateInfo* pImageInfo);
264     VkResult on_vkGetBufferCollectionPropertiesFUCHSIA(
265         void* context, VkResult input_result,
266         VkDevice device,
267         VkBufferCollectionFUCHSIA collection,
268         VkBufferCollectionPropertiesFUCHSIA* pProperties);
269 #endif
270 
271     VkResult on_vkGetAndroidHardwareBufferPropertiesANDROID(
272         void* context, VkResult input_result,
273         VkDevice device,
274         const AHardwareBuffer* buffer,
275         VkAndroidHardwareBufferPropertiesANDROID* pProperties);
276     VkResult on_vkGetMemoryAndroidHardwareBufferANDROID(
277         void* context, VkResult input_result,
278         VkDevice device,
279         const VkMemoryGetAndroidHardwareBufferInfoANDROID *pInfo,
280         struct AHardwareBuffer** pBuffer);
281 
282     VkResult on_vkCreateSamplerYcbcrConversion(
283         void* context, VkResult input_result,
284         VkDevice device,
285         const VkSamplerYcbcrConversionCreateInfo* pCreateInfo,
286         const VkAllocationCallbacks* pAllocator,
287         VkSamplerYcbcrConversion* pYcbcrConversion);
288     void on_vkDestroySamplerYcbcrConversion(
289         void* context,
290         VkDevice device,
291         VkSamplerYcbcrConversion ycbcrConversion,
292         const VkAllocationCallbacks* pAllocator);
293     VkResult on_vkCreateSamplerYcbcrConversionKHR(
294         void* context, VkResult input_result,
295         VkDevice device,
296         const VkSamplerYcbcrConversionCreateInfo* pCreateInfo,
297         const VkAllocationCallbacks* pAllocator,
298         VkSamplerYcbcrConversion* pYcbcrConversion);
299     void on_vkDestroySamplerYcbcrConversionKHR(
300         void* context,
301         VkDevice device,
302         VkSamplerYcbcrConversion ycbcrConversion,
303         const VkAllocationCallbacks* pAllocator);
304 
305     VkResult on_vkCreateSampler(
306         void* context, VkResult input_result,
307         VkDevice device,
308         const VkSamplerCreateInfo* pCreateInfo,
309         const VkAllocationCallbacks* pAllocator,
310         VkSampler* pSampler);
311 
312     void on_vkGetPhysicalDeviceExternalFenceProperties(
313         void* context,
314         VkPhysicalDevice physicalDevice,
315         const VkPhysicalDeviceExternalFenceInfo* pExternalFenceInfo,
316         VkExternalFenceProperties* pExternalFenceProperties);
317 
318     void on_vkGetPhysicalDeviceExternalFencePropertiesKHR(
319         void* context,
320         VkPhysicalDevice physicalDevice,
321         const VkPhysicalDeviceExternalFenceInfo* pExternalFenceInfo,
322         VkExternalFenceProperties* pExternalFenceProperties);
323 
324     VkResult on_vkCreateFence(
325         void* context,
326         VkResult input_result,
327         VkDevice device,
328         const VkFenceCreateInfo* pCreateInfo,
329         const VkAllocationCallbacks* pAllocator, VkFence* pFence);
330 
331     void on_vkDestroyFence(
332         void* context,
333         VkDevice device,
334         VkFence fence,
335         const VkAllocationCallbacks* pAllocator);
336 
337     VkResult on_vkResetFences(
338         void* context,
339         VkResult input_result,
340         VkDevice device,
341         uint32_t fenceCount,
342         const VkFence* pFences);
343 
344     VkResult on_vkImportFenceFdKHR(
345         void* context,
346         VkResult input_result,
347         VkDevice device,
348         const VkImportFenceFdInfoKHR* pImportFenceFdInfo);
349 
350     VkResult on_vkGetFenceFdKHR(
351         void* context,
352         VkResult input_result,
353         VkDevice device,
354         const VkFenceGetFdInfoKHR* pGetFdInfo,
355         int* pFd);
356 
357     VkResult on_vkWaitForFences(
358         void* context,
359         VkResult input_result,
360         VkDevice device,
361         uint32_t fenceCount,
362         const VkFence* pFences,
363         VkBool32 waitAll,
364         uint64_t timeout);
365 
366     VkResult on_vkCreateDescriptorPool(
367         void* context,
368         VkResult input_result,
369         VkDevice device,
370         const VkDescriptorPoolCreateInfo* pCreateInfo,
371         const VkAllocationCallbacks* pAllocator,
372         VkDescriptorPool* pDescriptorPool);
373 
374     void on_vkDestroyDescriptorPool(
375         void* context,
376         VkDevice device,
377         VkDescriptorPool descriptorPool,
378         const VkAllocationCallbacks* pAllocator);
379 
380     VkResult on_vkResetDescriptorPool(
381         void* context,
382         VkResult input_result,
383         VkDevice device,
384         VkDescriptorPool descriptorPool,
385         VkDescriptorPoolResetFlags flags);
386 
387     VkResult on_vkAllocateDescriptorSets(
388         void* context,
389         VkResult input_result,
390         VkDevice                                    device,
391         const VkDescriptorSetAllocateInfo*          pAllocateInfo,
392         VkDescriptorSet*                            pDescriptorSets);
393 
394     VkResult on_vkFreeDescriptorSets(
395         void* context,
396         VkResult input_result,
397         VkDevice                                    device,
398         VkDescriptorPool                            descriptorPool,
399         uint32_t                                    descriptorSetCount,
400         const VkDescriptorSet*                      pDescriptorSets);
401 
402     VkResult on_vkCreateDescriptorSetLayout(
403         void* context,
404         VkResult input_result,
405         VkDevice device,
406         const VkDescriptorSetLayoutCreateInfo* pCreateInfo,
407         const VkAllocationCallbacks* pAllocator,
408         VkDescriptorSetLayout* pSetLayout);
409 
410     void on_vkUpdateDescriptorSets(
411         void* context,
412         VkDevice device,
413         uint32_t descriptorWriteCount,
414         const VkWriteDescriptorSet* pDescriptorWrites,
415         uint32_t descriptorCopyCount,
416         const VkCopyDescriptorSet* pDescriptorCopies);
417 
418     VkResult on_vkMapMemoryIntoAddressSpaceGOOGLE_pre(
419         void* context,
420         VkResult input_result,
421         VkDevice device,
422         VkDeviceMemory memory,
423         uint64_t* pAddress);
424     VkResult on_vkMapMemoryIntoAddressSpaceGOOGLE(
425         void* context,
426         VkResult input_result,
427         VkDevice device,
428         VkDeviceMemory memory,
429         uint64_t* pAddress);
430 
431     VkResult on_vkCreateDescriptorUpdateTemplate(
432         void* context, VkResult input_result,
433         VkDevice device,
434         const VkDescriptorUpdateTemplateCreateInfo* pCreateInfo,
435         const VkAllocationCallbacks* pAllocator,
436         VkDescriptorUpdateTemplate* pDescriptorUpdateTemplate);
437 
438     VkResult on_vkCreateDescriptorUpdateTemplateKHR(
439         void* context, VkResult input_result,
440         VkDevice device,
441         const VkDescriptorUpdateTemplateCreateInfo* pCreateInfo,
442         const VkAllocationCallbacks* pAllocator,
443         VkDescriptorUpdateTemplate* pDescriptorUpdateTemplate);
444 
445     void on_vkUpdateDescriptorSetWithTemplate(
446         void* context,
447         VkDevice device,
448         VkDescriptorSet descriptorSet,
449         VkDescriptorUpdateTemplate descriptorUpdateTemplate,
450         const void* pData);
451 
452     VkResult on_vkGetPhysicalDeviceImageFormatProperties2(
453         void* context, VkResult input_result,
454         VkPhysicalDevice physicalDevice,
455         const VkPhysicalDeviceImageFormatInfo2* pImageFormatInfo,
456         VkImageFormatProperties2* pImageFormatProperties);
457 
458     VkResult on_vkGetPhysicalDeviceImageFormatProperties2KHR(
459         void* context, VkResult input_result,
460         VkPhysicalDevice physicalDevice,
461         const VkPhysicalDeviceImageFormatInfo2* pImageFormatInfo,
462         VkImageFormatProperties2* pImageFormatProperties);
463 
464     uint32_t syncEncodersForCommandBuffer(VkCommandBuffer commandBuffer, VkEncoder* current);
465 
466     VkResult on_vkBeginCommandBuffer(
467         void* context, VkResult input_result,
468         VkCommandBuffer commandBuffer,
469         const VkCommandBufferBeginInfo* pBeginInfo);
470     VkResult on_vkEndCommandBuffer(
471         void* context, VkResult input_result,
472         VkCommandBuffer commandBuffer);
473     VkResult on_vkResetCommandBuffer(
474         void* context, VkResult input_result,
475         VkCommandBuffer commandBuffer,
476         VkCommandBufferResetFlags flags);
477 
478     VkResult on_vkCreateImageView(
479         void* context, VkResult input_result,
480         VkDevice device,
481         const VkImageViewCreateInfo* pCreateInfo,
482         const VkAllocationCallbacks* pAllocator,
483         VkImageView* pView);
484 
485     bool isMemoryTypeHostVisible(VkDevice device, uint32_t typeIndex) const;
486     uint8_t* getMappedPointer(VkDeviceMemory memory);
487     VkDeviceSize getMappedSize(VkDeviceMemory memory);
488     VkDeviceSize getNonCoherentExtendedSize(VkDevice device, VkDeviceSize basicSize) const;
489     bool isValidMemoryRange(const VkMappedMemoryRange& range) const;
490     void setupFeatures(const EmulatorFeatureInfo* features);
491     void setThreadingCallbacks(const ThreadingCallbacks& callbacks);
492     bool hostSupportsVulkan() const;
493     bool usingDirectMapping() const;
494     uint32_t getStreamFeatures() const;
495     uint32_t getApiVersionFromInstance(VkInstance instance) const;
496     uint32_t getApiVersionFromDevice(VkDevice device) const;
497     bool hasInstanceExtension(VkInstance instance, const std::string& name) const;
498     bool hasDeviceExtension(VkDevice instance, const std::string& name) const;
499 
500     // Transforms
501     void deviceMemoryTransform_tohost(
502         VkDeviceMemory* memory, uint32_t memoryCount,
503         VkDeviceSize* offset, uint32_t offsetCount,
504         VkDeviceSize* size, uint32_t sizeCount,
505         uint32_t* typeIndex, uint32_t typeIndexCount,
506         uint32_t* typeBits, uint32_t typeBitsCount);
507     void deviceMemoryTransform_fromhost(
508         VkDeviceMemory* memory, uint32_t memoryCount,
509         VkDeviceSize* offset, uint32_t offsetCount,
510         VkDeviceSize* size, uint32_t sizeCount,
511         uint32_t* typeIndex, uint32_t typeIndexCount,
512         uint32_t* typeBits, uint32_t typeBitsCount);
513 
514 #define DEFINE_TRANSFORMED_TYPE_PROTOTYPE(type) \
515     void transformImpl_##type##_tohost(const type*, uint32_t); \
516     void transformImpl_##type##_fromhost(const type*, uint32_t); \
517 
518 LIST_TRANSFORMED_TYPES(DEFINE_TRANSFORMED_TYPE_PROTOTYPE)
519 
520   private:
521     class Impl;
522     std::unique_ptr<Impl> mImpl;
523 };
524 
525 } // namespace goldfish_vk
526