2024-11-29 17:22:59 +00:00
|
|
|
/*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2025-09-05 15:06:38 +00:00
|
|
|
#include <util/custom_mutex.hpp>
|
2024-11-29 17:22:59 +00:00
|
|
|
#include "present_timing_handler.hpp"
|
2025-07-24 08:39:09 +00:00
|
|
|
#include "surface.hpp"
|
2024-11-29 17:22:59 +00:00
|
|
|
|
2025-07-24 08:39:09 +00:00
|
|
|
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>> &×tamp_first_pixel_out_storage)
|
2025-05-28 15:59:16 +00:00
|
|
|
: wsi_ext_present_timing(allocator, device, num_images)
|
2025-07-24 08:39:09 +00:00
|
|
|
, m_timestamp_first_pixel_out(allocator)
|
2024-11-29 17:22:59 +00:00
|
|
|
{
|
2025-07-24 08:39:09 +00:00
|
|
|
m_timestamp_first_pixel_out.swap(timestamp_first_pixel_out_storage);
|
2024-11-29 17:22:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
util::unique_ptr<wsi_ext_present_timing_wayland> wsi_ext_present_timing_wayland::create(
|
2025-07-24 08:39:09 +00:00
|
|
|
VkDevice device, const util::allocator &allocator, std::optional<VkTimeDomainKHR> image_first_pixel_out_time_domain,
|
|
|
|
|
uint32_t num_images)
|
2024-11-29 17:22:59 +00:00
|
|
|
{
|
2025-06-18 12:15:41 +00:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-24 08:39:09 +00:00
|
|
|
if (image_first_pixel_out_time_domain.has_value())
|
2025-06-18 12:15:41 +00:00
|
|
|
{
|
2025-07-24 08:39:09 +00:00
|
|
|
std::tuple<VkTimeDomainEXT, bool> monotonic_query = { *image_first_pixel_out_time_domain, false };
|
2025-06-18 12:15:41 +00:00
|
|
|
|
|
|
|
|
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>(
|
2025-07-24 08:39:09 +00:00
|
|
|
VK_PRESENT_STAGE_IMAGE_FIRST_PIXEL_OUT_BIT_EXT, image_first_pixel_out_time_domain.value())))
|
2025-06-18 12:15:41 +00:00
|
|
|
{
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-07-24 08:39:09 +00:00
|
|
|
util::vector<std::optional<uint64_t>> timestamp_first_pixel_out_storage(allocator);
|
|
|
|
|
if (!timestamp_first_pixel_out_storage.try_resize(num_images))
|
|
|
|
|
{
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2025-06-18 12:15:41 +00:00
|
|
|
|
2025-07-24 08:39:09 +00:00
|
|
|
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));
|
2024-11-29 17:22:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
2025-09-04 16:48:10 +00:00
|
|
|
timing_properties.refreshInterval = 0;
|
2024-11-29 17:22:59 +00:00
|
|
|
|
|
|
|
|
return VK_SUCCESS;
|
2025-03-07 10:28:00 +00:00
|
|
|
}
|
2025-07-24 08:39:09 +00:00
|
|
|
|
2025-10-31 10:35:08 +00:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-24 08:39:09 +00:00
|
|
|
presentation_feedback *wsi_ext_present_timing_wayland::insert_into_pending_present_feedback_list(
|
|
|
|
|
uint32_t image_index, struct wp_presentation_feedback *feedback_obj)
|
|
|
|
|
{
|
2025-09-11 15:47:13 +00:00
|
|
|
|
2025-09-05 15:06:38 +00:00
|
|
|
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();
|
|
|
|
|
}
|
2025-10-31 10:35:08 +00:00
|
|
|
|
|
|
|
|
/* 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());
|
|
|
|
|
|
2025-09-11 15:47:13 +00:00
|
|
|
m_pending_presents[image_index] = presentation_feedback(feedback_obj, this, image_index);
|
|
|
|
|
return &m_pending_presents[image_index].value();
|
2025-07-24 08:39:09 +00:00
|
|
|
}
|
|
|
|
|
|
2025-10-31 10:35:08 +00:00
|
|
|
bool wsi_ext_present_timing_wayland::remove_from_pending_present_feedback_list(uint32_t image_index)
|
2025-07-24 08:39:09 +00:00
|
|
|
{
|
2025-09-05 15:06:38 +00:00
|
|
|
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();
|
|
|
|
|
}
|
2025-10-31 10:35:08 +00:00
|
|
|
|
|
|
|
|
const bool has_entry = m_pending_presents[image_index].has_value();
|
2025-09-11 15:47:13 +00:00
|
|
|
m_pending_presents[image_index].reset();
|
2025-10-31 10:35:08 +00:00
|
|
|
return has_entry;
|
2025-07-24 08:39:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2025-07-28 11:43:34 +01:00
|
|
|
/* 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())
|
2025-07-24 08:39:09 +00:00
|
|
|
{
|
2025-07-28 11:43:34 +01:00
|
|
|
int res = dispatch_queue(m_display, m_queue, 0);
|
|
|
|
|
if (res < 0)
|
2025-07-24 08:39:09 +00:00
|
|
|
{
|
|
|
|
|
return VK_ERROR_SURFACE_LOST_KHR;
|
|
|
|
|
}
|
2025-07-28 11:43:34 +01:00
|
|
|
else if (res == 0)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
2025-07-24 08:39:09 +00:00
|
|
|
}
|
|
|
|
|
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
|