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 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 26 | #include <errno.h> |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 27 | #include <stdio.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
| 30 | #include <fcntl.h> |
| 31 | #include <unistd.h> |
| 32 | |
Benjamin Franzke | c649a92 | 2011-03-02 11:56:04 +0100 | [diff] [blame] | 33 | #include <xf86drm.h> |
| 34 | #include <xf86drmMode.h> |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 35 | #include <drm_fourcc.h> |
Benjamin Franzke | c649a92 | 2011-03-02 11:56:04 +0100 | [diff] [blame] | 36 | |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 37 | #include <gbm.h> |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 38 | #include <libbacklight.h> |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 39 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 40 | #include "compositor.h" |
Tiago Vignatti | ce03ec3 | 2011-12-19 01:14:03 +0200 | [diff] [blame] | 41 | #include "evdev.h" |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 42 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 43 | struct drm_compositor { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 44 | struct weston_compositor base; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 45 | |
| 46 | struct udev *udev; |
| 47 | struct wl_event_source *drm_source; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 48 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 49 | struct udev_monitor *udev_monitor; |
| 50 | struct wl_event_source *udev_drm_source; |
| 51 | |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 52 | struct { |
David Herrmann | d7488c2 | 2012-03-11 20:05:21 +0100 | [diff] [blame^] | 53 | int id; |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 54 | int fd; |
| 55 | } drm; |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 56 | struct gbm_device *gbm; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 57 | uint32_t *crtcs; |
| 58 | int num_crtcs; |
Marty Jack | 13d9db2 | 2011-02-09 19:01:42 -0500 | [diff] [blame] | 59 | uint32_t crtc_allocator; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 60 | uint32_t connector_allocator; |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 61 | struct tty *tty; |
Pekka Paalanen | bce2d3f | 2011-12-02 13:07:27 +0200 | [diff] [blame] | 62 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 63 | struct wl_list sprite_list; |
Kristian Høgsberg | 65bec24 | 2012-03-05 19:57:35 -0500 | [diff] [blame] | 64 | int sprites_are_broken; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 65 | |
Pekka Paalanen | bce2d3f | 2011-12-02 13:07:27 +0200 | [diff] [blame] | 66 | uint32_t prev_state; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 67 | }; |
| 68 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 69 | struct drm_mode { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 70 | struct weston_mode base; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 71 | drmModeModeInfo mode_info; |
| 72 | }; |
| 73 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 74 | struct drm_output { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 75 | struct weston_output base; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 76 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 77 | uint32_t crtc_id; |
| 78 | uint32_t connector_id; |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 79 | drmModeCrtcPtr original_crtc; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 80 | GLuint rbo[2]; |
| 81 | uint32_t fb_id[2]; |
| 82 | EGLImageKHR image[2]; |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 83 | struct gbm_bo *bo[2]; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 84 | uint32_t current; |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 85 | |
| 86 | uint32_t fs_surf_fb_id; |
| 87 | uint32_t pending_fs_surf_fb_id; |
Kristian Høgsberg | 9f404b7 | 2012-01-26 00:11:01 -0500 | [diff] [blame] | 88 | struct wl_buffer *scanout_buffer; |
| 89 | struct wl_listener scanout_buffer_destroy_listener; |
| 90 | struct wl_buffer *pending_scanout_buffer; |
| 91 | struct wl_listener pending_scanout_buffer_destroy_listener; |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 92 | struct backlight *backlight; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 93 | }; |
| 94 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 95 | /* |
| 96 | * An output has a primary display plane plus zero or more sprites for |
| 97 | * blending display contents. |
| 98 | */ |
| 99 | struct drm_sprite { |
| 100 | struct wl_list link; |
| 101 | |
| 102 | uint32_t fb_id; |
| 103 | uint32_t pending_fb_id; |
| 104 | struct weston_surface *surface; |
| 105 | struct weston_surface *pending_surface; |
| 106 | |
| 107 | struct drm_compositor *compositor; |
| 108 | |
| 109 | struct wl_listener destroy_listener; |
| 110 | struct wl_listener pending_destroy_listener; |
| 111 | |
| 112 | uint32_t possible_crtcs; |
| 113 | uint32_t plane_id; |
| 114 | uint32_t count_formats; |
| 115 | |
| 116 | int32_t src_x, src_y; |
| 117 | uint32_t src_w, src_h; |
| 118 | uint32_t dest_x, dest_y; |
| 119 | uint32_t dest_w, dest_h; |
| 120 | |
| 121 | uint32_t formats[]; |
| 122 | }; |
| 123 | |
| 124 | static int |
| 125 | surface_is_primary(struct weston_compositor *ec, struct weston_surface *es) |
| 126 | { |
| 127 | struct weston_surface *primary; |
| 128 | |
| 129 | primary = container_of(ec->surface_list.next, struct weston_surface, |
| 130 | link); |
| 131 | if (es == primary) |
| 132 | return -1; |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | static int |
| 137 | drm_sprite_crtc_supported(struct weston_output *output_base, uint32_t supported) |
| 138 | { |
| 139 | struct weston_compositor *ec = output_base->compositor; |
| 140 | struct drm_compositor *c =(struct drm_compositor *) ec; |
| 141 | struct drm_output *output = (struct drm_output *) output_base; |
| 142 | int crtc; |
| 143 | |
| 144 | for (crtc = 0; crtc < c->num_crtcs; crtc++) { |
| 145 | if (c->crtcs[crtc] != output->crtc_id) |
| 146 | continue; |
| 147 | |
| 148 | if (supported & (1 << crtc)) |
| 149 | return -1; |
| 150 | } |
| 151 | |
| 152 | return 0; |
| 153 | } |
| 154 | |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 155 | static int |
| 156 | drm_output_prepare_scanout_surface(struct drm_output *output) |
| 157 | { |
| 158 | struct drm_compositor *c = |
| 159 | (struct drm_compositor *) output->base.compositor; |
| 160 | struct weston_surface *es; |
| 161 | EGLint handle, stride; |
| 162 | int ret; |
| 163 | uint32_t fb_id = 0; |
| 164 | struct gbm_bo *bo; |
| 165 | |
| 166 | es = container_of(c->base.surface_list.next, |
| 167 | struct weston_surface, link); |
| 168 | |
Kristian Høgsberg | 101cb65 | 2012-02-17 10:45:16 -0500 | [diff] [blame] | 169 | /* Need to verify output->region contained in surface opaque |
| 170 | * region. Or maybe just that format doesn't have alpha. */ |
| 171 | return -1; |
| 172 | |
| 173 | if (es->geometry.x != output->base.x || |
Pekka Paalanen | ba3cf95 | 2012-01-25 16:22:05 +0200 | [diff] [blame] | 174 | es->geometry.y != output->base.y || |
Pekka Paalanen | 60921e5 | 2012-01-25 15:55:43 +0200 | [diff] [blame] | 175 | es->geometry.width != output->base.current->width || |
| 176 | es->geometry.height != output->base.current->height || |
Pekka Paalanen | f1f5b36 | 2012-01-25 14:33:33 +0200 | [diff] [blame] | 177 | es->transform.enabled || |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 178 | es->image == EGL_NO_IMAGE_KHR) |
| 179 | return -1; |
| 180 | |
| 181 | bo = gbm_bo_create_from_egl_image(c->gbm, |
| 182 | c->base.display, es->image, |
Pekka Paalanen | 60921e5 | 2012-01-25 15:55:43 +0200 | [diff] [blame] | 183 | es->geometry.width, |
| 184 | es->geometry.height, |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 185 | GBM_BO_USE_SCANOUT); |
| 186 | |
| 187 | handle = gbm_bo_get_handle(bo).s32; |
| 188 | stride = gbm_bo_get_pitch(bo); |
| 189 | |
| 190 | gbm_bo_destroy(bo); |
| 191 | |
| 192 | if (handle == 0) |
| 193 | return -1; |
| 194 | |
| 195 | ret = drmModeAddFB(c->drm.fd, |
| 196 | output->base.current->width, |
| 197 | output->base.current->height, |
| 198 | 24, 32, stride, handle, &fb_id); |
| 199 | |
| 200 | if (ret) |
| 201 | return -1; |
| 202 | |
| 203 | output->pending_fs_surf_fb_id = fb_id; |
| 204 | |
| 205 | /* assert output->pending_scanout_buffer == NULL */ |
Kristian Høgsberg | 9f404b7 | 2012-01-26 00:11:01 -0500 | [diff] [blame] | 206 | output->pending_scanout_buffer = es->buffer; |
| 207 | output->pending_scanout_buffer->busy_count++; |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 208 | |
Kristian Høgsberg | 9f404b7 | 2012-01-26 00:11:01 -0500 | [diff] [blame] | 209 | wl_list_insert(output->pending_scanout_buffer->resource.destroy_listener_list.prev, |
| 210 | &output->pending_scanout_buffer_destroy_listener.link); |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 211 | |
| 212 | pixman_region32_fini(&es->damage); |
| 213 | pixman_region32_init(&es->damage); |
| 214 | |
| 215 | return 0; |
| 216 | } |
| 217 | |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 218 | static void |
Kristian Høgsberg | 6ddcdae | 2012-02-28 22:31:58 -0500 | [diff] [blame] | 219 | drm_output_repaint(struct weston_output *output_base, |
| 220 | pixman_region32_t *damage) |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 221 | { |
| 222 | struct drm_output *output = (struct drm_output *) output_base; |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 223 | struct drm_compositor *compositor = |
| 224 | (struct drm_compositor *) output->base.compositor; |
| 225 | struct weston_surface *surface; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 226 | struct drm_sprite *s; |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 227 | uint32_t fb_id = 0; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 228 | int ret = 0; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 229 | |
| 230 | glFramebufferRenderbuffer(GL_FRAMEBUFFER, |
| 231 | GL_COLOR_ATTACHMENT0, |
| 232 | GL_RENDERBUFFER, |
| 233 | output->rbo[output->current]); |
| 234 | |
| 235 | if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 236 | return; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 237 | |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 238 | drm_output_prepare_scanout_surface(output); |
| 239 | |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 240 | wl_list_for_each_reverse(surface, &compositor->base.surface_list, link) |
Kristian Høgsberg | 6ddcdae | 2012-02-28 22:31:58 -0500 | [diff] [blame] | 241 | weston_surface_draw(surface, &output->base, damage); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 242 | |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 243 | glFlush(); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 244 | |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 245 | output->current ^= 1; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 246 | |
Kristian Høgsberg | 191454e | 2011-10-20 17:51:45 -0400 | [diff] [blame] | 247 | if (output->pending_fs_surf_fb_id != 0) { |
| 248 | fb_id = output->pending_fs_surf_fb_id; |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 249 | } else { |
| 250 | fb_id = output->fb_id[output->current ^ 1]; |
| 251 | } |
| 252 | |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 253 | if (drmModePageFlip(compositor->drm.fd, output->crtc_id, |
Kristian Høgsberg | 54f14c3 | 2012-01-18 11:47:41 -0500 | [diff] [blame] | 254 | fb_id, |
| 255 | DRM_MODE_PAGE_FLIP_EVENT, output) < 0) { |
| 256 | fprintf(stderr, "queueing pageflip failed: %m\n"); |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 257 | return; |
Kristian Høgsberg | 54f14c3 | 2012-01-18 11:47:41 -0500 | [diff] [blame] | 258 | } |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 259 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 260 | /* |
| 261 | * Now, update all the sprite surfaces |
| 262 | */ |
| 263 | wl_list_for_each(s, &compositor->sprite_list, link) { |
| 264 | uint32_t flags = 0; |
| 265 | drmVBlank vbl = { |
| 266 | .request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT, |
| 267 | .request.sequence = 1, |
| 268 | }; |
| 269 | |
| 270 | if (!drm_sprite_crtc_supported(output_base, s->possible_crtcs)) |
| 271 | continue; |
| 272 | |
| 273 | ret = drmModeSetPlane(compositor->drm.fd, s->plane_id, |
| 274 | output->crtc_id, s->pending_fb_id, flags, |
| 275 | s->dest_x, s->dest_y, |
| 276 | s->dest_w, s->dest_h, |
| 277 | s->src_x, s->src_y, |
| 278 | s->src_w, s->src_h); |
| 279 | if (ret) |
| 280 | fprintf(stderr, "setplane failed: %d: %s\n", |
| 281 | ret, strerror(errno)); |
| 282 | |
| 283 | /* |
| 284 | * Queue a vblank signal so we know when the surface |
| 285 | * becomes active on the display or has been replaced. |
| 286 | */ |
| 287 | vbl.request.signal = (unsigned long)s; |
| 288 | ret = drmWaitVBlank(compositor->drm.fd, &vbl); |
| 289 | if (ret) { |
| 290 | fprintf(stderr, "vblank event request failed: %d: %s\n", |
| 291 | ret, strerror(errno)); |
| 292 | } |
| 293 | } |
| 294 | |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 295 | return; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | static void |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 299 | vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec, |
| 300 | void *data) |
| 301 | { |
| 302 | struct drm_sprite *s = (struct drm_sprite *)data; |
| 303 | struct drm_compositor *c = s->compositor; |
| 304 | |
| 305 | if (s->surface) { |
| 306 | weston_buffer_post_release(s->surface->buffer); |
| 307 | wl_list_remove(&s->destroy_listener.link); |
| 308 | s->surface = NULL; |
| 309 | drmModeRmFB(c->drm.fd, s->fb_id); |
| 310 | s->fb_id = 0; |
| 311 | } |
| 312 | |
| 313 | if (s->pending_surface) { |
| 314 | wl_list_remove(&s->pending_destroy_listener.link); |
| 315 | wl_list_insert(s->pending_surface->buffer->resource.destroy_listener_list.prev, |
| 316 | &s->destroy_listener.link); |
| 317 | s->surface = s->pending_surface; |
| 318 | s->pending_surface = NULL; |
| 319 | s->fb_id = s->pending_fb_id; |
| 320 | s->pending_fb_id = 0; |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | static void |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 325 | page_flip_handler(int fd, unsigned int frame, |
| 326 | unsigned int sec, unsigned int usec, void *data) |
| 327 | { |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 328 | struct drm_output *output = (struct drm_output *) data; |
| 329 | struct drm_compositor *c = |
| 330 | (struct drm_compositor *) output->base.compositor; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 331 | uint32_t msecs; |
| 332 | |
Kristian Høgsberg | 9f404b7 | 2012-01-26 00:11:01 -0500 | [diff] [blame] | 333 | if (output->scanout_buffer) { |
| 334 | weston_buffer_post_release(output->scanout_buffer); |
| 335 | wl_list_remove(&output->scanout_buffer_destroy_listener.link); |
| 336 | output->scanout_buffer = NULL; |
Kristian Høgsberg | 191454e | 2011-10-20 17:51:45 -0400 | [diff] [blame] | 337 | drmModeRmFB(c->drm.fd, output->fs_surf_fb_id); |
| 338 | output->fs_surf_fb_id = 0; |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 339 | } |
| 340 | |
Kristian Høgsberg | 9f404b7 | 2012-01-26 00:11:01 -0500 | [diff] [blame] | 341 | if (output->pending_scanout_buffer) { |
| 342 | output->scanout_buffer = output->pending_scanout_buffer; |
| 343 | wl_list_remove(&output->pending_scanout_buffer_destroy_listener.link); |
| 344 | wl_list_insert(output->scanout_buffer->resource.destroy_listener_list.prev, |
| 345 | &output->scanout_buffer_destroy_listener.link); |
| 346 | output->pending_scanout_buffer = NULL; |
Kristian Høgsberg | 191454e | 2011-10-20 17:51:45 -0400 | [diff] [blame] | 347 | output->fs_surf_fb_id = output->pending_fs_surf_fb_id; |
| 348 | output->pending_fs_surf_fb_id = 0; |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 349 | } |
| 350 | |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 351 | msecs = sec * 1000 + usec / 1000; |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 352 | weston_output_finish_frame(&output->base, msecs); |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | static int |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 356 | drm_surface_format_supported(struct drm_sprite *s, uint32_t format) |
| 357 | { |
| 358 | int i; |
| 359 | |
| 360 | for (i = 0; i < s->count_formats; i++) |
| 361 | if (s->formats[i] == format) |
| 362 | return 1; |
| 363 | |
| 364 | return 0; |
| 365 | } |
| 366 | |
| 367 | static int |
| 368 | drm_surface_transform_supported(struct weston_surface *es) |
| 369 | { |
| 370 | if (es->transform.enabled) |
| 371 | return 0; |
| 372 | |
| 373 | return 1; |
| 374 | } |
| 375 | |
| 376 | static int |
| 377 | drm_surface_overlap_supported(struct weston_output *output_base, |
| 378 | pixman_region32_t *overlap) |
| 379 | { |
| 380 | /* We could potentially use a color key here if the surface left |
| 381 | * to display has rectangular regions |
| 382 | */ |
| 383 | if (pixman_region32_not_empty(overlap)) |
| 384 | return 0; |
| 385 | return 1; |
| 386 | } |
| 387 | |
| 388 | static void |
| 389 | drm_disable_unused_sprites(struct weston_output *output_base) |
| 390 | { |
| 391 | struct weston_compositor *ec = output_base->compositor; |
| 392 | struct drm_compositor *c =(struct drm_compositor *) ec; |
| 393 | struct drm_output *output = (struct drm_output *) output_base; |
| 394 | struct drm_sprite *s; |
| 395 | int ret; |
| 396 | |
| 397 | wl_list_for_each(s, &c->sprite_list, link) { |
| 398 | if (s->pending_fb_id) |
| 399 | continue; |
| 400 | |
| 401 | ret = drmModeSetPlane(c->drm.fd, s->plane_id, |
| 402 | output->crtc_id, 0, 0, |
| 403 | 0, 0, 0, 0, 0, 0, 0, 0); |
| 404 | if (ret) |
| 405 | fprintf(stderr, |
| 406 | "failed to disable plane: %d: %s\n", |
| 407 | ret, strerror(errno)); |
| 408 | drmModeRmFB(c->drm.fd, s->fb_id); |
| 409 | s->surface = NULL; |
| 410 | s->pending_surface = NULL; |
| 411 | s->fb_id = 0; |
| 412 | s->pending_fb_id = 0; |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | /* |
| 417 | * This function must take care to damage any previously assigned surface |
| 418 | * if the sprite ends up binding to a different surface than in the |
| 419 | * previous frame. |
| 420 | */ |
| 421 | static int |
| 422 | drm_output_prepare_overlay_surface(struct weston_output *output_base, |
| 423 | struct weston_surface *es, |
| 424 | pixman_region32_t *overlap) |
| 425 | { |
| 426 | struct weston_compositor *ec = output_base->compositor; |
| 427 | struct drm_compositor *c =(struct drm_compositor *) ec; |
| 428 | struct drm_sprite *s; |
| 429 | int found = 0; |
| 430 | EGLint handle, stride; |
| 431 | struct gbm_bo *bo; |
| 432 | uint32_t fb_id = 0; |
| 433 | uint32_t handles[4], pitches[4], offsets[4]; |
| 434 | int ret = 0; |
| 435 | pixman_region32_t dest_rect, src_rect; |
| 436 | pixman_box32_t *box; |
| 437 | uint32_t format; |
| 438 | |
Kristian Høgsberg | 65bec24 | 2012-03-05 19:57:35 -0500 | [diff] [blame] | 439 | if (c->sprites_are_broken) |
| 440 | return -1; |
| 441 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 442 | if (surface_is_primary(ec, es)) |
| 443 | return -1; |
| 444 | |
| 445 | if (!es->buffer) |
| 446 | return -1; |
| 447 | |
| 448 | if (!drm_surface_transform_supported(es)) |
| 449 | return -1; |
| 450 | |
| 451 | if (!drm_surface_overlap_supported(output_base, overlap)) |
| 452 | return -1; |
| 453 | |
| 454 | wl_list_for_each(s, &c->sprite_list, link) { |
| 455 | if (!drm_sprite_crtc_supported(output_base, s->possible_crtcs)) |
| 456 | continue; |
| 457 | |
| 458 | if (!s->pending_fb_id) { |
| 459 | found = 1; |
| 460 | break; |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | /* No sprites available */ |
| 465 | if (!found) |
| 466 | return -1; |
| 467 | |
| 468 | bo = gbm_bo_create_from_egl_image(c->gbm, c->base.display, es->image, |
| 469 | es->geometry.width, es->geometry.height, |
| 470 | GBM_BO_USE_SCANOUT); |
| 471 | format = gbm_bo_get_format(bo); |
| 472 | handle = gbm_bo_get_handle(bo).s32; |
| 473 | stride = gbm_bo_get_pitch(bo); |
| 474 | |
| 475 | gbm_bo_destroy(bo); |
| 476 | |
| 477 | if (!drm_surface_format_supported(s, format)) |
| 478 | return -1; |
| 479 | |
| 480 | if (!handle) |
| 481 | return -1; |
| 482 | |
| 483 | handles[0] = handle; |
| 484 | pitches[0] = stride; |
| 485 | offsets[0] = 0; |
| 486 | |
| 487 | ret = drmModeAddFB2(c->drm.fd, es->geometry.width, es->geometry.height, |
| 488 | format, handles, pitches, offsets, |
| 489 | &fb_id, 0); |
| 490 | if (ret) { |
| 491 | fprintf(stderr, "addfb2 failed: %d\n", ret); |
Kristian Høgsberg | 65bec24 | 2012-03-05 19:57:35 -0500 | [diff] [blame] | 492 | c->sprites_are_broken = 1; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 493 | return -1; |
| 494 | } |
| 495 | |
| 496 | if (s->surface && s->surface != es) { |
| 497 | struct weston_surface *old_surf = s->surface; |
| 498 | pixman_region32_fini(&old_surf->damage); |
| 499 | pixman_region32_init_rect(&old_surf->damage, |
| 500 | old_surf->geometry.x, old_surf->geometry.y, |
| 501 | old_surf->geometry.width, old_surf->geometry.height); |
| 502 | } |
| 503 | |
| 504 | s->pending_fb_id = fb_id; |
| 505 | s->pending_surface = es; |
| 506 | es->buffer->busy_count++; |
| 507 | |
| 508 | /* |
| 509 | * Calculate the source & dest rects properly based on actual |
| 510 | * postion (note the caller has called weston_surface_update_transform() |
| 511 | * for us already). |
| 512 | */ |
| 513 | pixman_region32_init(&dest_rect); |
| 514 | pixman_region32_intersect(&dest_rect, &es->transform.boundingbox, |
| 515 | &output_base->region); |
| 516 | pixman_region32_translate(&dest_rect, -output_base->x, -output_base->y); |
| 517 | box = pixman_region32_extents(&dest_rect); |
| 518 | s->dest_x = box->x1; |
| 519 | s->dest_y = box->y1; |
| 520 | s->dest_w = box->x2 - box->x1; |
| 521 | s->dest_h = box->y2 - box->y1; |
| 522 | pixman_region32_fini(&dest_rect); |
| 523 | |
| 524 | pixman_region32_init(&src_rect); |
| 525 | pixman_region32_intersect(&src_rect, &es->transform.boundingbox, |
| 526 | &output_base->region); |
| 527 | pixman_region32_translate(&src_rect, -es->geometry.x, -es->geometry.y); |
| 528 | box = pixman_region32_extents(&src_rect); |
| 529 | s->src_x = box->x1; |
| 530 | s->src_y = box->y1; |
| 531 | s->src_w = box->x2 - box->x1; |
| 532 | s->src_h = box->y2 - box->y1; |
| 533 | pixman_region32_fini(&src_rect); |
| 534 | |
| 535 | |
| 536 | wl_list_insert(es->buffer->resource.destroy_listener_list.prev, |
| 537 | &s->pending_destroy_listener.link); |
| 538 | return 0; |
| 539 | } |
| 540 | |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 541 | static int |
| 542 | drm_output_set_cursor(struct weston_output *output_base, |
| 543 | struct weston_input_device *eid); |
| 544 | |
| 545 | static void |
| 546 | weston_output_set_cursor(struct weston_output *output, |
| 547 | struct weston_input_device *device, |
| 548 | pixman_region32_t *overlap) |
| 549 | { |
| 550 | pixman_region32_t cursor_region; |
| 551 | int prior_was_hardware; |
| 552 | |
| 553 | if (device->sprite == NULL) |
| 554 | return; |
| 555 | |
| 556 | pixman_region32_init(&cursor_region); |
| 557 | pixman_region32_intersect(&cursor_region, |
| 558 | &device->sprite->transform.boundingbox, |
| 559 | &output->region); |
| 560 | |
| 561 | if (!pixman_region32_not_empty(&cursor_region)) { |
| 562 | drm_output_set_cursor(output, NULL); |
| 563 | goto out; |
| 564 | } |
| 565 | |
| 566 | prior_was_hardware = device->hw_cursor; |
| 567 | if (pixman_region32_not_empty(overlap) || |
| 568 | drm_output_set_cursor(output, device) < 0) { |
| 569 | if (prior_was_hardware) { |
| 570 | weston_surface_damage(device->sprite); |
| 571 | drm_output_set_cursor(output, NULL); |
| 572 | } |
| 573 | device->hw_cursor = 0; |
| 574 | } else { |
| 575 | if (!prior_was_hardware) |
| 576 | weston_surface_damage_below(device->sprite); |
| 577 | pixman_region32_fini(&device->sprite->damage); |
| 578 | pixman_region32_init(&device->sprite->damage); |
| 579 | device->hw_cursor = 1; |
| 580 | } |
| 581 | |
| 582 | out: |
| 583 | pixman_region32_fini(&cursor_region); |
| 584 | } |
| 585 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 586 | static void |
| 587 | drm_assign_planes(struct weston_output *output) |
| 588 | { |
| 589 | struct weston_compositor *ec = output->compositor; |
| 590 | struct weston_surface *es; |
| 591 | pixman_region32_t overlap, surface_overlap; |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 592 | struct weston_input_device *device; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 593 | |
| 594 | /* |
| 595 | * Find a surface for each sprite in the output using some heuristics: |
| 596 | * 1) size |
| 597 | * 2) frequency of update |
| 598 | * 3) opacity (though some hw might support alpha blending) |
| 599 | * 4) clipping (this can be fixed with color keys) |
| 600 | * |
| 601 | * The idea is to save on blitting since this should save power. |
| 602 | * If we can get a large video surface on the sprite for example, |
| 603 | * the main display surface may not need to update at all, and |
| 604 | * the client buffer can be used directly for the sprite surface |
| 605 | * as we do for flipping full screen surfaces. |
| 606 | */ |
| 607 | pixman_region32_init(&overlap); |
| 608 | wl_list_for_each(es, &ec->surface_list, link) { |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 609 | /* |
| 610 | * FIXME: try to assign hw cursors here too, they're just |
| 611 | * special overlays |
| 612 | */ |
| 613 | pixman_region32_init(&surface_overlap); |
| 614 | pixman_region32_intersect(&surface_overlap, &overlap, |
| 615 | &es->transform.boundingbox); |
| 616 | |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 617 | device = (struct weston_input_device *) ec->input_device; |
| 618 | if (es == device->sprite) { |
| 619 | weston_output_set_cursor(output, device, |
| 620 | &surface_overlap); |
| 621 | |
| 622 | if (!device->hw_cursor) |
| 623 | pixman_region32_union(&overlap, &overlap, |
| 624 | &es->transform.boundingbox); |
| 625 | } else if (!drm_output_prepare_overlay_surface(output, es, |
| 626 | &surface_overlap)) { |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 627 | pixman_region32_fini(&es->damage); |
| 628 | pixman_region32_init(&es->damage); |
| 629 | } else { |
| 630 | pixman_region32_union(&overlap, &overlap, |
| 631 | &es->transform.boundingbox); |
| 632 | } |
| 633 | pixman_region32_fini(&surface_overlap); |
| 634 | } |
| 635 | pixman_region32_fini(&overlap); |
| 636 | |
| 637 | drm_disable_unused_sprites(output); |
| 638 | } |
| 639 | |
| 640 | static int |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 641 | drm_output_set_cursor(struct weston_output *output_base, |
| 642 | struct weston_input_device *eid) |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 643 | { |
| 644 | struct drm_output *output = (struct drm_output *) output_base; |
| 645 | struct drm_compositor *c = |
| 646 | (struct drm_compositor *) output->base.compositor; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 647 | EGLint handle, stride; |
| 648 | int ret = -1; |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 649 | struct gbm_bo *bo; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 650 | |
Kristian Høgsberg | e4c40a4 | 2011-05-06 14:04:21 -0400 | [diff] [blame] | 651 | if (eid == NULL) { |
| 652 | drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0); |
| 653 | return 0; |
| 654 | } |
| 655 | |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 656 | if (eid->sprite->image == EGL_NO_IMAGE_KHR) |
| 657 | goto out; |
| 658 | |
Pekka Paalanen | 60921e5 | 2012-01-25 15:55:43 +0200 | [diff] [blame] | 659 | if (eid->sprite->geometry.width > 64 || |
| 660 | eid->sprite->geometry.height > 64) |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 661 | goto out; |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 662 | |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 663 | bo = gbm_bo_create_from_egl_image(c->gbm, |
| 664 | c->base.display, |
| 665 | eid->sprite->image, 64, 64, |
| 666 | GBM_BO_USE_CURSOR_64X64); |
Kristian Høgsberg | 6916d9c | 2012-01-18 15:11:07 -0500 | [diff] [blame] | 667 | /* Not suitable for hw cursor, fall back */ |
| 668 | if (bo == NULL) |
| 669 | goto out; |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 670 | |
| 671 | handle = gbm_bo_get_handle(bo).s32; |
| 672 | stride = gbm_bo_get_pitch(bo); |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 673 | gbm_bo_destroy(bo); |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 674 | |
Kristian Høgsberg | 6916d9c | 2012-01-18 15:11:07 -0500 | [diff] [blame] | 675 | /* gbm_bo_create_from_egl_image() didn't always validate the usage |
| 676 | * flags, and in that case we might end up with a bad stride. */ |
| 677 | if (stride != 64 * 4) |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 678 | goto out; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 679 | |
| 680 | ret = drmModeSetCursor(c->drm.fd, output->crtc_id, handle, 64, 64); |
| 681 | if (ret) { |
| 682 | fprintf(stderr, "failed to set cursor: %s\n", strerror(-ret)); |
| 683 | goto out; |
| 684 | } |
| 685 | |
| 686 | ret = drmModeMoveCursor(c->drm.fd, output->crtc_id, |
Pekka Paalanen | ba3cf95 | 2012-01-25 16:22:05 +0200 | [diff] [blame] | 687 | eid->sprite->geometry.x - output->base.x, |
| 688 | eid->sprite->geometry.y - output->base.y); |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 689 | if (ret) { |
| 690 | fprintf(stderr, "failed to move cursor: %s\n", strerror(-ret)); |
| 691 | goto out; |
| 692 | } |
| 693 | |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 694 | out: |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 695 | if (ret) |
| 696 | drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0); |
| 697 | return ret; |
| 698 | } |
| 699 | |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 700 | static void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 701 | drm_output_destroy(struct weston_output *output_base) |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 702 | { |
| 703 | struct drm_output *output = (struct drm_output *) output_base; |
| 704 | struct drm_compositor *c = |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 705 | (struct drm_compositor *) output->base.compositor; |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 706 | drmModeCrtcPtr origcrtc = output->original_crtc; |
| 707 | int i; |
| 708 | |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 709 | if (output->backlight) |
| 710 | backlight_destroy(output->backlight); |
| 711 | |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 712 | /* Turn off hardware cursor */ |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 713 | drm_output_set_cursor(&output->base, NULL); |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 714 | |
| 715 | /* Restore original CRTC state */ |
| 716 | drmModeSetCrtc(c->drm.fd, origcrtc->crtc_id, origcrtc->buffer_id, |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 717 | origcrtc->x, origcrtc->y, |
| 718 | &output->connector_id, 1, &origcrtc->mode); |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 719 | drmModeFreeCrtc(origcrtc); |
| 720 | |
Benjamin Franzke | 48c4ea2 | 2011-08-30 11:44:56 +0200 | [diff] [blame] | 721 | glFramebufferRenderbuffer(GL_FRAMEBUFFER, |
| 722 | GL_COLOR_ATTACHMENT0, |
| 723 | GL_RENDERBUFFER, |
| 724 | 0); |
| 725 | |
| 726 | glBindRenderbuffer(GL_RENDERBUFFER, 0); |
| 727 | glDeleteRenderbuffers(2, output->rbo); |
| 728 | |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 729 | /* Destroy output buffers */ |
| 730 | for (i = 0; i < 2; i++) { |
| 731 | drmModeRmFB(c->drm.fd, output->fb_id[i]); |
| 732 | c->base.destroy_image(c->base.display, output->image[i]); |
| 733 | gbm_bo_destroy(output->bo[i]); |
| 734 | } |
| 735 | |
Benjamin Franzke | 48c4ea2 | 2011-08-30 11:44:56 +0200 | [diff] [blame] | 736 | c->crtc_allocator &= ~(1 << output->crtc_id); |
| 737 | c->connector_allocator &= ~(1 << output->connector_id); |
| 738 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 739 | weston_output_destroy(&output->base); |
Benjamin Franzke | 48c4ea2 | 2011-08-30 11:44:56 +0200 | [diff] [blame] | 740 | wl_list_remove(&output->base.link); |
| 741 | |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 742 | free(output); |
| 743 | } |
| 744 | |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 745 | static int |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 746 | on_drm_input(int fd, uint32_t mask, void *data) |
| 747 | { |
| 748 | drmEventContext evctx; |
| 749 | |
| 750 | memset(&evctx, 0, sizeof evctx); |
| 751 | evctx.version = DRM_EVENT_CONTEXT_VERSION; |
| 752 | evctx.page_flip_handler = page_flip_handler; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 753 | evctx.vblank_handler = vblank_handler; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 754 | drmHandleEvent(fd, &evctx); |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 755 | |
| 756 | return 1; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | static int |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 760 | init_egl(struct drm_compositor *ec, struct udev_device *device) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 761 | { |
Kristian Høgsberg | 379b678 | 2010-07-28 22:52:28 -0400 | [diff] [blame] | 762 | EGLint major, minor; |
David Herrmann | d7488c2 | 2012-03-11 20:05:21 +0100 | [diff] [blame^] | 763 | const char *extensions, *filename, *sysnum; |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 764 | int fd; |
Kristian Høgsberg | 2c28aa5 | 2010-07-28 23:47:54 -0400 | [diff] [blame] | 765 | static const EGLint context_attribs[] = { |
| 766 | EGL_CONTEXT_CLIENT_VERSION, 2, |
| 767 | EGL_NONE |
| 768 | }; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 769 | |
David Herrmann | d7488c2 | 2012-03-11 20:05:21 +0100 | [diff] [blame^] | 770 | sysnum = udev_device_get_sysnum(device); |
| 771 | if (sysnum) |
| 772 | ec->drm.id = atoi(sysnum); |
| 773 | if (!sysnum || ec->drm.id < 0) { |
| 774 | fprintf(stderr, "cannot get device sysnum\n"); |
| 775 | return -1; |
| 776 | } |
| 777 | |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 778 | filename = udev_device_get_devnode(device); |
David Herrmann | 63ff706 | 2011-11-05 18:46:01 +0100 | [diff] [blame] | 779 | fd = open(filename, O_RDWR | O_CLOEXEC); |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 780 | if (fd < 0) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 781 | /* Probably permissions error */ |
| 782 | fprintf(stderr, "couldn't open %s, skipping\n", |
| 783 | udev_device_get_devnode(device)); |
| 784 | return -1; |
| 785 | } |
| 786 | |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 787 | ec->drm.fd = fd; |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 788 | ec->gbm = gbm_create_device(ec->drm.fd); |
| 789 | ec->base.display = eglGetDisplay(ec->gbm); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 790 | if (ec->base.display == NULL) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 791 | fprintf(stderr, "failed to create display\n"); |
| 792 | return -1; |
| 793 | } |
| 794 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 795 | if (!eglInitialize(ec->base.display, &major, &minor)) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 796 | fprintf(stderr, "failed to initialize display\n"); |
| 797 | return -1; |
| 798 | } |
| 799 | |
Kristian Høgsberg | 379b678 | 2010-07-28 22:52:28 -0400 | [diff] [blame] | 800 | extensions = eglQueryString(ec->base.display, EGL_EXTENSIONS); |
Ander Conselvan de Oliveira | ef7c8d9 | 2011-11-01 16:37:41 +0200 | [diff] [blame] | 801 | if (!strstr(extensions, "EGL_KHR_surfaceless_gles2")) { |
| 802 | fprintf(stderr, "EGL_KHR_surfaceless_gles2 not available\n"); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 803 | return -1; |
| 804 | } |
| 805 | |
Darxus | 55973f2 | 2010-11-22 21:24:39 -0500 | [diff] [blame] | 806 | if (!eglBindAPI(EGL_OPENGL_ES_API)) { |
| 807 | fprintf(stderr, "failed to bind api EGL_OPENGL_ES_API\n"); |
| 808 | return -1; |
| 809 | } |
| 810 | |
Kristian Høgsberg | 2c28aa5 | 2010-07-28 23:47:54 -0400 | [diff] [blame] | 811 | ec->base.context = eglCreateContext(ec->base.display, NULL, |
| 812 | EGL_NO_CONTEXT, context_attribs); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 813 | if (ec->base.context == NULL) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 814 | fprintf(stderr, "failed to create context\n"); |
| 815 | return -1; |
| 816 | } |
| 817 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 818 | if (!eglMakeCurrent(ec->base.display, EGL_NO_SURFACE, |
| 819 | EGL_NO_SURFACE, ec->base.context)) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 820 | fprintf(stderr, "failed to make context current\n"); |
| 821 | return -1; |
| 822 | } |
| 823 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 824 | return 0; |
| 825 | } |
| 826 | |
| 827 | static drmModeModeInfo builtin_1024x768 = { |
| 828 | 63500, /* clock */ |
| 829 | 1024, 1072, 1176, 1328, 0, |
| 830 | 768, 771, 775, 798, 0, |
| 831 | 59920, |
| 832 | DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC, |
| 833 | 0, |
| 834 | "1024x768" |
| 835 | }; |
| 836 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 837 | |
| 838 | static int |
| 839 | drm_output_add_mode(struct drm_output *output, drmModeModeInfo *info) |
| 840 | { |
| 841 | struct drm_mode *mode; |
| 842 | |
| 843 | mode = malloc(sizeof *mode); |
| 844 | if (mode == NULL) |
| 845 | return -1; |
| 846 | |
| 847 | mode->base.flags = 0; |
| 848 | mode->base.width = info->hdisplay; |
| 849 | mode->base.height = info->vdisplay; |
| 850 | mode->base.refresh = info->vrefresh; |
| 851 | mode->mode_info = *info; |
| 852 | wl_list_insert(output->base.mode_list.prev, &mode->base.link); |
| 853 | |
| 854 | return 0; |
| 855 | } |
| 856 | |
| 857 | static int |
| 858 | drm_subpixel_to_wayland(int drm_value) |
| 859 | { |
| 860 | switch (drm_value) { |
| 861 | default: |
| 862 | case DRM_MODE_SUBPIXEL_UNKNOWN: |
| 863 | return WL_OUTPUT_SUBPIXEL_UNKNOWN; |
| 864 | case DRM_MODE_SUBPIXEL_NONE: |
| 865 | return WL_OUTPUT_SUBPIXEL_NONE; |
| 866 | case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB: |
| 867 | return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB; |
| 868 | case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR: |
| 869 | return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR; |
| 870 | case DRM_MODE_SUBPIXEL_VERTICAL_RGB: |
| 871 | return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB; |
| 872 | case DRM_MODE_SUBPIXEL_VERTICAL_BGR: |
| 873 | return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR; |
| 874 | } |
| 875 | } |
| 876 | |
Kristian Høgsberg | 9f404b7 | 2012-01-26 00:11:01 -0500 | [diff] [blame] | 877 | static void |
| 878 | output_handle_scanout_buffer_destroy(struct wl_listener *listener, |
| 879 | struct wl_resource *resource, |
| 880 | uint32_t time) |
| 881 | { |
| 882 | struct drm_output *output = |
| 883 | container_of(listener, struct drm_output, |
| 884 | scanout_buffer_destroy_listener); |
| 885 | |
| 886 | output->scanout_buffer = NULL; |
| 887 | |
| 888 | if (!output->pending_scanout_buffer) |
| 889 | weston_compositor_schedule_repaint(output->base.compositor); |
| 890 | } |
| 891 | |
| 892 | static void |
| 893 | output_handle_pending_scanout_buffer_destroy(struct wl_listener *listener, |
| 894 | struct wl_resource *resource, |
| 895 | uint32_t time) |
| 896 | { |
| 897 | struct drm_output *output = |
| 898 | container_of(listener, struct drm_output, |
| 899 | pending_scanout_buffer_destroy_listener); |
| 900 | |
| 901 | output->pending_scanout_buffer = NULL; |
| 902 | |
| 903 | weston_compositor_schedule_repaint(output->base.compositor); |
| 904 | } |
| 905 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 906 | static void |
| 907 | sprite_handle_buffer_destroy(struct wl_listener *listener, |
| 908 | struct wl_resource *resource, |
| 909 | uint32_t time) |
| 910 | { |
| 911 | struct drm_sprite *sprite = |
| 912 | container_of(listener, struct drm_sprite, |
| 913 | destroy_listener); |
| 914 | |
| 915 | sprite->surface = NULL; |
| 916 | } |
| 917 | |
| 918 | static void |
| 919 | sprite_handle_pending_buffer_destroy(struct wl_listener *listener, |
| 920 | struct wl_resource *resource, |
| 921 | uint32_t time) |
| 922 | { |
| 923 | struct drm_sprite *sprite = |
| 924 | container_of(listener, struct drm_sprite, |
| 925 | pending_destroy_listener); |
| 926 | |
| 927 | sprite->pending_surface = NULL; |
| 928 | } |
| 929 | |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 930 | /* returns a value between 1-10 range, where higher is brighter */ |
| 931 | static uint32_t |
| 932 | drm_get_backlight(struct drm_output *output) |
| 933 | { |
| 934 | long brightness, max_brightness, norm; |
| 935 | |
| 936 | brightness = backlight_get_brightness(output->backlight); |
| 937 | max_brightness = backlight_get_max_brightness(output->backlight); |
| 938 | |
| 939 | /* convert it on a scale of 1 to 10 */ |
| 940 | norm = 1 + ((brightness) * 9)/(max_brightness); |
| 941 | |
| 942 | return (uint32_t) norm; |
| 943 | } |
| 944 | |
| 945 | /* values accepted are between 1-10 range */ |
| 946 | static void |
| 947 | drm_set_backlight(struct weston_output *output_base, uint32_t value) |
| 948 | { |
| 949 | struct drm_output *output = (struct drm_output *) output_base; |
| 950 | long max_brightness, new_brightness; |
| 951 | |
| 952 | if (!output->backlight) |
| 953 | return; |
| 954 | |
| 955 | if (value < 1 || value > 10) |
| 956 | return; |
| 957 | |
| 958 | max_brightness = backlight_get_max_brightness(output->backlight); |
| 959 | |
| 960 | /* get denormalized value */ |
| 961 | new_brightness = ((value - 1) * (max_brightness)) / 9; |
| 962 | |
| 963 | backlight_set_brightness(output->backlight, new_brightness); |
| 964 | } |
| 965 | |
| 966 | static drmModePropertyPtr |
| 967 | drm_get_prop(int fd, drmModeConnectorPtr connector, const char *name) |
| 968 | { |
| 969 | drmModePropertyPtr props; |
| 970 | int i; |
| 971 | |
| 972 | for (i = 0; i < connector->count_props; i++) { |
| 973 | props = drmModeGetProperty(fd, connector->props[i]); |
| 974 | if (!props) |
| 975 | continue; |
| 976 | |
| 977 | if (!strcmp(props->name, name)) |
| 978 | return props; |
| 979 | |
| 980 | drmModeFreeProperty(props); |
| 981 | } |
| 982 | |
| 983 | return NULL; |
| 984 | } |
| 985 | |
| 986 | static void |
| 987 | drm_set_dpms(struct weston_output *output_base, enum dpms_enum level) |
| 988 | { |
| 989 | struct drm_output *output = (struct drm_output *) output_base; |
| 990 | struct weston_compositor *ec = output_base->compositor; |
| 991 | struct drm_compositor *c = (struct drm_compositor *) ec; |
| 992 | drmModeConnectorPtr connector; |
| 993 | drmModePropertyPtr prop; |
| 994 | |
| 995 | connector = drmModeGetConnector(c->drm.fd, output->connector_id); |
| 996 | if (!connector) |
| 997 | return; |
| 998 | |
| 999 | prop = drm_get_prop(c->drm.fd, connector, "DPMS"); |
| 1000 | if (!prop) { |
| 1001 | drmModeFreeConnector(connector); |
| 1002 | return; |
| 1003 | } |
| 1004 | |
| 1005 | drmModeConnectorSetProperty(c->drm.fd, connector->connector_id, |
| 1006 | prop->prop_id, level); |
| 1007 | drmModeFreeProperty(prop); |
| 1008 | drmModeFreeConnector(connector); |
| 1009 | } |
| 1010 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1011 | static int |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1012 | create_output_for_connector(struct drm_compositor *ec, |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1013 | drmModeRes *resources, |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 1014 | drmModeConnector *connector, |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1015 | int x, int y, struct udev_device *drm_device) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1016 | { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1017 | struct drm_output *output; |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 1018 | struct drm_mode *drm_mode, *next; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1019 | drmModeEncoder *encoder; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1020 | int i, ret; |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 1021 | unsigned handle, stride; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1022 | |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 1023 | encoder = drmModeGetEncoder(ec->drm.fd, connector->encoders[0]); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1024 | if (encoder == NULL) { |
| 1025 | fprintf(stderr, "No encoder for connector.\n"); |
| 1026 | return -1; |
| 1027 | } |
| 1028 | |
| 1029 | for (i = 0; i < resources->count_crtcs; i++) { |
Marty Jack | 13d9db2 | 2011-02-09 19:01:42 -0500 | [diff] [blame] | 1030 | if (encoder->possible_crtcs & (1 << i) && |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1031 | !(ec->crtc_allocator & (1 << resources->crtcs[i]))) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1032 | break; |
| 1033 | } |
| 1034 | if (i == resources->count_crtcs) { |
| 1035 | fprintf(stderr, "No usable crtc for encoder.\n"); |
David Herrmann | eb8bed5 | 2011-12-08 17:05:44 +0100 | [diff] [blame] | 1036 | drmModeFreeEncoder(encoder); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1037 | return -1; |
| 1038 | } |
| 1039 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1040 | output = malloc(sizeof *output); |
David Herrmann | eb8bed5 | 2011-12-08 17:05:44 +0100 | [diff] [blame] | 1041 | if (output == NULL) { |
| 1042 | drmModeFreeEncoder(encoder); |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1043 | return -1; |
David Herrmann | eb8bed5 | 2011-12-08 17:05:44 +0100 | [diff] [blame] | 1044 | } |
David Herrmann | d7c4551 | 2011-12-08 17:05:46 +0100 | [diff] [blame] | 1045 | output->fb_id[0] = -1; |
| 1046 | output->fb_id[1] = -1; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1047 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1048 | memset(output, 0, sizeof *output); |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1049 | output->base.subpixel = drm_subpixel_to_wayland(connector->subpixel); |
| 1050 | output->base.make = "unknown"; |
| 1051 | output->base.model = "unknown"; |
| 1052 | wl_list_init(&output->base.mode_list); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1053 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1054 | output->crtc_id = resources->crtcs[i]; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1055 | ec->crtc_allocator |= (1 << output->crtc_id); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1056 | output->connector_id = connector->connector_id; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1057 | ec->connector_allocator |= (1 << output->connector_id); |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1058 | |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 1059 | output->original_crtc = drmModeGetCrtc(ec->drm.fd, output->crtc_id); |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 1060 | drmModeFreeEncoder(encoder); |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 1061 | |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 1062 | for (i = 0; i < connector->count_modes; i++) { |
| 1063 | ret = drm_output_add_mode(output, &connector->modes[i]); |
| 1064 | if (ret) |
| 1065 | goto err_free; |
| 1066 | } |
| 1067 | |
| 1068 | if (connector->count_modes == 0) { |
| 1069 | ret = drm_output_add_mode(output, &builtin_1024x768); |
| 1070 | if (ret) |
| 1071 | goto err_free; |
| 1072 | } |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1073 | |
| 1074 | drm_mode = container_of(output->base.mode_list.next, |
| 1075 | struct drm_mode, base.link); |
| 1076 | output->base.current = &drm_mode->base; |
| 1077 | drm_mode->base.flags = |
| 1078 | WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED; |
| 1079 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1080 | glGenRenderbuffers(2, output->rbo); |
| 1081 | for (i = 0; i < 2; i++) { |
| 1082 | glBindRenderbuffer(GL_RENDERBUFFER, output->rbo[i]); |
| 1083 | |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 1084 | output->bo[i] = |
| 1085 | gbm_bo_create(ec->gbm, |
| 1086 | output->base.current->width, |
| 1087 | output->base.current->height, |
| 1088 | GBM_BO_FORMAT_XRGB8888, |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1089 | GBM_BO_USE_SCANOUT | |
| 1090 | GBM_BO_USE_RENDERING); |
David Herrmann | d7c4551 | 2011-12-08 17:05:46 +0100 | [diff] [blame] | 1091 | if (!output->bo[i]) |
| 1092 | goto err_bufs; |
| 1093 | |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 1094 | output->image[i] = ec->base.create_image(ec->base.display, |
| 1095 | NULL, |
| 1096 | EGL_NATIVE_PIXMAP_KHR, |
| 1097 | output->bo[i], NULL); |
David Herrmann | d7c4551 | 2011-12-08 17:05:46 +0100 | [diff] [blame] | 1098 | if (!output->image[i]) |
| 1099 | goto err_bufs; |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 1100 | |
Kristian Høgsberg | b5819dc | 2011-04-25 15:08:20 -0400 | [diff] [blame] | 1101 | ec->base.image_target_renderbuffer_storage(GL_RENDERBUFFER, |
| 1102 | output->image[i]); |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 1103 | stride = gbm_bo_get_pitch(output->bo[i]); |
| 1104 | handle = gbm_bo_get_handle(output->bo[i]).u32; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1105 | |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 1106 | ret = drmModeAddFB(ec->drm.fd, |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1107 | output->base.current->width, |
| 1108 | output->base.current->height, |
Kristian Høgsberg | 9c46ff1 | 2011-10-24 13:13:22 -0400 | [diff] [blame] | 1109 | 24, 32, stride, handle, &output->fb_id[i]); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1110 | if (ret) { |
| 1111 | fprintf(stderr, "failed to add fb %d: %m\n", i); |
David Herrmann | d7c4551 | 2011-12-08 17:05:46 +0100 | [diff] [blame] | 1112 | goto err_bufs; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1113 | } |
| 1114 | } |
| 1115 | |
| 1116 | output->current = 0; |
| 1117 | glFramebufferRenderbuffer(GL_FRAMEBUFFER, |
| 1118 | GL_COLOR_ATTACHMENT0, |
| 1119 | GL_RENDERBUFFER, |
| 1120 | output->rbo[output->current]); |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 1121 | ret = drmModeSetCrtc(ec->drm.fd, output->crtc_id, |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1122 | output->fb_id[output->current ^ 1], 0, 0, |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1123 | &output->connector_id, 1, |
| 1124 | &drm_mode->mode_info); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1125 | if (ret) { |
| 1126 | fprintf(stderr, "failed to set mode: %m\n"); |
David Herrmann | d7c4551 | 2011-12-08 17:05:46 +0100 | [diff] [blame] | 1127 | goto err_fb; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1128 | } |
| 1129 | |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1130 | output->backlight = backlight_init(drm_device, |
| 1131 | connector->connector_type); |
| 1132 | if (output->backlight) { |
| 1133 | output->base.set_backlight = drm_set_backlight; |
| 1134 | output->base.backlight_current = drm_get_backlight(output); |
| 1135 | } |
| 1136 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1137 | weston_output_init(&output->base, &ec->base, x, y, |
Kristian Høgsberg | a4b7e20 | 2011-10-24 13:26:32 -0400 | [diff] [blame] | 1138 | connector->mmWidth, connector->mmHeight, 0); |
| 1139 | |
| 1140 | wl_list_insert(ec->base.output_list.prev, &output->base.link); |
| 1141 | |
Kristian Høgsberg | 9f404b7 | 2012-01-26 00:11:01 -0500 | [diff] [blame] | 1142 | output->scanout_buffer_destroy_listener.func = |
| 1143 | output_handle_scanout_buffer_destroy; |
| 1144 | output->pending_scanout_buffer_destroy_listener.func = |
| 1145 | output_handle_pending_scanout_buffer_destroy; |
| 1146 | |
Kristian Høgsberg | a4b7e20 | 2011-10-24 13:26:32 -0400 | [diff] [blame] | 1147 | output->pending_fs_surf_fb_id = 0; |
Kristian Høgsberg | 68c479a | 2012-01-25 23:32:28 -0500 | [diff] [blame] | 1148 | output->base.repaint = drm_output_repaint; |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 1149 | output->base.destroy = drm_output_destroy; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1150 | output->base.assign_planes = drm_assign_planes; |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1151 | output->base.set_dpms = drm_set_dpms; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 1152 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1153 | return 0; |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 1154 | |
David Herrmann | d7c4551 | 2011-12-08 17:05:46 +0100 | [diff] [blame] | 1155 | err_fb: |
| 1156 | glFramebufferRenderbuffer(GL_FRAMEBUFFER, |
| 1157 | GL_COLOR_ATTACHMENT0, |
| 1158 | GL_RENDERBUFFER, |
| 1159 | 0); |
| 1160 | err_bufs: |
| 1161 | for (i = 0; i < 2; i++) { |
| 1162 | if (output->fb_id[i] != -1) |
| 1163 | drmModeRmFB(ec->drm.fd, output->fb_id[i]); |
| 1164 | if (output->image[i]) |
| 1165 | ec->base.destroy_image(ec->base.display, |
| 1166 | output->image[i]); |
| 1167 | if (output->bo[i]) |
| 1168 | gbm_bo_destroy(output->bo[i]); |
| 1169 | } |
| 1170 | glBindRenderbuffer(GL_RENDERBUFFER, 0); |
| 1171 | glDeleteRenderbuffers(2, output->rbo); |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 1172 | err_free: |
| 1173 | wl_list_for_each_safe(drm_mode, next, &output->base.mode_list, |
| 1174 | base.link) { |
| 1175 | wl_list_remove(&drm_mode->base.link); |
| 1176 | free(drm_mode); |
| 1177 | } |
| 1178 | |
| 1179 | drmModeFreeCrtc(output->original_crtc); |
| 1180 | ec->crtc_allocator &= ~(1 << output->crtc_id); |
| 1181 | ec->connector_allocator &= ~(1 << output->connector_id); |
| 1182 | |
| 1183 | free(output); |
| 1184 | return -1; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1185 | } |
| 1186 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1187 | static void |
| 1188 | create_sprites(struct drm_compositor *ec) |
| 1189 | { |
| 1190 | struct drm_sprite *sprite; |
| 1191 | drmModePlaneRes *plane_res; |
| 1192 | drmModePlane *plane; |
| 1193 | int i; |
| 1194 | |
| 1195 | plane_res = drmModeGetPlaneResources(ec->drm.fd); |
| 1196 | if (!plane_res) { |
| 1197 | fprintf(stderr, "failed to get plane resources: %s\n", |
| 1198 | strerror(errno)); |
| 1199 | return; |
| 1200 | } |
| 1201 | |
| 1202 | for (i = 0; i < plane_res->count_planes; i++) { |
| 1203 | plane = drmModeGetPlane(ec->drm.fd, plane_res->planes[i]); |
| 1204 | if (!plane) |
| 1205 | continue; |
| 1206 | |
| 1207 | sprite = malloc(sizeof(*sprite) + ((sizeof(uint32_t)) * |
| 1208 | plane->count_formats)); |
| 1209 | if (!sprite) { |
| 1210 | fprintf(stderr, "%s: out of memory\n", |
| 1211 | __func__); |
| 1212 | free(plane); |
| 1213 | continue; |
| 1214 | } |
| 1215 | |
| 1216 | memset(sprite, 0, sizeof *sprite); |
| 1217 | |
| 1218 | sprite->possible_crtcs = plane->possible_crtcs; |
| 1219 | sprite->plane_id = plane->plane_id; |
| 1220 | sprite->surface = NULL; |
| 1221 | sprite->pending_surface = NULL; |
| 1222 | sprite->fb_id = 0; |
| 1223 | sprite->pending_fb_id = 0; |
| 1224 | sprite->destroy_listener.func = sprite_handle_buffer_destroy; |
| 1225 | sprite->pending_destroy_listener.func = |
| 1226 | sprite_handle_pending_buffer_destroy; |
| 1227 | sprite->compositor = ec; |
| 1228 | sprite->count_formats = plane->count_formats; |
| 1229 | memcpy(sprite->formats, plane->formats, |
| 1230 | plane->count_formats); |
| 1231 | drmModeFreePlane(plane); |
| 1232 | |
| 1233 | wl_list_insert(&ec->sprite_list, &sprite->link); |
| 1234 | } |
| 1235 | |
| 1236 | free(plane_res->planes); |
| 1237 | free(plane_res); |
| 1238 | } |
| 1239 | |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 1240 | static void |
| 1241 | destroy_sprites(struct drm_compositor *compositor) |
| 1242 | { |
| 1243 | struct drm_sprite *sprite, *next; |
| 1244 | struct drm_output *output; |
| 1245 | |
| 1246 | output = container_of(compositor->base.output_list.next, |
| 1247 | struct drm_output, base.link); |
| 1248 | |
| 1249 | wl_list_for_each_safe(sprite, next, &compositor->sprite_list, link) { |
| 1250 | drmModeSetPlane(compositor->drm.fd, |
| 1251 | sprite->plane_id, |
| 1252 | output->crtc_id, 0, 0, |
| 1253 | 0, 0, 0, 0, 0, 0, 0, 0); |
| 1254 | drmModeRmFB(compositor->drm.fd, sprite->fb_id); |
| 1255 | free(sprite); |
| 1256 | } |
| 1257 | } |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1258 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1259 | static int |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1260 | create_outputs(struct drm_compositor *ec, int option_connector, |
| 1261 | struct udev_device *drm_device) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1262 | { |
| 1263 | drmModeConnector *connector; |
| 1264 | drmModeRes *resources; |
| 1265 | int i; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 1266 | int x = 0, y = 0; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1267 | |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 1268 | resources = drmModeGetResources(ec->drm.fd); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1269 | if (!resources) { |
| 1270 | fprintf(stderr, "drmModeGetResources failed\n"); |
| 1271 | return -1; |
| 1272 | } |
| 1273 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1274 | ec->crtcs = calloc(resources->count_crtcs, sizeof(uint32_t)); |
Christopher Michael | eb866cd | 2012-03-07 14:55:21 -0500 | [diff] [blame] | 1275 | if (!ec->crtcs) { |
| 1276 | drmModeFreeResources(resources); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1277 | return -1; |
Christopher Michael | eb866cd | 2012-03-07 14:55:21 -0500 | [diff] [blame] | 1278 | } |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1279 | |
| 1280 | ec->num_crtcs = resources->count_crtcs; |
| 1281 | memcpy(ec->crtcs, resources->crtcs, sizeof(uint32_t) * ec->num_crtcs); |
| 1282 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1283 | for (i = 0; i < resources->count_connectors; i++) { |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1284 | connector = drmModeGetConnector(ec->drm.fd, |
| 1285 | resources->connectors[i]); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1286 | if (connector == NULL) |
| 1287 | continue; |
| 1288 | |
| 1289 | if (connector->connection == DRM_MODE_CONNECTED && |
| 1290 | (option_connector == 0 || |
Benjamin Franzke | 9eaee35 | 2011-08-02 13:03:54 +0200 | [diff] [blame] | 1291 | connector->connector_id == option_connector)) { |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 1292 | if (create_output_for_connector(ec, resources, |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1293 | connector, x, y, |
| 1294 | drm_device) < 0) { |
Benjamin Franzke | 439d986 | 2011-10-07 08:20:53 +0200 | [diff] [blame] | 1295 | drmModeFreeConnector(connector); |
| 1296 | continue; |
| 1297 | } |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1298 | |
Benjamin Franzke | 9eaee35 | 2011-08-02 13:03:54 +0200 | [diff] [blame] | 1299 | x += container_of(ec->base.output_list.prev, |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1300 | struct weston_output, |
Benjamin Franzke | 9eaee35 | 2011-08-02 13:03:54 +0200 | [diff] [blame] | 1301 | link)->current->width; |
| 1302 | } |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 1303 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1304 | drmModeFreeConnector(connector); |
| 1305 | } |
| 1306 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1307 | if (wl_list_empty(&ec->base.output_list)) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1308 | fprintf(stderr, "No currently active connector found.\n"); |
Christopher Michael | eb866cd | 2012-03-07 14:55:21 -0500 | [diff] [blame] | 1309 | drmModeFreeResources(resources); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1310 | return -1; |
| 1311 | } |
| 1312 | |
| 1313 | drmModeFreeResources(resources); |
| 1314 | |
| 1315 | return 0; |
| 1316 | } |
| 1317 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1318 | static void |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1319 | update_outputs(struct drm_compositor *ec, struct udev_device *drm_device) |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1320 | { |
| 1321 | drmModeConnector *connector; |
| 1322 | drmModeRes *resources; |
| 1323 | struct drm_output *output, *next; |
| 1324 | int x = 0, y = 0; |
| 1325 | int x_offset = 0, y_offset = 0; |
| 1326 | uint32_t connected = 0, disconnects = 0; |
| 1327 | int i; |
| 1328 | |
| 1329 | resources = drmModeGetResources(ec->drm.fd); |
| 1330 | if (!resources) { |
| 1331 | fprintf(stderr, "drmModeGetResources failed\n"); |
| 1332 | return; |
| 1333 | } |
| 1334 | |
| 1335 | /* collect new connects */ |
| 1336 | for (i = 0; i < resources->count_connectors; i++) { |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1337 | int connector_id = resources->connectors[i]; |
| 1338 | |
| 1339 | connector = drmModeGetConnector(ec->drm.fd, connector_id); |
David Herrmann | 7551cff | 2011-12-08 17:05:43 +0100 | [diff] [blame] | 1340 | if (connector == NULL) |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1341 | continue; |
| 1342 | |
David Herrmann | 7551cff | 2011-12-08 17:05:43 +0100 | [diff] [blame] | 1343 | if (connector->connection != DRM_MODE_CONNECTED) { |
| 1344 | drmModeFreeConnector(connector); |
| 1345 | continue; |
| 1346 | } |
| 1347 | |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1348 | connected |= (1 << connector_id); |
| 1349 | |
| 1350 | if (!(ec->connector_allocator & (1 << connector_id))) { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1351 | struct weston_output *last = |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1352 | container_of(ec->base.output_list.prev, |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1353 | struct weston_output, link); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1354 | |
| 1355 | /* XXX: not yet needed, we die with 0 outputs */ |
| 1356 | if (!wl_list_empty(&ec->base.output_list)) |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1357 | x = last->x + last->current->width; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1358 | else |
| 1359 | x = 0; |
| 1360 | y = 0; |
| 1361 | create_output_for_connector(ec, resources, |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1362 | connector, x, y, |
| 1363 | drm_device); |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1364 | printf("connector %d connected\n", connector_id); |
| 1365 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1366 | } |
| 1367 | drmModeFreeConnector(connector); |
| 1368 | } |
| 1369 | drmModeFreeResources(resources); |
| 1370 | |
| 1371 | disconnects = ec->connector_allocator & ~connected; |
| 1372 | if (disconnects) { |
| 1373 | wl_list_for_each_safe(output, next, &ec->base.output_list, |
| 1374 | base.link) { |
| 1375 | if (x_offset != 0 || y_offset != 0) { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1376 | weston_output_move(&output->base, |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1377 | output->base.x - x_offset, |
| 1378 | output->base.y - y_offset); |
| 1379 | } |
| 1380 | |
| 1381 | if (disconnects & (1 << output->connector_id)) { |
| 1382 | disconnects &= ~(1 << output->connector_id); |
| 1383 | printf("connector %d disconnected\n", |
| 1384 | output->connector_id); |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1385 | x_offset += output->base.current->width; |
Benjamin Franzke | 48c4ea2 | 2011-08-30 11:44:56 +0200 | [diff] [blame] | 1386 | drm_output_destroy(&output->base); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1387 | } |
| 1388 | } |
| 1389 | } |
| 1390 | |
| 1391 | /* FIXME: handle zero outputs, without terminating */ |
| 1392 | if (ec->connector_allocator == 0) |
| 1393 | wl_display_terminate(ec->base.wl_display); |
| 1394 | } |
| 1395 | |
| 1396 | static int |
David Herrmann | d7488c2 | 2012-03-11 20:05:21 +0100 | [diff] [blame^] | 1397 | udev_event_is_hotplug(struct drm_compositor *ec, struct udev_device *device) |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1398 | { |
| 1399 | struct udev_list_entry *list, *hotplug_entry; |
David Herrmann | d7488c2 | 2012-03-11 20:05:21 +0100 | [diff] [blame^] | 1400 | const char *sysnum; |
| 1401 | |
| 1402 | sysnum = udev_device_get_sysnum(device); |
| 1403 | if (!sysnum || atoi(sysnum) != ec->drm.id) |
| 1404 | return 0; |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1405 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1406 | list = udev_device_get_properties_list_entry(device); |
| 1407 | |
| 1408 | hotplug_entry = udev_list_entry_get_by_name(list, "HOTPLUG"); |
| 1409 | if (hotplug_entry == NULL) |
| 1410 | return 0; |
| 1411 | |
| 1412 | return strcmp(udev_list_entry_get_value(hotplug_entry), "1") == 0; |
| 1413 | } |
| 1414 | |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 1415 | static int |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1416 | udev_drm_event(int fd, uint32_t mask, void *data) |
| 1417 | { |
| 1418 | struct drm_compositor *ec = data; |
| 1419 | struct udev_device *event; |
| 1420 | |
| 1421 | event = udev_monitor_receive_device(ec->udev_monitor); |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1422 | |
David Herrmann | d7488c2 | 2012-03-11 20:05:21 +0100 | [diff] [blame^] | 1423 | if (udev_event_is_hotplug(ec, event)) |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1424 | update_outputs(ec, event); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1425 | |
| 1426 | udev_device_unref(event); |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 1427 | |
| 1428 | return 1; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1429 | } |
| 1430 | |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 1431 | static void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1432 | drm_destroy(struct weston_compositor *ec) |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 1433 | { |
| 1434 | struct drm_compositor *d = (struct drm_compositor *) ec; |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1435 | struct weston_input_device *input, *next; |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 1436 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1437 | weston_compositor_shutdown(ec); |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 1438 | gbm_device_destroy(d->gbm); |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 1439 | destroy_sprites(d); |
Kristian Høgsberg | 62d2774 | 2012-01-18 12:38:33 -0500 | [diff] [blame] | 1440 | drmDropMaster(d->drm.fd); |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 1441 | tty_destroy(d->tty); |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 1442 | |
Tiago Vignatti | c349e1d | 2011-12-18 23:52:18 +0200 | [diff] [blame] | 1443 | wl_list_for_each_safe(input, next, &ec->input_device_list, link) |
| 1444 | evdev_input_destroy(input); |
| 1445 | |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 1446 | free(d); |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 1447 | } |
| 1448 | |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 1449 | static void |
Kristian Høgsberg | 835cd49 | 2012-01-18 11:48:46 -0500 | [diff] [blame] | 1450 | drm_compositor_set_modes(struct drm_compositor *compositor) |
| 1451 | { |
| 1452 | struct drm_output *output; |
| 1453 | struct drm_mode *drm_mode; |
| 1454 | int ret; |
| 1455 | |
| 1456 | wl_list_for_each(output, &compositor->base.output_list, base.link) { |
| 1457 | drm_mode = (struct drm_mode *) output->base.current; |
| 1458 | ret = drmModeSetCrtc(compositor->drm.fd, output->crtc_id, |
| 1459 | output->fb_id[output->current ^ 1], 0, 0, |
| 1460 | &output->connector_id, 1, |
| 1461 | &drm_mode->mode_info); |
| 1462 | if (ret < 0) { |
| 1463 | fprintf(stderr, |
| 1464 | "failed to set mode %dx%d for output at %d,%d: %m", |
| 1465 | drm_mode->base.width, drm_mode->base.height, |
| 1466 | output->base.x, output->base.y); |
| 1467 | } |
| 1468 | } |
| 1469 | } |
| 1470 | |
| 1471 | static void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1472 | vt_func(struct weston_compositor *compositor, int event) |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 1473 | { |
| 1474 | struct drm_compositor *ec = (struct drm_compositor *) compositor; |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1475 | struct weston_output *output; |
| 1476 | struct weston_input_device *input; |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 1477 | struct drm_sprite *sprite; |
| 1478 | struct drm_output *drm_output; |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 1479 | |
| 1480 | switch (event) { |
| 1481 | case TTY_ENTER_VT: |
| 1482 | compositor->focus = 1; |
Kristian Høgsberg | a018fb0 | 2012-01-16 10:52:52 -0500 | [diff] [blame] | 1483 | if (drmSetMaster(ec->drm.fd)) { |
| 1484 | fprintf(stderr, "failed to set master: %m\n"); |
| 1485 | wl_display_terminate(compositor->wl_display); |
| 1486 | } |
Pekka Paalanen | bce2d3f | 2011-12-02 13:07:27 +0200 | [diff] [blame] | 1487 | compositor->state = ec->prev_state; |
Kristian Høgsberg | 835cd49 | 2012-01-18 11:48:46 -0500 | [diff] [blame] | 1488 | drm_compositor_set_modes(ec); |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1489 | weston_compositor_damage_all(compositor); |
Tiago Vignatti | 6e2d5f1 | 2011-12-19 00:32:48 +0200 | [diff] [blame] | 1490 | wl_list_for_each(input, &compositor->input_device_list, link) |
| 1491 | evdev_add_devices(ec->udev, input); |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 1492 | break; |
| 1493 | case TTY_LEAVE_VT: |
| 1494 | compositor->focus = 0; |
Pekka Paalanen | bce2d3f | 2011-12-02 13:07:27 +0200 | [diff] [blame] | 1495 | ec->prev_state = compositor->state; |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1496 | compositor->state = WESTON_COMPOSITOR_SLEEPING; |
Kristian Høgsberg | d8e181b | 2011-05-06 15:38:28 -0400 | [diff] [blame] | 1497 | |
Kristian Høgsberg | 34f80ff | 2012-01-18 11:50:31 -0500 | [diff] [blame] | 1498 | /* If we have a repaint scheduled (either from a |
| 1499 | * pending pageflip or the idle handler), make sure we |
| 1500 | * cancel that so we don't try to pageflip when we're |
| 1501 | * vt switched away. The SLEEPING state will prevent |
| 1502 | * further attemps at repainting. When we switch |
| 1503 | * back, we schedule a repaint, which will process |
| 1504 | * pending frame callbacks. */ |
| 1505 | |
| 1506 | wl_list_for_each(output, &ec->base.output_list, link) { |
| 1507 | output->repaint_needed = 0; |
| 1508 | drm_output_set_cursor(output, NULL); |
| 1509 | } |
| 1510 | |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 1511 | drm_output = container_of(ec->base.output_list.next, |
| 1512 | struct drm_output, base.link); |
| 1513 | |
| 1514 | wl_list_for_each(sprite, &ec->sprite_list, link) |
| 1515 | drmModeSetPlane(ec->drm.fd, |
| 1516 | sprite->plane_id, |
| 1517 | drm_output->crtc_id, 0, 0, |
| 1518 | 0, 0, 0, 0, 0, 0, 0, 0); |
| 1519 | |
Tiago Vignatti | 6e2d5f1 | 2011-12-19 00:32:48 +0200 | [diff] [blame] | 1520 | wl_list_for_each(input, &compositor->input_device_list, link) |
| 1521 | evdev_remove_devices(input); |
Kristian Høgsberg | d8e181b | 2011-05-06 15:38:28 -0400 | [diff] [blame] | 1522 | |
Kristian Høgsberg | a018fb0 | 2012-01-16 10:52:52 -0500 | [diff] [blame] | 1523 | if (drmDropMaster(ec->drm.fd) < 0) |
| 1524 | fprintf(stderr, "failed to drop master: %m\n"); |
| 1525 | |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 1526 | break; |
| 1527 | }; |
| 1528 | } |
| 1529 | |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 1530 | static const char default_seat[] = "seat0"; |
| 1531 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1532 | static struct weston_compositor * |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 1533 | drm_compositor_create(struct wl_display *display, |
Tiago Vignatti | faee801 | 2011-09-01 15:58:17 -0400 | [diff] [blame] | 1534 | int connector, const char *seat, int tty) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1535 | { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1536 | struct drm_compositor *ec; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1537 | struct udev_enumerate *e; |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1538 | struct udev_list_entry *entry; |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 1539 | struct udev_device *device, *drm_device; |
| 1540 | const char *path, *device_seat; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1541 | struct wl_event_loop *loop; |
| 1542 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1543 | ec = malloc(sizeof *ec); |
| 1544 | if (ec == NULL) |
| 1545 | return NULL; |
| 1546 | |
| 1547 | memset(ec, 0, sizeof *ec); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1548 | ec->udev = udev_new(); |
| 1549 | if (ec->udev == NULL) { |
| 1550 | fprintf(stderr, "failed to initialize udev context\n"); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1551 | return NULL; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1552 | } |
| 1553 | |
Kristian Høgsberg | c5b9ddb | 2012-01-15 14:29:09 -0500 | [diff] [blame] | 1554 | ec->base.wl_display = display; |
| 1555 | ec->tty = tty_create(&ec->base, vt_func, tty); |
| 1556 | if (!ec->tty) { |
| 1557 | fprintf(stderr, "failed to initialize tty\n"); |
| 1558 | free(ec); |
| 1559 | return NULL; |
| 1560 | } |
| 1561 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1562 | e = udev_enumerate_new(ec->udev); |
| 1563 | udev_enumerate_add_match_subsystem(e, "drm"); |
Benjamin Franzke | a764ee5 | 2011-10-07 08:23:22 +0200 | [diff] [blame] | 1564 | udev_enumerate_add_match_sysname(e, "card[0-9]*"); |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 1565 | |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1566 | udev_enumerate_scan_devices(e); |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 1567 | drm_device = NULL; |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1568 | udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1569 | path = udev_list_entry_get_name(entry); |
| 1570 | device = udev_device_new_from_syspath(ec->udev, path); |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1571 | device_seat = |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 1572 | udev_device_get_property_value(device, "ID_SEAT"); |
| 1573 | if (!device_seat) |
| 1574 | device_seat = default_seat; |
| 1575 | if (strcmp(device_seat, seat) == 0) { |
| 1576 | drm_device = device; |
| 1577 | break; |
| 1578 | } |
| 1579 | udev_device_unref(device); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1580 | } |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 1581 | |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 1582 | if (drm_device == NULL) { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1583 | fprintf(stderr, "no drm device found\n"); |
| 1584 | return NULL; |
| 1585 | } |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1586 | |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 1587 | if (init_egl(ec, drm_device) < 0) { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1588 | fprintf(stderr, "failed to initialize egl\n"); |
| 1589 | return NULL; |
| 1590 | } |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 1591 | |
| 1592 | ec->base.destroy = drm_destroy; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 1593 | |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 1594 | ec->base.focus = 1; |
| 1595 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1596 | ec->prev_state = WESTON_COMPOSITOR_ACTIVE; |
Pekka Paalanen | bce2d3f | 2011-12-02 13:07:27 +0200 | [diff] [blame] | 1597 | |
Benjamin Franzke | 5b2cb6f | 2011-02-18 16:54:55 +0100 | [diff] [blame] | 1598 | glGenFramebuffers(1, &ec->base.fbo); |
| 1599 | glBindFramebuffer(GL_FRAMEBUFFER, ec->base.fbo); |
| 1600 | |
Benjamin Franzke | d59eb1c | 2011-04-29 22:14:54 +0200 | [diff] [blame] | 1601 | /* 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] | 1602 | if (weston_compositor_init(&ec->base, display) < 0) |
Benjamin Franzke | d59eb1c | 2011-04-29 22:14:54 +0200 | [diff] [blame] | 1603 | return NULL; |
| 1604 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1605 | wl_list_init(&ec->sprite_list); |
| 1606 | create_sprites(ec); |
| 1607 | |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1608 | if (create_outputs(ec, connector, drm_device) < 0) { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1609 | fprintf(stderr, "failed to create output for %s\n", path); |
| 1610 | return NULL; |
| 1611 | } |
| 1612 | |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1613 | udev_device_unref(drm_device); |
Benjamin Franzke | 02dee2c | 2011-10-07 08:27:26 +0200 | [diff] [blame] | 1614 | udev_enumerate_unref(e); |
| 1615 | path = NULL; |
| 1616 | |
Tiago Vignatti | ce03ec3 | 2011-12-19 01:14:03 +0200 | [diff] [blame] | 1617 | evdev_input_create(&ec->base, ec->udev, seat); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1618 | |
| 1619 | loop = wl_display_get_event_loop(ec->base.wl_display); |
| 1620 | ec->drm_source = |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 1621 | wl_event_loop_add_fd(loop, ec->drm.fd, |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1622 | WL_EVENT_READABLE, on_drm_input, ec); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1623 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1624 | ec->udev_monitor = udev_monitor_new_from_netlink(ec->udev, "udev"); |
| 1625 | if (ec->udev_monitor == NULL) { |
| 1626 | fprintf(stderr, "failed to intialize udev monitor\n"); |
| 1627 | return NULL; |
| 1628 | } |
| 1629 | udev_monitor_filter_add_match_subsystem_devtype(ec->udev_monitor, |
| 1630 | "drm", NULL); |
| 1631 | ec->udev_drm_source = |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1632 | wl_event_loop_add_fd(loop, |
| 1633 | udev_monitor_get_fd(ec->udev_monitor), |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1634 | WL_EVENT_READABLE, udev_drm_event, ec); |
| 1635 | |
| 1636 | if (udev_monitor_enable_receiving(ec->udev_monitor) < 0) { |
| 1637 | fprintf(stderr, "failed to enable udev-monitor receiving\n"); |
| 1638 | return NULL; |
| 1639 | } |
| 1640 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1641 | return &ec->base; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1642 | } |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 1643 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1644 | struct weston_compositor * |
Kristian Høgsberg | 6c709a3 | 2011-05-06 14:52:41 -0400 | [diff] [blame] | 1645 | backend_init(struct wl_display *display, char *options); |
| 1646 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1647 | WL_EXPORT struct weston_compositor * |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 1648 | backend_init(struct wl_display *display, char *options) |
| 1649 | { |
Kristian Høgsberg | 8050bd2 | 2011-05-04 15:08:04 -0400 | [diff] [blame] | 1650 | int connector = 0, i; |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 1651 | const char *seat; |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 1652 | char *p, *value; |
Kristian Høgsberg | 1201b75 | 2012-01-15 14:27:10 -0500 | [diff] [blame] | 1653 | int tty = 0; |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 1654 | |
Tiago Vignatti | faee801 | 2011-09-01 15:58:17 -0400 | [diff] [blame] | 1655 | static char * const tokens[] = { "connector", "seat", "tty", NULL }; |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1656 | |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 1657 | p = options; |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 1658 | seat = default_seat; |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 1659 | while (i = getsubopt(&p, tokens, &value), i != -1) { |
| 1660 | switch (i) { |
| 1661 | case 0: |
Kristian Høgsberg | 8050bd2 | 2011-05-04 15:08:04 -0400 | [diff] [blame] | 1662 | connector = strtol(value, NULL, 0); |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 1663 | break; |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 1664 | case 1: |
| 1665 | seat = value; |
| 1666 | break; |
Tiago Vignatti | faee801 | 2011-09-01 15:58:17 -0400 | [diff] [blame] | 1667 | case 2: |
| 1668 | tty = strtol(value, NULL, 0); |
| 1669 | break; |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 1670 | } |
| 1671 | } |
| 1672 | |
Tiago Vignatti | faee801 | 2011-09-01 15:58:17 -0400 | [diff] [blame] | 1673 | return drm_compositor_create(display, connector, seat, tty); |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 1674 | } |