summaryrefslogtreecommitdiff
path: root/src/tests/clutter/conform/frame-clock.c
blob: 810c39a02cbb811bf998fee713b09ea9e8796c20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
#include "clutter/clutter.h"
#include "tests/clutter-test-utils.h"

static const float refresh_rate = 60.0;
static const int64_t refresh_interval_us = (int64_t) (0.5 + G_USEC_PER_SEC /
                                                      refresh_rate);

static int64_t test_frame_count;
static int64_t expected_frame_count;

typedef struct _FakeHwClock
{
  GSource source;

  ClutterFrameClock *frame_clock;

  int64_t next_presentation_time_us;
  gboolean has_pending_present;
} FakeHwClock;

typedef struct _FrameClockTest
{
  FakeHwClock *fake_hw_clock;

  GMainLoop *main_loop;
} FrameClockTest;

static void
init_frame_info (ClutterFrameInfo *frame_info,
                 int64_t           presentation_time_us)
{
  *frame_info = (ClutterFrameInfo) {
    .presentation_time = presentation_time_us,
    .refresh_rate = refresh_rate,
    .flags = CLUTTER_FRAME_INFO_FLAG_NONE,
    .sequence = 0,
  };
}

static gboolean
fake_hw_clock_source_dispatch (GSource     *source,
                               GSourceFunc  callback,
                               gpointer     user_data)
{
  FakeHwClock *fake_hw_clock = (FakeHwClock *) source;
  ClutterFrameClock *frame_clock = fake_hw_clock->frame_clock;

  if (fake_hw_clock->has_pending_present)
    {
      ClutterFrameInfo frame_info;

      fake_hw_clock->has_pending_present = FALSE;
      init_frame_info (&frame_info, g_source_get_time (source));
      clutter_frame_clock_notify_presented (frame_clock, &frame_info);
      if (callback)
        callback (user_data);
    }

  fake_hw_clock->next_presentation_time_us += refresh_interval_us;
  g_source_set_ready_time (source, fake_hw_clock->next_presentation_time_us);

  return G_SOURCE_CONTINUE;
}

static GSourceFuncs fake_hw_clock_source_funcs = {
  NULL,
  NULL,
  fake_hw_clock_source_dispatch,
  NULL
};

static FakeHwClock *
fake_hw_clock_new (ClutterFrameClock *frame_clock,
                   GSourceFunc        callback,
                   gpointer           user_data)
{
  GSource *source;
  FakeHwClock *fake_hw_clock;

  source = g_source_new (&fake_hw_clock_source_funcs, sizeof (FakeHwClock));
  fake_hw_clock = (FakeHwClock *) source;
  fake_hw_clock->frame_clock = frame_clock;

  fake_hw_clock->next_presentation_time_us =
    g_get_monotonic_time () + refresh_interval_us;
  g_source_set_ready_time (source, fake_hw_clock->next_presentation_time_us);
  g_source_set_callback (source, callback, user_data, NULL);

  return fake_hw_clock;
}

static ClutterFrameResult
frame_clock_frame (ClutterFrameClock *frame_clock,
                   int64_t            frame_count,
                   int64_t            time_us,
                   gpointer           user_data)
{
  FrameClockTest *test = user_data;
  GMainLoop *main_loop = test->main_loop;

  g_assert_cmpint (frame_count, ==, expected_frame_count);

  expected_frame_count++;

  if (test_frame_count == 0)
    {
      g_main_loop_quit (main_loop);
      return CLUTTER_FRAME_RESULT_IDLE;
    }
  else
    {
      test->fake_hw_clock->has_pending_present = TRUE;
    }

  test_frame_count--;

  return CLUTTER_FRAME_RESULT_PENDING_PRESENTED;
}

static const ClutterFrameListenerIface frame_listener_iface = {
  .frame = frame_clock_frame,
};

static gboolean
schedule_update_hw_callback (gpointer user_data)
{
  ClutterFrameClock *frame_clock = user_data;

  clutter_frame_clock_schedule_update (frame_clock);

  return G_SOURCE_CONTINUE;
}

