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 #ifndef DRM_CAP_CURSOR_WIDTH
59 #define DRM_CAP_CURSOR_WIDTH 0x8
62 #ifndef DRM_CAP_CURSOR_HEIGHT
63 #define DRM_CAP_CURSOR_HEIGHT 0x9
66 #ifndef GBM_BO_USE_CURSOR
67 #define GBM_BO_USE_CURSOR GBM_BO_USE_CURSOR_64X64
70 static int option_current_mode = 0;
73 OUTPUT_CONFIG_INVALID = 0,
75 OUTPUT_CONFIG_PREFERRED,
76 OUTPUT_CONFIG_CURRENT,
78 OUTPUT_CONFIG_MODELINE
81 struct drm_compositor {
82 struct weston_compositor base;
85 struct wl_event_source *drm_source;
87 struct udev_monitor *udev_monitor;
88 struct wl_event_source *udev_drm_source;
95 struct gbm_device *gbm;
98 uint32_t crtc_allocator;
99 uint32_t connector_allocator;
100 struct wl_listener session_listener;
103 /* we need these parameters in order to not fail drmModeAddFB2()
104 * due to out of bounds dimensions, and then mistakenly set
105 * sprites_are_broken:
107 uint32_t min_width, max_width;
108 uint32_t min_height, max_height;
111 struct wl_list sprite_list;
112 int sprites_are_broken;
115 int cursors_are_broken;
122 struct udev_input input;
125 uint32_t cursor_width;
126 uint32_t cursor_height;
130 struct weston_mode base;
131 drmModeModeInfo mode_info;
137 struct drm_output *output;
138 uint32_t fb_id, stride, handle, size;
140 int is_client_buffer;
141 struct weston_buffer_reference buffer_ref;
143 /* Used by gbm fbs */
146 /* Used by dumb fbs */
152 char monitor_name[13];
154 char serial_number[13];
158 struct weston_output base;
162 uint32_t connector_id;
163 drmModeCrtcPtr original_crtc;
164 struct drm_edid edid;
165 drmModePropertyPtr dpms_prop;
169 int page_flip_pending;
173 struct gbm_surface *surface;
174 struct gbm_bo *cursor_bo[2];
175 struct weston_plane cursor_plane;
176 struct weston_plane fb_plane;
177 struct weston_view *cursor_view;
179 struct drm_fb *current, *next;
180 struct backlight *backlight;
182 struct drm_fb *dumb[2];
183 pixman_image_t *image[2];
185 pixman_region32_t previous_damage;
187 struct vaapi_recorder *recorder;
188 struct wl_listener recorder_frame_listener;
192 * An output has a primary display plane plus zero or more sprites for
193 * blending display contents.
198 struct weston_plane plane;
200 struct drm_fb *current, *next;
201 struct drm_output *output;
202 struct drm_compositor *compositor;
204 uint32_t possible_crtcs;
206 uint32_t count_formats;
208 int32_t src_x, src_y;
209 uint32_t src_w, src_h;
210 uint32_t dest_x, dest_y;
211 uint32_t dest_w, dest_h;
216 struct drm_parameters {
223 static struct gl_renderer_interface *gl_renderer;
225 static const char default_seat[] = "seat0";
228 drm_output_set_cursor(struct drm_output *output);
231 drm_sprite_crtc_supported(struct weston_output *output_base, uint32_t supported)
233 struct weston_compositor *ec = output_base->compositor;
234 struct drm_compositor *c =(struct drm_compositor *) ec;
235 struct drm_output *output = (struct drm_output *) output_base;
238 for (crtc = 0; crtc < c->num_crtcs; crtc++) {
239 if (c->crtcs[crtc] != output->crtc_id)
242 if (supported & (1 << crtc))
250 drm_fb_destroy_callback(struct gbm_bo *bo, void *data)
252 struct drm_fb *fb = data;
253 struct gbm_device *gbm = gbm_bo_get_device(bo);
256 drmModeRmFB(gbm_device_get_fd(gbm), fb->fb_id);
258 weston_buffer_reference(&fb->buffer_ref, NULL);
263 static struct drm_fb *
264 drm_fb_create_dumb(struct drm_compositor *ec, unsigned width, unsigned height)
269 struct drm_mode_create_dumb create_arg;
270 struct drm_mode_destroy_dumb destroy_arg;
271 struct drm_mode_map_dumb map_arg;
273 fb = zalloc(sizeof *fb);
277 memset(&create_arg, 0, sizeof create_arg);
279 create_arg.width = width;
280 create_arg.height = height;
282 ret = drmIoctl(ec->drm.fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_arg);
286 fb->handle = create_arg.handle;
287 fb->stride = create_arg.pitch;
288 fb->size = create_arg.size;
291 ret = drmModeAddFB(ec->drm.fd, width, height, 24, 32,
292 fb->stride, fb->handle, &fb->fb_id);
296 memset(&map_arg, 0, sizeof map_arg);
297 map_arg.handle = fb->handle;
298 ret = drmIoctl(fb->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_arg);
302 fb->map = mmap(0, fb->size, PROT_WRITE,
303 MAP_SHARED, ec->drm.fd, map_arg.offset);
304 if (fb->map == MAP_FAILED)
310 drmModeRmFB(ec->drm.fd, fb->fb_id);
312 memset(&destroy_arg, 0, sizeof(destroy_arg));
313 destroy_arg.handle = create_arg.handle;
314 drmIoctl(ec->drm.fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
321 drm_fb_destroy_dumb(struct drm_fb *fb)
323 struct drm_mode_destroy_dumb destroy_arg;
329 drmModeRmFB(fb->fd, fb->fb_id);
331 weston_buffer_reference(&fb->buffer_ref, NULL);
333 munmap(fb->map, fb->size);
335 memset(&destroy_arg, 0, sizeof(destroy_arg));
336 destroy_arg.handle = fb->handle;
337 drmIoctl(fb->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
342 static struct drm_fb *
343 drm_fb_get_from_bo(struct gbm_bo *bo,
344 struct drm_compositor *compositor, uint32_t format)
346 struct drm_fb *fb = gbm_bo_get_user_data(bo);
347 uint32_t width, height;
348 uint32_t handles[4], pitches[4], offsets[4];
354 fb = calloc(1, sizeof *fb);
360 width = gbm_bo_get_width(bo);
361 height = gbm_bo_get_height(bo);
362 fb->stride = gbm_bo_get_stride(bo);
363 fb->handle = gbm_bo_get_handle(bo).u32;
364 fb->size = fb->stride * height;
365 fb->fd = compositor->drm.fd;
367 if (compositor->min_width > width || width > compositor->max_width ||
368 compositor->min_height > height ||
369 height > compositor->max_height) {
370 weston_log("bo geometry out of bounds\n");
376 if (format && !compositor->no_addfb2) {
377 handles[0] = fb->handle;
378 pitches[0] = fb->stride;
381 ret = drmModeAddFB2(compositor->drm.fd, width, height,
382 format, handles, pitches, offsets,
385 weston_log("addfb2 failed: %m\n");
386 compositor->no_addfb2 = 1;
387 compositor->sprites_are_broken = 1;
392 ret = drmModeAddFB(compositor->drm.fd, width, height, 24, 32,
393 fb->stride, fb->handle, &fb->fb_id);
396 weston_log("failed to create kms fb: %m\n");
400 gbm_bo_set_user_data(bo, fb, drm_fb_destroy_callback);
410 drm_fb_set_buffer(struct drm_fb *fb, struct weston_buffer *buffer)
412 assert(fb->buffer_ref.buffer == NULL);
414 fb->is_client_buffer = 1;
416 weston_buffer_reference(&fb->buffer_ref, buffer);
420 drm_output_release_fb(struct drm_output *output, struct drm_fb *fb)
426 (fb != output->dumb[0] && fb != output->dumb[1])) {
427 drm_fb_destroy_dumb(fb);
429 if (fb->is_client_buffer)
430 gbm_bo_destroy(fb->bo);
432 gbm_surface_release_buffer(output->surface,
438 drm_output_check_scanout_format(struct drm_output *output,
439 struct weston_surface *es, struct gbm_bo *bo)
444 format = gbm_bo_get_format(bo);
446 if (format == GBM_FORMAT_ARGB8888) {
447 /* We can scanout an ARGB buffer if the surface's
448 * opaque region covers the whole output, but we have
449 * to use XRGB as the KMS format code. */
450 pixman_region32_init_rect(&r, 0, 0,
452 output->base.height);
453 pixman_region32_subtract(&r, &r, &es->opaque);
455 if (!pixman_region32_not_empty(&r))
456 format = GBM_FORMAT_XRGB8888;
458 pixman_region32_fini(&r);
461 if (output->format == format)
467 static struct weston_plane *
468 drm_output_prepare_scanout_view(struct weston_output *_output,
469 struct weston_view *ev)
471 struct drm_output *output = (struct drm_output *) _output;
472 struct drm_compositor *c =
473 (struct drm_compositor *) output->base.compositor;
474 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
475 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
479 if (ev->geometry.x != output->base.x ||
480 ev->geometry.y != output->base.y ||
481 buffer == NULL || c->gbm == NULL ||
482 buffer->width != output->base.current_mode->width ||
483 buffer->height != output->base.current_mode->height ||
484 output->base.transform != viewport->buffer.transform ||
485 ev->transform.enabled)
488 bo = gbm_bo_import(c->gbm, GBM_BO_IMPORT_WL_BUFFER,
489 buffer->resource, GBM_BO_USE_SCANOUT);
491 /* Unable to use the buffer for scanout */
495 format = drm_output_check_scanout_format(output, ev->surface, bo);
501 output->next = drm_fb_get_from_bo(bo, c, format);
507 drm_fb_set_buffer(output->next, buffer);
509 return &output->fb_plane;
513 drm_output_render_gl(struct drm_output *output, pixman_region32_t *damage)
515 struct drm_compositor *c =
516 (struct drm_compositor *) output->base.compositor;
519 c->base.renderer->repaint_output(&output->base, damage);
521 bo = gbm_surface_lock_front_buffer(output->surface);
523 weston_log("failed to lock front buffer: %m\n");
527 output->next = drm_fb_get_from_bo(bo, c, output->format);
529 weston_log("failed to get drm_fb for bo\n");
530 gbm_surface_release_buffer(output->surface, bo);
536 drm_output_render_pixman(struct drm_output *output, pixman_region32_t *damage)
538 struct weston_compositor *ec = output->base.compositor;
539 pixman_region32_t total_damage, previous_damage;
541 pixman_region32_init(&total_damage);
542 pixman_region32_init(&previous_damage);
544 pixman_region32_copy(&previous_damage, damage);
546 pixman_region32_union(&total_damage, damage, &output->previous_damage);
547 pixman_region32_copy(&output->previous_damage, &previous_damage);
549 output->current_image ^= 1;
551 output->next = output->dumb[output->current_image];
552 pixman_renderer_output_set_buffer(&output->base,
553 output->image[output->current_image]);
555 ec->renderer->repaint_output(&output->base, &total_damage);
557 pixman_region32_fini(&total_damage);
558 pixman_region32_fini(&previous_damage);
562 drm_output_render(struct drm_output *output, pixman_region32_t *damage)
564 struct drm_compositor *c =
565 (struct drm_compositor *) output->base.compositor;
568 drm_output_render_pixman(output, damage);
570 drm_output_render_gl(output, damage);
572 pixman_region32_subtract(&c->base.primary_plane.damage,
573 &c->base.primary_plane.damage, damage);
577 drm_output_set_gamma(struct weston_output *output_base,
578 uint16_t size, uint16_t *r, uint16_t *g, uint16_t *b)
581 struct drm_output *output = (struct drm_output *) output_base;
582 struct drm_compositor *compositor = (struct drm_compositor *) output->base.compositor;
585 if (output_base->gamma_size != size)
587 if (!output->original_crtc)
590 rc = drmModeCrtcSetGamma(compositor->drm.fd,
594 weston_log("set gamma failed: %m\n");
598 drm_output_repaint(struct weston_output *output_base,
599 pixman_region32_t *damage)
601 struct drm_output *output = (struct drm_output *) output_base;
602 struct drm_compositor *compositor =
603 (struct drm_compositor *) output->base.compositor;
604 struct drm_sprite *s;
605 struct drm_mode *mode;
608 if (output->destroy_pending)
612 drm_output_render(output, damage);
616 mode = container_of(output->base.current_mode, struct drm_mode, base);
617 if (!output->current ||
618 output->current->stride != output->next->stride ||
619 output->force_modeset) {
620 ret = drmModeSetCrtc(compositor->drm.fd, output->crtc_id,
621 output->next->fb_id, 0, 0,
622 &output->connector_id, 1,
625 weston_log("set mode failed: %m\n");
628 output_base->set_dpms(output_base, WESTON_DPMS_ON);
629 output->force_modeset = 0;
632 if (drmModePageFlip(compositor->drm.fd, output->crtc_id,
634 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
635 weston_log("queueing pageflip failed: %m\n");
639 output->page_flip_pending = 1;
641 drm_output_set_cursor(output);
644 * Now, update all the sprite surfaces
646 wl_list_for_each(s, &compositor->sprite_list, link) {
647 uint32_t flags = 0, fb_id = 0;
649 .request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT,
650 .request.sequence = 1,
653 if ((!s->current && !s->next) ||
654 !drm_sprite_crtc_supported(output_base, s->possible_crtcs))
657 if (s->next && !compositor->sprites_hidden)
658 fb_id = s->next->fb_id;
660 ret = drmModeSetPlane(compositor->drm.fd, s->plane_id,
661 output->crtc_id, fb_id, flags,
662 s->dest_x, s->dest_y,
663 s->dest_w, s->dest_h,
667 weston_log("setplane failed: %d: %s\n",
668 ret, strerror(errno));
670 if (output->pipe > 0)
671 vbl.request.type |= DRM_VBLANK_SECONDARY;
674 * Queue a vblank signal so we know when the surface
675 * becomes active on the display or has been replaced.
677 vbl.request.signal = (unsigned long)s;
678 ret = drmWaitVBlank(compositor->drm.fd, &vbl);
680 weston_log("vblank event request failed: %d: %s\n",
681 ret, strerror(errno));
685 output->vblank_pending = 1;
691 output->cursor_view = NULL;
693 drm_output_release_fb(output, output->next);
701 drm_output_start_repaint_loop(struct weston_output *output_base)
703 struct drm_output *output = (struct drm_output *) output_base;
704 struct drm_compositor *compositor = (struct drm_compositor *)
705 output_base->compositor;
710 if (output->destroy_pending)
713 if (!output->current) {
714 /* We can't page flip if there's no mode set */
718 fb_id = output->current->fb_id;
720 if (drmModePageFlip(compositor->drm.fd, output->crtc_id, fb_id,
721 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
722 weston_log("queueing pageflip failed: %m\n");
729 /* if we cannot page-flip, immediately finish frame */
730 clock_gettime(compositor->clock, &ts);
731 msec = ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
732 weston_output_finish_frame(output_base, msec);
736 vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec,
739 struct drm_sprite *s = (struct drm_sprite *)data;
740 struct drm_output *output = s->output;
743 output->vblank_pending = 0;
745 drm_output_release_fb(output, s->current);
746 s->current = s->next;
749 if (!output->page_flip_pending) {
750 msecs = sec * 1000 + usec / 1000;
751 weston_output_finish_frame(&output->base, msecs);
756 drm_output_destroy(struct weston_output *output_base);
759 page_flip_handler(int fd, unsigned int frame,
760 unsigned int sec, unsigned int usec, void *data)
762 struct drm_output *output = (struct drm_output *) data;
765 /* We don't set page_flip_pending on start_repaint_loop, in that case
766 * we just want to page flip to the current buffer to get an accurate
768 if (output->page_flip_pending) {
769 drm_output_release_fb(output, output->current);
770 output->current = output->next;
774 output->page_flip_pending = 0;
776 if (output->destroy_pending)
777 drm_output_destroy(&output->base);
778 else if (!output->vblank_pending) {
779 msecs = sec * 1000 + usec / 1000;
780 weston_output_finish_frame(&output->base, msecs);
782 /* We can't call this from frame_notify, because the output's
783 * repaint needed flag is cleared just after that */
784 if (output->recorder)
785 weston_output_schedule_repaint(&output->base);
790 drm_output_check_sprite_format(struct drm_sprite *s,
791 struct weston_view *ev, struct gbm_bo *bo)
795 format = gbm_bo_get_format(bo);
797 if (format == GBM_FORMAT_ARGB8888) {
800 pixman_region32_init_rect(&r, 0, 0,
802 ev->surface->height);
803 pixman_region32_subtract(&r, &r, &ev->surface->opaque);
805 if (!pixman_region32_not_empty(&r))
806 format = GBM_FORMAT_XRGB8888;
808 pixman_region32_fini(&r);
811 for (i = 0; i < s->count_formats; i++)
812 if (s->formats[i] == format)
819 drm_view_transform_supported(struct weston_view *ev)
821 return !ev->transform.enabled ||
822 (ev->transform.matrix.type < WESTON_MATRIX_TRANSFORM_ROTATE);
825 static struct weston_plane *
826 drm_output_prepare_overlay_view(struct weston_output *output_base,
827 struct weston_view *ev)
829 struct weston_compositor *ec = output_base->compositor;
830 struct drm_compositor *c =(struct drm_compositor *) ec;
831 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
832 struct drm_sprite *s;
835 pixman_region32_t dest_rect, src_rect;
836 pixman_box32_t *box, tbox;
838 wl_fixed_t sx1, sy1, sx2, sy2;
843 if (viewport->buffer.transform != output_base->transform)
846 if (viewport->buffer.scale != output_base->current_scale)
849 if (c->sprites_are_broken)
852 if (ev->output_mask != (1u << output_base->id))
855 if (ev->surface->buffer_ref.buffer == NULL)
858 if (ev->alpha != 1.0f)
861 if (wl_shm_buffer_get(ev->surface->buffer_ref.buffer->resource))
864 if (!drm_view_transform_supported(ev))
867 wl_list_for_each(s, &c->sprite_list, link) {
868 if (!drm_sprite_crtc_supported(output_base, s->possible_crtcs))
877 /* No sprites available */
881 bo = gbm_bo_import(c->gbm, GBM_BO_IMPORT_WL_BUFFER,
882 ev->surface->buffer_ref.buffer->resource,
887 format = drm_output_check_sprite_format(s, ev, bo);
893 s->next = drm_fb_get_from_bo(bo, c, format);
899 drm_fb_set_buffer(s->next, ev->surface->buffer_ref.buffer);
901 box = pixman_region32_extents(&ev->transform.boundingbox);
902 s->plane.x = box->x1;
903 s->plane.y = box->y1;
906 * Calculate the source & dest rects properly based on actual
907 * position (note the caller has called weston_view_update_transform()
910 pixman_region32_init(&dest_rect);
911 pixman_region32_intersect(&dest_rect, &ev->transform.boundingbox,
912 &output_base->region);
913 pixman_region32_translate(&dest_rect, -output_base->x, -output_base->y);
914 box = pixman_region32_extents(&dest_rect);
915 tbox = weston_transformed_rect(output_base->width,
917 output_base->transform,
918 output_base->current_scale,
922 s->dest_w = tbox.x2 - tbox.x1;
923 s->dest_h = tbox.y2 - tbox.y1;
924 pixman_region32_fini(&dest_rect);
926 pixman_region32_init(&src_rect);
927 pixman_region32_intersect(&src_rect, &ev->transform.boundingbox,
928 &output_base->region);
929 box = pixman_region32_extents(&src_rect);
931 weston_view_from_global_fixed(ev,
932 wl_fixed_from_int(box->x1),
933 wl_fixed_from_int(box->y1),
935 weston_view_from_global_fixed(ev,
936 wl_fixed_from_int(box->x2),
937 wl_fixed_from_int(box->y2),
944 if (sx2 > wl_fixed_from_int(ev->surface->width))
945 sx2 = wl_fixed_from_int(ev->surface->width);
946 if (sy2 > wl_fixed_from_int(ev->surface->height))
947 sy2 = wl_fixed_from_int(ev->surface->height);
954 tbox = weston_transformed_rect(wl_fixed_from_int(ev->surface->width),
955 wl_fixed_from_int(ev->surface->height),
956 viewport->buffer.transform,
957 viewport->buffer.scale,
960 s->src_x = tbox.x1 << 8;
961 s->src_y = tbox.y1 << 8;
962 s->src_w = (tbox.x2 - tbox.x1) << 8;
963 s->src_h = (tbox.y2 - tbox.y1) << 8;
964 pixman_region32_fini(&src_rect);
969 static struct weston_plane *
970 drm_output_prepare_cursor_view(struct weston_output *output_base,
971 struct weston_view *ev)
973 struct drm_compositor *c =
974 (struct drm_compositor *) output_base->compositor;
975 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
976 struct drm_output *output = (struct drm_output *) output_base;
980 if (output->base.transform != WL_OUTPUT_TRANSFORM_NORMAL)
982 if (viewport->buffer.scale != output_base->current_scale)
984 if (output->cursor_view)
986 if (ev->output_mask != (1u << output_base->id))
988 if (c->cursors_are_broken)
990 if (ev->surface->buffer_ref.buffer == NULL ||
991 !wl_shm_buffer_get(ev->surface->buffer_ref.buffer->resource) ||
992 ev->surface->width > 64 || ev->surface->height > 64)
995 output->cursor_view = ev;
997 return &output->cursor_plane;
1001 drm_output_set_cursor(struct drm_output *output)
1003 struct weston_view *ev = output->cursor_view;
1004 struct weston_buffer *buffer;
1005 struct drm_compositor *c =
1006 (struct drm_compositor *) output->base.compositor;
1007 EGLint handle, stride;
1009 uint32_t buf[c->cursor_width * c->cursor_height];
1013 output->cursor_view = NULL;
1015 drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0);
1019 buffer = ev->surface->buffer_ref.buffer;
1022 pixman_region32_not_empty(&output->cursor_plane.damage)) {
1023 pixman_region32_fini(&output->cursor_plane.damage);
1024 pixman_region32_init(&output->cursor_plane.damage);
1025 output->current_cursor ^= 1;
1026 bo = output->cursor_bo[output->current_cursor];
1027 memset(buf, 0, sizeof buf);
1028 stride = wl_shm_buffer_get_stride(buffer->shm_buffer);
1029 s = wl_shm_buffer_get_data(buffer->shm_buffer);
1030 wl_shm_buffer_begin_access(buffer->shm_buffer);
1031 for (i = 0; i < ev->surface->height; i++)
1032 memcpy(buf + i * c->cursor_width, s + i * stride,
1033 ev->surface->width * 4);
1034 wl_shm_buffer_end_access(buffer->shm_buffer);
1036 if (gbm_bo_write(bo, buf, sizeof buf) < 0)
1037 weston_log("failed update cursor: %m\n");
1039 handle = gbm_bo_get_handle(bo).s32;
1040 if (drmModeSetCursor(c->drm.fd, output->crtc_id, handle,
1041 c->cursor_width, c->cursor_height)) {
1042 weston_log("failed to set cursor: %m\n");
1043 c->cursors_are_broken = 1;
1047 x = (ev->geometry.x - output->base.x) * output->base.current_scale;
1048 y = (ev->geometry.y - output->base.y) * output->base.current_scale;
1049 if (output->cursor_plane.x != x || output->cursor_plane.y != y) {
1050 if (drmModeMoveCursor(c->drm.fd, output->crtc_id, x, y)) {
1051 weston_log("failed to move cursor: %m\n");
1052 c->cursors_are_broken = 1;
1055 output->cursor_plane.x = x;
1056 output->cursor_plane.y = y;
1061 drm_assign_planes(struct weston_output *output)
1063 struct drm_compositor *c =
1064 (struct drm_compositor *) output->compositor;
1065 struct weston_view *ev, *next;
1066 pixman_region32_t overlap, surface_overlap;
1067 struct weston_plane *primary, *next_plane;
1070 * Find a surface for each sprite in the output using some heuristics:
1072 * 2) frequency of update
1073 * 3) opacity (though some hw might support alpha blending)
1074 * 4) clipping (this can be fixed with color keys)
1076 * The idea is to save on blitting since this should save power.
1077 * If we can get a large video surface on the sprite for example,
1078 * the main display surface may not need to update at all, and
1079 * the client buffer can be used directly for the sprite surface
1080 * as we do for flipping full screen surfaces.
1082 pixman_region32_init(&overlap);
1083 primary = &c->base.primary_plane;
1085 wl_list_for_each_safe(ev, next, &c->base.view_list, link) {
1086 struct weston_surface *es = ev->surface;
1088 /* Test whether this buffer can ever go into a plane:
1089 * non-shm, or small enough to be a cursor.
1091 * Also, keep a reference when using the pixman renderer.
1092 * That makes it possible to do a seamless switch to the GL
1093 * renderer and since the pixman renderer keeps a reference
1094 * to the buffer anyway, there is no side effects.
1096 if (c->use_pixman ||
1097 (es->buffer_ref.buffer &&
1098 (!wl_shm_buffer_get(es->buffer_ref.buffer->resource) ||
1099 (ev->surface->width <= 64 && ev->surface->height <= 64))))
1100 es->keep_buffer = 1;
1102 es->keep_buffer = 0;
1104 pixman_region32_init(&surface_overlap);
1105 pixman_region32_intersect(&surface_overlap, &overlap,
1106 &ev->transform.boundingbox);
1109 if (pixman_region32_not_empty(&surface_overlap))
1110 next_plane = primary;
1111 if (next_plane == NULL)
1112 next_plane = drm_output_prepare_cursor_view(output, ev);
1113 if (next_plane == NULL)
1114 next_plane = drm_output_prepare_scanout_view(output, ev);
1115 if (next_plane == NULL)
1116 next_plane = drm_output_prepare_overlay_view(output, ev);
1117 if (next_plane == NULL)
1118 next_plane = primary;
1119 weston_view_move_to_plane(ev, next_plane);
1120 if (next_plane == primary)
1121 pixman_region32_union(&overlap, &overlap,
1122 &ev->transform.boundingbox);
1124 pixman_region32_fini(&surface_overlap);
1126 pixman_region32_fini(&overlap);
1130 drm_output_fini_pixman(struct drm_output *output);
1133 drm_output_destroy(struct weston_output *output_base)
1135 struct drm_output *output = (struct drm_output *) output_base;
1136 struct drm_compositor *c =
1137 (struct drm_compositor *) output->base.compositor;
1138 drmModeCrtcPtr origcrtc = output->original_crtc;
1140 if (output->page_flip_pending) {
1141 output->destroy_pending = 1;
1142 weston_log("destroy output while page flip pending\n");
1146 if (output->backlight)
1147 backlight_destroy(output->backlight);
1149 drmModeFreeProperty(output->dpms_prop);
1151 /* Turn off hardware cursor */
1152 drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0);
1154 /* Restore original CRTC state */
1155 drmModeSetCrtc(c->drm.fd, origcrtc->crtc_id, origcrtc->buffer_id,
1156 origcrtc->x, origcrtc->y,
1157 &output->connector_id, 1, &origcrtc->mode);
1158 drmModeFreeCrtc(origcrtc);
1160 c->crtc_allocator &= ~(1 << output->crtc_id);
1161 c->connector_allocator &= ~(1 << output->connector_id);
1163 if (c->use_pixman) {
1164 drm_output_fini_pixman(output);
1166 gl_renderer->output_destroy(output_base);
1167 gbm_surface_destroy(output->surface);
1170 weston_plane_release(&output->fb_plane);
1171 weston_plane_release(&output->cursor_plane);
1173 weston_output_destroy(&output->base);
1178 static struct drm_mode *
1179 choose_mode (struct drm_output *output, struct weston_mode *target_mode)
1181 struct drm_mode *tmp_mode = NULL, *mode;
1183 if (output->base.current_mode->width == target_mode->width &&
1184 output->base.current_mode->height == target_mode->height &&
1185 (output->base.current_mode->refresh == target_mode->refresh ||
1186 target_mode->refresh == 0))
1187 return (struct drm_mode *)output->base.current_mode;
1189 wl_list_for_each(mode, &output->base.mode_list, base.link) {
1190 if (mode->mode_info.hdisplay == target_mode->width &&
1191 mode->mode_info.vdisplay == target_mode->height) {
1192 if (mode->mode_info.vrefresh == target_mode->refresh ||
1193 target_mode->refresh == 0) {
1195 } else if (!tmp_mode)
1204 drm_output_init_egl(struct drm_output *output, struct drm_compositor *ec);
1206 drm_output_init_pixman(struct drm_output *output, struct drm_compositor *c);
1209 drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mode)
1211 struct drm_output *output;
1212 struct drm_mode *drm_mode;
1213 struct drm_compositor *ec;
1215 if (output_base == NULL) {
1216 weston_log("output is NULL.\n");
1221 weston_log("mode is NULL.\n");
1225 ec = (struct drm_compositor *)output_base->compositor;
1226 output = (struct drm_output *)output_base;
1227 drm_mode = choose_mode (output, mode);
1230 weston_log("%s, invalid resolution:%dx%d\n", __func__, mode->width, mode->height);
1234 if (&drm_mode->base == output->base.current_mode)
1237 output->base.current_mode->flags = 0;
1239 output->base.current_mode = &drm_mode->base;
1240 output->base.current_mode->flags =
1241 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
1243 /* reset rendering stuff. */
1244 drm_output_release_fb(output, output->current);
1245 drm_output_release_fb(output, output->next);
1246 output->current = output->next = NULL;
1248 if (ec->use_pixman) {
1249 drm_output_fini_pixman(output);
1250 if (drm_output_init_pixman(output, ec) < 0) {
1251 weston_log("failed to init output pixman state with "
1256 gl_renderer->output_destroy(&output->base);
1257 gbm_surface_destroy(output->surface);
1259 if (drm_output_init_egl(output, ec) < 0) {
1260 weston_log("failed to init output egl state with "
1270 on_drm_input(int fd, uint32_t mask, void *data)
1272 drmEventContext evctx;
1274 memset(&evctx, 0, sizeof evctx);
1275 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1276 evctx.page_flip_handler = page_flip_handler;
1277 evctx.vblank_handler = vblank_handler;
1278 drmHandleEvent(fd, &evctx);
1284 init_drm(struct drm_compositor *ec, struct udev_device *device)
1286 const char *filename, *sysnum;
1290 sysnum = udev_device_get_sysnum(device);
1292 ec->drm.id = atoi(sysnum);
1293 if (!sysnum || ec->drm.id < 0) {
1294 weston_log("cannot get device sysnum\n");
1298 filename = udev_device_get_devnode(device);
1299 fd = weston_launcher_open(ec->base.launcher, filename, O_RDWR);
1301 /* Probably permissions error */
1302 weston_log("couldn't open %s, skipping\n",
1303 udev_device_get_devnode(device));
1307 weston_log("using %s\n", filename);
1310 ec->drm.filename = strdup(filename);
1312 ret = drmGetCap(fd, DRM_CAP_TIMESTAMP_MONOTONIC, &cap);
1313 if (ret == 0 && cap == 1)
1314 ec->clock = CLOCK_MONOTONIC;
1316 ec->clock = CLOCK_REALTIME;
1318 ret = drmGetCap(fd, DRM_CAP_CURSOR_WIDTH, &cap);
1320 ec->cursor_width = cap;
1322 ec->cursor_width = 64;
1324 ret = drmGetCap(fd, DRM_CAP_CURSOR_HEIGHT, &cap);
1326 ec->cursor_height = cap;
1328 ec->cursor_height = 64;
1333 static struct gbm_device *
1334 create_gbm_device(int fd)
1336 struct gbm_device *gbm;
1338 gl_renderer = weston_load_module("gl-renderer.so",
1339 "gl_renderer_interface");
1343 /* GBM will load a dri driver, but even though they need symbols from
1344 * libglapi, in some version of Mesa they are not linked to it. Since
1345 * only the gl-renderer module links to it, the call above won't make
1346 * these symbols globally available, and loading the DRI driver fails.
1347 * Workaround this by dlopen()'ing libglapi with RTLD_GLOBAL. */
1348 dlopen("libglapi.so.0", RTLD_LAZY | RTLD_GLOBAL);
1350 gbm = gbm_create_device(fd);
1356 drm_compositor_create_gl_renderer(struct drm_compositor *ec)
1360 format = ec->format;
1361 if (gl_renderer->create(&ec->base, ec->gbm,
1362 gl_renderer->opaque_attribs, &format) < 0) {
1370 init_egl(struct drm_compositor *ec)
1372 ec->gbm = create_gbm_device(ec->drm.fd);
1377 if (drm_compositor_create_gl_renderer(ec) < 0) {
1378 gbm_device_destroy(ec->gbm);
1386 init_pixman(struct drm_compositor *ec)
1388 return pixman_renderer_init(&ec->base);
1391 static struct drm_mode *
1392 drm_output_add_mode(struct drm_output *output, drmModeModeInfo *info)
1394 struct drm_mode *mode;
1397 mode = malloc(sizeof *mode);
1401 mode->base.flags = 0;
1402 mode->base.width = info->hdisplay;
1403 mode->base.height = info->vdisplay;
1405 /* Calculate higher precision (mHz) refresh rate */
1406 refresh = (info->clock * 1000000LL / info->htotal +
1407 info->vtotal / 2) / info->vtotal;
1409 if (info->flags & DRM_MODE_FLAG_INTERLACE)
1411 if (info->flags & DRM_MODE_FLAG_DBLSCAN)
1413 if (info->vscan > 1)
1414 refresh /= info->vscan;
1416 mode->base.refresh = refresh;
1417 mode->mode_info = *info;
1419 if (info->type & DRM_MODE_TYPE_PREFERRED)
1420 mode->base.flags |= WL_OUTPUT_MODE_PREFERRED;
1422 wl_list_insert(output->base.mode_list.prev, &mode->base.link);
1428 drm_subpixel_to_wayland(int drm_value)
1430 switch (drm_value) {
1432 case DRM_MODE_SUBPIXEL_UNKNOWN:
1433 return WL_OUTPUT_SUBPIXEL_UNKNOWN;
1434 case DRM_MODE_SUBPIXEL_NONE:
1435 return WL_OUTPUT_SUBPIXEL_NONE;
1436 case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB:
1437 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB;
1438 case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR:
1439 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR;
1440 case DRM_MODE_SUBPIXEL_VERTICAL_RGB:
1441 return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB;
1442 case DRM_MODE_SUBPIXEL_VERTICAL_BGR:
1443 return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR;
1447 /* returns a value between 0-255 range, where higher is brighter */
1449 drm_get_backlight(struct drm_output *output)
1451 long brightness, max_brightness, norm;
1453 brightness = backlight_get_brightness(output->backlight);
1454 max_brightness = backlight_get_max_brightness(output->backlight);
1456 /* convert it on a scale of 0 to 255 */
1457 norm = (brightness * 255)/(max_brightness);
1459 return (uint32_t) norm;
1462 /* values accepted are between 0-255 range */
1464 drm_set_backlight(struct weston_output *output_base, uint32_t value)
1466 struct drm_output *output = (struct drm_output *) output_base;
1467 long max_brightness, new_brightness;
1469 if (!output->backlight)
1475 max_brightness = backlight_get_max_brightness(output->backlight);
1477 /* get denormalized value */
1478 new_brightness = (value * max_brightness) / 255;
1480 backlight_set_brightness(output->backlight, new_brightness);
1483 static drmModePropertyPtr
1484 drm_get_prop(int fd, drmModeConnectorPtr connector, const char *name)
1486 drmModePropertyPtr props;
1489 for (i = 0; i < connector->count_props; i++) {
1490 props = drmModeGetProperty(fd, connector->props[i]);
1494 if (!strcmp(props->name, name))
1497 drmModeFreeProperty(props);
1504 drm_set_dpms(struct weston_output *output_base, enum dpms_enum level)
1506 struct drm_output *output = (struct drm_output *) output_base;
1507 struct weston_compositor *ec = output_base->compositor;
1508 struct drm_compositor *c = (struct drm_compositor *) ec;
1510 if (!output->dpms_prop)
1513 drmModeConnectorSetProperty(c->drm.fd, output->connector_id,
1514 output->dpms_prop->prop_id, level);
1517 static const char *connector_type_names[] = {
1536 find_crtc_for_connector(struct drm_compositor *ec,
1537 drmModeRes *resources, drmModeConnector *connector)
1539 drmModeEncoder *encoder;
1540 uint32_t possible_crtcs;
1543 for (j = 0; j < connector->count_encoders; j++) {
1544 encoder = drmModeGetEncoder(ec->drm.fd, connector->encoders[j]);
1545 if (encoder == NULL) {
1546 weston_log("Failed to get encoder.\n");
1549 possible_crtcs = encoder->possible_crtcs;
1550 drmModeFreeEncoder(encoder);
1552 for (i = 0; i < resources->count_crtcs; i++) {
1553 if (possible_crtcs & (1 << i) &&
1554 !(ec->crtc_allocator & (1 << resources->crtcs[i])))
1562 /* Init output state that depends on gl or gbm */
1564 drm_output_init_egl(struct drm_output *output, struct drm_compositor *ec)
1566 EGLint format = output->format;
1569 output->surface = gbm_surface_create(ec->gbm,
1570 output->base.current_mode->width,
1571 output->base.current_mode->height,
1573 GBM_BO_USE_SCANOUT |
1574 GBM_BO_USE_RENDERING);
1575 if (!output->surface) {
1576 weston_log("failed to create gbm surface\n");
1580 if (gl_renderer->output_create(&output->base, output->surface,
1581 gl_renderer->opaque_attribs,
1583 weston_log("failed to create gl renderer output state\n");
1584 gbm_surface_destroy(output->surface);
1588 flags = GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE;
1590 for (i = 0; i < 2; i++) {
1591 if (output->cursor_bo[i])
1594 output->cursor_bo[i] =
1595 gbm_bo_create(ec->gbm, ec->cursor_width, ec->cursor_height,
1596 GBM_FORMAT_ARGB8888, flags);
1599 if (output->cursor_bo[0] == NULL || output->cursor_bo[1] == NULL) {
1600 weston_log("cursor buffers unavailable, using gl cursors\n");
1601 ec->cursors_are_broken = 1;
1608 drm_output_init_pixman(struct drm_output *output, struct drm_compositor *c)
1610 int w = output->base.current_mode->width;
1611 int h = output->base.current_mode->height;
1614 /* FIXME error checking */
1616 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
1617 output->dumb[i] = drm_fb_create_dumb(c, w, h);
1618 if (!output->dumb[i])
1622 pixman_image_create_bits(PIXMAN_x8r8g8b8, w, h,
1623 output->dumb[i]->map,
1624 output->dumb[i]->stride);
1625 if (!output->image[i])
1629 if (pixman_renderer_output_create(&output->base) < 0)
1632 pixman_region32_init_rect(&output->previous_damage,
1633 output->base.x, output->base.y, output->base.width, output->base.height);
1638 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
1639 if (output->dumb[i])
1640 drm_fb_destroy_dumb(output->dumb[i]);
1641 if (output->image[i])
1642 pixman_image_unref(output->image[i]);
1644 output->dumb[i] = NULL;
1645 output->image[i] = NULL;
1652 drm_output_fini_pixman(struct drm_output *output)
1656 pixman_renderer_output_destroy(&output->base);
1657 pixman_region32_fini(&output->previous_damage);
1659 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
1660 drm_fb_destroy_dumb(output->dumb[i]);
1661 pixman_image_unref(output->image[i]);
1662 output->dumb[i] = NULL;
1663 output->image[i] = NULL;
1668 edid_parse_string(const uint8_t *data, char text[])
1673 /* this is always 12 bytes, but we can't guarantee it's null
1674 * terminated or not junk. */
1675 strncpy(text, (const char *) data, 12);
1677 /* remove insane chars */
1678 for (i = 0; text[i] != '\0'; i++) {
1679 if (text[i] == '\n' ||
1686 /* ensure string is printable */
1687 for (i = 0; text[i] != '\0'; i++) {
1688 if (!isprint(text[i])) {
1694 /* if the string is random junk, ignore the string */
1699 #define EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING 0xfe
1700 #define EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME 0xfc
1701 #define EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER 0xff
1702 #define EDID_OFFSET_DATA_BLOCKS 0x36
1703 #define EDID_OFFSET_LAST_BLOCK 0x6c
1704 #define EDID_OFFSET_PNPID 0x08
1705 #define EDID_OFFSET_SERIAL 0x0c
1708 edid_parse(struct drm_edid *edid, const uint8_t *data, size_t length)
1711 uint32_t serial_number;
1716 if (data[0] != 0x00 || data[1] != 0xff)
1719 /* decode the PNP ID from three 5 bit words packed into 2 bytes
1724 edid->pnp_id[0] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x7c) / 4) - 1;
1725 edid->pnp_id[1] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x3) * 8) + ((data[EDID_OFFSET_PNPID + 1] & 0xe0) / 32) - 1;
1726 edid->pnp_id[2] = 'A' + (data[EDID_OFFSET_PNPID + 1] & 0x1f) - 1;
1727 edid->pnp_id[3] = '\0';
1729 /* maybe there isn't a ASCII serial number descriptor, so use this instead */
1730 serial_number = (uint32_t) data[EDID_OFFSET_SERIAL + 0];
1731 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 1] * 0x100;
1732 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 2] * 0x10000;
1733 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 3] * 0x1000000;
1734 if (serial_number > 0)
1735 sprintf(edid->serial_number, "%lu", (unsigned long) serial_number);
1737 /* parse EDID data */
1738 for (i = EDID_OFFSET_DATA_BLOCKS;
1739 i <= EDID_OFFSET_LAST_BLOCK;
1741 /* ignore pixel clock data */
1747 /* any useful blocks? */
1748 if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME) {
1749 edid_parse_string(&data[i+5],
1750 edid->monitor_name);
1751 } else if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER) {
1752 edid_parse_string(&data[i+5],
1753 edid->serial_number);
1754 } else if (data[i+3] == EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING) {
1755 edid_parse_string(&data[i+5],
1763 find_and_parse_output_edid(struct drm_compositor *ec,
1764 struct drm_output *output,
1765 drmModeConnector *connector)
1767 drmModePropertyBlobPtr edid_blob = NULL;
1768 drmModePropertyPtr property;
1772 for (i = 0; i < connector->count_props && !edid_blob; i++) {
1773 property = drmModeGetProperty(ec->drm.fd, connector->props[i]);
1776 if ((property->flags & DRM_MODE_PROP_BLOB) &&
1777 !strcmp(property->name, "EDID")) {
1778 edid_blob = drmModeGetPropertyBlob(ec->drm.fd,
1779 connector->prop_values[i]);
1781 drmModeFreeProperty(property);
1786 rc = edid_parse(&output->edid,
1790 weston_log("EDID data '%s', '%s', '%s'\n",
1791 output->edid.pnp_id,
1792 output->edid.monitor_name,
1793 output->edid.serial_number);
1794 if (output->edid.pnp_id[0] != '\0')
1795 output->base.make = output->edid.pnp_id;
1796 if (output->edid.monitor_name[0] != '\0')
1797 output->base.model = output->edid.monitor_name;
1798 if (output->edid.serial_number[0] != '\0')
1799 output->base.serial_number = output->edid.serial_number;
1801 drmModeFreePropertyBlob(edid_blob);
1807 parse_modeline(const char *s, drmModeModeInfo *mode)
1813 mode->type = DRM_MODE_TYPE_USERDEF;
1819 if (sscanf(s, "%f %hd %hd %hd %hd %hd %hd %hd %hd %15s %15s",
1828 &mode->vtotal, hsync, vsync) != 11)
1831 mode->clock = fclock * 1000;
1832 if (strcmp(hsync, "+hsync") == 0)
1833 mode->flags |= DRM_MODE_FLAG_PHSYNC;
1834 else if (strcmp(hsync, "-hsync") == 0)
1835 mode->flags |= DRM_MODE_FLAG_NHSYNC;
1839 if (strcmp(vsync, "+vsync") == 0)
1840 mode->flags |= DRM_MODE_FLAG_PVSYNC;
1841 else if (strcmp(vsync, "-vsync") == 0)
1842 mode->flags |= DRM_MODE_FLAG_NVSYNC;
1850 parse_transform(const char *transform, const char *output_name)
1852 static const struct { const char *name; uint32_t token; } names[] = {
1853 { "normal", WL_OUTPUT_TRANSFORM_NORMAL },
1854 { "90", WL_OUTPUT_TRANSFORM_90 },
1855 { "180", WL_OUTPUT_TRANSFORM_180 },
1856 { "270", WL_OUTPUT_TRANSFORM_270 },
1857 { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED },
1858 { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
1859 { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
1860 { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
1864 for (i = 0; i < ARRAY_LENGTH(names); i++)
1865 if (strcmp(names[i].name, transform) == 0)
1866 return names[i].token;
1868 weston_log("Invalid transform \"%s\" for output %s\n",
1869 transform, output_name);
1871 return WL_OUTPUT_TRANSFORM_NORMAL;
1875 setup_output_seat_constraint(struct drm_compositor *ec,
1876 struct weston_output *output,
1879 if (strcmp(s, "") != 0) {
1880 struct udev_seat *seat;
1882 seat = udev_seat_get_named(&ec->base, s);
1884 udev_seat_link_output(seat, output);
1886 if (!seat->input.libinput)
1887 udev_input_init(&seat->input, &ec->base,
1889 else if (seat->input.suspended)
1890 udev_input_enable(&seat->input);
1892 if (seat->base.pointer)
1893 weston_pointer_clamp(seat->base.pointer,
1894 &seat->base.pointer->x,
1895 &seat->base.pointer->y);
1901 get_gbm_format_from_section(struct weston_config_section *section,
1902 uint32_t default_value,
1908 weston_config_section_get_string(section,
1909 "gbm-format", &s, NULL);
1912 *format = default_value;
1913 else if (strcmp(s, "xrgb8888") == 0)
1914 *format = GBM_FORMAT_XRGB8888;
1915 else if (strcmp(s, "rgb565") == 0)
1916 *format = GBM_FORMAT_RGB565;
1917 else if (strcmp(s, "xrgb2101010") == 0)
1918 *format = GBM_FORMAT_XRGB2101010;
1920 weston_log("fatal: unrecognized pixel format: %s\n", s);
1930 create_output_for_connector(struct drm_compositor *ec,
1931 drmModeRes *resources,
1932 drmModeConnector *connector,
1933 int x, int y, struct udev_device *drm_device)
1935 struct drm_output *output;
1936 struct drm_mode *drm_mode, *next, *preferred, *current, *configured, *best;
1937 struct weston_mode *m;
1938 struct weston_config_section *section;
1939 drmModeEncoder *encoder;
1940 drmModeModeInfo crtc_mode, modeline;
1942 int i, width, height, scale;
1944 const char *type_name;
1945 enum output_config config;
1949 i = find_crtc_for_connector(ec, resources, connector);
1951 weston_log("No usable crtc/encoder pair for connector.\n");
1955 output = zalloc(sizeof *output);
1959 output->base.subpixel = drm_subpixel_to_wayland(connector->subpixel);
1960 output->base.make = "unknown";
1961 output->base.model = "unknown";
1962 output->base.serial_number = "unknown";
1963 wl_list_init(&output->base.mode_list);
1965 if (connector->connector_type < ARRAY_LENGTH(connector_type_names))
1966 type_name = connector_type_names[connector->connector_type];
1968 type_name = "UNKNOWN";
1969 snprintf(name, 32, "%s%d", type_name, connector->connector_type_id);
1970 output->base.name = strdup(name);
1972 section = weston_config_get_section(ec->base.config, "output", "name",
1974 weston_config_section_get_string(section, "mode", &s, "preferred");
1975 if (strcmp(s, "off") == 0)
1976 config = OUTPUT_CONFIG_OFF;
1977 else if (strcmp(s, "preferred") == 0)
1978 config = OUTPUT_CONFIG_PREFERRED;
1979 else if (strcmp(s, "current") == 0)
1980 config = OUTPUT_CONFIG_CURRENT;
1981 else if (sscanf(s, "%dx%d", &width, &height) == 2)
1982 config = OUTPUT_CONFIG_MODE;
1983 else if (parse_modeline(s, &modeline) == 0)
1984 config = OUTPUT_CONFIG_MODELINE;
1986 weston_log("Invalid mode \"%s\" for output %s\n",
1987 s, output->base.name);
1988 config = OUTPUT_CONFIG_PREFERRED;
1992 weston_config_section_get_int(section, "scale", &scale, 1);
1993 weston_config_section_get_string(section, "transform", &s, "normal");
1994 transform = parse_transform(s, output->base.name);
1996 weston_config_section_get_int(section, "default_output",
1997 &default_output, 0);
1999 if (get_gbm_format_from_section(section,
2001 &output->format) == -1)
2002 output->format = ec->format;
2004 weston_config_section_get_string(section, "seat", &s, "");
2005 output->base.seat_data.seatname = strdup(s);
2006 weston_log("output %p belongs to seat '%s'\n", output,
2007 output->base.seat_data.seatname);
2008 setup_output_seat_constraint(ec, &output->base, s);
2011 output->crtc_id = resources->crtcs[i];
2013 ec->crtc_allocator |= (1 << output->crtc_id);
2014 output->connector_id = connector->connector_id;
2015 ec->connector_allocator |= (1 << output->connector_id);
2017 output->original_crtc = drmModeGetCrtc(ec->drm.fd, output->crtc_id);
2018 output->dpms_prop = drm_get_prop(ec->drm.fd, connector, "DPMS");
2020 /* Get the current mode on the crtc that's currently driving
2021 * this connector. */
2022 encoder = drmModeGetEncoder(ec->drm.fd, connector->encoder_id);
2023 memset(&crtc_mode, 0, sizeof crtc_mode);
2024 if (encoder != NULL) {
2025 crtc = drmModeGetCrtc(ec->drm.fd, encoder->crtc_id);
2026 drmModeFreeEncoder(encoder);
2029 if (crtc->mode_valid)
2030 crtc_mode = crtc->mode;
2031 drmModeFreeCrtc(crtc);
2034 for (i = 0; i < connector->count_modes; i++) {
2035 drm_mode = drm_output_add_mode(output, &connector->modes[i]);
2040 if (config == OUTPUT_CONFIG_OFF) {
2041 weston_log("Disabling output %s\n", output->base.name);
2042 drmModeSetCrtc(ec->drm.fd, output->crtc_id,
2043 0, 0, 0, 0, 0, NULL);
2052 wl_list_for_each_reverse(drm_mode, &output->base.mode_list, base.link) {
2053 if (config == OUTPUT_CONFIG_MODE &&
2054 width == drm_mode->base.width &&
2055 height == drm_mode->base.height)
2056 configured = drm_mode;
2057 if (!memcmp(&crtc_mode, &drm_mode->mode_info, sizeof crtc_mode))
2059 if (drm_mode->base.flags & WL_OUTPUT_MODE_PREFERRED)
2060 preferred = drm_mode;
2064 if (config == OUTPUT_CONFIG_MODELINE) {
2065 configured = drm_output_add_mode(output, &modeline);
2070 if (current == NULL && crtc_mode.clock != 0) {
2071 current = drm_output_add_mode(output, &crtc_mode);
2076 if (config == OUTPUT_CONFIG_CURRENT)
2077 configured = current;
2079 if (option_current_mode && current)
2080 output->base.current_mode = ¤t->base;
2081 else if (configured)
2082 output->base.current_mode = &configured->base;
2084 output->base.current_mode = &preferred->base;
2086 output->base.current_mode = ¤t->base;
2088 output->base.current_mode = &best->base;
2090 if (output->base.current_mode == NULL) {
2091 weston_log("no available modes for %s\n", output->base.name);
2095 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
2097 weston_output_init(&output->base, &ec->base, x, y,
2098 connector->mmWidth, connector->mmHeight,
2101 if (ec->use_pixman) {
2102 if (drm_output_init_pixman(output, ec) < 0) {
2103 weston_log("Failed to init output pixman state\n");
2106 } else if (drm_output_init_egl(output, ec) < 0) {
2107 weston_log("Failed to init output gl state\n");
2111 output->backlight = backlight_init(drm_device,
2112 connector->connector_type);
2113 if (output->backlight) {
2114 weston_log("Initialized backlight, device %s\n",
2115 output->backlight->path);
2116 output->base.set_backlight = drm_set_backlight;
2117 output->base.backlight_current = drm_get_backlight(output);
2119 weston_log("Failed to initialize backlight\n");
2122 wl_list_insert(ec->base.output_list.prev, &output->base.link);
2124 ec->base.default_output = &output->base;
2126 find_and_parse_output_edid(ec, output, connector);
2127 if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS)
2128 output->base.connection_internal = 1;
2130 output->base.start_repaint_loop = drm_output_start_repaint_loop;
2131 output->base.repaint = drm_output_repaint;
2132 output->base.destroy = drm_output_destroy;
2133 output->base.assign_planes = drm_assign_planes;
2134 output->base.set_dpms = drm_set_dpms;
2135 output->base.switch_mode = drm_output_switch_mode;
2137 output->base.gamma_size = output->original_crtc->gamma_size;
2138 output->base.set_gamma = drm_output_set_gamma;
2140 weston_plane_init(&output->cursor_plane, &ec->base, 0, 0);
2141 weston_plane_init(&output->fb_plane, &ec->base, 0, 0);
2143 weston_compositor_stack_plane(&ec->base, &output->cursor_plane, NULL);
2144 weston_compositor_stack_plane(&ec->base, &output->fb_plane,
2145 &ec->base.primary_plane);
2147 weston_log("Output %s, (connector %d, crtc %d)\n",
2148 output->base.name, output->connector_id, output->crtc_id);
2149 wl_list_for_each(m, &output->base.mode_list, link)
2150 weston_log_continue(STAMP_SPACE "mode %dx%d@%.1f%s%s%s\n",
2151 m->width, m->height, m->refresh / 1000.0,
2152 m->flags & WL_OUTPUT_MODE_PREFERRED ?
2154 m->flags & WL_OUTPUT_MODE_CURRENT ?
2156 connector->count_modes == 0 ?
2162 weston_output_destroy(&output->base);
2164 wl_list_for_each_safe(drm_mode, next, &output->base.mode_list,
2166 wl_list_remove(&drm_mode->base.link);
2170 drmModeFreeCrtc(output->original_crtc);
2171 ec->crtc_allocator &= ~(1 << output->crtc_id);
2172 ec->connector_allocator &= ~(1 << output->connector_id);
2179 create_sprites(struct drm_compositor *ec)
2181 struct drm_sprite *sprite;
2182 drmModePlaneRes *plane_res;
2183 drmModePlane *plane;
2186 plane_res = drmModeGetPlaneResources(ec->drm.fd);
2188 weston_log("failed to get plane resources: %s\n",
2193 for (i = 0; i < plane_res->count_planes; i++) {
2194 plane = drmModeGetPlane(ec->drm.fd, plane_res->planes[i]);
2198 sprite = zalloc(sizeof(*sprite) + ((sizeof(uint32_t)) *
2199 plane->count_formats));
2201 weston_log("%s: out of memory\n",
2203 drmModeFreePlane(plane);
2207 sprite->possible_crtcs = plane->possible_crtcs;
2208 sprite->plane_id = plane->plane_id;
2209 sprite->current = NULL;
2210 sprite->next = NULL;
2211 sprite->compositor = ec;
2212 sprite->count_formats = plane->count_formats;
2213 memcpy(sprite->formats, plane->formats,
2214 plane->count_formats * sizeof(plane->formats[0]));
2215 drmModeFreePlane(plane);
2216 weston_plane_init(&sprite->plane, &ec->base, 0, 0);
2217 weston_compositor_stack_plane(&ec->base, &sprite->plane,
2218 &ec->base.primary_plane);
2220 wl_list_insert(&ec->sprite_list, &sprite->link);
2223 drmModeFreePlaneResources(plane_res);
2227 destroy_sprites(struct drm_compositor *compositor)
2229 struct drm_sprite *sprite, *next;
2230 struct drm_output *output;
2232 output = container_of(compositor->base.output_list.next,
2233 struct drm_output, base.link);
2235 wl_list_for_each_safe(sprite, next, &compositor->sprite_list, link) {
2236 drmModeSetPlane(compositor->drm.fd,
2238 output->crtc_id, 0, 0,
2239 0, 0, 0, 0, 0, 0, 0, 0);
2240 drm_output_release_fb(output, sprite->current);
2241 drm_output_release_fb(output, sprite->next);
2242 weston_plane_release(&sprite->plane);
2248 create_outputs(struct drm_compositor *ec, uint32_t option_connector,
2249 struct udev_device *drm_device)
2251 drmModeConnector *connector;
2252 drmModeRes *resources;
2256 resources = drmModeGetResources(ec->drm.fd);
2258 weston_log("drmModeGetResources failed\n");
2262 ec->crtcs = calloc(resources->count_crtcs, sizeof(uint32_t));
2264 drmModeFreeResources(resources);
2268 ec->min_width = resources->min_width;
2269 ec->max_width = resources->max_width;
2270 ec->min_height = resources->min_height;
2271 ec->max_height = resources->max_height;
2273 ec->num_crtcs = resources->count_crtcs;
2274 memcpy(ec->crtcs, resources->crtcs, sizeof(uint32_t) * ec->num_crtcs);
2276 for (i = 0; i < resources->count_connectors; i++) {
2277 connector = drmModeGetConnector(ec->drm.fd,
2278 resources->connectors[i]);
2279 if (connector == NULL)
2282 if (connector->connection == DRM_MODE_CONNECTED &&
2283 (option_connector == 0 ||
2284 connector->connector_id == option_connector)) {
2285 if (create_output_for_connector(ec, resources,
2288 drmModeFreeConnector(connector);
2292 x += container_of(ec->base.output_list.prev,
2293 struct weston_output,
2297 drmModeFreeConnector(connector);
2300 if (wl_list_empty(&ec->base.output_list)) {
2301 weston_log("No currently active connector found.\n");
2302 drmModeFreeResources(resources);
2306 drmModeFreeResources(resources);
2312 update_outputs(struct drm_compositor *ec, struct udev_device *drm_device)
2314 drmModeConnector *connector;
2315 drmModeRes *resources;
2316 struct drm_output *output, *next;
2318 uint32_t connected = 0, disconnects = 0;
2321 resources = drmModeGetResources(ec->drm.fd);
2323 weston_log("drmModeGetResources failed\n");
2327 /* collect new connects */
2328 for (i = 0; i < resources->count_connectors; i++) {
2329 int connector_id = resources->connectors[i];
2331 connector = drmModeGetConnector(ec->drm.fd, connector_id);
2332 if (connector == NULL)
2335 if (connector->connection != DRM_MODE_CONNECTED) {
2336 drmModeFreeConnector(connector);
2340 connected |= (1 << connector_id);
2342 if (!(ec->connector_allocator & (1 << connector_id))) {
2343 struct weston_output *last =
2344 container_of(ec->base.output_list.prev,
2345 struct weston_output, link);
2347 /* XXX: not yet needed, we die with 0 outputs */
2348 if (!wl_list_empty(&ec->base.output_list))
2349 x = last->x + last->width;
2353 create_output_for_connector(ec, resources,
2356 weston_log("connector %d connected\n", connector_id);
2359 drmModeFreeConnector(connector);
2361 drmModeFreeResources(resources);
2363 disconnects = ec->connector_allocator & ~connected;
2365 wl_list_for_each_safe(output, next, &ec->base.output_list,
2367 if (disconnects & (1 << output->connector_id)) {
2368 disconnects &= ~(1 << output->connector_id);
2369 weston_log("connector %d disconnected\n",
2370 output->connector_id);
2371 drm_output_destroy(&output->base);
2376 /* FIXME: handle zero outputs, without terminating */
2377 if (ec->connector_allocator == 0)
2378 wl_display_terminate(ec->base.wl_display);
2382 udev_event_is_hotplug(struct drm_compositor *ec, struct udev_device *device)
2387 sysnum = udev_device_get_sysnum(device);
2388 if (!sysnum || atoi(sysnum) != ec->drm.id)
2391 val = udev_device_get_property_value(device, "HOTPLUG");
2395 return strcmp(val, "1") == 0;
2399 udev_drm_event(int fd, uint32_t mask, void *data)
2401 struct drm_compositor *ec = data;
2402 struct udev_device *event;
2404 event = udev_monitor_receive_device(ec->udev_monitor);
2406 if (udev_event_is_hotplug(ec, event))
2407 update_outputs(ec, event);
2409 udev_device_unref(event);
2415 drm_restore(struct weston_compositor *ec)
2417 weston_launcher_restore(ec->launcher);
2421 drm_destroy(struct weston_compositor *ec)
2423 struct drm_compositor *d = (struct drm_compositor *) ec;
2425 wl_event_source_remove(d->udev_drm_source);
2426 wl_event_source_remove(d->drm_source);
2430 weston_compositor_shutdown(ec);
2433 gbm_device_destroy(d->gbm);
2435 weston_launcher_destroy(d->base.launcher);
2438 free (d->main_seat);
2443 drm_compositor_set_modes(struct drm_compositor *compositor)
2445 struct drm_output *output;
2446 struct drm_mode *drm_mode;
2449 wl_list_for_each(output, &compositor->base.output_list, base.link) {
2450 if (!output->current) {
2451 /* If something that would cause the output to
2452 * switch mode happened while in another vt, we
2453 * might not have a current drm_fb. In that case,
2454 * schedule a repaint and let drm_output_repaint
2455 * handle setting the mode. */
2456 weston_output_schedule_repaint(&output->base);
2460 drm_mode = (struct drm_mode *) output->base.current_mode;
2461 ret = drmModeSetCrtc(compositor->drm.fd, output->crtc_id,
2462 output->current->fb_id, 0, 0,
2463 &output->connector_id, 1,
2464 &drm_mode->mode_info);
2467 "failed to set mode %dx%d for output at %d,%d: %m\n",
2468 drm_mode->base.width, drm_mode->base.height,
2469 output->base.x, output->base.y);
2475 session_notify(struct wl_listener *listener, void *data)
2477 struct weston_compositor *compositor = data;
2478 struct drm_compositor *ec = data;
2479 struct drm_sprite *sprite;
2480 struct drm_output *output;
2481 struct udev_seat *useat;
2482 struct weston_seat *seat, *next;
2484 if (ec->base.session_active) {
2485 weston_log("activating session\n");
2486 compositor->state = ec->prev_state;
2487 drm_compositor_set_modes(ec);
2488 weston_compositor_damage_all(compositor);
2489 wl_list_for_each_safe(seat, next, &ec->base.seat_list, link) {
2490 useat = container_of(seat, struct udev_seat, base);
2491 udev_input_enable(&useat->input);
2494 weston_log("deactivating session\n");
2495 wl_list_for_each_safe(seat, next, &ec->base.seat_list, link) {
2496 useat = container_of(seat, struct udev_seat, base);
2497 udev_input_disable(&useat->input);
2500 ec->prev_state = compositor->state;
2501 weston_compositor_offscreen(compositor);
2503 /* If we have a repaint scheduled (either from a
2504 * pending pageflip or the idle handler), make sure we
2505 * cancel that so we don't try to pageflip when we're
2506 * vt switched away. The OFFSCREEN state will prevent
2507 * further attemps at repainting. When we switch
2508 * back, we schedule a repaint, which will process
2509 * pending frame callbacks. */
2511 wl_list_for_each(output, &ec->base.output_list, base.link) {
2512 output->base.repaint_needed = 0;
2513 drmModeSetCursor(ec->drm.fd, output->crtc_id, 0, 0, 0);
2516 output = container_of(ec->base.output_list.next,
2517 struct drm_output, base.link);
2519 wl_list_for_each(sprite, &ec->sprite_list, link)
2520 drmModeSetPlane(ec->drm.fd,
2522 output->crtc_id, 0, 0,
2523 0, 0, 0, 0, 0, 0, 0, 0);
2528 switch_vt_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
2530 struct weston_compositor *compositor = data;
2532 weston_launcher_activate_vt(compositor->launcher, key - KEY_F1 + 1);
2537 * Some systems may have multiple DRM devices attached to a single seat. This
2538 * function loops over all devices and tries to find a PCI device with the
2539 * boot_vga sysfs attribute set to 1.
2540 * If no such device is found, the first DRM device reported by udev is used.
2542 static struct udev_device*
2543 find_primary_gpu(struct drm_compositor *ec, const char *seat)
2545 struct udev_enumerate *e;
2546 struct udev_list_entry *entry;
2547 const char *path, *device_seat, *id;
2548 struct udev_device *device, *drm_device, *pci;
2550 e = udev_enumerate_new(ec->udev);
2551 udev_enumerate_add_match_subsystem(e, "drm");
2552 udev_enumerate_add_match_sysname(e, "card[0-9]*");
2554 udev_enumerate_scan_devices(e);
2556 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
2557 path = udev_list_entry_get_name(entry);
2558 device = udev_device_new_from_syspath(ec->udev, path);
2561 device_seat = udev_device_get_property_value(device, "ID_SEAT");
2563 device_seat = default_seat;
2564 if (strcmp(device_seat, seat)) {
2565 udev_device_unref(device);
2569 pci = udev_device_get_parent_with_subsystem_devtype(device,
2572 id = udev_device_get_sysattr_value(pci, "boot_vga");
2573 if (id && !strcmp(id, "1")) {
2575 udev_device_unref(drm_device);
2576 drm_device = device;
2582 drm_device = device;
2584 udev_device_unref(device);
2587 udev_enumerate_unref(e);
2592 planes_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
2594 struct drm_compositor *c = data;
2598 c->cursors_are_broken ^= 1;
2601 c->sprites_are_broken ^= 1;
2604 c->sprites_hidden ^= 1;
2611 #ifdef BUILD_VAAPI_RECORDER
2613 recorder_destroy(struct drm_output *output)
2615 vaapi_recorder_destroy(output->recorder);
2616 output->recorder = NULL;
2618 output->base.disable_planes--;
2620 wl_list_remove(&output->recorder_frame_listener.link);
2621 weston_log("[libva recorder] done\n");
2625 recorder_frame_notify(struct wl_listener *listener, void *data)
2627 struct drm_output *output;
2628 struct drm_compositor *c;
2631 output = container_of(listener, struct drm_output,
2632 recorder_frame_listener);
2633 c = (struct drm_compositor *) output->base.compositor;
2635 if (!output->recorder)
2638 ret = drmPrimeHandleToFD(c->drm.fd, output->current->handle,
2641 weston_log("[libva recorder] "
2642 "failed to create prime fd for front buffer\n");
2646 ret = vaapi_recorder_frame(output->recorder, fd,
2647 output->current->stride);
2649 weston_log("[libva recorder] aborted: %m\n");
2650 recorder_destroy(output);
2655 create_recorder(struct drm_compositor *c, int width, int height,
2656 const char *filename)
2661 fd = open(c->drm.filename, O_RDWR | O_CLOEXEC);
2665 drmGetMagic(fd, &magic);
2666 drmAuthMagic(c->drm.fd, magic);
2668 return vaapi_recorder_create(fd, width, height, filename);
2672 recorder_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
2675 struct drm_compositor *c = data;
2676 struct drm_output *output;
2679 output = container_of(c->base.output_list.next,
2680 struct drm_output, base.link);
2682 if (!output->recorder) {
2683 if (output->format != GBM_FORMAT_XRGB8888) {
2684 weston_log("failed to start vaapi recorder: "
2685 "output format not supported\n");
2689 width = output->base.current_mode->width;
2690 height = output->base.current_mode->height;
2693 create_recorder(c, width, height, "capture.h264");
2694 if (!output->recorder) {
2695 weston_log("failed to create vaapi recorder\n");
2699 output->base.disable_planes++;
2701 output->recorder_frame_listener.notify = recorder_frame_notify;
2702 wl_signal_add(&output->base.frame_signal,
2703 &output->recorder_frame_listener);
2705 weston_output_schedule_repaint(&output->base);
2707 weston_log("[libva recorder] initialized\n");
2709 recorder_destroy(output);
2714 recorder_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
2717 weston_log("Compiled without libva support\n");
2722 switch_to_gl_renderer(struct drm_compositor *c)
2724 struct drm_output *output;
2729 weston_log("Switching to GL renderer\n");
2731 c->gbm = create_gbm_device(c->drm.fd);
2733 weston_log("Failed to create gbm device. "
2734 "Aborting renderer switch\n");
2738 wl_list_for_each(output, &c->base.output_list, base.link)
2739 pixman_renderer_output_destroy(&output->base);
2741 c->base.renderer->destroy(&c->base);
2743 if (drm_compositor_create_gl_renderer(c) < 0) {
2744 gbm_device_destroy(c->gbm);
2745 weston_log("Failed to create GL renderer. Quitting.\n");
2746 /* FIXME: we need a function to shutdown cleanly */
2750 wl_list_for_each(output, &c->base.output_list, base.link) {
2751 /* Workaround page flip not setting the tiling mode on BYT */
2752 output->force_modeset = 1;
2753 drm_output_init_egl(output, c);
2760 renderer_switch_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
2763 struct drm_compositor *c = (struct drm_compositor *) seat->compositor;
2765 switch_to_gl_renderer(c);
2769 create_seats(struct drm_compositor *ec, int connector,
2770 struct udev_device *drm_device)
2772 struct udev_seat *seat = udev_seat_get_named(&ec->base, ec->main_seat);
2773 if (seat && udev_input_init(&seat->input, &ec->base,
2774 ec->udev, ec->main_seat) < 0) {
2775 weston_log("failed to create input devices\n");
2779 if (create_outputs(ec, connector, drm_device) < 0)
2785 static struct weston_compositor *
2786 drm_compositor_create(struct wl_display *display,
2787 struct drm_parameters *param,
2788 int *argc, char *argv[],
2789 struct weston_config *config)
2791 struct drm_compositor *ec;
2792 struct weston_config_section *section;
2793 struct udev_device *drm_device;
2794 struct wl_event_loop *loop;
2798 weston_log("initializing drm backend\n");
2800 ec = zalloc(sizeof *ec);
2804 /* KMS support for sprites is not complete yet, so disable the
2805 * functionality for now. */
2806 ec->sprites_are_broken = 1;
2808 section = weston_config_get_section(config, "core", NULL, NULL);
2809 if (get_gbm_format_from_section(section,
2810 GBM_FORMAT_XRGB8888,
2814 ec->use_pixman = param->use_pixman;
2816 if (weston_compositor_init(&ec->base, display, argc, argv,
2818 weston_log("%s failed\n", __func__);
2822 /* Check if we run drm-backend using weston-launch */
2823 ec->base.launcher = weston_launcher_connect(&ec->base, param->tty,
2825 if (ec->base.launcher == NULL) {
2826 weston_log("fatal: drm backend should be run "
2827 "using weston-launch binary or as root\n");
2828 goto err_compositor;
2831 ec->udev = udev_new();
2832 if (ec->udev == NULL) {
2833 weston_log("failed to initialize udev context\n");
2837 ec->base.wl_display = display;
2838 ec->session_listener.notify = session_notify;
2839 wl_signal_add(&ec->base.session_signal, &ec->session_listener);
2841 drm_device = find_primary_gpu(ec, param->seat_id);
2842 if (drm_device == NULL) {
2843 weston_log("no drm device found\n");
2846 path = udev_device_get_syspath(drm_device);
2848 if (init_drm(ec, drm_device) < 0) {
2849 weston_log("failed to initialize kms\n");
2853 if (ec->use_pixman) {
2854 if (init_pixman(ec) < 0) {
2855 weston_log("failed to initialize pixman renderer\n");
2859 if (init_egl(ec) < 0) {
2860 weston_log("failed to initialize egl\n");
2865 ec->base.destroy = drm_destroy;
2866 ec->base.restore = drm_restore;
2868 ec->prev_state = WESTON_COMPOSITOR_ACTIVE;
2870 for (key = KEY_F1; key < KEY_F9; key++)
2871 weston_compositor_add_key_binding(&ec->base, key,
2872 MODIFIER_CTRL | MODIFIER_ALT,
2873 switch_vt_binding, ec);
2875 wl_list_init(&ec->sprite_list);
2878 ec->main_seat = strdup(param->seat_id);
2879 if (create_seats(ec, param->connector, drm_device) < 0) {
2880 weston_log("failed to create output for %s\n", path);
2884 /* A this point we have some idea of whether or not we have a working
2886 if (!ec->cursors_are_broken)
2887 ec->base.capabilities |= WESTON_CAP_CURSOR_PLANE;
2891 loop = wl_display_get_event_loop(ec->base.wl_display);
2893 wl_event_loop_add_fd(loop, ec->drm.fd,
2894 WL_EVENT_READABLE, on_drm_input, ec);
2896 ec->udev_monitor = udev_monitor_new_from_netlink(ec->udev, "udev");
2897 if (ec->udev_monitor == NULL) {
2898 weston_log("failed to intialize udev monitor\n");
2899 goto err_drm_source;
2901 udev_monitor_filter_add_match_subsystem_devtype(ec->udev_monitor,
2903 ec->udev_drm_source =
2904 wl_event_loop_add_fd(loop,
2905 udev_monitor_get_fd(ec->udev_monitor),
2906 WL_EVENT_READABLE, udev_drm_event, ec);
2908 if (udev_monitor_enable_receiving(ec->udev_monitor) < 0) {
2909 weston_log("failed to enable udev-monitor receiving\n");
2910 goto err_udev_monitor;
2913 udev_device_unref(drm_device);
2915 weston_compositor_add_debug_binding(&ec->base, KEY_O,
2916 planes_binding, ec);
2917 weston_compositor_add_debug_binding(&ec->base, KEY_C,
2918 planes_binding, ec);
2919 weston_compositor_add_debug_binding(&ec->base, KEY_V,
2920 planes_binding, ec);
2921 weston_compositor_add_debug_binding(&ec->base, KEY_Q,
2922 recorder_binding, ec);
2923 weston_compositor_add_debug_binding(&ec->base, KEY_W,
2924 renderer_switch_binding, ec);
2929 wl_event_source_remove(ec->udev_drm_source);
2930 udev_monitor_unref(ec->udev_monitor);
2932 wl_event_source_remove(ec->drm_source);
2934 ec->base.renderer->destroy(&ec->base);
2935 gbm_device_destroy(ec->gbm);
2936 destroy_sprites(ec);
2938 udev_device_unref(drm_device);
2940 weston_launcher_destroy(ec->base.launcher);
2942 udev_unref(ec->udev);
2944 weston_compositor_shutdown(&ec->base);
2946 free(ec->main_seat);
2951 WL_EXPORT struct weston_compositor *
2952 backend_init(struct wl_display *display, int *argc, char *argv[],
2953 struct weston_config *config)
2955 struct drm_parameters param = { 0, };
2957 const struct weston_option drm_options[] = {
2958 { WESTON_OPTION_INTEGER, "connector", 0, ¶m.connector },
2959 { WESTON_OPTION_STRING, "seat", 0, ¶m.seat_id },
2960 { WESTON_OPTION_INTEGER, "tty", 0, ¶m.tty },
2961 { WESTON_OPTION_BOOLEAN, "current-mode", 0, &option_current_mode },
2962 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, ¶m.use_pixman },
2965 param.seat_id = default_seat;
2967 parse_options(drm_options, ARRAY_LENGTH(drm_options), argc, argv);
2969 return drm_compositor_create(display, ¶m, argc, argv, config);