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 char *output_transform;
50 static struct wl_list configured_output_list;
53 OUTPUT_CONFIG_INVALID = 0,
55 OUTPUT_CONFIG_PREFERRED,
56 OUTPUT_CONFIG_CURRENT,
58 OUTPUT_CONFIG_MODELINE
61 struct drm_configured_output {
65 int32_t width, height;
66 drmModeModeInfo crtc_mode;
67 enum output_config config;
71 struct drm_compositor {
72 struct weston_compositor base;
75 struct wl_event_source *drm_source;
77 struct udev_monitor *udev_monitor;
78 struct wl_event_source *udev_drm_source;
84 struct gbm_device *gbm;
87 uint32_t crtc_allocator;
88 uint32_t connector_allocator;
91 struct gbm_surface *dummy_surface;
92 EGLSurface dummy_egl_surface;
94 /* we need these parameters in order to not fail drmModeAddFB2()
95 * due to out of bounds dimensions, and then mistakenly set
98 int32_t min_width, max_width;
99 int32_t min_height, max_height;
101 struct wl_list sprite_list;
102 int sprites_are_broken;
104 int cursors_are_broken;
110 struct weston_mode base;
111 drmModeModeInfo mode_info;
118 struct drm_output *output;
120 int is_client_buffer;
121 struct wl_buffer *buffer;
122 struct wl_listener buffer_destroy_listener;
126 struct weston_output base;
131 uint32_t connector_id;
132 drmModeCrtcPtr original_crtc;
135 int page_flip_pending;
137 struct gbm_surface *surface;
138 struct gbm_bo *cursor_bo[2];
139 struct weston_plane cursor_plane;
140 struct weston_plane fb_plane;
141 struct weston_surface *cursor_surface;
143 struct drm_fb *current, *next;
144 struct backlight *backlight;
148 * An output has a primary display plane plus zero or more sprites for
149 * blending display contents.
155 uint32_t pending_fb_id;
156 struct weston_surface *surface;
157 struct weston_surface *pending_surface;
158 struct weston_plane plane;
160 struct drm_output *output;
162 struct drm_compositor *compositor;
164 struct wl_listener destroy_listener;
165 struct wl_listener pending_destroy_listener;
167 uint32_t possible_crtcs;
169 uint32_t count_formats;
171 int32_t src_x, src_y;
172 uint32_t src_w, src_h;
173 uint32_t dest_x, dest_y;
174 uint32_t dest_w, dest_h;
180 struct weston_seat base;
181 struct wl_list devices_list;
182 struct udev_monitor *udev_monitor;
183 struct wl_event_source *udev_monitor_source;
188 drm_output_set_cursor(struct drm_output *output);
190 drm_disable_unused_sprites(struct weston_output *output_base);
193 drm_sprite_crtc_supported(struct weston_output *output_base, uint32_t supported)
195 struct weston_compositor *ec = output_base->compositor;
196 struct drm_compositor *c =(struct drm_compositor *) ec;
197 struct drm_output *output = (struct drm_output *) output_base;
200 for (crtc = 0; crtc < c->num_crtcs; crtc++) {
201 if (c->crtcs[crtc] != output->crtc_id)
204 if (supported & (1 << crtc))
212 drm_fb_destroy_callback(struct gbm_bo *bo, void *data)
214 struct drm_fb *fb = data;
215 struct gbm_device *gbm = gbm_bo_get_device(bo);
218 drmModeRmFB(gbm_device_get_fd(gbm), fb->fb_id);
221 weston_buffer_post_release(fb->buffer);
222 wl_list_remove(&fb->buffer_destroy_listener.link);
228 static struct drm_fb *
229 drm_fb_get_from_bo(struct gbm_bo *bo, struct drm_output *output)
231 struct drm_fb *fb = gbm_bo_get_user_data(bo);
232 struct drm_compositor *compositor =
233 (struct drm_compositor *) output->base.compositor;
234 uint32_t width, height, stride, handle;
240 fb = malloc(sizeof *fb);
244 fb->is_client_buffer = 0;
247 width = gbm_bo_get_width(bo);
248 height = gbm_bo_get_height(bo);
249 stride = gbm_bo_get_stride(bo);
250 handle = gbm_bo_get_handle(bo).u32;
252 ret = drmModeAddFB(compositor->drm.fd, width, height, 24, 32,
253 stride, handle, &fb->fb_id);
255 weston_log("failed to create kms fb: %m\n");
260 gbm_bo_set_user_data(bo, fb, drm_fb_destroy_callback);
266 fb_handle_buffer_destroy(struct wl_listener *listener, void *data)
268 struct drm_fb *fb = container_of(listener, struct drm_fb,
269 buffer_destroy_listener);
273 if (fb == fb->output->next ||
274 (fb == fb->output->current && !fb->output->next))
275 weston_output_schedule_repaint(&fb->output->base);
278 static struct weston_plane *
279 drm_output_prepare_scanout_surface(struct weston_output *_output,
280 struct weston_surface *es)
282 struct drm_output *output = (struct drm_output *) _output;
283 struct drm_compositor *c =
284 (struct drm_compositor *) output->base.compositor;
287 if (es->geometry.x != output->base.x ||
288 es->geometry.y != output->base.y ||
289 es->geometry.width != output->base.current->width ||
290 es->geometry.height != output->base.current->height ||
291 es->transform.enabled ||
295 bo = gbm_bo_import(c->gbm, GBM_BO_IMPORT_WL_BUFFER,
296 es->buffer, GBM_BO_USE_SCANOUT);
298 /* Need to verify output->region contained in surface opaque
299 * region. Or maybe just that format doesn't have alpha.
300 * For now, scanout only if format is XRGB8888. */
301 if (gbm_bo_get_format(bo) != GBM_FORMAT_XRGB8888) {
306 output->next = drm_fb_get_from_bo(bo, output);
312 output->next->is_client_buffer = 1;
313 output->next->buffer = es->buffer;
314 output->next->buffer->busy_count++;
315 output->next->buffer_destroy_listener.notify = fb_handle_buffer_destroy;
317 wl_signal_add(&output->next->buffer->resource.destroy_signal,
318 &output->next->buffer_destroy_listener);
320 return &output->fb_plane;
324 drm_output_render(struct drm_output *output, pixman_region32_t *damage)
328 gles2_renderer_repaint_output(&output->base, damage);
330 bo = gbm_surface_lock_front_buffer(output->surface);
332 weston_log("failed to lock front buffer: %m\n");
336 output->next = drm_fb_get_from_bo(bo, output);
338 weston_log("failed to get drm_fb for bo\n");
339 gbm_surface_release_buffer(output->surface, bo);
345 drm_output_repaint(struct weston_output *output_base,
346 pixman_region32_t *damage)
348 struct drm_output *output = (struct drm_output *) output_base;
349 struct drm_compositor *compositor =
350 (struct drm_compositor *) output->base.compositor;
351 struct drm_sprite *s;
352 struct drm_mode *mode;
356 drm_output_render(output, damage);
360 mode = container_of(output->base.current, struct drm_mode, base);
361 if (!output->current) {
362 ret = drmModeSetCrtc(compositor->drm.fd, output->crtc_id,
363 output->next->fb_id, 0, 0,
364 &output->connector_id, 1,
367 weston_log("set mode failed: %m\n");
372 if (drmModePageFlip(compositor->drm.fd, output->crtc_id,
374 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
375 weston_log("queueing pageflip failed: %m\n");
379 output->page_flip_pending = 1;
381 drm_output_set_cursor(output);
384 * Now, update all the sprite surfaces
386 wl_list_for_each(s, &compositor->sprite_list, link) {
389 .request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT,
390 .request.sequence = 1,
393 if (!drm_sprite_crtc_supported(output_base, s->possible_crtcs))
396 ret = drmModeSetPlane(compositor->drm.fd, s->plane_id,
397 output->crtc_id, s->pending_fb_id, flags,
398 s->dest_x, s->dest_y,
399 s->dest_w, s->dest_h,
403 weston_log("setplane failed: %d: %s\n",
404 ret, strerror(errno));
406 if (output->pipe > 0)
407 vbl.request.type |= DRM_VBLANK_SECONDARY;
410 * Queue a vblank signal so we know when the surface
411 * becomes active on the display or has been replaced.
413 vbl.request.signal = (unsigned long)s;
414 ret = drmWaitVBlank(compositor->drm.fd, &vbl);
416 weston_log("vblank event request failed: %d: %s\n",
417 ret, strerror(errno));
421 output->vblank_pending = 1;
424 drm_disable_unused_sprites(&output->base);
430 vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec,
433 struct drm_sprite *s = (struct drm_sprite *)data;
434 struct drm_compositor *c = s->compositor;
435 struct drm_output *output = s->output;
438 output->vblank_pending = 0;
441 weston_buffer_post_release(s->surface->buffer);
442 wl_list_remove(&s->destroy_listener.link);
444 drmModeRmFB(c->drm.fd, s->fb_id);
448 if (s->pending_surface) {
449 wl_list_remove(&s->pending_destroy_listener.link);
450 wl_signal_add(&s->pending_surface->buffer->resource.destroy_signal,
451 &s->destroy_listener);
452 s->surface = s->pending_surface;
453 s->pending_surface = NULL;
454 s->fb_id = s->pending_fb_id;
455 s->pending_fb_id = 0;
458 if (!output->page_flip_pending) {
459 msecs = sec * 1000 + usec / 1000;
460 weston_output_finish_frame(&output->base, msecs);
465 page_flip_handler(int fd, unsigned int frame,
466 unsigned int sec, unsigned int usec, void *data)
468 struct drm_output *output = (struct drm_output *) data;
471 output->page_flip_pending = 0;
473 if (output->current) {
474 if (output->current->is_client_buffer)
475 gbm_bo_destroy(output->current->bo);
477 gbm_surface_release_buffer(output->surface,
478 output->current->bo);
481 output->current = output->next;
484 if (!output->vblank_pending) {
485 msecs = sec * 1000 + usec / 1000;
486 weston_output_finish_frame(&output->base, msecs);
491 drm_surface_format_supported(struct drm_sprite *s, uint32_t format)
495 for (i = 0; i < s->count_formats; i++)
496 if (s->formats[i] == format)
503 drm_surface_transform_supported(struct weston_surface *es)
505 struct weston_matrix *matrix = &es->transform.matrix;
508 if (!es->transform.enabled)
511 for (i = 0; i < 16; i++) {
515 if (matrix->d[i] != 1.0)
524 if (matrix->d[i] != 0.0)
534 drm_disable_unused_sprites(struct weston_output *output_base)
536 struct weston_compositor *ec = output_base->compositor;
537 struct drm_compositor *c =(struct drm_compositor *) ec;
538 struct drm_output *output = (struct drm_output *) output_base;
539 struct drm_sprite *s;
542 wl_list_for_each(s, &c->sprite_list, link) {
543 if (s->pending_fb_id)
546 ret = drmModeSetPlane(c->drm.fd, s->plane_id,
547 output->crtc_id, 0, 0,
548 0, 0, 0, 0, 0, 0, 0, 0);
550 weston_log("failed to disable plane: %d: %s\n",
551 ret, strerror(errno));
552 drmModeRmFB(c->drm.fd, s->fb_id);
556 wl_list_remove(&s->destroy_listener.link);
559 assert(!s->pending_surface);
561 s->pending_fb_id = 0;
566 * This function must take care to damage any previously assigned surface
567 * if the sprite ends up binding to a different surface than in the
570 static struct weston_plane *
571 drm_output_prepare_overlay_surface(struct weston_output *output_base,
572 struct weston_surface *es)
574 struct weston_compositor *ec = output_base->compositor;
575 struct drm_compositor *c =(struct drm_compositor *) ec;
576 struct drm_sprite *s;
578 EGLint handle, stride;
581 uint32_t handles[4], pitches[4], offsets[4];
583 pixman_region32_t dest_rect, src_rect;
586 wl_fixed_t sx1, sy1, sx2, sy2;
587 int32_t width, height;
589 if (c->sprites_are_broken)
592 if (es->output_mask != (1u << output_base->id))
595 if (es->buffer == NULL)
598 if (wl_buffer_is_shm(es->buffer))
601 if (!drm_surface_transform_supported(es))
604 wl_list_for_each(s, &c->sprite_list, link) {
605 if (!drm_sprite_crtc_supported(output_base, s->possible_crtcs))
608 if (!s->pending_fb_id) {
614 /* No sprites available */
618 width = es->geometry.width;
619 height = es->geometry.height;
621 /* If geometry is out of bounds, don't even bother trying because
622 * we know the AddFB2() call will fail:
624 if (c->min_width > width || width > c->max_width ||
625 c->min_height > height || height > c->max_height)
628 bo = gbm_bo_import(c->gbm, GBM_BO_IMPORT_WL_BUFFER,
629 es->buffer, GBM_BO_USE_SCANOUT);
633 format = gbm_bo_get_format(bo);
634 handle = gbm_bo_get_handle(bo).s32;
635 stride = gbm_bo_get_stride(bo);
639 if (!drm_surface_format_supported(s, format))
649 ret = drmModeAddFB2(c->drm.fd, width, height,
650 format, handles, pitches, offsets,
653 weston_log("addfb2 failed: %d\n", ret);
654 c->sprites_are_broken = 1;
658 s->pending_fb_id = fb_id;
659 s->pending_surface = es;
660 es->buffer->busy_count++;
662 box = pixman_region32_extents(&es->transform.boundingbox);
663 s->plane.x = box->x1;
664 s->plane.y = box->y1;
667 * Calculate the source & dest rects properly based on actual
668 * postion (note the caller has called weston_surface_update_transform()
671 pixman_region32_init(&dest_rect);
672 pixman_region32_intersect(&dest_rect, &es->transform.boundingbox,
673 &output_base->region);
674 pixman_region32_translate(&dest_rect, -output_base->x, -output_base->y);
675 box = pixman_region32_extents(&dest_rect);
678 s->dest_w = box->x2 - box->x1;
679 s->dest_h = box->y2 - box->y1;
680 pixman_region32_fini(&dest_rect);
682 pixman_region32_init(&src_rect);
683 pixman_region32_intersect(&src_rect, &es->transform.boundingbox,
684 &output_base->region);
685 box = pixman_region32_extents(&src_rect);
687 weston_surface_from_global_fixed(es,
688 wl_fixed_from_int(box->x1),
689 wl_fixed_from_int(box->y1),
691 weston_surface_from_global_fixed(es,
692 wl_fixed_from_int(box->x2),
693 wl_fixed_from_int(box->y2),
700 if (sx2 > wl_fixed_from_int(es->geometry.width))
701 sx2 = wl_fixed_from_int(es->geometry.width);
702 if (sy2 > wl_fixed_from_int(es->geometry.height))
703 sy2 = wl_fixed_from_int(es->geometry.height);
707 s->src_w = (sx2 - sx1) << 8;
708 s->src_h = (sy2 - sy1) << 8;
709 pixman_region32_fini(&src_rect);
711 wl_signal_add(&es->buffer->resource.destroy_signal,
712 &s->pending_destroy_listener);
717 static struct weston_plane *
718 drm_output_prepare_cursor_surface(struct weston_output *output_base,
719 struct weston_surface *es)
721 struct drm_compositor *c =
722 (struct drm_compositor *) output_base->compositor;
723 struct drm_output *output = (struct drm_output *) output_base;
725 if (output->cursor_surface)
727 if (es->output_mask != (1u << output_base->id))
729 if (c->cursors_are_broken)
731 if (es->buffer == NULL || !wl_buffer_is_shm(es->buffer) ||
732 es->geometry.width > 64 || es->geometry.height > 64)
735 output->cursor_surface = es;
737 return &output->cursor_plane;
741 drm_output_set_cursor(struct drm_output *output)
743 struct weston_surface *es = output->cursor_surface;
744 struct drm_compositor *c =
745 (struct drm_compositor *) output->base.compositor;
746 EGLint handle, stride;
748 uint32_t buf[64 * 64];
752 output->cursor_surface = NULL;
754 drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0);
758 if (es->buffer && pixman_region32_not_empty(&output->cursor_plane.damage)) {
759 pixman_region32_fini(&output->cursor_plane.damage);
760 pixman_region32_init(&output->cursor_plane.damage);
761 output->current_cursor ^= 1;
762 bo = output->cursor_bo[output->current_cursor];
763 memset(buf, 0, sizeof buf);
764 stride = wl_shm_buffer_get_stride(es->buffer);
765 s = wl_shm_buffer_get_data(es->buffer);
766 for (i = 0; i < es->geometry.height; i++)
767 memcpy(buf + i * 64, s + i * stride,
768 es->geometry.width * 4);
770 if (gbm_bo_write(bo, buf, sizeof buf) < 0)
771 weston_log("failed update cursor: %m\n");
773 handle = gbm_bo_get_handle(bo).s32;
774 if (drmModeSetCursor(c->drm.fd,
775 output->crtc_id, handle, 64, 64)) {
776 weston_log("failed to set cursor: %m\n");
777 c->cursors_are_broken = 1;
781 x = es->geometry.x - output->base.x;
782 y = es->geometry.y - output->base.y;
783 if (output->cursor_plane.x != x || output->cursor_plane.y != y) {
784 if (drmModeMoveCursor(c->drm.fd, output->crtc_id, x, y)) {
785 weston_log("failed to move cursor: %m\n");
786 c->cursors_are_broken = 1;
789 output->cursor_plane.x = x;
790 output->cursor_plane.y = y;
795 drm_assign_planes(struct weston_output *output)
797 struct drm_compositor *c =
798 (struct drm_compositor *) output->compositor;
799 struct weston_surface *es, *next;
800 pixman_region32_t overlap, surface_overlap;
801 struct weston_plane *primary, *next_plane;
804 * Find a surface for each sprite in the output using some heuristics:
806 * 2) frequency of update
807 * 3) opacity (though some hw might support alpha blending)
808 * 4) clipping (this can be fixed with color keys)
810 * The idea is to save on blitting since this should save power.
811 * If we can get a large video surface on the sprite for example,
812 * the main display surface may not need to update at all, and
813 * the client buffer can be used directly for the sprite surface
814 * as we do for flipping full screen surfaces.
816 pixman_region32_init(&overlap);
817 primary = &c->base.primary_plane;
818 wl_list_for_each_safe(es, next, &c->base.surface_list, link) {
819 pixman_region32_init(&surface_overlap);
820 pixman_region32_intersect(&surface_overlap, &overlap,
821 &es->transform.boundingbox);
824 if (pixman_region32_not_empty(&surface_overlap))
825 next_plane = primary;
826 if (next_plane == NULL)
827 next_plane = drm_output_prepare_cursor_surface(output, es);
828 if (next_plane == NULL)
829 next_plane = drm_output_prepare_scanout_surface(output, es);
830 if (next_plane == NULL)
831 next_plane = drm_output_prepare_overlay_surface(output, es);
832 if (next_plane == NULL)
833 next_plane = primary;
834 weston_surface_move_to_plane(es, next_plane);
835 if (next_plane == primary)
836 pixman_region32_union(&overlap, &overlap,
837 &es->transform.boundingbox);
839 pixman_region32_fini(&surface_overlap);
841 pixman_region32_fini(&overlap);
845 drm_output_destroy(struct weston_output *output_base)
847 struct drm_output *output = (struct drm_output *) output_base;
848 struct drm_compositor *c =
849 (struct drm_compositor *) output->base.compositor;
850 drmModeCrtcPtr origcrtc = output->original_crtc;
852 if (output->backlight)
853 backlight_destroy(output->backlight);
855 /* Turn off hardware cursor */
856 drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0);
858 /* Restore original CRTC state */
859 drmModeSetCrtc(c->drm.fd, origcrtc->crtc_id, origcrtc->buffer_id,
860 origcrtc->x, origcrtc->y,
861 &output->connector_id, 1, &origcrtc->mode);
862 drmModeFreeCrtc(origcrtc);
864 c->crtc_allocator &= ~(1 << output->crtc_id);
865 c->connector_allocator &= ~(1 << output->connector_id);
867 eglDestroySurface(c->base.egl_display, output->base.egl_surface);
868 gbm_surface_destroy(output->surface);
870 weston_plane_release(&output->fb_plane);
871 weston_plane_release(&output->cursor_plane);
873 weston_output_destroy(&output->base);
874 wl_list_remove(&output->base.link);
880 static struct drm_mode *
881 choose_mode (struct drm_output *output, struct weston_mode *target_mode)
883 struct drm_mode *tmp_mode = NULL, *mode;
885 if (output->base.current->width == target_mode->width &&
886 output->base.current->height == target_mode->height &&
887 (output->base.current->refresh == target_mode->refresh ||
888 target_mode->refresh == 0))
889 return (struct drm_mode *)output->base.current;
891 wl_list_for_each(mode, &output->base.mode_list, base.link) {
892 if (mode->mode_info.hdisplay == target_mode->width &&
893 mode->mode_info.vdisplay == target_mode->height) {
894 if (mode->mode_info.vrefresh == target_mode->refresh ||
895 target_mode->refresh == 0) {
897 } else if (!tmp_mode)
906 drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mode)
908 struct drm_output *output;
909 struct drm_mode *drm_mode;
911 struct drm_compositor *ec;
912 struct gbm_surface *surface;
913 EGLSurface egl_surface;
915 if (output_base == NULL) {
916 weston_log("output is NULL.\n");
921 weston_log("mode is NULL.\n");
925 ec = (struct drm_compositor *)output_base->compositor;
926 output = (struct drm_output *)output_base;
927 drm_mode = choose_mode (output, mode);
930 weston_log("%s, invalid resolution:%dx%d\n", __func__, mode->width, mode->height);
932 } else if (&drm_mode->base == output->base.current) {
934 } else if (drm_mode->base.width == output->base.current->width &&
935 drm_mode->base.height == output->base.current->height) {
936 /* only change refresh value */
937 ret = drmModeSetCrtc(ec->drm.fd,
939 output->current->fb_id, 0, 0,
940 &output->connector_id, 1, &drm_mode->mode_info);
943 weston_log("failed to set mode (%dx%d) %u Hz\n",
944 drm_mode->base.width,
945 drm_mode->base.height,
946 drm_mode->base.refresh / 1000);
949 output->base.current->flags = 0;
950 output->base.current = &drm_mode->base;
951 drm_mode->base.flags =
952 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
959 drm_mode->base.flags =
960 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
962 surface = gbm_surface_create(ec->gbm,
963 drm_mode->base.width,
964 drm_mode->base.height,
967 GBM_BO_USE_RENDERING);
969 weston_log("failed to create gbm surface\n");
974 eglCreateWindowSurface(ec->base.egl_display,
978 if (egl_surface == EGL_NO_SURFACE) {
979 weston_log("failed to create egl surface\n");
983 ret = drmModeSetCrtc(ec->drm.fd,
985 output->current->fb_id, 0, 0,
986 &output->connector_id, 1, &drm_mode->mode_info);
988 weston_log("failed to set mode\n");
992 /* reset rendering stuff. */
993 if (output->current) {
994 if (output->current->is_client_buffer)
995 gbm_bo_destroy(output->current->bo);
997 gbm_surface_release_buffer(output->surface,
998 output->current->bo);
1000 output->current = NULL;
1003 if (output->next->is_client_buffer)
1004 gbm_bo_destroy(output->next->bo);
1006 gbm_surface_release_buffer(output->surface,
1009 output->next = NULL;
1011 eglDestroySurface(ec->base.egl_display, output->base.egl_surface);
1012 gbm_surface_destroy(output->surface);
1013 output->base.egl_surface = egl_surface;
1014 output->surface = surface;
1017 output->base.current = &drm_mode->base;
1018 output->base.dirty = 1;
1019 weston_output_move(&output->base, output->base.x, output->base.y);
1023 eglDestroySurface(ec->base.egl_display, egl_surface);
1024 gbm_surface_destroy(surface);
1029 on_drm_input(int fd, uint32_t mask, void *data)
1031 drmEventContext evctx;
1033 memset(&evctx, 0, sizeof evctx);
1034 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1035 evctx.page_flip_handler = page_flip_handler;
1036 evctx.vblank_handler = vblank_handler;
1037 drmHandleEvent(fd, &evctx);
1043 init_egl(struct drm_compositor *ec, struct udev_device *device)
1045 EGLint major, minor, n;
1046 const char *filename, *sysnum;
1048 static const EGLint context_attribs[] = {
1049 EGL_CONTEXT_CLIENT_VERSION, 2,
1053 static const EGLint config_attribs[] = {
1054 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
1059 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
1063 sysnum = udev_device_get_sysnum(device);
1065 ec->drm.id = atoi(sysnum);
1066 if (!sysnum || ec->drm.id < 0) {
1067 weston_log("cannot get device sysnum\n");
1071 filename = udev_device_get_devnode(device);
1072 fd = open(filename, O_RDWR | O_CLOEXEC);
1074 /* Probably permissions error */
1075 weston_log("couldn't open %s, skipping\n",
1076 udev_device_get_devnode(device));
1080 weston_log("using %s\n", filename);
1083 ec->gbm = gbm_create_device(ec->drm.fd);
1084 ec->base.egl_display = eglGetDisplay(ec->gbm);
1085 if (ec->base.egl_display == NULL) {
1086 weston_log("failed to create display\n");
1090 if (!eglInitialize(ec->base.egl_display, &major, &minor)) {
1091 weston_log("failed to initialize display\n");
1095 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
1096 weston_log("failed to bind api EGL_OPENGL_ES_API\n");
1100 if (!eglChooseConfig(ec->base.egl_display, config_attribs,
1101 &ec->base.egl_config, 1, &n) || n != 1) {
1102 weston_log("failed to choose config: %d\n", n);
1106 ec->base.egl_context =
1107 eglCreateContext(ec->base.egl_display, ec->base.egl_config,
1108 EGL_NO_CONTEXT, context_attribs);
1109 if (ec->base.egl_context == NULL) {
1110 weston_log("failed to create context\n");
1114 ec->dummy_surface = gbm_surface_create(ec->gbm, 10, 10,
1115 GBM_FORMAT_XRGB8888,
1116 GBM_BO_USE_RENDERING);
1117 if (!ec->dummy_surface) {
1118 weston_log("failed to create dummy gbm surface\n");
1122 ec->dummy_egl_surface =
1123 eglCreateWindowSurface(ec->base.egl_display,
1124 ec->base.egl_config,
1127 if (ec->dummy_egl_surface == EGL_NO_SURFACE) {
1128 weston_log("failed to create egl surface\n");
1132 if (!eglMakeCurrent(ec->base.egl_display, ec->dummy_egl_surface,
1133 ec->dummy_egl_surface, ec->base.egl_context)) {
1134 weston_log("failed to make context current\n");
1141 static struct drm_mode *
1142 drm_output_add_mode(struct drm_output *output, drmModeModeInfo *info)
1144 struct drm_mode *mode;
1147 mode = malloc(sizeof *mode);
1151 mode->base.flags = 0;
1152 mode->base.width = info->hdisplay;
1153 mode->base.height = info->vdisplay;
1155 /* Calculate higher precision (mHz) refresh rate */
1156 refresh = (info->clock * 1000000LL / info->htotal +
1157 info->vtotal / 2) / info->vtotal;
1159 if (info->flags & DRM_MODE_FLAG_INTERLACE)
1161 if (info->flags & DRM_MODE_FLAG_DBLSCAN)
1163 if (info->vscan > 1)
1164 refresh /= info->vscan;
1166 mode->base.refresh = refresh;
1167 mode->mode_info = *info;
1169 if (info->type & DRM_MODE_TYPE_PREFERRED)
1170 mode->base.flags |= WL_OUTPUT_MODE_PREFERRED;
1172 wl_list_insert(output->base.mode_list.prev, &mode->base.link);
1178 drm_subpixel_to_wayland(int drm_value)
1180 switch (drm_value) {
1182 case DRM_MODE_SUBPIXEL_UNKNOWN:
1183 return WL_OUTPUT_SUBPIXEL_UNKNOWN;
1184 case DRM_MODE_SUBPIXEL_NONE:
1185 return WL_OUTPUT_SUBPIXEL_NONE;
1186 case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB:
1187 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB;
1188 case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR:
1189 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR;
1190 case DRM_MODE_SUBPIXEL_VERTICAL_RGB:
1191 return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB;
1192 case DRM_MODE_SUBPIXEL_VERTICAL_BGR:
1193 return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR;
1198 sprite_handle_buffer_destroy(struct wl_listener *listener, void *data)
1200 struct drm_sprite *sprite =
1201 container_of(listener, struct drm_sprite,
1203 struct drm_compositor *compositor = sprite->compositor;
1205 sprite->surface = NULL;
1206 drmModeRmFB(compositor->drm.fd, sprite->fb_id);
1211 sprite_handle_pending_buffer_destroy(struct wl_listener *listener, void *data)
1213 struct drm_sprite *sprite =
1214 container_of(listener, struct drm_sprite,
1215 pending_destroy_listener);
1216 struct drm_compositor *compositor = sprite->compositor;
1218 sprite->pending_surface = NULL;
1219 drmModeRmFB(compositor->drm.fd, sprite->pending_fb_id);
1220 sprite->pending_fb_id = 0;
1223 /* returns a value between 0-255 range, where higher is brighter */
1225 drm_get_backlight(struct drm_output *output)
1227 long brightness, max_brightness, norm;
1229 brightness = backlight_get_brightness(output->backlight);
1230 max_brightness = backlight_get_max_brightness(output->backlight);
1232 /* convert it on a scale of 0 to 255 */
1233 norm = (brightness * 255)/(max_brightness);
1235 return (uint32_t) norm;
1238 /* values accepted are between 0-255 range */
1240 drm_set_backlight(struct weston_output *output_base, uint32_t value)
1242 struct drm_output *output = (struct drm_output *) output_base;
1243 long max_brightness, new_brightness;
1245 if (!output->backlight)
1251 max_brightness = backlight_get_max_brightness(output->backlight);
1253 /* get denormalized value */
1254 new_brightness = (value * max_brightness) / 255;
1256 backlight_set_brightness(output->backlight, new_brightness);
1259 static drmModePropertyPtr
1260 drm_get_prop(int fd, drmModeConnectorPtr connector, const char *name)
1262 drmModePropertyPtr props;
1265 for (i = 0; i < connector->count_props; i++) {
1266 props = drmModeGetProperty(fd, connector->props[i]);
1270 if (!strcmp(props->name, name))
1273 drmModeFreeProperty(props);
1280 drm_set_dpms(struct weston_output *output_base, enum dpms_enum level)
1282 struct drm_output *output = (struct drm_output *) output_base;
1283 struct weston_compositor *ec = output_base->compositor;
1284 struct drm_compositor *c = (struct drm_compositor *) ec;
1285 drmModeConnectorPtr connector;
1286 drmModePropertyPtr prop;
1288 connector = drmModeGetConnector(c->drm.fd, output->connector_id);
1292 prop = drm_get_prop(c->drm.fd, connector, "DPMS");
1294 drmModeFreeConnector(connector);
1298 drmModeConnectorSetProperty(c->drm.fd, connector->connector_id,
1299 prop->prop_id, level);
1300 drmModeFreeProperty(prop);
1301 drmModeFreeConnector(connector);
1304 static const char *connector_type_names[] = {
1323 find_crtc_for_connector(struct drm_compositor *ec,
1324 drmModeRes *resources, drmModeConnector *connector)
1326 drmModeEncoder *encoder;
1327 uint32_t possible_crtcs;
1330 for (j = 0; j < connector->count_encoders; j++) {
1331 encoder = drmModeGetEncoder(ec->drm.fd, connector->encoders[j]);
1332 if (encoder == NULL) {
1333 weston_log("Failed to get encoder.\n");
1336 possible_crtcs = encoder->possible_crtcs;
1337 drmModeFreeEncoder(encoder);
1339 for (i = 0; i < resources->count_crtcs; i++) {
1340 if (possible_crtcs & (1 << i) &&
1341 !(ec->crtc_allocator & (1 << resources->crtcs[i])))
1350 create_output_for_connector(struct drm_compositor *ec,
1351 drmModeRes *resources,
1352 drmModeConnector *connector,
1353 int x, int y, struct udev_device *drm_device)
1355 struct drm_output *output;
1356 struct drm_mode *drm_mode, *next, *preferred, *current, *configured;
1357 struct weston_mode *m;
1358 struct drm_configured_output *o = NULL, *temp;
1359 drmModeEncoder *encoder;
1360 drmModeModeInfo crtc_mode;
1364 const char *type_name;
1366 i = find_crtc_for_connector(ec, resources, connector);
1368 weston_log("No usable crtc/encoder pair for connector.\n");
1372 output = malloc(sizeof *output);
1376 memset(output, 0, sizeof *output);
1377 output->base.subpixel = drm_subpixel_to_wayland(connector->subpixel);
1378 output->base.make = "unknown";
1379 output->base.model = "unknown";
1380 wl_list_init(&output->base.mode_list);
1382 if (connector->connector_type < ARRAY_LENGTH(connector_type_names))
1383 type_name = connector_type_names[connector->connector_type];
1385 type_name = "UNKNOWN";
1386 snprintf(name, 32, "%s%d", type_name, connector->connector_type_id);
1387 output->name = strdup(name);
1389 output->crtc_id = resources->crtcs[i];
1391 ec->crtc_allocator |= (1 << output->crtc_id);
1392 output->connector_id = connector->connector_id;
1393 ec->connector_allocator |= (1 << output->connector_id);
1395 output->original_crtc = drmModeGetCrtc(ec->drm.fd, output->crtc_id);
1397 /* Get the current mode on the crtc that's currently driving
1398 * this connector. */
1399 encoder = drmModeGetEncoder(ec->drm.fd, connector->encoder_id);
1400 memset(&crtc_mode, 0, sizeof crtc_mode);
1401 if (encoder != NULL) {
1402 crtc = drmModeGetCrtc(ec->drm.fd, encoder->crtc_id);
1403 drmModeFreeEncoder(encoder);
1406 if (crtc->mode_valid)
1407 crtc_mode = crtc->mode;
1408 drmModeFreeCrtc(crtc);
1411 for (i = 0; i < connector->count_modes; i++) {
1412 drm_mode = drm_output_add_mode(output, &connector->modes[i]);
1421 wl_list_for_each(temp, &configured_output_list, link) {
1422 if (strcmp(temp->name, output->name) == 0) {
1424 weston_log("%s mode \"%s\" in config\n",
1425 temp->name, temp->mode);
1431 if (o && o->config == OUTPUT_CONFIG_OFF) {
1432 weston_log("Disabling output %s\n", o->name);
1434 drmModeSetCrtc(ec->drm.fd, output->crtc_id,
1435 0, 0, 0, 0, 0, NULL);
1439 wl_list_for_each(drm_mode, &output->base.mode_list, base.link) {
1440 if (o && o->config == OUTPUT_CONFIG_MODE &&
1441 o->width == drm_mode->base.width &&
1442 o->height == drm_mode->base.height)
1443 configured = drm_mode;
1444 if (!memcmp(&crtc_mode, &drm_mode->mode_info, sizeof crtc_mode))
1446 if (drm_mode->base.flags & WL_OUTPUT_MODE_PREFERRED)
1447 preferred = drm_mode;
1450 if (o && o->config == OUTPUT_CONFIG_MODELINE) {
1451 configured = drm_output_add_mode(output, &o->crtc_mode);
1454 current = configured;
1457 if (current == NULL && crtc_mode.clock != 0) {
1458 current = drm_output_add_mode(output, &crtc_mode);
1463 if (o && o->config == OUTPUT_CONFIG_CURRENT)
1464 configured = current;
1466 if (option_current_mode && current)
1467 output->base.current = ¤t->base;
1468 else if (configured)
1469 output->base.current = &configured->base;
1471 output->base.current = &preferred->base;
1473 output->base.current = ¤t->base;
1475 if (output->base.current == NULL) {
1476 weston_log("no available modes for %s\n", output->name);
1480 output->base.current->flags |= WL_OUTPUT_MODE_CURRENT;
1482 output->surface = gbm_surface_create(ec->gbm,
1483 output->base.current->width,
1484 output->base.current->height,
1485 GBM_FORMAT_XRGB8888,
1486 GBM_BO_USE_SCANOUT |
1487 GBM_BO_USE_RENDERING);
1488 if (!output->surface) {
1489 weston_log("failed to create gbm surface\n");
1493 output->base.egl_surface =
1494 eglCreateWindowSurface(ec->base.egl_display,
1495 ec->base.egl_config,
1498 if (output->base.egl_surface == EGL_NO_SURFACE) {
1499 weston_log("failed to create egl surface\n");
1503 output->cursor_bo[0] =
1504 gbm_bo_create(ec->gbm, 64, 64, GBM_FORMAT_ARGB8888,
1505 GBM_BO_USE_CURSOR_64X64 | GBM_BO_USE_WRITE);
1506 output->cursor_bo[1] =
1507 gbm_bo_create(ec->gbm, 64, 64, GBM_FORMAT_ARGB8888,
1508 GBM_BO_USE_CURSOR_64X64 | GBM_BO_USE_WRITE);
1510 output->backlight = backlight_init(drm_device,
1511 connector->connector_type);
1512 if (output->backlight) {
1513 output->base.set_backlight = drm_set_backlight;
1514 output->base.backlight_current = drm_get_backlight(output);
1517 weston_output_init(&output->base, &ec->base, x, y,
1518 connector->mmWidth, connector->mmHeight,
1519 o ? o->transform : WL_OUTPUT_TRANSFORM_NORMAL);
1521 wl_list_insert(ec->base.output_list.prev, &output->base.link);
1523 output->base.origin = output->base.current;
1524 output->base.repaint = drm_output_repaint;
1525 output->base.destroy = drm_output_destroy;
1526 output->base.assign_planes = drm_assign_planes;
1527 output->base.set_dpms = drm_set_dpms;
1528 output->base.switch_mode = drm_output_switch_mode;
1530 weston_plane_init(&output->cursor_plane, 0, 0);
1531 weston_plane_init(&output->fb_plane, 0, 0);
1533 weston_log("Output %s, (connector %d, crtc %d)\n",
1534 output->name, output->connector_id, output->crtc_id);
1535 wl_list_for_each(m, &output->base.mode_list, link)
1536 weston_log_continue(" mode %dx%d@%.1f%s%s%s\n",
1537 m->width, m->height, m->refresh / 1000.0,
1538 m->flags & WL_OUTPUT_MODE_PREFERRED ?
1540 m->flags & WL_OUTPUT_MODE_CURRENT ?
1542 connector->count_modes == 0 ?
1548 gbm_surface_destroy(output->surface);
1550 wl_list_for_each_safe(drm_mode, next, &output->base.mode_list,
1552 wl_list_remove(&drm_mode->base.link);
1556 drmModeFreeCrtc(output->original_crtc);
1557 ec->crtc_allocator &= ~(1 << output->crtc_id);
1558 ec->connector_allocator &= ~(1 << output->connector_id);
1566 create_sprites(struct drm_compositor *ec)
1568 struct drm_sprite *sprite;
1569 drmModePlaneRes *plane_res;
1570 drmModePlane *plane;
1573 plane_res = drmModeGetPlaneResources(ec->drm.fd);
1575 weston_log("failed to get plane resources: %s\n",
1580 for (i = 0; i < plane_res->count_planes; i++) {
1581 plane = drmModeGetPlane(ec->drm.fd, plane_res->planes[i]);
1585 sprite = malloc(sizeof(*sprite) + ((sizeof(uint32_t)) *
1586 plane->count_formats));
1588 weston_log("%s: out of memory\n",
1594 memset(sprite, 0, sizeof *sprite);
1596 sprite->possible_crtcs = plane->possible_crtcs;
1597 sprite->plane_id = plane->plane_id;
1598 sprite->surface = NULL;
1599 sprite->pending_surface = NULL;
1601 sprite->pending_fb_id = 0;
1602 sprite->destroy_listener.notify = sprite_handle_buffer_destroy;
1603 sprite->pending_destroy_listener.notify =
1604 sprite_handle_pending_buffer_destroy;
1605 sprite->compositor = ec;
1606 sprite->count_formats = plane->count_formats;
1607 memcpy(sprite->formats, plane->formats,
1608 plane->count_formats * sizeof(plane->formats[0]));
1609 drmModeFreePlane(plane);
1610 weston_plane_init(&sprite->plane, 0, 0);
1612 wl_list_insert(&ec->sprite_list, &sprite->link);
1615 free(plane_res->planes);
1620 destroy_sprites(struct drm_compositor *compositor)
1622 struct drm_sprite *sprite, *next;
1623 struct drm_output *output;
1625 output = container_of(compositor->base.output_list.next,
1626 struct drm_output, base.link);
1628 wl_list_for_each_safe(sprite, next, &compositor->sprite_list, link) {
1629 drmModeSetPlane(compositor->drm.fd,
1631 output->crtc_id, 0, 0,
1632 0, 0, 0, 0, 0, 0, 0, 0);
1633 drmModeRmFB(compositor->drm.fd, sprite->fb_id);
1634 weston_plane_release(&sprite->plane);
1640 create_outputs(struct drm_compositor *ec, uint32_t option_connector,
1641 struct udev_device *drm_device)
1643 drmModeConnector *connector;
1644 drmModeRes *resources;
1648 resources = drmModeGetResources(ec->drm.fd);
1650 weston_log("drmModeGetResources failed\n");
1654 ec->crtcs = calloc(resources->count_crtcs, sizeof(uint32_t));
1656 drmModeFreeResources(resources);
1660 ec->min_width = resources->min_width;
1661 ec->max_width = resources->max_width;
1662 ec->min_height = resources->min_height;
1663 ec->max_height = resources->max_height;
1665 ec->num_crtcs = resources->count_crtcs;
1666 memcpy(ec->crtcs, resources->crtcs, sizeof(uint32_t) * ec->num_crtcs);
1668 for (i = 0; i < resources->count_connectors; i++) {
1669 connector = drmModeGetConnector(ec->drm.fd,
1670 resources->connectors[i]);
1671 if (connector == NULL)
1674 if (connector->connection == DRM_MODE_CONNECTED &&
1675 (option_connector == 0 ||
1676 connector->connector_id == option_connector)) {
1677 if (create_output_for_connector(ec, resources,
1680 drmModeFreeConnector(connector);
1684 x += container_of(ec->base.output_list.prev,
1685 struct weston_output,
1689 drmModeFreeConnector(connector);
1692 if (wl_list_empty(&ec->base.output_list)) {
1693 weston_log("No currently active connector found.\n");
1694 drmModeFreeResources(resources);
1698 drmModeFreeResources(resources);
1704 update_outputs(struct drm_compositor *ec, struct udev_device *drm_device)
1706 drmModeConnector *connector;
1707 drmModeRes *resources;
1708 struct drm_output *output, *next;
1710 int x_offset = 0, y_offset = 0;
1711 uint32_t connected = 0, disconnects = 0;
1714 resources = drmModeGetResources(ec->drm.fd);
1716 weston_log("drmModeGetResources failed\n");
1720 /* collect new connects */
1721 for (i = 0; i < resources->count_connectors; i++) {
1722 int connector_id = resources->connectors[i];
1724 connector = drmModeGetConnector(ec->drm.fd, connector_id);
1725 if (connector == NULL)
1728 if (connector->connection != DRM_MODE_CONNECTED) {
1729 drmModeFreeConnector(connector);
1733 connected |= (1 << connector_id);
1735 if (!(ec->connector_allocator & (1 << connector_id))) {
1736 struct weston_output *last =
1737 container_of(ec->base.output_list.prev,
1738 struct weston_output, link);
1740 /* XXX: not yet needed, we die with 0 outputs */
1741 if (!wl_list_empty(&ec->base.output_list))
1742 x = last->x + last->width;
1746 create_output_for_connector(ec, resources,
1749 weston_log("connector %d connected\n", connector_id);
1752 drmModeFreeConnector(connector);
1754 drmModeFreeResources(resources);
1756 disconnects = ec->connector_allocator & ~connected;
1758 wl_list_for_each_safe(output, next, &ec->base.output_list,
1760 if (x_offset != 0 || y_offset != 0) {
1761 weston_output_move(&output->base,
1762 output->base.x - x_offset,
1763 output->base.y - y_offset);
1766 if (disconnects & (1 << output->connector_id)) {
1767 disconnects &= ~(1 << output->connector_id);
1768 weston_log("connector %d disconnected\n",
1769 output->connector_id);
1770 x_offset += output->base.width;
1771 drm_output_destroy(&output->base);
1776 /* FIXME: handle zero outputs, without terminating */
1777 if (ec->connector_allocator == 0)
1778 wl_display_terminate(ec->base.wl_display);
1782 udev_event_is_hotplug(struct drm_compositor *ec, struct udev_device *device)
1787 sysnum = udev_device_get_sysnum(device);
1788 if (!sysnum || atoi(sysnum) != ec->drm.id)
1791 val = udev_device_get_property_value(device, "HOTPLUG");
1795 return strcmp(val, "1") == 0;
1799 udev_drm_event(int fd, uint32_t mask, void *data)
1801 struct drm_compositor *ec = data;
1802 struct udev_device *event;
1804 event = udev_monitor_receive_device(ec->udev_monitor);
1806 if (udev_event_is_hotplug(ec, event))
1807 update_outputs(ec, event);
1809 udev_device_unref(event);
1815 drm_restore(struct weston_compositor *ec)
1817 struct drm_compositor *d = (struct drm_compositor *) ec;
1819 if (weston_launcher_drm_set_master(&d->base, d->drm.fd, 0) < 0)
1820 weston_log("failed to drop master: %m\n");
1824 static const char default_seat[] = "seat0";
1827 device_added(struct udev_device *udev_device, struct drm_seat *master)
1829 struct weston_compositor *c;
1830 struct evdev_device *device;
1831 const char *devnode;
1832 const char *device_seat;
1835 device_seat = udev_device_get_property_value(udev_device, "ID_SEAT");
1837 device_seat = default_seat;
1839 if (strcmp(device_seat, master->seat_id))
1842 c = master->base.compositor;
1843 devnode = udev_device_get_devnode(udev_device);
1845 /* Use non-blocking mode so that we can loop on read on
1846 * evdev_device_data() until all events on the fd are
1847 * read. mtdev_get() also expects this. */
1848 fd = weston_launcher_open(c, devnode, O_RDWR | O_NONBLOCK);
1850 weston_log("opening input device '%s' failed.\n", devnode);
1854 device = evdev_device_create(&master->base, devnode, fd);
1857 weston_log("not using input device '%s'.\n", devnode);
1861 wl_list_insert(master->devices_list.prev, &device->link);
1865 evdev_add_devices(struct udev *udev, struct weston_seat *seat_base)
1867 struct drm_seat *seat = (struct drm_seat *) seat_base;
1868 struct udev_enumerate *e;
1869 struct udev_list_entry *entry;
1870 struct udev_device *device;
1871 const char *path, *sysname;
1873 e = udev_enumerate_new(udev);
1874 udev_enumerate_add_match_subsystem(e, "input");
1875 udev_enumerate_scan_devices(e);
1876 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
1877 path = udev_list_entry_get_name(entry);
1878 device = udev_device_new_from_syspath(udev, path);
1880 sysname = udev_device_get_sysname(device);
1881 if (strncmp("event", sysname, 5) != 0) {
1882 udev_device_unref(device);
1886 device_added(device, seat);
1888 udev_device_unref(device);
1890 udev_enumerate_unref(e);
1892 evdev_notify_keyboard_focus(&seat->base, &seat->devices_list);
1894 if (wl_list_empty(&seat->devices_list)) {
1896 "warning: no input devices on entering Weston. "
1897 "Possible causes:\n"
1898 "\t- no permissions to read /dev/input/event*\n"
1899 "\t- seats misconfigured "
1900 "(Weston backend option 'seat', "
1901 "udev device property ID_SEAT)\n");
1906 evdev_udev_handler(int fd, uint32_t mask, void *data)
1908 struct drm_seat *seat = data;
1909 struct udev_device *udev_device;
1910 struct evdev_device *device, *next;
1912 const char *devnode;
1914 udev_device = udev_monitor_receive_device(seat->udev_monitor);
1918 action = udev_device_get_action(udev_device);
1922 if (strncmp("event", udev_device_get_sysname(udev_device), 5) != 0)
1925 if (!strcmp(action, "add")) {
1926 device_added(udev_device, seat);
1928 else if (!strcmp(action, "remove")) {
1929 devnode = udev_device_get_devnode(udev_device);
1930 wl_list_for_each_safe(device, next, &seat->devices_list, link)
1931 if (!strcmp(device->devnode, devnode)) {
1932 weston_log("input device %s, %s removed\n",
1933 device->devname, device->devnode);
1934 evdev_device_destroy(device);
1940 udev_device_unref(udev_device);
1946 evdev_enable_udev_monitor(struct udev *udev, struct weston_seat *seat_base)
1948 struct drm_seat *master = (struct drm_seat *) seat_base;
1949 struct wl_event_loop *loop;
1950 struct weston_compositor *c = master->base.compositor;
1953 master->udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
1954 if (!master->udev_monitor) {
1955 weston_log("udev: failed to create the udev monitor\n");
1959 udev_monitor_filter_add_match_subsystem_devtype(master->udev_monitor,
1962 if (udev_monitor_enable_receiving(master->udev_monitor)) {
1963 weston_log("udev: failed to bind the udev monitor\n");
1964 udev_monitor_unref(master->udev_monitor);
1968 loop = wl_display_get_event_loop(c->wl_display);
1969 fd = udev_monitor_get_fd(master->udev_monitor);
1970 master->udev_monitor_source =
1971 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1972 evdev_udev_handler, master);
1973 if (!master->udev_monitor_source) {
1974 udev_monitor_unref(master->udev_monitor);
1982 evdev_disable_udev_monitor(struct weston_seat *seat_base)
1984 struct drm_seat *seat = (struct drm_seat *) seat_base;
1986 if (!seat->udev_monitor)
1989 udev_monitor_unref(seat->udev_monitor);
1990 seat->udev_monitor = NULL;
1991 wl_event_source_remove(seat->udev_monitor_source);
1992 seat->udev_monitor_source = NULL;
1996 drm_led_update(struct weston_seat *seat_base, enum weston_led leds)
1998 struct drm_seat *seat = (struct drm_seat *) seat_base;
1999 struct evdev_device *device;
2001 wl_list_for_each(device, &seat->devices_list, link)
2002 evdev_led_update(device, leds);
2006 evdev_input_create(struct weston_compositor *c, struct udev *udev,
2007 const char *seat_id)
2009 struct drm_seat *seat;
2011 seat = malloc(sizeof *seat);
2015 memset(seat, 0, sizeof *seat);
2016 weston_seat_init(&seat->base, c);
2017 seat->base.led_update = drm_led_update;
2019 wl_list_init(&seat->devices_list);
2020 seat->seat_id = strdup(seat_id);
2021 if (!evdev_enable_udev_monitor(udev, &seat->base)) {
2022 free(seat->seat_id);
2027 evdev_add_devices(udev, &seat->base);
2031 evdev_remove_devices(struct weston_seat *seat_base)
2033 struct drm_seat *seat = (struct drm_seat *) seat_base;
2034 struct evdev_device *device, *next;
2036 wl_list_for_each_safe(device, next, &seat->devices_list, link)
2037 evdev_device_destroy(device);
2039 if (seat->base.seat.keyboard)
2040 notify_keyboard_focus_out(&seat->base);
2044 evdev_input_destroy(struct weston_seat *seat_base)
2046 struct drm_seat *seat = (struct drm_seat *) seat_base;
2048 evdev_remove_devices(seat_base);
2049 evdev_disable_udev_monitor(&seat->base);
2051 weston_seat_release(seat_base);
2052 free(seat->seat_id);
2057 drm_free_configured_output(struct drm_configured_output *output)
2065 drm_destroy(struct weston_compositor *ec)
2067 struct drm_compositor *d = (struct drm_compositor *) ec;
2068 struct weston_seat *seat, *next;
2069 struct drm_configured_output *o, *n;
2071 wl_list_for_each_safe(seat, next, &ec->seat_list, link)
2072 evdev_input_destroy(seat);
2073 wl_list_for_each_safe(o, n, &configured_output_list, link)
2074 drm_free_configured_output(o);
2076 wl_event_source_remove(d->udev_drm_source);
2077 wl_event_source_remove(d->drm_source);
2079 weston_compositor_shutdown(ec);
2081 /* Work around crash in egl_dri2.c's dri2_make_current() */
2082 eglMakeCurrent(ec->egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
2084 eglTerminate(ec->egl_display);
2087 gbm_device_destroy(d->gbm);
2089 if (weston_launcher_drm_set_master(&d->base, d->drm.fd, 0) < 0)
2090 weston_log("failed to drop master: %m\n");
2091 tty_destroy(d->tty);
2097 drm_compositor_set_modes(struct drm_compositor *compositor)
2099 struct drm_output *output;
2100 struct drm_mode *drm_mode;
2103 wl_list_for_each(output, &compositor->base.output_list, base.link) {
2104 drm_mode = (struct drm_mode *) output->base.current;
2105 ret = drmModeSetCrtc(compositor->drm.fd, output->crtc_id,
2106 output->current->fb_id, 0, 0,
2107 &output->connector_id, 1,
2108 &drm_mode->mode_info);
2111 "failed to set mode %dx%d for output at %d,%d: %m\n",
2112 drm_mode->base.width, drm_mode->base.height,
2113 output->base.x, output->base.y);
2119 vt_func(struct weston_compositor *compositor, int event)
2121 struct drm_compositor *ec = (struct drm_compositor *) compositor;
2122 struct weston_seat *seat;
2123 struct drm_sprite *sprite;
2124 struct drm_output *output;
2128 weston_log("entering VT\n");
2129 compositor->focus = 1;
2130 if (weston_launcher_drm_set_master(&ec->base, ec->drm.fd, 1)) {
2131 weston_log("failed to set master: %m\n");
2132 wl_display_terminate(compositor->wl_display);
2134 compositor->state = ec->prev_state;
2135 drm_compositor_set_modes(ec);
2136 weston_compositor_damage_all(compositor);
2137 wl_list_for_each(seat, &compositor->seat_list, link) {
2138 evdev_add_devices(ec->udev, seat);
2139 evdev_enable_udev_monitor(ec->udev, seat);
2143 weston_log("leaving VT\n");
2144 wl_list_for_each(seat, &compositor->seat_list, link) {
2145 evdev_disable_udev_monitor(seat);
2146 evdev_remove_devices(seat);
2149 compositor->focus = 0;
2150 ec->prev_state = compositor->state;
2151 compositor->state = WESTON_COMPOSITOR_SLEEPING;
2153 /* If we have a repaint scheduled (either from a
2154 * pending pageflip or the idle handler), make sure we
2155 * cancel that so we don't try to pageflip when we're
2156 * vt switched away. The SLEEPING state will prevent
2157 * further attemps at repainting. When we switch
2158 * back, we schedule a repaint, which will process
2159 * pending frame callbacks. */
2161 wl_list_for_each(output, &ec->base.output_list, base.link) {
2162 output->base.repaint_needed = 0;
2163 drmModeSetCursor(ec->drm.fd, output->crtc_id, 0, 0, 0);
2166 output = container_of(ec->base.output_list.next,
2167 struct drm_output, base.link);
2169 wl_list_for_each(sprite, &ec->sprite_list, link)
2170 drmModeSetPlane(ec->drm.fd,
2172 output->crtc_id, 0, 0,
2173 0, 0, 0, 0, 0, 0, 0, 0);
2175 if (weston_launcher_drm_set_master(&ec->base, ec->drm.fd, 0) < 0)
2176 weston_log("failed to drop master: %m\n");
2183 switch_vt_binding(struct wl_seat *seat, uint32_t time, uint32_t key, void *data)
2185 struct drm_compositor *ec = data;
2187 tty_activate_vt(ec->tty, key - KEY_F1 + 1);
2190 static struct weston_compositor *
2191 drm_compositor_create(struct wl_display *display,
2192 int connector, const char *seat, int tty,
2193 int argc, char *argv[], const char *config_file)
2195 struct drm_compositor *ec;
2196 struct udev_enumerate *e;
2197 struct udev_list_entry *entry;
2198 struct udev_device *device, *drm_device;
2199 const char *path, *device_seat;
2200 struct wl_event_loop *loop;
2201 struct weston_seat *weston_seat, *next;
2204 weston_log("initializing drm backend\n");
2206 ec = malloc(sizeof *ec);
2209 memset(ec, 0, sizeof *ec);
2211 if (weston_compositor_init(&ec->base, display, argc, argv,
2213 weston_log("weston_compositor_init failed\n");
2217 ec->udev = udev_new();
2218 if (ec->udev == NULL) {
2219 weston_log("failed to initialize udev context\n");
2220 goto err_compositor;
2223 ec->base.wl_display = display;
2224 ec->tty = tty_create(&ec->base, vt_func, tty);
2226 weston_log("failed to initialize tty\n");
2230 e = udev_enumerate_new(ec->udev);
2231 udev_enumerate_add_match_subsystem(e, "drm");
2232 udev_enumerate_add_match_sysname(e, "card[0-9]*");
2234 udev_enumerate_scan_devices(e);
2236 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
2237 path = udev_list_entry_get_name(entry);
2238 device = udev_device_new_from_syspath(ec->udev, path);
2240 udev_device_get_property_value(device, "ID_SEAT");
2242 device_seat = default_seat;
2243 if (strcmp(device_seat, seat) == 0) {
2244 drm_device = device;
2247 udev_device_unref(device);
2250 if (drm_device == NULL) {
2251 weston_log("no drm device found\n");
2255 if (init_egl(ec, drm_device) < 0) {
2256 weston_log("failed to initialize egl\n");
2260 ec->base.destroy = drm_destroy;
2261 ec->base.restore = drm_restore;
2265 ec->prev_state = WESTON_COMPOSITOR_ACTIVE;
2267 if (weston_compositor_init_gl(&ec->base) < 0)
2270 for (key = KEY_F1; key < KEY_F9; key++)
2271 weston_compositor_add_key_binding(&ec->base, key,
2272 MODIFIER_CTRL | MODIFIER_ALT,
2273 switch_vt_binding, ec);
2275 wl_list_init(&ec->sprite_list);
2278 if (create_outputs(ec, connector, drm_device) < 0) {
2279 weston_log("failed to create output for %s\n", path);
2285 evdev_input_create(&ec->base, ec->udev, seat);
2287 loop = wl_display_get_event_loop(ec->base.wl_display);
2289 wl_event_loop_add_fd(loop, ec->drm.fd,
2290 WL_EVENT_READABLE, on_drm_input, ec);
2292 ec->udev_monitor = udev_monitor_new_from_netlink(ec->udev, "udev");
2293 if (ec->udev_monitor == NULL) {
2294 weston_log("failed to intialize udev monitor\n");
2295 goto err_drm_source;
2297 udev_monitor_filter_add_match_subsystem_devtype(ec->udev_monitor,
2299 ec->udev_drm_source =
2300 wl_event_loop_add_fd(loop,
2301 udev_monitor_get_fd(ec->udev_monitor),
2302 WL_EVENT_READABLE, udev_drm_event, ec);
2304 if (udev_monitor_enable_receiving(ec->udev_monitor) < 0) {
2305 weston_log("failed to enable udev-monitor receiving\n");
2306 goto err_udev_monitor;
2309 udev_device_unref(drm_device);
2310 udev_enumerate_unref(e);
2315 wl_event_source_remove(ec->udev_drm_source);
2316 udev_monitor_unref(ec->udev_monitor);
2318 wl_event_source_remove(ec->drm_source);
2319 wl_list_for_each_safe(weston_seat, next, &ec->base.seat_list, link)
2320 evdev_input_destroy(weston_seat);
2322 destroy_sprites(ec);
2324 eglMakeCurrent(ec->base.egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
2326 eglTerminate(ec->base.egl_display);
2328 gbm_device_destroy(ec->gbm);
2330 udev_device_unref(drm_device);
2332 udev_enumerate_unref(e);
2333 tty_destroy(ec->tty);
2335 udev_unref(ec->udev);
2337 weston_compositor_shutdown(&ec->base);
2344 set_sync_flags(drmModeModeInfo *mode, char *hsync, char *vsync)
2348 if (strcmp(hsync, "+hsync") == 0)
2349 mode->flags |= DRM_MODE_FLAG_PHSYNC;
2350 else if (strcmp(hsync, "-hsync") == 0)
2351 mode->flags |= DRM_MODE_FLAG_NHSYNC;
2355 if (strcmp(vsync, "+vsync") == 0)
2356 mode->flags |= DRM_MODE_FLAG_PVSYNC;
2357 else if (strcmp(vsync, "-vsync") == 0)
2358 mode->flags |= DRM_MODE_FLAG_NVSYNC;
2366 check_for_modeline(struct drm_configured_output *output)
2368 drmModeModeInfo mode;
2374 mode.type = DRM_MODE_TYPE_USERDEF;
2379 if (sscanf(output_mode, "%f %hd %hd %hd %hd %hd %hd %hd %hd %s %s",
2380 &fclock, &mode.hdisplay,
2382 &mode.hsync_end, &mode.htotal,
2385 &mode.vsync_end, &mode.vtotal,
2386 hsync, vsync) == 11) {
2387 if (set_sync_flags(&mode, hsync, vsync))
2390 sprintf(mode_name, "%dx%d", mode.hdisplay, mode.vdisplay);
2391 strcpy(mode.name, mode_name);
2393 mode.clock = fclock * 1000;
2397 output->crtc_mode = mode;
2403 drm_output_set_transform(struct drm_configured_output *output)
2405 if (!output_transform) {
2406 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
2410 if (!strcmp(output_transform, "normal"))
2411 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
2412 else if (!strcmp(output_transform, "90"))
2413 output->transform = WL_OUTPUT_TRANSFORM_90;
2414 else if (!strcmp(output_transform, "180"))
2415 output->transform = WL_OUTPUT_TRANSFORM_180;
2416 else if (!strcmp(output_transform, "270"))
2417 output->transform = WL_OUTPUT_TRANSFORM_270;
2418 else if (!strcmp(output_transform, "flipped"))
2419 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED;
2420 else if (!strcmp(output_transform, "flipped-90"))
2421 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_90;
2422 else if (!strcmp(output_transform, "flipped-180"))
2423 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_180;
2424 else if (!strcmp(output_transform, "flipped-270"))
2425 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_270;
2427 weston_log("Invalid transform \"%s\" for output %s\n",
2428 output_transform, output_name);
2429 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
2432 free(output_transform);
2433 output_transform = NULL;
2437 output_section_done(void *data)
2439 struct drm_configured_output *output;
2441 output = malloc(sizeof *output);
2443 if (!output || !output_name || (output_name[0] == 'X') ||
2444 (!output_mode && !output_transform)) {
2447 free(output_transform);
2450 output_transform = NULL;
2454 output->config = OUTPUT_CONFIG_INVALID;
2455 output->name = output_name;
2456 output->mode = output_mode;
2459 if (strcmp(output_mode, "off") == 0)
2460 output->config = OUTPUT_CONFIG_OFF;
2461 else if (strcmp(output_mode, "preferred") == 0)
2462 output->config = OUTPUT_CONFIG_PREFERRED;
2463 else if (strcmp(output_mode, "current") == 0)
2464 output->config = OUTPUT_CONFIG_CURRENT;
2465 else if (sscanf(output_mode, "%dx%d",
2466 &output->width, &output->height) == 2)
2467 output->config = OUTPUT_CONFIG_MODE;
2468 else if (check_for_modeline(output) == 0)
2469 output->config = OUTPUT_CONFIG_MODELINE;
2471 if (output->config == OUTPUT_CONFIG_INVALID)
2472 weston_log("Invalid mode \"%s\" for output %s\n",
2473 output_mode, output_name);
2477 drm_output_set_transform(output);
2479 wl_list_insert(&configured_output_list, &output->link);
2481 if (output_transform)
2482 free(output_transform);
2483 output_transform = NULL;
2486 WL_EXPORT struct weston_compositor *
2487 backend_init(struct wl_display *display, int argc, char *argv[],
2488 const char *config_file)
2490 int connector = 0, tty = 0;
2491 const char *seat = default_seat;
2493 const struct weston_option drm_options[] = {
2494 { WESTON_OPTION_INTEGER, "connector", 0, &connector },
2495 { WESTON_OPTION_STRING, "seat", 0, &seat },
2496 { WESTON_OPTION_INTEGER, "tty", 0, &tty },
2497 { WESTON_OPTION_BOOLEAN, "current-mode", 0, &option_current_mode },
2500 parse_options(drm_options, ARRAY_LENGTH(drm_options), argc, argv);
2502 wl_list_init(&configured_output_list);
2504 const struct config_key drm_config_keys[] = {
2505 { "name", CONFIG_KEY_STRING, &output_name },
2506 { "mode", CONFIG_KEY_STRING, &output_mode },
2507 { "transform", CONFIG_KEY_STRING, &output_transform },
2510 const struct config_section config_section[] = {
2511 { "output", drm_config_keys,
2512 ARRAY_LENGTH(drm_config_keys), output_section_done },
2515 parse_config_file(config_file, config_section,
2516 ARRAY_LENGTH(config_section), NULL);
2518 return drm_compositor_create(display, connector, seat, tty, argc, argv,