| Lingfeng Yang | 8e89eab | 2019-01-06 16:16:53 -0800 | [diff] [blame] | 1 | /// Copyright (C) 2018 The Android Open Source Project |
| Lingfeng Yang | 71b596b | 2018-11-07 18:03:25 -0800 | [diff] [blame] | 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 | |
| Lingfeng Yang | 71b596b | 2018-11-07 18:03:25 -0800 | [diff] [blame] | 16 | #include "ResourceTracker.h" |
| 17 | |
| Lingfeng Yang | 3175463 | 2018-12-21 18:24:55 -0800 | [diff] [blame] | 18 | #include "../OpenglSystemCommon/EmulatorFeatureInfo.h" |
| Lingfeng Yang | 9b82e33 | 2019-02-13 17:53:57 -0800 | [diff] [blame] | 19 | #include "AndroidHardwareBuffer.h" |
| Lingfeng Yang | 58b89c8 | 2018-12-25 11:23:21 -0800 | [diff] [blame] | 20 | #include "HostVisibleMemoryVirtualization.h" |
| Lingfeng Yang | 71b596b | 2018-11-07 18:03:25 -0800 | [diff] [blame] | 21 | #include "Resources.h" |
| Lingfeng Yang | 131d5a4 | 2018-11-30 12:00:33 -0800 | [diff] [blame] | 22 | #include "VkEncoder.h" |
| Lingfeng Yang | 71b596b | 2018-11-07 18:03:25 -0800 | [diff] [blame] | 23 | |
| Lingfeng Yang | 131d5a4 | 2018-11-30 12:00:33 -0800 | [diff] [blame] | 24 | #include "android/base/AlignedBuf.h" |
| Lingfeng Yang | 6ab1b0d | 2018-11-27 23:36:03 -0800 | [diff] [blame] | 25 | #include "android/base/synchronization/AndroidLock.h" |
| 26 | |
| Lingfeng Yang | def88ba | 2018-12-13 12:43:17 -0800 | [diff] [blame] | 27 | #include "gralloc_cb.h" |
| Lingfeng Yang | 236abc9 | 2018-12-21 20:19:33 -0800 | [diff] [blame] | 28 | #include "goldfish_address_space.h" |
| Lingfeng Yang | def88ba | 2018-12-13 12:43:17 -0800 | [diff] [blame] | 29 | #include "goldfish_vk_private_defs.h" |
| Lingfeng Yang | 4af5f32 | 2019-02-14 08:10:28 -0800 | [diff] [blame] | 30 | #include "vk_util.h" |
| Lingfeng Yang | def88ba | 2018-12-13 12:43:17 -0800 | [diff] [blame] | 31 | |
| Lingfeng Yang | 154a33c | 2019-01-29 19:06:23 -0800 | [diff] [blame] | 32 | #include <string> |
| Lingfeng Yang | 6ab1b0d | 2018-11-27 23:36:03 -0800 | [diff] [blame] | 33 | #include <unordered_map> |
| Lingfeng Yang | f0654ff | 2019-02-02 12:21:24 -0800 | [diff] [blame] | 34 | #include <set> |
| Lingfeng Yang | 6ab1b0d | 2018-11-27 23:36:03 -0800 | [diff] [blame] | 35 | |
| Lingfeng Yang | 29cf075 | 2019-02-13 14:12:25 -0800 | [diff] [blame] | 36 | #include <vndk/hardware_buffer.h> |
| Lingfeng Yang | 131d5a4 | 2018-11-30 12:00:33 -0800 | [diff] [blame] | 37 | #include <log/log.h> |
| Lingfeng Yang | def88ba | 2018-12-13 12:43:17 -0800 | [diff] [blame] | 38 | #include <stdlib.h> |
| 39 | #include <sync/sync.h> |
| Lingfeng Yang | 131d5a4 | 2018-11-30 12:00:33 -0800 | [diff] [blame] | 40 | |
| 41 | #define RESOURCE_TRACKER_DEBUG 0 |
| 42 | |
| 43 | #if RESOURCE_TRACKER_DEBUG |
| Lingfeng Yang | 49c7de2 | 2019-01-23 16:19:50 -0800 | [diff] [blame] | 44 | #undef D |
| Lingfeng Yang | 131d5a4 | 2018-11-30 12:00:33 -0800 | [diff] [blame] | 45 | #define D(fmt,...) ALOGD("%s: " fmt, __func__, ##__VA_ARGS__); |
| 46 | #else |
| 47 | #ifndef D |
| 48 | #define D(fmt,...) |
| 49 | #endif |
| 50 | #endif |
| 51 | |
| 52 | using android::aligned_buf_alloc; |
| 53 | using android::aligned_buf_free; |
| Lingfeng Yang | 6ab1b0d | 2018-11-27 23:36:03 -0800 | [diff] [blame] | 54 | using android::base::guest::AutoLock; |
| 55 | using android::base::guest::Lock; |
| 56 | |
| Lingfeng Yang | 71b596b | 2018-11-07 18:03:25 -0800 | [diff] [blame] | 57 | namespace goldfish_vk { |
| 58 | |
| Lingfeng Yang | 2285df1 | 2018-11-17 16:25:11 -0800 | [diff] [blame] | 59 | #define MAKE_HANDLE_MAPPING_FOREACH(type_name, map_impl, map_to_u64_impl, map_from_u64_impl) \ |
| 60 | void mapHandles_##type_name(type_name* handles, size_t count) override { \ |
| 61 | for (size_t i = 0; i < count; ++i) { \ |
| 62 | map_impl; \ |
| 63 | } \ |
| 64 | } \ |
| 65 | void mapHandles_##type_name##_u64(const type_name* handles, uint64_t* handle_u64s, size_t count) override { \ |
| 66 | for (size_t i = 0; i < count; ++i) { \ |
| 67 | map_to_u64_impl; \ |
| 68 | } \ |
| 69 | } \ |
| 70 | void mapHandles_u64_##type_name(const uint64_t* handle_u64s, type_name* handles, size_t count) override { \ |
| 71 | for (size_t i = 0; i < count; ++i) { \ |
| 72 | map_from_u64_impl; \ |
| 73 | } \ |
| 74 | } \ |
| 75 | |
| 76 | #define DEFINE_RESOURCE_TRACKING_CLASS(class_name, impl) \ |
| 77 | class class_name : public VulkanHandleMapping { \ |
| 78 | public: \ |
| 79 | virtual ~class_name() { } \ |
| 80 | GOLDFISH_VK_LIST_HANDLE_TYPES(impl) \ |
| 81 | }; \ |
| 82 | |
| 83 | #define CREATE_MAPPING_IMPL_FOR_TYPE(type_name) \ |
| 84 | MAKE_HANDLE_MAPPING_FOREACH(type_name, \ |
| Lingfeng Yang | 6ab1b0d | 2018-11-27 23:36:03 -0800 | [diff] [blame] | 85 | handles[i] = new_from_host_##type_name(handles[i]); ResourceTracker::get()->register_##type_name(handles[i]);, \ |
| Lingfeng Yang | 04a5719 | 2018-12-20 14:06:45 -0800 | [diff] [blame] | 86 | handle_u64s[i] = (uint64_t)new_from_host_##type_name(handles[i]), \ |
| 87 | handles[i] = (type_name)new_from_host_u64_##type_name(handle_u64s[i]); ResourceTracker::get()->register_##type_name(handles[i]);) |
| Lingfeng Yang | 2285df1 | 2018-11-17 16:25:11 -0800 | [diff] [blame] | 88 | |
| 89 | #define UNWRAP_MAPPING_IMPL_FOR_TYPE(type_name) \ |
| 90 | MAKE_HANDLE_MAPPING_FOREACH(type_name, \ |
| 91 | handles[i] = get_host_##type_name(handles[i]), \ |
| Lingfeng Yang | 04a5719 | 2018-12-20 14:06:45 -0800 | [diff] [blame] | 92 | handle_u64s[i] = (uint64_t)get_host_u64_##type_name(handles[i]), \ |
| 93 | handles[i] = (type_name)get_host_##type_name((type_name)handle_u64s[i])) |
| Lingfeng Yang | 2285df1 | 2018-11-17 16:25:11 -0800 | [diff] [blame] | 94 | |
| 95 | #define DESTROY_MAPPING_IMPL_FOR_TYPE(type_name) \ |
| 96 | MAKE_HANDLE_MAPPING_FOREACH(type_name, \ |
| Lingfeng Yang | 6ab1b0d | 2018-11-27 23:36:03 -0800 | [diff] [blame] | 97 | ResourceTracker::get()->unregister_##type_name(handles[i]); delete_goldfish_##type_name(handles[i]), \ |
| Lingfeng Yang | 2285df1 | 2018-11-17 16:25:11 -0800 | [diff] [blame] | 98 | (void)handle_u64s[i]; delete_goldfish_##type_name(handles[i]), \ |
| Lingfeng Yang | 04a5719 | 2018-12-20 14:06:45 -0800 | [diff] [blame] | 99 | (void)handles[i]; delete_goldfish_##type_name((type_name)handle_u64s[i])) |
| Lingfeng Yang | 2285df1 | 2018-11-17 16:25:11 -0800 | [diff] [blame] | 100 | |
| 101 | DEFINE_RESOURCE_TRACKING_CLASS(CreateMapping, CREATE_MAPPING_IMPL_FOR_TYPE) |
| 102 | DEFINE_RESOURCE_TRACKING_CLASS(UnwrapMapping, UNWRAP_MAPPING_IMPL_FOR_TYPE) |
| 103 | DEFINE_RESOURCE_TRACKING_CLASS(DestroyMapping, DESTROY_MAPPING_IMPL_FOR_TYPE) |
| Lingfeng Yang | 71b596b | 2018-11-07 18:03:25 -0800 | [diff] [blame] | 104 | |
| 105 | class ResourceTracker::Impl { |
| 106 | public: |
| 107 | Impl() = default; |
| 108 | CreateMapping createMapping; |
| 109 | UnwrapMapping unwrapMapping; |
| 110 | DestroyMapping destroyMapping; |
| Lingfeng Yang | 2285df1 | 2018-11-17 16:25:11 -0800 | [diff] [blame] | 111 | DefaultHandleMapping defaultMapping; |
| Lingfeng Yang | 6ab1b0d | 2018-11-27 23:36:03 -0800 | [diff] [blame] | 112 | |
| 113 | #define HANDLE_DEFINE_TRIVIAL_INFO_STRUCT(type) \ |
| 114 | struct type##_Info { \ |
| 115 | uint32_t unused; \ |
| 116 | }; \ |
| 117 | |
| 118 | GOLDFISH_VK_LIST_TRIVIAL_HANDLE_TYPES(HANDLE_DEFINE_TRIVIAL_INFO_STRUCT) |
| 119 | |
| Lingfeng Yang | f0654ff | 2019-02-02 12:21:24 -0800 | [diff] [blame] | 120 | struct VkInstance_Info { |
| 121 | uint32_t highestApiVersion; |
| 122 | std::set<std::string> enabledExtensions; |
| Lingfeng Yang | b64ca45 | 2019-02-14 22:04:28 -0800 | [diff] [blame] | 123 | // Fodder for vkEnumeratePhysicalDevices. |
| 124 | std::vector<VkPhysicalDevice> physicalDevices; |
| Lingfeng Yang | f0654ff | 2019-02-02 12:21:24 -0800 | [diff] [blame] | 125 | }; |
| 126 | |
| Lingfeng Yang | 6ab1b0d | 2018-11-27 23:36:03 -0800 | [diff] [blame] | 127 | struct VkDevice_Info { |
| 128 | VkPhysicalDevice physdev; |
| 129 | VkPhysicalDeviceProperties props; |
| 130 | VkPhysicalDeviceMemoryProperties memProps; |
| Lingfeng Yang | 35e9c6a | 2018-12-25 17:13:36 -0800 | [diff] [blame] | 131 | HostMemAlloc hostMemAllocs[VK_MAX_MEMORY_TYPES] = {}; |
| Lingfeng Yang | f0654ff | 2019-02-02 12:21:24 -0800 | [diff] [blame] | 132 | uint32_t apiVersion; |
| 133 | std::set<std::string> enabledExtensions; |
| Lingfeng Yang | 6ab1b0d | 2018-11-27 23:36:03 -0800 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | struct VkDeviceMemory_Info { |
| Lingfeng Yang | def88ba | 2018-12-13 12:43:17 -0800 | [diff] [blame] | 137 | VkDeviceSize allocationSize = 0; |
| 138 | VkDeviceSize mappedSize = 0; |
| 139 | uint8_t* mappedPtr = nullptr; |
| 140 | uint32_t memoryTypeIndex = 0; |
| Lingfeng Yang | 35e9c6a | 2018-12-25 17:13:36 -0800 | [diff] [blame] | 141 | bool virtualHostVisibleBacking = false; |
| Lingfeng Yang | 236abc9 | 2018-12-21 20:19:33 -0800 | [diff] [blame] | 142 | bool directMapped = false; |
| Lingfeng Yang | 35e9c6a | 2018-12-25 17:13:36 -0800 | [diff] [blame] | 143 | GoldfishAddressSpaceBlock* |
| 144 | goldfishAddressSpaceBlock = nullptr; |
| 145 | SubAlloc subAlloc; |
| Lingfeng Yang | 9b82e33 | 2019-02-13 17:53:57 -0800 | [diff] [blame] | 146 | AHardwareBuffer** ahbHandle = nullptr; |
| Lingfeng Yang | 6ab1b0d | 2018-11-27 23:36:03 -0800 | [diff] [blame] | 147 | }; |
| 148 | |
| Lingfeng Yang | 4af5f32 | 2019-02-14 08:10:28 -0800 | [diff] [blame] | 149 | // custom guest-side structs for images/buffers because of AHardwareBuffer :(( |
| 150 | struct VkImage_Info { |
| 151 | VkDevice device; |
| 152 | VkImageCreateInfo createInfo; |
| 153 | VkDeviceMemory currentBacking = VK_NULL_HANDLE; |
| 154 | VkDeviceSize currentBackingOffset = 0; |
| 155 | VkDeviceSize currentBackingSize = 0; |
| 156 | }; |
| 157 | |
| 158 | struct VkBuffer_Info { |
| 159 | VkDevice device; |
| 160 | VkBufferCreateInfo createInfo; |
| 161 | VkDeviceMemory currentBacking = VK_NULL_HANDLE; |
| 162 | VkDeviceSize currentBackingOffset = 0; |
| 163 | VkDeviceSize currentBackingSize = 0; |
| 164 | }; |
| 165 | |
| Lingfeng Yang | 6ab1b0d | 2018-11-27 23:36:03 -0800 | [diff] [blame] | 166 | #define HANDLE_REGISTER_IMPL_IMPL(type) \ |
| 167 | std::unordered_map<type, type##_Info> info_##type; \ |
| 168 | void register_##type(type obj) { \ |
| 169 | AutoLock lock(mLock); \ |
| 170 | info_##type[obj] = type##_Info(); \ |
| 171 | } \ |
| Lingfeng Yang | def88ba | 2018-12-13 12:43:17 -0800 | [diff] [blame] | 172 | |
| 173 | #define HANDLE_UNREGISTER_IMPL_IMPL(type) \ |
| Lingfeng Yang | 6ab1b0d | 2018-11-27 23:36:03 -0800 | [diff] [blame] | 174 | void unregister_##type(type obj) { \ |
| 175 | AutoLock lock(mLock); \ |
| 176 | info_##type.erase(obj); \ |
| 177 | } \ |
| 178 | |
| 179 | GOLDFISH_VK_LIST_HANDLE_TYPES(HANDLE_REGISTER_IMPL_IMPL) |
| Lingfeng Yang | def88ba | 2018-12-13 12:43:17 -0800 | [diff] [blame] | 180 | GOLDFISH_VK_LIST_TRIVIAL_HANDLE_TYPES(HANDLE_UNREGISTER_IMPL_IMPL) |
| 181 | |
| Lingfeng Yang | f0654ff | 2019-02-02 12:21:24 -0800 | [diff] [blame] | 182 | void unregister_VkInstance(VkInstance instance) { |
| 183 | AutoLock lock(mLock); |
| 184 | |
| 185 | auto it = info_VkInstance.find(instance); |
| 186 | if (it == info_VkInstance.end()) return; |
| 187 | auto info = it->second; |
| 188 | info_VkInstance.erase(instance); |
| 189 | lock.unlock(); |
| 190 | } |
| 191 | |
| Lingfeng Yang | def88ba | 2018-12-13 12:43:17 -0800 | [diff] [blame] | 192 | void unregister_VkDevice(VkDevice device) { |
| 193 | AutoLock lock(mLock); |
| Lingfeng Yang | 35e9c6a | 2018-12-25 17:13:36 -0800 | [diff] [blame] | 194 | |
| 195 | auto it = info_VkDevice.find(device); |
| 196 | if (it == info_VkDevice.end()) return; |
| 197 | auto info = it->second; |
| Lingfeng Yang | def88ba | 2018-12-13 12:43:17 -0800 | [diff] [blame] | 198 | info_VkDevice.erase(device); |
| Lingfeng Yang | 35e9c6a | 2018-12-25 17:13:36 -0800 | [diff] [blame] | 199 | lock.unlock(); |
| Lingfeng Yang | def88ba | 2018-12-13 12:43:17 -0800 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | void unregister_VkDeviceMemory(VkDeviceMemory mem) { |
| 203 | AutoLock lock(mLock); |
| Lingfeng Yang | 236abc9 | 2018-12-21 20:19:33 -0800 | [diff] [blame] | 204 | |
| Lingfeng Yang | def88ba | 2018-12-13 12:43:17 -0800 | [diff] [blame] | 205 | auto it = info_VkDeviceMemory.find(mem); |
| 206 | if (it == info_VkDeviceMemory.end()) return; |
| 207 | |
| 208 | auto& memInfo = it->second; |
| Lingfeng Yang | 236abc9 | 2018-12-21 20:19:33 -0800 | [diff] [blame] | 209 | |
| Lingfeng Yang | 35e9c6a | 2018-12-25 17:13:36 -0800 | [diff] [blame] | 210 | if (memInfo.mappedPtr && |
| 211 | !memInfo.virtualHostVisibleBacking && |
| 212 | !memInfo.directMapped) { |
| Lingfeng Yang | def88ba | 2018-12-13 12:43:17 -0800 | [diff] [blame] | 213 | aligned_buf_free(memInfo.mappedPtr); |
| 214 | } |
| Lingfeng Yang | 236abc9 | 2018-12-21 20:19:33 -0800 | [diff] [blame] | 215 | |
| Lingfeng Yang | 35e9c6a | 2018-12-25 17:13:36 -0800 | [diff] [blame] | 216 | if (memInfo.directMapped) { |
| 217 | subFreeHostMemory(&memInfo.subAlloc); |
| 218 | } |
| 219 | |
| 220 | delete memInfo.goldfishAddressSpaceBlock; |
| 221 | |
| Lingfeng Yang | 236abc9 | 2018-12-21 20:19:33 -0800 | [diff] [blame] | 222 | info_VkDeviceMemory.erase(mem); |
| Lingfeng Yang | def88ba | 2018-12-13 12:43:17 -0800 | [diff] [blame] | 223 | } |
| Lingfeng Yang | 6ab1b0d | 2018-11-27 23:36:03 -0800 | [diff] [blame] | 224 | |
| Lingfeng Yang | 4af5f32 | 2019-02-14 08:10:28 -0800 | [diff] [blame] | 225 | void unregister_VkImage(VkImage img) { |
| 226 | AutoLock lock(mLock); |
| 227 | |
| 228 | auto it = info_VkImage.find(img); |
| 229 | if (it == info_VkImage.end()) return; |
| 230 | |
| 231 | info_VkImage.erase(img); |
| 232 | } |
| 233 | |
| 234 | void unregister_VkBuffer(VkBuffer buf) { |
| 235 | AutoLock lock(mLock); |
| 236 | |
| 237 | auto it = info_VkBuffer.find(buf); |
| 238 | if (it == info_VkBuffer.end()) return; |
| 239 | |
| 240 | info_VkBuffer.erase(buf); |
| 241 | } |
| 242 | |
| Lingfeng Yang | f0654ff | 2019-02-02 12:21:24 -0800 | [diff] [blame] | 243 | // TODO: Upgrade to 1.1 |
| 244 | static constexpr uint32_t kMaxApiVersion = VK_MAKE_VERSION(1, 0, 65); |
| 245 | static constexpr uint32_t kMinApiVersion = VK_MAKE_VERSION(1, 0, 0); |
| 246 | |
| 247 | void setInstanceInfo(VkInstance instance, |
| 248 | uint32_t enabledExtensionCount, |
| 249 | const char* const* ppEnabledExtensionNames) { |
| 250 | AutoLock lock(mLock); |
| 251 | auto& info = info_VkInstance[instance]; |
| 252 | info.highestApiVersion = kMaxApiVersion; |
| 253 | |
| 254 | if (!ppEnabledExtensionNames) return; |
| 255 | |
| 256 | for (uint32_t i = 0; i < enabledExtensionCount; ++i) { |
| 257 | info.enabledExtensions.insert(ppEnabledExtensionNames[i]); |
| 258 | } |
| 259 | } |
| 260 | |
| Lingfeng Yang | 6ab1b0d | 2018-11-27 23:36:03 -0800 | [diff] [blame] | 261 | void setDeviceInfo(VkDevice device, |
| 262 | VkPhysicalDevice physdev, |
| 263 | VkPhysicalDeviceProperties props, |
| Lingfeng Yang | f0654ff | 2019-02-02 12:21:24 -0800 | [diff] [blame] | 264 | VkPhysicalDeviceMemoryProperties memProps, |
| 265 | uint32_t enabledExtensionCount, |
| 266 | const char* const* ppEnabledExtensionNames) { |
| Lingfeng Yang | 6ab1b0d | 2018-11-27 23:36:03 -0800 | [diff] [blame] | 267 | AutoLock lock(mLock); |
| 268 | auto& info = info_VkDevice[device]; |
| 269 | info.physdev = physdev; |
| 270 | info.props = props; |
| 271 | info.memProps = memProps; |
| Lingfeng Yang | 58b89c8 | 2018-12-25 11:23:21 -0800 | [diff] [blame] | 272 | initHostVisibleMemoryVirtualizationInfo( |
| Lingfeng Yang | afe29d3 | 2018-12-25 13:01:52 -0800 | [diff] [blame] | 273 | physdev, &memProps, |
| 274 | mFeatureInfo->hasDirectMem, |
| 275 | &mHostVisibleMemoryVirtInfo); |
| Lingfeng Yang | f0654ff | 2019-02-02 12:21:24 -0800 | [diff] [blame] | 276 | info.apiVersion = props.apiVersion; |
| 277 | |
| 278 | if (!ppEnabledExtensionNames) return; |
| 279 | |
| 280 | for (uint32_t i = 0; i < enabledExtensionCount; ++i) { |
| 281 | info.enabledExtensions.insert(ppEnabledExtensionNames[i]); |
| 282 | } |
| Lingfeng Yang | 6ab1b0d | 2018-11-27 23:36:03 -0800 | [diff] [blame] | 283 | } |
| 284 | |
| Lingfeng Yang | 58b89c8 | 2018-12-25 11:23:21 -0800 | [diff] [blame] | 285 | void setDeviceMemoryInfo(VkDevice device, |
| 286 | VkDeviceMemory memory, |
| Lingfeng Yang | def88ba | 2018-12-13 12:43:17 -0800 | [diff] [blame] | 287 | VkDeviceSize allocationSize, |
| 288 | VkDeviceSize mappedSize, |
| 289 | uint8_t* ptr, |
| 290 | uint32_t memoryTypeIndex) { |
| 291 | AutoLock lock(mLock); |
| Lingfeng Yang | 58b89c8 | 2018-12-25 11:23:21 -0800 | [diff] [blame] | 292 | auto& deviceInfo = info_VkDevice[device]; |
| Lingfeng Yang | def88ba | 2018-12-13 12:43:17 -0800 | [diff] [blame] | 293 | auto& info = info_VkDeviceMemory[memory]; |
| Lingfeng Yang | 58b89c8 | 2018-12-25 11:23:21 -0800 | [diff] [blame] | 294 | |
| Lingfeng Yang | def88ba | 2018-12-13 12:43:17 -0800 | [diff] [blame] | 295 | info.allocationSize = allocationSize; |
| 296 | info.mappedSize = mappedSize; |
| 297 | info.mappedPtr = ptr; |
| 298 | info.memoryTypeIndex = memoryTypeIndex; |
| 299 | } |
| 300 | |
| Lingfeng Yang | 6ab1b0d | 2018-11-27 23:36:03 -0800 | [diff] [blame] | 301 | bool isMemoryTypeHostVisible(VkDevice device, uint32_t typeIndex) const { |
| 302 | AutoLock lock(mLock); |
| 303 | const auto it = info_VkDevice.find(device); |
| 304 | |
| 305 | if (it == info_VkDevice.end()) return false; |
| 306 | |
| 307 | const auto& info = it->second; |
| Lingfeng Yang | def88ba | 2018-12-13 12:43:17 -0800 | [diff] [blame] | 308 | return info.memProps.memoryTypes[typeIndex].propertyFlags & |
| Lingfeng Yang | 6ab1b0d | 2018-11-27 23:36:03 -0800 | [diff] [blame] | 309 | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT; |
| 310 | } |
| 311 | |
| Lingfeng Yang | def88ba | 2018-12-13 12:43:17 -0800 | [diff] [blame] | 312 | uint8_t* getMappedPointer(VkDeviceMemory memory) { |
| 313 | AutoLock lock(mLock); |
| 314 | const auto it = info_VkDeviceMemory.find(memory); |
| 315 | if (it == info_VkDeviceMemory.end()) return nullptr; |
| 316 | |
| 317 | const auto& info = it->second; |
| 318 | return info.mappedPtr; |
| 319 | } |
| 320 | |
| 321 | VkDeviceSize getMappedSize(VkDeviceMemory memory) { |
| 322 | AutoLock lock(mLock); |
| 323 | const auto it = info_VkDeviceMemory.find(memory); |
| 324 | if (it == info_VkDeviceMemory.end()) return 0; |
| 325 | |
| 326 | const auto& info = it->second; |
| 327 | return info.mappedSize; |
| 328 | } |
| 329 | |
| Lingfeng Yang | 6ab1b0d | 2018-11-27 23:36:03 -0800 | [diff] [blame] | 330 | VkDeviceSize getNonCoherentExtendedSize(VkDevice device, VkDeviceSize basicSize) const { |
| 331 | AutoLock lock(mLock); |
| 332 | const auto it = info_VkDevice.find(device); |
| 333 | if (it == info_VkDevice.end()) return basicSize; |
| 334 | const auto& info = it->second; |
| 335 | |
| 336 | VkDeviceSize nonCoherentAtomSize = |
| 337 | info.props.limits.nonCoherentAtomSize; |
| 338 | VkDeviceSize atoms = |
| 339 | (basicSize + nonCoherentAtomSize - 1) / nonCoherentAtomSize; |
| 340 | return atoms * nonCoherentAtomSize; |
| 341 | } |
| 342 | |
| Lingfeng Yang | def88ba | 2018-12-13 12:43:17 -0800 | [diff] [blame] | 343 | bool isValidMemoryRange(const VkMappedMemoryRange& range) const { |
| 344 | AutoLock lock(mLock); |
| 345 | const auto it = info_VkDeviceMemory.find(range.memory); |
| 346 | if (it == info_VkDeviceMemory.end()) return false; |
| 347 | const auto& info = it->second; |
| 348 | |
| 349 | if (!info.mappedPtr) return false; |
| 350 | |
| 351 | VkDeviceSize offset = range.offset; |
| 352 | VkDeviceSize size = range.size; |
| 353 | |
| 354 | if (size == VK_WHOLE_SIZE) { |
| 355 | return offset <= info.mappedSize; |
| 356 | } |
| 357 | |
| 358 | return offset + size <= info.mappedSize; |
| 359 | } |
| 360 | |
| Lingfeng Yang | 3175463 | 2018-12-21 18:24:55 -0800 | [diff] [blame] | 361 | void setupFeatures(const EmulatorFeatureInfo* features) { |
| 362 | if (!features || mFeatureInfo) return; |
| 363 | mFeatureInfo.reset(new EmulatorFeatureInfo); |
| 364 | *mFeatureInfo = *features; |
| Lingfeng Yang | 236abc9 | 2018-12-21 20:19:33 -0800 | [diff] [blame] | 365 | |
| 366 | if (mFeatureInfo->hasDirectMem) { |
| 367 | mGoldfishAddressSpaceBlockProvider.reset( |
| 368 | new GoldfishAddressSpaceBlockProvider); |
| 369 | } |
| 370 | } |
| 371 | |
| Lingfeng Yang | b8a38c7 | 2019-02-02 20:27:54 -0800 | [diff] [blame] | 372 | bool hostSupportsVulkan() const { |
| 373 | if (!mFeatureInfo) return false; |
| 374 | |
| 375 | return mFeatureInfo->hasVulkan; |
| 376 | } |
| 377 | |
| Lingfeng Yang | 236abc9 | 2018-12-21 20:19:33 -0800 | [diff] [blame] | 378 | bool usingDirectMapping() const { |
| Lingfeng Yang | afe29d3 | 2018-12-25 13:01:52 -0800 | [diff] [blame] | 379 | return mHostVisibleMemoryVirtInfo.virtualizationSupported; |
| Lingfeng Yang | 3175463 | 2018-12-21 18:24:55 -0800 | [diff] [blame] | 380 | } |
| 381 | |
| Lingfeng Yang | 154a33c | 2019-01-29 19:06:23 -0800 | [diff] [blame] | 382 | int getHostInstanceExtensionIndex(const std::string& extName) const { |
| 383 | int i = 0; |
| 384 | for (const auto& prop : mHostInstanceExtensions) { |
| 385 | if (extName == std::string(prop.extensionName)) { |
| 386 | return i; |
| 387 | } |
| 388 | ++i; |
| 389 | } |
| 390 | return -1; |
| 391 | } |
| 392 | |
| 393 | int getHostDeviceExtensionIndex(const std::string& extName) const { |
| 394 | int i = 0; |
| 395 | for (const auto& prop : mHostDeviceExtensions) { |
| 396 | if (extName == std::string(prop.extensionName)) { |
| 397 | return i; |
| 398 | } |
| 399 | ++i; |
| 400 | } |
| 401 | return -1; |
| 402 | } |
| 403 | |
| Lingfeng Yang | 97a0670 | 2018-12-24 17:02:43 -0800 | [diff] [blame] | 404 | void deviceMemoryTransform_tohost( |
| 405 | VkDeviceMemory* memory, uint32_t memoryCount, |
| 406 | VkDeviceSize* offset, uint32_t offsetCount, |
| 407 | VkDeviceSize* size, uint32_t sizeCount, |
| 408 | uint32_t* typeIndex, uint32_t typeIndexCount, |
| 409 | uint32_t* typeBits, uint32_t typeBitsCount) { |
| Lingfeng Yang | afe29d3 | 2018-12-25 13:01:52 -0800 | [diff] [blame] | 410 | |
| 411 | (void)memoryCount; |
| 412 | (void)offsetCount; |
| 413 | (void)sizeCount; |
| Lingfeng Yang | 35e9c6a | 2018-12-25 17:13:36 -0800 | [diff] [blame] | 414 | |
| Lingfeng Yang | df17313 | 2018-12-25 13:30:57 -0800 | [diff] [blame] | 415 | const auto& hostVirt = |
| 416 | mHostVisibleMemoryVirtInfo; |
| 417 | |
| 418 | if (!hostVirt.virtualizationSupported) return; |
| Lingfeng Yang | 97a0670 | 2018-12-24 17:02:43 -0800 | [diff] [blame] | 419 | |
| Lingfeng Yang | 35e9c6a | 2018-12-25 17:13:36 -0800 | [diff] [blame] | 420 | if (memory) { |
| 421 | AutoLock lock (mLock); |
| 422 | |
| 423 | for (uint32_t i = 0; i < memoryCount; ++i) { |
| 424 | VkDeviceMemory mem = memory[i]; |
| 425 | |
| 426 | auto it = info_VkDeviceMemory.find(mem); |
| 427 | if (it == info_VkDeviceMemory.end()) return; |
| 428 | |
| 429 | const auto& info = it->second; |
| 430 | |
| 431 | if (!info.directMapped) continue; |
| 432 | |
| 433 | memory[i] = info.subAlloc.baseMemory; |
| 434 | |
| 435 | if (offset) { |
| 436 | offset[i] = info.subAlloc.baseOffset + offset[i]; |
| 437 | } |
| 438 | |
| 439 | if (size) { |
| 440 | if (size[i] == VK_WHOLE_SIZE) { |
| 441 | size[i] = info.subAlloc.subMappedSize; |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | // TODO |
| 446 | (void)memory; |
| 447 | (void)offset; |
| 448 | (void)size; |
| 449 | } |
| Lingfeng Yang | 97a0670 | 2018-12-24 17:02:43 -0800 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | for (uint32_t i = 0; i < typeIndexCount; ++i) { |
| Lingfeng Yang | df17313 | 2018-12-25 13:30:57 -0800 | [diff] [blame] | 453 | typeIndex[i] = |
| 454 | hostVirt.memoryTypeIndexMappingToHost[typeIndex[i]]; |
| Lingfeng Yang | 97a0670 | 2018-12-24 17:02:43 -0800 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | for (uint32_t i = 0; i < typeBitsCount; ++i) { |
| Lingfeng Yang | df17313 | 2018-12-25 13:30:57 -0800 | [diff] [blame] | 458 | uint32_t bits = 0; |
| 459 | for (uint32_t j = 0; j < VK_MAX_MEMORY_TYPES; ++j) { |
| 460 | bool guestHas = typeBits[i] & (1 << j); |
| 461 | uint32_t hostIndex = |
| 462 | hostVirt.memoryTypeIndexMappingToHost[j]; |
| 463 | bits |= guestHas ? (1 << hostIndex) : 0; |
| 464 | } |
| 465 | typeBits[i] = bits; |
| Lingfeng Yang | 97a0670 | 2018-12-24 17:02:43 -0800 | [diff] [blame] | 466 | } |
| 467 | } |
| 468 | |
| 469 | void deviceMemoryTransform_fromhost( |
| Lingfeng Yang | 62b2332 | 2018-12-24 12:45:47 -0800 | [diff] [blame] | 470 | VkDeviceMemory* memory, uint32_t memoryCount, |
| 471 | VkDeviceSize* offset, uint32_t offsetCount, |
| 472 | VkDeviceSize* size, uint32_t sizeCount, |
| 473 | uint32_t* typeIndex, uint32_t typeIndexCount, |
| 474 | uint32_t* typeBits, uint32_t typeBitsCount) { |
| Lingfeng Yang | 62b2332 | 2018-12-24 12:45:47 -0800 | [diff] [blame] | 475 | |
| Lingfeng Yang | afe29d3 | 2018-12-25 13:01:52 -0800 | [diff] [blame] | 476 | (void)memoryCount; |
| 477 | (void)offsetCount; |
| 478 | (void)sizeCount; |
| Lingfeng Yang | 35e9c6a | 2018-12-25 17:13:36 -0800 | [diff] [blame] | 479 | |
| Lingfeng Yang | df17313 | 2018-12-25 13:30:57 -0800 | [diff] [blame] | 480 | const auto& hostVirt = |
| 481 | mHostVisibleMemoryVirtInfo; |
| 482 | |
| 483 | if (!hostVirt.virtualizationSupported) return; |
| 484 | |
| Lingfeng Yang | 35e9c6a | 2018-12-25 17:13:36 -0800 | [diff] [blame] | 485 | AutoLock lock (mLock); |
| 486 | |
| Lingfeng Yang | 62b2332 | 2018-12-24 12:45:47 -0800 | [diff] [blame] | 487 | for (uint32_t i = 0; i < memoryCount; ++i) { |
| 488 | // TODO |
| 489 | (void)memory; |
| 490 | (void)offset; |
| 491 | (void)size; |
| 492 | } |
| 493 | |
| 494 | for (uint32_t i = 0; i < typeIndexCount; ++i) { |
| Lingfeng Yang | df17313 | 2018-12-25 13:30:57 -0800 | [diff] [blame] | 495 | typeIndex[i] = |
| 496 | hostVirt.memoryTypeIndexMappingFromHost[typeIndex[i]]; |
| Lingfeng Yang | 62b2332 | 2018-12-24 12:45:47 -0800 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | for (uint32_t i = 0; i < typeBitsCount; ++i) { |
| Lingfeng Yang | df17313 | 2018-12-25 13:30:57 -0800 | [diff] [blame] | 500 | uint32_t bits = 0; |
| 501 | for (uint32_t j = 0; j < VK_MAX_MEMORY_TYPES; ++j) { |
| 502 | bool hostHas = typeBits[i] & (1 << j); |
| 503 | uint32_t guestIndex = |
| 504 | hostVirt.memoryTypeIndexMappingFromHost[j]; |
| 505 | bits |= hostHas ? (1 << guestIndex) : 0; |
| 506 | |
| 507 | if (hostVirt.memoryTypeBitsShouldAdvertiseBoth[j]) { |
| 508 | bits |= hostHas ? (1 << j) : 0; |
| 509 | } |
| 510 | } |
| 511 | typeBits[i] = bits; |
| Lingfeng Yang | 62b2332 | 2018-12-24 12:45:47 -0800 | [diff] [blame] | 512 | } |
| 513 | } |
| 514 | |
| Lingfeng Yang | def88ba | 2018-12-13 12:43:17 -0800 | [diff] [blame] | 515 | VkResult on_vkEnumerateInstanceVersion( |
| 516 | void*, |
| 517 | VkResult, |
| 518 | uint32_t* apiVersion) { |
| 519 | if (apiVersion) { |
| Lingfeng Yang | f0654ff | 2019-02-02 12:21:24 -0800 | [diff] [blame] | 520 | *apiVersion = kMaxApiVersion; |
| Lingfeng Yang | def88ba | 2018-12-13 12:43:17 -0800 | [diff] [blame] | 521 | } |
| 522 | return VK_SUCCESS; |
| 523 | } |
| 524 | |
| Lingfeng Yang | 154a33c | 2019-01-29 19:06:23 -0800 | [diff] [blame] | 525 | VkResult on_vkEnumerateInstanceExtensionProperties( |
| 526 | void* context, |
| Lingfeng Yang | def88ba | 2018-12-13 12:43:17 -0800 | [diff] [blame] | 527 | VkResult, |
| Lingfeng Yang | def88ba | 2018-12-13 12:43:17 -0800 | [diff] [blame] | 528 | const char*, |
| 529 | uint32_t* pPropertyCount, |
| Lingfeng Yang | 7dea7b3 | 2018-12-18 09:49:02 -0800 | [diff] [blame] | 530 | VkExtensionProperties* pProperties) { |
| Lingfeng Yang | 7dea7b3 | 2018-12-18 09:49:02 -0800 | [diff] [blame] | 531 | |
| Lingfeng Yang | 154a33c | 2019-01-29 19:06:23 -0800 | [diff] [blame] | 532 | std::vector<const char*> allowedExtensionNames = { |
| 533 | "VK_KHR_get_physical_device_properties2", |
| 534 | // TODO: |
| 535 | // VK_KHR_external_memory_capabilities |
| 536 | }; |
| 537 | |
| 538 | VkEncoder* enc = (VkEncoder*)context; |
| 539 | |
| 540 | // Only advertise a select set of extensions. |
| 541 | if (mHostInstanceExtensions.empty()) { |
| 542 | uint32_t hostPropCount = 0; |
| 543 | enc->vkEnumerateInstanceExtensionProperties(nullptr, &hostPropCount, nullptr); |
| 544 | mHostInstanceExtensions.resize(hostPropCount); |
| 545 | |
| 546 | VkResult hostRes = |
| 547 | enc->vkEnumerateInstanceExtensionProperties( |
| 548 | nullptr, &hostPropCount, mHostInstanceExtensions.data()); |
| 549 | |
| 550 | if (hostRes != VK_SUCCESS) { |
| 551 | return hostRes; |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | std::vector<VkExtensionProperties> filteredExts; |
| 556 | |
| 557 | for (size_t i = 0; i < allowedExtensionNames.size(); ++i) { |
| 558 | auto extIndex = getHostInstanceExtensionIndex(allowedExtensionNames[i]); |
| 559 | if (extIndex != -1) { |
| 560 | filteredExts.push_back(mHostInstanceExtensions[extIndex]); |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | VkExtensionProperties anbExtProp = { |
| 565 | "VK_ANDROID_native_buffer", 7, |
| 566 | }; |
| 567 | |
| 568 | filteredExts.push_back(anbExtProp); |
| 569 | |
| 570 | if (pPropertyCount) { |
| 571 | *pPropertyCount = filteredExts.size(); |
| 572 | } |
| 573 | |
| 574 | if (pPropertyCount && pProperties) { |
| 575 | for (size_t i = 0; i < *pPropertyCount; ++i) { |
| 576 | pProperties[i] = filteredExts[i]; |
| 577 | } |
| 578 | } |
| Lingfeng Yang | 7dea7b3 | 2018-12-18 09:49:02 -0800 | [diff] [blame] | 579 | |
| Lingfeng Yang | def88ba | 2018-12-13 12:43:17 -0800 | [diff] [blame] | 580 | return VK_SUCCESS; |
| 581 | } |
| 582 | |
| Lingfeng Yang | 154a33c | 2019-01-29 19:06:23 -0800 | [diff] [blame] | 583 | VkResult on_vkEnumerateDeviceExtensionProperties( |
| 584 | void* context, |
| 585 | VkResult, |
| 586 | VkPhysicalDevice physdev, |
| 587 | const char*, |
| 588 | uint32_t* pPropertyCount, |
| 589 | VkExtensionProperties* pProperties) { |
| 590 | |
| 591 | std::vector<const char*> allowedExtensionNames = { |
| Lingfeng Yang | 422d22d | 2019-02-02 19:27:15 -0800 | [diff] [blame] | 592 | "VK_KHR_maintenance1", |
| 593 | "VK_KHR_get_memory_requirements2", |
| Lingfeng Yang | d27fc83 | 2019-02-19 10:49:05 -0800 | [diff] [blame] | 594 | "VK_KHR_dedicated_allocation", |
| Lingfeng Yang | 154a33c | 2019-01-29 19:06:23 -0800 | [diff] [blame] | 595 | // "VK_KHR_maintenance2", |
| 596 | // "VK_KHR_maintenance3", |
| 597 | // TODO: |
| 598 | // VK_KHR_external_memory_capabilities |
| 599 | }; |
| 600 | |
| 601 | VkEncoder* enc = (VkEncoder*)context; |
| 602 | |
| 603 | if (mHostDeviceExtensions.empty()) { |
| 604 | uint32_t hostPropCount = 0; |
| 605 | enc->vkEnumerateDeviceExtensionProperties(physdev, nullptr, &hostPropCount, nullptr); |
| 606 | mHostDeviceExtensions.resize(hostPropCount); |
| 607 | |
| 608 | VkResult hostRes = |
| 609 | enc->vkEnumerateDeviceExtensionProperties( |
| 610 | physdev, nullptr, &hostPropCount, mHostDeviceExtensions.data()); |
| 611 | |
| 612 | if (hostRes != VK_SUCCESS) { |
| 613 | return hostRes; |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | std::vector<VkExtensionProperties> filteredExts; |
| 618 | |
| 619 | for (size_t i = 0; i < allowedExtensionNames.size(); ++i) { |
| 620 | auto extIndex = getHostDeviceExtensionIndex(allowedExtensionNames[i]); |
| 621 | if (extIndex != -1) { |
| 622 | filteredExts.push_back(mHostDeviceExtensions[extIndex]); |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | VkExtensionProperties anbExtProp = { |
| 627 | "VK_ANDROID_native_buffer", 7, |
| 628 | }; |
| 629 | |
| 630 | filteredExts.push_back(anbExtProp); |
| 631 | |
| 632 | if (pPropertyCount) { |
| 633 | *pPropertyCount = filteredExts.size(); |
| 634 | } |
| 635 | |
| 636 | if (pPropertyCount && pProperties) { |
| 637 | for (size_t i = 0; i < *pPropertyCount; ++i) { |
| 638 | pProperties[i] = filteredExts[i]; |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | return VK_SUCCESS; |
| Lingfeng Yang | def88ba | 2018-12-13 12:43:17 -0800 | [diff] [blame] | 643 | } |
| 644 | |
| Lingfeng Yang | b64ca45 | 2019-02-14 22:04:28 -0800 | [diff] [blame] | 645 | VkResult on_vkEnumeratePhysicalDevices( |
| 646 | void* context, VkResult, |
| 647 | VkInstance instance, uint32_t* pPhysicalDeviceCount, |
| 648 | VkPhysicalDevice* pPhysicalDevices) { |
| 649 | |
| 650 | VkEncoder* enc = (VkEncoder*)context; |
| 651 | |
| 652 | if (!instance) return VK_ERROR_INITIALIZATION_FAILED; |
| 653 | |
| 654 | if (!pPhysicalDeviceCount) return VK_ERROR_INITIALIZATION_FAILED; |
| 655 | |
| 656 | AutoLock lock(mLock); |
| 657 | |
| 658 | auto it = info_VkInstance.find(instance); |
| 659 | |
| 660 | if (it == info_VkInstance.end()) return VK_ERROR_INITIALIZATION_FAILED; |
| 661 | |
| 662 | auto& info = it->second; |
| 663 | |
| 664 | if (info.physicalDevices.empty()) { |
| 665 | uint32_t physdevCount = 0; |
| 666 | |
| 667 | lock.unlock(); |
| 668 | VkResult countRes = enc->vkEnumeratePhysicalDevices( |
| 669 | instance, &physdevCount, nullptr); |
| 670 | lock.lock(); |
| 671 | |
| 672 | if (countRes != VK_SUCCESS) { |
| 673 | ALOGE("%s: failed: could not count host physical devices. " |
| 674 | "Error %d\n", __func__, countRes); |
| 675 | return countRes; |
| 676 | } |
| 677 | |
| 678 | info.physicalDevices.resize(physdevCount); |
| 679 | |
| 680 | lock.unlock(); |
| 681 | VkResult enumRes = enc->vkEnumeratePhysicalDevices( |
| 682 | instance, &physdevCount, info.physicalDevices.data()); |
| 683 | lock.lock(); |
| 684 | |
| 685 | if (enumRes != VK_SUCCESS) { |
| 686 | ALOGE("%s: failed: could not retrieve host physical devices. " |
| 687 | "Error %d\n", __func__, enumRes); |
| 688 | return enumRes; |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | *pPhysicalDeviceCount = (uint32_t)info.physicalDevices.size(); |
| 693 | |
| 694 | if (pPhysicalDevices && *pPhysicalDeviceCount) { |
| 695 | memcpy(pPhysicalDevices, |
| 696 | info.physicalDevices.data(), |
| 697 | sizeof(VkPhysicalDevice) * |
| 698 | info.physicalDevices.size()); |
| 699 | } |
| 700 | |
| 701 | return VK_SUCCESS; |
| 702 | } |
| 703 | |
| Lingfeng Yang | 97a0670 | 2018-12-24 17:02:43 -0800 | [diff] [blame] | 704 | void on_vkGetPhysicalDeviceMemoryProperties( |
| 705 | void*, |
| Lingfeng Yang | df17313 | 2018-12-25 13:30:57 -0800 | [diff] [blame] | 706 | VkPhysicalDevice physdev, |
| 707 | VkPhysicalDeviceMemoryProperties* out) { |
| Lingfeng Yang | 35e9c6a | 2018-12-25 17:13:36 -0800 | [diff] [blame] | 708 | |
| Lingfeng Yang | df17313 | 2018-12-25 13:30:57 -0800 | [diff] [blame] | 709 | initHostVisibleMemoryVirtualizationInfo( |
| 710 | physdev, |
| 711 | out, |
| 712 | mFeatureInfo->hasDirectMem, |
| 713 | &mHostVisibleMemoryVirtInfo); |
| Lingfeng Yang | 35e9c6a | 2018-12-25 17:13:36 -0800 | [diff] [blame] | 714 | |
| Lingfeng Yang | df17313 | 2018-12-25 13:30:57 -0800 | [diff] [blame] | 715 | if (mHostVisibleMemoryVirtInfo.virtualizationSupported) { |
| 716 | *out = mHostVisibleMemoryVirtInfo.guestMemoryProperties; |
| 717 | } |
| 718 | } |
| Lingfeng Yang | 97a0670 | 2018-12-24 17:02:43 -0800 | [diff] [blame] | 719 | |
| Lingfeng Yang | 154a33c | 2019-01-29 19:06:23 -0800 | [diff] [blame] | 720 | void on_vkGetPhysicalDeviceMemoryProperties2( |
| 721 | void*, |
| 722 | VkPhysicalDevice physdev, |
| 723 | VkPhysicalDeviceMemoryProperties2* out) { |
| 724 | |
| 725 | initHostVisibleMemoryVirtualizationInfo( |
| 726 | physdev, |
| 727 | &out->memoryProperties, |
| 728 | mFeatureInfo->hasDirectMem, |
| 729 | &mHostVisibleMemoryVirtInfo); |
| 730 | |
| 731 | if (mHostVisibleMemoryVirtInfo.virtualizationSupported) { |
| 732 | out->memoryProperties = mHostVisibleMemoryVirtInfo.guestMemoryProperties; |
| 733 | } |
| 734 | } |
| 735 | |
| Lingfeng Yang | f0654ff | 2019-02-02 12:21:24 -0800 | [diff] [blame] | 736 | VkResult on_vkCreateInstance( |
| 737 | void*, |
| 738 | VkResult input_result, |
| 739 | const VkInstanceCreateInfo* createInfo, |
| 740 | const VkAllocationCallbacks*, |
| 741 | VkInstance* pInstance) { |
| 742 | |
| 743 | if (input_result != VK_SUCCESS) return input_result; |
| 744 | |
| 745 | setInstanceInfo( |
| 746 | *pInstance, |
| 747 | createInfo->enabledExtensionCount, |
| 748 | createInfo->ppEnabledExtensionNames); |
| 749 | |
| 750 | return input_result; |
| 751 | } |
| Lingfeng Yang | 154a33c | 2019-01-29 19:06:23 -0800 | [diff] [blame] | 752 | |
| Lingfeng Yang | 131d5a4 | 2018-11-30 12:00:33 -0800 | [diff] [blame] | 753 | VkResult on_vkCreateDevice( |
| 754 | void* context, |
| 755 | VkResult input_result, |
| 756 | VkPhysicalDevice physicalDevice, |
| Lingfeng Yang | f0654ff | 2019-02-02 12:21:24 -0800 | [diff] [blame] | 757 | const VkDeviceCreateInfo* pCreateInfo, |
| Lingfeng Yang | 131d5a4 | 2018-11-30 12:00:33 -0800 | [diff] [blame] | 758 | const VkAllocationCallbacks*, |
| 759 | VkDevice* pDevice) { |
| 760 | |
| 761 | if (input_result != VK_SUCCESS) return input_result; |
| 762 | |
| 763 | VkEncoder* enc = (VkEncoder*)context; |
| 764 | |
| 765 | VkPhysicalDeviceProperties props; |
| 766 | VkPhysicalDeviceMemoryProperties memProps; |
| 767 | enc->vkGetPhysicalDeviceProperties(physicalDevice, &props); |
| 768 | enc->vkGetPhysicalDeviceMemoryProperties(physicalDevice, &memProps); |
| 769 | |
| Lingfeng Yang | f0654ff | 2019-02-02 12:21:24 -0800 | [diff] [blame] | 770 | setDeviceInfo( |
| 771 | *pDevice, physicalDevice, props, memProps, |
| 772 | pCreateInfo->enabledExtensionCount, pCreateInfo->ppEnabledExtensionNames); |
| Lingfeng Yang | 131d5a4 | 2018-11-30 12:00:33 -0800 | [diff] [blame] | 773 | |
| 774 | return input_result; |
| 775 | } |
| 776 | |
| Lingfeng Yang | 35e9c6a | 2018-12-25 17:13:36 -0800 | [diff] [blame] | 777 | void on_vkDestroyDevice_pre( |
| Lingfeng Yang | 8e89eab | 2019-01-06 16:16:53 -0800 | [diff] [blame] | 778 | void* context, |
| Lingfeng Yang | 35e9c6a | 2018-12-25 17:13:36 -0800 | [diff] [blame] | 779 | VkDevice device, |
| 780 | const VkAllocationCallbacks*) { |
| 781 | |
| 782 | AutoLock lock(mLock); |
| 783 | |
| 784 | auto it = info_VkDevice.find(device); |
| 785 | if (it == info_VkDevice.end()) return; |
| 786 | auto info = it->second; |
| 787 | |
| 788 | lock.unlock(); |
| 789 | |
| Lingfeng Yang | 8e89eab | 2019-01-06 16:16:53 -0800 | [diff] [blame] | 790 | VkEncoder* enc = (VkEncoder*)context; |
| 791 | |
| Lingfeng Yang | 35e9c6a | 2018-12-25 17:13:36 -0800 | [diff] [blame] | 792 | for (uint32_t i = 0; i < VK_MAX_MEMORY_TYPES; ++i) { |
| Lingfeng Yang | 8e89eab | 2019-01-06 16:16:53 -0800 | [diff] [blame] | 793 | destroyHostMemAlloc(enc, device, &info.hostMemAllocs[i]); |
| Lingfeng Yang | 35e9c6a | 2018-12-25 17:13:36 -0800 | [diff] [blame] | 794 | } |
| 795 | } |
| 796 | |
| Lingfeng Yang | 9b82e33 | 2019-02-13 17:53:57 -0800 | [diff] [blame] | 797 | VkResult on_vkGetAndroidHardwareBufferPropertiesANDROID( |
| 798 | VkDevice device, |
| 799 | const AHardwareBuffer* buffer, |
| 800 | VkAndroidHardwareBufferPropertiesANDROID* pProperties) { |
| 801 | return getAndroidHardwareBufferPropertiesANDROID( |
| 802 | &mHostVisibleMemoryVirtInfo, |
| 803 | device, buffer, pProperties); |
| 804 | } |
| 805 | |
| 806 | VkResult on_vkGetMemoryAndroidHardwareBufferANDROID( |
| 807 | VkDevice device, |
| 808 | const VkMemoryGetAndroidHardwareBufferInfoANDROID *pInfo, |
| 809 | struct AHardwareBuffer** pBuffer) { |
| 810 | |
| 811 | if (!pInfo) return VK_ERROR_INITIALIZATION_FAILED; |
| 812 | if (!pInfo->memory) return VK_ERROR_INITIALIZATION_FAILED; |
| 813 | |
| 814 | AutoLock lock(mLock); |
| 815 | |
| 816 | auto deviceIt = info_VkDevice.find(device); |
| 817 | |
| 818 | if (deviceIt == info_VkDevice.end()) { |
| 819 | return VK_ERROR_INITIALIZATION_FAILED; |
| 820 | } |
| 821 | |
| 822 | auto memoryIt = info_VkDeviceMemory.find(pInfo->memory); |
| 823 | |
| 824 | if (memoryIt == info_VkDeviceMemory.end()) { |
| 825 | return VK_ERROR_INITIALIZATION_FAILED; |
| 826 | } |
| 827 | |
| 828 | auto& info = memoryIt->second; |
| 829 | |
| 830 | VkResult queryRes = |
| 831 | getMemoryAndroidHardwareBufferANDROID(info.ahbHandle); |
| 832 | |
| 833 | if (queryRes != VK_SUCCESS) return queryRes; |
| 834 | |
| 835 | *pBuffer = *(info.ahbHandle); |
| 836 | |
| 837 | return queryRes; |
| 838 | } |
| 839 | |
| Lingfeng Yang | 131d5a4 | 2018-11-30 12:00:33 -0800 | [diff] [blame] | 840 | VkResult on_vkAllocateMemory( |
| Lingfeng Yang | 236abc9 | 2018-12-21 20:19:33 -0800 | [diff] [blame] | 841 | void* context, |
| Lingfeng Yang | 131d5a4 | 2018-11-30 12:00:33 -0800 | [diff] [blame] | 842 | VkResult input_result, |
| 843 | VkDevice device, |
| 844 | const VkMemoryAllocateInfo* pAllocateInfo, |
| Lingfeng Yang | e997952 | 2018-12-25 14:44:52 -0800 | [diff] [blame] | 845 | const VkAllocationCallbacks* pAllocator, |
| Lingfeng Yang | 131d5a4 | 2018-11-30 12:00:33 -0800 | [diff] [blame] | 846 | VkDeviceMemory* pMemory) { |
| 847 | |
| 848 | if (input_result != VK_SUCCESS) return input_result; |
| 849 | |
| Lingfeng Yang | e997952 | 2018-12-25 14:44:52 -0800 | [diff] [blame] | 850 | VkEncoder* enc = (VkEncoder*)context; |
| 851 | |
| Lingfeng Yang | 35e9c6a | 2018-12-25 17:13:36 -0800 | [diff] [blame] | 852 | // Device local memory: pass through |
| 853 | if (!isHostVisibleMemoryTypeIndexForGuest( |
| 854 | &mHostVisibleMemoryVirtInfo, |
| 855 | pAllocateInfo->memoryTypeIndex)) { |
| Lingfeng Yang | e997952 | 2018-12-25 14:44:52 -0800 | [diff] [blame] | 856 | |
| Lingfeng Yang | 35e9c6a | 2018-12-25 17:13:36 -0800 | [diff] [blame] | 857 | input_result = |
| 858 | enc->vkAllocateMemory( |
| 859 | device, pAllocateInfo, pAllocator, pMemory); |
| 860 | |
| 861 | if (input_result != VK_SUCCESS) return input_result; |
| 862 | |
| 863 | VkDeviceSize allocationSize = pAllocateInfo->allocationSize; |
| 864 | setDeviceMemoryInfo( |
| 865 | device, *pMemory, |
| 866 | pAllocateInfo->allocationSize, |
| 867 | 0, nullptr, |
| 868 | pAllocateInfo->memoryTypeIndex); |
| 869 | |
| 870 | return VK_SUCCESS; |
| 871 | } |
| 872 | |
| 873 | // Host visible memory with no direct mapping support |
| Lingfeng Yang | afe29d3 | 2018-12-25 13:01:52 -0800 | [diff] [blame] | 874 | bool directMappingSupported = usingDirectMapping(); |
| Lingfeng Yang | 35e9c6a | 2018-12-25 17:13:36 -0800 | [diff] [blame] | 875 | if (!directMappingSupported) { |
| Lingfeng Yang | 630abfb | 2018-12-26 12:31:47 -0800 | [diff] [blame] | 876 | input_result = |
| 877 | enc->vkAllocateMemory( |
| 878 | device, pAllocateInfo, pAllocator, pMemory); |
| 879 | |
| 880 | if (input_result != VK_SUCCESS) return input_result; |
| 881 | |
| Lingfeng Yang | 35e9c6a | 2018-12-25 17:13:36 -0800 | [diff] [blame] | 882 | VkDeviceSize mappedSize = |
| 883 | getNonCoherentExtendedSize(device, |
| 884 | pAllocateInfo->allocationSize); |
| 885 | uint8_t* mappedPtr = (uint8_t*)aligned_buf_alloc(4096, mappedSize); |
| Lingfeng Yang | 236abc9 | 2018-12-21 20:19:33 -0800 | [diff] [blame] | 886 | D("host visible alloc (non-direct): " |
| 887 | "size 0x%llx host ptr %p mapped size 0x%llx", |
| Lingfeng Yang | 49c7de2 | 2019-01-23 16:19:50 -0800 | [diff] [blame] | 888 | (unsigned long long)pAllocateInfo->allocationSize, mappedPtr, |
| Lingfeng Yang | 236abc9 | 2018-12-21 20:19:33 -0800 | [diff] [blame] | 889 | (unsigned long long)mappedSize); |
| Lingfeng Yang | 35e9c6a | 2018-12-25 17:13:36 -0800 | [diff] [blame] | 890 | setDeviceMemoryInfo( |
| 891 | device, *pMemory, |
| 892 | pAllocateInfo->allocationSize, |
| 893 | mappedSize, mappedPtr, |
| 894 | pAllocateInfo->memoryTypeIndex); |
| 895 | return VK_SUCCESS; |
| Lingfeng Yang | 131d5a4 | 2018-11-30 12:00:33 -0800 | [diff] [blame] | 896 | } |
| 897 | |
| Lingfeng Yang | 35e9c6a | 2018-12-25 17:13:36 -0800 | [diff] [blame] | 898 | // Host visible memory with direct mapping |
| 899 | AutoLock lock(mLock); |
| Lingfeng Yang | 131d5a4 | 2018-11-30 12:00:33 -0800 | [diff] [blame] | 900 | |
| Lingfeng Yang | 35e9c6a | 2018-12-25 17:13:36 -0800 | [diff] [blame] | 901 | auto it = info_VkDevice.find(device); |
| 902 | if (it == info_VkDevice.end()) return VK_ERROR_DEVICE_LOST; |
| 903 | auto& deviceInfo = it->second; |
| Lingfeng Yang | 236abc9 | 2018-12-21 20:19:33 -0800 | [diff] [blame] | 904 | |
| Lingfeng Yang | 35e9c6a | 2018-12-25 17:13:36 -0800 | [diff] [blame] | 905 | HostMemAlloc* hostMemAlloc = |
| 906 | &deviceInfo.hostMemAllocs[pAllocateInfo->memoryTypeIndex]; |
| 907 | |
| 908 | if (!hostMemAlloc->initialized) { |
| 909 | VkMemoryAllocateInfo allocInfoForHost = *pAllocateInfo; |
| 910 | allocInfoForHost.allocationSize = VIRTUAL_HOST_VISIBLE_HEAP_SIZE; |
| 911 | // TODO: Support dedicated allocation |
| 912 | allocInfoForHost.pNext = nullptr; |
| 913 | |
| 914 | lock.unlock(); |
| 915 | VkResult host_res = |
| 916 | enc->vkAllocateMemory( |
| 917 | device, |
| 918 | &allocInfoForHost, |
| 919 | nullptr, |
| 920 | &hostMemAlloc->memory); |
| 921 | lock.lock(); |
| 922 | |
| 923 | if (host_res != VK_SUCCESS) { |
| 924 | ALOGE("Could not allocate backing for virtual host visible memory: %d", |
| 925 | host_res); |
| 926 | hostMemAlloc->initialized = true; |
| 927 | hostMemAlloc->initResult = host_res; |
| 928 | return host_res; |
| 929 | } |
| 930 | |
| 931 | auto& hostMemInfo = info_VkDeviceMemory[hostMemAlloc->memory]; |
| 932 | hostMemInfo.allocationSize = allocInfoForHost.allocationSize; |
| 933 | VkDeviceSize nonCoherentAtomSize = |
| 934 | deviceInfo.props.limits.nonCoherentAtomSize; |
| 935 | hostMemInfo.mappedSize = hostMemInfo.allocationSize; |
| 936 | hostMemInfo.memoryTypeIndex = |
| 937 | pAllocateInfo->memoryTypeIndex; |
| Lingfeng Yang | 49c7de2 | 2019-01-23 16:19:50 -0800 | [diff] [blame] | 938 | hostMemAlloc->nonCoherentAtomSize = nonCoherentAtomSize; |
| Lingfeng Yang | 236abc9 | 2018-12-21 20:19:33 -0800 | [diff] [blame] | 939 | |
| 940 | uint64_t directMappedAddr = 0; |
| Lingfeng Yang | 35e9c6a | 2018-12-25 17:13:36 -0800 | [diff] [blame] | 941 | lock.unlock(); |
| Lingfeng Yang | 236abc9 | 2018-12-21 20:19:33 -0800 | [diff] [blame] | 942 | VkResult directMapResult = |
| 943 | enc->vkMapMemoryIntoAddressSpaceGOOGLE( |
| Lingfeng Yang | 35e9c6a | 2018-12-25 17:13:36 -0800 | [diff] [blame] | 944 | device, hostMemAlloc->memory, &directMappedAddr); |
| 945 | lock.lock(); |
| Lingfeng Yang | 236abc9 | 2018-12-21 20:19:33 -0800 | [diff] [blame] | 946 | |
| 947 | if (directMapResult != VK_SUCCESS) { |
| Lingfeng Yang | 35e9c6a | 2018-12-25 17:13:36 -0800 | [diff] [blame] | 948 | hostMemAlloc->initialized = true; |
| 949 | hostMemAlloc->initResult = directMapResult; |
| Lingfeng Yang | 236abc9 | 2018-12-21 20:19:33 -0800 | [diff] [blame] | 950 | return directMapResult; |
| 951 | } |
| 952 | |
| Lingfeng Yang | 35e9c6a | 2018-12-25 17:13:36 -0800 | [diff] [blame] | 953 | hostMemInfo.mappedPtr = |
| 954 | (uint8_t*)(uintptr_t)directMappedAddr; |
| 955 | hostMemInfo.virtualHostVisibleBacking = true; |
| Lingfeng Yang | 236abc9 | 2018-12-21 20:19:33 -0800 | [diff] [blame] | 956 | |
| Lingfeng Yang | 35e9c6a | 2018-12-25 17:13:36 -0800 | [diff] [blame] | 957 | VkResult hostMemAllocRes = |
| 958 | finishHostMemAllocInit( |
| 959 | enc, |
| 960 | device, |
| 961 | pAllocateInfo->memoryTypeIndex, |
| 962 | nonCoherentAtomSize, |
| 963 | hostMemInfo.allocationSize, |
| 964 | hostMemInfo.mappedSize, |
| 965 | hostMemInfo.mappedPtr, |
| 966 | hostMemAlloc); |
| 967 | |
| 968 | if (hostMemAllocRes != VK_SUCCESS) { |
| 969 | return hostMemAllocRes; |
| Lingfeng Yang | 236abc9 | 2018-12-21 20:19:33 -0800 | [diff] [blame] | 970 | } |
| Lingfeng Yang | 236abc9 | 2018-12-21 20:19:33 -0800 | [diff] [blame] | 971 | } |
| 972 | |
| Lingfeng Yang | 35e9c6a | 2018-12-25 17:13:36 -0800 | [diff] [blame] | 973 | VkDeviceMemory_Info virtualMemInfo; |
| 974 | |
| 975 | subAllocHostMemory( |
| 976 | hostMemAlloc, |
| 977 | pAllocateInfo, |
| 978 | &virtualMemInfo.subAlloc); |
| 979 | |
| 980 | virtualMemInfo.allocationSize = virtualMemInfo.subAlloc.subAllocSize; |
| 981 | virtualMemInfo.mappedSize = virtualMemInfo.subAlloc.subMappedSize; |
| 982 | virtualMemInfo.mappedPtr = virtualMemInfo.subAlloc.mappedPtr; |
| 983 | virtualMemInfo.memoryTypeIndex = pAllocateInfo->memoryTypeIndex; |
| 984 | virtualMemInfo.directMapped = true; |
| 985 | |
| 986 | D("host visible alloc (direct, suballoc): " |
| 987 | "size 0x%llx ptr %p mapped size 0x%llx", |
| 988 | (unsigned long long)virtualMemInfo.allocationSize, virtualMemInfo.mappedPtr, |
| Lingfeng Yang | 49c7de2 | 2019-01-23 16:19:50 -0800 | [diff] [blame] | 989 | (unsigned long long)virtualMemInfo.mappedSize); |
| Lingfeng Yang | 35e9c6a | 2018-12-25 17:13:36 -0800 | [diff] [blame] | 990 | |
| 991 | info_VkDeviceMemory[ |
| 992 | virtualMemInfo.subAlloc.subMemory] = virtualMemInfo; |
| 993 | |
| 994 | *pMemory = virtualMemInfo.subAlloc.subMemory; |
| 995 | |
| 996 | return VK_SUCCESS; |
| Lingfeng Yang | 131d5a4 | 2018-11-30 12:00:33 -0800 | [diff] [blame] | 997 | } |
| 998 | |
| Lingfeng Yang | e997952 | 2018-12-25 14:44:52 -0800 | [diff] [blame] | 999 | void on_vkFreeMemory( |
| 1000 | void* context, |
| 1001 | VkDevice device, |
| 1002 | VkDeviceMemory memory, |
| 1003 | const VkAllocationCallbacks* pAllocateInfo) { |
| 1004 | |
| Lingfeng Yang | 35e9c6a | 2018-12-25 17:13:36 -0800 | [diff] [blame] | 1005 | AutoLock lock(mLock); |
| 1006 | |
| 1007 | auto it = info_VkDeviceMemory.find(memory); |
| 1008 | if (it == info_VkDeviceMemory.end()) return; |
| 1009 | auto& info = it->second; |
| 1010 | |
| 1011 | if (!info.directMapped) { |
| 1012 | lock.unlock(); |
| 1013 | VkEncoder* enc = (VkEncoder*)context; |
| 1014 | enc->vkFreeMemory(device, memory, pAllocateInfo); |
| 1015 | return; |
| 1016 | } |
| 1017 | |
| 1018 | subFreeHostMemory(&info.subAlloc); |
| Lingfeng Yang | e997952 | 2018-12-25 14:44:52 -0800 | [diff] [blame] | 1019 | } |
| 1020 | |
| Lingfeng Yang | def88ba | 2018-12-13 12:43:17 -0800 | [diff] [blame] | 1021 | VkResult on_vkMapMemory( |
| 1022 | void*, |
| 1023 | VkResult host_result, |
| 1024 | VkDevice, |
| 1025 | VkDeviceMemory memory, |
| 1026 | VkDeviceSize offset, |
| 1027 | VkDeviceSize size, |
| 1028 | VkMemoryMapFlags, |
| 1029 | void** ppData) { |
| 1030 | |
| 1031 | if (host_result != VK_SUCCESS) return host_result; |
| 1032 | |
| 1033 | AutoLock lock(mLock); |
| 1034 | |
| 1035 | auto it = info_VkDeviceMemory.find(memory); |
| 1036 | if (it == info_VkDeviceMemory.end()) return VK_ERROR_MEMORY_MAP_FAILED; |
| 1037 | |
| 1038 | auto& info = it->second; |
| 1039 | |
| 1040 | if (!info.mappedPtr) return VK_ERROR_MEMORY_MAP_FAILED; |
| 1041 | |
| 1042 | if (size != VK_WHOLE_SIZE && |
| 1043 | (info.mappedPtr + offset + size > info.mappedPtr + info.allocationSize)) { |
| 1044 | return VK_ERROR_MEMORY_MAP_FAILED; |
| 1045 | } |
| 1046 | |
| 1047 | *ppData = info.mappedPtr + offset; |
| 1048 | |
| 1049 | return host_result; |
| 1050 | } |
| 1051 | |
| 1052 | void on_vkUnmapMemory( |
| 1053 | void*, |
| 1054 | VkDevice, |
| 1055 | VkDeviceMemory) { |
| 1056 | // no-op |
| 1057 | } |
| 1058 | |
| Lingfeng Yang | 4af5f32 | 2019-02-14 08:10:28 -0800 | [diff] [blame] | 1059 | VkResult on_vkCreateImage( |
| 1060 | void* context, VkResult, |
| 1061 | VkDevice device, const VkImageCreateInfo *pCreateInfo, |
| 1062 | const VkAllocationCallbacks *pAllocator, |
| 1063 | VkImage *pImage) { |
| 1064 | VkEncoder* enc = (VkEncoder*)context; |
| Lingfeng Yang | 3e87e85 | 2019-02-19 14:12:49 -0800 | [diff] [blame] | 1065 | |
| 1066 | VkResult res = enc->vkCreateImage(device, pCreateInfo, pAllocator, pImage); |
| 1067 | |
| 1068 | if (res != VK_SUCCESS) return res; |
| 1069 | |
| 1070 | AutoLock lock(mLock); |
| 1071 | |
| 1072 | auto it = info_VkImage.find(*pImage); |
| 1073 | if (it == info_VkImage.end()) return VK_ERROR_INITIALIZATION_FAILED; |
| 1074 | |
| 1075 | auto& info = it->second; |
| 1076 | |
| 1077 | info.createInfo = *pCreateInfo; |
| 1078 | info.createInfo.pNext = nullptr; |
| 1079 | |
| 1080 | return res; |
| Lingfeng Yang | 4af5f32 | 2019-02-14 08:10:28 -0800 | [diff] [blame] | 1081 | } |
| 1082 | |
| 1083 | void on_vkDestroyImage( |
| 1084 | void* context, |
| 1085 | VkDevice device, VkImage image, const VkAllocationCallbacks *pAllocator) { |
| 1086 | VkEncoder* enc = (VkEncoder*)context; |
| 1087 | enc->vkDestroyImage(device, image, pAllocator); |
| 1088 | } |
| 1089 | |
| 1090 | void on_vkGetImageMemoryRequirements( |
| 1091 | void *context, VkDevice device, VkImage image, |
| 1092 | VkMemoryRequirements *pMemoryRequirements) { |
| 1093 | VkEncoder* enc = (VkEncoder*)context; |
| 1094 | enc->vkGetImageMemoryRequirements( |
| 1095 | device, image, pMemoryRequirements); |
| 1096 | } |
| 1097 | |
| 1098 | void on_vkGetImageMemoryRequirements2( |
| 1099 | void *context, VkDevice device, const VkImageMemoryRequirementsInfo2 *pInfo, |
| 1100 | VkMemoryRequirements2 *pMemoryRequirements) { |
| 1101 | VkEncoder* enc = (VkEncoder*)context; |
| 1102 | enc->vkGetImageMemoryRequirements2( |
| 1103 | device, pInfo, pMemoryRequirements); |
| 1104 | } |
| 1105 | |
| 1106 | void on_vkGetImageMemoryRequirements2KHR( |
| 1107 | void *context, VkDevice device, const VkImageMemoryRequirementsInfo2 *pInfo, |
| 1108 | VkMemoryRequirements2 *pMemoryRequirements) { |
| 1109 | VkEncoder* enc = (VkEncoder*)context; |
| 1110 | enc->vkGetImageMemoryRequirements2KHR( |
| 1111 | device, pInfo, pMemoryRequirements); |
| 1112 | } |
| 1113 | |
| 1114 | VkResult on_vkBindImageMemory( |
| 1115 | void* context, VkResult, |
| 1116 | VkDevice device, VkImage image, VkDeviceMemory memory, |
| 1117 | VkDeviceSize memoryOffset) { |
| 1118 | VkEncoder* enc = (VkEncoder*)context; |
| 1119 | return enc->vkBindImageMemory(device, image, memory, memoryOffset); |
| 1120 | } |
| 1121 | |
| 1122 | VkResult on_vkBindImageMemory2( |
| 1123 | void* context, VkResult, |
| 1124 | VkDevice device, uint32_t bindingCount, const VkBindImageMemoryInfo* pBindInfos) { |
| 1125 | VkEncoder* enc = (VkEncoder*)context; |
| 1126 | return enc->vkBindImageMemory2(device, bindingCount, pBindInfos); |
| 1127 | } |
| 1128 | |
| 1129 | VkResult on_vkBindImageMemory2KHR( |
| 1130 | void* context, VkResult, |
| 1131 | VkDevice device, uint32_t bindingCount, const VkBindImageMemoryInfo* pBindInfos) { |
| 1132 | VkEncoder* enc = (VkEncoder*)context; |
| 1133 | return enc->vkBindImageMemory2KHR(device, bindingCount, pBindInfos); |
| 1134 | } |
| 1135 | |
| 1136 | VkResult on_vkCreateBuffer( |
| 1137 | void* context, VkResult, |
| 1138 | VkDevice device, const VkBufferCreateInfo *pCreateInfo, |
| 1139 | const VkAllocationCallbacks *pAllocator, |
| 1140 | VkBuffer *pBuffer) { |
| 1141 | VkEncoder* enc = (VkEncoder*)context; |
| Lingfeng Yang | 3e87e85 | 2019-02-19 14:12:49 -0800 | [diff] [blame] | 1142 | |
| 1143 | VkResult res = enc->vkCreateBuffer(device, pCreateInfo, pAllocator, pBuffer); |
| 1144 | |
| 1145 | if (res != VK_SUCCESS) return res; |
| 1146 | |
| 1147 | AutoLock lock(mLock); |
| 1148 | |
| 1149 | auto it = info_VkBuffer.find(*pBuffer); |
| 1150 | if (it == info_VkBuffer.end()) return VK_ERROR_INITIALIZATION_FAILED; |
| 1151 | |
| 1152 | auto& info = it->second; |
| 1153 | |
| 1154 | info.createInfo = *pCreateInfo; |
| 1155 | info.createInfo.pNext = nullptr; |
| 1156 | |
| 1157 | return res; |
| Lingfeng Yang | 4af5f32 | 2019-02-14 08:10:28 -0800 | [diff] [blame] | 1158 | } |
| 1159 | |
| 1160 | void on_vkDestroyBuffer( |
| 1161 | void* context, |
| 1162 | VkDevice device, VkBuffer buffer, const VkAllocationCallbacks *pAllocator) { |
| 1163 | VkEncoder* enc = (VkEncoder*)context; |
| 1164 | enc->vkDestroyBuffer(device, buffer, pAllocator); |
| 1165 | } |
| 1166 | |
| 1167 | void on_vkGetBufferMemoryRequirements( |
| 1168 | void* context, VkDevice device, VkBuffer buffer, VkMemoryRequirements *pMemoryRequirements) { |
| 1169 | VkEncoder* enc = (VkEncoder*)context; |
| 1170 | enc->vkGetBufferMemoryRequirements( |
| 1171 | device, buffer, pMemoryRequirements); |
| 1172 | } |
| 1173 | |
| 1174 | void on_vkGetBufferMemoryRequirements2( |
| 1175 | void* context, VkDevice device, const VkBufferMemoryRequirementsInfo2* pInfo, |
| 1176 | VkMemoryRequirements2* pMemoryRequirements) { |
| 1177 | VkEncoder* enc = (VkEncoder*)context; |
| 1178 | enc->vkGetBufferMemoryRequirements2(device, pInfo, pMemoryRequirements); |
| 1179 | } |
| 1180 | |
| 1181 | void on_vkGetBufferMemoryRequirements2KHR( |
| 1182 | void* context, VkDevice device, const VkBufferMemoryRequirementsInfo2* pInfo, |
| 1183 | VkMemoryRequirements2* pMemoryRequirements) { |
| 1184 | VkEncoder* enc = (VkEncoder*)context; |
| 1185 | enc->vkGetBufferMemoryRequirements2KHR(device, pInfo, pMemoryRequirements); |
| 1186 | } |
| 1187 | |
| 1188 | VkResult on_vkBindBufferMemory( |
| 1189 | void *context, VkResult, |
| 1190 | VkDevice device, VkBuffer buffer, VkDeviceMemory memory, VkDeviceSize memoryOffset) { |
| 1191 | VkEncoder *enc = (VkEncoder *)context; |
| 1192 | return enc->vkBindBufferMemory( |
| 1193 | device, buffer, memory, memoryOffset); |
| 1194 | } |
| 1195 | |
| 1196 | VkResult on_vkBindBufferMemory2( |
| 1197 | void *context, VkResult, |
| 1198 | VkDevice device, uint32_t bindInfoCount, const VkBindBufferMemoryInfo *pBindInfos) { |
| 1199 | VkEncoder *enc = (VkEncoder *)context; |
| 1200 | return enc->vkBindBufferMemory2( |
| 1201 | device, bindInfoCount, pBindInfos); |
| 1202 | } |
| 1203 | |
| 1204 | VkResult on_vkBindBufferMemory2KHR( |
| 1205 | void *context, VkResult, |
| 1206 | VkDevice device, uint32_t bindInfoCount, const VkBindBufferMemoryInfo *pBindInfos) { |
| 1207 | VkEncoder *enc = (VkEncoder *)context; |
| 1208 | return enc->vkBindBufferMemory2KHR( |
| 1209 | device, bindInfoCount, pBindInfos); |
| 1210 | } |
| 1211 | |
| David Reveman | 32b110e | 2019-02-21 13:20:54 -0500 | [diff] [blame^] | 1212 | VkResult on_vkCreateSemaphore( |
| 1213 | void* context, VkResult, |
| 1214 | VkDevice device, const VkSemaphoreCreateInfo* pCreateInfo, |
| 1215 | const VkAllocationCallbacks* pAllocator, |
| 1216 | VkSemaphore* pSemaphore) { |
| 1217 | VkEncoder* enc = (VkEncoder*)context; |
| 1218 | return enc->vkCreateSemaphore( |
| 1219 | device, pCreateInfo, pAllocator, pSemaphore); |
| 1220 | } |
| 1221 | |
| 1222 | void on_vkDestroySemaphore( |
| 1223 | void* context, |
| 1224 | VkDevice device, VkSemaphore semaphore, const VkAllocationCallbacks *pAllocator) { |
| 1225 | VkEncoder* enc = (VkEncoder*)context; |
| 1226 | enc->vkDestroySemaphore(device, semaphore, pAllocator); |
| 1227 | } |
| 1228 | |
| Lingfeng Yang | def88ba | 2018-12-13 12:43:17 -0800 | [diff] [blame] | 1229 | void unwrap_VkNativeBufferANDROID( |
| 1230 | const VkImageCreateInfo* pCreateInfo, |
| 1231 | VkImageCreateInfo* local_pCreateInfo) { |
| 1232 | |
| 1233 | if (!pCreateInfo->pNext) return; |
| 1234 | |
| 1235 | const VkNativeBufferANDROID* nativeInfo = |
| 1236 | reinterpret_cast<const VkNativeBufferANDROID*>(pCreateInfo->pNext); |
| 1237 | |
| 1238 | if (VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID != nativeInfo->sType) { |
| 1239 | return; |
| 1240 | } |
| 1241 | |
| 1242 | const cb_handle_t* cb_handle = |
| 1243 | reinterpret_cast<const cb_handle_t*>(nativeInfo->handle); |
| 1244 | |
| 1245 | if (!cb_handle) return; |
| 1246 | |
| 1247 | VkNativeBufferANDROID* nativeInfoOut = |
| Lingfeng Yang | e637ca5 | 2018-12-14 18:24:56 -0800 | [diff] [blame] | 1248 | reinterpret_cast<VkNativeBufferANDROID*>( |
| 1249 | const_cast<void*>( |
| 1250 | local_pCreateInfo->pNext)); |
| Lingfeng Yang | def88ba | 2018-12-13 12:43:17 -0800 | [diff] [blame] | 1251 | |
| 1252 | if (!nativeInfoOut->handle) { |
| 1253 | ALOGE("FATAL: Local native buffer info not properly allocated!"); |
| 1254 | abort(); |
| 1255 | } |
| 1256 | |
| 1257 | *(uint32_t*)(nativeInfoOut->handle) = cb_handle->hostHandle; |
| 1258 | } |
| 1259 | |
| 1260 | void unwrap_vkAcquireImageANDROID_nativeFenceFd(int fd, int*) { |
| 1261 | if (fd != -1) { |
| 1262 | sync_wait(fd, 3000); |
| 1263 | } |
| 1264 | } |
| 1265 | |
| David Reveman | 32b110e | 2019-02-21 13:20:54 -0500 | [diff] [blame^] | 1266 | void unwrap_vkQueueSubmit(uint32_t, const VkSubmitInfo*, VkSubmitInfo*) { |
| 1267 | } |
| 1268 | |
| Lingfeng Yang | 236abc9 | 2018-12-21 20:19:33 -0800 | [diff] [blame] | 1269 | // Action of vkMapMemoryIntoAddressSpaceGOOGLE: |
| 1270 | // 1. preprocess (on_vkMapMemoryIntoAddressSpaceGOOGLE_pre): |
| 1271 | // uses address space device to reserve the right size of |
| 1272 | // memory. |
| 1273 | // 2. the reservation results in a physical address. the physical |
| 1274 | // address is set as |*pAddress|. |
| 1275 | // 3. after pre, the API call is encoded to the host, where the |
| 1276 | // value of pAddress is also sent (the physical address). |
| 1277 | // 4. the host will obtain the actual gpu pointer and send it |
| 1278 | // back out in |*pAddress|. |
| 1279 | // 5. postprocess (on_vkMapMemoryIntoAddressSpaceGOOGLE) will run, |
| 1280 | // using the mmap() method of GoldfishAddressSpaceBlock to obtain |
| 1281 | // a pointer in guest userspace corresponding to the host pointer. |
| 1282 | VkResult on_vkMapMemoryIntoAddressSpaceGOOGLE_pre( |
| 1283 | void*, |
| 1284 | VkResult, |
| 1285 | VkDevice, |
| 1286 | VkDeviceMemory memory, |
| 1287 | uint64_t* pAddress) { |
| 1288 | |
| 1289 | AutoLock lock(mLock); |
| 1290 | |
| 1291 | auto it = info_VkDeviceMemory.find(memory); |
| 1292 | if (it == info_VkDeviceMemory.end()) { |
| 1293 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
| 1294 | } |
| 1295 | |
| 1296 | auto& memInfo = it->second; |
| Lingfeng Yang | 35e9c6a | 2018-12-25 17:13:36 -0800 | [diff] [blame] | 1297 | memInfo.goldfishAddressSpaceBlock = |
| 1298 | new GoldfishAddressSpaceBlock; |
| Lingfeng Yang | 236abc9 | 2018-12-21 20:19:33 -0800 | [diff] [blame] | 1299 | auto& block = *(memInfo.goldfishAddressSpaceBlock); |
| 1300 | |
| 1301 | block.allocate( |
| 1302 | mGoldfishAddressSpaceBlockProvider.get(), |
| 1303 | memInfo.mappedSize); |
| 1304 | |
| 1305 | *pAddress = block.physAddr(); |
| 1306 | |
| 1307 | return VK_SUCCESS; |
| 1308 | } |
| 1309 | |
| 1310 | VkResult on_vkMapMemoryIntoAddressSpaceGOOGLE( |
| 1311 | void*, |
| 1312 | VkResult input_result, |
| 1313 | VkDevice, |
| 1314 | VkDeviceMemory memory, |
| 1315 | uint64_t* pAddress) { |
| 1316 | |
| 1317 | if (input_result != VK_SUCCESS) { |
| 1318 | return input_result; |
| 1319 | } |
| 1320 | |
| 1321 | // Now pAddress points to the gpu addr from host. |
| 1322 | AutoLock lock(mLock); |
| 1323 | |
| 1324 | auto it = info_VkDeviceMemory.find(memory); |
| 1325 | if (it == info_VkDeviceMemory.end()) { |
| 1326 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
| 1327 | } |
| 1328 | |
| 1329 | auto& memInfo = it->second; |
| 1330 | auto& block = *(memInfo.goldfishAddressSpaceBlock); |
| 1331 | |
| 1332 | uint64_t gpuAddr = *pAddress; |
| 1333 | |
| 1334 | void* userPtr = block.mmap(gpuAddr); |
| 1335 | |
| Lingfeng Yang | 49c7de2 | 2019-01-23 16:19:50 -0800 | [diff] [blame] | 1336 | D("%s: Got new host visible alloc. " |
| 1337 | "Sizeof void: %zu map size: %zu Range: [%p %p]", |
| 1338 | __func__, |
| 1339 | sizeof(void*), (size_t)memInfo.mappedSize, |
| 1340 | userPtr, |
| 1341 | (unsigned char*)userPtr + memInfo.mappedSize); |
| 1342 | |
| Lingfeng Yang | 236abc9 | 2018-12-21 20:19:33 -0800 | [diff] [blame] | 1343 | *pAddress = (uint64_t)(uintptr_t)userPtr; |
| 1344 | |
| 1345 | return input_result; |
| 1346 | } |
| 1347 | |
| Lingfeng Yang | f0654ff | 2019-02-02 12:21:24 -0800 | [diff] [blame] | 1348 | uint32_t getApiVersionFromInstance(VkInstance instance) const { |
| 1349 | AutoLock lock(mLock); |
| 1350 | uint32_t api = kMinApiVersion; |
| 1351 | |
| 1352 | auto it = info_VkInstance.find(instance); |
| 1353 | if (it == info_VkInstance.end()) return api; |
| 1354 | |
| 1355 | api = it->second.highestApiVersion; |
| 1356 | |
| 1357 | return api; |
| 1358 | } |
| 1359 | |
| 1360 | uint32_t getApiVersionFromDevice(VkDevice device) const { |
| 1361 | AutoLock lock(mLock); |
| 1362 | |
| 1363 | uint32_t api = kMinApiVersion; |
| 1364 | |
| 1365 | auto it = info_VkDevice.find(device); |
| 1366 | if (it == info_VkDevice.end()) return api; |
| 1367 | |
| 1368 | api = it->second.apiVersion; |
| 1369 | |
| 1370 | return api; |
| 1371 | } |
| 1372 | |
| 1373 | bool hasInstanceExtension(VkInstance instance, const std::string& name) const { |
| 1374 | AutoLock lock(mLock); |
| 1375 | |
| 1376 | auto it = info_VkInstance.find(instance); |
| 1377 | if (it == info_VkInstance.end()) return false; |
| 1378 | |
| 1379 | return it->second.enabledExtensions.find(name) != |
| 1380 | it->second.enabledExtensions.end(); |
| 1381 | } |
| 1382 | |
| 1383 | bool hasDeviceExtension(VkDevice device, const std::string& name) const { |
| 1384 | AutoLock lock(mLock); |
| 1385 | |
| 1386 | auto it = info_VkDevice.find(device); |
| 1387 | if (it == info_VkDevice.end()) return false; |
| 1388 | |
| 1389 | return it->second.enabledExtensions.find(name) != |
| 1390 | it->second.enabledExtensions.end(); |
| 1391 | } |
| 1392 | |
| Lingfeng Yang | 6ab1b0d | 2018-11-27 23:36:03 -0800 | [diff] [blame] | 1393 | private: |
| 1394 | mutable Lock mLock; |
| Lingfeng Yang | afe29d3 | 2018-12-25 13:01:52 -0800 | [diff] [blame] | 1395 | HostVisibleMemoryVirtualizationInfo mHostVisibleMemoryVirtInfo; |
| Lingfeng Yang | 3175463 | 2018-12-21 18:24:55 -0800 | [diff] [blame] | 1396 | std::unique_ptr<EmulatorFeatureInfo> mFeatureInfo; |
| Lingfeng Yang | 236abc9 | 2018-12-21 20:19:33 -0800 | [diff] [blame] | 1397 | std::unique_ptr<GoldfishAddressSpaceBlockProvider> mGoldfishAddressSpaceBlockProvider; |
| Lingfeng Yang | 154a33c | 2019-01-29 19:06:23 -0800 | [diff] [blame] | 1398 | |
| 1399 | std::vector<VkExtensionProperties> mHostInstanceExtensions; |
| 1400 | std::vector<VkExtensionProperties> mHostDeviceExtensions; |
| Lingfeng Yang | 71b596b | 2018-11-07 18:03:25 -0800 | [diff] [blame] | 1401 | }; |
| Lingfeng Yang | 154a33c | 2019-01-29 19:06:23 -0800 | [diff] [blame] | 1402 | |
| Lingfeng Yang | 71b596b | 2018-11-07 18:03:25 -0800 | [diff] [blame] | 1403 | ResourceTracker::ResourceTracker() : mImpl(new ResourceTracker::Impl()) { } |
| 1404 | ResourceTracker::~ResourceTracker() { } |
| Lingfeng Yang | 71b596b | 2018-11-07 18:03:25 -0800 | [diff] [blame] | 1405 | VulkanHandleMapping* ResourceTracker::createMapping() { |
| 1406 | return &mImpl->createMapping; |
| 1407 | } |
| Lingfeng Yang | 71b596b | 2018-11-07 18:03:25 -0800 | [diff] [blame] | 1408 | VulkanHandleMapping* ResourceTracker::unwrapMapping() { |
| 1409 | return &mImpl->unwrapMapping; |
| 1410 | } |
| Lingfeng Yang | 71b596b | 2018-11-07 18:03:25 -0800 | [diff] [blame] | 1411 | VulkanHandleMapping* ResourceTracker::destroyMapping() { |
| 1412 | return &mImpl->destroyMapping; |
| 1413 | } |
| Lingfeng Yang | 2285df1 | 2018-11-17 16:25:11 -0800 | [diff] [blame] | 1414 | VulkanHandleMapping* ResourceTracker::defaultMapping() { |
| 1415 | return &mImpl->defaultMapping; |
| 1416 | } |
| Lingfeng Yang | 71b596b | 2018-11-07 18:03:25 -0800 | [diff] [blame] | 1417 | static ResourceTracker* sTracker = nullptr; |
| Lingfeng Yang | 71b596b | 2018-11-07 18:03:25 -0800 | [diff] [blame] | 1418 | // static |
| 1419 | ResourceTracker* ResourceTracker::get() { |
| 1420 | if (!sTracker) { |
| 1421 | // To be initialized once on vulkan device open. |
| 1422 | sTracker = new ResourceTracker; |
| 1423 | } |
| 1424 | return sTracker; |
| 1425 | } |
| Lingfeng Yang | 71b596b | 2018-11-07 18:03:25 -0800 | [diff] [blame] | 1426 | |
| Lingfeng Yang | 6ab1b0d | 2018-11-27 23:36:03 -0800 | [diff] [blame] | 1427 | #define HANDLE_REGISTER_IMPL(type) \ |
| 1428 | void ResourceTracker::register_##type(type obj) { \ |
| 1429 | mImpl->register_##type(obj); \ |
| 1430 | } \ |
| 1431 | void ResourceTracker::unregister_##type(type obj) { \ |
| 1432 | mImpl->unregister_##type(obj); \ |
| 1433 | } \ |
| 1434 | |
| 1435 | GOLDFISH_VK_LIST_HANDLE_TYPES(HANDLE_REGISTER_IMPL) |
| 1436 | |
| Lingfeng Yang | 6ab1b0d | 2018-11-27 23:36:03 -0800 | [diff] [blame] | 1437 | bool ResourceTracker::isMemoryTypeHostVisible( |
| 1438 | VkDevice device, uint32_t typeIndex) const { |
| 1439 | return mImpl->isMemoryTypeHostVisible(device, typeIndex); |
| 1440 | } |
| 1441 | |
| Lingfeng Yang | def88ba | 2018-12-13 12:43:17 -0800 | [diff] [blame] | 1442 | uint8_t* ResourceTracker::getMappedPointer(VkDeviceMemory memory) { |
| 1443 | return mImpl->getMappedPointer(memory); |
| 1444 | } |
| 1445 | |
| 1446 | VkDeviceSize ResourceTracker::getMappedSize(VkDeviceMemory memory) { |
| 1447 | return mImpl->getMappedSize(memory); |
| 1448 | } |
| 1449 | |
| Lingfeng Yang | 6ab1b0d | 2018-11-27 23:36:03 -0800 | [diff] [blame] | 1450 | VkDeviceSize ResourceTracker::getNonCoherentExtendedSize(VkDevice device, VkDeviceSize basicSize) const { |
| 1451 | return mImpl->getNonCoherentExtendedSize(device, basicSize); |
| 1452 | } |
| 1453 | |
| Lingfeng Yang | def88ba | 2018-12-13 12:43:17 -0800 | [diff] [blame] | 1454 | bool ResourceTracker::isValidMemoryRange(const VkMappedMemoryRange& range) const { |
| 1455 | return mImpl->isValidMemoryRange(range); |
| 1456 | } |
| 1457 | |
| Lingfeng Yang | 3175463 | 2018-12-21 18:24:55 -0800 | [diff] [blame] | 1458 | void ResourceTracker::setupFeatures(const EmulatorFeatureInfo* features) { |
| 1459 | mImpl->setupFeatures(features); |
| 1460 | } |
| 1461 | |
| Lingfeng Yang | b8a38c7 | 2019-02-02 20:27:54 -0800 | [diff] [blame] | 1462 | bool ResourceTracker::hostSupportsVulkan() const { |
| 1463 | return mImpl->hostSupportsVulkan(); |
| 1464 | } |
| 1465 | |
| Lingfeng Yang | 236abc9 | 2018-12-21 20:19:33 -0800 | [diff] [blame] | 1466 | bool ResourceTracker::usingDirectMapping() const { |
| 1467 | return mImpl->usingDirectMapping(); |
| 1468 | } |
| 1469 | |
| Lingfeng Yang | f0654ff | 2019-02-02 12:21:24 -0800 | [diff] [blame] | 1470 | uint32_t ResourceTracker::getApiVersionFromInstance(VkInstance instance) const { |
| 1471 | return mImpl->getApiVersionFromInstance(instance); |
| 1472 | } |
| 1473 | |
| 1474 | uint32_t ResourceTracker::getApiVersionFromDevice(VkDevice device) const { |
| 1475 | return mImpl->getApiVersionFromDevice(device); |
| 1476 | } |
| 1477 | bool ResourceTracker::hasInstanceExtension(VkInstance instance, const std::string &name) const { |
| 1478 | return mImpl->hasInstanceExtension(instance, name); |
| 1479 | } |
| 1480 | bool ResourceTracker::hasDeviceExtension(VkDevice device, const std::string &name) const { |
| 1481 | return mImpl->hasDeviceExtension(device, name); |
| 1482 | } |
| 1483 | |
| Lingfeng Yang | def88ba | 2018-12-13 12:43:17 -0800 | [diff] [blame] | 1484 | VkResult ResourceTracker::on_vkEnumerateInstanceVersion( |
| 1485 | void* context, |
| 1486 | VkResult input_result, |
| 1487 | uint32_t* apiVersion) { |
| 1488 | return mImpl->on_vkEnumerateInstanceVersion(context, input_result, apiVersion); |
| 1489 | } |
| 1490 | |
| Lingfeng Yang | 154a33c | 2019-01-29 19:06:23 -0800 | [diff] [blame] | 1491 | VkResult ResourceTracker::on_vkEnumerateInstanceExtensionProperties( |
| 1492 | void* context, |
| 1493 | VkResult input_result, |
| 1494 | const char* pLayerName, |
| 1495 | uint32_t* pPropertyCount, |
| 1496 | VkExtensionProperties* pProperties) { |
| 1497 | return mImpl->on_vkEnumerateInstanceExtensionProperties( |
| 1498 | context, input_result, pLayerName, pPropertyCount, pProperties); |
| 1499 | } |
| 1500 | |
| Lingfeng Yang | def88ba | 2018-12-13 12:43:17 -0800 | [diff] [blame] | 1501 | VkResult ResourceTracker::on_vkEnumerateDeviceExtensionProperties( |
| 1502 | void* context, |
| 1503 | VkResult input_result, |
| 1504 | VkPhysicalDevice physicalDevice, |
| 1505 | const char* pLayerName, |
| 1506 | uint32_t* pPropertyCount, |
| 1507 | VkExtensionProperties* pProperties) { |
| 1508 | return mImpl->on_vkEnumerateDeviceExtensionProperties( |
| 1509 | context, input_result, physicalDevice, pLayerName, pPropertyCount, pProperties); |
| 1510 | } |
| 1511 | |
| Lingfeng Yang | b64ca45 | 2019-02-14 22:04:28 -0800 | [diff] [blame] | 1512 | VkResult ResourceTracker::on_vkEnumeratePhysicalDevices( |
| 1513 | void* context, VkResult input_result, |
| 1514 | VkInstance instance, uint32_t* pPhysicalDeviceCount, |
| 1515 | VkPhysicalDevice* pPhysicalDevices) { |
| 1516 | return mImpl->on_vkEnumeratePhysicalDevices( |
| 1517 | context, input_result, instance, pPhysicalDeviceCount, |
| 1518 | pPhysicalDevices); |
| 1519 | } |
| 1520 | |
| Lingfeng Yang | 97a0670 | 2018-12-24 17:02:43 -0800 | [diff] [blame] | 1521 | void ResourceTracker::on_vkGetPhysicalDeviceMemoryProperties( |
| 1522 | void* context, |
| 1523 | VkPhysicalDevice physicalDevice, |
| 1524 | VkPhysicalDeviceMemoryProperties* pMemoryProperties) { |
| 1525 | mImpl->on_vkGetPhysicalDeviceMemoryProperties( |
| 1526 | context, physicalDevice, pMemoryProperties); |
| 1527 | } |
| 1528 | |
| Lingfeng Yang | 154a33c | 2019-01-29 19:06:23 -0800 | [diff] [blame] | 1529 | void ResourceTracker::on_vkGetPhysicalDeviceMemoryProperties2( |
| 1530 | void* context, |
| 1531 | VkPhysicalDevice physicalDevice, |
| 1532 | VkPhysicalDeviceMemoryProperties2* pMemoryProperties) { |
| 1533 | mImpl->on_vkGetPhysicalDeviceMemoryProperties2( |
| 1534 | context, physicalDevice, pMemoryProperties); |
| 1535 | } |
| 1536 | |
| 1537 | void ResourceTracker::on_vkGetPhysicalDeviceMemoryProperties2KHR( |
| 1538 | void* context, |
| 1539 | VkPhysicalDevice physicalDevice, |
| 1540 | VkPhysicalDeviceMemoryProperties2* pMemoryProperties) { |
| 1541 | mImpl->on_vkGetPhysicalDeviceMemoryProperties2( |
| 1542 | context, physicalDevice, pMemoryProperties); |
| 1543 | } |
| 1544 | |
| Lingfeng Yang | f0654ff | 2019-02-02 12:21:24 -0800 | [diff] [blame] | 1545 | VkResult ResourceTracker::on_vkCreateInstance( |
| 1546 | void* context, |
| 1547 | VkResult input_result, |
| 1548 | const VkInstanceCreateInfo* pCreateInfo, |
| 1549 | const VkAllocationCallbacks* pAllocator, |
| 1550 | VkInstance* pInstance) { |
| 1551 | return mImpl->on_vkCreateInstance( |
| 1552 | context, input_result, pCreateInfo, pAllocator, pInstance); |
| 1553 | } |
| 1554 | |
| Lingfeng Yang | 131d5a4 | 2018-11-30 12:00:33 -0800 | [diff] [blame] | 1555 | VkResult ResourceTracker::on_vkCreateDevice( |
| 1556 | void* context, |
| 1557 | VkResult input_result, |
| 1558 | VkPhysicalDevice physicalDevice, |
| 1559 | const VkDeviceCreateInfo* pCreateInfo, |
| 1560 | const VkAllocationCallbacks* pAllocator, |
| 1561 | VkDevice* pDevice) { |
| 1562 | return mImpl->on_vkCreateDevice( |
| 1563 | context, input_result, physicalDevice, pCreateInfo, pAllocator, pDevice); |
| 1564 | } |
| 1565 | |
| Lingfeng Yang | 35e9c6a | 2018-12-25 17:13:36 -0800 | [diff] [blame] | 1566 | void ResourceTracker::on_vkDestroyDevice_pre( |
| 1567 | void* context, |
| 1568 | VkDevice device, |
| 1569 | const VkAllocationCallbacks* pAllocator) { |
| 1570 | mImpl->on_vkDestroyDevice_pre(context, device, pAllocator); |
| 1571 | } |
| 1572 | |
| Lingfeng Yang | 131d5a4 | 2018-11-30 12:00:33 -0800 | [diff] [blame] | 1573 | VkResult ResourceTracker::on_vkAllocateMemory( |
| 1574 | void* context, |
| 1575 | VkResult input_result, |
| 1576 | VkDevice device, |
| 1577 | const VkMemoryAllocateInfo* pAllocateInfo, |
| 1578 | const VkAllocationCallbacks* pAllocator, |
| 1579 | VkDeviceMemory* pMemory) { |
| 1580 | return mImpl->on_vkAllocateMemory( |
| 1581 | context, input_result, device, pAllocateInfo, pAllocator, pMemory); |
| 1582 | } |
| 1583 | |
| Lingfeng Yang | e997952 | 2018-12-25 14:44:52 -0800 | [diff] [blame] | 1584 | void ResourceTracker::on_vkFreeMemory( |
| 1585 | void* context, |
| 1586 | VkDevice device, |
| 1587 | VkDeviceMemory memory, |
| 1588 | const VkAllocationCallbacks* pAllocator) { |
| 1589 | return mImpl->on_vkFreeMemory( |
| 1590 | context, device, memory, pAllocator); |
| 1591 | } |
| 1592 | |
| Lingfeng Yang | def88ba | 2018-12-13 12:43:17 -0800 | [diff] [blame] | 1593 | VkResult ResourceTracker::on_vkMapMemory( |
| 1594 | void* context, |
| 1595 | VkResult input_result, |
| 1596 | VkDevice device, |
| 1597 | VkDeviceMemory memory, |
| 1598 | VkDeviceSize offset, |
| 1599 | VkDeviceSize size, |
| 1600 | VkMemoryMapFlags flags, |
| 1601 | void** ppData) { |
| 1602 | return mImpl->on_vkMapMemory( |
| 1603 | context, input_result, device, memory, offset, size, flags, ppData); |
| 1604 | } |
| 1605 | |
| 1606 | void ResourceTracker::on_vkUnmapMemory( |
| 1607 | void* context, |
| 1608 | VkDevice device, |
| 1609 | VkDeviceMemory memory) { |
| 1610 | mImpl->on_vkUnmapMemory(context, device, memory); |
| 1611 | } |
| 1612 | |
| Lingfeng Yang | 4af5f32 | 2019-02-14 08:10:28 -0800 | [diff] [blame] | 1613 | VkResult ResourceTracker::on_vkCreateImage( |
| 1614 | void* context, VkResult input_result, |
| 1615 | VkDevice device, const VkImageCreateInfo *pCreateInfo, |
| 1616 | const VkAllocationCallbacks *pAllocator, |
| 1617 | VkImage *pImage) { |
| 1618 | return mImpl->on_vkCreateImage( |
| 1619 | context, input_result, |
| 1620 | device, pCreateInfo, pAllocator, pImage); |
| 1621 | } |
| 1622 | |
| 1623 | void ResourceTracker::on_vkDestroyImage( |
| 1624 | void* context, |
| 1625 | VkDevice device, VkImage image, const VkAllocationCallbacks *pAllocator) { |
| 1626 | mImpl->on_vkDestroyImage(context, |
| 1627 | device, image, pAllocator); |
| 1628 | } |
| 1629 | |
| 1630 | void ResourceTracker::on_vkGetImageMemoryRequirements( |
| 1631 | void *context, VkDevice device, VkImage image, |
| 1632 | VkMemoryRequirements *pMemoryRequirements) { |
| 1633 | mImpl->on_vkGetImageMemoryRequirements( |
| 1634 | context, device, image, pMemoryRequirements); |
| 1635 | } |
| 1636 | |
| 1637 | void ResourceTracker::on_vkGetImageMemoryRequirements2( |
| 1638 | void *context, VkDevice device, const VkImageMemoryRequirementsInfo2 *pInfo, |
| 1639 | VkMemoryRequirements2 *pMemoryRequirements) { |
| 1640 | mImpl->on_vkGetImageMemoryRequirements2( |
| 1641 | context, device, pInfo, pMemoryRequirements); |
| 1642 | } |
| 1643 | |
| 1644 | void ResourceTracker::on_vkGetImageMemoryRequirements2KHR( |
| 1645 | void *context, VkDevice device, const VkImageMemoryRequirementsInfo2 *pInfo, |
| 1646 | VkMemoryRequirements2 *pMemoryRequirements) { |
| 1647 | mImpl->on_vkGetImageMemoryRequirements2KHR( |
| 1648 | context, device, pInfo, pMemoryRequirements); |
| 1649 | } |
| 1650 | |
| 1651 | VkResult ResourceTracker::on_vkBindImageMemory( |
| 1652 | void* context, VkResult input_result, |
| 1653 | VkDevice device, VkImage image, VkDeviceMemory memory, |
| 1654 | VkDeviceSize memoryOffset) { |
| 1655 | return mImpl->on_vkBindImageMemory( |
| 1656 | context, input_result, device, image, memory, memoryOffset); |
| 1657 | } |
| 1658 | |
| 1659 | VkResult ResourceTracker::on_vkBindImageMemory2( |
| 1660 | void* context, VkResult input_result, |
| 1661 | VkDevice device, uint32_t bindingCount, const VkBindImageMemoryInfo* pBindInfos) { |
| 1662 | return mImpl->on_vkBindImageMemory2( |
| 1663 | context, input_result, device, bindingCount, pBindInfos); |
| 1664 | } |
| 1665 | |
| 1666 | VkResult ResourceTracker::on_vkBindImageMemory2KHR( |
| 1667 | void* context, VkResult input_result, |
| 1668 | VkDevice device, uint32_t bindingCount, const VkBindImageMemoryInfo* pBindInfos) { |
| 1669 | return mImpl->on_vkBindImageMemory2KHR( |
| 1670 | context, input_result, device, bindingCount, pBindInfos); |
| 1671 | } |
| 1672 | |
| 1673 | VkResult ResourceTracker::on_vkCreateBuffer( |
| 1674 | void* context, VkResult input_result, |
| 1675 | VkDevice device, const VkBufferCreateInfo *pCreateInfo, |
| 1676 | const VkAllocationCallbacks *pAllocator, |
| 1677 | VkBuffer *pBuffer) { |
| 1678 | return mImpl->on_vkCreateBuffer( |
| 1679 | context, input_result, |
| 1680 | device, pCreateInfo, pAllocator, pBuffer); |
| 1681 | } |
| 1682 | |
| 1683 | void ResourceTracker::on_vkDestroyBuffer( |
| 1684 | void* context, |
| 1685 | VkDevice device, VkBuffer buffer, const VkAllocationCallbacks *pAllocator) { |
| 1686 | mImpl->on_vkDestroyBuffer(context, device, buffer, pAllocator); |
| 1687 | } |
| 1688 | |
| 1689 | void ResourceTracker::on_vkGetBufferMemoryRequirements( |
| 1690 | void* context, VkDevice device, VkBuffer buffer, VkMemoryRequirements *pMemoryRequirements) { |
| 1691 | mImpl->on_vkGetBufferMemoryRequirements(context, device, buffer, pMemoryRequirements); |
| 1692 | } |
| 1693 | |
| 1694 | void ResourceTracker::on_vkGetBufferMemoryRequirements2( |
| 1695 | void* context, VkDevice device, const VkBufferMemoryRequirementsInfo2* pInfo, |
| 1696 | VkMemoryRequirements2* pMemoryRequirements) { |
| 1697 | mImpl->on_vkGetBufferMemoryRequirements2( |
| 1698 | context, device, pInfo, pMemoryRequirements); |
| 1699 | } |
| 1700 | |
| 1701 | void ResourceTracker::on_vkGetBufferMemoryRequirements2KHR( |
| 1702 | void* context, VkDevice device, const VkBufferMemoryRequirementsInfo2* pInfo, |
| 1703 | VkMemoryRequirements2* pMemoryRequirements) { |
| 1704 | mImpl->on_vkGetBufferMemoryRequirements2KHR( |
| 1705 | context, device, pInfo, pMemoryRequirements); |
| 1706 | } |
| 1707 | |
| 1708 | VkResult ResourceTracker::on_vkBindBufferMemory( |
| 1709 | void* context, VkResult input_result, |
| 1710 | VkDevice device, VkBuffer buffer, VkDeviceMemory memory, VkDeviceSize memoryOffset) { |
| 1711 | return mImpl->on_vkBindBufferMemory( |
| 1712 | context, input_result, |
| 1713 | device, buffer, memory, memoryOffset); |
| 1714 | } |
| 1715 | |
| 1716 | VkResult ResourceTracker::on_vkBindBufferMemory2( |
| 1717 | void* context, VkResult input_result, |
| 1718 | VkDevice device, uint32_t bindInfoCount, const VkBindBufferMemoryInfo *pBindInfos) { |
| 1719 | return mImpl->on_vkBindBufferMemory2( |
| 1720 | context, input_result, |
| 1721 | device, bindInfoCount, pBindInfos); |
| 1722 | } |
| 1723 | |
| 1724 | VkResult ResourceTracker::on_vkBindBufferMemory2KHR( |
| 1725 | void* context, VkResult input_result, |
| 1726 | VkDevice device, uint32_t bindInfoCount, const VkBindBufferMemoryInfo *pBindInfos) { |
| 1727 | return mImpl->on_vkBindBufferMemory2KHR( |
| 1728 | context, input_result, |
| 1729 | device, bindInfoCount, pBindInfos); |
| 1730 | } |
| 1731 | |
| David Reveman | 32b110e | 2019-02-21 13:20:54 -0500 | [diff] [blame^] | 1732 | VkResult ResourceTracker::on_vkCreateSemaphore( |
| 1733 | void* context, VkResult input_result, |
| 1734 | VkDevice device, const VkSemaphoreCreateInfo *pCreateInfo, |
| 1735 | const VkAllocationCallbacks *pAllocator, |
| 1736 | VkSemaphore *pSemaphore) { |
| 1737 | return mImpl->on_vkCreateSemaphore( |
| 1738 | context, input_result, |
| 1739 | device, pCreateInfo, pAllocator, pSemaphore); |
| 1740 | } |
| 1741 | |
| 1742 | void ResourceTracker::on_vkDestroySemaphore( |
| 1743 | void* context, |
| 1744 | VkDevice device, VkSemaphore semaphore, const VkAllocationCallbacks *pAllocator) { |
| 1745 | mImpl->on_vkDestroySemaphore(context, device, semaphore, pAllocator); |
| 1746 | } |
| 1747 | |
| Lingfeng Yang | def88ba | 2018-12-13 12:43:17 -0800 | [diff] [blame] | 1748 | void ResourceTracker::unwrap_VkNativeBufferANDROID( |
| 1749 | const VkImageCreateInfo* pCreateInfo, |
| 1750 | VkImageCreateInfo* local_pCreateInfo) { |
| 1751 | mImpl->unwrap_VkNativeBufferANDROID(pCreateInfo, local_pCreateInfo); |
| 1752 | } |
| 1753 | |
| 1754 | void ResourceTracker::unwrap_vkAcquireImageANDROID_nativeFenceFd(int fd, int* fd_out) { |
| 1755 | mImpl->unwrap_vkAcquireImageANDROID_nativeFenceFd(fd, fd_out); |
| 1756 | } |
| 1757 | |
| David Reveman | 32b110e | 2019-02-21 13:20:54 -0500 | [diff] [blame^] | 1758 | void ResourceTracker::unwrap_vkQueueSubmit( |
| 1759 | uint32_t submitCount, const VkSubmitInfo* pSubmits, VkSubmitInfo* local_pSubmits) { |
| 1760 | mImpl->unwrap_vkQueueSubmit(submitCount, pSubmits, local_pSubmits); |
| 1761 | } |
| 1762 | |
| Lingfeng Yang | 236abc9 | 2018-12-21 20:19:33 -0800 | [diff] [blame] | 1763 | VkResult ResourceTracker::on_vkMapMemoryIntoAddressSpaceGOOGLE_pre( |
| 1764 | void* context, |
| 1765 | VkResult input_result, |
| 1766 | VkDevice device, |
| 1767 | VkDeviceMemory memory, |
| 1768 | uint64_t* pAddress) { |
| 1769 | return mImpl->on_vkMapMemoryIntoAddressSpaceGOOGLE_pre( |
| 1770 | context, input_result, device, memory, pAddress); |
| 1771 | } |
| 1772 | |
| 1773 | VkResult ResourceTracker::on_vkMapMemoryIntoAddressSpaceGOOGLE( |
| 1774 | void* context, |
| 1775 | VkResult input_result, |
| 1776 | VkDevice device, |
| 1777 | VkDeviceMemory memory, |
| 1778 | uint64_t* pAddress) { |
| 1779 | return mImpl->on_vkMapMemoryIntoAddressSpaceGOOGLE( |
| 1780 | context, input_result, device, memory, pAddress); |
| 1781 | } |
| 1782 | |
| Lingfeng Yang | 2b1b8cf | 2019-02-08 09:53:36 -0800 | [diff] [blame] | 1783 | void ResourceTracker::deviceMemoryTransform_tohost( |
| 1784 | VkDeviceMemory* memory, uint32_t memoryCount, |
| 1785 | VkDeviceSize* offset, uint32_t offsetCount, |
| 1786 | VkDeviceSize* size, uint32_t sizeCount, |
| 1787 | uint32_t* typeIndex, uint32_t typeIndexCount, |
| 1788 | uint32_t* typeBits, uint32_t typeBitsCount) { |
| 1789 | mImpl->deviceMemoryTransform_tohost( |
| 1790 | memory, memoryCount, |
| 1791 | offset, offsetCount, |
| 1792 | size, sizeCount, |
| 1793 | typeIndex, typeIndexCount, |
| 1794 | typeBits, typeBitsCount); |
| 1795 | } |
| 1796 | |
| 1797 | void ResourceTracker::deviceMemoryTransform_fromhost( |
| 1798 | VkDeviceMemory* memory, uint32_t memoryCount, |
| 1799 | VkDeviceSize* offset, uint32_t offsetCount, |
| 1800 | VkDeviceSize* size, uint32_t sizeCount, |
| 1801 | uint32_t* typeIndex, uint32_t typeIndexCount, |
| 1802 | uint32_t* typeBits, uint32_t typeBitsCount) { |
| 1803 | mImpl->deviceMemoryTransform_fromhost( |
| 1804 | memory, memoryCount, |
| 1805 | offset, offsetCount, |
| 1806 | size, sizeCount, |
| 1807 | typeIndex, typeIndexCount, |
| 1808 | typeBits, typeBitsCount); |
| 1809 | } |
| 1810 | |
| 1811 | #define DEFINE_TRANSFORMED_TYPE_IMPL(type) \ |
| 1812 | void ResourceTracker::transformImpl_##type##_tohost(const type*, uint32_t) { } \ |
| 1813 | void ResourceTracker::transformImpl_##type##_fromhost(const type*, uint32_t) { } \ |
| 1814 | |
| 1815 | LIST_TRANSFORMED_TYPES(DEFINE_TRANSFORMED_TYPE_IMPL) |
| 1816 | |
| Lingfeng Yang | 29cf075 | 2019-02-13 14:12:25 -0800 | [diff] [blame] | 1817 | } // namespace goldfish_vk |