gdkframeclockidle.c 18.1 KB
Newer Older
Owen W. Taylor's avatar
Owen W. Taylor committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* GDK - The GIMP Drawing Kit
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * 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
15
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Owen W. Taylor's avatar
Owen W. Taylor committed
16 17 18 19 20 21 22 23 24 25 26
 */

/*
 * Modified by the GTK+ Team and others 1997-2010.  See the AUTHORS
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
 */

#include "config.h"

Owen W. Taylor's avatar
Owen W. Taylor committed
27
#include "gdkinternals.h"
28
#include "gdkframeclockprivate.h"
Owen W. Taylor's avatar
Owen W. Taylor committed
29 30 31
#include "gdkframeclockidle.h"
#include "gdk.h"

32 33 34 35
#ifdef G_OS_WIN32
#include <windows.h>
#endif

Matthias Clasen's avatar
Matthias Clasen committed
36
#define FRAME_INTERVAL 16667 /* microseconds */
37

Owen W. Taylor's avatar
Owen W. Taylor committed
38 39 40 41
struct _GdkFrameClockIdlePrivate
{
  GTimer *timer;
  /* timer_base is used to avoid ever going backward */
42 43 44
  gint64 timer_base;
  gint64 frame_time;
  gint64 min_next_frame_time;
45
  gint64 sleep_serial;
Owen W. Taylor's avatar
Owen W. Taylor committed
46

47 48
  guint flush_idle_id;
  guint paint_idle_id;
49
  guint freeze_count;
50
  guint updating_count;
Owen W. Taylor's avatar
Owen W. Taylor committed
51

52 53
  GdkFrameClockPhase requested;
  GdkFrameClockPhase phase;
54 55

  guint in_paint_idle : 1;
56 57 58
#ifdef G_OS_WIN32
  guint begin_period : 1;
#endif
Owen W. Taylor's avatar
Owen W. Taylor committed
59 60
};

61
static gboolean gdk_frame_clock_flush_idle (void *data);
62 63
static gboolean gdk_frame_clock_paint_idle (void *data);

64
G_DEFINE_TYPE_WITH_PRIVATE (GdkFrameClockIdle, gdk_frame_clock_idle, GDK_TYPE_FRAME_CLOCK)
Owen W. Taylor's avatar
Owen W. Taylor committed
65

66 67 68 69
static gint64 sleep_serial;
static gint64 sleep_source_prepare_time;
static GSource *sleep_source;

70
static gboolean
71 72 73 74 75 76 77 78
sleep_source_prepare (GSource *source,
                      gint    *timeout)
{
  sleep_source_prepare_time = g_source_get_time (source);
  *timeout = -1;
  return FALSE;
}

79
static gboolean
80 81 82 83 84 85 86 87
sleep_source_check (GSource *source)
{
  if (g_source_get_time (source) != sleep_source_prepare_time)
    sleep_serial++;

  return FALSE;
}

88
static gboolean
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
sleep_source_dispatch (GSource     *source,
                       GSourceFunc  callback,
                       gpointer     user_data)
{
  return TRUE;
}

static GSourceFuncs sleep_source_funcs = {
  sleep_source_prepare,
  sleep_source_check,
  sleep_source_dispatch,
  NULL /* finalize */
};

static gint64
get_sleep_serial (void)
{
  if (sleep_source == NULL)
    {
      sleep_source = g_source_new (&sleep_source_funcs, sizeof (GSource));

      g_source_set_priority (sleep_source, G_PRIORITY_HIGH);
      g_source_attach (sleep_source, NULL);
      g_source_unref (sleep_source);
    }

  return sleep_serial;
}

Owen W. Taylor's avatar
Owen W. Taylor committed
118 119 120 121 122
static void
gdk_frame_clock_idle_init (GdkFrameClockIdle *frame_clock_idle)
{
  GdkFrameClockIdlePrivate *priv;

123 124
  frame_clock_idle->priv = priv =
    gdk_frame_clock_idle_get_instance_private (frame_clock_idle);
Owen W. Taylor's avatar
Owen W. Taylor committed
125

126
  priv->freeze_count = 0;
Owen W. Taylor's avatar
Owen W. Taylor committed
127 128 129
}

