Commit Graph

67 Commits

Author SHA1 Message Date
Benjamin Otte
988e20cd53 frameclock: Run paint idle from flush idle
Don't return to the main loop, instead force a run of the paint idle.
The paint idle will know to skip all the phases that aren't requested.

This is critically important becuase gdksurface.c assumes the
FLUSH_EVENTS and RESUME_EVENTS phases are matched, and we cannot
guarantee that if we return to the main loop and let various reentrant
code change the frame clock state.

This would lead to bugs with events being paused and never unpaused
again or even crashes.

Fixes #4941
2022-07-10 21:33:32 +02:00
Benjamin Otte
a29474567a gdk: Remove gdkinternals.h 2021-09-24 22:50:29 +02:00
Benjamin Otte
c7992884ae gdk: Remove gdk.h include form gdkframeclockidle.c 2021-09-24 22:11:57 +02:00
Matthias Clasen
19b534f7de Avoid copying static debug strings
The g_source_set_name calls were showing up as a
major source of strdups in our profiles. Avoid that
by using new GLib api when available.
2021-07-28 22:42:46 -04:00
Matthias Clasen
125ed52ccb Merge branch 'new-sysprof' into 'master'
Port profiling to sysprof-collector api

See merge request GNOME/gtk!2457
2020-08-21 23:58:09 +00:00
Matthias Clasen
d4e069a629 Port tracing to the sysprof collector api
Use the new sysprof collector api to do tracing.
2020-08-21 10:55:01 -04:00
Björn Daase
6315cd977c *: Fix spelling mistakes found by codespell 2020-08-21 15:29:34 +02:00
Benjamin Otte
d7266b25ba Replace "gint" with "int" 2020-07-25 00:47:36 +02:00
Yariv Barkan
887aa2abdd frame clock: fix scheduling
Fix scheduling of the frame clock when we don't receive "frame drawn"
messages from the compositor.

If we received "frame drawn" events recently, then the "smooth frame
time" would be in sync with the vsync time. When we don't receive frame
drawn events, the "smooth frame time" is simply incremented by constant
multiples of the refresh interval. In both cases we can use this smooth
time as the basis for scheduling the next clock cycle.

By only using the "smooth frame time" as a basis we also benefit from
more consistent scheduling cadence. If, for example, we got "frame
drawn" events, then didn't receive them for a few frames, we would still
be in sync when we start receiving these events again.
2020-06-15 08:54:18 +02:00
Yariv Barkan
9c5d6eaa71 frame clock: adjust reported frame time
When an animation is started while the application is idle, that often
happens as a result of some external event. This can be an input event,
an expired timer, data arriving over the network etc. The result is that
the first animation clock cycle could be scheduled at some random time,
as opposed to follow up cycles which are usually scheduled right after a
vsync.

Since the frame time we report to the application is correlated to the
time when the frame clock was scheduled to run, this can result in
uneven times reported in the first few animation frames. In order to fix
that, we measure the phase of the first clock cycle - i.e. the offset
between the first cycle and the preceding vsync. Once we start receiving
"frame drawn" signals, the cadence of the frame clock scheduling becomes
tied to the vsync. In order to maintain the regularity of the reported
frame times, we adjust subsequent reported frame times with the
aforementioned phase.
2020-06-15 08:54:17 +02:00
Yariv Barkan
f5de46670b frame clock: schedule in refresh intervals
When the application does not receive "frame drawn" signals we schedule
the clock to run more or less at intervals equal to the last known
refresh interval. In order to minimize clock skew we have to aim for
exact intervals.
2020-06-05 11:57:49 +02:00
Alexander Larsson
91af8a705b frame clock: Better handle non-regular clock cycles
We try to step the frame clock in whole refresh_interval steps, but to
avoid drift and rounding issues we additionally try to converge it to
be synced to the physical vblank (actually the time we get the
frame-drawn message from the compositor, but these are tied together).

However, the convergence to vsync only really makes sense if the new
frame_time actually is tied to the vsync. It may very well be that
some other kind of event (say a network or mouse event) triggered
the redraw, and not a vsync presentation.

We used to assume that all frames that are close in time (< 4 frames
apart) were regular and thus tied to the vsync, but there is really no
guarantee of that. Even non regular times could be rapid.

This commit changes the code to only do the convergence-to-real-time
if the cause of the clock cycle was a thaw (i.e. last frame drawn and
animating). Paint cycles for any other kind of reason are always
scheduled an integer number of frames after the last cycle that was
caused by a thaw.
2020-06-05 11:30:47 +02:00
Alexander Larsson
82c314f1af frame clock: Track if paint is caused by thaw or not
When we get to a paint cycle we now know if this was caused by a
thaw, which typically means last frame was drawn, or some other event.

