Add ServiceImplementation class in external_memory

This CL introduced ServiceImplementation class. Service class deleate
its work to ServiceImplementation class.

Different platforms inherit ServiceImplementation class and implement
it to support Service class. It helps isolate implementaion and
interface. And it's the base for supporting choosing implementation
in runtime.

Bug: dawn:1593
Change-Id: Ib18dfaa41ef4a1ce9554a5241cac1ef0fede8dd8
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/125481
Reviewed-by: Austin Eng <enga@chromium.org>
Commit-Queue: Shaobo Yan <shaobo.yan@intel.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
This commit is contained in:
Yan,Shaobo 2023-03-28 06:33:41 +00:00 committed by Dawn LUCI CQ
parent eaedb8e7de
commit 8e414572be
12 changed files with 1005 additions and 755 deletions

View File

@ -696,8 +696,11 @@ source_set("sources") {
"vulkan/VulkanFunctions.h",
"vulkan/VulkanInfo.cpp",
"vulkan/VulkanInfo.h",
"vulkan/external_memory/MemoryImportParams.h",
"vulkan/external_memory/MemoryService.cpp",
"vulkan/external_memory/MemoryService.h",
"vulkan/external_memory/MemoryServiceImplementation.cpp",
"vulkan/external_memory/MemoryServiceImplementation.h",
"vulkan/external_semaphore/SemaphoreService.cpp",
"vulkan/external_semaphore/SemaphoreService.h",
"vulkan/external_semaphore/SemaphoreServiceImplementation.cpp",

View File

@ -569,8 +569,11 @@ if (DAWN_ENABLE_VULKAN)
"vulkan/VulkanFunctions.h"
"vulkan/VulkanInfo.cpp"
"vulkan/VulkanInfo.h"
"vulkan/external_memory/MemoryImportParams.h"
"vulkan/external_memory/MemoryService.cpp"
"vulkan/external_memory/MemoryService.h"
"vulkan/external_memory/MemoryServiceImplementation.cpp"
"vulkan/external_memory/MemoryServiceImplementation.h"
"vulkan/external_semaphore/SemaphoreService.cpp"
"vulkan/external_semaphore/SemaphoreService.h"
"vulkan/external_semaphore/SemaphoreServiceImplementation.cpp"

View File

@ -0,0 +1,30 @@
// Copyright 2023 The Dawn Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef SRC_DAWN_NATIVE_VULKAN_EXTERNAL_MEMORY_MEMORYIMPORTPARAMS_H_
#define SRC_DAWN_NATIVE_VULKAN_EXTERNAL_MEMORY_MEMORYIMPORTPARAMS_H_
#include "dawn/native/VulkanBackend.h"
namespace dawn::native::vulkan::external_memory {
struct MemoryImportParams {
VkDeviceSize allocationSize;
uint32_t memoryTypeIndex;
bool dedicatedAllocation = false;
};
} // namespace dawn::native::vulkan::external_memory
#endif // SRC_DAWN_NATIVE_VULKAN_EXTERNAL_MEMORY_MEMORYIMPORTPARAMS_H_

View File

@ -13,46 +13,46 @@
// limitations under the License.
#include "dawn/native/vulkan/external_memory/MemoryService.h"
#include "dawn/native/vulkan/DeviceVk.h"
#include "dawn/native/vulkan/external_memory/MemoryServiceImplementation.h"
namespace dawn::native::vulkan::external_memory {
bool Service::RequiresDedicatedAllocation(const ExternalImageDescriptorVk* descriptor,
Service::~Service() = default;
bool Service::SupportsImportMemory(VkFormat format,
VkImageType type,
VkImageTiling tiling,
VkImageUsageFlags usage,
VkImageCreateFlags flags) {
return mImpl->SupportsImportMemory(format, type, tiling, usage, flags);
}
bool Service::SupportsCreateImage(const ExternalImageDescriptor* descriptor,
VkFormat format,
VkImageUsageFlags usage,
bool* supportsDisjoint) {
return mImpl->SupportsCreateImage(descriptor, format, usage, supportsDisjoint);
}
ResultOrError<MemoryImportParams> Service::GetMemoryImportParams(
const ExternalImageDescriptor* descriptor,
VkImage image) {
switch (descriptor->dedicatedAllocation) {
case NeedsDedicatedAllocation::Yes:
return true;
return mImpl->GetMemoryImportParams(descriptor, image);
}
case NeedsDedicatedAllocation::No:
return false;
uint32_t Service::GetQueueFamilyIndex() {
return mImpl->GetQueueFamilyIndex();
}
case NeedsDedicatedAllocation::Detect:
if (!mDevice->GetDeviceInfo().HasExt(DeviceExt::DedicatedAllocation)) {
return false;
}
ResultOrError<VkDeviceMemory> Service::ImportMemory(ExternalMemoryHandle handle,
const MemoryImportParams& importParams,
VkImage image) {
return mImpl->ImportMemory(handle, importParams, image);
}
VkMemoryDedicatedRequirements dedicatedRequirements;
dedicatedRequirements.sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS;
dedicatedRequirements.pNext = nullptr;
VkMemoryRequirements2 baseRequirements;
baseRequirements.sType = VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2;
baseRequirements.pNext = &dedicatedRequirements;
VkImageMemoryRequirementsInfo2 imageInfo;
imageInfo.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_REQUIREMENTS_INFO_2;
imageInfo.pNext = nullptr;
imageInfo.image = image;
mDevice->fn.GetImageMemoryRequirements2(mDevice->GetVkDevice(), &imageInfo,
&baseRequirements);
// The Vulkan spec requires that prefersDA is set if requiresDA is, so we can just check
// for prefersDA.
return dedicatedRequirements.prefersDedicatedAllocation;
}
DAWN_UNREACHABLE();
ResultOrError<VkImage> Service::CreateImage(const ExternalImageDescriptor* descriptor,
const VkImageCreateInfo& baseCreateInfo) {
return mImpl->CreateImage(descriptor, baseCreateInfo);
}
} // namespace dawn::native::vulkan::external_memory

View File

@ -15,10 +15,11 @@
#ifndef SRC_DAWN_NATIVE_VULKAN_EXTERNAL_MEMORY_MEMORYSERVICE_H_
#define SRC_DAWN_NATIVE_VULKAN_EXTERNAL_MEMORY_MEMORYSERVICE_H_
#include "dawn/common/vulkan_platform.h"
#include <memory>
#include "dawn/native/Error.h"
#include "dawn/native/VulkanBackend.h"
#include "dawn/native/vulkan/ExternalHandle.h"
#include "dawn/native/vulkan/external_memory/MemoryImportParams.h"
namespace dawn::native::vulkan {
class Device;
@ -27,11 +28,7 @@ struct VulkanDeviceInfo;
namespace dawn::native::vulkan::external_memory {
struct MemoryImportParams {
VkDeviceSize allocationSize;
uint32_t memoryTypeIndex;
bool dedicatedAllocation = false;
};
class ServiceImplementation;
class Service {
public:
@ -71,12 +68,7 @@ class Service {
const VkImageCreateInfo& baseCreateInfo);
private:
bool RequiresDedicatedAllocation(const ExternalImageDescriptorVk* descriptor, VkImage image);
Device* mDevice = nullptr;
// True if early checks pass that determine if the service is supported
bool mSupported = false;
std::unique_ptr<ServiceImplementation> mImpl;
};
} // namespace dawn::native::vulkan::external_memory

View File

@ -0,0 +1,60 @@
// Copyright 2023 The Dawn Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "dawn/native/vulkan/external_memory/MemoryServiceImplementation.h"
#include "dawn/native/vulkan/DeviceVk.h"
namespace dawn::native::vulkan::external_memory {
ServiceImplementation::ServiceImplementation(Device* device) : mDevice(device) {}
ServiceImplementation::~ServiceImplementation() = default;
bool ServiceImplementation::RequiresDedicatedAllocation(const ExternalImageDescriptorVk* descriptor,
VkImage image) const {
switch (descriptor->dedicatedAllocation) {
case NeedsDedicatedAllocation::Yes:
return true;
case NeedsDedicatedAllocation::No:
return false;
case NeedsDedicatedAllocation::Detect:
if (!mDevice->GetDeviceInfo().HasExt(DeviceExt::DedicatedAllocation)) {
return false;
}
VkMemoryDedicatedRequirements dedicatedRequirements;
dedicatedRequirements.sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS;
dedicatedRequirements.pNext = nullptr;
VkMemoryRequirements2 baseRequirements;
baseRequirements.sType = VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2;
baseRequirements.pNext = &dedicatedRequirements;
VkImageMemoryRequirementsInfo2 imageInfo;
imageInfo.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_REQUIREMENTS_INFO_2;
imageInfo.pNext = nullptr;
imageInfo.image = image;
mDevice->fn.GetImageMemoryRequirements2(mDevice->GetVkDevice(), &imageInfo,
&baseRequirements);
// The Vulkan spec requires that prefersDA is set if requiresDA is, so we can just check
// for prefersDA.
return dedicatedRequirements.prefersDedicatedAllocation;
}
DAWN_UNREACHABLE();
}
} // namespace dawn::native::vulkan::external_memory

View File

@ -0,0 +1,78 @@
// Copyright 2023 The Dawn Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef SRC_DAWN_NATIVE_VULKAN_EXTERNAL_MEMORY_SERVICEIMPLEMENTATION_H_
#define SRC_DAWN_NATIVE_VULKAN_EXTERNAL_MEMORY_SERVICEIMPLEMENTATION_H_
#include "dawn/common/vulkan_platform.h"
#include "dawn/native/Error.h"
#include "dawn/native/VulkanBackend.h"
#include "dawn/native/vulkan/ExternalHandle.h"
#include "dawn/native/vulkan/external_memory/MemoryImportParams.h"
namespace dawn::native::vulkan {
class Device;
struct VulkanDeviceInfo;
} // namespace dawn::native::vulkan
namespace dawn::native::vulkan::external_memory {
class ServiceImplementation {
public:
explicit ServiceImplementation(Device* device);
virtual ~ServiceImplementation();
// True if the device reports it supports importing external memory.
virtual bool SupportsImportMemory(VkFormat format,
VkImageType type,
VkImageTiling tiling,
VkImageUsageFlags usage,
VkImageCreateFlags flags) = 0;
// True if the device reports it supports creating VkImages from external memory.
virtual bool SupportsCreateImage(const ExternalImageDescriptor* descriptor,
VkFormat format,
VkImageUsageFlags usage,
bool* supportsDisjoint) = 0;
// Returns the parameters required for importing memory
virtual ResultOrError<MemoryImportParams> GetMemoryImportParams(
const ExternalImageDescriptor* descriptor,
VkImage image) = 0;
// Returns the index of the queue memory from this services should be exported with.
virtual uint32_t GetQueueFamilyIndex() = 0;
// Given an external handle pointing to memory, import it into a VkDeviceMemory
virtual ResultOrError<VkDeviceMemory> ImportMemory(ExternalMemoryHandle handle,
const MemoryImportParams& importParams,
VkImage image) = 0;
// Create a VkImage for the given handle type
virtual ResultOrError<VkImage> CreateImage(const ExternalImageDescriptor* descriptor,
const VkImageCreateInfo& baseCreateInfo) = 0;
// True if the device reports it supports this feature
virtual bool Supported() const = 0;
protected:
bool RequiresDedicatedAllocation(const ExternalImageDescriptorVk* descriptor,
VkImage image) const;
Device* mDevice = nullptr;
};
} // namespace dawn::native::vulkan::external_memory
#endif // SRC_DAWN_NATIVE_VULKAN_EXTERNAL_MEMORY_SERVICEIMPLEMENTATION_H_

View File

@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include <memory>
#include "dawn/common/Assert.h"
#include "dawn/native/vulkan/AdapterVk.h"
#include "dawn/native/vulkan/BackendVk.h"
@ -20,24 +22,25 @@
#include "dawn/native/vulkan/UtilsVulkan.h"
#include "dawn/native/vulkan/VulkanError.h"
#include "dawn/native/vulkan/external_memory/MemoryService.h"
#include "dawn/native/vulkan/external_memory/MemoryServiceImplementation.h"
namespace dawn::native::vulkan::external_memory {
Service::Service(Device* device)
: mDevice(device), mSupported(CheckSupport(device->GetDeviceInfo())) {}
class ServiceImplementationAHardwareBuffer : public ServiceImplementation {
public:
explicit ServiceImplementationAHardwareBuffer(Device* device)
: ServiceImplementation(device), mSupported(CheckSupport(device->GetDeviceInfo())) {}
~ServiceImplementationAHardwareBuffer() override = default;
Service::~Service() = default;
// static
bool Service::CheckSupport(const VulkanDeviceInfo& deviceInfo) {
static bool CheckSupport(const VulkanDeviceInfo& deviceInfo) {
return deviceInfo.HasExt(DeviceExt::ExternalMemoryAndroidHardwareBuffer);
}
}
bool Service::SupportsImportMemory(VkFormat format,
bool SupportsImportMemory(VkFormat format,
VkImageType type,
VkImageTiling tiling,
VkImageUsageFlags usage,
VkImageCreateFlags flags) {
VkImageCreateFlags flags) override {
// Early out before we try using extension functions
if (!mSupported) {
return false;
@ -81,21 +84,22 @@ bool Service::SupportsImportMemory(VkFormat format,
}
// TODO(http://crbug.com/dawn/206): Investigate dedicated only images
VkFlags memoryFlags = externalFormatProperties.externalMemoryProperties.externalMemoryFeatures;
VkFlags memoryFlags =
externalFormatProperties.externalMemoryProperties.externalMemoryFeatures;
return (memoryFlags & VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT_KHR) != 0;
}
}
bool Service::SupportsCreateImage(const ExternalImageDescriptor* descriptor,
bool SupportsCreateImage(const ExternalImageDescriptor* descriptor,
VkFormat format,
VkImageUsageFlags usage,
bool* supportsDisjoint) {
bool* supportsDisjoint) override {
*supportsDisjoint = false;
return mSupported;
}
}
ResultOrError<MemoryImportParams> Service::GetMemoryImportParams(
ResultOrError<MemoryImportParams> GetMemoryImportParams(
const ExternalImageDescriptor* descriptor,
VkImage image) {
VkImage image) override {
DAWN_INVALID_IF(descriptor->GetType() != ExternalImageType::AHardwareBuffer,
"ExternalImageDescriptor is not an AHardwareBuffer descriptor.");
@ -110,7 +114,8 @@ ResultOrError<MemoryImportParams> Service::GetMemoryImportParams(
PNextChainBuilder bufferPropertiesChain(&bufferProperties);
VkAndroidHardwareBufferFormatPropertiesANDROID bufferFormatProperties;
bufferPropertiesChain.Add(&bufferFormatProperties,
bufferPropertiesChain.Add(
&bufferFormatProperties,
VK_STRUCTURE_TYPE_ANDROID_HARDWARE_BUFFER_FORMAT_PROPERTIES_ANDROID);
DAWN_TRY(CheckVkSuccess(
@ -123,15 +128,13 @@ ResultOrError<MemoryImportParams> Service::GetMemoryImportParams(
params.memoryTypeIndex = bufferProperties.memoryTypeBits;
params.dedicatedAllocation = RequiresDedicatedAllocation(aHardwareBufferDescriptor, image);
return params;
}
}
uint32_t Service::GetQueueFamilyIndex() {
return VK_QUEUE_FAMILY_FOREIGN_EXT;
}
uint32_t GetQueueFamilyIndex() override { return VK_QUEUE_FAMILY_FOREIGN_EXT; }
ResultOrError<VkDeviceMemory> Service::ImportMemory(ExternalMemoryHandle handle,
ResultOrError<VkDeviceMemory> ImportMemory(ExternalMemoryHandle handle,
const MemoryImportParams& importParams,
VkImage image) {
VkImage image) override {
DAWN_INVALID_IF(handle == nullptr, "Importing memory with an invalid handle.");
VkMemoryRequirements requirements;
@ -168,10 +171,10 @@ ResultOrError<VkDeviceMemory> Service::ImportMemory(ExternalMemoryHandle handle,
nullptr, &*allocatedMemory),
"vkAllocateMemory"));
return allocatedMemory;
}
}
ResultOrError<VkImage> Service::CreateImage(const ExternalImageDescriptor* descriptor,
const VkImageCreateInfo& baseCreateInfo) {
ResultOrError<VkImage> CreateImage(const ExternalImageDescriptor* descriptor,
const VkImageCreateInfo& baseCreateInfo) override {
VkImageCreateInfo createInfo = baseCreateInfo;
createInfo.flags |= VK_IMAGE_CREATE_ALIAS_BIT_KHR;
createInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
@ -192,6 +195,21 @@ ResultOrError<VkImage> Service::CreateImage(const ExternalImageDescriptor* descr
mDevice->fn.CreateImage(mDevice->GetVkDevice(), &createInfo, nullptr, &*image),
"CreateImage"));
return image;
}
bool Supported() const override { return mSupported; }
private:
bool mSupported = false;
};
Service::Service(Device* device) {
mImpl = std::make_unique<ServiceImplementationAHardwareBuffer>(device);
}
// static
bool Service::CheckSupport(const VulkanDeviceInfo& deviceInfo) {
return deviceInfo.HasExt(DeviceExt::ExternalMemoryAndroidHardwareBuffer);
}
} // namespace dawn::native::vulkan::external_memory

View File

@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include <memory>
#include <vector>
#include "dawn/common/Assert.h"
@ -22,6 +23,7 @@
#include "dawn/native/vulkan/UtilsVulkan.h"
#include "dawn/native/vulkan/VulkanError.h"
#include "dawn/native/vulkan/external_memory/MemoryService.h"
#include "dawn/native/vulkan/external_memory/MemoryServiceImplementation.h"
namespace dawn::native::vulkan::external_memory {
@ -115,31 +117,31 @@ bool SupportsDisjoint(const VulkanFunctions& fn,
} // namespace
Service::Service(Device* device)
: mDevice(device), mSupported(CheckSupport(device->GetDeviceInfo())) {}
class ServiceImplementationDmaBuf : public ServiceImplementation {
public:
explicit ServiceImplementationDmaBuf(Device* device)
: ServiceImplementation(device), mSupported(CheckSupport(device->GetDeviceInfo())) {}
~ServiceImplementationDmaBuf() override = default;
Service::~Service() = default;
// static
bool Service::CheckSupport(const VulkanDeviceInfo& deviceInfo) {
static bool CheckSupport(const VulkanDeviceInfo& deviceInfo) {
return deviceInfo.HasExt(DeviceExt::ExternalMemoryFD) &&
deviceInfo.HasExt(DeviceExt::ImageDrmFormatModifier);
}
}
bool Service::SupportsImportMemory(VkFormat format,
bool SupportsImportMemory(VkFormat format,
VkImageType type,
VkImageTiling tiling,
VkImageUsageFlags usage,
VkImageCreateFlags flags) {
VkImageCreateFlags flags) override {
return mSupported && (!IsMultiPlanarVkFormat(format) ||
(format == VK_FORMAT_G8_B8R8_2PLANE_420_UNORM &&
mDevice->GetDeviceInfo().HasExt(DeviceExt::ImageFormatList)));
}
}
bool Service::SupportsCreateImage(const ExternalImageDescriptor* descriptor,
bool SupportsCreateImage(const ExternalImageDescriptor* descriptor,
VkFormat format,
VkImageUsageFlags usage,
bool* supportsDisjoint) {
bool* supportsDisjoint) override {
*supportsDisjoint = false;
// Early out before we try using extension functions
if (!mSupported) {
@ -188,8 +190,8 @@ bool Service::SupportsCreateImage(const ExternalImageDescriptor* descriptor,
VkPhysicalDeviceImageDrmFormatModifierInfoEXT drmModifierInfo = {};
drmModifierInfo.drmFormatModifier = dmaBufDescriptor->drmModifier;
drmModifierInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
imageFormatInfoChain.Add(&drmModifierInfo,
VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_DRM_FORMAT_MODIFIER_INFO_EXT);
imageFormatInfoChain.Add(
&drmModifierInfo, VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_DRM_FORMAT_MODIFIER_INFO_EXT);
// For mutable vkimage of multi-planar format, we also need to make sure the each
// plane's view format can be supported.
@ -222,11 +224,11 @@ bool Service::SupportsCreateImage(const ExternalImageDescriptor* descriptor,
VkExternalMemoryFeatureFlags featureFlags =
externalImageFormatProps.externalMemoryProperties.externalMemoryFeatures;
return featureFlags & VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT;
}
}
ResultOrError<MemoryImportParams> Service::GetMemoryImportParams(
ResultOrError<MemoryImportParams> GetMemoryImportParams(
const ExternalImageDescriptor* descriptor,
VkImage image) {
VkImage image) override {
DAWN_INVALID_IF(descriptor->GetType() != ExternalImageType::DmaBuf,
"ExternalImageDescriptor is not a ExternalImageDescriptorDmaBuf.");
@ -250,22 +252,21 @@ ResultOrError<MemoryImportParams> Service::GetMemoryImportParams(
memoryRequirements.memoryTypeBits &= fdProperties.memoryTypeBits;
int memoryTypeIndex = mDevice->GetResourceMemoryAllocator()->FindBestTypeIndex(
memoryRequirements, MemoryKind::Opaque);
DAWN_INVALID_IF(memoryTypeIndex == -1, "Unable to find an appropriate memory type for import.");
DAWN_INVALID_IF(memoryTypeIndex == -1,
"Unable to find an appropriate memory type for import.");
MemoryImportParams params;
params.allocationSize = memoryRequirements.size;
params.memoryTypeIndex = static_cast<uint32_t>(memoryTypeIndex);
params.dedicatedAllocation = RequiresDedicatedAllocation(dmaBufDescriptor, image);
return params;
}
}
uint32_t Service::GetQueueFamilyIndex() {
return VK_QUEUE_FAMILY_EXTERNAL_KHR;
}
uint32_t GetQueueFamilyIndex() override { return VK_QUEUE_FAMILY_EXTERNAL_KHR; }
ResultOrError<VkDeviceMemory> Service::ImportMemory(ExternalMemoryHandle handle,
ResultOrError<VkDeviceMemory> ImportMemory(ExternalMemoryHandle handle,
const MemoryImportParams& importParams,
VkImage image) {
VkImage image) override {
DAWN_INVALID_IF(handle < 0, "Importing memory with an invalid handle.");
VkMemoryAllocateInfo memoryAllocateInfo = {};
@ -277,7 +278,8 @@ ResultOrError<VkDeviceMemory> Service::ImportMemory(ExternalMemoryHandle handle,
VkImportMemoryFdInfoKHR importMemoryFdInfo;
importMemoryFdInfo.handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
importMemoryFdInfo.fd = handle;
memoryAllocateInfoChain.Add(&importMemoryFdInfo, VK_STRUCTURE_TYPE_IMPORT_MEMORY_FD_INFO_KHR);
memoryAllocateInfoChain.Add(&importMemoryFdInfo,
VK_STRUCTURE_TYPE_IMPORT_MEMORY_FD_INFO_KHR);
VkMemoryDedicatedAllocateInfo memoryDedicatedAllocateInfo;
if (importParams.dedicatedAllocation) {
@ -288,14 +290,15 @@ ResultOrError<VkDeviceMemory> Service::ImportMemory(ExternalMemoryHandle handle,
}
VkDeviceMemory allocatedMemory = VK_NULL_HANDLE;
DAWN_TRY(CheckVkSuccess(mDevice->fn.AllocateMemory(mDevice->GetVkDevice(), &memoryAllocateInfo,
DAWN_TRY(
CheckVkSuccess(mDevice->fn.AllocateMemory(mDevice->GetVkDevice(), &memoryAllocateInfo,
nullptr, &*allocatedMemory),
"vkAllocateMemory"));
return allocatedMemory;
}
}
ResultOrError<VkImage> Service::CreateImage(const ExternalImageDescriptor* descriptor,
const VkImageCreateInfo& baseCreateInfo) {
ResultOrError<VkImage> CreateImage(const ExternalImageDescriptor* descriptor,
const VkImageCreateInfo& baseCreateInfo) override {
DAWN_INVALID_IF(descriptor->GetType() != ExternalImageType::DmaBuf,
"ExternalImageDescriptor is not a dma-buf descriptor.");
@ -347,6 +350,22 @@ ResultOrError<VkImage> Service::CreateImage(const ExternalImageDescriptor* descr
DAWN_TRY(CheckVkSuccess(mDevice->fn.CreateImage(device, &createInfo, nullptr, &*image),
"CreateImage"));
return image;
}
bool Supported() const override { return mSupported; }
private:
bool mSupported = false;
};
Service::Service(Device* device) {
mImpl = std::make_unique<ServiceImplementationDmaBuf>(device);
}
// static
bool Service::CheckSupport(const VulkanDeviceInfo& deviceInfo) {
return deviceInfo.HasExt(DeviceExt::ExternalMemoryFD) &&
deviceInfo.HasExt(DeviceExt::ImageDrmFormatModifier);
}
} // namespace dawn::native::vulkan::external_memory

View File

@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include <memory>
#include "dawn/common/Assert.h"
#include "dawn/native/vulkan/AdapterVk.h"
#include "dawn/native/vulkan/BackendVk.h"
@ -20,24 +22,25 @@
#include "dawn/native/vulkan/UtilsVulkan.h"
#include "dawn/native/vulkan/VulkanError.h"
#include "dawn/native/vulkan/external_memory/MemoryService.h"
#include "dawn/native/vulkan/external_memory/MemoryServiceImplementation.h"
namespace dawn::native::vulkan::external_memory {
Service::Service(Device* device)
: mDevice(device), mSupported(CheckSupport(device->GetDeviceInfo())) {}
class ServiceImplementationOpaqueFD : public ServiceImplementation {
public:
explicit ServiceImplementationOpaqueFD(Device* device)
: ServiceImplementation(device), mSupported(CheckSupport(device->GetDeviceInfo())) {}
~ServiceImplementationOpaqueFD() override = default;
Service::~Service() = default;
// static
bool Service::CheckSupport(const VulkanDeviceInfo& deviceInfo) {
static bool CheckSupport(const VulkanDeviceInfo& deviceInfo) {
return deviceInfo.HasExt(DeviceExt::ExternalMemoryFD);
}
}
bool Service::SupportsImportMemory(VkFormat format,
bool SupportsImportMemory(VkFormat format,
VkImageType type,
VkImageTiling tiling,
VkImageUsageFlags usage,
VkImageCreateFlags flags) {
VkImageCreateFlags flags) override {
// Early out before we try using extension functions
if (!mSupported) {
return false;
@ -73,21 +76,22 @@ bool Service::SupportsImportMemory(VkFormat format,
return false;
}
VkFlags memoryFlags = externalFormatProperties.externalMemoryProperties.externalMemoryFeatures;
VkFlags memoryFlags =
externalFormatProperties.externalMemoryProperties.externalMemoryFeatures;
return (memoryFlags & VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT_KHR) != 0;
}
}
bool Service::SupportsCreateImage(const ExternalImageDescriptor* descriptor,
bool SupportsCreateImage(const ExternalImageDescriptor* descriptor,
VkFormat format,
VkImageUsageFlags usage,
bool* supportsDisjoint) {
bool* supportsDisjoint) override {
*supportsDisjoint = false;
return mSupported;
}
}
ResultOrError<MemoryImportParams> Service::GetMemoryImportParams(
ResultOrError<MemoryImportParams> GetMemoryImportParams(
const ExternalImageDescriptor* descriptor,
VkImage image) {
VkImage image) override {
DAWN_INVALID_IF(descriptor->GetType() != ExternalImageType::OpaqueFD,
"ExternalImageDescriptor is not an OpaqueFD descriptor.");
@ -99,15 +103,13 @@ ResultOrError<MemoryImportParams> Service::GetMemoryImportParams(
params.memoryTypeIndex = opaqueFDDescriptor->memoryTypeIndex;
params.dedicatedAllocation = RequiresDedicatedAllocation(opaqueFDDescriptor, image);
return params;
}
}
uint32_t Service::GetQueueFamilyIndex() {
return VK_QUEUE_FAMILY_EXTERNAL_KHR;
}
uint32_t GetQueueFamilyIndex() override { return VK_QUEUE_FAMILY_EXTERNAL_KHR; }
ResultOrError<VkDeviceMemory> Service::ImportMemory(ExternalMemoryHandle handle,
ResultOrError<VkDeviceMemory> ImportMemory(ExternalMemoryHandle handle,
const MemoryImportParams& importParams,
VkImage image) {
VkImage image) override {
DAWN_INVALID_IF(handle < 0, "Importing memory with an invalid handle.");
VkMemoryRequirements requirements;
@ -141,10 +143,10 @@ ResultOrError<VkDeviceMemory> Service::ImportMemory(ExternalMemoryHandle handle,
nullptr, &*allocatedMemory),
"vkAllocateMemory"));
return allocatedMemory;
}
}
ResultOrError<VkImage> Service::CreateImage(const ExternalImageDescriptor* descriptor,
const VkImageCreateInfo& baseCreateInfo) {
ResultOrError<VkImage> CreateImage(const ExternalImageDescriptor* descriptor,
const VkImageCreateInfo& baseCreateInfo) override {
VkImageCreateInfo createInfo = baseCreateInfo;
createInfo.flags |= VK_IMAGE_CREATE_ALIAS_BIT_KHR;
createInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
@ -166,6 +168,21 @@ ResultOrError<VkImage> Service::CreateImage(const ExternalImageDescriptor* descr
mDevice->fn.CreateImage(mDevice->GetVkDevice(), &createInfo, nullptr, &*image),
"CreateImage"));
return image;
}
bool Supported() const override { return mSupported; }
private:
bool mSupported = false;
};
Service::Service(Device* device) {
mImpl = std::make_unique<ServiceImplementationOpaqueFD>(device);
}
// static
bool Service::CheckSupport(const VulkanDeviceInfo& deviceInfo) {
return deviceInfo.HasExt(DeviceExt::ExternalMemoryFD);
}
} // namespace dawn::native::vulkan::external_memory

View File

@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include <memory>
#include "dawn/common/Assert.h"
#include "dawn/native/vulkan/AdapterVk.h"
#include "dawn/native/vulkan/BackendVk.h"
@ -20,24 +22,25 @@
#include "dawn/native/vulkan/UtilsVulkan.h"
#include "dawn/native/vulkan/VulkanError.h"
#include "dawn/native/vulkan/external_memory/MemoryService.h"
#include "dawn/native/vulkan/external_memory/MemoryServiceImplementation.h"
namespace dawn::native::vulkan::external_memory {
Service::Service(Device* device)
: mDevice(device), mSupported(CheckSupport(device->GetDeviceInfo())) {}
class ServiceImplementationZicronHandle : public ServiceImplementation {
public:
explicit ServiceImplementationZicronHandle(Device* device)
: ServiceImplementation(device), mSupported(CheckSupport(device->GetDeviceInfo())) {}
~ServiceImplementationZicronHandle() override = default;
Service::~Service() = default;
// static
bool Service::CheckSupport(const VulkanDeviceInfo& deviceInfo) {
static bool CheckSupport(const VulkanDeviceInfo& deviceInfo) {
return deviceInfo.HasExt(DeviceExt::ExternalMemoryZirconHandle);
}
}
bool Service::SupportsImportMemory(VkFormat format,
bool SupportsImportMemory(VkFormat format,
VkImageType type,
VkImageTiling tiling,
VkImageUsageFlags usage,
VkImageCreateFlags flags) {
VkImageCreateFlags flags) override {
// Early out before we try using extension functions
if (!mSupported) {
return false;
@ -74,21 +77,22 @@ bool Service::SupportsImportMemory(VkFormat format,
}
// TODO(http://crbug.com/dawn/206): Investigate dedicated only images
VkFlags memoryFlags = externalFormatProperties.externalMemoryProperties.externalMemoryFeatures;
VkFlags memoryFlags =
externalFormatProperties.externalMemoryProperties.externalMemoryFeatures;
return (memoryFlags & VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT_KHR) != 0;
}
}
bool Service::SupportsCreateImage(const ExternalImageDescriptor* descriptor,
bool SupportsCreateImage(const ExternalImageDescriptor* descriptor,
VkFormat format,
VkImageUsageFlags usage,
bool* supportsDisjoint) {
bool* supportsDisjoint) override {
*supportsDisjoint = false;
return mSupported;
}
}
ResultOrError<MemoryImportParams> Service::GetMemoryImportParams(
ResultOrError<MemoryImportParams> GetMemoryImportParams(
const ExternalImageDescriptor* descriptor,
VkImage image) {
VkImage image) override {
DAWN_INVALID_IF(descriptor->type != ExternalImageType::OpaqueFD,
"ExternalImageDescriptor is not an OpaqueFD descriptor.");
@ -100,20 +104,19 @@ ResultOrError<MemoryImportParams> Service::GetMemoryImportParams(
params.memoryTypeIndex = opaqueFDDescriptor->memoryTypeIndex;
params.dedicatedAllocation = RequiresDedicatedAllocation(opaqueFDDescriptor, image);
return params;
}
}
uint32_t Service::GetQueueFamilyIndex() {
return VK_QUEUE_FAMILY_EXTERNAL_KHR;
}
uint32_t GetQueueFamilyIndex() override { return VK_QUEUE_FAMILY_EXTERNAL_KHR; }
ResultOrError<VkDeviceMemory> Service::ImportMemory(ExternalMemoryHandle handle,
ResultOrError<VkDeviceMemory> ImportMemory(ExternalMemoryHandle handle,
const MemoryImportParams& importParams,
VkImage image) {
VkImage image) override {
DAWN_INVALID_IF(handle == ZX_HANDLE_INVALID, "Importing memory with an invalid handle.");
VkMemoryRequirements requirements;
mDevice->fn.GetImageMemoryRequirements(mDevice->GetVkDevice(), image, &requirements);
DAWN_INVALID_IF(requirements.size > importParams.allocationSize,
DAWN_INVALID_IF(
requirements.size > importParams.allocationSize,
"Requested allocation size (%u) is smaller than the required image size (%u).",
importParams.allocationSize, requirements.size);
@ -143,10 +146,10 @@ ResultOrError<VkDeviceMemory> Service::ImportMemory(ExternalMemoryHandle handle,
nullptr, &*allocatedMemory),
"vkAllocateMemory"));
return allocatedMemory;
}
}
ResultOrError<VkImage> Service::CreateImage(const ExternalImageDescriptor* descriptor,
const VkImageCreateInfo& baseCreateInfo) {
ResultOrError<VkImage> CreateImage(const ExternalImageDescriptor* descriptor,
const VkImageCreateInfo& baseCreateInfo) override {
VkImageCreateInfo createInfo = baseCreateInfo;
createInfo.flags |= VK_IMAGE_CREATE_ALIAS_BIT_KHR;
createInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
@ -169,6 +172,21 @@ ResultOrError<VkImage> Service::CreateImage(const ExternalImageDescriptor* descr
mDevice->fn.CreateImage(mDevice->GetVkDevice(), &createInfo, nullptr, &*image),
"CreateImage"));
return image;
}
bool Supported() const override { return mSupported; }
private:
bool mSupported = false;
};
Service::Service(Device* device) {
mImpl = std::make_unique<ServiceImplementationZicronHandle>(device);
}
// static
bool Service::CheckSupport(const VulkanDeviceInfo& deviceInfo) {
return deviceInfo.HasExt(DeviceExt::ExternalMemoryZirconHandle);
}
} // namespace dawn::native::vulkan::external_memory

View File

@ -12,58 +12,70 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include <memory>
#include "dawn/native/vulkan/DeviceVk.h"
#include "dawn/native/vulkan/external_memory/MemoryService.h"
#include "dawn/native/vulkan/external_memory/MemoryServiceImplementation.h"
namespace dawn::native::vulkan::external_memory {
Service::Service(Device* device) : mDevice(device) {
DAWN_UNUSED(mDevice);
DAWN_UNUSED(mSupported);
}
class ServiceImplementationNull : public ServiceImplementation {
public:
explicit ServiceImplementationNull(Device* device)
: ServiceImplementation(device), mSupported(CheckSupport(device->GetDeviceInfo())) {}
~ServiceImplementationNull() override = default;
Service::~Service() = default;
static bool CheckSupport(const VulkanDeviceInfo& deviceInfo) { return false; }
bool SupportsImportMemory(VkFormat format,
VkImageType type,
VkImageTiling tiling,
VkImageUsageFlags usage,
VkImageCreateFlags flags) override {
return false;
}
bool SupportsCreateImage(const ExternalImageDescriptor* descriptor,
VkFormat format,
VkImageUsageFlags usage,
bool* supportsDisjoint) override {
*supportsDisjoint = false;
return false;
}
ResultOrError<MemoryImportParams> GetMemoryImportParams(
const ExternalImageDescriptor* descriptor,
VkImage image) override {
return DAWN_UNIMPLEMENTED_ERROR("Using null memory service to interop inside Vulkan");
}
uint32_t GetQueueFamilyIndex() override { return VK_QUEUE_FAMILY_EXTERNAL_KHR; }
ResultOrError<VkDeviceMemory> ImportMemory(ExternalMemoryHandle handle,
const MemoryImportParams& importParams,
VkImage image) override {
return DAWN_UNIMPLEMENTED_ERROR("Using null memory service to interop inside Vulkan");
}
ResultOrError<VkImage> CreateImage(const ExternalImageDescriptor* descriptor,
const VkImageCreateInfo& baseCreateInfo) override {
return DAWN_UNIMPLEMENTED_ERROR("Using null memory service to interop inside Vulkan");
}
bool Supported() const override { return false; }
private:
bool mSupported = false;
};
Service::Service(Device* device) {
mImpl = std::make_unique<ServiceImplementationNull>(device);
}
// static
bool Service::CheckSupport(const VulkanDeviceInfo& deviceInfo) {
return false;
}
bool Service::SupportsImportMemory(VkFormat format,
VkImageType type,
VkImageTiling tiling,
VkImageUsageFlags usage,
VkImageCreateFlags flags) {
return false;
}
bool Service::SupportsCreateImage(const ExternalImageDescriptor* descriptor,
VkFormat format,
VkImageUsageFlags usage,
bool* supportsDisjoint) {
*supportsDisjoint = false;
return false;
}
ResultOrError<MemoryImportParams> Service::GetMemoryImportParams(
const ExternalImageDescriptor* descriptor,
VkImage image) {
return DAWN_UNIMPLEMENTED_ERROR("Using null memory service to interop inside Vulkan");
}
uint32_t Service::GetQueueFamilyIndex() {
return VK_QUEUE_FAMILY_EXTERNAL_KHR;
}
ResultOrError<VkDeviceMemory> Service::ImportMemory(ExternalMemoryHandle handle,
const MemoryImportParams& importParams,
VkImage image) {
return DAWN_UNIMPLEMENTED_ERROR("Using null memory service to interop inside Vulkan");
}
ResultOrError<VkImage> Service::CreateImage(const ExternalImageDescriptor* descriptor,
const VkImageCreateInfo& baseCreateInfo) {
return DAWN_UNIMPLEMENTED_ERROR("Using null memory service to interop inside Vulkan");
}
} // namespace dawn::native::vulkan::external_memory