xwayland-present.c 30.7 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

#define XWL_PRESENT_CAPS PresentCapabilityAsync


41
42
43
44
45
46
47
/*
 * 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
48
49
#define TIMER_LEN_FLIP    1000  // 1fps

50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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->msc = 1;
        xwl_present_window->ust = GetTimeInMicros();

72
        xorg_list_init(&xwl_present_window->frame_callback_list);
73
        xorg_list_init(&xwl_present_window->wait_list);
74
75
        xorg_list_init(&xwl_present_window->flip_queue);
        xorg_list_init(&xwl_present_window->idle_queue);
76
77
78
79
80
81
82
83
84

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

    return xwl_present_window;
}

85
86
87
88
89
90
static struct xwl_present_event *
xwl_present_event_from_id(uint64_t event_id)
{
    return (struct xwl_present_event*)(uintptr_t)event_id;
}

91
static void
92
xwl_present_free_timer(struct xwl_present_window *xwl_present_window)
93
{
94
95
    TimerFree(xwl_present_window->frame_timer);
    xwl_present_window->frame_timer = NULL;
96
97
98
99
100
101
102
}

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

103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
static present_vblank_ptr
xwl_present_get_pending_flip(struct xwl_present_window *xwl_present_window)
{
    present_vblank_ptr flip_pending;

    if (xorg_list_is_empty(&xwl_present_window->flip_queue))
        return NULL;

    flip_pending = xorg_list_first_entry(&xwl_present_window->flip_queue, present_vblank_rec,
                                         event_queue);

    if (flip_pending->queued)
        return NULL;

    return flip_pending;
}

120
static inline Bool
121
xwl_present_has_pending_events(struct xwl_present_window *xwl_present_window)
122
{
123
124
125
    present_vblank_ptr flip_pending = xwl_present_get_pending_flip(xwl_present_window);

    return (flip_pending && flip_pending->sync_flip) ||
126
           !xorg_list_is_empty(&xwl_present_window->wait_list);
127
128
}

129
static void
130
xwl_present_reset_timer(struct xwl_present_window *xwl_present_window)
131
{
132
    if (xwl_present_has_pending_events(xwl_present_window)) {
133
134
        CARD32 timeout;

135
        if (!xorg_list_is_empty(&xwl_present_window->frame_callback_list))
136
137
138
            timeout = TIMER_LEN_FLIP;
        else
            timeout = TIMER_LEN_COPY;
139
140

        xwl_present_window->frame_timer = TimerSet(xwl_present_window->frame_timer,
141
                                                   0, timeout,
142
143
                                                   &xwl_present_timer_callback,
                                                   xwl_present_window);
144
    } else {
145
        xwl_present_free_timer(xwl_present_window);
146
147
148
    }
}

149
150

static void
151
xwl_present_execute(present_vblank_ptr vblank, uint64_t ust, uint64_t crtc_msc);
152
153

static uint32_t
154
xwl_present_query_capabilities(present_screen_priv_ptr screen_priv)
155
{
156
    return XWL_PRESENT_CAPS;
157
158
159
}

static int
160
161
162
163
xwl_present_get_ust_msc(ScreenPtr screen,
                        WindowPtr present_window,
                        uint64_t *ust,
                        uint64_t *msc)
164
{
165
166
167
168
169
170
171
172
    struct xwl_present_window *xwl_present_window = xwl_present_window_get_priv(present_window);
    if (!xwl_present_window)
        return BadAlloc;

    *ust = xwl_present_window->ust;
    *msc = xwl_present_window->msc;

    return Success;
173
174
175
176
177
178
179
}

/*
 * When the wait fence or previous flip is completed, it's time
 * to re-try the request
 */
static void
180
xwl_present_re_execute(present_vblank_ptr vblank)
181
182
183
{
    uint64_t ust = 0, crtc_msc = 0;

184
    (void) xwl_present_get_ust_msc(vblank->screen, vblank->window, &ust, &crtc_msc);
185
    xwl_present_execute(vblank, ust, crtc_msc);
186
187
188
}

