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>
41 #include "compositor.h"
43 #include "launcher-util.h"
46 static int option_current_mode = 0;
49 WESTON_PLANE_DRM_CURSOR = 0x100,
50 WESTON_PLANE_DRM_PLANE = 0x101,
51 WESTON_PLANE_DRM_FB = 0x102
54 struct drm_compositor {
55 struct weston_compositor base;
58 struct wl_event_source *drm_source;
60 struct udev_monitor *udev_monitor;
61 struct wl_event_source *udev_drm_source;
67 struct gbm_device *gbm;
70 uint32_t crtc_allocator;
71 uint32_t connector_allocator;
74 struct gbm_surface *dummy_surface;
75 EGLSurface dummy_egl_surface;
77 struct wl_list sprite_list;
78 int sprites_are_broken;
84 struct weston_mode base;
85 drmModeModeInfo mode_info;
92 struct drm_output *output;
95 struct wl_buffer *buffer;
96 struct wl_listener buffer_destroy_listener;
100 struct weston_output base;
104 uint32_t connector_id;
105 drmModeCrtcPtr original_crtc;
108 int page_flip_pending;
110 struct gbm_surface *surface;
111 struct gbm_bo *cursor_bo[2];
112 int current_cursor, cursor_free, cursor_x, cursor_y;
113 EGLSurface egl_surface;
114 struct drm_fb *current, *next;
115 struct backlight *backlight;
119 * An output has a primary display plane plus zero or more sprites for
120 * blending display contents.
126 uint32_t pending_fb_id;
127 struct weston_surface *surface;
128 struct weston_surface *pending_surface;
130 struct drm_output *output;
132 struct drm_compositor *compositor;
134 struct wl_listener destroy_listener;
135 struct wl_listener pending_destroy_listener;
137 uint32_t possible_crtcs;
139 uint32_t count_formats;
141 int32_t src_x, src_y;
142 uint32_t src_w, src_h;
143 uint32_t dest_x, dest_y;
144 uint32_t dest_w, dest_h;
150 drm_sprite_crtc_supported(struct weston_output *output_base, uint32_t supported)
152 struct weston_compositor *ec = output_base->compositor;
153 struct drm_compositor *c =(struct drm_compositor *) ec;
154 struct drm_output *output = (struct drm_output *) output_base;
157 for (crtc = 0; crtc < c->num_crtcs; crtc++) {
158 if (c->crtcs[crtc] != output->crtc_id)
161 if (supported & (1 << crtc))
169 drm_fb_destroy_callback(struct gbm_bo *bo, void *data)
171 struct drm_fb *fb = data;
172 struct gbm_device *gbm = gbm_bo_get_device(bo);
175 drmModeRmFB(gbm_device_get_fd(gbm), fb->fb_id);
178 weston_buffer_post_release(fb->buffer);
179 wl_list_remove(&fb->buffer_destroy_listener.link);
185 static struct drm_fb *
186 drm_fb_get_from_bo(struct gbm_bo *bo, struct drm_output *output)
188 struct drm_fb *fb = gbm_bo_get_user_data(bo);
189 struct drm_compositor *compositor =
190 (struct drm_compositor *) output->base.compositor;
191 uint32_t width, height, stride, handle;
197 fb = malloc(sizeof *fb);
201 fb->is_client_buffer = 0;
204 width = gbm_bo_get_width(bo);
205 height = gbm_bo_get_height(bo);
206 stride = gbm_bo_get_stride(bo);
207 handle = gbm_bo_get_handle(bo).u32;
209 ret = drmModeAddFB(compositor->drm.fd, width, height, 24, 32,
210 stride, handle, &fb->fb_id);
212 weston_log("failed to create kms fb: %m\n");
217 gbm_bo_set_user_data(bo, fb, drm_fb_destroy_callback);
223 fb_handle_buffer_destroy(struct wl_listener *listener, void *data)
225 struct drm_fb *fb = container_of(listener, struct drm_fb,
226 buffer_destroy_listener);
230 if (fb == fb->output->next ||
231 (fb == fb->output->current && !fb->output->next))
232 weston_output_schedule_repaint(&fb->output->base);
236 drm_output_prepare_scanout_surface(struct weston_output *_output,
237 struct weston_surface *es)
239 struct drm_output *output = (struct drm_output *) _output;
240 struct drm_compositor *c =
241 (struct drm_compositor *) output->base.compositor;
244 if (es->geometry.x != output->base.x ||
245 es->geometry.y != output->base.y ||
246 es->geometry.width != output->base.current->width ||
247 es->geometry.height != output->base.current->height ||
248 es->transform.enabled ||
252 bo = gbm_bo_import(c->gbm, GBM_BO_IMPORT_WL_BUFFER,
253 es->buffer, GBM_BO_USE_SCANOUT);
255 /* Need to verify output->region contained in surface opaque
256 * region. Or maybe just that format doesn't have alpha.
257 * For now, scanout only if format is XRGB8888. */
258 if (gbm_bo_get_format(bo) != GBM_FORMAT_XRGB8888) {
263 output->next = drm_fb_get_from_bo(bo, output);
269 output->next->is_client_buffer = 1;
270 output->next->buffer = es->buffer;
271 output->next->buffer->busy_count++;
272 output->next->buffer_destroy_listener.notify = fb_handle_buffer_destroy;
274 wl_signal_add(&output->next->buffer->resource.destroy_signal,
275 &output->next->buffer_destroy_listener);
277 es->plane = WESTON_PLANE_DRM_FB;
283 drm_output_render(struct drm_output *output, pixman_region32_t *damage)
285 struct drm_compositor *compositor =
286 (struct drm_compositor *) output->base.compositor;
287 struct weston_surface *surface;
290 if (!eglMakeCurrent(compositor->base.egl_display, output->egl_surface,
292 compositor->base.egl_context)) {
293 weston_log("failed to make current\n");
297 wl_list_for_each_reverse(surface, &compositor->base.surface_list, link)
298 if (surface->plane == WESTON_PLANE_PRIMARY)
299 weston_surface_draw(surface, &output->base, damage);
301 wl_signal_emit(&output->base.frame_signal, output);
303 eglSwapBuffers(compositor->base.egl_display, output->egl_surface);
304 bo = gbm_surface_lock_front_buffer(output->surface);
306 weston_log("failed to lock front buffer: %m\n");
310 output->next = drm_fb_get_from_bo(bo, output);
312 weston_log("failed to get drm_fb for bo\n");
313 gbm_surface_release_buffer(output->surface, bo);
319 drm_output_repaint(struct weston_output *output_base,
320 pixman_region32_t *damage)
322 struct drm_output *output = (struct drm_output *) output_base;
323 struct drm_compositor *compositor =
324 (struct drm_compositor *) output->base.compositor;
325 struct drm_sprite *s;
326 struct drm_mode *mode;
330 drm_output_render(output, damage);
334 mode = container_of(output->base.current, struct drm_mode, base);
335 if (!output->current) {
336 ret = drmModeSetCrtc(compositor->drm.fd, output->crtc_id,
337 output->next->fb_id, 0, 0,
338 &output->connector_id, 1,
341 weston_log("set mode failed: %m\n");
346 if (drmModePageFlip(compositor->drm.fd, output->crtc_id,
348 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
349 weston_log("queueing pageflip failed: %m\n");
353 output->page_flip_pending = 1;
356 * Now, update all the sprite surfaces
358 wl_list_for_each(s, &compositor->sprite_list, link) {
361 .request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT,
362 .request.sequence = 1,
365 if (!drm_sprite_crtc_supported(output_base, s->possible_crtcs))
368 ret = drmModeSetPlane(compositor->drm.fd, s->plane_id,
369 output->crtc_id, s->pending_fb_id, flags,
370 s->dest_x, s->dest_y,
371 s->dest_w, s->dest_h,
375 weston_log("setplane failed: %d: %s\n",
376 ret, strerror(errno));
379 * Queue a vblank signal so we know when the surface
380 * becomes active on the display or has been replaced.
382 vbl.request.signal = (unsigned long)s;
383 ret = drmWaitVBlank(compositor->drm.fd, &vbl);
385 weston_log("vblank event request failed: %d: %s\n",
386 ret, strerror(errno));
390 output->vblank_pending = 1;
397 vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec,
400 struct drm_sprite *s = (struct drm_sprite *)data;
401 struct drm_compositor *c = s->compositor;
402 struct drm_output *output = s->output;
405 output->vblank_pending = 0;
408 weston_buffer_post_release(s->surface->buffer);
409 wl_list_remove(&s->destroy_listener.link);
411 drmModeRmFB(c->drm.fd, s->fb_id);
415 if (s->pending_surface) {
416 wl_list_remove(&s->pending_destroy_listener.link);
417 wl_signal_add(&s->pending_surface->buffer->resource.destroy_signal,
418 &s->destroy_listener);
419 s->surface = s->pending_surface;
420 s->pending_surface = NULL;
421 s->fb_id = s->pending_fb_id;
422 s->pending_fb_id = 0;
425 if (!output->page_flip_pending) {
426 msecs = sec * 1000 + usec / 1000;
427 weston_output_finish_frame(&output->base, msecs);
432 page_flip_handler(int fd, unsigned int frame,
433 unsigned int sec, unsigned int usec, void *data)
435 struct drm_output *output = (struct drm_output *) data;
438 output->page_flip_pending = 0;
440 if (output->current) {
441 if (output->current->is_client_buffer)
442 gbm_bo_destroy(output->current->bo);
444 gbm_surface_release_buffer(output->surface,
445 output->current->bo);
448 output->current = output->next;
451 if (!output->vblank_pending) {
452 msecs = sec * 1000 + usec / 1000;
453 weston_output_finish_frame(&output->base, msecs);
458 drm_surface_format_supported(struct drm_sprite *s, uint32_t format)
462 for (i = 0; i < s->count_formats; i++)
463 if (s->formats[i] == format)
470 drm_surface_transform_supported(struct weston_surface *es)
472 struct weston_matrix *matrix = &es->transform.matrix;
475 if (!es->transform.enabled)
478 for (i = 0; i < 16; i++) {
482 if (matrix->d[i] != 1.0)
491 if (matrix->d[i] != 0.0)
501 drm_disable_unused_sprites(struct weston_output *output_base)
503 struct weston_compositor *ec = output_base->compositor;
504 struct drm_compositor *c =(struct drm_compositor *) ec;
505 struct drm_output *output = (struct drm_output *) output_base;
506 struct drm_sprite *s;
509 wl_list_for_each(s, &c->sprite_list, link) {
510 if (s->pending_fb_id)
513 ret = drmModeSetPlane(c->drm.fd, s->plane_id,
514 output->crtc_id, 0, 0,
515 0, 0, 0, 0, 0, 0, 0, 0);
517 weston_log("failed to disable plane: %d: %s\n",
518 ret, strerror(errno));
519 drmModeRmFB(c->drm.fd, s->fb_id);
523 wl_list_remove(&s->destroy_listener.link);
526 assert(!s->pending_surface);
528 s->pending_fb_id = 0;
533 * This function must take care to damage any previously assigned surface
534 * if the sprite ends up binding to a different surface than in the
538 drm_output_prepare_overlay_surface(struct weston_output *output_base,
539 struct weston_surface *es)
541 struct weston_compositor *ec = output_base->compositor;
542 struct drm_compositor *c =(struct drm_compositor *) ec;
543 struct drm_sprite *s;
545 EGLint handle, stride;
548 uint32_t handles[4], pitches[4], offsets[4];
550 pixman_region32_t dest_rect, src_rect;
553 wl_fixed_t sx1, sy1, sx2, sy2;
555 if (c->sprites_are_broken)
558 if (es->output_mask != (1u << output_base->id))
561 if (es->buffer == NULL)
564 if (!drm_surface_transform_supported(es))
567 wl_list_for_each(s, &c->sprite_list, link) {
568 if (!drm_sprite_crtc_supported(output_base, s->possible_crtcs))
571 if (!s->pending_fb_id) {
577 /* No sprites available */
581 bo = gbm_bo_import(c->gbm, GBM_BO_IMPORT_WL_BUFFER,
582 es->buffer, GBM_BO_USE_SCANOUT);
586 format = gbm_bo_get_format(bo);
587 handle = gbm_bo_get_handle(bo).s32;
588 stride = gbm_bo_get_stride(bo);
592 if (!drm_surface_format_supported(s, format))
602 ret = drmModeAddFB2(c->drm.fd, es->geometry.width, es->geometry.height,
603 format, handles, pitches, offsets,
606 weston_log("addfb2 failed: %d\n", ret);
607 c->sprites_are_broken = 1;
611 s->pending_fb_id = fb_id;
612 s->pending_surface = es;
613 es->buffer->busy_count++;
616 * Calculate the source & dest rects properly based on actual
617 * postion (note the caller has called weston_surface_update_transform()
620 pixman_region32_init(&dest_rect);
621 pixman_region32_intersect(&dest_rect, &es->transform.boundingbox,
622 &output_base->region);
623 pixman_region32_translate(&dest_rect, -output_base->x, -output_base->y);
624 box = pixman_region32_extents(&dest_rect);
627 s->dest_w = box->x2 - box->x1;
628 s->dest_h = box->y2 - box->y1;
629 pixman_region32_fini(&dest_rect);
631 pixman_region32_init(&src_rect);
632 pixman_region32_intersect(&src_rect, &es->transform.boundingbox,
633 &output_base->region);
634 box = pixman_region32_extents(&src_rect);
636 weston_surface_from_global_fixed(es,
637 wl_fixed_from_int(box->x1),
638 wl_fixed_from_int(box->y1),
640 weston_surface_from_global_fixed(es,
641 wl_fixed_from_int(box->x2),
642 wl_fixed_from_int(box->y2),
649 if (sx2 > wl_fixed_from_int(es->geometry.width))
650 sx2 = wl_fixed_from_int(es->geometry.width);
651 if (sy2 > wl_fixed_from_int(es->geometry.height))
652 sy2 = wl_fixed_from_int(es->geometry.height);
656 s->src_w = (sx2 - sx1) << 8;
657 s->src_h = (sy2 - sy1) << 8;
658 pixman_region32_fini(&src_rect);
660 es->plane = WESTON_PLANE_DRM_PLANE;
662 wl_signal_add(&es->buffer->resource.destroy_signal,
663 &s->pending_destroy_listener);
669 drm_output_set_cursor(struct weston_output *output_base,
670 struct weston_surface *es)
672 struct drm_output *output = (struct drm_output *) output_base;
673 struct drm_compositor *c =
674 (struct drm_compositor *) output->base.compositor;
675 EGLint handle, stride;
677 uint32_t buf[64 * 64];
681 if (!output->cursor_free)
683 if (es->output_mask != (1u << output_base->id))
685 if (es->buffer == NULL || !wl_buffer_is_shm(es->buffer) ||
686 es->geometry.width > 64 || es->geometry.height > 64)
689 if (es->buffer && pixman_region32_not_empty(&es->damage)) {
690 output->current_cursor ^= 1;
691 bo = output->cursor_bo[output->current_cursor];
692 memset(buf, 0, sizeof buf);
693 stride = wl_shm_buffer_get_stride(es->buffer);
694 s = wl_shm_buffer_get_data(es->buffer);
695 for (i = 0; i < es->geometry.height; i++)
696 memcpy(buf + i * 64, s + i * stride,
697 es->geometry.width * 4);
699 if (gbm_bo_write(bo, buf, sizeof buf) < 0)
702 handle = gbm_bo_get_handle(bo).s32;
703 if (drmModeSetCursor(c->drm.fd,
704 output->crtc_id, handle, 64, 64)) {
705 weston_log("failed to set cursor: %n\n");
710 x = es->geometry.x - output->base.x;
711 y = es->geometry.y - output->base.y;
712 if (output->cursor_x != x || output->cursor_y != y) {
713 if (drmModeMoveCursor(c->drm.fd, output->crtc_id, x, y)) {
714 weston_log("failed to move cursor: %m\n");
717 output->cursor_x = x;
718 output->cursor_y = y;
721 es->plane = WESTON_PLANE_DRM_CURSOR;
722 output->cursor_free = 0;
726 drm_assign_planes(struct weston_output *output)
728 struct drm_compositor *c =
729 (struct drm_compositor *) output->compositor;
730 struct drm_output *drm_output = (struct drm_output *) output;
731 struct weston_surface *es, *next;
732 pixman_region32_t overlap, surface_overlap;
736 * Find a surface for each sprite in the output using some heuristics:
738 * 2) frequency of update
739 * 3) opacity (though some hw might support alpha blending)
740 * 4) clipping (this can be fixed with color keys)
742 * The idea is to save on blitting since this should save power.
743 * If we can get a large video surface on the sprite for example,
744 * the main display surface may not need to update at all, and
745 * the client buffer can be used directly for the sprite surface
746 * as we do for flipping full screen surfaces.
748 pixman_region32_init(&overlap);
749 drm_output->cursor_free = 1;
750 wl_list_for_each_safe(es, next, &c->base.surface_list, link) {
751 pixman_region32_init(&surface_overlap);
752 pixman_region32_intersect(&surface_overlap, &overlap,
753 &es->transform.boundingbox);
755 prev_plane = es->plane;
756 es->plane = WESTON_PLANE_PRIMARY;
757 if (pixman_region32_not_empty(&surface_overlap))
760 if (es->plane == WESTON_PLANE_PRIMARY)
761 drm_output_set_cursor(output, es);
762 if (es->plane == WESTON_PLANE_PRIMARY)
763 drm_output_prepare_scanout_surface(output, es);
764 if (es->plane == WESTON_PLANE_PRIMARY)
765 drm_output_prepare_overlay_surface(output, es);
768 if (es->plane == WESTON_PLANE_PRIMARY)
769 pixman_region32_union(&overlap, &overlap,
770 &es->transform.boundingbox);
772 if (prev_plane != WESTON_PLANE_PRIMARY &&
773 es->plane == WESTON_PLANE_PRIMARY)
774 weston_surface_damage(es);
775 else if (prev_plane == WESTON_PLANE_PRIMARY &&
776 es->plane != WESTON_PLANE_PRIMARY)
777 weston_surface_damage_below(es);
779 pixman_region32_fini(&surface_overlap);
781 pixman_region32_fini(&overlap);
783 if (drm_output->cursor_free)
784 drmModeSetCursor(c->drm.fd, drm_output->crtc_id, 0, 0, 0);
786 drm_disable_unused_sprites(output);
790 drm_output_destroy(struct weston_output *output_base)
792 struct drm_output *output = (struct drm_output *) output_base;
793 struct drm_compositor *c =
794 (struct drm_compositor *) output->base.compositor;
795 drmModeCrtcPtr origcrtc = output->original_crtc;
797 if (output->backlight)
798 backlight_destroy(output->backlight);
800 /* Turn off hardware cursor */
801 drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0);
803 /* Restore original CRTC state */
804 drmModeSetCrtc(c->drm.fd, origcrtc->crtc_id, origcrtc->buffer_id,
805 origcrtc->x, origcrtc->y,
806 &output->connector_id, 1, &origcrtc->mode);
807 drmModeFreeCrtc(origcrtc);
809 c->crtc_allocator &= ~(1 << output->crtc_id);
810 c->connector_allocator &= ~(1 << output->connector_id);
812 eglDestroySurface(c->base.egl_display, output->egl_surface);
813 gbm_surface_destroy(output->surface);
815 weston_output_destroy(&output->base);
816 wl_list_remove(&output->base.link);
822 static struct drm_mode *
823 choose_mode (struct drm_output *output, struct weston_mode *target_mode)
825 struct drm_mode *tmp_mode = NULL, *mode;
827 if (output->base.current->width == target_mode->width &&
828 output->base.current->height == target_mode->height &&
829 (output->base.current->refresh == target_mode->refresh ||
830 target_mode->refresh == 0))
831 return (struct drm_mode *)output->base.current;
833 wl_list_for_each(mode, &output->base.mode_list, base.link) {
834 if (mode->mode_info.hdisplay == target_mode->width &&
835 mode->mode_info.vdisplay == target_mode->height) {
836 if (mode->mode_info.vrefresh == target_mode->refresh ||
837 target_mode->refresh == 0) {
839 } else if (!tmp_mode)
848 drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mode)
850 struct drm_output *output;
851 struct drm_mode *drm_mode;
853 struct drm_compositor *ec;
854 struct gbm_surface *surface;
855 EGLSurface egl_surface;
857 if (output_base == NULL) {
858 weston_log("output is NULL.\n");
863 weston_log("mode is NULL.\n");
867 ec = (struct drm_compositor *)output_base->compositor;
868 output = (struct drm_output *)output_base;
869 drm_mode = choose_mode (output, mode);
872 weston_log("%s, invalid resolution:%dx%d\n", __func__, mode->width, mode->height);
874 } else if (&drm_mode->base == output->base.current) {
876 } else if (drm_mode->base.width == output->base.current->width &&
877 drm_mode->base.height == output->base.current->height) {
878 /* only change refresh value */
879 ret = drmModeSetCrtc(ec->drm.fd,
881 output->current->fb_id, 0, 0,
882 &output->connector_id, 1, &drm_mode->mode_info);
885 weston_log("failed to set mode (%dx%d) %u Hz\n",
886 drm_mode->base.width,
887 drm_mode->base.height,
888 drm_mode->base.refresh / 1000);
891 output->base.current->flags = 0;
892 output->base.current = &drm_mode->base;
893 drm_mode->base.flags =
894 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
901 drm_mode->base.flags =
902 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
904 surface = gbm_surface_create(ec->gbm,
905 drm_mode->base.width,
906 drm_mode->base.height,
909 GBM_BO_USE_RENDERING);
911 weston_log("failed to create gbm surface\n");
916 eglCreateWindowSurface(ec->base.egl_display,
920 if (egl_surface == EGL_NO_SURFACE) {
921 weston_log("failed to create egl surface\n");
925 ret = drmModeSetCrtc(ec->drm.fd,
927 output->current->fb_id, 0, 0,
928 &output->connector_id, 1, &drm_mode->mode_info);
930 weston_log("failed to set mode\n");
934 /* reset rendering stuff. */
935 if (output->current) {
936 if (output->current->is_client_buffer)
937 gbm_bo_destroy(output->current->bo);
939 gbm_surface_release_buffer(output->surface,
940 output->current->bo);
942 output->current = NULL;
945 if (output->next->is_client_buffer)
946 gbm_bo_destroy(output->next->bo);
948 gbm_surface_release_buffer(output->surface,
953 eglDestroySurface(ec->base.egl_display, output->egl_surface);
954 gbm_surface_destroy(output->surface);
955 output->egl_surface = egl_surface;
956 output->surface = surface;
959 output->base.current = &drm_mode->base;
960 output->base.dirty = 1;
961 weston_output_move(&output->base, output->base.x, output->base.y);
965 eglDestroySurface(ec->base.egl_display, egl_surface);
966 gbm_surface_destroy(surface);
971 on_drm_input(int fd, uint32_t mask, void *data)
973 drmEventContext evctx;
975 memset(&evctx, 0, sizeof evctx);
976 evctx.version = DRM_EVENT_CONTEXT_VERSION;
977 evctx.page_flip_handler = page_flip_handler;
978 evctx.vblank_handler = vblank_handler;
979 drmHandleEvent(fd, &evctx);
985 init_egl(struct drm_compositor *ec, struct udev_device *device)
987 EGLint major, minor, n;
988 const char *filename, *sysnum;
990 static const EGLint context_attribs[] = {
991 EGL_CONTEXT_CLIENT_VERSION, 2,
995 static const EGLint config_attribs[] = {
996 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
1001 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
1005 sysnum = udev_device_get_sysnum(device);
1007 ec->drm.id = atoi(sysnum);
1008 if (!sysnum || ec->drm.id < 0) {
1009 weston_log("cannot get device sysnum\n");
1013 filename = udev_device_get_devnode(device);
1014 fd = open(filename, O_RDWR | O_CLOEXEC);
1016 /* Probably permissions error */
1017 weston_log("couldn't open %s, skipping\n",
1018 udev_device_get_devnode(device));
1022 weston_log("using %s\n", filename);
1025 ec->gbm = gbm_create_device(ec->drm.fd);
1026 ec->base.egl_display = eglGetDisplay(ec->gbm);
1027 if (ec->base.egl_display == NULL) {
1028 weston_log("failed to create display\n");
1032 if (!eglInitialize(ec->base.egl_display, &major, &minor)) {
1033 weston_log("failed to initialize display\n");
1037 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
1038 weston_log("failed to bind api EGL_OPENGL_ES_API\n");
1042 if (!eglChooseConfig(ec->base.egl_display, config_attribs,
1043 &ec->base.egl_config, 1, &n) || n != 1) {
1044 weston_log("failed to choose config: %d\n", n);
1048 ec->base.egl_context =
1049 eglCreateContext(ec->base.egl_display, ec->base.egl_config,
1050 EGL_NO_CONTEXT, context_attribs);
1051 if (ec->base.egl_context == NULL) {
1052 weston_log("failed to create context\n");
1056 ec->dummy_surface = gbm_surface_create(ec->gbm, 10, 10,
1057 GBM_FORMAT_XRGB8888,
1058 GBM_BO_USE_RENDERING);
1059 if (!ec->dummy_surface) {
1060 weston_log("failed to create dummy gbm surface\n");
1064 ec->dummy_egl_surface =
1065 eglCreateWindowSurface(ec->base.egl_display,
1066 ec->base.egl_config,
1069 if (ec->dummy_egl_surface == EGL_NO_SURFACE) {
1070 weston_log("failed to create egl surface\n");
1074 if (!eglMakeCurrent(ec->base.egl_display, ec->dummy_egl_surface,
1075 ec->dummy_egl_surface, ec->base.egl_context)) {
1076 weston_log("failed to make context current\n");
1084 drm_output_add_mode(struct drm_output *output, drmModeModeInfo *info)
1086 struct drm_mode *mode;
1089 mode = malloc(sizeof *mode);
1093 mode->base.flags = 0;
1094 mode->base.width = info->hdisplay;
1095 mode->base.height = info->vdisplay;
1097 /* Calculate higher precision (mHz) refresh rate */
1098 refresh = (info->clock * 1000000LL / info->htotal +
1099 info->vtotal / 2) / info->vtotal;
1101 if (info->flags & DRM_MODE_FLAG_INTERLACE)
1103 if (info->flags & DRM_MODE_FLAG_DBLSCAN)
1105 if (info->vscan > 1)
1106 refresh /= info->vscan;
1108 mode->base.refresh = refresh;
1109 mode->mode_info = *info;
1111 if (info->type & DRM_MODE_TYPE_PREFERRED)
1112 mode->base.flags |= WL_OUTPUT_MODE_PREFERRED;
1114 wl_list_insert(output->base.mode_list.prev, &mode->base.link);
1120 drm_subpixel_to_wayland(int drm_value)
1122 switch (drm_value) {
1124 case DRM_MODE_SUBPIXEL_UNKNOWN:
1125 return WL_OUTPUT_SUBPIXEL_UNKNOWN;
1126 case DRM_MODE_SUBPIXEL_NONE:
1127 return WL_OUTPUT_SUBPIXEL_NONE;
1128 case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB:
1129 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB;
1130 case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR:
1131 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR;
1132 case DRM_MODE_SUBPIXEL_VERTICAL_RGB:
1133 return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB;
1134 case DRM_MODE_SUBPIXEL_VERTICAL_BGR:
1135 return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR;
1140 sprite_handle_buffer_destroy(struct wl_listener *listener, void *data)
1142 struct drm_sprite *sprite =
1143 container_of(listener, struct drm_sprite,
1145 struct drm_compositor *compositor = sprite->compositor;
1147 sprite->surface = NULL;
1148 drmModeRmFB(compositor->drm.fd, sprite->fb_id);
1153 sprite_handle_pending_buffer_destroy(struct wl_listener *listener, void *data)
1155 struct drm_sprite *sprite =
1156 container_of(listener, struct drm_sprite,
1157 pending_destroy_listener);
1158 struct drm_compositor *compositor = sprite->compositor;
1160 sprite->pending_surface = NULL;
1161 drmModeRmFB(compositor->drm.fd, sprite->pending_fb_id);
1162 sprite->pending_fb_id = 0;
1165 /* returns a value between 0-255 range, where higher is brighter */
1167 drm_get_backlight(struct drm_output *output)
1169 long brightness, max_brightness, norm;
1171 brightness = backlight_get_brightness(output->backlight);
1172 max_brightness = backlight_get_max_brightness(output->backlight);
1174 /* convert it on a scale of 0 to 255 */
1175 norm = (brightness * 255)/(max_brightness);
1177 return (uint32_t) norm;
1180 /* values accepted are between 0-255 range */
1182 drm_set_backlight(struct weston_output *output_base, uint32_t value)
1184 struct drm_output *output = (struct drm_output *) output_base;
1185 long max_brightness, new_brightness;
1187 if (!output->backlight)
1193 max_brightness = backlight_get_max_brightness(output->backlight);
1195 /* get denormalized value */
1196 new_brightness = (value * max_brightness) / 255;
1198 backlight_set_brightness(output->backlight, new_brightness);
1201 static drmModePropertyPtr
1202 drm_get_prop(int fd, drmModeConnectorPtr connector, const char *name)
1204 drmModePropertyPtr props;
1207 for (i = 0; i < connector->count_props; i++) {
1208 props = drmModeGetProperty(fd, connector->props[i]);
1212 if (!strcmp(props->name, name))
1215 drmModeFreeProperty(props);
1222 drm_set_dpms(struct weston_output *output_base, enum dpms_enum level)
1224 struct drm_output *output = (struct drm_output *) output_base;
1225 struct weston_compositor *ec = output_base->compositor;
1226 struct drm_compositor *c = (struct drm_compositor *) ec;
1227 drmModeConnectorPtr connector;
1228 drmModePropertyPtr prop;
1230 connector = drmModeGetConnector(c->drm.fd, output->connector_id);
1234 prop = drm_get_prop(c->drm.fd, connector, "DPMS");
1236 drmModeFreeConnector(connector);
1240 drmModeConnectorSetProperty(c->drm.fd, connector->connector_id,
1241 prop->prop_id, level);
1242 drmModeFreeProperty(prop);
1243 drmModeFreeConnector(connector);
1246 static const char *connector_type_names[] = {
1265 find_crtc_for_connector(struct drm_compositor *ec,
1266 drmModeRes *resources, drmModeConnector *connector)
1268 drmModeEncoder *encoder;
1269 uint32_t possible_crtcs;
1272 for (j = 0; j < connector->count_encoders; j++) {
1273 encoder = drmModeGetEncoder(ec->drm.fd, connector->encoders[j]);
1274 if (encoder == NULL) {
1275 weston_log("Failed to get encoder.\n");
1278 possible_crtcs = encoder->possible_crtcs;
1279 drmModeFreeEncoder(encoder);
1281 for (i = 0; i < resources->count_crtcs; i++) {
1282 if (possible_crtcs & (1 << i) &&
1283 !(ec->crtc_allocator & (1 << resources->crtcs[i])))
1292 create_output_for_connector(struct drm_compositor *ec,
1293 drmModeRes *resources,
1294 drmModeConnector *connector,
1295 int x, int y, struct udev_device *drm_device)
1297 struct drm_output *output;
1298 struct drm_mode *drm_mode, *next;
1299 struct weston_mode *m, *preferred, *current;
1300 drmModeEncoder *encoder;
1301 drmModeModeInfo crtc_mode;
1305 const char *type_name;
1307 i = find_crtc_for_connector(ec, resources, connector);
1309 weston_log("No usable crtc/encoder pair for connector.\n");
1313 output = malloc(sizeof *output);
1317 memset(output, 0, sizeof *output);
1318 output->base.subpixel = drm_subpixel_to_wayland(connector->subpixel);
1319 output->base.make = "unknown";
1320 output->base.model = "unknown";
1321 wl_list_init(&output->base.mode_list);
1323 if (connector->connector_type < ARRAY_LENGTH(connector_type_names))
1324 type_name = connector_type_names[connector->connector_type];
1326 type_name = "UNKNOWN";
1327 snprintf(name, 32, "%s%d", type_name, connector->connector_type_id);
1328 output->name = strdup(name);
1330 output->crtc_id = resources->crtcs[i];
1331 ec->crtc_allocator |= (1 << output->crtc_id);
1332 output->connector_id = connector->connector_id;
1333 ec->connector_allocator |= (1 << output->connector_id);
1335 output->original_crtc = drmModeGetCrtc(ec->drm.fd, output->crtc_id);
1337 /* Get the current mode on the crtc that's currently driving
1338 * this connector. */
1339 encoder = drmModeGetEncoder(ec->drm.fd, connector->encoder_id);
1340 if (encoder == NULL)
1342 crtc = drmModeGetCrtc(ec->drm.fd, encoder->crtc_id);
1343 drmModeFreeEncoder(encoder);
1346 crtc_mode = crtc->mode;
1347 drmModeFreeCrtc(crtc);
1349 for (i = 0; i < connector->count_modes; i++) {
1350 ret = drm_output_add_mode(output, &connector->modes[i]);
1357 wl_list_for_each(drm_mode, &output->base.mode_list, base.link) {
1358 if (!memcmp(&crtc_mode, &drm_mode->mode_info, sizeof crtc_mode)) {
1359 drm_mode->base.flags |= WL_OUTPUT_MODE_CURRENT;
1360 current = &drm_mode->base;
1362 if (drm_mode->base.flags & WL_OUTPUT_MODE_PREFERRED)
1363 preferred = &drm_mode->base;
1366 if (current == NULL) {
1367 ret = drm_output_add_mode(output, &crtc_mode);
1370 current = container_of(output->base.mode_list.prev,
1371 struct weston_mode, link);
1372 current->flags |= WL_OUTPUT_MODE_CURRENT;
1375 if (preferred == NULL)
1376 preferred = current;
1378 if (option_current_mode) {
1379 output->base.current = current;
1381 output->base.current = preferred;
1382 current->flags &= ~WL_OUTPUT_MODE_CURRENT;
1383 preferred->flags |= WL_OUTPUT_MODE_CURRENT;
1386 output->surface = gbm_surface_create(ec->gbm,
1387 output->base.current->width,
1388 output->base.current->height,
1389 GBM_FORMAT_XRGB8888,
1390 GBM_BO_USE_SCANOUT |
1391 GBM_BO_USE_RENDERING);
1392 if (!output->surface) {
1393 weston_log("failed to create gbm surface\n");
1397 output->egl_surface =
1398 eglCreateWindowSurface(ec->base.egl_display,
1399 ec->base.egl_config,
1402 if (output->egl_surface == EGL_NO_SURFACE) {
1403 weston_log("failed to create egl surface\n");
1407 output->cursor_bo[0] =
1408 gbm_bo_create(ec->gbm, 64, 64, GBM_FORMAT_ARGB8888,
1409 GBM_BO_USE_CURSOR_64X64 | GBM_BO_USE_WRITE);
1410 output->cursor_bo[1] =
1411 gbm_bo_create(ec->gbm, 64, 64, GBM_FORMAT_ARGB8888,
1412 GBM_BO_USE_CURSOR_64X64 | GBM_BO_USE_WRITE);
1414 output->backlight = backlight_init(drm_device,
1415 connector->connector_type);
1416 if (output->backlight) {
1417 output->base.set_backlight = drm_set_backlight;
1418 output->base.backlight_current = drm_get_backlight(output);
1421 weston_output_init(&output->base, &ec->base, x, y,
1422 connector->mmWidth, connector->mmHeight,
1425 wl_list_insert(ec->base.output_list.prev, &output->base.link);
1427 output->base.origin = output->base.current;
1428 output->base.repaint = drm_output_repaint;
1429 output->base.destroy = drm_output_destroy;
1430 output->base.assign_planes = drm_assign_planes;
1431 output->base.set_dpms = drm_set_dpms;
1432 output->base.switch_mode = drm_output_switch_mode;
1434 weston_log("Output %s, (connector %d, crtc %d)\n",
1435 output->name, output->connector_id, output->crtc_id);
1436 wl_list_for_each(m, &output->base.mode_list, link)
1437 weston_log_continue(" mode %dx%d@%.1f%s%s%s\n",
1438 m->width, m->height, m->refresh / 1000.0,
1439 m->flags & WL_OUTPUT_MODE_PREFERRED ?
1441 m->flags & WL_OUTPUT_MODE_CURRENT ?
1443 connector->count_modes == 0 ?
1449 gbm_surface_destroy(output->surface);
1451 wl_list_for_each_safe(drm_mode, next, &output->base.mode_list,
1453 wl_list_remove(&drm_mode->base.link);
1457 drmModeFreeCrtc(output->original_crtc);
1458 ec->crtc_allocator &= ~(1 << output->crtc_id);
1459 ec->connector_allocator &= ~(1 << output->connector_id);
1467 create_sprites(struct drm_compositor *ec)
1469 struct drm_sprite *sprite;
1470 drmModePlaneRes *plane_res;
1471 drmModePlane *plane;
1474 plane_res = drmModeGetPlaneResources(ec->drm.fd);
1476 weston_log("failed to get plane resources: %s\n",
1481 for (i = 0; i < plane_res->count_planes; i++) {
1482 plane = drmModeGetPlane(ec->drm.fd, plane_res->planes[i]);
1486 sprite = malloc(sizeof(*sprite) + ((sizeof(uint32_t)) *
1487 plane->count_formats));
1489 weston_log("%s: out of memory\n",
1495 memset(sprite, 0, sizeof *sprite);
1497 sprite->possible_crtcs = plane->possible_crtcs;
1498 sprite->plane_id = plane->plane_id;
1499 sprite->surface = NULL;
1500 sprite->pending_surface = NULL;
1502 sprite->pending_fb_id = 0;
1503 sprite->destroy_listener.notify = sprite_handle_buffer_destroy;
1504 sprite->pending_destroy_listener.notify =
1505 sprite_handle_pending_buffer_destroy;
1506 sprite->compositor = ec;
1507 sprite->count_formats = plane->count_formats;
1508 memcpy(sprite->formats, plane->formats,
1509 plane->count_formats * sizeof(plane->formats[0]));
1510 drmModeFreePlane(plane);
1512 wl_list_insert(&ec->sprite_list, &sprite->link);
1515 free(plane_res->planes);
1520 destroy_sprites(struct drm_compositor *compositor)
1522 struct drm_sprite *sprite, *next;
1523 struct drm_output *output;
1525 output = container_of(compositor->base.output_list.next,
1526 struct drm_output, base.link);
1528 wl_list_for_each_safe(sprite, next, &compositor->sprite_list, link) {
1529 drmModeSetPlane(compositor->drm.fd,
1531 output->crtc_id, 0, 0,
1532 0, 0, 0, 0, 0, 0, 0, 0);
1533 drmModeRmFB(compositor->drm.fd, sprite->fb_id);
1539 create_outputs(struct drm_compositor *ec, uint32_t option_connector,
1540 struct udev_device *drm_device)
1542 drmModeConnector *connector;
1543 drmModeRes *resources;
1547 resources = drmModeGetResources(ec->drm.fd);
1549 weston_log("drmModeGetResources failed\n");
1553 ec->crtcs = calloc(resources->count_crtcs, sizeof(uint32_t));
1555 drmModeFreeResources(resources);
1559 ec->num_crtcs = resources->count_crtcs;
1560 memcpy(ec->crtcs, resources->crtcs, sizeof(uint32_t) * ec->num_crtcs);
1562 for (i = 0; i < resources->count_connectors; i++) {
1563 connector = drmModeGetConnector(ec->drm.fd,
1564 resources->connectors[i]);
1565 if (connector == NULL)
1568 if (connector->connection == DRM_MODE_CONNECTED &&
1569 (option_connector == 0 ||
1570 connector->connector_id == option_connector)) {
1571 if (create_output_for_connector(ec, resources,
1574 drmModeFreeConnector(connector);
1578 x += container_of(ec->base.output_list.prev,
1579 struct weston_output,
1580 link)->current->width;
1583 drmModeFreeConnector(connector);
1586 if (wl_list_empty(&ec->base.output_list)) {
1587 weston_log("No currently active connector found.\n");
1588 drmModeFreeResources(resources);
1592 drmModeFreeResources(resources);
1598 update_outputs(struct drm_compositor *ec, struct udev_device *drm_device)
1600 drmModeConnector *connector;
1601 drmModeRes *resources;
1602 struct drm_output *output, *next;
1604 int x_offset = 0, y_offset = 0;
1605 uint32_t connected = 0, disconnects = 0;
1608 resources = drmModeGetResources(ec->drm.fd);
1610 weston_log("drmModeGetResources failed\n");
1614 /* collect new connects */
1615 for (i = 0; i < resources->count_connectors; i++) {
1616 int connector_id = resources->connectors[i];
1618 connector = drmModeGetConnector(ec->drm.fd, connector_id);
1619 if (connector == NULL)
1622 if (connector->connection != DRM_MODE_CONNECTED) {
1623 drmModeFreeConnector(connector);
1627 connected |= (1 << connector_id);
1629 if (!(ec->connector_allocator & (1 << connector_id))) {
1630 struct weston_output *last =
1631 container_of(ec->base.output_list.prev,
1632 struct weston_output, link);
1634 /* XXX: not yet needed, we die with 0 outputs */
1635 if (!wl_list_empty(&ec->base.output_list))
1636 x = last->x + last->current->width;
1640 create_output_for_connector(ec, resources,
1643 weston_log("connector %d connected\n", connector_id);
1646 drmModeFreeConnector(connector);
1648 drmModeFreeResources(resources);
1650 disconnects = ec->connector_allocator & ~connected;
1652 wl_list_for_each_safe(output, next, &ec->base.output_list,
1654 if (x_offset != 0 || y_offset != 0) {
1655 weston_output_move(&output->base,
1656 output->base.x - x_offset,
1657 output->base.y - y_offset);
1660 if (disconnects & (1 << output->connector_id)) {
1661 disconnects &= ~(1 << output->connector_id);
1662 weston_log("connector %d disconnected\n",
1663 output->connector_id);
1664 x_offset += output->base.current->width;
1665 drm_output_destroy(&output->base);
1670 /* FIXME: handle zero outputs, without terminating */
1671 if (ec->connector_allocator == 0)
1672 wl_display_terminate(ec->base.wl_display);
1676 udev_event_is_hotplug(struct drm_compositor *ec, struct udev_device *device)
1681 sysnum = udev_device_get_sysnum(device);
1682 if (!sysnum || atoi(sysnum) != ec->drm.id)
1685 val = udev_device_get_property_value(device, "HOTPLUG");
1689 return strcmp(val, "1") == 0;
1693 udev_drm_event(int fd, uint32_t mask, void *data)
1695 struct drm_compositor *ec = data;
1696 struct udev_device *event;
1698 event = udev_monitor_receive_device(ec->udev_monitor);
1700 if (udev_event_is_hotplug(ec, event))
1701 update_outputs(ec, event);
1703 udev_device_unref(event);
1709 drm_destroy(struct weston_compositor *ec)
1711 struct drm_compositor *d = (struct drm_compositor *) ec;
1712 struct weston_seat *seat, *next;
1714 wl_list_for_each_safe(seat, next, &ec->seat_list, link)
1715 evdev_input_destroy(seat);
1717 wl_event_source_remove(d->udev_drm_source);
1718 wl_event_source_remove(d->drm_source);
1720 weston_compositor_shutdown(ec);
1722 /* Work around crash in egl_dri2.c's dri2_make_current() */
1723 eglMakeCurrent(ec->egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
1725 eglTerminate(ec->egl_display);
1728 gbm_device_destroy(d->gbm);
1730 if (weston_launcher_drm_set_master(&d->base, d->drm.fd, 0) < 0)
1731 weston_log("failed to drop master: %m\n");
1732 tty_destroy(d->tty);
1738 drm_compositor_set_modes(struct drm_compositor *compositor)
1740 struct drm_output *output;
1741 struct drm_mode *drm_mode;
1744 wl_list_for_each(output, &compositor->base.output_list, base.link) {
1745 drm_mode = (struct drm_mode *) output->base.current;
1746 ret = drmModeSetCrtc(compositor->drm.fd, output->crtc_id,
1747 output->current->fb_id, 0, 0,
1748 &output->connector_id, 1,
1749 &drm_mode->mode_info);
1752 "failed to set mode %dx%d for output at %d,%d: %m\n",
1753 drm_mode->base.width, drm_mode->base.height,
1754 output->base.x, output->base.y);
1760 vt_func(struct weston_compositor *compositor, int event)
1762 struct drm_compositor *ec = (struct drm_compositor *) compositor;
1763 struct weston_seat *seat;
1764 struct drm_sprite *sprite;
1765 struct drm_output *output;
1769 compositor->focus = 1;
1770 if (weston_launcher_drm_set_master(&ec->base, ec->drm.fd, 1)) {
1771 weston_log("failed to set master: %m\n");
1772 wl_display_terminate(compositor->wl_display);
1774 compositor->state = ec->prev_state;
1775 drm_compositor_set_modes(ec);
1776 weston_compositor_damage_all(compositor);
1777 wl_list_for_each(seat, &compositor->seat_list, link) {
1778 evdev_add_devices(ec->udev, seat);
1779 evdev_enable_udev_monitor(ec->udev, seat);
1783 wl_list_for_each(seat, &compositor->seat_list, link) {
1784 evdev_disable_udev_monitor(seat);
1785 evdev_remove_devices(seat);
1788 compositor->focus = 0;
1789 ec->prev_state = compositor->state;
1790 compositor->state = WESTON_COMPOSITOR_SLEEPING;
1792 /* If we have a repaint scheduled (either from a
1793 * pending pageflip or the idle handler), make sure we
1794 * cancel that so we don't try to pageflip when we're
1795 * vt switched away. The SLEEPING state will prevent
1796 * further attemps at repainting. When we switch
1797 * back, we schedule a repaint, which will process
1798 * pending frame callbacks. */
1800 wl_list_for_each(output, &ec->base.output_list, base.link) {
1801 output->base.repaint_needed = 0;
1802 drmModeSetCursor(ec->drm.fd, output->crtc_id, 0, 0, 0);
1805 output = container_of(ec->base.output_list.next,
1806 struct drm_output, base.link);
1808 wl_list_for_each(sprite, &ec->sprite_list, link)
1809 drmModeSetPlane(ec->drm.fd,
1811 output->crtc_id, 0, 0,
1812 0, 0, 0, 0, 0, 0, 0, 0);
1814 if (weston_launcher_drm_set_master(&ec->base, ec->drm.fd, 0) < 0)
1815 weston_log("failed to drop master: %m\n");
1822 switch_vt_binding(struct wl_seat *seat, uint32_t time, uint32_t key, void *data)
1824 struct drm_compositor *ec = data;
1826 tty_activate_vt(ec->tty, key - KEY_F1 + 1);
1829 static const char default_seat[] = "seat0";
1831 static struct weston_compositor *
1832 drm_compositor_create(struct wl_display *display,
1833 int connector, const char *seat, int tty,
1834 int argc, char *argv[], const char *config_file)
1836 struct drm_compositor *ec;
1837 struct udev_enumerate *e;
1838 struct udev_list_entry *entry;
1839 struct udev_device *device, *drm_device;
1840 const char *path, *device_seat;
1841 struct wl_event_loop *loop;
1842 struct weston_seat *weston_seat, *next;
1845 weston_log("initializing drm backend\n");
1847 ec = malloc(sizeof *ec);
1850 memset(ec, 0, sizeof *ec);
1852 if (weston_compositor_init(&ec->base, display, argc, argv,
1854 weston_log("weston_compositor_init failed\n");
1858 ec->udev = udev_new();
1859 if (ec->udev == NULL) {
1860 weston_log("failed to initialize udev context\n");
1861 goto err_compositor;
1864 ec->base.wl_display = display;
1865 ec->tty = tty_create(&ec->base, vt_func, tty);
1867 weston_log("failed to initialize tty\n");
1871 e = udev_enumerate_new(ec->udev);
1872 udev_enumerate_add_match_subsystem(e, "drm");
1873 udev_enumerate_add_match_sysname(e, "card[0-9]*");
1875 udev_enumerate_scan_devices(e);
1877 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
1878 path = udev_list_entry_get_name(entry);
1879 device = udev_device_new_from_syspath(ec->udev, path);
1881 udev_device_get_property_value(device, "ID_SEAT");
1883 device_seat = default_seat;
1884 if (strcmp(device_seat, seat) == 0) {
1885 drm_device = device;
1888 udev_device_unref(device);
1891 if (drm_device == NULL) {
1892 weston_log("no drm device found\n");
1896 if (init_egl(ec, drm_device) < 0) {
1897 weston_log("failed to initialize egl\n");
1901 ec->base.destroy = drm_destroy;
1905 ec->prev_state = WESTON_COMPOSITOR_ACTIVE;
1907 if (weston_compositor_init_gl(&ec->base) < 0)
1910 for (key = KEY_F1; key < KEY_F9; key++)
1911 weston_compositor_add_key_binding(&ec->base, key,
1912 MODIFIER_CTRL | MODIFIER_ALT,
1913 switch_vt_binding, ec);
1915 wl_list_init(&ec->sprite_list);
1918 if (create_outputs(ec, connector, drm_device) < 0) {
1919 weston_log("failed to create output for %s\n", path);
1925 evdev_input_create(&ec->base, ec->udev, seat);
1927 loop = wl_display_get_event_loop(ec->base.wl_display);
1929 wl_event_loop_add_fd(loop, ec->drm.fd,
1930 WL_EVENT_READABLE, on_drm_input, ec);
1932 ec->udev_monitor = udev_monitor_new_from_netlink(ec->udev, "udev");
1933 if (ec->udev_monitor == NULL) {
1934 weston_log("failed to intialize udev monitor\n");
1935 goto err_drm_source;
1937 udev_monitor_filter_add_match_subsystem_devtype(ec->udev_monitor,
1939 ec->udev_drm_source =
1940 wl_event_loop_add_fd(loop,
1941 udev_monitor_get_fd(ec->udev_monitor),
1942 WL_EVENT_READABLE, udev_drm_event, ec);
1944 if (udev_monitor_enable_receiving(ec->udev_monitor) < 0) {
1945 weston_log("failed to enable udev-monitor receiving\n");
1946 goto err_udev_monitor;
1949 udev_device_unref(drm_device);
1950 udev_enumerate_unref(e);
1955 wl_event_source_remove(ec->udev_drm_source);
1956 udev_monitor_unref(ec->udev_monitor);
1958 wl_event_source_remove(ec->drm_source);
1959 wl_list_for_each_safe(weston_seat, next, &ec->base.seat_list, link)
1960 evdev_input_destroy(weston_seat);
1962 destroy_sprites(ec);
1964 eglMakeCurrent(ec->base.egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
1966 eglTerminate(ec->base.egl_display);
1968 gbm_device_destroy(ec->gbm);
1970 udev_device_unref(drm_device);
1972 udev_enumerate_unref(e);
1973 tty_destroy(ec->tty);
1975 udev_unref(ec->udev);
1977 weston_compositor_shutdown(&ec->base);
1983 WL_EXPORT struct weston_compositor *
1984 backend_init(struct wl_display *display, int argc, char *argv[],
1985 const char *config_file)
1987 int connector = 0, tty = 0;
1988 const char *seat = default_seat;
1990 const struct weston_option drm_options[] = {
1991 { WESTON_OPTION_INTEGER, "connector", 0, &connector },
1992 { WESTON_OPTION_STRING, "seat", 0, &seat },
1993 { WESTON_OPTION_INTEGER, "tty", 0, &tty },
1994 { WESTON_OPTION_BOOLEAN, "current-mode", 0, &option_current_mode },
1997 parse_options(drm_options, ARRAY_LENGTH(drm_options), argc, argv);
1999 return drm_compositor_create(display, connector, seat, tty, argc, argv,