present_wnmd.c 24 KB
Newer Older
Roman Gilg's avatar
Roman Gilg committed
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
/*
 * 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.
 */

#ifdef HAVE_XORG_CONFIG_H
#include <xorg-config.h>
#endif

#include "present_priv.h"

/*
 * Window flip mode
 *
 * Provides per-window flips. Flips can be processed on windows that
 * have the same size as their parents, which they share their pixmap with.
 *
 * A flip still requires a copy currently, since the original pixmap needs
 * to be updated with the new pixmap content. Just a flip of all windows
Alan Coopersmith's avatar
Alan Coopersmith committed
37
 * to the new pixmap is difficult, because the original pixmap might not be
Roman Gilg's avatar
Roman Gilg committed
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
 * controlled by the Xserver.
 *
 */

static void
present_wnmd_execute(present_vblank_ptr vblank, uint64_t ust, uint64_t crtc_msc);

static void
present_wnmd_create_event_id(present_window_priv_ptr window_priv, present_vblank_ptr vblank)
{
    vblank->event_id = ++window_priv->event_id;
}

static uint32_t
present_wnmd_query_capabilities(present_screen_priv_ptr screen_priv)
{
    return screen_priv->wnmd_info->capabilities;
}

static RRCrtcPtr
present_wnmd_get_crtc(present_screen_priv_ptr screen_priv, WindowPtr window)
{
    return (*screen_priv->wnmd_info->get_crtc)(window);
}

static int
present_wnmd_get_ust_msc(ScreenPtr screen, WindowPtr window, uint64_t *ust, uint64_t *msc)
{
    present_screen_priv_ptr screen_priv = present_screen_priv(screen);
    return (*screen_priv->wnmd_info->get_ust_msc)(window, ust, msc);
}

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

    (void) present_wnmd_get_ust_msc(vblank->screen, vblank->window, &ust, &crtc_msc);
    present_wnmd_execute(vblank, ust, crtc_msc);
}

static void
present_wnmd_flip_try_ready(WindowPtr window)
{
    present_window_priv_ptr window_priv = present_window_priv(window);
    present_vblank_ptr      vblank;

    xorg_list_for_each_entry(vblank, &window_priv->flip_queue, event_queue) {
        if (vblank->queued) {
            present_wnmd_re_execute(vblank);
            return;
        }
    }
}

static void
present_wnmd_free_idle_vblank(present_vblank_ptr vblank)
{
    present_pixmap_idle(vblank->pixmap, vblank->window, vblank->serial, vblank->idle_fence);
    present_vblank_destroy(vblank);
}

/*
 * Free any left over idle vblanks
 */
static void
present_wnmd_free_idle_vblanks(WindowPtr window)
{
    present_window_priv_ptr         window_priv = present_window_priv(window);
    present_vblank_ptr              vblank, tmp;

    xorg_list_for_each_entry_safe(vblank, tmp, &window_priv->idle_queue, event_queue) {
        present_wnmd_free_idle_vblank(vblank);
    }

    if (window_priv->flip_active) {
        present_wnmd_free_idle_vblank(window_priv->flip_active);
        window_priv->flip_active = NULL;
    }
}