static void
189
xwl_present_flip_try_ready(struct xwl_present_window *xwl_present_window)
190
{
191
    present_vblank_ptr vblank;
192
193
194

    xorg_list_for_each_entry(vblank, &xwl_present_window->flip_queue, event_queue) {
        if (vblank->queued) {
195
            xwl_present_re_execute(vblank);
196
197
198
199
200
            return;
        }
    }
}

201
202
203
204
205
206
207
208
209
210
211
212
static void
xwl_present_release_pixmap(struct xwl_present_event *event)
{
    if (!event->pixmap)
        return;

    xwl_pixmap_del_buffer_release_cb(event->pixmap);
    dixDestroyPixmap(event->pixmap, event->pixmap->drawable.id);
    event->pixmap = NULL;
}

static void
213
xwl_present_free_event(struct xwl_present_event *event)
214
215
{
    xwl_present_release_pixmap(event);
216
    xorg_list_del(&event->vblank.event_queue);
217
218
219
    present_vblank_destroy(&event->vblank);
}

220
static void
221
xwl_present_free_idle_vblank(present_vblank_ptr vblank)
222
223
{
    present_pixmap_idle(vblank->pixmap, vblank->window, vblank->serial, vblank->idle_fence);
224
    xwl_present_free_event(xwl_present_event_from_id((uintptr_t)vblank));
225
226
227
}

static WindowPtr
228
xwl_present_toplvl_pixmap_window(WindowPtr window)
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
{
    ScreenPtr       screen = window->drawable.pScreen;
    PixmapPtr       pixmap = (*screen->GetWindowPixmap)(window);
    WindowPtr       w = window;
    WindowPtr       next_w;

    while(w->parent) {
        next_w = w->parent;
        if ( (*screen->GetWindowPixmap)(next_w) != pixmap) {
            break;
        }
        w = next_w;
    }
    return w;
}

static void
246
xwl_present_flips_stop(WindowPtr window)
247
248
{
    struct xwl_present_window *xwl_present_window = xwl_present_window_priv(window);
249
    present_vblank_ptr vblank, tmp;
250

251
252
    /* Change back to the fast refresh rate */
    xwl_present_reset_timer(xwl_present_window);
253

254
255
256
257
258
259
260
261
262
263
    /* Free any left over idle vblanks */
    xorg_list_for_each_entry_safe(vblank, tmp, &xwl_present_window->idle_queue, event_queue)
        xwl_present_free_idle_vblank(vblank);

    if (xwl_present_window->flip_active) {
        xwl_present_free_idle_vblank(xwl_present_window->flip_active);
        xwl_present_window->flip_active = NULL;
    }

    xwl_present_flip_try_ready(xwl_present_window);
264
265
266
}

static void
267
xwl_present_flip_notify_vblank(present_vblank_ptr vblank, uint64_t ust, uint64_t crtc_msc)
268
269
270
271
272
273
274
275
276
{
    WindowPtr                   window = vblank->window;
    struct xwl_present_window *xwl_present_window = xwl_present_window_priv(window);

    DebugPresent(("\tn %" PRIu64 " %p %" PRIu64 " %" PRIu64 ": %08" PRIx32 " -> %08" PRIx32 "\n",
                  vblank->event_id, vblank, vblank->exec_msc, vblank->target_msc,
                  vblank->pixmap ? vblank->pixmap->drawable.id : 0,
                  vblank->window ? vblank->window->drawable.id : 0));

277
    assert (&vblank->event_queue == xwl_present_window->flip_queue.next);
278
279
280
281

    xorg_list_del(&vblank->event_queue);

    if (xwl_present_window->flip_active) {
282
283
284
285
286
        struct xwl_present_event *event =
            xwl_present_event_from_id((uintptr_t)xwl_present_window->flip_active);

        if (!event->pixmap)
            xwl_present_free_event(event);
287
288
289
290
291
292
293
294
295
296
297
298
        else
            /* Put the previous flip in the idle_queue and wait for further notice from
             * the Wayland compositor
             */
            xorg_list_append(&xwl_present_window->flip_active->event_queue, &xwl_present_window->idle_queue);
    }

    xwl_present_window->flip_active = vblank;

    present_vblank_notify(vblank, PresentCompleteKindPixmap, PresentCompleteModeFlip, ust, crtc_msc);

    if (vblank->abort_flip)
299
        xwl_present_flips_stop(window);
300

301
    xwl_present_flip_try_ready(xwl_present_window);
302
303
304
}

