summaryrefslogtreecommitdiff
path: root/hw/xwayland/xwayland-present.c
blob: f403ff701dba7b423b73eb703f45f59fbe83d994 (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
/*
 * Copyright © 2018 Roman Gilg
 *
 * Permission to use, copy, modify, distribute, and sell this software
 * and its documentation for any purpose is hereby granted without
 * fee, provided that the above copyright notice appear in all copies
 * and that both that copyright notice and this permission notice
 * appear in supporting documentation, and that the name of the
 * copyright holders not be used in advertising or publicity
 * pertaining to distribution of the software without specific,
 * written prior permission.  The copyright holders make no
 * representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied
 * warranty.
 *
 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 * SOFTWARE.
 */

#include "xwayland.h"

#include <present.h>

/*
 * When not flipping let Present copy with 60fps.
 * When flipping wait on frame_callback, otherwise
 * the surface is not visible, in this case update
 * with long interval.
 */
#define TIMER_LEN_COPY      17  // ~60fps
#define TIMER_LEN_FLIP    1000  // 1fps

static void
xwl_present_free_timer(struct xwl_window *xwl_window)
{
    TimerFree(xwl_window->present_timer);
    xwl_window->present_timer = NULL;
}

static CARD32
xwl_present_timer_callback(OsTimerPtr timer,
                           CARD32 time,
                           void *arg);

static inline Bool
xwl_present_has_events(struct xwl_window *xwl_window)
{
    return !xorg_list_is_empty(&xwl_window->present_event_list) ||
           !xorg_list_is_empty(&xwl_window->present_release_queue);
}

static void
xwl_present_reset_timer(struct xwl_window *xwl_window)
{
    if (xwl_present_has_events(xwl_window)) {
        uint32_t timer_len = xwl_window->present_window ? TIMER_LEN_FLIP :
                                                          TIMER_LEN_COPY;

        xwl_window->present_timer = TimerSet(xwl_window->present_timer,
                                             0,
                                             timer_len,
                                             &xwl_present_timer_callback,
                                             xwl_window);
    } else {
        xwl_present_free_timer(xwl_window);
    }
}

void
xwl_present_cleanup(WindowPtr window)
{
    struct xwl_window           *xwl_window = xwl_window_of_top(window);
    struct xwl_present_event    *event, *tmp;

    if (!xwl_window)
        return;

    if (xwl_window->present_window == window) {
        if (xwl_window->present_frame_callback) {
            wl_callback_destroy(xwl_window->present_frame_callback);
            xwl_window->present_frame_callback = NULL;
        }
        xwl_window->present_window = NULL;
    }

    /* Clear remaining events */
    xorg_list_for_each_entry_safe(event, tmp, &xwl_window->present_event_list, list) {
        if (event->present_window == window) {
            xorg_list_del(&event->list);
            free(event);
        }
    }

    /* Clear remaining buffer releases and inform Present about free ressources */
    xorg_list_for_each_entry_safe(event, tmp, &xwl_window->present_release_queue, list) {
        if (event->present_window == window) {
            xorg_list_del(&event->list);
            event->abort = TRUE;
        }
    }

    /* Clear timer */
    if (!xwl_present_has_events(xwl_window))
        xwl_present_free_timer(xwl_window);
}

static void
xwl_present_free_event(struct xwl_present_event *event)
{
    xorg_list_del(&event->list);
    free(event);
}

static void
xwl_present_buffer_release(void *data, struct wl_buffer *buffer)
{
    struct xwl_present_event *event = data;

    if (!event)
        return;
    wl_buffer_set_user_data(buffer, NULL);

    event->buffer_released = TRUE;

    if (event->abort) {
        if (!event->pending)
            xwl_present_free_event(event);
        return;
    }

    if (!event->pending) {
        present_wnmd_event_notify(event->present_window,
                                  event->event_id,
                                  event->xwl_window->present_ust,
                                  event->xwl_window->present_msc);
        xwl_present_free_event(event);
    }
}

static const struct wl_buffer_listener xwl_present_release_listener = {
    xwl_present_buffer_release
};

static void
xwl_present_events_notify(struct xwl_window *xwl_window)
{
    uint64_t                    msc = xwl_window->present_msc;
    struct xwl_present_event    *event, *tmp;

    xorg_list_for_each_entry_safe(event, tmp,
                                  &xwl_window->present_event_list,
                                  list) {
        if (event->target_msc <= msc) {
            present_wnmd_event_notify(event->present_window,
                                      event->event_id,
                                      xwl_window->present_ust,
                                      msc);
            xwl_present_free_event(event);
        }
    }
}

CARD32
xwl_present_timer_callback(OsTimerPtr timer,
                           CARD32 time,
                           void *arg)
{
    struct xwl_window *xwl_window = arg;

    xwl_window->present_timer_firing = TRUE;
    xwl_window->present_msc++;
    xwl_window->present_ust = GetTimeInMicros();

    xwl_present_events_notify(xwl_window);

    if (xwl_present_has_events(xwl_window)) {
        /* Still events, restart timer */
        return xwl_window->present_window ? TIMER_LEN_FLIP :
                                            TIMER_LEN_COPY;
    } else {
        /* No more events, do not restart timer and delete it instead */
        xwl_present_free_timer(xwl_window);
        return 0;
    }
}

static void
xwl_present_frame_callback(void *data,
               struct wl_callback *callback,
               uint32_t time)
{
    struct xwl_window *xwl_window = data;

    wl_callback_destroy(xwl_window->present_frame_callback);
    xwl_window->present_frame_callback = NULL;

    if (xwl_window->present_timer_firing) {
        /* If the timer is firing, this frame callback is too late */
        return;
    }

    xwl_window->present_msc++;
    xwl_window->present_ust = GetTimeInMicros();

    xwl_present_events_notify(xwl_window);

    /* we do not need the timer anymore for this frame,
     * reset it for potentially the next one
     */
    xwl_present_reset_timer(xwl_window);
}

static const struct wl_callback_listener xwl_present_frame_listener = {
    xwl_present_frame_callback
};

static void
xwl_present_sync_callback(void *data,
               struct wl_callback *callback,
               uint32_t time)
{
    struct xwl_present_event *event = data;
    struct xwl_window *xwl_window = event->xwl_window;

    event->pending = FALSE;

    if (event->abort) {
        /* Event might have been aborted */
        if (event->buffer_released)
            /* Buffer was already released, cleanup now */
            xwl_present_free_event(event);
        return;
    }

    present_wnmd_event_notify(event->present_window,
                              event->event_id,
                              xwl_window->present_ust,
                              xwl_window->present_msc);

    if (event->buffer_released)
        /* If the buffer was already released, send the event now again */
        present_wnmd_event_notify(event->present_window,
                                  event->event_id,
                                  xwl_window->present_ust,
                                  xwl_window->present_msc);
}

static const struct wl_callback_listener xwl_present_sync_listener = {
    xwl_present_sync_callback
};

static RRCrtcPtr
xwl_present_get_crtc(WindowPtr present_window)
{
    struct xwl_window *xwl_window = xwl_window_of_top(present_window);
    if (xwl_window == NULL)
        return NULL;

    return xwl_window->present_crtc_fake;
}

static int
xwl_present_get_ust_msc(WindowPtr present_window, uint64_t *ust, uint64_t *msc)
{
    struct xwl_window *xwl_window = xwl_window_of_top(present_window);
    if (!xwl_window)
        return BadAlloc;
    *ust = xwl_window->present_ust;
    *msc = xwl_window->present_msc;

    return Success;
}

static void
xwl_present_set_present_window(struct xwl_window *xwl_window,
                               WindowPtr present_window)
{
    if (xwl_window->present_window)
        return;

    xwl_window->present_window = present_window;
}

/*
 * Queue an event to report back to the Present extension when the specified
 * MSC has past
 */
static int
xwl_present_queue_vblank(WindowPtr present_window,
                         RRCrtcPtr crtc,
                         uint64_t event_id,
                         uint64_t msc)
{
    struct xwl_window *xwl_window = xwl_window_of_top(present_window);
    struct xwl_present_event *event;

    if (!xwl_window)
        return BadMatch;

    if (xwl_window->present_crtc_fake != crtc)
        return BadRequest;

    if (xwl_window->present_window &&
            xwl_window->present_window != present_window)
        return BadMatch;

    event = malloc(sizeof *event);
    if (!event)
        return BadAlloc;

    event->event_id = event_id;
    event->present_window = present_window;
    event->xwl_window = xwl_window;
    event->target_msc = msc;

    xorg_list_append(&event->list, &xwl_window->present_event_list);

    if (!xwl_window->present_timer)
        xwl_present_reset_timer(xwl_window);

    return Success;
}

/*
 * Remove a pending vblank event so that it is not reported
 * to the extension
 */
static void
xwl_present_abort_vblank(WindowPtr present_window,
                         RRCrtcPtr crtc,
                         uint64_t event_id,
                         uint64_t msc)
{
    struct xwl_window *xwl_window = xwl_window_of_top(present_window);
    struct xwl_present_event *event, *tmp;

    if (!xwl_window)
        return;

    xorg_list_for_each_entry_safe(event, tmp, &xwl_window->present_event_list, list) {
        if (event->event_id == event_id) {
            xorg_list_del(&event->list);
            free(event);
            return;
        }
    }

    xorg_list_for_each_entry(event, &xwl_window->present_release_queue, list) {
        if (event->event_id == event_id) {
            event->abort = TRUE;
            return;
        }
    }
}

static void
xwl_present_flush(WindowPtr window)
{
    /* Only called when a Pixmap is copied instead of flipped,
     * but in this case we wait on the next block_handler.
     */
}

static Bool
xwl_present_check_flip2(RRCrtcPtr crtc,
                        WindowPtr present_window,
                        PixmapPtr pixmap,
                        Bool sync_flip,
                        PresentFlipReason *reason)
{
    struct xwl_window *xwl_window = xwl_window_of_top(present_window);

    if (!xwl_window)
        return FALSE;

    /*
     * Do not flip if there is already another child window doing flips.
     */
    if (xwl_window->present_window &&
            xwl_window->present_window != present_window)
        return FALSE;

    if (!xwl_window->present_crtc_fake)
        return FALSE;
    /*
     * Make sure the client doesn't try to flip to another crtc
     * than the one created for 'xwl_window'.
     */
    if (xwl_window->present_crtc_fake != crtc)
        return FALSE;

    /*
     * We currently only allow flips of windows, that have the same
     * dimensions as their xwl_window parent window. For the case of
     * different sizes subsurfaces are presumably the way forward.
     */
    if (!RegionEqual(&xwl_window->window->winSize, &present_window->winSize))
        return FALSE;

    return TRUE;
}

static Bool
xwl_present_flip(WindowPtr present_window,
                 RRCrtcPtr crtc,
                 uint64_t event_id,
                 uint64_t target_msc,
                 PixmapPtr pixmap,
                 Bool sync_flip,
                 RegionPtr damage)
{
    struct xwl_window           *xwl_window = xwl_window_of_top(present_window);
    BoxPtr                      present_box, damage_box;
    Bool                        buffer_created;
    struct wl_buffer            *buffer;
    struct xwl_present_event    *event;

    present_box = RegionExtents(&present_window->winSize);
    damage_box = RegionExtents(damage);

    xwl_present_set_present_window(xwl_window, present_window);

    event = malloc(sizeof *event);
    if (!event)
        return FALSE;

    buffer = xwl_glamor_pixmap_get_wl_buffer(pixmap,
                                             present_box->x2 - present_box->x1,
                                             present_box->y2 - present_box->y1,
                                             &buffer_created);

    event->event_id = event_id;
    event->present_window = present_window;
    event->xwl_window = xwl_window;
    event->buffer = buffer;
    event->target_msc = xwl_window->present_msc;
    event->pending = TRUE;
    event->abort = FALSE;
    event->buffer_released = FALSE;

    xorg_list_add(&event->list, &xwl_window->present_release_queue);

    if (buffer_created)
        wl_buffer_add_listener(buffer, &xwl_present_release_listener, NULL);
    wl_buffer_set_user_data(buffer, event);

    /* We can flip directly to the main surface (full screen window without clips) */
    wl_surface_attach(xwl_window->surface, buffer, 0, 0);

    if (!xwl_window->present_timer ||
            xwl_window->present_timer_firing) {
        /* Realign timer */
        xwl_window->present_timer_firing = FALSE;
        xwl_present_reset_timer(xwl_window);
    }

    if (!xwl_window->present_frame_callback) {
        xwl_window->present_frame_callback = wl_surface_frame(xwl_window->surface);
        wl_callback_add_listener(xwl_window->present_frame_callback,
                                 &xwl_present_frame_listener,
                                 xwl_window);
    }

    wl_surface_damage(xwl_window->surface, 0, 0,
                      damage_box->x2 - damage_box->x1,
                      damage_box->y2 - damage_box->y1);

    wl_surface_commit(xwl_window->surface);

    xwl_window->present_sync_callback = wl_display_sync(xwl_window->xwl_screen->display);
    wl_callback_add_listener(xwl_window->present_sync_callback,
                             &xwl_present_sync_listener,
                             event);

    wl_display_flush(xwl_window->xwl_screen->display);
    return TRUE;
}

static void
xwl_present_flips_stop(WindowPtr window)
{
    struct xwl_window *xwl_window = xwl_window_of_top(window);

    if (!xwl_window)
        return;

    assert(xwl_window->present_window == window);

    xwl_window->present_window = NULL;

    /* Change back to the fast refresh rate */
    xwl_present_reset_timer(xwl_window);
}

static present_wnmd_info_rec xwl_present_info = {
    .version = PRESENT_SCREEN_INFO_VERSION,
    .get_crtc = xwl_present_get_crtc,

    .get_ust_msc = xwl_present_get_ust_msc,
    .queue_vblank = xwl_present_queue_vblank,
    .abort_vblank = xwl_present_abort_vblank,

    .flush = xwl_present_flush,

    .capabilities = PresentCapabilityAsync,
    .check_flip2 = xwl_present_check_flip2,
    .flip = xwl_present_flip,
    .flips_stop = xwl_present_flips_stop
};

Bool
xwl_present_init(ScreenPtr screen)
{
    return present_wnmd_screen_init(screen, &xwl_present_info);
}