blob: ae526e53208603eefe935552dad8999ba6f1a23c [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;
Derek Foremane516c3b2015-03-17 13:22:34 -0500553 output->base.name = strdup("fbdev");
Philip Withnall4f499172013-02-02 12:02:32 +0000554
555 weston_output_init(&output->base, &compositor->base,
556 0, 0, output->fb_info.width_mm,
557 output->fb_info.height_mm,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200558 WL_OUTPUT_TRANSFORM_NORMAL,
559 1);
Philip Withnall4f499172013-02-02 12:02:32 +0000560
561 width = output->fb_info.x_resolution;
562 height = output->fb_info.y_resolution;
563
564 pixman_transform_init_identity(&transform);
565 switch (output->base.transform) {
566 default:
567 case WL_OUTPUT_TRANSFORM_NORMAL:
568 shadow_width = width;
569 shadow_height = height;
570 pixman_transform_rotate(&transform,
571 NULL, 0, 0);
572 pixman_transform_translate(&transform, NULL,
573 0, 0);
574 break;
575 case WL_OUTPUT_TRANSFORM_180:
576 shadow_width = width;
577 shadow_height = height;
578 pixman_transform_rotate(&transform,
579 NULL, -pixman_fixed_1, 0);
580 pixman_transform_translate(NULL, &transform,
581 pixman_int_to_fixed(shadow_width),
582 pixman_int_to_fixed(shadow_height));
583 break;
584 case WL_OUTPUT_TRANSFORM_270:
585 shadow_width = height;
586 shadow_height = width;
587 pixman_transform_rotate(&transform,
588 NULL, 0, pixman_fixed_1);
589 pixman_transform_translate(&transform,
590 NULL,
591 pixman_int_to_fixed(shadow_width),
592 0);
593 break;
594 case WL_OUTPUT_TRANSFORM_90:
595 shadow_width = height;
596 shadow_height = width;
597 pixman_transform_rotate(&transform,
598 NULL, 0, -pixman_fixed_1);
599 pixman_transform_translate(&transform,
600 NULL,
601 0,
602 pixman_int_to_fixed(shadow_height));
603 break;
604 }
605
606 bytes_per_pixel = output->fb_info.bits_per_pixel / 8;
607
608 output->shadow_buf = malloc(width * height * bytes_per_pixel);
609 output->shadow_surface =
610 pixman_image_create_bits(output->fb_info.pixel_format,
611 shadow_width, shadow_height,
612 output->shadow_buf,
613 shadow_width * bytes_per_pixel);
614 if (output->shadow_buf == NULL || output->shadow_surface == NULL) {
615 weston_log("Failed to create surface for frame buffer.\n");
616 goto out_hw_surface;
617 }
618
619 /* No need in transform for normal output */
620 if (output->base.transform != WL_OUTPUT_TRANSFORM_NORMAL)
621 pixman_image_set_transform(output->shadow_surface, &transform);
622
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300623 if (compositor->use_pixman) {
624 if (pixman_renderer_output_create(&output->base) < 0)
625 goto out_shadow_surface;
626 } else {
627 setenv("HYBRIS_EGLPLATFORM", "wayland", 1);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300628 if (gl_renderer->output_create(&output->base,
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000629 (EGLNativeWindowType)NULL,
630 gl_renderer->opaque_attribs,
631 NULL) < 0) {
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300632 weston_log("gl_renderer_output_create failed.\n");
633 goto out_shadow_surface;
634 }
635 }
636
Philip Withnall4f499172013-02-02 12:02:32 +0000637
638 loop = wl_display_get_event_loop(compositor->base.wl_display);
639 output->finish_frame_timer =
640 wl_event_loop_add_timer(loop, finish_frame_handler, output);
641
642 wl_list_insert(compositor->base.output_list.prev, &output->base.link);
643
644 weston_log("fbdev output %d×%d px\n",
645 output->mode.width, output->mode.height);
646 weston_log_continue(STAMP_SPACE "guessing %d Hz and 96 dpi\n",
647 output->mode.refresh / 1000);
648
649 return 0;
650
651out_shadow_surface:
652 pixman_image_unref(output->shadow_surface);
653 output->shadow_surface = NULL;
654out_hw_surface:
655 free(output->shadow_buf);
656 pixman_image_unref(output->hw_surface);
657 output->hw_surface = NULL;
658 weston_output_destroy(&output->base);
659 fbdev_frame_buffer_destroy(output);
660out_free:
661 free(output);
662
663 return -1;
664}
665
666static void
667fbdev_output_destroy(struct weston_output *base)
668{
669 struct fbdev_output *output = to_fbdev_output(base);
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300670 struct fbdev_compositor *compositor = output->compositor;
Philip Withnall4f499172013-02-02 12:02:32 +0000671
672 weston_log("Destroying fbdev output.\n");
673
674 /* Close the frame buffer. */
675 fbdev_output_disable(base);
676
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300677 if (compositor->use_pixman) {
678 if (base->renderer_state != NULL)
679 pixman_renderer_output_destroy(base);
Philip Withnall4f499172013-02-02 12:02:32 +0000680
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300681 if (output->shadow_surface != NULL) {
682 pixman_image_unref(output->shadow_surface);
683 output->shadow_surface = NULL;
684 }
Philip Withnall4f499172013-02-02 12:02:32 +0000685
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300686 if (output->shadow_buf != NULL) {
687 free(output->shadow_buf);
688 output->shadow_buf = NULL;
689 }
690 } else {
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300691 gl_renderer->output_destroy(base);
Philip Withnall4f499172013-02-02 12:02:32 +0000692 }
693
694 /* Remove the output. */
Philip Withnall4f499172013-02-02 12:02:32 +0000695 weston_output_destroy(&output->base);
696
697 free(output);
698}
699
700/* strcmp()-style return values. */
701static int
702compare_screen_info (const struct fbdev_screeninfo *a,
703 const struct fbdev_screeninfo *b)
704{
705 if (a->x_resolution == b->x_resolution &&
706 a->y_resolution == b->y_resolution &&
707 a->width_mm == b->width_mm &&
708 a->height_mm == b->height_mm &&
709 a->bits_per_pixel == b->bits_per_pixel &&
710 a->pixel_format == b->pixel_format &&
711 a->refresh_rate == b->refresh_rate)
712 return 0;
713
714 return 1;
715}
716
717static int
718fbdev_output_reenable(struct fbdev_compositor *compositor,
719 struct weston_output *base)
720{
721 struct fbdev_output *output = to_fbdev_output(base);
722 struct fbdev_screeninfo new_screen_info;
723 int fb_fd;
Rob Bradfordf8ef42f2013-07-26 16:29:38 +0100724 const char *device;
Philip Withnall4f499172013-02-02 12:02:32 +0000725
726 weston_log("Re-enabling fbdev output.\n");
727
728 /* Create the frame buffer. */
729 fb_fd = fbdev_frame_buffer_open(output, output->device,
730 &new_screen_info);
731 if (fb_fd < 0) {
732 weston_log("Creating frame buffer failed.\n");
733 goto err;
734 }
735
736 /* Check whether the frame buffer details have changed since we were
737 * disabled. */
738 if (compare_screen_info (&output->fb_info, &new_screen_info) != 0) {
739 /* Perform a mode-set to restore the old mode. */
740 if (fbdev_set_screen_info(output, fb_fd,
741 &output->fb_info) < 0) {
742 weston_log("Failed to restore mode settings. "
743 "Attempting to re-open output anyway.\n");
744 }
745
Rob Bradford581b3fd2013-07-26 16:29:39 +0100746 close(fb_fd);
747
Philip Withnall4f499172013-02-02 12:02:32 +0000748 /* Remove and re-add the output so that resources depending on
749 * the frame buffer X/Y resolution (such as the shadow buffer)
750 * are re-initialised. */
Rob Bradfordf8ef42f2013-07-26 16:29:38 +0100751 device = output->device;
Philip Withnall4f499172013-02-02 12:02:32 +0000752 fbdev_output_destroy(base);
Rob Bradfordf8ef42f2013-07-26 16:29:38 +0100753 fbdev_output_create(compositor, device);
Philip Withnall4f499172013-02-02 12:02:32 +0000754
755 return 0;
756 }
757
758 /* Map the device if it has the same details as before. */
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300759 if (compositor->use_pixman) {
760 if (fbdev_frame_buffer_map(output, fb_fd) < 0) {
761 weston_log("Mapping frame buffer failed.\n");
762 goto err;
763 }
Philip Withnall4f499172013-02-02 12:02:32 +0000764 }
765
766 return 0;
767
768err:
769 return -1;
770}
771
772/* NOTE: This leaves output->fb_info populated, caching data so that if
773 * fbdev_output_reenable() is called again, it can determine whether a mode-set
774 * is needed. */
775static void
776fbdev_output_disable(struct weston_output *base)
777{
778 struct fbdev_output *output = to_fbdev_output(base);
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300779 struct fbdev_compositor *compositor = output->compositor;
Philip Withnall4f499172013-02-02 12:02:32 +0000780
781 weston_log("Disabling fbdev output.\n");
782
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300783 if ( ! compositor->use_pixman) return;
784
Philip Withnall4f499172013-02-02 12:02:32 +0000785 if (output->hw_surface != NULL) {
786 pixman_image_unref(output->hw_surface);
787 output->hw_surface = NULL;
788 }
789
790 fbdev_frame_buffer_destroy(output);
791}
792
793static void
Philip Withnall4f499172013-02-02 12:02:32 +0000794fbdev_compositor_destroy(struct weston_compositor *base)
795{
796 struct fbdev_compositor *compositor = to_fbdev_compositor(base);
Philip Withnall4f499172013-02-02 12:02:32 +0000797
Rob Bradfordd355b802013-05-31 18:09:55 +0100798 udev_input_destroy(&compositor->input);
Philip Withnall4f499172013-02-02 12:02:32 +0000799
800 /* Destroy the output. */
801 weston_compositor_shutdown(&compositor->base);
802
803 /* Chain up. */
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700804 weston_launcher_destroy(compositor->base.launcher);
Philip Withnall4f499172013-02-02 12:02:32 +0000805
806 free(compositor);
807}
808
809static void
Kristian Høgsberg61741a22013-09-17 16:02:57 -0700810session_notify(struct wl_listener *listener, void *data)
Philip Withnall4f499172013-02-02 12:02:32 +0000811{
Kristian Høgsberg61741a22013-09-17 16:02:57 -0700812 struct fbdev_compositor *compositor = data;
Philip Withnall4f499172013-02-02 12:02:32 +0000813 struct weston_output *output;
814
Kristian Høgsberg61741a22013-09-17 16:02:57 -0700815 if (compositor->base.session_active) {
Philip Withnall4f499172013-02-02 12:02:32 +0000816 weston_log("entering VT\n");
Philip Withnall4f499172013-02-02 12:02:32 +0000817 compositor->base.state = compositor->prev_state;
818
819 wl_list_for_each(output, &compositor->base.output_list, link) {
820 fbdev_output_reenable(compositor, output);
821 }
822
823 weston_compositor_damage_all(&compositor->base);
824
Jonas Ådahl0feb32e2014-03-12 22:08:41 +0100825 udev_input_enable(&compositor->input);
Kristian Høgsberg61741a22013-09-17 16:02:57 -0700826 } else {
Philip Withnall4f499172013-02-02 12:02:32 +0000827 weston_log("leaving VT\n");
Rob Bradfordd355b802013-05-31 18:09:55 +0100828 udev_input_disable(&compositor->input);
Philip Withnall4f499172013-02-02 12:02:32 +0000829
830 wl_list_for_each(output, &compositor->base.output_list, link) {
831 fbdev_output_disable(output);
832 }
833
Philip Withnall4f499172013-02-02 12:02:32 +0000834 compositor->prev_state = compositor->base.state;
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +0100835 weston_compositor_offscreen(&compositor->base);
Philip Withnall4f499172013-02-02 12:02:32 +0000836
837 /* If we have a repaint scheduled (from the idle handler), make
838 * sure we cancel that so we don't try to pageflip when we're
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +0100839 * vt switched away. The OFFSCREEN state will prevent
Philip Withnall4f499172013-02-02 12:02:32 +0000840 * further attemps at repainting. When we switch
841 * back, we schedule a repaint, which will process
842 * pending frame callbacks. */
843
844 wl_list_for_each(output,
845 &compositor->base.output_list, link) {
846 output->repaint_needed = 0;
847 }
nerdopolis38946702014-12-03 15:53:03 +0000848 }
Philip Withnall4f499172013-02-02 12:02:32 +0000849}
850
851static void
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700852fbdev_restore(struct weston_compositor *compositor)
Philip Withnall4f499172013-02-02 12:02:32 +0000853{
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700854 weston_launcher_restore(compositor->launcher);
Philip Withnall4f499172013-02-02 12:02:32 +0000855}
856
857static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400858switch_vt_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Philip Withnall4f499172013-02-02 12:02:32 +0000859{
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700860 struct weston_compositor *compositor = data;
Philip Withnall4f499172013-02-02 12:02:32 +0000861
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700862 weston_launcher_activate_vt(compositor->launcher, key - KEY_F1 + 1);
Philip Withnall4f499172013-02-02 12:02:32 +0000863}
864
865static struct weston_compositor *
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500866fbdev_compositor_create(struct wl_display *display, int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400867 struct weston_config *config,
868 struct fbdev_parameters *param)
Philip Withnall4f499172013-02-02 12:02:32 +0000869{
870 struct fbdev_compositor *compositor;
Rob Bradford2387fde2013-05-31 18:09:57 +0100871 const char *seat_id = default_seat;
Philip Withnall4f499172013-02-02 12:02:32 +0000872 uint32_t key;
873
874 weston_log("initializing fbdev backend\n");
875
Bryce Harringtonde16d892014-11-20 22:21:57 -0800876 compositor = zalloc(sizeof *compositor);
Philip Withnall4f499172013-02-02 12:02:32 +0000877 if (compositor == NULL)
878 return NULL;
879
880 if (weston_compositor_init(&compositor->base, display, argc, argv,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400881 config) < 0)
Philip Withnall4f499172013-02-02 12:02:32 +0000882 goto out_free;
883
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400884 if (weston_compositor_set_presentation_clock_software(
885 &compositor->base) < 0)
886 goto out_compositor;
887
Philip Withnall4f499172013-02-02 12:02:32 +0000888 compositor->udev = udev_new();
889 if (compositor->udev == NULL) {
890 weston_log("Failed to initialize udev context.\n");
891 goto out_compositor;
892 }
893
894 /* Set up the TTY. */
Kristian Høgsberg61741a22013-09-17 16:02:57 -0700895 compositor->session_listener.notify = session_notify;
896 wl_signal_add(&compositor->base.session_signal,
897 &compositor->session_listener);
David Herrmann2ecb84a2014-12-30 14:33:22 +0100898 compositor->base.launcher = weston_launcher_connect(&compositor->base,
899 param->tty, "seat0",
900 false);
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700901 if (!compositor->base.launcher) {
David Herrmann1641d142013-10-15 14:29:57 +0200902 weston_log("fatal: fbdev backend should be run "
903 "using weston-launch binary or as root\n");
Philip Withnall4f499172013-02-02 12:02:32 +0000904 goto out_udev;
905 }
906
907 compositor->base.destroy = fbdev_compositor_destroy;
908 compositor->base.restore = fbdev_restore;
909
Philip Withnall4f499172013-02-02 12:02:32 +0000910 compositor->prev_state = WESTON_COMPOSITOR_ACTIVE;
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300911 compositor->use_pixman = !param->use_gl;
Philip Withnall4f499172013-02-02 12:02:32 +0000912
913 for (key = KEY_F1; key < KEY_F9; key++)
914 weston_compositor_add_key_binding(&compositor->base, key,
915 MODIFIER_CTRL | MODIFIER_ALT,
916 switch_vt_binding,
917 compositor);
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300918 if (compositor->use_pixman) {
919 if (pixman_renderer_init(&compositor->base) < 0)
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700920 goto out_launcher;
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300921 } else {
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300922 gl_renderer = weston_load_module("gl-renderer.so",
923 "gl_renderer_interface");
924 if (!gl_renderer) {
925 weston_log("could not load gl renderer\n");
926 goto out_launcher;
927 }
928
929 if (gl_renderer->create(&compositor->base, EGL_DEFAULT_DISPLAY,
930 gl_renderer->opaque_attribs,
931 NULL) < 0) {
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300932 weston_log("gl_renderer_create failed.\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700933 goto out_launcher;
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300934 }
935 }
Philip Withnall4f499172013-02-02 12:02:32 +0000936
937 if (fbdev_output_create(compositor, param->device) < 0)
938 goto out_pixman;
939
Rob Bradford2387fde2013-05-31 18:09:57 +0100940 udev_input_init(&compositor->input, &compositor->base, compositor->udev, seat_id);
Philip Withnall4f499172013-02-02 12:02:32 +0000941
942 return &compositor->base;
943
944out_pixman:
945 compositor->base.renderer->destroy(&compositor->base);
946
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700947out_launcher:
948 weston_launcher_destroy(compositor->base.launcher);
Philip Withnall4f499172013-02-02 12:02:32 +0000949
950out_udev:
951 udev_unref(compositor->udev);
952
953out_compositor:
954 weston_compositor_shutdown(&compositor->base);
955
956out_free:
957 free(compositor);
958
959 return NULL;
960}
961
962WL_EXPORT struct weston_compositor *
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500963backend_init(struct wl_display *display, int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400964 struct weston_config *config)
Philip Withnall4f499172013-02-02 12:02:32 +0000965{
966 /* TODO: Ideally, available frame buffers should be enumerated using
967 * udev, rather than passing a device node in as a parameter. */
968 struct fbdev_parameters param = {
969 .tty = 0, /* default to current tty */
970 .device = "/dev/fb0", /* default frame buffer */
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300971 .use_gl = 0,
Philip Withnall4f499172013-02-02 12:02:32 +0000972 };
973
974 const struct weston_option fbdev_options[] = {
975 { WESTON_OPTION_INTEGER, "tty", 0, &param.tty },
976 { WESTON_OPTION_STRING, "device", 0, &param.device },
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300977 { WESTON_OPTION_BOOLEAN, "use-gl", 0, &param.use_gl },
Philip Withnall4f499172013-02-02 12:02:32 +0000978 };
979
980 parse_options(fbdev_options, ARRAY_LENGTH(fbdev_options), argc, argv);
981
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400982 return fbdev_compositor_create(display, argc, argv, config, &param);
Philip Withnall4f499172013-02-02 12:02:32 +0000983}