blob: eedf252bf99e8b4fc34b5ce1feb536919fa30989 [file] [log] [blame]
Philip Withnall4f499172013-02-02 12:02:32 +00001/*
2 * Copyright © 2008-2011 Kristian Høgsberg
3 * Copyright © 2011 Intel Corporation
4 * Copyright © 2012 Raspberry Pi Foundation
5 * Copyright © 2013 Philip Withnall
6 *
7 * Permission to use, copy, modify, distribute, and sell this software and
8 * its documentation for any purpose is hereby granted without fee, provided
9 * that the above copyright notice appear in all copies and that both that
10 * copyright notice and this permission notice appear in supporting
11 * documentation, and that the name of the copyright holders not be used in
12 * advertising or publicity pertaining to distribution of the software
13 * without specific, written prior permission. The copyright holders make
14 * no representations about the suitability of this software for any
15 * purpose. It is provided "as is" without express or implied warranty.
16 *
17 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
18 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
19 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
21 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
22 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
23 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 */
25
Daniel Stonec228e232013-05-22 18:03:19 +030026#include "config.h"
Philip Withnall4f499172013-02-02 12:02:32 +000027
28#include <errno.h>
29#include <stdlib.h>
30#include <stdio.h>
31#include <string.h>
32#include <math.h>
33#include <sys/mman.h>
34#include <sys/types.h>
35#include <fcntl.h>
36#include <unistd.h>
37#include <linux/fb.h>
Kristian Høgsberg7e597f22013-02-18 16:35:26 -050038#include <linux/input.h>
Philip Withnall4f499172013-02-02 12:02:32 +000039
40#include <libudev.h>
41
Philip Withnall4f499172013-02-02 12:02:32 +000042#include "compositor.h"
43#include "launcher-util.h"
44#include "pixman-renderer.h"
Peter Hutterer823ad332014-11-26 07:06:31 +100045#include "libinput-seat.h"
Adrian Negreanu4aa756d2013-09-06 15:16:09 +030046#include "gl-renderer.h"
Pekka Paalanen363aa7b2014-12-17 16:20:40 +020047#include "presentation_timing-server-protocol.h"
Philip Withnall4f499172013-02-02 12:02:32 +000048
49struct fbdev_compositor {
50 struct weston_compositor base;
51 uint32_t prev_state;
52
53 struct udev *udev;
Rob Bradfordd355b802013-05-31 18:09:55 +010054 struct udev_input input;
Adrian Negreanu4aa756d2013-09-06 15:16:09 +030055 int use_pixman;
Kristian Høgsberg61741a22013-09-17 16:02:57 -070056 struct wl_listener session_listener;
Philip Withnall4f499172013-02-02 12:02:32 +000057};
58
59struct fbdev_screeninfo {
60 unsigned int x_resolution; /* pixels, visible area */
61 unsigned int y_resolution; /* pixels, visible area */
62 unsigned int width_mm; /* visible screen width in mm */
63 unsigned int height_mm; /* visible screen height in mm */
64 unsigned int bits_per_pixel;
65
66 size_t buffer_length; /* length of frame buffer memory in bytes */
67 size_t line_length; /* length of a line in bytes */
68 char id[16]; /* screen identifier */
69
70 pixman_format_code_t pixel_format; /* frame buffer pixel format */
71 unsigned int refresh_rate; /* Hertz */
72};
73
74struct fbdev_output {
75 struct fbdev_compositor *compositor;
76 struct weston_output base;
77
78 struct weston_mode mode;
79 struct wl_event_source *finish_frame_timer;
80
81 /* Frame buffer details. */
82 const char *device; /* ownership shared with fbdev_parameters */
83 struct fbdev_screeninfo fb_info;
84 void *fb; /* length is fb_info.buffer_length */
85
86 /* pixman details. */
87 pixman_image_t *hw_surface;
88 pixman_image_t *shadow_surface;
89 void *shadow_buf;
90 uint8_t depth;
91};
92
Philip Withnall4f499172013-02-02 12:02:32 +000093struct fbdev_parameters {
94 int tty;
95 char *device;
Adrian Negreanu4aa756d2013-09-06 15:16:09 +030096 int use_gl;
Philip Withnall4f499172013-02-02 12:02:32 +000097};
98
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +030099struct gl_renderer_interface *gl_renderer;
100
Kristian Høgsberg7e597f22013-02-18 16:35:26 -0500101static const char default_seat[] = "seat0";
102
Philip Withnall4f499172013-02-02 12:02:32 +0000103static inline struct fbdev_output *
104to_fbdev_output(struct weston_output *base)
105{
106 return container_of(base, struct fbdev_output, base);
107}
108
Philip Withnall4f499172013-02-02 12:02:32 +0000109static inline struct fbdev_compositor *
110to_fbdev_compositor(struct weston_compositor *base)
111{
112 return container_of(base, struct fbdev_compositor, base);
113}
114
115static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200116fbdev_output_start_repaint_loop(struct weston_output *output)
117{
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400118 struct timespec ts;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200119
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400120 clock_gettime(output->compositor->presentation_clock, &ts);
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200121 weston_output_finish_frame(output, &ts, PRESENTATION_FEEDBACK_INVALID);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200122}
123
124static void
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300125fbdev_output_repaint_pixman(struct weston_output *base, pixman_region32_t *damage)
Philip Withnall4f499172013-02-02 12:02:32 +0000126{
127 struct fbdev_output *output = to_fbdev_output(base);
128 struct weston_compositor *ec = output->base.compositor;
129 pixman_box32_t *rects;
130 int nrects, i, src_x, src_y, x1, y1, x2, y2, width, height;
131
132 /* Repaint the damaged region onto the back buffer. */
133 pixman_renderer_output_set_buffer(base, output->shadow_surface);
134 ec->renderer->repaint_output(base, damage);
135
136 /* Transform and composite onto the frame buffer. */
137 width = pixman_image_get_width(output->shadow_surface);
138 height = pixman_image_get_height(output->shadow_surface);
139 rects = pixman_region32_rectangles(damage, &nrects);
140
141 for (i = 0; i < nrects; i++) {
142 switch (base->transform) {
143 default:
144 case WL_OUTPUT_TRANSFORM_NORMAL:
145 x1 = rects[i].x1;
146 x2 = rects[i].x2;
147 y1 = rects[i].y1;
148 y2 = rects[i].y2;
149 break;
150 case WL_OUTPUT_TRANSFORM_180:
151 x1 = width - rects[i].x2;
152 x2 = width - rects[i].x1;
153 y1 = height - rects[i].y2;
154 y2 = height - rects[i].y1;
155 break;
156 case WL_OUTPUT_TRANSFORM_90:
157 x1 = height - rects[i].y2;
158 x2 = height - rects[i].y1;
159 y1 = rects[i].x1;
160 y2 = rects[i].x2;
161 break;
162 case WL_OUTPUT_TRANSFORM_270:
163 x1 = rects[i].y1;
164 x2 = rects[i].y2;
165 y1 = width - rects[i].x2;
166 y2 = width - rects[i].x1;
167 break;
168 }
169 src_x = x1;
170 src_y = y1;
171
172 pixman_image_composite32(PIXMAN_OP_SRC,
173 output->shadow_surface, /* src */
174 NULL /* mask */,
175 output->hw_surface, /* dest */
176 src_x, src_y, /* src_x, src_y */
177 0, 0, /* mask_x, mask_y */
178 x1, y1, /* dest_x, dest_y */
179 x2 - x1, /* width */
180 y2 - y1 /* height */);
181 }
182
183 /* Update the damage region. */
184 pixman_region32_subtract(&ec->primary_plane.damage,
185 &ec->primary_plane.damage, damage);
186
187 /* Schedule the end of the frame. We do not sync this to the frame
188 * buffer clock because users who want that should be using the DRM
189 * compositor. FBIO_WAITFORVSYNC blocks and FB_ACTIVATE_VBL requires
190 * panning, which is broken in most kernel drivers.
191 *
192 * Finish the frame synchronised to the specified refresh rate. The
193 * refresh rate is given in mHz and the interval in ms. */
194 wl_event_source_timer_update(output->finish_frame_timer,
195 1000000 / output->mode.refresh);
196}
197
David Herrmann1edf44c2013-10-22 17:11:26 +0200198static int
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300199fbdev_output_repaint(struct weston_output *base, pixman_region32_t *damage)
200{
201 struct fbdev_output *output = to_fbdev_output(base);
202 struct fbdev_compositor *fbc = output->compositor;
203 struct weston_compositor *ec = & fbc->base;
204
205 if (fbc->use_pixman) {
206 fbdev_output_repaint_pixman(base,damage);
207 } else {
208 ec->renderer->repaint_output(base, damage);
209 /* Update the damage region. */
210 pixman_region32_subtract(&ec->primary_plane.damage,
211 &ec->primary_plane.damage, damage);
212
213 wl_event_source_timer_update(output->finish_frame_timer,
214 1000000 / output->mode.refresh);
215 }
David Herrmann1edf44c2013-10-22 17:11:26 +0200216
217 return 0;
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300218}
219
Philip Withnall4f499172013-02-02 12:02:32 +0000220static int
221finish_frame_handler(void *data)
222{
223 struct fbdev_output *output = data;
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200224 struct timespec ts;
Philip Withnall4f499172013-02-02 12:02:32 +0000225
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200226 clock_gettime(output->base.compositor->presentation_clock, &ts);
227 weston_output_finish_frame(&output->base, &ts, 0);
Philip Withnall4f499172013-02-02 12:02:32 +0000228
229 return 1;
230}
231
232static pixman_format_code_t
233calculate_pixman_format(struct fb_var_screeninfo *vinfo,
234 struct fb_fix_screeninfo *finfo)
235{
236 /* Calculate the pixman format supported by the frame buffer from the
237 * buffer's metadata. Return 0 if no known pixman format is supported
238 * (since this has depth 0 it's guaranteed to not conflict with any
239 * actual pixman format).
240 *
241 * Documentation on the vinfo and finfo structures:
242 * http://www.mjmwired.net/kernel/Documentation/fb/api.txt
243 *
244 * TODO: Try a bit harder to support other formats, including setting
245 * the preferred format in the hardware. */
246 int type;
247
248 weston_log("Calculating pixman format from:\n"
249 STAMP_SPACE " - type: %i (aux: %i)\n"
250 STAMP_SPACE " - visual: %i\n"
251 STAMP_SPACE " - bpp: %i (grayscale: %i)\n"
252 STAMP_SPACE " - red: offset: %i, length: %i, MSB: %i\n"
253 STAMP_SPACE " - green: offset: %i, length: %i, MSB: %i\n"
254 STAMP_SPACE " - blue: offset: %i, length: %i, MSB: %i\n"
255 STAMP_SPACE " - transp: offset: %i, length: %i, MSB: %i\n",
256 finfo->type, finfo->type_aux, finfo->visual,
257 vinfo->bits_per_pixel, vinfo->grayscale,
258 vinfo->red.offset, vinfo->red.length, vinfo->red.msb_right,
259 vinfo->green.offset, vinfo->green.length,
260 vinfo->green.msb_right,
261 vinfo->blue.offset, vinfo->blue.length,
262 vinfo->blue.msb_right,
263 vinfo->transp.offset, vinfo->transp.length,
264 vinfo->transp.msb_right);
265
266 /* We only handle packed formats at the moment. */
267 if (finfo->type != FB_TYPE_PACKED_PIXELS)
268 return 0;
269
270 /* We only handle true-colour frame buffers at the moment. */
Marc Chalainffbddff2013-09-03 16:47:43 +0200271 switch(finfo->visual) {
272 case FB_VISUAL_TRUECOLOR:
273 case FB_VISUAL_DIRECTCOLOR:
274 if (vinfo->grayscale != 0)
275 return 0;
276 break;
277 default:
278 return 0;
279 }
Philip Withnall4f499172013-02-02 12:02:32 +0000280
281 /* We only support formats with MSBs on the left. */
282 if (vinfo->red.msb_right != 0 || vinfo->green.msb_right != 0 ||
283 vinfo->blue.msb_right != 0)
284 return 0;
285
286 /* Work out the format type from the offsets. We only support RGBA and
287 * ARGB at the moment. */
288 type = PIXMAN_TYPE_OTHER;
289
290 if ((vinfo->transp.offset >= vinfo->red.offset ||
291 vinfo->transp.length == 0) &&
292 vinfo->red.offset >= vinfo->green.offset &&
293 vinfo->green.offset >= vinfo->blue.offset)
294 type = PIXMAN_TYPE_ARGB;
295 else if (vinfo->red.offset >= vinfo->green.offset &&
296 vinfo->green.offset >= vinfo->blue.offset &&
297 vinfo->blue.offset >= vinfo->transp.offset)
298 type = PIXMAN_TYPE_RGBA;
299
300 if (type == PIXMAN_TYPE_OTHER)
301 return 0;
302
303 /* Build the format. */
304 return PIXMAN_FORMAT(vinfo->bits_per_pixel, type,
305 vinfo->transp.length,
306 vinfo->red.length,
307 vinfo->green.length,
308 vinfo->blue.length);
309}
310
311static int
312calculate_refresh_rate(struct fb_var_screeninfo *vinfo)
313{
314 uint64_t quot;
315
316 /* Calculate monitor refresh rate. Default is 60 Hz. Units are mHz. */
317 quot = (vinfo->upper_margin + vinfo->lower_margin + vinfo->yres);
318 quot *= (vinfo->left_margin + vinfo->right_margin + vinfo->xres);
319 quot *= vinfo->pixclock;
320
321 if (quot > 0) {
322 uint64_t refresh_rate;
323
324 refresh_rate = 1000000000000000LLU / quot;
325 if (refresh_rate > 200000)
326 refresh_rate = 200000; /* cap at 200 Hz */
327
328 return refresh_rate;
329 }
330
331 return 60 * 1000; /* default to 60 Hz */
332}
333
334static int
335fbdev_query_screen_info(struct fbdev_output *output, int fd,
336 struct fbdev_screeninfo *info)
337{
338 struct fb_var_screeninfo varinfo;
339 struct fb_fix_screeninfo fixinfo;
340
341 /* Probe the device for screen information. */
342 if (ioctl(fd, FBIOGET_FSCREENINFO, &fixinfo) < 0 ||
343 ioctl(fd, FBIOGET_VSCREENINFO, &varinfo) < 0) {
344 return -1;
345 }
346
347 /* Store the pertinent data. */
348 info->x_resolution = varinfo.xres;
349 info->y_resolution = varinfo.yres;
350 info->width_mm = varinfo.width;
351 info->height_mm = varinfo.height;
352 info->bits_per_pixel = varinfo.bits_per_pixel;
353
354 info->buffer_length = fixinfo.smem_len;
355 info->line_length = fixinfo.line_length;
356 strncpy(info->id, fixinfo.id, sizeof(info->id) / sizeof(*info->id));
357
358 info->pixel_format = calculate_pixman_format(&varinfo, &fixinfo);
359 info->refresh_rate = calculate_refresh_rate(&varinfo);
360
361 if (info->pixel_format == 0) {
362 weston_log("Frame buffer uses an unsupported format.\n");
363 return -1;
364 }
365
366 return 1;
367}
368
369static int
370fbdev_set_screen_info(struct fbdev_output *output, int fd,
371 struct fbdev_screeninfo *info)
372{
373 struct fb_var_screeninfo varinfo;
374
375 /* Grab the current screen information. */
376 if (ioctl(fd, FBIOGET_VSCREENINFO, &varinfo) < 0) {
377 return -1;
378 }
379
380 /* Update the information. */
381 varinfo.xres = info->x_resolution;
382 varinfo.yres = info->y_resolution;
383 varinfo.width = info->width_mm;
384 varinfo.height = info->height_mm;
385 varinfo.bits_per_pixel = info->bits_per_pixel;
386
387 /* Try to set up an ARGB (x8r8g8b8) pixel format. */
388 varinfo.grayscale = 0;
389 varinfo.transp.offset = 24;
390 varinfo.transp.length = 0;
391 varinfo.transp.msb_right = 0;
392 varinfo.red.offset = 16;
393 varinfo.red.length = 8;
394 varinfo.red.msb_right = 0;
395 varinfo.green.offset = 8;
396 varinfo.green.length = 8;
397 varinfo.green.msb_right = 0;
398 varinfo.blue.offset = 0;
399 varinfo.blue.length = 8;
400 varinfo.blue.msb_right = 0;
401
402 /* Set the device's screen information. */
403 if (ioctl(fd, FBIOPUT_VSCREENINFO, &varinfo) < 0) {
404 return -1;
405 }
406
407 return 1;
408}
409
410static void fbdev_frame_buffer_destroy(struct fbdev_output *output);
411
412/* Returns an FD for the frame buffer device. */
413static int
414fbdev_frame_buffer_open(struct fbdev_output *output, const char *fb_dev,
415 struct fbdev_screeninfo *screen_info)
416{
417 int fd = -1;
418
419 weston_log("Opening fbdev frame buffer.\n");
420
421 /* Open the frame buffer device. */
422 fd = open(fb_dev, O_RDWR | O_CLOEXEC);
423 if (fd < 0) {
424 weston_log("Failed to open frame buffer device ‘%s’: %s\n",
425 fb_dev, strerror(errno));
426 return -1;
427 }
428
429 /* Grab the screen info. */
430 if (fbdev_query_screen_info(output, fd, screen_info) < 0) {
431 weston_log("Failed to get frame buffer info: %s\n",
432 strerror(errno));
433
434 close(fd);
435 return -1;
436 }
437
438 return fd;
439}
440
441/* Closes the FD on success or failure. */
442static int
443fbdev_frame_buffer_map(struct fbdev_output *output, int fd)
444{
445 int retval = -1;
446
447 weston_log("Mapping fbdev frame buffer.\n");
448
449 /* Map the frame buffer. Write-only mode, since we don't want to read
450 * anything back (because it's slow). */
451 output->fb = mmap(NULL, output->fb_info.buffer_length,
452 PROT_WRITE, MAP_SHARED, fd, 0);
453 if (output->fb == MAP_FAILED) {
454 weston_log("Failed to mmap frame buffer: %s\n",
455 strerror(errno));
456 goto out_close;
457 }
458
459 /* Create a pixman image to wrap the memory mapped frame buffer. */
460 output->hw_surface =
461 pixman_image_create_bits(output->fb_info.pixel_format,
462 output->fb_info.x_resolution,
463 output->fb_info.y_resolution,
464 output->fb,
465 output->fb_info.line_length);
466 if (output->hw_surface == NULL) {
467 weston_log("Failed to create surface for frame buffer.\n");
468 goto out_unmap;
469 }
470
471 /* Success! */
472 retval = 0;
473
474out_unmap:
475 if (retval != 0 && output->fb != NULL)
476 fbdev_frame_buffer_destroy(output);
477
478out_close:
479 if (fd >= 0)
480 close(fd);
481
482 return retval;
483}
484
485static void
486fbdev_frame_buffer_destroy(struct fbdev_output *output)
487{
488 weston_log("Destroying fbdev frame buffer.\n");
489
490 if (munmap(output->fb, output->fb_info.buffer_length) < 0)
491 weston_log("Failed to munmap frame buffer: %s\n",
492 strerror(errno));
493
494 output->fb = NULL;
495}
496
497static void fbdev_output_destroy(struct weston_output *base);
498static void fbdev_output_disable(struct weston_output *base);
499
500static int
501fbdev_output_create(struct fbdev_compositor *compositor,
502 const char *device)
503{
504 struct fbdev_output *output;
505 pixman_transform_t transform;
506 int fb_fd;
507 int shadow_width, shadow_height;
508 int width, height;
509 unsigned int bytes_per_pixel;
510 struct wl_event_loop *loop;
511
512 weston_log("Creating fbdev output.\n");
513
Bryce Harringtonde16d892014-11-20 22:21:57 -0800514 output = zalloc(sizeof *output);
515 if (output == NULL)
Philip Withnall4f499172013-02-02 12:02:32 +0000516 return -1;
517
518 output->compositor = compositor;
519 output->device = device;
520
521 /* Create the frame buffer. */
522 fb_fd = fbdev_frame_buffer_open(output, device, &output->fb_info);
523 if (fb_fd < 0) {
524 weston_log("Creating frame buffer failed.\n");
525 goto out_free;
526 }
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300527 if (compositor->use_pixman) {
528 if (fbdev_frame_buffer_map(output, fb_fd) < 0) {
529 weston_log("Mapping frame buffer failed.\n");
530 goto out_free;
531 }
Kristian Høgsberg8ac6a2d2013-10-09 09:59:06 -0700532 } else {
533 close(fb_fd);
Philip Withnall4f499172013-02-02 12:02:32 +0000534 }
535
Jonas Ådahle5a12252013-04-05 23:07:11 +0200536 output->base.start_repaint_loop = fbdev_output_start_repaint_loop;
Philip Withnall4f499172013-02-02 12:02:32 +0000537 output->base.repaint = fbdev_output_repaint;
538 output->base.destroy = fbdev_output_destroy;
Philip Withnall4f499172013-02-02 12:02:32 +0000539
540 /* only one static mode in list */
541 output->mode.flags =
542 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
543 output->mode.width = output->fb_info.x_resolution;
544 output->mode.height = output->fb_info.y_resolution;
545 output->mode.refresh = output->fb_info.refresh_rate;
546 wl_list_init(&output->base.mode_list);
547 wl_list_insert(&output->base.mode_list, &output->mode.link);
548
Hardeningff39efa2013-09-18 23:56:35 +0200549 output->base.current_mode = &output->mode;
Philip Withnall4f499172013-02-02 12:02:32 +0000550 output->base.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
551 output->base.make = "unknown";
552 output->base.model = output->fb_info.id;
553
554 weston_output_init(&output->base, &compositor->base,
555 0, 0, output->fb_info.width_mm,
556 output->fb_info.height_mm,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200557 WL_OUTPUT_TRANSFORM_NORMAL,
558 1);
Philip Withnall4f499172013-02-02 12:02:32 +0000559
560 width = output->fb_info.x_resolution;
561 height = output->fb_info.y_resolution;
562
563 pixman_transform_init_identity(&transform);
564 switch (output->base.transform) {
565 default:
566 case WL_OUTPUT_TRANSFORM_NORMAL:
567 shadow_width = width;
568 shadow_height = height;
569 pixman_transform_rotate(&transform,
570 NULL, 0, 0);
571 pixman_transform_translate(&transform, NULL,
572 0, 0);
573 break;
574 case WL_OUTPUT_TRANSFORM_180:
575 shadow_width = width;
576 shadow_height = height;
577 pixman_transform_rotate(&transform,
578 NULL, -pixman_fixed_1, 0);
579 pixman_transform_translate(NULL, &transform,
580 pixman_int_to_fixed(shadow_width),
581 pixman_int_to_fixed(shadow_height));
582 break;
583 case WL_OUTPUT_TRANSFORM_270:
584 shadow_width = height;
585 shadow_height = width;
586 pixman_transform_rotate(&transform,
587 NULL, 0, pixman_fixed_1);
588 pixman_transform_translate(&transform,
589 NULL,
590 pixman_int_to_fixed(shadow_width),
591 0);
592 break;
593 case WL_OUTPUT_TRANSFORM_90:
594 shadow_width = height;
595 shadow_height = width;
596 pixman_transform_rotate(&transform,
597 NULL, 0, -pixman_fixed_1);
598 pixman_transform_translate(&transform,
599 NULL,
600 0,
601 pixman_int_to_fixed(shadow_height));
602 break;
603 }
604
605 bytes_per_pixel = output->fb_info.bits_per_pixel / 8;
606
607 output->shadow_buf = malloc(width * height * bytes_per_pixel);
608 output->shadow_surface =
609 pixman_image_create_bits(output->fb_info.pixel_format,
610 shadow_width, shadow_height,
611 output->shadow_buf,
612 shadow_width * bytes_per_pixel);
613 if (output->shadow_buf == NULL || output->shadow_surface == NULL) {
614 weston_log("Failed to create surface for frame buffer.\n");
615 goto out_hw_surface;
616 }
617
618 /* No need in transform for normal output */
619 if (output->base.transform != WL_OUTPUT_TRANSFORM_NORMAL)
620 pixman_image_set_transform(output->shadow_surface, &transform);
621
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300622 if (compositor->use_pixman) {
623 if (pixman_renderer_output_create(&output->base) < 0)
624 goto out_shadow_surface;
625 } else {
626 setenv("HYBRIS_EGLPLATFORM", "wayland", 1);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300627 if (gl_renderer->output_create(&output->base,
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000628 (EGLNativeWindowType)NULL,
629 gl_renderer->opaque_attribs,
630 NULL) < 0) {
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300631 weston_log("gl_renderer_output_create failed.\n");
632 goto out_shadow_surface;
633 }
634 }
635
Philip Withnall4f499172013-02-02 12:02:32 +0000636
637 loop = wl_display_get_event_loop(compositor->base.wl_display);
638 output->finish_frame_timer =
639 wl_event_loop_add_timer(loop, finish_frame_handler, output);
640
641 wl_list_insert(compositor->base.output_list.prev, &output->base.link);
642
643 weston_log("fbdev output %d×%d px\n",
644 output->mode.width, output->mode.height);
645 weston_log_continue(STAMP_SPACE "guessing %d Hz and 96 dpi\n",
646 output->mode.refresh / 1000);
647
648 return 0;
649
650out_shadow_surface:
651 pixman_image_unref(output->shadow_surface);
652 output->shadow_surface = NULL;
653out_hw_surface:
654 free(output->shadow_buf);
655 pixman_image_unref(output->hw_surface);
656 output->hw_surface = NULL;
657 weston_output_destroy(&output->base);
658 fbdev_frame_buffer_destroy(output);
659out_free:
660 free(output);
661
662 return -1;
663}
664
665static void
666fbdev_output_destroy(struct weston_output *base)
667{
668 struct fbdev_output *output = to_fbdev_output(base);
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300669 struct fbdev_compositor *compositor = output->compositor;
Philip Withnall4f499172013-02-02 12:02:32 +0000670
671 weston_log("Destroying fbdev output.\n");
672
673 /* Close the frame buffer. */
674 fbdev_output_disable(base);
675
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300676 if (compositor->use_pixman) {
677 if (base->renderer_state != NULL)
678 pixman_renderer_output_destroy(base);
Philip Withnall4f499172013-02-02 12:02:32 +0000679
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300680 if (output->shadow_surface != NULL) {
681 pixman_image_unref(output->shadow_surface);
682 output->shadow_surface = NULL;
683 }
Philip Withnall4f499172013-02-02 12:02:32 +0000684
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300685 if (output->shadow_buf != NULL) {
686 free(output->shadow_buf);
687 output->shadow_buf = NULL;
688 }
689 } else {
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300690 gl_renderer->output_destroy(base);
Philip Withnall4f499172013-02-02 12:02:32 +0000691 }
692
693 /* Remove the output. */
Philip Withnall4f499172013-02-02 12:02:32 +0000694 weston_output_destroy(&output->base);
695
696 free(output);
697}
698
699/* strcmp()-style return values. */
700static int
701compare_screen_info (const struct fbdev_screeninfo *a,
702 const struct fbdev_screeninfo *b)
703{
704 if (a->x_resolution == b->x_resolution &&
705 a->y_resolution == b->y_resolution &&
706 a->width_mm == b->width_mm &&
707 a->height_mm == b->height_mm &&
708 a->bits_per_pixel == b->bits_per_pixel &&
709 a->pixel_format == b->pixel_format &&
710 a->refresh_rate == b->refresh_rate)
711 return 0;
712
713 return 1;
714}
715
716static int
717fbdev_output_reenable(struct fbdev_compositor *compositor,
718 struct weston_output *base)
719{
720 struct fbdev_output *output = to_fbdev_output(base);
721 struct fbdev_screeninfo new_screen_info;
722 int fb_fd;
Rob Bradfordf8ef42f2013-07-26 16:29:38 +0100723 const char *device;
Philip Withnall4f499172013-02-02 12:02:32 +0000724
725 weston_log("Re-enabling fbdev output.\n");
726
727 /* Create the frame buffer. */
728 fb_fd = fbdev_frame_buffer_open(output, output->device,
729 &new_screen_info);
730 if (fb_fd < 0) {
731 weston_log("Creating frame buffer failed.\n");
732 goto err;
733 }
734
735 /* Check whether the frame buffer details have changed since we were
736 * disabled. */
737 if (compare_screen_info (&output->fb_info, &new_screen_info) != 0) {
738 /* Perform a mode-set to restore the old mode. */
739 if (fbdev_set_screen_info(output, fb_fd,
740 &output->fb_info) < 0) {
741 weston_log("Failed to restore mode settings. "
742 "Attempting to re-open output anyway.\n");
743 }
744
Rob Bradford581b3fd2013-07-26 16:29:39 +0100745 close(fb_fd);
746
Philip Withnall4f499172013-02-02 12:02:32 +0000747 /* Remove and re-add the output so that resources depending on
748 * the frame buffer X/Y resolution (such as the shadow buffer)
749 * are re-initialised. */
Rob Bradfordf8ef42f2013-07-26 16:29:38 +0100750 device = output->device;
Philip Withnall4f499172013-02-02 12:02:32 +0000751 fbdev_output_destroy(base);
Rob Bradfordf8ef42f2013-07-26 16:29:38 +0100752 fbdev_output_create(compositor, device);
Philip Withnall4f499172013-02-02 12:02:32 +0000753
754 return 0;
755 }
756
757 /* Map the device if it has the same details as before. */
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300758 if (compositor->use_pixman) {
759 if (fbdev_frame_buffer_map(output, fb_fd) < 0) {
760 weston_log("Mapping frame buffer failed.\n");
761 goto err;
762 }
Philip Withnall4f499172013-02-02 12:02:32 +0000763 }
764
765 return 0;
766
767err:
768 return -1;
769}
770
771/* NOTE: This leaves output->fb_info populated, caching data so that if
772 * fbdev_output_reenable() is called again, it can determine whether a mode-set
773 * is needed. */
774static void
775fbdev_output_disable(struct weston_output *base)
776{
777 struct fbdev_output *output = to_fbdev_output(base);
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300778 struct fbdev_compositor *compositor = output->compositor;
Philip Withnall4f499172013-02-02 12:02:32 +0000779
780 weston_log("Disabling fbdev output.\n");
781
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300782 if ( ! compositor->use_pixman) return;
783
Philip Withnall4f499172013-02-02 12:02:32 +0000784 if (output->hw_surface != NULL) {
785 pixman_image_unref(output->hw_surface);
786 output->hw_surface = NULL;
787 }
788
789 fbdev_frame_buffer_destroy(output);
790}
791
792static void
Philip Withnall4f499172013-02-02 12:02:32 +0000793fbdev_compositor_destroy(struct weston_compositor *base)
794{
795 struct fbdev_compositor *compositor = to_fbdev_compositor(base);
Philip Withnall4f499172013-02-02 12:02:32 +0000796
Rob Bradfordd355b802013-05-31 18:09:55 +0100797 udev_input_destroy(&compositor->input);
Philip Withnall4f499172013-02-02 12:02:32 +0000798
799 /* Destroy the output. */
800 weston_compositor_shutdown(&compositor->base);
801
802 /* Chain up. */
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700803 weston_launcher_destroy(compositor->base.launcher);
Philip Withnall4f499172013-02-02 12:02:32 +0000804
805 free(compositor);
806}
807
808static void
Kristian Høgsberg61741a22013-09-17 16:02:57 -0700809session_notify(struct wl_listener *listener, void *data)
Philip Withnall4f499172013-02-02 12:02:32 +0000810{
Kristian Høgsberg61741a22013-09-17 16:02:57 -0700811 struct fbdev_compositor *compositor = data;
Philip Withnall4f499172013-02-02 12:02:32 +0000812 struct weston_output *output;
813
Kristian Høgsberg61741a22013-09-17 16:02:57 -0700814 if (compositor->base.session_active) {
Philip Withnall4f499172013-02-02 12:02:32 +0000815 weston_log("entering VT\n");
Philip Withnall4f499172013-02-02 12:02:32 +0000816 compositor->base.state = compositor->prev_state;
817
818 wl_list_for_each(output, &compositor->base.output_list, link) {
819 fbdev_output_reenable(compositor, output);
820 }
821
822 weston_compositor_damage_all(&compositor->base);
823
Jonas Ådahl0feb32e2014-03-12 22:08:41 +0100824 udev_input_enable(&compositor->input);
Kristian Høgsberg61741a22013-09-17 16:02:57 -0700825 } else {
Philip Withnall4f499172013-02-02 12:02:32 +0000826 weston_log("leaving VT\n");
Rob Bradfordd355b802013-05-31 18:09:55 +0100827 udev_input_disable(&compositor->input);
Philip Withnall4f499172013-02-02 12:02:32 +0000828
829 wl_list_for_each(output, &compositor->base.output_list, link) {
830 fbdev_output_disable(output);
831 }
832
Philip Withnall4f499172013-02-02 12:02:32 +0000833 compositor->prev_state = compositor->base.state;
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +0100834 weston_compositor_offscreen(&compositor->base);
Philip Withnall4f499172013-02-02 12:02:32 +0000835
836 /* If we have a repaint scheduled (from the idle handler), make
837 * sure we cancel that so we don't try to pageflip when we're
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +0100838 * vt switched away. The OFFSCREEN state will prevent
Philip Withnall4f499172013-02-02 12:02:32 +0000839 * further attemps at repainting. When we switch
840 * back, we schedule a repaint, which will process
841 * pending frame callbacks. */
842
843 wl_list_for_each(output,
844 &compositor->base.output_list, link) {
845 output->repaint_needed = 0;
846 }
nerdopolis38946702014-12-03 15:53:03 +0000847 }
Philip Withnall4f499172013-02-02 12:02:32 +0000848}
849
850static void
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700851fbdev_restore(struct weston_compositor *compositor)
Philip Withnall4f499172013-02-02 12:02:32 +0000852{
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700853 weston_launcher_restore(compositor->launcher);
Philip Withnall4f499172013-02-02 12:02:32 +0000854}
855
856static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400857switch_vt_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Philip Withnall4f499172013-02-02 12:02:32 +0000858{
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700859 struct weston_compositor *compositor = data;
Philip Withnall4f499172013-02-02 12:02:32 +0000860
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700861 weston_launcher_activate_vt(compositor->launcher, key - KEY_F1 + 1);
Philip Withnall4f499172013-02-02 12:02:32 +0000862}
863
864static struct weston_compositor *
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500865fbdev_compositor_create(struct wl_display *display, int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400866 struct weston_config *config,
867 struct fbdev_parameters *param)
Philip Withnall4f499172013-02-02 12:02:32 +0000868{
869 struct fbdev_compositor *compositor;
Rob Bradford2387fde2013-05-31 18:09:57 +0100870 const char *seat_id = default_seat;
Philip Withnall4f499172013-02-02 12:02:32 +0000871 uint32_t key;
872
873 weston_log("initializing fbdev backend\n");
874
Bryce Harringtonde16d892014-11-20 22:21:57 -0800875 compositor = zalloc(sizeof *compositor);
Philip Withnall4f499172013-02-02 12:02:32 +0000876 if (compositor == NULL)
877 return NULL;
878
879 if (weston_compositor_init(&compositor->base, display, argc, argv,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400880 config) < 0)
Philip Withnall4f499172013-02-02 12:02:32 +0000881 goto out_free;
882
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400883 if (weston_compositor_set_presentation_clock_software(
884 &compositor->base) < 0)
885 goto out_compositor;
886
Philip Withnall4f499172013-02-02 12:02:32 +0000887 compositor->udev = udev_new();
888 if (compositor->udev == NULL) {
889 weston_log("Failed to initialize udev context.\n");
890 goto out_compositor;
891 }
892
893 /* Set up the TTY. */
Kristian Høgsberg61741a22013-09-17 16:02:57 -0700894 compositor->session_listener.notify = session_notify;
895 wl_signal_add(&compositor->base.session_signal,
896 &compositor->session_listener);
David Herrmann2ecb84a2014-12-30 14:33:22 +0100897 compositor->base.launcher = weston_launcher_connect(&compositor->base,
898 param->tty, "seat0",
899 false);
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700900 if (!compositor->base.launcher) {
David Herrmann1641d142013-10-15 14:29:57 +0200901 weston_log("fatal: fbdev backend should be run "
902 "using weston-launch binary or as root\n");
Philip Withnall4f499172013-02-02 12:02:32 +0000903 goto out_udev;
904 }
905
906 compositor->base.destroy = fbdev_compositor_destroy;
907 compositor->base.restore = fbdev_restore;
908
Philip Withnall4f499172013-02-02 12:02:32 +0000909 compositor->prev_state = WESTON_COMPOSITOR_ACTIVE;
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300910 compositor->use_pixman = !param->use_gl;
Philip Withnall4f499172013-02-02 12:02:32 +0000911
912 for (key = KEY_F1; key < KEY_F9; key++)
913 weston_compositor_add_key_binding(&compositor->base, key,
914 MODIFIER_CTRL | MODIFIER_ALT,
915 switch_vt_binding,
916 compositor);
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300917 if (compositor->use_pixman) {
918 if (pixman_renderer_init(&compositor->base) < 0)
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700919 goto out_launcher;
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300920 } else {
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300921 gl_renderer = weston_load_module("gl-renderer.so",
922 "gl_renderer_interface");
923 if (!gl_renderer) {
924 weston_log("could not load gl renderer\n");
925 goto out_launcher;
926 }
927
928 if (gl_renderer->create(&compositor->base, EGL_DEFAULT_DISPLAY,
929 gl_renderer->opaque_attribs,
930 NULL) < 0) {
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300931 weston_log("gl_renderer_create failed.\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700932 goto out_launcher;
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300933 }
934 }
Philip Withnall4f499172013-02-02 12:02:32 +0000935
936 if (fbdev_output_create(compositor, param->device) < 0)
937 goto out_pixman;
938
Rob Bradford2387fde2013-05-31 18:09:57 +0100939 udev_input_init(&compositor->input, &compositor->base, compositor->udev, seat_id);
Philip Withnall4f499172013-02-02 12:02:32 +0000940
941 return &compositor->base;
942
943out_pixman:
944 compositor->base.renderer->destroy(&compositor->base);
945
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700946out_launcher:
947 weston_launcher_destroy(compositor->base.launcher);
Philip Withnall4f499172013-02-02 12:02:32 +0000948
949out_udev:
950 udev_unref(compositor->udev);
951
952out_compositor:
953 weston_compositor_shutdown(&compositor->base);
954
955out_free:
956 free(compositor);
957
958 return NULL;
959}
960
961WL_EXPORT struct weston_compositor *
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500962backend_init(struct wl_display *display, int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400963 struct weston_config *config)
Philip Withnall4f499172013-02-02 12:02:32 +0000964{
965 /* TODO: Ideally, available frame buffers should be enumerated using
966 * udev, rather than passing a device node in as a parameter. */
967 struct fbdev_parameters param = {
968 .tty = 0, /* default to current tty */
969 .device = "/dev/fb0", /* default frame buffer */
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300970 .use_gl = 0,
Philip Withnall4f499172013-02-02 12:02:32 +0000971 };
972
973 const struct weston_option fbdev_options[] = {
974 { WESTON_OPTION_INTEGER, "tty", 0, &param.tty },
975 { WESTON_OPTION_STRING, "device", 0, &param.device },
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300976 { WESTON_OPTION_BOOLEAN, "use-gl", 0, &param.use_gl },
Philip Withnall4f499172013-02-02 12:02:32 +0000977 };
978
979 parse_options(fbdev_options, ARRAY_LENGTH(fbdev_options), argc, argv);
980
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400981 return fbdev_compositor_create(display, argc, argv, config, &param);
Philip Withnall4f499172013-02-02 12:02:32 +0000982}