In the first case the time of the cycle is tied to the vblank in some
sense, and in the others it is essentially random. We can use this
information to compute better frame times. (Will be done in later
commits.)
2020-06-05 11:16:57 +02:00
Alexander Larsson
a36e2bc764 Fix frameclock going backwards
When we run the frameclock RUN_FLUSH_IDLE idle before the paint,
then gdk_frame_clock_flush_idle() sets
```
  priv->phase = GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT
```
at the end if there is a paint comming.

But, before doing the paint cycle it may handle other X events, and
during that time the phase is set to BEFORE_PAINT. This means that the
current check on whether we're inside a paint is wrong:

```
  if (priv->phase != GDK_FRAME_CLOCK_PHASE_NONE &&
     priv->phase != GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS)
    return priv->smoothed_frame_time_base;
```

This caused us to sometimes use this smoothed_frame_time_base even
though we previously reported a later value during PHASE_NONE, thus
being non-monotonic.

We can't just additionally check for the BEGIN_PAINT phase though,
becasue if we are in the paint loop actually doing that phase we
should use the time base. Instead we check for `!(BEFORE_PAINT &&
in_paint_idle)`.
2020-06-01 10:53:24 +02:00
Alexander Larsson
a27fed47e0 frame-clock: Ensure we're always monotonic
A call to frame gdk_frame_clock_get_frame_time() outside of the paint
cycle could report an un-error-corrected frame time, and later a
corrected value could be earlier than the previously reported value.

We now always store the latest reported time so we can ensure
monotonicity.
2020-05-28 17:44:51 +02:00
Alexander Larsson
9ef3e70040 frame-clock: New approach in smoothing frame clock
In commit c6901a8b, the frame clock reported time was changed from
simply reporting the time we ran the frame clock cycle to reporting a
smoothed value that increased by the frame interval each time it was
called.

However, this change caused some problems, such as:
 https://gitlab.gnome.org/GNOME/gtk/-/merge_requests/1415
 https://gitlab.gnome.org/GNOME/gtk/-/merge_requests/1416
 https://gitlab.gnome.org/GNOME/gtk/-/merge_requests/1482

I think a lot of this is caused by the fact that we just overwrote the
old frame time with the smoothed, monotonous timestamp, breaking
some things that relied on knowing the actual time something happened.

This is a new approach to doing the smoothing that is more explicit.
The "frame_time" we store is the actual time we ran the update cycle,
and then we separately compute and store the derived smoothed time and
its period, allowing us to easily return a smoothed time at any time
by rounding the time difference to an integer number of frames.

The initial frame_time can be somewhat arbitrary, as it depends on the
first cycle which is not driven by the frame clock. But follow-up
cycles are typically tied to the the compositor sending the drawn
signal. It may happen that the initial frame is exactly in the middle
between two frames where jitter causes us to randomly round in
different directions when rounding to nearest frame. To fix this we
additionally do a quadratic convergence towards the "real" time,
during presentation driven clock cycles (i.e. when the frame times are
small).
2020-05-28 15:13:07 +02:00
Alexander Larsson
324202a56c profiler: Clean up profiler marks for frameclock
This drops the marks for before/after-paint as they are internal
things that very rarely use any time, and also flush/resume-events
as any events reported here will get separate marks so will be easy
to see anyway.

Also, we rename the entire frameclock cycle to "frameclock cycle"
rather than "paint_idle" which is rather cryptic.
2020-02-12 13:20:32 +01:00
Alexander Larsson
2890cd849f profiler: Add _end_mark() version of _add_mark()
These don't take a duration, instead they call g_get_monotonic_time() to
and subtract the start time for it.

Almost all our calls are like this, and this makes the callsites clearer
and avoids inlining the clock call into the call site.
2020-02-12 11:25:34 +01:00
Alexander Larsson
01d5ad2056 profiler: Make profiler-is-running a macro
When we use if (GDK_PROFILER_IS_RUNNING) this means we get an
inlined if (FALSE) when the compiler support is not compiled in, which
gets rid of all the related code completely.

We also expand to  G_UNLIKELY(gdk_profiler_is_running ()) in the supported
case which might cause somewhat better code generation.
2020-02-12 11:05:01 +01:00
Alexander Larsson
cc643df88b Convert all profiler times from nsec to usec
usec is the scale of the monotonic timer which is where we get almost
all the times from. The only actual source of nsec is the opengl
GPU time (but who knows what the actual resulution of that is).