static void
305
xwl_present_update_window_crtc(present_window_priv_ptr window_priv, RRCrtcPtr crtc, uint64_t new_msc)
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
{
    /* Crtc unchanged, no offset. */
    if (crtc == window_priv->crtc)
        return;

    /* No crtc earlier to offset against, just set the crtc. */
    if (window_priv->crtc == PresentCrtcNeverSet) {
        window_priv->msc_offset = 0;
        window_priv->crtc = crtc;
        return;
    }

    /* In window-mode the last correct msc-offset is always kept
     * in window-priv struct because msc is saved per window and
     * not per crtc as in screen-mode.
     */
    window_priv->msc_offset += new_msc - window_priv->msc;
    window_priv->crtc = crtc;
}


327
void
328
xwl_present_cleanup(WindowPtr window)
329
{
330
    struct xwl_present_window *xwl_present_window = xwl_present_window_priv(window);
331
    present_window_priv_ptr window_priv = present_window_priv(window);
332
    struct xwl_present_event *event, *tmp;
333

334
    if (!xwl_present_window)
335
336
        return;

337
    xorg_list_del(&xwl_present_window->frame_callback_list);
338

339
340
341
342
343
    if (xwl_present_window->sync_callback) {
        wl_callback_destroy(xwl_present_window->sync_callback);
        xwl_present_window->sync_callback = NULL;
    }

344
    /* Clear remaining events */
345
    xorg_list_for_each_entry_safe(event, tmp, &window_priv->vblank, vblank.window_list)
346
        xwl_present_free_event(event);
347
348

    /* Clear timer */
349
350
    xwl_present_free_timer(xwl_present_window);

351
352
353
354
355
    /* Remove from privates so we don't try to access it later */
    dixSetPrivate(&window->devPrivates,
                  &xwl_present_window_private_key,
                  NULL);

356
    free(xwl_present_window);
357
358
359
}

static void
360
xwl_present_buffer_release(void *data)
361
{
362
    struct xwl_present_window *xwl_present_window;
363
    struct xwl_present_event *event = data;
364
    present_vblank_ptr vblank;
365

366
367
368
    if (!event)
        return;

369
370
    vblank = &event->vblank;
    present_pixmap_idle(vblank->pixmap, vblank->window, vblank->serial, vblank->idle_fence);
371

372
373
374
    xwl_present_window = xwl_present_window_priv(vblank->window);
    if (xwl_present_window->flip_active == vblank ||
        xwl_present_get_pending_flip(xwl_present_window) == vblank)
375
        xwl_present_release_pixmap(event);
376
377
    else
        xwl_present_free_event(event);
378
379
}

380
static void
381
xwl_present_msc_bump(struct xwl_present_window *xwl_present_window)
382
{
383
    present_vblank_ptr flip_pending = xwl_present_get_pending_flip(xwl_present_window);
384
    uint64_t msc = ++xwl_present_window->msc;
385
    present_vblank_ptr vblank, tmp;
386

387
388
    xwl_present_window->ust = GetTimeInMicros();

389
    if (flip_pending && flip_pending->sync_flip)
390
        xwl_present_flip_notify_vblank(flip_pending, xwl_present_window->ust, msc);
391

392
393
    xorg_list_for_each_entry_safe(vblank, tmp, &xwl_present_window->wait_list, event_queue) {
        if (vblank->exec_msc <= msc) {
394
            DebugPresent(("\te %" PRIu64 " ust %" PRIu64 " msc %" PRIu64 "\n",
395
                          vblank->event_id, xwl_present_window->ust, msc));
396

397
            xwl_present_execute(vblank, xwl_present_window->ust, msc);
398
399
400
401
        }
    }
}

