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>
46 #include "libbacklight.h"
47 #include "compositor.h"
48 #include "gl-renderer.h"
49 #include "pixman-renderer.h"
50 #include "udev-input.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;
153 int page_flip_pending;
156 struct gbm_surface *surface;
157 struct gbm_bo *cursor_bo[2];
158 struct weston_plane cursor_plane;
159 struct weston_plane fb_plane;
160 struct weston_view *cursor_view;
162 struct drm_fb *current, *next;
163 struct backlight *backlight;
165 struct drm_fb *dumb[2];
166 pixman_image_t *image[2];
168 pixman_region32_t previous_damage;
170 struct vaapi_recorder *recorder;
171 struct wl_listener recorder_frame_listener;
175 * An output has a primary display plane plus zero or more sprites for
176 * blending display contents.
181 struct weston_plane plane;
183 struct drm_fb *current, *next;
184 struct drm_output *output;
185 struct drm_compositor *compositor;
187 uint32_t possible_crtcs;
189 uint32_t count_formats;
191 int32_t src_x, src_y;
192 uint32_t src_w, src_h;
193 uint32_t dest_x, dest_y;
194 uint32_t dest_w, dest_h;
199 struct drm_parameters {
206 static struct gl_renderer_interface *gl_renderer;
208 static const char default_seat[] = "seat0";
211 drm_output_set_cursor(struct drm_output *output);
214 drm_sprite_crtc_supported(struct weston_output *output_base, uint32_t supported)
216 struct weston_compositor *ec = output_base->compositor;
217 struct drm_compositor *c =(struct drm_compositor *) ec;
218 struct drm_output *output = (struct drm_output *) output_base;
221 for (crtc = 0; crtc < c->num_crtcs; crtc++) {
222 if (c->crtcs[crtc] != output->crtc_id)
225 if (supported & (1 << crtc))
233 drm_fb_destroy_callback(struct gbm_bo *bo, void *data)
235 struct drm_fb *fb = data;
236 struct gbm_device *gbm = gbm_bo_get_device(bo);
239 drmModeRmFB(gbm_device_get_fd(gbm), fb->fb_id);
241 weston_buffer_reference(&fb->buffer_ref, NULL);
246 static struct drm_fb *
247 drm_fb_create_dumb(struct drm_compositor *ec, unsigned width, unsigned height)
252 struct drm_mode_create_dumb create_arg;
253 struct drm_mode_destroy_dumb destroy_arg;
254 struct drm_mode_map_dumb map_arg;
256 fb = zalloc(sizeof *fb);
260 memset(&create_arg, 0, sizeof create_arg);
262 create_arg.width = width;
263 create_arg.height = height;
265 ret = drmIoctl(ec->drm.fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_arg);
269 fb->handle = create_arg.handle;
270 fb->stride = create_arg.pitch;
271 fb->size = create_arg.size;
274 ret = drmModeAddFB(ec->drm.fd, width, height, 24, 32,
275 fb->stride, fb->handle, &fb->fb_id);
279 memset(&map_arg, 0, sizeof map_arg);
280 map_arg.handle = fb->handle;
281 ret = drmIoctl(fb->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_arg);
285 fb->map = mmap(0, fb->size, PROT_WRITE,
286 MAP_SHARED, ec->drm.fd, map_arg.offset);
287 if (fb->map == MAP_FAILED)
293 drmModeRmFB(ec->drm.fd, fb->fb_id);
295 memset(&destroy_arg, 0, sizeof(destroy_arg));
296 destroy_arg.handle = create_arg.handle;
297 drmIoctl(ec->drm.fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
304 drm_fb_destroy_dumb(struct drm_fb *fb)
306 struct drm_mode_destroy_dumb destroy_arg;
312 drmModeRmFB(fb->fd, fb->fb_id);
314 weston_buffer_reference(&fb->buffer_ref, NULL);
316 munmap(fb->map, fb->size);
318 memset(&destroy_arg, 0, sizeof(destroy_arg));
319 destroy_arg.handle = fb->handle;
320 drmIoctl(fb->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
325 static struct drm_fb *
326 drm_fb_get_from_bo(struct gbm_bo *bo,
327 struct drm_compositor *compositor, uint32_t format)
329 struct drm_fb *fb = gbm_bo_get_user_data(bo);
330 uint32_t width, height;
331 uint32_t handles[4], pitches[4], offsets[4];
337 fb = calloc(1, sizeof *fb);
343 width = gbm_bo_get_width(bo);
344 height = gbm_bo_get_height(bo);
345 fb->stride = gbm_bo_get_stride(bo);
346 fb->handle = gbm_bo_get_handle(bo).u32;
347 fb->size = fb->stride * height;
348 fb->fd = compositor->drm.fd;
350 if (compositor->min_width > width || width > compositor->max_width ||
351 compositor->min_height > height ||
352 height > compositor->max_height) {
353 weston_log("bo geometry out of bounds\n");
359 if (format && !compositor->no_addfb2) {
360 handles[0] = fb->handle;
361 pitches[0] = fb->stride;
364 ret = drmModeAddFB2(compositor->drm.fd, width, height,
365 format, handles, pitches, offsets,
368 weston_log("addfb2 failed: %m\n");
369 compositor->no_addfb2 = 1;
370 compositor->sprites_are_broken = 1;
375 ret = drmModeAddFB(compositor->drm.fd, width, height, 24, 32,
376 fb->stride, fb->handle, &fb->fb_id);
379 weston_log("failed to create kms fb: %m\n");
383 gbm_bo_set_user_data(bo, fb, drm_fb_destroy_callback);
393 drm_fb_set_buffer(struct drm_fb *fb, struct weston_buffer *buffer)
395 assert(fb->buffer_ref.buffer == NULL);
397 fb->is_client_buffer = 1;
399 weston_buffer_reference(&fb->buffer_ref, buffer);
403 drm_output_release_fb(struct drm_output *output, struct drm_fb *fb)
409 (fb != output->dumb[0] && fb != output->dumb[1])) {
410 drm_fb_destroy_dumb(fb);
412 if (fb->is_client_buffer)
413 gbm_bo_destroy(fb->bo);
415 gbm_surface_release_buffer(output->surface,
421 drm_output_check_scanout_format(struct drm_output *output,
422 struct weston_surface *es, struct gbm_bo *bo)
427 format = gbm_bo_get_format(bo);
429 if (format == GBM_FORMAT_ARGB8888) {
430 /* We can scanout an ARGB buffer if the surface's
431 * opaque region covers the whole output, but we have
432 * to use XRGB as the KMS format code. */
433 pixman_region32_init_rect(&r, 0, 0,
435 output->base.height);
436 pixman_region32_subtract(&r, &r, &es->opaque);
438 if (!pixman_region32_not_empty(&r))
439 format = GBM_FORMAT_XRGB8888;
441 pixman_region32_fini(&r);
444 if (output->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;
458 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
462 if (ev->geometry.x != output->base.x ||
463 ev->geometry.y != output->base.y ||
464 buffer == NULL || c->gbm == NULL ||
465 buffer->width != output->base.current_mode->width ||
466 buffer->height != output->base.current_mode->height ||
467 output->base.transform != viewport->buffer.transform ||
468 ev->transform.enabled)
471 bo = gbm_bo_import(c->gbm, GBM_BO_IMPORT_WL_BUFFER,
472 buffer->resource, GBM_BO_USE_SCANOUT);
474 /* Unable to use the buffer for scanout */
478 format = drm_output_check_scanout_format(output, ev->surface, bo);
484 output->next = drm_fb_get_from_bo(bo, c, format);
490 drm_fb_set_buffer(output->next, buffer);
492 return &output->fb_plane;
496 drm_output_render_gl(struct drm_output *output, pixman_region32_t *damage)
498 struct drm_compositor *c =
499 (struct drm_compositor *) output->base.compositor;
502 c->base.renderer->repaint_output(&output->base, damage);
504 bo = gbm_surface_lock_front_buffer(output->surface);
506 weston_log("failed to lock front buffer: %m\n");
510 output->next = drm_fb_get_from_bo(bo, c, output->format);
512 weston_log("failed to get drm_fb for bo\n");
513 gbm_surface_release_buffer(output->surface, bo);
519 drm_output_render_pixman(struct drm_output *output, pixman_region32_t *damage)
521 struct weston_compositor *ec = output->base.compositor;
522 pixman_region32_t total_damage, previous_damage;
524 pixman_region32_init(&total_damage);
525 pixman_region32_init(&previous_damage);
527 pixman_region32_copy(&previous_damage, damage);
529 pixman_region32_union(&total_damage, damage, &output->previous_damage);
530 pixman_region32_copy(&output->previous_damage, &previous_damage);
532 output->current_image ^= 1;
534 output->next = output->dumb[output->current_image];
535 pixman_renderer_output_set_buffer(&output->base,
536 output->image[output->current_image]);
538 ec->renderer->repaint_output(&output->base, &total_damage);
540 pixman_region32_fini(&total_damage);
541 pixman_region32_fini(&previous_damage);
545 drm_output_render(struct drm_output *output, pixman_region32_t *damage)
547 struct drm_compositor *c =
548 (struct drm_compositor *) output->base.compositor;
551 drm_output_render_pixman(output, damage);
553 drm_output_render_gl(output, damage);
555 pixman_region32_subtract(&c->base.primary_plane.damage,
556 &c->base.primary_plane.damage, damage);
560 drm_output_set_gamma(struct weston_output *output_base,
561 uint16_t size, uint16_t *r, uint16_t *g, uint16_t *b)
564 struct drm_output *output = (struct drm_output *) output_base;
565 struct drm_compositor *compositor = (struct drm_compositor *) output->base.compositor;
568 if (output_base->gamma_size != size)
570 if (!output->original_crtc)
573 rc = drmModeCrtcSetGamma(compositor->drm.fd,
577 weston_log("set gamma failed: %m\n");
581 drm_output_repaint(struct weston_output *output_base,
582 pixman_region32_t *damage)
584 struct drm_output *output = (struct drm_output *) output_base;
585 struct drm_compositor *compositor =
586 (struct drm_compositor *) output->base.compositor;
587 struct drm_sprite *s;
588 struct drm_mode *mode;
591 if (output->destroy_pending)
595 drm_output_render(output, damage);
599 mode = container_of(output->base.current_mode, struct drm_mode, base);
600 if (!output->current ||
601 output->current->stride != output->next->stride) {
602 ret = drmModeSetCrtc(compositor->drm.fd, output->crtc_id,
603 output->next->fb_id, 0, 0,
604 &output->connector_id, 1,
607 weston_log("set mode failed: %m\n");
610 output_base->set_dpms(output_base, WESTON_DPMS_ON);
613 if (drmModePageFlip(compositor->drm.fd, output->crtc_id,
615 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
616 weston_log("queueing pageflip failed: %m\n");
620 output->page_flip_pending = 1;
622 drm_output_set_cursor(output);
625 * Now, update all the sprite surfaces
627 wl_list_for_each(s, &compositor->sprite_list, link) {
628 uint32_t flags = 0, fb_id = 0;
630 .request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT,
631 .request.sequence = 1,
634 if ((!s->current && !s->next) ||
635 !drm_sprite_crtc_supported(output_base, s->possible_crtcs))
638 if (s->next && !compositor->sprites_hidden)
639 fb_id = s->next->fb_id;
641 ret = drmModeSetPlane(compositor->drm.fd, s->plane_id,
642 output->crtc_id, fb_id, flags,
643 s->dest_x, s->dest_y,
644 s->dest_w, s->dest_h,
648 weston_log("setplane failed: %d: %s\n",
649 ret, strerror(errno));
651 if (output->pipe > 0)
652 vbl.request.type |= DRM_VBLANK_SECONDARY;
655 * Queue a vblank signal so we know when the surface
656 * becomes active on the display or has been replaced.
658 vbl.request.signal = (unsigned long)s;
659 ret = drmWaitVBlank(compositor->drm.fd, &vbl);
661 weston_log("vblank event request failed: %d: %s\n",
662 ret, strerror(errno));
666 output->vblank_pending = 1;
672 output->cursor_view = NULL;
674 drm_output_release_fb(output, output->next);
682 drm_output_start_repaint_loop(struct weston_output *output_base)
684 struct drm_output *output = (struct drm_output *) output_base;
685 struct drm_compositor *compositor = (struct drm_compositor *)
686 output_base->compositor;
691 if (output->destroy_pending)
694 if (!output->current) {
695 /* We can't page flip if there's no mode set */
699 fb_id = output->current->fb_id;
701 if (drmModePageFlip(compositor->drm.fd, output->crtc_id, fb_id,
702 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
703 weston_log("queueing pageflip failed: %m\n");
710 /* if we cannot page-flip, immediately finish frame */
711 clock_gettime(compositor->clock, &ts);
712 msec = ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
713 weston_output_finish_frame(output_base, msec);
717 vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec,
720 struct drm_sprite *s = (struct drm_sprite *)data;
721 struct drm_output *output = s->output;
724 output->vblank_pending = 0;
726 drm_output_release_fb(output, s->current);
727 s->current = s->next;
730 if (!output->page_flip_pending) {
731 msecs = sec * 1000 + usec / 1000;
732 weston_output_finish_frame(&output->base, msecs);
737 drm_output_destroy(struct weston_output *output_base);
740 page_flip_handler(int fd, unsigned int frame,
741 unsigned int sec, unsigned int usec, void *data)
743 struct drm_output *output = (struct drm_output *) data;
746 /* We don't set page_flip_pending on start_repaint_loop, in that case
747 * we just want to page flip to the current buffer to get an accurate
749 if (output->page_flip_pending) {
750 drm_output_release_fb(output, output->current);
751 output->current = output->next;
755 output->page_flip_pending = 0;
757 if (output->destroy_pending)
758 drm_output_destroy(&output->base);
759 else if (!output->vblank_pending) {
760 msecs = sec * 1000 + usec / 1000;
761 weston_output_finish_frame(&output->base, msecs);
763 /* We can't call this from frame_notify, because the output's
764 * repaint needed flag is cleared just after that */
765 if (output->recorder)
766 weston_output_schedule_repaint(&output->base);
771 drm_output_check_sprite_format(struct drm_sprite *s,
772 struct weston_view *ev, struct gbm_bo *bo)
776 format = gbm_bo_get_format(bo);
778 if (format == GBM_FORMAT_ARGB8888) {
781 pixman_region32_init_rect(&r, 0, 0,
783 ev->surface->height);
784 pixman_region32_subtract(&r, &r, &ev->surface->opaque);
786 if (!pixman_region32_not_empty(&r))
787 format = GBM_FORMAT_XRGB8888;
789 pixman_region32_fini(&r);
792 for (i = 0; i < s->count_formats; i++)
793 if (s->formats[i] == format)
800 drm_view_transform_supported(struct weston_view *ev)
802 return !ev->transform.enabled ||
803 (ev->transform.matrix.type < WESTON_MATRIX_TRANSFORM_ROTATE);
806 static struct weston_plane *
807 drm_output_prepare_overlay_view(struct weston_output *output_base,
808 struct weston_view *ev)
810 struct weston_compositor *ec = output_base->compositor;
811 struct drm_compositor *c =(struct drm_compositor *) ec;
812 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
813 struct drm_sprite *s;
816 pixman_region32_t dest_rect, src_rect;
817 pixman_box32_t *box, tbox;
819 wl_fixed_t sx1, sy1, sx2, sy2;
824 if (viewport->buffer.transform != output_base->transform)
827 if (viewport->buffer.scale != output_base->current_scale)
830 if (c->sprites_are_broken)
833 if (ev->output_mask != (1u << output_base->id))
836 if (ev->surface->buffer_ref.buffer == NULL)
839 if (ev->alpha != 1.0f)
842 if (wl_shm_buffer_get(ev->surface->buffer_ref.buffer->resource))
845 if (!drm_view_transform_supported(ev))
848 wl_list_for_each(s, &c->sprite_list, link) {
849 if (!drm_sprite_crtc_supported(output_base, s->possible_crtcs))
858 /* No sprites available */
862 bo = gbm_bo_import(c->gbm, GBM_BO_IMPORT_WL_BUFFER,
863 ev->surface->buffer_ref.buffer->resource,
868 format = drm_output_check_sprite_format(s, ev, bo);
874 s->next = drm_fb_get_from_bo(bo, c, format);
880 drm_fb_set_buffer(s->next, ev->surface->buffer_ref.buffer);
882 box = pixman_region32_extents(&ev->transform.boundingbox);
883 s->plane.x = box->x1;
884 s->plane.y = box->y1;
887 * Calculate the source & dest rects properly based on actual
888 * position (note the caller has called weston_surface_update_transform()
891 pixman_region32_init(&dest_rect);
892 pixman_region32_intersect(&dest_rect, &ev->transform.boundingbox,
893 &output_base->region);
894 pixman_region32_translate(&dest_rect, -output_base->x, -output_base->y);
895 box = pixman_region32_extents(&dest_rect);
896 tbox = weston_transformed_rect(output_base->width,
898 output_base->transform,
899 output_base->current_scale,
903 s->dest_w = tbox.x2 - tbox.x1;
904 s->dest_h = tbox.y2 - tbox.y1;
905 pixman_region32_fini(&dest_rect);
907 pixman_region32_init(&src_rect);
908 pixman_region32_intersect(&src_rect, &ev->transform.boundingbox,
909 &output_base->region);
910 box = pixman_region32_extents(&src_rect);
912 weston_view_from_global_fixed(ev,
913 wl_fixed_from_int(box->x1),
914 wl_fixed_from_int(box->y1),
916 weston_view_from_global_fixed(ev,
917 wl_fixed_from_int(box->x2),
918 wl_fixed_from_int(box->y2),
925 if (sx2 > wl_fixed_from_int(ev->surface->width))
926 sx2 = wl_fixed_from_int(ev->surface->width);
927 if (sy2 > wl_fixed_from_int(ev->surface->height))
928 sy2 = wl_fixed_from_int(ev->surface->height);
935 tbox = weston_transformed_rect(wl_fixed_from_int(ev->surface->width),
936 wl_fixed_from_int(ev->surface->height),
937 viewport->buffer.transform,
938 viewport->buffer.scale,
941 s->src_x = tbox.x1 << 8;
942 s->src_y = tbox.y1 << 8;
943 s->src_w = (tbox.x2 - tbox.x1) << 8;
944 s->src_h = (tbox.y2 - tbox.y1) << 8;
945 pixman_region32_fini(&src_rect);
950 static struct weston_plane *
951 drm_output_prepare_cursor_view(struct weston_output *output_base,
952 struct weston_view *ev)
954 struct drm_compositor *c =
955 (struct drm_compositor *) output_base->compositor;
956 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
957 struct drm_output *output = (struct drm_output *) output_base;
961 if (output->base.transform != WL_OUTPUT_TRANSFORM_NORMAL)
963 if (viewport->buffer.scale != output_base->current_scale)
965 if (output->cursor_view)
967 if (ev->output_mask != (1u << output_base->id))
969 if (c->cursors_are_broken)
971 if (ev->surface->buffer_ref.buffer == NULL ||
972 !wl_shm_buffer_get(ev->surface->buffer_ref.buffer->resource) ||
973 ev->surface->width > 64 || ev->surface->height > 64)
976 output->cursor_view = ev;
978 return &output->cursor_plane;
982 drm_output_set_cursor(struct drm_output *output)
984 struct weston_view *ev = output->cursor_view;
985 struct weston_buffer *buffer;
986 struct drm_compositor *c =
987 (struct drm_compositor *) output->base.compositor;
988 EGLint handle, stride;
990 uint32_t buf[64 * 64];
994 output->cursor_view = NULL;
996 drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0);
1000 buffer = ev->surface->buffer_ref.buffer;
1003 pixman_region32_not_empty(&output->cursor_plane.damage)) {
1004 pixman_region32_fini(&output->cursor_plane.damage);
1005 pixman_region32_init(&output->cursor_plane.damage);
1006 output->current_cursor ^= 1;
1007 bo = output->cursor_bo[output->current_cursor];
1008 memset(buf, 0, sizeof buf);
1009 stride = wl_shm_buffer_get_stride(buffer->shm_buffer);
1010 s = wl_shm_buffer_get_data(buffer->shm_buffer);
1011 wl_shm_buffer_begin_access(buffer->shm_buffer);
1012 for (i = 0; i < ev->surface->height; i++)
1013 memcpy(buf + i * 64, s + i * stride,
1014 ev->surface->width * 4);
1015 wl_shm_buffer_end_access(buffer->shm_buffer);
1017 if (gbm_bo_write(bo, buf, sizeof buf) < 0)
1018 weston_log("failed update cursor: %m\n");
1020 handle = gbm_bo_get_handle(bo).s32;
1021 if (drmModeSetCursor(c->drm.fd,
1022 output->crtc_id, handle, 64, 64)) {
1023 weston_log("failed to set cursor: %m\n");
1024 c->cursors_are_broken = 1;
1028 x = (ev->geometry.x - output->base.x) * output->base.current_scale;
1029 y = (ev->geometry.y - output->base.y) * output->base.current_scale;
1030 if (output->cursor_plane.x != x || output->cursor_plane.y != y) {
1031 if (drmModeMoveCursor(c->drm.fd, output->crtc_id, x, y)) {
1032 weston_log("failed to move cursor: %m\n");
1033 c->cursors_are_broken = 1;
1036 output->cursor_plane.x = x;
1037 output->cursor_plane.y = y;
1042 drm_assign_planes(struct weston_output *output)
1044 struct drm_compositor *c =
1045 (struct drm_compositor *) output->compositor;
1046 struct weston_view *ev, *next;
1047 pixman_region32_t overlap, surface_overlap;
1048 struct weston_plane *primary, *next_plane;
1051 * Find a surface for each sprite in the output using some heuristics:
1053 * 2) frequency of update
1054 * 3) opacity (though some hw might support alpha blending)
1055 * 4) clipping (this can be fixed with color keys)
1057 * The idea is to save on blitting since this should save power.
1058 * If we can get a large video surface on the sprite for example,
1059 * the main display surface may not need to update at all, and
1060 * the client buffer can be used directly for the sprite surface
1061 * as we do for flipping full screen surfaces.
1063 pixman_region32_init(&overlap);
1064 primary = &c->base.primary_plane;
1066 wl_list_for_each_safe(ev, next, &c->base.view_list, link) {
1067 struct weston_surface *es = ev->surface;
1069 /* Test whether this buffer can ever go into a plane:
1070 * non-shm, or small enough to be a cursor.
1072 * Also, keep a reference when using the pixman renderer.
1073 * That makes it possible to do a seamless switch to the GL
1074 * renderer and since the pixman renderer keeps a reference
1075 * to the buffer anyway, there is no side effects.
1077 if (c->use_pixman ||
1078 (es->buffer_ref.buffer &&
1079 (!wl_shm_buffer_get(es->buffer_ref.buffer->resource) ||
1080 (ev->surface->width <= 64 && ev->surface->height <= 64))))
1081 es->keep_buffer = 1;
1083 es->keep_buffer = 0;
1085 pixman_region32_init(&surface_overlap);
1086 pixman_region32_intersect(&surface_overlap, &overlap,
1087 &ev->transform.boundingbox);
1090 if (pixman_region32_not_empty(&surface_overlap))
1091 next_plane = primary;
1092 if (next_plane == NULL)
1093 next_plane = drm_output_prepare_cursor_view(output, ev);
1094 if (next_plane == NULL)
1095 next_plane = drm_output_prepare_scanout_view(output, ev);
1096 if (next_plane == NULL)
1097 next_plane = drm_output_prepare_overlay_view(output, ev);
1098 if (next_plane == NULL)
1099 next_plane = primary;
1100 weston_view_move_to_plane(ev, next_plane);
1101 if (next_plane == primary)
1102 pixman_region32_union(&overlap, &overlap,
1103 &ev->transform.boundingbox);
1105 pixman_region32_fini(&surface_overlap);
1107 pixman_region32_fini(&overlap);
1111 drm_output_fini_pixman(struct drm_output *output);
1114 drm_output_destroy(struct weston_output *output_base)
1116 struct drm_output *output = (struct drm_output *) output_base;
1117 struct drm_compositor *c =
1118 (struct drm_compositor *) output->base.compositor;
1119 drmModeCrtcPtr origcrtc = output->original_crtc;
1121 if (output->page_flip_pending) {
1122 output->destroy_pending = 1;
1123 weston_log("destroy output while page flip pending\n");
1127 if (output->backlight)
1128 backlight_destroy(output->backlight);
1130 drmModeFreeProperty(output->dpms_prop);
1132 /* Turn off hardware cursor */
1133 drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0);
1135 /* Restore original CRTC state */
1136 drmModeSetCrtc(c->drm.fd, origcrtc->crtc_id, origcrtc->buffer_id,
1137 origcrtc->x, origcrtc->y,
1138 &output->connector_id, 1, &origcrtc->mode);
1139 drmModeFreeCrtc(origcrtc);
1141 c->crtc_allocator &= ~(1 << output->crtc_id);
1142 c->connector_allocator &= ~(1 << output->connector_id);
1144 if (c->use_pixman) {
1145 drm_output_fini_pixman(output);
1147 gl_renderer->output_destroy(output_base);
1148 gbm_surface_destroy(output->surface);
1151 weston_plane_release(&output->fb_plane);
1152 weston_plane_release(&output->cursor_plane);
1154 weston_output_destroy(&output->base);
1159 static struct drm_mode *
1160 choose_mode (struct drm_output *output, struct weston_mode *target_mode)
1162 struct drm_mode *tmp_mode = NULL, *mode;
1164 if (output->base.current_mode->width == target_mode->width &&
1165 output->base.current_mode->height == target_mode->height &&
1166 (output->base.current_mode->refresh == target_mode->refresh ||
1167 target_mode->refresh == 0))
1168 return (struct drm_mode *)output->base.current_mode;
1170 wl_list_for_each(mode, &output->base.mode_list, base.link) {
1171 if (mode->mode_info.hdisplay == target_mode->width &&
1172 mode->mode_info.vdisplay == target_mode->height) {
1173 if (mode->mode_info.vrefresh == target_mode->refresh ||
1174 target_mode->refresh == 0) {
1176 } else if (!tmp_mode)
1185 drm_output_init_egl(struct drm_output *output, struct drm_compositor *ec);
1187 drm_output_init_pixman(struct drm_output *output, struct drm_compositor *c);
1190 drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mode)
1192 struct drm_output *output;
1193 struct drm_mode *drm_mode;
1194 struct drm_compositor *ec;
1196 if (output_base == NULL) {
1197 weston_log("output is NULL.\n");
1202 weston_log("mode is NULL.\n");
1206 ec = (struct drm_compositor *)output_base->compositor;
1207 output = (struct drm_output *)output_base;
1208 drm_mode = choose_mode (output, mode);
1211 weston_log("%s, invalid resolution:%dx%d\n", __func__, mode->width, mode->height);
1215 if (&drm_mode->base == output->base.current_mode)
1218 output->base.current_mode->flags = 0;
1220 output->base.current_mode = &drm_mode->base;
1221 output->base.current_mode->flags =
1222 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
1224 /* reset rendering stuff. */
1225 drm_output_release_fb(output, output->current);
1226 drm_output_release_fb(output, output->next);
1227 output->current = output->next = NULL;
1229 if (ec->use_pixman) {
1230 drm_output_fini_pixman(output);
1231 if (drm_output_init_pixman(output, ec) < 0) {
1232 weston_log("failed to init output pixman state with "
1237 gl_renderer->output_destroy(&output->base);
1238 gbm_surface_destroy(output->surface);
1240 if (drm_output_init_egl(output, ec) < 0) {
1241 weston_log("failed to init output egl state with "
1251 on_drm_input(int fd, uint32_t mask, void *data)
1253 drmEventContext evctx;
1255 memset(&evctx, 0, sizeof evctx);
1256 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1257 evctx.page_flip_handler = page_flip_handler;
1258 evctx.vblank_handler = vblank_handler;
1259 drmHandleEvent(fd, &evctx);
1265 init_drm(struct drm_compositor *ec, struct udev_device *device)
1267 const char *filename, *sysnum;
1271 sysnum = udev_device_get_sysnum(device);
1273 ec->drm.id = atoi(sysnum);
1274 if (!sysnum || ec->drm.id < 0) {
1275 weston_log("cannot get device sysnum\n");
1279 filename = udev_device_get_devnode(device);
1280 fd = weston_launcher_open(ec->base.launcher, filename, O_RDWR);
1282 /* Probably permissions error */
1283 weston_log("couldn't open %s, skipping\n",
1284 udev_device_get_devnode(device));
1288 weston_log("using %s\n", filename);
1291 ec->drm.filename = strdup(filename);
1293 ret = drmGetCap(fd, DRM_CAP_TIMESTAMP_MONOTONIC, &cap);
1294 if (ret == 0 && cap == 1)
1295 ec->clock = CLOCK_MONOTONIC;
1297 ec->clock = CLOCK_REALTIME;
1302 static struct gbm_device *
1303 create_gbm_device(int fd)
1305 struct gbm_device *gbm;
1307 gl_renderer = weston_load_module("gl-renderer.so",
1308 "gl_renderer_interface");
1312 /* GBM will load a dri driver, but even though they need symbols from
1313 * libglapi, in some version of Mesa they are not linked to it. Since
1314 * only the gl-renderer module links to it, the call above won't make
1315 * these symbols globally available, and loading the DRI driver fails.
1316 * Workaround this by dlopen()'ing libglapi with RTLD_GLOBAL. */
1317 dlopen("libglapi.so.0", RTLD_LAZY | RTLD_GLOBAL);
1319 gbm = gbm_create_device(fd);
1325 drm_compositor_create_gl_renderer(struct drm_compositor *ec)
1329 format = ec->format;
1330 if (gl_renderer->create(&ec->base, ec->gbm,
1331 gl_renderer->opaque_attribs, &format) < 0) {
1339 init_egl(struct drm_compositor *ec)
1341 ec->gbm = create_gbm_device(ec->drm.fd);
1346 if (drm_compositor_create_gl_renderer(ec) < 0) {
1347 gbm_device_destroy(ec->gbm);
1355 init_pixman(struct drm_compositor *ec)
1357 return pixman_renderer_init(&ec->base);
1360 static struct drm_mode *
1361 drm_output_add_mode(struct drm_output *output, drmModeModeInfo *info)
1363 struct drm_mode *mode;
1366 mode = malloc(sizeof *mode);
1370 mode->base.flags = 0;
1371 mode->base.width = info->hdisplay;
1372 mode->base.height = info->vdisplay;
1374 /* Calculate higher precision (mHz) refresh rate */
1375 refresh = (info->clock * 1000000LL / info->htotal +
1376 info->vtotal / 2) / info->vtotal;
1378 if (info->flags & DRM_MODE_FLAG_INTERLACE)
1380 if (info->flags & DRM_MODE_FLAG_DBLSCAN)
1382 if (info->vscan > 1)
1383 refresh /= info->vscan;
1385 mode->base.refresh = refresh;
1386 mode->mode_info = *info;
1388 if (info->type & DRM_MODE_TYPE_PREFERRED)
1389 mode->base.flags |= WL_OUTPUT_MODE_PREFERRED;
1391 wl_list_insert(output->base.mode_list.prev, &mode->base.link);
1397 drm_subpixel_to_wayland(int drm_value)
1399 switch (drm_value) {
1401 case DRM_MODE_SUBPIXEL_UNKNOWN:
1402 return WL_OUTPUT_SUBPIXEL_UNKNOWN;
1403 case DRM_MODE_SUBPIXEL_NONE:
1404 return WL_OUTPUT_SUBPIXEL_NONE;
1405 case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB:
1406 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB;
1407 case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR:
1408 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR;
1409 case DRM_MODE_SUBPIXEL_VERTICAL_RGB:
1410 return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB;
1411 case DRM_MODE_SUBPIXEL_VERTICAL_BGR:
1412 return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR;
1416 /* returns a value between 0-255 range, where higher is brighter */
1418 drm_get_backlight(struct drm_output *output)
1420 long brightness, max_brightness, norm;
1422 brightness = backlight_get_brightness(output->backlight);
1423 max_brightness = backlight_get_max_brightness(output->backlight);
1425 /* convert it on a scale of 0 to 255 */
1426 norm = (brightness * 255)/(max_brightness);
1428 return (uint32_t) norm;
1431 /* values accepted are between 0-255 range */
1433 drm_set_backlight(struct weston_output *output_base, uint32_t value)
1435 struct drm_output *output = (struct drm_output *) output_base;
1436 long max_brightness, new_brightness;
1438 if (!output->backlight)
1444 max_brightness = backlight_get_max_brightness(output->backlight);
1446 /* get denormalized value */
1447 new_brightness = (value * max_brightness) / 255;
1449 backlight_set_brightness(output->backlight, new_brightness);
1452 static drmModePropertyPtr
1453 drm_get_prop(int fd, drmModeConnectorPtr connector, const char *name)
1455 drmModePropertyPtr props;
1458 for (i = 0; i < connector->count_props; i++) {
1459 props = drmModeGetProperty(fd, connector->props[i]);
1463 if (!strcmp(props->name, name))
1466 drmModeFreeProperty(props);
1473 drm_set_dpms(struct weston_output *output_base, enum dpms_enum level)
1475 struct drm_output *output = (struct drm_output *) output_base;
1476 struct weston_compositor *ec = output_base->compositor;
1477 struct drm_compositor *c = (struct drm_compositor *) ec;
1479 if (!output->dpms_prop)
1482 drmModeConnectorSetProperty(c->drm.fd, output->connector_id,
1483 output->dpms_prop->prop_id, level);
1486 static const char *connector_type_names[] = {
1505 find_crtc_for_connector(struct drm_compositor *ec,
1506 drmModeRes *resources, drmModeConnector *connector)
1508 drmModeEncoder *encoder;
1509 uint32_t possible_crtcs;
1512 for (j = 0; j < connector->count_encoders; j++) {
1513 encoder = drmModeGetEncoder(ec->drm.fd, connector->encoders[j]);
1514 if (encoder == NULL) {
1515 weston_log("Failed to get encoder.\n");
1518 possible_crtcs = encoder->possible_crtcs;
1519 drmModeFreeEncoder(encoder);
1521 for (i = 0; i < resources->count_crtcs; i++) {
1522 if (possible_crtcs & (1 << i) &&
1523 !(ec->crtc_allocator & (1 << resources->crtcs[i])))
1531 /* Init output state that depends on gl or gbm */
1533 drm_output_init_egl(struct drm_output *output, struct drm_compositor *ec)
1535 EGLint format = output->format;
1538 output->surface = gbm_surface_create(ec->gbm,
1539 output->base.current_mode->width,
1540 output->base.current_mode->height,
1542 GBM_BO_USE_SCANOUT |
1543 GBM_BO_USE_RENDERING);
1544 if (!output->surface) {
1545 weston_log("failed to create gbm surface\n");
1549 if (gl_renderer->output_create(&output->base, output->surface,
1550 gl_renderer->opaque_attribs,
1552 weston_log("failed to create gl renderer output state\n");
1553 gbm_surface_destroy(output->surface);
1557 flags = GBM_BO_USE_CURSOR_64X64 | GBM_BO_USE_WRITE;
1559 for (i = 0; i < 2; i++) {
1560 if (output->cursor_bo[i])
1563 output->cursor_bo[i] =
1564 gbm_bo_create(ec->gbm, 64, 64, GBM_FORMAT_ARGB8888,
1568 if (output->cursor_bo[0] == NULL || output->cursor_bo[1] == NULL) {
1569 weston_log("cursor buffers unavailable, using gl cursors\n");
1570 ec->cursors_are_broken = 1;
1577 drm_output_init_pixman(struct drm_output *output, struct drm_compositor *c)
1579 int w = output->base.current_mode->width;
1580 int h = output->base.current_mode->height;
1583 /* FIXME error checking */
1585 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
1586 output->dumb[i] = drm_fb_create_dumb(c, w, h);
1587 if (!output->dumb[i])
1591 pixman_image_create_bits(PIXMAN_x8r8g8b8, w, h,
1592 output->dumb[i]->map,
1593 output->dumb[i]->stride);
1594 if (!output->image[i])
1598 if (pixman_renderer_output_create(&output->base) < 0)
1601 pixman_region32_init_rect(&output->previous_damage,
1602 output->base.x, output->base.y, output->base.width, output->base.height);
1607 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
1608 if (output->dumb[i])
1609 drm_fb_destroy_dumb(output->dumb[i]);
1610 if (output->image[i])
1611 pixman_image_unref(output->image[i]);
1613 output->dumb[i] = NULL;
1614 output->image[i] = NULL;
1621 drm_output_fini_pixman(struct drm_output *output)
1625 pixman_renderer_output_destroy(&output->base);
1626 pixman_region32_fini(&output->previous_damage);
1628 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
1629 drm_fb_destroy_dumb(output->dumb[i]);
1630 pixman_image_unref(output->image[i]);
1631 output->dumb[i] = NULL;
1632 output->image[i] = NULL;
1637 edid_parse_string(const uint8_t *data, char text[])
1642 /* this is always 12 bytes, but we can't guarantee it's null
1643 * terminated or not junk. */
1644 strncpy(text, (const char *) data, 12);
1646 /* remove insane chars */
1647 for (i = 0; text[i] != '\0'; i++) {
1648 if (text[i] == '\n' ||
1655 /* ensure string is printable */
1656 for (i = 0; text[i] != '\0'; i++) {
1657 if (!isprint(text[i])) {
1663 /* if the string is random junk, ignore the string */
1668 #define EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING 0xfe
1669 #define EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME 0xfc
1670 #define EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER 0xff
1671 #define EDID_OFFSET_DATA_BLOCKS 0x36
1672 #define EDID_OFFSET_LAST_BLOCK 0x6c
1673 #define EDID_OFFSET_PNPID 0x08
1674 #define EDID_OFFSET_SERIAL 0x0c
1677 edid_parse(struct drm_edid *edid, const uint8_t *data, size_t length)
1680 uint32_t serial_number;
1685 if (data[0] != 0x00 || data[1] != 0xff)
1688 /* decode the PNP ID from three 5 bit words packed into 2 bytes
1693 edid->pnp_id[0] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x7c) / 4) - 1;
1694 edid->pnp_id[1] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x3) * 8) + ((data[EDID_OFFSET_PNPID + 1] & 0xe0) / 32) - 1;
1695 edid->pnp_id[2] = 'A' + (data[EDID_OFFSET_PNPID + 1] & 0x1f) - 1;
1696 edid->pnp_id[3] = '\0';
1698 /* maybe there isn't a ASCII serial number descriptor, so use this instead */
1699 serial_number = (uint32_t) data[EDID_OFFSET_SERIAL + 0];
1700 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 1] * 0x100;
1701 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 2] * 0x10000;
1702 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 3] * 0x1000000;
1703 if (serial_number > 0)
1704 sprintf(edid->serial_number, "%lu", (unsigned long) serial_number);
1706 /* parse EDID data */
1707 for (i = EDID_OFFSET_DATA_BLOCKS;
1708 i <= EDID_OFFSET_LAST_BLOCK;
1710 /* ignore pixel clock data */
1716 /* any useful blocks? */
1717 if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME) {
1718 edid_parse_string(&data[i+5],
1719 edid->monitor_name);
1720 } else if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER) {
1721 edid_parse_string(&data[i+5],
1722 edid->serial_number);
1723 } else if (data[i+3] == EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING) {
1724 edid_parse_string(&data[i+5],
1732 find_and_parse_output_edid(struct drm_compositor *ec,
1733 struct drm_output *output,
1734 drmModeConnector *connector)
1736 drmModePropertyBlobPtr edid_blob = NULL;
1737 drmModePropertyPtr property;
1741 for (i = 0; i < connector->count_props && !edid_blob; i++) {
1742 property = drmModeGetProperty(ec->drm.fd, connector->props[i]);
1745 if ((property->flags & DRM_MODE_PROP_BLOB) &&
1746 !strcmp(property->name, "EDID")) {
1747 edid_blob = drmModeGetPropertyBlob(ec->drm.fd,
1748 connector->prop_values[i]);
1750 drmModeFreeProperty(property);
1755 rc = edid_parse(&output->edid,
1759 weston_log("EDID data '%s', '%s', '%s'\n",
1760 output->edid.pnp_id,
1761 output->edid.monitor_name,
1762 output->edid.serial_number);
1763 if (output->edid.pnp_id[0] != '\0')
1764 output->base.make = output->edid.pnp_id;
1765 if (output->edid.monitor_name[0] != '\0')
1766 output->base.model = output->edid.monitor_name;
1767 if (output->edid.serial_number[0] != '\0')
1768 output->base.serial_number = output->edid.serial_number;
1770 drmModeFreePropertyBlob(edid_blob);
1776 parse_modeline(const char *s, drmModeModeInfo *mode)
1782 mode->type = DRM_MODE_TYPE_USERDEF;
1788 if (sscanf(s, "%f %hd %hd %hd %hd %hd %hd %hd %hd %15s %15s",
1797 &mode->vtotal, hsync, vsync) != 11)
1800 mode->clock = fclock * 1000;
1801 if (strcmp(hsync, "+hsync") == 0)
1802 mode->flags |= DRM_MODE_FLAG_PHSYNC;
1803 else if (strcmp(hsync, "-hsync") == 0)
1804 mode->flags |= DRM_MODE_FLAG_NHSYNC;
1808 if (strcmp(vsync, "+vsync") == 0)
1809 mode->flags |= DRM_MODE_FLAG_PVSYNC;
1810 else if (strcmp(vsync, "-vsync") == 0)
1811 mode->flags |= DRM_MODE_FLAG_NVSYNC;
1819 parse_transform(const char *transform, const char *output_name)
1821 static const struct { const char *name; uint32_t token; } names[] = {
1822 { "normal", WL_OUTPUT_TRANSFORM_NORMAL },
1823 { "90", WL_OUTPUT_TRANSFORM_90 },
1824 { "180", WL_OUTPUT_TRANSFORM_180 },
1825 { "270", WL_OUTPUT_TRANSFORM_270 },
1826 { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED },
1827 { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
1828 { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
1829 { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
1833 for (i = 0; i < ARRAY_LENGTH(names); i++)
1834 if (strcmp(names[i].name, transform) == 0)
1835 return names[i].token;
1837 weston_log("Invalid transform \"%s\" for output %s\n",
1838 transform, output_name);
1840 return WL_OUTPUT_TRANSFORM_NORMAL;
1844 setup_output_seat_constraint(struct drm_compositor *ec,
1845 struct weston_output *output,
1848 if (strcmp(s, "") != 0) {
1849 struct udev_seat *seat;
1851 seat = udev_seat_get_named(&ec->input, s);
1853 seat->base.output = output;
1855 if (seat && seat->base.pointer)
1856 weston_pointer_clamp(seat->base.pointer,
1857 &seat->base.pointer->x,
1858 &seat->base.pointer->y);
1863 get_gbm_format_from_section(struct weston_config_section *section,
1864 uint32_t default_value,
1870 weston_config_section_get_string(section,
1871 "gbm-format", &s, NULL);
1874 *format = default_value;
1875 else if (strcmp(s, "xrgb8888") == 0)
1876 *format = GBM_FORMAT_XRGB8888;
1877 else if (strcmp(s, "rgb565") == 0)
1878 *format = GBM_FORMAT_RGB565;
1879 else if (strcmp(s, "xrgb2101010") == 0)
1880 *format = GBM_FORMAT_XRGB2101010;
1882 weston_log("fatal: unrecognized pixel format: %s\n", s);
1892 create_output_for_connector(struct drm_compositor *ec,
1893 drmModeRes *resources,
1894 drmModeConnector *connector,
1895 int x, int y, struct udev_device *drm_device)
1897 struct drm_output *output;
1898 struct drm_mode *drm_mode, *next, *preferred, *current, *configured, *best;
1899 struct weston_mode *m;
1900 struct weston_config_section *section;
1901 drmModeEncoder *encoder;
1902 drmModeModeInfo crtc_mode, modeline;
1904 int i, width, height, scale;
1906 const char *type_name;
1907 enum output_config config;
1910 i = find_crtc_for_connector(ec, resources, connector);
1912 weston_log("No usable crtc/encoder pair for connector.\n");
1916 output = zalloc(sizeof *output);
1920 output->base.subpixel = drm_subpixel_to_wayland(connector->subpixel);
1921 output->base.make = "unknown";
1922 output->base.model = "unknown";
1923 output->base.serial_number = "unknown";
1924 wl_list_init(&output->base.mode_list);
1926 if (connector->connector_type < ARRAY_LENGTH(connector_type_names))
1927 type_name = connector_type_names[connector->connector_type];
1929 type_name = "UNKNOWN";
1930 snprintf(name, 32, "%s%d", type_name, connector->connector_type_id);
1931 output->base.name = strdup(name);
1933 section = weston_config_get_section(ec->base.config, "output", "name",
1935 weston_config_section_get_string(section, "mode", &s, "preferred");
1936 if (strcmp(s, "off") == 0)
1937 config = OUTPUT_CONFIG_OFF;
1938 else if (strcmp(s, "preferred") == 0)
1939 config = OUTPUT_CONFIG_PREFERRED;
1940 else if (strcmp(s, "current") == 0)
1941 config = OUTPUT_CONFIG_CURRENT;
1942 else if (sscanf(s, "%dx%d", &width, &height) == 2)
1943 config = OUTPUT_CONFIG_MODE;
1944 else if (parse_modeline(s, &modeline) == 0)
1945 config = OUTPUT_CONFIG_MODELINE;
1947 weston_log("Invalid mode \"%s\" for output %s\n",
1948 s, output->base.name);
1949 config = OUTPUT_CONFIG_PREFERRED;
1953 weston_config_section_get_int(section, "scale", &scale, 1);
1954 weston_config_section_get_string(section, "transform", &s, "normal");
1955 transform = parse_transform(s, output->base.name);
1958 if (get_gbm_format_from_section(section,
1960 &output->format) == -1)
1961 output->format = ec->format;
1963 weston_config_section_get_string(section, "seat", &s, "");
1964 setup_output_seat_constraint(ec, &output->base, s);
1967 output->crtc_id = resources->crtcs[i];
1969 ec->crtc_allocator |= (1 << output->crtc_id);
1970 output->connector_id = connector->connector_id;
1971 ec->connector_allocator |= (1 << output->connector_id);
1973 output->original_crtc = drmModeGetCrtc(ec->drm.fd, output->crtc_id);
1974 output->dpms_prop = drm_get_prop(ec->drm.fd, connector, "DPMS");
1976 /* Get the current mode on the crtc that's currently driving
1977 * this connector. */
1978 encoder = drmModeGetEncoder(ec->drm.fd, connector->encoder_id);
1979 memset(&crtc_mode, 0, sizeof crtc_mode);
1980 if (encoder != NULL) {
1981 crtc = drmModeGetCrtc(ec->drm.fd, encoder->crtc_id);
1982 drmModeFreeEncoder(encoder);
1985 if (crtc->mode_valid)
1986 crtc_mode = crtc->mode;
1987 drmModeFreeCrtc(crtc);
1990 for (i = 0; i < connector->count_modes; i++) {
1991 drm_mode = drm_output_add_mode(output, &connector->modes[i]);
1996 if (config == OUTPUT_CONFIG_OFF) {
1997 weston_log("Disabling output %s\n", output->base.name);
1998 drmModeSetCrtc(ec->drm.fd, output->crtc_id,
1999 0, 0, 0, 0, 0, NULL);
2008 wl_list_for_each_reverse(drm_mode, &output->base.mode_list, base.link) {
2009 if (config == OUTPUT_CONFIG_MODE &&
2010 width == drm_mode->base.width &&
2011 height == drm_mode->base.height)
2012 configured = drm_mode;
2013 if (!memcmp(&crtc_mode, &drm_mode->mode_info, sizeof crtc_mode))
2015 if (drm_mode->base.flags & WL_OUTPUT_MODE_PREFERRED)
2016 preferred = drm_mode;
2020 if (config == OUTPUT_CONFIG_MODELINE) {
2021 configured = drm_output_add_mode(output, &modeline);
2026 if (current == NULL && crtc_mode.clock != 0) {
2027 current = drm_output_add_mode(output, &crtc_mode);
2032 if (config == OUTPUT_CONFIG_CURRENT)
2033 configured = current;
2035 if (option_current_mode && current)
2036 output->base.current_mode = ¤t->base;
2037 else if (configured)
2038 output->base.current_mode = &configured->base;
2040 output->base.current_mode = &preferred->base;
2042 output->base.current_mode = ¤t->base;
2044 output->base.current_mode = &best->base;
2046 if (output->base.current_mode == NULL) {
2047 weston_log("no available modes for %s\n", output->base.name);
2051 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
2053 weston_output_init(&output->base, &ec->base, x, y,
2054 connector->mmWidth, connector->mmHeight,
2057 if (ec->use_pixman) {
2058 if (drm_output_init_pixman(output, ec) < 0) {
2059 weston_log("Failed to init output pixman state\n");
2062 } else if (drm_output_init_egl(output, ec) < 0) {
2063 weston_log("Failed to init output gl state\n");
2067 output->backlight = backlight_init(drm_device,
2068 connector->connector_type);
2069 if (output->backlight) {
2070 weston_log("Initialized backlight, device %s\n",
2071 output->backlight->path);
2072 output->base.set_backlight = drm_set_backlight;
2073 output->base.backlight_current = drm_get_backlight(output);
2075 weston_log("Failed to initialize backlight\n");
2078 wl_list_insert(ec->base.output_list.prev, &output->base.link);
2080 find_and_parse_output_edid(ec, output, connector);
2081 if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS)
2082 output->base.connection_internal = 1;
2084 output->base.start_repaint_loop = drm_output_start_repaint_loop;
2085 output->base.repaint = drm_output_repaint;
2086 output->base.destroy = drm_output_destroy;
2087 output->base.assign_planes = drm_assign_planes;
2088 output->base.set_dpms = drm_set_dpms;
2089 output->base.switch_mode = drm_output_switch_mode;
2091 output->base.gamma_size = output->original_crtc->gamma_size;
2092 output->base.set_gamma = drm_output_set_gamma;
2094 weston_plane_init(&output->cursor_plane, &ec->base, 0, 0);
2095 weston_plane_init(&output->fb_plane, &ec->base, 0, 0);
2097 weston_compositor_stack_plane(&ec->base, &output->cursor_plane, NULL);
2098 weston_compositor_stack_plane(&ec->base, &output->fb_plane,
2099 &ec->base.primary_plane);
2101 weston_log("Output %s, (connector %d, crtc %d)\n",
2102 output->base.name, output->connector_id, output->crtc_id);
2103 wl_list_for_each(m, &output->base.mode_list, link)
2104 weston_log_continue(STAMP_SPACE "mode %dx%d@%.1f%s%s%s\n",
2105 m->width, m->height, m->refresh / 1000.0,
2106 m->flags & WL_OUTPUT_MODE_PREFERRED ?
2108 m->flags & WL_OUTPUT_MODE_CURRENT ?
2110 connector->count_modes == 0 ?
2116 weston_output_destroy(&output->base);
2118 wl_list_for_each_safe(drm_mode, next, &output->base.mode_list,
2120 wl_list_remove(&drm_mode->base.link);
2124 drmModeFreeCrtc(output->original_crtc);
2125 ec->crtc_allocator &= ~(1 << output->crtc_id);
2126 ec->connector_allocator &= ~(1 << output->connector_id);
2133 create_sprites(struct drm_compositor *ec)
2135 struct drm_sprite *sprite;
2136 drmModePlaneRes *plane_res;
2137 drmModePlane *plane;
2140 plane_res = drmModeGetPlaneResources(ec->drm.fd);
2142 weston_log("failed to get plane resources: %s\n",
2147 for (i = 0; i < plane_res->count_planes; i++) {
2148 plane = drmModeGetPlane(ec->drm.fd, plane_res->planes[i]);
2152 sprite = zalloc(sizeof(*sprite) + ((sizeof(uint32_t)) *
2153 plane->count_formats));
2155 weston_log("%s: out of memory\n",
2157 drmModeFreePlane(plane);
2161 sprite->possible_crtcs = plane->possible_crtcs;
2162 sprite->plane_id = plane->plane_id;
2163 sprite->current = NULL;
2164 sprite->next = NULL;
2165 sprite->compositor = ec;
2166 sprite->count_formats = plane->count_formats;
2167 memcpy(sprite->formats, plane->formats,
2168 plane->count_formats * sizeof(plane->formats[0]));
2169 drmModeFreePlane(plane);
2170 weston_plane_init(&sprite->plane, &ec->base, 0, 0);
2171 weston_compositor_stack_plane(&ec->base, &sprite->plane,
2172 &ec->base.primary_plane);
2174 wl_list_insert(&ec->sprite_list, &sprite->link);
2177 drmModeFreePlaneResources(plane_res);
2181 destroy_sprites(struct drm_compositor *compositor)
2183 struct drm_sprite *sprite, *next;
2184 struct drm_output *output;
2186 output = container_of(compositor->base.output_list.next,
2187 struct drm_output, base.link);
2189 wl_list_for_each_safe(sprite, next, &compositor->sprite_list, link) {
2190 drmModeSetPlane(compositor->drm.fd,
2192 output->crtc_id, 0, 0,
2193 0, 0, 0, 0, 0, 0, 0, 0);
2194 drm_output_release_fb(output, sprite->current);
2195 drm_output_release_fb(output, sprite->next);
2196 weston_plane_release(&sprite->plane);
2202 create_outputs(struct drm_compositor *ec, uint32_t option_connector,
2203 struct udev_device *drm_device)
2205 drmModeConnector *connector;
2206 drmModeRes *resources;
2210 resources = drmModeGetResources(ec->drm.fd);
2212 weston_log("drmModeGetResources failed\n");
2216 ec->crtcs = calloc(resources->count_crtcs, sizeof(uint32_t));
2218 drmModeFreeResources(resources);
2222 ec->min_width = resources->min_width;
2223 ec->max_width = resources->max_width;
2224 ec->min_height = resources->min_height;
2225 ec->max_height = resources->max_height;
2227 ec->num_crtcs = resources->count_crtcs;
2228 memcpy(ec->crtcs, resources->crtcs, sizeof(uint32_t) * ec->num_crtcs);
2230 for (i = 0; i < resources->count_connectors; i++) {
2231 connector = drmModeGetConnector(ec->drm.fd,
2232 resources->connectors[i]);
2233 if (connector == NULL)
2236 if (connector->connection == DRM_MODE_CONNECTED &&
2237 (option_connector == 0 ||
2238 connector->connector_id == option_connector)) {
2239 if (create_output_for_connector(ec, resources,
2242 drmModeFreeConnector(connector);
2246 x += container_of(ec->base.output_list.prev,
2247 struct weston_output,
2251 drmModeFreeConnector(connector);
2254 if (wl_list_empty(&ec->base.output_list)) {
2255 weston_log("No currently active connector found.\n");
2256 drmModeFreeResources(resources);
2260 drmModeFreeResources(resources);
2266 update_outputs(struct drm_compositor *ec, struct udev_device *drm_device)
2268 drmModeConnector *connector;
2269 drmModeRes *resources;
2270 struct drm_output *output, *next;
2272 uint32_t connected = 0, disconnects = 0;
2275 resources = drmModeGetResources(ec->drm.fd);
2277 weston_log("drmModeGetResources failed\n");
2281 /* collect new connects */
2282 for (i = 0; i < resources->count_connectors; i++) {
2283 int connector_id = resources->connectors[i];
2285 connector = drmModeGetConnector(ec->drm.fd, connector_id);
2286 if (connector == NULL)
2289 if (connector->connection != DRM_MODE_CONNECTED) {
2290 drmModeFreeConnector(connector);
2294 connected |= (1 << connector_id);
2296 if (!(ec->connector_allocator & (1 << connector_id))) {
2297 struct weston_output *last =
2298 container_of(ec->base.output_list.prev,
2299 struct weston_output, link);
2301 /* XXX: not yet needed, we die with 0 outputs */
2302 if (!wl_list_empty(&ec->base.output_list))
2303 x = last->x + last->width;
2307 create_output_for_connector(ec, resources,
2310 weston_log("connector %d connected\n", connector_id);
2313 drmModeFreeConnector(connector);
2315 drmModeFreeResources(resources);
2317 disconnects = ec->connector_allocator & ~connected;
2319 wl_list_for_each_safe(output, next, &ec->base.output_list,
2321 if (disconnects & (1 << output->connector_id)) {
2322 disconnects &= ~(1 << output->connector_id);
2323 weston_log("connector %d disconnected\n",
2324 output->connector_id);
2325 drm_output_destroy(&output->base);
2330 /* FIXME: handle zero outputs, without terminating */
2331 if (ec->connector_allocator == 0)
2332 wl_display_terminate(ec->base.wl_display);
2336 udev_event_is_hotplug(struct drm_compositor *ec, struct udev_device *device)
2341 sysnum = udev_device_get_sysnum(device);
2342 if (!sysnum || atoi(sysnum) != ec->drm.id)
2345 val = udev_device_get_property_value(device, "HOTPLUG");
2349 return strcmp(val, "1") == 0;
2353 udev_drm_event(int fd, uint32_t mask, void *data)
2355 struct drm_compositor *ec = data;
2356 struct udev_device *event;
2358 event = udev_monitor_receive_device(ec->udev_monitor);
2360 if (udev_event_is_hotplug(ec, event))
2361 update_outputs(ec, event);
2363 udev_device_unref(event);
2369 drm_restore(struct weston_compositor *ec)
2371 weston_launcher_restore(ec->launcher);
2375 drm_destroy(struct weston_compositor *ec)
2377 struct drm_compositor *d = (struct drm_compositor *) ec;
2379 udev_input_destroy(&d->input);
2381 wl_event_source_remove(d->udev_drm_source);
2382 wl_event_source_remove(d->drm_source);
2386 weston_compositor_shutdown(ec);
2389 gbm_device_destroy(d->gbm);
2391 weston_launcher_destroy(d->base.launcher);
2399 drm_compositor_set_modes(struct drm_compositor *compositor)
2401 struct drm_output *output;
2402 struct drm_mode *drm_mode;
2405 wl_list_for_each(output, &compositor->base.output_list, base.link) {
2406 if (!output->current) {
2407 /* If something that would cause the output to
2408 * switch mode happened while in another vt, we
2409 * might not have a current drm_fb. In that case,
2410 * schedule a repaint and let drm_output_repaint
2411 * handle setting the mode. */
2412 weston_output_schedule_repaint(&output->base);
2416 drm_mode = (struct drm_mode *) output->base.current_mode;
2417 ret = drmModeSetCrtc(compositor->drm.fd, output->crtc_id,
2418 output->current->fb_id, 0, 0,
2419 &output->connector_id, 1,
2420 &drm_mode->mode_info);
2423 "failed to set mode %dx%d for output at %d,%d: %m\n",
2424 drm_mode->base.width, drm_mode->base.height,
2425 output->base.x, output->base.y);
2431 session_notify(struct wl_listener *listener, void *data)
2433 struct weston_compositor *compositor = data;
2434 struct drm_compositor *ec = data;
2435 struct drm_sprite *sprite;
2436 struct drm_output *output;
2438 if (ec->base.session_active) {
2439 weston_log("activating session\n");
2440 compositor->state = ec->prev_state;
2441 drm_compositor_set_modes(ec);
2442 weston_compositor_damage_all(compositor);
2443 udev_input_enable(&ec->input);
2445 weston_log("deactivating session\n");
2446 udev_input_disable(&ec->input);
2448 ec->prev_state = compositor->state;
2449 weston_compositor_offscreen(compositor);
2451 /* If we have a repaint scheduled (either from a
2452 * pending pageflip or the idle handler), make sure we
2453 * cancel that so we don't try to pageflip when we're
2454 * vt switched away. The OFFSCREEN state will prevent
2455 * further attemps at repainting. When we switch
2456 * back, we schedule a repaint, which will process
2457 * pending frame callbacks. */
2459 wl_list_for_each(output, &ec->base.output_list, base.link) {
2460 output->base.repaint_needed = 0;
2461 drmModeSetCursor(ec->drm.fd, output->crtc_id, 0, 0, 0);
2464 output = container_of(ec->base.output_list.next,
2465 struct drm_output, base.link);
2467 wl_list_for_each(sprite, &ec->sprite_list, link)
2468 drmModeSetPlane(ec->drm.fd,
2470 output->crtc_id, 0, 0,
2471 0, 0, 0, 0, 0, 0, 0, 0);
2476 switch_vt_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
2478 struct weston_compositor *compositor = data;
2480 weston_launcher_activate_vt(compositor->launcher, key - KEY_F1 + 1);
2485 * Some systems may have multiple DRM devices attached to a single seat. This
2486 * function loops over all devices and tries to find a PCI device with the
2487 * boot_vga sysfs attribute set to 1.
2488 * If no such device is found, the first DRM device reported by udev is used.
2490 static struct udev_device*
2491 find_primary_gpu(struct drm_compositor *ec, const char *seat)
2493 struct udev_enumerate *e;
2494 struct udev_list_entry *entry;
2495 const char *path, *device_seat, *id;
2496 struct udev_device *device, *drm_device, *pci;
2498 e = udev_enumerate_new(ec->udev);
2499 udev_enumerate_add_match_subsystem(e, "drm");
2500 udev_enumerate_add_match_sysname(e, "card[0-9]*");
2502 udev_enumerate_scan_devices(e);
2504 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
2505 path = udev_list_entry_get_name(entry);
2506 device = udev_device_new_from_syspath(ec->udev, path);
2509 device_seat = udev_device_get_property_value(device, "ID_SEAT");
2511 device_seat = default_seat;
2512 if (strcmp(device_seat, seat)) {
2513 udev_device_unref(device);
2517 pci = udev_device_get_parent_with_subsystem_devtype(device,
2520 id = udev_device_get_sysattr_value(pci, "boot_vga");
2521 if (id && !strcmp(id, "1")) {
2523 udev_device_unref(drm_device);
2524 drm_device = device;
2530 drm_device = device;
2532 udev_device_unref(device);
2535 udev_enumerate_unref(e);
2540 planes_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
2542 struct drm_compositor *c = data;
2546 c->cursors_are_broken ^= 1;
2549 c->sprites_are_broken ^= 1;
2552 c->sprites_hidden ^= 1;
2559 #ifdef BUILD_VAAPI_RECORDER
2561 recorder_destroy(struct drm_output *output)
2563 vaapi_recorder_destroy(output->recorder);
2564 output->recorder = NULL;
2566 output->base.disable_planes--;
2568 wl_list_remove(&output->recorder_frame_listener.link);
2569 weston_log("[libva recorder] done\n");
2573 recorder_frame_notify(struct wl_listener *listener, void *data)
2575 struct drm_output *output;
2576 struct drm_compositor *c;
2579 output = container_of(listener, struct drm_output,
2580 recorder_frame_listener);
2581 c = (struct drm_compositor *) output->base.compositor;
2583 if (!output->recorder)
2586 ret = drmPrimeHandleToFD(c->drm.fd, output->current->handle,
2589 weston_log("[libva recorder] "
2590 "failed to create prime fd for front buffer\n");
2594 ret = vaapi_recorder_frame(output->recorder, fd,
2595 output->current->stride);
2597 weston_log("[libva recorder] aborted: %m\n");
2598 recorder_destroy(output);
2603 create_recorder(struct drm_compositor *c, int width, int height,
2604 const char *filename)
2609 fd = open(c->drm.filename, O_RDWR | O_CLOEXEC);
2613 drmGetMagic(fd, &magic);
2614 drmAuthMagic(c->drm.fd, magic);
2616 return vaapi_recorder_create(fd, width, height, filename);
2620 recorder_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
2623 struct drm_compositor *c = data;
2624 struct drm_output *output;
2627 output = container_of(c->base.output_list.next,
2628 struct drm_output, base.link);
2630 if (!output->recorder) {
2631 if (output->format != GBM_FORMAT_XRGB8888) {
2632 weston_log("failed to start vaapi recorder: "
2633 "output format not supported\n");
2637 width = output->base.current_mode->width;
2638 height = output->base.current_mode->height;
2641 create_recorder(c, width, height, "capture.h264");
2642 if (!output->recorder) {
2643 weston_log("failed to create vaapi recorder\n");
2647 output->base.disable_planes++;
2649 output->recorder_frame_listener.notify = recorder_frame_notify;
2650 wl_signal_add(&output->base.frame_signal,
2651 &output->recorder_frame_listener);
2653 weston_output_schedule_repaint(&output->base);
2655 weston_log("[libva recorder] initialized\n");
2657 recorder_destroy(output);
2662 recorder_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
2665 weston_log("Compiled without libva support\n");
2670 switch_to_gl_renderer(struct drm_compositor *c)
2672 struct drm_output *output;
2677 weston_log("Switching to GL renderer\n");
2679 c->gbm = create_gbm_device(c->drm.fd);
2681 weston_log("Failed to create gbm device. "
2682 "Aborting renderer switch\n");
2686 wl_list_for_each(output, &c->base.output_list, base.link)
2687 pixman_renderer_output_destroy(&output->base);
2689 c->base.renderer->destroy(&c->base);
2691 if (drm_compositor_create_gl_renderer(c) < 0) {
2692 gbm_device_destroy(c->gbm);
2693 weston_log("Failed to create GL renderer. Quitting.\n");
2694 /* FIXME: we need a function to shutdown cleanly */
2698 wl_list_for_each(output, &c->base.output_list, base.link)
2699 drm_output_init_egl(output, c);
2705 renderer_switch_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
2708 struct drm_compositor *c = (struct drm_compositor *) seat->compositor;
2710 switch_to_gl_renderer(c);
2713 static struct weston_compositor *
2714 drm_compositor_create(struct wl_display *display,
2715 struct drm_parameters *param,
2716 int *argc, char *argv[],
2717 struct weston_config *config)
2719 struct drm_compositor *ec;
2720 struct weston_config_section *section;
2721 struct udev_device *drm_device;
2722 struct wl_event_loop *loop;
2726 weston_log("initializing drm backend\n");
2728 ec = zalloc(sizeof *ec);
2732 /* KMS support for sprites is not complete yet, so disable the
2733 * functionality for now. */
2734 ec->sprites_are_broken = 1;
2736 section = weston_config_get_section(config, "core", NULL, NULL);
2737 if (get_gbm_format_from_section(section,
2738 GBM_FORMAT_XRGB8888,
2742 ec->use_pixman = param->use_pixman;
2744 if (weston_compositor_init(&ec->base, display, argc, argv,
2746 weston_log("%s failed\n", __func__);
2750 /* Check if we run drm-backend using weston-launch */
2751 ec->base.launcher = weston_launcher_connect(&ec->base, param->tty,
2753 if (ec->base.launcher == NULL) {
2754 weston_log("fatal: drm backend should be run "
2755 "using weston-launch binary or as root\n");
2756 goto err_compositor;
2759 ec->udev = udev_new();
2760 if (ec->udev == NULL) {
2761 weston_log("failed to initialize udev context\n");
2765 ec->base.wl_display = display;
2766 ec->session_listener.notify = session_notify;
2767 wl_signal_add(&ec->base.session_signal, &ec->session_listener);
2769 drm_device = find_primary_gpu(ec, param->seat_id);
2770 if (drm_device == NULL) {
2771 weston_log("no drm device found\n");
2774 path = udev_device_get_syspath(drm_device);
2776 if (init_drm(ec, drm_device) < 0) {
2777 weston_log("failed to initialize kms\n");
2781 if (ec->use_pixman) {
2782 if (init_pixman(ec) < 0) {
2783 weston_log("failed to initialize pixman renderer\n");
2787 if (init_egl(ec) < 0) {
2788 weston_log("failed to initialize egl\n");
2793 ec->base.destroy = drm_destroy;
2794 ec->base.restore = drm_restore;
2796 ec->prev_state = WESTON_COMPOSITOR_ACTIVE;
2798 for (key = KEY_F1; key < KEY_F9; key++)
2799 weston_compositor_add_key_binding(&ec->base, key,
2800 MODIFIER_CTRL | MODIFIER_ALT,
2801 switch_vt_binding, ec);
2803 wl_list_init(&ec->sprite_list);
2806 if (udev_input_init(&ec->input,
2807 &ec->base, ec->udev, param->seat_id) < 0) {
2808 weston_log("failed to create input devices\n");
2812 if (create_outputs(ec, param->connector, drm_device) < 0) {
2813 weston_log("failed to create output for %s\n", path);
2814 goto err_udev_input;
2817 /* A this point we have some idea of whether or not we have a working
2819 if (!ec->cursors_are_broken)
2820 ec->base.capabilities |= WESTON_CAP_CURSOR_PLANE;
2824 loop = wl_display_get_event_loop(ec->base.wl_display);
2826 wl_event_loop_add_fd(loop, ec->drm.fd,
2827 WL_EVENT_READABLE, on_drm_input, ec);
2829 ec->udev_monitor = udev_monitor_new_from_netlink(ec->udev, "udev");
2830 if (ec->udev_monitor == NULL) {
2831 weston_log("failed to intialize udev monitor\n");
2832 goto err_drm_source;
2834 udev_monitor_filter_add_match_subsystem_devtype(ec->udev_monitor,
2836 ec->udev_drm_source =
2837 wl_event_loop_add_fd(loop,
2838 udev_monitor_get_fd(ec->udev_monitor),
2839 WL_EVENT_READABLE, udev_drm_event, ec);
2841 if (udev_monitor_enable_receiving(ec->udev_monitor) < 0) {
2842 weston_log("failed to enable udev-monitor receiving\n");
2843 goto err_udev_monitor;
2846 udev_device_unref(drm_device);
2848 weston_compositor_add_debug_binding(&ec->base, KEY_O,
2849 planes_binding, ec);
2850 weston_compositor_add_debug_binding(&ec->base, KEY_C,
2851 planes_binding, ec);
2852 weston_compositor_add_debug_binding(&ec->base, KEY_V,
2853 planes_binding, ec);
2854 weston_compositor_add_debug_binding(&ec->base, KEY_Q,
2855 recorder_binding, ec);
2856 weston_compositor_add_debug_binding(&ec->base, KEY_W,
2857 renderer_switch_binding, ec);
2862 wl_event_source_remove(ec->udev_drm_source);
2863 udev_monitor_unref(ec->udev_monitor);
2865 wl_event_source_remove(ec->drm_source);
2867 udev_input_destroy(&ec->input);
2869 ec->base.renderer->destroy(&ec->base);
2870 gbm_device_destroy(ec->gbm);
2871 destroy_sprites(ec);
2873 udev_device_unref(drm_device);
2875 weston_launcher_destroy(ec->base.launcher);
2877 udev_unref(ec->udev);
2879 weston_compositor_shutdown(&ec->base);
2885 WL_EXPORT struct weston_compositor *
2886 backend_init(struct wl_display *display, int *argc, char *argv[],
2887 struct weston_config *config)
2889 struct drm_parameters param = { 0, };
2891 const struct weston_option drm_options[] = {
2892 { WESTON_OPTION_INTEGER, "connector", 0, ¶m.connector },
2893 { WESTON_OPTION_STRING, "seat", 0, ¶m.seat_id },
2894 { WESTON_OPTION_INTEGER, "tty", 0, ¶m.tty },
2895 { WESTON_OPTION_BOOLEAN, "current-mode", 0, &option_current_mode },
2896 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, ¶m.use_pixman },
2899 param.seat_id = default_seat;
2901 parse_options(drm_options, ARRAY_LENGTH(drm_options), argc, argv);
2903 return drm_compositor_create(display, ¶m, argc, argv, config);