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