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