2 * Copyright © 2008-2011 Kristian Høgsberg
3 * Copyright © 2011 Intel Corporation
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.
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.
31 #include <linux/input.h>
35 #include <xf86drmMode.h>
36 #include <drm_fourcc.h>
39 #include <libbacklight.h>
42 #include "compositor.h"
44 #include "launcher-util.h"
46 static int option_current_mode = 0;
47 static char *output_name;
48 static char *output_mode;
49 static struct wl_list configured_output_list;
52 OUTPUT_CONFIG_INVALID = 0,
54 OUTPUT_CONFIG_PREFERRED,
55 OUTPUT_CONFIG_CURRENT,
57 OUTPUT_CONFIG_MODELINE
60 struct drm_configured_output {
63 int32_t width, height;
64 drmModeModeInfo crtc_mode;
65 enum output_config config;
69 struct drm_compositor {
70 struct weston_compositor base;
73 struct wl_event_source *drm_source;
75 struct udev_monitor *udev_monitor;
76 struct wl_event_source *udev_drm_source;
82 struct gbm_device *gbm;
85 uint32_t crtc_allocator;
86 uint32_t connector_allocator;
89 struct gbm_surface *dummy_surface;
90 EGLSurface dummy_egl_surface;
92 struct wl_list sprite_list;
93 int sprites_are_broken;
99 struct weston_mode base;
100 drmModeModeInfo mode_info;
107 struct drm_output *output;
109 int is_client_buffer;
110 struct wl_buffer *buffer;
111 struct wl_listener buffer_destroy_listener;
115 struct weston_output base;
119 uint32_t connector_id;
120 drmModeCrtcPtr original_crtc;
123 int page_flip_pending;
125 struct gbm_surface *surface;
126 struct gbm_bo *cursor_bo[2];
127 struct weston_plane cursor_plane;
128 struct weston_plane fb_plane;
129 struct weston_surface *cursor_surface;
131 EGLSurface egl_surface;
132 struct drm_fb *current, *next;
133 struct backlight *backlight;
137 * An output has a primary display plane plus zero or more sprites for
138 * blending display contents.
144 uint32_t pending_fb_id;
145 struct weston_surface *surface;
146 struct weston_surface *pending_surface;
147 struct weston_plane plane;
149 struct drm_output *output;
151 struct drm_compositor *compositor;
153 struct wl_listener destroy_listener;
154 struct wl_listener pending_destroy_listener;
156 uint32_t possible_crtcs;
158 uint32_t count_formats;
160 int32_t src_x, src_y;
161 uint32_t src_w, src_h;
162 uint32_t dest_x, dest_y;
163 uint32_t dest_w, dest_h;
169 struct weston_seat base;
170 struct wl_list devices_list;
171 struct udev_monitor *udev_monitor;
172 struct wl_event_source *udev_monitor_source;
177 drm_output_set_cursor(struct drm_output *output);
179 drm_disable_unused_sprites(struct weston_output *output_base);
182 drm_sprite_crtc_supported(struct weston_output *output_base, uint32_t supported)
184 struct weston_compositor *ec = output_base->compositor;
185 struct drm_compositor *c =(struct drm_compositor *) ec;
186 struct drm_output *output = (struct drm_output *) output_base;
189 for (crtc = 0; crtc < c->num_crtcs; crtc++) {
190 if (c->crtcs[crtc] != output->crtc_id)
193 if (supported & (1 << crtc))
201 drm_fb_destroy_callback(struct gbm_bo *bo, void *data)
203 struct drm_fb *fb = data;
204 struct gbm_device *gbm = gbm_bo_get_device(bo);
207 drmModeRmFB(gbm_device_get_fd(gbm), fb->fb_id);
210 weston_buffer_post_release(fb->buffer);
211 wl_list_remove(&fb->buffer_destroy_listener.link);
217 static struct drm_fb *
218 drm_fb_get_from_bo(struct gbm_bo *bo, struct drm_output *output)
220 struct drm_fb *fb = gbm_bo_get_user_data(bo);
221 struct drm_compositor *compositor =
222 (struct drm_compositor *) output->base.compositor;
223 uint32_t width, height, stride, handle;
229 fb = malloc(sizeof *fb);
233 fb->is_client_buffer = 0;
236 width = gbm_bo_get_width(bo);
237 height = gbm_bo_get_height(bo);
238 stride = gbm_bo_get_stride(bo);
239 handle = gbm_bo_get_handle(bo).u32;
241 ret = drmModeAddFB(compositor->drm.fd, width, height, 24, 32,
242 stride, handle, &fb->fb_id);
244 weston_log("failed to create kms fb: %m\n");
249 gbm_bo_set_user_data(bo, fb, drm_fb_destroy_callback);
255 fb_handle_buffer_destroy(struct wl_listener *listener, void *data)
257 struct drm_fb *fb = container_of(listener, struct drm_fb,
258 buffer_destroy_listener);
262 if (fb == fb->output->next ||
263 (fb == fb->output->current && !fb->output->next))
264 weston_output_schedule_repaint(&fb->output->base);
267 static struct weston_plane *
268 drm_output_prepare_scanout_surface(struct weston_output *_output,
269 struct weston_surface *es)
271 struct drm_output *output = (struct drm_output *) _output;
272 struct drm_compositor *c =
273 (struct drm_compositor *) output->base.compositor;
276 if (es->geometry.x != output->base.x ||
277 es->geometry.y != output->base.y ||
278 es->geometry.width != output->base.current->width ||
279 es->geometry.height != output->base.current->height ||
280 es->transform.enabled ||
284 bo = gbm_bo_import(c->gbm, GBM_BO_IMPORT_WL_BUFFER,
285 es->buffer, GBM_BO_USE_SCANOUT);
287 /* Need to verify output->region contained in surface opaque
288 * region. Or maybe just that format doesn't have alpha.
289 * For now, scanout only if format is XRGB8888. */
290 if (gbm_bo_get_format(bo) != GBM_FORMAT_XRGB8888) {
295 output->next = drm_fb_get_from_bo(bo, output);
301 output->next->is_client_buffer = 1;
302 output->next->buffer = es->buffer;
303 output->next->buffer->busy_count++;
304 output->next->buffer_destroy_listener.notify = fb_handle_buffer_destroy;
306 wl_signal_add(&output->next->buffer->resource.destroy_signal,
307 &output->next->buffer_destroy_listener);
309 return &output->fb_plane;
313 drm_output_render(struct drm_output *output, pixman_region32_t *damage)
315 struct drm_compositor *compositor =
316 (struct drm_compositor *) output->base.compositor;
317 struct weston_surface *surface;
320 if (!eglMakeCurrent(compositor->base.egl_display, output->egl_surface,
322 compositor->base.egl_context)) {
323 weston_log("failed to make current\n");
327 wl_list_for_each_reverse(surface, &compositor->base.surface_list, link)
328 if (surface->plane == &compositor->base.primary_plane)
329 weston_surface_draw(surface, &output->base, damage);
331 wl_signal_emit(&output->base.frame_signal, output);
333 eglSwapBuffers(compositor->base.egl_display, output->egl_surface);
334 bo = gbm_surface_lock_front_buffer(output->surface);
336 weston_log("failed to lock front buffer: %m\n");
340 output->next = drm_fb_get_from_bo(bo, output);
342 weston_log("failed to get drm_fb for bo\n");
343 gbm_surface_release_buffer(output->surface, bo);
349 drm_output_repaint(struct weston_output *output_base,
350 pixman_region32_t *damage)
352 struct drm_output *output = (struct drm_output *) output_base;
353 struct drm_compositor *compositor =
354 (struct drm_compositor *) output->base.compositor;
355 struct drm_sprite *s;
356 struct drm_mode *mode;
360 drm_output_render(output, damage);
364 mode = container_of(output->base.current, struct drm_mode, base);
365 if (!output->current) {
366 ret = drmModeSetCrtc(compositor->drm.fd, output->crtc_id,
367 output->next->fb_id, 0, 0,
368 &output->connector_id, 1,
371 weston_log("set mode failed: %m\n");
376 if (drmModePageFlip(compositor->drm.fd, output->crtc_id,
378 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
379 weston_log("queueing pageflip failed: %m\n");
383 output->page_flip_pending = 1;
385 drm_output_set_cursor(output);
388 * Now, update all the sprite surfaces
390 wl_list_for_each(s, &compositor->sprite_list, link) {
393 .request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT,
394 .request.sequence = 1,
397 if (!drm_sprite_crtc_supported(output_base, s->possible_crtcs))
400 ret = drmModeSetPlane(compositor->drm.fd, s->plane_id,
401 output->crtc_id, s->pending_fb_id, flags,
402 s->dest_x, s->dest_y,
403 s->dest_w, s->dest_h,
407 weston_log("setplane failed: %d: %s\n",
408 ret, strerror(errno));
411 * Queue a vblank signal so we know when the surface
412 * becomes active on the display or has been replaced.
414 vbl.request.signal = (unsigned long)s;
415 ret = drmWaitVBlank(compositor->drm.fd, &vbl);
417 weston_log("vblank event request failed: %d: %s\n",
418 ret, strerror(errno));
422 output->vblank_pending = 1;
425 drm_disable_unused_sprites(&output->base);
431 vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec,
434 struct drm_sprite *s = (struct drm_sprite *)data;
435 struct drm_compositor *c = s->compositor;
436 struct drm_output *output = s->output;
439 output->vblank_pending = 0;
442 weston_buffer_post_release(s->surface->buffer);
443 wl_list_remove(&s->destroy_listener.link);
445 drmModeRmFB(c->drm.fd, s->fb_id);
449 if (s->pending_surface) {
450 wl_list_remove(&s->pending_destroy_listener.link);
451 wl_signal_add(&s->pending_surface->buffer->resource.destroy_signal,
452 &s->destroy_listener);
453 s->surface = s->pending_surface;
454 s->pending_surface = NULL;
455 s->fb_id = s->pending_fb_id;
456 s->pending_fb_id = 0;
459 if (!output->page_flip_pending) {
460 msecs = sec * 1000 + usec / 1000;
461 weston_output_finish_frame(&output->base, msecs);
466 page_flip_handler(int fd, unsigned int frame,
467 unsigned int sec, unsigned int usec, void *data)
469 struct drm_output *output = (struct drm_output *) data;
472 output->page_flip_pending = 0;
474 if (output->current) {
475 if (output->current->is_client_buffer)
476 gbm_bo_destroy(output->current->bo);
478 gbm_surface_release_buffer(output->surface,
479 output->current->bo);
482 output->current = output->next;
485 if (!output->vblank_pending) {
486 msecs = sec * 1000 + usec / 1000;
487 weston_output_finish_frame(&output->base, msecs);
492 drm_surface_format_supported(struct drm_sprite *s, uint32_t format)
496 for (i = 0; i < s->count_formats; i++)
497 if (s->formats[i] == format)
504 drm_surface_transform_supported(struct weston_surface *es)
506 struct weston_matrix *matrix = &es->transform.matrix;
509 if (!es->transform.enabled)
512 for (i = 0; i < 16; i++) {
516 if (matrix->d[i] != 1.0)
525 if (matrix->d[i] != 0.0)
535 drm_disable_unused_sprites(struct weston_output *output_base)
537 struct weston_compositor *ec = output_base->compositor;
538 struct drm_compositor *c =(struct drm_compositor *) ec;
539 struct drm_output *output = (struct drm_output *) output_base;
540 struct drm_sprite *s;
543 wl_list_for_each(s, &c->sprite_list, link) {
544 if (s->pending_fb_id)
547 ret = drmModeSetPlane(c->drm.fd, s->plane_id,
548 output->crtc_id, 0, 0,
549 0, 0, 0, 0, 0, 0, 0, 0);
551 weston_log("failed to disable plane: %d: %s\n",
552 ret, strerror(errno));
553 drmModeRmFB(c->drm.fd, s->fb_id);
557 wl_list_remove(&s->destroy_listener.link);
560 assert(!s->pending_surface);
562 s->pending_fb_id = 0;
567 * This function must take care to damage any previously assigned surface
568 * if the sprite ends up binding to a different surface than in the
571 static struct weston_plane *
572 drm_output_prepare_overlay_surface(struct weston_output *output_base,
573 struct weston_surface *es)
575 struct weston_compositor *ec = output_base->compositor;
576 struct drm_compositor *c =(struct drm_compositor *) ec;
577 struct drm_sprite *s;
579 EGLint handle, stride;
582 uint32_t handles[4], pitches[4], offsets[4];
584 pixman_region32_t dest_rect, src_rect;
587 wl_fixed_t sx1, sy1, sx2, sy2;
589 if (c->sprites_are_broken)
592 if (es->output_mask != (1u << output_base->id))
595 if (es->buffer == NULL)
598 if (!drm_surface_transform_supported(es))
601 wl_list_for_each(s, &c->sprite_list, link) {
602 if (!drm_sprite_crtc_supported(output_base, s->possible_crtcs))
605 if (!s->pending_fb_id) {
611 /* No sprites available */
615 bo = gbm_bo_import(c->gbm, GBM_BO_IMPORT_WL_BUFFER,
616 es->buffer, GBM_BO_USE_SCANOUT);
620 format = gbm_bo_get_format(bo);
621 handle = gbm_bo_get_handle(bo).s32;
622 stride = gbm_bo_get_stride(bo);
626 if (!drm_surface_format_supported(s, format))
636 ret = drmModeAddFB2(c->drm.fd, es->geometry.width, es->geometry.height,
637 format, handles, pitches, offsets,
640 weston_log("addfb2 failed: %d\n", ret);
641 c->sprites_are_broken = 1;
645 s->pending_fb_id = fb_id;
646 s->pending_surface = es;
647 es->buffer->busy_count++;
649 box = pixman_region32_extents(&es->transform.boundingbox);
650 s->plane.x = box->x1;
651 s->plane.y = box->y1;
654 * Calculate the source & dest rects properly based on actual
655 * postion (note the caller has called weston_surface_update_transform()
658 pixman_region32_init(&dest_rect);
659 pixman_region32_intersect(&dest_rect, &es->transform.boundingbox,
660 &output_base->region);
661 pixman_region32_translate(&dest_rect, -output_base->x, -output_base->y);
662 box = pixman_region32_extents(&dest_rect);
665 s->dest_w = box->x2 - box->x1;
666 s->dest_h = box->y2 - box->y1;
667 pixman_region32_fini(&dest_rect);
669 pixman_region32_init(&src_rect);
670 pixman_region32_intersect(&src_rect, &es->transform.boundingbox,
671 &output_base->region);
672 box = pixman_region32_extents(&src_rect);
674 weston_surface_from_global_fixed(es,
675 wl_fixed_from_int(box->x1),
676 wl_fixed_from_int(box->y1),
678 weston_surface_from_global_fixed(es,
679 wl_fixed_from_int(box->x2),
680 wl_fixed_from_int(box->y2),
687 if (sx2 > wl_fixed_from_int(es->geometry.width))
688 sx2 = wl_fixed_from_int(es->geometry.width);
689 if (sy2 > wl_fixed_from_int(es->geometry.height))
690 sy2 = wl_fixed_from_int(es->geometry.height);
694 s->src_w = (sx2 - sx1) << 8;
695 s->src_h = (sy2 - sy1) << 8;
696 pixman_region32_fini(&src_rect);
698 wl_signal_add(&es->buffer->resource.destroy_signal,
699 &s->pending_destroy_listener);
704 static struct weston_plane *
705 drm_output_prepare_cursor_surface(struct weston_output *output_base,
706 struct weston_surface *es)
708 struct drm_output *output = (struct drm_output *) output_base;
710 if (output->cursor_surface)
712 if (es->output_mask != (1u << output_base->id))
714 if (es->buffer == NULL || !wl_buffer_is_shm(es->buffer) ||
715 es->geometry.width > 64 || es->geometry.height > 64)
718 output->cursor_surface = es;
720 return &output->cursor_plane;
724 drm_output_set_cursor(struct drm_output *output)
726 struct weston_surface *es = output->cursor_surface;
727 struct drm_compositor *c =
728 (struct drm_compositor *) output->base.compositor;
729 EGLint handle, stride;
731 uint32_t buf[64 * 64];
735 output->cursor_surface = NULL;
737 drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0);
741 if (es->buffer && pixman_region32_not_empty(&output->cursor_plane.damage)) {
742 pixman_region32_fini(&output->cursor_plane.damage);
743 pixman_region32_init(&output->cursor_plane.damage);
744 output->current_cursor ^= 1;
745 bo = output->cursor_bo[output->current_cursor];
746 memset(buf, 0, sizeof buf);
747 stride = wl_shm_buffer_get_stride(es->buffer);
748 s = wl_shm_buffer_get_data(es->buffer);
749 for (i = 0; i < es->geometry.height; i++)
750 memcpy(buf + i * 64, s + i * stride,
751 es->geometry.width * 4);
753 if (gbm_bo_write(bo, buf, sizeof buf) < 0)
754 weston_log("failed update cursor: %m\n");
756 handle = gbm_bo_get_handle(bo).s32;
757 if (drmModeSetCursor(c->drm.fd,
758 output->crtc_id, handle, 64, 64))
759 weston_log("failed to set cursor: %m\n");
762 x = es->geometry.x - output->base.x;
763 y = es->geometry.y - output->base.y;
764 if (output->cursor_plane.x != x || output->cursor_plane.y != y) {
765 if (drmModeMoveCursor(c->drm.fd, output->crtc_id, x, y))
766 weston_log("failed to move cursor: %m\n");
767 output->cursor_plane.x = x;
768 output->cursor_plane.y = y;
773 drm_assign_planes(struct weston_output *output)
775 struct drm_compositor *c =
776 (struct drm_compositor *) output->compositor;
777 struct weston_surface *es, *next;
778 pixman_region32_t overlap, surface_overlap;
779 struct weston_plane *primary, *next_plane;
782 * Find a surface for each sprite in the output using some heuristics:
784 * 2) frequency of update
785 * 3) opacity (though some hw might support alpha blending)
786 * 4) clipping (this can be fixed with color keys)
788 * The idea is to save on blitting since this should save power.
789 * If we can get a large video surface on the sprite for example,
790 * the main display surface may not need to update at all, and
791 * the client buffer can be used directly for the sprite surface
792 * as we do for flipping full screen surfaces.
794 pixman_region32_init(&overlap);
795 primary = &c->base.primary_plane;
796 wl_list_for_each_safe(es, next, &c->base.surface_list, link) {
797 pixman_region32_init(&surface_overlap);
798 pixman_region32_intersect(&surface_overlap, &overlap,
799 &es->transform.boundingbox);
802 if (pixman_region32_not_empty(&surface_overlap))
803 next_plane = primary;
804 if (next_plane == NULL)
805 next_plane = drm_output_prepare_cursor_surface(output, es);
806 if (next_plane == NULL)
807 next_plane = drm_output_prepare_scanout_surface(output, es);
808 if (next_plane == NULL)
809 next_plane = drm_output_prepare_overlay_surface(output, es);
810 if (next_plane == NULL)
811 next_plane = primary;
812 weston_surface_move_to_plane(es, next_plane);
813 if (next_plane == primary)
814 pixman_region32_union(&overlap, &overlap,
815 &es->transform.boundingbox);
817 pixman_region32_fini(&surface_overlap);
819 pixman_region32_fini(&overlap);
823 drm_output_destroy(struct weston_output *output_base)
825 struct drm_output *output = (struct drm_output *) output_base;
826 struct drm_compositor *c =
827 (struct drm_compositor *) output->base.compositor;
828 drmModeCrtcPtr origcrtc = output->original_crtc;
830 if (output->backlight)
831 backlight_destroy(output->backlight);
833 /* Turn off hardware cursor */
834 drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0);
836 /* Restore original CRTC state */
837 drmModeSetCrtc(c->drm.fd, origcrtc->crtc_id, origcrtc->buffer_id,
838 origcrtc->x, origcrtc->y,
839 &output->connector_id, 1, &origcrtc->mode);
840 drmModeFreeCrtc(origcrtc);
842 c->crtc_allocator &= ~(1 << output->crtc_id);
843 c->connector_allocator &= ~(1 << output->connector_id);
845 eglDestroySurface(c->base.egl_display, output->egl_surface);
846 gbm_surface_destroy(output->surface);
848 weston_plane_release(&output->fb_plane);
849 weston_plane_release(&output->cursor_plane);
851 weston_output_destroy(&output->base);
852 wl_list_remove(&output->base.link);
858 static struct drm_mode *
859 choose_mode (struct drm_output *output, struct weston_mode *target_mode)
861 struct drm_mode *tmp_mode = NULL, *mode;
863 if (output->base.current->width == target_mode->width &&
864 output->base.current->height == target_mode->height &&
865 (output->base.current->refresh == target_mode->refresh ||
866 target_mode->refresh == 0))
867 return (struct drm_mode *)output->base.current;
869 wl_list_for_each(mode, &output->base.mode_list, base.link) {
870 if (mode->mode_info.hdisplay == target_mode->width &&
871 mode->mode_info.vdisplay == target_mode->height) {
872 if (mode->mode_info.vrefresh == target_mode->refresh ||
873 target_mode->refresh == 0) {
875 } else if (!tmp_mode)
884 drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mode)
886 struct drm_output *output;
887 struct drm_mode *drm_mode;
889 struct drm_compositor *ec;
890 struct gbm_surface *surface;
891 EGLSurface egl_surface;
893 if (output_base == NULL) {
894 weston_log("output is NULL.\n");
899 weston_log("mode is NULL.\n");
903 ec = (struct drm_compositor *)output_base->compositor;
904 output = (struct drm_output *)output_base;
905 drm_mode = choose_mode (output, mode);
908 weston_log("%s, invalid resolution:%dx%d\n", __func__, mode->width, mode->height);
910 } else if (&drm_mode->base == output->base.current) {
912 } else if (drm_mode->base.width == output->base.current->width &&
913 drm_mode->base.height == output->base.current->height) {
914 /* only change refresh value */
915 ret = drmModeSetCrtc(ec->drm.fd,
917 output->current->fb_id, 0, 0,
918 &output->connector_id, 1, &drm_mode->mode_info);
921 weston_log("failed to set mode (%dx%d) %u Hz\n",
922 drm_mode->base.width,
923 drm_mode->base.height,
924 drm_mode->base.refresh / 1000);
927 output->base.current->flags = 0;
928 output->base.current = &drm_mode->base;
929 drm_mode->base.flags =
930 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
937 drm_mode->base.flags =
938 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
940 surface = gbm_surface_create(ec->gbm,
941 drm_mode->base.width,
942 drm_mode->base.height,
945 GBM_BO_USE_RENDERING);
947 weston_log("failed to create gbm surface\n");
952 eglCreateWindowSurface(ec->base.egl_display,
956 if (egl_surface == EGL_NO_SURFACE) {
957 weston_log("failed to create egl surface\n");
961 ret = drmModeSetCrtc(ec->drm.fd,
963 output->current->fb_id, 0, 0,
964 &output->connector_id, 1, &drm_mode->mode_info);
966 weston_log("failed to set mode\n");
970 /* reset rendering stuff. */
971 if (output->current) {
972 if (output->current->is_client_buffer)
973 gbm_bo_destroy(output->current->bo);
975 gbm_surface_release_buffer(output->surface,
976 output->current->bo);
978 output->current = NULL;
981 if (output->next->is_client_buffer)
982 gbm_bo_destroy(output->next->bo);
984 gbm_surface_release_buffer(output->surface,
989 eglDestroySurface(ec->base.egl_display, output->egl_surface);
990 gbm_surface_destroy(output->surface);
991 output->egl_surface = egl_surface;
992 output->surface = surface;
995 output->base.current = &drm_mode->base;
996 output->base.dirty = 1;
997 weston_output_move(&output->base, output->base.x, output->base.y);
1001 eglDestroySurface(ec->base.egl_display, egl_surface);
1002 gbm_surface_destroy(surface);
1007 on_drm_input(int fd, uint32_t mask, void *data)
1009 drmEventContext evctx;
1011 memset(&evctx, 0, sizeof evctx);
1012 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1013 evctx.page_flip_handler = page_flip_handler;
1014 evctx.vblank_handler = vblank_handler;
1015 drmHandleEvent(fd, &evctx);
1021 init_egl(struct drm_compositor *ec, struct udev_device *device)
1023 EGLint major, minor, n;
1024 const char *filename, *sysnum;
1026 static const EGLint context_attribs[] = {
1027 EGL_CONTEXT_CLIENT_VERSION, 2,
1031 static const EGLint config_attribs[] = {
1032 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
1037 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
1041 sysnum = udev_device_get_sysnum(device);
1043 ec->drm.id = atoi(sysnum);
1044 if (!sysnum || ec->drm.id < 0) {
1045 weston_log("cannot get device sysnum\n");
1049 filename = udev_device_get_devnode(device);
1050 fd = open(filename, O_RDWR | O_CLOEXEC);
1052 /* Probably permissions error */
1053 weston_log("couldn't open %s, skipping\n",
1054 udev_device_get_devnode(device));
1058 weston_log("using %s\n", filename);
1061 ec->gbm = gbm_create_device(ec->drm.fd);
1062 ec->base.egl_display = eglGetDisplay(ec->gbm);
1063 if (ec->base.egl_display == NULL) {
1064 weston_log("failed to create display\n");
1068 if (!eglInitialize(ec->base.egl_display, &major, &minor)) {
1069 weston_log("failed to initialize display\n");
1073 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
1074 weston_log("failed to bind api EGL_OPENGL_ES_API\n");
1078 if (!eglChooseConfig(ec->base.egl_display, config_attribs,
1079 &ec->base.egl_config, 1, &n) || n != 1) {
1080 weston_log("failed to choose config: %d\n", n);
1084 ec->base.egl_context =
1085 eglCreateContext(ec->base.egl_display, ec->base.egl_config,
1086 EGL_NO_CONTEXT, context_attribs);
1087 if (ec->base.egl_context == NULL) {
1088 weston_log("failed to create context\n");
1092 ec->dummy_surface = gbm_surface_create(ec->gbm, 10, 10,
1093 GBM_FORMAT_XRGB8888,
1094 GBM_BO_USE_RENDERING);
1095 if (!ec->dummy_surface) {
1096 weston_log("failed to create dummy gbm surface\n");
1100 ec->dummy_egl_surface =
1101 eglCreateWindowSurface(ec->base.egl_display,
1102 ec->base.egl_config,
1105 if (ec->dummy_egl_surface == EGL_NO_SURFACE) {
1106 weston_log("failed to create egl surface\n");
1110 if (!eglMakeCurrent(ec->base.egl_display, ec->dummy_egl_surface,
1111 ec->dummy_egl_surface, ec->base.egl_context)) {
1112 weston_log("failed to make context current\n");
1120 drm_output_add_mode(struct drm_output *output, drmModeModeInfo *info)
1122 struct drm_mode *mode;
1125 mode = malloc(sizeof *mode);
1129 mode->base.flags = 0;
1130 mode->base.width = info->hdisplay;
1131 mode->base.height = info->vdisplay;
1133 /* Calculate higher precision (mHz) refresh rate */
1134 refresh = (info->clock * 1000000LL / info->htotal +
1135 info->vtotal / 2) / info->vtotal;
1137 if (info->flags & DRM_MODE_FLAG_INTERLACE)
1139 if (info->flags & DRM_MODE_FLAG_DBLSCAN)
1141 if (info->vscan > 1)
1142 refresh /= info->vscan;
1144 mode->base.refresh = refresh;
1145 mode->mode_info = *info;
1147 if (info->type & DRM_MODE_TYPE_PREFERRED)
1148 mode->base.flags |= WL_OUTPUT_MODE_PREFERRED;
1150 wl_list_insert(output->base.mode_list.prev, &mode->base.link);
1156 drm_subpixel_to_wayland(int drm_value)
1158 switch (drm_value) {
1160 case DRM_MODE_SUBPIXEL_UNKNOWN:
1161 return WL_OUTPUT_SUBPIXEL_UNKNOWN;
1162 case DRM_MODE_SUBPIXEL_NONE:
1163 return WL_OUTPUT_SUBPIXEL_NONE;
1164 case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB:
1165 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB;
1166 case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR:
1167 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR;
1168 case DRM_MODE_SUBPIXEL_VERTICAL_RGB:
1169 return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB;
1170 case DRM_MODE_SUBPIXEL_VERTICAL_BGR:
1171 return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR;
1176 sprite_handle_buffer_destroy(struct wl_listener *listener, void *data)
1178 struct drm_sprite *sprite =
1179 container_of(listener, struct drm_sprite,
1181 struct drm_compositor *compositor = sprite->compositor;
1183 sprite->surface = NULL;
1184 drmModeRmFB(compositor->drm.fd, sprite->fb_id);
1189 sprite_handle_pending_buffer_destroy(struct wl_listener *listener, void *data)
1191 struct drm_sprite *sprite =
1192 container_of(listener, struct drm_sprite,
1193 pending_destroy_listener);
1194 struct drm_compositor *compositor = sprite->compositor;
1196 sprite->pending_surface = NULL;
1197 drmModeRmFB(compositor->drm.fd, sprite->pending_fb_id);
1198 sprite->pending_fb_id = 0;
1201 /* returns a value between 0-255 range, where higher is brighter */
1203 drm_get_backlight(struct drm_output *output)
1205 long brightness, max_brightness, norm;
1207 brightness = backlight_get_brightness(output->backlight);
1208 max_brightness = backlight_get_max_brightness(output->backlight);
1210 /* convert it on a scale of 0 to 255 */
1211 norm = (brightness * 255)/(max_brightness);
1213 return (uint32_t) norm;
1216 /* values accepted are between 0-255 range */
1218 drm_set_backlight(struct weston_output *output_base, uint32_t value)
1220 struct drm_output *output = (struct drm_output *) output_base;
1221 long max_brightness, new_brightness;
1223 if (!output->backlight)
1229 max_brightness = backlight_get_max_brightness(output->backlight);
1231 /* get denormalized value */
1232 new_brightness = (value * max_brightness) / 255;
1234 backlight_set_brightness(output->backlight, new_brightness);
1237 static drmModePropertyPtr
1238 drm_get_prop(int fd, drmModeConnectorPtr connector, const char *name)
1240 drmModePropertyPtr props;
1243 for (i = 0; i < connector->count_props; i++) {
1244 props = drmModeGetProperty(fd, connector->props[i]);
1248 if (!strcmp(props->name, name))
1251 drmModeFreeProperty(props);
1258 drm_set_dpms(struct weston_output *output_base, enum dpms_enum level)
1260 struct drm_output *output = (struct drm_output *) output_base;
1261 struct weston_compositor *ec = output_base->compositor;
1262 struct drm_compositor *c = (struct drm_compositor *) ec;
1263 drmModeConnectorPtr connector;
1264 drmModePropertyPtr prop;
1266 connector = drmModeGetConnector(c->drm.fd, output->connector_id);
1270 prop = drm_get_prop(c->drm.fd, connector, "DPMS");
1272 drmModeFreeConnector(connector);
1276 drmModeConnectorSetProperty(c->drm.fd, connector->connector_id,
1277 prop->prop_id, level);
1278 drmModeFreeProperty(prop);
1279 drmModeFreeConnector(connector);
1282 static const char *connector_type_names[] = {
1301 find_crtc_for_connector(struct drm_compositor *ec,
1302 drmModeRes *resources, drmModeConnector *connector)
1304 drmModeEncoder *encoder;
1305 uint32_t possible_crtcs;
1308 for (j = 0; j < connector->count_encoders; j++) {
1309 encoder = drmModeGetEncoder(ec->drm.fd, connector->encoders[j]);
1310 if (encoder == NULL) {
1311 weston_log("Failed to get encoder.\n");
1314 possible_crtcs = encoder->possible_crtcs;
1315 drmModeFreeEncoder(encoder);
1317 for (i = 0; i < resources->count_crtcs; i++) {
1318 if (possible_crtcs & (1 << i) &&
1319 !(ec->crtc_allocator & (1 << resources->crtcs[i])))
1328 create_output_for_connector(struct drm_compositor *ec,
1329 drmModeRes *resources,
1330 drmModeConnector *connector,
1331 int x, int y, struct udev_device *drm_device)
1333 struct drm_output *output;
1334 struct drm_mode *drm_mode, *next;
1335 struct weston_mode *m, *preferred, *current, *configured;
1336 struct drm_configured_output *o = NULL, *temp;
1337 drmModeEncoder *encoder;
1338 drmModeModeInfo crtc_mode;
1342 const char *type_name;
1344 i = find_crtc_for_connector(ec, resources, connector);
1346 weston_log("No usable crtc/encoder pair for connector.\n");
1350 output = malloc(sizeof *output);
1354 memset(output, 0, sizeof *output);
1355 output->base.subpixel = drm_subpixel_to_wayland(connector->subpixel);
1356 output->base.make = "unknown";
1357 output->base.model = "unknown";
1358 wl_list_init(&output->base.mode_list);
1360 if (connector->connector_type < ARRAY_LENGTH(connector_type_names))
1361 type_name = connector_type_names[connector->connector_type];
1363 type_name = "UNKNOWN";
1364 snprintf(name, 32, "%s%d", type_name, connector->connector_type_id);
1365 output->name = strdup(name);
1367 output->crtc_id = resources->crtcs[i];
1368 ec->crtc_allocator |= (1 << output->crtc_id);
1369 output->connector_id = connector->connector_id;
1370 ec->connector_allocator |= (1 << output->connector_id);
1372 output->original_crtc = drmModeGetCrtc(ec->drm.fd, output->crtc_id);
1374 /* Get the current mode on the crtc that's currently driving
1375 * this connector. */
1376 encoder = drmModeGetEncoder(ec->drm.fd, connector->encoder_id);
1377 memset(&crtc_mode, 0, sizeof crtc_mode);
1378 if (encoder != NULL) {
1379 crtc = drmModeGetCrtc(ec->drm.fd, encoder->crtc_id);
1380 drmModeFreeEncoder(encoder);
1383 if (crtc->mode_valid)
1384 crtc_mode = crtc->mode;
1385 drmModeFreeCrtc(crtc);
1388 for (i = 0; i < connector->count_modes; i++) {
1389 ret = drm_output_add_mode(output, &connector->modes[i]);
1398 wl_list_for_each(temp, &configured_output_list, link) {
1399 if (strcmp(temp->name, output->name) == 0) {
1400 weston_log("%s mode \"%s\" in config\n",
1401 temp->name, temp->mode);
1407 if (o && strcmp("off", o->mode) == 0) {
1408 weston_log("Disabling output %s\n", o->name);
1410 drmModeSetCrtc(ec->drm.fd, output->crtc_id,
1411 0, 0, 0, 0, 0, NULL);
1415 wl_list_for_each(drm_mode, &output->base.mode_list, base.link) {
1416 if (o && o->width == drm_mode->base.width &&
1417 o->height == drm_mode->base.height &&
1418 o->config == OUTPUT_CONFIG_MODE)
1419 configured = &drm_mode->base;
1420 if (!memcmp(&crtc_mode, &drm_mode->mode_info, sizeof crtc_mode))
1421 current = &drm_mode->base;
1422 if (drm_mode->base.flags & WL_OUTPUT_MODE_PREFERRED)
1423 preferred = &drm_mode->base;
1426 if (o && o->config == OUTPUT_CONFIG_MODELINE) {
1427 ret = drm_output_add_mode(output, &o->crtc_mode);
1430 configured = container_of(output->base.mode_list.prev,
1431 struct weston_mode, link);
1432 current = configured;
1435 if (current == NULL && crtc_mode.clock != 0) {
1436 ret = drm_output_add_mode(output, &crtc_mode);
1439 current = container_of(output->base.mode_list.prev,
1440 struct weston_mode, link);
1443 if (o && o->config == OUTPUT_CONFIG_CURRENT)
1444 configured = current;
1446 if (option_current_mode && current)
1447 output->base.current = current;
1448 else if (configured)
1449 output->base.current = configured;
1451 output->base.current = preferred;
1453 output->base.current = current;
1455 if (output->base.current == NULL) {
1456 weston_log("no available modes for %s\n", output->name);
1460 output->base.current->flags |= WL_OUTPUT_MODE_CURRENT;
1462 output->surface = gbm_surface_create(ec->gbm,
1463 output->base.current->width,
1464 output->base.current->height,
1465 GBM_FORMAT_XRGB8888,
1466 GBM_BO_USE_SCANOUT |
1467 GBM_BO_USE_RENDERING);
1468 if (!output->surface) {
1469 weston_log("failed to create gbm surface\n");
1473 output->egl_surface =
1474 eglCreateWindowSurface(ec->base.egl_display,
1475 ec->base.egl_config,
1478 if (output->egl_surface == EGL_NO_SURFACE) {
1479 weston_log("failed to create egl surface\n");
1483 output->cursor_bo[0] =
1484 gbm_bo_create(ec->gbm, 64, 64, GBM_FORMAT_ARGB8888,
1485 GBM_BO_USE_CURSOR_64X64 | GBM_BO_USE_WRITE);
1486 output->cursor_bo[1] =
1487 gbm_bo_create(ec->gbm, 64, 64, GBM_FORMAT_ARGB8888,
1488 GBM_BO_USE_CURSOR_64X64 | GBM_BO_USE_WRITE);
1490 output->backlight = backlight_init(drm_device,
1491 connector->connector_type);
1492 if (output->backlight) {
1493 output->base.set_backlight = drm_set_backlight;
1494 output->base.backlight_current = drm_get_backlight(output);
1497 weston_output_init(&output->base, &ec->base, x, y,
1498 connector->mmWidth, connector->mmHeight,
1501 wl_list_insert(ec->base.output_list.prev, &output->base.link);
1503 output->base.origin = output->base.current;
1504 output->base.repaint = drm_output_repaint;
1505 output->base.destroy = drm_output_destroy;
1506 output->base.assign_planes = drm_assign_planes;
1507 output->base.set_dpms = drm_set_dpms;
1508 output->base.switch_mode = drm_output_switch_mode;
1510 weston_plane_init(&output->cursor_plane, 0, 0);
1511 weston_plane_init(&output->fb_plane, 0, 0);
1513 weston_log("Output %s, (connector %d, crtc %d)\n",
1514 output->name, output->connector_id, output->crtc_id);
1515 wl_list_for_each(m, &output->base.mode_list, link)
1516 weston_log_continue(" mode %dx%d@%.1f%s%s%s\n",
1517 m->width, m->height, m->refresh / 1000.0,
1518 m->flags & WL_OUTPUT_MODE_PREFERRED ?
1520 m->flags & WL_OUTPUT_MODE_CURRENT ?
1522 connector->count_modes == 0 ?
1528 gbm_surface_destroy(output->surface);
1530 wl_list_for_each_safe(drm_mode, next, &output->base.mode_list,
1532 wl_list_remove(&drm_mode->base.link);
1536 drmModeFreeCrtc(output->original_crtc);
1537 ec->crtc_allocator &= ~(1 << output->crtc_id);
1538 ec->connector_allocator &= ~(1 << output->connector_id);
1546 create_sprites(struct drm_compositor *ec)
1548 struct drm_sprite *sprite;
1549 drmModePlaneRes *plane_res;
1550 drmModePlane *plane;
1553 plane_res = drmModeGetPlaneResources(ec->drm.fd);
1555 weston_log("failed to get plane resources: %s\n",
1560 for (i = 0; i < plane_res->count_planes; i++) {
1561 plane = drmModeGetPlane(ec->drm.fd, plane_res->planes[i]);
1565 sprite = malloc(sizeof(*sprite) + ((sizeof(uint32_t)) *
1566 plane->count_formats));
1568 weston_log("%s: out of memory\n",
1574 memset(sprite, 0, sizeof *sprite);
1576 sprite->possible_crtcs = plane->possible_crtcs;
1577 sprite->plane_id = plane->plane_id;
1578 sprite->surface = NULL;
1579 sprite->pending_surface = NULL;
1581 sprite->pending_fb_id = 0;
1582 sprite->destroy_listener.notify = sprite_handle_buffer_destroy;
1583 sprite->pending_destroy_listener.notify =
1584 sprite_handle_pending_buffer_destroy;
1585 sprite->compositor = ec;
1586 sprite->count_formats = plane->count_formats;
1587 memcpy(sprite->formats, plane->formats,
1588 plane->count_formats * sizeof(plane->formats[0]));
1589 drmModeFreePlane(plane);
1590 weston_plane_init(&sprite->plane, 0, 0);
1592 wl_list_insert(&ec->sprite_list, &sprite->link);
1595 free(plane_res->planes);
1600 destroy_sprites(struct drm_compositor *compositor)
1602 struct drm_sprite *sprite, *next;
1603 struct drm_output *output;
1605 output = container_of(compositor->base.output_list.next,
1606 struct drm_output, base.link);
1608 wl_list_for_each_safe(sprite, next, &compositor->sprite_list, link) {
1609 drmModeSetPlane(compositor->drm.fd,
1611 output->crtc_id, 0, 0,
1612 0, 0, 0, 0, 0, 0, 0, 0);
1613 drmModeRmFB(compositor->drm.fd, sprite->fb_id);
1614 weston_plane_release(&sprite->plane);
1620 create_outputs(struct drm_compositor *ec, uint32_t option_connector,
1621 struct udev_device *drm_device)
1623 drmModeConnector *connector;
1624 drmModeRes *resources;
1628 resources = drmModeGetResources(ec->drm.fd);
1630 weston_log("drmModeGetResources failed\n");
1634 ec->crtcs = calloc(resources->count_crtcs, sizeof(uint32_t));
1636 drmModeFreeResources(resources);
1640 ec->num_crtcs = resources->count_crtcs;
1641 memcpy(ec->crtcs, resources->crtcs, sizeof(uint32_t) * ec->num_crtcs);
1643 for (i = 0; i < resources->count_connectors; i++) {
1644 connector = drmModeGetConnector(ec->drm.fd,
1645 resources->connectors[i]);
1646 if (connector == NULL)
1649 if (connector->connection == DRM_MODE_CONNECTED &&
1650 (option_connector == 0 ||
1651 connector->connector_id == option_connector)) {
1652 if (create_output_for_connector(ec, resources,
1655 drmModeFreeConnector(connector);
1659 x += container_of(ec->base.output_list.prev,
1660 struct weston_output,
1661 link)->current->width;
1664 drmModeFreeConnector(connector);
1667 if (wl_list_empty(&ec->base.output_list)) {
1668 weston_log("No currently active connector found.\n");
1669 drmModeFreeResources(resources);
1673 drmModeFreeResources(resources);
1679 update_outputs(struct drm_compositor *ec, struct udev_device *drm_device)
1681 drmModeConnector *connector;
1682 drmModeRes *resources;
1683 struct drm_output *output, *next;
1685 int x_offset = 0, y_offset = 0;
1686 uint32_t connected = 0, disconnects = 0;
1689 resources = drmModeGetResources(ec->drm.fd);
1691 weston_log("drmModeGetResources failed\n");
1695 /* collect new connects */
1696 for (i = 0; i < resources->count_connectors; i++) {
1697 int connector_id = resources->connectors[i];
1699 connector = drmModeGetConnector(ec->drm.fd, connector_id);
1700 if (connector == NULL)
1703 if (connector->connection != DRM_MODE_CONNECTED) {
1704 drmModeFreeConnector(connector);
1708 connected |= (1 << connector_id);
1710 if (!(ec->connector_allocator & (1 << connector_id))) {
1711 struct weston_output *last =
1712 container_of(ec->base.output_list.prev,
1713 struct weston_output, link);
1715 /* XXX: not yet needed, we die with 0 outputs */
1716 if (!wl_list_empty(&ec->base.output_list))
1717 x = last->x + last->current->width;
1721 create_output_for_connector(ec, resources,
1724 weston_log("connector %d connected\n", connector_id);
1727 drmModeFreeConnector(connector);
1729 drmModeFreeResources(resources);
1731 disconnects = ec->connector_allocator & ~connected;
1733 wl_list_for_each_safe(output, next, &ec->base.output_list,
1735 if (x_offset != 0 || y_offset != 0) {
1736 weston_output_move(&output->base,
1737 output->base.x - x_offset,
1738 output->base.y - y_offset);
1741 if (disconnects & (1 << output->connector_id)) {
1742 disconnects &= ~(1 << output->connector_id);
1743 weston_log("connector %d disconnected\n",
1744 output->connector_id);
1745 x_offset += output->base.current->width;
1746 drm_output_destroy(&output->base);
1751 /* FIXME: handle zero outputs, without terminating */
1752 if (ec->connector_allocator == 0)
1753 wl_display_terminate(ec->base.wl_display);
1757 udev_event_is_hotplug(struct drm_compositor *ec, struct udev_device *device)
1762 sysnum = udev_device_get_sysnum(device);
1763 if (!sysnum || atoi(sysnum) != ec->drm.id)
1766 val = udev_device_get_property_value(device, "HOTPLUG");
1770 return strcmp(val, "1") == 0;
1774 udev_drm_event(int fd, uint32_t mask, void *data)
1776 struct drm_compositor *ec = data;
1777 struct udev_device *event;
1779 event = udev_monitor_receive_device(ec->udev_monitor);
1781 if (udev_event_is_hotplug(ec, event))
1782 update_outputs(ec, event);
1784 udev_device_unref(event);
1790 drm_restore(struct weston_compositor *ec)
1792 struct drm_compositor *d = (struct drm_compositor *) ec;
1794 if (weston_launcher_drm_set_master(&d->base, d->drm.fd, 0) < 0)
1795 weston_log("failed to drop master: %m\n");
1799 static const char default_seat[] = "seat0";
1802 device_added(struct udev_device *udev_device, struct drm_seat *master)
1804 struct weston_compositor *c;
1805 struct evdev_device *device;
1806 const char *devnode;
1807 const char *device_seat;
1810 device_seat = udev_device_get_property_value(udev_device, "ID_SEAT");
1812 device_seat = default_seat;
1814 if (strcmp(device_seat, master->seat_id))
1817 c = master->base.compositor;
1818 devnode = udev_device_get_devnode(udev_device);
1820 /* Use non-blocking mode so that we can loop on read on
1821 * evdev_device_data() until all events on the fd are
1822 * read. mtdev_get() also expects this. */
1823 fd = weston_launcher_open(c, devnode, O_RDWR | O_NONBLOCK);
1825 weston_log("opening input device '%s' failed.\n", devnode);
1829 device = evdev_device_create(&master->base, devnode, fd);
1832 weston_log("not using input device '%s'.\n", devnode);
1836 wl_list_insert(master->devices_list.prev, &device->link);
1840 evdev_add_devices(struct udev *udev, struct weston_seat *seat_base)
1842 struct drm_seat *seat = (struct drm_seat *) seat_base;
1843 struct udev_enumerate *e;
1844 struct udev_list_entry *entry;
1845 struct udev_device *device;
1846 const char *path, *sysname;
1848 e = udev_enumerate_new(udev);
1849 udev_enumerate_add_match_subsystem(e, "input");
1850 udev_enumerate_scan_devices(e);
1851 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
1852 path = udev_list_entry_get_name(entry);
1853 device = udev_device_new_from_syspath(udev, path);
1855 sysname = udev_device_get_sysname(device);
1856 if (strncmp("event", sysname, 5) != 0) {
1857 udev_device_unref(device);
1861 device_added(device, seat);
1863 udev_device_unref(device);
1865 udev_enumerate_unref(e);
1867 evdev_notify_keyboard_focus(&seat->base, &seat->devices_list);
1869 if (wl_list_empty(&seat->devices_list)) {
1871 "warning: no input devices on entering Weston. "
1872 "Possible causes:\n"
1873 "\t- no permissions to read /dev/input/event*\n"
1874 "\t- seats misconfigured "
1875 "(Weston backend option 'seat', "
1876 "udev device property ID_SEAT)\n");
1881 evdev_udev_handler(int fd, uint32_t mask, void *data)
1883 struct drm_seat *seat = data;
1884 struct udev_device *udev_device;
1885 struct evdev_device *device, *next;
1887 const char *devnode;
1889 udev_device = udev_monitor_receive_device(seat->udev_monitor);
1893 action = udev_device_get_action(udev_device);
1897 if (strncmp("event", udev_device_get_sysname(udev_device), 5) != 0)
1900 if (!strcmp(action, "add")) {
1901 device_added(udev_device, seat);
1903 else if (!strcmp(action, "remove")) {
1904 devnode = udev_device_get_devnode(udev_device);
1905 wl_list_for_each_safe(device, next, &seat->devices_list, link)
1906 if (!strcmp(device->devnode, devnode)) {
1907 weston_log("input device %s, %s removed\n",
1908 device->devname, device->devnode);
1909 evdev_device_destroy(device);
1915 udev_device_unref(udev_device);
1921 evdev_enable_udev_monitor(struct udev *udev, struct weston_seat *seat_base)
1923 struct drm_seat *master = (struct drm_seat *) seat_base;
1924 struct wl_event_loop *loop;
1925 struct weston_compositor *c = master->base.compositor;
1928 master->udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
1929 if (!master->udev_monitor) {
1930 weston_log("udev: failed to create the udev monitor\n");
1934 udev_monitor_filter_add_match_subsystem_devtype(master->udev_monitor,
1937 if (udev_monitor_enable_receiving(master->udev_monitor)) {
1938 weston_log("udev: failed to bind the udev monitor\n");
1939 udev_monitor_unref(master->udev_monitor);
1943 loop = wl_display_get_event_loop(c->wl_display);
1944 fd = udev_monitor_get_fd(master->udev_monitor);
1945 master->udev_monitor_source =
1946 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1947 evdev_udev_handler, master);
1948 if (!master->udev_monitor_source) {
1949 udev_monitor_unref(master->udev_monitor);
1957 evdev_disable_udev_monitor(struct weston_seat *seat_base)
1959 struct drm_seat *seat = (struct drm_seat *) seat_base;
1961 if (!seat->udev_monitor)
1964 udev_monitor_unref(seat->udev_monitor);
1965 seat->udev_monitor = NULL;
1966 wl_event_source_remove(seat->udev_monitor_source);
1967 seat->udev_monitor_source = NULL;
1971 drm_led_update(struct weston_seat *seat_base, enum weston_led leds)
1973 struct drm_seat *seat = (struct drm_seat *) seat_base;
1974 struct evdev_device *device;
1976 wl_list_for_each(device, &seat->devices_list, link)
1977 evdev_led_update(device, leds);
1981 evdev_input_create(struct weston_compositor *c, struct udev *udev,
1982 const char *seat_id)
1984 struct drm_seat *seat;
1986 seat = malloc(sizeof *seat);
1990 memset(seat, 0, sizeof *seat);
1991 weston_seat_init(&seat->base, c);
1992 seat->base.led_update = drm_led_update;
1994 wl_list_init(&seat->devices_list);
1995 seat->seat_id = strdup(seat_id);
1996 if (!evdev_enable_udev_monitor(udev, &seat->base)) {
1997 free(seat->seat_id);
2002 evdev_add_devices(udev, &seat->base);
2004 c->seat = &seat->base;
2008 evdev_remove_devices(struct weston_seat *seat_base)
2010 struct drm_seat *seat = (struct drm_seat *) seat_base;
2011 struct evdev_device *device, *next;
2013 wl_list_for_each_safe(device, next, &seat->devices_list, link)
2014 evdev_device_destroy(device);
2016 if (seat->base.seat.keyboard)
2017 notify_keyboard_focus_out(&seat->base.seat);
2021 evdev_input_destroy(struct weston_seat *seat_base)
2023 struct drm_seat *seat = (struct drm_seat *) seat_base;
2025 evdev_remove_devices(seat_base);
2026 evdev_disable_udev_monitor(&seat->base);
2028 weston_seat_release(seat_base);
2029 free(seat->seat_id);
2034 drm_free_configured_output(struct drm_configured_output *output)
2042 drm_destroy(struct weston_compositor *ec)
2044 struct drm_compositor *d = (struct drm_compositor *) ec;
2045 struct weston_seat *seat, *next;
2046 struct drm_configured_output *o, *n;
2048 wl_list_for_each_safe(seat, next, &ec->seat_list, link)
2049 evdev_input_destroy(seat);
2050 wl_list_for_each_safe(o, n, &configured_output_list, link)
2051 drm_free_configured_output(o);
2053 wl_event_source_remove(d->udev_drm_source);
2054 wl_event_source_remove(d->drm_source);
2056 weston_compositor_shutdown(ec);
2058 /* Work around crash in egl_dri2.c's dri2_make_current() */
2059 eglMakeCurrent(ec->egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
2061 eglTerminate(ec->egl_display);
2064 gbm_device_destroy(d->gbm);
2066 if (weston_launcher_drm_set_master(&d->base, d->drm.fd, 0) < 0)
2067 weston_log("failed to drop master: %m\n");
2068 tty_destroy(d->tty);
2074 drm_compositor_set_modes(struct drm_compositor *compositor)
2076 struct drm_output *output;
2077 struct drm_mode *drm_mode;
2080 wl_list_for_each(output, &compositor->base.output_list, base.link) {
2081 drm_mode = (struct drm_mode *) output->base.current;
2082 ret = drmModeSetCrtc(compositor->drm.fd, output->crtc_id,
2083 output->current->fb_id, 0, 0,
2084 &output->connector_id, 1,
2085 &drm_mode->mode_info);
2088 "failed to set mode %dx%d for output at %d,%d: %m\n",
2089 drm_mode->base.width, drm_mode->base.height,
2090 output->base.x, output->base.y);
2096 vt_func(struct weston_compositor *compositor, int event)
2098 struct drm_compositor *ec = (struct drm_compositor *) compositor;
2099 struct weston_seat *seat;
2100 struct drm_sprite *sprite;
2101 struct drm_output *output;
2105 weston_log("entering VT\n");
2106 compositor->focus = 1;
2107 if (weston_launcher_drm_set_master(&ec->base, ec->drm.fd, 1)) {
2108 weston_log("failed to set master: %m\n");
2109 wl_display_terminate(compositor->wl_display);
2111 compositor->state = ec->prev_state;
2112 drm_compositor_set_modes(ec);
2113 weston_compositor_damage_all(compositor);
2114 wl_list_for_each(seat, &compositor->seat_list, link) {
2115 evdev_add_devices(ec->udev, seat);
2116 evdev_enable_udev_monitor(ec->udev, seat);
2120 weston_log("leaving VT\n");
2121 wl_list_for_each(seat, &compositor->seat_list, link) {
2122 evdev_disable_udev_monitor(seat);
2123 evdev_remove_devices(seat);
2126 compositor->focus = 0;
2127 ec->prev_state = compositor->state;
2128 compositor->state = WESTON_COMPOSITOR_SLEEPING;
2130 /* If we have a repaint scheduled (either from a
2131 * pending pageflip or the idle handler), make sure we
2132 * cancel that so we don't try to pageflip when we're
2133 * vt switched away. The SLEEPING state will prevent
2134 * further attemps at repainting. When we switch
2135 * back, we schedule a repaint, which will process
2136 * pending frame callbacks. */
2138 wl_list_for_each(output, &ec->base.output_list, base.link) {
2139 output->base.repaint_needed = 0;
2140 drmModeSetCursor(ec->drm.fd, output->crtc_id, 0, 0, 0);
2143 output = container_of(ec->base.output_list.next,
2144 struct drm_output, base.link);
2146 wl_list_for_each(sprite, &ec->sprite_list, link)
2147 drmModeSetPlane(ec->drm.fd,
2149 output->crtc_id, 0, 0,
2150 0, 0, 0, 0, 0, 0, 0, 0);
2152 if (weston_launcher_drm_set_master(&ec->base, ec->drm.fd, 0) < 0)
2153 weston_log("failed to drop master: %m\n");
2160 switch_vt_binding(struct wl_seat *seat, uint32_t time, uint32_t key, void *data)
2162 struct drm_compositor *ec = data;
2164 tty_activate_vt(ec->tty, key - KEY_F1 + 1);
2167 static struct weston_compositor *
2168 drm_compositor_create(struct wl_display *display,
2169 int connector, const char *seat, int tty,
2170 int argc, char *argv[], const char *config_file)
2172 struct drm_compositor *ec;
2173 struct udev_enumerate *e;
2174 struct udev_list_entry *entry;
2175 struct udev_device *device, *drm_device;
2176 const char *path, *device_seat;
2177 struct wl_event_loop *loop;
2178 struct weston_seat *weston_seat, *next;
2181 weston_log("initializing drm backend\n");
2183 ec = malloc(sizeof *ec);
2186 memset(ec, 0, sizeof *ec);
2188 if (weston_compositor_init(&ec->base, display, argc, argv,
2190 weston_log("weston_compositor_init failed\n");
2194 ec->udev = udev_new();
2195 if (ec->udev == NULL) {
2196 weston_log("failed to initialize udev context\n");
2197 goto err_compositor;
2200 ec->base.wl_display = display;
2201 ec->tty = tty_create(&ec->base, vt_func, tty);
2203 weston_log("failed to initialize tty\n");
2207 e = udev_enumerate_new(ec->udev);
2208 udev_enumerate_add_match_subsystem(e, "drm");
2209 udev_enumerate_add_match_sysname(e, "card[0-9]*");
2211 udev_enumerate_scan_devices(e);
2213 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
2214 path = udev_list_entry_get_name(entry);
2215 device = udev_device_new_from_syspath(ec->udev, path);
2217 udev_device_get_property_value(device, "ID_SEAT");
2219 device_seat = default_seat;
2220 if (strcmp(device_seat, seat) == 0) {
2221 drm_device = device;
2224 udev_device_unref(device);
2227 if (drm_device == NULL) {
2228 weston_log("no drm device found\n");
2232 if (init_egl(ec, drm_device) < 0) {
2233 weston_log("failed to initialize egl\n");
2237 ec->base.destroy = drm_destroy;
2238 ec->base.restore = drm_restore;
2242 ec->prev_state = WESTON_COMPOSITOR_ACTIVE;
2244 if (weston_compositor_init_gl(&ec->base) < 0)
2247 for (key = KEY_F1; key < KEY_F9; key++)
2248 weston_compositor_add_key_binding(&ec->base, key,
2249 MODIFIER_CTRL | MODIFIER_ALT,
2250 switch_vt_binding, ec);
2252 wl_list_init(&ec->sprite_list);
2255 if (create_outputs(ec, connector, drm_device) < 0) {
2256 weston_log("failed to create output for %s\n", path);
2262 evdev_input_create(&ec->base, ec->udev, seat);
2264 loop = wl_display_get_event_loop(ec->base.wl_display);
2266 wl_event_loop_add_fd(loop, ec->drm.fd,
2267 WL_EVENT_READABLE, on_drm_input, ec);
2269 ec->udev_monitor = udev_monitor_new_from_netlink(ec->udev, "udev");
2270 if (ec->udev_monitor == NULL) {
2271 weston_log("failed to intialize udev monitor\n");
2272 goto err_drm_source;
2274 udev_monitor_filter_add_match_subsystem_devtype(ec->udev_monitor,
2276 ec->udev_drm_source =
2277 wl_event_loop_add_fd(loop,
2278 udev_monitor_get_fd(ec->udev_monitor),
2279 WL_EVENT_READABLE, udev_drm_event, ec);
2281 if (udev_monitor_enable_receiving(ec->udev_monitor) < 0) {
2282 weston_log("failed to enable udev-monitor receiving\n");
2283 goto err_udev_monitor;
2286 udev_device_unref(drm_device);
2287 udev_enumerate_unref(e);
2292 wl_event_source_remove(ec->udev_drm_source);
2293 udev_monitor_unref(ec->udev_monitor);
2295 wl_event_source_remove(ec->drm_source);
2296 wl_list_for_each_safe(weston_seat, next, &ec->base.seat_list, link)
2297 evdev_input_destroy(weston_seat);
2299 destroy_sprites(ec);
2301 eglMakeCurrent(ec->base.egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
2303 eglTerminate(ec->base.egl_display);
2305 gbm_device_destroy(ec->gbm);
2307 udev_device_unref(drm_device);
2309 udev_enumerate_unref(e);
2310 tty_destroy(ec->tty);
2312 udev_unref(ec->udev);
2314 weston_compositor_shutdown(&ec->base);
2321 set_sync_flags(drmModeModeInfo *mode, char *hsync, char *vsync)
2325 if (strcmp(hsync, "+hsync") == 0)
2326 mode->flags |= DRM_MODE_FLAG_PHSYNC;
2327 else if (strcmp(hsync, "-hsync") == 0)
2328 mode->flags |= DRM_MODE_FLAG_NHSYNC;
2332 if (strcmp(vsync, "+vsync") == 0)
2333 mode->flags |= DRM_MODE_FLAG_PVSYNC;
2334 else if (strcmp(vsync, "-vsync") == 0)
2335 mode->flags |= DRM_MODE_FLAG_NVSYNC;
2343 check_for_modeline(struct drm_configured_output *output)
2345 drmModeModeInfo mode;
2351 mode.type = DRM_MODE_TYPE_USERDEF;
2356 if (sscanf(output_mode, "%f %hd %hd %hd %hd %hd %hd %hd %hd %s %s",
2357 &fclock, &mode.hdisplay,
2359 &mode.hsync_end, &mode.htotal,
2362 &mode.vsync_end, &mode.vtotal,
2363 hsync, vsync) == 11) {
2364 if (set_sync_flags(&mode, hsync, vsync))
2367 sprintf(mode_name, "%dx%d", mode.hdisplay, mode.vdisplay);
2368 strcpy(mode.name, mode_name);
2370 mode.clock = fclock * 1000;
2374 output->crtc_mode = mode;
2380 output_section_done(void *data)
2382 struct drm_configured_output *output;
2384 output = malloc(sizeof *output);
2386 if (!output || !output_name || !output_mode) {
2394 output->config = OUTPUT_CONFIG_INVALID;
2395 output->name = output_name;
2396 output->mode = output_mode;
2398 if (strcmp(output_mode, "off") == 0)
2399 output->config = OUTPUT_CONFIG_OFF;
2400 else if (strcmp(output_mode, "preferred") == 0)
2401 output->config = OUTPUT_CONFIG_PREFERRED;
2402 else if (strcmp(output_mode, "current") == 0)
2403 output->config = OUTPUT_CONFIG_CURRENT;
2404 else if (sscanf(output_mode, "%dx%d", &output->width, &output->height) == 2)
2405 output->config = OUTPUT_CONFIG_MODE;
2406 else if (check_for_modeline(output) == 0)
2407 output->config = OUTPUT_CONFIG_MODELINE;
2409 if (output->config != OUTPUT_CONFIG_INVALID)
2410 wl_list_insert(&configured_output_list, &output->link);
2412 weston_log("Invalid mode \"%s\" for output %s\n",
2413 output_mode, output_name);
2414 drm_free_configured_output(output);
2418 WL_EXPORT struct weston_compositor *
2419 backend_init(struct wl_display *display, int argc, char *argv[],
2420 const char *config_file)
2422 int connector = 0, tty = 0;
2423 const char *seat = default_seat;
2425 const struct weston_option drm_options[] = {
2426 { WESTON_OPTION_INTEGER, "connector", 0, &connector },
2427 { WESTON_OPTION_STRING, "seat", 0, &seat },
2428 { WESTON_OPTION_INTEGER, "tty", 0, &tty },
2429 { WESTON_OPTION_BOOLEAN, "current-mode", 0, &option_current_mode },
2432 parse_options(drm_options, ARRAY_LENGTH(drm_options), argc, argv);
2434 wl_list_init(&configured_output_list);
2436 const struct config_key drm_config_keys[] = {
2437 { "name", CONFIG_KEY_STRING, &output_name },
2438 { "mode", CONFIG_KEY_STRING, &output_mode },
2441 const struct config_section config_section[] = {
2442 { "output", drm_config_keys,
2443 ARRAY_LENGTH(drm_config_keys), output_section_done },
2446 parse_config_file(config_file, config_section,
2447 ARRAY_LENGTH(config_section), NULL);
2449 return drm_compositor_create(display, connector, seat, tty, argc, argv,