2012-10-03 22:34:01 +00:00
|
|
|
/* GDK - The GIMP Drawing Kit
|
|
|
|
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
|
|
|
|
*
|
|
|
|
* This library 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 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
2014-01-12 12:56:49 +00:00
|
|
|
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
2012-10-03 22:34:01 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Modified by the GTK+ Team and others 1997-2010. See the AUTHORS
|
|
|
|
* file for a list of people on the GTK+ Team. See the ChangeLog
|
|
|
|
* files for a list of changes. These files are distributed with
|
|
|
|
* GTK+ at ftp://ftp.gtk.org/pub/gtk/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2012-11-15 00:21:33 +00:00
|
|
|
#include "gdkinternals.h"
|
2013-02-12 20:02:21 +00:00
|
|
|
#include "gdkframeclockprivate.h"
|
2012-10-03 22:34:01 +00:00
|
|
|
#include "gdkframeclockidle.h"
|
2019-06-06 23:19:36 +00:00
|
|
|
#include "gdkprofilerprivate.h"
|
2012-10-03 22:34:01 +00:00
|
|
|
#include "gdk.h"
|
|
|
|
|
2013-02-18 14:37:22 +00:00
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
2013-02-25 09:42:42 +00:00
|
|
|
#define FRAME_INTERVAL 16667 /* microseconds */
|
2012-09-27 21:55:55 +00:00
|
|
|
|
2012-10-03 22:34:01 +00:00
|
|
|
struct _GdkFrameClockIdlePrivate
|
|
|
|
{
|
2020-05-28 15:44:51 +00:00
|
|
|
gint64 frame_time; /* The exact time we last ran the clock cycle, or 0 if never */
|
|
|
|
gint64 smoothed_frame_time_base; /* A grid-aligned version of frame_time (grid size == refresh period), never more than half a grid from frame_time */
|
|
|
|
gint64 smoothed_frame_time_period; /* The grid size that smoothed_frame_time_base is aligned to */
|
|
|
|
gint64 smoothed_frame_time_reported; /* Ensures we are always monotonic */
|
|
|
|
gint64 min_next_frame_time; /* We're not synced to vblank, so wait at least until this before next cycle to avoid busy looping */
|
2012-11-14 21:08:08 +00:00
|
|
|
gint64 sleep_serial;
|
2019-06-06 23:19:36 +00:00
|
|
|
#ifdef G_ENABLE_DEBUG
|
|
|
|
gint64 freeze_time;
|
|
|
|
#endif
|
2012-10-03 22:34:01 +00:00
|
|
|
|
2012-09-26 14:28:06 +00:00
|
|
|
guint flush_idle_id;
|
|
|
|
guint paint_idle_id;
|
2012-10-03 23:42:13 +00:00
|
|
|
guint freeze_count;
|
2013-02-15 22:04:39 +00:00
|
|
|
guint updating_count;
|
2012-10-03 22:34:01 +00:00
|
|
|
|
2012-10-03 23:38:40 +00:00
|
|
|
GdkFrameClockPhase requested;
|
|
|
|
GdkFrameClockPhase phase;
|
2012-11-14 18:26:13 +00:00
|
|
|
|
|
|
|
guint in_paint_idle : 1;
|
2020-06-05 09:16:57 +00:00
|
|
|
guint paint_is_thaw : 1;
|
2013-02-18 14:37:22 +00:00
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
guint begin_period : 1;
|
|
|
|
#endif
|
2012-10-03 22:34:01 +00:00
|
|
|
};
|
|
|
|
|
2012-09-26 14:28:06 +00:00
|
|
|
static gboolean gdk_frame_clock_flush_idle (void *data);
|
2012-10-03 23:38:40 +00:00
|
|
|
static gboolean gdk_frame_clock_paint_idle (void *data);
|
|
|
|
|
2013-06-24 17:54:05 +00:00
|
|
|
G_DEFINE_TYPE_WITH_PRIVATE (GdkFrameClockIdle, gdk_frame_clock_idle, GDK_TYPE_FRAME_CLOCK)
|
2012-10-03 22:34:01 +00:00
|
|
|
|
2012-11-14 21:08:08 +00:00
|
|
|
static gint64 sleep_serial;
|
|
|
|
static gint64 sleep_source_prepare_time;
|
|
|
|
static GSource *sleep_source;
|
|
|
|
|
2013-02-16 00:42:32 +00:00
|
|
|
static gboolean
|
2012-11-14 21:08:08 +00:00
|
|
|
sleep_source_prepare (GSource *source,
|
|
|
|
gint *timeout)
|
|
|
|
{
|
|
|
|
sleep_source_prepare_time = g_source_get_time (source);
|
|
|
|
*timeout = -1;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2013-02-16 00:42:32 +00:00
|
|
|
static gboolean
|
2012-11-14 21:08:08 +00:00
|
|
|
sleep_source_check (GSource *source)
|
|
|
|
{
|
|
|
|
if (g_source_get_time (source) != sleep_source_prepare_time)
|
|
|
|
sleep_serial++;
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2013-02-16 00:42:32 +00:00
|
|
|
static gboolean
|
2012-11-14 21:08:08 +00:00
|
|
|
sleep_source_dispatch (GSource *source,
|
|
|
|
GSourceFunc callback,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GSourceFuncs sleep_source_funcs = {
|
|
|
|
sleep_source_prepare,
|
|
|
|
sleep_source_check,
|
|
|
|
sleep_source_dispatch,
|
|
|
|
NULL /* finalize */
|
|
|
|
};
|
|
|
|
|
|
|
|
static gint64
|
|
|
|
get_sleep_serial (void)
|
|
|
|
{
|
|
|
|
if (sleep_source == NULL)
|
|
|
|
{
|
|
|
|
sleep_source = g_source_new (&sleep_source_funcs, sizeof (GSource));
|
|
|
|
|
|
|
|
g_source_set_priority (sleep_source, G_PRIORITY_HIGH);
|
|
|
|
g_source_attach (sleep_source, NULL);
|
|
|
|
g_source_unref (sleep_source);
|
|
|
|
}
|
|
|
|
|
|
|
|
return sleep_serial;
|
|
|
|
}
|
|
|
|
|
2012-10-03 22:34:01 +00:00
|
|
|
static void
|
|
|
|
gdk_frame_clock_idle_init (GdkFrameClockIdle *frame_clock_idle)
|
|
|
|
{
|
|
|
|
GdkFrameClockIdlePrivate *priv;
|
|
|
|
|
2013-06-24 17:54:05 +00:00
|
|
|
frame_clock_idle->priv = priv =
|
|
|
|
gdk_frame_clock_idle_get_instance_private (frame_clock_idle);
|
2012-10-03 22:34:01 +00:00
|
|
|
|
2012-10-03 23:42:13 +00:00
|
|
|
priv->freeze_count = 0;
|
2012-10-03 22:34:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-02-16 03:21:21 +00:00
|
|
|
gdk_frame_clock_idle_dispose (GObject *object)
|
2012-10-03 22:34:01 +00:00
|
|
|
{
|
|
|
|
GdkFrameClockIdlePrivate *priv = GDK_FRAME_CLOCK_IDLE (object)->priv;
|
|
|
|
|
2013-02-16 03:21:21 +00:00
|
|
|
if (priv->flush_idle_id != 0)
|
|
|
|
{
|
|
|
|
g_source_remove (priv->flush_idle_id);
|
|
|
|
priv->flush_idle_id = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (priv->paint_idle_id != 0)
|
|
|
|
{
|
|
|
|
g_source_remove (priv->paint_idle_id);
|
|
|
|
priv->paint_idle_id = 0;
|
|
|
|
}
|
2012-10-03 22:34:01 +00:00
|
|
|
|
2013-02-18 14:37:22 +00:00
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
if (priv->begin_period)
|
|
|
|
{
|
|
|
|
timeEndPeriod(1);
|
|
|
|
priv->begin_period = FALSE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-02-16 03:21:21 +00:00
|
|
|
G_OBJECT_CLASS (gdk_frame_clock_idle_parent_class)->dispose (object);
|
2012-10-03 22:34:01 +00:00
|
|
|
}
|
|
|
|
|
2020-05-18 13:48:03 +00:00
|
|
|
/* Note: This is never called on first frame, so
|
|
|
|
* smoothed_frame_time_base != 0 and we have a valid frame_interval. */
|
2013-02-13 13:35:05 +00:00
|
|
|
static gint64
|
2020-05-18 13:48:03 +00:00
|
|
|
compute_smooth_frame_time (GdkFrameClock *clock,
|
|
|
|
gint64 new_frame_time,
|
|
|
|
gboolean new_frame_time_is_regular,
|
|
|
|
gint64 smoothed_frame_time_base,
|
|
|
|
gint64 frame_interval)
|
2012-10-03 22:34:01 +00:00
|
|
|
{
|
2020-05-18 13:48:03 +00:00
|
|
|
GdkFrameClockIdlePrivate *priv = GDK_FRAME_CLOCK_IDLE (clock)->priv;
|
|
|
|
int frames_passed;
|
|
|
|
gint64 new_smoothed_time;
|
|
|
|
gint64 current_error;
|
|
|
|
gint64 correction_magnitude;
|
|
|
|
|
|
|
|
/* Consecutive frame, assume it is an integer number of frames later, so round to nearest such */
|
|
|
|
/* NOTE: This is >= 0, because smoothed_frame_time_base is < frame_interval/2 from old_frame_time
|
|
|
|
* and new_frame_time >= old_frame_time. */
|
|
|
|
frames_passed = (new_frame_time - smoothed_frame_time_base + frame_interval / 2) / frame_interval;
|
|
|
|
|
|
|
|
/* We use an approximately whole number of frames in the future from
|
|
|
|
* last smoothed frame time. This way we avoid minor jitter in the
|
|
|
|
* frame times making the animation speed uneven, but still animate
|
|
|
|
* evenly in case of whole frame skips. */
|
|
|
|
new_smoothed_time = smoothed_frame_time_base + frames_passed * frame_interval;
|
|
|
|
|
|
|
|
/* However, sometimes the smoothed time is too much off from the
|
|
|
|
* real time. For example, if the first frame clock cycle happened
|
|
|
|
* not due to a frame rendering but an input event, then
|
|
|
|
* new_frame_time could happen to be near the middle between two
|
|
|
|
* frames. If that happens and we then start regularly animating at
|
|
|
|
* the refresh_rate, then the jitter in the real time may cause us
|
|
|
|
* to randomly sometimes round up, and sometimes down.
|
|
|
|
*
|
|
|
|
* To combat this we converge the smooth time towards the real time
|
|
|
|
* in a way that is slow when they are near and fast when they are
|
|
|
|
* far from each other.
|
|
|
|
*
|
|
|
|
* This is done by using the square of the error as the correction
|
|
|
|
* magnitude. I.e. if the error is 0.5 frame, we correct by
|
|
|
|
* 0.5*0.5=0.25 frame, if the error is 0.25 we correct by 0.125, if
|
|
|
|
* the error is 0.1, frame we correct by 0.01 frame, etc.
|
|
|
|
*
|
|
|
|
* The actual computation is:
|
|
|
|
* (current_error/frame_interval)*(current_error/frame_interval)*frame_interval
|
|
|
|
* But this can be simplified as below.
|
|
|
|
*
|
|
|
|
* Note: We only do this correction if we're regularly animating (no
|
|
|
|
* or low frame skip). If the last frame was a long time ago, or if
|
|
|
|
* we're not doing this in the frame cycle this call was likely
|
|
|
|
* triggered by an input event and new_frame_time is essentially
|
|
|
|
* random and not tied to the presentation time.
|
|
|
|
*/
|
|
|
|
if (new_frame_time_is_regular)
|
|
|
|
{
|
|
|
|
current_error = new_smoothed_time - new_frame_time;
|
|
|
|
correction_magnitude = current_error * current_error / frame_interval; /* Note, this is always > 0 due to the square */
|
|
|
|
if (current_error > 0)
|
|
|
|
new_smoothed_time -= correction_magnitude;
|
|
|
|
else
|
|
|
|
new_smoothed_time += correction_magnitude;
|
|
|
|
}
|
2019-04-09 20:26:25 +00:00
|
|
|
|
2020-05-28 15:44:51 +00:00
|
|
|
/* Ensure we're always monotonic */
|
|
|
|
if (new_smoothed_time <= priv->smoothed_frame_time_reported)
|
|
|
|
new_smoothed_time = priv->smoothed_frame_time_reported;
|
2012-10-03 22:34:01 +00:00
|
|
|
|
2020-05-18 13:48:03 +00:00
|
|
|
return new_smoothed_time;
|
2012-10-03 22:34:01 +00:00
|
|
|
}
|
|
|
|
|
2013-02-13 13:35:05 +00:00
|
|
|
static gint64
|
2012-10-03 22:34:01 +00:00
|
|
|
gdk_frame_clock_idle_get_frame_time (GdkFrameClock *clock)
|
|
|
|
{
|
|
|
|
GdkFrameClockIdlePrivate *priv = GDK_FRAME_CLOCK_IDLE (clock)->priv;
|
2020-05-18 13:48:03 +00:00
|
|
|
gint64 now;
|
2020-05-28 15:44:51 +00:00
|
|
|
gint64 new_smoothed_time;
|
2012-10-03 22:34:01 +00:00
|
|
|
|
|
|
|
/* can't change frame time during a paint */
|
2012-09-27 21:55:55 +00:00
|
|
|
if (priv->phase != GDK_FRAME_CLOCK_PHASE_NONE &&
|
2020-06-01 08:53:24 +00:00
|
|
|
priv->phase != GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS &&
|
|
|
|
(priv->phase != GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT || priv->in_paint_idle))
|
2020-05-18 13:48:03 +00:00
|
|
|
return priv->smoothed_frame_time_base;
|
2012-10-03 22:34:01 +00:00
|
|
|
|
2020-05-18 13:48:03 +00:00
|
|
|
/* Outside a paint, pick something smoothed close to now */
|
|
|
|
now = g_get_monotonic_time ();
|
2012-10-03 22:34:01 +00:00
|
|
|
|
2020-05-18 13:48:03 +00:00
|
|
|
/* First time frame, just return something */
|
|
|
|
if (priv->smoothed_frame_time_base == 0)
|
2020-05-28 15:44:51 +00:00
|
|
|
{
|
|
|
|
priv->smoothed_frame_time_reported = now;
|
|
|
|
return now;
|
|
|
|
}
|
2012-10-03 22:34:01 +00:00
|
|
|
|
2020-05-18 13:48:03 +00:00
|
|
|
/* Since time is monotonic this is <= what we will pick for the next cycle, but
|
|
|
|
more likely than not it will be equal if we're doing a constant animation. */
|
2020-05-28 15:44:51 +00:00
|
|
|
new_smoothed_time = compute_smooth_frame_time (clock, now, FALSE,
|
|
|
|
priv->smoothed_frame_time_base,
|
|
|
|
priv->smoothed_frame_time_period);
|
|
|
|
|
|
|
|
priv->smoothed_frame_time_reported = new_smoothed_time;
|
|
|
|
return new_smoothed_time;
|
2012-10-03 22:34:01 +00:00
|
|
|
}
|
|
|
|
|
2013-02-15 22:04:39 +00:00
|
|
|
#define RUN_FLUSH_IDLE(priv) \
|
|
|
|
((priv)->freeze_count == 0 && \
|
|
|
|
((priv)->requested & GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS) != 0)
|
|
|
|
|
|
|
|
/* The reason why we track updating_count separately here and don't
|
|
|
|
* just add GDK_FRAME_CLOCK_PHASE_UPDATE into ->request on every frame
|
|
|
|
* is so that we can avoid doing one more frame when an animation
|
|
|
|
* is cancelled.
|
|
|
|
*/
|
|
|
|
#define RUN_PAINT_IDLE(priv) \
|
|
|
|
((priv)->freeze_count == 0 && \
|
|
|
|
(((priv)->requested & ~GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS) != 0 || \
|
|
|
|
(priv)->updating_count > 0))
|
|
|
|
|
2012-10-03 23:38:40 +00:00
|
|
|
static void
|
2020-06-05 09:16:57 +00:00
|
|
|
maybe_start_idle (GdkFrameClockIdle *clock_idle,
|
|
|
|
gboolean caused_by_thaw)
|
2012-10-03 23:38:40 +00:00
|
|
|
{
|
|
|
|
GdkFrameClockIdlePrivate *priv = clock_idle->priv;
|
|
|
|
|
2013-02-15 22:04:39 +00:00
|
|
|
if (RUN_FLUSH_IDLE (priv) || RUN_PAINT_IDLE (priv))
|
2012-10-03 23:38:40 +00:00
|
|
|
{
|
2012-09-27 21:55:55 +00:00
|
|
|
guint min_interval = 0;
|
|
|
|
|
|
|
|
if (priv->min_next_frame_time != 0)
|
|
|
|
{
|
2020-05-18 13:48:03 +00:00
|
|
|
gint64 now = g_get_monotonic_time ();
|
2013-02-13 13:35:05 +00:00
|
|
|
gint64 min_interval_us = MAX (priv->min_next_frame_time, now) - now;
|
2012-09-27 21:55:55 +00:00
|
|
|
min_interval = (min_interval_us + 500) / 1000;
|
|
|
|
}
|
|
|
|
|
2013-02-15 22:04:39 +00:00
|
|
|
if (priv->flush_idle_id == 0 && RUN_FLUSH_IDLE (priv))
|
2012-09-26 14:28:06 +00:00
|
|
|
{
|
|
|
|
priv->flush_idle_id = gdk_threads_add_timeout_full (GDK_PRIORITY_EVENTS + 1,
|
|
|
|
min_interval,
|
|
|
|
gdk_frame_clock_flush_idle,
|
|
|
|
g_object_ref (clock_idle),
|
|
|
|
(GDestroyNotify) g_object_unref);
|
2013-10-22 13:43:43 +00:00
|
|
|
g_source_set_name_by_id (priv->flush_idle_id, "[gtk+] gdk_frame_clock_flush_idle");
|
2012-09-26 14:28:06 +00:00
|
|
|
}
|
|
|
|
|
2013-02-19 20:03:35 +00:00
|
|
|
if (!priv->in_paint_idle &&
|
|
|
|
priv->paint_idle_id == 0 && RUN_PAINT_IDLE (priv))
|
2012-09-26 14:28:06 +00:00
|
|
|
{
|
2020-06-05 09:16:57 +00:00
|
|
|
priv->paint_is_thaw = caused_by_thaw;
|
2012-09-26 14:28:06 +00:00
|
|
|
priv->paint_idle_id = gdk_threads_add_timeout_full (GDK_PRIORITY_REDRAW,
|
|
|
|
min_interval,
|
|
|
|
gdk_frame_clock_paint_idle,
|
|
|
|
g_object_ref (clock_idle),
|
|
|
|
(GDestroyNotify) g_object_unref);
|
2013-10-22 13:43:43 +00:00
|
|
|
g_source_set_name_by_id (priv->paint_idle_id, "[gtk+] gdk_frame_clock_paint_idle");
|
2012-09-26 14:28:06 +00:00
|
|
|
}
|
2012-10-03 23:38:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-15 22:04:39 +00:00
|
|
|
static void
|
|
|
|
maybe_stop_idle (GdkFrameClockIdle *clock_idle)
|
|
|
|
{
|
|
|
|
GdkFrameClockIdlePrivate *priv = clock_idle->priv;
|
|
|
|
|
|
|
|
if (priv->flush_idle_id != 0 && !RUN_FLUSH_IDLE (priv))
|
|
|
|
{
|
|
|
|
g_source_remove (priv->flush_idle_id);
|
|
|
|
priv->flush_idle_id = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (priv->paint_idle_id != 0 && !RUN_PAINT_IDLE (priv))
|
|
|
|
{
|
|
|
|
g_source_remove (priv->paint_idle_id);
|
|
|
|
priv->paint_idle_id = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-15 19:11:41 +00:00
|
|
|
static gint64
|
|
|
|
compute_min_next_frame_time (GdkFrameClockIdle *clock_idle,
|
|
|
|
gint64 last_frame_time)
|
|
|
|
{
|
|
|
|
gint64 presentation_time;
|
|
|
|
gint64 refresh_interval;
|
|
|
|
|
|
|
|
gdk_frame_clock_get_refresh_info (GDK_FRAME_CLOCK (clock_idle),
|
|
|
|
last_frame_time,
|
|
|
|
&refresh_interval, &presentation_time);
|
|
|
|
|
|
|
|
if (presentation_time == 0)
|
|
|
|
return last_frame_time + refresh_interval;
|
|
|
|
else
|
|
|
|
return presentation_time + refresh_interval / 2;
|
|
|
|
}
|
|
|
|
|
2012-09-26 14:28:06 +00:00
|
|
|
static gboolean
|
|
|
|
gdk_frame_clock_flush_idle (void *data)
|
|
|
|
{
|
|
|
|
GdkFrameClock *clock = GDK_FRAME_CLOCK (data);
|
|
|
|
GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
|
|
|
|
GdkFrameClockIdlePrivate *priv = clock_idle->priv;
|
|
|
|
|
|
|
|
priv->flush_idle_id = 0;
|
|
|
|
|
|
|
|
if (priv->phase != GDK_FRAME_CLOCK_PHASE_NONE)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
priv->phase = GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS;
|
|
|
|
priv->requested &= ~GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS;
|
|
|
|
|
2016-04-18 21:16:20 +00:00
|
|
|
_gdk_frame_clock_emit_flush_events (clock);
|
2012-09-26 14:28:06 +00:00
|
|
|
|
2013-02-15 22:04:39 +00:00
|
|
|
if ((priv->requested & ~GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS) != 0 ||
|
|
|
|
priv->updating_count > 0)
|
2012-09-26 14:28:06 +00:00
|
|
|
priv->phase = GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT;
|
|
|
|
else
|
|
|
|
priv->phase = GDK_FRAME_CLOCK_PHASE_NONE;
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2012-10-03 22:34:01 +00:00
|
|
|
static gboolean
|
|
|
|
gdk_frame_clock_paint_idle (void *data)
|
|
|
|
{
|
|
|
|
GdkFrameClock *clock = GDK_FRAME_CLOCK (data);
|
|
|
|
GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
|
|
|
|
GdkFrameClockIdlePrivate *priv = clock_idle->priv;
|
2012-09-26 14:28:06 +00:00
|
|
|
gboolean skip_to_resume_events;
|
2012-11-15 00:21:33 +00:00
|
|
|
GdkFrameTimings *timings = NULL;
|
2012-10-03 22:34:01 +00:00
|
|
|
|
2012-09-26 14:28:06 +00:00
|
|
|
priv->paint_idle_id = 0;
|
2012-11-14 18:26:13 +00:00
|
|
|
priv->in_paint_idle = TRUE;
|
2012-11-15 19:11:41 +00:00
|
|
|
priv->min_next_frame_time = 0;
|
2012-09-26 14:28:06 +00:00
|
|
|
|
|
|
|
skip_to_resume_events =
|
2013-02-15 22:04:39 +00:00
|
|
|
(priv->requested & ~(GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS | GDK_FRAME_CLOCK_PHASE_RESUME_EVENTS)) == 0 &&
|
|
|
|
priv->updating_count == 0;
|
2012-10-03 22:34:01 +00:00
|
|
|
|
2012-11-15 00:21:33 +00:00
|
|
|
if (priv->phase > GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT)
|
|
|
|
{
|
2013-02-12 20:03:21 +00:00
|
|
|
timings = gdk_frame_clock_get_current_timings (clock);
|
2012-11-15 00:21:33 +00:00
|
|
|
}
|
|
|
|
|
2012-10-07 15:42:45 +00:00
|
|
|
if (!skip_to_resume_events)
|
2012-10-03 23:42:13 +00:00
|
|
|
{
|
2012-10-07 15:42:45 +00:00
|
|
|
switch (priv->phase)
|
|
|
|
{
|
|
|
|
case GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS:
|
|
|
|
break;
|
|
|
|
case GDK_FRAME_CLOCK_PHASE_NONE:
|
|
|
|
case GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT:
|
|
|
|
if (priv->freeze_count == 0)
|
2012-09-26 14:28:06 +00:00
|
|
|
{
|
2017-09-15 09:49:12 +00:00
|
|
|
gint64 frame_interval = FRAME_INTERVAL;
|
2020-05-18 13:48:03 +00:00
|
|
|
GdkFrameTimings *prev_timings = gdk_frame_clock_get_current_timings (clock);
|
|
|
|
gint64 old_frame_time = priv->frame_time;
|
2017-09-15 09:49:12 +00:00
|
|
|
|
|
|
|
if (prev_timings && prev_timings->refresh_interval)
|
|
|
|
frame_interval = prev_timings->refresh_interval;
|
|
|
|
|
2020-05-18 13:48:03 +00:00
|
|
|
priv->frame_time = g_get_monotonic_time ();
|
|
|
|
|
|
|
|
if (priv->smoothed_frame_time_base == 0)
|
|
|
|
{
|
|
|
|
/* First frame */
|
|
|
|
priv->smoothed_frame_time_base = priv->frame_time;
|
|
|
|
priv->smoothed_frame_time_period = frame_interval;
|
|
|
|
}
|
2017-09-15 09:49:12 +00:00
|
|
|
else
|
2020-05-18 13:48:03 +00:00
|
|
|
{
|
|
|
|
/* For long delays, cycle was probably caused by input event rather than animation */
|
|
|
|
gboolean is_regular = priv->frame_time - old_frame_time < 4 * FRAME_INTERVAL;
|
|
|
|
priv->smoothed_frame_time_base =
|
|
|
|
compute_smooth_frame_time (clock, priv->frame_time,
|
|
|
|
is_regular,
|
|
|
|
priv->smoothed_frame_time_base,
|
|
|
|
priv->smoothed_frame_time_period);
|
|
|
|
priv->smoothed_frame_time_period = frame_interval;
|
|
|
|
}
|
2020-05-28 15:44:51 +00:00
|
|
|
priv->smoothed_frame_time_reported = priv->smoothed_frame_time_base;
|
2012-11-15 00:21:33 +00:00
|
|
|
|
2013-02-12 20:47:38 +00:00
|
|
|
_gdk_frame_clock_begin_frame (clock);
|
2017-09-15 09:49:12 +00:00
|
|
|
/* Note "current" is different now so timings != prev_timings */
|
2013-02-12 20:03:21 +00:00
|
|
|
timings = gdk_frame_clock_get_current_timings (clock);
|
2012-11-15 00:21:33 +00:00
|
|
|
|
2013-02-12 21:14:24 +00:00
|
|
|
timings->frame_time = priv->frame_time;
|
2020-05-18 13:48:03 +00:00
|
|
|
timings->smoothed_frame_time = priv->smoothed_frame_time_base;
|
2013-02-12 21:14:24 +00:00
|
|
|
timings->slept_before = priv->sleep_serial != get_sleep_serial ();
|
2012-11-14 21:08:08 +00:00
|
|
|
|
2012-10-07 15:42:45 +00:00
|
|
|
priv->phase = GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT;
|
|
|
|
|
|
|
|
/* We always emit ::before-paint and ::after-paint if
|
|
|
|
* any of the intermediate phases are requested and
|
|
|
|
* they don't get repeated if you freeze/thaw while
|
2013-02-25 09:42:42 +00:00
|
|
|
* in them.
|
|
|
|
*/
|
2012-09-26 14:28:06 +00:00
|
|
|
priv->requested &= ~GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT;
|
2016-04-18 21:16:20 +00:00
|
|
|
_gdk_frame_clock_emit_before_paint (clock);
|
2012-10-07 15:42:45 +00:00
|
|
|
priv->phase = GDK_FRAME_CLOCK_PHASE_UPDATE;
|
2012-09-26 14:28:06 +00:00
|
|
|
}
|
2013-09-03 20:58:12 +00:00
|
|
|
/* fallthrough */
|
2012-10-07 15:42:45 +00:00
|
|
|
case GDK_FRAME_CLOCK_PHASE_UPDATE:
|
|
|
|
if (priv->freeze_count == 0)
|
2012-09-26 19:44:30 +00:00
|
|
|
{
|
2013-02-15 22:04:39 +00:00
|
|
|
if ((priv->requested & GDK_FRAME_CLOCK_PHASE_UPDATE) != 0 ||
|
|
|
|
priv->updating_count > 0)
|
2012-10-07 15:42:45 +00:00
|
|
|
{
|
|
|
|
priv->requested &= ~GDK_FRAME_CLOCK_PHASE_UPDATE;
|
2016-04-18 21:16:20 +00:00
|
|
|
_gdk_frame_clock_emit_update (clock);
|
2012-10-07 15:42:45 +00:00
|
|
|
}
|
2012-09-26 19:44:30 +00:00
|
|
|
}
|
2013-09-03 20:58:12 +00:00
|
|
|
/* fallthrough */
|
2012-10-07 15:42:45 +00:00
|
|
|
case GDK_FRAME_CLOCK_PHASE_LAYOUT:
|
|
|
|
if (priv->freeze_count == 0)
|
2012-10-03 23:42:13 +00:00
|
|
|
{
|
2013-04-24 20:21:06 +00:00
|
|
|
int iter;
|
2012-11-15 00:21:33 +00:00
|
|
|
#ifdef G_ENABLE_DEBUG
|
2019-06-06 23:19:36 +00:00
|
|
|
if (GDK_DEBUG_CHECK (FRAMES) || gdk_profiler_is_running ())
|
2012-11-15 00:21:33 +00:00
|
|
|
{
|
|
|
|
if (priv->phase != GDK_FRAME_CLOCK_PHASE_LAYOUT &&
|
|
|
|
(priv->requested & GDK_FRAME_CLOCK_PHASE_LAYOUT))
|
2013-02-12 21:14:24 +00:00
|
|
|
timings->layout_start_time = g_get_monotonic_time ();
|
2012-11-15 00:21:33 +00:00
|
|
|
}
|
|
|
|
#endif /* G_ENABLE_DEBUG */
|
|
|
|
|
2012-10-07 15:42:45 +00:00
|
|
|
priv->phase = GDK_FRAME_CLOCK_PHASE_LAYOUT;
|
2013-04-24 20:21:06 +00:00
|
|
|
/* We loop in the layout phase, because we don't want to progress
|
|
|
|
* into the paint phase with invalid size allocations. This may
|
|
|
|
* happen in some situation like races between user window
|
|
|
|
* resizes and natural size changes.
|
|
|
|
*/
|
|
|
|
iter = 0;
|
|
|
|
while ((priv->requested & GDK_FRAME_CLOCK_PHASE_LAYOUT) &&
|
|
|
|
priv->freeze_count == 0 && iter++ < 4)
|
2012-10-07 15:42:45 +00:00
|
|
|
{
|
|
|
|
priv->requested &= ~GDK_FRAME_CLOCK_PHASE_LAYOUT;
|
2016-04-18 21:16:20 +00:00
|
|
|
_gdk_frame_clock_emit_layout (clock);
|
2012-10-07 15:42:45 +00:00
|
|
|
}
|
2013-04-24 20:21:06 +00:00
|
|
|
if (iter == 5)
|
|
|
|
g_warning ("gdk-frame-clock: layout continuously requested, giving up after 4 tries");
|
2012-10-03 23:42:13 +00:00
|
|
|
}
|
2013-09-03 20:58:12 +00:00
|
|
|
/* fallthrough */
|
2012-10-07 15:42:45 +00:00
|
|
|
case GDK_FRAME_CLOCK_PHASE_PAINT:
|
|
|
|
if (priv->freeze_count == 0)
|
2012-10-03 23:42:13 +00:00
|
|
|
{
|
2012-11-15 00:21:33 +00:00
|
|
|
#ifdef G_ENABLE_DEBUG
|
2019-06-06 23:19:36 +00:00
|
|
|
if (GDK_DEBUG_CHECK (FRAMES) || gdk_profiler_is_running ())
|
2012-11-15 00:21:33 +00:00
|
|
|
{
|
|
|
|
if (priv->phase != GDK_FRAME_CLOCK_PHASE_PAINT &&
|
|
|
|
(priv->requested & GDK_FRAME_CLOCK_PHASE_PAINT))
|
2013-02-12 21:14:24 +00:00
|
|
|
timings->paint_start_time = g_get_monotonic_time ();
|
2012-11-15 00:21:33 +00:00
|
|
|
}
|
|
|
|
#endif /* G_ENABLE_DEBUG */
|
|
|
|
|
2012-10-07 15:42:45 +00:00
|
|
|
priv->phase = GDK_FRAME_CLOCK_PHASE_PAINT;
|
|
|
|
if (priv->requested & GDK_FRAME_CLOCK_PHASE_PAINT)
|
|
|
|
{
|
|
|
|
priv->requested &= ~GDK_FRAME_CLOCK_PHASE_PAINT;
|
2016-04-18 21:16:20 +00:00
|
|
|
_gdk_frame_clock_emit_paint (clock);
|
2012-10-07 15:42:45 +00:00
|
|
|
}
|
2012-10-03 23:42:13 +00:00
|
|
|
}
|
2013-09-03 20:58:12 +00:00
|
|
|
/* fallthrough */
|
2012-10-07 15:42:45 +00:00
|
|
|
case GDK_FRAME_CLOCK_PHASE_AFTER_PAINT:
|
|
|
|
if (priv->freeze_count == 0)
|
2012-09-26 14:28:06 +00:00
|
|
|
{
|
|
|
|
priv->requested &= ~GDK_FRAME_CLOCK_PHASE_AFTER_PAINT;
|
2016-04-18 21:16:20 +00:00
|
|
|
_gdk_frame_clock_emit_after_paint (clock);
|
2012-10-07 15:42:45 +00:00
|
|
|
/* the ::after-paint phase doesn't get repeated on freeze/thaw,
|
|
|
|
*/
|
|
|
|
priv->phase = GDK_FRAME_CLOCK_PHASE_NONE;
|
2012-11-15 00:21:33 +00:00
|
|
|
|
|
|
|
#ifdef G_ENABLE_DEBUG
|
2019-06-06 23:19:36 +00:00
|
|
|
if (GDK_DEBUG_CHECK (FRAMES) || gdk_profiler_is_running ())
|
2013-02-12 21:14:24 +00:00
|
|
|
timings->frame_end_time = g_get_monotonic_time ();
|
2012-11-15 00:21:33 +00:00
|
|
|
#endif /* G_ENABLE_DEBUG */
|
2012-09-26 14:28:06 +00:00
|
|
|
}
|
2013-09-03 20:58:12 +00:00
|
|
|
/* fallthrough */
|
2012-10-07 15:42:45 +00:00
|
|
|
case GDK_FRAME_CLOCK_PHASE_RESUME_EVENTS:
|
|
|
|
;
|
|
|
|
}
|
2012-10-03 23:42:13 +00:00
|
|
|
}
|
2012-10-03 23:38:40 +00:00
|
|
|
|
2012-11-15 00:21:33 +00:00
|
|
|
#ifdef G_ENABLE_DEBUG
|
2016-02-29 02:29:35 +00:00
|
|
|
if (GDK_DEBUG_CHECK (FRAMES))
|
2012-11-15 00:21:33 +00:00
|
|
|
{
|
2013-02-26 11:06:45 +00:00
|
|
|
if (timings && timings->complete)
|
2013-02-12 20:47:38 +00:00
|
|
|
_gdk_frame_clock_debug_print_timings (clock, timings);
|
2012-11-15 00:21:33 +00:00
|
|
|
}
|
|
|
|
#endif /* G_ENABLE_DEBUG */
|
|
|
|
|
2012-10-07 15:42:45 +00:00
|
|
|
if (priv->requested & GDK_FRAME_CLOCK_PHASE_RESUME_EVENTS)
|
|
|
|
{
|
|
|
|
priv->requested &= ~GDK_FRAME_CLOCK_PHASE_RESUME_EVENTS;
|
2016-04-18 21:16:20 +00:00
|
|
|
_gdk_frame_clock_emit_resume_events (clock);
|
2012-10-07 15:42:45 +00:00
|
|
|
}
|
|
|
|
|
2012-11-14 18:26:13 +00:00
|
|
|
if (priv->freeze_count == 0)
|
|
|
|
priv->phase = GDK_FRAME_CLOCK_PHASE_NONE;
|
|
|
|
|
|
|
|
priv->in_paint_idle = FALSE;
|
2012-10-07 15:42:45 +00:00
|
|
|
|
2012-11-15 19:11:41 +00:00
|
|
|
/* If there is throttling in the backend layer, then we'll do another
|
|
|
|
* update as soon as the backend unthrottles (if there is work to do),
|
|
|
|
* otherwise we need to figure when the next frame should be.
|
|
|
|
*/
|
|
|
|
if (priv->freeze_count == 0)
|
2012-09-27 21:55:55 +00:00
|
|
|
{
|
2012-11-15 19:11:41 +00:00
|
|
|
priv->min_next_frame_time = compute_min_next_frame_time (clock_idle,
|
|
|
|
priv->frame_time);
|
2020-06-05 09:16:57 +00:00
|
|
|
maybe_start_idle (clock_idle, FALSE);
|
2012-09-27 21:55:55 +00:00
|
|
|
}
|
2012-10-03 22:34:01 +00:00
|
|
|
|
2012-11-14 21:08:08 +00:00
|
|
|
if (priv->freeze_count == 0)
|
|
|
|
priv->sleep_serial = get_sleep_serial ();
|
|
|
|
|
2012-10-03 22:34:01 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-10-03 23:38:40 +00:00
|
|
|
gdk_frame_clock_idle_request_phase (GdkFrameClock *clock,
|
|
|
|
GdkFrameClockPhase phase)
|
2012-10-03 22:34:01 +00:00
|
|
|
{
|
2012-10-03 23:38:40 +00:00
|
|
|
GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
|
|
|
|
GdkFrameClockIdlePrivate *priv = clock_idle->priv;
|
2012-10-03 22:34:01 +00:00
|
|
|
|
2012-10-03 23:38:40 +00:00
|
|
|
priv->requested |= phase;
|
2020-06-05 09:16:57 +00:00
|
|
|
maybe_start_idle (clock_idle, FALSE);
|
2012-10-03 22:34:01 +00:00
|
|
|
}
|
|
|
|
|
2013-02-15 22:04:39 +00:00
|
|
|
static void
|
|
|
|
gdk_frame_clock_idle_begin_updating (GdkFrameClock *clock)
|
|
|
|
{
|
|
|
|
GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
|
|
|
|
GdkFrameClockIdlePrivate *priv = clock_idle->priv;
|
|
|
|
|
2013-02-18 14:37:22 +00:00
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
/* We need a higher resolution timer while doing animations */
|
|
|
|
if (priv->updating_count == 0 && !priv->begin_period)
|
|
|
|
{
|
|
|
|
timeBeginPeriod(1);
|
|
|
|
priv->begin_period = TRUE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-02-15 22:04:39 +00:00
|
|
|
priv->updating_count++;
|
2020-06-05 09:16:57 +00:00
|
|
|
maybe_start_idle (clock_idle, FALSE);
|
2013-02-15 22:04:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gdk_frame_clock_idle_end_updating (GdkFrameClock *clock)
|
|
|
|
{
|
|
|
|
GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
|
|
|
|
GdkFrameClockIdlePrivate *priv = clock_idle->priv;
|
|
|
|
|
|
|
|
g_return_if_fail (priv->updating_count > 0);
|
|
|
|
|
|
|
|
priv->updating_count--;
|
|
|
|
maybe_stop_idle (clock_idle);
|
2013-02-18 14:37:22 +00:00
|
|
|
|
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
if (priv->updating_count == 0 && priv->begin_period)
|
|
|
|
{
|
|
|
|
timeEndPeriod(1);
|
|
|
|
priv->begin_period = FALSE;
|
|
|
|
}
|
|
|
|
#endif
|
2013-02-15 22:04:39 +00:00
|
|
|
}
|
|
|
|
|
2012-10-03 23:42:13 +00:00
|
|
|
static void
|
|
|
|
gdk_frame_clock_idle_freeze (GdkFrameClock *clock)
|
|
|
|
{
|
2013-02-15 22:04:39 +00:00
|
|
|
GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
|
|
|
|
GdkFrameClockIdlePrivate *priv = clock_idle->priv;
|
2012-10-03 23:42:13 +00:00
|
|
|
|
2019-06-06 23:19:36 +00:00
|
|
|
#ifdef G_ENABLE_DEBUG
|
|
|
|
if (priv->freeze_count == 0)
|
|
|
|
{
|
|
|
|
if (gdk_profiler_is_running ())
|
|
|
|
priv->freeze_time = g_get_monotonic_time ();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-10-03 23:42:13 +00:00
|
|
|
priv->freeze_count++;
|
2013-02-15 22:04:39 +00:00
|
|
|
maybe_stop_idle (clock_idle);
|
2012-10-03 23:42:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gdk_frame_clock_idle_thaw (GdkFrameClock *clock)
|
|
|
|
{
|
|
|
|
GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
|
|
|
|
GdkFrameClockIdlePrivate *priv = clock_idle->priv;
|
|
|
|
|
|
|
|
g_return_if_fail (priv->freeze_count > 0);
|
|
|
|
|
|
|
|
priv->freeze_count--;
|
|
|
|
if (priv->freeze_count == 0)
|
2012-09-26 14:28:06 +00:00
|
|
|
{
|
2020-06-05 09:16:57 +00:00
|
|
|
maybe_start_idle (clock_idle, TRUE);
|
2012-09-26 14:28:06 +00:00
|
|
|
/* If nothing is requested so we didn't start an idle, we need
|
|
|
|
* to skip to the end of the state chain, since the idle won't
|
2013-02-25 09:42:42 +00:00
|
|
|
* run and do it for us.
|
|
|
|
*/
|
2012-09-26 14:28:06 +00:00
|
|
|
if (priv->paint_idle_id == 0)
|
|
|
|
priv->phase = GDK_FRAME_CLOCK_PHASE_NONE;
|
2012-11-14 21:08:08 +00:00
|
|
|
|
|
|
|
priv->sleep_serial = get_sleep_serial ();
|
2019-06-06 23:19:36 +00:00
|
|
|
|
|
|
|
#ifdef G_ENABLE_DEBUG
|
|
|
|
if (gdk_profiler_is_running ())
|
|
|
|
{
|
|
|
|
if (priv->freeze_time != 0)
|
|
|
|
{
|
|
|
|
gint64 thaw_time = g_get_monotonic_time ();
|
|
|
|
gdk_profiler_add_mark (priv->freeze_time * 1000,
|
|
|
|
(thaw_time - priv->freeze_time) * 1000,
|
|
|
|
"freeze", "");
|
|
|
|
priv->freeze_time = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2012-09-26 14:28:06 +00:00
|
|
|
}
|
2012-10-03 23:42:13 +00:00
|
|
|
}
|
|
|
|
|
2013-02-12 20:02:21 +00:00
|
|
|
static void
|
|
|
|
gdk_frame_clock_idle_class_init (GdkFrameClockIdleClass *klass)
|
2012-11-14 17:49:06 +00:00
|
|
|
{
|
2013-02-12 20:02:21 +00:00
|
|
|
GObjectClass *gobject_class = (GObjectClass*) klass;
|
|
|
|
GdkFrameClockClass *frame_clock_class = (GdkFrameClockClass *)klass;
|
2012-11-14 17:49:06 +00:00
|
|
|
|
2013-02-16 03:21:21 +00:00
|
|
|
gobject_class->dispose = gdk_frame_clock_idle_dispose;
|
2012-11-14 17:49:06 +00:00
|
|
|
|
2013-02-12 20:02:21 +00:00
|
|
|
frame_clock_class->get_frame_time = gdk_frame_clock_idle_get_frame_time;
|
|
|
|
frame_clock_class->request_phase = gdk_frame_clock_idle_request_phase;
|
2013-02-15 22:04:39 +00:00
|
|
|
frame_clock_class->begin_updating = gdk_frame_clock_idle_begin_updating;
|
|
|
|
frame_clock_class->end_updating = gdk_frame_clock_idle_end_updating;
|
2013-02-12 20:02:21 +00:00
|
|
|
frame_clock_class->freeze = gdk_frame_clock_idle_freeze;
|
|
|
|
frame_clock_class->thaw = gdk_frame_clock_idle_thaw;
|
2012-10-03 23:38:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GdkFrameClock *
|
|
|
|
_gdk_frame_clock_idle_new (void)
|
|
|
|
{
|
|
|
|
GdkFrameClockIdle *clock;
|
|
|
|
|
|
|
|
clock = g_object_new (GDK_TYPE_FRAME_CLOCK_IDLE, NULL);
|
|
|
|
|
|
|
|
return GDK_FRAME_CLOCK (clock);
|
2012-10-03 22:34:01 +00:00
|
|
|
}
|