fliptest: request OMAP_BO_SCANOUT buffers
[glsdk/omapdrmtest.git] / util / display-kms.c
1 /*
2  * Copyright (C) 2011 Texas Instruments
3  * Author: Rob Clark <rob.clark@linaro.org>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 as published by
7  * the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
18 #ifdef HAVE_CONFIG_H
19 #include "config.h"
20 #endif
22 #include "util.h"
24 #include <xf86drmMode.h>
27 /* NOTE: healthy dose of recycling from libdrm modetest app.. */
29 /*
30  * Mode setting with the kernel interfaces is a bit of a chore.
31  * First you have to find the connector in question and make sure the
32  * requested mode is available.
33  * Then you need to find the encoder attached to that connector so you
34  * can bind it with a free crtc.
35  */
36 struct connector {
37         uint32_t id;
38         char mode_str[64];
39         drmModeModeInfo *mode;
40         drmModeEncoder *encoder;
41         int crtc;
42         int pipe;
43 };
45 #define to_display_kms(x) container_of(x, struct display_kms, base)
46 struct display_kms {
47         struct display base;
49         uint32_t connectors_count;
50         struct connector connector[10];
51         drmModePlane *ovr[10];
53         int scheduled_flips, completed_flips;
54         uint32_t bo_flags;
55         drmModeResPtr resources;
56         drmModePlaneRes *plane_resources;
57         struct buffer *current;
58 };
60 #define to_buffer_kms(x) container_of(x, struct buffer_kms, base)
61 struct buffer_kms {
62         struct buffer base;
63         uint32_t fb_id;
64 };
66 static struct omap_bo *
67 alloc_bo(struct display *disp, uint32_t bpp, uint32_t width, uint32_t height,
68                 uint32_t *bo_handle, uint32_t *pitch)
69 {
70         struct display_kms *disp_kms = to_display_kms(disp);
71         struct omap_bo *bo;
72         uint32_t bo_flags = disp_kms->bo_flags;
74         if ((bo_flags & OMAP_BO_TILED) == OMAP_BO_TILED) {
75                 bo_flags &= ~OMAP_BO_TILED;
76                 if (bpp == 8) {
77                         bo_flags |= OMAP_BO_TILED_8;
78                 } else if (bpp == 16) {
79                         bo_flags |= OMAP_BO_TILED_16;
80                 } else if (bpp == 32) {
81                         bo_flags |= OMAP_BO_TILED_32;
82                 }
83         }
85         bo_flags |= OMAP_BO_WC;
87         if (bo_flags & OMAP_BO_TILED) {
88                 bo = omap_bo_new_tiled(disp->dev, width, height, bo_flags);
89         } else {
90                 bo = omap_bo_new(disp->dev, width * height * bpp / 8, bo_flags);
91         }
93         if (bo) {
94                 *bo_handle = omap_bo_handle(bo);
95                 *pitch = width * bpp / 8;
96                 if (bo_flags & OMAP_BO_TILED)
97                         *pitch = ALIGN2(*pitch, PAGE_SHIFT);
98         }
100         return bo;
103 static struct buffer *
104 alloc_buffer(struct display *disp, uint32_t fourcc, uint32_t w, uint32_t h)
106         struct buffer_kms *buf_kms;
107         struct buffer *buf;
108         uint32_t bo_handles[4] = {0}, offsets[4] = {0};
109         int ret;
111         buf_kms = calloc(1, sizeof(*buf_kms));
112         if (!buf_kms) {
113                 ERROR("allocation failed");
114                 return NULL;
115         }
116         buf = &buf_kms->base;
118         buf->fourcc = fourcc;
119         buf->width = w;
120         buf->height = h;
122         buf->nbo = 1;
124         if (!fourcc)
125                 fourcc = FOURCC('A','R','2','4');
127         switch(fourcc) {
128         case FOURCC('A','R','2','4'):
129                 buf->nbo = 1;
130                 buf->bo[0] = alloc_bo(disp, 32, buf->width, buf->height,
131                                 &bo_handles[0], &buf->pitches[0]);
132                 break;
133         case FOURCC('U','Y','V','Y'):
134         case FOURCC('Y','U','Y','V'):
135                 buf->nbo = 1;
136                 buf->bo[0] = alloc_bo(disp, 16, buf->width, buf->height,
137                                 &bo_handles[0], &buf->pitches[0]);
138                 break;
139         case FOURCC('N','V','1','2'):
140                 buf->nbo = 2;
141                 buf->bo[0] = alloc_bo(disp, 8, buf->width, buf->height,
142                                 &bo_handles[0], &buf->pitches[0]);
143                 buf->bo[1] = alloc_bo(disp, 16, buf->width/2, buf->height/2,
144                                 &bo_handles[1], &buf->pitches[1]);
145                 break;
146         case FOURCC('I','4','2','0'):
147                 buf->nbo = 3;
148                 buf->bo[0] = alloc_bo(disp, 8, buf->width, buf->height,
149                                 &bo_handles[0], &buf->pitches[0]);
150                 buf->bo[1] = alloc_bo(disp, 8, buf->width/2, buf->height/2,
151                                 &bo_handles[1], &buf->pitches[1]);
152                 buf->bo[2] = alloc_bo(disp, 8, buf->width/2, buf->height/2,
153                                 &bo_handles[2], &buf->pitches[2]);
154                 break;
155         default:
156                 ERROR("invalid format: 0x%08x", fourcc);
157                 goto fail;
158         }
160         ret = drmModeAddFB2(disp->fd, buf->width, buf->height, fourcc,
161                         bo_handles, buf->pitches, offsets, &buf_kms->fb_id, 0);
162         if (ret) {
163                 ERROR("drmModeAddFB2 failed: %s (%d)", strerror(errno), ret);
164                 goto fail;
165         }
167         return buf;
169 fail:
170         // XXX cleanup
171         return NULL;
174 static struct buffer **
175 alloc_buffers(struct display *disp, uint32_t n,
176                 uint32_t fourcc, uint32_t w, uint32_t h)
178         struct buffer **bufs;
179         uint32_t i = 0;
181         bufs = calloc(n, sizeof(*bufs));
182         if (!bufs) {
183                 ERROR("allocation failed");
184                 goto fail;
185         }
187         for (i = 0; i < n; i++) {
188                 bufs[i] = alloc_buffer(disp, fourcc, w, h);
189                 if (!bufs[i]) {
190                         ERROR("allocation failed");
191                         goto fail;
192                 }
193         }
195         return bufs;
197 fail:
198         // XXX cleanup
199         return NULL;
202 static struct buffer **
203 get_buffers(struct display *disp, uint32_t n)
205         return alloc_buffers(disp, n, 0, disp->width, disp->height);
208 static struct buffer **
209 get_vid_buffers(struct display *disp, uint32_t n,
210                 uint32_t fourcc, uint32_t w, uint32_t h)
212         return alloc_buffers(disp, n, fourcc, w, h);
215 static void
216 page_flip_handler(int fd, unsigned int frame,
217                 unsigned int sec, unsigned int usec, void *data)
219         struct display *disp = data;
220         struct display_kms *disp_kms = to_display_kms(disp);
222         disp_kms->completed_flips++;
224         MSG("Page flip: frame=%d, sec=%d, usec=%d, remaining=%d", frame, sec, usec,
225                         disp_kms->scheduled_flips - disp_kms->completed_flips);
228 static int
229 post_buffer(struct display *disp, struct buffer *buf)
231         struct display_kms *disp_kms = to_display_kms(disp);
232         struct buffer_kms *buf_kms = to_buffer_kms(buf);
233         int ret, last_err = 0, x = 0;
234         uint32_t i;
236         for (i = 0; i < disp_kms->connectors_count; i++) {
237                 struct connector *connector = &disp_kms->connector[i];
239                 if (! connector->mode) {
240                         continue;
241                 }
243                 if (! disp_kms->current) {
244                         /* first buffer we flip to, setup the mode (since this can't
245                          * be done earlier without a buffer to scanout)
246                          */
247                         MSG("Setting mode %s on connector %d, crtc %d",
248                                         connector->mode_str, connector->id, connector->crtc);
250                         ret = drmModeSetCrtc(disp->fd, connector->crtc, buf_kms->fb_id,
251                                         x, 0, &connector->id, 1, connector->mode);
253                         x += connector->mode->hdisplay;
254                 } else {
255                         ret = drmModePageFlip(disp->fd, connector->crtc, buf_kms->fb_id,
256                                         DRM_MODE_PAGE_FLIP_EVENT, disp);
257                         disp_kms->scheduled_flips++;
258                 }
260                 if (ret) {
261                         ERROR("Could not post buffer on crtc %d: %s (%d)",
262                                         connector->crtc, strerror(errno), ret);
263                         last_err = ret;
264                         /* well, keep trying the reset of the connectors.. */
265                 }
266         }
268         /* if we flipped, wait for all flips to complete! */
269         while (disp_kms->scheduled_flips > disp_kms->completed_flips) {
270                 drmEventContext evctx = {
271                                 .version = DRM_EVENT_CONTEXT_VERSION,
272                                 .page_flip_handler = page_flip_handler,
273                 };
274                 struct timeval timeout = {
275                                 .tv_sec = 3,
276                                 .tv_usec = 0,
277                 };
278                 fd_set fds;
280                 FD_ZERO(&fds);
281                 FD_SET(disp->fd, &fds);
283                 ret = select(disp->fd + 1, &fds, NULL, NULL, &timeout);
284                 if (ret <= 0) {
285                         if (errno == EAGAIN) {
286                                 continue;    /* keep going */
287                         } else {
288                                 ERROR("Timeout waiting for flip complete: %s (%d)",
289                                                 strerror(errno), ret);
290                                 last_err = ret;
291                                 break;
292                         }
293                 }
295                 drmHandleEvent(disp->fd, &evctx);
296         }
298         disp_kms->current = buf;
300         return last_err;
303 static int
304 post_vid_buffer(struct display *disp, struct buffer *buf,
305                 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
307         struct display_kms *disp_kms = to_display_kms(disp);
308         struct buffer_kms *buf_kms = to_buffer_kms(buf);
309         int ret = 0;
310         uint32_t i, j;
312         /* ensure we have the overlay setup: */
313         for (i = 0; i < disp_kms->connectors_count; i++) {
314                 struct connector *connector = &disp_kms->connector[i];
315                 uint32_t used_planes = 0;
316                 drmModeModeInfo *mode = connector->mode;
318                 if (! mode) {
319                         continue;
320                 }
322                 if (! disp_kms->ovr[i]) {
324                         for (j = 0; j < disp_kms->plane_resources->count_planes; j++) {
325                                 drmModePlane *ovr = drmModeGetPlane(disp->fd,
326                                                 disp_kms->plane_resources->planes[j]);
327                                 if ((ovr->possible_crtcs & (1 << connector->pipe)) &&
328                                                 !(used_planes & (1 << j))) {
329                                         disp_kms->ovr[i] = ovr;
330                                         used_planes |= (1 << j);
331                                         break;
332                                 }
333                         }
334                 }
336                 if (! disp_kms->ovr[i]) {
337                         MSG("Could not find plane for crtc %d", connector->crtc);
338                         ret = -1;
339                         /* carry on and see if we can find at least one usable plane */
340                         continue;
341                 }
343                 ret = drmModeSetPlane(disp->fd, disp_kms->ovr[i]->plane_id,
344                                 connector->crtc, buf_kms->fb_id, 0,
345                                 /* make video fullscreen: */
346                                 0, 0, mode->hdisplay, mode->vdisplay,
347                                 /* source/cropping coordinates are given in Q16 */
348                                 x << 16, y << 16, w << 16, h << 16);
349                 if (ret) {
350                         ERROR("failed to enable plane %d: %s",
351                                         disp_kms->ovr[i]->plane_id, strerror(errno));
352                 }
353         }
355         return ret;
358 static void
359 connector_find_mode(struct display *disp, struct connector *c)
361         struct display_kms *disp_kms = to_display_kms(disp);
362         drmModeConnector *connector;
363         int i, j;
365         /* First, find the connector & mode */
366         c->mode = NULL;
367         for (i = 0; i < disp_kms->resources->count_connectors; i++) {
368                 connector = drmModeGetConnector(disp->fd,
369                                 disp_kms->resources->connectors[i]);
371                 if (!connector) {
372                         ERROR("could not get connector %i: %s",
373                                         disp_kms->resources->connectors[i], strerror(errno));
374                         drmModeFreeConnector(connector);
375                         continue;
376                 }
378                 if (!connector->count_modes) {
379                         drmModeFreeConnector(connector);
380                         continue;
381                 }
383                 if (connector->connector_id != c->id) {
384                         drmModeFreeConnector(connector);
385                         continue;
386                 }
388                 for (j = 0; j < connector->count_modes; j++) {
389                         c->mode = &connector->modes[j];
390                         if (!strcmp(c->mode->name, c->mode_str))
391                                 break;
392                 }
394                 /* Found it, break out */
395                 if (c->mode)
396                         break;
398                 drmModeFreeConnector(connector);
399         }
401         if (!c->mode) {
402                 ERROR("failed to find mode \"%s\"", c->mode_str);
403                 return;
404         }
406         /* Now get the encoder */
407         for (i = 0; i < disp_kms->resources->count_encoders; i++) {
408                 c->encoder = drmModeGetEncoder(disp->fd,
409                                 disp_kms->resources->encoders[i]);
411                 if (!c->encoder) {
412                         ERROR("could not get encoder %i: %s",
413                                         disp_kms->resources->encoders[i], strerror(errno));
414                         drmModeFreeEncoder(c->encoder);
415                         continue;
416                 }
418                 if (c->encoder->encoder_id  == connector->encoder_id)
419                         break;
421                 drmModeFreeEncoder(c->encoder);
422         }
424         if (c->crtc == -1)
425                 c->crtc = c->encoder->crtc_id;
427         /* and figure out which crtc index it is: */
428         for (i = 0; i < disp_kms->resources->count_crtcs; i++) {
429                 if (c->crtc == (int)disp_kms->resources->crtcs[i]) {
430                         c->pipe = i;
431                         break;
432                 }
433         }
436 void
437 disp_kms_usage(void)
439         MSG("KMS Display Options:");
440         MSG("\t-t <tiled-mode>\t8, 16, 32, or auto");
441         MSG("\t-s <connector_id>:<mode>\tset a mode");
442         MSG("\t-s <connector_id>@<crtc_id>:<mode>\tset a mode");
445 struct display *
446 disp_kms_open(int argc, char **argv)
448         struct display_kms *disp_kms = NULL;
449         struct display *disp;
450         int i;
452         disp_kms = calloc(1, sizeof(*disp_kms));
453         if (!disp_kms) {
454                 ERROR("allocation failed");
455                 goto fail;
456         }
457         disp = &disp_kms->base;
459         disp->fd = drmOpen("omapdrm", NULL);
460         if (disp->fd < 0) {
461                 ERROR("could not open drm device: %s (%d)", strerror(errno), errno);
462                 goto fail;
463         }
465         disp->dev = omap_device_new(disp->fd);
466         if (!disp->dev) {
467                 ERROR("couldn't create device");
468                 goto fail;
469         }
471         disp->get_buffers = get_buffers;
472         disp->get_vid_buffers = get_vid_buffers;
473         disp->post_buffer = post_buffer;
474         disp->post_vid_buffer = post_vid_buffer;
476         disp_kms->resources = drmModeGetResources(disp->fd);
477         if (!disp_kms->resources) {
478                 ERROR("drmModeGetResources failed: %s", strerror(errno));
479                 goto fail;
480         }
482         disp_kms->plane_resources = drmModeGetPlaneResources(disp->fd);
483         if (!disp_kms->plane_resources) {
484                 ERROR("drmModeGetPlaneResources failed: %s", strerror(errno));
485                 goto fail;
486         }
488         /* note: set args to NULL after we've parsed them so other modules know
489          * that it is already parsed (since the arg parsing is decentralized)
490          */
491         for (i = 1; i < argc; i++) {
492                 if (!argv[i]) {
493                         continue;
494                 }
495                 if (!strcmp("-t", argv[i])) {
496                         int n;
497                         argv[i++] = NULL;
498                         if (!strcmp(argv[i], "auto")) {
499                                 n = 0;
500                         } else if (sscanf(argv[i], "%d", &n) != 1) {
501                                 ERROR("invalid arg: %s", argv[i]);
502                                 goto fail;
503                         }
505                         disp_kms->bo_flags &= ~OMAP_BO_TILED;
507                         if (n == 8) {
508                                 disp_kms->bo_flags |= OMAP_BO_TILED_8;
509                         } else if (n == 16) {
510                                 disp_kms->bo_flags |= OMAP_BO_TILED_16;
511                         } else if (n == 32) {
512                                 disp_kms->bo_flags |= OMAP_BO_TILED_32;
513                         } else if (n == 0) {
514                                 disp_kms->bo_flags |= OMAP_BO_TILED;
515                         } else {
516                                 ERROR("invalid arg: %s", argv[i]);
517                                 goto fail;
518                         }
519                 } else if (!strcmp("-s", argv[i])) {
520                         struct connector *connector =
521                                         &disp_kms->connector[disp_kms->connectors_count++];
522                         connector->crtc = -1;
523                         argv[i++] = NULL;
524                         if (sscanf(argv[i], "%d:%64s",
525                                    &connector->id,
526                                    connector->mode_str) != 2 &&
527                             sscanf(argv[i], "%d@%d:%64s",
528                                    &connector->id,
529                                    &connector->crtc,
530                                    connector->mode_str) != 3) {
531                                 // TODO: we could support connector specified as a name too, I suppose
532                                 ERROR("invalid arg: %s", argv[i]);
533                                 goto fail;
534                         }
535                         disp_kms->bo_flags |= OMAP_BO_SCANOUT;
536                 } else {
537                         /* ignore */
538                         continue;
539                 }
540                 argv[i] = NULL;
541         }
543         disp->width = 0;
544         disp->height = 0;
545         for (i = 0; i < (int)disp_kms->connectors_count; i++) {
546                 struct connector *c = &disp_kms->connector[i];
547                 connector_find_mode(disp, c);
548                 if (c->mode == NULL)
549                         continue;
550                 /* setup side-by-side virtual display */
551                 disp->width += c->mode->hdisplay;
552                 if (disp->height < c->mode->vdisplay) {
553                         disp->height = c->mode->vdisplay;
554                 }
555         }
557         MSG("using %d connectors, %dx%d display",
558                         disp_kms->connectors_count, disp->width, disp->height);
560         return disp;
562 fail:
563         // XXX cleanup
564         return NULL;