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