Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2008-2010 Kristian Høgsberg |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software Foundation, |
| 16 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | */ |
| 18 | |
Kristian Høgsberg | 0b9334a | 2011-04-12 11:34:32 -0400 | [diff] [blame] | 19 | #define _GNU_SOURCE |
| 20 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <fcntl.h> |
| 25 | #include <unistd.h> |
| 26 | |
Benjamin Franzke | c649a92 | 2011-03-02 11:56:04 +0100 | [diff] [blame] | 27 | #include <xf86drm.h> |
| 28 | #include <xf86drmMode.h> |
| 29 | |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 30 | #include <gbm.h> |
| 31 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 32 | #include "compositor.h" |
| 33 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 34 | struct drm_compositor { |
| 35 | struct wlsc_compositor base; |
| 36 | |
| 37 | struct udev *udev; |
| 38 | struct wl_event_source *drm_source; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 39 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 40 | struct udev_monitor *udev_monitor; |
| 41 | struct wl_event_source *udev_drm_source; |
| 42 | |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 43 | struct { |
| 44 | int fd; |
| 45 | } drm; |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 46 | struct gbm_device *gbm; |
Marty Jack | 13d9db2 | 2011-02-09 19:01:42 -0500 | [diff] [blame] | 47 | uint32_t crtc_allocator; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 48 | uint32_t connector_allocator; |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 49 | struct tty *tty; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 50 | }; |
| 51 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 52 | struct drm_mode { |
| 53 | struct wlsc_mode base; |
| 54 | drmModeModeInfo mode_info; |
| 55 | }; |
| 56 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 57 | struct drm_output { |
| 58 | struct wlsc_output base; |
| 59 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 60 | uint32_t crtc_id; |
| 61 | uint32_t connector_id; |
| 62 | GLuint rbo[2]; |
| 63 | uint32_t fb_id[2]; |
| 64 | EGLImageKHR image[2]; |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 65 | struct gbm_bo *bo[2]; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 66 | uint32_t current; |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 67 | |
Benjamin Franzke | 66aa235 | 2011-04-20 17:06:13 +0200 | [diff] [blame] | 68 | struct wlsc_surface *scanout_surface; |
| 69 | |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 70 | uint32_t fs_surf_fb_id; |
| 71 | uint32_t pending_fs_surf_fb_id; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 72 | }; |
| 73 | |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 74 | static int |
| 75 | drm_output_prepare_render(struct wlsc_output *output_base) |
| 76 | { |
| 77 | struct drm_output *output = (struct drm_output *) output_base; |
| 78 | |
| 79 | glFramebufferRenderbuffer(GL_FRAMEBUFFER, |
| 80 | GL_COLOR_ATTACHMENT0, |
| 81 | GL_RENDERBUFFER, |
| 82 | output->rbo[output->current]); |
| 83 | |
| 84 | if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) |
| 85 | return -1; |
| 86 | |
| 87 | return 0; |
| 88 | } |
| 89 | |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 90 | static int |
| 91 | drm_output_present(struct wlsc_output *output_base) |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 92 | { |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 93 | struct drm_output *output = (struct drm_output *) output_base; |
| 94 | struct drm_compositor *c = |
| 95 | (struct drm_compositor *) output->base.compositor; |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 96 | uint32_t fb_id = 0; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 97 | |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 98 | if (drm_output_prepare_render(&output->base)) |
| 99 | return -1; |
| 100 | glFlush(); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 101 | |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 102 | output->current ^= 1; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 103 | |
Benjamin Franzke | 66aa235 | 2011-04-20 17:06:13 +0200 | [diff] [blame] | 104 | if (output->scanout_surface != NULL) { |
| 105 | output->scanout_surface = NULL; |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 106 | fb_id = output->fs_surf_fb_id; |
| 107 | } else { |
| 108 | fb_id = output->fb_id[output->current ^ 1]; |
| 109 | } |
| 110 | |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 111 | drmModePageFlip(c->drm.fd, output->crtc_id, |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 112 | fb_id, |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 113 | DRM_MODE_PAGE_FLIP_EVENT, output); |
| 114 | |
| 115 | return 0; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | static void |
| 119 | page_flip_handler(int fd, unsigned int frame, |
| 120 | unsigned int sec, unsigned int usec, void *data) |
| 121 | { |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 122 | struct drm_output *output = (struct drm_output *) data; |
| 123 | struct drm_compositor *c = |
| 124 | (struct drm_compositor *) output->base.compositor; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 125 | uint32_t msecs; |
| 126 | |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 127 | if (output->pending_fs_surf_fb_id) { |
| 128 | drmModeRmFB(c->drm.fd, output->pending_fs_surf_fb_id); |
| 129 | output->pending_fs_surf_fb_id = 0; |
| 130 | } |
| 131 | |
| 132 | if (output->fs_surf_fb_id) { |
| 133 | output->pending_fs_surf_fb_id = output->fs_surf_fb_id; |
| 134 | output->fs_surf_fb_id = 0; |
| 135 | } |
| 136 | |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 137 | msecs = sec * 1000 + usec / 1000; |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 138 | wlsc_output_finish_frame(&output->base, msecs); |
| 139 | } |
| 140 | |
| 141 | static int |
Benjamin Franzke | 66aa235 | 2011-04-20 17:06:13 +0200 | [diff] [blame] | 142 | drm_output_prepare_scanout_surface(struct wlsc_output *output_base, |
| 143 | struct wlsc_surface *es) |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 144 | { |
| 145 | struct drm_output *output = (struct drm_output *) output_base; |
| 146 | struct drm_compositor *c = |
| 147 | (struct drm_compositor *) output->base.compositor; |
| 148 | EGLint handle, stride; |
| 149 | int ret; |
| 150 | uint32_t fb_id = 0; |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 151 | struct gbm_bo *bo; |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 152 | |
Kristian Høgsberg | 4d07a1c | 2011-05-06 14:03:12 -0400 | [diff] [blame] | 153 | if (es->x != output->base.x || |
| 154 | es->y != output->base.y || |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 155 | es->width != output->base.current->width || |
| 156 | es->height != output->base.current->height || |
Benjamin Franzke | 66aa235 | 2011-04-20 17:06:13 +0200 | [diff] [blame] | 157 | es->image == EGL_NO_IMAGE_KHR) |
| 158 | return -1; |
| 159 | |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 160 | bo = gbm_bo_create_from_egl_image(c->gbm, |
| 161 | c->base.display, es->image, |
| 162 | es->width, es->height, |
| 163 | GBM_BO_USE_SCANOUT); |
| 164 | |
| 165 | handle = gbm_bo_get_handle(bo).s32; |
| 166 | stride = gbm_bo_get_pitch(bo); |
| 167 | |
| 168 | gbm_bo_destroy(bo); |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 169 | |
Benjamin Franzke | 66aa235 | 2011-04-20 17:06:13 +0200 | [diff] [blame] | 170 | if (handle == 0) |
| 171 | return -1; |
| 172 | |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 173 | ret = drmModeAddFB(c->drm.fd, |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 174 | output->base.current->width, |
| 175 | output->base.current->height, |
Benjamin Franzke | 66aa235 | 2011-04-20 17:06:13 +0200 | [diff] [blame] | 176 | 32, 32, stride, handle, &fb_id); |
| 177 | |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 178 | if (ret) |
Benjamin Franzke | 66aa235 | 2011-04-20 17:06:13 +0200 | [diff] [blame] | 179 | return -1; |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 180 | |
Benjamin Franzke | 66aa235 | 2011-04-20 17:06:13 +0200 | [diff] [blame] | 181 | output->fs_surf_fb_id = fb_id; |
| 182 | output->scanout_surface = es; |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 183 | |
Benjamin Franzke | 66aa235 | 2011-04-20 17:06:13 +0200 | [diff] [blame] | 184 | return 0; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 185 | } |
| 186 | |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 187 | static int |
| 188 | drm_output_set_cursor(struct wlsc_output *output_base, |
Kristian Høgsberg | e4c40a4 | 2011-05-06 14:04:21 -0400 | [diff] [blame] | 189 | struct wlsc_input_device *eid) |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 190 | { |
| 191 | struct drm_output *output = (struct drm_output *) output_base; |
| 192 | struct drm_compositor *c = |
| 193 | (struct drm_compositor *) output->base.compositor; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 194 | EGLint handle, stride; |
| 195 | int ret = -1; |
| 196 | pixman_region32_t cursor_region; |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 197 | struct gbm_bo *bo; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 198 | |
Kristian Høgsberg | e4c40a4 | 2011-05-06 14:04:21 -0400 | [diff] [blame] | 199 | if (eid == NULL) { |
| 200 | drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0); |
| 201 | return 0; |
| 202 | } |
| 203 | |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 204 | pixman_region32_init_rect(&cursor_region, |
| 205 | eid->sprite->x, eid->sprite->y, |
| 206 | eid->sprite->width, eid->sprite->height); |
| 207 | |
| 208 | pixman_region32_intersect_rect(&cursor_region, &cursor_region, |
| 209 | output->base.x, output->base.y, |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 210 | output->base.current->width, |
| 211 | output->base.current->height); |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 212 | |
Benjamin Franzke | a8bdeae | 2011-06-28 22:56:43 +0200 | [diff] [blame] | 213 | if (!pixman_region32_not_empty(&cursor_region)) |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 214 | goto out; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 215 | |
| 216 | if (eid->sprite->image == EGL_NO_IMAGE_KHR) |
| 217 | goto out; |
| 218 | |
| 219 | if (eid->sprite->width > 64 || eid->sprite->height > 64) |
| 220 | goto out; |
| 221 | |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 222 | bo = gbm_bo_create_from_egl_image(c->gbm, |
| 223 | c->base.display, |
| 224 | eid->sprite->image, 64, 64, |
| 225 | GBM_BO_USE_CURSOR_64X64); |
| 226 | |
| 227 | handle = gbm_bo_get_handle(bo).s32; |
| 228 | stride = gbm_bo_get_pitch(bo); |
| 229 | |
| 230 | gbm_bo_destroy(bo); |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 231 | |
| 232 | if (stride != 64 * 4) { |
| 233 | fprintf(stderr, "info: cursor stride is != 64\n"); |
| 234 | goto out; |
| 235 | } |
| 236 | |
| 237 | ret = drmModeSetCursor(c->drm.fd, output->crtc_id, handle, 64, 64); |
| 238 | if (ret) { |
| 239 | fprintf(stderr, "failed to set cursor: %s\n", strerror(-ret)); |
| 240 | goto out; |
| 241 | } |
| 242 | |
| 243 | ret = drmModeMoveCursor(c->drm.fd, output->crtc_id, |
| 244 | eid->sprite->x - output->base.x, |
| 245 | eid->sprite->y - output->base.y); |
| 246 | if (ret) { |
| 247 | fprintf(stderr, "failed to move cursor: %s\n", strerror(-ret)); |
| 248 | goto out; |
| 249 | } |
| 250 | |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 251 | out: |
| 252 | pixman_region32_fini(&cursor_region); |
| 253 | if (ret) |
| 254 | drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0); |
| 255 | return ret; |
| 256 | } |
| 257 | |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 258 | static int |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 259 | on_drm_input(int fd, uint32_t mask, void *data) |
| 260 | { |
| 261 | drmEventContext evctx; |
| 262 | |
| 263 | memset(&evctx, 0, sizeof evctx); |
| 264 | evctx.version = DRM_EVENT_CONTEXT_VERSION; |
| 265 | evctx.page_flip_handler = page_flip_handler; |
| 266 | drmHandleEvent(fd, &evctx); |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 267 | |
| 268 | return 1; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | static int |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 272 | init_egl(struct drm_compositor *ec, struct udev_device *device) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 273 | { |
Kristian Høgsberg | 379b678 | 2010-07-28 22:52:28 -0400 | [diff] [blame] | 274 | EGLint major, minor; |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 275 | const char *extensions, *filename; |
| 276 | int fd; |
Kristian Høgsberg | 2c28aa5 | 2010-07-28 23:47:54 -0400 | [diff] [blame] | 277 | static const EGLint context_attribs[] = { |
| 278 | EGL_CONTEXT_CLIENT_VERSION, 2, |
| 279 | EGL_NONE |
| 280 | }; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 281 | |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 282 | filename = udev_device_get_devnode(device); |
Kristian Høgsberg | 9cd7aca | 2011-04-11 13:19:16 -0400 | [diff] [blame] | 283 | fd = open(filename, O_RDWR, O_CLOEXEC); |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 284 | if (fd < 0) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 285 | /* Probably permissions error */ |
| 286 | fprintf(stderr, "couldn't open %s, skipping\n", |
| 287 | udev_device_get_devnode(device)); |
| 288 | return -1; |
| 289 | } |
| 290 | |
Egbert Eich | e7b8d90 | 2011-05-10 20:00:19 +0000 | [diff] [blame] | 291 | setenv("EGL_PLATFORM", "drm", 1); |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 292 | ec->drm.fd = fd; |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 293 | ec->gbm = gbm_create_device(ec->drm.fd); |
| 294 | ec->base.display = eglGetDisplay(ec->gbm); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 295 | if (ec->base.display == NULL) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 296 | fprintf(stderr, "failed to create display\n"); |
| 297 | return -1; |
| 298 | } |
| 299 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 300 | if (!eglInitialize(ec->base.display, &major, &minor)) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 301 | fprintf(stderr, "failed to initialize display\n"); |
| 302 | return -1; |
| 303 | } |
| 304 | |
Kristian Høgsberg | 379b678 | 2010-07-28 22:52:28 -0400 | [diff] [blame] | 305 | extensions = eglQueryString(ec->base.display, EGL_EXTENSIONS); |
| 306 | if (!strstr(extensions, "EGL_KHR_surfaceless_opengl")) { |
| 307 | fprintf(stderr, "EGL_KHR_surfaceless_opengl not available\n"); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 308 | return -1; |
| 309 | } |
| 310 | |
Darxus | 55973f2 | 2010-11-22 21:24:39 -0500 | [diff] [blame] | 311 | if (!eglBindAPI(EGL_OPENGL_ES_API)) { |
| 312 | fprintf(stderr, "failed to bind api EGL_OPENGL_ES_API\n"); |
| 313 | return -1; |
| 314 | } |
| 315 | |
Kristian Høgsberg | 2c28aa5 | 2010-07-28 23:47:54 -0400 | [diff] [blame] | 316 | ec->base.context = eglCreateContext(ec->base.display, NULL, |
| 317 | EGL_NO_CONTEXT, context_attribs); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 318 | if (ec->base.context == NULL) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 319 | fprintf(stderr, "failed to create context\n"); |
| 320 | return -1; |
| 321 | } |
| 322 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 323 | if (!eglMakeCurrent(ec->base.display, EGL_NO_SURFACE, |
| 324 | EGL_NO_SURFACE, ec->base.context)) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 325 | fprintf(stderr, "failed to make context current\n"); |
| 326 | return -1; |
| 327 | } |
| 328 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 329 | return 0; |
| 330 | } |
| 331 | |
| 332 | static drmModeModeInfo builtin_1024x768 = { |
| 333 | 63500, /* clock */ |
| 334 | 1024, 1072, 1176, 1328, 0, |
| 335 | 768, 771, 775, 798, 0, |
| 336 | 59920, |
| 337 | DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC, |
| 338 | 0, |
| 339 | "1024x768" |
| 340 | }; |
| 341 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 342 | |
| 343 | static int |
| 344 | drm_output_add_mode(struct drm_output *output, drmModeModeInfo *info) |
| 345 | { |
| 346 | struct drm_mode *mode; |
| 347 | |
| 348 | mode = malloc(sizeof *mode); |
| 349 | if (mode == NULL) |
| 350 | return -1; |
| 351 | |
| 352 | mode->base.flags = 0; |
| 353 | mode->base.width = info->hdisplay; |
| 354 | mode->base.height = info->vdisplay; |
| 355 | mode->base.refresh = info->vrefresh; |
| 356 | mode->mode_info = *info; |
| 357 | wl_list_insert(output->base.mode_list.prev, &mode->base.link); |
| 358 | |
| 359 | return 0; |
| 360 | } |
| 361 | |
| 362 | static int |
| 363 | drm_subpixel_to_wayland(int drm_value) |
| 364 | { |
| 365 | switch (drm_value) { |
| 366 | default: |
| 367 | case DRM_MODE_SUBPIXEL_UNKNOWN: |
| 368 | return WL_OUTPUT_SUBPIXEL_UNKNOWN; |
| 369 | case DRM_MODE_SUBPIXEL_NONE: |
| 370 | return WL_OUTPUT_SUBPIXEL_NONE; |
| 371 | case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB: |
| 372 | return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB; |
| 373 | case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR: |
| 374 | return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR; |
| 375 | case DRM_MODE_SUBPIXEL_VERTICAL_RGB: |
| 376 | return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB; |
| 377 | case DRM_MODE_SUBPIXEL_VERTICAL_BGR: |
| 378 | return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR; |
| 379 | } |
| 380 | } |
| 381 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 382 | static int |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 383 | create_output_for_connector(struct drm_compositor *ec, |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 384 | drmModeRes *resources, |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 385 | drmModeConnector *connector, |
| 386 | int x, int y) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 387 | { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 388 | struct drm_output *output; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 389 | struct drm_mode *drm_mode; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 390 | drmModeEncoder *encoder; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 391 | int i, ret; |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 392 | unsigned handle, stride; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 393 | |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 394 | encoder = drmModeGetEncoder(ec->drm.fd, connector->encoders[0]); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 395 | if (encoder == NULL) { |
| 396 | fprintf(stderr, "No encoder for connector.\n"); |
| 397 | return -1; |
| 398 | } |
| 399 | |
| 400 | for (i = 0; i < resources->count_crtcs; i++) { |
Marty Jack | 13d9db2 | 2011-02-09 19:01:42 -0500 | [diff] [blame] | 401 | if (encoder->possible_crtcs & (1 << i) && |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 402 | !(ec->crtc_allocator & (1 << resources->crtcs[i]))) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 403 | break; |
| 404 | } |
| 405 | if (i == resources->count_crtcs) { |
| 406 | fprintf(stderr, "No usable crtc for encoder.\n"); |
| 407 | return -1; |
| 408 | } |
| 409 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 410 | output = malloc(sizeof *output); |
| 411 | if (output == NULL) |
| 412 | return -1; |
| 413 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 414 | memset(output, 0, sizeof *output); |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 415 | output->base.subpixel = drm_subpixel_to_wayland(connector->subpixel); |
| 416 | output->base.make = "unknown"; |
| 417 | output->base.model = "unknown"; |
| 418 | wl_list_init(&output->base.mode_list); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 419 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 420 | output->crtc_id = resources->crtcs[i]; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 421 | ec->crtc_allocator |= (1 << output->crtc_id); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 422 | output->connector_id = connector->connector_id; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 423 | ec->connector_allocator |= (1 << output->connector_id); |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 424 | |
| 425 | for (i = 0; i < connector->count_modes; i++) |
| 426 | drm_output_add_mode(output, &connector->modes[i]); |
| 427 | if (connector->count_modes == 0) |
| 428 | drm_output_add_mode(output, &builtin_1024x768); |
| 429 | |
| 430 | drm_mode = container_of(output->base.mode_list.next, |
| 431 | struct drm_mode, base.link); |
| 432 | output->base.current = &drm_mode->base; |
| 433 | drm_mode->base.flags = |
| 434 | WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED; |
| 435 | |
Benjamin Franzke | a16e002 | 2011-06-24 11:56:25 +0200 | [diff] [blame] | 436 | wlsc_output_init(&output->base, &ec->base, x, y, |
| 437 | connector->mmWidth, connector->mmHeight, 0); |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 438 | |
| 439 | wl_list_insert(ec->base.output_list.prev, &output->base.link); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 440 | |
| 441 | drmModeFreeEncoder(encoder); |
| 442 | |
| 443 | glGenRenderbuffers(2, output->rbo); |
| 444 | for (i = 0; i < 2; i++) { |
| 445 | glBindRenderbuffer(GL_RENDERBUFFER, output->rbo[i]); |
| 446 | |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 447 | output->bo[i] = |
| 448 | gbm_bo_create(ec->gbm, |
| 449 | output->base.current->width, |
| 450 | output->base.current->height, |
| 451 | GBM_BO_FORMAT_XRGB8888, |
| 452 | GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING); |
| 453 | output->image[i] = ec->base.create_image(ec->base.display, |
| 454 | NULL, |
| 455 | EGL_NATIVE_PIXMAP_KHR, |
| 456 | output->bo[i], NULL); |
| 457 | |
| 458 | |
Kristian Høgsberg | b5819dc | 2011-04-25 15:08:20 -0400 | [diff] [blame] | 459 | ec->base.image_target_renderbuffer_storage(GL_RENDERBUFFER, |
| 460 | output->image[i]); |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 461 | stride = gbm_bo_get_pitch(output->bo[i]); |
| 462 | handle = gbm_bo_get_handle(output->bo[i]).u32; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 463 | |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 464 | ret = drmModeAddFB(ec->drm.fd, |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 465 | output->base.current->width, |
| 466 | output->base.current->height, |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 467 | 32, 32, stride, handle, &output->fb_id[i]); |
| 468 | if (ret) { |
| 469 | fprintf(stderr, "failed to add fb %d: %m\n", i); |
| 470 | return -1; |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | output->current = 0; |
| 475 | glFramebufferRenderbuffer(GL_FRAMEBUFFER, |
| 476 | GL_COLOR_ATTACHMENT0, |
| 477 | GL_RENDERBUFFER, |
| 478 | output->rbo[output->current]); |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 479 | ret = drmModeSetCrtc(ec->drm.fd, output->crtc_id, |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 480 | output->fb_id[output->current ^ 1], 0, 0, |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 481 | &output->connector_id, 1, |
| 482 | &drm_mode->mode_info); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 483 | if (ret) { |
| 484 | fprintf(stderr, "failed to set mode: %m\n"); |
| 485 | return -1; |
| 486 | } |
| 487 | |
Benjamin Franzke | 66aa235 | 2011-04-20 17:06:13 +0200 | [diff] [blame] | 488 | output->scanout_surface = NULL; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 489 | output->base.prepare_render = drm_output_prepare_render; |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 490 | output->base.present = drm_output_present; |
Benjamin Franzke | 66aa235 | 2011-04-20 17:06:13 +0200 | [diff] [blame] | 491 | output->base.prepare_scanout_surface = |
| 492 | drm_output_prepare_scanout_surface; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 493 | output->base.set_hardware_cursor = drm_output_set_cursor; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 494 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 495 | return 0; |
| 496 | } |
| 497 | |
| 498 | static int |
Kristian Høgsberg | 61a8251 | 2010-10-26 11:26:44 -0400 | [diff] [blame] | 499 | create_outputs(struct drm_compositor *ec, int option_connector) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 500 | { |
| 501 | drmModeConnector *connector; |
| 502 | drmModeRes *resources; |
| 503 | int i; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 504 | int x = 0, y = 0; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 505 | |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 506 | resources = drmModeGetResources(ec->drm.fd); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 507 | if (!resources) { |
| 508 | fprintf(stderr, "drmModeGetResources failed\n"); |
| 509 | return -1; |
| 510 | } |
| 511 | |
| 512 | for (i = 0; i < resources->count_connectors; i++) { |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 513 | connector = drmModeGetConnector(ec->drm.fd, resources->connectors[i]); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 514 | if (connector == NULL) |
| 515 | continue; |
| 516 | |
| 517 | if (connector->connection == DRM_MODE_CONNECTED && |
| 518 | (option_connector == 0 || |
Benjamin Franzke | 9eaee35 | 2011-08-02 13:03:54 +0200 | [diff] [blame] | 519 | connector->connector_id == option_connector)) { |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 520 | if (create_output_for_connector(ec, resources, |
| 521 | connector, x, y) < 0) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 522 | return -1; |
| 523 | |
Benjamin Franzke | 9eaee35 | 2011-08-02 13:03:54 +0200 | [diff] [blame] | 524 | x += container_of(ec->base.output_list.prev, |
| 525 | struct wlsc_output, |
| 526 | link)->current->width; |
| 527 | } |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 528 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 529 | drmModeFreeConnector(connector); |
| 530 | } |
| 531 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 532 | if (wl_list_empty(&ec->base.output_list)) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 533 | fprintf(stderr, "No currently active connector found.\n"); |
| 534 | return -1; |
| 535 | } |
| 536 | |
| 537 | drmModeFreeResources(resources); |
| 538 | |
| 539 | return 0; |
| 540 | } |
| 541 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 542 | static int |
| 543 | destroy_output(struct drm_output *output) |
| 544 | { |
| 545 | struct drm_compositor *ec = |
| 546 | (struct drm_compositor *) output->base.compositor; |
| 547 | int i; |
| 548 | |
| 549 | glFramebufferRenderbuffer(GL_FRAMEBUFFER, |
| 550 | GL_COLOR_ATTACHMENT0, |
| 551 | GL_RENDERBUFFER, |
| 552 | 0); |
| 553 | |
| 554 | glBindRenderbuffer(GL_RENDERBUFFER, 0); |
| 555 | glDeleteRenderbuffers(2, output->rbo); |
| 556 | |
| 557 | for (i = 0; i < 2; i++) { |
Kristian Høgsberg | b5819dc | 2011-04-25 15:08:20 -0400 | [diff] [blame] | 558 | ec->base.destroy_image(ec->base.display, output->image[i]); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 559 | drmModeRmFB(ec->drm.fd, output->fb_id[i]); |
| 560 | } |
| 561 | |
| 562 | ec->crtc_allocator &= ~(1 << output->crtc_id); |
| 563 | ec->connector_allocator &= ~(1 << output->connector_id); |
| 564 | |
| 565 | wlsc_output_destroy(&output->base); |
| 566 | wl_list_remove(&output->base.link); |
| 567 | |
| 568 | free(output); |
| 569 | |
| 570 | return 0; |
| 571 | } |
| 572 | |
| 573 | static void |
| 574 | update_outputs(struct drm_compositor *ec) |
| 575 | { |
| 576 | drmModeConnector *connector; |
| 577 | drmModeRes *resources; |
| 578 | struct drm_output *output, *next; |
| 579 | int x = 0, y = 0; |
| 580 | int x_offset = 0, y_offset = 0; |
| 581 | uint32_t connected = 0, disconnects = 0; |
| 582 | int i; |
| 583 | |
| 584 | resources = drmModeGetResources(ec->drm.fd); |
| 585 | if (!resources) { |
| 586 | fprintf(stderr, "drmModeGetResources failed\n"); |
| 587 | return; |
| 588 | } |
| 589 | |
| 590 | /* collect new connects */ |
| 591 | for (i = 0; i < resources->count_connectors; i++) { |
| 592 | connector = |
| 593 | drmModeGetConnector(ec->drm.fd, |
| 594 | resources->connectors[i]); |
| 595 | if (connector == NULL || |
| 596 | connector->connection != DRM_MODE_CONNECTED) |
| 597 | continue; |
| 598 | |
| 599 | connected |= (1 << connector->connector_id); |
| 600 | |
| 601 | if (!(ec->connector_allocator & (1 << connector->connector_id))) { |
| 602 | struct wlsc_output *last_output = |
| 603 | container_of(ec->base.output_list.prev, |
| 604 | struct wlsc_output, link); |
| 605 | |
| 606 | /* XXX: not yet needed, we die with 0 outputs */ |
| 607 | if (!wl_list_empty(&ec->base.output_list)) |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 608 | x = last_output->x + last_output->current->width; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 609 | else |
| 610 | x = 0; |
| 611 | y = 0; |
| 612 | create_output_for_connector(ec, resources, |
| 613 | connector, x, y); |
| 614 | printf("connector %d connected\n", |
| 615 | connector->connector_id); |
| 616 | |
| 617 | } |
| 618 | drmModeFreeConnector(connector); |
| 619 | } |
| 620 | drmModeFreeResources(resources); |
| 621 | |
| 622 | disconnects = ec->connector_allocator & ~connected; |
| 623 | if (disconnects) { |
| 624 | wl_list_for_each_safe(output, next, &ec->base.output_list, |
| 625 | base.link) { |
| 626 | if (x_offset != 0 || y_offset != 0) { |
| 627 | wlsc_output_move(&output->base, |
| 628 | output->base.x - x_offset, |
| 629 | output->base.y - y_offset); |
| 630 | } |
| 631 | |
| 632 | if (disconnects & (1 << output->connector_id)) { |
| 633 | disconnects &= ~(1 << output->connector_id); |
| 634 | printf("connector %d disconnected\n", |
| 635 | output->connector_id); |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 636 | x_offset += output->base.current->width; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 637 | destroy_output(output); |
| 638 | } |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | /* FIXME: handle zero outputs, without terminating */ |
| 643 | if (ec->connector_allocator == 0) |
| 644 | wl_display_terminate(ec->base.wl_display); |
| 645 | } |
| 646 | |
| 647 | static int |
| 648 | udev_event_is_hotplug(struct udev_device *device) |
| 649 | { |
| 650 | struct udev_list_entry *list, *hotplug_entry; |
| 651 | |
| 652 | list = udev_device_get_properties_list_entry(device); |
| 653 | |
| 654 | hotplug_entry = udev_list_entry_get_by_name(list, "HOTPLUG"); |
| 655 | if (hotplug_entry == NULL) |
| 656 | return 0; |
| 657 | |
| 658 | return strcmp(udev_list_entry_get_value(hotplug_entry), "1") == 0; |
| 659 | } |
| 660 | |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 661 | static int |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 662 | udev_drm_event(int fd, uint32_t mask, void *data) |
| 663 | { |
| 664 | struct drm_compositor *ec = data; |
| 665 | struct udev_device *event; |
| 666 | |
| 667 | event = udev_monitor_receive_device(ec->udev_monitor); |
| 668 | |
| 669 | if (udev_event_is_hotplug(event)) |
| 670 | update_outputs(ec); |
| 671 | |
| 672 | udev_device_unref(event); |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 673 | |
| 674 | return 1; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 675 | } |
| 676 | |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 677 | static EGLImageKHR |
| 678 | drm_compositor_create_cursor_image(struct wlsc_compositor *ec, |
| 679 | int32_t width, int32_t height) |
| 680 | { |
Kristian Høgsberg | b5819dc | 2011-04-25 15:08:20 -0400 | [diff] [blame] | 681 | struct drm_compositor *c = (struct drm_compositor *) ec; |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 682 | struct gbm_bo *bo; |
| 683 | EGLImageKHR image; |
Ander Conselvan de Oliveira | c483917 | 2011-08-16 14:25:15 +0300 | [diff] [blame^] | 684 | uint32_t *pixels; |
| 685 | GLuint tex; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 686 | |
| 687 | if (width > 64 || height > 64) |
| 688 | return EGL_NO_IMAGE_KHR; |
| 689 | |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 690 | bo = gbm_bo_create(c->gbm, |
| 691 | /* width, height, */ 64, 64, |
| 692 | GBM_BO_FORMAT_ARGB8888, |
| 693 | GBM_BO_USE_CURSOR_64X64 | GBM_BO_USE_RENDERING); |
| 694 | |
| 695 | image = ec->create_image(c->base.display, NULL, |
| 696 | EGL_NATIVE_PIXMAP_KHR, bo, NULL); |
| 697 | gbm_bo_destroy(bo); |
| 698 | |
Ander Conselvan de Oliveira | c483917 | 2011-08-16 14:25:15 +0300 | [diff] [blame^] | 699 | /* If the requested size is smaller than the allocated one, make the |
| 700 | * whole image transparent. */ |
| 701 | if (width != 64 || height != 64) { |
| 702 | pixels = calloc(64 * 64, sizeof *pixels); |
| 703 | |
| 704 | glGenTextures(1, &tex); |
| 705 | glBindTexture(GL_TEXTURE_2D, tex); |
| 706 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 707 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 708 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 709 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 710 | |
| 711 | c->base.image_target_texture_2d(GL_TEXTURE_2D, image); |
| 712 | glTexSubImage(GL_TEXTURE_2D, 0, 0, 0, 64, 64, |
| 713 | GL_BGRA_EXT, GL_UNSIGNED_BYTE, pixels); |
| 714 | |
| 715 | glDeleteTextures(1, &tex); |
| 716 | free(pixels); |
| 717 | } |
| 718 | |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 719 | return image; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 720 | } |
| 721 | |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 722 | static void |
| 723 | drm_destroy(struct wlsc_compositor *ec) |
| 724 | { |
| 725 | struct drm_compositor *d = (struct drm_compositor *) ec; |
| 726 | |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 727 | tty_destroy(d->tty); |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 728 | |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 729 | free(d); |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 730 | } |
| 731 | |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 732 | static void |
| 733 | vt_func(struct wlsc_compositor *compositor, int event) |
| 734 | { |
| 735 | struct drm_compositor *ec = (struct drm_compositor *) compositor; |
Kristian Høgsberg | d8e181b | 2011-05-06 15:38:28 -0400 | [diff] [blame] | 736 | struct wlsc_output *output; |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 737 | |
| 738 | switch (event) { |
| 739 | case TTY_ENTER_VT: |
| 740 | compositor->focus = 1; |
| 741 | drmSetMaster(ec->drm.fd); |
| 742 | compositor->state = WLSC_COMPOSITOR_ACTIVE; |
| 743 | wlsc_compositor_damage_all(compositor); |
| 744 | break; |
| 745 | case TTY_LEAVE_VT: |
| 746 | compositor->focus = 0; |
| 747 | compositor->state = WLSC_COMPOSITOR_SLEEPING; |
| 748 | drmDropMaster(ec->drm.fd); |
Kristian Høgsberg | d8e181b | 2011-05-06 15:38:28 -0400 | [diff] [blame] | 749 | |
| 750 | wl_list_for_each(output, &ec->base.output_list, link) |
| 751 | drm_output_set_cursor(output, NULL); |
| 752 | |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 753 | break; |
| 754 | }; |
| 755 | } |
| 756 | |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 757 | static const char default_seat[] = "seat0"; |
| 758 | |
Kristian Høgsberg | 6c709a3 | 2011-05-06 14:52:41 -0400 | [diff] [blame] | 759 | static struct wlsc_compositor * |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 760 | drm_compositor_create(struct wl_display *display, |
| 761 | int connector, const char *seat) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 762 | { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 763 | struct drm_compositor *ec; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 764 | struct udev_enumerate *e; |
| 765 | struct udev_list_entry *entry; |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 766 | struct udev_device *device, *drm_device; |
| 767 | const char *path, *device_seat; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 768 | struct wl_event_loop *loop; |
| 769 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 770 | ec = malloc(sizeof *ec); |
| 771 | if (ec == NULL) |
| 772 | return NULL; |
| 773 | |
| 774 | memset(ec, 0, sizeof *ec); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 775 | ec->udev = udev_new(); |
| 776 | if (ec->udev == NULL) { |
| 777 | fprintf(stderr, "failed to initialize udev context\n"); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 778 | return NULL; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 779 | } |
| 780 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 781 | e = udev_enumerate_new(ec->udev); |
| 782 | udev_enumerate_add_match_subsystem(e, "drm"); |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 783 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 784 | udev_enumerate_scan_devices(e); |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 785 | drm_device = NULL; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 786 | udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) { |
| 787 | path = udev_list_entry_get_name(entry); |
| 788 | device = udev_device_new_from_syspath(ec->udev, path); |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 789 | device_seat = |
| 790 | udev_device_get_property_value(device, "ID_SEAT"); |
| 791 | if (!device_seat) |
| 792 | device_seat = default_seat; |
| 793 | if (strcmp(device_seat, seat) == 0) { |
| 794 | drm_device = device; |
| 795 | break; |
| 796 | } |
| 797 | udev_device_unref(device); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 798 | } |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 799 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 800 | udev_enumerate_unref(e); |
| 801 | |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 802 | if (drm_device == NULL) { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 803 | fprintf(stderr, "no drm device found\n"); |
| 804 | return NULL; |
| 805 | } |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 806 | |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 807 | ec->base.wl_display = display; |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 808 | if (init_egl(ec, drm_device) < 0) { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 809 | fprintf(stderr, "failed to initialize egl\n"); |
| 810 | return NULL; |
| 811 | } |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 812 | |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 813 | udev_device_unref(drm_device); |
| 814 | |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 815 | ec->base.destroy = drm_destroy; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 816 | ec->base.create_cursor_image = drm_compositor_create_cursor_image; |
| 817 | |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 818 | ec->base.focus = 1; |
| 819 | |
Benjamin Franzke | 5b2cb6f | 2011-02-18 16:54:55 +0100 | [diff] [blame] | 820 | glGenFramebuffers(1, &ec->base.fbo); |
| 821 | glBindFramebuffer(GL_FRAMEBUFFER, ec->base.fbo); |
| 822 | |
Benjamin Franzke | d59eb1c | 2011-04-29 22:14:54 +0200 | [diff] [blame] | 823 | /* Can't init base class until we have a current egl context */ |
| 824 | if (wlsc_compositor_init(&ec->base, display) < 0) |
| 825 | return NULL; |
| 826 | |
Kristian Høgsberg | 61a8251 | 2010-10-26 11:26:44 -0400 | [diff] [blame] | 827 | if (create_outputs(ec, connector) < 0) { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 828 | fprintf(stderr, "failed to create output for %s\n", path); |
| 829 | return NULL; |
| 830 | } |
| 831 | |
Kristian Høgsberg | 86ec8e8 | 2011-07-19 16:10:11 -0700 | [diff] [blame] | 832 | evdev_input_add_devices(&ec->base, ec->udev, seat); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 833 | |
| 834 | loop = wl_display_get_event_loop(ec->base.wl_display); |
| 835 | ec->drm_source = |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 836 | wl_event_loop_add_fd(loop, ec->drm.fd, |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 837 | WL_EVENT_READABLE, on_drm_input, ec); |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 838 | ec->tty = tty_create(&ec->base, vt_func); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 839 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 840 | ec->udev_monitor = udev_monitor_new_from_netlink(ec->udev, "udev"); |
| 841 | if (ec->udev_monitor == NULL) { |
| 842 | fprintf(stderr, "failed to intialize udev monitor\n"); |
| 843 | return NULL; |
| 844 | } |
| 845 | udev_monitor_filter_add_match_subsystem_devtype(ec->udev_monitor, |
| 846 | "drm", NULL); |
| 847 | ec->udev_drm_source = |
| 848 | wl_event_loop_add_fd(loop, udev_monitor_get_fd(ec->udev_monitor), |
| 849 | WL_EVENT_READABLE, udev_drm_event, ec); |
| 850 | |
| 851 | if (udev_monitor_enable_receiving(ec->udev_monitor) < 0) { |
| 852 | fprintf(stderr, "failed to enable udev-monitor receiving\n"); |
| 853 | return NULL; |
| 854 | } |
| 855 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 856 | return &ec->base; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 857 | } |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 858 | |
| 859 | struct wlsc_compositor * |
Kristian Høgsberg | 6c709a3 | 2011-05-06 14:52:41 -0400 | [diff] [blame] | 860 | backend_init(struct wl_display *display, char *options); |
| 861 | |
| 862 | WL_EXPORT struct wlsc_compositor * |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 863 | backend_init(struct wl_display *display, char *options) |
| 864 | { |
Kristian Høgsberg | 8050bd2 | 2011-05-04 15:08:04 -0400 | [diff] [blame] | 865 | int connector = 0, i; |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 866 | const char *seat; |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 867 | char *p, *value; |
| 868 | |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 869 | static char * const tokens[] = { "connector", "seat", NULL }; |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 870 | |
| 871 | p = options; |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 872 | seat = default_seat; |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 873 | while (i = getsubopt(&p, tokens, &value), i != -1) { |
| 874 | switch (i) { |
| 875 | case 0: |
Kristian Høgsberg | 8050bd2 | 2011-05-04 15:08:04 -0400 | [diff] [blame] | 876 | connector = strtol(value, NULL, 0); |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 877 | break; |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 878 | case 1: |
| 879 | seat = value; |
| 880 | break; |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 881 | } |
| 882 | } |
| 883 | |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 884 | return drm_compositor_create(display, connector, seat); |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 885 | } |