clutter/frame-clock: Set the last "next presentation time" on feedback

This removes an incorrect implicit assumption in
calculate_next_update_time_us() that a frame may only be scheduled
once in the duration of a refresh cycle. It accomplishes this by
setting last_next_presentation_time_us on presentation feedback
instead of calculating it every time an update is scheduled.

Specifically, it corrects the intended scheduling logic in scenarios
like the following, when all of the below occur in the context of a
single refresh cycle:
  1. Frame update (1) is scheduled normally, and
     "is_next_presentation_time_valid" is set to TRUE
  2. Frame update (1) is dispatched but ends up being "empty" (no
     presentation necessary)
  3. Frame update (2) is scheduled "now" and
     "is_next_presentation_time_valid" is set to FALSE
  4. Frame update (2) is dispatched but ends up being "empty" (no
     presentation necessary)
  5. Frame update (3) is scheduled normally, and since
     "is_next_presentation_time_valid" is set to FALSE, the
     "early presented event" logic is unintentionally skipped in
     calculate_next_update_time_us().
  6. Frame update (3) is dispatched and ends up being a "non-empty"
     update, but its update time was calculated incorrectly because
     some logic was skipped.

Scenarios such as this would become more common with the introduction
of variable refresh rate since it makes scheduling "now" a commonplace
occurrence.

Part-of: <https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/3560>
This commit is contained in:
Dor Askayo 2024-02-03 13:51:39 +02:00 committed by Marge Bot
parent cd245bce0c
commit aedb0f200d

View File

@ -83,6 +83,9 @@ struct _ClutterFrameClock
gboolean has_next_frame_deadline;
int64_t next_frame_deadline_us;
gboolean has_last_next_presentation_time;
int64_t last_next_presentation_time_us;
/* Buffer must be submitted to KMS and GPU rendering must be finished
* this amount of time before the next presentation time.
*/
@ -258,18 +261,23 @@ clutter_frame_clock_notify_presented (ClutterFrameClock *frame_clock,
COGL_TRACE_DESCRIBE (ClutterFrameClockNotifyPresented,
frame_clock->output_name);
frame_clock->last_next_presentation_time_us =
frame_clock->next_presentation_time_us;
frame_clock->has_last_next_presentation_time =
frame_clock->is_next_presentation_time_valid;
if (G_UNLIKELY (CLUTTER_HAS_DEBUG (FRAME_CLOCK)))
{
int64_t now_us;
if (frame_clock->is_next_presentation_time_valid &&
if (frame_clock->has_last_next_presentation_time &&
frame_info->presentation_time != 0)
{
int64_t diff_us;
int n_missed_frames;
diff_us = llabs (frame_info->presentation_time -
frame_clock->next_presentation_time_us);
frame_clock->last_next_presentation_time_us);
n_missed_frames =
(int) roundf ((float) diff_us /
(float) frame_clock->refresh_interval_us);
@ -543,9 +551,8 @@ calculate_next_update_time_us (ClutterFrameClock *frame_clock,
next_presentation_time_us = now_us - current_phase_us + refresh_interval_us;
}
if (frame_clock->is_next_presentation_time_valid)
if (frame_clock->has_last_next_presentation_time)
{
int64_t last_next_presentation_time_us;
int64_t time_since_last_next_presentation_time_us;
/*
@ -562,9 +569,8 @@ calculate_next_update_time_us (ClutterFrameClock *frame_clock,
* time_since_last_next_presentation_time_us
*
*/
last_next_presentation_time_us = frame_clock->next_presentation_time_us;
time_since_last_next_presentation_time_us =
next_presentation_time_us - last_next_presentation_time_us;
next_presentation_time_us - frame_clock->last_next_presentation_time_us;
if (time_since_last_next_presentation_time_us > 0 &&
time_since_last_next_presentation_time_us < (refresh_interval_us / 2))
{