402
static CARD32
403
404
405
406
xwl_present_timer_callback(OsTimerPtr timer,
                           CARD32 time,
                           void *arg)
{
407
    struct xwl_present_window *xwl_present_window = arg;
408

409
410
411
412
    /* 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);
413

414
    xwl_present_msc_bump(xwl_present_window);
415
    xwl_present_reset_timer(xwl_present_window);
416

417
    return 0;
418
419
}

420
421
void
xwl_present_frame_callback(struct xwl_present_window *xwl_present_window)
422
{
423
    xorg_list_del(&xwl_present_window->frame_callback_list);
424

425
    xwl_present_msc_bump(xwl_present_window);
426

427
428
429
    /* we do not need the timer anymore for this frame,
     * reset it for potentially the next one
     */
430
    xwl_present_reset_timer(xwl_present_window);
431
432
433
434
435
436
437
}

static void
xwl_present_sync_callback(void *data,
               struct wl_callback *callback,
               uint32_t time)
{
438
439
    present_vblank_ptr vblank = data;
    struct xwl_present_window *xwl_present_window = xwl_present_window_get_priv(vblank->window);
440

441
442
443
    wl_callback_destroy(xwl_present_window->sync_callback);
    xwl_present_window->sync_callback = NULL;

444
    xwl_present_flip_notify_vblank(vblank, xwl_present_window->ust, xwl_present_window->msc);
445
446
447
448
449
450
451
}

static const struct wl_callback_listener xwl_present_sync_listener = {
    xwl_present_sync_callback
};

static RRCrtcPtr
452
453
xwl_present_get_crtc(present_screen_priv_ptr screen_priv,
                     WindowPtr present_window)
454
{
455
    struct xwl_present_window *xwl_present_window = xwl_present_window_get_priv(present_window);
456
457
    rrScrPrivPtr rr_private;

458
    if (xwl_present_window == NULL)
459
460
        return NULL;

461
    rr_private = rrGetScrPriv(present_window->drawable.pScreen);
462
463
464
465

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

466
    return rr_private->crtcs[0];
467
468
469
470
}

/*
 * Queue an event to report back to the Present extension when the specified
471
 * MSC has passed
472
473
 */
