gfxstream: mega-change to support guest Linux WSI with gfxstream
This is a mega-change to support Linux guest WSI with gfxstream.
We tried to do a branch where every commit was buildable and
runnable, but that quickly proved unworkable. So we squashed
the branch into a mega-change.
Zink provides the GL implementation for Linux guests, so we just
needed to implement the proper Vulkan Wayland/X11 WSI
entrypoints.
The overall strategy to support this is to use Mesa's WSI
functions. The Vulkan WSI layer was also considered:
https://gitlab.freedesktop.org/mesa/vulkan-wsi-layer
But it was less maintained compared to Mesa. The way Mesa common
layers communicate with drivers is the through base objects
embedded in driver and a common dispatch layer:
https://gitlab.freedesktop.org/mesa/mesa/-/blob/main/docs/vulkan/dispatch.rst
https://gitlab.freedesktop.org/mesa/mesa/-/blob/main/docs/vulkan/base-objs.rst
Our objects are defined in gfxstream_vk_private.h. Currently,
Mesa-derived Vulkan objects just serve as shim to gfxstream
Vulkan’s internal handle mapping. Long-term, we can use
Mesa-derived objects inside gfxstream guest Vulkan exclusively.
The flow is typically inside a Vulkan entrypoint is:
- VK_FROM_HANDLE(vk-object) to convert to a gfxstream_vk_obj
object
- Call ResourceTracker::func(gfxstream_vk_obj->internal) or
VkEncoder::func(gfxstream_vk_obj>internal)
- Return result
A good follow-up cleanup would be to delete older gfxstream
objects. For example, we now have struct gfxstream_vk_device
and info_VkDevice in ResourceTracker.
Most of this logic was auto-generated and included in
func_table.cpp. Some vulkan functions were too difficult to
auto-generate or required special logic, and these are included
in gfxstream_vk_device.cpp. For example, anything that needs to
setup the HostConnection requires special handling.
Android Blueprint support is added to the parts of Mesa needed
to build the Vulkan runtime. One thing to call out it's
required to build the guest/vulkan_enc and guest/vulkan files
in the same shared library now, when previously have
libvulkan_enc.so and libvulkan_ranchu.so was sufficient
[otherwise, some weak pointer logic wouldn't work].
A side effect of this is libOpenglSystem must also be a static
lib, and so should libandroid_aemu too. That conceptually makes
sense and the Meson build had been doing this all a long. We
can probably transition everything besides libGLESv1_emulation.so,
libGLESv2_emulation.so and libvulkan_ranchu.so to be static.
This requires changes in the end2end tests, because since each
HostConnection is separate and internal to it's constituent
library. Lifetimes need to be managed separately: for example
the HostConnection instance created by the end2end tests would
not be visible inside libvulkan_ranchu.so anymore. Probably the
best solution would be improve the testing facade so a
HostConnection represents one virtio-gpu context, while some
other entity represents a virtio-gpu device (client-server
would work).
vk.xml was modified, but change sent to Khronos:
https://gitlab.khronos.org/vulkan/vulkan/-/merge_requests/6325
Fuchsia builds still need to be migrated, but they already have
Fuchsia Mesa with all the build rules so that shouldn't be too
bad. Just need to copy them over the gfxstream/Mesa hybrid.
The new command for building Linux guests is:
meson amd64-build/ -Dvulkan-drivers="gfxstream" -Dgallium-drivers="" -Dvk-no-nir=true -Dopengl=false
Big shout-out to Aaron Ruby, who did most of the gnarly codegen
needed to get the function table logic to work.
BUG=313466265
BUG=298679844
TEST=
* Run Weston/vkcube on Linux and automotive platform
* launch_cvd --gpu_mode=gfxstream vkcube
* launch_cvd --gpu_mod=gfxstream_guest_angle
* vkcube + 3D Mark Slingshot extreme work with guest ANGLE and
GL-VK interop
* GfxstreamEnd2EndTests
* Some select dEQP tests
Aaron Ruby (46):
gfxstream: function table: remove entry points that are hand-written.
gfxstream: function table: more changes
gfxstream: function table: scope internal_arrays to encoder
gfxstream: function table: autogenerate compoundType params
gfxstream: add handwritten EnumeratePhysicalDeviceGroup entrypoint.
gfxstream: function table: handle nested handle arrays
gfxstream: function table: adding some handwritten implementations
gfxstream: revert some unnecessary changes
gfxstream: use vk_object_zalloc/free instead of vk_zalloc/free.
gfxstream: revert most gfxstream objects to use vk_object_base
gfxstream: function table: handwritten commmand-buffers/pools
gfxstream: codegen functionality to handle special param
gfxstream: function table: random fixes
gfxstream: add vk_command_buffer_ops handlers
gfxstream: func_table.py: Codegen support for nested compound type
gfxstream: remove handwritten/add autogen entry points
gfxstream: add gfxstream_vk_device.cpp
gfxstream: query device and instance extensions early
gfxstream: func_table: explicit allocation for nested arrays/compound
types
gfxstream: goldfish_vulkan: fix commandBuffer allocation.
gfxstream: meson: Raise api_version in ICD config to 1.1.
gfxstream: function table: add more handwritten entries
gfxstream: goldfish_vulkan: update VkDescriptorSetAllocateInfo logic
gfxstream: function table: NULL check on internal_object dereference
gfxstream: function table: Remove POSTPROCESSES handling from
functable
gfxstream: mesa: Add 'gfxstream' as a -Dvulkan-drivers
gfxstream: ResourceTracker: add some allowedExtensions
gfxstream: gfxstream_vk_device: add wsi_common_entrypoints
gfxstream: Move instance handling into gfxstream_vk_device.cpp
gfxstream: ResourceTracker: Enable Linux WSI-related extensions
gfxstream: wsi: add wsi_device initialization
gfxstream: gfxstream_vk_device: use Mesa common physical device
management
gfxstream: ResourceTracker: translate mesa objects in user buffer
gfxstream: exclude VkSampler and VkDescriptorSet objects from
translation
gfxstream: Add guest-side external memory support with colorBuffers.
gfxstream: function table: Modify semaphoreList inputs to no-op
semaphores
gfxstream: function table: Allow VK_NULL_HANDLE for free/destroy APIs.
gfxstream: cereal: Add VK_EXT_depth_clip_enable as supported feature.
gfxstream: vulkan_enc: un-namespace vk_util.h and vk_struct_id.h
gfxstream: gfxstream_vk_device.cpp: Support VK_KHR_surface and
VK_*_surface
gfxstream: vulkan_enc: Add support for Mesa-only extensions.
gfxstream: ResourceTracker: Use DEVICE_TYPE_VIRTUAL_GPU always
gfxstream: platform: add dma-buf export support with dedicatedBuffer.
gfxstream: ResourceTracker: add VK_EXT_depth_clip_enable allowed
extension
gfxstream: ResourceTracker: external memory via QNX_screen_buffer
extension
gfxstream: Add VK_QNX_external_memory_screen_buffer to VulkanDispatch
Gurchetan Singh (18):
gfxstream: mesa: write Android.bp files
gfxstream: generate gfxstream_vk_entrypoints.{c, h}
gfxstream: vulkan_enc: add gfxstream_vk_private.h (objects)
gfxstream: function table: modify function table to use gfxstream_vk_*
gfxstream: compiles
gfxstream: build system improvements
gfxstream: ResourceTracker: don't crash without
VkBindImageMemorySwapchainInfoKHR
gfxstream: vk.xml: make some vkAcquireImageANDROID params optional
gfxstream_vk_device: filter out swapchain maintenance guest side
gfxstream: end2end: fixes for End2End tests
gfxstream: func_table: custom vkEnumerateInstanceLayerProperties
gfxstream: add VK_EXT_DEBUG_UTILS_EXTENSION_NAME into Mesa list
gfxstream: clang-format guest code
gfxstream: libandroid AEMU static
gfxstream: vkEnumerateInstanceVersion
gfxstream: vkCreateComputePipeLines
gfxstream: make end2end tests happy
gfxstream: delete prior vk.xml, vk_icd_gen.py
Change-Id: Ic4cc50d5098dddd77c9265afa8f432a2fdea8845
diff --git a/guest/vulkan_enc/ResourceTracker.cpp b/guest/vulkan_enc/ResourceTracker.cpp
index 4e4d0e7..202090a 100644
--- a/guest/vulkan_enc/ResourceTracker.cpp
+++ b/guest/vulkan_enc/ResourceTracker.cpp
@@ -23,6 +23,7 @@
#include "Resources.h"
#include "VkEncoder.h"
#include "aemu/base/AlignedBuf.h"
+#include "gfxstream_vk_private.h"
#include "goldfish_address_space.h"
#include "goldfish_vk_private_defs.h"
#include "util.h"
@@ -200,10 +201,10 @@
std::unordered_set<VkDescriptorSet> sets;
};
-#define HANDLE_REGISTER_IMPL_IMPL(type) \
- void ResourceTracker::register_##type(type obj) { \
- AutoLock<RecursiveLock> lock(mLock); \
- info_##type[obj] = type##_Info(); \
+#define HANDLE_REGISTER_IMPL_IMPL(type) \
+ void ResourceTracker::register_##type(type obj) { \
+ AutoLock<RecursiveLock> lock(mLock); \
+ info_##type[obj] = type##_Info(); \
}
#define HANDLE_UNREGISTER_IMPL_IMPL(type) \
@@ -286,7 +287,8 @@
return as_goldfish_VkDescriptorSet(dstSet)->reified->bindingIsImmutableSampler[dstBinding];
}
-VkDescriptorImageInfo ResourceTracker::filterNonexistentSampler(const VkDescriptorImageInfo& inputInfo) {
+VkDescriptorImageInfo ResourceTracker::filterNonexistentSampler(
+ const VkDescriptorImageInfo& inputInfo) {
VkSampler sampler = inputInfo.sampler;
VkDescriptorImageInfo res = inputInfo;
@@ -300,9 +302,11 @@
return res;
}
-void ResourceTracker::emitDeviceMemoryReport(VkDevice_Info info, VkDeviceMemoryReportEventTypeEXT type,
- uint64_t memoryObjectId, VkDeviceSize size, VkObjectType objectType,
- uint64_t objectHandle, uint32_t heapIndex) {
+void ResourceTracker::emitDeviceMemoryReport(VkDevice_Info info,
+ VkDeviceMemoryReportEventTypeEXT type,
+ uint64_t memoryObjectId, VkDeviceSize size,
+ VkObjectType objectType, uint64_t objectHandle,
+ uint32_t heapIndex) {
if (info.deviceMemoryReportCallbacks.empty()) return;
const VkDeviceMemoryReportCallbackDataEXT callbackData = {
@@ -640,9 +644,10 @@
createInfoDup.pNext = nullptr;
enc->vkGetLinearImageLayout2GOOGLE(device, &createInfoDup, &offset, &rowPitchAlignment,
true /* do lock */);
- ALOGD("vkGetLinearImageLayout2GOOGLE: format %d offset %lu "
- "rowPitchAlignment = %lu",
- (int)createInfo->format, offset, rowPitchAlignment);
+ ALOGD(
+ "vkGetLinearImageLayout2GOOGLE: format %d offset %lu "
+ "rowPitchAlignment = %lu",
+ (int)createInfo->format, offset, rowPitchAlignment);
}
imageConstraints.min_coded_width = createInfo->extent.width;
@@ -721,8 +726,8 @@
dedicatedReqs->requiresDedicatedAllocation = VK_TRUE;
}
-void ResourceTracker::setMemoryRequirementsForSysmemBackedImage(VkImage image,
- VkMemoryRequirements* pMemoryRequirements) {
+void ResourceTracker::transformImageMemoryRequirementsForGuestLocked(VkImage image,
+ VkMemoryRequirements* reqs) {
#ifdef VK_USE_PLATFORM_FUCHSIA
auto it = info_VkImage.find(image);
if (it == info_VkImage.end()) return;
@@ -730,21 +735,25 @@
if (info.isSysmemBackedMemory) {
auto width = info.createInfo.extent.width;
auto height = info.createInfo.extent.height;
- pMemoryRequirements->size = width * height * 4;
+ reqs->size = width * height * 4;
+ }
+#elif defined(__linux__) && !defined(VK_USE_PLATFORM_ANDROID_KHR)
+ auto it = info_VkImage.find(image);
+ if (it == info_VkImage.end()) return;
+ auto& info = it->second;
+ if (info.isWsiImage) {
+ static const uint32_t kColorBufferBpp = 4;
+ reqs->size = kColorBufferBpp * info.createInfo.extent.width * info.createInfo.extent.height;
}
#else
// Bypass "unused parameter" checks.
(void)image;
- (void)pMemoryRequirements;
+ (void)reqs;
#endif
}
-void ResourceTracker::transformImageMemoryRequirementsForGuestLocked(VkImage image,
- VkMemoryRequirements* reqs) {
- setMemoryRequirementsForSysmemBackedImage(image, reqs);
-}
-
-CoherentMemoryPtr ResourceTracker::freeCoherentMemoryLocked(VkDeviceMemory memory, VkDeviceMemory_Info& info) {
+CoherentMemoryPtr ResourceTracker::freeCoherentMemoryLocked(VkDeviceMemory memory,
+ VkDeviceMemory_Info& info) {
if (info.coherentMemory && info.ptr) {
if (info.coherentMemory->getDeviceMemory() != memory) {
delete_goldfish_VkDeviceMemory(memory);
@@ -1341,7 +1350,8 @@
void ResourceTracker::setDeviceMemoryInfo(VkDevice device, VkDeviceMemory memory,
VkDeviceSize allocationSize, uint8_t* ptr,
uint32_t memoryTypeIndex, AHardwareBuffer* ahw,
- bool imported, zx_handle_t vmoHandle) {
+ bool imported, zx_handle_t vmoHandle,
+ VirtGpuBlobPtr blobPtr) {
AutoLock<RecursiveLock> lock(mLock);
auto& info = info_VkDeviceMemory[memory];
@@ -1354,6 +1364,7 @@
#endif
info.imported = imported;
info.vmoHandle = vmoHandle;
+ info.blobPtr = blobPtr;
}
void ResourceTracker::setImageInfo(VkImage image, VkDevice device,
@@ -1763,6 +1774,7 @@
"VK_EXT_scalar_block_layout",
"VK_KHR_descriptor_update_template",
"VK_KHR_storage_buffer_storage_class",
+ "VK_EXT_depth_clip_enable",
#if defined(VK_USE_PLATFORM_ANDROID_KHR) || defined(__linux__)
"VK_KHR_external_semaphore",
"VK_KHR_external_semaphore_fd",
@@ -1772,7 +1784,7 @@
"VK_KHR_external_fence_fd",
"VK_EXT_device_memory_report",
#endif
-#if !defined(VK_USE_PLATFORM_ANDROID_KHR) && defined(__linux__)
+#if defined(__linux__) && !defined(VK_USE_PLATFORM_ANDROID_KHR)
"VK_KHR_create_renderpass2",
"VK_KHR_imageless_framebuffer",
#endif
@@ -1847,9 +1859,11 @@
bool win32ExtMemAvailable = getHostDeviceExtensionIndex("VK_KHR_external_memory_win32") != -1;
bool posixExtMemAvailable = getHostDeviceExtensionIndex("VK_KHR_external_memory_fd") != -1;
bool moltenVkExtAvailable = getHostDeviceExtensionIndex("VK_MVK_moltenvk") != -1;
+ bool qnxExtMemAvailable =
+ getHostDeviceExtensionIndex("VK_QNX_external_memory_screen_buffer") != -1;
bool hostHasExternalMemorySupport =
- win32ExtMemAvailable || posixExtMemAvailable || moltenVkExtAvailable;
+ win32ExtMemAvailable || posixExtMemAvailable || moltenVkExtAvailable || qnxExtMemAvailable;
if (hostHasExternalMemorySupport) {
#ifdef VK_USE_PLATFORM_ANDROID_KHR
@@ -2029,7 +2043,19 @@
}
void ResourceTracker::on_vkGetPhysicalDeviceProperties(void*, VkPhysicalDevice,
- VkPhysicalDeviceProperties*) {}
+ VkPhysicalDeviceProperties* pProperties) {
+#if defined(__linux__) && !defined(VK_USE_PLATFORM_ANDROID_KHR)
+ if (pProperties) {
+ if (VK_PHYSICAL_DEVICE_TYPE_CPU == pProperties->deviceType) {
+ /* For Linux guest: Even if host driver reports DEVICE_TYPE_CPU,
+ * override this to VIRTUAL_GPU, otherwise Linux DRM interfaces
+ * will take unexpected code paths to deal with "software" driver
+ */
+ pProperties->deviceType = VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU;
+ }
+ }
+#endif
+}
void ResourceTracker::on_vkGetPhysicalDeviceFeatures2(void*, VkPhysicalDevice,
VkPhysicalDeviceFeatures2* pFeatures) {
@@ -2048,7 +2074,8 @@
on_vkGetPhysicalDeviceFeatures2(context, physicalDevice, pFeatures);
}
-void ResourceTracker::on_vkGetPhysicalDeviceProperties2(void*, VkPhysicalDevice,
+void ResourceTracker::on_vkGetPhysicalDeviceProperties2(void* context,
+ VkPhysicalDevice physicalDevice,
VkPhysicalDeviceProperties2* pProperties) {
if (pProperties) {
VkPhysicalDeviceDeviceMemoryReportFeaturesEXT* memoryReportFeaturesEXT =
@@ -2056,6 +2083,7 @@
if (memoryReportFeaturesEXT) {
memoryReportFeaturesEXT->deviceMemoryReport = VK_TRUE;
}
+ on_vkGetPhysicalDeviceProperties(context, physicalDevice, &pProperties->properties);
}
}
@@ -2142,6 +2170,12 @@
}
}
+#if defined(VK_USE_PLATFORM_ANDROID_KHR) || defined(__linux__)
+void updateMemoryTypeBits(uint32_t* memoryTypeBits, uint32_t memoryIndex) {
+ *memoryTypeBits = 1u << memoryIndex;
+}
+#endif
+
#ifdef VK_USE_PLATFORM_ANDROID_KHR
VkResult ResourceTracker::on_vkGetAndroidHardwareBufferPropertiesANDROID(
@@ -2843,6 +2877,33 @@
}
#endif
+static uint32_t getVirglFormat(VkFormat vkFormat) {
+ uint32_t virglFormat = 0;
+
+ switch (vkFormat) {
+ case VK_FORMAT_R8G8B8A8_SINT:
+ case VK_FORMAT_R8G8B8A8_UNORM:
+ case VK_FORMAT_R8G8B8A8_SRGB:
+ case VK_FORMAT_R8G8B8A8_SNORM:
+ case VK_FORMAT_R8G8B8A8_SSCALED:
+ case VK_FORMAT_R8G8B8A8_USCALED:
+ virglFormat = VIRGL_FORMAT_R8G8B8A8_UNORM;
+ break;
+ case VK_FORMAT_B8G8R8A8_SINT:
+ case VK_FORMAT_B8G8R8A8_UNORM:
+ case VK_FORMAT_B8G8R8A8_SRGB:
+ case VK_FORMAT_B8G8R8A8_SNORM:
+ case VK_FORMAT_B8G8R8A8_SSCALED:
+ case VK_FORMAT_B8G8R8A8_USCALED:
+ virglFormat = VIRGL_FORMAT_B8G8R8A8_UNORM;
+ break;
+ default:
+ break;
+ }
+
+ return virglFormat;
+}
+
CoherentMemoryPtr ResourceTracker::createCoherentMemory(
VkDevice device, VkDeviceMemory mem, const VkMemoryAllocateInfo& hostAllocationInfo,
VkEncoder* enc, VkResult& res) {
@@ -3221,6 +3282,13 @@
const void* importAhbInfoPtr = nullptr;
#endif
+#if defined(__linux__) && !defined(VK_USE_PLATFORM_ANDROID_KHR)
+ const VkImportMemoryFdInfoKHR* importFdInfoPtr =
+ vk_find_struct<VkImportMemoryFdInfoKHR>(pAllocateInfo);
+#else
+ const VkImportMemoryFdInfoKHR* importFdInfoPtr = nullptr;
+#endif
+
#ifdef VK_USE_PLATFORM_FUCHSIA
const VkImportMemoryBufferCollectionFUCHSIA* importBufferCollectionInfoPtr =
vk_find_struct<VkImportMemoryBufferCollectionFUCHSIA>(pAllocateInfo);
@@ -3268,9 +3336,11 @@
// State needed for import/export.
bool exportAhb = false;
bool exportVmo = false;
+ bool exportDmabuf = false;
bool importAhb = false;
bool importBufferCollection = false;
bool importVmo = false;
+ bool importDmabuf = false;
(void)exportVmo;
// Even if we export allocate, the underlying operation
@@ -3291,6 +3361,9 @@
exportVmo = exportAllocateInfoPtr->handleTypes &
VK_EXTERNAL_MEMORY_HANDLE_TYPE_ZIRCON_VMO_BIT_FUCHSIA;
#endif // VK_USE_PLATFORM_FUCHSIA
+ exportDmabuf =
+ exportAllocateInfoPtr->handleTypes & (VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT |
+ VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT);
} else if (importAhbInfoPtr) {
importAhb = true;
} else if (importBufferCollectionInfoPtr) {
@@ -3298,7 +3371,13 @@
} else if (importVmoInfoPtr) {
importVmo = true;
}
- bool isImport = importAhb || importBufferCollection || importVmo;
+
+ if (importFdInfoPtr) {
+ importDmabuf =
+ (importFdInfoPtr->handleType & (VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT |
+ VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT));
+ }
+ bool isImport = importAhb || importBufferCollection || importVmo || importDmabuf;
#if defined(VK_USE_PLATFORM_ANDROID_KHR)
if (exportAhb) {
@@ -3671,7 +3750,96 @@
}
#endif
- if (ahw || !requestedMemoryIsHostVisible) {
+ VirtGpuBlobPtr colorBufferBlob = nullptr;
+#if defined(__linux__) && !defined(VK_USE_PLATFORM_ANDROID_KHR)
+ if (exportDmabuf) {
+ VirtGpuDevice* instance = VirtGpuDevice::getInstance();
+ // // TODO: any special action for VK_STRUCTURE_TYPE_WSI_MEMORY_ALLOCATE_INFO_MESA? Can mark
+ // special state if needed.
+ // // const wsi_memory_allocate_info* wsiAllocateInfoPtr =
+ // vk_find_struct<wsi_memory_allocate_info>(pAllocateInfo);
+ bool hasDedicatedImage =
+ dedicatedAllocInfoPtr && (dedicatedAllocInfoPtr->image != VK_NULL_HANDLE);
+ bool hasDedicatedBuffer =
+ dedicatedAllocInfoPtr && (dedicatedAllocInfoPtr->buffer != VK_NULL_HANDLE);
+ if (!hasDedicatedImage && !hasDedicatedBuffer) {
+ ALOGE(
+ "%s: dma-buf exportable memory requires dedicated Image or Buffer information.\n");
+ return VK_ERROR_OUT_OF_DEVICE_MEMORY;
+ }
+
+ if (hasDedicatedImage) {
+ VkImageCreateInfo imageCreateInfo;
+ {
+ AutoLock<RecursiveLock> lock(mLock);
+
+ auto it = info_VkImage.find(dedicatedAllocInfoPtr->image);
+ if (it == info_VkImage.end()) return VK_ERROR_INITIALIZATION_FAILED;
+ const auto& imageInfo = it->second;
+
+ imageCreateInfo = imageInfo.createInfo;
+ }
+ uint32_t virglFormat = gfxstream::vk::getVirglFormat(imageCreateInfo.format);
+ if (virglFormat < 0) {
+ ALOGE("%s: Unsupported VK format for colorBuffer, vkFormat: 0x%x", __func__,
+ imageCreateInfo.format);
+ return VK_ERROR_FORMAT_NOT_SUPPORTED;
+ }
+ colorBufferBlob = instance->createVirglBlob(imageCreateInfo.extent.width,
+ imageCreateInfo.extent.height, virglFormat);
+ if (!colorBufferBlob) {
+ ALOGE("%s: Failed to create colorBuffer resource for Image memory\n", __func__);
+ return VK_ERROR_OUT_OF_DEVICE_MEMORY;
+ }
+ if (0 != colorBufferBlob->wait()) {
+ ALOGE("%s: Failed to wait for colorBuffer resource for Image memory\n", __func__);
+ return VK_ERROR_OUT_OF_DEVICE_MEMORY;
+ }
+ }
+
+ if (hasDedicatedBuffer) {
+ VkBufferCreateInfo bufferCreateInfo;
+ {
+ AutoLock<RecursiveLock> lock(mLock);
+
+ auto it = info_VkBuffer.find(dedicatedAllocInfoPtr->buffer);
+ if (it == info_VkBuffer.end()) return VK_ERROR_INITIALIZATION_FAILED;
+ const auto& bufferInfo = it->second;
+ bufferCreateInfo = bufferInfo.createInfo;
+ }
+ colorBufferBlob = instance->createVirglBlob(bufferCreateInfo.size / 4, 1,
+ VIRGL_FORMAT_R8G8B8A8_UNORM);
+ if (!colorBufferBlob) {
+ ALOGE("%s: Failed to create colorBuffer resource for Buffer memory\n", __func__);
+ return VK_ERROR_OUT_OF_DEVICE_MEMORY;
+ }
+ if (0 != colorBufferBlob->wait()) {
+ ALOGE("%s: Failed to wait for colorBuffer resource for Buffer memory\n", __func__);
+ return VK_ERROR_OUT_OF_DEVICE_MEMORY;
+ }
+ }
+ }
+
+ if (importDmabuf) {
+ VirtGpuExternalHandle importHandle = {};
+ importHandle.osHandle = importFdInfoPtr->fd;
+ importHandle.type = kMemHandleDmabuf;
+
+ auto instance = VirtGpuDevice::getInstance();
+ colorBufferBlob = instance->importBlob(importHandle);
+ if (!colorBufferBlob) {
+ ALOGE("%s: Failed to import colorBuffer resource\n", __func__);
+ return VK_ERROR_OUT_OF_DEVICE_MEMORY;
+ }
+ }
+
+ if (colorBufferBlob) {
+ importCbInfo.colorBuffer = colorBufferBlob->getResourceHandle();
+ vk_append_struct(&structChainIter, &importCbInfo);
+ }
+#endif
+
+ if (ahw || colorBufferBlob || !requestedMemoryIsHostVisible) {
input_result =
enc->vkAllocateMemory(device, &finalAllocInfo, pAllocator, pMemory, true /* do lock */);
@@ -3679,7 +3847,7 @@
VkDeviceSize allocationSize = finalAllocInfo.allocationSize;
setDeviceMemoryInfo(device, *pMemory, 0, nullptr, finalAllocInfo.memoryTypeIndex, ahw,
- isImport, vmo_handle);
+ isImport, vmo_handle, colorBufferBlob);
_RETURN_SCUCCESS_WITH_DEVICE_MEMORY_REPORT;
}
@@ -3714,7 +3882,7 @@
setDeviceMemoryInfo(device, *pMemory, finalAllocInfo.allocationSize,
reinterpret_cast<uint8_t*>(addr), finalAllocInfo.memoryTypeIndex,
- /*ahw=*/nullptr, isImport, vmo_handle);
+ /*ahw=*/nullptr, isImport, vmo_handle, /*blobPtr=*/nullptr);
return VK_SUCCESS;
}
#endif
@@ -3857,11 +4025,11 @@
auto& info = it->second;
if (!info.external || !info.externalCreateInfo.handleTypes) {
- setMemoryRequirementsForSysmemBackedImage(image, &reqs2->memoryRequirements);
+ transformImageMemoryRequirementsForGuestLocked(image, &reqs2->memoryRequirements);
return;
}
- setMemoryRequirementsForSysmemBackedImage(image, &reqs2->memoryRequirements);
+ transformImageMemoryRequirementsForGuestLocked(image, &reqs2->memoryRequirements);
VkMemoryDedicatedRequirements* dedicatedReqs =
vk_find_struct<VkMemoryDedicatedRequirements>(reqs2);
@@ -3909,11 +4077,29 @@
const VkExternalMemoryImageCreateInfo* extImgCiPtr =
vk_find_struct<VkExternalMemoryImageCreateInfo>(pCreateInfo);
+
if (extImgCiPtr) {
localExtImgCi = vk_make_orphan_copy(*extImgCiPtr);
vk_append_struct(&structChainIter, &localExtImgCi);
}
+ bool isWsiImage = false;
+
+#if defined(__linux__) && !defined(VK_USE_PLATFORM_ANDROID_KHR)
+ if (extImgCiPtr &&
+ (extImgCiPtr->handleTypes & VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT)) {
+ // Assumes that handleType with DMA_BUF_BIT indicates creation of a
+ // image for WSI use; no other external dma_buf usage is supported
+ isWsiImage = true;
+ // Must be linear. Otherwise querying stride and other properties
+ // can be implementation-dependent.
+ localCreateInfo.tiling = VK_IMAGE_TILING_LINEAR;
+ if (gfxstream::vk::getVirglFormat(localCreateInfo.format) < 0) {
+ localCreateInfo.format = VK_FORMAT_R8G8B8A8_UNORM;
+ }
+ }
+#endif
+
#ifdef VK_USE_PLATFORM_ANDROID_KHR
VkNativeBufferANDROID localAnb;
const VkNativeBufferANDROID* anbInfoPtr = vk_find_struct<VkNativeBufferANDROID>(pCreateInfo);
@@ -4085,13 +4271,16 @@
}
#endif
+ info.isWsiImage = isWsiImage;
+
// Delete `protocolVersion` check goldfish drivers are gone.
-#ifdef VK_USE_PLATFORM_ANDROID_KHR
+#if defined(VK_USE_PLATFORM_ANDROID_KHR) || defined(__linux__)
if (mCaps.vulkanCapset.colorBufferMemoryIndex == 0xFFFFFFFF) {
mCaps.vulkanCapset.colorBufferMemoryIndex = getColorBufferMemoryIndex(context, device);
}
- if (extImgCiPtr && (extImgCiPtr->handleTypes &
- VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID)) {
+ if (isWsiImage ||
+ (extImgCiPtr && (extImgCiPtr->handleTypes &
+ VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID))) {
updateMemoryTypeBits(&memReqs.memoryTypeBits, mCaps.vulkanCapset.colorBufferMemoryIndex);
}
#endif
@@ -4981,7 +5170,6 @@
vk_append_struct(&structChainIter, &localExtBufCi);
}
-
VkBufferOpaqueCaptureAddressCreateInfo localCapAddrCi;
const VkBufferOpaqueCaptureAddressCreateInfo* pCapAddrCi =
vk_find_struct<VkBufferOpaqueCaptureAddressCreateInfo>(pCreateInfo);
@@ -5057,12 +5245,14 @@
if (res != VK_SUCCESS) return res;
-#ifdef VK_USE_PLATFORM_ANDROID_KHR
+#if defined(VK_USE_PLATFORM_ANDROID_KHR) || defined(__linux__)
if (mCaps.vulkanCapset.colorBufferMemoryIndex == 0xFFFFFFFF) {
mCaps.vulkanCapset.colorBufferMemoryIndex = getColorBufferMemoryIndex(context, device);
}
- if (extBufCiPtr && (extBufCiPtr->handleTypes &
- VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID)) {
+ if (extBufCiPtr &&
+ ((extBufCiPtr->handleTypes &
+ VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID) ||
+ (extBufCiPtr->handleTypes & VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT))) {
updateMemoryTypeBits(&memReqs.memoryTypeBits, mCaps.vulkanCapset.colorBufferMemoryIndex);
}
#endif
@@ -5354,6 +5544,53 @@
#endif
}
+VkResult ResourceTracker::on_vkGetMemoryFdKHR(void* context, VkResult, VkDevice device,
+ const VkMemoryGetFdInfoKHR* pGetFdInfo, int* pFd) {
+#if defined(__linux__) && !defined(VK_USE_PLATFORM_ANDROID_KHR)
+ if (!pGetFdInfo) return VK_ERROR_OUT_OF_HOST_MEMORY;
+ if (!pGetFdInfo->memory) return VK_ERROR_OUT_OF_HOST_MEMORY;
+
+ if (!(pGetFdInfo->handleType & (VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT |
+ VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT))) {
+ ALOGE("%s: Export operation not defined for handleType: 0x%x\n", __func__,
+ pGetFdInfo->handleType);
+ return VK_ERROR_OUT_OF_HOST_MEMORY;
+ }
+ // Sanity-check device
+ AutoLock<RecursiveLock> lock(mLock);
+ auto deviceIt = info_VkDevice.find(device);
+ if (deviceIt == info_VkDevice.end()) {
+ return VK_ERROR_OUT_OF_HOST_MEMORY;
+ }
+
+ auto deviceMemIt = info_VkDeviceMemory.find(pGetFdInfo->memory);
+ if (deviceMemIt == info_VkDeviceMemory.end()) {
+ return VK_ERROR_OUT_OF_HOST_MEMORY;
+ }
+ auto& info = deviceMemIt->second;
+
+ if (!info.blobPtr) {
+ ALOGE("%s: VkDeviceMemory does not have a resource available for export.\n", __func__);
+ return VK_ERROR_OUT_OF_HOST_MEMORY;
+ }
+
+ VirtGpuExternalHandle handle{};
+ int ret = info.blobPtr->exportBlob(handle);
+ if (ret != 0 || handle.osHandle < 0) {
+ ALOGE("%s: Failed to export host resource to FD.\n", __func__);
+ return VK_ERROR_OUT_OF_HOST_MEMORY;
+ }
+ *pFd = handle.osHandle;
+ return VK_SUCCESS;
+#else
+ (void)context;
+ (void)device;
+ (void)pGetFdInfo;
+ (void)pFd;
+ return VK_ERROR_INCOMPATIBLE_DRIVER;
+#endif
+}
+
void ResourceTracker::flushCommandBufferPendingCommandsBottomUp(
void* context, VkQueue queue, const std::vector<VkCommandBuffer>& workingSet) {
if (workingSet.empty()) return;
@@ -5704,9 +5941,14 @@
}
auto* gralloc = ResourceTracker::threadingCallbacks.hostConnectionGetFunc()->grallocHelper();
+ const native_handle_t* nativeHandle = (const native_handle_t*)inputNativeInfo->handle;
- *(uint32_t*)(outputNativeInfo->handle) =
- gralloc->getHostHandle((const native_handle_t*)inputNativeInfo->handle);
+#if defined(END2END_TESTS)
+ // This is valid since the testing backend creates the handle and we know the layout.
+ *(uint32_t*)(outputNativeInfo->handle) = (uint32_t)nativeHandle->data[0];
+#else
+ *(uint32_t*)(outputNativeInfo->handle) = gralloc->getHostHandle(nativeHandle);
+#endif
}
void ResourceTracker::unwrap_VkBindImageMemorySwapchainInfoKHR(
@@ -6040,6 +6282,14 @@
memcpy(((uint8_t*)imageInfos) + currImageInfoOffset, user,
sizeof(VkDescriptorImageInfo));
+#if defined(__linux__) && !defined(VK_USE_PLATFORM_ANDROID_KHR)
+ // Convert mesa to internal for objects in the user buffer
+ VkDescriptorImageInfo* internalImageInfo =
+ (VkDescriptorImageInfo*)(((uint8_t*)imageInfos) + currImageInfoOffset);
+ VK_FROM_HANDLE(gfxstream_vk_image_view, gfxstream_image_view,
+ internalImageInfo->imageView);
+ internalImageInfo->imageView = gfxstream_image_view->internal_object;
+#endif
currImageInfoOffset += sizeof(VkDescriptorImageInfo);
}
@@ -6059,6 +6309,13 @@
memcpy(((uint8_t*)bufferInfos) + currBufferInfoOffset, user,
sizeof(VkDescriptorBufferInfo));
+#if defined(__linux__) && !defined(VK_USE_PLATFORM_ANDROID_KHR)
+ // Convert mesa to internal for objects in the user buffer
+ VkDescriptorBufferInfo* internalBufferInfo =
+ (VkDescriptorBufferInfo*)(((uint8_t*)bufferInfos) + currBufferInfoOffset);
+ VK_FROM_HANDLE(gfxstream_vk_buffer, gfxstream_buffer, internalBufferInfo->buffer);
+ internalBufferInfo->buffer = gfxstream_buffer->internal_object;
+#endif
currBufferInfoOffset += sizeof(VkDescriptorBufferInfo);
}
@@ -6077,6 +6334,15 @@
const VkBufferView* user = (const VkBufferView*)(userBuffer + offset + j * stride);
memcpy(((uint8_t*)bufferViews) + currBufferViewOffset, user, sizeof(VkBufferView));
+#if defined(__linux__) && !defined(VK_USE_PLATFORM_ANDROID_KHR)
+ // Convert mesa to internal for objects in the user buffer
+ VkBufferView* internalBufferView =
+ (VkBufferView*)(((uint8_t*)bufferViews) + currBufferViewOffset);
+ VK_FROM_HANDLE(gfxstream_vk_buffer_view, gfxstream_buffer_view,
+ *internalBufferView);
+ *internalBufferView = gfxstream_buffer_view->internal_object;
+#endif
+
currBufferViewOffset += sizeof(VkBufferView);
}
@@ -6139,7 +6405,6 @@
VK_FORMAT_R8G8_SSCALED, VK_FORMAT_R8G8_SRGB,
};
-
if (ext_img_properties) {
if (std::find(std::begin(kExternalImageSupportedFormats),
std::end(kExternalImageSupportedFormats),
@@ -6154,7 +6419,7 @@
VkAndroidHardwareBufferUsageANDROID* output_ahw_usage =
vk_find_struct<VkAndroidHardwareBufferUsageANDROID>(pImageFormatProperties);
supportedHandleType |= VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT |
- VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID;
+ VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID;
#endif
const VkPhysicalDeviceExternalImageFormatInfo* ext_img_info =
vk_find_struct<VkPhysicalDeviceExternalImageFormatInfo>(pImageFormatInfo);
@@ -6201,7 +6466,8 @@
}
#endif
if (ext_img_properties) {
- transformImpl_VkExternalMemoryProperties_fromhost(&ext_img_properties->externalMemoryProperties, 0);
+ transformImpl_VkExternalMemoryProperties_fromhost(
+ &ext_img_properties->externalMemoryProperties, 0);
}
return hostRes;
}
@@ -6236,11 +6502,12 @@
#endif
#ifdef VK_USE_PLATFORM_ANDROID_KHR
supportedHandleType |= VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT |
- VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID;
+ VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID;
#endif
if (supportedHandleType) {
// 0 is a valid handleType so we can't check against 0
- if (pExternalBufferInfo->handleType != (pExternalBufferInfo->handleType & supportedHandleType)) {
+ if (pExternalBufferInfo->handleType !=
+ (pExternalBufferInfo->handleType & supportedHandleType)) {
return;
}
}
@@ -6252,7 +6519,8 @@
enc->vkGetPhysicalDeviceExternalBufferProperties(
physicalDevice, pExternalBufferInfo, pExternalBufferProperties, true /* do lock */);
}
- transformImpl_VkExternalMemoryProperties_fromhost(&pExternalBufferProperties->externalMemoryProperties, 0);
+ transformImpl_VkExternalMemoryProperties_fromhost(
+ &pExternalBufferProperties->externalMemoryProperties, 0);
}
void ResourceTracker::on_vkGetPhysicalDeviceExternalBufferProperties(
@@ -6261,8 +6529,7 @@
VkExternalBufferProperties* pExternalBufferProperties) {
return on_vkGetPhysicalDeviceExternalBufferProperties_common(
false /* not KHR */, context, physicalDevice, pExternalBufferInfo,
- pExternalBufferProperties
- );
+ pExternalBufferProperties);
}
void ResourceTracker::on_vkGetPhysicalDeviceExternalBufferPropertiesKHR(
@@ -6270,9 +6537,7 @@
const VkPhysicalDeviceExternalBufferInfoKHR* pExternalBufferInfo,
VkExternalBufferPropertiesKHR* pExternalBufferProperties) {
return on_vkGetPhysicalDeviceExternalBufferProperties_common(
- true /* is KHR */, context, physicalDevice, pExternalBufferInfo,
- pExternalBufferProperties
- );
+ true /* is KHR */, context, physicalDevice, pExternalBufferInfo, pExternalBufferProperties);
}
void ResourceTracker::on_vkGetPhysicalDeviceExternalSemaphoreProperties(
@@ -6776,8 +7041,10 @@
vk_find_struct<VkPipelineRenderingCreateInfo>(&graphicsPipelineCreateInfo);
if (pipelineRenderingInfo) {
- forceDepthStencilState |= pipelineRenderingInfo->depthAttachmentFormat != VK_FORMAT_UNDEFINED;
- forceDepthStencilState |= pipelineRenderingInfo->stencilAttachmentFormat != VK_FORMAT_UNDEFINED;
+ forceDepthStencilState |=
+ pipelineRenderingInfo->depthAttachmentFormat != VK_FORMAT_UNDEFINED;
+ forceDepthStencilState |=
+ pipelineRenderingInfo->stencilAttachmentFormat != VK_FORMAT_UNDEFINED;
forceColorBlendState |= pipelineRenderingInfo->colorAttachmentCount != 0;
}
@@ -6998,7 +7265,8 @@
}
// static
-ALWAYS_INLINE VkEncoder* ResourceTracker::getCommandBufferEncoder(VkCommandBuffer commandBuffer) {
+ALWAYS_INLINE_GFXSTREAM VkEncoder* ResourceTracker::getCommandBufferEncoder(
+ VkCommandBuffer commandBuffer) {
if (!(ResourceTracker::streamFeatureBits &
VULKAN_STREAM_FEATURE_QUEUE_SUBMIT_WITH_COMMANDS_BIT)) {
auto enc = ResourceTracker::getThreadLocalEncoder();
@@ -7019,7 +7287,7 @@
}
// static
-ALWAYS_INLINE VkEncoder* ResourceTracker::getQueueEncoder(VkQueue queue) {
+ALWAYS_INLINE_GFXSTREAM VkEncoder* ResourceTracker::getQueueEncoder(VkQueue queue) {
auto enc = ResourceTracker::getThreadLocalEncoder();
if (!(ResourceTracker::streamFeatureBits &
VULKAN_STREAM_FEATURE_QUEUE_SUBMIT_WITH_COMMANDS_BIT)) {
@@ -7029,7 +7297,7 @@
}
// static
-ALWAYS_INLINE VkEncoder* ResourceTracker::getThreadLocalEncoder() {
+ALWAYS_INLINE_GFXSTREAM VkEncoder* ResourceTracker::getThreadLocalEncoder() {
auto hostConn = ResourceTracker::threadingCallbacks.hostConnectionGetFunc();
auto vkEncoder = ResourceTracker::threadingCallbacks.vkEncoderGetFunc(hostConn);
return vkEncoder;
@@ -7039,13 +7307,13 @@
void ResourceTracker::setSeqnoPtr(uint32_t* seqnoptr) { sSeqnoPtr = seqnoptr; }
// static
-ALWAYS_INLINE uint32_t ResourceTracker::nextSeqno() {
+ALWAYS_INLINE_GFXSTREAM uint32_t ResourceTracker::nextSeqno() {
uint32_t res = __atomic_add_fetch(sSeqnoPtr, 1, __ATOMIC_SEQ_CST);
return res;
}
// static
-ALWAYS_INLINE uint32_t ResourceTracker::getSeqno() {
+ALWAYS_INLINE_GFXSTREAM uint32_t ResourceTracker::getSeqno() {
uint32_t res = __atomic_load_n(sSeqnoPtr, __ATOMIC_SEQ_CST);
return res;
}