blob: c75fe41557b5e6a983cbbe28b96c53c005b5c6b5 [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"
Pekka Paalanen363aa7b2014-12-17 16:20:40 +020047#include "presentation_timing-server-protocol.h"
Philip Withnall4f499172013-02-02 12:02:32 +000048
49struct fbdev_compositor {
50 struct weston_compositor base;
51 uint32_t prev_state;
52
53 struct udev *udev;
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
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +030099struct gl_renderer_interface *gl_renderer;
100
Kristian Høgsberg7e597f22013-02-18 16:35:26 -0500101static const char default_seat[] = "seat0";
102
Philip Withnall4f499172013-02-02 12:02:32 +0000103static inline struct fbdev_output *
104to_fbdev_output(struct weston_output *base)
105{
106 return container_of(base, struct fbdev_output, base);
107}
108
Philip Withnall4f499172013-02-02 12:02:32 +0000109static inline struct fbdev_compositor *
110to_fbdev_compositor(struct weston_compositor *base)
111{
112 return container_of(base, struct fbdev_compositor, base);
113}
114
115static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200116fbdev_output_start_repaint_loop(struct weston_output *output)
117{
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400118 struct timespec ts;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200119
Pekka Paalanen662f3842015-03-18 12:17:26 +0200120 weston_compositor_read_presentation_clock(output->compositor, &ts);
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200121 weston_output_finish_frame(output, &ts, PRESENTATION_FEEDBACK_INVALID);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200122}
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;
Derek Foreman561662b2015-03-17 13:22:38 -0500130 int nrects, i;
Philip Withnall4f499172013-02-02 12:02:32 +0000131
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. */
Philip Withnall4f499172013-02-02 12:02:32 +0000137 rects = pixman_region32_rectangles(damage, &nrects);
138
139 for (i = 0; i < nrects; i++) {
Derek Foreman561662b2015-03-17 13:22:38 -0500140 pixman_box32_t transformed_rect;
141 int width, height;
Philip Withnall4f499172013-02-02 12:02:32 +0000142
Derek Foreman561662b2015-03-17 13:22:38 -0500143 transformed_rect = weston_transformed_rect(base->width,
144 base->height,
145 base->transform,
146 1, rects[i]);
147 width = transformed_rect.x2 - transformed_rect.x1;
148 height = transformed_rect.y2 - transformed_rect.y1;
Philip Withnall4f499172013-02-02 12:02:32 +0000149 pixman_image_composite32(PIXMAN_OP_SRC,
150 output->shadow_surface, /* src */
151 NULL /* mask */,
152 output->hw_surface, /* dest */
Derek Foreman561662b2015-03-17 13:22:38 -0500153 transformed_rect.x1, /* src_x */
154 transformed_rect.y1, /* src_y */
Philip Withnall4f499172013-02-02 12:02:32 +0000155 0, 0, /* mask_x, mask_y */
Derek Foreman561662b2015-03-17 13:22:38 -0500156 transformed_rect.x1, /* dest_x */
157 transformed_rect.y1, /* dest_y */
158 width, /* width */
159 height /* height */);
Philip Withnall4f499172013-02-02 12:02:32 +0000160 }
161
162 /* Update the damage region. */
163 pixman_region32_subtract(&ec->primary_plane.damage,
164 &ec->primary_plane.damage, damage);
165
166 /* Schedule the end of the frame. We do not sync this to the frame
167 * buffer clock because users who want that should be using the DRM
168 * compositor. FBIO_WAITFORVSYNC blocks and FB_ACTIVATE_VBL requires
169 * panning, which is broken in most kernel drivers.
170 *
171 * Finish the frame synchronised to the specified refresh rate. The
172 * refresh rate is given in mHz and the interval in ms. */
173 wl_event_source_timer_update(output->finish_frame_timer,
174 1000000 / output->mode.refresh);
175}
176
David Herrmann1edf44c2013-10-22 17:11:26 +0200177static int
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300178fbdev_output_repaint(struct weston_output *base, pixman_region32_t *damage)
179{
180 struct fbdev_output *output = to_fbdev_output(base);
181 struct fbdev_compositor *fbc = output->compositor;
182 struct weston_compositor *ec = & fbc->base;
183
184 if (fbc->use_pixman) {
185 fbdev_output_repaint_pixman(base,damage);
186 } else {
187 ec->renderer->repaint_output(base, damage);
188 /* Update the damage region. */
189 pixman_region32_subtract(&ec->primary_plane.damage,
190 &ec->primary_plane.damage, damage);
191
192 wl_event_source_timer_update(output->finish_frame_timer,
193 1000000 / output->mode.refresh);
194 }
David Herrmann1edf44c2013-10-22 17:11:26 +0200195
196 return 0;
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300197}
198
Philip Withnall4f499172013-02-02 12:02:32 +0000199static int
200finish_frame_handler(void *data)
201{
202 struct fbdev_output *output = data;
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200203 struct timespec ts;
Philip Withnall4f499172013-02-02 12:02:32 +0000204
Pekka Paalanen662f3842015-03-18 12:17:26 +0200205 weston_compositor_read_presentation_clock(output->base.compositor, &ts);
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200206 weston_output_finish_frame(&output->base, &ts, 0);
Philip Withnall4f499172013-02-02 12:02:32 +0000207
208 return 1;
209}
210
211static pixman_format_code_t
212calculate_pixman_format(struct fb_var_screeninfo *vinfo,
213 struct fb_fix_screeninfo *finfo)
214{
215 /* Calculate the pixman format supported by the frame buffer from the
216 * buffer's metadata. Return 0 if no known pixman format is supported
217 * (since this has depth 0 it's guaranteed to not conflict with any
218 * actual pixman format).
219 *
220 * Documentation on the vinfo and finfo structures:
221 * http://www.mjmwired.net/kernel/Documentation/fb/api.txt
222 *
223 * TODO: Try a bit harder to support other formats, including setting
224 * the preferred format in the hardware. */
225 int type;
226
227 weston_log("Calculating pixman format from:\n"
228 STAMP_SPACE " - type: %i (aux: %i)\n"
229 STAMP_SPACE " - visual: %i\n"
230 STAMP_SPACE " - bpp: %i (grayscale: %i)\n"
231 STAMP_SPACE " - red: offset: %i, length: %i, MSB: %i\n"
232 STAMP_SPACE " - green: offset: %i, length: %i, MSB: %i\n"
233 STAMP_SPACE " - blue: offset: %i, length: %i, MSB: %i\n"
234 STAMP_SPACE " - transp: offset: %i, length: %i, MSB: %i\n",
235 finfo->type, finfo->type_aux, finfo->visual,
236 vinfo->bits_per_pixel, vinfo->grayscale,
237 vinfo->red.offset, vinfo->red.length, vinfo->red.msb_right,
238 vinfo->green.offset, vinfo->green.length,
239 vinfo->green.msb_right,
240 vinfo->blue.offset, vinfo->blue.length,
241 vinfo->blue.msb_right,
242 vinfo->transp.offset, vinfo->transp.length,
243 vinfo->transp.msb_right);
244
245 /* We only handle packed formats at the moment. */
246 if (finfo->type != FB_TYPE_PACKED_PIXELS)
247 return 0;
248
249 /* We only handle true-colour frame buffers at the moment. */
Marc Chalainffbddff2013-09-03 16:47:43 +0200250 switch(finfo->visual) {
251 case FB_VISUAL_TRUECOLOR:
252 case FB_VISUAL_DIRECTCOLOR:
253 if (vinfo->grayscale != 0)
254 return 0;
255 break;
256 default:
257 return 0;
258 }
Philip Withnall4f499172013-02-02 12:02:32 +0000259
260 /* We only support formats with MSBs on the left. */
261 if (vinfo->red.msb_right != 0 || vinfo->green.msb_right != 0 ||
262 vinfo->blue.msb_right != 0)
263 return 0;
264
265 /* Work out the format type from the offsets. We only support RGBA and
266 * ARGB at the moment. */
267 type = PIXMAN_TYPE_OTHER;
268
269 if ((vinfo->transp.offset >= vinfo->red.offset ||
270 vinfo->transp.length == 0) &&
271 vinfo->red.offset >= vinfo->green.offset &&
272 vinfo->green.offset >= vinfo->blue.offset)
273 type = PIXMAN_TYPE_ARGB;
274 else if (vinfo->red.offset >= vinfo->green.offset &&
275 vinfo->green.offset >= vinfo->blue.offset &&
276 vinfo->blue.offset >= vinfo->transp.offset)
277 type = PIXMAN_TYPE_RGBA;
278
279 if (type == PIXMAN_TYPE_OTHER)
280 return 0;
281
282 /* Build the format. */
283 return PIXMAN_FORMAT(vinfo->bits_per_pixel, type,
284 vinfo->transp.length,
285 vinfo->red.length,
286 vinfo->green.length,
287 vinfo->blue.length);
288}
289
290static int
291calculate_refresh_rate(struct fb_var_screeninfo *vinfo)
292{
293 uint64_t quot;
294
295 /* Calculate monitor refresh rate. Default is 60 Hz. Units are mHz. */
296 quot = (vinfo->upper_margin + vinfo->lower_margin + vinfo->yres);
297 quot *= (vinfo->left_margin + vinfo->right_margin + vinfo->xres);
298 quot *= vinfo->pixclock;
299
300 if (quot > 0) {
301 uint64_t refresh_rate;
302
303 refresh_rate = 1000000000000000LLU / quot;
304 if (refresh_rate > 200000)
305 refresh_rate = 200000; /* cap at 200 Hz */
306
307 return refresh_rate;
308 }
309
310 return 60 * 1000; /* default to 60 Hz */
311}
312
313static int
314fbdev_query_screen_info(struct fbdev_output *output, int fd,
315 struct fbdev_screeninfo *info)
316{
317 struct fb_var_screeninfo varinfo;
318 struct fb_fix_screeninfo fixinfo;
319
320 /* Probe the device for screen information. */
321 if (ioctl(fd, FBIOGET_FSCREENINFO, &fixinfo) < 0 ||
322 ioctl(fd, FBIOGET_VSCREENINFO, &varinfo) < 0) {
323 return -1;
324 }
325
326 /* Store the pertinent data. */
327 info->x_resolution = varinfo.xres;
328 info->y_resolution = varinfo.yres;
329 info->width_mm = varinfo.width;
330 info->height_mm = varinfo.height;
331 info->bits_per_pixel = varinfo.bits_per_pixel;
332
333 info->buffer_length = fixinfo.smem_len;
334 info->line_length = fixinfo.line_length;
335 strncpy(info->id, fixinfo.id, sizeof(info->id) / sizeof(*info->id));
336
337 info->pixel_format = calculate_pixman_format(&varinfo, &fixinfo);
338 info->refresh_rate = calculate_refresh_rate(&varinfo);
339
340 if (info->pixel_format == 0) {
341 weston_log("Frame buffer uses an unsupported format.\n");
342 return -1;
343 }
344
345 return 1;
346}
347
348static int
349fbdev_set_screen_info(struct fbdev_output *output, int fd,
350 struct fbdev_screeninfo *info)
351{
352 struct fb_var_screeninfo varinfo;
353
354 /* Grab the current screen information. */
355 if (ioctl(fd, FBIOGET_VSCREENINFO, &varinfo) < 0) {
356 return -1;
357 }
358
359 /* Update the information. */
360 varinfo.xres = info->x_resolution;
361 varinfo.yres = info->y_resolution;
362 varinfo.width = info->width_mm;
363 varinfo.height = info->height_mm;
364 varinfo.bits_per_pixel = info->bits_per_pixel;
365
366 /* Try to set up an ARGB (x8r8g8b8) pixel format. */
367 varinfo.grayscale = 0;
368 varinfo.transp.offset = 24;
369 varinfo.transp.length = 0;
370 varinfo.transp.msb_right = 0;
371 varinfo.red.offset = 16;
372 varinfo.red.length = 8;
373 varinfo.red.msb_right = 0;
374 varinfo.green.offset = 8;
375 varinfo.green.length = 8;
376 varinfo.green.msb_right = 0;
377 varinfo.blue.offset = 0;
378 varinfo.blue.length = 8;
379 varinfo.blue.msb_right = 0;
380
381 /* Set the device's screen information. */
382 if (ioctl(fd, FBIOPUT_VSCREENINFO, &varinfo) < 0) {
383 return -1;
384 }
385
386 return 1;
387}
388
389static void fbdev_frame_buffer_destroy(struct fbdev_output *output);
390
391/* Returns an FD for the frame buffer device. */
392static int
393fbdev_frame_buffer_open(struct fbdev_output *output, const char *fb_dev,
394 struct fbdev_screeninfo *screen_info)
395{
396 int fd = -1;
397
398 weston_log("Opening fbdev frame buffer.\n");
399
400 /* Open the frame buffer device. */
401 fd = open(fb_dev, O_RDWR | O_CLOEXEC);
402 if (fd < 0) {
403 weston_log("Failed to open frame buffer device ‘%s’: %s\n",
404 fb_dev, strerror(errno));
405 return -1;
406 }
407
408 /* Grab the screen info. */
409 if (fbdev_query_screen_info(output, fd, screen_info) < 0) {
410 weston_log("Failed to get frame buffer info: %s\n",
411 strerror(errno));
412
413 close(fd);
414 return -1;
415 }
416
417 return fd;
418}
419
420/* Closes the FD on success or failure. */
421static int
422fbdev_frame_buffer_map(struct fbdev_output *output, int fd)
423{
424 int retval = -1;
425
426 weston_log("Mapping fbdev frame buffer.\n");
427
428 /* Map the frame buffer. Write-only mode, since we don't want to read
429 * anything back (because it's slow). */
430 output->fb = mmap(NULL, output->fb_info.buffer_length,
431 PROT_WRITE, MAP_SHARED, fd, 0);
432 if (output->fb == MAP_FAILED) {
433 weston_log("Failed to mmap frame buffer: %s\n",
434 strerror(errno));
435 goto out_close;
436 }
437
438 /* Create a pixman image to wrap the memory mapped frame buffer. */
439 output->hw_surface =
440 pixman_image_create_bits(output->fb_info.pixel_format,
441 output->fb_info.x_resolution,
442 output->fb_info.y_resolution,
443 output->fb,
444 output->fb_info.line_length);
445 if (output->hw_surface == NULL) {
446 weston_log("Failed to create surface for frame buffer.\n");
447 goto out_unmap;
448 }
449
450 /* Success! */
451 retval = 0;
452
453out_unmap:
454 if (retval != 0 && output->fb != NULL)
455 fbdev_frame_buffer_destroy(output);
456
457out_close:
458 if (fd >= 0)
459 close(fd);
460
461 return retval;
462}
463
464static void
465fbdev_frame_buffer_destroy(struct fbdev_output *output)
466{
467 weston_log("Destroying fbdev frame buffer.\n");
468
469 if (munmap(output->fb, output->fb_info.buffer_length) < 0)
470 weston_log("Failed to munmap frame buffer: %s\n",
471 strerror(errno));
472
473 output->fb = NULL;
474}
475
476static void fbdev_output_destroy(struct weston_output *base);
477static void fbdev_output_disable(struct weston_output *base);
478
479static int
480fbdev_output_create(struct fbdev_compositor *compositor,
481 const char *device)
482{
483 struct fbdev_output *output;
Derek Foreman44ed70b2015-03-17 13:22:37 -0500484 struct weston_config_section *section;
Philip Withnall4f499172013-02-02 12:02:32 +0000485 int fb_fd;
Philip Withnall4f499172013-02-02 12:02:32 +0000486 int width, height;
487 unsigned int bytes_per_pixel;
488 struct wl_event_loop *loop;
Derek Foreman44ed70b2015-03-17 13:22:37 -0500489 uint32_t config_transform;
490 char *s;
Philip Withnall4f499172013-02-02 12:02:32 +0000491
492 weston_log("Creating fbdev output.\n");
493
Bryce Harringtonde16d892014-11-20 22:21:57 -0800494 output = zalloc(sizeof *output);
495 if (output == NULL)
Philip Withnall4f499172013-02-02 12:02:32 +0000496 return -1;
497
498 output->compositor = compositor;
499 output->device = device;
500
501 /* Create the frame buffer. */
502 fb_fd = fbdev_frame_buffer_open(output, device, &output->fb_info);
503 if (fb_fd < 0) {
504 weston_log("Creating frame buffer failed.\n");
505 goto out_free;
506 }
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300507 if (compositor->use_pixman) {
508 if (fbdev_frame_buffer_map(output, fb_fd) < 0) {
509 weston_log("Mapping frame buffer failed.\n");
510 goto out_free;
511 }
Kristian Høgsberg8ac6a2d2013-10-09 09:59:06 -0700512 } else {
513 close(fb_fd);
Philip Withnall4f499172013-02-02 12:02:32 +0000514 }
515
Jonas Ådahle5a12252013-04-05 23:07:11 +0200516 output->base.start_repaint_loop = fbdev_output_start_repaint_loop;
Philip Withnall4f499172013-02-02 12:02:32 +0000517 output->base.repaint = fbdev_output_repaint;
518 output->base.destroy = fbdev_output_destroy;
Philip Withnall4f499172013-02-02 12:02:32 +0000519
520 /* only one static mode in list */
521 output->mode.flags =
522 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
523 output->mode.width = output->fb_info.x_resolution;
524 output->mode.height = output->fb_info.y_resolution;
525 output->mode.refresh = output->fb_info.refresh_rate;
526 wl_list_init(&output->base.mode_list);
527 wl_list_insert(&output->base.mode_list, &output->mode.link);
528
Hardeningff39efa2013-09-18 23:56:35 +0200529 output->base.current_mode = &output->mode;
Philip Withnall4f499172013-02-02 12:02:32 +0000530 output->base.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
531 output->base.make = "unknown";
532 output->base.model = output->fb_info.id;
Derek Foremane516c3b2015-03-17 13:22:34 -0500533 output->base.name = strdup("fbdev");
Philip Withnall4f499172013-02-02 12:02:32 +0000534
Derek Foreman44ed70b2015-03-17 13:22:37 -0500535 section = weston_config_get_section(compositor->base.config,
536 "output", "name",
537 output->base.name);
538 weston_config_section_get_string(section, "transform", &s, "normal");
539 if (weston_parse_transform(s, &config_transform) < 0)
540 weston_log("Invalid transform \"%s\" for output %s\n",
541 s, output->base.name);
542 free(s);
543
Philip Withnall4f499172013-02-02 12:02:32 +0000544 weston_output_init(&output->base, &compositor->base,
545 0, 0, output->fb_info.width_mm,
546 output->fb_info.height_mm,
Derek Foreman44ed70b2015-03-17 13:22:37 -0500547 config_transform,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200548 1);
Philip Withnall4f499172013-02-02 12:02:32 +0000549
Derek Foreman561662b2015-03-17 13:22:38 -0500550 width = output->mode.width;
551 height = output->mode.height;
Philip Withnall4f499172013-02-02 12:02:32 +0000552 bytes_per_pixel = output->fb_info.bits_per_pixel / 8;
553
554 output->shadow_buf = malloc(width * height * bytes_per_pixel);
555 output->shadow_surface =
556 pixman_image_create_bits(output->fb_info.pixel_format,
Derek Foreman561662b2015-03-17 13:22:38 -0500557 width, height,
Philip Withnall4f499172013-02-02 12:02:32 +0000558 output->shadow_buf,
Derek Foreman561662b2015-03-17 13:22:38 -0500559 width * bytes_per_pixel);
Philip Withnall4f499172013-02-02 12:02:32 +0000560 if (output->shadow_buf == NULL || output->shadow_surface == NULL) {
561 weston_log("Failed to create surface for frame buffer.\n");
562 goto out_hw_surface;
563 }
564
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300565 if (compositor->use_pixman) {
566 if (pixman_renderer_output_create(&output->base) < 0)
567 goto out_shadow_surface;
568 } else {
569 setenv("HYBRIS_EGLPLATFORM", "wayland", 1);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300570 if (gl_renderer->output_create(&output->base,
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000571 (EGLNativeWindowType)NULL,
572 gl_renderer->opaque_attribs,
573 NULL) < 0) {
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300574 weston_log("gl_renderer_output_create failed.\n");
575 goto out_shadow_surface;
576 }
577 }
578
Philip Withnall4f499172013-02-02 12:02:32 +0000579
580 loop = wl_display_get_event_loop(compositor->base.wl_display);
581 output->finish_frame_timer =
582 wl_event_loop_add_timer(loop, finish_frame_handler, output);
583
584 wl_list_insert(compositor->base.output_list.prev, &output->base.link);
585
586 weston_log("fbdev output %d×%d px\n",
587 output->mode.width, output->mode.height);
588 weston_log_continue(STAMP_SPACE "guessing %d Hz and 96 dpi\n",
589 output->mode.refresh / 1000);
590
591 return 0;
592
593out_shadow_surface:
594 pixman_image_unref(output->shadow_surface);
595 output->shadow_surface = NULL;
596out_hw_surface:
597 free(output->shadow_buf);
598 pixman_image_unref(output->hw_surface);
599 output->hw_surface = NULL;
600 weston_output_destroy(&output->base);
601 fbdev_frame_buffer_destroy(output);
602out_free:
603 free(output);
604
605 return -1;
606}
607
608static void
609fbdev_output_destroy(struct weston_output *base)
610{
611 struct fbdev_output *output = to_fbdev_output(base);
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300612 struct fbdev_compositor *compositor = output->compositor;
Philip Withnall4f499172013-02-02 12:02:32 +0000613
614 weston_log("Destroying fbdev output.\n");
615
616 /* Close the frame buffer. */
617 fbdev_output_disable(base);
618
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300619 if (compositor->use_pixman) {
620 if (base->renderer_state != NULL)
621 pixman_renderer_output_destroy(base);
Philip Withnall4f499172013-02-02 12:02:32 +0000622
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300623 if (output->shadow_surface != NULL) {
624 pixman_image_unref(output->shadow_surface);
625 output->shadow_surface = NULL;
626 }
Philip Withnall4f499172013-02-02 12:02:32 +0000627
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300628 if (output->shadow_buf != NULL) {
629 free(output->shadow_buf);
630 output->shadow_buf = NULL;
631 }
632 } else {
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300633 gl_renderer->output_destroy(base);
Philip Withnall4f499172013-02-02 12:02:32 +0000634 }
635
636 /* Remove the output. */
Philip Withnall4f499172013-02-02 12:02:32 +0000637 weston_output_destroy(&output->base);
638
639 free(output);
640}
641
642/* strcmp()-style return values. */
643static int
644compare_screen_info (const struct fbdev_screeninfo *a,
645 const struct fbdev_screeninfo *b)
646{
647 if (a->x_resolution == b->x_resolution &&
648 a->y_resolution == b->y_resolution &&
649 a->width_mm == b->width_mm &&
650 a->height_mm == b->height_mm &&
651 a->bits_per_pixel == b->bits_per_pixel &&
652 a->pixel_format == b->pixel_format &&
653 a->refresh_rate == b->refresh_rate)
654 return 0;
655
656 return 1;
657}
658
659static int
660fbdev_output_reenable(struct fbdev_compositor *compositor,
661 struct weston_output *base)
662{
663 struct fbdev_output *output = to_fbdev_output(base);
664 struct fbdev_screeninfo new_screen_info;
665 int fb_fd;
Rob Bradfordf8ef42f2013-07-26 16:29:38 +0100666 const char *device;
Philip Withnall4f499172013-02-02 12:02:32 +0000667
668 weston_log("Re-enabling fbdev output.\n");
669
670 /* Create the frame buffer. */
671 fb_fd = fbdev_frame_buffer_open(output, output->device,
672 &new_screen_info);
673 if (fb_fd < 0) {
674 weston_log("Creating frame buffer failed.\n");
675 goto err;
676 }
677
678 /* Check whether the frame buffer details have changed since we were
679 * disabled. */
680 if (compare_screen_info (&output->fb_info, &new_screen_info) != 0) {
681 /* Perform a mode-set to restore the old mode. */
682 if (fbdev_set_screen_info(output, fb_fd,
683 &output->fb_info) < 0) {
684 weston_log("Failed to restore mode settings. "
685 "Attempting to re-open output anyway.\n");
686 }
687
Rob Bradford581b3fd2013-07-26 16:29:39 +0100688 close(fb_fd);
689
Philip Withnall4f499172013-02-02 12:02:32 +0000690 /* Remove and re-add the output so that resources depending on
691 * the frame buffer X/Y resolution (such as the shadow buffer)
692 * are re-initialised. */
Rob Bradfordf8ef42f2013-07-26 16:29:38 +0100693 device = output->device;
Philip Withnall4f499172013-02-02 12:02:32 +0000694 fbdev_output_destroy(base);
Rob Bradfordf8ef42f2013-07-26 16:29:38 +0100695 fbdev_output_create(compositor, device);
Philip Withnall4f499172013-02-02 12:02:32 +0000696
697 return 0;
698 }
699
700 /* Map the device if it has the same details as before. */
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300701 if (compositor->use_pixman) {
702 if (fbdev_frame_buffer_map(output, fb_fd) < 0) {
703 weston_log("Mapping frame buffer failed.\n");
704 goto err;
705 }
Philip Withnall4f499172013-02-02 12:02:32 +0000706 }
707
708 return 0;
709
710err:
711 return -1;
712}
713
714/* NOTE: This leaves output->fb_info populated, caching data so that if
715 * fbdev_output_reenable() is called again, it can determine whether a mode-set
716 * is needed. */
717static void
718fbdev_output_disable(struct weston_output *base)
719{
720 struct fbdev_output *output = to_fbdev_output(base);
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300721 struct fbdev_compositor *compositor = output->compositor;
Philip Withnall4f499172013-02-02 12:02:32 +0000722
723 weston_log("Disabling fbdev output.\n");
724
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300725 if ( ! compositor->use_pixman) return;
726
Philip Withnall4f499172013-02-02 12:02:32 +0000727 if (output->hw_surface != NULL) {
728 pixman_image_unref(output->hw_surface);
729 output->hw_surface = NULL;
730 }
731
732 fbdev_frame_buffer_destroy(output);
733}
734
735static void
Philip Withnall4f499172013-02-02 12:02:32 +0000736fbdev_compositor_destroy(struct weston_compositor *base)
737{
738 struct fbdev_compositor *compositor = to_fbdev_compositor(base);
Philip Withnall4f499172013-02-02 12:02:32 +0000739
Rob Bradfordd355b802013-05-31 18:09:55 +0100740 udev_input_destroy(&compositor->input);
Philip Withnall4f499172013-02-02 12:02:32 +0000741
742 /* Destroy the output. */
743 weston_compositor_shutdown(&compositor->base);
744
745 /* Chain up. */
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700746 weston_launcher_destroy(compositor->base.launcher);
Philip Withnall4f499172013-02-02 12:02:32 +0000747
748 free(compositor);
749}
750
751static void
Kristian Høgsberg61741a22013-09-17 16:02:57 -0700752session_notify(struct wl_listener *listener, void *data)
Philip Withnall4f499172013-02-02 12:02:32 +0000753{
Kristian Høgsberg61741a22013-09-17 16:02:57 -0700754 struct fbdev_compositor *compositor = data;
Philip Withnall4f499172013-02-02 12:02:32 +0000755 struct weston_output *output;
756
Kristian Høgsberg61741a22013-09-17 16:02:57 -0700757 if (compositor->base.session_active) {
Philip Withnall4f499172013-02-02 12:02:32 +0000758 weston_log("entering VT\n");
Philip Withnall4f499172013-02-02 12:02:32 +0000759 compositor->base.state = compositor->prev_state;
760
761 wl_list_for_each(output, &compositor->base.output_list, link) {
762 fbdev_output_reenable(compositor, output);
763 }
764
765 weston_compositor_damage_all(&compositor->base);
766
Jonas Ådahl0feb32e2014-03-12 22:08:41 +0100767 udev_input_enable(&compositor->input);
Kristian Høgsberg61741a22013-09-17 16:02:57 -0700768 } else {
Philip Withnall4f499172013-02-02 12:02:32 +0000769 weston_log("leaving VT\n");
Rob Bradfordd355b802013-05-31 18:09:55 +0100770 udev_input_disable(&compositor->input);
Philip Withnall4f499172013-02-02 12:02:32 +0000771
772 wl_list_for_each(output, &compositor->base.output_list, link) {
773 fbdev_output_disable(output);
774 }
775
Philip Withnall4f499172013-02-02 12:02:32 +0000776 compositor->prev_state = compositor->base.state;
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +0100777 weston_compositor_offscreen(&compositor->base);
Philip Withnall4f499172013-02-02 12:02:32 +0000778
779 /* If we have a repaint scheduled (from the idle handler), make
780 * sure we cancel that so we don't try to pageflip when we're
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +0100781 * vt switched away. The OFFSCREEN state will prevent
Philip Withnall4f499172013-02-02 12:02:32 +0000782 * further attemps at repainting. When we switch
783 * back, we schedule a repaint, which will process
784 * pending frame callbacks. */
785
786 wl_list_for_each(output,
787 &compositor->base.output_list, link) {
788 output->repaint_needed = 0;
789 }
nerdopolis38946702014-12-03 15:53:03 +0000790 }
Philip Withnall4f499172013-02-02 12:02:32 +0000791}
792
793static void
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700794fbdev_restore(struct weston_compositor *compositor)
Philip Withnall4f499172013-02-02 12:02:32 +0000795{
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700796 weston_launcher_restore(compositor->launcher);
Philip Withnall4f499172013-02-02 12:02:32 +0000797}
798
799static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400800switch_vt_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Philip Withnall4f499172013-02-02 12:02:32 +0000801{
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700802 struct weston_compositor *compositor = data;
Philip Withnall4f499172013-02-02 12:02:32 +0000803
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700804 weston_launcher_activate_vt(compositor->launcher, key - KEY_F1 + 1);
Philip Withnall4f499172013-02-02 12:02:32 +0000805}
806
807static struct weston_compositor *
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500808fbdev_compositor_create(struct wl_display *display, int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400809 struct weston_config *config,
810 struct fbdev_parameters *param)
Philip Withnall4f499172013-02-02 12:02:32 +0000811{
812 struct fbdev_compositor *compositor;
Rob Bradford2387fde2013-05-31 18:09:57 +0100813 const char *seat_id = default_seat;
Philip Withnall4f499172013-02-02 12:02:32 +0000814 uint32_t key;
815
816 weston_log("initializing fbdev backend\n");
817
Bryce Harringtonde16d892014-11-20 22:21:57 -0800818 compositor = zalloc(sizeof *compositor);
Philip Withnall4f499172013-02-02 12:02:32 +0000819 if (compositor == NULL)
820 return NULL;
821
822 if (weston_compositor_init(&compositor->base, display, argc, argv,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400823 config) < 0)
Philip Withnall4f499172013-02-02 12:02:32 +0000824 goto out_free;
825
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400826 if (weston_compositor_set_presentation_clock_software(
827 &compositor->base) < 0)
828 goto out_compositor;
829
Philip Withnall4f499172013-02-02 12:02:32 +0000830 compositor->udev = udev_new();
831 if (compositor->udev == NULL) {
832 weston_log("Failed to initialize udev context.\n");
833 goto out_compositor;
834 }
835
836 /* Set up the TTY. */
Kristian Høgsberg61741a22013-09-17 16:02:57 -0700837 compositor->session_listener.notify = session_notify;
838 wl_signal_add(&compositor->base.session_signal,
839 &compositor->session_listener);
David Herrmann2ecb84a2014-12-30 14:33:22 +0100840 compositor->base.launcher = weston_launcher_connect(&compositor->base,
841 param->tty, "seat0",
842 false);
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700843 if (!compositor->base.launcher) {
David Herrmann1641d142013-10-15 14:29:57 +0200844 weston_log("fatal: fbdev backend should be run "
845 "using weston-launch binary or as root\n");
Philip Withnall4f499172013-02-02 12:02:32 +0000846 goto out_udev;
847 }
848
849 compositor->base.destroy = fbdev_compositor_destroy;
850 compositor->base.restore = fbdev_restore;
851
Philip Withnall4f499172013-02-02 12:02:32 +0000852 compositor->prev_state = WESTON_COMPOSITOR_ACTIVE;
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300853 compositor->use_pixman = !param->use_gl;
Philip Withnall4f499172013-02-02 12:02:32 +0000854
855 for (key = KEY_F1; key < KEY_F9; key++)
856 weston_compositor_add_key_binding(&compositor->base, key,
857 MODIFIER_CTRL | MODIFIER_ALT,
858 switch_vt_binding,
859 compositor);
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300860 if (compositor->use_pixman) {
861 if (pixman_renderer_init(&compositor->base) < 0)
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700862 goto out_launcher;
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300863 } else {
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300864 gl_renderer = weston_load_module("gl-renderer.so",
865 "gl_renderer_interface");
866 if (!gl_renderer) {
867 weston_log("could not load gl renderer\n");
868 goto out_launcher;
869 }
870
871 if (gl_renderer->create(&compositor->base, EGL_DEFAULT_DISPLAY,
872 gl_renderer->opaque_attribs,
873 NULL) < 0) {
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300874 weston_log("gl_renderer_create failed.\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700875 goto out_launcher;
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300876 }
877 }
Philip Withnall4f499172013-02-02 12:02:32 +0000878
879 if (fbdev_output_create(compositor, param->device) < 0)
880 goto out_pixman;
881
Rob Bradford2387fde2013-05-31 18:09:57 +0100882 udev_input_init(&compositor->input, &compositor->base, compositor->udev, seat_id);
Philip Withnall4f499172013-02-02 12:02:32 +0000883
884 return &compositor->base;
885
886out_pixman:
887 compositor->base.renderer->destroy(&compositor->base);
888
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700889out_launcher:
890 weston_launcher_destroy(compositor->base.launcher);
Philip Withnall4f499172013-02-02 12:02:32 +0000891
892out_udev:
893 udev_unref(compositor->udev);
894
895out_compositor:
896 weston_compositor_shutdown(&compositor->base);
897
898out_free:
899 free(compositor);
900
901 return NULL;
902}
903
904WL_EXPORT struct weston_compositor *
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500905backend_init(struct wl_display *display, int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400906 struct weston_config *config)
Philip Withnall4f499172013-02-02 12:02:32 +0000907{
908 /* TODO: Ideally, available frame buffers should be enumerated using
909 * udev, rather than passing a device node in as a parameter. */
910 struct fbdev_parameters param = {
911 .tty = 0, /* default to current tty */
912 .device = "/dev/fb0", /* default frame buffer */
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300913 .use_gl = 0,
Philip Withnall4f499172013-02-02 12:02:32 +0000914 };
915
916 const struct weston_option fbdev_options[] = {
917 { WESTON_OPTION_INTEGER, "tty", 0, &param.tty },
918 { WESTON_OPTION_STRING, "device", 0, &param.device },
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300919 { WESTON_OPTION_BOOLEAN, "use-gl", 0, &param.use_gl },
Philip Withnall4f499172013-02-02 12:02:32 +0000920 };
921
922 parse_options(fbdev_options, ARRAY_LENGTH(fbdev_options), argc, argv);
923
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400924 return fbdev_compositor_create(display, argc, argv, config, &param);
Philip Withnall4f499172013-02-02 12:02:32 +0000925}