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 };
87 
88 VkResult finishHostMemAllocInit(
89     VkEncoder* enc,
90     VkDevice device,
91     uint32_t memoryTypeIndex,
92     VkDeviceSize nonCoherentAtomSize,
93     VkDeviceSize allocSize,
94     VkDeviceSize mappedSize,
95     uint8_t* mappedPtr,
96     HostMemAlloc* out);
97 
98 void destroyHostMemAlloc(
99     bool freeMemorySyncSupported,
100     VkEncoder* enc,
101     VkDevice device,
102     HostMemAlloc* toDestroy);
103 
104 struct SubAlloc {
105     uint8_t* mappedPtr = nullptr;
106     VkDeviceSize subAllocSize = 0;
107     VkDeviceSize subMappedSize = 0;
108 
109     VkDeviceMemory baseMemory = VK_NULL_HANDLE;
110     VkDeviceSize baseOffset = 0;
111     android::base::guest::SubAllocator* subAlloc = nullptr;
112     VkDeviceMemory subMemory = VK_NULL_HANDLE;
113 };
114 
115 void subAllocHostMemory(
116     HostMemAlloc* alloc,
117     const VkMemoryAllocateInfo* pAllocateInfo,
118     SubAlloc* out);
119 
120 void subFreeHostMemory(SubAlloc* toFree);
121 
122 bool canSubAlloc(android::base::guest::SubAllocator* subAlloc, VkDeviceSize size);
123 
124 bool isNoFlagsMemoryTypeIndexForGuest(
125     const HostVisibleMemoryVirtualizationInfo* info,
126     uint32_t index);
127 
128 } // namespace goldfish_vk
129