2009-03-21 16:39:32 -04:00
|
|
|
/*
|
|
|
|
* Clutter.
|
|
|
|
*
|
|
|
|
* An OpenGL based 'interactive canvas' library.
|
|
|
|
*
|
|
|
|
* Authored By: Emmanuele Bassi <ebassi@linux.intel.com>
|
|
|
|
*
|
|
|
|
* Copyright (C) 2009 Intel Corporation.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SECTION:clutter-master-clock
|
|
|
|
* @short_description: The master clock for all animations
|
|
|
|
*
|
|
|
|
* The #ClutterMasterClock class is responsible for advancing all
|
|
|
|
* #ClutterTimelines when a stage is being redrawn. The master clock
|
|
|
|
* makes sure that the scenegraph is always integrally updated before
|
|
|
|
* painting it.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "clutter-master-clock.h"
|
|
|
|
#include "clutter-debug.h"
|
|
|
|
#include "clutter-private.h"
|
2010-01-14 07:28:07 -05:00
|
|
|
#include "clutter-profile.h"
|
2010-10-21 07:16:05 -04:00
|
|
|
#include "clutter-stage-manager-private.h"
|
2010-10-21 06:29:09 -04:00
|
|
|
#include "clutter-stage-private.h"
|
2009-03-21 16:39:32 -04:00
|
|
|
|
|
|
|
#define CLUTTER_MASTER_CLOCK_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), CLUTTER_TYPE_MASTER_CLOCK, ClutterMasterClockClass))
|
|
|
|
#define CLUTTER_IS_MASTER_CLOCK_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), CLUTTER_TYPE_MASTER_CLOCK))
|
|
|
|
#define CLUTTER_MASTER_CLASS_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), CLUTTER_TYPE_MASTER_CLOCK, ClutterMasterClockClass))
|
|
|
|
|
2009-05-07 14:07:21 -04:00
|
|
|
typedef struct _ClutterClockSource ClutterClockSource;
|
2009-03-21 16:39:32 -04:00
|
|
|
typedef struct _ClutterMasterClockClass ClutterMasterClockClass;
|
|
|
|
|
|
|
|
struct _ClutterMasterClock
|
|
|
|
{
|
|
|
|
GObject parent_instance;
|
|
|
|
|
2009-05-07 14:07:21 -04:00
|
|
|
/* the list of timelines handled by the clock */
|
2009-03-21 16:39:32 -04:00
|
|
|
GSList *timelines;
|
|
|
|
|
2010-11-07 10:57:33 -05:00
|
|
|
/* the current state of the clock, in usecs */
|
|
|
|
gint64 cur_tick;
|
2009-06-06 22:48:15 -04:00
|
|
|
|
2010-11-07 10:57:33 -05:00
|
|
|
/* the previous state of the clock, in usecs, used to compute the delta */
|
|
|
|
gint64 prev_tick;
|
2009-03-21 16:39:32 -04:00
|
|
|
|
2009-05-07 14:07:21 -04:00
|
|
|
/* an idle source, used by the Master Clock to queue
|
|
|
|
* a redraw on the stage and drive the animations
|
|
|
|
*/
|
|
|
|
GSource *source;
|
2009-06-10 10:50:27 -04:00
|
|
|
|
2010-11-07 10:57:33 -05:00
|
|
|
/* If the master clock is idle that means it has
|
2009-11-12 15:37:01 -05:00
|
|
|
* fallen back to idle polling for timeline
|
|
|
|
* progressions and it may have been some time since
|
|
|
|
* the last real stage update.
|
|
|
|
*/
|
|
|
|
guint idle : 1;
|
2009-07-09 15:03:01 -04:00
|
|
|
guint ensure_next_iteration : 1;
|
2009-03-21 16:39:32 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
struct _ClutterMasterClockClass
|
|
|
|
{
|
|
|
|
GObjectClass parent_class;
|
|
|
|
};
|
|
|
|
|
2009-05-07 14:07:21 -04:00
|
|
|
struct _ClutterClockSource
|
|
|
|
{
|
|
|
|
GSource source;
|
|
|
|
|
|
|
|
ClutterMasterClock *master_clock;
|
|
|
|
};
|
2009-03-21 16:39:32 -04:00
|
|
|
|
2009-05-07 14:07:21 -04:00
|
|
|
static gboolean clutter_clock_prepare (GSource *source,
|
|
|
|
gint *timeout);
|
|
|
|
static gboolean clutter_clock_check (GSource *source);
|
|
|
|
static gboolean clutter_clock_dispatch (GSource *source,
|
|
|
|
GSourceFunc callback,
|
|
|
|
gpointer user_data);
|
|
|
|
|
|
|
|
static ClutterMasterClock *default_clock = NULL;
|
|
|
|
|
|
|
|
static GSourceFuncs clock_funcs = {
|
|
|
|
clutter_clock_prepare,
|
|
|
|
clutter_clock_check,
|
|
|
|
clutter_clock_dispatch,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2009-03-21 16:39:32 -04:00
|
|
|
G_DEFINE_TYPE (ClutterMasterClock, clutter_master_clock, G_TYPE_OBJECT);
|
|
|
|
|
2009-05-29 09:25:19 -04:00
|
|
|
/*
|
2009-06-06 18:22:51 -04:00
|
|
|
* master_clock_is_running:
|
2009-05-29 09:25:19 -04:00
|
|
|
* @master_clock: a #ClutterMasterClock
|
|
|
|
*
|
2009-06-06 18:22:51 -04:00
|
|
|
* Checks if we should currently be advancing timelines or redrawing
|
|
|
|
* stages.
|
2009-05-29 09:25:19 -04:00
|
|
|
*
|
|
|
|
* Return value: %TRUE if the #ClutterMasterClock has at least
|
|
|
|
* one running timeline
|
|
|
|
*/
|
2009-05-07 14:07:21 -04:00
|
|
|
static gboolean
|
2009-06-06 18:22:51 -04:00
|
|
|
master_clock_is_running (ClutterMasterClock *master_clock)
|
2009-03-21 16:39:32 -04:00
|
|
|
{
|
2009-06-06 17:44:40 -04:00
|
|
|
ClutterStageManager *stage_manager = clutter_stage_manager_get_default ();
|
|
|
|
const GSList *stages, *l;
|
2009-11-12 15:37:01 -05:00
|
|
|
gboolean stage_free = FALSE;
|
|
|
|
|
|
|
|
stages = clutter_stage_manager_peek_stages (stage_manager);
|
|
|
|
|
|
|
|
/* If all of the stages are busy waiting for a swap-buffers to complete
|
|
|
|
* then we stop the master clock... */
|
|
|
|
for (l = stages; l != NULL; l = l->next)
|
2010-02-15 07:04:50 -05:00
|
|
|
{
|
|
|
|
if (_clutter_stage_get_pending_swaps (l->data) == 0)
|
|
|
|
{
|
|
|
|
stage_free = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-12 15:37:01 -05:00
|
|
|
if (!stage_free)
|
|
|
|
return FALSE;
|
2009-05-07 14:07:21 -04:00
|
|
|
|
2009-06-06 17:44:40 -04:00
|
|
|
if (master_clock->timelines)
|
|
|
|
return TRUE;
|
2009-05-07 14:07:21 -04:00
|
|
|
|
2009-06-06 18:22:51 -04:00
|
|
|
for (l = stages; l; l = l->next)
|
2010-02-15 07:04:50 -05:00
|
|
|
{
|
|
|
|
if (_clutter_stage_has_queued_events (l->data) ||
|
|
|
|
_clutter_stage_needs_update (l->data))
|
|
|
|
return TRUE;
|
|
|
|
}
|
2009-06-06 18:22:51 -04:00
|
|
|
|
2009-07-09 15:03:01 -04:00
|
|
|
if (master_clock->ensure_next_iteration)
|
|
|
|
{
|
|
|
|
master_clock->ensure_next_iteration = FALSE;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2009-05-07 14:07:21 -04:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2009-06-06 22:48:15 -04:00
|
|
|
/*
|
|
|
|
* master_clock_next_frame_delay:
|
|
|
|
* @master_clock: a #ClutterMasterClock
|
|
|
|
*
|
|
|
|
* Computes the number of delay before we need to draw the next frame.
|
|
|
|
*
|
|
|
|
* Return value: -1 if there is no next frame pending, otherwise the
|
|
|
|
* number of millseconds before the we need to draw the next frame
|
|
|
|
*/
|
|
|
|
static gint
|
|
|
|
master_clock_next_frame_delay (ClutterMasterClock *master_clock)
|
|
|
|
{
|
2010-11-07 10:57:33 -05:00
|
|
|
gint64 now, next;
|
2009-06-06 22:48:15 -04:00
|
|
|
|
|
|
|
if (!master_clock_is_running (master_clock))
|
|
|
|
return -1;
|
|
|
|
|
2009-11-12 15:37:01 -05:00
|
|
|
/* When we have sync-to-vblank, we count on swap-buffer requests (or
|
|
|
|
* swap-buffer-complete events if supported in the backend) to throttle our
|
|
|
|
* frame rate so no additional delay is needed to start the next frame.
|
|
|
|
*
|
|
|
|
* If the master-clock has become idle due to no timeline progression causing
|
|
|
|
* redraws then we can no longer rely on vblank synchronization because the
|
|
|
|
* last real stage update/redraw may have happened a long time ago and so we
|
|
|
|
* fallback to polling for timeline progressions every 1/frame_rate seconds.
|
|
|
|
*
|
|
|
|
* (NB: if there aren't even any timelines running then the master clock will
|
|
|
|
* be completely stopped in master_clock_is_running())
|
|
|
|
*/
|
2009-06-10 10:50:27 -04:00
|
|
|
if (clutter_feature_available (CLUTTER_FEATURE_SYNC_TO_VBLANK) &&
|
2009-11-12 15:37:01 -05:00
|
|
|
!master_clock->idle)
|
2009-06-06 22:48:15 -04:00
|
|
|
{
|
2009-06-29 11:33:07 -04:00
|
|
|
CLUTTER_NOTE (SCHEDULER, "vblank available and updated stages");
|
2009-06-06 22:48:15 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-11-07 10:57:33 -05:00
|
|
|
if (master_clock->prev_tick == 0)
|
2009-06-06 22:48:15 -04:00
|
|
|
{
|
|
|
|
/* If we weren't previously running, then draw the next frame
|
|
|
|
* immediately
|
|
|
|
*/
|
2009-06-29 11:33:07 -04:00
|
|
|
CLUTTER_NOTE (SCHEDULER, "draw the first frame immediately");
|
2009-06-06 22:48:15 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-06-29 11:33:07 -04:00
|
|
|
/* Otherwise, wait at least 1/frame_rate seconds since we last
|
|
|
|
* started a frame
|
|
|
|
*/
|
2010-11-07 11:20:51 -05:00
|
|
|
#if GLIB_CHECK_VERSION (2, 27, 3)
|
|
|
|
now = g_source_get_time (master_clock->source);
|
|
|
|
#else
|
|
|
|
{
|
|
|
|
GTimeVal source_time;
|
|
|
|
g_source_get_current_time (master_clock->source, &source_time);
|
|
|
|
now = source_time.tv_sec * 1000000L + source_time.tv_usec;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-06-06 22:48:15 -04:00
|
|
|
next = master_clock->prev_tick;
|
2010-02-05 16:56:31 -05:00
|
|
|
|
|
|
|
/* If time has gone backwards then there's no way of knowing how
|
|
|
|
long we should wait so let's just dispatch immediately */
|
2010-11-07 10:57:33 -05:00
|
|
|
if (now <= next)
|
2010-02-05 16:56:31 -05:00
|
|
|
{
|
|
|
|
CLUTTER_NOTE (SCHEDULER, "Time has gone backwards");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-11-07 10:57:33 -05:00
|
|
|
next += (1000000L / clutter_get_default_frame_rate ());
|
2009-06-06 22:48:15 -04:00
|
|
|
|
2010-11-07 10:57:33 -05:00
|
|
|
if (next <= now)
|
2009-06-06 22:48:15 -04:00
|
|
|
{
|
2009-06-29 11:33:07 -04:00
|
|
|
CLUTTER_NOTE (SCHEDULER, "Less than %lu microsecs",
|
|
|
|
1000000L / (gulong) clutter_get_default_frame_rate ());
|
|
|
|
|
2009-06-06 22:48:15 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-11-07 10:57:33 -05:00
|
|
|
CLUTTER_NOTE (SCHEDULER, "Waiting %" G_GINT64_FORMAT " msecs",
|
|
|
|
(next - now) / 1000);
|
2009-06-29 11:33:07 -04:00
|
|
|
|
2010-11-07 10:57:33 -05:00
|
|
|
return (next - now) / 1000;
|
2009-06-06 22:48:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-29 09:25:19 -04:00
|
|
|
/*
|
|
|
|
* clutter_clock_source_new:
|
|
|
|
* @master_clock: a #ClutterMasterClock for the source
|
|
|
|
*
|
|
|
|
* The #ClutterClockSource is an idle GSource that will queue a redraw
|
|
|
|
* if @master_clock has at least a running #ClutterTimeline. The redraw
|
|
|
|
* will cause @master_clock to advance all timelines, thus advancing all
|
|
|
|
* animations as well.
|
|
|
|
*
|
|
|
|
* Return value: the newly created #GSource
|
|
|
|
*/
|
2009-05-07 14:07:21 -04:00
|
|
|
static GSource *
|
|
|
|
clutter_clock_source_new (ClutterMasterClock *master_clock)
|
|
|
|
{
|
|
|
|
GSource *source = g_source_new (&clock_funcs, sizeof (ClutterClockSource));
|
|
|
|
ClutterClockSource *clock_source = (ClutterClockSource *) source;
|
|
|
|
|
2010-06-03 23:34:09 -04:00
|
|
|
g_source_set_name (source, "Clutter master clock");
|
2009-05-07 14:07:21 -04:00
|
|
|
clock_source->master_clock = master_clock;
|
|
|
|
|
|
|
|
return source;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
clutter_clock_prepare (GSource *source,
|
|
|
|
gint *timeout)
|
|
|
|
{
|
|
|
|
ClutterClockSource *clock_source = (ClutterClockSource *) source;
|
|
|
|
ClutterMasterClock *master_clock = clock_source->master_clock;
|
2009-06-06 22:48:15 -04:00
|
|
|
int delay;
|
2009-05-07 14:07:21 -04:00
|
|
|
|
2009-06-18 17:53:46 -04:00
|
|
|
clutter_threads_enter ();
|
2009-06-06 22:48:15 -04:00
|
|
|
delay = master_clock_next_frame_delay (master_clock);
|
2009-06-18 17:53:46 -04:00
|
|
|
clutter_threads_leave ();
|
2009-05-07 14:07:21 -04:00
|
|
|
|
2009-06-06 22:48:15 -04:00
|
|
|
*timeout = delay;
|
2009-05-07 14:07:21 -04:00
|
|
|
|
2009-06-06 22:48:15 -04:00
|
|
|
return delay == 0;
|
2009-05-07 14:07:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
clutter_clock_check (GSource *source)
|
|
|
|
{
|
|
|
|
ClutterClockSource *clock_source = (ClutterClockSource *) source;
|
|
|
|
ClutterMasterClock *master_clock = clock_source->master_clock;
|
2009-06-06 22:48:15 -04:00
|
|
|
int delay;
|
2009-05-07 14:07:21 -04:00
|
|
|
|
2009-06-18 17:53:46 -04:00
|
|
|
clutter_threads_enter ();
|
2009-06-06 22:48:15 -04:00
|
|
|
delay = master_clock_next_frame_delay (master_clock);
|
2009-06-18 17:53:46 -04:00
|
|
|
clutter_threads_leave ();
|
2009-05-07 14:07:21 -04:00
|
|
|
|
2009-06-06 22:48:15 -04:00
|
|
|
return delay == 0;
|
2009-05-07 14:07:21 -04:00
|
|
|
}
|
2009-03-21 16:39:32 -04:00
|
|
|
|
2009-05-07 14:07:21 -04:00
|
|
|
static gboolean
|
|
|
|
clutter_clock_dispatch (GSource *source,
|
|
|
|
GSourceFunc callback,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
2009-06-06 18:22:51 -04:00
|
|
|
ClutterClockSource *clock_source = (ClutterClockSource *) source;
|
|
|
|
ClutterMasterClock *master_clock = clock_source->master_clock;
|
2009-05-07 14:07:21 -04:00
|
|
|
ClutterStageManager *stage_manager = clutter_stage_manager_get_default ();
|
2009-11-12 15:37:01 -05:00
|
|
|
gboolean stages_updated = FALSE;
|
2010-11-07 10:57:33 -05:00
|
|
|
GSList *stages, *l;
|
2009-05-07 14:07:21 -04:00
|
|
|
|
2010-01-14 07:28:07 -05:00
|
|
|
CLUTTER_STATIC_TIMER (master_dispatch_timer,
|
|
|
|
"Mainloop",
|
|
|
|
"Master Clock",
|
|
|
|
"Master clock dispatch",
|
|
|
|
0);
|
|
|
|
CLUTTER_STATIC_TIMER (master_event_process,
|
|
|
|
"Master Clock",
|
|
|
|
"Event Processing",
|
|
|
|
"The time spent processing events on all stages",
|
|
|
|
0);
|
|
|
|
|
|
|
|
CLUTTER_TIMER_START (_clutter_uprof_context, master_dispatch_timer);
|
|
|
|
|
2009-05-07 14:07:21 -04:00
|
|
|
CLUTTER_NOTE (SCHEDULER, "Master clock [tick]");
|
|
|
|
|
2009-06-18 17:53:46 -04:00
|
|
|
clutter_threads_enter ();
|
|
|
|
|
2010-11-07 10:57:33 -05:00
|
|
|
/* Get the time to use for this frame */
|
2010-11-07 11:20:51 -05:00
|
|
|
#if GLIB_CHECK_VERSION (2, 27, 3)
|
|
|
|
master_clock->cur_tick = g_source_get_time (source);
|
|
|
|
#else
|
|
|
|
{
|
|
|
|
GTimeVal source_time;
|
|
|
|
g_source_get_current_time (source, &source_time);
|
|
|
|
master_clock->cur_tick = source_time.tv_sec * 1000000L
|
|
|
|
+ source_time.tv_usec;
|
|
|
|
}
|
|
|
|
#endif
|
2009-06-06 22:48:15 -04:00
|
|
|
|
2009-06-06 19:10:41 -04:00
|
|
|
/* We need to protect ourselves against stages being destroyed during
|
|
|
|
* event handling
|
|
|
|
*/
|
|
|
|
stages = clutter_stage_manager_list_stages (stage_manager);
|
2010-01-08 12:51:00 -05:00
|
|
|
g_slist_foreach (stages, (GFunc) g_object_ref, NULL);
|
2009-06-06 19:10:41 -04:00
|
|
|
|
2010-01-14 07:28:07 -05:00
|
|
|
CLUTTER_TIMER_START (_clutter_uprof_context, master_event_process);
|
|
|
|
|
2009-11-12 15:37:01 -05:00
|
|
|
master_clock->idle = FALSE;
|
2009-06-10 10:50:27 -04:00
|
|
|
|
2010-01-08 12:51:00 -05:00
|
|
|
/* Process queued events */
|
2009-06-06 19:10:41 -04:00
|
|
|
for (l = stages; l != NULL; l = l->next)
|
2009-11-12 15:37:01 -05:00
|
|
|
{
|
|
|
|
/* NB: If a stage is busy waiting for a swap-buffers completion then
|
|
|
|
* we don't process its events so we can maximize the benefits of
|
|
|
|
* motion compression, and avoid multiple picks per frame.
|
|
|
|
*/
|
|
|
|
if (_clutter_stage_get_pending_swaps (l->data) == 0)
|
|
|
|
_clutter_stage_process_queued_events (l->data);
|
|
|
|
}
|
2009-05-07 14:07:21 -04:00
|
|
|
|
2010-01-14 07:28:07 -05:00
|
|
|
CLUTTER_TIMER_STOP (_clutter_uprof_context, master_event_process);
|
|
|
|
|
2009-06-09 11:28:25 -04:00
|
|
|
_clutter_master_clock_advance (master_clock);
|
2010-01-14 07:28:07 -05:00
|
|
|
|
2009-06-09 11:28:25 -04:00
|
|
|
_clutter_run_repaint_functions ();
|
2009-06-06 18:22:51 -04:00
|
|
|
|
|
|
|
/* Update any stage that needs redraw/relayout after the clock
|
|
|
|
* is advanced.
|
2009-05-29 09:25:19 -04:00
|
|
|
*/
|
2009-05-07 14:07:21 -04:00
|
|
|
for (l = stages; l != NULL; l = l->next)
|
2009-11-12 15:37:01 -05:00
|
|
|
{
|
|
|
|
/* If a stage has a swap-buffers pending we don't want to draw to it
|
|
|
|
* in case the driver may block the CPU while it waits for the next
|
|
|
|
* backbuffer to become available.
|
|
|
|
*
|
|
|
|
* TODO: We should be able to identify if we are running triple or N
|
|
|
|
* buffered and in these cases we can still draw if there is 1 swap
|
|
|
|
* pending so we can hopefully always be ready to swap for the next
|
|
|
|
* vblank and really match the vsync frequency.
|
|
|
|
*/
|
|
|
|
if (_clutter_stage_get_pending_swaps (l->data) == 0)
|
|
|
|
stages_updated |= _clutter_stage_do_update (l->data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The master clock goes idle if no stages were updated and falls back
|
|
|
|
* to polling for timeline progressions... */
|
|
|
|
if (!stages_updated)
|
|
|
|
master_clock->idle = TRUE;
|
2009-05-07 14:07:21 -04:00
|
|
|
|
2010-01-08 12:51:00 -05:00
|
|
|
g_slist_foreach (stages, (GFunc) g_object_unref, NULL);
|
2009-06-06 19:10:41 -04:00
|
|
|
g_slist_free (stages);
|
|
|
|
|
2009-06-06 22:48:15 -04:00
|
|
|
master_clock->prev_tick = master_clock->cur_tick;
|
|
|
|
|
2009-06-18 17:53:46 -04:00
|
|
|
clutter_threads_leave ();
|
|
|
|
|
2010-01-14 07:28:07 -05:00
|
|
|
CLUTTER_TIMER_STOP (_clutter_uprof_context, master_dispatch_timer);
|
|
|
|
|
2009-05-07 14:07:21 -04:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2009-03-21 16:39:32 -04:00
|
|
|
static void
|
|
|
|
clutter_master_clock_finalize (GObject *gobject)
|
|
|
|
{
|
|
|
|
ClutterMasterClock *master_clock = CLUTTER_MASTER_CLOCK (gobject);
|
|
|
|
|
|
|
|
g_slist_free (master_clock->timelines);
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (clutter_master_clock_parent_class)->finalize (gobject);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
clutter_master_clock_class_init (ClutterMasterClockClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
|
|
|
|
|
|
|
gobject_class->finalize = clutter_master_clock_finalize;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
clutter_master_clock_init (ClutterMasterClock *self)
|
|
|
|
{
|
2009-05-07 14:07:21 -04:00
|
|
|
GSource *source;
|
|
|
|
|
|
|
|
source = clutter_clock_source_new (self);
|
|
|
|
self->source = source;
|
|
|
|
|
2009-11-12 15:37:01 -05:00
|
|
|
self->idle = FALSE;
|
2009-07-09 15:03:01 -04:00
|
|
|
self->ensure_next_iteration = FALSE;
|
2009-06-10 10:50:27 -04:00
|
|
|
|
2009-05-07 14:07:21 -04:00
|
|
|
g_source_set_priority (source, CLUTTER_PRIORITY_REDRAW);
|
|
|
|
g_source_set_can_recurse (source, FALSE);
|
|
|
|
g_source_attach (source, NULL);
|
2009-03-21 16:39:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* _clutter_master_clock_get_default:
|
|
|
|
*
|
|
|
|
* Retrieves the default master clock. If this function has never
|
|
|
|
* been called before, the default master clock is created.
|
|
|
|
*
|
|
|
|
* Return value: the default master clock. The returned object is
|
|
|
|
* owned by Clutter and should not be modified or freed
|
|
|
|
*/
|
|
|
|
ClutterMasterClock *
|
|
|
|
_clutter_master_clock_get_default (void)
|
|
|
|
{
|
|
|
|
if (G_LIKELY (default_clock != NULL))
|
|
|
|
return default_clock;
|
|
|
|
|
|
|
|
default_clock = g_object_new (CLUTTER_TYPE_MASTER_CLOCK, NULL);
|
|
|
|
|
|
|
|
return default_clock;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* _clutter_master_clock_add_timeline:
|
|
|
|
* @master_clock: a #ClutterMasterClock
|
|
|
|
* @timeline: a #ClutterTimeline
|
|
|
|
*
|
2009-06-06 17:44:40 -04:00
|
|
|
* Adds @timeline to the list of playing timelines held by the master
|
|
|
|
* clock.
|
2009-03-21 16:39:32 -04:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
_clutter_master_clock_add_timeline (ClutterMasterClock *master_clock,
|
|
|
|
ClutterTimeline *timeline)
|
|
|
|
{
|
2009-06-06 17:44:40 -04:00
|
|
|
gboolean is_first;
|
2009-03-21 16:39:32 -04:00
|
|
|
|
|
|
|
if (g_slist_find (master_clock->timelines, timeline))
|
|
|
|
return;
|
|
|
|
|
2009-06-06 17:44:40 -04:00
|
|
|
is_first = master_clock->timelines == NULL;
|
2009-03-21 16:39:32 -04:00
|
|
|
|
|
|
|
master_clock->timelines = g_slist_prepend (master_clock->timelines,
|
|
|
|
timeline);
|
|
|
|
|
2009-06-06 17:44:40 -04:00
|
|
|
if (is_first)
|
2009-06-06 22:48:15 -04:00
|
|
|
_clutter_master_clock_start_running (master_clock);
|
2009-03-21 16:39:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* _clutter_master_clock_remove_timeline:
|
|
|
|
* @master_clock: a #ClutterMasterClock
|
|
|
|
* @timeline: a #ClutterTimeline
|
|
|
|
*
|
2009-06-06 17:44:40 -04:00
|
|
|
* Removes @timeline from the list of playing timelines held by the
|
|
|
|
* master clock.
|
2009-03-21 16:39:32 -04:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
_clutter_master_clock_remove_timeline (ClutterMasterClock *master_clock,
|
|
|
|
ClutterTimeline *timeline)
|
|
|
|
{
|
|
|
|
master_clock->timelines = g_slist_remove (master_clock->timelines,
|
|
|
|
timeline);
|
2009-06-06 22:48:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* _clutter_master_clock_start_running:
|
|
|
|
* @master_clock: a #ClutterMasterClock
|
|
|
|
*
|
|
|
|
* Called when we have events or redraws to process; if the clock
|
|
|
|
* is stopped, does the processing necessary to wake it up again.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
_clutter_master_clock_start_running (ClutterMasterClock *master_clock)
|
|
|
|
{
|
|
|
|
/* If called from a different thread, we need to wake up the
|
|
|
|
* main loop to start running the timelines
|
|
|
|
*/
|
|
|
|
g_main_context_wakeup (NULL);
|
2009-03-21 16:39:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* _clutter_master_clock_advance:
|
|
|
|
* @master_clock: a #ClutterMasterClock
|
|
|
|
*
|
|
|
|
* Advances all the timelines held by the master clock. This function
|
|
|
|
* should be called before calling clutter_redraw() to make sure that
|
|
|
|
* all the timelines are advanced and the scene is updated.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
_clutter_master_clock_advance (ClutterMasterClock *master_clock)
|
|
|
|
{
|
2010-01-21 18:41:18 -05:00
|
|
|
GSList *timelines, *l;
|
2009-03-21 16:39:32 -04:00
|
|
|
|
2010-01-14 07:28:07 -05:00
|
|
|
CLUTTER_STATIC_TIMER (master_timeline_advance,
|
|
|
|
"Master Clock",
|
|
|
|
"Timelines Advancement",
|
|
|
|
"The time spent advancing all timelines",
|
|
|
|
0);
|
|
|
|
|
2009-03-21 16:39:32 -04:00
|
|
|
g_return_if_fail (CLUTTER_IS_MASTER_CLOCK (master_clock));
|
|
|
|
|
2010-01-21 18:41:18 -05:00
|
|
|
CLUTTER_TIMER_START (_clutter_uprof_context, master_timeline_advance);
|
|
|
|
|
2009-11-11 06:00:29 -05:00
|
|
|
/* we protect ourselves from timelines being removed during
|
2010-01-21 18:41:18 -05:00
|
|
|
* the advancement by other timelines by copying the list of
|
|
|
|
* timelines, taking a reference on them, iterating over the
|
|
|
|
* copied list and then releasing the reference.
|
|
|
|
*
|
|
|
|
* we cannot simply take a reference on the timelines and still
|
|
|
|
* use the list held by the master clock because the do_tick()
|
|
|
|
* might result in the creation of a new timeline, which gets
|
|
|
|
* added at the end of the list with no reference increase and
|
|
|
|
* thus gets disposed at the end of the iteration.
|
|
|
|
*
|
|
|
|
* this implies that a newly added timeline will not be advanced
|
|
|
|
* by this clock iteration, which is perfectly fine since we're
|
|
|
|
* in its first cycle.
|
|
|
|
*
|
|
|
|
* we also cannot steal the master clock timelines list because
|
|
|
|
* a timeline might be removed as the direct result of do_tick()
|
|
|
|
* and remove_timeline() would not find the timeline, failing
|
|
|
|
* and leaving a dangling pointer behind.
|
2009-11-11 06:00:29 -05:00
|
|
|
*/
|
2010-01-21 18:41:18 -05:00
|
|
|
timelines = g_slist_copy (master_clock->timelines);
|
|
|
|
g_slist_foreach (timelines, (GFunc) g_object_ref, NULL);
|
2009-11-11 06:00:29 -05:00
|
|
|
|
2010-01-21 18:41:18 -05:00
|
|
|
for (l = timelines; l != NULL; l = l->next)
|
2010-11-07 10:57:33 -05:00
|
|
|
_clutter_timeline_do_tick (l->data, master_clock->cur_tick / 1000);
|
2010-01-14 07:28:07 -05:00
|
|
|
|
2010-01-21 18:41:18 -05:00
|
|
|
g_slist_foreach (timelines, (GFunc) g_object_unref, NULL);
|
|
|
|
g_slist_free (timelines);
|
2009-11-11 06:00:29 -05:00
|
|
|
|
2010-01-14 07:28:07 -05:00
|
|
|
CLUTTER_TIMER_STOP (_clutter_uprof_context, master_timeline_advance);
|
2009-03-21 16:39:32 -04:00
|
|
|
}
|
2009-07-09 15:03:01 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* _clutter_master_clock_ensure_next_iteration:
|
|
|
|
* @master_clock: a #ClutterMasterClock
|
|
|
|
*
|
|
|
|
* Ensures that the master clock will run at least one iteration
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
_clutter_master_clock_ensure_next_iteration (ClutterMasterClock *master_clock)
|
|
|
|
{
|
|
|
|
g_return_if_fail (CLUTTER_IS_MASTER_CLOCK (master_clock));
|
|
|
|
|
|
|
|
master_clock->ensure_next_iteration = TRUE;
|
|
|
|
}
|
|
|
|
|