Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1 | /* |
Kristian Høgsberg | 96aa7da | 2011-09-15 15:43:14 -0400 | [diff] [blame] | 2 | * Copyright © 2008-2011 Kristian Høgsberg |
| 3 | * Copyright © 2011 Intel Corporation |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 4 | * |
Kristian Høgsberg | 96aa7da | 2011-09-15 15:43:14 -0400 | [diff] [blame] | 5 | * Permission to use, copy, modify, distribute, and sell this software and |
| 6 | * its documentation for any purpose is hereby granted without fee, provided |
| 7 | * that the above copyright notice appear in all copies and that both that |
| 8 | * copyright notice and this permission notice appear in supporting |
| 9 | * documentation, and that the name of the copyright holders not be used in |
| 10 | * advertising or publicity pertaining to distribution of the software |
| 11 | * without specific, written prior permission. The copyright holders make |
| 12 | * no representations about the suitability of this software for any |
| 13 | * purpose. It is provided "as is" without express or implied warranty. |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 14 | * |
Kristian Høgsberg | 96aa7da | 2011-09-15 15:43:14 -0400 | [diff] [blame] | 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS |
| 16 | * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 18 | * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER |
| 19 | * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF |
| 20 | * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 21 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 22 | */ |
| 23 | |
Kristian Høgsberg | 0b9334a | 2011-04-12 11:34:32 -0400 | [diff] [blame] | 24 | #define _GNU_SOURCE |
| 25 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 26 | #include <errno.h> |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
| 29 | #include <fcntl.h> |
| 30 | #include <unistd.h> |
Kristian Høgsberg | 5d1c0c5 | 2012-04-10 00:11:50 -0400 | [diff] [blame] | 31 | #include <linux/input.h> |
Ander Conselvan de Oliveira | fd1f4c6 | 2012-06-26 17:09:14 +0300 | [diff] [blame] | 32 | #include <assert.h> |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 33 | |
Benjamin Franzke | c649a92 | 2011-03-02 11:56:04 +0100 | [diff] [blame] | 34 | #include <xf86drm.h> |
| 35 | #include <xf86drmMode.h> |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 36 | #include <drm_fourcc.h> |
Benjamin Franzke | c649a92 | 2011-03-02 11:56:04 +0100 | [diff] [blame] | 37 | |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 38 | #include <gbm.h> |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 39 | #include <libbacklight.h> |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 40 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 41 | #include "compositor.h" |
Tiago Vignatti | ce03ec3 | 2011-12-19 01:14:03 +0200 | [diff] [blame] | 42 | #include "evdev.h" |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 43 | #include "launcher-util.h" |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 44 | #include "log.h" |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 45 | |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 46 | static int option_current_mode = 0; |
Scott Moreau | 8ab5d45 | 2012-07-30 19:51:08 -0600 | [diff] [blame] | 47 | static char *output_name; |
| 48 | static char *output_mode; |
| 49 | static struct wl_list configured_output_list; |
| 50 | |
| 51 | enum output_config { |
| 52 | OUTPUT_CONFIG_INVALID = 0, |
| 53 | OUTPUT_CONFIG_OFF, |
| 54 | OUTPUT_CONFIG_PREFERRED, |
| 55 | OUTPUT_CONFIG_CURRENT, |
| 56 | OUTPUT_CONFIG_MODE |
| 57 | }; |
| 58 | |
| 59 | struct drm_configured_output { |
| 60 | char *name; |
| 61 | char *mode; |
| 62 | int32_t width, height; |
| 63 | enum output_config config; |
| 64 | struct wl_list link; |
| 65 | }; |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 66 | |
Kristian Høgsberg | d553bfc | 2012-06-18 22:37:35 -0400 | [diff] [blame] | 67 | enum { |
Kristian Høgsberg | 6143f7d | 2012-07-14 00:31:32 -0400 | [diff] [blame] | 68 | WESTON_PLANE_DRM_CURSOR = 0x100, |
| 69 | WESTON_PLANE_DRM_PLANE = 0x101, |
| 70 | WESTON_PLANE_DRM_FB = 0x102 |
Kristian Høgsberg | d553bfc | 2012-06-18 22:37:35 -0400 | [diff] [blame] | 71 | }; |
| 72 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 73 | struct drm_compositor { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 74 | struct weston_compositor base; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 75 | |
| 76 | struct udev *udev; |
| 77 | struct wl_event_source *drm_source; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 78 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 79 | struct udev_monitor *udev_monitor; |
| 80 | struct wl_event_source *udev_drm_source; |
| 81 | |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 82 | struct { |
David Herrmann | d7488c2 | 2012-03-11 20:05:21 +0100 | [diff] [blame] | 83 | int id; |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 84 | int fd; |
| 85 | } drm; |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 86 | struct gbm_device *gbm; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 87 | uint32_t *crtcs; |
| 88 | int num_crtcs; |
Marty Jack | 13d9db2 | 2011-02-09 19:01:42 -0500 | [diff] [blame] | 89 | uint32_t crtc_allocator; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 90 | uint32_t connector_allocator; |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 91 | struct tty *tty; |
Pekka Paalanen | bce2d3f | 2011-12-02 13:07:27 +0200 | [diff] [blame] | 92 | |
Kristian Høgsberg | b5ef591 | 2012-03-28 22:53:49 -0400 | [diff] [blame] | 93 | struct gbm_surface *dummy_surface; |
| 94 | EGLSurface dummy_egl_surface; |
| 95 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 96 | struct wl_list sprite_list; |
Kristian Høgsberg | 65bec24 | 2012-03-05 19:57:35 -0500 | [diff] [blame] | 97 | int sprites_are_broken; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 98 | |
Pekka Paalanen | bce2d3f | 2011-12-02 13:07:27 +0200 | [diff] [blame] | 99 | uint32_t prev_state; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 100 | }; |
| 101 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 102 | struct drm_mode { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 103 | struct weston_mode base; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 104 | drmModeModeInfo mode_info; |
| 105 | }; |
| 106 | |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 107 | struct drm_output; |
| 108 | |
| 109 | struct drm_fb { |
| 110 | struct gbm_bo *bo; |
| 111 | struct drm_output *output; |
| 112 | uint32_t fb_id; |
| 113 | int is_client_buffer; |
| 114 | struct wl_buffer *buffer; |
| 115 | struct wl_listener buffer_destroy_listener; |
| 116 | }; |
| 117 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 118 | struct drm_output { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 119 | struct weston_output base; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 120 | |
Kristian Høgsberg | 2f9ed71 | 2012-07-26 17:57:15 -0400 | [diff] [blame] | 121 | char *name; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 122 | uint32_t crtc_id; |
| 123 | uint32_t connector_id; |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 124 | drmModeCrtcPtr original_crtc; |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 125 | |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 126 | int vblank_pending; |
| 127 | int page_flip_pending; |
| 128 | |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 129 | struct gbm_surface *surface; |
Kristian Høgsberg | 8e1f77f | 2012-05-03 11:39:35 -0400 | [diff] [blame] | 130 | struct gbm_bo *cursor_bo[2]; |
Kristian Høgsberg | 24e4275 | 2012-07-18 12:08:37 -0400 | [diff] [blame] | 131 | int current_cursor, cursor_free, cursor_x, cursor_y; |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 132 | EGLSurface egl_surface; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 133 | struct drm_fb *current, *next; |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 134 | struct backlight *backlight; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 135 | }; |
| 136 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 137 | /* |
| 138 | * An output has a primary display plane plus zero or more sprites for |
| 139 | * blending display contents. |
| 140 | */ |
| 141 | struct drm_sprite { |
| 142 | struct wl_list link; |
| 143 | |
| 144 | uint32_t fb_id; |
| 145 | uint32_t pending_fb_id; |
| 146 | struct weston_surface *surface; |
| 147 | struct weston_surface *pending_surface; |
| 148 | |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 149 | struct drm_output *output; |
| 150 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 151 | struct drm_compositor *compositor; |
| 152 | |
| 153 | struct wl_listener destroy_listener; |
| 154 | struct wl_listener pending_destroy_listener; |
| 155 | |
| 156 | uint32_t possible_crtcs; |
| 157 | uint32_t plane_id; |
| 158 | uint32_t count_formats; |
| 159 | |
| 160 | int32_t src_x, src_y; |
| 161 | uint32_t src_w, src_h; |
| 162 | uint32_t dest_x, dest_y; |
| 163 | uint32_t dest_w, dest_h; |
| 164 | |
| 165 | uint32_t formats[]; |
| 166 | }; |
| 167 | |
| 168 | static int |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 169 | drm_sprite_crtc_supported(struct weston_output *output_base, uint32_t supported) |
| 170 | { |
| 171 | struct weston_compositor *ec = output_base->compositor; |
| 172 | struct drm_compositor *c =(struct drm_compositor *) ec; |
| 173 | struct drm_output *output = (struct drm_output *) output_base; |
| 174 | int crtc; |
| 175 | |
| 176 | for (crtc = 0; crtc < c->num_crtcs; crtc++) { |
| 177 | if (c->crtcs[crtc] != output->crtc_id) |
| 178 | continue; |
| 179 | |
| 180 | if (supported & (1 << crtc)) |
| 181 | return -1; |
| 182 | } |
| 183 | |
| 184 | return 0; |
| 185 | } |
| 186 | |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 187 | static void |
| 188 | drm_fb_destroy_callback(struct gbm_bo *bo, void *data) |
| 189 | { |
| 190 | struct drm_fb *fb = data; |
| 191 | struct gbm_device *gbm = gbm_bo_get_device(bo); |
| 192 | |
| 193 | if (fb->fb_id) |
| 194 | drmModeRmFB(gbm_device_get_fd(gbm), fb->fb_id); |
| 195 | |
| 196 | if (fb->buffer) { |
| 197 | weston_buffer_post_release(fb->buffer); |
| 198 | wl_list_remove(&fb->buffer_destroy_listener.link); |
| 199 | } |
| 200 | |
| 201 | free(data); |
| 202 | } |
| 203 | |
| 204 | static struct drm_fb * |
| 205 | drm_fb_get_from_bo(struct gbm_bo *bo, struct drm_output *output) |
| 206 | { |
| 207 | struct drm_fb *fb = gbm_bo_get_user_data(bo); |
| 208 | struct drm_compositor *compositor = |
| 209 | (struct drm_compositor *) output->base.compositor; |
| 210 | uint32_t width, height, stride, handle; |
| 211 | int ret; |
| 212 | |
| 213 | if (fb) |
| 214 | return fb; |
| 215 | |
| 216 | fb = malloc(sizeof *fb); |
| 217 | |
| 218 | fb->bo = bo; |
| 219 | fb->output = output; |
| 220 | fb->is_client_buffer = 0; |
| 221 | fb->buffer = NULL; |
| 222 | |
| 223 | width = gbm_bo_get_width(bo); |
| 224 | height = gbm_bo_get_height(bo); |
Kristian Høgsberg | 270a7cb | 2012-07-16 16:44:16 -0400 | [diff] [blame] | 225 | stride = gbm_bo_get_stride(bo); |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 226 | handle = gbm_bo_get_handle(bo).u32; |
| 227 | |
| 228 | ret = drmModeAddFB(compositor->drm.fd, width, height, 24, 32, |
| 229 | stride, handle, &fb->fb_id); |
| 230 | if (ret) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 231 | weston_log("failed to create kms fb: %m\n"); |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 232 | free(fb); |
| 233 | return NULL; |
| 234 | } |
| 235 | |
| 236 | gbm_bo_set_user_data(bo, fb, drm_fb_destroy_callback); |
| 237 | |
| 238 | return fb; |
| 239 | } |
| 240 | |
| 241 | static void |
| 242 | fb_handle_buffer_destroy(struct wl_listener *listener, void *data) |
| 243 | { |
| 244 | struct drm_fb *fb = container_of(listener, struct drm_fb, |
| 245 | buffer_destroy_listener); |
| 246 | |
| 247 | fb->buffer = NULL; |
| 248 | |
| 249 | if (fb == fb->output->next || |
| 250 | (fb == fb->output->current && !fb->output->next)) |
Kristian Høgsberg | 49952d1 | 2012-06-20 00:35:59 -0400 | [diff] [blame] | 251 | weston_output_schedule_repaint(&fb->output->base); |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 252 | } |
| 253 | |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 254 | static int |
Kristian Høgsberg | 6143f7d | 2012-07-14 00:31:32 -0400 | [diff] [blame] | 255 | drm_output_prepare_scanout_surface(struct weston_output *_output, |
| 256 | struct weston_surface *es) |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 257 | { |
Kristian Høgsberg | 6143f7d | 2012-07-14 00:31:32 -0400 | [diff] [blame] | 258 | struct drm_output *output = (struct drm_output *) _output; |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 259 | struct drm_compositor *c = |
| 260 | (struct drm_compositor *) output->base.compositor; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 261 | struct gbm_bo *bo; |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 262 | |
Kristian Høgsberg | 101cb65 | 2012-02-17 10:45:16 -0500 | [diff] [blame] | 263 | if (es->geometry.x != output->base.x || |
Pekka Paalanen | ba3cf95 | 2012-01-25 16:22:05 +0200 | [diff] [blame] | 264 | es->geometry.y != output->base.y || |
Pekka Paalanen | 60921e5 | 2012-01-25 15:55:43 +0200 | [diff] [blame] | 265 | es->geometry.width != output->base.current->width || |
| 266 | es->geometry.height != output->base.current->height || |
Pekka Paalanen | f1f5b36 | 2012-01-25 14:33:33 +0200 | [diff] [blame] | 267 | es->transform.enabled || |
Kristian Høgsberg | 2763a2e | 2012-07-13 22:54:43 -0400 | [diff] [blame] | 268 | es->buffer == NULL) |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 269 | return -1; |
| 270 | |
Kristian Høgsberg | 2763a2e | 2012-07-13 22:54:43 -0400 | [diff] [blame] | 271 | bo = gbm_bo_import(c->gbm, GBM_BO_IMPORT_WL_BUFFER, |
| 272 | es->buffer, GBM_BO_USE_SCANOUT); |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 273 | |
Ander Conselvan de Oliveira | a64b15d | 2012-05-02 16:42:22 +0300 | [diff] [blame] | 274 | /* Need to verify output->region contained in surface opaque |
| 275 | * region. Or maybe just that format doesn't have alpha. |
| 276 | * For now, scanout only if format is XRGB8888. */ |
| 277 | if (gbm_bo_get_format(bo) != GBM_FORMAT_XRGB8888) { |
| 278 | gbm_bo_destroy(bo); |
| 279 | return -1; |
| 280 | } |
| 281 | |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 282 | output->next = drm_fb_get_from_bo(bo, output); |
| 283 | if (!output->next) { |
| 284 | gbm_bo_destroy(bo); |
| 285 | return -1; |
| 286 | } |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 287 | |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 288 | output->next->is_client_buffer = 1; |
| 289 | output->next->buffer = es->buffer; |
| 290 | output->next->buffer->busy_count++; |
| 291 | output->next->buffer_destroy_listener.notify = fb_handle_buffer_destroy; |
| 292 | |
| 293 | wl_signal_add(&output->next->buffer->resource.destroy_signal, |
| 294 | &output->next->buffer_destroy_listener); |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 295 | |
Kristian Høgsberg | 6143f7d | 2012-07-14 00:31:32 -0400 | [diff] [blame] | 296 | es->plane = WESTON_PLANE_DRM_FB; |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 297 | |
| 298 | return 0; |
| 299 | } |
| 300 | |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 301 | static void |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 302 | drm_output_render(struct drm_output *output, pixman_region32_t *damage) |
| 303 | { |
| 304 | struct drm_compositor *compositor = |
| 305 | (struct drm_compositor *) output->base.compositor; |
| 306 | struct weston_surface *surface; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 307 | struct gbm_bo *bo; |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 308 | |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 309 | if (!eglMakeCurrent(compositor->base.egl_display, output->egl_surface, |
| 310 | output->egl_surface, |
| 311 | compositor->base.egl_context)) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 312 | weston_log("failed to make current\n"); |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 313 | return; |
| 314 | } |
| 315 | |
| 316 | wl_list_for_each_reverse(surface, &compositor->base.surface_list, link) |
Kristian Høgsberg | 6143f7d | 2012-07-14 00:31:32 -0400 | [diff] [blame] | 317 | if (surface->plane == WESTON_PLANE_PRIMARY) |
| 318 | weston_surface_draw(surface, &output->base, damage); |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 319 | |
Kristian Høgsberg | e0f832b | 2012-06-20 00:13:18 -0400 | [diff] [blame] | 320 | wl_signal_emit(&output->base.frame_signal, output); |
Scott Moreau | 062be7e | 2012-04-20 13:37:33 -0600 | [diff] [blame] | 321 | |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 322 | eglSwapBuffers(compositor->base.egl_display, output->egl_surface); |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 323 | bo = gbm_surface_lock_front_buffer(output->surface); |
| 324 | if (!bo) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 325 | weston_log("failed to lock front buffer: %m\n"); |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 326 | return; |
| 327 | } |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 328 | |
| 329 | output->next = drm_fb_get_from_bo(bo, output); |
| 330 | if (!output->next) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 331 | weston_log("failed to get drm_fb for bo\n"); |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 332 | gbm_surface_release_buffer(output->surface, bo); |
| 333 | return; |
| 334 | } |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | static void |
Kristian Høgsberg | 6ddcdae | 2012-02-28 22:31:58 -0500 | [diff] [blame] | 338 | drm_output_repaint(struct weston_output *output_base, |
| 339 | pixman_region32_t *damage) |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 340 | { |
| 341 | struct drm_output *output = (struct drm_output *) output_base; |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 342 | struct drm_compositor *compositor = |
| 343 | (struct drm_compositor *) output->base.compositor; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 344 | struct drm_sprite *s; |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 345 | struct drm_mode *mode; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 346 | int ret = 0; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 347 | |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 348 | if (!output->next) |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 349 | drm_output_render(output, damage); |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 350 | if (!output->next) |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 351 | return; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 352 | |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 353 | mode = container_of(output->base.current, struct drm_mode, base); |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 354 | if (!output->current) { |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 355 | ret = drmModeSetCrtc(compositor->drm.fd, output->crtc_id, |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 356 | output->next->fb_id, 0, 0, |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 357 | &output->connector_id, 1, |
| 358 | &mode->mode_info); |
| 359 | if (ret) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 360 | weston_log("set mode failed: %m\n"); |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 361 | return; |
| 362 | } |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 363 | } |
| 364 | |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 365 | if (drmModePageFlip(compositor->drm.fd, output->crtc_id, |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 366 | output->next->fb_id, |
Kristian Høgsberg | 54f14c3 | 2012-01-18 11:47:41 -0500 | [diff] [blame] | 367 | DRM_MODE_PAGE_FLIP_EVENT, output) < 0) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 368 | weston_log("queueing pageflip failed: %m\n"); |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 369 | return; |
Kristian Høgsberg | 54f14c3 | 2012-01-18 11:47:41 -0500 | [diff] [blame] | 370 | } |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 371 | |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 372 | output->page_flip_pending = 1; |
| 373 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 374 | /* |
| 375 | * Now, update all the sprite surfaces |
| 376 | */ |
| 377 | wl_list_for_each(s, &compositor->sprite_list, link) { |
| 378 | uint32_t flags = 0; |
| 379 | drmVBlank vbl = { |
| 380 | .request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT, |
| 381 | .request.sequence = 1, |
| 382 | }; |
| 383 | |
| 384 | if (!drm_sprite_crtc_supported(output_base, s->possible_crtcs)) |
| 385 | continue; |
| 386 | |
| 387 | ret = drmModeSetPlane(compositor->drm.fd, s->plane_id, |
| 388 | output->crtc_id, s->pending_fb_id, flags, |
| 389 | s->dest_x, s->dest_y, |
| 390 | s->dest_w, s->dest_h, |
| 391 | s->src_x, s->src_y, |
| 392 | s->src_w, s->src_h); |
| 393 | if (ret) |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 394 | weston_log("setplane failed: %d: %s\n", |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 395 | ret, strerror(errno)); |
| 396 | |
| 397 | /* |
| 398 | * Queue a vblank signal so we know when the surface |
| 399 | * becomes active on the display or has been replaced. |
| 400 | */ |
| 401 | vbl.request.signal = (unsigned long)s; |
| 402 | ret = drmWaitVBlank(compositor->drm.fd, &vbl); |
| 403 | if (ret) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 404 | weston_log("vblank event request failed: %d: %s\n", |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 405 | ret, strerror(errno)); |
| 406 | } |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 407 | |
| 408 | s->output = output; |
| 409 | output->vblank_pending = 1; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 410 | } |
| 411 | |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 412 | return; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | static void |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 416 | vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec, |
| 417 | void *data) |
| 418 | { |
| 419 | struct drm_sprite *s = (struct drm_sprite *)data; |
| 420 | struct drm_compositor *c = s->compositor; |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 421 | struct drm_output *output = s->output; |
| 422 | uint32_t msecs; |
| 423 | |
| 424 | output->vblank_pending = 0; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 425 | |
| 426 | if (s->surface) { |
| 427 | weston_buffer_post_release(s->surface->buffer); |
| 428 | wl_list_remove(&s->destroy_listener.link); |
| 429 | s->surface = NULL; |
| 430 | drmModeRmFB(c->drm.fd, s->fb_id); |
| 431 | s->fb_id = 0; |
| 432 | } |
| 433 | |
| 434 | if (s->pending_surface) { |
| 435 | wl_list_remove(&s->pending_destroy_listener.link); |
Kristian Høgsberg | 27e3052 | 2012-04-11 23:18:23 -0400 | [diff] [blame] | 436 | wl_signal_add(&s->pending_surface->buffer->resource.destroy_signal, |
| 437 | &s->destroy_listener); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 438 | s->surface = s->pending_surface; |
| 439 | s->pending_surface = NULL; |
| 440 | s->fb_id = s->pending_fb_id; |
| 441 | s->pending_fb_id = 0; |
| 442 | } |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 443 | |
| 444 | if (!output->page_flip_pending) { |
| 445 | msecs = sec * 1000 + usec / 1000; |
| 446 | weston_output_finish_frame(&output->base, msecs); |
| 447 | } |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | static void |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 451 | page_flip_handler(int fd, unsigned int frame, |
| 452 | unsigned int sec, unsigned int usec, void *data) |
| 453 | { |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 454 | struct drm_output *output = (struct drm_output *) data; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 455 | uint32_t msecs; |
| 456 | |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 457 | output->page_flip_pending = 0; |
| 458 | |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 459 | if (output->current) { |
| 460 | if (output->current->is_client_buffer) |
| 461 | gbm_bo_destroy(output->current->bo); |
| 462 | else |
| 463 | gbm_surface_release_buffer(output->surface, |
| 464 | output->current->bo); |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 465 | } |
| 466 | |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 467 | output->current = output->next; |
| 468 | output->next = NULL; |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 469 | |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 470 | if (!output->vblank_pending) { |
| 471 | msecs = sec * 1000 + usec / 1000; |
| 472 | weston_output_finish_frame(&output->base, msecs); |
| 473 | } |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | static int |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 477 | drm_surface_format_supported(struct drm_sprite *s, uint32_t format) |
| 478 | { |
Kristian Høgsberg | 875ab9e | 2012-03-30 11:52:39 -0400 | [diff] [blame] | 479 | uint32_t i; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 480 | |
| 481 | for (i = 0; i < s->count_formats; i++) |
| 482 | if (s->formats[i] == format) |
| 483 | return 1; |
| 484 | |
| 485 | return 0; |
| 486 | } |
| 487 | |
| 488 | static int |
| 489 | drm_surface_transform_supported(struct weston_surface *es) |
| 490 | { |
Kristian Høgsberg | 3b00bae | 2012-07-13 15:25:07 -0400 | [diff] [blame] | 491 | struct weston_matrix *matrix = &es->transform.matrix; |
| 492 | int i; |
| 493 | |
| 494 | if (!es->transform.enabled) |
| 495 | return 1; |
| 496 | |
| 497 | for (i = 0; i < 16; i++) { |
| 498 | switch (i) { |
| 499 | case 10: |
| 500 | case 15: |
| 501 | if (matrix->d[i] != 1.0) |
| 502 | return 0; |
| 503 | break; |
| 504 | case 0: |
| 505 | case 5: |
| 506 | case 12: |
| 507 | case 13: |
| 508 | break; |
| 509 | default: |
| 510 | if (matrix->d[i] != 0.0) |
| 511 | return 0; |
| 512 | break; |
| 513 | } |
| 514 | } |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 515 | |
| 516 | return 1; |
| 517 | } |
| 518 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 519 | static void |
| 520 | drm_disable_unused_sprites(struct weston_output *output_base) |
| 521 | { |
| 522 | struct weston_compositor *ec = output_base->compositor; |
| 523 | struct drm_compositor *c =(struct drm_compositor *) ec; |
| 524 | struct drm_output *output = (struct drm_output *) output_base; |
| 525 | struct drm_sprite *s; |
| 526 | int ret; |
| 527 | |
| 528 | wl_list_for_each(s, &c->sprite_list, link) { |
| 529 | if (s->pending_fb_id) |
| 530 | continue; |
| 531 | |
| 532 | ret = drmModeSetPlane(c->drm.fd, s->plane_id, |
| 533 | output->crtc_id, 0, 0, |
| 534 | 0, 0, 0, 0, 0, 0, 0, 0); |
| 535 | if (ret) |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 536 | weston_log("failed to disable plane: %d: %s\n", |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 537 | ret, strerror(errno)); |
| 538 | drmModeRmFB(c->drm.fd, s->fb_id); |
Ander Conselvan de Oliveira | fd1f4c6 | 2012-06-26 17:09:14 +0300 | [diff] [blame] | 539 | |
| 540 | if (s->surface) { |
| 541 | s->surface = NULL; |
| 542 | wl_list_remove(&s->destroy_listener.link); |
| 543 | } |
| 544 | |
| 545 | assert(!s->pending_surface); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 546 | s->fb_id = 0; |
| 547 | s->pending_fb_id = 0; |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | /* |
| 552 | * This function must take care to damage any previously assigned surface |
| 553 | * if the sprite ends up binding to a different surface than in the |
| 554 | * previous frame. |
| 555 | */ |
| 556 | static int |
| 557 | drm_output_prepare_overlay_surface(struct weston_output *output_base, |
Kristian Høgsberg | 6143f7d | 2012-07-14 00:31:32 -0400 | [diff] [blame] | 558 | struct weston_surface *es) |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 559 | { |
| 560 | struct weston_compositor *ec = output_base->compositor; |
| 561 | struct drm_compositor *c =(struct drm_compositor *) ec; |
| 562 | struct drm_sprite *s; |
| 563 | int found = 0; |
| 564 | EGLint handle, stride; |
| 565 | struct gbm_bo *bo; |
| 566 | uint32_t fb_id = 0; |
| 567 | uint32_t handles[4], pitches[4], offsets[4]; |
| 568 | int ret = 0; |
| 569 | pixman_region32_t dest_rect, src_rect; |
| 570 | pixman_box32_t *box; |
| 571 | uint32_t format; |
Kristian Høgsberg | 3b00bae | 2012-07-13 15:25:07 -0400 | [diff] [blame] | 572 | wl_fixed_t sx1, sy1, sx2, sy2; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 573 | |
Kristian Høgsberg | 65bec24 | 2012-03-05 19:57:35 -0500 | [diff] [blame] | 574 | if (c->sprites_are_broken) |
| 575 | return -1; |
| 576 | |
Ander Conselvan de Oliveira | d450b19 | 2012-06-26 17:09:12 +0300 | [diff] [blame] | 577 | if (es->output_mask != (1u << output_base->id)) |
| 578 | return -1; |
| 579 | |
Kristian Høgsberg | 2763a2e | 2012-07-13 22:54:43 -0400 | [diff] [blame] | 580 | if (es->buffer == NULL) |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 581 | return -1; |
| 582 | |
| 583 | if (!drm_surface_transform_supported(es)) |
| 584 | return -1; |
| 585 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 586 | wl_list_for_each(s, &c->sprite_list, link) { |
| 587 | if (!drm_sprite_crtc_supported(output_base, s->possible_crtcs)) |
| 588 | continue; |
| 589 | |
| 590 | if (!s->pending_fb_id) { |
| 591 | found = 1; |
| 592 | break; |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | /* No sprites available */ |
| 597 | if (!found) |
| 598 | return -1; |
| 599 | |
Kristian Høgsberg | 2763a2e | 2012-07-13 22:54:43 -0400 | [diff] [blame] | 600 | bo = gbm_bo_import(c->gbm, GBM_BO_IMPORT_WL_BUFFER, |
| 601 | es->buffer, GBM_BO_USE_SCANOUT); |
| 602 | if (!bo) |
| 603 | return -1; |
| 604 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 605 | format = gbm_bo_get_format(bo); |
| 606 | handle = gbm_bo_get_handle(bo).s32; |
Kristian Høgsberg | 270a7cb | 2012-07-16 16:44:16 -0400 | [diff] [blame] | 607 | stride = gbm_bo_get_stride(bo); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 608 | |
| 609 | gbm_bo_destroy(bo); |
| 610 | |
| 611 | if (!drm_surface_format_supported(s, format)) |
| 612 | return -1; |
| 613 | |
| 614 | if (!handle) |
| 615 | return -1; |
| 616 | |
| 617 | handles[0] = handle; |
| 618 | pitches[0] = stride; |
| 619 | offsets[0] = 0; |
| 620 | |
| 621 | ret = drmModeAddFB2(c->drm.fd, es->geometry.width, es->geometry.height, |
| 622 | format, handles, pitches, offsets, |
| 623 | &fb_id, 0); |
| 624 | if (ret) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 625 | weston_log("addfb2 failed: %d\n", ret); |
Kristian Høgsberg | 65bec24 | 2012-03-05 19:57:35 -0500 | [diff] [blame] | 626 | c->sprites_are_broken = 1; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 627 | return -1; |
| 628 | } |
| 629 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 630 | s->pending_fb_id = fb_id; |
| 631 | s->pending_surface = es; |
| 632 | es->buffer->busy_count++; |
| 633 | |
| 634 | /* |
| 635 | * Calculate the source & dest rects properly based on actual |
| 636 | * postion (note the caller has called weston_surface_update_transform() |
| 637 | * for us already). |
| 638 | */ |
| 639 | pixman_region32_init(&dest_rect); |
| 640 | pixman_region32_intersect(&dest_rect, &es->transform.boundingbox, |
| 641 | &output_base->region); |
| 642 | pixman_region32_translate(&dest_rect, -output_base->x, -output_base->y); |
| 643 | box = pixman_region32_extents(&dest_rect); |
| 644 | s->dest_x = box->x1; |
| 645 | s->dest_y = box->y1; |
| 646 | s->dest_w = box->x2 - box->x1; |
| 647 | s->dest_h = box->y2 - box->y1; |
| 648 | pixman_region32_fini(&dest_rect); |
| 649 | |
| 650 | pixman_region32_init(&src_rect); |
| 651 | pixman_region32_intersect(&src_rect, &es->transform.boundingbox, |
| 652 | &output_base->region); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 653 | box = pixman_region32_extents(&src_rect); |
Kristian Høgsberg | 3b00bae | 2012-07-13 15:25:07 -0400 | [diff] [blame] | 654 | |
| 655 | weston_surface_from_global_fixed(es, |
| 656 | wl_fixed_from_int(box->x1), |
| 657 | wl_fixed_from_int(box->y1), |
| 658 | &sx1, &sy1); |
| 659 | weston_surface_from_global_fixed(es, |
| 660 | wl_fixed_from_int(box->x2), |
| 661 | wl_fixed_from_int(box->y2), |
| 662 | &sx2, &sy2); |
| 663 | |
| 664 | if (sx1 < 0) |
| 665 | sx1 = 0; |
| 666 | if (sy1 < 0) |
| 667 | sy1 = 0; |
| 668 | if (sx2 > wl_fixed_from_int(es->geometry.width)) |
| 669 | sx2 = wl_fixed_from_int(es->geometry.width); |
| 670 | if (sy2 > wl_fixed_from_int(es->geometry.height)) |
| 671 | sy2 = wl_fixed_from_int(es->geometry.height); |
| 672 | |
| 673 | s->src_x = sx1 << 8; |
| 674 | s->src_y = sy1 << 8; |
| 675 | s->src_w = (sx2 - sx1) << 8; |
| 676 | s->src_h = (sy2 - sy1) << 8; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 677 | pixman_region32_fini(&src_rect); |
| 678 | |
Kristian Høgsberg | 6143f7d | 2012-07-14 00:31:32 -0400 | [diff] [blame] | 679 | es->plane = WESTON_PLANE_DRM_PLANE; |
| 680 | |
Kristian Høgsberg | 27e3052 | 2012-04-11 23:18:23 -0400 | [diff] [blame] | 681 | wl_signal_add(&es->buffer->resource.destroy_signal, |
| 682 | &s->pending_destroy_listener); |
Kristian Høgsberg | 3b00bae | 2012-07-13 15:25:07 -0400 | [diff] [blame] | 683 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 684 | return 0; |
| 685 | } |
| 686 | |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 687 | static void |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 688 | drm_output_set_cursor(struct weston_output *output_base, |
Kristian Høgsberg | 4901f6c | 2012-07-14 01:24:58 -0400 | [diff] [blame] | 689 | struct weston_surface *es) |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 690 | { |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 691 | struct drm_output *output = (struct drm_output *) output_base; |
| 692 | struct drm_compositor *c = |
| 693 | (struct drm_compositor *) output->base.compositor; |
| 694 | EGLint handle, stride; |
| 695 | struct gbm_bo *bo; |
| 696 | uint32_t buf[64 * 64]; |
| 697 | unsigned char *s; |
Kristian Høgsberg | 24e4275 | 2012-07-18 12:08:37 -0400 | [diff] [blame] | 698 | int i, x, y; |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 699 | |
Kristian Høgsberg | 4901f6c | 2012-07-14 01:24:58 -0400 | [diff] [blame] | 700 | if (!output->cursor_free) |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 701 | return; |
Kristian Høgsberg | 4901f6c | 2012-07-14 01:24:58 -0400 | [diff] [blame] | 702 | if (es->output_mask != (1u << output_base->id)) |
| 703 | return; |
| 704 | if (es->buffer == NULL || !wl_buffer_is_shm(es->buffer) || |
| 705 | es->geometry.width > 64 || es->geometry.height > 64) |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 706 | return; |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 707 | |
Kristian Høgsberg | 1f5de35 | 2012-07-18 12:09:58 -0400 | [diff] [blame] | 708 | if (es->buffer && pixman_region32_not_empty(&es->damage)) { |
| 709 | output->current_cursor ^= 1; |
| 710 | bo = output->cursor_bo[output->current_cursor]; |
| 711 | memset(buf, 0, sizeof buf); |
| 712 | stride = wl_shm_buffer_get_stride(es->buffer); |
| 713 | s = wl_shm_buffer_get_data(es->buffer); |
| 714 | for (i = 0; i < es->geometry.height; i++) |
| 715 | memcpy(buf + i * 64, s + i * stride, |
| 716 | es->geometry.width * 4); |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 717 | |
Kristian Høgsberg | 1f5de35 | 2012-07-18 12:09:58 -0400 | [diff] [blame] | 718 | if (gbm_bo_write(bo, buf, sizeof buf) < 0) |
| 719 | return; |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 720 | |
Kristian Høgsberg | 1f5de35 | 2012-07-18 12:09:58 -0400 | [diff] [blame] | 721 | handle = gbm_bo_get_handle(bo).s32; |
| 722 | if (drmModeSetCursor(c->drm.fd, |
| 723 | output->crtc_id, handle, 64, 64)) { |
| 724 | weston_log("failed to set cursor: %n\n"); |
| 725 | return; |
| 726 | } |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 727 | } |
| 728 | |
Kristian Høgsberg | 24e4275 | 2012-07-18 12:08:37 -0400 | [diff] [blame] | 729 | x = es->geometry.x - output->base.x; |
| 730 | y = es->geometry.y - output->base.y; |
| 731 | if (output->cursor_x != x || output->cursor_y != y) { |
| 732 | if (drmModeMoveCursor(c->drm.fd, output->crtc_id, x, y)) { |
| 733 | weston_log("failed to move cursor: %m\n"); |
| 734 | return; |
| 735 | } |
| 736 | output->cursor_x = x; |
| 737 | output->cursor_y = y; |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 738 | } |
| 739 | |
Kristian Høgsberg | 4901f6c | 2012-07-14 01:24:58 -0400 | [diff] [blame] | 740 | es->plane = WESTON_PLANE_DRM_CURSOR; |
| 741 | output->cursor_free = 0; |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 742 | } |
| 743 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 744 | static void |
| 745 | drm_assign_planes(struct weston_output *output) |
| 746 | { |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 747 | struct drm_compositor *c = |
| 748 | (struct drm_compositor *) output->compositor; |
| 749 | struct drm_output *drm_output = (struct drm_output *) output; |
Kristian Høgsberg | 8e1f77f | 2012-05-03 11:39:35 -0400 | [diff] [blame] | 750 | struct weston_surface *es, *next; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 751 | pixman_region32_t overlap, surface_overlap; |
Kristian Høgsberg | 6143f7d | 2012-07-14 00:31:32 -0400 | [diff] [blame] | 752 | int prev_plane; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 753 | |
| 754 | /* |
| 755 | * Find a surface for each sprite in the output using some heuristics: |
| 756 | * 1) size |
| 757 | * 2) frequency of update |
| 758 | * 3) opacity (though some hw might support alpha blending) |
| 759 | * 4) clipping (this can be fixed with color keys) |
| 760 | * |
| 761 | * The idea is to save on blitting since this should save power. |
| 762 | * If we can get a large video surface on the sprite for example, |
| 763 | * the main display surface may not need to update at all, and |
| 764 | * the client buffer can be used directly for the sprite surface |
| 765 | * as we do for flipping full screen surfaces. |
| 766 | */ |
| 767 | pixman_region32_init(&overlap); |
Kristian Høgsberg | 4901f6c | 2012-07-14 01:24:58 -0400 | [diff] [blame] | 768 | drm_output->cursor_free = 1; |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 769 | wl_list_for_each_safe(es, next, &c->base.surface_list, link) { |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 770 | pixman_region32_init(&surface_overlap); |
| 771 | pixman_region32_intersect(&surface_overlap, &overlap, |
| 772 | &es->transform.boundingbox); |
| 773 | |
Kristian Høgsberg | 6143f7d | 2012-07-14 00:31:32 -0400 | [diff] [blame] | 774 | prev_plane = es->plane; |
| 775 | es->plane = WESTON_PLANE_PRIMARY; |
| 776 | if (pixman_region32_not_empty(&surface_overlap)) |
| 777 | goto bail; |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 778 | |
Kristian Høgsberg | 4901f6c | 2012-07-14 01:24:58 -0400 | [diff] [blame] | 779 | if (es->plane == WESTON_PLANE_PRIMARY) |
| 780 | drm_output_set_cursor(output, es); |
Kristian Høgsberg | 6143f7d | 2012-07-14 00:31:32 -0400 | [diff] [blame] | 781 | if (es->plane == WESTON_PLANE_PRIMARY) |
| 782 | drm_output_prepare_scanout_surface(output, es); |
| 783 | if (es->plane == WESTON_PLANE_PRIMARY) |
| 784 | drm_output_prepare_overlay_surface(output, es); |
| 785 | |
| 786 | bail: |
| 787 | if (es->plane == WESTON_PLANE_PRIMARY) |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 788 | pixman_region32_union(&overlap, &overlap, |
| 789 | &es->transform.boundingbox); |
Kristian Høgsberg | 6143f7d | 2012-07-14 00:31:32 -0400 | [diff] [blame] | 790 | |
| 791 | if (prev_plane != WESTON_PLANE_PRIMARY && |
| 792 | es->plane == WESTON_PLANE_PRIMARY) |
| 793 | weston_surface_damage(es); |
| 794 | else if (prev_plane == WESTON_PLANE_PRIMARY && |
| 795 | es->plane != WESTON_PLANE_PRIMARY) |
| 796 | weston_surface_damage_below(es); |
| 797 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 798 | pixman_region32_fini(&surface_overlap); |
| 799 | } |
| 800 | pixman_region32_fini(&overlap); |
| 801 | |
Kristian Høgsberg | 4901f6c | 2012-07-14 01:24:58 -0400 | [diff] [blame] | 802 | if (drm_output->cursor_free) |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 803 | drmModeSetCursor(c->drm.fd, drm_output->crtc_id, 0, 0, 0); |
Kristian Høgsberg | f2735ea | 2012-06-20 23:03:53 -0400 | [diff] [blame] | 804 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 805 | drm_disable_unused_sprites(output); |
| 806 | } |
| 807 | |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 808 | static void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 809 | drm_output_destroy(struct weston_output *output_base) |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 810 | { |
| 811 | struct drm_output *output = (struct drm_output *) output_base; |
| 812 | struct drm_compositor *c = |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 813 | (struct drm_compositor *) output->base.compositor; |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 814 | drmModeCrtcPtr origcrtc = output->original_crtc; |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 815 | |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 816 | if (output->backlight) |
| 817 | backlight_destroy(output->backlight); |
| 818 | |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 819 | /* Turn off hardware cursor */ |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 820 | drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0); |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 821 | |
| 822 | /* Restore original CRTC state */ |
| 823 | drmModeSetCrtc(c->drm.fd, origcrtc->crtc_id, origcrtc->buffer_id, |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 824 | origcrtc->x, origcrtc->y, |
| 825 | &output->connector_id, 1, &origcrtc->mode); |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 826 | drmModeFreeCrtc(origcrtc); |
| 827 | |
Benjamin Franzke | 48c4ea2 | 2011-08-30 11:44:56 +0200 | [diff] [blame] | 828 | c->crtc_allocator &= ~(1 << output->crtc_id); |
| 829 | c->connector_allocator &= ~(1 << output->connector_id); |
| 830 | |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 831 | eglDestroySurface(c->base.egl_display, output->egl_surface); |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 832 | gbm_surface_destroy(output->surface); |
| 833 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 834 | weston_output_destroy(&output->base); |
Benjamin Franzke | 48c4ea2 | 2011-08-30 11:44:56 +0200 | [diff] [blame] | 835 | wl_list_remove(&output->base.link); |
| 836 | |
Kristian Høgsberg | 148ef01 | 2012-07-26 23:03:57 -0400 | [diff] [blame] | 837 | free(output->name); |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 838 | free(output); |
| 839 | } |
| 840 | |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 841 | static struct drm_mode * |
| 842 | choose_mode (struct drm_output *output, struct weston_mode *target_mode) |
| 843 | { |
| 844 | struct drm_mode *tmp_mode = NULL, *mode; |
| 845 | |
| 846 | if (output->base.current->width == target_mode->width && |
| 847 | output->base.current->height == target_mode->height && |
| 848 | (output->base.current->refresh == target_mode->refresh || |
| 849 | target_mode->refresh == 0)) |
| 850 | return (struct drm_mode *)output->base.current; |
| 851 | |
| 852 | wl_list_for_each(mode, &output->base.mode_list, base.link) { |
| 853 | if (mode->mode_info.hdisplay == target_mode->width && |
| 854 | mode->mode_info.vdisplay == target_mode->height) { |
| 855 | if (mode->mode_info.vrefresh == target_mode->refresh || |
| 856 | target_mode->refresh == 0) { |
| 857 | return mode; |
| 858 | } else if (!tmp_mode) |
| 859 | tmp_mode = mode; |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | return tmp_mode; |
| 864 | } |
| 865 | |
| 866 | static int |
| 867 | drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mode) |
| 868 | { |
| 869 | struct drm_output *output; |
| 870 | struct drm_mode *drm_mode; |
| 871 | int ret; |
| 872 | struct drm_compositor *ec; |
| 873 | struct gbm_surface *surface; |
| 874 | EGLSurface egl_surface; |
| 875 | |
| 876 | if (output_base == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 877 | weston_log("output is NULL.\n"); |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 878 | return -1; |
| 879 | } |
| 880 | |
| 881 | if (mode == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 882 | weston_log("mode is NULL.\n"); |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 883 | return -1; |
| 884 | } |
| 885 | |
| 886 | ec = (struct drm_compositor *)output_base->compositor; |
| 887 | output = (struct drm_output *)output_base; |
| 888 | drm_mode = choose_mode (output, mode); |
| 889 | |
| 890 | if (!drm_mode) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 891 | weston_log("%s, invalid resolution:%dx%d\n", __func__, mode->width, mode->height); |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 892 | return -1; |
| 893 | } else if (&drm_mode->base == output->base.current) { |
| 894 | return 0; |
| 895 | } else if (drm_mode->base.width == output->base.current->width && |
| 896 | drm_mode->base.height == output->base.current->height) { |
| 897 | /* only change refresh value */ |
| 898 | ret = drmModeSetCrtc(ec->drm.fd, |
| 899 | output->crtc_id, |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 900 | output->current->fb_id, 0, 0, |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 901 | &output->connector_id, 1, &drm_mode->mode_info); |
| 902 | |
| 903 | if (ret) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 904 | weston_log("failed to set mode (%dx%d) %u Hz\n", |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 905 | drm_mode->base.width, |
| 906 | drm_mode->base.height, |
Kristian Høgsberg | c4621b0 | 2012-05-10 12:23:53 -0400 | [diff] [blame] | 907 | drm_mode->base.refresh / 1000); |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 908 | ret = -1; |
| 909 | } else { |
| 910 | output->base.current->flags = 0; |
| 911 | output->base.current = &drm_mode->base; |
| 912 | drm_mode->base.flags = |
| 913 | WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED; |
| 914 | ret = 0; |
| 915 | } |
| 916 | |
| 917 | return ret; |
| 918 | } |
| 919 | |
| 920 | drm_mode->base.flags = |
| 921 | WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED; |
| 922 | |
| 923 | surface = gbm_surface_create(ec->gbm, |
| 924 | drm_mode->base.width, |
| 925 | drm_mode->base.height, |
| 926 | GBM_FORMAT_XRGB8888, |
| 927 | GBM_BO_USE_SCANOUT | |
| 928 | GBM_BO_USE_RENDERING); |
| 929 | if (!surface) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 930 | weston_log("failed to create gbm surface\n"); |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 931 | return -1; |
| 932 | } |
| 933 | |
| 934 | egl_surface = |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 935 | eglCreateWindowSurface(ec->base.egl_display, |
| 936 | ec->base.egl_config, |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 937 | surface, NULL); |
| 938 | |
| 939 | if (egl_surface == EGL_NO_SURFACE) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 940 | weston_log("failed to create egl surface\n"); |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 941 | goto err; |
| 942 | } |
| 943 | |
| 944 | ret = drmModeSetCrtc(ec->drm.fd, |
| 945 | output->crtc_id, |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 946 | output->current->fb_id, 0, 0, |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 947 | &output->connector_id, 1, &drm_mode->mode_info); |
| 948 | if (ret) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 949 | weston_log("failed to set mode\n"); |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 950 | goto err; |
| 951 | } |
| 952 | |
| 953 | /* reset rendering stuff. */ |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 954 | if (output->current) { |
| 955 | if (output->current->is_client_buffer) |
| 956 | gbm_bo_destroy(output->current->bo); |
| 957 | else |
| 958 | gbm_surface_release_buffer(output->surface, |
| 959 | output->current->bo); |
| 960 | } |
| 961 | output->current = NULL; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 962 | |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 963 | if (output->next) { |
| 964 | if (output->next->is_client_buffer) |
| 965 | gbm_bo_destroy(output->next->bo); |
| 966 | else |
| 967 | gbm_surface_release_buffer(output->surface, |
| 968 | output->next->bo); |
| 969 | } |
| 970 | output->next = NULL; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 971 | |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 972 | eglDestroySurface(ec->base.egl_display, output->egl_surface); |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 973 | gbm_surface_destroy(output->surface); |
| 974 | output->egl_surface = egl_surface; |
| 975 | output->surface = surface; |
| 976 | |
| 977 | /*update output*/ |
| 978 | output->base.current = &drm_mode->base; |
| 979 | output->base.dirty = 1; |
| 980 | weston_output_move(&output->base, output->base.x, output->base.y); |
| 981 | return 0; |
| 982 | |
| 983 | err: |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 984 | eglDestroySurface(ec->base.egl_display, egl_surface); |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 985 | gbm_surface_destroy(surface); |
| 986 | return -1; |
| 987 | } |
| 988 | |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 989 | static int |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 990 | on_drm_input(int fd, uint32_t mask, void *data) |
| 991 | { |
| 992 | drmEventContext evctx; |
| 993 | |
| 994 | memset(&evctx, 0, sizeof evctx); |
| 995 | evctx.version = DRM_EVENT_CONTEXT_VERSION; |
| 996 | evctx.page_flip_handler = page_flip_handler; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 997 | evctx.vblank_handler = vblank_handler; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 998 | drmHandleEvent(fd, &evctx); |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 999 | |
| 1000 | return 1; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1001 | } |
| 1002 | |
| 1003 | static int |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1004 | init_egl(struct drm_compositor *ec, struct udev_device *device) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1005 | { |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1006 | EGLint major, minor, n; |
Kristian Høgsberg | b5ef591 | 2012-03-28 22:53:49 -0400 | [diff] [blame] | 1007 | const char *filename, *sysnum; |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 1008 | int fd; |
Kristian Høgsberg | 2c28aa5 | 2010-07-28 23:47:54 -0400 | [diff] [blame] | 1009 | static const EGLint context_attribs[] = { |
| 1010 | EGL_CONTEXT_CLIENT_VERSION, 2, |
| 1011 | EGL_NONE |
| 1012 | }; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1013 | |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1014 | static const EGLint config_attribs[] = { |
| 1015 | EGL_SURFACE_TYPE, EGL_WINDOW_BIT, |
| 1016 | EGL_RED_SIZE, 1, |
| 1017 | EGL_GREEN_SIZE, 1, |
| 1018 | EGL_BLUE_SIZE, 1, |
| 1019 | EGL_ALPHA_SIZE, 0, |
| 1020 | EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, |
| 1021 | EGL_NONE |
| 1022 | }; |
| 1023 | |
Kristian Høgsberg | b71302e | 2012-05-10 12:28:35 -0400 | [diff] [blame] | 1024 | sysnum = udev_device_get_sysnum(device); |
| 1025 | if (sysnum) |
| 1026 | ec->drm.id = atoi(sysnum); |
| 1027 | if (!sysnum || ec->drm.id < 0) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1028 | weston_log("cannot get device sysnum\n"); |
Kristian Høgsberg | b71302e | 2012-05-10 12:28:35 -0400 | [diff] [blame] | 1029 | return -1; |
| 1030 | } |
| 1031 | |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 1032 | filename = udev_device_get_devnode(device); |
David Herrmann | 63ff706 | 2011-11-05 18:46:01 +0100 | [diff] [blame] | 1033 | fd = open(filename, O_RDWR | O_CLOEXEC); |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 1034 | if (fd < 0) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1035 | /* Probably permissions error */ |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1036 | weston_log("couldn't open %s, skipping\n", |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1037 | udev_device_get_devnode(device)); |
| 1038 | return -1; |
| 1039 | } |
| 1040 | |
Kristian Høgsberg | fc9c5e0 | 2012-06-08 16:45:33 -0400 | [diff] [blame] | 1041 | weston_log("using %s\n", filename); |
| 1042 | |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 1043 | ec->drm.fd = fd; |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 1044 | ec->gbm = gbm_create_device(ec->drm.fd); |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 1045 | ec->base.egl_display = eglGetDisplay(ec->gbm); |
| 1046 | if (ec->base.egl_display == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1047 | weston_log("failed to create display\n"); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1048 | return -1; |
| 1049 | } |
| 1050 | |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 1051 | if (!eglInitialize(ec->base.egl_display, &major, &minor)) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1052 | weston_log("failed to initialize display\n"); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1053 | return -1; |
| 1054 | } |
| 1055 | |
Darxus | 55973f2 | 2010-11-22 21:24:39 -0500 | [diff] [blame] | 1056 | if (!eglBindAPI(EGL_OPENGL_ES_API)) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1057 | weston_log("failed to bind api EGL_OPENGL_ES_API\n"); |
Darxus | 55973f2 | 2010-11-22 21:24:39 -0500 | [diff] [blame] | 1058 | return -1; |
| 1059 | } |
| 1060 | |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 1061 | if (!eglChooseConfig(ec->base.egl_display, config_attribs, |
| 1062 | &ec->base.egl_config, 1, &n) || n != 1) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1063 | weston_log("failed to choose config: %d\n", n); |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1064 | return -1; |
| 1065 | } |
| 1066 | |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 1067 | ec->base.egl_context = |
| 1068 | eglCreateContext(ec->base.egl_display, ec->base.egl_config, |
| 1069 | EGL_NO_CONTEXT, context_attribs); |
| 1070 | if (ec->base.egl_context == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1071 | weston_log("failed to create context\n"); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1072 | return -1; |
| 1073 | } |
| 1074 | |
Kristian Høgsberg | b5ef591 | 2012-03-28 22:53:49 -0400 | [diff] [blame] | 1075 | ec->dummy_surface = gbm_surface_create(ec->gbm, 10, 10, |
| 1076 | GBM_FORMAT_XRGB8888, |
| 1077 | GBM_BO_USE_RENDERING); |
| 1078 | if (!ec->dummy_surface) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1079 | weston_log("failed to create dummy gbm surface\n"); |
Kristian Høgsberg | b5ef591 | 2012-03-28 22:53:49 -0400 | [diff] [blame] | 1080 | return -1; |
| 1081 | } |
| 1082 | |
| 1083 | ec->dummy_egl_surface = |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 1084 | eglCreateWindowSurface(ec->base.egl_display, |
| 1085 | ec->base.egl_config, |
| 1086 | ec->dummy_surface, |
| 1087 | NULL); |
Kristian Høgsberg | b5ef591 | 2012-03-28 22:53:49 -0400 | [diff] [blame] | 1088 | if (ec->dummy_egl_surface == EGL_NO_SURFACE) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1089 | weston_log("failed to create egl surface\n"); |
Kristian Høgsberg | b5ef591 | 2012-03-28 22:53:49 -0400 | [diff] [blame] | 1090 | return -1; |
| 1091 | } |
| 1092 | |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 1093 | if (!eglMakeCurrent(ec->base.egl_display, ec->dummy_egl_surface, |
| 1094 | ec->dummy_egl_surface, ec->base.egl_context)) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1095 | weston_log("failed to make context current\n"); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1096 | return -1; |
| 1097 | } |
| 1098 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1099 | return 0; |
| 1100 | } |
| 1101 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1102 | static int |
| 1103 | drm_output_add_mode(struct drm_output *output, drmModeModeInfo *info) |
| 1104 | { |
| 1105 | struct drm_mode *mode; |
Kristian Høgsberg | c4621b0 | 2012-05-10 12:23:53 -0400 | [diff] [blame] | 1106 | uint64_t refresh; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1107 | |
| 1108 | mode = malloc(sizeof *mode); |
| 1109 | if (mode == NULL) |
| 1110 | return -1; |
| 1111 | |
| 1112 | mode->base.flags = 0; |
| 1113 | mode->base.width = info->hdisplay; |
| 1114 | mode->base.height = info->vdisplay; |
Kristian Høgsberg | c4621b0 | 2012-05-10 12:23:53 -0400 | [diff] [blame] | 1115 | |
| 1116 | /* Calculate higher precision (mHz) refresh rate */ |
| 1117 | refresh = (info->clock * 1000000LL / info->htotal + |
| 1118 | info->vtotal / 2) / info->vtotal; |
| 1119 | |
| 1120 | if (info->flags & DRM_MODE_FLAG_INTERLACE) |
| 1121 | refresh *= 2; |
| 1122 | if (info->flags & DRM_MODE_FLAG_DBLSCAN) |
| 1123 | refresh /= 2; |
| 1124 | if (info->vscan > 1) |
| 1125 | refresh /= info->vscan; |
| 1126 | |
| 1127 | mode->base.refresh = refresh; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1128 | mode->mode_info = *info; |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 1129 | |
| 1130 | if (info->type & DRM_MODE_TYPE_PREFERRED) |
| 1131 | mode->base.flags |= WL_OUTPUT_MODE_PREFERRED; |
| 1132 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1133 | wl_list_insert(output->base.mode_list.prev, &mode->base.link); |
| 1134 | |
| 1135 | return 0; |
| 1136 | } |
| 1137 | |
| 1138 | static int |
| 1139 | drm_subpixel_to_wayland(int drm_value) |
| 1140 | { |
| 1141 | switch (drm_value) { |
| 1142 | default: |
| 1143 | case DRM_MODE_SUBPIXEL_UNKNOWN: |
| 1144 | return WL_OUTPUT_SUBPIXEL_UNKNOWN; |
| 1145 | case DRM_MODE_SUBPIXEL_NONE: |
| 1146 | return WL_OUTPUT_SUBPIXEL_NONE; |
| 1147 | case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB: |
| 1148 | return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB; |
| 1149 | case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR: |
| 1150 | return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR; |
| 1151 | case DRM_MODE_SUBPIXEL_VERTICAL_RGB: |
| 1152 | return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB; |
| 1153 | case DRM_MODE_SUBPIXEL_VERTICAL_BGR: |
| 1154 | return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR; |
| 1155 | } |
| 1156 | } |
| 1157 | |
Kristian Høgsberg | 9f404b7 | 2012-01-26 00:11:01 -0500 | [diff] [blame] | 1158 | static void |
Kristian Høgsberg | 27e3052 | 2012-04-11 23:18:23 -0400 | [diff] [blame] | 1159 | sprite_handle_buffer_destroy(struct wl_listener *listener, void *data) |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1160 | { |
| 1161 | struct drm_sprite *sprite = |
| 1162 | container_of(listener, struct drm_sprite, |
| 1163 | destroy_listener); |
Ander Conselvan de Oliveira | 01a57ed | 2012-06-26 17:09:15 +0300 | [diff] [blame] | 1164 | struct drm_compositor *compositor = sprite->compositor; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1165 | |
| 1166 | sprite->surface = NULL; |
Ander Conselvan de Oliveira | 01a57ed | 2012-06-26 17:09:15 +0300 | [diff] [blame] | 1167 | drmModeRmFB(compositor->drm.fd, sprite->fb_id); |
| 1168 | sprite->fb_id = 0; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1169 | } |
| 1170 | |
| 1171 | static void |
Kristian Høgsberg | 27e3052 | 2012-04-11 23:18:23 -0400 | [diff] [blame] | 1172 | sprite_handle_pending_buffer_destroy(struct wl_listener *listener, void *data) |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1173 | { |
| 1174 | struct drm_sprite *sprite = |
| 1175 | container_of(listener, struct drm_sprite, |
| 1176 | pending_destroy_listener); |
Ander Conselvan de Oliveira | 01a57ed | 2012-06-26 17:09:15 +0300 | [diff] [blame] | 1177 | struct drm_compositor *compositor = sprite->compositor; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1178 | |
| 1179 | sprite->pending_surface = NULL; |
Ander Conselvan de Oliveira | 01a57ed | 2012-06-26 17:09:15 +0300 | [diff] [blame] | 1180 | drmModeRmFB(compositor->drm.fd, sprite->pending_fb_id); |
| 1181 | sprite->pending_fb_id = 0; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1182 | } |
| 1183 | |
Tiago Vignatti | 5ab91ad | 2012-03-12 19:40:09 -0300 | [diff] [blame] | 1184 | /* returns a value between 0-255 range, where higher is brighter */ |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1185 | static uint32_t |
| 1186 | drm_get_backlight(struct drm_output *output) |
| 1187 | { |
| 1188 | long brightness, max_brightness, norm; |
| 1189 | |
| 1190 | brightness = backlight_get_brightness(output->backlight); |
| 1191 | max_brightness = backlight_get_max_brightness(output->backlight); |
| 1192 | |
Tiago Vignatti | 5ab91ad | 2012-03-12 19:40:09 -0300 | [diff] [blame] | 1193 | /* convert it on a scale of 0 to 255 */ |
| 1194 | norm = (brightness * 255)/(max_brightness); |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1195 | |
| 1196 | return (uint32_t) norm; |
| 1197 | } |
| 1198 | |
Tiago Vignatti | 5ab91ad | 2012-03-12 19:40:09 -0300 | [diff] [blame] | 1199 | /* values accepted are between 0-255 range */ |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1200 | static void |
| 1201 | drm_set_backlight(struct weston_output *output_base, uint32_t value) |
| 1202 | { |
| 1203 | struct drm_output *output = (struct drm_output *) output_base; |
| 1204 | long max_brightness, new_brightness; |
| 1205 | |
| 1206 | if (!output->backlight) |
| 1207 | return; |
| 1208 | |
Kristian Høgsberg | 875ab9e | 2012-03-30 11:52:39 -0400 | [diff] [blame] | 1209 | if (value > 255) |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1210 | return; |
| 1211 | |
| 1212 | max_brightness = backlight_get_max_brightness(output->backlight); |
| 1213 | |
| 1214 | /* get denormalized value */ |
Tiago Vignatti | 5ab91ad | 2012-03-12 19:40:09 -0300 | [diff] [blame] | 1215 | new_brightness = (value * max_brightness) / 255; |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1216 | |
| 1217 | backlight_set_brightness(output->backlight, new_brightness); |
| 1218 | } |
| 1219 | |
| 1220 | static drmModePropertyPtr |
| 1221 | drm_get_prop(int fd, drmModeConnectorPtr connector, const char *name) |
| 1222 | { |
| 1223 | drmModePropertyPtr props; |
| 1224 | int i; |
| 1225 | |
| 1226 | for (i = 0; i < connector->count_props; i++) { |
| 1227 | props = drmModeGetProperty(fd, connector->props[i]); |
| 1228 | if (!props) |
| 1229 | continue; |
| 1230 | |
| 1231 | if (!strcmp(props->name, name)) |
| 1232 | return props; |
| 1233 | |
| 1234 | drmModeFreeProperty(props); |
| 1235 | } |
| 1236 | |
| 1237 | return NULL; |
| 1238 | } |
| 1239 | |
| 1240 | static void |
| 1241 | drm_set_dpms(struct weston_output *output_base, enum dpms_enum level) |
| 1242 | { |
| 1243 | struct drm_output *output = (struct drm_output *) output_base; |
| 1244 | struct weston_compositor *ec = output_base->compositor; |
| 1245 | struct drm_compositor *c = (struct drm_compositor *) ec; |
| 1246 | drmModeConnectorPtr connector; |
| 1247 | drmModePropertyPtr prop; |
| 1248 | |
| 1249 | connector = drmModeGetConnector(c->drm.fd, output->connector_id); |
| 1250 | if (!connector) |
| 1251 | return; |
| 1252 | |
| 1253 | prop = drm_get_prop(c->drm.fd, connector, "DPMS"); |
| 1254 | if (!prop) { |
| 1255 | drmModeFreeConnector(connector); |
| 1256 | return; |
| 1257 | } |
| 1258 | |
| 1259 | drmModeConnectorSetProperty(c->drm.fd, connector->connector_id, |
| 1260 | prop->prop_id, level); |
| 1261 | drmModeFreeProperty(prop); |
| 1262 | drmModeFreeConnector(connector); |
| 1263 | } |
| 1264 | |
Kristian Høgsberg | 2f9ed71 | 2012-07-26 17:57:15 -0400 | [diff] [blame] | 1265 | static const char *connector_type_names[] = { |
| 1266 | "None", |
| 1267 | "VGA", |
| 1268 | "DVI", |
| 1269 | "DVI", |
| 1270 | "DVI", |
| 1271 | "Composite", |
| 1272 | "TV", |
| 1273 | "LVDS", |
| 1274 | "CTV", |
| 1275 | "DIN", |
| 1276 | "DP", |
| 1277 | "HDMI", |
| 1278 | "HDMI", |
| 1279 | "TV", |
| 1280 | "eDP", |
| 1281 | }; |
| 1282 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1283 | static int |
Kristian Høgsberg | 9ca3846 | 2012-07-26 22:44:55 -0400 | [diff] [blame] | 1284 | find_crtc_for_connector(struct drm_compositor *ec, |
| 1285 | drmModeRes *resources, drmModeConnector *connector) |
| 1286 | { |
| 1287 | drmModeEncoder *encoder; |
| 1288 | uint32_t possible_crtcs; |
| 1289 | int i, j; |
| 1290 | |
| 1291 | for (j = 0; j < connector->count_encoders; j++) { |
| 1292 | encoder = drmModeGetEncoder(ec->drm.fd, connector->encoders[j]); |
| 1293 | if (encoder == NULL) { |
| 1294 | weston_log("Failed to get encoder.\n"); |
| 1295 | return -1; |
| 1296 | } |
| 1297 | possible_crtcs = encoder->possible_crtcs; |
| 1298 | drmModeFreeEncoder(encoder); |
| 1299 | |
| 1300 | for (i = 0; i < resources->count_crtcs; i++) { |
| 1301 | if (possible_crtcs & (1 << i) && |
| 1302 | !(ec->crtc_allocator & (1 << resources->crtcs[i]))) |
| 1303 | return i; |
| 1304 | } |
| 1305 | } |
| 1306 | |
| 1307 | return -1; |
| 1308 | } |
| 1309 | |
| 1310 | static int |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1311 | create_output_for_connector(struct drm_compositor *ec, |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1312 | drmModeRes *resources, |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 1313 | drmModeConnector *connector, |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1314 | int x, int y, struct udev_device *drm_device) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1315 | { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1316 | struct drm_output *output; |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 1317 | struct drm_mode *drm_mode, *next; |
Scott Moreau | 8ab5d45 | 2012-07-30 19:51:08 -0600 | [diff] [blame] | 1318 | struct weston_mode *m, *preferred, *current, *configured; |
| 1319 | struct drm_configured_output *o = NULL, *temp; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1320 | drmModeEncoder *encoder; |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 1321 | drmModeModeInfo crtc_mode; |
| 1322 | drmModeCrtc *crtc; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1323 | int i, ret; |
Kristian Høgsberg | 2f9ed71 | 2012-07-26 17:57:15 -0400 | [diff] [blame] | 1324 | char name[32]; |
| 1325 | const char *type_name; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1326 | |
Kristian Høgsberg | 9ca3846 | 2012-07-26 22:44:55 -0400 | [diff] [blame] | 1327 | i = find_crtc_for_connector(ec, resources, connector); |
| 1328 | if (i < 0) { |
| 1329 | weston_log("No usable crtc/encoder pair for connector.\n"); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1330 | return -1; |
| 1331 | } |
| 1332 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1333 | output = malloc(sizeof *output); |
Kristian Høgsberg | 9ca3846 | 2012-07-26 22:44:55 -0400 | [diff] [blame] | 1334 | if (output == NULL) |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1335 | return -1; |
| 1336 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1337 | memset(output, 0, sizeof *output); |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1338 | output->base.subpixel = drm_subpixel_to_wayland(connector->subpixel); |
| 1339 | output->base.make = "unknown"; |
| 1340 | output->base.model = "unknown"; |
| 1341 | wl_list_init(&output->base.mode_list); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1342 | |
Kristian Høgsberg | 2f9ed71 | 2012-07-26 17:57:15 -0400 | [diff] [blame] | 1343 | if (connector->connector_type < ARRAY_LENGTH(connector_type_names)) |
| 1344 | type_name = connector_type_names[connector->connector_type]; |
| 1345 | else |
| 1346 | type_name = "UNKNOWN"; |
| 1347 | snprintf(name, 32, "%s%d", type_name, connector->connector_type_id); |
| 1348 | output->name = strdup(name); |
| 1349 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1350 | output->crtc_id = resources->crtcs[i]; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1351 | ec->crtc_allocator |= (1 << output->crtc_id); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1352 | output->connector_id = connector->connector_id; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1353 | ec->connector_allocator |= (1 << output->connector_id); |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1354 | |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 1355 | output->original_crtc = drmModeGetCrtc(ec->drm.fd, output->crtc_id); |
| 1356 | |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 1357 | /* Get the current mode on the crtc that's currently driving |
| 1358 | * this connector. */ |
| 1359 | encoder = drmModeGetEncoder(ec->drm.fd, connector->encoder_id); |
Wang Quanxian | acb805a | 2012-07-30 18:09:46 -0400 | [diff] [blame] | 1360 | memset(&crtc_mode, 0, sizeof crtc_mode); |
| 1361 | if (encoder != NULL) { |
| 1362 | crtc = drmModeGetCrtc(ec->drm.fd, encoder->crtc_id); |
| 1363 | drmModeFreeEncoder(encoder); |
| 1364 | if (crtc == NULL) |
| 1365 | goto err_free; |
| 1366 | if (crtc->mode_valid) |
| 1367 | crtc_mode = crtc->mode; |
| 1368 | drmModeFreeCrtc(crtc); |
| 1369 | } |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 1370 | |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 1371 | for (i = 0; i < connector->count_modes; i++) { |
| 1372 | ret = drm_output_add_mode(output, &connector->modes[i]); |
| 1373 | if (ret) |
| 1374 | goto err_free; |
| 1375 | } |
| 1376 | |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 1377 | preferred = NULL; |
| 1378 | current = NULL; |
Scott Moreau | 8ab5d45 | 2012-07-30 19:51:08 -0600 | [diff] [blame] | 1379 | configured = NULL; |
| 1380 | |
| 1381 | wl_list_for_each(temp, &configured_output_list, link) { |
| 1382 | if (strcmp(temp->name, output->name) == 0) { |
| 1383 | weston_log("%s mode \"%s\" in config\n", |
| 1384 | temp->name, temp->mode); |
| 1385 | o = temp; |
| 1386 | break; |
| 1387 | } |
| 1388 | } |
| 1389 | |
| 1390 | if (o && strcmp("off", o->mode) == 0) { |
| 1391 | weston_log("Disabling output %s\n", o->name); |
| 1392 | |
| 1393 | drmModeSetCrtc(ec->drm.fd, output->crtc_id, |
| 1394 | 0, 0, 0, 0, 0, NULL); |
| 1395 | goto err_free; |
| 1396 | } |
| 1397 | |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 1398 | wl_list_for_each(drm_mode, &output->base.mode_list, base.link) { |
Scott Moreau | 8ab5d45 | 2012-07-30 19:51:08 -0600 | [diff] [blame] | 1399 | if (o && o->width == drm_mode->base.width && |
| 1400 | o->height == drm_mode->base.height) |
| 1401 | configured = &drm_mode->base; |
Wang Quanxian | acb805a | 2012-07-30 18:09:46 -0400 | [diff] [blame] | 1402 | if (!memcmp(&crtc_mode, &drm_mode->mode_info, sizeof crtc_mode)) |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 1403 | current = &drm_mode->base; |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 1404 | if (drm_mode->base.flags & WL_OUTPUT_MODE_PREFERRED) |
| 1405 | preferred = &drm_mode->base; |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 1406 | } |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1407 | |
Wang Quanxian | acb805a | 2012-07-30 18:09:46 -0400 | [diff] [blame] | 1408 | if (current == NULL && crtc_mode.clock != 0) { |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 1409 | ret = drm_output_add_mode(output, &crtc_mode); |
| 1410 | if (ret) |
| 1411 | goto err_free; |
| 1412 | current = container_of(output->base.mode_list.prev, |
| 1413 | struct weston_mode, link); |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 1414 | } |
| 1415 | |
Scott Moreau | 8ab5d45 | 2012-07-30 19:51:08 -0600 | [diff] [blame] | 1416 | if (o && o->config == OUTPUT_CONFIG_CURRENT) |
| 1417 | configured = current; |
| 1418 | |
Wang Quanxian | acb805a | 2012-07-30 18:09:46 -0400 | [diff] [blame] | 1419 | if (option_current_mode && current) |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 1420 | output->base.current = current; |
Scott Moreau | 8ab5d45 | 2012-07-30 19:51:08 -0600 | [diff] [blame] | 1421 | else if (configured) |
| 1422 | output->base.current = configured; |
Wang Quanxian | acb805a | 2012-07-30 18:09:46 -0400 | [diff] [blame] | 1423 | else if (preferred) |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 1424 | output->base.current = preferred; |
Wang Quanxian | acb805a | 2012-07-30 18:09:46 -0400 | [diff] [blame] | 1425 | else if (current) |
| 1426 | output->base.current = current; |
| 1427 | |
| 1428 | if (output->base.current == NULL) { |
| 1429 | weston_log("no available modes for %s\n", output->name); |
| 1430 | goto err_free; |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 1431 | } |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1432 | |
Wang Quanxian | acb805a | 2012-07-30 18:09:46 -0400 | [diff] [blame] | 1433 | output->base.current->flags |= WL_OUTPUT_MODE_CURRENT; |
| 1434 | |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1435 | output->surface = gbm_surface_create(ec->gbm, |
| 1436 | output->base.current->width, |
| 1437 | output->base.current->height, |
| 1438 | GBM_FORMAT_XRGB8888, |
| 1439 | GBM_BO_USE_SCANOUT | |
| 1440 | GBM_BO_USE_RENDERING); |
| 1441 | if (!output->surface) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1442 | weston_log("failed to create gbm surface\n"); |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1443 | goto err_free; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1444 | } |
| 1445 | |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1446 | output->egl_surface = |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 1447 | eglCreateWindowSurface(ec->base.egl_display, |
| 1448 | ec->base.egl_config, |
| 1449 | output->surface, |
| 1450 | NULL); |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1451 | if (output->egl_surface == EGL_NO_SURFACE) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1452 | weston_log("failed to create egl surface\n"); |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1453 | goto err_surface; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1454 | } |
| 1455 | |
Kristian Høgsberg | 8e1f77f | 2012-05-03 11:39:35 -0400 | [diff] [blame] | 1456 | output->cursor_bo[0] = |
| 1457 | gbm_bo_create(ec->gbm, 64, 64, GBM_FORMAT_ARGB8888, |
| 1458 | GBM_BO_USE_CURSOR_64X64 | GBM_BO_USE_WRITE); |
| 1459 | output->cursor_bo[1] = |
| 1460 | gbm_bo_create(ec->gbm, 64, 64, GBM_FORMAT_ARGB8888, |
| 1461 | GBM_BO_USE_CURSOR_64X64 | GBM_BO_USE_WRITE); |
| 1462 | |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1463 | output->backlight = backlight_init(drm_device, |
| 1464 | connector->connector_type); |
| 1465 | if (output->backlight) { |
| 1466 | output->base.set_backlight = drm_set_backlight; |
| 1467 | output->base.backlight_current = drm_get_backlight(output); |
| 1468 | } |
| 1469 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1470 | weston_output_init(&output->base, &ec->base, x, y, |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1471 | connector->mmWidth, connector->mmHeight, |
| 1472 | WL_OUTPUT_FLIPPED); |
Kristian Høgsberg | a4b7e20 | 2011-10-24 13:26:32 -0400 | [diff] [blame] | 1473 | |
| 1474 | wl_list_insert(ec->base.output_list.prev, &output->base.link); |
| 1475 | |
Alex Wu | bd3354b | 2012-04-17 17:20:49 +0800 | [diff] [blame] | 1476 | output->base.origin = output->base.current; |
Kristian Høgsberg | 68c479a | 2012-01-25 23:32:28 -0500 | [diff] [blame] | 1477 | output->base.repaint = drm_output_repaint; |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 1478 | output->base.destroy = drm_output_destroy; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1479 | output->base.assign_planes = drm_assign_planes; |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1480 | output->base.set_dpms = drm_set_dpms; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1481 | output->base.switch_mode = drm_output_switch_mode; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 1482 | |
Kristian Høgsberg | 2f9ed71 | 2012-07-26 17:57:15 -0400 | [diff] [blame] | 1483 | weston_log("Output %s, (connector %d, crtc %d)\n", |
| 1484 | output->name, output->connector_id, output->crtc_id); |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 1485 | wl_list_for_each(m, &output->base.mode_list, link) |
| 1486 | weston_log_continue(" mode %dx%d@%.1f%s%s%s\n", |
| 1487 | m->width, m->height, m->refresh / 1000.0, |
| 1488 | m->flags & WL_OUTPUT_MODE_PREFERRED ? |
| 1489 | ", preferred" : "", |
| 1490 | m->flags & WL_OUTPUT_MODE_CURRENT ? |
| 1491 | ", current" : "", |
| 1492 | connector->count_modes == 0 ? |
| 1493 | ", built-in" : ""); |
Kristian Høgsberg | fc9c5e0 | 2012-06-08 16:45:33 -0400 | [diff] [blame] | 1494 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1495 | return 0; |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 1496 | |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1497 | err_surface: |
| 1498 | gbm_surface_destroy(output->surface); |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 1499 | err_free: |
| 1500 | wl_list_for_each_safe(drm_mode, next, &output->base.mode_list, |
| 1501 | base.link) { |
| 1502 | wl_list_remove(&drm_mode->base.link); |
| 1503 | free(drm_mode); |
| 1504 | } |
| 1505 | |
| 1506 | drmModeFreeCrtc(output->original_crtc); |
| 1507 | ec->crtc_allocator &= ~(1 << output->crtc_id); |
| 1508 | ec->connector_allocator &= ~(1 << output->connector_id); |
Kristian Høgsberg | 148ef01 | 2012-07-26 23:03:57 -0400 | [diff] [blame] | 1509 | free(output->name); |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 1510 | free(output); |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1511 | |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 1512 | return -1; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1513 | } |
| 1514 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1515 | static void |
| 1516 | create_sprites(struct drm_compositor *ec) |
| 1517 | { |
| 1518 | struct drm_sprite *sprite; |
| 1519 | drmModePlaneRes *plane_res; |
| 1520 | drmModePlane *plane; |
Kristian Høgsberg | 875ab9e | 2012-03-30 11:52:39 -0400 | [diff] [blame] | 1521 | uint32_t i; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1522 | |
| 1523 | plane_res = drmModeGetPlaneResources(ec->drm.fd); |
| 1524 | if (!plane_res) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1525 | weston_log("failed to get plane resources: %s\n", |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1526 | strerror(errno)); |
| 1527 | return; |
| 1528 | } |
| 1529 | |
| 1530 | for (i = 0; i < plane_res->count_planes; i++) { |
| 1531 | plane = drmModeGetPlane(ec->drm.fd, plane_res->planes[i]); |
| 1532 | if (!plane) |
| 1533 | continue; |
| 1534 | |
| 1535 | sprite = malloc(sizeof(*sprite) + ((sizeof(uint32_t)) * |
| 1536 | plane->count_formats)); |
| 1537 | if (!sprite) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1538 | weston_log("%s: out of memory\n", |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1539 | __func__); |
| 1540 | free(plane); |
| 1541 | continue; |
| 1542 | } |
| 1543 | |
| 1544 | memset(sprite, 0, sizeof *sprite); |
| 1545 | |
| 1546 | sprite->possible_crtcs = plane->possible_crtcs; |
| 1547 | sprite->plane_id = plane->plane_id; |
| 1548 | sprite->surface = NULL; |
| 1549 | sprite->pending_surface = NULL; |
| 1550 | sprite->fb_id = 0; |
| 1551 | sprite->pending_fb_id = 0; |
Kristian Høgsberg | 27e3052 | 2012-04-11 23:18:23 -0400 | [diff] [blame] | 1552 | sprite->destroy_listener.notify = sprite_handle_buffer_destroy; |
| 1553 | sprite->pending_destroy_listener.notify = |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1554 | sprite_handle_pending_buffer_destroy; |
| 1555 | sprite->compositor = ec; |
| 1556 | sprite->count_formats = plane->count_formats; |
| 1557 | memcpy(sprite->formats, plane->formats, |
Rob Clark | 8efbc8e | 2012-03-11 19:48:44 -0500 | [diff] [blame] | 1558 | plane->count_formats * sizeof(plane->formats[0])); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1559 | drmModeFreePlane(plane); |
| 1560 | |
| 1561 | wl_list_insert(&ec->sprite_list, &sprite->link); |
| 1562 | } |
| 1563 | |
| 1564 | free(plane_res->planes); |
| 1565 | free(plane_res); |
| 1566 | } |
| 1567 | |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 1568 | static void |
| 1569 | destroy_sprites(struct drm_compositor *compositor) |
| 1570 | { |
| 1571 | struct drm_sprite *sprite, *next; |
| 1572 | struct drm_output *output; |
| 1573 | |
| 1574 | output = container_of(compositor->base.output_list.next, |
| 1575 | struct drm_output, base.link); |
| 1576 | |
| 1577 | wl_list_for_each_safe(sprite, next, &compositor->sprite_list, link) { |
| 1578 | drmModeSetPlane(compositor->drm.fd, |
| 1579 | sprite->plane_id, |
| 1580 | output->crtc_id, 0, 0, |
| 1581 | 0, 0, 0, 0, 0, 0, 0, 0); |
| 1582 | drmModeRmFB(compositor->drm.fd, sprite->fb_id); |
| 1583 | free(sprite); |
| 1584 | } |
| 1585 | } |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1586 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1587 | static int |
Kristian Høgsberg | 875ab9e | 2012-03-30 11:52:39 -0400 | [diff] [blame] | 1588 | create_outputs(struct drm_compositor *ec, uint32_t option_connector, |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1589 | struct udev_device *drm_device) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1590 | { |
| 1591 | drmModeConnector *connector; |
| 1592 | drmModeRes *resources; |
| 1593 | int i; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 1594 | int x = 0, y = 0; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1595 | |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 1596 | resources = drmModeGetResources(ec->drm.fd); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1597 | if (!resources) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1598 | weston_log("drmModeGetResources failed\n"); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1599 | return -1; |
| 1600 | } |
| 1601 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1602 | ec->crtcs = calloc(resources->count_crtcs, sizeof(uint32_t)); |
Christopher Michael | eb866cd | 2012-03-07 14:55:21 -0500 | [diff] [blame] | 1603 | if (!ec->crtcs) { |
| 1604 | drmModeFreeResources(resources); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1605 | return -1; |
Christopher Michael | eb866cd | 2012-03-07 14:55:21 -0500 | [diff] [blame] | 1606 | } |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1607 | |
| 1608 | ec->num_crtcs = resources->count_crtcs; |
| 1609 | memcpy(ec->crtcs, resources->crtcs, sizeof(uint32_t) * ec->num_crtcs); |
| 1610 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1611 | for (i = 0; i < resources->count_connectors; i++) { |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1612 | connector = drmModeGetConnector(ec->drm.fd, |
| 1613 | resources->connectors[i]); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1614 | if (connector == NULL) |
| 1615 | continue; |
| 1616 | |
| 1617 | if (connector->connection == DRM_MODE_CONNECTED && |
| 1618 | (option_connector == 0 || |
Benjamin Franzke | 9eaee35 | 2011-08-02 13:03:54 +0200 | [diff] [blame] | 1619 | connector->connector_id == option_connector)) { |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 1620 | if (create_output_for_connector(ec, resources, |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1621 | connector, x, y, |
| 1622 | drm_device) < 0) { |
Benjamin Franzke | 439d986 | 2011-10-07 08:20:53 +0200 | [diff] [blame] | 1623 | drmModeFreeConnector(connector); |
| 1624 | continue; |
| 1625 | } |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1626 | |
Benjamin Franzke | 9eaee35 | 2011-08-02 13:03:54 +0200 | [diff] [blame] | 1627 | x += container_of(ec->base.output_list.prev, |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1628 | struct weston_output, |
Benjamin Franzke | 9eaee35 | 2011-08-02 13:03:54 +0200 | [diff] [blame] | 1629 | link)->current->width; |
| 1630 | } |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 1631 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1632 | drmModeFreeConnector(connector); |
| 1633 | } |
| 1634 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1635 | if (wl_list_empty(&ec->base.output_list)) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1636 | weston_log("No currently active connector found.\n"); |
Christopher Michael | eb866cd | 2012-03-07 14:55:21 -0500 | [diff] [blame] | 1637 | drmModeFreeResources(resources); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1638 | return -1; |
| 1639 | } |
| 1640 | |
| 1641 | drmModeFreeResources(resources); |
| 1642 | |
| 1643 | return 0; |
| 1644 | } |
| 1645 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1646 | static void |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1647 | update_outputs(struct drm_compositor *ec, struct udev_device *drm_device) |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1648 | { |
| 1649 | drmModeConnector *connector; |
| 1650 | drmModeRes *resources; |
| 1651 | struct drm_output *output, *next; |
| 1652 | int x = 0, y = 0; |
| 1653 | int x_offset = 0, y_offset = 0; |
| 1654 | uint32_t connected = 0, disconnects = 0; |
| 1655 | int i; |
| 1656 | |
| 1657 | resources = drmModeGetResources(ec->drm.fd); |
| 1658 | if (!resources) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1659 | weston_log("drmModeGetResources failed\n"); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1660 | return; |
| 1661 | } |
| 1662 | |
| 1663 | /* collect new connects */ |
| 1664 | for (i = 0; i < resources->count_connectors; i++) { |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1665 | int connector_id = resources->connectors[i]; |
| 1666 | |
| 1667 | connector = drmModeGetConnector(ec->drm.fd, connector_id); |
David Herrmann | 7551cff | 2011-12-08 17:05:43 +0100 | [diff] [blame] | 1668 | if (connector == NULL) |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1669 | continue; |
| 1670 | |
David Herrmann | 7551cff | 2011-12-08 17:05:43 +0100 | [diff] [blame] | 1671 | if (connector->connection != DRM_MODE_CONNECTED) { |
| 1672 | drmModeFreeConnector(connector); |
| 1673 | continue; |
| 1674 | } |
| 1675 | |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1676 | connected |= (1 << connector_id); |
| 1677 | |
| 1678 | if (!(ec->connector_allocator & (1 << connector_id))) { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1679 | struct weston_output *last = |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1680 | container_of(ec->base.output_list.prev, |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1681 | struct weston_output, link); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1682 | |
| 1683 | /* XXX: not yet needed, we die with 0 outputs */ |
| 1684 | if (!wl_list_empty(&ec->base.output_list)) |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1685 | x = last->x + last->current->width; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1686 | else |
| 1687 | x = 0; |
| 1688 | y = 0; |
| 1689 | create_output_for_connector(ec, resources, |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1690 | connector, x, y, |
| 1691 | drm_device); |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1692 | weston_log("connector %d connected\n", connector_id); |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1693 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1694 | } |
| 1695 | drmModeFreeConnector(connector); |
| 1696 | } |
| 1697 | drmModeFreeResources(resources); |
| 1698 | |
| 1699 | disconnects = ec->connector_allocator & ~connected; |
| 1700 | if (disconnects) { |
| 1701 | wl_list_for_each_safe(output, next, &ec->base.output_list, |
| 1702 | base.link) { |
| 1703 | if (x_offset != 0 || y_offset != 0) { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1704 | weston_output_move(&output->base, |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1705 | output->base.x - x_offset, |
| 1706 | output->base.y - y_offset); |
| 1707 | } |
| 1708 | |
| 1709 | if (disconnects & (1 << output->connector_id)) { |
| 1710 | disconnects &= ~(1 << output->connector_id); |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1711 | weston_log("connector %d disconnected\n", |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1712 | output->connector_id); |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1713 | x_offset += output->base.current->width; |
Benjamin Franzke | 48c4ea2 | 2011-08-30 11:44:56 +0200 | [diff] [blame] | 1714 | drm_output_destroy(&output->base); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1715 | } |
| 1716 | } |
| 1717 | } |
| 1718 | |
| 1719 | /* FIXME: handle zero outputs, without terminating */ |
| 1720 | if (ec->connector_allocator == 0) |
| 1721 | wl_display_terminate(ec->base.wl_display); |
| 1722 | } |
| 1723 | |
| 1724 | static int |
David Herrmann | d7488c2 | 2012-03-11 20:05:21 +0100 | [diff] [blame] | 1725 | udev_event_is_hotplug(struct drm_compositor *ec, struct udev_device *device) |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1726 | { |
David Herrmann | d7488c2 | 2012-03-11 20:05:21 +0100 | [diff] [blame] | 1727 | const char *sysnum; |
David Herrmann | 6ac52db | 2012-03-11 20:05:22 +0100 | [diff] [blame] | 1728 | const char *val; |
David Herrmann | d7488c2 | 2012-03-11 20:05:21 +0100 | [diff] [blame] | 1729 | |
| 1730 | sysnum = udev_device_get_sysnum(device); |
| 1731 | if (!sysnum || atoi(sysnum) != ec->drm.id) |
| 1732 | return 0; |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1733 | |
David Herrmann | 6ac52db | 2012-03-11 20:05:22 +0100 | [diff] [blame] | 1734 | val = udev_device_get_property_value(device, "HOTPLUG"); |
| 1735 | if (!val) |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1736 | return 0; |
| 1737 | |
David Herrmann | 6ac52db | 2012-03-11 20:05:22 +0100 | [diff] [blame] | 1738 | return strcmp(val, "1") == 0; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1739 | } |
| 1740 | |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 1741 | static int |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1742 | udev_drm_event(int fd, uint32_t mask, void *data) |
| 1743 | { |
| 1744 | struct drm_compositor *ec = data; |
| 1745 | struct udev_device *event; |
| 1746 | |
| 1747 | event = udev_monitor_receive_device(ec->udev_monitor); |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1748 | |
David Herrmann | d7488c2 | 2012-03-11 20:05:21 +0100 | [diff] [blame] | 1749 | if (udev_event_is_hotplug(ec, event)) |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1750 | update_outputs(ec, event); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1751 | |
| 1752 | udev_device_unref(event); |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 1753 | |
| 1754 | return 1; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1755 | } |
| 1756 | |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 1757 | static void |
Kristian Høgsberg | 7b884bc | 2012-07-31 14:32:01 -0400 | [diff] [blame^] | 1758 | drm_restore(struct weston_compositor *ec) |
| 1759 | { |
| 1760 | struct drm_compositor *d = (struct drm_compositor *) ec; |
| 1761 | |
| 1762 | if (weston_launcher_drm_set_master(&d->base, d->drm.fd, 0) < 0) |
| 1763 | weston_log("failed to drop master: %m\n"); |
| 1764 | tty_reset(d->tty); |
| 1765 | } |
| 1766 | |
| 1767 | static void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1768 | drm_destroy(struct weston_compositor *ec) |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 1769 | { |
| 1770 | struct drm_compositor *d = (struct drm_compositor *) ec; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1771 | struct weston_seat *seat, *next; |
Scott Moreau | 8ab5d45 | 2012-07-30 19:51:08 -0600 | [diff] [blame] | 1772 | struct drm_configured_output *o, *n; |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 1773 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1774 | wl_list_for_each_safe(seat, next, &ec->seat_list, link) |
| 1775 | evdev_input_destroy(seat); |
Scott Moreau | 8ab5d45 | 2012-07-30 19:51:08 -0600 | [diff] [blame] | 1776 | wl_list_for_each_safe(o, n, &configured_output_list, link) |
| 1777 | free(o); |
Jonas Ådahl | c97af92 | 2012-03-28 22:36:09 +0200 | [diff] [blame] | 1778 | |
| 1779 | wl_event_source_remove(d->udev_drm_source); |
| 1780 | wl_event_source_remove(d->drm_source); |
| 1781 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1782 | weston_compositor_shutdown(ec); |
Jonas Ådahl | c97af92 | 2012-03-28 22:36:09 +0200 | [diff] [blame] | 1783 | |
Ander Conselvan de Oliveira | 5f5f319 | 2012-04-30 13:31:28 +0300 | [diff] [blame] | 1784 | /* Work around crash in egl_dri2.c's dri2_make_current() */ |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 1785 | eglMakeCurrent(ec->egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, |
Ander Conselvan de Oliveira | 5f5f319 | 2012-04-30 13:31:28 +0300 | [diff] [blame] | 1786 | EGL_NO_CONTEXT); |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 1787 | eglTerminate(ec->egl_display); |
Ander Conselvan de Oliveira | 5f5f319 | 2012-04-30 13:31:28 +0300 | [diff] [blame] | 1788 | eglReleaseThread(); |
| 1789 | |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 1790 | gbm_device_destroy(d->gbm); |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 1791 | destroy_sprites(d); |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 1792 | if (weston_launcher_drm_set_master(&d->base, d->drm.fd, 0) < 0) |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1793 | weston_log("failed to drop master: %m\n"); |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 1794 | tty_destroy(d->tty); |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 1795 | |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 1796 | free(d); |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 1797 | } |
| 1798 | |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 1799 | static void |
Kristian Høgsberg | 835cd49 | 2012-01-18 11:48:46 -0500 | [diff] [blame] | 1800 | drm_compositor_set_modes(struct drm_compositor *compositor) |
| 1801 | { |
| 1802 | struct drm_output *output; |
| 1803 | struct drm_mode *drm_mode; |
| 1804 | int ret; |
| 1805 | |
| 1806 | wl_list_for_each(output, &compositor->base.output_list, base.link) { |
| 1807 | drm_mode = (struct drm_mode *) output->base.current; |
| 1808 | ret = drmModeSetCrtc(compositor->drm.fd, output->crtc_id, |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 1809 | output->current->fb_id, 0, 0, |
Kristian Høgsberg | 835cd49 | 2012-01-18 11:48:46 -0500 | [diff] [blame] | 1810 | &output->connector_id, 1, |
| 1811 | &drm_mode->mode_info); |
| 1812 | if (ret < 0) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1813 | weston_log( |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1814 | "failed to set mode %dx%d for output at %d,%d: %m\n", |
Kristian Høgsberg | 835cd49 | 2012-01-18 11:48:46 -0500 | [diff] [blame] | 1815 | drm_mode->base.width, drm_mode->base.height, |
| 1816 | output->base.x, output->base.y); |
| 1817 | } |
| 1818 | } |
| 1819 | } |
| 1820 | |
| 1821 | static void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1822 | vt_func(struct weston_compositor *compositor, int event) |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 1823 | { |
| 1824 | struct drm_compositor *ec = (struct drm_compositor *) compositor; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1825 | struct weston_seat *seat; |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 1826 | struct drm_sprite *sprite; |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 1827 | struct drm_output *output; |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 1828 | |
| 1829 | switch (event) { |
| 1830 | case TTY_ENTER_VT: |
| 1831 | compositor->focus = 1; |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 1832 | if (weston_launcher_drm_set_master(&ec->base, ec->drm.fd, 1)) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1833 | weston_log("failed to set master: %m\n"); |
Kristian Høgsberg | a018fb0 | 2012-01-16 10:52:52 -0500 | [diff] [blame] | 1834 | wl_display_terminate(compositor->wl_display); |
| 1835 | } |
Pekka Paalanen | bce2d3f | 2011-12-02 13:07:27 +0200 | [diff] [blame] | 1836 | compositor->state = ec->prev_state; |
Kristian Høgsberg | 835cd49 | 2012-01-18 11:48:46 -0500 | [diff] [blame] | 1837 | drm_compositor_set_modes(ec); |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1838 | weston_compositor_damage_all(compositor); |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1839 | wl_list_for_each(seat, &compositor->seat_list, link) { |
| 1840 | evdev_add_devices(ec->udev, seat); |
| 1841 | evdev_enable_udev_monitor(ec->udev, seat); |
Benjamin Franzke | 78d3afe | 2012-04-09 18:14:58 +0200 | [diff] [blame] | 1842 | } |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 1843 | break; |
| 1844 | case TTY_LEAVE_VT: |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1845 | wl_list_for_each(seat, &compositor->seat_list, link) { |
| 1846 | evdev_disable_udev_monitor(seat); |
| 1847 | evdev_remove_devices(seat); |
Kristian Høgsberg | 4014a6b | 2012-04-10 00:08:45 -0400 | [diff] [blame] | 1848 | } |
| 1849 | |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 1850 | compositor->focus = 0; |
Pekka Paalanen | bce2d3f | 2011-12-02 13:07:27 +0200 | [diff] [blame] | 1851 | ec->prev_state = compositor->state; |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1852 | compositor->state = WESTON_COMPOSITOR_SLEEPING; |
Kristian Høgsberg | d8e181b | 2011-05-06 15:38:28 -0400 | [diff] [blame] | 1853 | |
Kristian Høgsberg | 34f80ff | 2012-01-18 11:50:31 -0500 | [diff] [blame] | 1854 | /* If we have a repaint scheduled (either from a |
| 1855 | * pending pageflip or the idle handler), make sure we |
| 1856 | * cancel that so we don't try to pageflip when we're |
| 1857 | * vt switched away. The SLEEPING state will prevent |
| 1858 | * further attemps at repainting. When we switch |
| 1859 | * back, we schedule a repaint, which will process |
| 1860 | * pending frame callbacks. */ |
| 1861 | |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 1862 | wl_list_for_each(output, &ec->base.output_list, base.link) { |
| 1863 | output->base.repaint_needed = 0; |
| 1864 | drmModeSetCursor(ec->drm.fd, output->crtc_id, 0, 0, 0); |
Kristian Høgsberg | 34f80ff | 2012-01-18 11:50:31 -0500 | [diff] [blame] | 1865 | } |
| 1866 | |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 1867 | output = container_of(ec->base.output_list.next, |
| 1868 | struct drm_output, base.link); |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 1869 | |
| 1870 | wl_list_for_each(sprite, &ec->sprite_list, link) |
| 1871 | drmModeSetPlane(ec->drm.fd, |
| 1872 | sprite->plane_id, |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 1873 | output->crtc_id, 0, 0, |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 1874 | 0, 0, 0, 0, 0, 0, 0, 0); |
| 1875 | |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 1876 | if (weston_launcher_drm_set_master(&ec->base, ec->drm.fd, 0) < 0) |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1877 | weston_log("failed to drop master: %m\n"); |
Kristian Høgsberg | a018fb0 | 2012-01-16 10:52:52 -0500 | [diff] [blame] | 1878 | |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 1879 | break; |
| 1880 | }; |
| 1881 | } |
| 1882 | |
Kristian Høgsberg | 5d1c0c5 | 2012-04-10 00:11:50 -0400 | [diff] [blame] | 1883 | static void |
Daniel Stone | 325fc2d | 2012-05-30 16:31:58 +0100 | [diff] [blame] | 1884 | switch_vt_binding(struct wl_seat *seat, uint32_t time, uint32_t key, void *data) |
Kristian Høgsberg | 5d1c0c5 | 2012-04-10 00:11:50 -0400 | [diff] [blame] | 1885 | { |
| 1886 | struct drm_compositor *ec = data; |
| 1887 | |
Daniel Stone | 325fc2d | 2012-05-30 16:31:58 +0100 | [diff] [blame] | 1888 | tty_activate_vt(ec->tty, key - KEY_F1 + 1); |
Kristian Høgsberg | 5d1c0c5 | 2012-04-10 00:11:50 -0400 | [diff] [blame] | 1889 | } |
| 1890 | |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 1891 | static const char default_seat[] = "seat0"; |
| 1892 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1893 | static struct weston_compositor * |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 1894 | drm_compositor_create(struct wl_display *display, |
Daniel Stone | baf4359 | 2012-06-01 11:11:10 -0400 | [diff] [blame] | 1895 | int connector, const char *seat, int tty, |
Daniel Stone | c1be8e5 | 2012-06-01 11:14:02 -0400 | [diff] [blame] | 1896 | int argc, char *argv[], const char *config_file) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1897 | { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1898 | struct drm_compositor *ec; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1899 | struct udev_enumerate *e; |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1900 | struct udev_list_entry *entry; |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 1901 | struct udev_device *device, *drm_device; |
| 1902 | const char *path, *device_seat; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1903 | struct wl_event_loop *loop; |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 1904 | struct weston_seat *weston_seat, *next; |
Kristian Høgsberg | 5d1c0c5 | 2012-04-10 00:11:50 -0400 | [diff] [blame] | 1905 | uint32_t key; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1906 | |
Kristian Høgsberg | fc9c5e0 | 2012-06-08 16:45:33 -0400 | [diff] [blame] | 1907 | weston_log("initializing drm backend\n"); |
| 1908 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1909 | ec = malloc(sizeof *ec); |
| 1910 | if (ec == NULL) |
| 1911 | return NULL; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1912 | memset(ec, 0, sizeof *ec); |
Daniel Stone | 725c2c3 | 2012-06-22 14:04:36 +0100 | [diff] [blame] | 1913 | |
| 1914 | if (weston_compositor_init(&ec->base, display, argc, argv, |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 1915 | config_file) < 0) { |
| 1916 | weston_log("weston_compositor_init failed\n"); |
| 1917 | goto err_base; |
| 1918 | } |
Daniel Stone | 725c2c3 | 2012-06-22 14:04:36 +0100 | [diff] [blame] | 1919 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1920 | ec->udev = udev_new(); |
| 1921 | if (ec->udev == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1922 | weston_log("failed to initialize udev context\n"); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 1923 | goto err_compositor; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1924 | } |
| 1925 | |
Kristian Høgsberg | c5b9ddb | 2012-01-15 14:29:09 -0500 | [diff] [blame] | 1926 | ec->base.wl_display = display; |
| 1927 | ec->tty = tty_create(&ec->base, vt_func, tty); |
| 1928 | if (!ec->tty) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1929 | weston_log("failed to initialize tty\n"); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 1930 | goto err_udev; |
Kristian Høgsberg | c5b9ddb | 2012-01-15 14:29:09 -0500 | [diff] [blame] | 1931 | } |
| 1932 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1933 | e = udev_enumerate_new(ec->udev); |
| 1934 | udev_enumerate_add_match_subsystem(e, "drm"); |
Benjamin Franzke | a764ee5 | 2011-10-07 08:23:22 +0200 | [diff] [blame] | 1935 | udev_enumerate_add_match_sysname(e, "card[0-9]*"); |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 1936 | |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1937 | udev_enumerate_scan_devices(e); |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 1938 | drm_device = NULL; |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1939 | udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1940 | path = udev_list_entry_get_name(entry); |
| 1941 | device = udev_device_new_from_syspath(ec->udev, path); |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1942 | device_seat = |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 1943 | udev_device_get_property_value(device, "ID_SEAT"); |
| 1944 | if (!device_seat) |
| 1945 | device_seat = default_seat; |
| 1946 | if (strcmp(device_seat, seat) == 0) { |
| 1947 | drm_device = device; |
| 1948 | break; |
| 1949 | } |
| 1950 | udev_device_unref(device); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1951 | } |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 1952 | |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 1953 | if (drm_device == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1954 | weston_log("no drm device found\n"); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 1955 | goto err_udev_enum; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1956 | } |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1957 | |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 1958 | if (init_egl(ec, drm_device) < 0) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1959 | weston_log("failed to initialize egl\n"); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 1960 | goto err_udev_dev; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1961 | } |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 1962 | |
| 1963 | ec->base.destroy = drm_destroy; |
Kristian Høgsberg | 7b884bc | 2012-07-31 14:32:01 -0400 | [diff] [blame^] | 1964 | ec->base.restore = drm_restore; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 1965 | |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 1966 | ec->base.focus = 1; |
| 1967 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1968 | ec->prev_state = WESTON_COMPOSITOR_ACTIVE; |
Pekka Paalanen | bce2d3f | 2011-12-02 13:07:27 +0200 | [diff] [blame] | 1969 | |
Daniel Stone | 725c2c3 | 2012-06-22 14:04:36 +0100 | [diff] [blame] | 1970 | if (weston_compositor_init_gl(&ec->base) < 0) |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 1971 | goto err_egl; |
Benjamin Franzke | d59eb1c | 2011-04-29 22:14:54 +0200 | [diff] [blame] | 1972 | |
Kristian Høgsberg | 5d1c0c5 | 2012-04-10 00:11:50 -0400 | [diff] [blame] | 1973 | for (key = KEY_F1; key < KEY_F9; key++) |
Daniel Stone | 325fc2d | 2012-05-30 16:31:58 +0100 | [diff] [blame] | 1974 | weston_compositor_add_key_binding(&ec->base, key, |
| 1975 | MODIFIER_CTRL | MODIFIER_ALT, |
| 1976 | switch_vt_binding, ec); |
Kristian Høgsberg | 5d1c0c5 | 2012-04-10 00:11:50 -0400 | [diff] [blame] | 1977 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1978 | wl_list_init(&ec->sprite_list); |
| 1979 | create_sprites(ec); |
| 1980 | |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1981 | if (create_outputs(ec, connector, drm_device) < 0) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1982 | weston_log("failed to create output for %s\n", path); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 1983 | goto err_sprite; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1984 | } |
| 1985 | |
Benjamin Franzke | 02dee2c | 2011-10-07 08:27:26 +0200 | [diff] [blame] | 1986 | path = NULL; |
| 1987 | |
Tiago Vignatti | ce03ec3 | 2011-12-19 01:14:03 +0200 | [diff] [blame] | 1988 | evdev_input_create(&ec->base, ec->udev, seat); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1989 | |
| 1990 | loop = wl_display_get_event_loop(ec->base.wl_display); |
| 1991 | ec->drm_source = |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 1992 | wl_event_loop_add_fd(loop, ec->drm.fd, |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1993 | WL_EVENT_READABLE, on_drm_input, ec); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1994 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1995 | ec->udev_monitor = udev_monitor_new_from_netlink(ec->udev, "udev"); |
| 1996 | if (ec->udev_monitor == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1997 | weston_log("failed to intialize udev monitor\n"); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 1998 | goto err_drm_source; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1999 | } |
| 2000 | udev_monitor_filter_add_match_subsystem_devtype(ec->udev_monitor, |
| 2001 | "drm", NULL); |
| 2002 | ec->udev_drm_source = |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 2003 | wl_event_loop_add_fd(loop, |
| 2004 | udev_monitor_get_fd(ec->udev_monitor), |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2005 | WL_EVENT_READABLE, udev_drm_event, ec); |
| 2006 | |
| 2007 | if (udev_monitor_enable_receiving(ec->udev_monitor) < 0) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 2008 | weston_log("failed to enable udev-monitor receiving\n"); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 2009 | goto err_udev_monitor; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2010 | } |
| 2011 | |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 2012 | udev_device_unref(drm_device); |
| 2013 | udev_enumerate_unref(e); |
| 2014 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 2015 | return &ec->base; |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 2016 | |
| 2017 | err_udev_monitor: |
| 2018 | wl_event_source_remove(ec->udev_drm_source); |
| 2019 | udev_monitor_unref(ec->udev_monitor); |
| 2020 | err_drm_source: |
| 2021 | wl_event_source_remove(ec->drm_source); |
| 2022 | wl_list_for_each_safe(weston_seat, next, &ec->base.seat_list, link) |
| 2023 | evdev_input_destroy(weston_seat); |
| 2024 | err_sprite: |
| 2025 | destroy_sprites(ec); |
| 2026 | err_egl: |
| 2027 | eglMakeCurrent(ec->base.egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, |
| 2028 | EGL_NO_CONTEXT); |
| 2029 | eglTerminate(ec->base.egl_display); |
| 2030 | eglReleaseThread(); |
| 2031 | gbm_device_destroy(ec->gbm); |
| 2032 | err_udev_dev: |
| 2033 | udev_device_unref(drm_device); |
| 2034 | err_udev_enum: |
| 2035 | udev_enumerate_unref(e); |
| 2036 | tty_destroy(ec->tty); |
| 2037 | err_udev: |
| 2038 | udev_unref(ec->udev); |
| 2039 | err_compositor: |
| 2040 | weston_compositor_shutdown(&ec->base); |
| 2041 | err_base: |
| 2042 | free(ec); |
| 2043 | return NULL; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 2044 | } |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 2045 | |
Scott Moreau | 8ab5d45 | 2012-07-30 19:51:08 -0600 | [diff] [blame] | 2046 | static void |
| 2047 | output_section_done(void *data) |
| 2048 | { |
| 2049 | struct drm_configured_output *output; |
| 2050 | |
| 2051 | output = malloc(sizeof *output); |
| 2052 | |
| 2053 | if (!output) |
| 2054 | return; |
| 2055 | |
| 2056 | output->config = OUTPUT_CONFIG_INVALID; |
| 2057 | output->name = output_name; |
| 2058 | output->mode = output_mode; |
| 2059 | |
| 2060 | if (strcmp(output_mode, "off") == 0) |
| 2061 | output->config = OUTPUT_CONFIG_OFF; |
| 2062 | else if (strcmp(output_mode, "preferred") == 0) |
| 2063 | output->config = OUTPUT_CONFIG_PREFERRED; |
| 2064 | else if (strcmp(output_mode, "current") == 0) |
| 2065 | output->config = OUTPUT_CONFIG_CURRENT; |
| 2066 | else if (sscanf(output_mode, "%dx%d", &output->width, &output->height) == 2) |
| 2067 | output->config = OUTPUT_CONFIG_MODE; |
| 2068 | |
| 2069 | if (output->config != OUTPUT_CONFIG_INVALID) |
| 2070 | wl_list_insert(&configured_output_list, &output->link); |
| 2071 | else { |
| 2072 | free(output); |
| 2073 | weston_log("Invalid mode \"%s\" for output %s\n", |
| 2074 | output_mode, output_name); |
| 2075 | } |
| 2076 | } |
| 2077 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 2078 | WL_EXPORT struct weston_compositor * |
Daniel Stone | c1be8e5 | 2012-06-01 11:14:02 -0400 | [diff] [blame] | 2079 | backend_init(struct wl_display *display, int argc, char *argv[], |
| 2080 | const char *config_file) |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 2081 | { |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 2082 | int connector = 0, tty = 0; |
| 2083 | const char *seat = default_seat; |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 2084 | |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 2085 | const struct weston_option drm_options[] = { |
| 2086 | { WESTON_OPTION_INTEGER, "connector", 0, &connector }, |
| 2087 | { WESTON_OPTION_STRING, "seat", 0, &seat }, |
| 2088 | { WESTON_OPTION_INTEGER, "tty", 0, &tty }, |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 2089 | { WESTON_OPTION_BOOLEAN, "current-mode", 0, &option_current_mode }, |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 2090 | }; |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 2091 | |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 2092 | parse_options(drm_options, ARRAY_LENGTH(drm_options), argc, argv); |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 2093 | |
Scott Moreau | 8ab5d45 | 2012-07-30 19:51:08 -0600 | [diff] [blame] | 2094 | wl_list_init(&configured_output_list); |
| 2095 | |
| 2096 | const struct config_key drm_config_keys[] = { |
| 2097 | { "name", CONFIG_KEY_STRING, &output_name }, |
| 2098 | { "mode", CONFIG_KEY_STRING, &output_mode }, |
| 2099 | }; |
| 2100 | |
| 2101 | const struct config_section config_section[] = { |
| 2102 | { "output", drm_config_keys, |
| 2103 | ARRAY_LENGTH(drm_config_keys), output_section_done }, |
| 2104 | }; |
| 2105 | |
| 2106 | parse_config_file(config_file, config_section, |
| 2107 | ARRAY_LENGTH(config_section), NULL); |
| 2108 | |
Daniel Stone | c1be8e5 | 2012-06-01 11:14:02 -0400 | [diff] [blame] | 2109 | return drm_compositor_create(display, connector, seat, tty, argc, argv, |
| 2110 | config_file); |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 2111 | } |