static void
130
gdk_frame_clock_idle_dispose (GObject *object)
Owen W. Taylor's avatar
Owen W. Taylor committed
131 132 133
{
  GdkFrameClockIdlePrivate *priv = GDK_FRAME_CLOCK_IDLE (object)->priv;

134 135 136 137 138 139 140 141 142 143 144
  if (priv->flush_idle_id != 0)
    {
      g_source_remove (priv->flush_idle_id);
      priv->flush_idle_id = 0;
    }

  if (priv->paint_idle_id != 0)
    {
      g_source_remove (priv->paint_idle_id);
      priv->paint_idle_id = 0;
    }
Owen W. Taylor's avatar
Owen W. Taylor committed
145

146 147 148 149 150 151 152 153
#ifdef G_OS_WIN32
  if (priv->begin_period) 
    {
      timeEndPeriod(1);
      priv->begin_period = FALSE;
    }
#endif

154
  G_OBJECT_CLASS (gdk_frame_clock_idle_parent_class)->dispose (object);
Owen W. Taylor's avatar
Owen W. Taylor committed
155 156
}

157
static gint64
Owen W. Taylor's avatar
Owen W. Taylor committed
158 159 160
compute_frame_time (GdkFrameClockIdle *idle)
{
  GdkFrameClockIdlePrivate *priv = idle->priv;
161 162
  gint64 computed_frame_time;
  gint64 elapsed;
Owen W. Taylor's avatar
Owen W. Taylor committed
163

164
  elapsed = g_get_monotonic_time () + priv->timer_base;
Owen W. Taylor's avatar
Owen W. Taylor committed
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
  if (elapsed < priv->frame_time)
    {
      /* clock went backward. adapt to that by forevermore increasing
       * timer_base.  For now, assume we've gone forward in time 1ms.
       */
      /* hmm. just fix GTimer? */
      computed_frame_time = priv->frame_time + 1;
      priv->timer_base += (priv->frame_time - elapsed) + 1;
    }
  else
    {
      computed_frame_time = elapsed;
    }

  return computed_frame_time;
}

182
static gint64
Owen W. Taylor's avatar
Owen W. Taylor committed
183 184 185
gdk_frame_clock_idle_get_frame_time (GdkFrameClock *clock)
{
  GdkFrameClockIdlePrivate *priv = GDK_FRAME_CLOCK_IDLE (clock)->priv;
186
  gint64 computed_frame_time;
Owen W. Taylor's avatar
Owen W. Taylor committed
187 188

  /* can't change frame time during a paint */
189 190
  if (priv->phase != GDK_FRAME_CLOCK_PHASE_NONE &&
      priv->phase != GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS)
Owen W. Taylor's avatar
Owen W. Taylor committed
191 192 193 194 195 196 197 198 199 200
    return priv->frame_time;

  /* Outside a paint, pick something close to "now" */
  computed_frame_time = compute_frame_time (GDK_FRAME_CLOCK_IDLE (clock));

  /* 16ms is 60fps. We only update frame time that often because we'd
   * like to try to keep animations on the same start times.
   * get_frame_time() would normally be used outside of a paint to
   * record an animation start time for example.
   */
201
  if ((computed_frame_time - priv->frame_time) > FRAME_INTERVAL)
Owen W. Taylor's avatar
Owen W. Taylor committed
202 203 204 205 206
    priv->frame_time = computed_frame_time;

  return priv->frame_time;
}

207 208 209 210 211 212 213 214 215 216 217 218 219 220
#define RUN_FLUSH_IDLE(priv)                                            \
  ((priv)->freeze_count == 0 &&                                         \
   ((priv)->requested & GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS) != 0)

/* The reason why we track updating_count separately here and don't
 * just add GDK_FRAME_CLOCK_PHASE_UPDATE into ->request on every frame
 * is so that we can avoid doing one more frame when an animation
 * is cancelled.
 */
#define RUN_PAINT_IDLE(priv)                                            \
  ((priv)->freeze_count == 0 &&                                         \
   (((priv)->requested & ~GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS) != 0 ||   \
    (priv)->updating_count > 0))

