disp-kms: Add support for deletion of the devices
[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 static int
337 post_vid_buffer(struct display *disp, struct buffer *buf,
338                 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
340         struct display_kms *disp_kms = to_display_kms(disp);
341         struct buffer_kms *buf_kms = to_buffer_kms(buf);
342         int ret = 0;
343         uint32_t i, j;
345         /* ensure we have the overlay setup: */
346         for (i = 0; i < disp_kms->connectors_count; i++) {
347                 struct connector *connector = &disp_kms->connector[i];
348                 drmModeModeInfo *mode = connector->mode;
350                 if (! mode) {
351                         continue;
352                 }
354                 if (! disp_kms->ovr[i]) {
356                         for (j = 0; j < disp_kms->plane_resources->count_planes; j++) {
357                                 if (used_planes & (1 << j)) {
358                                         continue;
359                                 }
360                                 drmModePlane *ovr = drmModeGetPlane(disp->fd,
361                                                 disp_kms->plane_resources->planes[j]);
362                                 if (ovr->possible_crtcs & (1 << connector->pipe)) {
363                                         disp_kms->ovr[i] = ovr;
364                                         used_planes |= (1 << j);
365                                         break;
366                                 }
367                         }
368                 }
370                 if (! disp_kms->ovr[i]) {
371                         MSG("Could not find plane for crtc %d", connector->crtc);
372                         ret = -1;
373                         /* carry on and see if we can find at least one usable plane */
374                         continue;
375                 }
377                 ret = drmModeSetPlane(disp->fd, disp_kms->ovr[i]->plane_id,
378                                 connector->crtc, buf_kms->fb_id, 0,
379                                 /* make video fullscreen: */
380                                 0, 0, mode->hdisplay, mode->vdisplay,
381                                 /* source/cropping coordinates are given in Q16 */
382                                 x << 16, y << 16, w << 16, h << 16);
383                 if (ret) {
384                         ERROR("failed to enable plane %d: %s",
385                                         disp_kms->ovr[i]->plane_id, strerror(errno));
386                 }
387         }
389         return ret;
392 static void
393 close_kms(struct display *disp)
395         omap_device_del(disp->dev);
396         disp->dev = NULL;
397         if (used_planes) {
398                 used_planes >>= 1;
399         }
400         if (--ndisplays == 0) {
401                 close(global_fd);
402         }
405 static void
406 connector_find_mode(struct display *disp, struct connector *c)
408         struct display_kms *disp_kms = to_display_kms(disp);
409         drmModeConnector *connector;
410         int i, j;
412         /* First, find the connector & mode */
413         c->mode = NULL;
414         for (i = 0; i < disp_kms->resources->count_connectors; i++) {
415                 connector = drmModeGetConnector(disp->fd,
416                                 disp_kms->resources->connectors[i]);
418                 if (!connector) {
419                         ERROR("could not get connector %i: %s",
420                                         disp_kms->resources->connectors[i], strerror(errno));
421                         drmModeFreeConnector(connector);
422                         continue;
423                 }
425                 if (!connector->count_modes) {
426                         drmModeFreeConnector(connector);
427                         continue;
428                 }
430                 if (connector->connector_id != c->id) {
431                         drmModeFreeConnector(connector);
432                         continue;
433                 }
435                 for (j = 0; j < connector->count_modes; j++) {
436                         c->mode = &connector->modes[j];
437                         if (!strcmp(c->mode->name, c->mode_str))
438                                 break;
439                 }
441                 /* Found it, break out */
442                 if (c->mode)
443                         break;
445                 drmModeFreeConnector(connector);
446         }
448         if (!c->mode) {
449                 ERROR("failed to find mode \"%s\"", c->mode_str);
450                 return;
451         }
453         /* Now get the encoder */
454         for (i = 0; i < disp_kms->resources->count_encoders; i++) {
455                 c->encoder = drmModeGetEncoder(disp->fd,
456                                 disp_kms->resources->encoders[i]);
458                 if (!c->encoder) {
459                         ERROR("could not get encoder %i: %s",
460                                         disp_kms->resources->encoders[i], strerror(errno));
461                         drmModeFreeEncoder(c->encoder);
462                         continue;
463                 }
465                 if (c->encoder->encoder_id  == connector->encoder_id)
466                         break;
468                 drmModeFreeEncoder(c->encoder);
469         }
471         if (c->crtc == -1)
472                 c->crtc = c->encoder->crtc_id;
474         /* and figure out which crtc index it is: */
475         for (i = 0; i < disp_kms->resources->count_crtcs; i++) {
476                 if (c->crtc == (int)disp_kms->resources->crtcs[i]) {
477                         c->pipe = i;
478                         break;
479                 }
480         }
483 void
484 disp_kms_usage(void)
486         MSG("KMS Display Options:");
487         MSG("\t-1 \t\tforce single-plane buffers");
488         MSG("\t-t <tiled-mode>\t8, 16, 32, or auto");
489         MSG("\t-s <connector_id>:<mode>\tset a mode");
490         MSG("\t-s <connector_id>@<crtc_id>:<mode>\tset a mode");
493 struct display *
494 disp_kms_open(int argc, char **argv)
496         struct display_kms *disp_kms = NULL;
497         struct display *disp;
498         int i;
500         disp_kms = calloc(1, sizeof(*disp_kms));
501         if (!disp_kms) {
502                 ERROR("allocation failed");
503                 goto fail;
504         }
505         disp = &disp_kms->base;
507         if (!global_fd) {
508                 global_fd = drmOpen("omapdrm", NULL);
509                 if (global_fd < 0) {
510                         ERROR("could not open drm device: %s (%d)", strerror(errno), errno);
511                         goto fail;
512                 }
513         }
515         disp->fd = global_fd;
516         ndisplays++;  /* increment the number of displays counter */
518         disp->dev = omap_device_new(disp->fd);
519         if (!disp->dev) {
520                 ERROR("couldn't create device");
521                 goto fail;
522         }
524         disp->get_buffers = get_buffers;
525         disp->get_vid_buffers = get_vid_buffers;
526         disp->post_buffer = post_buffer;
527         disp->post_vid_buffer = post_vid_buffer;
528         disp->close = close_kms;
529         disp->disp_free_buf = free_buffers ;
530         disp_kms->resources = drmModeGetResources(disp->fd);
531         if (!disp_kms->resources) {
532                 ERROR("drmModeGetResources failed: %s", strerror(errno));
533                 goto fail;
534         }
536         disp_kms->plane_resources = drmModeGetPlaneResources(disp->fd);
537         if (!disp_kms->plane_resources) {
538                 ERROR("drmModeGetPlaneResources failed: %s", strerror(errno));
539                 goto fail;
540         }
542         disp->multiplanar = true;
544         /* note: set args to NULL after we've parsed them so other modules know
545          * that it is already parsed (since the arg parsing is decentralized)
546          */
547         for (i = 1; i < argc; i++) {
548                 if (!argv[i]) {
549                         continue;
550                 }
551                 if (!strcmp("-1", argv[i])) {
552                         disp->multiplanar = false;
553                 } else if (!strcmp("-t", argv[i])) {
554                         int n;
555                         argv[i++] = NULL;
556                         if (!strcmp(argv[i], "auto")) {
557                                 n = 0;
558                         } else if (sscanf(argv[i], "%d", &n) != 1) {
559                                 ERROR("invalid arg: %s", argv[i]);
560                                 goto fail;
561                         }
563                         disp_kms->bo_flags &= ~OMAP_BO_TILED;
565                         if (n == 8) {
566                                 disp_kms->bo_flags |= OMAP_BO_TILED_8;
567                         } else if (n == 16) {
568                                 disp_kms->bo_flags |= OMAP_BO_TILED_16;
569                         } else if (n == 32) {
570                                 disp_kms->bo_flags |= OMAP_BO_TILED_32;
571                         } else if (n == 0) {
572                                 disp_kms->bo_flags |= OMAP_BO_TILED;
573                         } else {
574                                 ERROR("invalid arg: %s", argv[i]);
575                                 goto fail;
576                         }
577                 } else if (!strcmp("-s", argv[i])) {
578                         struct connector *connector =
579                                         &disp_kms->connector[disp_kms->connectors_count++];
580                         connector->crtc = -1;
581                         argv[i++] = NULL;
582                         if (sscanf(argv[i], "%d:%64s",
583                                    &connector->id,
584                                    connector->mode_str) != 2 &&
585                             sscanf(argv[i], "%d@%d:%64s",
586                                    &connector->id,
587                                    &connector->crtc,
588                                    connector->mode_str) != 3) {
589                                 // TODO: we could support connector specified as a name too, I suppose
590                                 ERROR("invalid arg: %s", argv[i]);
591                                 goto fail;
592                         }
593                         disp_kms->bo_flags |= OMAP_BO_SCANOUT;
594                 } else {
595                         /* ignore */
596                         continue;
597                 }
598                 argv[i] = NULL;
599         }
601         disp->width = 0;
602         disp->height = 0;
603         for (i = 0; i < (int)disp_kms->connectors_count; i++) {
604                 struct connector *c = &disp_kms->connector[i];
605                 connector_find_mode(disp, c);
606                 if (c->mode == NULL)
607                         continue;
608                 /* setup side-by-side virtual display */
609                 disp->width += c->mode->hdisplay;
610                 if (disp->height < c->mode->vdisplay) {
611                         disp->height = c->mode->vdisplay;
612                 }
613         }
615         MSG("using %d connectors, %dx%d display, multiplanar: %d",
616                         disp_kms->connectors_count, disp->width, disp->height, disp->multiplanar);
618         return disp;
620 fail:
621         // XXX cleanup
622         return NULL;