vulkan-wsi-layer/wsi/wayland/present_timing_handler.cpp
Normunds Rieksts 9d9172fcbb Add a workaround when wp_feedback events are not delivered
Adds a workaround for compositors that do not deliver
wp_presentation_feedback events when images are submitted to the
compositor in quick succession resulting in some of them being
discarded by handling it in buffer_release event instead.

Additionally fixes a double-free bug when presentation_feedback was
replaced using move semantics.

Change-Id: I97715ffbc45c3c869f84e57dca532d3a58ad3b67
Signed-off-by: Normunds Rieksts <normunds.rieksts@arm.com>
2025-10-31 10:35:08 +00:00

195 lines
7.1 KiB
C++

/*
* Copyright (c) 2025 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.
*/
/**
* @file present_timing_handler.cpp
*
* @brief Contains the functionality to implement features for present timing extension.
*/
#include <util/custom_mutex.hpp>
#include "present_timing_handler.hpp"
#include "surface.hpp"
namespace wsi
{
namespace wayland
{
wsi_ext_present_timing_wayland::wsi_ext_present_timing_wayland(
const util::allocator &allocator, VkDevice device, uint32_t num_images,
util::vector<std::optional<uint64_t>> &&timestamp_first_pixel_out_storage)
: wsi_ext_present_timing(allocator, device, num_images)
, m_timestamp_first_pixel_out(allocator)
{
m_timestamp_first_pixel_out.swap(timestamp_first_pixel_out_storage);
}
util::unique_ptr<wsi_ext_present_timing_wayland> wsi_ext_present_timing_wayland::create(
VkDevice device, const util::allocator &allocator, std::optional<VkTimeDomainKHR> image_first_pixel_out_time_domain,
uint32_t num_images)
{
util::vector<util::unique_ptr<wsi::vulkan_time_domain>> domains(allocator);
if (!domains.try_push_back(allocator.make_unique<wsi::vulkan_time_domain>(
VK_PRESENT_STAGE_QUEUE_OPERATIONS_END_BIT_EXT, VK_TIME_DOMAIN_DEVICE_KHR)))
{
return nullptr;
}
if (image_first_pixel_out_time_domain.has_value())
{
std::tuple<VkTimeDomainEXT, bool> monotonic_query = { *image_first_pixel_out_time_domain, false };
const layer::device_private_data &device_data = layer::device_private_data::get(device);
auto result = wsi::check_time_domain_support(device_data.physical_device, &monotonic_query, 1);
if (result != VK_SUCCESS)
{
return nullptr;
}
if (std::get<1>(monotonic_query))
{
if (!domains.try_push_back(allocator.make_unique<wsi::vulkan_time_domain>(
VK_PRESENT_STAGE_IMAGE_FIRST_PIXEL_OUT_BIT_EXT, image_first_pixel_out_time_domain.value())))
{
return nullptr;
}
}
}
util::vector<std::optional<uint64_t>> timestamp_first_pixel_out_storage(allocator);
if (!timestamp_first_pixel_out_storage.try_resize(num_images))
{
return nullptr;
}
return wsi_ext_present_timing::create<wsi_ext_present_timing_wayland>(
allocator, domains.data(), domains.size(), device, num_images, std::move(timestamp_first_pixel_out_storage));
}
VkResult wsi_ext_present_timing_wayland::get_swapchain_timing_properties(
uint64_t &timing_properties_counter, VkSwapchainTimingPropertiesEXT &timing_properties)
{
timing_properties_counter = 0;
timing_properties.refreshDuration = 0;
timing_properties.refreshInterval = 0;
return VK_SUCCESS;
}
void wsi_ext_present_timing_wayland::mark_delivered(uint32_t image_index, uint64_t time)
{
pixelout_callback(image_index, time);
remove_from_pending_present_feedback_list(image_index);
}
void wsi_ext_present_timing_wayland::mark_buffer_release(uint32_t image_index)
{
auto was_removed = remove_from_pending_present_feedback_list(image_index);
if (was_removed)
{
pixelout_callback(image_index, 0);
}
}
presentation_feedback *wsi_ext_present_timing_wayland::insert_into_pending_present_feedback_list(
uint32_t image_index, struct wp_presentation_feedback *feedback_obj)
{
util::unique_lock<util::mutex> lock(m_pending_presents_lock);
if (!lock)
{
WSI_LOG_ERROR("Failed to acquire pending presents lock in insert_into_pending_present_feedback_list.\n");
abort();
}
/* We should not be replacing pending entries. This most likely has happened due to events not being triggered
* that would discard or mark the pending present request as completed which could be an inidcation of a bug somewhere. */
assert(!m_pending_presents[image_index].has_value());
m_pending_presents[image_index] = presentation_feedback(feedback_obj, this, image_index);
return &m_pending_presents[image_index].value();
}
bool wsi_ext_present_timing_wayland::remove_from_pending_present_feedback_list(uint32_t image_index)
{
util::unique_lock<util::mutex> lock(m_pending_presents_lock);
if (!lock)
{
WSI_LOG_ERROR("Failed to acquire pending presents lock in remove_from_pending_present_feedback_list.\n");
abort();
}
const bool has_entry = m_pending_presents[image_index].has_value();
m_pending_presents[image_index].reset();
return has_entry;
}
void wsi_ext_present_timing_wayland::pixelout_callback(uint32_t image_index, uint64_t time)
{
/* m_timestamp_first_pixel_out for a particular index is updated by thread safe Wayland event
* and is read only after the event had processed. This is because we get the read request
* either during the get_free_buffer() or by calling the dispatch_queue(). Additionally, the
* variable being std::optional, there is no time-race happening due to hardware reording.
* This is because the variable is read after checking for whether it has value.
* This way each index of the variable is thread safe.
*/
m_timestamp_first_pixel_out[image_index] = time;
}
VkResult wsi_ext_present_timing_wayland::get_pixel_out_timing_to_queue(
uint32_t image_index, std::optional<std::reference_wrapper<swapchain_presentation_timing>> stage_timing_optional)
{
/* Try to get the event until there are no more events in
* the queue or till we get the presentation callback. */
while (!m_timestamp_first_pixel_out[image_index].has_value())
{
int res = dispatch_queue(m_display, m_queue, 0);
if (res < 0)
{
return VK_ERROR_SURFACE_LOST_KHR;
}
else if (res == 0)
{
break;
}
}
if (m_timestamp_first_pixel_out[image_index].has_value())
{
stage_timing_optional->get().set_time(m_timestamp_first_pixel_out[image_index].value());
m_timestamp_first_pixel_out[image_index].reset();
}
return VK_SUCCESS;
}
void wsi_ext_present_timing_wayland::init(wl_display *display, struct wl_event_queue *queue)
{
/* These objects shouldn't be set twice */
assert(m_display == nullptr);
assert(m_queue == nullptr);
m_display = display;
m_queue = queue;
}
} // namespace wayland
} // namespace wsi