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>
34 #include <xf86drmMode.h>
35 #include <drm_fourcc.h>
38 #include <libbacklight.h>
40 #include "compositor.h"
42 #include "launcher-util.h"
46 WESTON_PLANE_DRM_CURSOR = 0x100
49 struct drm_compositor {
50 struct weston_compositor base;
53 struct wl_event_source *drm_source;
55 struct udev_monitor *udev_monitor;
56 struct wl_event_source *udev_drm_source;
62 struct gbm_device *gbm;
65 uint32_t crtc_allocator;
66 uint32_t connector_allocator;
69 struct gbm_surface *dummy_surface;
70 EGLSurface dummy_egl_surface;
72 struct wl_list sprite_list;
73 int sprites_are_broken;
79 struct weston_mode base;
80 drmModeModeInfo mode_info;
87 struct drm_output *output;
90 struct wl_buffer *buffer;
91 struct wl_listener buffer_destroy_listener;
95 struct weston_output base;
98 uint32_t connector_id;
99 drmModeCrtcPtr original_crtc;
102 int page_flip_pending;
104 struct gbm_surface *surface;
105 struct gbm_bo *cursor_bo[2];
107 EGLSurface egl_surface;
108 struct drm_fb *current, *next;
109 struct backlight *backlight;
113 * An output has a primary display plane plus zero or more sprites for
114 * blending display contents.
120 uint32_t pending_fb_id;
121 struct weston_surface *surface;
122 struct weston_surface *pending_surface;
124 struct drm_output *output;
126 struct drm_compositor *compositor;
128 struct wl_listener destroy_listener;
129 struct wl_listener pending_destroy_listener;
131 uint32_t possible_crtcs;
133 uint32_t count_formats;
135 int32_t src_x, src_y;
136 uint32_t src_w, src_h;
137 uint32_t dest_x, dest_y;
138 uint32_t dest_w, dest_h;
144 surface_is_primary(struct weston_compositor *ec, struct weston_surface *es)
146 struct weston_surface *primary;
148 primary = container_of(ec->surface_list.next, struct weston_surface,
156 drm_sprite_crtc_supported(struct weston_output *output_base, uint32_t supported)
158 struct weston_compositor *ec = output_base->compositor;
159 struct drm_compositor *c =(struct drm_compositor *) ec;
160 struct drm_output *output = (struct drm_output *) output_base;
163 for (crtc = 0; crtc < c->num_crtcs; crtc++) {
164 if (c->crtcs[crtc] != output->crtc_id)
167 if (supported & (1 << crtc))
175 drm_fb_destroy_callback(struct gbm_bo *bo, void *data)
177 struct drm_fb *fb = data;
178 struct gbm_device *gbm = gbm_bo_get_device(bo);
181 drmModeRmFB(gbm_device_get_fd(gbm), fb->fb_id);
184 weston_buffer_post_release(fb->buffer);
185 wl_list_remove(&fb->buffer_destroy_listener.link);
191 static struct drm_fb *
192 drm_fb_get_from_bo(struct gbm_bo *bo, struct drm_output *output)
194 struct drm_fb *fb = gbm_bo_get_user_data(bo);
195 struct drm_compositor *compositor =
196 (struct drm_compositor *) output->base.compositor;
197 uint32_t width, height, stride, handle;
203 fb = malloc(sizeof *fb);
207 fb->is_client_buffer = 0;
210 width = gbm_bo_get_width(bo);
211 height = gbm_bo_get_height(bo);
212 stride = gbm_bo_get_pitch(bo);
213 handle = gbm_bo_get_handle(bo).u32;
215 ret = drmModeAddFB(compositor->drm.fd, width, height, 24, 32,
216 stride, handle, &fb->fb_id);
218 weston_log("failed to create kms fb: %m\n");
223 gbm_bo_set_user_data(bo, fb, drm_fb_destroy_callback);
229 fb_handle_buffer_destroy(struct wl_listener *listener, void *data)
231 struct drm_fb *fb = container_of(listener, struct drm_fb,
232 buffer_destroy_listener);
236 if (fb == fb->output->next ||
237 (fb == fb->output->current && !fb->output->next))
238 weston_output_schedule_repaint(&fb->output->base);
242 drm_output_prepare_scanout_surface(struct drm_output *output)
244 struct drm_compositor *c =
245 (struct drm_compositor *) output->base.compositor;
246 struct weston_surface *es;
249 es = container_of(c->base.surface_list.next,
250 struct weston_surface, link);
252 if (es->geometry.x != output->base.x ||
253 es->geometry.y != output->base.y ||
254 es->geometry.width != output->base.current->width ||
255 es->geometry.height != output->base.current->height ||
256 es->transform.enabled ||
257 es->image == EGL_NO_IMAGE_KHR)
260 bo = gbm_bo_create_from_egl_image(c->gbm,
267 /* Need to verify output->region contained in surface opaque
268 * region. Or maybe just that format doesn't have alpha.
269 * For now, scanout only if format is XRGB8888. */
270 if (gbm_bo_get_format(bo) != GBM_FORMAT_XRGB8888) {
275 output->next = drm_fb_get_from_bo(bo, output);
281 output->next->is_client_buffer = 1;
282 output->next->buffer = es->buffer;
283 output->next->buffer->busy_count++;
284 output->next->buffer_destroy_listener.notify = fb_handle_buffer_destroy;
286 wl_signal_add(&output->next->buffer->resource.destroy_signal,
287 &output->next->buffer_destroy_listener);
289 pixman_region32_fini(&es->damage);
290 pixman_region32_init(&es->damage);
296 drm_output_render(struct drm_output *output, pixman_region32_t *damage)
298 struct drm_compositor *compositor =
299 (struct drm_compositor *) output->base.compositor;
300 struct weston_surface *surface;
303 if (!eglMakeCurrent(compositor->base.egl_display, output->egl_surface,
305 compositor->base.egl_context)) {
306 weston_log("failed to make current\n");
310 wl_list_for_each_reverse(surface, &compositor->base.surface_list, link)
311 weston_surface_draw(surface, &output->base, damage);
313 wl_signal_emit(&output->base.frame_signal, output);
315 eglSwapBuffers(compositor->base.egl_display, output->egl_surface);
316 bo = gbm_surface_lock_front_buffer(output->surface);
318 weston_log("failed to lock front buffer: %m\n");
322 output->next = drm_fb_get_from_bo(bo, output);
324 weston_log("failed to get drm_fb for bo\n");
325 gbm_surface_release_buffer(output->surface, bo);
331 drm_output_repaint(struct weston_output *output_base,
332 pixman_region32_t *damage)
334 struct drm_output *output = (struct drm_output *) output_base;
335 struct drm_compositor *compositor =
336 (struct drm_compositor *) output->base.compositor;
337 struct drm_sprite *s;
338 struct drm_mode *mode;
341 drm_output_prepare_scanout_surface(output);
343 drm_output_render(output, damage);
347 mode = container_of(output->base.current, struct drm_mode, base);
348 if (!output->current) {
349 ret = drmModeSetCrtc(compositor->drm.fd, output->crtc_id,
350 output->next->fb_id, 0, 0,
351 &output->connector_id, 1,
354 weston_log("set mode failed: %m\n");
359 if (drmModePageFlip(compositor->drm.fd, output->crtc_id,
361 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
362 weston_log("queueing pageflip failed: %m\n");
366 output->page_flip_pending = 1;
369 * Now, update all the sprite surfaces
371 wl_list_for_each(s, &compositor->sprite_list, link) {
374 .request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT,
375 .request.sequence = 1,
378 if (!drm_sprite_crtc_supported(output_base, s->possible_crtcs))
381 ret = drmModeSetPlane(compositor->drm.fd, s->plane_id,
382 output->crtc_id, s->pending_fb_id, flags,
383 s->dest_x, s->dest_y,
384 s->dest_w, s->dest_h,
388 weston_log("setplane failed: %d: %s\n",
389 ret, strerror(errno));
392 * Queue a vblank signal so we know when the surface
393 * becomes active on the display or has been replaced.
395 vbl.request.signal = (unsigned long)s;
396 ret = drmWaitVBlank(compositor->drm.fd, &vbl);
398 weston_log("vblank event request failed: %d: %s\n",
399 ret, strerror(errno));
403 output->vblank_pending = 1;
410 vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec,
413 struct drm_sprite *s = (struct drm_sprite *)data;
414 struct drm_compositor *c = s->compositor;
415 struct drm_output *output = s->output;
418 output->vblank_pending = 0;
421 weston_buffer_post_release(s->surface->buffer);
422 wl_list_remove(&s->destroy_listener.link);
424 drmModeRmFB(c->drm.fd, s->fb_id);
428 if (s->pending_surface) {
429 wl_list_remove(&s->pending_destroy_listener.link);
430 wl_signal_add(&s->pending_surface->buffer->resource.destroy_signal,
431 &s->destroy_listener);
432 s->surface = s->pending_surface;
433 s->pending_surface = NULL;
434 s->fb_id = s->pending_fb_id;
435 s->pending_fb_id = 0;
438 if (!output->page_flip_pending) {
439 msecs = sec * 1000 + usec / 1000;
440 weston_output_finish_frame(&output->base, msecs);
445 page_flip_handler(int fd, unsigned int frame,
446 unsigned int sec, unsigned int usec, void *data)
448 struct drm_output *output = (struct drm_output *) data;
451 output->page_flip_pending = 0;
453 if (output->current) {
454 if (output->current->is_client_buffer)
455 gbm_bo_destroy(output->current->bo);
457 gbm_surface_release_buffer(output->surface,
458 output->current->bo);
461 output->current = output->next;
464 if (!output->vblank_pending) {
465 msecs = sec * 1000 + usec / 1000;
466 weston_output_finish_frame(&output->base, msecs);
471 drm_surface_format_supported(struct drm_sprite *s, uint32_t format)
475 for (i = 0; i < s->count_formats; i++)
476 if (s->formats[i] == format)
483 drm_surface_transform_supported(struct weston_surface *es)
485 if (es->transform.enabled)
492 drm_surface_overlap_supported(struct weston_output *output_base,
493 pixman_region32_t *overlap)
495 /* We could potentially use a color key here if the surface left
496 * to display has rectangular regions
498 if (pixman_region32_not_empty(overlap))
504 drm_disable_unused_sprites(struct weston_output *output_base)
506 struct weston_compositor *ec = output_base->compositor;
507 struct drm_compositor *c =(struct drm_compositor *) ec;
508 struct drm_output *output = (struct drm_output *) output_base;
509 struct drm_sprite *s;
512 wl_list_for_each(s, &c->sprite_list, link) {
513 if (s->pending_fb_id)
516 ret = drmModeSetPlane(c->drm.fd, s->plane_id,
517 output->crtc_id, 0, 0,
518 0, 0, 0, 0, 0, 0, 0, 0);
520 weston_log("failed to disable plane: %d: %s\n",
521 ret, strerror(errno));
522 drmModeRmFB(c->drm.fd, s->fb_id);
524 s->pending_surface = NULL;
526 s->pending_fb_id = 0;
531 * This function must take care to damage any previously assigned surface
532 * if the sprite ends up binding to a different surface than in the
536 drm_output_prepare_overlay_surface(struct weston_output *output_base,
537 struct weston_surface *es,
538 pixman_region32_t *overlap)
540 struct weston_compositor *ec = output_base->compositor;
541 struct drm_compositor *c =(struct drm_compositor *) ec;
542 struct drm_sprite *s;
544 EGLint handle, stride;
547 uint32_t handles[4], pitches[4], offsets[4];
549 pixman_region32_t dest_rect, src_rect;
553 if (c->sprites_are_broken)
556 if (es->output_mask != (1u << output_base->id))
559 if (surface_is_primary(ec, es))
562 if (es->image == EGL_NO_IMAGE_KHR)
565 if (!drm_surface_transform_supported(es))
568 if (!drm_surface_overlap_supported(output_base, overlap))
571 wl_list_for_each(s, &c->sprite_list, link) {
572 if (!drm_sprite_crtc_supported(output_base, s->possible_crtcs))
575 if (!s->pending_fb_id) {
581 /* No sprites available */
585 bo = gbm_bo_create_from_egl_image(c->gbm, c->base.egl_display,
586 es->image, es->geometry.width,
589 format = gbm_bo_get_format(bo);
590 handle = gbm_bo_get_handle(bo).s32;
591 stride = gbm_bo_get_pitch(bo);
595 if (!drm_surface_format_supported(s, format))
605 ret = drmModeAddFB2(c->drm.fd, es->geometry.width, es->geometry.height,
606 format, handles, pitches, offsets,
609 weston_log("addfb2 failed: %d\n", ret);
610 c->sprites_are_broken = 1;
614 if (s->surface && s->surface != es) {
615 struct weston_surface *old_surf = s->surface;
616 pixman_region32_fini(&old_surf->damage);
617 pixman_region32_init_rect(&old_surf->damage,
618 old_surf->geometry.x, old_surf->geometry.y,
619 old_surf->geometry.width, old_surf->geometry.height);
622 s->pending_fb_id = fb_id;
623 s->pending_surface = es;
624 es->buffer->busy_count++;
627 * Calculate the source & dest rects properly based on actual
628 * postion (note the caller has called weston_surface_update_transform()
631 pixman_region32_init(&dest_rect);
632 pixman_region32_intersect(&dest_rect, &es->transform.boundingbox,
633 &output_base->region);
634 pixman_region32_translate(&dest_rect, -output_base->x, -output_base->y);
635 box = pixman_region32_extents(&dest_rect);
638 s->dest_w = box->x2 - box->x1;
639 s->dest_h = box->y2 - box->y1;
640 pixman_region32_fini(&dest_rect);
642 pixman_region32_init(&src_rect);
643 pixman_region32_intersect(&src_rect, &es->transform.boundingbox,
644 &output_base->region);
645 pixman_region32_translate(&src_rect, -es->geometry.x, -es->geometry.y);
646 box = pixman_region32_extents(&src_rect);
647 s->src_x = box->x1 << 16;
648 s->src_y = box->y1 << 16;
649 s->src_w = (box->x2 - box->x1) << 16;
650 s->src_h = (box->y2 - box->y1) << 16;
651 pixman_region32_fini(&src_rect);
653 wl_signal_add(&es->buffer->resource.destroy_signal,
654 &s->pending_destroy_listener);
659 drm_output_set_cursor(struct weston_output *output_base,
660 struct weston_seat *es);
663 weston_output_set_cursor(struct weston_output *output,
664 struct weston_seat *seat,
665 pixman_region32_t *overlap)
667 pixman_region32_t cursor_region;
669 if (seat->sprite == NULL)
672 pixman_region32_init(&cursor_region);
673 pixman_region32_intersect(&cursor_region,
674 &seat->sprite->transform.boundingbox,
677 if (!pixman_region32_not_empty(&cursor_region)) {
678 drm_output_set_cursor(output, NULL);
682 if (pixman_region32_not_empty(overlap) ||
683 drm_output_set_cursor(output, seat) < 0) {
684 if (seat->sprite->plane == WESTON_PLANE_DRM_CURSOR) {
685 weston_surface_damage(seat->sprite);
686 drm_output_set_cursor(output, NULL);
688 seat->sprite->plane = WESTON_PLANE_PRIMARY;
690 if (seat->sprite->plane == WESTON_PLANE_PRIMARY)
691 weston_surface_damage_below(seat->sprite);
692 wl_list_remove(&seat->sprite->link);
693 seat->sprite->plane = WESTON_PLANE_DRM_CURSOR;
697 pixman_region32_fini(&cursor_region);
701 drm_assign_planes(struct weston_output *output)
703 struct weston_compositor *ec = output->compositor;
704 struct weston_surface *es, *next;
705 pixman_region32_t overlap, surface_overlap;
706 struct weston_seat *seat;
709 * Find a surface for each sprite in the output using some heuristics:
711 * 2) frequency of update
712 * 3) opacity (though some hw might support alpha blending)
713 * 4) clipping (this can be fixed with color keys)
715 * The idea is to save on blitting since this should save power.
716 * If we can get a large video surface on the sprite for example,
717 * the main display surface may not need to update at all, and
718 * the client buffer can be used directly for the sprite surface
719 * as we do for flipping full screen surfaces.
721 seat = (struct weston_seat *) ec->seat;
722 pixman_region32_init(&overlap);
723 wl_list_for_each_safe(es, next, &ec->surface_list, link) {
725 * FIXME: try to assign hw cursors here too, they're just
728 pixman_region32_init(&surface_overlap);
729 pixman_region32_intersect(&surface_overlap, &overlap,
730 &es->transform.boundingbox);
732 if (es == seat->sprite) {
733 weston_output_set_cursor(output, seat,
736 if (seat->sprite->plane == WESTON_PLANE_PRIMARY)
737 pixman_region32_union(&overlap, &overlap,
738 &es->transform.boundingbox);
739 } else if (!drm_output_prepare_overlay_surface(output, es,
741 pixman_region32_fini(&es->damage);
742 pixman_region32_init(&es->damage);
744 pixman_region32_union(&overlap, &overlap,
745 &es->transform.boundingbox);
747 pixman_region32_fini(&surface_overlap);
749 pixman_region32_fini(&overlap);
751 if (!seat->sprite || !weston_surface_is_mapped(seat->sprite))
752 drm_output_set_cursor(output, NULL);
754 drm_disable_unused_sprites(output);
758 drm_output_set_cursor(struct weston_output *output_base,
759 struct weston_seat *es)
761 struct drm_output *output = (struct drm_output *) output_base;
762 struct drm_compositor *c =
763 (struct drm_compositor *) output->base.compositor;
764 EGLint handle, stride;
767 uint32_t buf[64 * 64];
768 unsigned char *d, *s, *end;
771 drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0);
775 if (es->sprite->buffer == NULL ||
776 !wl_buffer_is_shm(es->sprite->buffer))
779 if (es->sprite->geometry.width > 64 ||
780 es->sprite->geometry.height > 64)
783 output->current_cursor ^= 1;
784 bo = output->cursor_bo[output->current_cursor];
788 memset(buf, 0, sizeof buf);
789 d = (unsigned char *) buf;
790 stride = wl_shm_buffer_get_stride(es->sprite->buffer);
791 s = wl_shm_buffer_get_data(es->sprite->buffer);
792 end = s + stride * es->sprite->geometry.height;
794 memcpy(d, s, es->sprite->geometry.width * 4);
799 if (gbm_bo_write(bo, buf, sizeof buf) < 0)
802 handle = gbm_bo_get_handle(bo).s32;
803 ret = drmModeSetCursor(c->drm.fd, output->crtc_id, handle, 64, 64);
805 weston_log("failed to set cursor: %s\n", strerror(-ret));
809 ret = drmModeMoveCursor(c->drm.fd, output->crtc_id,
810 es->sprite->geometry.x - output->base.x,
811 es->sprite->geometry.y - output->base.y);
813 weston_log("failed to move cursor: %s\n", strerror(-ret));
819 drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0);
824 drm_output_destroy(struct weston_output *output_base)
826 struct drm_output *output = (struct drm_output *) output_base;
827 struct drm_compositor *c =
828 (struct drm_compositor *) output->base.compositor;
829 drmModeCrtcPtr origcrtc = output->original_crtc;
831 if (output->backlight)
832 backlight_destroy(output->backlight);
834 /* Turn off hardware cursor */
835 drm_output_set_cursor(&output->base, NULL);
837 /* Restore original CRTC state */
838 drmModeSetCrtc(c->drm.fd, origcrtc->crtc_id, origcrtc->buffer_id,
839 origcrtc->x, origcrtc->y,
840 &output->connector_id, 1, &origcrtc->mode);
841 drmModeFreeCrtc(origcrtc);
843 c->crtc_allocator &= ~(1 << output->crtc_id);
844 c->connector_allocator &= ~(1 << output->connector_id);
846 eglDestroySurface(c->base.egl_display, output->egl_surface);
847 gbm_surface_destroy(output->surface);
849 weston_output_destroy(&output->base);
850 wl_list_remove(&output->base.link);
855 static struct drm_mode *
856 choose_mode (struct drm_output *output, struct weston_mode *target_mode)
858 struct drm_mode *tmp_mode = NULL, *mode;
860 if (output->base.current->width == target_mode->width &&
861 output->base.current->height == target_mode->height &&
862 (output->base.current->refresh == target_mode->refresh ||
863 target_mode->refresh == 0))
864 return (struct drm_mode *)output->base.current;
866 wl_list_for_each(mode, &output->base.mode_list, base.link) {
867 if (mode->mode_info.hdisplay == target_mode->width &&
868 mode->mode_info.vdisplay == target_mode->height) {
869 if (mode->mode_info.vrefresh == target_mode->refresh ||
870 target_mode->refresh == 0) {
872 } else if (!tmp_mode)
881 drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mode)
883 struct drm_output *output;
884 struct drm_mode *drm_mode;
886 struct drm_compositor *ec;
887 struct gbm_surface *surface;
888 EGLSurface egl_surface;
890 if (output_base == NULL) {
891 weston_log("output is NULL.\n");
896 weston_log("mode is NULL.\n");
900 ec = (struct drm_compositor *)output_base->compositor;
901 output = (struct drm_output *)output_base;
902 drm_mode = choose_mode (output, mode);
905 weston_log("%s, invalid resolution:%dx%d\n", __func__, mode->width, mode->height);
907 } else if (&drm_mode->base == output->base.current) {
909 } else if (drm_mode->base.width == output->base.current->width &&
910 drm_mode->base.height == output->base.current->height) {
911 /* only change refresh value */
912 ret = drmModeSetCrtc(ec->drm.fd,
914 output->current->fb_id, 0, 0,
915 &output->connector_id, 1, &drm_mode->mode_info);
918 weston_log("failed to set mode (%dx%d) %u Hz\n",
919 drm_mode->base.width,
920 drm_mode->base.height,
921 drm_mode->base.refresh / 1000);
924 output->base.current->flags = 0;
925 output->base.current = &drm_mode->base;
926 drm_mode->base.flags =
927 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
934 drm_mode->base.flags =
935 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
937 surface = gbm_surface_create(ec->gbm,
938 drm_mode->base.width,
939 drm_mode->base.height,
942 GBM_BO_USE_RENDERING);
944 weston_log("failed to create gbm surface\n");
949 eglCreateWindowSurface(ec->base.egl_display,
953 if (egl_surface == EGL_NO_SURFACE) {
954 weston_log("failed to create egl surface\n");
958 ret = drmModeSetCrtc(ec->drm.fd,
960 output->current->fb_id, 0, 0,
961 &output->connector_id, 1, &drm_mode->mode_info);
963 weston_log("failed to set mode\n");
967 /* reset rendering stuff. */
968 if (output->current) {
969 if (output->current->is_client_buffer)
970 gbm_bo_destroy(output->current->bo);
972 gbm_surface_release_buffer(output->surface,
973 output->current->bo);
975 output->current = NULL;
978 if (output->next->is_client_buffer)
979 gbm_bo_destroy(output->next->bo);
981 gbm_surface_release_buffer(output->surface,
986 eglDestroySurface(ec->base.egl_display, output->egl_surface);
987 gbm_surface_destroy(output->surface);
988 output->egl_surface = egl_surface;
989 output->surface = surface;
992 output->base.current = &drm_mode->base;
993 output->base.dirty = 1;
994 weston_output_move(&output->base, output->base.x, output->base.y);
998 eglDestroySurface(ec->base.egl_display, egl_surface);
999 gbm_surface_destroy(surface);
1004 on_drm_input(int fd, uint32_t mask, void *data)
1006 drmEventContext evctx;
1008 memset(&evctx, 0, sizeof evctx);
1009 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1010 evctx.page_flip_handler = page_flip_handler;
1011 evctx.vblank_handler = vblank_handler;
1012 drmHandleEvent(fd, &evctx);
1018 init_egl(struct drm_compositor *ec, struct udev_device *device)
1020 EGLint major, minor, n;
1021 const char *filename, *sysnum;
1023 static const EGLint context_attribs[] = {
1024 EGL_CONTEXT_CLIENT_VERSION, 2,
1028 static const EGLint config_attribs[] = {
1029 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
1034 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
1038 sysnum = udev_device_get_sysnum(device);
1040 ec->drm.id = atoi(sysnum);
1041 if (!sysnum || ec->drm.id < 0) {
1042 weston_log("cannot get device sysnum\n");
1046 filename = udev_device_get_devnode(device);
1047 fd = open(filename, O_RDWR | O_CLOEXEC);
1049 /* Probably permissions error */
1050 weston_log("couldn't open %s, skipping\n",
1051 udev_device_get_devnode(device));
1055 weston_log("using %s\n", filename);
1058 ec->gbm = gbm_create_device(ec->drm.fd);
1059 ec->base.egl_display = eglGetDisplay(ec->gbm);
1060 if (ec->base.egl_display == NULL) {
1061 weston_log("failed to create display\n");
1065 if (!eglInitialize(ec->base.egl_display, &major, &minor)) {
1066 weston_log("failed to initialize display\n");
1070 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
1071 weston_log("failed to bind api EGL_OPENGL_ES_API\n");
1075 if (!eglChooseConfig(ec->base.egl_display, config_attribs,
1076 &ec->base.egl_config, 1, &n) || n != 1) {
1077 weston_log("failed to choose config: %d\n", n);
1081 ec->base.egl_context =
1082 eglCreateContext(ec->base.egl_display, ec->base.egl_config,
1083 EGL_NO_CONTEXT, context_attribs);
1084 if (ec->base.egl_context == NULL) {
1085 weston_log("failed to create context\n");
1089 ec->dummy_surface = gbm_surface_create(ec->gbm, 10, 10,
1090 GBM_FORMAT_XRGB8888,
1091 GBM_BO_USE_RENDERING);
1092 if (!ec->dummy_surface) {
1093 weston_log("failed to create dummy gbm surface\n");
1097 ec->dummy_egl_surface =
1098 eglCreateWindowSurface(ec->base.egl_display,
1099 ec->base.egl_config,
1102 if (ec->dummy_egl_surface == EGL_NO_SURFACE) {
1103 weston_log("failed to create egl surface\n");
1107 if (!eglMakeCurrent(ec->base.egl_display, ec->dummy_egl_surface,
1108 ec->dummy_egl_surface, ec->base.egl_context)) {
1109 weston_log("failed to make context current\n");
1116 static drmModeModeInfo builtin_1024x768 = {
1118 1024, 1072, 1176, 1328, 0,
1119 768, 771, 775, 798, 0,
1121 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC,
1128 drm_output_add_mode(struct drm_output *output, drmModeModeInfo *info)
1130 struct drm_mode *mode;
1133 mode = malloc(sizeof *mode);
1137 mode->base.flags = 0;
1138 mode->base.width = info->hdisplay;
1139 mode->base.height = info->vdisplay;
1141 /* Calculate higher precision (mHz) refresh rate */
1142 refresh = (info->clock * 1000000LL / info->htotal +
1143 info->vtotal / 2) / info->vtotal;
1145 if (info->flags & DRM_MODE_FLAG_INTERLACE)
1147 if (info->flags & DRM_MODE_FLAG_DBLSCAN)
1149 if (info->vscan > 1)
1150 refresh /= info->vscan;
1152 mode->base.refresh = refresh;
1153 mode->mode_info = *info;
1154 wl_list_insert(output->base.mode_list.prev, &mode->base.link);
1160 drm_subpixel_to_wayland(int drm_value)
1162 switch (drm_value) {
1164 case DRM_MODE_SUBPIXEL_UNKNOWN:
1165 return WL_OUTPUT_SUBPIXEL_UNKNOWN;
1166 case DRM_MODE_SUBPIXEL_NONE:
1167 return WL_OUTPUT_SUBPIXEL_NONE;
1168 case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB:
1169 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB;
1170 case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR:
1171 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR;
1172 case DRM_MODE_SUBPIXEL_VERTICAL_RGB:
1173 return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB;
1174 case DRM_MODE_SUBPIXEL_VERTICAL_BGR:
1175 return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR;
1180 sprite_handle_buffer_destroy(struct wl_listener *listener, void *data)
1182 struct drm_sprite *sprite =
1183 container_of(listener, struct drm_sprite,
1186 sprite->surface = NULL;
1190 sprite_handle_pending_buffer_destroy(struct wl_listener *listener, void *data)
1192 struct drm_sprite *sprite =
1193 container_of(listener, struct drm_sprite,
1194 pending_destroy_listener);
1196 sprite->pending_surface = NULL;
1199 /* returns a value between 0-255 range, where higher is brighter */
1201 drm_get_backlight(struct drm_output *output)
1203 long brightness, max_brightness, norm;
1205 brightness = backlight_get_brightness(output->backlight);
1206 max_brightness = backlight_get_max_brightness(output->backlight);
1208 /* convert it on a scale of 0 to 255 */
1209 norm = (brightness * 255)/(max_brightness);
1211 return (uint32_t) norm;
1214 /* values accepted are between 0-255 range */
1216 drm_set_backlight(struct weston_output *output_base, uint32_t value)
1218 struct drm_output *output = (struct drm_output *) output_base;
1219 long max_brightness, new_brightness;
1221 if (!output->backlight)
1227 max_brightness = backlight_get_max_brightness(output->backlight);
1229 /* get denormalized value */
1230 new_brightness = (value * max_brightness) / 255;
1232 backlight_set_brightness(output->backlight, new_brightness);
1235 static drmModePropertyPtr
1236 drm_get_prop(int fd, drmModeConnectorPtr connector, const char *name)
1238 drmModePropertyPtr props;
1241 for (i = 0; i < connector->count_props; i++) {
1242 props = drmModeGetProperty(fd, connector->props[i]);
1246 if (!strcmp(props->name, name))
1249 drmModeFreeProperty(props);
1256 drm_set_dpms(struct weston_output *output_base, enum dpms_enum level)
1258 struct drm_output *output = (struct drm_output *) output_base;
1259 struct weston_compositor *ec = output_base->compositor;
1260 struct drm_compositor *c = (struct drm_compositor *) ec;
1261 drmModeConnectorPtr connector;
1262 drmModePropertyPtr prop;
1264 connector = drmModeGetConnector(c->drm.fd, output->connector_id);
1268 prop = drm_get_prop(c->drm.fd, connector, "DPMS");
1270 drmModeFreeConnector(connector);
1274 drmModeConnectorSetProperty(c->drm.fd, connector->connector_id,
1275 prop->prop_id, level);
1276 drmModeFreeProperty(prop);
1277 drmModeFreeConnector(connector);
1281 create_output_for_connector(struct drm_compositor *ec,
1282 drmModeRes *resources,
1283 drmModeConnector *connector,
1284 int x, int y, struct udev_device *drm_device)
1286 struct drm_output *output;
1287 struct drm_mode *drm_mode, *next;
1288 drmModeEncoder *encoder;
1291 encoder = drmModeGetEncoder(ec->drm.fd, connector->encoders[0]);
1292 if (encoder == NULL) {
1293 weston_log("No encoder for connector.\n");
1297 for (i = 0; i < resources->count_crtcs; i++) {
1298 if (encoder->possible_crtcs & (1 << i) &&
1299 !(ec->crtc_allocator & (1 << resources->crtcs[i])))
1302 if (i == resources->count_crtcs) {
1303 weston_log("No usable crtc for encoder.\n");
1304 drmModeFreeEncoder(encoder);
1308 output = malloc(sizeof *output);
1309 if (output == NULL) {
1310 drmModeFreeEncoder(encoder);
1314 memset(output, 0, sizeof *output);
1315 output->base.subpixel = drm_subpixel_to_wayland(connector->subpixel);
1316 output->base.make = "unknown";
1317 output->base.model = "unknown";
1318 wl_list_init(&output->base.mode_list);
1320 output->crtc_id = resources->crtcs[i];
1321 ec->crtc_allocator |= (1 << output->crtc_id);
1322 output->connector_id = connector->connector_id;
1323 ec->connector_allocator |= (1 << output->connector_id);
1325 output->original_crtc = drmModeGetCrtc(ec->drm.fd, output->crtc_id);
1326 drmModeFreeEncoder(encoder);
1328 for (i = 0; i < connector->count_modes; i++) {
1329 ret = drm_output_add_mode(output, &connector->modes[i]);
1334 if (connector->count_modes == 0) {
1335 ret = drm_output_add_mode(output, &builtin_1024x768);
1340 drm_mode = container_of(output->base.mode_list.next,
1341 struct drm_mode, base.link);
1342 output->base.current = &drm_mode->base;
1343 drm_mode->base.flags =
1344 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
1346 output->surface = gbm_surface_create(ec->gbm,
1347 output->base.current->width,
1348 output->base.current->height,
1349 GBM_FORMAT_XRGB8888,
1350 GBM_BO_USE_SCANOUT |
1351 GBM_BO_USE_RENDERING);
1352 if (!output->surface) {
1353 weston_log("failed to create gbm surface\n");
1357 output->egl_surface =
1358 eglCreateWindowSurface(ec->base.egl_display,
1359 ec->base.egl_config,
1362 if (output->egl_surface == EGL_NO_SURFACE) {
1363 weston_log("failed to create egl surface\n");
1367 output->cursor_bo[0] =
1368 gbm_bo_create(ec->gbm, 64, 64, GBM_FORMAT_ARGB8888,
1369 GBM_BO_USE_CURSOR_64X64 | GBM_BO_USE_WRITE);
1370 output->cursor_bo[1] =
1371 gbm_bo_create(ec->gbm, 64, 64, GBM_FORMAT_ARGB8888,
1372 GBM_BO_USE_CURSOR_64X64 | GBM_BO_USE_WRITE);
1374 output->backlight = backlight_init(drm_device,
1375 connector->connector_type);
1376 if (output->backlight) {
1377 output->base.set_backlight = drm_set_backlight;
1378 output->base.backlight_current = drm_get_backlight(output);
1381 weston_output_init(&output->base, &ec->base, x, y,
1382 connector->mmWidth, connector->mmHeight,
1385 wl_list_insert(ec->base.output_list.prev, &output->base.link);
1387 output->base.origin = output->base.current;
1388 output->base.repaint = drm_output_repaint;
1389 output->base.destroy = drm_output_destroy;
1390 output->base.assign_planes = drm_assign_planes;
1391 output->base.set_dpms = drm_set_dpms;
1392 output->base.switch_mode = drm_output_switch_mode;
1394 weston_log("kms connector %d, crtc %d at mode %dx%d@%.1f\n",
1395 output->connector_id, output->crtc_id,
1396 output->base.current->width,
1397 output->base.current->height,
1398 output->base.current->refresh / 1000.0);
1403 gbm_surface_destroy(output->surface);
1405 wl_list_for_each_safe(drm_mode, next, &output->base.mode_list,
1407 wl_list_remove(&drm_mode->base.link);
1411 drmModeFreeCrtc(output->original_crtc);
1412 ec->crtc_allocator &= ~(1 << output->crtc_id);
1413 ec->connector_allocator &= ~(1 << output->connector_id);
1420 create_sprites(struct drm_compositor *ec)
1422 struct drm_sprite *sprite;
1423 drmModePlaneRes *plane_res;
1424 drmModePlane *plane;
1427 plane_res = drmModeGetPlaneResources(ec->drm.fd);
1429 weston_log("failed to get plane resources: %s\n",
1434 for (i = 0; i < plane_res->count_planes; i++) {
1435 plane = drmModeGetPlane(ec->drm.fd, plane_res->planes[i]);
1439 sprite = malloc(sizeof(*sprite) + ((sizeof(uint32_t)) *
1440 plane->count_formats));
1442 weston_log("%s: out of memory\n",
1448 memset(sprite, 0, sizeof *sprite);
1450 sprite->possible_crtcs = plane->possible_crtcs;
1451 sprite->plane_id = plane->plane_id;
1452 sprite->surface = NULL;
1453 sprite->pending_surface = NULL;
1455 sprite->pending_fb_id = 0;
1456 sprite->destroy_listener.notify = sprite_handle_buffer_destroy;
1457 sprite->pending_destroy_listener.notify =
1458 sprite_handle_pending_buffer_destroy;
1459 sprite->compositor = ec;
1460 sprite->count_formats = plane->count_formats;
1461 memcpy(sprite->formats, plane->formats,
1462 plane->count_formats * sizeof(plane->formats[0]));
1463 drmModeFreePlane(plane);
1465 wl_list_insert(&ec->sprite_list, &sprite->link);
1468 free(plane_res->planes);
1473 destroy_sprites(struct drm_compositor *compositor)
1475 struct drm_sprite *sprite, *next;
1476 struct drm_output *output;
1478 output = container_of(compositor->base.output_list.next,
1479 struct drm_output, base.link);
1481 wl_list_for_each_safe(sprite, next, &compositor->sprite_list, link) {
1482 drmModeSetPlane(compositor->drm.fd,
1484 output->crtc_id, 0, 0,
1485 0, 0, 0, 0, 0, 0, 0, 0);
1486 drmModeRmFB(compositor->drm.fd, sprite->fb_id);
1492 create_outputs(struct drm_compositor *ec, uint32_t option_connector,
1493 struct udev_device *drm_device)
1495 drmModeConnector *connector;
1496 drmModeRes *resources;
1500 resources = drmModeGetResources(ec->drm.fd);
1502 weston_log("drmModeGetResources failed\n");
1506 ec->crtcs = calloc(resources->count_crtcs, sizeof(uint32_t));
1508 drmModeFreeResources(resources);
1512 ec->num_crtcs = resources->count_crtcs;
1513 memcpy(ec->crtcs, resources->crtcs, sizeof(uint32_t) * ec->num_crtcs);
1515 for (i = 0; i < resources->count_connectors; i++) {
1516 connector = drmModeGetConnector(ec->drm.fd,
1517 resources->connectors[i]);
1518 if (connector == NULL)
1521 if (connector->connection == DRM_MODE_CONNECTED &&
1522 (option_connector == 0 ||
1523 connector->connector_id == option_connector)) {
1524 if (create_output_for_connector(ec, resources,
1527 drmModeFreeConnector(connector);
1531 x += container_of(ec->base.output_list.prev,
1532 struct weston_output,
1533 link)->current->width;
1536 drmModeFreeConnector(connector);
1539 if (wl_list_empty(&ec->base.output_list)) {
1540 weston_log("No currently active connector found.\n");
1541 drmModeFreeResources(resources);
1545 drmModeFreeResources(resources);
1551 update_outputs(struct drm_compositor *ec, struct udev_device *drm_device)
1553 drmModeConnector *connector;
1554 drmModeRes *resources;
1555 struct drm_output *output, *next;
1557 int x_offset = 0, y_offset = 0;
1558 uint32_t connected = 0, disconnects = 0;
1561 resources = drmModeGetResources(ec->drm.fd);
1563 weston_log("drmModeGetResources failed\n");
1567 /* collect new connects */
1568 for (i = 0; i < resources->count_connectors; i++) {
1569 int connector_id = resources->connectors[i];
1571 connector = drmModeGetConnector(ec->drm.fd, connector_id);
1572 if (connector == NULL)
1575 if (connector->connection != DRM_MODE_CONNECTED) {
1576 drmModeFreeConnector(connector);
1580 connected |= (1 << connector_id);
1582 if (!(ec->connector_allocator & (1 << connector_id))) {
1583 struct weston_output *last =
1584 container_of(ec->base.output_list.prev,
1585 struct weston_output, link);
1587 /* XXX: not yet needed, we die with 0 outputs */
1588 if (!wl_list_empty(&ec->base.output_list))
1589 x = last->x + last->current->width;
1593 create_output_for_connector(ec, resources,
1596 weston_log("connector %d connected\n", connector_id);
1599 drmModeFreeConnector(connector);
1601 drmModeFreeResources(resources);
1603 disconnects = ec->connector_allocator & ~connected;
1605 wl_list_for_each_safe(output, next, &ec->base.output_list,
1607 if (x_offset != 0 || y_offset != 0) {
1608 weston_output_move(&output->base,
1609 output->base.x - x_offset,
1610 output->base.y - y_offset);
1613 if (disconnects & (1 << output->connector_id)) {
1614 disconnects &= ~(1 << output->connector_id);
1615 weston_log("connector %d disconnected\n",
1616 output->connector_id);
1617 x_offset += output->base.current->width;
1618 drm_output_destroy(&output->base);
1623 /* FIXME: handle zero outputs, without terminating */
1624 if (ec->connector_allocator == 0)
1625 wl_display_terminate(ec->base.wl_display);
1629 udev_event_is_hotplug(struct drm_compositor *ec, struct udev_device *device)
1634 sysnum = udev_device_get_sysnum(device);
1635 if (!sysnum || atoi(sysnum) != ec->drm.id)
1638 val = udev_device_get_property_value(device, "HOTPLUG");
1642 return strcmp(val, "1") == 0;
1646 udev_drm_event(int fd, uint32_t mask, void *data)
1648 struct drm_compositor *ec = data;
1649 struct udev_device *event;
1651 event = udev_monitor_receive_device(ec->udev_monitor);
1653 if (udev_event_is_hotplug(ec, event))
1654 update_outputs(ec, event);
1656 udev_device_unref(event);
1662 drm_destroy(struct weston_compositor *ec)
1664 struct drm_compositor *d = (struct drm_compositor *) ec;
1665 struct weston_seat *seat, *next;
1667 wl_list_for_each_safe(seat, next, &ec->seat_list, link)
1668 evdev_input_destroy(seat);
1670 wl_event_source_remove(d->udev_drm_source);
1671 wl_event_source_remove(d->drm_source);
1673 weston_compositor_shutdown(ec);
1675 /* Work around crash in egl_dri2.c's dri2_make_current() */
1676 eglMakeCurrent(ec->egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
1678 eglTerminate(ec->egl_display);
1681 gbm_device_destroy(d->gbm);
1683 if (weston_launcher_drm_set_master(&d->base, d->drm.fd, 0) < 0)
1684 weston_log("failed to drop master: %m\n");
1685 tty_destroy(d->tty);
1691 drm_compositor_set_modes(struct drm_compositor *compositor)
1693 struct drm_output *output;
1694 struct drm_mode *drm_mode;
1697 wl_list_for_each(output, &compositor->base.output_list, base.link) {
1698 drm_mode = (struct drm_mode *) output->base.current;
1699 ret = drmModeSetCrtc(compositor->drm.fd, output->crtc_id,
1700 output->current->fb_id, 0, 0,
1701 &output->connector_id, 1,
1702 &drm_mode->mode_info);
1705 "failed to set mode %dx%d for output at %d,%d: %m\n",
1706 drm_mode->base.width, drm_mode->base.height,
1707 output->base.x, output->base.y);
1713 vt_func(struct weston_compositor *compositor, int event)
1715 struct drm_compositor *ec = (struct drm_compositor *) compositor;
1716 struct weston_output *output;
1717 struct weston_seat *seat;
1718 struct drm_sprite *sprite;
1719 struct drm_output *drm_output;
1723 compositor->focus = 1;
1724 if (weston_launcher_drm_set_master(&ec->base, ec->drm.fd, 1)) {
1725 weston_log("failed to set master: %m\n");
1726 wl_display_terminate(compositor->wl_display);
1728 compositor->state = ec->prev_state;
1729 drm_compositor_set_modes(ec);
1730 weston_compositor_damage_all(compositor);
1731 wl_list_for_each(seat, &compositor->seat_list, link) {
1732 evdev_add_devices(ec->udev, seat);
1733 evdev_enable_udev_monitor(ec->udev, seat);
1737 wl_list_for_each(seat, &compositor->seat_list, link) {
1738 evdev_disable_udev_monitor(seat);
1739 evdev_remove_devices(seat);
1742 compositor->focus = 0;
1743 ec->prev_state = compositor->state;
1744 compositor->state = WESTON_COMPOSITOR_SLEEPING;
1746 /* If we have a repaint scheduled (either from a
1747 * pending pageflip or the idle handler), make sure we
1748 * cancel that so we don't try to pageflip when we're
1749 * vt switched away. The SLEEPING state will prevent
1750 * further attemps at repainting. When we switch
1751 * back, we schedule a repaint, which will process
1752 * pending frame callbacks. */
1754 wl_list_for_each(output, &ec->base.output_list, link) {
1755 output->repaint_needed = 0;
1756 drm_output_set_cursor(output, NULL);
1759 drm_output = container_of(ec->base.output_list.next,
1760 struct drm_output, base.link);
1762 wl_list_for_each(sprite, &ec->sprite_list, link)
1763 drmModeSetPlane(ec->drm.fd,
1765 drm_output->crtc_id, 0, 0,
1766 0, 0, 0, 0, 0, 0, 0, 0);
1768 if (weston_launcher_drm_set_master(&ec->base, ec->drm.fd, 0) < 0)
1769 weston_log("failed to drop master: %m\n");
1776 switch_vt_binding(struct wl_seat *seat, uint32_t time, uint32_t key, void *data)
1778 struct drm_compositor *ec = data;
1780 tty_activate_vt(ec->tty, key - KEY_F1 + 1);
1783 static const char default_seat[] = "seat0";
1785 static struct weston_compositor *
1786 drm_compositor_create(struct wl_display *display,
1787 int connector, const char *seat, int tty,
1788 int argc, char *argv[], const char *config_file)
1790 struct drm_compositor *ec;
1791 struct udev_enumerate *e;
1792 struct udev_list_entry *entry;
1793 struct udev_device *device, *drm_device;
1794 const char *path, *device_seat;
1795 struct wl_event_loop *loop;
1796 struct weston_seat *weston_seat, *next;
1799 weston_log("initializing drm backend\n");
1801 ec = malloc(sizeof *ec);
1804 memset(ec, 0, sizeof *ec);
1806 if (weston_compositor_init(&ec->base, display, argc, argv,
1808 weston_log("weston_compositor_init failed\n");
1812 ec->udev = udev_new();
1813 if (ec->udev == NULL) {
1814 weston_log("failed to initialize udev context\n");
1815 goto err_compositor;
1818 ec->base.wl_display = display;
1819 ec->tty = tty_create(&ec->base, vt_func, tty);
1821 weston_log("failed to initialize tty\n");
1825 e = udev_enumerate_new(ec->udev);
1826 udev_enumerate_add_match_subsystem(e, "drm");
1827 udev_enumerate_add_match_sysname(e, "card[0-9]*");
1829 udev_enumerate_scan_devices(e);
1831 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
1832 path = udev_list_entry_get_name(entry);
1833 device = udev_device_new_from_syspath(ec->udev, path);
1835 udev_device_get_property_value(device, "ID_SEAT");
1837 device_seat = default_seat;
1838 if (strcmp(device_seat, seat) == 0) {
1839 drm_device = device;
1842 udev_device_unref(device);
1845 if (drm_device == NULL) {
1846 weston_log("no drm device found\n");
1850 if (init_egl(ec, drm_device) < 0) {
1851 weston_log("failed to initialize egl\n");
1855 ec->base.destroy = drm_destroy;
1859 ec->prev_state = WESTON_COMPOSITOR_ACTIVE;
1861 if (weston_compositor_init_gl(&ec->base) < 0)
1864 for (key = KEY_F1; key < KEY_F9; key++)
1865 weston_compositor_add_key_binding(&ec->base, key,
1866 MODIFIER_CTRL | MODIFIER_ALT,
1867 switch_vt_binding, ec);
1869 wl_list_init(&ec->sprite_list);
1872 if (create_outputs(ec, connector, drm_device) < 0) {
1873 weston_log("failed to create output for %s\n", path);
1879 evdev_input_create(&ec->base, ec->udev, seat);
1881 loop = wl_display_get_event_loop(ec->base.wl_display);
1883 wl_event_loop_add_fd(loop, ec->drm.fd,
1884 WL_EVENT_READABLE, on_drm_input, ec);
1886 ec->udev_monitor = udev_monitor_new_from_netlink(ec->udev, "udev");
1887 if (ec->udev_monitor == NULL) {
1888 weston_log("failed to intialize udev monitor\n");
1889 goto err_drm_source;
1891 udev_monitor_filter_add_match_subsystem_devtype(ec->udev_monitor,
1893 ec->udev_drm_source =
1894 wl_event_loop_add_fd(loop,
1895 udev_monitor_get_fd(ec->udev_monitor),
1896 WL_EVENT_READABLE, udev_drm_event, ec);
1898 if (udev_monitor_enable_receiving(ec->udev_monitor) < 0) {
1899 weston_log("failed to enable udev-monitor receiving\n");
1900 goto err_udev_monitor;
1903 udev_device_unref(drm_device);
1904 udev_enumerate_unref(e);
1909 wl_event_source_remove(ec->udev_drm_source);
1910 udev_monitor_unref(ec->udev_monitor);
1912 wl_event_source_remove(ec->drm_source);
1913 wl_list_for_each_safe(weston_seat, next, &ec->base.seat_list, link)
1914 evdev_input_destroy(weston_seat);
1916 destroy_sprites(ec);
1918 eglMakeCurrent(ec->base.egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
1920 eglTerminate(ec->base.egl_display);
1922 gbm_device_destroy(ec->gbm);
1924 udev_device_unref(drm_device);
1926 udev_enumerate_unref(e);
1927 tty_destroy(ec->tty);
1929 udev_unref(ec->udev);
1931 weston_compositor_shutdown(&ec->base);
1937 WL_EXPORT struct weston_compositor *
1938 backend_init(struct wl_display *display, int argc, char *argv[],
1939 const char *config_file)
1941 int connector = 0, tty = 0;
1942 const char *seat = default_seat;
1944 const struct weston_option drm_options[] = {
1945 { WESTON_OPTION_INTEGER, "connector", 0, &connector },
1946 { WESTON_OPTION_STRING, "seat", 0, &seat },
1947 { WESTON_OPTION_INTEGER, "tty", 0, &tty },
1950 parse_options(drm_options, ARRAY_LENGTH(drm_options), argc, argv);
1952 return drm_compositor_create(display, connector, seat, tty, argc, argv,