static void
frame_clock_schedule_update (void)
{
  FrameClockTest test;
  ClutterFrameClock *frame_clock;
  int64_t before_us;
  int64_t after_us;
  GSource *source;
  FakeHwClock *fake_hw_clock;

  test_frame_count = 10;
  expected_frame_count = 0;

  test.main_loop = g_main_loop_new (NULL, FALSE);
  frame_clock = clutter_frame_clock_new (refresh_rate,
                                         0,
                                         &frame_listener_iface,
                                         &test);

  fake_hw_clock = fake_hw_clock_new (frame_clock,
                                     schedule_update_hw_callback,
                                     frame_clock);
  source = &fake_hw_clock->source;
  g_source_attach (source, NULL);

  test.fake_hw_clock = fake_hw_clock;

  before_us = g_get_monotonic_time ();

  clutter_frame_clock_schedule_update (frame_clock);
  g_main_loop_run (test.main_loop);

  after_us = g_get_monotonic_time ();

  g_assert_cmpint (after_us - before_us, >, 10 * refresh_interval_us);

  g_main_loop_unref (test.main_loop);

  clutter_frame_clock_destroy (frame_clock);
  g_source_destroy (source);
  g_source_unref (source);
}

static gboolean
schedule_update_idle (gpointer user_data)
{
  ClutterFrameClock *frame_clock = user_data;

  clutter_frame_clock_schedule_update (frame_clock);

  return G_SOURCE_REMOVE;
}

static ClutterFrameResult
immediate_frame_clock_frame (ClutterFrameClock *frame_clock,
                             int64_t            frame_count,
                             int64_t            time_us,
                             gpointer           user_data)
{
  GMainLoop *main_loop = user_data;
  ClutterFrameInfo frame_info;

  g_assert_cmpint (frame_count, ==, expected_frame_count);

  expected_frame_count++;

  if (test_frame_count == 0)
    {
      g_main_loop_quit (main_loop);
      return CLUTTER_FRAME_RESULT_IDLE;
    }

  test_frame_count--;

  init_frame_info (&frame_info, g_get_monotonic_time ());
  clutter_frame_clock_notify_presented (frame_clock, &frame_info);
  g_idle_add (schedule_update_idle, frame_clock);

  return CLUTTER_FRAME_RESULT_PENDING_PRESENTED;
}

static const ClutterFrameListenerIface immediate_frame_listener_iface = {
  .frame = immediate_frame_clock_frame,
};

static void
frame_clock_immediate_present (void)
{
  GMainLoop *main_loop;
  ClutterFrameClock *frame_clock;
  int64_t before_us;
  int64_t after_us;

  test_frame_count = 10;
  expected_frame_count = 0;

  main_loop = g_main_loop_new (NULL, FALSE);
  frame_clock = clutter_frame_clock_new (refresh_rate,
                                         0,
                                         &immediate_frame_listener_iface,
                                         main_loop);

  before_us = g_get_monotonic_time ();

  clutter_frame_clock_schedule_update (frame_clock);
  g_main_loop_run (main_loop);

  after_us = g_get_monotonic_time ();

  /* The initial frame will only be delayed by 2 ms, so we are checking one
   * less.
   */
  g_assert_cmpint (after_us - before_us, >, 9 * refresh_interval_us);

  g_main_loop_unref (main_loop);
  clutter_frame_clock_destroy (frame_clock);
}

static gboolean
schedule_update_timeout (gpointer user_data)
{
  ClutterFrameClock *frame_clock = user_data;

  clutter_frame_clock_schedule_update (frame_clock);

  return G_SOURCE_REMOVE;
}

static ClutterFrameResult
delayed_damage_frame_clock_frame (ClutterFrameClock *frame_clock,
                                  int64_t            frame_count,
                                  int64_t            time_us,
                                  gpointer           user_data)
{
  FrameClockTest *test = user_data;
  GMainLoop *main_loop = test->main_loop;

  g_assert_cmpint (frame_count, ==, expected_frame_count);

  expected_frame_count++;

  if (test_frame_count == 0)
    {
      g_main_loop_quit (main_loop);
      return CLUTTER_FRAME_RESULT_IDLE;
    }
  else
    {
      test->fake_hw_clock->has_pending_present = TRUE;
    }

  test_frame_count--;

  g_timeout_add (100, schedule_update_timeout, frame_clock);

  return CLUTTER_FRAME_RESULT_PENDING_PRESENTED;
}

static const ClutterFrameListenerIface delayed_damage_frame_listener_iface = {
  .frame = delayed_damage_frame_clock_frame,
};

