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 rendernodeFd = -1; 87 bool boCreated = false; 88 uint32_t boHandle = 0; 89 uint64_t memoryAddr = 0; 90 size_t memorySize = 0; 91 bool isDeviceAddressMemoryAllocation = false; 92 bool isDedicated = false; 93 }; 94 95 VkResult finishHostMemAllocInit( 96 VkEncoder* enc, 97 VkDevice device, 98 uint32_t memoryTypeIndex, 99 VkDeviceSize nonCoherentAtomSize, 100 VkDeviceSize allocSize, 101 VkDeviceSize mappedSize, 102 uint8_t* mappedPtr, 103 HostMemAlloc* out); 104 105 void destroyHostMemAlloc( 106 bool freeMemorySyncSupported, 107 VkEncoder* enc, 108 VkDevice device, 109 HostMemAlloc* toDestroy, 110 bool doLock); 111 112 struct SubAlloc { 113 uint8_t* mappedPtr = nullptr; 114 VkDeviceSize subAllocSize = 0; 115 VkDeviceSize subMappedSize = 0; 116 117 VkDeviceMemory baseMemory = VK_NULL_HANDLE; 118 VkDeviceSize baseOffset = 0; 119 android::base::guest::SubAllocator* subAlloc = nullptr; 120 VkDeviceMemory subMemory = VK_NULL_HANDLE; 121 bool isDeviceAddressMemoryAllocation = false; 122 uint32_t memoryTypeIndex = 0; 123 }; 124 125 void subAllocHostMemory( 126 HostMemAlloc* alloc, 127 const VkMemoryAllocateInfo* pAllocateInfo, 128 SubAlloc* out); 129 130 // Returns true if the block would have been emptied. 131 // In that case, we can then go back and tear down the block itself. 132 bool subFreeHostMemory(SubAlloc* toFree); 133 134 bool canSubAlloc(android::base::guest::SubAllocator* subAlloc, VkDeviceSize size); 135 136 bool isNoFlagsMemoryTypeIndexForGuest( 137 const HostVisibleMemoryVirtualizationInfo* info, 138 uint32_t index); 139 140 } // namespace goldfish_vk 141