221 222 223 224 225
static void
maybe_start_idle (GdkFrameClockIdle *clock_idle)
{
  GdkFrameClockIdlePrivate *priv = clock_idle->priv;

226
  if (RUN_FLUSH_IDLE (priv) || RUN_PAINT_IDLE (priv))
227
    {
228 229 230 231
      guint min_interval = 0;

      if (priv->min_next_frame_time != 0)
        {
232 233
          gint64 now = compute_frame_time (clock_idle);
          gint64 min_interval_us = MAX (priv->min_next_frame_time, now) - now;
234 235 236
          min_interval = (min_interval_us + 500) / 1000;
        }

237
      if (priv->flush_idle_id == 0 && RUN_FLUSH_IDLE (priv))
238 239 240 241 242 243
        {
          priv->flush_idle_id = gdk_threads_add_timeout_full (GDK_PRIORITY_EVENTS + 1,
                                                              min_interval,
                                                              gdk_frame_clock_flush_idle,
                                                              g_object_ref (clock_idle),
                                                              (GDestroyNotify) g_object_unref);
244
          g_source_set_name_by_id (priv->flush_idle_id, "[gtk+] gdk_frame_clock_flush_idle");
245 246
        }

247 248
      if (!priv->in_paint_idle &&
	  priv->paint_idle_id == 0 && RUN_PAINT_IDLE (priv))
249 250 251 252 253 254
        {
          priv->paint_idle_id = gdk_threads_add_timeout_full (GDK_PRIORITY_REDRAW,
                                                              min_interval,
                                                              gdk_frame_clock_paint_idle,
                                                              g_object_ref (clock_idle),
                                                              (GDestroyNotify) g_object_unref);
255
          g_source_set_name_by_id (priv->paint_idle_id, "[gtk+] gdk_frame_clock_paint_idle");
256
        }
257 258 259
    }
}

260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
static void
maybe_stop_idle (GdkFrameClockIdle *clock_idle)
{
  GdkFrameClockIdlePrivate *priv = clock_idle->priv;

  if (priv->flush_idle_id != 0 && !RUN_FLUSH_IDLE (priv))
    {
      g_source_remove (priv->flush_idle_id);
      priv->flush_idle_id = 0;
    }

  if (priv->paint_idle_id != 0 && !RUN_PAINT_IDLE (priv))
    {
      g_source_remove (priv->paint_idle_id);
      priv->paint_idle_id = 0;
    }
}

278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
static gint64
compute_min_next_frame_time (GdkFrameClockIdle *clock_idle,
                             gint64             last_frame_time)
{
  gint64 presentation_time;
  gint64 refresh_interval;

  gdk_frame_clock_get_refresh_info (GDK_FRAME_CLOCK (clock_idle),
                                    last_frame_time,
                                    &refresh_interval, &presentation_time);

  if (presentation_time == 0)
    return last_frame_time + refresh_interval;
  else
    return presentation_time + refresh_interval / 2;
}

295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
static gboolean
gdk_frame_clock_flush_idle (void *data)
{
  GdkFrameClock *clock = GDK_FRAME_CLOCK (data);
  GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
  GdkFrameClockIdlePrivate *priv = clock_idle->priv;

  priv->flush_idle_id = 0;

  if (priv->phase != GDK_FRAME_CLOCK_PHASE_NONE)
    return FALSE;

  priv->phase = GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS;
  priv->requested &= ~GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS;

  g_signal_emit_by_name (G_OBJECT (clock), "flush-events");

312 313
  if ((priv->requested & ~GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS) != 0 ||
      priv->updating_count > 0)
314 315 316 317 318 319 320
    priv->phase = GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT;
  else
    priv->phase = GDK_FRAME_CLOCK_PHASE_NONE;

  return FALSE;
}

