present_fake.c revision 1b5d61b8
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    TimerFree(fake_vblank->timer);
68    free(fake_vblank);
69    return 0;
70}
71
72void
73present_fake_abort_vblank(ScreenPtr screen, uint64_t event_id, uint64_t msc)
74{
75    present_fake_vblank_ptr     fake_vblank, tmp;
76
77    xorg_list_for_each_entry_safe(fake_vblank, tmp, &fake_vblank_queue, list) {
78        if (fake_vblank->event_id == event_id) {
79            TimerFree(fake_vblank->timer); /* TimerFree will call TimerCancel() */
80            xorg_list_del(&fake_vblank->list);
81            free (fake_vblank);
82            break;
83        }
84    }
85}
86
87int
88present_fake_queue_vblank(ScreenPtr     screen,
89                          uint64_t      event_id,
90                          uint64_t      msc)
91{
92    present_screen_priv_ptr     screen_priv = present_screen_priv(screen);
93    uint64_t                    ust = msc * screen_priv->fake_interval;
94    uint64_t                    now = GetTimeInMicros();
95    INT32                       delay = ((int64_t) (ust - now)) / 1000;
96    present_fake_vblank_ptr     fake_vblank;
97
98    if (delay <= 0) {
99        present_fake_notify(screen, event_id);
100        return Success;
101    }
102
103    fake_vblank = calloc (1, sizeof (present_fake_vblank_rec));
104    if (!fake_vblank)
105        return BadAlloc;
106
107    fake_vblank->screen = screen;
108    fake_vblank->event_id = event_id;
109    fake_vblank->timer = TimerSet(NULL, 0, delay, present_fake_do_timer, fake_vblank);
110    if (!fake_vblank->timer) {
111        free(fake_vblank);
112        return BadAlloc;
113    }
114
115    xorg_list_add(&fake_vblank->list, &fake_vblank_queue);
116
117    return Success;
118}
119
120void
121present_fake_screen_init(ScreenPtr screen)
122{
123    present_screen_priv_ptr screen_priv = present_screen_priv(screen);
124
125    /* For screens with hardware vblank support, the fake code
126     * will be used for off-screen windows and while screens are blanked,
127     * in which case we want a slow interval here
128     *
129     * Otherwise, pretend that the screen runs at 60Hz
130     */
131    if (screen_priv->info && screen_priv->info->get_crtc)
132        screen_priv->fake_interval = 1000000;
133    else
134        screen_priv->fake_interval = 16667;
135}
136
137void
138present_fake_queue_init(void)
139{
140    xorg_list_init(&fake_vblank_queue);
141}
142