Debian Codegraph @

clutter/gdk/clutter-master-clock-gdk.c

raw ◀ ▶
1 Debian patch touch this file
1 /*
2 * Clutter.
3 *
4 * An OpenGL based 'interactive canvas' library.
5 *
6 * Authored By: Lionel Landwerlin <[email protected]>
7 *
8 * Copyright (C) 2015 Intel Corporation.
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 /*
25 * SECTION:clutter-master-clock-gdk
26 * @short_description: The GDK master clock for all animations
27 *
28 * The #ClutterMasterClockDefault class is the GdkFrameClock based implementation
29 * of #ClutterMasterClock.
30 */
31
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35
36 #include <gdk/gdk.h>
37
38 #include "clutter-master-clock.h"
39 #include "clutter-master-clock-gdk.h"
40 #include "clutter-stage-gdk.h"
41 #include "clutter-debug.h"
42 #include "clutter-private.h"
43 #include "clutter-stage-manager-private.h"
44 #include "clutter-stage-private.h"
45
46 #ifdef CLUTTER_ENABLE_DEBUG
47 #define clutter_warn_if_over_budget(master_clock,start_time,section) G_STMT_START { \
48 gint64 __delta = g_get_monotonic_time () - start_time; \
49 gint64 __budget = master_clock->remaining_budget; \
50 if (__budget > 0 && __delta >= __budget) { \
51 _clutter_diagnostic_message ("%s took %" G_GINT64_FORMAT " microseconds " \
52 "more than the remaining budget of %" G_GINT64_FORMAT \
53 " microseconds", \
54 section, __delta - __budget, __budget); \
55 } } G_STMT_END
56 #else
57 #define clutter_warn_if_over_budget(master_clock,start_time,section)
58 #endif
59
60 typedef struct _ClutterClockSource ClutterClockSource;
61
62 struct _ClutterMasterClockGdk
63 {
64 GObject parent_instance;
65
66 /* the list of timelines handled by the clock */
67 GSList *timelines;
68
69 /* mapping between ClutterStages and GdkFrameClocks.
70 *
71 * @stage_to_clock: a direct mapping because each stage has at most one clock
72 * @clock_to_stage: each clock can have more than one stage
73 */
74 GHashTable *stage_to_clock;
75 GHashTable *clock_to_stage;
76
77 /* the current state of the clock, in usecs */
78 gint64 cur_tick;
79
80 /* the previous state of the clock, in usecs, used to compute the delta */
81 gint64 prev_tick;
82
83 #ifdef CLUTTER_ENABLE_DEBUG
84 gint64 frame_budget;
85 gint64 remaining_budget;
86 #endif
87 };
88
89 struct _ClutterClockSource
90 {
91 GSource source;
92
93 ClutterMasterClock *master_clock;
94 };
95
96 static void clutter_master_clock_iface_init (ClutterMasterClockIface *iface);
97
98 G_DEFINE_TYPE_WITH_CODE (ClutterMasterClockGdk,
99 clutter_master_clock_gdk,
100 G_TYPE_OBJECT,
101 G_IMPLEMENT_INTERFACE (CLUTTER_TYPE_MASTER_CLOCK,
102 clutter_master_clock_iface_init));
103
104 static void
105 master_clock_schedule_forced_stages_updates (ClutterMasterClockGdk *master_clock)
106 {
107 GHashTableIter iter;
108 gpointer stage, frame_clock;
109
110 g_hash_table_iter_init (&iter, master_clock->stage_to_clock);
111 while (g_hash_table_iter_next (&iter, &stage, &frame_clock))
112 gdk_frame_clock_request_phase (GDK_FRAME_CLOCK (frame_clock),
113 GDK_FRAME_CLOCK_PHASE_PAINT);
114 }
115
116 static void
117 master_clock_sync_frame_clock_update (ClutterMasterClockGdk *master_clock)
118 {
119 gboolean updating = master_clock->timelines != NULL;
120 gpointer frame_clock, stage_list;
121 GHashTableIter iter;
122
123 g_hash_table_iter_init (&iter, master_clock->clock_to_stage);
124 while (g_hash_table_iter_next (&iter, &frame_clock, &stage_list))
125 {
126 gboolean clock_updating =
127 GPOINTER_TO_UINT (g_object_get_data (frame_clock,
128 "clutter-master-clock-updating"));
129 if (clock_updating != updating)
130 {
131 if (updating)
132 gdk_frame_clock_begin_updating (GDK_FRAME_CLOCK (frame_clock));
133 else
134 gdk_frame_clock_end_updating (GDK_FRAME_CLOCK (frame_clock));
135 g_object_set_data (frame_clock,
136 "clutter-master-clock-updating",
137 GUINT_TO_POINTER (updating));
138 }
139 }
140 }
141
142 static void
143 master_clock_schedule_stage_update (ClutterMasterClockGdk *master_clock,
144 ClutterStage *stage,
145 GdkFrameClock *frame_clock)
146 {
147 /* Clear the old update time */
148 _clutter_stage_clear_update_time (stage);
149
150 /* And if there is still work to be done, schedule a new one */
151 if (_clutter_stage_has_queued_events (stage) ||
152 _clutter_stage_needs_update (stage))
153 _clutter_stage_schedule_update (stage);
154
155 /* We can avoid to schedule a new frame if the stage doesn't need
156 * anymore redrawing. But in the case we still have timelines alive,
157 * we have no choice, we need to advance the timelines for the next
158 * frame. */
159 if (master_clock->timelines != NULL)
160 gdk_frame_clock_request_phase (frame_clock, GDK_FRAME_CLOCK_PHASE_PAINT);
161 }
162
163 static void
164 master_clock_process_stage_events (ClutterMasterClockGdk *master_clock,
165 ClutterStage *stage)
166 {
167 #ifdef CLUTTER_ENABLE_DEBUG
168 gint64 start = g_get_monotonic_time ();
169 #endif
170
171 /* Process queued events */
172 _clutter_stage_process_queued_events (stage);
173
174 #ifdef CLUTTER_ENABLE_DEBUG
175 if (_clutter_diagnostic_enabled ())
176 clutter_warn_if_over_budget (master_clock, start, "Event processing");
177
178 master_clock->remaining_budget -= (g_get_monotonic_time () - start);
179 #endif
180 }
181
182 /*
183 * master_clock_advance_timelines:
184 * @master_clock: a #ClutterMasterClock
185 *
186 * Advances all the timelines held by the master clock. This function
187 * should be called before calling _clutter_stage_do_update() to
188 * make sure that all the timelines are advanced and the scene is updated.
189 */
190 static void
191 master_clock_advance_timelines (ClutterMasterClockGdk *master_clock)
192 {
193 GSList *timelines, *l;
194 #ifdef CLUTTER_ENABLE_DEBUG
195 gint64 start = g_get_monotonic_time ();
196 #endif
197
198 /* we protect ourselves from timelines being removed during
199 * the advancement by other timelines by copying the list of
200 * timelines, taking a reference on them, iterating over the
201 * copied list and then releasing the reference.
202 *
203 * we cannot simply take a reference on the timelines and still
204 * use the list held by the master clock because the do_tick()
205 * might result in the creation of a new timeline, which gets
206 * added at the end of the list with no reference increase and
207 * thus gets disposed at the end of the iteration.
208 *
209 * this implies that a newly added timeline will not be advanced
210 * by this clock iteration, which is perfectly fine since we're
211 * in its first cycle.
212 *
213 * we also cannot steal the master clock timelines list because
214 * a timeline might be removed as the direct result of do_tick()
215 * and remove_timeline() would not find the timeline, failing
216 * and leaving a dangling pointer behind.
217 */
218 timelines = g_slist_copy (master_clock->timelines);
219 g_slist_foreach (timelines, (GFunc) g_object_ref, NULL);
220
221 for (l = timelines; l != NULL; l = l->next)
222 _clutter_timeline_do_tick (l->data, master_clock->cur_tick / 1000);
223
224 g_slist_foreach (timelines, (GFunc) g_object_unref, NULL);
225 g_slist_free (timelines);
226
227 #ifdef CLUTTER_ENABLE_DEBUG
228 if (_clutter_diagnostic_enabled ())
229 clutter_warn_if_over_budget (master_clock, start, "Animations");
230
231 master_clock->remaining_budget -= (g_get_monotonic_time () - start);
232 #endif
233 }
234
235 static gboolean
236 master_clock_update_stage (ClutterMasterClockGdk *master_clock,
237 ClutterStage *stage)
238 {
239 gboolean stage_updated = FALSE;
240 #ifdef CLUTTER_ENABLE_DEBUG
241 gint64 start = g_get_monotonic_time ();
242 #endif
243
244 _clutter_run_repaint_functions (CLUTTER_REPAINT_FLAGS_PRE_PAINT);
245
246 /* Update any stage that needs redraw/relayout after the clock
247 * is advanced.
248 */
249 stage_updated |= _clutter_stage_do_update (stage);
250
251 _clutter_run_repaint_functions (CLUTTER_REPAINT_FLAGS_POST_PAINT);
252
253 #ifdef CLUTTER_ENABLE_DEBUG
254 if (_clutter_diagnostic_enabled ())
255 clutter_warn_if_over_budget (master_clock, start, "Updating the stage");
256
257 master_clock->remaining_budget -= (g_get_monotonic_time () - start);
258 #endif
259
260 return stage_updated;
261 }
262
263 static void
264 clutter_master_clock_gdk_update (GdkFrameClock *frame_clock,
265 ClutterMasterClockGdk *master_clock)
266 {
•267 GList *stages, *l, *next;
268
269 _clutter_threads_acquire_lock ();
270
271 /* Get the time to use for this frame */
272 master_clock->cur_tick = gdk_frame_clock_get_frame_time (frame_clock);
273
274 #ifdef CLUTTER_ENABLE_DEBUG
275 /* Update the remaining budget */
276 master_clock->remaining_budget = master_clock->frame_budget;
277 #endif
278
279 stages = g_hash_table_lookup (master_clock->clock_to_stage, frame_clock);
280 CLUTTER_NOTE (SCHEDULER, "Updating %d stages tied to frame clock %p",
281 g_list_length (stages), frame_clock);
•282 for (l = stages; l != NULL; l = next)
283 {
284 ClutterStage *stage = l->data;
285
286 CLUTTER_NOTE (SCHEDULER, "Master clock (stage:%p, clock:%p) [tick]", stage, frame_clock);
287
288 /* Each frame is split into three separate phases: */
289
290 /* 1. process all the events; goes through the stage's event queue
291 * and processes each event according to its type, then emits the
292 * various signals that are associated with the event
293 */
294 master_clock_process_stage_events (master_clock, stage);
295
296 /* 2. advance the timelines */
297 master_clock_advance_timelines (master_clock);
298
299 /* 3. relayout and redraw the stage; the stage might have been
300 * destroyed in 1. when processing events, check whether it's
301 * still alive.
302 */
303
304 if (g_hash_table_lookup (master_clock->stage_to_clock, stage) != NULL)
305 {
306 master_clock_update_stage (master_clock, stage);
•307
308 if (g_hash_table_lookup (master_clock->stage_to_clock, stage) != NULL)
309 {
310 master_clock_schedule_stage_update (master_clock, stage, frame_clock);
311 next = l->next;
312 }
313 else
314 {
315 next = g_hash_table_lookup (master_clock->clock_to_stage, frame_clock);
316 }
317 }
318 else
319 {
320 next = g_hash_table_lookup (master_clock->clock_to_stage, frame_clock);
321 }
322 }
323
324 master_clock->prev_tick = master_clock->cur_tick;
325
326 _clutter_threads_release_lock ();
327 }
328
329 static void
330 clutter_master_clock_gdk_remove_stage_clock (ClutterMasterClockGdk *master_clock,
331 ClutterStage *stage)
332 {
333 gpointer frame_clock = g_hash_table_lookup (master_clock->stage_to_clock, stage);
334 GList *stages;
335
336 if (frame_clock == NULL)
337 return;
338
339 CLUTTER_NOTE (SCHEDULER, "Removing stage %p with clock %p", stage, frame_clock);
340
341 g_hash_table_remove (master_clock->stage_to_clock, stage);
342
343 stages = g_hash_table_lookup (master_clock->clock_to_stage, frame_clock);
344 if (stages != NULL)
345 {
346 if (stages->next == NULL)
347 {
348 /* Deleting the last stage linked to a given clock. We can stop
349 listening to that clock and also tell the clock we're finish
350 updating it. */
351 if (GPOINTER_TO_UINT (g_object_get_data (frame_clock,
352 "clutter-master-clock-updating")))
353 {
354 gdk_frame_clock_end_updating (GDK_FRAME_CLOCK (frame_clock));
355 g_object_set_data (frame_clock, "clutter-master-clock-updating", NULL);
356 }
357 g_signal_handlers_disconnect_by_func (frame_clock,
358 clutter_master_clock_gdk_update,
359 master_clock);
360
361 g_hash_table_remove (master_clock->clock_to_stage, frame_clock);
362 g_list_free (stages);
363 }
364 else
365 {
366 stages = g_list_remove (stages, stage);
367 g_hash_table_replace (master_clock->clock_to_stage,
368 g_object_ref (frame_clock),
369 stages);
370 }
371 }
372 }
373
374 static void
375 clutter_master_clock_gdk_add_stage_clock (ClutterMasterClockGdk *master_clock,
376 ClutterStage *stage,
377 GdkFrameClock *frame_clock)
378 {
379 GList *stages;
380
381 clutter_master_clock_gdk_remove_stage_clock (master_clock, stage);
382
383 CLUTTER_NOTE (SCHEDULER, "Adding stage %p with clock %p", stage, frame_clock);
384
385 g_hash_table_insert (master_clock->stage_to_clock, stage, g_object_ref (frame_clock));
386
387 stages = g_hash_table_lookup (master_clock->clock_to_stage, frame_clock);
388 if (stages == NULL)
389 {
390 g_hash_table_insert (master_clock->clock_to_stage, g_object_ref (frame_clock),
391 g_list_append (NULL, stage));
392
393 g_signal_connect (frame_clock, "paint",
394 G_CALLBACK (clutter_master_clock_gdk_update),
395 master_clock);
396 }
397 else
398 stages = g_list_append (stages, stage);
399
400 if (master_clock->timelines != NULL)
401 {
402 _clutter_master_clock_start_running ((ClutterMasterClock *) master_clock);
403 /* We only need to synchronize the frame clock state if we have
404 timelines running. */
405 master_clock_sync_frame_clock_update (master_clock);
406 }
407 }
408
409 static void
410 clutter_master_clock_gdk_listen_to_stage (ClutterMasterClockGdk *master_clock,
411 ClutterStage *stage)
412 {
413 ClutterStageWindow *stage_window;
414 ClutterStageGdk *stage_window_gdk;
415 GdkFrameClock *frame_clock;
416
417 stage_window = _clutter_stage_get_window (stage);
418 if (stage_window == NULL)
419 {
420 clutter_master_clock_gdk_remove_stage_clock (master_clock, stage);
421 return;
422 }
423
424 stage_window_gdk = CLUTTER_STAGE_GDK (stage_window);
425 if (stage_window_gdk->window == NULL)
426 {
427 clutter_master_clock_gdk_remove_stage_clock (master_clock, stage);
428 return;
429 }
430
431 frame_clock = gdk_window_get_frame_clock (stage_window_gdk->window);
432 if (frame_clock == NULL)
433 {
434 clutter_master_clock_gdk_remove_stage_clock (master_clock, stage);
435 return;
436 }
437
438 clutter_master_clock_gdk_add_stage_clock (master_clock, stage, frame_clock);
439 }
440
441 static void
442 clutter_master_clock_gdk_stage_visibility (ClutterStage *stage,
443 GParamSpec *spec,
444 ClutterMasterClockGdk *master_clock)
445 {
446 ClutterActor *actor = CLUTTER_ACTOR (stage);
447 if (clutter_actor_is_mapped (actor))
448 clutter_master_clock_gdk_listen_to_stage (master_clock, stage);
449 else
450 clutter_master_clock_gdk_remove_stage_clock (master_clock, stage);
451 }
452
453 static void
454 clutter_master_clock_gdk_stage_added (ClutterStageManager *manager,
455 ClutterStage *stage,
456 ClutterMasterClockGdk *master_clock)
457 {
458 g_signal_connect (stage, "notify::mapped",
459 G_CALLBACK (clutter_master_clock_gdk_stage_visibility),
460 master_clock);
461
462 clutter_master_clock_gdk_stage_visibility (stage, NULL, master_clock);
463 }
464
465 static void
466 clutter_master_clock_gdk_stage_removed (ClutterStageManager *manager,
467 ClutterStage *stage,
468 ClutterMasterClockGdk *master_clock)
469 {
470 clutter_master_clock_gdk_remove_stage_clock (master_clock, stage);
471
472 g_signal_handlers_disconnect_by_func (stage,
473 clutter_master_clock_gdk_stage_visibility,
474 master_clock);
475 }
476
477 static void
478 clutter_master_clock_gdk_dispose (GObject *gobject)
479 {
480 ClutterStageManager *manager = clutter_stage_manager_get_default ();
481
482 g_signal_handlers_disconnect_by_func (manager,
483 clutter_master_clock_gdk_stage_added,
484 gobject);
485 g_signal_handlers_disconnect_by_func (manager,
486 clutter_master_clock_gdk_stage_removed,
487 gobject);
488
489 G_OBJECT_CLASS (clutter_master_clock_gdk_parent_class)->dispose (gobject);
490 }
491
492 static void
493 clutter_master_clock_gdk_finalize (GObject *gobject)
494 {
495 ClutterMasterClockGdk *master_clock = CLUTTER_MASTER_CLOCK_GDK (gobject);
496
497 g_hash_table_unref (master_clock->clock_to_stage);
498 g_hash_table_unref (master_clock->stage_to_clock);
499 g_slist_free (master_clock->timelines);
500
501 G_OBJECT_CLASS (clutter_master_clock_gdk_parent_class)->finalize (gobject);
502 }
503
504 static void
505 clutter_master_clock_gdk_class_init (ClutterMasterClockGdkClass *klass)
506 {
507 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
508
509 gobject_class->dispose = clutter_master_clock_gdk_dispose;
510 gobject_class->finalize = clutter_master_clock_gdk_finalize;
511 }
512
513 static void
514 clutter_master_clock_gdk_init (ClutterMasterClockGdk *self)
515 {
516 ClutterStageManager *manager;
517 const GSList *stages, *l;
518
519 #ifdef CLUTTER_ENABLE_DEBUG
520 self->frame_budget = G_USEC_PER_SEC / 60;
521 #endif
522
523 self->clock_to_stage = g_hash_table_new_full (g_direct_hash, g_direct_equal,
524 g_object_unref, NULL);
525 self->stage_to_clock = g_hash_table_new_full (g_direct_hash, g_direct_equal,
526 NULL, g_object_unref);
527
528 manager = clutter_stage_manager_get_default ();
529 g_signal_connect (manager, "stage-added",
530 G_CALLBACK (clutter_master_clock_gdk_stage_added), self);
531 g_signal_connect (manager, "stage-removed",
532 G_CALLBACK (clutter_master_clock_gdk_stage_removed), self);
533
534 stages = clutter_stage_manager_peek_stages (manager);
535 for (l = stages; l; l = l->next)
536 clutter_master_clock_gdk_stage_added (manager, l->data, self);
537
538 if (G_UNLIKELY (clutter_paint_debug_flags & CLUTTER_DEBUG_CONTINUOUS_REDRAW))
539 g_warning ("Continuous redraw is not supported with the GDK backend.");
540 }
541
542 static void
543 clutter_master_clock_gdk_add_timeline (ClutterMasterClock *clock,
544 ClutterTimeline *timeline)
545 {
546 ClutterMasterClockGdk *master_clock = (ClutterMasterClockGdk *) clock;
547 gboolean is_first;
548
549 if (g_slist_find (master_clock->timelines, timeline))
550 return;
551
552 is_first = master_clock->timelines == NULL;
553
554 master_clock->timelines = g_slist_prepend (master_clock->timelines,
555 timeline);
556
557 if (is_first)
558 {
559 _clutter_master_clock_start_running (clock);
560 /* Sync frame clock update state if needed. */
561 master_clock_sync_frame_clock_update (master_clock);
562 }
563 }
564
565 static void
566 clutter_master_clock_gdk_remove_timeline (ClutterMasterClock *clock,
567 ClutterTimeline *timeline)
568 {
569 ClutterMasterClockGdk *master_clock = (ClutterMasterClockGdk *) clock;
570
571 master_clock->timelines = g_slist_remove (master_clock->timelines,
572 timeline);
573
574 /* Sync frame clock update state if we have no more timelines running. */
575 if (master_clock->timelines == NULL)
576 master_clock_sync_frame_clock_update (master_clock);
577 }
578
579 static void
580 clutter_master_clock_gdk_start_running (ClutterMasterClock *clock)
581 {
582 master_clock_schedule_forced_stages_updates ((ClutterMasterClockGdk *) clock);
583 }
584
585 static void
586 clutter_master_clock_gdk_ensure_next_iteration (ClutterMasterClock *clock)
587 {
588 master_clock_schedule_forced_stages_updates ((ClutterMasterClockGdk *) clock);
589 }
590
591 static void
592 clutter_master_clock_gdk_set_paused (ClutterMasterClock *clock,
593 gboolean paused)
594 {
595 /* GdkFrameClock runs the show here. We do not decide whether the
596 clock is paused or not. */
597 }
598
599 static void
600 clutter_master_clock_iface_init (ClutterMasterClockIface *iface)
601 {
602 iface->add_timeline = clutter_master_clock_gdk_add_timeline;
603 iface->remove_timeline = clutter_master_clock_gdk_remove_timeline;
604 iface->start_running = clutter_master_clock_gdk_start_running;
605 iface->ensure_next_iteration = clutter_master_clock_gdk_ensure_next_iteration;
606 iface->set_paused = clutter_master_clock_gdk_set_paused;
607 }