c0730bc55ae04fcdcf13d0c9902247383109be78
[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;
69 static int ndisplays = 0;
71 static struct omap_bo *
72 alloc_bo(struct display *disp, uint32_t bpp, uint32_t width, uint32_t height,
73                 uint32_t *bo_handle, uint32_t *pitch)
74 {
75         struct display_kms *disp_kms = to_display_kms(disp);
76         struct omap_bo *bo;
77         uint32_t bo_flags = disp_kms->bo_flags;
79         if ((bo_flags & OMAP_BO_TILED) == OMAP_BO_TILED) {
80                 bo_flags &= ~OMAP_BO_TILED;
81                 if (bpp == 8) {
82                         bo_flags |= OMAP_BO_TILED_8;
83                 } else if (bpp == 16) {
84                         bo_flags |= OMAP_BO_TILED_16;
85                 } else if (bpp == 32) {
86                         bo_flags |= OMAP_BO_TILED_32;
87                 }
88         }
89         bo_flags |= OMAP_BO_WC;
91         if (bo_flags & OMAP_BO_TILED) {
92                 bo = omap_bo_new_tiled(disp->dev, ALIGN2(width,7), height, bo_flags);
93         } else {
94                 bo = omap_bo_new(disp->dev, width * height * bpp / 8, bo_flags);
95         }
97         if (bo) {
98                 *bo_handle = omap_bo_handle(bo);
99                 *pitch = width * bpp / 8;
100                 if (bo_flags & OMAP_BO_TILED)
101                         *pitch = ALIGN2(*pitch, PAGE_SHIFT);
102         }
104         return bo;
107 static struct buffer *
108 alloc_buffer(struct display *disp, uint32_t fourcc, uint32_t w, uint32_t h)
110         struct buffer_kms *buf_kms;
111         struct buffer *buf;
112         uint32_t bo_handles[4] = {0}, offsets[4] = {0};
113         int ret;
115         buf_kms = calloc(1, sizeof(*buf_kms));
116         if (!buf_kms) {
117                 ERROR("allocation failed");
118                 return NULL;
119         }
120         buf = &buf_kms->base;
122         buf->fourcc = fourcc;
123         buf->width = w;
124         buf->height = h;
125         buf->multiplanar = true;
127         buf->nbo = 1;
129         if (!fourcc)
130                 fourcc = FOURCC('A','R','2','4');
132         switch(fourcc) {
133         case FOURCC('A','R','2','4'):
134                 buf->nbo = 1;
135                 buf->bo[0] = alloc_bo(disp, 32, buf->width, buf->height,
136                                 &bo_handles[0], &buf->pitches[0]);
137                 break;
138         case FOURCC('U','Y','V','Y'):
139         case FOURCC('Y','U','Y','V'):
140                 buf->nbo = 1;
141                 buf->bo[0] = alloc_bo(disp, 16, buf->width, buf->height,
142                                 &bo_handles[0], &buf->pitches[0]);
143                 break;
144         case FOURCC('N','V','1','2'):
145                 if (disp->multiplanar) {
146                         buf->nbo = 2;
147                         buf->bo[0] = alloc_bo(disp, 8, buf->width, buf->height,
148                                         &bo_handles[0], &buf->pitches[0]);
149                         buf->fd[0] = omap_bo_dmabuf(buf->bo[0]);
150                         buf->bo[1] = alloc_bo(disp, 16, buf->width/2, buf->height/2,
151                                         &bo_handles[1], &buf->pitches[1]);
152                         buf->fd[1] = omap_bo_dmabuf(buf->bo[1]);
153                 } else {
154                         buf->nbo = 1;
155                         buf->bo[0] = alloc_bo(disp, 8, buf->width, buf->height * 3 / 2,
156                                         &bo_handles[0], &buf->pitches[0]);
157                         buf->fd[0] = omap_bo_dmabuf(buf->bo[0]);
158                         bo_handles[1] = bo_handles[0];
159                         buf->pitches[1] = buf->pitches[0];
160                         offsets[1] = buf->width * buf->height;
161                         buf->multiplanar = false;
162                 }
163                 break;
164         case FOURCC('I','4','2','0'):
165                 buf->nbo = 3;
166                 buf->bo[0] = alloc_bo(disp, 8, buf->width, buf->height,
167                                 &bo_handles[0], &buf->pitches[0]);
168                 buf->bo[1] = alloc_bo(disp, 8, buf->width/2, buf->height/2,
169                                 &bo_handles[1], &buf->pitches[1]);
170                 buf->bo[2] = alloc_bo(disp, 8, buf->width/2, buf->height/2,
171                                 &bo_handles[2], &buf->pitches[2]);
172                 break;
173         default:
174                 ERROR("invalid format: 0x%08x", fourcc);
175                 goto fail;
176         }
178         ret = drmModeAddFB2(disp->fd, buf->width, buf->height, fourcc,
179                         bo_handles, buf->pitches, offsets, &buf_kms->fb_id, 0);
180         if (ret) {
181                 ERROR("drmModeAddFB2 failed: %s (%d)", strerror(errno), ret);
182                 goto fail;
183         }
185         return buf;
187 fail:
188         // XXX cleanup
189         return NULL;
192 void
193 free_buffers(struct display *disp, uint32_t n)
195         uint32_t i;
196         for (i = 0; i < n; i++) {
197                 if (disp->buf[i]) {
198                         omap_bo_del(disp->buf[i]->bo[0]);
199                         if(disp->multiplanar){
200                                 omap_bo_del(disp->buf[i]->bo[1]);
201                         }
202                 }
203         }
204 free(disp->buf);
207 static struct buffer **
208 alloc_buffers(struct display *disp, uint32_t n,
209                 uint32_t fourcc, uint32_t w, uint32_t h)
211         struct buffer **bufs;
212         uint32_t i = 0;
214         bufs = calloc(n, sizeof(*bufs));
215         if (!bufs) {
216                 ERROR("allocation failed");
217                 goto fail;
218         }
220         for (i = 0; i < n; i++) {
221                 bufs[i] = alloc_buffer(disp, fourcc, w, h);
222                 if (!bufs[i]) {
223                         ERROR("allocation failed");
224                         goto fail;
225                 }
226         }
227         disp->buf=bufs;
228         return bufs;
230 fail:
231         // XXX cleanup
232         return NULL;
235 static struct buffer **
236 get_buffers(struct display *disp, uint32_t n)
238         return alloc_buffers(disp, n, 0, disp->width, disp->height);
241 static struct buffer **
242 get_vid_buffers(struct display *disp, uint32_t n,
243                 uint32_t fourcc, uint32_t w, uint32_t h)
245         return alloc_buffers(disp, n, fourcc, w, h);
248 static void
249 page_flip_handler(int fd, unsigned int frame,
250                 unsigned int sec, unsigned int usec, void *data)
252         struct display *disp = data;
253         struct display_kms *disp_kms = to_display_kms(disp);
255         disp_kms->completed_flips++;
257         MSG("Page flip: frame=%d, sec=%d, usec=%d, remaining=%d", frame, sec, usec,
258                         disp_kms->scheduled_flips - disp_kms->completed_flips);
261 static int
262 post_buffer(struct display *disp, struct buffer *buf)
264         struct display_kms *disp_kms = to_display_kms(disp);
265         struct buffer_kms *buf_kms = to_buffer_kms(buf);
266         int ret, last_err = 0, x = 0;
267         uint32_t i;
269         for (i = 0; i < disp_kms->connectors_count; i++) {
270                 struct connector *connector = &disp_kms->connector[i];
272                 if (! connector->mode) {
273                         continue;
274                 }
276                 if (! disp_kms->current) {
277                         /* first buffer we flip to, setup the mode (since this can't
278                          * be done earlier without a buffer to scanout)
279                          */
280                         MSG("Setting mode %s on connector %d, crtc %d",
281                                         connector->mode_str, connector->id, connector->crtc);
283                         ret = drmModeSetCrtc(disp->fd, connector->crtc, buf_kms->fb_id,
284                                         x, 0, &connector->id, 1, connector->mode);
286                         x += connector->mode->hdisplay;
287                 } else {
288                         ret = drmModePageFlip(disp->fd, connector->crtc, buf_kms->fb_id,
289                                         DRM_MODE_PAGE_FLIP_EVENT, disp);
290                         disp_kms->scheduled_flips++;
291                 }
293                 if (ret) {
294                         ERROR("Could not post buffer on crtc %d: %s (%d)",
295                                         connector->crtc, strerror(errno), ret);
296                         last_err = ret;
297                         /* well, keep trying the reset of the connectors.. */
298                 }
299         }
301         /* if we flipped, wait for all flips to complete! */
302         while (disp_kms->scheduled_flips > disp_kms->completed_flips) {
303                 drmEventContext evctx = {
304                                 .version = DRM_EVENT_CONTEXT_VERSION,
305                                 .page_flip_handler = page_flip_handler,
306                 };
307                 struct timeval timeout = {
308                                 .tv_sec = 3,
309                                 .tv_usec = 0,
310                 };
311                 fd_set fds;
313                 FD_ZERO(&fds);
314                 FD_SET(disp->fd, &fds);
316                 ret = select(disp->fd + 1, &fds, NULL, NULL, &timeout);
317                 if (ret <= 0) {
318                         if (errno == EAGAIN) {
319                                 continue;    /* keep going */
320                         } else {
321                                 ERROR("Timeout waiting for flip complete: %s (%d)",
322                                                 strerror(errno), ret);
323                                 last_err = ret;
324                                 break;
325                         }
326                 }
328                 drmHandleEvent(disp->fd, &evctx);
329         }
331         disp_kms->current = buf;
333         return last_err;
336 int
337 get_overlay_plane(struct display *disp, struct buffer *buf)
339         struct display_kms *disp_kms = to_display_kms(disp);
340         uint32_t i, ret;
342         for (i = 0; i < disp_kms->connectors_count; i++) {
343                 struct connector *connector = &disp_kms->connector[i];
344                 drmModeModeInfo *mode = connector->mode;
346                 disp_kms->ovr[i] = drmModeGetPlane(disp->fd,
347                                         disp_kms->plane_resources->planes[1]);
349                 ret = drmModeObjectSetProperty(disp->fd,
350                         disp_kms->ovr[i]->plane_id, DRM_MODE_OBJECT_PLANE, 7, 3);
351                 if (ret < 0) {
352                         MSG("Could not set Z order for plane");
353                         return ret;
354                 }
355         }
356         return 0;
359 static int
360 post_vid_buffer(struct display *disp, struct buffer *buf,
361                 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
363         struct display_kms *disp_kms = to_display_kms(disp);
364         struct buffer_kms *buf_kms = to_buffer_kms(buf);
365         int ret = 0;
366         uint32_t i, j;
368         /* ensure we have the overlay setup: */
369         for (i = 0; i < disp_kms->connectors_count; i++) {
370                 struct connector *connector = &disp_kms->connector[i];
371                 drmModeModeInfo *mode = connector->mode;
373                 if (! mode) {
374                         continue;
375                 }
377                 if (! disp_kms->ovr[i]) {
379                         for (j = 0; j < disp_kms->plane_resources->count_planes; j++) {
380                                 if (used_planes & (1 << j)) {
381                                         continue;
382                                 }
383                                 drmModePlane *ovr = drmModeGetPlane(disp->fd,
384                                                 disp_kms->plane_resources->planes[j]);
385                                 if (ovr->possible_crtcs & (1 << connector->pipe)) {
386                                         disp_kms->ovr[i] = ovr;
387                                         used_planes |= (1 << j);
388                                         break;
389                                 }
390                         }
391                 }
393                 if (! disp_kms->ovr[i]) {
394                         MSG("Could not find plane for crtc %d", connector->crtc);
395                         ret = -1;
396                         /* carry on and see if we can find at least one usable plane */
397                         continue;
398                 }
400                 if(buf->noScale) {
401                         ret = drmModeSetPlane(disp->fd, disp_kms->ovr[i]->plane_id,
402                                 connector->crtc, buf_kms->fb_id, 0,
403                                 /* Use x and y as co-ordinates of overlay */
404                                 x, y, buf->width, buf->height,
405                                 /* Consider source x and y is 0 always */
406                                 0, 0, w << 16, h << 16);
407                 } else {
408                         ret = drmModeSetPlane(disp->fd, disp_kms->ovr[i]->plane_id,
409                                 connector->crtc, buf_kms->fb_id, 0,
410                                 /* make video fullscreen: */
411                                 0, 0, mode->hdisplay, mode->vdisplay,
412                                 /* source/cropping coordinates are given in Q16 */
413                                 x << 16, y << 16, w << 16, h << 16);
414                 }
415                 if (ret) {
416                         ERROR("failed to enable plane %d: %s",
417                                         disp_kms->ovr[i]->plane_id, strerror(errno));
418                 }
419         }
421         return ret;
424 static void
425 close_kms(struct display *disp)
427         omap_device_del(disp->dev);
428         disp->dev = NULL;
429         if (used_planes) {
430                 used_planes >>= 1;
431         }
432         if (--ndisplays == 0) {
433                 close(global_fd);
434         }
437 static void
438 connector_find_mode(struct display *disp, struct connector *c)
440         struct display_kms *disp_kms = to_display_kms(disp);
441         drmModeConnector *connector;
442         int i, j;
444         /* First, find the connector & mode */
445         c->mode = NULL;
446         for (i = 0; i < disp_kms->resources->count_connectors; i++) {
447                 connector = drmModeGetConnector(disp->fd,
448                                 disp_kms->resources->connectors[i]);
450                 if (!connector) {
451                         ERROR("could not get connector %i: %s",
452                                         disp_kms->resources->connectors[i], strerror(errno));
453                         drmModeFreeConnector(connector);
454                         continue;
455                 }
457                 if (!connector->count_modes) {
458                         drmModeFreeConnector(connector);
459                         continue;
460                 }
462                 if (connector->connector_id != c->id) {
463                         drmModeFreeConnector(connector);
464                         continue;
465                 }
467                 for (j = 0; j < connector->count_modes; j++) {
468                         c->mode = &connector->modes[j];
469                         if (!strcmp(c->mode->name, c->mode_str))
470                                 break;
471                 }
473                 /* Found it, break out */
474                 if (c->mode)
475                         break;
477                 drmModeFreeConnector(connector);
478         }
480         if (!c->mode) {
481                 ERROR("failed to find mode \"%s\"", c->mode_str);
482                 return;
483         }
485         /* Now get the encoder */
486         for (i = 0; i < disp_kms->resources->count_encoders; i++) {
487                 c->encoder = drmModeGetEncoder(disp->fd,
488                                 disp_kms->resources->encoders[i]);
490                 if (!c->encoder) {
491                         ERROR("could not get encoder %i: %s",
492                                         disp_kms->resources->encoders[i], strerror(errno));
493                         drmModeFreeEncoder(c->encoder);
494                         continue;
495                 }
497                 if (c->encoder->encoder_id  == connector->encoder_id)
498                         break;
500                 drmModeFreeEncoder(c->encoder);
501         }
503         if (c->crtc == -1)
504                 c->crtc = c->encoder->crtc_id;
506         /* and figure out which crtc index it is: */
507         for (i = 0; i < disp_kms->resources->count_crtcs; i++) {
508                 if (c->crtc == (int)disp_kms->resources->crtcs[i]) {
509                         c->pipe = i;
510                         break;
511                 }
512         }
515 void
516 disp_kms_usage(void)
518         MSG("KMS Display Options:");
519         MSG("\t-1 \t\tforce single-plane buffers");
520         MSG("\t-t <tiled-mode>\t8, 16, 32, or auto");
521         MSG("\t-s <connector_id>:<mode>\tset a mode");
522         MSG("\t-s <connector_id>@<crtc_id>:<mode>\tset a mode");
525 struct display *
526 disp_kms_open(int argc, char **argv)
528         struct display_kms *disp_kms = NULL;
529         struct display *disp;
530         int i;
532         disp_kms = calloc(1, sizeof(*disp_kms));
533         if (!disp_kms) {
534                 ERROR("allocation failed");
535                 goto fail;
536         }
537         disp = &disp_kms->base;
539         if (!global_fd) {
540                 global_fd = drmOpen("omapdrm", NULL);
541                 if (global_fd < 0) {
542                         ERROR("could not open drm device: %s (%d)", strerror(errno), errno);
543                         goto fail;
544                 }
545         }
547         disp->fd = global_fd;
548         ndisplays++;  /* increment the number of displays counter */
550         disp->dev = omap_device_new(disp->fd);
551         if (!disp->dev) {
552                 ERROR("couldn't create device");
553                 goto fail;
554         }
556         disp->get_buffers = get_buffers;
557         disp->get_vid_buffers = get_vid_buffers;
558         disp->post_buffer = post_buffer;
559         disp->post_vid_buffer = post_vid_buffer;
560         disp->close = close_kms;
561         disp->disp_free_buf = free_buffers ;
562         disp_kms->resources = drmModeGetResources(disp->fd);
563         if (!disp_kms->resources) {
564                 ERROR("drmModeGetResources failed: %s", strerror(errno));
565                 goto fail;
566         }
568         disp_kms->plane_resources = drmModeGetPlaneResources(disp->fd);
569         if (!disp_kms->plane_resources) {
570                 ERROR("drmModeGetPlaneResources failed: %s", strerror(errno));
571                 goto fail;
572         }
574         disp->multiplanar = true;
576         /* note: set args to NULL after we've parsed them so other modules know
577          * that it is already parsed (since the arg parsing is decentralized)
578          */
579         for (i = 1; i < argc; i++) {
580                 if (!argv[i]) {
581                         continue;
582                 }
583                 if (!strcmp("-1", argv[i])) {
584                         disp->multiplanar = false;
585                 } else if (!strcmp("-t", argv[i])) {
586                         int n;
587                         argv[i++] = NULL;
588                         if (!strcmp(argv[i], "auto")) {
589                                 n = 0;
590                         } else if (sscanf(argv[i], "%d", &n) != 1) {
591                                 ERROR("invalid arg: %s", argv[i]);
592                                 goto fail;
593                         }
595                         disp_kms->bo_flags &= ~OMAP_BO_TILED;
597                         if (n == 8) {
598                                 disp_kms->bo_flags |= OMAP_BO_TILED_8;
599                         } else if (n == 16) {
600                                 disp_kms->bo_flags |= OMAP_BO_TILED_16;
601                         } else if (n == 32) {
602                                 disp_kms->bo_flags |= OMAP_BO_TILED_32;
603                         } else if (n == 0) {
604                                 disp_kms->bo_flags |= OMAP_BO_TILED;
605                         } else {
606                                 ERROR("invalid arg: %s", argv[i]);
607                                 goto fail;
608                         }
609                 } else if (!strcmp("-s", argv[i])) {
610                         struct connector *connector =
611                                         &disp_kms->connector[disp_kms->connectors_count++];
612                         connector->crtc = -1;
613                         argv[i++] = NULL;
614                         if (sscanf(argv[i], "%d:%64s",
615                                    &connector->id,
616                                    connector->mode_str) != 2 &&
617                             sscanf(argv[i], "%d@%d:%64s",
618                                    &connector->id,
619                                    &connector->crtc,
620                                    connector->mode_str) != 3) {
621                                 // TODO: we could support connector specified as a name too, I suppose
622                                 ERROR("invalid arg: %s", argv[i]);
623                                 goto fail;
624                         }
625                         disp_kms->bo_flags |= OMAP_BO_SCANOUT;
626                 } else {
627                         /* ignore */
628                         continue;
629                 }
630                 argv[i] = NULL;
631         }
633         disp->width = 0;
634         disp->height = 0;
635         for (i = 0; i < (int)disp_kms->connectors_count; i++) {
636                 struct connector *c = &disp_kms->connector[i];
637                 connector_find_mode(disp, c);
638                 if (c->mode == NULL)
639                         continue;
640                 /* setup side-by-side virtual display */
641                 disp->width += c->mode->hdisplay;
642                 if (disp->height < c->mode->vdisplay) {
643                         disp->height = c->mode->vdisplay;
644                 }
645         }
647         MSG("using %d connectors, %dx%d display, multiplanar: %d",
648                         disp_kms->connectors_count, disp->width, disp->height, disp->multiplanar);
650         return disp;
652 fail:
653         // XXX cleanup
654         return NULL;