2009-03-21 20:39:32 +00: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"
|
|
|
|
|
|
|
|
#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 18:07:21 +00:00
|
|
|
typedef struct _ClutterClockSource ClutterClockSource;
|
2009-03-21 20:39:32 +00:00
|
|
|
typedef struct _ClutterMasterClockClass ClutterMasterClockClass;
|
|
|
|
|
|
|
|
struct _ClutterMasterClock
|
|
|
|
{
|
|
|
|
GObject parent_instance;
|
|
|
|
|
2009-05-07 18:07:21 +00:00
|
|
|
/* the list of timelines handled by the clock */
|
2009-03-21 20:39:32 +00:00
|
|
|
GSList *timelines;
|
|
|
|
|
2009-06-07 02:48:15 +00:00
|
|
|
/* the current state of the clock
|
|
|
|
*/
|
|
|
|
GTimeVal cur_tick;
|
|
|
|
|
2009-05-07 18:07:21 +00:00
|
|
|
/* the previous state of the clock, used to compute
|
|
|
|
* the delta
|
|
|
|
*/
|
2009-03-21 20:39:32 +00:00
|
|
|
GTimeVal prev_tick;
|
|
|
|
|
2009-05-07 18:07:21 +00: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 14:50:27 +00:00
|
|
|
|
|
|
|
guint updated_stages : 1;
|
2009-03-21 20:39:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct _ClutterMasterClockClass
|
|
|
|
{
|
|
|
|
GObjectClass parent_class;
|
|
|
|
};
|
|
|
|
|
2009-05-07 18:07:21 +00:00
|
|
|
struct _ClutterClockSource
|
|
|
|
{
|
|
|
|
GSource source;
|
|
|
|
|
|
|
|
ClutterMasterClock *master_clock;
|
|
|
|
};
|
2009-03-21 20:39:32 +00:00
|
|
|
|
2009-05-07 18:07:21 +00: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 20:39:32 +00:00
|
|
|
G_DEFINE_TYPE (ClutterMasterClock, clutter_master_clock, G_TYPE_OBJECT);
|
|
|
|
|
2009-05-29 13:25:19 +00:00
|
|
|
/*
|
2009-06-06 22:22:51 +00:00
|
|
|
* master_clock_is_running:
|
2009-05-29 13:25:19 +00:00
|
|
|
* @master_clock: a #ClutterMasterClock
|
|
|
|
*
|
2009-06-06 22:22:51 +00:00
|
|
|
* Checks if we should currently be advancing timelines or redrawing
|
|
|
|
* stages.
|
2009-05-29 13:25:19 +00:00
|
|
|
*
|
|
|
|
* Return value: %TRUE if the #ClutterMasterClock has at least
|
|
|
|
* one running timeline
|
|
|
|
*/
|
2009-05-07 18:07:21 +00:00
|
|
|
static gboolean
|
2009-06-06 22:22:51 +00:00
|
|
|
master_clock_is_running (ClutterMasterClock *master_clock)
|
2009-03-21 20:39:32 +00:00
|
|
|
{
|
2009-06-06 21:44:40 +00:00
|
|
|
ClutterStageManager *stage_manager = clutter_stage_manager_get_default ();
|
|
|
|
const GSList *stages, *l;
|
2009-05-07 18:07:21 +00:00
|
|
|
|
2009-06-06 21:44:40 +00:00
|
|
|
if (master_clock->timelines)
|
|
|
|
return TRUE;
|
2009-05-07 18:07:21 +00:00
|
|
|
|
2009-06-06 22:22:51 +00:00
|
|
|
stages = clutter_stage_manager_peek_stages (stage_manager);
|
|
|
|
for (l = stages; l; l = l->next)
|
2009-06-06 23:10:41 +00:00
|
|
|
if (_clutter_stage_has_queued_events (l->data) ||
|
|
|
|
_clutter_stage_needs_update (l->data))
|
2009-06-06 22:22:51 +00:00
|
|
|
return TRUE;
|
|
|
|
|
2009-05-07 18:07:21 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2009-06-07 02:48:15 +00: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)
|
|
|
|
{
|
|
|
|
GTimeVal now;
|
|
|
|
GTimeVal next;
|
|
|
|
|
|
|
|
if (!master_clock_is_running (master_clock))
|
|
|
|
return -1;
|
|
|
|
|
2009-06-10 14:50:27 +00:00
|
|
|
if (clutter_feature_available (CLUTTER_FEATURE_SYNC_TO_VBLANK) &&
|
|
|
|
master_clock->updated_stages)
|
2009-06-07 02:48:15 +00:00
|
|
|
{
|
|
|
|
/* When we have sync-to-vblank, we count on that to throttle
|
|
|
|
* our frame rate, and otherwise draw frames as fast as possible.
|
|
|
|
*/
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (master_clock->prev_tick.tv_sec == 0)
|
|
|
|
{
|
|
|
|
/* If we weren't previously running, then draw the next frame
|
|
|
|
* immediately
|
|
|
|
*/
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise, wait at least 1/frame_rate seconds since we last started a frame */
|
|
|
|
|
|
|
|
g_source_get_current_time (master_clock->source, &now);
|
|
|
|
|
|
|
|
next = master_clock->prev_tick;
|
|
|
|
g_time_val_add (&next, 1000000 / clutter_get_default_frame_rate ());
|
|
|
|
|
|
|
|
if (next.tv_sec < now.tv_sec ||
|
|
|
|
(next.tv_sec == now.tv_sec && next.tv_usec < now.tv_usec))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return ((next.tv_sec - now.tv_sec) * 1000 +
|
|
|
|
(next.tv_usec - now.tv_usec) / 1000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-29 13:25:19 +00: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 18:07:21 +00:00
|
|
|
static GSource *
|
|
|
|
clutter_clock_source_new (ClutterMasterClock *master_clock)
|
|
|
|
{
|
|
|
|
GSource *source = g_source_new (&clock_funcs, sizeof (ClutterClockSource));
|
|
|
|
ClutterClockSource *clock_source = (ClutterClockSource *) source;
|
|
|
|
|
|
|
|
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-07 02:48:15 +00:00
|
|
|
int delay;
|
2009-05-07 18:07:21 +00:00
|
|
|
|
2009-06-07 02:48:15 +00:00
|
|
|
delay = master_clock_next_frame_delay (master_clock);
|
2009-05-07 18:07:21 +00:00
|
|
|
|
2009-06-07 02:48:15 +00:00
|
|
|
*timeout = delay;
|
2009-05-07 18:07:21 +00:00
|
|
|
|
2009-06-07 02:48:15 +00:00
|
|
|
return delay == 0;
|
2009-05-07 18:07:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
clutter_clock_check (GSource *source)
|
|
|
|
{
|
|
|
|
ClutterClockSource *clock_source = (ClutterClockSource *) source;
|
|
|
|
ClutterMasterClock *master_clock = clock_source->master_clock;
|
2009-06-07 02:48:15 +00:00
|
|
|
int delay;
|
2009-05-07 18:07:21 +00:00
|
|
|
|
2009-06-07 02:48:15 +00:00
|
|
|
delay = master_clock_next_frame_delay (master_clock);
|
2009-05-07 18:07:21 +00:00
|
|
|
|
2009-06-07 02:48:15 +00:00
|
|
|
return delay == 0;
|
2009-05-07 18:07:21 +00:00
|
|
|
}
|
2009-03-21 20:39:32 +00:00
|
|
|
|
2009-05-07 18:07:21 +00:00
|
|
|
static gboolean
|
|
|
|
clutter_clock_dispatch (GSource *source,
|
|
|
|
GSourceFunc callback,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
2009-06-06 22:22:51 +00:00
|
|
|
ClutterClockSource *clock_source = (ClutterClockSource *) source;
|
|
|
|
ClutterMasterClock *master_clock = clock_source->master_clock;
|
2009-05-07 18:07:21 +00:00
|
|
|
ClutterStageManager *stage_manager = clutter_stage_manager_get_default ();
|
2009-06-06 23:10:41 +00:00
|
|
|
GSList *stages, *l;
|
2009-05-07 18:07:21 +00:00
|
|
|
|
|
|
|
CLUTTER_NOTE (SCHEDULER, "Master clock [tick]");
|
|
|
|
|
2009-06-07 02:48:15 +00:00
|
|
|
/* Get the time to use for this frame.
|
|
|
|
*/
|
|
|
|
g_source_get_current_time (source, &master_clock->cur_tick);
|
|
|
|
|
2009-06-06 23:10:41 +00:00
|
|
|
/* We need to protect ourselves against stages being destroyed during
|
|
|
|
* event handling
|
|
|
|
*/
|
|
|
|
stages = clutter_stage_manager_list_stages (stage_manager);
|
|
|
|
g_slist_foreach (stages, (GFunc)g_object_ref, NULL);
|
|
|
|
|
2009-06-10 14:50:27 +00:00
|
|
|
master_clock->updated_stages = FALSE;
|
|
|
|
|
2009-06-06 23:10:41 +00:00
|
|
|
/* Process queued events
|
|
|
|
*/
|
|
|
|
for (l = stages; l != NULL; l = l->next)
|
|
|
|
_clutter_stage_process_queued_events (l->data);
|
2009-05-07 18:07:21 +00:00
|
|
|
|
2009-06-09 15:28:25 +00:00
|
|
|
_clutter_master_clock_advance (master_clock);
|
|
|
|
_clutter_run_repaint_functions ();
|
2009-06-06 22:22:51 +00:00
|
|
|
|
|
|
|
/* Update any stage that needs redraw/relayout after the clock
|
|
|
|
* is advanced.
|
2009-05-29 13:25:19 +00:00
|
|
|
*/
|
2009-05-07 18:07:21 +00:00
|
|
|
for (l = stages; l != NULL; l = l->next)
|
2009-06-10 14:50:27 +00:00
|
|
|
master_clock->updated_stages |= _clutter_stage_do_update (l->data);
|
2009-05-07 18:07:21 +00:00
|
|
|
|
2009-06-06 23:10:41 +00:00
|
|
|
g_slist_foreach (stages, (GFunc)g_object_unref, NULL);
|
|
|
|
g_slist_free (stages);
|
|
|
|
|
2009-06-07 02:48:15 +00:00
|
|
|
master_clock->prev_tick = master_clock->cur_tick;
|
|
|
|
|
2009-05-07 18:07:21 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2009-03-21 20:39:32 +00: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 18:07:21 +00:00
|
|
|
GSource *source;
|
|
|
|
|
|
|
|
source = clutter_clock_source_new (self);
|
|
|
|
self->source = source;
|
|
|
|
|
2009-06-10 14:50:27 +00:00
|
|
|
self->updated_stages = TRUE;
|
|
|
|
|
2009-05-07 18:07:21 +00:00
|
|
|
g_source_set_priority (source, CLUTTER_PRIORITY_REDRAW);
|
|
|
|
g_source_set_can_recurse (source, FALSE);
|
|
|
|
g_source_attach (source, NULL);
|
2009-03-21 20:39:32 +00: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 21:44:40 +00:00
|
|
|
* Adds @timeline to the list of playing timelines held by the master
|
|
|
|
* clock.
|
2009-03-21 20:39:32 +00:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
_clutter_master_clock_add_timeline (ClutterMasterClock *master_clock,
|
|
|
|
ClutterTimeline *timeline)
|
|
|
|
{
|
2009-06-06 21:44:40 +00:00
|
|
|
gboolean is_first;
|
2009-03-21 20:39:32 +00:00
|
|
|
|
|
|
|
if (g_slist_find (master_clock->timelines, timeline))
|
|
|
|
return;
|
|
|
|
|
2009-06-06 21:44:40 +00:00
|
|
|
is_first = master_clock->timelines == NULL;
|
2009-03-21 20:39:32 +00:00
|
|
|
|
|
|
|
master_clock->timelines = g_slist_prepend (master_clock->timelines,
|
|
|
|
timeline);
|
|
|
|
|
2009-06-06 21:44:40 +00:00
|
|
|
if (is_first)
|
2009-06-07 02:48:15 +00:00
|
|
|
_clutter_master_clock_start_running (master_clock);
|
2009-03-21 20:39:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* _clutter_master_clock_remove_timeline:
|
|
|
|
* @master_clock: a #ClutterMasterClock
|
|
|
|
* @timeline: a #ClutterTimeline
|
|
|
|
*
|
2009-06-06 21:44:40 +00:00
|
|
|
* Removes @timeline from the list of playing timelines held by the
|
|
|
|
* master clock.
|
2009-03-21 20:39:32 +00:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
_clutter_master_clock_remove_timeline (ClutterMasterClock *master_clock,
|
|
|
|
ClutterTimeline *timeline)
|
|
|
|
{
|
|
|
|
master_clock->timelines = g_slist_remove (master_clock->timelines,
|
|
|
|
timeline);
|
2009-06-07 02:48:15 +00: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 20:39:32 +00: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)
|
|
|
|
{
|
2009-06-08 17:00:09 +00:00
|
|
|
GSList *l, *next;
|
2009-03-21 20:39:32 +00:00
|
|
|
|
|
|
|
g_return_if_fail (CLUTTER_IS_MASTER_CLOCK (master_clock));
|
|
|
|
|
2009-06-08 17:00:09 +00:00
|
|
|
for (l = master_clock->timelines; l != NULL; l = next)
|
2009-03-21 20:39:32 +00:00
|
|
|
{
|
2009-06-08 17:00:09 +00:00
|
|
|
next = l->next;
|
2009-03-21 20:39:32 +00:00
|
|
|
|
2009-06-08 17:00:09 +00:00
|
|
|
clutter_timeline_do_tick (l->data, &master_clock->cur_tick);
|
2009-03-21 20:39:32 +00:00
|
|
|
}
|
|
|
|
}
|