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 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
| 29 | #include <fcntl.h> |
| 30 | #include <unistd.h> |
| 31 | |
Benjamin Franzke | c649a92 | 2011-03-02 11:56:04 +0100 | [diff] [blame] | 32 | #include <xf86drm.h> |
| 33 | #include <xf86drmMode.h> |
| 34 | |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 35 | #include <gbm.h> |
| 36 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 37 | #include "compositor.h" |
Tiago Vignatti | ce03ec3 | 2011-12-19 01:14:03 +0200 | [diff] [blame] | 38 | #include "evdev.h" |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 39 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 40 | struct drm_compositor { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 41 | struct weston_compositor base; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 42 | |
| 43 | struct udev *udev; |
| 44 | struct wl_event_source *drm_source; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 45 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 46 | struct udev_monitor *udev_monitor; |
| 47 | struct wl_event_source *udev_drm_source; |
| 48 | |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 49 | struct { |
| 50 | int fd; |
| 51 | } drm; |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 52 | struct gbm_device *gbm; |
Marty Jack | 13d9db2 | 2011-02-09 19:01:42 -0500 | [diff] [blame] | 53 | uint32_t crtc_allocator; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 54 | uint32_t connector_allocator; |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 55 | struct tty *tty; |
Pekka Paalanen | bce2d3f | 2011-12-02 13:07:27 +0200 | [diff] [blame] | 56 | |
| 57 | uint32_t prev_state; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 58 | }; |
| 59 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 60 | struct drm_mode { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 61 | struct weston_mode base; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 62 | drmModeModeInfo mode_info; |
| 63 | }; |
| 64 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 65 | struct drm_output { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 66 | struct weston_output base; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 67 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 68 | uint32_t crtc_id; |
| 69 | uint32_t connector_id; |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 70 | drmModeCrtcPtr original_crtc; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 71 | GLuint rbo[2]; |
| 72 | uint32_t fb_id[2]; |
| 73 | EGLImageKHR image[2]; |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 74 | struct gbm_bo *bo[2]; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 75 | uint32_t current; |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 76 | |
| 77 | uint32_t fs_surf_fb_id; |
| 78 | uint32_t pending_fs_surf_fb_id; |
Kristian Høgsberg | 9f404b7 | 2012-01-26 00:11:01 -0500 | [diff] [blame^] | 79 | struct wl_buffer *scanout_buffer; |
| 80 | struct wl_listener scanout_buffer_destroy_listener; |
| 81 | struct wl_buffer *pending_scanout_buffer; |
| 82 | struct wl_listener pending_scanout_buffer_destroy_listener; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 83 | }; |
| 84 | |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 85 | static int |
| 86 | drm_output_prepare_scanout_surface(struct drm_output *output) |
| 87 | { |
| 88 | struct drm_compositor *c = |
| 89 | (struct drm_compositor *) output->base.compositor; |
| 90 | struct weston_surface *es; |
| 91 | EGLint handle, stride; |
| 92 | int ret; |
| 93 | uint32_t fb_id = 0; |
| 94 | struct gbm_bo *bo; |
| 95 | |
| 96 | es = container_of(c->base.surface_list.next, |
| 97 | struct weston_surface, link); |
| 98 | |
| 99 | if (es->visual != WESTON_RGB_VISUAL || |
| 100 | es->x != output->base.x || |
| 101 | es->y != output->base.y || |
| 102 | es->width != output->base.current->width || |
| 103 | es->height != output->base.current->height || |
| 104 | es->image == EGL_NO_IMAGE_KHR) |
| 105 | return -1; |
| 106 | |
| 107 | bo = gbm_bo_create_from_egl_image(c->gbm, |
| 108 | c->base.display, es->image, |
| 109 | es->width, es->height, |
| 110 | GBM_BO_USE_SCANOUT); |
| 111 | |
| 112 | handle = gbm_bo_get_handle(bo).s32; |
| 113 | stride = gbm_bo_get_pitch(bo); |
| 114 | |
| 115 | gbm_bo_destroy(bo); |
| 116 | |
| 117 | if (handle == 0) |
| 118 | return -1; |
| 119 | |
| 120 | ret = drmModeAddFB(c->drm.fd, |
| 121 | output->base.current->width, |
| 122 | output->base.current->height, |
| 123 | 24, 32, stride, handle, &fb_id); |
| 124 | |
| 125 | if (ret) |
| 126 | return -1; |
| 127 | |
| 128 | output->pending_fs_surf_fb_id = fb_id; |
| 129 | |
| 130 | /* assert output->pending_scanout_buffer == NULL */ |
Kristian Høgsberg | 9f404b7 | 2012-01-26 00:11:01 -0500 | [diff] [blame^] | 131 | output->pending_scanout_buffer = es->buffer; |
| 132 | output->pending_scanout_buffer->busy_count++; |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 133 | |
Kristian Høgsberg | 9f404b7 | 2012-01-26 00:11:01 -0500 | [diff] [blame^] | 134 | wl_list_insert(output->pending_scanout_buffer->resource.destroy_listener_list.prev, |
| 135 | &output->pending_scanout_buffer_destroy_listener.link); |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 136 | |
| 137 | pixman_region32_fini(&es->damage); |
| 138 | pixman_region32_init(&es->damage); |
| 139 | |
| 140 | return 0; |
| 141 | } |
| 142 | |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 143 | static void |
| 144 | drm_output_repaint(struct weston_output *output_base) |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 145 | { |
| 146 | struct drm_output *output = (struct drm_output *) output_base; |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 147 | struct drm_compositor *compositor = |
| 148 | (struct drm_compositor *) output->base.compositor; |
| 149 | struct weston_surface *surface; |
| 150 | uint32_t fb_id = 0; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 151 | |
| 152 | glFramebufferRenderbuffer(GL_FRAMEBUFFER, |
| 153 | GL_COLOR_ATTACHMENT0, |
| 154 | GL_RENDERBUFFER, |
| 155 | output->rbo[output->current]); |
| 156 | |
| 157 | if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 158 | return; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 159 | |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 160 | drm_output_prepare_scanout_surface(output); |
| 161 | |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 162 | wl_list_for_each_reverse(surface, &compositor->base.surface_list, link) |
| 163 | weston_surface_draw(surface, &output->base); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 164 | |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 165 | glFlush(); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 166 | |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 167 | output->current ^= 1; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 168 | |
Kristian Høgsberg | 191454e | 2011-10-20 17:51:45 -0400 | [diff] [blame] | 169 | if (output->pending_fs_surf_fb_id != 0) { |
| 170 | fb_id = output->pending_fs_surf_fb_id; |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 171 | } else { |
| 172 | fb_id = output->fb_id[output->current ^ 1]; |
| 173 | } |
| 174 | |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 175 | if (drmModePageFlip(compositor->drm.fd, output->crtc_id, |
Kristian Høgsberg | 54f14c3 | 2012-01-18 11:47:41 -0500 | [diff] [blame] | 176 | fb_id, |
| 177 | DRM_MODE_PAGE_FLIP_EVENT, output) < 0) { |
| 178 | fprintf(stderr, "queueing pageflip failed: %m\n"); |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 179 | return; |
Kristian Høgsberg | 54f14c3 | 2012-01-18 11:47:41 -0500 | [diff] [blame] | 180 | } |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 181 | |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 182 | return; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | static void |
| 186 | page_flip_handler(int fd, unsigned int frame, |
| 187 | unsigned int sec, unsigned int usec, void *data) |
| 188 | { |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 189 | struct drm_output *output = (struct drm_output *) data; |
| 190 | struct drm_compositor *c = |
| 191 | (struct drm_compositor *) output->base.compositor; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 192 | uint32_t msecs; |
| 193 | |
Kristian Høgsberg | 9f404b7 | 2012-01-26 00:11:01 -0500 | [diff] [blame^] | 194 | if (output->scanout_buffer) { |
| 195 | weston_buffer_post_release(output->scanout_buffer); |
| 196 | wl_list_remove(&output->scanout_buffer_destroy_listener.link); |
| 197 | output->scanout_buffer = NULL; |
Kristian Høgsberg | 191454e | 2011-10-20 17:51:45 -0400 | [diff] [blame] | 198 | drmModeRmFB(c->drm.fd, output->fs_surf_fb_id); |
| 199 | output->fs_surf_fb_id = 0; |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 200 | } |
| 201 | |
Kristian Høgsberg | 9f404b7 | 2012-01-26 00:11:01 -0500 | [diff] [blame^] | 202 | if (output->pending_scanout_buffer) { |
| 203 | output->scanout_buffer = output->pending_scanout_buffer; |
| 204 | wl_list_remove(&output->pending_scanout_buffer_destroy_listener.link); |
| 205 | wl_list_insert(output->scanout_buffer->resource.destroy_listener_list.prev, |
| 206 | &output->scanout_buffer_destroy_listener.link); |
| 207 | output->pending_scanout_buffer = NULL; |
Kristian Høgsberg | 191454e | 2011-10-20 17:51:45 -0400 | [diff] [blame] | 208 | output->fs_surf_fb_id = output->pending_fs_surf_fb_id; |
| 209 | output->pending_fs_surf_fb_id = 0; |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 210 | } |
| 211 | |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 212 | msecs = sec * 1000 + usec / 1000; |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 213 | weston_output_finish_frame(&output->base, msecs); |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | static int |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 217 | drm_output_set_cursor(struct weston_output *output_base, |
| 218 | struct weston_input_device *eid) |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 219 | { |
| 220 | struct drm_output *output = (struct drm_output *) output_base; |
| 221 | struct drm_compositor *c = |
| 222 | (struct drm_compositor *) output->base.compositor; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 223 | EGLint handle, stride; |
| 224 | int ret = -1; |
| 225 | pixman_region32_t cursor_region; |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 226 | struct gbm_bo *bo; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 227 | |
Kristian Høgsberg | e4c40a4 | 2011-05-06 14:04:21 -0400 | [diff] [blame] | 228 | if (eid == NULL) { |
| 229 | drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0); |
| 230 | return 0; |
| 231 | } |
| 232 | |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 233 | pixman_region32_init_rect(&cursor_region, |
| 234 | eid->sprite->x, eid->sprite->y, |
| 235 | eid->sprite->width, eid->sprite->height); |
| 236 | |
| 237 | pixman_region32_intersect_rect(&cursor_region, &cursor_region, |
| 238 | output->base.x, output->base.y, |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 239 | output->base.current->width, |
| 240 | output->base.current->height); |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 241 | |
Benjamin Franzke | a8bdeae | 2011-06-28 22:56:43 +0200 | [diff] [blame] | 242 | if (!pixman_region32_not_empty(&cursor_region)) |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 243 | goto out; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 244 | |
| 245 | if (eid->sprite->image == EGL_NO_IMAGE_KHR) |
| 246 | goto out; |
| 247 | |
| 248 | if (eid->sprite->width > 64 || eid->sprite->height > 64) |
| 249 | goto out; |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 250 | |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 251 | bo = gbm_bo_create_from_egl_image(c->gbm, |
| 252 | c->base.display, |
| 253 | eid->sprite->image, 64, 64, |
| 254 | GBM_BO_USE_CURSOR_64X64); |
Kristian Høgsberg | 6916d9c | 2012-01-18 15:11:07 -0500 | [diff] [blame] | 255 | /* Not suitable for hw cursor, fall back */ |
| 256 | if (bo == NULL) |
| 257 | goto out; |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 258 | |
| 259 | handle = gbm_bo_get_handle(bo).s32; |
| 260 | stride = gbm_bo_get_pitch(bo); |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 261 | gbm_bo_destroy(bo); |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 262 | |
Kristian Høgsberg | 6916d9c | 2012-01-18 15:11:07 -0500 | [diff] [blame] | 263 | /* gbm_bo_create_from_egl_image() didn't always validate the usage |
| 264 | * flags, and in that case we might end up with a bad stride. */ |
| 265 | if (stride != 64 * 4) |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 266 | goto out; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 267 | |
| 268 | ret = drmModeSetCursor(c->drm.fd, output->crtc_id, handle, 64, 64); |
| 269 | if (ret) { |
| 270 | fprintf(stderr, "failed to set cursor: %s\n", strerror(-ret)); |
| 271 | goto out; |
| 272 | } |
| 273 | |
| 274 | ret = drmModeMoveCursor(c->drm.fd, output->crtc_id, |
| 275 | eid->sprite->x - output->base.x, |
| 276 | eid->sprite->y - output->base.y); |
| 277 | if (ret) { |
| 278 | fprintf(stderr, "failed to move cursor: %s\n", strerror(-ret)); |
| 279 | goto out; |
| 280 | } |
| 281 | |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 282 | out: |
| 283 | pixman_region32_fini(&cursor_region); |
| 284 | if (ret) |
| 285 | drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0); |
| 286 | return ret; |
| 287 | } |
| 288 | |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 289 | static void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 290 | drm_output_destroy(struct weston_output *output_base) |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 291 | { |
| 292 | struct drm_output *output = (struct drm_output *) output_base; |
| 293 | struct drm_compositor *c = |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 294 | (struct drm_compositor *) output->base.compositor; |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 295 | drmModeCrtcPtr origcrtc = output->original_crtc; |
| 296 | int i; |
| 297 | |
| 298 | /* Turn off hardware cursor */ |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 299 | drm_output_set_cursor(&output->base, NULL); |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 300 | |
| 301 | /* Restore original CRTC state */ |
| 302 | drmModeSetCrtc(c->drm.fd, origcrtc->crtc_id, origcrtc->buffer_id, |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 303 | origcrtc->x, origcrtc->y, |
| 304 | &output->connector_id, 1, &origcrtc->mode); |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 305 | drmModeFreeCrtc(origcrtc); |
| 306 | |
Benjamin Franzke | 48c4ea2 | 2011-08-30 11:44:56 +0200 | [diff] [blame] | 307 | glFramebufferRenderbuffer(GL_FRAMEBUFFER, |
| 308 | GL_COLOR_ATTACHMENT0, |
| 309 | GL_RENDERBUFFER, |
| 310 | 0); |
| 311 | |
| 312 | glBindRenderbuffer(GL_RENDERBUFFER, 0); |
| 313 | glDeleteRenderbuffers(2, output->rbo); |
| 314 | |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 315 | /* Destroy output buffers */ |
| 316 | for (i = 0; i < 2; i++) { |
| 317 | drmModeRmFB(c->drm.fd, output->fb_id[i]); |
| 318 | c->base.destroy_image(c->base.display, output->image[i]); |
| 319 | gbm_bo_destroy(output->bo[i]); |
| 320 | } |
| 321 | |
Benjamin Franzke | 48c4ea2 | 2011-08-30 11:44:56 +0200 | [diff] [blame] | 322 | c->crtc_allocator &= ~(1 << output->crtc_id); |
| 323 | c->connector_allocator &= ~(1 << output->connector_id); |
| 324 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 325 | weston_output_destroy(&output->base); |
Benjamin Franzke | 48c4ea2 | 2011-08-30 11:44:56 +0200 | [diff] [blame] | 326 | wl_list_remove(&output->base.link); |
| 327 | |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 328 | free(output); |
| 329 | } |
| 330 | |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 331 | static int |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 332 | on_drm_input(int fd, uint32_t mask, void *data) |
| 333 | { |
| 334 | drmEventContext evctx; |
| 335 | |
| 336 | memset(&evctx, 0, sizeof evctx); |
| 337 | evctx.version = DRM_EVENT_CONTEXT_VERSION; |
| 338 | evctx.page_flip_handler = page_flip_handler; |
| 339 | drmHandleEvent(fd, &evctx); |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 340 | |
| 341 | return 1; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | static int |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 345 | init_egl(struct drm_compositor *ec, struct udev_device *device) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 346 | { |
Kristian Høgsberg | 379b678 | 2010-07-28 22:52:28 -0400 | [diff] [blame] | 347 | EGLint major, minor; |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 348 | const char *extensions, *filename; |
| 349 | int fd; |
Kristian Høgsberg | 2c28aa5 | 2010-07-28 23:47:54 -0400 | [diff] [blame] | 350 | static const EGLint context_attribs[] = { |
| 351 | EGL_CONTEXT_CLIENT_VERSION, 2, |
| 352 | EGL_NONE |
| 353 | }; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 354 | |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 355 | filename = udev_device_get_devnode(device); |
David Herrmann | 63ff706 | 2011-11-05 18:46:01 +0100 | [diff] [blame] | 356 | fd = open(filename, O_RDWR | O_CLOEXEC); |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 357 | if (fd < 0) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 358 | /* Probably permissions error */ |
| 359 | fprintf(stderr, "couldn't open %s, skipping\n", |
| 360 | udev_device_get_devnode(device)); |
| 361 | return -1; |
| 362 | } |
| 363 | |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 364 | ec->drm.fd = fd; |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 365 | ec->gbm = gbm_create_device(ec->drm.fd); |
| 366 | ec->base.display = eglGetDisplay(ec->gbm); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 367 | if (ec->base.display == NULL) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 368 | fprintf(stderr, "failed to create display\n"); |
| 369 | return -1; |
| 370 | } |
| 371 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 372 | if (!eglInitialize(ec->base.display, &major, &minor)) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 373 | fprintf(stderr, "failed to initialize display\n"); |
| 374 | return -1; |
| 375 | } |
| 376 | |
Kristian Høgsberg | 379b678 | 2010-07-28 22:52:28 -0400 | [diff] [blame] | 377 | extensions = eglQueryString(ec->base.display, EGL_EXTENSIONS); |
Ander Conselvan de Oliveira | ef7c8d9 | 2011-11-01 16:37:41 +0200 | [diff] [blame] | 378 | if (!strstr(extensions, "EGL_KHR_surfaceless_gles2")) { |
| 379 | fprintf(stderr, "EGL_KHR_surfaceless_gles2 not available\n"); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 380 | return -1; |
| 381 | } |
| 382 | |
Darxus | 55973f2 | 2010-11-22 21:24:39 -0500 | [diff] [blame] | 383 | if (!eglBindAPI(EGL_OPENGL_ES_API)) { |
| 384 | fprintf(stderr, "failed to bind api EGL_OPENGL_ES_API\n"); |
| 385 | return -1; |
| 386 | } |
| 387 | |
Kristian Høgsberg | 2c28aa5 | 2010-07-28 23:47:54 -0400 | [diff] [blame] | 388 | ec->base.context = eglCreateContext(ec->base.display, NULL, |
| 389 | EGL_NO_CONTEXT, context_attribs); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 390 | if (ec->base.context == NULL) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 391 | fprintf(stderr, "failed to create context\n"); |
| 392 | return -1; |
| 393 | } |
| 394 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 395 | if (!eglMakeCurrent(ec->base.display, EGL_NO_SURFACE, |
| 396 | EGL_NO_SURFACE, ec->base.context)) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 397 | fprintf(stderr, "failed to make context current\n"); |
| 398 | return -1; |
| 399 | } |
| 400 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 401 | return 0; |
| 402 | } |
| 403 | |
| 404 | static drmModeModeInfo builtin_1024x768 = { |
| 405 | 63500, /* clock */ |
| 406 | 1024, 1072, 1176, 1328, 0, |
| 407 | 768, 771, 775, 798, 0, |
| 408 | 59920, |
| 409 | DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC, |
| 410 | 0, |
| 411 | "1024x768" |
| 412 | }; |
| 413 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 414 | |
| 415 | static int |
| 416 | drm_output_add_mode(struct drm_output *output, drmModeModeInfo *info) |
| 417 | { |
| 418 | struct drm_mode *mode; |
| 419 | |
| 420 | mode = malloc(sizeof *mode); |
| 421 | if (mode == NULL) |
| 422 | return -1; |
| 423 | |
| 424 | mode->base.flags = 0; |
| 425 | mode->base.width = info->hdisplay; |
| 426 | mode->base.height = info->vdisplay; |
| 427 | mode->base.refresh = info->vrefresh; |
| 428 | mode->mode_info = *info; |
| 429 | wl_list_insert(output->base.mode_list.prev, &mode->base.link); |
| 430 | |
| 431 | return 0; |
| 432 | } |
| 433 | |
| 434 | static int |
| 435 | drm_subpixel_to_wayland(int drm_value) |
| 436 | { |
| 437 | switch (drm_value) { |
| 438 | default: |
| 439 | case DRM_MODE_SUBPIXEL_UNKNOWN: |
| 440 | return WL_OUTPUT_SUBPIXEL_UNKNOWN; |
| 441 | case DRM_MODE_SUBPIXEL_NONE: |
| 442 | return WL_OUTPUT_SUBPIXEL_NONE; |
| 443 | case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB: |
| 444 | return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB; |
| 445 | case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR: |
| 446 | return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR; |
| 447 | case DRM_MODE_SUBPIXEL_VERTICAL_RGB: |
| 448 | return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB; |
| 449 | case DRM_MODE_SUBPIXEL_VERTICAL_BGR: |
| 450 | return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR; |
| 451 | } |
| 452 | } |
| 453 | |
Kristian Høgsberg | 9f404b7 | 2012-01-26 00:11:01 -0500 | [diff] [blame^] | 454 | static void |
| 455 | output_handle_scanout_buffer_destroy(struct wl_listener *listener, |
| 456 | struct wl_resource *resource, |
| 457 | uint32_t time) |
| 458 | { |
| 459 | struct drm_output *output = |
| 460 | container_of(listener, struct drm_output, |
| 461 | scanout_buffer_destroy_listener); |
| 462 | |
| 463 | output->scanout_buffer = NULL; |
| 464 | |
| 465 | if (!output->pending_scanout_buffer) |
| 466 | weston_compositor_schedule_repaint(output->base.compositor); |
| 467 | } |
| 468 | |
| 469 | static void |
| 470 | output_handle_pending_scanout_buffer_destroy(struct wl_listener *listener, |
| 471 | struct wl_resource *resource, |
| 472 | uint32_t time) |
| 473 | { |
| 474 | struct drm_output *output = |
| 475 | container_of(listener, struct drm_output, |
| 476 | pending_scanout_buffer_destroy_listener); |
| 477 | |
| 478 | output->pending_scanout_buffer = NULL; |
| 479 | |
| 480 | weston_compositor_schedule_repaint(output->base.compositor); |
| 481 | } |
| 482 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 483 | static int |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 484 | create_output_for_connector(struct drm_compositor *ec, |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 485 | drmModeRes *resources, |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 486 | drmModeConnector *connector, |
| 487 | int x, int y) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 488 | { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 489 | struct drm_output *output; |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 490 | struct drm_mode *drm_mode, *next; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 491 | drmModeEncoder *encoder; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 492 | int i, ret; |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 493 | unsigned handle, stride; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 494 | |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 495 | encoder = drmModeGetEncoder(ec->drm.fd, connector->encoders[0]); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 496 | if (encoder == NULL) { |
| 497 | fprintf(stderr, "No encoder for connector.\n"); |
| 498 | return -1; |
| 499 | } |
| 500 | |
| 501 | for (i = 0; i < resources->count_crtcs; i++) { |
Marty Jack | 13d9db2 | 2011-02-09 19:01:42 -0500 | [diff] [blame] | 502 | if (encoder->possible_crtcs & (1 << i) && |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 503 | !(ec->crtc_allocator & (1 << resources->crtcs[i]))) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 504 | break; |
| 505 | } |
| 506 | if (i == resources->count_crtcs) { |
| 507 | fprintf(stderr, "No usable crtc for encoder.\n"); |
David Herrmann | eb8bed5 | 2011-12-08 17:05:44 +0100 | [diff] [blame] | 508 | drmModeFreeEncoder(encoder); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 509 | return -1; |
| 510 | } |
| 511 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 512 | output = malloc(sizeof *output); |
David Herrmann | eb8bed5 | 2011-12-08 17:05:44 +0100 | [diff] [blame] | 513 | if (output == NULL) { |
| 514 | drmModeFreeEncoder(encoder); |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 515 | return -1; |
David Herrmann | eb8bed5 | 2011-12-08 17:05:44 +0100 | [diff] [blame] | 516 | } |
David Herrmann | d7c4551 | 2011-12-08 17:05:46 +0100 | [diff] [blame] | 517 | output->fb_id[0] = -1; |
| 518 | output->fb_id[1] = -1; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 519 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 520 | memset(output, 0, sizeof *output); |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 521 | output->base.subpixel = drm_subpixel_to_wayland(connector->subpixel); |
| 522 | output->base.make = "unknown"; |
| 523 | output->base.model = "unknown"; |
| 524 | wl_list_init(&output->base.mode_list); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 525 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 526 | output->crtc_id = resources->crtcs[i]; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 527 | ec->crtc_allocator |= (1 << output->crtc_id); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 528 | output->connector_id = connector->connector_id; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 529 | ec->connector_allocator |= (1 << output->connector_id); |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 530 | |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 531 | output->original_crtc = drmModeGetCrtc(ec->drm.fd, output->crtc_id); |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 532 | drmModeFreeEncoder(encoder); |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 533 | |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 534 | for (i = 0; i < connector->count_modes; i++) { |
| 535 | ret = drm_output_add_mode(output, &connector->modes[i]); |
| 536 | if (ret) |
| 537 | goto err_free; |
| 538 | } |
| 539 | |
| 540 | if (connector->count_modes == 0) { |
| 541 | ret = drm_output_add_mode(output, &builtin_1024x768); |
| 542 | if (ret) |
| 543 | goto err_free; |
| 544 | } |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 545 | |
| 546 | drm_mode = container_of(output->base.mode_list.next, |
| 547 | struct drm_mode, base.link); |
| 548 | output->base.current = &drm_mode->base; |
| 549 | drm_mode->base.flags = |
| 550 | WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED; |
| 551 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 552 | glGenRenderbuffers(2, output->rbo); |
| 553 | for (i = 0; i < 2; i++) { |
| 554 | glBindRenderbuffer(GL_RENDERBUFFER, output->rbo[i]); |
| 555 | |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 556 | output->bo[i] = |
| 557 | gbm_bo_create(ec->gbm, |
| 558 | output->base.current->width, |
| 559 | output->base.current->height, |
| 560 | GBM_BO_FORMAT_XRGB8888, |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 561 | GBM_BO_USE_SCANOUT | |
| 562 | GBM_BO_USE_RENDERING); |
David Herrmann | d7c4551 | 2011-12-08 17:05:46 +0100 | [diff] [blame] | 563 | if (!output->bo[i]) |
| 564 | goto err_bufs; |
| 565 | |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 566 | output->image[i] = ec->base.create_image(ec->base.display, |
| 567 | NULL, |
| 568 | EGL_NATIVE_PIXMAP_KHR, |
| 569 | output->bo[i], NULL); |
David Herrmann | d7c4551 | 2011-12-08 17:05:46 +0100 | [diff] [blame] | 570 | if (!output->image[i]) |
| 571 | goto err_bufs; |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 572 | |
Kristian Høgsberg | b5819dc | 2011-04-25 15:08:20 -0400 | [diff] [blame] | 573 | ec->base.image_target_renderbuffer_storage(GL_RENDERBUFFER, |
| 574 | output->image[i]); |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 575 | stride = gbm_bo_get_pitch(output->bo[i]); |
| 576 | handle = gbm_bo_get_handle(output->bo[i]).u32; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 577 | |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 578 | ret = drmModeAddFB(ec->drm.fd, |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 579 | output->base.current->width, |
| 580 | output->base.current->height, |
Kristian Høgsberg | 9c46ff1 | 2011-10-24 13:13:22 -0400 | [diff] [blame] | 581 | 24, 32, stride, handle, &output->fb_id[i]); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 582 | if (ret) { |
| 583 | fprintf(stderr, "failed to add fb %d: %m\n", i); |
David Herrmann | d7c4551 | 2011-12-08 17:05:46 +0100 | [diff] [blame] | 584 | goto err_bufs; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 585 | } |
| 586 | } |
| 587 | |
| 588 | output->current = 0; |
| 589 | glFramebufferRenderbuffer(GL_FRAMEBUFFER, |
| 590 | GL_COLOR_ATTACHMENT0, |
| 591 | GL_RENDERBUFFER, |
| 592 | output->rbo[output->current]); |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 593 | ret = drmModeSetCrtc(ec->drm.fd, output->crtc_id, |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 594 | output->fb_id[output->current ^ 1], 0, 0, |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 595 | &output->connector_id, 1, |
| 596 | &drm_mode->mode_info); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 597 | if (ret) { |
| 598 | fprintf(stderr, "failed to set mode: %m\n"); |
David Herrmann | d7c4551 | 2011-12-08 17:05:46 +0100 | [diff] [blame] | 599 | goto err_fb; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 600 | } |
| 601 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 602 | weston_output_init(&output->base, &ec->base, x, y, |
Kristian Høgsberg | a4b7e20 | 2011-10-24 13:26:32 -0400 | [diff] [blame] | 603 | connector->mmWidth, connector->mmHeight, 0); |
| 604 | |
| 605 | wl_list_insert(ec->base.output_list.prev, &output->base.link); |
| 606 | |
Kristian Høgsberg | 9f404b7 | 2012-01-26 00:11:01 -0500 | [diff] [blame^] | 607 | output->scanout_buffer_destroy_listener.func = |
| 608 | output_handle_scanout_buffer_destroy; |
| 609 | output->pending_scanout_buffer_destroy_listener.func = |
| 610 | output_handle_pending_scanout_buffer_destroy; |
| 611 | |
Kristian Høgsberg | a4b7e20 | 2011-10-24 13:26:32 -0400 | [diff] [blame] | 612 | output->pending_fs_surf_fb_id = 0; |
Kristian Høgsberg | 68c479a | 2012-01-25 23:32:28 -0500 | [diff] [blame] | 613 | output->base.repaint = drm_output_repaint; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 614 | output->base.set_hardware_cursor = drm_output_set_cursor; |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 615 | output->base.destroy = drm_output_destroy; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 616 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 617 | return 0; |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 618 | |
David Herrmann | d7c4551 | 2011-12-08 17:05:46 +0100 | [diff] [blame] | 619 | err_fb: |
| 620 | glFramebufferRenderbuffer(GL_FRAMEBUFFER, |
| 621 | GL_COLOR_ATTACHMENT0, |
| 622 | GL_RENDERBUFFER, |
| 623 | 0); |
| 624 | err_bufs: |
| 625 | for (i = 0; i < 2; i++) { |
| 626 | if (output->fb_id[i] != -1) |
| 627 | drmModeRmFB(ec->drm.fd, output->fb_id[i]); |
| 628 | if (output->image[i]) |
| 629 | ec->base.destroy_image(ec->base.display, |
| 630 | output->image[i]); |
| 631 | if (output->bo[i]) |
| 632 | gbm_bo_destroy(output->bo[i]); |
| 633 | } |
| 634 | glBindRenderbuffer(GL_RENDERBUFFER, 0); |
| 635 | glDeleteRenderbuffers(2, output->rbo); |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 636 | err_free: |
| 637 | wl_list_for_each_safe(drm_mode, next, &output->base.mode_list, |
| 638 | base.link) { |
| 639 | wl_list_remove(&drm_mode->base.link); |
| 640 | free(drm_mode); |
| 641 | } |
| 642 | |
| 643 | drmModeFreeCrtc(output->original_crtc); |
| 644 | ec->crtc_allocator &= ~(1 << output->crtc_id); |
| 645 | ec->connector_allocator &= ~(1 << output->connector_id); |
| 646 | |
| 647 | free(output); |
| 648 | return -1; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | static int |
Kristian Høgsberg | 61a8251 | 2010-10-26 11:26:44 -0400 | [diff] [blame] | 652 | create_outputs(struct drm_compositor *ec, int option_connector) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 653 | { |
| 654 | drmModeConnector *connector; |
| 655 | drmModeRes *resources; |
| 656 | int i; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 657 | int x = 0, y = 0; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 658 | |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 659 | resources = drmModeGetResources(ec->drm.fd); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 660 | if (!resources) { |
| 661 | fprintf(stderr, "drmModeGetResources failed\n"); |
| 662 | return -1; |
| 663 | } |
| 664 | |
| 665 | for (i = 0; i < resources->count_connectors; i++) { |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 666 | connector = drmModeGetConnector(ec->drm.fd, |
| 667 | resources->connectors[i]); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 668 | if (connector == NULL) |
| 669 | continue; |
| 670 | |
| 671 | if (connector->connection == DRM_MODE_CONNECTED && |
| 672 | (option_connector == 0 || |
Benjamin Franzke | 9eaee35 | 2011-08-02 13:03:54 +0200 | [diff] [blame] | 673 | connector->connector_id == option_connector)) { |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 674 | if (create_output_for_connector(ec, resources, |
Benjamin Franzke | 439d986 | 2011-10-07 08:20:53 +0200 | [diff] [blame] | 675 | connector, x, y) < 0) { |
| 676 | drmModeFreeConnector(connector); |
| 677 | continue; |
| 678 | } |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 679 | |
Benjamin Franzke | 9eaee35 | 2011-08-02 13:03:54 +0200 | [diff] [blame] | 680 | x += container_of(ec->base.output_list.prev, |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 681 | struct weston_output, |
Benjamin Franzke | 9eaee35 | 2011-08-02 13:03:54 +0200 | [diff] [blame] | 682 | link)->current->width; |
| 683 | } |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 684 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 685 | drmModeFreeConnector(connector); |
| 686 | } |
| 687 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 688 | if (wl_list_empty(&ec->base.output_list)) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 689 | fprintf(stderr, "No currently active connector found.\n"); |
| 690 | return -1; |
| 691 | } |
| 692 | |
| 693 | drmModeFreeResources(resources); |
| 694 | |
| 695 | return 0; |
| 696 | } |
| 697 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 698 | static void |
| 699 | update_outputs(struct drm_compositor *ec) |
| 700 | { |
| 701 | drmModeConnector *connector; |
| 702 | drmModeRes *resources; |
| 703 | struct drm_output *output, *next; |
| 704 | int x = 0, y = 0; |
| 705 | int x_offset = 0, y_offset = 0; |
| 706 | uint32_t connected = 0, disconnects = 0; |
| 707 | int i; |
| 708 | |
| 709 | resources = drmModeGetResources(ec->drm.fd); |
| 710 | if (!resources) { |
| 711 | fprintf(stderr, "drmModeGetResources failed\n"); |
| 712 | return; |
| 713 | } |
| 714 | |
| 715 | /* collect new connects */ |
| 716 | for (i = 0; i < resources->count_connectors; i++) { |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 717 | int connector_id = resources->connectors[i]; |
| 718 | |
| 719 | connector = drmModeGetConnector(ec->drm.fd, connector_id); |
David Herrmann | 7551cff | 2011-12-08 17:05:43 +0100 | [diff] [blame] | 720 | if (connector == NULL) |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 721 | continue; |
| 722 | |
David Herrmann | 7551cff | 2011-12-08 17:05:43 +0100 | [diff] [blame] | 723 | if (connector->connection != DRM_MODE_CONNECTED) { |
| 724 | drmModeFreeConnector(connector); |
| 725 | continue; |
| 726 | } |
| 727 | |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 728 | connected |= (1 << connector_id); |
| 729 | |
| 730 | if (!(ec->connector_allocator & (1 << connector_id))) { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 731 | struct weston_output *last = |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 732 | container_of(ec->base.output_list.prev, |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 733 | struct weston_output, link); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 734 | |
| 735 | /* XXX: not yet needed, we die with 0 outputs */ |
| 736 | if (!wl_list_empty(&ec->base.output_list)) |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 737 | x = last->x + last->current->width; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 738 | else |
| 739 | x = 0; |
| 740 | y = 0; |
| 741 | create_output_for_connector(ec, resources, |
| 742 | connector, x, y); |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 743 | printf("connector %d connected\n", connector_id); |
| 744 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 745 | } |
| 746 | drmModeFreeConnector(connector); |
| 747 | } |
| 748 | drmModeFreeResources(resources); |
| 749 | |
| 750 | disconnects = ec->connector_allocator & ~connected; |
| 751 | if (disconnects) { |
| 752 | wl_list_for_each_safe(output, next, &ec->base.output_list, |
| 753 | base.link) { |
| 754 | if (x_offset != 0 || y_offset != 0) { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 755 | weston_output_move(&output->base, |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 756 | output->base.x - x_offset, |
| 757 | output->base.y - y_offset); |
| 758 | } |
| 759 | |
| 760 | if (disconnects & (1 << output->connector_id)) { |
| 761 | disconnects &= ~(1 << output->connector_id); |
| 762 | printf("connector %d disconnected\n", |
| 763 | output->connector_id); |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 764 | x_offset += output->base.current->width; |
Benjamin Franzke | 48c4ea2 | 2011-08-30 11:44:56 +0200 | [diff] [blame] | 765 | drm_output_destroy(&output->base); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 766 | } |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | /* FIXME: handle zero outputs, without terminating */ |
| 771 | if (ec->connector_allocator == 0) |
| 772 | wl_display_terminate(ec->base.wl_display); |
| 773 | } |
| 774 | |
| 775 | static int |
| 776 | udev_event_is_hotplug(struct udev_device *device) |
| 777 | { |
| 778 | struct udev_list_entry *list, *hotplug_entry; |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 779 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 780 | list = udev_device_get_properties_list_entry(device); |
| 781 | |
| 782 | hotplug_entry = udev_list_entry_get_by_name(list, "HOTPLUG"); |
| 783 | if (hotplug_entry == NULL) |
| 784 | return 0; |
| 785 | |
| 786 | return strcmp(udev_list_entry_get_value(hotplug_entry), "1") == 0; |
| 787 | } |
| 788 | |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 789 | static int |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 790 | udev_drm_event(int fd, uint32_t mask, void *data) |
| 791 | { |
| 792 | struct drm_compositor *ec = data; |
| 793 | struct udev_device *event; |
| 794 | |
| 795 | event = udev_monitor_receive_device(ec->udev_monitor); |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 796 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 797 | if (udev_event_is_hotplug(event)) |
| 798 | update_outputs(ec); |
| 799 | |
| 800 | udev_device_unref(event); |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 801 | |
| 802 | return 1; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 803 | } |
| 804 | |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 805 | static EGLImageKHR |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 806 | drm_compositor_create_cursor_image(struct weston_compositor *ec, |
Ander Conselvan de Oliveira | 0de0aaf | 2011-10-27 17:09:17 +0300 | [diff] [blame] | 807 | int32_t *width, int32_t *height) |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 808 | { |
Kristian Høgsberg | b5819dc | 2011-04-25 15:08:20 -0400 | [diff] [blame] | 809 | struct drm_compositor *c = (struct drm_compositor *) ec; |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 810 | struct gbm_bo *bo; |
| 811 | EGLImageKHR image; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 812 | |
Ander Conselvan de Oliveira | 0de0aaf | 2011-10-27 17:09:17 +0300 | [diff] [blame] | 813 | if (*width > 64 || *height > 64) |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 814 | return EGL_NO_IMAGE_KHR; |
| 815 | |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 816 | bo = gbm_bo_create(c->gbm, |
| 817 | /* width, height, */ 64, 64, |
| 818 | GBM_BO_FORMAT_ARGB8888, |
| 819 | GBM_BO_USE_CURSOR_64X64 | GBM_BO_USE_RENDERING); |
| 820 | |
| 821 | image = ec->create_image(c->base.display, NULL, |
| 822 | EGL_NATIVE_PIXMAP_KHR, bo, NULL); |
| 823 | gbm_bo_destroy(bo); |
| 824 | |
Kristian Høgsberg | ff298d9 | 2011-10-28 20:41:28 -0400 | [diff] [blame] | 825 | *width = 64; |
| 826 | *height = 64; |
Ander Conselvan de Oliveira | c483917 | 2011-08-16 14:25:15 +0300 | [diff] [blame] | 827 | |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 828 | return image; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 829 | } |
| 830 | |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 831 | static void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 832 | drm_destroy(struct weston_compositor *ec) |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 833 | { |
| 834 | struct drm_compositor *d = (struct drm_compositor *) ec; |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 835 | struct weston_input_device *input, *next; |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 836 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 837 | weston_compositor_shutdown(ec); |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 838 | gbm_device_destroy(d->gbm); |
Kristian Høgsberg | 62d2774 | 2012-01-18 12:38:33 -0500 | [diff] [blame] | 839 | drmDropMaster(d->drm.fd); |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 840 | tty_destroy(d->tty); |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 841 | |
Tiago Vignatti | c349e1d | 2011-12-18 23:52:18 +0200 | [diff] [blame] | 842 | wl_list_for_each_safe(input, next, &ec->input_device_list, link) |
| 843 | evdev_input_destroy(input); |
| 844 | |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 845 | free(d); |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 846 | } |
| 847 | |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 848 | static void |
Kristian Høgsberg | 835cd49 | 2012-01-18 11:48:46 -0500 | [diff] [blame] | 849 | drm_compositor_set_modes(struct drm_compositor *compositor) |
| 850 | { |
| 851 | struct drm_output *output; |
| 852 | struct drm_mode *drm_mode; |
| 853 | int ret; |
| 854 | |
| 855 | wl_list_for_each(output, &compositor->base.output_list, base.link) { |
| 856 | drm_mode = (struct drm_mode *) output->base.current; |
| 857 | ret = drmModeSetCrtc(compositor->drm.fd, output->crtc_id, |
| 858 | output->fb_id[output->current ^ 1], 0, 0, |
| 859 | &output->connector_id, 1, |
| 860 | &drm_mode->mode_info); |
| 861 | if (ret < 0) { |
| 862 | fprintf(stderr, |
| 863 | "failed to set mode %dx%d for output at %d,%d: %m", |
| 864 | drm_mode->base.width, drm_mode->base.height, |
| 865 | output->base.x, output->base.y); |
| 866 | } |
| 867 | } |
| 868 | } |
| 869 | |
| 870 | static void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 871 | vt_func(struct weston_compositor *compositor, int event) |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 872 | { |
| 873 | struct drm_compositor *ec = (struct drm_compositor *) compositor; |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 874 | struct weston_output *output; |
| 875 | struct weston_input_device *input; |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 876 | |
| 877 | switch (event) { |
| 878 | case TTY_ENTER_VT: |
| 879 | compositor->focus = 1; |
Kristian Høgsberg | a018fb0 | 2012-01-16 10:52:52 -0500 | [diff] [blame] | 880 | if (drmSetMaster(ec->drm.fd)) { |
| 881 | fprintf(stderr, "failed to set master: %m\n"); |
| 882 | wl_display_terminate(compositor->wl_display); |
| 883 | } |
Pekka Paalanen | bce2d3f | 2011-12-02 13:07:27 +0200 | [diff] [blame] | 884 | compositor->state = ec->prev_state; |
Kristian Høgsberg | 835cd49 | 2012-01-18 11:48:46 -0500 | [diff] [blame] | 885 | drm_compositor_set_modes(ec); |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 886 | weston_compositor_damage_all(compositor); |
Tiago Vignatti | 6e2d5f1 | 2011-12-19 00:32:48 +0200 | [diff] [blame] | 887 | wl_list_for_each(input, &compositor->input_device_list, link) |
| 888 | evdev_add_devices(ec->udev, input); |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 889 | break; |
| 890 | case TTY_LEAVE_VT: |
| 891 | compositor->focus = 0; |
Pekka Paalanen | bce2d3f | 2011-12-02 13:07:27 +0200 | [diff] [blame] | 892 | ec->prev_state = compositor->state; |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 893 | compositor->state = WESTON_COMPOSITOR_SLEEPING; |
Kristian Høgsberg | d8e181b | 2011-05-06 15:38:28 -0400 | [diff] [blame] | 894 | |
Kristian Høgsberg | 34f80ff | 2012-01-18 11:50:31 -0500 | [diff] [blame] | 895 | /* If we have a repaint scheduled (either from a |
| 896 | * pending pageflip or the idle handler), make sure we |
| 897 | * cancel that so we don't try to pageflip when we're |
| 898 | * vt switched away. The SLEEPING state will prevent |
| 899 | * further attemps at repainting. When we switch |
| 900 | * back, we schedule a repaint, which will process |
| 901 | * pending frame callbacks. */ |
| 902 | |
| 903 | wl_list_for_each(output, &ec->base.output_list, link) { |
| 904 | output->repaint_needed = 0; |
| 905 | drm_output_set_cursor(output, NULL); |
| 906 | } |
| 907 | |
Tiago Vignatti | 6e2d5f1 | 2011-12-19 00:32:48 +0200 | [diff] [blame] | 908 | wl_list_for_each(input, &compositor->input_device_list, link) |
| 909 | evdev_remove_devices(input); |
Kristian Høgsberg | d8e181b | 2011-05-06 15:38:28 -0400 | [diff] [blame] | 910 | |
Kristian Høgsberg | a018fb0 | 2012-01-16 10:52:52 -0500 | [diff] [blame] | 911 | if (drmDropMaster(ec->drm.fd) < 0) |
| 912 | fprintf(stderr, "failed to drop master: %m\n"); |
| 913 | |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 914 | break; |
| 915 | }; |
| 916 | } |
| 917 | |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 918 | static const char default_seat[] = "seat0"; |
| 919 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 920 | static struct weston_compositor * |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 921 | drm_compositor_create(struct wl_display *display, |
Tiago Vignatti | faee801 | 2011-09-01 15:58:17 -0400 | [diff] [blame] | 922 | int connector, const char *seat, int tty) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 923 | { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 924 | struct drm_compositor *ec; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 925 | struct udev_enumerate *e; |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 926 | struct udev_list_entry *entry; |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 927 | struct udev_device *device, *drm_device; |
| 928 | const char *path, *device_seat; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 929 | struct wl_event_loop *loop; |
| 930 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 931 | ec = malloc(sizeof *ec); |
| 932 | if (ec == NULL) |
| 933 | return NULL; |
| 934 | |
| 935 | memset(ec, 0, sizeof *ec); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 936 | ec->udev = udev_new(); |
| 937 | if (ec->udev == NULL) { |
| 938 | fprintf(stderr, "failed to initialize udev context\n"); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 939 | return NULL; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 940 | } |
| 941 | |
Kristian Høgsberg | c5b9ddb | 2012-01-15 14:29:09 -0500 | [diff] [blame] | 942 | ec->base.wl_display = display; |
| 943 | ec->tty = tty_create(&ec->base, vt_func, tty); |
| 944 | if (!ec->tty) { |
| 945 | fprintf(stderr, "failed to initialize tty\n"); |
| 946 | free(ec); |
| 947 | return NULL; |
| 948 | } |
| 949 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 950 | e = udev_enumerate_new(ec->udev); |
| 951 | udev_enumerate_add_match_subsystem(e, "drm"); |
Benjamin Franzke | a764ee5 | 2011-10-07 08:23:22 +0200 | [diff] [blame] | 952 | udev_enumerate_add_match_sysname(e, "card[0-9]*"); |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 953 | |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 954 | udev_enumerate_scan_devices(e); |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 955 | drm_device = NULL; |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 956 | udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 957 | path = udev_list_entry_get_name(entry); |
| 958 | device = udev_device_new_from_syspath(ec->udev, path); |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 959 | device_seat = |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 960 | udev_device_get_property_value(device, "ID_SEAT"); |
| 961 | if (!device_seat) |
| 962 | device_seat = default_seat; |
| 963 | if (strcmp(device_seat, seat) == 0) { |
| 964 | drm_device = device; |
| 965 | break; |
| 966 | } |
| 967 | udev_device_unref(device); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 968 | } |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 969 | |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 970 | if (drm_device == NULL) { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 971 | fprintf(stderr, "no drm device found\n"); |
| 972 | return NULL; |
| 973 | } |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 974 | |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 975 | if (init_egl(ec, drm_device) < 0) { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 976 | fprintf(stderr, "failed to initialize egl\n"); |
| 977 | return NULL; |
| 978 | } |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 979 | |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 980 | udev_device_unref(drm_device); |
| 981 | |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 982 | ec->base.destroy = drm_destroy; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 983 | ec->base.create_cursor_image = drm_compositor_create_cursor_image; |
| 984 | |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 985 | ec->base.focus = 1; |
| 986 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 987 | ec->prev_state = WESTON_COMPOSITOR_ACTIVE; |
Pekka Paalanen | bce2d3f | 2011-12-02 13:07:27 +0200 | [diff] [blame] | 988 | |
Benjamin Franzke | 5b2cb6f | 2011-02-18 16:54:55 +0100 | [diff] [blame] | 989 | glGenFramebuffers(1, &ec->base.fbo); |
| 990 | glBindFramebuffer(GL_FRAMEBUFFER, ec->base.fbo); |
| 991 | |
Benjamin Franzke | d59eb1c | 2011-04-29 22:14:54 +0200 | [diff] [blame] | 992 | /* Can't init base class until we have a current egl context */ |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 993 | if (weston_compositor_init(&ec->base, display) < 0) |
Benjamin Franzke | d59eb1c | 2011-04-29 22:14:54 +0200 | [diff] [blame] | 994 | return NULL; |
| 995 | |
Kristian Høgsberg | 61a8251 | 2010-10-26 11:26:44 -0400 | [diff] [blame] | 996 | if (create_outputs(ec, connector) < 0) { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 997 | fprintf(stderr, "failed to create output for %s\n", path); |
| 998 | return NULL; |
| 999 | } |
| 1000 | |
Benjamin Franzke | 02dee2c | 2011-10-07 08:27:26 +0200 | [diff] [blame] | 1001 | udev_enumerate_unref(e); |
| 1002 | path = NULL; |
| 1003 | |
Tiago Vignatti | ce03ec3 | 2011-12-19 01:14:03 +0200 | [diff] [blame] | 1004 | evdev_input_create(&ec->base, ec->udev, seat); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1005 | |
| 1006 | loop = wl_display_get_event_loop(ec->base.wl_display); |
| 1007 | ec->drm_source = |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 1008 | wl_event_loop_add_fd(loop, ec->drm.fd, |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1009 | WL_EVENT_READABLE, on_drm_input, ec); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1010 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1011 | ec->udev_monitor = udev_monitor_new_from_netlink(ec->udev, "udev"); |
| 1012 | if (ec->udev_monitor == NULL) { |
| 1013 | fprintf(stderr, "failed to intialize udev monitor\n"); |
| 1014 | return NULL; |
| 1015 | } |
| 1016 | udev_monitor_filter_add_match_subsystem_devtype(ec->udev_monitor, |
| 1017 | "drm", NULL); |
| 1018 | ec->udev_drm_source = |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1019 | wl_event_loop_add_fd(loop, |
| 1020 | udev_monitor_get_fd(ec->udev_monitor), |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1021 | WL_EVENT_READABLE, udev_drm_event, ec); |
| 1022 | |
| 1023 | if (udev_monitor_enable_receiving(ec->udev_monitor) < 0) { |
| 1024 | fprintf(stderr, "failed to enable udev-monitor receiving\n"); |
| 1025 | return NULL; |
| 1026 | } |
| 1027 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1028 | return &ec->base; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1029 | } |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 1030 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1031 | struct weston_compositor * |
Kristian Høgsberg | 6c709a3 | 2011-05-06 14:52:41 -0400 | [diff] [blame] | 1032 | backend_init(struct wl_display *display, char *options); |
| 1033 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1034 | WL_EXPORT struct weston_compositor * |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 1035 | backend_init(struct wl_display *display, char *options) |
| 1036 | { |
Kristian Høgsberg | 8050bd2 | 2011-05-04 15:08:04 -0400 | [diff] [blame] | 1037 | int connector = 0, i; |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 1038 | const char *seat; |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 1039 | char *p, *value; |
Kristian Høgsberg | 1201b75 | 2012-01-15 14:27:10 -0500 | [diff] [blame] | 1040 | int tty = 0; |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 1041 | |
Tiago Vignatti | faee801 | 2011-09-01 15:58:17 -0400 | [diff] [blame] | 1042 | static char * const tokens[] = { "connector", "seat", "tty", NULL }; |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1043 | |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 1044 | p = options; |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 1045 | seat = default_seat; |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 1046 | while (i = getsubopt(&p, tokens, &value), i != -1) { |
| 1047 | switch (i) { |
| 1048 | case 0: |
Kristian Høgsberg | 8050bd2 | 2011-05-04 15:08:04 -0400 | [diff] [blame] | 1049 | connector = strtol(value, NULL, 0); |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 1050 | break; |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 1051 | case 1: |
| 1052 | seat = value; |
| 1053 | break; |
Tiago Vignatti | faee801 | 2011-09-01 15:58:17 -0400 | [diff] [blame] | 1054 | case 2: |
| 1055 | tty = strtol(value, NULL, 0); |
| 1056 | break; |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 1057 | } |
| 1058 | } |
| 1059 | |
Tiago Vignatti | faee801 | 2011-09-01 15:58:17 -0400 | [diff] [blame] | 1060 | return drm_compositor_create(display, connector, seat, tty); |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 1061 | } |