present_fake.c revision 35c4bbdf
1/*
2 * Copyright © 2013 Keith Packard
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission.  The copyright holders make no representations
11 * about the suitability of this software for any purpose.  It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
23#ifdef HAVE_XORG_CONFIG_H
24#include <xorg-config.h>
25#endif
26
27#include "present_priv.h"
28#include "list.h"
29
30static struct xorg_list fake_vblank_queue;
31
32typedef struct present_fake_vblank {
33    struct xorg_list            list;
34    uint64_t                    event_id;
35    OsTimerPtr                  timer;
36    ScreenPtr                   screen;
37} present_fake_vblank_rec, *present_fake_vblank_ptr;
38
39int
40present_fake_get_ust_msc(ScreenPtr screen, uint64_t *ust, uint64_t *msc)
41{
42    present_screen_priv_ptr screen_priv = present_screen_priv(screen);
43
44    *ust = GetTimeInMicros();
45    *msc = (*ust + screen_priv->fake_interval / 2) / screen_priv->fake_interval;
46    return Success;
47}
48
49static void
50present_fake_notify(ScreenPtr screen, uint64_t event_id)
51{
52    uint64_t                    ust, msc;
53
54    present_fake_get_ust_msc(screen, &ust, &msc);
55    present_event_notify(event_id, ust, msc);
56}
57
58static CARD32
59present_fake_do_timer(OsTimerPtr timer,
60                      CARD32 time,
61                      void *arg)
62{
63    present_fake_vblank_ptr     fake_vblank = arg;
64
65    present_fake_notify(fake_vblank->screen, fake_vblank->event_id);
66    xorg_list_del(&fake_vblank->list);
67    free(fake_vblank);
68    return 0;
69}
70
71void
72present_fake_abort_vblank(ScreenPtr screen, uint64_t event_id, uint64_t msc)
73{
74    present_fake_vblank_ptr     fake_vblank, tmp;
75
76    xorg_list_for_each_entry_safe(fake_vblank, tmp, &fake_vblank_queue, list) {
77        if (fake_vblank->event_id == event_id) {
78            TimerCancel(fake_vblank->timer);
79            xorg_list_del(&fake_vblank->list);
80            free (fake_vblank);
81            break;
82        }
83    }
84}
85
86int
87present_fake_queue_vblank(ScreenPtr     screen,
88                          uint64_t      event_id,
89                          uint64_t      msc)
90{
91    present_screen_priv_ptr     screen_priv = present_screen_priv(screen);
92    uint64_t                    ust = msc * screen_priv->fake_interval;
93    uint64_t                    now = GetTimeInMicros();
94    INT32                       delay = ((int64_t) (ust - now)) / 1000;
95    present_fake_vblank_ptr     fake_vblank;
96
97    if (delay <= 0) {
98        present_fake_notify(screen, event_id);
99        return Success;
100    }
101
102    fake_vblank = calloc (1, sizeof (present_fake_vblank_rec));
103    if (!fake_vblank)
104        return BadAlloc;
105
106    fake_vblank->screen = screen;
107    fake_vblank->event_id = event_id;
108    fake_vblank->timer = TimerSet(NULL, 0, delay, present_fake_do_timer, fake_vblank);
109    if (!fake_vblank->timer) {
110        free(fake_vblank);
111        return BadAlloc;
112    }
113
114    xorg_list_add(&fake_vblank->list, &fake_vblank_queue);
115
116    return Success;
117}
118
119void
120present_fake_screen_init(ScreenPtr screen)
121{
122    present_screen_priv_ptr screen_priv = present_screen_priv(screen);
123
124    /* For screens with hardware vblank support, the fake code
125     * will be used for off-screen windows and while screens are blanked,
126     * in which case we want a slow interval here
127     *
128     * Otherwise, pretend that the screen runs at 60Hz
129     */
130    if (screen_priv->info && screen_priv->info->get_crtc)
131        screen_priv->fake_interval = 1000000;
132    else
133        screen_priv->fake_interval = 16667;
134}
135
136void
137present_fake_queue_init(void)
138{
139    xorg_list_init(&fake_vblank_queue);
140}
141