blob: c08f1706a0484f0cfeec934ee99fb7885e060b91 [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;
Derek Foreman44ed70b2015-03-17 13:22:37 -0500506 struct weston_config_section *section;
Philip Withnall4f499172013-02-02 12:02:32 +0000507 int fb_fd;
508 int shadow_width, shadow_height;
509 int width, height;
510 unsigned int bytes_per_pixel;
511 struct wl_event_loop *loop;
Derek Foreman44ed70b2015-03-17 13:22:37 -0500512 uint32_t config_transform;
513 char *s;
Philip Withnall4f499172013-02-02 12:02:32 +0000514
515 weston_log("Creating fbdev output.\n");
516
Bryce Harringtonde16d892014-11-20 22:21:57 -0800517 output = zalloc(sizeof *output);
518 if (output == NULL)
Philip Withnall4f499172013-02-02 12:02:32 +0000519 return -1;
520
521 output->compositor = compositor;
522 output->device = device;
523
524 /* Create the frame buffer. */
525 fb_fd = fbdev_frame_buffer_open(output, device, &output->fb_info);
526 if (fb_fd < 0) {
527 weston_log("Creating frame buffer failed.\n");
528 goto out_free;
529 }
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300530 if (compositor->use_pixman) {
531 if (fbdev_frame_buffer_map(output, fb_fd) < 0) {
532 weston_log("Mapping frame buffer failed.\n");
533 goto out_free;
534 }
Kristian Høgsberg8ac6a2d2013-10-09 09:59:06 -0700535 } else {
536 close(fb_fd);
Philip Withnall4f499172013-02-02 12:02:32 +0000537 }
538
Jonas Ådahle5a12252013-04-05 23:07:11 +0200539 output->base.start_repaint_loop = fbdev_output_start_repaint_loop;
Philip Withnall4f499172013-02-02 12:02:32 +0000540 output->base.repaint = fbdev_output_repaint;
541 output->base.destroy = fbdev_output_destroy;
Philip Withnall4f499172013-02-02 12:02:32 +0000542
543 /* only one static mode in list */
544 output->mode.flags =
545 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
546 output->mode.width = output->fb_info.x_resolution;
547 output->mode.height = output->fb_info.y_resolution;
548 output->mode.refresh = output->fb_info.refresh_rate;
549 wl_list_init(&output->base.mode_list);
550 wl_list_insert(&output->base.mode_list, &output->mode.link);
551
Hardeningff39efa2013-09-18 23:56:35 +0200552 output->base.current_mode = &output->mode;
Philip Withnall4f499172013-02-02 12:02:32 +0000553 output->base.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
554 output->base.make = "unknown";
555 output->base.model = output->fb_info.id;
Derek Foremane516c3b2015-03-17 13:22:34 -0500556 output->base.name = strdup("fbdev");
Philip Withnall4f499172013-02-02 12:02:32 +0000557
Derek Foreman44ed70b2015-03-17 13:22:37 -0500558 section = weston_config_get_section(compositor->base.config,
559 "output", "name",
560 output->base.name);
561 weston_config_section_get_string(section, "transform", &s, "normal");
562 if (weston_parse_transform(s, &config_transform) < 0)
563 weston_log("Invalid transform \"%s\" for output %s\n",
564 s, output->base.name);
565 free(s);
566
Philip Withnall4f499172013-02-02 12:02:32 +0000567 weston_output_init(&output->base, &compositor->base,
568 0, 0, output->fb_info.width_mm,
569 output->fb_info.height_mm,
Derek Foreman44ed70b2015-03-17 13:22:37 -0500570 config_transform,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200571 1);
Philip Withnall4f499172013-02-02 12:02:32 +0000572
573 width = output->fb_info.x_resolution;
574 height = output->fb_info.y_resolution;
575
576 pixman_transform_init_identity(&transform);
577 switch (output->base.transform) {
578 default:
579 case WL_OUTPUT_TRANSFORM_NORMAL:
580 shadow_width = width;
581 shadow_height = height;
582 pixman_transform_rotate(&transform,
583 NULL, 0, 0);
584 pixman_transform_translate(&transform, NULL,
585 0, 0);
586 break;
587 case WL_OUTPUT_TRANSFORM_180:
588 shadow_width = width;
589 shadow_height = height;
590 pixman_transform_rotate(&transform,
591 NULL, -pixman_fixed_1, 0);
592 pixman_transform_translate(NULL, &transform,
593 pixman_int_to_fixed(shadow_width),
594 pixman_int_to_fixed(shadow_height));
595 break;
596 case WL_OUTPUT_TRANSFORM_270:
597 shadow_width = height;
598 shadow_height = width;
599 pixman_transform_rotate(&transform,
600 NULL, 0, pixman_fixed_1);
601 pixman_transform_translate(&transform,
602 NULL,
603 pixman_int_to_fixed(shadow_width),
604 0);
605 break;
606 case WL_OUTPUT_TRANSFORM_90:
607 shadow_width = height;
608 shadow_height = width;
609 pixman_transform_rotate(&transform,
610 NULL, 0, -pixman_fixed_1);
611 pixman_transform_translate(&transform,
612 NULL,
613 0,
614 pixman_int_to_fixed(shadow_height));
615 break;
616 }
617
618 bytes_per_pixel = output->fb_info.bits_per_pixel / 8;
619
620 output->shadow_buf = malloc(width * height * bytes_per_pixel);
621 output->shadow_surface =
622 pixman_image_create_bits(output->fb_info.pixel_format,
623 shadow_width, shadow_height,
624 output->shadow_buf,
625 shadow_width * bytes_per_pixel);
626 if (output->shadow_buf == NULL || output->shadow_surface == NULL) {
627 weston_log("Failed to create surface for frame buffer.\n");
628 goto out_hw_surface;
629 }
630
631 /* No need in transform for normal output */
632 if (output->base.transform != WL_OUTPUT_TRANSFORM_NORMAL)
633 pixman_image_set_transform(output->shadow_surface, &transform);
634
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300635 if (compositor->use_pixman) {
636 if (pixman_renderer_output_create(&output->base) < 0)
637 goto out_shadow_surface;
638 } else {
639 setenv("HYBRIS_EGLPLATFORM", "wayland", 1);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300640 if (gl_renderer->output_create(&output->base,
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000641 (EGLNativeWindowType)NULL,
642 gl_renderer->opaque_attribs,
643 NULL) < 0) {
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300644 weston_log("gl_renderer_output_create failed.\n");
645 goto out_shadow_surface;
646 }
647 }
648
Philip Withnall4f499172013-02-02 12:02:32 +0000649
650 loop = wl_display_get_event_loop(compositor->base.wl_display);
651 output->finish_frame_timer =
652 wl_event_loop_add_timer(loop, finish_frame_handler, output);
653
654 wl_list_insert(compositor->base.output_list.prev, &output->base.link);
655
656 weston_log("fbdev output %d×%d px\n",
657 output->mode.width, output->mode.height);
658 weston_log_continue(STAMP_SPACE "guessing %d Hz and 96 dpi\n",
659 output->mode.refresh / 1000);
660
661 return 0;
662
663out_shadow_surface:
664 pixman_image_unref(output->shadow_surface);
665 output->shadow_surface = NULL;
666out_hw_surface:
667 free(output->shadow_buf);
668 pixman_image_unref(output->hw_surface);
669 output->hw_surface = NULL;
670 weston_output_destroy(&output->base);
671 fbdev_frame_buffer_destroy(output);
672out_free:
673 free(output);
674
675 return -1;
676}
677
678static void
679fbdev_output_destroy(struct weston_output *base)
680{
681 struct fbdev_output *output = to_fbdev_output(base);
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300682 struct fbdev_compositor *compositor = output->compositor;
Philip Withnall4f499172013-02-02 12:02:32 +0000683
684 weston_log("Destroying fbdev output.\n");
685
686 /* Close the frame buffer. */
687 fbdev_output_disable(base);
688
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300689 if (compositor->use_pixman) {
690 if (base->renderer_state != NULL)
691 pixman_renderer_output_destroy(base);
Philip Withnall4f499172013-02-02 12:02:32 +0000692
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300693 if (output->shadow_surface != NULL) {
694 pixman_image_unref(output->shadow_surface);
695 output->shadow_surface = NULL;
696 }
Philip Withnall4f499172013-02-02 12:02:32 +0000697
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300698 if (output->shadow_buf != NULL) {
699 free(output->shadow_buf);
700 output->shadow_buf = NULL;
701 }
702 } else {
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300703 gl_renderer->output_destroy(base);
Philip Withnall4f499172013-02-02 12:02:32 +0000704 }
705
706 /* Remove the output. */
Philip Withnall4f499172013-02-02 12:02:32 +0000707 weston_output_destroy(&output->base);
708
709 free(output);
710}
711
712/* strcmp()-style return values. */
713static int
714compare_screen_info (const struct fbdev_screeninfo *a,
715 const struct fbdev_screeninfo *b)
716{
717 if (a->x_resolution == b->x_resolution &&
718 a->y_resolution == b->y_resolution &&
719 a->width_mm == b->width_mm &&
720 a->height_mm == b->height_mm &&
721 a->bits_per_pixel == b->bits_per_pixel &&
722 a->pixel_format == b->pixel_format &&
723 a->refresh_rate == b->refresh_rate)
724 return 0;
725
726 return 1;
727}
728
729static int
730fbdev_output_reenable(struct fbdev_compositor *compositor,
731 struct weston_output *base)
732{
733 struct fbdev_output *output = to_fbdev_output(base);
734 struct fbdev_screeninfo new_screen_info;
735 int fb_fd;
Rob Bradfordf8ef42f2013-07-26 16:29:38 +0100736 const char *device;
Philip Withnall4f499172013-02-02 12:02:32 +0000737
738 weston_log("Re-enabling fbdev output.\n");
739
740 /* Create the frame buffer. */
741 fb_fd = fbdev_frame_buffer_open(output, output->device,
742 &new_screen_info);
743 if (fb_fd < 0) {
744 weston_log("Creating frame buffer failed.\n");
745 goto err;
746 }
747
748 /* Check whether the frame buffer details have changed since we were
749 * disabled. */
750 if (compare_screen_info (&output->fb_info, &new_screen_info) != 0) {
751 /* Perform a mode-set to restore the old mode. */
752 if (fbdev_set_screen_info(output, fb_fd,
753 &output->fb_info) < 0) {
754 weston_log("Failed to restore mode settings. "
755 "Attempting to re-open output anyway.\n");
756 }
757
Rob Bradford581b3fd2013-07-26 16:29:39 +0100758 close(fb_fd);
759
Philip Withnall4f499172013-02-02 12:02:32 +0000760 /* Remove and re-add the output so that resources depending on
761 * the frame buffer X/Y resolution (such as the shadow buffer)
762 * are re-initialised. */
Rob Bradfordf8ef42f2013-07-26 16:29:38 +0100763 device = output->device;
Philip Withnall4f499172013-02-02 12:02:32 +0000764 fbdev_output_destroy(base);
Rob Bradfordf8ef42f2013-07-26 16:29:38 +0100765 fbdev_output_create(compositor, device);
Philip Withnall4f499172013-02-02 12:02:32 +0000766
767 return 0;
768 }
769
770 /* Map the device if it has the same details as before. */
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300771 if (compositor->use_pixman) {
772 if (fbdev_frame_buffer_map(output, fb_fd) < 0) {
773 weston_log("Mapping frame buffer failed.\n");
774 goto err;
775 }
Philip Withnall4f499172013-02-02 12:02:32 +0000776 }
777
778 return 0;
779
780err:
781 return -1;
782}
783
784/* NOTE: This leaves output->fb_info populated, caching data so that if
785 * fbdev_output_reenable() is called again, it can determine whether a mode-set
786 * is needed. */
787static void
788fbdev_output_disable(struct weston_output *base)
789{
790 struct fbdev_output *output = to_fbdev_output(base);
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300791 struct fbdev_compositor *compositor = output->compositor;
Philip Withnall4f499172013-02-02 12:02:32 +0000792
793 weston_log("Disabling fbdev output.\n");
794
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300795 if ( ! compositor->use_pixman) return;
796
Philip Withnall4f499172013-02-02 12:02:32 +0000797 if (output->hw_surface != NULL) {
798 pixman_image_unref(output->hw_surface);
799 output->hw_surface = NULL;
800 }
801
802 fbdev_frame_buffer_destroy(output);
803}
804
805static void
Philip Withnall4f499172013-02-02 12:02:32 +0000806fbdev_compositor_destroy(struct weston_compositor *base)
807{
808 struct fbdev_compositor *compositor = to_fbdev_compositor(base);
Philip Withnall4f499172013-02-02 12:02:32 +0000809
Rob Bradfordd355b802013-05-31 18:09:55 +0100810 udev_input_destroy(&compositor->input);
Philip Withnall4f499172013-02-02 12:02:32 +0000811
812 /* Destroy the output. */
813 weston_compositor_shutdown(&compositor->base);
814
815 /* Chain up. */
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700816 weston_launcher_destroy(compositor->base.launcher);
Philip Withnall4f499172013-02-02 12:02:32 +0000817
818 free(compositor);
819}
820
821static void
Kristian Høgsberg61741a22013-09-17 16:02:57 -0700822session_notify(struct wl_listener *listener, void *data)
Philip Withnall4f499172013-02-02 12:02:32 +0000823{
Kristian Høgsberg61741a22013-09-17 16:02:57 -0700824 struct fbdev_compositor *compositor = data;
Philip Withnall4f499172013-02-02 12:02:32 +0000825 struct weston_output *output;
826
Kristian Høgsberg61741a22013-09-17 16:02:57 -0700827 if (compositor->base.session_active) {
Philip Withnall4f499172013-02-02 12:02:32 +0000828 weston_log("entering VT\n");
Philip Withnall4f499172013-02-02 12:02:32 +0000829 compositor->base.state = compositor->prev_state;
830
831 wl_list_for_each(output, &compositor->base.output_list, link) {
832 fbdev_output_reenable(compositor, output);
833 }
834
835 weston_compositor_damage_all(&compositor->base);
836
Jonas Ådahl0feb32e2014-03-12 22:08:41 +0100837 udev_input_enable(&compositor->input);
Kristian Høgsberg61741a22013-09-17 16:02:57 -0700838 } else {
Philip Withnall4f499172013-02-02 12:02:32 +0000839 weston_log("leaving VT\n");
Rob Bradfordd355b802013-05-31 18:09:55 +0100840 udev_input_disable(&compositor->input);
Philip Withnall4f499172013-02-02 12:02:32 +0000841
842 wl_list_for_each(output, &compositor->base.output_list, link) {
843 fbdev_output_disable(output);
844 }
845
Philip Withnall4f499172013-02-02 12:02:32 +0000846 compositor->prev_state = compositor->base.state;
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +0100847 weston_compositor_offscreen(&compositor->base);
Philip Withnall4f499172013-02-02 12:02:32 +0000848
849 /* If we have a repaint scheduled (from the idle handler), make
850 * sure we cancel that so we don't try to pageflip when we're
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +0100851 * vt switched away. The OFFSCREEN state will prevent
Philip Withnall4f499172013-02-02 12:02:32 +0000852 * further attemps at repainting. When we switch
853 * back, we schedule a repaint, which will process
854 * pending frame callbacks. */
855
856 wl_list_for_each(output,
857 &compositor->base.output_list, link) {
858 output->repaint_needed = 0;
859 }
nerdopolis38946702014-12-03 15:53:03 +0000860 }
Philip Withnall4f499172013-02-02 12:02:32 +0000861}
862
863static void
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700864fbdev_restore(struct weston_compositor *compositor)
Philip Withnall4f499172013-02-02 12:02:32 +0000865{
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700866 weston_launcher_restore(compositor->launcher);
Philip Withnall4f499172013-02-02 12:02:32 +0000867}
868
869static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400870switch_vt_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Philip Withnall4f499172013-02-02 12:02:32 +0000871{
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700872 struct weston_compositor *compositor = data;
Philip Withnall4f499172013-02-02 12:02:32 +0000873
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700874 weston_launcher_activate_vt(compositor->launcher, key - KEY_F1 + 1);
Philip Withnall4f499172013-02-02 12:02:32 +0000875}
876
877static struct weston_compositor *
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500878fbdev_compositor_create(struct wl_display *display, int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400879 struct weston_config *config,
880 struct fbdev_parameters *param)
Philip Withnall4f499172013-02-02 12:02:32 +0000881{
882 struct fbdev_compositor *compositor;
Rob Bradford2387fde2013-05-31 18:09:57 +0100883 const char *seat_id = default_seat;
Philip Withnall4f499172013-02-02 12:02:32 +0000884 uint32_t key;
885
886 weston_log("initializing fbdev backend\n");
887
Bryce Harringtonde16d892014-11-20 22:21:57 -0800888 compositor = zalloc(sizeof *compositor);
Philip Withnall4f499172013-02-02 12:02:32 +0000889 if (compositor == NULL)
890 return NULL;
891
892 if (weston_compositor_init(&compositor->base, display, argc, argv,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400893 config) < 0)
Philip Withnall4f499172013-02-02 12:02:32 +0000894 goto out_free;
895
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400896 if (weston_compositor_set_presentation_clock_software(
897 &compositor->base) < 0)
898 goto out_compositor;
899
Philip Withnall4f499172013-02-02 12:02:32 +0000900 compositor->udev = udev_new();
901 if (compositor->udev == NULL) {
902 weston_log("Failed to initialize udev context.\n");
903 goto out_compositor;
904 }
905
906 /* Set up the TTY. */
Kristian Høgsberg61741a22013-09-17 16:02:57 -0700907 compositor->session_listener.notify = session_notify;
908 wl_signal_add(&compositor->base.session_signal,
909 &compositor->session_listener);
David Herrmann2ecb84a2014-12-30 14:33:22 +0100910 compositor->base.launcher = weston_launcher_connect(&compositor->base,
911 param->tty, "seat0",
912 false);
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700913 if (!compositor->base.launcher) {
David Herrmann1641d142013-10-15 14:29:57 +0200914 weston_log("fatal: fbdev backend should be run "
915 "using weston-launch binary or as root\n");
Philip Withnall4f499172013-02-02 12:02:32 +0000916 goto out_udev;
917 }
918
919 compositor->base.destroy = fbdev_compositor_destroy;
920 compositor->base.restore = fbdev_restore;
921
Philip Withnall4f499172013-02-02 12:02:32 +0000922 compositor->prev_state = WESTON_COMPOSITOR_ACTIVE;
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300923 compositor->use_pixman = !param->use_gl;
Philip Withnall4f499172013-02-02 12:02:32 +0000924
925 for (key = KEY_F1; key < KEY_F9; key++)
926 weston_compositor_add_key_binding(&compositor->base, key,
927 MODIFIER_CTRL | MODIFIER_ALT,
928 switch_vt_binding,
929 compositor);
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300930 if (compositor->use_pixman) {
931 if (pixman_renderer_init(&compositor->base) < 0)
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700932 goto out_launcher;
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300933 } else {
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300934 gl_renderer = weston_load_module("gl-renderer.so",
935 "gl_renderer_interface");
936 if (!gl_renderer) {
937 weston_log("could not load gl renderer\n");
938 goto out_launcher;
939 }
940
941 if (gl_renderer->create(&compositor->base, EGL_DEFAULT_DISPLAY,
942 gl_renderer->opaque_attribs,
943 NULL) < 0) {
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300944 weston_log("gl_renderer_create failed.\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700945 goto out_launcher;
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300946 }
947 }
Philip Withnall4f499172013-02-02 12:02:32 +0000948
949 if (fbdev_output_create(compositor, param->device) < 0)
950 goto out_pixman;
951
Rob Bradford2387fde2013-05-31 18:09:57 +0100952 udev_input_init(&compositor->input, &compositor->base, compositor->udev, seat_id);
Philip Withnall4f499172013-02-02 12:02:32 +0000953
954 return &compositor->base;
955
956out_pixman:
957 compositor->base.renderer->destroy(&compositor->base);
958
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700959out_launcher:
960 weston_launcher_destroy(compositor->base.launcher);
Philip Withnall4f499172013-02-02 12:02:32 +0000961
962out_udev:
963 udev_unref(compositor->udev);
964
965out_compositor:
966 weston_compositor_shutdown(&compositor->base);
967
968out_free:
969 free(compositor);
970
971 return NULL;
972}
973
974WL_EXPORT struct weston_compositor *
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500975backend_init(struct wl_display *display, int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400976 struct weston_config *config)
Philip Withnall4f499172013-02-02 12:02:32 +0000977{
978 /* TODO: Ideally, available frame buffers should be enumerated using
979 * udev, rather than passing a device node in as a parameter. */
980 struct fbdev_parameters param = {
981 .tty = 0, /* default to current tty */
982 .device = "/dev/fb0", /* default frame buffer */
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300983 .use_gl = 0,
Philip Withnall4f499172013-02-02 12:02:32 +0000984 };
985
986 const struct weston_option fbdev_options[] = {
987 { WESTON_OPTION_INTEGER, "tty", 0, &param.tty },
988 { WESTON_OPTION_STRING, "device", 0, &param.device },
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300989 { WESTON_OPTION_BOOLEAN, "use-gl", 0, &param.use_gl },
Philip Withnall4f499172013-02-02 12:02:32 +0000990 };
991
992 parse_options(fbdev_options, ARRAY_LENGTH(fbdev_options), argc, argv);
993
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400994 return fbdev_compositor_create(display, argc, argv, config, &param);
Philip Withnall4f499172013-02-02 12:02:32 +0000995}