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