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 /* we need these parameters in order to not fail drmModeAddFB2()
92 * due to out of bounds dimensions, and then mistakenly set
95 int32_t min_width, max_width;
96 int32_t min_height, max_height;
98 struct wl_list sprite_list;
99 int sprites_are_broken;
102 int cursors_are_broken;
108 struct weston_mode base;
109 drmModeModeInfo mode_info;
116 struct drm_output *output;
118 int is_client_buffer;
119 struct wl_buffer *buffer;
120 struct wl_listener buffer_destroy_listener;
124 struct weston_output base;
129 uint32_t connector_id;
130 drmModeCrtcPtr original_crtc;
133 int page_flip_pending;
135 struct gbm_surface *surface;
136 struct gbm_bo *cursor_bo[2];
137 struct weston_plane cursor_plane;
138 struct weston_plane fb_plane;
139 struct weston_surface *cursor_surface;
141 struct drm_fb *current, *next;
142 struct backlight *backlight;
146 * An output has a primary display plane plus zero or more sprites for
147 * blending display contents.
153 uint32_t pending_fb_id;
154 struct weston_surface *surface;
155 struct weston_surface *pending_surface;
156 struct weston_plane plane;
158 struct drm_output *output;
160 struct drm_compositor *compositor;
162 struct wl_listener destroy_listener;
163 struct wl_listener pending_destroy_listener;
165 uint32_t possible_crtcs;
167 uint32_t count_formats;
169 int32_t src_x, src_y;
170 uint32_t src_w, src_h;
171 uint32_t dest_x, dest_y;
172 uint32_t dest_w, dest_h;
178 struct weston_seat base;
179 struct wl_list devices_list;
180 struct udev_monitor *udev_monitor;
181 struct wl_event_source *udev_monitor_source;
186 drm_output_set_cursor(struct drm_output *output);
189 drm_sprite_crtc_supported(struct weston_output *output_base, uint32_t supported)
191 struct weston_compositor *ec = output_base->compositor;
192 struct drm_compositor *c =(struct drm_compositor *) ec;
193 struct drm_output *output = (struct drm_output *) output_base;
196 for (crtc = 0; crtc < c->num_crtcs; crtc++) {
197 if (c->crtcs[crtc] != output->crtc_id)
200 if (supported & (1 << crtc))
208 drm_fb_destroy_callback(struct gbm_bo *bo, void *data)
210 struct drm_fb *fb = data;
211 struct gbm_device *gbm = gbm_bo_get_device(bo);
214 drmModeRmFB(gbm_device_get_fd(gbm), fb->fb_id);
217 weston_buffer_post_release(fb->buffer);
218 wl_list_remove(&fb->buffer_destroy_listener.link);
224 static struct drm_fb *
225 drm_fb_get_from_bo(struct gbm_bo *bo, struct drm_output *output)
227 struct drm_fb *fb = gbm_bo_get_user_data(bo);
228 struct drm_compositor *compositor =
229 (struct drm_compositor *) output->base.compositor;
230 uint32_t width, height, stride, handle;
236 fb = malloc(sizeof *fb);
240 fb->is_client_buffer = 0;
243 width = gbm_bo_get_width(bo);
244 height = gbm_bo_get_height(bo);
245 stride = gbm_bo_get_stride(bo);
246 handle = gbm_bo_get_handle(bo).u32;
248 ret = drmModeAddFB(compositor->drm.fd, width, height, 24, 32,
249 stride, handle, &fb->fb_id);
251 weston_log("failed to create kms fb: %m\n");
256 gbm_bo_set_user_data(bo, fb, drm_fb_destroy_callback);
262 fb_handle_buffer_destroy(struct wl_listener *listener, void *data)
264 struct drm_fb *fb = container_of(listener, struct drm_fb,
265 buffer_destroy_listener);
269 if (fb == fb->output->next ||
270 (fb == fb->output->current && !fb->output->next))
271 weston_output_schedule_repaint(&fb->output->base);
275 drm_output_check_scanout_format(struct drm_output *output,
276 struct weston_surface *es, struct gbm_bo *bo)
282 format = gbm_bo_get_format(bo);
284 if (format == GBM_FORMAT_XRGB8888)
286 else if (format == GBM_FORMAT_ARGB8888) {
287 /* We can only scanout an ARGB buffer if the surface's
288 * opaque region covers the whole output */
289 pixman_region32_init(&r);
290 pixman_region32_subtract(&r, &output->base.region,
293 if (!pixman_region32_not_empty(&r))
296 pixman_region32_fini(&r);
302 static struct weston_plane *
303 drm_output_prepare_scanout_surface(struct weston_output *_output,
304 struct weston_surface *es)
306 struct drm_output *output = (struct drm_output *) _output;
307 struct drm_compositor *c =
308 (struct drm_compositor *) output->base.compositor;
311 if (es->geometry.x != output->base.x ||
312 es->geometry.y != output->base.y ||
313 es->geometry.width != output->base.current->width ||
314 es->geometry.height != output->base.current->height ||
315 es->transform.enabled ||
319 if (wl_buffer_is_shm(es->buffer))
322 bo = gbm_bo_import(c->gbm, GBM_BO_IMPORT_WL_BUFFER,
323 es->buffer, GBM_BO_USE_SCANOUT);
325 /* Unable to use the buffer for scanout */
329 if (!drm_output_check_scanout_format(output, es, bo)) {
334 output->next = drm_fb_get_from_bo(bo, output);
340 output->next->is_client_buffer = 1;
341 output->next->buffer = es->buffer;
342 output->next->buffer->busy_count++;
343 output->next->buffer_destroy_listener.notify = fb_handle_buffer_destroy;
345 wl_signal_add(&output->next->buffer->resource.destroy_signal,
346 &output->next->buffer_destroy_listener);
348 return &output->fb_plane;
352 drm_output_render(struct drm_output *output, pixman_region32_t *damage)
354 struct weston_compositor *ec = output->base.compositor;
357 ec->renderer->repaint_output(&output->base, damage);
359 pixman_region32_subtract(&ec->primary_plane.damage,
360 &ec->primary_plane.damage, damage);
362 bo = gbm_surface_lock_front_buffer(output->surface);
364 weston_log("failed to lock front buffer: %m\n");
368 output->next = drm_fb_get_from_bo(bo, output);
370 weston_log("failed to get drm_fb for bo\n");
371 gbm_surface_release_buffer(output->surface, bo);
377 drm_output_repaint(struct weston_output *output_base,
378 pixman_region32_t *damage)
380 struct drm_output *output = (struct drm_output *) output_base;
381 struct drm_compositor *compositor =
382 (struct drm_compositor *) output->base.compositor;
383 struct drm_sprite *s;
384 struct drm_mode *mode;
388 drm_output_render(output, damage);
392 mode = container_of(output->base.current, struct drm_mode, base);
393 if (!output->current) {
394 ret = drmModeSetCrtc(compositor->drm.fd, output->crtc_id,
395 output->next->fb_id, 0, 0,
396 &output->connector_id, 1,
399 weston_log("set mode failed: %m\n");
404 if (drmModePageFlip(compositor->drm.fd, output->crtc_id,
406 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
407 weston_log("queueing pageflip failed: %m\n");
411 output->page_flip_pending = 1;
413 drm_output_set_cursor(output);
416 * Now, update all the sprite surfaces
418 wl_list_for_each(s, &compositor->sprite_list, link) {
421 .request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT,
422 .request.sequence = 1,
425 if (!drm_sprite_crtc_supported(output_base, s->possible_crtcs))
428 ret = drmModeSetPlane(compositor->drm.fd, s->plane_id,
430 compositor->sprites_hidden ?
431 0 : s->pending_fb_id,
433 s->dest_x, s->dest_y,
434 s->dest_w, s->dest_h,
438 weston_log("setplane failed: %d: %s\n",
439 ret, strerror(errno));
441 if (output->pipe > 0)
442 vbl.request.type |= DRM_VBLANK_SECONDARY;
445 * Queue a vblank signal so we know when the surface
446 * becomes active on the display or has been replaced.
448 vbl.request.signal = (unsigned long)s;
449 ret = drmWaitVBlank(compositor->drm.fd, &vbl);
451 weston_log("vblank event request failed: %d: %s\n",
452 ret, strerror(errno));
456 output->vblank_pending = 1;
463 vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec,
466 struct drm_sprite *s = (struct drm_sprite *)data;
467 struct drm_compositor *c = s->compositor;
468 struct drm_output *output = s->output;
471 output->vblank_pending = 0;
474 weston_buffer_post_release(s->surface->buffer);
475 wl_list_remove(&s->destroy_listener.link);
477 drmModeRmFB(c->drm.fd, s->fb_id);
481 if (s->pending_surface) {
482 wl_list_remove(&s->pending_destroy_listener.link);
483 wl_signal_add(&s->pending_surface->buffer->resource.destroy_signal,
484 &s->destroy_listener);
485 s->surface = s->pending_surface;
486 s->pending_surface = NULL;
487 s->fb_id = s->pending_fb_id;
488 s->pending_fb_id = 0;
491 if (!output->page_flip_pending) {
492 msecs = sec * 1000 + usec / 1000;
493 weston_output_finish_frame(&output->base, msecs);
498 page_flip_handler(int fd, unsigned int frame,
499 unsigned int sec, unsigned int usec, void *data)
501 struct drm_output *output = (struct drm_output *) data;
504 output->page_flip_pending = 0;
506 if (output->current) {
507 if (output->current->is_client_buffer)
508 gbm_bo_destroy(output->current->bo);
510 gbm_surface_release_buffer(output->surface,
511 output->current->bo);
514 output->current = output->next;
517 if (!output->vblank_pending) {
518 msecs = sec * 1000 + usec / 1000;
519 weston_output_finish_frame(&output->base, msecs);
524 drm_surface_format_supported(struct drm_sprite *s, uint32_t format)
528 for (i = 0; i < s->count_formats; i++)
529 if (s->formats[i] == format)
536 drm_surface_transform_supported(struct weston_surface *es)
538 struct weston_matrix *matrix = &es->transform.matrix;
541 if (!es->transform.enabled)
544 for (i = 0; i < 16; i++) {
548 if (matrix->d[i] != 1.0)
557 if (matrix->d[i] != 0.0)
566 static struct weston_plane *
567 drm_output_prepare_overlay_surface(struct weston_output *output_base,
568 struct weston_surface *es)
570 struct weston_compositor *ec = output_base->compositor;
571 struct drm_compositor *c =(struct drm_compositor *) ec;
572 struct drm_sprite *s;
574 EGLint handle, stride;
577 uint32_t handles[4], pitches[4], offsets[4];
579 pixman_region32_t dest_rect, src_rect;
582 wl_fixed_t sx1, sy1, sx2, sy2;
583 int32_t width, height;
585 if (c->sprites_are_broken)
588 if (es->output_mask != (1u << output_base->id))
591 if (es->buffer == NULL)
594 if (es->alpha != 1.0f)
597 if (wl_buffer_is_shm(es->buffer))
600 if (!drm_surface_transform_supported(es))
603 wl_list_for_each(s, &c->sprite_list, link) {
604 if (!drm_sprite_crtc_supported(output_base, s->possible_crtcs))
607 if (!s->pending_fb_id) {
613 /* No sprites available */
617 width = es->geometry.width;
618 height = es->geometry.height;
620 /* If geometry is out of bounds, don't even bother trying because
621 * we know the AddFB2() call will fail:
623 if (c->min_width > width || width > c->max_width ||
624 c->min_height > height || height > c->max_height)
627 bo = gbm_bo_import(c->gbm, GBM_BO_IMPORT_WL_BUFFER,
628 es->buffer, GBM_BO_USE_SCANOUT);
632 format = gbm_bo_get_format(bo);
633 handle = gbm_bo_get_handle(bo).s32;
634 stride = gbm_bo_get_stride(bo);
638 if (!drm_surface_format_supported(s, format))
648 ret = drmModeAddFB2(c->drm.fd, width, height,
649 format, handles, pitches, offsets,
652 weston_log("addfb2 failed: %d\n", ret);
653 c->sprites_are_broken = 1;
657 s->pending_fb_id = fb_id;
658 s->pending_surface = es;
659 es->buffer->busy_count++;
661 box = pixman_region32_extents(&es->transform.boundingbox);
662 s->plane.x = box->x1;
663 s->plane.y = box->y1;
666 * Calculate the source & dest rects properly based on actual
667 * position (note the caller has called weston_surface_update_transform()
670 pixman_region32_init(&dest_rect);
671 pixman_region32_intersect(&dest_rect, &es->transform.boundingbox,
672 &output_base->region);
673 pixman_region32_translate(&dest_rect, -output_base->x, -output_base->y);
674 box = pixman_region32_extents(&dest_rect);
677 s->dest_w = box->x2 - box->x1;
678 s->dest_h = box->y2 - box->y1;
679 pixman_region32_fini(&dest_rect);
681 pixman_region32_init(&src_rect);
682 pixman_region32_intersect(&src_rect, &es->transform.boundingbox,
683 &output_base->region);
684 box = pixman_region32_extents(&src_rect);
686 weston_surface_from_global_fixed(es,
687 wl_fixed_from_int(box->x1),
688 wl_fixed_from_int(box->y1),
690 weston_surface_from_global_fixed(es,
691 wl_fixed_from_int(box->x2),
692 wl_fixed_from_int(box->y2),
699 if (sx2 > wl_fixed_from_int(es->geometry.width))
700 sx2 = wl_fixed_from_int(es->geometry.width);
701 if (sy2 > wl_fixed_from_int(es->geometry.height))
702 sy2 = wl_fixed_from_int(es->geometry.height);
706 s->src_w = (sx2 - sx1) << 8;
707 s->src_h = (sy2 - sy1) << 8;
708 pixman_region32_fini(&src_rect);
710 wl_signal_add(&es->buffer->resource.destroy_signal,
711 &s->pending_destroy_listener);
716 static struct weston_plane *
717 drm_output_prepare_cursor_surface(struct weston_output *output_base,
718 struct weston_surface *es)
720 struct drm_compositor *c =
721 (struct drm_compositor *) output_base->compositor;
722 struct drm_output *output = (struct drm_output *) output_base;
724 if (output->cursor_surface)
726 if (es->output_mask != (1u << output_base->id))
728 if (c->cursors_are_broken)
730 if (es->buffer == NULL || !wl_buffer_is_shm(es->buffer) ||
731 es->geometry.width > 64 || es->geometry.height > 64)
734 output->cursor_surface = es;
736 return &output->cursor_plane;
740 drm_output_set_cursor(struct drm_output *output)
742 struct weston_surface *es = output->cursor_surface;
743 struct drm_compositor *c =
744 (struct drm_compositor *) output->base.compositor;
745 EGLint handle, stride;
747 uint32_t buf[64 * 64];
751 output->cursor_surface = NULL;
753 drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0);
757 if (es->buffer && pixman_region32_not_empty(&output->cursor_plane.damage)) {
758 pixman_region32_fini(&output->cursor_plane.damage);
759 pixman_region32_init(&output->cursor_plane.damage);
760 output->current_cursor ^= 1;
761 bo = output->cursor_bo[output->current_cursor];
762 memset(buf, 0, sizeof buf);
763 stride = wl_shm_buffer_get_stride(es->buffer);
764 s = wl_shm_buffer_get_data(es->buffer);
765 for (i = 0; i < es->geometry.height; i++)
766 memcpy(buf + i * 64, s + i * stride,
767 es->geometry.width * 4);
769 if (gbm_bo_write(bo, buf, sizeof buf) < 0)
770 weston_log("failed update cursor: %m\n");
772 handle = gbm_bo_get_handle(bo).s32;
773 if (drmModeSetCursor(c->drm.fd,
774 output->crtc_id, handle, 64, 64)) {
775 weston_log("failed to set cursor: %m\n");
776 c->cursors_are_broken = 1;
780 x = es->geometry.x - output->base.x;
781 y = es->geometry.y - output->base.y;
782 if (output->cursor_plane.x != x || output->cursor_plane.y != y) {
783 if (drmModeMoveCursor(c->drm.fd, output->crtc_id, x, y)) {
784 weston_log("failed to move cursor: %m\n");
785 c->cursors_are_broken = 1;
788 output->cursor_plane.x = x;
789 output->cursor_plane.y = y;
794 drm_assign_planes(struct weston_output *output)
796 struct drm_compositor *c =
797 (struct drm_compositor *) output->compositor;
798 struct drm_output *drm_output = (struct drm_output *) output;
799 struct drm_sprite *s;
800 struct weston_surface *es, *next;
801 pixman_region32_t overlap, surface_overlap;
802 struct weston_plane *primary, *next_plane;
804 /* Reset the opaque region of the planes */
805 pixman_region32_fini(&drm_output->cursor_plane.opaque);
806 pixman_region32_init(&drm_output->cursor_plane.opaque);
807 pixman_region32_fini(&drm_output->fb_plane.opaque);
808 pixman_region32_init(&drm_output->fb_plane.opaque);
810 wl_list_for_each (s, &c->sprite_list, link) {
811 if (!drm_sprite_crtc_supported(output, s->possible_crtcs))
814 pixman_region32_fini(&s->plane.opaque);
815 pixman_region32_init(&s->plane.opaque);
819 * Find a surface for each sprite in the output using some heuristics:
821 * 2) frequency of update
822 * 3) opacity (though some hw might support alpha blending)
823 * 4) clipping (this can be fixed with color keys)
825 * The idea is to save on blitting since this should save power.
826 * If we can get a large video surface on the sprite for example,
827 * the main display surface may not need to update at all, and
828 * the client buffer can be used directly for the sprite surface
829 * as we do for flipping full screen surfaces.
831 pixman_region32_init(&overlap);
832 primary = &c->base.primary_plane;
833 wl_list_for_each_safe(es, next, &c->base.surface_list, link) {
834 pixman_region32_init(&surface_overlap);
835 pixman_region32_intersect(&surface_overlap, &overlap,
836 &es->transform.boundingbox);
839 if (pixman_region32_not_empty(&surface_overlap))
840 next_plane = primary;
841 if (next_plane == NULL)
842 next_plane = drm_output_prepare_cursor_surface(output, es);
843 if (next_plane == NULL)
844 next_plane = drm_output_prepare_scanout_surface(output, es);
845 if (next_plane == NULL)
846 next_plane = drm_output_prepare_overlay_surface(output, es);
847 if (next_plane == NULL)
848 next_plane = primary;
849 weston_surface_move_to_plane(es, next_plane);
850 if (next_plane == primary)
851 pixman_region32_union(&overlap, &overlap,
852 &es->transform.boundingbox);
854 pixman_region32_fini(&surface_overlap);
856 pixman_region32_fini(&overlap);
860 drm_output_destroy(struct weston_output *output_base)
862 struct drm_output *output = (struct drm_output *) output_base;
863 struct drm_compositor *c =
864 (struct drm_compositor *) output->base.compositor;
865 drmModeCrtcPtr origcrtc = output->original_crtc;
867 if (output->backlight)
868 backlight_destroy(output->backlight);
870 /* Turn off hardware cursor */
871 drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0);
873 /* Restore original CRTC state */
874 drmModeSetCrtc(c->drm.fd, origcrtc->crtc_id, origcrtc->buffer_id,
875 origcrtc->x, origcrtc->y,
876 &output->connector_id, 1, &origcrtc->mode);
877 drmModeFreeCrtc(origcrtc);
879 c->crtc_allocator &= ~(1 << output->crtc_id);
880 c->connector_allocator &= ~(1 << output->connector_id);
882 eglDestroySurface(c->base.egl_display, output->base.egl_surface);
883 gbm_surface_destroy(output->surface);
885 weston_plane_release(&output->fb_plane);
886 weston_plane_release(&output->cursor_plane);
888 weston_output_destroy(&output->base);
889 wl_list_remove(&output->base.link);
895 static struct drm_mode *
896 choose_mode (struct drm_output *output, struct weston_mode *target_mode)
898 struct drm_mode *tmp_mode = NULL, *mode;
900 if (output->base.current->width == target_mode->width &&
901 output->base.current->height == target_mode->height &&
902 (output->base.current->refresh == target_mode->refresh ||
903 target_mode->refresh == 0))
904 return (struct drm_mode *)output->base.current;
906 wl_list_for_each(mode, &output->base.mode_list, base.link) {
907 if (mode->mode_info.hdisplay == target_mode->width &&
908 mode->mode_info.vdisplay == target_mode->height) {
909 if (mode->mode_info.vrefresh == target_mode->refresh ||
910 target_mode->refresh == 0) {
912 } else if (!tmp_mode)
921 drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mode)
923 struct drm_output *output;
924 struct drm_mode *drm_mode;
926 struct drm_compositor *ec;
927 struct gbm_surface *surface;
928 EGLSurface egl_surface;
930 if (output_base == NULL) {
931 weston_log("output is NULL.\n");
936 weston_log("mode is NULL.\n");
940 ec = (struct drm_compositor *)output_base->compositor;
941 output = (struct drm_output *)output_base;
942 drm_mode = choose_mode (output, mode);
945 weston_log("%s, invalid resolution:%dx%d\n", __func__, mode->width, mode->height);
947 } else if (&drm_mode->base == output->base.current) {
949 } else if (drm_mode->base.width == output->base.current->width &&
950 drm_mode->base.height == output->base.current->height) {
951 /* only change refresh value */
952 ret = drmModeSetCrtc(ec->drm.fd,
954 output->current->fb_id, 0, 0,
955 &output->connector_id, 1, &drm_mode->mode_info);
958 weston_log("failed to set mode (%dx%d) %u Hz\n",
959 drm_mode->base.width,
960 drm_mode->base.height,
961 drm_mode->base.refresh / 1000);
964 output->base.current->flags = 0;
965 output->base.current = &drm_mode->base;
966 drm_mode->base.flags =
967 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
974 drm_mode->base.flags =
975 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
977 surface = gbm_surface_create(ec->gbm,
978 drm_mode->base.width,
979 drm_mode->base.height,
982 GBM_BO_USE_RENDERING);
984 weston_log("failed to create gbm surface\n");
989 eglCreateWindowSurface(ec->base.egl_display,
993 if (egl_surface == EGL_NO_SURFACE) {
994 weston_log("failed to create egl surface\n");
998 ret = drmModeSetCrtc(ec->drm.fd,
1000 output->current->fb_id, 0, 0,
1001 &output->connector_id, 1, &drm_mode->mode_info);
1003 weston_log("failed to set mode\n");
1007 /* reset rendering stuff. */
1008 if (output->current) {
1009 if (output->current->is_client_buffer)
1010 gbm_bo_destroy(output->current->bo);
1012 gbm_surface_release_buffer(output->surface,
1013 output->current->bo);
1015 output->current = NULL;
1018 if (output->next->is_client_buffer)
1019 gbm_bo_destroy(output->next->bo);
1021 gbm_surface_release_buffer(output->surface,
1024 output->next = NULL;
1026 eglDestroySurface(ec->base.egl_display, output->base.egl_surface);
1027 gbm_surface_destroy(output->surface);
1028 output->base.egl_surface = egl_surface;
1029 output->surface = surface;
1032 output->base.current = &drm_mode->base;
1033 output->base.dirty = 1;
1034 weston_output_move(&output->base, output->base.x, output->base.y);
1038 eglDestroySurface(ec->base.egl_display, egl_surface);
1039 gbm_surface_destroy(surface);
1044 on_drm_input(int fd, uint32_t mask, void *data)
1046 drmEventContext evctx;
1048 memset(&evctx, 0, sizeof evctx);
1049 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1050 evctx.page_flip_handler = page_flip_handler;
1051 evctx.vblank_handler = vblank_handler;
1052 drmHandleEvent(fd, &evctx);
1058 init_egl(struct drm_compositor *ec, struct udev_device *device)
1060 EGLint major, minor, n;
1061 const char *filename, *sysnum;
1063 static const EGLint config_attribs[] = {
1064 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
1069 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
1073 sysnum = udev_device_get_sysnum(device);
1075 ec->drm.id = atoi(sysnum);
1076 if (!sysnum || ec->drm.id < 0) {
1077 weston_log("cannot get device sysnum\n");
1081 filename = udev_device_get_devnode(device);
1082 fd = open(filename, O_RDWR | O_CLOEXEC);
1084 /* Probably permissions error */
1085 weston_log("couldn't open %s, skipping\n",
1086 udev_device_get_devnode(device));
1090 weston_log("using %s\n", filename);
1093 ec->gbm = gbm_create_device(ec->drm.fd);
1094 ec->base.egl_display = eglGetDisplay(ec->gbm);
1095 if (ec->base.egl_display == NULL) {
1096 weston_log("failed to create display\n");
1100 if (!eglInitialize(ec->base.egl_display, &major, &minor)) {
1101 weston_log("failed to initialize display\n");
1105 if (!eglChooseConfig(ec->base.egl_display, config_attribs,
1106 &ec->base.egl_config, 1, &n) || n != 1) {
1107 weston_log("failed to choose config: %d\n", n);
1114 static struct drm_mode *
1115 drm_output_add_mode(struct drm_output *output, drmModeModeInfo *info)
1117 struct drm_mode *mode;
1120 mode = malloc(sizeof *mode);
1124 mode->base.flags = 0;
1125 mode->base.width = info->hdisplay;
1126 mode->base.height = info->vdisplay;
1128 /* Calculate higher precision (mHz) refresh rate */
1129 refresh = (info->clock * 1000000LL / info->htotal +
1130 info->vtotal / 2) / info->vtotal;
1132 if (info->flags & DRM_MODE_FLAG_INTERLACE)
1134 if (info->flags & DRM_MODE_FLAG_DBLSCAN)
1136 if (info->vscan > 1)
1137 refresh /= info->vscan;
1139 mode->base.refresh = refresh;
1140 mode->mode_info = *info;
1142 if (info->type & DRM_MODE_TYPE_PREFERRED)
1143 mode->base.flags |= WL_OUTPUT_MODE_PREFERRED;
1145 wl_list_insert(output->base.mode_list.prev, &mode->base.link);
1151 drm_subpixel_to_wayland(int drm_value)
1153 switch (drm_value) {
1155 case DRM_MODE_SUBPIXEL_UNKNOWN:
1156 return WL_OUTPUT_SUBPIXEL_UNKNOWN;
1157 case DRM_MODE_SUBPIXEL_NONE:
1158 return WL_OUTPUT_SUBPIXEL_NONE;
1159 case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB:
1160 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB;
1161 case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR:
1162 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR;
1163 case DRM_MODE_SUBPIXEL_VERTICAL_RGB:
1164 return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB;
1165 case DRM_MODE_SUBPIXEL_VERTICAL_BGR:
1166 return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR;
1171 sprite_handle_buffer_destroy(struct wl_listener *listener, void *data)
1173 struct drm_sprite *sprite =
1174 container_of(listener, struct drm_sprite,
1176 struct drm_compositor *compositor = sprite->compositor;
1178 sprite->surface = NULL;
1179 drmModeRmFB(compositor->drm.fd, sprite->fb_id);
1184 sprite_handle_pending_buffer_destroy(struct wl_listener *listener, void *data)
1186 struct drm_sprite *sprite =
1187 container_of(listener, struct drm_sprite,
1188 pending_destroy_listener);
1189 struct drm_compositor *compositor = sprite->compositor;
1191 sprite->pending_surface = NULL;
1192 drmModeRmFB(compositor->drm.fd, sprite->pending_fb_id);
1193 sprite->pending_fb_id = 0;
1196 /* returns a value between 0-255 range, where higher is brighter */
1198 drm_get_backlight(struct drm_output *output)
1200 long brightness, max_brightness, norm;
1202 brightness = backlight_get_brightness(output->backlight);
1203 max_brightness = backlight_get_max_brightness(output->backlight);
1205 /* convert it on a scale of 0 to 255 */
1206 norm = (brightness * 255)/(max_brightness);
1208 return (uint32_t) norm;
1211 /* values accepted are between 0-255 range */
1213 drm_set_backlight(struct weston_output *output_base, uint32_t value)
1215 struct drm_output *output = (struct drm_output *) output_base;
1216 long max_brightness, new_brightness;
1218 if (!output->backlight)
1224 max_brightness = backlight_get_max_brightness(output->backlight);
1226 /* get denormalized value */
1227 new_brightness = (value * max_brightness) / 255;
1229 backlight_set_brightness(output->backlight, new_brightness);
1232 static drmModePropertyPtr
1233 drm_get_prop(int fd, drmModeConnectorPtr connector, const char *name)
1235 drmModePropertyPtr props;
1238 for (i = 0; i < connector->count_props; i++) {
1239 props = drmModeGetProperty(fd, connector->props[i]);
1243 if (!strcmp(props->name, name))
1246 drmModeFreeProperty(props);
1253 drm_set_dpms(struct weston_output *output_base, enum dpms_enum level)
1255 struct drm_output *output = (struct drm_output *) output_base;
1256 struct weston_compositor *ec = output_base->compositor;
1257 struct drm_compositor *c = (struct drm_compositor *) ec;
1258 drmModeConnectorPtr connector;
1259 drmModePropertyPtr prop;
1261 connector = drmModeGetConnector(c->drm.fd, output->connector_id);
1265 prop = drm_get_prop(c->drm.fd, connector, "DPMS");
1267 drmModeFreeConnector(connector);
1271 drmModeConnectorSetProperty(c->drm.fd, connector->connector_id,
1272 prop->prop_id, level);
1273 drmModeFreeProperty(prop);
1274 drmModeFreeConnector(connector);
1277 static const char *connector_type_names[] = {
1296 find_crtc_for_connector(struct drm_compositor *ec,
1297 drmModeRes *resources, drmModeConnector *connector)
1299 drmModeEncoder *encoder;
1300 uint32_t possible_crtcs;
1303 for (j = 0; j < connector->count_encoders; j++) {
1304 encoder = drmModeGetEncoder(ec->drm.fd, connector->encoders[j]);
1305 if (encoder == NULL) {
1306 weston_log("Failed to get encoder.\n");
1309 possible_crtcs = encoder->possible_crtcs;
1310 drmModeFreeEncoder(encoder);
1312 for (i = 0; i < resources->count_crtcs; i++) {
1313 if (possible_crtcs & (1 << i) &&
1314 !(ec->crtc_allocator & (1 << resources->crtcs[i])))
1323 create_output_for_connector(struct drm_compositor *ec,
1324 drmModeRes *resources,
1325 drmModeConnector *connector,
1326 int x, int y, struct udev_device *drm_device)
1328 struct drm_output *output;
1329 struct drm_mode *drm_mode, *next, *preferred, *current, *configured;
1330 struct weston_mode *m;
1331 struct drm_configured_output *o = NULL, *temp;
1332 drmModeEncoder *encoder;
1333 drmModeModeInfo crtc_mode;
1337 const char *type_name;
1339 i = find_crtc_for_connector(ec, resources, connector);
1341 weston_log("No usable crtc/encoder pair for connector.\n");
1345 output = malloc(sizeof *output);
1349 memset(output, 0, sizeof *output);
1350 output->base.subpixel = drm_subpixel_to_wayland(connector->subpixel);
1351 output->base.make = "unknown";
1352 output->base.model = "unknown";
1353 wl_list_init(&output->base.mode_list);
1355 if (connector->connector_type < ARRAY_LENGTH(connector_type_names))
1356 type_name = connector_type_names[connector->connector_type];
1358 type_name = "UNKNOWN";
1359 snprintf(name, 32, "%s%d", type_name, connector->connector_type_id);
1360 output->name = strdup(name);
1362 output->crtc_id = resources->crtcs[i];
1364 ec->crtc_allocator |= (1 << output->crtc_id);
1365 output->connector_id = connector->connector_id;
1366 ec->connector_allocator |= (1 << output->connector_id);
1368 output->original_crtc = drmModeGetCrtc(ec->drm.fd, output->crtc_id);
1370 /* Get the current mode on the crtc that's currently driving
1371 * this connector. */
1372 encoder = drmModeGetEncoder(ec->drm.fd, connector->encoder_id);
1373 memset(&crtc_mode, 0, sizeof crtc_mode);
1374 if (encoder != NULL) {
1375 crtc = drmModeGetCrtc(ec->drm.fd, encoder->crtc_id);
1376 drmModeFreeEncoder(encoder);
1379 if (crtc->mode_valid)
1380 crtc_mode = crtc->mode;
1381 drmModeFreeCrtc(crtc);
1384 for (i = 0; i < connector->count_modes; i++) {
1385 drm_mode = drm_output_add_mode(output, &connector->modes[i]);
1394 wl_list_for_each(temp, &configured_output_list, link) {
1395 if (strcmp(temp->name, output->name) == 0) {
1397 weston_log("%s mode \"%s\" in config\n",
1398 temp->name, temp->mode);
1404 if (o && o->config == OUTPUT_CONFIG_OFF) {
1405 weston_log("Disabling output %s\n", o->name);
1407 drmModeSetCrtc(ec->drm.fd, output->crtc_id,
1408 0, 0, 0, 0, 0, NULL);
1412 wl_list_for_each(drm_mode, &output->base.mode_list, base.link) {
1413 if (o && o->config == OUTPUT_CONFIG_MODE &&
1414 o->width == drm_mode->base.width &&
1415 o->height == drm_mode->base.height)
1416 configured = drm_mode;
1417 if (!memcmp(&crtc_mode, &drm_mode->mode_info, sizeof crtc_mode))
1419 if (drm_mode->base.flags & WL_OUTPUT_MODE_PREFERRED)
1420 preferred = drm_mode;
1423 if (o && o->config == OUTPUT_CONFIG_MODELINE) {
1424 configured = drm_output_add_mode(output, &o->crtc_mode);
1427 current = configured;
1430 if (current == NULL && crtc_mode.clock != 0) {
1431 current = drm_output_add_mode(output, &crtc_mode);
1436 if (o && o->config == OUTPUT_CONFIG_CURRENT)
1437 configured = current;
1439 if (option_current_mode && current)
1440 output->base.current = ¤t->base;
1441 else if (configured)
1442 output->base.current = &configured->base;
1444 output->base.current = &preferred->base;
1446 output->base.current = ¤t->base;
1448 if (output->base.current == NULL) {
1449 weston_log("no available modes for %s\n", output->name);
1453 output->base.current->flags |= WL_OUTPUT_MODE_CURRENT;
1455 output->surface = gbm_surface_create(ec->gbm,
1456 output->base.current->width,
1457 output->base.current->height,
1458 GBM_FORMAT_XRGB8888,
1459 GBM_BO_USE_SCANOUT |
1460 GBM_BO_USE_RENDERING);
1461 if (!output->surface) {
1462 weston_log("failed to create gbm surface\n");
1466 output->base.egl_surface =
1467 eglCreateWindowSurface(ec->base.egl_display,
1468 ec->base.egl_config,
1471 if (output->base.egl_surface == EGL_NO_SURFACE) {
1472 weston_log("failed to create egl surface\n");
1476 output->cursor_bo[0] =
1477 gbm_bo_create(ec->gbm, 64, 64, GBM_FORMAT_ARGB8888,
1478 GBM_BO_USE_CURSOR_64X64 | GBM_BO_USE_WRITE);
1479 output->cursor_bo[1] =
1480 gbm_bo_create(ec->gbm, 64, 64, GBM_FORMAT_ARGB8888,
1481 GBM_BO_USE_CURSOR_64X64 | GBM_BO_USE_WRITE);
1482 if (output->cursor_bo[0] == NULL || output->cursor_bo[1] == NULL) {
1483 weston_log("cursor buffers unavailable, using gl cursors\n");
1484 ec->cursors_are_broken = 1;
1487 output->backlight = backlight_init(drm_device,
1488 connector->connector_type);
1489 if (output->backlight) {
1490 output->base.set_backlight = drm_set_backlight;
1491 output->base.backlight_current = drm_get_backlight(output);
1494 weston_output_init(&output->base, &ec->base, x, y,
1495 connector->mmWidth, connector->mmHeight,
1496 o ? o->transform : WL_OUTPUT_TRANSFORM_NORMAL);
1498 wl_list_insert(ec->base.output_list.prev, &output->base.link);
1500 output->base.origin = output->base.current;
1501 output->base.repaint = drm_output_repaint;
1502 output->base.destroy = drm_output_destroy;
1503 output->base.assign_planes = drm_assign_planes;
1504 output->base.set_dpms = drm_set_dpms;
1505 output->base.switch_mode = drm_output_switch_mode;
1507 weston_plane_init(&output->cursor_plane, 0, 0);
1508 weston_plane_init(&output->fb_plane, 0, 0);
1510 weston_log("Output %s, (connector %d, crtc %d)\n",
1511 output->name, output->connector_id, output->crtc_id);
1512 wl_list_for_each(m, &output->base.mode_list, link)
1513 weston_log_continue(" mode %dx%d@%.1f%s%s%s\n",
1514 m->width, m->height, m->refresh / 1000.0,
1515 m->flags & WL_OUTPUT_MODE_PREFERRED ?
1517 m->flags & WL_OUTPUT_MODE_CURRENT ?
1519 connector->count_modes == 0 ?
1525 gbm_surface_destroy(output->surface);
1527 wl_list_for_each_safe(drm_mode, next, &output->base.mode_list,
1529 wl_list_remove(&drm_mode->base.link);
1533 drmModeFreeCrtc(output->original_crtc);
1534 ec->crtc_allocator &= ~(1 << output->crtc_id);
1535 ec->connector_allocator &= ~(1 << output->connector_id);
1543 create_sprites(struct drm_compositor *ec)
1545 struct drm_sprite *sprite;
1546 drmModePlaneRes *plane_res;
1547 drmModePlane *plane;
1550 plane_res = drmModeGetPlaneResources(ec->drm.fd);
1552 weston_log("failed to get plane resources: %s\n",
1557 for (i = 0; i < plane_res->count_planes; i++) {
1558 plane = drmModeGetPlane(ec->drm.fd, plane_res->planes[i]);
1562 sprite = malloc(sizeof(*sprite) + ((sizeof(uint32_t)) *
1563 plane->count_formats));
1565 weston_log("%s: out of memory\n",
1571 memset(sprite, 0, sizeof *sprite);
1573 sprite->possible_crtcs = plane->possible_crtcs;
1574 sprite->plane_id = plane->plane_id;
1575 sprite->surface = NULL;
1576 sprite->pending_surface = NULL;
1578 sprite->pending_fb_id = 0;
1579 sprite->destroy_listener.notify = sprite_handle_buffer_destroy;
1580 sprite->pending_destroy_listener.notify =
1581 sprite_handle_pending_buffer_destroy;
1582 sprite->compositor = ec;
1583 sprite->count_formats = plane->count_formats;
1584 memcpy(sprite->formats, plane->formats,
1585 plane->count_formats * sizeof(plane->formats[0]));
1586 drmModeFreePlane(plane);
1587 weston_plane_init(&sprite->plane, 0, 0);
1589 wl_list_insert(&ec->sprite_list, &sprite->link);
1592 free(plane_res->planes);
1597 destroy_sprites(struct drm_compositor *compositor)
1599 struct drm_sprite *sprite, *next;
1600 struct drm_output *output;
1602 output = container_of(compositor->base.output_list.next,
1603 struct drm_output, base.link);
1605 wl_list_for_each_safe(sprite, next, &compositor->sprite_list, link) {
1606 drmModeSetPlane(compositor->drm.fd,
1608 output->crtc_id, 0, 0,
1609 0, 0, 0, 0, 0, 0, 0, 0);
1610 drmModeRmFB(compositor->drm.fd, sprite->fb_id);
1611 weston_plane_release(&sprite->plane);
1617 create_outputs(struct drm_compositor *ec, uint32_t option_connector,
1618 struct udev_device *drm_device)
1620 drmModeConnector *connector;
1621 drmModeRes *resources;
1625 resources = drmModeGetResources(ec->drm.fd);
1627 weston_log("drmModeGetResources failed\n");
1631 ec->crtcs = calloc(resources->count_crtcs, sizeof(uint32_t));
1633 drmModeFreeResources(resources);
1637 ec->min_width = resources->min_width;
1638 ec->max_width = resources->max_width;
1639 ec->min_height = resources->min_height;
1640 ec->max_height = resources->max_height;
1642 ec->num_crtcs = resources->count_crtcs;
1643 memcpy(ec->crtcs, resources->crtcs, sizeof(uint32_t) * ec->num_crtcs);
1645 for (i = 0; i < resources->count_connectors; i++) {
1646 connector = drmModeGetConnector(ec->drm.fd,
1647 resources->connectors[i]);
1648 if (connector == NULL)
1651 if (connector->connection == DRM_MODE_CONNECTED &&
1652 (option_connector == 0 ||
1653 connector->connector_id == option_connector)) {
1654 if (create_output_for_connector(ec, resources,
1657 drmModeFreeConnector(connector);
1661 x += container_of(ec->base.output_list.prev,
1662 struct weston_output,
1666 drmModeFreeConnector(connector);
1669 if (wl_list_empty(&ec->base.output_list)) {
1670 weston_log("No currently active connector found.\n");
1671 drmModeFreeResources(resources);
1675 drmModeFreeResources(resources);
1681 update_outputs(struct drm_compositor *ec, struct udev_device *drm_device)
1683 drmModeConnector *connector;
1684 drmModeRes *resources;
1685 struct drm_output *output, *next;
1687 int x_offset = 0, y_offset = 0;
1688 uint32_t connected = 0, disconnects = 0;
1691 resources = drmModeGetResources(ec->drm.fd);
1693 weston_log("drmModeGetResources failed\n");
1697 /* collect new connects */
1698 for (i = 0; i < resources->count_connectors; i++) {
1699 int connector_id = resources->connectors[i];
1701 connector = drmModeGetConnector(ec->drm.fd, connector_id);
1702 if (connector == NULL)
1705 if (connector->connection != DRM_MODE_CONNECTED) {
1706 drmModeFreeConnector(connector);
1710 connected |= (1 << connector_id);
1712 if (!(ec->connector_allocator & (1 << connector_id))) {
1713 struct weston_output *last =
1714 container_of(ec->base.output_list.prev,
1715 struct weston_output, link);
1717 /* XXX: not yet needed, we die with 0 outputs */
1718 if (!wl_list_empty(&ec->base.output_list))
1719 x = last->x + last->width;
1723 create_output_for_connector(ec, resources,
1726 weston_log("connector %d connected\n", connector_id);
1729 drmModeFreeConnector(connector);
1731 drmModeFreeResources(resources);
1733 disconnects = ec->connector_allocator & ~connected;
1735 wl_list_for_each_safe(output, next, &ec->base.output_list,
1737 if (x_offset != 0 || y_offset != 0) {
1738 weston_output_move(&output->base,
1739 output->base.x - x_offset,
1740 output->base.y - y_offset);
1743 if (disconnects & (1 << output->connector_id)) {
1744 disconnects &= ~(1 << output->connector_id);
1745 weston_log("connector %d disconnected\n",
1746 output->connector_id);
1747 x_offset += output->base.width;
1748 drm_output_destroy(&output->base);
1753 /* FIXME: handle zero outputs, without terminating */
1754 if (ec->connector_allocator == 0)
1755 wl_display_terminate(ec->base.wl_display);
1759 udev_event_is_hotplug(struct drm_compositor *ec, struct udev_device *device)
1764 sysnum = udev_device_get_sysnum(device);
1765 if (!sysnum || atoi(sysnum) != ec->drm.id)
1768 val = udev_device_get_property_value(device, "HOTPLUG");
1772 return strcmp(val, "1") == 0;
1776 udev_drm_event(int fd, uint32_t mask, void *data)
1778 struct drm_compositor *ec = data;
1779 struct udev_device *event;
1781 event = udev_monitor_receive_device(ec->udev_monitor);
1783 if (udev_event_is_hotplug(ec, event))
1784 update_outputs(ec, event);
1786 udev_device_unref(event);
1792 drm_restore(struct weston_compositor *ec)
1794 struct drm_compositor *d = (struct drm_compositor *) ec;
1796 if (weston_launcher_drm_set_master(&d->base, d->drm.fd, 0) < 0)
1797 weston_log("failed to drop master: %m\n");
1802 device_parse_quirks(struct evdev_device *evdev_device,
1803 struct udev_device *udev_device)
1805 if (udev_device_get_property_value(udev_device, "WL_QUIRK_SWAP_AXES"))
1806 evdev_device->quirks |= EVDEV_QUIRK_SWAP_AXES;
1807 else if (udev_device_get_property_value(udev_device, "WL_QUIRK_SWAP_XAXIS"))
1808 evdev_device->quirks |= EVDEV_QUIRK_SWAP_XAXIS;
1811 static const char default_seat[] = "seat0";
1814 device_added(struct udev_device *udev_device, struct drm_seat *master)
1816 struct weston_compositor *c;
1817 struct evdev_device *device;
1818 const char *devnode;
1819 const char *device_seat;
1822 device_seat = udev_device_get_property_value(udev_device, "ID_SEAT");
1824 device_seat = default_seat;
1826 if (strcmp(device_seat, master->seat_id))
1829 c = master->base.compositor;
1830 devnode = udev_device_get_devnode(udev_device);
1832 /* Use non-blocking mode so that we can loop on read on
1833 * evdev_device_data() until all events on the fd are
1834 * read. mtdev_get() also expects this. */
1835 fd = weston_launcher_open(c, devnode, O_RDWR | O_NONBLOCK);
1837 weston_log("opening input device '%s' failed.\n", devnode);
1841 device = evdev_device_create(&master->base, devnode, fd);
1842 if (device == EVDEV_UNHANDLED_DEVICE) {
1844 weston_log("not using input device '%s'.\n", devnode);
1846 } else if (device == NULL) {
1848 weston_log("failed to create input device '%s'.\n", devnode);
1853 device_parse_quirks(device, udev_device);
1855 wl_list_insert(master->devices_list.prev, &device->link);
1861 evdev_add_devices(struct udev *udev, struct weston_seat *seat_base)
1863 struct drm_seat *seat = (struct drm_seat *) seat_base;
1864 struct udev_enumerate *e;
1865 struct udev_list_entry *entry;
1866 struct udev_device *device;
1867 const char *path, *sysname;
1869 e = udev_enumerate_new(udev);
1870 udev_enumerate_add_match_subsystem(e, "input");
1871 udev_enumerate_scan_devices(e);
1872 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
1873 path = udev_list_entry_get_name(entry);
1874 device = udev_device_new_from_syspath(udev, path);
1876 sysname = udev_device_get_sysname(device);
1877 if (strncmp("event", sysname, 5) != 0) {
1878 udev_device_unref(device);
1882 if (device_added(device, seat) < 0) {
1883 udev_device_unref(device);
1884 udev_enumerate_unref(e);
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");
1908 evdev_udev_handler(int fd, uint32_t mask, void *data)
1910 struct drm_seat *seat = data;
1911 struct udev_device *udev_device;
1912 struct evdev_device *device, *next;
1914 const char *devnode;
1916 udev_device = udev_monitor_receive_device(seat->udev_monitor);
1920 action = udev_device_get_action(udev_device);
1924 if (strncmp("event", udev_device_get_sysname(udev_device), 5) != 0)
1927 if (!strcmp(action, "add")) {
1928 device_added(udev_device, seat);
1930 else if (!strcmp(action, "remove")) {
1931 devnode = udev_device_get_devnode(udev_device);
1932 wl_list_for_each_safe(device, next, &seat->devices_list, link)
1933 if (!strcmp(device->devnode, devnode)) {
1934 weston_log("input device %s, %s removed\n",
1935 device->devname, device->devnode);
1936 evdev_device_destroy(device);
1942 udev_device_unref(udev_device);
1948 evdev_enable_udev_monitor(struct udev *udev, struct weston_seat *seat_base)
1950 struct drm_seat *master = (struct drm_seat *) seat_base;
1951 struct wl_event_loop *loop;
1952 struct weston_compositor *c = master->base.compositor;
1955 master->udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
1956 if (!master->udev_monitor) {
1957 weston_log("udev: failed to create the udev monitor\n");
1961 udev_monitor_filter_add_match_subsystem_devtype(master->udev_monitor,
1964 if (udev_monitor_enable_receiving(master->udev_monitor)) {
1965 weston_log("udev: failed to bind the udev monitor\n");
1966 udev_monitor_unref(master->udev_monitor);
1970 loop = wl_display_get_event_loop(c->wl_display);
1971 fd = udev_monitor_get_fd(master->udev_monitor);
1972 master->udev_monitor_source =
1973 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1974 evdev_udev_handler, master);
1975 if (!master->udev_monitor_source) {
1976 udev_monitor_unref(master->udev_monitor);
1984 evdev_disable_udev_monitor(struct weston_seat *seat_base)
1986 struct drm_seat *seat = (struct drm_seat *) seat_base;
1988 if (!seat->udev_monitor)
1991 udev_monitor_unref(seat->udev_monitor);
1992 seat->udev_monitor = NULL;
1993 wl_event_source_remove(seat->udev_monitor_source);
1994 seat->udev_monitor_source = NULL;
1998 drm_led_update(struct weston_seat *seat_base, enum weston_led leds)
2000 struct drm_seat *seat = (struct drm_seat *) seat_base;
2001 struct evdev_device *device;
2003 wl_list_for_each(device, &seat->devices_list, link)
2004 evdev_led_update(device, leds);
2007 static struct drm_seat *
2008 evdev_input_create(struct weston_compositor *c, struct udev *udev,
2009 const char *seat_id)
2011 struct drm_seat *seat;
2013 seat = malloc(sizeof *seat);
2017 memset(seat, 0, sizeof *seat);
2018 weston_seat_init(&seat->base, c);
2019 seat->base.led_update = drm_led_update;
2021 wl_list_init(&seat->devices_list);
2022 seat->seat_id = strdup(seat_id);
2023 if (!evdev_enable_udev_monitor(udev, &seat->base))
2025 if (evdev_add_devices(udev, &seat->base) < 0)
2031 free(seat->seat_id);
2037 evdev_remove_devices(struct weston_seat *seat_base)
2039 struct drm_seat *seat = (struct drm_seat *) seat_base;
2040 struct evdev_device *device, *next;
2042 wl_list_for_each_safe(device, next, &seat->devices_list, link)
2043 evdev_device_destroy(device);
2045 if (seat->base.seat.keyboard)
2046 notify_keyboard_focus_out(&seat->base);
2050 evdev_input_destroy(struct weston_seat *seat_base)
2052 struct drm_seat *seat = (struct drm_seat *) seat_base;
2054 evdev_remove_devices(seat_base);
2055 evdev_disable_udev_monitor(&seat->base);
2057 weston_seat_release(seat_base);
2058 free(seat->seat_id);
2063 drm_free_configured_output(struct drm_configured_output *output)
2071 drm_destroy(struct weston_compositor *ec)
2073 struct drm_compositor *d = (struct drm_compositor *) ec;
2074 struct weston_seat *seat, *next;
2075 struct drm_configured_output *o, *n;
2077 wl_list_for_each_safe(seat, next, &ec->seat_list, link)
2078 evdev_input_destroy(seat);
2079 wl_list_for_each_safe(o, n, &configured_output_list, link)
2080 drm_free_configured_output(o);
2082 wl_event_source_remove(d->udev_drm_source);
2083 wl_event_source_remove(d->drm_source);
2085 weston_compositor_shutdown(ec);
2087 gles2_renderer_destroy(ec);
2089 /* Work around crash in egl_dri2.c's dri2_make_current() */
2090 eglMakeCurrent(ec->egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
2092 eglTerminate(ec->egl_display);
2095 gbm_device_destroy(d->gbm);
2097 if (weston_launcher_drm_set_master(&d->base, d->drm.fd, 0) < 0)
2098 weston_log("failed to drop master: %m\n");
2099 tty_destroy(d->tty);
2105 drm_compositor_set_modes(struct drm_compositor *compositor)
2107 struct drm_output *output;
2108 struct drm_mode *drm_mode;
2111 wl_list_for_each(output, &compositor->base.output_list, base.link) {
2112 if (!output->current) {
2113 /* If something that would cause the output to
2114 * switch mode happened while in another vt, we
2115 * might not have a current drm_fb. In that case,
2116 * schedule a repaint and let drm_output_repaint
2117 * handle setting the mode. */
2118 weston_output_schedule_repaint(&output->base);
2122 drm_mode = (struct drm_mode *) output->base.current;
2123 ret = drmModeSetCrtc(compositor->drm.fd, output->crtc_id,
2124 output->current->fb_id, 0, 0,
2125 &output->connector_id, 1,
2126 &drm_mode->mode_info);
2129 "failed to set mode %dx%d for output at %d,%d: %m\n",
2130 drm_mode->base.width, drm_mode->base.height,
2131 output->base.x, output->base.y);
2137 vt_func(struct weston_compositor *compositor, int event)
2139 struct drm_compositor *ec = (struct drm_compositor *) compositor;
2140 struct weston_seat *seat;
2141 struct drm_sprite *sprite;
2142 struct drm_output *output;
2146 weston_log("entering VT\n");
2147 compositor->focus = 1;
2148 if (weston_launcher_drm_set_master(&ec->base, ec->drm.fd, 1)) {
2149 weston_log("failed to set master: %m\n");
2150 wl_display_terminate(compositor->wl_display);
2152 compositor->state = ec->prev_state;
2153 drm_compositor_set_modes(ec);
2154 weston_compositor_damage_all(compositor);
2155 wl_list_for_each(seat, &compositor->seat_list, link) {
2156 evdev_add_devices(ec->udev, seat);
2157 evdev_enable_udev_monitor(ec->udev, seat);
2161 weston_log("leaving VT\n");
2162 wl_list_for_each(seat, &compositor->seat_list, link) {
2163 evdev_disable_udev_monitor(seat);
2164 evdev_remove_devices(seat);
2167 compositor->focus = 0;
2168 ec->prev_state = compositor->state;
2169 compositor->state = WESTON_COMPOSITOR_SLEEPING;
2171 /* If we have a repaint scheduled (either from a
2172 * pending pageflip or the idle handler), make sure we
2173 * cancel that so we don't try to pageflip when we're
2174 * vt switched away. The SLEEPING state will prevent
2175 * further attemps at repainting. When we switch
2176 * back, we schedule a repaint, which will process
2177 * pending frame callbacks. */
2179 wl_list_for_each(output, &ec->base.output_list, base.link) {
2180 output->base.repaint_needed = 0;
2181 drmModeSetCursor(ec->drm.fd, output->crtc_id, 0, 0, 0);
2184 output = container_of(ec->base.output_list.next,
2185 struct drm_output, base.link);
2187 wl_list_for_each(sprite, &ec->sprite_list, link)
2188 drmModeSetPlane(ec->drm.fd,
2190 output->crtc_id, 0, 0,
2191 0, 0, 0, 0, 0, 0, 0, 0);
2193 if (weston_launcher_drm_set_master(&ec->base, ec->drm.fd, 0) < 0)
2194 weston_log("failed to drop master: %m\n");
2201 switch_vt_binding(struct wl_seat *seat, uint32_t time, uint32_t key, void *data)
2203 struct drm_compositor *ec = data;
2205 tty_activate_vt(ec->tty, key - KEY_F1 + 1);
2210 * Some systems may have multiple DRM devices attached to a single seat. This
2211 * function loops over all devices and tries to find a PCI device with the
2212 * boot_vga sysfs attribute set to 1.
2213 * If no such device is found, the first DRM device reported by udev is used.
2215 static struct udev_device*
2216 find_primary_gpu(struct drm_compositor *ec, const char *seat)
2218 struct udev_enumerate *e;
2219 struct udev_list_entry *entry;
2220 const char *path, *device_seat, *id;
2221 struct udev_device *device, *drm_device, *pci;
2223 e = udev_enumerate_new(ec->udev);
2224 udev_enumerate_add_match_subsystem(e, "drm");
2225 udev_enumerate_add_match_sysname(e, "card[0-9]*");
2227 udev_enumerate_scan_devices(e);
2229 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
2230 path = udev_list_entry_get_name(entry);
2231 device = udev_device_new_from_syspath(ec->udev, path);
2234 device_seat = udev_device_get_property_value(device, "ID_SEAT");
2236 device_seat = default_seat;
2237 if (strcmp(device_seat, seat)) {
2238 udev_device_unref(device);
2242 pci = udev_device_get_parent_with_subsystem_devtype(device,
2245 id = udev_device_get_sysattr_value(pci, "boot_vga");
2246 if (id && !strcmp(id, "1")) {
2248 udev_device_unref(drm_device);
2249 drm_device = device;
2255 drm_device = device;
2257 udev_device_unref(device);
2260 udev_enumerate_unref(e);
2265 hide_sprites_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2268 struct drm_compositor *c = data;
2270 c->sprites_hidden ^= 1;
2273 static struct weston_compositor *
2274 drm_compositor_create(struct wl_display *display,
2275 int connector, const char *seat, int tty,
2276 int argc, char *argv[], const char *config_file)
2278 struct drm_compositor *ec;
2279 struct udev_device *drm_device;
2280 struct wl_event_loop *loop;
2281 struct weston_seat *weston_seat, *next;
2285 weston_log("initializing drm backend\n");
2287 ec = malloc(sizeof *ec);
2290 memset(ec, 0, sizeof *ec);
2292 /* KMS support for sprites is not complete yet, so disable the
2293 * functionality for now. */
2294 ec->sprites_are_broken = 1;
2296 if (weston_compositor_init(&ec->base, display, argc, argv,
2298 weston_log("weston_compositor_init failed\n");
2302 ec->udev = udev_new();
2303 if (ec->udev == NULL) {
2304 weston_log("failed to initialize udev context\n");
2305 goto err_compositor;
2308 ec->base.wl_display = display;
2309 ec->tty = tty_create(&ec->base, vt_func, tty);
2311 weston_log("failed to initialize tty\n");
2315 drm_device = find_primary_gpu(ec, seat);
2316 if (drm_device == NULL) {
2317 weston_log("no drm device found\n");
2320 path = udev_device_get_syspath(drm_device);
2322 if (init_egl(ec, drm_device) < 0) {
2323 weston_log("failed to initialize egl\n");
2327 ec->base.destroy = drm_destroy;
2328 ec->base.restore = drm_restore;
2332 ec->prev_state = WESTON_COMPOSITOR_ACTIVE;
2334 for (key = KEY_F1; key < KEY_F9; key++)
2335 weston_compositor_add_key_binding(&ec->base, key,
2336 MODIFIER_CTRL | MODIFIER_ALT,
2337 switch_vt_binding, ec);
2339 wl_list_init(&ec->sprite_list);
2342 if (create_outputs(ec, connector, drm_device) < 0) {
2343 weston_log("failed to create output for %s\n", path);
2347 if (gles2_renderer_init(&ec->base) < 0)
2352 if (evdev_input_create(&ec->base, ec->udev, seat) == NULL) {
2353 weston_log("failed to create input devices\n");
2357 loop = wl_display_get_event_loop(ec->base.wl_display);
2359 wl_event_loop_add_fd(loop, ec->drm.fd,
2360 WL_EVENT_READABLE, on_drm_input, ec);
2362 ec->udev_monitor = udev_monitor_new_from_netlink(ec->udev, "udev");
2363 if (ec->udev_monitor == NULL) {
2364 weston_log("failed to intialize udev monitor\n");
2365 goto err_drm_source;
2367 udev_monitor_filter_add_match_subsystem_devtype(ec->udev_monitor,
2369 ec->udev_drm_source =
2370 wl_event_loop_add_fd(loop,
2371 udev_monitor_get_fd(ec->udev_monitor),
2372 WL_EVENT_READABLE, udev_drm_event, ec);
2374 if (udev_monitor_enable_receiving(ec->udev_monitor) < 0) {
2375 weston_log("failed to enable udev-monitor receiving\n");
2376 goto err_udev_monitor;
2379 udev_device_unref(drm_device);
2381 weston_compositor_add_key_binding(&ec->base, KEY_O,
2382 MODIFIER_CTRL | MODIFIER_ALT,
2383 hide_sprites_binding, ec);
2388 wl_event_source_remove(ec->udev_drm_source);
2389 udev_monitor_unref(ec->udev_monitor);
2391 wl_event_source_remove(ec->drm_source);
2392 wl_list_for_each_safe(weston_seat, next, &ec->base.seat_list, link)
2393 evdev_input_destroy(weston_seat);
2395 eglMakeCurrent(ec->base.egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
2397 eglTerminate(ec->base.egl_display);
2399 gbm_device_destroy(ec->gbm);
2401 destroy_sprites(ec);
2403 udev_device_unref(drm_device);
2405 if (weston_launcher_drm_set_master(&ec->base, ec->drm.fd, 0) < 0)
2406 weston_log("failed to drop master: %m\n");
2407 tty_destroy(ec->tty);
2409 udev_unref(ec->udev);
2411 weston_compositor_shutdown(&ec->base);
2418 set_sync_flags(drmModeModeInfo *mode, char *hsync, char *vsync)
2422 if (strcmp(hsync, "+hsync") == 0)
2423 mode->flags |= DRM_MODE_FLAG_PHSYNC;
2424 else if (strcmp(hsync, "-hsync") == 0)
2425 mode->flags |= DRM_MODE_FLAG_NHSYNC;
2429 if (strcmp(vsync, "+vsync") == 0)
2430 mode->flags |= DRM_MODE_FLAG_PVSYNC;
2431 else if (strcmp(vsync, "-vsync") == 0)
2432 mode->flags |= DRM_MODE_FLAG_NVSYNC;
2440 check_for_modeline(struct drm_configured_output *output)
2442 drmModeModeInfo mode;
2448 mode.type = DRM_MODE_TYPE_USERDEF;
2453 if (sscanf(output_mode, "%f %hd %hd %hd %hd %hd %hd %hd %hd %s %s",
2454 &fclock, &mode.hdisplay,
2456 &mode.hsync_end, &mode.htotal,
2459 &mode.vsync_end, &mode.vtotal,
2460 hsync, vsync) == 11) {
2461 if (set_sync_flags(&mode, hsync, vsync))
2464 sprintf(mode_name, "%dx%d", mode.hdisplay, mode.vdisplay);
2465 strcpy(mode.name, mode_name);
2467 mode.clock = fclock * 1000;
2471 output->crtc_mode = mode;
2477 drm_output_set_transform(struct drm_configured_output *output)
2479 if (!output_transform) {
2480 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
2484 if (!strcmp(output_transform, "normal"))
2485 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
2486 else if (!strcmp(output_transform, "90"))
2487 output->transform = WL_OUTPUT_TRANSFORM_90;
2488 else if (!strcmp(output_transform, "180"))
2489 output->transform = WL_OUTPUT_TRANSFORM_180;
2490 else if (!strcmp(output_transform, "270"))
2491 output->transform = WL_OUTPUT_TRANSFORM_270;
2492 else if (!strcmp(output_transform, "flipped"))
2493 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED;
2494 else if (!strcmp(output_transform, "flipped-90"))
2495 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_90;
2496 else if (!strcmp(output_transform, "flipped-180"))
2497 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_180;
2498 else if (!strcmp(output_transform, "flipped-270"))
2499 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_270;
2501 weston_log("Invalid transform \"%s\" for output %s\n",
2502 output_transform, output_name);
2503 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
2506 free(output_transform);
2507 output_transform = NULL;
2511 output_section_done(void *data)
2513 struct drm_configured_output *output;
2515 output = malloc(sizeof *output);
2517 if (!output || !output_name || (output_name[0] == 'X') ||
2518 (!output_mode && !output_transform)) {
2521 free(output_transform);
2525 output_transform = NULL;
2529 output->config = OUTPUT_CONFIG_INVALID;
2530 output->name = output_name;
2531 output->mode = output_mode;
2534 if (strcmp(output_mode, "off") == 0)
2535 output->config = OUTPUT_CONFIG_OFF;
2536 else if (strcmp(output_mode, "preferred") == 0)
2537 output->config = OUTPUT_CONFIG_PREFERRED;
2538 else if (strcmp(output_mode, "current") == 0)
2539 output->config = OUTPUT_CONFIG_CURRENT;
2540 else if (sscanf(output_mode, "%dx%d",
2541 &output->width, &output->height) == 2)
2542 output->config = OUTPUT_CONFIG_MODE;
2543 else if (check_for_modeline(output) == 0)
2544 output->config = OUTPUT_CONFIG_MODELINE;
2546 if (output->config == OUTPUT_CONFIG_INVALID)
2547 weston_log("Invalid mode \"%s\" for output %s\n",
2548 output_mode, output_name);
2552 drm_output_set_transform(output);
2554 wl_list_insert(&configured_output_list, &output->link);
2556 if (output_transform)
2557 free(output_transform);
2558 output_transform = NULL;
2561 WL_EXPORT struct weston_compositor *
2562 backend_init(struct wl_display *display, int argc, char *argv[],
2563 const char *config_file)
2565 int connector = 0, tty = 0;
2566 const char *seat = default_seat;
2568 const struct weston_option drm_options[] = {
2569 { WESTON_OPTION_INTEGER, "connector", 0, &connector },
2570 { WESTON_OPTION_STRING, "seat", 0, &seat },
2571 { WESTON_OPTION_INTEGER, "tty", 0, &tty },
2572 { WESTON_OPTION_BOOLEAN, "current-mode", 0, &option_current_mode },
2575 parse_options(drm_options, ARRAY_LENGTH(drm_options), argc, argv);
2577 wl_list_init(&configured_output_list);
2579 const struct config_key drm_config_keys[] = {
2580 { "name", CONFIG_KEY_STRING, &output_name },
2581 { "mode", CONFIG_KEY_STRING, &output_mode },
2582 { "transform", CONFIG_KEY_STRING, &output_transform },
2585 const struct config_section config_section[] = {
2586 { "output", drm_config_keys,
2587 ARRAY_LENGTH(drm_config_keys), output_section_done },
2590 parse_config_file(config_file, config_section,
2591 ARRAY_LENGTH(config_section), NULL);
2593 return drm_compositor_create(display, connector, seat, tty, argc, argv,