ff4ca7d8eb081a01b152624fc10b0088f698aefd
[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"
23 #include <libdce.h>
25 #include <xf86drmMode.h>
28 /* NOTE: healthy dose of recycling from libdrm modetest app.. */
30 /*
31  * Mode setting with the kernel interfaces is a bit of a chore.
32  * First you have to find the connector in question and make sure the
33  * requested mode is available.
34  * Then you need to find the encoder attached to that connector so you
35  * can bind it with a free crtc.
36  */
37 struct connector {
38         uint32_t id;
39         char mode_str[64];
40         drmModeModeInfo *mode;
41         drmModeEncoder *encoder;
42         int crtc;
43         int pipe;
44 };
46 #define to_display_kms(x) container_of(x, struct display_kms, base)
47 struct display_kms {
48         struct display base;
50         uint32_t connectors_count;
51         struct connector connector[10];
52         drmModePlane *ovr[10];
54         int scheduled_flips, completed_flips;
55         uint32_t bo_flags;
56         drmModeResPtr resources;
57         drmModePlaneRes *plane_resources;
58         struct buffer *current;
59 };
61 #define to_buffer_kms(x) container_of(x, struct buffer_kms, base)
62 struct buffer_kms {
63         struct buffer base;
64         uint32_t fb_id;
65 };
67 static int global_fd = 0;
68 static uint32_t used_planes = 0;
70 static struct omap_bo *
71 alloc_bo(struct display *disp, uint32_t bpp, uint32_t width, uint32_t height,
72                 uint32_t *bo_handle, uint32_t *pitch)
73 {
74         struct display_kms *disp_kms = to_display_kms(disp);
75         struct omap_bo *bo;
76         uint32_t bo_flags = disp_kms->bo_flags;
78         if ((bo_flags & OMAP_BO_TILED) == OMAP_BO_TILED) {
79                 bo_flags &= ~OMAP_BO_TILED;
80                 if (bpp == 8) {
81                         bo_flags |= OMAP_BO_TILED_8;
82                 } else if (bpp == 16) {
83                         bo_flags |= OMAP_BO_TILED_16;
84                 } else if (bpp == 32) {
85                         bo_flags |= OMAP_BO_TILED_32;
86                 }
87         }
88         bo_flags |= OMAP_BO_WC;
90         if (bo_flags & OMAP_BO_TILED) {
91                 bo = omap_bo_new_tiled(disp->dev, ALIGN2(width,7), height, bo_flags);
92         } else {
93                 bo = omap_bo_new(disp->dev, width * height * bpp / 8, bo_flags);
94         }
96         if (bo) {
97                 *bo_handle = omap_bo_handle(bo);
98                 *pitch = width * bpp / 8;
99                 if (bo_flags & OMAP_BO_TILED)
100                         *pitch = ALIGN2(*pitch, PAGE_SHIFT);
101         }
103         return bo;
106 static struct buffer *
107 alloc_buffer(struct display *disp, uint32_t fourcc, uint32_t w, uint32_t h)
109         struct buffer_kms *buf_kms;
110         struct buffer *buf;
111         uint32_t bo_handles[4] = {0}, offsets[4] = {0};
112         int ret;
114         buf_kms = calloc(1, sizeof(*buf_kms));
115         if (!buf_kms) {
116                 ERROR("allocation failed");
117                 return NULL;
118         }
119         buf = &buf_kms->base;
121         buf->fourcc = fourcc;
122         buf->width = w;
123         buf->height = h;
124         buf->multiplanar = true;
126         buf->nbo = 1;
128         if (!fourcc)
129                 fourcc = FOURCC('A','R','2','4');
131         switch(fourcc) {
132         case FOURCC('A','R','2','4'):
133                 buf->nbo = 1;
134                 buf->bo[0] = alloc_bo(disp, 32, buf->width, buf->height,
135                                 &bo_handles[0], &buf->pitches[0]);
136                 break;
137         case FOURCC('U','Y','V','Y'):
138         case FOURCC('Y','U','Y','V'):
139                 buf->nbo = 1;
140                 buf->bo[0] = alloc_bo(disp, 16, buf->width, buf->height,
141                                 &bo_handles[0], &buf->pitches[0]);
142                 break;
143         case FOURCC('N','V','1','2'):
144                 if (disp->multiplanar) {
145                         buf->nbo = 2;
146                         buf->bo[0] = alloc_bo(disp, 8, buf->width, buf->height,
147                                         &bo_handles[0], &buf->pitches[0]);
148                         buf->fd[0] = omap_bo_dmabuf(buf->bo[0]);
149                         buf->bo[1] = alloc_bo(disp, 16, buf->width/2, buf->height/2,
150                                         &bo_handles[1], &buf->pitches[1]);
151                         buf->fd[1] = omap_bo_dmabuf(buf->bo[1]);
152                 } else {
153                         buf->nbo = 1;
154                         buf->bo[0] = alloc_bo(disp, 8, buf->width, buf->height * 3 / 2,
155                                         &bo_handles[0], &buf->pitches[0]);
156                         buf->fd[0] = omap_bo_dmabuf(buf->bo[0]);
157                         bo_handles[1] = bo_handles[0];
158                         buf->pitches[1] = buf->pitches[0];
159                         offsets[1] = buf->width * buf->height;
160                         buf->multiplanar = false;
161                 }
162                 break;
163         case FOURCC('I','4','2','0'):
164                 buf->nbo = 3;
165                 buf->bo[0] = alloc_bo(disp, 8, buf->width, buf->height,
166                                 &bo_handles[0], &buf->pitches[0]);
167                 buf->bo[1] = alloc_bo(disp, 8, buf->width/2, buf->height/2,
168                                 &bo_handles[1], &buf->pitches[1]);
169                 buf->bo[2] = alloc_bo(disp, 8, buf->width/2, buf->height/2,
170                                 &bo_handles[2], &buf->pitches[2]);
171                 break;
172         default:
173                 ERROR("invalid format: 0x%08x", fourcc);
174                 goto fail;
175         }
177         ret = drmModeAddFB2(disp->fd, buf->width, buf->height, fourcc,
178                         bo_handles, buf->pitches, offsets, &buf_kms->fb_id, 0);
179         if (ret) {
180                 ERROR("drmModeAddFB2 failed: %s (%d)", strerror(errno), ret);
181                 goto fail;
182         }
184         return buf;
186 fail:
187         // XXX cleanup
188         return NULL;
191 void
192 free_buffers(struct display *disp, uint32_t n)
194         uint32_t i;
195         for (i = 0; i < n; i++) {
196                 if (disp->buf[i]) {
197                         omap_bo_del(disp->buf[i]->bo[0]);
198                         if(disp->multiplanar){
199                                 omap_bo_del(disp->buf[i]->bo[1]);
200                         }
201                 }
202         }
203 free(disp->buf);
206 static struct buffer **
207 alloc_buffers(struct display *disp, uint32_t n,
208                 uint32_t fourcc, uint32_t w, uint32_t h)
210         struct buffer **bufs;
211         uint32_t i = 0;
213         bufs = calloc(n, sizeof(*bufs));
214         if (!bufs) {
215                 ERROR("allocation failed");
216                 goto fail;
217         }
219         for (i = 0; i < n; i++) {
220                 bufs[i] = alloc_buffer(disp, fourcc, w, h);
221                 if (!bufs[i]) {
222                         ERROR("allocation failed");
223                         goto fail;
224                 }
225         }
226         disp->buf=bufs;
227         return bufs;
229 fail:
230         // XXX cleanup
231         return NULL;
234 static struct buffer **
235 get_buffers(struct display *disp, uint32_t n)
237         return alloc_buffers(disp, n, 0, disp->width, disp->height);
240 static struct buffer **
241 get_vid_buffers(struct display *disp, uint32_t n,
242                 uint32_t fourcc, uint32_t w, uint32_t h)
244         return alloc_buffers(disp, n, fourcc, w, h);
247 static void
248 page_flip_handler(int fd, unsigned int frame,
249                 unsigned int sec, unsigned int usec, void *data)
251         struct display *disp = data;
252         struct display_kms *disp_kms = to_display_kms(disp);
254         disp_kms->completed_flips++;
256         MSG("Page flip: frame=%d, sec=%d, usec=%d, remaining=%d", frame, sec, usec,
257                         disp_kms->scheduled_flips - disp_kms->completed_flips);
260 static int
261 post_buffer(struct display *disp, struct buffer *buf)
263         struct display_kms *disp_kms = to_display_kms(disp);
264         struct buffer_kms *buf_kms = to_buffer_kms(buf);
265         int ret, last_err = 0, x = 0;
266         uint32_t i;
268         for (i = 0; i < disp_kms->connectors_count; i++) {
269                 struct connector *connector = &disp_kms->connector[i];
271                 if (! connector->mode) {
272                         continue;
273                 }
275                 if (! disp_kms->current) {
276                         /* first buffer we flip to, setup the mode (since this can't
277                          * be done earlier without a buffer to scanout)
278                          */
279                         MSG("Setting mode %s on connector %d, crtc %d",
280                                         connector->mode_str, connector->id, connector->crtc);
282                         ret = drmModeSetCrtc(disp->fd, connector->crtc, buf_kms->fb_id,
283                                         x, 0, &connector->id, 1, connector->mode);
285                         x += connector->mode->hdisplay;
286                 } else {
287                         ret = drmModePageFlip(disp->fd, connector->crtc, buf_kms->fb_id,
288                                         DRM_MODE_PAGE_FLIP_EVENT, disp);
289                         disp_kms->scheduled_flips++;
290                 }
292                 if (ret) {
293                         ERROR("Could not post buffer on crtc %d: %s (%d)",
294                                         connector->crtc, strerror(errno), ret);
295                         last_err = ret;
296                         /* well, keep trying the reset of the connectors.. */
297                 }
298         }
300         /* if we flipped, wait for all flips to complete! */
301         while (disp_kms->scheduled_flips > disp_kms->completed_flips) {
302                 drmEventContext evctx = {
303                                 .version = DRM_EVENT_CONTEXT_VERSION,
304                                 .page_flip_handler = page_flip_handler,
305                 };
306                 struct timeval timeout = {
307                                 .tv_sec = 3,
308                                 .tv_usec = 0,
309                 };
310                 fd_set fds;
312                 FD_ZERO(&fds);
313                 FD_SET(disp->fd, &fds);
315                 ret = select(disp->fd + 1, &fds, NULL, NULL, &timeout);
316                 if (ret <= 0) {
317                         if (errno == EAGAIN) {
318                                 continue;    /* keep going */
319                         } else {
320                                 ERROR("Timeout waiting for flip complete: %s (%d)",
321                                                 strerror(errno), ret);
322                                 last_err = ret;
323                                 break;
324                         }
325                 }
327                 drmHandleEvent(disp->fd, &evctx);
328         }
330         disp_kms->current = buf;
332         return last_err;
335 static int
336 post_vid_buffer(struct display *disp, struct buffer *buf,
337                 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
339         struct display_kms *disp_kms = to_display_kms(disp);
340         struct buffer_kms *buf_kms = to_buffer_kms(buf);
341         int ret = 0;
342         uint32_t i, j;
344         /* ensure we have the overlay setup: */
345         for (i = 0; i < disp_kms->connectors_count; i++) {
346                 struct connector *connector = &disp_kms->connector[i];
347                 drmModeModeInfo *mode = connector->mode;
349                 if (! mode) {
350                         continue;
351                 }
353                 if (! disp_kms->ovr[i]) {
355                         for (j = 0; j < disp_kms->plane_resources->count_planes; j++) {
356                                 if (used_planes & (1 << j)) {
357                                         continue;
358                                 }
359                                 drmModePlane *ovr = drmModeGetPlane(disp->fd,
360                                                 disp_kms->plane_resources->planes[j]);
361                                 if (ovr->possible_crtcs & (1 << connector->pipe)) {
362                                         disp_kms->ovr[i] = ovr;
363                                         used_planes |= (1 << j);
364                                         break;
365                                 }
366                         }
367                 }
369                 if (! disp_kms->ovr[i]) {
370                         MSG("Could not find plane for crtc %d", connector->crtc);
371                         ret = -1;
372                         /* carry on and see if we can find at least one usable plane */
373                         continue;
374                 }
376                 ret = drmModeSetPlane(disp->fd, disp_kms->ovr[i]->plane_id,
377                                 connector->crtc, buf_kms->fb_id, 0,
378                                 /* make video fullscreen: */
379                                 0, 0, mode->hdisplay, mode->vdisplay,
380                                 /* source/cropping coordinates are given in Q16 */
381                                 x << 16, y << 16, w << 16, h << 16);
382                 if (ret) {
383                         ERROR("failed to enable plane %d: %s",
384                                         disp_kms->ovr[i]->plane_id, strerror(errno));
385                 }
386         }
388         return ret;
391 static void
392 close_kms(struct display *disp)
396 static void
397 connector_find_mode(struct display *disp, struct connector *c)
399         struct display_kms *disp_kms = to_display_kms(disp);
400         drmModeConnector *connector;
401         int i, j;
403         /* First, find the connector & mode */
404         c->mode = NULL;
405         for (i = 0; i < disp_kms->resources->count_connectors; i++) {
406                 connector = drmModeGetConnector(disp->fd,
407                                 disp_kms->resources->connectors[i]);
409                 if (!connector) {
410                         ERROR("could not get connector %i: %s",
411                                         disp_kms->resources->connectors[i], strerror(errno));
412                         drmModeFreeConnector(connector);
413                         continue;
414                 }
416                 if (!connector->count_modes) {
417                         drmModeFreeConnector(connector);
418                         continue;
419                 }
421                 if (connector->connector_id != c->id) {
422                         drmModeFreeConnector(connector);
423                         continue;
424                 }
426                 for (j = 0; j < connector->count_modes; j++) {
427                         c->mode = &connector->modes[j];
428                         if (!strcmp(c->mode->name, c->mode_str))
429                                 break;
430                 }
432                 /* Found it, break out */
433                 if (c->mode)
434                         break;
436                 drmModeFreeConnector(connector);
437         }
439         if (!c->mode) {
440                 ERROR("failed to find mode \"%s\"", c->mode_str);
441                 return;
442         }
444         /* Now get the encoder */
445         for (i = 0; i < disp_kms->resources->count_encoders; i++) {
446                 c->encoder = drmModeGetEncoder(disp->fd,
447                                 disp_kms->resources->encoders[i]);
449                 if (!c->encoder) {
450                         ERROR("could not get encoder %i: %s",
451                                         disp_kms->resources->encoders[i], strerror(errno));
452                         drmModeFreeEncoder(c->encoder);
453                         continue;
454                 }
456                 if (c->encoder->encoder_id  == connector->encoder_id)
457                         break;
459                 drmModeFreeEncoder(c->encoder);
460         }
462         if (c->crtc == -1)
463                 c->crtc = c->encoder->crtc_id;
465         /* and figure out which crtc index it is: */
466         for (i = 0; i < disp_kms->resources->count_crtcs; i++) {
467                 if (c->crtc == (int)disp_kms->resources->crtcs[i]) {
468                         c->pipe = i;
469                         break;
470                 }
471         }
474 void
475 disp_kms_usage(void)
477         MSG("KMS Display Options:");
478         MSG("\t-1 \t\tforce single-plane buffers");
479         MSG("\t-t <tiled-mode>\t8, 16, 32, or auto");
480         MSG("\t-s <connector_id>:<mode>\tset a mode");
481         MSG("\t-s <connector_id>@<crtc_id>:<mode>\tset a mode");
484 struct display *
485 disp_kms_open(int argc, char **argv)
487         struct display_kms *disp_kms = NULL;
488         struct display *disp;
489         int i;
491         disp_kms = calloc(1, sizeof(*disp_kms));
492         if (!disp_kms) {
493                 ERROR("allocation failed");
494                 goto fail;
495         }
496         disp = &disp_kms->base;
498         if (!global_fd) {
499                 global_fd = drmOpen("omapdrm", NULL);
500                 if (global_fd < 0) {
501                         ERROR("could not open drm device: %s (%d)", strerror(errno), errno);
502                         goto fail;
503                 }
504         }
506         disp->fd = global_fd;
508         disp->dev = omap_device_new(disp->fd);
509         if (!disp->dev) {
510                 ERROR("couldn't create device");
511                 goto fail;
512         }
514         disp->get_buffers = get_buffers;
515         disp->get_vid_buffers = get_vid_buffers;
516         disp->post_buffer = post_buffer;
517         disp->post_vid_buffer = post_vid_buffer;
518         disp->close = close_kms;
519         disp->disp_free_buf = free_buffers ;
520         disp_kms->resources = drmModeGetResources(disp->fd);
521         if (!disp_kms->resources) {
522                 ERROR("drmModeGetResources failed: %s", strerror(errno));
523                 goto fail;
524         }
526         disp_kms->plane_resources = drmModeGetPlaneResources(disp->fd);
527         if (!disp_kms->plane_resources) {
528                 ERROR("drmModeGetPlaneResources failed: %s", strerror(errno));
529                 goto fail;
530         }
532         disp->multiplanar = true;
534         /* note: set args to NULL after we've parsed them so other modules know
535          * that it is already parsed (since the arg parsing is decentralized)
536          */
537         for (i = 1; i < argc; i++) {
538                 if (!argv[i]) {
539                         continue;
540                 }
541                 if (!strcmp("-1", argv[i])) {
542                         disp->multiplanar = false;
543                 } else if (!strcmp("-t", argv[i])) {
544                         int n;
545                         argv[i++] = NULL;
546                         if (!strcmp(argv[i], "auto")) {
547                                 n = 0;
548                         } else if (sscanf(argv[i], "%d", &n) != 1) {
549                                 ERROR("invalid arg: %s", argv[i]);
550                                 goto fail;
551                         }
553                         disp_kms->bo_flags &= ~OMAP_BO_TILED;
555                         if (n == 8) {
556                                 disp_kms->bo_flags |= OMAP_BO_TILED_8;
557                         } else if (n == 16) {
558                                 disp_kms->bo_flags |= OMAP_BO_TILED_16;
559                         } else if (n == 32) {
560                                 disp_kms->bo_flags |= OMAP_BO_TILED_32;
561                         } else if (n == 0) {
562                                 disp_kms->bo_flags |= OMAP_BO_TILED;
563                         } else {
564                                 ERROR("invalid arg: %s", argv[i]);
565                                 goto fail;
566                         }
567                 } else if (!strcmp("-s", argv[i])) {
568                         struct connector *connector =
569                                         &disp_kms->connector[disp_kms->connectors_count++];
570                         connector->crtc = -1;
571                         argv[i++] = NULL;
572                         if (sscanf(argv[i], "%d:%64s",
573                                    &connector->id,
574                                    connector->mode_str) != 2 &&
575                             sscanf(argv[i], "%d@%d:%64s",
576                                    &connector->id,
577                                    &connector->crtc,
578                                    connector->mode_str) != 3) {
579                                 // TODO: we could support connector specified as a name too, I suppose
580                                 ERROR("invalid arg: %s", argv[i]);
581                                 goto fail;
582                         }
583                         disp_kms->bo_flags |= OMAP_BO_SCANOUT;
584                 } else {
585                         /* ignore */
586                         continue;
587                 }
588                 argv[i] = NULL;
589         }
591         disp->width = 0;
592         disp->height = 0;
593         for (i = 0; i < (int)disp_kms->connectors_count; i++) {
594                 struct connector *c = &disp_kms->connector[i];
595                 connector_find_mode(disp, c);
596                 if (c->mode == NULL)
597                         continue;
598                 /* setup side-by-side virtual display */
599                 disp->width += c->mode->hdisplay;
600                 if (disp->height < c->mode->vdisplay) {
601                         disp->height = c->mode->vdisplay;
602                 }
603         }
605         MSG("using %d connectors, %dx%d display, multiplanar: %d",
606                         disp_kms->connectors_count, disp->width, disp->height, disp->multiplanar);
608         return disp;
610 fail:
611         // XXX cleanup
612         return NULL;