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