xref: /OK3568_Linux_fs/external/xserver/hw/xfree86/drivers/modesetting/present.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright © 2014 Intel Corporation
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_DIX_CONFIG_H
24*4882a593Smuzhiyun #include "dix-config.h"
25*4882a593Smuzhiyun #endif
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun #include <assert.h>
28*4882a593Smuzhiyun #include <errno.h>
29*4882a593Smuzhiyun #include <fcntl.h>
30*4882a593Smuzhiyun #include <unistd.h>
31*4882a593Smuzhiyun #include <stdio.h>
32*4882a593Smuzhiyun #include <stdint.h>
33*4882a593Smuzhiyun #include <string.h>
34*4882a593Smuzhiyun #include <sys/ioctl.h>
35*4882a593Smuzhiyun #include <sys/time.h>
36*4882a593Smuzhiyun #include <sys/types.h>
37*4882a593Smuzhiyun #include <time.h>
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun #include <xf86.h>
40*4882a593Smuzhiyun #include <xf86Crtc.h>
41*4882a593Smuzhiyun #include <xf86drm.h>
42*4882a593Smuzhiyun #include <xf86str.h>
43*4882a593Smuzhiyun #include <present.h>
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun #include "driver.h"
46*4882a593Smuzhiyun #include "drmmode_display.h"
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun #if 0
49*4882a593Smuzhiyun #define DebugPresent(x) ErrorF x
50*4882a593Smuzhiyun #else
51*4882a593Smuzhiyun #define DebugPresent(x)
52*4882a593Smuzhiyun #endif
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun struct ms_present_vblank_event {
55*4882a593Smuzhiyun     uint64_t        event_id;
56*4882a593Smuzhiyun     Bool            unflip;
57*4882a593Smuzhiyun };
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun static RRCrtcPtr
ms_present_get_crtc(WindowPtr window)60*4882a593Smuzhiyun ms_present_get_crtc(WindowPtr window)
61*4882a593Smuzhiyun {
62*4882a593Smuzhiyun     return ms_randr_crtc_covering_drawable(&window->drawable);
63*4882a593Smuzhiyun }
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun static int
ms_present_get_ust_msc(RRCrtcPtr crtc,CARD64 * ust,CARD64 * msc)66*4882a593Smuzhiyun ms_present_get_ust_msc(RRCrtcPtr crtc, CARD64 *ust, CARD64 *msc)
67*4882a593Smuzhiyun {
68*4882a593Smuzhiyun     xf86CrtcPtr xf86_crtc = crtc->devPrivate;
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun     return ms_get_crtc_ust_msc(xf86_crtc, ust, msc);
71*4882a593Smuzhiyun }
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun /*
74*4882a593Smuzhiyun  * Called when the queued vblank event has occurred
75*4882a593Smuzhiyun  */
76*4882a593Smuzhiyun static void
ms_present_vblank_handler(uint64_t msc,uint64_t usec,void * data)77*4882a593Smuzhiyun ms_present_vblank_handler(uint64_t msc, uint64_t usec, void *data)
78*4882a593Smuzhiyun {
79*4882a593Smuzhiyun     struct ms_present_vblank_event *event = data;
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun     DebugPresent(("\t\tmh %lld msc %llu\n",
82*4882a593Smuzhiyun                  (long long) event->event_id, (long long) msc));
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun     present_event_notify(event->event_id, usec, msc);
85*4882a593Smuzhiyun     free(event);
86*4882a593Smuzhiyun }
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun /*
89*4882a593Smuzhiyun  * Called when the queued vblank is aborted
90*4882a593Smuzhiyun  */
91*4882a593Smuzhiyun static void
ms_present_vblank_abort(void * data)92*4882a593Smuzhiyun ms_present_vblank_abort(void *data)
93*4882a593Smuzhiyun {
94*4882a593Smuzhiyun     struct ms_present_vblank_event *event = data;
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun     DebugPresent(("\t\tma %lld\n", (long long) event->event_id));
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun     free(event);
99*4882a593Smuzhiyun }
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun /*
102*4882a593Smuzhiyun  * Queue an event to report back to the Present extension when the specified
103*4882a593Smuzhiyun  * MSC has past
104*4882a593Smuzhiyun  */
105*4882a593Smuzhiyun static int
ms_present_queue_vblank(RRCrtcPtr crtc,uint64_t event_id,uint64_t msc)106*4882a593Smuzhiyun ms_present_queue_vblank(RRCrtcPtr crtc,
107*4882a593Smuzhiyun                         uint64_t event_id,
108*4882a593Smuzhiyun                         uint64_t msc)
109*4882a593Smuzhiyun {
110*4882a593Smuzhiyun     xf86CrtcPtr xf86_crtc = crtc->devPrivate;
111*4882a593Smuzhiyun     struct ms_present_vblank_event *event;
112*4882a593Smuzhiyun     uint32_t seq;
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun     event = calloc(sizeof(struct ms_present_vblank_event), 1);
115*4882a593Smuzhiyun     if (!event)
116*4882a593Smuzhiyun         return BadAlloc;
117*4882a593Smuzhiyun     event->event_id = event_id;
118*4882a593Smuzhiyun     seq = ms_drm_queue_alloc(xf86_crtc, event,
119*4882a593Smuzhiyun                              ms_present_vblank_handler,
120*4882a593Smuzhiyun                              ms_present_vblank_abort);
121*4882a593Smuzhiyun     if (!seq) {
122*4882a593Smuzhiyun         free(event);
123*4882a593Smuzhiyun         return BadAlloc;
124*4882a593Smuzhiyun     }
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun     if (!ms_queue_vblank(xf86_crtc, MS_QUEUE_ABSOLUTE, msc, NULL, seq))
127*4882a593Smuzhiyun         return BadAlloc;
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun     DebugPresent(("\t\tmq %lld seq %u msc %llu (hw msc %u)\n",
130*4882a593Smuzhiyun                  (long long) event_id, seq, (long long) msc,
131*4882a593Smuzhiyun                  vbl.request.sequence));
132*4882a593Smuzhiyun     return Success;
133*4882a593Smuzhiyun }
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun static Bool
ms_present_event_match(void * data,void * match_data)136*4882a593Smuzhiyun ms_present_event_match(void *data, void *match_data)
137*4882a593Smuzhiyun {
138*4882a593Smuzhiyun     struct ms_present_vblank_event *event = data;
139*4882a593Smuzhiyun     uint64_t *match = match_data;
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun     return *match == event->event_id;
142*4882a593Smuzhiyun }
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun /*
145*4882a593Smuzhiyun  * Remove a pending vblank event from the DRM queue so that it is not reported
146*4882a593Smuzhiyun  * to the extension
147*4882a593Smuzhiyun  */
148*4882a593Smuzhiyun static void
ms_present_abort_vblank(RRCrtcPtr crtc,uint64_t event_id,uint64_t msc)149*4882a593Smuzhiyun ms_present_abort_vblank(RRCrtcPtr crtc, uint64_t event_id, uint64_t msc)
150*4882a593Smuzhiyun {
151*4882a593Smuzhiyun     ScreenPtr screen = crtc->pScreen;
152*4882a593Smuzhiyun     ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun     ms_drm_abort(scrn, ms_present_event_match, &event_id);
155*4882a593Smuzhiyun }
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun /*
158*4882a593Smuzhiyun  * Flush our batch buffer when requested by the Present extension.
159*4882a593Smuzhiyun  */
160*4882a593Smuzhiyun static void
ms_present_flush(WindowPtr window)161*4882a593Smuzhiyun ms_present_flush(WindowPtr window)
162*4882a593Smuzhiyun {
163*4882a593Smuzhiyun #ifdef GLAMOR_HAS_GBM
164*4882a593Smuzhiyun     ScreenPtr screen = window->drawable.pScreen;
165*4882a593Smuzhiyun     ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
166*4882a593Smuzhiyun     modesettingPtr ms = modesettingPTR(scrn);
167*4882a593Smuzhiyun 
168*4882a593Smuzhiyun     if (ms->drmmode.glamor)
169*4882a593Smuzhiyun         glamor_block_handler(screen);
170*4882a593Smuzhiyun #endif
171*4882a593Smuzhiyun }
172*4882a593Smuzhiyun 
173*4882a593Smuzhiyun /**
174*4882a593Smuzhiyun  * Callback for the DRM event queue when a flip has completed on all pipes
175*4882a593Smuzhiyun  *
176*4882a593Smuzhiyun  * Notify the extension code
177*4882a593Smuzhiyun  */
178*4882a593Smuzhiyun static void
ms_present_flip_handler(modesettingPtr ms,uint64_t msc,uint64_t ust,void * data)179*4882a593Smuzhiyun ms_present_flip_handler(modesettingPtr ms, uint64_t msc,
180*4882a593Smuzhiyun                         uint64_t ust, void *data)
181*4882a593Smuzhiyun {
182*4882a593Smuzhiyun     struct ms_present_vblank_event *event = data;
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun     DebugPresent(("\t\tms:fc %lld msc %llu ust %llu\n",
185*4882a593Smuzhiyun                   (long long) event->event_id,
186*4882a593Smuzhiyun                   (long long) msc, (long long) ust));
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun     if (event->unflip)
189*4882a593Smuzhiyun         ms->drmmode.present_flipping = FALSE;
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun     ms_present_vblank_handler(msc, ust, event);
192*4882a593Smuzhiyun }
193*4882a593Smuzhiyun 
194*4882a593Smuzhiyun /*
195*4882a593Smuzhiyun  * Callback for the DRM queue abort code.  A flip has been aborted.
196*4882a593Smuzhiyun  */
197*4882a593Smuzhiyun static void
ms_present_flip_abort(modesettingPtr ms,void * data)198*4882a593Smuzhiyun ms_present_flip_abort(modesettingPtr ms, void *data)
199*4882a593Smuzhiyun {
200*4882a593Smuzhiyun     struct ms_present_vblank_event *event = data;
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun     DebugPresent(("\t\tms:fa %lld\n", (long long) event->event_id));
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun     free(event);
205*4882a593Smuzhiyun }
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun /*
208*4882a593Smuzhiyun  * Test to see if page flipping is possible on the target crtc
209*4882a593Smuzhiyun  *
210*4882a593Smuzhiyun  * We ignore sw-cursors when *disabling* flipping, we may very well be
211*4882a593Smuzhiyun  * returning to scanning out the normal framebuffer *because* we just
212*4882a593Smuzhiyun  * switched to sw-cursor mode and check_flip just failed because of that.
213*4882a593Smuzhiyun  */
214*4882a593Smuzhiyun static Bool
ms_present_check_unflip(RRCrtcPtr crtc,WindowPtr window,PixmapPtr pixmap,Bool sync_flip,PresentFlipReason * reason)215*4882a593Smuzhiyun ms_present_check_unflip(RRCrtcPtr crtc,
216*4882a593Smuzhiyun                         WindowPtr window,
217*4882a593Smuzhiyun                         PixmapPtr pixmap,
218*4882a593Smuzhiyun                         Bool sync_flip,
219*4882a593Smuzhiyun                         PresentFlipReason *reason)
220*4882a593Smuzhiyun {
221*4882a593Smuzhiyun     ScreenPtr screen = window->drawable.pScreen;
222*4882a593Smuzhiyun     ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
223*4882a593Smuzhiyun     modesettingPtr ms = modesettingPTR(scrn);
224*4882a593Smuzhiyun     xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(scrn);
225*4882a593Smuzhiyun     int num_crtcs_on = 0;
226*4882a593Smuzhiyun     int i;
227*4882a593Smuzhiyun     struct gbm_bo *gbm;
228*4882a593Smuzhiyun 
229*4882a593Smuzhiyun     if (!ms->drmmode.pageflip)
230*4882a593Smuzhiyun         return FALSE;
231*4882a593Smuzhiyun 
232*4882a593Smuzhiyun     if (ms->drmmode.dri2_flipping)
233*4882a593Smuzhiyun         return FALSE;
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun     if (!scrn->vtSema)
236*4882a593Smuzhiyun         return FALSE;
237*4882a593Smuzhiyun 
238*4882a593Smuzhiyun     for (i = 0; i < config->num_crtc; i++) {
239*4882a593Smuzhiyun #ifdef GLAMOR_HAS_GBM
240*4882a593Smuzhiyun         drmmode_crtc_private_ptr drmmode_crtc = config->crtc[i]->driver_private;
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun         /* Don't do pageflipping if CRTCs are rotated. */
243*4882a593Smuzhiyun         if (drmmode_crtc->rotate_bo.gbm)
244*4882a593Smuzhiyun             return FALSE;
245*4882a593Smuzhiyun #endif
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun         if (ms_crtc_on(config->crtc[i]))
248*4882a593Smuzhiyun             num_crtcs_on++;
249*4882a593Smuzhiyun     }
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun     /* We can't do pageflipping if all the CRTCs are off. */
252*4882a593Smuzhiyun     if (num_crtcs_on == 0)
253*4882a593Smuzhiyun         return FALSE;
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun     /* Check stride, can't change that on flip */
256*4882a593Smuzhiyun     if (!ms->atomic_modeset &&
257*4882a593Smuzhiyun         pixmap->devKind != drmmode_bo_get_pitch(&ms->drmmode.front_bo))
258*4882a593Smuzhiyun         return FALSE;
259*4882a593Smuzhiyun 
260*4882a593Smuzhiyun     if (ms->drmmode.exa)
261*4882a593Smuzhiyun         return TRUE;
262*4882a593Smuzhiyun 
263*4882a593Smuzhiyun     if (!ms->drmmode.glamor)
264*4882a593Smuzhiyun         return FALSE;
265*4882a593Smuzhiyun 
266*4882a593Smuzhiyun #ifdef GBM_BO_WITH_MODIFIERS
267*4882a593Smuzhiyun     /* Check if buffer format/modifier is supported by all active CRTCs */
268*4882a593Smuzhiyun     gbm = glamor_gbm_bo_from_pixmap(screen, pixmap);
269*4882a593Smuzhiyun     if (gbm) {
270*4882a593Smuzhiyun         uint32_t format;
271*4882a593Smuzhiyun         uint64_t modifier;
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun         format = gbm_bo_get_format(gbm);
274*4882a593Smuzhiyun         modifier = gbm_bo_get_modifier(gbm);
275*4882a593Smuzhiyun 
276*4882a593Smuzhiyun         if (!drmmode_is_format_supported(scrn, format, modifier)) {
277*4882a593Smuzhiyun             if (reason)
278*4882a593Smuzhiyun                 *reason = PRESENT_FLIP_REASON_BUFFER_FORMAT;
279*4882a593Smuzhiyun             return FALSE;
280*4882a593Smuzhiyun         }
281*4882a593Smuzhiyun     }
282*4882a593Smuzhiyun #endif
283*4882a593Smuzhiyun 
284*4882a593Smuzhiyun     /* Make sure there's a bo we can get to */
285*4882a593Smuzhiyun     /* XXX: actually do this.  also...is it sufficient?
286*4882a593Smuzhiyun      * if (!glamor_get_pixmap_private(pixmap))
287*4882a593Smuzhiyun      *     return FALSE;
288*4882a593Smuzhiyun      */
289*4882a593Smuzhiyun 
290*4882a593Smuzhiyun     return TRUE;
291*4882a593Smuzhiyun }
292*4882a593Smuzhiyun 
293*4882a593Smuzhiyun static Bool
ms_present_check_flip(RRCrtcPtr crtc,WindowPtr window,PixmapPtr pixmap,Bool sync_flip,PresentFlipReason * reason)294*4882a593Smuzhiyun ms_present_check_flip(RRCrtcPtr crtc,
295*4882a593Smuzhiyun                       WindowPtr window,
296*4882a593Smuzhiyun                       PixmapPtr pixmap,
297*4882a593Smuzhiyun                       Bool sync_flip,
298*4882a593Smuzhiyun                       PresentFlipReason *reason)
299*4882a593Smuzhiyun {
300*4882a593Smuzhiyun     ScreenPtr screen = window->drawable.pScreen;
301*4882a593Smuzhiyun     ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
302*4882a593Smuzhiyun     modesettingPtr ms = modesettingPTR(scrn);
303*4882a593Smuzhiyun 
304*4882a593Smuzhiyun     if (ms->drmmode.sprites_visible > 0)
305*4882a593Smuzhiyun         return FALSE;
306*4882a593Smuzhiyun 
307*4882a593Smuzhiyun     return ms_present_check_unflip(crtc, window, pixmap, sync_flip, reason);
308*4882a593Smuzhiyun }
309*4882a593Smuzhiyun 
310*4882a593Smuzhiyun /*
311*4882a593Smuzhiyun  * Queue a flip on 'crtc' to 'pixmap' at 'target_msc'. If 'sync_flip' is true,
312*4882a593Smuzhiyun  * then wait for vblank. Otherwise, flip immediately
313*4882a593Smuzhiyun  */
314*4882a593Smuzhiyun static Bool
ms_present_flip(RRCrtcPtr crtc,uint64_t event_id,uint64_t target_msc,PixmapPtr pixmap,Bool sync_flip)315*4882a593Smuzhiyun ms_present_flip(RRCrtcPtr crtc,
316*4882a593Smuzhiyun                 uint64_t event_id,
317*4882a593Smuzhiyun                 uint64_t target_msc,
318*4882a593Smuzhiyun                 PixmapPtr pixmap,
319*4882a593Smuzhiyun                 Bool sync_flip)
320*4882a593Smuzhiyun {
321*4882a593Smuzhiyun     ScreenPtr screen = crtc->pScreen;
322*4882a593Smuzhiyun     ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
323*4882a593Smuzhiyun     modesettingPtr ms = modesettingPTR(scrn);
324*4882a593Smuzhiyun     xf86CrtcPtr xf86_crtc = crtc->devPrivate;
325*4882a593Smuzhiyun     drmmode_crtc_private_ptr drmmode_crtc = xf86_crtc->driver_private;
326*4882a593Smuzhiyun     Bool ret;
327*4882a593Smuzhiyun     struct ms_present_vblank_event *event;
328*4882a593Smuzhiyun 
329*4882a593Smuzhiyun     if (!ms_present_check_flip(crtc, screen->root, pixmap, sync_flip, NULL))
330*4882a593Smuzhiyun         return FALSE;
331*4882a593Smuzhiyun 
332*4882a593Smuzhiyun     event = calloc(1, sizeof(struct ms_present_vblank_event));
333*4882a593Smuzhiyun     if (!event)
334*4882a593Smuzhiyun         return FALSE;
335*4882a593Smuzhiyun 
336*4882a593Smuzhiyun     DebugPresent(("\t\tms:pf %lld msc %llu\n",
337*4882a593Smuzhiyun                   (long long) event_id, (long long) target_msc));
338*4882a593Smuzhiyun 
339*4882a593Smuzhiyun     event->event_id = event_id;
340*4882a593Smuzhiyun     event->unflip = FALSE;
341*4882a593Smuzhiyun 
342*4882a593Smuzhiyun     ret = ms_do_pageflip(screen, pixmap, event, drmmode_crtc->vblank_pipe, !sync_flip,
343*4882a593Smuzhiyun                          ms_present_flip_handler, ms_present_flip_abort);
344*4882a593Smuzhiyun     if (!ret) {
345*4882a593Smuzhiyun         xf86DrvMsg(scrn->scrnIndex, X_ERROR, "present flip failed\n");
346*4882a593Smuzhiyun     } else {
347*4882a593Smuzhiyun         ms->drmmode.present_flipping = TRUE;
348*4882a593Smuzhiyun         drmmode_crtc->external_flipped = TRUE;
349*4882a593Smuzhiyun     }
350*4882a593Smuzhiyun 
351*4882a593Smuzhiyun     return ret;
352*4882a593Smuzhiyun }
353*4882a593Smuzhiyun 
354*4882a593Smuzhiyun /*
355*4882a593Smuzhiyun  * Queue a flip back to the normal frame buffer
356*4882a593Smuzhiyun  */
357*4882a593Smuzhiyun static void
ms_present_unflip(ScreenPtr screen,uint64_t event_id)358*4882a593Smuzhiyun ms_present_unflip(ScreenPtr screen, uint64_t event_id)
359*4882a593Smuzhiyun {
360*4882a593Smuzhiyun     ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
361*4882a593Smuzhiyun     modesettingPtr ms = modesettingPTR(scrn);
362*4882a593Smuzhiyun     PixmapPtr pixmap = screen->GetScreenPixmap(screen);
363*4882a593Smuzhiyun     xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(scrn);
364*4882a593Smuzhiyun     int i;
365*4882a593Smuzhiyun     struct ms_present_vblank_event *event;
366*4882a593Smuzhiyun 
367*4882a593Smuzhiyun     event = calloc(1, sizeof(struct ms_present_vblank_event));
368*4882a593Smuzhiyun     if (!event)
369*4882a593Smuzhiyun         return;
370*4882a593Smuzhiyun 
371*4882a593Smuzhiyun     event->event_id = event_id;
372*4882a593Smuzhiyun     event->unflip = TRUE;
373*4882a593Smuzhiyun 
374*4882a593Smuzhiyun     if (ms_present_check_unflip(NULL, screen->root, pixmap, TRUE, NULL) &&
375*4882a593Smuzhiyun         ms_do_pageflip(screen, pixmap, event, -1, FALSE,
376*4882a593Smuzhiyun                        ms_present_flip_handler, ms_present_flip_abort)) {
377*4882a593Smuzhiyun         return;
378*4882a593Smuzhiyun     }
379*4882a593Smuzhiyun 
380*4882a593Smuzhiyun     for (i = 0; i < config->num_crtc; i++) {
381*4882a593Smuzhiyun         xf86CrtcPtr crtc = config->crtc[i];
382*4882a593Smuzhiyun 	drmmode_crtc_private_ptr drmmode_crtc = crtc->driver_private;
383*4882a593Smuzhiyun 
384*4882a593Smuzhiyun 	if (!crtc->enabled)
385*4882a593Smuzhiyun 	    continue;
386*4882a593Smuzhiyun 
387*4882a593Smuzhiyun 	/* info->drmmode.fb_id still points to the FB for the last flipped BO.
388*4882a593Smuzhiyun 	 * Clear it, drmmode_set_mode_major will re-create it
389*4882a593Smuzhiyun 	 */
390*4882a593Smuzhiyun 	if (drmmode_crtc->drmmode->fb_id) {
391*4882a593Smuzhiyun 		drmModeRmFB(drmmode_crtc->drmmode->fd,
392*4882a593Smuzhiyun 			    drmmode_crtc->drmmode->fb_id);
393*4882a593Smuzhiyun 		drmmode_crtc->drmmode->fb_id = 0;
394*4882a593Smuzhiyun 	}
395*4882a593Smuzhiyun 
396*4882a593Smuzhiyun 	if (drmmode_crtc->dpms_mode == DPMSModeOn)
397*4882a593Smuzhiyun 	    crtc->funcs->set_mode_major(crtc, &crtc->mode, crtc->rotation,
398*4882a593Smuzhiyun 					crtc->x, crtc->y);
399*4882a593Smuzhiyun 	else
400*4882a593Smuzhiyun 	    drmmode_crtc->need_modeset = TRUE;
401*4882a593Smuzhiyun     }
402*4882a593Smuzhiyun 
403*4882a593Smuzhiyun     present_event_notify(event_id, 0, 0);
404*4882a593Smuzhiyun     ms->drmmode.present_flipping = FALSE;
405*4882a593Smuzhiyun }
406*4882a593Smuzhiyun 
407*4882a593Smuzhiyun static present_screen_info_rec ms_present_screen_info = {
408*4882a593Smuzhiyun     .version = PRESENT_SCREEN_INFO_VERSION,
409*4882a593Smuzhiyun 
410*4882a593Smuzhiyun     .get_crtc = ms_present_get_crtc,
411*4882a593Smuzhiyun     .get_ust_msc = ms_present_get_ust_msc,
412*4882a593Smuzhiyun     .queue_vblank = ms_present_queue_vblank,
413*4882a593Smuzhiyun     .abort_vblank = ms_present_abort_vblank,
414*4882a593Smuzhiyun     .flush = ms_present_flush,
415*4882a593Smuzhiyun 
416*4882a593Smuzhiyun     .capabilities = PresentCapabilityNone,
417*4882a593Smuzhiyun     .check_flip = NULL,
418*4882a593Smuzhiyun     .check_flip2 = ms_present_check_flip,
419*4882a593Smuzhiyun     .flip = ms_present_flip,
420*4882a593Smuzhiyun     .unflip = ms_present_unflip,
421*4882a593Smuzhiyun };
422*4882a593Smuzhiyun 
423*4882a593Smuzhiyun Bool
ms_present_screen_init(ScreenPtr screen)424*4882a593Smuzhiyun ms_present_screen_init(ScreenPtr screen)
425*4882a593Smuzhiyun {
426*4882a593Smuzhiyun     ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
427*4882a593Smuzhiyun     modesettingPtr ms = modesettingPTR(scrn);
428*4882a593Smuzhiyun     uint64_t value;
429*4882a593Smuzhiyun     int ret;
430*4882a593Smuzhiyun 
431*4882a593Smuzhiyun     ret = drmGetCap(ms->fd, DRM_CAP_ASYNC_PAGE_FLIP, &value);
432*4882a593Smuzhiyun     if (ret == 0 && value == 1)
433*4882a593Smuzhiyun         ms_present_screen_info.capabilities |= PresentCapabilityAsync;
434*4882a593Smuzhiyun 
435*4882a593Smuzhiyun     return present_screen_init(screen, &ms_present_screen_info);
436*4882a593Smuzhiyun }
437