xwayland-present.c 17.1 KB
Newer Older
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
/*
 * 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.
 */

26
#include <xwayland-config.h>
27

28
#include <windowstr.h>
29
30
#include <present.h>

31
#include "xwayland-present.h"
32
#include "xwayland-screen.h"
33
#include "xwayland-window.h"
34
#include "xwayland-pixmap.h"
35
36
#include "glamor.h"

37
38
39
40
41
42
43
/*
 * 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
44
45
#define TIMER_LEN_FLIP    1000  // 1fps

46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
static DevPrivateKeyRec xwl_present_window_private_key;

static struct xwl_present_window *
xwl_present_window_priv(WindowPtr window)
{
    return dixGetPrivate(&window->devPrivates,
                         &xwl_present_window_private_key);
}

static struct xwl_present_window *
xwl_present_window_get_priv(WindowPtr window)
{
    struct xwl_present_window *xwl_present_window = xwl_present_window_priv(window);

    if (xwl_present_window == NULL) {
        xwl_present_window = calloc (1, sizeof (struct xwl_present_window));
        if (!xwl_present_window)
            return NULL;

        xwl_present_window->window = window;
        xwl_present_window->msc = 1;
        xwl_present_window->ust = GetTimeInMicros();

69
        xorg_list_init(&xwl_present_window->frame_callback_list);
70
71
72
73
74
75
76
77
78
79
80
        xorg_list_init(&xwl_present_window->event_list);
        xorg_list_init(&xwl_present_window->release_queue);

        dixSetPrivate(&window->devPrivates,
                      &xwl_present_window_private_key,
                      xwl_present_window);
    }

    return xwl_present_window;
}

81
static void
82
xwl_present_free_timer(struct xwl_present_window *xwl_present_window)
83
{
84
85
    TimerFree(xwl_present_window->frame_timer);
    xwl_present_window->frame_timer = NULL;
86
87
88
89
90
91
92
}

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

93
static inline Bool
94
xwl_present_has_events(struct xwl_present_window *xwl_present_window)
95
{
96
    return !!xwl_present_window->sync_flip ||
97
           !xorg_list_is_empty(&xwl_present_window->event_list);
98
99
}

100
static void
101
xwl_present_reset_timer(struct xwl_present_window *xwl_present_window)
102
{
103
    if (xwl_present_has_events(xwl_present_window)) {
104
105
        CARD32 timeout;

106
        if (!xorg_list_is_empty(&xwl_present_window->frame_callback_list))
107
108
109
            timeout = TIMER_LEN_FLIP;
        else
            timeout = TIMER_LEN_COPY;
110
111

        xwl_present_window->frame_timer = TimerSet(xwl_present_window->frame_timer,
112
                                                   0, timeout,
113
114
                                                   &xwl_present_timer_callback,
                                                   xwl_present_window);
115
    } else {
116
        xwl_present_free_timer(xwl_present_window);
117
118
119
    }
}

120
static void
121
xwl_present_release_pixmap(struct xwl_present_event *event)
122
{
123
    if (!event->pixmap)
124
125
        return;

126
127
128
129
    xwl_pixmap_del_buffer_release_cb(event->pixmap);
    dixDestroyPixmap(event->pixmap, event->pixmap->drawable.id);
    event->pixmap = NULL;
}
130

131
132
133
134
135
static void
xwl_present_free_event(struct xwl_present_event *event)
{
    if (!event)
        return;
136

137
    xwl_present_release_pixmap(event);
138
139
140
141
    xorg_list_del(&event->list);
    free(event);
}

142
void
143
xwl_present_cleanup(WindowPtr window)
144
{
145
    struct xwl_present_window *xwl_present_window = xwl_present_window_priv(window);
146
    struct xwl_present_event *event, *tmp;
147

148
    if (!xwl_present_window)
149
150
        return;

151
    xorg_list_del(&xwl_present_window->frame_callback_list);
152

153
154
155
156
157
    if (xwl_present_window->sync_callback) {
        wl_callback_destroy(xwl_present_window->sync_callback);
        xwl_present_window->sync_callback = NULL;
    }

158
    /* Clear remaining events */