Owen W. Taylor's avatar
Owen W. Taylor committed
321 322 323 324 325 326
static gboolean
gdk_frame_clock_paint_idle (void *data)
{
  GdkFrameClock *clock = GDK_FRAME_CLOCK (data);
  GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
  GdkFrameClockIdlePrivate *priv = clock_idle->priv;
327
  gboolean skip_to_resume_events;
Owen W. Taylor's avatar
Owen W. Taylor committed
328
  GdkFrameTimings *timings = NULL;
Owen W. Taylor's avatar
Owen W. Taylor committed
329

330
  priv->paint_idle_id = 0;
331
  priv->in_paint_idle = TRUE;
332
  priv->min_next_frame_time = 0;
333 334

  skip_to_resume_events =
335 336
    (priv->requested & ~(GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS | GDK_FRAME_CLOCK_PHASE_RESUME_EVENTS)) == 0 &&
    priv->updating_count == 0;
Owen W. Taylor's avatar
Owen W. Taylor committed
337

Owen W. Taylor's avatar
Owen W. Taylor committed
338 339
  if (priv->phase > GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT)
    {
340
      timings = gdk_frame_clock_get_current_timings (clock);
Owen W. Taylor's avatar
Owen W. Taylor committed
341 342
    }

343
  if (!skip_to_resume_events)
344
    {
345 346 347 348 349 350 351
      switch (priv->phase)
        {
        case GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS:
          break;
        case GDK_FRAME_CLOCK_PHASE_NONE:
        case GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT:
          if (priv->freeze_count == 0)
352
            {
353
              priv->frame_time = compute_frame_time (clock_idle);
Owen W. Taylor's avatar
Owen W. Taylor committed
354

355
              _gdk_frame_clock_begin_frame (clock);
356
              timings = gdk_frame_clock_get_current_timings (clock);
Owen W. Taylor's avatar
Owen W. Taylor committed
357

358 359
              timings->frame_time = priv->frame_time;
              timings->slept_before = priv->sleep_serial != get_sleep_serial ();
360

361 362 363 364 365
              priv->phase = GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT;

              /* We always emit ::before-paint and ::after-paint if
               * any of the intermediate phases are requested and
               * they don't get repeated if you freeze/thaw while
Matthias Clasen's avatar
Matthias Clasen committed
366 367
               * in them.
               */
368 369
              priv->requested &= ~GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT;
              g_signal_emit_by_name (G_OBJECT (clock), "before-paint");
370
              priv->phase = GDK_FRAME_CLOCK_PHASE_UPDATE;
371
            }
372
          /* fallthrough */
373 374
        case GDK_FRAME_CLOCK_PHASE_UPDATE:
          if (priv->freeze_count == 0)
375
            {
376 377
              if ((priv->requested & GDK_FRAME_CLOCK_PHASE_UPDATE) != 0 ||
                  priv->updating_count > 0)
378 379 380 381
                {
                  priv->requested &= ~GDK_FRAME_CLOCK_PHASE_UPDATE;
                  g_signal_emit_by_name (G_OBJECT (clock), "update");
                }
382
            }
383
          /* fallthrough */
384 385
        case GDK_FRAME_CLOCK_PHASE_LAYOUT:
          if (priv->freeze_count == 0)
386
            {
387
	      int iter;
Owen W. Taylor's avatar
Owen W. Taylor committed
388 389 390 391 392
#ifdef G_ENABLE_DEBUG
              if ((_gdk_debug_flags & GDK_DEBUG_FRAMES) != 0)
                {
                  if (priv->phase != GDK_FRAME_CLOCK_PHASE_LAYOUT &&
                      (priv->requested & GDK_FRAME_CLOCK_PHASE_LAYOUT))
393
                    timings->layout_start_time = g_get_monotonic_time ();
Owen W. Taylor's avatar
Owen W. Taylor committed
394 395 396
                }
#endif /* G_ENABLE_DEBUG */

397
              priv->phase = GDK_FRAME_CLOCK_PHASE_LAYOUT;
398 399 400 401 402 403 404 405
	      /* We loop in the layout phase, because we don't want to progress
	       * into the paint phase with invalid size allocations. This may
	       * happen in some situation like races between user window
	       * resizes and natural size changes.
	       */
	      iter = 0;
              while ((priv->requested & GDK_FRAME_CLOCK_PHASE_LAYOUT) &&
		     priv->freeze_count == 0 && iter++ < 4)
406 407 408 409
                {
                  priv->requested &= ~GDK_FRAME_CLOCK_PHASE_LAYOUT;
                  g_signal_emit_by_name (G_OBJECT (clock), "layout");
                }
410 411
	      if (iter == 5)
		g_warning ("gdk-frame-clock: layout continuously requested, giving up after 4 tries");
412
            }
413
          /* fallthrough */
414 415
        case GDK_FRAME_CLOCK_PHASE_PAINT:
          if (priv->freeze_count == 0)
416
            {
Owen W. Taylor's avatar
Owen W. Taylor committed
417 418 419 420 421
#ifdef G_ENABLE_DEBUG
              if ((_gdk_debug_flags & GDK_DEBUG_FRAMES) != 0)
                {
                  if (priv->phase != GDK_FRAME_CLOCK_PHASE_PAINT &&
                      (priv->requested & GDK_FRAME_CLOCK_PHASE_PAINT))
422
                    timings->paint_start_time = g_get_monotonic_time ();
Owen W. Taylor's avatar
Owen W. Taylor committed
423 424 425
                }
#endif /* G_ENABLE_DEBUG */

426 427 428 429 430 431
              priv->phase = GDK_FRAME_CLOCK_PHASE_PAINT;
              if (priv->requested & GDK_FRAME_CLOCK_PHASE_PAINT)
                {
                  priv->requested &= ~GDK_FRAME_CLOCK_PHASE_PAINT;
                  g_signal_emit_by_name (G_OBJECT (clock), "paint");
                }
432
            }
433
          /* fallthrough */
434 435
        case GDK_FRAME_CLOCK_PHASE_AFTER_PAINT:
          if (priv->freeze_count == 0)
436 437 438
            {
              priv->requested &= ~GDK_FRAME_CLOCK_PHASE_AFTER_PAINT;
              g_signal_emit_by_name (G_OBJECT (clock), "after-paint");
439 440 441
              /* the ::after-paint phase doesn't get repeated on freeze/thaw,
               */
              priv->phase = GDK_FRAME_CLOCK_PHASE_NONE;
Owen W. Taylor's avatar
Owen W. Taylor committed
442 443 444

#ifdef G_ENABLE_DEBUG
              if ((_gdk_debug_flags & GDK_DEBUG_FRAMES) != 0)
445
                timings->frame_end_time = g_get_monotonic_time ();
Owen W. Taylor's avatar
Owen W. Taylor committed
446
#endif /* G_ENABLE_DEBUG */
447
            }
448
          /* fallthrough */
449 450 451
        case GDK_FRAME_CLOCK_PHASE_RESUME_EVENTS:
          ;
        }
452
    }
453

Owen W. Taylor's avatar
Owen W. Taylor committed
454 455 456
#ifdef G_ENABLE_DEBUG
  if ((_gdk_debug_flags & GDK_DEBUG_FRAMES) != 0)
    {
457
      if (timings && timings->complete)
458
        _gdk_frame_clock_debug_print_timings (clock, timings);
Owen W. Taylor's avatar
Owen W. Taylor committed
459 460 461
    }
#endif /* G_ENABLE_DEBUG */

462 463 464 465 466 467
  if (priv->requested & GDK_FRAME_CLOCK_PHASE_RESUME_EVENTS)
    {
      priv->requested &= ~GDK_FRAME_CLOCK_PHASE_RESUME_EVENTS;
      g_signal_emit_by_name (G_OBJECT (clock), "resume-events");
    }

468 469 470 471
  if (priv->freeze_count == 0)
    priv->phase = GDK_FRAME_CLOCK_PHASE_NONE;

  priv->in_paint_idle = FALSE;
472

473 474 475 476 477
  /* If there is throttling in the backend layer, then we'll do another
   * update as soon as the backend unthrottles (if there is work to do),
   * otherwise we need to figure when the next frame should be.
   */
  if (priv->freeze_count == 0)
478
    {
479 480
      priv->min_next_frame_time = compute_min_next_frame_time (clock_idle,
                                                               priv->frame_time);
481 482
      maybe_start_idle (clock_idle);
    }
Owen W. Taylor's avatar
Owen W. Taylor committed
483

484 485 486
  if (priv->freeze_count == 0)
    priv->sleep_serial = get_sleep_serial ();

Owen W. Taylor's avatar
Owen W. Taylor committed
487 488 489 490
  return FALSE;
}

