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.
37 #include <linux/input.h>
43 #include <xf86drmMode.h>
44 #include <drm_fourcc.h>
47 #include <libbacklight.h>
50 #include "compositor.h"
51 #include "gl-renderer.h"
52 #include "pixman-renderer.h"
53 #include "udev-seat.h"
54 #include "launcher-util.h"
56 #ifndef DRM_CAP_TIMESTAMP_MONOTONIC
57 #define DRM_CAP_TIMESTAMP_MONOTONIC 0x6
60 static int option_current_mode = 0;
61 static char *output_name;
62 static char *output_mode;
63 static char *output_transform;
64 static struct wl_list configured_output_list;
67 OUTPUT_CONFIG_INVALID = 0,
69 OUTPUT_CONFIG_PREFERRED,
70 OUTPUT_CONFIG_CURRENT,
72 OUTPUT_CONFIG_MODELINE
75 struct drm_configured_output {
79 int32_t width, height;
80 drmModeModeInfo crtc_mode;
81 enum output_config config;
85 struct drm_compositor {
86 struct weston_compositor base;
89 struct wl_event_source *drm_source;
91 struct udev_monitor *udev_monitor;
92 struct wl_event_source *udev_drm_source;
98 struct gbm_device *gbm;
101 uint32_t crtc_allocator;
102 uint32_t connector_allocator;
105 /* we need these parameters in order to not fail drmModeAddFB2()
106 * due to out of bounds dimensions, and then mistakenly set
107 * sprites_are_broken:
109 uint32_t min_width, max_width;
110 uint32_t min_height, max_height;
113 struct wl_list sprite_list;
114 int sprites_are_broken;
117 int cursors_are_broken;
127 struct weston_mode base;
128 drmModeModeInfo mode_info;
134 struct drm_output *output;
135 uint32_t fb_id, stride, handle, size;
137 int is_client_buffer;
138 struct weston_buffer_reference buffer_ref;
140 /* Used by gbm fbs */
143 /* Used by dumb fbs */
149 char monitor_name[13];
151 char serial_number[13];
155 struct weston_output base;
159 uint32_t connector_id;
160 drmModeCrtcPtr original_crtc;
161 struct drm_edid edid;
164 int page_flip_pending;
166 struct gbm_surface *surface;
167 struct gbm_bo *cursor_bo[2];
168 struct weston_plane cursor_plane;
169 struct weston_plane fb_plane;
170 struct weston_surface *cursor_surface;
172 struct drm_fb *current, *next;
173 struct backlight *backlight;
175 struct drm_fb *dumb[2];
176 pixman_image_t *image[2];
178 pixman_region32_t previous_damage;
182 * An output has a primary display plane plus zero or more sprites for
183 * blending display contents.
188 struct weston_plane plane;
190 struct drm_fb *current, *next;
191 struct drm_output *output;
192 struct drm_compositor *compositor;
194 uint32_t possible_crtcs;
196 uint32_t count_formats;
198 int32_t src_x, src_y;
199 uint32_t src_w, src_h;
200 uint32_t dest_x, dest_y;
201 uint32_t dest_w, dest_h;
206 static const char default_seat[] = "seat0";
209 drm_output_set_cursor(struct drm_output *output);
212 drm_sprite_crtc_supported(struct weston_output *output_base, uint32_t supported)
214 struct weston_compositor *ec = output_base->compositor;
215 struct drm_compositor *c =(struct drm_compositor *) ec;
216 struct drm_output *output = (struct drm_output *) output_base;
219 for (crtc = 0; crtc < c->num_crtcs; crtc++) {
220 if (c->crtcs[crtc] != output->crtc_id)
223 if (supported & (1 << crtc))
231 drm_fb_destroy_callback(struct gbm_bo *bo, void *data)
233 struct drm_fb *fb = data;
234 struct gbm_device *gbm = gbm_bo_get_device(bo);
237 drmModeRmFB(gbm_device_get_fd(gbm), fb->fb_id);
239 weston_buffer_reference(&fb->buffer_ref, NULL);
244 static struct drm_fb *
245 drm_fb_create_dumb(struct drm_compositor *ec, unsigned width, unsigned height)
250 struct drm_mode_create_dumb create_arg;
251 struct drm_mode_destroy_dumb destroy_arg;
252 struct drm_mode_map_dumb map_arg;
254 fb = calloc(1, sizeof *fb);
259 create_arg.width = width;
260 create_arg.height = height;
262 ret = drmIoctl(ec->drm.fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_arg);
266 fb->handle = create_arg.handle;
267 fb->stride = create_arg.pitch;
268 fb->size = create_arg.size;
271 ret = drmModeAddFB(ec->drm.fd, width, height, 24, 32,
272 fb->stride, fb->handle, &fb->fb_id);
276 memset(&map_arg, 0, sizeof(map_arg));
277 map_arg.handle = fb->handle;
278 ret = drmIoctl(fb->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_arg);
282 fb->map = mmap(0, fb->size, PROT_WRITE,
283 MAP_SHARED, ec->drm.fd, map_arg.offset);
284 if (fb->map == MAP_FAILED)
290 drmModeRmFB(ec->drm.fd, fb->fb_id);
292 memset(&destroy_arg, 0, sizeof(destroy_arg));
293 destroy_arg.handle = create_arg.handle;
294 drmIoctl(ec->drm.fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
301 drm_fb_destroy_dumb(struct drm_fb *fb)
303 struct drm_mode_destroy_dumb destroy_arg;
309 drmModeRmFB(fb->fd, fb->fb_id);
311 weston_buffer_reference(&fb->buffer_ref, NULL);
313 munmap(fb->map, fb->size);
315 memset(&destroy_arg, 0, sizeof(destroy_arg));
316 destroy_arg.handle = fb->handle;
317 drmIoctl(fb->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
322 static struct drm_fb *
323 drm_fb_get_from_bo(struct gbm_bo *bo,
324 struct drm_compositor *compositor, uint32_t format)
326 struct drm_fb *fb = gbm_bo_get_user_data(bo);
327 uint32_t width, height;
328 uint32_t handles[4], pitches[4], offsets[4];
334 fb = calloc(1, sizeof *fb);
340 width = gbm_bo_get_width(bo);
341 height = gbm_bo_get_height(bo);
342 fb->stride = gbm_bo_get_stride(bo);
343 fb->handle = gbm_bo_get_handle(bo).u32;
344 fb->size = fb->stride * height;
345 fb->fd = compositor->drm.fd;
347 if (compositor->min_width > width || width > compositor->max_width ||
348 compositor->min_height > height ||
349 height > compositor->max_height) {
350 weston_log("bo geometry out of bounds\n");
356 if (format && !compositor->no_addfb2) {
357 handles[0] = fb->handle;
358 pitches[0] = fb->stride;
361 ret = drmModeAddFB2(compositor->drm.fd, width, height,
362 format, handles, pitches, offsets,
365 weston_log("addfb2 failed: %m\n");
366 compositor->no_addfb2 = 1;
367 compositor->sprites_are_broken = 1;
372 ret = drmModeAddFB(compositor->drm.fd, width, height, 24, 32,
373 fb->stride, fb->handle, &fb->fb_id);
376 weston_log("failed to create kms fb: %m\n");
380 gbm_bo_set_user_data(bo, fb, drm_fb_destroy_callback);
390 drm_fb_set_buffer(struct drm_fb *fb, struct wl_buffer *buffer)
392 assert(fb->buffer_ref.buffer == NULL);
394 fb->is_client_buffer = 1;
396 weston_buffer_reference(&fb->buffer_ref, buffer);
400 drm_output_release_fb(struct drm_output *output, struct drm_fb *fb)
406 (fb != output->dumb[0] && fb != output->dumb[1])) {
407 drm_fb_destroy_dumb(fb);
409 if (fb->is_client_buffer)
410 gbm_bo_destroy(fb->bo);
412 gbm_surface_release_buffer(output->surface,
413 output->current->bo);
418 drm_output_check_scanout_format(struct drm_output *output,
419 struct weston_surface *es, struct gbm_bo *bo)
424 format = gbm_bo_get_format(bo);
427 case GBM_FORMAT_XRGB8888:
429 case GBM_FORMAT_ARGB8888:
430 /* We can only scanout an ARGB buffer if the surface's
431 * opaque region covers the whole output */
432 pixman_region32_init(&r);
433 pixman_region32_subtract(&r, &output->base.region,
436 if (!pixman_region32_not_empty(&r))
437 format = GBM_FORMAT_XRGB8888;
441 pixman_region32_fini(&r);
449 static struct weston_plane *
450 drm_output_prepare_scanout_surface(struct weston_output *_output,
451 struct weston_surface *es)
453 struct drm_output *output = (struct drm_output *) _output;
454 struct drm_compositor *c =
455 (struct drm_compositor *) output->base.compositor;
456 struct wl_buffer *buffer = es->buffer_ref.buffer;
460 if (es->geometry.x != output->base.x ||
461 es->geometry.y != output->base.y ||
462 buffer == NULL || c->gbm == NULL ||
463 buffer->width != output->base.current->width ||
464 buffer->height != output->base.current->height ||
465 output->base.transform != es->buffer_transform ||
466 es->transform.enabled)
469 bo = gbm_bo_import(c->gbm, GBM_BO_IMPORT_WL_BUFFER,
470 buffer, GBM_BO_USE_SCANOUT);
472 /* Unable to use the buffer for scanout */
476 format = drm_output_check_scanout_format(output, es, bo);
482 output->next = drm_fb_get_from_bo(bo, c, format);
488 drm_fb_set_buffer(output->next, buffer);
490 return &output->fb_plane;
494 drm_output_render_gl(struct drm_output *output, pixman_region32_t *damage)
496 struct drm_compositor *c =
497 (struct drm_compositor *) output->base.compositor;
500 c->base.renderer->repaint_output(&output->base, damage);
502 bo = gbm_surface_lock_front_buffer(output->surface);
504 weston_log("failed to lock front buffer: %m\n");
508 output->next = drm_fb_get_from_bo(bo, c, GBM_FORMAT_XRGB8888);
510 weston_log("failed to get drm_fb for bo\n");
511 gbm_surface_release_buffer(output->surface, bo);
517 drm_output_render_pixman(struct drm_output *output, pixman_region32_t *damage)
519 struct weston_compositor *ec = output->base.compositor;
520 pixman_region32_t total_damage, previous_damage;
522 pixman_region32_init(&total_damage);
523 pixman_region32_init(&previous_damage);
525 pixman_region32_copy(&previous_damage, damage);
527 pixman_region32_union(&total_damage, damage, &output->previous_damage);
528 pixman_region32_copy(&output->previous_damage, &previous_damage);
530 output->current_image ^= 1;
532 output->next = output->dumb[output->current_image];
533 pixman_renderer_output_set_buffer(&output->base,
534 output->image[output->current_image]);
536 ec->renderer->repaint_output(&output->base, &total_damage);
538 pixman_region32_fini(&total_damage);
539 pixman_region32_fini(&previous_damage);
543 drm_output_render(struct drm_output *output, pixman_region32_t *damage)
545 struct drm_compositor *c =
546 (struct drm_compositor *) output->base.compositor;
549 drm_output_render_pixman(output, damage);
551 drm_output_render_gl(output, damage);
553 pixman_region32_subtract(&c->base.primary_plane.damage,
554 &c->base.primary_plane.damage, damage);
558 drm_output_set_gamma(struct weston_output *output_base,
559 uint16_t size, uint16_t *r, uint16_t *g, uint16_t *b)
562 struct drm_output *output = (struct drm_output *) output_base;
563 struct drm_compositor *compositor = (struct drm_compositor *) output->base.compositor;
566 if (output_base->gamma_size != size)
568 if (!output->original_crtc)
571 rc = drmModeCrtcSetGamma(compositor->drm.fd,
575 weston_log("set gamma failed: %m\n");
579 drm_output_repaint(struct weston_output *output_base,
580 pixman_region32_t *damage)
582 struct drm_output *output = (struct drm_output *) output_base;
583 struct drm_compositor *compositor =
584 (struct drm_compositor *) output->base.compositor;
585 struct drm_sprite *s;
586 struct drm_mode *mode;
590 drm_output_render(output, damage);
594 mode = container_of(output->base.current, struct drm_mode, base);
595 if (!output->current) {
596 ret = drmModeSetCrtc(compositor->drm.fd, output->crtc_id,
597 output->next->fb_id, 0, 0,
598 &output->connector_id, 1,
601 weston_log("set mode failed: %m\n");
606 if (drmModePageFlip(compositor->drm.fd, output->crtc_id,
608 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
609 weston_log("queueing pageflip failed: %m\n");
613 output->page_flip_pending = 1;
615 drm_output_set_cursor(output);
618 * Now, update all the sprite surfaces
620 wl_list_for_each(s, &compositor->sprite_list, link) {
621 uint32_t flags = 0, fb_id = 0;
623 .request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT,
624 .request.sequence = 1,
627 if ((!s->current && !s->next) ||
628 !drm_sprite_crtc_supported(output_base, s->possible_crtcs))
631 if (s->next && !compositor->sprites_hidden)
632 fb_id = s->next->fb_id;
634 ret = drmModeSetPlane(compositor->drm.fd, s->plane_id,
635 output->crtc_id, fb_id, flags,
636 s->dest_x, s->dest_y,
637 s->dest_w, s->dest_h,
641 weston_log("setplane failed: %d: %s\n",
642 ret, strerror(errno));
644 if (output->pipe > 0)
645 vbl.request.type |= DRM_VBLANK_SECONDARY;
648 * Queue a vblank signal so we know when the surface
649 * becomes active on the display or has been replaced.
651 vbl.request.signal = (unsigned long)s;
652 ret = drmWaitVBlank(compositor->drm.fd, &vbl);
654 weston_log("vblank event request failed: %d: %s\n",
655 ret, strerror(errno));
659 output->vblank_pending = 1;
666 drm_output_start_repaint_loop(struct weston_output *output_base)
668 struct drm_output *output = (struct drm_output *) output_base;
669 struct drm_compositor *compositor = (struct drm_compositor *)
670 output_base->compositor;
675 if (!output->current) {
676 /* We can't page flip if there's no mode set */
679 clock_gettime(compositor->clock, &ts);
680 msec = ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
681 weston_output_finish_frame(output_base, msec);
685 fb_id = output->current->fb_id;
687 if (drmModePageFlip(compositor->drm.fd, output->crtc_id, fb_id,
688 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
689 weston_log("queueing pageflip failed: %m\n");
695 vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec,
698 struct drm_sprite *s = (struct drm_sprite *)data;
699 struct drm_output *output = s->output;
702 output->vblank_pending = 0;
704 drm_output_release_fb(output, s->current);
705 s->current = s->next;
708 if (!output->page_flip_pending) {
709 msecs = sec * 1000 + usec / 1000;
710 weston_output_finish_frame(&output->base, msecs);
715 page_flip_handler(int fd, unsigned int frame,
716 unsigned int sec, unsigned int usec, void *data)
718 struct drm_output *output = (struct drm_output *) data;
721 /* We don't set page_flip_pending on start_repaint_loop, in that case
722 * we just want to page flip to the current buffer to get an accurate
724 if (output->page_flip_pending) {
725 drm_output_release_fb(output, output->current);
726 output->current = output->next;
730 output->page_flip_pending = 0;
732 if (!output->vblank_pending) {
733 msecs = sec * 1000 + usec / 1000;
734 weston_output_finish_frame(&output->base, msecs);
739 drm_output_check_sprite_format(struct drm_sprite *s,
740 struct weston_surface *es, struct gbm_bo *bo)
744 format = gbm_bo_get_format(bo);
746 if (format == GBM_FORMAT_ARGB8888) {
749 pixman_region32_init_rect(&r, 0, 0,
751 es->geometry.height);
752 pixman_region32_subtract(&r, &r, &es->opaque);
754 if (!pixman_region32_not_empty(&r))
755 format = GBM_FORMAT_XRGB8888;
757 pixman_region32_fini(&r);
760 for (i = 0; i < s->count_formats; i++)
761 if (s->formats[i] == format)
768 drm_surface_transform_supported(struct weston_surface *es)
770 return !es->transform.enabled ||
771 (es->transform.matrix.type < WESTON_MATRIX_TRANSFORM_ROTATE);
774 static struct weston_plane *
775 drm_output_prepare_overlay_surface(struct weston_output *output_base,
776 struct weston_surface *es)
778 struct weston_compositor *ec = output_base->compositor;
779 struct drm_compositor *c =(struct drm_compositor *) ec;
780 struct drm_sprite *s;
783 pixman_region32_t dest_rect, src_rect;
784 pixman_box32_t *box, tbox;
786 wl_fixed_t sx1, sy1, sx2, sy2;
791 if (es->buffer_transform != output_base->transform)
794 if (c->sprites_are_broken)
797 if (es->output_mask != (1u << output_base->id))
800 if (es->buffer_ref.buffer == NULL)
803 if (es->alpha != 1.0f)
806 if (wl_buffer_is_shm(es->buffer_ref.buffer))
809 if (!drm_surface_transform_supported(es))
812 wl_list_for_each(s, &c->sprite_list, link) {
813 if (!drm_sprite_crtc_supported(output_base, s->possible_crtcs))
822 /* No sprites available */
826 bo = gbm_bo_import(c->gbm, GBM_BO_IMPORT_WL_BUFFER,
827 es->buffer_ref.buffer, GBM_BO_USE_SCANOUT);
831 format = drm_output_check_sprite_format(s, es, bo);
837 s->next = drm_fb_get_from_bo(bo, c, format);
843 drm_fb_set_buffer(s->next, es->buffer_ref.buffer);
845 box = pixman_region32_extents(&es->transform.boundingbox);
846 s->plane.x = box->x1;
847 s->plane.y = box->y1;
850 * Calculate the source & dest rects properly based on actual
851 * position (note the caller has called weston_surface_update_transform()
854 pixman_region32_init(&dest_rect);
855 pixman_region32_intersect(&dest_rect, &es->transform.boundingbox,
856 &output_base->region);
857 pixman_region32_translate(&dest_rect, -output_base->x, -output_base->y);
858 box = pixman_region32_extents(&dest_rect);
859 tbox = weston_transformed_rect(output_base->width,
861 output_base->transform,
865 s->dest_w = tbox.x2 - tbox.x1;
866 s->dest_h = tbox.y2 - tbox.y1;
867 pixman_region32_fini(&dest_rect);
869 pixman_region32_init(&src_rect);
870 pixman_region32_intersect(&src_rect, &es->transform.boundingbox,
871 &output_base->region);
872 box = pixman_region32_extents(&src_rect);
874 weston_surface_from_global_fixed(es,
875 wl_fixed_from_int(box->x1),
876 wl_fixed_from_int(box->y1),
878 weston_surface_from_global_fixed(es,
879 wl_fixed_from_int(box->x2),
880 wl_fixed_from_int(box->y2),
887 if (sx2 > wl_fixed_from_int(es->geometry.width))
888 sx2 = wl_fixed_from_int(es->geometry.width);
889 if (sy2 > wl_fixed_from_int(es->geometry.height))
890 sy2 = wl_fixed_from_int(es->geometry.height);
897 tbox = weston_transformed_rect(wl_fixed_from_int(es->geometry.width),
898 wl_fixed_from_int(es->geometry.height),
899 es->buffer_transform, 1, tbox);
901 s->src_x = tbox.x1 << 8;
902 s->src_y = tbox.y1 << 8;
903 s->src_w = (tbox.x2 - tbox.x1) << 8;
904 s->src_h = (tbox.y2 - tbox.y1) << 8;
905 pixman_region32_fini(&src_rect);
910 static struct weston_plane *
911 drm_output_prepare_cursor_surface(struct weston_output *output_base,
912 struct weston_surface *es)
914 struct drm_compositor *c =
915 (struct drm_compositor *) output_base->compositor;
916 struct drm_output *output = (struct drm_output *) output_base;
920 if (output->base.transform != WL_OUTPUT_TRANSFORM_NORMAL)
922 if (output->cursor_surface)
924 if (es->output_mask != (1u << output_base->id))
926 if (c->cursors_are_broken)
928 if (es->buffer_ref.buffer == NULL ||
929 !wl_buffer_is_shm(es->buffer_ref.buffer) ||
930 es->geometry.width > 64 || es->geometry.height > 64)
933 output->cursor_surface = es;
935 return &output->cursor_plane;
939 drm_output_set_cursor(struct drm_output *output)
941 struct weston_surface *es = output->cursor_surface;
942 struct drm_compositor *c =
943 (struct drm_compositor *) output->base.compositor;
944 EGLint handle, stride;
946 uint32_t buf[64 * 64];
950 output->cursor_surface = NULL;
952 drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0);
956 if (es->buffer_ref.buffer &&
957 pixman_region32_not_empty(&output->cursor_plane.damage)) {
958 pixman_region32_fini(&output->cursor_plane.damage);
959 pixman_region32_init(&output->cursor_plane.damage);
960 output->current_cursor ^= 1;
961 bo = output->cursor_bo[output->current_cursor];
962 memset(buf, 0, sizeof buf);
963 stride = wl_shm_buffer_get_stride(es->buffer_ref.buffer);
964 s = wl_shm_buffer_get_data(es->buffer_ref.buffer);
965 for (i = 0; i < es->geometry.height; i++)
966 memcpy(buf + i * 64, s + i * stride,
967 es->geometry.width * 4);
969 if (gbm_bo_write(bo, buf, sizeof buf) < 0)
970 weston_log("failed update cursor: %m\n");
972 handle = gbm_bo_get_handle(bo).s32;
973 if (drmModeSetCursor(c->drm.fd,
974 output->crtc_id, handle, 64, 64)) {
975 weston_log("failed to set cursor: %m\n");
976 c->cursors_are_broken = 1;
980 x = es->geometry.x - output->base.x;
981 y = es->geometry.y - output->base.y;
982 if (output->cursor_plane.x != x || output->cursor_plane.y != y) {
983 if (drmModeMoveCursor(c->drm.fd, output->crtc_id, x, y)) {
984 weston_log("failed to move cursor: %m\n");
985 c->cursors_are_broken = 1;
988 output->cursor_plane.x = x;
989 output->cursor_plane.y = y;
994 drm_assign_planes(struct weston_output *output)
996 struct drm_compositor *c =
997 (struct drm_compositor *) output->compositor;
998 struct weston_surface *es, *next;
999 pixman_region32_t overlap, surface_overlap;
1000 struct weston_plane *primary, *next_plane;
1003 * Find a surface for each sprite in the output using some heuristics:
1005 * 2) frequency of update
1006 * 3) opacity (though some hw might support alpha blending)
1007 * 4) clipping (this can be fixed with color keys)
1009 * The idea is to save on blitting since this should save power.
1010 * If we can get a large video surface on the sprite for example,
1011 * the main display surface may not need to update at all, and
1012 * the client buffer can be used directly for the sprite surface
1013 * as we do for flipping full screen surfaces.
1015 pixman_region32_init(&overlap);
1016 primary = &c->base.primary_plane;
1017 wl_list_for_each_safe(es, next, &c->base.surface_list, link) {
1018 /* test whether this buffer can ever go into a plane:
1019 * non-shm, or small enough to be a cursor
1021 if ((es->buffer_ref.buffer &&
1022 !wl_buffer_is_shm(es->buffer_ref.buffer)) ||
1023 (es->geometry.width <= 64 && es->geometry.height <= 64))
1024 es->keep_buffer = 1;
1026 es->keep_buffer = 0;
1028 pixman_region32_init(&surface_overlap);
1029 pixman_region32_intersect(&surface_overlap, &overlap,
1030 &es->transform.boundingbox);
1033 if (pixman_region32_not_empty(&surface_overlap))
1034 next_plane = primary;
1035 if (next_plane == NULL)
1036 next_plane = drm_output_prepare_cursor_surface(output, es);
1037 if (next_plane == NULL)
1038 next_plane = drm_output_prepare_scanout_surface(output, es);
1039 if (next_plane == NULL)
1040 next_plane = drm_output_prepare_overlay_surface(output, es);
1041 if (next_plane == NULL)
1042 next_plane = primary;
1043 weston_surface_move_to_plane(es, next_plane);
1044 if (next_plane == primary)
1045 pixman_region32_union(&overlap, &overlap,
1046 &es->transform.boundingbox);
1048 pixman_region32_fini(&surface_overlap);
1050 pixman_region32_fini(&overlap);
1054 drm_output_fini_pixman(struct drm_output *output);
1057 drm_output_destroy(struct weston_output *output_base)
1059 struct drm_output *output = (struct drm_output *) output_base;
1060 struct drm_compositor *c =
1061 (struct drm_compositor *) output->base.compositor;
1062 drmModeCrtcPtr origcrtc = output->original_crtc;
1064 if (output->backlight)
1065 backlight_destroy(output->backlight);
1067 /* Turn off hardware cursor */
1068 drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0);
1070 /* Restore original CRTC state */
1071 drmModeSetCrtc(c->drm.fd, origcrtc->crtc_id, origcrtc->buffer_id,
1072 origcrtc->x, origcrtc->y,
1073 &output->connector_id, 1, &origcrtc->mode);
1074 drmModeFreeCrtc(origcrtc);
1076 c->crtc_allocator &= ~(1 << output->crtc_id);
1077 c->connector_allocator &= ~(1 << output->connector_id);
1079 if (c->use_pixman) {
1080 drm_output_fini_pixman(output);
1082 gl_renderer_output_destroy(output_base);
1083 gbm_surface_destroy(output->surface);
1086 weston_plane_release(&output->fb_plane);
1087 weston_plane_release(&output->cursor_plane);
1089 weston_output_destroy(&output->base);
1090 wl_list_remove(&output->base.link);
1095 static struct drm_mode *
1096 choose_mode (struct drm_output *output, struct weston_mode *target_mode)
1098 struct drm_mode *tmp_mode = NULL, *mode;
1100 if (output->base.current->width == target_mode->width &&
1101 output->base.current->height == target_mode->height &&
1102 (output->base.current->refresh == target_mode->refresh ||
1103 target_mode->refresh == 0))
1104 return (struct drm_mode *)output->base.current;
1106 wl_list_for_each(mode, &output->base.mode_list, base.link) {
1107 if (mode->mode_info.hdisplay == target_mode->width &&
1108 mode->mode_info.vdisplay == target_mode->height) {
1109 if (mode->mode_info.vrefresh == target_mode->refresh ||
1110 target_mode->refresh == 0) {
1112 } else if (!tmp_mode)
1121 drm_output_init_egl(struct drm_output *output, struct drm_compositor *ec);
1123 drm_output_init_pixman(struct drm_output *output, struct drm_compositor *c);
1126 drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mode)
1128 struct drm_output *output;
1129 struct drm_mode *drm_mode;
1130 struct drm_compositor *ec;
1132 if (output_base == NULL) {
1133 weston_log("output is NULL.\n");
1138 weston_log("mode is NULL.\n");
1142 ec = (struct drm_compositor *)output_base->compositor;
1143 output = (struct drm_output *)output_base;
1144 drm_mode = choose_mode (output, mode);
1147 weston_log("%s, invalid resolution:%dx%d\n", __func__, mode->width, mode->height);
1151 if (&drm_mode->base == output->base.current)
1154 output->base.current->flags = 0;
1156 output->base.current = &drm_mode->base;
1157 output->base.current->flags =
1158 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
1160 /* reset rendering stuff. */
1161 drm_output_release_fb(output, output->current);
1162 drm_output_release_fb(output, output->next);
1163 output->current = output->next = NULL;
1165 if (ec->use_pixman) {
1166 drm_output_fini_pixman(output);
1167 if (drm_output_init_pixman(output, ec) < 0) {
1168 weston_log("failed to init output pixman state with "
1173 gl_renderer_output_destroy(&output->base);
1174 gbm_surface_destroy(output->surface);
1176 if (drm_output_init_egl(output, ec) < 0) {
1177 weston_log("failed to init output egl state with "
1187 on_drm_input(int fd, uint32_t mask, void *data)
1189 drmEventContext evctx;
1191 memset(&evctx, 0, sizeof evctx);
1192 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1193 evctx.page_flip_handler = page_flip_handler;
1194 evctx.vblank_handler = vblank_handler;
1195 drmHandleEvent(fd, &evctx);
1201 init_drm(struct drm_compositor *ec, struct udev_device *device)
1203 const char *filename, *sysnum;
1207 sysnum = udev_device_get_sysnum(device);
1209 ec->drm.id = atoi(sysnum);
1210 if (!sysnum || ec->drm.id < 0) {
1211 weston_log("cannot get device sysnum\n");
1215 filename = udev_device_get_devnode(device);
1216 fd = open(filename, O_RDWR | O_CLOEXEC);
1218 /* Probably permissions error */
1219 weston_log("couldn't open %s, skipping\n",
1220 udev_device_get_devnode(device));
1224 weston_log("using %s\n", filename);
1228 ret = drmGetCap(fd, DRM_CAP_TIMESTAMP_MONOTONIC, &cap);
1229 if (ret == 0 && cap == 1)
1230 ec->clock = CLOCK_MONOTONIC;
1232 ec->clock = CLOCK_REALTIME;
1238 init_egl(struct drm_compositor *ec)
1240 ec->gbm = gbm_create_device(ec->drm.fd);
1245 if (gl_renderer_create(&ec->base, ec->gbm, gl_renderer_opaque_attribs,
1247 gbm_device_destroy(ec->gbm);
1255 init_pixman(struct drm_compositor *ec)
1257 return pixman_renderer_init(&ec->base);
1260 static struct drm_mode *
1261 drm_output_add_mode(struct drm_output *output, drmModeModeInfo *info)
1263 struct drm_mode *mode;
1266 mode = malloc(sizeof *mode);
1270 mode->base.flags = 0;
1271 mode->base.width = info->hdisplay;
1272 mode->base.height = info->vdisplay;
1274 /* Calculate higher precision (mHz) refresh rate */
1275 refresh = (info->clock * 1000000LL / info->htotal +
1276 info->vtotal / 2) / info->vtotal;
1278 if (info->flags & DRM_MODE_FLAG_INTERLACE)
1280 if (info->flags & DRM_MODE_FLAG_DBLSCAN)
1282 if (info->vscan > 1)
1283 refresh /= info->vscan;
1285 mode->base.refresh = refresh;
1286 mode->mode_info = *info;
1288 if (info->type & DRM_MODE_TYPE_PREFERRED)
1289 mode->base.flags |= WL_OUTPUT_MODE_PREFERRED;
1291 wl_list_insert(output->base.mode_list.prev, &mode->base.link);
1297 drm_subpixel_to_wayland(int drm_value)
1299 switch (drm_value) {
1301 case DRM_MODE_SUBPIXEL_UNKNOWN:
1302 return WL_OUTPUT_SUBPIXEL_UNKNOWN;
1303 case DRM_MODE_SUBPIXEL_NONE:
1304 return WL_OUTPUT_SUBPIXEL_NONE;
1305 case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB:
1306 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB;
1307 case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR:
1308 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR;
1309 case DRM_MODE_SUBPIXEL_VERTICAL_RGB:
1310 return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB;
1311 case DRM_MODE_SUBPIXEL_VERTICAL_BGR:
1312 return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR;
1316 /* returns a value between 0-255 range, where higher is brighter */
1318 drm_get_backlight(struct drm_output *output)
1320 long brightness, max_brightness, norm;
1322 brightness = backlight_get_brightness(output->backlight);
1323 max_brightness = backlight_get_max_brightness(output->backlight);
1325 /* convert it on a scale of 0 to 255 */
1326 norm = (brightness * 255)/(max_brightness);
1328 return (uint32_t) norm;
1331 /* values accepted are between 0-255 range */
1333 drm_set_backlight(struct weston_output *output_base, uint32_t value)
1335 struct drm_output *output = (struct drm_output *) output_base;
1336 long max_brightness, new_brightness;
1338 if (!output->backlight)
1344 max_brightness = backlight_get_max_brightness(output->backlight);
1346 /* get denormalized value */
1347 new_brightness = (value * max_brightness) / 255;
1349 backlight_set_brightness(output->backlight, new_brightness);
1352 static drmModePropertyPtr
1353 drm_get_prop(int fd, drmModeConnectorPtr connector, const char *name)
1355 drmModePropertyPtr props;
1358 for (i = 0; i < connector->count_props; i++) {
1359 props = drmModeGetProperty(fd, connector->props[i]);
1363 if (!strcmp(props->name, name))
1366 drmModeFreeProperty(props);
1373 drm_set_dpms(struct weston_output *output_base, enum dpms_enum level)
1375 struct drm_output *output = (struct drm_output *) output_base;
1376 struct weston_compositor *ec = output_base->compositor;
1377 struct drm_compositor *c = (struct drm_compositor *) ec;
1378 drmModeConnectorPtr connector;
1379 drmModePropertyPtr prop;
1381 connector = drmModeGetConnector(c->drm.fd, output->connector_id);
1385 prop = drm_get_prop(c->drm.fd, connector, "DPMS");
1387 drmModeFreeConnector(connector);
1391 drmModeConnectorSetProperty(c->drm.fd, connector->connector_id,
1392 prop->prop_id, level);
1393 drmModeFreeProperty(prop);
1394 drmModeFreeConnector(connector);
1397 static const char *connector_type_names[] = {
1416 find_crtc_for_connector(struct drm_compositor *ec,
1417 drmModeRes *resources, drmModeConnector *connector)
1419 drmModeEncoder *encoder;
1420 uint32_t possible_crtcs;
1423 for (j = 0; j < connector->count_encoders; j++) {
1424 encoder = drmModeGetEncoder(ec->drm.fd, connector->encoders[j]);
1425 if (encoder == NULL) {
1426 weston_log("Failed to get encoder.\n");
1429 possible_crtcs = encoder->possible_crtcs;
1430 drmModeFreeEncoder(encoder);
1432 for (i = 0; i < resources->count_crtcs; i++) {
1433 if (possible_crtcs & (1 << i) &&
1434 !(ec->crtc_allocator & (1 << resources->crtcs[i])))
1442 /* Init output state that depends on gl or gbm */
1444 drm_output_init_egl(struct drm_output *output, struct drm_compositor *ec)
1448 output->surface = gbm_surface_create(ec->gbm,
1449 output->base.current->width,
1450 output->base.current->height,
1451 GBM_FORMAT_XRGB8888,
1452 GBM_BO_USE_SCANOUT |
1453 GBM_BO_USE_RENDERING);
1454 if (!output->surface) {
1455 weston_log("failed to create gbm surface\n");
1459 if (gl_renderer_output_create(&output->base, output->surface) < 0) {
1460 weston_log("failed to create gl renderer output state\n");
1461 gbm_surface_destroy(output->surface);
1465 flags = GBM_BO_USE_CURSOR_64X64 | GBM_BO_USE_WRITE;
1467 for (i = 0; i < 2; i++) {
1468 if (output->cursor_bo[i])
1471 output->cursor_bo[i] =
1472 gbm_bo_create(ec->gbm, 64, 64, GBM_FORMAT_ARGB8888,
1476 if (output->cursor_bo[0] == NULL || output->cursor_bo[1] == NULL) {
1477 weston_log("cursor buffers unavailable, using gl cursors\n");
1478 ec->cursors_are_broken = 1;
1485 drm_output_init_pixman(struct drm_output *output, struct drm_compositor *c)
1487 int w = output->base.current->width;
1488 int h = output->base.current->height;
1491 /* FIXME error checking */
1493 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
1494 output->dumb[i] = drm_fb_create_dumb(c, w, h);
1495 if (!output->dumb[i])
1499 pixman_image_create_bits(PIXMAN_x8r8g8b8, w, h,
1500 output->dumb[i]->map,
1501 output->dumb[i]->stride);
1502 if (!output->image[i])
1506 if (pixman_renderer_output_create(&output->base) < 0)
1509 pixman_region32_init_rect(&output->previous_damage,
1510 output->base.x, output->base.y, w, h);
1515 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
1516 if (output->dumb[i])
1517 drm_fb_destroy_dumb(output->dumb[i]);
1518 if (output->image[i])
1519 pixman_image_unref(output->image[i]);
1521 output->dumb[i] = NULL;
1522 output->image[i] = NULL;
1529 drm_output_fini_pixman(struct drm_output *output)
1533 pixman_renderer_output_destroy(&output->base);
1534 pixman_region32_fini(&output->previous_damage);
1536 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
1537 drm_fb_destroy_dumb(output->dumb[i]);
1538 pixman_image_unref(output->image[i]);
1539 output->dumb[i] = NULL;
1540 output->image[i] = NULL;
1545 edid_parse_string(const uint8_t *data, char text[])
1550 /* this is always 12 bytes, but we can't guarantee it's null
1551 * terminated or not junk. */
1552 strncpy(text, (const char *) data, 12);
1554 /* remove insane chars */
1555 for (i = 0; text[i] != '\0'; i++) {
1556 if (text[i] == '\n' ||
1563 /* ensure string is printable */
1564 for (i = 0; text[i] != '\0'; i++) {
1565 if (!isprint(text[i])) {
1571 /* if the string is random junk, ignore the string */
1576 #define EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING 0xfe
1577 #define EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME 0xfc
1578 #define EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER 0xff
1579 #define EDID_OFFSET_DATA_BLOCKS 0x36
1580 #define EDID_OFFSET_LAST_BLOCK 0x6c
1581 #define EDID_OFFSET_PNPID 0x08
1582 #define EDID_OFFSET_SERIAL 0x0c
1585 edid_parse(struct drm_edid *edid, const uint8_t *data, size_t length)
1588 uint32_t serial_number;
1593 if (data[0] != 0x00 || data[1] != 0xff)
1596 /* decode the PNP ID from three 5 bit words packed into 2 bytes
1601 edid->pnp_id[0] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x7c) / 4) - 1;
1602 edid->pnp_id[1] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x3) * 8) + ((data[EDID_OFFSET_PNPID + 1] & 0xe0) / 32) - 1;
1603 edid->pnp_id[2] = 'A' + (data[EDID_OFFSET_PNPID + 1] & 0x1f) - 1;
1604 edid->pnp_id[3] = '\0';
1606 /* maybe there isn't a ASCII serial number descriptor, so use this instead */
1607 serial_number = (uint32_t) data[EDID_OFFSET_SERIAL + 0];
1608 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 1] * 0x100;
1609 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 2] * 0x10000;
1610 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 3] * 0x1000000;
1611 if (serial_number > 0)
1612 sprintf(edid->serial_number, "%lu", (unsigned long) serial_number);
1614 /* parse EDID data */
1615 for (i = EDID_OFFSET_DATA_BLOCKS;
1616 i <= EDID_OFFSET_LAST_BLOCK;
1618 /* ignore pixel clock data */
1624 /* any useful blocks? */
1625 if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME) {
1626 edid_parse_string(&data[i+5],
1627 edid->monitor_name);
1628 } else if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER) {
1629 edid_parse_string(&data[i+5],
1630 edid->serial_number);
1631 } else if (data[i+3] == EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING) {
1632 edid_parse_string(&data[i+5],
1640 find_and_parse_output_edid(struct drm_compositor *ec,
1641 struct drm_output *output,
1642 drmModeConnector *connector)
1644 drmModePropertyBlobPtr edid_blob = NULL;
1645 drmModePropertyPtr property;
1649 for (i = 0; i < connector->count_props && !edid_blob; i++) {
1650 property = drmModeGetProperty(ec->drm.fd, connector->props[i]);
1653 if ((property->flags & DRM_MODE_PROP_BLOB) &&
1654 !strcmp(property->name, "EDID")) {
1655 edid_blob = drmModeGetPropertyBlob(ec->drm.fd,
1656 connector->prop_values[i]);
1658 drmModeFreeProperty(property);
1663 rc = edid_parse(&output->edid,
1667 weston_log("EDID data '%s', '%s', '%s'\n",
1668 output->edid.pnp_id,
1669 output->edid.monitor_name,
1670 output->edid.serial_number);
1671 if (output->edid.pnp_id[0] != '\0')
1672 output->base.make = output->edid.pnp_id;
1673 if (output->edid.monitor_name[0] != '\0')
1674 output->base.model = output->edid.monitor_name;
1675 if (output->edid.serial_number[0] != '\0')
1676 output->base.serial_number = output->edid.serial_number;
1678 drmModeFreePropertyBlob(edid_blob);
1682 create_output_for_connector(struct drm_compositor *ec,
1683 drmModeRes *resources,
1684 drmModeConnector *connector,
1685 int x, int y, struct udev_device *drm_device)
1687 struct drm_output *output;
1688 struct drm_mode *drm_mode, *next, *preferred, *current, *configured;
1689 struct weston_mode *m;
1690 struct drm_configured_output *o = NULL, *temp;
1691 drmModeEncoder *encoder;
1692 drmModeModeInfo crtc_mode;
1696 const char *type_name;
1698 i = find_crtc_for_connector(ec, resources, connector);
1700 weston_log("No usable crtc/encoder pair for connector.\n");
1704 output = malloc(sizeof *output);
1708 memset(output, 0, sizeof *output);
1709 output->base.subpixel = drm_subpixel_to_wayland(connector->subpixel);
1710 output->base.make = "unknown";
1711 output->base.model = "unknown";
1712 output->base.serial_number = "unknown";
1713 wl_list_init(&output->base.mode_list);
1715 if (connector->connector_type < ARRAY_LENGTH(connector_type_names))
1716 type_name = connector_type_names[connector->connector_type];
1718 type_name = "UNKNOWN";
1719 snprintf(name, 32, "%s%d", type_name, connector->connector_type_id);
1720 output->base.name = strdup(name);
1722 output->crtc_id = resources->crtcs[i];
1724 ec->crtc_allocator |= (1 << output->crtc_id);
1725 output->connector_id = connector->connector_id;
1726 ec->connector_allocator |= (1 << output->connector_id);
1728 output->original_crtc = drmModeGetCrtc(ec->drm.fd, output->crtc_id);
1730 /* Get the current mode on the crtc that's currently driving
1731 * this connector. */
1732 encoder = drmModeGetEncoder(ec->drm.fd, connector->encoder_id);
1733 memset(&crtc_mode, 0, sizeof crtc_mode);
1734 if (encoder != NULL) {
1735 crtc = drmModeGetCrtc(ec->drm.fd, encoder->crtc_id);
1736 drmModeFreeEncoder(encoder);
1739 if (crtc->mode_valid)
1740 crtc_mode = crtc->mode;
1741 drmModeFreeCrtc(crtc);
1744 for (i = 0; i < connector->count_modes; i++) {
1745 drm_mode = drm_output_add_mode(output, &connector->modes[i]);
1754 wl_list_for_each(temp, &configured_output_list, link) {
1755 if (strcmp(temp->name, output->base.name) == 0) {
1757 weston_log("%s mode \"%s\" in config\n",
1758 temp->name, temp->mode);
1764 if (o && o->config == OUTPUT_CONFIG_OFF) {
1765 weston_log("Disabling output %s\n", o->name);
1767 drmModeSetCrtc(ec->drm.fd, output->crtc_id,
1768 0, 0, 0, 0, 0, NULL);
1772 wl_list_for_each(drm_mode, &output->base.mode_list, base.link) {
1773 if (o && o->config == OUTPUT_CONFIG_MODE &&
1774 o->width == drm_mode->base.width &&
1775 o->height == drm_mode->base.height)
1776 configured = drm_mode;
1777 if (!memcmp(&crtc_mode, &drm_mode->mode_info, sizeof crtc_mode))
1779 if (drm_mode->base.flags & WL_OUTPUT_MODE_PREFERRED)
1780 preferred = drm_mode;
1783 if (o && o->config == OUTPUT_CONFIG_MODELINE) {
1784 configured = drm_output_add_mode(output, &o->crtc_mode);
1787 current = configured;
1790 if (current == NULL && crtc_mode.clock != 0) {
1791 current = drm_output_add_mode(output, &crtc_mode);
1796 if (o && o->config == OUTPUT_CONFIG_CURRENT)
1797 configured = current;
1799 if (option_current_mode && current)
1800 output->base.current = ¤t->base;
1801 else if (configured)
1802 output->base.current = &configured->base;
1804 output->base.current = &preferred->base;
1806 output->base.current = ¤t->base;
1808 if (output->base.current == NULL) {
1809 weston_log("no available modes for %s\n", output->base.name);
1813 output->base.current->flags |= WL_OUTPUT_MODE_CURRENT;
1815 weston_output_init(&output->base, &ec->base, x, y,
1816 connector->mmWidth, connector->mmHeight,
1817 o ? o->transform : WL_OUTPUT_TRANSFORM_NORMAL, 1);
1819 if (ec->use_pixman) {
1820 if (drm_output_init_pixman(output, ec) < 0) {
1821 weston_log("Failed to init output pixman state\n");
1824 } else if (drm_output_init_egl(output, ec) < 0) {
1825 weston_log("Failed to init output gl state\n");
1829 output->backlight = backlight_init(drm_device,
1830 connector->connector_type);
1831 if (output->backlight) {
1832 output->base.set_backlight = drm_set_backlight;
1833 output->base.backlight_current = drm_get_backlight(output);
1836 wl_list_insert(ec->base.output_list.prev, &output->base.link);
1838 find_and_parse_output_edid(ec, output, connector);
1839 if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS)
1840 output->base.connection_internal = 1;
1842 output->base.origin = output->base.current;
1843 output->base.start_repaint_loop = drm_output_start_repaint_loop;
1844 output->base.repaint = drm_output_repaint;
1845 output->base.destroy = drm_output_destroy;
1846 output->base.assign_planes = drm_assign_planes;
1847 output->base.set_dpms = drm_set_dpms;
1848 output->base.switch_mode = drm_output_switch_mode;
1850 output->base.gamma_size = output->original_crtc->gamma_size;
1851 output->base.set_gamma = drm_output_set_gamma;
1853 weston_plane_init(&output->cursor_plane, 0, 0);
1854 weston_plane_init(&output->fb_plane, 0, 0);
1856 weston_compositor_stack_plane(&ec->base, &output->cursor_plane, NULL);
1857 weston_compositor_stack_plane(&ec->base, &output->fb_plane,
1858 &ec->base.primary_plane);
1860 weston_log("Output %s, (connector %d, crtc %d)\n",
1861 output->base.name, output->connector_id, output->crtc_id);
1862 wl_list_for_each(m, &output->base.mode_list, link)
1863 weston_log_continue(" mode %dx%d@%.1f%s%s%s\n",
1864 m->width, m->height, m->refresh / 1000.0,
1865 m->flags & WL_OUTPUT_MODE_PREFERRED ?
1867 m->flags & WL_OUTPUT_MODE_CURRENT ?
1869 connector->count_modes == 0 ?
1875 weston_output_destroy(&output->base);
1877 wl_list_for_each_safe(drm_mode, next, &output->base.mode_list,
1879 wl_list_remove(&drm_mode->base.link);
1883 drmModeFreeCrtc(output->original_crtc);
1884 ec->crtc_allocator &= ~(1 << output->crtc_id);
1885 ec->connector_allocator &= ~(1 << output->connector_id);
1892 create_sprites(struct drm_compositor *ec)
1894 struct drm_sprite *sprite;
1895 drmModePlaneRes *plane_res;
1896 drmModePlane *plane;
1899 plane_res = drmModeGetPlaneResources(ec->drm.fd);
1901 weston_log("failed to get plane resources: %s\n",
1906 for (i = 0; i < plane_res->count_planes; i++) {
1907 plane = drmModeGetPlane(ec->drm.fd, plane_res->planes[i]);
1911 sprite = malloc(sizeof(*sprite) + ((sizeof(uint32_t)) *
1912 plane->count_formats));
1914 weston_log("%s: out of memory\n",
1920 memset(sprite, 0, sizeof *sprite);
1922 sprite->possible_crtcs = plane->possible_crtcs;
1923 sprite->plane_id = plane->plane_id;
1924 sprite->current = NULL;
1925 sprite->next = NULL;
1926 sprite->compositor = ec;
1927 sprite->count_formats = plane->count_formats;
1928 memcpy(sprite->formats, plane->formats,
1929 plane->count_formats * sizeof(plane->formats[0]));
1930 drmModeFreePlane(plane);
1931 weston_plane_init(&sprite->plane, 0, 0);
1932 weston_compositor_stack_plane(&ec->base, &sprite->plane,
1933 &ec->base.primary_plane);
1935 wl_list_insert(&ec->sprite_list, &sprite->link);
1938 free(plane_res->planes);
1943 destroy_sprites(struct drm_compositor *compositor)
1945 struct drm_sprite *sprite, *next;
1946 struct drm_output *output;
1948 output = container_of(compositor->base.output_list.next,
1949 struct drm_output, base.link);
1951 wl_list_for_each_safe(sprite, next, &compositor->sprite_list, link) {
1952 drmModeSetPlane(compositor->drm.fd,
1954 output->crtc_id, 0, 0,
1955 0, 0, 0, 0, 0, 0, 0, 0);
1956 drm_output_release_fb(output, sprite->current);
1957 drm_output_release_fb(output, sprite->next);
1958 weston_plane_release(&sprite->plane);
1964 create_outputs(struct drm_compositor *ec, uint32_t option_connector,
1965 struct udev_device *drm_device)
1967 drmModeConnector *connector;
1968 drmModeRes *resources;
1972 resources = drmModeGetResources(ec->drm.fd);
1974 weston_log("drmModeGetResources failed\n");
1978 ec->crtcs = calloc(resources->count_crtcs, sizeof(uint32_t));
1980 drmModeFreeResources(resources);
1984 ec->min_width = resources->min_width;
1985 ec->max_width = resources->max_width;
1986 ec->min_height = resources->min_height;
1987 ec->max_height = resources->max_height;
1989 ec->num_crtcs = resources->count_crtcs;
1990 memcpy(ec->crtcs, resources->crtcs, sizeof(uint32_t) * ec->num_crtcs);
1992 for (i = 0; i < resources->count_connectors; i++) {
1993 connector = drmModeGetConnector(ec->drm.fd,
1994 resources->connectors[i]);
1995 if (connector == NULL)
1998 if (connector->connection == DRM_MODE_CONNECTED &&
1999 (option_connector == 0 ||
2000 connector->connector_id == option_connector)) {
2001 if (create_output_for_connector(ec, resources,
2004 drmModeFreeConnector(connector);
2008 x += container_of(ec->base.output_list.prev,
2009 struct weston_output,
2013 drmModeFreeConnector(connector);
2016 if (wl_list_empty(&ec->base.output_list)) {
2017 weston_log("No currently active connector found.\n");
2018 drmModeFreeResources(resources);
2022 drmModeFreeResources(resources);
2028 update_outputs(struct drm_compositor *ec, struct udev_device *drm_device)
2030 drmModeConnector *connector;
2031 drmModeRes *resources;
2032 struct drm_output *output, *next;
2034 int x_offset = 0, y_offset = 0;
2035 uint32_t connected = 0, disconnects = 0;
2038 resources = drmModeGetResources(ec->drm.fd);
2040 weston_log("drmModeGetResources failed\n");
2044 /* collect new connects */
2045 for (i = 0; i < resources->count_connectors; i++) {
2046 int connector_id = resources->connectors[i];
2048 connector = drmModeGetConnector(ec->drm.fd, connector_id);
2049 if (connector == NULL)
2052 if (connector->connection != DRM_MODE_CONNECTED) {
2053 drmModeFreeConnector(connector);
2057 connected |= (1 << connector_id);
2059 if (!(ec->connector_allocator & (1 << connector_id))) {
2060 struct weston_output *last =
2061 container_of(ec->base.output_list.prev,
2062 struct weston_output, link);
2064 /* XXX: not yet needed, we die with 0 outputs */
2065 if (!wl_list_empty(&ec->base.output_list))
2066 x = last->x + last->width;
2070 create_output_for_connector(ec, resources,
2073 weston_log("connector %d connected\n", connector_id);
2076 drmModeFreeConnector(connector);
2078 drmModeFreeResources(resources);
2080 disconnects = ec->connector_allocator & ~connected;
2082 wl_list_for_each_safe(output, next, &ec->base.output_list,
2084 if (x_offset != 0 || y_offset != 0) {
2085 weston_output_move(&output->base,
2086 output->base.x - x_offset,
2087 output->base.y - y_offset);
2090 if (disconnects & (1 << output->connector_id)) {
2091 disconnects &= ~(1 << output->connector_id);
2092 weston_log("connector %d disconnected\n",
2093 output->connector_id);
2094 x_offset += output->base.width;
2095 drm_output_destroy(&output->base);
2100 /* FIXME: handle zero outputs, without terminating */
2101 if (ec->connector_allocator == 0)
2102 wl_display_terminate(ec->base.wl_display);
2106 udev_event_is_hotplug(struct drm_compositor *ec, struct udev_device *device)
2111 sysnum = udev_device_get_sysnum(device);
2112 if (!sysnum || atoi(sysnum) != ec->drm.id)
2115 val = udev_device_get_property_value(device, "HOTPLUG");
2119 return strcmp(val, "1") == 0;
2123 udev_drm_event(int fd, uint32_t mask, void *data)
2125 struct drm_compositor *ec = data;
2126 struct udev_device *event;
2128 event = udev_monitor_receive_device(ec->udev_monitor);
2130 if (udev_event_is_hotplug(ec, event))
2131 update_outputs(ec, event);
2133 udev_device_unref(event);
2139 drm_restore(struct weston_compositor *ec)
2141 struct drm_compositor *d = (struct drm_compositor *) ec;
2143 if (weston_launcher_drm_set_master(&d->base, d->drm.fd, 0) < 0)
2144 weston_log("failed to drop master: %m\n");
2149 drm_free_configured_output(struct drm_configured_output *output)
2157 drm_destroy(struct weston_compositor *ec)
2159 struct drm_compositor *d = (struct drm_compositor *) ec;
2160 struct udev_seat *seat, *next;
2161 struct drm_configured_output *o, *n;
2163 wl_list_for_each_safe(seat, next, &ec->seat_list, base.link)
2164 udev_seat_destroy(seat);
2165 wl_list_for_each_safe(o, n, &configured_output_list, link)
2166 drm_free_configured_output(o);
2168 wl_event_source_remove(d->udev_drm_source);
2169 wl_event_source_remove(d->drm_source);
2173 weston_compositor_shutdown(ec);
2175 ec->renderer->destroy(ec);
2178 gbm_device_destroy(d->gbm);
2180 if (weston_launcher_drm_set_master(&d->base, d->drm.fd, 0) < 0)
2181 weston_log("failed to drop master: %m\n");
2182 tty_destroy(d->tty);
2188 drm_compositor_set_modes(struct drm_compositor *compositor)
2190 struct drm_output *output;
2191 struct drm_mode *drm_mode;
2194 wl_list_for_each(output, &compositor->base.output_list, base.link) {
2195 if (!output->current) {
2196 /* If something that would cause the output to
2197 * switch mode happened while in another vt, we
2198 * might not have a current drm_fb. In that case,
2199 * schedule a repaint and let drm_output_repaint
2200 * handle setting the mode. */
2201 weston_output_schedule_repaint(&output->base);
2205 drm_mode = (struct drm_mode *) output->base.current;
2206 ret = drmModeSetCrtc(compositor->drm.fd, output->crtc_id,
2207 output->current->fb_id, 0, 0,
2208 &output->connector_id, 1,
2209 &drm_mode->mode_info);
2212 "failed to set mode %dx%d for output at %d,%d: %m\n",
2213 drm_mode->base.width, drm_mode->base.height,
2214 output->base.x, output->base.y);
2220 vt_func(struct weston_compositor *compositor, int event)
2222 struct drm_compositor *ec = (struct drm_compositor *) compositor;
2223 struct udev_seat *seat;
2224 struct drm_sprite *sprite;
2225 struct drm_output *output;
2229 weston_log("entering VT\n");
2230 compositor->focus = 1;
2231 if (weston_launcher_drm_set_master(&ec->base, ec->drm.fd, 1)) {
2232 weston_log("failed to set master: %m\n");
2233 wl_display_terminate(compositor->wl_display);
2235 compositor->state = ec->prev_state;
2236 drm_compositor_set_modes(ec);
2237 weston_compositor_damage_all(compositor);
2238 wl_list_for_each(seat, &compositor->seat_list, base.link)
2239 udev_seat_enable(seat, ec->udev);
2242 weston_log("leaving VT\n");
2243 wl_list_for_each(seat, &compositor->seat_list, base.link)
2244 udev_seat_disable(seat);
2246 compositor->focus = 0;
2247 ec->prev_state = compositor->state;
2248 weston_compositor_offscreen(compositor);
2250 /* If we have a repaint scheduled (either from a
2251 * pending pageflip or the idle handler), make sure we
2252 * cancel that so we don't try to pageflip when we're
2253 * vt switched away. The OFFSCREEN state will prevent
2254 * further attemps at repainting. When we switch
2255 * back, we schedule a repaint, which will process
2256 * pending frame callbacks. */
2258 wl_list_for_each(output, &ec->base.output_list, base.link) {
2259 output->base.repaint_needed = 0;
2260 drmModeSetCursor(ec->drm.fd, output->crtc_id, 0, 0, 0);
2263 output = container_of(ec->base.output_list.next,
2264 struct drm_output, base.link);
2266 wl_list_for_each(sprite, &ec->sprite_list, link)
2267 drmModeSetPlane(ec->drm.fd,
2269 output->crtc_id, 0, 0,
2270 0, 0, 0, 0, 0, 0, 0, 0);
2272 if (weston_launcher_drm_set_master(&ec->base, ec->drm.fd, 0) < 0)
2273 weston_log("failed to drop master: %m\n");
2280 switch_vt_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
2282 struct drm_compositor *ec = data;
2284 tty_activate_vt(ec->tty, key - KEY_F1 + 1);
2289 * Some systems may have multiple DRM devices attached to a single seat. This
2290 * function loops over all devices and tries to find a PCI device with the
2291 * boot_vga sysfs attribute set to 1.
2292 * If no such device is found, the first DRM device reported by udev is used.
2294 static struct udev_device*
2295 find_primary_gpu(struct drm_compositor *ec, const char *seat)
2297 struct udev_enumerate *e;
2298 struct udev_list_entry *entry;
2299 const char *path, *device_seat, *id;
2300 struct udev_device *device, *drm_device, *pci;
2302 e = udev_enumerate_new(ec->udev);
2303 udev_enumerate_add_match_subsystem(e, "drm");
2304 udev_enumerate_add_match_sysname(e, "card[0-9]*");
2306 udev_enumerate_scan_devices(e);
2308 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
2309 path = udev_list_entry_get_name(entry);
2310 device = udev_device_new_from_syspath(ec->udev, path);
2313 device_seat = udev_device_get_property_value(device, "ID_SEAT");
2315 device_seat = default_seat;
2316 if (strcmp(device_seat, seat)) {
2317 udev_device_unref(device);
2321 pci = udev_device_get_parent_with_subsystem_devtype(device,
2324 id = udev_device_get_sysattr_value(pci, "boot_vga");
2325 if (id && !strcmp(id, "1")) {
2327 udev_device_unref(drm_device);
2328 drm_device = device;
2334 drm_device = device;
2336 udev_device_unref(device);
2339 udev_enumerate_unref(e);
2344 planes_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
2346 struct drm_compositor *c = data;
2350 c->cursors_are_broken ^= 1;
2353 c->sprites_are_broken ^= 1;
2356 c->sprites_hidden ^= 1;
2363 static struct weston_compositor *
2364 drm_compositor_create(struct wl_display *display,
2365 int connector, const char *seat, int tty, int pixman,
2366 int *argc, char *argv[], int config_fd)
2368 struct drm_compositor *ec;
2369 struct udev_device *drm_device;
2370 struct wl_event_loop *loop;
2371 struct udev_seat *udev_seat, *next;
2375 weston_log("initializing drm backend\n");
2377 ec = malloc(sizeof *ec);
2380 memset(ec, 0, sizeof *ec);
2382 /* KMS support for sprites is not complete yet, so disable the
2383 * functionality for now. */
2384 ec->sprites_are_broken = 1;
2386 ec->use_pixman = pixman;
2388 if (weston_compositor_init(&ec->base, display, argc, argv,
2390 weston_log("%s failed\n", __func__);
2394 /* Check if we run drm-backend using weston-launch */
2395 if (ec->base.launcher_sock == -1 && geteuid() != 0) {
2396 weston_log("fatal: drm backend should be run "
2397 "using weston-launch binary or as root\n");
2398 goto err_compositor;
2401 ec->udev = udev_new();
2402 if (ec->udev == NULL) {
2403 weston_log("failed to initialize udev context\n");
2404 goto err_compositor;
2407 ec->base.wl_display = display;
2408 ec->tty = tty_create(&ec->base, vt_func, tty);
2410 weston_log("failed to initialize tty\n");
2414 drm_device = find_primary_gpu(ec, seat);
2415 if (drm_device == NULL) {
2416 weston_log("no drm device found\n");
2419 path = udev_device_get_syspath(drm_device);
2421 if (init_drm(ec, drm_device) < 0) {
2422 weston_log("failed to initialize kms\n");
2426 if (ec->use_pixman) {
2427 if (init_pixman(ec) < 0) {
2428 weston_log("failed to initialize pixman renderer\n");
2432 if (init_egl(ec) < 0) {
2433 weston_log("failed to initialize egl\n");
2438 ec->base.destroy = drm_destroy;
2439 ec->base.restore = drm_restore;
2443 ec->prev_state = WESTON_COMPOSITOR_ACTIVE;
2445 for (key = KEY_F1; key < KEY_F9; key++)
2446 weston_compositor_add_key_binding(&ec->base, key,
2447 MODIFIER_CTRL | MODIFIER_ALT,
2448 switch_vt_binding, ec);
2450 wl_list_init(&ec->sprite_list);
2453 if (create_outputs(ec, connector, drm_device) < 0) {
2454 weston_log("failed to create output for %s\n", path);
2460 if (udev_seat_create(&ec->base, ec->udev, seat) == NULL) {
2461 weston_log("failed to create input devices\n");
2465 loop = wl_display_get_event_loop(ec->base.wl_display);
2467 wl_event_loop_add_fd(loop, ec->drm.fd,
2468 WL_EVENT_READABLE, on_drm_input, ec);
2470 ec->udev_monitor = udev_monitor_new_from_netlink(ec->udev, "udev");
2471 if (ec->udev_monitor == NULL) {
2472 weston_log("failed to intialize udev monitor\n");
2473 goto err_drm_source;
2475 udev_monitor_filter_add_match_subsystem_devtype(ec->udev_monitor,
2477 ec->udev_drm_source =
2478 wl_event_loop_add_fd(loop,
2479 udev_monitor_get_fd(ec->udev_monitor),
2480 WL_EVENT_READABLE, udev_drm_event, ec);
2482 if (udev_monitor_enable_receiving(ec->udev_monitor) < 0) {
2483 weston_log("failed to enable udev-monitor receiving\n");
2484 goto err_udev_monitor;
2487 udev_device_unref(drm_device);
2489 weston_compositor_add_debug_binding(&ec->base, KEY_O,
2490 planes_binding, ec);
2491 weston_compositor_add_debug_binding(&ec->base, KEY_C,
2492 planes_binding, ec);
2493 weston_compositor_add_debug_binding(&ec->base, KEY_V,
2494 planes_binding, ec);
2499 wl_event_source_remove(ec->udev_drm_source);
2500 udev_monitor_unref(ec->udev_monitor);
2502 wl_event_source_remove(ec->drm_source);
2503 wl_list_for_each_safe(udev_seat, next, &ec->base.seat_list, base.link)
2504 udev_seat_destroy(udev_seat);
2506 ec->base.renderer->destroy(&ec->base);
2507 gbm_device_destroy(ec->gbm);
2508 destroy_sprites(ec);
2510 udev_device_unref(drm_device);
2512 if (weston_launcher_drm_set_master(&ec->base, ec->drm.fd, 0) < 0)
2513 weston_log("failed to drop master: %m\n");
2514 tty_destroy(ec->tty);
2516 udev_unref(ec->udev);
2518 weston_compositor_shutdown(&ec->base);
2525 set_sync_flags(drmModeModeInfo *mode, char *hsync, char *vsync)
2529 if (strcmp(hsync, "+hsync") == 0)
2530 mode->flags |= DRM_MODE_FLAG_PHSYNC;
2531 else if (strcmp(hsync, "-hsync") == 0)
2532 mode->flags |= DRM_MODE_FLAG_NHSYNC;
2536 if (strcmp(vsync, "+vsync") == 0)
2537 mode->flags |= DRM_MODE_FLAG_PVSYNC;
2538 else if (strcmp(vsync, "-vsync") == 0)
2539 mode->flags |= DRM_MODE_FLAG_NVSYNC;
2547 check_for_modeline(struct drm_configured_output *output)
2549 drmModeModeInfo mode;
2555 mode.type = DRM_MODE_TYPE_USERDEF;
2560 if (sscanf(output_mode, "%f %hd %hd %hd %hd %hd %hd %hd %hd %s %s",
2561 &fclock, &mode.hdisplay,
2563 &mode.hsync_end, &mode.htotal,
2566 &mode.vsync_end, &mode.vtotal,
2567 hsync, vsync) == 11) {
2568 if (set_sync_flags(&mode, hsync, vsync))
2571 sprintf(mode_name, "%dx%d", mode.hdisplay, mode.vdisplay);
2572 strcpy(mode.name, mode_name);
2574 mode.clock = fclock * 1000;
2578 output->crtc_mode = mode;
2584 drm_output_set_transform(struct drm_configured_output *output)
2586 if (!output_transform) {
2587 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
2591 if (!strcmp(output_transform, "normal"))
2592 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
2593 else if (!strcmp(output_transform, "90"))
2594 output->transform = WL_OUTPUT_TRANSFORM_90;
2595 else if (!strcmp(output_transform, "180"))
2596 output->transform = WL_OUTPUT_TRANSFORM_180;
2597 else if (!strcmp(output_transform, "270"))
2598 output->transform = WL_OUTPUT_TRANSFORM_270;
2599 else if (!strcmp(output_transform, "flipped"))
2600 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED;
2601 else if (!strcmp(output_transform, "flipped-90"))
2602 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_90;
2603 else if (!strcmp(output_transform, "flipped-180"))
2604 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_180;
2605 else if (!strcmp(output_transform, "flipped-270"))
2606 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_270;
2608 weston_log("Invalid transform \"%s\" for output %s\n",
2609 output_transform, output_name);
2610 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
2613 free(output_transform);
2614 output_transform = NULL;
2618 output_section_done(void *data)
2620 struct drm_configured_output *output;
2622 output = malloc(sizeof *output);
2624 if (!output || !output_name || (output_name[0] == 'X') ||
2625 (!output_mode && !output_transform)) {
2628 free(output_transform);
2632 output_transform = NULL;
2636 output->config = OUTPUT_CONFIG_INVALID;
2637 output->name = output_name;
2638 output->mode = output_mode;
2641 if (strcmp(output_mode, "off") == 0)
2642 output->config = OUTPUT_CONFIG_OFF;
2643 else if (strcmp(output_mode, "preferred") == 0)
2644 output->config = OUTPUT_CONFIG_PREFERRED;
2645 else if (strcmp(output_mode, "current") == 0)
2646 output->config = OUTPUT_CONFIG_CURRENT;
2647 else if (sscanf(output_mode, "%dx%d",
2648 &output->width, &output->height) == 2)
2649 output->config = OUTPUT_CONFIG_MODE;
2650 else if (check_for_modeline(output) == 0)
2651 output->config = OUTPUT_CONFIG_MODELINE;
2653 if (output->config == OUTPUT_CONFIG_INVALID)
2654 weston_log("Invalid mode \"%s\" for output %s\n",
2655 output_mode, output_name);
2659 drm_output_set_transform(output);
2661 wl_list_insert(&configured_output_list, &output->link);
2663 if (output_transform)
2664 free(output_transform);
2665 output_transform = NULL;
2668 WL_EXPORT struct weston_compositor *
2669 backend_init(struct wl_display *display, int *argc, char *argv[],
2672 int connector = 0, tty = 0, use_pixman = 0;
2673 const char *seat = default_seat;
2675 const struct weston_option drm_options[] = {
2676 { WESTON_OPTION_INTEGER, "connector", 0, &connector },
2677 { WESTON_OPTION_STRING, "seat", 0, &seat },
2678 { WESTON_OPTION_INTEGER, "tty", 0, &tty },
2679 { WESTON_OPTION_BOOLEAN, "current-mode", 0, &option_current_mode },
2680 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
2683 parse_options(drm_options, ARRAY_LENGTH(drm_options), argc, argv);
2685 wl_list_init(&configured_output_list);
2687 const struct config_key drm_config_keys[] = {
2688 { "name", CONFIG_KEY_STRING, &output_name },
2689 { "mode", CONFIG_KEY_STRING, &output_mode },
2690 { "transform", CONFIG_KEY_STRING, &output_transform },
2693 const struct config_section config_section[] = {
2694 { "output", drm_config_keys,
2695 ARRAY_LENGTH(drm_config_keys), output_section_done },
2698 parse_config_file(config_fd, config_section,
2699 ARRAY_LENGTH(config_section), NULL);
2701 return drm_compositor_create(display, connector, seat, tty, use_pixman,
2702 argc, argv, config_fd);