2022-06-10 16:49:28 +00:00
|
|
|
/*
|
|
|
|
* This file is part of mpv.
|
|
|
|
*
|
|
|
|
* mpv is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* mpv is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with mpv. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <time.h>
|
|
|
|
|
2023-11-02 18:41:32 +00:00
|
|
|
#include "misc/linked_list.h"
|
2022-06-10 16:49:28 +00:00
|
|
|
#include "mpv_talloc.h"
|
|
|
|
#include "osdep/timer.h"
|
|
|
|
#include "present_sync.h"
|
|
|
|
|
|
|
|
/* General nonsense about this mechanism.
|
|
|
|
*
|
|
|
|
* This requires that that caller has access to two, related values:
|
|
|
|
* (ust, msc): clock time and incrementing counter of last vsync (this is
|
|
|
|
* increased continuously, even if we don't swap)
|
|
|
|
*
|
2023-10-29 17:41:26 +00:00
|
|
|
* Note that this concept originates from the GLX_OML_sync_control extension
|
2022-06-10 16:49:28 +00:00
|
|
|
* which includes another parameter: sbc (swap counter of frame that was
|
|
|
|
* last displayed). Both the xorg present extension and wayland's
|
|
|
|
* presentation-time protocol do not include sbc values so they are omitted
|
|
|
|
* from this mechanism. mpv does not need to keep track of sbc calls and can
|
|
|
|
* have reliable presentation without it.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void present_sync_get_info(struct mp_present *present, struct vo_vsync_info *info)
|
|
|
|
{
|
2023-11-02 18:41:32 +00:00
|
|
|
struct mp_present_entry *cur = present->head;
|
|
|
|
while (cur) {
|
|
|
|
if (cur->queue_display_time)
|
|
|
|
break;
|
|
|
|
cur = cur->list_node.next;
|
|
|
|
}
|
|
|
|
if (!cur)
|
|
|
|
return;
|
|
|
|
|
|
|
|
info->vsync_duration = cur->vsync_duration;
|
|
|
|
info->skipped_vsyncs = cur->skipped_vsyncs;
|
|
|
|
info->last_queue_display_time = cur->queue_display_time;
|
|
|
|
|
|
|
|
// Remove from the list, zero out everything, and append at the end
|
|
|
|
LL_REMOVE(list_node, present, cur);
|
|
|
|
*cur = (struct mp_present_entry){0};
|
|
|
|
LL_APPEND(list_node, present, cur);
|
|
|
|
}
|
|
|
|
|
2023-11-06 22:35:04 +00:00
|
|
|
struct mp_present *mp_present_initialize(void *talloc_ctx, struct mp_vo_opts *opts, int entries)
|
2023-11-02 18:41:32 +00:00
|
|
|
{
|
|
|
|
struct mp_present *present = talloc_zero(talloc_ctx, struct mp_present);
|
|
|
|
for (int i = 0; i < entries; i++) {
|
|
|
|
struct mp_present_entry *entry = talloc_zero(present, struct mp_present_entry);
|
|
|
|
LL_APPEND(list_node, present, entry);
|
|
|
|
}
|
2023-11-06 22:35:04 +00:00
|
|
|
present->opts = opts;
|
2023-11-02 18:41:32 +00:00
|
|
|
return present;
|
2022-06-10 16:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void present_sync_swap(struct mp_present *present)
|
|
|
|
{
|
2023-11-02 18:41:32 +00:00
|
|
|
struct mp_present_entry *cur = present->head;
|
|
|
|
while (cur) {
|
|
|
|
if (!cur->queue_display_time)
|
|
|
|
break;
|
|
|
|
cur = cur->list_node.next;
|
|
|
|
}
|
|
|
|
if (!cur)
|
|
|
|
return;
|
|
|
|
|
|
|
|
int64_t ust = cur->ust;
|
|
|
|
int64_t msc = cur->msc;
|
|
|
|
int64_t last_ust = cur->list_node.prev ? cur->list_node.prev->ust : 0;
|
|
|
|
int64_t last_msc = cur->list_node.prev ? cur->list_node.prev->msc : 0;
|
2022-06-10 16:49:28 +00:00
|
|
|
|
|
|
|
// Avoid attempting to use any presentation statistics if the ust is 0 or has
|
2023-11-02 18:41:32 +00:00
|
|
|
// not actually updated (i.e. the last_ust is equal to ust).
|
|
|
|
if (!ust || ust == last_ust) {
|
|
|
|
cur->skipped_vsyncs = -1;
|
|
|
|
cur->vsync_duration = -1;
|
|
|
|
cur->queue_display_time = -1;
|
2022-06-10 16:49:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-11-02 18:41:32 +00:00
|
|
|
cur->skipped_vsyncs = 0;
|
|
|
|
int64_t ust_passed = ust ? ust - last_ust: 0;
|
|
|
|
int64_t msc_passed = msc ? msc - last_msc: 0;
|
2022-06-10 16:49:28 +00:00
|
|
|
if (msc_passed && ust_passed)
|
2023-11-02 18:41:32 +00:00
|
|
|
cur->vsync_duration = ust_passed / msc_passed;
|
2022-06-10 16:49:28 +00:00
|
|
|
|
|
|
|
struct timespec ts;
|
2023-09-13 03:17:05 +00:00
|
|
|
if (clock_gettime(CLOCK_MONOTONIC, &ts))
|
2022-06-10 16:49:28 +00:00
|
|
|
return;
|
|
|
|
|
2023-10-22 02:31:36 +00:00
|
|
|
int64_t now_monotonic = MP_TIME_S_TO_NS(ts.tv_sec) + ts.tv_nsec;
|
2023-09-16 02:45:24 +00:00
|
|
|
int64_t ust_mp_time = mp_time_ns() - (now_monotonic - ust);
|
2023-11-02 18:41:32 +00:00
|
|
|
cur->queue_display_time = ust_mp_time;
|
2022-06-10 16:49:28 +00:00
|
|
|
}
|
|
|
|
|
2023-11-02 21:03:03 +00:00
|
|
|
void present_sync_update_values(struct mp_present *present, int64_t ust,
|
2022-06-10 16:49:28 +00:00
|
|
|
int64_t msc)
|
|
|
|
{
|
2023-11-02 18:41:32 +00:00
|
|
|
struct mp_present_entry *cur = present->head;
|
2023-11-06 22:35:04 +00:00
|
|
|
int index = 0;
|
|
|
|
while (cur && ++index) {
|
|
|
|
if (!cur->ust || index == present->opts->swapchain_depth)
|
2023-11-02 18:41:32 +00:00
|
|
|
break;
|
|
|
|
cur = cur->list_node.next;
|
|
|
|
}
|
|
|
|
if (!cur)
|
|
|
|
return;
|
|
|
|
|
|
|
|
cur->ust = ust;
|
|
|
|
cur->msc = msc;
|
2022-06-10 16:49:28 +00:00
|
|
|
}
|