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.
32 #include <linux/input.h>
40 #include <xf86drmMode.h>
41 #include <drm_fourcc.h>
44 #include <libbacklight.h>
47 #include "compositor.h"
48 #include "gl-renderer.h"
49 #include "pixman-renderer.h"
50 #include "udev-seat.h"
51 #include "launcher-util.h"
52 #include "vaapi-recorder.h"
54 #ifndef DRM_CAP_TIMESTAMP_MONOTONIC
55 #define DRM_CAP_TIMESTAMP_MONOTONIC 0x6
58 static int option_current_mode = 0;
61 OUTPUT_CONFIG_INVALID = 0,
63 OUTPUT_CONFIG_PREFERRED,
64 OUTPUT_CONFIG_CURRENT,
66 OUTPUT_CONFIG_MODELINE
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;
83 struct gbm_device *gbm;
86 uint32_t crtc_allocator;
87 uint32_t connector_allocator;
88 struct wl_listener session_listener;
91 /* we need these parameters in order to not fail drmModeAddFB2()
92 * due to out of bounds dimensions, and then mistakenly set
95 uint32_t min_width, max_width;
96 uint32_t min_height, max_height;
99 struct wl_list sprite_list;
100 int sprites_are_broken;
103 int cursors_are_broken;
110 struct udev_input input;
114 struct weston_mode base;
115 drmModeModeInfo mode_info;
121 struct drm_output *output;
122 uint32_t fb_id, stride, handle, size;
124 int is_client_buffer;
125 struct weston_buffer_reference buffer_ref;
127 /* Used by gbm fbs */
130 /* Used by dumb fbs */
136 char monitor_name[13];
138 char serial_number[13];
142 struct weston_output base;
146 uint32_t connector_id;
147 drmModeCrtcPtr original_crtc;
148 struct drm_edid edid;
149 drmModePropertyPtr dpms_prop;
152 int page_flip_pending;
155 struct gbm_surface *surface;
156 struct gbm_bo *cursor_bo[2];
157 struct weston_plane cursor_plane;
158 struct weston_plane fb_plane;
159 struct weston_surface *cursor_surface;
161 struct drm_fb *current, *next;
162 struct backlight *backlight;
164 struct drm_fb *dumb[2];
165 pixman_image_t *image[2];
167 pixman_region32_t previous_damage;
169 struct vaapi_recorder *recorder;
170 struct wl_listener recorder_frame_listener;
174 * An output has a primary display plane plus zero or more sprites for
175 * blending display contents.
180 struct weston_plane plane;
182 struct drm_fb *current, *next;
183 struct drm_output *output;
184 struct drm_compositor *compositor;
186 uint32_t possible_crtcs;
188 uint32_t count_formats;
190 int32_t src_x, src_y;
191 uint32_t src_w, src_h;
192 uint32_t dest_x, dest_y;
193 uint32_t dest_w, dest_h;
198 struct drm_parameters {
205 static struct gl_renderer_interface *gl_renderer;
207 static const char default_seat[] = "seat0";
210 drm_output_set_cursor(struct drm_output *output);
213 drm_sprite_crtc_supported(struct weston_output *output_base, uint32_t supported)
215 struct weston_compositor *ec = output_base->compositor;
216 struct drm_compositor *c =(struct drm_compositor *) ec;
217 struct drm_output *output = (struct drm_output *) output_base;
220 for (crtc = 0; crtc < c->num_crtcs; crtc++) {
221 if (c->crtcs[crtc] != output->crtc_id)
224 if (supported & (1 << crtc))
232 drm_fb_destroy_callback(struct gbm_bo *bo, void *data)
234 struct drm_fb *fb = data;
235 struct gbm_device *gbm = gbm_bo_get_device(bo);
238 drmModeRmFB(gbm_device_get_fd(gbm), fb->fb_id);
240 weston_buffer_reference(&fb->buffer_ref, NULL);
245 static struct drm_fb *
246 drm_fb_create_dumb(struct drm_compositor *ec, unsigned width, unsigned height)
251 struct drm_mode_create_dumb create_arg;
252 struct drm_mode_destroy_dumb destroy_arg;
253 struct drm_mode_map_dumb map_arg;
255 fb = zalloc(sizeof *fb);
259 memset(&create_arg, 0, sizeof create_arg);
261 create_arg.width = width;
262 create_arg.height = height;
264 ret = drmIoctl(ec->drm.fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_arg);
268 fb->handle = create_arg.handle;
269 fb->stride = create_arg.pitch;
270 fb->size = create_arg.size;
273 ret = drmModeAddFB(ec->drm.fd, width, height, 24, 32,
274 fb->stride, fb->handle, &fb->fb_id);
278 memset(&map_arg, 0, sizeof map_arg);
279 map_arg.handle = fb->handle;
280 ret = drmIoctl(fb->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_arg);
284 fb->map = mmap(0, fb->size, PROT_WRITE,
285 MAP_SHARED, ec->drm.fd, map_arg.offset);
286 if (fb->map == MAP_FAILED)
292 drmModeRmFB(ec->drm.fd, fb->fb_id);
294 memset(&destroy_arg, 0, sizeof(destroy_arg));
295 destroy_arg.handle = create_arg.handle;
296 drmIoctl(ec->drm.fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
303 drm_fb_destroy_dumb(struct drm_fb *fb)
305 struct drm_mode_destroy_dumb destroy_arg;
311 drmModeRmFB(fb->fd, fb->fb_id);
313 weston_buffer_reference(&fb->buffer_ref, NULL);
315 munmap(fb->map, fb->size);
317 memset(&destroy_arg, 0, sizeof(destroy_arg));
318 destroy_arg.handle = fb->handle;
319 drmIoctl(fb->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
324 static struct drm_fb *
325 drm_fb_get_from_bo(struct gbm_bo *bo,
326 struct drm_compositor *compositor, uint32_t format)
328 struct drm_fb *fb = gbm_bo_get_user_data(bo);
329 uint32_t width, height;
330 uint32_t handles[4], pitches[4], offsets[4];
336 fb = calloc(1, sizeof *fb);
342 width = gbm_bo_get_width(bo);
343 height = gbm_bo_get_height(bo);
344 fb->stride = gbm_bo_get_stride(bo);
345 fb->handle = gbm_bo_get_handle(bo).u32;
346 fb->size = fb->stride * height;
347 fb->fd = compositor->drm.fd;
349 if (compositor->min_width > width || width > compositor->max_width ||
350 compositor->min_height > height ||
351 height > compositor->max_height) {
352 weston_log("bo geometry out of bounds\n");
358 if (format && !compositor->no_addfb2) {
359 handles[0] = fb->handle;
360 pitches[0] = fb->stride;
363 ret = drmModeAddFB2(compositor->drm.fd, width, height,
364 format, handles, pitches, offsets,
367 weston_log("addfb2 failed: %m\n");
368 compositor->no_addfb2 = 1;
369 compositor->sprites_are_broken = 1;
374 ret = drmModeAddFB(compositor->drm.fd, width, height, 24, 32,
375 fb->stride, fb->handle, &fb->fb_id);
378 weston_log("failed to create kms fb: %m\n");
382 gbm_bo_set_user_data(bo, fb, drm_fb_destroy_callback);
392 drm_fb_set_buffer(struct drm_fb *fb, struct weston_buffer *buffer)
394 assert(fb->buffer_ref.buffer == NULL);
396 fb->is_client_buffer = 1;
398 weston_buffer_reference(&fb->buffer_ref, buffer);
402 drm_output_release_fb(struct drm_output *output, struct drm_fb *fb)
408 (fb != output->dumb[0] && fb != output->dumb[1])) {
409 drm_fb_destroy_dumb(fb);
411 if (fb->is_client_buffer)
412 gbm_bo_destroy(fb->bo);
414 gbm_surface_release_buffer(output->surface,
415 output->current->bo);
420 drm_output_check_scanout_format(struct drm_output *output,
421 struct weston_surface *es, struct gbm_bo *bo)
423 struct drm_compositor *c =
424 (struct drm_compositor *) output->base.compositor;
428 format = gbm_bo_get_format(bo);
430 if (format == GBM_FORMAT_ARGB8888) {
431 /* We can scanout an ARGB buffer if the surface's
432 * opaque region covers the whole output, but we have
433 * to use XRGB as the KMS format code. */
434 pixman_region32_init(&r);
435 pixman_region32_subtract(&r, &output->base.region,
438 if (!pixman_region32_not_empty(&r))
439 format = GBM_FORMAT_XRGB8888;
441 pixman_region32_fini(&r);
444 if (c->format == format)
450 static struct weston_plane *
451 drm_output_prepare_scanout_surface(struct weston_output *_output,
452 struct weston_surface *es)
454 struct drm_output *output = (struct drm_output *) _output;
455 struct drm_compositor *c =
456 (struct drm_compositor *) output->base.compositor;
457 struct weston_buffer *buffer = es->buffer_ref.buffer;
461 if (es->geometry.x != output->base.x ||
462 es->geometry.y != output->base.y ||
463 buffer == NULL || c->gbm == NULL ||
464 buffer->width != output->base.current_mode->width ||
465 buffer->height != output->base.current_mode->height ||
466 output->base.transform != es->buffer_transform ||
467 es->transform.enabled)
470 bo = gbm_bo_import(c->gbm, GBM_BO_IMPORT_WL_BUFFER,
471 buffer->resource, GBM_BO_USE_SCANOUT);
473 /* Unable to use the buffer for scanout */
477 format = drm_output_check_scanout_format(output, es, bo);
483 output->next = drm_fb_get_from_bo(bo, c, format);
489 drm_fb_set_buffer(output->next, buffer);
491 return &output->fb_plane;
495 drm_output_render_gl(struct drm_output *output, pixman_region32_t *damage)
497 struct drm_compositor *c =
498 (struct drm_compositor *) output->base.compositor;
501 c->base.renderer->repaint_output(&output->base, damage);
503 bo = gbm_surface_lock_front_buffer(output->surface);
505 weston_log("failed to lock front buffer: %m\n");
509 output->next = drm_fb_get_from_bo(bo, c, c->format);
511 weston_log("failed to get drm_fb for bo\n");
512 gbm_surface_release_buffer(output->surface, bo);
518 drm_output_render_pixman(struct drm_output *output, pixman_region32_t *damage)
520 struct weston_compositor *ec = output->base.compositor;
521 pixman_region32_t total_damage, previous_damage;
523 pixman_region32_init(&total_damage);
524 pixman_region32_init(&previous_damage);
526 pixman_region32_copy(&previous_damage, damage);
528 pixman_region32_union(&total_damage, damage, &output->previous_damage);
529 pixman_region32_copy(&output->previous_damage, &previous_damage);
531 output->current_image ^= 1;
533 output->next = output->dumb[output->current_image];
534 pixman_renderer_output_set_buffer(&output->base,
535 output->image[output->current_image]);
537 ec->renderer->repaint_output(&output->base, &total_damage);
539 pixman_region32_fini(&total_damage);
540 pixman_region32_fini(&previous_damage);
544 drm_output_render(struct drm_output *output, pixman_region32_t *damage)
546 struct drm_compositor *c =
547 (struct drm_compositor *) output->base.compositor;
550 drm_output_render_pixman(output, damage);
552 drm_output_render_gl(output, damage);
554 pixman_region32_subtract(&c->base.primary_plane.damage,
555 &c->base.primary_plane.damage, damage);
559 drm_output_set_gamma(struct weston_output *output_base,
560 uint16_t size, uint16_t *r, uint16_t *g, uint16_t *b)
563 struct drm_output *output = (struct drm_output *) output_base;
564 struct drm_compositor *compositor = (struct drm_compositor *) output->base.compositor;
567 if (output_base->gamma_size != size)
569 if (!output->original_crtc)
572 rc = drmModeCrtcSetGamma(compositor->drm.fd,
576 weston_log("set gamma failed: %m\n");
580 drm_output_repaint(struct weston_output *output_base,
581 pixman_region32_t *damage)
583 struct drm_output *output = (struct drm_output *) output_base;
584 struct drm_compositor *compositor =
585 (struct drm_compositor *) output->base.compositor;
586 struct drm_sprite *s;
587 struct drm_mode *mode;
590 if (output->destroy_pending)
594 drm_output_render(output, damage);
598 mode = container_of(output->base.current_mode, struct drm_mode, base);
599 if (!output->current) {
600 ret = drmModeSetCrtc(compositor->drm.fd, output->crtc_id,
601 output->next->fb_id, 0, 0,
602 &output->connector_id, 1,
605 weston_log("set mode failed: %m\n");
608 output_base->set_dpms(output_base, WESTON_DPMS_ON);
611 if (drmModePageFlip(compositor->drm.fd, output->crtc_id,
613 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
614 weston_log("queueing pageflip failed: %m\n");
618 output->page_flip_pending = 1;
620 drm_output_set_cursor(output);
623 * Now, update all the sprite surfaces
625 wl_list_for_each(s, &compositor->sprite_list, link) {
626 uint32_t flags = 0, fb_id = 0;
628 .request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT,
629 .request.sequence = 1,
632 if ((!s->current && !s->next) ||
633 !drm_sprite_crtc_supported(output_base, s->possible_crtcs))
636 if (s->next && !compositor->sprites_hidden)
637 fb_id = s->next->fb_id;
639 ret = drmModeSetPlane(compositor->drm.fd, s->plane_id,
640 output->crtc_id, fb_id, flags,
641 s->dest_x, s->dest_y,
642 s->dest_w, s->dest_h,
646 weston_log("setplane failed: %d: %s\n",
647 ret, strerror(errno));
649 if (output->pipe > 0)
650 vbl.request.type |= DRM_VBLANK_SECONDARY;
653 * Queue a vblank signal so we know when the surface
654 * becomes active on the display or has been replaced.
656 vbl.request.signal = (unsigned long)s;
657 ret = drmWaitVBlank(compositor->drm.fd, &vbl);
659 weston_log("vblank event request failed: %d: %s\n",
660 ret, strerror(errno));
664 output->vblank_pending = 1;
671 drm_output_start_repaint_loop(struct weston_output *output_base)
673 struct drm_output *output = (struct drm_output *) output_base;
674 struct drm_compositor *compositor = (struct drm_compositor *)
675 output_base->compositor;
680 if (output->destroy_pending)
683 if (!output->current) {
684 /* We can't page flip if there's no mode set */
688 fb_id = output->current->fb_id;
690 if (drmModePageFlip(compositor->drm.fd, output->crtc_id, fb_id,
691 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
692 weston_log("queueing pageflip failed: %m\n");
699 /* if we cannot page-flip, immediately finish frame */
700 clock_gettime(compositor->clock, &ts);
701 msec = ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
702 weston_output_finish_frame(output_base, msec);
706 vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec,
709 struct drm_sprite *s = (struct drm_sprite *)data;
710 struct drm_output *output = s->output;
713 output->vblank_pending = 0;
715 drm_output_release_fb(output, s->current);
716 s->current = s->next;
719 if (!output->page_flip_pending) {
720 msecs = sec * 1000 + usec / 1000;
721 weston_output_finish_frame(&output->base, msecs);
726 drm_output_destroy(struct weston_output *output_base);
729 page_flip_handler(int fd, unsigned int frame,
730 unsigned int sec, unsigned int usec, void *data)
732 struct drm_output *output = (struct drm_output *) data;
735 /* We don't set page_flip_pending on start_repaint_loop, in that case
736 * we just want to page flip to the current buffer to get an accurate
738 if (output->page_flip_pending) {
739 drm_output_release_fb(output, output->current);
740 output->current = output->next;
744 output->page_flip_pending = 0;
746 if (output->destroy_pending)
747 drm_output_destroy(&output->base);
748 else if (!output->vblank_pending) {
749 msecs = sec * 1000 + usec / 1000;
750 weston_output_finish_frame(&output->base, msecs);
752 /* We can't call this from frame_notify, because the output's
753 * repaint needed flag is cleared just after that */
754 if (output->recorder)
755 weston_output_schedule_repaint(&output->base);
760 drm_output_check_sprite_format(struct drm_sprite *s,
761 struct weston_surface *es, struct gbm_bo *bo)
765 format = gbm_bo_get_format(bo);
767 if (format == GBM_FORMAT_ARGB8888) {
770 pixman_region32_init_rect(&r, 0, 0,
772 es->geometry.height);
773 pixman_region32_subtract(&r, &r, &es->opaque);
775 if (!pixman_region32_not_empty(&r))
776 format = GBM_FORMAT_XRGB8888;
778 pixman_region32_fini(&r);
781 for (i = 0; i < s->count_formats; i++)
782 if (s->formats[i] == format)
789 drm_surface_transform_supported(struct weston_surface *es)
791 return !es->transform.enabled ||
792 (es->transform.matrix.type < WESTON_MATRIX_TRANSFORM_ROTATE);
795 static struct weston_plane *
796 drm_output_prepare_overlay_surface(struct weston_output *output_base,
797 struct weston_surface *es)
799 struct weston_compositor *ec = output_base->compositor;
800 struct drm_compositor *c =(struct drm_compositor *) ec;
801 struct drm_sprite *s;
804 pixman_region32_t dest_rect, src_rect;
805 pixman_box32_t *box, tbox;
807 wl_fixed_t sx1, sy1, sx2, sy2;
812 if (es->buffer_transform != output_base->transform)
815 if (es->buffer_scale != output_base->current_scale)
818 if (c->sprites_are_broken)
821 if (es->output_mask != (1u << output_base->id))
824 if (es->buffer_ref.buffer == NULL)
827 if (es->alpha != 1.0f)
830 if (wl_shm_buffer_get(es->buffer_ref.buffer->resource))
833 if (!drm_surface_transform_supported(es))
836 wl_list_for_each(s, &c->sprite_list, link) {
837 if (!drm_sprite_crtc_supported(output_base, s->possible_crtcs))
846 /* No sprites available */
850 bo = gbm_bo_import(c->gbm, GBM_BO_IMPORT_WL_BUFFER,
851 es->buffer_ref.buffer->resource,
856 format = drm_output_check_sprite_format(s, es, bo);
862 s->next = drm_fb_get_from_bo(bo, c, format);
868 drm_fb_set_buffer(s->next, es->buffer_ref.buffer);
870 box = pixman_region32_extents(&es->transform.boundingbox);
871 s->plane.x = box->x1;
872 s->plane.y = box->y1;
875 * Calculate the source & dest rects properly based on actual
876 * position (note the caller has called weston_surface_update_transform()
879 pixman_region32_init(&dest_rect);
880 pixman_region32_intersect(&dest_rect, &es->transform.boundingbox,
881 &output_base->region);
882 pixman_region32_translate(&dest_rect, -output_base->x, -output_base->y);
883 box = pixman_region32_extents(&dest_rect);
884 tbox = weston_transformed_rect(output_base->width,
886 output_base->transform,
887 output_base->current_scale,
891 s->dest_w = tbox.x2 - tbox.x1;
892 s->dest_h = tbox.y2 - tbox.y1;
893 pixman_region32_fini(&dest_rect);
895 pixman_region32_init(&src_rect);
896 pixman_region32_intersect(&src_rect, &es->transform.boundingbox,
897 &output_base->region);
898 box = pixman_region32_extents(&src_rect);
900 weston_surface_from_global_fixed(es,
901 wl_fixed_from_int(box->x1),
902 wl_fixed_from_int(box->y1),
904 weston_surface_from_global_fixed(es,
905 wl_fixed_from_int(box->x2),
906 wl_fixed_from_int(box->y2),
913 if (sx2 > wl_fixed_from_int(es->geometry.width))
914 sx2 = wl_fixed_from_int(es->geometry.width);
915 if (sy2 > wl_fixed_from_int(es->geometry.height))
916 sy2 = wl_fixed_from_int(es->geometry.height);
923 tbox = weston_transformed_rect(wl_fixed_from_int(es->geometry.width),
924 wl_fixed_from_int(es->geometry.height),
925 es->buffer_transform, es->buffer_scale, tbox);
927 s->src_x = tbox.x1 << 8;
928 s->src_y = tbox.y1 << 8;
929 s->src_w = (tbox.x2 - tbox.x1) << 8;
930 s->src_h = (tbox.y2 - tbox.y1) << 8;
931 pixman_region32_fini(&src_rect);
936 static struct weston_plane *
937 drm_output_prepare_cursor_surface(struct weston_output *output_base,
938 struct weston_surface *es)
940 struct drm_compositor *c =
941 (struct drm_compositor *) output_base->compositor;
942 struct drm_output *output = (struct drm_output *) output_base;
946 if (output->base.transform != WL_OUTPUT_TRANSFORM_NORMAL)
948 if (output->cursor_surface)
950 if (es->output_mask != (1u << output_base->id))
952 if (c->cursors_are_broken)
954 if (es->buffer_ref.buffer == NULL ||
955 !wl_shm_buffer_get(es->buffer_ref.buffer->resource) ||
956 es->geometry.width > 64 || es->geometry.height > 64)
959 output->cursor_surface = es;
961 return &output->cursor_plane;
965 drm_output_set_cursor(struct drm_output *output)
967 struct weston_surface *es = output->cursor_surface;
968 struct drm_compositor *c =
969 (struct drm_compositor *) output->base.compositor;
970 EGLint handle, stride;
972 uint32_t buf[64 * 64];
976 output->cursor_surface = NULL;
978 drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0);
982 if (es->buffer_ref.buffer &&
983 pixman_region32_not_empty(&output->cursor_plane.damage)) {
984 pixman_region32_fini(&output->cursor_plane.damage);
985 pixman_region32_init(&output->cursor_plane.damage);
986 output->current_cursor ^= 1;
987 bo = output->cursor_bo[output->current_cursor];
988 memset(buf, 0, sizeof buf);
989 stride = wl_shm_buffer_get_stride(es->buffer_ref.buffer->shm_buffer);
990 s = wl_shm_buffer_get_data(es->buffer_ref.buffer->shm_buffer);
991 for (i = 0; i < es->geometry.height; i++)
992 memcpy(buf + i * 64, s + i * stride,
993 es->geometry.width * 4);
995 if (gbm_bo_write(bo, buf, sizeof buf) < 0)
996 weston_log("failed update cursor: %m\n");
998 handle = gbm_bo_get_handle(bo).s32;
999 if (drmModeSetCursor(c->drm.fd,
1000 output->crtc_id, handle, 64, 64)) {
1001 weston_log("failed to set cursor: %m\n");
1002 c->cursors_are_broken = 1;
1006 x = (es->geometry.x - output->base.x) * output->base.current_scale;
1007 y = (es->geometry.y - output->base.y) * output->base.current_scale;
1008 if (output->cursor_plane.x != x || output->cursor_plane.y != y) {
1009 if (drmModeMoveCursor(c->drm.fd, output->crtc_id, x, y)) {
1010 weston_log("failed to move cursor: %m\n");
1011 c->cursors_are_broken = 1;
1014 output->cursor_plane.x = x;
1015 output->cursor_plane.y = y;
1020 drm_assign_planes(struct weston_output *output)
1022 struct drm_compositor *c =
1023 (struct drm_compositor *) output->compositor;
1024 struct weston_surface *es, *next;
1025 pixman_region32_t overlap, surface_overlap;
1026 struct weston_plane *primary, *next_plane;
1029 * Find a surface for each sprite in the output using some heuristics:
1031 * 2) frequency of update
1032 * 3) opacity (though some hw might support alpha blending)
1033 * 4) clipping (this can be fixed with color keys)
1035 * The idea is to save on blitting since this should save power.
1036 * If we can get a large video surface on the sprite for example,
1037 * the main display surface may not need to update at all, and
1038 * the client buffer can be used directly for the sprite surface
1039 * as we do for flipping full screen surfaces.
1041 pixman_region32_init(&overlap);
1042 primary = &c->base.primary_plane;
1043 wl_list_for_each_safe(es, next, &c->base.surface_list, link) {
1044 /* test whether this buffer can ever go into a plane:
1045 * non-shm, or small enough to be a cursor
1047 if ((es->buffer_ref.buffer &&
1048 !wl_shm_buffer_get(es->buffer_ref.buffer->resource)) ||
1049 (es->geometry.width <= 64 && es->geometry.height <= 64))
1050 es->keep_buffer = 1;
1052 es->keep_buffer = 0;
1054 pixman_region32_init(&surface_overlap);
1055 pixman_region32_intersect(&surface_overlap, &overlap,
1056 &es->transform.boundingbox);
1059 if (pixman_region32_not_empty(&surface_overlap))
1060 next_plane = primary;
1061 if (next_plane == NULL)
1062 next_plane = drm_output_prepare_cursor_surface(output, es);
1063 if (next_plane == NULL)
1064 next_plane = drm_output_prepare_scanout_surface(output, es);
1065 if (next_plane == NULL)
1066 next_plane = drm_output_prepare_overlay_surface(output, es);
1067 if (next_plane == NULL)
1068 next_plane = primary;
1069 weston_surface_move_to_plane(es, next_plane);
1070 if (next_plane == primary)
1071 pixman_region32_union(&overlap, &overlap,
1072 &es->transform.boundingbox);
1074 pixman_region32_fini(&surface_overlap);
1076 pixman_region32_fini(&overlap);
1080 drm_output_fini_pixman(struct drm_output *output);
1083 drm_output_destroy(struct weston_output *output_base)
1085 struct drm_output *output = (struct drm_output *) output_base;
1086 struct drm_compositor *c =
1087 (struct drm_compositor *) output->base.compositor;
1088 drmModeCrtcPtr origcrtc = output->original_crtc;
1090 if (output->page_flip_pending) {
1091 output->destroy_pending = 1;
1092 weston_log("destroy output while page flip pending\n");
1096 if (output->backlight)
1097 backlight_destroy(output->backlight);
1099 drmModeFreeProperty(output->dpms_prop);
1101 /* Turn off hardware cursor */
1102 drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0);
1104 /* Restore original CRTC state */
1105 drmModeSetCrtc(c->drm.fd, origcrtc->crtc_id, origcrtc->buffer_id,
1106 origcrtc->x, origcrtc->y,
1107 &output->connector_id, 1, &origcrtc->mode);
1108 drmModeFreeCrtc(origcrtc);
1110 c->crtc_allocator &= ~(1 << output->crtc_id);
1111 c->connector_allocator &= ~(1 << output->connector_id);
1113 if (c->use_pixman) {
1114 drm_output_fini_pixman(output);
1116 gl_renderer->output_destroy(output_base);
1117 gbm_surface_destroy(output->surface);
1120 weston_plane_release(&output->fb_plane);
1121 weston_plane_release(&output->cursor_plane);
1123 weston_output_destroy(&output->base);
1124 wl_list_remove(&output->base.link);
1129 static struct drm_mode *
1130 choose_mode (struct drm_output *output, struct weston_mode *target_mode)
1132 struct drm_mode *tmp_mode = NULL, *mode;
1134 if (output->base.current_mode->width == target_mode->width &&
1135 output->base.current_mode->height == target_mode->height &&
1136 (output->base.current_mode->refresh == target_mode->refresh ||
1137 target_mode->refresh == 0))
1138 return (struct drm_mode *)output->base.current_mode;
1140 wl_list_for_each(mode, &output->base.mode_list, base.link) {
1141 if (mode->mode_info.hdisplay == target_mode->width &&
1142 mode->mode_info.vdisplay == target_mode->height) {
1143 if (mode->mode_info.vrefresh == target_mode->refresh ||
1144 target_mode->refresh == 0) {
1146 } else if (!tmp_mode)
1155 drm_output_init_egl(struct drm_output *output, struct drm_compositor *ec);
1157 drm_output_init_pixman(struct drm_output *output, struct drm_compositor *c);
1160 drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mode)
1162 struct drm_output *output;
1163 struct drm_mode *drm_mode;
1164 struct drm_compositor *ec;
1166 if (output_base == NULL) {
1167 weston_log("output is NULL.\n");
1172 weston_log("mode is NULL.\n");
1176 ec = (struct drm_compositor *)output_base->compositor;
1177 output = (struct drm_output *)output_base;
1178 drm_mode = choose_mode (output, mode);
1181 weston_log("%s, invalid resolution:%dx%d\n", __func__, mode->width, mode->height);
1185 if (&drm_mode->base == output->base.current_mode)
1188 output->base.current_mode->flags = 0;
1190 output->base.current_mode = &drm_mode->base;
1191 output->base.current_mode->flags =
1192 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
1194 /* reset rendering stuff. */
1195 drm_output_release_fb(output, output->current);
1196 drm_output_release_fb(output, output->next);
1197 output->current = output->next = NULL;
1199 if (ec->use_pixman) {
1200 drm_output_fini_pixman(output);
1201 if (drm_output_init_pixman(output, ec) < 0) {
1202 weston_log("failed to init output pixman state with "
1207 gl_renderer->output_destroy(&output->base);
1208 gbm_surface_destroy(output->surface);
1210 if (drm_output_init_egl(output, ec) < 0) {
1211 weston_log("failed to init output egl state with "
1221 on_drm_input(int fd, uint32_t mask, void *data)
1223 drmEventContext evctx;
1225 memset(&evctx, 0, sizeof evctx);
1226 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1227 evctx.page_flip_handler = page_flip_handler;
1228 evctx.vblank_handler = vblank_handler;
1229 drmHandleEvent(fd, &evctx);
1235 init_drm(struct drm_compositor *ec, struct udev_device *device)
1237 const char *filename, *sysnum;
1241 sysnum = udev_device_get_sysnum(device);
1243 ec->drm.id = atoi(sysnum);
1244 if (!sysnum || ec->drm.id < 0) {
1245 weston_log("cannot get device sysnum\n");
1249 filename = udev_device_get_devnode(device);
1250 fd = weston_launcher_open(ec->base.launcher, filename, O_RDWR);
1252 /* Probably permissions error */
1253 weston_log("couldn't open %s, skipping\n",
1254 udev_device_get_devnode(device));
1258 weston_log("using %s\n", filename);
1261 ec->drm.filename = strdup(filename);
1263 ret = drmGetCap(fd, DRM_CAP_TIMESTAMP_MONOTONIC, &cap);
1264 if (ret == 0 && cap == 1)
1265 ec->clock = CLOCK_MONOTONIC;
1267 ec->clock = CLOCK_REALTIME;
1273 init_egl(struct drm_compositor *ec)
1277 gl_renderer = weston_load_module("gl-renderer.so",
1278 "gl_renderer_interface");
1282 /* GBM will load a dri driver, but even though they need symbols from
1283 * libglapi, in some version of Mesa they are not linked to it. Since
1284 * only the gl-renderer module links to it, the call above won't make
1285 * these symbols globally available, and loading the DRI driver fails.
1286 * Workaround this by dlopen()'ing libglapi with RTLD_GLOBAL. */
1287 dlopen("libglapi.so.0", RTLD_LAZY | RTLD_GLOBAL);
1289 ec->gbm = gbm_create_device(ec->drm.fd);
1294 format = ec->format;
1295 if (gl_renderer->create(&ec->base, ec->gbm,
1296 gl_renderer->opaque_attribs, &format) < 0) {
1297 gbm_device_destroy(ec->gbm);
1305 init_pixman(struct drm_compositor *ec)
1307 return pixman_renderer_init(&ec->base);
1310 static struct drm_mode *
1311 drm_output_add_mode(struct drm_output *output, drmModeModeInfo *info)
1313 struct drm_mode *mode;
1316 mode = malloc(sizeof *mode);
1320 mode->base.flags = 0;
1321 mode->base.width = info->hdisplay;
1322 mode->base.height = info->vdisplay;
1324 /* Calculate higher precision (mHz) refresh rate */
1325 refresh = (info->clock * 1000000LL / info->htotal +
1326 info->vtotal / 2) / info->vtotal;
1328 if (info->flags & DRM_MODE_FLAG_INTERLACE)
1330 if (info->flags & DRM_MODE_FLAG_DBLSCAN)
1332 if (info->vscan > 1)
1333 refresh /= info->vscan;
1335 mode->base.refresh = refresh;
1336 mode->mode_info = *info;
1338 if (info->type & DRM_MODE_TYPE_PREFERRED)
1339 mode->base.flags |= WL_OUTPUT_MODE_PREFERRED;
1341 wl_list_insert(output->base.mode_list.prev, &mode->base.link);
1347 drm_subpixel_to_wayland(int drm_value)
1349 switch (drm_value) {
1351 case DRM_MODE_SUBPIXEL_UNKNOWN:
1352 return WL_OUTPUT_SUBPIXEL_UNKNOWN;
1353 case DRM_MODE_SUBPIXEL_NONE:
1354 return WL_OUTPUT_SUBPIXEL_NONE;
1355 case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB:
1356 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB;
1357 case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR:
1358 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR;
1359 case DRM_MODE_SUBPIXEL_VERTICAL_RGB:
1360 return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB;
1361 case DRM_MODE_SUBPIXEL_VERTICAL_BGR:
1362 return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR;
1366 /* returns a value between 0-255 range, where higher is brighter */
1368 drm_get_backlight(struct drm_output *output)
1370 long brightness, max_brightness, norm;
1372 brightness = backlight_get_brightness(output->backlight);
1373 max_brightness = backlight_get_max_brightness(output->backlight);
1375 /* convert it on a scale of 0 to 255 */
1376 norm = (brightness * 255)/(max_brightness);
1378 return (uint32_t) norm;
1381 /* values accepted are between 0-255 range */
1383 drm_set_backlight(struct weston_output *output_base, uint32_t value)
1385 struct drm_output *output = (struct drm_output *) output_base;
1386 long max_brightness, new_brightness;
1388 if (!output->backlight)
1394 max_brightness = backlight_get_max_brightness(output->backlight);
1396 /* get denormalized value */
1397 new_brightness = (value * max_brightness) / 255;
1399 backlight_set_brightness(output->backlight, new_brightness);
1402 static drmModePropertyPtr
1403 drm_get_prop(int fd, drmModeConnectorPtr connector, const char *name)
1405 drmModePropertyPtr props;
1408 for (i = 0; i < connector->count_props; i++) {
1409 props = drmModeGetProperty(fd, connector->props[i]);
1413 if (!strcmp(props->name, name))
1416 drmModeFreeProperty(props);
1423 drm_set_dpms(struct weston_output *output_base, enum dpms_enum level)
1425 struct drm_output *output = (struct drm_output *) output_base;
1426 struct weston_compositor *ec = output_base->compositor;
1427 struct drm_compositor *c = (struct drm_compositor *) ec;
1429 if (!output->dpms_prop)
1432 drmModeConnectorSetProperty(c->drm.fd, output->connector_id,
1433 output->dpms_prop->prop_id, level);
1436 static const char *connector_type_names[] = {
1455 find_crtc_for_connector(struct drm_compositor *ec,
1456 drmModeRes *resources, drmModeConnector *connector)
1458 drmModeEncoder *encoder;
1459 uint32_t possible_crtcs;
1462 for (j = 0; j < connector->count_encoders; j++) {
1463 encoder = drmModeGetEncoder(ec->drm.fd, connector->encoders[j]);
1464 if (encoder == NULL) {
1465 weston_log("Failed to get encoder.\n");
1468 possible_crtcs = encoder->possible_crtcs;
1469 drmModeFreeEncoder(encoder);
1471 for (i = 0; i < resources->count_crtcs; i++) {
1472 if (possible_crtcs & (1 << i) &&
1473 !(ec->crtc_allocator & (1 << resources->crtcs[i])))
1481 /* Init output state that depends on gl or gbm */
1483 drm_output_init_egl(struct drm_output *output, struct drm_compositor *ec)
1487 output->surface = gbm_surface_create(ec->gbm,
1488 output->base.current_mode->width,
1489 output->base.current_mode->height,
1491 GBM_BO_USE_SCANOUT |
1492 GBM_BO_USE_RENDERING);
1493 if (!output->surface) {
1494 weston_log("failed to create gbm surface\n");
1498 if (gl_renderer->output_create(&output->base, output->surface) < 0) {
1499 weston_log("failed to create gl renderer output state\n");
1500 gbm_surface_destroy(output->surface);
1504 flags = GBM_BO_USE_CURSOR_64X64 | GBM_BO_USE_WRITE;
1506 for (i = 0; i < 2; i++) {
1507 if (output->cursor_bo[i])
1510 output->cursor_bo[i] =
1511 gbm_bo_create(ec->gbm, 64, 64, GBM_FORMAT_ARGB8888,
1515 if (output->cursor_bo[0] == NULL || output->cursor_bo[1] == NULL) {
1516 weston_log("cursor buffers unavailable, using gl cursors\n");
1517 ec->cursors_are_broken = 1;
1524 drm_output_init_pixman(struct drm_output *output, struct drm_compositor *c)
1526 int w = output->base.current_mode->width;
1527 int h = output->base.current_mode->height;
1530 /* FIXME error checking */
1532 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
1533 output->dumb[i] = drm_fb_create_dumb(c, w, h);
1534 if (!output->dumb[i])
1538 pixman_image_create_bits(PIXMAN_x8r8g8b8, w, h,
1539 output->dumb[i]->map,
1540 output->dumb[i]->stride);
1541 if (!output->image[i])
1545 if (pixman_renderer_output_create(&output->base) < 0)
1548 pixman_region32_init_rect(&output->previous_damage,
1549 output->base.x, output->base.y, output->base.width, output->base.height);
1554 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
1555 if (output->dumb[i])
1556 drm_fb_destroy_dumb(output->dumb[i]);
1557 if (output->image[i])
1558 pixman_image_unref(output->image[i]);
1560 output->dumb[i] = NULL;
1561 output->image[i] = NULL;
1568 drm_output_fini_pixman(struct drm_output *output)
1572 pixman_renderer_output_destroy(&output->base);
1573 pixman_region32_fini(&output->previous_damage);
1575 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
1576 drm_fb_destroy_dumb(output->dumb[i]);
1577 pixman_image_unref(output->image[i]);
1578 output->dumb[i] = NULL;
1579 output->image[i] = NULL;
1584 edid_parse_string(const uint8_t *data, char text[])
1589 /* this is always 12 bytes, but we can't guarantee it's null
1590 * terminated or not junk. */
1591 strncpy(text, (const char *) data, 12);
1593 /* remove insane chars */
1594 for (i = 0; text[i] != '\0'; i++) {
1595 if (text[i] == '\n' ||
1602 /* ensure string is printable */
1603 for (i = 0; text[i] != '\0'; i++) {
1604 if (!isprint(text[i])) {
1610 /* if the string is random junk, ignore the string */
1615 #define EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING 0xfe
1616 #define EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME 0xfc
1617 #define EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER 0xff
1618 #define EDID_OFFSET_DATA_BLOCKS 0x36
1619 #define EDID_OFFSET_LAST_BLOCK 0x6c
1620 #define EDID_OFFSET_PNPID 0x08
1621 #define EDID_OFFSET_SERIAL 0x0c
1624 edid_parse(struct drm_edid *edid, const uint8_t *data, size_t length)
1627 uint32_t serial_number;
1632 if (data[0] != 0x00 || data[1] != 0xff)
1635 /* decode the PNP ID from three 5 bit words packed into 2 bytes
1640 edid->pnp_id[0] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x7c) / 4) - 1;
1641 edid->pnp_id[1] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x3) * 8) + ((data[EDID_OFFSET_PNPID + 1] & 0xe0) / 32) - 1;
1642 edid->pnp_id[2] = 'A' + (data[EDID_OFFSET_PNPID + 1] & 0x1f) - 1;
1643 edid->pnp_id[3] = '\0';
1645 /* maybe there isn't a ASCII serial number descriptor, so use this instead */
1646 serial_number = (uint32_t) data[EDID_OFFSET_SERIAL + 0];
1647 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 1] * 0x100;
1648 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 2] * 0x10000;
1649 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 3] * 0x1000000;
1650 if (serial_number > 0)
1651 sprintf(edid->serial_number, "%lu", (unsigned long) serial_number);
1653 /* parse EDID data */
1654 for (i = EDID_OFFSET_DATA_BLOCKS;
1655 i <= EDID_OFFSET_LAST_BLOCK;
1657 /* ignore pixel clock data */
1663 /* any useful blocks? */
1664 if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME) {
1665 edid_parse_string(&data[i+5],
1666 edid->monitor_name);
1667 } else if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER) {
1668 edid_parse_string(&data[i+5],
1669 edid->serial_number);
1670 } else if (data[i+3] == EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING) {
1671 edid_parse_string(&data[i+5],
1679 find_and_parse_output_edid(struct drm_compositor *ec,
1680 struct drm_output *output,
1681 drmModeConnector *connector)
1683 drmModePropertyBlobPtr edid_blob = NULL;
1684 drmModePropertyPtr property;
1688 for (i = 0; i < connector->count_props && !edid_blob; i++) {
1689 property = drmModeGetProperty(ec->drm.fd, connector->props[i]);
1692 if ((property->flags & DRM_MODE_PROP_BLOB) &&
1693 !strcmp(property->name, "EDID")) {
1694 edid_blob = drmModeGetPropertyBlob(ec->drm.fd,
1695 connector->prop_values[i]);
1697 drmModeFreeProperty(property);
1702 rc = edid_parse(&output->edid,
1706 weston_log("EDID data '%s', '%s', '%s'\n",
1707 output->edid.pnp_id,
1708 output->edid.monitor_name,
1709 output->edid.serial_number);
1710 if (output->edid.pnp_id[0] != '\0')
1711 output->base.make = output->edid.pnp_id;
1712 if (output->edid.monitor_name[0] != '\0')
1713 output->base.model = output->edid.monitor_name;
1714 if (output->edid.serial_number[0] != '\0')
1715 output->base.serial_number = output->edid.serial_number;
1717 drmModeFreePropertyBlob(edid_blob);
1723 parse_modeline(const char *s, drmModeModeInfo *mode)
1729 mode->type = DRM_MODE_TYPE_USERDEF;
1735 if (sscanf(s, "%f %hd %hd %hd %hd %hd %hd %hd %hd %15s %15s",
1744 &mode->vtotal, hsync, vsync) != 11)
1747 mode->clock = fclock * 1000;
1748 if (strcmp(hsync, "+hsync") == 0)
1749 mode->flags |= DRM_MODE_FLAG_PHSYNC;
1750 else if (strcmp(hsync, "-hsync") == 0)
1751 mode->flags |= DRM_MODE_FLAG_NHSYNC;
1755 if (strcmp(vsync, "+vsync") == 0)
1756 mode->flags |= DRM_MODE_FLAG_PVSYNC;
1757 else if (strcmp(vsync, "-vsync") == 0)
1758 mode->flags |= DRM_MODE_FLAG_NVSYNC;
1766 parse_transform(const char *transform, const char *output_name)
1768 static const struct { const char *name; uint32_t token; } names[] = {
1769 { "normal", WL_OUTPUT_TRANSFORM_NORMAL },
1770 { "90", WL_OUTPUT_TRANSFORM_90 },
1771 { "180", WL_OUTPUT_TRANSFORM_180 },
1772 { "270", WL_OUTPUT_TRANSFORM_270 },
1773 { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED },
1774 { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
1775 { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
1776 { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
1780 for (i = 0; i < ARRAY_LENGTH(names); i++)
1781 if (strcmp(names[i].name, transform) == 0)
1782 return names[i].token;
1784 weston_log("Invalid transform \"%s\" for output %s\n",
1785 transform, output_name);
1787 return WL_OUTPUT_TRANSFORM_NORMAL;
1791 setup_output_seat_constraint(struct drm_compositor *ec,
1792 struct weston_output *output,
1795 if (strcmp(s, "") != 0) {
1796 struct udev_seat *seat;
1798 seat = udev_seat_get_named(&ec->base, s);
1800 seat->base.output = output;
1802 if (seat && seat->base.pointer)
1803 weston_pointer_clamp(seat->base.pointer,
1804 &seat->base.pointer->x,
1805 &seat->base.pointer->y);
1810 create_output_for_connector(struct drm_compositor *ec,
1811 drmModeRes *resources,
1812 drmModeConnector *connector,
1813 int x, int y, struct udev_device *drm_device)
1815 struct drm_output *output;
1816 struct drm_mode *drm_mode, *next, *preferred, *current, *configured;
1817 struct weston_mode *m;
1818 struct weston_config_section *section;
1819 drmModeEncoder *encoder;
1820 drmModeModeInfo crtc_mode, modeline;
1822 int i, width, height, scale;
1824 const char *type_name;
1825 enum output_config config;
1828 i = find_crtc_for_connector(ec, resources, connector);
1830 weston_log("No usable crtc/encoder pair for connector.\n");
1834 output = zalloc(sizeof *output);
1838 output->base.subpixel = drm_subpixel_to_wayland(connector->subpixel);
1839 output->base.make = "unknown";
1840 output->base.model = "unknown";
1841 output->base.serial_number = "unknown";
1842 wl_list_init(&output->base.mode_list);
1844 if (connector->connector_type < ARRAY_LENGTH(connector_type_names))
1845 type_name = connector_type_names[connector->connector_type];
1847 type_name = "UNKNOWN";
1848 snprintf(name, 32, "%s%d", type_name, connector->connector_type_id);
1849 output->base.name = strdup(name);
1851 section = weston_config_get_section(ec->base.config, "output", "name",
1853 weston_config_section_get_string(section, "mode", &s, "preferred");
1854 if (strcmp(s, "off") == 0)
1855 config = OUTPUT_CONFIG_OFF;
1856 else if (strcmp(s, "preferred") == 0)
1857 config = OUTPUT_CONFIG_PREFERRED;
1858 else if (strcmp(s, "current") == 0)
1859 config = OUTPUT_CONFIG_CURRENT;
1860 else if (sscanf(s, "%dx%d", &width, &height) == 2)
1861 config = OUTPUT_CONFIG_MODE;
1862 else if (parse_modeline(s, &modeline) == 0)
1863 config = OUTPUT_CONFIG_MODELINE;
1865 weston_log("Invalid mode \"%s\" for output %s\n",
1866 s, output->base.name);
1867 config = OUTPUT_CONFIG_PREFERRED;
1871 weston_config_section_get_int(section, "scale", &scale, 1);
1872 weston_config_section_get_string(section, "transform", &s, "normal");
1873 transform = parse_transform(s, output->base.name);
1876 weston_config_section_get_string(section, "seat", &s, "");
1877 setup_output_seat_constraint(ec, &output->base, s);
1880 output->crtc_id = resources->crtcs[i];
1882 ec->crtc_allocator |= (1 << output->crtc_id);
1883 output->connector_id = connector->connector_id;
1884 ec->connector_allocator |= (1 << output->connector_id);
1886 output->original_crtc = drmModeGetCrtc(ec->drm.fd, output->crtc_id);
1887 output->dpms_prop = drm_get_prop(ec->drm.fd, connector, "DPMS");
1889 /* Get the current mode on the crtc that's currently driving
1890 * this connector. */
1891 encoder = drmModeGetEncoder(ec->drm.fd, connector->encoder_id);
1892 memset(&crtc_mode, 0, sizeof crtc_mode);
1893 if (encoder != NULL) {
1894 crtc = drmModeGetCrtc(ec->drm.fd, encoder->crtc_id);
1895 drmModeFreeEncoder(encoder);
1898 if (crtc->mode_valid)
1899 crtc_mode = crtc->mode;
1900 drmModeFreeCrtc(crtc);
1903 for (i = 0; i < connector->count_modes; i++) {
1904 drm_mode = drm_output_add_mode(output, &connector->modes[i]);
1909 if (config == OUTPUT_CONFIG_OFF) {
1910 weston_log("Disabling output %s\n", output->base.name);
1911 drmModeSetCrtc(ec->drm.fd, output->crtc_id,
1912 0, 0, 0, 0, 0, NULL);
1920 wl_list_for_each_reverse(drm_mode, &output->base.mode_list, base.link) {
1921 if (config == OUTPUT_CONFIG_MODE &&
1922 width == drm_mode->base.width &&
1923 height == drm_mode->base.height)
1924 configured = drm_mode;
1925 if (!memcmp(&crtc_mode, &drm_mode->mode_info, sizeof crtc_mode))
1927 if (drm_mode->base.flags & WL_OUTPUT_MODE_PREFERRED)
1928 preferred = drm_mode;
1931 if (config == OUTPUT_CONFIG_MODELINE) {
1932 configured = drm_output_add_mode(output, &modeline);
1937 if (current == NULL && crtc_mode.clock != 0) {
1938 current = drm_output_add_mode(output, &crtc_mode);
1943 if (config == OUTPUT_CONFIG_CURRENT)
1944 configured = current;
1946 if (option_current_mode && current)
1947 output->base.current_mode = ¤t->base;
1948 else if (configured)
1949 output->base.current_mode = &configured->base;
1951 output->base.current_mode = &preferred->base;
1953 output->base.current_mode = ¤t->base;
1955 if (output->base.current_mode == NULL) {
1956 weston_log("no available modes for %s\n", output->base.name);
1960 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
1962 weston_output_init(&output->base, &ec->base, x, y,
1963 connector->mmWidth, connector->mmHeight,
1966 if (ec->use_pixman) {
1967 if (drm_output_init_pixman(output, ec) < 0) {
1968 weston_log("Failed to init output pixman state\n");
1971 } else if (drm_output_init_egl(output, ec) < 0) {
1972 weston_log("Failed to init output gl state\n");
1976 output->backlight = backlight_init(drm_device,
1977 connector->connector_type);
1978 if (output->backlight) {
1979 weston_log("Initialized backlight, device %s\n",
1980 output->backlight->path);
1981 output->base.set_backlight = drm_set_backlight;
1982 output->base.backlight_current = drm_get_backlight(output);
1984 weston_log("Failed to initialize backlight\n");
1987 wl_list_insert(ec->base.output_list.prev, &output->base.link);
1989 find_and_parse_output_edid(ec, output, connector);
1990 if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS)
1991 output->base.connection_internal = 1;
1993 output->base.start_repaint_loop = drm_output_start_repaint_loop;
1994 output->base.repaint = drm_output_repaint;
1995 output->base.destroy = drm_output_destroy;
1996 output->base.assign_planes = drm_assign_planes;
1997 output->base.set_dpms = drm_set_dpms;
1998 output->base.switch_mode = drm_output_switch_mode;
2000 output->base.gamma_size = output->original_crtc->gamma_size;
2001 output->base.set_gamma = drm_output_set_gamma;
2003 weston_plane_init(&output->cursor_plane, 0, 0);
2004 weston_plane_init(&output->fb_plane, 0, 0);
2006 weston_compositor_stack_plane(&ec->base, &output->cursor_plane, NULL);
2007 weston_compositor_stack_plane(&ec->base, &output->fb_plane,
2008 &ec->base.primary_plane);
2010 weston_log("Output %s, (connector %d, crtc %d)\n",
2011 output->base.name, output->connector_id, output->crtc_id);
2012 wl_list_for_each(m, &output->base.mode_list, link)
2013 weston_log_continue(" mode %dx%d@%.1f%s%s%s\n",
2014 m->width, m->height, m->refresh / 1000.0,
2015 m->flags & WL_OUTPUT_MODE_PREFERRED ?
2017 m->flags & WL_OUTPUT_MODE_CURRENT ?
2019 connector->count_modes == 0 ?
2025 weston_output_destroy(&output->base);
2027 wl_list_for_each_safe(drm_mode, next, &output->base.mode_list,
2029 wl_list_remove(&drm_mode->base.link);
2033 drmModeFreeCrtc(output->original_crtc);
2034 ec->crtc_allocator &= ~(1 << output->crtc_id);
2035 ec->connector_allocator &= ~(1 << output->connector_id);
2042 create_sprites(struct drm_compositor *ec)
2044 struct drm_sprite *sprite;
2045 drmModePlaneRes *plane_res;
2046 drmModePlane *plane;
2049 plane_res = drmModeGetPlaneResources(ec->drm.fd);
2051 weston_log("failed to get plane resources: %s\n",
2056 for (i = 0; i < plane_res->count_planes; i++) {
2057 plane = drmModeGetPlane(ec->drm.fd, plane_res->planes[i]);
2061 sprite = zalloc(sizeof(*sprite) + ((sizeof(uint32_t)) *
2062 plane->count_formats));
2064 weston_log("%s: out of memory\n",
2070 sprite->possible_crtcs = plane->possible_crtcs;
2071 sprite->plane_id = plane->plane_id;
2072 sprite->current = NULL;
2073 sprite->next = NULL;
2074 sprite->compositor = ec;
2075 sprite->count_formats = plane->count_formats;
2076 memcpy(sprite->formats, plane->formats,
2077 plane->count_formats * sizeof(plane->formats[0]));
2078 drmModeFreePlane(plane);
2079 weston_plane_init(&sprite->plane, 0, 0);
2080 weston_compositor_stack_plane(&ec->base, &sprite->plane,
2081 &ec->base.primary_plane);
2083 wl_list_insert(&ec->sprite_list, &sprite->link);
2086 drmModeFreePlaneResources(plane_res);
2090 destroy_sprites(struct drm_compositor *compositor)
2092 struct drm_sprite *sprite, *next;
2093 struct drm_output *output;
2095 output = container_of(compositor->base.output_list.next,
2096 struct drm_output, base.link);
2098 wl_list_for_each_safe(sprite, next, &compositor->sprite_list, link) {
2099 drmModeSetPlane(compositor->drm.fd,
2101 output->crtc_id, 0, 0,
2102 0, 0, 0, 0, 0, 0, 0, 0);
2103 drm_output_release_fb(output, sprite->current);
2104 drm_output_release_fb(output, sprite->next);
2105 weston_plane_release(&sprite->plane);
2111 create_outputs(struct drm_compositor *ec, uint32_t option_connector,
2112 struct udev_device *drm_device)
2114 drmModeConnector *connector;
2115 drmModeRes *resources;
2119 resources = drmModeGetResources(ec->drm.fd);
2121 weston_log("drmModeGetResources failed\n");
2125 ec->crtcs = calloc(resources->count_crtcs, sizeof(uint32_t));
2127 drmModeFreeResources(resources);
2131 ec->min_width = resources->min_width;
2132 ec->max_width = resources->max_width;
2133 ec->min_height = resources->min_height;
2134 ec->max_height = resources->max_height;
2136 ec->num_crtcs = resources->count_crtcs;
2137 memcpy(ec->crtcs, resources->crtcs, sizeof(uint32_t) * ec->num_crtcs);
2139 for (i = 0; i < resources->count_connectors; i++) {
2140 connector = drmModeGetConnector(ec->drm.fd,
2141 resources->connectors[i]);
2142 if (connector == NULL)
2145 if (connector->connection == DRM_MODE_CONNECTED &&
2146 (option_connector == 0 ||
2147 connector->connector_id == option_connector)) {
2148 if (create_output_for_connector(ec, resources,
2151 drmModeFreeConnector(connector);
2155 x += container_of(ec->base.output_list.prev,
2156 struct weston_output,
2160 drmModeFreeConnector(connector);
2163 if (wl_list_empty(&ec->base.output_list)) {
2164 weston_log("No currently active connector found.\n");
2165 drmModeFreeResources(resources);
2169 drmModeFreeResources(resources);
2175 update_outputs(struct drm_compositor *ec, struct udev_device *drm_device)
2177 drmModeConnector *connector;
2178 drmModeRes *resources;
2179 struct drm_output *output, *next;
2181 int x_offset = 0, y_offset = 0;
2182 uint32_t connected = 0, disconnects = 0;
2185 resources = drmModeGetResources(ec->drm.fd);
2187 weston_log("drmModeGetResources failed\n");
2191 /* collect new connects */
2192 for (i = 0; i < resources->count_connectors; i++) {
2193 int connector_id = resources->connectors[i];
2195 connector = drmModeGetConnector(ec->drm.fd, connector_id);
2196 if (connector == NULL)
2199 if (connector->connection != DRM_MODE_CONNECTED) {
2200 drmModeFreeConnector(connector);
2204 connected |= (1 << connector_id);
2206 if (!(ec->connector_allocator & (1 << connector_id))) {
2207 struct weston_output *last =
2208 container_of(ec->base.output_list.prev,
2209 struct weston_output, link);
2211 /* XXX: not yet needed, we die with 0 outputs */
2212 if (!wl_list_empty(&ec->base.output_list))
2213 x = last->x + last->width;
2217 create_output_for_connector(ec, resources,
2220 weston_log("connector %d connected\n", connector_id);
2223 drmModeFreeConnector(connector);
2225 drmModeFreeResources(resources);
2227 disconnects = ec->connector_allocator & ~connected;
2229 wl_list_for_each_safe(output, next, &ec->base.output_list,
2231 if (x_offset != 0 || y_offset != 0) {
2232 weston_output_move(&output->base,
2233 output->base.x - x_offset,
2234 output->base.y - y_offset);
2237 if (disconnects & (1 << output->connector_id)) {
2238 disconnects &= ~(1 << output->connector_id);
2239 weston_log("connector %d disconnected\n",
2240 output->connector_id);
2241 x_offset += output->base.width;
2242 drm_output_destroy(&output->base);
2247 /* FIXME: handle zero outputs, without terminating */
2248 if (ec->connector_allocator == 0)
2249 wl_display_terminate(ec->base.wl_display);
2253 udev_event_is_hotplug(struct drm_compositor *ec, struct udev_device *device)
2258 sysnum = udev_device_get_sysnum(device);
2259 if (!sysnum || atoi(sysnum) != ec->drm.id)
2262 val = udev_device_get_property_value(device, "HOTPLUG");
2266 return strcmp(val, "1") == 0;
2270 udev_drm_event(int fd, uint32_t mask, void *data)
2272 struct drm_compositor *ec = data;
2273 struct udev_device *event;
2275 event = udev_monitor_receive_device(ec->udev_monitor);
2277 if (udev_event_is_hotplug(ec, event))
2278 update_outputs(ec, event);
2280 udev_device_unref(event);
2286 drm_restore(struct weston_compositor *ec)
2288 weston_launcher_restore(ec->launcher);
2292 drm_destroy(struct weston_compositor *ec)
2294 struct drm_compositor *d = (struct drm_compositor *) ec;
2296 udev_input_destroy(&d->input);
2298 wl_event_source_remove(d->udev_drm_source);
2299 wl_event_source_remove(d->drm_source);
2303 weston_compositor_shutdown(ec);
2305 ec->renderer->destroy(ec);
2308 gbm_device_destroy(d->gbm);
2310 weston_launcher_destroy(d->base.launcher);
2318 drm_compositor_set_modes(struct drm_compositor *compositor)
2320 struct drm_output *output;
2321 struct drm_mode *drm_mode;
2324 wl_list_for_each(output, &compositor->base.output_list, base.link) {
2325 if (!output->current) {
2326 /* If something that would cause the output to
2327 * switch mode happened while in another vt, we
2328 * might not have a current drm_fb. In that case,
2329 * schedule a repaint and let drm_output_repaint
2330 * handle setting the mode. */
2331 weston_output_schedule_repaint(&output->base);
2335 drm_mode = (struct drm_mode *) output->base.current_mode;
2336 ret = drmModeSetCrtc(compositor->drm.fd, output->crtc_id,
2337 output->current->fb_id, 0, 0,
2338 &output->connector_id, 1,
2339 &drm_mode->mode_info);
2342 "failed to set mode %dx%d for output at %d,%d: %m\n",
2343 drm_mode->base.width, drm_mode->base.height,
2344 output->base.x, output->base.y);
2350 session_notify(struct wl_listener *listener, void *data)
2352 struct weston_compositor *compositor = data;
2353 struct drm_compositor *ec = data;
2354 struct drm_sprite *sprite;
2355 struct drm_output *output;
2357 if (ec->base.session_active) {
2358 weston_log("activating session\n");
2359 compositor->focus = 1;
2360 if (ec->base.launcher == NULL && drmSetMaster(ec->drm.fd)) {
2361 weston_log("failed to set master: %m\n");
2362 wl_display_terminate(compositor->wl_display);
2364 compositor->state = ec->prev_state;
2365 drm_compositor_set_modes(ec);
2366 weston_compositor_damage_all(compositor);
2367 udev_input_enable(&ec->input, ec->udev);
2369 weston_log("deactivating session\n");
2370 udev_input_disable(&ec->input);
2372 compositor->focus = 0;
2373 ec->prev_state = compositor->state;
2374 weston_compositor_offscreen(compositor);
2376 /* If we have a repaint scheduled (either from a
2377 * pending pageflip or the idle handler), make sure we
2378 * cancel that so we don't try to pageflip when we're
2379 * vt switched away. The OFFSCREEN state will prevent
2380 * further attemps at repainting. When we switch
2381 * back, we schedule a repaint, which will process
2382 * pending frame callbacks. */
2384 wl_list_for_each(output, &ec->base.output_list, base.link) {
2385 output->base.repaint_needed = 0;
2386 drmModeSetCursor(ec->drm.fd, output->crtc_id, 0, 0, 0);
2389 output = container_of(ec->base.output_list.next,
2390 struct drm_output, base.link);
2392 wl_list_for_each(sprite, &ec->sprite_list, link)
2393 drmModeSetPlane(ec->drm.fd,
2395 output->crtc_id, 0, 0,
2396 0, 0, 0, 0, 0, 0, 0, 0);
2398 if (ec->base.launcher == NULL && drmDropMaster(ec->drm.fd) < 0)
2399 weston_log("failed to drop master: %m\n");
2404 switch_vt_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
2406 struct weston_compositor *compositor = data;
2408 weston_launcher_activate_vt(compositor->launcher, key - KEY_F1 + 1);
2413 * Some systems may have multiple DRM devices attached to a single seat. This
2414 * function loops over all devices and tries to find a PCI device with the
2415 * boot_vga sysfs attribute set to 1.
2416 * If no such device is found, the first DRM device reported by udev is used.
2418 static struct udev_device*
2419 find_primary_gpu(struct drm_compositor *ec, const char *seat)
2421 struct udev_enumerate *e;
2422 struct udev_list_entry *entry;
2423 const char *path, *device_seat, *id;
2424 struct udev_device *device, *drm_device, *pci;
2426 e = udev_enumerate_new(ec->udev);
2427 udev_enumerate_add_match_subsystem(e, "drm");
2428 udev_enumerate_add_match_sysname(e, "card[0-9]*");
2430 udev_enumerate_scan_devices(e);
2432 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
2433 path = udev_list_entry_get_name(entry);
2434 device = udev_device_new_from_syspath(ec->udev, path);
2437 device_seat = udev_device_get_property_value(device, "ID_SEAT");
2439 device_seat = default_seat;
2440 if (strcmp(device_seat, seat)) {
2441 udev_device_unref(device);
2445 pci = udev_device_get_parent_with_subsystem_devtype(device,
2448 id = udev_device_get_sysattr_value(pci, "boot_vga");
2449 if (id && !strcmp(id, "1")) {
2451 udev_device_unref(drm_device);
2452 drm_device = device;
2458 drm_device = device;
2460 udev_device_unref(device);
2463 udev_enumerate_unref(e);
2468 planes_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
2470 struct drm_compositor *c = data;
2474 c->cursors_are_broken ^= 1;
2477 c->sprites_are_broken ^= 1;
2480 c->sprites_hidden ^= 1;
2487 #ifdef BUILD_VAAPI_RECORDER
2489 recorder_frame_notify(struct wl_listener *listener, void *data)
2491 struct drm_output *output;
2492 struct drm_compositor *c;
2495 output = container_of(listener, struct drm_output,
2496 recorder_frame_listener);
2497 c = (struct drm_compositor *) output->base.compositor;
2499 if (!output->recorder)
2502 ret = drmPrimeHandleToFD(c->drm.fd, output->current->handle,
2505 weston_log("[libva recorder] "
2506 "failed to create prime fd for front buffer\n");
2510 vaapi_recorder_frame(output->recorder, fd, output->current->stride / 4);
2514 create_recorder(struct drm_compositor *c, int width, int height,
2515 const char *filename)
2520 fd = open(c->drm.filename, O_RDWR | O_CLOEXEC);
2524 drmGetMagic(fd, &magic);
2525 drmAuthMagic(c->drm.fd, magic);
2527 return vaapi_recorder_create(fd, width, height, filename);
2531 recorder_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
2534 struct drm_compositor *c = data;
2535 struct drm_output *output;
2538 output = container_of(c->base.output_list.next,
2539 struct drm_output, base.link);
2541 if (!output->recorder) {
2542 width = output->base.current_mode->width;
2543 height = output->base.current_mode->height;
2546 create_recorder(c, width, height, "capture.h264");
2547 if (!output->recorder) {
2548 weston_log("failed to create vaapi recorder\n");
2552 output->base.disable_planes++;
2554 output->recorder_frame_listener.notify = recorder_frame_notify;
2555 wl_signal_add(&output->base.frame_signal,
2556 &output->recorder_frame_listener);
2558 weston_output_schedule_repaint(&output->base);
2560 weston_log("[libva recorder] initialized\n");
2562 vaapi_recorder_destroy(output->recorder);
2563 output->recorder = NULL;
2565 output->base.disable_planes--;
2567 wl_list_remove(&output->recorder_frame_listener.link);
2568 weston_log("[libva recorder] done\n");
2573 recorder_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
2576 weston_log("Compiled without libva support\n");
2580 static struct weston_compositor *
2581 drm_compositor_create(struct wl_display *display,
2582 struct drm_parameters *param,
2583 int *argc, char *argv[],
2584 struct weston_config *config)
2586 struct drm_compositor *ec;
2587 struct weston_config_section *section;
2588 struct udev_device *drm_device;
2589 struct wl_event_loop *loop;
2594 weston_log("initializing drm backend\n");
2596 ec = zalloc(sizeof *ec);
2600 /* KMS support for sprites is not complete yet, so disable the
2601 * functionality for now. */
2602 ec->sprites_are_broken = 1;
2604 section = weston_config_get_section(config, "core", NULL, NULL);
2605 weston_config_section_get_string(section,
2606 "gbm-format", &s, "xrgb8888");
2607 if (strcmp(s, "xrgb8888") == 0)
2608 ec->format = GBM_FORMAT_XRGB8888;
2609 else if (strcmp(s, "rgb565") == 0)
2610 ec->format = GBM_FORMAT_RGB565;
2611 else if (strcmp(s, "xrgb2101010") == 0)
2612 ec->format = GBM_FORMAT_XRGB2101010;
2614 weston_log("fatal: unrecognized pixel format: %s\n", s);
2620 ec->use_pixman = param->use_pixman;
2622 if (weston_compositor_init(&ec->base, display, argc, argv,
2624 weston_log("%s failed\n", __func__);
2628 /* Check if we run drm-backend using weston-launch */
2629 ec->base.launcher = weston_launcher_connect(&ec->base, param->tty,
2631 if (ec->base.launcher == NULL) {
2632 weston_log("fatal: drm backend should be run "
2633 "using weston-launch binary or as root\n");
2634 goto err_compositor;
2637 ec->udev = udev_new();
2638 if (ec->udev == NULL) {
2639 weston_log("failed to initialize udev context\n");
2643 ec->base.wl_display = display;
2644 ec->session_listener.notify = session_notify;
2645 wl_signal_add(&ec->base.session_signal, &ec->session_listener);
2647 drm_device = find_primary_gpu(ec, param->seat_id);
2648 if (drm_device == NULL) {
2649 weston_log("no drm device found\n");
2652 path = udev_device_get_syspath(drm_device);
2654 if (init_drm(ec, drm_device) < 0) {
2655 weston_log("failed to initialize kms\n");
2659 if (ec->use_pixman) {
2660 if (init_pixman(ec) < 0) {
2661 weston_log("failed to initialize pixman renderer\n");
2665 if (init_egl(ec) < 0) {
2666 weston_log("failed to initialize egl\n");
2671 ec->base.destroy = drm_destroy;
2672 ec->base.restore = drm_restore;
2676 ec->prev_state = WESTON_COMPOSITOR_ACTIVE;
2678 for (key = KEY_F1; key < KEY_F9; key++)
2679 weston_compositor_add_key_binding(&ec->base, key,
2680 MODIFIER_CTRL | MODIFIER_ALT,
2681 switch_vt_binding, ec);
2683 wl_list_init(&ec->sprite_list);
2686 if (create_outputs(ec, param->connector, drm_device) < 0) {
2687 weston_log("failed to create output for %s\n", path);
2693 if (udev_input_init(&ec->input,
2694 &ec->base, ec->udev, param->seat_id) < 0) {
2695 weston_log("failed to create input devices\n");
2699 loop = wl_display_get_event_loop(ec->base.wl_display);
2701 wl_event_loop_add_fd(loop, ec->drm.fd,
2702 WL_EVENT_READABLE, on_drm_input, ec);
2704 ec->udev_monitor = udev_monitor_new_from_netlink(ec->udev, "udev");
2705 if (ec->udev_monitor == NULL) {
2706 weston_log("failed to intialize udev monitor\n");
2707 goto err_drm_source;
2709 udev_monitor_filter_add_match_subsystem_devtype(ec->udev_monitor,
2711 ec->udev_drm_source =
2712 wl_event_loop_add_fd(loop,
2713 udev_monitor_get_fd(ec->udev_monitor),
2714 WL_EVENT_READABLE, udev_drm_event, ec);
2716 if (udev_monitor_enable_receiving(ec->udev_monitor) < 0) {
2717 weston_log("failed to enable udev-monitor receiving\n");
2718 goto err_udev_monitor;
2721 udev_device_unref(drm_device);
2723 weston_compositor_add_debug_binding(&ec->base, KEY_O,
2724 planes_binding, ec);
2725 weston_compositor_add_debug_binding(&ec->base, KEY_C,
2726 planes_binding, ec);
2727 weston_compositor_add_debug_binding(&ec->base, KEY_V,
2728 planes_binding, ec);
2729 weston_compositor_add_debug_binding(&ec->base, KEY_Q,
2730 recorder_binding, ec);
2735 wl_event_source_remove(ec->udev_drm_source);
2736 udev_monitor_unref(ec->udev_monitor);
2738 wl_event_source_remove(ec->drm_source);
2739 udev_input_destroy(&ec->input);
2741 ec->base.renderer->destroy(&ec->base);
2742 gbm_device_destroy(ec->gbm);
2743 destroy_sprites(ec);
2745 udev_device_unref(drm_device);
2747 weston_launcher_destroy(ec->base.launcher);
2749 udev_unref(ec->udev);
2751 weston_compositor_shutdown(&ec->base);
2757 WL_EXPORT struct weston_compositor *
2758 backend_init(struct wl_display *display, int *argc, char *argv[],
2759 struct weston_config *config)
2761 struct drm_parameters param = { 0, };
2763 const struct weston_option drm_options[] = {
2764 { WESTON_OPTION_INTEGER, "connector", 0, ¶m.connector },
2765 { WESTON_OPTION_STRING, "seat", 0, ¶m.seat_id },
2766 { WESTON_OPTION_INTEGER, "tty", 0, ¶m.tty },
2767 { WESTON_OPTION_BOOLEAN, "current-mode", 0, &option_current_mode },
2768 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, ¶m.use_pixman },
2771 param.seat_id = default_seat;
2773 parse_options(drm_options, ARRAY_LENGTH(drm_options), argc, argv);
2775 return drm_compositor_create(display, ¶m, argc, argv, config);