blob: 8f16f8a0ec174583dd4c8799eee0f3a9bc9463e7 [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
David Reveman32b110e2019-02-21 13:20:54 -05001212 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 Yangdef88ba2018-12-13 12:43:17 -08001229 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 Yange637ca52018-12-14 18:24:56 -08001248 reinterpret_cast<VkNativeBufferANDROID*>(
1249 const_cast<void*>(
1250 local_pCreateInfo->pNext));
Lingfeng Yangdef88ba2018-12-13 12:43:17 -08001251
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 Reveman32b110e2019-02-21 13:20:54 -05001266 void unwrap_vkQueueSubmit(uint32_t, const VkSubmitInfo*, VkSubmitInfo*) {
1267 }
1268
Lingfeng Yang236abc92018-12-21 20:19:33 -08001269 // 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 Yang35e9c6a2018-12-25 17:13:36 -08001297 memInfo.goldfishAddressSpaceBlock =
1298 new GoldfishAddressSpaceBlock;
Lingfeng Yang236abc92018-12-21 20:19:33 -08001299 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 Yang49c7de22019-01-23 16:19:50 -08001336 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 Yang236abc92018-12-21 20:19:33 -08001343 *pAddress = (uint64_t)(uintptr_t)userPtr;
1344
1345 return input_result;
1346 }
1347
Lingfeng Yangf0654ff2019-02-02 12:21:24 -08001348 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 Yang6ab1b0d2018-11-27 23:36:03 -08001393private:
1394 mutable Lock mLock;
Lingfeng Yangafe29d32018-12-25 13:01:52 -08001395 HostVisibleMemoryVirtualizationInfo mHostVisibleMemoryVirtInfo;
Lingfeng Yang31754632018-12-21 18:24:55 -08001396 std::unique_ptr<EmulatorFeatureInfo> mFeatureInfo;
Lingfeng Yang236abc92018-12-21 20:19:33 -08001397 std::unique_ptr<GoldfishAddressSpaceBlockProvider> mGoldfishAddressSpaceBlockProvider;
Lingfeng Yang154a33c2019-01-29 19:06:23 -08001398
1399 std::vector<VkExtensionProperties> mHostInstanceExtensions;
1400 std::vector<VkExtensionProperties> mHostDeviceExtensions;
Lingfeng Yang71b596b2018-11-07 18:03:25 -08001401};
Lingfeng Yang154a33c2019-01-29 19:06:23 -08001402
Lingfeng Yang71b596b2018-11-07 18:03:25 -08001403ResourceTracker::ResourceTracker() : mImpl(new ResourceTracker::Impl()) { }
1404ResourceTracker::~ResourceTracker() { }
Lingfeng Yang71b596b2018-11-07 18:03:25 -08001405VulkanHandleMapping* ResourceTracker::createMapping() {
1406 return &mImpl->createMapping;
1407}
Lingfeng Yang71b596b2018-11-07 18:03:25 -08001408VulkanHandleMapping* ResourceTracker::unwrapMapping() {
1409 return &mImpl->unwrapMapping;
1410}
Lingfeng Yang71b596b2018-11-07 18:03:25 -08001411VulkanHandleMapping* ResourceTracker::destroyMapping() {
1412 return &mImpl->destroyMapping;
1413}
Lingfeng Yang2285df12018-11-17 16:25:11 -08001414VulkanHandleMapping* ResourceTracker::defaultMapping() {
1415 return &mImpl->defaultMapping;
1416}
Lingfeng Yang71b596b2018-11-07 18:03:25 -08001417static ResourceTracker* sTracker = nullptr;
Lingfeng Yang71b596b2018-11-07 18:03:25 -08001418// static
1419ResourceTracker* ResourceTracker::get() {
1420 if (!sTracker) {
1421 // To be initialized once on vulkan device open.
1422 sTracker = new ResourceTracker;
1423 }
1424 return sTracker;
1425}
Lingfeng Yang71b596b2018-11-07 18:03:25 -08001426
Lingfeng Yang6ab1b0d2018-11-27 23:36:03 -08001427#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
1435GOLDFISH_VK_LIST_HANDLE_TYPES(HANDLE_REGISTER_IMPL)
1436
Lingfeng Yang6ab1b0d2018-11-27 23:36:03 -08001437bool ResourceTracker::isMemoryTypeHostVisible(
1438 VkDevice device, uint32_t typeIndex) const {
1439 return mImpl->isMemoryTypeHostVisible(device, typeIndex);
1440}
1441
Lingfeng Yangdef88ba2018-12-13 12:43:17 -08001442uint8_t* ResourceTracker::getMappedPointer(VkDeviceMemory memory) {
1443 return mImpl->getMappedPointer(memory);
1444}
1445
1446VkDeviceSize ResourceTracker::getMappedSize(VkDeviceMemory memory) {
1447 return mImpl->getMappedSize(memory);
1448}
1449
Lingfeng Yang6ab1b0d2018-11-27 23:36:03 -08001450VkDeviceSize ResourceTracker::getNonCoherentExtendedSize(VkDevice device, VkDeviceSize basicSize) const {
1451 return mImpl->getNonCoherentExtendedSize(device, basicSize);
1452}
1453
Lingfeng Yangdef88ba2018-12-13 12:43:17 -08001454bool ResourceTracker::isValidMemoryRange(const VkMappedMemoryRange& range) const {
1455 return mImpl->isValidMemoryRange(range);
1456}
1457
Lingfeng Yang31754632018-12-21 18:24:55 -08001458void ResourceTracker::setupFeatures(const EmulatorFeatureInfo* features) {
1459 mImpl->setupFeatures(features);
1460}
1461
Lingfeng Yangb8a38c72019-02-02 20:27:54 -08001462bool ResourceTracker::hostSupportsVulkan() const {
1463 return mImpl->hostSupportsVulkan();
1464}
1465
Lingfeng Yang236abc92018-12-21 20:19:33 -08001466bool ResourceTracker::usingDirectMapping() const {
1467 return mImpl->usingDirectMapping();
1468}
1469
Lingfeng Yangf0654ff2019-02-02 12:21:24 -08001470uint32_t ResourceTracker::getApiVersionFromInstance(VkInstance instance) const {
1471 return mImpl->getApiVersionFromInstance(instance);
1472}
1473
1474uint32_t ResourceTracker::getApiVersionFromDevice(VkDevice device) const {
1475 return mImpl->getApiVersionFromDevice(device);
1476}
1477bool ResourceTracker::hasInstanceExtension(VkInstance instance, const std::string &name) const {
1478 return mImpl->hasInstanceExtension(instance, name);
1479}
1480bool ResourceTracker::hasDeviceExtension(VkDevice device, const std::string &name) const {
1481 return mImpl->hasDeviceExtension(device, name);
1482}
1483
Lingfeng Yangdef88ba2018-12-13 12:43:17 -08001484VkResult 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 Yang154a33c2019-01-29 19:06:23 -08001491VkResult 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 Yangdef88ba2018-12-13 12:43:17 -08001501VkResult 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 Yangb64ca452019-02-14 22:04:28 -08001512VkResult 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 Yang97a06702018-12-24 17:02:43 -08001521void ResourceTracker::on_vkGetPhysicalDeviceMemoryProperties(
1522 void* context,
1523 VkPhysicalDevice physicalDevice,
1524 VkPhysicalDeviceMemoryProperties* pMemoryProperties) {
1525 mImpl->on_vkGetPhysicalDeviceMemoryProperties(
1526 context, physicalDevice, pMemoryProperties);
1527}
1528
Lingfeng Yang154a33c2019-01-29 19:06:23 -08001529void ResourceTracker::on_vkGetPhysicalDeviceMemoryProperties2(
1530 void* context,
1531 VkPhysicalDevice physicalDevice,
1532 VkPhysicalDeviceMemoryProperties2* pMemoryProperties) {
1533 mImpl->on_vkGetPhysicalDeviceMemoryProperties2(
1534 context, physicalDevice, pMemoryProperties);
1535}
1536
1537void ResourceTracker::on_vkGetPhysicalDeviceMemoryProperties2KHR(
1538 void* context,
1539 VkPhysicalDevice physicalDevice,
1540 VkPhysicalDeviceMemoryProperties2* pMemoryProperties) {
1541 mImpl->on_vkGetPhysicalDeviceMemoryProperties2(
1542 context, physicalDevice, pMemoryProperties);
1543}
1544
Lingfeng Yangf0654ff2019-02-02 12:21:24 -08001545VkResult 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 Yang131d5a42018-11-30 12:00:33 -08001555VkResult 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 Yang35e9c6a2018-12-25 17:13:36 -08001566void 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 Yang131d5a42018-11-30 12:00:33 -08001573VkResult 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 Yange9979522018-12-25 14:44:52 -08001584void 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 Yangdef88ba2018-12-13 12:43:17 -08001593VkResult 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
1606void ResourceTracker::on_vkUnmapMemory(
1607 void* context,
1608 VkDevice device,
1609 VkDeviceMemory memory) {
1610 mImpl->on_vkUnmapMemory(context, device, memory);
1611}
1612
Lingfeng Yang4af5f322019-02-14 08:10:28 -08001613VkResult 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
1623void 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
1630void ResourceTracker::on_vkGetImageMemoryRequirements(
1631 void *context, VkDevice device, VkImage image,
1632 VkMemoryRequirements *pMemoryRequirements) {
1633 mImpl->on_vkGetImageMemoryRequirements(
1634 context, device, image, pMemoryRequirements);
1635}
1636
1637void 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
1644void 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
1651VkResult 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
1659VkResult 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
1666VkResult 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
1673VkResult 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
1683void ResourceTracker::on_vkDestroyBuffer(
1684 void* context,
1685 VkDevice device, VkBuffer buffer, const VkAllocationCallbacks *pAllocator) {
1686 mImpl->on_vkDestroyBuffer(context, device, buffer, pAllocator);
1687}
1688
1689void ResourceTracker::on_vkGetBufferMemoryRequirements(
1690 void* context, VkDevice device, VkBuffer buffer, VkMemoryRequirements *pMemoryRequirements) {
1691 mImpl->on_vkGetBufferMemoryRequirements(context, device, buffer, pMemoryRequirements);
1692}
1693
1694void 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
1701void 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
1708VkResult 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
1716VkResult 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
1724VkResult 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 Reveman32b110e2019-02-21 13:20:54 -05001732VkResult 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
1742void 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 Yangdef88ba2018-12-13 12:43:17 -08001748void ResourceTracker::unwrap_VkNativeBufferANDROID(
1749 const VkImageCreateInfo* pCreateInfo,
1750 VkImageCreateInfo* local_pCreateInfo) {
1751 mImpl->unwrap_VkNativeBufferANDROID(pCreateInfo, local_pCreateInfo);
1752}
1753
1754void ResourceTracker::unwrap_vkAcquireImageANDROID_nativeFenceFd(int fd, int* fd_out) {
1755 mImpl->unwrap_vkAcquireImageANDROID_nativeFenceFd(fd, fd_out);
1756}
1757
David Reveman32b110e2019-02-21 13:20:54 -05001758void ResourceTracker::unwrap_vkQueueSubmit(
1759 uint32_t submitCount, const VkSubmitInfo* pSubmits, VkSubmitInfo* local_pSubmits) {
1760 mImpl->unwrap_vkQueueSubmit(submitCount, pSubmits, local_pSubmits);
1761}
1762
Lingfeng Yang236abc92018-12-21 20:19:33 -08001763VkResult 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
1773VkResult 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 Yang2b1b8cf2019-02-08 09:53:36 -08001783void 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
1797void 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
1815LIST_TRANSFORMED_TYPES(DEFINE_TRANSFORMED_TYPE_IMPL)
1816
Lingfeng Yang29cf0752019-02-13 14:12:25 -08001817} // namespace goldfish_vk