static int
474
475
xwl_present_queue_vblank(ScreenPtr screen,
                         WindowPtr present_window,
476
477
478
479
                         RRCrtcPtr crtc,
                         uint64_t event_id,
                         uint64_t msc)
{
480
    struct xwl_present_window *xwl_present_window = xwl_present_window_get_priv(present_window);
481
    struct xwl_window *xwl_window = xwl_window_from_window(present_window);
482
    struct xwl_present_event *event = xwl_present_event_from_id(event_id);
483

484
    event->vblank.exec_msc = msc;
485

486
487
    xorg_list_del(&event->vblank.event_queue);
    xorg_list_append(&event->vblank.event_queue, &xwl_present_window->wait_list);
488

489
490
491
492
493
494
495
496
497
    /* 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)
498
        xwl_present_reset_timer(xwl_present_window);
499
500

    return Success;
501
502
503
504
505
506
507
}

/*
 * Remove a pending vblank event so that it is not reported
 * to the extension
 */
static void
508
509
xwl_present_abort_vblank(ScreenPtr screen,
                         WindowPtr present_window,
510
511
512
513
                         RRCrtcPtr crtc,
                         uint64_t event_id,
                         uint64_t msc)
{
514
    static Bool called;
515

516
517
    if (called)
        return;
518

519
520
521
522
523
    /* xwl_present_cleanup should have cleaned up everything,
     * present_free_window_vblank shouldn't need to call this.
     */
    ErrorF("Unexpected call to %s:\n", __func__);
    xorg_backtrace();
524
525
526
527
528
}

static void
xwl_present_flush(WindowPtr window)
{
529
    glamor_block_handler(window->drawable.pScreen);
530
531
532
}

static Bool
533
534
535
536
537
538
539
540
xwl_present_check_flip(RRCrtcPtr crtc,
                       WindowPtr present_window,
                       PixmapPtr pixmap,
                       Bool sync_flip,
                       RegionPtr valid,
                       int16_t x_off,
                       int16_t y_off,
                       PresentFlipReason *reason)
541
{
542
    WindowPtr toplvl_window = xwl_present_toplvl_pixmap_window(present_window);
543
    struct xwl_window *xwl_window = xwl_window_from_window(present_window);
544
    ScreenPtr screen = pixmap->drawable.pScreen;
545

546
547
548
    if (reason)
        *reason = PRESENT_FLIP_REASON_UNKNOWN;

549
550
551
    if (!xwl_window)
        return FALSE;

552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
    if (!crtc)
        return FALSE;

    /* Source pixmap must align with window exactly */
    if (x_off || y_off)
        return FALSE;

    /* Valid area must contain window (for simplicity for now just never flip when one is set). */
    if (valid)
        return FALSE;

    /* Flip pixmap must have same dimensions as window */
    if (present_window->drawable.width != pixmap->drawable.width ||
            present_window->drawable.height != pixmap->drawable.height)
        return FALSE;

    /* Window must be same region as toplevel window */
    if ( !RegionEqual(&present_window->winSize, &toplvl_window->winSize) )
        return FALSE;

    /* Can't flip if window clipped by children */
    if (!RegionEqual(&present_window->clipList, &present_window->winSize))
        return FALSE;

576
577
578
    if (!xwl_glamor_check_flip(pixmap))
        return FALSE;

579
580
581
582
583
584
585
    /* Can't flip if the window pixmap doesn't match the xwl_window parent
     * window's, e.g. because a client redirected this window or one of its
     * parents.
     */
    if (screen->GetWindowPixmap(xwl_window->window) != screen->GetWindowPixmap(present_window))
        return FALSE;

586
587
588
589
590
591
592
593
594
595
596
    /*
     * 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;
}

597
598
599
600
601
/*
 * 'window' is being reconfigured. Check to see if it is involved
 * in flipping and clean up as necessary.
 */
static void
602
xwl_present_check_flip_window (WindowPtr window)
603
604
605
606
607
608
609
610
611
612
613
614
615
616
{
    struct xwl_present_window *xwl_present_window = xwl_present_window_priv(window);
    present_window_priv_ptr window_priv = present_window_priv(window);
    present_vblank_ptr      flip_pending;
    present_vblank_ptr      flip_active;
    present_vblank_ptr      vblank;
    PresentFlipReason       reason;

    /* If this window hasn't ever been used with Present, it can't be
     * flipping
     */
    if (!xwl_present_window || !window_priv)
        return;

617
    flip_pending = xwl_present_get_pending_flip(xwl_present_window);
618
619
620
621
622
    flip_active = xwl_present_window->flip_active;

    if (flip_pending) {
        if (!xwl_present_check_flip(flip_pending->crtc, flip_pending->window, flip_pending->pixmap,
                                    flip_pending->sync_flip, flip_pending->valid, 0, 0, NULL))
623
            flip_pending->abort_flip = TRUE;
624
625
626
    } else if (flip_active) {
        if (!xwl_present_check_flip(flip_active->crtc, flip_active->window, flip_active->pixmap,
                                    flip_active->sync_flip, flip_active->valid, 0, 0, NULL))
627
            xwl_present_flips_stop(window);
628
629
630
631
632
633
634
635
636
637
638
639
640
    }

    /* Now check any queued vblanks */
    xorg_list_for_each_entry(vblank, &window_priv->vblank, window_list) {
        if (vblank->queued && vblank->flip &&
                !xwl_present_check_flip(vblank->crtc, window, vblank->pixmap,
                                        vblank->sync_flip, vblank->valid, 0, 0, &reason)) {
            vblank->flip = FALSE;
            vblank->reason = reason;
        }
    }
}

641
642
643
644
645
646
647
648
649
/*
 * Clean up any pending or current flips for this window
 */
static void
xwl_present_clear_window_flip(WindowPtr window)
{
    /* xwl_present_cleanup cleaned up everything */
}

650
651
652
653
654
655
656
657
static Bool
xwl_present_flip(WindowPtr present_window,
                 RRCrtcPtr crtc,
                 uint64_t event_id,
                 PixmapPtr pixmap,
                 Bool sync_flip,
                 RegionPtr damage)
{
658
    struct xwl_window           *xwl_window = xwl_window_from_window(present_window);
659
    struct xwl_present_window   *xwl_present_window = xwl_present_window_priv(present_window);
660
    BoxPtr                      damage_box;
661
    struct wl_buffer            *buffer;
662
    struct xwl_present_event    *event = xwl_present_event_from_id(event_id);
663

664
665
666
    if (!xwl_window)
        return FALSE;

667
668
669
670
671
672
    buffer = xwl_glamor_pixmap_get_wl_buffer(pixmap);
    if (!buffer) {
        ErrorF("present: Error getting buffer\n");
        return FALSE;
    }

673
674
    damage_box = RegionExtents(damage);

675
    pixmap->refcnt++;
676

677
    event->pixmap = pixmap;
678

679
    xwl_pixmap_set_buffer_release_cb(pixmap, xwl_present_buffer_release, event);
680
681
682
683

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

684
685
686
687
688
689
    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);
690
691
    }

692
693
694
    /* Realign timer */
    xwl_present_reset_timer(xwl_present_window);

695
    xwl_surface_damage(xwl_window->xwl_screen, xwl_window->surface,
696
697
                       damage_box->x1 - present_window->drawable.x,
                       damage_box->y1 - present_window->drawable.y,
698
699
                       damage_box->x2 - damage_box->x1,
                       damage_box->y2 - damage_box->y1);
700
701
702

    wl_surface_commit(xwl_window->surface);

703
704
705
706
707
    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,
708
                                 &event->vblank);