static void
491 492
gdk_frame_clock_idle_request_phase (GdkFrameClock      *clock,
                                    GdkFrameClockPhase  phase)
Owen W. Taylor's avatar
Owen W. Taylor committed
493
{
494 495
  GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
  GdkFrameClockIdlePrivate *priv = clock_idle->priv;
Owen W. Taylor's avatar
Owen W. Taylor committed
496

497 498
  priv->requested |= phase;
  maybe_start_idle (clock_idle);
Owen W. Taylor's avatar
Owen W. Taylor committed
499 500
}

501 502 503 504 505 506
static void
gdk_frame_clock_idle_begin_updating (GdkFrameClock *clock)
{
  GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
  GdkFrameClockIdlePrivate *priv = clock_idle->priv;

507 508 509 510 511 512 513 514 515
#ifdef G_OS_WIN32
  /* We need a higher resolution timer while doing animations */
  if (priv->updating_count == 0 && !priv->begin_period)
    {
      timeBeginPeriod(1);
      priv->begin_period = TRUE;
    }
#endif

516 517 518 519 520 521 522 523 524 525 526 527 528 529
  priv->updating_count++;
  maybe_start_idle (clock_idle);
}

static void
gdk_frame_clock_idle_end_updating (GdkFrameClock *clock)
{
  GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
  GdkFrameClockIdlePrivate *priv = clock_idle->priv;

  g_return_if_fail (priv->updating_count > 0);

  priv->updating_count--;
  maybe_stop_idle (clock_idle);
530 531 532 533 534 535 536 537

#ifdef G_OS_WIN32
  if (priv->updating_count == 0 && priv->begin_period)
    {
      timeEndPeriod(1);
      priv->begin_period = FALSE;
    }
#endif
538 539
}

