if(BUILD_WSI_WAYLAND)
add_library(wayland_wsi STATIC
wsi/wayland/surface_properties.cpp
- wsi/wayland/swapchain_wl_helpers.cpp
+ wsi/wayland/wl_helpers.cpp
wsi/wayland/swapchain.cpp)
pkg_check_modules(WAYLAND_CLIENT REQUIRED wayland-client)
* SOFTWARE.
*/
-extern "C" {
+#define VK_USE_PLATFORM_WAYLAND_KHR 1
+
#include <wayland-client.h>
#include <linux-dmabuf-unstable-v1-client-protocol.h>
-}
#include <cassert>
#include <cstdlib>
#include <algorithm>
#include <array>
-#include <string.h>
+#include <cstring>
#include "surface_properties.hpp"
#include "layer/private_data.hpp"
+#include "wl_helpers.hpp"
+#include "wl_object_owner.hpp"
+#include "util/drm/drm_utils.hpp"
#define NELEMS(x) (sizeof(x) / sizeof(x[0]))
namespace wayland
{
+struct vk_format_hasher
+{
+ size_t operator()(const VkFormat format) const
+ {
+ return std::hash<uint64_t>()(static_cast<uint64_t>(format));
+ }
+};
+
+using vk_format_set = std::unordered_set<VkFormat, vk_format_hasher>;
+
surface_properties &surface_properties::get_instance()
{
static surface_properties instance;
return VK_SUCCESS;
}
+static void get_vk_supported_formats(const util::vector<drm_format_pair> &drm_supported_formats,
+ vk_format_set &vk_supported_formats)
+{
+ for (const auto &drm_format : drm_supported_formats)
+ {
+ const VkFormat vk_format = util::drm::drm_to_vk_format(drm_format.fourcc);
+ if (vk_format != VK_FORMAT_UNDEFINED)
+ {
+ const VkFormat srgb_vk_format = util::drm::drm_to_vk_srgb_format(drm_format.fourcc);
+ if (srgb_vk_format != VK_FORMAT_UNDEFINED)
+ {
+ vk_supported_formats.insert({srgb_vk_format, vk_format});
+ }
+ else
+ {
+ vk_supported_formats.insert(vk_format);
+ }
+ }
+ }
+}
+
+/*
+ * @brief Query a surface's supported formats from the compositor.
+ *
+ * @details A wl_registry is created in order to get a zwp_linux_dmabuf_v1 object.
+ * Then a listener is attached to that object in order to get the supported formats
+ * from the server. The supported formats are stored in @p vk_supported_formats.
+ *
+ * @param[in] surface The surface, which the supported formats
+ * are for.
+ * @param[out] vk_supported_formats unordered_set which will store the supported
+ * formats.
+ *
+ * @retval VK_SUCCESS Indicates success.
+ * @retval VK_ERROR_SURFACE_LOST_KHR Indicates one of the Wayland functions failed.
+ * @retval VK_ERROR_OUT_OF_DEVICE_MEMORY Indicates the host went out of memory.
+ */
+static VkResult query_supported_formats(
+ const VkSurfaceKHR surface, vk_format_set &vk_supported_formats)
+{
+ const VkIcdSurfaceWayland *vk_surf = reinterpret_cast<VkIcdSurfaceWayland *>(surface);
+ wl_display *display = vk_surf->display;
+
+ auto registry = registry_owner{wl_display_get_registry(display)};
+ if (registry.get() == nullptr)
+ {
+ WSI_PRINT_ERROR("Failed to get wl display registry.\n");
+ return VK_ERROR_SURFACE_LOST_KHR;
+ }
+
+ auto dmabuf_interface = zwp_linux_dmabuf_v1_owner{nullptr};
+ const wl_registry_listener registry_listener = { registry_handler };
+ int res = wl_registry_add_listener(registry.get(), ®istry_listener, &dmabuf_interface);
+ if (res < 0)
+ {
+ WSI_PRINT_ERROR("Failed to add registry listener.\n");
+ return VK_ERROR_SURFACE_LOST_KHR;
+ }
+
+ /* Get the dma buf interface. */
+ res = wl_display_roundtrip(display);
+ if (res < 0)
+ {
+ WSI_PRINT_ERROR("Roundtrip failed.\n");
+ return VK_ERROR_SURFACE_LOST_KHR;
+ }
+
+ if (dmabuf_interface.get() == nullptr)
+ {
+ return VK_ERROR_SURFACE_LOST_KHR;
+ }
+
+ util::vector<drm_format_pair> drm_supported_formats(util::allocator::get_generic());
+ const VkResult ret = get_supported_formats_and_modifiers(display, dmabuf_interface.get(), drm_supported_formats);
+ if (ret != VK_SUCCESS)
+ {
+ return ret == VK_ERROR_UNKNOWN ? VK_ERROR_SURFACE_LOST_KHR : ret;
+ }
+
+ get_vk_supported_formats(drm_supported_formats, vk_supported_formats);
+
+ return ret;
+}
+
VkResult surface_properties::get_surface_formats(VkPhysicalDevice physical_device, VkSurfaceKHR surface,
uint32_t *surfaceFormatCount, VkSurfaceFormatKHR *surfaceFormats)
{
-
- VkResult res = VK_SUCCESS;
- /* TODO: Hardcoding a list of sensible formats, may be query it from compositor later. */
- static std::array<const VkFormat, 2> formats = { VK_FORMAT_R8G8B8A8_UNORM, VK_FORMAT_R8G8B8A8_SRGB };
+ vk_format_set formats;
+ const auto query_res = query_supported_formats(surface, formats);
+ if (query_res != VK_SUCCESS)
+ {
+ return query_res;
+ }
assert(surfaceFormatCount != nullptr);
- res = VK_SUCCESS;
if (nullptr == surfaceFormats)
{
*surfaceFormatCount = formats.size();
+ return VK_SUCCESS;
}
- else
+
+ VkResult res = VK_SUCCESS;
+
+ if (formats.size() > *surfaceFormatCount)
{
- if (formats.size() > *surfaceFormatCount)
- {
- res = VK_INCOMPLETE;
- }
+ res = VK_INCOMPLETE;
+ }
- *surfaceFormatCount = std::min(*surfaceFormatCount, static_cast<uint32_t>(formats.size()));
- for (uint32_t i = 0; i < *surfaceFormatCount; ++i)
+ uint32_t format_count = 0;
+ for (const auto &format : formats)
+ {
+ if (format_count >= *surfaceFormatCount)
{
- surfaceFormats[i].format = formats[i];
- surfaceFormats[i].colorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR;
+ break;
}
+ surfaceFormats[format_count].format = format;
+ surfaceFormats[format_count++].colorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR;
}
+ *surfaceFormatCount = format_count;
return res;
}
#define VK_USE_PLATFORM_WAYLAND_KHR 1
#include "swapchain.hpp"
-#include "swapchain_wl_helpers.hpp"
+#include "wl_helpers.hpp"
#include <cstring>
#include <cassert>
#include "util/drm/drm_utils.hpp"
-#if VULKAN_WSI_DEBUG > 0
-#define WSI_PRINT_ERROR(...) fprintf(stderr, __FILE__, __LINE__, __func__, ##__VA_ARGS__)
-#else
-#define WSI_PRINT_ERROR(...) (void)0
-#endif
-
namespace wsi
{
namespace wayland
{
int res;
teardown();
- if (m_dmabuf_interface != nullptr)
- {
- zwp_linux_dmabuf_v1_destroy(m_dmabuf_interface);
- }
+
res = wsialloc_delete(&m_wsi_allocator);
if (res != 0)
{
return VK_ERROR_INITIALIZATION_FAILED;
}
- wl_registry *registry = wl_display_get_registry(m_display);
- if (registry == nullptr)
+ auto registry = registry_owner{wl_display_get_registry(m_display)};
+ if (registry.get() == nullptr)
{
WSI_PRINT_ERROR("Failed to get wl display registry.\n");
return VK_ERROR_INITIALIZATION_FAILED;
}
- wl_proxy_set_queue((struct wl_proxy *)registry, m_surface_queue);
+ wl_proxy_set_queue((struct wl_proxy *)registry.get(), m_surface_queue);
const wl_registry_listener registry_listener = { registry_handler };
- int res = wl_registry_add_listener(registry, ®istry_listener, &m_dmabuf_interface);
+ int res = wl_registry_add_listener(registry.get(), ®istry_listener, &m_dmabuf_interface);
if (res < 0)
{
WSI_PRINT_ERROR("Failed to add registry listener.\n");
return VK_ERROR_INITIALIZATION_FAILED;
}
- /* we should have the dma_buf interface by now */
- assert(m_dmabuf_interface);
-
- wl_registry_destroy(registry);
+ if (m_dmabuf_interface.get() == nullptr)
+ {
+ return VK_ERROR_INITIALIZATION_FAILED;
+ }
res = wsialloc_new(-1, &m_wsi_allocator);
if (res != 0)
/* create a wl_buffer using the dma_buf protocol */
struct zwp_linux_buffer_params_v1 *params;
- params = zwp_linux_dmabuf_v1_create_params(m_dmabuf_interface);
+ params = zwp_linux_dmabuf_v1_create_params(m_dmabuf_interface.get());
zwp_linux_buffer_params_v1_add(params, image_data->buffer_fd, 0, image_data->offset, image_data->stride, 0, 0);
wl_proxy_set_queue((struct wl_proxy *)params, m_surface_queue);
res = zwp_linux_buffer_params_v1_add_listener(params, ¶ms_listener, &image_data->buffer);
extern "C" {
#include <vulkan/vk_icd.h>
-#include <util/wsialloc/wsialloc.h>
+}
+
#include <wayland-client.h>
#include <linux-dmabuf-unstable-v1-client-protocol.h>
-}
+#include "util/wsialloc/wsialloc.h"
+#include "wl_object_owner.hpp"
namespace wsi
{
struct wl_display *m_display;
struct wl_surface *m_surface;
- struct zwp_linux_dmabuf_v1 *m_dmabuf_interface;
+ zwp_linux_dmabuf_v1_owner m_dmabuf_interface;
/* The queue on which we dispatch the swapchain related events, mostly frame completion */
struct wl_event_queue *m_surface_queue;
* SOFTWARE.
*/
-#include "swapchain_wl_helpers.hpp"
+#include "wl_helpers.hpp"
-#include <wayland-client.h>
-#include <linux-dmabuf-unstable-v1-client-protocol.h>
-#include <string.h>
-#include <assert.h>
+#include <cstring>
+#include <memory>
#include <poll.h>
#include <errno.h>
+#include <cassert>
+
+#include "wl_object_owner.hpp"
+
+struct formats_vector
+{
+ util::vector<drm_format_pair> *formats{nullptr};
+ bool is_out_of_memory{false};
+};
+
+namespace
+{
+ /* Handler for format event of the zwp_linux_dmabuf_v1 interface. */
+ extern "C" void dma_buf_format_handler(void *data,
+ struct zwp_linux_dmabuf_v1 *dma_buf,
+ uint32_t drm_format) {}
+
+ /* Handler for modifier event of the zwp_linux_dmabuf_v1 interface. */
+ extern "C" void dma_buf_modifier_handler(void *data,
+ struct zwp_linux_dmabuf_v1 *dma_buf,
+ uint32_t drm_format, uint32_t modifier_hi,
+ uint32_t modifier_low)
+ {
+ auto *drm_supported_formats = reinterpret_cast<formats_vector *>(data);
+
+ drm_format_pair format = {};
+ format.fourcc = drm_format;
+ format.modifier = (static_cast<uint64_t>(modifier_hi) << 32) | modifier_low;
+
+ if (!drm_supported_formats->formats->try_push_back(format))
+ {
+ drm_supported_formats->is_out_of_memory = true;
+ }
+ }
+}
+
+VkResult get_supported_formats_and_modifiers(
+ wl_display* display, zwp_linux_dmabuf_v1 *dmabuf_interface,
+ util::vector<drm_format_pair> &supported_formats)
+{
+ formats_vector drm_supported_formats;
+ drm_supported_formats.formats = &supported_formats;
+
+ const zwp_linux_dmabuf_v1_listener dma_buf_listener = {
+ .format = dma_buf_format_handler, .modifier = dma_buf_modifier_handler,
+ };
+ int res = zwp_linux_dmabuf_v1_add_listener(dmabuf_interface, &dma_buf_listener,
+ &drm_supported_formats);
+ if (res < 0)
+ {
+ WSI_PRINT_ERROR("Failed to add zwp_linux_dmabuf_v1 listener.\n");
+ return VK_ERROR_UNKNOWN;
+ }
+
+ /* Get all modifier events. */
+ res = wl_display_roundtrip(display);
+ if (res < 0)
+ {
+ WSI_PRINT_ERROR("Roundtrip failed.\n");
+ return VK_ERROR_UNKNOWN;
+ }
+
+ if (drm_supported_formats.is_out_of_memory)
+ {
+ WSI_PRINT_ERROR("Host got out of memory.\n");
+ return VK_ERROR_OUT_OF_HOST_MEMORY;
+ }
+
+ return VK_SUCCESS;
+}
extern "C" {
void registry_handler(void *data, struct wl_registry *wl_registry, uint32_t name, const char *interface,
uint32_t version)
{
- zwp_linux_dmabuf_v1 **dmabuf_interface = (zwp_linux_dmabuf_v1 **)data;
+ auto dmabuf_interface = reinterpret_cast<wsi::wayland::zwp_linux_dmabuf_v1_owner* >(data);
if (!strcmp(interface, "zwp_linux_dmabuf_v1"))
{
- *dmabuf_interface =
- (zwp_linux_dmabuf_v1 *)wl_registry_bind(wl_registry, name, &zwp_linux_dmabuf_v1_interface, version);
- assert(*dmabuf_interface);
+ version = ZWP_LINUX_DMABUF_V1_MODIFIER_SINCE_VERSION;
+ zwp_linux_dmabuf_v1 *dmabuf_interface_obj =
+ reinterpret_cast<zwp_linux_dmabuf_v1 *>(wl_registry_bind(
+ wl_registry, name, &zwp_linux_dmabuf_v1_interface, version));
+
+ if (dmabuf_interface_obj == nullptr)
+ {
+ WSI_PRINT_ERROR("Failed to get zwp_linux_dmabuf_v1 interface.\n");
+ return;
+ }
+
+ dmabuf_interface->reset(dmabuf_interface_obj);
}
}
#pragma once
+#include <stdint.h>
#include <wayland-client.h>
+#include <linux-dmabuf-unstable-v1-client-protocol.h>
+#include "util/custom_allocator.hpp"
+
+#if VULKAN_WSI_DEBUG > 0
+#define WSI_PRINT_ERROR(...) fprintf(stderr, __FILE__, __LINE__, __func__, ##__VA_ARGS__)
+#else
+#define WSI_PRINT_ERROR(...) (void)0
+#endif
+
+struct drm_format_pair
+{
+ uint32_t fourcc;
+ uint64_t modifier;
+};
+
+/*
+ * @brief Get supported formats and modifiers using the zwp_linux_dmabuf_v1 interface.
+ *
+ * @param[in] display The wl_display that is being used.
+ * @param[in] dmabuf_interface Object of the zwp_linux_dmabuf_v1 interface.
+ * @param[out] supported_formats Vector which will contain the supported drm
+ * formats and their modifiers.
+ *
+ * @retval VK_SUCCESS Indicates success.
+ * @retval VK_ERROR_UNKNOWN Indicates one of the Wayland functions failed.
+ * @retval VK_ERROR_OUT_OF_DEVICE_MEMORY Indicates the host went out of memory.
+ */
+VkResult get_supported_formats_and_modifiers(
+ struct wl_display* display, struct zwp_linux_dmabuf_v1 *dmabuf_interface,
+ util::vector<drm_format_pair> &supported_formats);
extern "C" {
void registry_handler(void *data, struct wl_registry *wl_registry, uint32_t name, const char *interface,
--- /dev/null
+/*
+ * Copyright (c) 2021 Arm Limited.
+ *
+ * SPDX-License-Identifier: MIT
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#pragma once
+
+#include <wayland-client.h>
+#include <linux-dmabuf-unstable-v1-client-protocol.h>
+#include <memory.h>
+
+namespace wsi
+{
+namespace wayland
+{
+
+struct registry_deleter
+{
+ void operator()(wl_registry* obj) const
+ {
+ if (obj != nullptr)
+ {
+ wl_registry_destroy(obj);
+ }
+ }
+};
+
+struct dmabuf_deleter
+{
+ void operator()(zwp_linux_dmabuf_v1* obj) const
+ {
+ if (obj != nullptr)
+ {
+ zwp_linux_dmabuf_v1_destroy(obj);
+ }
+ }
+};
+
+using registry_owner = std::unique_ptr<wl_registry, registry_deleter>;
+using zwp_linux_dmabuf_v1_owner = std::unique_ptr<zwp_linux_dmabuf_v1, dmabuf_deleter>;
+} // namespace wayland
+} // namespace wsi