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.
34 #include <xf86drmMode.h>
35 #include <drm_fourcc.h>
38 #include <libbacklight.h>
40 #include "compositor.h"
43 struct drm_compositor {
44 struct weston_compositor base;
47 struct wl_event_source *drm_source;
49 struct udev_monitor *udev_monitor;
50 struct wl_event_source *udev_drm_source;
56 struct gbm_device *gbm;
59 uint32_t crtc_allocator;
60 uint32_t connector_allocator;
63 struct gbm_surface *dummy_surface;
64 EGLSurface dummy_egl_surface;
66 struct wl_list sprite_list;
67 int sprites_are_broken;
73 struct weston_mode base;
74 drmModeModeInfo mode_info;
78 struct weston_output base;
81 uint32_t connector_id;
82 drmModeCrtcPtr original_crtc;
84 struct gbm_surface *surface;
85 EGLSurface egl_surface;
86 uint32_t current_fb_id;
88 struct gbm_bo *current_bo;
89 struct gbm_bo *next_bo;
91 struct wl_buffer *scanout_buffer;
92 struct wl_listener scanout_buffer_destroy_listener;
93 struct wl_buffer *pending_scanout_buffer;
94 struct wl_listener pending_scanout_buffer_destroy_listener;
95 struct backlight *backlight;
99 * An output has a primary display plane plus zero or more sprites for
100 * blending display contents.
106 uint32_t pending_fb_id;
107 struct weston_surface *surface;
108 struct weston_surface *pending_surface;
110 struct drm_compositor *compositor;
112 struct wl_listener destroy_listener;
113 struct wl_listener pending_destroy_listener;
115 uint32_t possible_crtcs;
117 uint32_t count_formats;
119 int32_t src_x, src_y;
120 uint32_t src_w, src_h;
121 uint32_t dest_x, dest_y;
122 uint32_t dest_w, dest_h;
128 surface_is_primary(struct weston_compositor *ec, struct weston_surface *es)
130 struct weston_surface *primary;
132 primary = container_of(ec->surface_list.next, struct weston_surface,
140 drm_sprite_crtc_supported(struct weston_output *output_base, uint32_t supported)
142 struct weston_compositor *ec = output_base->compositor;
143 struct drm_compositor *c =(struct drm_compositor *) ec;
144 struct drm_output *output = (struct drm_output *) output_base;
147 for (crtc = 0; crtc < c->num_crtcs; crtc++) {
148 if (c->crtcs[crtc] != output->crtc_id)
151 if (supported & (1 << crtc))
159 drm_output_prepare_scanout_surface(struct drm_output *output)
161 struct drm_compositor *c =
162 (struct drm_compositor *) output->base.compositor;
163 struct weston_surface *es;
165 es = container_of(c->base.surface_list.next,
166 struct weston_surface, link);
168 /* Need to verify output->region contained in surface opaque
169 * region. Or maybe just that format doesn't have alpha. */
172 if (es->geometry.x != output->base.x ||
173 es->geometry.y != output->base.y ||
174 es->geometry.width != output->base.current->width ||
175 es->geometry.height != output->base.current->height ||
176 es->transform.enabled ||
177 es->image == EGL_NO_IMAGE_KHR)
181 gbm_bo_create_from_egl_image(c->gbm,
182 c->base.display, es->image,
187 /* assert output->pending_scanout_buffer == NULL */
188 output->pending_scanout_buffer = es->buffer;
189 output->pending_scanout_buffer->busy_count++;
191 wl_list_insert(output->pending_scanout_buffer->resource.destroy_listener_list.prev,
192 &output->pending_scanout_buffer_destroy_listener.link);
194 pixman_region32_fini(&es->damage);
195 pixman_region32_init(&es->damage);
201 drm_output_render(struct drm_output *output, pixman_region32_t *damage)
203 struct drm_compositor *compositor =
204 (struct drm_compositor *) output->base.compositor;
205 struct weston_surface *surface;
207 if (!eglMakeCurrent(compositor->base.display, output->egl_surface,
208 output->egl_surface, compositor->base.context)) {
209 fprintf(stderr, "failed to make current\n");
213 wl_list_for_each_reverse(surface, &compositor->base.surface_list, link)
214 weston_surface_draw(surface, &output->base, damage);
216 eglSwapBuffers(compositor->base.display, output->egl_surface);
217 output->next_bo = gbm_surface_lock_front_buffer(output->surface);
218 if (!output->next_bo) {
219 fprintf(stderr, "failed to lock front buffer: %m\n");
225 drm_output_repaint(struct weston_output *output_base,
226 pixman_region32_t *damage)
228 struct drm_output *output = (struct drm_output *) output_base;
229 struct drm_compositor *compositor =
230 (struct drm_compositor *) output->base.compositor;
231 struct drm_sprite *s;
232 struct drm_mode *mode;
233 uint32_t stride, handle;
236 drm_output_prepare_scanout_surface(output);
237 if (!output->next_bo)
238 drm_output_render(output, damage);
240 stride = gbm_bo_get_pitch(output->next_bo);
241 handle = gbm_bo_get_handle(output->next_bo).u32;
243 /* Destroy and set to NULL now so we don't try to
244 * gbm_surface_release_buffer() a client buffer in the
245 * page_flip_handler. */
246 if (output->pending_scanout_buffer) {
247 gbm_bo_destroy(output->next_bo);
248 output->next_bo = NULL;
251 ret = drmModeAddFB(compositor->drm.fd,
252 output->base.current->width,
253 output->base.current->height,
254 24, 32, stride, handle, &output->next_fb_id);
256 fprintf(stderr, "failed to create kms fb: %m\n");
257 gbm_surface_release_buffer(output->surface, output->next_bo);
258 output->next_bo = NULL;
262 mode = container_of(output->base.current, struct drm_mode, base);
263 if (output->current_fb_id == 0) {
264 ret = drmModeSetCrtc(compositor->drm.fd, output->crtc_id,
265 output->next_fb_id, 0, 0,
266 &output->connector_id, 1,
269 fprintf(stderr, "set mode failed: %m\n");
274 if (drmModePageFlip(compositor->drm.fd, output->crtc_id,
276 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
277 fprintf(stderr, "queueing pageflip failed: %m\n");
282 * Now, update all the sprite surfaces
284 wl_list_for_each(s, &compositor->sprite_list, link) {
287 .request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT,
288 .request.sequence = 1,
291 if (!drm_sprite_crtc_supported(output_base, s->possible_crtcs))
294 ret = drmModeSetPlane(compositor->drm.fd, s->plane_id,
295 output->crtc_id, s->pending_fb_id, flags,
296 s->dest_x, s->dest_y,
297 s->dest_w, s->dest_h,
301 fprintf(stderr, "setplane failed: %d: %s\n",
302 ret, strerror(errno));
305 * Queue a vblank signal so we know when the surface
306 * becomes active on the display or has been replaced.
308 vbl.request.signal = (unsigned long)s;
309 ret = drmWaitVBlank(compositor->drm.fd, &vbl);
311 fprintf(stderr, "vblank event request failed: %d: %s\n",
312 ret, strerror(errno));
320 vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec,
323 struct drm_sprite *s = (struct drm_sprite *)data;
324 struct drm_compositor *c = s->compositor;
327 weston_buffer_post_release(s->surface->buffer);
328 wl_list_remove(&s->destroy_listener.link);
330 drmModeRmFB(c->drm.fd, s->fb_id);
334 if (s->pending_surface) {
335 wl_list_remove(&s->pending_destroy_listener.link);
336 wl_list_insert(s->pending_surface->buffer->resource.destroy_listener_list.prev,
337 &s->destroy_listener.link);
338 s->surface = s->pending_surface;
339 s->pending_surface = NULL;
340 s->fb_id = s->pending_fb_id;
341 s->pending_fb_id = 0;
346 page_flip_handler(int fd, unsigned int frame,
347 unsigned int sec, unsigned int usec, void *data)
349 struct drm_output *output = (struct drm_output *) data;
350 struct drm_compositor *c =
351 (struct drm_compositor *) output->base.compositor;
354 if (output->current_fb_id)
355 drmModeRmFB(c->drm.fd, output->current_fb_id);
356 output->current_fb_id = output->next_fb_id;
357 output->next_fb_id = 0;
359 if (output->scanout_buffer) {
360 weston_buffer_post_release(output->scanout_buffer);
361 wl_list_remove(&output->scanout_buffer_destroy_listener.link);
362 output->scanout_buffer = NULL;
363 } else if (output->current_bo) {
364 gbm_surface_release_buffer(output->surface,
368 output->current_bo = output->next_bo;
369 output->next_bo = NULL;
371 if (output->pending_scanout_buffer) {
372 output->scanout_buffer = output->pending_scanout_buffer;
373 wl_list_remove(&output->pending_scanout_buffer_destroy_listener.link);
374 wl_list_insert(output->scanout_buffer->resource.destroy_listener_list.prev,
375 &output->scanout_buffer_destroy_listener.link);
376 output->pending_scanout_buffer = NULL;
378 msecs = sec * 1000 + usec / 1000;
379 weston_output_finish_frame(&output->base, msecs);
383 drm_surface_format_supported(struct drm_sprite *s, uint32_t format)
387 for (i = 0; i < s->count_formats; i++)
388 if (s->formats[i] == format)
395 drm_surface_transform_supported(struct weston_surface *es)
397 if (es->transform.enabled)
404 drm_surface_overlap_supported(struct weston_output *output_base,
405 pixman_region32_t *overlap)
407 /* We could potentially use a color key here if the surface left
408 * to display has rectangular regions
410 if (pixman_region32_not_empty(overlap))
416 drm_disable_unused_sprites(struct weston_output *output_base)
418 struct weston_compositor *ec = output_base->compositor;
419 struct drm_compositor *c =(struct drm_compositor *) ec;
420 struct drm_output *output = (struct drm_output *) output_base;
421 struct drm_sprite *s;
424 wl_list_for_each(s, &c->sprite_list, link) {
425 if (s->pending_fb_id)
428 ret = drmModeSetPlane(c->drm.fd, s->plane_id,
429 output->crtc_id, 0, 0,
430 0, 0, 0, 0, 0, 0, 0, 0);
433 "failed to disable plane: %d: %s\n",
434 ret, strerror(errno));
435 drmModeRmFB(c->drm.fd, s->fb_id);
437 s->pending_surface = NULL;
439 s->pending_fb_id = 0;
444 * This function must take care to damage any previously assigned surface
445 * if the sprite ends up binding to a different surface than in the
449 drm_output_prepare_overlay_surface(struct weston_output *output_base,
450 struct weston_surface *es,
451 pixman_region32_t *overlap)
453 struct weston_compositor *ec = output_base->compositor;
454 struct drm_compositor *c =(struct drm_compositor *) ec;
455 struct drm_sprite *s;
457 EGLint handle, stride;
460 uint32_t handles[4], pitches[4], offsets[4];
462 pixman_region32_t dest_rect, src_rect;
466 if (c->sprites_are_broken)
469 if (surface_is_primary(ec, es))
472 if (es->image == EGL_NO_IMAGE_KHR)
475 if (!drm_surface_transform_supported(es))
478 if (!drm_surface_overlap_supported(output_base, overlap))
481 wl_list_for_each(s, &c->sprite_list, link) {
482 if (!drm_sprite_crtc_supported(output_base, s->possible_crtcs))
485 if (!s->pending_fb_id) {
491 /* No sprites available */
495 bo = gbm_bo_create_from_egl_image(c->gbm, c->base.display, es->image,
496 es->geometry.width, es->geometry.height,
498 format = gbm_bo_get_format(bo);
499 handle = gbm_bo_get_handle(bo).s32;
500 stride = gbm_bo_get_pitch(bo);
504 if (!drm_surface_format_supported(s, format))
514 ret = drmModeAddFB2(c->drm.fd, es->geometry.width, es->geometry.height,
515 format, handles, pitches, offsets,
518 fprintf(stderr, "addfb2 failed: %d\n", ret);
519 c->sprites_are_broken = 1;
523 if (s->surface && s->surface != es) {
524 struct weston_surface *old_surf = s->surface;
525 pixman_region32_fini(&old_surf->damage);
526 pixman_region32_init_rect(&old_surf->damage,
527 old_surf->geometry.x, old_surf->geometry.y,
528 old_surf->geometry.width, old_surf->geometry.height);
531 s->pending_fb_id = fb_id;
532 s->pending_surface = es;
533 es->buffer->busy_count++;
536 * Calculate the source & dest rects properly based on actual
537 * postion (note the caller has called weston_surface_update_transform()
540 pixman_region32_init(&dest_rect);
541 pixman_region32_intersect(&dest_rect, &es->transform.boundingbox,
542 &output_base->region);
543 pixman_region32_translate(&dest_rect, -output_base->x, -output_base->y);
544 box = pixman_region32_extents(&dest_rect);
547 s->dest_w = box->x2 - box->x1;
548 s->dest_h = box->y2 - box->y1;
549 pixman_region32_fini(&dest_rect);
551 pixman_region32_init(&src_rect);
552 pixman_region32_intersect(&src_rect, &es->transform.boundingbox,
553 &output_base->region);
554 pixman_region32_translate(&src_rect, -es->geometry.x, -es->geometry.y);
555 box = pixman_region32_extents(&src_rect);
556 s->src_x = box->x1 << 16;
557 s->src_y = box->y1 << 16;
558 s->src_w = (box->x2 - box->x1) << 16;
559 s->src_h = (box->y2 - box->y1) << 16;
560 pixman_region32_fini(&src_rect);
562 wl_list_insert(es->buffer->resource.destroy_listener_list.prev,
563 &s->pending_destroy_listener.link);
568 drm_output_set_cursor(struct weston_output *output_base,
569 struct weston_input_device *eid);
572 weston_output_set_cursor(struct weston_output *output,
573 struct weston_input_device *device,
574 pixman_region32_t *overlap)
576 pixman_region32_t cursor_region;
577 int prior_was_hardware;
579 if (device->sprite == NULL)
582 pixman_region32_init(&cursor_region);
583 pixman_region32_intersect(&cursor_region,
584 &device->sprite->transform.boundingbox,
587 if (!pixman_region32_not_empty(&cursor_region)) {
588 drm_output_set_cursor(output, NULL);
592 prior_was_hardware = device->hw_cursor;
593 if (pixman_region32_not_empty(overlap) ||
594 drm_output_set_cursor(output, device) < 0) {
595 if (prior_was_hardware) {
596 weston_surface_damage(device->sprite);
597 drm_output_set_cursor(output, NULL);
599 device->hw_cursor = 0;
601 if (!prior_was_hardware)
602 weston_surface_damage_below(device->sprite);
603 pixman_region32_fini(&device->sprite->damage);
604 pixman_region32_init(&device->sprite->damage);
605 device->hw_cursor = 1;
609 pixman_region32_fini(&cursor_region);
613 drm_assign_planes(struct weston_output *output)
615 struct weston_compositor *ec = output->compositor;
616 struct weston_surface *es;
617 pixman_region32_t overlap, surface_overlap;
618 struct weston_input_device *device;
621 * Find a surface for each sprite in the output using some heuristics:
623 * 2) frequency of update
624 * 3) opacity (though some hw might support alpha blending)
625 * 4) clipping (this can be fixed with color keys)
627 * The idea is to save on blitting since this should save power.
628 * If we can get a large video surface on the sprite for example,
629 * the main display surface may not need to update at all, and
630 * the client buffer can be used directly for the sprite surface
631 * as we do for flipping full screen surfaces.
633 pixman_region32_init(&overlap);
634 wl_list_for_each(es, &ec->surface_list, link) {
636 * FIXME: try to assign hw cursors here too, they're just
639 pixman_region32_init(&surface_overlap);
640 pixman_region32_intersect(&surface_overlap, &overlap,
641 &es->transform.boundingbox);
643 device = (struct weston_input_device *) ec->input_device;
644 if (es == device->sprite) {
645 weston_output_set_cursor(output, device,
648 if (!device->hw_cursor)
649 pixman_region32_union(&overlap, &overlap,
650 &es->transform.boundingbox);
651 } else if (!drm_output_prepare_overlay_surface(output, es,
653 pixman_region32_fini(&es->damage);
654 pixman_region32_init(&es->damage);
656 pixman_region32_union(&overlap, &overlap,
657 &es->transform.boundingbox);
659 pixman_region32_fini(&surface_overlap);
661 pixman_region32_fini(&overlap);
663 drm_disable_unused_sprites(output);
667 drm_output_set_cursor(struct weston_output *output_base,
668 struct weston_input_device *eid)
670 struct drm_output *output = (struct drm_output *) output_base;
671 struct drm_compositor *c =
672 (struct drm_compositor *) output->base.compositor;
673 EGLint handle, stride;
678 drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0);
682 if (eid->sprite->image == EGL_NO_IMAGE_KHR)
685 if (eid->sprite->geometry.width > 64 ||
686 eid->sprite->geometry.height > 64)
689 bo = gbm_bo_create_from_egl_image(c->gbm,
691 eid->sprite->image, 64, 64,
692 GBM_BO_USE_CURSOR_64X64);
693 /* Not suitable for hw cursor, fall back */
697 handle = gbm_bo_get_handle(bo).s32;
698 stride = gbm_bo_get_pitch(bo);
701 /* gbm_bo_create_from_egl_image() didn't always validate the usage
702 * flags, and in that case we might end up with a bad stride. */
703 if (stride != 64 * 4)
706 ret = drmModeSetCursor(c->drm.fd, output->crtc_id, handle, 64, 64);
708 fprintf(stderr, "failed to set cursor: %s\n", strerror(-ret));
712 ret = drmModeMoveCursor(c->drm.fd, output->crtc_id,
713 eid->sprite->geometry.x - output->base.x,
714 eid->sprite->geometry.y - output->base.y);
716 fprintf(stderr, "failed to move cursor: %s\n", strerror(-ret));
722 drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0);
727 drm_output_destroy(struct weston_output *output_base)
729 struct drm_output *output = (struct drm_output *) output_base;
730 struct drm_compositor *c =
731 (struct drm_compositor *) output->base.compositor;
732 drmModeCrtcPtr origcrtc = output->original_crtc;
734 if (output->backlight)
735 backlight_destroy(output->backlight);
737 /* Turn off hardware cursor */
738 drm_output_set_cursor(&output->base, NULL);
740 /* Restore original CRTC state */
741 drmModeSetCrtc(c->drm.fd, origcrtc->crtc_id, origcrtc->buffer_id,
742 origcrtc->x, origcrtc->y,
743 &output->connector_id, 1, &origcrtc->mode);
744 drmModeFreeCrtc(origcrtc);
746 c->crtc_allocator &= ~(1 << output->crtc_id);
747 c->connector_allocator &= ~(1 << output->connector_id);
749 eglDestroySurface(c->base.display, output->egl_surface);
750 gbm_surface_destroy(output->surface);
752 weston_output_destroy(&output->base);
753 wl_list_remove(&output->base.link);
759 on_drm_input(int fd, uint32_t mask, void *data)
761 drmEventContext evctx;
763 memset(&evctx, 0, sizeof evctx);
764 evctx.version = DRM_EVENT_CONTEXT_VERSION;
765 evctx.page_flip_handler = page_flip_handler;
766 evctx.vblank_handler = vblank_handler;
767 drmHandleEvent(fd, &evctx);
773 init_egl(struct drm_compositor *ec, struct udev_device *device)
775 EGLint major, minor, n;
776 const char *filename, *sysnum;
778 static const EGLint context_attribs[] = {
779 EGL_CONTEXT_CLIENT_VERSION, 2,
783 sysnum = udev_device_get_sysnum(device);
785 ec->drm.id = atoi(sysnum);
786 if (!sysnum || ec->drm.id < 0) {
787 fprintf(stderr, "cannot get device sysnum\n");
791 static const EGLint config_attribs[] = {
792 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
797 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
801 filename = udev_device_get_devnode(device);
802 fd = open(filename, O_RDWR | O_CLOEXEC);
804 /* Probably permissions error */
805 fprintf(stderr, "couldn't open %s, skipping\n",
806 udev_device_get_devnode(device));
811 ec->gbm = gbm_create_device(ec->drm.fd);
812 ec->base.display = eglGetDisplay(ec->gbm);
813 if (ec->base.display == NULL) {
814 fprintf(stderr, "failed to create display\n");
818 if (!eglInitialize(ec->base.display, &major, &minor)) {
819 fprintf(stderr, "failed to initialize display\n");
823 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
824 fprintf(stderr, "failed to bind api EGL_OPENGL_ES_API\n");
828 if (!eglChooseConfig(ec->base.display, config_attribs,
829 &ec->base.config, 1, &n) || n != 1) {
830 fprintf(stderr, "failed to choose config: %d\n", n);
834 ec->base.context = eglCreateContext(ec->base.display, ec->base.config,
835 EGL_NO_CONTEXT, context_attribs);
836 if (ec->base.context == NULL) {
837 fprintf(stderr, "failed to create context\n");
841 ec->dummy_surface = gbm_surface_create(ec->gbm, 10, 10,
843 GBM_BO_USE_RENDERING);
844 if (!ec->dummy_surface) {
845 fprintf(stderr, "failed to create dummy gbm surface\n");
849 ec->dummy_egl_surface =
850 eglCreateWindowSurface(ec->base.display, ec->base.config,
851 ec->dummy_surface, NULL);
852 if (ec->dummy_egl_surface == EGL_NO_SURFACE) {
853 fprintf(stderr, "failed to create egl surface\n");
857 if (!eglMakeCurrent(ec->base.display, ec->dummy_egl_surface,
858 ec->dummy_egl_surface, ec->base.context)) {
859 fprintf(stderr, "failed to make context current\n");
866 static drmModeModeInfo builtin_1024x768 = {
868 1024, 1072, 1176, 1328, 0,
869 768, 771, 775, 798, 0,
871 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC,
878 drm_output_add_mode(struct drm_output *output, drmModeModeInfo *info)
880 struct drm_mode *mode;
882 mode = malloc(sizeof *mode);
886 mode->base.flags = 0;
887 mode->base.width = info->hdisplay;
888 mode->base.height = info->vdisplay;
889 mode->base.refresh = info->vrefresh;
890 mode->mode_info = *info;
891 wl_list_insert(output->base.mode_list.prev, &mode->base.link);
897 drm_subpixel_to_wayland(int drm_value)
901 case DRM_MODE_SUBPIXEL_UNKNOWN:
902 return WL_OUTPUT_SUBPIXEL_UNKNOWN;
903 case DRM_MODE_SUBPIXEL_NONE:
904 return WL_OUTPUT_SUBPIXEL_NONE;
905 case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB:
906 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB;
907 case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR:
908 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR;
909 case DRM_MODE_SUBPIXEL_VERTICAL_RGB:
910 return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB;
911 case DRM_MODE_SUBPIXEL_VERTICAL_BGR:
912 return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR;
917 output_handle_scanout_buffer_destroy(struct wl_listener *listener,
918 struct wl_resource *resource,
921 struct drm_output *output =
922 container_of(listener, struct drm_output,
923 scanout_buffer_destroy_listener);
925 output->scanout_buffer = NULL;
927 if (!output->pending_scanout_buffer)
928 weston_compositor_schedule_repaint(output->base.compositor);
932 output_handle_pending_scanout_buffer_destroy(struct wl_listener *listener,
933 struct wl_resource *resource,
936 struct drm_output *output =
937 container_of(listener, struct drm_output,
938 pending_scanout_buffer_destroy_listener);
940 output->pending_scanout_buffer = NULL;
942 weston_compositor_schedule_repaint(output->base.compositor);
946 sprite_handle_buffer_destroy(struct wl_listener *listener,
947 struct wl_resource *resource,
950 struct drm_sprite *sprite =
951 container_of(listener, struct drm_sprite,
954 sprite->surface = NULL;
958 sprite_handle_pending_buffer_destroy(struct wl_listener *listener,
959 struct wl_resource *resource,
962 struct drm_sprite *sprite =
963 container_of(listener, struct drm_sprite,
964 pending_destroy_listener);
966 sprite->pending_surface = NULL;
969 /* returns a value between 0-255 range, where higher is brighter */
971 drm_get_backlight(struct drm_output *output)
973 long brightness, max_brightness, norm;
975 brightness = backlight_get_brightness(output->backlight);
976 max_brightness = backlight_get_max_brightness(output->backlight);
978 /* convert it on a scale of 0 to 255 */
979 norm = (brightness * 255)/(max_brightness);
981 return (uint32_t) norm;
984 /* values accepted are between 0-255 range */
986 drm_set_backlight(struct weston_output *output_base, uint32_t value)
988 struct drm_output *output = (struct drm_output *) output_base;
989 long max_brightness, new_brightness;
991 if (!output->backlight)
994 if (value < 0 || value > 255)
997 max_brightness = backlight_get_max_brightness(output->backlight);
999 /* get denormalized value */
1000 new_brightness = (value * max_brightness) / 255;
1002 backlight_set_brightness(output->backlight, new_brightness);
1005 static drmModePropertyPtr
1006 drm_get_prop(int fd, drmModeConnectorPtr connector, const char *name)
1008 drmModePropertyPtr props;
1011 for (i = 0; i < connector->count_props; i++) {
1012 props = drmModeGetProperty(fd, connector->props[i]);
1016 if (!strcmp(props->name, name))
1019 drmModeFreeProperty(props);
1026 drm_set_dpms(struct weston_output *output_base, enum dpms_enum level)
1028 struct drm_output *output = (struct drm_output *) output_base;
1029 struct weston_compositor *ec = output_base->compositor;
1030 struct drm_compositor *c = (struct drm_compositor *) ec;
1031 drmModeConnectorPtr connector;
1032 drmModePropertyPtr prop;
1034 connector = drmModeGetConnector(c->drm.fd, output->connector_id);
1038 prop = drm_get_prop(c->drm.fd, connector, "DPMS");
1040 drmModeFreeConnector(connector);
1044 drmModeConnectorSetProperty(c->drm.fd, connector->connector_id,
1045 prop->prop_id, level);
1046 drmModeFreeProperty(prop);
1047 drmModeFreeConnector(connector);
1051 create_output_for_connector(struct drm_compositor *ec,
1052 drmModeRes *resources,
1053 drmModeConnector *connector,
1054 int x, int y, struct udev_device *drm_device)
1056 struct drm_output *output;
1057 struct drm_mode *drm_mode, *next;
1058 drmModeEncoder *encoder;
1061 encoder = drmModeGetEncoder(ec->drm.fd, connector->encoders[0]);
1062 if (encoder == NULL) {
1063 fprintf(stderr, "No encoder for connector.\n");
1067 for (i = 0; i < resources->count_crtcs; i++) {
1068 if (encoder->possible_crtcs & (1 << i) &&
1069 !(ec->crtc_allocator & (1 << resources->crtcs[i])))
1072 if (i == resources->count_crtcs) {
1073 fprintf(stderr, "No usable crtc for encoder.\n");
1074 drmModeFreeEncoder(encoder);
1078 output = malloc(sizeof *output);
1079 if (output == NULL) {
1080 drmModeFreeEncoder(encoder);
1084 memset(output, 0, sizeof *output);
1085 output->base.subpixel = drm_subpixel_to_wayland(connector->subpixel);
1086 output->base.make = "unknown";
1087 output->base.model = "unknown";
1088 wl_list_init(&output->base.mode_list);
1090 output->crtc_id = resources->crtcs[i];
1091 ec->crtc_allocator |= (1 << output->crtc_id);
1092 output->connector_id = connector->connector_id;
1093 ec->connector_allocator |= (1 << output->connector_id);
1095 output->original_crtc = drmModeGetCrtc(ec->drm.fd, output->crtc_id);
1096 drmModeFreeEncoder(encoder);
1098 for (i = 0; i < connector->count_modes; i++) {
1099 ret = drm_output_add_mode(output, &connector->modes[i]);
1104 if (connector->count_modes == 0) {
1105 ret = drm_output_add_mode(output, &builtin_1024x768);
1110 drm_mode = container_of(output->base.mode_list.next,
1111 struct drm_mode, base.link);
1112 output->base.current = &drm_mode->base;
1113 drm_mode->base.flags =
1114 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
1116 output->surface = gbm_surface_create(ec->gbm,
1117 output->base.current->width,
1118 output->base.current->height,
1119 GBM_FORMAT_XRGB8888,
1120 GBM_BO_USE_SCANOUT |
1121 GBM_BO_USE_RENDERING);
1122 if (!output->surface) {
1123 fprintf(stderr, "failed to create gbm surface\n");
1127 output->egl_surface =
1128 eglCreateWindowSurface(ec->base.display, ec->base.config,
1129 output->surface, NULL);
1130 if (output->egl_surface == EGL_NO_SURFACE) {
1131 fprintf(stderr, "failed to create egl surface\n");
1135 output->backlight = backlight_init(drm_device,
1136 connector->connector_type);
1137 if (output->backlight) {
1138 output->base.set_backlight = drm_set_backlight;
1139 output->base.backlight_current = drm_get_backlight(output);
1142 weston_output_init(&output->base, &ec->base, x, y,
1143 connector->mmWidth, connector->mmHeight,
1146 wl_list_insert(ec->base.output_list.prev, &output->base.link);
1148 output->scanout_buffer_destroy_listener.func =
1149 output_handle_scanout_buffer_destroy;
1150 output->pending_scanout_buffer_destroy_listener.func =
1151 output_handle_pending_scanout_buffer_destroy;
1153 output->next_fb_id = 0;
1154 output->base.repaint = drm_output_repaint;
1155 output->base.destroy = drm_output_destroy;
1156 output->base.assign_planes = drm_assign_planes;
1157 output->base.set_dpms = drm_set_dpms;
1161 eglDestroySurface(ec->base.display, output->egl_surface);
1163 gbm_surface_destroy(output->surface);
1165 wl_list_for_each_safe(drm_mode, next, &output->base.mode_list,
1167 wl_list_remove(&drm_mode->base.link);
1171 drmModeFreeCrtc(output->original_crtc);
1172 ec->crtc_allocator &= ~(1 << output->crtc_id);
1173 ec->connector_allocator &= ~(1 << output->connector_id);
1180 create_sprites(struct drm_compositor *ec)
1182 struct drm_sprite *sprite;
1183 drmModePlaneRes *plane_res;
1184 drmModePlane *plane;
1187 plane_res = drmModeGetPlaneResources(ec->drm.fd);
1189 fprintf(stderr, "failed to get plane resources: %s\n",
1194 for (i = 0; i < plane_res->count_planes; i++) {
1195 plane = drmModeGetPlane(ec->drm.fd, plane_res->planes[i]);
1199 sprite = malloc(sizeof(*sprite) + ((sizeof(uint32_t)) *
1200 plane->count_formats));
1202 fprintf(stderr, "%s: out of memory\n",
1208 memset(sprite, 0, sizeof *sprite);
1210 sprite->possible_crtcs = plane->possible_crtcs;
1211 sprite->plane_id = plane->plane_id;
1212 sprite->surface = NULL;
1213 sprite->pending_surface = NULL;
1215 sprite->pending_fb_id = 0;
1216 sprite->destroy_listener.func = sprite_handle_buffer_destroy;
1217 sprite->pending_destroy_listener.func =
1218 sprite_handle_pending_buffer_destroy;
1219 sprite->compositor = ec;
1220 sprite->count_formats = plane->count_formats;
1221 memcpy(sprite->formats, plane->formats,
1222 plane->count_formats * sizeof(plane->formats[0]));
1223 drmModeFreePlane(plane);
1225 wl_list_insert(&ec->sprite_list, &sprite->link);
1228 free(plane_res->planes);
1233 destroy_sprites(struct drm_compositor *compositor)
1235 struct drm_sprite *sprite, *next;
1236 struct drm_output *output;
1238 output = container_of(compositor->base.output_list.next,
1239 struct drm_output, base.link);
1241 wl_list_for_each_safe(sprite, next, &compositor->sprite_list, link) {
1242 drmModeSetPlane(compositor->drm.fd,
1244 output->crtc_id, 0, 0,
1245 0, 0, 0, 0, 0, 0, 0, 0);
1246 drmModeRmFB(compositor->drm.fd, sprite->fb_id);
1252 create_outputs(struct drm_compositor *ec, int option_connector,
1253 struct udev_device *drm_device)
1255 drmModeConnector *connector;
1256 drmModeRes *resources;
1260 resources = drmModeGetResources(ec->drm.fd);
1262 fprintf(stderr, "drmModeGetResources failed\n");
1266 ec->crtcs = calloc(resources->count_crtcs, sizeof(uint32_t));
1268 drmModeFreeResources(resources);
1272 ec->num_crtcs = resources->count_crtcs;
1273 memcpy(ec->crtcs, resources->crtcs, sizeof(uint32_t) * ec->num_crtcs);
1275 for (i = 0; i < resources->count_connectors; i++) {
1276 connector = drmModeGetConnector(ec->drm.fd,
1277 resources->connectors[i]);
1278 if (connector == NULL)
1281 if (connector->connection == DRM_MODE_CONNECTED &&
1282 (option_connector == 0 ||
1283 connector->connector_id == option_connector)) {
1284 if (create_output_for_connector(ec, resources,
1287 drmModeFreeConnector(connector);
1291 x += container_of(ec->base.output_list.prev,
1292 struct weston_output,
1293 link)->current->width;
1296 drmModeFreeConnector(connector);
1299 if (wl_list_empty(&ec->base.output_list)) {
1300 fprintf(stderr, "No currently active connector found.\n");
1301 drmModeFreeResources(resources);
1305 drmModeFreeResources(resources);
1311 update_outputs(struct drm_compositor *ec, struct udev_device *drm_device)
1313 drmModeConnector *connector;
1314 drmModeRes *resources;
1315 struct drm_output *output, *next;
1317 int x_offset = 0, y_offset = 0;
1318 uint32_t connected = 0, disconnects = 0;
1321 resources = drmModeGetResources(ec->drm.fd);
1323 fprintf(stderr, "drmModeGetResources failed\n");
1327 /* collect new connects */
1328 for (i = 0; i < resources->count_connectors; i++) {
1329 int connector_id = resources->connectors[i];
1331 connector = drmModeGetConnector(ec->drm.fd, connector_id);
1332 if (connector == NULL)
1335 if (connector->connection != DRM_MODE_CONNECTED) {
1336 drmModeFreeConnector(connector);
1340 connected |= (1 << connector_id);
1342 if (!(ec->connector_allocator & (1 << connector_id))) {
1343 struct weston_output *last =
1344 container_of(ec->base.output_list.prev,
1345 struct weston_output, link);
1347 /* XXX: not yet needed, we die with 0 outputs */
1348 if (!wl_list_empty(&ec->base.output_list))
1349 x = last->x + last->current->width;
1353 create_output_for_connector(ec, resources,
1356 printf("connector %d connected\n", connector_id);
1359 drmModeFreeConnector(connector);
1361 drmModeFreeResources(resources);
1363 disconnects = ec->connector_allocator & ~connected;
1365 wl_list_for_each_safe(output, next, &ec->base.output_list,
1367 if (x_offset != 0 || y_offset != 0) {
1368 weston_output_move(&output->base,
1369 output->base.x - x_offset,
1370 output->base.y - y_offset);
1373 if (disconnects & (1 << output->connector_id)) {
1374 disconnects &= ~(1 << output->connector_id);
1375 printf("connector %d disconnected\n",
1376 output->connector_id);
1377 x_offset += output->base.current->width;
1378 drm_output_destroy(&output->base);
1383 /* FIXME: handle zero outputs, without terminating */
1384 if (ec->connector_allocator == 0)
1385 wl_display_terminate(ec->base.wl_display);
1389 udev_event_is_hotplug(struct drm_compositor *ec, struct udev_device *device)
1394 sysnum = udev_device_get_sysnum(device);
1395 if (!sysnum || atoi(sysnum) != ec->drm.id)
1398 val = udev_device_get_property_value(device, "HOTPLUG");
1402 return strcmp(val, "1") == 0;
1406 udev_drm_event(int fd, uint32_t mask, void *data)
1408 struct drm_compositor *ec = data;
1409 struct udev_device *event;
1411 event = udev_monitor_receive_device(ec->udev_monitor);
1413 if (udev_event_is_hotplug(ec, event))
1414 update_outputs(ec, event);
1416 udev_device_unref(event);
1422 drm_destroy(struct weston_compositor *ec)
1424 struct drm_compositor *d = (struct drm_compositor *) ec;
1425 struct weston_input_device *input, *next;
1427 weston_compositor_shutdown(ec);
1428 gbm_device_destroy(d->gbm);
1430 drmDropMaster(d->drm.fd);
1431 tty_destroy(d->tty);
1433 wl_list_for_each_safe(input, next, &ec->input_device_list, link)
1434 evdev_input_destroy(input);
1440 drm_compositor_set_modes(struct drm_compositor *compositor)
1442 struct drm_output *output;
1443 struct drm_mode *drm_mode;
1446 wl_list_for_each(output, &compositor->base.output_list, base.link) {
1447 drm_mode = (struct drm_mode *) output->base.current;
1448 ret = drmModeSetCrtc(compositor->drm.fd, output->crtc_id,
1449 output->current_fb_id, 0, 0,
1450 &output->connector_id, 1,
1451 &drm_mode->mode_info);
1454 "failed to set mode %dx%d for output at %d,%d: %m\n",
1455 drm_mode->base.width, drm_mode->base.height,
1456 output->base.x, output->base.y);
1462 vt_func(struct weston_compositor *compositor, int event)
1464 struct drm_compositor *ec = (struct drm_compositor *) compositor;
1465 struct weston_output *output;
1466 struct weston_input_device *input;
1467 struct drm_sprite *sprite;
1468 struct drm_output *drm_output;
1472 compositor->focus = 1;
1473 if (drmSetMaster(ec->drm.fd)) {
1474 fprintf(stderr, "failed to set master: %m\n");
1475 wl_display_terminate(compositor->wl_display);
1477 compositor->state = ec->prev_state;
1478 drm_compositor_set_modes(ec);
1479 weston_compositor_damage_all(compositor);
1480 wl_list_for_each(input, &compositor->input_device_list, link)
1481 evdev_add_devices(ec->udev, input);
1484 compositor->focus = 0;
1485 ec->prev_state = compositor->state;
1486 compositor->state = WESTON_COMPOSITOR_SLEEPING;
1488 /* If we have a repaint scheduled (either from a
1489 * pending pageflip or the idle handler), make sure we
1490 * cancel that so we don't try to pageflip when we're
1491 * vt switched away. The SLEEPING state will prevent
1492 * further attemps at repainting. When we switch
1493 * back, we schedule a repaint, which will process
1494 * pending frame callbacks. */
1496 wl_list_for_each(output, &ec->base.output_list, link) {
1497 output->repaint_needed = 0;
1498 drm_output_set_cursor(output, NULL);
1501 drm_output = container_of(ec->base.output_list.next,
1502 struct drm_output, base.link);
1504 wl_list_for_each(sprite, &ec->sprite_list, link)
1505 drmModeSetPlane(ec->drm.fd,
1507 drm_output->crtc_id, 0, 0,
1508 0, 0, 0, 0, 0, 0, 0, 0);
1510 wl_list_for_each(input, &compositor->input_device_list, link)
1511 evdev_remove_devices(input);
1513 if (drmDropMaster(ec->drm.fd) < 0)
1514 fprintf(stderr, "failed to drop master: %m\n");
1520 static const char default_seat[] = "seat0";
1522 static struct weston_compositor *
1523 drm_compositor_create(struct wl_display *display,
1524 int connector, const char *seat, int tty)
1526 struct drm_compositor *ec;
1527 struct udev_enumerate *e;
1528 struct udev_list_entry *entry;
1529 struct udev_device *device, *drm_device;
1530 const char *path, *device_seat;
1531 struct wl_event_loop *loop;
1533 ec = malloc(sizeof *ec);
1537 memset(ec, 0, sizeof *ec);
1538 ec->udev = udev_new();
1539 if (ec->udev == NULL) {
1540 fprintf(stderr, "failed to initialize udev context\n");
1544 ec->base.wl_display = display;
1545 ec->tty = tty_create(&ec->base, vt_func, tty);
1547 fprintf(stderr, "failed to initialize tty\n");
1552 e = udev_enumerate_new(ec->udev);
1553 udev_enumerate_add_match_subsystem(e, "drm");
1554 udev_enumerate_add_match_sysname(e, "card[0-9]*");
1556 udev_enumerate_scan_devices(e);
1558 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
1559 path = udev_list_entry_get_name(entry);
1560 device = udev_device_new_from_syspath(ec->udev, path);
1562 udev_device_get_property_value(device, "ID_SEAT");
1564 device_seat = default_seat;
1565 if (strcmp(device_seat, seat) == 0) {
1566 drm_device = device;
1569 udev_device_unref(device);
1572 if (drm_device == NULL) {
1573 fprintf(stderr, "no drm device found\n");
1577 if (init_egl(ec, drm_device) < 0) {
1578 fprintf(stderr, "failed to initialize egl\n");
1582 ec->base.destroy = drm_destroy;
1586 ec->prev_state = WESTON_COMPOSITOR_ACTIVE;
1588 /* Can't init base class until we have a current egl context */
1589 if (weston_compositor_init(&ec->base, display) < 0)
1592 wl_list_init(&ec->sprite_list);
1595 if (create_outputs(ec, connector, drm_device) < 0) {
1596 fprintf(stderr, "failed to create output for %s\n", path);
1600 udev_device_unref(drm_device);
1601 udev_enumerate_unref(e);
1604 evdev_input_create(&ec->base, ec->udev, seat);
1606 loop = wl_display_get_event_loop(ec->base.wl_display);
1608 wl_event_loop_add_fd(loop, ec->drm.fd,
1609 WL_EVENT_READABLE, on_drm_input, ec);
1611 ec->udev_monitor = udev_monitor_new_from_netlink(ec->udev, "udev");
1612 if (ec->udev_monitor == NULL) {
1613 fprintf(stderr, "failed to intialize udev monitor\n");
1616 udev_monitor_filter_add_match_subsystem_devtype(ec->udev_monitor,
1618 ec->udev_drm_source =
1619 wl_event_loop_add_fd(loop,
1620 udev_monitor_get_fd(ec->udev_monitor),
1621 WL_EVENT_READABLE, udev_drm_event, ec);
1623 if (udev_monitor_enable_receiving(ec->udev_monitor) < 0) {
1624 fprintf(stderr, "failed to enable udev-monitor receiving\n");
1631 WL_EXPORT struct weston_compositor *
1632 backend_init(struct wl_display *display, int argc, char *argv[])
1634 int connector = 0, tty = 0;
1635 const char *seat = default_seat;
1637 const struct weston_option drm_options[] = {
1638 { WESTON_OPTION_INTEGER, "connector", 0, &connector },
1639 { WESTON_OPTION_STRING, "seat", 0, &seat },
1640 { WESTON_OPTION_INTEGER, "tty", 0, &tty },
1643 parse_options(drm_options, ARRAY_LENGTH(drm_options), argc, argv);
1645 return drm_compositor_create(display, connector, seat, tty);