Debian Codegraph @

src/gfxstream/guest/vulkan_enc/ResourceTracker.cpp

raw
1 /*
2 * Copyright 2018 Google
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "ResourceTracker.h"
7
8 #include "CommandBufferStagingStream.h"
9 #include "DescriptorSetVirtualization.h"
10 #include "HostVisibleMemoryVirtualization.h"
11 #include "Resources.h"
12 #include "VkEncoder.h"
13 #include "gfxstream_vk_private.h"
14 #include "git_sha1.h"
15 #include "goldfish_address_space.h"
16 #include "goldfish_vk_private_defs.h"
17 #include "util/anon_file.h"
18 #include "util/detect_os.h"
19 #include "util/log.h"
20 #include "util/macros.h"
21 #include "virtio/virtio-gpu/virgl_hw.h"
22 #include "vulkan/vulkan_core.h"
23
24 #ifdef VK_USE_PLATFORM_ANDROID_KHR
25 #include "vk_format_info.h"
26 #include <vndk/hardware_buffer.h>
27 #endif
28 #include <stdlib.h>
29 #include <stdint.h>
30
31 #include <algorithm>
32 #include <chrono>
33 #include <random>
34 #include <set>
35 #include <string>
36 #include <unordered_map>
37 #include <unordered_set>
38
39 #ifdef HAVE_PERFETTO
40 #ifndef ANDROID_LIBPERFETTO
41 #include <perfetto.h>
42 #else
43 #include <perfetto/tracing.h>
44 #endif
45
46 #define GFXSTREAM_TRACE_DEFAULT_CATEGORY "gfxstream.default"
47
48 PERFETTO_DEFINE_CATEGORIES(
49 perfetto::Category(GFXSTREAM_TRACE_DEFAULT_CATEGORY));
50
51 #endif // HAVE_PERFETTO
52
53 #include "vk_util.h"
54
55 #if DETECT_OS_LINUX
56 #include <drm_fourcc.h>
57 #endif
58
59 #ifndef VK_USE_PLATFORM_FUCHSIA
60 static void zx_handle_close(zx_handle_t) {}
61 #endif
62
63 static constexpr uint32_t kDefaultApiVersion = VK_MAKE_VERSION(1, 1, 0);
64
65 struct vk_struct_chain_iterator {
66 VkBaseOutStructure* value;
67 };
68
69 template <class T>
70 static vk_struct_chain_iterator vk_make_chain_iterator(T* vk_struct) {
71 vk_struct_chain_iterator result = {reinterpret_cast<VkBaseOutStructure*>(vk_struct)};
72 return result;
73 }
74
75 template <class T>
76 static void vk_append_struct(vk_struct_chain_iterator* i, T* vk_struct) {
77 VkBaseOutStructure* p = i->value;
78 if (p->pNext) {
79 ::abort();
80 }
81
82 p->pNext = reinterpret_cast<VkBaseOutStructure*>(vk_struct);
83 vk_struct->pNext = NULL;
84
85 *i = vk_make_chain_iterator(vk_struct);
86 }
87
88 template <class T>
89 static T vk_make_orphan_copy(const T& vk_struct) {
90 T copy = vk_struct;
91 copy.pNext = NULL;
92 return copy;
93 }
94
95
96 namespace gfxstream {
97 namespace vk {
98 namespace {
99
100 #ifdef HAVE_PERFETTO
101 uint64_t GeneratePseudoUniqueId() {
102 thread_local std::mt19937 generator(std::random_device{}());
103 std::uniform_int_distribution<uint64_t> distribution(0, std::numeric_limits<uint64_t>::max());
104 return distribution(generator);
105 }
106 #endif
107
108 } // namespace
109
110 #define MAKE_HANDLE_MAPPING_FOREACH(type_name, map_impl, map_to_u64_impl, map_from_u64_impl) \
111 void mapHandles_##type_name(type_name* handles, size_t count) override { \
112 for (size_t i = 0; i < count; ++i) { \
113 map_impl; \
114 } \
115 } \
116 void mapHandles_##type_name##_u64(const type_name* handles, uint64_t* handle_u64s, \
117 size_t count) override { \
118 for (size_t i = 0; i < count; ++i) { \
119 map_to_u64_impl; \
120 } \
121 } \
122 void mapHandles_u64_##type_name(const uint64_t* handle_u64s, type_name* handles, size_t count) \
123 override { \
124 for (size_t i = 0; i < count; ++i) { \
125 map_from_u64_impl; \
126 } \
127 }
128
129 #define DEFINE_RESOURCE_TRACKING_CLASS(class_name, impl) \
130 class class_name : public VulkanHandleMapping { \
131 public: \
132 virtual ~class_name() {} \
133 GOLDFISH_VK_LIST_HANDLE_TYPES(impl) \
134 };
135
136 #define CREATE_MAPPING_IMPL_FOR_TYPE(type_name) \
137 MAKE_HANDLE_MAPPING_FOREACH( \
138 type_name, handles[i] = new_from_host_##type_name(handles[i]); \
139 ResourceTracker::get()->register_##type_name(handles[i]); \
140 , handle_u64s[i] = (uint64_t)new_from_host_##type_name(handles[i]), \
141 handles[i] = (type_name)new_from_host_u64_##type_name(handle_u64s[i]); \
142 ResourceTracker::get()->register_##type_name(handles[i]);)
143
144 #define UNWRAP_MAPPING_IMPL_FOR_TYPE(type_name) \
145 MAKE_HANDLE_MAPPING_FOREACH( \
146 type_name, handles[i] = get_host_##type_name(handles[i]), \
147 handle_u64s[i] = (uint64_t)get_host_u64_##type_name(handles[i]), \
148 handles[i] = (type_name)get_host_##type_name((type_name)handle_u64s[i]))
149
150 #define DESTROY_MAPPING_IMPL_FOR_TYPE(type_name) \
151 MAKE_HANDLE_MAPPING_FOREACH(type_name, \
152 ResourceTracker::get()->unregister_##type_name(handles[i]); \
153 delete_goldfish_##type_name(handles[i]), (void)handle_u64s[i]; \
154 delete_goldfish_##type_name(handles[i]), (void)handles[i]; \
155 delete_goldfish_##type_name((type_name)handle_u64s[i]))
156
157 DEFINE_RESOURCE_TRACKING_CLASS(CreateMapping, CREATE_MAPPING_IMPL_FOR_TYPE)
158 DEFINE_RESOURCE_TRACKING_CLASS(DestroyMapping, DESTROY_MAPPING_IMPL_FOR_TYPE)
159
160 static uint32_t* sSeqnoPtr = nullptr;
161
162 // static
163 uint32_t ResourceTracker::streamFeatureBits = 0;
164 ResourceTracker::ThreadingCallbacks ResourceTracker::threadingCallbacks;
165
166 struct StagingInfo {
167 std::mutex mLock;
168 std::vector<CommandBufferStagingStream*> streams;
169 std::vector<VkEncoder*> encoders;
170 /// \brief sets alloc and free callbacks for memory allocation for CommandBufferStagingStream(s)
171 /// \param allocFn is the callback to allocate memory
172 /// \param freeFn is the callback to free memory
173 void setAllocFree(CommandBufferStagingStream::Alloc&& allocFn,
174 CommandBufferStagingStream::Free&& freeFn) {
175 mAlloc = allocFn;
176 mFree = freeFn;
177 }
178
179 ~StagingInfo() {
180 for (auto encoder : encoders) {
181 delete encoder;
182 }
183 for (auto stream : streams) {
184 delete stream;
185 }
186 }
187
188 void pushStaging(CommandBufferStagingStream* stream, VkEncoder* encoder) {
189 std::lock_guard<std::mutex> lock(mLock);
190 stream->reset();
191 streams.push_back(stream);
192 encoders.push_back(encoder);
193 }
194
195 void popStaging(CommandBufferStagingStream** streamOut, VkEncoder** encoderOut) {
196 std::lock_guard<std::mutex> lock(mLock);
197 CommandBufferStagingStream* stream;
198 VkEncoder* encoder;
199 if (streams.empty()) {
200 if (mAlloc && mFree) {
201 // if custom allocators are provided, forward them to CommandBufferStagingStream
202 stream = new CommandBufferStagingStream(mAlloc, mFree);
203 } else {
204 stream = new CommandBufferStagingStream;
205 }
206 encoder = new VkEncoder(stream);
207 } else {
208 stream = streams.back();
209 encoder = encoders.back();
210 streams.pop_back();
211 encoders.pop_back();
212 }
213 *streamOut = stream;
214 *encoderOut = encoder;
215 }
216
217 private:
218 CommandBufferStagingStream::Alloc mAlloc = nullptr;
219 CommandBufferStagingStream::Free mFree = nullptr;
220 };
221
222 static StagingInfo sStaging;
223
224 struct CommandBufferPendingDescriptorSets {
225 std::unordered_set<VkDescriptorSet> sets;
226 };
227
228 #define HANDLE_REGISTER_IMPL_IMPL(type) \
229 void ResourceTracker::register_##type(type obj) { \
230 std::lock_guard<std::recursive_mutex> lock(mLock); \
231 info_##type[obj] = type##_Info(); \
232 }
233
234 #define HANDLE_UNREGISTER_IMPL_IMPL(type) \
235 void ResourceTracker::unregister_##type(type obj) { \
236 std::lock_guard<std::recursive_mutex> lock(mLock); \
237 info_##type.erase(obj); \
238 }
239
240 GOLDFISH_VK_LIST_HANDLE_TYPES(HANDLE_REGISTER_IMPL_IMPL)
241 GOLDFISH_VK_LIST_TRIVIAL_HANDLE_TYPES(HANDLE_UNREGISTER_IMPL_IMPL)
242 static uint32_t getWaitSemaphoreCount(const VkSubmitInfo& pSubmit) {
243 return pSubmit.waitSemaphoreCount;
244 }
245
246 static uint32_t getWaitSemaphoreCount(const VkSubmitInfo2& pSubmit) {
247 return pSubmit.waitSemaphoreInfoCount;
248 }
249
250 static uint32_t getCommandBufferCount(const VkSubmitInfo& pSubmit) {
251 return pSubmit.commandBufferCount;
252 }
253
254 static uint32_t getCommandBufferCount(const VkSubmitInfo2& pSubmit) {
255 return pSubmit.commandBufferInfoCount;
256 }
257
258 static uint32_t getSignalSemaphoreCount(const VkSubmitInfo& pSubmit) {
259 return pSubmit.signalSemaphoreCount;
260 }
261
262 static uint32_t getSignalSemaphoreCount(const VkSubmitInfo2& pSubmit) {
263 return pSubmit.signalSemaphoreInfoCount;
264 }
265
266 static VkSemaphore getWaitSemaphore(const VkSubmitInfo& pSubmit, int i) {
267 return pSubmit.pWaitSemaphores[i];
268 }
269
270 static VkSemaphore getWaitSemaphore(const VkSubmitInfo2& pSubmit, int i) {
271 return pSubmit.pWaitSemaphoreInfos[i].semaphore;
272 }
273
274 static VkSemaphore getSignalSemaphore(const VkSubmitInfo& pSubmit, int i) {
275 return pSubmit.pSignalSemaphores[i];
276 }
277
278 static VkSemaphore getSignalSemaphore(const VkSubmitInfo2& pSubmit, int i) {
279 return pSubmit.pSignalSemaphoreInfos[i].semaphore;
280 }
281
282 static VkCommandBuffer getCommandBuffer(const VkSubmitInfo& pSubmit, int i) {
283 return pSubmit.pCommandBuffers[i];
284 }
285
286 static VkCommandBuffer getCommandBuffer(const VkSubmitInfo2& pSubmit, int i) {
287 return pSubmit.pCommandBufferInfos[i].commandBuffer;
288 }
289
290 static bool descriptorPoolSupportsIndividualFreeLocked(VkDescriptorPool pool) {
291 return as_goldfish_VkDescriptorPool(pool)->allocInfo->createFlags &
292 VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT;
293 }
294
295 static bool descriptorBindingIsImmutableSampler(VkDescriptorSet dstSet, uint32_t dstBinding) {
296 return as_goldfish_VkDescriptorSet(dstSet)->reified->bindingIsImmutableSampler[dstBinding];
297 }
298
299 static bool isHostVisible(const VkPhysicalDeviceMemoryProperties* memoryProps, uint32_t index) {
300 return memoryProps->memoryTypes[index].propertyFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
301 }
302
303 VkDescriptorImageInfo ResourceTracker::filterNonexistentSampler(
304 const VkDescriptorImageInfo& inputInfo) {
305 VkSampler sampler = inputInfo.sampler;
306
307 VkDescriptorImageInfo res = inputInfo;
308
309 if (sampler) {
310 auto it = info_VkSampler.find(sampler);
311 bool samplerExists = it != info_VkSampler.end();
312 if (!samplerExists) res.sampler = 0;
313 }
314
315 return res;
316 }
317
318 void ResourceTracker::emitDeviceMemoryReport(VkDevice_Info info,
319 VkDeviceMemoryReportEventTypeEXT type,
320 uint64_t memoryObjectId, VkDeviceSize size,
321 VkObjectType objectType, uint64_t objectHandle,
322 uint32_t heapIndex) {
323 if (info.deviceMemoryReportCallbacks.empty()) return;
324
325 const VkDeviceMemoryReportCallbackDataEXT callbackData = {
326 VK_STRUCTURE_TYPE_DEVICE_MEMORY_REPORT_CALLBACK_DATA_EXT, // sType
327 nullptr, // pNext
328 0, // flags
329 type, // type
330 memoryObjectId, // memoryObjectId
331 size, // size
332 objectType, // objectType
333 objectHandle, // objectHandle
334 heapIndex, // heapIndex
335 };
336 for (const auto& callback : info.deviceMemoryReportCallbacks) {
337 callback.first(&callbackData, callback.second);
338 }
339 }
340
341 #ifdef VK_USE_PLATFORM_FUCHSIA
342 inline fuchsia_sysmem::wire::BufferCollectionConstraints defaultBufferCollectionConstraints(
343 size_t minSizeBytes, size_t minBufferCount, size_t maxBufferCount = 0u,
344 size_t minBufferCountForCamping = 0u, size_t minBufferCountForDedicatedSlack = 0u,
345 size_t minBufferCountForSharedSlack = 0u) {
346 fuchsia_sysmem::wire::BufferCollectionConstraints constraints = {};
347 constraints.min_buffer_count = minBufferCount;
348 if (maxBufferCount > 0) {
349 constraints.max_buffer_count = maxBufferCount;
350 }
351 if (minBufferCountForCamping) {
352 constraints.min_buffer_count_for_camping = minBufferCountForCamping;
353 }
354 if (minBufferCountForSharedSlack) {
355 constraints.min_buffer_count_for_shared_slack = minBufferCountForSharedSlack;
356 }
357 constraints.has_buffer_memory_constraints = true;
358 fuchsia_sysmem::wire::BufferMemoryConstraints& buffer_constraints =
359 constraints.buffer_memory_constraints;
360
361 buffer_constraints.min_size_bytes = minSizeBytes;
362 buffer_constraints.max_size_bytes = 0xffffffff;
363 buffer_constraints.physically_contiguous_required = false;
364 buffer_constraints.secure_required = false;
365
366 // No restrictions on coherency domain or Heaps.
367 buffer_constraints.ram_domain_supported = true;
368 buffer_constraints.cpu_domain_supported = true;
369 buffer_constraints.inaccessible_domain_supported = true;
370 buffer_constraints.heap_permitted_count = 2;
371 buffer_constraints.heap_permitted[0] = fuchsia_sysmem::wire::HeapType::kGoldfishDeviceLocal;
372 buffer_constraints.heap_permitted[1] = fuchsia_sysmem::wire::HeapType::kGoldfishHostVisible;
373
374 return constraints;
375 }
376
377 uint32_t getBufferCollectionConstraintsVulkanImageUsage(const VkImageCreateInfo* pImageInfo) {
378 uint32_t usage = 0u;
379 VkImageUsageFlags imageUsage = pImageInfo->usage;
380
381 #define SetUsageBit(BIT, VALUE) \
382 if (imageUsage & VK_IMAGE_USAGE_##BIT##_BIT) { \
383 usage |= fuchsia_sysmem::wire::kVulkanImageUsage##VALUE; \
384 }
385
386 SetUsageBit(COLOR_ATTACHMENT, ColorAttachment);
387 SetUsageBit(TRANSFER_SRC, TransferSrc);
388 SetUsageBit(TRANSFER_DST, TransferDst);
389 SetUsageBit(SAMPLED, Sampled);
390
391 #undef SetUsageBit
392 return usage;
393 }
394
395 uint32_t getBufferCollectionConstraintsVulkanBufferUsage(VkBufferUsageFlags bufferUsage) {
396 uint32_t usage = 0u;
397
398 #define SetUsageBit(BIT, VALUE) \
399 if (bufferUsage & VK_BUFFER_USAGE_##BIT##_BIT) { \
400 usage |= fuchsia_sysmem::wire::kVulkanBufferUsage##VALUE; \
401 }
402
403 SetUsageBit(TRANSFER_SRC, TransferSrc);
404 SetUsageBit(TRANSFER_DST, TransferDst);
405 SetUsageBit(UNIFORM_TEXEL_BUFFER, UniformTexelBuffer);
406 SetUsageBit(STORAGE_TEXEL_BUFFER, StorageTexelBuffer);
407 SetUsageBit(UNIFORM_BUFFER, UniformBuffer);
408 SetUsageBit(STORAGE_BUFFER, StorageBuffer);
409 SetUsageBit(INDEX_BUFFER, IndexBuffer);
410 SetUsageBit(VERTEX_BUFFER, VertexBuffer);
411 SetUsageBit(INDIRECT_BUFFER, IndirectBuffer);
412
413 #undef SetUsageBit
414 return usage;
415 }
416
417 uint32_t getBufferCollectionConstraintsVulkanBufferUsage(
418 const VkBufferConstraintsInfoFUCHSIA* pBufferConstraintsInfo) {
419 VkBufferUsageFlags bufferUsage = pBufferConstraintsInfo->createInfo.usage;
420 return getBufferCollectionConstraintsVulkanBufferUsage(bufferUsage);
421 }
422
423 static fuchsia_sysmem::wire::PixelFormatType vkFormatTypeToSysmem(VkFormat format) {
424 switch (format) {
425 case VK_FORMAT_B8G8R8A8_SINT:
426 case VK_FORMAT_B8G8R8A8_UNORM:
427 case VK_FORMAT_B8G8R8A8_SRGB:
428 case VK_FORMAT_B8G8R8A8_SNORM:
429 case VK_FORMAT_B8G8R8A8_SSCALED:
430 case VK_FORMAT_B8G8R8A8_USCALED:
431 return fuchsia_sysmem::wire::PixelFormatType::kBgra32;
432 case VK_FORMAT_R8G8B8A8_SINT:
433 case VK_FORMAT_R8G8B8A8_UNORM:
434 case VK_FORMAT_R8G8B8A8_SRGB:
435 case VK_FORMAT_R8G8B8A8_SNORM:
436 case VK_FORMAT_R8G8B8A8_SSCALED:
437 case VK_FORMAT_R8G8B8A8_USCALED:
438 return fuchsia_sysmem::wire::PixelFormatType::kR8G8B8A8;
439 case VK_FORMAT_R8_UNORM:
440 case VK_FORMAT_R8_UINT:
441 case VK_FORMAT_R8_USCALED:
442 case VK_FORMAT_R8_SNORM:
443 case VK_FORMAT_R8_SINT:
444 case VK_FORMAT_R8_SSCALED:
445 case VK_FORMAT_R8_SRGB:
446 return fuchsia_sysmem::wire::PixelFormatType::kR8;
447 case VK_FORMAT_R8G8_UNORM:
448 case VK_FORMAT_R8G8_UINT:
449 case VK_FORMAT_R8G8_USCALED:
450 case VK_FORMAT_R8G8_SNORM:
451 case VK_FORMAT_R8G8_SINT:
452 case VK_FORMAT_R8G8_SSCALED:
453 case VK_FORMAT_R8G8_SRGB:
454 return fuchsia_sysmem::wire::PixelFormatType::kR8G8;
455 default:
456 return fuchsia_sysmem::wire::PixelFormatType::kInvalid;
457 }
458 }
459
460 static bool vkFormatMatchesSysmemFormat(VkFormat vkFormat,
461 fuchsia_sysmem::wire::PixelFormatType sysmemFormat) {
462 switch (vkFormat) {
463 case VK_FORMAT_B8G8R8A8_SINT:
464 case VK_FORMAT_B8G8R8A8_UNORM:
465 case VK_FORMAT_B8G8R8A8_SRGB:
466 case VK_FORMAT_B8G8R8A8_SNORM:
467 case VK_FORMAT_B8G8R8A8_SSCALED:
468 case VK_FORMAT_B8G8R8A8_USCALED:
469 return sysmemFormat == fuchsia_sysmem::wire::PixelFormatType::kBgra32;
470 case VK_FORMAT_R8G8B8A8_SINT:
471 case VK_FORMAT_R8G8B8A8_UNORM:
472 case VK_FORMAT_R8G8B8A8_SRGB:
473 case VK_FORMAT_R8G8B8A8_SNORM:
474 case VK_FORMAT_R8G8B8A8_SSCALED:
475 case VK_FORMAT_R8G8B8A8_USCALED:
476 return sysmemFormat == fuchsia_sysmem::wire::PixelFormatType::kR8G8B8A8;
477 case VK_FORMAT_R8_UNORM:
478 case VK_FORMAT_R8_UINT:
479 case VK_FORMAT_R8_USCALED:
480 case VK_FORMAT_R8_SNORM:
481 case VK_FORMAT_R8_SINT:
482 case VK_FORMAT_R8_SSCALED:
483 case VK_FORMAT_R8_SRGB:
484 return sysmemFormat == fuchsia_sysmem::wire::PixelFormatType::kR8 ||
485 sysmemFormat == fuchsia_sysmem::wire::PixelFormatType::kL8;
486 case VK_FORMAT_R8G8_UNORM:
487 case VK_FORMAT_R8G8_UINT:
488 case VK_FORMAT_R8G8_USCALED:
489 case VK_FORMAT_R8G8_SNORM:
490 case VK_FORMAT_R8G8_SINT:
491 case VK_FORMAT_R8G8_SSCALED:
492 case VK_FORMAT_R8G8_SRGB:
493 return sysmemFormat == fuchsia_sysmem::wire::PixelFormatType::kR8G8;
494 default:
495 return false;
496 }
497 }
498
499 static VkFormat sysmemPixelFormatTypeToVk(fuchsia_sysmem::wire::PixelFormatType format) {
500 switch (format) {
501 case fuchsia_sysmem::wire::PixelFormatType::kBgra32:
502 return VK_FORMAT_B8G8R8A8_SRGB;
503 case fuchsia_sysmem::wire::PixelFormatType::kR8G8B8A8:
504 return VK_FORMAT_R8G8B8A8_SRGB;
505 case fuchsia_sysmem::wire::PixelFormatType::kL8:
506 case fuchsia_sysmem::wire::PixelFormatType::kR8:
507 return VK_FORMAT_R8_UNORM;
508 case fuchsia_sysmem::wire::PixelFormatType::kR8G8:
509 return VK_FORMAT_R8G8_UNORM;
510 default:
511 return VK_FORMAT_UNDEFINED;
512 }
513 }
514
515 // TODO(fxbug.dev/42172354): This is currently only used for allocating
516 // memory for dedicated external images. It should be migrated to use
517 // SetBufferCollectionImageConstraintsFUCHSIA.
518 VkResult ResourceTracker::setBufferCollectionConstraintsFUCHSIA(
519 VkEncoder* enc, VkDevice device,
520 fidl::WireSyncClient<fuchsia_sysmem::BufferCollection>* collection,
521 const VkImageCreateInfo* pImageInfo) {
522 if (pImageInfo == nullptr) {
523 mesa_loge("setBufferCollectionConstraints: pImageInfo cannot be null.");
524 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
525 }
526
527 const VkSysmemColorSpaceFUCHSIA kDefaultColorSpace = {
528 .sType = VK_STRUCTURE_TYPE_SYSMEM_COLOR_SPACE_FUCHSIA,
529 .pNext = nullptr,
530 .colorSpace = static_cast<uint32_t>(fuchsia_sysmem::wire::ColorSpaceType::kSrgb),
531 };
532
533 std::vector<VkImageFormatConstraintsInfoFUCHSIA> formatInfos;
534 if (pImageInfo->format == VK_FORMAT_UNDEFINED) {
535 const auto kFormats = {
536 VK_FORMAT_B8G8R8A8_SRGB,
537 VK_FORMAT_R8G8B8A8_SRGB,
538 };
539 for (auto format : kFormats) {
540 // shallow copy, using pNext from pImageInfo directly.
541 auto createInfo = *pImageInfo;
542 createInfo.format = format;
543 formatInfos.push_back(VkImageFormatConstraintsInfoFUCHSIA{
544 .sType = VK_STRUCTURE_TYPE_IMAGE_FORMAT_CONSTRAINTS_INFO_FUCHSIA,
545 .pNext = nullptr,
546 .imageCreateInfo = createInfo,
547 .colorSpaceCount = 1,
548 .pColorSpaces = &kDefaultColorSpace,
549 });
550 }
551 } else {
552 formatInfos.push_back(VkImageFormatConstraintsInfoFUCHSIA{
553 .sType = VK_STRUCTURE_TYPE_IMAGE_FORMAT_CONSTRAINTS_INFO_FUCHSIA,
554 .pNext = nullptr,
555 .imageCreateInfo = *pImageInfo,
556 .colorSpaceCount = 1,
557 .pColorSpaces = &kDefaultColorSpace,
558 });
559 }
560
561 VkImageConstraintsInfoFUCHSIA imageConstraints = {
562 .sType = VK_STRUCTURE_TYPE_IMAGE_CONSTRAINTS_INFO_FUCHSIA,
563 .pNext = nullptr,
564 .formatConstraintsCount = static_cast<uint32_t>(formatInfos.size()),
565 .pFormatConstraints = formatInfos.data(),
566 .bufferCollectionConstraints =
567 VkBufferCollectionConstraintsInfoFUCHSIA{
568 .sType = VK_STRUCTURE_TYPE_BUFFER_COLLECTION_CONSTRAINTS_INFO_FUCHSIA,
569 .pNext = nullptr,
570 .minBufferCount = 1,
571 .maxBufferCount = 0,
572 .minBufferCountForCamping = 0,
573 .minBufferCountForDedicatedSlack = 0,
574 .minBufferCountForSharedSlack = 0,
575 },
576 .flags = 0u,
577 };
578
579 return setBufferCollectionImageConstraintsFUCHSIA(enc, device, collection, &imageConstraints);
580 }
581
582 VkResult addImageBufferCollectionConstraintsFUCHSIA(
583 VkEncoder* enc, VkDevice device, VkPhysicalDevice physicalDevice,
584 const VkImageFormatConstraintsInfoFUCHSIA* formatConstraints, // always non-zero
585 VkImageTiling tiling, fuchsia_sysmem::wire::BufferCollectionConstraints* constraints) {
586 // First check if the format, tiling and usage is supported on host.
587 VkImageFormatProperties imageFormatProperties;
588 auto createInfo = &formatConstraints->imageCreateInfo;
589 auto result = enc->vkGetPhysicalDeviceImageFormatProperties(
590 physicalDevice, createInfo->format, createInfo->imageType, tiling, createInfo->usage,
591 createInfo->flags, &imageFormatProperties, true /* do lock */);
592 if (result != VK_SUCCESS) {
593 mesa_logd(
594 "%s: Image format (%u) type (%u) tiling (%u) "
595 "usage (%u) flags (%u) not supported by physical "
596 "device",
597 __func__, static_cast<uint32_t>(createInfo->format),
598 static_cast<uint32_t>(createInfo->imageType), static_cast<uint32_t>(tiling),
599 static_cast<uint32_t>(createInfo->usage), static_cast<uint32_t>(createInfo->flags));
600 return VK_ERROR_FORMAT_NOT_SUPPORTED;
601 }
602
603 // Check if format constraints contains unsupported format features.
604 {
605 VkFormatProperties formatProperties;
606 enc->vkGetPhysicalDeviceFormatProperties(physicalDevice, createInfo->format,
607 &formatProperties, true /* do lock */);
608
609 auto supportedFeatures = (tiling == VK_IMAGE_TILING_LINEAR)
610 ? formatProperties.linearTilingFeatures
611 : formatProperties.optimalTilingFeatures;
612 auto requiredFeatures = formatConstraints->requiredFormatFeatures;
613 if ((~supportedFeatures) & requiredFeatures) {
614 mesa_logd(
615 "%s: Host device support features for %s tiling: %08x, "
616 "required features: %08x, feature bits %08x missing",
617 __func__, tiling == VK_IMAGE_TILING_LINEAR ? "LINEAR" : "OPTIMAL",
618 static_cast<uint32_t>(requiredFeatures), static_cast<uint32_t>(supportedFeatures),
619 static_cast<uint32_t>((~supportedFeatures) & requiredFeatures));
620 return VK_ERROR_FORMAT_NOT_SUPPORTED;
621 }
622 }
623
624 fuchsia_sysmem::wire::ImageFormatConstraints imageConstraints;
625 if (formatConstraints->sysmemPixelFormat != 0) {
626 auto pixelFormat = static_cast<fuchsia_sysmem::wire::PixelFormatType>(
627 formatConstraints->sysmemPixelFormat);
628 if (createInfo->format != VK_FORMAT_UNDEFINED &&
629 !vkFormatMatchesSysmemFormat(createInfo->format, pixelFormat)) {
630 mesa_logd("%s: VkFormat %u doesn't match sysmem pixelFormat %lu", __func__,
631 static_cast<uint32_t>(createInfo->format), formatConstraints->sysmemPixelFormat);
632 return VK_ERROR_FORMAT_NOT_SUPPORTED;
633 }
634 imageConstraints.pixel_format.type = pixelFormat;
635 } else {
636 auto pixel_format = vkFormatTypeToSysmem(createInfo->format);
637 if (pixel_format == fuchsia_sysmem::wire::PixelFormatType::kInvalid) {
638 mesa_logd("%s: Unsupported VkFormat %u", __func__,
639 static_cast<uint32_t>(createInfo->format));
640 return VK_ERROR_FORMAT_NOT_SUPPORTED;
641 }
642 imageConstraints.pixel_format.type = pixel_format;
643 }
644
645 imageConstraints.color_spaces_count = formatConstraints->colorSpaceCount;
646 for (size_t i = 0; i < formatConstraints->colorSpaceCount; i++) {
647 imageConstraints.color_space[0].type = static_cast<fuchsia_sysmem::wire::ColorSpaceType>(
648 formatConstraints->pColorSpaces[i].colorSpace);
649 }
650
651 // Get row alignment from host GPU.
652 VkDeviceSize offset = 0;
653 VkDeviceSize rowPitchAlignment = 1u;
654
655 if (tiling == VK_IMAGE_TILING_LINEAR) {
656 VkImageCreateInfo createInfoDup = *createInfo;
657 createInfoDup.pNext = nullptr;
658 enc->vkGetLinearImageLayout2GOOGLE(device, &createInfoDup, &offset, &rowPitchAlignment,
659 true /* do lock */);
660 mesa_logd(
661 "vkGetLinearImageLayout2GOOGLE: format %d offset %lu "
662 "rowPitchAlignment = %lu",
663 (int)createInfo->format, offset, rowPitchAlignment);
664 }
665
666 imageConstraints.min_coded_width = createInfo->extent.width;
667 imageConstraints.max_coded_width = 0xfffffff;
668 imageConstraints.min_coded_height = createInfo->extent.height;
669 imageConstraints.max_coded_height = 0xffffffff;
670 // The min_bytes_per_row can be calculated by sysmem using
671 // |min_coded_width|, |bytes_per_row_divisor| and color format.
672 imageConstraints.min_bytes_per_row = 0;
673 imageConstraints.max_bytes_per_row = 0xffffffff;
674 imageConstraints.max_coded_width_times_coded_height = 0xffffffff;
675
676 imageConstraints.layers = 1;
677 imageConstraints.coded_width_divisor = 1;
678 imageConstraints.coded_height_divisor = 1;
679 imageConstraints.bytes_per_row_divisor = rowPitchAlignment;
680 imageConstraints.start_offset_divisor = 1;
681 imageConstraints.display_width_divisor = 1;
682 imageConstraints.display_height_divisor = 1;
683 imageConstraints.pixel_format.has_format_modifier = true;
684 imageConstraints.pixel_format.format_modifier.value =
685 (tiling == VK_IMAGE_TILING_LINEAR)
686 ? fuchsia_sysmem::wire::kFormatModifierLinear
687 : fuchsia_sysmem::wire::kFormatModifierGoogleGoldfishOptimal;
688
689 constraints->image_format_constraints[constraints->image_format_constraints_count++] =
690 imageConstraints;
691 return VK_SUCCESS;
692 }
693
694 SetBufferCollectionBufferConstraintsResult setBufferCollectionBufferConstraintsImpl(
695 fidl::WireSyncClient<fuchsia_sysmem::BufferCollection>* pCollection,
696 const VkBufferConstraintsInfoFUCHSIA* pBufferConstraintsInfo) {
697 const auto& collection = *pCollection;
698 if (pBufferConstraintsInfo == nullptr) {
699 mesa_loge(
700 "setBufferCollectionBufferConstraints: "
701 "pBufferConstraintsInfo cannot be null.");
702 return {VK_ERROR_OUT_OF_DEVICE_MEMORY};
703 }
704
705 fuchsia_sysmem::wire::BufferCollectionConstraints constraints =
706 defaultBufferCollectionConstraints(
707 /* min_size_bytes */ pBufferConstraintsInfo->createInfo.size,
708 /* buffer_count */ pBufferConstraintsInfo->bufferCollectionConstraints.minBufferCount);
709 constraints.usage.vulkan =
710 getBufferCollectionConstraintsVulkanBufferUsage(pBufferConstraintsInfo);
711
712 constexpr uint32_t kVulkanPriority = 5;
713 const char kName[] = "GoldfishBufferSysmemShared";
714 collection->SetName(kVulkanPriority, fidl::StringView(kName));
715
716 auto result = collection->SetConstraints(true, constraints);
717 if (!result.ok()) {
718 mesa_loge("setBufferCollectionConstraints: SetConstraints failed: %d", result.status());
719 return {VK_ERROR_OUT_OF_DEVICE_MEMORY};
720 }
721
722 return {VK_SUCCESS, constraints};
723 }
724 #endif
725
726 #ifdef VK_USE_PLATFORM_ANDROID_KHR
727 uint64_t ResourceTracker::getAHardwareBufferId(AHardwareBuffer* ahw) {
728 uint64_t id = 0;
729 mGralloc->getId(ahw, &id);
730 return id;
731 }
732 #endif
733
734 static void transformExternalResourceMemoryDedicatedRequirementsForGuest(
735 VkMemoryDedicatedRequirements* dedicatedReqs) {
736 dedicatedReqs->prefersDedicatedAllocation = VK_TRUE;
737 dedicatedReqs->requiresDedicatedAllocation = VK_TRUE;
738 }
739
740 void ResourceTracker::transformImageMemoryRequirementsForGuestLocked(VkImage image,
741 VkMemoryRequirements* reqs) {
742 #ifdef VK_USE_PLATFORM_FUCHSIA
743 auto it = info_VkImage.find(image);
744 if (it == info_VkImage.end()) return;
745 auto& info = it->second;
746 if (info.isSysmemBackedMemory) {
747 auto width = info.createInfo.extent.width;
748 auto height = info.createInfo.extent.height;
749 reqs->size = width * height * 4;
750 }
751 #else
752 // Bypass "unused parameter" checks.
753 (void)image;
754 (void)reqs;
755 #endif
756 }
757
758 CoherentMemoryPtr ResourceTracker::freeCoherentMemoryLocked(VkDeviceMemory memory,
759 VkDeviceMemory_Info& info) {
760 if (info.coherentMemory && info.ptr) {
761 if (info.coherentMemory->getDeviceMemory() != memory) {
762 delete_goldfish_VkDeviceMemory(memory);
763 }
764
765 info.coherentMemory->release(info.coherentMemoryOffset);
766 info.ptr = nullptr;
767
768 return std::move(info.coherentMemory);
769 }
770
771 return nullptr;
772 }
773
774 void ResourceTracker::EmitGuestAndHostTraceMarker(VkEncoder* encoder) {
775 #ifdef HAVE_PERFETTO
776 const uint64_t flowId = GeneratePseudoUniqueId();
777
778 TRACE_EVENT_INSTANT(
779 GFXSTREAM_TRACE_DEFAULT_CATEGORY,
780 "vkTraceAsyncGOOGLE",
781 perfetto::Flow::Global(flowId),
782 "flow id", flowId);
783
784 if (mCaps.vulkanCapset.hasTraceAsyncCommand) {
785 encoder->vkTraceAsyncGOOGLE(flowId, true /* do lock */);
786 }
787 #else
788 (void)encoder;
789 #endif // HAVE_PERFETTO
790 }
791
792 static VkResult acquireSync(uint64_t syncId, int64_t& osHandle) {
793 struct VirtGpuExecBuffer exec = {};
794 struct gfxstreamAcquireSync acquireSync = {};
795 VirtGpuDevice* instance = VirtGpuDevice::getInstance();
796
797 acquireSync.hdr.opCode = GFXSTREAM_ACQUIRE_SYNC;
798 acquireSync.syncId = syncId;
799
800 exec.command = static_cast<void*>(&acquireSync);
801 exec.command_size = sizeof(acquireSync);
802 exec.flags = kFenceOut | kRingIdx | kShareableOut;
803
804 if (instance->execBuffer(exec, nullptr)) return VK_ERROR_OUT_OF_HOST_MEMORY;
805
806 osHandle = exec.handle.osHandle;
807 return VK_SUCCESS;
808 }
809
810 static VkResult createFence(VkDevice device, uint64_t hostFenceHandle, int64_t& osHandle) {
811 struct VirtGpuExecBuffer exec = {};
812 struct gfxstreamCreateExportSyncVK exportSync = {};
813 VirtGpuDevice* instance = VirtGpuDevice::getInstance();
814
815 uint64_t hostDeviceHandle = get_host_u64_VkDevice(device);
816
817 exportSync.hdr.opCode = GFXSTREAM_CREATE_EXPORT_SYNC_VK;
818 exportSync.deviceHandleLo = (uint32_t)hostDeviceHandle;
819 exportSync.deviceHandleHi = (uint32_t)(hostDeviceHandle >> 32);
820 exportSync.fenceHandleLo = (uint32_t)hostFenceHandle;
821 exportSync.fenceHandleHi = (uint32_t)(hostFenceHandle >> 32);
822
823 exec.command = static_cast<void*>(&exportSync);
824 exec.command_size = sizeof(exportSync);
825 exec.flags = kFenceOut | kRingIdx;
826 if (instance->execBuffer(exec, nullptr)) return VK_ERROR_OUT_OF_HOST_MEMORY;
827
828 osHandle = exec.handle.osHandle;
829 return VK_SUCCESS;
830 }
831
832 static void collectAllPendingDescriptorSetsBottomUp(const std::vector<VkCommandBuffer>& workingSet,
833 std::unordered_set<VkDescriptorSet>& allDs) {
834 if (workingSet.empty()) return;
835
836 std::vector<VkCommandBuffer> nextLevel;
837 for (auto commandBuffer : workingSet) {
838 struct goldfish_VkCommandBuffer* cb = as_goldfish_VkCommandBuffer(commandBuffer);
839 forAllObjects(cb->subObjects, [&nextLevel](void* secondary) {
840 nextLevel.push_back((VkCommandBuffer)secondary);
841 });
842 }
843
844 collectAllPendingDescriptorSetsBottomUp(nextLevel, allDs);
845
846 for (auto cmdbuf : workingSet) {
847 struct goldfish_VkCommandBuffer* cb = as_goldfish_VkCommandBuffer(cmdbuf);
848
849 if (!cb->userPtr) {
850 continue; // No descriptors to update.
851 }
852
853 CommandBufferPendingDescriptorSets* pendingDescriptorSets =
854 (CommandBufferPendingDescriptorSets*)(cb->userPtr);
855
856 if (pendingDescriptorSets->sets.empty()) {
857 continue; // No descriptors to update.
858 }
859
860 allDs.insert(pendingDescriptorSets->sets.begin(), pendingDescriptorSets->sets.end());
861 }
862 }
863
864 static void commitDescriptorSetUpdates(void* context, VkQueue queue,
865 const std::unordered_set<VkDescriptorSet>& sets) {
866 VkEncoder* enc = (VkEncoder*)context;
867
868 std::unordered_map<VkDescriptorPool, uint32_t> poolSet;
869 std::vector<VkDescriptorPool> pools;
870 std::vector<VkDescriptorSetLayout> setLayouts;
871 std::vector<uint64_t> poolIds;
872 std::vector<uint32_t> descriptorSetWhichPool;
873 std::vector<uint32_t> pendingAllocations;
874 std::vector<uint32_t> writeStartingIndices;
875 std::vector<VkWriteDescriptorSet> writesForHost;
876
877 uint32_t poolIndex = 0;
878 uint32_t currentWriteIndex = 0;
879 for (auto set : sets) {
880 ReifiedDescriptorSet* reified = as_goldfish_VkDescriptorSet(set)->reified;
881 VkDescriptorPool pool = reified->pool;
882 VkDescriptorSetLayout setLayout = reified->setLayout;
883
884 auto it = poolSet.find(pool);
885 if (it == poolSet.end()) {
886 poolSet[pool] = poolIndex;
887 descriptorSetWhichPool.push_back(poolIndex);
888 pools.push_back(pool);
889 ++poolIndex;
890 } else {
891 uint32_t savedPoolIndex = it->second;
892 descriptorSetWhichPool.push_back(savedPoolIndex);
893 }
894
895 poolIds.push_back(reified->poolId);
896 setLayouts.push_back(setLayout);
897 pendingAllocations.push_back(reified->allocationPending ? 1 : 0);
898 writeStartingIndices.push_back(currentWriteIndex);
899
900 auto& writes = reified->allWrites;
901
902 for (size_t i = 0; i < writes.size(); ++i) {
903 uint32_t binding = i;
904
905 for (size_t j = 0; j < writes[i].size(); ++j) {
906 auto& write = writes[i][j];
907
908 if (write.type == DescriptorWriteType::Empty) continue;
909
910 uint32_t dstArrayElement = 0;
911
912 VkDescriptorImageInfo* imageInfo = nullptr;
913 VkDescriptorBufferInfo* bufferInfo = nullptr;
914 VkBufferView* bufferView = nullptr;
915
916 switch (write.type) {
917 case DescriptorWriteType::Empty:
918 break;
919 case DescriptorWriteType::ImageInfo:
920 dstArrayElement = j;
921 imageInfo = &write.imageInfo;
922 break;
923 case DescriptorWriteType::BufferInfo:
924 dstArrayElement = j;
925 bufferInfo = &write.bufferInfo;
926 break;
927 case DescriptorWriteType::BufferView:
928 dstArrayElement = j;
929 bufferView = &write.bufferView;
930 break;
931 case DescriptorWriteType::InlineUniformBlock:
932 case DescriptorWriteType::AccelerationStructure:
933 // TODO
934 mesa_loge(
935 "Encountered pending inline uniform block or acceleration structure "
936 "desc write, abort (NYI)\n");
937 abort();
938 default:
939 break;
940 }
941
942 // TODO: Combine multiple writes into one VkWriteDescriptorSet.
943 VkWriteDescriptorSet forHost = {
944 VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
945 0 /* TODO: inline uniform block */,
946 set,
947 binding,
948 dstArrayElement,
949 1,
950 write.descriptorType,
951 imageInfo,
952 bufferInfo,
953 bufferView,
954 };
955
956 writesForHost.push_back(forHost);
957 ++currentWriteIndex;
958
959 // Set it back to empty.
960 write.type = DescriptorWriteType::Empty;
961 }
962 }
963 }
964
965 // Skip out if there's nothing to VkWriteDescriptorSet home about.
966 if (writesForHost.empty()) {
967 return;
968 }
969
970 enc->vkQueueCommitDescriptorSetUpdatesGOOGLE(
971 queue, (uint32_t)pools.size(), pools.data(), (uint32_t)sets.size(), setLayouts.data(),
972 poolIds.data(), descriptorSetWhichPool.data(), pendingAllocations.data(),
973 writeStartingIndices.data(), (uint32_t)writesForHost.size(), writesForHost.data(),
974 false /* no lock */);
975
976 // If we got here, then we definitely serviced the allocations.
977 for (auto set : sets) {
978 ReifiedDescriptorSet* reified = as_goldfish_VkDescriptorSet(set)->reified;
979 reified->allocationPending = false;
980 }
981 }
982
983 uint32_t ResourceTracker::syncEncodersForCommandBuffer(VkCommandBuffer commandBuffer,
984 VkEncoder* currentEncoder) {
985 struct goldfish_VkCommandBuffer* cb = as_goldfish_VkCommandBuffer(commandBuffer);
986 if (!cb) return 0;
987
988 auto lastEncoder = cb->lastUsedEncoder;
989
990 if (lastEncoder == currentEncoder) return 0;
991
992 currentEncoder->incRef();
993
994 cb->lastUsedEncoder = currentEncoder;
995
996 if (!lastEncoder) return 0;
997
998 auto oldSeq = cb->sequenceNumber;
999 cb->sequenceNumber += 2;
1000 lastEncoder->vkCommandBufferHostSyncGOOGLE(commandBuffer, false, oldSeq + 1,
1001 true /* do lock */);
1002 lastEncoder->flush();
1003 currentEncoder->vkCommandBufferHostSyncGOOGLE(commandBuffer, true, oldSeq + 2,
1004 true /* do lock */);
1005
1006 if (lastEncoder->decRef()) {
1007 cb->lastUsedEncoder = nullptr;
1008 }
1009 return 0;
1010 }
1011
1012 static void addPendingDescriptorSets(VkCommandBuffer commandBuffer, uint32_t descriptorSetCount,
1013 const VkDescriptorSet* pDescriptorSets) {
1014 struct goldfish_VkCommandBuffer* cb = as_goldfish_VkCommandBuffer(commandBuffer);
1015
1016 if (!cb->userPtr) {
1017 CommandBufferPendingDescriptorSets* newPendingSets = new CommandBufferPendingDescriptorSets;
1018 cb->userPtr = newPendingSets;
1019 }
1020
1021 CommandBufferPendingDescriptorSets* pendingSets =
1022 (CommandBufferPendingDescriptorSets*)cb->userPtr;
1023
1024 for (uint32_t i = 0; i < descriptorSetCount; ++i) {
1025 pendingSets->sets.insert(pDescriptorSets[i]);
1026 }
1027 }
1028
1029 static void decDescriptorSetLayoutRef(void* context, VkDevice device,
1030 VkDescriptorSetLayout descriptorSetLayout,
1031 const VkAllocationCallbacks* pAllocator) {
1032 if (!descriptorSetLayout) return;
1033
1034 struct goldfish_VkDescriptorSetLayout* setLayout =
1035 as_goldfish_VkDescriptorSetLayout(descriptorSetLayout);
1036
1037 if (0 == --setLayout->layoutInfo->refcount) {
1038 VkEncoder* enc = (VkEncoder*)context;
1039 enc->vkDestroyDescriptorSetLayout(device, descriptorSetLayout, pAllocator,
1040 true /* do lock */);
1041 }
1042 }
1043
1044 void ResourceTracker::ensureSyncDeviceFd() {
1045 #if GFXSTREAM_ENABLE_GUEST_GOLDFISH
1046 if (mSyncDeviceFd >= 0) return;
1047 mSyncDeviceFd = goldfish_sync_open();
1048 if (mSyncDeviceFd >= 0) {
1049 mesa_logd("%s: created sync device for current Vulkan process: %d\n", __func__, mSyncDeviceFd);
1050 } else {
1051 mesa_logd("%s: failed to create sync device for current Vulkan process\n", __func__);
1052 }
1053 #endif
1054 }
1055
1056 void ResourceTracker::unregister_VkInstance(VkInstance instance) {
1057 std::lock_guard<std::recursive_mutex> lock(mLock);
1058
1059 auto it = info_VkInstance.find(instance);
1060 if (it == info_VkInstance.end()) return;
1061 auto info = it->second;
1062 info_VkInstance.erase(instance);
1063 }
1064
1065 void ResourceTracker::unregister_VkDevice(VkDevice device) {
1066 std::lock_guard<std::recursive_mutex> lock(mLock);
1067
1068 auto it = info_VkDevice.find(device);
1069 if (it == info_VkDevice.end()) return;
1070 auto info = it->second;
1071 info_VkDevice.erase(device);
1072 }
1073
1074 void ResourceTracker::unregister_VkCommandPool(VkCommandPool pool) {
1075 if (!pool) return;
1076
1077 clearCommandPool(pool);
1078
1079 std::lock_guard<std::recursive_mutex> lock(mLock);
1080 info_VkCommandPool.erase(pool);
1081 }
1082
1083 void ResourceTracker::unregister_VkSampler(VkSampler sampler) {
1084 if (!sampler) return;
1085
1086 std::lock_guard<std::recursive_mutex> lock(mLock);
1087 info_VkSampler.erase(sampler);
1088 }
1089
1090 void ResourceTracker::unregister_VkPrivateDataSlot(VkPrivateDataSlot privateSlot) {
1091 if (!privateSlot) return;
1092
1093 std::lock_guard<std::recursive_mutex> lock(mLock);
1094 info_VkPrivateDataSlot.erase(privateSlot);
1095 }
1096
1097 void ResourceTracker::unregister_VkCommandBuffer(VkCommandBuffer commandBuffer) {
1098 resetCommandBufferStagingInfo(commandBuffer, true /* also reset primaries */,
1099 true /* also clear pending descriptor sets */);
1100
1101 struct goldfish_VkCommandBuffer* cb = as_goldfish_VkCommandBuffer(commandBuffer);
1102 if (!cb) return;
1103 if (cb->lastUsedEncoder) {
1104 cb->lastUsedEncoder->decRef();
1105 }
1106 eraseObjects(&cb->subObjects);
1107 forAllObjects(cb->poolObjects, [cb](void* commandPool) {
1108 struct goldfish_VkCommandPool* p = as_goldfish_VkCommandPool((VkCommandPool)commandPool);
1109 eraseObject(&p->subObjects, (void*)cb);
1110 });
1111 eraseObjects(&cb->poolObjects);
1112
1113 if (cb->userPtr) {
1114 CommandBufferPendingDescriptorSets* pendingSets =
1115 (CommandBufferPendingDescriptorSets*)cb->userPtr;
1116 delete pendingSets;
1117 }
1118
1119 std::lock_guard<std::recursive_mutex> lock(mLock);
1120 info_VkCommandBuffer.erase(commandBuffer);
1121 }
1122
1123 void ResourceTracker::unregister_VkQueue(VkQueue queue) {
1124 struct goldfish_VkQueue* q = as_goldfish_VkQueue(queue);
1125 if (!q) return;
1126 if (q->lastUsedEncoder) {
1127 q->lastUsedEncoder->decRef();
1128 }
1129
1130 std::lock_guard<std::recursive_mutex> lock(mLock);
1131 info_VkQueue.erase(queue);
1132 }
1133
1134 void ResourceTracker::unregister_VkDeviceMemory(VkDeviceMemory mem) {
1135 std::lock_guard<std::recursive_mutex> lock(mLock);
1136
1137 auto it = info_VkDeviceMemory.find(mem);
1138 if (it == info_VkDeviceMemory.end()) return;
1139
1140 auto& memInfo = it->second;
1141
1142 #ifdef VK_USE_PLATFORM_ANDROID_KHR
1143 if (memInfo.ahw) {
1144 mGralloc->release(memInfo.ahw);
1145 }
1146 #endif
1147
1148 if (memInfo.vmoHandle != ZX_HANDLE_INVALID) {
1149 zx_handle_close(memInfo.vmoHandle);
1150 }
1151
1152 info_VkDeviceMemory.erase(mem);
1153 }
1154
1155 void ResourceTracker::unregister_VkImage(VkImage img) {
1156 std::lock_guard<std::recursive_mutex> lock(mLock);
1157
1158 auto it = info_VkImage.find(img);
1159 if (it == info_VkImage.end()) return;
1160
1161 info_VkImage.erase(img);
1162 }
1163
1164 void ResourceTracker::unregister_VkBuffer(VkBuffer buf) {
1165 std::lock_guard<std::recursive_mutex> lock(mLock);
1166
1167 auto it = info_VkBuffer.find(buf);
1168 if (it == info_VkBuffer.end()) return;
1169
1170 info_VkBuffer.erase(buf);
1171 }
1172
1173 void ResourceTracker::unregister_VkSemaphore(VkSemaphore sem) {
1174 std::lock_guard<std::recursive_mutex> lock(mLock);
1175
1176 auto it = info_VkSemaphore.find(sem);
1177 if (it == info_VkSemaphore.end()) return;
1178
1179 auto& semInfo = it->second;
1180
1181 if (semInfo.eventHandle != ZX_HANDLE_INVALID) {
1182 zx_handle_close(semInfo.eventHandle);
1183 }
1184
1185 #if defined(VK_USE_PLATFORM_ANDROID_KHR) || DETECT_OS_LINUX
1186 if (semInfo.syncFd.value_or(-1) >= 0) {
1187 mSyncHelper->close(semInfo.syncFd.value());
1188 }
1189 #endif
1190
1191 info_VkSemaphore.erase(sem);
1192 }
1193
1194 void ResourceTracker::unregister_VkDescriptorUpdateTemplate(VkDescriptorUpdateTemplate templ) {
1195 std::lock_guard<std::recursive_mutex> lock(mLock);
1196 auto it = info_VkDescriptorUpdateTemplate.find(templ);
1197 if (it == info_VkDescriptorUpdateTemplate.end()) return;
1198
1199 auto& info = it->second;
1200 if (info.templateEntryCount) delete[] info.templateEntries;
1201 if (info.imageInfoCount) {
1202 delete[] info.imageInfoIndices;
1203 delete[] info.imageInfos;
1204 }
1205 if (info.bufferInfoCount) {
1206 delete[] info.bufferInfoIndices;
1207 delete[] info.bufferInfos;
1208 }
1209 if (info.bufferViewCount) {
1210 delete[] info.bufferViewIndices;
1211 delete[] info.bufferViews;
1212 }
1213 info_VkDescriptorUpdateTemplate.erase(it);
1214 }
1215
1216 void ResourceTracker::unregister_VkFence(VkFence fence) {
1217 std::lock_guard<std::recursive_mutex> lock(mLock);
1218 auto it = info_VkFence.find(fence);
1219 if (it == info_VkFence.end()) return;
1220
1221 auto& fenceInfo = it->second;
1222 (void)fenceInfo;
1223
1224 #if defined(VK_USE_PLATFORM_ANDROID_KHR) || DETECT_OS_LINUX
1225 if (fenceInfo.syncFd && *fenceInfo.syncFd >= 0) {
1226 mSyncHelper->close(*fenceInfo.syncFd);
1227 }
1228 #endif
1229
1230 info_VkFence.erase(fence);
1231 }
1232
1233 #ifdef VK_USE_PLATFORM_FUCHSIA
1234 void ResourceTracker::unregister_VkBufferCollectionFUCHSIA(VkBufferCollectionFUCHSIA collection) {
1235 std::lock_guard<std::recursive_mutex> lock(mLock);
1236 info_VkBufferCollectionFUCHSIA.erase(collection);
1237 }
1238 #endif
1239
1240 void ResourceTracker::unregister_VkDescriptorSet_locked(VkDescriptorSet set) {
1241 struct goldfish_VkDescriptorSet* ds = as_goldfish_VkDescriptorSet(set);
1242 delete ds->reified;
1243 info_VkDescriptorSet.erase(set);
1244 }
1245
1246 void ResourceTracker::unregister_VkDescriptorSet(VkDescriptorSet set) {
1247 if (!set) return;
1248
1249 std::lock_guard<std::recursive_mutex> lock(mLock);
1250 unregister_VkDescriptorSet_locked(set);
1251 }
1252
1253 void ResourceTracker::unregister_VkDescriptorSetLayout(VkDescriptorSetLayout setLayout) {
1254 if (!setLayout) return;
1255
1256 std::lock_guard<std::recursive_mutex> lock(mLock);
1257 delete as_goldfish_VkDescriptorSetLayout(setLayout)->layoutInfo;
1258 info_VkDescriptorSetLayout.erase(setLayout);
1259 }
1260
1261 void ResourceTracker::freeDescriptorSetsIfHostAllocated(VkEncoder* enc, VkDevice device,
1262 uint32_t descriptorSetCount,
1263 const VkDescriptorSet* sets) {
1264 for (uint32_t i = 0; i < descriptorSetCount; ++i) {
1265 struct goldfish_VkDescriptorSet* ds = as_goldfish_VkDescriptorSet(sets[i]);
1266 if (ds->reified->allocationPending) {
1267 unregister_VkDescriptorSet(sets[i]);
1268 delete_goldfish_VkDescriptorSet(sets[i]);
1269 } else {
1270 enc->vkFreeDescriptorSets(device, ds->reified->pool, 1, &sets[i], false /* no lock */);
1271 }
1272 }
1273 }
1274
1275 void ResourceTracker::clearDescriptorPoolAndUnregisterDescriptorSets(void* context, VkDevice device,
1276 VkDescriptorPool pool) {
1277 std::vector<VkDescriptorSet> toClear =
1278 clearDescriptorPool(pool, mFeatureInfo.hasVulkanBatchedDescriptorSetUpdate);
1279
1280 for (auto set : toClear) {
1281 if (mFeatureInfo.hasVulkanBatchedDescriptorSetUpdate) {
1282 VkDescriptorSetLayout setLayout = as_goldfish_VkDescriptorSet(set)->reified->setLayout;
1283 decDescriptorSetLayoutRef(context, device, setLayout, nullptr);
1284 }
1285 unregister_VkDescriptorSet(set);
1286 delete_goldfish_VkDescriptorSet(set);
1287 }
1288 }
1289
1290 void ResourceTracker::unregister_VkDescriptorPool(VkDescriptorPool pool) {
1291 if (!pool) return;
1292
1293 std::lock_guard<std::recursive_mutex> lock(mLock);
1294
1295 struct goldfish_VkDescriptorPool* dp = as_goldfish_VkDescriptorPool(pool);
1296 delete dp->allocInfo;
1297
1298 info_VkDescriptorPool.erase(pool);
1299 }
1300
1301 void ResourceTracker::deviceMemoryTransform_fromhost(VkDeviceMemory* memory, uint32_t memoryCount,
1302 VkDeviceSize* offset, uint32_t offsetCount,
1303 VkDeviceSize* size, uint32_t sizeCount,
1304 uint32_t* typeIndex, uint32_t typeIndexCount,
1305 uint32_t* typeBits, uint32_t typeBitsCount) {
1306 (void)memory;
1307 (void)memoryCount;
1308 (void)offset;
1309 (void)offsetCount;
1310 (void)size;
1311 (void)sizeCount;
1312 (void)typeIndex;
1313 (void)typeIndexCount;
1314 (void)typeBits;
1315 (void)typeBitsCount;
1316 }
1317
1318 void ResourceTracker::transformImpl_VkExternalMemoryProperties_fromhost(
1319 VkExternalMemoryProperties* pProperties, uint32_t) {
1320 VkExternalMemoryHandleTypeFlags supportedHandleType = 0u;
1321 #ifdef VK_USE_PLATFORM_FUCHSIA
1322 supportedHandleType |= VK_EXTERNAL_MEMORY_HANDLE_TYPE_ZIRCON_VMO_BIT_FUCHSIA;
1323 #endif // VK_USE_PLATFORM_FUCHSIA
1324 #ifdef VK_USE_PLATFORM_ANDROID_KHR
1325 supportedHandleType |= VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID;
1326 #endif // VK_USE_PLATFORM_ANDROID_KHR
1327 if (supportedHandleType) {
1328 pProperties->compatibleHandleTypes &= supportedHandleType;
1329 pProperties->exportFromImportedHandleTypes &= supportedHandleType;
1330 }
1331 }
1332
1333 void ResourceTracker::setInstanceInfo(VkInstance instance, uint32_t enabledExtensionCount,
1334 const char* const* ppEnabledExtensionNames,
1335 uint32_t apiVersion) {
1336 std::lock_guard<std::recursive_mutex> lock(mLock);
1337 auto& info = info_VkInstance[instance];
1338 info.highestApiVersion = apiVersion;
1339 }
1340
1341 void ResourceTracker::setDeviceInfo(VkDevice device, VkPhysicalDevice physdev,
1342 VkPhysicalDeviceProperties props,
1343 VkPhysicalDeviceMemoryProperties memProps,
1344 uint32_t enabledExtensionCount,
1345 const char* const* ppEnabledExtensionNames, const void* pNext) {
1346 std::lock_guard<std::recursive_mutex> lock(mLock);
1347 auto& info = info_VkDevice[device];
1348 info.physdev = physdev;
1349 info.props = props;
1350 info.memProps = memProps;
1351 info.apiVersion = props.apiVersion;
1352
1353 const VkBaseInStructure* extensionCreateInfo =
1354 reinterpret_cast<const VkBaseInStructure*>(pNext);
1355 while (extensionCreateInfo) {
1356 if (extensionCreateInfo->sType ==
1357 VK_STRUCTURE_TYPE_DEVICE_DEVICE_MEMORY_REPORT_CREATE_INFO_EXT) {
1358 auto deviceMemoryReportCreateInfo =
1359 reinterpret_cast<const VkDeviceDeviceMemoryReportCreateInfoEXT*>(
1360 extensionCreateInfo);
1361 if (deviceMemoryReportCreateInfo->pfnUserCallback != nullptr) {
1362 info.deviceMemoryReportCallbacks.emplace_back(
1363 deviceMemoryReportCreateInfo->pfnUserCallback,
1364 deviceMemoryReportCreateInfo->pUserData);
1365 }
1366 }
1367 extensionCreateInfo = extensionCreateInfo->pNext;
1368 }
1369 }
1370
1371 void ResourceTracker::setDeviceMemoryInfo(VkDevice device, VkDeviceMemory memory,
1372 VkDeviceSize allocationSize, uint8_t* ptr,
1373 uint32_t memoryTypeIndex, void* ahw, bool imported,
1374 zx_handle_t vmoHandle, VirtGpuResourcePtr blobPtr,
1375 int importedFd) {
1376 std::lock_guard<std::recursive_mutex> lock(mLock);
1377 auto& info = info_VkDeviceMemory[memory];
1378
1379 info.device = device;
1380 info.allocationSize = allocationSize;
1381 info.ptr = ptr;
1382 info.memoryTypeIndex = memoryTypeIndex;
1383 #ifdef VK_USE_PLATFORM_ANDROID_KHR
1384 info.ahw = (AHardwareBuffer*)ahw;
1385 #endif
1386 info.imported = imported;
1387 info.vmoHandle = vmoHandle;
1388 info.blobPtr = blobPtr;
1389 info.importedFd = importedFd;
1390 }
1391
1392 void ResourceTracker::setImageInfo(VkImage image, VkDevice device,
1393 const VkImageCreateInfo* pCreateInfo) {
1394 std::lock_guard<std::recursive_mutex> lock(mLock);
1395 auto& info = info_VkImage[image];
1396
1397 info.device = device;
1398 info.createInfo = *pCreateInfo;
1399 }
1400
1401 uint8_t* ResourceTracker::getMappedPointer(VkDeviceMemory memory) {
1402 std::lock_guard<std::recursive_mutex> lock(mLock);
1403 const auto it = info_VkDeviceMemory.find(memory);
1404 if (it == info_VkDeviceMemory.end()) return nullptr;
1405
1406 const auto& info = it->second;
1407 return info.ptr;
1408 }
1409
1410 VkDeviceSize ResourceTracker::getMappedSize(VkDeviceMemory memory) {
1411 std::lock_guard<std::recursive_mutex> lock(mLock);
1412 const auto it = info_VkDeviceMemory.find(memory);
1413 if (it == info_VkDeviceMemory.end()) return 0;
1414
1415 const auto& info = it->second;
1416 return info.allocationSize;
1417 }
1418
1419 bool ResourceTracker::isValidMemoryRange(const VkMappedMemoryRange& range) {
1420 std::lock_guard<std::recursive_mutex> lock(mLock);
1421 const auto it = info_VkDeviceMemory.find(range.memory);
1422 if (it == info_VkDeviceMemory.end()) return false;
1423 const auto& info = it->second;
1424
1425 if (!info.ptr) return false;
1426
1427 VkDeviceSize offset = range.offset;
1428 VkDeviceSize size = range.size;
1429
1430 if (size == VK_WHOLE_SIZE) {
1431 return offset <= info.allocationSize;
1432 }
1433
1434 return offset + size <= info.allocationSize;
1435 }
1436
1437 void ResourceTracker::setupCaps(uint32_t& noRenderControlEnc) {
1438 VirtGpuDevice* instance = VirtGpuDevice::getInstance(kCapsetGfxStreamVulkan);
1439 if (instance) {
1440 mCaps = instance->getCaps();
1441 }
1442
1443 // Delete once goldfish Linux drivers are gone
1444 if (mCaps.vulkanCapset.protocolVersion == 0) {
1445 mCaps.vulkanCapset.colorBufferMemoryIndex = 0xFFFFFFFF;
1446 } else {
1447 // Don't query the render control encoder for features, since for virtio-gpu the
1448 // capabilities provide versioning. Set features to be unconditionally true, since
1449 // using virtio-gpu encompasses all prior goldfish features. mFeatureInfo should be
1450 // deprecated in favor of caps.
1451 mFeatureInfo.hasVulkanNullOptionalStrings = true;
1452 mFeatureInfo.hasVulkanIgnoredHandles = true;
1453 mFeatureInfo.hasVulkanShaderFloat16Int8 = true;
1454 mFeatureInfo.hasVulkanQueueSubmitWithCommands = true;
1455 mFeatureInfo.hasDeferredVulkanCommands = true;
1456 mFeatureInfo.hasVulkanAsyncQueueSubmit = true;
1457 mFeatureInfo.hasVulkanCreateResourcesWithRequirements = true;
1458 mFeatureInfo.hasVirtioGpuNext = true;
1459 mFeatureInfo.hasVirtioGpuNativeSync = true;
1460 mFeatureInfo.hasVulkanBatchedDescriptorSetUpdate =
1461 mCaps.vulkanCapset.vulkanBatchedDescriptorSetUpdate;
1462 mFeatureInfo.hasVulkanAsyncQsri = true;
1463
1464 ResourceTracker::streamFeatureBits |= VULKAN_STREAM_FEATURE_NULL_OPTIONAL_STRINGS_BIT;
1465 ResourceTracker::streamFeatureBits |= VULKAN_STREAM_FEATURE_IGNORED_HANDLES_BIT;
1466 ResourceTracker::streamFeatureBits |= VULKAN_STREAM_FEATURE_SHADER_FLOAT16_INT8_BIT;
1467 ResourceTracker::streamFeatureBits |= VULKAN_STREAM_FEATURE_QUEUE_SUBMIT_WITH_COMMANDS_BIT;
1468 }
1469
1470 noRenderControlEnc = mCaps.vulkanCapset.noRenderControlEnc;
1471 }
1472
1473 void ResourceTracker::setupFeatures(const struct GfxStreamVkFeatureInfo* features) {
1474 if (mFeatureInfo.setupComplete) {
1475 return;
1476 }
1477
1478 mFeatureInfo = *features;
1479 #if DETECT_OS_ANDROID
1480 if (mFeatureInfo.hasDirectMem) {
1481 mGoldfishAddressSpaceBlockProvider.reset(
1482 new GoldfishAddressSpaceBlockProvider(GoldfishAddressSpaceSubdeviceType::NoSubdevice));
1483 }
1484 #endif // DETECT_OS_ANDROID
1485
1486 #ifdef VK_USE_PLATFORM_FUCHSIA
1487 if (mFeatureInfo.hasVulkan) {
1488 fidl::ClientEnd<fuchsia_hardware_goldfish::ControlDevice> channel{zx::channel(
1489 GetConnectToServiceFunction()("/loader-gpu-devices/class/goldfish-control/000"))};
1490 if (!channel) {
1491 mesa_loge("failed to open control device");
1492 abort();
1493 }
1494 mControlDevice =
1495 fidl::WireSyncClient<fuchsia_hardware_goldfish::ControlDevice>(std::move(channel));
1496
1497 fidl::ClientEnd<fuchsia_sysmem::Allocator> sysmem_channel{
1498 zx::channel(GetConnectToServiceFunction()("/svc/fuchsia.sysmem.Allocator"))};
1499 if (!sysmem_channel) {
1500 mesa_loge("failed to open sysmem connection");
1501 }
1502 mSysmemAllocator =
1503 fidl::WireSyncClient<fuchsia_sysmem::Allocator>(std::move(sysmem_channel));
1504 char name[ZX_MAX_NAME_LEN] = {};
1505 zx_object_get_property(zx_process_self(), ZX_PROP_NAME, name, sizeof(name));
1506 std::string client_name(name);
1507 client_name += "-goldfish";
1508 zx_info_handle_basic_t info;
1509 zx_object_get_info(zx_process_self(), ZX_INFO_HANDLE_BASIC, &info, sizeof(info), nullptr,
1510 nullptr);
1511 mSysmemAllocator->SetDebugClientInfo(fidl::StringView::FromExternal(client_name),
1512 info.koid);
1513 }
1514 #endif
1515
1516 if (mFeatureInfo.hasVulkanNullOptionalStrings) {
1517 ResourceTracker::streamFeatureBits |= VULKAN_STREAM_FEATURE_NULL_OPTIONAL_STRINGS_BIT;
1518 }
1519 if (mFeatureInfo.hasVulkanIgnoredHandles) {
1520 ResourceTracker::streamFeatureBits |= VULKAN_STREAM_FEATURE_IGNORED_HANDLES_BIT;
1521 }
1522 if (mFeatureInfo.hasVulkanShaderFloat16Int8) {
1523 ResourceTracker::streamFeatureBits |= VULKAN_STREAM_FEATURE_SHADER_FLOAT16_INT8_BIT;
1524 }
1525 if (mFeatureInfo.hasVulkanQueueSubmitWithCommands) {
1526 ResourceTracker::streamFeatureBits |= VULKAN_STREAM_FEATURE_QUEUE_SUBMIT_WITH_COMMANDS_BIT;
1527 }
1528
1529 mFeatureInfo.setupComplete = true;
1530 }
1531
1532 void ResourceTracker::setupPlatformHelpers() {
1533 #if defined(VK_USE_PLATFORM_ANDROID_KHR)
1534 VirtGpuDevice* instance = VirtGpuDevice::getInstance(kCapsetGfxStreamVulkan);
1535 auto deviceHandle = instance ? instance->getDeviceHandle() : INVALID_DESCRIPTOR;
1536 if (mGralloc == nullptr) {
1537 mGralloc.reset(gfxstream::createPlatformGralloc(deviceHandle));
1538 }
1539 #endif
1540
1541 if (mSyncHelper == nullptr) {
1542 mSyncHelper.reset(gfxstream::createPlatformSyncHelper());
1543 }
1544 }
1545
1546 void ResourceTracker::setThreadingCallbacks(const ResourceTracker::ThreadingCallbacks& callbacks) {
1547 ResourceTracker::threadingCallbacks = callbacks;
1548 }
1549
1550 bool ResourceTracker::usingDirectMapping() const { return true; }
1551
1552 uint32_t ResourceTracker::getStreamFeatures() const { return ResourceTracker::streamFeatureBits; }
1553
1554 bool ResourceTracker::supportsDeferredCommands() const {
1555 return mFeatureInfo.hasDeferredVulkanCommands;
1556 }
1557
1558 bool ResourceTracker::supportsAsyncQueueSubmit() const {
1559 return mFeatureInfo.hasVulkanAsyncQueueSubmit;
1560 }
1561
1562 bool ResourceTracker::supportsCreateResourcesWithRequirements() const {
1563 return mFeatureInfo.hasVulkanCreateResourcesWithRequirements;
1564 }
1565
1566 int ResourceTracker::getHostInstanceExtensionIndex(const std::string& extName) const {
1567 int i = 0;
1568 for (const auto& prop : mHostInstanceExtensions) {
1569 if (extName == std::string(prop.extensionName)) {
1570 return i;
1571 }
1572 ++i;
1573 }
1574 return -1;
1575 }
1576
1577 int ResourceTracker::getHostDeviceExtensionIndex(const std::string& extName) const {
1578 int i = 0;
1579 for (const auto& prop : mHostDeviceExtensions) {
1580 if (extName == std::string(prop.extensionName)) {
1581 return i;
1582 }
1583 ++i;
1584 }
1585 return -1;
1586 }
1587
1588 void ResourceTracker::deviceMemoryTransform_tohost(VkDeviceMemory* memory, uint32_t memoryCount,
1589 VkDeviceSize* offset, uint32_t offsetCount,
1590 VkDeviceSize* size, uint32_t sizeCount,
1591 uint32_t* typeIndex, uint32_t typeIndexCount,
1592 uint32_t* typeBits, uint32_t typeBitsCount) {
1593 (void)memoryCount;
1594 (void)offsetCount;
1595 (void)sizeCount;
1596 (void)typeIndex;
1597 (void)typeIndexCount;
1598 (void)typeBits;
1599 (void)typeBitsCount;
1600
1601 if (memory) {
1602 std::lock_guard<std::recursive_mutex> lock(mLock);
1603
1604 for (uint32_t i = 0; i < memoryCount; ++i) {
1605 VkDeviceMemory mem = memory[i];
1606 if (!mem) {
1607 return;
1608 }
1609
1610 auto it = info_VkDeviceMemory.find(mem);
1611 if (it == info_VkDeviceMemory.end()) {
1612 mesa_logw("%s cannot find memory!", __func__);
1613 return;
1614 }
1615
1616 const auto& info = it->second;
1617
1618 if (!info.coherentMemory) continue;
1619
1620 memory[i] = info.coherentMemory->getDeviceMemory();
1621
1622 if (offset) {
1623 offset[i] = info.coherentMemoryOffset + offset[i];
1624 }
1625
1626 if (size && size[i] == VK_WHOLE_SIZE) {
1627 size[i] = info.allocationSize;
1628 }
1629 }
1630 }
1631 }
1632
1633 uint32_t ResourceTracker::getColorBufferMemoryIndex(void* context, VkDevice device) {
1634 // Create test image to get the memory requirements
1635 VkEncoder* enc = (VkEncoder*)context;
1636 VkImageCreateInfo createInfo = {
1637 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
1638 .imageType = VK_IMAGE_TYPE_2D,
1639 .format = VK_FORMAT_R8G8B8A8_UNORM,
1640 .extent = {64, 64, 1},
1641 .mipLevels = 1,
1642 .arrayLayers = 1,
1643 .samples = VK_SAMPLE_COUNT_1_BIT,
1644 .tiling = VK_IMAGE_TILING_OPTIMAL,
1645 .usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT |
1646 VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
1647 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT,
1648 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
1649 };
1650 VkImage image = VK_NULL_HANDLE;
1651 VkResult res = enc->vkCreateImage(device, &createInfo, nullptr, &image, true /* do lock */);
1652
1653 if (res != VK_SUCCESS) {
1654 return 0;
1655 }
1656
1657 VkMemoryRequirements memReqs;
1658 enc->vkGetImageMemoryRequirements(device, image, &memReqs, true /* do lock */);
1659 enc->vkDestroyImage(device, image, nullptr, true /* do lock */);
1660
1661 const VkPhysicalDeviceMemoryProperties& memProps =
1662 getPhysicalDeviceMemoryProperties(context, device, VK_NULL_HANDLE);
1663
1664 // Currently, host looks for the last index that has with memory
1665 // property type VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
1666 VkMemoryPropertyFlags memoryProperty = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
1667 for (int i = VK_MAX_MEMORY_TYPES - 1; i >= 0; --i) {
1668 if ((memReqs.memoryTypeBits & (1u << i)) &&
1669 (memProps.memoryTypes[i].propertyFlags & memoryProperty)) {
1670 return i;
1671 }
1672 }
1673
1674 return 0;
1675 }
1676
1677 VkResult ResourceTracker::on_vkEnumerateInstanceExtensionProperties(
1678 void* context, VkResult, const char*, uint32_t* pPropertyCount,
1679 VkExtensionProperties* pProperties) {
1680 std::vector<const char*> allowedExtensionNames = {
1681 "VK_KHR_get_physical_device_properties2",
1682 "VK_KHR_sampler_ycbcr_conversion",
1683 #if defined(VK_USE_PLATFORM_ANDROID_KHR) || DETECT_OS_LINUX
1684 "VK_KHR_external_semaphore_capabilities",
1685 "VK_KHR_external_memory_capabilities",
1686 "VK_KHR_external_fence_capabilities",
1687 "VK_EXT_debug_utils",
1688 #endif
1689 };
1690
1691 VkEncoder* enc = (VkEncoder*)context;
1692
1693 // Only advertise a select set of extensions.
1694 if (mHostInstanceExtensions.empty()) {
1695 uint32_t hostPropCount = 0;
1696 enc->vkEnumerateInstanceExtensionProperties(nullptr, &hostPropCount, nullptr,
1697 true /* do lock */);
1698 mHostInstanceExtensions.resize(hostPropCount);
1699
1700 VkResult hostRes = enc->vkEnumerateInstanceExtensionProperties(
1701 nullptr, &hostPropCount, mHostInstanceExtensions.data(), true /* do lock */);
1702
1703 if (hostRes != VK_SUCCESS) {
1704 return hostRes;
1705 }
1706 }
1707
1708 std::vector<VkExtensionProperties> filteredExts;
1709
1710 for (size_t i = 0; i < allowedExtensionNames.size(); ++i) {
1711 auto extIndex = getHostInstanceExtensionIndex(allowedExtensionNames[i]);
1712 if (extIndex != -1) {
1713 filteredExts.push_back(mHostInstanceExtensions[extIndex]);
1714 }
1715 }
1716
1717 VkExtensionProperties anbExtProps[] = {
1718 #ifdef VK_USE_PLATFORM_FUCHSIA
1719 {"VK_KHR_external_memory_capabilities", 1},
1720 {"VK_KHR_external_semaphore_capabilities", 1},
1721 #endif
1722 };
1723
1724 for (auto& anbExtProp : anbExtProps) {
1725 filteredExts.push_back(anbExtProp);
1726 }
1727
1728 // Spec:
1729 //
1730 // https://docs.vulkan.org/refpages/latest/refpages/source/vkEnumerateInstanceExtensionProperties.html
1731 //
1732 // If pProperties is NULL, then the number of extensions properties
1733 // available is returned in pPropertyCount. Otherwise, pPropertyCount
1734 // must point to a variable set by the user to the number of elements
1735 // in the pProperties array, and on return the variable is overwritten
1736 // with the number of structures actually written to pProperties. If
1737 // pPropertyCount is less than the number of extension properties
1738 // available, at most pPropertyCount structures will be written. If
1739 // pPropertyCount is smaller than the number of extensions available,
1740 // VK_INCOMPLETE will be returned instead of VK_SUCCESS, to indicate
1741 // that not all the available properties were returned.
1742 //
1743 // pPropertyCount must be a valid pointer to a uint32_t value
1744 if (!pPropertyCount) return VK_ERROR_INITIALIZATION_FAILED;
1745
1746 if (!pProperties) {
1747 *pPropertyCount = (uint32_t)filteredExts.size();
1748 return VK_SUCCESS;
1749 } else {
1750 auto actualExtensionCount = (uint32_t)filteredExts.size();
1751 if (*pPropertyCount > actualExtensionCount) {
1752 *pPropertyCount = actualExtensionCount;
1753 }
1754
1755 for (uint32_t i = 0; i < *pPropertyCount; ++i) {
1756 pProperties[i] = filteredExts[i];
1757 }
1758
1759 if (actualExtensionCount > *pPropertyCount) {
1760 return VK_INCOMPLETE;
1761 }
1762
1763 return VK_SUCCESS;
1764 }
1765 }
1766
1767 VkResult ResourceTracker::on_vkEnumerateDeviceExtensionProperties(
1768 void* context, VkResult, VkPhysicalDevice physdev, const char*, uint32_t* pPropertyCount,
1769 VkExtensionProperties* pProperties) {
1770 std::vector<const char*> allowedExtensionNames = {
1771 "VK_KHR_vulkan_memory_model",
1772 "VK_KHR_buffer_device_address",
1773 "VK_KHR_maintenance1",
1774 "VK_KHR_maintenance2",
1775 "VK_KHR_maintenance3",
1776 "VK_KHR_bind_memory2",
1777 "VK_KHR_dedicated_allocation",
1778 "VK_KHR_get_memory_requirements2",
1779 "VK_KHR_sampler_ycbcr_conversion",
1780 "VK_KHR_shader_float16_int8",
1781 "VK_KHR_timeline_semaphore",
1782 "VK_AMD_gpu_shader_half_float",
1783 "VK_NV_shader_subgroup_partitioned",
1784 "VK_KHR_shader_subgroup_extended_types",
1785 "VK_EXT_subgroup_size_control",
1786 "VK_EXT_provoking_vertex",
1787 "VK_KHR_line_rasterization",
1788 "VK_EXT_line_rasterization",
1789 "VK_KHR_shader_terminate_invocation",
1790 "VK_EXT_transform_feedback",
1791 "VK_EXT_primitive_topology_list_restart",
1792 "VK_EXT_index_type_uint8",
1793 "VK_EXT_load_store_op_none",
1794 "VK_EXT_swapchain_colorspace",
1795 "VK_EXT_image_robustness",
1796 "VK_EXT_custom_border_color",
1797 "VK_EXT_shader_stencil_export",
1798 "VK_KHR_image_format_list",
1799 "VK_KHR_incremental_present",
1800 "VK_KHR_pipeline_executable_properties",
1801 "VK_EXT_queue_family_foreign",
1802 "VK_EXT_scalar_block_layout",
1803 "VK_KHR_descriptor_update_template",
1804 "VK_KHR_storage_buffer_storage_class",
1805 "VK_EXT_depth_clip_enable",
1806 "VK_KHR_create_renderpass2",
1807 "VK_EXT_vertex_attribute_divisor",
1808 "VK_EXT_host_query_reset",
1809 "VK_EXT_blend_operation_advanced",
1810 "VK_EXT_frame_boundary",
1811 "VK_EXT_primitives_generated_query",
1812 #if defined(VK_USE_PLATFORM_ANDROID_KHR) || DETECT_OS_LINUX
1813 "VK_KHR_external_semaphore",
1814 "VK_KHR_external_semaphore_fd",
1815 "VK_KHR_external_memory",
1816 "VK_KHR_external_fence",
1817 "VK_KHR_external_fence_fd",
1818 "VK_EXT_device_memory_report",
1819 #endif
1820 #ifdef LINUX_GUEST_BUILD
1821 // Required by Zink
1822 "VK_KHR_imageless_framebuffer",
1823 // Passthrough if available on host. Will otherwise be emulated by guest
1824 "VK_EXT_image_drm_format_modifier",
1825 "VK_KHR_external_memory_fd",
1826 #endif
1827 // Vulkan 1.1
1828 // "VK_KHR_16bit_storage",
1829 "VK_KHR_device_group",
1830 "VK_KHR_device_group_creation",
1831 "VK_KHR_external_fence_capabilities",
1832 "VK_KHR_external_memory_capabilities",
1833 "VK_KHR_external_semaphore_capabilities",
1834 "VK_KHR_get_physical_device_properties2",
1835 "VK_KHR_relaxed_block_layout",
1836 "VK_KHR_shader_draw_parameters",
1837 "VK_KHR_storage_buffer_storage_class",
1838 "VK_KHR_variable_pointers",
1839 "VK_EXT_color_write_enable",
1840 "VK_EXT_memory_budget",
1841 "VK_KHR_multiview",
1842
1843 // Vulkan 1.2
1844 "VK_KHR_8bit_storage",
1845 "VK_KHR_depth_stencil_resolve",
1846 "VK_KHR_draw_indirect_count",
1847 "VK_KHR_driver_properties",
1848 "VK_KHR_imageless_framebuffer",
1849 "VK_KHR_sampler_mirror_clamp_to_edge",
1850 "VK_KHR_separate_depth_stencil_layouts",
1851 "VK_KHR_shader_atomic_int64",
1852 "VK_KHR_shader_float16_int8",
1853 "VK_KHR_shader_float_controls",
1854 "VK_KHR_spirv_1_4",
1855 "VK_KHR_uniform_buffer_standard_layout",
1856 "VK_EXT_descriptor_indexing",
1857 "VK_EXT_sampler_filter_minmax",
1858 "VK_EXT_scalar_block_layout",
1859 "VK_EXT_separate_stencil_usage",
1860 "VK_EXT_shader_viewport_index_layer",
1861
1862 // Vulkan 1.3
1863 "VK_KHR_synchronization2",
1864 "VK_EXT_private_data",
1865 "VK_KHR_dynamic_rendering",
1866 "VK_KHR_copy_commands2",
1867 "VK_KHR_format_feature_flags2",
1868 "VK_KHR_maintenance4",
1869 "VK_KHR_shader_integer_dot_product",
1870 "VK_KHR_shader_non_semantic_info",
1871 "VK_KHR_zero_initialize_workgroup_memory",
1872 "VK_EXT_4444_formats",
1873 "VK_EXT_extended_dynamic_state",
1874 "VK_EXT_extended_dynamic_state2",
1875 "VK_EXT_inline_uniform_block",
1876 "VK_EXT_pipeline_creation_cache_control",
1877 "VK_EXT_pipeline_creation_feedback",
1878 "VK_EXT_shader_demote_to_helper_invocation",
1879 "VK_EXT_texel_buffer_alignment",
1880 "VK_EXT_texture_compression_astc_hdr",
1881 "VK_EXT_tooling_info",
1882 "VK_EXT_ycbcr_2plane_444_formats",
1883 };
1884
1885 VkEncoder* enc = (VkEncoder*)context;
1886
1887 if (mHostDeviceExtensions.empty()) {
1888 uint32_t hostPropCount = 0;
1889 enc->vkEnumerateDeviceExtensionProperties(physdev, nullptr, &hostPropCount, nullptr,
1890 true /* do lock */);
1891 mHostDeviceExtensions.resize(hostPropCount);
1892
1893 VkResult hostRes = enc->vkEnumerateDeviceExtensionProperties(
1894 physdev, nullptr, &hostPropCount, mHostDeviceExtensions.data(), true /* do lock */);
1895
1896 if (hostRes != VK_SUCCESS) {
1897 return hostRes;
1898 }
1899 }
1900
1901 std::vector<VkExtensionProperties> filteredExts;
1902
1903 for (size_t i = 0; i < allowedExtensionNames.size(); ++i) {
1904 auto extIndex = getHostDeviceExtensionIndex(allowedExtensionNames[i]);
1905 if (extIndex != -1) {
1906 filteredExts.push_back(mHostDeviceExtensions[extIndex]);
1907 }
1908 }
1909
1910 VkExtensionProperties anbExtProps[] = {
1911 #ifdef VK_USE_PLATFORM_ANDROID_KHR
1912 {"VK_ANDROID_native_buffer", 7},
1913 #endif
1914 #ifdef VK_USE_PLATFORM_FUCHSIA
1915 {"VK_KHR_external_memory", 1},
1916 {"VK_KHR_external_semaphore", 1},
1917 {"VK_FUCHSIA_external_semaphore", 1},
1918 #endif
1919 };
1920
1921 for (auto& anbExtProp : anbExtProps) {
1922 filteredExts.push_back(anbExtProp);
1923 }
1924
1925 /*
1926 * GfxstreamEnd2EndVkTest::DeviceMemoryReport always assumes the memory report
1927 * extension is present. It's is filtered out when sent host side, since for a
1928 * virtual GPU this is quite difficult to implement.
1929 *
1930 * Mesa runtime checks physical device features. So if the test tries to enable
1931 * device level extension without it definitely existing, the test will fail.
1932 *
1933 * The test can also be modified to check VkPhysicalDeviceDeviceMemoryReportFeaturesEXT,
1934 * but that's more involved. Work around this by always advertising the extension.
1935 * Tracking bug: b/338270042
1936 */
1937 filteredExts.push_back(VkExtensionProperties{"VK_EXT_device_memory_report", 1});
1938
1939 #if defined(VK_USE_PLATFORM_ANDROID_KHR) || DETECT_OS_LINUX
1940 bool hostSupportsExternalFenceFd =
1941 getHostDeviceExtensionIndex("VK_KHR_external_fence_fd") != -1;
1942 if (!hostSupportsExternalFenceFd) {
1943 filteredExts.push_back(VkExtensionProperties{"VK_KHR_external_fence_fd", 1});
1944 }
1945 #endif
1946
1947 #if defined(VK_USE_PLATFORM_ANDROID_KHR) || DETECT_OS_LINUX
1948 bool hostHasPosixExternalSemaphore =
1949 getHostDeviceExtensionIndex("VK_KHR_external_semaphore_fd") != -1;
1950 if (!hostHasPosixExternalSemaphore) {
1951 // Always advertise posix external semaphore capabilities on Android/Linux.
1952 // SYNC_FD handles will always work, regardless of host support. Support
1953 // for non-sync, opaque FDs, depends on host driver support, but will
1954 // be handled accordingly by host.
1955 filteredExts.push_back(VkExtensionProperties{"VK_KHR_external_semaphore_fd", 1});
1956 }
1957 #endif
1958
1959 // TODO: query ExternalMemoryMode from vkEmulation directly to compansate for the host platform
1960 // correctly, instead of host extension checks, or make this support mandatory for Android.
1961 bool win32ExtMemAvailable = getHostDeviceExtensionIndex("VK_KHR_external_memory_win32") != -1;
1962 bool posixExtMemAvailable = getHostDeviceExtensionIndex("VK_KHR_external_memory_fd") != -1;
1963 bool metalExtMemAvailable = getHostDeviceExtensionIndex("VK_EXT_external_memory_metal") != -1;
1964 bool hostAllocationExtMemAvailable =
1965 getHostDeviceExtensionIndex("VK_EXT_external_memory_host") != -1;
1966 bool qnxExtMemAvailable =
1967 getHostDeviceExtensionIndex("VK_QNX_external_memory_screen_buffer") != -1;
1968
1969 bool hostHasExternalMemorySupport = win32ExtMemAvailable || posixExtMemAvailable ||
1970 metalExtMemAvailable || hostAllocationExtMemAvailable ||
1971 qnxExtMemAvailable;
1972
1973 if (hostHasExternalMemorySupport) {
1974 #ifdef VK_USE_PLATFORM_ANDROID_KHR
1975 filteredExts.push_back(
1976 VkExtensionProperties{"VK_ANDROID_external_memory_android_hardware_buffer", 7});
1977 #endif
1978 #ifdef VK_USE_PLATFORM_FUCHSIA
1979 filteredExts.push_back(VkExtensionProperties{"VK_FUCHSIA_external_memory", 1});
1980 filteredExts.push_back(VkExtensionProperties{"VK_FUCHSIA_buffer_collection", 1});
1981 #endif
1982 filteredExts.push_back(VkExtensionProperties{"VK_EXT_queue_family_foreign", 1});
1983 } else {
1984 #ifdef VK_USE_PLATFORM_ANDROID_KHR
1985 mesa_loge(
1986 "%s: Did not recognize any form of external memory support on the host device/driver. "
1987 "Guest won't support VK_ANDROID_external_memory_android_hardware_buffer!",
1988 __func__);
1989 #endif
1990 #ifdef LINUX_GUEST_BUILD
1991 // Note: Linux gfxstream-vulkan driver automatically assumes some form of external memory
1992 // support on the host, and advertises VK_KHR_external_memory_fd and
1993 // VK_EXT_external_memory_dma_buf unconditionally.
1994 mesa_logw(
1995 "%s: Did not recognize any form of external memory support on the host device/driver. "
1996 "This may result in some unexpected functionality, "
1997 "specifically when using external memory Vulkan extensions.",
1998 __func__);
1999 #endif
2000 }
2001
2002 // NOTE: the Vulkan Loader's trampoline functions will remove duplicates. This can lead
2003 // to lead errors if this function returns VK_SUCCESS with N elements (including a duplicate)
2004 // but the Vulkan Loader's trampoline function returns VK_INCOMPLETE with N-1 elements
2005 // (without the duplicate).
2006 std::sort(filteredExts.begin(),
2007 filteredExts.end(),
2008 [](const VkExtensionProperties& a,
2009 const VkExtensionProperties& b) {
2010 return strcmp(a.extensionName, b.extensionName) < 0;
2011 });
2012 filteredExts.erase(std::unique(filteredExts.begin(),
2013 filteredExts.end(),
2014 [](const VkExtensionProperties& a,
2015 const VkExtensionProperties& b) {
2016 return strcmp(a.extensionName, b.extensionName) == 0;
2017 }),
2018 filteredExts.end());
2019
2020 // Spec:
2021 //
2022 // https://docs.vulkan.org/refpages/latest/refpages/source/vkEnumerateDeviceExtensionProperties.html
2023 //
2024 // pPropertyCount is a pointer to an integer related to the number of
2025 // extension properties available or queried, and is treated in the
2026 // same fashion as the
2027 // vkEnumerateInstanceExtensionProperties::pPropertyCount parameter.
2028 //
2029 // https://docs.vulkan.org/refpages/latest/refpages/source/vkEnumerateInstanceExtensionProperties.html
2030 //
2031 // If pProperties is NULL, then the number of extensions properties
2032 // available is returned in pPropertyCount. Otherwise, pPropertyCount
2033 // must point to a variable set by the user to the number of elements
2034 // in the pProperties array, and on return the variable is overwritten
2035 // with the number of structures actually written to pProperties. If
2036 // pPropertyCount is less than the number of extension properties
2037 // available, at most pPropertyCount structures will be written. If
2038 // pPropertyCount is smaller than the number of extensions available,
2039 // VK_INCOMPLETE will be returned instead of VK_SUCCESS, to indicate
2040 // that not all the available properties were returned.
2041 //
2042 // pPropertyCount must be a valid pointer to a uint32_t value
2043
2044 if (!pPropertyCount) return VK_ERROR_INITIALIZATION_FAILED;
2045
2046 if (!pProperties) {
2047 *pPropertyCount = (uint32_t)filteredExts.size();
2048 return VK_SUCCESS;
2049 } else {
2050 auto actualExtensionCount = (uint32_t)filteredExts.size();
2051 if (*pPropertyCount > actualExtensionCount) {
2052 *pPropertyCount = actualExtensionCount;
2053 }
2054
2055 for (uint32_t i = 0; i < *pPropertyCount; ++i) {
2056 pProperties[i] = filteredExts[i];
2057 }
2058
2059 if (actualExtensionCount > *pPropertyCount) {
2060 return VK_INCOMPLETE;
2061 }
2062
2063 return VK_SUCCESS;
2064 }
2065 }
2066
2067 VkResult ResourceTracker::on_vkEnumeratePhysicalDevices(void* context, VkResult,
2068 VkInstance instance,
2069 uint32_t* pPhysicalDeviceCount,
2070 VkPhysicalDevice* pPhysicalDevices) {
2071 VkEncoder* enc = (VkEncoder*)context;
2072
2073 if (!instance) return VK_ERROR_INITIALIZATION_FAILED;
2074
2075 if (!pPhysicalDeviceCount) return VK_ERROR_INITIALIZATION_FAILED;
2076
2077 std::unique_lock<std::recursive_mutex> lock(mLock);
2078
2079 // When this function is called, we actually need to do two things:
2080 // - Get full information about physical devices from the host,
2081 // even if the guest did not ask for it
2082 // - Serve the guest query according to the spec:
2083 //
2084 // https://docs.vulkan.org/refpages/latest/refpages/source/vkEnumeratePhysicalDevices.html
2085
2086 auto it = info_VkInstance.find(instance);
2087
2088 if (it == info_VkInstance.end()) return VK_ERROR_INITIALIZATION_FAILED;
2089
2090 auto& info = it->second;
2091
2092 // Get the full host information here if it doesn't exist already.
2093 if (info.physicalDevices.empty()) {
2094 uint32_t hostPhysicalDeviceCount = 0;
2095
2096 lock.unlock();
2097 VkResult countRes = enc->vkEnumeratePhysicalDevices(instance, &hostPhysicalDeviceCount,
2098 nullptr, false /* no lock */);
2099 lock.lock();
2100
2101 if (countRes != VK_SUCCESS) {
2102 mesa_loge(
2103 "%s: failed: could not count host physical devices. "
2104 "Error %d\n",
2105 __func__, countRes);
2106 return countRes;
2107 }
2108
2109 info.physicalDevices.resize(hostPhysicalDeviceCount);
2110
2111 lock.unlock();
2112 VkResult enumRes = enc->vkEnumeratePhysicalDevices(
2113 instance, &hostPhysicalDeviceCount, info.physicalDevices.data(), false /* no lock */);
2114 lock.lock();
2115
2116 if (enumRes != VK_SUCCESS) {
2117 mesa_loge(
2118 "%s: failed: could not retrieve host physical devices. "
2119 "Error %d\n",
2120 __func__, enumRes);
2121 return enumRes;
2122 }
2123 }
2124
2125 // Serve the guest query according to the spec.
2126 //
2127 // https://docs.vulkan.org/refpages/latest/refpages/source/vkEnumeratePhysicalDevices.html
2128 //
2129 // If pPhysicalDevices is NULL, then the number of physical devices
2130 // available is returned in pPhysicalDeviceCount. Otherwise,
2131 // pPhysicalDeviceCount must point to a variable set by the user to the
2132 // number of elements in the pPhysicalDevices array, and on return the
2133 // variable is overwritten with the number of handles actually written
2134 // to pPhysicalDevices. If pPhysicalDeviceCount is less than the number
2135 // of physical devices available, at most pPhysicalDeviceCount
2136 // structures will be written. If pPhysicalDeviceCount is smaller than
2137 // the number of physical devices available, VK_INCOMPLETE will be
2138 // returned instead of VK_SUCCESS, to indicate that not all the
2139 // available physical devices were returned.
2140
2141 if (!pPhysicalDevices) {
2142 *pPhysicalDeviceCount = (uint32_t)info.physicalDevices.size();
2143 return VK_SUCCESS;
2144 } else {
2145 uint32_t actualDeviceCount = (uint32_t)info.physicalDevices.size();
2146 uint32_t toWrite =
2147 actualDeviceCount < *pPhysicalDeviceCount ? actualDeviceCount : *pPhysicalDeviceCount;
2148
2149 for (uint32_t i = 0; i < toWrite; ++i) {
2150 pPhysicalDevices[i] = info.physicalDevices[i];
2151 }
2152
2153 *pPhysicalDeviceCount = toWrite;
2154
2155 if (actualDeviceCount > *pPhysicalDeviceCount) {
2156 return VK_INCOMPLETE;
2157 }
2158
2159 return VK_SUCCESS;
2160 }
2161 }
2162
2163 void ResourceTracker::on_vkGetPhysicalDeviceFeatures2(void*, VkPhysicalDevice,
2164 VkPhysicalDeviceFeatures2* pFeatures) {
2165 if (pFeatures) {
2166 VkPhysicalDeviceDeviceMemoryReportFeaturesEXT* memoryReportFeaturesEXT =
2167 vk_find_struct(pFeatures, PHYSICAL_DEVICE_DEVICE_MEMORY_REPORT_FEATURES_EXT);
2168 if (memoryReportFeaturesEXT) {
2169 memoryReportFeaturesEXT->deviceMemoryReport = VK_TRUE;
2170 }
2171 }
2172 }
2173
2174 void ResourceTracker::on_vkGetPhysicalDeviceProperties2KHR(
2175 void* context, VkPhysicalDevice physicalDevice, VkPhysicalDeviceProperties2* pProperties) {
2176 on_vkGetPhysicalDeviceProperties2(context, physicalDevice, pProperties);
2177 }
2178
2179 void ResourceTracker::on_vkGetPhysicalDeviceProperties2(void* context,
2180 VkPhysicalDevice physicalDevice,
2181 VkPhysicalDeviceProperties2* pProperties) {
2182 VkEncoder* enc = (VkEncoder*)context;
2183 VirtGpuDevice* instance = VirtGpuDevice::getInstance();
2184 if (!pProperties) {
2185 return;
2186 }
2187
2188 void* pNextOriginal = pProperties->pNext;
2189 VkPhysicalDeviceProperties2 localProps = *pProperties;
2190
2191 if (vk_find_struct(&localProps, PHYSICAL_DEVICE_DRM_PROPERTIES_EXT)) {
2192 vk_filter_struct(&localProps, PHYSICAL_DEVICE_DRM_PROPERTIES_EXT);
2193 }
2194
2195 if (vk_find_struct(&localProps, PHYSICAL_DEVICE_PCI_BUS_INFO_PROPERTIES_EXT)) {
2196 vk_filter_struct(&localProps, PHYSICAL_DEVICE_PCI_BUS_INFO_PROPERTIES_EXT);
2197 }
2198
2199 if (vk_find_struct(&localProps, PHYSICAL_DEVICE_DRIVER_PROPERTIES)) {
2200 vk_filter_struct(&localProps, PHYSICAL_DEVICE_DRIVER_PROPERTIES);
2201 }
2202
2203 if (vk_find_struct(&localProps, PHYSICAL_DEVICE_ID_PROPERTIES)) {
2204 vk_filter_struct(&localProps, PHYSICAL_DEVICE_ID_PROPERTIES);
2205 }
2206
2207 #if defined(VK_USE_PLATFORM_ANDROID_KHR)
2208 if (vk_find_struct(&localProps, PHYSICAL_DEVICE_PRESENTATION_PROPERTIES_ANDROID)) {
2209 vk_filter_struct(&localProps, PHYSICAL_DEVICE_PRESENTATION_PROPERTIES_ANDROID);
2210 }
2211 #endif
2212
2213 enc->vkGetPhysicalDeviceProperties2(physicalDevice, &localProps, false /* no lock */);
2214
2215 *pProperties = localProps;
2216 pProperties->pNext = pNextOriginal;
2217
2218 if (pProperties->properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_CPU) {
2219 pProperties->properties.deviceType = VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU;
2220 }
2221
2222 pProperties->properties.driverVersion = vk_get_driver_version();
2223 VkPhysicalDeviceVulkan12Properties* vulkan12Props =
2224 vk_find_struct(pProperties, PHYSICAL_DEVICE_VULKAN_1_2_PROPERTIES);
2225 if (vulkan12Props) {
2226 snprintf(vulkan12Props->driverName, sizeof(vulkan12Props->driverName), "gfxstream");
2227 snprintf(vulkan12Props->driverInfo, sizeof(vulkan12Props->driverInfo),
2228 "Mesa " PACKAGE_VERSION MESA_GIT_SHA1);
2229 }
2230
2231 VkPhysicalDeviceDriverProperties* driverProps =
2232 vk_find_struct(pProperties, PHYSICAL_DEVICE_DRIVER_PROPERTIES);
2233 if (driverProps) {
2234 snprintf(driverProps->driverName, sizeof(driverProps->driverName), "gfxstream");
2235 snprintf(driverProps->driverInfo, sizeof(driverProps->driverInfo),
2236 "Mesa " PACKAGE_VERSION MESA_GIT_SHA1);
2237 }
2238
2239 const char* transport_name = instance ? "Virtio-GPU GFXStream" : "Goldfish GFXStream";
2240 char device_name[VK_MAX_PHYSICAL_DEVICE_NAME_SIZE];
2241 int device_name_len = snprintf(device_name, sizeof(device_name), "%s (%s)",
2242 transport_name, pProperties->properties.deviceName);
2243 if (device_name_len >= (int)VK_MAX_PHYSICAL_DEVICE_NAME_SIZE) {
2244 memcpy(device_name + VK_MAX_PHYSICAL_DEVICE_NAME_SIZE - 5, "...)", 4);
2245 device_name_len = VK_MAX_PHYSICAL_DEVICE_NAME_SIZE - 1;
2246 }
2247 memcpy(pProperties->properties.deviceName, device_name, device_name_len + 1);
2248
2249 VkPhysicalDeviceDrmPropertiesEXT* drmProps =
2250 vk_find_struct(pProperties, PHYSICAL_DEVICE_DRM_PROPERTIES_EXT);
2251 if (instance && drmProps) {
2252 VirtGpuDrmInfo drmInfo;
2253 if (instance->getDrmInfo(&drmInfo)) {
2254 drmProps->hasPrimary = drmInfo.hasPrimary;
2255 drmProps->hasRender = drmInfo.hasRender;
2256 drmProps->primaryMajor = drmInfo.primaryMajor;
2257 drmProps->primaryMinor = drmInfo.primaryMinor;
2258 drmProps->renderMajor = drmInfo.renderMajor;
2259 drmProps->renderMinor = drmInfo.renderMinor;
2260 } else {
2261 mesa_logd(
2262 "%s: encountered VkPhysicalDeviceDrmPropertiesEXT in pProperties::pNext chain, "
2263 "but failed to query DrmInfo from the VirtGpuDevice",
2264 __func__);
2265 }
2266 }
2267
2268 VkPhysicalDevicePCIBusInfoPropertiesEXT* pciBusInfoProps =
2269 vk_find_struct(pProperties, PHYSICAL_DEVICE_PCI_BUS_INFO_PROPERTIES_EXT);
2270 if (instance && pciBusInfoProps) {
2271 VirtGpuPciBusInfo pciBusInfo;
2272 if (instance->getPciBusInfo(&pciBusInfo)) {
2273 pciBusInfoProps->pciDomain = pciBusInfo.domain;
2274 pciBusInfoProps->pciBus = pciBusInfo.bus;
2275 pciBusInfoProps->pciDevice = pciBusInfo.device;
2276 pciBusInfoProps->pciFunction = pciBusInfo.function;
2277 } else {
2278 mesa_logd(
2279 "%s: encountered VkPhysicalDevicePCIBusInfoPropertiesEXT in pProperties::pNext "
2280 "chain, but failed to query PciBusInfo from the VirtGpuDevice",
2281 __func__);
2282 }
2283 }
2284 }
2285
2286 void ResourceTracker::on_vkGetPhysicalDeviceMemoryProperties(
2287 void* context, VkPhysicalDevice physicalDevice, VkPhysicalDeviceMemoryProperties* out) {
2288 // gfxstream decides which physical device to expose to the guest on startup.
2289 // Otherwise, we would need a physical device to properties mapping.
2290 *out = getPhysicalDeviceMemoryProperties(context, VK_NULL_HANDLE, physicalDevice);
2291 }
2292
2293 void ResourceTracker::on_vkGetPhysicalDeviceMemoryProperties2(
2294 void*, VkPhysicalDevice physdev, VkPhysicalDeviceMemoryProperties2* out) {
2295 on_vkGetPhysicalDeviceMemoryProperties(nullptr, physdev, &out->memoryProperties);
2296 }
2297
2298 VkResult ResourceTracker::on_vkCreateInstance(void* context, VkResult input_result,
2299 const VkInstanceCreateInfo* createInfo,
2300 const VkAllocationCallbacks*, VkInstance* pInstance) {
2301 if (input_result != VK_SUCCESS) return input_result;
2302
2303 VkEncoder* enc = (VkEncoder*)context;
2304
2305 uint32_t apiVersion;
2306 input_result = enc->vkEnumerateInstanceVersion(&apiVersion, false /* no lock */);
2307
2308 setInstanceInfo(*pInstance, createInfo->enabledExtensionCount,
2309 createInfo->ppEnabledExtensionNames, apiVersion);
2310
2311 return input_result;
2312 }
2313
2314 VkResult ResourceTracker::on_vkCreateDevice(void* context, VkResult input_result,
2315 VkPhysicalDevice physicalDevice,
2316 const VkDeviceCreateInfo* pCreateInfo,
2317 const VkAllocationCallbacks*, VkDevice* pDevice) {
2318 if (input_result != VK_SUCCESS) return input_result;
2319
2320 VkEncoder* enc = (VkEncoder*)context;
2321
2322 VkPhysicalDeviceProperties props;
2323 VkPhysicalDeviceMemoryProperties memProps;
2324 enc->vkGetPhysicalDeviceProperties(physicalDevice, &props, false /* no lock */);
2325 enc->vkGetPhysicalDeviceMemoryProperties(physicalDevice, &memProps, false /* no lock */);
2326
2327 setDeviceInfo(*pDevice, physicalDevice, props, memProps, pCreateInfo->enabledExtensionCount,
2328 pCreateInfo->ppEnabledExtensionNames, pCreateInfo->pNext);
2329
2330 return input_result;
2331 }
2332
2333 void ResourceTracker::on_vkDestroyDevice_pre(void* context, VkDevice device,
2334 const VkAllocationCallbacks*) {
2335 (void)context;
2336 std::lock_guard<std::recursive_mutex> lock(mLock);
2337
2338 auto it = info_VkDevice.find(device);
2339 if (it == info_VkDevice.end()) return;
2340
2341 for (auto itr = info_VkDeviceMemory.cbegin(); itr != info_VkDeviceMemory.cend();) {
2342 auto& memInfo = itr->second;
2343 if (memInfo.device == device) {
2344 itr = info_VkDeviceMemory.erase(itr);
2345 } else {
2346 itr++;
2347 }
2348 }
2349 }
2350
2351 #if defined(VK_USE_PLATFORM_ANDROID_KHR) || DETECT_OS_LINUX
2352 static void updateMemoryTypeBits(uint32_t* memoryTypeBits, uint32_t memoryIndex) {
2353 *memoryTypeBits = 1u << memoryIndex;
2354 }
2355 #endif
2356
2357 #ifdef VK_USE_PLATFORM_ANDROID_KHR
2358
2359 VkResult ResourceTracker::on_vkGetAndroidHardwareBufferPropertiesANDROID(
2360 void* context, VkResult, VkDevice device, const AHardwareBuffer* buffer,
2361 VkAndroidHardwareBufferPropertiesANDROID* pProperties) {
2362 // Delete once goldfish Linux drivers are gone
2363 if (mCaps.vulkanCapset.colorBufferMemoryIndex == 0xFFFFFFFF) {
2364 mCaps.vulkanCapset.colorBufferMemoryIndex = getColorBufferMemoryIndex(context, device);
2365 }
2366
2367 updateMemoryTypeBits(&pProperties->memoryTypeBits, mCaps.vulkanCapset.colorBufferMemoryIndex);
2368
2369 return getAndroidHardwareBufferPropertiesANDROID(mGralloc.get(), buffer, pProperties);
2370 }
2371
2372 VkResult ResourceTracker::on_vkGetMemoryAndroidHardwareBufferANDROID(
2373 void*, VkResult, VkDevice device, const VkMemoryGetAndroidHardwareBufferInfoANDROID* pInfo,
2374 struct AHardwareBuffer** pBuffer) {
2375 if (!pInfo) return VK_ERROR_INITIALIZATION_FAILED;
2376 if (!pInfo->memory) return VK_ERROR_INITIALIZATION_FAILED;
2377
2378 std::lock_guard<std::recursive_mutex> lock(mLock);
2379
2380 auto deviceIt = info_VkDevice.find(device);
2381
2382 if (deviceIt == info_VkDevice.end()) {
2383 return VK_ERROR_INITIALIZATION_FAILED;
2384 }
2385
2386 auto memoryIt = info_VkDeviceMemory.find(pInfo->memory);
2387
2388 if (memoryIt == info_VkDeviceMemory.end()) {
2389 return VK_ERROR_INITIALIZATION_FAILED;
2390 }
2391
2392 auto& info = memoryIt->second;
2393 VkResult queryRes = getMemoryAndroidHardwareBufferANDROID(mGralloc.get(), &info.ahw);
2394
2395 if (queryRes != VK_SUCCESS) return queryRes;
2396
2397 *pBuffer = info.ahw;
2398
2399 return queryRes;
2400 }
2401 #endif
2402
2403 #ifdef VK_USE_PLATFORM_FUCHSIA
2404 VkResult ResourceTracker::on_vkGetMemoryZirconHandleFUCHSIA(
2405 void*, VkResult, VkDevice device, const VkMemoryGetZirconHandleInfoFUCHSIA* pInfo,
2406 uint32_t* pHandle) {
2407 if (!pInfo) return VK_ERROR_INITIALIZATION_FAILED;
2408 if (!pInfo->memory) return VK_ERROR_INITIALIZATION_FAILED;
2409
2410 std::lock_guard<std::recursive_mutex> lock(mLock);
2411
2412 auto deviceIt = info_VkDevice.find(device);
2413
2414 if (deviceIt == info_VkDevice.end()) {
2415 return VK_ERROR_INITIALIZATION_FAILED;
2416 }
2417
2418 auto memoryIt = info_VkDeviceMemory.find(pInfo->memory);
2419
2420 if (memoryIt == info_VkDeviceMemory.end()) {
2421 return VK_ERROR_INITIALIZATION_FAILED;
2422 }
2423
2424 auto& info = memoryIt->second;
2425
2426 if (info.vmoHandle == ZX_HANDLE_INVALID) {
2427 mesa_loge("%s: memory cannot be exported", __func__);
2428 return VK_ERROR_INITIALIZATION_FAILED;
2429 }
2430
2431 *pHandle = ZX_HANDLE_INVALID;
2432 zx_handle_duplicate(info.vmoHandle, ZX_RIGHT_SAME_RIGHTS, pHandle);
2433 return VK_SUCCESS;
2434 }
2435
2436 VkResult ResourceTracker::on_vkGetMemoryZirconHandlePropertiesFUCHSIA(
2437 void*, VkResult, VkDevice device, VkExternalMemoryHandleTypeFlagBits handleType,
2438 uint32_t handle, VkMemoryZirconHandlePropertiesFUCHSIA* pProperties) {
2439 using fuchsia_hardware_goldfish::wire::kMemoryPropertyDeviceLocal;
2440 using fuchsia_hardware_goldfish::wire::kMemoryPropertyHostVisible;
2441
2442 if (handleType != VK_EXTERNAL_MEMORY_HANDLE_TYPE_ZIRCON_VMO_BIT_FUCHSIA) {
2443 return VK_ERROR_INITIALIZATION_FAILED;
2444 }
2445
2446 zx_info_handle_basic_t handleInfo;
2447 zx_status_t status = zx::unowned_vmo(handle)->get_info(ZX_INFO_HANDLE_BASIC, &handleInfo,
2448 sizeof(handleInfo), nullptr, nullptr);
2449 if (status != ZX_OK || handleInfo.type != ZX_OBJ_TYPE_VMO) {
2450 return VK_ERROR_INVALID_EXTERNAL_HANDLE;
2451 }
2452
2453 std::lock_guard<std::recursive_mutex> lock(mLock);
2454
2455 auto deviceIt = info_VkDevice.find(device);
2456
2457 if (deviceIt == info_VkDevice.end()) {
2458 return VK_ERROR_INITIALIZATION_FAILED;
2459 }
2460
2461 auto& info = deviceIt->second;
2462
2463 zx::vmo vmo_dup;
2464 status = zx::unowned_vmo(handle)->duplicate(ZX_RIGHT_SAME_RIGHTS, &vmo_dup);
2465 if (status != ZX_OK) {
2466 mesa_loge("zx_handle_duplicate() error: %d", status);
2467 return VK_ERROR_INITIALIZATION_FAILED;
2468 }
2469
2470 uint32_t memoryProperty = 0u;
2471
2472 auto result = mControlDevice->GetBufferHandleInfo(std::move(vmo_dup));
2473 if (!result.ok()) {
2474 mesa_loge("mControlDevice->GetBufferHandleInfo fatal error: epitaph: %d", result.status());
2475 return VK_ERROR_INITIALIZATION_FAILED;
2476 }
2477 if (result.value().is_ok()) {
2478 memoryProperty = result.value().value()->info.memory_property();
2479 } else if (result.value().error_value() == ZX_ERR_NOT_FOUND) {
2480 // If a VMO is allocated while ColorBuffer/Buffer is not created,
2481 // it must be a device-local buffer, since for host-visible buffers,
2482 // ColorBuffer/Buffer is created at sysmem allocation time.
2483 memoryProperty = kMemoryPropertyDeviceLocal;
2484 } else {
2485 // Importing read-only host memory into the Vulkan driver should not
2486 // work, but it is not an error to try to do so. Returning a
2487 // VkMemoryZirconHandlePropertiesFUCHSIA with no available
2488 // memoryType bits should be enough for clients. See fxbug.dev/42098398
2489 // for other issues this this flow.
2490 mesa_logw("GetBufferHandleInfo failed: %d", result.value().error_value());
2491 pProperties->memoryTypeBits = 0;
2492 return VK_SUCCESS;
2493 }
2494
2495 pProperties->memoryTypeBits = 0;
2496 for (uint32_t i = 0; i < info.memProps.memoryTypeCount; ++i) {
2497 if (((memoryProperty & kMemoryPropertyDeviceLocal) &&
2498 (info.memProps.memoryTypes[i].propertyFlags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT)) ||
2499 ((memoryProperty & kMemoryPropertyHostVisible) &&
2500 (info.memProps.memoryTypes[i].propertyFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT))) {
2501 pProperties->memoryTypeBits |= 1ull << i;
2502 }
2503 }
2504 return VK_SUCCESS;
2505 }
2506
2507 zx_koid_t getEventKoid(zx_handle_t eventHandle) {
2508 if (eventHandle == ZX_HANDLE_INVALID) {
2509 return ZX_KOID_INVALID;
2510 }
2511
2512 zx_info_handle_basic_t info;
2513 zx_status_t status = zx_object_get_info(eventHandle, ZX_INFO_HANDLE_BASIC, &info, sizeof(info),
2514 nullptr, nullptr);
2515 if (status != ZX_OK) {
2516 mesa_loge("Cannot get object info of handle %u: %d", eventHandle, status);
2517 return ZX_KOID_INVALID;
2518 }
2519 return info.koid;
2520 }
2521
2522 VkResult ResourceTracker::on_vkImportSemaphoreZirconHandleFUCHSIA(
2523 void*, VkResult, VkDevice device, const VkImportSemaphoreZirconHandleInfoFUCHSIA* pInfo) {
2524 if (!pInfo) return VK_ERROR_INITIALIZATION_FAILED;
2525 if (!pInfo->semaphore) return VK_ERROR_INITIALIZATION_FAILED;
2526
2527 std::lock_guard<std::recursive_mutex> lock(mLock);
2528
2529 auto deviceIt = info_VkDevice.find(device);
2530
2531 if (deviceIt == info_VkDevice.end()) {
2532 return VK_ERROR_INITIALIZATION_FAILED;
2533 }
2534
2535 auto semaphoreIt = info_VkSemaphore.find(pInfo->semaphore);
2536
2537 if (semaphoreIt == info_VkSemaphore.end()) {
2538 return VK_ERROR_INITIALIZATION_FAILED;
2539 }
2540
2541 auto& info = semaphoreIt->second;
2542
2543 if (info.eventHandle != ZX_HANDLE_INVALID) {
2544 zx_handle_close(info.eventHandle);
2545 }
2546 #if VK_HEADER_VERSION < 174
2547 info.eventHandle = pInfo->handle;
2548 #else // VK_HEADER_VERSION >= 174
2549 info.eventHandle = pInfo->zirconHandle;
2550 #endif // VK_HEADER_VERSION < 174
2551 if (info.eventHandle != ZX_HANDLE_INVALID) {
2552 info.eventKoid = getEventKoid(info.eventHandle);
2553 }
2554
2555 return VK_SUCCESS;
2556 }
2557
2558 VkResult ResourceTracker::on_vkGetSemaphoreZirconHandleFUCHSIA(
2559 void*, VkResult, VkDevice device, const VkSemaphoreGetZirconHandleInfoFUCHSIA* pInfo,
2560 uint32_t* pHandle) {
2561 if (!pInfo) return VK_ERROR_INITIALIZATION_FAILED;
2562 if (!pInfo->semaphore) return VK_ERROR_INITIALIZATION_FAILED;
2563
2564 std::lock_guard<std::recursive_mutex> lock(mLock);
2565
2566 auto deviceIt = info_VkDevice.find(device);
2567
2568 if (deviceIt == info_VkDevice.end()) {
2569 return VK_ERROR_INITIALIZATION_FAILED;
2570 }
2571
2572 auto semaphoreIt = info_VkSemaphore.find(pInfo->semaphore);
2573
2574 if (semaphoreIt == info_VkSemaphore.end()) {
2575 return VK_ERROR_INITIALIZATION_FAILED;
2576 }
2577
2578 auto& info = semaphoreIt->second;
2579
2580 if (info.eventHandle == ZX_HANDLE_INVALID) {
2581 return VK_ERROR_INITIALIZATION_FAILED;
2582 }
2583
2584 *pHandle = ZX_HANDLE_INVALID;
2585 zx_handle_duplicate(info.eventHandle, ZX_RIGHT_SAME_RIGHTS, pHandle);
2586 return VK_SUCCESS;
2587 }
2588
2589 VkResult ResourceTracker::on_vkCreateBufferCollectionFUCHSIA(
2590 void*, VkResult, VkDevice, const VkBufferCollectionCreateInfoFUCHSIA* pInfo,
2591 const VkAllocationCallbacks*, VkBufferCollectionFUCHSIA* pCollection) {
2592 fidl::ClientEnd<::fuchsia_sysmem::BufferCollectionToken> token_client;
2593
2594 if (pInfo->collectionToken) {
2595 token_client = fidl::ClientEnd<::fuchsia_sysmem::BufferCollectionToken>(
2596 zx::channel(pInfo->collectionToken));
2597 } else {
2598 auto endpoints = fidl::CreateEndpoints<::fuchsia_sysmem::BufferCollectionToken>();
2599 if (!endpoints.is_ok()) {
2600 mesa_loge("zx_channel_create failed: %d", endpoints.status_value());
2601 return VK_ERROR_INITIALIZATION_FAILED;
2602 }
2603
2604 auto result = mSysmemAllocator->AllocateSharedCollection(std::move(endpoints->server));
2605 if (!result.ok()) {
2606 mesa_loge("AllocateSharedCollection failed: %d", result.status());
2607 return VK_ERROR_INITIALIZATION_FAILED;
2608 }
2609 token_client = std::move(endpoints->client);
2610 }
2611
2612 auto endpoints = fidl::CreateEndpoints<::fuchsia_sysmem::BufferCollection>();
2613 if (!endpoints.is_ok()) {
2614 mesa_loge("zx_channel_create failed: %d", endpoints.status_value());
2615 return VK_ERROR_INITIALIZATION_FAILED;
2616 }
2617 auto [collection_client, collection_server] = std::move(endpoints.value());
2618
2619 auto result = mSysmemAllocator->BindSharedCollection(std::move(token_client),
2620 std::move(collection_server));
2621 if (!result.ok()) {
2622 mesa_loge("BindSharedCollection failed: %d", result.status());
2623 return VK_ERROR_INITIALIZATION_FAILED;
2624 }
2625
2626 auto* sysmem_collection =
2627 new fidl::WireSyncClient<fuchsia_sysmem::BufferCollection>(std::move(collection_client));
2628 *pCollection = reinterpret_cast<VkBufferCollectionFUCHSIA>(sysmem_collection);
2629
2630 register_VkBufferCollectionFUCHSIA(*pCollection);
2631 return VK_SUCCESS;
2632 }
2633
2634 void ResourceTracker::on_vkDestroyBufferCollectionFUCHSIA(void*, VkResult, VkDevice,
2635 VkBufferCollectionFUCHSIA collection,
2636 const VkAllocationCallbacks*) {
2637 auto sysmem_collection =
2638 reinterpret_cast<fidl::WireSyncClient<fuchsia_sysmem::BufferCollection>*>(collection);
2639 if (sysmem_collection) {
2640 (*sysmem_collection)->Close();
2641 }
2642 delete sysmem_collection;
2643
2644 unregister_VkBufferCollectionFUCHSIA(collection);
2645 }
2646
2647 SetBufferCollectionImageConstraintsResult ResourceTracker::setBufferCollectionImageConstraintsImpl(
2648 VkEncoder* enc, VkDevice device,
2649 fidl::WireSyncClient<fuchsia_sysmem::BufferCollection>* pCollection,
2650 const VkImageConstraintsInfoFUCHSIA* pImageConstraintsInfo) {
2651 const auto& collection = *pCollection;
2652 if (!pImageConstraintsInfo ||
2653 pImageConstraintsInfo->sType != VK_STRUCTURE_TYPE_IMAGE_CONSTRAINTS_INFO_FUCHSIA) {
2654 mesa_loge("%s: invalid pImageConstraintsInfo", __func__);
2655 return {VK_ERROR_INITIALIZATION_FAILED};
2656 }
2657
2658 if (pImageConstraintsInfo->formatConstraintsCount == 0) {
2659 mesa_loge("%s: formatConstraintsCount must be greater than 0", __func__);
2660 abort();
2661 }
2662
2663 fuchsia_sysmem::wire::BufferCollectionConstraints constraints =
2664 defaultBufferCollectionConstraints(
2665 /* min_size_bytes */ 0,
2666 pImageConstraintsInfo->bufferCollectionConstraints.minBufferCount,
2667 pImageConstraintsInfo->bufferCollectionConstraints.maxBufferCount,
2668 pImageConstraintsInfo->bufferCollectionConstraints.minBufferCountForCamping,
2669 pImageConstraintsInfo->bufferCollectionConstraints.minBufferCountForDedicatedSlack,
2670 pImageConstraintsInfo->bufferCollectionConstraints.minBufferCountForSharedSlack);
2671
2672 std::vector<fuchsia_sysmem::wire::ImageFormatConstraints> format_constraints;
2673
2674 VkPhysicalDevice physicalDevice;
2675 {
2676 std::lock_guard<std::recursive_mutex> lock(mLock);
2677 auto deviceIt = info_VkDevice.find(device);
2678 if (deviceIt == info_VkDevice.end()) {
2679 return {VK_ERROR_INITIALIZATION_FAILED};
2680 }
2681 physicalDevice = deviceIt->second.physdev;
2682 }
2683
2684 std::vector<uint32_t> createInfoIndex;
2685
2686 bool hasOptimalTiling = false;
2687 for (uint32_t i = 0; i < pImageConstraintsInfo->formatConstraintsCount; i++) {
2688 const VkImageCreateInfo* createInfo =
2689 &pImageConstraintsInfo->pFormatConstraints[i].imageCreateInfo;
2690 const VkImageFormatConstraintsInfoFUCHSIA* formatConstraints =
2691 &pImageConstraintsInfo->pFormatConstraints[i];
2692
2693 // add ImageFormatConstraints for *optimal* tiling
2694 VkResult optimalResult = VK_ERROR_FORMAT_NOT_SUPPORTED;
2695 if (createInfo->tiling == VK_IMAGE_TILING_OPTIMAL) {
2696 optimalResult = addImageBufferCollectionConstraintsFUCHSIA(
2697 enc, device, physicalDevice, formatConstraints, VK_IMAGE_TILING_OPTIMAL,
2698 &constraints);
2699 if (optimalResult == VK_SUCCESS) {
2700 createInfoIndex.push_back(i);
2701 hasOptimalTiling = true;
2702 }
2703 }
2704
2705 // Add ImageFormatConstraints for *linear* tiling
2706 VkResult linearResult = addImageBufferCollectionConstraintsFUCHSIA(
2707 enc, device, physicalDevice, formatConstraints, VK_IMAGE_TILING_LINEAR, &constraints);
2708 if (linearResult == VK_SUCCESS) {
2709 createInfoIndex.push_back(i);
2710 }
2711
2712 // Update usage and BufferMemoryConstraints
2713 if (linearResult == VK_SUCCESS || optimalResult == VK_SUCCESS) {
2714 constraints.usage.vulkan |= getBufferCollectionConstraintsVulkanImageUsage(createInfo);
2715
2716 if (formatConstraints && formatConstraints->flags) {
2717 mesa_logw(
2718 "%s: Non-zero flags (%08x) in image format "
2719 "constraints; this is currently not supported, see "
2720 "fxbug.dev/42147900.",
2721 __func__, formatConstraints->flags);
2722 }
2723 }
2724 }
2725
2726 // Set buffer memory constraints based on optimal/linear tiling support
2727 // and flags.
2728 VkImageConstraintsInfoFlagsFUCHSIA flags = pImageConstraintsInfo->flags;
2729 if (flags & VK_IMAGE_CONSTRAINTS_INFO_CPU_READ_RARELY_FUCHSIA)
2730 constraints.usage.cpu |= fuchsia_sysmem::wire::kCpuUsageRead;
2731 if (flags & VK_IMAGE_CONSTRAINTS_INFO_CPU_READ_OFTEN_FUCHSIA)
2732 constraints.usage.cpu |= fuchsia_sysmem::wire::kCpuUsageReadOften;
2733 if (flags & VK_IMAGE_CONSTRAINTS_INFO_CPU_WRITE_RARELY_FUCHSIA)
2734 constraints.usage.cpu |= fuchsia_sysmem::wire::kCpuUsageWrite;
2735 if (flags & VK_IMAGE_CONSTRAINTS_INFO_CPU_WRITE_OFTEN_FUCHSIA)
2736 constraints.usage.cpu |= fuchsia_sysmem::wire::kCpuUsageWriteOften;
2737
2738 constraints.has_buffer_memory_constraints = true;
2739 auto& memory_constraints = constraints.buffer_memory_constraints;
2740 memory_constraints.cpu_domain_supported = true;
2741 memory_constraints.ram_domain_supported = true;
2742 memory_constraints.inaccessible_domain_supported =
2743 hasOptimalTiling && !(flags & (VK_IMAGE_CONSTRAINTS_INFO_CPU_READ_RARELY_FUCHSIA |
2744 VK_IMAGE_CONSTRAINTS_INFO_CPU_READ_OFTEN_FUCHSIA |
2745 VK_IMAGE_CONSTRAINTS_INFO_CPU_WRITE_RARELY_FUCHSIA |
2746 VK_IMAGE_CONSTRAINTS_INFO_CPU_WRITE_OFTEN_FUCHSIA));
2747
2748 if (memory_constraints.inaccessible_domain_supported) {
2749 memory_constraints.heap_permitted_count = 2;
2750 memory_constraints.heap_permitted[0] = fuchsia_sysmem::wire::HeapType::kGoldfishDeviceLocal;
2751 memory_constraints.heap_permitted[1] = fuchsia_sysmem::wire::HeapType::kGoldfishHostVisible;
2752 } else {
2753 memory_constraints.heap_permitted_count = 1;
2754 memory_constraints.heap_permitted[0] = fuchsia_sysmem::wire::HeapType::kGoldfishHostVisible;
2755 }
2756
2757 if (constraints.image_format_constraints_count == 0) {
2758 mesa_loge("%s: none of the specified formats is supported by device", __func__);
2759 return {VK_ERROR_FORMAT_NOT_SUPPORTED};
2760 }
2761
2762 constexpr uint32_t kVulkanPriority = 5;
2763 const char kName[] = "GoldfishSysmemShared";
2764 collection->SetName(kVulkanPriority, fidl::StringView(kName));
2765
2766 auto result = collection->SetConstraints(true, constraints);
2767 if (!result.ok()) {
2768 mesa_loge("setBufferCollectionConstraints: SetConstraints failed: %d", result.status());
2769 return {VK_ERROR_INITIALIZATION_FAILED};
2770 }
2771
2772 return {VK_SUCCESS, constraints, std::move(createInfoIndex)};
2773 }
2774
2775 VkResult ResourceTracker::setBufferCollectionImageConstraintsFUCHSIA(
2776 VkEncoder* enc, VkDevice device,
2777 fidl::WireSyncClient<fuchsia_sysmem::BufferCollection>* pCollection,
2778 const VkImageConstraintsInfoFUCHSIA* pImageConstraintsInfo) {
2779 const auto& collection = *pCollection;
2780
2781 auto setConstraintsResult =
2782 setBufferCollectionImageConstraintsImpl(enc, device, pCollection, pImageConstraintsInfo);
2783 if (setConstraintsResult.result != VK_SUCCESS) {
2784 return setConstraintsResult.result;
2785 }
2786
2787 // copy constraints to info_VkBufferCollectionFUCHSIA if
2788 // |collection| is a valid VkBufferCollectionFUCHSIA handle.
2789 std::lock_guard<std::recursive_mutex> lock(mLock);
2790 VkBufferCollectionFUCHSIA buffer_collection =
2791 reinterpret_cast<VkBufferCollectionFUCHSIA>(pCollection);
2792 if (info_VkBufferCollectionFUCHSIA.find(buffer_collection) !=
2793 info_VkBufferCollectionFUCHSIA.end()) {
2794 info_VkBufferCollectionFUCHSIA[buffer_collection].constraints =
2795 std::make_optional<fuchsia_sysmem::wire::BufferCollectionConstraints>(
2796 std::move(setConstraintsResult.constraints));
2797 info_VkBufferCollectionFUCHSIA[buffer_collection].createInfoIndex =
2798 std::move(setConstraintsResult.createInfoIndex);
2799 }
2800
2801 return VK_SUCCESS;
2802 }
2803
2804 VkResult ResourceTracker::setBufferCollectionBufferConstraintsFUCHSIA(
2805 fidl::WireSyncClient<fuchsia_sysmem::BufferCollection>* pCollection,
2806 const VkBufferConstraintsInfoFUCHSIA* pBufferConstraintsInfo) {
2807 auto setConstraintsResult =
2808 setBufferCollectionBufferConstraintsImpl(pCollection, pBufferConstraintsInfo);
2809 if (setConstraintsResult.result != VK_SUCCESS) {
2810 return setConstraintsResult.result;
2811 }
2812
2813 // copy constraints to info_VkBufferCollectionFUCHSIA if
2814 // |collection| is a valid VkBufferCollectionFUCHSIA handle.
2815 std::lock_guard<std::recursive_mutex> lock(mLock);
2816 VkBufferCollectionFUCHSIA buffer_collection =
2817 reinterpret_cast<VkBufferCollectionFUCHSIA>(pCollection);
2818 if (info_VkBufferCollectionFUCHSIA.find(buffer_collection) !=
2819 info_VkBufferCollectionFUCHSIA.end()) {
2820 info_VkBufferCollectionFUCHSIA[buffer_collection].constraints =
2821 std::make_optional<fuchsia_sysmem::wire::BufferCollectionConstraints>(
2822 setConstraintsResult.constraints);
2823 }
2824
2825 return VK_SUCCESS;
2826 }
2827
2828 VkResult ResourceTracker::on_vkSetBufferCollectionImageConstraintsFUCHSIA(
2829 void* context, VkResult, VkDevice device, VkBufferCollectionFUCHSIA collection,
2830 const VkImageConstraintsInfoFUCHSIA* pImageConstraintsInfo) {
2831 VkEncoder* enc = (VkEncoder*)context;
2832 auto sysmem_collection =
2833 reinterpret_cast<fidl::WireSyncClient<fuchsia_sysmem::BufferCollection>*>(collection);
2834 return setBufferCollectionImageConstraintsFUCHSIA(enc, device, sysmem_collection,
2835 pImageConstraintsInfo);
2836 }
2837
2838 VkResult ResourceTracker::on_vkSetBufferCollectionBufferConstraintsFUCHSIA(
2839 void*, VkResult, VkDevice, VkBufferCollectionFUCHSIA collection,
2840 const VkBufferConstraintsInfoFUCHSIA* pBufferConstraintsInfo) {
2841 auto sysmem_collection =
2842 reinterpret_cast<fidl::WireSyncClient<fuchsia_sysmem::BufferCollection>*>(collection);
2843 return setBufferCollectionBufferConstraintsFUCHSIA(sysmem_collection, pBufferConstraintsInfo);
2844 }
2845
2846 VkResult ResourceTracker::getBufferCollectionImageCreateInfoIndexLocked(
2847 VkBufferCollectionFUCHSIA collection, fuchsia_sysmem::wire::BufferCollectionInfo2& info,
2848 uint32_t* outCreateInfoIndex) {
2849 if (!info_VkBufferCollectionFUCHSIA[collection].constraints.has_value()) {
2850 mesa_loge("%s: constraints not set", __func__);
2851 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
2852 }
2853
2854 if (!info.settings.has_image_format_constraints) {
2855 // no image format constraints, skip getting createInfoIndex.
2856 return VK_SUCCESS;
2857 }
2858
2859 const auto& constraints = *info_VkBufferCollectionFUCHSIA[collection].constraints;
2860 const auto& createInfoIndices = info_VkBufferCollectionFUCHSIA[collection].createInfoIndex;
2861 const auto& out = info.settings.image_format_constraints;
2862 bool foundCreateInfo = false;
2863
2864 for (size_t imageFormatIndex = 0; imageFormatIndex < constraints.image_format_constraints_count;
2865 imageFormatIndex++) {
2866 const auto& in = constraints.image_format_constraints[imageFormatIndex];
2867 // These checks are sorted in order of how often they're expected to
2868 // mismatch, from most likely to least likely. They aren't always
2869 // equality comparisons, since sysmem may change some values in
2870 // compatible ways on behalf of the other participants.
2871 if ((out.pixel_format.type != in.pixel_format.type) ||
2872 (out.pixel_format.has_format_modifier != in.pixel_format.has_format_modifier) ||
2873 (out.pixel_format.format_modifier.value != in.pixel_format.format_modifier.value) ||
2874 (out.min_bytes_per_row < in.min_bytes_per_row) ||
2875 (out.required_max_coded_width < in.required_max_coded_width) ||
2876 (out.required_max_coded_height < in.required_max_coded_height) ||
2877 (in.bytes_per_row_divisor != 0 &&
2878 out.bytes_per_row_divisor % in.bytes_per_row_divisor != 0)) {
2879 continue;
2880 }
2881 // Check if the out colorspaces are a subset of the in color spaces.
2882 bool all_color_spaces_found = true;
2883 for (uint32_t j = 0; j < out.color_spaces_count; j++) {
2884 bool found_matching_color_space = false;
2885 for (uint32_t k = 0; k < in.color_spaces_count; k++) {
2886 if (out.color_space[j].type == in.color_space[k].type) {
2887 found_matching_color_space = true;
2888 break;
2889 }
2890 }
2891 if (!found_matching_color_space) {
2892 all_color_spaces_found = false;
2893 break;
2894 }
2895 }
2896 if (!all_color_spaces_found) {
2897 continue;
2898 }
2899
2900 // Choose the first valid format for now.
2901 *outCreateInfoIndex = createInfoIndices[imageFormatIndex];
2902 return VK_SUCCESS;
2903 }
2904
2905 mesa_loge("%s: cannot find a valid image format in constraints", __func__);
2906 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
2907 }
2908
2909 VkResult ResourceTracker::on_vkGetBufferCollectionPropertiesFUCHSIA(
2910 void* context, VkResult, VkDevice device, VkBufferCollectionFUCHSIA collection,
2911 VkBufferCollectionPropertiesFUCHSIA* pProperties) {
2912 VkEncoder* enc = (VkEncoder*)context;
2913 const auto& sysmem_collection =
2914 *reinterpret_cast<fidl::WireSyncClient<fuchsia_sysmem::BufferCollection>*>(collection);
2915
2916 auto result = sysmem_collection->WaitForBuffersAllocated();
2917 if (!result.ok() || result->status != ZX_OK) {
2918 mesa_loge("Failed wait for allocation: %d %d", result.status(),
2919 GET_STATUS_SAFE(result, status));
2920 return VK_ERROR_INITIALIZATION_FAILED;
2921 }
2922 fuchsia_sysmem::wire::BufferCollectionInfo2 info = std::move(result->buffer_collection_info);
2923
2924 bool is_host_visible =
2925 info.settings.buffer_settings.heap == fuchsia_sysmem::wire::HeapType::kGoldfishHostVisible;
2926 bool is_device_local =
2927 info.settings.buffer_settings.heap == fuchsia_sysmem::wire::HeapType::kGoldfishDeviceLocal;
2928 if (!is_host_visible && !is_device_local) {
2929 mesa_loge("buffer collection uses a non-goldfish heap (type 0x%lu)",
2930 static_cast<uint64_t>(info.settings.buffer_settings.heap));
2931 return VK_ERROR_INITIALIZATION_FAILED;
2932 }
2933
2934 // memoryTypeBits
2935 // ====================================================================
2936 {
2937 std::lock_guard<std::recursive_mutex> lock(mLock);
2938 auto deviceIt = info_VkDevice.find(device);
2939 if (deviceIt == info_VkDevice.end()) {
2940 return VK_ERROR_INITIALIZATION_FAILED;
2941 }
2942 auto& deviceInfo = deviceIt->second;
2943
2944 // Device local memory type supported.
2945 pProperties->memoryTypeBits = 0;
2946 for (uint32_t i = 0; i < deviceInfo.memProps.memoryTypeCount; ++i) {
2947 if ((is_device_local && (deviceInfo.memProps.memoryTypes[i].propertyFlags &
2948 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT)) ||
2949 (is_host_visible && (deviceInfo.memProps.memoryTypes[i].propertyFlags &
2950 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT))) {
2951 pProperties->memoryTypeBits |= 1ull << i;
2952 }
2953 }
2954 }
2955
2956 // bufferCount
2957 // ====================================================================
2958 pProperties->bufferCount = info.buffer_count;
2959
2960 auto storeProperties = [this, collection, pProperties]() -> VkResult {
2961 // store properties to storage
2962 std::lock_guard<std::recursive_mutex> lock(mLock);
2963 if (info_VkBufferCollectionFUCHSIA.find(collection) ==
2964 info_VkBufferCollectionFUCHSIA.end()) {
2965 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
2966 }
2967
2968 info_VkBufferCollectionFUCHSIA[collection].properties =
2969 std::make_optional<VkBufferCollectionPropertiesFUCHSIA>(*pProperties);
2970
2971 // We only do a shallow copy so we should remove all pNext pointers.
2972 info_VkBufferCollectionFUCHSIA[collection].properties->pNext = nullptr;
2973 info_VkBufferCollectionFUCHSIA[collection].properties->sysmemColorSpaceIndex.pNext =
2974 nullptr;
2975 return VK_SUCCESS;
2976 };
2977
2978 // The fields below only apply to buffer collections with image formats.
2979 if (!info.settings.has_image_format_constraints) {
2980 mesa_logd("%s: buffer collection doesn't have image format constraints", __func__);
2981 return storeProperties();
2982 }
2983
2984 // sysmemFormat
2985 // ====================================================================
2986
2987 pProperties->sysmemPixelFormat =
2988 static_cast<uint64_t>(info.settings.image_format_constraints.pixel_format.type);
2989
2990 // colorSpace
2991 // ====================================================================
2992 if (info.settings.image_format_constraints.color_spaces_count == 0) {
2993 mesa_loge(
2994 "%s: color space missing from allocated buffer collection "
2995 "constraints",
2996 __func__);
2997 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
2998 }
2999 // Only report first colorspace for now.
3000 pProperties->sysmemColorSpaceIndex.colorSpace =
3001 static_cast<uint32_t>(info.settings.image_format_constraints.color_space[0].type);
3002
3003 // createInfoIndex
3004 // ====================================================================
3005 {
3006 std::lock_guard<std::recursive_mutex> lock(mLock);
3007 auto getIndexResult = getBufferCollectionImageCreateInfoIndexLocked(
3008 collection, info, &pProperties->createInfoIndex);
3009 if (getIndexResult != VK_SUCCESS) {
3010 return getIndexResult;
3011 }
3012 }
3013
3014 // formatFeatures
3015 // ====================================================================
3016 VkPhysicalDevice physicalDevice;
3017 {
3018 std::lock_guard<std::recursive_mutex> lock(mLock);
3019 auto deviceIt = info_VkDevice.find(device);
3020 if (deviceIt == info_VkDevice.end()) {
3021 return VK_ERROR_INITIALIZATION_FAILED;
3022 }
3023 physicalDevice = deviceIt->second.physdev;
3024 }
3025
3026 VkFormat vkFormat =
3027 sysmemPixelFormatTypeToVk(info.settings.image_format_constraints.pixel_format.type);
3028 VkFormatProperties formatProperties;
3029 enc->vkGetPhysicalDeviceFormatProperties(physicalDevice, vkFormat, &formatProperties,
3030 true /* do lock */);
3031 if (is_device_local) {
3032 pProperties->formatFeatures = formatProperties.optimalTilingFeatures;
3033 }
3034 if (is_host_visible) {
3035 pProperties->formatFeatures = formatProperties.linearTilingFeatures;
3036 }
3037
3038 // YCbCr properties
3039 // ====================================================================
3040 // TODO(59804): Implement this correctly when we support YUV pixel
3041 // formats in goldfish ICD.
3042 pProperties->samplerYcbcrConversionComponents.r = VK_COMPONENT_SWIZZLE_IDENTITY;
3043 pProperties->samplerYcbcrConversionComponents.g = VK_COMPONENT_SWIZZLE_IDENTITY;
3044 pProperties->samplerYcbcrConversionComponents.b = VK_COMPONENT_SWIZZLE_IDENTITY;
3045 pProperties->samplerYcbcrConversionComponents.a = VK_COMPONENT_SWIZZLE_IDENTITY;
3046 pProperties->suggestedYcbcrModel = VK_SAMPLER_YCBCR_MODEL_CONVERSION_RGB_IDENTITY;
3047 pProperties->suggestedYcbcrRange = VK_SAMPLER_YCBCR_RANGE_ITU_FULL;
3048 pProperties->suggestedXChromaOffset = VK_CHROMA_LOCATION_MIDPOINT;
3049 pProperties->suggestedYChromaOffset = VK_CHROMA_LOCATION_MIDPOINT;
3050
3051 return storeProperties();
3052 }
3053 #endif
3054
3055 static uint32_t getVirglFormat(VkFormat vkFormat) {
3056 uint32_t virglFormat = 0;
3057
3058 switch (vkFormat) {
3059 case VK_FORMAT_R8G8B8A8_SINT:
3060 case VK_FORMAT_R8G8B8A8_UNORM:
3061 case VK_FORMAT_R8G8B8A8_SRGB:
3062 case VK_FORMAT_R8G8B8A8_SNORM:
3063 case VK_FORMAT_R8G8B8A8_SSCALED:
3064 case VK_FORMAT_R8G8B8A8_USCALED:
3065 virglFormat = VIRGL_FORMAT_R8G8B8A8_UNORM;
3066 break;
3067 case VK_FORMAT_B8G8R8A8_SINT:
3068 case VK_FORMAT_B8G8R8A8_UNORM:
3069 case VK_FORMAT_B8G8R8A8_SRGB:
3070 case VK_FORMAT_B8G8R8A8_SNORM:
3071 case VK_FORMAT_B8G8R8A8_SSCALED:
3072 case VK_FORMAT_B8G8R8A8_USCALED:
3073 virglFormat = VIRGL_FORMAT_B8G8R8A8_UNORM;
3074 break;
3075 case VK_FORMAT_A2R10G10B10_UNORM_PACK32:
3076 virglFormat = VIRGL_FORMAT_R10G10B10A2_UNORM;
3077 break;
3078 default:
3079 break;
3080 }
3081
3082 return virglFormat;
3083 }
3084
3085 CoherentMemoryPtr ResourceTracker::createCoherentMemory(
3086 VkDevice device, VkDeviceMemory mem, const VkMemoryAllocateInfo& hostAllocationInfo,
3087 VkEncoder* enc, VkResult& res) {
3088 CoherentMemoryPtr coherentMemory = nullptr;
3089
3090 #if DETECT_OS_ANDROID
3091 if (mFeatureInfo.hasDirectMem) {
3092 uint64_t gpuAddr = 0;
3093 GoldfishAddressSpaceBlockPtr block = nullptr;
3094 res = enc->vkMapMemoryIntoAddressSpaceGOOGLE(device, mem, &gpuAddr, true);
3095 if (res != VK_SUCCESS) {
3096 mesa_loge(
3097 "Failed to create coherent memory: vkMapMemoryIntoAddressSpaceGOOGLE "
3098 "returned:%d.",
3099 res);
3100 return coherentMemory;
3101 }
3102 {
3103 std::lock_guard<std::recursive_mutex> lock(mLock);
3104 auto it = info_VkDeviceMemory.find(mem);
3105 if (it == info_VkDeviceMemory.end()) {
3106 mesa_loge("Failed to create coherent memory: failed to find device memory.");
3107 res = VK_ERROR_OUT_OF_HOST_MEMORY;
3108 return coherentMemory;
3109 }
3110 auto& info = it->second;
3111 block = info.goldfishBlock;
3112 info.goldfishBlock = nullptr;
3113
3114 coherentMemory = std::make_shared<CoherentMemory>(
3115 block, gpuAddr, hostAllocationInfo.allocationSize, device, mem);
3116 }
3117 } else
3118 #endif // DETECT_OS_ANDROID
3119 if (mFeatureInfo.hasVirtioGpuNext) {
3120 struct VirtGpuCreateBlob createBlob = {0};
3121 uint64_t hvaSizeId[3];
3122 res = enc->vkGetMemoryHostAddressInfoGOOGLE(device, mem, &hvaSizeId[0], &hvaSizeId[1],
3123 &hvaSizeId[2], true /* do lock */);
3124 if (res != VK_SUCCESS) {
3125 mesa_loge(
3126 "Failed to create coherent memory: vkMapMemoryIntoAddressSpaceGOOGLE "
3127 "returned:%d.",
3128 res);
3129 return coherentMemory;
3130 }
3131 {
3132 std::lock_guard<std::recursive_mutex> lock(mLock);
3133 VirtGpuDevice* instance = VirtGpuDevice::getInstance((enum VirtGpuCapset)3);
3134 createBlob.blobMem = kBlobMemHost3d;
3135 createBlob.flags = kBlobFlagMappable;
3136 createBlob.blobId = hvaSizeId[2];
3137 createBlob.size = hostAllocationInfo.allocationSize;
3138
3139 auto blob = instance->createBlob(createBlob);
3140 if (!blob) {
3141 mesa_loge("Failed to create coherent memory: failed to create blob.");
3142 res = VK_ERROR_OUT_OF_DEVICE_MEMORY;
3143 return coherentMemory;
3144 }
3145
3146 VirtGpuResourceMappingPtr mapping = blob->createMapping();
3147 if (!mapping) {
3148 mesa_loge("Failed to create coherent memory: failed to create blob mapping.");
3149 res = VK_ERROR_OUT_OF_DEVICE_MEMORY;
3150 return coherentMemory;
3151 }
3152
3153 coherentMemory =
3154 std::make_shared<CoherentMemory>(mapping, createBlob.size, device, mem);
3155 }
3156 } else {
3157 mesa_loge("FATAL: Unsupported virtual memory feature");
3158 abort();
3159 }
3160 return coherentMemory;
3161 }
3162
3163 VkResult ResourceTracker::allocateCoherentMemory(VkDevice device,
3164 const VkMemoryAllocateInfo* pAllocateInfo,
3165 VkEncoder* enc, VkDeviceMemory* pMemory) {
3166 uint64_t offset = 0;
3167 uint8_t* ptr = nullptr;
3168 VkMemoryAllocateFlagsInfo allocFlagsInfo;
3169 VkMemoryOpaqueCaptureAddressAllocateInfo opaqueCaptureAddressAllocInfo;
3170 VkCreateBlobGOOGLE createBlobInfo;
3171 VirtGpuResourcePtr guestBlob = nullptr;
3172
3173 memset(&createBlobInfo, 0, sizeof(struct VkCreateBlobGOOGLE));
3174 createBlobInfo.sType = VK_STRUCTURE_TYPE_CREATE_BLOB_GOOGLE;
3175
3176 const VkMemoryAllocateFlagsInfo* allocFlagsInfoPtr =
3177 vk_find_struct_const(pAllocateInfo, MEMORY_ALLOCATE_FLAGS_INFO);
3178 const VkMemoryOpaqueCaptureAddressAllocateInfo* opaqueCaptureAddressAllocInfoPtr =
3179 vk_find_struct_const(pAllocateInfo, MEMORY_OPAQUE_CAPTURE_ADDRESS_ALLOCATE_INFO);
3180
3181 bool deviceAddressMemoryAllocation =
3182 allocFlagsInfoPtr &&
3183 ((allocFlagsInfoPtr->flags & VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT) ||
3184 (allocFlagsInfoPtr->flags & VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_CAPTURE_REPLAY_BIT));
3185
3186 bool dedicated = deviceAddressMemoryAllocation;
3187
3188 if (mCaps.vulkanCapset.deferredMapping || mCaps.params[kParamCreateGuestHandle])
3189 dedicated = true;
3190
3191 VkMemoryAllocateInfo hostAllocationInfo = vk_make_orphan_copy(*pAllocateInfo);
3192 vk_struct_chain_iterator structChainIter = vk_make_chain_iterator(&hostAllocationInfo);
3193
3194 if (mCaps.vulkanCapset.deferredMapping || mCaps.params[kParamCreateGuestHandle]) {
3195 hostAllocationInfo.allocationSize =
3196 ALIGN_POT(pAllocateInfo->allocationSize, mCaps.vulkanCapset.blobAlignment);
3197 } else if (dedicated) {
3198 // Over-aligning to kLargestSize to some Windows drivers (b:152769369). Can likely
3199 // have host report the desired alignment.
3200 hostAllocationInfo.allocationSize =
3201 ALIGN_POT(pAllocateInfo->allocationSize, kLargestPageSize);
3202 } else {
3203 VkDeviceSize roundedUpAllocSize = ALIGN_POT(pAllocateInfo->allocationSize, kMegaByte);
3204 hostAllocationInfo.allocationSize = std::max(roundedUpAllocSize, kDefaultHostMemBlockSize);
3205 }
3206
3207 // Support device address capture/replay allocations
3208 if (deviceAddressMemoryAllocation) {
3209 if (allocFlagsInfoPtr) {
3210 MESA_TRACE_SCOPE("%s: has alloc flags\n", __func__);
3211 allocFlagsInfo = *allocFlagsInfoPtr;
3212 vk_append_struct(&structChainIter, &allocFlagsInfo);
3213 }
3214
3215 if (opaqueCaptureAddressAllocInfoPtr) {
3216 MESA_TRACE_SCOPE("%s: has opaque capture address\n", __func__);
3217 opaqueCaptureAddressAllocInfo = *opaqueCaptureAddressAllocInfoPtr;
3218 vk_append_struct(&structChainIter, &opaqueCaptureAddressAllocInfo);
3219 }
3220 }
3221
3222 if (mCaps.params[kParamCreateGuestHandle]) {
3223 struct VirtGpuCreateBlob createBlob = {0};
3224 struct VirtGpuExecBuffer exec = {};
3225 VirtGpuDevice* instance = VirtGpuDevice::getInstance();
3226 struct gfxstreamPlaceholderCommandVk placeholderCmd = {};
3227
3228 createBlobInfo.blobId = ++mAtomicId;
3229 createBlobInfo.blobMem = kBlobMemGuest;
3230 createBlobInfo.blobFlags = kBlobFlagCreateGuestHandle;
3231 vk_append_struct(&structChainIter, &createBlobInfo);
3232
3233 createBlob.blobMem = kBlobMemGuest;
3234 createBlob.flags = kBlobFlagCreateGuestHandle;
3235 createBlob.blobId = createBlobInfo.blobId;
3236 createBlob.size = hostAllocationInfo.allocationSize;
3237
3238 guestBlob = instance->createBlob(createBlob);
3239 if (!guestBlob) {
3240 mesa_loge("Failed to allocate coherent memory: failed to create blob.");
3241 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
3242 }
3243
3244 placeholderCmd.hdr.opCode = GFXSTREAM_PLACEHOLDER_COMMAND_VK;
3245 exec.command = static_cast<void*>(&placeholderCmd);
3246 exec.command_size = sizeof(placeholderCmd);
3247 exec.flags = kRingIdx;
3248 exec.ring_idx = 1;
3249 if (instance->execBuffer(exec, guestBlob.get())) {
3250 mesa_loge("Failed to allocate coherent memory: failed to execbuffer for wait.");
3251 return VK_ERROR_OUT_OF_HOST_MEMORY;
3252 }
3253
3254 guestBlob->wait();
3255 } else if (mCaps.vulkanCapset.deferredMapping) {
3256 createBlobInfo.blobId = ++mAtomicId;
3257 createBlobInfo.blobMem = kBlobMemHost3d;
3258 vk_append_struct(&structChainIter, &createBlobInfo);
3259 }
3260
3261 VkDeviceMemory mem = VK_NULL_HANDLE;
3262 VkResult host_res =
3263 enc->vkAllocateMemory(device, &hostAllocationInfo, nullptr, &mem, true /* do lock */);
3264 if (host_res != VK_SUCCESS) {
3265 mesa_loge("Failed to allocate coherent memory: failed to allocate on the host: %d.",
3266 host_res);
3267 return host_res;
3268 }
3269
3270 struct VkDeviceMemory_Info info;
3271 if (mCaps.vulkanCapset.deferredMapping || mCaps.params[kParamCreateGuestHandle]) {
3272 info.allocationSize = pAllocateInfo->allocationSize;
3273 info.blobId = createBlobInfo.blobId;
3274 }
3275
3276 if (guestBlob) {
3277 auto mapping = guestBlob->createMapping();
3278 if (!mapping) {
3279 mesa_loge("Failed to allocate coherent memory: failed to create blob mapping.");
3280 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
3281 }
3282
3283 auto coherentMemory = std::make_shared<CoherentMemory>(
3284 mapping, hostAllocationInfo.allocationSize, device, mem);
3285
3286 coherentMemory->subAllocate(pAllocateInfo->allocationSize, &ptr, offset);
3287 info.coherentMemoryOffset = offset;
3288 info.coherentMemory = coherentMemory;
3289 info.ptr = ptr;
3290 }
3291
3292 info.coherentMemorySize = hostAllocationInfo.allocationSize;
3293 info.memoryTypeIndex = hostAllocationInfo.memoryTypeIndex;
3294 info.device = device;
3295 info.dedicated = dedicated;
3296 {
3297 // createCoherentMemory inside need to access info_VkDeviceMemory
3298 // information. set it before use.
3299 std::lock_guard<std::recursive_mutex> lock(mLock);
3300 info_VkDeviceMemory[mem] = info;
3301 }
3302
3303 if (mCaps.vulkanCapset.deferredMapping || mCaps.params[kParamCreateGuestHandle]) {
3304 *pMemory = mem;
3305 return host_res;
3306 }
3307
3308 auto coherentMemory = createCoherentMemory(device, mem, hostAllocationInfo, enc, host_res);
3309 if (coherentMemory) {
3310 std::lock_guard<std::recursive_mutex> lock(mLock);
3311 coherentMemory->subAllocate(pAllocateInfo->allocationSize, &ptr, offset);
3312 info.allocationSize = pAllocateInfo->allocationSize;
3313 info.coherentMemoryOffset = offset;
3314 info.coherentMemory = coherentMemory;
3315 info.ptr = ptr;
3316 info_VkDeviceMemory[mem] = info;
3317 *pMemory = mem;
3318 } else {
3319 enc->vkFreeMemory(device, mem, nullptr, true);
3320 std::lock_guard<std::recursive_mutex> lock(mLock);
3321 info_VkDeviceMemory.erase(mem);
3322 }
3323 return host_res;
3324 }
3325
3326 VkResult ResourceTracker::getCoherentMemory(const VkMemoryAllocateInfo* pAllocateInfo,
3327 VkEncoder* enc, VkDevice device,
3328 VkDeviceMemory* pMemory) {
3329 // Add buffer device address capture structs
3330 const VkMemoryAllocateFlagsInfo* allocFlagsInfoPtr =
3331 vk_find_struct_const(pAllocateInfo, MEMORY_ALLOCATE_FLAGS_INFO);
3332
3333 bool dedicated =
3334 allocFlagsInfoPtr &&
3335 ((allocFlagsInfoPtr->flags & VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT) ||
3336 (allocFlagsInfoPtr->flags & VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_CAPTURE_REPLAY_BIT));
3337
3338 if (mCaps.vulkanCapset.deferredMapping || mCaps.params[kParamCreateGuestHandle])
3339 dedicated = true;
3340
3341 CoherentMemoryPtr coherentMemory = nullptr;
3342 uint8_t* ptr = nullptr;
3343 uint64_t offset = 0;
3344 {
3345 std::lock_guard<std::recursive_mutex> lock(mLock);
3346 for (const auto& [memory, info] : info_VkDeviceMemory) {
3347 if (info.device != device) continue;
3348
3349 if (info.memoryTypeIndex != pAllocateInfo->memoryTypeIndex) continue;
3350
3351 if (info.dedicated || dedicated) continue;
3352
3353 if (!info.coherentMemory) continue;
3354
3355 if (!info.coherentMemory->subAllocate(pAllocateInfo->allocationSize, &ptr, offset))
3356 continue;
3357
3358 coherentMemory = info.coherentMemory;
3359 break;
3360 }
3361 if (coherentMemory) {
3362 struct VkDeviceMemory_Info info;
3363 info.coherentMemoryOffset = offset;
3364 info.ptr = ptr;
3365 info.memoryTypeIndex = pAllocateInfo->memoryTypeIndex;
3366 info.allocationSize = pAllocateInfo->allocationSize;
3367 info.coherentMemory = coherentMemory;
3368 info.device = device;
3369
3370 // for suballocated memory, create an alias VkDeviceMemory handle for application
3371 // memory used for suballocations will still be VkDeviceMemory associated with
3372 // CoherentMemory
3373 auto mem = new_from_host_VkDeviceMemory(VK_NULL_HANDLE);
3374 info_VkDeviceMemory[mem] = info;
3375 *pMemory = mem;
3376 return VK_SUCCESS;
3377 }
3378 }
3379 return allocateCoherentMemory(device, pAllocateInfo, enc, pMemory);
3380 }
3381
3382 VkResult ResourceTracker::on_vkAllocateMemory(void* context, VkResult input_result, VkDevice device,
3383 const VkMemoryAllocateInfo* pAllocateInfo,
3384 const VkAllocationCallbacks* pAllocator,
3385 VkDeviceMemory* pMemory) {
3386 #define _RETURN_FAILURE_WITH_DEVICE_MEMORY_REPORT(result) \
3387 { \
3388 auto it = info_VkDevice.find(device); \
3389 if (it == info_VkDevice.end()) return result; \
3390 emitDeviceMemoryReport(it->second, \
3391 VK_DEVICE_MEMORY_REPORT_EVENT_TYPE_ALLOCATION_FAILED_EXT, 0, \
3392 pAllocateInfo->allocationSize, VK_OBJECT_TYPE_DEVICE_MEMORY, 0, \
3393 pAllocateInfo->memoryTypeIndex); \
3394 return result; \
3395 }
3396
3397 if (input_result != VK_SUCCESS) _RETURN_FAILURE_WITH_DEVICE_MEMORY_REPORT(input_result);
3398
3399 VkEncoder* enc = (VkEncoder*)context;
3400
3401 bool hasDedicatedImage = false;
3402 bool hasDedicatedBuffer = false;
3403
3404 VkMemoryAllocateInfo finalAllocInfo = vk_make_orphan_copy(*pAllocateInfo);
3405 vk_struct_chain_iterator structChainIter = vk_make_chain_iterator(&finalAllocInfo);
3406
3407 VkMemoryAllocateFlagsInfo allocFlagsInfo;
3408 VkMemoryOpaqueCaptureAddressAllocateInfo opaqueCaptureAddressAllocInfo;
3409
3410 // Add buffer device address capture structs
3411 const VkMemoryAllocateFlagsInfo* allocFlagsInfoPtr =
3412 vk_find_struct_const(pAllocateInfo, MEMORY_ALLOCATE_FLAGS_INFO);
3413 const VkMemoryOpaqueCaptureAddressAllocateInfo* opaqueCaptureAddressAllocInfoPtr =
3414 vk_find_struct_const(pAllocateInfo, MEMORY_OPAQUE_CAPTURE_ADDRESS_ALLOCATE_INFO);
3415
3416 if (allocFlagsInfoPtr) {
3417 MESA_TRACE_SCOPE("%s: has alloc flags\n", __func__);
3418 allocFlagsInfo = *allocFlagsInfoPtr;
3419 vk_append_struct(&structChainIter, &allocFlagsInfo);
3420 }
3421
3422 if (opaqueCaptureAddressAllocInfoPtr) {
3423 MESA_TRACE_SCOPE("%s: has opaque capture address\n", __func__);
3424 opaqueCaptureAddressAllocInfo = *opaqueCaptureAddressAllocInfoPtr;
3425 vk_append_struct(&structChainIter, &opaqueCaptureAddressAllocInfo);
3426 }
3427
3428 VkMemoryDedicatedAllocateInfo dedicatedAllocInfo;
3429 VkImportColorBufferGOOGLE importCbInfo = {
3430 VK_STRUCTURE_TYPE_IMPORT_COLOR_BUFFER_GOOGLE,
3431 0,
3432 };
3433 VkImportBufferGOOGLE importBufferInfo = {
3434 VK_STRUCTURE_TYPE_IMPORT_BUFFER_GOOGLE,
3435 0,
3436 };
3437 // VkImportPhysicalAddressGOOGLE importPhysAddrInfo = {
3438 // VK_STRUCTURE_TYPE_IMPORT_PHYSICAL_ADDRESS_GOOGLE, 0,
3439 // };
3440
3441 const VkExportMemoryAllocateInfo* exportAllocateInfoPtr =
3442 vk_find_struct_const(pAllocateInfo, EXPORT_MEMORY_ALLOCATE_INFO);
3443
3444 #ifdef VK_USE_PLATFORM_ANDROID_KHR
3445 const VkImportAndroidHardwareBufferInfoANDROID* importAhbInfoPtr =
3446 vk_find_struct_const(pAllocateInfo, IMPORT_ANDROID_HARDWARE_BUFFER_INFO_ANDROID);
3447 // Even if we export allocate, the underlying operation
3448 // for the host is always going to be an import operation.
3449 // This is also how Intel's implementation works,
3450 // and is generally simpler;
3451 // even in an export allocation,
3452 // we perform AHardwareBuffer allocation
3453 // on the guest side, at this layer,
3454 // and then we attach a new VkDeviceMemory
3455 // to the AHardwareBuffer on the host via an "import" operation.
3456 AHardwareBuffer* ahw = nullptr;
3457 #else
3458 const void* importAhbInfoPtr = nullptr;
3459 void* ahw = nullptr;
3460 #endif
3461
3462 #ifdef VK_USE_PLATFORM_FUCHSIA
3463 const VkImportMemoryBufferCollectionFUCHSIA* importBufferCollectionInfoPtr =
3464 vk_find_struct_const(pAllocateInfo, IMPORT_MEMORY_BUFFER_COLLECTION_FUCHSIA);
3465
3466 const VkImportMemoryZirconHandleInfoFUCHSIA* importVmoInfoPtr =
3467 vk_find_struct_const(pAllocateInfo, IMPORT_MEMORY_ZIRCON_HANDLE_INFO_FUCHSIA);
3468 #else
3469 const void* importBufferCollectionInfoPtr = nullptr;
3470 const void* importVmoInfoPtr = nullptr;
3471 #endif // VK_USE_PLATFORM_FUCHSIA
3472
3473 const VkMemoryDedicatedAllocateInfo* dedicatedAllocInfoPtr =
3474 vk_find_struct_const(pAllocateInfo, MEMORY_DEDICATED_ALLOCATE_INFO);
3475
3476 // Note for AHardwareBuffers, the Vulkan spec states:
3477 //
3478 // Android hardware buffers have intrinsic width, height, format, and usage
3479 // properties, so Vulkan images bound to memory imported from an Android
3480 // hardware buffer must use dedicated allocations
3481 //
3482 // so any allocation requests with a VkImportAndroidHardwareBufferInfoANDROID
3483 // will necessarily have a VkMemoryDedicatedAllocateInfo. However, the host
3484 // may or may not actually use a dedicated allocation to emulate
3485 // AHardwareBuffers. As such, the VkMemoryDedicatedAllocateInfo is passed to the
3486 // host and the host will decide whether or not to use it.
3487
3488 bool shouldPassThroughDedicatedAllocInfo =
3489 !exportAllocateInfoPtr && !importBufferCollectionInfoPtr && !importVmoInfoPtr;
3490
3491 const VkPhysicalDeviceMemoryProperties& physicalDeviceMemoryProps =
3492 getPhysicalDeviceMemoryProperties(context, device, VK_NULL_HANDLE);
3493
3494 const bool requestedMemoryIsHostVisible =
3495 isHostVisible(&physicalDeviceMemoryProps, pAllocateInfo->memoryTypeIndex);
3496
3497 #if defined(VK_USE_PLATFORM_ANDROID_KHR) || DETECT_OS_LINUX
3498 shouldPassThroughDedicatedAllocInfo &= !requestedMemoryIsHostVisible;
3499 #endif // VK_USE_PLATFORM_FUCHSIA
3500
3501 if (shouldPassThroughDedicatedAllocInfo && dedicatedAllocInfoPtr) {
3502 dedicatedAllocInfo = vk_make_orphan_copy(*dedicatedAllocInfoPtr);
3503 vk_append_struct(&structChainIter, &dedicatedAllocInfo);
3504 }
3505
3506 // State needed for import/export.
3507 bool exportAhb = false;
3508 bool exportVmo = false;
3509 bool exportDmabuf = false;
3510 bool importAhb = false;
3511 bool importBufferCollection = false;
3512 bool importVmo = false;
3513 bool importDmabuf = false;
3514 (void)exportVmo;
3515 (void)exportAhb;
3516
3517 if (exportAllocateInfoPtr) {
3518 exportAhb = exportAllocateInfoPtr->handleTypes &
3519 VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID;
3520 #ifdef VK_USE_PLATFORM_FUCHSIA
3521 exportVmo = exportAllocateInfoPtr->handleTypes &
3522 VK_EXTERNAL_MEMORY_HANDLE_TYPE_ZIRCON_VMO_BIT_FUCHSIA;
3523 #endif // VK_USE_PLATFORM_FUCHSIA
3524 exportDmabuf =
3525 (exportAllocateInfoPtr->handleTypes & VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT);
3526 } else if (importAhbInfoPtr) {
3527 importAhb = true;
3528 } else if (importBufferCollectionInfoPtr) {
3529 importBufferCollection = true;
3530 } else if (importVmoInfoPtr) {
3531 importVmo = true;
3532 }
3533
3534 const VkImportMemoryFdInfoKHR* importFdInfoPtr =
3535 vk_find_struct_const(pAllocateInfo, IMPORT_MEMORY_FD_INFO_KHR);
3536 if (importFdInfoPtr) {
3537 importDmabuf =
3538 (importFdInfoPtr->handleType & VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT);
3539 }
3540 bool isImport = importAhb || importBufferCollection || importVmo || importDmabuf;
3541
3542 #if defined(VK_USE_PLATFORM_ANDROID_KHR)
3543 if (exportAhb) {
3544 hasDedicatedImage =
3545 dedicatedAllocInfoPtr && (dedicatedAllocInfoPtr->image != VK_NULL_HANDLE);
3546 hasDedicatedBuffer =
3547 dedicatedAllocInfoPtr && (dedicatedAllocInfoPtr->buffer != VK_NULL_HANDLE);
3548 VkExtent3D imageExtent = {0, 0, 0};
3549 uint32_t imageLayers = 0;
3550 VkFormat imageFormat = VK_FORMAT_UNDEFINED;
3551 VkImageUsageFlags imageUsage = 0;
3552 VkImageCreateFlags imageCreateFlags = 0;
3553 VkDeviceSize bufferSize = 0;
3554 VkDeviceSize allocationInfoAllocSize = finalAllocInfo.allocationSize;
3555
3556 if (hasDedicatedImage) {
3557 std::lock_guard<std::recursive_mutex> lock(mLock);
3558
3559 auto it = info_VkImage.find(dedicatedAllocInfoPtr->image);
3560 if (it == info_VkImage.end())
3561 _RETURN_FAILURE_WITH_DEVICE_MEMORY_REPORT(VK_ERROR_INITIALIZATION_FAILED);
3562 const auto& info = it->second;
3563 const auto& imgCi = info.createInfo;
3564
3565 imageExtent = imgCi.extent;
3566 imageLayers = imgCi.arrayLayers;
3567 imageFormat = imgCi.format;
3568 imageUsage = imgCi.usage;
3569 imageCreateFlags = imgCi.flags;
3570 }
3571
3572 if (hasDedicatedBuffer) {
3573 std::lock_guard<std::recursive_mutex> lock(mLock);
3574
3575 auto it = info_VkBuffer.find(dedicatedAllocInfoPtr->buffer);
3576 if (it == info_VkBuffer.end())
3577 _RETURN_FAILURE_WITH_DEVICE_MEMORY_REPORT(VK_ERROR_INITIALIZATION_FAILED);
3578 const auto& info = it->second;
3579 const auto& bufCi = info.createInfo;
3580
3581 bufferSize = bufCi.size;
3582 }
3583
3584 VkResult ahbCreateRes = createAndroidHardwareBuffer(
3585 mGralloc.get(), hasDedicatedImage, hasDedicatedBuffer, imageExtent, imageLayers,
3586 imageFormat, imageUsage, imageCreateFlags, bufferSize, allocationInfoAllocSize, &ahw);
3587
3588 if (ahbCreateRes != VK_SUCCESS) {
3589 _RETURN_FAILURE_WITH_DEVICE_MEMORY_REPORT(ahbCreateRes);
3590 }
3591 }
3592
3593 if (importAhb) {
3594 ahw = importAhbInfoPtr->buffer;
3595 // We still need to acquire the AHardwareBuffer.
3596 importAndroidHardwareBuffer(mGralloc.get(), importAhbInfoPtr, nullptr);
3597 }
3598
3599 if (ahw) {
3600 const uint32_t hostHandle = mGralloc->getHostHandle(ahw);
3601 if (mGralloc->getFormat(ahw) == AHARDWAREBUFFER_FORMAT_BLOB &&
3602 !mGralloc->treatBlobAsImage()) {
3603 importBufferInfo.buffer = hostHandle;
3604 vk_append_struct(&structChainIter, &importBufferInfo);
3605 } else {
3606 importCbInfo.colorBuffer = hostHandle;
3607 vk_append_struct(&structChainIter, &importCbInfo);
3608 }
3609 }
3610 #endif
3611 zx_handle_t vmo_handle = ZX_HANDLE_INVALID;
3612
3613 #ifdef VK_USE_PLATFORM_FUCHSIA
3614 if (importBufferCollection) {
3615 const auto& collection =
3616 *reinterpret_cast<fidl::WireSyncClient<fuchsia_sysmem::BufferCollection>*>(
3617 importBufferCollectionInfoPtr->collection);
3618 auto result = collection->WaitForBuffersAllocated();
3619 if (!result.ok() || result->status != ZX_OK) {
3620 mesa_loge("WaitForBuffersAllocated failed: %d %d", result.status(),
3621 GET_STATUS_SAFE(result, status));
3622 _RETURN_FAILURE_WITH_DEVICE_MEMORY_REPORT(VK_ERROR_INITIALIZATION_FAILED);
3623 }
3624 fuchsia_sysmem::wire::BufferCollectionInfo2& info = result->buffer_collection_info;
3625 uint32_t index = importBufferCollectionInfoPtr->index;
3626 if (info.buffer_count < index) {
3627 mesa_loge("Invalid buffer index: %d", index);
3628 _RETURN_FAILURE_WITH_DEVICE_MEMORY_REPORT(VK_ERROR_INITIALIZATION_FAILED);
3629 }
3630 vmo_handle = info.buffers[index].vmo.release();
3631 }
3632
3633 if (importVmo) {
3634 vmo_handle = importVmoInfoPtr->handle;
3635 }
3636
3637 if (exportVmo) {
3638 hasDedicatedImage =
3639 dedicatedAllocInfoPtr && (dedicatedAllocInfoPtr->image != VK_NULL_HANDLE);
3640 hasDedicatedBuffer =
3641 dedicatedAllocInfoPtr && (dedicatedAllocInfoPtr->buffer != VK_NULL_HANDLE);
3642
3643 if (hasDedicatedImage && hasDedicatedBuffer) {
3644 mesa_loge(
3645 "Invalid VkMemoryDedicatedAllocationInfo: At least one "
3646 "of image and buffer must be VK_NULL_HANDLE.");
3647 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
3648 }
3649
3650 const VkImageCreateInfo* pImageCreateInfo = nullptr;
3651
3652 VkBufferConstraintsInfoFUCHSIA bufferConstraintsInfo = {
3653 .sType = VK_STRUCTURE_TYPE_BUFFER_COLLECTION_CREATE_INFO_FUCHSIA,
3654 .pNext = nullptr,
3655 .createInfo = {},
3656 .requiredFormatFeatures = 0,
3657 .bufferCollectionConstraints =
3658 VkBufferCollectionConstraintsInfoFUCHSIA{
3659 .sType = VK_STRUCTURE_TYPE_BUFFER_COLLECTION_CONSTRAINTS_INFO_FUCHSIA,
3660 .pNext = nullptr,
3661 .minBufferCount = 1,
3662 .maxBufferCount = 0,
3663 .minBufferCountForCamping = 0,
3664 .minBufferCountForDedicatedSlack = 0,
3665 .minBufferCountForSharedSlack = 0,
3666 },
3667 };
3668 const VkBufferConstraintsInfoFUCHSIA* pBufferConstraintsInfo = nullptr;
3669
3670 if (hasDedicatedImage) {
3671 std::lock_guard<std::recursive_mutex> lock(mLock);
3672
3673 auto it = info_VkImage.find(dedicatedAllocInfoPtr->image);
3674 if (it == info_VkImage.end()) return VK_ERROR_INITIALIZATION_FAILED;
3675 const auto& imageInfo = it->second;
3676
3677 pImageCreateInfo = &imageInfo.createInfo;
3678 }
3679
3680 if (hasDedicatedBuffer) {
3681 std::lock_guard<std::recursive_mutex> lock(mLock);
3682
3683 auto it = info_VkBuffer.find(dedicatedAllocInfoPtr->buffer);
3684 if (it == info_VkBuffer.end()) return VK_ERROR_INITIALIZATION_FAILED;
3685 const auto& bufferInfo = it->second;
3686
3687 bufferConstraintsInfo.createInfo = bufferInfo.createInfo;
3688 pBufferConstraintsInfo = &bufferConstraintsInfo;
3689 }
3690
3691 hasDedicatedImage =
3692 hasDedicatedImage && getBufferCollectionConstraintsVulkanImageUsage(pImageCreateInfo);
3693 hasDedicatedBuffer = hasDedicatedBuffer && getBufferCollectionConstraintsVulkanBufferUsage(
3694 pBufferConstraintsInfo);
3695
3696 if (hasDedicatedImage || hasDedicatedBuffer) {
3697 auto token_ends = fidl::CreateEndpoints<::fuchsia_sysmem::BufferCollectionToken>();
3698 if (!token_ends.is_ok()) {
3699 mesa_loge("zx_channel_create failed: %d", token_ends.status_value());
3700 abort();
3701 }
3702
3703 {
3704 auto result =
3705 mSysmemAllocator->AllocateSharedCollection(std::move(token_ends->server));
3706 if (!result.ok()) {
3707 mesa_loge("AllocateSharedCollection failed: %d", result.status());
3708 abort();
3709 }
3710 }
3711
3712 auto collection_ends = fidl::CreateEndpoints<::fuchsia_sysmem::BufferCollection>();
3713 if (!collection_ends.is_ok()) {
3714 mesa_loge("zx_channel_create failed: %d", collection_ends.status_value());
3715 abort();
3716 }
3717
3718 {
3719 auto result = mSysmemAllocator->BindSharedCollection(
3720 std::move(token_ends->client), std::move(collection_ends->server));
3721 if (!result.ok()) {
3722 mesa_loge("BindSharedCollection failed: %d", result.status());
3723 abort();
3724 }
3725 }
3726
3727 fidl::WireSyncClient<fuchsia_sysmem::BufferCollection> collection(
3728 std::move(collection_ends->client));
3729 if (hasDedicatedImage) {
3730 // TODO(fxbug.dev/42172354): Use setBufferCollectionImageConstraintsFUCHSIA.
3731 VkResult res = setBufferCollectionConstraintsFUCHSIA(enc, device, &collection,
3732 pImageCreateInfo);
3733 if (res == VK_ERROR_FORMAT_NOT_SUPPORTED) {
3734 mesa_loge("setBufferCollectionConstraints failed: format %u is not supported",
3735 pImageCreateInfo->format);
3736 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
3737 }
3738 if (res != VK_SUCCESS) {
3739 mesa_loge("setBufferCollectionConstraints failed: %d", res);
3740 abort();
3741 }
3742 }
3743
3744 if (hasDedicatedBuffer) {
3745 VkResult res = setBufferCollectionBufferConstraintsFUCHSIA(&collection,
3746 pBufferConstraintsInfo);
3747 if (res != VK_SUCCESS) {
3748 mesa_loge("setBufferCollectionBufferConstraints failed: %d", res);
3749 abort();
3750 }
3751 }
3752
3753 {
3754 auto result = collection->WaitForBuffersAllocated();
3755 if (result.ok() && result->status == ZX_OK) {
3756 fuchsia_sysmem::wire::BufferCollectionInfo2& info =
3757 result->buffer_collection_info;
3758 if (!info.buffer_count) {
3759 mesa_loge(
3760 "WaitForBuffersAllocated returned "
3761 "invalid count: %d",
3762 info.buffer_count);
3763 abort();
3764 }
3765 vmo_handle = info.buffers[0].vmo.release();
3766 } else {
3767 mesa_loge("WaitForBuffersAllocated failed: %d %d", result.status(),
3768 GET_STATUS_SAFE(result, status));
3769 abort();
3770 }
3771 }
3772
3773 collection->Close();
3774
3775 zx::vmo vmo_copy;
3776 zx_status_t status = zx_handle_duplicate(vmo_handle, ZX_RIGHT_SAME_RIGHTS,
3777 vmo_copy.reset_and_get_address());
3778 if (status != ZX_OK) {
3779 mesa_loge("Failed to duplicate VMO: %d", status);
3780 abort();
3781 }
3782
3783 if (pImageCreateInfo) {
3784 // Only device-local images need to create color buffer; for
3785 // host-visible images, the color buffer is already created
3786 // when sysmem allocates memory. Here we use the |tiling|
3787 // field of image creation info to determine if it uses
3788 // host-visible memory.
3789 bool isLinear = pImageCreateInfo->tiling == VK_IMAGE_TILING_LINEAR;
3790 if (!isLinear) {
3791 fuchsia_hardware_goldfish::wire::ColorBufferFormatType format;
3792 switch (pImageCreateInfo->format) {
3793 case VK_FORMAT_B8G8R8A8_SINT:
3794 case VK_FORMAT_B8G8R8A8_UNORM:
3795 case VK_FORMAT_B8G8R8A8_SRGB:
3796 case VK_FORMAT_B8G8R8A8_SNORM:
3797 case VK_FORMAT_B8G8R8A8_SSCALED:
3798 case VK_FORMAT_B8G8R8A8_USCALED:
3799 format = fuchsia_hardware_goldfish::wire::ColorBufferFormatType::kBgra;
3800 break;
3801 case VK_FORMAT_R8G8B8A8_SINT:
3802 case VK_FORMAT_R8G8B8A8_UNORM:
3803 case VK_FORMAT_R8G8B8A8_SRGB:
3804 case VK_FORMAT_R8G8B8A8_SNORM:
3805 case VK_FORMAT_R8G8B8A8_SSCALED:
3806 case VK_FORMAT_R8G8B8A8_USCALED:
3807 format = fuchsia_hardware_goldfish::wire::ColorBufferFormatType::kRgba;
3808 break;
3809 case VK_FORMAT_R8_UNORM:
3810 case VK_FORMAT_R8_UINT:
3811 case VK_FORMAT_R8_USCALED:
3812 case VK_FORMAT_R8_SNORM:
3813 case VK_FORMAT_R8_SINT:
3814 case VK_FORMAT_R8_SSCALED:
3815 case VK_FORMAT_R8_SRGB:
3816 format =
3817 fuchsia_hardware_goldfish::wire::ColorBufferFormatType::kLuminance;
3818 break;
3819 case VK_FORMAT_R8G8_UNORM:
3820 case VK_FORMAT_R8G8_UINT:
3821 case VK_FORMAT_R8G8_USCALED:
3822 case VK_FORMAT_R8G8_SNORM:
3823 case VK_FORMAT_R8G8_SINT:
3824 case VK_FORMAT_R8G8_SSCALED:
3825 case VK_FORMAT_R8G8_SRGB:
3826 format = fuchsia_hardware_goldfish::wire::ColorBufferFormatType::kRg;
3827 break;
3828 default:
3829 mesa_loge("Unsupported format: %d", pImageCreateInfo->format);
3830 abort();
3831 }
3832
3833 fidl::Arena arena;
3834 fuchsia_hardware_goldfish::wire::CreateColorBuffer2Params createParams(arena);
3835 createParams.set_width(pImageCreateInfo->extent.width)
3836 .set_height(pImageCreateInfo->extent.height)
3837 .set_format(format)
3838 .set_memory_property(
3839 fuchsia_hardware_goldfish::wire::kMemoryPropertyDeviceLocal);
3840
3841 auto result = mControlDevice->CreateColorBuffer2(std::move(vmo_copy),
3842 std::move(createParams));
3843 if (!result.ok() || result->res != ZX_OK) {
3844 if (result.ok() && result->res == ZX_ERR_ALREADY_EXISTS) {
3845 mesa_logd(
3846 "CreateColorBuffer: color buffer already "
3847 "exists\n");
3848 } else {
3849 mesa_loge("CreateColorBuffer failed: %d:%d", result.status(),
3850 GET_STATUS_SAFE(result, res));
3851 abort();
3852 }
3853 }
3854 }
3855 }
3856
3857 if (pBufferConstraintsInfo) {
3858 fidl::Arena arena;
3859 fuchsia_hardware_goldfish::wire::CreateBuffer2Params createParams(arena);
3860 createParams.set_size(arena, pBufferConstraintsInfo->createInfo.size)
3861 .set_memory_property(
3862 fuchsia_hardware_goldfish::wire::kMemoryPropertyDeviceLocal);
3863
3864 auto result =
3865 mControlDevice->CreateBuffer2(std::move(vmo_copy), std::move(createParams));
3866 if (!result.ok() || result->is_error()) {
3867 mesa_loge("CreateBuffer2 failed: %d:%d", result.status(),
3868 GET_STATUS_SAFE(result, error_value()));
3869 abort();
3870 }
3871 }
3872 } else {
3873 mesa_logw(
3874 "Dedicated image / buffer not available. Cannot create "
3875 "BufferCollection to export VMOs.");
3876 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
3877 }
3878 }
3879
3880 if (vmo_handle != ZX_HANDLE_INVALID) {
3881 zx::vmo vmo_copy;
3882 zx_status_t status =
3883 zx_handle_duplicate(vmo_handle, ZX_RIGHT_SAME_RIGHTS, vmo_copy.reset_and_get_address());
3884 if (status != ZX_OK) {
3885 mesa_loge("Failed to duplicate VMO: %d", status);
3886 abort();
3887 }
3888 zx_status_t status2 = ZX_OK;
3889
3890 auto result = mControlDevice->GetBufferHandle(std::move(vmo_copy));
3891 if (!result.ok() || result->res != ZX_OK) {
3892 mesa_loge("GetBufferHandle failed: %d:%d", result.status(),
3893 GET_STATUS_SAFE(result, res));
3894 } else {
3895 fuchsia_hardware_goldfish::wire::BufferHandleType handle_type = result->type;
3896 uint32_t buffer_handle = result->id;
3897
3898 if (handle_type == fuchsia_hardware_goldfish::wire::BufferHandleType::kBuffer) {
3899 importBufferInfo.buffer = buffer_handle;
3900 vk_append_struct(&structChainIter, &importBufferInfo);
3901 } else {
3902 importCbInfo.colorBuffer = buffer_handle;
3903 vk_append_struct(&structChainIter, &importCbInfo);
3904 }
3905 }
3906 }
3907 #endif
3908
3909 VirtGpuResourcePtr bufferBlob = nullptr;
3910 int importedFd = -1;
3911 #if defined(LINUX_GUEST_BUILD)
3912 // Check for import first; this takes precedence over exportDmabuf in creating the
3913 // VirtGpuResource
3914 if (importDmabuf) {
3915 VirtGpuExternalHandle importHandle = {};
3916 // importBlob impl may close the receivedFd after it creates an GEM handle from it. dup()
3917 // the FD here for input to that impl, then manage the original FD at the Vulkan level
3918 importHandle.osHandle = dup(importFdInfoPtr->fd);
3919 importHandle.type = kMemHandleDmabuf;
3920
3921 auto instance = VirtGpuDevice::getInstance();
3922 bufferBlob = instance->importBlob(importHandle);
3923 if (!bufferBlob) {
3924 mesa_loge("%s: Failed to import colorBuffer resource\n", __func__);
3925 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
3926 }
3927 // As per the Vulkan spec, the ownership of this FD has been transferred
3928 // to the implementation
3929 importedFd = importFdInfoPtr->fd;
3930 } else if (exportDmabuf) {
3931 VirtGpuDevice* instance = VirtGpuDevice::getInstance();
3932 hasDedicatedImage =
3933 dedicatedAllocInfoPtr && (dedicatedAllocInfoPtr->image != VK_NULL_HANDLE);
3934 hasDedicatedBuffer =
3935 dedicatedAllocInfoPtr && (dedicatedAllocInfoPtr->buffer != VK_NULL_HANDLE);
3936
3937 if (hasDedicatedImage) {
3938 VkImageCreateInfo imageCreateInfo;
3939 {
3940 std::lock_guard<std::recursive_mutex> lock(mLock);
3941
3942 auto it = info_VkImage.find(dedicatedAllocInfoPtr->image);
3943 if (it == info_VkImage.end()) return VK_ERROR_INITIALIZATION_FAILED;
3944 const auto& imageInfo = it->second;
3945
3946 imageCreateInfo = imageInfo.createInfo;
3947 }
3948
3949 // Need to query the stride of the underyling image resource
3950 // (VkSubresourceLayout::rowPitch) In most cases, the application will have created the
3951 // VkImage w/ VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT, in which case the aspectMask to
3952 // query is the PLANE_0_BIT resource. Otherwise, query the more generic COLOR_BIT.
3953 // Note: For VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT, the image may actually be emulated
3954 // with VK_IMAGE_TILING_LINEAR.
3955 const VkImageSubresource imageSubresource = {
3956 .aspectMask = (imageCreateInfo.tiling == VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT)
3957 ? VK_IMAGE_ASPECT_MEMORY_PLANE_0_BIT_EXT
3958 : VK_IMAGE_ASPECT_COLOR_BIT,
3959 .mipLevel = 0,
3960 .arrayLayer = 0,
3961 };
3962 VkSubresourceLayout subResourceLayout;
3963 enc->vkGetImageSubresourceLayout(device, dedicatedAllocInfoPtr->image,
3964 &imageSubresource, &subResourceLayout,
3965 true /* do lock */);
3966 if (!subResourceLayout.rowPitch) {
3967 mesa_loge("Failed to query stride for VirtGpu resource creation.");
3968 return VK_ERROR_INITIALIZATION_FAILED;
3969 }
3970
3971 uint32_t virglFormat = gfxstream::vk::getVirglFormat(imageCreateInfo.format);
3972 if (!virglFormat) {
3973 mesa_loge("Unsupported VK format for VirtGpu resource, vkFormat: 0x%x",
3974 imageCreateInfo.format);
3975 return VK_ERROR_FORMAT_NOT_SUPPORTED;
3976 }
3977 const uint32_t target = PIPE_TEXTURE_2D;
3978 uint32_t bind = VIRGL_BIND_RENDER_TARGET;
3979
3980 if (mCaps.vulkanCapset.alwaysBlob) {
3981 struct gfxstreamResourceCreate3d create3d = {};
3982 struct VirtGpuExecBuffer exec = {};
3983 struct gfxstreamPlaceholderCommandVk placeholderCmd = {};
3984 struct VirtGpuCreateBlob createBlob = {};
3985
3986 create3d.hdr.opCode = GFXSTREAM_RESOURCE_CREATE_3D;
3987 create3d.bind = bind;
3988 create3d.target = target;
3989 create3d.format = virglFormat;
3990 create3d.width = imageCreateInfo.extent.width;
3991 create3d.height = imageCreateInfo.extent.height;
3992 create3d.blobId = ++mAtomicId;
3993
3994 createBlob.blobCmd = reinterpret_cast<uint8_t*>(&create3d);
3995 createBlob.blobCmdSize = sizeof(create3d);
3996 createBlob.blobMem = kBlobMemHost3d;
3997 createBlob.flags = kBlobFlagShareable | kBlobFlagCrossDevice;
3998 createBlob.blobId = create3d.blobId;
3999 createBlob.size = finalAllocInfo.allocationSize;
4000
4001 bufferBlob = instance->createBlob(createBlob);
4002 if (!bufferBlob) return VK_ERROR_OUT_OF_DEVICE_MEMORY;
4003
4004 placeholderCmd.hdr.opCode = GFXSTREAM_PLACEHOLDER_COMMAND_VK;
4005 exec.command = static_cast<void*>(&placeholderCmd);
4006 exec.command_size = sizeof(placeholderCmd);
4007 exec.flags = kRingIdx;
4008 exec.ring_idx = 1;
4009 if (instance->execBuffer(exec, bufferBlob.get())) {
4010 mesa_loge("Failed to execbuffer placeholder command.");
4011 return VK_ERROR_OUT_OF_HOST_MEMORY;
4012 }
4013
4014 if (bufferBlob->wait()) {
4015 mesa_loge("Failed to wait for blob.");
4016 return VK_ERROR_OUT_OF_HOST_MEMORY;
4017 }
4018 } else {
4019 bufferBlob = instance->createResource(
4020 imageCreateInfo.extent.width, imageCreateInfo.extent.height,
4021 subResourceLayout.rowPitch,
4022 subResourceLayout.rowPitch * imageCreateInfo.extent.height, virglFormat, target,
4023 bind);
4024 if (!bufferBlob) {
4025 mesa_loge("Failed to create colorBuffer resource for Image memory");
4026 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
4027 }
4028 if (bufferBlob->wait()) {
4029 mesa_loge("Failed to wait for colorBuffer resource for Image memory");
4030 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
4031 }
4032 }
4033 } else if (hasDedicatedBuffer) {
4034 uint32_t virglFormat = VIRGL_FORMAT_R8_UNORM;
4035 const uint32_t target = PIPE_BUFFER;
4036 uint32_t bind = VIRGL_BIND_LINEAR;
4037 uint32_t width = finalAllocInfo.allocationSize;
4038 uint32_t height = 1;
4039
4040 if (mCaps.vulkanCapset.alwaysBlob) {
4041 struct gfxstreamResourceCreate3d create3d = {};
4042 struct VirtGpuExecBuffer exec = {};
4043 struct gfxstreamPlaceholderCommandVk placeholderCmd = {};
4044 struct VirtGpuCreateBlob createBlob = {};
4045
4046 create3d.hdr.opCode = GFXSTREAM_RESOURCE_CREATE_3D;
4047 create3d.bind = bind;
4048 create3d.target = target;
4049 create3d.format = virglFormat;
4050 create3d.width = width;
4051 create3d.height = height;
4052 create3d.blobId = ++mAtomicId;
4053
4054 createBlob.blobCmd = reinterpret_cast<uint8_t*>(&create3d);
4055 createBlob.blobCmdSize = sizeof(create3d);
4056 createBlob.blobMem = kBlobMemHost3d;
4057 createBlob.flags = kBlobFlagShareable | kBlobFlagCrossDevice;
4058 createBlob.blobId = create3d.blobId;
4059 createBlob.size = width;
4060
4061 bufferBlob = instance->createBlob(createBlob);
4062 if (!bufferBlob) return VK_ERROR_OUT_OF_DEVICE_MEMORY;
4063
4064 placeholderCmd.hdr.opCode = GFXSTREAM_PLACEHOLDER_COMMAND_VK;
4065 exec.command = static_cast<void*>(&placeholderCmd);
4066 exec.command_size = sizeof(placeholderCmd);
4067 exec.flags = kRingIdx;
4068 exec.ring_idx = 1;
4069 if (instance->execBuffer(exec, bufferBlob.get())) {
4070 mesa_loge("Failed to allocate coherent memory: failed to execbuffer for wait.");
4071 return VK_ERROR_OUT_OF_HOST_MEMORY;
4072 }
4073
4074 bufferBlob->wait();
4075 } else {
4076 bufferBlob = instance->createResource(width, height, width, width * height,
4077 virglFormat, target, bind);
4078 if (!bufferBlob) {
4079 mesa_loge("Failed to create colorBuffer resource for Image memory");
4080 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
4081 }
4082 if (bufferBlob->wait()) {
4083 mesa_loge("Failed to wait for colorBuffer resource for Image memory");
4084 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
4085 }
4086 }
4087 } else {
4088 mesa_logw(
4089 "VkDeviceMemory is not exportable (VkExportMemoryAllocateInfo). Requires "
4090 "VkMemoryDedicatedAllocateInfo::image to create external resource.");
4091 }
4092 }
4093
4094 if (bufferBlob) {
4095 if (hasDedicatedBuffer) {
4096 importBufferInfo.buffer = bufferBlob->getResourceHandle();
4097 vk_append_struct(&structChainIter, &importBufferInfo);
4098 } else {
4099 importCbInfo.colorBuffer = bufferBlob->getResourceHandle();
4100 vk_append_struct(&structChainIter, &importCbInfo);
4101 }
4102 }
4103 #endif
4104
4105 if (ahw || bufferBlob || !requestedMemoryIsHostVisible) {
4106 input_result =
4107 enc->vkAllocateMemory(device, &finalAllocInfo, pAllocator, pMemory, true /* do lock */);
4108
4109 if (input_result != VK_SUCCESS) _RETURN_FAILURE_WITH_DEVICE_MEMORY_REPORT(input_result);
4110
4111 setDeviceMemoryInfo(device, *pMemory, 0, nullptr, finalAllocInfo.memoryTypeIndex, ahw,
4112 isImport, vmo_handle, bufferBlob, importedFd);
4113
4114 uint64_t memoryObjectId = (uint64_t)(void*)*pMemory;
4115 #ifdef VK_USE_PLATFORM_ANDROID_KHR
4116 if (ahw) {
4117 memoryObjectId = getAHardwareBufferId(ahw);
4118 }
4119 #endif
4120 emitDeviceMemoryReport(info_VkDevice[device],
4121 isImport ? VK_DEVICE_MEMORY_REPORT_EVENT_TYPE_IMPORT_EXT
4122 : VK_DEVICE_MEMORY_REPORT_EVENT_TYPE_ALLOCATE_EXT,
4123 memoryObjectId, pAllocateInfo->allocationSize,
4124 VK_OBJECT_TYPE_DEVICE_MEMORY, (uint64_t)(void*)*pMemory,
4125 pAllocateInfo->memoryTypeIndex);
4126 return VK_SUCCESS;
4127 }
4128
4129 #ifdef VK_USE_PLATFORM_FUCHSIA
4130 if (vmo_handle != ZX_HANDLE_INVALID) {
4131 input_result =
4132 enc->vkAllocateMemory(device, &finalAllocInfo, pAllocator, pMemory, true /* do lock */);
4133
4134 // Get VMO handle rights, and only use allowed rights to map the
4135 // host memory.
4136 zx_info_handle_basic handle_info;
4137 zx_status_t status = zx_object_get_info(vmo_handle, ZX_INFO_HANDLE_BASIC, &handle_info,
4138 sizeof(handle_info), nullptr, nullptr);
4139 if (status != ZX_OK) {
4140 mesa_loge("%s: cannot get vmo object info: vmo = %u status: %d.", __func__, vmo_handle,
4141 status);
4142 return VK_ERROR_OUT_OF_HOST_MEMORY;
4143 }
4144
4145 zx_vm_option_t vm_permission = 0u;
4146 vm_permission |= (handle_info.rights & ZX_RIGHT_READ) ? ZX_VM_PERM_READ : 0;
4147 vm_permission |= (handle_info.rights & ZX_RIGHT_WRITE) ? ZX_VM_PERM_WRITE : 0;
4148
4149 zx_paddr_t addr;
4150 status = zx_vmar_map(zx_vmar_root_self(), vm_permission, 0, vmo_handle, 0,
4151 finalAllocInfo.allocationSize, &addr);
4152 if (status != ZX_OK) {
4153 mesa_loge("%s: cannot map vmar: status %d.", __func__, status);
4154 return VK_ERROR_OUT_OF_HOST_MEMORY;
4155 }
4156
4157 setDeviceMemoryInfo(device, *pMemory, finalAllocInfo.allocationSize,
4158 reinterpret_cast<uint8_t*>(addr), finalAllocInfo.memoryTypeIndex,
4159 /*ahw=*/nullptr, isImport, vmo_handle, /*blobPtr=*/nullptr,
4160 /*importedFd=*/-1);
4161 return VK_SUCCESS;
4162 }
4163 #endif
4164
4165 // Host visible memory with direct mapping
4166 VkResult result = getCoherentMemory(&finalAllocInfo, enc, device, pMemory);
4167 if (result != VK_SUCCESS) return result;
4168
4169 uint64_t memoryObjectId = (uint64_t)(void*)*pMemory;
4170
4171 #ifdef VK_USE_PLATFORM_ANDROID_KHR
4172 if (ahw) {
4173 memoryObjectId = getAHardwareBufferId(ahw);
4174 }
4175 #endif
4176
4177 emitDeviceMemoryReport(info_VkDevice[device],
4178 isImport ? VK_DEVICE_MEMORY_REPORT_EVENT_TYPE_IMPORT_EXT
4179 : VK_DEVICE_MEMORY_REPORT_EVENT_TYPE_ALLOCATE_EXT,
4180 memoryObjectId, pAllocateInfo->allocationSize,
4181 VK_OBJECT_TYPE_DEVICE_MEMORY, (uint64_t)(void*)*pMemory,
4182 pAllocateInfo->memoryTypeIndex);
4183 return VK_SUCCESS;
4184 }
4185
4186 void ResourceTracker::on_vkFreeMemory(void* context, VkDevice device, VkDeviceMemory memory,
4187 const VkAllocationCallbacks* pAllocateInfo) {
4188 std::unique_lock<std::recursive_mutex> lock(mLock);
4189
4190 auto it = info_VkDeviceMemory.find(memory);
4191 if (it == info_VkDeviceMemory.end()) return;
4192 auto& info = it->second;
4193 uint64_t memoryObjectId = (uint64_t)(void*)memory;
4194 #ifdef VK_USE_PLATFORM_ANDROID_KHR
4195 if (info.ahw) {
4196 memoryObjectId = getAHardwareBufferId(info.ahw);
4197 }
4198 #endif
4199 if (info.importedFd >= 0) {
4200 close(info.importedFd);
4201 info.importedFd = -1;
4202 }
4203
4204 emitDeviceMemoryReport(info_VkDevice[device],
4205 info.imported ? VK_DEVICE_MEMORY_REPORT_EVENT_TYPE_UNIMPORT_EXT
4206 : VK_DEVICE_MEMORY_REPORT_EVENT_TYPE_FREE_EXT,
4207 memoryObjectId, 0 /* size */, VK_OBJECT_TYPE_DEVICE_MEMORY,
4208 (uint64_t)(void*)memory);
4209
4210 #ifdef VK_USE_PLATFORM_FUCHSIA
4211 if (info.vmoHandle && info.ptr) {
4212 zx_status_t status = zx_vmar_unmap(
4213 zx_vmar_root_self(), reinterpret_cast<zx_paddr_t>(info.ptr), info.allocationSize);
4214 if (status != ZX_OK) {
4215 mesa_loge("%s: Cannot unmap ptr: status %d", __func__, status);
4216 }
4217 info.ptr = nullptr;
4218 }
4219 #endif
4220
4221 if (!info.coherentMemory) {
4222 lock.unlock();
4223 VkEncoder* enc = (VkEncoder*)context;
4224 enc->vkFreeMemory(device, memory, pAllocateInfo, true /* do lock */);
4225 return;
4226 }
4227
4228 auto coherentMemory = freeCoherentMemoryLocked(memory, info);
4229
4230 // We have to release the lock before we could possibly free a
4231 // CoherentMemory, because that will call into VkEncoder, which
4232 // shouldn't be called when the lock is held.
4233 lock.unlock();
4234 coherentMemory = nullptr;
4235 }
4236
4237 VkResult ResourceTracker::on_vkMapMemory(void* context, VkResult host_result, VkDevice device,
4238 VkDeviceMemory memory, VkDeviceSize offset,
4239 VkDeviceSize size, VkMemoryMapFlags, void** ppData) {
4240 if (host_result != VK_SUCCESS) {
4241 mesa_loge("%s: Host failed to map", __func__);
4242 return host_result;
4243 }
4244
4245 std::unique_lock<std::recursive_mutex> lock(mLock);
4246
4247 auto deviceMemoryInfoIt = info_VkDeviceMemory.find(memory);
4248 if (deviceMemoryInfoIt == info_VkDeviceMemory.end()) {
4249 mesa_loge("%s: Failed to find VkDeviceMemory.", __func__);
4250 return VK_ERROR_MEMORY_MAP_FAILED;
4251 }
4252 auto& deviceMemoryInfo = deviceMemoryInfoIt->second;
4253
4254 if (deviceMemoryInfo.blobId && !deviceMemoryInfo.coherentMemory &&
4255 !mCaps.params[kParamCreateGuestHandle]) {
4256 // NOTE: must not hold lock while calling into the encoder.
4257 lock.unlock();
4258 VkEncoder* enc = (VkEncoder*)context;
4259 VkResult vkResult = enc->vkGetBlobGOOGLE(device, memory, /*doLock*/ false);
4260 if (vkResult != VK_SUCCESS) {
4261 mesa_loge("%s: Failed to vkGetBlobGOOGLE().", __func__);
4262 return vkResult;
4263 }
4264 lock.lock();
4265
4266 // NOTE: deviceMemoryInfoIt potentially invalidated but deviceMemoryInfo still okay.
4267
4268 struct VirtGpuCreateBlob createBlob = {};
4269 createBlob.blobMem = kBlobMemHost3d;
4270 createBlob.flags = kBlobFlagMappable;
4271 createBlob.blobId = deviceMemoryInfo.blobId;
4272 createBlob.size = deviceMemoryInfo.coherentMemorySize;
4273
4274 auto blob = VirtGpuDevice::getInstance()->createBlob(createBlob);
4275 if (!blob) return VK_ERROR_OUT_OF_DEVICE_MEMORY;
4276
4277 VirtGpuResourceMappingPtr mapping = blob->createMapping();
4278 if (!mapping) return VK_ERROR_OUT_OF_DEVICE_MEMORY;
4279
4280 auto coherentMemory =
4281 std::make_shared<CoherentMemory>(mapping, createBlob.size, device, memory);
4282
4283 uint8_t* ptr;
4284 uint64_t offset;
4285 coherentMemory->subAllocate(deviceMemoryInfo.allocationSize, &ptr, offset);
4286
4287 deviceMemoryInfo.coherentMemoryOffset = offset;
4288 deviceMemoryInfo.coherentMemory = coherentMemory;
4289 deviceMemoryInfo.ptr = ptr;
4290 }
4291
4292 if (!deviceMemoryInfo.ptr) {
4293 mesa_loge("%s: VkDeviceMemory has nullptr.", __func__);
4294 return VK_ERROR_MEMORY_MAP_FAILED;
4295 }
4296
4297 if (size != VK_WHOLE_SIZE && (deviceMemoryInfo.ptr + offset + size >
4298 deviceMemoryInfo.ptr + deviceMemoryInfo.allocationSize)) {
4299 mesa_loge(
4300 "%s: size is too big. alloc size 0x%llx while we wanted offset 0x%llx size 0x%llx "
4301 "total 0x%llx",
4302 __func__, (unsigned long long)deviceMemoryInfo.allocationSize,
4303 (unsigned long long)offset, (unsigned long long)size, (unsigned long long)offset);
4304 return VK_ERROR_MEMORY_MAP_FAILED;
4305 }
4306
4307 *ppData = deviceMemoryInfo.ptr + offset;
4308
4309 return host_result;
4310 }
4311
4312 void ResourceTracker::on_vkUnmapMemory(void*, VkDevice, VkDeviceMemory) {
4313 // no-op
4314 }
4315
4316 void ResourceTracker::transformImageMemoryRequirements2ForGuest(VkImage image,
4317 VkMemoryRequirements2* reqs2) {
4318 std::lock_guard<std::recursive_mutex> lock(mLock);
4319
4320 auto it = info_VkImage.find(image);
4321 if (it == info_VkImage.end()) return;
4322
4323 auto& info = it->second;
4324
4325 if (!info.external || !info.externalCreateInfo.handleTypes) {
4326 transformImageMemoryRequirementsForGuestLocked(image, &reqs2->memoryRequirements);
4327 return;
4328 }
4329
4330 transformImageMemoryRequirementsForGuestLocked(image, &reqs2->memoryRequirements);
4331
4332 VkMemoryDedicatedRequirements* dedicatedReqs =
4333 vk_find_struct(reqs2, MEMORY_DEDICATED_REQUIREMENTS);
4334
4335 if (!dedicatedReqs) return;
4336
4337 transformExternalResourceMemoryDedicatedRequirementsForGuest(dedicatedReqs);
4338 }
4339
4340 void ResourceTracker::transformBufferMemoryRequirements2ForGuest(VkBuffer buffer,
4341 VkMemoryRequirements2* reqs2) {
4342 std::lock_guard<std::recursive_mutex> lock(mLock);
4343
4344 auto it = info_VkBuffer.find(buffer);
4345 if (it == info_VkBuffer.end()) return;
4346
4347 auto& info = it->second;
4348
4349 if (!info.external || !info.externalCreateInfo.handleTypes) {
4350 return;
4351 }
4352
4353 VkMemoryDedicatedRequirements* dedicatedReqs =
4354 vk_find_struct(reqs2, MEMORY_DEDICATED_REQUIREMENTS);
4355
4356 if (!dedicatedReqs) return;
4357
4358 transformExternalResourceMemoryDedicatedRequirementsForGuest(dedicatedReqs);
4359 }
4360
4361 VkResult ResourceTracker::on_vkCreateImage(void* context, VkResult, VkDevice device,
4362 const VkImageCreateInfo* pCreateInfo,
4363 const VkAllocationCallbacks* pAllocator,
4364 VkImage* pImage) {
4365 VkEncoder* enc = (VkEncoder*)context;
4366
4367 VkImageCreateInfo localCreateInfo = vk_make_orphan_copy(*pCreateInfo);
4368 if (localCreateInfo.sharingMode != VK_SHARING_MODE_CONCURRENT) {
4369 localCreateInfo.queueFamilyIndexCount = 0;
4370 localCreateInfo.pQueueFamilyIndices = nullptr;
4371 }
4372
4373 vk_struct_chain_iterator structChainIter = vk_make_chain_iterator(&localCreateInfo);
4374 VkExternalMemoryImageCreateInfo localExtImgCi;
4375
4376 const VkExternalMemoryImageCreateInfo* extImgCiPtr =
4377 vk_find_struct_const(pCreateInfo, EXTERNAL_MEMORY_IMAGE_CREATE_INFO);
4378
4379 if (extImgCiPtr) {
4380 localExtImgCi = vk_make_orphan_copy(*extImgCiPtr);
4381 vk_append_struct(&structChainIter, &localExtImgCi);
4382 }
4383
4384 #if defined(LINUX_GUEST_BUILD)
4385 VkImageDrmFormatModifierExplicitCreateInfoEXT localDrmFormatModifierInfo;
4386 VkImageDrmFormatModifierListCreateInfoEXT localDrmFormatModifierList;
4387
4388 // If the VkImage will be bound to guest-dmabuf memory
4389 if (extImgCiPtr &&
4390 (extImgCiPtr->handleTypes & VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT)) {
4391 const wsi_image_create_info* wsiImageCi =
4392 vk_find_struct_const(pCreateInfo, WSI_IMAGE_CREATE_INFO_MESA);
4393 if (wsiImageCi && wsiImageCi->scanout) {
4394 // Linux WSI creates swapchain images with VK_IMAGE_CREATE_ALIAS_BIT. Vulkan spec
4395 // states: "If the pNext chain includes a VkExternalMemoryImageCreateInfo or
4396 // VkExternalMemoryImageCreateInfoNV structure whose handleTypes member is not 0, it is
4397 // as if VK_IMAGE_CREATE_ALIAS_BIT is set." To avoid flag mismatches on host driver,
4398 // remove the VK_IMAGE_CREATE_ALIAS_BIT here.
4399 localCreateInfo.flags &= ~VK_IMAGE_CREATE_ALIAS_BIT;
4400 }
4401
4402 const VkImageDrmFormatModifierExplicitCreateInfoEXT* drmFmtMod =
4403 vk_find_struct_const(pCreateInfo, IMAGE_DRM_FORMAT_MODIFIER_EXPLICIT_CREATE_INFO_EXT);
4404 const VkImageDrmFormatModifierListCreateInfoEXT* drmFmtModList =
4405 vk_find_struct_const(pCreateInfo, IMAGE_DRM_FORMAT_MODIFIER_LIST_CREATE_INFO_EXT);
4406 if ((pCreateInfo->tiling == VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT) &&
4407 (drmFmtMod || drmFmtModList)) {
4408 VkPhysicalDevice physicalDevice;
4409 {
4410 std::lock_guard<std::recursive_mutex> lock(mLock);
4411 auto it = info_VkDevice.find(device);
4412 if (it == info_VkDevice.end()) return VK_ERROR_UNKNOWN;
4413 physicalDevice = it->second.physdev;
4414 }
4415 if (doImageDrmFormatModifierEmulation(physicalDevice)) {
4416 bool canUseLinearModifier =
4417 (drmFmtMod && drmFmtMod->drmFormatModifier == DRM_FORMAT_MOD_LINEAR) ||
4418 std::any_of(
4419 drmFmtModList->pDrmFormatModifiers,
4420 drmFmtModList->pDrmFormatModifiers + drmFmtModList->drmFormatModifierCount,
4421 [](const uint64_t mod) { return mod == DRM_FORMAT_MOD_LINEAR; });
4422 // host doesn't support DRM format modifiers, try emulating
4423 if (canUseLinearModifier) {
4424 mesa_logd(
4425 "vkCreateImage: emulating DRM_FORMAT_MOD_LINEAR with "
4426 "VK_IMAGE_TILING_LINEAR");
4427 localCreateInfo.tiling = VK_IMAGE_TILING_LINEAR;
4428 } else {
4429 mesa_loge(
4430 "Host does not support DRM format modifiers; DRM_FORMAT_MOD_LINEAR must be "
4431 "provided, as it is the only format modifier that can be emulated");
4432 return VK_ERROR_FORMAT_NOT_SUPPORTED;
4433 }
4434 } else {
4435 // host supports DRM format modifiers => forward the struct
4436 if (drmFmtMod) {
4437 localDrmFormatModifierInfo = vk_make_orphan_copy(*drmFmtMod);
4438 vk_append_struct(&structChainIter, &localDrmFormatModifierInfo);
4439 }
4440 if (drmFmtModList) {
4441 localDrmFormatModifierList = vk_make_orphan_copy(*drmFmtModList);
4442 vk_append_struct(&structChainIter, &localDrmFormatModifierList);
4443 }
4444 }
4445 }
4446 }
4447 #endif
4448
4449 #ifdef VK_USE_PLATFORM_ANDROID_KHR
4450 VkNativeBufferANDROID localAnb;
4451 const VkNativeBufferANDROID* anbInfoPtr = vk_find_struct_const(pCreateInfo, NATIVE_BUFFER_ANDROID);
4452 if (anbInfoPtr) {
4453 localAnb = vk_make_orphan_copy(*anbInfoPtr);
4454 vk_append_struct(&structChainIter, &localAnb);
4455 }
4456
4457 VkExternalFormatANDROID localExtFormatAndroid;
4458 const VkExternalFormatANDROID* extFormatAndroidPtr =
4459 vk_find_struct_const(pCreateInfo, EXTERNAL_FORMAT_ANDROID);
4460 if (extFormatAndroidPtr) {
4461 localExtFormatAndroid = vk_make_orphan_copy(*extFormatAndroidPtr);
4462
4463 // Do not append external format android;
4464 // instead, replace the local image localCreateInfo format
4465 // with the corresponding Vulkan format
4466 if (extFormatAndroidPtr->externalFormat) {
4467 localCreateInfo.format = vk_format_from_fourcc(extFormatAndroidPtr->externalFormat);
4468 if (localCreateInfo.format == VK_FORMAT_UNDEFINED)
4469 return VK_ERROR_VALIDATION_FAILED_EXT;
4470 }
4471 }
4472 #endif
4473
4474 #ifdef VK_USE_PLATFORM_FUCHSIA
4475 const VkBufferCollectionImageCreateInfoFUCHSIA* extBufferCollectionPtr =
4476 vk_find_struct_const(pCreateInfo, BUFFER_COLLECTION_IMAGE_CREATE_INFO_FUCHSIA);
4477
4478 bool isSysmemBackedMemory = false;
4479
4480 if (extImgCiPtr &&
4481 (extImgCiPtr->handleTypes & VK_EXTERNAL_MEMORY_HANDLE_TYPE_ZIRCON_VMO_BIT_FUCHSIA)) {
4482 isSysmemBackedMemory = true;
4483 }
4484
4485 if (extBufferCollectionPtr) {
4486 const auto& collection =
4487 *reinterpret_cast<fidl::WireSyncClient<fuchsia_sysmem::BufferCollection>*>(
4488 extBufferCollectionPtr->collection);
4489 uint32_t index = extBufferCollectionPtr->index;
4490 zx::vmo vmo;
4491
4492 fuchsia_sysmem::wire::BufferCollectionInfo2 info;
4493
4494 auto result = collection->WaitForBuffersAllocated();
4495 if (result.ok() && result->status == ZX_OK) {
4496 info = std::move(result->buffer_collection_info);
4497 if (index < info.buffer_count && info.settings.has_image_format_constraints) {
4498 vmo = std::move(info.buffers[index].vmo);
4499 }
4500 } else {
4501 mesa_loge("WaitForBuffersAllocated failed: %d %d", result.status(),
4502 GET_STATUS_SAFE(result, status));
4503 }
4504
4505 if (vmo.is_valid()) {
4506 zx::vmo vmo_dup;
4507 if (zx_status_t status = vmo.duplicate(ZX_RIGHT_SAME_RIGHTS, &vmo_dup);
4508 status != ZX_OK) {
4509 mesa_loge("%s: zx_vmo_duplicate failed: %d", __func__, status);
4510 abort();
4511 }
4512
4513 auto buffer_handle_result = mControlDevice->GetBufferHandle(std::move(vmo_dup));
4514 if (!buffer_handle_result.ok()) {
4515 mesa_loge("%s: GetBufferHandle FIDL error: %d", __func__,
4516 buffer_handle_result.status());
4517 abort();
4518 }
4519 if (buffer_handle_result.value().res == ZX_OK) {
4520 // Buffer handle already exists.
4521 // If it is a ColorBuffer, no-op; Otherwise return error.
4522 if (buffer_handle_result.value().type !=
4523 fuchsia_hardware_goldfish::wire::BufferHandleType::kColorBuffer) {
4524 mesa_loge("%s: BufferHandle %u is not a ColorBuffer", __func__,
4525 buffer_handle_result.value().id);
4526 return VK_ERROR_OUT_OF_HOST_MEMORY;
4527 }
4528 } else if (buffer_handle_result.value().res == ZX_ERR_NOT_FOUND) {
4529 // Buffer handle not found. Create ColorBuffer based on buffer settings.
4530 auto format = info.settings.image_format_constraints.pixel_format.type ==
4531 fuchsia_sysmem::wire::PixelFormatType::kR8G8B8A8
4532 ? fuchsia_hardware_goldfish::wire::ColorBufferFormatType::kRgba
4533 : fuchsia_hardware_goldfish::wire::ColorBufferFormatType::kBgra;
4534
4535 uint32_t memory_property =
4536 info.settings.buffer_settings.heap ==
4537 fuchsia_sysmem::wire::HeapType::kGoldfishDeviceLocal
4538 ? fuchsia_hardware_goldfish::wire::kMemoryPropertyDeviceLocal
4539 : fuchsia_hardware_goldfish::wire::kMemoryPropertyHostVisible;
4540
4541 fidl::Arena arena;
4542 fuchsia_hardware_goldfish::wire::CreateColorBuffer2Params createParams(arena);
4543 createParams.set_width(info.settings.image_format_constraints.min_coded_width)
4544 .set_height(info.settings.image_format_constraints.min_coded_height)
4545 .set_format(format)
4546 .set_memory_property(memory_property);
4547
4548 auto result =
4549 mControlDevice->CreateColorBuffer2(std::move(vmo), std::move(createParams));
4550 if (result.ok() && result->res == ZX_ERR_ALREADY_EXISTS) {
4551 mesa_logd("CreateColorBuffer: color buffer already exists\n");
4552 } else if (!result.ok() || result->res != ZX_OK) {
4553 mesa_loge("CreateColorBuffer failed: %d:%d", result.status(),
4554 GET_STATUS_SAFE(result, res));
4555 }
4556 }
4557
4558 if (info.settings.buffer_settings.heap ==
4559 fuchsia_sysmem::wire::HeapType::kGoldfishHostVisible) {
4560 mesa_logd(
4561 "%s: Image uses host visible memory heap; set tiling "
4562 "to linear to match host ImageCreateInfo",
4563 __func__);
4564 localCreateInfo.tiling = VK_IMAGE_TILING_LINEAR;
4565 }
4566 }
4567 isSysmemBackedMemory = true;
4568 }
4569
4570 if (isSysmemBackedMemory) {
4571 localCreateInfo.flags |= VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT;
4572 }
4573 #endif
4574
4575 VkResult res;
4576 VkMemoryRequirements memReqs;
4577
4578 if (supportsCreateResourcesWithRequirements()) {
4579 res = enc->vkCreateImageWithRequirementsGOOGLE(device, &localCreateInfo, pAllocator, pImage,
4580 &memReqs, true /* do lock */);
4581 } else {
4582 res = enc->vkCreateImage(device, &localCreateInfo, pAllocator, pImage, true /* do lock */);
4583 }
4584
4585 if (res != VK_SUCCESS) return res;
4586
4587 std::lock_guard<std::recursive_mutex> lock(mLock);
4588
4589 auto it = info_VkImage.find(*pImage);
4590 if (it == info_VkImage.end()) return VK_ERROR_INITIALIZATION_FAILED;
4591
4592 auto& info = it->second;
4593
4594 info.device = device;
4595 info.createInfo = *pCreateInfo;
4596 info.createInfo.pNext = nullptr;
4597
4598 #ifdef VK_USE_PLATFORM_ANDROID_KHR
4599 if (extFormatAndroidPtr && extFormatAndroidPtr->externalFormat) {
4600 info.hasExternalFormat = true;
4601 info.externalFourccFormat = extFormatAndroidPtr->externalFormat;
4602 }
4603 info.hasAnb = (anbInfoPtr != nullptr);
4604 #endif // VK_USE_PLATFORM_ANDROID_KHR
4605
4606 if (supportsCreateResourcesWithRequirements()) {
4607 info.baseRequirementsKnown = true;
4608 }
4609
4610 if (extImgCiPtr) {
4611 info.external = true;
4612 info.externalCreateInfo = *extImgCiPtr;
4613 }
4614
4615 #ifdef VK_USE_PLATFORM_FUCHSIA
4616 if (isSysmemBackedMemory) {
4617 info.isSysmemBackedMemory = true;
4618 }
4619 #endif
4620
4621 // Delete `protocolVersion` check goldfish drivers are gone.
4622 #if defined(VK_USE_PLATFORM_ANDROID_KHR)
4623 if (mCaps.vulkanCapset.colorBufferMemoryIndex == 0xFFFFFFFF) {
4624 mCaps.vulkanCapset.colorBufferMemoryIndex = getColorBufferMemoryIndex(context, device);
4625 }
4626 if ((extImgCiPtr && (extImgCiPtr->handleTypes &
4627 VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID))) {
4628 updateMemoryTypeBits(&memReqs.memoryTypeBits, mCaps.vulkanCapset.colorBufferMemoryIndex);
4629 }
4630 #endif
4631 #if defined(LINUX_GUEST_BUILD)
4632 if (mCaps.vulkanCapset.colorBufferMemoryIndex == 0xFFFFFFFF) {
4633 mCaps.vulkanCapset.colorBufferMemoryIndex = getColorBufferMemoryIndex(context, device);
4634 }
4635 if (extImgCiPtr &&
4636 (extImgCiPtr->handleTypes & VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT)) {
4637 updateMemoryTypeBits(&memReqs.memoryTypeBits, mCaps.vulkanCapset.colorBufferMemoryIndex);
4638 }
4639 #endif
4640
4641 if (info.baseRequirementsKnown) {
4642 transformImageMemoryRequirementsForGuestLocked(*pImage, &memReqs);
4643 info.baseRequirements = memReqs;
4644 }
4645 return res;
4646 }
4647
4648 VkResult ResourceTracker::on_vkCreateSamplerYcbcrConversion(
4649 void* context, VkResult, VkDevice device, const VkSamplerYcbcrConversionCreateInfo* pCreateInfo,
4650 const VkAllocationCallbacks* pAllocator, VkSamplerYcbcrConversion* pYcbcrConversion) {
4651 VkSamplerYcbcrConversionCreateInfo localCreateInfo = vk_make_orphan_copy(*pCreateInfo);
4652
4653 #ifdef VK_USE_PLATFORM_ANDROID_KHR
4654 const VkExternalFormatANDROID* extFormatAndroidPtr =
4655 vk_find_struct_const(pCreateInfo, EXTERNAL_FORMAT_ANDROID);
4656 if (extFormatAndroidPtr) {
4657 if (extFormatAndroidPtr->externalFormat == DRM_FORMAT_RGB565) {
4658 // We don't support external formats on host and it causes RGB565
4659 // to fail in CtsGraphicsTestCases android.graphics.cts.BasicVulkanGpuTest
4660 // when passed as an external format.
4661 // We may consider doing this for all external formats.
4662 // See b/134771579.
4663 *pYcbcrConversion = VK_YCBCR_CONVERSION_DO_NOTHING;
4664 return VK_SUCCESS;
4665 } else if (extFormatAndroidPtr->externalFormat) {
4666 localCreateInfo.format = vk_format_from_fourcc(extFormatAndroidPtr->externalFormat);
4667 }
4668 }
4669 #endif
4670
4671 VkEncoder* enc = (VkEncoder*)context;
4672 VkResult res = enc->vkCreateSamplerYcbcrConversion(device, &localCreateInfo, pAllocator,
4673 pYcbcrConversion, true /* do lock */);
4674
4675 if (*pYcbcrConversion == VK_YCBCR_CONVERSION_DO_NOTHING) {
4676 mesa_loge(
4677 "FATAL: vkCreateSamplerYcbcrConversion returned a reserved value "
4678 "(VK_YCBCR_CONVERSION_DO_NOTHING)");
4679 abort();
4680 }
4681 return res;
4682 }
4683
4684 void ResourceTracker::on_vkDestroySamplerYcbcrConversion(void* context, VkDevice device,
4685 VkSamplerYcbcrConversion ycbcrConversion,
4686 const VkAllocationCallbacks* pAllocator) {
4687 VkEncoder* enc = (VkEncoder*)context;
4688 if (ycbcrConversion != VK_YCBCR_CONVERSION_DO_NOTHING) {
4689 enc->vkDestroySamplerYcbcrConversion(device, ycbcrConversion, pAllocator,
4690 true /* do lock */);
4691 }
4692 }
4693
4694 VkResult ResourceTracker::on_vkCreateSampler(void* context, VkResult, VkDevice device,
4695 const VkSamplerCreateInfo* pCreateInfo,
4696 const VkAllocationCallbacks* pAllocator,
4697 VkSampler* pSampler) {
4698 VkSamplerCreateInfo localCreateInfo = vk_make_orphan_copy(*pCreateInfo);
4699
4700 vk_struct_chain_iterator structChainIter = vk_make_chain_iterator(&localCreateInfo);
4701 #if defined(VK_USE_PLATFORM_ANDROID_KHR) || defined(VK_USE_PLATFORM_FUCHSIA)
4702 VkSamplerYcbcrConversionInfo localVkSamplerYcbcrConversionInfo;
4703 const VkSamplerYcbcrConversionInfo* samplerYcbcrConversionInfo =
4704 vk_find_struct_const(pCreateInfo, SAMPLER_YCBCR_CONVERSION_INFO);
4705 if (samplerYcbcrConversionInfo) {
4706 if (samplerYcbcrConversionInfo->conversion != VK_YCBCR_CONVERSION_DO_NOTHING) {
4707 localVkSamplerYcbcrConversionInfo = vk_make_orphan_copy(*samplerYcbcrConversionInfo);
4708 vk_append_struct(&structChainIter, &localVkSamplerYcbcrConversionInfo);
4709 }
4710 }
4711
4712 VkSamplerCustomBorderColorCreateInfoEXT localVkSamplerCustomBorderColorCreateInfo;
4713 const VkSamplerCustomBorderColorCreateInfoEXT* samplerCustomBorderColorCreateInfo =
4714 vk_find_struct_const(pCreateInfo, SAMPLER_CUSTOM_BORDER_COLOR_CREATE_INFO_EXT);
4715 if (samplerCustomBorderColorCreateInfo) {
4716 localVkSamplerCustomBorderColorCreateInfo =
4717 vk_make_orphan_copy(*samplerCustomBorderColorCreateInfo);
4718 vk_append_struct(&structChainIter, &localVkSamplerCustomBorderColorCreateInfo);
4719 }
4720 #endif
4721
4722 VkSamplerReductionModeCreateInfo localVkSamplerReductionModeCreateInfo;
4723 const VkSamplerReductionModeCreateInfo* samplerReductionModeCreateInfo =
4724 vk_find_struct_const(pCreateInfo, SAMPLER_REDUCTION_MODE_CREATE_INFO);
4725 if (samplerReductionModeCreateInfo) {
4726 localVkSamplerReductionModeCreateInfo =
4727 vk_make_orphan_copy(*samplerReductionModeCreateInfo);
4728 vk_append_struct(&structChainIter, &localVkSamplerReductionModeCreateInfo);
4729 }
4730
4731 VkEncoder* enc = (VkEncoder*)context;
4732 return enc->vkCreateSampler(device, &localCreateInfo, pAllocator, pSampler, true /* do lock */);
4733 }
4734
4735 void ResourceTracker::on_vkGetPhysicalDeviceExternalFenceProperties(
4736 void* context, VkPhysicalDevice physicalDevice,
4737 const VkPhysicalDeviceExternalFenceInfo* pExternalFenceInfo,
4738 VkExternalFenceProperties* pExternalFenceProperties) {
4739 (void)context;
4740 (void)physicalDevice;
4741
4742 pExternalFenceProperties->exportFromImportedHandleTypes = 0;
4743 pExternalFenceProperties->compatibleHandleTypes = 0;
4744 pExternalFenceProperties->externalFenceFeatures = 0;
4745
4746 bool syncFd = pExternalFenceInfo->handleType & VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT;
4747
4748 if (!syncFd) {
4749 return;
4750 }
4751
4752 #if defined(VK_USE_PLATFORM_ANDROID_KHR) || DETECT_OS_LINUX
4753 pExternalFenceProperties->exportFromImportedHandleTypes =
4754 VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT;
4755 pExternalFenceProperties->compatibleHandleTypes = VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT;
4756 pExternalFenceProperties->externalFenceFeatures =
4757 VK_EXTERNAL_FENCE_FEATURE_IMPORTABLE_BIT | VK_EXTERNAL_FENCE_FEATURE_EXPORTABLE_BIT;
4758 #endif
4759 }
4760
4761 VkResult ResourceTracker::on_vkCreateFence(void* context, VkResult input_result, VkDevice device,
4762 const VkFenceCreateInfo* pCreateInfo,
4763 const VkAllocationCallbacks* pAllocator,
4764 VkFence* pFence) {
4765 VkEncoder* enc = (VkEncoder*)context;
4766 VkFenceCreateInfo finalCreateInfo = *pCreateInfo;
4767
4768 const VkExportFenceCreateInfo* exportFenceInfoPtr =
4769 vk_find_struct_const(pCreateInfo, EXPORT_FENCE_CREATE_INFO);
4770
4771 #if defined(VK_USE_PLATFORM_ANDROID_KHR) || DETECT_OS_LINUX
4772 bool exportSyncFd = exportFenceInfoPtr && (exportFenceInfoPtr->handleTypes &
4773 VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT);
4774 #endif
4775
4776 input_result =
4777 enc->vkCreateFence(device, &finalCreateInfo, pAllocator, pFence, true /* do lock */);
4778
4779 if (input_result != VK_SUCCESS) return input_result;
4780
4781 #if defined(VK_USE_PLATFORM_ANDROID_KHR) || DETECT_OS_LINUX
4782 if (exportSyncFd) {
4783 if (!mFeatureInfo.hasVirtioGpuNativeSync) {
4784 MESA_TRACE_SCOPE("%s: ensure sync device\n", __func__);
4785 ensureSyncDeviceFd();
4786 }
4787
4788 MESA_TRACE_SCOPE("%s: getting fence info\n", __func__);
4789 std::lock_guard<std::recursive_mutex> lock(mLock);
4790 auto it = info_VkFence.find(*pFence);
4791
4792 if (it == info_VkFence.end()) return VK_ERROR_INITIALIZATION_FAILED;
4793
4794 auto& info = it->second;
4795
4796 info.external = true;
4797 info.exportFenceCreateInfo = *exportFenceInfoPtr;
4798 MESA_TRACE_SCOPE("%s: info set (fence still -1). fence: %p\n", __func__, (void*)(*pFence));
4799 // syncFd is still -1 because we expect user to explicitly
4800 // export it via vkGetFenceFdKHR
4801 }
4802 #endif
4803
4804 return input_result;
4805 }
4806
4807 void ResourceTracker::on_vkDestroyFence(void* context, VkDevice device, VkFence fence,
4808 const VkAllocationCallbacks* pAllocator) {
4809 VkEncoder* enc = (VkEncoder*)context;
4810 enc->vkDestroyFence(device, fence, pAllocator, true /* do lock */);
4811 }
4812
4813 VkResult ResourceTracker::on_vkResetFences(void* context, VkResult, VkDevice device,
4814 uint32_t fenceCount, const VkFence* pFences) {
4815 VkEncoder* enc = (VkEncoder*)context;
4816 VkResult res = enc->vkResetFences(device, fenceCount, pFences, true /* do lock */);
4817
4818 if (res != VK_SUCCESS) return res;
4819
4820 if (!fenceCount) return res;
4821
4822 // Permanence: temporary
4823 // on fence reset, close the fence fd
4824 // and act like we need to GetFenceFdKHR/ImportFenceFdKHR again
4825 std::lock_guard<std::recursive_mutex> lock(mLock);
4826 for (uint32_t i = 0; i < fenceCount; ++i) {
4827 VkFence fence = pFences[i];
4828 auto it = info_VkFence.find(fence);
4829 auto& info = it->second;
4830 if (!info.external) continue;
4831
4832 if (info.syncFd && *info.syncFd >= 0) {
4833 MESA_TRACE_SCOPE("%s: resetting fence. make fd -1\n", __func__);
4834
4835 #if GFXSTREAM_ENABLE_GUEST_GOLDFISH
4836 goldfish_sync_signal(*info.syncFd);
4837 #endif
4838
4839 mSyncHelper->close(*info.syncFd);
4840 }
4841 info.syncFd.reset();
4842 }
4843
4844 return res;
4845 }
4846
4847 VkResult ResourceTracker::on_vkImportFenceFdKHR(void* context, VkResult, VkDevice device,
4848 const VkImportFenceFdInfoKHR* pImportFenceFdInfo) {
4849 (void)context;
4850 (void)device;
4851 (void)pImportFenceFdInfo;
4852
4853 // Transference: copy
4854 // meaning dup() the incoming fd
4855
4856 bool hasFence = pImportFenceFdInfo->fence != VK_NULL_HANDLE;
4857
4858 if (!hasFence) return VK_ERROR_OUT_OF_HOST_MEMORY;
4859
4860 #if defined(VK_USE_PLATFORM_ANDROID_KHR) || DETECT_OS_LINUX
4861
4862 bool syncFdImport = pImportFenceFdInfo->handleType & VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT;
4863
4864 if (!syncFdImport) {
4865 mesa_loge("%s: VK_ERROR_OUT_OF_HOST_MEMORY: no sync fd import\n", __func__);
4866 return VK_ERROR_OUT_OF_HOST_MEMORY;
4867 }
4868
4869 std::lock_guard<std::recursive_mutex> lock(mLock);
4870 auto it = info_VkFence.find(pImportFenceFdInfo->fence);
4871 if (it == info_VkFence.end()) {
4872 mesa_loge("%s: VK_ERROR_OUT_OF_HOST_MEMORY: no fence info\n", __func__);
4873 return VK_ERROR_OUT_OF_HOST_MEMORY;
4874 }
4875
4876 auto& info = it->second;
4877
4878 if (info.syncFd && *info.syncFd >= 0) {
4879 MESA_TRACE_SCOPE("%s: previous sync fd exists, close it\n", __func__);
4880
4881 #if GFXSTREAM_ENABLE_GUEST_GOLDFISH
4882 goldfish_sync_signal(*info.syncFd);
4883 #endif
4884
4885 mSyncHelper->close(*info.syncFd);
4886 }
4887
4888 if (pImportFenceFdInfo->fd < 0) {
4889 MESA_TRACE_SCOPE("%s: import -1, set to -1 and exit\n", __func__);
4890 info.syncFd = -1;
4891 } else {
4892 MESA_TRACE_SCOPE("%s: import actual fd, dup and close()\n", __func__);
4893
4894 int fenceCopy = mSyncHelper->dup(pImportFenceFdInfo->fd);
4895 if (fenceCopy < 0) {
4896 mesa_loge("Failed to dup() import sync fd.");
4897 return VK_ERROR_OUT_OF_HOST_MEMORY;
4898 }
4899
4900 info.syncFd = fenceCopy;
4901
4902 mSyncHelper->close(pImportFenceFdInfo->fd);
4903 }
4904 return VK_SUCCESS;
4905 #else
4906 return VK_ERROR_OUT_OF_HOST_MEMORY;
4907 #endif
4908 }
4909
4910 VkResult ResourceTracker::on_vkGetFenceFdKHR(void* context, VkResult, VkDevice device,
4911 const VkFenceGetFdInfoKHR* pGetFdInfo, int* pFd) {
4912 // export operation.
4913 // first check if fence is signaled
4914 // then if so, return -1
4915 // else, queue work
4916
4917 VkEncoder* enc = (VkEncoder*)context;
4918
4919 bool hasFence = pGetFdInfo->fence != VK_NULL_HANDLE;
4920
4921 if (!hasFence) {
4922 mesa_loge("%s: VK_ERROR_OUT_OF_HOST_MEMORY: no fence\n", __func__);
4923 return VK_ERROR_OUT_OF_HOST_MEMORY;
4924 }
4925
4926 #if defined(VK_USE_PLATFORM_ANDROID_KHR) || DETECT_OS_LINUX
4927 bool syncFdExport = pGetFdInfo->handleType & VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT;
4928
4929 if (!syncFdExport) {
4930 mesa_loge("%s: VK_ERROR_OUT_OF_HOST_MEMORY: no sync fd fence\n", __func__);
4931 return VK_ERROR_OUT_OF_HOST_MEMORY;
4932 }
4933
4934 VkResult currentFenceStatus =
4935 enc->vkGetFenceStatus(device, pGetFdInfo->fence, true /* do lock */);
4936
4937 if (VK_ERROR_DEVICE_LOST == currentFenceStatus) { // Other error
4938 mesa_loge("%s: VK_ERROR_DEVICE_LOST: Other error\n", __func__);
4939 *pFd = -1;
4940 return VK_ERROR_DEVICE_LOST;
4941 }
4942
4943 if (VK_NOT_READY == currentFenceStatus || VK_SUCCESS == currentFenceStatus) {
4944 // Fence is valid. We also create a new sync fd for a signaled
4945 // fence, because ANGLE will use the returned fd directly to
4946 // implement eglDupNativeFenceFDANDROID, where -1 is only returned
4947 // when error occurs.
4948 std::lock_guard<std::recursive_mutex> lock(mLock);
4949
4950 auto it = info_VkFence.find(pGetFdInfo->fence);
4951 if (it == info_VkFence.end()) {
4952 mesa_loge("%s: VK_ERROR_OUT_OF_HOST_MEMORY: no fence info\n", __func__);
4953 return VK_ERROR_OUT_OF_HOST_MEMORY;
4954 }
4955
4956 auto& info = it->second;
4957
4958 bool syncFdCreated = info.external && (info.exportFenceCreateInfo.handleTypes &
4959 VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT);
4960
4961 if (!syncFdCreated) {
4962 mesa_loge("%s: VK_ERROR_OUT_OF_HOST_MEMORY: no sync fd created\n", __func__);
4963 return VK_ERROR_OUT_OF_HOST_MEMORY;
4964 }
4965
4966 if (mFeatureInfo.hasVirtioGpuNativeSync) {
4967 VkResult result;
4968 int64_t osHandle;
4969 uint64_t hostFenceHandle = get_host_u64_VkFence(pGetFdInfo->fence);
4970
4971 result = createFence(device, hostFenceHandle, osHandle);
4972 if (result != VK_SUCCESS) return result;
4973
4974 *pFd = osHandle;
4975 } else {
4976 #if GFXSTREAM_ENABLE_GUEST_GOLDFISH
4977 goldfish_sync_queue_work(
4978 mSyncDeviceFd, get_host_u64_VkFence(pGetFdInfo->fence) /* the handle */,
4979 GOLDFISH_SYNC_VULKAN_SEMAPHORE_SYNC /* thread handle (doubling as type field) */,
4980 pFd);
4981 #endif
4982 }
4983
4984 // relinquish ownership
4985 info.syncFd.reset();
4986
4987 MESA_TRACE_SCOPE("%s: got fd: %d\n", __func__, *pFd);
4988 return VK_SUCCESS;
4989 }
4990 return VK_ERROR_DEVICE_LOST;
4991 #else
4992 return VK_ERROR_OUT_OF_HOST_MEMORY;
4993 #endif
4994 }
4995
4996 VkResult ResourceTracker::on_vkGetFenceStatus(void* context, VkResult input_result, VkDevice device,
4997 VkFence fence) {
4998 VkEncoder* enc = (VkEncoder*)context;
4999
5000 #if defined(VK_USE_PLATFORM_ANDROID_KHR) || DETECT_OS_LINUX
5001 {
5002 std::unique_lock<std::recursive_mutex> lock(mLock);
5003
5004 auto fenceInfoIt = info_VkFence.find(fence);
5005 if (fenceInfoIt == info_VkFence.end()) {
5006 #if VK_USE_64_BIT_PTR_DEFINES
5007 mesa_loge("Failed to find VkFence:%p", fence);
5008 #else
5009 mesa_loge("Failed to find VkFence:0x%" PRIx64, fence);
5010 #endif
5011 return VK_NOT_READY;
5012 }
5013 auto& fenceInfo = fenceInfoIt->second;
5014
5015 if (fenceInfo.syncFd) {
5016 if (*fenceInfo.syncFd == -1) {
5017 return VK_SUCCESS;
5018 }
5019
5020 int syncFdSignaled = mSyncHelper->wait(*fenceInfo.syncFd, /*timeout=*/0) == 0;
5021 return syncFdSignaled ? VK_SUCCESS : VK_NOT_READY;
5022 }
5023 }
5024 #endif
5025
5026 return enc->vkGetFenceStatus(device, fence, /*doLock=*/true);
5027 }
5028
5029 VkResult ResourceTracker::on_vkWaitForFences(void* context, VkResult, VkDevice device,
5030 uint32_t fenceCount, const VkFence* pFences,
5031 VkBool32 waitAll, uint64_t timeout) {
5032 #if defined(VK_USE_PLATFORM_ANDROID_KHR) || DETECT_OS_LINUX
5033 (void)context;
5034 std::vector<int> fencesExternalSyncFds;
5035 std::vector<VkFence> fencesNonExternal;
5036
5037 std::unique_lock<std::recursive_mutex> lock(mLock);
5038
5039 for (uint32_t i = 0; i < fenceCount; ++i) {
5040 auto it = info_VkFence.find(pFences[i]);
5041 if (it == info_VkFence.end()) continue;
5042 const auto& info = it->second;
5043 if (info.syncFd) {
5044 if (*info.syncFd >= 0) {
5045 fencesExternalSyncFds.push_back(*info.syncFd);
5046 }
5047 } else {
5048 fencesNonExternal.push_back(pFences[i]);
5049 }
5050 }
5051
5052 lock.unlock();
5053
5054 for (auto fd : fencesExternalSyncFds) {
5055 MESA_TRACE_SCOPE("Waiting on sync fd: %d", fd);
5056
5057 std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
5058 // syncHelper works in milliseconds
5059 mSyncHelper->wait(fd, DIV_ROUND_UP(timeout, 1000));
5060 std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
5061
5062 uint64_t timeTaken =
5063 std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin).count();
5064 if (timeTaken >= timeout) {
5065 return VK_TIMEOUT;
5066 }
5067
5068 timeout -= timeTaken;
5069 MESA_TRACE_SCOPE("Done waiting on sync fd: %d", fd);
5070
5071 #if GFXSTREAM_SYNC_DEBUG
5072 mSyncHelper->debugPrint(fd);
5073 #endif
5074 }
5075
5076 if (!fencesNonExternal.empty()) {
5077 auto hostConn = ResourceTracker::threadingCallbacks.hostConnectionGetFunc();
5078 auto vkEncoder = ResourceTracker::threadingCallbacks.vkEncoderGetFunc(hostConn);
5079 MESA_TRACE_SCOPE("vkWaitForFences to host");
5080 return vkEncoder->vkWaitForFences(device, fencesNonExternal.size(),
5081 fencesNonExternal.data(), waitAll, timeout,
5082 true /* do lock */);
5083 }
5084
5085 return VK_SUCCESS;
5086
5087 #else
5088 VkEncoder* enc = (VkEncoder*)context;
5089 return enc->vkWaitForFences(device, fenceCount, pFences, waitAll, timeout, true /* do lock */);
5090 #endif
5091 }
5092
5093 VkResult ResourceTracker::on_vkSetPrivateData(void* context, VkResult input_result, VkDevice device,
5094 VkObjectType objectType, uint64_t objectHandle,
5095 VkPrivateDataSlot privateDataSlot, uint64_t data) {
5096 if (input_result != VK_SUCCESS) return input_result;
5097
5098 VkPrivateDataSlot_Info::PrivateDataKey key = std::make_pair(objectHandle, objectType);
5099
5100 std::lock_guard<std::recursive_mutex> lock(mLock);
5101 auto it = info_VkPrivateDataSlot.find(privateDataSlot);
5102
5103 // Do not forward calls with invalid handles to host.
5104 if (it == info_VkPrivateDataSlot.end()) {
5105 return VK_ERROR_OUT_OF_HOST_MEMORY;
5106 }
5107
5108 auto& slotInfoTable = it->second.privateDataTable;
5109 slotInfoTable[key] = data;
5110 return VK_SUCCESS;
5111 }
5112
5113 VkResult ResourceTracker::on_vkSetPrivateDataEXT(void* context, VkResult input_result,
5114 VkDevice device, VkObjectType objectType,
5115 uint64_t objectHandle,
5116 VkPrivateDataSlot privateDataSlot, uint64_t data) {
5117 return on_vkSetPrivateData(context, input_result, device, objectType, objectHandle,
5118 privateDataSlot, data);
5119 }
5120
5121 void ResourceTracker::on_vkGetPrivateData(void* context, VkDevice device, VkObjectType objectType,
5122 uint64_t objectHandle, VkPrivateDataSlot privateDataSlot,
5123 uint64_t* pData) {
5124 VkPrivateDataSlot_Info::PrivateDataKey key = std::make_pair(objectHandle, objectType);
5125
5126 std::lock_guard<std::recursive_mutex> lock(mLock);
5127 auto it = info_VkPrivateDataSlot.find(privateDataSlot);
5128
5129 // Do not forward calls with invalid handles to host.
5130 if (it == info_VkPrivateDataSlot.end()) {
5131 return;
5132 }
5133
5134 auto& slotInfoTable = it->second.privateDataTable;
5135 *pData = slotInfoTable[key];
5136 }
5137
5138 void ResourceTracker::on_vkGetPrivateDataEXT(void* context, VkDevice device,
5139 VkObjectType objectType, uint64_t objectHandle,
5140 VkPrivateDataSlot privateDataSlot, uint64_t* pData) {
5141 return on_vkGetPrivateData(context, device, objectType, objectHandle, privateDataSlot, pData);
5142 }
5143
5144 VkResult ResourceTracker::on_vkCreatePrivateDataSlot(void* context, VkResult input_result,
5145 VkDevice device,
5146 const VkPrivateDataSlotCreateInfo* pCreateInfo,
5147 const VkAllocationCallbacks* pAllocator,
5148 VkPrivateDataSlot* pPrivateDataSlot) {
5149 if (input_result != VK_SUCCESS) {
5150 return input_result;
5151 }
5152 VkEncoder* enc = (VkEncoder*)context;
5153 return enc->vkCreatePrivateDataSlot(device, pCreateInfo, pAllocator, pPrivateDataSlot,
5154 true /* do lock */);
5155 }
5156 VkResult ResourceTracker::on_vkCreatePrivateDataSlotEXT(
5157 void* context, VkResult input_result, VkDevice device,
5158 const VkPrivateDataSlotCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator,
5159 VkPrivateDataSlot* pPrivateDataSlot) {
5160 return on_vkCreatePrivateDataSlot(context, input_result, device, pCreateInfo, pAllocator,
5161 pPrivateDataSlot);
5162 }
5163
5164 void ResourceTracker::on_vkDestroyPrivateDataSlot(void* context, VkDevice device,
5165 VkPrivateDataSlot privateDataSlot,
5166 const VkAllocationCallbacks* pAllocator) {
5167 if (!privateDataSlot) return;
5168
5169 VkEncoder* enc = (VkEncoder*)context;
5170 enc->vkDestroyPrivateDataSlot(device, privateDataSlot, pAllocator, true /* do lock */);
5171 }
5172 void ResourceTracker::on_vkDestroyPrivateDataSlotEXT(void* context, VkDevice device,
5173 VkPrivateDataSlot privateDataSlot,
5174 const VkAllocationCallbacks* pAllocator) {
5175 return on_vkDestroyPrivateDataSlot(context, device, privateDataSlot, pAllocator);
5176 }
5177
5178 VkResult ResourceTracker::on_vkCreateDescriptorPool(void* context, VkResult, VkDevice device,
5179 const VkDescriptorPoolCreateInfo* pCreateInfo,
5180 const VkAllocationCallbacks* pAllocator,
5181 VkDescriptorPool* pDescriptorPool) {
5182 VkEncoder* enc = (VkEncoder*)context;
5183
5184 VkResult res = enc->vkCreateDescriptorPool(device, pCreateInfo, pAllocator, pDescriptorPool,
5185 true /* do lock */);
5186
5187 if (res != VK_SUCCESS) return res;
5188
5189 VkDescriptorPool pool = *pDescriptorPool;
5190
5191 struct goldfish_VkDescriptorPool* dp = as_goldfish_VkDescriptorPool(pool);
5192 dp->allocInfo = new DescriptorPoolAllocationInfo;
5193 dp->allocInfo->device = device;
5194 dp->allocInfo->createFlags = pCreateInfo->flags;
5195 dp->allocInfo->maxSets = pCreateInfo->maxSets;
5196 dp->allocInfo->usedSets = 0;
5197
5198 for (uint32_t i = 0; i < pCreateInfo->poolSizeCount; ++i) {
5199 dp->allocInfo->descriptorCountInfo.push_back({
5200 pCreateInfo->pPoolSizes[i].type, pCreateInfo->pPoolSizes[i].descriptorCount,
5201 0, /* used */
5202 });
5203 }
5204
5205 if (mFeatureInfo.hasVulkanBatchedDescriptorSetUpdate) {
5206 std::vector<uint64_t> poolIds(pCreateInfo->maxSets);
5207
5208 uint32_t count = pCreateInfo->maxSets;
5209 enc->vkCollectDescriptorPoolIdsGOOGLE(device, pool, &count, poolIds.data(),
5210 true /* do lock */);
5211
5212 dp->allocInfo->freePoolIds = poolIds;
5213 }
5214
5215 return res;
5216 }
5217
5218 void ResourceTracker::on_vkDestroyDescriptorPool(void* context, VkDevice device,
5219 VkDescriptorPool descriptorPool,
5220 const VkAllocationCallbacks* pAllocator) {
5221 if (!descriptorPool) return;
5222
5223 VkEncoder* enc = (VkEncoder*)context;
5224
5225 clearDescriptorPoolAndUnregisterDescriptorSets(context, device, descriptorPool);
5226
5227 enc->vkDestroyDescriptorPool(device, descriptorPool, pAllocator, true /* do lock */);
5228 }
5229
5230 VkResult ResourceTracker::on_vkResetDescriptorPool(void* context, VkResult, VkDevice device,
5231 VkDescriptorPool descriptorPool,
5232 VkDescriptorPoolResetFlags flags) {
5233 if (!descriptorPool) return VK_ERROR_INITIALIZATION_FAILED;
5234
5235 VkEncoder* enc = (VkEncoder*)context;
5236
5237 VkResult res = enc->vkResetDescriptorPool(device, descriptorPool, flags, true /* do lock */);
5238
5239 if (res != VK_SUCCESS) return res;
5240
5241 clearDescriptorPoolAndUnregisterDescriptorSets(context, device, descriptorPool);
5242 return res;
5243 }
5244
5245 VkResult ResourceTracker::on_vkAllocateDescriptorSets(
5246 void* context, VkResult, VkDevice device, const VkDescriptorSetAllocateInfo* pAllocateInfo,
5247 VkDescriptorSet* pDescriptorSets) {
5248 VkEncoder* enc = (VkEncoder*)context;
5249 auto ci = pAllocateInfo;
5250 auto sets = pDescriptorSets;
5251 if (mFeatureInfo.hasVulkanBatchedDescriptorSetUpdate) {
5252 // Using the pool ID's we collected earlier from the host
5253 VkResult poolAllocResult = validateAndApplyVirtualDescriptorSetAllocation(ci, sets);
5254
5255 if (poolAllocResult != VK_SUCCESS) return poolAllocResult;
5256
5257 for (uint32_t i = 0; i < ci->descriptorSetCount; ++i) {
5258 register_VkDescriptorSet(sets[i]);
5259 VkDescriptorSetLayout setLayout =
5260 as_goldfish_VkDescriptorSet(sets[i])->reified->setLayout;
5261
5262 // Need to add ref to the set layout in the virtual case
5263 // because the set itself might not be realized on host at the
5264 // same time
5265 struct goldfish_VkDescriptorSetLayout* dsl =
5266 as_goldfish_VkDescriptorSetLayout(setLayout);
5267 ++dsl->layoutInfo->refcount;
5268 }
5269 } else {
5270 VkResult allocRes = enc->vkAllocateDescriptorSets(device, ci, sets, true /* do lock */);
5271
5272 if (allocRes != VK_SUCCESS) return allocRes;
5273
5274 for (uint32_t i = 0; i < ci->descriptorSetCount; ++i) {
5275 applyDescriptorSetAllocation(ci->descriptorPool, ci->pSetLayouts[i]);
5276 fillDescriptorSetInfoForPool(ci->descriptorPool, ci->pSetLayouts[i], sets[i]);
5277 }
5278 }
5279
5280 return VK_SUCCESS;
5281 }
5282
5283 VkResult ResourceTracker::on_vkFreeDescriptorSets(void* context, VkResult, VkDevice device,
5284 VkDescriptorPool descriptorPool,
5285 uint32_t descriptorSetCount,
5286 const VkDescriptorSet* pDescriptorSets) {
5287 VkEncoder* enc = (VkEncoder*)context;
5288
5289 // Bit of robustness so that we can double free descriptor sets
5290 // and do other invalid usages
5291 // https://github.com/KhronosGroup/Vulkan-Docs/issues/1070
5292 // (people expect VK_SUCCESS to always be returned by vkFreeDescriptorSets)
5293 std::vector<VkDescriptorSet> toActuallyFree;
5294 {
5295 std::lock_guard<std::recursive_mutex> lock(mLock);
5296
5297 // Pool was destroyed
5298 if (info_VkDescriptorPool.find(descriptorPool) == info_VkDescriptorPool.end()) {
5299 return VK_SUCCESS;
5300 }
5301
5302 if (!descriptorPoolSupportsIndividualFreeLocked(descriptorPool)) return VK_SUCCESS;
5303
5304 std::vector<VkDescriptorSet> existingDescriptorSets;
5305 ;
5306
5307 // Check if this descriptor set was in the pool's set of allocated descriptor sets,
5308 // to guard against double free (Double free is allowed by the client)
5309 {
5310 auto allocedSets = as_goldfish_VkDescriptorPool(descriptorPool)->allocInfo->allocedSets;
5311
5312 for (uint32_t i = 0; i < descriptorSetCount; ++i) {
5313 if (allocedSets.end() == allocedSets.find(pDescriptorSets[i])) {
5314 mesa_loge(
5315 "%s: Warning: descriptor set %p not found in pool. Was this "
5316 "double-freed?\n",
5317 __func__, (void*)pDescriptorSets[i]);
5318 continue;
5319 }
5320
5321 auto it = info_VkDescriptorSet.find(pDescriptorSets[i]);
5322 if (it == info_VkDescriptorSet.end()) continue;
5323
5324 existingDescriptorSets.push_back(pDescriptorSets[i]);
5325 }
5326 }
5327
5328 for (auto set : existingDescriptorSets) {
5329 if (removeDescriptorSetFromPool(set,
5330 mFeatureInfo.hasVulkanBatchedDescriptorSetUpdate)) {
5331 toActuallyFree.push_back(set);
5332 }
5333 }
5334
5335 if (toActuallyFree.empty()) return VK_SUCCESS;
5336 }
5337
5338 if (mFeatureInfo.hasVulkanBatchedDescriptorSetUpdate) {
5339 // In the batched set update case, decrement refcount on the set layout
5340 // and only free on host if we satisfied a pending allocation on the
5341 // host.
5342 for (uint32_t i = 0; i < toActuallyFree.size(); ++i) {
5343 VkDescriptorSetLayout setLayout =
5344 as_goldfish_VkDescriptorSet(toActuallyFree[i])->reified->setLayout;
5345 decDescriptorSetLayoutRef(context, device, setLayout, nullptr);
5346 }
5347 freeDescriptorSetsIfHostAllocated(enc, device, (uint32_t)toActuallyFree.size(),
5348 toActuallyFree.data());
5349 } else {
5350 // In the non-batched set update case, just free them directly.
5351 enc->vkFreeDescriptorSets(device, descriptorPool, (uint32_t)toActuallyFree.size(),
5352 toActuallyFree.data(), true /* do lock */);
5353 }
5354 return VK_SUCCESS;
5355 }
5356
5357 VkResult ResourceTracker::on_vkCreateDescriptorSetLayout(
5358 void* context, VkResult, VkDevice device, const VkDescriptorSetLayoutCreateInfo* pCreateInfo,
5359 const VkAllocationCallbacks* pAllocator, VkDescriptorSetLayout* pSetLayout) {
5360 VkEncoder* enc = (VkEncoder*)context;
5361
5362 VkResult res = enc->vkCreateDescriptorSetLayout(device, pCreateInfo, pAllocator, pSetLayout,
5363 true /* do lock */);
5364
5365 if (res != VK_SUCCESS) return res;
5366
5367 struct goldfish_VkDescriptorSetLayout* dsl = as_goldfish_VkDescriptorSetLayout(*pSetLayout);
5368 dsl->layoutInfo = new DescriptorSetLayoutInfo;
5369 for (uint32_t i = 0; i < pCreateInfo->bindingCount; ++i) {
5370 dsl->layoutInfo->bindings.push_back(pCreateInfo->pBindings[i]);
5371 }
5372 dsl->layoutInfo->refcount = 1;
5373
5374 return res;
5375 }
5376
5377 void ResourceTracker::on_vkUpdateDescriptorSets(void* context, VkDevice device,
5378 uint32_t descriptorWriteCount,
5379 const VkWriteDescriptorSet* pDescriptorWrites,
5380 uint32_t descriptorCopyCount,
5381 const VkCopyDescriptorSet* pDescriptorCopies) {
5382 VkEncoder* enc = (VkEncoder*)context;
5383
5384 std::vector<VkDescriptorImageInfo> transformedImageInfos;
5385 std::vector<VkWriteDescriptorSet> transformedWrites(descriptorWriteCount);
5386
5387 memcpy(transformedWrites.data(), pDescriptorWrites,
5388 sizeof(VkWriteDescriptorSet) * descriptorWriteCount);
5389
5390 size_t imageInfosNeeded = 0;
5391 for (uint32_t i = 0; i < descriptorWriteCount; ++i) {
5392 if (!isDescriptorTypeImageInfo(transformedWrites[i].descriptorType)) continue;
5393 if (!transformedWrites[i].pImageInfo) continue;
5394
5395 imageInfosNeeded += transformedWrites[i].descriptorCount;
5396 }
5397
5398 transformedImageInfos.resize(imageInfosNeeded);
5399
5400 size_t imageInfoIndex = 0;
5401 for (uint32_t i = 0; i < descriptorWriteCount; ++i) {
5402 if (!isDescriptorTypeImageInfo(transformedWrites[i].descriptorType)) continue;
5403 if (!transformedWrites[i].pImageInfo) continue;
5404
5405 for (uint32_t j = 0; j < transformedWrites[i].descriptorCount; ++j) {
5406 transformedImageInfos[imageInfoIndex] = transformedWrites[i].pImageInfo[j];
5407 ++imageInfoIndex;
5408 }
5409 transformedWrites[i].pImageInfo =
5410 &transformedImageInfos[imageInfoIndex - transformedWrites[i].descriptorCount];
5411 }
5412
5413 {
5414 // Validate and filter samplers
5415 std::lock_guard<std::recursive_mutex> lock(mLock);
5416 size_t imageInfoIndex = 0;
5417 for (uint32_t i = 0; i < descriptorWriteCount; ++i) {
5418 if (!isDescriptorTypeImageInfo(transformedWrites[i].descriptorType)) continue;
5419 if (!transformedWrites[i].pImageInfo) continue;
5420
5421 bool isImmutableSampler = descriptorBindingIsImmutableSampler(
5422 transformedWrites[i].dstSet, transformedWrites[i].dstBinding);
5423
5424 for (uint32_t j = 0; j < transformedWrites[i].descriptorCount; ++j) {
5425 if (isImmutableSampler) {
5426 transformedImageInfos[imageInfoIndex].sampler = 0;
5427 }
5428 transformedImageInfos[imageInfoIndex] =
5429 filterNonexistentSampler(transformedImageInfos[imageInfoIndex]);
5430 ++imageInfoIndex;
5431 }
5432 }
5433 }
5434
5435 if (mFeatureInfo.hasVulkanBatchedDescriptorSetUpdate) {
5436 for (uint32_t i = 0; i < descriptorWriteCount; ++i) {
5437 VkDescriptorSet set = transformedWrites[i].dstSet;
5438 doEmulatedDescriptorWrite(&transformedWrites[i],
5439 as_goldfish_VkDescriptorSet(set)->reified);
5440 }
5441
5442 for (uint32_t i = 0; i < descriptorCopyCount; ++i) {
5443 doEmulatedDescriptorCopy(
5444 &pDescriptorCopies[i],
5445 as_goldfish_VkDescriptorSet(pDescriptorCopies[i].srcSet)->reified,
5446 as_goldfish_VkDescriptorSet(pDescriptorCopies[i].dstSet)->reified);
5447 }
5448 } else {
5449 enc->vkUpdateDescriptorSets(device, descriptorWriteCount, transformedWrites.data(),
5450 descriptorCopyCount, pDescriptorCopies, true /* do lock */);
5451 }
5452 }
5453
5454 void ResourceTracker::on_vkDestroyImage(void* context, VkDevice device, VkImage image,
5455 const VkAllocationCallbacks* pAllocator) {
5456 #ifdef VK_USE_PLATFORM_ANDROID_KHR
5457 {
5458 std::lock_guard<std::recursive_mutex> lock(mLock); // do not guard encoder may cause
5459 // deadlock b/243339973
5460
5461 // Wait for any pending QSRIs to prevent a race between the Gfxstream host
5462 // potentially processing the below `vkDestroyImage()` from the VK encoder
5463 // command stream before processing a previously submitted
5464 // `VIRTIO_GPU_NATIVE_SYNC_VULKAN_QSRI_EXPORT` from the virtio-gpu command
5465 // stream which relies on the image existing.
5466 auto imageInfoIt = info_VkImage.find(image);
5467 if (imageInfoIt != info_VkImage.end()) {
5468 auto& imageInfo = imageInfoIt->second;
5469 for (int syncFd : imageInfo.pendingQsriSyncFds) {
5470 int syncWaitRet = mSyncHelper->wait(syncFd, 3000);
5471 if (syncWaitRet < 0) {
5472 mesa_loge("%s: Failed to wait for pending QSRI sync: sterror: %s errno: %d",
5473 __func__, strerror(errno), errno);
5474 }
5475
5476 #if GFXSTREAM_SYNC_DEBUG
5477 mSyncHelper->debugPrint(syncFd);
5478 #endif
5479 mSyncHelper->close(syncFd);
5480 }
5481 imageInfo.pendingQsriSyncFds.clear();
5482 }
5483 }
5484 #endif
5485 VkEncoder* enc = (VkEncoder*)context;
5486 enc->vkDestroyImage(device, image, pAllocator, true /* do lock */);
5487 }
5488
5489 void ResourceTracker::on_vkGetImageMemoryRequirements(void* context, VkDevice device, VkImage image,
5490 VkMemoryRequirements* pMemoryRequirements) {
5491 std::unique_lock<std::recursive_mutex> lock(mLock);
5492
5493 auto it = info_VkImage.find(image);
5494 if (it == info_VkImage.end()) return;
5495
5496 auto& info = it->second;
5497
5498 if (info.baseRequirementsKnown) {
5499 *pMemoryRequirements = info.baseRequirements;
5500 return;
5501 }
5502
5503 lock.unlock();
5504
5505 VkEncoder* enc = (VkEncoder*)context;
5506
5507 enc->vkGetImageMemoryRequirements(device, image, pMemoryRequirements, true /* do lock */);
5508
5509 lock.lock();
5510
5511 transformImageMemoryRequirementsForGuestLocked(image, pMemoryRequirements);
5512
5513 info.baseRequirementsKnown = true;
5514 info.baseRequirements = *pMemoryRequirements;
5515 }
5516
5517 void ResourceTracker::on_vkGetImageMemoryRequirements2(void* context, VkDevice device,
5518 const VkImageMemoryRequirementsInfo2* pInfo,
5519 VkMemoryRequirements2* pMemoryRequirements) {
5520 VkEncoder* enc = (VkEncoder*)context;
5521 enc->vkGetImageMemoryRequirements2(device, pInfo, pMemoryRequirements, true /* do lock */);
5522 transformImageMemoryRequirements2ForGuest(pInfo->image, pMemoryRequirements);
5523 }
5524
5525 VkResult ResourceTracker::on_vkGetImageDrmFormatModifierPropertiesEXT(
5526 void* context, VkResult, VkDevice device, VkImage image,
5527 VkImageDrmFormatModifierPropertiesEXT* pProperties) {
5528 #ifdef LINUX_GUEST_BUILD
5529 auto it = info_VkDevice.find(device);
5530 if (it == info_VkDevice.end()) return VK_ERROR_UNKNOWN;
5531 if (doImageDrmFormatModifierEmulation(it->second.physdev)) {
5532 // This is the only format modifier that will be used in emulation
5533 pProperties->drmFormatModifier = DRM_FORMAT_MOD_LINEAR;
5534 return VK_SUCCESS;
5535 } else {
5536 // Passthrough to host
5537 VkEncoder* enc = (VkEncoder*)context;
5538 return enc->vkGetImageDrmFormatModifierPropertiesEXT(device, image, pProperties,
5539 true /* do lock */);
5540 }
5541 #else
5542 (void)context;
5543 (void)device;
5544 (void)image;
5545 (void)pProperties;
5546 return VK_ERROR_INCOMPATIBLE_DRIVER;
5547 #endif
5548 }
5549
5550 VkResult ResourceTracker::on_vkBindImageMemory(void* context, VkResult, VkDevice device,
5551 VkImage image, VkDeviceMemory memory,
5552 VkDeviceSize memoryOffset) {
5553 VkEncoder* enc = (VkEncoder*)context;
5554 // Do not forward calls with invalid handles to host.
5555 if (info_VkDeviceMemory.find(memory) == info_VkDeviceMemory.end() ||
5556 info_VkImage.find(image) == info_VkImage.end()) {
5557 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
5558 }
5559 return enc->vkBindImageMemory(device, image, memory, memoryOffset, true /* do lock */);
5560 }
5561
5562 VkResult ResourceTracker::on_vkBindImageMemory2(void* context, VkResult, VkDevice device,
5563 uint32_t bindingCount,
5564 const VkBindImageMemoryInfo* pBindInfos) {
5565 VkEncoder* enc = (VkEncoder*)context;
5566
5567 if (bindingCount < 1 || !pBindInfos) {
5568 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
5569 }
5570
5571 for (uint32_t i = 0; i < bindingCount; i++) {
5572 const VkBindImageMemoryInfo& bimi = pBindInfos[i];
5573
5574 auto imageIt = info_VkImage.find(bimi.image);
5575 if (imageIt == info_VkImage.end()) {
5576 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
5577 }
5578
5579 if (bimi.memory != VK_NULL_HANDLE) {
5580 auto memoryIt = info_VkDeviceMemory.find(bimi.memory);
5581 if (memoryIt == info_VkDeviceMemory.end()) {
5582 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
5583 }
5584 }
5585 }
5586
5587 return enc->vkBindImageMemory2(device, bindingCount, pBindInfos, true /* do lock */);
5588 }
5589
5590 VkResult ResourceTracker::on_vkCreateBuffer(void* context, VkResult, VkDevice device,
5591 const VkBufferCreateInfo* pCreateInfo,
5592 const VkAllocationCallbacks* pAllocator,
5593 VkBuffer* pBuffer) {
5594 VkEncoder* enc = (VkEncoder*)context;
5595
5596 VkBufferCreateInfo localCreateInfo = vk_make_orphan_copy(*pCreateInfo);
5597 vk_struct_chain_iterator structChainIter = vk_make_chain_iterator(&localCreateInfo);
5598 VkExternalMemoryBufferCreateInfo localExtBufCi;
5599
5600 const VkExternalMemoryBufferCreateInfo* extBufCiPtr =
5601 vk_find_struct_const(pCreateInfo, EXTERNAL_MEMORY_BUFFER_CREATE_INFO);
5602 if (extBufCiPtr) {
5603 localExtBufCi = vk_make_orphan_copy(*extBufCiPtr);
5604 vk_append_struct(&structChainIter, &localExtBufCi);
5605 }
5606
5607 VkBufferOpaqueCaptureAddressCreateInfo localCapAddrCi;
5608 const VkBufferOpaqueCaptureAddressCreateInfo* pCapAddrCi =
5609 vk_find_struct_const(pCreateInfo, BUFFER_OPAQUE_CAPTURE_ADDRESS_CREATE_INFO);
5610 if (pCapAddrCi) {
5611 localCapAddrCi = vk_make_orphan_copy(*pCapAddrCi);
5612 vk_append_struct(&structChainIter, &localCapAddrCi);
5613 }
5614
5615 VkBufferDeviceAddressCreateInfoEXT localDevAddrCi;
5616 const VkBufferDeviceAddressCreateInfoEXT* pDevAddrCi =
5617 vk_find_struct_const(pCreateInfo, BUFFER_DEVICE_ADDRESS_CREATE_INFO_EXT);
5618 if (pDevAddrCi) {
5619 localDevAddrCi = vk_make_orphan_copy(*pDevAddrCi);
5620 vk_append_struct(&structChainIter, &localDevAddrCi);
5621 }
5622
5623 #ifdef VK_USE_PLATFORM_FUCHSIA
5624 std::optional<zx::vmo> vmo;
5625 bool isSysmemBackedMemory = false;
5626
5627 if (extBufCiPtr &&
5628 (extBufCiPtr->handleTypes & VK_EXTERNAL_MEMORY_HANDLE_TYPE_ZIRCON_VMO_BIT_FUCHSIA)) {
5629 isSysmemBackedMemory = true;
5630 }
5631
5632 const VkBufferCollectionBufferCreateInfoFUCHSIA* extBufferCollectionPtr =
5633 vk_find_struct_const(pCreateInfo, BUFFER_COLLECTION_BUFFER_CREATE_INFO_FUCHSIA);
5634
5635 if (extBufferCollectionPtr) {
5636 const auto& collection =
5637 *reinterpret_cast<fidl::WireSyncClient<fuchsia_sysmem::BufferCollection>*>(
5638 extBufferCollectionPtr->collection);
5639 uint32_t index = extBufferCollectionPtr->index;
5640
5641 auto result = collection->WaitForBuffersAllocated();
5642 if (result.ok() && result->status == ZX_OK) {
5643 auto& info = result->buffer_collection_info;
5644 if (index < info.buffer_count) {
5645 vmo = std::make_optional<zx::vmo>(std::move(info.buffers[index].vmo));
5646 }
5647 } else {
5648 mesa_loge("WaitForBuffersAllocated failed: %d %d", result.status(),
5649 GET_STATUS_SAFE(result, status));
5650 }
5651
5652 if (vmo && vmo->is_valid()) {
5653 fidl::Arena arena;
5654 fuchsia_hardware_goldfish::wire::CreateBuffer2Params createParams(arena);
5655 createParams.set_size(arena, pCreateInfo->size)
5656 .set_memory_property(fuchsia_hardware_goldfish::wire::kMemoryPropertyDeviceLocal);
5657
5658 auto result = mControlDevice->CreateBuffer2(std::move(*vmo), createParams);
5659 if (!result.ok() ||
5660 (result->is_error() != ZX_OK && result->error_value() != ZX_ERR_ALREADY_EXISTS)) {
5661 mesa_loge("CreateBuffer2 failed: %d:%d", result.status(),
5662 GET_STATUS_SAFE(result, error_value()));
5663 }
5664 isSysmemBackedMemory = true;
5665 }
5666 }
5667 #endif // VK_USE_PLATFORM_FUCHSIA
5668
5669 VkResult res;
5670 VkMemoryRequirements memReqs;
5671
5672 if (supportsCreateResourcesWithRequirements()) {
5673 res = enc->vkCreateBufferWithRequirementsGOOGLE(device, &localCreateInfo, pAllocator,
5674 pBuffer, &memReqs, true /* do lock */);
5675 } else {
5676 res =
5677 enc->vkCreateBuffer(device, &localCreateInfo, pAllocator, pBuffer, true /* do lock */);
5678 }
5679
5680 if (res != VK_SUCCESS) return res;
5681
5682 #if defined(VK_USE_PLATFORM_ANDROID_KHR) || DETECT_OS_LINUX
5683 if (mCaps.vulkanCapset.colorBufferMemoryIndex == 0xFFFFFFFF) {
5684 mCaps.vulkanCapset.colorBufferMemoryIndex = getColorBufferMemoryIndex(context, device);
5685 }
5686 if (extBufCiPtr &&
5687 ((extBufCiPtr->handleTypes &
5688 VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID) ||
5689 (extBufCiPtr->handleTypes & VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT))) {
5690 updateMemoryTypeBits(&memReqs.memoryTypeBits, mCaps.vulkanCapset.colorBufferMemoryIndex);
5691 }
5692 #endif
5693
5694 std::lock_guard<std::recursive_mutex> lock(mLock);
5695
5696 auto it = info_VkBuffer.find(*pBuffer);
5697 if (it == info_VkBuffer.end()) return VK_ERROR_INITIALIZATION_FAILED;
5698
5699 auto& info = it->second;
5700
5701 info.createInfo = localCreateInfo;
5702 info.createInfo.pNext = nullptr;
5703
5704 if (supportsCreateResourcesWithRequirements()) {
5705 info.baseRequirementsKnown = true;
5706 info.baseRequirements = memReqs;
5707 }
5708
5709 if (extBufCiPtr) {
5710 info.external = true;
5711 info.externalCreateInfo = *extBufCiPtr;
5712 }
5713
5714 #ifdef VK_USE_PLATFORM_FUCHSIA
5715 if (isSysmemBackedMemory) {
5716 info.isSysmemBackedMemory = true;
5717 }
5718 #endif
5719
5720 return res;
5721 }
5722
5723 void ResourceTracker::on_vkDestroyBuffer(void* context, VkDevice device, VkBuffer buffer,
5724 const VkAllocationCallbacks* pAllocator) {
5725 VkEncoder* enc = (VkEncoder*)context;
5726 enc->vkDestroyBuffer(device, buffer, pAllocator, true /* do lock */);
5727 }
5728
5729 void ResourceTracker::on_vkGetBufferMemoryRequirements(void* context, VkDevice device,
5730 VkBuffer buffer,
5731 VkMemoryRequirements* pMemoryRequirements) {
5732 std::unique_lock<std::recursive_mutex> lock(mLock);
5733
5734 auto it = info_VkBuffer.find(buffer);
5735 if (it == info_VkBuffer.end()) return;
5736
5737 auto& info = it->second;
5738
5739 if (info.baseRequirementsKnown) {
5740 *pMemoryRequirements = info.baseRequirements;
5741 return;
5742 }
5743
5744 lock.unlock();
5745
5746 VkEncoder* enc = (VkEncoder*)context;
5747 enc->vkGetBufferMemoryRequirements(device, buffer, pMemoryRequirements, true /* do lock */);
5748
5749 lock.lock();
5750
5751 info.baseRequirementsKnown = true;
5752 info.baseRequirements = *pMemoryRequirements;
5753 }
5754
5755 void ResourceTracker::on_vkGetBufferMemoryRequirements2(
5756 void* context, VkDevice device, const VkBufferMemoryRequirementsInfo2* pInfo,
5757 VkMemoryRequirements2* pMemoryRequirements) {
5758 VkEncoder* enc = (VkEncoder*)context;
5759 enc->vkGetBufferMemoryRequirements2(device, pInfo, pMemoryRequirements, true /* do lock */);
5760 transformBufferMemoryRequirements2ForGuest(pInfo->buffer, pMemoryRequirements);
5761 }
5762
5763 VkResult ResourceTracker::on_vkCreateSemaphore(void* context, VkResult input_result,
5764 VkDevice device,
5765 const VkSemaphoreCreateInfo* pCreateInfo,
5766 const VkAllocationCallbacks* pAllocator,
5767 VkSemaphore* pSemaphore) {
5768 (void)input_result;
5769 VkEncoder* enc = (VkEncoder*)context;
5770
5771 VkSemaphoreCreateInfo finalCreateInfo = *pCreateInfo;
5772
5773 const VkExportSemaphoreCreateInfoKHR* exportSemaphoreInfoPtr =
5774 vk_find_struct_const(pCreateInfo, EXPORT_SEMAPHORE_CREATE_INFO);
5775
5776 #ifdef VK_USE_PLATFORM_FUCHSIA
5777 bool exportEvent =
5778 exportSemaphoreInfoPtr && (exportSemaphoreInfoPtr->handleTypes &
5779 VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_ZIRCON_EVENT_BIT_FUCHSIA);
5780
5781 if (exportEvent) {
5782 finalCreateInfo.pNext = nullptr;
5783 // If we have timeline semaphores externally, leave it there.
5784 const VkSemaphoreTypeCreateInfo* typeCi =
5785 vk_find_struct_const(pCreateInfo, SEMAPHORE_TYPE_CREATE_INFO);
5786 if (typeCi) finalCreateInfo.pNext = typeCi;
5787 }
5788 #endif
5789
5790 #if defined(VK_USE_PLATFORM_ANDROID_KHR) || DETECT_OS_LINUX
5791 bool exportSyncFd = exportSemaphoreInfoPtr && (exportSemaphoreInfoPtr->handleTypes &
5792 VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT);
5793
5794 if (exportSyncFd) {
5795 finalCreateInfo.pNext = nullptr;
5796 // If we have timeline semaphores externally, leave it there.
5797 const VkSemaphoreTypeCreateInfo* typeCi =
5798 vk_find_struct_const(pCreateInfo, SEMAPHORE_TYPE_CREATE_INFO);
5799 if (typeCi) finalCreateInfo.pNext = typeCi;
5800 }
5801 #endif
5802 input_result = enc->vkCreateSemaphore(device, &finalCreateInfo, pAllocator, pSemaphore,
5803 true /* do lock */);
5804
5805 zx_handle_t event_handle = ZX_HANDLE_INVALID;
5806
5807 #ifdef VK_USE_PLATFORM_FUCHSIA
5808 if (exportEvent) {
5809 zx_event_create(0, &event_handle);
5810 }
5811 #endif
5812
5813 std::lock_guard<std::recursive_mutex> lock(mLock);
5814
5815 auto it = info_VkSemaphore.find(*pSemaphore);
5816 if (it == info_VkSemaphore.end()) return VK_ERROR_INITIALIZATION_FAILED;
5817
5818 auto& info = it->second;
5819
5820 info.device = device;
5821 info.eventHandle = event_handle;
5822 #ifdef VK_USE_PLATFORM_FUCHSIA
5823 info.eventKoid = getEventKoid(info.eventHandle);
5824 #endif
5825
5826 #if defined(VK_USE_PLATFORM_ANDROID_KHR) || DETECT_OS_LINUX
5827 if (exportSyncFd) {
5828 if (mFeatureInfo.hasVirtioGpuNativeSync &&
5829 !(mCaps.params[kParamFencePassing] && mCaps.vulkanCapset.externalSync)) {
5830 VkResult result;
5831 int64_t osHandle;
5832 uint64_t hostFenceHandle = get_host_u64_VkSemaphore(*pSemaphore);
5833
5834 result = createFence(device, hostFenceHandle, osHandle);
5835 if (result != VK_SUCCESS) return result;
5836
5837 info.syncFd.emplace(osHandle);
5838 } else {
5839 #if GFXSTREAM_ENABLE_GUEST_GOLDFISH
5840 ensureSyncDeviceFd();
5841
5842 if (exportSyncFd) {
5843 int syncFd = -1;
5844 goldfish_sync_queue_work(
5845 mSyncDeviceFd, get_host_u64_VkSemaphore(*pSemaphore) /* the handle */,
5846 GOLDFISH_SYNC_VULKAN_SEMAPHORE_SYNC /* thread handle (doubling as type field) */
5847 ,
5848 &syncFd);
5849 info.syncFd.emplace(syncFd);
5850 }
5851 #endif
5852 }
5853 }
5854 #endif
5855
5856 return VK_SUCCESS;
5857 }
5858
5859 void ResourceTracker::on_vkDestroySemaphore(void* context, VkDevice device, VkSemaphore semaphore,
5860 const VkAllocationCallbacks* pAllocator) {
5861 VkEncoder* enc = (VkEncoder*)context;
5862 enc->vkDestroySemaphore(device, semaphore, pAllocator, true /* do lock */);
5863 }
5864
5865 // https://docs.vulkan.org/refpages/latest/refpages/source/vkGetSemaphoreFdKHR.html
5866 // Each call to vkGetSemaphoreFdKHR must create a new file descriptor and transfer ownership
5867 // of it to the application. To avoid leaking resources, the application must release ownership
5868 // of the file descriptor when it is no longer needed.
5869 VkResult ResourceTracker::on_vkGetSemaphoreFdKHR(void* context, VkResult, VkDevice device,
5870 const VkSemaphoreGetFdInfoKHR* pGetFdInfo,
5871 int* pFd) {
5872 #if defined(VK_USE_PLATFORM_ANDROID_KHR) || DETECT_OS_LINUX
5873 VkEncoder* enc = (VkEncoder*)context;
5874 bool getSyncFd = pGetFdInfo->handleType & VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
5875
5876 if (getSyncFd) {
5877 if (mCaps.params[kParamFencePassing] && mCaps.vulkanCapset.externalSync) {
5878 uint64_t syncId = ++mAtomicId;
5879 int64_t osHandle = -1;
5880
5881 VkResult result = enc->vkGetSemaphoreGOOGLE(device, pGetFdInfo->semaphore, syncId,
5882 true /* do lock */);
5883 if (result != VK_SUCCESS) {
5884 mesa_loge("unable to get the semaphore");
5885 return result;
5886 }
5887
5888 result = acquireSync(syncId, osHandle);
5889 if (result != VK_SUCCESS) {
5890 mesa_loge("unable to create host sync object");
5891 return result;
5892 }
5893
5894 *pFd = (int)osHandle;
5895 return VK_SUCCESS;
5896 } else {
5897 // Doesn't this assume that sync file descriptor generated via the non-fence
5898 // passing path during "on_vkCreateSemaphore" is the same one that would be
5899 // generated via guest's "okGetSemaphoreFdKHR" call?
5900 std::lock_guard<std::recursive_mutex> lock(mLock);
5901 auto it = info_VkSemaphore.find(pGetFdInfo->semaphore);
5902 if (it == info_VkSemaphore.end()) return VK_ERROR_OUT_OF_HOST_MEMORY;
5903 auto& semInfo = it->second;
5904 // syncFd is supposed to have value.
5905 *pFd = mSyncHelper->dup(semInfo.syncFd.value_or(-1));
5906 return VK_SUCCESS;
5907 }
5908 } else {
5909 // opaque fd
5910 int hostFd = 0;
5911 int32_t size = 0;
5912 VkResult result = enc->vkGetSemaphoreFdKHR(device, pGetFdInfo, &hostFd, true /* do lock */);
5913 if (result != VK_SUCCESS) {
5914 return result;
5915 }
5916 *pFd = os_create_anonymous_file(size, "vk_opaque_fd");
5917 int write_result = write(*pFd, &hostFd, sizeof(hostFd));
5918 (void)write_result;
5919 return VK_SUCCESS;
5920 }
5921 #else
5922 (void)context;
5923 (void)device;
5924 (void)pGetFdInfo;
5925 (void)pFd;
5926 return VK_ERROR_INCOMPATIBLE_DRIVER;
5927 #endif
5928 }
5929
5930 VkResult ResourceTracker::on_vkImportSemaphoreFdKHR(
5931 void* context, VkResult input_result, VkDevice device,
5932 const VkImportSemaphoreFdInfoKHR* pImportSemaphoreFdInfo) {
5933 #if defined(VK_USE_PLATFORM_ANDROID_KHR) || DETECT_OS_LINUX
5934 VkEncoder* enc = (VkEncoder*)context;
5935 if (input_result != VK_SUCCESS) {
5936 return input_result;
5937 }
5938
5939 if (pImportSemaphoreFdInfo->handleType & VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT) {
5940 std::lock_guard<std::recursive_mutex> lock(mLock);
5941
5942 auto semaphoreIt = info_VkSemaphore.find(pImportSemaphoreFdInfo->semaphore);
5943 auto& info = semaphoreIt->second;
5944
5945 if (info.syncFd.value_or(-1) >= 0) {
5946 mSyncHelper->close(info.syncFd.value());
5947 }
5948
5949 info.syncFd.emplace(pImportSemaphoreFdInfo->fd);
5950
5951 return VK_SUCCESS;
5952 } else {
5953 int fd = pImportSemaphoreFdInfo->fd;
5954 int err = lseek(fd, 0, SEEK_SET);
5955 if (err == -1) {
5956 mesa_loge("lseek fail on import semaphore");
5957 }
5958 int hostFd = 0;
5959 int read_result = read(fd, &hostFd, sizeof(hostFd));
5960 (void)read_result;
5961 VkImportSemaphoreFdInfoKHR tmpInfo = *pImportSemaphoreFdInfo;
5962 tmpInfo.fd = hostFd;
5963 VkResult result = enc->vkImportSemaphoreFdKHR(device, &tmpInfo, true /* do lock */);
5964 mSyncHelper->close(fd);
5965 return result;
5966 }
5967 #else
5968 (void)context;
5969 (void)input_result;
5970 (void)device;
5971 (void)pImportSemaphoreFdInfo;
5972 return VK_ERROR_INCOMPATIBLE_DRIVER;
5973 #endif
5974 }
5975
5976 VkResult ResourceTracker::on_vkGetMemoryFdPropertiesKHR(
5977 void* context, VkResult, VkDevice device, VkExternalMemoryHandleTypeFlagBits handleType, int fd,
5978 VkMemoryFdPropertiesKHR* pMemoryFdProperties) {
5979 #ifdef LINUX_GUEST_BUILD
5980 if (!(handleType & VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT)) {
5981 mesa_loge("%s: VK_KHR_external_memory_fd behavior not defined for handleType: 0x%x\n",
5982 __func__, handleType);
5983 return VK_ERROR_INVALID_EXTERNAL_HANDLE;
5984 }
5985 // Sanity-check device
5986 std::lock_guard<std::recursive_mutex> lock(mLock);
5987 auto deviceIt = info_VkDevice.find(device);
5988 if (deviceIt == info_VkDevice.end()) {
5989 return VK_ERROR_OUT_OF_HOST_MEMORY;
5990 }
5991 // TODO: Verify FD valid ?
5992 (void)fd;
5993
5994 if (mCaps.vulkanCapset.colorBufferMemoryIndex == 0xFFFFFFFF) {
5995 mCaps.vulkanCapset.colorBufferMemoryIndex = getColorBufferMemoryIndex(context, device);
5996 }
5997
5998 updateMemoryTypeBits(&pMemoryFdProperties->memoryTypeBits,
5999 mCaps.vulkanCapset.colorBufferMemoryIndex);
6000
6001 return VK_SUCCESS;
6002 #else
6003 (void)context;
6004 (void)device;
6005 (void)handleType;
6006 (void)fd;
6007 (void)pMemoryFdProperties;
6008 return VK_ERROR_INCOMPATIBLE_DRIVER;
6009 #endif
6010 }
6011
6012 VkResult ResourceTracker::on_vkGetMemoryFdKHR(void* context, VkResult, VkDevice device,
6013 const VkMemoryGetFdInfoKHR* pGetFdInfo, int* pFd) {
6014 #ifdef LINUX_GUEST_BUILD
6015 if (!pGetFdInfo) return VK_ERROR_OUT_OF_HOST_MEMORY;
6016 if (!pGetFdInfo->memory) return VK_ERROR_OUT_OF_HOST_MEMORY;
6017
6018 if (!(pGetFdInfo->handleType & (VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT |
6019 VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT))) {
6020 mesa_loge("%s: Export operation not defined for handleType: 0x%x\n", __func__,
6021 pGetFdInfo->handleType);
6022 return VK_ERROR_OUT_OF_HOST_MEMORY;
6023 }
6024 // Sanity-check device
6025 std::lock_guard<std::recursive_mutex> lock(mLock);
6026 auto deviceIt = info_VkDevice.find(device);
6027 if (deviceIt == info_VkDevice.end()) {
6028 return VK_ERROR_OUT_OF_HOST_MEMORY;
6029 }
6030
6031 auto deviceMemIt = info_VkDeviceMemory.find(pGetFdInfo->memory);
6032 if (deviceMemIt == info_VkDeviceMemory.end()) {
6033 return VK_ERROR_OUT_OF_HOST_MEMORY;
6034 }
6035 auto& info = deviceMemIt->second;
6036
6037 if (!info.blobPtr) {
6038 mesa_loge("%s: VkDeviceMemory does not have a resource available for export.\n", __func__);
6039 return VK_ERROR_OUT_OF_HOST_MEMORY;
6040 }
6041
6042 VirtGpuExternalHandle handle{};
6043 int ret = info.blobPtr->exportBlob(handle);
6044 if (ret != 0 || handle.osHandle < 0) {
6045 mesa_loge("%s: Failed to export host resource to FD.\n", __func__);
6046 return VK_ERROR_OUT_OF_HOST_MEMORY;
6047 }
6048 *pFd = handle.osHandle;
6049 return VK_SUCCESS;
6050 #else
6051 (void)context;
6052 (void)device;
6053 (void)pGetFdInfo;
6054 (void)pFd;
6055 return VK_ERROR_INCOMPATIBLE_DRIVER;
6056 #endif
6057 }
6058
6059 void ResourceTracker::flushCommandBufferPendingCommandsBottomUp(
6060 void* context, VkQueue queue, const std::vector<VkCommandBuffer>& workingSet) {
6061 if (workingSet.empty()) return;
6062
6063 std::vector<VkCommandBuffer> nextLevel;
6064 for (auto commandBuffer : workingSet) {
6065 struct goldfish_VkCommandBuffer* cb = as_goldfish_VkCommandBuffer(commandBuffer);
6066 forAllObjects(cb->subObjects, [&nextLevel](void* secondary) {
6067 nextLevel.push_back((VkCommandBuffer)secondary);
6068 });
6069 }
6070
6071 flushCommandBufferPendingCommandsBottomUp(context, queue, nextLevel);
6072
6073 // After this point, everyone at the previous level has been flushed
6074 for (auto cmdbuf : workingSet) {
6075 struct goldfish_VkCommandBuffer* cb = as_goldfish_VkCommandBuffer(cmdbuf);
6076
6077 // There's no pending commands here, skip. (case 1)
6078 if (!cb->privateStream) continue;
6079
6080 unsigned char* writtenPtr = 0;
6081 size_t written = 0;
6082 CommandBufferStagingStream* cmdBufStream =
6083 static_cast<CommandBufferStagingStream*>(cb->privateStream);
6084 cmdBufStream->getWritten(&writtenPtr, &written);
6085
6086 // There's no pending commands here, skip. (case 2, stream created but no new recordings)
6087 if (!written) continue;
6088
6089 // There are pending commands to flush.
6090 VkEncoder* enc = (VkEncoder*)context;
6091 VkDeviceMemory deviceMemory = cmdBufStream->getDeviceMemory();
6092 VkDeviceSize dataOffset = 0;
6093 if (mFeatureInfo.hasVulkanAuxCommandMemory) {
6094 // for suballocations, deviceMemory is an alias VkDeviceMemory
6095 // get underling VkDeviceMemory for given alias
6096 deviceMemoryTransform_tohost(&deviceMemory, 1 /*memoryCount*/, &dataOffset,
6097 1 /*offsetCount*/, nullptr /*size*/, 0 /*sizeCount*/,
6098 nullptr /*typeIndex*/, 0 /*typeIndexCount*/,
6099 nullptr /*typeBits*/, 0 /*typeBitCounts*/);
6100
6101 // mark stream as flushing before flushing commands
6102 cmdBufStream->markFlushing();
6103 enc->vkQueueFlushCommandsFromAuxMemoryGOOGLE(queue, cmdbuf, deviceMemory, dataOffset,
6104 written, true /*do lock*/);
6105 } else {
6106 enc->vkQueueFlushCommandsGOOGLE(queue, cmdbuf, written, (const void*)writtenPtr,
6107 true /* do lock */);
6108 }
6109 // Reset this stream.
6110 // flushing happens on vkQueueSubmit
6111 // vulkan api states that on queue submit,
6112 // applications MUST not attempt to modify the command buffer in any way
6113 // -as the device may be processing the commands recorded to it.
6114 // It is safe to call reset() here for this reason.
6115 // Command Buffer associated with this stream will only leave pending state
6116 // after queue submit is complete and host has read the data
6117 cmdBufStream->reset();
6118 }
6119 }
6120
6121 uint32_t ResourceTracker::syncEncodersForQueue(VkQueue queue, VkEncoder* currentEncoder) {
6122 if (!supportsAsyncQueueSubmit()) {
6123 return 0;
6124 }
6125
6126 struct goldfish_VkQueue* q = as_goldfish_VkQueue(queue);
6127 if (!q) return 0;
6128
6129 auto lastEncoder = q->lastUsedEncoder;
6130
6131 if (lastEncoder == currentEncoder) return 0;
6132
6133 currentEncoder->incRef();
6134
6135 q->lastUsedEncoder = currentEncoder;
6136
6137 if (!lastEncoder) return 0;
6138
6139 auto oldSeq = q->sequenceNumber;
6140 q->sequenceNumber += 2;
6141 lastEncoder->vkQueueHostSyncGOOGLE(queue, false, oldSeq + 1, true /* do lock */);
6142 lastEncoder->flush();
6143 currentEncoder->vkQueueHostSyncGOOGLE(queue, true, oldSeq + 2, true /* do lock */);
6144
6145 if (lastEncoder->decRef()) {
6146 q->lastUsedEncoder = nullptr;
6147 }
6148
6149 return 0;
6150 }
6151
6152 template <class VkSubmitInfoType>
6153 void ResourceTracker::flushStagingStreams(void* context, VkQueue queue, uint32_t submitCount,
6154 const VkSubmitInfoType* pSubmits) {
6155 std::vector<VkCommandBuffer> toFlush;
6156 for (uint32_t i = 0; i < submitCount; ++i) {
6157 for (uint32_t j = 0; j < getCommandBufferCount(pSubmits[i]); ++j) {
6158 toFlush.push_back(getCommandBuffer(pSubmits[i], j));
6159 }
6160 }
6161
6162 std::unordered_set<VkDescriptorSet> pendingSets;
6163 collectAllPendingDescriptorSetsBottomUp(toFlush, pendingSets);
6164 commitDescriptorSetUpdates(context, queue, pendingSets);
6165
6166 flushCommandBufferPendingCommandsBottomUp(context, queue, toFlush);
6167
6168 for (auto cb : toFlush) {
6169 resetCommandBufferPendingTopology(cb);
6170 }
6171 }
6172
6173 VkResult ResourceTracker::on_vkQueueSubmit(void* context, VkResult input_result, VkQueue queue,
6174 uint32_t submitCount, const VkSubmitInfo* pSubmits,
6175 VkFence fence) {
6176 MESA_TRACE_SCOPE("on_vkQueueSubmit");
6177 EmitGuestAndHostTraceMarker((VkEncoder*)context);
6178
6179 /* From the Vulkan 1.3.204 spec:
6180 *
6181 * VUID-VkSubmitInfo-pNext-03240
6182 *
6183 * "If the pNext chain of this structure includes a VkTimelineSemaphoreSubmitInfo structure
6184 * and any element of pSignalSemaphores was created with a VkSemaphoreType of
6185 * VK_SEMAPHORE_TYPE_TIMELINE, then its signalSemaphoreValueCount member must equal
6186 * signalSemaphoreCount"
6187 *
6188 * Internally, Mesa WSI creates placeholder semaphores/fences (see transformVkSemaphore functions
6189 * in in gfxstream_vk_private.cpp). We don't want to forward that to the host, since there is
6190 * no host side Vulkan object associated with the placeholder sync objects.
6191 *
6192 * The way to test this behavior is Zink + glxgears, on Linux hosts. It should fail without
6193 * this check.
6194 */
6195 for (uint32_t i = 0; i < submitCount; i++) {
6196 VkTimelineSemaphoreSubmitInfo* tssi =
6197 vk_find_struct(const_cast<VkSubmitInfo*>(&pSubmits[i]), TIMELINE_SEMAPHORE_SUBMIT_INFO);
6198
6199 if (tssi) {
6200 uint32_t count = getSignalSemaphoreCount(pSubmits[i]);
6201 if (count != tssi->signalSemaphoreValueCount) {
6202 tssi->signalSemaphoreValueCount = count;
6203 }
6204 }
6205 }
6206
6207 return on_vkQueueSubmitTemplate<VkSubmitInfo, VkSemaphore>(context, input_result, queue, submitCount,
6208 pSubmits, fence);
6209 }
6210
6211 VkResult ResourceTracker::on_vkQueueSubmit2(void* context, VkResult input_result, VkQueue queue,
6212 uint32_t submitCount, const VkSubmitInfo2* pSubmits,
6213 VkFence fence) {
6214 MESA_TRACE_SCOPE("on_vkQueueSubmit2");
6215 EmitGuestAndHostTraceMarker((VkEncoder*)context);
6216 return on_vkQueueSubmitTemplate<VkSubmitInfo2, VkSemaphoreSubmitInfo>(context, input_result, queue, submitCount,
6217 pSubmits, fence);
6218 }
6219
6220 VkResult ResourceTracker::vkQueueSubmitEnc(VkEncoder* enc, VkQueue queue, uint32_t submitCount,
6221 const VkSubmitInfo* pSubmits, VkFence fence) {
6222 if (supportsAsyncQueueSubmit()) {
6223 enc->vkQueueSubmitAsyncGOOGLE(queue, submitCount, pSubmits, fence, true /* do lock */);
6224 return VK_SUCCESS;
6225 } else {
6226 return enc->vkQueueSubmit(queue, submitCount, pSubmits, fence, true /* do lock */);
6227 }
6228 }
6229
6230 VkResult ResourceTracker::vkQueueSubmitEnc(VkEncoder* enc, VkQueue queue, uint32_t submitCount,
6231 const VkSubmitInfo2* pSubmits, VkFence fence) {
6232 if (supportsAsyncQueueSubmit()) {
6233 enc->vkQueueSubmitAsync2GOOGLE(queue, submitCount, pSubmits, fence, true /* do lock */);
6234 return VK_SUCCESS;
6235 } else {
6236 return enc->vkQueueSubmit2(queue, submitCount, pSubmits, fence, true /* do lock */);
6237 }
6238 }
6239
6240 static void pruneWaitSemaphores(const std::vector<VkSemaphore> toRemove,
6241 VkSubmitInfo& submitInfo,
6242 const VkTimelineSemaphoreSubmitInfo* currTssi,
6243 std::vector<VkSemaphore>& newSemList,
6244 std::vector<VkPipelineStageFlags>& newWaitDstStageMaskList,
6245 VkTimelineSemaphoreSubmitInfo& newTssi,
6246 std::vector<uint64_t>& newSemValueList)
6247 {
6248 newSemList.clear();
6249 newSemValueList.clear();
6250 for (uint32_t i = 0; i < submitInfo.waitSemaphoreCount; i++) {
6251 auto it = std::find(toRemove.begin(), toRemove.end(), submitInfo.pWaitSemaphores[i]);
6252 if (it == toRemove.end()) {
6253 newSemList.push_back(submitInfo.pWaitSemaphores[i]);
6254 newWaitDstStageMaskList.push_back(submitInfo.pWaitDstStageMask[i]);
6255 if (currTssi) {
6256 newSemValueList.push_back(currTssi->pWaitSemaphoreValues[i]);
6257 }
6258 }
6259 }
6260 submitInfo.waitSemaphoreCount = static_cast<uint32_t>(newSemList.size());
6261 submitInfo.pWaitSemaphores = newSemList.data();
6262 submitInfo.pWaitDstStageMask = newWaitDstStageMaskList.data();
6263 if (newSemValueList.size() > 0) {
6264 newTssi.waitSemaphoreValueCount = static_cast<uint32_t>(newSemValueList.size());
6265 newTssi.pWaitSemaphoreValues = newSemValueList.data();
6266 submitInfo.pNext = &newTssi;
6267 }
6268 }
6269
6270 static void pruneWaitSemaphores(const std::vector<VkSemaphore> toRemove,
6271 VkSubmitInfo2& submitInfo,
6272 const VkTimelineSemaphoreSubmitInfo* currTssi,
6273 std::vector<VkSemaphoreSubmitInfo>& newSemList,
6274 std::vector<VkPipelineStageFlags>& newWaitDstStageMaskList,
6275 VkTimelineSemaphoreSubmitInfo& newTssi,
6276 std::vector<uint64_t>& newSemValueList)
6277 {
6278 // All of this info is contained in VkSubmitInfo2, so goes unused in this implementation
6279 // of pruneWaitSemaphores
6280 (void)newWaitDstStageMaskList;
6281 (void)newTssi;
6282 (void)newSemValueList;
6283 newSemList.clear();
6284 for (uint32_t i = 0; i < submitInfo.waitSemaphoreInfoCount; i++) {
6285 auto it = std::find(toRemove.begin(), toRemove.end(), submitInfo.pWaitSemaphoreInfos[i].semaphore);
6286 if (it == toRemove.end()) {
6287 newSemList.push_back(submitInfo.pWaitSemaphoreInfos[i]);
6288 }
6289 }
6290 submitInfo.waitSemaphoreInfoCount = static_cast<uint32_t>(newSemList.size());
6291 submitInfo.pWaitSemaphoreInfos = newSemList.data();
6292 }
6293
6294 static void pruneSignalSemaphores(const std::vector<VkSemaphore> toRemove,
6295 VkSubmitInfo& submitInfo,
6296 const VkTimelineSemaphoreSubmitInfo* currTssi,
6297 std::vector<VkSemaphore>& newSemList,
6298 VkTimelineSemaphoreSubmitInfo& newTssi,
6299 std::vector<uint64_t>& newSemValueList)
6300 {
6301 newSemList.clear();
6302 newSemValueList.clear();
6303 for (uint32_t i = 0; i < submitInfo.signalSemaphoreCount; i++) {
6304 auto it = std::find(toRemove.begin(), toRemove.end(), submitInfo.pSignalSemaphores[i]);
6305 if (it == toRemove.end()) {
6306 newSemList.push_back(submitInfo.pSignalSemaphores[i]);
6307 if (currTssi) {
6308 newSemValueList.push_back(currTssi->pSignalSemaphoreValues[i]);
6309 }
6310 }
6311 }
6312 submitInfo.signalSemaphoreCount = static_cast<uint32_t>(newSemList.size());
6313 submitInfo.pSignalSemaphores = newSemList.data();
6314 if (newSemValueList.size() > 0) {
6315 newTssi.signalSemaphoreValueCount = static_cast<uint32_t>(newSemValueList.size());
6316 newTssi.pSignalSemaphoreValues = newSemValueList.data();
6317 submitInfo.pNext = &newTssi;
6318 }
6319 }
6320
6321 static void pruneSignalSemaphores(const std::vector<VkSemaphore> toRemove,
6322 VkSubmitInfo2& submitInfo,
6323 const VkTimelineSemaphoreSubmitInfo* currTssi,
6324 std::vector<VkSemaphoreSubmitInfo>& newSemList,
6325 VkTimelineSemaphoreSubmitInfo& newTssi,
6326 std::vector<uint64_t>& newSemValueList)
6327 {
6328 // All of this info is contained in VkSubmitInfo2, so goes unused in this implementation
6329 // of pruneSignalSemaphores
6330 (void)newTssi;
6331 (void)newSemValueList;
6332 newSemList.clear();
6333 for (uint32_t i = 0; i < submitInfo.signalSemaphoreInfoCount; i++) {
6334 auto it = std::find(toRemove.begin(), toRemove.end(), submitInfo.pSignalSemaphoreInfos[i].semaphore);
6335 if (it == toRemove.end()) {
6336 newSemList.push_back(submitInfo.pSignalSemaphoreInfos[i]);
6337 }
6338 }
6339 submitInfo.signalSemaphoreInfoCount = static_cast<uint32_t>(newSemList.size());
6340 submitInfo.pSignalSemaphoreInfos = newSemList.data();
6341 }
6342
6343 template <typename VkSubmitInfoType, typename VkSemaphoreInfoType>
6344 VkResult ResourceTracker::on_vkQueueSubmitTemplate(void* context, VkResult input_result,
6345 VkQueue queue, uint32_t submitCount,
6346 const VkSubmitInfoType* pSubmits,
6347 VkFence fence) {
6348 flushStagingStreams(context, queue, submitCount, pSubmits);
6349
6350 #ifdef VK_USE_PLATFORM_FUCHSIA
6351 std::vector<VkSemaphore> pre_signal_semaphores;
6352 std::vector<zx_handle_t> pre_signal_events;
6353 #endif
6354 #if defined(VK_USE_PLATFORM_ANDROID_KHR) || DETECT_OS_LINUX
6355 std::vector<int> preSignalSyncFds;
6356 #endif
6357
6358 std::vector<std::pair<zx_handle_t, zx_koid_t>> post_wait_events;
6359 std::vector<int> post_wait_sync_fds;
6360
6361 VkEncoder* enc = (VkEncoder*)context;
6362
6363 // The scope of all these "pruned" submitInfos, semLists, etc.. must be at the level that
6364 // the encoder queue submission will be called, otherwise the vector storage will go out of scope
6365 std::vector<VkSubmitInfoType> prunedSubmitInfos(submitCount);
6366 std::vector<std::vector<VkSemaphoreInfoType>> prunedWaitSemaphoreLists(submitCount);
6367 std::vector<std::vector<VkPipelineStageFlags>> prunedWaitDstStageMaskFlagLists(submitCount);
6368 std::vector<std::vector<VkSemaphoreInfoType>> prunedSignalSemaphoreLists(submitCount);
6369 std::vector<VkTimelineSemaphoreSubmitInfo> prunedTssis(submitCount);
6370 std::vector<std::vector<uint64_t>> prunedWaitSemaphoreValueLists(submitCount);
6371 std::vector<std::vector<uint64_t>> prunedSignalSemaphoreValueLists(submitCount);
6372
6373 {
6374 std::unique_lock<std::recursive_mutex> lock(mLock);
6375
6376 for (uint32_t i = 0; i < submitCount; ++i) {
6377 std::vector<VkSemaphore> waitSemsToRemove;
6378 std::vector<VkSemaphore> signalSemsToRemove;
6379 for (uint32_t j = 0; j < getWaitSemaphoreCount(pSubmits[i]); ++j) {
6380 VkSemaphore semaphore = getWaitSemaphore(pSubmits[i], j);
6381 auto it = info_VkSemaphore.find(semaphore);
6382 if (it != info_VkSemaphore.end()) {
6383 auto& semInfo = it->second;
6384 #ifdef VK_USE_PLATFORM_FUCHSIA
6385 if (semInfo.eventHandle) {
6386 pre_signal_events.push_back(semInfo.eventHandle);
6387 pre_signal_semaphores.push_back(semaphore);
6388 }
6389 #endif
6390 #if defined(VK_USE_PLATFORM_ANDROID_KHR) || DETECT_OS_LINUX
6391 if (semInfo.syncFd.has_value()) {
6392 preSignalSyncFds.push_back(semInfo.syncFd.value());
6393 waitSemsToRemove.push_back(semaphore);
6394 }
6395 #endif
6396 }
6397 }
6398 for (uint32_t j = 0; j < getSignalSemaphoreCount(pSubmits[i]); ++j) {
6399 VkSemaphore semaphore = getSignalSemaphore(pSubmits[i], j);
6400 auto it = info_VkSemaphore.find(semaphore);
6401 if (it != info_VkSemaphore.end()) {
6402 auto& semInfo = it->second;
6403 #ifdef VK_USE_PLATFORM_FUCHSIA
6404 if (semInfo.eventHandle) {
6405 post_wait_events.push_back({semInfo.eventHandle, semInfo.eventKoid});
6406 #ifndef FUCHSIA_NO_TRACE
6407 if (semInfo.eventKoid != ZX_KOID_INVALID) {
6408 // TODO(fxbug.dev/42144867): Remove the "semaphore"
6409 // FLOW_END events once it is removed from clients
6410 // (for example, gfx Engine).
6411 TRACE_FLOW_END("gfx", "semaphore", semInfo.eventKoid);
6412 TRACE_FLOW_BEGIN("gfx", "goldfish_post_wait_event", semInfo.eventKoid);
6413 }
6414 #endif
6415 }
6416 #endif
6417 #if defined(VK_USE_PLATFORM_ANDROID_KHR) || DETECT_OS_LINUX
6418 if (semInfo.syncFd.value_or(-1) >= 0) {
6419 post_wait_sync_fds.push_back(semInfo.syncFd.value());
6420 signalSemsToRemove.push_back(semaphore);
6421 }
6422 #endif
6423 }
6424 }
6425
6426 // Get the current TSSI from the unorphaned submitInfo, the prune functions may need this.
6427 const VkTimelineSemaphoreSubmitInfo* currTssi = vk_find_struct_const(&pSubmits[i], TIMELINE_SEMAPHORE_SUBMIT_INFO);
6428 // Start with an orphan copy of the current submitInfo
6429 prunedSubmitInfos[i] = vk_make_orphan_copy(pSubmits[i]);
6430 // Do initial setup for the new tssi struct; prune functions may or may not actually add to submitInfo.
6431 prunedTssis[i] = {
6432 .sType = VK_STRUCTURE_TYPE_TIMELINE_SEMAPHORE_SUBMIT_INFO,
6433 .pNext = NULL
6434 };
6435 // Finally, prune the wait/signal semaphores accordingly!
6436 pruneWaitSemaphores(waitSemsToRemove, prunedSubmitInfos[i], currTssi, prunedWaitSemaphoreLists[i], prunedWaitDstStageMaskFlagLists[i], prunedTssis[i], prunedWaitSemaphoreValueLists[i]);
6437 pruneSignalSemaphores(signalSemsToRemove, prunedSubmitInfos[i], currTssi, prunedSignalSemaphoreLists[i], prunedTssis[i], prunedSignalSemaphoreValueLists[i]);
6438 }
6439 }
6440
6441 // Schedule waits on the OS external objects and
6442 // signal the wait semaphores
6443 // in a separate thread.
6444 #ifdef VK_USE_PLATFORM_FUCHSIA
6445 if (!pre_signal_semaphores.empty()) {
6446 for (auto event : pre_signal_events) {
6447 preSignalTasks.push_back([event] {
6448 zx_object_wait_one(event, ZX_EVENT_SIGNALED, ZX_TIME_INFINITE, nullptr);
6449 });
6450 }
6451 // Use the old version of VkSubmitInfo
6452 VkSubmitInfo submit_info = {
6453 .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
6454 .waitSemaphoreCount = 0,
6455 .pWaitSemaphores = nullptr,
6456 .pWaitDstStageMask = nullptr,
6457 .signalSemaphoreCount = static_cast<uint32_t>(pre_signal_semaphores.size()),
6458 .pSignalSemaphores = pre_signal_semaphores.data()};
6459 vkQueueSubmitEnc(enc, queue, 1, &submit_info, VK_NULL_HANDLE);
6460 }
6461 #endif
6462 #if defined(VK_USE_PLATFORM_ANDROID_KHR) || DETECT_OS_LINUX
6463 for (auto fd : preSignalSyncFds) {
6464 // https://docs.vulkan.org/refpages/latest/refpages/source/VkImportSemaphoreFdInfoKHR.html
6465 // fd == -1 is treated as already signaled
6466 if (fd != -1) {
6467 mSyncHelper->wait(fd, 3000);
6468 #if GFXSTREAM_SYNC_DEBUG
6469 mSyncHelper->debugPrint(fd);
6470 #endif
6471 }
6472 }
6473 #endif
6474
6475 // The main queue submission
6476 input_result = vkQueueSubmitEnc(enc, queue, submitCount, prunedSubmitInfos.data(), fence);
6477 if (input_result != VK_SUCCESS) return input_result;
6478
6479 int externalFenceFdToSignal = -1;
6480 #if defined(VK_USE_PLATFORM_ANDROID_KHR) || DETECT_OS_LINUX
6481 {
6482 std::unique_lock<std::recursive_mutex> lock(mLock);
6483 if (fence != VK_NULL_HANDLE) {
6484 auto it = info_VkFence.find(fence);
6485 if (it != info_VkFence.end()) {
6486 const auto& info = it->second;
6487 if (info.syncFd && *info.syncFd >= 0) {
6488 externalFenceFdToSignal = *info.syncFd;
6489 }
6490 }
6491 }
6492 }
6493 #endif
6494
6495 VkResult waitIdleRes = VK_SUCCESS;
6496 if (externalFenceFdToSignal >= 0 || !post_wait_events.empty() || !post_wait_sync_fds.empty()) {
6497 auto hostConn = ResourceTracker::threadingCallbacks.hostConnectionGetFunc();
6498 auto vkEncoder = ResourceTracker::threadingCallbacks.vkEncoderGetFunc(hostConn);
6499 waitIdleRes = vkEncoder->vkQueueWaitIdle(queue, true /* do lock */);
6500 if (VK_SUCCESS == waitIdleRes) {
6501 #ifdef VK_USE_PLATFORM_FUCHSIA
6502 MESA_TRACE_SCOPE("on_vkQueueSubmit::SignalSemaphores");
6503 (void)externalFenceFdToSignal;
6504 for (auto& [event, koid] : post_wait_events) {
6505 #ifndef FUCHSIA_NO_TRACE
6506 if (koid != ZX_KOID_INVALID) {
6507 TRACE_FLOW_END("gfx", "goldfish_post_wait_event", koid);
6508 TRACE_FLOW_BEGIN("gfx", "event_signal", koid);
6509 }
6510 #endif
6511 zx_object_signal(event, 0, ZX_EVENT_SIGNALED);
6512 }
6513 #endif
6514 #if GFXSTREAM_ENABLE_GUEST_GOLDFISH
6515 for (auto& fd : post_wait_sync_fds) {
6516 goldfish_sync_signal(fd);
6517 }
6518
6519 if (externalFenceFdToSignal >= 0) {
6520 MESA_TRACE_SCOPE("%s: external fence real signal: %d\n", __func__,
6521 externalFenceFdToSignal);
6522 goldfish_sync_signal(externalFenceFdToSignal);
6523 }
6524 #endif
6525 }
6526 }
6527 return waitIdleRes;
6528 }
6529
6530 VkResult ResourceTracker::on_vkQueueWaitIdle(void* context, VkResult, VkQueue queue) {
6531 VkEncoder* enc = (VkEncoder*)context;
6532
6533 // now done waiting, get the host's opinion
6534 return enc->vkQueueWaitIdle(queue, true /* do lock */);
6535 }
6536
6537 #ifdef VK_USE_PLATFORM_ANDROID_KHR
6538 void ResourceTracker::unwrap_VkNativeBufferANDROID(const VkNativeBufferANDROID* inputNativeInfo,
6539 VkNativeBufferANDROID* outputNativeInfo) {
6540 if (!inputNativeInfo || !inputNativeInfo->handle) {
6541 return;
6542 }
6543
6544 if (!outputNativeInfo || !outputNativeInfo) {
6545 mesa_loge("FATAL: Local native buffer info not properly allocated!");
6546 abort();
6547 }
6548
6549 const native_handle_t* nativeHandle = (const native_handle_t*)inputNativeInfo->handle;
6550 *(uint32_t*)(outputNativeInfo->handle) = mGralloc->getHostHandle(nativeHandle);
6551 }
6552
6553 void ResourceTracker::unwrap_VkBindImageMemorySwapchainInfoKHR(
6554 const VkBindImageMemorySwapchainInfoKHR* inputBimsi,
6555 VkBindImageMemorySwapchainInfoKHR* outputBimsi) {
6556 if (!inputBimsi || !inputBimsi->swapchain) {
6557 return;
6558 }
6559
6560 if (!outputBimsi || !outputBimsi->swapchain) {
6561 return;
6562 }
6563
6564 // Android based swapchains are implemented by the Android framework's
6565 // libvulkan. The only exist within the guest and should not be sent to
6566 // the host.
6567 outputBimsi->swapchain = VK_NULL_HANDLE;
6568 }
6569 #endif
6570
6571 void ResourceTracker::unwrap_vkCreateImage_pCreateInfo(const VkImageCreateInfo* pCreateInfo,
6572 VkImageCreateInfo* local_pCreateInfo) {
6573 #ifdef VK_USE_PLATFORM_ANDROID_KHR
6574 const VkNativeBufferANDROID* inputNativeInfo =
6575 vk_find_struct_const(pCreateInfo, NATIVE_BUFFER_ANDROID);
6576
6577 VkNativeBufferANDROID* outputNativeInfo = vk_find_struct(local_pCreateInfo, NATIVE_BUFFER_ANDROID);
6578
6579 unwrap_VkNativeBufferANDROID(inputNativeInfo, outputNativeInfo);
6580 #endif
6581 }
6582
6583 void ResourceTracker::unwrap_vkAcquireImageANDROID_nativeFenceFd(int fd, int* fd_out) {
6584 #ifdef VK_USE_PLATFORM_ANDROID_KHR
6585 (void)fd_out;
6586 if (fd != -1) {
6587 MESA_TRACE_SCOPE("waitNativeFenceInAcquire");
6588 // Implicit Synchronization
6589 mSyncHelper->wait(fd, 3000);
6590 // From libvulkan's swapchain.cpp:
6591 // """
6592 // NOTE: we're relying on AcquireImageANDROID to close fence_clone,
6593 // even if the call fails. We could close it ourselves on failure, but
6594 // that would create a race condition if the driver closes it on a
6595 // failure path: some other thread might create an fd with the same
6596 // number between the time the driver closes it and the time we close
6597 // it. We must assume one of: the driver *always* closes it even on
6598 // failure, or *never* closes it on failure.
6599 // """
6600 // Therefore, assume contract where we need to close fd in this driver
6601
6602 #if GFXSTREAM_SYNC_DEBUG
6603 mSyncHelper->debugPrint(fd);
6604 #endif
6605 mSyncHelper->close(fd);
6606 }
6607 #endif
6608 }
6609
6610 void ResourceTracker::unwrap_VkBindImageMemory2_pBindInfos(
6611 uint32_t bindInfoCount, const VkBindImageMemoryInfo* inputBindInfos,
6612 VkBindImageMemoryInfo* outputBindInfos) {
6613 #ifdef VK_USE_PLATFORM_ANDROID_KHR
6614 for (uint32_t i = 0; i < bindInfoCount; ++i) {
6615 const VkBindImageMemoryInfo* inputBindInfo = &inputBindInfos[i];
6616 VkBindImageMemoryInfo* outputBindInfo = &outputBindInfos[i];
6617
6618 const VkNativeBufferANDROID* inputNativeInfo =
6619 vk_find_struct_const(inputBindInfo, NATIVE_BUFFER_ANDROID);
6620
6621 VkNativeBufferANDROID* outputNativeInfo = vk_find_struct(outputBindInfo, NATIVE_BUFFER_ANDROID);
6622
6623 unwrap_VkNativeBufferANDROID(inputNativeInfo, outputNativeInfo);
6624
6625 const VkBindImageMemorySwapchainInfoKHR* inputBimsi =
6626 vk_find_struct_const(inputBindInfo, BIND_IMAGE_MEMORY_SWAPCHAIN_INFO_KHR);
6627
6628 VkBindImageMemorySwapchainInfoKHR* outputBimsi = vk_find_struct(outputBindInfo, BIND_IMAGE_MEMORY_SWAPCHAIN_INFO_KHR);
6629
6630 unwrap_VkBindImageMemorySwapchainInfoKHR(inputBimsi, outputBimsi);
6631 }
6632 #endif
6633 }
6634
6635 // Action of vkMapMemoryIntoAddressSpaceGOOGLE:
6636 // 1. preprocess (on_vkMapMemoryIntoAddressSpaceGOOGLE_pre):
6637 // uses address space device to reserve the right size of
6638 // memory.
6639 // 2. the reservation results in a physical address. the physical
6640 // address is set as |*pAddress|.
6641 // 3. after pre, the API call is encoded to the host, where the
6642 // value of pAddress is also sent (the physical address).
6643 // 4. the host will obtain the actual gpu pointer and send it
6644 // back out in |*pAddress|.
6645 // 5. postprocess (on_vkMapMemoryIntoAddressSpaceGOOGLE) will run,
6646 // using the mmap() method of GoldfishAddressSpaceBlock to obtain
6647 // a pointer in guest userspace corresponding to the host pointer.
6648 VkResult ResourceTracker::on_vkMapMemoryIntoAddressSpaceGOOGLE_pre(void*, VkResult, VkDevice,
6649 VkDeviceMemory memory,
6650 uint64_t* pAddress) {
6651 std::lock_guard<std::recursive_mutex> lock(mLock);
6652
6653 auto it = info_VkDeviceMemory.find(memory);
6654 if (it == info_VkDeviceMemory.end()) {
6655 return VK_ERROR_OUT_OF_HOST_MEMORY;
6656 }
6657
6658 #if DETECT_OS_ANDROID
6659 auto& memInfo = it->second;
6660
6661 GoldfishAddressSpaceBlockPtr block = std::make_shared<GoldfishAddressSpaceBlock>();
6662 block->allocate(mGoldfishAddressSpaceBlockProvider.get(), memInfo.coherentMemorySize);
6663
6664 memInfo.goldfishBlock = block;
6665 *pAddress = block->physAddr();
6666
6667 return VK_SUCCESS;
6668 #else
6669 (void)pAddress;
6670 return VK_ERROR_MEMORY_MAP_FAILED;
6671 #endif
6672 }
6673
6674 VkResult ResourceTracker::on_vkMapMemoryIntoAddressSpaceGOOGLE(void*, VkResult input_result,
6675 VkDevice, VkDeviceMemory memory,
6676 uint64_t* pAddress) {
6677 (void)memory;
6678 (void)pAddress;
6679
6680 if (input_result != VK_SUCCESS) {
6681 return input_result;
6682 }
6683
6684 return input_result;
6685 }
6686
6687 VkResult ResourceTracker::initDescriptorUpdateTemplateBuffers(
6688 const VkDescriptorUpdateTemplateCreateInfo* pCreateInfo,
6689 VkDescriptorUpdateTemplate descriptorUpdateTemplate) {
6690 std::lock_guard<std::recursive_mutex> lock(mLock);
6691
6692 auto it = info_VkDescriptorUpdateTemplate.find(descriptorUpdateTemplate);
6693 if (it == info_VkDescriptorUpdateTemplate.end()) {
6694 return VK_ERROR_INITIALIZATION_FAILED;
6695 }
6696
6697 auto& info = it->second;
6698 uint32_t inlineUniformBlockBufferSize = 0;
6699
6700 for (uint32_t i = 0; i < pCreateInfo->descriptorUpdateEntryCount; ++i) {
6701 const auto& entry = pCreateInfo->pDescriptorUpdateEntries[i];
6702 uint32_t descCount = entry.descriptorCount;
6703 VkDescriptorType descType = entry.descriptorType;
6704 ++info.templateEntryCount;
6705 if (isDescriptorTypeInlineUniformBlock(descType)) {
6706 inlineUniformBlockBufferSize += descCount;
6707 ++info.inlineUniformBlockCount;
6708 } else {
6709 for (uint32_t j = 0; j < descCount; ++j) {
6710 if (isDescriptorTypeImageInfo(descType)) {
6711 ++info.imageInfoCount;
6712 } else if (isDescriptorTypeBufferInfo(descType)) {
6713 ++info.bufferInfoCount;
6714 } else if (isDescriptorTypeBufferView(descType)) {
6715 ++info.bufferViewCount;
6716 } else {
6717 mesa_loge("%s: FATAL: Unknown descriptor type %d\n", __func__, descType);
6718 // abort();
6719 }
6720 }
6721 }
6722 }
6723
6724 if (info.templateEntryCount)
6725 info.templateEntries = new VkDescriptorUpdateTemplateEntry[info.templateEntryCount];
6726
6727 if (info.imageInfoCount) {
6728 info.imageInfoIndices = new uint32_t[info.imageInfoCount];
6729 info.imageInfos = new VkDescriptorImageInfo[info.imageInfoCount];
6730 }
6731
6732 if (info.bufferInfoCount) {
6733 info.bufferInfoIndices = new uint32_t[info.bufferInfoCount];
6734 info.bufferInfos = new VkDescriptorBufferInfo[info.bufferInfoCount];
6735 }
6736
6737 if (info.bufferViewCount) {
6738 info.bufferViewIndices = new uint32_t[info.bufferViewCount];
6739 info.bufferViews = new VkBufferView[info.bufferViewCount];
6740 }
6741
6742 if (info.inlineUniformBlockCount) {
6743 info.inlineUniformBlockBuffer.resize(inlineUniformBlockBufferSize);
6744 info.inlineUniformBlockBytesPerBlocks.resize(info.inlineUniformBlockCount);
6745 }
6746
6747 uint32_t imageInfoIndex = 0;
6748 uint32_t bufferInfoIndex = 0;
6749 uint32_t bufferViewIndex = 0;
6750 uint32_t inlineUniformBlockIndex = 0;
6751
6752 for (uint32_t i = 0; i < pCreateInfo->descriptorUpdateEntryCount; ++i) {
6753 const auto& entry = pCreateInfo->pDescriptorUpdateEntries[i];
6754 uint32_t descCount = entry.descriptorCount;
6755 VkDescriptorType descType = entry.descriptorType;
6756
6757 info.templateEntries[i] = entry;
6758
6759 if (isDescriptorTypeInlineUniformBlock(descType)) {
6760 info.inlineUniformBlockBytesPerBlocks[inlineUniformBlockIndex] = descCount;
6761 ++inlineUniformBlockIndex;
6762 } else {
6763 for (uint32_t j = 0; j < descCount; ++j) {
6764 if (isDescriptorTypeImageInfo(descType)) {
6765 info.imageInfoIndices[imageInfoIndex] = i;
6766 ++imageInfoIndex;
6767 } else if (isDescriptorTypeBufferInfo(descType)) {
6768 info.bufferInfoIndices[bufferInfoIndex] = i;
6769 ++bufferInfoIndex;
6770 } else if (isDescriptorTypeBufferView(descType)) {
6771 info.bufferViewIndices[bufferViewIndex] = i;
6772 ++bufferViewIndex;
6773 } else {
6774 mesa_loge("%s: FATAL: Unknown descriptor type %d\n", __func__, descType);
6775 // abort();
6776 }
6777 }
6778 }
6779 }
6780
6781 return VK_SUCCESS;
6782 }
6783
6784 VkResult ResourceTracker::on_vkCreateDescriptorUpdateTemplate(
6785 void* context, VkResult input_result, VkDevice device,
6786 const VkDescriptorUpdateTemplateCreateInfo* pCreateInfo,
6787 const VkAllocationCallbacks* pAllocator,
6788 VkDescriptorUpdateTemplate* pDescriptorUpdateTemplate) {
6789 (void)context;
6790 (void)device;
6791 (void)pAllocator;
6792
6793 if (input_result != VK_SUCCESS) return input_result;
6794
6795 return initDescriptorUpdateTemplateBuffers(pCreateInfo, *pDescriptorUpdateTemplate);
6796 }
6797
6798 void ResourceTracker::on_vkUpdateDescriptorSetWithTemplate(
6799 void* context, VkDevice device, VkDescriptorSet descriptorSet,
6800 VkDescriptorUpdateTemplate descriptorUpdateTemplate, const void* pData) {
6801 VkEncoder* enc = (VkEncoder*)context;
6802
6803 uint8_t* userBuffer = (uint8_t*)pData;
6804 if (!userBuffer) return;
6805
6806 // TODO: Make this thread safe
6807 std::unique_lock<std::recursive_mutex> lock(mLock);
6808
6809 auto it = info_VkDescriptorUpdateTemplate.find(descriptorUpdateTemplate);
6810 if (it == info_VkDescriptorUpdateTemplate.end()) {
6811 return;
6812 }
6813
6814 auto& info = it->second;
6815
6816 uint32_t templateEntryCount = info.templateEntryCount;
6817 VkDescriptorUpdateTemplateEntry* templateEntries = info.templateEntries;
6818
6819 uint32_t imageInfoCount = info.imageInfoCount;
6820 uint32_t bufferInfoCount = info.bufferInfoCount;
6821 uint32_t bufferViewCount = info.bufferViewCount;
6822 uint32_t* imageInfoIndices = info.imageInfoIndices;
6823 uint32_t* bufferInfoIndices = info.bufferInfoIndices;
6824 uint32_t* bufferViewIndices = info.bufferViewIndices;
6825 VkDescriptorImageInfo* imageInfos = info.imageInfos;
6826 VkDescriptorBufferInfo* bufferInfos = info.bufferInfos;
6827 VkBufferView* bufferViews = info.bufferViews;
6828 uint8_t* inlineUniformBlockBuffer = info.inlineUniformBlockBuffer.data();
6829 uint32_t* inlineUniformBlockBytesPerBlocks = info.inlineUniformBlockBytesPerBlocks.data();
6830
6831 lock.unlock();
6832
6833 size_t currImageInfoOffset = 0;
6834 size_t currBufferInfoOffset = 0;
6835 size_t currBufferViewOffset = 0;
6836 size_t inlineUniformBlockOffset = 0;
6837 size_t inlineUniformBlockIdx = 0;
6838
6839 struct goldfish_VkDescriptorSet* ds = as_goldfish_VkDescriptorSet(descriptorSet);
6840 ReifiedDescriptorSet* reified = ds->reified;
6841
6842 bool batched = mFeatureInfo.hasVulkanBatchedDescriptorSetUpdate;
6843
6844 for (uint32_t i = 0; i < templateEntryCount; ++i) {
6845 const auto& entry = templateEntries[i];
6846 VkDescriptorType descType = entry.descriptorType;
6847 uint32_t dstBinding = entry.dstBinding;
6848
6849 auto offset = entry.offset;
6850 auto stride = entry.stride;
6851 auto dstArrayElement = entry.dstArrayElement;
6852
6853 uint32_t descCount = entry.descriptorCount;
6854
6855 if (isDescriptorTypeImageInfo(descType)) {
6856 if (!stride) stride = sizeof(VkDescriptorImageInfo);
6857
6858 const VkDescriptorImageInfo* currImageInfoBegin =
6859 (const VkDescriptorImageInfo*)((uint8_t*)imageInfos + currImageInfoOffset);
6860
6861 for (uint32_t j = 0; j < descCount; ++j) {
6862 const VkDescriptorImageInfo* user =
6863 (const VkDescriptorImageInfo*)(userBuffer + offset + j * stride);
6864
6865 memcpy(((uint8_t*)imageInfos) + currImageInfoOffset, user,
6866 sizeof(VkDescriptorImageInfo));
6867 currImageInfoOffset += sizeof(VkDescriptorImageInfo);
6868 }
6869
6870 if (batched) {
6871 doEmulatedDescriptorImageInfoWriteFromTemplate(
6872 descType, dstBinding, dstArrayElement, descCount, currImageInfoBegin, reified);
6873 }
6874 } else if (isDescriptorTypeBufferInfo(descType)) {
6875 if (!stride) stride = sizeof(VkDescriptorBufferInfo);
6876
6877 const VkDescriptorBufferInfo* currBufferInfoBegin =
6878 (const VkDescriptorBufferInfo*)((uint8_t*)bufferInfos + currBufferInfoOffset);
6879
6880 for (uint32_t j = 0; j < descCount; ++j) {
6881 const VkDescriptorBufferInfo* user =
6882 (const VkDescriptorBufferInfo*)(userBuffer + offset + j * stride);
6883
6884 memcpy(((uint8_t*)bufferInfos) + currBufferInfoOffset, user,
6885 sizeof(VkDescriptorBufferInfo));
6886
6887 // TODO(b/355497683): move this into gfxstream_vk_UpdateDescriptorSetWithTemplate().
6888 #if DETECT_OS_LINUX || defined(VK_USE_PLATFORM_ANDROID_KHR)
6889 // Convert mesa to internal for objects in the user buffer
6890 VkDescriptorBufferInfo* internalBufferInfo =
6891 (VkDescriptorBufferInfo*)(((uint8_t*)bufferInfos) + currBufferInfoOffset);
6892 VK_FROM_HANDLE(gfxstream_vk_buffer, gfxstream_buffer, internalBufferInfo->buffer);
6893 internalBufferInfo->buffer = gfxstream_buffer->internal_object;
6894 #endif
6895 currBufferInfoOffset += sizeof(VkDescriptorBufferInfo);
6896 }
6897
6898 if (batched) {
6899 doEmulatedDescriptorBufferInfoWriteFromTemplate(
6900 descType, dstBinding, dstArrayElement, descCount, currBufferInfoBegin, reified);
6901 }
6902
6903 } else if (isDescriptorTypeBufferView(descType)) {
6904 if (!stride) stride = sizeof(VkBufferView);
6905
6906 const VkBufferView* currBufferViewBegin =
6907 (const VkBufferView*)((uint8_t*)bufferViews + currBufferViewOffset);
6908
6909 for (uint32_t j = 0; j < descCount; ++j) {
6910 const VkBufferView* user = (const VkBufferView*)(userBuffer + offset + j * stride);
6911
6912 memcpy(((uint8_t*)bufferViews) + currBufferViewOffset, user, sizeof(VkBufferView));
6913 currBufferViewOffset += sizeof(VkBufferView);
6914 }
6915
6916 if (batched) {
6917 doEmulatedDescriptorBufferViewWriteFromTemplate(
6918 descType, dstBinding, dstArrayElement, descCount, currBufferViewBegin, reified);
6919 }
6920 } else if (isDescriptorTypeInlineUniformBlock(descType)) {
6921 uint32_t inlineUniformBlockBytesPerBlock =
6922 inlineUniformBlockBytesPerBlocks[inlineUniformBlockIdx];
6923 uint8_t* currInlineUniformBlockBufferBegin =
6924 inlineUniformBlockBuffer + inlineUniformBlockOffset;
6925 memcpy(currInlineUniformBlockBufferBegin, userBuffer + offset,
6926 inlineUniformBlockBytesPerBlock);
6927 inlineUniformBlockIdx++;
6928 inlineUniformBlockOffset += inlineUniformBlockBytesPerBlock;
6929
6930 if (batched) {
6931 doEmulatedDescriptorInlineUniformBlockFromTemplate(
6932 descType, dstBinding, dstArrayElement, descCount,
6933 currInlineUniformBlockBufferBegin, reified);
6934 }
6935 } else {
6936 mesa_loge("%s: FATAL: Unknown descriptor type %d\n", __func__, descType);
6937 abort();
6938 }
6939 }
6940
6941 if (batched) return;
6942
6943 enc->vkUpdateDescriptorSetWithTemplateSized2GOOGLE(
6944 device, descriptorSet, descriptorUpdateTemplate, imageInfoCount, bufferInfoCount,
6945 bufferViewCount, static_cast<uint32_t>(info.inlineUniformBlockBuffer.size()),
6946 imageInfoIndices, bufferInfoIndices, bufferViewIndices, imageInfos, bufferInfos,
6947 bufferViews, inlineUniformBlockBuffer, true /* do lock */);
6948 }
6949
6950 #ifdef LINUX_GUEST_BUILD
6951 static void fillEmulatedDrmFormatModPropsList(
6952 const VkFormatProperties* pFormatProperties,
6953 VkDrmFormatModifierPropertiesListEXT* emulatedDrmFmtModPropsList) {
6954 mesa_logd(
6955 "VkDrmFormatModifierPropertiesListEXT: emulating DRM_FORMAT_MOD_LINEAR with linear tiling "
6956 "features");
6957 emulatedDrmFmtModPropsList->drmFormatModifierCount = 1;
6958 if (emulatedDrmFmtModPropsList->pDrmFormatModifierProperties) {
6959 emulatedDrmFmtModPropsList->pDrmFormatModifierProperties[0] = {
6960 .drmFormatModifier = DRM_FORMAT_MOD_LINEAR,
6961 .drmFormatModifierPlaneCount = 1,
6962 .drmFormatModifierTilingFeatures = VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT |
6963 VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT |
6964 VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT,
6965 };
6966 };
6967 }
6968 #endif
6969
6970 void ResourceTracker::on_vkGetPhysicalDeviceFormatProperties2(
6971 void* context, VkPhysicalDevice physicalDevice, VkFormat format,
6972 VkFormatProperties2* pFormatProperties) {
6973 VkEncoder* enc = (VkEncoder*)context;
6974 enc->vkGetPhysicalDeviceFormatProperties2(physicalDevice, format, pFormatProperties,
6975 true /* do lock */);
6976
6977 #ifdef LINUX_GUEST_BUILD
6978 VkDrmFormatModifierPropertiesListEXT* emulatedDrmFmtModPropsList =
6979 vk_find_struct(pFormatProperties, DRM_FORMAT_MODIFIER_PROPERTIES_LIST_EXT);
6980 if (emulatedDrmFmtModPropsList && doImageDrmFormatModifierEmulation(physicalDevice)) {
6981 fillEmulatedDrmFormatModPropsList(&pFormatProperties->formatProperties,
6982 emulatedDrmFmtModPropsList);
6983 }
6984 #endif
6985 }
6986
6987 VkResult ResourceTracker::on_vkGetPhysicalDeviceImageFormatProperties2(
6988 void* context, VkResult input_result, VkPhysicalDevice physicalDevice,
6989 const VkPhysicalDeviceImageFormatInfo2* pImageFormatInfo,
6990 VkImageFormatProperties2* pImageFormatProperties) {
6991 VkEncoder* enc = (VkEncoder*)context;
6992 (void)input_result;
6993
6994 VkPhysicalDeviceImageFormatInfo2 localImageFormatInfo = *pImageFormatInfo;
6995
6996 uint32_t supportedHandleType = 0;
6997 VkExternalImageFormatProperties* ext_img_properties =
6998 vk_find_struct(pImageFormatProperties, EXTERNAL_IMAGE_FORMAT_PROPERTIES);
6999
7000 #ifdef VK_USE_PLATFORM_FUCHSIA
7001
7002 constexpr VkFormat kExternalImageSupportedFormats[] = {
7003 VK_FORMAT_B8G8R8A8_SINT, VK_FORMAT_B8G8R8A8_UNORM, VK_FORMAT_B8G8R8A8_SRGB,
7004 VK_FORMAT_B8G8R8A8_SNORM, VK_FORMAT_B8G8R8A8_SSCALED, VK_FORMAT_B8G8R8A8_USCALED,
7005 VK_FORMAT_R8G8B8A8_SINT, VK_FORMAT_R8G8B8A8_UNORM, VK_FORMAT_R8G8B8A8_SRGB,
7006 VK_FORMAT_R8G8B8A8_SNORM, VK_FORMAT_R8G8B8A8_SSCALED, VK_FORMAT_R8G8B8A8_USCALED,
7007 VK_FORMAT_R8_UNORM, VK_FORMAT_R8_UINT, VK_FORMAT_R8_USCALED,
7008 VK_FORMAT_R8_SNORM, VK_FORMAT_R8_SINT, VK_FORMAT_R8_SSCALED,
7009 VK_FORMAT_R8_SRGB, VK_FORMAT_R8G8_UNORM, VK_FORMAT_R8G8_UINT,
7010 VK_FORMAT_R8G8_USCALED, VK_FORMAT_R8G8_SNORM, VK_FORMAT_R8G8_SINT,
7011 VK_FORMAT_R8G8_SSCALED, VK_FORMAT_R8G8_SRGB,
7012 };
7013
7014 if (ext_img_properties) {
7015 if (std::find(std::begin(kExternalImageSupportedFormats),
7016 std::end(kExternalImageSupportedFormats),
7017 pImageFormatInfo->format) == std::end(kExternalImageSupportedFormats)) {
7018 return VK_ERROR_FORMAT_NOT_SUPPORTED;
7019 }
7020 }
7021 supportedHandleType |= VK_EXTERNAL_MEMORY_HANDLE_TYPE_ZIRCON_VMO_BIT_FUCHSIA;
7022 #endif
7023
7024 #ifdef VK_USE_PLATFORM_ANDROID_KHR
7025 VkAndroidHardwareBufferUsageANDROID* output_ahw_usage = vk_find_struct(pImageFormatProperties, ANDROID_HARDWARE_BUFFER_USAGE_ANDROID);
7026 supportedHandleType |= VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID;
7027 #endif
7028 const VkPhysicalDeviceExternalImageFormatInfo* ext_img_info =
7029 vk_find_struct_const(pImageFormatInfo, PHYSICAL_DEVICE_EXTERNAL_IMAGE_FORMAT_INFO);
7030 if (supportedHandleType && ext_img_info) {
7031 // 0 is a valid handleType so we don't check against 0
7032 if (ext_img_info->handleType != (ext_img_info->handleType & supportedHandleType)) {
7033 return VK_ERROR_FORMAT_NOT_SUPPORTED;
7034 }
7035 }
7036
7037 #ifdef LINUX_GUEST_BUILD
7038 const VkPhysicalDeviceImageDrmFormatModifierInfoEXT* drmFmtMod =
7039 vk_find_struct_const(pImageFormatInfo, PHYSICAL_DEVICE_IMAGE_DRM_FORMAT_MODIFIER_INFO_EXT);
7040 VkDrmFormatModifierPropertiesListEXT* emulatedDrmFmtModPropsList = nullptr;
7041 if (drmFmtMod) {
7042 if (doImageDrmFormatModifierEmulation(physicalDevice)) {
7043 emulatedDrmFmtModPropsList =
7044 vk_find_struct(pImageFormatProperties, DRM_FORMAT_MODIFIER_PROPERTIES_LIST_EXT);
7045
7046 if (drmFmtMod->drmFormatModifier == DRM_FORMAT_MOD_LINEAR) {
7047 // Remove the drmFmtMod from the localImageFormatInfo
7048 vk_filter_struct(&localImageFormatInfo,
7049 PHYSICAL_DEVICE_IMAGE_DRM_FORMAT_MODIFIER_INFO_EXT);
7050 mesa_logd(
7051 "getPhysicalDeviceImageFormatProperties2: emulating DRM_FORMAT_MOD_LINEAR with "
7052 "VK_IMAGE_TILING_LINEAR");
7053 localImageFormatInfo.tiling = VK_IMAGE_TILING_LINEAR;
7054 localImageFormatInfo.usage &=
7055 ~(VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT);
7056 } else {
7057 return VK_ERROR_FORMAT_NOT_SUPPORTED;
7058 }
7059 } else {
7060 // Host supports DRM format modifiers => leave the input unchanged.
7061 }
7062 }
7063 #endif // LINUX_GUEST_BUILD
7064
7065 VkResult hostRes;
7066
7067 hostRes = enc->vkGetPhysicalDeviceImageFormatProperties2(
7068 physicalDevice, &localImageFormatInfo, pImageFormatProperties, true /* do lock */);
7069
7070 if (hostRes != VK_SUCCESS) return hostRes;
7071
7072 #ifdef LINUX_GUEST_BUILD
7073 if (emulatedDrmFmtModPropsList) {
7074 VkFormatProperties formatProperties;
7075 enc->vkGetPhysicalDeviceFormatProperties(physicalDevice, localImageFormatInfo.format,
7076 &formatProperties, true /* do lock */);
7077 fillEmulatedDrmFormatModPropsList(&formatProperties, emulatedDrmFmtModPropsList);
7078 }
7079 if (ext_img_properties) {
7080 if (ext_img_info) {
7081 if (ext_img_info->handleType == VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT) {
7082 ext_img_properties->externalMemoryProperties = {
7083 .externalMemoryFeatures = VK_EXTERNAL_MEMORY_FEATURE_EXPORTABLE_BIT |
7084 VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT,
7085 .exportFromImportedHandleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
7086 .compatibleHandleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
7087 };
7088 }
7089 }
7090 }
7091 #endif // LINUX_GUEST_BUILD
7092
7093 #ifdef VK_USE_PLATFORM_FUCHSIA
7094 if (ext_img_properties) {
7095 if (ext_img_info) {
7096 if (static_cast<uint32_t>(ext_img_info->handleType) ==
7097 VK_EXTERNAL_MEMORY_HANDLE_TYPE_ZIRCON_VMO_BIT_FUCHSIA) {
7098 ext_img_properties->externalMemoryProperties = {
7099 .externalMemoryFeatures = VK_EXTERNAL_MEMORY_FEATURE_EXPORTABLE_BIT |
7100 VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT,
7101 .exportFromImportedHandleTypes =
7102 VK_EXTERNAL_MEMORY_HANDLE_TYPE_ZIRCON_VMO_BIT_FUCHSIA,
7103 .compatibleHandleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_ZIRCON_VMO_BIT_FUCHSIA,
7104 };
7105 }
7106 }
7107 }
7108 #endif
7109
7110 #ifdef VK_USE_PLATFORM_ANDROID_KHR
7111 if (output_ahw_usage) {
7112 output_ahw_usage->androidHardwareBufferUsage = getAndroidHardwareBufferUsageFromVkUsage(
7113 pImageFormatInfo->flags, pImageFormatInfo->usage);
7114 }
7115 #endif
7116 if (ext_img_properties) {
7117 transformImpl_VkExternalMemoryProperties_fromhost(
7118 &ext_img_properties->externalMemoryProperties, 0);
7119 }
7120 return hostRes;
7121 }
7122
7123 void ResourceTracker::on_vkGetPhysicalDeviceExternalBufferProperties(
7124 void* context, VkPhysicalDevice physicalDevice,
7125 const VkPhysicalDeviceExternalBufferInfo* pExternalBufferInfo,
7126 VkExternalBufferProperties* pExternalBufferProperties) {
7127 VkEncoder* enc = (VkEncoder*)context;
7128
7129 #ifdef VK_USE_PLATFORM_ANDROID_KHR
7130 // Older versions of Goldfish's Gralloc did not support allocating AHARDWAREBUFFER_FORMAT_BLOB
7131 // with GPU usage (b/299520213).
7132 if (mGralloc->treatBlobAsImage() &&
7133 pExternalBufferInfo->handleType ==
7134 VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID) {
7135 pExternalBufferProperties->externalMemoryProperties.externalMemoryFeatures = 0;
7136 pExternalBufferProperties->externalMemoryProperties.exportFromImportedHandleTypes = 0;
7137 pExternalBufferProperties->externalMemoryProperties.compatibleHandleTypes = 0;
7138 return;
7139 }
7140 #endif
7141
7142 uint32_t supportedHandleType = 0;
7143 #ifdef VK_USE_PLATFORM_FUCHSIA
7144 supportedHandleType |= VK_EXTERNAL_MEMORY_HANDLE_TYPE_ZIRCON_VMO_BIT_FUCHSIA;
7145 #endif
7146 #ifdef VK_USE_PLATFORM_ANDROID_KHR
7147 supportedHandleType |= VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID;
7148 #endif
7149 if (supportedHandleType) {
7150 // 0 is a valid handleType so we can't check against 0
7151 if (pExternalBufferInfo->handleType !=
7152 (pExternalBufferInfo->handleType & supportedHandleType)) {
7153 return;
7154 }
7155 }
7156
7157 enc->vkGetPhysicalDeviceExternalBufferProperties(
7158 physicalDevice, pExternalBufferInfo, pExternalBufferProperties, true /* do lock */);
7159
7160 transformImpl_VkExternalMemoryProperties_fromhost(
7161 &pExternalBufferProperties->externalMemoryProperties, 0);
7162 }
7163
7164 void ResourceTracker::on_vkGetPhysicalDeviceExternalSemaphoreProperties(
7165 void*, VkPhysicalDevice, const VkPhysicalDeviceExternalSemaphoreInfo* pExternalSemaphoreInfo,
7166 VkExternalSemaphoreProperties* pExternalSemaphoreProperties) {
7167 (void)pExternalSemaphoreInfo;
7168 (void)pExternalSemaphoreProperties;
7169 #ifdef VK_USE_PLATFORM_FUCHSIA
7170 if (pExternalSemaphoreInfo->handleType ==
7171 static_cast<uint32_t>(VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_ZIRCON_EVENT_BIT_FUCHSIA)) {
7172 pExternalSemaphoreProperties->compatibleHandleTypes |=
7173 VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_ZIRCON_EVENT_BIT_FUCHSIA;
7174 pExternalSemaphoreProperties->exportFromImportedHandleTypes |=
7175 VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_ZIRCON_EVENT_BIT_FUCHSIA;
7176 pExternalSemaphoreProperties->externalSemaphoreFeatures |=
7177 VK_EXTERNAL_SEMAPHORE_FEATURE_EXPORTABLE_BIT |
7178 VK_EXTERNAL_SEMAPHORE_FEATURE_IMPORTABLE_BIT;
7179 }
7180 #else
7181 const VkSemaphoreTypeCreateInfo* semaphoreTypeCi =
7182 vk_find_struct_const(pExternalSemaphoreInfo, SEMAPHORE_TYPE_CREATE_INFO);
7183 bool isSemaphoreTimeline =
7184 semaphoreTypeCi != nullptr && semaphoreTypeCi->semaphoreType == VK_SEMAPHORE_TYPE_TIMELINE;
7185 if (isSemaphoreTimeline) {
7186 // b/304373623
7187 // dEQP-VK.api.external.semaphore.sync_fd#info_timeline
7188 pExternalSemaphoreProperties->compatibleHandleTypes = 0;
7189 pExternalSemaphoreProperties->exportFromImportedHandleTypes = 0;
7190 pExternalSemaphoreProperties->externalSemaphoreFeatures = 0;
7191 } else if (pExternalSemaphoreInfo->handleType ==
7192 VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT) {
7193 pExternalSemaphoreProperties->compatibleHandleTypes |=
7194 VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
7195 pExternalSemaphoreProperties->exportFromImportedHandleTypes |=
7196 VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
7197 pExternalSemaphoreProperties->externalSemaphoreFeatures |=
7198 VK_EXTERNAL_SEMAPHORE_FEATURE_EXPORTABLE_BIT |
7199 VK_EXTERNAL_SEMAPHORE_FEATURE_IMPORTABLE_BIT;
7200 }
7201 #endif // VK_USE_PLATFORM_FUCHSIA
7202 }
7203
7204 void ResourceTracker::registerEncoderCleanupCallback(const VkEncoder* encoder, void* object,
7205 CleanupCallback callback) {
7206 std::lock_guard<std::recursive_mutex> lock(mLock);
7207 auto& callbacks = mEncoderCleanupCallbacks[encoder];
7208 callbacks[object] = callback;
7209 }
7210
7211 void ResourceTracker::unregisterEncoderCleanupCallback(const VkEncoder* encoder, void* object) {
7212 std::lock_guard<std::recursive_mutex> lock(mLock);
7213 mEncoderCleanupCallbacks[encoder].erase(object);
7214 }
7215
7216 void ResourceTracker::onEncoderDeleted(const VkEncoder* encoder) {
7217 std::unique_lock<std::recursive_mutex> lock(mLock);
7218 if (mEncoderCleanupCallbacks.find(encoder) == mEncoderCleanupCallbacks.end()) return;
7219
7220 std::unordered_map<void*, CleanupCallback> callbackCopies = mEncoderCleanupCallbacks[encoder];
7221
7222 mEncoderCleanupCallbacks.erase(encoder);
7223 lock.unlock();
7224
7225 for (auto it : callbackCopies) {
7226 it.second();
7227 }
7228 }
7229
7230 CommandBufferStagingStream::Alloc ResourceTracker::getAlloc() {
7231 if (mFeatureInfo.hasVulkanAuxCommandMemory) {
7232 return [this](size_t size) -> CommandBufferStagingStream::Memory {
7233 VkMemoryAllocateInfo info{
7234 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
7235 .pNext = nullptr,
7236 .allocationSize = size,
7237 .memoryTypeIndex = VK_MAX_MEMORY_TYPES // indicates auxiliary memory
7238 };
7239
7240 auto enc = ResourceTracker::getThreadLocalEncoder();
7241 VkDevice device = VK_NULL_HANDLE;
7242 VkDeviceMemory vkDeviceMem = VK_NULL_HANDLE;
7243 VkResult result = getCoherentMemory(&info, enc, device, &vkDeviceMem);
7244 if (result != VK_SUCCESS) {
7245 mesa_loge("Failed to get coherent memory %u", result);
7246 return {.deviceMemory = VK_NULL_HANDLE, .ptr = nullptr};
7247 }
7248
7249 // getCoherentMemory() uses suballocations.
7250 // To retrieve the suballocated memory address, look up
7251 // VkDeviceMemory filled in by getCoherentMemory()
7252 // scope of mLock
7253 {
7254 std::lock_guard<std::recursive_mutex> lock(mLock);
7255 const auto it = info_VkDeviceMemory.find(vkDeviceMem);
7256 if (it == info_VkDeviceMemory.end()) {
7257 mesa_loge("Coherent memory allocated %u not found", result);
7258 return {.deviceMemory = VK_NULL_HANDLE, .ptr = nullptr};
7259 };
7260
7261 const auto& info = it->second;
7262 return {.deviceMemory = vkDeviceMem, .ptr = info.ptr};
7263 }
7264 };
7265 }
7266 return nullptr;
7267 }
7268
7269 CommandBufferStagingStream::Free ResourceTracker::getFree() {
7270 if (mFeatureInfo.hasVulkanAuxCommandMemory) {
7271 return [this](const CommandBufferStagingStream::Memory& memory) {
7272 // deviceMemory may not be the actual backing auxiliary VkDeviceMemory
7273 // for suballocations, deviceMemory is a alias VkDeviceMemory hand;
7274 // freeCoherentMemoryLocked maps the alias to the backing VkDeviceMemory
7275 VkDeviceMemory deviceMemory = memory.deviceMemory;
7276 std::unique_lock<std::recursive_mutex> lock(mLock);
7277 auto it = info_VkDeviceMemory.find(deviceMemory);
7278 if (it == info_VkDeviceMemory.end()) {
7279 mesa_loge("Device memory to free not found");
7280 return;
7281 }
7282 auto coherentMemory = freeCoherentMemoryLocked(deviceMemory, it->second);
7283 // We have to release the lock before we could possibly free a
7284 // CoherentMemory, because that will call into VkEncoder, which
7285 // shouldn't be called when the lock is held.
7286 lock.unlock();
7287 coherentMemory = nullptr;
7288 };
7289 }
7290 return nullptr;
7291 }
7292
7293 VkResult ResourceTracker::on_vkBeginCommandBuffer(void* context, VkResult input_result,
7294 VkCommandBuffer commandBuffer,
7295 const VkCommandBufferBeginInfo* pBeginInfo) {
7296 (void)context;
7297
7298 resetCommandBufferStagingInfo(commandBuffer, true /* also reset primaries */,
7299 true /* also clear pending descriptor sets */);
7300
7301 VkEncoder* enc = ResourceTracker::getCommandBufferEncoder(commandBuffer);
7302 (void)input_result;
7303
7304 struct goldfish_VkCommandBuffer* cb = as_goldfish_VkCommandBuffer(commandBuffer);
7305 cb->flags = pBeginInfo->flags;
7306
7307 VkCommandBufferBeginInfo modifiedBeginInfo;
7308
7309 if (pBeginInfo->pInheritanceInfo && !cb->isSecondary) {
7310 modifiedBeginInfo = *pBeginInfo;
7311 modifiedBeginInfo.pInheritanceInfo = nullptr;
7312 pBeginInfo = &modifiedBeginInfo;
7313 }
7314
7315 if (!supportsDeferredCommands()) {
7316 return enc->vkBeginCommandBuffer(commandBuffer, pBeginInfo, true /* do lock */);
7317 }
7318
7319 enc->vkBeginCommandBufferAsyncGOOGLE(commandBuffer, pBeginInfo, true /* do lock */);
7320
7321 return VK_SUCCESS;
7322 }
7323
7324 VkResult ResourceTracker::on_vkEndCommandBuffer(void* context, VkResult input_result,
7325 VkCommandBuffer commandBuffer) {
7326 VkEncoder* enc = (VkEncoder*)context;
7327 (void)input_result;
7328
7329 if (!supportsDeferredCommands()) {
7330 return enc->vkEndCommandBuffer(commandBuffer, true /* do lock */);
7331 }
7332
7333 enc->vkEndCommandBufferAsyncGOOGLE(commandBuffer, true /* do lock */);
7334
7335 return VK_SUCCESS;
7336 }
7337
7338 VkResult ResourceTracker::on_vkResetCommandBuffer(void* context, VkResult input_result,
7339 VkCommandBuffer commandBuffer,
7340 VkCommandBufferResetFlags flags) {
7341 VkEncoder* enc = (VkEncoder*)context;
7342 (void)input_result;
7343
7344 if (!supportsDeferredCommands()) {
7345 VkResult res = enc->vkResetCommandBuffer(commandBuffer, flags, true /* do lock */);
7346 resetCommandBufferStagingInfo(commandBuffer, true /* also reset primaries */,
7347 true /* also clear pending descriptor sets */);
7348 return res;
7349 }
7350
7351 enc->vkResetCommandBufferAsyncGOOGLE(commandBuffer, flags, true /* do lock */);
7352 resetCommandBufferStagingInfo(commandBuffer, true /* also reset primaries */,
7353 true /* also clear pending descriptor sets */);
7354 return VK_SUCCESS;
7355 }
7356
7357 VkResult ResourceTracker::on_vkCreateImageView(void* context, VkResult input_result,
7358 VkDevice device,
7359 const VkImageViewCreateInfo* pCreateInfo,
7360 const VkAllocationCallbacks* pAllocator,
7361 VkImageView* pView) {
7362 VkEncoder* enc = (VkEncoder*)context;
7363 (void)input_result;
7364
7365 VkImageViewCreateInfo localCreateInfo = vk_make_orphan_copy(*pCreateInfo);
7366
7367 #if defined(VK_USE_PLATFORM_ANDROID_KHR)
7368 vk_struct_chain_iterator structChainIter = vk_make_chain_iterator(&localCreateInfo);
7369 if (pCreateInfo->format == VK_FORMAT_UNDEFINED) {
7370 std::lock_guard<std::recursive_mutex> lock(mLock);
7371
7372 auto it = info_VkImage.find(pCreateInfo->image);
7373 if (it != info_VkImage.end() && it->second.hasExternalFormat) {
7374 localCreateInfo.format = vk_format_from_fourcc(it->second.externalFourccFormat);
7375 }
7376 }
7377 VkSamplerYcbcrConversionInfo localVkSamplerYcbcrConversionInfo;
7378 const VkSamplerYcbcrConversionInfo* samplerYcbcrConversionInfo = vk_find_struct_const(pCreateInfo, SAMPLER_YCBCR_CONVERSION_INFO);
7379 if (samplerYcbcrConversionInfo) {
7380 if (samplerYcbcrConversionInfo->conversion != VK_YCBCR_CONVERSION_DO_NOTHING) {
7381 localVkSamplerYcbcrConversionInfo = vk_make_orphan_copy(*samplerYcbcrConversionInfo);
7382 vk_append_struct(&structChainIter, &localVkSamplerYcbcrConversionInfo);
7383 }
7384 }
7385 #endif
7386
7387 return enc->vkCreateImageView(device, &localCreateInfo, pAllocator, pView, true /* do lock */);
7388 }
7389
7390 void ResourceTracker::on_vkCmdExecuteCommands(void* context, VkCommandBuffer commandBuffer,
7391 uint32_t commandBufferCount,
7392 const VkCommandBuffer* pCommandBuffers) {
7393 VkEncoder* enc = (VkEncoder*)context;
7394
7395 if (!mFeatureInfo.hasVulkanQueueSubmitWithCommands) {
7396 enc->vkCmdExecuteCommands(commandBuffer, commandBufferCount, pCommandBuffers,
7397 true /* do lock */);
7398 return;
7399 }
7400
7401 struct goldfish_VkCommandBuffer* primary = as_goldfish_VkCommandBuffer(commandBuffer);
7402 for (uint32_t i = 0; i < commandBufferCount; ++i) {
7403 struct goldfish_VkCommandBuffer* secondary =
7404 as_goldfish_VkCommandBuffer(pCommandBuffers[i]);
7405 appendObject(&secondary->superObjects, primary);
7406 appendObject(&primary->subObjects, secondary);
7407 }
7408
7409 enc->vkCmdExecuteCommands(commandBuffer, commandBufferCount, pCommandBuffers,
7410 true /* do lock */);
7411 }
7412
7413 void ResourceTracker::on_vkCmdBindDescriptorSets(void* context, VkCommandBuffer commandBuffer,
7414 VkPipelineBindPoint pipelineBindPoint,
7415 VkPipelineLayout layout, uint32_t firstSet,
7416 uint32_t descriptorSetCount,
7417 const VkDescriptorSet* pDescriptorSets,
7418 uint32_t dynamicOffsetCount,
7419 const uint32_t* pDynamicOffsets) {
7420 VkEncoder* enc = (VkEncoder*)context;
7421
7422 if (mFeatureInfo.hasVulkanBatchedDescriptorSetUpdate)
7423 addPendingDescriptorSets(commandBuffer, descriptorSetCount, pDescriptorSets);
7424
7425 enc->vkCmdBindDescriptorSets(commandBuffer, pipelineBindPoint, layout, firstSet,
7426 descriptorSetCount, pDescriptorSets, dynamicOffsetCount,
7427 pDynamicOffsets, true /* do lock */);
7428 }
7429
7430 void ResourceTracker::on_vkCmdPipelineBarrier(
7431 void* context, VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
7432 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
7433 uint32_t memoryBarrierCount, const VkMemoryBarrier* pMemoryBarriers,
7434 uint32_t bufferMemoryBarrierCount, const VkBufferMemoryBarrier* pBufferMemoryBarriers,
7435 uint32_t imageMemoryBarrierCount, const VkImageMemoryBarrier* pImageMemoryBarriers) {
7436 VkEncoder* enc = (VkEncoder*)context;
7437
7438 std::vector<VkImageMemoryBarrier> updatedImageMemoryBarriers;
7439 updatedImageMemoryBarriers.reserve(imageMemoryBarrierCount);
7440 for (uint32_t i = 0; i < imageMemoryBarrierCount; i++) {
7441 VkImageMemoryBarrier barrier = pImageMemoryBarriers[i];
7442
7443 #ifdef VK_USE_PLATFORM_ANDROID_KHR
7444 // Unfortunetly, Android does not yet have a mechanism for sharing the expected
7445 // VkImageLayout when passing around AHardwareBuffer-s so many existing users
7446 // that import AHardwareBuffer-s into VkImage-s/VkDeviceMemory-s simply use
7447 // VK_IMAGE_LAYOUT_UNDEFINED. However, the Vulkan spec's image layout transition
7448 // sections says "If the old layout is VK_IMAGE_LAYOUT_UNDEFINED, the contents of
7449 // that range may be discarded." Some Vulkan drivers have been observed to actually
7450 // perform the discard which leads to AHardwareBuffer-s being unintentionally
7451 // cleared. See go/ahb-vkimagelayout for more information.
7452 if (barrier.srcQueueFamilyIndex != barrier.dstQueueFamilyIndex &&
7453 (barrier.srcQueueFamilyIndex == VK_QUEUE_FAMILY_EXTERNAL ||
7454 barrier.srcQueueFamilyIndex == VK_QUEUE_FAMILY_FOREIGN_EXT) &&
7455 barrier.oldLayout == VK_IMAGE_LAYOUT_UNDEFINED) {
7456 // This is not a complete solution as the Vulkan spec does not require that
7457 // Vulkan drivers perform a no-op in the case when oldLayout equals newLayout
7458 // but this has been observed to be enough to work for now to avoid clearing
7459 // out images.
7460 // TODO(b/236179843): figure out long term solution.
7461 barrier.oldLayout = barrier.newLayout;
7462 }
7463 #endif
7464
7465 updatedImageMemoryBarriers.push_back(barrier);
7466 }
7467
7468 enc->vkCmdPipelineBarrier(commandBuffer, srcStageMask, dstStageMask, dependencyFlags,
7469 memoryBarrierCount, pMemoryBarriers, bufferMemoryBarrierCount,
7470 pBufferMemoryBarriers, updatedImageMemoryBarriers.size(),
7471 updatedImageMemoryBarriers.data(), true /* do lock */);
7472 }
7473
7474 void ResourceTracker::on_vkCmdClearColorImage(void* context, VkCommandBuffer commandBuffer, VkImage image,
7475 VkImageLayout imageLayout, const VkClearColorValue* pColor,
7476 uint32_t rangeCount, const VkImageSubresourceRange* pRanges) {
7477 VkEncoder* enc = (VkEncoder*)context;
7478 if (!pColor) {
7479 mesa_loge("%s: Null VkClearColorValue requested", __func__);
7480 return;
7481 }
7482 auto imageInfoIt = info_VkImage.find(image);
7483 if (imageInfoIt == info_VkImage.end()) {
7484 mesa_loge("%s: Failed to find image required for vkCmdClearColorImage", __func__);
7485 return;
7486 }
7487
7488 VkClearColorValue convertedColor = *pColor;
7489
7490 #ifdef VK_USE_PLATFORM_ANDROID_KHR
7491 // Color buffer image on the host will be created with UNORM format to ensure
7492 // it'll have the identical parameters, so we need to convert the linearized
7493 // clear color back to sRGB at this point.
7494 // TODO(b/420857458): revise the allocation logic to support mutable formats better
7495 auto& imageInfo = imageInfoIt->second;
7496 VkFormat actualFormat = imageInfo.createInfo.format;
7497 if (imageInfo.hasAnb && srgbFormatNeedsConversionForClearColor(actualFormat)) {
7498 // Perform linear to srgb conversion
7499 // Backing image is UNORM for vkCmdClearColorImage so we convert pColor
7500 convertedColor.float32[0] = linearChannelToSRGB(convertedColor.float32[0]);
7501 convertedColor.float32[1] = linearChannelToSRGB(convertedColor.float32[1]);
7502 convertedColor.float32[2] = linearChannelToSRGB(convertedColor.float32[2]);
7503 }
7504 #endif
7505 enc->vkCmdClearColorImage(commandBuffer, image, imageLayout, &convertedColor, rangeCount, pRanges, true);
7506 return;
7507 }
7508
7509 void ResourceTracker::on_vkDestroyDescriptorSetLayout(void* context, VkDevice device,
7510 VkDescriptorSetLayout descriptorSetLayout,
7511 const VkAllocationCallbacks* pAllocator) {
7512 decDescriptorSetLayoutRef(context, device, descriptorSetLayout, pAllocator);
7513 }
7514
7515 VkResult ResourceTracker::on_vkAllocateCommandBuffers(
7516 void* context, VkResult input_result, VkDevice device,
7517 const VkCommandBufferAllocateInfo* pAllocateInfo, VkCommandBuffer* pCommandBuffers) {
7518 (void)input_result;
7519
7520 VkEncoder* enc = (VkEncoder*)context;
7521 VkResult res =
7522 enc->vkAllocateCommandBuffers(device, pAllocateInfo, pCommandBuffers, true /* do lock */);
7523 if (VK_SUCCESS != res) return res;
7524
7525 for (uint32_t i = 0; i < pAllocateInfo->commandBufferCount; ++i) {
7526 struct goldfish_VkCommandBuffer* cb = as_goldfish_VkCommandBuffer(pCommandBuffers[i]);
7527 cb->isSecondary = pAllocateInfo->level == VK_COMMAND_BUFFER_LEVEL_SECONDARY;
7528 cb->device = device;
7529 }
7530
7531 return res;
7532 }
7533
7534 #if defined(VK_USE_PLATFORM_ANDROID_KHR)
7535 VkResult ResourceTracker::exportSyncFdForQSRILocked(VkImage image, int* fd) {
7536 mesa_logd("%s: call for image %p host image handle 0x%llx\n", __func__, (void*)image,
7537 (unsigned long long)get_host_u64_VkImage(image));
7538
7539 if (mFeatureInfo.hasVirtioGpuNativeSync) {
7540 struct VirtGpuExecBuffer exec = {};
7541 struct gfxstreamCreateQSRIExportVK exportQSRI = {};
7542 VirtGpuDevice* instance = VirtGpuDevice::getInstance();
7543
7544 uint64_t hostImageHandle = get_host_u64_VkImage(image);
7545
7546 exportQSRI.hdr.opCode = GFXSTREAM_CREATE_QSRI_EXPORT_VK;
7547 exportQSRI.imageHandleLo = (uint32_t)hostImageHandle;
7548 exportQSRI.imageHandleHi = (uint32_t)(hostImageHandle >> 32);
7549
7550 exec.command = static_cast<void*>(&exportQSRI);
7551 exec.command_size = sizeof(exportQSRI);
7552 exec.flags = kFenceOut | kRingIdx;
7553 if (instance->execBuffer(exec, nullptr)) return VK_ERROR_OUT_OF_HOST_MEMORY;
7554
7555 *fd = exec.handle.osHandle;
7556 } else {
7557 #if GFXSTREAM_ENABLE_GUEST_GOLDFISH
7558 ensureSyncDeviceFd();
7559 goldfish_sync_queue_work(
7560 mSyncDeviceFd, get_host_u64_VkImage(image) /* the handle */,
7561 GOLDFISH_SYNC_VULKAN_QSRI /* thread handle (doubling as type field) */, fd);
7562 #endif
7563 }
7564
7565 MESA_TRACE_SCOPE("%s: got fd: %d\n", __func__, *fd);
7566 auto imageInfoIt = info_VkImage.find(image);
7567 if (imageInfoIt != info_VkImage.end()) {
7568 auto& imageInfo = imageInfoIt->second;
7569
7570 // Remove any pending QSRI sync fds that are already signaled.
7571 auto syncFdIt = imageInfo.pendingQsriSyncFds.begin();
7572 while (syncFdIt != imageInfo.pendingQsriSyncFds.end()) {
7573 int syncFd = *syncFdIt;
7574 int syncWaitRet = mSyncHelper->wait(syncFd, /*timeout msecs*/ 0);
7575 if (syncWaitRet == 0) {
7576 // Sync fd is signaled.
7577 syncFdIt = imageInfo.pendingQsriSyncFds.erase(syncFdIt);
7578 mSyncHelper->close(syncFd);
7579 } else {
7580 if (errno != ETIME) {
7581 mesa_loge("%s: Failed to wait for pending QSRI sync: sterror: %s errno: %d",
7582 __func__, strerror(errno), errno);
7583 }
7584 break;
7585 }
7586 }
7587
7588 int syncFdDup = mSyncHelper->dup(*fd);
7589 if (syncFdDup < 0) {
7590 mesa_loge("%s: Failed to dup() QSRI sync fd : sterror: %s errno: %d", __func__,
7591 strerror(errno), errno);
7592 } else {
7593 imageInfo.pendingQsriSyncFds.push_back(syncFdDup);
7594 }
7595 }
7596
7597 return VK_SUCCESS;
7598 }
7599
7600 VkResult ResourceTracker::on_vkQueueSignalReleaseImageANDROID(void* context, VkResult input_result,
7601 VkQueue queue,
7602 uint32_t waitSemaphoreCount,
7603 const VkSemaphore* pWaitSemaphores,
7604 VkImage image, int* pNativeFenceFd) {
7605 (void)input_result;
7606
7607 VkEncoder* enc = (VkEncoder*)context;
7608
7609 if (!mFeatureInfo.hasVulkanAsyncQsri) {
7610 return enc->vkQueueSignalReleaseImageANDROID(queue, waitSemaphoreCount, pWaitSemaphores,
7611 image, pNativeFenceFd, true /* lock */);
7612 }
7613
7614 {
7615 std::lock_guard<std::recursive_mutex> lock(mLock);
7616 auto it = info_VkImage.find(image);
7617 if (it == info_VkImage.end()) {
7618 if (pNativeFenceFd) *pNativeFenceFd = -1;
7619 return VK_ERROR_INITIALIZATION_FAILED;
7620 }
7621 }
7622
7623 enc->vkQueueSignalReleaseImageANDROIDAsyncGOOGLE(queue, waitSemaphoreCount, pWaitSemaphores,
7624 image, true /* lock */);
7625
7626 std::lock_guard<std::recursive_mutex> lock(mLock);
7627 VkResult result;
7628 if (pNativeFenceFd) {
7629 result = exportSyncFdForQSRILocked(image, pNativeFenceFd);
7630 } else {
7631 int syncFd;
7632 result = exportSyncFdForQSRILocked(image, &syncFd);
7633
7634 if (syncFd >= 0) {
7635 mSyncHelper->close(syncFd);
7636 }
7637 }
7638
7639 return result;
7640 }
7641 #endif
7642
7643 VkResult ResourceTracker::on_vkCreateGraphicsPipelines(
7644 void* context, VkResult input_result, VkDevice device, VkPipelineCache pipelineCache,
7645 uint32_t createInfoCount, const VkGraphicsPipelineCreateInfo* pCreateInfos,
7646 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines) {
7647 (void)input_result;
7648 VkEncoder* enc = (VkEncoder*)context;
7649 std::vector<VkGraphicsPipelineCreateInfo> localCreateInfos(pCreateInfos,
7650 pCreateInfos + createInfoCount);
7651 for (VkGraphicsPipelineCreateInfo& graphicsPipelineCreateInfo : localCreateInfos) {
7652 // dEQP-VK.api.pipeline.pipeline_invalid_pointers_unused_structs#graphics
7653 bool requireViewportState = false;
7654 // VUID-VkGraphicsPipelineCreateInfo-rasterizerDiscardEnable-00750
7655 requireViewportState |=
7656 graphicsPipelineCreateInfo.pRasterizationState != nullptr &&
7657 graphicsPipelineCreateInfo.pRasterizationState->rasterizerDiscardEnable == VK_FALSE;
7658 // VUID-VkGraphicsPipelineCreateInfo-pViewportState-04892
7659 #ifdef VK_EXT_extended_dynamic_state2
7660 if (!requireViewportState && graphicsPipelineCreateInfo.pDynamicState) {
7661 for (uint32_t i = 0; i < graphicsPipelineCreateInfo.pDynamicState->dynamicStateCount;
7662 i++) {
7663 if (VK_DYNAMIC_STATE_RASTERIZER_DISCARD_ENABLE_EXT ==
7664 graphicsPipelineCreateInfo.pDynamicState->pDynamicStates[i]) {
7665 requireViewportState = true;
7666 break;
7667 }
7668 }
7669 }
7670 #endif // VK_EXT_extended_dynamic_state2
7671 if (!requireViewportState) {
7672 graphicsPipelineCreateInfo.pViewportState = nullptr;
7673 }
7674
7675 // It has the same requirement as for pViewportState.
7676 bool shouldIncludeFragmentShaderState = requireViewportState;
7677
7678 // VUID-VkGraphicsPipelineCreateInfo-rasterizerDiscardEnable-00751
7679 if (!shouldIncludeFragmentShaderState) {
7680 graphicsPipelineCreateInfo.pMultisampleState = nullptr;
7681 }
7682
7683 bool forceDepthStencilState = false;
7684 bool forceColorBlendState = false;
7685
7686 const VkPipelineRenderingCreateInfo* pipelineRenderingInfo =
7687 vk_find_struct_const(&graphicsPipelineCreateInfo, PIPELINE_RENDERING_CREATE_INFO);
7688
7689 if (pipelineRenderingInfo) {
7690 forceDepthStencilState |=
7691 pipelineRenderingInfo->depthAttachmentFormat != VK_FORMAT_UNDEFINED;
7692 forceDepthStencilState |=
7693 pipelineRenderingInfo->stencilAttachmentFormat != VK_FORMAT_UNDEFINED;
7694 forceColorBlendState |= pipelineRenderingInfo->colorAttachmentCount != 0;
7695 }
7696
7697 // VUID-VkGraphicsPipelineCreateInfo-renderPass-06043
7698 // VUID-VkGraphicsPipelineCreateInfo-renderPass-06044
7699 if (graphicsPipelineCreateInfo.renderPass == VK_NULL_HANDLE ||
7700 !shouldIncludeFragmentShaderState) {
7701 // VUID-VkGraphicsPipelineCreateInfo-renderPass-06053
7702 if (!forceDepthStencilState) {
7703 graphicsPipelineCreateInfo.pDepthStencilState = nullptr;
7704 }
7705 if (!forceColorBlendState) {
7706 graphicsPipelineCreateInfo.pColorBlendState = nullptr;
7707 }
7708 }
7709 }
7710 return enc->vkCreateGraphicsPipelines(device, pipelineCache, localCreateInfos.size(),
7711 localCreateInfos.data(), pAllocator, pPipelines,
7712 true /* do lock */);
7713 }
7714
7715 uint32_t ResourceTracker::getApiVersionFromInstance(VkInstance instance) {
7716 std::lock_guard<std::recursive_mutex> lock(mLock);
7717 uint32_t api = kDefaultApiVersion;
7718
7719 auto it = info_VkInstance.find(instance);
7720 if (it == info_VkInstance.end()) return api;
7721
7722 api = it->second.highestApiVersion;
7723
7724 return api;
7725 }
7726
7727 uint32_t ResourceTracker::getApiVersionFromDevice(VkDevice device) {
7728 std::lock_guard<std::recursive_mutex> lock(mLock);
7729
7730 uint32_t api = kDefaultApiVersion;
7731
7732 auto it = info_VkDevice.find(device);
7733 if (it == info_VkDevice.end()) return api;
7734
7735 api = it->second.apiVersion;
7736
7737 return api;
7738 }
7739
7740 #ifdef LINUX_GUEST_BUILD
7741 bool ResourceTracker::doImageDrmFormatModifierEmulation(VkPhysicalDevice physicalDevice) {
7742 std::lock_guard<std::recursive_mutex> lock(mLock);
7743
7744 bool hostSupportsImageDrmFormatModifiers =
7745 getHostDeviceExtensionIndex(VK_EXT_IMAGE_DRM_FORMAT_MODIFIER_EXTENSION_NAME) != -1;
7746
7747 // If the host device supports the extension, then this gets passed through for
7748 // all functionality relating to the extension. If it doesn't, then this is emulated
7749 // in the guest-side driver
7750 return !hostSupportsImageDrmFormatModifiers;
7751 }
7752 #endif
7753
7754 VkDevice ResourceTracker::getDevice(VkCommandBuffer commandBuffer) const {
7755 struct goldfish_VkCommandBuffer* cb = as_goldfish_VkCommandBuffer(commandBuffer);
7756 if (!cb) {
7757 return nullptr;
7758 }
7759 return cb->device;
7760 }
7761
7762 // Resets staging stream for this command buffer and primary command buffers
7763 // where this command buffer has been recorded. If requested, also clears the pending
7764 // descriptor sets.
7765 void ResourceTracker::resetCommandBufferStagingInfo(VkCommandBuffer commandBuffer,
7766 bool alsoResetPrimaries,
7767 bool alsoClearPendingDescriptorSets) {
7768 struct goldfish_VkCommandBuffer* cb = as_goldfish_VkCommandBuffer(commandBuffer);
7769 if (!cb) {
7770 return;
7771 }
7772 if (cb->privateEncoder) {
7773 sStaging.pushStaging((CommandBufferStagingStream*)cb->privateStream, cb->privateEncoder);
7774 cb->privateEncoder = nullptr;
7775 cb->privateStream = nullptr;
7776 }
7777
7778 if (alsoClearPendingDescriptorSets && cb->userPtr) {
7779 CommandBufferPendingDescriptorSets* pendingSets =
7780 (CommandBufferPendingDescriptorSets*)cb->userPtr;
7781 pendingSets->sets.clear();
7782 }
7783
7784 if (alsoResetPrimaries) {
7785 forAllObjects(cb->superObjects, [this, alsoResetPrimaries,
7786 alsoClearPendingDescriptorSets](void* obj) {
7787 VkCommandBuffer superCommandBuffer = (VkCommandBuffer)obj;
7788 this->resetCommandBufferStagingInfo(superCommandBuffer, alsoResetPrimaries,
7789 alsoClearPendingDescriptorSets);
7790 });
7791 eraseObjects(&cb->superObjects);
7792 }
7793
7794 forAllObjects(cb->subObjects, [cb](void* obj) {
7795 VkCommandBuffer subCommandBuffer = (VkCommandBuffer)obj;
7796 struct goldfish_VkCommandBuffer* subCb = as_goldfish_VkCommandBuffer(subCommandBuffer);
7797 // We don't do resetCommandBufferStagingInfo(subCommandBuffer)
7798 // since the user still might have submittable stuff pending there.
7799 eraseObject(&subCb->superObjects, (void*)cb);
7800 });
7801
7802 eraseObjects(&cb->subObjects);
7803 }
7804
7805 // Unlike resetCommandBufferStagingInfo, this does not always erase its
7806 // superObjects pointers because the command buffer has merely been
7807 // submitted, not reset. However, if the command buffer was recorded with
7808 // ONE_TIME_SUBMIT_BIT, then it will also reset its primaries.
7809 //
7810 // Also, we save the set of descriptor sets referenced by this command
7811 // buffer because we only submitted the command buffer and it's possible to
7812 // update the descriptor set again and re-submit the same command without
7813 // recording it (Update-after-bind descriptor sets)
7814 void ResourceTracker::resetCommandBufferPendingTopology(VkCommandBuffer commandBuffer) {
7815 struct goldfish_VkCommandBuffer* cb = as_goldfish_VkCommandBuffer(commandBuffer);
7816 if (cb->flags & VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT) {
7817 resetCommandBufferStagingInfo(commandBuffer, true /* reset primaries */,
7818 true /* clear pending descriptor sets */);
7819 } else {
7820 resetCommandBufferStagingInfo(commandBuffer, false /* Don't reset primaries */,
7821 false /* Don't clear pending descriptor sets */);
7822 }
7823 }
7824
7825 void ResourceTracker::resetCommandPoolStagingInfo(VkCommandPool commandPool) {
7826 struct goldfish_VkCommandPool* p = as_goldfish_VkCommandPool(commandPool);
7827
7828 if (!p) return;
7829
7830 forAllObjects(p->subObjects, [this](void* commandBuffer) {
7831 this->resetCommandBufferStagingInfo((VkCommandBuffer)commandBuffer,
7832 true /* also reset primaries */,
7833 true /* also clear pending descriptor sets */);
7834 });
7835 }
7836
7837 void ResourceTracker::addToCommandPool(VkCommandPool commandPool, uint32_t commandBufferCount,
7838 VkCommandBuffer* pCommandBuffers) {
7839 for (uint32_t i = 0; i < commandBufferCount; ++i) {
7840 struct goldfish_VkCommandPool* p = as_goldfish_VkCommandPool(commandPool);
7841 struct goldfish_VkCommandBuffer* cb = as_goldfish_VkCommandBuffer(pCommandBuffers[i]);
7842 appendObject(&p->subObjects, (void*)(pCommandBuffers[i]));
7843 appendObject(&cb->poolObjects, (void*)commandPool);
7844 }
7845 }
7846
7847 void ResourceTracker::clearCommandPool(VkCommandPool commandPool) {
7848 resetCommandPoolStagingInfo(commandPool);
7849 struct goldfish_VkCommandPool* p = as_goldfish_VkCommandPool(commandPool);
7850 forAllObjects(p->subObjects, [this](void* commandBuffer) {
7851 this->unregister_VkCommandBuffer((VkCommandBuffer)commandBuffer);
7852 });
7853 eraseObjects(&p->subObjects);
7854 }
7855
7856 const VkPhysicalDeviceMemoryProperties& ResourceTracker::getPhysicalDeviceMemoryProperties(
7857 void* context, VkDevice device, VkPhysicalDevice physicalDevice) {
7858 if (!mCachedPhysicalDeviceMemoryProps) {
7859 if (physicalDevice == VK_NULL_HANDLE) {
7860 std::lock_guard<std::recursive_mutex> lock(mLock);
7861
7862 auto deviceInfoIt = info_VkDevice.find(device);
7863 if (deviceInfoIt == info_VkDevice.end()) {
7864 mesa_loge("Failed to pass device or physical device.");
7865 abort();
7866 }
7867 const auto& deviceInfo = deviceInfoIt->second;
7868 physicalDevice = deviceInfo.physdev;
7869 }
7870
7871 VkEncoder* enc = (VkEncoder*)context;
7872
7873 VkPhysicalDeviceMemoryProperties properties;
7874 enc->vkGetPhysicalDeviceMemoryProperties(physicalDevice, &properties, true /* no lock */);
7875
7876 mCachedPhysicalDeviceMemoryProps.emplace(std::move(properties));
7877 }
7878 return *mCachedPhysicalDeviceMemoryProps;
7879 }
7880
7881 static ResourceTracker* sTracker = nullptr;
7882
7883 ResourceTracker::ResourceTracker() {
7884 mCreateMapping = new CreateMapping();
7885 mDestroyMapping = new DestroyMapping();
7886 // nothing to do
7887 }
7888
7889 ResourceTracker::~ResourceTracker() {
7890 delete mCreateMapping;
7891 delete mDestroyMapping;
7892 }
7893
7894 VulkanHandleMapping* ResourceTracker::createMapping() { return mCreateMapping; }
7895
7896 VulkanHandleMapping* ResourceTracker::destroyMapping() { return mDestroyMapping; }
7897
7898 // static
7899 ResourceTracker* ResourceTracker::get() {
7900 if (!sTracker) {
7901 // To be initialized once on vulkan device open.
7902 sTracker = new ResourceTracker;
7903 }
7904 return sTracker;
7905 }
7906
7907 // static
7908 ALWAYS_INLINE_GFXSTREAM VkEncoder* ResourceTracker::getCommandBufferEncoder(
7909 VkCommandBuffer commandBuffer) {
7910 if (!(ResourceTracker::streamFeatureBits &
7911 VULKAN_STREAM_FEATURE_QUEUE_SUBMIT_WITH_COMMANDS_BIT)) {
7912 auto enc = ResourceTracker::getThreadLocalEncoder();
7913 ResourceTracker::get()->syncEncodersForCommandBuffer(commandBuffer, enc);
7914 return enc;
7915 }
7916
7917 struct goldfish_VkCommandBuffer* cb = as_goldfish_VkCommandBuffer(commandBuffer);
7918 if (!cb->privateEncoder) {
7919 sStaging.setAllocFree(ResourceTracker::get()->getAlloc(),
7920 ResourceTracker::get()->getFree());
7921 sStaging.popStaging((CommandBufferStagingStream**)&cb->privateStream, &cb->privateEncoder);
7922 }
7923 uint8_t* writtenPtr;
7924 size_t written;
7925 ((CommandBufferStagingStream*)cb->privateStream)->getWritten(&writtenPtr, &written);
7926 return cb->privateEncoder;
7927 }
7928
7929 // static
7930 ALWAYS_INLINE_GFXSTREAM VkEncoder* ResourceTracker::getQueueEncoder(VkQueue queue) {
7931 auto enc = ResourceTracker::getThreadLocalEncoder();
7932 if (!(ResourceTracker::streamFeatureBits &
7933 VULKAN_STREAM_FEATURE_QUEUE_SUBMIT_WITH_COMMANDS_BIT)) {
7934 ResourceTracker::get()->syncEncodersForQueue(queue, enc);
7935 }
7936 return enc;
7937 }
7938
7939 // static
7940 ALWAYS_INLINE_GFXSTREAM VkEncoder* ResourceTracker::getThreadLocalEncoder() {
7941 auto hostConn = ResourceTracker::threadingCallbacks.hostConnectionGetFunc();
7942 auto vkEncoder = ResourceTracker::threadingCallbacks.vkEncoderGetFunc(hostConn);
7943 return vkEncoder;
7944 }
7945
7946 // static
7947 void ResourceTracker::setSeqnoPtr(uint32_t* seqnoptr) { sSeqnoPtr = seqnoptr; }
7948
7949 // static
7950 ALWAYS_INLINE_GFXSTREAM uint32_t ResourceTracker::nextSeqno() {
7951 uint32_t res = __atomic_add_fetch(sSeqnoPtr, 1, __ATOMIC_SEQ_CST);
7952 return res;
7953 }
7954
7955 // static
7956 ALWAYS_INLINE_GFXSTREAM uint32_t ResourceTracker::getSeqno() {
7957 uint32_t res = __atomic_load_n(sSeqnoPtr, __ATOMIC_SEQ_CST);
7958 return res;
7959 }
7960
7961 void ResourceTracker::transformImpl_VkExternalMemoryProperties_tohost(VkExternalMemoryProperties*,
7962 uint32_t) {}
7963
7964 void ResourceTracker::transformImpl_VkImageCreateInfo_fromhost(const VkImageCreateInfo*, uint32_t) {
7965 }
7966 void ResourceTracker::transformImpl_VkImageCreateInfo_tohost(const VkImageCreateInfo*, uint32_t) {}
7967
7968 #define DEFINE_TRANSFORMED_TYPE_IMPL(type) \
7969 void ResourceTracker::transformImpl_##type##_tohost(type*, uint32_t) {} \
7970 void ResourceTracker::transformImpl_##type##_fromhost(type*, uint32_t) {}
7971
7972 LIST_TRIVIAL_TRANSFORMED_TYPES(DEFINE_TRANSFORMED_TYPE_IMPL)
7973
7974 } // namespace vk
7975 } // namespace gfxstream