blob: a6fd8231557e59c489a78b8b3974e3f6da2717bb [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
53struct fbdev_compositor {
54 struct weston_compositor base;
55 uint32_t prev_state;
56
57 struct udev *udev;
Rob Bradfordd355b802013-05-31 18:09:55 +010058 struct udev_input input;
Adrian Negreanu4aa756d2013-09-06 15:16:09 +030059 int use_pixman;
Kristian Høgsberg61741a22013-09-17 16:02:57 -070060 struct wl_listener session_listener;
Philip Withnall4f499172013-02-02 12:02:32 +000061};
62
63struct fbdev_screeninfo {
64 unsigned int x_resolution; /* pixels, visible area */
65 unsigned int y_resolution; /* pixels, visible area */
66 unsigned int width_mm; /* visible screen width in mm */
67 unsigned int height_mm; /* visible screen height in mm */
68 unsigned int bits_per_pixel;
69
70 size_t buffer_length; /* length of frame buffer memory in bytes */
71 size_t line_length; /* length of a line in bytes */
72 char id[16]; /* screen identifier */
73
74 pixman_format_code_t pixel_format; /* frame buffer pixel format */
75 unsigned int refresh_rate; /* Hertz */
76};
77
78struct fbdev_output {
79 struct fbdev_compositor *compositor;
80 struct weston_output base;
81
82 struct weston_mode mode;
83 struct wl_event_source *finish_frame_timer;
84
85 /* Frame buffer details. */
86 const char *device; /* ownership shared with fbdev_parameters */
87 struct fbdev_screeninfo fb_info;
88 void *fb; /* length is fb_info.buffer_length */
89
90 /* pixman details. */
91 pixman_image_t *hw_surface;
92 pixman_image_t *shadow_surface;
93 void *shadow_buf;
94 uint8_t depth;
95};
96
Philip Withnall4f499172013-02-02 12:02:32 +000097struct fbdev_parameters {
98 int tty;
99 char *device;
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300100 int use_gl;
Philip Withnall4f499172013-02-02 12:02:32 +0000101};
102
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300103struct gl_renderer_interface *gl_renderer;
104
Kristian Høgsberg7e597f22013-02-18 16:35:26 -0500105static const char default_seat[] = "seat0";
106
Philip Withnall4f499172013-02-02 12:02:32 +0000107static inline struct fbdev_output *
108to_fbdev_output(struct weston_output *base)
109{
110 return container_of(base, struct fbdev_output, base);
111}
112
Philip Withnall4f499172013-02-02 12:02:32 +0000113static inline struct fbdev_compositor *
114to_fbdev_compositor(struct weston_compositor *base)
115{
116 return container_of(base, struct fbdev_compositor, base);
117}
118
119static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200120fbdev_output_start_repaint_loop(struct weston_output *output)
121{
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400122 struct timespec ts;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200123
Pekka Paalanen662f3842015-03-18 12:17:26 +0200124 weston_compositor_read_presentation_clock(output->compositor, &ts);
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200125 weston_output_finish_frame(output, &ts, PRESENTATION_FEEDBACK_INVALID);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200126}
127
128static void
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300129fbdev_output_repaint_pixman(struct weston_output *base, pixman_region32_t *damage)
Philip Withnall4f499172013-02-02 12:02:32 +0000130{
131 struct fbdev_output *output = to_fbdev_output(base);
132 struct weston_compositor *ec = output->base.compositor;
133 pixman_box32_t *rects;
Derek Foreman561662b2015-03-17 13:22:38 -0500134 int nrects, i;
Philip Withnall4f499172013-02-02 12:02:32 +0000135
136 /* Repaint the damaged region onto the back buffer. */
137 pixman_renderer_output_set_buffer(base, output->shadow_surface);
138 ec->renderer->repaint_output(base, damage);
139
140 /* Transform and composite onto the frame buffer. */
Philip Withnall4f499172013-02-02 12:02:32 +0000141 rects = pixman_region32_rectangles(damage, &nrects);
142
143 for (i = 0; i < nrects; i++) {
Derek Foreman561662b2015-03-17 13:22:38 -0500144 pixman_box32_t transformed_rect;
145 int width, height;
Philip Withnall4f499172013-02-02 12:02:32 +0000146
Derek Foreman561662b2015-03-17 13:22:38 -0500147 transformed_rect = weston_transformed_rect(base->width,
148 base->height,
149 base->transform,
150 1, rects[i]);
151 width = transformed_rect.x2 - transformed_rect.x1;
152 height = transformed_rect.y2 - transformed_rect.y1;
Philip Withnall4f499172013-02-02 12:02:32 +0000153 pixman_image_composite32(PIXMAN_OP_SRC,
154 output->shadow_surface, /* src */
155 NULL /* mask */,
156 output->hw_surface, /* dest */
Derek Foreman561662b2015-03-17 13:22:38 -0500157 transformed_rect.x1, /* src_x */
158 transformed_rect.y1, /* src_y */
Philip Withnall4f499172013-02-02 12:02:32 +0000159 0, 0, /* mask_x, mask_y */
Derek Foreman561662b2015-03-17 13:22:38 -0500160 transformed_rect.x1, /* dest_x */
161 transformed_rect.y1, /* dest_y */
162 width, /* width */
163 height /* height */);
Philip Withnall4f499172013-02-02 12:02:32 +0000164 }
165
166 /* Update the damage region. */
167 pixman_region32_subtract(&ec->primary_plane.damage,
168 &ec->primary_plane.damage, damage);
169
170 /* Schedule the end of the frame. We do not sync this to the frame
171 * buffer clock because users who want that should be using the DRM
172 * compositor. FBIO_WAITFORVSYNC blocks and FB_ACTIVATE_VBL requires
173 * panning, which is broken in most kernel drivers.
174 *
175 * Finish the frame synchronised to the specified refresh rate. The
176 * refresh rate is given in mHz and the interval in ms. */
177 wl_event_source_timer_update(output->finish_frame_timer,
178 1000000 / output->mode.refresh);
179}
180
David Herrmann1edf44c2013-10-22 17:11:26 +0200181static int
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300182fbdev_output_repaint(struct weston_output *base, pixman_region32_t *damage)
183{
184 struct fbdev_output *output = to_fbdev_output(base);
185 struct fbdev_compositor *fbc = output->compositor;
186 struct weston_compositor *ec = & fbc->base;
187
188 if (fbc->use_pixman) {
189 fbdev_output_repaint_pixman(base,damage);
190 } else {
191 ec->renderer->repaint_output(base, damage);
192 /* Update the damage region. */
193 pixman_region32_subtract(&ec->primary_plane.damage,
194 &ec->primary_plane.damage, damage);
195
196 wl_event_source_timer_update(output->finish_frame_timer,
197 1000000 / output->mode.refresh);
198 }
David Herrmann1edf44c2013-10-22 17:11:26 +0200199
200 return 0;
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300201}
202
Philip Withnall4f499172013-02-02 12:02:32 +0000203static int
204finish_frame_handler(void *data)
205{
206 struct fbdev_output *output = data;
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200207 struct timespec ts;
Philip Withnall4f499172013-02-02 12:02:32 +0000208
Pekka Paalanen662f3842015-03-18 12:17:26 +0200209 weston_compositor_read_presentation_clock(output->base.compositor, &ts);
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200210 weston_output_finish_frame(&output->base, &ts, 0);
Philip Withnall4f499172013-02-02 12:02:32 +0000211
212 return 1;
213}
214
215static pixman_format_code_t
216calculate_pixman_format(struct fb_var_screeninfo *vinfo,
217 struct fb_fix_screeninfo *finfo)
218{
219 /* Calculate the pixman format supported by the frame buffer from the
220 * buffer's metadata. Return 0 if no known pixman format is supported
221 * (since this has depth 0 it's guaranteed to not conflict with any
222 * actual pixman format).
223 *
224 * Documentation on the vinfo and finfo structures:
225 * http://www.mjmwired.net/kernel/Documentation/fb/api.txt
226 *
227 * TODO: Try a bit harder to support other formats, including setting
228 * the preferred format in the hardware. */
229 int type;
230
231 weston_log("Calculating pixman format from:\n"
232 STAMP_SPACE " - type: %i (aux: %i)\n"
233 STAMP_SPACE " - visual: %i\n"
234 STAMP_SPACE " - bpp: %i (grayscale: %i)\n"
235 STAMP_SPACE " - red: offset: %i, length: %i, MSB: %i\n"
236 STAMP_SPACE " - green: offset: %i, length: %i, MSB: %i\n"
237 STAMP_SPACE " - blue: offset: %i, length: %i, MSB: %i\n"
238 STAMP_SPACE " - transp: offset: %i, length: %i, MSB: %i\n",
239 finfo->type, finfo->type_aux, finfo->visual,
240 vinfo->bits_per_pixel, vinfo->grayscale,
241 vinfo->red.offset, vinfo->red.length, vinfo->red.msb_right,
242 vinfo->green.offset, vinfo->green.length,
243 vinfo->green.msb_right,
244 vinfo->blue.offset, vinfo->blue.length,
245 vinfo->blue.msb_right,
246 vinfo->transp.offset, vinfo->transp.length,
247 vinfo->transp.msb_right);
248
249 /* We only handle packed formats at the moment. */
250 if (finfo->type != FB_TYPE_PACKED_PIXELS)
251 return 0;
252
253 /* We only handle true-colour frame buffers at the moment. */
Marc Chalainffbddff2013-09-03 16:47:43 +0200254 switch(finfo->visual) {
255 case FB_VISUAL_TRUECOLOR:
256 case FB_VISUAL_DIRECTCOLOR:
257 if (vinfo->grayscale != 0)
258 return 0;
259 break;
260 default:
261 return 0;
262 }
Philip Withnall4f499172013-02-02 12:02:32 +0000263
264 /* We only support formats with MSBs on the left. */
265 if (vinfo->red.msb_right != 0 || vinfo->green.msb_right != 0 ||
266 vinfo->blue.msb_right != 0)
267 return 0;
268
269 /* Work out the format type from the offsets. We only support RGBA and
270 * ARGB at the moment. */
271 type = PIXMAN_TYPE_OTHER;
272
273 if ((vinfo->transp.offset >= vinfo->red.offset ||
274 vinfo->transp.length == 0) &&
275 vinfo->red.offset >= vinfo->green.offset &&
276 vinfo->green.offset >= vinfo->blue.offset)
277 type = PIXMAN_TYPE_ARGB;
278 else if (vinfo->red.offset >= vinfo->green.offset &&
279 vinfo->green.offset >= vinfo->blue.offset &&
280 vinfo->blue.offset >= vinfo->transp.offset)
281 type = PIXMAN_TYPE_RGBA;
282
283 if (type == PIXMAN_TYPE_OTHER)
284 return 0;
285
286 /* Build the format. */
287 return PIXMAN_FORMAT(vinfo->bits_per_pixel, type,
288 vinfo->transp.length,
289 vinfo->red.length,
290 vinfo->green.length,
291 vinfo->blue.length);
292}
293
294static int
295calculate_refresh_rate(struct fb_var_screeninfo *vinfo)
296{
297 uint64_t quot;
298
299 /* Calculate monitor refresh rate. Default is 60 Hz. Units are mHz. */
300 quot = (vinfo->upper_margin + vinfo->lower_margin + vinfo->yres);
301 quot *= (vinfo->left_margin + vinfo->right_margin + vinfo->xres);
302 quot *= vinfo->pixclock;
303
304 if (quot > 0) {
305 uint64_t refresh_rate;
306
307 refresh_rate = 1000000000000000LLU / quot;
308 if (refresh_rate > 200000)
309 refresh_rate = 200000; /* cap at 200 Hz */
310
311 return refresh_rate;
312 }
313
314 return 60 * 1000; /* default to 60 Hz */
315}
316
317static int
318fbdev_query_screen_info(struct fbdev_output *output, int fd,
319 struct fbdev_screeninfo *info)
320{
321 struct fb_var_screeninfo varinfo;
322 struct fb_fix_screeninfo fixinfo;
323
324 /* Probe the device for screen information. */
325 if (ioctl(fd, FBIOGET_FSCREENINFO, &fixinfo) < 0 ||
326 ioctl(fd, FBIOGET_VSCREENINFO, &varinfo) < 0) {
327 return -1;
328 }
329
330 /* Store the pertinent data. */
331 info->x_resolution = varinfo.xres;
332 info->y_resolution = varinfo.yres;
333 info->width_mm = varinfo.width;
334 info->height_mm = varinfo.height;
335 info->bits_per_pixel = varinfo.bits_per_pixel;
336
337 info->buffer_length = fixinfo.smem_len;
338 info->line_length = fixinfo.line_length;
339 strncpy(info->id, fixinfo.id, sizeof(info->id) / sizeof(*info->id));
340
341 info->pixel_format = calculate_pixman_format(&varinfo, &fixinfo);
342 info->refresh_rate = calculate_refresh_rate(&varinfo);
343
344 if (info->pixel_format == 0) {
345 weston_log("Frame buffer uses an unsupported format.\n");
346 return -1;
347 }
348
349 return 1;
350}
351
352static int
353fbdev_set_screen_info(struct fbdev_output *output, int fd,
354 struct fbdev_screeninfo *info)
355{
356 struct fb_var_screeninfo varinfo;
357
358 /* Grab the current screen information. */
359 if (ioctl(fd, FBIOGET_VSCREENINFO, &varinfo) < 0) {
360 return -1;
361 }
362
363 /* Update the information. */
364 varinfo.xres = info->x_resolution;
365 varinfo.yres = info->y_resolution;
366 varinfo.width = info->width_mm;
367 varinfo.height = info->height_mm;
368 varinfo.bits_per_pixel = info->bits_per_pixel;
369
370 /* Try to set up an ARGB (x8r8g8b8) pixel format. */
371 varinfo.grayscale = 0;
372 varinfo.transp.offset = 24;
373 varinfo.transp.length = 0;
374 varinfo.transp.msb_right = 0;
375 varinfo.red.offset = 16;
376 varinfo.red.length = 8;
377 varinfo.red.msb_right = 0;
378 varinfo.green.offset = 8;
379 varinfo.green.length = 8;
380 varinfo.green.msb_right = 0;
381 varinfo.blue.offset = 0;
382 varinfo.blue.length = 8;
383 varinfo.blue.msb_right = 0;
384
385 /* Set the device's screen information. */
386 if (ioctl(fd, FBIOPUT_VSCREENINFO, &varinfo) < 0) {
387 return -1;
388 }
389
390 return 1;
391}
392
393static void fbdev_frame_buffer_destroy(struct fbdev_output *output);
394
395/* Returns an FD for the frame buffer device. */
396static int
397fbdev_frame_buffer_open(struct fbdev_output *output, const char *fb_dev,
398 struct fbdev_screeninfo *screen_info)
399{
400 int fd = -1;
401
402 weston_log("Opening fbdev frame buffer.\n");
403
404 /* Open the frame buffer device. */
405 fd = open(fb_dev, O_RDWR | O_CLOEXEC);
406 if (fd < 0) {
407 weston_log("Failed to open frame buffer device ‘%s’: %s\n",
408 fb_dev, strerror(errno));
409 return -1;
410 }
411
412 /* Grab the screen info. */
413 if (fbdev_query_screen_info(output, fd, screen_info) < 0) {
414 weston_log("Failed to get frame buffer info: %s\n",
415 strerror(errno));
416
417 close(fd);
418 return -1;
419 }
420
421 return fd;
422}
423
424/* Closes the FD on success or failure. */
425static int
426fbdev_frame_buffer_map(struct fbdev_output *output, int fd)
427{
428 int retval = -1;
429
430 weston_log("Mapping fbdev frame buffer.\n");
431
432 /* Map the frame buffer. Write-only mode, since we don't want to read
433 * anything back (because it's slow). */
434 output->fb = mmap(NULL, output->fb_info.buffer_length,
435 PROT_WRITE, MAP_SHARED, fd, 0);
436 if (output->fb == MAP_FAILED) {
437 weston_log("Failed to mmap frame buffer: %s\n",
438 strerror(errno));
439 goto out_close;
440 }
441
442 /* Create a pixman image to wrap the memory mapped frame buffer. */
443 output->hw_surface =
444 pixman_image_create_bits(output->fb_info.pixel_format,
445 output->fb_info.x_resolution,
446 output->fb_info.y_resolution,
447 output->fb,
448 output->fb_info.line_length);
449 if (output->hw_surface == NULL) {
450 weston_log("Failed to create surface for frame buffer.\n");
451 goto out_unmap;
452 }
453
454 /* Success! */
455 retval = 0;
456
457out_unmap:
458 if (retval != 0 && output->fb != NULL)
459 fbdev_frame_buffer_destroy(output);
460
461out_close:
462 if (fd >= 0)
463 close(fd);
464
465 return retval;
466}
467
468static void
469fbdev_frame_buffer_destroy(struct fbdev_output *output)
470{
471 weston_log("Destroying fbdev frame buffer.\n");
472
473 if (munmap(output->fb, output->fb_info.buffer_length) < 0)
474 weston_log("Failed to munmap frame buffer: %s\n",
475 strerror(errno));
476
477 output->fb = NULL;
478}
479
480static void fbdev_output_destroy(struct weston_output *base);
481static void fbdev_output_disable(struct weston_output *base);
482
483static int
484fbdev_output_create(struct fbdev_compositor *compositor,
485 const char *device)
486{
487 struct fbdev_output *output;
Derek Foreman44ed70b2015-03-17 13:22:37 -0500488 struct weston_config_section *section;
Philip Withnall4f499172013-02-02 12:02:32 +0000489 int fb_fd;
Philip Withnall4f499172013-02-02 12:02:32 +0000490 int width, height;
491 unsigned int bytes_per_pixel;
492 struct wl_event_loop *loop;
Derek Foreman44ed70b2015-03-17 13:22:37 -0500493 uint32_t config_transform;
494 char *s;
Philip Withnall4f499172013-02-02 12:02:32 +0000495
496 weston_log("Creating fbdev output.\n");
497
Bryce Harringtonde16d892014-11-20 22:21:57 -0800498 output = zalloc(sizeof *output);
499 if (output == NULL)
Philip Withnall4f499172013-02-02 12:02:32 +0000500 return -1;
501
502 output->compositor = compositor;
503 output->device = device;
504
505 /* Create the frame buffer. */
506 fb_fd = fbdev_frame_buffer_open(output, device, &output->fb_info);
507 if (fb_fd < 0) {
508 weston_log("Creating frame buffer failed.\n");
509 goto out_free;
510 }
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300511 if (compositor->use_pixman) {
512 if (fbdev_frame_buffer_map(output, fb_fd) < 0) {
513 weston_log("Mapping frame buffer failed.\n");
514 goto out_free;
515 }
Kristian Høgsberg8ac6a2d2013-10-09 09:59:06 -0700516 } else {
517 close(fb_fd);
Philip Withnall4f499172013-02-02 12:02:32 +0000518 }
519
Jonas Ådahle5a12252013-04-05 23:07:11 +0200520 output->base.start_repaint_loop = fbdev_output_start_repaint_loop;
Philip Withnall4f499172013-02-02 12:02:32 +0000521 output->base.repaint = fbdev_output_repaint;
522 output->base.destroy = fbdev_output_destroy;
Philip Withnall4f499172013-02-02 12:02:32 +0000523
524 /* only one static mode in list */
525 output->mode.flags =
526 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
527 output->mode.width = output->fb_info.x_resolution;
528 output->mode.height = output->fb_info.y_resolution;
529 output->mode.refresh = output->fb_info.refresh_rate;
530 wl_list_init(&output->base.mode_list);
531 wl_list_insert(&output->base.mode_list, &output->mode.link);
532
Hardeningff39efa2013-09-18 23:56:35 +0200533 output->base.current_mode = &output->mode;
Philip Withnall4f499172013-02-02 12:02:32 +0000534 output->base.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
535 output->base.make = "unknown";
536 output->base.model = output->fb_info.id;
Derek Foremane516c3b2015-03-17 13:22:34 -0500537 output->base.name = strdup("fbdev");
Philip Withnall4f499172013-02-02 12:02:32 +0000538
Derek Foreman44ed70b2015-03-17 13:22:37 -0500539 section = weston_config_get_section(compositor->base.config,
540 "output", "name",
541 output->base.name);
542 weston_config_section_get_string(section, "transform", &s, "normal");
543 if (weston_parse_transform(s, &config_transform) < 0)
544 weston_log("Invalid transform \"%s\" for output %s\n",
545 s, output->base.name);
546 free(s);
547
Philip Withnall4f499172013-02-02 12:02:32 +0000548 weston_output_init(&output->base, &compositor->base,
549 0, 0, output->fb_info.width_mm,
550 output->fb_info.height_mm,
Derek Foreman44ed70b2015-03-17 13:22:37 -0500551 config_transform,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200552 1);
Philip Withnall4f499172013-02-02 12:02:32 +0000553
Derek Foreman561662b2015-03-17 13:22:38 -0500554 width = output->mode.width;
555 height = output->mode.height;
Philip Withnall4f499172013-02-02 12:02:32 +0000556 bytes_per_pixel = output->fb_info.bits_per_pixel / 8;
557
558 output->shadow_buf = malloc(width * height * bytes_per_pixel);
559 output->shadow_surface =
560 pixman_image_create_bits(output->fb_info.pixel_format,
Derek Foreman561662b2015-03-17 13:22:38 -0500561 width, height,
Philip Withnall4f499172013-02-02 12:02:32 +0000562 output->shadow_buf,
Derek Foreman561662b2015-03-17 13:22:38 -0500563 width * bytes_per_pixel);
Philip Withnall4f499172013-02-02 12:02:32 +0000564 if (output->shadow_buf == NULL || output->shadow_surface == NULL) {
565 weston_log("Failed to create surface for frame buffer.\n");
566 goto out_hw_surface;
567 }
568
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300569 if (compositor->use_pixman) {
570 if (pixman_renderer_output_create(&output->base) < 0)
571 goto out_shadow_surface;
572 } else {
573 setenv("HYBRIS_EGLPLATFORM", "wayland", 1);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300574 if (gl_renderer->output_create(&output->base,
Jonny Lamb671148f2015-03-20 15:26:52 +0100575 (EGLNativeWindowType)NULL, NULL,
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000576 gl_renderer->opaque_attribs,
Derek Foremane76f1852015-05-15 12:12:39 -0500577 NULL, 0) < 0) {
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300578 weston_log("gl_renderer_output_create failed.\n");
579 goto out_shadow_surface;
580 }
581 }
582
Philip Withnall4f499172013-02-02 12:02:32 +0000583
584 loop = wl_display_get_event_loop(compositor->base.wl_display);
585 output->finish_frame_timer =
586 wl_event_loop_add_timer(loop, finish_frame_handler, output);
587
Giulio Camuffob1147152015-05-06 21:41:57 +0300588 weston_compositor_add_output(&compositor->base, &output->base);
Philip Withnall4f499172013-02-02 12:02:32 +0000589
590 weston_log("fbdev output %d×%d px\n",
591 output->mode.width, output->mode.height);
592 weston_log_continue(STAMP_SPACE "guessing %d Hz and 96 dpi\n",
593 output->mode.refresh / 1000);
594
595 return 0;
596
597out_shadow_surface:
598 pixman_image_unref(output->shadow_surface);
599 output->shadow_surface = NULL;
600out_hw_surface:
601 free(output->shadow_buf);
602 pixman_image_unref(output->hw_surface);
603 output->hw_surface = NULL;
604 weston_output_destroy(&output->base);
605 fbdev_frame_buffer_destroy(output);
606out_free:
607 free(output);
608
609 return -1;
610}
611
612static void
613fbdev_output_destroy(struct weston_output *base)
614{
615 struct fbdev_output *output = to_fbdev_output(base);
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300616 struct fbdev_compositor *compositor = output->compositor;
Philip Withnall4f499172013-02-02 12:02:32 +0000617
618 weston_log("Destroying fbdev output.\n");
619
620 /* Close the frame buffer. */
621 fbdev_output_disable(base);
622
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300623 if (compositor->use_pixman) {
624 if (base->renderer_state != NULL)
625 pixman_renderer_output_destroy(base);
Philip Withnall4f499172013-02-02 12:02:32 +0000626
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300627 if (output->shadow_surface != NULL) {
628 pixman_image_unref(output->shadow_surface);
629 output->shadow_surface = NULL;
630 }
Philip Withnall4f499172013-02-02 12:02:32 +0000631
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300632 if (output->shadow_buf != NULL) {
633 free(output->shadow_buf);
634 output->shadow_buf = NULL;
635 }
636 } else {
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300637 gl_renderer->output_destroy(base);
Philip Withnall4f499172013-02-02 12:02:32 +0000638 }
639
640 /* Remove the output. */
Philip Withnall4f499172013-02-02 12:02:32 +0000641 weston_output_destroy(&output->base);
642
643 free(output);
644}
645
646/* strcmp()-style return values. */
647static int
648compare_screen_info (const struct fbdev_screeninfo *a,
649 const struct fbdev_screeninfo *b)
650{
651 if (a->x_resolution == b->x_resolution &&
652 a->y_resolution == b->y_resolution &&
653 a->width_mm == b->width_mm &&
654 a->height_mm == b->height_mm &&
655 a->bits_per_pixel == b->bits_per_pixel &&
656 a->pixel_format == b->pixel_format &&
657 a->refresh_rate == b->refresh_rate)
658 return 0;
659
660 return 1;
661}
662
663static int
664fbdev_output_reenable(struct fbdev_compositor *compositor,
665 struct weston_output *base)
666{
667 struct fbdev_output *output = to_fbdev_output(base);
668 struct fbdev_screeninfo new_screen_info;
669 int fb_fd;
Rob Bradfordf8ef42f2013-07-26 16:29:38 +0100670 const char *device;
Philip Withnall4f499172013-02-02 12:02:32 +0000671
672 weston_log("Re-enabling fbdev output.\n");
673
674 /* Create the frame buffer. */
675 fb_fd = fbdev_frame_buffer_open(output, output->device,
676 &new_screen_info);
677 if (fb_fd < 0) {
678 weston_log("Creating frame buffer failed.\n");
679 goto err;
680 }
681
682 /* Check whether the frame buffer details have changed since we were
683 * disabled. */
684 if (compare_screen_info (&output->fb_info, &new_screen_info) != 0) {
685 /* Perform a mode-set to restore the old mode. */
686 if (fbdev_set_screen_info(output, fb_fd,
687 &output->fb_info) < 0) {
688 weston_log("Failed to restore mode settings. "
689 "Attempting to re-open output anyway.\n");
690 }
691
Rob Bradford581b3fd2013-07-26 16:29:39 +0100692 close(fb_fd);
693
Philip Withnall4f499172013-02-02 12:02:32 +0000694 /* Remove and re-add the output so that resources depending on
695 * the frame buffer X/Y resolution (such as the shadow buffer)
696 * are re-initialised. */
Rob Bradfordf8ef42f2013-07-26 16:29:38 +0100697 device = output->device;
Philip Withnall4f499172013-02-02 12:02:32 +0000698 fbdev_output_destroy(base);
Rob Bradfordf8ef42f2013-07-26 16:29:38 +0100699 fbdev_output_create(compositor, device);
Philip Withnall4f499172013-02-02 12:02:32 +0000700
701 return 0;
702 }
703
704 /* Map the device if it has the same details as before. */
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300705 if (compositor->use_pixman) {
706 if (fbdev_frame_buffer_map(output, fb_fd) < 0) {
707 weston_log("Mapping frame buffer failed.\n");
708 goto err;
709 }
Philip Withnall4f499172013-02-02 12:02:32 +0000710 }
711
712 return 0;
713
714err:
715 return -1;
716}
717
718/* NOTE: This leaves output->fb_info populated, caching data so that if
719 * fbdev_output_reenable() is called again, it can determine whether a mode-set
720 * is needed. */
721static void
722fbdev_output_disable(struct weston_output *base)
723{
724 struct fbdev_output *output = to_fbdev_output(base);
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300725 struct fbdev_compositor *compositor = output->compositor;
Philip Withnall4f499172013-02-02 12:02:32 +0000726
727 weston_log("Disabling fbdev output.\n");
728
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300729 if ( ! compositor->use_pixman) return;
730
Philip Withnall4f499172013-02-02 12:02:32 +0000731 if (output->hw_surface != NULL) {
732 pixman_image_unref(output->hw_surface);
733 output->hw_surface = NULL;
734 }
735
736 fbdev_frame_buffer_destroy(output);
737}
738
739static void
Philip Withnall4f499172013-02-02 12:02:32 +0000740fbdev_compositor_destroy(struct weston_compositor *base)
741{
742 struct fbdev_compositor *compositor = to_fbdev_compositor(base);
Philip Withnall4f499172013-02-02 12:02:32 +0000743
Rob Bradfordd355b802013-05-31 18:09:55 +0100744 udev_input_destroy(&compositor->input);
Philip Withnall4f499172013-02-02 12:02:32 +0000745
746 /* Destroy the output. */
747 weston_compositor_shutdown(&compositor->base);
748
749 /* Chain up. */
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700750 weston_launcher_destroy(compositor->base.launcher);
Philip Withnall4f499172013-02-02 12:02:32 +0000751
752 free(compositor);
753}
754
755static void
Kristian Høgsberg61741a22013-09-17 16:02:57 -0700756session_notify(struct wl_listener *listener, void *data)
Philip Withnall4f499172013-02-02 12:02:32 +0000757{
Kristian Høgsberg61741a22013-09-17 16:02:57 -0700758 struct fbdev_compositor *compositor = data;
Philip Withnall4f499172013-02-02 12:02:32 +0000759 struct weston_output *output;
760
Kristian Høgsberg61741a22013-09-17 16:02:57 -0700761 if (compositor->base.session_active) {
Philip Withnall4f499172013-02-02 12:02:32 +0000762 weston_log("entering VT\n");
Philip Withnall4f499172013-02-02 12:02:32 +0000763 compositor->base.state = compositor->prev_state;
764
765 wl_list_for_each(output, &compositor->base.output_list, link) {
766 fbdev_output_reenable(compositor, output);
767 }
768
769 weston_compositor_damage_all(&compositor->base);
770
Jonas Ådahl0feb32e2014-03-12 22:08:41 +0100771 udev_input_enable(&compositor->input);
Kristian Høgsberg61741a22013-09-17 16:02:57 -0700772 } else {
Philip Withnall4f499172013-02-02 12:02:32 +0000773 weston_log("leaving VT\n");
Rob Bradfordd355b802013-05-31 18:09:55 +0100774 udev_input_disable(&compositor->input);
Philip Withnall4f499172013-02-02 12:02:32 +0000775
776 wl_list_for_each(output, &compositor->base.output_list, link) {
777 fbdev_output_disable(output);
778 }
779
Philip Withnall4f499172013-02-02 12:02:32 +0000780 compositor->prev_state = compositor->base.state;
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +0100781 weston_compositor_offscreen(&compositor->base);
Philip Withnall4f499172013-02-02 12:02:32 +0000782
783 /* If we have a repaint scheduled (from the idle handler), make
784 * sure we cancel that so we don't try to pageflip when we're
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +0100785 * vt switched away. The OFFSCREEN state will prevent
Philip Withnall4f499172013-02-02 12:02:32 +0000786 * further attemps at repainting. When we switch
787 * back, we schedule a repaint, which will process
788 * pending frame callbacks. */
789
790 wl_list_for_each(output,
791 &compositor->base.output_list, link) {
792 output->repaint_needed = 0;
793 }
nerdopolis38946702014-12-03 15:53:03 +0000794 }
Philip Withnall4f499172013-02-02 12:02:32 +0000795}
796
797static void
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700798fbdev_restore(struct weston_compositor *compositor)
Philip Withnall4f499172013-02-02 12:02:32 +0000799{
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700800 weston_launcher_restore(compositor->launcher);
Philip Withnall4f499172013-02-02 12:02:32 +0000801}
802
803static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400804switch_vt_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Philip Withnall4f499172013-02-02 12:02:32 +0000805{
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700806 struct weston_compositor *compositor = data;
Philip Withnall4f499172013-02-02 12:02:32 +0000807
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700808 weston_launcher_activate_vt(compositor->launcher, key - KEY_F1 + 1);
Philip Withnall4f499172013-02-02 12:02:32 +0000809}
810
811static struct weston_compositor *
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500812fbdev_compositor_create(struct wl_display *display, int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400813 struct weston_config *config,
814 struct fbdev_parameters *param)
Philip Withnall4f499172013-02-02 12:02:32 +0000815{
816 struct fbdev_compositor *compositor;
Rob Bradford2387fde2013-05-31 18:09:57 +0100817 const char *seat_id = default_seat;
Philip Withnall4f499172013-02-02 12:02:32 +0000818 uint32_t key;
819
820 weston_log("initializing fbdev backend\n");
821
Bryce Harringtonde16d892014-11-20 22:21:57 -0800822 compositor = zalloc(sizeof *compositor);
Philip Withnall4f499172013-02-02 12:02:32 +0000823 if (compositor == NULL)
824 return NULL;
825
826 if (weston_compositor_init(&compositor->base, display, argc, argv,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400827 config) < 0)
Philip Withnall4f499172013-02-02 12:02:32 +0000828 goto out_free;
829
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400830 if (weston_compositor_set_presentation_clock_software(
831 &compositor->base) < 0)
832 goto out_compositor;
833
Philip Withnall4f499172013-02-02 12:02:32 +0000834 compositor->udev = udev_new();
835 if (compositor->udev == NULL) {
836 weston_log("Failed to initialize udev context.\n");
837 goto out_compositor;
838 }
839
840 /* Set up the TTY. */
Kristian Høgsberg61741a22013-09-17 16:02:57 -0700841 compositor->session_listener.notify = session_notify;
842 wl_signal_add(&compositor->base.session_signal,
843 &compositor->session_listener);
David Herrmann2ecb84a2014-12-30 14:33:22 +0100844 compositor->base.launcher = weston_launcher_connect(&compositor->base,
845 param->tty, "seat0",
846 false);
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700847 if (!compositor->base.launcher) {
David Herrmann1641d142013-10-15 14:29:57 +0200848 weston_log("fatal: fbdev backend should be run "
849 "using weston-launch binary or as root\n");
Philip Withnall4f499172013-02-02 12:02:32 +0000850 goto out_udev;
851 }
852
853 compositor->base.destroy = fbdev_compositor_destroy;
854 compositor->base.restore = fbdev_restore;
855
Philip Withnall4f499172013-02-02 12:02:32 +0000856 compositor->prev_state = WESTON_COMPOSITOR_ACTIVE;
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300857 compositor->use_pixman = !param->use_gl;
Philip Withnall4f499172013-02-02 12:02:32 +0000858
859 for (key = KEY_F1; key < KEY_F9; key++)
860 weston_compositor_add_key_binding(&compositor->base, key,
861 MODIFIER_CTRL | MODIFIER_ALT,
862 switch_vt_binding,
863 compositor);
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300864 if (compositor->use_pixman) {
865 if (pixman_renderer_init(&compositor->base) < 0)
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700866 goto out_launcher;
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300867 } else {
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300868 gl_renderer = weston_load_module("gl-renderer.so",
869 "gl_renderer_interface");
870 if (!gl_renderer) {
871 weston_log("could not load gl renderer\n");
872 goto out_launcher;
873 }
874
Jonny Lamb74eed312015-03-24 13:12:04 +0100875 if (gl_renderer->create(&compositor->base, NO_EGL_PLATFORM,
876 EGL_DEFAULT_DISPLAY,
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300877 gl_renderer->opaque_attribs,
Derek Foremane76f1852015-05-15 12:12:39 -0500878 NULL, 0) < 0) {
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300879 weston_log("gl_renderer_create failed.\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700880 goto out_launcher;
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300881 }
882 }
Philip Withnall4f499172013-02-02 12:02:32 +0000883
884 if (fbdev_output_create(compositor, param->device) < 0)
885 goto out_pixman;
886
Rob Bradford2387fde2013-05-31 18:09:57 +0100887 udev_input_init(&compositor->input, &compositor->base, compositor->udev, seat_id);
Philip Withnall4f499172013-02-02 12:02:32 +0000888
889 return &compositor->base;
890
891out_pixman:
892 compositor->base.renderer->destroy(&compositor->base);
893
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700894out_launcher:
895 weston_launcher_destroy(compositor->base.launcher);
Philip Withnall4f499172013-02-02 12:02:32 +0000896
897out_udev:
898 udev_unref(compositor->udev);
899
900out_compositor:
901 weston_compositor_shutdown(&compositor->base);
902
903out_free:
904 free(compositor);
905
906 return NULL;
907}
908
909WL_EXPORT struct weston_compositor *
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500910backend_init(struct wl_display *display, int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400911 struct weston_config *config)
Philip Withnall4f499172013-02-02 12:02:32 +0000912{
913 /* TODO: Ideally, available frame buffers should be enumerated using
914 * udev, rather than passing a device node in as a parameter. */
915 struct fbdev_parameters param = {
916 .tty = 0, /* default to current tty */
917 .device = "/dev/fb0", /* default frame buffer */
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300918 .use_gl = 0,
Philip Withnall4f499172013-02-02 12:02:32 +0000919 };
920
921 const struct weston_option fbdev_options[] = {
922 { WESTON_OPTION_INTEGER, "tty", 0, &param.tty },
923 { WESTON_OPTION_STRING, "device", 0, &param.device },
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300924 { WESTON_OPTION_BOOLEAN, "use-gl", 0, &param.use_gl },
Philip Withnall4f499172013-02-02 12:02:32 +0000925 };
926
927 parse_options(fbdev_options, ARRAY_LENGTH(fbdev_options), argc, argv);
928
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400929 return fbdev_compositor_create(display, argc, argv, config, &param);
Philip Withnall4f499172013-02-02 12:02:32 +0000930}