709
    }
710
711

    wl_display_flush(xwl_window->xwl_screen->display);
712
    xwl_window->present_flipped = TRUE;
713
714
715
    return TRUE;
}

716
717
718
719
720
721
722
723
724
725
/*
 * Once the required MSC has been reached, execute the pending request.
 *
 * For requests to actually present something, either blt contents to
 * the window pixmap or queue a window buffer swap on the backend.
 *
 * For requests to just get the current MSC/UST combo, skip that part and
 * go straight to event delivery.
 */
static void
726
xwl_present_execute(present_vblank_ptr vblank, uint64_t ust, uint64_t crtc_msc)
727
728
729
{
    WindowPtr               window = vblank->window;
    struct xwl_present_window *xwl_present_window = xwl_present_window_get_priv(window);
730
    present_vblank_ptr flip_pending = xwl_present_get_pending_flip(xwl_present_window);
731

732
733
    xorg_list_del(&vblank->event_queue);

734
735
736
    if (present_execute_wait(vblank, crtc_msc))
        return;

737
738
739
740
741
742
    if (flip_pending && vblank->flip && vblank->pixmap && vblank->window) {
        DebugPresent(("\tr %" PRIu64 " %p (pending %p)\n",
                      vblank->event_id, vblank, flip_pending));
        xorg_list_append(&vblank->event_queue, &xwl_present_window->flip_queue);
        vblank->flip_ready = TRUE;
        return;
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
    }

    vblank->queued = FALSE;

    if (vblank->pixmap && vblank->window) {
        ScreenPtr screen = window->drawable.pScreen;

        if (vblank->flip) {
            RegionPtr damage;

            DebugPresent(("\tf %" PRIu64 " %p %" PRIu64 ": %08" PRIx32 " -> %08" PRIx32 "\n",
                          vblank->event_id, vblank, crtc_msc,
                          vblank->pixmap->drawable.id, vblank->window->drawable.id));

            /* Set update region as damaged */
            if (vblank->update) {
                damage = RegionDuplicate(vblank->update);
                /* Translate update region to screen space */
                assert(vblank->x_off == 0 && vblank->y_off == 0);
                RegionTranslate(damage, window->drawable.x, window->drawable.y);
                RegionIntersect(damage, damage, &window->clipList);
            } else
                damage = RegionDuplicate(&window->clipList);

            if (xwl_present_flip(vblank->window, vblank->crtc, vblank->event_id,
768
                                 vblank->pixmap, vblank->sync_flip, damage)) {
769
                WindowPtr toplvl_window = xwl_present_toplvl_pixmap_window(vblank->window);
770
771
772
773
774
775
776
777
778
779
780
781
782
783
                PixmapPtr old_pixmap = screen->GetWindowPixmap(window);

                /* Replace window pixmap with flip pixmap */
#ifdef COMPOSITE
                vblank->pixmap->screen_x = old_pixmap->screen_x;
                vblank->pixmap->screen_y = old_pixmap->screen_y;
#endif
                present_set_tree_pixmap(toplvl_window, old_pixmap, vblank->pixmap);
                vblank->pixmap->refcnt++;
                dixDestroyPixmap(old_pixmap, old_pixmap->drawable.id);

                /* Report damage */
                DamageDamageRegion(&vblank->window->drawable, damage);
                RegionDestroy(damage);
784
785
786

                /* Put pending flip at the flip queue head */
                xorg_list_add(&vblank->event_queue, &xwl_present_window->flip_queue);
787
788
789
790
791
792
793
794
                return;
            }

            vblank->flip = FALSE;
        }
        DebugPresent(("\tc %p %" PRIu64 ": %08" PRIx32 " -> %08" PRIx32 "\n",
                      vblank, crtc_msc, vblank->pixmap->drawable.id, vblank->window->drawable.id));

795
796
        if (flip_pending)
            flip_pending->abort_flip = TRUE;
797
798
        else if (xwl_present_window->flip_active)
            xwl_present_flips_stop(window);
799
800
801
802

        present_execute_copy(vblank, crtc_msc);
        assert(!vblank->queued);

803
804
        if (xwl_present_queue_vblank(screen, window, vblank->crtc,
                                     vblank->event_id, crtc_msc + 1)
805
            == Success) {
806
807
808
            /* Clear the pixmap field, so this will fall through to present_execute_post next time */
            dixDestroyPixmap(vblank->pixmap, vblank->pixmap->drawable.id);
            vblank->pixmap = NULL;
809
810
811
812
813
814
815
            return;
        }
    }

    present_execute_post(vblank, ust, crtc_msc);
}

