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;
63 OUTPUT_CONFIG_INVALID = 0,
65 OUTPUT_CONFIG_PREFERRED,
66 OUTPUT_CONFIG_CURRENT,
68 OUTPUT_CONFIG_MODELINE
71 struct drm_compositor {
72 struct weston_compositor base;
75 struct wl_event_source *drm_source;
77 struct udev_monitor *udev_monitor;
78 struct wl_event_source *udev_drm_source;
84 struct gbm_device *gbm;
87 uint32_t crtc_allocator;
88 uint32_t connector_allocator;
91 /* we need these parameters in order to not fail drmModeAddFB2()
92 * due to out of bounds dimensions, and then mistakenly set
95 uint32_t min_width, max_width;
96 uint32_t min_height, max_height;
99 struct wl_list sprite_list;
100 int sprites_are_broken;
103 int cursors_are_broken;
113 struct weston_mode base;
114 drmModeModeInfo mode_info;
120 struct drm_output *output;
121 uint32_t fb_id, stride, handle, size;
123 int is_client_buffer;
124 struct weston_buffer_reference buffer_ref;
126 /* Used by gbm fbs */
129 /* Used by dumb fbs */
135 char monitor_name[13];
137 char serial_number[13];
141 struct weston_output base;
145 uint32_t connector_id;
146 drmModeCrtcPtr original_crtc;
147 struct drm_edid edid;
148 drmModePropertyPtr dpms_prop;
151 int page_flip_pending;
153 struct gbm_surface *surface;
154 struct gbm_bo *cursor_bo[2];
155 struct weston_plane cursor_plane;
156 struct weston_plane fb_plane;
157 struct weston_surface *cursor_surface;
159 struct drm_fb *current, *next;
160 struct backlight *backlight;
162 struct drm_fb *dumb[2];
163 pixman_image_t *image[2];
165 pixman_region32_t previous_damage;
169 * An output has a primary display plane plus zero or more sprites for
170 * blending display contents.
175 struct weston_plane plane;
177 struct drm_fb *current, *next;
178 struct drm_output *output;
179 struct drm_compositor *compositor;
181 uint32_t possible_crtcs;
183 uint32_t count_formats;
185 int32_t src_x, src_y;
186 uint32_t src_w, src_h;
187 uint32_t dest_x, dest_y;
188 uint32_t dest_w, dest_h;
193 static const char default_seat[] = "seat0";
196 drm_output_set_cursor(struct drm_output *output);
199 drm_sprite_crtc_supported(struct weston_output *output_base, uint32_t supported)
201 struct weston_compositor *ec = output_base->compositor;
202 struct drm_compositor *c =(struct drm_compositor *) ec;
203 struct drm_output *output = (struct drm_output *) output_base;
206 for (crtc = 0; crtc < c->num_crtcs; crtc++) {
207 if (c->crtcs[crtc] != output->crtc_id)
210 if (supported & (1 << crtc))
218 drm_fb_destroy_callback(struct gbm_bo *bo, void *data)
220 struct drm_fb *fb = data;
221 struct gbm_device *gbm = gbm_bo_get_device(bo);
224 drmModeRmFB(gbm_device_get_fd(gbm), fb->fb_id);
226 weston_buffer_reference(&fb->buffer_ref, NULL);
231 static struct drm_fb *
232 drm_fb_create_dumb(struct drm_compositor *ec, unsigned width, unsigned height)
237 struct drm_mode_create_dumb create_arg;
238 struct drm_mode_destroy_dumb destroy_arg;
239 struct drm_mode_map_dumb map_arg;
241 fb = calloc(1, sizeof *fb);
246 create_arg.width = width;
247 create_arg.height = height;
249 ret = drmIoctl(ec->drm.fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_arg);
253 fb->handle = create_arg.handle;
254 fb->stride = create_arg.pitch;
255 fb->size = create_arg.size;
258 ret = drmModeAddFB(ec->drm.fd, width, height, 24, 32,
259 fb->stride, fb->handle, &fb->fb_id);
263 memset(&map_arg, 0, sizeof(map_arg));
264 map_arg.handle = fb->handle;
265 ret = drmIoctl(fb->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_arg);
269 fb->map = mmap(0, fb->size, PROT_WRITE,
270 MAP_SHARED, ec->drm.fd, map_arg.offset);
271 if (fb->map == MAP_FAILED)
277 drmModeRmFB(ec->drm.fd, fb->fb_id);
279 memset(&destroy_arg, 0, sizeof(destroy_arg));
280 destroy_arg.handle = create_arg.handle;
281 drmIoctl(ec->drm.fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
288 drm_fb_destroy_dumb(struct drm_fb *fb)
290 struct drm_mode_destroy_dumb destroy_arg;
296 drmModeRmFB(fb->fd, fb->fb_id);
298 weston_buffer_reference(&fb->buffer_ref, NULL);
300 munmap(fb->map, fb->size);
302 memset(&destroy_arg, 0, sizeof(destroy_arg));
303 destroy_arg.handle = fb->handle;
304 drmIoctl(fb->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
309 static struct drm_fb *
310 drm_fb_get_from_bo(struct gbm_bo *bo,
311 struct drm_compositor *compositor, uint32_t format)
313 struct drm_fb *fb = gbm_bo_get_user_data(bo);
314 uint32_t width, height;
315 uint32_t handles[4], pitches[4], offsets[4];
321 fb = calloc(1, sizeof *fb);
327 width = gbm_bo_get_width(bo);
328 height = gbm_bo_get_height(bo);
329 fb->stride = gbm_bo_get_stride(bo);
330 fb->handle = gbm_bo_get_handle(bo).u32;
331 fb->size = fb->stride * height;
332 fb->fd = compositor->drm.fd;
334 if (compositor->min_width > width || width > compositor->max_width ||
335 compositor->min_height > height ||
336 height > compositor->max_height) {
337 weston_log("bo geometry out of bounds\n");
343 if (format && !compositor->no_addfb2) {
344 handles[0] = fb->handle;
345 pitches[0] = fb->stride;
348 ret = drmModeAddFB2(compositor->drm.fd, width, height,
349 format, handles, pitches, offsets,
352 weston_log("addfb2 failed: %m\n");
353 compositor->no_addfb2 = 1;
354 compositor->sprites_are_broken = 1;
359 ret = drmModeAddFB(compositor->drm.fd, width, height, 24, 32,
360 fb->stride, fb->handle, &fb->fb_id);
363 weston_log("failed to create kms fb: %m\n");
367 gbm_bo_set_user_data(bo, fb, drm_fb_destroy_callback);
377 drm_fb_set_buffer(struct drm_fb *fb, struct wl_buffer *buffer)
379 assert(fb->buffer_ref.buffer == NULL);
381 fb->is_client_buffer = 1;
383 weston_buffer_reference(&fb->buffer_ref, buffer);
387 drm_output_release_fb(struct drm_output *output, struct drm_fb *fb)
393 (fb != output->dumb[0] && fb != output->dumb[1])) {
394 drm_fb_destroy_dumb(fb);
396 if (fb->is_client_buffer)
397 gbm_bo_destroy(fb->bo);
399 gbm_surface_release_buffer(output->surface,
400 output->current->bo);
405 drm_output_check_scanout_format(struct drm_output *output,
406 struct weston_surface *es, struct gbm_bo *bo)
411 format = gbm_bo_get_format(bo);
414 case GBM_FORMAT_XRGB8888:
416 case GBM_FORMAT_ARGB8888:
417 /* We can only scanout an ARGB buffer if the surface's
418 * opaque region covers the whole output */
419 pixman_region32_init(&r);
420 pixman_region32_subtract(&r, &output->base.region,
423 if (!pixman_region32_not_empty(&r))
424 format = GBM_FORMAT_XRGB8888;
428 pixman_region32_fini(&r);
436 static struct weston_plane *
437 drm_output_prepare_scanout_surface(struct weston_output *_output,
438 struct weston_surface *es)
440 struct drm_output *output = (struct drm_output *) _output;
441 struct drm_compositor *c =
442 (struct drm_compositor *) output->base.compositor;
443 struct wl_buffer *buffer = es->buffer_ref.buffer;
447 if (es->geometry.x != output->base.x ||
448 es->geometry.y != output->base.y ||
449 buffer == NULL || c->gbm == NULL ||
450 buffer->width != output->base.current->width ||
451 buffer->height != output->base.current->height ||
452 output->base.transform != es->buffer_transform ||
453 es->transform.enabled)
456 bo = gbm_bo_import(c->gbm, GBM_BO_IMPORT_WL_BUFFER,
457 buffer, GBM_BO_USE_SCANOUT);
459 /* Unable to use the buffer for scanout */
463 format = drm_output_check_scanout_format(output, es, bo);
469 output->next = drm_fb_get_from_bo(bo, c, format);
475 drm_fb_set_buffer(output->next, buffer);
477 return &output->fb_plane;
481 drm_output_render_gl(struct drm_output *output, pixman_region32_t *damage)
483 struct drm_compositor *c =
484 (struct drm_compositor *) output->base.compositor;
487 c->base.renderer->repaint_output(&output->base, damage);
489 bo = gbm_surface_lock_front_buffer(output->surface);
491 weston_log("failed to lock front buffer: %m\n");
495 output->next = drm_fb_get_from_bo(bo, c, GBM_FORMAT_XRGB8888);
497 weston_log("failed to get drm_fb for bo\n");
498 gbm_surface_release_buffer(output->surface, bo);
504 drm_output_render_pixman(struct drm_output *output, pixman_region32_t *damage)
506 struct weston_compositor *ec = output->base.compositor;
507 pixman_region32_t total_damage, previous_damage;
509 pixman_region32_init(&total_damage);
510 pixman_region32_init(&previous_damage);
512 pixman_region32_copy(&previous_damage, damage);
514 pixman_region32_union(&total_damage, damage, &output->previous_damage);
515 pixman_region32_copy(&output->previous_damage, &previous_damage);
517 output->current_image ^= 1;
519 output->next = output->dumb[output->current_image];
520 pixman_renderer_output_set_buffer(&output->base,
521 output->image[output->current_image]);
523 ec->renderer->repaint_output(&output->base, &total_damage);
525 pixman_region32_fini(&total_damage);
526 pixman_region32_fini(&previous_damage);
530 drm_output_render(struct drm_output *output, pixman_region32_t *damage)
532 struct drm_compositor *c =
533 (struct drm_compositor *) output->base.compositor;
536 drm_output_render_pixman(output, damage);
538 drm_output_render_gl(output, damage);
540 pixman_region32_subtract(&c->base.primary_plane.damage,
541 &c->base.primary_plane.damage, damage);
545 drm_output_set_gamma(struct weston_output *output_base,
546 uint16_t size, uint16_t *r, uint16_t *g, uint16_t *b)
549 struct drm_output *output = (struct drm_output *) output_base;
550 struct drm_compositor *compositor = (struct drm_compositor *) output->base.compositor;
553 if (output_base->gamma_size != size)
555 if (!output->original_crtc)
558 rc = drmModeCrtcSetGamma(compositor->drm.fd,
562 weston_log("set gamma failed: %m\n");
566 drm_output_repaint(struct weston_output *output_base,
567 pixman_region32_t *damage)
569 struct drm_output *output = (struct drm_output *) output_base;
570 struct drm_compositor *compositor =
571 (struct drm_compositor *) output->base.compositor;
572 struct drm_sprite *s;
573 struct drm_mode *mode;
577 drm_output_render(output, damage);
581 mode = container_of(output->base.current, struct drm_mode, base);
582 if (!output->current) {
583 ret = drmModeSetCrtc(compositor->drm.fd, output->crtc_id,
584 output->next->fb_id, 0, 0,
585 &output->connector_id, 1,
588 weston_log("set mode failed: %m\n");
591 output_base->set_dpms(output_base, WESTON_DPMS_ON);
594 if (drmModePageFlip(compositor->drm.fd, output->crtc_id,
596 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
597 weston_log("queueing pageflip failed: %m\n");
601 output->page_flip_pending = 1;
603 drm_output_set_cursor(output);
606 * Now, update all the sprite surfaces
608 wl_list_for_each(s, &compositor->sprite_list, link) {
609 uint32_t flags = 0, fb_id = 0;
611 .request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT,
612 .request.sequence = 1,
615 if ((!s->current && !s->next) ||
616 !drm_sprite_crtc_supported(output_base, s->possible_crtcs))
619 if (s->next && !compositor->sprites_hidden)
620 fb_id = s->next->fb_id;
622 ret = drmModeSetPlane(compositor->drm.fd, s->plane_id,
623 output->crtc_id, fb_id, flags,
624 s->dest_x, s->dest_y,
625 s->dest_w, s->dest_h,
629 weston_log("setplane failed: %d: %s\n",
630 ret, strerror(errno));
632 if (output->pipe > 0)
633 vbl.request.type |= DRM_VBLANK_SECONDARY;
636 * Queue a vblank signal so we know when the surface
637 * becomes active on the display or has been replaced.
639 vbl.request.signal = (unsigned long)s;
640 ret = drmWaitVBlank(compositor->drm.fd, &vbl);
642 weston_log("vblank event request failed: %d: %s\n",
643 ret, strerror(errno));
647 output->vblank_pending = 1;
654 drm_output_start_repaint_loop(struct weston_output *output_base)
656 struct drm_output *output = (struct drm_output *) output_base;
657 struct drm_compositor *compositor = (struct drm_compositor *)
658 output_base->compositor;
663 if (!output->current) {
664 /* We can't page flip if there's no mode set */
667 clock_gettime(compositor->clock, &ts);
668 msec = ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
669 weston_output_finish_frame(output_base, msec);
673 fb_id = output->current->fb_id;
675 if (drmModePageFlip(compositor->drm.fd, output->crtc_id, fb_id,
676 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
677 weston_log("queueing pageflip failed: %m\n");
683 vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec,
686 struct drm_sprite *s = (struct drm_sprite *)data;
687 struct drm_output *output = s->output;
690 output->vblank_pending = 0;
692 drm_output_release_fb(output, s->current);
693 s->current = s->next;
696 if (!output->page_flip_pending) {
697 msecs = sec * 1000 + usec / 1000;
698 weston_output_finish_frame(&output->base, msecs);
703 page_flip_handler(int fd, unsigned int frame,
704 unsigned int sec, unsigned int usec, void *data)
706 struct drm_output *output = (struct drm_output *) data;
709 /* We don't set page_flip_pending on start_repaint_loop, in that case
710 * we just want to page flip to the current buffer to get an accurate
712 if (output->page_flip_pending) {
713 drm_output_release_fb(output, output->current);
714 output->current = output->next;
718 output->page_flip_pending = 0;
720 if (!output->vblank_pending) {
721 msecs = sec * 1000 + usec / 1000;
722 weston_output_finish_frame(&output->base, msecs);
727 drm_output_check_sprite_format(struct drm_sprite *s,
728 struct weston_surface *es, struct gbm_bo *bo)
732 format = gbm_bo_get_format(bo);
734 if (format == GBM_FORMAT_ARGB8888) {
737 pixman_region32_init_rect(&r, 0, 0,
739 es->geometry.height);
740 pixman_region32_subtract(&r, &r, &es->opaque);
742 if (!pixman_region32_not_empty(&r))
743 format = GBM_FORMAT_XRGB8888;
745 pixman_region32_fini(&r);
748 for (i = 0; i < s->count_formats; i++)
749 if (s->formats[i] == format)
756 drm_surface_transform_supported(struct weston_surface *es)
758 return !es->transform.enabled ||
759 (es->transform.matrix.type < WESTON_MATRIX_TRANSFORM_ROTATE);
762 static struct weston_plane *
763 drm_output_prepare_overlay_surface(struct weston_output *output_base,
764 struct weston_surface *es)
766 struct weston_compositor *ec = output_base->compositor;
767 struct drm_compositor *c =(struct drm_compositor *) ec;
768 struct drm_sprite *s;
771 pixman_region32_t dest_rect, src_rect;
772 pixman_box32_t *box, tbox;
774 wl_fixed_t sx1, sy1, sx2, sy2;
779 if (es->buffer_transform != output_base->transform)
782 if (es->buffer_scale != output_base->scale)
785 if (c->sprites_are_broken)
788 if (es->output_mask != (1u << output_base->id))
791 if (es->buffer_ref.buffer == NULL)
794 if (es->alpha != 1.0f)
797 if (wl_buffer_is_shm(es->buffer_ref.buffer))
800 if (!drm_surface_transform_supported(es))
803 wl_list_for_each(s, &c->sprite_list, link) {
804 if (!drm_sprite_crtc_supported(output_base, s->possible_crtcs))
813 /* No sprites available */
817 bo = gbm_bo_import(c->gbm, GBM_BO_IMPORT_WL_BUFFER,
818 es->buffer_ref.buffer, GBM_BO_USE_SCANOUT);
822 format = drm_output_check_sprite_format(s, es, bo);
828 s->next = drm_fb_get_from_bo(bo, c, format);
834 drm_fb_set_buffer(s->next, es->buffer_ref.buffer);
836 box = pixman_region32_extents(&es->transform.boundingbox);
837 s->plane.x = box->x1;
838 s->plane.y = box->y1;
841 * Calculate the source & dest rects properly based on actual
842 * position (note the caller has called weston_surface_update_transform()
845 pixman_region32_init(&dest_rect);
846 pixman_region32_intersect(&dest_rect, &es->transform.boundingbox,
847 &output_base->region);
848 pixman_region32_translate(&dest_rect, -output_base->x, -output_base->y);
849 box = pixman_region32_extents(&dest_rect);
850 tbox = weston_transformed_rect(output_base->width,
852 output_base->transform,
857 s->dest_w = tbox.x2 - tbox.x1;
858 s->dest_h = tbox.y2 - tbox.y1;
859 pixman_region32_fini(&dest_rect);
861 pixman_region32_init(&src_rect);
862 pixman_region32_intersect(&src_rect, &es->transform.boundingbox,
863 &output_base->region);
864 box = pixman_region32_extents(&src_rect);
866 weston_surface_from_global_fixed(es,
867 wl_fixed_from_int(box->x1),
868 wl_fixed_from_int(box->y1),
870 weston_surface_from_global_fixed(es,
871 wl_fixed_from_int(box->x2),
872 wl_fixed_from_int(box->y2),
879 if (sx2 > wl_fixed_from_int(es->geometry.width))
880 sx2 = wl_fixed_from_int(es->geometry.width);
881 if (sy2 > wl_fixed_from_int(es->geometry.height))
882 sy2 = wl_fixed_from_int(es->geometry.height);
889 tbox = weston_transformed_rect(wl_fixed_from_int(es->geometry.width),
890 wl_fixed_from_int(es->geometry.height),
891 es->buffer_transform, es->buffer_scale, tbox);
893 s->src_x = tbox.x1 << 8;
894 s->src_y = tbox.y1 << 8;
895 s->src_w = (tbox.x2 - tbox.x1) << 8;
896 s->src_h = (tbox.y2 - tbox.y1) << 8;
897 pixman_region32_fini(&src_rect);
902 static struct weston_plane *
903 drm_output_prepare_cursor_surface(struct weston_output *output_base,
904 struct weston_surface *es)
906 struct drm_compositor *c =
907 (struct drm_compositor *) output_base->compositor;
908 struct drm_output *output = (struct drm_output *) output_base;
912 if (output->base.transform != WL_OUTPUT_TRANSFORM_NORMAL)
914 if (output->cursor_surface)
916 if (es->output_mask != (1u << output_base->id))
918 if (c->cursors_are_broken)
920 if (es->buffer_ref.buffer == NULL ||
921 !wl_buffer_is_shm(es->buffer_ref.buffer) ||
922 es->geometry.width > 64 || es->geometry.height > 64)
925 output->cursor_surface = es;
927 return &output->cursor_plane;
931 drm_output_set_cursor(struct drm_output *output)
933 struct weston_surface *es = output->cursor_surface;
934 struct drm_compositor *c =
935 (struct drm_compositor *) output->base.compositor;
936 EGLint handle, stride;
938 uint32_t buf[64 * 64];
942 output->cursor_surface = NULL;
944 drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0);
948 if (es->buffer_ref.buffer &&
949 pixman_region32_not_empty(&output->cursor_plane.damage)) {
950 pixman_region32_fini(&output->cursor_plane.damage);
951 pixman_region32_init(&output->cursor_plane.damage);
952 output->current_cursor ^= 1;
953 bo = output->cursor_bo[output->current_cursor];
954 memset(buf, 0, sizeof buf);
955 stride = wl_shm_buffer_get_stride(es->buffer_ref.buffer);
956 s = wl_shm_buffer_get_data(es->buffer_ref.buffer);
957 for (i = 0; i < es->geometry.height; i++)
958 memcpy(buf + i * 64, s + i * stride,
959 es->geometry.width * 4);
961 if (gbm_bo_write(bo, buf, sizeof buf) < 0)
962 weston_log("failed update cursor: %m\n");
964 handle = gbm_bo_get_handle(bo).s32;
965 if (drmModeSetCursor(c->drm.fd,
966 output->crtc_id, handle, 64, 64)) {
967 weston_log("failed to set cursor: %m\n");
968 c->cursors_are_broken = 1;
972 x = (es->geometry.x - output->base.x) * output->base.scale;
973 y = (es->geometry.y - output->base.y) * output->base.scale;
974 if (output->cursor_plane.x != x || output->cursor_plane.y != y) {
975 if (drmModeMoveCursor(c->drm.fd, output->crtc_id, x, y)) {
976 weston_log("failed to move cursor: %m\n");
977 c->cursors_are_broken = 1;
980 output->cursor_plane.x = x;
981 output->cursor_plane.y = y;
986 drm_assign_planes(struct weston_output *output)
988 struct drm_compositor *c =
989 (struct drm_compositor *) output->compositor;
990 struct weston_surface *es, *next;
991 pixman_region32_t overlap, surface_overlap;
992 struct weston_plane *primary, *next_plane;
995 * Find a surface for each sprite in the output using some heuristics:
997 * 2) frequency of update
998 * 3) opacity (though some hw might support alpha blending)
999 * 4) clipping (this can be fixed with color keys)
1001 * The idea is to save on blitting since this should save power.
1002 * If we can get a large video surface on the sprite for example,
1003 * the main display surface may not need to update at all, and
1004 * the client buffer can be used directly for the sprite surface
1005 * as we do for flipping full screen surfaces.
1007 pixman_region32_init(&overlap);
1008 primary = &c->base.primary_plane;
1009 wl_list_for_each_safe(es, next, &c->base.surface_list, link) {
1010 /* test whether this buffer can ever go into a plane:
1011 * non-shm, or small enough to be a cursor
1013 if ((es->buffer_ref.buffer &&
1014 !wl_buffer_is_shm(es->buffer_ref.buffer)) ||
1015 (es->geometry.width <= 64 && es->geometry.height <= 64))
1016 es->keep_buffer = 1;
1018 es->keep_buffer = 0;
1020 pixman_region32_init(&surface_overlap);
1021 pixman_region32_intersect(&surface_overlap, &overlap,
1022 &es->transform.boundingbox);
1025 if (pixman_region32_not_empty(&surface_overlap))
1026 next_plane = primary;
1027 if (next_plane == NULL)
1028 next_plane = drm_output_prepare_cursor_surface(output, es);
1029 if (next_plane == NULL)
1030 next_plane = drm_output_prepare_scanout_surface(output, es);
1031 if (next_plane == NULL)
1032 next_plane = drm_output_prepare_overlay_surface(output, es);
1033 if (next_plane == NULL)
1034 next_plane = primary;
1035 weston_surface_move_to_plane(es, next_plane);
1036 if (next_plane == primary)
1037 pixman_region32_union(&overlap, &overlap,
1038 &es->transform.boundingbox);
1040 pixman_region32_fini(&surface_overlap);
1042 pixman_region32_fini(&overlap);
1046 drm_output_fini_pixman(struct drm_output *output);
1049 drm_output_destroy(struct weston_output *output_base)
1051 struct drm_output *output = (struct drm_output *) output_base;
1052 struct drm_compositor *c =
1053 (struct drm_compositor *) output->base.compositor;
1054 drmModeCrtcPtr origcrtc = output->original_crtc;
1056 if (output->backlight)
1057 backlight_destroy(output->backlight);
1059 drmModeFreeProperty(output->dpms_prop);
1061 /* Turn off hardware cursor */
1062 drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0);
1064 /* Restore original CRTC state */
1065 drmModeSetCrtc(c->drm.fd, origcrtc->crtc_id, origcrtc->buffer_id,
1066 origcrtc->x, origcrtc->y,
1067 &output->connector_id, 1, &origcrtc->mode);
1068 drmModeFreeCrtc(origcrtc);
1070 c->crtc_allocator &= ~(1 << output->crtc_id);
1071 c->connector_allocator &= ~(1 << output->connector_id);
1073 if (c->use_pixman) {
1074 drm_output_fini_pixman(output);
1076 gl_renderer_output_destroy(output_base);
1077 gbm_surface_destroy(output->surface);
1080 weston_plane_release(&output->fb_plane);
1081 weston_plane_release(&output->cursor_plane);
1083 weston_output_destroy(&output->base);
1084 wl_list_remove(&output->base.link);
1089 static struct drm_mode *
1090 choose_mode (struct drm_output *output, struct weston_mode *target_mode)
1092 struct drm_mode *tmp_mode = NULL, *mode;
1094 if (output->base.current->width == target_mode->width &&
1095 output->base.current->height == target_mode->height &&
1096 (output->base.current->refresh == target_mode->refresh ||
1097 target_mode->refresh == 0))
1098 return (struct drm_mode *)output->base.current;
1100 wl_list_for_each(mode, &output->base.mode_list, base.link) {
1101 if (mode->mode_info.hdisplay == target_mode->width &&
1102 mode->mode_info.vdisplay == target_mode->height) {
1103 if (mode->mode_info.vrefresh == target_mode->refresh ||
1104 target_mode->refresh == 0) {
1106 } else if (!tmp_mode)
1115 drm_output_init_egl(struct drm_output *output, struct drm_compositor *ec);
1117 drm_output_init_pixman(struct drm_output *output, struct drm_compositor *c);
1120 drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mode)
1122 struct drm_output *output;
1123 struct drm_mode *drm_mode;
1124 struct drm_compositor *ec;
1126 if (output_base == NULL) {
1127 weston_log("output is NULL.\n");
1132 weston_log("mode is NULL.\n");
1136 ec = (struct drm_compositor *)output_base->compositor;
1137 output = (struct drm_output *)output_base;
1138 drm_mode = choose_mode (output, mode);
1141 weston_log("%s, invalid resolution:%dx%d\n", __func__, mode->width, mode->height);
1145 if (&drm_mode->base == output->base.current)
1148 output->base.current->flags = 0;
1150 output->base.current = &drm_mode->base;
1151 output->base.current->flags =
1152 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
1154 /* reset rendering stuff. */
1155 drm_output_release_fb(output, output->current);
1156 drm_output_release_fb(output, output->next);
1157 output->current = output->next = NULL;
1159 if (ec->use_pixman) {
1160 drm_output_fini_pixman(output);
1161 if (drm_output_init_pixman(output, ec) < 0) {
1162 weston_log("failed to init output pixman state with "
1167 gl_renderer_output_destroy(&output->base);
1168 gbm_surface_destroy(output->surface);
1170 if (drm_output_init_egl(output, ec) < 0) {
1171 weston_log("failed to init output egl state with "
1181 on_drm_input(int fd, uint32_t mask, void *data)
1183 drmEventContext evctx;
1185 memset(&evctx, 0, sizeof evctx);
1186 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1187 evctx.page_flip_handler = page_flip_handler;
1188 evctx.vblank_handler = vblank_handler;
1189 drmHandleEvent(fd, &evctx);
1195 init_drm(struct drm_compositor *ec, struct udev_device *device)
1197 const char *filename, *sysnum;
1201 sysnum = udev_device_get_sysnum(device);
1203 ec->drm.id = atoi(sysnum);
1204 if (!sysnum || ec->drm.id < 0) {
1205 weston_log("cannot get device sysnum\n");
1209 filename = udev_device_get_devnode(device);
1210 fd = open(filename, O_RDWR | O_CLOEXEC);
1212 /* Probably permissions error */
1213 weston_log("couldn't open %s, skipping\n",
1214 udev_device_get_devnode(device));
1218 weston_log("using %s\n", filename);
1222 ret = drmGetCap(fd, DRM_CAP_TIMESTAMP_MONOTONIC, &cap);
1223 if (ret == 0 && cap == 1)
1224 ec->clock = CLOCK_MONOTONIC;
1226 ec->clock = CLOCK_REALTIME;
1232 init_egl(struct drm_compositor *ec)
1234 ec->gbm = gbm_create_device(ec->drm.fd);
1239 if (gl_renderer_create(&ec->base, ec->gbm, gl_renderer_opaque_attribs,
1241 gbm_device_destroy(ec->gbm);
1249 init_pixman(struct drm_compositor *ec)
1251 return pixman_renderer_init(&ec->base);
1254 static struct drm_mode *
1255 drm_output_add_mode(struct drm_output *output, drmModeModeInfo *info)
1257 struct drm_mode *mode;
1260 mode = malloc(sizeof *mode);
1264 mode->base.flags = 0;
1265 mode->base.width = info->hdisplay;
1266 mode->base.height = info->vdisplay;
1268 /* Calculate higher precision (mHz) refresh rate */
1269 refresh = (info->clock * 1000000LL / info->htotal +
1270 info->vtotal / 2) / info->vtotal;
1272 if (info->flags & DRM_MODE_FLAG_INTERLACE)
1274 if (info->flags & DRM_MODE_FLAG_DBLSCAN)
1276 if (info->vscan > 1)
1277 refresh /= info->vscan;
1279 mode->base.refresh = refresh;
1280 mode->mode_info = *info;
1282 if (info->type & DRM_MODE_TYPE_PREFERRED)
1283 mode->base.flags |= WL_OUTPUT_MODE_PREFERRED;
1285 wl_list_insert(output->base.mode_list.prev, &mode->base.link);
1291 drm_subpixel_to_wayland(int drm_value)
1293 switch (drm_value) {
1295 case DRM_MODE_SUBPIXEL_UNKNOWN:
1296 return WL_OUTPUT_SUBPIXEL_UNKNOWN;
1297 case DRM_MODE_SUBPIXEL_NONE:
1298 return WL_OUTPUT_SUBPIXEL_NONE;
1299 case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB:
1300 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB;
1301 case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR:
1302 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR;
1303 case DRM_MODE_SUBPIXEL_VERTICAL_RGB:
1304 return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB;
1305 case DRM_MODE_SUBPIXEL_VERTICAL_BGR:
1306 return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR;
1310 /* returns a value between 0-255 range, where higher is brighter */
1312 drm_get_backlight(struct drm_output *output)
1314 long brightness, max_brightness, norm;
1316 brightness = backlight_get_brightness(output->backlight);
1317 max_brightness = backlight_get_max_brightness(output->backlight);
1319 /* convert it on a scale of 0 to 255 */
1320 norm = (brightness * 255)/(max_brightness);
1322 return (uint32_t) norm;
1325 /* values accepted are between 0-255 range */
1327 drm_set_backlight(struct weston_output *output_base, uint32_t value)
1329 struct drm_output *output = (struct drm_output *) output_base;
1330 long max_brightness, new_brightness;
1332 if (!output->backlight)
1338 max_brightness = backlight_get_max_brightness(output->backlight);
1340 /* get denormalized value */
1341 new_brightness = (value * max_brightness) / 255;
1343 backlight_set_brightness(output->backlight, new_brightness);
1346 static drmModePropertyPtr
1347 drm_get_prop(int fd, drmModeConnectorPtr connector, const char *name)
1349 drmModePropertyPtr props;
1352 for (i = 0; i < connector->count_props; i++) {
1353 props = drmModeGetProperty(fd, connector->props[i]);
1357 if (!strcmp(props->name, name))
1360 drmModeFreeProperty(props);
1367 drm_set_dpms(struct weston_output *output_base, enum dpms_enum level)
1369 struct drm_output *output = (struct drm_output *) output_base;
1370 struct weston_compositor *ec = output_base->compositor;
1371 struct drm_compositor *c = (struct drm_compositor *) ec;
1373 if (!output->dpms_prop)
1376 drmModeConnectorSetProperty(c->drm.fd, output->connector_id,
1377 output->dpms_prop->prop_id, level);
1380 static const char *connector_type_names[] = {
1399 find_crtc_for_connector(struct drm_compositor *ec,
1400 drmModeRes *resources, drmModeConnector *connector)
1402 drmModeEncoder *encoder;
1403 uint32_t possible_crtcs;
1406 for (j = 0; j < connector->count_encoders; j++) {
1407 encoder = drmModeGetEncoder(ec->drm.fd, connector->encoders[j]);
1408 if (encoder == NULL) {
1409 weston_log("Failed to get encoder.\n");
1412 possible_crtcs = encoder->possible_crtcs;
1413 drmModeFreeEncoder(encoder);
1415 for (i = 0; i < resources->count_crtcs; i++) {
1416 if (possible_crtcs & (1 << i) &&
1417 !(ec->crtc_allocator & (1 << resources->crtcs[i])))
1425 /* Init output state that depends on gl or gbm */
1427 drm_output_init_egl(struct drm_output *output, struct drm_compositor *ec)
1431 output->surface = gbm_surface_create(ec->gbm,
1432 output->base.current->width,
1433 output->base.current->height,
1434 GBM_FORMAT_XRGB8888,
1435 GBM_BO_USE_SCANOUT |
1436 GBM_BO_USE_RENDERING);
1437 if (!output->surface) {
1438 weston_log("failed to create gbm surface\n");
1442 if (gl_renderer_output_create(&output->base, output->surface) < 0) {
1443 weston_log("failed to create gl renderer output state\n");
1444 gbm_surface_destroy(output->surface);
1448 flags = GBM_BO_USE_CURSOR_64X64 | GBM_BO_USE_WRITE;
1450 for (i = 0; i < 2; i++) {
1451 if (output->cursor_bo[i])
1454 output->cursor_bo[i] =
1455 gbm_bo_create(ec->gbm, 64, 64, GBM_FORMAT_ARGB8888,
1459 if (output->cursor_bo[0] == NULL || output->cursor_bo[1] == NULL) {
1460 weston_log("cursor buffers unavailable, using gl cursors\n");
1461 ec->cursors_are_broken = 1;
1468 drm_output_init_pixman(struct drm_output *output, struct drm_compositor *c)
1470 int w = output->base.current->width;
1471 int h = output->base.current->height;
1474 /* FIXME error checking */
1476 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
1477 output->dumb[i] = drm_fb_create_dumb(c, w, h);
1478 if (!output->dumb[i])
1482 pixman_image_create_bits(PIXMAN_x8r8g8b8, w, h,
1483 output->dumb[i]->map,
1484 output->dumb[i]->stride);
1485 if (!output->image[i])
1489 if (pixman_renderer_output_create(&output->base) < 0)
1492 pixman_region32_init_rect(&output->previous_damage,
1493 output->base.x, output->base.y, output->base.width, output->base.height);
1498 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
1499 if (output->dumb[i])
1500 drm_fb_destroy_dumb(output->dumb[i]);
1501 if (output->image[i])
1502 pixman_image_unref(output->image[i]);
1504 output->dumb[i] = NULL;
1505 output->image[i] = NULL;
1512 drm_output_fini_pixman(struct drm_output *output)
1516 pixman_renderer_output_destroy(&output->base);
1517 pixman_region32_fini(&output->previous_damage);
1519 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
1520 drm_fb_destroy_dumb(output->dumb[i]);
1521 pixman_image_unref(output->image[i]);
1522 output->dumb[i] = NULL;
1523 output->image[i] = NULL;
1528 edid_parse_string(const uint8_t *data, char text[])
1533 /* this is always 12 bytes, but we can't guarantee it's null
1534 * terminated or not junk. */
1535 strncpy(text, (const char *) data, 12);
1537 /* remove insane chars */
1538 for (i = 0; text[i] != '\0'; i++) {
1539 if (text[i] == '\n' ||
1546 /* ensure string is printable */
1547 for (i = 0; text[i] != '\0'; i++) {
1548 if (!isprint(text[i])) {
1554 /* if the string is random junk, ignore the string */
1559 #define EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING 0xfe
1560 #define EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME 0xfc
1561 #define EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER 0xff
1562 #define EDID_OFFSET_DATA_BLOCKS 0x36
1563 #define EDID_OFFSET_LAST_BLOCK 0x6c
1564 #define EDID_OFFSET_PNPID 0x08
1565 #define EDID_OFFSET_SERIAL 0x0c
1568 edid_parse(struct drm_edid *edid, const uint8_t *data, size_t length)
1571 uint32_t serial_number;
1576 if (data[0] != 0x00 || data[1] != 0xff)
1579 /* decode the PNP ID from three 5 bit words packed into 2 bytes
1584 edid->pnp_id[0] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x7c) / 4) - 1;
1585 edid->pnp_id[1] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x3) * 8) + ((data[EDID_OFFSET_PNPID + 1] & 0xe0) / 32) - 1;
1586 edid->pnp_id[2] = 'A' + (data[EDID_OFFSET_PNPID + 1] & 0x1f) - 1;
1587 edid->pnp_id[3] = '\0';
1589 /* maybe there isn't a ASCII serial number descriptor, so use this instead */
1590 serial_number = (uint32_t) data[EDID_OFFSET_SERIAL + 0];
1591 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 1] * 0x100;
1592 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 2] * 0x10000;
1593 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 3] * 0x1000000;
1594 if (serial_number > 0)
1595 sprintf(edid->serial_number, "%lu", (unsigned long) serial_number);
1597 /* parse EDID data */
1598 for (i = EDID_OFFSET_DATA_BLOCKS;
1599 i <= EDID_OFFSET_LAST_BLOCK;
1601 /* ignore pixel clock data */
1607 /* any useful blocks? */
1608 if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME) {
1609 edid_parse_string(&data[i+5],
1610 edid->monitor_name);
1611 } else if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER) {
1612 edid_parse_string(&data[i+5],
1613 edid->serial_number);
1614 } else if (data[i+3] == EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING) {
1615 edid_parse_string(&data[i+5],
1623 find_and_parse_output_edid(struct drm_compositor *ec,
1624 struct drm_output *output,
1625 drmModeConnector *connector)
1627 drmModePropertyBlobPtr edid_blob = NULL;
1628 drmModePropertyPtr property;
1632 for (i = 0; i < connector->count_props && !edid_blob; i++) {
1633 property = drmModeGetProperty(ec->drm.fd, connector->props[i]);
1636 if ((property->flags & DRM_MODE_PROP_BLOB) &&
1637 !strcmp(property->name, "EDID")) {
1638 edid_blob = drmModeGetPropertyBlob(ec->drm.fd,
1639 connector->prop_values[i]);
1641 drmModeFreeProperty(property);
1646 rc = edid_parse(&output->edid,
1650 weston_log("EDID data '%s', '%s', '%s'\n",
1651 output->edid.pnp_id,
1652 output->edid.monitor_name,
1653 output->edid.serial_number);
1654 if (output->edid.pnp_id[0] != '\0')
1655 output->base.make = output->edid.pnp_id;
1656 if (output->edid.monitor_name[0] != '\0')
1657 output->base.model = output->edid.monitor_name;
1658 if (output->edid.serial_number[0] != '\0')
1659 output->base.serial_number = output->edid.serial_number;
1661 drmModeFreePropertyBlob(edid_blob);
1667 parse_modeline(const char *s, drmModeModeInfo *mode)
1673 mode->type = DRM_MODE_TYPE_USERDEF;
1679 if (sscanf(s, "%f %hd %hd %hd %hd %hd %hd %hd %hd %s %s",
1688 &mode->vtotal, hsync, vsync) != 11)
1691 mode->clock = fclock * 1000;
1692 if (strcmp(hsync, "+hsync") == 0)
1693 mode->flags |= DRM_MODE_FLAG_PHSYNC;
1694 else if (strcmp(hsync, "-hsync") == 0)
1695 mode->flags |= DRM_MODE_FLAG_NHSYNC;
1699 if (strcmp(vsync, "+vsync") == 0)
1700 mode->flags |= DRM_MODE_FLAG_PVSYNC;
1701 else if (strcmp(vsync, "-vsync") == 0)
1702 mode->flags |= DRM_MODE_FLAG_NVSYNC;
1710 parse_transform(const char *transform, const char *output_name)
1712 static const struct { const char *name; uint32_t token; } names[] = {
1713 { "normal", WL_OUTPUT_TRANSFORM_NORMAL },
1714 { "90", WL_OUTPUT_TRANSFORM_90 },
1715 { "180", WL_OUTPUT_TRANSFORM_180 },
1716 { "270", WL_OUTPUT_TRANSFORM_270 },
1717 { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED },
1718 { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
1719 { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
1720 { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
1724 for (i = 0; i < ARRAY_LENGTH(names); i++)
1725 if (strcmp(names[i].name, transform) == 0)
1726 return names[i].token;
1728 weston_log("Invalid transform \"%s\" for output %s\n",
1729 transform, output_name);
1731 return WL_OUTPUT_TRANSFORM_NORMAL;
1735 create_output_for_connector(struct drm_compositor *ec,
1736 drmModeRes *resources,
1737 drmModeConnector *connector,
1738 int x, int y, struct udev_device *drm_device)
1740 struct drm_output *output;
1741 struct drm_mode *drm_mode, *next, *preferred, *current, *configured;
1742 struct weston_mode *m;
1743 struct weston_config_section *section;
1744 drmModeEncoder *encoder;
1745 drmModeModeInfo crtc_mode, modeline;
1747 int i, width, height, scale;
1749 const char *type_name;
1750 enum output_config config;
1753 i = find_crtc_for_connector(ec, resources, connector);
1755 weston_log("No usable crtc/encoder pair for connector.\n");
1759 output = malloc(sizeof *output);
1763 memset(output, 0, sizeof *output);
1764 output->base.subpixel = drm_subpixel_to_wayland(connector->subpixel);
1765 output->base.make = "unknown";
1766 output->base.model = "unknown";
1767 output->base.serial_number = "unknown";
1768 wl_list_init(&output->base.mode_list);
1770 if (connector->connector_type < ARRAY_LENGTH(connector_type_names))
1771 type_name = connector_type_names[connector->connector_type];
1773 type_name = "UNKNOWN";
1774 snprintf(name, 32, "%s%d", type_name, connector->connector_type_id);
1775 output->base.name = strdup(name);
1777 section = weston_config_get_section(ec->base.config, "output", "name",
1779 weston_config_section_get_string(section, "mode", &s, "preferred");
1780 if (strcmp(s, "off") == 0)
1781 config = OUTPUT_CONFIG_OFF;
1782 else if (strcmp(s, "preferred") == 0)
1783 config = OUTPUT_CONFIG_PREFERRED;
1784 else if (strcmp(s, "current") == 0)
1785 config = OUTPUT_CONFIG_CURRENT;
1786 else if (sscanf(s, "%dx%d", &width, &height) == 2)
1787 config = OUTPUT_CONFIG_MODE;
1788 else if (parse_modeline(s, &modeline) == 0)
1789 config = OUTPUT_CONFIG_MODELINE;
1791 weston_log("Invalid mode \"%s\" for output %s\n",
1792 s, output->base.name);
1793 config = OUTPUT_CONFIG_PREFERRED;
1797 weston_config_section_get_int(section, "scale", &scale, 1);
1798 weston_config_section_get_string(section, "transform", &s, "normal");
1799 transform = parse_transform(s, output->base.name);
1802 output->crtc_id = resources->crtcs[i];
1804 ec->crtc_allocator |= (1 << output->crtc_id);
1805 output->connector_id = connector->connector_id;
1806 ec->connector_allocator |= (1 << output->connector_id);
1808 output->original_crtc = drmModeGetCrtc(ec->drm.fd, output->crtc_id);
1809 output->dpms_prop = drm_get_prop(ec->drm.fd, connector, "DPMS");
1811 /* Get the current mode on the crtc that's currently driving
1812 * this connector. */
1813 encoder = drmModeGetEncoder(ec->drm.fd, connector->encoder_id);
1814 memset(&crtc_mode, 0, sizeof crtc_mode);
1815 if (encoder != NULL) {
1816 crtc = drmModeGetCrtc(ec->drm.fd, encoder->crtc_id);
1817 drmModeFreeEncoder(encoder);
1820 if (crtc->mode_valid)
1821 crtc_mode = crtc->mode;
1822 drmModeFreeCrtc(crtc);
1825 for (i = 0; i < connector->count_modes; i++) {
1826 drm_mode = drm_output_add_mode(output, &connector->modes[i]);
1831 if (config == OUTPUT_CONFIG_OFF) {
1832 weston_log("Disabling output %s\n", output->base.name);
1833 drmModeSetCrtc(ec->drm.fd, output->crtc_id,
1834 0, 0, 0, 0, 0, NULL);
1842 wl_list_for_each(drm_mode, &output->base.mode_list, base.link) {
1843 if (config == OUTPUT_CONFIG_MODE &&
1844 width == drm_mode->base.width &&
1845 height == drm_mode->base.height)
1846 configured = drm_mode;
1847 if (!memcmp(&crtc_mode, &drm_mode->mode_info, sizeof crtc_mode))
1849 if (drm_mode->base.flags & WL_OUTPUT_MODE_PREFERRED)
1850 preferred = drm_mode;
1853 if (config == OUTPUT_CONFIG_MODELINE) {
1854 configured = drm_output_add_mode(output, &modeline);
1859 if (current == NULL && crtc_mode.clock != 0) {
1860 current = drm_output_add_mode(output, &crtc_mode);
1865 if (config == OUTPUT_CONFIG_CURRENT)
1866 configured = current;
1868 if (option_current_mode && current)
1869 output->base.current = ¤t->base;
1870 else if (configured)
1871 output->base.current = &configured->base;
1873 output->base.current = &preferred->base;
1875 output->base.current = ¤t->base;
1877 if (output->base.current == NULL) {
1878 weston_log("no available modes for %s\n", output->base.name);
1882 output->base.current->flags |= WL_OUTPUT_MODE_CURRENT;
1884 weston_output_init(&output->base, &ec->base, x, y,
1885 connector->mmWidth, connector->mmHeight,
1888 if (ec->use_pixman) {
1889 if (drm_output_init_pixman(output, ec) < 0) {
1890 weston_log("Failed to init output pixman state\n");
1893 } else if (drm_output_init_egl(output, ec) < 0) {
1894 weston_log("Failed to init output gl state\n");
1898 output->backlight = backlight_init(drm_device,
1899 connector->connector_type);
1900 if (output->backlight) {
1901 weston_log("Initialized backlight, device %s\n",
1902 output->backlight->path);
1903 output->base.set_backlight = drm_set_backlight;
1904 output->base.backlight_current = drm_get_backlight(output);
1906 weston_log("Failed to initialize backlight\n");
1909 wl_list_insert(ec->base.output_list.prev, &output->base.link);
1911 find_and_parse_output_edid(ec, output, connector);
1912 if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS)
1913 output->base.connection_internal = 1;
1915 output->base.origin = output->base.current;
1916 output->base.start_repaint_loop = drm_output_start_repaint_loop;
1917 output->base.repaint = drm_output_repaint;
1918 output->base.destroy = drm_output_destroy;
1919 output->base.assign_planes = drm_assign_planes;
1920 output->base.set_dpms = drm_set_dpms;
1921 output->base.switch_mode = drm_output_switch_mode;
1923 output->base.gamma_size = output->original_crtc->gamma_size;
1924 output->base.set_gamma = drm_output_set_gamma;
1926 weston_plane_init(&output->cursor_plane, 0, 0);
1927 weston_plane_init(&output->fb_plane, 0, 0);
1929 weston_compositor_stack_plane(&ec->base, &output->cursor_plane, NULL);
1930 weston_compositor_stack_plane(&ec->base, &output->fb_plane,
1931 &ec->base.primary_plane);
1933 weston_log("Output %s, (connector %d, crtc %d)\n",
1934 output->base.name, output->connector_id, output->crtc_id);
1935 wl_list_for_each(m, &output->base.mode_list, link)
1936 weston_log_continue(" mode %dx%d@%.1f%s%s%s\n",
1937 m->width, m->height, m->refresh / 1000.0,
1938 m->flags & WL_OUTPUT_MODE_PREFERRED ?
1940 m->flags & WL_OUTPUT_MODE_CURRENT ?
1942 connector->count_modes == 0 ?
1948 weston_output_destroy(&output->base);
1950 wl_list_for_each_safe(drm_mode, next, &output->base.mode_list,
1952 wl_list_remove(&drm_mode->base.link);
1956 drmModeFreeCrtc(output->original_crtc);
1957 ec->crtc_allocator &= ~(1 << output->crtc_id);
1958 ec->connector_allocator &= ~(1 << output->connector_id);
1965 create_sprites(struct drm_compositor *ec)
1967 struct drm_sprite *sprite;
1968 drmModePlaneRes *plane_res;
1969 drmModePlane *plane;
1972 plane_res = drmModeGetPlaneResources(ec->drm.fd);
1974 weston_log("failed to get plane resources: %s\n",
1979 for (i = 0; i < plane_res->count_planes; i++) {
1980 plane = drmModeGetPlane(ec->drm.fd, plane_res->planes[i]);
1984 sprite = malloc(sizeof(*sprite) + ((sizeof(uint32_t)) *
1985 plane->count_formats));
1987 weston_log("%s: out of memory\n",
1993 memset(sprite, 0, sizeof *sprite);
1995 sprite->possible_crtcs = plane->possible_crtcs;
1996 sprite->plane_id = plane->plane_id;
1997 sprite->current = NULL;
1998 sprite->next = NULL;
1999 sprite->compositor = ec;
2000 sprite->count_formats = plane->count_formats;
2001 memcpy(sprite->formats, plane->formats,
2002 plane->count_formats * sizeof(plane->formats[0]));
2003 drmModeFreePlane(plane);
2004 weston_plane_init(&sprite->plane, 0, 0);
2005 weston_compositor_stack_plane(&ec->base, &sprite->plane,
2006 &ec->base.primary_plane);
2008 wl_list_insert(&ec->sprite_list, &sprite->link);
2011 free(plane_res->planes);
2016 destroy_sprites(struct drm_compositor *compositor)
2018 struct drm_sprite *sprite, *next;
2019 struct drm_output *output;
2021 output = container_of(compositor->base.output_list.next,
2022 struct drm_output, base.link);
2024 wl_list_for_each_safe(sprite, next, &compositor->sprite_list, link) {
2025 drmModeSetPlane(compositor->drm.fd,
2027 output->crtc_id, 0, 0,
2028 0, 0, 0, 0, 0, 0, 0, 0);
2029 drm_output_release_fb(output, sprite->current);
2030 drm_output_release_fb(output, sprite->next);
2031 weston_plane_release(&sprite->plane);
2037 create_outputs(struct drm_compositor *ec, uint32_t option_connector,
2038 struct udev_device *drm_device)
2040 drmModeConnector *connector;
2041 drmModeRes *resources;
2045 resources = drmModeGetResources(ec->drm.fd);
2047 weston_log("drmModeGetResources failed\n");
2051 ec->crtcs = calloc(resources->count_crtcs, sizeof(uint32_t));
2053 drmModeFreeResources(resources);
2057 ec->min_width = resources->min_width;
2058 ec->max_width = resources->max_width;
2059 ec->min_height = resources->min_height;
2060 ec->max_height = resources->max_height;
2062 ec->num_crtcs = resources->count_crtcs;
2063 memcpy(ec->crtcs, resources->crtcs, sizeof(uint32_t) * ec->num_crtcs);
2065 for (i = 0; i < resources->count_connectors; i++) {
2066 connector = drmModeGetConnector(ec->drm.fd,
2067 resources->connectors[i]);
2068 if (connector == NULL)
2071 if (connector->connection == DRM_MODE_CONNECTED &&
2072 (option_connector == 0 ||
2073 connector->connector_id == option_connector)) {
2074 if (create_output_for_connector(ec, resources,
2077 drmModeFreeConnector(connector);
2081 x += container_of(ec->base.output_list.prev,
2082 struct weston_output,
2086 drmModeFreeConnector(connector);
2089 if (wl_list_empty(&ec->base.output_list)) {
2090 weston_log("No currently active connector found.\n");
2091 drmModeFreeResources(resources);
2095 drmModeFreeResources(resources);
2101 update_outputs(struct drm_compositor *ec, struct udev_device *drm_device)
2103 drmModeConnector *connector;
2104 drmModeRes *resources;
2105 struct drm_output *output, *next;
2107 int x_offset = 0, y_offset = 0;
2108 uint32_t connected = 0, disconnects = 0;
2111 resources = drmModeGetResources(ec->drm.fd);
2113 weston_log("drmModeGetResources failed\n");
2117 /* collect new connects */
2118 for (i = 0; i < resources->count_connectors; i++) {
2119 int connector_id = resources->connectors[i];
2121 connector = drmModeGetConnector(ec->drm.fd, connector_id);
2122 if (connector == NULL)
2125 if (connector->connection != DRM_MODE_CONNECTED) {
2126 drmModeFreeConnector(connector);
2130 connected |= (1 << connector_id);
2132 if (!(ec->connector_allocator & (1 << connector_id))) {
2133 struct weston_output *last =
2134 container_of(ec->base.output_list.prev,
2135 struct weston_output, link);
2137 /* XXX: not yet needed, we die with 0 outputs */
2138 if (!wl_list_empty(&ec->base.output_list))
2139 x = last->x + last->width;
2143 create_output_for_connector(ec, resources,
2146 weston_log("connector %d connected\n", connector_id);
2149 drmModeFreeConnector(connector);
2151 drmModeFreeResources(resources);
2153 disconnects = ec->connector_allocator & ~connected;
2155 wl_list_for_each_safe(output, next, &ec->base.output_list,
2157 if (x_offset != 0 || y_offset != 0) {
2158 weston_output_move(&output->base,
2159 output->base.x - x_offset,
2160 output->base.y - y_offset);
2163 if (disconnects & (1 << output->connector_id)) {
2164 disconnects &= ~(1 << output->connector_id);
2165 weston_log("connector %d disconnected\n",
2166 output->connector_id);
2167 x_offset += output->base.width;
2168 drm_output_destroy(&output->base);
2173 /* FIXME: handle zero outputs, without terminating */
2174 if (ec->connector_allocator == 0)
2175 wl_display_terminate(ec->base.wl_display);
2179 udev_event_is_hotplug(struct drm_compositor *ec, struct udev_device *device)
2184 sysnum = udev_device_get_sysnum(device);
2185 if (!sysnum || atoi(sysnum) != ec->drm.id)
2188 val = udev_device_get_property_value(device, "HOTPLUG");
2192 return strcmp(val, "1") == 0;
2196 udev_drm_event(int fd, uint32_t mask, void *data)
2198 struct drm_compositor *ec = data;
2199 struct udev_device *event;
2201 event = udev_monitor_receive_device(ec->udev_monitor);
2203 if (udev_event_is_hotplug(ec, event))
2204 update_outputs(ec, event);
2206 udev_device_unref(event);
2212 drm_restore(struct weston_compositor *ec)
2214 struct drm_compositor *d = (struct drm_compositor *) ec;
2216 if (weston_launcher_drm_set_master(&d->base, d->drm.fd, 0) < 0)
2217 weston_log("failed to drop master: %m\n");
2222 drm_destroy(struct weston_compositor *ec)
2224 struct drm_compositor *d = (struct drm_compositor *) ec;
2225 struct udev_seat *seat, *next;
2227 wl_list_for_each_safe(seat, next, &ec->seat_list, base.link)
2228 udev_seat_destroy(seat);
2230 wl_event_source_remove(d->udev_drm_source);
2231 wl_event_source_remove(d->drm_source);
2235 weston_compositor_shutdown(ec);
2237 ec->renderer->destroy(ec);
2240 gbm_device_destroy(d->gbm);
2242 if (weston_launcher_drm_set_master(&d->base, d->drm.fd, 0) < 0)
2243 weston_log("failed to drop master: %m\n");
2244 tty_destroy(d->tty);
2250 drm_compositor_set_modes(struct drm_compositor *compositor)
2252 struct drm_output *output;
2253 struct drm_mode *drm_mode;
2256 wl_list_for_each(output, &compositor->base.output_list, base.link) {
2257 if (!output->current) {
2258 /* If something that would cause the output to
2259 * switch mode happened while in another vt, we
2260 * might not have a current drm_fb. In that case,
2261 * schedule a repaint and let drm_output_repaint
2262 * handle setting the mode. */
2263 weston_output_schedule_repaint(&output->base);
2267 drm_mode = (struct drm_mode *) output->base.current;
2268 ret = drmModeSetCrtc(compositor->drm.fd, output->crtc_id,
2269 output->current->fb_id, 0, 0,
2270 &output->connector_id, 1,
2271 &drm_mode->mode_info);
2274 "failed to set mode %dx%d for output at %d,%d: %m\n",
2275 drm_mode->base.width, drm_mode->base.height,
2276 output->base.x, output->base.y);
2282 vt_func(struct weston_compositor *compositor, int event)
2284 struct drm_compositor *ec = (struct drm_compositor *) compositor;
2285 struct udev_seat *seat;
2286 struct drm_sprite *sprite;
2287 struct drm_output *output;
2291 weston_log("entering VT\n");
2292 compositor->focus = 1;
2293 if (weston_launcher_drm_set_master(&ec->base, ec->drm.fd, 1)) {
2294 weston_log("failed to set master: %m\n");
2295 wl_display_terminate(compositor->wl_display);
2297 compositor->state = ec->prev_state;
2298 drm_compositor_set_modes(ec);
2299 weston_compositor_damage_all(compositor);
2300 wl_list_for_each(seat, &compositor->seat_list, base.link)
2301 udev_seat_enable(seat, ec->udev);
2304 weston_log("leaving VT\n");
2305 wl_list_for_each(seat, &compositor->seat_list, base.link)
2306 udev_seat_disable(seat);
2308 compositor->focus = 0;
2309 ec->prev_state = compositor->state;
2310 weston_compositor_offscreen(compositor);
2312 /* If we have a repaint scheduled (either from a
2313 * pending pageflip or the idle handler), make sure we
2314 * cancel that so we don't try to pageflip when we're
2315 * vt switched away. The OFFSCREEN state will prevent
2316 * further attemps at repainting. When we switch
2317 * back, we schedule a repaint, which will process
2318 * pending frame callbacks. */
2320 wl_list_for_each(output, &ec->base.output_list, base.link) {
2321 output->base.repaint_needed = 0;
2322 drmModeSetCursor(ec->drm.fd, output->crtc_id, 0, 0, 0);
2325 output = container_of(ec->base.output_list.next,
2326 struct drm_output, base.link);
2328 wl_list_for_each(sprite, &ec->sprite_list, link)
2329 drmModeSetPlane(ec->drm.fd,
2331 output->crtc_id, 0, 0,
2332 0, 0, 0, 0, 0, 0, 0, 0);
2334 if (weston_launcher_drm_set_master(&ec->base, ec->drm.fd, 0) < 0)
2335 weston_log("failed to drop master: %m\n");
2342 switch_vt_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
2344 struct drm_compositor *ec = data;
2346 tty_activate_vt(ec->tty, key - KEY_F1 + 1);
2351 * Some systems may have multiple DRM devices attached to a single seat. This
2352 * function loops over all devices and tries to find a PCI device with the
2353 * boot_vga sysfs attribute set to 1.
2354 * If no such device is found, the first DRM device reported by udev is used.
2356 static struct udev_device*
2357 find_primary_gpu(struct drm_compositor *ec, const char *seat)
2359 struct udev_enumerate *e;
2360 struct udev_list_entry *entry;
2361 const char *path, *device_seat, *id;
2362 struct udev_device *device, *drm_device, *pci;
2364 e = udev_enumerate_new(ec->udev);
2365 udev_enumerate_add_match_subsystem(e, "drm");
2366 udev_enumerate_add_match_sysname(e, "card[0-9]*");
2368 udev_enumerate_scan_devices(e);
2370 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
2371 path = udev_list_entry_get_name(entry);
2372 device = udev_device_new_from_syspath(ec->udev, path);
2375 device_seat = udev_device_get_property_value(device, "ID_SEAT");
2377 device_seat = default_seat;
2378 if (strcmp(device_seat, seat)) {
2379 udev_device_unref(device);
2383 pci = udev_device_get_parent_with_subsystem_devtype(device,
2386 id = udev_device_get_sysattr_value(pci, "boot_vga");
2387 if (id && !strcmp(id, "1")) {
2389 udev_device_unref(drm_device);
2390 drm_device = device;
2396 drm_device = device;
2398 udev_device_unref(device);
2401 udev_enumerate_unref(e);
2406 planes_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
2408 struct drm_compositor *c = data;
2412 c->cursors_are_broken ^= 1;
2415 c->sprites_are_broken ^= 1;
2418 c->sprites_hidden ^= 1;
2425 static struct weston_compositor *
2426 drm_compositor_create(struct wl_display *display,
2427 int connector, const char *seat_id, int tty, int pixman,
2428 int *argc, char *argv[],
2429 struct weston_config *config)
2431 struct drm_compositor *ec;
2432 struct udev_device *drm_device;
2433 struct wl_event_loop *loop;
2434 struct udev_seat *udev_seat, *next;
2438 weston_log("initializing drm backend\n");
2440 ec = malloc(sizeof *ec);
2443 memset(ec, 0, sizeof *ec);
2445 /* KMS support for sprites is not complete yet, so disable the
2446 * functionality for now. */
2447 ec->sprites_are_broken = 1;
2449 ec->use_pixman = pixman;
2451 if (weston_compositor_init(&ec->base, display, argc, argv,
2453 weston_log("%s failed\n", __func__);
2457 /* Check if we run drm-backend using weston-launch */
2458 if (ec->base.launcher_sock == -1 && geteuid() != 0) {
2459 weston_log("fatal: drm backend should be run "
2460 "using weston-launch binary or as root\n");
2461 goto err_compositor;
2464 ec->udev = udev_new();
2465 if (ec->udev == NULL) {
2466 weston_log("failed to initialize udev context\n");
2467 goto err_compositor;
2470 ec->base.wl_display = display;
2471 ec->tty = tty_create(&ec->base, vt_func, tty);
2473 weston_log("failed to initialize tty\n");
2477 drm_device = find_primary_gpu(ec, seat_id);
2478 if (drm_device == NULL) {
2479 weston_log("no drm device found\n");
2482 path = udev_device_get_syspath(drm_device);
2484 if (init_drm(ec, drm_device) < 0) {
2485 weston_log("failed to initialize kms\n");
2489 if (ec->use_pixman) {
2490 if (init_pixman(ec) < 0) {
2491 weston_log("failed to initialize pixman renderer\n");
2495 if (init_egl(ec) < 0) {
2496 weston_log("failed to initialize egl\n");
2501 ec->base.destroy = drm_destroy;
2502 ec->base.restore = drm_restore;
2506 ec->prev_state = WESTON_COMPOSITOR_ACTIVE;
2508 for (key = KEY_F1; key < KEY_F9; key++)
2509 weston_compositor_add_key_binding(&ec->base, key,
2510 MODIFIER_CTRL | MODIFIER_ALT,
2511 switch_vt_binding, ec);
2513 wl_list_init(&ec->sprite_list);
2516 if (create_outputs(ec, connector, drm_device) < 0) {
2517 weston_log("failed to create output for %s\n", path);
2523 if (udev_input_create(&ec->base, ec->udev, seat_id) == NULL) {
2524 weston_log("failed to create input devices\n");
2528 loop = wl_display_get_event_loop(ec->base.wl_display);
2530 wl_event_loop_add_fd(loop, ec->drm.fd,
2531 WL_EVENT_READABLE, on_drm_input, ec);
2533 ec->udev_monitor = udev_monitor_new_from_netlink(ec->udev, "udev");
2534 if (ec->udev_monitor == NULL) {
2535 weston_log("failed to intialize udev monitor\n");
2536 goto err_drm_source;
2538 udev_monitor_filter_add_match_subsystem_devtype(ec->udev_monitor,
2540 ec->udev_drm_source =
2541 wl_event_loop_add_fd(loop,
2542 udev_monitor_get_fd(ec->udev_monitor),
2543 WL_EVENT_READABLE, udev_drm_event, ec);
2545 if (udev_monitor_enable_receiving(ec->udev_monitor) < 0) {
2546 weston_log("failed to enable udev-monitor receiving\n");
2547 goto err_udev_monitor;
2550 udev_device_unref(drm_device);
2552 weston_compositor_add_debug_binding(&ec->base, KEY_O,
2553 planes_binding, ec);
2554 weston_compositor_add_debug_binding(&ec->base, KEY_C,
2555 planes_binding, ec);
2556 weston_compositor_add_debug_binding(&ec->base, KEY_V,
2557 planes_binding, ec);
2562 wl_event_source_remove(ec->udev_drm_source);
2563 udev_monitor_unref(ec->udev_monitor);
2565 wl_event_source_remove(ec->drm_source);
2566 wl_list_for_each_safe(udev_seat, next, &ec->base.seat_list, base.link)
2567 udev_seat_destroy(udev_seat);
2569 ec->base.renderer->destroy(&ec->base);
2570 gbm_device_destroy(ec->gbm);
2571 destroy_sprites(ec);
2573 udev_device_unref(drm_device);
2575 if (weston_launcher_drm_set_master(&ec->base, ec->drm.fd, 0) < 0)
2576 weston_log("failed to drop master: %m\n");
2577 tty_destroy(ec->tty);
2579 udev_unref(ec->udev);
2581 weston_compositor_shutdown(&ec->base);
2587 WL_EXPORT struct weston_compositor *
2588 backend_init(struct wl_display *display, int *argc, char *argv[],
2589 struct weston_config *config)
2591 int connector = 0, tty = 0, use_pixman = 0;
2592 const char *seat_id = default_seat;
2594 const struct weston_option drm_options[] = {
2595 { WESTON_OPTION_INTEGER, "connector", 0, &connector },
2596 { WESTON_OPTION_STRING, "seat", 0, &seat_id },
2597 { WESTON_OPTION_INTEGER, "tty", 0, &tty },
2598 { WESTON_OPTION_BOOLEAN, "current-mode", 0, &option_current_mode },
2599 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
2602 parse_options(drm_options, ARRAY_LENGTH(drm_options), argc, argv);
2604 return drm_compositor_create(display, connector, seat_id, tty, use_pixman,
2605 argc, argv, config);