summaryrefslogtreecommitdiff
path: root/src/wayland/meta-wayland-presentation-time.c
blob: 54351690ac160c09d70e9eafad9bd33c48b44c87 (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
/*
 * presentation-time protocol
 *
 * Copyright (C) 2020 Ivan Molodetskikh <yalterz@gmail.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program 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
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 */

#include "config.h"

#include "meta-wayland-presentation-time-private.h"

#include <glib.h>

#include "wayland/meta-wayland-private.h"
#include "wayland/meta-wayland-surface.h"
#include "wayland/meta-wayland-outputs.h"
#include "wayland/meta-wayland-versions.h"

#include "presentation-time-server-protocol.h"

static void
wp_presentation_feedback_destructor (struct wl_resource *resource)
{
  MetaWaylandPresentationFeedback *feedback =
    wl_resource_get_user_data (resource);

  wl_list_remove (&feedback->link);
  g_free (feedback);
}

static void
wp_presentation_destroy (struct wl_client   *client,
                         struct wl_resource *resource)
{
  wl_resource_destroy (resource);
}

static void
wp_presentation_feedback (struct wl_client   *client,
                          struct wl_resource *resource,
                          struct wl_resource *surface_resource,
                          uint32_t            callback_id)
{
  MetaWaylandSurface *surface = wl_resource_get_user_data (surface_resource);
  MetaWaylandSurfaceState *pending;
  MetaWaylandPresentationFeedback *feedback;

  feedback = g_new0 (MetaWaylandPresentationFeedback, 1);
  wl_list_init (&feedback->link);
  feedback->resource = wl_resource_create (client,
                                           &wp_presentation_feedback_interface,
                                           wl_resource_get_version (resource),
                                           callback_id);
  wl_resource_set_implementation (feedback->resource,
                                  NULL,
                                  feedback,
                                  wp_presentation_feedback_destructor);

  if (surface == NULL)
    {
      g_warn_if_reached ();
      meta_wayland_presentation_feedback_discard (feedback);
      return;
    }

  pending = meta_wayland_surface_get_pending_state (surface);
  wl_list_insert (&pending->presentation_feedback_list, &feedback->link);

  feedback->surface = surface;
}

static const struct wp_presentation_interface
meta_wayland_presentation_interface = {
  wp_presentation_destroy,
  wp_presentation_feedback,
};

static void
wp_presentation_bind (struct wl_client *client,
                      void             *data,
                      uint32_t          version,
                      uint32_t          id)
{
  struct wl_resource *resource;

  resource = wl_resource_create (client,
                                 &wp_presentation_interface,
                                 version,
                                 id);
  wl_resource_set_implementation (resource,
                                  &meta_wayland_presentation_interface,
                                  NULL,
                                  NULL);

  /* Presentation timestamps in Mutter are guaranteed to be CLOCK_MONOTONIC. */
  wp_presentation_send_clock_id (resource, CLOCK_MONOTONIC);
}

static void
destroy_feedback_list (gpointer data)
{
  struct wl_list *feedbacks = data;

  while (!wl_list_empty (feedbacks))
    {
      MetaWaylandPresentationFeedback *feedback =
        wl_container_of (feedbacks->next, feedback, link);

      meta_wayland_presentation_feedback_discard (feedback);
    }

  g_free (feedbacks);
}

static void
on_monitors_changed (MetaMonitorManager    *manager,
                     MetaWaylandCompositor *compositor)
{
  /* All ClutterStageViews were re-created, so clear our map. */
  g_hash_table_remove_all (compositor->presentation_time.feedbacks);
}

void
meta_wayland_init_presentation_time (MetaWaylandCompositor *compositor)
{
  MetaBackend *backend = compositor->backend;
  MetaMonitorManager *monitor_manager =
    meta_backend_get_monitor_manager (backend);

  compositor->presentation_time.feedbacks =
    g_hash_table_new_full (NULL, NULL, NULL, destroy_feedback_list);

  g_signal_connect (monitor_manager, "monitors-changed-internal",
                    G_CALLBACK (on_monitors_changed), compositor);

  if (wl_global_create (compositor->wayland_display,
                        &wp_presentation_interface,
                        META_WP_PRESENTATION_VERSION,
                        NULL,
                        wp_presentation_bind) == NULL)
    g_error ("Failed to register a global wp_presentation object");
}

void
meta_wayland_presentation_feedback_discard (MetaWaylandPresentationFeedback *feedback)
{
  wp_presentation_feedback_send_discarded (feedback->resource);
  wl_resource_destroy (feedback->resource);
}

static void
maybe_update_presentation_sequence (MetaWaylandSurface *surface,
                                    ClutterFrameInfo   *frame_info,
                                    MetaWaylandOutput  *output)
{
  unsigned int sequence_delta;

  if (!surface->presentation_time.needs_sequence_update)
    return;

  surface->presentation_time.needs_sequence_update = FALSE;

  if (!(frame_info->flags & CLUTTER_FRAME_INFO_FLAG_VSYNC))
    goto invalid_sequence;

  /* Getting sequence = 0 after sequence = UINT_MAX is likely valid (32-bit
   * overflow, on a 144 Hz display that's ~173 days of operation). Getting it
   * otherwise is usually a driver bug.
   */
  if (frame_info->sequence == 0 &&
      !(surface->presentation_time.is_last_output_sequence_valid &&
        surface->presentation_time.last_output_sequence == UINT_MAX))
    {
      g_warning_once ("Invalid sequence for VSYNC frame info");
      goto invalid_sequence;
    }

  if (surface->presentation_time.is_last_output_sequence_valid &&
      surface->presentation_time.last_output == output)
    {
      sequence_delta =
        frame_info->sequence - surface->presentation_time.last_output_sequence;
    }
  else
    {
      /* Sequence generally has different base between different outputs, but we
       * want to keep it monotonic and without sudden jumps when the surface is
       * moved between outputs. This matches the Xorg behavior with regards to
       * the GLX_OML_sync_control implementation.
       */
      sequence_delta = 1;
    }

  surface->presentation_time.sequence += sequence_delta;
  surface->presentation_time.last_output = output;
  surface->presentation_time.last_output_sequence = frame_info->sequence;
  surface->presentation_time.is_last_output_sequence_valid = TRUE;

  return;

invalid_sequence:
  surface->presentation_time.sequence += 1;
  surface->presentation_time.last_output = output;
  surface->presentation_time.is_last_output_sequence_valid = FALSE;
}

void
meta_wayland_presentation_feedback_present (MetaWaylandPresentationFeedback *feedback,
                                            ClutterFrameInfo                *frame_info,
                                            MetaWaylandOutput               *output)
{
  MetaWaylandSurface *surface = feedback->surface;
  int64_t time_us = frame_info->presentation_time;
  uint64_t time_s;
  uint32_t tv_sec_hi, tv_sec_lo, tv_nsec;
  uint32_t refresh_interval_ns;
  uint32_t seq_hi, seq_lo;
  uint32_t flags;
  GList *l;

  if (output == NULL)
    {
      g_warning ("Output is NULL while sending presentation feedback");
      meta_wayland_presentation_feedback_discard (feedback);
      return;
    }

  time_s = us2s (time_us);

  tv_sec_hi = time_s >> 32;
  tv_sec_lo = time_s;
  tv_nsec = (uint32_t) us2ns (time_us - s2us (time_s));

  refresh_interval_ns = (uint32_t) (0.5 + s2ns (1) / frame_info->refresh_rate);

  maybe_update_presentation_sequence (surface, frame_info, output);

  seq_hi = surface->presentation_time.sequence >> 32;
  seq_lo = surface->presentation_time.sequence;

  flags = WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION;

  if (frame_info->flags & CLUTTER_FRAME_INFO_FLAG_HW_CLOCK)
    flags |= WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;

  if (frame_info->flags & CLUTTER_FRAME_INFO_FLAG_ZERO_COPY)
    flags |= WP_PRESENTATION_FEEDBACK_KIND_ZERO_COPY;

  if (frame_info->flags & CLUTTER_FRAME_INFO_FLAG_VSYNC)
    flags |= WP_PRESENTATION_FEEDBACK_KIND_VSYNC;

  for (l = output->resources; l; l = l->next)
    {
      struct wl_resource *output_resource = l->data;

      if (feedback->resource->client == output_resource->client)
        {
          wp_presentation_feedback_send_sync_output (feedback->resource,
                                                     output_resource);
        }
    }

  wp_presentation_feedback_send_presented (feedback->resource,
                                           tv_sec_hi,
                                           tv_sec_lo,
                                           tv_nsec,
                                           refresh_interval_ns,
                                           seq_hi,
                                           seq_lo,
                                           flags);

  wl_resource_destroy (feedback->resource);
}

struct wl_list *
meta_wayland_presentation_time_ensure_feedbacks (MetaWaylandPresentationTime *presentation_time,
                                                 ClutterStageView            *stage_view)
{
  if (!g_hash_table_contains (presentation_time->feedbacks, stage_view))
    {
      struct wl_list *list;

      list = g_new0 (struct wl_list, 1);
      wl_list_init (list);
      g_hash_table_insert (presentation_time->feedbacks, stage_view, list);

      return list;
    }

  return g_hash_table_lookup (presentation_time->feedbacks, stage_view);
}