816
static int
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
xwl_present_pixmap(WindowPtr window,
                   PixmapPtr pixmap,
                   CARD32 serial,
                   RegionPtr valid,
                   RegionPtr update,
                   int16_t x_off,
                   int16_t y_off,
                   RRCrtcPtr target_crtc,
                   SyncFence *wait_fence,
                   SyncFence *idle_fence,
                   uint32_t options,
                   uint64_t target_window_msc,
                   uint64_t divisor,
                   uint64_t remainder,
                   present_notify_ptr notifies,
                   int num_notifies)
833
834
835
836
837
838
839
840
841
{
    uint64_t                    ust = 0;
    uint64_t                    target_msc;
    uint64_t                    crtc_msc = 0;
    int                         ret;
    present_vblank_ptr          vblank, tmp;
    ScreenPtr                   screen = window->drawable.pScreen;
    present_window_priv_ptr     window_priv = present_get_window_priv(window, TRUE);
    present_screen_priv_ptr     screen_priv = present_screen_priv(screen);
842
    struct xwl_present_event *event;
843
844
845
846
847
848

    if (!window_priv)
        return BadAlloc;

    target_crtc = xwl_present_get_crtc(screen_priv, window);

849
    ret = xwl_present_get_ust_msc(screen, window, &ust, &crtc_msc);
850

851
    xwl_present_update_window_crtc(window_priv, target_crtc, crtc_msc);
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882

    if (ret == Success) {
        /* Stash the current MSC away in case we need it later
         */
        window_priv->msc = crtc_msc;
    }

    target_msc = present_get_target_msc(target_window_msc + window_priv->msc_offset,
                                        crtc_msc,
                                        divisor,
                                        remainder,
                                        options);

    /*
     * Look for a matching presentation already on the list...
     */

    if (!update && pixmap) {
        xorg_list_for_each_entry_safe(vblank, tmp, &window_priv->vblank, window_list) {

            if (!vblank->pixmap)
                continue;

            if (!vblank->queued)
                continue;

            if (vblank->target_msc != target_msc)
                continue;

            present_vblank_scrap(vblank);
            if (vblank->flip_ready)
883
                xwl_present_re_execute(vblank);
884
885
886
        }
    }

887
888
    event = calloc(1, sizeof(*event));
    if (!event)
889
890
        return BadAlloc;

891
892
893
894
895
896
897
898
899
900
    vblank = &event->vblank;
    if (!present_vblank_init(vblank, window, pixmap, serial, valid, update, x_off, y_off,
                             target_crtc, wait_fence, idle_fence, options, XWL_PRESENT_CAPS,
                             notifies, num_notifies, target_msc, crtc_msc)) {
        present_vblank_destroy(vblank);
        return BadAlloc;
    }

    vblank->event_id = (uintptr_t)event;

901
    /* Xwayland presentations always complete (at least) one frame after they
902
903
904
905
906
907
     * are executed
     */
    vblank->exec_msc = vblank->target_msc - 1;

    vblank->queued = TRUE;
    if (crtc_msc < vblank->exec_msc) {
908
        if (xwl_present_queue_vblank(screen, window, target_crtc, vblank->event_id, vblank->exec_msc) == Success)
909
            return Success;
910

911
912
913
        DebugPresent(("present_queue_vblank failed\n"));
    }

914
    xwl_present_execute(vblank, ust, crtc_msc);
915
916
917
    return Success;
}

