blob: 04fb718892d110a7e0af329709b7e8c56ec7d486 [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 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -07007 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
Philip Withnall4f499172013-02-02 12:02:32 +000014 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -070015 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial
17 * portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
23 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
24 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
Philip Withnall4f499172013-02-02 12:02:32 +000027 */
28
Daniel Stonec228e232013-05-22 18:03:19 +030029#include "config.h"
Philip Withnall4f499172013-02-02 12:02:32 +000030
31#include <errno.h>
32#include <stdlib.h>
33#include <stdio.h>
34#include <string.h>
35#include <math.h>
36#include <sys/mman.h>
37#include <sys/types.h>
38#include <fcntl.h>
39#include <unistd.h>
40#include <linux/fb.h>
Kristian Høgsberg7e597f22013-02-18 16:35:26 -050041#include <linux/input.h>
Philip Withnall4f499172013-02-02 12:02:32 +000042
43#include <libudev.h>
44
Philip Withnall4f499172013-02-02 12:02:32 +000045#include "compositor.h"
46#include "launcher-util.h"
47#include "pixman-renderer.h"
Peter Hutterer823ad332014-11-26 07:06:31 +100048#include "libinput-seat.h"
Adrian Negreanu4aa756d2013-09-06 15:16:09 +030049#include "gl-renderer.h"
Pekka Paalanen363aa7b2014-12-17 16:20:40 +020050#include "presentation_timing-server-protocol.h"
Philip Withnall4f499172013-02-02 12:02:32 +000051
52struct fbdev_compositor {
53 struct weston_compositor base;
54 uint32_t prev_state;
55
56 struct udev *udev;
Rob Bradfordd355b802013-05-31 18:09:55 +010057 struct udev_input input;
Adrian Negreanu4aa756d2013-09-06 15:16:09 +030058 int use_pixman;
Kristian Høgsberg61741a22013-09-17 16:02:57 -070059 struct wl_listener session_listener;
Philip Withnall4f499172013-02-02 12:02:32 +000060};
61
62struct fbdev_screeninfo {
63 unsigned int x_resolution; /* pixels, visible area */
64 unsigned int y_resolution; /* pixels, visible area */
65 unsigned int width_mm; /* visible screen width in mm */
66 unsigned int height_mm; /* visible screen height in mm */
67 unsigned int bits_per_pixel;
68
69 size_t buffer_length; /* length of frame buffer memory in bytes */
70 size_t line_length; /* length of a line in bytes */
71 char id[16]; /* screen identifier */
72
73 pixman_format_code_t pixel_format; /* frame buffer pixel format */
74 unsigned int refresh_rate; /* Hertz */
75};
76
77struct fbdev_output {
78 struct fbdev_compositor *compositor;
79 struct weston_output base;
80
81 struct weston_mode mode;
82 struct wl_event_source *finish_frame_timer;
83
84 /* Frame buffer details. */
85 const char *device; /* ownership shared with fbdev_parameters */
86 struct fbdev_screeninfo fb_info;
87 void *fb; /* length is fb_info.buffer_length */
88
89 /* pixman details. */
90 pixman_image_t *hw_surface;
91 pixman_image_t *shadow_surface;
92 void *shadow_buf;
93 uint8_t depth;
94};
95
Philip Withnall4f499172013-02-02 12:02:32 +000096struct fbdev_parameters {
97 int tty;
98 char *device;
Adrian Negreanu4aa756d2013-09-06 15:16:09 +030099 int use_gl;
Philip Withnall4f499172013-02-02 12:02:32 +0000100};
101
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300102struct gl_renderer_interface *gl_renderer;
103
Kristian Høgsberg7e597f22013-02-18 16:35:26 -0500104static const char default_seat[] = "seat0";
105
Philip Withnall4f499172013-02-02 12:02:32 +0000106static inline struct fbdev_output *
107to_fbdev_output(struct weston_output *base)
108{
109 return container_of(base, struct fbdev_output, base);
110}
111
Philip Withnall4f499172013-02-02 12:02:32 +0000112static inline struct fbdev_compositor *
113to_fbdev_compositor(struct weston_compositor *base)
114{
115 return container_of(base, struct fbdev_compositor, base);
116}
117
118static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200119fbdev_output_start_repaint_loop(struct weston_output *output)
120{
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400121 struct timespec ts;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200122
Pekka Paalanen662f3842015-03-18 12:17:26 +0200123 weston_compositor_read_presentation_clock(output->compositor, &ts);
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200124 weston_output_finish_frame(output, &ts, PRESENTATION_FEEDBACK_INVALID);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200125}
126
127static void
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300128fbdev_output_repaint_pixman(struct weston_output *base, pixman_region32_t *damage)
Philip Withnall4f499172013-02-02 12:02:32 +0000129{
130 struct fbdev_output *output = to_fbdev_output(base);
131 struct weston_compositor *ec = output->base.compositor;
132 pixman_box32_t *rects;
Derek Foreman561662b2015-03-17 13:22:38 -0500133 int nrects, i;
Philip Withnall4f499172013-02-02 12:02:32 +0000134
135 /* Repaint the damaged region onto the back buffer. */
136 pixman_renderer_output_set_buffer(base, output->shadow_surface);
137 ec->renderer->repaint_output(base, damage);
138
139 /* Transform and composite onto the frame buffer. */
Philip Withnall4f499172013-02-02 12:02:32 +0000140 rects = pixman_region32_rectangles(damage, &nrects);
141
142 for (i = 0; i < nrects; i++) {
Derek Foreman561662b2015-03-17 13:22:38 -0500143 pixman_box32_t transformed_rect;
144 int width, height;
Philip Withnall4f499172013-02-02 12:02:32 +0000145
Derek Foreman561662b2015-03-17 13:22:38 -0500146 transformed_rect = weston_transformed_rect(base->width,
147 base->height,
148 base->transform,
149 1, rects[i]);
150 width = transformed_rect.x2 - transformed_rect.x1;
151 height = transformed_rect.y2 - transformed_rect.y1;
Philip Withnall4f499172013-02-02 12:02:32 +0000152 pixman_image_composite32(PIXMAN_OP_SRC,
153 output->shadow_surface, /* src */
154 NULL /* mask */,
155 output->hw_surface, /* dest */
Derek Foreman561662b2015-03-17 13:22:38 -0500156 transformed_rect.x1, /* src_x */
157 transformed_rect.y1, /* src_y */
Philip Withnall4f499172013-02-02 12:02:32 +0000158 0, 0, /* mask_x, mask_y */
Derek Foreman561662b2015-03-17 13:22:38 -0500159 transformed_rect.x1, /* dest_x */
160 transformed_rect.y1, /* dest_y */
161 width, /* width */
162 height /* height */);
Philip Withnall4f499172013-02-02 12:02:32 +0000163 }
164
165 /* Update the damage region. */
166 pixman_region32_subtract(&ec->primary_plane.damage,
167 &ec->primary_plane.damage, damage);
168
169 /* Schedule the end of the frame. We do not sync this to the frame
170 * buffer clock because users who want that should be using the DRM
171 * compositor. FBIO_WAITFORVSYNC blocks and FB_ACTIVATE_VBL requires
172 * panning, which is broken in most kernel drivers.
173 *
174 * Finish the frame synchronised to the specified refresh rate. The
175 * refresh rate is given in mHz and the interval in ms. */
176 wl_event_source_timer_update(output->finish_frame_timer,
177 1000000 / output->mode.refresh);
178}
179
David Herrmann1edf44c2013-10-22 17:11:26 +0200180static int
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300181fbdev_output_repaint(struct weston_output *base, pixman_region32_t *damage)
182{
183 struct fbdev_output *output = to_fbdev_output(base);
184 struct fbdev_compositor *fbc = output->compositor;
185 struct weston_compositor *ec = & fbc->base;
186
187 if (fbc->use_pixman) {
188 fbdev_output_repaint_pixman(base,damage);
189 } else {
190 ec->renderer->repaint_output(base, damage);
191 /* Update the damage region. */
192 pixman_region32_subtract(&ec->primary_plane.damage,
193 &ec->primary_plane.damage, damage);
194
195 wl_event_source_timer_update(output->finish_frame_timer,
196 1000000 / output->mode.refresh);
197 }
David Herrmann1edf44c2013-10-22 17:11:26 +0200198
199 return 0;
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300200}
201
Philip Withnall4f499172013-02-02 12:02:32 +0000202static int
203finish_frame_handler(void *data)
204{
205 struct fbdev_output *output = data;
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200206 struct timespec ts;
Philip Withnall4f499172013-02-02 12:02:32 +0000207
Pekka Paalanen662f3842015-03-18 12:17:26 +0200208 weston_compositor_read_presentation_clock(output->base.compositor, &ts);
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200209 weston_output_finish_frame(&output->base, &ts, 0);
Philip Withnall4f499172013-02-02 12:02:32 +0000210
211 return 1;
212}
213
214static pixman_format_code_t
215calculate_pixman_format(struct fb_var_screeninfo *vinfo,
216 struct fb_fix_screeninfo *finfo)
217{
218 /* Calculate the pixman format supported by the frame buffer from the
219 * buffer's metadata. Return 0 if no known pixman format is supported
220 * (since this has depth 0 it's guaranteed to not conflict with any
221 * actual pixman format).
222 *
223 * Documentation on the vinfo and finfo structures:
224 * http://www.mjmwired.net/kernel/Documentation/fb/api.txt
225 *
226 * TODO: Try a bit harder to support other formats, including setting
227 * the preferred format in the hardware. */
228 int type;
229
230 weston_log("Calculating pixman format from:\n"
231 STAMP_SPACE " - type: %i (aux: %i)\n"
232 STAMP_SPACE " - visual: %i\n"
233 STAMP_SPACE " - bpp: %i (grayscale: %i)\n"
234 STAMP_SPACE " - red: offset: %i, length: %i, MSB: %i\n"
235 STAMP_SPACE " - green: offset: %i, length: %i, MSB: %i\n"
236 STAMP_SPACE " - blue: offset: %i, length: %i, MSB: %i\n"
237 STAMP_SPACE " - transp: offset: %i, length: %i, MSB: %i\n",
238 finfo->type, finfo->type_aux, finfo->visual,
239 vinfo->bits_per_pixel, vinfo->grayscale,
240 vinfo->red.offset, vinfo->red.length, vinfo->red.msb_right,
241 vinfo->green.offset, vinfo->green.length,
242 vinfo->green.msb_right,
243 vinfo->blue.offset, vinfo->blue.length,
244 vinfo->blue.msb_right,
245 vinfo->transp.offset, vinfo->transp.length,
246 vinfo->transp.msb_right);
247
248 /* We only handle packed formats at the moment. */
249 if (finfo->type != FB_TYPE_PACKED_PIXELS)
250 return 0;
251
252 /* We only handle true-colour frame buffers at the moment. */
Marc Chalainffbddff2013-09-03 16:47:43 +0200253 switch(finfo->visual) {
254 case FB_VISUAL_TRUECOLOR:
255 case FB_VISUAL_DIRECTCOLOR:
256 if (vinfo->grayscale != 0)
257 return 0;
258 break;
259 default:
260 return 0;
261 }
Philip Withnall4f499172013-02-02 12:02:32 +0000262
263 /* We only support formats with MSBs on the left. */
264 if (vinfo->red.msb_right != 0 || vinfo->green.msb_right != 0 ||
265 vinfo->blue.msb_right != 0)
266 return 0;
267
268 /* Work out the format type from the offsets. We only support RGBA and
269 * ARGB at the moment. */
270 type = PIXMAN_TYPE_OTHER;
271
272 if ((vinfo->transp.offset >= vinfo->red.offset ||
273 vinfo->transp.length == 0) &&
274 vinfo->red.offset >= vinfo->green.offset &&
275 vinfo->green.offset >= vinfo->blue.offset)
276 type = PIXMAN_TYPE_ARGB;
277 else if (vinfo->red.offset >= vinfo->green.offset &&
278 vinfo->green.offset >= vinfo->blue.offset &&
279 vinfo->blue.offset >= vinfo->transp.offset)
280 type = PIXMAN_TYPE_RGBA;
281
282 if (type == PIXMAN_TYPE_OTHER)
283 return 0;
284
285 /* Build the format. */
286 return PIXMAN_FORMAT(vinfo->bits_per_pixel, type,
287 vinfo->transp.length,
288 vinfo->red.length,
289 vinfo->green.length,
290 vinfo->blue.length);
291}
292
293static int
294calculate_refresh_rate(struct fb_var_screeninfo *vinfo)
295{
296 uint64_t quot;
297
298 /* Calculate monitor refresh rate. Default is 60 Hz. Units are mHz. */
299 quot = (vinfo->upper_margin + vinfo->lower_margin + vinfo->yres);
300 quot *= (vinfo->left_margin + vinfo->right_margin + vinfo->xres);
301 quot *= vinfo->pixclock;
302
303 if (quot > 0) {
304 uint64_t refresh_rate;
305
306 refresh_rate = 1000000000000000LLU / quot;
307 if (refresh_rate > 200000)
308 refresh_rate = 200000; /* cap at 200 Hz */
309
310 return refresh_rate;
311 }
312
313 return 60 * 1000; /* default to 60 Hz */
314}
315
316static int
317fbdev_query_screen_info(struct fbdev_output *output, int fd,
318 struct fbdev_screeninfo *info)
319{
320 struct fb_var_screeninfo varinfo;
321 struct fb_fix_screeninfo fixinfo;
322
323 /* Probe the device for screen information. */
324 if (ioctl(fd, FBIOGET_FSCREENINFO, &fixinfo) < 0 ||
325 ioctl(fd, FBIOGET_VSCREENINFO, &varinfo) < 0) {
326 return -1;
327 }
328
329 /* Store the pertinent data. */
330 info->x_resolution = varinfo.xres;
331 info->y_resolution = varinfo.yres;
332 info->width_mm = varinfo.width;
333 info->height_mm = varinfo.height;
334 info->bits_per_pixel = varinfo.bits_per_pixel;
335
336 info->buffer_length = fixinfo.smem_len;
337 info->line_length = fixinfo.line_length;
338 strncpy(info->id, fixinfo.id, sizeof(info->id) / sizeof(*info->id));
339
340 info->pixel_format = calculate_pixman_format(&varinfo, &fixinfo);
341 info->refresh_rate = calculate_refresh_rate(&varinfo);
342
343 if (info->pixel_format == 0) {
344 weston_log("Frame buffer uses an unsupported format.\n");
345 return -1;
346 }
347
348 return 1;
349}
350
351static int
352fbdev_set_screen_info(struct fbdev_output *output, int fd,
353 struct fbdev_screeninfo *info)
354{
355 struct fb_var_screeninfo varinfo;
356
357 /* Grab the current screen information. */
358 if (ioctl(fd, FBIOGET_VSCREENINFO, &varinfo) < 0) {
359 return -1;
360 }
361
362 /* Update the information. */
363 varinfo.xres = info->x_resolution;
364 varinfo.yres = info->y_resolution;
365 varinfo.width = info->width_mm;
366 varinfo.height = info->height_mm;
367 varinfo.bits_per_pixel = info->bits_per_pixel;
368
369 /* Try to set up an ARGB (x8r8g8b8) pixel format. */
370 varinfo.grayscale = 0;
371 varinfo.transp.offset = 24;
372 varinfo.transp.length = 0;
373 varinfo.transp.msb_right = 0;
374 varinfo.red.offset = 16;
375 varinfo.red.length = 8;
376 varinfo.red.msb_right = 0;
377 varinfo.green.offset = 8;
378 varinfo.green.length = 8;
379 varinfo.green.msb_right = 0;
380 varinfo.blue.offset = 0;
381 varinfo.blue.length = 8;
382 varinfo.blue.msb_right = 0;
383
384 /* Set the device's screen information. */
385 if (ioctl(fd, FBIOPUT_VSCREENINFO, &varinfo) < 0) {
386 return -1;
387 }
388
389 return 1;
390}
391
392static void fbdev_frame_buffer_destroy(struct fbdev_output *output);
393
394/* Returns an FD for the frame buffer device. */
395static int
396fbdev_frame_buffer_open(struct fbdev_output *output, const char *fb_dev,
397 struct fbdev_screeninfo *screen_info)
398{
399 int fd = -1;
400
401 weston_log("Opening fbdev frame buffer.\n");
402
403 /* Open the frame buffer device. */
404 fd = open(fb_dev, O_RDWR | O_CLOEXEC);
405 if (fd < 0) {
406 weston_log("Failed to open frame buffer device ‘%s’: %s\n",
407 fb_dev, strerror(errno));
408 return -1;
409 }
410
411 /* Grab the screen info. */
412 if (fbdev_query_screen_info(output, fd, screen_info) < 0) {
413 weston_log("Failed to get frame buffer info: %s\n",
414 strerror(errno));
415
416 close(fd);
417 return -1;
418 }
419
420 return fd;
421}
422
423/* Closes the FD on success or failure. */
424static int
425fbdev_frame_buffer_map(struct fbdev_output *output, int fd)
426{
427 int retval = -1;
428
429 weston_log("Mapping fbdev frame buffer.\n");
430
431 /* Map the frame buffer. Write-only mode, since we don't want to read
432 * anything back (because it's slow). */
433 output->fb = mmap(NULL, output->fb_info.buffer_length,
434 PROT_WRITE, MAP_SHARED, fd, 0);
435 if (output->fb == MAP_FAILED) {
436 weston_log("Failed to mmap frame buffer: %s\n",
437 strerror(errno));
438 goto out_close;
439 }
440
441 /* Create a pixman image to wrap the memory mapped frame buffer. */
442 output->hw_surface =
443 pixman_image_create_bits(output->fb_info.pixel_format,
444 output->fb_info.x_resolution,
445 output->fb_info.y_resolution,
446 output->fb,
447 output->fb_info.line_length);
448 if (output->hw_surface == NULL) {
449 weston_log("Failed to create surface for frame buffer.\n");
450 goto out_unmap;
451 }
452
453 /* Success! */
454 retval = 0;
455
456out_unmap:
457 if (retval != 0 && output->fb != NULL)
458 fbdev_frame_buffer_destroy(output);
459
460out_close:
461 if (fd >= 0)
462 close(fd);
463
464 return retval;
465}
466
467static void
468fbdev_frame_buffer_destroy(struct fbdev_output *output)
469{
470 weston_log("Destroying fbdev frame buffer.\n");
471
472 if (munmap(output->fb, output->fb_info.buffer_length) < 0)
473 weston_log("Failed to munmap frame buffer: %s\n",
474 strerror(errno));
475
476 output->fb = NULL;
477}
478
479static void fbdev_output_destroy(struct weston_output *base);
480static void fbdev_output_disable(struct weston_output *base);
481
482static int
483fbdev_output_create(struct fbdev_compositor *compositor,
484 const char *device)
485{
486 struct fbdev_output *output;
Derek Foreman44ed70b2015-03-17 13:22:37 -0500487 struct weston_config_section *section;
Philip Withnall4f499172013-02-02 12:02:32 +0000488 int fb_fd;
Philip Withnall4f499172013-02-02 12:02:32 +0000489 int width, height;
490 unsigned int bytes_per_pixel;
491 struct wl_event_loop *loop;
Derek Foreman44ed70b2015-03-17 13:22:37 -0500492 uint32_t config_transform;
493 char *s;
Philip Withnall4f499172013-02-02 12:02:32 +0000494
495 weston_log("Creating fbdev output.\n");
496
Bryce Harringtonde16d892014-11-20 22:21:57 -0800497 output = zalloc(sizeof *output);
498 if (output == NULL)
Philip Withnall4f499172013-02-02 12:02:32 +0000499 return -1;
500
501 output->compositor = compositor;
502 output->device = device;
503
504 /* Create the frame buffer. */
505 fb_fd = fbdev_frame_buffer_open(output, device, &output->fb_info);
506 if (fb_fd < 0) {
507 weston_log("Creating frame buffer failed.\n");
508 goto out_free;
509 }
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300510 if (compositor->use_pixman) {
511 if (fbdev_frame_buffer_map(output, fb_fd) < 0) {
512 weston_log("Mapping frame buffer failed.\n");
513 goto out_free;
514 }
Kristian Høgsberg8ac6a2d2013-10-09 09:59:06 -0700515 } else {
516 close(fb_fd);
Philip Withnall4f499172013-02-02 12:02:32 +0000517 }
518
Jonas Ådahle5a12252013-04-05 23:07:11 +0200519 output->base.start_repaint_loop = fbdev_output_start_repaint_loop;
Philip Withnall4f499172013-02-02 12:02:32 +0000520 output->base.repaint = fbdev_output_repaint;
521 output->base.destroy = fbdev_output_destroy;
Philip Withnall4f499172013-02-02 12:02:32 +0000522
523 /* only one static mode in list */
524 output->mode.flags =
525 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
526 output->mode.width = output->fb_info.x_resolution;
527 output->mode.height = output->fb_info.y_resolution;
528 output->mode.refresh = output->fb_info.refresh_rate;
529 wl_list_init(&output->base.mode_list);
530 wl_list_insert(&output->base.mode_list, &output->mode.link);
531
Hardeningff39efa2013-09-18 23:56:35 +0200532 output->base.current_mode = &output->mode;
Philip Withnall4f499172013-02-02 12:02:32 +0000533 output->base.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
534 output->base.make = "unknown";
535 output->base.model = output->fb_info.id;
Derek Foremane516c3b2015-03-17 13:22:34 -0500536 output->base.name = strdup("fbdev");
Philip Withnall4f499172013-02-02 12:02:32 +0000537
Derek Foreman44ed70b2015-03-17 13:22:37 -0500538 section = weston_config_get_section(compositor->base.config,
539 "output", "name",
540 output->base.name);
541 weston_config_section_get_string(section, "transform", &s, "normal");
542 if (weston_parse_transform(s, &config_transform) < 0)
543 weston_log("Invalid transform \"%s\" for output %s\n",
544 s, output->base.name);
545 free(s);
546
Philip Withnall4f499172013-02-02 12:02:32 +0000547 weston_output_init(&output->base, &compositor->base,
548 0, 0, output->fb_info.width_mm,
549 output->fb_info.height_mm,
Derek Foreman44ed70b2015-03-17 13:22:37 -0500550 config_transform,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200551 1);
Philip Withnall4f499172013-02-02 12:02:32 +0000552
Derek Foreman561662b2015-03-17 13:22:38 -0500553 width = output->mode.width;
554 height = output->mode.height;
Philip Withnall4f499172013-02-02 12:02:32 +0000555 bytes_per_pixel = output->fb_info.bits_per_pixel / 8;
556
557 output->shadow_buf = malloc(width * height * bytes_per_pixel);
558 output->shadow_surface =
559 pixman_image_create_bits(output->fb_info.pixel_format,
Derek Foreman561662b2015-03-17 13:22:38 -0500560 width, height,
Philip Withnall4f499172013-02-02 12:02:32 +0000561 output->shadow_buf,
Derek Foreman561662b2015-03-17 13:22:38 -0500562 width * bytes_per_pixel);
Philip Withnall4f499172013-02-02 12:02:32 +0000563 if (output->shadow_buf == NULL || output->shadow_surface == NULL) {
564 weston_log("Failed to create surface for frame buffer.\n");
565 goto out_hw_surface;
566 }
567
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300568 if (compositor->use_pixman) {
569 if (pixman_renderer_output_create(&output->base) < 0)
570 goto out_shadow_surface;
571 } else {
572 setenv("HYBRIS_EGLPLATFORM", "wayland", 1);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300573 if (gl_renderer->output_create(&output->base,
Jonny Lamb671148f2015-03-20 15:26:52 +0100574 (EGLNativeWindowType)NULL, NULL,
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000575 gl_renderer->opaque_attribs,
Derek Foremane76f1852015-05-15 12:12:39 -0500576 NULL, 0) < 0) {
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300577 weston_log("gl_renderer_output_create failed.\n");
578 goto out_shadow_surface;
579 }
580 }
581
Philip Withnall4f499172013-02-02 12:02:32 +0000582
583 loop = wl_display_get_event_loop(compositor->base.wl_display);
584 output->finish_frame_timer =
585 wl_event_loop_add_timer(loop, finish_frame_handler, output);
586
Giulio Camuffob1147152015-05-06 21:41:57 +0300587 weston_compositor_add_output(&compositor->base, &output->base);
Philip Withnall4f499172013-02-02 12:02:32 +0000588
589 weston_log("fbdev output %d×%d px\n",
590 output->mode.width, output->mode.height);
591 weston_log_continue(STAMP_SPACE "guessing %d Hz and 96 dpi\n",
592 output->mode.refresh / 1000);
593
594 return 0;
595
596out_shadow_surface:
597 pixman_image_unref(output->shadow_surface);
598 output->shadow_surface = NULL;
599out_hw_surface:
600 free(output->shadow_buf);
601 pixman_image_unref(output->hw_surface);
602 output->hw_surface = NULL;
603 weston_output_destroy(&output->base);
604 fbdev_frame_buffer_destroy(output);
605out_free:
606 free(output);
607
608 return -1;
609}
610
611static void
612fbdev_output_destroy(struct weston_output *base)
613{
614 struct fbdev_output *output = to_fbdev_output(base);
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300615 struct fbdev_compositor *compositor = output->compositor;
Philip Withnall4f499172013-02-02 12:02:32 +0000616
617 weston_log("Destroying fbdev output.\n");
618
619 /* Close the frame buffer. */
620 fbdev_output_disable(base);
621
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300622 if (compositor->use_pixman) {
623 if (base->renderer_state != NULL)
624 pixman_renderer_output_destroy(base);
Philip Withnall4f499172013-02-02 12:02:32 +0000625
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300626 if (output->shadow_surface != NULL) {
627 pixman_image_unref(output->shadow_surface);
628 output->shadow_surface = NULL;
629 }
Philip Withnall4f499172013-02-02 12:02:32 +0000630
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300631 if (output->shadow_buf != NULL) {
632 free(output->shadow_buf);
633 output->shadow_buf = NULL;
634 }
635 } else {
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300636 gl_renderer->output_destroy(base);
Philip Withnall4f499172013-02-02 12:02:32 +0000637 }
638
639 /* Remove the output. */
Philip Withnall4f499172013-02-02 12:02:32 +0000640 weston_output_destroy(&output->base);
641
642 free(output);
643}
644
645/* strcmp()-style return values. */
646static int
647compare_screen_info (const struct fbdev_screeninfo *a,
648 const struct fbdev_screeninfo *b)
649{
650 if (a->x_resolution == b->x_resolution &&
651 a->y_resolution == b->y_resolution &&
652 a->width_mm == b->width_mm &&
653 a->height_mm == b->height_mm &&
654 a->bits_per_pixel == b->bits_per_pixel &&
655 a->pixel_format == b->pixel_format &&
656 a->refresh_rate == b->refresh_rate)
657 return 0;
658
659 return 1;
660}
661
662static int
663fbdev_output_reenable(struct fbdev_compositor *compositor,
664 struct weston_output *base)
665{
666 struct fbdev_output *output = to_fbdev_output(base);
667 struct fbdev_screeninfo new_screen_info;
668 int fb_fd;
Rob Bradfordf8ef42f2013-07-26 16:29:38 +0100669 const char *device;
Philip Withnall4f499172013-02-02 12:02:32 +0000670
671 weston_log("Re-enabling fbdev output.\n");
672
673 /* Create the frame buffer. */
674 fb_fd = fbdev_frame_buffer_open(output, output->device,
675 &new_screen_info);
676 if (fb_fd < 0) {
677 weston_log("Creating frame buffer failed.\n");
678 goto err;
679 }
680
681 /* Check whether the frame buffer details have changed since we were
682 * disabled. */
683 if (compare_screen_info (&output->fb_info, &new_screen_info) != 0) {
684 /* Perform a mode-set to restore the old mode. */
685 if (fbdev_set_screen_info(output, fb_fd,
686 &output->fb_info) < 0) {
687 weston_log("Failed to restore mode settings. "
688 "Attempting to re-open output anyway.\n");
689 }
690
Rob Bradford581b3fd2013-07-26 16:29:39 +0100691 close(fb_fd);
692
Philip Withnall4f499172013-02-02 12:02:32 +0000693 /* Remove and re-add the output so that resources depending on
694 * the frame buffer X/Y resolution (such as the shadow buffer)
695 * are re-initialised. */
Rob Bradfordf8ef42f2013-07-26 16:29:38 +0100696 device = output->device;
Philip Withnall4f499172013-02-02 12:02:32 +0000697 fbdev_output_destroy(base);
Rob Bradfordf8ef42f2013-07-26 16:29:38 +0100698 fbdev_output_create(compositor, device);
Philip Withnall4f499172013-02-02 12:02:32 +0000699
700 return 0;
701 }
702
703 /* Map the device if it has the same details as before. */
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300704 if (compositor->use_pixman) {
705 if (fbdev_frame_buffer_map(output, fb_fd) < 0) {
706 weston_log("Mapping frame buffer failed.\n");
707 goto err;
708 }
Philip Withnall4f499172013-02-02 12:02:32 +0000709 }
710
711 return 0;
712
713err:
714 return -1;
715}
716
717/* NOTE: This leaves output->fb_info populated, caching data so that if
718 * fbdev_output_reenable() is called again, it can determine whether a mode-set
719 * is needed. */
720static void
721fbdev_output_disable(struct weston_output *base)
722{
723 struct fbdev_output *output = to_fbdev_output(base);
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300724 struct fbdev_compositor *compositor = output->compositor;
Philip Withnall4f499172013-02-02 12:02:32 +0000725
726 weston_log("Disabling fbdev output.\n");
727
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300728 if ( ! compositor->use_pixman) return;
729
Philip Withnall4f499172013-02-02 12:02:32 +0000730 if (output->hw_surface != NULL) {
731 pixman_image_unref(output->hw_surface);
732 output->hw_surface = NULL;
733 }
734
735 fbdev_frame_buffer_destroy(output);
736}
737
738static void
Philip Withnall4f499172013-02-02 12:02:32 +0000739fbdev_compositor_destroy(struct weston_compositor *base)
740{
741 struct fbdev_compositor *compositor = to_fbdev_compositor(base);
Philip Withnall4f499172013-02-02 12:02:32 +0000742
Rob Bradfordd355b802013-05-31 18:09:55 +0100743 udev_input_destroy(&compositor->input);
Philip Withnall4f499172013-02-02 12:02:32 +0000744
745 /* Destroy the output. */
746 weston_compositor_shutdown(&compositor->base);
747
748 /* Chain up. */
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700749 weston_launcher_destroy(compositor->base.launcher);
Philip Withnall4f499172013-02-02 12:02:32 +0000750
751 free(compositor);
752}
753
754static void
Kristian Høgsberg61741a22013-09-17 16:02:57 -0700755session_notify(struct wl_listener *listener, void *data)
Philip Withnall4f499172013-02-02 12:02:32 +0000756{
Kristian Høgsberg61741a22013-09-17 16:02:57 -0700757 struct fbdev_compositor *compositor = data;
Philip Withnall4f499172013-02-02 12:02:32 +0000758 struct weston_output *output;
759
Kristian Høgsberg61741a22013-09-17 16:02:57 -0700760 if (compositor->base.session_active) {
Philip Withnall4f499172013-02-02 12:02:32 +0000761 weston_log("entering VT\n");
Philip Withnall4f499172013-02-02 12:02:32 +0000762 compositor->base.state = compositor->prev_state;
763
764 wl_list_for_each(output, &compositor->base.output_list, link) {
765 fbdev_output_reenable(compositor, output);
766 }
767
768 weston_compositor_damage_all(&compositor->base);
769
Jonas Ådahl0feb32e2014-03-12 22:08:41 +0100770 udev_input_enable(&compositor->input);
Kristian Høgsberg61741a22013-09-17 16:02:57 -0700771 } else {
Philip Withnall4f499172013-02-02 12:02:32 +0000772 weston_log("leaving VT\n");
Rob Bradfordd355b802013-05-31 18:09:55 +0100773 udev_input_disable(&compositor->input);
Philip Withnall4f499172013-02-02 12:02:32 +0000774
775 wl_list_for_each(output, &compositor->base.output_list, link) {
776 fbdev_output_disable(output);
777 }
778
Philip Withnall4f499172013-02-02 12:02:32 +0000779 compositor->prev_state = compositor->base.state;
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +0100780 weston_compositor_offscreen(&compositor->base);
Philip Withnall4f499172013-02-02 12:02:32 +0000781
782 /* If we have a repaint scheduled (from the idle handler), make
783 * sure we cancel that so we don't try to pageflip when we're
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +0100784 * vt switched away. The OFFSCREEN state will prevent
Philip Withnall4f499172013-02-02 12:02:32 +0000785 * further attemps at repainting. When we switch
786 * back, we schedule a repaint, which will process
787 * pending frame callbacks. */
788
789 wl_list_for_each(output,
790 &compositor->base.output_list, link) {
791 output->repaint_needed = 0;
792 }
nerdopolis38946702014-12-03 15:53:03 +0000793 }
Philip Withnall4f499172013-02-02 12:02:32 +0000794}
795
796static void
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700797fbdev_restore(struct weston_compositor *compositor)
Philip Withnall4f499172013-02-02 12:02:32 +0000798{
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700799 weston_launcher_restore(compositor->launcher);
Philip Withnall4f499172013-02-02 12:02:32 +0000800}
801
802static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400803switch_vt_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Philip Withnall4f499172013-02-02 12:02:32 +0000804{
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700805 struct weston_compositor *compositor = data;
Philip Withnall4f499172013-02-02 12:02:32 +0000806
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700807 weston_launcher_activate_vt(compositor->launcher, key - KEY_F1 + 1);
Philip Withnall4f499172013-02-02 12:02:32 +0000808}
809
810static struct weston_compositor *
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500811fbdev_compositor_create(struct wl_display *display, int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400812 struct weston_config *config,
813 struct fbdev_parameters *param)
Philip Withnall4f499172013-02-02 12:02:32 +0000814{
815 struct fbdev_compositor *compositor;
Rob Bradford2387fde2013-05-31 18:09:57 +0100816 const char *seat_id = default_seat;
Philip Withnall4f499172013-02-02 12:02:32 +0000817 uint32_t key;
818
819 weston_log("initializing fbdev backend\n");
820
Bryce Harringtonde16d892014-11-20 22:21:57 -0800821 compositor = zalloc(sizeof *compositor);
Philip Withnall4f499172013-02-02 12:02:32 +0000822 if (compositor == NULL)
823 return NULL;
824
825 if (weston_compositor_init(&compositor->base, display, argc, argv,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400826 config) < 0)
Philip Withnall4f499172013-02-02 12:02:32 +0000827 goto out_free;
828
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400829 if (weston_compositor_set_presentation_clock_software(
830 &compositor->base) < 0)
831 goto out_compositor;
832
Philip Withnall4f499172013-02-02 12:02:32 +0000833 compositor->udev = udev_new();
834 if (compositor->udev == NULL) {
835 weston_log("Failed to initialize udev context.\n");
836 goto out_compositor;
837 }
838
839 /* Set up the TTY. */
Kristian Høgsberg61741a22013-09-17 16:02:57 -0700840 compositor->session_listener.notify = session_notify;
841 wl_signal_add(&compositor->base.session_signal,
842 &compositor->session_listener);
David Herrmann2ecb84a2014-12-30 14:33:22 +0100843 compositor->base.launcher = weston_launcher_connect(&compositor->base,
844 param->tty, "seat0",
845 false);
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700846 if (!compositor->base.launcher) {
David Herrmann1641d142013-10-15 14:29:57 +0200847 weston_log("fatal: fbdev backend should be run "
848 "using weston-launch binary or as root\n");
Philip Withnall4f499172013-02-02 12:02:32 +0000849 goto out_udev;
850 }
851
852 compositor->base.destroy = fbdev_compositor_destroy;
853 compositor->base.restore = fbdev_restore;
854
Philip Withnall4f499172013-02-02 12:02:32 +0000855 compositor->prev_state = WESTON_COMPOSITOR_ACTIVE;
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300856 compositor->use_pixman = !param->use_gl;
Philip Withnall4f499172013-02-02 12:02:32 +0000857
858 for (key = KEY_F1; key < KEY_F9; key++)
859 weston_compositor_add_key_binding(&compositor->base, key,
860 MODIFIER_CTRL | MODIFIER_ALT,
861 switch_vt_binding,
862 compositor);
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300863 if (compositor->use_pixman) {
864 if (pixman_renderer_init(&compositor->base) < 0)
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700865 goto out_launcher;
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300866 } else {
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300867 gl_renderer = weston_load_module("gl-renderer.so",
868 "gl_renderer_interface");
869 if (!gl_renderer) {
870 weston_log("could not load gl renderer\n");
871 goto out_launcher;
872 }
873
Jonny Lamb74eed312015-03-24 13:12:04 +0100874 if (gl_renderer->create(&compositor->base, NO_EGL_PLATFORM,
875 EGL_DEFAULT_DISPLAY,
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300876 gl_renderer->opaque_attribs,
Derek Foremane76f1852015-05-15 12:12:39 -0500877 NULL, 0) < 0) {
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300878 weston_log("gl_renderer_create failed.\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700879 goto out_launcher;
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300880 }
881 }
Philip Withnall4f499172013-02-02 12:02:32 +0000882
883 if (fbdev_output_create(compositor, param->device) < 0)
884 goto out_pixman;
885
Rob Bradford2387fde2013-05-31 18:09:57 +0100886 udev_input_init(&compositor->input, &compositor->base, compositor->udev, seat_id);
Philip Withnall4f499172013-02-02 12:02:32 +0000887
888 return &compositor->base;
889
890out_pixman:
891 compositor->base.renderer->destroy(&compositor->base);
892
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700893out_launcher:
894 weston_launcher_destroy(compositor->base.launcher);
Philip Withnall4f499172013-02-02 12:02:32 +0000895
896out_udev:
897 udev_unref(compositor->udev);
898
899out_compositor:
900 weston_compositor_shutdown(&compositor->base);
901
902out_free:
903 free(compositor);
904
905 return NULL;
906}
907
908WL_EXPORT struct weston_compositor *
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500909backend_init(struct wl_display *display, int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400910 struct weston_config *config)
Philip Withnall4f499172013-02-02 12:02:32 +0000911{
912 /* TODO: Ideally, available frame buffers should be enumerated using
913 * udev, rather than passing a device node in as a parameter. */
914 struct fbdev_parameters param = {
915 .tty = 0, /* default to current tty */
916 .device = "/dev/fb0", /* default frame buffer */
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300917 .use_gl = 0,
Philip Withnall4f499172013-02-02 12:02:32 +0000918 };
919
920 const struct weston_option fbdev_options[] = {
921 { WESTON_OPTION_INTEGER, "tty", 0, &param.tty },
922 { WESTON_OPTION_STRING, "device", 0, &param.device },
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300923 { WESTON_OPTION_BOOLEAN, "use-gl", 0, &param.use_gl },
Philip Withnall4f499172013-02-02 12:02:32 +0000924 };
925
926 parse_options(fbdev_options, ARRAY_LENGTH(fbdev_options), argc, argv);
927
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400928 return fbdev_compositor_create(display, argc, argv, config, &param);
Philip Withnall4f499172013-02-02 12:02:32 +0000929}