2 * Copyright © 2008-2011 Kristian Høgsberg
3 * Copyright © 2011 Intel Corporation
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the copyright holders not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. The copyright holders make
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied warranty.
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
31 #include <linux/input.h>
35 #include <xf86drmMode.h>
36 #include <drm_fourcc.h>
39 #include <libbacklight.h>
42 #include "compositor.h"
44 #include "launcher-util.h"
46 static int option_current_mode = 0;
47 static char *output_name;
48 static char *output_mode;
49 static struct wl_list configured_output_list;
52 OUTPUT_CONFIG_INVALID = 0,
54 OUTPUT_CONFIG_PREFERRED,
55 OUTPUT_CONFIG_CURRENT,
57 OUTPUT_CONFIG_MODELINE
60 struct drm_configured_output {
63 int32_t width, height;
64 drmModeModeInfo crtc_mode;
65 enum output_config config;
69 struct drm_compositor {
70 struct weston_compositor base;
73 struct wl_event_source *drm_source;
75 struct udev_monitor *udev_monitor;
76 struct wl_event_source *udev_drm_source;
82 struct gbm_device *gbm;
85 uint32_t crtc_allocator;
86 uint32_t connector_allocator;
89 struct gbm_surface *dummy_surface;
90 EGLSurface dummy_egl_surface;
92 /* we need these parameters in order to not fail drmModeAddFB2()
93 * due to out of bounds dimensions, and then mistakenly set
96 int32_t min_width, max_width;
97 int32_t min_height, max_height;
99 struct wl_list sprite_list;
100 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 EGLSurface egl_surface;
142 struct drm_fb *current, *next;
143 struct backlight *backlight;
147 * An output has a primary display plane plus zero or more sprites for
148 * blending display contents.
154 uint32_t pending_fb_id;
155 struct weston_surface *surface;
156 struct weston_surface *pending_surface;
157 struct weston_plane plane;
159 struct drm_output *output;
161 struct drm_compositor *compositor;
163 struct wl_listener destroy_listener;
164 struct wl_listener pending_destroy_listener;
166 uint32_t possible_crtcs;
168 uint32_t count_formats;
170 int32_t src_x, src_y;
171 uint32_t src_w, src_h;
172 uint32_t dest_x, dest_y;
173 uint32_t dest_w, dest_h;
179 struct weston_seat base;
180 struct wl_list devices_list;
181 struct udev_monitor *udev_monitor;
182 struct wl_event_source *udev_monitor_source;
187 drm_output_set_cursor(struct drm_output *output);
189 drm_disable_unused_sprites(struct weston_output *output_base);
192 drm_sprite_crtc_supported(struct weston_output *output_base, uint32_t supported)
194 struct weston_compositor *ec = output_base->compositor;
195 struct drm_compositor *c =(struct drm_compositor *) ec;
196 struct drm_output *output = (struct drm_output *) output_base;
199 for (crtc = 0; crtc < c->num_crtcs; crtc++) {
200 if (c->crtcs[crtc] != output->crtc_id)
203 if (supported & (1 << crtc))
211 drm_fb_destroy_callback(struct gbm_bo *bo, void *data)
213 struct drm_fb *fb = data;
214 struct gbm_device *gbm = gbm_bo_get_device(bo);
217 drmModeRmFB(gbm_device_get_fd(gbm), fb->fb_id);
220 weston_buffer_post_release(fb->buffer);
221 wl_list_remove(&fb->buffer_destroy_listener.link);
227 static struct drm_fb *
228 drm_fb_get_from_bo(struct gbm_bo *bo, struct drm_output *output)
230 struct drm_fb *fb = gbm_bo_get_user_data(bo);
231 struct drm_compositor *compositor =
232 (struct drm_compositor *) output->base.compositor;
233 uint32_t width, height, stride, handle;
239 fb = malloc(sizeof *fb);
243 fb->is_client_buffer = 0;
246 width = gbm_bo_get_width(bo);
247 height = gbm_bo_get_height(bo);
248 stride = gbm_bo_get_stride(bo);
249 handle = gbm_bo_get_handle(bo).u32;
251 ret = drmModeAddFB(compositor->drm.fd, width, height, 24, 32,
252 stride, handle, &fb->fb_id);
254 weston_log("failed to create kms fb: %m\n");
259 gbm_bo_set_user_data(bo, fb, drm_fb_destroy_callback);
265 fb_handle_buffer_destroy(struct wl_listener *listener, void *data)
267 struct drm_fb *fb = container_of(listener, struct drm_fb,
268 buffer_destroy_listener);
272 if (fb == fb->output->next ||
273 (fb == fb->output->current && !fb->output->next))
274 weston_output_schedule_repaint(&fb->output->base);
277 static struct weston_plane *
278 drm_output_prepare_scanout_surface(struct weston_output *_output,
279 struct weston_surface *es)
281 struct drm_output *output = (struct drm_output *) _output;
282 struct drm_compositor *c =
283 (struct drm_compositor *) output->base.compositor;
286 if (es->geometry.x != output->base.x ||
287 es->geometry.y != output->base.y ||
288 es->geometry.width != output->base.current->width ||
289 es->geometry.height != output->base.current->height ||
290 es->transform.enabled ||
294 bo = gbm_bo_import(c->gbm, GBM_BO_IMPORT_WL_BUFFER,
295 es->buffer, GBM_BO_USE_SCANOUT);
297 /* Need to verify output->region contained in surface opaque
298 * region. Or maybe just that format doesn't have alpha.
299 * For now, scanout only if format is XRGB8888. */
300 if (gbm_bo_get_format(bo) != GBM_FORMAT_XRGB8888) {
305 output->next = drm_fb_get_from_bo(bo, output);
311 output->next->is_client_buffer = 1;
312 output->next->buffer = es->buffer;
313 output->next->buffer->busy_count++;
314 output->next->buffer_destroy_listener.notify = fb_handle_buffer_destroy;
316 wl_signal_add(&output->next->buffer->resource.destroy_signal,
317 &output->next->buffer_destroy_listener);
319 return &output->fb_plane;
323 drm_output_render(struct drm_output *output, pixman_region32_t *damage)
325 struct drm_compositor *compositor =
326 (struct drm_compositor *) output->base.compositor;
327 struct weston_surface *surface;
330 if (!eglMakeCurrent(compositor->base.egl_display, output->egl_surface,
332 compositor->base.egl_context)) {
333 weston_log("failed to make current\n");
337 wl_list_for_each_reverse(surface, &compositor->base.surface_list, link)
338 if (surface->plane == &compositor->base.primary_plane)
339 weston_surface_draw(surface, &output->base, damage);
341 wl_signal_emit(&output->base.frame_signal, output);
343 eglSwapBuffers(compositor->base.egl_display, output->egl_surface);
344 bo = gbm_surface_lock_front_buffer(output->surface);
346 weston_log("failed to lock front buffer: %m\n");
350 output->next = drm_fb_get_from_bo(bo, output);
352 weston_log("failed to get drm_fb for bo\n");
353 gbm_surface_release_buffer(output->surface, bo);
359 drm_output_repaint(struct weston_output *output_base,
360 pixman_region32_t *damage)
362 struct drm_output *output = (struct drm_output *) output_base;
363 struct drm_compositor *compositor =
364 (struct drm_compositor *) output->base.compositor;
365 struct drm_sprite *s;
366 struct drm_mode *mode;
370 drm_output_render(output, damage);
374 mode = container_of(output->base.current, struct drm_mode, base);
375 if (!output->current) {
376 ret = drmModeSetCrtc(compositor->drm.fd, output->crtc_id,
377 output->next->fb_id, 0, 0,
378 &output->connector_id, 1,
381 weston_log("set mode failed: %m\n");
386 if (drmModePageFlip(compositor->drm.fd, output->crtc_id,
388 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
389 weston_log("queueing pageflip failed: %m\n");
393 output->page_flip_pending = 1;
395 drm_output_set_cursor(output);
398 * Now, update all the sprite surfaces
400 wl_list_for_each(s, &compositor->sprite_list, link) {
403 .request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT,
404 .request.sequence = 1,
407 if (!drm_sprite_crtc_supported(output_base, s->possible_crtcs))
410 ret = drmModeSetPlane(compositor->drm.fd, s->plane_id,
411 output->crtc_id, s->pending_fb_id, flags,
412 s->dest_x, s->dest_y,
413 s->dest_w, s->dest_h,
417 weston_log("setplane failed: %d: %s\n",
418 ret, strerror(errno));
420 if (output->pipe > 0)
421 vbl.request.type |= DRM_VBLANK_SECONDARY;
424 * Queue a vblank signal so we know when the surface
425 * becomes active on the display or has been replaced.
427 vbl.request.signal = (unsigned long)s;
428 ret = drmWaitVBlank(compositor->drm.fd, &vbl);
430 weston_log("vblank event request failed: %d: %s\n",
431 ret, strerror(errno));
435 output->vblank_pending = 1;
438 drm_disable_unused_sprites(&output->base);
444 vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec,
447 struct drm_sprite *s = (struct drm_sprite *)data;
448 struct drm_compositor *c = s->compositor;
449 struct drm_output *output = s->output;
452 output->vblank_pending = 0;
455 weston_buffer_post_release(s->surface->buffer);
456 wl_list_remove(&s->destroy_listener.link);
458 drmModeRmFB(c->drm.fd, s->fb_id);
462 if (s->pending_surface) {
463 wl_list_remove(&s->pending_destroy_listener.link);
464 wl_signal_add(&s->pending_surface->buffer->resource.destroy_signal,
465 &s->destroy_listener);
466 s->surface = s->pending_surface;
467 s->pending_surface = NULL;
468 s->fb_id = s->pending_fb_id;
469 s->pending_fb_id = 0;
472 if (!output->page_flip_pending) {
473 msecs = sec * 1000 + usec / 1000;
474 weston_output_finish_frame(&output->base, msecs);
479 page_flip_handler(int fd, unsigned int frame,
480 unsigned int sec, unsigned int usec, void *data)
482 struct drm_output *output = (struct drm_output *) data;
485 output->page_flip_pending = 0;
487 if (output->current) {
488 if (output->current->is_client_buffer)
489 gbm_bo_destroy(output->current->bo);
491 gbm_surface_release_buffer(output->surface,
492 output->current->bo);
495 output->current = output->next;
498 if (!output->vblank_pending) {
499 msecs = sec * 1000 + usec / 1000;
500 weston_output_finish_frame(&output->base, msecs);
505 drm_surface_format_supported(struct drm_sprite *s, uint32_t format)
509 for (i = 0; i < s->count_formats; i++)
510 if (s->formats[i] == format)
517 drm_surface_transform_supported(struct weston_surface *es)
519 struct weston_matrix *matrix = &es->transform.matrix;
522 if (!es->transform.enabled)
525 for (i = 0; i < 16; i++) {
529 if (matrix->d[i] != 1.0)
538 if (matrix->d[i] != 0.0)
548 drm_disable_unused_sprites(struct weston_output *output_base)
550 struct weston_compositor *ec = output_base->compositor;
551 struct drm_compositor *c =(struct drm_compositor *) ec;
552 struct drm_output *output = (struct drm_output *) output_base;
553 struct drm_sprite *s;
556 wl_list_for_each(s, &c->sprite_list, link) {
557 if (s->pending_fb_id)
560 ret = drmModeSetPlane(c->drm.fd, s->plane_id,
561 output->crtc_id, 0, 0,
562 0, 0, 0, 0, 0, 0, 0, 0);
564 weston_log("failed to disable plane: %d: %s\n",
565 ret, strerror(errno));
566 drmModeRmFB(c->drm.fd, s->fb_id);
570 wl_list_remove(&s->destroy_listener.link);
573 assert(!s->pending_surface);
575 s->pending_fb_id = 0;
580 * This function must take care to damage any previously assigned surface
581 * if the sprite ends up binding to a different surface than in the
584 static struct weston_plane *
585 drm_output_prepare_overlay_surface(struct weston_output *output_base,
586 struct weston_surface *es)
588 struct weston_compositor *ec = output_base->compositor;
589 struct drm_compositor *c =(struct drm_compositor *) ec;
590 struct drm_sprite *s;
592 EGLint handle, stride;
595 uint32_t handles[4], pitches[4], offsets[4];
597 pixman_region32_t dest_rect, src_rect;
600 wl_fixed_t sx1, sy1, sx2, sy2;
601 int32_t width, height;
603 if (c->sprites_are_broken)
606 if (es->output_mask != (1u << output_base->id))
609 if (es->buffer == NULL)
612 if (wl_buffer_is_shm(es->buffer))
615 if (!drm_surface_transform_supported(es))
618 wl_list_for_each(s, &c->sprite_list, link) {
619 if (!drm_sprite_crtc_supported(output_base, s->possible_crtcs))
622 if (!s->pending_fb_id) {
628 /* No sprites available */
632 width = es->geometry.width;
633 height = es->geometry.height;
635 /* If geometry is out of bounds, don't even bother trying because
636 * we know the AddFB2() call will fail:
638 if (c->min_width > width || width > c->max_width ||
639 c->min_height > height || height > c->max_height)
642 bo = gbm_bo_import(c->gbm, GBM_BO_IMPORT_WL_BUFFER,
643 es->buffer, GBM_BO_USE_SCANOUT);
647 format = gbm_bo_get_format(bo);
648 handle = gbm_bo_get_handle(bo).s32;
649 stride = gbm_bo_get_stride(bo);
653 if (!drm_surface_format_supported(s, format))
663 ret = drmModeAddFB2(c->drm.fd, width, height,
664 format, handles, pitches, offsets,
667 weston_log("addfb2 failed: %d\n", ret);
668 c->sprites_are_broken = 1;
672 s->pending_fb_id = fb_id;
673 s->pending_surface = es;
674 es->buffer->busy_count++;
676 box = pixman_region32_extents(&es->transform.boundingbox);
677 s->plane.x = box->x1;
678 s->plane.y = box->y1;
681 * Calculate the source & dest rects properly based on actual
682 * postion (note the caller has called weston_surface_update_transform()
685 pixman_region32_init(&dest_rect);
686 pixman_region32_intersect(&dest_rect, &es->transform.boundingbox,
687 &output_base->region);
688 pixman_region32_translate(&dest_rect, -output_base->x, -output_base->y);
689 box = pixman_region32_extents(&dest_rect);
692 s->dest_w = box->x2 - box->x1;
693 s->dest_h = box->y2 - box->y1;
694 pixman_region32_fini(&dest_rect);
696 pixman_region32_init(&src_rect);
697 pixman_region32_intersect(&src_rect, &es->transform.boundingbox,
698 &output_base->region);
699 box = pixman_region32_extents(&src_rect);
701 weston_surface_from_global_fixed(es,
702 wl_fixed_from_int(box->x1),
703 wl_fixed_from_int(box->y1),
705 weston_surface_from_global_fixed(es,
706 wl_fixed_from_int(box->x2),
707 wl_fixed_from_int(box->y2),
714 if (sx2 > wl_fixed_from_int(es->geometry.width))
715 sx2 = wl_fixed_from_int(es->geometry.width);
716 if (sy2 > wl_fixed_from_int(es->geometry.height))
717 sy2 = wl_fixed_from_int(es->geometry.height);
721 s->src_w = (sx2 - sx1) << 8;
722 s->src_h = (sy2 - sy1) << 8;
723 pixman_region32_fini(&src_rect);
725 wl_signal_add(&es->buffer->resource.destroy_signal,
726 &s->pending_destroy_listener);
731 static struct weston_plane *
732 drm_output_prepare_cursor_surface(struct weston_output *output_base,
733 struct weston_surface *es)
735 struct drm_compositor *c =
736 (struct drm_compositor *) output_base->compositor;
737 struct drm_output *output = (struct drm_output *) output_base;
739 if (output->cursor_surface)
741 if (es->output_mask != (1u << output_base->id))
743 if (c->cursors_are_broken)
745 if (es->buffer == NULL || !wl_buffer_is_shm(es->buffer) ||
746 es->geometry.width > 64 || es->geometry.height > 64)
749 output->cursor_surface = es;
751 return &output->cursor_plane;
755 drm_output_set_cursor(struct drm_output *output)
757 struct weston_surface *es = output->cursor_surface;
758 struct drm_compositor *c =
759 (struct drm_compositor *) output->base.compositor;
760 EGLint handle, stride;
762 uint32_t buf[64 * 64];
766 output->cursor_surface = NULL;
768 drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0);
772 if (es->buffer && pixman_region32_not_empty(&output->cursor_plane.damage)) {
773 pixman_region32_fini(&output->cursor_plane.damage);
774 pixman_region32_init(&output->cursor_plane.damage);
775 output->current_cursor ^= 1;
776 bo = output->cursor_bo[output->current_cursor];
777 memset(buf, 0, sizeof buf);
778 stride = wl_shm_buffer_get_stride(es->buffer);
779 s = wl_shm_buffer_get_data(es->buffer);
780 for (i = 0; i < es->geometry.height; i++)
781 memcpy(buf + i * 64, s + i * stride,
782 es->geometry.width * 4);
784 if (gbm_bo_write(bo, buf, sizeof buf) < 0)
785 weston_log("failed update cursor: %m\n");
787 handle = gbm_bo_get_handle(bo).s32;
788 if (drmModeSetCursor(c->drm.fd,
789 output->crtc_id, handle, 64, 64)) {
790 weston_log("failed to set cursor: %m\n");
791 c->cursors_are_broken = 1;
795 x = es->geometry.x - output->base.x;
796 y = es->geometry.y - output->base.y;
797 if (output->cursor_plane.x != x || output->cursor_plane.y != y) {
798 if (drmModeMoveCursor(c->drm.fd, output->crtc_id, x, y)) {
799 weston_log("failed to move cursor: %m\n");
800 c->cursors_are_broken = 1;
803 output->cursor_plane.x = x;
804 output->cursor_plane.y = y;
809 drm_assign_planes(struct weston_output *output)
811 struct drm_compositor *c =
812 (struct drm_compositor *) output->compositor;
813 struct weston_surface *es, *next;
814 pixman_region32_t overlap, surface_overlap;
815 struct weston_plane *primary, *next_plane;
818 * Find a surface for each sprite in the output using some heuristics:
820 * 2) frequency of update
821 * 3) opacity (though some hw might support alpha blending)
822 * 4) clipping (this can be fixed with color keys)
824 * The idea is to save on blitting since this should save power.
825 * If we can get a large video surface on the sprite for example,
826 * the main display surface may not need to update at all, and
827 * the client buffer can be used directly for the sprite surface
828 * as we do for flipping full screen surfaces.
830 pixman_region32_init(&overlap);
831 primary = &c->base.primary_plane;
832 wl_list_for_each_safe(es, next, &c->base.surface_list, link) {
833 pixman_region32_init(&surface_overlap);
834 pixman_region32_intersect(&surface_overlap, &overlap,
835 &es->transform.boundingbox);
838 if (pixman_region32_not_empty(&surface_overlap))
839 next_plane = primary;
840 if (next_plane == NULL)
841 next_plane = drm_output_prepare_cursor_surface(output, es);
842 if (next_plane == NULL)
843 next_plane = drm_output_prepare_scanout_surface(output, es);
844 if (next_plane == NULL)
845 next_plane = drm_output_prepare_overlay_surface(output, es);
846 if (next_plane == NULL)
847 next_plane = primary;
848 weston_surface_move_to_plane(es, next_plane);
849 if (next_plane == primary)
850 pixman_region32_union(&overlap, &overlap,
851 &es->transform.boundingbox);
853 pixman_region32_fini(&surface_overlap);
855 pixman_region32_fini(&overlap);
859 drm_output_destroy(struct weston_output *output_base)
861 struct drm_output *output = (struct drm_output *) output_base;
862 struct drm_compositor *c =
863 (struct drm_compositor *) output->base.compositor;
864 drmModeCrtcPtr origcrtc = output->original_crtc;
866 if (output->backlight)
867 backlight_destroy(output->backlight);
869 /* Turn off hardware cursor */
870 drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0);
872 /* Restore original CRTC state */
873 drmModeSetCrtc(c->drm.fd, origcrtc->crtc_id, origcrtc->buffer_id,
874 origcrtc->x, origcrtc->y,
875 &output->connector_id, 1, &origcrtc->mode);
876 drmModeFreeCrtc(origcrtc);
878 c->crtc_allocator &= ~(1 << output->crtc_id);
879 c->connector_allocator &= ~(1 << output->connector_id);
881 eglDestroySurface(c->base.egl_display, output->egl_surface);
882 gbm_surface_destroy(output->surface);
884 weston_plane_release(&output->fb_plane);
885 weston_plane_release(&output->cursor_plane);
887 weston_output_destroy(&output->base);
888 wl_list_remove(&output->base.link);
894 static struct drm_mode *
895 choose_mode (struct drm_output *output, struct weston_mode *target_mode)
897 struct drm_mode *tmp_mode = NULL, *mode;
899 if (output->base.current->width == target_mode->width &&
900 output->base.current->height == target_mode->height &&
901 (output->base.current->refresh == target_mode->refresh ||
902 target_mode->refresh == 0))
903 return (struct drm_mode *)output->base.current;
905 wl_list_for_each(mode, &output->base.mode_list, base.link) {
906 if (mode->mode_info.hdisplay == target_mode->width &&
907 mode->mode_info.vdisplay == target_mode->height) {
908 if (mode->mode_info.vrefresh == target_mode->refresh ||
909 target_mode->refresh == 0) {
911 } else if (!tmp_mode)
920 drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mode)
922 struct drm_output *output;
923 struct drm_mode *drm_mode;
925 struct drm_compositor *ec;
926 struct gbm_surface *surface;
927 EGLSurface egl_surface;
929 if (output_base == NULL) {
930 weston_log("output is NULL.\n");
935 weston_log("mode is NULL.\n");
939 ec = (struct drm_compositor *)output_base->compositor;
940 output = (struct drm_output *)output_base;
941 drm_mode = choose_mode (output, mode);
944 weston_log("%s, invalid resolution:%dx%d\n", __func__, mode->width, mode->height);
946 } else if (&drm_mode->base == output->base.current) {
948 } else if (drm_mode->base.width == output->base.current->width &&
949 drm_mode->base.height == output->base.current->height) {
950 /* only change refresh value */
951 ret = drmModeSetCrtc(ec->drm.fd,
953 output->current->fb_id, 0, 0,
954 &output->connector_id, 1, &drm_mode->mode_info);
957 weston_log("failed to set mode (%dx%d) %u Hz\n",
958 drm_mode->base.width,
959 drm_mode->base.height,
960 drm_mode->base.refresh / 1000);
963 output->base.current->flags = 0;
964 output->base.current = &drm_mode->base;
965 drm_mode->base.flags =
966 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
973 drm_mode->base.flags =
974 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
976 surface = gbm_surface_create(ec->gbm,
977 drm_mode->base.width,
978 drm_mode->base.height,
981 GBM_BO_USE_RENDERING);
983 weston_log("failed to create gbm surface\n");
988 eglCreateWindowSurface(ec->base.egl_display,
992 if (egl_surface == EGL_NO_SURFACE) {
993 weston_log("failed to create egl surface\n");
997 ret = drmModeSetCrtc(ec->drm.fd,
999 output->current->fb_id, 0, 0,
1000 &output->connector_id, 1, &drm_mode->mode_info);
1002 weston_log("failed to set mode\n");
1006 /* reset rendering stuff. */
1007 if (output->current) {
1008 if (output->current->is_client_buffer)
1009 gbm_bo_destroy(output->current->bo);
1011 gbm_surface_release_buffer(output->surface,
1012 output->current->bo);
1014 output->current = NULL;
1017 if (output->next->is_client_buffer)
1018 gbm_bo_destroy(output->next->bo);
1020 gbm_surface_release_buffer(output->surface,
1023 output->next = NULL;
1025 eglDestroySurface(ec->base.egl_display, output->egl_surface);
1026 gbm_surface_destroy(output->surface);
1027 output->egl_surface = egl_surface;
1028 output->surface = surface;
1031 output->base.current = &drm_mode->base;
1032 output->base.dirty = 1;
1033 weston_output_move(&output->base, output->base.x, output->base.y);
1037 eglDestroySurface(ec->base.egl_display, egl_surface);
1038 gbm_surface_destroy(surface);
1043 on_drm_input(int fd, uint32_t mask, void *data)
1045 drmEventContext evctx;
1047 memset(&evctx, 0, sizeof evctx);
1048 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1049 evctx.page_flip_handler = page_flip_handler;
1050 evctx.vblank_handler = vblank_handler;
1051 drmHandleEvent(fd, &evctx);
1057 init_egl(struct drm_compositor *ec, struct udev_device *device)
1059 EGLint major, minor, n;
1060 const char *filename, *sysnum;
1062 static const EGLint context_attribs[] = {
1063 EGL_CONTEXT_CLIENT_VERSION, 2,
1067 static const EGLint config_attribs[] = {
1068 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
1073 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
1077 sysnum = udev_device_get_sysnum(device);
1079 ec->drm.id = atoi(sysnum);
1080 if (!sysnum || ec->drm.id < 0) {
1081 weston_log("cannot get device sysnum\n");
1085 filename = udev_device_get_devnode(device);
1086 fd = open(filename, O_RDWR | O_CLOEXEC);
1088 /* Probably permissions error */
1089 weston_log("couldn't open %s, skipping\n",
1090 udev_device_get_devnode(device));
1094 weston_log("using %s\n", filename);
1097 ec->gbm = gbm_create_device(ec->drm.fd);
1098 ec->base.egl_display = eglGetDisplay(ec->gbm);
1099 if (ec->base.egl_display == NULL) {
1100 weston_log("failed to create display\n");
1104 if (!eglInitialize(ec->base.egl_display, &major, &minor)) {
1105 weston_log("failed to initialize display\n");
1109 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
1110 weston_log("failed to bind api EGL_OPENGL_ES_API\n");
1114 if (!eglChooseConfig(ec->base.egl_display, config_attribs,
1115 &ec->base.egl_config, 1, &n) || n != 1) {
1116 weston_log("failed to choose config: %d\n", n);
1120 ec->base.egl_context =
1121 eglCreateContext(ec->base.egl_display, ec->base.egl_config,
1122 EGL_NO_CONTEXT, context_attribs);
1123 if (ec->base.egl_context == NULL) {
1124 weston_log("failed to create context\n");
1128 ec->dummy_surface = gbm_surface_create(ec->gbm, 10, 10,
1129 GBM_FORMAT_XRGB8888,
1130 GBM_BO_USE_RENDERING);
1131 if (!ec->dummy_surface) {
1132 weston_log("failed to create dummy gbm surface\n");
1136 ec->dummy_egl_surface =
1137 eglCreateWindowSurface(ec->base.egl_display,
1138 ec->base.egl_config,
1141 if (ec->dummy_egl_surface == EGL_NO_SURFACE) {
1142 weston_log("failed to create egl surface\n");
1146 if (!eglMakeCurrent(ec->base.egl_display, ec->dummy_egl_surface,
1147 ec->dummy_egl_surface, ec->base.egl_context)) {
1148 weston_log("failed to make context current\n");
1155 static struct drm_mode *
1156 drm_output_add_mode(struct drm_output *output, drmModeModeInfo *info)
1158 struct drm_mode *mode;
1161 mode = malloc(sizeof *mode);
1165 mode->base.flags = 0;
1166 mode->base.width = info->hdisplay;
1167 mode->base.height = info->vdisplay;
1169 /* Calculate higher precision (mHz) refresh rate */
1170 refresh = (info->clock * 1000000LL / info->htotal +
1171 info->vtotal / 2) / info->vtotal;
1173 if (info->flags & DRM_MODE_FLAG_INTERLACE)
1175 if (info->flags & DRM_MODE_FLAG_DBLSCAN)
1177 if (info->vscan > 1)
1178 refresh /= info->vscan;
1180 mode->base.refresh = refresh;
1181 mode->mode_info = *info;
1183 if (info->type & DRM_MODE_TYPE_PREFERRED)
1184 mode->base.flags |= WL_OUTPUT_MODE_PREFERRED;
1186 wl_list_insert(output->base.mode_list.prev, &mode->base.link);
1192 drm_subpixel_to_wayland(int drm_value)
1194 switch (drm_value) {
1196 case DRM_MODE_SUBPIXEL_UNKNOWN:
1197 return WL_OUTPUT_SUBPIXEL_UNKNOWN;
1198 case DRM_MODE_SUBPIXEL_NONE:
1199 return WL_OUTPUT_SUBPIXEL_NONE;
1200 case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB:
1201 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB;
1202 case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR:
1203 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR;
1204 case DRM_MODE_SUBPIXEL_VERTICAL_RGB:
1205 return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB;
1206 case DRM_MODE_SUBPIXEL_VERTICAL_BGR:
1207 return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR;
1212 sprite_handle_buffer_destroy(struct wl_listener *listener, void *data)
1214 struct drm_sprite *sprite =
1215 container_of(listener, struct drm_sprite,
1217 struct drm_compositor *compositor = sprite->compositor;
1219 sprite->surface = NULL;
1220 drmModeRmFB(compositor->drm.fd, sprite->fb_id);
1225 sprite_handle_pending_buffer_destroy(struct wl_listener *listener, void *data)
1227 struct drm_sprite *sprite =
1228 container_of(listener, struct drm_sprite,
1229 pending_destroy_listener);
1230 struct drm_compositor *compositor = sprite->compositor;
1232 sprite->pending_surface = NULL;
1233 drmModeRmFB(compositor->drm.fd, sprite->pending_fb_id);
1234 sprite->pending_fb_id = 0;
1237 /* returns a value between 0-255 range, where higher is brighter */
1239 drm_get_backlight(struct drm_output *output)
1241 long brightness, max_brightness, norm;
1243 brightness = backlight_get_brightness(output->backlight);
1244 max_brightness = backlight_get_max_brightness(output->backlight);
1246 /* convert it on a scale of 0 to 255 */
1247 norm = (brightness * 255)/(max_brightness);
1249 return (uint32_t) norm;
1252 /* values accepted are between 0-255 range */
1254 drm_set_backlight(struct weston_output *output_base, uint32_t value)
1256 struct drm_output *output = (struct drm_output *) output_base;
1257 long max_brightness, new_brightness;
1259 if (!output->backlight)
1265 max_brightness = backlight_get_max_brightness(output->backlight);
1267 /* get denormalized value */
1268 new_brightness = (value * max_brightness) / 255;
1270 backlight_set_brightness(output->backlight, new_brightness);
1273 static drmModePropertyPtr
1274 drm_get_prop(int fd, drmModeConnectorPtr connector, const char *name)
1276 drmModePropertyPtr props;
1279 for (i = 0; i < connector->count_props; i++) {
1280 props = drmModeGetProperty(fd, connector->props[i]);
1284 if (!strcmp(props->name, name))
1287 drmModeFreeProperty(props);
1294 drm_set_dpms(struct weston_output *output_base, enum dpms_enum level)
1296 struct drm_output *output = (struct drm_output *) output_base;
1297 struct weston_compositor *ec = output_base->compositor;
1298 struct drm_compositor *c = (struct drm_compositor *) ec;
1299 drmModeConnectorPtr connector;
1300 drmModePropertyPtr prop;
1302 connector = drmModeGetConnector(c->drm.fd, output->connector_id);
1306 prop = drm_get_prop(c->drm.fd, connector, "DPMS");
1308 drmModeFreeConnector(connector);
1312 drmModeConnectorSetProperty(c->drm.fd, connector->connector_id,
1313 prop->prop_id, level);
1314 drmModeFreeProperty(prop);
1315 drmModeFreeConnector(connector);
1318 static const char *connector_type_names[] = {
1337 find_crtc_for_connector(struct drm_compositor *ec,
1338 drmModeRes *resources, drmModeConnector *connector)
1340 drmModeEncoder *encoder;
1341 uint32_t possible_crtcs;
1344 for (j = 0; j < connector->count_encoders; j++) {
1345 encoder = drmModeGetEncoder(ec->drm.fd, connector->encoders[j]);
1346 if (encoder == NULL) {
1347 weston_log("Failed to get encoder.\n");
1350 possible_crtcs = encoder->possible_crtcs;
1351 drmModeFreeEncoder(encoder);
1353 for (i = 0; i < resources->count_crtcs; i++) {
1354 if (possible_crtcs & (1 << i) &&
1355 !(ec->crtc_allocator & (1 << resources->crtcs[i])))
1364 create_output_for_connector(struct drm_compositor *ec,
1365 drmModeRes *resources,
1366 drmModeConnector *connector,
1367 int x, int y, struct udev_device *drm_device)
1369 struct drm_output *output;
1370 struct drm_mode *drm_mode, *next, *preferred, *current, *configured;
1371 struct weston_mode *m;
1372 struct drm_configured_output *o = NULL, *temp;
1373 drmModeEncoder *encoder;
1374 drmModeModeInfo crtc_mode;
1378 const char *type_name;
1380 i = find_crtc_for_connector(ec, resources, connector);
1382 weston_log("No usable crtc/encoder pair for connector.\n");
1386 output = malloc(sizeof *output);
1390 memset(output, 0, sizeof *output);
1391 output->base.subpixel = drm_subpixel_to_wayland(connector->subpixel);
1392 output->base.make = "unknown";
1393 output->base.model = "unknown";
1394 wl_list_init(&output->base.mode_list);
1396 if (connector->connector_type < ARRAY_LENGTH(connector_type_names))
1397 type_name = connector_type_names[connector->connector_type];
1399 type_name = "UNKNOWN";
1400 snprintf(name, 32, "%s%d", type_name, connector->connector_type_id);
1401 output->name = strdup(name);
1403 output->crtc_id = resources->crtcs[i];
1405 ec->crtc_allocator |= (1 << output->crtc_id);
1406 output->connector_id = connector->connector_id;
1407 ec->connector_allocator |= (1 << output->connector_id);
1409 output->original_crtc = drmModeGetCrtc(ec->drm.fd, output->crtc_id);
1411 /* Get the current mode on the crtc that's currently driving
1412 * this connector. */
1413 encoder = drmModeGetEncoder(ec->drm.fd, connector->encoder_id);
1414 memset(&crtc_mode, 0, sizeof crtc_mode);
1415 if (encoder != NULL) {
1416 crtc = drmModeGetCrtc(ec->drm.fd, encoder->crtc_id);
1417 drmModeFreeEncoder(encoder);
1420 if (crtc->mode_valid)
1421 crtc_mode = crtc->mode;
1422 drmModeFreeCrtc(crtc);
1425 for (i = 0; i < connector->count_modes; i++) {
1426 drm_mode = drm_output_add_mode(output, &connector->modes[i]);
1435 wl_list_for_each(temp, &configured_output_list, link) {
1436 if (strcmp(temp->name, output->name) == 0) {
1437 weston_log("%s mode \"%s\" in config\n",
1438 temp->name, temp->mode);
1444 if (o && o->config == OUTPUT_CONFIG_OFF) {
1445 weston_log("Disabling output %s\n", o->name);
1447 drmModeSetCrtc(ec->drm.fd, output->crtc_id,
1448 0, 0, 0, 0, 0, NULL);
1452 wl_list_for_each(drm_mode, &output->base.mode_list, base.link) {
1453 if (o && o->width == drm_mode->base.width &&
1454 o->height == drm_mode->base.height &&
1455 o->config == OUTPUT_CONFIG_MODE)
1456 configured = drm_mode;
1457 if (!memcmp(&crtc_mode, &drm_mode->mode_info, sizeof crtc_mode))
1459 if (drm_mode->base.flags & WL_OUTPUT_MODE_PREFERRED)
1460 preferred = drm_mode;
1463 if (o && o->config == OUTPUT_CONFIG_MODELINE) {
1464 configured = drm_output_add_mode(output, &o->crtc_mode);
1467 current = configured;
1470 if (current == NULL && crtc_mode.clock != 0) {
1471 current = drm_output_add_mode(output, &crtc_mode);
1476 if (o && o->config == OUTPUT_CONFIG_CURRENT)
1477 configured = current;
1479 if (option_current_mode && current)
1480 output->base.current = ¤t->base;
1481 else if (configured)
1482 output->base.current = &configured->base;
1484 output->base.current = &preferred->base;
1486 output->base.current = ¤t->base;
1488 if (output->base.current == NULL) {
1489 weston_log("no available modes for %s\n", output->name);
1493 output->base.current->flags |= WL_OUTPUT_MODE_CURRENT;
1495 output->surface = gbm_surface_create(ec->gbm,
1496 output->base.current->width,
1497 output->base.current->height,
1498 GBM_FORMAT_XRGB8888,
1499 GBM_BO_USE_SCANOUT |
1500 GBM_BO_USE_RENDERING);
1501 if (!output->surface) {
1502 weston_log("failed to create gbm surface\n");
1506 output->egl_surface =
1507 eglCreateWindowSurface(ec->base.egl_display,
1508 ec->base.egl_config,
1511 if (output->egl_surface == EGL_NO_SURFACE) {
1512 weston_log("failed to create egl surface\n");
1516 output->cursor_bo[0] =
1517 gbm_bo_create(ec->gbm, 64, 64, GBM_FORMAT_ARGB8888,
1518 GBM_BO_USE_CURSOR_64X64 | GBM_BO_USE_WRITE);
1519 output->cursor_bo[1] =
1520 gbm_bo_create(ec->gbm, 64, 64, GBM_FORMAT_ARGB8888,
1521 GBM_BO_USE_CURSOR_64X64 | GBM_BO_USE_WRITE);
1523 output->backlight = backlight_init(drm_device,
1524 connector->connector_type);
1525 if (output->backlight) {
1526 output->base.set_backlight = drm_set_backlight;
1527 output->base.backlight_current = drm_get_backlight(output);
1530 weston_output_init(&output->base, &ec->base, x, y,
1531 connector->mmWidth, connector->mmHeight);
1533 wl_list_insert(ec->base.output_list.prev, &output->base.link);
1535 output->base.origin = output->base.current;
1536 output->base.repaint = drm_output_repaint;
1537 output->base.destroy = drm_output_destroy;
1538 output->base.assign_planes = drm_assign_planes;
1539 output->base.set_dpms = drm_set_dpms;
1540 output->base.switch_mode = drm_output_switch_mode;
1542 weston_plane_init(&output->cursor_plane, 0, 0);
1543 weston_plane_init(&output->fb_plane, 0, 0);
1545 weston_log("Output %s, (connector %d, crtc %d)\n",
1546 output->name, output->connector_id, output->crtc_id);
1547 wl_list_for_each(m, &output->base.mode_list, link)
1548 weston_log_continue(" mode %dx%d@%.1f%s%s%s\n",
1549 m->width, m->height, m->refresh / 1000.0,
1550 m->flags & WL_OUTPUT_MODE_PREFERRED ?
1552 m->flags & WL_OUTPUT_MODE_CURRENT ?
1554 connector->count_modes == 0 ?
1560 gbm_surface_destroy(output->surface);
1562 wl_list_for_each_safe(drm_mode, next, &output->base.mode_list,
1564 wl_list_remove(&drm_mode->base.link);
1568 drmModeFreeCrtc(output->original_crtc);
1569 ec->crtc_allocator &= ~(1 << output->crtc_id);
1570 ec->connector_allocator &= ~(1 << output->connector_id);
1578 create_sprites(struct drm_compositor *ec)
1580 struct drm_sprite *sprite;
1581 drmModePlaneRes *plane_res;
1582 drmModePlane *plane;
1585 plane_res = drmModeGetPlaneResources(ec->drm.fd);
1587 weston_log("failed to get plane resources: %s\n",
1592 for (i = 0; i < plane_res->count_planes; i++) {
1593 plane = drmModeGetPlane(ec->drm.fd, plane_res->planes[i]);
1597 sprite = malloc(sizeof(*sprite) + ((sizeof(uint32_t)) *
1598 plane->count_formats));
1600 weston_log("%s: out of memory\n",
1606 memset(sprite, 0, sizeof *sprite);
1608 sprite->possible_crtcs = plane->possible_crtcs;
1609 sprite->plane_id = plane->plane_id;
1610 sprite->surface = NULL;
1611 sprite->pending_surface = NULL;
1613 sprite->pending_fb_id = 0;
1614 sprite->destroy_listener.notify = sprite_handle_buffer_destroy;
1615 sprite->pending_destroy_listener.notify =
1616 sprite_handle_pending_buffer_destroy;
1617 sprite->compositor = ec;
1618 sprite->count_formats = plane->count_formats;
1619 memcpy(sprite->formats, plane->formats,
1620 plane->count_formats * sizeof(plane->formats[0]));
1621 drmModeFreePlane(plane);
1622 weston_plane_init(&sprite->plane, 0, 0);
1624 wl_list_insert(&ec->sprite_list, &sprite->link);
1627 free(plane_res->planes);
1632 destroy_sprites(struct drm_compositor *compositor)
1634 struct drm_sprite *sprite, *next;
1635 struct drm_output *output;
1637 output = container_of(compositor->base.output_list.next,
1638 struct drm_output, base.link);
1640 wl_list_for_each_safe(sprite, next, &compositor->sprite_list, link) {
1641 drmModeSetPlane(compositor->drm.fd,
1643 output->crtc_id, 0, 0,
1644 0, 0, 0, 0, 0, 0, 0, 0);
1645 drmModeRmFB(compositor->drm.fd, sprite->fb_id);
1646 weston_plane_release(&sprite->plane);
1652 create_outputs(struct drm_compositor *ec, uint32_t option_connector,
1653 struct udev_device *drm_device)
1655 drmModeConnector *connector;
1656 drmModeRes *resources;
1660 resources = drmModeGetResources(ec->drm.fd);
1662 weston_log("drmModeGetResources failed\n");
1666 ec->crtcs = calloc(resources->count_crtcs, sizeof(uint32_t));
1668 drmModeFreeResources(resources);
1672 ec->min_width = resources->min_width;
1673 ec->max_width = resources->max_width;
1674 ec->min_height = resources->min_height;
1675 ec->max_height = resources->max_height;
1677 ec->num_crtcs = resources->count_crtcs;
1678 memcpy(ec->crtcs, resources->crtcs, sizeof(uint32_t) * ec->num_crtcs);
1680 for (i = 0; i < resources->count_connectors; i++) {
1681 connector = drmModeGetConnector(ec->drm.fd,
1682 resources->connectors[i]);
1683 if (connector == NULL)
1686 if (connector->connection == DRM_MODE_CONNECTED &&
1687 (option_connector == 0 ||
1688 connector->connector_id == option_connector)) {
1689 if (create_output_for_connector(ec, resources,
1692 drmModeFreeConnector(connector);
1696 x += container_of(ec->base.output_list.prev,
1697 struct weston_output,
1698 link)->current->width;
1701 drmModeFreeConnector(connector);
1704 if (wl_list_empty(&ec->base.output_list)) {
1705 weston_log("No currently active connector found.\n");
1706 drmModeFreeResources(resources);
1710 drmModeFreeResources(resources);
1716 update_outputs(struct drm_compositor *ec, struct udev_device *drm_device)
1718 drmModeConnector *connector;
1719 drmModeRes *resources;
1720 struct drm_output *output, *next;
1722 int x_offset = 0, y_offset = 0;
1723 uint32_t connected = 0, disconnects = 0;
1726 resources = drmModeGetResources(ec->drm.fd);
1728 weston_log("drmModeGetResources failed\n");
1732 /* collect new connects */
1733 for (i = 0; i < resources->count_connectors; i++) {
1734 int connector_id = resources->connectors[i];
1736 connector = drmModeGetConnector(ec->drm.fd, connector_id);
1737 if (connector == NULL)
1740 if (connector->connection != DRM_MODE_CONNECTED) {
1741 drmModeFreeConnector(connector);
1745 connected |= (1 << connector_id);
1747 if (!(ec->connector_allocator & (1 << connector_id))) {
1748 struct weston_output *last =
1749 container_of(ec->base.output_list.prev,
1750 struct weston_output, link);
1752 /* XXX: not yet needed, we die with 0 outputs */
1753 if (!wl_list_empty(&ec->base.output_list))
1754 x = last->x + last->current->width;
1758 create_output_for_connector(ec, resources,
1761 weston_log("connector %d connected\n", connector_id);
1764 drmModeFreeConnector(connector);
1766 drmModeFreeResources(resources);
1768 disconnects = ec->connector_allocator & ~connected;
1770 wl_list_for_each_safe(output, next, &ec->base.output_list,
1772 if (x_offset != 0 || y_offset != 0) {
1773 weston_output_move(&output->base,
1774 output->base.x - x_offset,
1775 output->base.y - y_offset);
1778 if (disconnects & (1 << output->connector_id)) {
1779 disconnects &= ~(1 << output->connector_id);
1780 weston_log("connector %d disconnected\n",
1781 output->connector_id);
1782 x_offset += output->base.current->width;
1783 drm_output_destroy(&output->base);
1788 /* FIXME: handle zero outputs, without terminating */
1789 if (ec->connector_allocator == 0)
1790 wl_display_terminate(ec->base.wl_display);
1794 udev_event_is_hotplug(struct drm_compositor *ec, struct udev_device *device)
1799 sysnum = udev_device_get_sysnum(device);
1800 if (!sysnum || atoi(sysnum) != ec->drm.id)
1803 val = udev_device_get_property_value(device, "HOTPLUG");
1807 return strcmp(val, "1") == 0;
1811 udev_drm_event(int fd, uint32_t mask, void *data)
1813 struct drm_compositor *ec = data;
1814 struct udev_device *event;
1816 event = udev_monitor_receive_device(ec->udev_monitor);
1818 if (udev_event_is_hotplug(ec, event))
1819 update_outputs(ec, event);
1821 udev_device_unref(event);
1827 drm_restore(struct weston_compositor *ec)
1829 struct drm_compositor *d = (struct drm_compositor *) ec;
1831 if (weston_launcher_drm_set_master(&d->base, d->drm.fd, 0) < 0)
1832 weston_log("failed to drop master: %m\n");
1836 static const char default_seat[] = "seat0";
1839 device_added(struct udev_device *udev_device, struct drm_seat *master)
1841 struct weston_compositor *c;
1842 struct evdev_device *device;
1843 const char *devnode;
1844 const char *device_seat;
1847 device_seat = udev_device_get_property_value(udev_device, "ID_SEAT");
1849 device_seat = default_seat;
1851 if (strcmp(device_seat, master->seat_id))
1854 c = master->base.compositor;
1855 devnode = udev_device_get_devnode(udev_device);
1857 /* Use non-blocking mode so that we can loop on read on
1858 * evdev_device_data() until all events on the fd are
1859 * read. mtdev_get() also expects this. */
1860 fd = weston_launcher_open(c, devnode, O_RDWR | O_NONBLOCK);
1862 weston_log("opening input device '%s' failed.\n", devnode);
1866 device = evdev_device_create(&master->base, devnode, fd);
1869 weston_log("not using input device '%s'.\n", devnode);
1873 wl_list_insert(master->devices_list.prev, &device->link);
1877 evdev_add_devices(struct udev *udev, struct weston_seat *seat_base)
1879 struct drm_seat *seat = (struct drm_seat *) seat_base;
1880 struct udev_enumerate *e;
1881 struct udev_list_entry *entry;
1882 struct udev_device *device;
1883 const char *path, *sysname;
1885 e = udev_enumerate_new(udev);
1886 udev_enumerate_add_match_subsystem(e, "input");
1887 udev_enumerate_scan_devices(e);
1888 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
1889 path = udev_list_entry_get_name(entry);
1890 device = udev_device_new_from_syspath(udev, path);
1892 sysname = udev_device_get_sysname(device);
1893 if (strncmp("event", sysname, 5) != 0) {
1894 udev_device_unref(device);
1898 device_added(device, seat);
1900 udev_device_unref(device);
1902 udev_enumerate_unref(e);
1904 evdev_notify_keyboard_focus(&seat->base, &seat->devices_list);
1906 if (wl_list_empty(&seat->devices_list)) {
1908 "warning: no input devices on entering Weston. "
1909 "Possible causes:\n"
1910 "\t- no permissions to read /dev/input/event*\n"
1911 "\t- seats misconfigured "
1912 "(Weston backend option 'seat', "
1913 "udev device property ID_SEAT)\n");
1918 evdev_udev_handler(int fd, uint32_t mask, void *data)
1920 struct drm_seat *seat = data;
1921 struct udev_device *udev_device;
1922 struct evdev_device *device, *next;
1924 const char *devnode;
1926 udev_device = udev_monitor_receive_device(seat->udev_monitor);
1930 action = udev_device_get_action(udev_device);
1934 if (strncmp("event", udev_device_get_sysname(udev_device), 5) != 0)
1937 if (!strcmp(action, "add")) {
1938 device_added(udev_device, seat);
1940 else if (!strcmp(action, "remove")) {
1941 devnode = udev_device_get_devnode(udev_device);
1942 wl_list_for_each_safe(device, next, &seat->devices_list, link)
1943 if (!strcmp(device->devnode, devnode)) {
1944 weston_log("input device %s, %s removed\n",
1945 device->devname, device->devnode);
1946 evdev_device_destroy(device);
1952 udev_device_unref(udev_device);
1958 evdev_enable_udev_monitor(struct udev *udev, struct weston_seat *seat_base)
1960 struct drm_seat *master = (struct drm_seat *) seat_base;
1961 struct wl_event_loop *loop;
1962 struct weston_compositor *c = master->base.compositor;
1965 master->udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
1966 if (!master->udev_monitor) {
1967 weston_log("udev: failed to create the udev monitor\n");
1971 udev_monitor_filter_add_match_subsystem_devtype(master->udev_monitor,
1974 if (udev_monitor_enable_receiving(master->udev_monitor)) {
1975 weston_log("udev: failed to bind the udev monitor\n");
1976 udev_monitor_unref(master->udev_monitor);
1980 loop = wl_display_get_event_loop(c->wl_display);
1981 fd = udev_monitor_get_fd(master->udev_monitor);
1982 master->udev_monitor_source =
1983 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1984 evdev_udev_handler, master);
1985 if (!master->udev_monitor_source) {
1986 udev_monitor_unref(master->udev_monitor);
1994 evdev_disable_udev_monitor(struct weston_seat *seat_base)
1996 struct drm_seat *seat = (struct drm_seat *) seat_base;
1998 if (!seat->udev_monitor)
2001 udev_monitor_unref(seat->udev_monitor);
2002 seat->udev_monitor = NULL;
2003 wl_event_source_remove(seat->udev_monitor_source);
2004 seat->udev_monitor_source = NULL;
2008 drm_led_update(struct weston_seat *seat_base, enum weston_led leds)
2010 struct drm_seat *seat = (struct drm_seat *) seat_base;
2011 struct evdev_device *device;
2013 wl_list_for_each(device, &seat->devices_list, link)
2014 evdev_led_update(device, leds);
2018 evdev_input_create(struct weston_compositor *c, struct udev *udev,
2019 const char *seat_id)
2021 struct drm_seat *seat;
2023 seat = malloc(sizeof *seat);
2027 memset(seat, 0, sizeof *seat);
2028 weston_seat_init(&seat->base, c);
2029 seat->base.led_update = drm_led_update;
2031 wl_list_init(&seat->devices_list);
2032 seat->seat_id = strdup(seat_id);
2033 if (!evdev_enable_udev_monitor(udev, &seat->base)) {
2034 free(seat->seat_id);
2039 evdev_add_devices(udev, &seat->base);
2041 c->seat = &seat->base;
2045 evdev_remove_devices(struct weston_seat *seat_base)
2047 struct drm_seat *seat = (struct drm_seat *) seat_base;
2048 struct evdev_device *device, *next;
2050 wl_list_for_each_safe(device, next, &seat->devices_list, link)
2051 evdev_device_destroy(device);
2053 if (seat->base.seat.keyboard)
2054 notify_keyboard_focus_out(&seat->base.seat);
2058 evdev_input_destroy(struct weston_seat *seat_base)
2060 struct drm_seat *seat = (struct drm_seat *) seat_base;
2062 evdev_remove_devices(seat_base);
2063 evdev_disable_udev_monitor(&seat->base);
2065 weston_seat_release(seat_base);
2066 free(seat->seat_id);
2071 drm_free_configured_output(struct drm_configured_output *output)
2079 drm_destroy(struct weston_compositor *ec)
2081 struct drm_compositor *d = (struct drm_compositor *) ec;
2082 struct weston_seat *seat, *next;
2083 struct drm_configured_output *o, *n;
2085 wl_list_for_each_safe(seat, next, &ec->seat_list, link)
2086 evdev_input_destroy(seat);
2087 wl_list_for_each_safe(o, n, &configured_output_list, link)
2088 drm_free_configured_output(o);
2090 wl_event_source_remove(d->udev_drm_source);
2091 wl_event_source_remove(d->drm_source);
2093 weston_compositor_shutdown(ec);
2095 /* Work around crash in egl_dri2.c's dri2_make_current() */
2096 eglMakeCurrent(ec->egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
2098 eglTerminate(ec->egl_display);
2101 gbm_device_destroy(d->gbm);
2103 if (weston_launcher_drm_set_master(&d->base, d->drm.fd, 0) < 0)
2104 weston_log("failed to drop master: %m\n");
2105 tty_destroy(d->tty);
2111 drm_compositor_set_modes(struct drm_compositor *compositor)
2113 struct drm_output *output;
2114 struct drm_mode *drm_mode;
2117 wl_list_for_each(output, &compositor->base.output_list, base.link) {
2118 drm_mode = (struct drm_mode *) output->base.current;
2119 ret = drmModeSetCrtc(compositor->drm.fd, output->crtc_id,
2120 output->current->fb_id, 0, 0,
2121 &output->connector_id, 1,
2122 &drm_mode->mode_info);
2125 "failed to set mode %dx%d for output at %d,%d: %m\n",
2126 drm_mode->base.width, drm_mode->base.height,
2127 output->base.x, output->base.y);
2133 vt_func(struct weston_compositor *compositor, int event)
2135 struct drm_compositor *ec = (struct drm_compositor *) compositor;
2136 struct weston_seat *seat;
2137 struct drm_sprite *sprite;
2138 struct drm_output *output;
2142 weston_log("entering VT\n");
2143 compositor->focus = 1;
2144 if (weston_launcher_drm_set_master(&ec->base, ec->drm.fd, 1)) {
2145 weston_log("failed to set master: %m\n");
2146 wl_display_terminate(compositor->wl_display);
2148 compositor->state = ec->prev_state;
2149 drm_compositor_set_modes(ec);
2150 weston_compositor_damage_all(compositor);
2151 wl_list_for_each(seat, &compositor->seat_list, link) {
2152 evdev_add_devices(ec->udev, seat);
2153 evdev_enable_udev_monitor(ec->udev, seat);
2157 weston_log("leaving VT\n");
2158 wl_list_for_each(seat, &compositor->seat_list, link) {
2159 evdev_disable_udev_monitor(seat);
2160 evdev_remove_devices(seat);
2163 compositor->focus = 0;
2164 ec->prev_state = compositor->state;
2165 compositor->state = WESTON_COMPOSITOR_SLEEPING;
2167 /* If we have a repaint scheduled (either from a
2168 * pending pageflip or the idle handler), make sure we
2169 * cancel that so we don't try to pageflip when we're
2170 * vt switched away. The SLEEPING state will prevent
2171 * further attemps at repainting. When we switch
2172 * back, we schedule a repaint, which will process
2173 * pending frame callbacks. */
2175 wl_list_for_each(output, &ec->base.output_list, base.link) {
2176 output->base.repaint_needed = 0;
2177 drmModeSetCursor(ec->drm.fd, output->crtc_id, 0, 0, 0);
2180 output = container_of(ec->base.output_list.next,
2181 struct drm_output, base.link);
2183 wl_list_for_each(sprite, &ec->sprite_list, link)
2184 drmModeSetPlane(ec->drm.fd,
2186 output->crtc_id, 0, 0,
2187 0, 0, 0, 0, 0, 0, 0, 0);
2189 if (weston_launcher_drm_set_master(&ec->base, ec->drm.fd, 0) < 0)
2190 weston_log("failed to drop master: %m\n");
2197 switch_vt_binding(struct wl_seat *seat, uint32_t time, uint32_t key, void *data)
2199 struct drm_compositor *ec = data;
2201 tty_activate_vt(ec->tty, key - KEY_F1 + 1);
2204 static struct weston_compositor *
2205 drm_compositor_create(struct wl_display *display,
2206 int connector, const char *seat, int tty,
2207 int argc, char *argv[], const char *config_file)
2209 struct drm_compositor *ec;
2210 struct udev_enumerate *e;
2211 struct udev_list_entry *entry;
2212 struct udev_device *device, *drm_device;
2213 const char *path, *device_seat;
2214 struct wl_event_loop *loop;
2215 struct weston_seat *weston_seat, *next;
2218 weston_log("initializing drm backend\n");
2220 ec = malloc(sizeof *ec);
2223 memset(ec, 0, sizeof *ec);
2225 if (weston_compositor_init(&ec->base, display, argc, argv,
2227 weston_log("weston_compositor_init failed\n");
2231 ec->udev = udev_new();
2232 if (ec->udev == NULL) {
2233 weston_log("failed to initialize udev context\n");
2234 goto err_compositor;
2237 ec->base.wl_display = display;
2238 ec->tty = tty_create(&ec->base, vt_func, tty);
2240 weston_log("failed to initialize tty\n");
2244 e = udev_enumerate_new(ec->udev);
2245 udev_enumerate_add_match_subsystem(e, "drm");
2246 udev_enumerate_add_match_sysname(e, "card[0-9]*");
2248 udev_enumerate_scan_devices(e);
2250 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
2251 path = udev_list_entry_get_name(entry);
2252 device = udev_device_new_from_syspath(ec->udev, path);
2254 udev_device_get_property_value(device, "ID_SEAT");
2256 device_seat = default_seat;
2257 if (strcmp(device_seat, seat) == 0) {
2258 drm_device = device;
2261 udev_device_unref(device);
2264 if (drm_device == NULL) {
2265 weston_log("no drm device found\n");
2269 if (init_egl(ec, drm_device) < 0) {
2270 weston_log("failed to initialize egl\n");
2274 ec->base.destroy = drm_destroy;
2275 ec->base.restore = drm_restore;
2279 ec->prev_state = WESTON_COMPOSITOR_ACTIVE;
2281 if (weston_compositor_init_gl(&ec->base) < 0)
2284 for (key = KEY_F1; key < KEY_F9; key++)
2285 weston_compositor_add_key_binding(&ec->base, key,
2286 MODIFIER_CTRL | MODIFIER_ALT,
2287 switch_vt_binding, ec);
2289 wl_list_init(&ec->sprite_list);
2292 if (create_outputs(ec, connector, drm_device) < 0) {
2293 weston_log("failed to create output for %s\n", path);
2299 evdev_input_create(&ec->base, ec->udev, seat);
2301 loop = wl_display_get_event_loop(ec->base.wl_display);
2303 wl_event_loop_add_fd(loop, ec->drm.fd,
2304 WL_EVENT_READABLE, on_drm_input, ec);
2306 ec->udev_monitor = udev_monitor_new_from_netlink(ec->udev, "udev");
2307 if (ec->udev_monitor == NULL) {
2308 weston_log("failed to intialize udev monitor\n");
2309 goto err_drm_source;
2311 udev_monitor_filter_add_match_subsystem_devtype(ec->udev_monitor,
2313 ec->udev_drm_source =
2314 wl_event_loop_add_fd(loop,
2315 udev_monitor_get_fd(ec->udev_monitor),
2316 WL_EVENT_READABLE, udev_drm_event, ec);
2318 if (udev_monitor_enable_receiving(ec->udev_monitor) < 0) {
2319 weston_log("failed to enable udev-monitor receiving\n");
2320 goto err_udev_monitor;
2323 udev_device_unref(drm_device);
2324 udev_enumerate_unref(e);
2329 wl_event_source_remove(ec->udev_drm_source);
2330 udev_monitor_unref(ec->udev_monitor);
2332 wl_event_source_remove(ec->drm_source);
2333 wl_list_for_each_safe(weston_seat, next, &ec->base.seat_list, link)
2334 evdev_input_destroy(weston_seat);
2336 destroy_sprites(ec);
2338 eglMakeCurrent(ec->base.egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
2340 eglTerminate(ec->base.egl_display);
2342 gbm_device_destroy(ec->gbm);
2344 udev_device_unref(drm_device);
2346 udev_enumerate_unref(e);
2347 tty_destroy(ec->tty);
2349 udev_unref(ec->udev);
2351 weston_compositor_shutdown(&ec->base);
2358 set_sync_flags(drmModeModeInfo *mode, char *hsync, char *vsync)
2362 if (strcmp(hsync, "+hsync") == 0)
2363 mode->flags |= DRM_MODE_FLAG_PHSYNC;
2364 else if (strcmp(hsync, "-hsync") == 0)
2365 mode->flags |= DRM_MODE_FLAG_NHSYNC;
2369 if (strcmp(vsync, "+vsync") == 0)
2370 mode->flags |= DRM_MODE_FLAG_PVSYNC;
2371 else if (strcmp(vsync, "-vsync") == 0)
2372 mode->flags |= DRM_MODE_FLAG_NVSYNC;
2380 check_for_modeline(struct drm_configured_output *output)
2382 drmModeModeInfo mode;
2388 mode.type = DRM_MODE_TYPE_USERDEF;
2393 if (sscanf(output_mode, "%f %hd %hd %hd %hd %hd %hd %hd %hd %s %s",
2394 &fclock, &mode.hdisplay,
2396 &mode.hsync_end, &mode.htotal,
2399 &mode.vsync_end, &mode.vtotal,
2400 hsync, vsync) == 11) {
2401 if (set_sync_flags(&mode, hsync, vsync))
2404 sprintf(mode_name, "%dx%d", mode.hdisplay, mode.vdisplay);
2405 strcpy(mode.name, mode_name);
2407 mode.clock = fclock * 1000;
2411 output->crtc_mode = mode;
2417 output_section_done(void *data)
2419 struct drm_configured_output *output;
2421 output = malloc(sizeof *output);
2423 if (!output || !output_name || !output_mode) {
2431 output->config = OUTPUT_CONFIG_INVALID;
2432 output->name = output_name;
2433 output->mode = output_mode;
2435 if (strcmp(output_mode, "off") == 0)
2436 output->config = OUTPUT_CONFIG_OFF;
2437 else if (strcmp(output_mode, "preferred") == 0)
2438 output->config = OUTPUT_CONFIG_PREFERRED;
2439 else if (strcmp(output_mode, "current") == 0)
2440 output->config = OUTPUT_CONFIG_CURRENT;
2441 else if (sscanf(output_mode, "%dx%d", &output->width, &output->height) == 2)
2442 output->config = OUTPUT_CONFIG_MODE;
2443 else if (check_for_modeline(output) == 0)
2444 output->config = OUTPUT_CONFIG_MODELINE;
2446 if (output->config != OUTPUT_CONFIG_INVALID)
2447 wl_list_insert(&configured_output_list, &output->link);
2449 weston_log("Invalid mode \"%s\" for output %s\n",
2450 output_mode, output_name);
2451 drm_free_configured_output(output);
2455 WL_EXPORT struct weston_compositor *
2456 backend_init(struct wl_display *display, int argc, char *argv[],
2457 const char *config_file)
2459 int connector = 0, tty = 0;
2460 const char *seat = default_seat;
2462 const struct weston_option drm_options[] = {
2463 { WESTON_OPTION_INTEGER, "connector", 0, &connector },
2464 { WESTON_OPTION_STRING, "seat", 0, &seat },
2465 { WESTON_OPTION_INTEGER, "tty", 0, &tty },
2466 { WESTON_OPTION_BOOLEAN, "current-mode", 0, &option_current_mode },
2469 parse_options(drm_options, ARRAY_LENGTH(drm_options), argc, argv);
2471 wl_list_init(&configured_output_list);
2473 const struct config_key drm_config_keys[] = {
2474 { "name", CONFIG_KEY_STRING, &output_name },
2475 { "mode", CONFIG_KEY_STRING, &output_mode },
2478 const struct config_section config_section[] = {
2479 { "output", drm_config_keys,
2480 ARRAY_LENGTH(drm_config_keys), output_section_done },
2483 parse_config_file(config_file, config_section,
2484 ARRAY_LENGTH(config_section), NULL);
2486 return drm_compositor_create(display, connector, seat, tty, argc, argv,