Debian Codegraph @

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

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
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;
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 = 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 master_clock_schedule_stage_update (master_clock, stage, frame_clock);
308 }
309 }
310
311 master_clock->prev_tick = master_clock->cur_tick;
312
313 _clutter_threads_release_lock ();
314 }
315
316 static void
317 clutter_master_clock_gdk_remove_stage_clock (ClutterMasterClockGdk *master_clock,
318 ClutterStage *stage)
319 {
320 gpointer frame_clock = g_hash_table_lookup (master_clock->stage_to_clock, stage);
321 GList *stages;
322
323 if (frame_clock == NULL)
324 return;
325
326 CLUTTER_NOTE (SCHEDULER, "Removing stage %p with clock %p", stage, frame_clock);
327
328 g_hash_table_remove (master_clock->stage_to_clock, stage);
329
330 stages = g_hash_table_lookup (master_clock->clock_to_stage, frame_clock);
331 if (stages != NULL)
332 {
333 if (stages->next == NULL)
334 {
335 /* Deleting the last stage linked to a given clock. We can stop
336 listening to that clock and also tell the clock we're finish
337 updating it. */
338 if (GPOINTER_TO_UINT (g_object_get_data (frame_clock,
339 "clutter-master-clock-updating")))
340 {
341 gdk_frame_clock_end_updating (GDK_FRAME_CLOCK (frame_clock));
342 g_object_set_data (frame_clock, "clutter-master-clock-updating", NULL);
343 }
344 g_signal_handlers_disconnect_by_func (frame_clock,
345 clutter_master_clock_gdk_update,
346 master_clock);
347
348 g_hash_table_remove (master_clock->clock_to_stage, frame_clock);
349 g_list_free (stages);
350 }
351 else
352 {
353 stages = g_list_remove (stages, stage);
354 g_hash_table_replace (master_clock->clock_to_stage,
355 g_object_ref (frame_clock),
356 stages);
357 }
358 }
359 }
360
361 static void
362 clutter_master_clock_gdk_add_stage_clock (ClutterMasterClockGdk *master_clock,
363 ClutterStage *stage,
364 GdkFrameClock *frame_clock)
365 {
366 GList *stages;
367
368 clutter_master_clock_gdk_remove_stage_clock (master_clock, stage);
369
370 CLUTTER_NOTE (SCHEDULER, "Adding stage %p with clock %p", stage, frame_clock);
371
372 g_hash_table_insert (master_clock->stage_to_clock, stage, g_object_ref (frame_clock));
373
374 stages = g_hash_table_lookup (master_clock->clock_to_stage, frame_clock);
375 if (stages == NULL)
376 {
377 g_hash_table_insert (master_clock->clock_to_stage, g_object_ref (frame_clock),
378 g_list_append (NULL, stage));
379
380 g_signal_connect (frame_clock, "paint",
381 G_CALLBACK (clutter_master_clock_gdk_update),
382 master_clock);
383 }
384 else
385 stages = g_list_append (stages, stage);
386
387 if (master_clock->timelines != NULL)
388 {
389 _clutter_master_clock_start_running ((ClutterMasterClock *) master_clock);
390 /* We only need to synchronize the frame clock state if we have
391 timelines running. */
392 master_clock_sync_frame_clock_update (master_clock);
393 }
394 }
395
396 static void
397 clutter_master_clock_gdk_listen_to_stage (ClutterMasterClockGdk *master_clock,
398 ClutterStage *stage)
399 {
400 ClutterStageWindow *stage_window;
401 ClutterStageGdk *stage_window_gdk;
402 GdkFrameClock *frame_clock;
403
404 stage_window = _clutter_stage_get_window (stage);
405 if (stage_window == NULL)
406 {
407 clutter_master_clock_gdk_remove_stage_clock (master_clock, stage);
408 return;
409 }
410
411 stage_window_gdk = CLUTTER_STAGE_GDK (stage_window);
412 if (stage_window_gdk->window == NULL)
413 {
414 clutter_master_clock_gdk_remove_stage_clock (master_clock, stage);
415 return;
416 }
417
418 frame_clock = gdk_window_get_frame_clock (stage_window_gdk->window);
419 if (frame_clock == NULL)
420 {
421 clutter_master_clock_gdk_remove_stage_clock (master_clock, stage);
422 return;
423 }
424
425 clutter_master_clock_gdk_add_stage_clock (master_clock, stage, frame_clock);
426 }
427
428 static void
429 clutter_master_clock_gdk_stage_visibility (ClutterStage *stage,
430 GParamSpec *spec,
431 ClutterMasterClockGdk *master_clock)
432 {
433 ClutterActor *actor = CLUTTER_ACTOR (stage);
434 if (clutter_actor_is_mapped (actor))
435 clutter_master_clock_gdk_listen_to_stage (master_clock, stage);
436 else
437 clutter_master_clock_gdk_remove_stage_clock (master_clock, stage);
438 }
439
440 static void
441 clutter_master_clock_gdk_stage_added (ClutterStageManager *manager,
442 ClutterStage *stage,
443 ClutterMasterClockGdk *master_clock)
444 {
445 g_signal_connect (stage, "notify::mapped",
446 G_CALLBACK (clutter_master_clock_gdk_stage_visibility),
447 master_clock);
448
449 clutter_master_clock_gdk_stage_visibility (stage, NULL, master_clock);
450 }
451
452 static void
453 clutter_master_clock_gdk_stage_removed (ClutterStageManager *manager,
454 ClutterStage *stage,
455 ClutterMasterClockGdk *master_clock)
456 {
457 clutter_master_clock_gdk_remove_stage_clock (master_clock, stage);
458
459 g_signal_handlers_disconnect_by_func (stage,
460 clutter_master_clock_gdk_stage_visibility,
461 master_clock);
462 }
463
464 static void
465 clutter_master_clock_gdk_dispose (GObject *gobject)
466 {
467 ClutterStageManager *manager = clutter_stage_manager_get_default ();
468
469 g_signal_handlers_disconnect_by_func (manager,
470 clutter_master_clock_gdk_stage_added,
471 gobject);
472 g_signal_handlers_disconnect_by_func (manager,
473 clutter_master_clock_gdk_stage_removed,
474 gobject);
475
476 G_OBJECT_CLASS (clutter_master_clock_gdk_parent_class)->dispose (gobject);
477 }
478
479 static void
480 clutter_master_clock_gdk_finalize (GObject *gobject)
481 {
482 ClutterMasterClockGdk *master_clock = CLUTTER_MASTER_CLOCK_GDK (gobject);
483
484 g_hash_table_unref (master_clock->clock_to_stage);
485 g_hash_table_unref (master_clock->stage_to_clock);
486 g_slist_free (master_clock->timelines);
487
488 G_OBJECT_CLASS (clutter_master_clock_gdk_parent_class)->finalize (gobject);
489 }
490
491 static void
492 clutter_master_clock_gdk_class_init (ClutterMasterClockGdkClass *klass)
493 {
494 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
495
496 gobject_class->dispose = clutter_master_clock_gdk_dispose;
497 gobject_class->finalize = clutter_master_clock_gdk_finalize;
498 }
499
500 static void
501 clutter_master_clock_gdk_init (ClutterMasterClockGdk *self)
502 {
503 ClutterStageManager *manager;
504 const GSList *stages, *l;
505
506 #ifdef CLUTTER_ENABLE_DEBUG
507 self->frame_budget = G_USEC_PER_SEC / 60;
508 #endif
509
510 self->clock_to_stage = g_hash_table_new_full (g_direct_hash, g_direct_equal,
511 g_object_unref, NULL);
512 self->stage_to_clock = g_hash_table_new_full (g_direct_hash, g_direct_equal,
513 NULL, g_object_unref);
514
515 manager = clutter_stage_manager_get_default ();
516 g_signal_connect (manager, "stage-added",
517 G_CALLBACK (clutter_master_clock_gdk_stage_added), self);
518 g_signal_connect (manager, "stage-removed",
519 G_CALLBACK (clutter_master_clock_gdk_stage_removed), self);
520
521 stages = clutter_stage_manager_peek_stages (manager);
522 for (l = stages; l; l = l->next)
523 clutter_master_clock_gdk_stage_added (manager, l->data, self);
524
525 if (G_UNLIKELY (clutter_paint_debug_flags & CLUTTER_DEBUG_CONTINUOUS_REDRAW))
526 g_warning ("Continuous redraw is not supported with the GDK backend.");
527 }
528
529 static void
530 clutter_master_clock_gdk_add_timeline (ClutterMasterClock *clock,
531 ClutterTimeline *timeline)
532 {
533 ClutterMasterClockGdk *master_clock = (ClutterMasterClockGdk *) clock;
534 gboolean is_first;
535
536 if (g_slist_find (master_clock->timelines, timeline))
537 return;
538
539 is_first = master_clock->timelines == NULL;
540
541 master_clock->timelines = g_slist_prepend (master_clock->timelines,
542 timeline);
543
544 if (is_first)
545 {
546 _clutter_master_clock_start_running (clock);
547 /* Sync frame clock update state if needed. */
548 master_clock_sync_frame_clock_update (master_clock);
549 }
550 }
551
552 static void
553 clutter_master_clock_gdk_remove_timeline (ClutterMasterClock *clock,
554 ClutterTimeline *timeline)
555 {
556 ClutterMasterClockGdk *master_clock = (ClutterMasterClockGdk *) clock;
557
558 master_clock->timelines = g_slist_remove (master_clock->timelines,
559 timeline);
560
561 /* Sync frame clock update state if we have no more timelines running. */
562 if (master_clock->timelines == NULL)
563 master_clock_sync_frame_clock_update (master_clock);
564 }
565
566 static void
567 clutter_master_clock_gdk_start_running (ClutterMasterClock *clock)
568 {
569 master_clock_schedule_forced_stages_updates ((ClutterMasterClockGdk *) clock);
570 }
571
572 static void
573 clutter_master_clock_gdk_ensure_next_iteration (ClutterMasterClock *clock)
574 {
575 master_clock_schedule_forced_stages_updates ((ClutterMasterClockGdk *) clock);
576 }
577
578 static void
579 clutter_master_clock_gdk_set_paused (ClutterMasterClock *clock,
580 gboolean paused)
581 {
582 /* GdkFrameClock runs the show here. We do not decide whether the
583 clock is paused or not. */
584 }
585
586 static void
587 clutter_master_clock_iface_init (ClutterMasterClockIface *iface)
588 {
589 iface->add_timeline = clutter_master_clock_gdk_add_timeline;
590 iface->remove_timeline = clutter_master_clock_gdk_remove_timeline;
591 iface->start_running = clutter_master_clock_gdk_start_running;
592 iface->ensure_next_iteration = clutter_master_clock_gdk_ensure_next_iteration;
593 iface->set_paused = clutter_master_clock_gdk_set_paused;
594 }