static void
frame_clock_delayed_damage (void)
{
  FrameClockTest test;
  ClutterFrameClock *frame_clock;
  int64_t before_us;
  int64_t after_us;
  FakeHwClock *fake_hw_clock;
  GSource *source;

  test_frame_count = 2;
  expected_frame_count = 0;

  test.main_loop = g_main_loop_new (NULL, FALSE);
  frame_clock = clutter_frame_clock_new (refresh_rate,
                                         0,
                                         &delayed_damage_frame_listener_iface,
                                         &test);

  fake_hw_clock = fake_hw_clock_new (frame_clock, NULL, NULL);
  source = &fake_hw_clock->source;
  g_source_attach (source, NULL);

  test.fake_hw_clock = fake_hw_clock;

  before_us = g_get_monotonic_time ();

  clutter_frame_clock_schedule_update (frame_clock);
  g_main_loop_run (test.main_loop);

  after_us = g_get_monotonic_time ();

  g_assert_cmpint (after_us - before_us, >, 100000 + refresh_interval_us);

  g_main_loop_unref (test.main_loop);
  clutter_frame_clock_destroy (frame_clock);
  g_source_destroy (source);
  g_source_unref (source);
}

static ClutterFrameResult
no_damage_frame_clock_frame (ClutterFrameClock *frame_clock,
                             int64_t            frame_count,
                             int64_t            time_us,
                             gpointer           user_data)
{
  g_assert_not_reached ();

  return CLUTTER_FRAME_RESULT_PENDING_PRESENTED;
}

static const ClutterFrameListenerIface no_damage_frame_listener_iface = {
  .frame = no_damage_frame_clock_frame,
};

static gboolean
quit_main_loop_idle (gpointer user_data)
{
  GMainLoop *main_loop = user_data;

  g_main_loop_quit (main_loop);

  return G_SOURCE_REMOVE;
}

static void
frame_clock_no_damage (void)
{
  GMainLoop *main_loop;
  ClutterFrameClock *frame_clock;

  test_frame_count = 10;
  expected_frame_count = 0;

  main_loop = g_main_loop_new (NULL, FALSE);
  frame_clock = clutter_frame_clock_new (refresh_rate,
                                         0,
                                         &no_damage_frame_listener_iface,
                                         NULL);

  g_timeout_add (100, quit_main_loop_idle, main_loop);

  g_main_loop_run (main_loop);

  g_main_loop_unref (main_loop);
  clutter_frame_clock_destroy (frame_clock);
}

typedef struct _UpdateNowFrameClockTest
{
  FrameClockTest base;
  guint idle_source_id;
} UpdateNowFrameClockTest;

static ClutterFrameResult
update_now_frame_clock_frame (ClutterFrameClock *frame_clock,
                              int64_t            frame_count,
                              int64_t            time_us,
                              gpointer           user_data)
{
  UpdateNowFrameClockTest *test = user_data;
  GMainLoop *main_loop = test->base.main_loop;

  g_assert_cmpint (frame_count, ==, expected_frame_count);

  expected_frame_count++;

  g_clear_handle_id (&test->idle_source_id, g_source_remove);

  if (test_frame_count == 0)
    {
      g_main_loop_quit (main_loop);
      return CLUTTER_FRAME_RESULT_IDLE;
    }
  else
    {
      test->base.fake_hw_clock->has_pending_present = TRUE;
    }

  test_frame_count--;

  return CLUTTER_FRAME_RESULT_PENDING_PRESENTED;
}

static const ClutterFrameListenerIface update_now_frame_listener_iface = {
  .frame = update_now_frame_clock_frame,
};

static gboolean
assert_not_reached_idle (gpointer user_data)
{
  g_assert_not_reached ();
  return G_SOURCE_REMOVE;
}

static gboolean
schedule_update_now_hw_callback (gpointer user_data)
{
  UpdateNowFrameClockTest *test = user_data;
  ClutterFrameClock *frame_clock = test->base.fake_hw_clock->frame_clock;

  clutter_frame_clock_schedule_update_now (frame_clock);
  g_assert (!test->idle_source_id);
  test->idle_source_id = g_idle_add (assert_not_reached_idle, NULL);

  return G_SOURCE_CONTINUE;
}

