blob: 051a381a99cced0fefb6256014e51c8b479b9b3b [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
Jon Cruz35b2eaa2015-06-15 15:37:08 -070045#include "shared/helpers.h"
Philip Withnall4f499172013-02-02 12:02:32 +000046#include "compositor.h"
47#include "launcher-util.h"
48#include "pixman-renderer.h"
Peter Hutterer823ad332014-11-26 07:06:31 +100049#include "libinput-seat.h"
Adrian Negreanu4aa756d2013-09-06 15:16:09 +030050#include "gl-renderer.h"
Pekka Paalanen363aa7b2014-12-17 16:20:40 +020051#include "presentation_timing-server-protocol.h"
Philip Withnall4f499172013-02-02 12:02:32 +000052
Giulio Camuffo954f1832014-10-11 18:27:30 +030053struct fbdev_backend {
54 struct weston_backend base;
55 struct weston_compositor *compositor;
Philip Withnall4f499172013-02-02 12:02:32 +000056 uint32_t prev_state;
57
58 struct udev *udev;
Rob Bradfordd355b802013-05-31 18:09:55 +010059 struct udev_input input;
Adrian Negreanu4aa756d2013-09-06 15:16:09 +030060 int use_pixman;
Kristian Høgsberg61741a22013-09-17 16:02:57 -070061 struct wl_listener session_listener;
Philip Withnall4f499172013-02-02 12:02:32 +000062};
63
64struct fbdev_screeninfo {
65 unsigned int x_resolution; /* pixels, visible area */
66 unsigned int y_resolution; /* pixels, visible area */
67 unsigned int width_mm; /* visible screen width in mm */
68 unsigned int height_mm; /* visible screen height in mm */
69 unsigned int bits_per_pixel;
70
71 size_t buffer_length; /* length of frame buffer memory in bytes */
72 size_t line_length; /* length of a line in bytes */
73 char id[16]; /* screen identifier */
74
75 pixman_format_code_t pixel_format; /* frame buffer pixel format */
76 unsigned int refresh_rate; /* Hertz */
77};
78
79struct fbdev_output {
Giulio Camuffo954f1832014-10-11 18:27:30 +030080 struct fbdev_backend *backend;
Philip Withnall4f499172013-02-02 12:02:32 +000081 struct weston_output base;
82
83 struct weston_mode mode;
84 struct wl_event_source *finish_frame_timer;
85
86 /* Frame buffer details. */
87 const char *device; /* ownership shared with fbdev_parameters */
88 struct fbdev_screeninfo fb_info;
89 void *fb; /* length is fb_info.buffer_length */
90
91 /* pixman details. */
92 pixman_image_t *hw_surface;
93 pixman_image_t *shadow_surface;
94 void *shadow_buf;
95 uint8_t depth;
96};
97
Philip Withnall4f499172013-02-02 12:02:32 +000098struct fbdev_parameters {
99 int tty;
100 char *device;
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300101 int use_gl;
Philip Withnall4f499172013-02-02 12:02:32 +0000102};
103
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300104struct gl_renderer_interface *gl_renderer;
105
Kristian Høgsberg7e597f22013-02-18 16:35:26 -0500106static const char default_seat[] = "seat0";
107
Philip Withnall4f499172013-02-02 12:02:32 +0000108static inline struct fbdev_output *
109to_fbdev_output(struct weston_output *base)
110{
111 return container_of(base, struct fbdev_output, base);
112}
113
Giulio Camuffo954f1832014-10-11 18:27:30 +0300114static inline struct fbdev_backend *
115to_fbdev_backend(struct weston_compositor *base)
Philip Withnall4f499172013-02-02 12:02:32 +0000116{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300117 return container_of(base->backend, struct fbdev_backend, base);
Philip Withnall4f499172013-02-02 12:02:32 +0000118}
119
120static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200121fbdev_output_start_repaint_loop(struct weston_output *output)
122{
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400123 struct timespec ts;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200124
Pekka Paalanen662f3842015-03-18 12:17:26 +0200125 weston_compositor_read_presentation_clock(output->compositor, &ts);
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200126 weston_output_finish_frame(output, &ts, PRESENTATION_FEEDBACK_INVALID);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200127}
128
129static void
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300130fbdev_output_repaint_pixman(struct weston_output *base, pixman_region32_t *damage)
Philip Withnall4f499172013-02-02 12:02:32 +0000131{
132 struct fbdev_output *output = to_fbdev_output(base);
133 struct weston_compositor *ec = output->base.compositor;
134 pixman_box32_t *rects;
Derek Foreman561662b2015-03-17 13:22:38 -0500135 int nrects, i;
Philip Withnall4f499172013-02-02 12:02:32 +0000136
137 /* Repaint the damaged region onto the back buffer. */
138 pixman_renderer_output_set_buffer(base, output->shadow_surface);
139 ec->renderer->repaint_output(base, damage);
140
141 /* Transform and composite onto the frame buffer. */
Philip Withnall4f499172013-02-02 12:02:32 +0000142 rects = pixman_region32_rectangles(damage, &nrects);
143
144 for (i = 0; i < nrects; i++) {
Derek Foreman561662b2015-03-17 13:22:38 -0500145 pixman_box32_t transformed_rect;
146 int width, height;
Philip Withnall4f499172013-02-02 12:02:32 +0000147
Derek Foreman561662b2015-03-17 13:22:38 -0500148 transformed_rect = weston_transformed_rect(base->width,
149 base->height,
150 base->transform,
151 1, rects[i]);
152 width = transformed_rect.x2 - transformed_rect.x1;
153 height = transformed_rect.y2 - transformed_rect.y1;
Philip Withnall4f499172013-02-02 12:02:32 +0000154 pixman_image_composite32(PIXMAN_OP_SRC,
155 output->shadow_surface, /* src */
156 NULL /* mask */,
157 output->hw_surface, /* dest */
Derek Foreman561662b2015-03-17 13:22:38 -0500158 transformed_rect.x1, /* src_x */
159 transformed_rect.y1, /* src_y */
Philip Withnall4f499172013-02-02 12:02:32 +0000160 0, 0, /* mask_x, mask_y */
Derek Foreman561662b2015-03-17 13:22:38 -0500161 transformed_rect.x1, /* dest_x */
162 transformed_rect.y1, /* dest_y */
163 width, /* width */
164 height /* height */);
Philip Withnall4f499172013-02-02 12:02:32 +0000165 }
166
167 /* Update the damage region. */
168 pixman_region32_subtract(&ec->primary_plane.damage,
169 &ec->primary_plane.damage, damage);
170
171 /* Schedule the end of the frame. We do not sync this to the frame
172 * buffer clock because users who want that should be using the DRM
173 * compositor. FBIO_WAITFORVSYNC blocks and FB_ACTIVATE_VBL requires
174 * panning, which is broken in most kernel drivers.
175 *
176 * Finish the frame synchronised to the specified refresh rate. The
177 * refresh rate is given in mHz and the interval in ms. */
178 wl_event_source_timer_update(output->finish_frame_timer,
179 1000000 / output->mode.refresh);
180}
181
David Herrmann1edf44c2013-10-22 17:11:26 +0200182static int
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300183fbdev_output_repaint(struct weston_output *base, pixman_region32_t *damage)
184{
185 struct fbdev_output *output = to_fbdev_output(base);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300186 struct fbdev_backend *fbb = output->backend;
187 struct weston_compositor *ec = fbb->compositor;
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300188
Giulio Camuffo954f1832014-10-11 18:27:30 +0300189 if (fbb->use_pixman) {
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300190 fbdev_output_repaint_pixman(base,damage);
191 } else {
192 ec->renderer->repaint_output(base, damage);
193 /* Update the damage region. */
194 pixman_region32_subtract(&ec->primary_plane.damage,
195 &ec->primary_plane.damage, damage);
196
197 wl_event_source_timer_update(output->finish_frame_timer,
198 1000000 / output->mode.refresh);
199 }
David Herrmann1edf44c2013-10-22 17:11:26 +0200200
201 return 0;
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300202}
203
Philip Withnall4f499172013-02-02 12:02:32 +0000204static int
205finish_frame_handler(void *data)
206{
207 struct fbdev_output *output = data;
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200208 struct timespec ts;
Philip Withnall4f499172013-02-02 12:02:32 +0000209
Pekka Paalanen662f3842015-03-18 12:17:26 +0200210 weston_compositor_read_presentation_clock(output->base.compositor, &ts);
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200211 weston_output_finish_frame(&output->base, &ts, 0);
Philip Withnall4f499172013-02-02 12:02:32 +0000212
213 return 1;
214}
215
216static pixman_format_code_t
217calculate_pixman_format(struct fb_var_screeninfo *vinfo,
218 struct fb_fix_screeninfo *finfo)
219{
220 /* Calculate the pixman format supported by the frame buffer from the
221 * buffer's metadata. Return 0 if no known pixman format is supported
222 * (since this has depth 0 it's guaranteed to not conflict with any
223 * actual pixman format).
224 *
225 * Documentation on the vinfo and finfo structures:
226 * http://www.mjmwired.net/kernel/Documentation/fb/api.txt
227 *
228 * TODO: Try a bit harder to support other formats, including setting
229 * the preferred format in the hardware. */
230 int type;
231
232 weston_log("Calculating pixman format from:\n"
233 STAMP_SPACE " - type: %i (aux: %i)\n"
234 STAMP_SPACE " - visual: %i\n"
235 STAMP_SPACE " - bpp: %i (grayscale: %i)\n"
236 STAMP_SPACE " - red: offset: %i, length: %i, MSB: %i\n"
237 STAMP_SPACE " - green: offset: %i, length: %i, MSB: %i\n"
238 STAMP_SPACE " - blue: offset: %i, length: %i, MSB: %i\n"
239 STAMP_SPACE " - transp: offset: %i, length: %i, MSB: %i\n",
240 finfo->type, finfo->type_aux, finfo->visual,
241 vinfo->bits_per_pixel, vinfo->grayscale,
242 vinfo->red.offset, vinfo->red.length, vinfo->red.msb_right,
243 vinfo->green.offset, vinfo->green.length,
244 vinfo->green.msb_right,
245 vinfo->blue.offset, vinfo->blue.length,
246 vinfo->blue.msb_right,
247 vinfo->transp.offset, vinfo->transp.length,
248 vinfo->transp.msb_right);
249
250 /* We only handle packed formats at the moment. */
251 if (finfo->type != FB_TYPE_PACKED_PIXELS)
252 return 0;
253
254 /* We only handle true-colour frame buffers at the moment. */
Marc Chalainffbddff2013-09-03 16:47:43 +0200255 switch(finfo->visual) {
256 case FB_VISUAL_TRUECOLOR:
257 case FB_VISUAL_DIRECTCOLOR:
258 if (vinfo->grayscale != 0)
259 return 0;
260 break;
261 default:
262 return 0;
263 }
Philip Withnall4f499172013-02-02 12:02:32 +0000264
265 /* We only support formats with MSBs on the left. */
266 if (vinfo->red.msb_right != 0 || vinfo->green.msb_right != 0 ||
267 vinfo->blue.msb_right != 0)
268 return 0;
269
270 /* Work out the format type from the offsets. We only support RGBA and
271 * ARGB at the moment. */
272 type = PIXMAN_TYPE_OTHER;
273
274 if ((vinfo->transp.offset >= vinfo->red.offset ||
275 vinfo->transp.length == 0) &&
276 vinfo->red.offset >= vinfo->green.offset &&
277 vinfo->green.offset >= vinfo->blue.offset)
278 type = PIXMAN_TYPE_ARGB;
279 else if (vinfo->red.offset >= vinfo->green.offset &&
280 vinfo->green.offset >= vinfo->blue.offset &&
281 vinfo->blue.offset >= vinfo->transp.offset)
282 type = PIXMAN_TYPE_RGBA;
283
284 if (type == PIXMAN_TYPE_OTHER)
285 return 0;
286
287 /* Build the format. */
288 return PIXMAN_FORMAT(vinfo->bits_per_pixel, type,
289 vinfo->transp.length,
290 vinfo->red.length,
291 vinfo->green.length,
292 vinfo->blue.length);
293}
294
295static int
296calculate_refresh_rate(struct fb_var_screeninfo *vinfo)
297{
298 uint64_t quot;
299
300 /* Calculate monitor refresh rate. Default is 60 Hz. Units are mHz. */
301 quot = (vinfo->upper_margin + vinfo->lower_margin + vinfo->yres);
302 quot *= (vinfo->left_margin + vinfo->right_margin + vinfo->xres);
303 quot *= vinfo->pixclock;
304
305 if (quot > 0) {
306 uint64_t refresh_rate;
307
308 refresh_rate = 1000000000000000LLU / quot;
309 if (refresh_rate > 200000)
310 refresh_rate = 200000; /* cap at 200 Hz */
311
312 return refresh_rate;
313 }
314
315 return 60 * 1000; /* default to 60 Hz */
316}
317
318static int
319fbdev_query_screen_info(struct fbdev_output *output, int fd,
320 struct fbdev_screeninfo *info)
321{
322 struct fb_var_screeninfo varinfo;
323 struct fb_fix_screeninfo fixinfo;
324
325 /* Probe the device for screen information. */
326 if (ioctl(fd, FBIOGET_FSCREENINFO, &fixinfo) < 0 ||
327 ioctl(fd, FBIOGET_VSCREENINFO, &varinfo) < 0) {
328 return -1;
329 }
330
331 /* Store the pertinent data. */
332 info->x_resolution = varinfo.xres;
333 info->y_resolution = varinfo.yres;
334 info->width_mm = varinfo.width;
335 info->height_mm = varinfo.height;
336 info->bits_per_pixel = varinfo.bits_per_pixel;
337
338 info->buffer_length = fixinfo.smem_len;
339 info->line_length = fixinfo.line_length;
340 strncpy(info->id, fixinfo.id, sizeof(info->id) / sizeof(*info->id));
341
342 info->pixel_format = calculate_pixman_format(&varinfo, &fixinfo);
343 info->refresh_rate = calculate_refresh_rate(&varinfo);
344
345 if (info->pixel_format == 0) {
346 weston_log("Frame buffer uses an unsupported format.\n");
347 return -1;
348 }
349
350 return 1;
351}
352
353static int
354fbdev_set_screen_info(struct fbdev_output *output, int fd,
355 struct fbdev_screeninfo *info)
356{
357 struct fb_var_screeninfo varinfo;
358
359 /* Grab the current screen information. */
360 if (ioctl(fd, FBIOGET_VSCREENINFO, &varinfo) < 0) {
361 return -1;
362 }
363
364 /* Update the information. */
365 varinfo.xres = info->x_resolution;
366 varinfo.yres = info->y_resolution;
367 varinfo.width = info->width_mm;
368 varinfo.height = info->height_mm;
369 varinfo.bits_per_pixel = info->bits_per_pixel;
370
371 /* Try to set up an ARGB (x8r8g8b8) pixel format. */
372 varinfo.grayscale = 0;
373 varinfo.transp.offset = 24;
374 varinfo.transp.length = 0;
375 varinfo.transp.msb_right = 0;
376 varinfo.red.offset = 16;
377 varinfo.red.length = 8;
378 varinfo.red.msb_right = 0;
379 varinfo.green.offset = 8;
380 varinfo.green.length = 8;
381 varinfo.green.msb_right = 0;
382 varinfo.blue.offset = 0;
383 varinfo.blue.length = 8;
384 varinfo.blue.msb_right = 0;
385
386 /* Set the device's screen information. */
387 if (ioctl(fd, FBIOPUT_VSCREENINFO, &varinfo) < 0) {
388 return -1;
389 }
390
391 return 1;
392}
393
394static void fbdev_frame_buffer_destroy(struct fbdev_output *output);
395
396/* Returns an FD for the frame buffer device. */
397static int
398fbdev_frame_buffer_open(struct fbdev_output *output, const char *fb_dev,
399 struct fbdev_screeninfo *screen_info)
400{
401 int fd = -1;
402
403 weston_log("Opening fbdev frame buffer.\n");
404
405 /* Open the frame buffer device. */
406 fd = open(fb_dev, O_RDWR | O_CLOEXEC);
407 if (fd < 0) {
408 weston_log("Failed to open frame buffer device ‘%s’: %s\n",
409 fb_dev, strerror(errno));
410 return -1;
411 }
412
413 /* Grab the screen info. */
414 if (fbdev_query_screen_info(output, fd, screen_info) < 0) {
415 weston_log("Failed to get frame buffer info: %s\n",
416 strerror(errno));
417
418 close(fd);
419 return -1;
420 }
421
422 return fd;
423}
424
425/* Closes the FD on success or failure. */
426static int
427fbdev_frame_buffer_map(struct fbdev_output *output, int fd)
428{
429 int retval = -1;
430
431 weston_log("Mapping fbdev frame buffer.\n");
432
433 /* Map the frame buffer. Write-only mode, since we don't want to read
434 * anything back (because it's slow). */
435 output->fb = mmap(NULL, output->fb_info.buffer_length,
436 PROT_WRITE, MAP_SHARED, fd, 0);
437 if (output->fb == MAP_FAILED) {
438 weston_log("Failed to mmap frame buffer: %s\n",
439 strerror(errno));
440 goto out_close;
441 }
442
443 /* Create a pixman image to wrap the memory mapped frame buffer. */
444 output->hw_surface =
445 pixman_image_create_bits(output->fb_info.pixel_format,
446 output->fb_info.x_resolution,
447 output->fb_info.y_resolution,
448 output->fb,
449 output->fb_info.line_length);
450 if (output->hw_surface == NULL) {
451 weston_log("Failed to create surface for frame buffer.\n");
452 goto out_unmap;
453 }
454
455 /* Success! */
456 retval = 0;
457
458out_unmap:
459 if (retval != 0 && output->fb != NULL)
460 fbdev_frame_buffer_destroy(output);
461
462out_close:
463 if (fd >= 0)
464 close(fd);
465
466 return retval;
467}
468
469static void
470fbdev_frame_buffer_destroy(struct fbdev_output *output)
471{
472 weston_log("Destroying fbdev frame buffer.\n");
473
474 if (munmap(output->fb, output->fb_info.buffer_length) < 0)
475 weston_log("Failed to munmap frame buffer: %s\n",
476 strerror(errno));
477
478 output->fb = NULL;
479}
480
481static void fbdev_output_destroy(struct weston_output *base);
482static void fbdev_output_disable(struct weston_output *base);
483
484static int
Giulio Camuffo954f1832014-10-11 18:27:30 +0300485fbdev_output_create(struct fbdev_backend *backend,
Philip Withnall4f499172013-02-02 12:02:32 +0000486 const char *device)
487{
488 struct fbdev_output *output;
Derek Foreman44ed70b2015-03-17 13:22:37 -0500489 struct weston_config_section *section;
Philip Withnall4f499172013-02-02 12:02:32 +0000490 int fb_fd;
Philip Withnall4f499172013-02-02 12:02:32 +0000491 int width, height;
492 unsigned int bytes_per_pixel;
493 struct wl_event_loop *loop;
Derek Foreman44ed70b2015-03-17 13:22:37 -0500494 uint32_t config_transform;
495 char *s;
Philip Withnall4f499172013-02-02 12:02:32 +0000496
497 weston_log("Creating fbdev output.\n");
498
Bryce Harringtonde16d892014-11-20 22:21:57 -0800499 output = zalloc(sizeof *output);
500 if (output == NULL)
Philip Withnall4f499172013-02-02 12:02:32 +0000501 return -1;
502
Giulio Camuffo954f1832014-10-11 18:27:30 +0300503 output->backend = backend;
Philip Withnall4f499172013-02-02 12:02:32 +0000504 output->device = device;
505
506 /* Create the frame buffer. */
507 fb_fd = fbdev_frame_buffer_open(output, device, &output->fb_info);
508 if (fb_fd < 0) {
509 weston_log("Creating frame buffer failed.\n");
510 goto out_free;
511 }
Giulio Camuffo954f1832014-10-11 18:27:30 +0300512 if (backend->use_pixman) {
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300513 if (fbdev_frame_buffer_map(output, fb_fd) < 0) {
514 weston_log("Mapping frame buffer failed.\n");
515 goto out_free;
516 }
Kristian Høgsberg8ac6a2d2013-10-09 09:59:06 -0700517 } else {
518 close(fb_fd);
Philip Withnall4f499172013-02-02 12:02:32 +0000519 }
520
Jonas Ådahle5a12252013-04-05 23:07:11 +0200521 output->base.start_repaint_loop = fbdev_output_start_repaint_loop;
Philip Withnall4f499172013-02-02 12:02:32 +0000522 output->base.repaint = fbdev_output_repaint;
523 output->base.destroy = fbdev_output_destroy;
Philip Withnall4f499172013-02-02 12:02:32 +0000524
525 /* only one static mode in list */
526 output->mode.flags =
527 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
528 output->mode.width = output->fb_info.x_resolution;
529 output->mode.height = output->fb_info.y_resolution;
530 output->mode.refresh = output->fb_info.refresh_rate;
531 wl_list_init(&output->base.mode_list);
532 wl_list_insert(&output->base.mode_list, &output->mode.link);
533
Hardeningff39efa2013-09-18 23:56:35 +0200534 output->base.current_mode = &output->mode;
Philip Withnall4f499172013-02-02 12:02:32 +0000535 output->base.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
536 output->base.make = "unknown";
537 output->base.model = output->fb_info.id;
Derek Foremane516c3b2015-03-17 13:22:34 -0500538 output->base.name = strdup("fbdev");
Philip Withnall4f499172013-02-02 12:02:32 +0000539
Giulio Camuffo954f1832014-10-11 18:27:30 +0300540 section = weston_config_get_section(backend->compositor->config,
Derek Foreman44ed70b2015-03-17 13:22:37 -0500541 "output", "name",
542 output->base.name);
543 weston_config_section_get_string(section, "transform", &s, "normal");
544 if (weston_parse_transform(s, &config_transform) < 0)
545 weston_log("Invalid transform \"%s\" for output %s\n",
546 s, output->base.name);
547 free(s);
548
Giulio Camuffo954f1832014-10-11 18:27:30 +0300549 weston_output_init(&output->base, backend->compositor,
Philip Withnall4f499172013-02-02 12:02:32 +0000550 0, 0, output->fb_info.width_mm,
551 output->fb_info.height_mm,
Derek Foreman44ed70b2015-03-17 13:22:37 -0500552 config_transform,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200553 1);
Philip Withnall4f499172013-02-02 12:02:32 +0000554
Derek Foreman561662b2015-03-17 13:22:38 -0500555 width = output->mode.width;
556 height = output->mode.height;
Philip Withnall4f499172013-02-02 12:02:32 +0000557 bytes_per_pixel = output->fb_info.bits_per_pixel / 8;
558
559 output->shadow_buf = malloc(width * height * bytes_per_pixel);
560 output->shadow_surface =
561 pixman_image_create_bits(output->fb_info.pixel_format,
Derek Foreman561662b2015-03-17 13:22:38 -0500562 width, height,
Philip Withnall4f499172013-02-02 12:02:32 +0000563 output->shadow_buf,
Derek Foreman561662b2015-03-17 13:22:38 -0500564 width * bytes_per_pixel);
Philip Withnall4f499172013-02-02 12:02:32 +0000565 if (output->shadow_buf == NULL || output->shadow_surface == NULL) {
566 weston_log("Failed to create surface for frame buffer.\n");
567 goto out_hw_surface;
568 }
569
Giulio Camuffo954f1832014-10-11 18:27:30 +0300570 if (backend->use_pixman) {
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300571 if (pixman_renderer_output_create(&output->base) < 0)
572 goto out_shadow_surface;
573 } else {
574 setenv("HYBRIS_EGLPLATFORM", "wayland", 1);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300575 if (gl_renderer->output_create(&output->base,
Jonny Lamb671148f2015-03-20 15:26:52 +0100576 (EGLNativeWindowType)NULL, NULL,
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000577 gl_renderer->opaque_attribs,
Derek Foremane76f1852015-05-15 12:12:39 -0500578 NULL, 0) < 0) {
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300579 weston_log("gl_renderer_output_create failed.\n");
580 goto out_shadow_surface;
581 }
582 }
583
Philip Withnall4f499172013-02-02 12:02:32 +0000584
Giulio Camuffo954f1832014-10-11 18:27:30 +0300585 loop = wl_display_get_event_loop(backend->compositor->wl_display);
Philip Withnall4f499172013-02-02 12:02:32 +0000586 output->finish_frame_timer =
587 wl_event_loop_add_timer(loop, finish_frame_handler, output);
588
Giulio Camuffo954f1832014-10-11 18:27:30 +0300589 weston_compositor_add_output(backend->compositor, &output->base);
Philip Withnall4f499172013-02-02 12:02:32 +0000590
591 weston_log("fbdev output %d×%d px\n",
592 output->mode.width, output->mode.height);
593 weston_log_continue(STAMP_SPACE "guessing %d Hz and 96 dpi\n",
594 output->mode.refresh / 1000);
595
596 return 0;
597
598out_shadow_surface:
599 pixman_image_unref(output->shadow_surface);
600 output->shadow_surface = NULL;
601out_hw_surface:
602 free(output->shadow_buf);
603 pixman_image_unref(output->hw_surface);
604 output->hw_surface = NULL;
605 weston_output_destroy(&output->base);
606 fbdev_frame_buffer_destroy(output);
607out_free:
608 free(output);
609
610 return -1;
611}
612
613static void
614fbdev_output_destroy(struct weston_output *base)
615{
616 struct fbdev_output *output = to_fbdev_output(base);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300617 struct fbdev_backend *backend = output->backend;
Philip Withnall4f499172013-02-02 12:02:32 +0000618
619 weston_log("Destroying fbdev output.\n");
620
621 /* Close the frame buffer. */
622 fbdev_output_disable(base);
623
Giulio Camuffo954f1832014-10-11 18:27:30 +0300624 if (backend->use_pixman) {
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300625 if (base->renderer_state != NULL)
626 pixman_renderer_output_destroy(base);
Philip Withnall4f499172013-02-02 12:02:32 +0000627
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300628 if (output->shadow_surface != NULL) {
629 pixman_image_unref(output->shadow_surface);
630 output->shadow_surface = NULL;
631 }
Philip Withnall4f499172013-02-02 12:02:32 +0000632
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300633 if (output->shadow_buf != NULL) {
634 free(output->shadow_buf);
635 output->shadow_buf = NULL;
636 }
637 } else {
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300638 gl_renderer->output_destroy(base);
Philip Withnall4f499172013-02-02 12:02:32 +0000639 }
640
641 /* Remove the output. */
Philip Withnall4f499172013-02-02 12:02:32 +0000642 weston_output_destroy(&output->base);
643
644 free(output);
645}
646
647/* strcmp()-style return values. */
648static int
649compare_screen_info (const struct fbdev_screeninfo *a,
650 const struct fbdev_screeninfo *b)
651{
652 if (a->x_resolution == b->x_resolution &&
653 a->y_resolution == b->y_resolution &&
654 a->width_mm == b->width_mm &&
655 a->height_mm == b->height_mm &&
656 a->bits_per_pixel == b->bits_per_pixel &&
657 a->pixel_format == b->pixel_format &&
658 a->refresh_rate == b->refresh_rate)
659 return 0;
660
661 return 1;
662}
663
664static int
Giulio Camuffo954f1832014-10-11 18:27:30 +0300665fbdev_output_reenable(struct fbdev_backend *backend,
Philip Withnall4f499172013-02-02 12:02:32 +0000666 struct weston_output *base)
667{
668 struct fbdev_output *output = to_fbdev_output(base);
669 struct fbdev_screeninfo new_screen_info;
670 int fb_fd;
Rob Bradfordf8ef42f2013-07-26 16:29:38 +0100671 const char *device;
Philip Withnall4f499172013-02-02 12:02:32 +0000672
673 weston_log("Re-enabling fbdev output.\n");
674
675 /* Create the frame buffer. */
676 fb_fd = fbdev_frame_buffer_open(output, output->device,
677 &new_screen_info);
678 if (fb_fd < 0) {
679 weston_log("Creating frame buffer failed.\n");
680 goto err;
681 }
682
683 /* Check whether the frame buffer details have changed since we were
684 * disabled. */
685 if (compare_screen_info (&output->fb_info, &new_screen_info) != 0) {
686 /* Perform a mode-set to restore the old mode. */
687 if (fbdev_set_screen_info(output, fb_fd,
688 &output->fb_info) < 0) {
689 weston_log("Failed to restore mode settings. "
690 "Attempting to re-open output anyway.\n");
691 }
692
Rob Bradford581b3fd2013-07-26 16:29:39 +0100693 close(fb_fd);
694
Philip Withnall4f499172013-02-02 12:02:32 +0000695 /* Remove and re-add the output so that resources depending on
696 * the frame buffer X/Y resolution (such as the shadow buffer)
697 * are re-initialised. */
Rob Bradfordf8ef42f2013-07-26 16:29:38 +0100698 device = output->device;
Philip Withnall4f499172013-02-02 12:02:32 +0000699 fbdev_output_destroy(base);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300700 fbdev_output_create(backend, device);
Philip Withnall4f499172013-02-02 12:02:32 +0000701
702 return 0;
703 }
704
705 /* Map the device if it has the same details as before. */
Giulio Camuffo954f1832014-10-11 18:27:30 +0300706 if (backend->use_pixman) {
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300707 if (fbdev_frame_buffer_map(output, fb_fd) < 0) {
708 weston_log("Mapping frame buffer failed.\n");
709 goto err;
710 }
Philip Withnall4f499172013-02-02 12:02:32 +0000711 }
712
713 return 0;
714
715err:
716 return -1;
717}
718
719/* NOTE: This leaves output->fb_info populated, caching data so that if
720 * fbdev_output_reenable() is called again, it can determine whether a mode-set
721 * is needed. */
722static void
723fbdev_output_disable(struct weston_output *base)
724{
725 struct fbdev_output *output = to_fbdev_output(base);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300726 struct fbdev_backend *backend = output->backend;
Philip Withnall4f499172013-02-02 12:02:32 +0000727
728 weston_log("Disabling fbdev output.\n");
729
Giulio Camuffo954f1832014-10-11 18:27:30 +0300730 if ( ! backend->use_pixman) return;
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300731
Philip Withnall4f499172013-02-02 12:02:32 +0000732 if (output->hw_surface != NULL) {
733 pixman_image_unref(output->hw_surface);
734 output->hw_surface = NULL;
735 }
736
737 fbdev_frame_buffer_destroy(output);
738}
739
740static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300741fbdev_backend_destroy(struct weston_compositor *base)
Philip Withnall4f499172013-02-02 12:02:32 +0000742{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300743 struct fbdev_backend *backend = to_fbdev_backend(base);
Philip Withnall4f499172013-02-02 12:02:32 +0000744
Giulio Camuffo954f1832014-10-11 18:27:30 +0300745 udev_input_destroy(&backend->input);
Philip Withnall4f499172013-02-02 12:02:32 +0000746
747 /* Destroy the output. */
Giulio Camuffo954f1832014-10-11 18:27:30 +0300748 weston_compositor_shutdown(base);
Philip Withnall4f499172013-02-02 12:02:32 +0000749
750 /* Chain up. */
Giulio Camuffo954f1832014-10-11 18:27:30 +0300751 weston_launcher_destroy(base->launcher);
Philip Withnall4f499172013-02-02 12:02:32 +0000752
Giulio Camuffo954f1832014-10-11 18:27:30 +0300753 free(backend);
Philip Withnall4f499172013-02-02 12:02:32 +0000754}
755
756static void
Kristian Høgsberg61741a22013-09-17 16:02:57 -0700757session_notify(struct wl_listener *listener, void *data)
Philip Withnall4f499172013-02-02 12:02:32 +0000758{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300759 struct fbdev_backend *backend = data;
760 struct weston_compositor *compositor = backend->compositor;
Philip Withnall4f499172013-02-02 12:02:32 +0000761 struct weston_output *output;
762
Giulio Camuffo954f1832014-10-11 18:27:30 +0300763 if (compositor->session_active) {
Philip Withnall4f499172013-02-02 12:02:32 +0000764 weston_log("entering VT\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +0300765 compositor->state = backend->prev_state;
Philip Withnall4f499172013-02-02 12:02:32 +0000766
Giulio Camuffo954f1832014-10-11 18:27:30 +0300767 wl_list_for_each(output, &compositor->output_list, link) {
768 fbdev_output_reenable(backend, output);
Philip Withnall4f499172013-02-02 12:02:32 +0000769 }
770
Giulio Camuffo954f1832014-10-11 18:27:30 +0300771 weston_compositor_damage_all(compositor);
Philip Withnall4f499172013-02-02 12:02:32 +0000772
Giulio Camuffo954f1832014-10-11 18:27:30 +0300773 udev_input_enable(&backend->input);
Kristian Høgsberg61741a22013-09-17 16:02:57 -0700774 } else {
Philip Withnall4f499172013-02-02 12:02:32 +0000775 weston_log("leaving VT\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +0300776 udev_input_disable(&backend->input);
Philip Withnall4f499172013-02-02 12:02:32 +0000777
Giulio Camuffo954f1832014-10-11 18:27:30 +0300778 wl_list_for_each(output, &compositor->output_list, link) {
Philip Withnall4f499172013-02-02 12:02:32 +0000779 fbdev_output_disable(output);
780 }
781
Giulio Camuffo954f1832014-10-11 18:27:30 +0300782 backend->prev_state = compositor->state;
783 weston_compositor_offscreen(compositor);
Philip Withnall4f499172013-02-02 12:02:32 +0000784
785 /* If we have a repaint scheduled (from the idle handler), make
786 * sure we cancel that so we don't try to pageflip when we're
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +0100787 * vt switched away. The OFFSCREEN state will prevent
Philip Withnall4f499172013-02-02 12:02:32 +0000788 * further attemps at repainting. When we switch
789 * back, we schedule a repaint, which will process
790 * pending frame callbacks. */
791
792 wl_list_for_each(output,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300793 &compositor->output_list, link) {
Philip Withnall4f499172013-02-02 12:02:32 +0000794 output->repaint_needed = 0;
795 }
nerdopolis38946702014-12-03 15:53:03 +0000796 }
Philip Withnall4f499172013-02-02 12:02:32 +0000797}
798
799static void
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700800fbdev_restore(struct weston_compositor *compositor)
Philip Withnall4f499172013-02-02 12:02:32 +0000801{
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700802 weston_launcher_restore(compositor->launcher);
Philip Withnall4f499172013-02-02 12:02:32 +0000803}
804
805static void
Derek Foreman8ae2db52015-07-15 13:00:36 -0500806switch_vt_binding(struct weston_keyboard *keyboard, uint32_t time,
807 uint32_t key, void *data)
Philip Withnall4f499172013-02-02 12:02:32 +0000808{
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700809 struct weston_compositor *compositor = data;
Philip Withnall4f499172013-02-02 12:02:32 +0000810
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700811 weston_launcher_activate_vt(compositor->launcher, key - KEY_F1 + 1);
Philip Withnall4f499172013-02-02 12:02:32 +0000812}
813
Giulio Camuffo954f1832014-10-11 18:27:30 +0300814static struct fbdev_backend *
815fbdev_backend_create(struct weston_compositor *compositor, int *argc, char *argv[],
816 struct weston_config *config,
817 struct fbdev_parameters *param)
Philip Withnall4f499172013-02-02 12:02:32 +0000818{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300819 struct fbdev_backend *backend;
Rob Bradford2387fde2013-05-31 18:09:57 +0100820 const char *seat_id = default_seat;
Philip Withnall4f499172013-02-02 12:02:32 +0000821 uint32_t key;
822
823 weston_log("initializing fbdev backend\n");
824
Giulio Camuffo954f1832014-10-11 18:27:30 +0300825 backend = zalloc(sizeof *backend);
826 if (backend == NULL)
Philip Withnall4f499172013-02-02 12:02:32 +0000827 return NULL;
828
Giulio Camuffo954f1832014-10-11 18:27:30 +0300829 backend->compositor = compositor;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400830 if (weston_compositor_set_presentation_clock_software(
Giulio Camuffo954f1832014-10-11 18:27:30 +0300831 compositor) < 0)
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400832 goto out_compositor;
833
Giulio Camuffo954f1832014-10-11 18:27:30 +0300834 backend->udev = udev_new();
835 if (backend->udev == NULL) {
Philip Withnall4f499172013-02-02 12:02:32 +0000836 weston_log("Failed to initialize udev context.\n");
837 goto out_compositor;
838 }
839
840 /* Set up the TTY. */
Giulio Camuffo954f1832014-10-11 18:27:30 +0300841 backend->session_listener.notify = session_notify;
842 wl_signal_add(&compositor->session_signal,
843 &backend->session_listener);
844 compositor->launcher =
845 weston_launcher_connect(compositor, param->tty, "seat0", false);
846 if (!compositor->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
Giulio Camuffo954f1832014-10-11 18:27:30 +0300852 backend->base.destroy = fbdev_backend_destroy;
853 backend->base.restore = fbdev_restore;
Philip Withnall4f499172013-02-02 12:02:32 +0000854
Giulio Camuffo954f1832014-10-11 18:27:30 +0300855 backend->prev_state = WESTON_COMPOSITOR_ACTIVE;
856 backend->use_pixman = !param->use_gl;
Philip Withnall4f499172013-02-02 12:02:32 +0000857
858 for (key = KEY_F1; key < KEY_F9; key++)
Giulio Camuffo954f1832014-10-11 18:27:30 +0300859 weston_compositor_add_key_binding(compositor, key,
Philip Withnall4f499172013-02-02 12:02:32 +0000860 MODIFIER_CTRL | MODIFIER_ALT,
861 switch_vt_binding,
862 compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300863 if (backend->use_pixman) {
864 if (pixman_renderer_init(compositor) < 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
Giulio Camuffo954f1832014-10-11 18:27:30 +0300874 if (gl_renderer->create(compositor, NO_EGL_PLATFORM,
Jonny Lamb74eed312015-03-24 13:12:04 +0100875 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
Giulio Camuffo954f1832014-10-11 18:27:30 +0300883 if (fbdev_output_create(backend, param->device) < 0)
Dawid Gajownik82d49252015-07-31 00:02:28 -0300884 goto out_launcher;
Philip Withnall4f499172013-02-02 12:02:32 +0000885
Giulio Camuffo954f1832014-10-11 18:27:30 +0300886 udev_input_init(&backend->input, compositor, backend->udev, seat_id);
Philip Withnall4f499172013-02-02 12:02:32 +0000887
Giulio Camuffo954f1832014-10-11 18:27:30 +0300888 compositor->backend = &backend->base;
889 return backend;
Philip Withnall4f499172013-02-02 12:02:32 +0000890
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700891out_launcher:
Giulio Camuffo954f1832014-10-11 18:27:30 +0300892 weston_launcher_destroy(compositor->launcher);
Philip Withnall4f499172013-02-02 12:02:32 +0000893
894out_udev:
Giulio Camuffo954f1832014-10-11 18:27:30 +0300895 udev_unref(backend->udev);
Philip Withnall4f499172013-02-02 12:02:32 +0000896
897out_compositor:
Giulio Camuffo954f1832014-10-11 18:27:30 +0300898 weston_compositor_shutdown(compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300899 free(backend);
Philip Withnall4f499172013-02-02 12:02:32 +0000900
901 return NULL;
902}
903
Giulio Camuffo954f1832014-10-11 18:27:30 +0300904WL_EXPORT int
905backend_init(struct weston_compositor *compositor, 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{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300908 struct fbdev_backend *b;
Philip Withnall4f499172013-02-02 12:02:32 +0000909 /* TODO: Ideally, available frame buffers should be enumerated using
910 * udev, rather than passing a device node in as a parameter. */
911 struct fbdev_parameters param = {
912 .tty = 0, /* default to current tty */
913 .device = "/dev/fb0", /* default frame buffer */
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300914 .use_gl = 0,
Philip Withnall4f499172013-02-02 12:02:32 +0000915 };
916
917 const struct weston_option fbdev_options[] = {
918 { WESTON_OPTION_INTEGER, "tty", 0, &param.tty },
919 { WESTON_OPTION_STRING, "device", 0, &param.device },
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300920 { WESTON_OPTION_BOOLEAN, "use-gl", 0, &param.use_gl },
Philip Withnall4f499172013-02-02 12:02:32 +0000921 };
922
923 parse_options(fbdev_options, ARRAY_LENGTH(fbdev_options), argc, argv);
924
Giulio Camuffo954f1832014-10-11 18:27:30 +0300925 b = fbdev_backend_create(compositor, argc, argv, config, &param);
926 if (b == NULL)
927 return -1;
928 return 0;
Philip Withnall4f499172013-02-02 12:02:32 +0000929}