1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright © 2013 Keith Packard
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Permission to use, copy, modify, distribute, and sell this software and its
5*4882a593Smuzhiyun * documentation for any purpose is hereby granted without fee, provided that
6*4882a593Smuzhiyun * the above copyright notice appear in all copies and that both that copyright
7*4882a593Smuzhiyun * notice and this permission notice appear in supporting documentation, and
8*4882a593Smuzhiyun * that the name of the copyright holders not be used in advertising or
9*4882a593Smuzhiyun * publicity pertaining to distribution of the software without specific,
10*4882a593Smuzhiyun * written prior permission. The copyright holders make no representations
11*4882a593Smuzhiyun * about the suitability of this software for any purpose. It is provided "as
12*4882a593Smuzhiyun * is" without express or implied warranty.
13*4882a593Smuzhiyun *
14*4882a593Smuzhiyun * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15*4882a593Smuzhiyun * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16*4882a593Smuzhiyun * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17*4882a593Smuzhiyun * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18*4882a593Smuzhiyun * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19*4882a593Smuzhiyun * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20*4882a593Smuzhiyun * OF THIS SOFTWARE.
21*4882a593Smuzhiyun */
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun #ifdef HAVE_XORG_CONFIG_H
24*4882a593Smuzhiyun #include <xorg-config.h>
25*4882a593Smuzhiyun #endif
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun #include "present_priv.h"
28*4882a593Smuzhiyun #include <gcstruct.h>
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun /*
31*4882a593Smuzhiyun * Returns:
32*4882a593Smuzhiyun * TRUE if the first MSC value is equal to or after the second one
33*4882a593Smuzhiyun * FALSE if the first MSC value is before the second one
34*4882a593Smuzhiyun */
35*4882a593Smuzhiyun static Bool
msc_is_equal_or_after(uint64_t test,uint64_t reference)36*4882a593Smuzhiyun msc_is_equal_or_after(uint64_t test, uint64_t reference)
37*4882a593Smuzhiyun {
38*4882a593Smuzhiyun return (int64_t)(test - reference) >= 0;
39*4882a593Smuzhiyun }
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun uint32_t
present_query_capabilities(RRCrtcPtr crtc)42*4882a593Smuzhiyun present_query_capabilities(RRCrtcPtr crtc)
43*4882a593Smuzhiyun {
44*4882a593Smuzhiyun present_screen_priv_ptr screen_priv;
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun if (!crtc)
47*4882a593Smuzhiyun return 0;
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun screen_priv = present_screen_priv(crtc->pScreen);
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun if (!screen_priv)
52*4882a593Smuzhiyun return 0;
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun return screen_priv->query_capabilities(screen_priv);
55*4882a593Smuzhiyun }
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun RRCrtcPtr
present_get_crtc(WindowPtr window)58*4882a593Smuzhiyun present_get_crtc(WindowPtr window)
59*4882a593Smuzhiyun {
60*4882a593Smuzhiyun ScreenPtr screen = window->drawable.pScreen;
61*4882a593Smuzhiyun present_screen_priv_ptr screen_priv = present_screen_priv(screen);
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun if (!screen_priv)
64*4882a593Smuzhiyun return NULL;
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun return screen_priv->get_crtc(screen_priv, window);
67*4882a593Smuzhiyun }
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun /*
70*4882a593Smuzhiyun * Copies the update region from a pixmap to the target drawable
71*4882a593Smuzhiyun */
72*4882a593Smuzhiyun void
present_copy_region(DrawablePtr drawable,PixmapPtr pixmap,RegionPtr update,int16_t x_off,int16_t y_off)73*4882a593Smuzhiyun present_copy_region(DrawablePtr drawable,
74*4882a593Smuzhiyun PixmapPtr pixmap,
75*4882a593Smuzhiyun RegionPtr update,
76*4882a593Smuzhiyun int16_t x_off,
77*4882a593Smuzhiyun int16_t y_off)
78*4882a593Smuzhiyun {
79*4882a593Smuzhiyun ScreenPtr screen = drawable->pScreen;
80*4882a593Smuzhiyun GCPtr gc;
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun gc = GetScratchGC(drawable->depth, screen);
83*4882a593Smuzhiyun if (update) {
84*4882a593Smuzhiyun ChangeGCVal changes[2];
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun changes[0].val = x_off;
87*4882a593Smuzhiyun changes[1].val = y_off;
88*4882a593Smuzhiyun ChangeGC(serverClient, gc,
89*4882a593Smuzhiyun GCClipXOrigin|GCClipYOrigin,
90*4882a593Smuzhiyun changes);
91*4882a593Smuzhiyun (*gc->funcs->ChangeClip)(gc, CT_REGION, update, 0);
92*4882a593Smuzhiyun }
93*4882a593Smuzhiyun ValidateGC(drawable, gc);
94*4882a593Smuzhiyun (*gc->ops->CopyArea)(&pixmap->drawable,
95*4882a593Smuzhiyun drawable,
96*4882a593Smuzhiyun gc,
97*4882a593Smuzhiyun 0, 0,
98*4882a593Smuzhiyun pixmap->drawable.width, pixmap->drawable.height,
99*4882a593Smuzhiyun x_off, y_off);
100*4882a593Smuzhiyun if (update)
101*4882a593Smuzhiyun (*gc->funcs->ChangeClip)(gc, CT_NONE, NULL, 0);
102*4882a593Smuzhiyun FreeScratchGC(gc);
103*4882a593Smuzhiyun }
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun void
present_pixmap_idle(PixmapPtr pixmap,WindowPtr window,CARD32 serial,struct present_fence * present_fence)106*4882a593Smuzhiyun present_pixmap_idle(PixmapPtr pixmap, WindowPtr window, CARD32 serial, struct present_fence *present_fence)
107*4882a593Smuzhiyun {
108*4882a593Smuzhiyun if (present_fence)
109*4882a593Smuzhiyun present_fence_set_triggered(present_fence);
110*4882a593Smuzhiyun if (window) {
111*4882a593Smuzhiyun DebugPresent(("\ti %08" PRIx32 "\n", pixmap ? pixmap->drawable.id : 0));
112*4882a593Smuzhiyun present_send_idle_notify(window, serial, pixmap, present_fence);
113*4882a593Smuzhiyun }
114*4882a593Smuzhiyun }
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun struct pixmap_visit {
117*4882a593Smuzhiyun PixmapPtr old;
118*4882a593Smuzhiyun PixmapPtr new;
119*4882a593Smuzhiyun };
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun static int
present_set_tree_pixmap_visit(WindowPtr window,void * data)122*4882a593Smuzhiyun present_set_tree_pixmap_visit(WindowPtr window, void *data)
123*4882a593Smuzhiyun {
124*4882a593Smuzhiyun struct pixmap_visit *visit = data;
125*4882a593Smuzhiyun ScreenPtr screen = window->drawable.pScreen;
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun if ((*screen->GetWindowPixmap)(window) != visit->old)
128*4882a593Smuzhiyun return WT_DONTWALKCHILDREN;
129*4882a593Smuzhiyun (*screen->SetWindowPixmap)(window, visit->new);
130*4882a593Smuzhiyun return WT_WALKCHILDREN;
131*4882a593Smuzhiyun }
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun void
present_set_tree_pixmap(WindowPtr window,PixmapPtr expected,PixmapPtr pixmap)134*4882a593Smuzhiyun present_set_tree_pixmap(WindowPtr window,
135*4882a593Smuzhiyun PixmapPtr expected,
136*4882a593Smuzhiyun PixmapPtr pixmap)
137*4882a593Smuzhiyun {
138*4882a593Smuzhiyun struct pixmap_visit visit;
139*4882a593Smuzhiyun ScreenPtr screen = window->drawable.pScreen;
140*4882a593Smuzhiyun
141*4882a593Smuzhiyun visit.old = (*screen->GetWindowPixmap)(window);
142*4882a593Smuzhiyun if (expected && visit.old != expected)
143*4882a593Smuzhiyun return;
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun visit.new = pixmap;
146*4882a593Smuzhiyun if (visit.old == visit.new)
147*4882a593Smuzhiyun return;
148*4882a593Smuzhiyun TraverseTree(window, present_set_tree_pixmap_visit, &visit);
149*4882a593Smuzhiyun }
150*4882a593Smuzhiyun
151*4882a593Smuzhiyun Bool
present_can_window_flip(WindowPtr window)152*4882a593Smuzhiyun present_can_window_flip(WindowPtr window)
153*4882a593Smuzhiyun {
154*4882a593Smuzhiyun ScreenPtr screen = window->drawable.pScreen;
155*4882a593Smuzhiyun present_screen_priv_ptr screen_priv = present_screen_priv(screen);
156*4882a593Smuzhiyun
157*4882a593Smuzhiyun return screen_priv->can_window_flip(window);
158*4882a593Smuzhiyun }
159*4882a593Smuzhiyun
160*4882a593Smuzhiyun void
present_adjust_timings(uint32_t options,uint64_t * crtc_msc,uint64_t * target_msc,uint64_t divisor,uint64_t remainder)161*4882a593Smuzhiyun present_adjust_timings(uint32_t options,
162*4882a593Smuzhiyun uint64_t *crtc_msc,
163*4882a593Smuzhiyun uint64_t *target_msc,
164*4882a593Smuzhiyun uint64_t divisor,
165*4882a593Smuzhiyun uint64_t remainder)
166*4882a593Smuzhiyun {
167*4882a593Smuzhiyun /* Adjust target_msc to match modulus
168*4882a593Smuzhiyun */
169*4882a593Smuzhiyun if (msc_is_equal_or_after(*crtc_msc, *target_msc)) {
170*4882a593Smuzhiyun if (divisor != 0) {
171*4882a593Smuzhiyun *target_msc = *crtc_msc - (*crtc_msc % divisor) + remainder;
172*4882a593Smuzhiyun if (options & PresentOptionAsync) {
173*4882a593Smuzhiyun if (msc_is_after(*crtc_msc, *target_msc))
174*4882a593Smuzhiyun *target_msc += divisor;
175*4882a593Smuzhiyun } else {
176*4882a593Smuzhiyun if (msc_is_equal_or_after(*crtc_msc, *target_msc))
177*4882a593Smuzhiyun *target_msc += divisor;
178*4882a593Smuzhiyun }
179*4882a593Smuzhiyun } else {
180*4882a593Smuzhiyun *target_msc = *crtc_msc;
181*4882a593Smuzhiyun if (!(options & PresentOptionAsync))
182*4882a593Smuzhiyun (*target_msc)++;
183*4882a593Smuzhiyun }
184*4882a593Smuzhiyun }
185*4882a593Smuzhiyun }
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun int
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 window_msc,uint64_t divisor,uint64_t remainder,present_notify_ptr notifies,int num_notifies)188*4882a593Smuzhiyun present_pixmap(WindowPtr window,
189*4882a593Smuzhiyun PixmapPtr pixmap,
190*4882a593Smuzhiyun CARD32 serial,
191*4882a593Smuzhiyun RegionPtr valid,
192*4882a593Smuzhiyun RegionPtr update,
193*4882a593Smuzhiyun int16_t x_off,
194*4882a593Smuzhiyun int16_t y_off,
195*4882a593Smuzhiyun RRCrtcPtr target_crtc,
196*4882a593Smuzhiyun SyncFence *wait_fence,
197*4882a593Smuzhiyun SyncFence *idle_fence,
198*4882a593Smuzhiyun uint32_t options,
199*4882a593Smuzhiyun uint64_t window_msc,
200*4882a593Smuzhiyun uint64_t divisor,
201*4882a593Smuzhiyun uint64_t remainder,
202*4882a593Smuzhiyun present_notify_ptr notifies,
203*4882a593Smuzhiyun int num_notifies)
204*4882a593Smuzhiyun {
205*4882a593Smuzhiyun ScreenPtr screen = window->drawable.pScreen;
206*4882a593Smuzhiyun present_screen_priv_ptr screen_priv = present_screen_priv(screen);
207*4882a593Smuzhiyun
208*4882a593Smuzhiyun return screen_priv->present_pixmap(window,
209*4882a593Smuzhiyun pixmap,
210*4882a593Smuzhiyun serial,
211*4882a593Smuzhiyun valid,
212*4882a593Smuzhiyun update,
213*4882a593Smuzhiyun x_off,
214*4882a593Smuzhiyun y_off,
215*4882a593Smuzhiyun target_crtc,
216*4882a593Smuzhiyun wait_fence,
217*4882a593Smuzhiyun idle_fence,
218*4882a593Smuzhiyun options,
219*4882a593Smuzhiyun window_msc,
220*4882a593Smuzhiyun divisor,
221*4882a593Smuzhiyun remainder,
222*4882a593Smuzhiyun notifies,
223*4882a593Smuzhiyun num_notifies);
224*4882a593Smuzhiyun }
225*4882a593Smuzhiyun
226*4882a593Smuzhiyun int
present_notify_msc(WindowPtr window,CARD32 serial,uint64_t target_msc,uint64_t divisor,uint64_t remainder)227*4882a593Smuzhiyun present_notify_msc(WindowPtr window,
228*4882a593Smuzhiyun CARD32 serial,
229*4882a593Smuzhiyun uint64_t target_msc,
230*4882a593Smuzhiyun uint64_t divisor,
231*4882a593Smuzhiyun uint64_t remainder)
232*4882a593Smuzhiyun {
233*4882a593Smuzhiyun return present_pixmap(window,
234*4882a593Smuzhiyun NULL,
235*4882a593Smuzhiyun serial,
236*4882a593Smuzhiyun NULL, NULL,
237*4882a593Smuzhiyun 0, 0,
238*4882a593Smuzhiyun NULL,
239*4882a593Smuzhiyun NULL, NULL,
240*4882a593Smuzhiyun divisor == 0 ? PresentOptionAsync : 0,
241*4882a593Smuzhiyun target_msc, divisor, remainder, NULL, 0);
242*4882a593Smuzhiyun }
243