159
160
    xorg_list_for_each_entry_safe(event, tmp, &xwl_present_window->event_list, list)
        xwl_present_free_event(event);
161

162
163
164
165
    xwl_present_free_event(xwl_present_window->sync_flip);

    xorg_list_for_each_entry_safe(event, tmp, &xwl_present_window->release_queue, list)
        xwl_present_free_event(event);
166
167

    /* Clear timer */
168
169
    xwl_present_free_timer(xwl_present_window);

170
171
172
173
174
    /* Remove from privates so we don't try to access it later */
    dixSetPrivate(&window->devPrivates,
                  &xwl_present_window_private_key,
                  NULL);

175
    free(xwl_present_window);
176
177
178
}

static void
179
xwl_present_buffer_release(void *data)
180
181
{
    struct xwl_present_event *event = data;
182

183
184
185
    if (!event)
        return;

186
    xwl_present_release_pixmap(event);
187
188
189
190
191
192
193
194

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

    if (!event->pending) {
195
        present_wnmd_event_notify(event->xwl_present_window->window,
196
                                  event->event_id,
197
198
                                  event->xwl_present_window->ust,
                                  event->xwl_present_window->msc);
199
200
201
202
        xwl_present_free_event(event);
    }
}

203
static void
204
xwl_present_msc_bump(struct xwl_present_window *xwl_present_window)
205
{
206
    uint64_t msc = ++xwl_present_window->msc;
207
208
    struct xwl_present_event    *event, *tmp;

209
210
    xwl_present_window->ust = GetTimeInMicros();

211
212
213
214
215
216
217
218
    event = xwl_present_window->sync_flip;
    xwl_present_window->sync_flip = NULL;
    if (event) {
        event->pending = FALSE;

        present_wnmd_event_notify(xwl_present_window->window, event->event_id,
                                  xwl_present_window->ust, msc);

219
        if (!event->pixmap) {
220
221
222
            /* If the buffer was already released, clean up now */
            present_wnmd_event_notify(xwl_present_window->window, event->event_id,
                                      xwl_present_window->ust, msc);
223
            xwl_present_free_event(event);
224
225
226
227
228
        } else {
            xorg_list_add(&event->list, &xwl_present_window->release_queue);
        }
    }

229
    xorg_list_for_each_entry_safe(event, tmp,
230
                                  &xwl_present_window->event_list,
231
232
                                  list) {
        if (event->target_msc <= msc) {
233
            present_wnmd_event_notify(xwl_present_window->window,
234
                                      event->event_id,
235
                                      xwl_present_window->ust,
236
237
238
239
240
241
                                      msc);
            xwl_present_free_event(event);
        }
    }
}

242
static CARD32
243
244
245
246
xwl_present_timer_callback(OsTimerPtr timer,
                           CARD32 time,
                           void *arg)
{
247
    struct xwl_present_window *xwl_present_window = arg;
248

249
250
251
252
    /* If we were expecting a frame callback for this window, it didn't arrive
     * in a second. Stop listening to it to avoid double-bumping the MSC
     */
    xorg_list_del(&xwl_present_window->frame_callback_list);
253

254
    xwl_present_msc_bump(xwl_present_window);
255
    xwl_present_reset_timer(xwl_present_window);
256

257
    return 0;
258
259
}

260
261
void
xwl_present_frame_callback(struct xwl_present_window *xwl_present_window)
262
{
263
    xorg_list_del(&xwl_present_window->frame_callback_list);
264

265
    xwl_present_msc_bump(xwl_present_window);
266

267
268
269
    /* we do not need the timer anymore for this frame,
     * reset it for potentially the next one
     */
270
    xwl_present_reset_timer(xwl_present_window);
271
272
273
274
275
276
277
278
}

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

281
282
283
    wl_callback_destroy(xwl_present_window->sync_callback);
    xwl_present_window->sync_callback = NULL;

