blob: 17bb4fca8cb0e42ae82bdb45cc2a75b222c2713c [file] [log] [blame]
Lingfeng Yang8e89eab2019-01-06 16:16:53 -08001/// Copyright (C) 2018 The Android Open Source Project
Lingfeng Yang71b596b2018-11-07 18:03:25 -08002// 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 Yang71b596b2018-11-07 18:03:25 -080016#include "ResourceTracker.h"
17
Lingfeng Yang31754632018-12-21 18:24:55 -080018#include "../OpenglSystemCommon/EmulatorFeatureInfo.h"
Lingfeng Yang9b82e332019-02-13 17:53:57 -080019#include "AndroidHardwareBuffer.h"
Lingfeng Yang58b89c82018-12-25 11:23:21 -080020#include "HostVisibleMemoryVirtualization.h"
Lingfeng Yang71b596b2018-11-07 18:03:25 -080021#include "Resources.h"
Lingfeng Yang131d5a42018-11-30 12:00:33 -080022#include "VkEncoder.h"
Lingfeng Yang71b596b2018-11-07 18:03:25 -080023
Lingfeng Yang131d5a42018-11-30 12:00:33 -080024#include "android/base/AlignedBuf.h"
Lingfeng Yang6ab1b0d2018-11-27 23:36:03 -080025#include "android/base/synchronization/AndroidLock.h"
26
Lingfeng Yangdef88ba2018-12-13 12:43:17 -080027#include "gralloc_cb.h"
Lingfeng Yang236abc92018-12-21 20:19:33 -080028#include "goldfish_address_space.h"
Lingfeng Yangdef88ba2018-12-13 12:43:17 -080029#include "goldfish_vk_private_defs.h"
Lingfeng Yang4af5f322019-02-14 08:10:28 -080030#include "vk_util.h"
Lingfeng Yangdef88ba2018-12-13 12:43:17 -080031
Lingfeng Yang154a33c2019-01-29 19:06:23 -080032#include <string>
Lingfeng Yang6ab1b0d2018-11-27 23:36:03 -080033#include <unordered_map>
Lingfeng Yangf0654ff2019-02-02 12:21:24 -080034#include <set>
Lingfeng Yang6ab1b0d2018-11-27 23:36:03 -080035
Lingfeng Yang29cf0752019-02-13 14:12:25 -080036#include <vndk/hardware_buffer.h>
Lingfeng Yang131d5a42018-11-30 12:00:33 -080037#include <log/log.h>
Lingfeng Yangdef88ba2018-12-13 12:43:17 -080038#include <stdlib.h>
39#include <sync/sync.h>
Lingfeng Yang131d5a42018-11-30 12:00:33 -080040
41#define RESOURCE_TRACKER_DEBUG 0
42
43#if RESOURCE_TRACKER_DEBUG
Lingfeng Yang49c7de22019-01-23 16:19:50 -080044#undef D
Lingfeng Yang131d5a42018-11-30 12:00:33 -080045#define D(fmt,...) ALOGD("%s: " fmt, __func__, ##__VA_ARGS__);
46#else
47#ifndef D
48#define D(fmt,...)
49#endif
50#endif
51
52using android::aligned_buf_alloc;
53using android::aligned_buf_free;
Lingfeng Yang6ab1b0d2018-11-27 23:36:03 -080054using android::base::guest::AutoLock;
55using android::base::guest::Lock;
56
Lingfeng Yang71b596b2018-11-07 18:03:25 -080057namespace goldfish_vk {
58
Lingfeng Yang2285df12018-11-17 16:25:11 -080059#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) \
77class class_name : public VulkanHandleMapping { \
78public: \
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 Yang6ab1b0d2018-11-27 23:36:03 -080085 handles[i] = new_from_host_##type_name(handles[i]); ResourceTracker::get()->register_##type_name(handles[i]);, \
Lingfeng Yang04a57192018-12-20 14:06:45 -080086 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 Yang2285df12018-11-17 16:25:11 -080088
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 Yang04a57192018-12-20 14:06:45 -080092 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 Yang2285df12018-11-17 16:25:11 -080094
95#define DESTROY_MAPPING_IMPL_FOR_TYPE(type_name) \
96 MAKE_HANDLE_MAPPING_FOREACH(type_name, \
Lingfeng Yang6ab1b0d2018-11-27 23:36:03 -080097 ResourceTracker::get()->unregister_##type_name(handles[i]); delete_goldfish_##type_name(handles[i]), \
Lingfeng Yang2285df12018-11-17 16:25:11 -080098 (void)handle_u64s[i]; delete_goldfish_##type_name(handles[i]), \
Lingfeng Yang04a57192018-12-20 14:06:45 -080099 (void)handles[i]; delete_goldfish_##type_name((type_name)handle_u64s[i]))
Lingfeng Yang2285df12018-11-17 16:25:11 -0800100
101DEFINE_RESOURCE_TRACKING_CLASS(CreateMapping, CREATE_MAPPING_IMPL_FOR_TYPE)
102DEFINE_RESOURCE_TRACKING_CLASS(UnwrapMapping, UNWRAP_MAPPING_IMPL_FOR_TYPE)
103DEFINE_RESOURCE_TRACKING_CLASS(DestroyMapping, DESTROY_MAPPING_IMPL_FOR_TYPE)
Lingfeng Yang71b596b2018-11-07 18:03:25 -0800104
105class ResourceTracker::Impl {
106public:
107 Impl() = default;
108 CreateMapping createMapping;
109 UnwrapMapping unwrapMapping;
110 DestroyMapping destroyMapping;
Lingfeng Yang2285df12018-11-17 16:25:11 -0800111 DefaultHandleMapping defaultMapping;
Lingfeng Yang6ab1b0d2018-11-27 23:36:03 -0800112
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 Yangf0654ff2019-02-02 12:21:24 -0800120 struct VkInstance_Info {
121 uint32_t highestApiVersion;
122 std::set<std::string> enabledExtensions;
Lingfeng Yangb64ca452019-02-14 22:04:28 -0800123 // Fodder for vkEnumeratePhysicalDevices.
124 std::vector<VkPhysicalDevice> physicalDevices;
Lingfeng Yangf0654ff2019-02-02 12:21:24 -0800125 };
126
Lingfeng Yang6ab1b0d2018-11-27 23:36:03 -0800127 struct VkDevice_Info {
128 VkPhysicalDevice physdev;
129 VkPhysicalDeviceProperties props;
130 VkPhysicalDeviceMemoryProperties memProps;
Lingfeng Yang35e9c6a2018-12-25 17:13:36 -0800131 HostMemAlloc hostMemAllocs[VK_MAX_MEMORY_TYPES] = {};
Lingfeng Yangf0654ff2019-02-02 12:21:24 -0800132 uint32_t apiVersion;
133 std::set<std::string> enabledExtensions;
Lingfeng Yang6ab1b0d2018-11-27 23:36:03 -0800134 };
135
136 struct VkDeviceMemory_Info {
Lingfeng Yangdef88ba2018-12-13 12:43:17 -0800137 VkDeviceSize allocationSize = 0;
138 VkDeviceSize mappedSize = 0;
139 uint8_t* mappedPtr = nullptr;
140 uint32_t memoryTypeIndex = 0;
Lingfeng Yang35e9c6a2018-12-25 17:13:36 -0800141 bool virtualHostVisibleBacking = false;
Lingfeng Yang236abc92018-12-21 20:19:33 -0800142 bool directMapped = false;
Lingfeng Yang35e9c6a2018-12-25 17:13:36 -0800143 GoldfishAddressSpaceBlock*
144 goldfishAddressSpaceBlock = nullptr;
145 SubAlloc subAlloc;
Lingfeng Yang9b82e332019-02-13 17:53:57 -0800146 AHardwareBuffer** ahbHandle = nullptr;
Lingfeng Yang6ab1b0d2018-11-27 23:36:03 -0800147 };
148
Lingfeng Yang4af5f322019-02-14 08:10:28 -0800149 // 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 Yang6ab1b0d2018-11-27 23:36:03 -0800166#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 Yangdef88ba2018-12-13 12:43:17 -0800172
173#define HANDLE_UNREGISTER_IMPL_IMPL(type) \
Lingfeng Yang6ab1b0d2018-11-27 23:36:03 -0800174 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 Yangdef88ba2018-12-13 12:43:17 -0800180 GOLDFISH_VK_LIST_TRIVIAL_HANDLE_TYPES(HANDLE_UNREGISTER_IMPL_IMPL)
181
Lingfeng Yangf0654ff2019-02-02 12:21:24 -0800182 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 Yangdef88ba2018-12-13 12:43:17 -0800192 void unregister_VkDevice(VkDevice device) {
193 AutoLock lock(mLock);
Lingfeng Yang35e9c6a2018-12-25 17:13:36 -0800194
195 auto it = info_VkDevice.find(device);
196 if (it == info_VkDevice.end()) return;
197 auto info = it->second;
Lingfeng Yangdef88ba2018-12-13 12:43:17 -0800198 info_VkDevice.erase(device);
Lingfeng Yang35e9c6a2018-12-25 17:13:36 -0800199 lock.unlock();
Lingfeng Yangdef88ba2018-12-13 12:43:17 -0800200 }
201
202 void unregister_VkDeviceMemory(VkDeviceMemory mem) {
203 AutoLock lock(mLock);
Lingfeng Yang236abc92018-12-21 20:19:33 -0800204
Lingfeng Yangdef88ba2018-12-13 12:43:17 -0800205 auto it = info_VkDeviceMemory.find(mem);
206 if (it == info_VkDeviceMemory.end()) return;
207
208 auto& memInfo = it->second;
Lingfeng Yang236abc92018-12-21 20:19:33 -0800209
Lingfeng Yang35e9c6a2018-12-25 17:13:36 -0800210 if (memInfo.mappedPtr &&
211 !memInfo.virtualHostVisibleBacking &&
212 !memInfo.directMapped) {
Lingfeng Yangdef88ba2018-12-13 12:43:17 -0800213 aligned_buf_free(memInfo.mappedPtr);
214 }
Lingfeng Yang236abc92018-12-21 20:19:33 -0800215
Lingfeng Yang35e9c6a2018-12-25 17:13:36 -0800216 if (memInfo.directMapped) {
217 subFreeHostMemory(&memInfo.subAlloc);
218 }
219
220 delete memInfo.goldfishAddressSpaceBlock;
221
Lingfeng Yang236abc92018-12-21 20:19:33 -0800222 info_VkDeviceMemory.erase(mem);
Lingfeng Yangdef88ba2018-12-13 12:43:17 -0800223 }
Lingfeng Yang6ab1b0d2018-11-27 23:36:03 -0800224
Lingfeng Yang4af5f322019-02-14 08:10:28 -0800225 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 Yangf0654ff2019-02-02 12:21:24 -0800243 // 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 Yang6ab1b0d2018-11-27 23:36:03 -0800261 void setDeviceInfo(VkDevice device,
262 VkPhysicalDevice physdev,
263 VkPhysicalDeviceProperties props,
Lingfeng Yangf0654ff2019-02-02 12:21:24 -0800264 VkPhysicalDeviceMemoryProperties memProps,
265 uint32_t enabledExtensionCount,
266 const char* const* ppEnabledExtensionNames) {
Lingfeng Yang6ab1b0d2018-11-27 23:36:03 -0800267 AutoLock lock(mLock);
268 auto& info = info_VkDevice[device];
269 info.physdev = physdev;
270 info.props = props;
271 info.memProps = memProps;
Lingfeng Yang58b89c82018-12-25 11:23:21 -0800272 initHostVisibleMemoryVirtualizationInfo(
Lingfeng Yangafe29d32018-12-25 13:01:52 -0800273 physdev, &memProps,
274 mFeatureInfo->hasDirectMem,
275 &mHostVisibleMemoryVirtInfo);
Lingfeng Yangf0654ff2019-02-02 12:21:24 -0800276 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 Yang6ab1b0d2018-11-27 23:36:03 -0800283 }
284
Lingfeng Yang58b89c82018-12-25 11:23:21 -0800285 void setDeviceMemoryInfo(VkDevice device,
286 VkDeviceMemory memory,
Lingfeng Yangdef88ba2018-12-13 12:43:17 -0800287 VkDeviceSize allocationSize,
288 VkDeviceSize mappedSize,
289 uint8_t* ptr,
290 uint32_t memoryTypeIndex) {
291 AutoLock lock(mLock);
Lingfeng Yang58b89c82018-12-25 11:23:21 -0800292 auto& deviceInfo = info_VkDevice[device];
Lingfeng Yangdef88ba2018-12-13 12:43:17 -0800293 auto& info = info_VkDeviceMemory[memory];
Lingfeng Yang58b89c82018-12-25 11:23:21 -0800294
Lingfeng Yangdef88ba2018-12-13 12:43:17 -0800295 info.allocationSize = allocationSize;
296 info.mappedSize = mappedSize;
297 info.mappedPtr = ptr;
298 info.memoryTypeIndex = memoryTypeIndex;
299 }
300
Lingfeng Yang6ab1b0d2018-11-27 23:36:03 -0800301 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 Yangdef88ba2018-12-13 12:43:17 -0800308 return info.memProps.memoryTypes[typeIndex].propertyFlags &
Lingfeng Yang6ab1b0d2018-11-27 23:36:03 -0800309 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
310 }
311
Lingfeng Yangdef88ba2018-12-13 12:43:17 -0800312 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 Yang6ab1b0d2018-11-27 23:36:03 -0800330 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 Yangdef88ba2018-12-13 12:43:17 -0800343 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 Yang31754632018-12-21 18:24:55 -0800361 void setupFeatures(const EmulatorFeatureInfo* features) {
362 if (!features || mFeatureInfo) return;
363 mFeatureInfo.reset(new EmulatorFeatureInfo);
364 *mFeatureInfo = *features;
Lingfeng Yang236abc92018-12-21 20:19:33 -0800365
366 if (mFeatureInfo->hasDirectMem) {
367 mGoldfishAddressSpaceBlockProvider.reset(
368 new GoldfishAddressSpaceBlockProvider);
369 }
370 }
371
Lingfeng Yangb8a38c72019-02-02 20:27:54 -0800372 bool hostSupportsVulkan() const {
373 if (!mFeatureInfo) return false;
374
375 return mFeatureInfo->hasVulkan;
376 }
377
Lingfeng Yang236abc92018-12-21 20:19:33 -0800378 bool usingDirectMapping() const {
Lingfeng Yangafe29d32018-12-25 13:01:52 -0800379 return mHostVisibleMemoryVirtInfo.virtualizationSupported;
Lingfeng Yang31754632018-12-21 18:24:55 -0800380 }
381
Lingfeng Yang154a33c2019-01-29 19:06:23 -0800382 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 Yang97a06702018-12-24 17:02:43 -0800404 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 Yangafe29d32018-12-25 13:01:52 -0800410
411 (void)memoryCount;
412 (void)offsetCount;
413 (void)sizeCount;
Lingfeng Yang35e9c6a2018-12-25 17:13:36 -0800414
Lingfeng Yangdf173132018-12-25 13:30:57 -0800415 const auto& hostVirt =
416 mHostVisibleMemoryVirtInfo;
417
418 if (!hostVirt.virtualizationSupported) return;
Lingfeng Yang97a06702018-12-24 17:02:43 -0800419
Lingfeng Yang35e9c6a2018-12-25 17:13:36 -0800420 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 Yang97a06702018-12-24 17:02:43 -0800450 }
451
452 for (uint32_t i = 0; i < typeIndexCount; ++i) {
Lingfeng Yangdf173132018-12-25 13:30:57 -0800453 typeIndex[i] =
454 hostVirt.memoryTypeIndexMappingToHost[typeIndex[i]];
Lingfeng Yang97a06702018-12-24 17:02:43 -0800455 }
456
457 for (uint32_t i = 0; i < typeBitsCount; ++i) {
Lingfeng Yangdf173132018-12-25 13:30:57 -0800458 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 Yang97a06702018-12-24 17:02:43 -0800466 }
467 }
468
469 void deviceMemoryTransform_fromhost(
Lingfeng Yang62b23322018-12-24 12:45:47 -0800470 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 Yang62b23322018-12-24 12:45:47 -0800475
Lingfeng Yangafe29d32018-12-25 13:01:52 -0800476 (void)memoryCount;
477 (void)offsetCount;
478 (void)sizeCount;
Lingfeng Yang35e9c6a2018-12-25 17:13:36 -0800479
Lingfeng Yangdf173132018-12-25 13:30:57 -0800480 const auto& hostVirt =
481 mHostVisibleMemoryVirtInfo;
482
483 if (!hostVirt.virtualizationSupported) return;
484
Lingfeng Yang35e9c6a2018-12-25 17:13:36 -0800485 AutoLock lock (mLock);
486
Lingfeng Yang62b23322018-12-24 12:45:47 -0800487 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 Yangdf173132018-12-25 13:30:57 -0800495 typeIndex[i] =
496 hostVirt.memoryTypeIndexMappingFromHost[typeIndex[i]];
Lingfeng Yang62b23322018-12-24 12:45:47 -0800497 }
498
499 for (uint32_t i = 0; i < typeBitsCount; ++i) {
Lingfeng Yangdf173132018-12-25 13:30:57 -0800500 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 Yang62b23322018-12-24 12:45:47 -0800512 }
513 }
514
Lingfeng Yangdef88ba2018-12-13 12:43:17 -0800515 VkResult on_vkEnumerateInstanceVersion(
516 void*,
517 VkResult,
518 uint32_t* apiVersion) {
519 if (apiVersion) {
Lingfeng Yangf0654ff2019-02-02 12:21:24 -0800520 *apiVersion = kMaxApiVersion;
Lingfeng Yangdef88ba2018-12-13 12:43:17 -0800521 }
522 return VK_SUCCESS;
523 }
524
Lingfeng Yang154a33c2019-01-29 19:06:23 -0800525 VkResult on_vkEnumerateInstanceExtensionProperties(
526 void* context,
Lingfeng Yangdef88ba2018-12-13 12:43:17 -0800527 VkResult,
Lingfeng Yangdef88ba2018-12-13 12:43:17 -0800528 const char*,
529 uint32_t* pPropertyCount,
Lingfeng Yang7dea7b32018-12-18 09:49:02 -0800530 VkExtensionProperties* pProperties) {
Lingfeng Yang7dea7b32018-12-18 09:49:02 -0800531
Lingfeng Yang154a33c2019-01-29 19:06:23 -0800532 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 Yang7dea7b32018-12-18 09:49:02 -0800579
Lingfeng Yangdef88ba2018-12-13 12:43:17 -0800580 return VK_SUCCESS;
581 }
582
Lingfeng Yang154a33c2019-01-29 19:06:23 -0800583 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 Yang422d22d2019-02-02 19:27:15 -0800592 "VK_KHR_maintenance1",
593 "VK_KHR_get_memory_requirements2",
Lingfeng Yangd27fc832019-02-19 10:49:05 -0800594 "VK_KHR_dedicated_allocation",
Lingfeng Yang154a33c2019-01-29 19:06:23 -0800595 // "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 Yangdef88ba2018-12-13 12:43:17 -0800643 }
644
Lingfeng Yangb64ca452019-02-14 22:04:28 -0800645 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 Yang97a06702018-12-24 17:02:43 -0800704 void on_vkGetPhysicalDeviceMemoryProperties(
705 void*,
Lingfeng Yangdf173132018-12-25 13:30:57 -0800706 VkPhysicalDevice physdev,
707 VkPhysicalDeviceMemoryProperties* out) {
Lingfeng Yang35e9c6a2018-12-25 17:13:36 -0800708
Lingfeng Yangdf173132018-12-25 13:30:57 -0800709 initHostVisibleMemoryVirtualizationInfo(
710 physdev,
711 out,
712 mFeatureInfo->hasDirectMem,
713 &mHostVisibleMemoryVirtInfo);
Lingfeng Yang35e9c6a2018-12-25 17:13:36 -0800714
Lingfeng Yangdf173132018-12-25 13:30:57 -0800715 if (mHostVisibleMemoryVirtInfo.virtualizationSupported) {
716 *out = mHostVisibleMemoryVirtInfo.guestMemoryProperties;
717 }
718 }
Lingfeng Yang97a06702018-12-24 17:02:43 -0800719
Lingfeng Yang154a33c2019-01-29 19:06:23 -0800720 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 Yangf0654ff2019-02-02 12:21:24 -0800736 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 Yang154a33c2019-01-29 19:06:23 -0800752
Lingfeng Yang131d5a42018-11-30 12:00:33 -0800753 VkResult on_vkCreateDevice(
754 void* context,
755 VkResult input_result,
756 VkPhysicalDevice physicalDevice,
Lingfeng Yangf0654ff2019-02-02 12:21:24 -0800757 const VkDeviceCreateInfo* pCreateInfo,
Lingfeng Yang131d5a42018-11-30 12:00:33 -0800758 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 Yangf0654ff2019-02-02 12:21:24 -0800770 setDeviceInfo(
771 *pDevice, physicalDevice, props, memProps,
772 pCreateInfo->enabledExtensionCount, pCreateInfo->ppEnabledExtensionNames);
Lingfeng Yang131d5a42018-11-30 12:00:33 -0800773
774 return input_result;
775 }
776
Lingfeng Yang35e9c6a2018-12-25 17:13:36 -0800777 void on_vkDestroyDevice_pre(
Lingfeng Yang8e89eab2019-01-06 16:16:53 -0800778 void* context,
Lingfeng Yang35e9c6a2018-12-25 17:13:36 -0800779 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 Yang8e89eab2019-01-06 16:16:53 -0800790 VkEncoder* enc = (VkEncoder*)context;
791
Lingfeng Yang35e9c6a2018-12-25 17:13:36 -0800792 for (uint32_t i = 0; i < VK_MAX_MEMORY_TYPES; ++i) {
Lingfeng Yang8e89eab2019-01-06 16:16:53 -0800793 destroyHostMemAlloc(enc, device, &info.hostMemAllocs[i]);
Lingfeng Yang35e9c6a2018-12-25 17:13:36 -0800794 }
795 }
796
Lingfeng Yang9b82e332019-02-13 17:53:57 -0800797 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 Yang131d5a42018-11-30 12:00:33 -0800840 VkResult on_vkAllocateMemory(
Lingfeng Yang236abc92018-12-21 20:19:33 -0800841 void* context,
Lingfeng Yang131d5a42018-11-30 12:00:33 -0800842 VkResult input_result,
843 VkDevice device,
844 const VkMemoryAllocateInfo* pAllocateInfo,
Lingfeng Yange9979522018-12-25 14:44:52 -0800845 const VkAllocationCallbacks* pAllocator,
Lingfeng Yang131d5a42018-11-30 12:00:33 -0800846 VkDeviceMemory* pMemory) {
847
848 if (input_result != VK_SUCCESS) return input_result;
849
Lingfeng Yange9979522018-12-25 14:44:52 -0800850 VkEncoder* enc = (VkEncoder*)context;
851
Lingfeng Yang35e9c6a2018-12-25 17:13:36 -0800852 // Device local memory: pass through
853 if (!isHostVisibleMemoryTypeIndexForGuest(
854 &mHostVisibleMemoryVirtInfo,
855 pAllocateInfo->memoryTypeIndex)) {
Lingfeng Yange9979522018-12-25 14:44:52 -0800856
Lingfeng Yang35e9c6a2018-12-25 17:13:36 -0800857 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 Yangafe29d32018-12-25 13:01:52 -0800874 bool directMappingSupported = usingDirectMapping();
Lingfeng Yang35e9c6a2018-12-25 17:13:36 -0800875 if (!directMappingSupported) {
Lingfeng Yang630abfb2018-12-26 12:31:47 -0800876 input_result =
877 enc->vkAllocateMemory(
878 device, pAllocateInfo, pAllocator, pMemory);
879
880 if (input_result != VK_SUCCESS) return input_result;
881
Lingfeng Yang35e9c6a2018-12-25 17:13:36 -0800882 VkDeviceSize mappedSize =
883 getNonCoherentExtendedSize(device,
884 pAllocateInfo->allocationSize);
885 uint8_t* mappedPtr = (uint8_t*)aligned_buf_alloc(4096, mappedSize);
Lingfeng Yang236abc92018-12-21 20:19:33 -0800886 D("host visible alloc (non-direct): "
887 "size 0x%llx host ptr %p mapped size 0x%llx",
Lingfeng Yang49c7de22019-01-23 16:19:50 -0800888 (unsigned long long)pAllocateInfo->allocationSize, mappedPtr,
Lingfeng Yang236abc92018-12-21 20:19:33 -0800889 (unsigned long long)mappedSize);
Lingfeng Yang35e9c6a2018-12-25 17:13:36 -0800890 setDeviceMemoryInfo(
891 device, *pMemory,
892 pAllocateInfo->allocationSize,
893 mappedSize, mappedPtr,
894 pAllocateInfo->memoryTypeIndex);
895 return VK_SUCCESS;
Lingfeng Yang131d5a42018-11-30 12:00:33 -0800896 }
897
Lingfeng Yang35e9c6a2018-12-25 17:13:36 -0800898 // Host visible memory with direct mapping
899 AutoLock lock(mLock);
Lingfeng Yang131d5a42018-11-30 12:00:33 -0800900
Lingfeng Yang35e9c6a2018-12-25 17:13:36 -0800901 auto it = info_VkDevice.find(device);
902 if (it == info_VkDevice.end()) return VK_ERROR_DEVICE_LOST;
903 auto& deviceInfo = it->second;
Lingfeng Yang236abc92018-12-21 20:19:33 -0800904
Lingfeng Yang35e9c6a2018-12-25 17:13:36 -0800905 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 Yang49c7de22019-01-23 16:19:50 -0800938 hostMemAlloc->nonCoherentAtomSize = nonCoherentAtomSize;
Lingfeng Yang236abc92018-12-21 20:19:33 -0800939
940 uint64_t directMappedAddr = 0;
Lingfeng Yang35e9c6a2018-12-25 17:13:36 -0800941 lock.unlock();
Lingfeng Yang236abc92018-12-21 20:19:33 -0800942 VkResult directMapResult =
943 enc->vkMapMemoryIntoAddressSpaceGOOGLE(
Lingfeng Yang35e9c6a2018-12-25 17:13:36 -0800944 device, hostMemAlloc->memory, &directMappedAddr);
945 lock.lock();
Lingfeng Yang236abc92018-12-21 20:19:33 -0800946
947 if (directMapResult != VK_SUCCESS) {
Lingfeng Yang35e9c6a2018-12-25 17:13:36 -0800948 hostMemAlloc->initialized = true;
949 hostMemAlloc->initResult = directMapResult;
Lingfeng Yang236abc92018-12-21 20:19:33 -0800950 return directMapResult;
951 }
952
Lingfeng Yang35e9c6a2018-12-25 17:13:36 -0800953 hostMemInfo.mappedPtr =
954 (uint8_t*)(uintptr_t)directMappedAddr;
955 hostMemInfo.virtualHostVisibleBacking = true;
Lingfeng Yang236abc92018-12-21 20:19:33 -0800956
Lingfeng Yang35e9c6a2018-12-25 17:13:36 -0800957 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 Yang236abc92018-12-21 20:19:33 -0800970 }
Lingfeng Yang236abc92018-12-21 20:19:33 -0800971 }
972
Lingfeng Yang35e9c6a2018-12-25 17:13:36 -0800973 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 Yang49c7de22019-01-23 16:19:50 -0800989 (unsigned long long)virtualMemInfo.mappedSize);
Lingfeng Yang35e9c6a2018-12-25 17:13:36 -0800990
991 info_VkDeviceMemory[
992 virtualMemInfo.subAlloc.subMemory] = virtualMemInfo;
993
994 *pMemory = virtualMemInfo.subAlloc.subMemory;
995
996 return VK_SUCCESS;
Lingfeng Yang131d5a42018-11-30 12:00:33 -0800997 }
998
Lingfeng Yange9979522018-12-25 14:44:52 -0800999 void on_vkFreeMemory(
1000 void* context,
1001 VkDevice device,
1002 VkDeviceMemory memory,
1003 const VkAllocationCallbacks* pAllocateInfo) {
1004
Lingfeng Yang35e9c6a2018-12-25 17:13:36 -08001005 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 Yange9979522018-12-25 14:44:52 -08001019 }
1020
Lingfeng Yangdef88ba2018-12-13 12:43:17 -08001021 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 Yang4af5f322019-02-14 08:10:28 -08001059 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 Yang3e87e852019-02-19 14:12:49 -08001065
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 Yang4af5f322019-02-14 08:10:28 -08001081 }
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 Yang3e87e852019-02-19 14:12:49 -08001142
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 Yang4af5f322019-02-14 08:10:28 -08001158 }
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
Lingfeng Yangdef88ba2018-12-13 12:43:17 -08001212 void unwrap_VkNativeBufferANDROID(
1213 const VkImageCreateInfo* pCreateInfo,
1214 VkImageCreateInfo* local_pCreateInfo) {
1215
1216 if (!pCreateInfo->pNext) return;
1217
1218 const VkNativeBufferANDROID* nativeInfo =
1219 reinterpret_cast<const VkNativeBufferANDROID*>(pCreateInfo->pNext);
1220
1221 if (VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID != nativeInfo->sType) {
1222 return;
1223 }
1224
1225 const cb_handle_t* cb_handle =
1226 reinterpret_cast<const cb_handle_t*>(nativeInfo->handle);
1227
1228 if (!cb_handle) return;
1229
1230 VkNativeBufferANDROID* nativeInfoOut =
Lingfeng Yange637ca52018-12-14 18:24:56 -08001231 reinterpret_cast<VkNativeBufferANDROID*>(
1232 const_cast<void*>(
1233 local_pCreateInfo->pNext));
Lingfeng Yangdef88ba2018-12-13 12:43:17 -08001234
1235 if (!nativeInfoOut->handle) {
1236 ALOGE("FATAL: Local native buffer info not properly allocated!");
1237 abort();
1238 }
1239
1240 *(uint32_t*)(nativeInfoOut->handle) = cb_handle->hostHandle;
1241 }
1242
1243 void unwrap_vkAcquireImageANDROID_nativeFenceFd(int fd, int*) {
1244 if (fd != -1) {
1245 sync_wait(fd, 3000);
1246 }
1247 }
1248
Lingfeng Yang236abc92018-12-21 20:19:33 -08001249 // Action of vkMapMemoryIntoAddressSpaceGOOGLE:
1250 // 1. preprocess (on_vkMapMemoryIntoAddressSpaceGOOGLE_pre):
1251 // uses address space device to reserve the right size of
1252 // memory.
1253 // 2. the reservation results in a physical address. the physical
1254 // address is set as |*pAddress|.
1255 // 3. after pre, the API call is encoded to the host, where the
1256 // value of pAddress is also sent (the physical address).
1257 // 4. the host will obtain the actual gpu pointer and send it
1258 // back out in |*pAddress|.
1259 // 5. postprocess (on_vkMapMemoryIntoAddressSpaceGOOGLE) will run,
1260 // using the mmap() method of GoldfishAddressSpaceBlock to obtain
1261 // a pointer in guest userspace corresponding to the host pointer.
1262 VkResult on_vkMapMemoryIntoAddressSpaceGOOGLE_pre(
1263 void*,
1264 VkResult,
1265 VkDevice,
1266 VkDeviceMemory memory,
1267 uint64_t* pAddress) {
1268
1269 AutoLock lock(mLock);
1270
1271 auto it = info_VkDeviceMemory.find(memory);
1272 if (it == info_VkDeviceMemory.end()) {
1273 return VK_ERROR_OUT_OF_HOST_MEMORY;
1274 }
1275
1276 auto& memInfo = it->second;
Lingfeng Yang35e9c6a2018-12-25 17:13:36 -08001277 memInfo.goldfishAddressSpaceBlock =
1278 new GoldfishAddressSpaceBlock;
Lingfeng Yang236abc92018-12-21 20:19:33 -08001279 auto& block = *(memInfo.goldfishAddressSpaceBlock);
1280
1281 block.allocate(
1282 mGoldfishAddressSpaceBlockProvider.get(),
1283 memInfo.mappedSize);
1284
1285 *pAddress = block.physAddr();
1286
1287 return VK_SUCCESS;
1288 }
1289
1290 VkResult on_vkMapMemoryIntoAddressSpaceGOOGLE(
1291 void*,
1292 VkResult input_result,
1293 VkDevice,
1294 VkDeviceMemory memory,
1295 uint64_t* pAddress) {
1296
1297 if (input_result != VK_SUCCESS) {
1298 return input_result;
1299 }
1300
1301 // Now pAddress points to the gpu addr from host.
1302 AutoLock lock(mLock);
1303
1304 auto it = info_VkDeviceMemory.find(memory);
1305 if (it == info_VkDeviceMemory.end()) {
1306 return VK_ERROR_OUT_OF_HOST_MEMORY;
1307 }
1308
1309 auto& memInfo = it->second;
1310 auto& block = *(memInfo.goldfishAddressSpaceBlock);
1311
1312 uint64_t gpuAddr = *pAddress;
1313
1314 void* userPtr = block.mmap(gpuAddr);
1315
Lingfeng Yang49c7de22019-01-23 16:19:50 -08001316 D("%s: Got new host visible alloc. "
1317 "Sizeof void: %zu map size: %zu Range: [%p %p]",
1318 __func__,
1319 sizeof(void*), (size_t)memInfo.mappedSize,
1320 userPtr,
1321 (unsigned char*)userPtr + memInfo.mappedSize);
1322
Lingfeng Yang236abc92018-12-21 20:19:33 -08001323 *pAddress = (uint64_t)(uintptr_t)userPtr;
1324
1325 return input_result;
1326 }
1327
Lingfeng Yangf0654ff2019-02-02 12:21:24 -08001328 uint32_t getApiVersionFromInstance(VkInstance instance) const {
1329 AutoLock lock(mLock);
1330 uint32_t api = kMinApiVersion;
1331
1332 auto it = info_VkInstance.find(instance);
1333 if (it == info_VkInstance.end()) return api;
1334
1335 api = it->second.highestApiVersion;
1336
1337 return api;
1338 }
1339
1340 uint32_t getApiVersionFromDevice(VkDevice device) const {
1341 AutoLock lock(mLock);
1342
1343 uint32_t api = kMinApiVersion;
1344
1345 auto it = info_VkDevice.find(device);
1346 if (it == info_VkDevice.end()) return api;
1347
1348 api = it->second.apiVersion;
1349
1350 return api;
1351 }
1352
1353 bool hasInstanceExtension(VkInstance instance, const std::string& name) const {
1354 AutoLock lock(mLock);
1355
1356 auto it = info_VkInstance.find(instance);
1357 if (it == info_VkInstance.end()) return false;
1358
1359 return it->second.enabledExtensions.find(name) !=
1360 it->second.enabledExtensions.end();
1361 }
1362
1363 bool hasDeviceExtension(VkDevice device, const std::string& name) const {
1364 AutoLock lock(mLock);
1365
1366 auto it = info_VkDevice.find(device);
1367 if (it == info_VkDevice.end()) return false;
1368
1369 return it->second.enabledExtensions.find(name) !=
1370 it->second.enabledExtensions.end();
1371 }
1372
Lingfeng Yang6ab1b0d2018-11-27 23:36:03 -08001373private:
1374 mutable Lock mLock;
Lingfeng Yangafe29d32018-12-25 13:01:52 -08001375 HostVisibleMemoryVirtualizationInfo mHostVisibleMemoryVirtInfo;
Lingfeng Yang31754632018-12-21 18:24:55 -08001376 std::unique_ptr<EmulatorFeatureInfo> mFeatureInfo;
Lingfeng Yang236abc92018-12-21 20:19:33 -08001377 std::unique_ptr<GoldfishAddressSpaceBlockProvider> mGoldfishAddressSpaceBlockProvider;
Lingfeng Yang154a33c2019-01-29 19:06:23 -08001378
1379 std::vector<VkExtensionProperties> mHostInstanceExtensions;
1380 std::vector<VkExtensionProperties> mHostDeviceExtensions;
Lingfeng Yang71b596b2018-11-07 18:03:25 -08001381};
Lingfeng Yang154a33c2019-01-29 19:06:23 -08001382
Lingfeng Yang71b596b2018-11-07 18:03:25 -08001383ResourceTracker::ResourceTracker() : mImpl(new ResourceTracker::Impl()) { }
1384ResourceTracker::~ResourceTracker() { }
Lingfeng Yang71b596b2018-11-07 18:03:25 -08001385VulkanHandleMapping* ResourceTracker::createMapping() {
1386 return &mImpl->createMapping;
1387}
Lingfeng Yang71b596b2018-11-07 18:03:25 -08001388VulkanHandleMapping* ResourceTracker::unwrapMapping() {
1389 return &mImpl->unwrapMapping;
1390}
Lingfeng Yang71b596b2018-11-07 18:03:25 -08001391VulkanHandleMapping* ResourceTracker::destroyMapping() {
1392 return &mImpl->destroyMapping;
1393}
Lingfeng Yang2285df12018-11-17 16:25:11 -08001394VulkanHandleMapping* ResourceTracker::defaultMapping() {
1395 return &mImpl->defaultMapping;
1396}
Lingfeng Yang71b596b2018-11-07 18:03:25 -08001397static ResourceTracker* sTracker = nullptr;
Lingfeng Yang71b596b2018-11-07 18:03:25 -08001398// static
1399ResourceTracker* ResourceTracker::get() {
1400 if (!sTracker) {
1401 // To be initialized once on vulkan device open.
1402 sTracker = new ResourceTracker;
1403 }
1404 return sTracker;
1405}
Lingfeng Yang71b596b2018-11-07 18:03:25 -08001406
Lingfeng Yang6ab1b0d2018-11-27 23:36:03 -08001407#define HANDLE_REGISTER_IMPL(type) \
1408 void ResourceTracker::register_##type(type obj) { \
1409 mImpl->register_##type(obj); \
1410 } \
1411 void ResourceTracker::unregister_##type(type obj) { \
1412 mImpl->unregister_##type(obj); \
1413 } \
1414
1415GOLDFISH_VK_LIST_HANDLE_TYPES(HANDLE_REGISTER_IMPL)
1416
Lingfeng Yang6ab1b0d2018-11-27 23:36:03 -08001417bool ResourceTracker::isMemoryTypeHostVisible(
1418 VkDevice device, uint32_t typeIndex) const {
1419 return mImpl->isMemoryTypeHostVisible(device, typeIndex);
1420}
1421
Lingfeng Yangdef88ba2018-12-13 12:43:17 -08001422uint8_t* ResourceTracker::getMappedPointer(VkDeviceMemory memory) {
1423 return mImpl->getMappedPointer(memory);
1424}
1425
1426VkDeviceSize ResourceTracker::getMappedSize(VkDeviceMemory memory) {
1427 return mImpl->getMappedSize(memory);
1428}
1429
Lingfeng Yang6ab1b0d2018-11-27 23:36:03 -08001430VkDeviceSize ResourceTracker::getNonCoherentExtendedSize(VkDevice device, VkDeviceSize basicSize) const {
1431 return mImpl->getNonCoherentExtendedSize(device, basicSize);
1432}
1433
Lingfeng Yangdef88ba2018-12-13 12:43:17 -08001434bool ResourceTracker::isValidMemoryRange(const VkMappedMemoryRange& range) const {
1435 return mImpl->isValidMemoryRange(range);
1436}
1437
Lingfeng Yang31754632018-12-21 18:24:55 -08001438void ResourceTracker::setupFeatures(const EmulatorFeatureInfo* features) {
1439 mImpl->setupFeatures(features);
1440}
1441
Lingfeng Yangb8a38c72019-02-02 20:27:54 -08001442bool ResourceTracker::hostSupportsVulkan() const {
1443 return mImpl->hostSupportsVulkan();
1444}
1445
Lingfeng Yang236abc92018-12-21 20:19:33 -08001446bool ResourceTracker::usingDirectMapping() const {
1447 return mImpl->usingDirectMapping();
1448}
1449
Lingfeng Yangf0654ff2019-02-02 12:21:24 -08001450uint32_t ResourceTracker::getApiVersionFromInstance(VkInstance instance) const {
1451 return mImpl->getApiVersionFromInstance(instance);
1452}
1453
1454uint32_t ResourceTracker::getApiVersionFromDevice(VkDevice device) const {
1455 return mImpl->getApiVersionFromDevice(device);
1456}
1457bool ResourceTracker::hasInstanceExtension(VkInstance instance, const std::string &name) const {
1458 return mImpl->hasInstanceExtension(instance, name);
1459}
1460bool ResourceTracker::hasDeviceExtension(VkDevice device, const std::string &name) const {
1461 return mImpl->hasDeviceExtension(device, name);
1462}
1463
Lingfeng Yangdef88ba2018-12-13 12:43:17 -08001464VkResult ResourceTracker::on_vkEnumerateInstanceVersion(
1465 void* context,
1466 VkResult input_result,
1467 uint32_t* apiVersion) {
1468 return mImpl->on_vkEnumerateInstanceVersion(context, input_result, apiVersion);
1469}
1470
Lingfeng Yang154a33c2019-01-29 19:06:23 -08001471VkResult ResourceTracker::on_vkEnumerateInstanceExtensionProperties(
1472 void* context,
1473 VkResult input_result,
1474 const char* pLayerName,
1475 uint32_t* pPropertyCount,
1476 VkExtensionProperties* pProperties) {
1477 return mImpl->on_vkEnumerateInstanceExtensionProperties(
1478 context, input_result, pLayerName, pPropertyCount, pProperties);
1479}
1480
Lingfeng Yangdef88ba2018-12-13 12:43:17 -08001481VkResult ResourceTracker::on_vkEnumerateDeviceExtensionProperties(
1482 void* context,
1483 VkResult input_result,
1484 VkPhysicalDevice physicalDevice,
1485 const char* pLayerName,
1486 uint32_t* pPropertyCount,
1487 VkExtensionProperties* pProperties) {
1488 return mImpl->on_vkEnumerateDeviceExtensionProperties(
1489 context, input_result, physicalDevice, pLayerName, pPropertyCount, pProperties);
1490}
1491
Lingfeng Yangb64ca452019-02-14 22:04:28 -08001492VkResult ResourceTracker::on_vkEnumeratePhysicalDevices(
1493 void* context, VkResult input_result,
1494 VkInstance instance, uint32_t* pPhysicalDeviceCount,
1495 VkPhysicalDevice* pPhysicalDevices) {
1496 return mImpl->on_vkEnumeratePhysicalDevices(
1497 context, input_result, instance, pPhysicalDeviceCount,
1498 pPhysicalDevices);
1499}
1500
Lingfeng Yang97a06702018-12-24 17:02:43 -08001501void ResourceTracker::on_vkGetPhysicalDeviceMemoryProperties(
1502 void* context,
1503 VkPhysicalDevice physicalDevice,
1504 VkPhysicalDeviceMemoryProperties* pMemoryProperties) {
1505 mImpl->on_vkGetPhysicalDeviceMemoryProperties(
1506 context, physicalDevice, pMemoryProperties);
1507}
1508
Lingfeng Yang154a33c2019-01-29 19:06:23 -08001509void ResourceTracker::on_vkGetPhysicalDeviceMemoryProperties2(
1510 void* context,
1511 VkPhysicalDevice physicalDevice,
1512 VkPhysicalDeviceMemoryProperties2* pMemoryProperties) {
1513 mImpl->on_vkGetPhysicalDeviceMemoryProperties2(
1514 context, physicalDevice, pMemoryProperties);
1515}
1516
1517void ResourceTracker::on_vkGetPhysicalDeviceMemoryProperties2KHR(
1518 void* context,
1519 VkPhysicalDevice physicalDevice,
1520 VkPhysicalDeviceMemoryProperties2* pMemoryProperties) {
1521 mImpl->on_vkGetPhysicalDeviceMemoryProperties2(
1522 context, physicalDevice, pMemoryProperties);
1523}
1524
Lingfeng Yangf0654ff2019-02-02 12:21:24 -08001525VkResult ResourceTracker::on_vkCreateInstance(
1526 void* context,
1527 VkResult input_result,
1528 const VkInstanceCreateInfo* pCreateInfo,
1529 const VkAllocationCallbacks* pAllocator,
1530 VkInstance* pInstance) {
1531 return mImpl->on_vkCreateInstance(
1532 context, input_result, pCreateInfo, pAllocator, pInstance);
1533}
1534
Lingfeng Yang131d5a42018-11-30 12:00:33 -08001535VkResult ResourceTracker::on_vkCreateDevice(
1536 void* context,
1537 VkResult input_result,
1538 VkPhysicalDevice physicalDevice,
1539 const VkDeviceCreateInfo* pCreateInfo,
1540 const VkAllocationCallbacks* pAllocator,
1541 VkDevice* pDevice) {
1542 return mImpl->on_vkCreateDevice(
1543 context, input_result, physicalDevice, pCreateInfo, pAllocator, pDevice);
1544}
1545
Lingfeng Yang35e9c6a2018-12-25 17:13:36 -08001546void ResourceTracker::on_vkDestroyDevice_pre(
1547 void* context,
1548 VkDevice device,
1549 const VkAllocationCallbacks* pAllocator) {
1550 mImpl->on_vkDestroyDevice_pre(context, device, pAllocator);
1551}
1552
Lingfeng Yang131d5a42018-11-30 12:00:33 -08001553VkResult ResourceTracker::on_vkAllocateMemory(
1554 void* context,
1555 VkResult input_result,
1556 VkDevice device,
1557 const VkMemoryAllocateInfo* pAllocateInfo,
1558 const VkAllocationCallbacks* pAllocator,
1559 VkDeviceMemory* pMemory) {
1560 return mImpl->on_vkAllocateMemory(
1561 context, input_result, device, pAllocateInfo, pAllocator, pMemory);
1562}
1563
Lingfeng Yange9979522018-12-25 14:44:52 -08001564void ResourceTracker::on_vkFreeMemory(
1565 void* context,
1566 VkDevice device,
1567 VkDeviceMemory memory,
1568 const VkAllocationCallbacks* pAllocator) {
1569 return mImpl->on_vkFreeMemory(
1570 context, device, memory, pAllocator);
1571}
1572
Lingfeng Yangdef88ba2018-12-13 12:43:17 -08001573VkResult ResourceTracker::on_vkMapMemory(
1574 void* context,
1575 VkResult input_result,
1576 VkDevice device,
1577 VkDeviceMemory memory,
1578 VkDeviceSize offset,
1579 VkDeviceSize size,
1580 VkMemoryMapFlags flags,
1581 void** ppData) {
1582 return mImpl->on_vkMapMemory(
1583 context, input_result, device, memory, offset, size, flags, ppData);
1584}
1585
1586void ResourceTracker::on_vkUnmapMemory(
1587 void* context,
1588 VkDevice device,
1589 VkDeviceMemory memory) {
1590 mImpl->on_vkUnmapMemory(context, device, memory);
1591}
1592
Lingfeng Yang4af5f322019-02-14 08:10:28 -08001593VkResult ResourceTracker::on_vkCreateImage(
1594 void* context, VkResult input_result,
1595 VkDevice device, const VkImageCreateInfo *pCreateInfo,
1596 const VkAllocationCallbacks *pAllocator,
1597 VkImage *pImage) {
1598 return mImpl->on_vkCreateImage(
1599 context, input_result,
1600 device, pCreateInfo, pAllocator, pImage);
1601}
1602
1603void ResourceTracker::on_vkDestroyImage(
1604 void* context,
1605 VkDevice device, VkImage image, const VkAllocationCallbacks *pAllocator) {
1606 mImpl->on_vkDestroyImage(context,
1607 device, image, pAllocator);
1608}
1609
1610void ResourceTracker::on_vkGetImageMemoryRequirements(
1611 void *context, VkDevice device, VkImage image,
1612 VkMemoryRequirements *pMemoryRequirements) {
1613 mImpl->on_vkGetImageMemoryRequirements(
1614 context, device, image, pMemoryRequirements);
1615}
1616
1617void ResourceTracker::on_vkGetImageMemoryRequirements2(
1618 void *context, VkDevice device, const VkImageMemoryRequirementsInfo2 *pInfo,
1619 VkMemoryRequirements2 *pMemoryRequirements) {
1620 mImpl->on_vkGetImageMemoryRequirements2(
1621 context, device, pInfo, pMemoryRequirements);
1622}
1623
1624void ResourceTracker::on_vkGetImageMemoryRequirements2KHR(
1625 void *context, VkDevice device, const VkImageMemoryRequirementsInfo2 *pInfo,
1626 VkMemoryRequirements2 *pMemoryRequirements) {
1627 mImpl->on_vkGetImageMemoryRequirements2KHR(
1628 context, device, pInfo, pMemoryRequirements);
1629}
1630
1631VkResult ResourceTracker::on_vkBindImageMemory(
1632 void* context, VkResult input_result,
1633 VkDevice device, VkImage image, VkDeviceMemory memory,
1634 VkDeviceSize memoryOffset) {
1635 return mImpl->on_vkBindImageMemory(
1636 context, input_result, device, image, memory, memoryOffset);
1637}
1638
1639VkResult ResourceTracker::on_vkBindImageMemory2(
1640 void* context, VkResult input_result,
1641 VkDevice device, uint32_t bindingCount, const VkBindImageMemoryInfo* pBindInfos) {
1642 return mImpl->on_vkBindImageMemory2(
1643 context, input_result, device, bindingCount, pBindInfos);
1644}
1645
1646VkResult ResourceTracker::on_vkBindImageMemory2KHR(
1647 void* context, VkResult input_result,
1648 VkDevice device, uint32_t bindingCount, const VkBindImageMemoryInfo* pBindInfos) {
1649 return mImpl->on_vkBindImageMemory2KHR(
1650 context, input_result, device, bindingCount, pBindInfos);
1651}
1652
1653VkResult ResourceTracker::on_vkCreateBuffer(
1654 void* context, VkResult input_result,
1655 VkDevice device, const VkBufferCreateInfo *pCreateInfo,
1656 const VkAllocationCallbacks *pAllocator,
1657 VkBuffer *pBuffer) {
1658 return mImpl->on_vkCreateBuffer(
1659 context, input_result,
1660 device, pCreateInfo, pAllocator, pBuffer);
1661}
1662
1663void ResourceTracker::on_vkDestroyBuffer(
1664 void* context,
1665 VkDevice device, VkBuffer buffer, const VkAllocationCallbacks *pAllocator) {
1666 mImpl->on_vkDestroyBuffer(context, device, buffer, pAllocator);
1667}
1668
1669void ResourceTracker::on_vkGetBufferMemoryRequirements(
1670 void* context, VkDevice device, VkBuffer buffer, VkMemoryRequirements *pMemoryRequirements) {
1671 mImpl->on_vkGetBufferMemoryRequirements(context, device, buffer, pMemoryRequirements);
1672}
1673
1674void ResourceTracker::on_vkGetBufferMemoryRequirements2(
1675 void* context, VkDevice device, const VkBufferMemoryRequirementsInfo2* pInfo,
1676 VkMemoryRequirements2* pMemoryRequirements) {
1677 mImpl->on_vkGetBufferMemoryRequirements2(
1678 context, device, pInfo, pMemoryRequirements);
1679}
1680
1681void ResourceTracker::on_vkGetBufferMemoryRequirements2KHR(
1682 void* context, VkDevice device, const VkBufferMemoryRequirementsInfo2* pInfo,
1683 VkMemoryRequirements2* pMemoryRequirements) {
1684 mImpl->on_vkGetBufferMemoryRequirements2KHR(
1685 context, device, pInfo, pMemoryRequirements);
1686}
1687
1688VkResult ResourceTracker::on_vkBindBufferMemory(
1689 void* context, VkResult input_result,
1690 VkDevice device, VkBuffer buffer, VkDeviceMemory memory, VkDeviceSize memoryOffset) {
1691 return mImpl->on_vkBindBufferMemory(
1692 context, input_result,
1693 device, buffer, memory, memoryOffset);
1694}
1695
1696VkResult ResourceTracker::on_vkBindBufferMemory2(
1697 void* context, VkResult input_result,
1698 VkDevice device, uint32_t bindInfoCount, const VkBindBufferMemoryInfo *pBindInfos) {
1699 return mImpl->on_vkBindBufferMemory2(
1700 context, input_result,
1701 device, bindInfoCount, pBindInfos);
1702}
1703
1704VkResult ResourceTracker::on_vkBindBufferMemory2KHR(
1705 void* context, VkResult input_result,
1706 VkDevice device, uint32_t bindInfoCount, const VkBindBufferMemoryInfo *pBindInfos) {
1707 return mImpl->on_vkBindBufferMemory2KHR(
1708 context, input_result,
1709 device, bindInfoCount, pBindInfos);
1710}
1711
Lingfeng Yangdef88ba2018-12-13 12:43:17 -08001712void ResourceTracker::unwrap_VkNativeBufferANDROID(
1713 const VkImageCreateInfo* pCreateInfo,
1714 VkImageCreateInfo* local_pCreateInfo) {
1715 mImpl->unwrap_VkNativeBufferANDROID(pCreateInfo, local_pCreateInfo);
1716}
1717
1718void ResourceTracker::unwrap_vkAcquireImageANDROID_nativeFenceFd(int fd, int* fd_out) {
1719 mImpl->unwrap_vkAcquireImageANDROID_nativeFenceFd(fd, fd_out);
1720}
1721
Lingfeng Yang236abc92018-12-21 20:19:33 -08001722VkResult ResourceTracker::on_vkMapMemoryIntoAddressSpaceGOOGLE_pre(
1723 void* context,
1724 VkResult input_result,
1725 VkDevice device,
1726 VkDeviceMemory memory,
1727 uint64_t* pAddress) {
1728 return mImpl->on_vkMapMemoryIntoAddressSpaceGOOGLE_pre(
1729 context, input_result, device, memory, pAddress);
1730}
1731
1732VkResult ResourceTracker::on_vkMapMemoryIntoAddressSpaceGOOGLE(
1733 void* context,
1734 VkResult input_result,
1735 VkDevice device,
1736 VkDeviceMemory memory,
1737 uint64_t* pAddress) {
1738 return mImpl->on_vkMapMemoryIntoAddressSpaceGOOGLE(
1739 context, input_result, device, memory, pAddress);
1740}
1741
Lingfeng Yang2b1b8cf2019-02-08 09:53:36 -08001742void ResourceTracker::deviceMemoryTransform_tohost(
1743 VkDeviceMemory* memory, uint32_t memoryCount,
1744 VkDeviceSize* offset, uint32_t offsetCount,
1745 VkDeviceSize* size, uint32_t sizeCount,
1746 uint32_t* typeIndex, uint32_t typeIndexCount,
1747 uint32_t* typeBits, uint32_t typeBitsCount) {
1748 mImpl->deviceMemoryTransform_tohost(
1749 memory, memoryCount,
1750 offset, offsetCount,
1751 size, sizeCount,
1752 typeIndex, typeIndexCount,
1753 typeBits, typeBitsCount);
1754}
1755
1756void ResourceTracker::deviceMemoryTransform_fromhost(
1757 VkDeviceMemory* memory, uint32_t memoryCount,
1758 VkDeviceSize* offset, uint32_t offsetCount,
1759 VkDeviceSize* size, uint32_t sizeCount,
1760 uint32_t* typeIndex, uint32_t typeIndexCount,
1761 uint32_t* typeBits, uint32_t typeBitsCount) {
1762 mImpl->deviceMemoryTransform_fromhost(
1763 memory, memoryCount,
1764 offset, offsetCount,
1765 size, sizeCount,
1766 typeIndex, typeIndexCount,
1767 typeBits, typeBitsCount);
1768}
1769
1770#define DEFINE_TRANSFORMED_TYPE_IMPL(type) \
1771 void ResourceTracker::transformImpl_##type##_tohost(const type*, uint32_t) { } \
1772 void ResourceTracker::transformImpl_##type##_fromhost(const type*, uint32_t) { } \
1773
1774LIST_TRANSFORMED_TYPES(DEFINE_TRANSFORMED_TYPE_IMPL)
1775
Lingfeng Yang29cf0752019-02-13 14:12:25 -08001776} // namespace goldfish_vk