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