Move ExternalMemoryHost to its own file
As a follow-up to:
https://swiftshader-review.googlesource.com/c/SwiftShader/+/56930
where this suggestion was made:
https://swiftshader-review.googlesource.com/c/SwiftShader/+/56930/5..8/src/Vulkan/VkDeviceMemory.cpp#b24
this CL moves the ExternalMemoryHost class into its own header and
source files.
Bug: b/199306480
Change-Id: I1cd8cd67163bec0a0823e26fa0f2004697d33a1b
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/58009
Presubmit-Ready: Alexis Hétu <sugoi@google.com>
Reviewed-by: Nicolas Capens <nicolascapens@google.com>
Tested-by: Alexis Hétu <sugoi@google.com>
Commit-Queue: Alexis Hétu <sugoi@google.com>
diff --git a/src/Vulkan/BUILD.gn b/src/Vulkan/BUILD.gn
index 1612e1a..e20282c 100644
--- a/src/Vulkan/BUILD.gn
+++ b/src/Vulkan/BUILD.gn
@@ -72,6 +72,7 @@
"VkDestroy.hpp",
"VkDevice.hpp",
"VkDeviceMemory.hpp",
+ "VkDeviceMemoryExternalHost.hpp",
"VkEvent.hpp",
"VkFence.hpp",
"VkFormat.hpp",
@@ -124,6 +125,7 @@
"VkDescriptorUpdateTemplate.cpp",
"VkDevice.cpp",
"VkDeviceMemory.cpp",
+ "VkDeviceMemoryExternalHost.cpp",
"VkFormat.cpp",
"VkFramebuffer.cpp",
"VkGetProcAddress.cpp",
diff --git a/src/Vulkan/CMakeLists.txt b/src/Vulkan/CMakeLists.txt
index 6c9e696..fd21cd1 100644
--- a/src/Vulkan/CMakeLists.txt
+++ b/src/Vulkan/CMakeLists.txt
@@ -46,6 +46,8 @@
VkDevice.hpp
VkDeviceMemory.cpp
VkDeviceMemory.hpp
+ VkDeviceMemoryExternalHost.cpp
+ VkDeviceMemoryExternalHost.hpp
VkDeviceMemoryExternalLinux.hpp
VkEvent.hpp
VkFence.hpp
diff --git a/src/Vulkan/VkDeviceMemory.cpp b/src/Vulkan/VkDeviceMemory.cpp
index aa6d7e2..bb0eaa8 100644
--- a/src/Vulkan/VkDeviceMemory.cpp
+++ b/src/Vulkan/VkDeviceMemory.cpp
@@ -13,6 +13,7 @@
// limitations under the License.
#include "VkDeviceMemory.hpp"
+#include "VkDeviceMemoryExternalHost.hpp"
#include "VkBuffer.hpp"
#include "VkConfig.hpp"
@@ -21,68 +22,6 @@
#include "VkMemory.hpp"
#include "VkStringify.hpp"
-// Host-allocated memory and host-mapped foreign memory
-class ExternalMemoryHost : public vk::DeviceMemory, public vk::ObjectBase<ExternalMemoryHost, VkDeviceMemory>
-{
-public:
- struct AllocateInfo
- {
- bool supported = false;
- void *hostPointer = nullptr;
-
- AllocateInfo() = default;
-
- AllocateInfo(const vk::DeviceMemory::ExtendedAllocationInfo &extendedAllocationInfo)
- {
- if(extendedAllocationInfo.importMemoryHostPointerInfo)
- {
- if((extendedAllocationInfo.importMemoryHostPointerInfo->handleType != VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT) &&
- (extendedAllocationInfo.importMemoryHostPointerInfo->handleType != VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_MAPPED_FOREIGN_MEMORY_BIT_EXT))
- {
- UNSUPPORTED("extendedAllocationInfo.importMemoryHostPointerInfo->handleType, %d",
- int(extendedAllocationInfo.importMemoryHostPointerInfo->handleType));
- }
- hostPointer = extendedAllocationInfo.importMemoryHostPointerInfo->pHostPointer;
- supported = true;
- }
- }
- };
-
- static const VkExternalMemoryHandleTypeFlagBits typeFlagBit = (VkExternalMemoryHandleTypeFlagBits)(VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT | VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_MAPPED_FOREIGN_MEMORY_BIT_EXT);
-
- static bool SupportsAllocateInfo(const vk::DeviceMemory::ExtendedAllocationInfo &extendedAllocationInfo)
- {
- AllocateInfo info(extendedAllocationInfo);
- return info.supported;
- }
-
- explicit ExternalMemoryHost(const VkMemoryAllocateInfo *pCreateInfo, void *mem, const DeviceMemory::ExtendedAllocationInfo &extendedAllocationInfo, vk::Device *pDevice)
- : vk::DeviceMemory(pCreateInfo, pDevice)
- , allocateInfo(extendedAllocationInfo)
- {}
-
- VkResult allocateBuffer() override
- {
- if(allocateInfo.supported)
- {
- buffer = allocateInfo.hostPointer;
- return VK_SUCCESS;
- }
- return VK_ERROR_INVALID_EXTERNAL_HANDLE;
- }
-
- void freeBuffer() override
- {}
-
- VkExternalMemoryHandleTypeFlagBits getFlagBit() const override
- {
- return typeFlagBit;
- }
-
-private:
- AllocateInfo allocateInfo;
-};
-
#if SWIFTSHADER_EXTERNAL_MEMORY_OPAQUE_FD
// Helper struct which reads the parsed allocation info and
diff --git a/src/Vulkan/VkDeviceMemoryExternalHost.cpp b/src/Vulkan/VkDeviceMemoryExternalHost.cpp
new file mode 100644
index 0000000..13022ac
--- /dev/null
+++ b/src/Vulkan/VkDeviceMemoryExternalHost.cpp
@@ -0,0 +1,59 @@
+// Copyright 2021 The SwiftShader Authors. All Rights Reserved.
+//
+// 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 "VkDeviceMemoryExternalHost.hpp"
+
+ExternalMemoryHost::AllocateInfo::AllocateInfo(const vk::DeviceMemory::ExtendedAllocationInfo &extendedAllocationInfo)
+{
+ if(extendedAllocationInfo.importMemoryHostPointerInfo)
+ {
+ if((extendedAllocationInfo.importMemoryHostPointerInfo->handleType != VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT) &&
+ (extendedAllocationInfo.importMemoryHostPointerInfo->handleType != VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_MAPPED_FOREIGN_MEMORY_BIT_EXT))
+ {
+ UNSUPPORTED("extendedAllocationInfo.importMemoryHostPointerInfo->handleType, %d",
+ int(extendedAllocationInfo.importMemoryHostPointerInfo->handleType));
+ }
+ hostPointer = extendedAllocationInfo.importMemoryHostPointerInfo->pHostPointer;
+ supported = true;
+ }
+}
+
+bool ExternalMemoryHost::SupportsAllocateInfo(const vk::DeviceMemory::ExtendedAllocationInfo &extendedAllocationInfo)
+{
+ AllocateInfo info(extendedAllocationInfo);
+ return info.supported;
+}
+
+ExternalMemoryHost::ExternalMemoryHost(const VkMemoryAllocateInfo *pCreateInfo, void *mem, const DeviceMemory::ExtendedAllocationInfo &extendedAllocationInfo, vk::Device *pDevice)
+ : vk::DeviceMemory(pCreateInfo, pDevice)
+ , allocateInfo(extendedAllocationInfo)
+{}
+
+VkResult ExternalMemoryHost::allocateBuffer()
+{
+ if(allocateInfo.supported)
+ {
+ buffer = allocateInfo.hostPointer;
+ return VK_SUCCESS;
+ }
+ return VK_ERROR_INVALID_EXTERNAL_HANDLE;
+}
+
+void ExternalMemoryHost::freeBuffer()
+{}
+
+VkExternalMemoryHandleTypeFlagBits ExternalMemoryHost::getFlagBit() const
+{
+ return typeFlagBit;
+}
\ No newline at end of file
diff --git a/src/Vulkan/VkDeviceMemoryExternalHost.hpp b/src/Vulkan/VkDeviceMemoryExternalHost.hpp
new file mode 100644
index 0000000..ce13be3
--- /dev/null
+++ b/src/Vulkan/VkDeviceMemoryExternalHost.hpp
@@ -0,0 +1,48 @@
+// Copyright 2021 The SwiftShader Authors. All Rights Reserved.
+//
+// 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 VK_DEVICE_MEMORY_EXTERNAL_HOST_HPP_
+#define VK_DEVICE_MEMORY_EXTERNAL_HOST_HPP_
+
+#include "VkDeviceMemory.hpp"
+
+// Host-allocated memory and host-mapped foreign memory
+class ExternalMemoryHost : public vk::DeviceMemory, public vk::ObjectBase<ExternalMemoryHost, VkDeviceMemory>
+{
+public:
+ struct AllocateInfo
+ {
+ bool supported = false;
+ void *hostPointer = nullptr;
+
+ AllocateInfo() = default;
+
+ AllocateInfo(const vk::DeviceMemory::ExtendedAllocationInfo &extendedAllocationInfo);
+ };
+
+ static const VkExternalMemoryHandleTypeFlagBits typeFlagBit = (VkExternalMemoryHandleTypeFlagBits)(VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT | VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_MAPPED_FOREIGN_MEMORY_BIT_EXT);
+
+ static bool SupportsAllocateInfo(const vk::DeviceMemory::ExtendedAllocationInfo &extendedAllocationInfo);
+
+ explicit ExternalMemoryHost(const VkMemoryAllocateInfo *pCreateInfo, void *mem, const DeviceMemory::ExtendedAllocationInfo &extendedAllocationInfo, vk::Device *pDevice);
+
+ VkResult allocateBuffer() override;
+ void freeBuffer() override;
+ VkExternalMemoryHandleTypeFlagBits getFlagBit() const override;
+
+private:
+ AllocateInfo allocateInfo;
+};
+
+#endif // VK_DEVICE_MEMORY_EXTERNAL_HOST_HPP_