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