static void
frame_clock_schedule_update_now (void)
{
  UpdateNowFrameClockTest test = { 0 };
  ClutterFrameClock *frame_clock;
  int64_t before_us;
  int64_t after_us;
  GSource *source;
  FakeHwClock *fake_hw_clock;

  test_frame_count = 10;
  expected_frame_count = 0;

  test.base.main_loop = g_main_loop_new (NULL, FALSE);
  frame_clock = clutter_frame_clock_new (refresh_rate,
                                         0,
                                         &update_now_frame_listener_iface,
                                         &test);

  fake_hw_clock = fake_hw_clock_new (frame_clock,
                                     schedule_update_now_hw_callback,
                                     &test);
  source = &fake_hw_clock->source;
  g_source_attach (source, NULL);

  test.base.fake_hw_clock = fake_hw_clock;

  before_us = g_get_monotonic_time ();

  clutter_frame_clock_schedule_update (frame_clock);
  g_main_loop_run (test.base.main_loop);

  after_us = g_get_monotonic_time ();

  g_assert_cmpint (after_us - before_us, >, 10 * refresh_interval_us);

  g_main_loop_unref (test.base.main_loop);

  clutter_frame_clock_destroy (frame_clock);
  g_source_destroy (source);
  g_source_unref (source);
}

static void
before_frame_frame_clock_before_frame (ClutterFrameClock *frame_clock,
                                       int64_t            frame_count,
                                       gpointer           user_data)
{
  int64_t *expected_frame_count = user_data;

  g_assert_cmpint (*expected_frame_count, ==, frame_count);
}

static ClutterFrameResult
before_frame_frame_clock_frame (ClutterFrameClock *frame_clock,
                                int64_t            frame_count,
                                int64_t            time_us,
                                gpointer           user_data)
{
  int64_t *expected_frame_count = user_data;
  ClutterFrameInfo frame_info;

  g_assert_cmpint (*expected_frame_count, ==, frame_count);

  (*expected_frame_count)++;

  init_frame_info (&frame_info, g_get_monotonic_time ());
  clutter_frame_clock_notify_presented (frame_clock, &frame_info);
  clutter_frame_clock_schedule_update (frame_clock);

  return CLUTTER_FRAME_RESULT_PENDING_PRESENTED;
}

static const ClutterFrameListenerIface before_frame_frame_listener_iface = {
  .before_frame = before_frame_frame_clock_before_frame,
  .frame = before_frame_frame_clock_frame,
};

static gboolean
quit_main_loop_timeout (gpointer user_data)
{
  GMainLoop *main_loop = user_data;

  g_main_loop_quit (main_loop);

  return G_SOURCE_REMOVE;
}

static void
frame_clock_before_frame (void)
{
  GMainLoop *main_loop;
  ClutterFrameClock *frame_clock;

  expected_frame_count = 0;

  main_loop = g_main_loop_new (NULL, FALSE);
  frame_clock = clutter_frame_clock_new (refresh_rate,
                                         0,
                                         &before_frame_frame_listener_iface,
                                         &expected_frame_count);

  clutter_frame_clock_schedule_update (frame_clock);
  g_timeout_add (100, quit_main_loop_timeout, main_loop);
  g_main_loop_run (main_loop);

  /* We should have at least processed a couple of frames within 100 ms. */
  g_assert_cmpint (expected_frame_count, >, 2);

  g_main_loop_unref (main_loop);
  clutter_frame_clock_destroy (frame_clock);
}

typedef struct _InhibitTest
{
  GMainLoop *main_loop;
  ClutterFrameClock *frame_clock;

  gboolean frame_count;
  gboolean pending_inhibit;
  gboolean pending_quit;
} InhibitTest;

static ClutterFrameResult
inhibit_frame_clock_frame (ClutterFrameClock *frame_clock,
                           int64_t            frame_count,
                           int64_t            time_us,
                           gpointer           user_data)
{
  InhibitTest *test = user_data;
  ClutterFrameInfo frame_info;

  g_assert_cmpint (frame_count, ==, test->frame_count);

  test->frame_count++;

  init_frame_info (&frame_info, g_get_monotonic_time ());
  clutter_frame_clock_notify_presented (frame_clock, &frame_info);
  clutter_frame_clock_schedule_update (frame_clock);

  if (test->pending_inhibit)
    {
      test->pending_inhibit = FALSE;
      clutter_frame_clock_inhibit (frame_clock);
    }

  clutter_frame_clock_schedule_update (frame_clock);

  if (test->pending_quit)
    g_main_loop_quit (test->main_loop);

  return CLUTTER_FRAME_RESULT_PENDING_PRESENTED;
}