static WindowPtr
present_wnmd_toplvl_pixmap_window(WindowPtr window)
{
    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
present_wnmd_flips_stop(WindowPtr window)
{
    present_window_priv_ptr window_priv = present_window_priv(window);
    present_screen_priv_ptr screen_priv = present_screen_priv(window->drawable.pScreen);

    assert (!window_priv->flip_pending);

    (*screen_priv->wnmd_info->flips_stop) (window);

    present_wnmd_free_idle_vblanks(window_priv->window);
    present_wnmd_flip_try_ready(window_priv->window);
}

static void
156
present_wnmd_flip_notify_vblank(present_vblank_ptr vblank, uint64_t ust, uint64_t crtc_msc)
Roman Gilg's avatar
Roman Gilg committed
157
158
159
160
{
    WindowPtr                   window = vblank->window;
    present_window_priv_ptr     window_priv = present_window_priv(window);

161
    DebugPresent(("\tn %" PRIu64 " %p %" PRIu64 ": %08" PRIx32 " -> %08" PRIx32 "\n",
Roman Gilg's avatar
Roman Gilg committed
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
                  vblank->event_id, vblank, vblank->target_msc,
                  vblank->pixmap ? vblank->pixmap->drawable.id : 0,
                  vblank->window ? vblank->window->drawable.id : 0));

    assert (vblank == window_priv->flip_pending);

    xorg_list_del(&vblank->event_queue);

    if (window_priv->flip_active) {
        if (window_priv->flip_active->flip_idler)
            present_wnmd_free_idle_vblank(window_priv->flip_active);
        else
            /* Put the previous flip in the idle_queue and wait for further notice from DDX */
            xorg_list_append(&window_priv->flip_active->event_queue, &window_priv->idle_queue);
    }

    window_priv->flip_active = vblank;
    window_priv->flip_pending = NULL;

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

Roman Gilg's avatar
Roman Gilg committed
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
    if (vblank->abort_flip)
        present_wnmd_flips_stop(window);

    present_wnmd_flip_try_ready(window);
}

void
present_wnmd_event_notify(WindowPtr window, uint64_t event_id, uint64_t ust, uint64_t msc)
{
    present_window_priv_ptr     window_priv = present_window_priv(window);
    present_vblank_ptr          vblank;

    if (!window_priv)
        return;
    if (!event_id)
        return;

200
    DebugPresent(("\te %" PRIu64 " ust %" PRIu64 " msc %" PRIu64 "\n", event_id, ust, msc));
Roman Gilg's avatar
Roman Gilg committed
201
202
203
204
205
206
207
208
    xorg_list_for_each_entry(vblank, &window_priv->exec_queue, event_queue) {
        if (event_id == vblank->event_id) {
            present_wnmd_execute(vblank, ust, msc);
            return;
        }
    }
    xorg_list_for_each_entry(vblank, &window_priv->flip_queue, event_queue) {
        if (vblank->event_id == event_id) {
209
210
            assert(vblank->queued);
            present_wnmd_execute(vblank, ust, msc);
Roman Gilg's avatar
Roman Gilg committed
211
212
213
            return;
        }
    }
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
}

void
present_wnmd_flip_notify(WindowPtr window, uint64_t event_id, uint64_t ust, uint64_t msc)
{
    present_window_priv_ptr     window_priv = present_window_priv(window);
    present_vblank_ptr          vblank;

    xorg_list_for_each_entry(vblank, &window_priv->flip_queue, event_queue) {
        if (vblank->event_id == event_id) {
            assert(!vblank->queued);
            assert(vblank->window);
            present_wnmd_flip_notify_vblank(vblank, ust, msc);
            return;
        }
    }
}

void
present_wnmd_idle_notify(WindowPtr window, uint64_t event_id)
{
    present_window_priv_ptr     window_priv = present_window_priv(window);
    present_vblank_ptr          vblank;

    if (window_priv->flip_active && window_priv->flip_active->event_id == event_id) {
        /* Active flip is allowed to become idle directly when it becomes unactive again. */
        window_priv->flip_active->flip_idler = TRUE;
        return;
    }
Roman Gilg's avatar
Roman Gilg committed
243
244
245
246
247
248
249

    xorg_list_for_each_entry(vblank, &window_priv->idle_queue, event_queue) {
        if (vblank->event_id == event_id) {
            present_wnmd_free_idle_vblank(vblank);
            return;
        }
    }
250
251
252
253
254
255
256

    xorg_list_for_each_entry(vblank, &window_priv->flip_queue, event_queue) {
        if (vblank->event_id == event_id) {
            vblank->flip_idler = TRUE;
            return;
        }
    }
Roman Gilg's avatar
Roman Gilg committed
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
}

static Bool
present_wnmd_check_flip(RRCrtcPtr           crtc,
                        WindowPtr           window,
                        PixmapPtr           pixmap,
                        Bool                sync_flip,
                        RegionPtr           valid,
                        int16_t             x_off,
                        int16_t             y_off,
                        PresentFlipReason   *reason)
{
    ScreenPtr               screen = window->drawable.pScreen;
    present_screen_priv_ptr screen_priv = present_screen_priv(screen);
    WindowPtr               toplvl_window = present_wnmd_toplvl_pixmap_window(window);

    if (reason)
        *reason = PRESENT_FLIP_REASON_UNKNOWN;

    if (!screen_priv)
        return FALSE;

    if (!screen_priv->wnmd_info)
        return FALSE;

    if (!crtc)
        return FALSE;

    /* Check to see if the driver supports flips at all */
    if (!screen_priv->wnmd_info->flip)
        return FALSE;

289
290
    /* Can't flip redirected child windows */
    if (screen->GetWindowPixmap(window) != screen->GetWindowPixmap(toplvl_window))
Roman Gilg's avatar
Roman Gilg committed
291
292
293
294
295
296
        return FALSE;

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

297
298
299
    /* Valid area must contain window (for simplicity for now just never flip when one is set). */
    if (valid)
        return FALSE;
Roman Gilg's avatar
Roman Gilg committed
300
301
302
303
304
305
306
307
308
309
310
311
312

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

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

    /* Ask the driver for permission */
    if (screen_priv->wnmd_info->check_flip2) {
        if (!(*screen_priv->wnmd_info->check_flip2) (crtc, window, pixmap, sync_flip, reason)) {
313
314
            DebugPresent(("\td %08" PRIx32 " -> %08" PRIx32 "\n",
                          window->drawable.id, pixmap ? pixmap->drawable.id : 0));
Roman Gilg's avatar
Roman Gilg committed
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
            return FALSE;
        }
    }

    return TRUE;
}

/*
 * 'window' is being reconfigured. Check to see if it is involved
 * in flipping and clean up as necessary.
 */
static void
present_wnmd_check_flip_window (WindowPtr 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 (!window_priv)
        return;

    flip_pending = window_priv->flip_pending;
    flip_active = window_priv->flip_active;

    if (flip_pending) {
        if (!present_wnmd_check_flip(flip_pending->crtc, flip_pending->window, flip_pending->pixmap,
346
                                flip_pending->sync_flip, flip_pending->valid, 0, 0, NULL))
347
            window_priv->flip_pending->abort_flip = TRUE;
Roman Gilg's avatar
Roman Gilg committed
348
349
    } else if (flip_active) {
        if (!present_wnmd_check_flip(flip_active->crtc, flip_active->window, flip_active->pixmap,
350
                                     flip_active->sync_flip, flip_active->valid, 0, 0, NULL))
Roman Gilg's avatar
Roman Gilg committed
351
352
353
354
355
356
357
            present_wnmd_flips_stop(window);
    }

    /* Now check any queued vblanks */
    xorg_list_for_each_entry(vblank, &window_priv->vblank, window_list) {
        if (vblank->queued && vblank->flip &&
                !present_wnmd_check_flip(vblank->crtc, window, vblank->pixmap,
358
                                         vblank->sync_flip, vblank->valid, 0, 0, &reason)) {
Roman Gilg's avatar
Roman Gilg committed
359
360
361
362
363
364
365
366
367
368
369
370
371
372
            vblank->flip = FALSE;
            vblank->reason = reason;
            if (vblank->sync_flip)
                vblank->requeue = TRUE;
        }
    }
}

static Bool
present_wnmd_flip(WindowPtr window,
                  RRCrtcPtr crtc,
                  uint64_t event_id,
                  uint64_t target_msc,
                  PixmapPtr pixmap,
373
374
                  Bool sync_flip,
                  RegionPtr damage)
Roman Gilg's avatar
Roman Gilg committed
375
{
376
    ScreenPtr                   screen = window->drawable.pScreen;
Roman Gilg's avatar
Roman Gilg committed
377
378
379
380
381
382
383
    present_screen_priv_ptr     screen_priv = present_screen_priv(screen);

    return (*screen_priv->wnmd_info->flip) (window,
                                            crtc,
                                            event_id,
                                            target_msc,
                                            pixmap,
384
385
                                            sync_flip,
                                            damage);
Roman Gilg's avatar
Roman Gilg committed
386
387
388
389
390
391
392
393
}

static void
present_wnmd_cancel_flip(WindowPtr window)
{
    present_window_priv_ptr window_priv = present_window_priv(window);

    if (window_priv->flip_pending)
394
        window_priv->flip_pending->abort_flip = TRUE;
Roman Gilg's avatar
Roman Gilg committed
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
    else if (window_priv->flip_active)
        present_wnmd_flips_stop(window);
}

static Bool
present_wnmd_can_window_flip(WindowPtr window)
{
    ScreenPtr                   screen = window->drawable.pScreen;
    present_screen_priv_ptr     screen_priv = present_screen_priv(screen);
    WindowPtr                   toplvl_window = present_wnmd_toplvl_pixmap_window(window);

    if (!screen_priv)
        return FALSE;

    if (!screen_priv->wnmd_info)
        return FALSE;

    /* Check to see if the driver supports flips at all */
    if (!screen_priv->wnmd_info->flip)
        return FALSE;

    /* Don't flip redirected windows */
    if (window->redirectDraw != RedirectDrawNone)
        return FALSE;

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

    return TRUE;
}

/*
 * 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
present_wnmd_execute(present_vblank_ptr vblank, uint64_t ust, uint64_t crtc_msc)
{
    WindowPtr               window = vblank->window;
    present_window_priv_ptr window_priv = present_window_priv(window);

    if (present_execute_wait(vblank, crtc_msc))
        return;

    if (vblank->flip && vblank->pixmap && vblank->window) {
        if (window_priv->flip_pending) {
447
            DebugPresent(("\tr %" PRIu64 " %p (pending %p)\n",
Roman Gilg's avatar
Roman Gilg committed
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
                          vblank->event_id, vblank,
                          window_priv->flip_pending));
            xorg_list_del(&vblank->event_queue);
            xorg_list_append(&vblank->event_queue, &window_priv->flip_queue);
            vblank->flip_ready = TRUE;
            return;
        }
    }

    xorg_list_del(&vblank->event_queue);
    xorg_list_del(&vblank->window_list);
    vblank->queued = FALSE;

    if (vblank->pixmap && vblank->window) {

        if (vblank->flip) {
            RegionPtr damage;

466
            DebugPresent(("\tf %" PRIu64 " %p %" PRIu64 ": %08" PRIx32 " -> %08" PRIx32 "\n",
Roman Gilg's avatar
Roman Gilg committed
467
468
469
470
471
472
473
                          vblank->event_id, vblank, crtc_msc,
                          vblank->pixmap->drawable.id, vblank->window->drawable.id));

            /* Prepare to flip by placing it in the flip queue
             */
            xorg_list_add(&vblank->event_queue, &window_priv->flip_queue);

474
475
476
477
478
479
480
            /* Set update region as damaged */
            if (vblank->update) {
                damage = vblank->update;
                RegionIntersect(damage, damage, &window->clipList);
            } else
                damage = &window->clipList;

Roman Gilg's avatar
Roman Gilg committed
481
482
483
484
485
            /* Try to flip - the vblank is now pending
             */
            window_priv->flip_pending = vblank;
            // ask the driver
            if (present_wnmd_flip(vblank->window, vblank->crtc, vblank->event_id,
486
                                     vblank->target_msc, vblank->pixmap, vblank->sync_flip, damage)) {
Roman Gilg's avatar
Roman Gilg committed
487
488
489
490
491
                ScreenPtr screen = window->drawable.pScreen;
                WindowPtr toplvl_window = present_wnmd_toplvl_pixmap_window(vblank->window);
                PixmapPtr old_pixmap = screen->GetWindowPixmap(window);

                /* Replace window pixmap with flip pixmap */
492
493
494
495
#ifdef COMPOSITE
                vblank->pixmap->screen_x = old_pixmap->screen_x;
                vblank->pixmap->screen_y = old_pixmap->screen_y;
#endif
Roman Gilg's avatar
Roman Gilg committed
496
497
498
499
                present_set_tree_pixmap(toplvl_window, old_pixmap, vblank->pixmap);
                vblank->pixmap->refcnt++;
                dixDestroyPixmap(old_pixmap, old_pixmap->drawable.id);

500
                /* Report damage */
Roman Gilg's avatar
Roman Gilg committed
501
502
503
504
505
506
507
508
509
510
                DamageDamageRegion(&vblank->window->drawable, damage);
                return;
            }

            xorg_list_del(&vblank->event_queue);
            /* Flip failed. Clear the flip_pending field
              */
            window_priv->flip_pending = NULL;
            vblank->flip = FALSE;
        }
