blob: 9fa42177965ac4efc1a6cba5981ff90d75a1a67b [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;
Kristian Høgsberg61741a22013-09-17 16:02:57 -070056 struct wl_listener session_listener;
Philip Withnall4f499172013-02-02 12:02:32 +000057};
58
59struct fbdev_screeninfo {
60 unsigned int x_resolution; /* pixels, visible area */
61 unsigned int y_resolution; /* pixels, visible area */
62 unsigned int width_mm; /* visible screen width in mm */
63 unsigned int height_mm; /* visible screen height in mm */
64 unsigned int bits_per_pixel;
65
66 size_t buffer_length; /* length of frame buffer memory in bytes */
67 size_t line_length; /* length of a line in bytes */
68 char id[16]; /* screen identifier */
69
70 pixman_format_code_t pixel_format; /* frame buffer pixel format */
71 unsigned int refresh_rate; /* Hertz */
72};
73
74struct fbdev_output {
75 struct fbdev_compositor *compositor;
76 struct weston_output base;
77
78 struct weston_mode mode;
79 struct wl_event_source *finish_frame_timer;
80
81 /* Frame buffer details. */
82 const char *device; /* ownership shared with fbdev_parameters */
83 struct fbdev_screeninfo fb_info;
84 void *fb; /* length is fb_info.buffer_length */
85
86 /* pixman details. */
87 pixman_image_t *hw_surface;
88 pixman_image_t *shadow_surface;
89 void *shadow_buf;
90 uint8_t depth;
91};
92
Philip Withnall4f499172013-02-02 12:02:32 +000093struct fbdev_parameters {
94 int tty;
95 char *device;
Adrian Negreanu4aa756d2013-09-06 15:16:09 +030096 int use_gl;
Philip Withnall4f499172013-02-02 12:02:32 +000097};
98
Kristian Høgsberg7e597f22013-02-18 16:35:26 -050099static const char default_seat[] = "seat0";
100
Philip Withnall4f499172013-02-02 12:02:32 +0000101static inline struct fbdev_output *
102to_fbdev_output(struct weston_output *base)
103{
104 return container_of(base, struct fbdev_output, base);
105}
106
Philip Withnall4f499172013-02-02 12:02:32 +0000107static inline struct fbdev_compositor *
108to_fbdev_compositor(struct weston_compositor *base)
109{
110 return container_of(base, struct fbdev_compositor, base);
111}
112
113static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200114fbdev_output_start_repaint_loop(struct weston_output *output)
115{
116 uint32_t msec;
117 struct timeval tv;
118
119 gettimeofday(&tv, NULL);
120 msec = tv.tv_sec * 1000 + tv.tv_usec / 1000;
121 weston_output_finish_frame(output, msec);
122}
123
124static void
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300125fbdev_output_repaint_pixman(struct weston_output *base, pixman_region32_t *damage)
Philip Withnall4f499172013-02-02 12:02:32 +0000126{
127 struct fbdev_output *output = to_fbdev_output(base);
128 struct weston_compositor *ec = output->base.compositor;
129 pixman_box32_t *rects;
130 int nrects, i, src_x, src_y, x1, y1, x2, y2, width, height;
131
132 /* Repaint the damaged region onto the back buffer. */
133 pixman_renderer_output_set_buffer(base, output->shadow_surface);
134 ec->renderer->repaint_output(base, damage);
135
136 /* Transform and composite onto the frame buffer. */
137 width = pixman_image_get_width(output->shadow_surface);
138 height = pixman_image_get_height(output->shadow_surface);
139 rects = pixman_region32_rectangles(damage, &nrects);
140
141 for (i = 0; i < nrects; i++) {
142 switch (base->transform) {
143 default:
144 case WL_OUTPUT_TRANSFORM_NORMAL:
145 x1 = rects[i].x1;
146 x2 = rects[i].x2;
147 y1 = rects[i].y1;
148 y2 = rects[i].y2;
149 break;
150 case WL_OUTPUT_TRANSFORM_180:
151 x1 = width - rects[i].x2;
152 x2 = width - rects[i].x1;
153 y1 = height - rects[i].y2;
154 y2 = height - rects[i].y1;
155 break;
156 case WL_OUTPUT_TRANSFORM_90:
157 x1 = height - rects[i].y2;
158 x2 = height - rects[i].y1;
159 y1 = rects[i].x1;
160 y2 = rects[i].x2;
161 break;
162 case WL_OUTPUT_TRANSFORM_270:
163 x1 = rects[i].y1;
164 x2 = rects[i].y2;
165 y1 = width - rects[i].x2;
166 y2 = width - rects[i].x1;
167 break;
168 }
169 src_x = x1;
170 src_y = y1;
171
172 pixman_image_composite32(PIXMAN_OP_SRC,
173 output->shadow_surface, /* src */
174 NULL /* mask */,
175 output->hw_surface, /* dest */
176 src_x, src_y, /* src_x, src_y */
177 0, 0, /* mask_x, mask_y */
178 x1, y1, /* dest_x, dest_y */
179 x2 - x1, /* width */
180 y2 - y1 /* height */);
181 }
182
183 /* Update the damage region. */
184 pixman_region32_subtract(&ec->primary_plane.damage,
185 &ec->primary_plane.damage, damage);
186
187 /* Schedule the end of the frame. We do not sync this to the frame
188 * buffer clock because users who want that should be using the DRM
189 * compositor. FBIO_WAITFORVSYNC blocks and FB_ACTIVATE_VBL requires
190 * panning, which is broken in most kernel drivers.
191 *
192 * Finish the frame synchronised to the specified refresh rate. The
193 * refresh rate is given in mHz and the interval in ms. */
194 wl_event_source_timer_update(output->finish_frame_timer,
195 1000000 / output->mode.refresh);
196}
197
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300198static void
199fbdev_output_repaint(struct weston_output *base, pixman_region32_t *damage)
200{
201 struct fbdev_output *output = to_fbdev_output(base);
202 struct fbdev_compositor *fbc = output->compositor;
203 struct weston_compositor *ec = & fbc->base;
204
205 if (fbc->use_pixman) {
206 fbdev_output_repaint_pixman(base,damage);
207 } else {
208 ec->renderer->repaint_output(base, damage);
209 /* Update the damage region. */
210 pixman_region32_subtract(&ec->primary_plane.damage,
211 &ec->primary_plane.damage, damage);
212
213 wl_event_source_timer_update(output->finish_frame_timer,
214 1000000 / output->mode.refresh);
215 }
216}
217
Philip Withnall4f499172013-02-02 12:02:32 +0000218static int
219finish_frame_handler(void *data)
220{
221 struct fbdev_output *output = data;
Philip Withnall4f499172013-02-02 12:02:32 +0000222
Jonas Ådahle5a12252013-04-05 23:07:11 +0200223 fbdev_output_start_repaint_loop(&output->base);
Philip Withnall4f499172013-02-02 12:02:32 +0000224
225 return 1;
226}
227
228static pixman_format_code_t
229calculate_pixman_format(struct fb_var_screeninfo *vinfo,
230 struct fb_fix_screeninfo *finfo)
231{
232 /* Calculate the pixman format supported by the frame buffer from the
233 * buffer's metadata. Return 0 if no known pixman format is supported
234 * (since this has depth 0 it's guaranteed to not conflict with any
235 * actual pixman format).
236 *
237 * Documentation on the vinfo and finfo structures:
238 * http://www.mjmwired.net/kernel/Documentation/fb/api.txt
239 *
240 * TODO: Try a bit harder to support other formats, including setting
241 * the preferred format in the hardware. */
242 int type;
243
244 weston_log("Calculating pixman format from:\n"
245 STAMP_SPACE " - type: %i (aux: %i)\n"
246 STAMP_SPACE " - visual: %i\n"
247 STAMP_SPACE " - bpp: %i (grayscale: %i)\n"
248 STAMP_SPACE " - red: offset: %i, length: %i, MSB: %i\n"
249 STAMP_SPACE " - green: offset: %i, length: %i, MSB: %i\n"
250 STAMP_SPACE " - blue: offset: %i, length: %i, MSB: %i\n"
251 STAMP_SPACE " - transp: offset: %i, length: %i, MSB: %i\n",
252 finfo->type, finfo->type_aux, finfo->visual,
253 vinfo->bits_per_pixel, vinfo->grayscale,
254 vinfo->red.offset, vinfo->red.length, vinfo->red.msb_right,
255 vinfo->green.offset, vinfo->green.length,
256 vinfo->green.msb_right,
257 vinfo->blue.offset, vinfo->blue.length,
258 vinfo->blue.msb_right,
259 vinfo->transp.offset, vinfo->transp.length,
260 vinfo->transp.msb_right);
261
262 /* We only handle packed formats at the moment. */
263 if (finfo->type != FB_TYPE_PACKED_PIXELS)
264 return 0;
265
266 /* We only handle true-colour frame buffers at the moment. */
Marc Chalainffbddff2013-09-03 16:47:43 +0200267 switch(finfo->visual) {
268 case FB_VISUAL_TRUECOLOR:
269 case FB_VISUAL_DIRECTCOLOR:
270 if (vinfo->grayscale != 0)
271 return 0;
272 break;
273 default:
274 return 0;
275 }
Philip Withnall4f499172013-02-02 12:02:32 +0000276
277 /* We only support formats with MSBs on the left. */
278 if (vinfo->red.msb_right != 0 || vinfo->green.msb_right != 0 ||
279 vinfo->blue.msb_right != 0)
280 return 0;
281
282 /* Work out the format type from the offsets. We only support RGBA and
283 * ARGB at the moment. */
284 type = PIXMAN_TYPE_OTHER;
285
286 if ((vinfo->transp.offset >= vinfo->red.offset ||
287 vinfo->transp.length == 0) &&
288 vinfo->red.offset >= vinfo->green.offset &&
289 vinfo->green.offset >= vinfo->blue.offset)
290 type = PIXMAN_TYPE_ARGB;
291 else if (vinfo->red.offset >= vinfo->green.offset &&
292 vinfo->green.offset >= vinfo->blue.offset &&
293 vinfo->blue.offset >= vinfo->transp.offset)
294 type = PIXMAN_TYPE_RGBA;
295
296 if (type == PIXMAN_TYPE_OTHER)
297 return 0;
298
299 /* Build the format. */
300 return PIXMAN_FORMAT(vinfo->bits_per_pixel, type,
301 vinfo->transp.length,
302 vinfo->red.length,
303 vinfo->green.length,
304 vinfo->blue.length);
305}
306
307static int
308calculate_refresh_rate(struct fb_var_screeninfo *vinfo)
309{
310 uint64_t quot;
311
312 /* Calculate monitor refresh rate. Default is 60 Hz. Units are mHz. */
313 quot = (vinfo->upper_margin + vinfo->lower_margin + vinfo->yres);
314 quot *= (vinfo->left_margin + vinfo->right_margin + vinfo->xres);
315 quot *= vinfo->pixclock;
316
317 if (quot > 0) {
318 uint64_t refresh_rate;
319
320 refresh_rate = 1000000000000000LLU / quot;
321 if (refresh_rate > 200000)
322 refresh_rate = 200000; /* cap at 200 Hz */
323
324 return refresh_rate;
325 }
326
327 return 60 * 1000; /* default to 60 Hz */
328}
329
330static int
331fbdev_query_screen_info(struct fbdev_output *output, int fd,
332 struct fbdev_screeninfo *info)
333{
334 struct fb_var_screeninfo varinfo;
335 struct fb_fix_screeninfo fixinfo;
336
337 /* Probe the device for screen information. */
338 if (ioctl(fd, FBIOGET_FSCREENINFO, &fixinfo) < 0 ||
339 ioctl(fd, FBIOGET_VSCREENINFO, &varinfo) < 0) {
340 return -1;
341 }
342
343 /* Store the pertinent data. */
344 info->x_resolution = varinfo.xres;
345 info->y_resolution = varinfo.yres;
346 info->width_mm = varinfo.width;
347 info->height_mm = varinfo.height;
348 info->bits_per_pixel = varinfo.bits_per_pixel;
349
350 info->buffer_length = fixinfo.smem_len;
351 info->line_length = fixinfo.line_length;
352 strncpy(info->id, fixinfo.id, sizeof(info->id) / sizeof(*info->id));
353
354 info->pixel_format = calculate_pixman_format(&varinfo, &fixinfo);
355 info->refresh_rate = calculate_refresh_rate(&varinfo);
356
357 if (info->pixel_format == 0) {
358 weston_log("Frame buffer uses an unsupported format.\n");
359 return -1;
360 }
361
362 return 1;
363}
364
365static int
366fbdev_set_screen_info(struct fbdev_output *output, int fd,
367 struct fbdev_screeninfo *info)
368{
369 struct fb_var_screeninfo varinfo;
370
371 /* Grab the current screen information. */
372 if (ioctl(fd, FBIOGET_VSCREENINFO, &varinfo) < 0) {
373 return -1;
374 }
375
376 /* Update the information. */
377 varinfo.xres = info->x_resolution;
378 varinfo.yres = info->y_resolution;
379 varinfo.width = info->width_mm;
380 varinfo.height = info->height_mm;
381 varinfo.bits_per_pixel = info->bits_per_pixel;
382
383 /* Try to set up an ARGB (x8r8g8b8) pixel format. */
384 varinfo.grayscale = 0;
385 varinfo.transp.offset = 24;
386 varinfo.transp.length = 0;
387 varinfo.transp.msb_right = 0;
388 varinfo.red.offset = 16;
389 varinfo.red.length = 8;
390 varinfo.red.msb_right = 0;
391 varinfo.green.offset = 8;
392 varinfo.green.length = 8;
393 varinfo.green.msb_right = 0;
394 varinfo.blue.offset = 0;
395 varinfo.blue.length = 8;
396 varinfo.blue.msb_right = 0;
397
398 /* Set the device's screen information. */
399 if (ioctl(fd, FBIOPUT_VSCREENINFO, &varinfo) < 0) {
400 return -1;
401 }
402
403 return 1;
404}
405
406static void fbdev_frame_buffer_destroy(struct fbdev_output *output);
407
408/* Returns an FD for the frame buffer device. */
409static int
410fbdev_frame_buffer_open(struct fbdev_output *output, const char *fb_dev,
411 struct fbdev_screeninfo *screen_info)
412{
413 int fd = -1;
414
415 weston_log("Opening fbdev frame buffer.\n");
416
417 /* Open the frame buffer device. */
418 fd = open(fb_dev, O_RDWR | O_CLOEXEC);
419 if (fd < 0) {
420 weston_log("Failed to open frame buffer device ‘%s’: %s\n",
421 fb_dev, strerror(errno));
422 return -1;
423 }
424
425 /* Grab the screen info. */
426 if (fbdev_query_screen_info(output, fd, screen_info) < 0) {
427 weston_log("Failed to get frame buffer info: %s\n",
428 strerror(errno));
429
430 close(fd);
431 return -1;
432 }
433
434 return fd;
435}
436
437/* Closes the FD on success or failure. */
438static int
439fbdev_frame_buffer_map(struct fbdev_output *output, int fd)
440{
441 int retval = -1;
442
443 weston_log("Mapping fbdev frame buffer.\n");
444
445 /* Map the frame buffer. Write-only mode, since we don't want to read
446 * anything back (because it's slow). */
447 output->fb = mmap(NULL, output->fb_info.buffer_length,
448 PROT_WRITE, MAP_SHARED, fd, 0);
449 if (output->fb == MAP_FAILED) {
450 weston_log("Failed to mmap frame buffer: %s\n",
451 strerror(errno));
452 goto out_close;
453 }
454
455 /* Create a pixman image to wrap the memory mapped frame buffer. */
456 output->hw_surface =
457 pixman_image_create_bits(output->fb_info.pixel_format,
458 output->fb_info.x_resolution,
459 output->fb_info.y_resolution,
460 output->fb,
461 output->fb_info.line_length);
462 if (output->hw_surface == NULL) {
463 weston_log("Failed to create surface for frame buffer.\n");
464 goto out_unmap;
465 }
466
467 /* Success! */
468 retval = 0;
469
470out_unmap:
471 if (retval != 0 && output->fb != NULL)
472 fbdev_frame_buffer_destroy(output);
473
474out_close:
475 if (fd >= 0)
476 close(fd);
477
478 return retval;
479}
480
481static void
482fbdev_frame_buffer_destroy(struct fbdev_output *output)
483{
484 weston_log("Destroying fbdev frame buffer.\n");
485
486 if (munmap(output->fb, output->fb_info.buffer_length) < 0)
487 weston_log("Failed to munmap frame buffer: %s\n",
488 strerror(errno));
489
490 output->fb = NULL;
491}
492
493static void fbdev_output_destroy(struct weston_output *base);
494static void fbdev_output_disable(struct weston_output *base);
495
496static int
497fbdev_output_create(struct fbdev_compositor *compositor,
498 const char *device)
499{
500 struct fbdev_output *output;
501 pixman_transform_t transform;
502 int fb_fd;
503 int shadow_width, shadow_height;
504 int width, height;
505 unsigned int bytes_per_pixel;
506 struct wl_event_loop *loop;
507
508 weston_log("Creating fbdev output.\n");
509
510 output = calloc(1, sizeof *output);
511 if (!output)
512 return -1;
513
514 output->compositor = compositor;
515 output->device = device;
516
517 /* Create the frame buffer. */
518 fb_fd = fbdev_frame_buffer_open(output, device, &output->fb_info);
519 if (fb_fd < 0) {
520 weston_log("Creating frame buffer failed.\n");
521 goto out_free;
522 }
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300523 if (compositor->use_pixman) {
524 if (fbdev_frame_buffer_map(output, fb_fd) < 0) {
525 weston_log("Mapping frame buffer failed.\n");
526 goto out_free;
527 }
Philip Withnall4f499172013-02-02 12:02:32 +0000528 }
529
Jonas Ådahle5a12252013-04-05 23:07:11 +0200530 output->base.start_repaint_loop = fbdev_output_start_repaint_loop;
Philip Withnall4f499172013-02-02 12:02:32 +0000531 output->base.repaint = fbdev_output_repaint;
532 output->base.destroy = fbdev_output_destroy;
533 output->base.assign_planes = NULL;
534 output->base.set_backlight = NULL;
535 output->base.set_dpms = NULL;
536 output->base.switch_mode = NULL;
537
538 /* only one static mode in list */
539 output->mode.flags =
540 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
541 output->mode.width = output->fb_info.x_resolution;
542 output->mode.height = output->fb_info.y_resolution;
543 output->mode.refresh = output->fb_info.refresh_rate;
544 wl_list_init(&output->base.mode_list);
545 wl_list_insert(&output->base.mode_list, &output->mode.link);
546
547 output->base.current = &output->mode;
548 output->base.origin = &output->mode;
549 output->base.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
550 output->base.make = "unknown";
551 output->base.model = output->fb_info.id;
552
553 weston_output_init(&output->base, &compositor->base,
554 0, 0, output->fb_info.width_mm,
555 output->fb_info.height_mm,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200556 WL_OUTPUT_TRANSFORM_NORMAL,
557 1);
Philip Withnall4f499172013-02-02 12:02:32 +0000558
559 width = output->fb_info.x_resolution;
560 height = output->fb_info.y_resolution;
561
562 pixman_transform_init_identity(&transform);
563 switch (output->base.transform) {
564 default:
565 case WL_OUTPUT_TRANSFORM_NORMAL:
566 shadow_width = width;
567 shadow_height = height;
568 pixman_transform_rotate(&transform,
569 NULL, 0, 0);
570 pixman_transform_translate(&transform, NULL,
571 0, 0);
572 break;
573 case WL_OUTPUT_TRANSFORM_180:
574 shadow_width = width;
575 shadow_height = height;
576 pixman_transform_rotate(&transform,
577 NULL, -pixman_fixed_1, 0);
578 pixman_transform_translate(NULL, &transform,
579 pixman_int_to_fixed(shadow_width),
580 pixman_int_to_fixed(shadow_height));
581 break;
582 case WL_OUTPUT_TRANSFORM_270:
583 shadow_width = height;
584 shadow_height = width;
585 pixman_transform_rotate(&transform,
586 NULL, 0, pixman_fixed_1);
587 pixman_transform_translate(&transform,
588 NULL,
589 pixman_int_to_fixed(shadow_width),
590 0);
591 break;
592 case WL_OUTPUT_TRANSFORM_90:
593 shadow_width = height;
594 shadow_height = width;
595 pixman_transform_rotate(&transform,
596 NULL, 0, -pixman_fixed_1);
597 pixman_transform_translate(&transform,
598 NULL,
599 0,
600 pixman_int_to_fixed(shadow_height));
601 break;
602 }
603
604 bytes_per_pixel = output->fb_info.bits_per_pixel / 8;
605
606 output->shadow_buf = malloc(width * height * bytes_per_pixel);
607 output->shadow_surface =
608 pixman_image_create_bits(output->fb_info.pixel_format,
609 shadow_width, shadow_height,
610 output->shadow_buf,
611 shadow_width * bytes_per_pixel);
612 if (output->shadow_buf == NULL || output->shadow_surface == NULL) {
613 weston_log("Failed to create surface for frame buffer.\n");
614 goto out_hw_surface;
615 }
616
617 /* No need in transform for normal output */
618 if (output->base.transform != WL_OUTPUT_TRANSFORM_NORMAL)
619 pixman_image_set_transform(output->shadow_surface, &transform);
620
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300621 if (compositor->use_pixman) {
622 if (pixman_renderer_output_create(&output->base) < 0)
623 goto out_shadow_surface;
624 } else {
625 setenv("HYBRIS_EGLPLATFORM", "wayland", 1);
626 if (gl_renderer_output_create(&output->base,
627 (EGLNativeWindowType)NULL) < 0) {
628 weston_log("gl_renderer_output_create failed.\n");
629 goto out_shadow_surface;
630 }
631 }
632
Philip Withnall4f499172013-02-02 12:02:32 +0000633
634 loop = wl_display_get_event_loop(compositor->base.wl_display);
635 output->finish_frame_timer =
636 wl_event_loop_add_timer(loop, finish_frame_handler, output);
637
638 wl_list_insert(compositor->base.output_list.prev, &output->base.link);
639
640 weston_log("fbdev output %d×%d px\n",
641 output->mode.width, output->mode.height);
642 weston_log_continue(STAMP_SPACE "guessing %d Hz and 96 dpi\n",
643 output->mode.refresh / 1000);
644
645 return 0;
646
647out_shadow_surface:
648 pixman_image_unref(output->shadow_surface);
649 output->shadow_surface = NULL;
650out_hw_surface:
651 free(output->shadow_buf);
652 pixman_image_unref(output->hw_surface);
653 output->hw_surface = NULL;
654 weston_output_destroy(&output->base);
655 fbdev_frame_buffer_destroy(output);
656out_free:
657 free(output);
658
659 return -1;
660}
661
662static void
663fbdev_output_destroy(struct weston_output *base)
664{
665 struct fbdev_output *output = to_fbdev_output(base);
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300666 struct fbdev_compositor *compositor = output->compositor;
Philip Withnall4f499172013-02-02 12:02:32 +0000667
668 weston_log("Destroying fbdev output.\n");
669
670 /* Close the frame buffer. */
671 fbdev_output_disable(base);
672
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300673 if (compositor->use_pixman) {
674 if (base->renderer_state != NULL)
675 pixman_renderer_output_destroy(base);
Philip Withnall4f499172013-02-02 12:02:32 +0000676
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300677 if (output->shadow_surface != NULL) {
678 pixman_image_unref(output->shadow_surface);
679 output->shadow_surface = NULL;
680 }
Philip Withnall4f499172013-02-02 12:02:32 +0000681
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300682 if (output->shadow_buf != NULL) {
683 free(output->shadow_buf);
684 output->shadow_buf = NULL;
685 }
686 } else {
687 gl_renderer_output_destroy(base);
Philip Withnall4f499172013-02-02 12:02:32 +0000688 }
689
690 /* Remove the output. */
691 wl_list_remove(&output->base.link);
692 weston_output_destroy(&output->base);
693
694 free(output);
695}
696
697/* strcmp()-style return values. */
698static int
699compare_screen_info (const struct fbdev_screeninfo *a,
700 const struct fbdev_screeninfo *b)
701{
702 if (a->x_resolution == b->x_resolution &&
703 a->y_resolution == b->y_resolution &&
704 a->width_mm == b->width_mm &&
705 a->height_mm == b->height_mm &&
706 a->bits_per_pixel == b->bits_per_pixel &&
707 a->pixel_format == b->pixel_format &&
708 a->refresh_rate == b->refresh_rate)
709 return 0;
710
711 return 1;
712}
713
714static int
715fbdev_output_reenable(struct fbdev_compositor *compositor,
716 struct weston_output *base)
717{
718 struct fbdev_output *output = to_fbdev_output(base);
719 struct fbdev_screeninfo new_screen_info;
720 int fb_fd;
Rob Bradfordf8ef42f2013-07-26 16:29:38 +0100721 const char *device;
Philip Withnall4f499172013-02-02 12:02:32 +0000722
723 weston_log("Re-enabling fbdev output.\n");
724
725 /* Create the frame buffer. */
726 fb_fd = fbdev_frame_buffer_open(output, output->device,
727 &new_screen_info);
728 if (fb_fd < 0) {
729 weston_log("Creating frame buffer failed.\n");
730 goto err;
731 }
732
733 /* Check whether the frame buffer details have changed since we were
734 * disabled. */
735 if (compare_screen_info (&output->fb_info, &new_screen_info) != 0) {
736 /* Perform a mode-set to restore the old mode. */
737 if (fbdev_set_screen_info(output, fb_fd,
738 &output->fb_info) < 0) {
739 weston_log("Failed to restore mode settings. "
740 "Attempting to re-open output anyway.\n");
741 }
742
Rob Bradford581b3fd2013-07-26 16:29:39 +0100743 close(fb_fd);
744
Philip Withnall4f499172013-02-02 12:02:32 +0000745 /* Remove and re-add the output so that resources depending on
746 * the frame buffer X/Y resolution (such as the shadow buffer)
747 * are re-initialised. */
Rob Bradfordf8ef42f2013-07-26 16:29:38 +0100748 device = output->device;
Philip Withnall4f499172013-02-02 12:02:32 +0000749 fbdev_output_destroy(base);
Rob Bradfordf8ef42f2013-07-26 16:29:38 +0100750 fbdev_output_create(compositor, device);
Philip Withnall4f499172013-02-02 12:02:32 +0000751
752 return 0;
753 }
754
755 /* Map the device if it has the same details as before. */
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300756 if (compositor->use_pixman) {
757 if (fbdev_frame_buffer_map(output, fb_fd) < 0) {
758 weston_log("Mapping frame buffer failed.\n");
759 goto err;
760 }
Philip Withnall4f499172013-02-02 12:02:32 +0000761 }
762
763 return 0;
764
765err:
766 return -1;
767}
768
769/* NOTE: This leaves output->fb_info populated, caching data so that if
770 * fbdev_output_reenable() is called again, it can determine whether a mode-set
771 * is needed. */
772static void
773fbdev_output_disable(struct weston_output *base)
774{
775 struct fbdev_output *output = to_fbdev_output(base);
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300776 struct fbdev_compositor *compositor = output->compositor;
Philip Withnall4f499172013-02-02 12:02:32 +0000777
778 weston_log("Disabling fbdev output.\n");
779
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300780 if ( ! compositor->use_pixman) return;
781
Philip Withnall4f499172013-02-02 12:02:32 +0000782 if (output->hw_surface != NULL) {
783 pixman_image_unref(output->hw_surface);
784 output->hw_surface = NULL;
785 }
786
787 fbdev_frame_buffer_destroy(output);
788}
789
790static void
Philip Withnall4f499172013-02-02 12:02:32 +0000791fbdev_compositor_destroy(struct weston_compositor *base)
792{
793 struct fbdev_compositor *compositor = to_fbdev_compositor(base);
Philip Withnall4f499172013-02-02 12:02:32 +0000794
Rob Bradfordd355b802013-05-31 18:09:55 +0100795 udev_input_destroy(&compositor->input);
Philip Withnall4f499172013-02-02 12:02:32 +0000796
797 /* Destroy the output. */
798 weston_compositor_shutdown(&compositor->base);
799
800 /* Chain up. */
801 compositor->base.renderer->destroy(&compositor->base);
802 tty_destroy(compositor->tty);
803
804 free(compositor);
805}
806
807static void
Kristian Høgsberg61741a22013-09-17 16:02:57 -0700808session_notify(struct wl_listener *listener, void *data)
Philip Withnall4f499172013-02-02 12:02:32 +0000809{
Kristian Høgsberg61741a22013-09-17 16:02:57 -0700810 struct fbdev_compositor *compositor = data;
Philip Withnall4f499172013-02-02 12:02:32 +0000811 struct weston_output *output;
812
Kristian Høgsberg61741a22013-09-17 16:02:57 -0700813 if (compositor->base.session_active) {
Philip Withnall4f499172013-02-02 12:02:32 +0000814 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);
Kristian Høgsberg61741a22013-09-17 16:02:57 -0700825 } else {
Philip Withnall4f499172013-02-02 12:02:32 +0000826 weston_log("leaving VT\n");
Rob Bradfordd355b802013-05-31 18:09:55 +0100827 udev_input_disable(&compositor->input);
Philip Withnall4f499172013-02-02 12:02:32 +0000828
829 wl_list_for_each(output, &compositor->base.output_list, link) {
830 fbdev_output_disable(output);
831 }
832
833 compositor->base.focus = 0;
834 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
852fbdev_restore(struct weston_compositor *base)
853{
854 struct fbdev_compositor *compositor = to_fbdev_compositor(base);
855
856 tty_reset(compositor->tty);
857}
858
859static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400860switch_vt_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Philip Withnall4f499172013-02-02 12:02:32 +0000861{
862 struct fbdev_compositor *ec = data;
863
864 tty_activate_vt(ec->tty, key - KEY_F1 + 1);
865}
866
867static struct weston_compositor *
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500868fbdev_compositor_create(struct wl_display *display, int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400869 struct weston_config *config,
870 struct fbdev_parameters *param)
Philip Withnall4f499172013-02-02 12:02:32 +0000871{
872 struct fbdev_compositor *compositor;
Rob Bradford2387fde2013-05-31 18:09:57 +0100873 const char *seat_id = default_seat;
Philip Withnall4f499172013-02-02 12:02:32 +0000874 uint32_t key;
875
876 weston_log("initializing fbdev backend\n");
877
878 compositor = calloc(1, sizeof *compositor);
879 if (compositor == NULL)
880 return NULL;
881
882 if (weston_compositor_init(&compositor->base, display, argc, argv,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400883 config) < 0)
Philip Withnall4f499172013-02-02 12:02:32 +0000884 goto out_free;
885
Adrian Negreanuc8384232013-07-28 18:27:23 +0300886 /* Check if we run fbdev-backend using weston-launch */
Kristian Høgsberg05ad1e42013-09-17 14:41:03 -0700887 compositor->base.launcher = weston_launcher_connect(&compositor->base);
888 if (compositor->base.launcher == NULL && geteuid() != 0) {
Adrian Negreanuc8384232013-07-28 18:27:23 +0300889 weston_log("fatal: fbdev backend should be run "
890 "using weston-launch binary or as root\n");
891 goto out_compositor;
892 }
893
Philip Withnall4f499172013-02-02 12:02:32 +0000894 compositor->udev = udev_new();
895 if (compositor->udev == NULL) {
896 weston_log("Failed to initialize udev context.\n");
897 goto out_compositor;
898 }
899
900 /* Set up the TTY. */
Kristian Høgsberg61741a22013-09-17 16:02:57 -0700901 compositor->session_listener.notify = session_notify;
902 wl_signal_add(&compositor->base.session_signal,
903 &compositor->session_listener);
904 compositor->tty = tty_create(&compositor->base, param->tty);
Philip Withnall4f499172013-02-02 12:02:32 +0000905 if (!compositor->tty) {
906 weston_log("Failed to initialize tty.\n");
907 goto out_udev;
908 }
909
910 compositor->base.destroy = fbdev_compositor_destroy;
911 compositor->base.restore = fbdev_restore;
912
913 compositor->base.focus = 1;
914 compositor->prev_state = WESTON_COMPOSITOR_ACTIVE;
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300915 compositor->use_pixman = !param->use_gl;
Philip Withnall4f499172013-02-02 12:02:32 +0000916
917 for (key = KEY_F1; key < KEY_F9; key++)
918 weston_compositor_add_key_binding(&compositor->base, key,
919 MODIFIER_CTRL | MODIFIER_ALT,
920 switch_vt_binding,
921 compositor);
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300922 if (compositor->use_pixman) {
923 if (pixman_renderer_init(&compositor->base) < 0)
924 goto out_tty;
925 } else {
926 if (gl_renderer_create(&compositor->base, EGL_DEFAULT_DISPLAY,
927 gl_renderer_opaque_attribs, NULL) < 0) {
928 weston_log("gl_renderer_create failed.\n");
929 goto out_tty;
930 }
931 }
Philip Withnall4f499172013-02-02 12:02:32 +0000932
933 if (fbdev_output_create(compositor, param->device) < 0)
934 goto out_pixman;
935
Rob Bradford2387fde2013-05-31 18:09:57 +0100936 udev_input_init(&compositor->input, &compositor->base, compositor->udev, seat_id);
Philip Withnall4f499172013-02-02 12:02:32 +0000937
938 return &compositor->base;
939
940out_pixman:
941 compositor->base.renderer->destroy(&compositor->base);
942
943out_tty:
944 tty_destroy(compositor->tty);
945
946out_udev:
947 udev_unref(compositor->udev);
948
949out_compositor:
950 weston_compositor_shutdown(&compositor->base);
951
952out_free:
953 free(compositor);
954
955 return NULL;
956}
957
958WL_EXPORT struct weston_compositor *
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500959backend_init(struct wl_display *display, int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400960 struct weston_config *config)
Philip Withnall4f499172013-02-02 12:02:32 +0000961{
962 /* TODO: Ideally, available frame buffers should be enumerated using
963 * udev, rather than passing a device node in as a parameter. */
964 struct fbdev_parameters param = {
965 .tty = 0, /* default to current tty */
966 .device = "/dev/fb0", /* default frame buffer */
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300967 .use_gl = 0,
Philip Withnall4f499172013-02-02 12:02:32 +0000968 };
969
970 const struct weston_option fbdev_options[] = {
971 { WESTON_OPTION_INTEGER, "tty", 0, &param.tty },
972 { WESTON_OPTION_STRING, "device", 0, &param.device },
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300973 { WESTON_OPTION_BOOLEAN, "use-gl", 0, &param.use_gl },
Philip Withnall4f499172013-02-02 12:02:32 +0000974 };
975
976 parse_options(fbdev_options, ARRAY_LENGTH(fbdev_options), argc, argv);
977
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400978 return fbdev_compositor_create(display, argc, argv, config, &param);
Philip Withnall4f499172013-02-02 12:02:32 +0000979}