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