284
285
286
287
    event->pending = FALSE;

    if (event->abort) {
        /* Event might have been aborted */
288
        if (!event->pixmap)
289
290
291
292
293
            /* Buffer was already released, cleanup now */
            xwl_present_free_event(event);
        return;
    }

294
    present_wnmd_event_notify(xwl_present_window->window,
295
                              event->event_id,
296
297
                              xwl_present_window->ust,
                              xwl_present_window->msc);
298

299
    if (!event->pixmap) {
300
        /* If the buffer was already released, send the event now again */
301
        present_wnmd_event_notify(xwl_present_window->window,
302
                                  event->event_id,
303
304
                                  xwl_present_window->ust,
                                  xwl_present_window->msc);
305
306
        xwl_present_free_event(event);
    }
307
308
309
310
311
312
313
314
315
}

static const struct wl_callback_listener xwl_present_sync_listener = {
    xwl_present_sync_callback
};

static RRCrtcPtr
xwl_present_get_crtc(WindowPtr present_window)
{
316
    struct xwl_present_window *xwl_present_window = xwl_present_window_get_priv(present_window);
317
318
    rrScrPrivPtr rr_private;

319
    if (xwl_present_window == NULL)
320
321
        return NULL;

322
    rr_private = rrGetScrPriv(present_window->drawable.pScreen);
323
324
325
326

    if (rr_private->numCrtcs == 0)
        return NULL;

327
    return rr_private->crtcs[0];
328
329
330
331
332
}

static int
xwl_present_get_ust_msc(WindowPtr present_window, uint64_t *ust, uint64_t *msc)
{
333
334
    struct xwl_present_window *xwl_present_window = xwl_present_window_get_priv(present_window);
    if (!xwl_present_window)
335
336
        return BadAlloc;

337
338
    *ust = xwl_present_window->ust;
    *msc = xwl_present_window->msc;
339

340
    return Success;
341
342
343
344
345
346
347
348
349
350
351
352
}

/*
 * 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)
{
353
    struct xwl_present_window *xwl_present_window = xwl_present_window_get_priv(present_window);
354
    struct xwl_window *xwl_window = xwl_window_from_window(present_window);
355
356
357
358
359
360
361
    struct xwl_present_event *event;

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

    event->event_id = event_id;
362
    event->pixmap = NULL;
363
    event->xwl_present_window = xwl_present_window;
364
365
    event->target_msc = msc;

366
    xorg_list_append(&event->list, &xwl_present_window->event_list);
367

368
369
370
371
372
373
374
375
376
    /* If there's a pending frame callback, use that */
    if (xwl_window && xwl_window->frame_callback &&
        xorg_list_is_empty(&xwl_present_window->frame_callback_list)) {
        xorg_list_add(&xwl_present_window->frame_callback_list,
                      &xwl_window->frame_callback_list);
    }

    if ((xwl_window && xwl_window->frame_callback) ||
        !xwl_present_window->frame_timer)
377
        xwl_present_reset_timer(xwl_present_window);
378
379

    return Success;
380
381
382
383
384
385
386
387
388
389
390
391
}

