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