static const ClutterFrameListenerIface inhibit_frame_listener_iface = {
  .frame = inhibit_frame_clock_frame,
};

static gboolean
uninhibit_timeout (gpointer user_data)
{
  InhibitTest *test = user_data;

  g_assert_cmpint (test->frame_count, ==, 1);

  clutter_frame_clock_uninhibit (test->frame_clock);
  test->pending_quit = TRUE;

  return G_SOURCE_REMOVE;
}

static void
frame_clock_inhibit (void)
{
  InhibitTest test = { 0 };

  expected_frame_count = 0;

  test.main_loop = g_main_loop_new (NULL, FALSE);
  test.frame_clock = clutter_frame_clock_new (refresh_rate,
                                              0,
                                              &inhibit_frame_listener_iface,
                                              &test);

  test.pending_inhibit = TRUE;

  clutter_frame_clock_schedule_update (test.frame_clock);
  g_timeout_add (100, uninhibit_timeout, &test);
  g_main_loop_run (test.main_loop);

  g_assert_cmpint (test.frame_count, ==, 2);

  g_main_loop_unref (test.main_loop);
  clutter_frame_clock_destroy (test.frame_clock);
}

typedef struct _RescheduleOnIdleFrameClockTest
{
  FrameClockTest base;
} RescheduleOnIdleFrameClockTest;

static ClutterFrameResult
reschedule_on_idle_clock_frame (ClutterFrameClock *frame_clock,
                                int64_t            frame_count,
                                int64_t            time_us,
                                gpointer           user_data)
{
  RescheduleOnIdleFrameClockTest *test = user_data;
  GMainLoop *main_loop = test->base.main_loop;

  g_assert_cmpint (frame_count, ==, expected_frame_count);

  expected_frame_count++;

  if (test_frame_count == 0)
    {
      g_main_loop_quit (main_loop);
      return CLUTTER_FRAME_RESULT_IDLE;
    }

  test_frame_count--;

  clutter_frame_clock_schedule_update (frame_clock);

  return CLUTTER_FRAME_RESULT_IDLE;
}

static const ClutterFrameListenerIface reschedule_on_idle_listener_iface = {
  .frame = reschedule_on_idle_clock_frame,
};

static void
frame_clock_reschedule_on_idle (void)
{
  RescheduleOnIdleFrameClockTest test;
  ClutterFrameClock *frame_clock;
  FakeHwClock *fake_hw_clock;
  GSource *source;

  test_frame_count = 10;
  expected_frame_count = 0;

  test.base.main_loop = g_main_loop_new (NULL, FALSE);
  frame_clock = clutter_frame_clock_new (refresh_rate,
                                         0,
                                         &reschedule_on_idle_listener_iface,
                                         &test);
  fake_hw_clock = fake_hw_clock_new (frame_clock, NULL, NULL);
  source = &fake_hw_clock->source;
  g_source_attach (source, NULL);
  test.base.fake_hw_clock = fake_hw_clock;

  clutter_frame_clock_schedule_update (frame_clock);
  g_main_loop_run (test.base.main_loop);

  g_main_loop_unref (test.base.main_loop);
  clutter_frame_clock_destroy (frame_clock);
}

static const ClutterFrameListenerIface dummy_frame_listener_iface = {
  .frame = NULL,
};

static void
on_destroy (ClutterFrameClock *frame_clock,
            gboolean          *destroy_signalled)
{
  g_assert_false (*destroy_signalled);
  *destroy_signalled = TRUE;
}