/*
 * 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)
{
392
    struct xwl_present_window *xwl_present_window = xwl_present_window_priv(present_window);
393
    struct xwl_present_event *event, *tmp;
394

395
    if (!xwl_present_window)
396
397
        return;

398
    xorg_list_for_each_entry_safe(event, tmp, &xwl_present_window->event_list, list) {
399
        if (event->event_id == event_id) {
400
            xwl_present_free_event(event);
401
402
403
404
            return;
        }
    }

405
    xorg_list_for_each_entry(event, &xwl_present_window->release_queue, list) {
406
407
408
409
410
411
412
413
414
415
        if (event->event_id == event_id) {
            event->abort = TRUE;
            return;
        }
    }
}

static void
xwl_present_flush(WindowPtr window)
{
416
    glamor_block_handler(window->drawable.pScreen);
417
418
419
420
421
422
423
424
425
}

static Bool
xwl_present_check_flip2(RRCrtcPtr crtc,
                        WindowPtr present_window,
                        PixmapPtr pixmap,
                        Bool sync_flip,
                        PresentFlipReason *reason)
{
426
    struct xwl_window *xwl_window = xwl_window_from_window(present_window);
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450

    if (!xwl_window)
        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)
{
451
    struct xwl_window           *xwl_window = xwl_window_from_window(present_window);
452
    struct xwl_present_window   *xwl_present_window = xwl_present_window_priv(present_window);
453
    BoxPtr                      damage_box;
454
455
456
    struct wl_buffer            *buffer;
    struct xwl_present_event    *event;

457
458
459
    if (!xwl_window)
        return FALSE;

460
461
462
463
464
465
    damage_box = RegionExtents(damage);

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

466
    pixmap->refcnt++;
467
    buffer = xwl_glamor_pixmap_get_wl_buffer(pixmap, NULL);
468
469

    event->event_id = event_id;
470
    event->xwl_present_window = xwl_present_window;
471
    event->pixmap = pixmap;
472
    event->target_msc = target_msc;
473
474
475
    event->pending = TRUE;
    event->abort = FALSE;

476
477
478
479
480
481
    if (sync_flip) {
        xorg_list_init(&event->list);
        xwl_present_window->sync_flip = event;
    } else {
        xorg_list_add(&event->list, &xwl_present_window->release_queue);
    }
482

483
    xwl_pixmap_set_buffer_release_cb(pixmap, xwl_present_buffer_release, event);
484
485
486
487

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

488
489
490
491
492
493
    if (!xwl_window->frame_callback)
        xwl_window_create_frame_callback(xwl_window);

    if (xorg_list_is_empty(&xwl_present_window->frame_callback_list)) {
        xorg_list_add(&xwl_present_window->frame_callback_list,
                      &xwl_window->frame_callback_list);
494
495
    }

496
497
498
    /* Realign timer */
    xwl_present_reset_timer(xwl_present_window);

499
    xwl_surface_damage(xwl_window->xwl_screen, xwl_window->surface,
500
501
                       damage_box->x1 - present_window->drawable.x,
                       damage_box->y1 - present_window->drawable.y,
502
503
                       damage_box->x2 - damage_box->x1,
                       damage_box->y2 - damage_box->y1);
504
505
506

    wl_surface_commit(xwl_window->surface);

507
508
509
510
511
512
513
    if (!sync_flip) {
        xwl_present_window->sync_callback =
            wl_display_sync(xwl_window->xwl_screen->display);
        wl_callback_add_listener(xwl_present_window->sync_callback,
                                 &xwl_present_sync_listener,
                                 event);
    }
514
515

    wl_display_flush(xwl_window->xwl_screen->display);
516
    xwl_window->present_flipped = TRUE;
517
518
519
520
521
522
    return TRUE;
}

static void
xwl_present_flips_stop(WindowPtr window)
{
523
    struct xwl_present_window   *xwl_present_window = xwl_present_window_priv(window);
524

525
    /* Change back to the fast refresh rate */
526
    xwl_present_reset_timer(xwl_present_window);
527
528
}

529
void
530
xwl_present_unrealize_window(struct xwl_present_window *xwl_present_window)
531
532
533
534
{
    /* The pending frame callback may never be called, so drop it and shorten
     * the frame timer interval.
     */
535
    xorg_list_del(&xwl_present_window->frame_callback_list);
536
537
538
    xwl_present_reset_timer(xwl_present_window);
}

539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
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)
{
558
559
560
    struct xwl_screen *xwl_screen = xwl_screen_get(screen);

    /*
561
     * doesn't work with the EGLStream backend.
562
     */
563
    if (xwl_screen->egl_backend == &xwl_screen->eglstream_backend)
564
565
        return FALSE;

566
567
568
    if (!dixRegisterPrivateKey(&xwl_present_window_private_key, PRIVATE_WINDOW, 0))
        return FALSE;

569
570
    return present_wnmd_screen_init(screen, &xwl_present_info);
}