918
void
919
xwl_present_unrealize_window(struct xwl_present_window *xwl_present_window)
920
921
922
923
{
    /* The pending frame callback may never be called, so drop it and shorten
     * the frame timer interval.
     */
924
    xorg_list_del(&xwl_present_window->frame_callback_list);
925
926
927
    xwl_present_reset_timer(xwl_present_window);
}

928
929
930
Bool
xwl_present_init(ScreenPtr screen)
{
931
    struct xwl_screen *xwl_screen = xwl_screen_get(screen);
932
    present_screen_priv_ptr screen_priv;
933

934
    if (!xwl_screen->glamor || !xwl_screen->egl_backend)
935
936
        return FALSE;

937
938
939
940
941
942
943
944
945
946
    if (!present_screen_register_priv_keys())
        return FALSE;

    if (present_screen_priv(screen))
        return TRUE;

    screen_priv = present_screen_priv_init(screen);
    if (!screen_priv)
        return FALSE;

947
948
949
    if (!dixRegisterPrivateKey(&xwl_present_window_private_key, PRIVATE_WINDOW, 0))
        return FALSE;

950
    screen_priv->query_capabilities = xwl_present_query_capabilities;
951
    screen_priv->get_crtc = xwl_present_get_crtc;
952

953
    screen_priv->check_flip = xwl_present_check_flip;
954
955
    screen_priv->check_flip_window = xwl_present_check_flip_window;
    screen_priv->clear_window_flip = xwl_present_clear_window_flip;
956

957
    screen_priv->present_pixmap = xwl_present_pixmap;
958
    screen_priv->queue_vblank = xwl_present_queue_vblank;
959
    screen_priv->flush = xwl_present_flush;
960
    screen_priv->re_execute = xwl_present_re_execute;
961

962
    screen_priv->abort_vblank = xwl_present_abort_vblank;
963
964

    return TRUE;
965
}