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_view *cursor_view;
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,
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_view(struct weston_output *_output,
452 struct weston_view *ev)
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 = ev->surface->buffer_ref.buffer;
461 if (ev->geometry.x != output->base.x ||
462 ev->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 != ev->surface->buffer_viewport.transform ||
467 ev->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, ev->surface, 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 output->current->stride != output->next->stride) {
601 ret = drmModeSetCrtc(compositor->drm.fd, output->crtc_id,
602 output->next->fb_id, 0, 0,
603 &output->connector_id, 1,
606 weston_log("set mode failed: %m\n");
609 output_base->set_dpms(output_base, WESTON_DPMS_ON);
612 if (drmModePageFlip(compositor->drm.fd, output->crtc_id,
614 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
615 weston_log("queueing pageflip failed: %m\n");
619 output->page_flip_pending = 1;
621 drm_output_set_cursor(output);
624 * Now, update all the sprite surfaces
626 wl_list_for_each(s, &compositor->sprite_list, link) {
627 uint32_t flags = 0, fb_id = 0;
629 .request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT,
630 .request.sequence = 1,
633 if ((!s->current && !s->next) ||
634 !drm_sprite_crtc_supported(output_base, s->possible_crtcs))
637 if (s->next && !compositor->sprites_hidden)
638 fb_id = s->next->fb_id;
640 ret = drmModeSetPlane(compositor->drm.fd, s->plane_id,
641 output->crtc_id, fb_id, flags,
642 s->dest_x, s->dest_y,
643 s->dest_w, s->dest_h,
647 weston_log("setplane failed: %d: %s\n",
648 ret, strerror(errno));
650 if (output->pipe > 0)
651 vbl.request.type |= DRM_VBLANK_SECONDARY;
654 * Queue a vblank signal so we know when the surface
655 * becomes active on the display or has been replaced.
657 vbl.request.signal = (unsigned long)s;
658 ret = drmWaitVBlank(compositor->drm.fd, &vbl);
660 weston_log("vblank event request failed: %d: %s\n",
661 ret, strerror(errno));
665 output->vblank_pending = 1;
672 drm_output_release_fb(output, output->next);
680 drm_output_start_repaint_loop(struct weston_output *output_base)
682 struct drm_output *output = (struct drm_output *) output_base;
683 struct drm_compositor *compositor = (struct drm_compositor *)
684 output_base->compositor;
689 if (output->destroy_pending)
692 if (!output->current) {
693 /* We can't page flip if there's no mode set */
697 fb_id = output->current->fb_id;
699 if (drmModePageFlip(compositor->drm.fd, output->crtc_id, fb_id,
700 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
701 weston_log("queueing pageflip failed: %m\n");
708 /* if we cannot page-flip, immediately finish frame */
709 clock_gettime(compositor->clock, &ts);
710 msec = ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
711 weston_output_finish_frame(output_base, msec);
715 vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec,
718 struct drm_sprite *s = (struct drm_sprite *)data;
719 struct drm_output *output = s->output;
722 output->vblank_pending = 0;
724 drm_output_release_fb(output, s->current);
725 s->current = s->next;
728 if (!output->page_flip_pending) {
729 msecs = sec * 1000 + usec / 1000;
730 weston_output_finish_frame(&output->base, msecs);
735 drm_output_destroy(struct weston_output *output_base);
738 page_flip_handler(int fd, unsigned int frame,
739 unsigned int sec, unsigned int usec, void *data)
741 struct drm_output *output = (struct drm_output *) data;
744 /* We don't set page_flip_pending on start_repaint_loop, in that case
745 * we just want to page flip to the current buffer to get an accurate
747 if (output->page_flip_pending) {
748 drm_output_release_fb(output, output->current);
749 output->current = output->next;
753 output->page_flip_pending = 0;
755 if (output->destroy_pending)
756 drm_output_destroy(&output->base);
757 else if (!output->vblank_pending) {
758 msecs = sec * 1000 + usec / 1000;
759 weston_output_finish_frame(&output->base, msecs);
761 /* We can't call this from frame_notify, because the output's
762 * repaint needed flag is cleared just after that */
763 if (output->recorder)
764 weston_output_schedule_repaint(&output->base);
769 drm_output_check_sprite_format(struct drm_sprite *s,
770 struct weston_view *ev, struct gbm_bo *bo)
774 format = gbm_bo_get_format(bo);
776 if (format == GBM_FORMAT_ARGB8888) {
779 pixman_region32_init_rect(&r, 0, 0,
781 ev->geometry.height);
782 pixman_region32_subtract(&r, &r, &ev->surface->opaque);
784 if (!pixman_region32_not_empty(&r))
785 format = GBM_FORMAT_XRGB8888;
787 pixman_region32_fini(&r);
790 for (i = 0; i < s->count_formats; i++)
791 if (s->formats[i] == format)
798 drm_view_transform_supported(struct weston_view *ev)
800 return !ev->transform.enabled ||
801 (ev->transform.matrix.type < WESTON_MATRIX_TRANSFORM_ROTATE);
804 static struct weston_plane *
805 drm_output_prepare_overlay_view(struct weston_output *output_base,
806 struct weston_view *ev)
808 struct weston_compositor *ec = output_base->compositor;
809 struct drm_compositor *c =(struct drm_compositor *) ec;
810 struct drm_sprite *s;
813 pixman_region32_t dest_rect, src_rect;
814 pixman_box32_t *box, tbox;
816 wl_fixed_t sx1, sy1, sx2, sy2;
821 if (ev->surface->buffer_viewport.transform != output_base->transform)
824 if (ev->surface->buffer_viewport.scale != output_base->current_scale)
827 if (c->sprites_are_broken)
830 if (ev->output_mask != (1u << output_base->id))
833 if (ev->surface->buffer_ref.buffer == NULL)
836 if (ev->alpha != 1.0f)
839 if (wl_shm_buffer_get(ev->surface->buffer_ref.buffer->resource))
842 if (!drm_view_transform_supported(ev))
845 wl_list_for_each(s, &c->sprite_list, link) {
846 if (!drm_sprite_crtc_supported(output_base, s->possible_crtcs))
855 /* No sprites available */
859 bo = gbm_bo_import(c->gbm, GBM_BO_IMPORT_WL_BUFFER,
860 ev->surface->buffer_ref.buffer->resource,
865 format = drm_output_check_sprite_format(s, ev, bo);
871 s->next = drm_fb_get_from_bo(bo, c, format);
877 drm_fb_set_buffer(s->next, ev->surface->buffer_ref.buffer);
879 box = pixman_region32_extents(&ev->transform.boundingbox);
880 s->plane.x = box->x1;
881 s->plane.y = box->y1;
884 * Calculate the source & dest rects properly based on actual
885 * position (note the caller has called weston_surface_update_transform()
888 pixman_region32_init(&dest_rect);
889 pixman_region32_intersect(&dest_rect, &ev->transform.boundingbox,
890 &output_base->region);
891 pixman_region32_translate(&dest_rect, -output_base->x, -output_base->y);
892 box = pixman_region32_extents(&dest_rect);
893 tbox = weston_transformed_rect(output_base->width,
895 output_base->transform,
896 output_base->current_scale,
900 s->dest_w = tbox.x2 - tbox.x1;
901 s->dest_h = tbox.y2 - tbox.y1;
902 pixman_region32_fini(&dest_rect);
904 pixman_region32_init(&src_rect);
905 pixman_region32_intersect(&src_rect, &ev->transform.boundingbox,
906 &output_base->region);
907 box = pixman_region32_extents(&src_rect);
909 weston_view_from_global_fixed(ev,
910 wl_fixed_from_int(box->x1),
911 wl_fixed_from_int(box->y1),
913 weston_view_from_global_fixed(ev,
914 wl_fixed_from_int(box->x2),
915 wl_fixed_from_int(box->y2),
922 if (sx2 > wl_fixed_from_int(ev->geometry.width))
923 sx2 = wl_fixed_from_int(ev->geometry.width);
924 if (sy2 > wl_fixed_from_int(ev->geometry.height))
925 sy2 = wl_fixed_from_int(ev->geometry.height);
932 tbox = weston_transformed_rect(wl_fixed_from_int(ev->geometry.width),
933 wl_fixed_from_int(ev->geometry.height),
934 ev->surface->buffer_viewport.transform,
935 ev->surface->buffer_viewport.scale,
938 s->src_x = tbox.x1 << 8;
939 s->src_y = tbox.y1 << 8;
940 s->src_w = (tbox.x2 - tbox.x1) << 8;
941 s->src_h = (tbox.y2 - tbox.y1) << 8;
942 pixman_region32_fini(&src_rect);
947 static struct weston_plane *
948 drm_output_prepare_cursor_view(struct weston_output *output_base,
949 struct weston_view *ev)
951 struct drm_compositor *c =
952 (struct drm_compositor *) output_base->compositor;
953 struct drm_output *output = (struct drm_output *) output_base;
957 if (output->base.transform != WL_OUTPUT_TRANSFORM_NORMAL)
959 if (output->cursor_view)
961 if (ev->output_mask != (1u << output_base->id))
963 if (c->cursors_are_broken)
965 if (ev->surface->buffer_ref.buffer == NULL ||
966 !wl_shm_buffer_get(ev->surface->buffer_ref.buffer->resource) ||
967 ev->geometry.width > 64 || ev->geometry.height > 64)
970 output->cursor_view = ev;
972 return &output->cursor_plane;
976 drm_output_set_cursor(struct drm_output *output)
978 struct weston_view *ev = output->cursor_view;
979 struct weston_buffer *buffer;
980 struct drm_compositor *c =
981 (struct drm_compositor *) output->base.compositor;
982 EGLint handle, stride;
984 uint32_t buf[64 * 64];
988 output->cursor_view = NULL;
990 drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0);
994 buffer = ev->surface->buffer_ref.buffer;
997 pixman_region32_not_empty(&output->cursor_plane.damage)) {
998 pixman_region32_fini(&output->cursor_plane.damage);
999 pixman_region32_init(&output->cursor_plane.damage);
1000 output->current_cursor ^= 1;
1001 bo = output->cursor_bo[output->current_cursor];
1002 memset(buf, 0, sizeof buf);
1003 stride = wl_shm_buffer_get_stride(buffer->shm_buffer);
1004 s = wl_shm_buffer_get_data(buffer->shm_buffer);
1005 wl_shm_buffer_begin_access(buffer->shm_buffer);
1006 for (i = 0; i < ev->geometry.height; i++)
1007 memcpy(buf + i * 64, s + i * stride,
1008 ev->geometry.width * 4);
1009 wl_shm_buffer_end_access(buffer->shm_buffer);
1011 if (gbm_bo_write(bo, buf, sizeof buf) < 0)
1012 weston_log("failed update cursor: %m\n");
1014 handle = gbm_bo_get_handle(bo).s32;
1015 if (drmModeSetCursor(c->drm.fd,
1016 output->crtc_id, handle, 64, 64)) {
1017 weston_log("failed to set cursor: %m\n");
1018 c->cursors_are_broken = 1;
1022 x = (ev->geometry.x - output->base.x) * output->base.current_scale;
1023 y = (ev->geometry.y - output->base.y) * output->base.current_scale;
1024 if (output->cursor_plane.x != x || output->cursor_plane.y != y) {
1025 if (drmModeMoveCursor(c->drm.fd, output->crtc_id, x, y)) {
1026 weston_log("failed to move cursor: %m\n");
1027 c->cursors_are_broken = 1;
1030 output->cursor_plane.x = x;
1031 output->cursor_plane.y = y;
1036 drm_assign_planes(struct weston_output *output)
1038 struct drm_compositor *c =
1039 (struct drm_compositor *) output->compositor;
1040 struct weston_view *ev, *next;
1041 pixman_region32_t overlap, surface_overlap;
1042 struct weston_plane *primary, *next_plane;
1045 * Find a surface for each sprite in the output using some heuristics:
1047 * 2) frequency of update
1048 * 3) opacity (though some hw might support alpha blending)
1049 * 4) clipping (this can be fixed with color keys)
1051 * The idea is to save on blitting since this should save power.
1052 * If we can get a large video surface on the sprite for example,
1053 * the main display surface may not need to update at all, and
1054 * the client buffer can be used directly for the sprite surface
1055 * as we do for flipping full screen surfaces.
1057 pixman_region32_init(&overlap);
1058 primary = &c->base.primary_plane;
1060 wl_list_for_each_safe(ev, next, &c->base.view_list, link) {
1061 struct weston_surface *es = ev->surface;
1063 /* Test whether this buffer can ever go into a plane:
1064 * non-shm, or small enough to be a cursor.
1066 * Also, keep a reference when using the pixman renderer.
1067 * That makes it possible to do a seamless switch to the GL
1068 * renderer and since the pixman renderer keeps a reference
1069 * to the buffer anyway, there is no side effects.
1071 if (c->use_pixman ||
1072 (es->buffer_ref.buffer &&
1073 (!wl_shm_buffer_get(es->buffer_ref.buffer->resource) ||
1074 (ev->geometry.width <= 64 && ev->geometry.height <= 64))))
1075 es->keep_buffer = 1;
1077 es->keep_buffer = 0;
1079 pixman_region32_init(&surface_overlap);
1080 pixman_region32_intersect(&surface_overlap, &overlap,
1081 &ev->transform.boundingbox);
1084 if (pixman_region32_not_empty(&surface_overlap))
1085 next_plane = primary;
1086 if (next_plane == NULL)
1087 next_plane = drm_output_prepare_cursor_view(output, ev);
1088 if (next_plane == NULL)
1089 next_plane = drm_output_prepare_scanout_view(output, ev);
1090 if (next_plane == NULL)
1091 next_plane = drm_output_prepare_overlay_view(output, ev);
1092 if (next_plane == NULL)
1093 next_plane = primary;
1094 weston_view_move_to_plane(ev, next_plane);
1095 if (next_plane == primary)
1096 pixman_region32_union(&overlap, &overlap,
1097 &ev->transform.boundingbox);
1099 pixman_region32_fini(&surface_overlap);
1101 pixman_region32_fini(&overlap);
1105 drm_output_fini_pixman(struct drm_output *output);
1108 drm_output_destroy(struct weston_output *output_base)
1110 struct drm_output *output = (struct drm_output *) output_base;
1111 struct drm_compositor *c =
1112 (struct drm_compositor *) output->base.compositor;
1113 drmModeCrtcPtr origcrtc = output->original_crtc;
1115 if (output->page_flip_pending) {
1116 output->destroy_pending = 1;
1117 weston_log("destroy output while page flip pending\n");
1121 if (output->backlight)
1122 backlight_destroy(output->backlight);
1124 drmModeFreeProperty(output->dpms_prop);
1126 /* Turn off hardware cursor */
1127 drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0);
1129 /* Restore original CRTC state */
1130 drmModeSetCrtc(c->drm.fd, origcrtc->crtc_id, origcrtc->buffer_id,
1131 origcrtc->x, origcrtc->y,
1132 &output->connector_id, 1, &origcrtc->mode);
1133 drmModeFreeCrtc(origcrtc);
1135 c->crtc_allocator &= ~(1 << output->crtc_id);
1136 c->connector_allocator &= ~(1 << output->connector_id);
1138 if (c->use_pixman) {
1139 drm_output_fini_pixman(output);
1141 gl_renderer->output_destroy(output_base);
1142 gbm_surface_destroy(output->surface);
1145 weston_plane_release(&output->fb_plane);
1146 weston_plane_release(&output->cursor_plane);
1148 weston_output_destroy(&output->base);
1149 wl_list_remove(&output->base.link);
1154 static struct drm_mode *
1155 choose_mode (struct drm_output *output, struct weston_mode *target_mode)
1157 struct drm_mode *tmp_mode = NULL, *mode;
1159 if (output->base.current_mode->width == target_mode->width &&
1160 output->base.current_mode->height == target_mode->height &&
1161 (output->base.current_mode->refresh == target_mode->refresh ||
1162 target_mode->refresh == 0))
1163 return (struct drm_mode *)output->base.current_mode;
1165 wl_list_for_each(mode, &output->base.mode_list, base.link) {
1166 if (mode->mode_info.hdisplay == target_mode->width &&
1167 mode->mode_info.vdisplay == target_mode->height) {
1168 if (mode->mode_info.vrefresh == target_mode->refresh ||
1169 target_mode->refresh == 0) {
1171 } else if (!tmp_mode)
1180 drm_output_init_egl(struct drm_output *output, struct drm_compositor *ec);
1182 drm_output_init_pixman(struct drm_output *output, struct drm_compositor *c);
1185 drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mode)
1187 struct drm_output *output;
1188 struct drm_mode *drm_mode;
1189 struct drm_compositor *ec;
1191 if (output_base == NULL) {
1192 weston_log("output is NULL.\n");
1197 weston_log("mode is NULL.\n");
1201 ec = (struct drm_compositor *)output_base->compositor;
1202 output = (struct drm_output *)output_base;
1203 drm_mode = choose_mode (output, mode);
1206 weston_log("%s, invalid resolution:%dx%d\n", __func__, mode->width, mode->height);
1210 if (&drm_mode->base == output->base.current_mode)
1213 output->base.current_mode->flags = 0;
1215 output->base.current_mode = &drm_mode->base;
1216 output->base.current_mode->flags =
1217 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
1219 /* reset rendering stuff. */
1220 drm_output_release_fb(output, output->current);
1221 drm_output_release_fb(output, output->next);
1222 output->current = output->next = NULL;
1224 if (ec->use_pixman) {
1225 drm_output_fini_pixman(output);
1226 if (drm_output_init_pixman(output, ec) < 0) {
1227 weston_log("failed to init output pixman state with "
1232 gl_renderer->output_destroy(&output->base);
1233 gbm_surface_destroy(output->surface);
1235 if (drm_output_init_egl(output, ec) < 0) {
1236 weston_log("failed to init output egl state with "
1246 on_drm_input(int fd, uint32_t mask, void *data)
1248 drmEventContext evctx;
1250 memset(&evctx, 0, sizeof evctx);
1251 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1252 evctx.page_flip_handler = page_flip_handler;
1253 evctx.vblank_handler = vblank_handler;
1254 drmHandleEvent(fd, &evctx);
1260 init_drm(struct drm_compositor *ec, struct udev_device *device)
1262 const char *filename, *sysnum;
1266 sysnum = udev_device_get_sysnum(device);
1268 ec->drm.id = atoi(sysnum);
1269 if (!sysnum || ec->drm.id < 0) {
1270 weston_log("cannot get device sysnum\n");
1274 filename = udev_device_get_devnode(device);
1275 fd = weston_launcher_open(ec->base.launcher, filename, O_RDWR);
1277 /* Probably permissions error */
1278 weston_log("couldn't open %s, skipping\n",
1279 udev_device_get_devnode(device));
1283 weston_log("using %s\n", filename);
1286 ec->drm.filename = strdup(filename);
1288 ret = drmGetCap(fd, DRM_CAP_TIMESTAMP_MONOTONIC, &cap);
1289 if (ret == 0 && cap == 1)
1290 ec->clock = CLOCK_MONOTONIC;
1292 ec->clock = CLOCK_REALTIME;
1297 static struct gbm_device *
1298 create_gbm_device(int fd)
1300 struct gbm_device *gbm;
1302 gl_renderer = weston_load_module("gl-renderer.so",
1303 "gl_renderer_interface");
1307 /* GBM will load a dri driver, but even though they need symbols from
1308 * libglapi, in some version of Mesa they are not linked to it. Since
1309 * only the gl-renderer module links to it, the call above won't make
1310 * these symbols globally available, and loading the DRI driver fails.
1311 * Workaround this by dlopen()'ing libglapi with RTLD_GLOBAL. */
1312 dlopen("libglapi.so.0", RTLD_LAZY | RTLD_GLOBAL);
1314 gbm = gbm_create_device(fd);
1320 drm_compositor_create_gl_renderer(struct drm_compositor *ec)
1324 format = ec->format;
1325 if (gl_renderer->create(&ec->base, ec->gbm,
1326 gl_renderer->opaque_attribs, &format) < 0) {
1334 init_egl(struct drm_compositor *ec)
1336 ec->gbm = create_gbm_device(ec->drm.fd);
1341 if (drm_compositor_create_gl_renderer(ec) < 0) {
1342 gbm_device_destroy(ec->gbm);
1350 init_pixman(struct drm_compositor *ec)
1352 return pixman_renderer_init(&ec->base);
1355 static struct drm_mode *
1356 drm_output_add_mode(struct drm_output *output, drmModeModeInfo *info)
1358 struct drm_mode *mode;
1361 mode = malloc(sizeof *mode);
1365 mode->base.flags = 0;
1366 mode->base.width = info->hdisplay;
1367 mode->base.height = info->vdisplay;
1369 /* Calculate higher precision (mHz) refresh rate */
1370 refresh = (info->clock * 1000000LL / info->htotal +
1371 info->vtotal / 2) / info->vtotal;
1373 if (info->flags & DRM_MODE_FLAG_INTERLACE)
1375 if (info->flags & DRM_MODE_FLAG_DBLSCAN)
1377 if (info->vscan > 1)
1378 refresh /= info->vscan;
1380 mode->base.refresh = refresh;
1381 mode->mode_info = *info;
1383 if (info->type & DRM_MODE_TYPE_PREFERRED)
1384 mode->base.flags |= WL_OUTPUT_MODE_PREFERRED;
1386 wl_list_insert(output->base.mode_list.prev, &mode->base.link);
1392 drm_subpixel_to_wayland(int drm_value)
1394 switch (drm_value) {
1396 case DRM_MODE_SUBPIXEL_UNKNOWN:
1397 return WL_OUTPUT_SUBPIXEL_UNKNOWN;
1398 case DRM_MODE_SUBPIXEL_NONE:
1399 return WL_OUTPUT_SUBPIXEL_NONE;
1400 case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB:
1401 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB;
1402 case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR:
1403 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR;
1404 case DRM_MODE_SUBPIXEL_VERTICAL_RGB:
1405 return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB;
1406 case DRM_MODE_SUBPIXEL_VERTICAL_BGR:
1407 return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR;
1411 /* returns a value between 0-255 range, where higher is brighter */
1413 drm_get_backlight(struct drm_output *output)
1415 long brightness, max_brightness, norm;
1417 brightness = backlight_get_brightness(output->backlight);
1418 max_brightness = backlight_get_max_brightness(output->backlight);
1420 /* convert it on a scale of 0 to 255 */
1421 norm = (brightness * 255)/(max_brightness);
1423 return (uint32_t) norm;
1426 /* values accepted are between 0-255 range */
1428 drm_set_backlight(struct weston_output *output_base, uint32_t value)
1430 struct drm_output *output = (struct drm_output *) output_base;
1431 long max_brightness, new_brightness;
1433 if (!output->backlight)
1439 max_brightness = backlight_get_max_brightness(output->backlight);
1441 /* get denormalized value */
1442 new_brightness = (value * max_brightness) / 255;
1444 backlight_set_brightness(output->backlight, new_brightness);
1447 static drmModePropertyPtr
1448 drm_get_prop(int fd, drmModeConnectorPtr connector, const char *name)
1450 drmModePropertyPtr props;
1453 for (i = 0; i < connector->count_props; i++) {
1454 props = drmModeGetProperty(fd, connector->props[i]);
1458 if (!strcmp(props->name, name))
1461 drmModeFreeProperty(props);
1468 drm_set_dpms(struct weston_output *output_base, enum dpms_enum level)
1470 struct drm_output *output = (struct drm_output *) output_base;
1471 struct weston_compositor *ec = output_base->compositor;
1472 struct drm_compositor *c = (struct drm_compositor *) ec;
1474 if (!output->dpms_prop)
1477 drmModeConnectorSetProperty(c->drm.fd, output->connector_id,
1478 output->dpms_prop->prop_id, level);
1481 static const char *connector_type_names[] = {
1500 find_crtc_for_connector(struct drm_compositor *ec,
1501 drmModeRes *resources, drmModeConnector *connector)
1503 drmModeEncoder *encoder;
1504 uint32_t possible_crtcs;
1507 for (j = 0; j < connector->count_encoders; j++) {
1508 encoder = drmModeGetEncoder(ec->drm.fd, connector->encoders[j]);
1509 if (encoder == NULL) {
1510 weston_log("Failed to get encoder.\n");
1513 possible_crtcs = encoder->possible_crtcs;
1514 drmModeFreeEncoder(encoder);
1516 for (i = 0; i < resources->count_crtcs; i++) {
1517 if (possible_crtcs & (1 << i) &&
1518 !(ec->crtc_allocator & (1 << resources->crtcs[i])))
1526 /* Init output state that depends on gl or gbm */
1528 drm_output_init_egl(struct drm_output *output, struct drm_compositor *ec)
1532 output->surface = gbm_surface_create(ec->gbm,
1533 output->base.current_mode->width,
1534 output->base.current_mode->height,
1536 GBM_BO_USE_SCANOUT |
1537 GBM_BO_USE_RENDERING);
1538 if (!output->surface) {
1539 weston_log("failed to create gbm surface\n");
1543 if (gl_renderer->output_create(&output->base, output->surface) < 0) {
1544 weston_log("failed to create gl renderer output state\n");
1545 gbm_surface_destroy(output->surface);
1549 flags = GBM_BO_USE_CURSOR_64X64 | GBM_BO_USE_WRITE;
1551 for (i = 0; i < 2; i++) {
1552 if (output->cursor_bo[i])
1555 output->cursor_bo[i] =
1556 gbm_bo_create(ec->gbm, 64, 64, GBM_FORMAT_ARGB8888,
1560 if (output->cursor_bo[0] == NULL || output->cursor_bo[1] == NULL) {
1561 weston_log("cursor buffers unavailable, using gl cursors\n");
1562 ec->cursors_are_broken = 1;
1569 drm_output_init_pixman(struct drm_output *output, struct drm_compositor *c)
1571 int w = output->base.current_mode->width;
1572 int h = output->base.current_mode->height;
1575 /* FIXME error checking */
1577 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
1578 output->dumb[i] = drm_fb_create_dumb(c, w, h);
1579 if (!output->dumb[i])
1583 pixman_image_create_bits(PIXMAN_x8r8g8b8, w, h,
1584 output->dumb[i]->map,
1585 output->dumb[i]->stride);
1586 if (!output->image[i])
1590 if (pixman_renderer_output_create(&output->base) < 0)
1593 pixman_region32_init_rect(&output->previous_damage,
1594 output->base.x, output->base.y, output->base.width, output->base.height);
1599 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
1600 if (output->dumb[i])
1601 drm_fb_destroy_dumb(output->dumb[i]);
1602 if (output->image[i])
1603 pixman_image_unref(output->image[i]);
1605 output->dumb[i] = NULL;
1606 output->image[i] = NULL;
1613 drm_output_fini_pixman(struct drm_output *output)
1617 pixman_renderer_output_destroy(&output->base);
1618 pixman_region32_fini(&output->previous_damage);
1620 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
1621 drm_fb_destroy_dumb(output->dumb[i]);
1622 pixman_image_unref(output->image[i]);
1623 output->dumb[i] = NULL;
1624 output->image[i] = NULL;
1629 edid_parse_string(const uint8_t *data, char text[])
1634 /* this is always 12 bytes, but we can't guarantee it's null
1635 * terminated or not junk. */
1636 strncpy(text, (const char *) data, 12);
1638 /* remove insane chars */
1639 for (i = 0; text[i] != '\0'; i++) {
1640 if (text[i] == '\n' ||
1647 /* ensure string is printable */
1648 for (i = 0; text[i] != '\0'; i++) {
1649 if (!isprint(text[i])) {
1655 /* if the string is random junk, ignore the string */
1660 #define EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING 0xfe
1661 #define EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME 0xfc
1662 #define EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER 0xff
1663 #define EDID_OFFSET_DATA_BLOCKS 0x36
1664 #define EDID_OFFSET_LAST_BLOCK 0x6c
1665 #define EDID_OFFSET_PNPID 0x08
1666 #define EDID_OFFSET_SERIAL 0x0c
1669 edid_parse(struct drm_edid *edid, const uint8_t *data, size_t length)
1672 uint32_t serial_number;
1677 if (data[0] != 0x00 || data[1] != 0xff)
1680 /* decode the PNP ID from three 5 bit words packed into 2 bytes
1685 edid->pnp_id[0] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x7c) / 4) - 1;
1686 edid->pnp_id[1] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x3) * 8) + ((data[EDID_OFFSET_PNPID + 1] & 0xe0) / 32) - 1;
1687 edid->pnp_id[2] = 'A' + (data[EDID_OFFSET_PNPID + 1] & 0x1f) - 1;
1688 edid->pnp_id[3] = '\0';
1690 /* maybe there isn't a ASCII serial number descriptor, so use this instead */
1691 serial_number = (uint32_t) data[EDID_OFFSET_SERIAL + 0];
1692 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 1] * 0x100;
1693 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 2] * 0x10000;
1694 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 3] * 0x1000000;
1695 if (serial_number > 0)
1696 sprintf(edid->serial_number, "%lu", (unsigned long) serial_number);
1698 /* parse EDID data */
1699 for (i = EDID_OFFSET_DATA_BLOCKS;
1700 i <= EDID_OFFSET_LAST_BLOCK;
1702 /* ignore pixel clock data */
1708 /* any useful blocks? */
1709 if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME) {
1710 edid_parse_string(&data[i+5],
1711 edid->monitor_name);
1712 } else if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER) {
1713 edid_parse_string(&data[i+5],
1714 edid->serial_number);
1715 } else if (data[i+3] == EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING) {
1716 edid_parse_string(&data[i+5],
1724 find_and_parse_output_edid(struct drm_compositor *ec,
1725 struct drm_output *output,
1726 drmModeConnector *connector)
1728 drmModePropertyBlobPtr edid_blob = NULL;
1729 drmModePropertyPtr property;
1733 for (i = 0; i < connector->count_props && !edid_blob; i++) {
1734 property = drmModeGetProperty(ec->drm.fd, connector->props[i]);
1737 if ((property->flags & DRM_MODE_PROP_BLOB) &&
1738 !strcmp(property->name, "EDID")) {
1739 edid_blob = drmModeGetPropertyBlob(ec->drm.fd,
1740 connector->prop_values[i]);
1742 drmModeFreeProperty(property);
1747 rc = edid_parse(&output->edid,
1751 weston_log("EDID data '%s', '%s', '%s'\n",
1752 output->edid.pnp_id,
1753 output->edid.monitor_name,
1754 output->edid.serial_number);
1755 if (output->edid.pnp_id[0] != '\0')
1756 output->base.make = output->edid.pnp_id;
1757 if (output->edid.monitor_name[0] != '\0')
1758 output->base.model = output->edid.monitor_name;
1759 if (output->edid.serial_number[0] != '\0')
1760 output->base.serial_number = output->edid.serial_number;
1762 drmModeFreePropertyBlob(edid_blob);
1768 parse_modeline(const char *s, drmModeModeInfo *mode)
1774 mode->type = DRM_MODE_TYPE_USERDEF;
1780 if (sscanf(s, "%f %hd %hd %hd %hd %hd %hd %hd %hd %15s %15s",
1789 &mode->vtotal, hsync, vsync) != 11)
1792 mode->clock = fclock * 1000;
1793 if (strcmp(hsync, "+hsync") == 0)
1794 mode->flags |= DRM_MODE_FLAG_PHSYNC;
1795 else if (strcmp(hsync, "-hsync") == 0)
1796 mode->flags |= DRM_MODE_FLAG_NHSYNC;
1800 if (strcmp(vsync, "+vsync") == 0)
1801 mode->flags |= DRM_MODE_FLAG_PVSYNC;
1802 else if (strcmp(vsync, "-vsync") == 0)
1803 mode->flags |= DRM_MODE_FLAG_NVSYNC;
1811 parse_transform(const char *transform, const char *output_name)
1813 static const struct { const char *name; uint32_t token; } names[] = {
1814 { "normal", WL_OUTPUT_TRANSFORM_NORMAL },
1815 { "90", WL_OUTPUT_TRANSFORM_90 },
1816 { "180", WL_OUTPUT_TRANSFORM_180 },
1817 { "270", WL_OUTPUT_TRANSFORM_270 },
1818 { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED },
1819 { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
1820 { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
1821 { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
1825 for (i = 0; i < ARRAY_LENGTH(names); i++)
1826 if (strcmp(names[i].name, transform) == 0)
1827 return names[i].token;
1829 weston_log("Invalid transform \"%s\" for output %s\n",
1830 transform, output_name);
1832 return WL_OUTPUT_TRANSFORM_NORMAL;
1836 setup_output_seat_constraint(struct drm_compositor *ec,
1837 struct weston_output *output,
1840 if (strcmp(s, "") != 0) {
1841 struct udev_seat *seat;
1843 seat = udev_seat_get_named(&ec->base, s);
1845 seat->base.output = output;
1847 if (seat && seat->base.pointer)
1848 weston_pointer_clamp(seat->base.pointer,
1849 &seat->base.pointer->x,
1850 &seat->base.pointer->y);
1855 create_output_for_connector(struct drm_compositor *ec,
1856 drmModeRes *resources,
1857 drmModeConnector *connector,
1858 int x, int y, struct udev_device *drm_device)
1860 struct drm_output *output;
1861 struct drm_mode *drm_mode, *next, *preferred, *current, *configured;
1862 struct weston_mode *m;
1863 struct weston_config_section *section;
1864 drmModeEncoder *encoder;
1865 drmModeModeInfo crtc_mode, modeline;
1867 int i, width, height, scale;
1869 const char *type_name;
1870 enum output_config config;
1873 i = find_crtc_for_connector(ec, resources, connector);
1875 weston_log("No usable crtc/encoder pair for connector.\n");
1879 output = zalloc(sizeof *output);
1883 output->base.subpixel = drm_subpixel_to_wayland(connector->subpixel);
1884 output->base.make = "unknown";
1885 output->base.model = "unknown";
1886 output->base.serial_number = "unknown";
1887 wl_list_init(&output->base.mode_list);
1889 if (connector->connector_type < ARRAY_LENGTH(connector_type_names))
1890 type_name = connector_type_names[connector->connector_type];
1892 type_name = "UNKNOWN";
1893 snprintf(name, 32, "%s%d", type_name, connector->connector_type_id);
1894 output->base.name = strdup(name);
1896 section = weston_config_get_section(ec->base.config, "output", "name",
1898 weston_config_section_get_string(section, "mode", &s, "preferred");
1899 if (strcmp(s, "off") == 0)
1900 config = OUTPUT_CONFIG_OFF;
1901 else if (strcmp(s, "preferred") == 0)
1902 config = OUTPUT_CONFIG_PREFERRED;
1903 else if (strcmp(s, "current") == 0)
1904 config = OUTPUT_CONFIG_CURRENT;
1905 else if (sscanf(s, "%dx%d", &width, &height) == 2)
1906 config = OUTPUT_CONFIG_MODE;
1907 else if (parse_modeline(s, &modeline) == 0)
1908 config = OUTPUT_CONFIG_MODELINE;
1910 weston_log("Invalid mode \"%s\" for output %s\n",
1911 s, output->base.name);
1912 config = OUTPUT_CONFIG_PREFERRED;
1916 weston_config_section_get_int(section, "scale", &scale, 1);
1917 weston_config_section_get_string(section, "transform", &s, "normal");
1918 transform = parse_transform(s, output->base.name);
1921 weston_config_section_get_string(section, "seat", &s, "");
1922 setup_output_seat_constraint(ec, &output->base, s);
1925 output->crtc_id = resources->crtcs[i];
1927 ec->crtc_allocator |= (1 << output->crtc_id);
1928 output->connector_id = connector->connector_id;
1929 ec->connector_allocator |= (1 << output->connector_id);
1931 output->original_crtc = drmModeGetCrtc(ec->drm.fd, output->crtc_id);
1932 output->dpms_prop = drm_get_prop(ec->drm.fd, connector, "DPMS");
1934 /* Get the current mode on the crtc that's currently driving
1935 * this connector. */
1936 encoder = drmModeGetEncoder(ec->drm.fd, connector->encoder_id);
1937 memset(&crtc_mode, 0, sizeof crtc_mode);
1938 if (encoder != NULL) {
1939 crtc = drmModeGetCrtc(ec->drm.fd, encoder->crtc_id);
1940 drmModeFreeEncoder(encoder);
1943 if (crtc->mode_valid)
1944 crtc_mode = crtc->mode;
1945 drmModeFreeCrtc(crtc);
1948 for (i = 0; i < connector->count_modes; i++) {
1949 drm_mode = drm_output_add_mode(output, &connector->modes[i]);
1954 if (config == OUTPUT_CONFIG_OFF) {
1955 weston_log("Disabling output %s\n", output->base.name);
1956 drmModeSetCrtc(ec->drm.fd, output->crtc_id,
1957 0, 0, 0, 0, 0, NULL);
1965 wl_list_for_each_reverse(drm_mode, &output->base.mode_list, base.link) {
1966 if (config == OUTPUT_CONFIG_MODE &&
1967 width == drm_mode->base.width &&
1968 height == drm_mode->base.height)
1969 configured = drm_mode;
1970 if (!memcmp(&crtc_mode, &drm_mode->mode_info, sizeof crtc_mode))
1972 if (drm_mode->base.flags & WL_OUTPUT_MODE_PREFERRED)
1973 preferred = drm_mode;
1976 if (config == OUTPUT_CONFIG_MODELINE) {
1977 configured = drm_output_add_mode(output, &modeline);
1982 if (current == NULL && crtc_mode.clock != 0) {
1983 current = drm_output_add_mode(output, &crtc_mode);
1988 if (config == OUTPUT_CONFIG_CURRENT)
1989 configured = current;
1991 if (option_current_mode && current)
1992 output->base.current_mode = ¤t->base;
1993 else if (configured)
1994 output->base.current_mode = &configured->base;
1996 output->base.current_mode = &preferred->base;
1998 output->base.current_mode = ¤t->base;
2000 if (output->base.current_mode == NULL) {
2001 weston_log("no available modes for %s\n", output->base.name);
2005 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
2007 weston_output_init(&output->base, &ec->base, x, y,
2008 connector->mmWidth, connector->mmHeight,
2011 if (ec->use_pixman) {
2012 if (drm_output_init_pixman(output, ec) < 0) {
2013 weston_log("Failed to init output pixman state\n");
2016 } else if (drm_output_init_egl(output, ec) < 0) {
2017 weston_log("Failed to init output gl state\n");
2021 output->backlight = backlight_init(drm_device,
2022 connector->connector_type);
2023 if (output->backlight) {
2024 weston_log("Initialized backlight, device %s\n",
2025 output->backlight->path);
2026 output->base.set_backlight = drm_set_backlight;
2027 output->base.backlight_current = drm_get_backlight(output);
2029 weston_log("Failed to initialize backlight\n");
2032 wl_list_insert(ec->base.output_list.prev, &output->base.link);
2034 find_and_parse_output_edid(ec, output, connector);
2035 if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS)
2036 output->base.connection_internal = 1;
2038 output->base.start_repaint_loop = drm_output_start_repaint_loop;
2039 output->base.repaint = drm_output_repaint;
2040 output->base.destroy = drm_output_destroy;
2041 output->base.assign_planes = drm_assign_planes;
2042 output->base.set_dpms = drm_set_dpms;
2043 output->base.switch_mode = drm_output_switch_mode;
2045 output->base.gamma_size = output->original_crtc->gamma_size;
2046 output->base.set_gamma = drm_output_set_gamma;
2048 weston_plane_init(&output->cursor_plane, &ec->base, 0, 0);
2049 weston_plane_init(&output->fb_plane, &ec->base, 0, 0);
2051 weston_compositor_stack_plane(&ec->base, &output->cursor_plane, NULL);
2052 weston_compositor_stack_plane(&ec->base, &output->fb_plane,
2053 &ec->base.primary_plane);
2055 weston_log("Output %s, (connector %d, crtc %d)\n",
2056 output->base.name, output->connector_id, output->crtc_id);
2057 wl_list_for_each(m, &output->base.mode_list, link)
2058 weston_log_continue(" mode %dx%d@%.1f%s%s%s\n",
2059 m->width, m->height, m->refresh / 1000.0,
2060 m->flags & WL_OUTPUT_MODE_PREFERRED ?
2062 m->flags & WL_OUTPUT_MODE_CURRENT ?
2064 connector->count_modes == 0 ?
2070 weston_output_destroy(&output->base);
2072 wl_list_for_each_safe(drm_mode, next, &output->base.mode_list,
2074 wl_list_remove(&drm_mode->base.link);
2078 drmModeFreeCrtc(output->original_crtc);
2079 ec->crtc_allocator &= ~(1 << output->crtc_id);
2080 ec->connector_allocator &= ~(1 << output->connector_id);
2087 create_sprites(struct drm_compositor *ec)
2089 struct drm_sprite *sprite;
2090 drmModePlaneRes *plane_res;
2091 drmModePlane *plane;
2094 plane_res = drmModeGetPlaneResources(ec->drm.fd);
2096 weston_log("failed to get plane resources: %s\n",
2101 for (i = 0; i < plane_res->count_planes; i++) {
2102 plane = drmModeGetPlane(ec->drm.fd, plane_res->planes[i]);
2106 sprite = zalloc(sizeof(*sprite) + ((sizeof(uint32_t)) *
2107 plane->count_formats));
2109 weston_log("%s: out of memory\n",
2115 sprite->possible_crtcs = plane->possible_crtcs;
2116 sprite->plane_id = plane->plane_id;
2117 sprite->current = NULL;
2118 sprite->next = NULL;
2119 sprite->compositor = ec;
2120 sprite->count_formats = plane->count_formats;
2121 memcpy(sprite->formats, plane->formats,
2122 plane->count_formats * sizeof(plane->formats[0]));
2123 drmModeFreePlane(plane);
2124 weston_plane_init(&sprite->plane, &ec->base, 0, 0);
2125 weston_compositor_stack_plane(&ec->base, &sprite->plane,
2126 &ec->base.primary_plane);
2128 wl_list_insert(&ec->sprite_list, &sprite->link);
2131 drmModeFreePlaneResources(plane_res);
2135 destroy_sprites(struct drm_compositor *compositor)
2137 struct drm_sprite *sprite, *next;
2138 struct drm_output *output;
2140 output = container_of(compositor->base.output_list.next,
2141 struct drm_output, base.link);
2143 wl_list_for_each_safe(sprite, next, &compositor->sprite_list, link) {
2144 drmModeSetPlane(compositor->drm.fd,
2146 output->crtc_id, 0, 0,
2147 0, 0, 0, 0, 0, 0, 0, 0);
2148 drm_output_release_fb(output, sprite->current);
2149 drm_output_release_fb(output, sprite->next);
2150 weston_plane_release(&sprite->plane);
2156 create_outputs(struct drm_compositor *ec, uint32_t option_connector,
2157 struct udev_device *drm_device)
2159 drmModeConnector *connector;
2160 drmModeRes *resources;
2164 resources = drmModeGetResources(ec->drm.fd);
2166 weston_log("drmModeGetResources failed\n");
2170 ec->crtcs = calloc(resources->count_crtcs, sizeof(uint32_t));
2172 drmModeFreeResources(resources);
2176 ec->min_width = resources->min_width;
2177 ec->max_width = resources->max_width;
2178 ec->min_height = resources->min_height;
2179 ec->max_height = resources->max_height;
2181 ec->num_crtcs = resources->count_crtcs;
2182 memcpy(ec->crtcs, resources->crtcs, sizeof(uint32_t) * ec->num_crtcs);
2184 for (i = 0; i < resources->count_connectors; i++) {
2185 connector = drmModeGetConnector(ec->drm.fd,
2186 resources->connectors[i]);
2187 if (connector == NULL)
2190 if (connector->connection == DRM_MODE_CONNECTED &&
2191 (option_connector == 0 ||
2192 connector->connector_id == option_connector)) {
2193 if (create_output_for_connector(ec, resources,
2196 drmModeFreeConnector(connector);
2200 x += container_of(ec->base.output_list.prev,
2201 struct weston_output,
2205 drmModeFreeConnector(connector);
2208 if (wl_list_empty(&ec->base.output_list)) {
2209 weston_log("No currently active connector found.\n");
2210 drmModeFreeResources(resources);
2214 drmModeFreeResources(resources);
2220 update_outputs(struct drm_compositor *ec, struct udev_device *drm_device)
2222 drmModeConnector *connector;
2223 drmModeRes *resources;
2224 struct drm_output *output, *next;
2226 int x_offset = 0, y_offset = 0;
2227 uint32_t connected = 0, disconnects = 0;
2230 resources = drmModeGetResources(ec->drm.fd);
2232 weston_log("drmModeGetResources failed\n");
2236 /* collect new connects */
2237 for (i = 0; i < resources->count_connectors; i++) {
2238 int connector_id = resources->connectors[i];
2240 connector = drmModeGetConnector(ec->drm.fd, connector_id);
2241 if (connector == NULL)
2244 if (connector->connection != DRM_MODE_CONNECTED) {
2245 drmModeFreeConnector(connector);
2249 connected |= (1 << connector_id);
2251 if (!(ec->connector_allocator & (1 << connector_id))) {
2252 struct weston_output *last =
2253 container_of(ec->base.output_list.prev,
2254 struct weston_output, link);
2256 /* XXX: not yet needed, we die with 0 outputs */
2257 if (!wl_list_empty(&ec->base.output_list))
2258 x = last->x + last->width;
2262 create_output_for_connector(ec, resources,
2265 weston_log("connector %d connected\n", connector_id);
2268 drmModeFreeConnector(connector);
2270 drmModeFreeResources(resources);
2272 disconnects = ec->connector_allocator & ~connected;
2274 wl_list_for_each_safe(output, next, &ec->base.output_list,
2276 if (x_offset != 0 || y_offset != 0) {
2277 weston_output_move(&output->base,
2278 output->base.x - x_offset,
2279 output->base.y - y_offset);
2282 if (disconnects & (1 << output->connector_id)) {
2283 disconnects &= ~(1 << output->connector_id);
2284 weston_log("connector %d disconnected\n",
2285 output->connector_id);
2286 x_offset += output->base.width;
2287 drm_output_destroy(&output->base);
2292 /* FIXME: handle zero outputs, without terminating */
2293 if (ec->connector_allocator == 0)
2294 wl_display_terminate(ec->base.wl_display);
2298 udev_event_is_hotplug(struct drm_compositor *ec, struct udev_device *device)
2303 sysnum = udev_device_get_sysnum(device);
2304 if (!sysnum || atoi(sysnum) != ec->drm.id)
2307 val = udev_device_get_property_value(device, "HOTPLUG");
2311 return strcmp(val, "1") == 0;
2315 udev_drm_event(int fd, uint32_t mask, void *data)
2317 struct drm_compositor *ec = data;
2318 struct udev_device *event;
2320 event = udev_monitor_receive_device(ec->udev_monitor);
2322 if (udev_event_is_hotplug(ec, event))
2323 update_outputs(ec, event);
2325 udev_device_unref(event);
2331 drm_restore(struct weston_compositor *ec)
2333 weston_launcher_restore(ec->launcher);
2337 drm_destroy(struct weston_compositor *ec)
2339 struct drm_compositor *d = (struct drm_compositor *) ec;
2341 udev_input_destroy(&d->input);
2343 wl_event_source_remove(d->udev_drm_source);
2344 wl_event_source_remove(d->drm_source);
2348 ec->renderer->destroy(ec);
2350 weston_compositor_shutdown(ec);
2353 gbm_device_destroy(d->gbm);
2355 weston_launcher_destroy(d->base.launcher);
2363 drm_compositor_set_modes(struct drm_compositor *compositor)
2365 struct drm_output *output;
2366 struct drm_mode *drm_mode;
2369 wl_list_for_each(output, &compositor->base.output_list, base.link) {
2370 if (!output->current) {
2371 /* If something that would cause the output to
2372 * switch mode happened while in another vt, we
2373 * might not have a current drm_fb. In that case,
2374 * schedule a repaint and let drm_output_repaint
2375 * handle setting the mode. */
2376 weston_output_schedule_repaint(&output->base);
2380 drm_mode = (struct drm_mode *) output->base.current_mode;
2381 ret = drmModeSetCrtc(compositor->drm.fd, output->crtc_id,
2382 output->current->fb_id, 0, 0,
2383 &output->connector_id, 1,
2384 &drm_mode->mode_info);
2387 "failed to set mode %dx%d for output at %d,%d: %m\n",
2388 drm_mode->base.width, drm_mode->base.height,
2389 output->base.x, output->base.y);
2395 session_notify(struct wl_listener *listener, void *data)
2397 struct weston_compositor *compositor = data;
2398 struct drm_compositor *ec = data;
2399 struct drm_sprite *sprite;
2400 struct drm_output *output;
2402 if (ec->base.session_active) {
2403 weston_log("activating session\n");
2404 compositor->state = ec->prev_state;
2405 drm_compositor_set_modes(ec);
2406 weston_compositor_damage_all(compositor);
2407 udev_input_enable(&ec->input, ec->udev);
2409 weston_log("deactivating session\n");
2410 udev_input_disable(&ec->input);
2412 ec->prev_state = compositor->state;
2413 weston_compositor_offscreen(compositor);
2415 /* If we have a repaint scheduled (either from a
2416 * pending pageflip or the idle handler), make sure we
2417 * cancel that so we don't try to pageflip when we're
2418 * vt switched away. The OFFSCREEN state will prevent
2419 * further attemps at repainting. When we switch
2420 * back, we schedule a repaint, which will process
2421 * pending frame callbacks. */
2423 wl_list_for_each(output, &ec->base.output_list, base.link) {
2424 output->base.repaint_needed = 0;
2425 drmModeSetCursor(ec->drm.fd, output->crtc_id, 0, 0, 0);
2428 output = container_of(ec->base.output_list.next,
2429 struct drm_output, base.link);
2431 wl_list_for_each(sprite, &ec->sprite_list, link)
2432 drmModeSetPlane(ec->drm.fd,
2434 output->crtc_id, 0, 0,
2435 0, 0, 0, 0, 0, 0, 0, 0);
2440 switch_vt_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
2442 struct weston_compositor *compositor = data;
2444 weston_launcher_activate_vt(compositor->launcher, key - KEY_F1 + 1);
2449 * Some systems may have multiple DRM devices attached to a single seat. This
2450 * function loops over all devices and tries to find a PCI device with the
2451 * boot_vga sysfs attribute set to 1.
2452 * If no such device is found, the first DRM device reported by udev is used.
2454 static struct udev_device*
2455 find_primary_gpu(struct drm_compositor *ec, const char *seat)
2457 struct udev_enumerate *e;
2458 struct udev_list_entry *entry;
2459 const char *path, *device_seat, *id;
2460 struct udev_device *device, *drm_device, *pci;
2462 e = udev_enumerate_new(ec->udev);
2463 udev_enumerate_add_match_subsystem(e, "drm");
2464 udev_enumerate_add_match_sysname(e, "card[0-9]*");
2466 udev_enumerate_scan_devices(e);
2468 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
2469 path = udev_list_entry_get_name(entry);
2470 device = udev_device_new_from_syspath(ec->udev, path);
2473 device_seat = udev_device_get_property_value(device, "ID_SEAT");
2475 device_seat = default_seat;
2476 if (strcmp(device_seat, seat)) {
2477 udev_device_unref(device);
2481 pci = udev_device_get_parent_with_subsystem_devtype(device,
2484 id = udev_device_get_sysattr_value(pci, "boot_vga");
2485 if (id && !strcmp(id, "1")) {
2487 udev_device_unref(drm_device);
2488 drm_device = device;
2494 drm_device = device;
2496 udev_device_unref(device);
2499 udev_enumerate_unref(e);
2504 planes_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
2506 struct drm_compositor *c = data;
2510 c->cursors_are_broken ^= 1;
2513 c->sprites_are_broken ^= 1;
2516 c->sprites_hidden ^= 1;
2523 #ifdef BUILD_VAAPI_RECORDER
2525 recorder_frame_notify(struct wl_listener *listener, void *data)
2527 struct drm_output *output;
2528 struct drm_compositor *c;
2531 output = container_of(listener, struct drm_output,
2532 recorder_frame_listener);
2533 c = (struct drm_compositor *) output->base.compositor;
2535 if (!output->recorder)
2538 ret = drmPrimeHandleToFD(c->drm.fd, output->current->handle,
2541 weston_log("[libva recorder] "
2542 "failed to create prime fd for front buffer\n");
2546 vaapi_recorder_frame(output->recorder, fd, output->current->stride / 4);
2550 create_recorder(struct drm_compositor *c, int width, int height,
2551 const char *filename)
2556 fd = open(c->drm.filename, O_RDWR | O_CLOEXEC);
2560 drmGetMagic(fd, &magic);
2561 drmAuthMagic(c->drm.fd, magic);
2563 return vaapi_recorder_create(fd, width, height, filename);
2567 recorder_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
2570 struct drm_compositor *c = data;
2571 struct drm_output *output;
2574 output = container_of(c->base.output_list.next,
2575 struct drm_output, base.link);
2577 if (!output->recorder) {
2578 width = output->base.current_mode->width;
2579 height = output->base.current_mode->height;
2582 create_recorder(c, width, height, "capture.h264");
2583 if (!output->recorder) {
2584 weston_log("failed to create vaapi recorder\n");
2588 output->base.disable_planes++;
2590 output->recorder_frame_listener.notify = recorder_frame_notify;
2591 wl_signal_add(&output->base.frame_signal,
2592 &output->recorder_frame_listener);
2594 weston_output_schedule_repaint(&output->base);
2596 weston_log("[libva recorder] initialized\n");
2598 vaapi_recorder_destroy(output->recorder);
2599 output->recorder = NULL;
2601 output->base.disable_planes--;
2603 wl_list_remove(&output->recorder_frame_listener.link);
2604 weston_log("[libva recorder] done\n");
2609 recorder_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
2612 weston_log("Compiled without libva support\n");
2617 switch_to_gl_renderer(struct drm_compositor *c)
2619 struct drm_output *output;
2624 weston_log("Switching to GL renderer\n");
2626 c->gbm = create_gbm_device(c->drm.fd);
2628 weston_log("Failed to create gbm device. "
2629 "Aborting renderer switch\n");
2633 wl_list_for_each(output, &c->base.output_list, base.link)
2634 pixman_renderer_output_destroy(&output->base);
2636 c->base.renderer->destroy(&c->base);
2638 if (drm_compositor_create_gl_renderer(c) < 0) {
2639 gbm_device_destroy(c->gbm);
2640 weston_log("Failed to create GL renderer. Quitting.\n");
2641 /* FIXME: we need a function to shutdown cleanly */
2645 wl_list_for_each(output, &c->base.output_list, base.link)
2646 drm_output_init_egl(output, c);
2652 renderer_switch_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
2655 struct drm_compositor *c = (struct drm_compositor *) seat->compositor;
2657 switch_to_gl_renderer(c);
2660 static struct weston_compositor *
2661 drm_compositor_create(struct wl_display *display,
2662 struct drm_parameters *param,
2663 int *argc, char *argv[],
2664 struct weston_config *config)
2666 struct drm_compositor *ec;
2667 struct weston_config_section *section;
2668 struct udev_device *drm_device;
2669 struct wl_event_loop *loop;
2674 weston_log("initializing drm backend\n");
2676 ec = zalloc(sizeof *ec);
2680 /* KMS support for sprites is not complete yet, so disable the
2681 * functionality for now. */
2682 ec->sprites_are_broken = 1;
2684 section = weston_config_get_section(config, "core", NULL, NULL);
2685 weston_config_section_get_string(section,
2686 "gbm-format", &s, "xrgb8888");
2687 if (strcmp(s, "xrgb8888") == 0)
2688 ec->format = GBM_FORMAT_XRGB8888;
2689 else if (strcmp(s, "rgb565") == 0)
2690 ec->format = GBM_FORMAT_RGB565;
2691 else if (strcmp(s, "xrgb2101010") == 0)
2692 ec->format = GBM_FORMAT_XRGB2101010;
2694 weston_log("fatal: unrecognized pixel format: %s\n", s);
2700 ec->use_pixman = param->use_pixman;
2702 if (weston_compositor_init(&ec->base, display, argc, argv,
2704 weston_log("%s failed\n", __func__);
2708 /* Check if we run drm-backend using weston-launch */
2709 ec->base.launcher = weston_launcher_connect(&ec->base, param->tty,
2711 if (ec->base.launcher == NULL) {
2712 weston_log("fatal: drm backend should be run "
2713 "using weston-launch binary or as root\n");
2714 goto err_compositor;
2717 ec->udev = udev_new();
2718 if (ec->udev == NULL) {
2719 weston_log("failed to initialize udev context\n");
2723 ec->base.wl_display = display;
2724 ec->session_listener.notify = session_notify;
2725 wl_signal_add(&ec->base.session_signal, &ec->session_listener);
2727 drm_device = find_primary_gpu(ec, param->seat_id);
2728 if (drm_device == NULL) {
2729 weston_log("no drm device found\n");
2732 path = udev_device_get_syspath(drm_device);
2734 if (init_drm(ec, drm_device) < 0) {
2735 weston_log("failed to initialize kms\n");
2739 if (ec->use_pixman) {
2740 if (init_pixman(ec) < 0) {
2741 weston_log("failed to initialize pixman renderer\n");
2745 if (init_egl(ec) < 0) {
2746 weston_log("failed to initialize egl\n");
2751 ec->base.destroy = drm_destroy;
2752 ec->base.restore = drm_restore;
2754 ec->prev_state = WESTON_COMPOSITOR_ACTIVE;
2756 for (key = KEY_F1; key < KEY_F9; key++)
2757 weston_compositor_add_key_binding(&ec->base, key,
2758 MODIFIER_CTRL | MODIFIER_ALT,
2759 switch_vt_binding, ec);
2761 wl_list_init(&ec->sprite_list);
2764 if (create_outputs(ec, param->connector, drm_device) < 0) {
2765 weston_log("failed to create output for %s\n", path);
2771 if (udev_input_init(&ec->input,
2772 &ec->base, ec->udev, param->seat_id) < 0) {
2773 weston_log("failed to create input devices\n");
2777 loop = wl_display_get_event_loop(ec->base.wl_display);
2779 wl_event_loop_add_fd(loop, ec->drm.fd,
2780 WL_EVENT_READABLE, on_drm_input, ec);
2782 ec->udev_monitor = udev_monitor_new_from_netlink(ec->udev, "udev");
2783 if (ec->udev_monitor == NULL) {
2784 weston_log("failed to intialize udev monitor\n");
2785 goto err_drm_source;
2787 udev_monitor_filter_add_match_subsystem_devtype(ec->udev_monitor,
2789 ec->udev_drm_source =
2790 wl_event_loop_add_fd(loop,
2791 udev_monitor_get_fd(ec->udev_monitor),
2792 WL_EVENT_READABLE, udev_drm_event, ec);
2794 if (udev_monitor_enable_receiving(ec->udev_monitor) < 0) {
2795 weston_log("failed to enable udev-monitor receiving\n");
2796 goto err_udev_monitor;
2799 udev_device_unref(drm_device);
2801 weston_compositor_add_debug_binding(&ec->base, KEY_O,
2802 planes_binding, ec);
2803 weston_compositor_add_debug_binding(&ec->base, KEY_C,
2804 planes_binding, ec);
2805 weston_compositor_add_debug_binding(&ec->base, KEY_V,
2806 planes_binding, ec);
2807 weston_compositor_add_debug_binding(&ec->base, KEY_Q,
2808 recorder_binding, ec);
2809 weston_compositor_add_debug_binding(&ec->base, KEY_W,
2810 renderer_switch_binding, ec);
2815 wl_event_source_remove(ec->udev_drm_source);
2816 udev_monitor_unref(ec->udev_monitor);
2818 wl_event_source_remove(ec->drm_source);
2819 udev_input_destroy(&ec->input);
2821 ec->base.renderer->destroy(&ec->base);
2822 gbm_device_destroy(ec->gbm);
2823 destroy_sprites(ec);
2825 udev_device_unref(drm_device);
2827 weston_launcher_destroy(ec->base.launcher);
2829 udev_unref(ec->udev);
2831 weston_compositor_shutdown(&ec->base);
2837 WL_EXPORT struct weston_compositor *
2838 backend_init(struct wl_display *display, int *argc, char *argv[],
2839 struct weston_config *config)
2841 struct drm_parameters param = { 0, };
2843 const struct weston_option drm_options[] = {
2844 { WESTON_OPTION_INTEGER, "connector", 0, ¶m.connector },
2845 { WESTON_OPTION_STRING, "seat", 0, ¶m.seat_id },
2846 { WESTON_OPTION_INTEGER, "tty", 0, ¶m.tty },
2847 { WESTON_OPTION_BOOLEAN, "current-mode", 0, &option_current_mode },
2848 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, ¶m.use_pixman },
2851 param.seat_id = default_seat;
2853 parse_options(drm_options, ARRAY_LENGTH(drm_options), argc, argv);
2855 return drm_compositor_create(display, ¶m, argc, argv, config);