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 | |
Kristian Høgsberg | 7a011ea | 2012-03-26 11:48:55 -0400 | [diff] [blame^] | 445 | if (es->image == EGL_NO_IMAGE_KHR) |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 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); |
Rob Clark | 3c97b90 | 2012-03-11 19:48:43 -0500 | [diff] [blame] | 529 | s->src_x = box->x1 << 16; |
| 530 | s->src_y = box->y1 << 16; |
| 531 | s->src_w = (box->x2 - box->x1) << 16; |
| 532 | s->src_h = (box->y2 - box->y1) << 16; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 533 | pixman_region32_fini(&src_rect); |
| 534 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 535 | wl_list_insert(es->buffer->resource.destroy_listener_list.prev, |
| 536 | &s->pending_destroy_listener.link); |
| 537 | return 0; |
| 538 | } |
| 539 | |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 540 | static int |
| 541 | drm_output_set_cursor(struct weston_output *output_base, |
| 542 | struct weston_input_device *eid); |
| 543 | |
| 544 | static void |
| 545 | weston_output_set_cursor(struct weston_output *output, |
| 546 | struct weston_input_device *device, |
| 547 | pixman_region32_t *overlap) |
| 548 | { |
| 549 | pixman_region32_t cursor_region; |
| 550 | int prior_was_hardware; |
| 551 | |
| 552 | if (device->sprite == NULL) |
| 553 | return; |
| 554 | |
| 555 | pixman_region32_init(&cursor_region); |
| 556 | pixman_region32_intersect(&cursor_region, |
| 557 | &device->sprite->transform.boundingbox, |
| 558 | &output->region); |
| 559 | |
| 560 | if (!pixman_region32_not_empty(&cursor_region)) { |
| 561 | drm_output_set_cursor(output, NULL); |
| 562 | goto out; |
| 563 | } |
| 564 | |
| 565 | prior_was_hardware = device->hw_cursor; |
| 566 | if (pixman_region32_not_empty(overlap) || |
| 567 | drm_output_set_cursor(output, device) < 0) { |
| 568 | if (prior_was_hardware) { |
| 569 | weston_surface_damage(device->sprite); |
| 570 | drm_output_set_cursor(output, NULL); |
| 571 | } |
| 572 | device->hw_cursor = 0; |
| 573 | } else { |
| 574 | if (!prior_was_hardware) |
| 575 | weston_surface_damage_below(device->sprite); |
| 576 | pixman_region32_fini(&device->sprite->damage); |
| 577 | pixman_region32_init(&device->sprite->damage); |
| 578 | device->hw_cursor = 1; |
| 579 | } |
| 580 | |
| 581 | out: |
| 582 | pixman_region32_fini(&cursor_region); |
| 583 | } |
| 584 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 585 | static void |
| 586 | drm_assign_planes(struct weston_output *output) |
| 587 | { |
| 588 | struct weston_compositor *ec = output->compositor; |
| 589 | struct weston_surface *es; |
| 590 | pixman_region32_t overlap, surface_overlap; |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 591 | struct weston_input_device *device; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 592 | |
| 593 | /* |
| 594 | * Find a surface for each sprite in the output using some heuristics: |
| 595 | * 1) size |
| 596 | * 2) frequency of update |
| 597 | * 3) opacity (though some hw might support alpha blending) |
| 598 | * 4) clipping (this can be fixed with color keys) |
| 599 | * |
| 600 | * The idea is to save on blitting since this should save power. |
| 601 | * If we can get a large video surface on the sprite for example, |
| 602 | * the main display surface may not need to update at all, and |
| 603 | * the client buffer can be used directly for the sprite surface |
| 604 | * as we do for flipping full screen surfaces. |
| 605 | */ |
| 606 | pixman_region32_init(&overlap); |
| 607 | wl_list_for_each(es, &ec->surface_list, link) { |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 608 | /* |
| 609 | * FIXME: try to assign hw cursors here too, they're just |
| 610 | * special overlays |
| 611 | */ |
| 612 | pixman_region32_init(&surface_overlap); |
| 613 | pixman_region32_intersect(&surface_overlap, &overlap, |
| 614 | &es->transform.boundingbox); |
| 615 | |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 616 | device = (struct weston_input_device *) ec->input_device; |
| 617 | if (es == device->sprite) { |
| 618 | weston_output_set_cursor(output, device, |
| 619 | &surface_overlap); |
| 620 | |
| 621 | if (!device->hw_cursor) |
| 622 | pixman_region32_union(&overlap, &overlap, |
| 623 | &es->transform.boundingbox); |
| 624 | } else if (!drm_output_prepare_overlay_surface(output, es, |
| 625 | &surface_overlap)) { |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 626 | pixman_region32_fini(&es->damage); |
| 627 | pixman_region32_init(&es->damage); |
| 628 | } else { |
| 629 | pixman_region32_union(&overlap, &overlap, |
| 630 | &es->transform.boundingbox); |
| 631 | } |
| 632 | pixman_region32_fini(&surface_overlap); |
| 633 | } |
| 634 | pixman_region32_fini(&overlap); |
| 635 | |
| 636 | drm_disable_unused_sprites(output); |
| 637 | } |
| 638 | |
| 639 | static int |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 640 | drm_output_set_cursor(struct weston_output *output_base, |
| 641 | struct weston_input_device *eid) |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 642 | { |
| 643 | struct drm_output *output = (struct drm_output *) output_base; |
| 644 | struct drm_compositor *c = |
| 645 | (struct drm_compositor *) output->base.compositor; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 646 | EGLint handle, stride; |
| 647 | int ret = -1; |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 648 | struct gbm_bo *bo; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 649 | |
Kristian Høgsberg | e4c40a4 | 2011-05-06 14:04:21 -0400 | [diff] [blame] | 650 | if (eid == NULL) { |
| 651 | drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0); |
| 652 | return 0; |
| 653 | } |
| 654 | |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 655 | if (eid->sprite->image == EGL_NO_IMAGE_KHR) |
| 656 | goto out; |
| 657 | |
Pekka Paalanen | 60921e5 | 2012-01-25 15:55:43 +0200 | [diff] [blame] | 658 | if (eid->sprite->geometry.width > 64 || |
| 659 | eid->sprite->geometry.height > 64) |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 660 | goto out; |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 661 | |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 662 | bo = gbm_bo_create_from_egl_image(c->gbm, |
| 663 | c->base.display, |
| 664 | eid->sprite->image, 64, 64, |
| 665 | GBM_BO_USE_CURSOR_64X64); |
Kristian Høgsberg | 6916d9c | 2012-01-18 15:11:07 -0500 | [diff] [blame] | 666 | /* Not suitable for hw cursor, fall back */ |
| 667 | if (bo == NULL) |
| 668 | goto out; |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 669 | |
| 670 | handle = gbm_bo_get_handle(bo).s32; |
| 671 | stride = gbm_bo_get_pitch(bo); |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 672 | gbm_bo_destroy(bo); |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 673 | |
Kristian Høgsberg | 6916d9c | 2012-01-18 15:11:07 -0500 | [diff] [blame] | 674 | /* gbm_bo_create_from_egl_image() didn't always validate the usage |
| 675 | * flags, and in that case we might end up with a bad stride. */ |
| 676 | if (stride != 64 * 4) |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 677 | goto out; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 678 | |
| 679 | ret = drmModeSetCursor(c->drm.fd, output->crtc_id, handle, 64, 64); |
| 680 | if (ret) { |
| 681 | fprintf(stderr, "failed to set cursor: %s\n", strerror(-ret)); |
| 682 | goto out; |
| 683 | } |
| 684 | |
| 685 | ret = drmModeMoveCursor(c->drm.fd, output->crtc_id, |
Pekka Paalanen | ba3cf95 | 2012-01-25 16:22:05 +0200 | [diff] [blame] | 686 | eid->sprite->geometry.x - output->base.x, |
| 687 | eid->sprite->geometry.y - output->base.y); |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 688 | if (ret) { |
| 689 | fprintf(stderr, "failed to move cursor: %s\n", strerror(-ret)); |
| 690 | goto out; |
| 691 | } |
| 692 | |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 693 | out: |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 694 | if (ret) |
| 695 | drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0); |
| 696 | return ret; |
| 697 | } |
| 698 | |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 699 | static void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 700 | drm_output_destroy(struct weston_output *output_base) |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 701 | { |
| 702 | struct drm_output *output = (struct drm_output *) output_base; |
| 703 | struct drm_compositor *c = |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 704 | (struct drm_compositor *) output->base.compositor; |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 705 | drmModeCrtcPtr origcrtc = output->original_crtc; |
| 706 | int i; |
| 707 | |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 708 | if (output->backlight) |
| 709 | backlight_destroy(output->backlight); |
| 710 | |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 711 | /* Turn off hardware cursor */ |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 712 | drm_output_set_cursor(&output->base, NULL); |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 713 | |
| 714 | /* Restore original CRTC state */ |
| 715 | drmModeSetCrtc(c->drm.fd, origcrtc->crtc_id, origcrtc->buffer_id, |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 716 | origcrtc->x, origcrtc->y, |
| 717 | &output->connector_id, 1, &origcrtc->mode); |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 718 | drmModeFreeCrtc(origcrtc); |
| 719 | |
Benjamin Franzke | 48c4ea2 | 2011-08-30 11:44:56 +0200 | [diff] [blame] | 720 | glFramebufferRenderbuffer(GL_FRAMEBUFFER, |
| 721 | GL_COLOR_ATTACHMENT0, |
| 722 | GL_RENDERBUFFER, |
| 723 | 0); |
| 724 | |
| 725 | glBindRenderbuffer(GL_RENDERBUFFER, 0); |
| 726 | glDeleteRenderbuffers(2, output->rbo); |
| 727 | |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 728 | /* Destroy output buffers */ |
| 729 | for (i = 0; i < 2; i++) { |
| 730 | drmModeRmFB(c->drm.fd, output->fb_id[i]); |
| 731 | c->base.destroy_image(c->base.display, output->image[i]); |
| 732 | gbm_bo_destroy(output->bo[i]); |
| 733 | } |
| 734 | |
Benjamin Franzke | 48c4ea2 | 2011-08-30 11:44:56 +0200 | [diff] [blame] | 735 | c->crtc_allocator &= ~(1 << output->crtc_id); |
| 736 | c->connector_allocator &= ~(1 << output->connector_id); |
| 737 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 738 | weston_output_destroy(&output->base); |
Benjamin Franzke | 48c4ea2 | 2011-08-30 11:44:56 +0200 | [diff] [blame] | 739 | wl_list_remove(&output->base.link); |
| 740 | |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 741 | free(output); |
| 742 | } |
| 743 | |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 744 | static int |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 745 | on_drm_input(int fd, uint32_t mask, void *data) |
| 746 | { |
| 747 | drmEventContext evctx; |
| 748 | |
| 749 | memset(&evctx, 0, sizeof evctx); |
| 750 | evctx.version = DRM_EVENT_CONTEXT_VERSION; |
| 751 | evctx.page_flip_handler = page_flip_handler; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 752 | evctx.vblank_handler = vblank_handler; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 753 | drmHandleEvent(fd, &evctx); |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 754 | |
| 755 | return 1; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | static int |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 759 | init_egl(struct drm_compositor *ec, struct udev_device *device) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 760 | { |
Kristian Høgsberg | 379b678 | 2010-07-28 22:52:28 -0400 | [diff] [blame] | 761 | EGLint major, minor; |
David Herrmann | d7488c2 | 2012-03-11 20:05:21 +0100 | [diff] [blame] | 762 | const char *extensions, *filename, *sysnum; |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 763 | int fd; |
Kristian Høgsberg | 2c28aa5 | 2010-07-28 23:47:54 -0400 | [diff] [blame] | 764 | static const EGLint context_attribs[] = { |
| 765 | EGL_CONTEXT_CLIENT_VERSION, 2, |
| 766 | EGL_NONE |
| 767 | }; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 768 | |
David Herrmann | d7488c2 | 2012-03-11 20:05:21 +0100 | [diff] [blame] | 769 | sysnum = udev_device_get_sysnum(device); |
| 770 | if (sysnum) |
| 771 | ec->drm.id = atoi(sysnum); |
| 772 | if (!sysnum || ec->drm.id < 0) { |
| 773 | fprintf(stderr, "cannot get device sysnum\n"); |
| 774 | return -1; |
| 775 | } |
| 776 | |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 777 | filename = udev_device_get_devnode(device); |
David Herrmann | 63ff706 | 2011-11-05 18:46:01 +0100 | [diff] [blame] | 778 | fd = open(filename, O_RDWR | O_CLOEXEC); |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 779 | if (fd < 0) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 780 | /* Probably permissions error */ |
| 781 | fprintf(stderr, "couldn't open %s, skipping\n", |
| 782 | udev_device_get_devnode(device)); |
| 783 | return -1; |
| 784 | } |
| 785 | |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 786 | ec->drm.fd = fd; |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 787 | ec->gbm = gbm_create_device(ec->drm.fd); |
| 788 | ec->base.display = eglGetDisplay(ec->gbm); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 789 | if (ec->base.display == NULL) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 790 | fprintf(stderr, "failed to create display\n"); |
| 791 | return -1; |
| 792 | } |
| 793 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 794 | if (!eglInitialize(ec->base.display, &major, &minor)) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 795 | fprintf(stderr, "failed to initialize display\n"); |
| 796 | return -1; |
| 797 | } |
| 798 | |
Kristian Høgsberg | 379b678 | 2010-07-28 22:52:28 -0400 | [diff] [blame] | 799 | extensions = eglQueryString(ec->base.display, EGL_EXTENSIONS); |
Ander Conselvan de Oliveira | ef7c8d9 | 2011-11-01 16:37:41 +0200 | [diff] [blame] | 800 | if (!strstr(extensions, "EGL_KHR_surfaceless_gles2")) { |
| 801 | fprintf(stderr, "EGL_KHR_surfaceless_gles2 not available\n"); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 802 | return -1; |
| 803 | } |
| 804 | |
Darxus | 55973f2 | 2010-11-22 21:24:39 -0500 | [diff] [blame] | 805 | if (!eglBindAPI(EGL_OPENGL_ES_API)) { |
| 806 | fprintf(stderr, "failed to bind api EGL_OPENGL_ES_API\n"); |
| 807 | return -1; |
| 808 | } |
| 809 | |
Kristian Høgsberg | 2c28aa5 | 2010-07-28 23:47:54 -0400 | [diff] [blame] | 810 | ec->base.context = eglCreateContext(ec->base.display, NULL, |
| 811 | EGL_NO_CONTEXT, context_attribs); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 812 | if (ec->base.context == NULL) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 813 | fprintf(stderr, "failed to create context\n"); |
| 814 | return -1; |
| 815 | } |
| 816 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 817 | if (!eglMakeCurrent(ec->base.display, EGL_NO_SURFACE, |
| 818 | EGL_NO_SURFACE, ec->base.context)) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 819 | fprintf(stderr, "failed to make context current\n"); |
| 820 | return -1; |
| 821 | } |
| 822 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 823 | return 0; |
| 824 | } |
| 825 | |
| 826 | static drmModeModeInfo builtin_1024x768 = { |
| 827 | 63500, /* clock */ |
| 828 | 1024, 1072, 1176, 1328, 0, |
| 829 | 768, 771, 775, 798, 0, |
| 830 | 59920, |
| 831 | DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC, |
| 832 | 0, |
| 833 | "1024x768" |
| 834 | }; |
| 835 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 836 | |
| 837 | static int |
| 838 | drm_output_add_mode(struct drm_output *output, drmModeModeInfo *info) |
| 839 | { |
| 840 | struct drm_mode *mode; |
| 841 | |
| 842 | mode = malloc(sizeof *mode); |
| 843 | if (mode == NULL) |
| 844 | return -1; |
| 845 | |
| 846 | mode->base.flags = 0; |
| 847 | mode->base.width = info->hdisplay; |
| 848 | mode->base.height = info->vdisplay; |
| 849 | mode->base.refresh = info->vrefresh; |
| 850 | mode->mode_info = *info; |
| 851 | wl_list_insert(output->base.mode_list.prev, &mode->base.link); |
| 852 | |
| 853 | return 0; |
| 854 | } |
| 855 | |
| 856 | static int |
| 857 | drm_subpixel_to_wayland(int drm_value) |
| 858 | { |
| 859 | switch (drm_value) { |
| 860 | default: |
| 861 | case DRM_MODE_SUBPIXEL_UNKNOWN: |
| 862 | return WL_OUTPUT_SUBPIXEL_UNKNOWN; |
| 863 | case DRM_MODE_SUBPIXEL_NONE: |
| 864 | return WL_OUTPUT_SUBPIXEL_NONE; |
| 865 | case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB: |
| 866 | return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB; |
| 867 | case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR: |
| 868 | return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR; |
| 869 | case DRM_MODE_SUBPIXEL_VERTICAL_RGB: |
| 870 | return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB; |
| 871 | case DRM_MODE_SUBPIXEL_VERTICAL_BGR: |
| 872 | return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR; |
| 873 | } |
| 874 | } |
| 875 | |
Kristian Høgsberg | 9f404b7 | 2012-01-26 00:11:01 -0500 | [diff] [blame] | 876 | static void |
| 877 | output_handle_scanout_buffer_destroy(struct wl_listener *listener, |
| 878 | struct wl_resource *resource, |
| 879 | uint32_t time) |
| 880 | { |
| 881 | struct drm_output *output = |
| 882 | container_of(listener, struct drm_output, |
| 883 | scanout_buffer_destroy_listener); |
| 884 | |
| 885 | output->scanout_buffer = NULL; |
| 886 | |
| 887 | if (!output->pending_scanout_buffer) |
| 888 | weston_compositor_schedule_repaint(output->base.compositor); |
| 889 | } |
| 890 | |
| 891 | static void |
| 892 | output_handle_pending_scanout_buffer_destroy(struct wl_listener *listener, |
| 893 | struct wl_resource *resource, |
| 894 | uint32_t time) |
| 895 | { |
| 896 | struct drm_output *output = |
| 897 | container_of(listener, struct drm_output, |
| 898 | pending_scanout_buffer_destroy_listener); |
| 899 | |
| 900 | output->pending_scanout_buffer = NULL; |
| 901 | |
| 902 | weston_compositor_schedule_repaint(output->base.compositor); |
| 903 | } |
| 904 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 905 | static void |
| 906 | sprite_handle_buffer_destroy(struct wl_listener *listener, |
| 907 | struct wl_resource *resource, |
| 908 | uint32_t time) |
| 909 | { |
| 910 | struct drm_sprite *sprite = |
| 911 | container_of(listener, struct drm_sprite, |
| 912 | destroy_listener); |
| 913 | |
| 914 | sprite->surface = NULL; |
| 915 | } |
| 916 | |
| 917 | static void |
| 918 | sprite_handle_pending_buffer_destroy(struct wl_listener *listener, |
| 919 | struct wl_resource *resource, |
| 920 | uint32_t time) |
| 921 | { |
| 922 | struct drm_sprite *sprite = |
| 923 | container_of(listener, struct drm_sprite, |
| 924 | pending_destroy_listener); |
| 925 | |
| 926 | sprite->pending_surface = NULL; |
| 927 | } |
| 928 | |
Tiago Vignatti | 5ab91ad | 2012-03-12 19:40:09 -0300 | [diff] [blame] | 929 | /* returns a value between 0-255 range, where higher is brighter */ |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 930 | static uint32_t |
| 931 | drm_get_backlight(struct drm_output *output) |
| 932 | { |
| 933 | long brightness, max_brightness, norm; |
| 934 | |
| 935 | brightness = backlight_get_brightness(output->backlight); |
| 936 | max_brightness = backlight_get_max_brightness(output->backlight); |
| 937 | |
Tiago Vignatti | 5ab91ad | 2012-03-12 19:40:09 -0300 | [diff] [blame] | 938 | /* convert it on a scale of 0 to 255 */ |
| 939 | norm = (brightness * 255)/(max_brightness); |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 940 | |
| 941 | return (uint32_t) norm; |
| 942 | } |
| 943 | |
Tiago Vignatti | 5ab91ad | 2012-03-12 19:40:09 -0300 | [diff] [blame] | 944 | /* values accepted are between 0-255 range */ |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 945 | static void |
| 946 | drm_set_backlight(struct weston_output *output_base, uint32_t value) |
| 947 | { |
| 948 | struct drm_output *output = (struct drm_output *) output_base; |
| 949 | long max_brightness, new_brightness; |
| 950 | |
| 951 | if (!output->backlight) |
| 952 | return; |
| 953 | |
Tiago Vignatti | 5ab91ad | 2012-03-12 19:40:09 -0300 | [diff] [blame] | 954 | if (value < 0 || value > 255) |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 955 | return; |
| 956 | |
| 957 | max_brightness = backlight_get_max_brightness(output->backlight); |
| 958 | |
| 959 | /* get denormalized value */ |
Tiago Vignatti | 5ab91ad | 2012-03-12 19:40:09 -0300 | [diff] [blame] | 960 | new_brightness = (value * max_brightness) / 255; |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 961 | |
| 962 | backlight_set_brightness(output->backlight, new_brightness); |
| 963 | } |
| 964 | |
| 965 | static drmModePropertyPtr |
| 966 | drm_get_prop(int fd, drmModeConnectorPtr connector, const char *name) |
| 967 | { |
| 968 | drmModePropertyPtr props; |
| 969 | int i; |
| 970 | |
| 971 | for (i = 0; i < connector->count_props; i++) { |
| 972 | props = drmModeGetProperty(fd, connector->props[i]); |
| 973 | if (!props) |
| 974 | continue; |
| 975 | |
| 976 | if (!strcmp(props->name, name)) |
| 977 | return props; |
| 978 | |
| 979 | drmModeFreeProperty(props); |
| 980 | } |
| 981 | |
| 982 | return NULL; |
| 983 | } |
| 984 | |
| 985 | static void |
| 986 | drm_set_dpms(struct weston_output *output_base, enum dpms_enum level) |
| 987 | { |
| 988 | struct drm_output *output = (struct drm_output *) output_base; |
| 989 | struct weston_compositor *ec = output_base->compositor; |
| 990 | struct drm_compositor *c = (struct drm_compositor *) ec; |
| 991 | drmModeConnectorPtr connector; |
| 992 | drmModePropertyPtr prop; |
| 993 | |
| 994 | connector = drmModeGetConnector(c->drm.fd, output->connector_id); |
| 995 | if (!connector) |
| 996 | return; |
| 997 | |
| 998 | prop = drm_get_prop(c->drm.fd, connector, "DPMS"); |
| 999 | if (!prop) { |
| 1000 | drmModeFreeConnector(connector); |
| 1001 | return; |
| 1002 | } |
| 1003 | |
| 1004 | drmModeConnectorSetProperty(c->drm.fd, connector->connector_id, |
| 1005 | prop->prop_id, level); |
| 1006 | drmModeFreeProperty(prop); |
| 1007 | drmModeFreeConnector(connector); |
| 1008 | } |
| 1009 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1010 | static int |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1011 | create_output_for_connector(struct drm_compositor *ec, |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1012 | drmModeRes *resources, |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 1013 | drmModeConnector *connector, |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1014 | int x, int y, struct udev_device *drm_device) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1015 | { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1016 | struct drm_output *output; |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 1017 | struct drm_mode *drm_mode, *next; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1018 | drmModeEncoder *encoder; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1019 | int i, ret; |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 1020 | unsigned handle, stride; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1021 | |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 1022 | encoder = drmModeGetEncoder(ec->drm.fd, connector->encoders[0]); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1023 | if (encoder == NULL) { |
| 1024 | fprintf(stderr, "No encoder for connector.\n"); |
| 1025 | return -1; |
| 1026 | } |
| 1027 | |
| 1028 | for (i = 0; i < resources->count_crtcs; i++) { |
Marty Jack | 13d9db2 | 2011-02-09 19:01:42 -0500 | [diff] [blame] | 1029 | if (encoder->possible_crtcs & (1 << i) && |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1030 | !(ec->crtc_allocator & (1 << resources->crtcs[i]))) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1031 | break; |
| 1032 | } |
| 1033 | if (i == resources->count_crtcs) { |
| 1034 | fprintf(stderr, "No usable crtc for encoder.\n"); |
David Herrmann | eb8bed5 | 2011-12-08 17:05:44 +0100 | [diff] [blame] | 1035 | drmModeFreeEncoder(encoder); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1036 | return -1; |
| 1037 | } |
| 1038 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1039 | output = malloc(sizeof *output); |
David Herrmann | eb8bed5 | 2011-12-08 17:05:44 +0100 | [diff] [blame] | 1040 | if (output == NULL) { |
| 1041 | drmModeFreeEncoder(encoder); |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1042 | return -1; |
David Herrmann | eb8bed5 | 2011-12-08 17:05:44 +0100 | [diff] [blame] | 1043 | } |
David Herrmann | d7c4551 | 2011-12-08 17:05:46 +0100 | [diff] [blame] | 1044 | output->fb_id[0] = -1; |
| 1045 | output->fb_id[1] = -1; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1046 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1047 | memset(output, 0, sizeof *output); |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1048 | output->base.subpixel = drm_subpixel_to_wayland(connector->subpixel); |
| 1049 | output->base.make = "unknown"; |
| 1050 | output->base.model = "unknown"; |
| 1051 | wl_list_init(&output->base.mode_list); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1052 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1053 | output->crtc_id = resources->crtcs[i]; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1054 | ec->crtc_allocator |= (1 << output->crtc_id); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1055 | output->connector_id = connector->connector_id; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1056 | ec->connector_allocator |= (1 << output->connector_id); |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1057 | |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 1058 | output->original_crtc = drmModeGetCrtc(ec->drm.fd, output->crtc_id); |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 1059 | drmModeFreeEncoder(encoder); |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 1060 | |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 1061 | for (i = 0; i < connector->count_modes; i++) { |
| 1062 | ret = drm_output_add_mode(output, &connector->modes[i]); |
| 1063 | if (ret) |
| 1064 | goto err_free; |
| 1065 | } |
| 1066 | |
| 1067 | if (connector->count_modes == 0) { |
| 1068 | ret = drm_output_add_mode(output, &builtin_1024x768); |
| 1069 | if (ret) |
| 1070 | goto err_free; |
| 1071 | } |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1072 | |
| 1073 | drm_mode = container_of(output->base.mode_list.next, |
| 1074 | struct drm_mode, base.link); |
| 1075 | output->base.current = &drm_mode->base; |
| 1076 | drm_mode->base.flags = |
| 1077 | WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED; |
| 1078 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1079 | glGenRenderbuffers(2, output->rbo); |
| 1080 | for (i = 0; i < 2; i++) { |
| 1081 | glBindRenderbuffer(GL_RENDERBUFFER, output->rbo[i]); |
| 1082 | |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 1083 | output->bo[i] = |
| 1084 | gbm_bo_create(ec->gbm, |
| 1085 | output->base.current->width, |
| 1086 | output->base.current->height, |
| 1087 | GBM_BO_FORMAT_XRGB8888, |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1088 | GBM_BO_USE_SCANOUT | |
| 1089 | GBM_BO_USE_RENDERING); |
David Herrmann | d7c4551 | 2011-12-08 17:05:46 +0100 | [diff] [blame] | 1090 | if (!output->bo[i]) |
| 1091 | goto err_bufs; |
| 1092 | |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 1093 | output->image[i] = ec->base.create_image(ec->base.display, |
| 1094 | NULL, |
| 1095 | EGL_NATIVE_PIXMAP_KHR, |
| 1096 | output->bo[i], NULL); |
David Herrmann | d7c4551 | 2011-12-08 17:05:46 +0100 | [diff] [blame] | 1097 | if (!output->image[i]) |
| 1098 | goto err_bufs; |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 1099 | |
Kristian Høgsberg | b5819dc | 2011-04-25 15:08:20 -0400 | [diff] [blame] | 1100 | ec->base.image_target_renderbuffer_storage(GL_RENDERBUFFER, |
| 1101 | output->image[i]); |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 1102 | stride = gbm_bo_get_pitch(output->bo[i]); |
| 1103 | handle = gbm_bo_get_handle(output->bo[i]).u32; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1104 | |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 1105 | ret = drmModeAddFB(ec->drm.fd, |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1106 | output->base.current->width, |
| 1107 | output->base.current->height, |
Kristian Høgsberg | 9c46ff1 | 2011-10-24 13:13:22 -0400 | [diff] [blame] | 1108 | 24, 32, stride, handle, &output->fb_id[i]); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1109 | if (ret) { |
| 1110 | fprintf(stderr, "failed to add fb %d: %m\n", i); |
David Herrmann | d7c4551 | 2011-12-08 17:05:46 +0100 | [diff] [blame] | 1111 | goto err_bufs; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1112 | } |
| 1113 | } |
| 1114 | |
| 1115 | output->current = 0; |
| 1116 | glFramebufferRenderbuffer(GL_FRAMEBUFFER, |
| 1117 | GL_COLOR_ATTACHMENT0, |
| 1118 | GL_RENDERBUFFER, |
| 1119 | output->rbo[output->current]); |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 1120 | ret = drmModeSetCrtc(ec->drm.fd, output->crtc_id, |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1121 | output->fb_id[output->current ^ 1], 0, 0, |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1122 | &output->connector_id, 1, |
| 1123 | &drm_mode->mode_info); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1124 | if (ret) { |
| 1125 | fprintf(stderr, "failed to set mode: %m\n"); |
David Herrmann | d7c4551 | 2011-12-08 17:05:46 +0100 | [diff] [blame] | 1126 | goto err_fb; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1127 | } |
| 1128 | |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1129 | output->backlight = backlight_init(drm_device, |
| 1130 | connector->connector_type); |
| 1131 | if (output->backlight) { |
| 1132 | output->base.set_backlight = drm_set_backlight; |
| 1133 | output->base.backlight_current = drm_get_backlight(output); |
| 1134 | } |
| 1135 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1136 | weston_output_init(&output->base, &ec->base, x, y, |
Kristian Høgsberg | a4b7e20 | 2011-10-24 13:26:32 -0400 | [diff] [blame] | 1137 | connector->mmWidth, connector->mmHeight, 0); |
| 1138 | |
| 1139 | wl_list_insert(ec->base.output_list.prev, &output->base.link); |
| 1140 | |
Kristian Høgsberg | 9f404b7 | 2012-01-26 00:11:01 -0500 | [diff] [blame] | 1141 | output->scanout_buffer_destroy_listener.func = |
| 1142 | output_handle_scanout_buffer_destroy; |
| 1143 | output->pending_scanout_buffer_destroy_listener.func = |
| 1144 | output_handle_pending_scanout_buffer_destroy; |
| 1145 | |
Kristian Høgsberg | a4b7e20 | 2011-10-24 13:26:32 -0400 | [diff] [blame] | 1146 | output->pending_fs_surf_fb_id = 0; |
Kristian Høgsberg | 68c479a | 2012-01-25 23:32:28 -0500 | [diff] [blame] | 1147 | output->base.repaint = drm_output_repaint; |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 1148 | output->base.destroy = drm_output_destroy; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1149 | output->base.assign_planes = drm_assign_planes; |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1150 | output->base.set_dpms = drm_set_dpms; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 1151 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1152 | return 0; |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 1153 | |
David Herrmann | d7c4551 | 2011-12-08 17:05:46 +0100 | [diff] [blame] | 1154 | err_fb: |
| 1155 | glFramebufferRenderbuffer(GL_FRAMEBUFFER, |
| 1156 | GL_COLOR_ATTACHMENT0, |
| 1157 | GL_RENDERBUFFER, |
| 1158 | 0); |
| 1159 | err_bufs: |
| 1160 | for (i = 0; i < 2; i++) { |
| 1161 | if (output->fb_id[i] != -1) |
| 1162 | drmModeRmFB(ec->drm.fd, output->fb_id[i]); |
| 1163 | if (output->image[i]) |
| 1164 | ec->base.destroy_image(ec->base.display, |
| 1165 | output->image[i]); |
| 1166 | if (output->bo[i]) |
| 1167 | gbm_bo_destroy(output->bo[i]); |
| 1168 | } |
| 1169 | glBindRenderbuffer(GL_RENDERBUFFER, 0); |
| 1170 | glDeleteRenderbuffers(2, output->rbo); |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 1171 | err_free: |
| 1172 | wl_list_for_each_safe(drm_mode, next, &output->base.mode_list, |
| 1173 | base.link) { |
| 1174 | wl_list_remove(&drm_mode->base.link); |
| 1175 | free(drm_mode); |
| 1176 | } |
| 1177 | |
| 1178 | drmModeFreeCrtc(output->original_crtc); |
| 1179 | ec->crtc_allocator &= ~(1 << output->crtc_id); |
| 1180 | ec->connector_allocator &= ~(1 << output->connector_id); |
| 1181 | |
| 1182 | free(output); |
| 1183 | return -1; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1184 | } |
| 1185 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1186 | static void |
| 1187 | create_sprites(struct drm_compositor *ec) |
| 1188 | { |
| 1189 | struct drm_sprite *sprite; |
| 1190 | drmModePlaneRes *plane_res; |
| 1191 | drmModePlane *plane; |
| 1192 | int i; |
| 1193 | |
| 1194 | plane_res = drmModeGetPlaneResources(ec->drm.fd); |
| 1195 | if (!plane_res) { |
| 1196 | fprintf(stderr, "failed to get plane resources: %s\n", |
| 1197 | strerror(errno)); |
| 1198 | return; |
| 1199 | } |
| 1200 | |
| 1201 | for (i = 0; i < plane_res->count_planes; i++) { |
| 1202 | plane = drmModeGetPlane(ec->drm.fd, plane_res->planes[i]); |
| 1203 | if (!plane) |
| 1204 | continue; |
| 1205 | |
| 1206 | sprite = malloc(sizeof(*sprite) + ((sizeof(uint32_t)) * |
| 1207 | plane->count_formats)); |
| 1208 | if (!sprite) { |
| 1209 | fprintf(stderr, "%s: out of memory\n", |
| 1210 | __func__); |
| 1211 | free(plane); |
| 1212 | continue; |
| 1213 | } |
| 1214 | |
| 1215 | memset(sprite, 0, sizeof *sprite); |
| 1216 | |
| 1217 | sprite->possible_crtcs = plane->possible_crtcs; |
| 1218 | sprite->plane_id = plane->plane_id; |
| 1219 | sprite->surface = NULL; |
| 1220 | sprite->pending_surface = NULL; |
| 1221 | sprite->fb_id = 0; |
| 1222 | sprite->pending_fb_id = 0; |
| 1223 | sprite->destroy_listener.func = sprite_handle_buffer_destroy; |
| 1224 | sprite->pending_destroy_listener.func = |
| 1225 | sprite_handle_pending_buffer_destroy; |
| 1226 | sprite->compositor = ec; |
| 1227 | sprite->count_formats = plane->count_formats; |
| 1228 | memcpy(sprite->formats, plane->formats, |
Rob Clark | 8efbc8e | 2012-03-11 19:48:44 -0500 | [diff] [blame] | 1229 | plane->count_formats * sizeof(plane->formats[0])); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1230 | drmModeFreePlane(plane); |
| 1231 | |
| 1232 | wl_list_insert(&ec->sprite_list, &sprite->link); |
| 1233 | } |
| 1234 | |
| 1235 | free(plane_res->planes); |
| 1236 | free(plane_res); |
| 1237 | } |
| 1238 | |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 1239 | static void |
| 1240 | destroy_sprites(struct drm_compositor *compositor) |
| 1241 | { |
| 1242 | struct drm_sprite *sprite, *next; |
| 1243 | struct drm_output *output; |
| 1244 | |
| 1245 | output = container_of(compositor->base.output_list.next, |
| 1246 | struct drm_output, base.link); |
| 1247 | |
| 1248 | wl_list_for_each_safe(sprite, next, &compositor->sprite_list, link) { |
| 1249 | drmModeSetPlane(compositor->drm.fd, |
| 1250 | sprite->plane_id, |
| 1251 | output->crtc_id, 0, 0, |
| 1252 | 0, 0, 0, 0, 0, 0, 0, 0); |
| 1253 | drmModeRmFB(compositor->drm.fd, sprite->fb_id); |
| 1254 | free(sprite); |
| 1255 | } |
| 1256 | } |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1257 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1258 | static int |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1259 | create_outputs(struct drm_compositor *ec, int option_connector, |
| 1260 | struct udev_device *drm_device) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1261 | { |
| 1262 | drmModeConnector *connector; |
| 1263 | drmModeRes *resources; |
| 1264 | int i; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 1265 | int x = 0, y = 0; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1266 | |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 1267 | resources = drmModeGetResources(ec->drm.fd); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1268 | if (!resources) { |
| 1269 | fprintf(stderr, "drmModeGetResources failed\n"); |
| 1270 | return -1; |
| 1271 | } |
| 1272 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1273 | ec->crtcs = calloc(resources->count_crtcs, sizeof(uint32_t)); |
Christopher Michael | eb866cd | 2012-03-07 14:55:21 -0500 | [diff] [blame] | 1274 | if (!ec->crtcs) { |
| 1275 | drmModeFreeResources(resources); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1276 | return -1; |
Christopher Michael | eb866cd | 2012-03-07 14:55:21 -0500 | [diff] [blame] | 1277 | } |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1278 | |
| 1279 | ec->num_crtcs = resources->count_crtcs; |
| 1280 | memcpy(ec->crtcs, resources->crtcs, sizeof(uint32_t) * ec->num_crtcs); |
| 1281 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1282 | for (i = 0; i < resources->count_connectors; i++) { |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1283 | connector = drmModeGetConnector(ec->drm.fd, |
| 1284 | resources->connectors[i]); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1285 | if (connector == NULL) |
| 1286 | continue; |
| 1287 | |
| 1288 | if (connector->connection == DRM_MODE_CONNECTED && |
| 1289 | (option_connector == 0 || |
Benjamin Franzke | 9eaee35 | 2011-08-02 13:03:54 +0200 | [diff] [blame] | 1290 | connector->connector_id == option_connector)) { |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 1291 | if (create_output_for_connector(ec, resources, |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1292 | connector, x, y, |
| 1293 | drm_device) < 0) { |
Benjamin Franzke | 439d986 | 2011-10-07 08:20:53 +0200 | [diff] [blame] | 1294 | drmModeFreeConnector(connector); |
| 1295 | continue; |
| 1296 | } |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1297 | |
Benjamin Franzke | 9eaee35 | 2011-08-02 13:03:54 +0200 | [diff] [blame] | 1298 | x += container_of(ec->base.output_list.prev, |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1299 | struct weston_output, |
Benjamin Franzke | 9eaee35 | 2011-08-02 13:03:54 +0200 | [diff] [blame] | 1300 | link)->current->width; |
| 1301 | } |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 1302 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1303 | drmModeFreeConnector(connector); |
| 1304 | } |
| 1305 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1306 | if (wl_list_empty(&ec->base.output_list)) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1307 | fprintf(stderr, "No currently active connector found.\n"); |
Christopher Michael | eb866cd | 2012-03-07 14:55:21 -0500 | [diff] [blame] | 1308 | drmModeFreeResources(resources); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1309 | return -1; |
| 1310 | } |
| 1311 | |
| 1312 | drmModeFreeResources(resources); |
| 1313 | |
| 1314 | return 0; |
| 1315 | } |
| 1316 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1317 | static void |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1318 | update_outputs(struct drm_compositor *ec, struct udev_device *drm_device) |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1319 | { |
| 1320 | drmModeConnector *connector; |
| 1321 | drmModeRes *resources; |
| 1322 | struct drm_output *output, *next; |
| 1323 | int x = 0, y = 0; |
| 1324 | int x_offset = 0, y_offset = 0; |
| 1325 | uint32_t connected = 0, disconnects = 0; |
| 1326 | int i; |
| 1327 | |
| 1328 | resources = drmModeGetResources(ec->drm.fd); |
| 1329 | if (!resources) { |
| 1330 | fprintf(stderr, "drmModeGetResources failed\n"); |
| 1331 | return; |
| 1332 | } |
| 1333 | |
| 1334 | /* collect new connects */ |
| 1335 | for (i = 0; i < resources->count_connectors; i++) { |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1336 | int connector_id = resources->connectors[i]; |
| 1337 | |
| 1338 | connector = drmModeGetConnector(ec->drm.fd, connector_id); |
David Herrmann | 7551cff | 2011-12-08 17:05:43 +0100 | [diff] [blame] | 1339 | if (connector == NULL) |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1340 | continue; |
| 1341 | |
David Herrmann | 7551cff | 2011-12-08 17:05:43 +0100 | [diff] [blame] | 1342 | if (connector->connection != DRM_MODE_CONNECTED) { |
| 1343 | drmModeFreeConnector(connector); |
| 1344 | continue; |
| 1345 | } |
| 1346 | |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1347 | connected |= (1 << connector_id); |
| 1348 | |
| 1349 | if (!(ec->connector_allocator & (1 << connector_id))) { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1350 | struct weston_output *last = |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1351 | container_of(ec->base.output_list.prev, |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1352 | struct weston_output, link); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1353 | |
| 1354 | /* XXX: not yet needed, we die with 0 outputs */ |
| 1355 | if (!wl_list_empty(&ec->base.output_list)) |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1356 | x = last->x + last->current->width; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1357 | else |
| 1358 | x = 0; |
| 1359 | y = 0; |
| 1360 | create_output_for_connector(ec, resources, |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1361 | connector, x, y, |
| 1362 | drm_device); |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1363 | printf("connector %d connected\n", connector_id); |
| 1364 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1365 | } |
| 1366 | drmModeFreeConnector(connector); |
| 1367 | } |
| 1368 | drmModeFreeResources(resources); |
| 1369 | |
| 1370 | disconnects = ec->connector_allocator & ~connected; |
| 1371 | if (disconnects) { |
| 1372 | wl_list_for_each_safe(output, next, &ec->base.output_list, |
| 1373 | base.link) { |
| 1374 | if (x_offset != 0 || y_offset != 0) { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1375 | weston_output_move(&output->base, |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1376 | output->base.x - x_offset, |
| 1377 | output->base.y - y_offset); |
| 1378 | } |
| 1379 | |
| 1380 | if (disconnects & (1 << output->connector_id)) { |
| 1381 | disconnects &= ~(1 << output->connector_id); |
| 1382 | printf("connector %d disconnected\n", |
| 1383 | output->connector_id); |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1384 | x_offset += output->base.current->width; |
Benjamin Franzke | 48c4ea2 | 2011-08-30 11:44:56 +0200 | [diff] [blame] | 1385 | drm_output_destroy(&output->base); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1386 | } |
| 1387 | } |
| 1388 | } |
| 1389 | |
| 1390 | /* FIXME: handle zero outputs, without terminating */ |
| 1391 | if (ec->connector_allocator == 0) |
| 1392 | wl_display_terminate(ec->base.wl_display); |
| 1393 | } |
| 1394 | |
| 1395 | static int |
David Herrmann | d7488c2 | 2012-03-11 20:05:21 +0100 | [diff] [blame] | 1396 | udev_event_is_hotplug(struct drm_compositor *ec, struct udev_device *device) |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1397 | { |
David Herrmann | d7488c2 | 2012-03-11 20:05:21 +0100 | [diff] [blame] | 1398 | const char *sysnum; |
David Herrmann | 6ac52db | 2012-03-11 20:05:22 +0100 | [diff] [blame] | 1399 | const char *val; |
David Herrmann | d7488c2 | 2012-03-11 20:05:21 +0100 | [diff] [blame] | 1400 | |
| 1401 | sysnum = udev_device_get_sysnum(device); |
| 1402 | if (!sysnum || atoi(sysnum) != ec->drm.id) |
| 1403 | return 0; |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1404 | |
David Herrmann | 6ac52db | 2012-03-11 20:05:22 +0100 | [diff] [blame] | 1405 | val = udev_device_get_property_value(device, "HOTPLUG"); |
| 1406 | if (!val) |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1407 | return 0; |
| 1408 | |
David Herrmann | 6ac52db | 2012-03-11 20:05:22 +0100 | [diff] [blame] | 1409 | return strcmp(val, "1") == 0; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1410 | } |
| 1411 | |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 1412 | static int |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1413 | udev_drm_event(int fd, uint32_t mask, void *data) |
| 1414 | { |
| 1415 | struct drm_compositor *ec = data; |
| 1416 | struct udev_device *event; |
| 1417 | |
| 1418 | event = udev_monitor_receive_device(ec->udev_monitor); |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1419 | |
David Herrmann | d7488c2 | 2012-03-11 20:05:21 +0100 | [diff] [blame] | 1420 | if (udev_event_is_hotplug(ec, event)) |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1421 | update_outputs(ec, event); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1422 | |
| 1423 | udev_device_unref(event); |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 1424 | |
| 1425 | return 1; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1426 | } |
| 1427 | |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 1428 | static void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1429 | drm_destroy(struct weston_compositor *ec) |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 1430 | { |
| 1431 | struct drm_compositor *d = (struct drm_compositor *) ec; |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1432 | struct weston_input_device *input, *next; |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 1433 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1434 | weston_compositor_shutdown(ec); |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 1435 | gbm_device_destroy(d->gbm); |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 1436 | destroy_sprites(d); |
Kristian Høgsberg | 62d2774 | 2012-01-18 12:38:33 -0500 | [diff] [blame] | 1437 | drmDropMaster(d->drm.fd); |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 1438 | tty_destroy(d->tty); |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 1439 | |
Tiago Vignatti | c349e1d | 2011-12-18 23:52:18 +0200 | [diff] [blame] | 1440 | wl_list_for_each_safe(input, next, &ec->input_device_list, link) |
| 1441 | evdev_input_destroy(input); |
| 1442 | |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 1443 | free(d); |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 1444 | } |
| 1445 | |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 1446 | static void |
Kristian Høgsberg | 835cd49 | 2012-01-18 11:48:46 -0500 | [diff] [blame] | 1447 | drm_compositor_set_modes(struct drm_compositor *compositor) |
| 1448 | { |
| 1449 | struct drm_output *output; |
| 1450 | struct drm_mode *drm_mode; |
| 1451 | int ret; |
| 1452 | |
| 1453 | wl_list_for_each(output, &compositor->base.output_list, base.link) { |
| 1454 | drm_mode = (struct drm_mode *) output->base.current; |
| 1455 | ret = drmModeSetCrtc(compositor->drm.fd, output->crtc_id, |
| 1456 | output->fb_id[output->current ^ 1], 0, 0, |
| 1457 | &output->connector_id, 1, |
| 1458 | &drm_mode->mode_info); |
| 1459 | if (ret < 0) { |
| 1460 | fprintf(stderr, |
| 1461 | "failed to set mode %dx%d for output at %d,%d: %m", |
| 1462 | drm_mode->base.width, drm_mode->base.height, |
| 1463 | output->base.x, output->base.y); |
| 1464 | } |
| 1465 | } |
| 1466 | } |
| 1467 | |
| 1468 | static void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1469 | vt_func(struct weston_compositor *compositor, int event) |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 1470 | { |
| 1471 | struct drm_compositor *ec = (struct drm_compositor *) compositor; |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1472 | struct weston_output *output; |
| 1473 | struct weston_input_device *input; |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 1474 | struct drm_sprite *sprite; |
| 1475 | struct drm_output *drm_output; |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 1476 | |
| 1477 | switch (event) { |
| 1478 | case TTY_ENTER_VT: |
| 1479 | compositor->focus = 1; |
Kristian Høgsberg | a018fb0 | 2012-01-16 10:52:52 -0500 | [diff] [blame] | 1480 | if (drmSetMaster(ec->drm.fd)) { |
| 1481 | fprintf(stderr, "failed to set master: %m\n"); |
| 1482 | wl_display_terminate(compositor->wl_display); |
| 1483 | } |
Pekka Paalanen | bce2d3f | 2011-12-02 13:07:27 +0200 | [diff] [blame] | 1484 | compositor->state = ec->prev_state; |
Kristian Høgsberg | 835cd49 | 2012-01-18 11:48:46 -0500 | [diff] [blame] | 1485 | drm_compositor_set_modes(ec); |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1486 | weston_compositor_damage_all(compositor); |
Tiago Vignatti | 6e2d5f1 | 2011-12-19 00:32:48 +0200 | [diff] [blame] | 1487 | wl_list_for_each(input, &compositor->input_device_list, link) |
| 1488 | evdev_add_devices(ec->udev, input); |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 1489 | break; |
| 1490 | case TTY_LEAVE_VT: |
| 1491 | compositor->focus = 0; |
Pekka Paalanen | bce2d3f | 2011-12-02 13:07:27 +0200 | [diff] [blame] | 1492 | ec->prev_state = compositor->state; |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1493 | compositor->state = WESTON_COMPOSITOR_SLEEPING; |
Kristian Høgsberg | d8e181b | 2011-05-06 15:38:28 -0400 | [diff] [blame] | 1494 | |
Kristian Høgsberg | 34f80ff | 2012-01-18 11:50:31 -0500 | [diff] [blame] | 1495 | /* If we have a repaint scheduled (either from a |
| 1496 | * pending pageflip or the idle handler), make sure we |
| 1497 | * cancel that so we don't try to pageflip when we're |
| 1498 | * vt switched away. The SLEEPING state will prevent |
| 1499 | * further attemps at repainting. When we switch |
| 1500 | * back, we schedule a repaint, which will process |
| 1501 | * pending frame callbacks. */ |
| 1502 | |
| 1503 | wl_list_for_each(output, &ec->base.output_list, link) { |
| 1504 | output->repaint_needed = 0; |
| 1505 | drm_output_set_cursor(output, NULL); |
| 1506 | } |
| 1507 | |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 1508 | drm_output = container_of(ec->base.output_list.next, |
| 1509 | struct drm_output, base.link); |
| 1510 | |
| 1511 | wl_list_for_each(sprite, &ec->sprite_list, link) |
| 1512 | drmModeSetPlane(ec->drm.fd, |
| 1513 | sprite->plane_id, |
| 1514 | drm_output->crtc_id, 0, 0, |
| 1515 | 0, 0, 0, 0, 0, 0, 0, 0); |
| 1516 | |
Tiago Vignatti | 6e2d5f1 | 2011-12-19 00:32:48 +0200 | [diff] [blame] | 1517 | wl_list_for_each(input, &compositor->input_device_list, link) |
| 1518 | evdev_remove_devices(input); |
Kristian Høgsberg | d8e181b | 2011-05-06 15:38:28 -0400 | [diff] [blame] | 1519 | |
Kristian Høgsberg | a018fb0 | 2012-01-16 10:52:52 -0500 | [diff] [blame] | 1520 | if (drmDropMaster(ec->drm.fd) < 0) |
| 1521 | fprintf(stderr, "failed to drop master: %m\n"); |
| 1522 | |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 1523 | break; |
| 1524 | }; |
| 1525 | } |
| 1526 | |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 1527 | static const char default_seat[] = "seat0"; |
| 1528 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1529 | static struct weston_compositor * |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 1530 | drm_compositor_create(struct wl_display *display, |
Tiago Vignatti | faee801 | 2011-09-01 15:58:17 -0400 | [diff] [blame] | 1531 | int connector, const char *seat, int tty) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1532 | { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1533 | struct drm_compositor *ec; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1534 | struct udev_enumerate *e; |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1535 | struct udev_list_entry *entry; |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 1536 | struct udev_device *device, *drm_device; |
| 1537 | const char *path, *device_seat; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1538 | struct wl_event_loop *loop; |
| 1539 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1540 | ec = malloc(sizeof *ec); |
| 1541 | if (ec == NULL) |
| 1542 | return NULL; |
| 1543 | |
| 1544 | memset(ec, 0, sizeof *ec); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1545 | ec->udev = udev_new(); |
| 1546 | if (ec->udev == NULL) { |
| 1547 | fprintf(stderr, "failed to initialize udev context\n"); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1548 | return NULL; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1549 | } |
| 1550 | |
Kristian Høgsberg | c5b9ddb | 2012-01-15 14:29:09 -0500 | [diff] [blame] | 1551 | ec->base.wl_display = display; |
| 1552 | ec->tty = tty_create(&ec->base, vt_func, tty); |
| 1553 | if (!ec->tty) { |
| 1554 | fprintf(stderr, "failed to initialize tty\n"); |
| 1555 | free(ec); |
| 1556 | return NULL; |
| 1557 | } |
| 1558 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1559 | e = udev_enumerate_new(ec->udev); |
| 1560 | udev_enumerate_add_match_subsystem(e, "drm"); |
Benjamin Franzke | a764ee5 | 2011-10-07 08:23:22 +0200 | [diff] [blame] | 1561 | udev_enumerate_add_match_sysname(e, "card[0-9]*"); |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 1562 | |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1563 | udev_enumerate_scan_devices(e); |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 1564 | drm_device = NULL; |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1565 | udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1566 | path = udev_list_entry_get_name(entry); |
| 1567 | device = udev_device_new_from_syspath(ec->udev, path); |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1568 | device_seat = |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 1569 | udev_device_get_property_value(device, "ID_SEAT"); |
| 1570 | if (!device_seat) |
| 1571 | device_seat = default_seat; |
| 1572 | if (strcmp(device_seat, seat) == 0) { |
| 1573 | drm_device = device; |
| 1574 | break; |
| 1575 | } |
| 1576 | udev_device_unref(device); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1577 | } |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 1578 | |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 1579 | if (drm_device == NULL) { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1580 | fprintf(stderr, "no drm device found\n"); |
| 1581 | return NULL; |
| 1582 | } |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1583 | |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 1584 | if (init_egl(ec, drm_device) < 0) { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1585 | fprintf(stderr, "failed to initialize egl\n"); |
| 1586 | return NULL; |
| 1587 | } |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 1588 | |
| 1589 | ec->base.destroy = drm_destroy; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 1590 | |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 1591 | ec->base.focus = 1; |
| 1592 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1593 | ec->prev_state = WESTON_COMPOSITOR_ACTIVE; |
Pekka Paalanen | bce2d3f | 2011-12-02 13:07:27 +0200 | [diff] [blame] | 1594 | |
Benjamin Franzke | 5b2cb6f | 2011-02-18 16:54:55 +0100 | [diff] [blame] | 1595 | glGenFramebuffers(1, &ec->base.fbo); |
| 1596 | glBindFramebuffer(GL_FRAMEBUFFER, ec->base.fbo); |
| 1597 | |
Benjamin Franzke | d59eb1c | 2011-04-29 22:14:54 +0200 | [diff] [blame] | 1598 | /* 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] | 1599 | if (weston_compositor_init(&ec->base, display) < 0) |
Benjamin Franzke | d59eb1c | 2011-04-29 22:14:54 +0200 | [diff] [blame] | 1600 | return NULL; |
| 1601 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1602 | wl_list_init(&ec->sprite_list); |
| 1603 | create_sprites(ec); |
| 1604 | |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1605 | if (create_outputs(ec, connector, drm_device) < 0) { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1606 | fprintf(stderr, "failed to create output for %s\n", path); |
| 1607 | return NULL; |
| 1608 | } |
| 1609 | |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1610 | udev_device_unref(drm_device); |
Benjamin Franzke | 02dee2c | 2011-10-07 08:27:26 +0200 | [diff] [blame] | 1611 | udev_enumerate_unref(e); |
| 1612 | path = NULL; |
| 1613 | |
Tiago Vignatti | ce03ec3 | 2011-12-19 01:14:03 +0200 | [diff] [blame] | 1614 | evdev_input_create(&ec->base, ec->udev, seat); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1615 | |
| 1616 | loop = wl_display_get_event_loop(ec->base.wl_display); |
| 1617 | ec->drm_source = |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 1618 | wl_event_loop_add_fd(loop, ec->drm.fd, |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1619 | WL_EVENT_READABLE, on_drm_input, ec); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1620 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1621 | ec->udev_monitor = udev_monitor_new_from_netlink(ec->udev, "udev"); |
| 1622 | if (ec->udev_monitor == NULL) { |
| 1623 | fprintf(stderr, "failed to intialize udev monitor\n"); |
| 1624 | return NULL; |
| 1625 | } |
| 1626 | udev_monitor_filter_add_match_subsystem_devtype(ec->udev_monitor, |
| 1627 | "drm", NULL); |
| 1628 | ec->udev_drm_source = |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1629 | wl_event_loop_add_fd(loop, |
| 1630 | udev_monitor_get_fd(ec->udev_monitor), |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1631 | WL_EVENT_READABLE, udev_drm_event, ec); |
| 1632 | |
| 1633 | if (udev_monitor_enable_receiving(ec->udev_monitor) < 0) { |
| 1634 | fprintf(stderr, "failed to enable udev-monitor receiving\n"); |
| 1635 | return NULL; |
| 1636 | } |
| 1637 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1638 | return &ec->base; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1639 | } |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 1640 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1641 | WL_EXPORT struct weston_compositor * |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 1642 | backend_init(struct wl_display *display, int argc, char *argv[]) |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 1643 | { |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 1644 | int connector = 0, tty = 0; |
| 1645 | const char *seat = default_seat; |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 1646 | |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 1647 | const struct weston_option drm_options[] = { |
| 1648 | { WESTON_OPTION_INTEGER, "connector", 0, &connector }, |
| 1649 | { WESTON_OPTION_STRING, "seat", 0, &seat }, |
| 1650 | { WESTON_OPTION_INTEGER, "tty", 0, &tty }, |
| 1651 | }; |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1652 | |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 1653 | parse_options(drm_options, ARRAY_LENGTH(drm_options), argc, argv); |
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 | return drm_compositor_create(display, connector, seat, tty); |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 1656 | } |