mirror of
https://gitlab.freedesktop.org/mesa/vulkan-wsi-layer.git
synced 2025-12-20 04:30:11 +01:00
Remove usages of generic allocator in the layer
Remove all the leftover instances in layer that still use the generic allocator rather than the VkAllocationCallbacks Add additional memory utility that allows to release memory allocated by custom allocators using RAII Change-Id: I43594ddd3c506048ca0f02e31b64597a7abc481b Signed-off-by: Normunds Rieksts <normunds.rieksts@arm.com>
This commit is contained in:
parent
b64498c021
commit
d63261cce5
6 changed files with 51 additions and 26 deletions
|
|
@ -98,8 +98,8 @@ VkResult instance_private_data::associate(VkInstance instance, instance_dispatch
|
|||
PFN_vkSetInstanceLoaderData set_loader_data,
|
||||
util::wsi_platform_set enabled_layer_platforms, const util::allocator &allocator)
|
||||
{
|
||||
auto *instance_data =
|
||||
allocator.create<instance_private_data>(1, table, set_loader_data, enabled_layer_platforms, allocator);
|
||||
auto instance_data =
|
||||
allocator.make_unique<instance_private_data>(table, set_loader_data, enabled_layer_platforms, allocator);
|
||||
|
||||
if (instance_data == nullptr)
|
||||
{
|
||||
|
|
@ -120,9 +120,10 @@ VkResult instance_private_data::associate(VkInstance instance, instance_dispatch
|
|||
g_instance_data.erase(it);
|
||||
}
|
||||
|
||||
auto result = g_instance_data.try_insert(std::make_pair(key, instance_data));
|
||||
auto result = g_instance_data.try_insert(std::make_pair(key, instance_data.get()));
|
||||
if (result.has_value())
|
||||
{
|
||||
instance_data.release();
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
else
|
||||
|
|
@ -130,7 +131,6 @@ VkResult instance_private_data::associate(VkInstance instance, instance_dispatch
|
|||
WSI_LOG_WARNING("Failed to insert instance_private_data for instance (%p) as host is out of memory",
|
||||
reinterpret_cast<void *>(instance));
|
||||
|
||||
destroy(instance_data);
|
||||
return VK_ERROR_OUT_OF_HOST_MEMORY;
|
||||
}
|
||||
}
|
||||
|
|
@ -231,8 +231,8 @@ VkResult device_private_data::associate(VkDevice dev, instance_private_data &ins
|
|||
const device_dispatch_table &table, PFN_vkSetDeviceLoaderData set_loader_data,
|
||||
const util::allocator &allocator)
|
||||
{
|
||||
auto *device_data =
|
||||
allocator.create<device_private_data>(1, inst_data, phys_dev, dev, table, set_loader_data, allocator);
|
||||
auto device_data =
|
||||
allocator.make_unique<device_private_data>(inst_data, phys_dev, dev, table, set_loader_data, allocator);
|
||||
|
||||
if (device_data == nullptr)
|
||||
{
|
||||
|
|
@ -252,9 +252,10 @@ VkResult device_private_data::associate(VkDevice dev, instance_private_data &ins
|
|||
g_device_data.erase(it);
|
||||
}
|
||||
|
||||
auto result = g_device_data.try_insert(std::make_pair(key, device_data));
|
||||
auto result = g_device_data.try_insert(std::make_pair(key, device_data.get()));
|
||||
if (result.has_value())
|
||||
{
|
||||
device_data.release();
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
else
|
||||
|
|
@ -262,7 +263,6 @@ VkResult device_private_data::associate(VkDevice dev, instance_private_data &ins
|
|||
WSI_LOG_WARNING("Failed to insert device_private_data for device (%p) as host is out of memory",
|
||||
reinterpret_cast<void *>(dev));
|
||||
|
||||
destroy(device_data);
|
||||
return VK_ERROR_OUT_OF_HOST_MEMORY;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,6 +26,7 @@
|
|||
#include <vector>
|
||||
#include <string>
|
||||
#include <cassert>
|
||||
#include <memory>
|
||||
|
||||
#include <vulkan/vulkan.h>
|
||||
|
||||
|
|
@ -34,6 +35,12 @@
|
|||
namespace util
|
||||
{
|
||||
|
||||
template <typename T>
|
||||
class deleter;
|
||||
|
||||
template <typename T>
|
||||
using unique_ptr = std::unique_ptr<T, deleter<T>>;
|
||||
|
||||
/**
|
||||
* @brief Minimalistic wrapper of VkAllocationCallbacks.
|
||||
*/
|
||||
|
|
@ -89,6 +96,9 @@ public:
|
|||
template <typename T>
|
||||
void destroy(size_t num_objects, T *obj) const noexcept;
|
||||
|
||||
template <typename T, typename... Args>
|
||||
util::unique_ptr<T> make_unique(Args &&...args) const noexcept;
|
||||
|
||||
VkAllocationCallbacks m_callbacks;
|
||||
VkSystemAllocationScope m_scope;
|
||||
};
|
||||
|
|
@ -223,9 +233,27 @@ void allocator::destroy(size_t num_objects, T *objects) const noexcept
|
|||
}
|
||||
|
||||
template <typename T>
|
||||
void destroy_custom(T *obj)
|
||||
class deleter
|
||||
{
|
||||
T::destroy(obj);
|
||||
public:
|
||||
deleter(allocator allocator)
|
||||
: m_allocator(std::move(allocator))
|
||||
{}
|
||||
|
||||
void operator()(T *object)
|
||||
{
|
||||
m_allocator.destroy<T>(1, object);
|
||||
}
|
||||
|
||||
private:
|
||||
allocator m_allocator;
|
||||
};
|
||||
|
||||
template <typename T, typename... Args>
|
||||
util::unique_ptr<T> allocator::make_unique(Args &&...args) const noexcept
|
||||
{
|
||||
T *object = create<T>(1, std::forward<Args>(args)...);
|
||||
return util::unique_ptr<T>(object, *this);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -63,10 +63,10 @@ public:
|
|||
/**
|
||||
* @brief Return the device extensions that this surface_properties implementation needs.
|
||||
*/
|
||||
virtual const util::extension_list &get_required_device_extensions()
|
||||
virtual VkResult get_required_device_extensions(util::extension_list &extension_list)
|
||||
{
|
||||
static const util::extension_list empty{util::allocator::get_generic()};
|
||||
return empty;
|
||||
/* Requires no additional extensions */
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -269,18 +269,9 @@ static const char *required_device_extensions[] = {
|
|||
VK_KHR_EXTERNAL_MEMORY_EXTENSION_NAME,
|
||||
};
|
||||
|
||||
static std::unique_ptr<util::extension_list> populate_device_extensions()
|
||||
VkResult surface_properties::get_required_device_extensions(util::extension_list &extension_list)
|
||||
{
|
||||
std::unique_ptr<util::extension_list> ret(new util::extension_list(util::allocator::get_generic()));
|
||||
ret->add(required_device_extensions, NELEMS(required_device_extensions));
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
const util::extension_list &surface_properties::get_required_device_extensions()
|
||||
{
|
||||
static std::unique_ptr<util::extension_list> device_extensions = populate_device_extensions();
|
||||
return *device_extensions;
|
||||
return extension_list.add(required_device_extensions, NELEMS(required_device_extensions));
|
||||
}
|
||||
|
||||
/* TODO: Check for zwp_linux_dmabuf_v1 protocol in display */
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@ public:
|
|||
VkResult get_surface_present_modes(VkPhysicalDevice physical_device, VkSurfaceKHR surface,
|
||||
uint32_t *pPresentModeCount, VkPresentModeKHR *pPresentModes) override;
|
||||
|
||||
const util::extension_list &get_required_device_extensions() override;
|
||||
VkResult get_required_device_extensions(util::extension_list &extension_list) override;
|
||||
|
||||
PFN_vkVoidFunction get_proc_addr(const char *name) override;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -162,8 +162,14 @@ VkResult add_extensions_required_by_layer(VkPhysicalDevice phys_dev, const util:
|
|||
continue;
|
||||
}
|
||||
|
||||
util::extension_list extensions_required_by_layer{allocator};
|
||||
surface_properties *props = get_surface_properties(wsi_ext.platform);
|
||||
const auto &extensions_required_by_layer = props->get_required_device_extensions();
|
||||
res = props->get_required_device_extensions(extensions_required_by_layer);
|
||||
if (res != VK_SUCCESS)
|
||||
{
|
||||
return res;
|
||||
}
|
||||
|
||||
bool supported = device_extensions.contains(extensions_required_by_layer);
|
||||
if (!supported)
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue