#ifdef UNSAFE_BUFFERS_BUILD
#pragma allow_unsafe_buffers
#endif
#include "gpu/vulkan/vulkan_memory.h"
#include <vulkan/vulkan.h>
#include <optional>
#include "base/logging.h"
#include "build/build_config.h"
#include "gpu/vulkan/vulkan_device_queue.h"
#include "gpu/vulkan/vulkan_function_pointers.h"
namespace gpu {
namespace {
std::optional<uint32_t> FindMemoryTypeIndex(
VkPhysicalDevice physical_device,
const VkMemoryRequirements* requirements,
VkMemoryPropertyFlags flags) { … }
}
VulkanMemory::VulkanMemory(base::PassKey<VulkanMemory> pass_key) { … }
VulkanMemory::~VulkanMemory() { … }
std::unique_ptr<VulkanMemory> VulkanMemory::Create(
VulkanDeviceQueue* device_queue,
VkDeviceMemory device_memory,
VkDeviceSize size,
uint32_t type_index) { … }
std::unique_ptr<VulkanMemory> VulkanMemory::Create(
VulkanDeviceQueue* device_queue,
const VkMemoryRequirements* requirements,
const void* extra_allocate_info) { … }
void VulkanMemory::Destroy() { … }
bool VulkanMemory::Initialize(VulkanDeviceQueue* device_queue,
const VkMemoryRequirements* requirements,
const void* extra_allocate_info) { … }
#if BUILDFLAG(IS_POSIX)
base::ScopedFD VulkanMemory::GetMemoryFd(
VkExternalMemoryHandleTypeFlagBits handle_type) { … }
#endif
#if BUILDFLAG(IS_WIN)
base::win::ScopedHandle VulkanMemory::GetMemoryHandle(
VkExternalMemoryHandleTypeFlagBits handle_type) {
VkMemoryGetWin32HandleInfoKHR get_handle_info = {
.sType = VK_STRUCTURE_TYPE_MEMORY_GET_WIN32_HANDLE_INFO_KHR,
.memory = device_memory(),
.handleType = handle_type,
};
VkDevice device = device_queue_->GetVulkanDevice();
HANDLE handle = nullptr;
vkGetMemoryWin32HandleKHR(device, &get_handle_info, &handle);
if (handle == nullptr) {
DLOG(ERROR) << "Unable to extract file handle out of external VkImage";
return base::win::ScopedHandle();
}
return base::win::ScopedHandle(handle);
}
#endif
#if BUILDFLAG(IS_FUCHSIA)
zx::vmo VulkanMemory::GetMemoryZirconHandle() {
VkMemoryGetZirconHandleInfoFUCHSIA get_handle_info = {
.sType = VK_STRUCTURE_TYPE_MEMORY_GET_ZIRCON_HANDLE_INFO_FUCHSIA,
.memory = device_memory(),
.handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_ZIRCON_VMO_BIT_FUCHSIA,
};
VkDevice device = device_queue_->GetVulkanDevice();
zx::vmo vmo;
VkResult result = vkGetMemoryZirconHandleFUCHSIA(device, &get_handle_info,
vmo.reset_and_get_address());
if (result != VK_SUCCESS) {
DLOG(ERROR) << "vkGetMemoryFuchsiaHandleKHR failed: " << result;
vmo.reset();
}
return vmo;
}
#endif
}