8fc38a1d6820f5160c973618727d563191feb76f
[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 struct omap_bo *
68 alloc_bo(struct display *disp, uint32_t bpp, uint32_t width, uint32_t height,
69                 uint32_t *bo_handle, uint32_t *pitch)
70 {
71         struct display_kms *disp_kms = to_display_kms(disp);
72         struct omap_bo *bo;
73         uint32_t bo_flags = disp_kms->bo_flags;
75         if ((bo_flags & OMAP_BO_TILED) == OMAP_BO_TILED) {
76                 bo_flags &= ~OMAP_BO_TILED;
77                 if (bpp == 8) {
78                         bo_flags |= OMAP_BO_TILED_8;
79                 } else if (bpp == 16) {
80                         bo_flags |= OMAP_BO_TILED_16;
81                 } else if (bpp == 32) {
82                         bo_flags |= OMAP_BO_TILED_32;
83                 }
84         }
85         bo_flags |= OMAP_BO_WC;
87         if (bo_flags & OMAP_BO_TILED) {
88                 bo = omap_bo_new_tiled(disp->dev, ALIGN2(width,7), 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;
121         buf->multiplanar = true;
123         buf->nbo = 1;
125         if (!fourcc)
126                 fourcc = FOURCC('A','R','2','4');
128         switch(fourcc) {
129         case FOURCC('A','R','2','4'):
130                 buf->nbo = 1;
131                 buf->bo[0] = alloc_bo(disp, 32, buf->width, buf->height,
132                                 &bo_handles[0], &buf->pitches[0]);
133                 break;
134         case FOURCC('U','Y','V','Y'):
135         case FOURCC('Y','U','Y','V'):
136                 buf->nbo = 1;
137                 buf->bo[0] = alloc_bo(disp, 16, buf->width, buf->height,
138                                 &bo_handles[0], &buf->pitches[0]);
139                 break;
140         case FOURCC('N','V','1','2'):
141                 if (disp->multiplanar) {
142                         buf->nbo = 2;
143                         buf->bo[0] = alloc_bo(disp, 8, buf->width, buf->height,
144                                         &bo_handles[0], &buf->pitches[0]);
145                         buf->fd[0] = omap_bo_dmabuf(buf->bo[0]);
146                         buf->bo[1] = alloc_bo(disp, 16, buf->width/2, buf->height/2,
147                                         &bo_handles[1], &buf->pitches[1]);
148                         buf->fd[1] = omap_bo_dmabuf(buf->bo[1]);
149                 } else {
150                         buf->nbo = 1;
151                         buf->bo[0] = alloc_bo(disp, 8, buf->width, buf->height * 3 / 2,
152                                         &bo_handles[0], &buf->pitches[0]);
153                         buf->fd[0] = omap_bo_dmabuf(buf->bo[0]);
154                         bo_handles[1] = bo_handles[0];
155                         buf->pitches[1] = buf->pitches[0];
156                         offsets[1] = buf->width * buf->height;
157                         buf->multiplanar = false;
158                 }
159                 break;
160         case FOURCC('I','4','2','0'):
161                 buf->nbo = 3;
162                 buf->bo[0] = alloc_bo(disp, 8, buf->width, buf->height,
163                                 &bo_handles[0], &buf->pitches[0]);
164                 buf->bo[1] = alloc_bo(disp, 8, buf->width/2, buf->height/2,
165                                 &bo_handles[1], &buf->pitches[1]);
166                 buf->bo[2] = alloc_bo(disp, 8, buf->width/2, buf->height/2,
167                                 &bo_handles[2], &buf->pitches[2]);
168                 break;
169         default:
170                 ERROR("invalid format: 0x%08x", fourcc);
171                 goto fail;
172         }
174         ret = drmModeAddFB2(disp->fd, buf->width, buf->height, fourcc,
175                         bo_handles, buf->pitches, offsets, &buf_kms->fb_id, 0);
176         if (ret) {
177                 ERROR("drmModeAddFB2 failed: %s (%d)", strerror(errno), ret);
178                 goto fail;
179         }
181         return buf;
183 fail:
184         // XXX cleanup
185         return NULL;
188 void
189 free_buffers(struct display *disp, uint32_t n)
191         uint32_t i;
192         for (i = 0; i < n; i++) {
193                 if (disp->buf[i]) {
194                         omap_bo_del(disp->buf[i]->bo[0]);
195                         if(disp->multiplanar){
196                                 omap_bo_del(disp->buf[i]->bo[1]);
197                         }
198                 }
199         }
200 free(disp->buf);
203 static struct buffer **
204 alloc_buffers(struct display *disp, uint32_t n,
205                 uint32_t fourcc, uint32_t w, uint32_t h)
207         struct buffer **bufs;
208         uint32_t i = 0;
210         bufs = calloc(n, sizeof(*bufs));
211         if (!bufs) {
212                 ERROR("allocation failed");
213                 goto fail;
214         }
216         for (i = 0; i < n; i++) {
217                 bufs[i] = alloc_buffer(disp, fourcc, w, h);
218                 if (!bufs[i]) {
219                         ERROR("allocation failed");
220                         goto fail;
221                 }
222         }
223         disp->buf=bufs;
224         return bufs;
226 fail:
227         // XXX cleanup
228         return NULL;
231 static struct buffer **
232 get_buffers(struct display *disp, uint32_t n)
234         return alloc_buffers(disp, n, 0, disp->width, disp->height);
237 static struct buffer **
238 get_vid_buffers(struct display *disp, uint32_t n,
239                 uint32_t fourcc, uint32_t w, uint32_t h)
241         return alloc_buffers(disp, n, fourcc, w, h);
244 static void
245 page_flip_handler(int fd, unsigned int frame,
246                 unsigned int sec, unsigned int usec, void *data)
248         struct display *disp = data;
249         struct display_kms *disp_kms = to_display_kms(disp);
251         disp_kms->completed_flips++;
253         MSG("Page flip: frame=%d, sec=%d, usec=%d, remaining=%d", frame, sec, usec,
254                         disp_kms->scheduled_flips - disp_kms->completed_flips);
257 static int
258 post_buffer(struct display *disp, struct buffer *buf)
260         struct display_kms *disp_kms = to_display_kms(disp);
261         struct buffer_kms *buf_kms = to_buffer_kms(buf);
262         int ret, last_err = 0, x = 0;
263         uint32_t i;
265         for (i = 0; i < disp_kms->connectors_count; i++) {
266                 struct connector *connector = &disp_kms->connector[i];
268                 if (! connector->mode) {
269                         continue;
270                 }
272                 if (! disp_kms->current) {
273                         /* first buffer we flip to, setup the mode (since this can't
274                          * be done earlier without a buffer to scanout)
275                          */
276                         MSG("Setting mode %s on connector %d, crtc %d",
277                                         connector->mode_str, connector->id, connector->crtc);
279                         ret = drmModeSetCrtc(disp->fd, connector->crtc, buf_kms->fb_id,
280                                         x, 0, &connector->id, 1, connector->mode);
282                         x += connector->mode->hdisplay;
283                 } else {
284                         ret = drmModePageFlip(disp->fd, connector->crtc, buf_kms->fb_id,
285                                         DRM_MODE_PAGE_FLIP_EVENT, disp);
286                         disp_kms->scheduled_flips++;
287                 }
289                 if (ret) {
290                         ERROR("Could not post buffer on crtc %d: %s (%d)",
291                                         connector->crtc, strerror(errno), ret);
292                         last_err = ret;
293                         /* well, keep trying the reset of the connectors.. */
294                 }
295         }
297         /* if we flipped, wait for all flips to complete! */
298         while (disp_kms->scheduled_flips > disp_kms->completed_flips) {
299                 drmEventContext evctx = {
300                                 .version = DRM_EVENT_CONTEXT_VERSION,
301                                 .page_flip_handler = page_flip_handler,
302                 };
303                 struct timeval timeout = {
304                                 .tv_sec = 3,
305                                 .tv_usec = 0,
306                 };
307                 fd_set fds;
309                 FD_ZERO(&fds);
310                 FD_SET(disp->fd, &fds);
312                 ret = select(disp->fd + 1, &fds, NULL, NULL, &timeout);
313                 if (ret <= 0) {
314                         if (errno == EAGAIN) {
315                                 continue;    /* keep going */
316                         } else {
317                                 ERROR("Timeout waiting for flip complete: %s (%d)",
318                                                 strerror(errno), ret);
319                                 last_err = ret;
320                                 break;
321                         }
322                 }
324                 drmHandleEvent(disp->fd, &evctx);
325         }
327         disp_kms->current = buf;
329         return last_err;
332 static int
333 post_vid_buffer(struct display *disp, struct buffer *buf,
334                 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
336         struct display_kms *disp_kms = to_display_kms(disp);
337         struct buffer_kms *buf_kms = to_buffer_kms(buf);
338         int ret = 0;
339         uint32_t i, j;
341         /* ensure we have the overlay setup: */
342         for (i = 0; i < disp_kms->connectors_count; i++) {
343                 struct connector *connector = &disp_kms->connector[i];
344                 uint32_t used_planes = 0;
345                 drmModeModeInfo *mode = connector->mode;
347                 if (! mode) {
348                         continue;
349                 }
351                 if (! disp_kms->ovr[i]) {
353                         for (j = 0; j < disp_kms->plane_resources->count_planes; j++) {
354                                 drmModePlane *ovr = drmModeGetPlane(disp->fd,
355                                                 disp_kms->plane_resources->planes[j]);
356                                 if ((ovr->possible_crtcs & (1 << connector->pipe)) &&
357                                                 !(used_planes & (1 << j))) {
358                                         disp_kms->ovr[i] = ovr;
359                                         used_planes |= (1 << j);
360                                         break;
361                                 }
362                         }
363                 }
365                 if (! disp_kms->ovr[i]) {
366                         MSG("Could not find plane for crtc %d", connector->crtc);
367                         ret = -1;
368                         /* carry on and see if we can find at least one usable plane */
369                         continue;
370                 }
372                 ret = drmModeSetPlane(disp->fd, disp_kms->ovr[i]->plane_id,
373                                 connector->crtc, buf_kms->fb_id, 0,
374                                 /* make video fullscreen: */
375                                 0, 0, mode->hdisplay, mode->vdisplay,
376                                 /* source/cropping coordinates are given in Q16 */
377                                 x << 16, y << 16, w << 16, h << 16);
378                 if (ret) {
379                         ERROR("failed to enable plane %d: %s",
380                                         disp_kms->ovr[i]->plane_id, strerror(errno));
381                 }
382         }
384         return ret;
387 static void
388 close_kms(struct display *disp)
392 static void
393 connector_find_mode(struct display *disp, struct connector *c)
395         struct display_kms *disp_kms = to_display_kms(disp);
396         drmModeConnector *connector;
397         int i, j;
399         /* First, find the connector & mode */
400         c->mode = NULL;
401         for (i = 0; i < disp_kms->resources->count_connectors; i++) {
402                 connector = drmModeGetConnector(disp->fd,
403                                 disp_kms->resources->connectors[i]);
405                 if (!connector) {
406                         ERROR("could not get connector %i: %s",
407                                         disp_kms->resources->connectors[i], strerror(errno));
408                         drmModeFreeConnector(connector);
409                         continue;
410                 }
412                 if (!connector->count_modes) {
413                         drmModeFreeConnector(connector);
414                         continue;
415                 }
417                 if (connector->connector_id != c->id) {
418                         drmModeFreeConnector(connector);
419                         continue;
420                 }
422                 for (j = 0; j < connector->count_modes; j++) {
423                         c->mode = &connector->modes[j];
424                         if (!strcmp(c->mode->name, c->mode_str))
425                                 break;
426                 }
428                 /* Found it, break out */
429                 if (c->mode)
430                         break;
432                 drmModeFreeConnector(connector);
433         }
435         if (!c->mode) {
436                 ERROR("failed to find mode \"%s\"", c->mode_str);
437                 return;
438         }
440         /* Now get the encoder */
441         for (i = 0; i < disp_kms->resources->count_encoders; i++) {
442                 c->encoder = drmModeGetEncoder(disp->fd,
443                                 disp_kms->resources->encoders[i]);
445                 if (!c->encoder) {
446                         ERROR("could not get encoder %i: %s",
447                                         disp_kms->resources->encoders[i], strerror(errno));
448                         drmModeFreeEncoder(c->encoder);
449                         continue;
450                 }
452                 if (c->encoder->encoder_id  == connector->encoder_id)
453                         break;
455                 drmModeFreeEncoder(c->encoder);
456         }
458         if (c->crtc == -1)
459                 c->crtc = c->encoder->crtc_id;
461         /* and figure out which crtc index it is: */
462         for (i = 0; i < disp_kms->resources->count_crtcs; i++) {
463                 if (c->crtc == (int)disp_kms->resources->crtcs[i]) {
464                         c->pipe = i;
465                         break;
466                 }
467         }
470 void
471 disp_kms_usage(void)
473         MSG("KMS Display Options:");
474         MSG("\t-1 \t\tforce single-plane buffers");
475         MSG("\t-t <tiled-mode>\t8, 16, 32, or auto");
476         MSG("\t-s <connector_id>:<mode>\tset a mode");
477         MSG("\t-s <connector_id>@<crtc_id>:<mode>\tset a mode");
480 struct display *
481 disp_kms_open(int argc, char **argv)
483         struct display_kms *disp_kms = NULL;
484         struct display *disp;
485         int i;
487         disp_kms = calloc(1, sizeof(*disp_kms));
488         if (!disp_kms) {
489                 ERROR("allocation failed");
490                 goto fail;
491         }
492         disp = &disp_kms->base;
494         disp->fd = drmOpen("omapdrm", NULL);
495         if (disp->fd < 0) {
496                 ERROR("could not open drm device: %s (%d)", strerror(errno), errno);
497                 goto fail;
498         }
500         disp->dev = omap_device_new(disp->fd);
501         if (!disp->dev) {
502                 ERROR("couldn't create device");
503                 goto fail;
504         }
506         disp->get_buffers = get_buffers;
507         disp->get_vid_buffers = get_vid_buffers;
508         disp->post_buffer = post_buffer;
509         disp->post_vid_buffer = post_vid_buffer;
510         disp->close = close_kms;
511         disp->disp_free_buf = free_buffers ;
512         disp_kms->resources = drmModeGetResources(disp->fd);
513         if (!disp_kms->resources) {
514                 ERROR("drmModeGetResources failed: %s", strerror(errno));
515                 goto fail;
516         }
518         disp_kms->plane_resources = drmModeGetPlaneResources(disp->fd);
519         if (!disp_kms->plane_resources) {
520                 ERROR("drmModeGetPlaneResources failed: %s", strerror(errno));
521                 goto fail;
522         }
524         disp->multiplanar = true;
526         /* note: set args to NULL after we've parsed them so other modules know
527          * that it is already parsed (since the arg parsing is decentralized)
528          */
529         for (i = 1; i < argc; i++) {
530                 if (!argv[i]) {
531                         continue;
532                 }
533                 if (!strcmp("-1", argv[i])) {
534                         disp->multiplanar = false;
535                 } else if (!strcmp("-t", argv[i])) {
536                         int n;
537                         argv[i++] = NULL;
538                         if (!strcmp(argv[i], "auto")) {
539                                 n = 0;
540                         } else if (sscanf(argv[i], "%d", &n) != 1) {
541                                 ERROR("invalid arg: %s", argv[i]);
542                                 goto fail;
543                         }
545                         disp_kms->bo_flags &= ~OMAP_BO_TILED;
547                         if (n == 8) {
548                                 disp_kms->bo_flags |= OMAP_BO_TILED_8;
549                         } else if (n == 16) {
550                                 disp_kms->bo_flags |= OMAP_BO_TILED_16;
551                         } else if (n == 32) {
552                                 disp_kms->bo_flags |= OMAP_BO_TILED_32;
553                         } else if (n == 0) {
554                                 disp_kms->bo_flags |= OMAP_BO_TILED;
555                         } else {
556                                 ERROR("invalid arg: %s", argv[i]);
557                                 goto fail;
558                         }
559                 } else if (!strcmp("-s", argv[i])) {
560                         struct connector *connector =
561                                         &disp_kms->connector[disp_kms->connectors_count++];
562                         connector->crtc = -1;
563                         argv[i++] = NULL;
564                         if (sscanf(argv[i], "%d:%64s",
565                                    &connector->id,
566                                    connector->mode_str) != 2 &&
567                             sscanf(argv[i], "%d@%d:%64s",
568                                    &connector->id,
569                                    &connector->crtc,
570                                    connector->mode_str) != 3) {
571                                 // TODO: we could support connector specified as a name too, I suppose
572                                 ERROR("invalid arg: %s", argv[i]);
573                                 goto fail;
574                         }
575                         disp_kms->bo_flags |= OMAP_BO_SCANOUT;
576                 } else {
577                         /* ignore */
578                         continue;
579                 }
580                 argv[i] = NULL;
581         }
583         disp->width = 0;
584         disp->height = 0;
585         for (i = 0; i < (int)disp_kms->connectors_count; i++) {
586                 struct connector *c = &disp_kms->connector[i];
587                 connector_find_mode(disp, c);
588                 if (c->mode == NULL)
589                         continue;
590                 /* setup side-by-side virtual display */
591                 disp->width += c->mode->hdisplay;
592                 if (disp->height < c->mode->vdisplay) {
593                         disp->height = c->mode->vdisplay;
594                 }
595         }
597         MSG("using %d connectors, %dx%d display, multiplanar: %d",
598                         disp_kms->connectors_count, disp->width, disp->height, disp->multiplanar);
600         return disp;
602 fail:
603         // XXX cleanup
604         return NULL;