2 * Copyright © 2008-2011 Kristian Høgsberg
3 * Copyright © 2011 Intel Corporation
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36 #include <linux/input.h>
44 #include <xf86drmMode.h>
45 #include <drm_fourcc.h>
50 #include "compositor.h"
51 #include "compositor-drm.h"
52 #include "shared/helpers.h"
53 #include "shared/timespec-util.h"
54 #include "gl-renderer.h"
55 #include "weston-egl-ext.h"
56 #include "pixman-renderer.h"
57 #include "pixel-formats.h"
58 #include "libbacklight.h"
59 #include "libinput-seat.h"
60 #include "launcher-util.h"
61 #include "vaapi-recorder.h"
62 #include "presentation-time-server-protocol.h"
63 #include "linux-dmabuf.h"
64 #include "linux-dmabuf-unstable-v1-server-protocol.h"
66 #ifndef DRM_CAP_TIMESTAMP_MONOTONIC
67 #define DRM_CAP_TIMESTAMP_MONOTONIC 0x6
70 #ifndef DRM_CLIENT_CAP_UNIVERSAL_PLANES
71 #define DRM_CLIENT_CAP_UNIVERSAL_PLANES 2
74 #ifndef DRM_CAP_CURSOR_WIDTH
75 #define DRM_CAP_CURSOR_WIDTH 0x8
78 #ifndef DRM_CAP_CURSOR_HEIGHT
79 #define DRM_CAP_CURSOR_HEIGHT 0x9
82 #ifndef GBM_BO_USE_CURSOR
83 #define GBM_BO_USE_CURSOR GBM_BO_USE_CURSOR_64X64
86 #define MAX_CLONED_CONNECTORS 1
89 * Represents the values of an enum-type KMS property
91 struct drm_property_enum_info {
92 const char *name; /**< name as string (static, not freed) */
93 bool valid; /**< true if value is supported; ignore if false */
94 uint64_t value; /**< raw value */
98 * Holds information on a DRM property, including its ID and the enum
101 * DRM properties are allocated dynamically, and maintained as DRM objects
102 * within the normal object ID space; they thus do not have a stable ID
103 * to refer to. This includes enum values, which must be referred to by
104 * integer values, but these are not stable.
106 * drm_property_info allows a cache to be maintained where Weston can use
107 * enum values internally to refer to properties, with the mapping to DRM
108 * ID values being maintained internally.
110 struct drm_property_info {
111 const char *name; /**< name as string (static, not freed) */
112 uint32_t prop_id; /**< KMS property object ID */
113 unsigned int num_enum_values; /**< number of enum values */
114 struct drm_property_enum_info *enum_values; /**< array of enum values */
118 * List of properties attached to DRM planes
120 enum wdrm_plane_property {
136 * Possible values for the WDRM_PLANE_TYPE property.
138 enum wdrm_plane_type {
139 WDRM_PLANE_TYPE_PRIMARY = 0,
140 WDRM_PLANE_TYPE_CURSOR,
141 WDRM_PLANE_TYPE_OVERLAY,
142 WDRM_PLANE_TYPE__COUNT
145 static struct drm_property_enum_info plane_type_enums[] = {
146 [WDRM_PLANE_TYPE_PRIMARY] = {
149 [WDRM_PLANE_TYPE_OVERLAY] = {
152 [WDRM_PLANE_TYPE_CURSOR] = {
157 static const struct drm_property_info plane_props[] = {
158 [WDRM_PLANE_TYPE] = {
160 .enum_values = plane_type_enums,
161 .num_enum_values = WDRM_PLANE_TYPE__COUNT,
163 [WDRM_PLANE_SRC_X] = { .name = "SRC_X", },
164 [WDRM_PLANE_SRC_Y] = { .name = "SRC_Y", },
165 [WDRM_PLANE_SRC_W] = { .name = "SRC_W", },
166 [WDRM_PLANE_SRC_H] = { .name = "SRC_H", },
167 [WDRM_PLANE_CRTC_X] = { .name = "CRTC_X", },
168 [WDRM_PLANE_CRTC_Y] = { .name = "CRTC_Y", },
169 [WDRM_PLANE_CRTC_W] = { .name = "CRTC_W", },
170 [WDRM_PLANE_CRTC_H] = { .name = "CRTC_H", },
171 [WDRM_PLANE_FB_ID] = { .name = "FB_ID", },
172 [WDRM_PLANE_CRTC_ID] = { .name = "CRTC_ID", },
176 * List of properties attached to a DRM connector
178 enum wdrm_connector_property {
179 WDRM_CONNECTOR_EDID = 0,
181 WDRM_CONNECTOR_CRTC_ID,
182 WDRM_CONNECTOR__COUNT
185 static const struct drm_property_info connector_props[] = {
186 [WDRM_CONNECTOR_EDID] = { .name = "EDID" },
187 [WDRM_CONNECTOR_DPMS] = { .name = "DPMS" },
188 [WDRM_CONNECTOR_CRTC_ID] = { .name = "CRTC_ID", },
192 * List of properties attached to DRM CRTCs
194 enum wdrm_crtc_property {
195 WDRM_CRTC_MODE_ID = 0,
200 static const struct drm_property_info crtc_props[] = {
201 [WDRM_CRTC_MODE_ID] = { .name = "MODE_ID", },
202 [WDRM_CRTC_ACTIVE] = { .name = "ACTIVE", },
206 * Mode for drm_output_state_duplicate.
208 enum drm_output_state_duplicate_mode {
209 DRM_OUTPUT_STATE_CLEAR_PLANES, /**< reset all planes to off */
210 DRM_OUTPUT_STATE_PRESERVE_PLANES, /**< preserve plane state */
214 * Mode for drm_pending_state_apply and co.
216 enum drm_state_apply_mode {
217 DRM_STATE_APPLY_SYNC, /**< state fully processed */
218 DRM_STATE_APPLY_ASYNC, /**< state pending event delivery */
222 struct weston_backend base;
223 struct weston_compositor *compositor;
226 struct wl_event_source *drm_source;
228 struct udev_monitor *udev_monitor;
229 struct wl_event_source *udev_drm_source;
236 struct gbm_device *gbm;
237 struct wl_listener session_listener;
240 /* we need these parameters in order to not fail drmModeAddFB2()
241 * due to out of bounds dimensions, and then mistakenly set
242 * sprites_are_broken:
244 int min_width, max_width;
245 int min_height, max_height;
247 struct wl_list plane_list;
248 int sprites_are_broken;
255 /* CRTC IDs not used by any enabled output. */
256 struct wl_array unused_crtcs;
258 int cursors_are_broken;
260 bool universal_planes;
265 struct udev_input input;
267 int32_t cursor_width;
268 int32_t cursor_height;
270 uint32_t pageflip_timeout;
276 struct weston_mode base;
277 drmModeModeInfo mode_info;
282 BUFFER_INVALID = 0, /**< never used */
283 BUFFER_CLIENT, /**< directly sourced from client */
284 BUFFER_PIXMAN_DUMB, /**< internal Pixman rendering */
285 BUFFER_GBM_SURFACE, /**< internal EGL rendering */
286 BUFFER_CURSOR, /**< internal cursor buffer */
290 enum drm_fb_type type;
294 uint32_t fb_id, stride, handle, size;
295 const struct pixel_format_info *format;
298 struct weston_buffer_reference buffer_ref;
300 /* Used by gbm fbs */
302 struct gbm_surface *gbm_surface;
304 /* Used by dumb fbs */
310 char monitor_name[13];
312 char serial_number[13];
316 * Pending state holds one or more drm_output_state structures, collected from
317 * performing repaint. This pending state is transient, and only lives between
318 * beginning a repaint group and flushing the results: after flush, each
319 * output state will complete and be retired separately.
321 struct drm_pending_state {
322 struct drm_backend *backend;
323 struct wl_list output_list;
327 * Output state holds the dynamic state for one Weston output, i.e. a KMS CRTC,
328 * plus >= 1 each of encoder/connector/plane. Since everything but the planes
329 * is currently statically assigned per-output, we mainly use this to track
332 * pending_state is set when the output state is owned by a pending_state,
333 * i.e. when it is being constructed and has not yet been applied. When the
334 * output state has been applied, the owning pending_state is freed.
336 struct drm_output_state {
337 struct drm_pending_state *pending_state;
338 struct drm_output *output;
341 struct wl_list plane_list;
345 * Plane state holds the dynamic state for a plane: where it is positioned,
346 * and which buffer it is currently displaying.
348 * The plane state is owned by an output state, except when setting an initial
349 * state. See drm_output_state for notes on state object lifetime.
351 struct drm_plane_state {
352 struct drm_plane *plane;
353 struct drm_output *output;
354 struct drm_output_state *output_state;
358 int32_t src_x, src_y;
359 uint32_t src_w, src_h;
360 int32_t dest_x, dest_y;
361 uint32_t dest_w, dest_h;
365 struct wl_list link; /* drm_output_state::plane_list */
369 * A plane represents one buffer, positioned within a CRTC, and stacked
370 * relative to other planes on the same CRTC.
372 * Each CRTC has a 'primary plane', which use used to display the classic
373 * framebuffer contents, as accessed through the legacy drmModeSetCrtc
374 * call (which combines setting the CRTC's actual physical mode, and the
375 * properties of the primary plane).
377 * The cursor plane also has its own alternate legacy API.
379 * Other planes are used opportunistically to display content we do not
380 * wish to blit into the primary plane. These non-primary/cursor planes
381 * are referred to as 'sprites'.
384 struct weston_plane base;
386 struct drm_backend *backend;
388 enum wdrm_plane_type type;
390 uint32_t possible_crtcs;
392 uint32_t count_formats;
394 struct drm_property_info props[WDRM_PLANE__COUNT];
396 /* The last state submitted to the kernel for this plane. */
397 struct drm_plane_state *state_cur;
405 struct weston_head base;
406 struct drm_backend *backend;
408 drmModeConnector *connector;
409 uint32_t connector_id;
410 struct drm_edid edid;
412 /* Holds the properties for the connector */
413 struct drm_property_info props_conn[WDRM_CONNECTOR__COUNT];
415 struct backlight *backlight;
417 drmModeModeInfo inherited_mode; /**< Original mode on the connector */
421 struct weston_output base;
423 uint32_t crtc_id; /* object ID to pass to DRM functions */
424 int pipe; /* index of CRTC in resource array / bitmasks */
426 /* Holds the properties for the CRTC */
427 struct drm_property_info props_crtc[WDRM_CRTC__COUNT];
430 int page_flip_pending;
431 int atomic_complete_pending;
434 int dpms_off_pending;
436 struct drm_fb *gbm_cursor_fb[2];
437 struct drm_plane *cursor_plane;
438 struct weston_view *cursor_view;
441 struct gbm_surface *gbm_surface;
444 /* Plane being displayed directly on the CRTC */
445 struct drm_plane *scanout_plane;
447 /* The last state submitted to the kernel for this CRTC. */
448 struct drm_output_state *state_cur;
449 /* The previously-submitted state, where the hardware has not
450 * yet acknowledged completion of state_cur. */
451 struct drm_output_state *state_last;
453 struct drm_fb *dumb[2];
454 pixman_image_t *image[2];
456 pixman_region32_t previous_damage;
458 struct vaapi_recorder *recorder;
459 struct wl_listener recorder_frame_listener;
461 struct wl_event_source *pageflip_timer;
464 static struct gl_renderer_interface *gl_renderer;
466 static const char default_seat[] = "seat0";
469 wl_array_remove_uint32(struct wl_array *array, uint32_t elm)
473 end = (uint32_t *) ((char *) array->data + array->size);
475 wl_array_for_each(pos, array) {
479 array->size -= sizeof(*pos);
483 memmove(pos, pos + 1, (char *) end - (char *) (pos + 1));
488 static inline struct drm_head *
489 to_drm_head(struct weston_head *base)
491 return container_of(base, struct drm_head, base);
494 static inline struct drm_output *
495 to_drm_output(struct weston_output *base)
497 return container_of(base, struct drm_output, base);
500 static inline struct drm_backend *
501 to_drm_backend(struct weston_compositor *base)
503 return container_of(base->backend, struct drm_backend, base);
507 pageflip_timeout(void *data) {
509 * Our timer just went off, that means we're not receiving drm
510 * page flip events anymore for that output. Let's gracefully exit
511 * weston with a return value so devs can debug what's going on.
513 struct drm_output *output = data;
514 struct weston_compositor *compositor = output->base.compositor;
516 weston_log("Pageflip timeout reached on output %s, your "
517 "driver is probably buggy! Exiting.\n",
519 weston_compositor_exit_with_code(compositor, EXIT_FAILURE);
524 /* Creates the pageflip timer. Note that it isn't armed by default */
526 drm_output_pageflip_timer_create(struct drm_output *output)
528 struct wl_event_loop *loop = NULL;
529 struct weston_compositor *ec = output->base.compositor;
531 loop = wl_display_get_event_loop(ec->wl_display);
533 output->pageflip_timer = wl_event_loop_add_timer(loop,
537 if (output->pageflip_timer == NULL) {
538 weston_log("creating drm pageflip timer failed: %m\n");
545 static inline struct drm_mode *
546 to_drm_mode(struct weston_mode *base)
548 return container_of(base, struct drm_mode, base);
552 * Get the current value of a KMS property
554 * Given a drmModeObjectGetProperties return, as well as the drm_property_info
555 * for the target property, return the current value of that property,
556 * with an optional default. If the property is a KMS enum type, the return
557 * value will be translated into the appropriate internal enum.
559 * If the property is not present, the default value will be returned.
561 * @param info Internal structure for property to look up
562 * @param props Raw KMS properties for the target object
563 * @param def Value to return if property is not found
566 drm_property_get_value(struct drm_property_info *info,
567 drmModeObjectPropertiesPtr props,
572 if (info->prop_id == 0)
575 for (i = 0; i < props->count_props; i++) {
578 if (props->props[i] != info->prop_id)
581 /* Simple (non-enum) types can return the value directly */
582 if (info->num_enum_values == 0)
583 return props->prop_values[i];
585 /* Map from raw value to enum value */
586 for (j = 0; j < info->num_enum_values; j++) {
587 if (!info->enum_values[j].valid)
589 if (info->enum_values[j].value != props->prop_values[i])
595 /* We don't have a mapping for this enum; return default. */
603 * Cache DRM property values
605 * Update a per-object array of drm_property_info structures, given the
606 * DRM properties of the object.
608 * Call this every time an object newly appears (note that only connectors
609 * can be hotplugged), the first time it is seen, or when its status changes
610 * in a way which invalidates the potential property values (currently, the
611 * only case for this is connector hotplug).
613 * This updates the property IDs and enum values within the drm_property_info
616 * DRM property enum values are dynamic at runtime; the user must query the
617 * property to find out the desired runtime value for a requested string
618 * name. Using the 'type' field on planes as an example, there is no single
619 * hardcoded constant for primary plane types; instead, the property must be
620 * queried at runtime to find the value associated with the string "Primary".
622 * This helper queries and caches the enum values, to allow us to use a set
623 * of compile-time-constant enums portably across various implementations.
624 * The values given in enum_names are searched for, and stored in the
625 * same-indexed field of the map array.
627 * @param b DRM backend object
628 * @param src DRM property info array to source from
629 * @param info DRM property info array to copy into
630 * @param num_infos Number of entries in the source array
631 * @param props DRM object properties for the object
634 drm_property_info_populate(struct drm_backend *b,
635 const struct drm_property_info *src,
636 struct drm_property_info *info,
637 unsigned int num_infos,
638 drmModeObjectProperties *props)
640 drmModePropertyRes *prop;
643 for (i = 0; i < num_infos; i++) {
646 info[i].name = src[i].name;
648 info[i].num_enum_values = src[i].num_enum_values;
650 if (src[i].num_enum_values == 0)
653 info[i].enum_values =
654 malloc(src[i].num_enum_values *
655 sizeof(*info[i].enum_values));
656 assert(info[i].enum_values);
657 for (j = 0; j < info[i].num_enum_values; j++) {
658 info[i].enum_values[j].name = src[i].enum_values[j].name;
659 info[i].enum_values[j].valid = false;
663 for (i = 0; i < props->count_props; i++) {
666 prop = drmModeGetProperty(b->drm.fd, props->props[i]);
670 for (j = 0; j < num_infos; j++) {
671 if (!strcmp(prop->name, info[j].name))
675 /* We don't know/care about this property. */
676 if (j == num_infos) {
678 weston_log("DRM debug: unrecognized property %u '%s'\n",
679 prop->prop_id, prop->name);
681 drmModeFreeProperty(prop);
685 if (info[j].num_enum_values == 0 &&
686 (prop->flags & DRM_MODE_PROP_ENUM)) {
687 weston_log("DRM: expected property %s to not be an"
688 " enum, but it is; ignoring\n", prop->name);
689 drmModeFreeProperty(prop);
693 info[j].prop_id = props->props[i];
695 if (info[j].num_enum_values == 0) {
696 drmModeFreeProperty(prop);
700 if (!(prop->flags & DRM_MODE_PROP_ENUM)) {
701 weston_log("DRM: expected property %s to be an enum,"
702 " but it is not; ignoring\n", prop->name);
703 drmModeFreeProperty(prop);
708 for (k = 0; k < info[j].num_enum_values; k++) {
711 for (l = 0; l < prop->count_enums; l++) {
712 if (!strcmp(prop->enums[l].name,
713 info[j].enum_values[k].name))
717 if (l == prop->count_enums)
720 info[j].enum_values[k].valid = true;
721 info[j].enum_values[k].value = prop->enums[l].value;
724 drmModeFreeProperty(prop);
728 for (i = 0; i < num_infos; i++) {
729 if (info[i].prop_id == 0)
730 weston_log("DRM warning: property '%s' missing\n",
737 * Free DRM property information
739 * Frees all memory associated with a DRM property info array and zeroes
740 * it out, leaving it usable for a further drm_property_info_update() or
741 * drm_property_info_free().
743 * @param info DRM property info array
744 * @param num_props Number of entries in array to free
747 drm_property_info_free(struct drm_property_info *info, int num_props)
751 for (i = 0; i < num_props; i++)
752 free(info[i].enum_values);
754 memset(info, 0, sizeof(*info) * num_props);
758 drm_output_set_cursor(struct drm_output_state *output_state);
761 drm_output_update_msc(struct drm_output *output, unsigned int seq);
764 drm_output_destroy(struct weston_output *output_base);
767 * Returns true if the plane can be used on the given output for its current
771 drm_plane_is_available(struct drm_plane *plane, struct drm_output *output)
773 assert(plane->state_cur);
775 /* The plane still has a request not yet completed by the kernel. */
776 if (!plane->state_cur->complete)
779 /* The plane is still active on another output. */
780 if (plane->state_cur->output && plane->state_cur->output != output)
783 /* Check whether the plane can be used with this CRTC; possible_crtcs
784 * is a bitmask of CRTC indices (pipe), rather than CRTC object ID. */
785 return !!(plane->possible_crtcs & (1 << output->pipe));
788 static struct drm_output *
789 drm_output_find_by_crtc(struct drm_backend *b, uint32_t crtc_id)
791 struct drm_output *output;
793 wl_list_for_each(output, &b->compositor->output_list, base.link) {
794 if (output->crtc_id == crtc_id)
801 static struct drm_head *
802 drm_head_find_by_connector(struct drm_backend *backend, uint32_t connector_id)
804 struct weston_head *base;
805 struct drm_head *head;
807 wl_list_for_each(base,
808 &backend->compositor->head_list, compositor_link) {
809 head = to_drm_head(base);
810 if (head->connector_id == connector_id)
818 drm_fb_destroy(struct drm_fb *fb)
821 drmModeRmFB(fb->fd, fb->fb_id);
822 weston_buffer_reference(&fb->buffer_ref, NULL);
827 drm_fb_destroy_dumb(struct drm_fb *fb)
829 struct drm_mode_destroy_dumb destroy_arg;
831 assert(fb->type == BUFFER_PIXMAN_DUMB);
833 if (fb->map && fb->size > 0)
834 munmap(fb->map, fb->size);
836 memset(&destroy_arg, 0, sizeof(destroy_arg));
837 destroy_arg.handle = fb->handle;
838 drmIoctl(fb->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
844 drm_fb_destroy_gbm(struct gbm_bo *bo, void *data)
846 struct drm_fb *fb = data;
848 assert(fb->type == BUFFER_GBM_SURFACE || fb->type == BUFFER_CLIENT ||
849 fb->type == BUFFER_CURSOR);
853 static struct drm_fb *
854 drm_fb_create_dumb(struct drm_backend *b, int width, int height,
860 struct drm_mode_create_dumb create_arg;
861 struct drm_mode_destroy_dumb destroy_arg;
862 struct drm_mode_map_dumb map_arg;
863 uint32_t handles[4] = { 0 }, pitches[4] = { 0 }, offsets[4] = { 0 };
865 fb = zalloc(sizeof *fb);
871 fb->format = pixel_format_get_info(format);
873 weston_log("failed to look up format 0x%lx\n",
874 (unsigned long) format);
878 if (!fb->format->depth || !fb->format->bpp) {
879 weston_log("format 0x%lx is not compatible with dumb buffers\n",
880 (unsigned long) format);
884 memset(&create_arg, 0, sizeof create_arg);
885 create_arg.bpp = fb->format->bpp;
886 create_arg.width = width;
887 create_arg.height = height;
889 ret = drmIoctl(b->drm.fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_arg);
893 fb->type = BUFFER_PIXMAN_DUMB;
894 fb->handle = create_arg.handle;
895 fb->stride = create_arg.pitch;
896 fb->size = create_arg.size;
901 handles[0] = fb->handle;
902 pitches[0] = fb->stride;
905 ret = drmModeAddFB2(b->drm.fd, width, height, fb->format->format,
906 handles, pitches, offsets, &fb->fb_id, 0);
908 ret = drmModeAddFB(b->drm.fd, width, height,
909 fb->format->depth, fb->format->bpp,
910 fb->stride, fb->handle, &fb->fb_id);
916 memset(&map_arg, 0, sizeof map_arg);
917 map_arg.handle = fb->handle;
918 ret = drmIoctl(fb->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_arg);
922 fb->map = mmap(NULL, fb->size, PROT_WRITE,
923 MAP_SHARED, b->drm.fd, map_arg.offset);
924 if (fb->map == MAP_FAILED)
930 drmModeRmFB(b->drm.fd, fb->fb_id);
932 memset(&destroy_arg, 0, sizeof(destroy_arg));
933 destroy_arg.handle = create_arg.handle;
934 drmIoctl(b->drm.fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
940 static struct drm_fb *
941 drm_fb_ref(struct drm_fb *fb)
947 static struct drm_fb *
948 drm_fb_get_from_bo(struct gbm_bo *bo, struct drm_backend *backend,
949 uint32_t format, enum drm_fb_type type)
951 struct drm_fb *fb = gbm_bo_get_user_data(bo);
952 uint32_t handles[4] = { 0 }, pitches[4] = { 0 }, offsets[4] = { 0 };
956 assert(fb->type == type);
957 return drm_fb_ref(fb);
962 fb = zalloc(sizeof *fb);
970 fb->width = gbm_bo_get_width(bo);
971 fb->height = gbm_bo_get_height(bo);
972 fb->stride = gbm_bo_get_stride(bo);
973 fb->handle = gbm_bo_get_handle(bo).u32;
974 fb->format = pixel_format_get_info(format);
975 fb->size = fb->stride * fb->height;
976 fb->fd = backend->drm.fd;
979 weston_log("couldn't look up format 0x%lx\n",
980 (unsigned long) format);
984 if (backend->min_width > fb->width ||
985 fb->width > backend->max_width ||
986 backend->min_height > fb->height ||
987 fb->height > backend->max_height) {
988 weston_log("bo geometry out of bounds\n");
992 handles[0] = fb->handle;
993 pitches[0] = fb->stride;
996 ret = drmModeAddFB2(backend->drm.fd, fb->width, fb->height,
997 fb->format->format, handles, pitches, offsets,
999 if (ret && fb->format->depth && fb->format->bpp)
1000 ret = drmModeAddFB(backend->drm.fd, fb->width, fb->height,
1001 fb->format->depth, fb->format->bpp,
1002 fb->stride, fb->handle, &fb->fb_id);
1005 weston_log("failed to create kms fb: %m\n");
1009 gbm_bo_set_user_data(bo, fb, drm_fb_destroy_gbm);
1019 drm_fb_set_buffer(struct drm_fb *fb, struct weston_buffer *buffer)
1021 assert(fb->buffer_ref.buffer == NULL);
1022 assert(fb->type == BUFFER_CLIENT);
1023 weston_buffer_reference(&fb->buffer_ref, buffer);
1027 drm_fb_unref(struct drm_fb *fb)
1032 assert(fb->refcnt > 0);
1033 if (--fb->refcnt > 0)
1037 case BUFFER_PIXMAN_DUMB:
1038 drm_fb_destroy_dumb(fb);
1042 gbm_bo_destroy(fb->bo);
1044 case BUFFER_GBM_SURFACE:
1045 gbm_surface_release_buffer(fb->gbm_surface, fb->bo);
1054 * Allocate a new, empty, plane state.
1056 static struct drm_plane_state *
1057 drm_plane_state_alloc(struct drm_output_state *state_output,
1058 struct drm_plane *plane)
1060 struct drm_plane_state *state = zalloc(sizeof(*state));
1063 state->output_state = state_output;
1064 state->plane = plane;
1066 /* Here we only add the plane state to the desired link, and not
1067 * set the member. Having an output pointer set means that the
1068 * plane will be displayed on the output; this won't be the case
1069 * when we go to disable a plane. In this case, it must be part of
1070 * the commit (and thus the output state), but the member must be
1071 * NULL, as it will not be on any output when the state takes
1075 wl_list_insert(&state_output->plane_list, &state->link);
1077 wl_list_init(&state->link);
1083 * Free an existing plane state. As a special case, the state will not
1084 * normally be freed if it is the current state; see drm_plane_set_state.
1087 drm_plane_state_free(struct drm_plane_state *state, bool force)
1092 wl_list_remove(&state->link);
1093 wl_list_init(&state->link);
1094 state->output_state = NULL;
1096 if (force || state != state->plane->state_cur) {
1097 drm_fb_unref(state->fb);
1103 * Duplicate an existing plane state into a new plane state, storing it within
1104 * the given output state. If the output state already contains a plane state
1105 * for the drm_plane referenced by 'src', that plane state is freed first.
1107 static struct drm_plane_state *
1108 drm_plane_state_duplicate(struct drm_output_state *state_output,
1109 struct drm_plane_state *src)
1111 struct drm_plane_state *dst = malloc(sizeof(*dst));
1112 struct drm_plane_state *old, *tmp;
1117 wl_list_init(&dst->link);
1119 wl_list_for_each_safe(old, tmp, &state_output->plane_list, link) {
1120 /* Duplicating a plane state into the same output state, so
1121 * it can replace itself with an identical copy of itself,
1122 * makes no sense. */
1124 if (old->plane == dst->plane)
1125 drm_plane_state_free(old, false);
1128 wl_list_insert(&state_output->plane_list, &dst->link);
1130 dst->fb = drm_fb_ref(src->fb);
1131 dst->output_state = state_output;
1132 dst->complete = false;
1138 * Remove a plane state from an output state; if the plane was previously
1139 * enabled, then replace it with a disabling state. This ensures that the
1140 * output state was untouched from it was before the plane state was
1141 * modified by the caller of this function.
1143 * This is required as drm_output_state_get_plane may either allocate a
1144 * new plane state, in which case this function will just perform a matching
1145 * drm_plane_state_free, or it may instead repurpose an existing disabling
1146 * state (if the plane was previously active), in which case this function
1150 drm_plane_state_put_back(struct drm_plane_state *state)
1152 struct drm_output_state *state_output;
1153 struct drm_plane *plane;
1158 state_output = state->output_state;
1159 plane = state->plane;
1160 drm_plane_state_free(state, false);
1162 /* Plane was previously disabled; no need to keep this temporary
1164 if (!plane->state_cur->fb)
1167 (void) drm_plane_state_alloc(state_output, plane);
1171 * Return a plane state from a drm_output_state.
1173 static struct drm_plane_state *
1174 drm_output_state_get_existing_plane(struct drm_output_state *state_output,
1175 struct drm_plane *plane)
1177 struct drm_plane_state *ps;
1179 wl_list_for_each(ps, &state_output->plane_list, link) {
1180 if (ps->plane == plane)
1188 * Return a plane state from a drm_output_state, either existing or
1189 * freshly allocated.
1191 static struct drm_plane_state *
1192 drm_output_state_get_plane(struct drm_output_state *state_output,
1193 struct drm_plane *plane)
1195 struct drm_plane_state *ps;
1197 ps = drm_output_state_get_existing_plane(state_output, plane);
1201 return drm_plane_state_alloc(state_output, plane);
1205 * Allocate a new, empty drm_output_state. This should not generally be used
1206 * in the repaint cycle; see drm_output_state_duplicate.
1208 static struct drm_output_state *
1209 drm_output_state_alloc(struct drm_output *output,
1210 struct drm_pending_state *pending_state)
1212 struct drm_output_state *state = zalloc(sizeof(*state));
1215 state->output = output;
1216 state->dpms = WESTON_DPMS_OFF;
1217 state->pending_state = pending_state;
1219 wl_list_insert(&pending_state->output_list, &state->link);
1221 wl_list_init(&state->link);
1223 wl_list_init(&state->plane_list);
1229 * Duplicate an existing drm_output_state into a new one. This is generally
1230 * used during the repaint cycle, to capture the existing state of an output
1231 * and modify it to create a new state to be used.
1233 * The mode determines whether the output will be reset to an a blank state,
1234 * or an exact mirror of the current state.
1236 static struct drm_output_state *
1237 drm_output_state_duplicate(struct drm_output_state *src,
1238 struct drm_pending_state *pending_state,
1239 enum drm_output_state_duplicate_mode plane_mode)
1241 struct drm_output_state *dst = malloc(sizeof(*dst));
1242 struct drm_plane_state *ps;
1246 /* Copy the whole structure, then individually modify the
1247 * pending_state, as well as the list link into our pending
1251 dst->pending_state = pending_state;
1253 wl_list_insert(&pending_state->output_list, &dst->link);
1255 wl_list_init(&dst->link);
1257 wl_list_init(&dst->plane_list);
1259 wl_list_for_each(ps, &src->plane_list, link) {
1260 /* Don't carry planes which are now disabled; these should be
1261 * free for other outputs to reuse. */
1265 if (plane_mode == DRM_OUTPUT_STATE_CLEAR_PLANES)
1266 (void) drm_plane_state_alloc(dst, ps->plane);
1268 (void) drm_plane_state_duplicate(dst, ps);
1275 * Free an unused drm_output_state.
1278 drm_output_state_free(struct drm_output_state *state)
1280 struct drm_plane_state *ps, *next;
1285 wl_list_for_each_safe(ps, next, &state->plane_list, link)
1286 drm_plane_state_free(ps, false);
1288 wl_list_remove(&state->link);
1294 * Get output state to disable output
1296 * Returns a pointer to an output_state object which can be used to disable
1297 * an output (e.g. DPMS off).
1299 * @param pending_state The pending state object owning this update
1300 * @param output The output to disable
1301 * @returns A drm_output_state to disable the output
1303 static struct drm_output_state *
1304 drm_output_get_disable_state(struct drm_pending_state *pending_state,
1305 struct drm_output *output)
1307 struct drm_output_state *output_state;
1309 output_state = drm_output_state_duplicate(output->state_cur,
1311 DRM_OUTPUT_STATE_CLEAR_PLANES);
1312 output_state->dpms = WESTON_DPMS_OFF;
1314 return output_state;
1318 * Allocate a new drm_pending_state
1320 * Allocate a new, empty, 'pending state' structure to be used across a
1321 * repaint cycle or similar.
1323 * @param backend DRM backend
1324 * @returns Newly-allocated pending state structure
1326 static struct drm_pending_state *
1327 drm_pending_state_alloc(struct drm_backend *backend)
1329 struct drm_pending_state *ret;
1331 ret = calloc(1, sizeof(*ret));
1335 ret->backend = backend;
1336 wl_list_init(&ret->output_list);
1342 * Free a drm_pending_state structure
1344 * Frees a pending_state structure, as well as any output_states connected
1345 * to this pending state.
1347 * @param pending_state Pending state structure to free
1350 drm_pending_state_free(struct drm_pending_state *pending_state)
1352 struct drm_output_state *output_state, *tmp;
1357 wl_list_for_each_safe(output_state, tmp, &pending_state->output_list,
1359 drm_output_state_free(output_state);
1362 free(pending_state);
1366 * Find an output state in a pending state
1368 * Given a pending_state structure, find the output_state for a particular
1371 * @param pending_state Pending state structure to search
1372 * @param output Output to find state for
1373 * @returns Output state if present, or NULL if not
1375 static struct drm_output_state *
1376 drm_pending_state_get_output(struct drm_pending_state *pending_state,
1377 struct drm_output *output)
1379 struct drm_output_state *output_state;
1381 wl_list_for_each(output_state, &pending_state->output_list, link) {
1382 if (output_state->output == output)
1383 return output_state;
1389 static int drm_pending_state_apply_sync(struct drm_pending_state *state);
1392 * Mark a drm_output_state (the output's last state) as complete. This handles
1393 * any post-completion actions such as updating the repaint timer, disabling the
1394 * output, and finally freeing the state.
1397 drm_output_update_complete(struct drm_output *output, uint32_t flags,
1398 unsigned int sec, unsigned int usec)
1400 struct drm_backend *b = to_drm_backend(output->base.compositor);
1401 struct drm_plane_state *ps;
1404 /* Stop the pageflip timer instead of rearming it here */
1405 if (output->pageflip_timer)
1406 wl_event_source_timer_update(output->pageflip_timer, 0);
1408 wl_list_for_each(ps, &output->state_cur->plane_list, link)
1409 ps->complete = true;
1411 drm_output_state_free(output->state_last);
1412 output->state_last = NULL;
1414 if (output->destroy_pending) {
1415 output->destroy_pending = 0;
1416 output->disable_pending = 0;
1417 output->dpms_off_pending = 0;
1418 drm_output_destroy(&output->base);
1420 } else if (output->disable_pending) {
1421 output->disable_pending = 0;
1422 output->dpms_off_pending = 0;
1423 weston_output_disable(&output->base);
1425 } else if (output->dpms_off_pending) {
1426 struct drm_pending_state *pending = drm_pending_state_alloc(b);
1427 output->dpms_off_pending = 0;
1428 drm_output_get_disable_state(pending, output);
1429 drm_pending_state_apply_sync(pending);
1431 } else if (output->state_cur->dpms == WESTON_DPMS_OFF &&
1432 output->base.repaint_status != REPAINT_AWAITING_COMPLETION) {
1433 /* DPMS can happen to us either in the middle of a repaint
1434 * cycle (when we have painted fresh content, only to throw it
1435 * away for DPMS off), or at any other random point. If the
1436 * latter is true, then we cannot go through finish_frame,
1437 * because the repaint machinery does not expect this. */
1442 ts.tv_nsec = usec * 1000;
1443 weston_output_finish_frame(&output->base, &ts, flags);
1445 /* We can't call this from frame_notify, because the output's
1446 * repaint needed flag is cleared just after that */
1447 if (output->recorder)
1448 weston_output_schedule_repaint(&output->base);
1452 * Mark an output state as current on the output, i.e. it has been
1453 * submitted to the kernel. The mode argument determines whether this
1454 * update will be applied synchronously (e.g. when calling drmModeSetCrtc),
1455 * or asynchronously (in which case we wait for events to complete).
1458 drm_output_assign_state(struct drm_output_state *state,
1459 enum drm_state_apply_mode mode)
1461 struct drm_output *output = state->output;
1462 struct drm_backend *b = to_drm_backend(output->base.compositor);
1463 struct drm_plane_state *plane_state;
1465 assert(!output->state_last);
1467 if (mode == DRM_STATE_APPLY_ASYNC)
1468 output->state_last = output->state_cur;
1470 drm_output_state_free(output->state_cur);
1472 wl_list_remove(&state->link);
1473 wl_list_init(&state->link);
1474 state->pending_state = NULL;
1476 output->state_cur = state;
1478 if (b->atomic_modeset && mode == DRM_STATE_APPLY_ASYNC)
1479 output->atomic_complete_pending = 1;
1481 /* Replace state_cur on each affected plane with the new state, being
1482 * careful to dispose of orphaned (but only orphaned) previous state.
1483 * If the previous state is not orphaned (still has an output_state
1484 * attached), it will be disposed of by freeing the output_state. */
1485 wl_list_for_each(plane_state, &state->plane_list, link) {
1486 struct drm_plane *plane = plane_state->plane;
1488 if (plane->state_cur && !plane->state_cur->output_state)
1489 drm_plane_state_free(plane->state_cur, true);
1490 plane->state_cur = plane_state;
1492 if (mode != DRM_STATE_APPLY_ASYNC) {
1493 plane_state->complete = true;
1497 if (b->atomic_modeset)
1500 if (plane->type == WDRM_PLANE_TYPE_OVERLAY)
1501 output->vblank_pending++;
1502 else if (plane->type == WDRM_PLANE_TYPE_PRIMARY)
1503 output->page_flip_pending = 1;
1508 drm_view_transform_supported(struct weston_view *ev)
1510 return !ev->transform.enabled ||
1511 (ev->transform.matrix.type < WESTON_MATRIX_TRANSFORM_ROTATE);
1515 drm_output_check_scanout_format(struct drm_output *output,
1516 struct weston_surface *es, struct gbm_bo *bo)
1519 pixman_region32_t r;
1521 format = gbm_bo_get_format(bo);
1523 if (format == GBM_FORMAT_ARGB8888) {
1524 /* We can scanout an ARGB buffer if the surface's
1525 * opaque region covers the whole output, but we have
1526 * to use XRGB as the KMS format code. */
1527 pixman_region32_init_rect(&r, 0, 0,
1529 output->base.height);
1530 pixman_region32_subtract(&r, &r, &es->opaque);
1532 if (!pixman_region32_not_empty(&r))
1533 format = GBM_FORMAT_XRGB8888;
1535 pixman_region32_fini(&r);
1538 if (output->gbm_format == format)
1544 static struct weston_plane *
1545 drm_output_prepare_scanout_view(struct drm_output_state *output_state,
1546 struct weston_view *ev)
1548 struct drm_output *output = output_state->output;
1549 struct drm_backend *b = to_drm_backend(output->base.compositor);
1550 struct drm_plane *scanout_plane = output->scanout_plane;
1551 struct drm_plane_state *state;
1552 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
1553 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
1557 /* Don't import buffers which span multiple outputs. */
1558 if (ev->output_mask != (1u << output->base.id))
1561 /* We use GBM to import buffers. */
1567 if (wl_shm_buffer_get(buffer->resource))
1570 /* Make sure our view is exactly compatible with the output. */
1571 if (ev->geometry.x != output->base.x ||
1572 ev->geometry.y != output->base.y)
1574 if (buffer->width != output->base.current_mode->width ||
1575 buffer->height != output->base.current_mode->height)
1578 if (ev->transform.enabled)
1580 if (ev->geometry.scissor_enabled)
1582 if (viewport->buffer.transform != output->base.transform)
1584 if (viewport->buffer.scale != output->base.current_scale)
1586 if (!drm_view_transform_supported(ev))
1589 if (ev->alpha != 1.0f)
1592 state = drm_output_state_get_plane(output_state, scanout_plane);
1594 /* If there is already a framebuffer on the scanout plane,
1595 * a client view has already been placed on the scanout
1596 * view. In that case, do not free or put back the state,
1597 * but just leave it in place and quietly exit. */
1601 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER,
1602 buffer->resource, GBM_BO_USE_SCANOUT);
1604 /* Unable to use the buffer for scanout */
1608 format = drm_output_check_scanout_format(output, ev->surface, bo);
1610 drm_plane_state_put_back(state);
1615 state->fb = drm_fb_get_from_bo(bo, b, format, BUFFER_CLIENT);
1617 drm_plane_state_put_back(state);
1622 drm_fb_set_buffer(state->fb, buffer);
1624 state->output = output;
1628 state->src_w = state->fb->width << 16;
1629 state->src_h = state->fb->height << 16;
1633 state->dest_w = output->base.current_mode->width;
1634 state->dest_h = output->base.current_mode->height;
1636 return &scanout_plane->base;
1639 static struct drm_fb *
1640 drm_output_render_gl(struct drm_output_state *state, pixman_region32_t *damage)
1642 struct drm_output *output = state->output;
1643 struct drm_backend *b = to_drm_backend(output->base.compositor);
1647 output->base.compositor->renderer->repaint_output(&output->base,
1650 bo = gbm_surface_lock_front_buffer(output->gbm_surface);
1652 weston_log("failed to lock front buffer: %m\n");
1656 ret = drm_fb_get_from_bo(bo, b, output->gbm_format, BUFFER_GBM_SURFACE);
1658 weston_log("failed to get drm_fb for bo\n");
1659 gbm_surface_release_buffer(output->gbm_surface, bo);
1662 ret->gbm_surface = output->gbm_surface;
1667 static struct drm_fb *
1668 drm_output_render_pixman(struct drm_output_state *state,
1669 pixman_region32_t *damage)
1671 struct drm_output *output = state->output;
1672 struct weston_compositor *ec = output->base.compositor;
1673 pixman_region32_t total_damage, previous_damage;
1675 pixman_region32_init(&total_damage);
1676 pixman_region32_init(&previous_damage);
1678 pixman_region32_copy(&previous_damage, damage);
1680 pixman_region32_union(&total_damage, damage, &output->previous_damage);
1681 pixman_region32_copy(&output->previous_damage, &previous_damage);
1683 output->current_image ^= 1;
1685 pixman_renderer_output_set_buffer(&output->base,
1686 output->image[output->current_image]);
1688 ec->renderer->repaint_output(&output->base, &total_damage);
1690 pixman_region32_fini(&total_damage);
1691 pixman_region32_fini(&previous_damage);
1693 return drm_fb_ref(output->dumb[output->current_image]);
1697 drm_output_render(struct drm_output_state *state, pixman_region32_t *damage)
1699 struct drm_output *output = state->output;
1700 struct weston_compositor *c = output->base.compositor;
1701 struct drm_plane_state *scanout_state;
1702 struct drm_plane *scanout_plane = output->scanout_plane;
1703 struct drm_backend *b = to_drm_backend(c);
1706 /* If we already have a client buffer promoted to scanout, then we don't
1707 * want to render. */
1708 scanout_state = drm_output_state_get_plane(state,
1709 output->scanout_plane);
1710 if (scanout_state->fb)
1713 if (!pixman_region32_not_empty(damage) &&
1714 scanout_plane->state_cur->fb &&
1715 (scanout_plane->state_cur->fb->type == BUFFER_GBM_SURFACE ||
1716 scanout_plane->state_cur->fb->type == BUFFER_PIXMAN_DUMB) &&
1717 scanout_plane->state_cur->fb->width ==
1718 output->base.current_mode->width &&
1719 scanout_plane->state_cur->fb->height ==
1720 output->base.current_mode->height) {
1721 fb = drm_fb_ref(scanout_plane->state_cur->fb);
1722 } else if (b->use_pixman) {
1723 fb = drm_output_render_pixman(state, damage);
1725 fb = drm_output_render_gl(state, damage);
1729 drm_plane_state_put_back(scanout_state);
1733 scanout_state->fb = fb;
1734 scanout_state->output = output;
1736 scanout_state->src_x = 0;
1737 scanout_state->src_y = 0;
1738 scanout_state->src_w = output->base.current_mode->width << 16;
1739 scanout_state->src_h = output->base.current_mode->height << 16;
1741 scanout_state->dest_x = 0;
1742 scanout_state->dest_y = 0;
1743 scanout_state->dest_w = scanout_state->src_w >> 16;
1744 scanout_state->dest_h = scanout_state->src_h >> 16;
1747 pixman_region32_subtract(&c->primary_plane.damage,
1748 &c->primary_plane.damage, damage);
1752 drm_output_set_gamma(struct weston_output *output_base,
1753 uint16_t size, uint16_t *r, uint16_t *g, uint16_t *b)
1756 struct drm_output *output = to_drm_output(output_base);
1757 struct drm_backend *backend =
1758 to_drm_backend(output->base.compositor);
1761 if (output_base->gamma_size != size)
1764 rc = drmModeCrtcSetGamma(backend->drm.fd,
1768 weston_log("set gamma failed: %m\n");
1771 /* Determine the type of vblank synchronization to use for the output.
1773 * The pipe parameter indicates which CRTC is in use. Knowing this, we
1774 * can determine which vblank sequence type to use for it. Traditional
1775 * cards had only two CRTCs, with CRTC 0 using no special flags, and
1776 * CRTC 1 using DRM_VBLANK_SECONDARY. The first bit of the pipe
1777 * parameter indicates this.
1779 * Bits 1-5 of the pipe parameter are 5 bit wide pipe number between
1780 * 0-31. If this is non-zero it indicates we're dealing with a
1781 * multi-gpu situation and we need to calculate the vblank sync
1782 * using DRM_BLANK_HIGH_CRTC_MASK.
1785 drm_waitvblank_pipe(struct drm_output *output)
1787 if (output->pipe > 1)
1788 return (output->pipe << DRM_VBLANK_HIGH_CRTC_SHIFT) &
1789 DRM_VBLANK_HIGH_CRTC_MASK;
1790 else if (output->pipe > 0)
1791 return DRM_VBLANK_SECONDARY;
1797 drm_output_apply_state_legacy(struct drm_output_state *state)
1799 struct drm_output *output = state->output;
1800 struct drm_head *head = to_drm_head(weston_output_get_first_head(&output->base));
1801 struct drm_backend *backend = to_drm_backend(output->base.compositor);
1802 struct drm_plane *scanout_plane = output->scanout_plane;
1803 struct drm_property_info *dpms_prop =
1804 &head->props_conn[WDRM_CONNECTOR_DPMS];
1805 struct drm_plane_state *scanout_state;
1806 struct drm_plane_state *ps;
1807 struct drm_plane *p;
1808 struct drm_mode *mode;
1809 struct timespec now;
1812 /* If disable_planes is set then assign_planes() wasn't
1813 * called for this render, so we could still have a stale
1814 * cursor plane set up.
1816 if (output->base.disable_planes) {
1817 output->cursor_view = NULL;
1818 if (output->cursor_plane) {
1819 output->cursor_plane->base.x = INT32_MIN;
1820 output->cursor_plane->base.y = INT32_MIN;
1824 if (state->dpms != WESTON_DPMS_ON) {
1825 wl_list_for_each(ps, &state->plane_list, link) {
1827 assert(ps->fb == NULL);
1828 assert(ps->output == NULL);
1830 if (p->type != WDRM_PLANE_TYPE_OVERLAY)
1833 ret = drmModeSetPlane(backend->drm.fd, p->plane_id,
1834 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
1836 weston_log("drmModeSetPlane failed disable: %m\n");
1839 if (output->cursor_plane) {
1840 ret = drmModeSetCursor(backend->drm.fd, output->crtc_id,
1843 weston_log("drmModeSetCursor failed disable: %m\n");
1846 ret = drmModeSetCrtc(backend->drm.fd, output->crtc_id, 0, 0, 0,
1847 &head->connector_id, 0, NULL);
1849 weston_log("drmModeSetCrtc failed disabling: %m\n");
1851 drm_output_assign_state(state, DRM_STATE_APPLY_SYNC);
1852 weston_compositor_read_presentation_clock(output->base.compositor, &now);
1853 drm_output_update_complete(output,
1854 WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION,
1855 now.tv_sec, now.tv_nsec / 1000);
1861 drm_output_state_get_existing_plane(state, scanout_plane);
1863 /* The legacy SetCrtc API doesn't allow us to do scaling, and the
1864 * legacy PageFlip API doesn't allow us to do clipping either. */
1865 assert(scanout_state->src_x == 0);
1866 assert(scanout_state->src_y == 0);
1867 assert(scanout_state->src_w ==
1868 (unsigned) (output->base.current_mode->width << 16));
1869 assert(scanout_state->src_h ==
1870 (unsigned) (output->base.current_mode->height << 16));
1871 assert(scanout_state->dest_x == 0);
1872 assert(scanout_state->dest_y == 0);
1873 assert(scanout_state->dest_w == scanout_state->src_w >> 16);
1874 assert(scanout_state->dest_h == scanout_state->src_h >> 16);
1876 mode = to_drm_mode(output->base.current_mode);
1877 if (backend->state_invalid || !scanout_plane->state_cur->fb ||
1878 scanout_plane->state_cur->fb->stride != scanout_state->fb->stride) {
1879 ret = drmModeSetCrtc(backend->drm.fd, output->crtc_id,
1880 scanout_state->fb->fb_id,
1882 &head->connector_id, 1,
1885 weston_log("set mode failed: %m\n");
1890 if (drmModePageFlip(backend->drm.fd, output->crtc_id,
1891 scanout_state->fb->fb_id,
1892 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
1893 weston_log("queueing pageflip failed: %m\n");
1897 assert(!output->page_flip_pending);
1899 if (output->pageflip_timer)
1900 wl_event_source_timer_update(output->pageflip_timer,
1901 backend->pageflip_timeout);
1903 drm_output_set_cursor(state);
1906 * Now, update all the sprite surfaces
1908 wl_list_for_each(ps, &state->plane_list, link) {
1909 uint32_t flags = 0, fb_id = 0;
1911 .request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT,
1912 .request.sequence = 1,
1916 if (p->type != WDRM_PLANE_TYPE_OVERLAY)
1919 assert(p->state_cur->complete);
1920 assert(!!p->state_cur->output == !!p->state_cur->fb);
1921 assert(!p->state_cur->output || p->state_cur->output == output);
1922 assert(!ps->complete);
1923 assert(!ps->output || ps->output == output);
1924 assert(!!ps->output == !!ps->fb);
1926 if (ps->fb && !backend->sprites_hidden)
1927 fb_id = ps->fb->fb_id;
1929 ret = drmModeSetPlane(backend->drm.fd, p->plane_id,
1930 output->crtc_id, fb_id, flags,
1931 ps->dest_x, ps->dest_y,
1932 ps->dest_w, ps->dest_h,
1933 ps->src_x, ps->src_y,
1934 ps->src_w, ps->src_h);
1936 weston_log("setplane failed: %d: %s\n",
1937 ret, strerror(errno));
1939 vbl.request.type |= drm_waitvblank_pipe(output);
1942 * Queue a vblank signal so we know when the surface
1943 * becomes active on the display or has been replaced.
1945 vbl.request.signal = (unsigned long) ps;
1946 ret = drmWaitVBlank(backend->drm.fd, &vbl);
1948 weston_log("vblank event request failed: %d: %s\n",
1949 ret, strerror(errno));
1953 if (dpms_prop->prop_id && state->dpms != output->state_cur->dpms) {
1954 ret = drmModeConnectorSetProperty(backend->drm.fd,
1959 weston_log("DRM: DPMS: failed property set for %s\n",
1964 drm_output_assign_state(state, DRM_STATE_APPLY_ASYNC);
1969 output->cursor_view = NULL;
1970 drm_output_state_free(state);
1974 #ifdef HAVE_DRM_ATOMIC
1976 crtc_add_prop(drmModeAtomicReq *req, struct drm_output *output,
1977 enum wdrm_crtc_property prop, uint64_t val)
1979 struct drm_property_info *info = &output->props_crtc[prop];
1982 if (info->prop_id == 0)
1985 ret = drmModeAtomicAddProperty(req, output->crtc_id, info->prop_id,
1987 return (ret <= 0) ? -1 : 0;
1991 connector_add_prop(drmModeAtomicReq *req, struct drm_head *head,
1992 enum wdrm_connector_property prop, uint64_t val)
1994 struct drm_property_info *info = &head->props_conn[prop];
1997 if (info->prop_id == 0)
2000 ret = drmModeAtomicAddProperty(req, head->connector_id,
2001 info->prop_id, val);
2002 return (ret <= 0) ? -1 : 0;
2006 plane_add_prop(drmModeAtomicReq *req, struct drm_plane *plane,
2007 enum wdrm_plane_property prop, uint64_t val)
2009 struct drm_property_info *info = &plane->props[prop];
2012 if (info->prop_id == 0)
2015 ret = drmModeAtomicAddProperty(req, plane->plane_id, info->prop_id,
2017 return (ret <= 0) ? -1 : 0;
2021 drm_mode_ensure_blob(struct drm_backend *backend, struct drm_mode *mode)
2028 ret = drmModeCreatePropertyBlob(backend->drm.fd,
2030 sizeof(mode->mode_info),
2033 weston_log("failed to create mode property blob: %m\n");
2039 drm_output_apply_state_atomic(struct drm_output_state *state,
2040 drmModeAtomicReq *req,
2043 struct drm_output *output = state->output;
2044 struct drm_head *head = to_drm_head(weston_output_get_first_head(&output->base));
2045 struct drm_backend *backend = to_drm_backend(output->base.compositor);
2046 struct drm_plane_state *plane_state;
2047 struct drm_mode *current_mode = to_drm_mode(output->base.current_mode);
2050 if (state->dpms != output->state_cur->dpms)
2051 *flags |= DRM_MODE_ATOMIC_ALLOW_MODESET;
2053 if (state->dpms == WESTON_DPMS_ON) {
2054 ret = drm_mode_ensure_blob(backend, current_mode);
2058 ret |= crtc_add_prop(req, output, WDRM_CRTC_MODE_ID,
2059 current_mode->blob_id);
2060 ret |= crtc_add_prop(req, output, WDRM_CRTC_ACTIVE, 1);
2061 ret |= connector_add_prop(req, head, WDRM_CONNECTOR_CRTC_ID,
2064 ret |= crtc_add_prop(req, output, WDRM_CRTC_MODE_ID, 0);
2065 ret |= crtc_add_prop(req, output, WDRM_CRTC_ACTIVE, 0);
2066 ret |= connector_add_prop(req, head, WDRM_CONNECTOR_CRTC_ID, 0);
2070 weston_log("couldn't set atomic CRTC/connector state\n");
2074 wl_list_for_each(plane_state, &state->plane_list, link) {
2075 struct drm_plane *plane = plane_state->plane;
2077 ret |= plane_add_prop(req, plane, WDRM_PLANE_FB_ID,
2078 plane_state->fb ? plane_state->fb->fb_id : 0);
2079 ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_ID,
2080 plane_state->fb ? output->crtc_id : 0);
2081 ret |= plane_add_prop(req, plane, WDRM_PLANE_SRC_X,
2082 plane_state->src_x);
2083 ret |= plane_add_prop(req, plane, WDRM_PLANE_SRC_Y,
2084 plane_state->src_y);
2085 ret |= plane_add_prop(req, plane, WDRM_PLANE_SRC_W,
2086 plane_state->src_w);
2087 ret |= plane_add_prop(req, plane, WDRM_PLANE_SRC_H,
2088 plane_state->src_h);
2089 ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_X,
2090 plane_state->dest_x);
2091 ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_Y,
2092 plane_state->dest_y);
2093 ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_W,
2094 plane_state->dest_w);
2095 ret |= plane_add_prop(req, plane, WDRM_PLANE_CRTC_H,
2096 plane_state->dest_h);
2099 weston_log("couldn't set plane state\n");
2108 * Helper function used only by drm_pending_state_apply, with the same
2109 * guarantees and constraints as that function.
2112 drm_pending_state_apply_atomic(struct drm_pending_state *pending_state,
2113 enum drm_state_apply_mode mode)
2115 struct drm_backend *b = pending_state->backend;
2116 struct drm_output_state *output_state, *tmp;
2117 struct drm_plane *plane;
2118 drmModeAtomicReq *req = drmModeAtomicAlloc();
2125 if (b->state_invalid) {
2126 struct weston_head *head_base;
2127 struct drm_head *head;
2131 /* If we need to reset all our state (e.g. because we've
2132 * just started, or just been VT-switched in), explicitly
2133 * disable all the CRTCs and connectors we aren't using. */
2134 wl_list_for_each(head_base,
2135 &b->compositor->head_list, compositor_link) {
2136 struct drm_property_info *info;
2138 if (weston_head_is_enabled(head_base))
2141 head = to_drm_head(head_base);
2143 info = &head->props_conn[WDRM_CONNECTOR_CRTC_ID];
2144 err = drmModeAtomicAddProperty(req, head->connector_id,
2149 info = &head->props_conn[WDRM_CONNECTOR_DPMS];
2150 if (info->prop_id > 0)
2151 err = drmModeAtomicAddProperty(req, head->connector_id,
2158 wl_array_for_each(unused, &b->unused_crtcs) {
2159 struct drm_property_info infos[WDRM_CRTC__COUNT];
2160 struct drm_property_info *info;
2161 drmModeObjectProperties *props;
2164 memset(infos, 0, sizeof(infos));
2166 /* We can't emit a disable on a CRTC that's already
2167 * off, as the kernel will refuse to generate an event
2168 * for an off->off state and fail the commit.
2170 props = drmModeObjectGetProperties(b->drm.fd,
2172 DRM_MODE_OBJECT_CRTC);
2178 drm_property_info_populate(b, crtc_props, infos,
2182 info = &infos[WDRM_CRTC_ACTIVE];
2183 active = drm_property_get_value(info, props, 0);
2184 drmModeFreeObjectProperties(props);
2186 drm_property_info_free(infos, WDRM_CRTC__COUNT);
2190 err = drmModeAtomicAddProperty(req, *unused,
2195 info = &infos[WDRM_CRTC_MODE_ID];
2196 err = drmModeAtomicAddProperty(req, *unused,
2201 drm_property_info_free(infos, WDRM_CRTC__COUNT);
2204 /* Disable all the planes; planes which are being used will
2205 * override this state in the output-state application. */
2206 wl_list_for_each(plane, &b->plane_list, link) {
2207 plane_add_prop(req, plane, WDRM_PLANE_CRTC_ID, 0);
2208 plane_add_prop(req, plane, WDRM_PLANE_FB_ID, 0);
2211 flags |= DRM_MODE_ATOMIC_ALLOW_MODESET;
2214 wl_list_for_each(output_state, &pending_state->output_list, link) {
2215 if (mode == DRM_STATE_APPLY_SYNC)
2216 assert(output_state->dpms == WESTON_DPMS_OFF);
2217 ret |= drm_output_apply_state_atomic(output_state, req, &flags);
2221 weston_log("atomic: couldn't compile atomic state\n");
2226 case DRM_STATE_APPLY_SYNC:
2228 case DRM_STATE_APPLY_ASYNC:
2229 flags |= DRM_MODE_PAGE_FLIP_EVENT | DRM_MODE_ATOMIC_NONBLOCK;
2233 ret = drmModeAtomicCommit(b->drm.fd, req, flags, b);
2235 weston_log("atomic: couldn't commit new state: %m\n");
2239 wl_list_for_each_safe(output_state, tmp, &pending_state->output_list,
2241 drm_output_assign_state(output_state, mode);
2243 b->state_invalid = false;
2245 assert(wl_list_empty(&pending_state->output_list));
2248 drmModeAtomicFree(req);
2249 drm_pending_state_free(pending_state);
2255 * Applies all of a pending_state asynchronously: the primary entry point for
2256 * applying KMS state to a device. Updates the state for all outputs in the
2257 * pending_state, as well as disabling any unclaimed outputs.
2259 * Unconditionally takes ownership of pending_state, and clears state_invalid.
2262 drm_pending_state_apply(struct drm_pending_state *pending_state)
2264 struct drm_backend *b = pending_state->backend;
2265 struct drm_output_state *output_state, *tmp;
2268 #ifdef HAVE_DRM_ATOMIC
2269 if (b->atomic_modeset)
2270 return drm_pending_state_apply_atomic(pending_state,
2271 DRM_STATE_APPLY_ASYNC);
2274 if (b->state_invalid) {
2275 /* If we need to reset all our state (e.g. because we've
2276 * just started, or just been VT-switched in), explicitly
2277 * disable all the CRTCs we aren't using. This also disables
2278 * all connectors on these CRTCs, so we don't need to do that
2279 * separately with the pre-atomic API. */
2280 wl_array_for_each(unused, &b->unused_crtcs)
2281 drmModeSetCrtc(b->drm.fd, *unused, 0, 0, 0, NULL, 0,
2285 wl_list_for_each_safe(output_state, tmp, &pending_state->output_list,
2287 struct drm_output *output = output_state->output;
2290 ret = drm_output_apply_state_legacy(output_state);
2292 weston_log("Couldn't apply state for output %s\n",
2297 b->state_invalid = false;
2299 assert(wl_list_empty(&pending_state->output_list));
2301 drm_pending_state_free(pending_state);
2307 * The synchronous version of drm_pending_state_apply. May only be used to
2308 * disable outputs. Does so synchronously: the request is guaranteed to have
2309 * completed on return, and the output will not be touched afterwards.
2311 * Unconditionally takes ownership of pending_state, and clears state_invalid.
2314 drm_pending_state_apply_sync(struct drm_pending_state *pending_state)
2316 struct drm_backend *b = pending_state->backend;
2317 struct drm_output_state *output_state, *tmp;
2320 #ifdef HAVE_DRM_ATOMIC
2321 if (b->atomic_modeset)
2322 return drm_pending_state_apply_atomic(pending_state,
2323 DRM_STATE_APPLY_SYNC);
2326 if (b->state_invalid) {
2327 /* If we need to reset all our state (e.g. because we've
2328 * just started, or just been VT-switched in), explicitly
2329 * disable all the CRTCs we aren't using. This also disables
2330 * all connectors on these CRTCs, so we don't need to do that
2331 * separately with the pre-atomic API. */
2332 wl_array_for_each(unused, &b->unused_crtcs)
2333 drmModeSetCrtc(b->drm.fd, *unused, 0, 0, 0, NULL, 0,
2337 wl_list_for_each_safe(output_state, tmp, &pending_state->output_list,
2341 assert(output_state->dpms == WESTON_DPMS_OFF);
2342 ret = drm_output_apply_state_legacy(output_state);
2344 weston_log("Couldn't apply state for output %s\n",
2345 output_state->output->base.name);
2349 b->state_invalid = false;
2351 assert(wl_list_empty(&pending_state->output_list));
2353 drm_pending_state_free(pending_state);
2359 drm_output_repaint(struct weston_output *output_base,
2360 pixman_region32_t *damage,
2363 struct drm_pending_state *pending_state = repaint_data;
2364 struct drm_output *output = to_drm_output(output_base);
2365 struct drm_output_state *state = NULL;
2366 struct drm_plane_state *scanout_state;
2368 if (output->disable_pending || output->destroy_pending)
2371 assert(!output->state_last);
2373 /* If planes have been disabled in the core, we might not have
2374 * hit assign_planes at all, so might not have valid output state
2376 state = drm_pending_state_get_output(pending_state, output);
2378 state = drm_output_state_duplicate(output->state_cur,
2380 DRM_OUTPUT_STATE_CLEAR_PLANES);
2381 state->dpms = WESTON_DPMS_ON;
2383 drm_output_render(state, damage);
2384 scanout_state = drm_output_state_get_plane(state,
2385 output->scanout_plane);
2386 if (!scanout_state || !scanout_state->fb)
2392 drm_output_state_free(state);
2397 drm_output_start_repaint_loop(struct weston_output *output_base)
2399 struct drm_output *output = to_drm_output(output_base);
2400 struct drm_pending_state *pending_state;
2401 struct drm_plane *scanout_plane = output->scanout_plane;
2402 struct drm_backend *backend =
2403 to_drm_backend(output_base->compositor);
2404 struct timespec ts, tnow;
2405 struct timespec vbl2now;
2406 int64_t refresh_nsec;
2409 .request.type = DRM_VBLANK_RELATIVE,
2410 .request.sequence = 0,
2411 .request.signal = 0,
2414 if (output->disable_pending || output->destroy_pending)
2417 if (!output->scanout_plane->state_cur->fb) {
2418 /* We can't page flip if there's no mode set */
2422 /* Need to smash all state in from scratch; current timings might not
2423 * be what we want, page flip might not work, etc.
2425 if (backend->state_invalid)
2428 assert(scanout_plane->state_cur->output == output);
2430 /* Try to get current msc and timestamp via instant query */
2431 vbl.request.type |= drm_waitvblank_pipe(output);
2432 ret = drmWaitVBlank(backend->drm.fd, &vbl);
2434 /* Error ret or zero timestamp means failure to get valid timestamp */
2435 if ((ret == 0) && (vbl.reply.tval_sec > 0 || vbl.reply.tval_usec > 0)) {
2436 ts.tv_sec = vbl.reply.tval_sec;
2437 ts.tv_nsec = vbl.reply.tval_usec * 1000;
2439 /* Valid timestamp for most recent vblank - not stale?
2440 * Stale ts could happen on Linux 3.17+, so make sure it
2441 * is not older than 1 refresh duration since now.
2443 weston_compositor_read_presentation_clock(backend->compositor,
2445 timespec_sub(&vbl2now, &tnow, &ts);
2447 millihz_to_nsec(output->base.current_mode->refresh);
2448 if (timespec_to_nsec(&vbl2now) < refresh_nsec) {
2449 drm_output_update_msc(output, vbl.reply.sequence);
2450 weston_output_finish_frame(output_base, &ts,
2451 WP_PRESENTATION_FEEDBACK_INVALID);
2456 /* Immediate query didn't provide valid timestamp.
2457 * Use pageflip fallback.
2460 assert(!output->page_flip_pending);
2461 assert(!output->state_last);
2463 pending_state = drm_pending_state_alloc(backend);
2464 drm_output_state_duplicate(output->state_cur, pending_state,
2465 DRM_OUTPUT_STATE_PRESERVE_PLANES);
2467 ret = drm_pending_state_apply(pending_state);
2469 weston_log("applying repaint-start state failed: %m\n");
2476 /* if we cannot page-flip, immediately finish frame */
2477 weston_output_finish_frame(output_base, NULL,
2478 WP_PRESENTATION_FEEDBACK_INVALID);
2482 drm_output_update_msc(struct drm_output *output, unsigned int seq)
2484 uint64_t msc_hi = output->base.msc >> 32;
2486 if (seq < (output->base.msc & 0xffffffff))
2489 output->base.msc = (msc_hi << 32) + seq;
2493 vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec,
2496 struct drm_plane_state *ps = (struct drm_plane_state *) data;
2497 struct drm_output_state *os = ps->output_state;
2498 struct drm_output *output = os->output;
2499 struct drm_backend *b = to_drm_backend(output->base.compositor);
2500 uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
2501 WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
2503 assert(!b->atomic_modeset);
2505 drm_output_update_msc(output, frame);
2506 output->vblank_pending--;
2507 assert(output->vblank_pending >= 0);
2511 if (output->page_flip_pending || output->vblank_pending)
2514 drm_output_update_complete(output, flags, sec, usec);
2518 page_flip_handler(int fd, unsigned int frame,
2519 unsigned int sec, unsigned int usec, void *data)
2521 struct drm_output *output = data;
2522 struct drm_backend *b = to_drm_backend(output->base.compositor);
2523 uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_VSYNC |
2524 WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
2525 WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
2527 drm_output_update_msc(output, frame);
2529 assert(!b->atomic_modeset);
2530 assert(output->page_flip_pending);
2531 output->page_flip_pending = 0;
2533 if (output->vblank_pending)
2536 drm_output_update_complete(output, flags, sec, usec);
2540 * Begin a new repaint cycle
2542 * Called by the core compositor at the beginning of a repaint cycle. Creates
2543 * a new pending_state structure to own any output state created by individual
2544 * output repaint functions until the repaint is flushed or cancelled.
2547 drm_repaint_begin(struct weston_compositor *compositor)
2549 struct drm_backend *b = to_drm_backend(compositor);
2550 struct drm_pending_state *ret;
2552 ret = drm_pending_state_alloc(b);
2553 b->repaint_data = ret;
2559 * Flush a repaint set
2561 * Called by the core compositor when a repaint cycle has been completed
2562 * and should be flushed. Frees the pending state, transitioning ownership
2563 * of the output state from the pending state, to the update itself. When
2564 * the update completes (see drm_output_update_complete), the output
2565 * state will be freed.
2568 drm_repaint_flush(struct weston_compositor *compositor, void *repaint_data)
2570 struct drm_backend *b = to_drm_backend(compositor);
2571 struct drm_pending_state *pending_state = repaint_data;
2573 drm_pending_state_apply(pending_state);
2574 b->repaint_data = NULL;
2578 * Cancel a repaint set
2580 * Called by the core compositor when a repaint has finished, so the data
2581 * held across the repaint cycle should be discarded.
2584 drm_repaint_cancel(struct weston_compositor *compositor, void *repaint_data)
2586 struct drm_backend *b = to_drm_backend(compositor);
2587 struct drm_pending_state *pending_state = repaint_data;
2589 drm_pending_state_free(pending_state);
2590 b->repaint_data = NULL;
2593 #ifdef HAVE_DRM_ATOMIC
2595 atomic_flip_handler(int fd, unsigned int frame, unsigned int sec,
2596 unsigned int usec, unsigned int crtc_id, void *data)
2598 struct drm_backend *b = data;
2599 struct drm_output *output = drm_output_find_by_crtc(b, crtc_id);
2600 uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_VSYNC |
2601 WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
2602 WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
2604 /* During the initial modeset, we can disable CRTCs which we don't
2605 * actually handle during normal operation; this will give us events
2606 * for unknown outputs. Ignore them. */
2607 if (!output || !output->base.enabled)
2610 drm_output_update_msc(output, frame);
2612 assert(b->atomic_modeset);
2613 assert(output->atomic_complete_pending);
2614 output->atomic_complete_pending = 0;
2616 drm_output_update_complete(output, flags, sec, usec);
2621 drm_output_check_plane_format(struct drm_plane *p,
2622 struct weston_view *ev, struct gbm_bo *bo)
2626 format = gbm_bo_get_format(bo);
2628 if (format == GBM_FORMAT_ARGB8888) {
2629 pixman_region32_t r;
2631 pixman_region32_init_rect(&r, 0, 0,
2633 ev->surface->height);
2634 pixman_region32_subtract(&r, &r, &ev->surface->opaque);
2636 if (!pixman_region32_not_empty(&r))
2637 format = GBM_FORMAT_XRGB8888;
2639 pixman_region32_fini(&r);
2642 for (i = 0; i < p->count_formats; i++)
2643 if (p->formats[i] == format)
2649 static struct weston_plane *
2650 drm_output_prepare_overlay_view(struct drm_output_state *output_state,
2651 struct weston_view *ev)
2653 struct drm_output *output = output_state->output;
2654 struct weston_compositor *ec = output->base.compositor;
2655 struct drm_backend *b = to_drm_backend(ec);
2656 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
2657 struct wl_resource *buffer_resource;
2658 struct drm_plane *p;
2659 struct drm_plane_state *state = NULL;
2660 struct linux_dmabuf_buffer *dmabuf;
2662 pixman_region32_t dest_rect, src_rect;
2663 pixman_box32_t *box, tbox;
2665 wl_fixed_t sx1, sy1, sx2, sy2;
2667 if (b->sprites_are_broken)
2670 /* Don't import buffers which span multiple outputs. */
2671 if (ev->output_mask != (1u << output->base.id))
2674 /* We can only import GBM buffers. */
2678 if (ev->surface->buffer_ref.buffer == NULL)
2680 buffer_resource = ev->surface->buffer_ref.buffer->resource;
2681 if (wl_shm_buffer_get(buffer_resource))
2684 if (viewport->buffer.transform != output->base.transform)
2686 if (viewport->buffer.scale != output->base.current_scale)
2688 if (!drm_view_transform_supported(ev))
2691 if (ev->alpha != 1.0f)
2694 wl_list_for_each(p, &b->plane_list, link) {
2695 if (p->type != WDRM_PLANE_TYPE_OVERLAY)
2698 if (!drm_plane_is_available(p, output))
2701 state = drm_output_state_get_plane(output_state, p);
2710 /* No sprites available */
2714 if ((dmabuf = linux_dmabuf_buffer_get(buffer_resource))) {
2715 #ifdef HAVE_GBM_FD_IMPORT
2718 * Use AddFB2 directly, do not go via GBM.
2719 * Add support for multiplanar formats.
2720 * Both require refactoring in the DRM-backend to
2721 * support a mix of gbm_bos and drmfbs.
2723 struct gbm_import_fd_data gbm_dmabuf = {
2724 .fd = dmabuf->attributes.fd[0],
2725 .width = dmabuf->attributes.width,
2726 .height = dmabuf->attributes.height,
2727 .stride = dmabuf->attributes.stride[0],
2728 .format = dmabuf->attributes.format
2733 * Currently the buffer is rejected if any dmabuf attribute
2734 * flag is set. This keeps us from passing an inverted /
2735 * interlaced / bottom-first buffer (or any other type that may
2736 * be added in the future) through to an overlay. Ultimately,
2737 * these types of buffers should be handled through buffer
2738 * transforms and not as spot-checks requiring specific
2740 if (dmabuf->attributes.n_planes != 1 ||
2741 dmabuf->attributes.offset[0] != 0 ||
2742 dmabuf->attributes.flags)
2745 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_FD, &gbm_dmabuf,
2746 GBM_BO_USE_SCANOUT);
2751 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER,
2752 buffer_resource, GBM_BO_USE_SCANOUT);
2757 format = drm_output_check_plane_format(p, ev, bo);
2761 state->fb = drm_fb_get_from_bo(bo, b, format, BUFFER_CLIENT);
2765 drm_fb_set_buffer(state->fb, ev->surface->buffer_ref.buffer);
2767 state->output = output;
2769 box = pixman_region32_extents(&ev->transform.boundingbox);
2770 p->base.x = box->x1;
2771 p->base.y = box->y1;
2774 * Calculate the source & dest rects properly based on actual
2775 * position (note the caller has called weston_view_update_transform()
2778 pixman_region32_init(&dest_rect);
2779 pixman_region32_intersect(&dest_rect, &ev->transform.boundingbox,
2780 &output->base.region);
2781 pixman_region32_translate(&dest_rect, -output->base.x, -output->base.y);
2782 box = pixman_region32_extents(&dest_rect);
2783 tbox = weston_transformed_rect(output->base.width,
2784 output->base.height,
2785 output->base.transform,
2786 output->base.current_scale,
2788 state->dest_x = tbox.x1;
2789 state->dest_y = tbox.y1;
2790 state->dest_w = tbox.x2 - tbox.x1;
2791 state->dest_h = tbox.y2 - tbox.y1;
2792 pixman_region32_fini(&dest_rect);
2794 pixman_region32_init(&src_rect);
2795 pixman_region32_intersect(&src_rect, &ev->transform.boundingbox,
2796 &output->base.region);
2797 box = pixman_region32_extents(&src_rect);
2799 weston_view_from_global_fixed(ev,
2800 wl_fixed_from_int(box->x1),
2801 wl_fixed_from_int(box->y1),
2803 weston_view_from_global_fixed(ev,
2804 wl_fixed_from_int(box->x2),
2805 wl_fixed_from_int(box->y2),
2812 if (sx2 > wl_fixed_from_int(ev->surface->width))
2813 sx2 = wl_fixed_from_int(ev->surface->width);
2814 if (sy2 > wl_fixed_from_int(ev->surface->height))
2815 sy2 = wl_fixed_from_int(ev->surface->height);
2822 tbox = weston_transformed_rect(wl_fixed_from_int(ev->surface->width),
2823 wl_fixed_from_int(ev->surface->height),
2824 viewport->buffer.transform,
2825 viewport->buffer.scale,
2828 state->src_x = tbox.x1 << 8;
2829 state->src_y = tbox.y1 << 8;
2830 state->src_w = (tbox.x2 - tbox.x1) << 8;
2831 state->src_h = (tbox.y2 - tbox.y1) << 8;
2832 pixman_region32_fini(&src_rect);
2837 drm_plane_state_put_back(state);
2844 * Update the image for the current cursor surface
2846 * @param b DRM backend structure
2847 * @param bo GBM buffer object to write into
2848 * @param ev View to use for cursor image
2851 cursor_bo_update(struct drm_backend *b, struct gbm_bo *bo,
2852 struct weston_view *ev)
2854 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
2855 uint32_t buf[b->cursor_width * b->cursor_height];
2860 assert(buffer && buffer->shm_buffer);
2861 assert(buffer->shm_buffer == wl_shm_buffer_get(buffer->resource));
2862 assert(ev->surface->width <= b->cursor_width);
2863 assert(ev->surface->height <= b->cursor_height);
2865 memset(buf, 0, sizeof buf);
2866 stride = wl_shm_buffer_get_stride(buffer->shm_buffer);
2867 s = wl_shm_buffer_get_data(buffer->shm_buffer);
2869 wl_shm_buffer_begin_access(buffer->shm_buffer);
2870 for (i = 0; i < ev->surface->height; i++)
2871 memcpy(buf + i * b->cursor_width,
2873 ev->surface->width * 4);
2874 wl_shm_buffer_end_access(buffer->shm_buffer);
2876 if (gbm_bo_write(bo, buf, sizeof buf) < 0)
2877 weston_log("failed update cursor: %m\n");
2880 static struct weston_plane *
2881 drm_output_prepare_cursor_view(struct drm_output_state *output_state,
2882 struct weston_view *ev)
2884 struct drm_output *output = output_state->output;
2885 struct drm_backend *b = to_drm_backend(output->base.compositor);
2886 struct drm_plane *plane = output->cursor_plane;
2887 struct drm_plane_state *plane_state;
2888 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
2889 struct wl_shm_buffer *shmbuf;
2890 bool needs_update = false;
2896 if (b->cursors_are_broken)
2899 if (!plane->state_cur->complete)
2902 if (plane->state_cur->output && plane->state_cur->output != output)
2905 /* Don't import buffers which span multiple outputs. */
2906 if (ev->output_mask != (1u << output->base.id))
2909 /* We use GBM to import SHM buffers. */
2913 if (ev->surface->buffer_ref.buffer == NULL)
2915 shmbuf = wl_shm_buffer_get(ev->surface->buffer_ref.buffer->resource);
2918 if (wl_shm_buffer_get_format(shmbuf) != WL_SHM_FORMAT_ARGB8888)
2921 if (output->base.transform != WL_OUTPUT_TRANSFORM_NORMAL)
2923 if (ev->transform.enabled &&
2924 (ev->transform.matrix.type > WESTON_MATRIX_TRANSFORM_TRANSLATE))
2926 if (viewport->buffer.scale != output->base.current_scale)
2928 if (ev->geometry.scissor_enabled)
2931 if (ev->surface->width > b->cursor_width ||
2932 ev->surface->height > b->cursor_height)
2936 drm_output_state_get_plane(output_state, output->cursor_plane);
2938 if (plane_state && plane_state->fb)
2941 /* Since we're setting plane state up front, we need to work out
2942 * whether or not we need to upload a new cursor. We can't use the
2943 * plane damage, since the planes haven't actually been calculated
2944 * yet: instead try to figure it out directly. KMS cursor planes are
2945 * pretty unique here, in that they lie partway between a Weston plane
2946 * (direct scanout) and a renderer. */
2947 if (ev != output->cursor_view ||
2948 pixman_region32_not_empty(&ev->surface->damage)) {
2949 output->current_cursor++;
2950 output->current_cursor =
2951 output->current_cursor %
2952 ARRAY_LENGTH(output->gbm_cursor_fb);
2953 needs_update = true;
2956 output->cursor_view = ev;
2957 weston_view_to_global_float(ev, 0, 0, &x, &y);
2962 drm_fb_ref(output->gbm_cursor_fb[output->current_cursor]);
2963 plane_state->output = output;
2964 plane_state->src_x = 0;
2965 plane_state->src_y = 0;
2966 plane_state->src_w = b->cursor_width << 16;
2967 plane_state->src_h = b->cursor_height << 16;
2968 plane_state->dest_x = (x - output->base.x) * output->base.current_scale;
2969 plane_state->dest_y = (y - output->base.y) * output->base.current_scale;
2970 plane_state->dest_w = b->cursor_width;
2971 plane_state->dest_h = b->cursor_height;
2974 cursor_bo_update(b, plane_state->fb->bo, ev);
2976 return &plane->base;
2980 drm_output_set_cursor(struct drm_output_state *output_state)
2982 struct drm_output *output = output_state->output;
2983 struct drm_backend *b = to_drm_backend(output->base.compositor);
2984 struct drm_plane *plane = output->cursor_plane;
2985 struct drm_plane_state *state;
2992 state = drm_output_state_get_existing_plane(output_state, plane);
2997 pixman_region32_fini(&plane->base.damage);
2998 pixman_region32_init(&plane->base.damage);
2999 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
3003 assert(state->fb == output->gbm_cursor_fb[output->current_cursor]);
3004 assert(!plane->state_cur->output || plane->state_cur->output == output);
3006 if (plane->state_cur->fb != state->fb) {
3008 handle = gbm_bo_get_handle(bo).s32;
3009 if (drmModeSetCursor(b->drm.fd, output->crtc_id, handle,
3010 b->cursor_width, b->cursor_height)) {
3011 weston_log("failed to set cursor: %m\n");
3016 pixman_region32_fini(&plane->base.damage);
3017 pixman_region32_init(&plane->base.damage);
3019 if (drmModeMoveCursor(b->drm.fd, output->crtc_id,
3020 state->dest_x, state->dest_y)) {
3021 weston_log("failed to move cursor: %m\n");
3028 b->cursors_are_broken = 1;
3029 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
3033 drm_assign_planes(struct weston_output *output_base, void *repaint_data)
3035 struct drm_backend *b = to_drm_backend(output_base->compositor);
3036 struct drm_pending_state *pending_state = repaint_data;
3037 struct drm_output *output = to_drm_output(output_base);
3038 struct drm_output_state *state;
3039 struct drm_plane_state *plane_state;
3040 struct weston_view *ev;
3041 pixman_region32_t surface_overlap, renderer_region;
3042 struct weston_plane *primary, *next_plane;
3043 bool picked_scanout = false;
3045 assert(!output->state_last);
3046 state = drm_output_state_duplicate(output->state_cur,
3048 DRM_OUTPUT_STATE_CLEAR_PLANES);
3051 * Find a surface for each sprite in the output using some heuristics:
3053 * 2) frequency of update
3054 * 3) opacity (though some hw might support alpha blending)
3055 * 4) clipping (this can be fixed with color keys)
3057 * The idea is to save on blitting since this should save power.
3058 * If we can get a large video surface on the sprite for example,
3059 * the main display surface may not need to update at all, and
3060 * the client buffer can be used directly for the sprite surface
3061 * as we do for flipping full screen surfaces.
3063 pixman_region32_init(&renderer_region);
3064 primary = &output_base->compositor->primary_plane;
3066 wl_list_for_each(ev, &output_base->compositor->view_list, link) {
3067 struct weston_surface *es = ev->surface;
3069 /* Test whether this buffer can ever go into a plane:
3070 * non-shm, or small enough to be a cursor.
3072 * Also, keep a reference when using the pixman renderer.
3073 * That makes it possible to do a seamless switch to the GL
3074 * renderer and since the pixman renderer keeps a reference
3075 * to the buffer anyway, there is no side effects.
3077 if (b->use_pixman ||
3078 (es->buffer_ref.buffer &&
3079 (!wl_shm_buffer_get(es->buffer_ref.buffer->resource) ||
3080 (ev->surface->width <= b->cursor_width &&
3081 ev->surface->height <= b->cursor_height))))
3082 es->keep_buffer = true;
3084 es->keep_buffer = false;
3086 pixman_region32_init(&surface_overlap);
3087 pixman_region32_intersect(&surface_overlap, &renderer_region,
3088 &ev->transform.boundingbox);
3091 if (pixman_region32_not_empty(&surface_overlap) || picked_scanout)
3092 next_plane = primary;
3093 if (next_plane == NULL)
3094 next_plane = drm_output_prepare_cursor_view(state, ev);
3096 /* If a higher-stacked view already got assigned to scanout, it's incorrect to
3097 * assign a subsequent (lower-stacked) view to scanout.
3099 if (next_plane == NULL) {
3100 next_plane = drm_output_prepare_scanout_view(state, ev);
3102 picked_scanout = true;
3105 if (next_plane == NULL)
3106 next_plane = drm_output_prepare_overlay_view(state, ev);
3108 if (next_plane == NULL)
3109 next_plane = primary;
3111 weston_view_move_to_plane(ev, next_plane);
3113 if (next_plane == primary)
3114 pixman_region32_union(&renderer_region,
3116 &ev->transform.boundingbox);
3118 if (next_plane == primary ||
3119 (output->cursor_plane &&
3120 next_plane == &output->cursor_plane->base)) {
3121 /* cursor plane involves a copy */
3124 /* All other planes are a direct scanout of a
3125 * single client buffer.
3127 ev->psf_flags = WP_PRESENTATION_FEEDBACK_KIND_ZERO_COPY;
3130 pixman_region32_fini(&surface_overlap);
3132 pixman_region32_fini(&renderer_region);
3134 /* We rely on output->cursor_view being both an accurate reflection of
3135 * the cursor plane's state, but also being maintained across repaints
3136 * to avoid unnecessary damage uploads, per the comment in
3137 * drm_output_prepare_cursor_view. In the event that we go from having
3138 * a cursor view to not having a cursor view, we need to clear it. */
3139 if (output->cursor_view) {
3141 drm_output_state_get_existing_plane(state,
3142 output->cursor_plane);
3143 if (!plane_state || !plane_state->fb)
3144 output->cursor_view = NULL;
3149 * Find the closest-matching mode for a given target
3151 * Given a target mode, find the most suitable mode amongst the output's
3152 * current mode list to use, preferring the current mode if possible, to
3153 * avoid an expensive mode switch.
3155 * @param output DRM output
3156 * @param target_mode Mode to attempt to match
3157 * @returns Pointer to a mode from the output's mode list
3159 static struct drm_mode *
3160 choose_mode (struct drm_output *output, struct weston_mode *target_mode)
3162 struct drm_mode *tmp_mode = NULL, *mode;
3164 if (output->base.current_mode->width == target_mode->width &&
3165 output->base.current_mode->height == target_mode->height &&
3166 (output->base.current_mode->refresh == target_mode->refresh ||
3167 target_mode->refresh == 0))
3168 return to_drm_mode(output->base.current_mode);
3170 wl_list_for_each(mode, &output->base.mode_list, base.link) {
3171 if (mode->mode_info.hdisplay == target_mode->width &&
3172 mode->mode_info.vdisplay == target_mode->height) {
3173 if (mode->base.refresh == target_mode->refresh ||
3174 target_mode->refresh == 0) {
3176 } else if (!tmp_mode)
3185 drm_output_init_egl(struct drm_output *output, struct drm_backend *b);
3187 drm_output_fini_egl(struct drm_output *output);
3189 drm_output_init_pixman(struct drm_output *output, struct drm_backend *b);
3191 drm_output_fini_pixman(struct drm_output *output);
3194 drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mode)
3196 struct drm_output *output = to_drm_output(output_base);
3197 struct drm_backend *b = to_drm_backend(output_base->compositor);
3198 struct drm_mode *drm_mode = choose_mode(output, mode);
3201 weston_log("%s: invalid resolution %dx%d\n",
3202 output_base->name, mode->width, mode->height);
3206 if (&drm_mode->base == output->base.current_mode)
3209 output->base.current_mode->flags = 0;
3211 output->base.current_mode = &drm_mode->base;
3212 output->base.current_mode->flags =
3213 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
3215 /* XXX: This drops our current buffer too early, before we've started
3216 * displaying it. Ideally this should be much more atomic and
3217 * integrated with a full repaint cycle, rather than doing a
3218 * sledgehammer modeswitch first, and only later showing new
3221 b->state_invalid = true;
3223 if (b->use_pixman) {
3224 drm_output_fini_pixman(output);
3225 if (drm_output_init_pixman(output, b) < 0) {
3226 weston_log("failed to init output pixman state with "
3231 drm_output_fini_egl(output);
3232 if (drm_output_init_egl(output, b) < 0) {
3233 weston_log("failed to init output egl state with "
3243 on_drm_input(int fd, uint32_t mask, void *data)
3245 #ifdef HAVE_DRM_ATOMIC
3246 struct drm_backend *b = data;
3248 drmEventContext evctx;
3250 memset(&evctx, 0, sizeof evctx);
3251 #ifndef HAVE_DRM_ATOMIC
3255 if (b->atomic_modeset)
3256 evctx.page_flip_handler2 = atomic_flip_handler;
3259 evctx.page_flip_handler = page_flip_handler;
3260 evctx.vblank_handler = vblank_handler;
3261 drmHandleEvent(fd, &evctx);
3267 init_kms_caps(struct drm_backend *b)
3273 weston_log("using %s\n", b->drm.filename);
3275 ret = drmGetCap(b->drm.fd, DRM_CAP_TIMESTAMP_MONOTONIC, &cap);
3276 if (ret == 0 && cap == 1)
3277 clk_id = CLOCK_MONOTONIC;
3279 clk_id = CLOCK_REALTIME;
3281 if (weston_compositor_set_presentation_clock(b->compositor, clk_id) < 0) {
3282 weston_log("Error: failed to set presentation clock %d.\n",
3287 ret = drmGetCap(b->drm.fd, DRM_CAP_CURSOR_WIDTH, &cap);
3289 b->cursor_width = cap;
3291 b->cursor_width = 64;
3293 ret = drmGetCap(b->drm.fd, DRM_CAP_CURSOR_HEIGHT, &cap);
3295 b->cursor_height = cap;
3297 b->cursor_height = 64;
3299 if (!getenv("WESTON_DISABLE_UNIVERSAL_PLANES")) {
3300 ret = drmSetClientCap(b->drm.fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
3301 b->universal_planes = (ret == 0);
3303 weston_log("DRM: %s universal planes\n",
3304 b->universal_planes ? "supports" : "does not support");
3306 #ifdef HAVE_DRM_ATOMIC
3307 if (b->universal_planes && !getenv("WESTON_DISABLE_ATOMIC")) {
3308 ret = drmGetCap(b->drm.fd, DRM_CAP_CRTC_IN_VBLANK_EVENT, &cap);
3311 ret = drmSetClientCap(b->drm.fd, DRM_CLIENT_CAP_ATOMIC, 1);
3312 b->atomic_modeset = ((ret == 0) && (cap == 1));
3315 weston_log("DRM: %s atomic modesetting\n",
3316 b->atomic_modeset ? "supports" : "does not support");
3321 static struct gbm_device *
3322 create_gbm_device(int fd)
3324 struct gbm_device *gbm;
3326 gl_renderer = weston_load_module("gl-renderer.so",
3327 "gl_renderer_interface");
3331 /* GBM will load a dri driver, but even though they need symbols from
3332 * libglapi, in some version of Mesa they are not linked to it. Since
3333 * only the gl-renderer module links to it, the call above won't make
3334 * these symbols globally available, and loading the DRI driver fails.
3335 * Workaround this by dlopen()'ing libglapi with RTLD_GLOBAL. */
3336 dlopen("libglapi.so.0", RTLD_LAZY | RTLD_GLOBAL);
3338 gbm = gbm_create_device(fd);
3343 /* When initializing EGL, if the preferred buffer format isn't available
3344 * we may be able to substitute an ARGB format for an XRGB one.
3346 * This returns 0 if substitution isn't possible, but 0 might be a
3347 * legitimate format for other EGL platforms, so the caller is
3348 * responsible for checking for 0 before calling gl_renderer->create().
3350 * This works around https://bugs.freedesktop.org/show_bug.cgi?id=89689
3351 * but it's entirely possible we'll see this again on other implementations.
3354 fallback_format_for(uint32_t format)
3357 case GBM_FORMAT_XRGB8888:
3358 return GBM_FORMAT_ARGB8888;
3359 case GBM_FORMAT_XRGB2101010:
3360 return GBM_FORMAT_ARGB2101010;
3367 drm_backend_create_gl_renderer(struct drm_backend *b)
3369 EGLint format[3] = {
3371 fallback_format_for(b->gbm_format),
3378 if (gl_renderer->display_create(b->compositor,
3379 EGL_PLATFORM_GBM_KHR,
3382 gl_renderer->opaque_attribs,
3392 init_egl(struct drm_backend *b)
3394 b->gbm = create_gbm_device(b->drm.fd);
3399 if (drm_backend_create_gl_renderer(b) < 0) {
3400 gbm_device_destroy(b->gbm);
3408 init_pixman(struct drm_backend *b)
3410 return pixman_renderer_init(b->compositor);
3414 * Create a drm_plane for a hardware plane
3416 * Creates one drm_plane structure for a hardware plane, and initialises its
3417 * properties and formats.
3419 * In the absence of universal plane support, where KMS does not explicitly
3420 * expose the primary and cursor planes to userspace, this may also create
3421 * an 'internal' plane for internal management.
3423 * This function does not add the plane to the list of usable planes in Weston
3424 * itself; the caller is responsible for this.
3426 * Call drm_plane_destroy to clean up the plane.
3428 * @sa drm_output_find_special_plane
3429 * @param b DRM compositor backend
3430 * @param kplane DRM plane to create, or NULL if creating internal plane
3431 * @param output Output to create internal plane for, or NULL
3432 * @param type Type to use when creating internal plane, or invalid
3433 * @param format Format to use for internal planes, or 0
3435 static struct drm_plane *
3436 drm_plane_create(struct drm_backend *b, const drmModePlane *kplane,
3437 struct drm_output *output, enum wdrm_plane_type type,
3440 struct drm_plane *plane;
3441 drmModeObjectProperties *props;
3442 int num_formats = (kplane) ? kplane->count_formats : 1;
3444 plane = zalloc(sizeof(*plane) +
3445 (sizeof(uint32_t) * num_formats));
3447 weston_log("%s: out of memory\n", __func__);
3452 plane->state_cur = drm_plane_state_alloc(NULL, plane);
3453 plane->state_cur->complete = true;
3456 plane->possible_crtcs = kplane->possible_crtcs;
3457 plane->plane_id = kplane->plane_id;
3458 plane->count_formats = kplane->count_formats;
3459 memcpy(plane->formats, kplane->formats,
3460 kplane->count_formats * sizeof(kplane->formats[0]));
3462 props = drmModeObjectGetProperties(b->drm.fd, kplane->plane_id,
3463 DRM_MODE_OBJECT_PLANE);
3465 weston_log("couldn't get plane properties\n");
3468 drm_property_info_populate(b, plane_props, plane->props,
3469 WDRM_PLANE__COUNT, props);
3471 drm_property_get_value(&plane->props[WDRM_PLANE_TYPE],
3473 WDRM_PLANE_TYPE__COUNT);
3474 drmModeFreeObjectProperties(props);
3477 plane->possible_crtcs = (1 << output->pipe);
3478 plane->plane_id = 0;
3479 plane->count_formats = 1;
3480 plane->formats[0] = format;
3484 if (plane->type == WDRM_PLANE_TYPE__COUNT)
3487 /* With universal planes, everything is a DRM plane; without
3488 * universal planes, the only DRM planes are overlay planes.
3489 * Everything else is a fake plane. */
3490 if (b->universal_planes) {
3494 assert(plane->type == WDRM_PLANE_TYPE_OVERLAY);
3496 assert(plane->type != WDRM_PLANE_TYPE_OVERLAY &&
3500 weston_plane_init(&plane->base, b->compositor, 0, 0);
3501 wl_list_insert(&b->plane_list, &plane->link);
3506 drm_property_info_free(plane->props, WDRM_PLANE__COUNT);
3508 drm_plane_state_free(plane->state_cur, true);
3514 * Find, or create, a special-purpose plane
3516 * Primary and cursor planes are a special case, in that before universal
3517 * planes, they are driven by non-plane API calls. Without universal plane
3518 * support, the only way to configure a primary plane is via drmModeSetCrtc,
3519 * and the only way to configure a cursor plane is drmModeSetCursor2.
3521 * Although they may actually be regular planes in the hardware, without
3522 * universal plane support, these planes are not actually exposed to
3523 * userspace in the regular plane list.
3525 * However, for ease of internal tracking, we want to manage all planes
3526 * through the same drm_plane structures. Therefore, when we are running
3527 * without universal plane support, we create fake drm_plane structures
3528 * to track these planes.
3530 * @param b DRM backend
3531 * @param output Output to use for plane
3532 * @param type Type of plane
3534 static struct drm_plane *
3535 drm_output_find_special_plane(struct drm_backend *b, struct drm_output *output,
3536 enum wdrm_plane_type type)
3538 struct drm_plane *plane;
3540 if (!b->universal_planes) {
3544 case WDRM_PLANE_TYPE_CURSOR:
3545 format = GBM_FORMAT_ARGB8888;
3547 case WDRM_PLANE_TYPE_PRIMARY:
3548 /* We don't know what formats the primary plane supports
3549 * before universal planes, so we just assume that the
3550 * GBM format works; however, this isn't set until after
3551 * the output is created. */
3555 assert(!"invalid type in drm_output_find_special_plane");
3559 return drm_plane_create(b, NULL, output, type, format);
3562 wl_list_for_each(plane, &b->plane_list, link) {
3563 struct drm_output *tmp;
3564 bool found_elsewhere = false;
3566 if (plane->type != type)
3568 if (!drm_plane_is_available(plane, output))
3571 /* On some platforms, primary/cursor planes can roam
3572 * between different CRTCs, so make sure we don't claim the
3573 * same plane for two outputs. */
3574 wl_list_for_each(tmp, &b->compositor->output_list,
3576 if (tmp->cursor_plane == plane ||
3577 tmp->scanout_plane == plane) {
3578 found_elsewhere = true;
3583 if (found_elsewhere)
3586 plane->possible_crtcs = (1 << output->pipe);
3594 * Destroy one DRM plane
3596 * Destroy a DRM plane, removing it from screen and releasing its retained
3597 * buffers in the process. The counterpart to drm_plane_create.
3599 * @param plane Plane to deallocate (will be freed)
3602 drm_plane_destroy(struct drm_plane *plane)
3604 if (plane->type == WDRM_PLANE_TYPE_OVERLAY)
3605 drmModeSetPlane(plane->backend->drm.fd, plane->plane_id,
3606 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
3607 drm_plane_state_free(plane->state_cur, true);
3608 drm_property_info_free(plane->props, WDRM_PLANE__COUNT);
3609 weston_plane_release(&plane->base);
3610 wl_list_remove(&plane->link);
3615 * Initialise sprites (overlay planes)
3617 * Walk the list of provided DRM planes, and add overlay planes.
3619 * Call destroy_sprites to free these planes.
3621 * @param b DRM compositor backend
3624 create_sprites(struct drm_backend *b)
3626 drmModePlaneRes *kplane_res;
3627 drmModePlane *kplane;
3628 struct drm_plane *drm_plane;
3630 kplane_res = drmModeGetPlaneResources(b->drm.fd);
3632 weston_log("failed to get plane resources: %s\n",
3637 for (i = 0; i < kplane_res->count_planes; i++) {
3638 kplane = drmModeGetPlane(b->drm.fd, kplane_res->planes[i]);
3642 drm_plane = drm_plane_create(b, kplane, NULL,
3643 WDRM_PLANE_TYPE__COUNT, 0);
3644 drmModeFreePlane(kplane);
3648 if (drm_plane->type == WDRM_PLANE_TYPE_OVERLAY)
3649 weston_compositor_stack_plane(b->compositor,
3651 &b->compositor->primary_plane);
3654 drmModeFreePlaneResources(kplane_res);
3658 * Clean up sprites (overlay planes)
3660 * The counterpart to create_sprites.
3662 * @param b DRM compositor backend
3665 destroy_sprites(struct drm_backend *b)
3667 struct drm_plane *plane, *next;
3669 wl_list_for_each_safe(plane, next, &b->plane_list, link)
3670 drm_plane_destroy(plane);
3674 drm_refresh_rate_mHz(const drmModeModeInfo *info)
3678 /* Calculate higher precision (mHz) refresh rate */
3679 refresh = (info->clock * 1000000LL / info->htotal +
3680 info->vtotal / 2) / info->vtotal;
3682 if (info->flags & DRM_MODE_FLAG_INTERLACE)
3684 if (info->flags & DRM_MODE_FLAG_DBLSCAN)
3686 if (info->vscan > 1)
3687 refresh /= info->vscan;
3693 * Add a mode to output's mode list
3695 * Copy the supplied DRM mode into a Weston mode structure, and add it to the
3696 * output's mode list.
3698 * @param output DRM output to add mode to
3699 * @param info DRM mode structure to add
3700 * @returns Newly-allocated Weston/DRM mode structure
3702 static struct drm_mode *
3703 drm_output_add_mode(struct drm_output *output, const drmModeModeInfo *info)
3705 struct drm_mode *mode;
3707 mode = malloc(sizeof *mode);
3711 mode->base.flags = 0;
3712 mode->base.width = info->hdisplay;
3713 mode->base.height = info->vdisplay;
3715 mode->base.refresh = drm_refresh_rate_mHz(info);
3716 mode->mode_info = *info;
3719 if (info->type & DRM_MODE_TYPE_PREFERRED)
3720 mode->base.flags |= WL_OUTPUT_MODE_PREFERRED;
3722 wl_list_insert(output->base.mode_list.prev, &mode->base.link);
3728 * Destroys a mode, and removes it from the list.
3731 drm_output_destroy_mode(struct drm_backend *backend, struct drm_mode *mode)
3734 drmModeDestroyPropertyBlob(backend->drm.fd, mode->blob_id);
3735 wl_list_remove(&mode->base.link);
3739 /** Destroy a list of drm_modes
3741 * @param backend The backend for releasing mode property blobs.
3742 * @param mode_list The list linked by drm_mode::base.link.
3745 drm_mode_list_destroy(struct drm_backend *backend, struct wl_list *mode_list)
3747 struct drm_mode *mode, *next;
3749 wl_list_for_each_safe(mode, next, mode_list, base.link)
3750 drm_output_destroy_mode(backend, mode);
3754 drm_subpixel_to_wayland(int drm_value)
3756 switch (drm_value) {
3758 case DRM_MODE_SUBPIXEL_UNKNOWN:
3759 return WL_OUTPUT_SUBPIXEL_UNKNOWN;
3760 case DRM_MODE_SUBPIXEL_NONE:
3761 return WL_OUTPUT_SUBPIXEL_NONE;
3762 case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB:
3763 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB;
3764 case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR:
3765 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR;
3766 case DRM_MODE_SUBPIXEL_VERTICAL_RGB:
3767 return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB;
3768 case DRM_MODE_SUBPIXEL_VERTICAL_BGR:
3769 return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR;
3773 /* returns a value between 0-255 range, where higher is brighter */
3775 drm_get_backlight(struct drm_head *head)
3777 long brightness, max_brightness, norm;
3779 brightness = backlight_get_brightness(head->backlight);
3780 max_brightness = backlight_get_max_brightness(head->backlight);
3782 /* convert it on a scale of 0 to 255 */
3783 norm = (brightness * 255)/(max_brightness);
3785 return (uint32_t) norm;
3788 /* values accepted are between 0-255 range */
3790 drm_set_backlight(struct weston_output *output_base, uint32_t value)
3792 struct drm_head *head = to_drm_head(weston_output_get_first_head(output_base));
3793 long max_brightness, new_brightness;
3795 if (!head->backlight)
3801 max_brightness = backlight_get_max_brightness(head->backlight);
3803 /* get denormalized value */
3804 new_brightness = (value * max_brightness) / 255;
3806 backlight_set_brightness(head->backlight, new_brightness);
3810 * Power output on or off
3812 * The DPMS/power level of an output is used to switch it on or off. This
3813 * is DRM's hook for doing so, which can called either as part of repaint,
3814 * or independently of the repaint loop.
3816 * If we are called as part of repaint, we simply set the relevant bit in
3820 drm_set_dpms(struct weston_output *output_base, enum dpms_enum level)
3822 struct drm_output *output = to_drm_output(output_base);
3823 struct drm_backend *b = to_drm_backend(output_base->compositor);
3824 struct drm_pending_state *pending_state = b->repaint_data;
3825 struct drm_output_state *state;
3828 if (output->state_cur->dpms == level)
3831 /* If we're being called during the repaint loop, then this is
3832 * simple: discard any previously-generated state, and create a new
3833 * state where we disable everything. When we come to flush, this
3836 * However, we need to be careful: we can be called whilst another
3837 * output is in its repaint cycle (pending_state exists), but our
3838 * output still has an incomplete state application outstanding.
3839 * In that case, we need to wait until that completes. */
3840 if (pending_state && !output->state_last) {
3841 /* The repaint loop already sets DPMS on; we don't need to
3842 * explicitly set it on here, as it will already happen
3843 * whilst applying the repaint state. */
3844 if (level == WESTON_DPMS_ON)
3847 state = drm_pending_state_get_output(pending_state, output);
3849 drm_output_state_free(state);
3850 state = drm_output_get_disable_state(pending_state, output);
3854 /* As we throw everything away when disabling, just send us back through
3855 * a repaint cycle. */
3856 if (level == WESTON_DPMS_ON) {
3857 if (output->dpms_off_pending)
3858 output->dpms_off_pending = 0;
3859 weston_output_schedule_repaint(output_base);
3863 /* If we've already got a request in the pipeline, then we need to
3864 * park our DPMS request until that request has quiesced. */
3865 if (output->state_last) {
3866 output->dpms_off_pending = 1;
3870 pending_state = drm_pending_state_alloc(b);
3871 drm_output_get_disable_state(pending_state, output);
3872 ret = drm_pending_state_apply_sync(pending_state);
3874 weston_log("drm_set_dpms: couldn't disable output?\n");
3877 static const char * const connector_type_names[] = {
3878 [DRM_MODE_CONNECTOR_Unknown] = "Unknown",
3879 [DRM_MODE_CONNECTOR_VGA] = "VGA",
3880 [DRM_MODE_CONNECTOR_DVII] = "DVI-I",
3881 [DRM_MODE_CONNECTOR_DVID] = "DVI-D",
3882 [DRM_MODE_CONNECTOR_DVIA] = "DVI-A",
3883 [DRM_MODE_CONNECTOR_Composite] = "Composite",
3884 [DRM_MODE_CONNECTOR_SVIDEO] = "SVIDEO",
3885 [DRM_MODE_CONNECTOR_LVDS] = "LVDS",
3886 [DRM_MODE_CONNECTOR_Component] = "Component",
3887 [DRM_MODE_CONNECTOR_9PinDIN] = "DIN",
3888 [DRM_MODE_CONNECTOR_DisplayPort] = "DP",
3889 [DRM_MODE_CONNECTOR_HDMIA] = "HDMI-A",
3890 [DRM_MODE_CONNECTOR_HDMIB] = "HDMI-B",
3891 [DRM_MODE_CONNECTOR_TV] = "TV",
3892 [DRM_MODE_CONNECTOR_eDP] = "eDP",
3893 #ifdef DRM_MODE_CONNECTOR_DSI
3894 [DRM_MODE_CONNECTOR_VIRTUAL] = "Virtual",
3895 [DRM_MODE_CONNECTOR_DSI] = "DSI",
3899 /** Create a name given a DRM connector
3901 * \param con The DRM connector whose type and id form the name.
3902 * \return A newly allocate string, or NULL on error. Must be free()'d
3905 * The name does not identify the DRM display device.
3908 make_connector_name(const drmModeConnector *con)
3911 const char *type_name = NULL;
3914 if (con->connector_type < ARRAY_LENGTH(connector_type_names))
3915 type_name = connector_type_names[con->connector_type];
3918 type_name = "UNNAMED";
3920 ret = asprintf(&name, "%s-%d", type_name, con->connector_type_id);
3928 find_crtc_for_connector(struct drm_backend *b,
3929 drmModeRes *resources, drmModeConnector *connector)
3931 drmModeEncoder *encoder;
3935 for (j = 0; j < connector->count_encoders; j++) {
3936 uint32_t possible_crtcs, encoder_id, crtc_id;
3938 encoder = drmModeGetEncoder(b->drm.fd, connector->encoders[j]);
3939 if (encoder == NULL) {
3940 weston_log("Failed to get encoder.\n");
3943 encoder_id = encoder->encoder_id;
3944 possible_crtcs = encoder->possible_crtcs;
3945 crtc_id = encoder->crtc_id;
3946 drmModeFreeEncoder(encoder);
3948 for (i = 0; i < resources->count_crtcs; i++) {
3949 if (!(possible_crtcs & (1 << i)))
3952 if (drm_output_find_by_crtc(b, resources->crtcs[i]))
3955 /* Try to preserve the existing
3956 * CRTC -> encoder -> connector routing; it makes
3957 * initialisation faster, and also since we have a
3958 * very dumb picking algorithm, may preserve a better
3960 if (!connector->encoder_id ||
3961 (encoder_id == connector->encoder_id &&
3962 crtc_id == resources->crtcs[i]))
3972 static void drm_output_fini_cursor_egl(struct drm_output *output)
3976 for (i = 0; i < ARRAY_LENGTH(output->gbm_cursor_fb); i++) {
3977 drm_fb_unref(output->gbm_cursor_fb[i]);
3978 output->gbm_cursor_fb[i] = NULL;
3983 drm_output_init_cursor_egl(struct drm_output *output, struct drm_backend *b)
3987 /* No point creating cursors if we don't have a plane for them. */
3988 if (!output->cursor_plane)
3991 for (i = 0; i < ARRAY_LENGTH(output->gbm_cursor_fb); i++) {
3994 bo = gbm_bo_create(b->gbm, b->cursor_width, b->cursor_height,
3995 GBM_FORMAT_ARGB8888,
3996 GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE);
4000 output->gbm_cursor_fb[i] =
4001 drm_fb_get_from_bo(bo, b, GBM_FORMAT_ARGB8888,
4003 if (!output->gbm_cursor_fb[i]) {
4012 weston_log("cursor buffers unavailable, using gl cursors\n");
4013 b->cursors_are_broken = 1;
4014 drm_output_fini_cursor_egl(output);
4018 /* Init output state that depends on gl or gbm */
4020 drm_output_init_egl(struct drm_output *output, struct drm_backend *b)
4022 EGLint format[2] = {
4024 fallback_format_for(output->gbm_format),
4028 output->gbm_surface = gbm_surface_create(b->gbm,
4029 output->base.current_mode->width,
4030 output->base.current_mode->height,
4032 GBM_BO_USE_SCANOUT |
4033 GBM_BO_USE_RENDERING);
4034 if (!output->gbm_surface) {
4035 weston_log("failed to create gbm surface\n");
4041 if (gl_renderer->output_window_create(&output->base,
4042 (EGLNativeWindowType)output->gbm_surface,
4043 output->gbm_surface,
4044 gl_renderer->opaque_attribs,
4047 weston_log("failed to create gl renderer output state\n");
4048 gbm_surface_destroy(output->gbm_surface);
4052 drm_output_init_cursor_egl(output, b);
4058 drm_output_fini_egl(struct drm_output *output)
4060 struct drm_backend *b = to_drm_backend(output->base.compositor);
4062 /* Destroying the GBM surface will destroy all our GBM buffers,
4063 * regardless of refcount. Ensure we destroy them here. */
4064 if (!b->shutting_down &&
4065 output->scanout_plane->state_cur->fb &&
4066 output->scanout_plane->state_cur->fb->type == BUFFER_GBM_SURFACE) {
4067 drm_plane_state_free(output->scanout_plane->state_cur, true);
4068 output->scanout_plane->state_cur =
4069 drm_plane_state_alloc(NULL, output->scanout_plane);
4070 output->scanout_plane->state_cur->complete = true;
4073 gl_renderer->output_destroy(&output->base);
4074 gbm_surface_destroy(output->gbm_surface);
4075 drm_output_fini_cursor_egl(output);
4079 drm_output_init_pixman(struct drm_output *output, struct drm_backend *b)
4081 int w = output->base.current_mode->width;
4082 int h = output->base.current_mode->height;
4083 uint32_t format = output->gbm_format;
4084 uint32_t pixman_format;
4088 case GBM_FORMAT_XRGB8888:
4089 pixman_format = PIXMAN_x8r8g8b8;
4091 case GBM_FORMAT_RGB565:
4092 pixman_format = PIXMAN_r5g6b5;
4095 weston_log("Unsupported pixman format 0x%x\n", format);
4099 /* FIXME error checking */
4100 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
4101 output->dumb[i] = drm_fb_create_dumb(b, w, h, format);
4102 if (!output->dumb[i])
4106 pixman_image_create_bits(pixman_format, w, h,
4107 output->dumb[i]->map,
4108 output->dumb[i]->stride);
4109 if (!output->image[i])
4113 if (pixman_renderer_output_create(&output->base) < 0)
4116 pixman_region32_init_rect(&output->previous_damage,
4117 output->base.x, output->base.y, output->base.width, output->base.height);
4122 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
4123 if (output->dumb[i])
4124 drm_fb_unref(output->dumb[i]);
4125 if (output->image[i])
4126 pixman_image_unref(output->image[i]);
4128 output->dumb[i] = NULL;
4129 output->image[i] = NULL;
4136 drm_output_fini_pixman(struct drm_output *output)
4138 struct drm_backend *b = to_drm_backend(output->base.compositor);
4141 /* Destroying the Pixman surface will destroy all our buffers,
4142 * regardless of refcount. Ensure we destroy them here. */
4143 if (!b->shutting_down &&
4144 output->scanout_plane->state_cur->fb &&
4145 output->scanout_plane->state_cur->fb->type == BUFFER_PIXMAN_DUMB) {
4146 drm_plane_state_free(output->scanout_plane->state_cur, true);
4147 output->scanout_plane->state_cur =
4148 drm_plane_state_alloc(NULL, output->scanout_plane);
4149 output->scanout_plane->state_cur->complete = true;
4152 pixman_renderer_output_destroy(&output->base);
4153 pixman_region32_fini(&output->previous_damage);
4155 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
4156 pixman_image_unref(output->image[i]);
4157 drm_fb_unref(output->dumb[i]);
4158 output->dumb[i] = NULL;
4159 output->image[i] = NULL;
4164 edid_parse_string(const uint8_t *data, char text[])
4169 /* this is always 12 bytes, but we can't guarantee it's null
4170 * terminated or not junk. */
4171 strncpy(text, (const char *) data, 12);
4173 /* guarantee our new string is null-terminated */
4176 /* remove insane chars */
4177 for (i = 0; text[i] != '\0'; i++) {
4178 if (text[i] == '\n' ||
4185 /* ensure string is printable */
4186 for (i = 0; text[i] != '\0'; i++) {
4187 if (!isprint(text[i])) {
4193 /* if the string is random junk, ignore the string */
4198 #define EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING 0xfe
4199 #define EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME 0xfc
4200 #define EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER 0xff
4201 #define EDID_OFFSET_DATA_BLOCKS 0x36
4202 #define EDID_OFFSET_LAST_BLOCK 0x6c
4203 #define EDID_OFFSET_PNPID 0x08
4204 #define EDID_OFFSET_SERIAL 0x0c
4207 edid_parse(struct drm_edid *edid, const uint8_t *data, size_t length)
4210 uint32_t serial_number;
4215 if (data[0] != 0x00 || data[1] != 0xff)
4218 /* decode the PNP ID from three 5 bit words packed into 2 bytes
4223 edid->pnp_id[0] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x7c) / 4) - 1;
4224 edid->pnp_id[1] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x3) * 8) + ((data[EDID_OFFSET_PNPID + 1] & 0xe0) / 32) - 1;
4225 edid->pnp_id[2] = 'A' + (data[EDID_OFFSET_PNPID + 1] & 0x1f) - 1;
4226 edid->pnp_id[3] = '\0';
4228 /* maybe there isn't a ASCII serial number descriptor, so use this instead */
4229 serial_number = (uint32_t) data[EDID_OFFSET_SERIAL + 0];
4230 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 1] * 0x100;
4231 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 2] * 0x10000;
4232 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 3] * 0x1000000;
4233 if (serial_number > 0)
4234 sprintf(edid->serial_number, "%lu", (unsigned long) serial_number);
4236 /* parse EDID data */
4237 for (i = EDID_OFFSET_DATA_BLOCKS;
4238 i <= EDID_OFFSET_LAST_BLOCK;
4240 /* ignore pixel clock data */
4246 /* any useful blocks? */
4247 if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME) {
4248 edid_parse_string(&data[i+5],
4249 edid->monitor_name);
4250 } else if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER) {
4251 edid_parse_string(&data[i+5],
4252 edid->serial_number);
4253 } else if (data[i+3] == EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING) {
4254 edid_parse_string(&data[i+5],
4261 /** Parse monitor make, model and serial from EDID
4263 * \param head The head whose \c drm_edid to fill in.
4264 * \param props The DRM connector properties to get the EDID from.
4265 * \param make[out] The monitor make (PNP ID).
4266 * \param model[out] The monitor model (name).
4267 * \param serial_number[out] The monitor serial number.
4269 * Each of \c *make, \c *model and \c *serial_number are set only if the
4270 * information is found in the EDID. The pointers they are set to must not
4271 * be free()'d explicitly, instead they get implicitly freed when the
4272 * \c drm_head is destroyed.
4275 find_and_parse_output_edid(struct drm_head *head,
4276 drmModeObjectPropertiesPtr props,
4279 const char **serial_number)
4281 drmModePropertyBlobPtr edid_blob = NULL;
4286 drm_property_get_value(&head->props_conn[WDRM_CONNECTOR_EDID],
4291 edid_blob = drmModeGetPropertyBlob(head->backend->drm.fd, blob_id);
4295 rc = edid_parse(&head->edid,
4299 weston_log("EDID data '%s', '%s', '%s'\n",
4301 head->edid.monitor_name,
4302 head->edid.serial_number);
4303 if (head->edid.pnp_id[0] != '\0')
4304 *make = head->edid.pnp_id;
4305 if (head->edid.monitor_name[0] != '\0')
4306 *model = head->edid.monitor_name;
4307 if (head->edid.serial_number[0] != '\0')
4308 *serial_number = head->edid.serial_number;
4310 drmModeFreePropertyBlob(edid_blob);
4314 parse_modeline(const char *s, drmModeModeInfo *mode)
4320 memset(mode, 0, sizeof *mode);
4322 mode->type = DRM_MODE_TYPE_USERDEF;
4328 if (sscanf(s, "%f %hd %hd %hd %hd %hd %hd %hd %hd %15s %15s",
4337 &mode->vtotal, hsync, vsync) != 11)
4340 mode->clock = fclock * 1000;
4341 if (strcmp(hsync, "+hsync") == 0)
4342 mode->flags |= DRM_MODE_FLAG_PHSYNC;
4343 else if (strcmp(hsync, "-hsync") == 0)
4344 mode->flags |= DRM_MODE_FLAG_NHSYNC;
4348 if (strcmp(vsync, "+vsync") == 0)
4349 mode->flags |= DRM_MODE_FLAG_PVSYNC;
4350 else if (strcmp(vsync, "-vsync") == 0)
4351 mode->flags |= DRM_MODE_FLAG_NVSYNC;
4355 snprintf(mode->name, sizeof mode->name, "%dx%d@%.3f",
4356 mode->hdisplay, mode->vdisplay, fclock);
4362 setup_output_seat_constraint(struct drm_backend *b,
4363 struct weston_output *output,
4366 if (strcmp(s, "") != 0) {
4367 struct weston_pointer *pointer;
4368 struct udev_seat *seat;
4370 seat = udev_seat_get_named(&b->input, s);
4374 seat->base.output = output;
4376 pointer = weston_seat_get_pointer(&seat->base);
4378 weston_pointer_clamp(pointer,
4385 drm_output_attach_head(struct weston_output *output_base,
4386 struct weston_head *head_base)
4388 if (wl_list_length(&output_base->head_list) >= MAX_CLONED_CONNECTORS)
4395 parse_gbm_format(const char *s, uint32_t default_value, uint32_t *gbm_format)
4400 *gbm_format = default_value;
4401 else if (strcmp(s, "xrgb8888") == 0)
4402 *gbm_format = GBM_FORMAT_XRGB8888;
4403 else if (strcmp(s, "rgb565") == 0)
4404 *gbm_format = GBM_FORMAT_RGB565;
4405 else if (strcmp(s, "xrgb2101010") == 0)
4406 *gbm_format = GBM_FORMAT_XRGB2101010;
4408 weston_log("fatal: unrecognized pixel format: %s\n", s);
4415 /** Rewrite the output's mode list
4417 * @param output The output.
4418 * @return 0 on success, -1 on failure.
4420 * Destroy all existing modes in the list, and reconstruct a new list from
4421 * scratch, based on the currently attached heads.
4423 * On failure the output's mode list may contain some modes.
4426 drm_output_update_modelist_from_heads(struct drm_output *output)
4428 struct drm_backend *backend = to_drm_backend(output->base.compositor);
4429 struct weston_head *head_base;
4430 struct drm_head *head;
4431 struct drm_mode *mode;
4434 assert(!output->base.enabled);
4436 drm_mode_list_destroy(backend, &output->base.mode_list);
4438 /* XXX: needs a strategy for combining mode lists from multiple heads */
4439 head_base = weston_output_get_first_head(&output->base);
4441 head = to_drm_head(head_base);
4443 for (i = 0; i < head->connector->count_modes; i++) {
4444 mode = drm_output_add_mode(output, &head->connector->modes[i]);
4453 * Choose suitable mode for an output
4455 * Find the most suitable mode to use for initial setup (or reconfiguration on
4456 * hotplug etc) for a DRM output.
4458 * @param output DRM output to choose mode for
4459 * @param kind Strategy and preference to use when choosing mode
4460 * @param width Desired width for this output
4461 * @param height Desired height for this output
4462 * @param current_mode Mode currently being displayed on this output
4463 * @param modeline Manually-entered mode (may be NULL)
4464 * @returns A mode from the output's mode list, or NULL if none available
4466 static struct drm_mode *
4467 drm_output_choose_initial_mode(struct drm_backend *backend,
4468 struct drm_output *output,
4469 enum weston_drm_backend_output_mode mode,
4470 const char *modeline,
4471 const drmModeModeInfo *current_mode)
4473 struct drm_mode *preferred = NULL;
4474 struct drm_mode *current = NULL;
4475 struct drm_mode *configured = NULL;
4476 struct drm_mode *best = NULL;
4477 struct drm_mode *drm_mode;
4478 drmModeModeInfo drm_modeline;
4481 uint32_t refresh = 0;
4484 if (mode == WESTON_DRM_BACKEND_OUTPUT_PREFERRED && modeline) {
4485 n = sscanf(modeline, "%dx%d@%d", &width, &height, &refresh);
4486 if (n != 2 && n != 3) {
4489 if (parse_modeline(modeline, &drm_modeline) == 0) {
4490 configured = drm_output_add_mode(output, &drm_modeline);
4494 weston_log("Invalid modeline \"%s\" for output %s\n",
4495 modeline, output->base.name);
4500 wl_list_for_each_reverse(drm_mode, &output->base.mode_list, base.link) {
4501 if (width == drm_mode->base.width &&
4502 height == drm_mode->base.height &&
4503 (refresh == 0 || refresh == drm_mode->mode_info.vrefresh))
4504 configured = drm_mode;
4506 if (memcmp(current_mode, &drm_mode->mode_info,
4507 sizeof *current_mode) == 0)
4510 if (drm_mode->base.flags & WL_OUTPUT_MODE_PREFERRED)
4511 preferred = drm_mode;
4516 if (current == NULL && current_mode->clock != 0) {
4517 current = drm_output_add_mode(output, current_mode);
4522 if (mode == WESTON_DRM_BACKEND_OUTPUT_CURRENT)
4523 configured = current;
4537 weston_log("no available modes for %s\n", output->base.name);
4542 connector_get_current_mode(drmModeConnector *connector, int drm_fd,
4543 drmModeModeInfo *mode)
4545 drmModeEncoder *encoder;
4548 /* Get the current mode on the crtc that's currently driving
4549 * this connector. */
4550 encoder = drmModeGetEncoder(drm_fd, connector->encoder_id);
4551 memset(mode, 0, sizeof *mode);
4552 if (encoder != NULL) {
4553 crtc = drmModeGetCrtc(drm_fd, encoder->crtc_id);
4554 drmModeFreeEncoder(encoder);
4557 if (crtc->mode_valid)
4559 drmModeFreeCrtc(crtc);
4566 drm_output_set_mode(struct weston_output *base,
4567 enum weston_drm_backend_output_mode mode,
4568 const char *modeline)
4570 struct drm_output *output = to_drm_output(base);
4571 struct drm_backend *b = to_drm_backend(base->compositor);
4572 struct drm_head *head = to_drm_head(weston_output_get_first_head(base));
4574 struct drm_mode *current;
4576 if (drm_output_update_modelist_from_heads(output) < 0)
4579 current = drm_output_choose_initial_mode(b, output, mode, modeline,
4580 &head->inherited_mode);
4584 output->base.current_mode = ¤t->base;
4585 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
4587 /* Set native_ fields, so weston_output_mode_switch_to_native() works */
4588 output->base.native_mode = output->base.current_mode;
4589 output->base.native_scale = output->base.current_scale;
4595 drm_output_set_gbm_format(struct weston_output *base,
4596 const char *gbm_format)
4598 struct drm_output *output = to_drm_output(base);
4599 struct drm_backend *b = to_drm_backend(base->compositor);
4601 if (parse_gbm_format(gbm_format, b->gbm_format, &output->gbm_format) == -1)
4602 output->gbm_format = b->gbm_format;
4604 /* Without universal planes, we can't discover which formats are
4605 * supported by the primary plane; we just hope that the GBM format
4607 if (!b->universal_planes)
4608 output->scanout_plane->formats[0] = output->gbm_format;
4612 drm_output_set_seat(struct weston_output *base,
4615 struct drm_output *output = to_drm_output(base);
4616 struct drm_backend *b = to_drm_backend(base->compositor);
4618 setup_output_seat_constraint(b, &output->base,
4623 drm_output_init_gamma_size(struct drm_output *output)
4625 struct drm_backend *backend = to_drm_backend(output->base.compositor);
4628 assert(output->base.compositor);
4629 assert(output->crtc_id != 0);
4630 crtc = drmModeGetCrtc(backend->drm.fd, output->crtc_id);
4634 output->base.gamma_size = crtc->gamma_size;
4636 drmModeFreeCrtc(crtc);
4641 /** Allocate a CRTC for the output
4643 * @param output The output with no allocated CRTC.
4644 * @param resources DRM KMS resources.
4645 * @param connector The DRM KMS connector data.
4646 * @return 0 on success, -1 on failure.
4648 * Finds a free CRTC that can drive the given connector, reserves the CRTC
4649 * for the output, and loads the CRTC properties.
4651 * Populates the cursor and scanout planes.
4653 * On failure, the output remains without a CRTC.
4656 drm_output_init_crtc(struct drm_output *output,
4657 drmModeRes *resources, drmModeConnector *connector)
4659 struct drm_backend *b = to_drm_backend(output->base.compositor);
4660 drmModeObjectPropertiesPtr props;
4663 assert(output->crtc_id == 0);
4665 i = find_crtc_for_connector(b, resources, connector);
4667 weston_log("No usable crtc/encoder pair for connector.\n");
4671 output->crtc_id = resources->crtcs[i];
4674 props = drmModeObjectGetProperties(b->drm.fd, output->crtc_id,
4675 DRM_MODE_OBJECT_CRTC);
4677 weston_log("failed to get CRTC properties\n");
4680 drm_property_info_populate(b, crtc_props, output->props_crtc,
4681 WDRM_CRTC__COUNT, props);
4682 drmModeFreeObjectProperties(props);
4684 output->scanout_plane =
4685 drm_output_find_special_plane(b, output,
4686 WDRM_PLANE_TYPE_PRIMARY);
4687 if (!output->scanout_plane) {
4688 weston_log("Failed to find primary plane for output %s\n",
4693 /* Failing to find a cursor plane is not fatal, as we'll fall back
4694 * to software cursor. */
4695 output->cursor_plane =
4696 drm_output_find_special_plane(b, output,
4697 WDRM_PLANE_TYPE_CURSOR);
4699 wl_array_remove_uint32(&b->unused_crtcs, output->crtc_id);
4704 output->crtc_id = 0;
4710 /** Free the CRTC from the output
4712 * @param output The output whose CRTC to deallocate.
4714 * The CRTC reserved for the given output becomes free to use again.
4717 drm_output_fini_crtc(struct drm_output *output)
4719 struct drm_backend *b = to_drm_backend(output->base.compositor);
4722 if (!b->universal_planes && !b->shutting_down) {
4723 /* With universal planes, the 'special' planes are allocated at
4724 * startup, freed at shutdown, and live on the plane list in
4725 * between. We want the planes to continue to exist and be freed
4726 * up for other outputs.
4728 * Without universal planes, our special planes are
4729 * pseudo-planes allocated at output creation, freed at output
4730 * destruction, and not usable by other outputs.
4732 * On the other hand, if the compositor is already shutting down,
4733 * the plane has already been destroyed.
4735 if (output->cursor_plane)
4736 drm_plane_destroy(output->cursor_plane);
4737 if (output->scanout_plane)
4738 drm_plane_destroy(output->scanout_plane);
4741 drm_property_info_free(output->props_crtc, WDRM_CRTC__COUNT);
4743 assert(output->crtc_id != 0);
4745 unused = wl_array_add(&b->unused_crtcs, sizeof(*unused));
4746 *unused = output->crtc_id;
4748 /* Force resetting unused CRTCs */
4749 b->state_invalid = true;
4751 output->crtc_id = 0;
4752 output->cursor_plane = NULL;
4753 output->scanout_plane = NULL;
4757 drm_output_enable(struct weston_output *base)
4759 struct drm_output *output = to_drm_output(base);
4760 struct drm_backend *b = to_drm_backend(base->compositor);
4761 struct drm_head *head = to_drm_head(weston_output_get_first_head(base));
4762 struct weston_mode *m;
4763 drmModeRes *resources;
4766 resources = drmModeGetResources(b->drm.fd);
4768 weston_log("drmModeGetResources failed\n");
4771 ret = drm_output_init_crtc(output, resources, head->connector);
4772 drmModeFreeResources(resources);
4776 if (drm_output_init_gamma_size(output) < 0)
4779 if (b->pageflip_timeout)
4780 drm_output_pageflip_timer_create(output);
4782 if (b->use_pixman) {
4783 if (drm_output_init_pixman(output, b) < 0) {
4784 weston_log("Failed to init output pixman state\n");
4787 } else if (drm_output_init_egl(output, b) < 0) {
4788 weston_log("Failed to init output gl state\n");
4792 if (head->backlight) {
4793 weston_log("Initialized backlight, device %s\n",
4794 head->backlight->path);
4795 output->base.set_backlight = drm_set_backlight;
4796 output->base.backlight_current = drm_get_backlight(head);
4798 weston_log("Failed to initialize backlight\n");
4801 output->base.start_repaint_loop = drm_output_start_repaint_loop;
4802 output->base.repaint = drm_output_repaint;
4803 output->base.assign_planes = drm_assign_planes;
4804 output->base.set_dpms = drm_set_dpms;
4805 output->base.switch_mode = drm_output_switch_mode;
4806 output->base.set_gamma = drm_output_set_gamma;
4808 if (output->cursor_plane)
4809 weston_compositor_stack_plane(b->compositor,
4810 &output->cursor_plane->base,
4813 b->cursors_are_broken = 1;
4815 weston_compositor_stack_plane(b->compositor,
4816 &output->scanout_plane->base,
4817 &b->compositor->primary_plane);
4819 weston_log("Output %s, (connector %d, crtc %d)\n",
4820 output->base.name, head->connector_id, output->crtc_id);
4821 wl_list_for_each(m, &output->base.mode_list, link)
4822 weston_log_continue(STAMP_SPACE "mode %dx%d@%.1f%s%s%s\n",
4823 m->width, m->height, m->refresh / 1000.0,
4824 m->flags & WL_OUTPUT_MODE_PREFERRED ?
4826 m->flags & WL_OUTPUT_MODE_CURRENT ?
4828 head->connector->count_modes == 0 ?
4834 drm_output_fini_crtc(output);
4840 drm_output_deinit(struct weston_output *base)
4842 struct drm_output *output = to_drm_output(base);
4843 struct drm_backend *b = to_drm_backend(base->compositor);
4846 drm_output_fini_pixman(output);
4848 drm_output_fini_egl(output);
4850 /* Since our planes are no longer in use anywhere, remove their base
4851 * weston_plane's link from the plane stacking list, unless we're
4852 * shutting down, in which case the plane has already been
4854 if (!b->shutting_down) {
4855 wl_list_remove(&output->scanout_plane->base.link);
4856 wl_list_init(&output->scanout_plane->base.link);
4858 if (output->cursor_plane) {
4859 wl_list_remove(&output->cursor_plane->base.link);
4860 wl_list_init(&output->cursor_plane->base.link);
4861 /* Turn off hardware cursor */
4862 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
4866 drm_output_fini_crtc(output);
4870 drm_head_destroy(struct drm_head *head);
4873 drm_output_destroy(struct weston_output *base)
4875 struct drm_output *output = to_drm_output(base);
4876 struct drm_backend *b = to_drm_backend(base->compositor);
4878 if (output->page_flip_pending || output->vblank_pending ||
4879 output->atomic_complete_pending) {
4880 output->destroy_pending = 1;
4881 weston_log("destroy output while page flip pending\n");
4885 if (output->base.enabled)
4886 drm_output_deinit(&output->base);
4888 drm_mode_list_destroy(b, &output->base.mode_list);
4890 if (output->pageflip_timer)
4891 wl_event_source_remove(output->pageflip_timer);
4893 weston_output_release(&output->base);
4895 assert(!output->state_last);
4896 drm_output_state_free(output->state_cur);
4902 drm_output_disable(struct weston_output *base)
4904 struct drm_output *output = to_drm_output(base);
4906 if (output->page_flip_pending || output->vblank_pending ||
4907 output->atomic_complete_pending) {
4908 output->disable_pending = 1;
4912 weston_log("Disabling output %s\n", output->base.name);
4914 if (output->base.enabled)
4915 drm_output_deinit(&output->base);
4917 output->disable_pending = 0;
4923 * Update the list of unused connectors and CRTCs
4925 * This keeps the unused_crtc arrays up to date.
4927 * @param b Weston backend structure
4928 * @param resources DRM resources for this device
4931 drm_backend_update_unused_outputs(struct drm_backend *b, drmModeRes *resources)
4935 wl_array_release(&b->unused_crtcs);
4936 wl_array_init(&b->unused_crtcs);
4938 for (i = 0; i < resources->count_crtcs; i++) {
4939 struct drm_output *output;
4942 output = drm_output_find_by_crtc(b, resources->crtcs[i]);
4943 if (output && output->base.enabled)
4946 crtc_id = wl_array_add(&b->unused_crtcs, sizeof(*crtc_id));
4947 *crtc_id = resources->crtcs[i];
4951 /** Replace connector data and monitor information
4953 * @param head The head to update.
4954 * @param connector The connector data to be owned by the head, must match
4955 * the head's connector ID.
4956 * @return 0 on success, -1 on failure.
4958 * Takes ownership of @c connector on success, not on failure.
4960 * May schedule a heads changed call.
4963 drm_head_assign_connector_info(struct drm_head *head,
4964 drmModeConnector *connector)
4966 drmModeObjectProperties *props;
4967 const char *make = "unknown";
4968 const char *model = "unknown";
4969 const char *serial_number = "unknown";
4972 assert(head->connector_id == connector->connector_id);
4974 props = drmModeObjectGetProperties(head->backend->drm.fd,
4976 DRM_MODE_OBJECT_CONNECTOR);
4978 weston_log("Error: failed to get connector '%s' properties\n",
4983 if (head->connector)
4984 drmModeFreeConnector(head->connector);
4985 head->connector = connector;
4987 drm_property_info_populate(head->backend, connector_props,
4989 WDRM_CONNECTOR__COUNT, props);
4990 find_and_parse_output_edid(head, props, &make, &model, &serial_number);
4991 weston_head_set_monitor_strings(&head->base, make, model, serial_number);
4992 weston_head_set_subpixel(&head->base,
4993 drm_subpixel_to_wayland(head->connector->subpixel));
4995 weston_head_set_physical_size(&head->base, head->connector->mmWidth,
4996 head->connector->mmHeight);
4998 drmModeFreeObjectProperties(props);
5000 /* Unknown connection status is assumed disconnected. */
5001 weston_head_set_connection_status(&head->base,
5002 head->connector->connection == DRM_MODE_CONNECTED);
5007 /** Update connector and monitor information
5009 * @param head The head to update.
5011 * Re-reads the DRM property lists for the connector and updates monitor
5012 * information and connection status. This may schedule a heads changed call
5016 drm_head_update_info(struct drm_head *head)
5018 drmModeConnector *connector;
5020 connector = drmModeGetConnector(head->backend->drm.fd,
5021 head->connector_id);
5023 weston_log("DRM: getting connector info for '%s' failed.\n",
5028 if (drm_head_assign_connector_info(head, connector) < 0)
5029 drmModeFreeConnector(connector);
5033 * Create a Weston head for a connector
5035 * Given a DRM connector, create a matching drm_head structure and add it
5036 * to Weston's head list.
5038 * @param b Weston backend structure
5039 * @param connector_id DRM connector ID for the head
5040 * @param drm_device udev device pointer
5041 * @returns The new head, or NULL on failure.
5043 static struct drm_head *
5044 drm_head_create(struct drm_backend *backend, uint32_t connector_id,
5045 struct udev_device *drm_device)
5047 struct drm_head *head;
5048 drmModeConnector *connector;
5051 head = zalloc(sizeof *head);
5055 connector = drmModeGetConnector(backend->drm.fd, connector_id);
5059 name = make_connector_name(connector);
5063 weston_head_init(&head->base, name);
5066 head->connector_id = connector_id;
5067 head->backend = backend;
5069 head->backlight = backlight_init(drm_device, connector->connector_type);
5071 if (drm_head_assign_connector_info(head, connector) < 0)
5074 if (head->connector->connector_type == DRM_MODE_CONNECTOR_LVDS ||
5075 head->connector->connector_type == DRM_MODE_CONNECTOR_eDP)
5076 weston_head_set_internal(&head->base);
5078 if (connector_get_current_mode(head->connector, backend->drm.fd,
5079 &head->inherited_mode) < 0) {
5080 weston_log("Failed to retrieve current mode from connector %d.\n",
5081 head->connector_id);
5082 /* Continue, inherited_mode was memset to zero. */
5085 weston_compositor_add_head(backend->compositor, &head->base);
5087 weston_log("DRM: found head '%s', connector %d %s.\n",
5088 head->base.name, head->connector_id,
5089 head->base.connected ? "connected" : "disconnected");
5094 weston_head_release(&head->base);
5098 drmModeFreeConnector(connector);
5106 drm_head_destroy(struct drm_head *head)
5108 weston_head_release(&head->base);
5110 drm_property_info_free(head->props_conn, WDRM_CONNECTOR__COUNT);
5111 drmModeFreeConnector(head->connector);
5113 if (head->backlight)
5114 backlight_destroy(head->backlight);
5120 * Create a Weston output structure
5122 * Create an "empty" drm_output. This is the implementation of
5123 * weston_backend::create_output.
5125 * Creating an output is usually followed by drm_output_attach_head()
5126 * and drm_output_enable() to make use of it.
5128 * @param compositor The compositor instance.
5129 * @param name Name for the new output.
5130 * @returns The output, or NULL on failure.
5132 static struct weston_output *
5133 drm_output_create(struct weston_compositor *compositor, const char *name)
5135 struct drm_backend *b = to_drm_backend(compositor);
5136 struct drm_output *output;
5138 output = zalloc(sizeof *output);
5142 weston_output_init(&output->base, compositor, name);
5144 output->base.enable = drm_output_enable;
5145 output->base.destroy = drm_output_destroy;
5146 output->base.disable = drm_output_disable;
5147 output->base.attach_head = drm_output_attach_head;
5149 output->destroy_pending = 0;
5150 output->disable_pending = 0;
5152 output->state_cur = drm_output_state_alloc(output, NULL);
5154 weston_compositor_add_pending_output(&output->base, b->compositor);
5156 return &output->base;
5160 drm_backend_create_heads(struct drm_backend *b, struct udev_device *drm_device)
5162 struct drm_head *head;
5163 drmModeRes *resources;
5166 resources = drmModeGetResources(b->drm.fd);
5168 weston_log("drmModeGetResources failed\n");
5172 b->min_width = resources->min_width;
5173 b->max_width = resources->max_width;
5174 b->min_height = resources->min_height;
5175 b->max_height = resources->max_height;
5177 for (i = 0; i < resources->count_connectors; i++) {
5178 uint32_t connector_id = resources->connectors[i];
5180 head = drm_head_create(b, connector_id, drm_device);
5182 weston_log("DRM: failed to create head for connector %d.\n",
5187 drm_backend_update_unused_outputs(b, resources);
5189 drmModeFreeResources(resources);
5195 drm_backend_update_heads(struct drm_backend *b, struct udev_device *drm_device)
5197 drmModeRes *resources;
5198 struct weston_head *base, *next;
5199 struct drm_head *head;
5202 resources = drmModeGetResources(b->drm.fd);
5204 weston_log("drmModeGetResources failed\n");
5208 /* collect new connectors that have appeared, e.g. MST */
5209 for (i = 0; i < resources->count_connectors; i++) {
5210 uint32_t connector_id = resources->connectors[i];
5212 head = drm_head_find_by_connector(b, connector_id);
5214 drm_head_update_info(head);
5216 head = drm_head_create(b, connector_id, drm_device);
5218 weston_log("DRM: failed to create head for hot-added connector %d.\n",
5223 /* Remove connectors that have disappeared. */
5224 wl_list_for_each_safe(base, next,
5225 &b->compositor->head_list, compositor_link) {
5226 bool removed = true;
5228 head = to_drm_head(base);
5230 for (i = 0; i < resources->count_connectors; i++) {
5231 if (resources->connectors[i] == head->connector_id) {
5240 weston_log("DRM: head '%s' (connector %d) disappeared.\n",
5241 head->base.name, head->connector_id);
5242 drm_head_destroy(head);
5245 drm_backend_update_unused_outputs(b, resources);
5247 drmModeFreeResources(resources);
5251 udev_event_is_hotplug(struct drm_backend *b, struct udev_device *device)
5256 sysnum = udev_device_get_sysnum(device);
5257 if (!sysnum || atoi(sysnum) != b->drm.id)
5260 val = udev_device_get_property_value(device, "HOTPLUG");
5264 return strcmp(val, "1") == 0;
5268 udev_drm_event(int fd, uint32_t mask, void *data)
5270 struct drm_backend *b = data;
5271 struct udev_device *event;
5273 event = udev_monitor_receive_device(b->udev_monitor);
5275 if (udev_event_is_hotplug(b, event))
5276 drm_backend_update_heads(b, event);
5278 udev_device_unref(event);
5284 drm_destroy(struct weston_compositor *ec)
5286 struct drm_backend *b = to_drm_backend(ec);
5287 struct weston_head *base, *next;
5289 udev_input_destroy(&b->input);
5291 wl_event_source_remove(b->udev_drm_source);
5292 wl_event_source_remove(b->drm_source);
5294 b->shutting_down = true;
5298 weston_compositor_shutdown(ec);
5300 wl_list_for_each_safe(base, next, &ec->head_list, compositor_link)
5301 drm_head_destroy(to_drm_head(base));
5304 gbm_device_destroy(b->gbm);
5306 udev_monitor_unref(b->udev_monitor);
5307 udev_unref(b->udev);
5309 weston_launcher_destroy(ec->launcher);
5311 wl_array_release(&b->unused_crtcs);
5314 free(b->drm.filename);
5319 session_notify(struct wl_listener *listener, void *data)
5321 struct weston_compositor *compositor = data;
5322 struct drm_backend *b = to_drm_backend(compositor);
5323 struct drm_plane *plane;
5324 struct drm_output *output;
5326 if (compositor->session_active) {
5327 weston_log("activating session\n");
5328 weston_compositor_wake(compositor);
5329 weston_compositor_damage_all(compositor);
5330 b->state_invalid = true;
5331 udev_input_enable(&b->input);
5333 weston_log("deactivating session\n");
5334 udev_input_disable(&b->input);
5336 weston_compositor_offscreen(compositor);
5338 /* If we have a repaint scheduled (either from a
5339 * pending pageflip or the idle handler), make sure we
5340 * cancel that so we don't try to pageflip when we're
5341 * vt switched away. The OFFSCREEN state will prevent
5342 * further attempts at repainting. When we switch
5343 * back, we schedule a repaint, which will process
5344 * pending frame callbacks. */
5346 wl_list_for_each(output, &compositor->output_list, base.link) {
5347 output->base.repaint_needed = false;
5348 if (output->cursor_plane)
5349 drmModeSetCursor(b->drm.fd, output->crtc_id,
5353 output = container_of(compositor->output_list.next,
5354 struct drm_output, base.link);
5356 wl_list_for_each(plane, &b->plane_list, link) {
5357 if (plane->type != WDRM_PLANE_TYPE_OVERLAY)
5360 drmModeSetPlane(b->drm.fd,
5362 output->crtc_id, 0, 0,
5363 0, 0, 0, 0, 0, 0, 0, 0);
5369 * Determines whether or not a device is capable of modesetting. If successful,
5370 * sets b->drm.fd and b->drm.filename to the opened device.
5373 drm_device_is_kms(struct drm_backend *b, struct udev_device *device)
5375 const char *filename = udev_device_get_devnode(device);
5376 const char *sysnum = udev_device_get_sysnum(device);
5383 fd = weston_launcher_open(b->compositor->launcher, filename, O_RDWR);
5387 res = drmModeGetResources(fd);
5391 if (res->count_crtcs <= 0 || res->count_connectors <= 0 ||
5392 res->count_encoders <= 0)
5397 if (!sysnum || id < 0) {
5398 weston_log("couldn't get sysnum for device %s\n", filename);
5402 /* We can be called successfully on multiple devices; if we have,
5403 * clean up old entries. */
5405 weston_launcher_close(b->compositor->launcher, b->drm.fd);
5406 free(b->drm.filename);
5410 b->drm.filename = strdup(filename);
5412 drmModeFreeResources(res);
5417 drmModeFreeResources(res);
5419 weston_launcher_close(b->compositor->launcher, fd);
5425 * Some systems may have multiple DRM devices attached to a single seat. This
5426 * function loops over all devices and tries to find a PCI device with the
5427 * boot_vga sysfs attribute set to 1.
5428 * If no such device is found, the first DRM device reported by udev is used.
5429 * Devices are also vetted to make sure they are are capable of modesetting,
5430 * rather than pure render nodes (GPU with no display), or pure
5431 * memory-allocation devices (VGEM).
5433 static struct udev_device*
5434 find_primary_gpu(struct drm_backend *b, const char *seat)
5436 struct udev_enumerate *e;
5437 struct udev_list_entry *entry;
5438 const char *path, *device_seat, *id;
5439 struct udev_device *device, *drm_device, *pci;
5441 e = udev_enumerate_new(b->udev);
5442 udev_enumerate_add_match_subsystem(e, "drm");
5443 udev_enumerate_add_match_sysname(e, "card[0-9]*");
5445 udev_enumerate_scan_devices(e);
5447 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
5448 bool is_boot_vga = false;
5450 path = udev_list_entry_get_name(entry);
5451 device = udev_device_new_from_syspath(b->udev, path);
5454 device_seat = udev_device_get_property_value(device, "ID_SEAT");
5456 device_seat = default_seat;
5457 if (strcmp(device_seat, seat)) {
5458 udev_device_unref(device);
5462 pci = udev_device_get_parent_with_subsystem_devtype(device,
5465 id = udev_device_get_sysattr_value(pci, "boot_vga");
5466 if (id && !strcmp(id, "1"))
5470 /* If we already have a modesetting-capable device, and this
5471 * device isn't our boot-VGA device, we aren't going to use
5473 if (!is_boot_vga && drm_device) {
5474 udev_device_unref(device);
5478 /* Make sure this device is actually capable of modesetting;
5479 * if this call succeeds, b->drm.{fd,filename} will be set,
5480 * and any old values freed. */
5481 if (!drm_device_is_kms(b, device)) {
5482 udev_device_unref(device);
5486 /* There can only be one boot_vga device, and we try to use it
5490 udev_device_unref(drm_device);
5491 drm_device = device;
5495 /* Per the (!is_boot_vga && drm_device) test above, we only
5496 * trump existing saved devices with boot-VGA devices, so if
5497 * we end up here, this must be the first device we've seen. */
5498 assert(!drm_device);
5499 drm_device = device;
5502 /* If we're returning a device to use, we must have an open FD for
5504 assert(!!drm_device == (b->drm.fd >= 0));
5506 udev_enumerate_unref(e);
5510 static struct udev_device *
5511 open_specific_drm_device(struct drm_backend *b, const char *name)
5513 struct udev_device *device;
5515 device = udev_device_new_from_subsystem_sysname(b->udev, "drm", name);
5517 weston_log("ERROR: could not open DRM device '%s'\n", name);
5521 if (!drm_device_is_kms(b, device)) {
5522 udev_device_unref(device);
5523 weston_log("ERROR: DRM device '%s' is not a KMS device.\n", name);
5527 /* If we're returning a device to use, we must have an open FD for
5529 assert(b->drm.fd >= 0);
5535 planes_binding(struct weston_keyboard *keyboard, const struct timespec *time,
5536 uint32_t key, void *data)
5538 struct drm_backend *b = data;
5542 b->cursors_are_broken ^= 1;
5545 b->sprites_are_broken ^= 1;
5548 b->sprites_hidden ^= 1;
5555 #ifdef BUILD_VAAPI_RECORDER
5557 recorder_destroy(struct drm_output *output)
5559 vaapi_recorder_destroy(output->recorder);
5560 output->recorder = NULL;
5562 output->base.disable_planes--;
5564 wl_list_remove(&output->recorder_frame_listener.link);
5565 weston_log("[libva recorder] done\n");
5569 recorder_frame_notify(struct wl_listener *listener, void *data)
5571 struct drm_output *output;
5572 struct drm_backend *b;
5575 output = container_of(listener, struct drm_output,
5576 recorder_frame_listener);
5577 b = to_drm_backend(output->base.compositor);
5579 if (!output->recorder)
5582 ret = drmPrimeHandleToFD(b->drm.fd,
5583 output->scanout_plane->state_cur->fb->handle,
5586 weston_log("[libva recorder] "
5587 "failed to create prime fd for front buffer\n");
5591 ret = vaapi_recorder_frame(output->recorder, fd,
5592 output->scanout_plane->state_cur->fb->stride);
5594 weston_log("[libva recorder] aborted: %m\n");
5595 recorder_destroy(output);
5600 create_recorder(struct drm_backend *b, int width, int height,
5601 const char *filename)
5606 fd = open(b->drm.filename, O_RDWR | O_CLOEXEC);
5610 drmGetMagic(fd, &magic);
5611 drmAuthMagic(b->drm.fd, magic);
5613 return vaapi_recorder_create(fd, width, height, filename);
5617 recorder_binding(struct weston_keyboard *keyboard, const struct timespec *time,
5618 uint32_t key, void *data)
5620 struct drm_backend *b = data;
5621 struct drm_output *output;
5624 output = container_of(b->compositor->output_list.next,
5625 struct drm_output, base.link);
5627 if (!output->recorder) {
5628 if (output->gbm_format != GBM_FORMAT_XRGB8888) {
5629 weston_log("failed to start vaapi recorder: "
5630 "output format not supported\n");
5634 width = output->base.current_mode->width;
5635 height = output->base.current_mode->height;
5638 create_recorder(b, width, height, "capture.h264");
5639 if (!output->recorder) {
5640 weston_log("failed to create vaapi recorder\n");
5644 output->base.disable_planes++;
5646 output->recorder_frame_listener.notify = recorder_frame_notify;
5647 wl_signal_add(&output->base.frame_signal,
5648 &output->recorder_frame_listener);
5650 weston_output_schedule_repaint(&output->base);
5652 weston_log("[libva recorder] initialized\n");
5654 recorder_destroy(output);
5659 recorder_binding(struct weston_keyboard *keyboard, const struct timespec *time,
5660 uint32_t key, void *data)
5662 weston_log("Compiled without libva support\n");
5667 switch_to_gl_renderer(struct drm_backend *b)
5669 struct drm_output *output;
5670 bool dmabuf_support_inited;
5675 dmabuf_support_inited = !!b->compositor->renderer->import_dmabuf;
5677 weston_log("Switching to GL renderer\n");
5679 b->gbm = create_gbm_device(b->drm.fd);
5681 weston_log("Failed to create gbm device. "
5682 "Aborting renderer switch\n");
5686 wl_list_for_each(output, &b->compositor->output_list, base.link)
5687 pixman_renderer_output_destroy(&output->base);
5689 b->compositor->renderer->destroy(b->compositor);
5691 if (drm_backend_create_gl_renderer(b) < 0) {
5692 gbm_device_destroy(b->gbm);
5693 weston_log("Failed to create GL renderer. Quitting.\n");
5694 /* FIXME: we need a function to shutdown cleanly */
5698 wl_list_for_each(output, &b->compositor->output_list, base.link)
5699 drm_output_init_egl(output, b);
5703 if (!dmabuf_support_inited && b->compositor->renderer->import_dmabuf) {
5704 if (linux_dmabuf_setup(b->compositor) < 0)
5705 weston_log("Error: initializing dmabuf "
5706 "support failed.\n");
5711 renderer_switch_binding(struct weston_keyboard *keyboard,
5712 const struct timespec *time, uint32_t key, void *data)
5714 struct drm_backend *b =
5715 to_drm_backend(keyboard->seat->compositor);
5717 switch_to_gl_renderer(b);
5720 static const struct weston_drm_output_api api = {
5721 drm_output_set_mode,
5722 drm_output_set_gbm_format,
5723 drm_output_set_seat,
5726 static struct drm_backend *
5727 drm_backend_create(struct weston_compositor *compositor,
5728 struct weston_drm_backend_config *config)
5730 struct drm_backend *b;
5731 struct udev_device *drm_device;
5732 struct wl_event_loop *loop;
5733 const char *seat_id = default_seat;
5736 weston_log("initializing drm backend\n");
5738 b = zalloc(sizeof *b);
5742 b->state_invalid = true;
5744 wl_array_init(&b->unused_crtcs);
5747 * KMS support for hardware planes cannot properly synchronize
5748 * without nuclear page flip. Without nuclear/atomic, hw plane
5749 * and cursor plane updates would either tear or cause extra
5750 * waits for vblanks which means dropping the compositor framerate
5751 * to a fraction. For cursors, it's not so bad, so they are
5754 * These can be enabled again when nuclear/atomic support lands.
5756 b->sprites_are_broken = 1;
5757 b->compositor = compositor;
5758 b->use_pixman = config->use_pixman;
5759 b->pageflip_timeout = config->pageflip_timeout;
5761 compositor->backend = &b->base;
5763 if (parse_gbm_format(config->gbm_format, GBM_FORMAT_XRGB8888, &b->gbm_format) < 0)
5764 goto err_compositor;
5766 if (config->seat_id)
5767 seat_id = config->seat_id;
5769 /* Check if we run drm-backend using weston-launch */
5770 compositor->launcher = weston_launcher_connect(compositor, config->tty,
5772 if (compositor->launcher == NULL) {
5773 weston_log("fatal: drm backend should be run using "
5774 "weston-launch binary, or your system should "
5775 "provide the logind D-Bus API.\n");
5776 goto err_compositor;
5779 b->udev = udev_new();
5780 if (b->udev == NULL) {
5781 weston_log("failed to initialize udev context\n");
5785 b->session_listener.notify = session_notify;
5786 wl_signal_add(&compositor->session_signal, &b->session_listener);
5788 if (config->specific_device)
5789 drm_device = open_specific_drm_device(b, config->specific_device);
5791 drm_device = find_primary_gpu(b, seat_id);
5792 if (drm_device == NULL) {
5793 weston_log("no drm device found\n");
5797 if (init_kms_caps(b) < 0) {
5798 weston_log("failed to initialize kms\n");
5802 if (b->use_pixman) {
5803 if (init_pixman(b) < 0) {
5804 weston_log("failed to initialize pixman renderer\n");
5808 if (init_egl(b) < 0) {
5809 weston_log("failed to initialize egl\n");
5814 b->base.destroy = drm_destroy;
5815 b->base.repaint_begin = drm_repaint_begin;
5816 b->base.repaint_flush = drm_repaint_flush;
5817 b->base.repaint_cancel = drm_repaint_cancel;
5818 b->base.create_output = drm_output_create;
5820 weston_setup_vt_switch_bindings(compositor);
5822 wl_list_init(&b->plane_list);
5825 if (udev_input_init(&b->input,
5826 compositor, b->udev, seat_id,
5827 config->configure_device) < 0) {
5828 weston_log("failed to create input devices\n");
5832 if (drm_backend_create_heads(b, drm_device) < 0) {
5833 weston_log("Failed to create heads for %s\n", b->drm.filename);
5834 goto err_udev_input;
5837 /* A this point we have some idea of whether or not we have a working
5839 if (!b->cursors_are_broken)
5840 compositor->capabilities |= WESTON_CAP_CURSOR_PLANE;
5842 loop = wl_display_get_event_loop(compositor->wl_display);
5844 wl_event_loop_add_fd(loop, b->drm.fd,
5845 WL_EVENT_READABLE, on_drm_input, b);
5847 b->udev_monitor = udev_monitor_new_from_netlink(b->udev, "udev");
5848 if (b->udev_monitor == NULL) {
5849 weston_log("failed to initialize udev monitor\n");
5850 goto err_drm_source;
5852 udev_monitor_filter_add_match_subsystem_devtype(b->udev_monitor,
5854 b->udev_drm_source =
5855 wl_event_loop_add_fd(loop,
5856 udev_monitor_get_fd(b->udev_monitor),
5857 WL_EVENT_READABLE, udev_drm_event, b);
5859 if (udev_monitor_enable_receiving(b->udev_monitor) < 0) {
5860 weston_log("failed to enable udev-monitor receiving\n");
5861 goto err_udev_monitor;
5864 udev_device_unref(drm_device);
5866 weston_compositor_add_debug_binding(compositor, KEY_O,
5868 weston_compositor_add_debug_binding(compositor, KEY_C,
5870 weston_compositor_add_debug_binding(compositor, KEY_V,
5872 weston_compositor_add_debug_binding(compositor, KEY_Q,
5873 recorder_binding, b);
5874 weston_compositor_add_debug_binding(compositor, KEY_W,
5875 renderer_switch_binding, b);
5877 if (compositor->renderer->import_dmabuf) {
5878 if (linux_dmabuf_setup(compositor) < 0)
5879 weston_log("Error: initializing dmabuf "
5880 "support failed.\n");
5883 ret = weston_plugin_api_register(compositor, WESTON_DRM_OUTPUT_API_NAME,
5887 weston_log("Failed to register output API.\n");
5888 goto err_udev_monitor;
5894 wl_event_source_remove(b->udev_drm_source);
5895 udev_monitor_unref(b->udev_monitor);
5897 wl_event_source_remove(b->drm_source);
5899 udev_input_destroy(&b->input);
5902 gbm_device_destroy(b->gbm);
5905 udev_device_unref(drm_device);
5907 weston_launcher_destroy(compositor->launcher);
5909 udev_unref(b->udev);
5911 weston_compositor_shutdown(compositor);
5917 config_init_to_defaults(struct weston_drm_backend_config *config)
5922 weston_backend_init(struct weston_compositor *compositor,
5923 struct weston_backend_config *config_base)
5925 struct drm_backend *b;
5926 struct weston_drm_backend_config config = {{ 0, }};
5928 if (config_base == NULL ||
5929 config_base->struct_version != WESTON_DRM_BACKEND_CONFIG_VERSION ||
5930 config_base->struct_size > sizeof(struct weston_drm_backend_config)) {
5931 weston_log("drm backend config structure is invalid\n");
5935 config_init_to_defaults(&config);
5936 memcpy(&config, config_base, config_base->struct_size);
5938 b = drm_backend_create(compositor, &config);