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 <vulkan/vulkan.h> 18 19 #define VIRTUAL_HOST_VISIBLE_HEAP_SIZE 512ULL * (1048576ULL) 20 21 struct EmulatorFeatureInfo; 22 23 namespace android { 24 namespace base { 25 namespace guest { 26 27 class SubAllocator; 28 29 } // namespace guest 30 } // namespace base 31 } // namespace android 32 33 namespace goldfish_vk { 34 35 class VkEncoder; 36 37 struct HostVisibleMemoryVirtualizationInfo { 38 bool initialized = false; 39 bool memoryPropertiesSupported; 40 bool directMemSupported; 41 bool virtualizationSupported; 42 bool virtioGpuNextSupported; 43 44 VkPhysicalDevice physicalDevice; 45 46 VkPhysicalDeviceMemoryProperties hostMemoryProperties; 47 VkPhysicalDeviceMemoryProperties guestMemoryProperties; 48 49 uint32_t memoryTypeIndexMappingToHost[VK_MAX_MEMORY_TYPES]; 50 uint32_t memoryHeapIndexMappingToHost[VK_MAX_MEMORY_TYPES]; 51 52 uint32_t memoryTypeIndexMappingFromHost[VK_MAX_MEMORY_TYPES]; 53 uint32_t memoryHeapIndexMappingFromHost[VK_MAX_MEMORY_TYPES]; 54 55 bool memoryTypeBitsShouldAdvertiseBoth[VK_MAX_MEMORY_TYPES]; 56 }; 57 58 bool canFitVirtualHostVisibleMemoryInfo( 59 const VkPhysicalDeviceMemoryProperties* memoryProperties); 60 61 void initHostVisibleMemoryVirtualizationInfo( 62 VkPhysicalDevice physicalDevice, 63 const VkPhysicalDeviceMemoryProperties* memoryProperties, 64 const EmulatorFeatureInfo* featureInfo, 65 HostVisibleMemoryVirtualizationInfo* info_out); 66 67 bool isHostVisibleMemoryTypeIndexForGuest( 68 const HostVisibleMemoryVirtualizationInfo* info, 69 uint32_t index); 70 71 bool isDeviceLocalMemoryTypeIndexForGuest( 72 const HostVisibleMemoryVirtualizationInfo* info, 73 uint32_t index); 74 75 struct HostMemAlloc { 76 bool initialized = false; 77 VkResult initResult = VK_SUCCESS; 78 VkDevice device = nullptr; 79 uint32_t memoryTypeIndex = 0; 80 VkDeviceSize nonCoherentAtomSize = 0; 81 VkDeviceMemory memory = VK_NULL_HANDLE; 82 VkDeviceSize allocSize = 0; 83 VkDeviceSize mappedSize = 0; 84 uint8_t* mappedPtr = nullptr; 85 android::base::guest::SubAllocator* subAlloc = nullptr; 86 int fd = -1; 87 uint64_t memoryAddr = 0; 88 size_t memorySize = 0; 89 }; 90 91 VkResult finishHostMemAllocInit( 92 VkEncoder* enc, 93 VkDevice device, 94 uint32_t memoryTypeIndex, 95 VkDeviceSize nonCoherentAtomSize, 96 VkDeviceSize allocSize, 97 VkDeviceSize mappedSize, 98 uint8_t* mappedPtr, 99 HostMemAlloc* out); 100 101 void destroyHostMemAlloc( 102 bool freeMemorySyncSupported, 103 VkEncoder* enc, 104 VkDevice device, 105 HostMemAlloc* toDestroy); 106 107 struct SubAlloc { 108 uint8_t* mappedPtr = nullptr; 109 VkDeviceSize subAllocSize = 0; 110 VkDeviceSize subMappedSize = 0; 111 112 VkDeviceMemory baseMemory = VK_NULL_HANDLE; 113 VkDeviceSize baseOffset = 0; 114 android::base::guest::SubAllocator* subAlloc = nullptr; 115 VkDeviceMemory subMemory = VK_NULL_HANDLE; 116 }; 117 118 void subAllocHostMemory( 119 HostMemAlloc* alloc, 120 const VkMemoryAllocateInfo* pAllocateInfo, 121 SubAlloc* out); 122 123 void subFreeHostMemory(SubAlloc* toFree); 124 125 bool canSubAlloc(android::base::guest::SubAllocator* subAlloc, VkDeviceSize size); 126 127 bool isNoFlagsMemoryTypeIndexForGuest( 128 const HostVisibleMemoryVirtualizationInfo* info, 129 uint32_t index); 130 131 } // namespace goldfish_vk 132