Changing this to usec allows us to get rid of " * 1000" in a *lot* of
places all over the codebase, which are ugly and confusing.
2020-02-12 10:44:17 +01:00
Matthias Clasen
59a31e05bc gdk: Add a profiler mark for paint_idle
This is the big frame clock function where most
signals are emitted, and it can keep us away from
the mainloop for a long time.
2020-01-22 23:44:01 -05:00
Matthias Clasen
3c03d7970a More profiler reshuffling
Add more of the marks without requiring a debug build.
2020-01-22 23:44:01 -05:00
Matthias Clasen
56b8d1dfbe frame clock: Redo the profiler marks
Instead of reporting the frame clock phases as defined,
report the duration of the signal emissions, which is more
useful for tracking down what is taking time.
2020-01-22 16:36:02 -05:00
Matthias Clasen
7e59ec4afc Add clock freezes to the profile 2019-05-16 20:50:31 +00:00
Matthias Clasen
15fec034df always record a frame end time when profiling
We need to keep this time, or we may end up with
negative frame intervals.
2019-05-16 19:45:30 +00:00
Matthias Clasen
33ffd54d5f Redo frame profiling
We were adding incomplete frame timings to the
profile, which lead to occasional nonsense
numbers. Instead, only add timings to the profile
once we marked them as complete. This also
gives us an opportunity to add the presentation
time as a marker.
2019-05-16 19:08:34 +00:00
Matthias Clasen
ebc6bd834b profiling: Only use frame timings when we have them
Christian Hergert reported a crash here,
when testing the profiling code.
2019-05-14 12:06:55 +00:00
Matthias Clasen
1660bac6bd frame clock: Add tracing
Emit tracing data for frames. For now, we emit the
entire frame, and the layout and paint phases. Also
emit frames-per-second.
2019-05-07 09:37:59 -07:00
Benjamin Otte
0791924bf7 Convert fallthrough comments to G_GNUC_FALLTHROUGH
This way it's very obvious that fallthrough is indeed what we want.

Also bump the glib requirement to 2.59 which introduced
G_GNUC_FALLTHROUGH.
2019-04-15 14:40:25 +02:00
Chris Williams
0bf4c2420d gdkframeclockidle: Don't permanently skew frame time
Since commit 3b2f9395, the frame time may be set into the future, so
only ensure monotonicity, and don't store the offset. This prevents the
frame time from becoming out of sync with g_get_monotonic_time().

Fixes #1612
2019-04-09 16:26:25 -04:00
Chris Williams
f813bc1823 gdkframeclockidle: Remove unused struct member
compute_frame_time() has been using g_get_monotonic_time() instead of
GTimer since commit 7dfa4121.
2019-04-08 20:37:00 -04:00
Emmanuele Bassi
45361e19c3 Rename more GSources to use gtk 2019-02-06 10:37:24 +01:00
Benjamin Otte
50b8ee3a5f frameclockidle: Rename header so it's private now 2018-03-29 05:02:15 +02:00
Emmanuele Bassi
c655759cef Replace gdk_threads_add_timeout* with g_timeout_add()
The main GDK thread lock is not portable and deprecated.

The only reason why gdk_threads_add_timeout() and
gdk_threads_add_timeout_full() exist is to allow invoking a callback
with the GDK lock held, in case 3rd party libraries still use the
deprecated gdk_threads_enter()/gdk_threads_leave() API.

Since we're removing the GDK lock, and we're releasing a new major API,
such code cannot exist any more; this means we can use the GLib API for
installing timeout callbacks.

https://bugzilla.gnome.org/show_bug.cgi?id=793124
2018-02-03 12:06:58 +01:00
Daniel van Vugt
3b2f939590 Fix irregular gdk_frame_clock_get_frame_time
This fixes stuttering in animations that rely on the regularity of
gdk_frame_clock_get_frame_time.

https://bugzilla.gnome.org/show_bug.cgi?id=787665

BEFORE
gdkgears:
58 FPS and visibly stuttering
gnome-maps on a 59.95Hz monitor:
"paint" g_get_monotonic_time +17278μs, gdk_frame_clock_get_frame_time +17278μs
"paint" g_get_monotonic_time +17449μs, gdk_frame_clock_get_frame_time +17426μs
"paint" g_get_monotonic_time +17620μs, gdk_frame_clock_get_frame_time +17600μs

AFTER
gdkgears:
60 FPS and smoother
gnome-maps on a 59.95Hz monitor:
"paint" g_get_monotonic_time +18228μs, gdk_frame_clock_get_frame_time +16680μs
"paint" g_get_monotonic_time +15010μs, gdk_frame_clock_get_frame_time +16680μs
"paint" g_get_monotonic_time +17134μs, gdk_frame_clock_get_frame_time +16680μs
2017-12-09 20:52:36 -05:00
Benjamin Otte
43c212ac28 build: Enable -Wswitch-enum and -Wswitch-default
This patch makes that work using 1 of 2 options:

1. Add all missing enums to the switch statement
  or
2. Cast the switch argument to a uint to avoid having to do that (mostly
   for GdkEventType).