static void
frame_clock_destroy_signal (void)
{
  ClutterFrameClock *frame_clock;
  ClutterFrameClock *frame_clock_backup;
  gboolean destroy_signalled;

  /* Test that the destroy signal is emitted when removing last reference. */

  frame_clock = clutter_frame_clock_new (refresh_rate,
                                         0,
                                         &dummy_frame_listener_iface,
                                         NULL);

  destroy_signalled = FALSE;
  g_signal_connect (frame_clock, "destroy",
                    G_CALLBACK (on_destroy),
                    &destroy_signalled);
  g_object_add_weak_pointer (G_OBJECT (frame_clock), (gpointer *) &frame_clock);

  g_object_unref (frame_clock);
  g_assert_true (destroy_signalled);
  g_assert_null (frame_clock);

  /* Test that destroy signal is emitted when destroying with references still
   * left.
   */

  frame_clock = clutter_frame_clock_new (refresh_rate,
                                         0,
                                         &dummy_frame_listener_iface,
                                         NULL);
  frame_clock_backup = frame_clock;

  destroy_signalled = FALSE;
  g_signal_connect (frame_clock, "destroy",
                    G_CALLBACK (on_destroy),
                    &destroy_signalled);
  g_object_add_weak_pointer (G_OBJECT (frame_clock), (gpointer *) &frame_clock);
  g_object_ref (frame_clock);

  clutter_frame_clock_destroy (frame_clock);
  g_assert_true (destroy_signalled);
  g_assert_null (frame_clock);
  g_object_unref (frame_clock_backup);
}

static gboolean
notify_ready_and_schedule_update_idle (gpointer user_data)
{
  ClutterFrameClock *frame_clock = user_data;

  clutter_frame_clock_notify_ready (frame_clock);
  clutter_frame_clock_schedule_update (frame_clock);

  return G_SOURCE_REMOVE;
}

static ClutterFrameResult
frame_clock_ready_frame (ClutterFrameClock *frame_clock,
                         int64_t            frame_count,
                         int64_t            time_us,
                         gpointer           user_data)
{
  GMainLoop *main_loop = user_data;

  g_assert_cmpint (frame_count, ==, expected_frame_count);

  expected_frame_count++;

  if (test_frame_count == 0)
    {
      g_main_loop_quit (main_loop);
      return CLUTTER_FRAME_RESULT_IDLE;
    }

  test_frame_count--;

  g_idle_add (notify_ready_and_schedule_update_idle, frame_clock);

  return CLUTTER_FRAME_RESULT_PENDING_PRESENTED;
}

static const ClutterFrameListenerIface frame_clock_ready_listener_iface = {
  .frame = frame_clock_ready_frame,
};

static void
frame_clock_notify_ready (void)
{
  GMainLoop *main_loop;
  ClutterFrameClock *frame_clock;
  int64_t before_us;
  int64_t after_us;

  test_frame_count = 10;
  expected_frame_count = 0;

  main_loop = g_main_loop_new (NULL, FALSE);
  frame_clock = clutter_frame_clock_new (refresh_rate,
                                         0,
                                         &frame_clock_ready_listener_iface,
                                         main_loop);

  before_us = g_get_monotonic_time ();

  clutter_frame_clock_schedule_update (frame_clock);
  g_main_loop_run (main_loop);

  after_us = g_get_monotonic_time ();

  /* The initial frame will only be delayed by 2 ms, so we are checking one
   * less.
   */
  g_assert_cmpint (after_us - before_us, >, 8 * refresh_interval_us);

  g_main_loop_unref (main_loop);
  clutter_frame_clock_destroy (frame_clock);
}

CLUTTER_TEST_SUITE (
  CLUTTER_TEST_UNIT ("/frame-clock/schedule-update", frame_clock_schedule_update)
  CLUTTER_TEST_UNIT ("/frame-clock/immediate-present", frame_clock_immediate_present)
  CLUTTER_TEST_UNIT ("/frame-clock/delayed-damage", frame_clock_delayed_damage)
  CLUTTER_TEST_UNIT ("/frame-clock/no-damage", frame_clock_no_damage)
  CLUTTER_TEST_UNIT ("/frame-clock/schedule-update-now", frame_clock_schedule_update_now)
  CLUTTER_TEST_UNIT ("/frame-clock/before-frame", frame_clock_before_frame)
  CLUTTER_TEST_UNIT ("/frame-clock/inhibit", frame_clock_inhibit)
  CLUTTER_TEST_UNIT ("/frame-clock/reschedule-on-idle", frame_clock_reschedule_on_idle)
  CLUTTER_TEST_UNIT ("/frame-clock/destroy-signal", frame_clock_destroy_signal)
  CLUTTER_TEST_UNIT ("/frame-clock/notify-ready", frame_clock_notify_ready)
)