vulkan-wsi-layer/wsi/wayland/wl_helpers.cpp
Rosen Zhelev 24f18c6c44 wsi: Move common objects to the wayland wsi::surface
Moves data and Wayland objects such as the registry to the
wsi::wayland::surface implementation. This also fixes a few issues
with queries where the Wayland display's default queue was wrongly
used and minimizes leaking of server memory from registry creation.

Moves the ownership of the zwp_linux_dmabuf_interface object to the
wsi::wayland::surface as it can be shared between swapchains.

Fixes issues with casting VkSurfaces in the Wayland backend that can
fail when using other layers that wrap the object.

Change-Id: Ibacf0d4229b73bd685254507f52e58d6341aa9b6
Signed-off-by: Rosen Zhelev <rosen.zhelev@arm.com>
2021-08-17 16:42:21 +01:00

142 lines
4.9 KiB
C++

/*
* Copyright (c) 2017-2019, 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.
*/
#include "wl_helpers.hpp"
#include <cstring>
#include <memory>
#include <poll.h>
#include <errno.h>
#include <cassert>
#include "wl_object_owner.hpp"
#include "util/log.hpp"
extern "C" {
void registry_handler(void *data, struct wl_registry *wl_registry, uint32_t name, const char *interface,
uint32_t version)
{
auto dmabuf_interface = reinterpret_cast<wsi::wayland::zwp_linux_dmabuf_v1_owner* >(data);
if (!strcmp(interface, "zwp_linux_dmabuf_v1"))
{
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_LOG_ERROR("Failed to get zwp_linux_dmabuf_v1 interface.");
return;
}
dmabuf_interface->reset(dmabuf_interface_obj);
}
}
int dispatch_queue(struct wl_display *display, struct wl_event_queue *queue, int timeout)
{
int err;
struct pollfd pfd = {};
int retval;
/* Before we sleep, dispatch any pending events. prepare_read_queue will return 0 whilst there are pending
* events to dispatch on the queue. */
while (0 != wl_display_prepare_read_queue(display, queue))
{
/* dispatch_queue_pending returns -1 on error, or the number of events dispatched otherwise. If we
* already dispatched some events, then we might not need to sleep, as we might have just dispatched
* the event we want, so return immediately. */
err = wl_display_dispatch_queue_pending(display, queue);
if (err)
{
return (0 > err) ? -1 : 1;
}
}
/* wl_display_read_events performs a non-blocking read. */
pfd.fd = wl_display_get_fd(display);
pfd.events = POLLIN;
while (true)
{
/* Timeout is given in milliseconds. A return value of 0, or -1 with errno set to EINTR means that we
* should retry as the timeout was exceeded or we were interrupted by a signal, respectively. A
* return value of 1 means that something happened, and we should inspect the pollfd structure to see
* just what that was.
*/
err = poll(&pfd, 1, timeout);
if (0 == err)
{
/* Timeout. */
wl_display_cancel_read(display);
return 0;
}
else if (-1 == err)
{
if (EINTR == errno)
{
/* Interrupted by a signal; restart. This resets the timeout. */
continue;
}
else
{
/* Something else bad happened; abort. */
wl_display_cancel_read(display);
return -1;
}
}
else
{
if (POLLIN == pfd.revents)
{
/* We have data to read, and no errors; proceed to read_events. */
break;
}
else
{
/* An error occurred, e.g. file descriptor was closed from underneath us. */
wl_display_cancel_read(display);
return -1;
}
}
}
/* Actually read the events from the display. A failure in read_events calls cancel_read internally for us,
* so we don't need to do that here. */
err = wl_display_read_events(display);
if (0 != err)
{
return -1;
}
/* Finally, if we read any events relevant to our queue, we can dispatch them. */
err = wl_display_dispatch_queue_pending(display, queue);
retval = err < 0 ? -1 : 1;
return retval;
}
}