I even found a bug while doing that: clearing a GtkImage with a surface
did not notify thae surface property.

The reason for enabling this flag even though it is tedious at times is
that it is very useful when adding values to an enum, because it makes
GTK immediately warn about all the switch statements where this enum is
relevant.
And I expect changes to enums to be frequent during the GTK4 development
cycle.
2017-10-06 21:23:39 +02:00
Christian Hergert
b584b3d12a frame-clock: avoid g_signal_emit_by_name()
These were showing up higher in Sysprof profiles.

The simple fix is to avoid the emit_by_name() and let the interface emit
the signals directly. No function preconditions are provided since these
are internal API.
2016-04-18 15:33:53 -07:00
Matthias Clasen
ee217dc823 Add and use GDK_CHECK_DEBUG macro
This is following what we've already done in GTK+. It avoids
direct access to _gdk_debug_flags all over the place.
2016-02-28 21:40:30 -05:00
Andika Triwidada
0a768d274d Changed obsolete FSF portal addresses to web address
Fixed https://bugzilla.gnome.org/show_bug.cgi?id=721530
2014-01-12 19:56:49 +07:00
Bastien Nocera
438cd857c4 all: Add names to timeouts
Add names to every timeout we setup, so it's easier to track their
usage, and debug possible misbehaviour.

https://bugzilla.gnome.org/show_bug.cgi?id=710651
2013-10-23 13:31:18 +02:00
Jasper St. Pierre
c6de1cd6b9 gdkframeclockidle: Add explicit fallthrough markers
So people reading the code don't get confused.
2013-09-03 17:59:19 -04:00
Emmanuele Bassi
9f2ca8d851 gdk: Use new macros for defining private data
Drop the g_type_class_add_private() function, and use the macros
instead.

https://bugzilla.gnome.org/show_bug.cgi?id=702996
2013-07-09 09:28:49 +01:00
Alexander Larsson
c3bff30b50 gdkframeclock: Loop the layout phase if needed
In the case where the layout phase queued a layout we don't
want to progress to the paint phase with invalid allocations, so
we loop the layout. This shouldn't normally happen, but it may
happen in some edge cases like if user/wm resizes clash with
natural window size changes from a gtk widget. This should not
generally loop though, so we detect this after 4 cycles and
print a warning.

This was detected because of an issue in GtkWindow where it
seems to incorrectly handle the case of a user interactive resize.
It seems gtk_window_move_resize() believes that configure_request_size_changed
changed due to hitting some corner case so it calls
gtk_widget_queue_resize_no_redraw(), marking the window as need_alloc
after the layout phase. This commit fixes the issue, but we should
also look into if we can fix that.
2013-05-07 16:33:01 +02:00
Thomas Wood
e97262489c gdk: prevent NULL pointer access when debugging is enabled
If no updates, redraws, or repaints have been scheduled for this frame,
we will skip immediately to RESUME_EVENTS, and no GdkFrameTimings will
be created.

https://bugzilla.gnome.org/show_bug.cgi?id=694732
2013-02-26 15:27:53 +00:00
Matthias Clasen
82928c2a2a Pedantic fixes
Remove a C99 comment, and line up comment formatting.
2013-02-25 10:54:32 +01:00
Alexander Larsson
3c690c52ba Avoid loop in paint update cycle
Commit 1db87c897f accidentally removed
a check for !in_paint_idle in maybe_start_idle which causes us
to create a paint loop whenever something requests a phase
inside the paint_idle.
2013-02-19 21:12:48 +01:00
Alexander Larsson
5dbf814f0c win32: Request higher precision timers during animations
The default windows timer resolution is 16msec, which is too little
for fluent animations (say at 60Hz). So, while a paint clock is
active we temporarily raise the timer resolution to 1 msec.
2013-02-18 17:14:25 +01:00
Owen W. Taylor
1db87c897f Add gdk_frame_clock_begin/end_updating()
Add an API to start or stop continually updating the frame clock.
This is a slight convenience for applcations and avoids the problem
of getting one more frame run after an animation stops, but the
primary motivation for this is because it looks like we might have
to use timeBeginPeriod()/timeEndPeriod() on Windows to get reasonably
accurate timing, and for that we'll need to know if there is an
animation running.

https://bugzilla.gnome.org/show_bug.cgi?id=693934
2013-02-18 17:14:24 +01:00
Owen W. Taylor
d203f63641 GdkFrameClockIdle: remove timeouts in dispose
When a frame clock is disposed, remove active timeouts; also get
rid of the last traces of an unused GTimer.
2013-02-15 22:28:52 -05:00
Jasper St. Pierre
b777a16b06 gdkframeclockidle: Don't expose sleep_source source funcs 2013-02-15 19:48:56 -05:00