blob: 6f5e62e3869c69b33439eb5fc1b2452305c35258 [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"
Kristian Høgsberg7e597f22013-02-18 16:35:26 -050045#include "udev-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;
53 struct tty *tty;
Rob Bradfordd355b802013-05-31 18:09:55 +010054 struct udev_input input;
Adrian Negreanu4aa756d2013-09-06 15:16:09 +030055 int use_pixman;
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
Kristian Høgsberg7e597f22013-02-18 16:35:26 -050098static const char default_seat[] = "seat0";
99
Philip Withnall4f499172013-02-02 12:02:32 +0000100static inline struct fbdev_output *
101to_fbdev_output(struct weston_output *base)
102{
103 return container_of(base, struct fbdev_output, base);
104}
105
Philip Withnall4f499172013-02-02 12:02:32 +0000106static inline struct fbdev_compositor *
107to_fbdev_compositor(struct weston_compositor *base)
108{
109 return container_of(base, struct fbdev_compositor, base);
110}
111
112static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200113fbdev_output_start_repaint_loop(struct weston_output *output)
114{
115 uint32_t msec;
116 struct timeval tv;
117
118 gettimeofday(&tv, NULL);
119 msec = tv.tv_sec * 1000 + tv.tv_usec / 1000;
120 weston_output_finish_frame(output, msec);
121}
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
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300197static void
198fbdev_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 }
215}
216
Philip Withnall4f499172013-02-02 12:02:32 +0000217static int
218finish_frame_handler(void *data)
219{
220 struct fbdev_output *output = data;
Philip Withnall4f499172013-02-02 12:02:32 +0000221
Jonas Ådahle5a12252013-04-05 23:07:11 +0200222 fbdev_output_start_repaint_loop(&output->base);
Philip Withnall4f499172013-02-02 12:02:32 +0000223
224 return 1;
225}
226
227static pixman_format_code_t
228calculate_pixman_format(struct fb_var_screeninfo *vinfo,
229 struct fb_fix_screeninfo *finfo)
230{
231 /* Calculate the pixman format supported by the frame buffer from the
232 * buffer's metadata. Return 0 if no known pixman format is supported
233 * (since this has depth 0 it's guaranteed to not conflict with any
234 * actual pixman format).
235 *
236 * Documentation on the vinfo and finfo structures:
237 * http://www.mjmwired.net/kernel/Documentation/fb/api.txt
238 *
239 * TODO: Try a bit harder to support other formats, including setting
240 * the preferred format in the hardware. */
241 int type;
242
243 weston_log("Calculating pixman format from:\n"
244 STAMP_SPACE " - type: %i (aux: %i)\n"
245 STAMP_SPACE " - visual: %i\n"
246 STAMP_SPACE " - bpp: %i (grayscale: %i)\n"
247 STAMP_SPACE " - red: offset: %i, length: %i, MSB: %i\n"
248 STAMP_SPACE " - green: offset: %i, length: %i, MSB: %i\n"
249 STAMP_SPACE " - blue: offset: %i, length: %i, MSB: %i\n"
250 STAMP_SPACE " - transp: offset: %i, length: %i, MSB: %i\n",
251 finfo->type, finfo->type_aux, finfo->visual,
252 vinfo->bits_per_pixel, vinfo->grayscale,
253 vinfo->red.offset, vinfo->red.length, vinfo->red.msb_right,
254 vinfo->green.offset, vinfo->green.length,
255 vinfo->green.msb_right,
256 vinfo->blue.offset, vinfo->blue.length,
257 vinfo->blue.msb_right,
258 vinfo->transp.offset, vinfo->transp.length,
259 vinfo->transp.msb_right);
260
261 /* We only handle packed formats at the moment. */
262 if (finfo->type != FB_TYPE_PACKED_PIXELS)
263 return 0;
264
265 /* We only handle true-colour frame buffers at the moment. */
Marc Chalainffbddff2013-09-03 16:47:43 +0200266 switch(finfo->visual) {
267 case FB_VISUAL_TRUECOLOR:
268 case FB_VISUAL_DIRECTCOLOR:
269 if (vinfo->grayscale != 0)
270 return 0;
271 break;
272 default:
273 return 0;
274 }
Philip Withnall4f499172013-02-02 12:02:32 +0000275
276 /* We only support formats with MSBs on the left. */
277 if (vinfo->red.msb_right != 0 || vinfo->green.msb_right != 0 ||
278 vinfo->blue.msb_right != 0)
279 return 0;
280
281 /* Work out the format type from the offsets. We only support RGBA and
282 * ARGB at the moment. */
283 type = PIXMAN_TYPE_OTHER;
284
285 if ((vinfo->transp.offset >= vinfo->red.offset ||
286 vinfo->transp.length == 0) &&
287 vinfo->red.offset >= vinfo->green.offset &&
288 vinfo->green.offset >= vinfo->blue.offset)
289 type = PIXMAN_TYPE_ARGB;
290 else if (vinfo->red.offset >= vinfo->green.offset &&
291 vinfo->green.offset >= vinfo->blue.offset &&
292 vinfo->blue.offset >= vinfo->transp.offset)
293 type = PIXMAN_TYPE_RGBA;
294
295 if (type == PIXMAN_TYPE_OTHER)
296 return 0;
297
298 /* Build the format. */
299 return PIXMAN_FORMAT(vinfo->bits_per_pixel, type,
300 vinfo->transp.length,
301 vinfo->red.length,
302 vinfo->green.length,
303 vinfo->blue.length);
304}
305
306static int
307calculate_refresh_rate(struct fb_var_screeninfo *vinfo)
308{
309 uint64_t quot;
310
311 /* Calculate monitor refresh rate. Default is 60 Hz. Units are mHz. */
312 quot = (vinfo->upper_margin + vinfo->lower_margin + vinfo->yres);
313 quot *= (vinfo->left_margin + vinfo->right_margin + vinfo->xres);
314 quot *= vinfo->pixclock;
315
316 if (quot > 0) {
317 uint64_t refresh_rate;
318
319 refresh_rate = 1000000000000000LLU / quot;
320 if (refresh_rate > 200000)
321 refresh_rate = 200000; /* cap at 200 Hz */
322
323 return refresh_rate;
324 }
325
326 return 60 * 1000; /* default to 60 Hz */
327}
328
329static int
330fbdev_query_screen_info(struct fbdev_output *output, int fd,
331 struct fbdev_screeninfo *info)
332{
333 struct fb_var_screeninfo varinfo;
334 struct fb_fix_screeninfo fixinfo;
335
336 /* Probe the device for screen information. */
337 if (ioctl(fd, FBIOGET_FSCREENINFO, &fixinfo) < 0 ||
338 ioctl(fd, FBIOGET_VSCREENINFO, &varinfo) < 0) {
339 return -1;
340 }
341
342 /* Store the pertinent data. */
343 info->x_resolution = varinfo.xres;
344 info->y_resolution = varinfo.yres;
345 info->width_mm = varinfo.width;
346 info->height_mm = varinfo.height;
347 info->bits_per_pixel = varinfo.bits_per_pixel;
348
349 info->buffer_length = fixinfo.smem_len;
350 info->line_length = fixinfo.line_length;
351 strncpy(info->id, fixinfo.id, sizeof(info->id) / sizeof(*info->id));
352
353 info->pixel_format = calculate_pixman_format(&varinfo, &fixinfo);
354 info->refresh_rate = calculate_refresh_rate(&varinfo);
355
356 if (info->pixel_format == 0) {
357 weston_log("Frame buffer uses an unsupported format.\n");
358 return -1;
359 }
360
361 return 1;
362}
363
364static int
365fbdev_set_screen_info(struct fbdev_output *output, int fd,
366 struct fbdev_screeninfo *info)
367{
368 struct fb_var_screeninfo varinfo;
369
370 /* Grab the current screen information. */
371 if (ioctl(fd, FBIOGET_VSCREENINFO, &varinfo) < 0) {
372 return -1;
373 }
374
375 /* Update the information. */
376 varinfo.xres = info->x_resolution;
377 varinfo.yres = info->y_resolution;
378 varinfo.width = info->width_mm;
379 varinfo.height = info->height_mm;
380 varinfo.bits_per_pixel = info->bits_per_pixel;
381
382 /* Try to set up an ARGB (x8r8g8b8) pixel format. */
383 varinfo.grayscale = 0;
384 varinfo.transp.offset = 24;
385 varinfo.transp.length = 0;
386 varinfo.transp.msb_right = 0;
387 varinfo.red.offset = 16;
388 varinfo.red.length = 8;
389 varinfo.red.msb_right = 0;
390 varinfo.green.offset = 8;
391 varinfo.green.length = 8;
392 varinfo.green.msb_right = 0;
393 varinfo.blue.offset = 0;
394 varinfo.blue.length = 8;
395 varinfo.blue.msb_right = 0;
396
397 /* Set the device's screen information. */
398 if (ioctl(fd, FBIOPUT_VSCREENINFO, &varinfo) < 0) {
399 return -1;
400 }
401
402 return 1;
403}
404
405static void fbdev_frame_buffer_destroy(struct fbdev_output *output);
406
407/* Returns an FD for the frame buffer device. */
408static int
409fbdev_frame_buffer_open(struct fbdev_output *output, const char *fb_dev,
410 struct fbdev_screeninfo *screen_info)
411{
412 int fd = -1;
413
414 weston_log("Opening fbdev frame buffer.\n");
415
416 /* Open the frame buffer device. */
417 fd = open(fb_dev, O_RDWR | O_CLOEXEC);
418 if (fd < 0) {
419 weston_log("Failed to open frame buffer device ‘%s’: %s\n",
420 fb_dev, strerror(errno));
421 return -1;
422 }
423
424 /* Grab the screen info. */
425 if (fbdev_query_screen_info(output, fd, screen_info) < 0) {
426 weston_log("Failed to get frame buffer info: %s\n",
427 strerror(errno));
428
429 close(fd);
430 return -1;
431 }
432
433 return fd;
434}
435
436/* Closes the FD on success or failure. */
437static int
438fbdev_frame_buffer_map(struct fbdev_output *output, int fd)
439{
440 int retval = -1;
441
442 weston_log("Mapping fbdev frame buffer.\n");
443
444 /* Map the frame buffer. Write-only mode, since we don't want to read
445 * anything back (because it's slow). */
446 output->fb = mmap(NULL, output->fb_info.buffer_length,
447 PROT_WRITE, MAP_SHARED, fd, 0);
448 if (output->fb == MAP_FAILED) {
449 weston_log("Failed to mmap frame buffer: %s\n",
450 strerror(errno));
451 goto out_close;
452 }
453
454 /* Create a pixman image to wrap the memory mapped frame buffer. */
455 output->hw_surface =
456 pixman_image_create_bits(output->fb_info.pixel_format,
457 output->fb_info.x_resolution,
458 output->fb_info.y_resolution,
459 output->fb,
460 output->fb_info.line_length);
461 if (output->hw_surface == NULL) {
462 weston_log("Failed to create surface for frame buffer.\n");
463 goto out_unmap;
464 }
465
466 /* Success! */
467 retval = 0;
468
469out_unmap:
470 if (retval != 0 && output->fb != NULL)
471 fbdev_frame_buffer_destroy(output);
472
473out_close:
474 if (fd >= 0)
475 close(fd);
476
477 return retval;
478}
479
480static void
481fbdev_frame_buffer_destroy(struct fbdev_output *output)
482{
483 weston_log("Destroying fbdev frame buffer.\n");
484
485 if (munmap(output->fb, output->fb_info.buffer_length) < 0)
486 weston_log("Failed to munmap frame buffer: %s\n",
487 strerror(errno));
488
489 output->fb = NULL;
490}
491
492static void fbdev_output_destroy(struct weston_output *base);
493static void fbdev_output_disable(struct weston_output *base);
494
495static int
496fbdev_output_create(struct fbdev_compositor *compositor,
497 const char *device)
498{
499 struct fbdev_output *output;
500 pixman_transform_t transform;
501 int fb_fd;
502 int shadow_width, shadow_height;
503 int width, height;
504 unsigned int bytes_per_pixel;
505 struct wl_event_loop *loop;
506
507 weston_log("Creating fbdev output.\n");
508
509 output = calloc(1, sizeof *output);
510 if (!output)
511 return -1;
512
513 output->compositor = compositor;
514 output->device = device;
515
516 /* Create the frame buffer. */
517 fb_fd = fbdev_frame_buffer_open(output, device, &output->fb_info);
518 if (fb_fd < 0) {
519 weston_log("Creating frame buffer failed.\n");
520 goto out_free;
521 }
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300522 if (compositor->use_pixman) {
523 if (fbdev_frame_buffer_map(output, fb_fd) < 0) {
524 weston_log("Mapping frame buffer failed.\n");
525 goto out_free;
526 }
Philip Withnall4f499172013-02-02 12:02:32 +0000527 }
528
Jonas Ådahle5a12252013-04-05 23:07:11 +0200529 output->base.start_repaint_loop = fbdev_output_start_repaint_loop;
Philip Withnall4f499172013-02-02 12:02:32 +0000530 output->base.repaint = fbdev_output_repaint;
531 output->base.destroy = fbdev_output_destroy;
532 output->base.assign_planes = NULL;
533 output->base.set_backlight = NULL;
534 output->base.set_dpms = NULL;
535 output->base.switch_mode = NULL;
536
537 /* only one static mode in list */
538 output->mode.flags =
539 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
540 output->mode.width = output->fb_info.x_resolution;
541 output->mode.height = output->fb_info.y_resolution;
542 output->mode.refresh = output->fb_info.refresh_rate;
543 wl_list_init(&output->base.mode_list);
544 wl_list_insert(&output->base.mode_list, &output->mode.link);
545
546 output->base.current = &output->mode;
547 output->base.origin = &output->mode;
548 output->base.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
549 output->base.make = "unknown";
550 output->base.model = output->fb_info.id;
551
552 weston_output_init(&output->base, &compositor->base,
553 0, 0, output->fb_info.width_mm,
554 output->fb_info.height_mm,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200555 WL_OUTPUT_TRANSFORM_NORMAL,
556 1);
Philip Withnall4f499172013-02-02 12:02:32 +0000557
558 width = output->fb_info.x_resolution;
559 height = output->fb_info.y_resolution;
560
561 pixman_transform_init_identity(&transform);
562 switch (output->base.transform) {
563 default:
564 case WL_OUTPUT_TRANSFORM_NORMAL:
565 shadow_width = width;
566 shadow_height = height;
567 pixman_transform_rotate(&transform,
568 NULL, 0, 0);
569 pixman_transform_translate(&transform, NULL,
570 0, 0);
571 break;
572 case WL_OUTPUT_TRANSFORM_180:
573 shadow_width = width;
574 shadow_height = height;
575 pixman_transform_rotate(&transform,
576 NULL, -pixman_fixed_1, 0);
577 pixman_transform_translate(NULL, &transform,
578 pixman_int_to_fixed(shadow_width),
579 pixman_int_to_fixed(shadow_height));
580 break;
581 case WL_OUTPUT_TRANSFORM_270:
582 shadow_width = height;
583 shadow_height = width;
584 pixman_transform_rotate(&transform,
585 NULL, 0, pixman_fixed_1);
586 pixman_transform_translate(&transform,
587 NULL,
588 pixman_int_to_fixed(shadow_width),
589 0);
590 break;
591 case WL_OUTPUT_TRANSFORM_90:
592 shadow_width = height;
593 shadow_height = width;
594 pixman_transform_rotate(&transform,
595 NULL, 0, -pixman_fixed_1);
596 pixman_transform_translate(&transform,
597 NULL,
598 0,
599 pixman_int_to_fixed(shadow_height));
600 break;
601 }
602
603 bytes_per_pixel = output->fb_info.bits_per_pixel / 8;
604
605 output->shadow_buf = malloc(width * height * bytes_per_pixel);
606 output->shadow_surface =
607 pixman_image_create_bits(output->fb_info.pixel_format,
608 shadow_width, shadow_height,
609 output->shadow_buf,
610 shadow_width * bytes_per_pixel);
611 if (output->shadow_buf == NULL || output->shadow_surface == NULL) {
612 weston_log("Failed to create surface for frame buffer.\n");
613 goto out_hw_surface;
614 }
615
616 /* No need in transform for normal output */
617 if (output->base.transform != WL_OUTPUT_TRANSFORM_NORMAL)
618 pixman_image_set_transform(output->shadow_surface, &transform);
619
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300620 if (compositor->use_pixman) {
621 if (pixman_renderer_output_create(&output->base) < 0)
622 goto out_shadow_surface;
623 } else {
624 setenv("HYBRIS_EGLPLATFORM", "wayland", 1);
625 if (gl_renderer_output_create(&output->base,
626 (EGLNativeWindowType)NULL) < 0) {
627 weston_log("gl_renderer_output_create failed.\n");
628 goto out_shadow_surface;
629 }
630 }
631
Philip Withnall4f499172013-02-02 12:02:32 +0000632
633 loop = wl_display_get_event_loop(compositor->base.wl_display);
634 output->finish_frame_timer =
635 wl_event_loop_add_timer(loop, finish_frame_handler, output);
636
637 wl_list_insert(compositor->base.output_list.prev, &output->base.link);
638
639 weston_log("fbdev output %d×%d px\n",
640 output->mode.width, output->mode.height);
641 weston_log_continue(STAMP_SPACE "guessing %d Hz and 96 dpi\n",
642 output->mode.refresh / 1000);
643
644 return 0;
645
646out_shadow_surface:
647 pixman_image_unref(output->shadow_surface);
648 output->shadow_surface = NULL;
649out_hw_surface:
650 free(output->shadow_buf);
651 pixman_image_unref(output->hw_surface);
652 output->hw_surface = NULL;
653 weston_output_destroy(&output->base);
654 fbdev_frame_buffer_destroy(output);
655out_free:
656 free(output);
657
658 return -1;
659}
660
661static void
662fbdev_output_destroy(struct weston_output *base)
663{
664 struct fbdev_output *output = to_fbdev_output(base);
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300665 struct fbdev_compositor *compositor = output->compositor;
Philip Withnall4f499172013-02-02 12:02:32 +0000666
667 weston_log("Destroying fbdev output.\n");
668
669 /* Close the frame buffer. */
670 fbdev_output_disable(base);
671
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300672 if (compositor->use_pixman) {
673 if (base->renderer_state != NULL)
674 pixman_renderer_output_destroy(base);
Philip Withnall4f499172013-02-02 12:02:32 +0000675
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300676 if (output->shadow_surface != NULL) {
677 pixman_image_unref(output->shadow_surface);
678 output->shadow_surface = NULL;
679 }
Philip Withnall4f499172013-02-02 12:02:32 +0000680
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300681 if (output->shadow_buf != NULL) {
682 free(output->shadow_buf);
683 output->shadow_buf = NULL;
684 }
685 } else {
686 gl_renderer_output_destroy(base);
Philip Withnall4f499172013-02-02 12:02:32 +0000687 }
688
689 /* Remove the output. */
690 wl_list_remove(&output->base.link);
691 weston_output_destroy(&output->base);
692
693 free(output);
694}
695
696/* strcmp()-style return values. */
697static int
698compare_screen_info (const struct fbdev_screeninfo *a,
699 const struct fbdev_screeninfo *b)
700{
701 if (a->x_resolution == b->x_resolution &&
702 a->y_resolution == b->y_resolution &&
703 a->width_mm == b->width_mm &&
704 a->height_mm == b->height_mm &&
705 a->bits_per_pixel == b->bits_per_pixel &&
706 a->pixel_format == b->pixel_format &&
707 a->refresh_rate == b->refresh_rate)
708 return 0;
709
710 return 1;
711}
712
713static int
714fbdev_output_reenable(struct fbdev_compositor *compositor,
715 struct weston_output *base)
716{
717 struct fbdev_output *output = to_fbdev_output(base);
718 struct fbdev_screeninfo new_screen_info;
719 int fb_fd;
Rob Bradfordf8ef42f2013-07-26 16:29:38 +0100720 const char *device;
Philip Withnall4f499172013-02-02 12:02:32 +0000721
722 weston_log("Re-enabling fbdev output.\n");
723
724 /* Create the frame buffer. */
725 fb_fd = fbdev_frame_buffer_open(output, output->device,
726 &new_screen_info);
727 if (fb_fd < 0) {
728 weston_log("Creating frame buffer failed.\n");
729 goto err;
730 }
731
732 /* Check whether the frame buffer details have changed since we were
733 * disabled. */
734 if (compare_screen_info (&output->fb_info, &new_screen_info) != 0) {
735 /* Perform a mode-set to restore the old mode. */
736 if (fbdev_set_screen_info(output, fb_fd,
737 &output->fb_info) < 0) {
738 weston_log("Failed to restore mode settings. "
739 "Attempting to re-open output anyway.\n");
740 }
741
Rob Bradford581b3fd2013-07-26 16:29:39 +0100742 close(fb_fd);
743
Philip Withnall4f499172013-02-02 12:02:32 +0000744 /* Remove and re-add the output so that resources depending on
745 * the frame buffer X/Y resolution (such as the shadow buffer)
746 * are re-initialised. */
Rob Bradfordf8ef42f2013-07-26 16:29:38 +0100747 device = output->device;
Philip Withnall4f499172013-02-02 12:02:32 +0000748 fbdev_output_destroy(base);
Rob Bradfordf8ef42f2013-07-26 16:29:38 +0100749 fbdev_output_create(compositor, device);
Philip Withnall4f499172013-02-02 12:02:32 +0000750
751 return 0;
752 }
753
754 /* Map the device if it has the same details as before. */
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300755 if (compositor->use_pixman) {
756 if (fbdev_frame_buffer_map(output, fb_fd) < 0) {
757 weston_log("Mapping frame buffer failed.\n");
758 goto err;
759 }
Philip Withnall4f499172013-02-02 12:02:32 +0000760 }
761
762 return 0;
763
764err:
765 return -1;
766}
767
768/* NOTE: This leaves output->fb_info populated, caching data so that if
769 * fbdev_output_reenable() is called again, it can determine whether a mode-set
770 * is needed. */
771static void
772fbdev_output_disable(struct weston_output *base)
773{
774 struct fbdev_output *output = to_fbdev_output(base);
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300775 struct fbdev_compositor *compositor = output->compositor;
Philip Withnall4f499172013-02-02 12:02:32 +0000776
777 weston_log("Disabling fbdev output.\n");
778
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300779 if ( ! compositor->use_pixman) return;
780
Philip Withnall4f499172013-02-02 12:02:32 +0000781 if (output->hw_surface != NULL) {
782 pixman_image_unref(output->hw_surface);
783 output->hw_surface = NULL;
784 }
785
786 fbdev_frame_buffer_destroy(output);
787}
788
789static void
Philip Withnall4f499172013-02-02 12:02:32 +0000790fbdev_compositor_destroy(struct weston_compositor *base)
791{
792 struct fbdev_compositor *compositor = to_fbdev_compositor(base);
Philip Withnall4f499172013-02-02 12:02:32 +0000793
Rob Bradfordd355b802013-05-31 18:09:55 +0100794 udev_input_destroy(&compositor->input);
Philip Withnall4f499172013-02-02 12:02:32 +0000795
796 /* Destroy the output. */
797 weston_compositor_shutdown(&compositor->base);
798
799 /* Chain up. */
800 compositor->base.renderer->destroy(&compositor->base);
801 tty_destroy(compositor->tty);
802
803 free(compositor);
804}
805
806static void
807vt_func(struct weston_compositor *base, int event)
808{
809 struct fbdev_compositor *compositor = to_fbdev_compositor(base);
Philip Withnall4f499172013-02-02 12:02:32 +0000810 struct weston_output *output;
811
812 switch (event) {
813 case TTY_ENTER_VT:
814 weston_log("entering VT\n");
815 compositor->base.focus = 1;
816 compositor->base.state = compositor->prev_state;
817
818 wl_list_for_each(output, &compositor->base.output_list, link) {
819 fbdev_output_reenable(compositor, output);
820 }
821
822 weston_compositor_damage_all(&compositor->base);
823
Rob Bradfordd355b802013-05-31 18:09:55 +0100824 udev_input_enable(&compositor->input, compositor->udev);
Philip Withnall4f499172013-02-02 12:02:32 +0000825 break;
826 case TTY_LEAVE_VT:
827 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
834 compositor->base.focus = 0;
835 compositor->prev_state = compositor->base.state;
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +0100836 weston_compositor_offscreen(&compositor->base);
Philip Withnall4f499172013-02-02 12:02:32 +0000837
838 /* If we have a repaint scheduled (from the idle handler), make
839 * sure we cancel that so we don't try to pageflip when we're
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +0100840 * vt switched away. The OFFSCREEN state will prevent
Philip Withnall4f499172013-02-02 12:02:32 +0000841 * further attemps at repainting. When we switch
842 * back, we schedule a repaint, which will process
843 * pending frame callbacks. */
844
845 wl_list_for_each(output,
846 &compositor->base.output_list, link) {
847 output->repaint_needed = 0;
848 }
849
850 break;
851 };
852}
853
854static void
855fbdev_restore(struct weston_compositor *base)
856{
857 struct fbdev_compositor *compositor = to_fbdev_compositor(base);
858
859 tty_reset(compositor->tty);
860}
861
862static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400863switch_vt_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Philip Withnall4f499172013-02-02 12:02:32 +0000864{
865 struct fbdev_compositor *ec = data;
866
867 tty_activate_vt(ec->tty, key - KEY_F1 + 1);
868}
869
870static struct weston_compositor *
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500871fbdev_compositor_create(struct wl_display *display, int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400872 struct weston_config *config,
873 struct fbdev_parameters *param)
Philip Withnall4f499172013-02-02 12:02:32 +0000874{
875 struct fbdev_compositor *compositor;
Rob Bradford2387fde2013-05-31 18:09:57 +0100876 const char *seat_id = default_seat;
Philip Withnall4f499172013-02-02 12:02:32 +0000877 uint32_t key;
878
879 weston_log("initializing fbdev backend\n");
880
881 compositor = calloc(1, sizeof *compositor);
882 if (compositor == NULL)
883 return NULL;
884
885 if (weston_compositor_init(&compositor->base, display, argc, argv,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400886 config) < 0)
Philip Withnall4f499172013-02-02 12:02:32 +0000887 goto out_free;
888
Adrian Negreanuc8384232013-07-28 18:27:23 +0300889 /* Check if we run fbdev-backend using weston-launch */
890 compositor->base.launcher_sock =
891 weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
892 if (compositor->base.launcher_sock == -1 && geteuid() != 0) {
893 weston_log("fatal: fbdev backend should be run "
894 "using weston-launch binary or as root\n");
895 goto out_compositor;
896 }
897
Philip Withnall4f499172013-02-02 12:02:32 +0000898 compositor->udev = udev_new();
899 if (compositor->udev == NULL) {
900 weston_log("Failed to initialize udev context.\n");
901 goto out_compositor;
902 }
903
904 /* Set up the TTY. */
905 compositor->tty = tty_create(&compositor->base, vt_func, param->tty);
906 if (!compositor->tty) {
907 weston_log("Failed to initialize tty.\n");
908 goto out_udev;
909 }
910
911 compositor->base.destroy = fbdev_compositor_destroy;
912 compositor->base.restore = fbdev_restore;
913
914 compositor->base.focus = 1;
915 compositor->prev_state = WESTON_COMPOSITOR_ACTIVE;
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300916 compositor->use_pixman = !param->use_gl;
Philip Withnall4f499172013-02-02 12:02:32 +0000917
918 for (key = KEY_F1; key < KEY_F9; key++)
919 weston_compositor_add_key_binding(&compositor->base, key,
920 MODIFIER_CTRL | MODIFIER_ALT,
921 switch_vt_binding,
922 compositor);
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300923 if (compositor->use_pixman) {
924 if (pixman_renderer_init(&compositor->base) < 0)
925 goto out_tty;
926 } else {
927 if (gl_renderer_create(&compositor->base, EGL_DEFAULT_DISPLAY,
928 gl_renderer_opaque_attribs, NULL) < 0) {
929 weston_log("gl_renderer_create failed.\n");
930 goto out_tty;
931 }
932 }
Philip Withnall4f499172013-02-02 12:02:32 +0000933
934 if (fbdev_output_create(compositor, param->device) < 0)
935 goto out_pixman;
936
Rob Bradford2387fde2013-05-31 18:09:57 +0100937 udev_input_init(&compositor->input, &compositor->base, compositor->udev, seat_id);
Philip Withnall4f499172013-02-02 12:02:32 +0000938
939 return &compositor->base;
940
941out_pixman:
942 compositor->base.renderer->destroy(&compositor->base);
943
944out_tty:
945 tty_destroy(compositor->tty);
946
947out_udev:
948 udev_unref(compositor->udev);
949
950out_compositor:
951 weston_compositor_shutdown(&compositor->base);
952
953out_free:
954 free(compositor);
955
956 return NULL;
957}
958
959WL_EXPORT struct weston_compositor *
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500960backend_init(struct wl_display *display, int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400961 struct weston_config *config)
Philip Withnall4f499172013-02-02 12:02:32 +0000962{
963 /* TODO: Ideally, available frame buffers should be enumerated using
964 * udev, rather than passing a device node in as a parameter. */
965 struct fbdev_parameters param = {
966 .tty = 0, /* default to current tty */
967 .device = "/dev/fb0", /* default frame buffer */
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300968 .use_gl = 0,
Philip Withnall4f499172013-02-02 12:02:32 +0000969 };
970
971 const struct weston_option fbdev_options[] = {
972 { WESTON_OPTION_INTEGER, "tty", 0, &param.tty },
973 { WESTON_OPTION_STRING, "device", 0, &param.device },
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300974 { WESTON_OPTION_BOOLEAN, "use-gl", 0, &param.use_gl },
Philip Withnall4f499172013-02-02 12:02:32 +0000975 };
976
977 parse_options(fbdev_options, ARRAY_LENGTH(fbdev_options), argc, argv);
978
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400979 return fbdev_compositor_create(display, argc, argv, config, &param);
Philip Withnall4f499172013-02-02 12:02:32 +0000980}