540 541 542
static void
gdk_frame_clock_idle_freeze (GdkFrameClock *clock)
{
543 544
  GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
  GdkFrameClockIdlePrivate *priv = clock_idle->priv;
545 546

  priv->freeze_count++;
547
  maybe_stop_idle (clock_idle);
548 549 550 551 552 553 554 555 556 557 558 559
}

static void
gdk_frame_clock_idle_thaw (GdkFrameClock *clock)
{
  GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
  GdkFrameClockIdlePrivate *priv = clock_idle->priv;

  g_return_if_fail (priv->freeze_count > 0);

  priv->freeze_count--;
  if (priv->freeze_count == 0)
560 561 562 563
    {
      maybe_start_idle (clock_idle);
      /* If nothing is requested so we didn't start an idle, we need
       * to skip to the end of the state chain, since the idle won't
Matthias Clasen's avatar
Matthias Clasen committed
564 565
       * run and do it for us.
       */
566 567
      if (priv->paint_idle_id == 0)
        priv->phase = GDK_FRAME_CLOCK_PHASE_NONE;
568 569

      priv->sleep_serial = get_sleep_serial ();
570
    }
571 572
}

573 574
static void
gdk_frame_clock_idle_class_init (GdkFrameClockIdleClass *klass)
575
{
576 577
  GObjectClass *gobject_class = (GObjectClass*) klass;
  GdkFrameClockClass *frame_clock_class = (GdkFrameClockClass *)klass;
578

579
  gobject_class->dispose = gdk_frame_clock_idle_dispose;
580

581 582
  frame_clock_class->get_frame_time = gdk_frame_clock_idle_get_frame_time;
  frame_clock_class->request_phase = gdk_frame_clock_idle_request_phase;
583 584
  frame_clock_class->begin_updating = gdk_frame_clock_idle_begin_updating;
  frame_clock_class->end_updating = gdk_frame_clock_idle_end_updating;
585 586
  frame_clock_class->freeze = gdk_frame_clock_idle_freeze;
  frame_clock_class->thaw = gdk_frame_clock_idle_thaw;
587 588 589 590 591 592 593 594 595 596
}

GdkFrameClock *
_gdk_frame_clock_idle_new (void)
{
  GdkFrameClockIdle *clock;

  clock = g_object_new (GDK_TYPE_FRAME_CLOCK_IDLE, NULL);

  return GDK_FRAME_CLOCK (clock);
Owen W. Taylor's avatar
Owen W. Taylor committed
597
}