Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2008-2011 Kristian Høgsberg |
| 3 | * Copyright © 2011 Intel Corporation |
| 4 | * Copyright © 2012 Raspberry Pi Foundation |
| 5 | * Copyright © 2013 Philip Withnall |
| 6 | * |
Bryce Harrington | a0bbfea | 2015-06-11 15:35:43 -0700 | [diff] [blame] | 7 | * Permission is hereby granted, free of charge, to any person obtaining |
| 8 | * a copy of this software and associated documentation files (the |
| 9 | * "Software"), to deal in the Software without restriction, including |
| 10 | * without limitation the rights to use, copy, modify, merge, publish, |
| 11 | * distribute, sublicense, and/or sell copies of the Software, and to |
| 12 | * permit persons to whom the Software is furnished to do so, subject to |
| 13 | * the following conditions: |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 14 | * |
Bryce Harrington | a0bbfea | 2015-06-11 15:35:43 -0700 | [diff] [blame] | 15 | * The above copyright notice and this permission notice (including the |
| 16 | * next paragraph) shall be included in all copies or substantial |
| 17 | * portions of the Software. |
| 18 | * |
| 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 20 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 21 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 22 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 23 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 24 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 25 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 26 | * SOFTWARE. |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 27 | */ |
| 28 | |
Daniel Stone | c228e23 | 2013-05-22 18:03:19 +0300 | [diff] [blame] | 29 | #include "config.h" |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 30 | |
| 31 | #include <errno.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <stdio.h> |
| 34 | #include <string.h> |
| 35 | #include <math.h> |
| 36 | #include <sys/mman.h> |
| 37 | #include <sys/types.h> |
| 38 | #include <fcntl.h> |
| 39 | #include <unistd.h> |
| 40 | #include <linux/fb.h> |
Kristian Høgsberg | 7e597f2 | 2013-02-18 16:35:26 -0500 | [diff] [blame] | 41 | #include <linux/input.h> |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 42 | |
| 43 | #include <libudev.h> |
| 44 | |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 45 | #include "compositor.h" |
| 46 | #include "launcher-util.h" |
| 47 | #include "pixman-renderer.h" |
Peter Hutterer | 823ad33 | 2014-11-26 07:06:31 +1000 | [diff] [blame] | 48 | #include "libinput-seat.h" |
Adrian Negreanu | 4aa756d | 2013-09-06 15:16:09 +0300 | [diff] [blame] | 49 | #include "gl-renderer.h" |
Pekka Paalanen | 363aa7b | 2014-12-17 16:20:40 +0200 | [diff] [blame] | 50 | #include "presentation_timing-server-protocol.h" |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 51 | |
| 52 | struct fbdev_compositor { |
| 53 | struct weston_compositor base; |
| 54 | uint32_t prev_state; |
| 55 | |
| 56 | struct udev *udev; |
Rob Bradford | d355b80 | 2013-05-31 18:09:55 +0100 | [diff] [blame] | 57 | struct udev_input input; |
Adrian Negreanu | 4aa756d | 2013-09-06 15:16:09 +0300 | [diff] [blame] | 58 | int use_pixman; |
Kristian Høgsberg | 61741a2 | 2013-09-17 16:02:57 -0700 | [diff] [blame] | 59 | struct wl_listener session_listener; |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | struct fbdev_screeninfo { |
| 63 | unsigned int x_resolution; /* pixels, visible area */ |
| 64 | unsigned int y_resolution; /* pixels, visible area */ |
| 65 | unsigned int width_mm; /* visible screen width in mm */ |
| 66 | unsigned int height_mm; /* visible screen height in mm */ |
| 67 | unsigned int bits_per_pixel; |
| 68 | |
| 69 | size_t buffer_length; /* length of frame buffer memory in bytes */ |
| 70 | size_t line_length; /* length of a line in bytes */ |
| 71 | char id[16]; /* screen identifier */ |
| 72 | |
| 73 | pixman_format_code_t pixel_format; /* frame buffer pixel format */ |
| 74 | unsigned int refresh_rate; /* Hertz */ |
| 75 | }; |
| 76 | |
| 77 | struct fbdev_output { |
| 78 | struct fbdev_compositor *compositor; |
| 79 | struct weston_output base; |
| 80 | |
| 81 | struct weston_mode mode; |
| 82 | struct wl_event_source *finish_frame_timer; |
| 83 | |
| 84 | /* Frame buffer details. */ |
| 85 | const char *device; /* ownership shared with fbdev_parameters */ |
| 86 | struct fbdev_screeninfo fb_info; |
| 87 | void *fb; /* length is fb_info.buffer_length */ |
| 88 | |
| 89 | /* pixman details. */ |
| 90 | pixman_image_t *hw_surface; |
| 91 | pixman_image_t *shadow_surface; |
| 92 | void *shadow_buf; |
| 93 | uint8_t depth; |
| 94 | }; |
| 95 | |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 96 | struct fbdev_parameters { |
| 97 | int tty; |
| 98 | char *device; |
Adrian Negreanu | 4aa756d | 2013-09-06 15:16:09 +0300 | [diff] [blame] | 99 | int use_gl; |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 100 | }; |
| 101 | |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 102 | struct gl_renderer_interface *gl_renderer; |
| 103 | |
Kristian Høgsberg | 7e597f2 | 2013-02-18 16:35:26 -0500 | [diff] [blame] | 104 | static const char default_seat[] = "seat0"; |
| 105 | |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 106 | static inline struct fbdev_output * |
| 107 | to_fbdev_output(struct weston_output *base) |
| 108 | { |
| 109 | return container_of(base, struct fbdev_output, base); |
| 110 | } |
| 111 | |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 112 | static inline struct fbdev_compositor * |
| 113 | to_fbdev_compositor(struct weston_compositor *base) |
| 114 | { |
| 115 | return container_of(base, struct fbdev_compositor, base); |
| 116 | } |
| 117 | |
| 118 | static void |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 119 | fbdev_output_start_repaint_loop(struct weston_output *output) |
| 120 | { |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame] | 121 | struct timespec ts; |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 122 | |
Pekka Paalanen | 662f384 | 2015-03-18 12:17:26 +0200 | [diff] [blame] | 123 | weston_compositor_read_presentation_clock(output->compositor, &ts); |
Pekka Paalanen | 363aa7b | 2014-12-17 16:20:40 +0200 | [diff] [blame] | 124 | weston_output_finish_frame(output, &ts, PRESENTATION_FEEDBACK_INVALID); |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | static void |
Adrian Negreanu | 4aa756d | 2013-09-06 15:16:09 +0300 | [diff] [blame] | 128 | fbdev_output_repaint_pixman(struct weston_output *base, pixman_region32_t *damage) |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 129 | { |
| 130 | struct fbdev_output *output = to_fbdev_output(base); |
| 131 | struct weston_compositor *ec = output->base.compositor; |
| 132 | pixman_box32_t *rects; |
Derek Foreman | 561662b | 2015-03-17 13:22:38 -0500 | [diff] [blame] | 133 | int nrects, i; |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 134 | |
| 135 | /* Repaint the damaged region onto the back buffer. */ |
| 136 | pixman_renderer_output_set_buffer(base, output->shadow_surface); |
| 137 | ec->renderer->repaint_output(base, damage); |
| 138 | |
| 139 | /* Transform and composite onto the frame buffer. */ |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 140 | rects = pixman_region32_rectangles(damage, &nrects); |
| 141 | |
| 142 | for (i = 0; i < nrects; i++) { |
Derek Foreman | 561662b | 2015-03-17 13:22:38 -0500 | [diff] [blame] | 143 | pixman_box32_t transformed_rect; |
| 144 | int width, height; |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 145 | |
Derek Foreman | 561662b | 2015-03-17 13:22:38 -0500 | [diff] [blame] | 146 | transformed_rect = weston_transformed_rect(base->width, |
| 147 | base->height, |
| 148 | base->transform, |
| 149 | 1, rects[i]); |
| 150 | width = transformed_rect.x2 - transformed_rect.x1; |
| 151 | height = transformed_rect.y2 - transformed_rect.y1; |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 152 | pixman_image_composite32(PIXMAN_OP_SRC, |
| 153 | output->shadow_surface, /* src */ |
| 154 | NULL /* mask */, |
| 155 | output->hw_surface, /* dest */ |
Derek Foreman | 561662b | 2015-03-17 13:22:38 -0500 | [diff] [blame] | 156 | transformed_rect.x1, /* src_x */ |
| 157 | transformed_rect.y1, /* src_y */ |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 158 | 0, 0, /* mask_x, mask_y */ |
Derek Foreman | 561662b | 2015-03-17 13:22:38 -0500 | [diff] [blame] | 159 | transformed_rect.x1, /* dest_x */ |
| 160 | transformed_rect.y1, /* dest_y */ |
| 161 | width, /* width */ |
| 162 | height /* height */); |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | /* Update the damage region. */ |
| 166 | pixman_region32_subtract(&ec->primary_plane.damage, |
| 167 | &ec->primary_plane.damage, damage); |
| 168 | |
| 169 | /* Schedule the end of the frame. We do not sync this to the frame |
| 170 | * buffer clock because users who want that should be using the DRM |
| 171 | * compositor. FBIO_WAITFORVSYNC blocks and FB_ACTIVATE_VBL requires |
| 172 | * panning, which is broken in most kernel drivers. |
| 173 | * |
| 174 | * Finish the frame synchronised to the specified refresh rate. The |
| 175 | * refresh rate is given in mHz and the interval in ms. */ |
| 176 | wl_event_source_timer_update(output->finish_frame_timer, |
| 177 | 1000000 / output->mode.refresh); |
| 178 | } |
| 179 | |
David Herrmann | 1edf44c | 2013-10-22 17:11:26 +0200 | [diff] [blame] | 180 | static int |
Adrian Negreanu | 4aa756d | 2013-09-06 15:16:09 +0300 | [diff] [blame] | 181 | fbdev_output_repaint(struct weston_output *base, pixman_region32_t *damage) |
| 182 | { |
| 183 | struct fbdev_output *output = to_fbdev_output(base); |
| 184 | struct fbdev_compositor *fbc = output->compositor; |
| 185 | struct weston_compositor *ec = & fbc->base; |
| 186 | |
| 187 | if (fbc->use_pixman) { |
| 188 | fbdev_output_repaint_pixman(base,damage); |
| 189 | } else { |
| 190 | ec->renderer->repaint_output(base, damage); |
| 191 | /* Update the damage region. */ |
| 192 | pixman_region32_subtract(&ec->primary_plane.damage, |
| 193 | &ec->primary_plane.damage, damage); |
| 194 | |
| 195 | wl_event_source_timer_update(output->finish_frame_timer, |
| 196 | 1000000 / output->mode.refresh); |
| 197 | } |
David Herrmann | 1edf44c | 2013-10-22 17:11:26 +0200 | [diff] [blame] | 198 | |
| 199 | return 0; |
Adrian Negreanu | 4aa756d | 2013-09-06 15:16:09 +0300 | [diff] [blame] | 200 | } |
| 201 | |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 202 | static int |
| 203 | finish_frame_handler(void *data) |
| 204 | { |
| 205 | struct fbdev_output *output = data; |
Pekka Paalanen | 363aa7b | 2014-12-17 16:20:40 +0200 | [diff] [blame] | 206 | struct timespec ts; |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 207 | |
Pekka Paalanen | 662f384 | 2015-03-18 12:17:26 +0200 | [diff] [blame] | 208 | weston_compositor_read_presentation_clock(output->base.compositor, &ts); |
Pekka Paalanen | 363aa7b | 2014-12-17 16:20:40 +0200 | [diff] [blame] | 209 | weston_output_finish_frame(&output->base, &ts, 0); |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 210 | |
| 211 | return 1; |
| 212 | } |
| 213 | |
| 214 | static pixman_format_code_t |
| 215 | calculate_pixman_format(struct fb_var_screeninfo *vinfo, |
| 216 | struct fb_fix_screeninfo *finfo) |
| 217 | { |
| 218 | /* Calculate the pixman format supported by the frame buffer from the |
| 219 | * buffer's metadata. Return 0 if no known pixman format is supported |
| 220 | * (since this has depth 0 it's guaranteed to not conflict with any |
| 221 | * actual pixman format). |
| 222 | * |
| 223 | * Documentation on the vinfo and finfo structures: |
| 224 | * http://www.mjmwired.net/kernel/Documentation/fb/api.txt |
| 225 | * |
| 226 | * TODO: Try a bit harder to support other formats, including setting |
| 227 | * the preferred format in the hardware. */ |
| 228 | int type; |
| 229 | |
| 230 | weston_log("Calculating pixman format from:\n" |
| 231 | STAMP_SPACE " - type: %i (aux: %i)\n" |
| 232 | STAMP_SPACE " - visual: %i\n" |
| 233 | STAMP_SPACE " - bpp: %i (grayscale: %i)\n" |
| 234 | STAMP_SPACE " - red: offset: %i, length: %i, MSB: %i\n" |
| 235 | STAMP_SPACE " - green: offset: %i, length: %i, MSB: %i\n" |
| 236 | STAMP_SPACE " - blue: offset: %i, length: %i, MSB: %i\n" |
| 237 | STAMP_SPACE " - transp: offset: %i, length: %i, MSB: %i\n", |
| 238 | finfo->type, finfo->type_aux, finfo->visual, |
| 239 | vinfo->bits_per_pixel, vinfo->grayscale, |
| 240 | vinfo->red.offset, vinfo->red.length, vinfo->red.msb_right, |
| 241 | vinfo->green.offset, vinfo->green.length, |
| 242 | vinfo->green.msb_right, |
| 243 | vinfo->blue.offset, vinfo->blue.length, |
| 244 | vinfo->blue.msb_right, |
| 245 | vinfo->transp.offset, vinfo->transp.length, |
| 246 | vinfo->transp.msb_right); |
| 247 | |
| 248 | /* We only handle packed formats at the moment. */ |
| 249 | if (finfo->type != FB_TYPE_PACKED_PIXELS) |
| 250 | return 0; |
| 251 | |
| 252 | /* We only handle true-colour frame buffers at the moment. */ |
Marc Chalain | ffbddff | 2013-09-03 16:47:43 +0200 | [diff] [blame] | 253 | switch(finfo->visual) { |
| 254 | case FB_VISUAL_TRUECOLOR: |
| 255 | case FB_VISUAL_DIRECTCOLOR: |
| 256 | if (vinfo->grayscale != 0) |
| 257 | return 0; |
| 258 | break; |
| 259 | default: |
| 260 | return 0; |
| 261 | } |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 262 | |
| 263 | /* We only support formats with MSBs on the left. */ |
| 264 | if (vinfo->red.msb_right != 0 || vinfo->green.msb_right != 0 || |
| 265 | vinfo->blue.msb_right != 0) |
| 266 | return 0; |
| 267 | |
| 268 | /* Work out the format type from the offsets. We only support RGBA and |
| 269 | * ARGB at the moment. */ |
| 270 | type = PIXMAN_TYPE_OTHER; |
| 271 | |
| 272 | if ((vinfo->transp.offset >= vinfo->red.offset || |
| 273 | vinfo->transp.length == 0) && |
| 274 | vinfo->red.offset >= vinfo->green.offset && |
| 275 | vinfo->green.offset >= vinfo->blue.offset) |
| 276 | type = PIXMAN_TYPE_ARGB; |
| 277 | else if (vinfo->red.offset >= vinfo->green.offset && |
| 278 | vinfo->green.offset >= vinfo->blue.offset && |
| 279 | vinfo->blue.offset >= vinfo->transp.offset) |
| 280 | type = PIXMAN_TYPE_RGBA; |
| 281 | |
| 282 | if (type == PIXMAN_TYPE_OTHER) |
| 283 | return 0; |
| 284 | |
| 285 | /* Build the format. */ |
| 286 | return PIXMAN_FORMAT(vinfo->bits_per_pixel, type, |
| 287 | vinfo->transp.length, |
| 288 | vinfo->red.length, |
| 289 | vinfo->green.length, |
| 290 | vinfo->blue.length); |
| 291 | } |
| 292 | |
| 293 | static int |
| 294 | calculate_refresh_rate(struct fb_var_screeninfo *vinfo) |
| 295 | { |
| 296 | uint64_t quot; |
| 297 | |
| 298 | /* Calculate monitor refresh rate. Default is 60 Hz. Units are mHz. */ |
| 299 | quot = (vinfo->upper_margin + vinfo->lower_margin + vinfo->yres); |
| 300 | quot *= (vinfo->left_margin + vinfo->right_margin + vinfo->xres); |
| 301 | quot *= vinfo->pixclock; |
| 302 | |
| 303 | if (quot > 0) { |
| 304 | uint64_t refresh_rate; |
| 305 | |
| 306 | refresh_rate = 1000000000000000LLU / quot; |
| 307 | if (refresh_rate > 200000) |
| 308 | refresh_rate = 200000; /* cap at 200 Hz */ |
| 309 | |
| 310 | return refresh_rate; |
| 311 | } |
| 312 | |
| 313 | return 60 * 1000; /* default to 60 Hz */ |
| 314 | } |
| 315 | |
| 316 | static int |
| 317 | fbdev_query_screen_info(struct fbdev_output *output, int fd, |
| 318 | struct fbdev_screeninfo *info) |
| 319 | { |
| 320 | struct fb_var_screeninfo varinfo; |
| 321 | struct fb_fix_screeninfo fixinfo; |
| 322 | |
| 323 | /* Probe the device for screen information. */ |
| 324 | if (ioctl(fd, FBIOGET_FSCREENINFO, &fixinfo) < 0 || |
| 325 | ioctl(fd, FBIOGET_VSCREENINFO, &varinfo) < 0) { |
| 326 | return -1; |
| 327 | } |
| 328 | |
| 329 | /* Store the pertinent data. */ |
| 330 | info->x_resolution = varinfo.xres; |
| 331 | info->y_resolution = varinfo.yres; |
| 332 | info->width_mm = varinfo.width; |
| 333 | info->height_mm = varinfo.height; |
| 334 | info->bits_per_pixel = varinfo.bits_per_pixel; |
| 335 | |
| 336 | info->buffer_length = fixinfo.smem_len; |
| 337 | info->line_length = fixinfo.line_length; |
| 338 | strncpy(info->id, fixinfo.id, sizeof(info->id) / sizeof(*info->id)); |
| 339 | |
| 340 | info->pixel_format = calculate_pixman_format(&varinfo, &fixinfo); |
| 341 | info->refresh_rate = calculate_refresh_rate(&varinfo); |
| 342 | |
| 343 | if (info->pixel_format == 0) { |
| 344 | weston_log("Frame buffer uses an unsupported format.\n"); |
| 345 | return -1; |
| 346 | } |
| 347 | |
| 348 | return 1; |
| 349 | } |
| 350 | |
| 351 | static int |
| 352 | fbdev_set_screen_info(struct fbdev_output *output, int fd, |
| 353 | struct fbdev_screeninfo *info) |
| 354 | { |
| 355 | struct fb_var_screeninfo varinfo; |
| 356 | |
| 357 | /* Grab the current screen information. */ |
| 358 | if (ioctl(fd, FBIOGET_VSCREENINFO, &varinfo) < 0) { |
| 359 | return -1; |
| 360 | } |
| 361 | |
| 362 | /* Update the information. */ |
| 363 | varinfo.xres = info->x_resolution; |
| 364 | varinfo.yres = info->y_resolution; |
| 365 | varinfo.width = info->width_mm; |
| 366 | varinfo.height = info->height_mm; |
| 367 | varinfo.bits_per_pixel = info->bits_per_pixel; |
| 368 | |
| 369 | /* Try to set up an ARGB (x8r8g8b8) pixel format. */ |
| 370 | varinfo.grayscale = 0; |
| 371 | varinfo.transp.offset = 24; |
| 372 | varinfo.transp.length = 0; |
| 373 | varinfo.transp.msb_right = 0; |
| 374 | varinfo.red.offset = 16; |
| 375 | varinfo.red.length = 8; |
| 376 | varinfo.red.msb_right = 0; |
| 377 | varinfo.green.offset = 8; |
| 378 | varinfo.green.length = 8; |
| 379 | varinfo.green.msb_right = 0; |
| 380 | varinfo.blue.offset = 0; |
| 381 | varinfo.blue.length = 8; |
| 382 | varinfo.blue.msb_right = 0; |
| 383 | |
| 384 | /* Set the device's screen information. */ |
| 385 | if (ioctl(fd, FBIOPUT_VSCREENINFO, &varinfo) < 0) { |
| 386 | return -1; |
| 387 | } |
| 388 | |
| 389 | return 1; |
| 390 | } |
| 391 | |
| 392 | static void fbdev_frame_buffer_destroy(struct fbdev_output *output); |
| 393 | |
| 394 | /* Returns an FD for the frame buffer device. */ |
| 395 | static int |
| 396 | fbdev_frame_buffer_open(struct fbdev_output *output, const char *fb_dev, |
| 397 | struct fbdev_screeninfo *screen_info) |
| 398 | { |
| 399 | int fd = -1; |
| 400 | |
| 401 | weston_log("Opening fbdev frame buffer.\n"); |
| 402 | |
| 403 | /* Open the frame buffer device. */ |
| 404 | fd = open(fb_dev, O_RDWR | O_CLOEXEC); |
| 405 | if (fd < 0) { |
| 406 | weston_log("Failed to open frame buffer device ‘%s’: %s\n", |
| 407 | fb_dev, strerror(errno)); |
| 408 | return -1; |
| 409 | } |
| 410 | |
| 411 | /* Grab the screen info. */ |
| 412 | if (fbdev_query_screen_info(output, fd, screen_info) < 0) { |
| 413 | weston_log("Failed to get frame buffer info: %s\n", |
| 414 | strerror(errno)); |
| 415 | |
| 416 | close(fd); |
| 417 | return -1; |
| 418 | } |
| 419 | |
| 420 | return fd; |
| 421 | } |
| 422 | |
| 423 | /* Closes the FD on success or failure. */ |
| 424 | static int |
| 425 | fbdev_frame_buffer_map(struct fbdev_output *output, int fd) |
| 426 | { |
| 427 | int retval = -1; |
| 428 | |
| 429 | weston_log("Mapping fbdev frame buffer.\n"); |
| 430 | |
| 431 | /* Map the frame buffer. Write-only mode, since we don't want to read |
| 432 | * anything back (because it's slow). */ |
| 433 | output->fb = mmap(NULL, output->fb_info.buffer_length, |
| 434 | PROT_WRITE, MAP_SHARED, fd, 0); |
| 435 | if (output->fb == MAP_FAILED) { |
| 436 | weston_log("Failed to mmap frame buffer: %s\n", |
| 437 | strerror(errno)); |
| 438 | goto out_close; |
| 439 | } |
| 440 | |
| 441 | /* Create a pixman image to wrap the memory mapped frame buffer. */ |
| 442 | output->hw_surface = |
| 443 | pixman_image_create_bits(output->fb_info.pixel_format, |
| 444 | output->fb_info.x_resolution, |
| 445 | output->fb_info.y_resolution, |
| 446 | output->fb, |
| 447 | output->fb_info.line_length); |
| 448 | if (output->hw_surface == NULL) { |
| 449 | weston_log("Failed to create surface for frame buffer.\n"); |
| 450 | goto out_unmap; |
| 451 | } |
| 452 | |
| 453 | /* Success! */ |
| 454 | retval = 0; |
| 455 | |
| 456 | out_unmap: |
| 457 | if (retval != 0 && output->fb != NULL) |
| 458 | fbdev_frame_buffer_destroy(output); |
| 459 | |
| 460 | out_close: |
| 461 | if (fd >= 0) |
| 462 | close(fd); |
| 463 | |
| 464 | return retval; |
| 465 | } |
| 466 | |
| 467 | static void |
| 468 | fbdev_frame_buffer_destroy(struct fbdev_output *output) |
| 469 | { |
| 470 | weston_log("Destroying fbdev frame buffer.\n"); |
| 471 | |
| 472 | if (munmap(output->fb, output->fb_info.buffer_length) < 0) |
| 473 | weston_log("Failed to munmap frame buffer: %s\n", |
| 474 | strerror(errno)); |
| 475 | |
| 476 | output->fb = NULL; |
| 477 | } |
| 478 | |
| 479 | static void fbdev_output_destroy(struct weston_output *base); |
| 480 | static void fbdev_output_disable(struct weston_output *base); |
| 481 | |
| 482 | static int |
| 483 | fbdev_output_create(struct fbdev_compositor *compositor, |
| 484 | const char *device) |
| 485 | { |
| 486 | struct fbdev_output *output; |
Derek Foreman | 44ed70b | 2015-03-17 13:22:37 -0500 | [diff] [blame] | 487 | struct weston_config_section *section; |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 488 | int fb_fd; |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 489 | int width, height; |
| 490 | unsigned int bytes_per_pixel; |
| 491 | struct wl_event_loop *loop; |
Derek Foreman | 44ed70b | 2015-03-17 13:22:37 -0500 | [diff] [blame] | 492 | uint32_t config_transform; |
| 493 | char *s; |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 494 | |
| 495 | weston_log("Creating fbdev output.\n"); |
| 496 | |
Bryce Harrington | de16d89 | 2014-11-20 22:21:57 -0800 | [diff] [blame] | 497 | output = zalloc(sizeof *output); |
| 498 | if (output == NULL) |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 499 | return -1; |
| 500 | |
| 501 | output->compositor = compositor; |
| 502 | output->device = device; |
| 503 | |
| 504 | /* Create the frame buffer. */ |
| 505 | fb_fd = fbdev_frame_buffer_open(output, device, &output->fb_info); |
| 506 | if (fb_fd < 0) { |
| 507 | weston_log("Creating frame buffer failed.\n"); |
| 508 | goto out_free; |
| 509 | } |
Adrian Negreanu | 4aa756d | 2013-09-06 15:16:09 +0300 | [diff] [blame] | 510 | if (compositor->use_pixman) { |
| 511 | if (fbdev_frame_buffer_map(output, fb_fd) < 0) { |
| 512 | weston_log("Mapping frame buffer failed.\n"); |
| 513 | goto out_free; |
| 514 | } |
Kristian Høgsberg | 8ac6a2d | 2013-10-09 09:59:06 -0700 | [diff] [blame] | 515 | } else { |
| 516 | close(fb_fd); |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 517 | } |
| 518 | |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 519 | output->base.start_repaint_loop = fbdev_output_start_repaint_loop; |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 520 | output->base.repaint = fbdev_output_repaint; |
| 521 | output->base.destroy = fbdev_output_destroy; |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 522 | |
| 523 | /* only one static mode in list */ |
| 524 | output->mode.flags = |
| 525 | WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED; |
| 526 | output->mode.width = output->fb_info.x_resolution; |
| 527 | output->mode.height = output->fb_info.y_resolution; |
| 528 | output->mode.refresh = output->fb_info.refresh_rate; |
| 529 | wl_list_init(&output->base.mode_list); |
| 530 | wl_list_insert(&output->base.mode_list, &output->mode.link); |
| 531 | |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 532 | output->base.current_mode = &output->mode; |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 533 | output->base.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN; |
| 534 | output->base.make = "unknown"; |
| 535 | output->base.model = output->fb_info.id; |
Derek Foreman | e516c3b | 2015-03-17 13:22:34 -0500 | [diff] [blame] | 536 | output->base.name = strdup("fbdev"); |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 537 | |
Derek Foreman | 44ed70b | 2015-03-17 13:22:37 -0500 | [diff] [blame] | 538 | section = weston_config_get_section(compositor->base.config, |
| 539 | "output", "name", |
| 540 | output->base.name); |
| 541 | weston_config_section_get_string(section, "transform", &s, "normal"); |
| 542 | if (weston_parse_transform(s, &config_transform) < 0) |
| 543 | weston_log("Invalid transform \"%s\" for output %s\n", |
| 544 | s, output->base.name); |
| 545 | free(s); |
| 546 | |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 547 | weston_output_init(&output->base, &compositor->base, |
| 548 | 0, 0, output->fb_info.width_mm, |
| 549 | output->fb_info.height_mm, |
Derek Foreman | 44ed70b | 2015-03-17 13:22:37 -0500 | [diff] [blame] | 550 | config_transform, |
Alexander Larsson | 4ea9552 | 2013-05-22 14:41:37 +0200 | [diff] [blame] | 551 | 1); |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 552 | |
Derek Foreman | 561662b | 2015-03-17 13:22:38 -0500 | [diff] [blame] | 553 | width = output->mode.width; |
| 554 | height = output->mode.height; |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 555 | bytes_per_pixel = output->fb_info.bits_per_pixel / 8; |
| 556 | |
| 557 | output->shadow_buf = malloc(width * height * bytes_per_pixel); |
| 558 | output->shadow_surface = |
| 559 | pixman_image_create_bits(output->fb_info.pixel_format, |
Derek Foreman | 561662b | 2015-03-17 13:22:38 -0500 | [diff] [blame] | 560 | width, height, |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 561 | output->shadow_buf, |
Derek Foreman | 561662b | 2015-03-17 13:22:38 -0500 | [diff] [blame] | 562 | width * bytes_per_pixel); |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 563 | if (output->shadow_buf == NULL || output->shadow_surface == NULL) { |
| 564 | weston_log("Failed to create surface for frame buffer.\n"); |
| 565 | goto out_hw_surface; |
| 566 | } |
| 567 | |
Adrian Negreanu | 4aa756d | 2013-09-06 15:16:09 +0300 | [diff] [blame] | 568 | if (compositor->use_pixman) { |
| 569 | if (pixman_renderer_output_create(&output->base) < 0) |
| 570 | goto out_shadow_surface; |
| 571 | } else { |
| 572 | setenv("HYBRIS_EGLPLATFORM", "wayland", 1); |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 573 | if (gl_renderer->output_create(&output->base, |
Jonny Lamb | 671148f | 2015-03-20 15:26:52 +0100 | [diff] [blame] | 574 | (EGLNativeWindowType)NULL, NULL, |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 575 | gl_renderer->opaque_attribs, |
Derek Foreman | e76f185 | 2015-05-15 12:12:39 -0500 | [diff] [blame] | 576 | NULL, 0) < 0) { |
Adrian Negreanu | 4aa756d | 2013-09-06 15:16:09 +0300 | [diff] [blame] | 577 | weston_log("gl_renderer_output_create failed.\n"); |
| 578 | goto out_shadow_surface; |
| 579 | } |
| 580 | } |
| 581 | |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 582 | |
| 583 | loop = wl_display_get_event_loop(compositor->base.wl_display); |
| 584 | output->finish_frame_timer = |
| 585 | wl_event_loop_add_timer(loop, finish_frame_handler, output); |
| 586 | |
Giulio Camuffo | b114715 | 2015-05-06 21:41:57 +0300 | [diff] [blame] | 587 | weston_compositor_add_output(&compositor->base, &output->base); |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 588 | |
| 589 | weston_log("fbdev output %d×%d px\n", |
| 590 | output->mode.width, output->mode.height); |
| 591 | weston_log_continue(STAMP_SPACE "guessing %d Hz and 96 dpi\n", |
| 592 | output->mode.refresh / 1000); |
| 593 | |
| 594 | return 0; |
| 595 | |
| 596 | out_shadow_surface: |
| 597 | pixman_image_unref(output->shadow_surface); |
| 598 | output->shadow_surface = NULL; |
| 599 | out_hw_surface: |
| 600 | free(output->shadow_buf); |
| 601 | pixman_image_unref(output->hw_surface); |
| 602 | output->hw_surface = NULL; |
| 603 | weston_output_destroy(&output->base); |
| 604 | fbdev_frame_buffer_destroy(output); |
| 605 | out_free: |
| 606 | free(output); |
| 607 | |
| 608 | return -1; |
| 609 | } |
| 610 | |
| 611 | static void |
| 612 | fbdev_output_destroy(struct weston_output *base) |
| 613 | { |
| 614 | struct fbdev_output *output = to_fbdev_output(base); |
Adrian Negreanu | 4aa756d | 2013-09-06 15:16:09 +0300 | [diff] [blame] | 615 | struct fbdev_compositor *compositor = output->compositor; |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 616 | |
| 617 | weston_log("Destroying fbdev output.\n"); |
| 618 | |
| 619 | /* Close the frame buffer. */ |
| 620 | fbdev_output_disable(base); |
| 621 | |
Adrian Negreanu | 4aa756d | 2013-09-06 15:16:09 +0300 | [diff] [blame] | 622 | if (compositor->use_pixman) { |
| 623 | if (base->renderer_state != NULL) |
| 624 | pixman_renderer_output_destroy(base); |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 625 | |
Adrian Negreanu | 4aa756d | 2013-09-06 15:16:09 +0300 | [diff] [blame] | 626 | if (output->shadow_surface != NULL) { |
| 627 | pixman_image_unref(output->shadow_surface); |
| 628 | output->shadow_surface = NULL; |
| 629 | } |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 630 | |
Adrian Negreanu | 4aa756d | 2013-09-06 15:16:09 +0300 | [diff] [blame] | 631 | if (output->shadow_buf != NULL) { |
| 632 | free(output->shadow_buf); |
| 633 | output->shadow_buf = NULL; |
| 634 | } |
| 635 | } else { |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 636 | gl_renderer->output_destroy(base); |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | /* Remove the output. */ |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 640 | weston_output_destroy(&output->base); |
| 641 | |
| 642 | free(output); |
| 643 | } |
| 644 | |
| 645 | /* strcmp()-style return values. */ |
| 646 | static int |
| 647 | compare_screen_info (const struct fbdev_screeninfo *a, |
| 648 | const struct fbdev_screeninfo *b) |
| 649 | { |
| 650 | if (a->x_resolution == b->x_resolution && |
| 651 | a->y_resolution == b->y_resolution && |
| 652 | a->width_mm == b->width_mm && |
| 653 | a->height_mm == b->height_mm && |
| 654 | a->bits_per_pixel == b->bits_per_pixel && |
| 655 | a->pixel_format == b->pixel_format && |
| 656 | a->refresh_rate == b->refresh_rate) |
| 657 | return 0; |
| 658 | |
| 659 | return 1; |
| 660 | } |
| 661 | |
| 662 | static int |
| 663 | fbdev_output_reenable(struct fbdev_compositor *compositor, |
| 664 | struct weston_output *base) |
| 665 | { |
| 666 | struct fbdev_output *output = to_fbdev_output(base); |
| 667 | struct fbdev_screeninfo new_screen_info; |
| 668 | int fb_fd; |
Rob Bradford | f8ef42f | 2013-07-26 16:29:38 +0100 | [diff] [blame] | 669 | const char *device; |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 670 | |
| 671 | weston_log("Re-enabling fbdev output.\n"); |
| 672 | |
| 673 | /* Create the frame buffer. */ |
| 674 | fb_fd = fbdev_frame_buffer_open(output, output->device, |
| 675 | &new_screen_info); |
| 676 | if (fb_fd < 0) { |
| 677 | weston_log("Creating frame buffer failed.\n"); |
| 678 | goto err; |
| 679 | } |
| 680 | |
| 681 | /* Check whether the frame buffer details have changed since we were |
| 682 | * disabled. */ |
| 683 | if (compare_screen_info (&output->fb_info, &new_screen_info) != 0) { |
| 684 | /* Perform a mode-set to restore the old mode. */ |
| 685 | if (fbdev_set_screen_info(output, fb_fd, |
| 686 | &output->fb_info) < 0) { |
| 687 | weston_log("Failed to restore mode settings. " |
| 688 | "Attempting to re-open output anyway.\n"); |
| 689 | } |
| 690 | |
Rob Bradford | 581b3fd | 2013-07-26 16:29:39 +0100 | [diff] [blame] | 691 | close(fb_fd); |
| 692 | |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 693 | /* Remove and re-add the output so that resources depending on |
| 694 | * the frame buffer X/Y resolution (such as the shadow buffer) |
| 695 | * are re-initialised. */ |
Rob Bradford | f8ef42f | 2013-07-26 16:29:38 +0100 | [diff] [blame] | 696 | device = output->device; |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 697 | fbdev_output_destroy(base); |
Rob Bradford | f8ef42f | 2013-07-26 16:29:38 +0100 | [diff] [blame] | 698 | fbdev_output_create(compositor, device); |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 699 | |
| 700 | return 0; |
| 701 | } |
| 702 | |
| 703 | /* Map the device if it has the same details as before. */ |
Adrian Negreanu | 4aa756d | 2013-09-06 15:16:09 +0300 | [diff] [blame] | 704 | if (compositor->use_pixman) { |
| 705 | if (fbdev_frame_buffer_map(output, fb_fd) < 0) { |
| 706 | weston_log("Mapping frame buffer failed.\n"); |
| 707 | goto err; |
| 708 | } |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | return 0; |
| 712 | |
| 713 | err: |
| 714 | return -1; |
| 715 | } |
| 716 | |
| 717 | /* NOTE: This leaves output->fb_info populated, caching data so that if |
| 718 | * fbdev_output_reenable() is called again, it can determine whether a mode-set |
| 719 | * is needed. */ |
| 720 | static void |
| 721 | fbdev_output_disable(struct weston_output *base) |
| 722 | { |
| 723 | struct fbdev_output *output = to_fbdev_output(base); |
Adrian Negreanu | 4aa756d | 2013-09-06 15:16:09 +0300 | [diff] [blame] | 724 | struct fbdev_compositor *compositor = output->compositor; |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 725 | |
| 726 | weston_log("Disabling fbdev output.\n"); |
| 727 | |
Adrian Negreanu | 4aa756d | 2013-09-06 15:16:09 +0300 | [diff] [blame] | 728 | if ( ! compositor->use_pixman) return; |
| 729 | |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 730 | if (output->hw_surface != NULL) { |
| 731 | pixman_image_unref(output->hw_surface); |
| 732 | output->hw_surface = NULL; |
| 733 | } |
| 734 | |
| 735 | fbdev_frame_buffer_destroy(output); |
| 736 | } |
| 737 | |
| 738 | static void |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 739 | fbdev_compositor_destroy(struct weston_compositor *base) |
| 740 | { |
| 741 | struct fbdev_compositor *compositor = to_fbdev_compositor(base); |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 742 | |
Rob Bradford | d355b80 | 2013-05-31 18:09:55 +0100 | [diff] [blame] | 743 | udev_input_destroy(&compositor->input); |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 744 | |
| 745 | /* Destroy the output. */ |
| 746 | weston_compositor_shutdown(&compositor->base); |
| 747 | |
| 748 | /* Chain up. */ |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 749 | weston_launcher_destroy(compositor->base.launcher); |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 750 | |
| 751 | free(compositor); |
| 752 | } |
| 753 | |
| 754 | static void |
Kristian Høgsberg | 61741a2 | 2013-09-17 16:02:57 -0700 | [diff] [blame] | 755 | session_notify(struct wl_listener *listener, void *data) |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 756 | { |
Kristian Høgsberg | 61741a2 | 2013-09-17 16:02:57 -0700 | [diff] [blame] | 757 | struct fbdev_compositor *compositor = data; |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 758 | struct weston_output *output; |
| 759 | |
Kristian Høgsberg | 61741a2 | 2013-09-17 16:02:57 -0700 | [diff] [blame] | 760 | if (compositor->base.session_active) { |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 761 | weston_log("entering VT\n"); |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 762 | compositor->base.state = compositor->prev_state; |
| 763 | |
| 764 | wl_list_for_each(output, &compositor->base.output_list, link) { |
| 765 | fbdev_output_reenable(compositor, output); |
| 766 | } |
| 767 | |
| 768 | weston_compositor_damage_all(&compositor->base); |
| 769 | |
Jonas Ådahl | 0feb32e | 2014-03-12 22:08:41 +0100 | [diff] [blame] | 770 | udev_input_enable(&compositor->input); |
Kristian Høgsberg | 61741a2 | 2013-09-17 16:02:57 -0700 | [diff] [blame] | 771 | } else { |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 772 | weston_log("leaving VT\n"); |
Rob Bradford | d355b80 | 2013-05-31 18:09:55 +0100 | [diff] [blame] | 773 | udev_input_disable(&compositor->input); |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 774 | |
| 775 | wl_list_for_each(output, &compositor->base.output_list, link) { |
| 776 | fbdev_output_disable(output); |
| 777 | } |
| 778 | |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 779 | compositor->prev_state = compositor->base.state; |
Philipp Brüschweiler | 57edf7f | 2013-03-29 13:01:56 +0100 | [diff] [blame] | 780 | weston_compositor_offscreen(&compositor->base); |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 781 | |
| 782 | /* If we have a repaint scheduled (from the idle handler), make |
| 783 | * sure we cancel that so we don't try to pageflip when we're |
Philipp Brüschweiler | 57edf7f | 2013-03-29 13:01:56 +0100 | [diff] [blame] | 784 | * vt switched away. The OFFSCREEN state will prevent |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 785 | * further attemps at repainting. When we switch |
| 786 | * back, we schedule a repaint, which will process |
| 787 | * pending frame callbacks. */ |
| 788 | |
| 789 | wl_list_for_each(output, |
| 790 | &compositor->base.output_list, link) { |
| 791 | output->repaint_needed = 0; |
| 792 | } |
nerdopolis | 3894670 | 2014-12-03 15:53:03 +0000 | [diff] [blame] | 793 | } |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | static void |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 797 | fbdev_restore(struct weston_compositor *compositor) |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 798 | { |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 799 | weston_launcher_restore(compositor->launcher); |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | static void |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 803 | switch_vt_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data) |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 804 | { |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 805 | struct weston_compositor *compositor = data; |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 806 | |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 807 | weston_launcher_activate_vt(compositor->launcher, key - KEY_F1 + 1); |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 808 | } |
| 809 | |
| 810 | static struct weston_compositor * |
Kristian Høgsberg | 4172f66 | 2013-02-20 15:27:49 -0500 | [diff] [blame] | 811 | fbdev_compositor_create(struct wl_display *display, int *argc, char *argv[], |
Kristian Høgsberg | 14e438c | 2013-05-26 21:48:14 -0400 | [diff] [blame] | 812 | struct weston_config *config, |
| 813 | struct fbdev_parameters *param) |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 814 | { |
| 815 | struct fbdev_compositor *compositor; |
Rob Bradford | 2387fde | 2013-05-31 18:09:57 +0100 | [diff] [blame] | 816 | const char *seat_id = default_seat; |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 817 | uint32_t key; |
| 818 | |
| 819 | weston_log("initializing fbdev backend\n"); |
| 820 | |
Bryce Harrington | de16d89 | 2014-11-20 22:21:57 -0800 | [diff] [blame] | 821 | compositor = zalloc(sizeof *compositor); |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 822 | if (compositor == NULL) |
| 823 | return NULL; |
| 824 | |
| 825 | if (weston_compositor_init(&compositor->base, display, argc, argv, |
Kristian Høgsberg | 14e438c | 2013-05-26 21:48:14 -0400 | [diff] [blame] | 826 | config) < 0) |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 827 | goto out_free; |
| 828 | |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame] | 829 | if (weston_compositor_set_presentation_clock_software( |
| 830 | &compositor->base) < 0) |
| 831 | goto out_compositor; |
| 832 | |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 833 | compositor->udev = udev_new(); |
| 834 | if (compositor->udev == NULL) { |
| 835 | weston_log("Failed to initialize udev context.\n"); |
| 836 | goto out_compositor; |
| 837 | } |
| 838 | |
| 839 | /* Set up the TTY. */ |
Kristian Høgsberg | 61741a2 | 2013-09-17 16:02:57 -0700 | [diff] [blame] | 840 | compositor->session_listener.notify = session_notify; |
| 841 | wl_signal_add(&compositor->base.session_signal, |
| 842 | &compositor->session_listener); |
David Herrmann | 2ecb84a | 2014-12-30 14:33:22 +0100 | [diff] [blame] | 843 | compositor->base.launcher = weston_launcher_connect(&compositor->base, |
| 844 | param->tty, "seat0", |
| 845 | false); |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 846 | if (!compositor->base.launcher) { |
David Herrmann | 1641d14 | 2013-10-15 14:29:57 +0200 | [diff] [blame] | 847 | weston_log("fatal: fbdev backend should be run " |
| 848 | "using weston-launch binary or as root\n"); |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 849 | goto out_udev; |
| 850 | } |
| 851 | |
| 852 | compositor->base.destroy = fbdev_compositor_destroy; |
| 853 | compositor->base.restore = fbdev_restore; |
| 854 | |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 855 | compositor->prev_state = WESTON_COMPOSITOR_ACTIVE; |
Adrian Negreanu | 4aa756d | 2013-09-06 15:16:09 +0300 | [diff] [blame] | 856 | compositor->use_pixman = !param->use_gl; |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 857 | |
| 858 | for (key = KEY_F1; key < KEY_F9; key++) |
| 859 | weston_compositor_add_key_binding(&compositor->base, key, |
| 860 | MODIFIER_CTRL | MODIFIER_ALT, |
| 861 | switch_vt_binding, |
| 862 | compositor); |
Adrian Negreanu | 4aa756d | 2013-09-06 15:16:09 +0300 | [diff] [blame] | 863 | if (compositor->use_pixman) { |
| 864 | if (pixman_renderer_init(&compositor->base) < 0) |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 865 | goto out_launcher; |
Adrian Negreanu | 4aa756d | 2013-09-06 15:16:09 +0300 | [diff] [blame] | 866 | } else { |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 867 | gl_renderer = weston_load_module("gl-renderer.so", |
| 868 | "gl_renderer_interface"); |
| 869 | if (!gl_renderer) { |
| 870 | weston_log("could not load gl renderer\n"); |
| 871 | goto out_launcher; |
| 872 | } |
| 873 | |
Jonny Lamb | 74eed31 | 2015-03-24 13:12:04 +0100 | [diff] [blame] | 874 | if (gl_renderer->create(&compositor->base, NO_EGL_PLATFORM, |
| 875 | EGL_DEFAULT_DISPLAY, |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 876 | gl_renderer->opaque_attribs, |
Derek Foreman | e76f185 | 2015-05-15 12:12:39 -0500 | [diff] [blame] | 877 | NULL, 0) < 0) { |
Adrian Negreanu | 4aa756d | 2013-09-06 15:16:09 +0300 | [diff] [blame] | 878 | weston_log("gl_renderer_create failed.\n"); |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 879 | goto out_launcher; |
Adrian Negreanu | 4aa756d | 2013-09-06 15:16:09 +0300 | [diff] [blame] | 880 | } |
| 881 | } |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 882 | |
| 883 | if (fbdev_output_create(compositor, param->device) < 0) |
| 884 | goto out_pixman; |
| 885 | |
Rob Bradford | 2387fde | 2013-05-31 18:09:57 +0100 | [diff] [blame] | 886 | udev_input_init(&compositor->input, &compositor->base, compositor->udev, seat_id); |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 887 | |
| 888 | return &compositor->base; |
| 889 | |
| 890 | out_pixman: |
| 891 | compositor->base.renderer->destroy(&compositor->base); |
| 892 | |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 893 | out_launcher: |
| 894 | weston_launcher_destroy(compositor->base.launcher); |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 895 | |
| 896 | out_udev: |
| 897 | udev_unref(compositor->udev); |
| 898 | |
| 899 | out_compositor: |
| 900 | weston_compositor_shutdown(&compositor->base); |
| 901 | |
| 902 | out_free: |
| 903 | free(compositor); |
| 904 | |
| 905 | return NULL; |
| 906 | } |
| 907 | |
| 908 | WL_EXPORT struct weston_compositor * |
Kristian Høgsberg | 4172f66 | 2013-02-20 15:27:49 -0500 | [diff] [blame] | 909 | backend_init(struct wl_display *display, int *argc, char *argv[], |
Kristian Høgsberg | 14e438c | 2013-05-26 21:48:14 -0400 | [diff] [blame] | 910 | struct weston_config *config) |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 911 | { |
| 912 | /* TODO: Ideally, available frame buffers should be enumerated using |
| 913 | * udev, rather than passing a device node in as a parameter. */ |
| 914 | struct fbdev_parameters param = { |
| 915 | .tty = 0, /* default to current tty */ |
| 916 | .device = "/dev/fb0", /* default frame buffer */ |
Adrian Negreanu | 4aa756d | 2013-09-06 15:16:09 +0300 | [diff] [blame] | 917 | .use_gl = 0, |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 918 | }; |
| 919 | |
| 920 | const struct weston_option fbdev_options[] = { |
| 921 | { WESTON_OPTION_INTEGER, "tty", 0, ¶m.tty }, |
| 922 | { WESTON_OPTION_STRING, "device", 0, ¶m.device }, |
Adrian Negreanu | 4aa756d | 2013-09-06 15:16:09 +0300 | [diff] [blame] | 923 | { WESTON_OPTION_BOOLEAN, "use-gl", 0, ¶m.use_gl }, |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 924 | }; |
| 925 | |
| 926 | parse_options(fbdev_options, ARRAY_LENGTH(fbdev_options), argc, argv); |
| 927 | |
Kristian Høgsberg | 14e438c | 2013-05-26 21:48:14 -0400 | [diff] [blame] | 928 | return fbdev_compositor_create(display, argc, argv, config, ¶m); |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 929 | } |