blob: bbc7176729061a302cd6eabf6fba7495f035ac65 [file] [log] [blame]
Philip Withnall4f499172013-02-02 12:02:32 +00001/*
2 * Copyright © 2008-2011 Kristian Høgsberg
3 * Copyright © 2011 Intel Corporation
4 * Copyright © 2012 Raspberry Pi Foundation
5 * Copyright © 2013 Philip Withnall
6 *
7 * Permission to use, copy, modify, distribute, and sell this software and
8 * its documentation for any purpose is hereby granted without fee, provided
9 * that the above copyright notice appear in all copies and that both that
10 * copyright notice and this permission notice appear in supporting
11 * documentation, and that the name of the copyright holders not be used in
12 * advertising or publicity pertaining to distribution of the software
13 * without specific, written prior permission. The copyright holders make
14 * no representations about the suitability of this software for any
15 * purpose. It is provided "as is" without express or implied warranty.
16 *
17 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
18 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
19 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
21 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
22 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
23 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 */
25
Daniel Stonec228e232013-05-22 18:03:19 +030026#include "config.h"
Philip Withnall4f499172013-02-02 12:02:32 +000027
28#include <errno.h>
29#include <stdlib.h>
30#include <stdio.h>
31#include <string.h>
32#include <math.h>
33#include <sys/mman.h>
34#include <sys/types.h>
35#include <fcntl.h>
36#include <unistd.h>
37#include <linux/fb.h>
Kristian Høgsberg7e597f22013-02-18 16:35:26 -050038#include <linux/input.h>
Philip Withnall4f499172013-02-02 12:02:32 +000039
40#include <libudev.h>
41
Philip Withnall4f499172013-02-02 12:02:32 +000042#include "compositor.h"
43#include "launcher-util.h"
44#include "pixman-renderer.h"
Peter Hutterer823ad332014-11-26 07:06:31 +100045#include "libinput-seat.h"
Adrian Negreanu4aa756d2013-09-06 15:16:09 +030046#include "gl-renderer.h"
Philip Withnall4f499172013-02-02 12:02:32 +000047
48struct fbdev_compositor {
49 struct weston_compositor base;
50 uint32_t prev_state;
51
52 struct udev *udev;
Rob Bradfordd355b802013-05-31 18:09:55 +010053 struct udev_input input;
Adrian Negreanu4aa756d2013-09-06 15:16:09 +030054 int use_pixman;
Kristian Høgsberg61741a22013-09-17 16:02:57 -070055 struct wl_listener session_listener;
Philip Withnall4f499172013-02-02 12:02:32 +000056};
57
58struct fbdev_screeninfo {
59 unsigned int x_resolution; /* pixels, visible area */
60 unsigned int y_resolution; /* pixels, visible area */
61 unsigned int width_mm; /* visible screen width in mm */
62 unsigned int height_mm; /* visible screen height in mm */
63 unsigned int bits_per_pixel;
64
65 size_t buffer_length; /* length of frame buffer memory in bytes */
66 size_t line_length; /* length of a line in bytes */
67 char id[16]; /* screen identifier */
68
69 pixman_format_code_t pixel_format; /* frame buffer pixel format */
70 unsigned int refresh_rate; /* Hertz */
71};
72
73struct fbdev_output {
74 struct fbdev_compositor *compositor;
75 struct weston_output base;
76
77 struct weston_mode mode;
78 struct wl_event_source *finish_frame_timer;
79
80 /* Frame buffer details. */
81 const char *device; /* ownership shared with fbdev_parameters */
82 struct fbdev_screeninfo fb_info;
83 void *fb; /* length is fb_info.buffer_length */
84
85 /* pixman details. */
86 pixman_image_t *hw_surface;
87 pixman_image_t *shadow_surface;
88 void *shadow_buf;
89 uint8_t depth;
90};
91
Philip Withnall4f499172013-02-02 12:02:32 +000092struct fbdev_parameters {
93 int tty;
94 char *device;
Adrian Negreanu4aa756d2013-09-06 15:16:09 +030095 int use_gl;
Philip Withnall4f499172013-02-02 12:02:32 +000096};
97
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +030098struct gl_renderer_interface *gl_renderer;
99
Kristian Høgsberg7e597f22013-02-18 16:35:26 -0500100static const char default_seat[] = "seat0";
101
Philip Withnall4f499172013-02-02 12:02:32 +0000102static inline struct fbdev_output *
103to_fbdev_output(struct weston_output *base)
104{
105 return container_of(base, struct fbdev_output, base);
106}
107
Philip Withnall4f499172013-02-02 12:02:32 +0000108static inline struct fbdev_compositor *
109to_fbdev_compositor(struct weston_compositor *base)
110{
111 return container_of(base, struct fbdev_compositor, base);
112}
113
114static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200115fbdev_output_start_repaint_loop(struct weston_output *output)
116{
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400117 struct timespec ts;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200118
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400119 clock_gettime(output->compositor->presentation_clock, &ts);
120 weston_output_finish_frame(output, &ts);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200121}
122
123static void
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300124fbdev_output_repaint_pixman(struct weston_output *base, pixman_region32_t *damage)
Philip Withnall4f499172013-02-02 12:02:32 +0000125{
126 struct fbdev_output *output = to_fbdev_output(base);
127 struct weston_compositor *ec = output->base.compositor;
128 pixman_box32_t *rects;
129 int nrects, i, src_x, src_y, x1, y1, x2, y2, width, height;
130
131 /* Repaint the damaged region onto the back buffer. */
132 pixman_renderer_output_set_buffer(base, output->shadow_surface);
133 ec->renderer->repaint_output(base, damage);
134
135 /* Transform and composite onto the frame buffer. */
136 width = pixman_image_get_width(output->shadow_surface);
137 height = pixman_image_get_height(output->shadow_surface);
138 rects = pixman_region32_rectangles(damage, &nrects);
139
140 for (i = 0; i < nrects; i++) {
141 switch (base->transform) {
142 default:
143 case WL_OUTPUT_TRANSFORM_NORMAL:
144 x1 = rects[i].x1;
145 x2 = rects[i].x2;
146 y1 = rects[i].y1;
147 y2 = rects[i].y2;
148 break;
149 case WL_OUTPUT_TRANSFORM_180:
150 x1 = width - rects[i].x2;
151 x2 = width - rects[i].x1;
152 y1 = height - rects[i].y2;
153 y2 = height - rects[i].y1;
154 break;
155 case WL_OUTPUT_TRANSFORM_90:
156 x1 = height - rects[i].y2;
157 x2 = height - rects[i].y1;
158 y1 = rects[i].x1;
159 y2 = rects[i].x2;
160 break;
161 case WL_OUTPUT_TRANSFORM_270:
162 x1 = rects[i].y1;
163 x2 = rects[i].y2;
164 y1 = width - rects[i].x2;
165 y2 = width - rects[i].x1;
166 break;
167 }
168 src_x = x1;
169 src_y = y1;
170
171 pixman_image_composite32(PIXMAN_OP_SRC,
172 output->shadow_surface, /* src */
173 NULL /* mask */,
174 output->hw_surface, /* dest */
175 src_x, src_y, /* src_x, src_y */
176 0, 0, /* mask_x, mask_y */
177 x1, y1, /* dest_x, dest_y */
178 x2 - x1, /* width */
179 y2 - y1 /* height */);
180 }
181
182 /* Update the damage region. */
183 pixman_region32_subtract(&ec->primary_plane.damage,
184 &ec->primary_plane.damage, damage);
185
186 /* Schedule the end of the frame. We do not sync this to the frame
187 * buffer clock because users who want that should be using the DRM
188 * compositor. FBIO_WAITFORVSYNC blocks and FB_ACTIVATE_VBL requires
189 * panning, which is broken in most kernel drivers.
190 *
191 * Finish the frame synchronised to the specified refresh rate. The
192 * refresh rate is given in mHz and the interval in ms. */
193 wl_event_source_timer_update(output->finish_frame_timer,
194 1000000 / output->mode.refresh);
195}
196
David Herrmann1edf44c2013-10-22 17:11:26 +0200197static int
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300198fbdev_output_repaint(struct weston_output *base, pixman_region32_t *damage)
199{
200 struct fbdev_output *output = to_fbdev_output(base);
201 struct fbdev_compositor *fbc = output->compositor;
202 struct weston_compositor *ec = & fbc->base;
203
204 if (fbc->use_pixman) {
205 fbdev_output_repaint_pixman(base,damage);
206 } else {
207 ec->renderer->repaint_output(base, damage);
208 /* Update the damage region. */
209 pixman_region32_subtract(&ec->primary_plane.damage,
210 &ec->primary_plane.damage, damage);
211
212 wl_event_source_timer_update(output->finish_frame_timer,
213 1000000 / output->mode.refresh);
214 }
David Herrmann1edf44c2013-10-22 17:11:26 +0200215
216 return 0;
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300217}
218
Philip Withnall4f499172013-02-02 12:02:32 +0000219static int
220finish_frame_handler(void *data)
221{
222 struct fbdev_output *output = data;
Philip Withnall4f499172013-02-02 12:02:32 +0000223
Jonas Ådahle5a12252013-04-05 23:07:11 +0200224 fbdev_output_start_repaint_loop(&output->base);
Philip Withnall4f499172013-02-02 12:02:32 +0000225
226 return 1;
227}
228
229static pixman_format_code_t
230calculate_pixman_format(struct fb_var_screeninfo *vinfo,
231 struct fb_fix_screeninfo *finfo)
232{
233 /* Calculate the pixman format supported by the frame buffer from the
234 * buffer's metadata. Return 0 if no known pixman format is supported
235 * (since this has depth 0 it's guaranteed to not conflict with any
236 * actual pixman format).
237 *
238 * Documentation on the vinfo and finfo structures:
239 * http://www.mjmwired.net/kernel/Documentation/fb/api.txt
240 *
241 * TODO: Try a bit harder to support other formats, including setting
242 * the preferred format in the hardware. */
243 int type;
244
245 weston_log("Calculating pixman format from:\n"
246 STAMP_SPACE " - type: %i (aux: %i)\n"
247 STAMP_SPACE " - visual: %i\n"
248 STAMP_SPACE " - bpp: %i (grayscale: %i)\n"
249 STAMP_SPACE " - red: offset: %i, length: %i, MSB: %i\n"
250 STAMP_SPACE " - green: offset: %i, length: %i, MSB: %i\n"
251 STAMP_SPACE " - blue: offset: %i, length: %i, MSB: %i\n"
252 STAMP_SPACE " - transp: offset: %i, length: %i, MSB: %i\n",
253 finfo->type, finfo->type_aux, finfo->visual,
254 vinfo->bits_per_pixel, vinfo->grayscale,
255 vinfo->red.offset, vinfo->red.length, vinfo->red.msb_right,
256 vinfo->green.offset, vinfo->green.length,
257 vinfo->green.msb_right,
258 vinfo->blue.offset, vinfo->blue.length,
259 vinfo->blue.msb_right,
260 vinfo->transp.offset, vinfo->transp.length,
261 vinfo->transp.msb_right);
262
263 /* We only handle packed formats at the moment. */
264 if (finfo->type != FB_TYPE_PACKED_PIXELS)
265 return 0;
266
267 /* We only handle true-colour frame buffers at the moment. */
Marc Chalainffbddff2013-09-03 16:47:43 +0200268 switch(finfo->visual) {
269 case FB_VISUAL_TRUECOLOR:
270 case FB_VISUAL_DIRECTCOLOR:
271 if (vinfo->grayscale != 0)
272 return 0;
273 break;
274 default:
275 return 0;
276 }
Philip Withnall4f499172013-02-02 12:02:32 +0000277
278 /* We only support formats with MSBs on the left. */
279 if (vinfo->red.msb_right != 0 || vinfo->green.msb_right != 0 ||
280 vinfo->blue.msb_right != 0)
281 return 0;
282
283 /* Work out the format type from the offsets. We only support RGBA and
284 * ARGB at the moment. */
285 type = PIXMAN_TYPE_OTHER;
286
287 if ((vinfo->transp.offset >= vinfo->red.offset ||
288 vinfo->transp.length == 0) &&
289 vinfo->red.offset >= vinfo->green.offset &&
290 vinfo->green.offset >= vinfo->blue.offset)
291 type = PIXMAN_TYPE_ARGB;
292 else if (vinfo->red.offset >= vinfo->green.offset &&
293 vinfo->green.offset >= vinfo->blue.offset &&
294 vinfo->blue.offset >= vinfo->transp.offset)
295 type = PIXMAN_TYPE_RGBA;
296
297 if (type == PIXMAN_TYPE_OTHER)
298 return 0;
299
300 /* Build the format. */
301 return PIXMAN_FORMAT(vinfo->bits_per_pixel, type,
302 vinfo->transp.length,
303 vinfo->red.length,
304 vinfo->green.length,
305 vinfo->blue.length);
306}
307
308static int
309calculate_refresh_rate(struct fb_var_screeninfo *vinfo)
310{
311 uint64_t quot;
312
313 /* Calculate monitor refresh rate. Default is 60 Hz. Units are mHz. */
314 quot = (vinfo->upper_margin + vinfo->lower_margin + vinfo->yres);
315 quot *= (vinfo->left_margin + vinfo->right_margin + vinfo->xres);
316 quot *= vinfo->pixclock;
317
318 if (quot > 0) {
319 uint64_t refresh_rate;
320
321 refresh_rate = 1000000000000000LLU / quot;
322 if (refresh_rate > 200000)
323 refresh_rate = 200000; /* cap at 200 Hz */
324
325 return refresh_rate;
326 }
327
328 return 60 * 1000; /* default to 60 Hz */
329}
330
331static int
332fbdev_query_screen_info(struct fbdev_output *output, int fd,
333 struct fbdev_screeninfo *info)
334{
335 struct fb_var_screeninfo varinfo;
336 struct fb_fix_screeninfo fixinfo;
337
338 /* Probe the device for screen information. */
339 if (ioctl(fd, FBIOGET_FSCREENINFO, &fixinfo) < 0 ||
340 ioctl(fd, FBIOGET_VSCREENINFO, &varinfo) < 0) {
341 return -1;
342 }
343
344 /* Store the pertinent data. */
345 info->x_resolution = varinfo.xres;
346 info->y_resolution = varinfo.yres;
347 info->width_mm = varinfo.width;
348 info->height_mm = varinfo.height;
349 info->bits_per_pixel = varinfo.bits_per_pixel;
350
351 info->buffer_length = fixinfo.smem_len;
352 info->line_length = fixinfo.line_length;
353 strncpy(info->id, fixinfo.id, sizeof(info->id) / sizeof(*info->id));
354
355 info->pixel_format = calculate_pixman_format(&varinfo, &fixinfo);
356 info->refresh_rate = calculate_refresh_rate(&varinfo);
357
358 if (info->pixel_format == 0) {
359 weston_log("Frame buffer uses an unsupported format.\n");
360 return -1;
361 }
362
363 return 1;
364}
365
366static int
367fbdev_set_screen_info(struct fbdev_output *output, int fd,
368 struct fbdev_screeninfo *info)
369{
370 struct fb_var_screeninfo varinfo;
371
372 /* Grab the current screen information. */
373 if (ioctl(fd, FBIOGET_VSCREENINFO, &varinfo) < 0) {
374 return -1;
375 }
376
377 /* Update the information. */
378 varinfo.xres = info->x_resolution;
379 varinfo.yres = info->y_resolution;
380 varinfo.width = info->width_mm;
381 varinfo.height = info->height_mm;
382 varinfo.bits_per_pixel = info->bits_per_pixel;
383
384 /* Try to set up an ARGB (x8r8g8b8) pixel format. */
385 varinfo.grayscale = 0;
386 varinfo.transp.offset = 24;
387 varinfo.transp.length = 0;
388 varinfo.transp.msb_right = 0;
389 varinfo.red.offset = 16;
390 varinfo.red.length = 8;
391 varinfo.red.msb_right = 0;
392 varinfo.green.offset = 8;
393 varinfo.green.length = 8;
394 varinfo.green.msb_right = 0;
395 varinfo.blue.offset = 0;
396 varinfo.blue.length = 8;
397 varinfo.blue.msb_right = 0;
398
399 /* Set the device's screen information. */
400 if (ioctl(fd, FBIOPUT_VSCREENINFO, &varinfo) < 0) {
401 return -1;
402 }
403
404 return 1;
405}
406
407static void fbdev_frame_buffer_destroy(struct fbdev_output *output);
408
409/* Returns an FD for the frame buffer device. */
410static int
411fbdev_frame_buffer_open(struct fbdev_output *output, const char *fb_dev,
412 struct fbdev_screeninfo *screen_info)
413{
414 int fd = -1;
415
416 weston_log("Opening fbdev frame buffer.\n");
417
418 /* Open the frame buffer device. */
419 fd = open(fb_dev, O_RDWR | O_CLOEXEC);
420 if (fd < 0) {
421 weston_log("Failed to open frame buffer device ‘%s’: %s\n",
422 fb_dev, strerror(errno));
423 return -1;
424 }
425
426 /* Grab the screen info. */
427 if (fbdev_query_screen_info(output, fd, screen_info) < 0) {
428 weston_log("Failed to get frame buffer info: %s\n",
429 strerror(errno));
430
431 close(fd);
432 return -1;
433 }
434
435 return fd;
436}
437
438/* Closes the FD on success or failure. */
439static int
440fbdev_frame_buffer_map(struct fbdev_output *output, int fd)
441{
442 int retval = -1;
443
444 weston_log("Mapping fbdev frame buffer.\n");
445
446 /* Map the frame buffer. Write-only mode, since we don't want to read
447 * anything back (because it's slow). */
448 output->fb = mmap(NULL, output->fb_info.buffer_length,
449 PROT_WRITE, MAP_SHARED, fd, 0);
450 if (output->fb == MAP_FAILED) {
451 weston_log("Failed to mmap frame buffer: %s\n",
452 strerror(errno));
453 goto out_close;
454 }
455
456 /* Create a pixman image to wrap the memory mapped frame buffer. */
457 output->hw_surface =
458 pixman_image_create_bits(output->fb_info.pixel_format,
459 output->fb_info.x_resolution,
460 output->fb_info.y_resolution,
461 output->fb,
462 output->fb_info.line_length);
463 if (output->hw_surface == NULL) {
464 weston_log("Failed to create surface for frame buffer.\n");
465 goto out_unmap;
466 }
467
468 /* Success! */
469 retval = 0;
470
471out_unmap:
472 if (retval != 0 && output->fb != NULL)
473 fbdev_frame_buffer_destroy(output);
474
475out_close:
476 if (fd >= 0)
477 close(fd);
478
479 return retval;
480}
481
482static void
483fbdev_frame_buffer_destroy(struct fbdev_output *output)
484{
485 weston_log("Destroying fbdev frame buffer.\n");
486
487 if (munmap(output->fb, output->fb_info.buffer_length) < 0)
488 weston_log("Failed to munmap frame buffer: %s\n",
489 strerror(errno));
490
491 output->fb = NULL;
492}
493
494static void fbdev_output_destroy(struct weston_output *base);
495static void fbdev_output_disable(struct weston_output *base);
496
497static int
498fbdev_output_create(struct fbdev_compositor *compositor,
499 const char *device)
500{
501 struct fbdev_output *output;
502 pixman_transform_t transform;
503 int fb_fd;
504 int shadow_width, shadow_height;
505 int width, height;
506 unsigned int bytes_per_pixel;
507 struct wl_event_loop *loop;
508
509 weston_log("Creating fbdev output.\n");
510
511 output = calloc(1, sizeof *output);
512 if (!output)
513 return -1;
514
515 output->compositor = compositor;
516 output->device = device;
517
518 /* Create the frame buffer. */
519 fb_fd = fbdev_frame_buffer_open(output, device, &output->fb_info);
520 if (fb_fd < 0) {
521 weston_log("Creating frame buffer failed.\n");
522 goto out_free;
523 }
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300524 if (compositor->use_pixman) {
525 if (fbdev_frame_buffer_map(output, fb_fd) < 0) {
526 weston_log("Mapping frame buffer failed.\n");
527 goto out_free;
528 }
Kristian Høgsberg8ac6a2d2013-10-09 09:59:06 -0700529 } else {
530 close(fb_fd);
Philip Withnall4f499172013-02-02 12:02:32 +0000531 }
532
Jonas Ådahle5a12252013-04-05 23:07:11 +0200533 output->base.start_repaint_loop = fbdev_output_start_repaint_loop;
Philip Withnall4f499172013-02-02 12:02:32 +0000534 output->base.repaint = fbdev_output_repaint;
535 output->base.destroy = fbdev_output_destroy;
536 output->base.assign_planes = NULL;
537 output->base.set_backlight = NULL;
538 output->base.set_dpms = NULL;
539 output->base.switch_mode = NULL;
540
541 /* only one static mode in list */
542 output->mode.flags =
543 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
544 output->mode.width = output->fb_info.x_resolution;
545 output->mode.height = output->fb_info.y_resolution;
546 output->mode.refresh = output->fb_info.refresh_rate;
547 wl_list_init(&output->base.mode_list);
548 wl_list_insert(&output->base.mode_list, &output->mode.link);
549
Hardeningff39efa2013-09-18 23:56:35 +0200550 output->base.current_mode = &output->mode;
Philip Withnall4f499172013-02-02 12:02:32 +0000551 output->base.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
552 output->base.make = "unknown";
553 output->base.model = output->fb_info.id;
554
555 weston_output_init(&output->base, &compositor->base,
556 0, 0, output->fb_info.width_mm,
557 output->fb_info.height_mm,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200558 WL_OUTPUT_TRANSFORM_NORMAL,
559 1);
Philip Withnall4f499172013-02-02 12:02:32 +0000560
561 width = output->fb_info.x_resolution;
562 height = output->fb_info.y_resolution;
563
564 pixman_transform_init_identity(&transform);
565 switch (output->base.transform) {
566 default:
567 case WL_OUTPUT_TRANSFORM_NORMAL:
568 shadow_width = width;
569 shadow_height = height;
570 pixman_transform_rotate(&transform,
571 NULL, 0, 0);
572 pixman_transform_translate(&transform, NULL,
573 0, 0);
574 break;
575 case WL_OUTPUT_TRANSFORM_180:
576 shadow_width = width;
577 shadow_height = height;
578 pixman_transform_rotate(&transform,
579 NULL, -pixman_fixed_1, 0);
580 pixman_transform_translate(NULL, &transform,
581 pixman_int_to_fixed(shadow_width),
582 pixman_int_to_fixed(shadow_height));
583 break;
584 case WL_OUTPUT_TRANSFORM_270:
585 shadow_width = height;
586 shadow_height = width;
587 pixman_transform_rotate(&transform,
588 NULL, 0, pixman_fixed_1);
589 pixman_transform_translate(&transform,
590 NULL,
591 pixman_int_to_fixed(shadow_width),
592 0);
593 break;
594 case WL_OUTPUT_TRANSFORM_90:
595 shadow_width = height;
596 shadow_height = width;
597 pixman_transform_rotate(&transform,
598 NULL, 0, -pixman_fixed_1);
599 pixman_transform_translate(&transform,
600 NULL,
601 0,
602 pixman_int_to_fixed(shadow_height));
603 break;
604 }
605
606 bytes_per_pixel = output->fb_info.bits_per_pixel / 8;
607
608 output->shadow_buf = malloc(width * height * bytes_per_pixel);
609 output->shadow_surface =
610 pixman_image_create_bits(output->fb_info.pixel_format,
611 shadow_width, shadow_height,
612 output->shadow_buf,
613 shadow_width * bytes_per_pixel);
614 if (output->shadow_buf == NULL || output->shadow_surface == NULL) {
615 weston_log("Failed to create surface for frame buffer.\n");
616 goto out_hw_surface;
617 }
618
619 /* No need in transform for normal output */
620 if (output->base.transform != WL_OUTPUT_TRANSFORM_NORMAL)
621 pixman_image_set_transform(output->shadow_surface, &transform);
622
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300623 if (compositor->use_pixman) {
624 if (pixman_renderer_output_create(&output->base) < 0)
625 goto out_shadow_surface;
626 } else {
627 setenv("HYBRIS_EGLPLATFORM", "wayland", 1);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300628 if (gl_renderer->output_create(&output->base,
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000629 (EGLNativeWindowType)NULL,
630 gl_renderer->opaque_attribs,
631 NULL) < 0) {
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300632 weston_log("gl_renderer_output_create failed.\n");
633 goto out_shadow_surface;
634 }
635 }
636
Philip Withnall4f499172013-02-02 12:02:32 +0000637
638 loop = wl_display_get_event_loop(compositor->base.wl_display);
639 output->finish_frame_timer =
640 wl_event_loop_add_timer(loop, finish_frame_handler, output);
641
642 wl_list_insert(compositor->base.output_list.prev, &output->base.link);
643
644 weston_log("fbdev output %d×%d px\n",
645 output->mode.width, output->mode.height);
646 weston_log_continue(STAMP_SPACE "guessing %d Hz and 96 dpi\n",
647 output->mode.refresh / 1000);
648
649 return 0;
650
651out_shadow_surface:
652 pixman_image_unref(output->shadow_surface);
653 output->shadow_surface = NULL;
654out_hw_surface:
655 free(output->shadow_buf);
656 pixman_image_unref(output->hw_surface);
657 output->hw_surface = NULL;
658 weston_output_destroy(&output->base);
659 fbdev_frame_buffer_destroy(output);
660out_free:
661 free(output);
662
663 return -1;
664}
665
666static void
667fbdev_output_destroy(struct weston_output *base)
668{
669 struct fbdev_output *output = to_fbdev_output(base);
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300670 struct fbdev_compositor *compositor = output->compositor;
Philip Withnall4f499172013-02-02 12:02:32 +0000671
672 weston_log("Destroying fbdev output.\n");
673
674 /* Close the frame buffer. */
675 fbdev_output_disable(base);
676
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300677 if (compositor->use_pixman) {
678 if (base->renderer_state != NULL)
679 pixman_renderer_output_destroy(base);
Philip Withnall4f499172013-02-02 12:02:32 +0000680
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300681 if (output->shadow_surface != NULL) {
682 pixman_image_unref(output->shadow_surface);
683 output->shadow_surface = NULL;
684 }
Philip Withnall4f499172013-02-02 12:02:32 +0000685
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300686 if (output->shadow_buf != NULL) {
687 free(output->shadow_buf);
688 output->shadow_buf = NULL;
689 }
690 } else {
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300691 gl_renderer->output_destroy(base);
Philip Withnall4f499172013-02-02 12:02:32 +0000692 }
693
694 /* Remove the output. */
Philip Withnall4f499172013-02-02 12:02:32 +0000695 weston_output_destroy(&output->base);
696
697 free(output);
698}
699
700/* strcmp()-style return values. */
701static int
702compare_screen_info (const struct fbdev_screeninfo *a,
703 const struct fbdev_screeninfo *b)
704{
705 if (a->x_resolution == b->x_resolution &&
706 a->y_resolution == b->y_resolution &&
707 a->width_mm == b->width_mm &&
708 a->height_mm == b->height_mm &&
709 a->bits_per_pixel == b->bits_per_pixel &&
710 a->pixel_format == b->pixel_format &&
711 a->refresh_rate == b->refresh_rate)
712 return 0;
713
714 return 1;
715}
716
717static int
718fbdev_output_reenable(struct fbdev_compositor *compositor,
719 struct weston_output *base)
720{
721 struct fbdev_output *output = to_fbdev_output(base);
722 struct fbdev_screeninfo new_screen_info;
723 int fb_fd;
Rob Bradfordf8ef42f2013-07-26 16:29:38 +0100724 const char *device;
Philip Withnall4f499172013-02-02 12:02:32 +0000725
726 weston_log("Re-enabling fbdev output.\n");
727
728 /* Create the frame buffer. */
729 fb_fd = fbdev_frame_buffer_open(output, output->device,
730 &new_screen_info);
731 if (fb_fd < 0) {
732 weston_log("Creating frame buffer failed.\n");
733 goto err;
734 }
735
736 /* Check whether the frame buffer details have changed since we were
737 * disabled. */
738 if (compare_screen_info (&output->fb_info, &new_screen_info) != 0) {
739 /* Perform a mode-set to restore the old mode. */
740 if (fbdev_set_screen_info(output, fb_fd,
741 &output->fb_info) < 0) {
742 weston_log("Failed to restore mode settings. "
743 "Attempting to re-open output anyway.\n");
744 }
745
Rob Bradford581b3fd2013-07-26 16:29:39 +0100746 close(fb_fd);
747
Philip Withnall4f499172013-02-02 12:02:32 +0000748 /* Remove and re-add the output so that resources depending on
749 * the frame buffer X/Y resolution (such as the shadow buffer)
750 * are re-initialised. */
Rob Bradfordf8ef42f2013-07-26 16:29:38 +0100751 device = output->device;
Philip Withnall4f499172013-02-02 12:02:32 +0000752 fbdev_output_destroy(base);
Rob Bradfordf8ef42f2013-07-26 16:29:38 +0100753 fbdev_output_create(compositor, device);
Philip Withnall4f499172013-02-02 12:02:32 +0000754
755 return 0;
756 }
757
758 /* Map the device if it has the same details as before. */
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300759 if (compositor->use_pixman) {
760 if (fbdev_frame_buffer_map(output, fb_fd) < 0) {
761 weston_log("Mapping frame buffer failed.\n");
762 goto err;
763 }
Philip Withnall4f499172013-02-02 12:02:32 +0000764 }
765
766 return 0;
767
768err:
769 return -1;
770}
771
772/* NOTE: This leaves output->fb_info populated, caching data so that if
773 * fbdev_output_reenable() is called again, it can determine whether a mode-set
774 * is needed. */
775static void
776fbdev_output_disable(struct weston_output *base)
777{
778 struct fbdev_output *output = to_fbdev_output(base);
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300779 struct fbdev_compositor *compositor = output->compositor;
Philip Withnall4f499172013-02-02 12:02:32 +0000780
781 weston_log("Disabling fbdev output.\n");
782
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300783 if ( ! compositor->use_pixman) return;
784
Philip Withnall4f499172013-02-02 12:02:32 +0000785 if (output->hw_surface != NULL) {
786 pixman_image_unref(output->hw_surface);
787 output->hw_surface = NULL;
788 }
789
790 fbdev_frame_buffer_destroy(output);
791}
792
793static void
Philip Withnall4f499172013-02-02 12:02:32 +0000794fbdev_compositor_destroy(struct weston_compositor *base)
795{
796 struct fbdev_compositor *compositor = to_fbdev_compositor(base);
Philip Withnall4f499172013-02-02 12:02:32 +0000797
Rob Bradfordd355b802013-05-31 18:09:55 +0100798 udev_input_destroy(&compositor->input);
Philip Withnall4f499172013-02-02 12:02:32 +0000799
800 /* Destroy the output. */
801 weston_compositor_shutdown(&compositor->base);
802
803 /* Chain up. */
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700804 weston_launcher_destroy(compositor->base.launcher);
Philip Withnall4f499172013-02-02 12:02:32 +0000805
806 free(compositor);
807}
808
809static void
Kristian Høgsberg61741a22013-09-17 16:02:57 -0700810session_notify(struct wl_listener *listener, void *data)
Philip Withnall4f499172013-02-02 12:02:32 +0000811{
Kristian Høgsberg61741a22013-09-17 16:02:57 -0700812 struct fbdev_compositor *compositor = data;
Philip Withnall4f499172013-02-02 12:02:32 +0000813 struct weston_output *output;
814
Kristian Høgsberg61741a22013-09-17 16:02:57 -0700815 if (compositor->base.session_active) {
Philip Withnall4f499172013-02-02 12:02:32 +0000816 weston_log("entering VT\n");
Philip Withnall4f499172013-02-02 12:02:32 +0000817 compositor->base.state = compositor->prev_state;
818
819 wl_list_for_each(output, &compositor->base.output_list, link) {
820 fbdev_output_reenable(compositor, output);
821 }
822
823 weston_compositor_damage_all(&compositor->base);
824
Jonas Ådahl0feb32e2014-03-12 22:08:41 +0100825 udev_input_enable(&compositor->input);
Kristian Høgsberg61741a22013-09-17 16:02:57 -0700826 } else {
Philip Withnall4f499172013-02-02 12:02:32 +0000827 weston_log("leaving VT\n");
Rob Bradfordd355b802013-05-31 18:09:55 +0100828 udev_input_disable(&compositor->input);
Philip Withnall4f499172013-02-02 12:02:32 +0000829
830 wl_list_for_each(output, &compositor->base.output_list, link) {
831 fbdev_output_disable(output);
832 }
833
Philip Withnall4f499172013-02-02 12:02:32 +0000834 compositor->prev_state = compositor->base.state;
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +0100835 weston_compositor_offscreen(&compositor->base);
Philip Withnall4f499172013-02-02 12:02:32 +0000836
837 /* If we have a repaint scheduled (from the idle handler), make
838 * sure we cancel that so we don't try to pageflip when we're
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +0100839 * vt switched away. The OFFSCREEN state will prevent
Philip Withnall4f499172013-02-02 12:02:32 +0000840 * further attemps at repainting. When we switch
841 * back, we schedule a repaint, which will process
842 * pending frame callbacks. */
843
844 wl_list_for_each(output,
845 &compositor->base.output_list, link) {
846 output->repaint_needed = 0;
847 }
Philip Withnall4f499172013-02-02 12:02:32 +0000848 };
849}
850
851static void
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700852fbdev_restore(struct weston_compositor *compositor)
Philip Withnall4f499172013-02-02 12:02:32 +0000853{
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700854 weston_launcher_restore(compositor->launcher);
Philip Withnall4f499172013-02-02 12:02:32 +0000855}
856
857static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400858switch_vt_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Philip Withnall4f499172013-02-02 12:02:32 +0000859{
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700860 struct weston_compositor *compositor = data;
Philip Withnall4f499172013-02-02 12:02:32 +0000861
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700862 weston_launcher_activate_vt(compositor->launcher, key - KEY_F1 + 1);
Philip Withnall4f499172013-02-02 12:02:32 +0000863}
864
865static struct weston_compositor *
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500866fbdev_compositor_create(struct wl_display *display, int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400867 struct weston_config *config,
868 struct fbdev_parameters *param)
Philip Withnall4f499172013-02-02 12:02:32 +0000869{
870 struct fbdev_compositor *compositor;
Rob Bradford2387fde2013-05-31 18:09:57 +0100871 const char *seat_id = default_seat;
Philip Withnall4f499172013-02-02 12:02:32 +0000872 uint32_t key;
873
874 weston_log("initializing fbdev backend\n");
875
876 compositor = calloc(1, sizeof *compositor);
877 if (compositor == NULL)
878 return NULL;
879
880 if (weston_compositor_init(&compositor->base, display, argc, argv,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400881 config) < 0)
Philip Withnall4f499172013-02-02 12:02:32 +0000882 goto out_free;
883
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400884 if (weston_compositor_set_presentation_clock_software(
885 &compositor->base) < 0)
886 goto out_compositor;
887
Philip Withnall4f499172013-02-02 12:02:32 +0000888 compositor->udev = udev_new();
889 if (compositor->udev == NULL) {
890 weston_log("Failed to initialize udev context.\n");
891 goto out_compositor;
892 }
893
894 /* Set up the TTY. */
Kristian Høgsberg61741a22013-09-17 16:02:57 -0700895 compositor->session_listener.notify = session_notify;
896 wl_signal_add(&compositor->base.session_signal,
897 &compositor->session_listener);
Kristian Høgsberg6ff3ff52013-10-02 10:53:33 -0700898 compositor->base.launcher =
David Herrmanncc5b2ed2013-10-22 00:28:09 +0200899 weston_launcher_connect(&compositor->base, param->tty, "seat0");
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700900 if (!compositor->base.launcher) {
David Herrmann1641d142013-10-15 14:29:57 +0200901 weston_log("fatal: fbdev backend should be run "
902 "using weston-launch binary or as root\n");
Philip Withnall4f499172013-02-02 12:02:32 +0000903 goto out_udev;
904 }
905
906 compositor->base.destroy = fbdev_compositor_destroy;
907 compositor->base.restore = fbdev_restore;
908
Philip Withnall4f499172013-02-02 12:02:32 +0000909 compositor->prev_state = WESTON_COMPOSITOR_ACTIVE;
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300910 compositor->use_pixman = !param->use_gl;
Philip Withnall4f499172013-02-02 12:02:32 +0000911
912 for (key = KEY_F1; key < KEY_F9; key++)
913 weston_compositor_add_key_binding(&compositor->base, key,
914 MODIFIER_CTRL | MODIFIER_ALT,
915 switch_vt_binding,
916 compositor);
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300917 if (compositor->use_pixman) {
918 if (pixman_renderer_init(&compositor->base) < 0)
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700919 goto out_launcher;
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300920 } else {
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300921 gl_renderer = weston_load_module("gl-renderer.so",
922 "gl_renderer_interface");
923 if (!gl_renderer) {
924 weston_log("could not load gl renderer\n");
925 goto out_launcher;
926 }
927
928 if (gl_renderer->create(&compositor->base, EGL_DEFAULT_DISPLAY,
929 gl_renderer->opaque_attribs,
930 NULL) < 0) {
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300931 weston_log("gl_renderer_create failed.\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700932 goto out_launcher;
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300933 }
934 }
Philip Withnall4f499172013-02-02 12:02:32 +0000935
936 if (fbdev_output_create(compositor, param->device) < 0)
937 goto out_pixman;
938
Rob Bradford2387fde2013-05-31 18:09:57 +0100939 udev_input_init(&compositor->input, &compositor->base, compositor->udev, seat_id);
Philip Withnall4f499172013-02-02 12:02:32 +0000940
941 return &compositor->base;
942
943out_pixman:
944 compositor->base.renderer->destroy(&compositor->base);
945
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700946out_launcher:
947 weston_launcher_destroy(compositor->base.launcher);
Philip Withnall4f499172013-02-02 12:02:32 +0000948
949out_udev:
950 udev_unref(compositor->udev);
951
952out_compositor:
953 weston_compositor_shutdown(&compositor->base);
954
955out_free:
956 free(compositor);
957
958 return NULL;
959}
960
961WL_EXPORT struct weston_compositor *
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500962backend_init(struct wl_display *display, int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400963 struct weston_config *config)
Philip Withnall4f499172013-02-02 12:02:32 +0000964{
965 /* TODO: Ideally, available frame buffers should be enumerated using
966 * udev, rather than passing a device node in as a parameter. */
967 struct fbdev_parameters param = {
968 .tty = 0, /* default to current tty */
969 .device = "/dev/fb0", /* default frame buffer */
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300970 .use_gl = 0,
Philip Withnall4f499172013-02-02 12:02:32 +0000971 };
972
973 const struct weston_option fbdev_options[] = {
974 { WESTON_OPTION_INTEGER, "tty", 0, &param.tty },
975 { WESTON_OPTION_STRING, "device", 0, &param.device },
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300976 { WESTON_OPTION_BOOLEAN, "use-gl", 0, &param.use_gl },
Philip Withnall4f499172013-02-02 12:02:32 +0000977 };
978
979 parse_options(fbdev_options, ARRAY_LENGTH(fbdev_options), argc, argv);
980
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400981 return fbdev_compositor_create(display, argc, argv, config, &param);
Philip Withnall4f499172013-02-02 12:02:32 +0000982}