511
512
        DebugPresent(("\tc %p %" PRIu64 ": %08" PRIx32 " -> %08" PRIx32 "\n",
                      vblank, crtc_msc, vblank->pixmap->drawable.id, vblank->window->drawable.id));
Roman Gilg's avatar
Roman Gilg committed
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539

        present_wnmd_cancel_flip(window);

        present_execute_copy(vblank, crtc_msc);

        if (vblank->queued) {
            xorg_list_add(&vblank->event_queue, &window_priv->exec_queue);
            xorg_list_append(&vblank->window_list, &window_priv->vblank);

            return;
        }
    }

    present_execute_post(vblank, ust, crtc_msc);
}

static int
present_wnmd_queue_vblank(ScreenPtr screen,
                             WindowPtr window,
                             RRCrtcPtr crtc,
                             uint64_t event_id,
                             uint64_t msc)
{
    present_screen_priv_ptr screen_priv = present_screen_priv(screen);
    return (*screen_priv->wnmd_info->queue_vblank) (window, crtc, event_id, msc);
}

540
541
static void
present_wnmd_update_window_crtc(WindowPtr window, RRCrtcPtr crtc, uint64_t new_msc)
Roman Gilg's avatar
Roman Gilg committed
542
543
544
{
    present_window_priv_ptr window_priv = present_get_window_priv(window, TRUE);

545
546
547
    /* Crtc unchanged, no offset. */
    if (crtc == window_priv->crtc)
        return;
Roman Gilg's avatar
Roman Gilg committed
548

549
550
551
    /* No crtc earlier to offset against, just set the crtc. */
    if (window_priv->crtc == PresentCrtcNeverSet) {
        window_priv->msc_offset = 0;
Roman Gilg's avatar
Roman Gilg committed
552
        window_priv->crtc = crtc;
553
        return;
Roman Gilg's avatar
Roman Gilg committed
554
555
    }

556
557
558
559
560
561
    /* 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;
Roman Gilg's avatar
Roman Gilg committed
562
563
564
565
566
567
568
569
570
571
572
573
574
575
}

static int
present_wnmd_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,
576
                    uint64_t target_window_msc,
Roman Gilg's avatar
Roman Gilg committed
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
                    uint64_t divisor,
                    uint64_t remainder,
                    present_notify_ptr notifies,
                    int num_notifies)
{
    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);

    if (!window_priv)
        return BadAlloc;

    target_crtc = present_wnmd_get_crtc(screen_priv, window);

    ret = present_wnmd_get_ust_msc(screen, window, &ust, &crtc_msc);

598
    present_wnmd_update_window_crtc(window, target_crtc, crtc_msc);
Roman Gilg's avatar
Roman Gilg committed
599
600
601
602
603
604
605

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

606
    target_msc = present_get_target_msc(target_window_msc + window_priv->msc_offset,
607
608
                                        crtc_msc,
                                        divisor,
609
610
                                        remainder,
                                        options);
Roman Gilg's avatar
Roman Gilg committed
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724

    /*
     * 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)
                present_wnmd_re_execute(vblank);
        }
    }

    vblank = present_vblank_create(window,
                                   pixmap,
                                   serial,
                                   valid,
                                   update,
                                   x_off,
                                   y_off,
                                   target_crtc,
                                   wait_fence,
                                   idle_fence,
                                   options,
                                   &screen_priv->wnmd_info->capabilities,
                                   notifies,
                                   num_notifies,
                                   &target_msc,
                                   crtc_msc);
    if (!vblank)
        return BadAlloc;

    xorg_list_append(&vblank->event_queue, &window_priv->exec_queue);
    vblank->queued = TRUE;
    if (crtc_msc < target_msc) {
        if (present_wnmd_queue_vblank(screen, window, target_crtc, vblank->event_id, target_msc) == Success) {
            return Success;
        }
        DebugPresent(("present_queue_vblank failed\n"));
    }

    present_wnmd_execute(vblank, ust, crtc_msc);
    return Success;
}

static void
present_wnmd_abort_vblank(ScreenPtr screen, WindowPtr window, RRCrtcPtr crtc, uint64_t event_id, uint64_t msc)
{
    present_screen_priv_ptr screen_priv = present_screen_priv(screen);
    present_window_priv_ptr window_priv = present_window_priv(window);
    present_vblank_ptr      vblank;

    (*screen_priv->wnmd_info->abort_vblank) (window, crtc, event_id, msc);

    xorg_list_for_each_entry(vblank, &window_priv->exec_queue, event_queue) {
        if (vblank->event_id == event_id) {
            xorg_list_del(&vblank->event_queue);
            vblank->queued = FALSE;
            return;
        }
    }
    xorg_list_for_each_entry(vblank, &window_priv->flip_queue, event_queue) {
        if (vblank->event_id == event_id) {
            xorg_list_del(&vblank->event_queue);
            vblank->queued = FALSE;
            return;
        }
    }
}

static void
present_wnmd_flip_destroy(ScreenPtr screen)
{
    /* Cleanup done on window destruction */
}

static void
present_wnmd_flush(WindowPtr window)
{
    ScreenPtr               screen = window->drawable.pScreen;
    present_screen_priv_ptr screen_priv = present_screen_priv(screen);

    (*screen_priv->wnmd_info->flush) (window);
}

void
present_wnmd_init_mode_hooks(present_screen_priv_ptr screen_priv)
{
    screen_priv->query_capabilities =   &present_wnmd_query_capabilities;
    screen_priv->get_crtc           =   &present_wnmd_get_crtc;

    screen_priv->check_flip         =   &present_wnmd_check_flip;
    screen_priv->check_flip_window  =   &present_wnmd_check_flip_window;
    screen_priv->can_window_flip    =   &present_wnmd_can_window_flip;

    screen_priv->present_pixmap     =   &present_wnmd_pixmap;
    screen_priv->create_event_id    =   &present_wnmd_create_event_id;
    screen_priv->queue_vblank       =   &present_wnmd_queue_vblank;
    screen_priv->flush              =   &present_wnmd_flush;
    screen_priv->re_execute         =   &present_wnmd_re_execute;

    screen_priv->abort_vblank       =   &present_wnmd_abort_vblank;
    screen_priv->flip_destroy       =   &present_wnmd_flip_destroy;
}