Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1 | /* |
Kristian Høgsberg | 96aa7da | 2011-09-15 15:43:14 -0400 | [diff] [blame] | 2 | * Copyright © 2008-2011 Kristian Høgsberg |
| 3 | * Copyright © 2011 Intel Corporation |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 4 | * |
Bryce Harrington | a0bbfea | 2015-06-11 15:35:43 -0700 | [diff] [blame] | 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: |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 12 | * |
Bryce Harrington | a0bbfea | 2015-06-11 15:35:43 -0700 | [diff] [blame] | 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. |
| 16 | * |
| 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 |
| 24 | * SOFTWARE. |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 25 | */ |
| 26 | |
Daniel Stone | c228e23 | 2013-05-22 18:03:19 +0300 | [diff] [blame] | 27 | #include "config.h" |
Kristian Høgsberg | 0b9334a | 2011-04-12 11:34:32 -0400 | [diff] [blame] | 28 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 29 | #include <errno.h> |
Jussi Kukkonen | 649bbce | 2016-07-19 14:16:27 +0300 | [diff] [blame] | 30 | #include <stdint.h> |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 31 | #include <stdlib.h> |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 32 | #include <ctype.h> |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 33 | #include <string.h> |
| 34 | #include <fcntl.h> |
| 35 | #include <unistd.h> |
Kristian Høgsberg | 5d1c0c5 | 2012-04-10 00:11:50 -0400 | [diff] [blame] | 36 | #include <linux/input.h> |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 37 | #include <linux/vt.h> |
Ander Conselvan de Oliveira | fd1f4c6 | 2012-06-26 17:09:14 +0300 | [diff] [blame] | 38 | #include <assert.h> |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 39 | #include <sys/mman.h> |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 40 | #include <dlfcn.h> |
Ander Conselvan de Oliveira | 95eb3a2 | 2013-05-07 14:16:59 +0300 | [diff] [blame] | 41 | #include <time.h> |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 42 | |
Benjamin Franzke | c649a92 | 2011-03-02 11:56:04 +0100 | [diff] [blame] | 43 | #include <xf86drm.h> |
| 44 | #include <xf86drmMode.h> |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 45 | #include <drm_fourcc.h> |
Benjamin Franzke | c649a92 | 2011-03-02 11:56:04 +0100 | [diff] [blame] | 46 | |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 47 | #include <gbm.h> |
Pekka Paalanen | 3315697 | 2012-08-03 13:30:30 -0400 | [diff] [blame] | 48 | #include <libudev.h> |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 49 | |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 50 | #include "compositor.h" |
| 51 | #include "compositor-drm.h" |
Jon Cruz | 35b2eaa | 2015-06-15 15:37:08 -0700 | [diff] [blame] | 52 | #include "shared/helpers.h" |
Mario Kleiner | f507ec3 | 2015-06-21 21:25:14 +0200 | [diff] [blame] | 53 | #include "shared/timespec-util.h" |
John Kåre Alsaker | 30d2b1f | 2012-11-13 19:10:28 +0100 | [diff] [blame] | 54 | #include "gl-renderer.h" |
Vincent Abriou | c950667 | 2016-10-05 16:14:07 +0200 | [diff] [blame] | 55 | #include "weston-egl-ext.h" |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 56 | #include "pixman-renderer.h" |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 57 | #include "libbacklight.h" |
Peter Hutterer | 823ad33 | 2014-11-26 07:06:31 +1000 | [diff] [blame] | 58 | #include "libinput-seat.h" |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 59 | #include "launcher-util.h" |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 60 | #include "vaapi-recorder.h" |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 61 | #include "presentation-time-server-protocol.h" |
Pekka Paalanen | e4d231e | 2014-06-12 15:12:48 +0300 | [diff] [blame] | 62 | #include "linux-dmabuf.h" |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 63 | |
Ander Conselvan de Oliveira | 95eb3a2 | 2013-05-07 14:16:59 +0300 | [diff] [blame] | 64 | #ifndef DRM_CAP_TIMESTAMP_MONOTONIC |
| 65 | #define DRM_CAP_TIMESTAMP_MONOTONIC 0x6 |
| 66 | #endif |
| 67 | |
Alvaro Fernando García | dce7c6e | 2014-07-28 18:30:17 -0300 | [diff] [blame] | 68 | #ifndef DRM_CAP_CURSOR_WIDTH |
| 69 | #define DRM_CAP_CURSOR_WIDTH 0x8 |
| 70 | #endif |
| 71 | |
| 72 | #ifndef DRM_CAP_CURSOR_HEIGHT |
| 73 | #define DRM_CAP_CURSOR_HEIGHT 0x9 |
| 74 | #endif |
| 75 | |
| 76 | #ifndef GBM_BO_USE_CURSOR |
| 77 | #define GBM_BO_USE_CURSOR GBM_BO_USE_CURSOR_64X64 |
| 78 | #endif |
| 79 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 80 | struct drm_backend { |
| 81 | struct weston_backend base; |
| 82 | struct weston_compositor *compositor; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 83 | |
| 84 | struct udev *udev; |
| 85 | struct wl_event_source *drm_source; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 86 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 87 | struct udev_monitor *udev_monitor; |
| 88 | struct wl_event_source *udev_drm_source; |
| 89 | |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 90 | struct { |
David Herrmann | d7488c2 | 2012-03-11 20:05:21 +0100 | [diff] [blame] | 91 | int id; |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 92 | int fd; |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 93 | char *filename; |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 94 | } drm; |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 95 | struct gbm_device *gbm; |
Marty Jack | 13d9db2 | 2011-02-09 19:01:42 -0500 | [diff] [blame] | 96 | uint32_t crtc_allocator; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 97 | uint32_t connector_allocator; |
Kristian Høgsberg | 61741a2 | 2013-09-17 16:02:57 -0700 | [diff] [blame] | 98 | struct wl_listener session_listener; |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 99 | uint32_t gbm_format; |
Pekka Paalanen | bce2d3f | 2011-12-02 13:07:27 +0200 | [diff] [blame] | 100 | |
Rob Clark | 4339add | 2012-08-09 14:18:28 -0500 | [diff] [blame] | 101 | /* we need these parameters in order to not fail drmModeAddFB2() |
| 102 | * due to out of bounds dimensions, and then mistakenly set |
| 103 | * sprites_are_broken: |
| 104 | */ |
Daniel Stone | f214fdc | 2016-11-14 17:43:57 +0000 | [diff] [blame] | 105 | int min_width, max_width; |
| 106 | int min_height, max_height; |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 107 | int no_addfb2; |
Rob Clark | 4339add | 2012-08-09 14:18:28 -0500 | [diff] [blame] | 108 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 109 | struct wl_list sprite_list; |
Kristian Høgsberg | 65bec24 | 2012-03-05 19:57:35 -0500 | [diff] [blame] | 110 | int sprites_are_broken; |
Ander Conselvan de Oliveira | da1c908 | 2012-10-31 17:55:46 +0200 | [diff] [blame] | 111 | int sprites_hidden; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 112 | |
Rob Clark | ab5b1e3 | 2012-08-09 13:24:45 -0500 | [diff] [blame] | 113 | int cursors_are_broken; |
| 114 | |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 115 | int use_pixman; |
| 116 | |
Rob Bradford | d355b80 | 2013-05-31 18:09:55 +0100 | [diff] [blame] | 117 | struct udev_input input; |
Alvaro Fernando García | dce7c6e | 2014-07-28 18:30:17 -0300 | [diff] [blame] | 118 | |
Daniel Stone | 70d337d | 2015-06-16 18:42:23 +0100 | [diff] [blame] | 119 | int32_t cursor_width; |
| 120 | int32_t cursor_height; |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 121 | |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 122 | bool use_current_mode; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 123 | }; |
| 124 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 125 | struct drm_mode { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 126 | struct weston_mode base; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 127 | drmModeModeInfo mode_info; |
| 128 | }; |
| 129 | |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 130 | struct drm_fb { |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 131 | uint32_t fb_id, stride, handle, size; |
Daniel Stone | c8c917c | 2016-11-14 17:45:58 +0000 | [diff] [blame] | 132 | int width, height; |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 133 | int fd; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 134 | int is_client_buffer; |
Pekka Paalanen | de685b8 | 2012-12-04 15:58:12 +0200 | [diff] [blame] | 135 | struct weston_buffer_reference buffer_ref; |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 136 | |
| 137 | /* Used by gbm fbs */ |
| 138 | struct gbm_bo *bo; |
| 139 | |
| 140 | /* Used by dumb fbs */ |
| 141 | void *map; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 142 | }; |
| 143 | |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 144 | struct drm_edid { |
| 145 | char eisa_id[13]; |
| 146 | char monitor_name[13]; |
| 147 | char pnp_id[5]; |
| 148 | char serial_number[13]; |
| 149 | }; |
| 150 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 151 | struct drm_output { |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 152 | struct weston_output base; |
| 153 | drmModeConnector *connector; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 154 | |
Daniel Stone | 1733923 | 2015-11-28 12:09:47 +0000 | [diff] [blame] | 155 | uint32_t crtc_id; /* object ID to pass to DRM functions */ |
| 156 | int pipe; /* index of CRTC in resource array / bitmasks */ |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 157 | uint32_t connector_id; |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 158 | drmModeCrtcPtr original_crtc; |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 159 | struct drm_edid edid; |
Ander Conselvan de Oliveira | a0a433a | 2013-06-04 16:24:04 +0300 | [diff] [blame] | 160 | drmModePropertyPtr dpms_prop; |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 161 | uint32_t gbm_format; |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 162 | |
Daniel Stone | 36609c7 | 2015-06-18 07:49:02 +0100 | [diff] [blame] | 163 | enum dpms_enum dpms; |
| 164 | |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 165 | int vblank_pending; |
| 166 | int page_flip_pending; |
Xiong Zhang | abd5d47 | 2013-10-11 14:43:07 +0800 | [diff] [blame] | 167 | int destroy_pending; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 168 | int disable_pending; |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 169 | |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 170 | struct gbm_surface *gbm_surface; |
| 171 | struct gbm_bo *gbm_cursor_bo[2]; |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 172 | struct weston_plane cursor_plane; |
| 173 | struct weston_plane fb_plane; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 174 | struct weston_view *cursor_view; |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 175 | int current_cursor; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 176 | struct drm_fb *current, *next; |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 177 | struct backlight *backlight; |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 178 | |
| 179 | struct drm_fb *dumb[2]; |
| 180 | pixman_image_t *image[2]; |
| 181 | int current_image; |
| 182 | pixman_region32_t previous_damage; |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 183 | |
| 184 | struct vaapi_recorder *recorder; |
| 185 | struct wl_listener recorder_frame_listener; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 186 | }; |
| 187 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 188 | /* |
| 189 | * An output has a primary display plane plus zero or more sprites for |
| 190 | * blending display contents. |
| 191 | */ |
| 192 | struct drm_sprite { |
| 193 | struct wl_list link; |
| 194 | |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 195 | struct weston_plane plane; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 196 | |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 197 | struct drm_fb *current, *next; |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 198 | struct drm_output *output; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 199 | struct drm_backend *backend; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 200 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 201 | uint32_t possible_crtcs; |
| 202 | uint32_t plane_id; |
| 203 | uint32_t count_formats; |
| 204 | |
| 205 | int32_t src_x, src_y; |
| 206 | uint32_t src_w, src_h; |
| 207 | uint32_t dest_x, dest_y; |
| 208 | uint32_t dest_w, dest_h; |
| 209 | |
| 210 | uint32_t formats[]; |
| 211 | }; |
| 212 | |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 213 | static struct gl_renderer_interface *gl_renderer; |
| 214 | |
Kristian Høgsberg | 98cfea6 | 2013-02-18 16:15:53 -0500 | [diff] [blame] | 215 | static const char default_seat[] = "seat0"; |
Pekka Paalanen | 3315697 | 2012-08-03 13:30:30 -0400 | [diff] [blame] | 216 | |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 217 | static inline struct drm_output * |
| 218 | to_drm_output(struct weston_output *base) |
| 219 | { |
| 220 | return container_of(base, struct drm_output, base); |
| 221 | } |
| 222 | |
| 223 | static inline struct drm_backend * |
| 224 | to_drm_backend(struct weston_compositor *base) |
| 225 | { |
| 226 | return container_of(base->backend, struct drm_backend, base); |
| 227 | } |
| 228 | |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 229 | static void |
| 230 | drm_output_set_cursor(struct drm_output *output); |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 231 | |
Mario Kleiner | f507ec3 | 2015-06-21 21:25:14 +0200 | [diff] [blame] | 232 | static void |
| 233 | drm_output_update_msc(struct drm_output *output, unsigned int seq); |
| 234 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 235 | static int |
Daniel Stone | a3ae476 | 2015-11-28 12:07:36 +0000 | [diff] [blame] | 236 | drm_sprite_crtc_supported(struct drm_output *output, struct drm_sprite *sprite) |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 237 | { |
Daniel Stone | a3ae476 | 2015-11-28 12:07:36 +0000 | [diff] [blame] | 238 | return !!(sprite->possible_crtcs & (1 << output->pipe)); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 239 | } |
| 240 | |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 241 | static void |
| 242 | drm_fb_destroy_callback(struct gbm_bo *bo, void *data) |
| 243 | { |
| 244 | struct drm_fb *fb = data; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 245 | |
| 246 | if (fb->fb_id) |
Daniel Stone | 4e5eceb | 2015-11-30 16:07:19 +0000 | [diff] [blame] | 247 | drmModeRmFB(fb->fd, fb->fb_id); |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 248 | |
Pekka Paalanen | de685b8 | 2012-12-04 15:58:12 +0200 | [diff] [blame] | 249 | weston_buffer_reference(&fb->buffer_ref, NULL); |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 250 | |
| 251 | free(data); |
| 252 | } |
| 253 | |
| 254 | static struct drm_fb * |
Daniel Stone | f214fdc | 2016-11-14 17:43:57 +0000 | [diff] [blame] | 255 | drm_fb_create_dumb(struct drm_backend *b, int width, int height, |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 256 | uint32_t format) |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 257 | { |
| 258 | struct drm_fb *fb; |
| 259 | int ret; |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 260 | uint32_t bpp, depth; |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 261 | |
| 262 | struct drm_mode_create_dumb create_arg; |
| 263 | struct drm_mode_destroy_dumb destroy_arg; |
| 264 | struct drm_mode_map_dumb map_arg; |
| 265 | |
Peter Hutterer | f3d6227 | 2013-08-08 11:57:05 +1000 | [diff] [blame] | 266 | fb = zalloc(sizeof *fb); |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 267 | if (!fb) |
| 268 | return NULL; |
| 269 | |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 270 | switch (format) { |
| 271 | case GBM_FORMAT_XRGB8888: |
| 272 | bpp = 32; |
| 273 | depth = 24; |
| 274 | break; |
| 275 | case GBM_FORMAT_RGB565: |
| 276 | bpp = depth = 16; |
| 277 | break; |
| 278 | default: |
| 279 | return NULL; |
| 280 | } |
| 281 | |
Kristian Høgsberg | ac6104e | 2013-08-21 22:14:14 -0700 | [diff] [blame] | 282 | memset(&create_arg, 0, sizeof create_arg); |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 283 | create_arg.bpp = bpp; |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 284 | create_arg.width = width; |
| 285 | create_arg.height = height; |
| 286 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 287 | ret = drmIoctl(b->drm.fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_arg); |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 288 | if (ret) |
| 289 | goto err_fb; |
| 290 | |
| 291 | fb->handle = create_arg.handle; |
| 292 | fb->stride = create_arg.pitch; |
| 293 | fb->size = create_arg.size; |
Daniel Stone | c8c917c | 2016-11-14 17:45:58 +0000 | [diff] [blame] | 294 | fb->width = width; |
| 295 | fb->height = height; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 296 | fb->fd = b->drm.fd; |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 297 | |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 298 | ret = -1; |
| 299 | |
| 300 | if (!b->no_addfb2) { |
Yong Bakos | 4b6321f | 2016-08-17 17:37:55 -0700 | [diff] [blame] | 301 | uint32_t handles[4] = { 0 }, pitches[4] = { 0 }, offsets[4] = { 0 }; |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 302 | |
| 303 | handles[0] = fb->handle; |
| 304 | pitches[0] = fb->stride; |
| 305 | offsets[0] = 0; |
| 306 | |
| 307 | ret = drmModeAddFB2(b->drm.fd, width, height, |
| 308 | format, handles, pitches, offsets, |
| 309 | &fb->fb_id, 0); |
| 310 | if (ret) { |
| 311 | weston_log("addfb2 failed: %m\n"); |
| 312 | b->no_addfb2 = 1; |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | if (ret) { |
| 317 | ret = drmModeAddFB(b->drm.fd, width, height, depth, bpp, |
| 318 | fb->stride, fb->handle, &fb->fb_id); |
| 319 | } |
| 320 | |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 321 | if (ret) |
| 322 | goto err_bo; |
| 323 | |
Kristian Høgsberg | ac6104e | 2013-08-21 22:14:14 -0700 | [diff] [blame] | 324 | memset(&map_arg, 0, sizeof map_arg); |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 325 | map_arg.handle = fb->handle; |
Chris Michael | eb2074a | 2013-05-01 21:26:02 -0400 | [diff] [blame] | 326 | ret = drmIoctl(fb->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_arg); |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 327 | if (ret) |
| 328 | goto err_add_fb; |
| 329 | |
Chris Michael | 4a7ce1f | 2015-11-10 10:40:37 -0500 | [diff] [blame] | 330 | fb->map = mmap(NULL, fb->size, PROT_WRITE, |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 331 | MAP_SHARED, b->drm.fd, map_arg.offset); |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 332 | if (fb->map == MAP_FAILED) |
| 333 | goto err_add_fb; |
| 334 | |
| 335 | return fb; |
| 336 | |
| 337 | err_add_fb: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 338 | drmModeRmFB(b->drm.fd, fb->fb_id); |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 339 | err_bo: |
| 340 | memset(&destroy_arg, 0, sizeof(destroy_arg)); |
| 341 | destroy_arg.handle = create_arg.handle; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 342 | drmIoctl(b->drm.fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg); |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 343 | err_fb: |
| 344 | free(fb); |
| 345 | return NULL; |
| 346 | } |
| 347 | |
| 348 | static void |
| 349 | drm_fb_destroy_dumb(struct drm_fb *fb) |
| 350 | { |
| 351 | struct drm_mode_destroy_dumb destroy_arg; |
| 352 | |
| 353 | if (!fb->map) |
| 354 | return; |
| 355 | |
| 356 | if (fb->fb_id) |
| 357 | drmModeRmFB(fb->fd, fb->fb_id); |
| 358 | |
| 359 | weston_buffer_reference(&fb->buffer_ref, NULL); |
| 360 | |
| 361 | munmap(fb->map, fb->size); |
| 362 | |
| 363 | memset(&destroy_arg, 0, sizeof(destroy_arg)); |
| 364 | destroy_arg.handle = fb->handle; |
| 365 | drmIoctl(fb->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg); |
| 366 | |
| 367 | free(fb); |
| 368 | } |
| 369 | |
| 370 | static struct drm_fb * |
Kristian Høgsberg | a2f84cc | 2012-12-07 12:37:58 -0500 | [diff] [blame] | 371 | drm_fb_get_from_bo(struct gbm_bo *bo, |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 372 | struct drm_backend *backend, uint32_t format) |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 373 | { |
| 374 | struct drm_fb *fb = gbm_bo_get_user_data(bo); |
Derek Foreman | 482ffdf | 2016-07-08 12:50:57 -0500 | [diff] [blame] | 375 | uint32_t handles[4] = { 0 }, pitches[4] = { 0 }, offsets[4] = { 0 }; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 376 | int ret; |
| 377 | |
| 378 | if (fb) |
| 379 | return fb; |
| 380 | |
Bryce Harrington | de16d89 | 2014-11-20 22:21:57 -0800 | [diff] [blame] | 381 | fb = zalloc(sizeof *fb); |
| 382 | if (fb == NULL) |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 383 | return NULL; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 384 | |
| 385 | fb->bo = bo; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 386 | |
Daniel Stone | c8c917c | 2016-11-14 17:45:58 +0000 | [diff] [blame] | 387 | fb->width = gbm_bo_get_width(bo); |
| 388 | fb->height = gbm_bo_get_height(bo); |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 389 | fb->stride = gbm_bo_get_stride(bo); |
| 390 | fb->handle = gbm_bo_get_handle(bo).u32; |
Daniel Stone | c8c917c | 2016-11-14 17:45:58 +0000 | [diff] [blame] | 391 | fb->size = fb->stride * fb->height; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 392 | fb->fd = backend->drm.fd; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 393 | |
Daniel Stone | c8c917c | 2016-11-14 17:45:58 +0000 | [diff] [blame] | 394 | if (backend->min_width > fb->width || |
| 395 | fb->width > backend->max_width || |
| 396 | backend->min_height > fb->height || |
| 397 | fb->height > backend->max_height) { |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 398 | weston_log("bo geometry out of bounds\n"); |
| 399 | goto err_free; |
| 400 | } |
| 401 | |
| 402 | ret = -1; |
| 403 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 404 | if (format && !backend->no_addfb2) { |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 405 | handles[0] = fb->handle; |
| 406 | pitches[0] = fb->stride; |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 407 | offsets[0] = 0; |
| 408 | |
Daniel Stone | c8c917c | 2016-11-14 17:45:58 +0000 | [diff] [blame] | 409 | ret = drmModeAddFB2(backend->drm.fd, fb->width, fb->height, |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 410 | format, handles, pitches, offsets, |
| 411 | &fb->fb_id, 0); |
| 412 | if (ret) { |
| 413 | weston_log("addfb2 failed: %m\n"); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 414 | backend->no_addfb2 = 1; |
| 415 | backend->sprites_are_broken = 1; |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 416 | } |
| 417 | } |
| 418 | |
| 419 | if (ret) |
Daniel Stone | c8c917c | 2016-11-14 17:45:58 +0000 | [diff] [blame] | 420 | ret = drmModeAddFB(backend->drm.fd, fb->width, fb->height, |
| 421 | 24, 32, fb->stride, fb->handle, &fb->fb_id); |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 422 | |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 423 | if (ret) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 424 | weston_log("failed to create kms fb: %m\n"); |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 425 | goto err_free; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | gbm_bo_set_user_data(bo, fb, drm_fb_destroy_callback); |
| 429 | |
| 430 | return fb; |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 431 | |
| 432 | err_free: |
| 433 | free(fb); |
| 434 | return NULL; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | static void |
Jason Ekstrand | 6bd6294 | 2013-06-20 20:38:23 -0500 | [diff] [blame] | 438 | drm_fb_set_buffer(struct drm_fb *fb, struct weston_buffer *buffer) |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 439 | { |
Pekka Paalanen | de685b8 | 2012-12-04 15:58:12 +0200 | [diff] [blame] | 440 | assert(fb->buffer_ref.buffer == NULL); |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 441 | |
| 442 | fb->is_client_buffer = 1; |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 443 | |
Pekka Paalanen | de685b8 | 2012-12-04 15:58:12 +0200 | [diff] [blame] | 444 | weston_buffer_reference(&fb->buffer_ref, buffer); |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 445 | } |
| 446 | |
Ander Conselvan de Oliveira | 526d461 | 2013-01-25 15:13:03 +0200 | [diff] [blame] | 447 | static void |
| 448 | drm_output_release_fb(struct drm_output *output, struct drm_fb *fb) |
| 449 | { |
| 450 | if (!fb) |
| 451 | return; |
| 452 | |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 453 | if (fb->map && |
| 454 | (fb != output->dumb[0] && fb != output->dumb[1])) { |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 455 | drm_fb_destroy_dumb(fb); |
| 456 | } else if (fb->bo) { |
Ander Conselvan de Oliveira | 526d461 | 2013-01-25 15:13:03 +0200 | [diff] [blame] | 457 | if (fb->is_client_buffer) |
| 458 | gbm_bo_destroy(fb->bo); |
| 459 | else |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 460 | gbm_surface_release_buffer(output->gbm_surface, |
Jason Ekstrand | 3ec57f5 | 2013-11-14 20:52:35 -0600 | [diff] [blame] | 461 | fb->bo); |
Ander Conselvan de Oliveira | 526d461 | 2013-01-25 15:13:03 +0200 | [diff] [blame] | 462 | } |
| 463 | } |
| 464 | |
Kristian Høgsberg | a2f84cc | 2012-12-07 12:37:58 -0500 | [diff] [blame] | 465 | static uint32_t |
Ander Conselvan de Oliveira | e920941 | 2012-11-30 17:34:22 +0200 | [diff] [blame] | 466 | drm_output_check_scanout_format(struct drm_output *output, |
| 467 | struct weston_surface *es, struct gbm_bo *bo) |
| 468 | { |
Ander Conselvan de Oliveira | e920941 | 2012-11-30 17:34:22 +0200 | [diff] [blame] | 469 | uint32_t format; |
| 470 | pixman_region32_t r; |
| 471 | |
| 472 | format = gbm_bo_get_format(bo); |
| 473 | |
Kristian Høgsberg | 3f97b34 | 2013-10-16 16:08:57 -0700 | [diff] [blame] | 474 | if (format == GBM_FORMAT_ARGB8888) { |
| 475 | /* We can scanout an ARGB buffer if the surface's |
| 476 | * opaque region covers the whole output, but we have |
| 477 | * to use XRGB as the KMS format code. */ |
Kristian Høgsberg | 1be87e3 | 2014-01-17 14:22:41 -0800 | [diff] [blame] | 478 | pixman_region32_init_rect(&r, 0, 0, |
| 479 | output->base.width, |
| 480 | output->base.height); |
| 481 | pixman_region32_subtract(&r, &r, &es->opaque); |
Ander Conselvan de Oliveira | e920941 | 2012-11-30 17:34:22 +0200 | [diff] [blame] | 482 | |
| 483 | if (!pixman_region32_not_empty(&r)) |
Kristian Høgsberg | a2f84cc | 2012-12-07 12:37:58 -0500 | [diff] [blame] | 484 | format = GBM_FORMAT_XRGB8888; |
Ander Conselvan de Oliveira | e920941 | 2012-11-30 17:34:22 +0200 | [diff] [blame] | 485 | |
| 486 | pixman_region32_fini(&r); |
Kristian Høgsberg | a2f84cc | 2012-12-07 12:37:58 -0500 | [diff] [blame] | 487 | } |
Kristian Høgsberg | 3f97b34 | 2013-10-16 16:08:57 -0700 | [diff] [blame] | 488 | |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 489 | if (output->gbm_format == format) |
Kristian Høgsberg | 3f97b34 | 2013-10-16 16:08:57 -0700 | [diff] [blame] | 490 | return format; |
| 491 | |
| 492 | return 0; |
Ander Conselvan de Oliveira | e920941 | 2012-11-30 17:34:22 +0200 | [diff] [blame] | 493 | } |
| 494 | |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 495 | static struct weston_plane * |
Pekka Paalanen | 050c1ba | 2014-12-17 16:20:38 +0200 | [diff] [blame] | 496 | drm_output_prepare_scanout_view(struct drm_output *output, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 497 | struct weston_view *ev) |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 498 | { |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 499 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 500 | struct weston_buffer *buffer = ev->surface->buffer_ref.buffer; |
Pekka Paalanen | 952b6c8 | 2014-03-14 14:38:15 +0200 | [diff] [blame] | 501 | struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 502 | struct gbm_bo *bo; |
Kristian Høgsberg | a2f84cc | 2012-12-07 12:37:58 -0500 | [diff] [blame] | 503 | uint32_t format; |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 504 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 505 | if (ev->geometry.x != output->base.x || |
| 506 | ev->geometry.y != output->base.y || |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 507 | buffer == NULL || b->gbm == NULL || |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 508 | buffer->width != output->base.current_mode->width || |
| 509 | buffer->height != output->base.current_mode->height || |
Pekka Paalanen | 952b6c8 | 2014-03-14 14:38:15 +0200 | [diff] [blame] | 510 | output->base.transform != viewport->buffer.transform || |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 511 | ev->transform.enabled) |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 512 | return NULL; |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 513 | |
Pekka Paalanen | 5580f22 | 2015-02-17 16:33:18 +0200 | [diff] [blame] | 514 | if (ev->geometry.scissor_enabled) |
| 515 | return NULL; |
| 516 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 517 | bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER, |
Kristian Høgsberg | 6399646 | 2013-09-03 22:27:08 -0700 | [diff] [blame] | 518 | buffer->resource, GBM_BO_USE_SCANOUT); |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 519 | |
Rob Bradford | 9b10187 | 2012-09-14 23:25:41 +0100 | [diff] [blame] | 520 | /* Unable to use the buffer for scanout */ |
| 521 | if (!bo) |
| 522 | return NULL; |
| 523 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 524 | format = drm_output_check_scanout_format(output, ev->surface, bo); |
Kristian Høgsberg | a2f84cc | 2012-12-07 12:37:58 -0500 | [diff] [blame] | 525 | if (format == 0) { |
Ander Conselvan de Oliveira | a64b15d | 2012-05-02 16:42:22 +0300 | [diff] [blame] | 526 | gbm_bo_destroy(bo); |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 527 | return NULL; |
Ander Conselvan de Oliveira | a64b15d | 2012-05-02 16:42:22 +0300 | [diff] [blame] | 528 | } |
| 529 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 530 | output->next = drm_fb_get_from_bo(bo, b, format); |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 531 | if (!output->next) { |
| 532 | gbm_bo_destroy(bo); |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 533 | return NULL; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 534 | } |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 535 | |
Pekka Paalanen | de685b8 | 2012-12-04 15:58:12 +0200 | [diff] [blame] | 536 | drm_fb_set_buffer(output->next, buffer); |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 537 | |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 538 | return &output->fb_plane; |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 539 | } |
| 540 | |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 541 | static void |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 542 | drm_output_render_gl(struct drm_output *output, pixman_region32_t *damage) |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 543 | { |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 544 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 545 | struct gbm_bo *bo; |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 546 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 547 | output->base.compositor->renderer->repaint_output(&output->base, |
| 548 | damage); |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 549 | |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 550 | bo = gbm_surface_lock_front_buffer(output->gbm_surface); |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 551 | if (!bo) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 552 | weston_log("failed to lock front buffer: %m\n"); |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 553 | return; |
| 554 | } |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 555 | |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 556 | output->next = drm_fb_get_from_bo(bo, b, output->gbm_format); |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 557 | if (!output->next) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 558 | weston_log("failed to get drm_fb for bo\n"); |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 559 | gbm_surface_release_buffer(output->gbm_surface, bo); |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 560 | return; |
| 561 | } |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | static void |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 565 | drm_output_render_pixman(struct drm_output *output, pixman_region32_t *damage) |
| 566 | { |
| 567 | struct weston_compositor *ec = output->base.compositor; |
| 568 | pixman_region32_t total_damage, previous_damage; |
| 569 | |
| 570 | pixman_region32_init(&total_damage); |
| 571 | pixman_region32_init(&previous_damage); |
| 572 | |
| 573 | pixman_region32_copy(&previous_damage, damage); |
| 574 | |
| 575 | pixman_region32_union(&total_damage, damage, &output->previous_damage); |
| 576 | pixman_region32_copy(&output->previous_damage, &previous_damage); |
| 577 | |
| 578 | output->current_image ^= 1; |
| 579 | |
| 580 | output->next = output->dumb[output->current_image]; |
| 581 | pixman_renderer_output_set_buffer(&output->base, |
| 582 | output->image[output->current_image]); |
| 583 | |
| 584 | ec->renderer->repaint_output(&output->base, &total_damage); |
| 585 | |
| 586 | pixman_region32_fini(&total_damage); |
| 587 | pixman_region32_fini(&previous_damage); |
| 588 | } |
| 589 | |
| 590 | static void |
| 591 | drm_output_render(struct drm_output *output, pixman_region32_t *damage) |
| 592 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 593 | struct weston_compositor *c = output->base.compositor; |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 594 | struct drm_backend *b = to_drm_backend(c); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 595 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 596 | if (b->use_pixman) |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 597 | drm_output_render_pixman(output, damage); |
| 598 | else |
| 599 | drm_output_render_gl(output, damage); |
| 600 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 601 | pixman_region32_subtract(&c->primary_plane.damage, |
| 602 | &c->primary_plane.damage, damage); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | static void |
Richard Hughes | e729996 | 2013-05-01 21:52:12 +0100 | [diff] [blame] | 606 | drm_output_set_gamma(struct weston_output *output_base, |
| 607 | uint16_t size, uint16_t *r, uint16_t *g, uint16_t *b) |
| 608 | { |
| 609 | int rc; |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 610 | struct drm_output *output = to_drm_output(output_base); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 611 | struct drm_backend *backend = |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 612 | to_drm_backend(output->base.compositor); |
Richard Hughes | e729996 | 2013-05-01 21:52:12 +0100 | [diff] [blame] | 613 | |
| 614 | /* check */ |
| 615 | if (output_base->gamma_size != size) |
| 616 | return; |
| 617 | if (!output->original_crtc) |
| 618 | return; |
| 619 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 620 | rc = drmModeCrtcSetGamma(backend->drm.fd, |
Richard Hughes | e729996 | 2013-05-01 21:52:12 +0100 | [diff] [blame] | 621 | output->crtc_id, |
| 622 | size, r, g, b); |
| 623 | if (rc) |
| 624 | weston_log("set gamma failed: %m\n"); |
| 625 | } |
| 626 | |
Bryce Harrington | ada4f07 | 2015-06-30 13:25:46 -0700 | [diff] [blame] | 627 | /* Determine the type of vblank synchronization to use for the output. |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 628 | * |
Bryce Harrington | ada4f07 | 2015-06-30 13:25:46 -0700 | [diff] [blame] | 629 | * The pipe parameter indicates which CRTC is in use. Knowing this, we |
| 630 | * can determine which vblank sequence type to use for it. Traditional |
| 631 | * cards had only two CRTCs, with CRTC 0 using no special flags, and |
| 632 | * CRTC 1 using DRM_VBLANK_SECONDARY. The first bit of the pipe |
| 633 | * parameter indicates this. |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 634 | * |
Bryce Harrington | ada4f07 | 2015-06-30 13:25:46 -0700 | [diff] [blame] | 635 | * Bits 1-5 of the pipe parameter are 5 bit wide pipe number between |
| 636 | * 0-31. If this is non-zero it indicates we're dealing with a |
| 637 | * multi-gpu situation and we need to calculate the vblank sync |
| 638 | * using DRM_BLANK_HIGH_CRTC_MASK. |
| 639 | */ |
Pekka Paalanen | c8a1ff0 | 2015-07-02 15:06:08 +0300 | [diff] [blame] | 640 | static unsigned int |
| 641 | drm_waitvblank_pipe(struct drm_output *output) |
Mario Kleiner | 2ab4f4e | 2015-06-21 21:25:13 +0200 | [diff] [blame] | 642 | { |
| 643 | if (output->pipe > 1) |
| 644 | return (output->pipe << DRM_VBLANK_HIGH_CRTC_SHIFT) & |
| 645 | DRM_VBLANK_HIGH_CRTC_MASK; |
| 646 | else if (output->pipe > 0) |
| 647 | return DRM_VBLANK_SECONDARY; |
| 648 | else |
| 649 | return 0; |
| 650 | } |
| 651 | |
David Herrmann | 1edf44c | 2013-10-22 17:11:26 +0200 | [diff] [blame] | 652 | static int |
Kristian Høgsberg | 6ddcdae | 2012-02-28 22:31:58 -0500 | [diff] [blame] | 653 | drm_output_repaint(struct weston_output *output_base, |
Kristian Høgsberg | d7c1726 | 2012-09-05 21:54:15 -0400 | [diff] [blame] | 654 | pixman_region32_t *damage) |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 655 | { |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 656 | struct drm_output *output = to_drm_output(output_base); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 657 | struct drm_backend *backend = |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 658 | to_drm_backend(output->base.compositor); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 659 | struct drm_sprite *s; |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 660 | struct drm_mode *mode; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 661 | int ret = 0; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 662 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 663 | if (output->disable_pending || output->destroy_pending) |
David Herrmann | 1edf44c | 2013-10-22 17:11:26 +0200 | [diff] [blame] | 664 | return -1; |
Xiong Zhang | abd5d47 | 2013-10-11 14:43:07 +0800 | [diff] [blame] | 665 | |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 666 | if (!output->next) |
Kristian Høgsberg | d7c1726 | 2012-09-05 21:54:15 -0400 | [diff] [blame] | 667 | drm_output_render(output, damage); |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 668 | if (!output->next) |
David Herrmann | 1edf44c | 2013-10-22 17:11:26 +0200 | [diff] [blame] | 669 | return -1; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 670 | |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 671 | mode = container_of(output->base.current_mode, struct drm_mode, base); |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 672 | if (!output->current || |
| 673 | output->current->stride != output->next->stride) { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 674 | ret = drmModeSetCrtc(backend->drm.fd, output->crtc_id, |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 675 | output->next->fb_id, 0, 0, |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 676 | &output->connector_id, 1, |
| 677 | &mode->mode_info); |
| 678 | if (ret) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 679 | weston_log("set mode failed: %m\n"); |
David Herrmann | 1edf44c | 2013-10-22 17:11:26 +0200 | [diff] [blame] | 680 | goto err_pageflip; |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 681 | } |
Ander Conselvan de Oliveira | 722a2d5 | 2013-06-04 16:24:05 +0300 | [diff] [blame] | 682 | output_base->set_dpms(output_base, WESTON_DPMS_ON); |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 683 | } |
| 684 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 685 | if (drmModePageFlip(backend->drm.fd, output->crtc_id, |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 686 | output->next->fb_id, |
Kristian Høgsberg | 54f14c3 | 2012-01-18 11:47:41 -0500 | [diff] [blame] | 687 | DRM_MODE_PAGE_FLIP_EVENT, output) < 0) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 688 | weston_log("queueing pageflip failed: %m\n"); |
David Herrmann | 1edf44c | 2013-10-22 17:11:26 +0200 | [diff] [blame] | 689 | goto err_pageflip; |
Kristian Høgsberg | 54f14c3 | 2012-01-18 11:47:41 -0500 | [diff] [blame] | 690 | } |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 691 | |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 692 | output->page_flip_pending = 1; |
| 693 | |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 694 | drm_output_set_cursor(output); |
| 695 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 696 | /* |
| 697 | * Now, update all the sprite surfaces |
| 698 | */ |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 699 | wl_list_for_each(s, &backend->sprite_list, link) { |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 700 | uint32_t flags = 0, fb_id = 0; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 701 | drmVBlank vbl = { |
| 702 | .request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT, |
| 703 | .request.sequence = 1, |
| 704 | }; |
| 705 | |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 706 | if ((!s->current && !s->next) || |
Daniel Stone | a3ae476 | 2015-11-28 12:07:36 +0000 | [diff] [blame] | 707 | !drm_sprite_crtc_supported(output, s)) |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 708 | continue; |
| 709 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 710 | if (s->next && !backend->sprites_hidden) |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 711 | fb_id = s->next->fb_id; |
| 712 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 713 | ret = drmModeSetPlane(backend->drm.fd, s->plane_id, |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 714 | output->crtc_id, fb_id, flags, |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 715 | s->dest_x, s->dest_y, |
| 716 | s->dest_w, s->dest_h, |
| 717 | s->src_x, s->src_y, |
| 718 | s->src_w, s->src_h); |
| 719 | if (ret) |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 720 | weston_log("setplane failed: %d: %s\n", |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 721 | ret, strerror(errno)); |
| 722 | |
Mario Kleiner | 2ab4f4e | 2015-06-21 21:25:13 +0200 | [diff] [blame] | 723 | vbl.request.type |= drm_waitvblank_pipe(output); |
Rob Clark | 5ca1a47 | 2012-08-08 20:27:37 -0500 | [diff] [blame] | 724 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 725 | /* |
| 726 | * Queue a vblank signal so we know when the surface |
| 727 | * becomes active on the display or has been replaced. |
| 728 | */ |
| 729 | vbl.request.signal = (unsigned long)s; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 730 | ret = drmWaitVBlank(backend->drm.fd, &vbl); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 731 | if (ret) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 732 | weston_log("vblank event request failed: %d: %s\n", |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 733 | ret, strerror(errno)); |
| 734 | } |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 735 | |
| 736 | s->output = output; |
| 737 | output->vblank_pending = 1; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 738 | } |
| 739 | |
David Herrmann | 1edf44c | 2013-10-22 17:11:26 +0200 | [diff] [blame] | 740 | return 0; |
| 741 | |
| 742 | err_pageflip: |
Kristian Høgsberg | b3955b0 | 2014-01-23 16:25:06 -0800 | [diff] [blame] | 743 | output->cursor_view = NULL; |
David Herrmann | 1edf44c | 2013-10-22 17:11:26 +0200 | [diff] [blame] | 744 | if (output->next) { |
| 745 | drm_output_release_fb(output, output->next); |
| 746 | output->next = NULL; |
| 747 | } |
| 748 | |
| 749 | return -1; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | static void |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 753 | drm_output_start_repaint_loop(struct weston_output *output_base) |
| 754 | { |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 755 | struct drm_output *output = to_drm_output(output_base); |
| 756 | struct drm_backend *backend = |
| 757 | to_drm_backend(output_base->compositor); |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 758 | uint32_t fb_id; |
Mario Kleiner | f507ec3 | 2015-06-21 21:25:14 +0200 | [diff] [blame] | 759 | struct timespec ts, tnow; |
| 760 | struct timespec vbl2now; |
| 761 | int64_t refresh_nsec; |
| 762 | int ret; |
| 763 | drmVBlank vbl = { |
| 764 | .request.type = DRM_VBLANK_RELATIVE, |
| 765 | .request.sequence = 0, |
| 766 | .request.signal = 0, |
| 767 | }; |
Ander Conselvan de Oliveira | 95eb3a2 | 2013-05-07 14:16:59 +0300 | [diff] [blame] | 768 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 769 | if (output->disable_pending || output->destroy_pending) |
Xiong Zhang | abd5d47 | 2013-10-11 14:43:07 +0800 | [diff] [blame] | 770 | return; |
| 771 | |
Ander Conselvan de Oliveira | 95eb3a2 | 2013-05-07 14:16:59 +0300 | [diff] [blame] | 772 | if (!output->current) { |
| 773 | /* We can't page flip if there's no mode set */ |
David Herrmann | 3c688c5 | 2013-10-22 17:11:25 +0200 | [diff] [blame] | 774 | goto finish_frame; |
Ander Conselvan de Oliveira | 95eb3a2 | 2013-05-07 14:16:59 +0300 | [diff] [blame] | 775 | } |
| 776 | |
Mario Kleiner | f507ec3 | 2015-06-21 21:25:14 +0200 | [diff] [blame] | 777 | /* Try to get current msc and timestamp via instant query */ |
| 778 | vbl.request.type |= drm_waitvblank_pipe(output); |
| 779 | ret = drmWaitVBlank(backend->drm.fd, &vbl); |
| 780 | |
| 781 | /* Error ret or zero timestamp means failure to get valid timestamp */ |
| 782 | if ((ret == 0) && (vbl.reply.tval_sec > 0 || vbl.reply.tval_usec > 0)) { |
| 783 | ts.tv_sec = vbl.reply.tval_sec; |
| 784 | ts.tv_nsec = vbl.reply.tval_usec * 1000; |
| 785 | |
| 786 | /* Valid timestamp for most recent vblank - not stale? |
| 787 | * Stale ts could happen on Linux 3.17+, so make sure it |
| 788 | * is not older than 1 refresh duration since now. |
| 789 | */ |
| 790 | weston_compositor_read_presentation_clock(backend->compositor, |
| 791 | &tnow); |
| 792 | timespec_sub(&vbl2now, &tnow, &ts); |
| 793 | refresh_nsec = |
| 794 | millihz_to_nsec(output->base.current_mode->refresh); |
| 795 | if (timespec_to_nsec(&vbl2now) < refresh_nsec) { |
| 796 | drm_output_update_msc(output, vbl.reply.sequence); |
| 797 | weston_output_finish_frame(output_base, &ts, |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 798 | WP_PRESENTATION_FEEDBACK_INVALID); |
Mario Kleiner | f507ec3 | 2015-06-21 21:25:14 +0200 | [diff] [blame] | 799 | return; |
| 800 | } |
| 801 | } |
| 802 | |
| 803 | /* Immediate query didn't provide valid timestamp. |
| 804 | * Use pageflip fallback. |
| 805 | */ |
Ander Conselvan de Oliveira | 95eb3a2 | 2013-05-07 14:16:59 +0300 | [diff] [blame] | 806 | fb_id = output->current->fb_id; |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 807 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 808 | if (drmModePageFlip(backend->drm.fd, output->crtc_id, fb_id, |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 809 | DRM_MODE_PAGE_FLIP_EVENT, output) < 0) { |
| 810 | weston_log("queueing pageflip failed: %m\n"); |
David Herrmann | 3c688c5 | 2013-10-22 17:11:25 +0200 | [diff] [blame] | 811 | goto finish_frame; |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 812 | } |
David Herrmann | 3c688c5 | 2013-10-22 17:11:25 +0200 | [diff] [blame] | 813 | |
| 814 | return; |
| 815 | |
| 816 | finish_frame: |
| 817 | /* if we cannot page-flip, immediately finish frame */ |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 818 | weston_compositor_read_presentation_clock(output_base->compositor, &ts); |
Pekka Paalanen | 363aa7b | 2014-12-17 16:20:40 +0200 | [diff] [blame] | 819 | weston_output_finish_frame(output_base, &ts, |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 820 | WP_PRESENTATION_FEEDBACK_INVALID); |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | static void |
Pekka Paalanen | 641307c | 2014-09-23 22:08:47 -0400 | [diff] [blame] | 824 | drm_output_update_msc(struct drm_output *output, unsigned int seq) |
| 825 | { |
| 826 | uint64_t msc_hi = output->base.msc >> 32; |
| 827 | |
| 828 | if (seq < (output->base.msc & 0xffffffff)) |
| 829 | msc_hi++; |
| 830 | |
| 831 | output->base.msc = (msc_hi << 32) + seq; |
| 832 | } |
| 833 | |
| 834 | static void |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 835 | vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec, |
| 836 | void *data) |
| 837 | { |
| 838 | struct drm_sprite *s = (struct drm_sprite *)data; |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 839 | struct drm_output *output = s->output; |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame] | 840 | struct timespec ts; |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 841 | uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION | |
| 842 | WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK; |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 843 | |
Pekka Paalanen | 641307c | 2014-09-23 22:08:47 -0400 | [diff] [blame] | 844 | drm_output_update_msc(output, frame); |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 845 | output->vblank_pending = 0; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 846 | |
Ander Conselvan de Oliveira | 526d461 | 2013-01-25 15:13:03 +0200 | [diff] [blame] | 847 | drm_output_release_fb(output, s->current); |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 848 | s->current = s->next; |
| 849 | s->next = NULL; |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 850 | |
| 851 | if (!output->page_flip_pending) { |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame] | 852 | ts.tv_sec = sec; |
| 853 | ts.tv_nsec = usec * 1000; |
Pekka Paalanen | 363aa7b | 2014-12-17 16:20:40 +0200 | [diff] [blame] | 854 | weston_output_finish_frame(&output->base, &ts, flags); |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 855 | } |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | static void |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 859 | drm_output_destroy(struct weston_output *base); |
Xiong Zhang | abd5d47 | 2013-10-11 14:43:07 +0800 | [diff] [blame] | 860 | |
| 861 | static void |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 862 | page_flip_handler(int fd, unsigned int frame, |
| 863 | unsigned int sec, unsigned int usec, void *data) |
| 864 | { |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 865 | struct drm_output *output = data; |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame] | 866 | struct timespec ts; |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 867 | uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_VSYNC | |
| 868 | WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION | |
| 869 | WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 870 | |
Pekka Paalanen | 641307c | 2014-09-23 22:08:47 -0400 | [diff] [blame] | 871 | drm_output_update_msc(output, frame); |
| 872 | |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 873 | /* We don't set page_flip_pending on start_repaint_loop, in that case |
| 874 | * we just want to page flip to the current buffer to get an accurate |
| 875 | * timestamp */ |
| 876 | if (output->page_flip_pending) { |
| 877 | drm_output_release_fb(output, output->current); |
| 878 | output->current = output->next; |
| 879 | output->next = NULL; |
| 880 | } |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 881 | |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 882 | output->page_flip_pending = 0; |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 883 | |
Xiong Zhang | abd5d47 | 2013-10-11 14:43:07 +0800 | [diff] [blame] | 884 | if (output->destroy_pending) |
| 885 | drm_output_destroy(&output->base); |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 886 | else if (output->disable_pending) |
| 887 | weston_output_disable(&output->base); |
Xiong Zhang | abd5d47 | 2013-10-11 14:43:07 +0800 | [diff] [blame] | 888 | else if (!output->vblank_pending) { |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame] | 889 | ts.tv_sec = sec; |
| 890 | ts.tv_nsec = usec * 1000; |
Pekka Paalanen | 363aa7b | 2014-12-17 16:20:40 +0200 | [diff] [blame] | 891 | weston_output_finish_frame(&output->base, &ts, flags); |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 892 | |
| 893 | /* We can't call this from frame_notify, because the output's |
| 894 | * repaint needed flag is cleared just after that */ |
| 895 | if (output->recorder) |
| 896 | weston_output_schedule_repaint(&output->base); |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 897 | } |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 898 | } |
| 899 | |
Kristian Høgsberg | a2f84cc | 2012-12-07 12:37:58 -0500 | [diff] [blame] | 900 | static uint32_t |
| 901 | drm_output_check_sprite_format(struct drm_sprite *s, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 902 | struct weston_view *ev, struct gbm_bo *bo) |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 903 | { |
Kristian Høgsberg | a2f84cc | 2012-12-07 12:37:58 -0500 | [diff] [blame] | 904 | uint32_t i, format; |
| 905 | |
| 906 | format = gbm_bo_get_format(bo); |
| 907 | |
| 908 | if (format == GBM_FORMAT_ARGB8888) { |
| 909 | pixman_region32_t r; |
| 910 | |
Kristian Høgsberg | 63093a3 | 2013-03-01 14:29:16 -0500 | [diff] [blame] | 911 | pixman_region32_init_rect(&r, 0, 0, |
Jason Ekstrand | 918f2dd | 2013-12-02 21:01:53 -0600 | [diff] [blame] | 912 | ev->surface->width, |
| 913 | ev->surface->height); |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 914 | pixman_region32_subtract(&r, &r, &ev->surface->opaque); |
Kristian Høgsberg | a2f84cc | 2012-12-07 12:37:58 -0500 | [diff] [blame] | 915 | |
| 916 | if (!pixman_region32_not_empty(&r)) |
| 917 | format = GBM_FORMAT_XRGB8888; |
| 918 | |
| 919 | pixman_region32_fini(&r); |
| 920 | } |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 921 | |
| 922 | for (i = 0; i < s->count_formats; i++) |
| 923 | if (s->formats[i] == format) |
Kristian Høgsberg | a2f84cc | 2012-12-07 12:37:58 -0500 | [diff] [blame] | 924 | return format; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 925 | |
| 926 | return 0; |
| 927 | } |
| 928 | |
| 929 | static int |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 930 | drm_view_transform_supported(struct weston_view *ev) |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 931 | { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 932 | return !ev->transform.enabled || |
| 933 | (ev->transform.matrix.type < WESTON_MATRIX_TRANSFORM_ROTATE); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 934 | } |
| 935 | |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 936 | static struct weston_plane * |
Pekka Paalanen | 050c1ba | 2014-12-17 16:20:38 +0200 | [diff] [blame] | 937 | drm_output_prepare_overlay_view(struct drm_output *output, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 938 | struct weston_view *ev) |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 939 | { |
Pekka Paalanen | 050c1ba | 2014-12-17 16:20:38 +0200 | [diff] [blame] | 940 | struct weston_compositor *ec = output->base.compositor; |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 941 | struct drm_backend *b = to_drm_backend(ec); |
Pekka Paalanen | 952b6c8 | 2014-03-14 14:38:15 +0200 | [diff] [blame] | 942 | struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport; |
Pekka Paalanen | bf8cc6f | 2014-06-12 17:12:12 +0300 | [diff] [blame] | 943 | struct wl_resource *buffer_resource; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 944 | struct drm_sprite *s; |
Pekka Paalanen | bf8cc6f | 2014-06-12 17:12:12 +0300 | [diff] [blame] | 945 | struct linux_dmabuf_buffer *dmabuf; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 946 | int found = 0; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 947 | struct gbm_bo *bo; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 948 | pixman_region32_t dest_rect, src_rect; |
Ander Conselvan de Oliveira | 409eebf | 2012-12-05 15:14:04 +0200 | [diff] [blame] | 949 | pixman_box32_t *box, tbox; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 950 | uint32_t format; |
Kristian Høgsberg | 3b00bae | 2012-07-13 15:25:07 -0400 | [diff] [blame] | 951 | wl_fixed_t sx1, sy1, sx2, sy2; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 952 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 953 | if (b->gbm == NULL) |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 954 | return NULL; |
| 955 | |
Pekka Paalanen | 050c1ba | 2014-12-17 16:20:38 +0200 | [diff] [blame] | 956 | if (viewport->buffer.transform != output->base.transform) |
Ander Conselvan de Oliveira | 2908a3d | 2012-11-27 17:03:43 +0200 | [diff] [blame] | 957 | return NULL; |
| 958 | |
Pekka Paalanen | 050c1ba | 2014-12-17 16:20:38 +0200 | [diff] [blame] | 959 | if (viewport->buffer.scale != output->base.current_scale) |
Alexander Larsson | d9a7bb7 | 2013-05-22 14:41:39 +0200 | [diff] [blame] | 960 | return NULL; |
| 961 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 962 | if (b->sprites_are_broken) |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 963 | return NULL; |
Kristian Høgsberg | 65bec24 | 2012-03-05 19:57:35 -0500 | [diff] [blame] | 964 | |
Pekka Paalanen | 050c1ba | 2014-12-17 16:20:38 +0200 | [diff] [blame] | 965 | if (ev->output_mask != (1u << output->base.id)) |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 966 | return NULL; |
Ander Conselvan de Oliveira | d450b19 | 2012-06-26 17:09:12 +0300 | [diff] [blame] | 967 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 968 | if (ev->surface->buffer_ref.buffer == NULL) |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 969 | return NULL; |
Pekka Paalanen | bf8cc6f | 2014-06-12 17:12:12 +0300 | [diff] [blame] | 970 | buffer_resource = ev->surface->buffer_ref.buffer->resource; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 971 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 972 | if (ev->alpha != 1.0f) |
Ville Syrjälä | 5a84f31 | 2012-11-16 11:48:46 +0200 | [diff] [blame] | 973 | return NULL; |
| 974 | |
Pekka Paalanen | bf8cc6f | 2014-06-12 17:12:12 +0300 | [diff] [blame] | 975 | if (wl_shm_buffer_get(buffer_resource)) |
Rob Clark | 702ffae | 2012-08-09 14:18:27 -0500 | [diff] [blame] | 976 | return NULL; |
| 977 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 978 | if (!drm_view_transform_supported(ev)) |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 979 | return NULL; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 980 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 981 | wl_list_for_each(s, &b->sprite_list, link) { |
Daniel Stone | a3ae476 | 2015-11-28 12:07:36 +0000 | [diff] [blame] | 982 | if (!drm_sprite_crtc_supported(output, s)) |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 983 | continue; |
| 984 | |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 985 | if (!s->next) { |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 986 | found = 1; |
| 987 | break; |
| 988 | } |
| 989 | } |
| 990 | |
| 991 | /* No sprites available */ |
| 992 | if (!found) |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 993 | return NULL; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 994 | |
Pekka Paalanen | bf8cc6f | 2014-06-12 17:12:12 +0300 | [diff] [blame] | 995 | if ((dmabuf = linux_dmabuf_buffer_get(buffer_resource))) { |
Bryce Harrington | a358207 | 2015-08-14 12:23:13 -0700 | [diff] [blame] | 996 | #ifdef HAVE_GBM_FD_IMPORT |
Pekka Paalanen | bf8cc6f | 2014-06-12 17:12:12 +0300 | [diff] [blame] | 997 | /* XXX: TODO: |
| 998 | * |
| 999 | * Use AddFB2 directly, do not go via GBM. |
| 1000 | * Add support for multiplanar formats. |
| 1001 | * Both require refactoring in the DRM-backend to |
| 1002 | * support a mix of gbm_bos and drmfbs. |
| 1003 | */ |
| 1004 | struct gbm_import_fd_data gbm_dmabuf = { |
Emmanuel Gil Peyrot | c399692 | 2015-11-24 19:28:24 +0000 | [diff] [blame] | 1005 | .fd = dmabuf->attributes.fd[0], |
| 1006 | .width = dmabuf->attributes.width, |
| 1007 | .height = dmabuf->attributes.height, |
| 1008 | .stride = dmabuf->attributes.stride[0], |
| 1009 | .format = dmabuf->attributes.format |
Pekka Paalanen | bf8cc6f | 2014-06-12 17:12:12 +0300 | [diff] [blame] | 1010 | }; |
| 1011 | |
Emmanuel Gil Peyrot | c399692 | 2015-11-24 19:28:24 +0000 | [diff] [blame] | 1012 | if (dmabuf->attributes.n_planes != 1 || dmabuf->attributes.offset[0] != 0) |
Pekka Paalanen | bf8cc6f | 2014-06-12 17:12:12 +0300 | [diff] [blame] | 1013 | return NULL; |
| 1014 | |
| 1015 | bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_FD, &gbm_dmabuf, |
| 1016 | GBM_BO_USE_SCANOUT); |
Bryce Harrington | a358207 | 2015-08-14 12:23:13 -0700 | [diff] [blame] | 1017 | #else |
| 1018 | return NULL; |
| 1019 | #endif |
Pekka Paalanen | bf8cc6f | 2014-06-12 17:12:12 +0300 | [diff] [blame] | 1020 | } else { |
| 1021 | bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER, |
| 1022 | buffer_resource, GBM_BO_USE_SCANOUT); |
| 1023 | } |
Kristian Høgsberg | 2763a2e | 2012-07-13 22:54:43 -0400 | [diff] [blame] | 1024 | if (!bo) |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1025 | return NULL; |
Kristian Høgsberg | 2763a2e | 2012-07-13 22:54:43 -0400 | [diff] [blame] | 1026 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1027 | format = drm_output_check_sprite_format(s, ev, bo); |
Kristian Høgsberg | a2f84cc | 2012-12-07 12:37:58 -0500 | [diff] [blame] | 1028 | if (format == 0) { |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 1029 | gbm_bo_destroy(bo); |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1030 | return NULL; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1031 | } |
| 1032 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1033 | s->next = drm_fb_get_from_bo(bo, b, format); |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 1034 | if (!s->next) { |
| 1035 | gbm_bo_destroy(bo); |
| 1036 | return NULL; |
| 1037 | } |
| 1038 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1039 | drm_fb_set_buffer(s->next, ev->surface->buffer_ref.buffer); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1040 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1041 | box = pixman_region32_extents(&ev->transform.boundingbox); |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1042 | s->plane.x = box->x1; |
| 1043 | s->plane.y = box->y1; |
| 1044 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1045 | /* |
| 1046 | * Calculate the source & dest rects properly based on actual |
Derek Foreman | 4b1a0a1 | 2014-09-10 15:37:33 -0500 | [diff] [blame] | 1047 | * position (note the caller has called weston_view_update_transform() |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1048 | * for us already). |
| 1049 | */ |
| 1050 | pixman_region32_init(&dest_rect); |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1051 | pixman_region32_intersect(&dest_rect, &ev->transform.boundingbox, |
Pekka Paalanen | 050c1ba | 2014-12-17 16:20:38 +0200 | [diff] [blame] | 1052 | &output->base.region); |
| 1053 | pixman_region32_translate(&dest_rect, -output->base.x, -output->base.y); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1054 | box = pixman_region32_extents(&dest_rect); |
Pekka Paalanen | 050c1ba | 2014-12-17 16:20:38 +0200 | [diff] [blame] | 1055 | tbox = weston_transformed_rect(output->base.width, |
| 1056 | output->base.height, |
| 1057 | output->base.transform, |
| 1058 | output->base.current_scale, |
Alexander Larsson | d9a7bb7 | 2013-05-22 14:41:39 +0200 | [diff] [blame] | 1059 | *box); |
Ander Conselvan de Oliveira | 409eebf | 2012-12-05 15:14:04 +0200 | [diff] [blame] | 1060 | s->dest_x = tbox.x1; |
| 1061 | s->dest_y = tbox.y1; |
| 1062 | s->dest_w = tbox.x2 - tbox.x1; |
| 1063 | s->dest_h = tbox.y2 - tbox.y1; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1064 | pixman_region32_fini(&dest_rect); |
| 1065 | |
| 1066 | pixman_region32_init(&src_rect); |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1067 | pixman_region32_intersect(&src_rect, &ev->transform.boundingbox, |
Pekka Paalanen | 050c1ba | 2014-12-17 16:20:38 +0200 | [diff] [blame] | 1068 | &output->base.region); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1069 | box = pixman_region32_extents(&src_rect); |
Kristian Høgsberg | 3b00bae | 2012-07-13 15:25:07 -0400 | [diff] [blame] | 1070 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1071 | weston_view_from_global_fixed(ev, |
| 1072 | wl_fixed_from_int(box->x1), |
| 1073 | wl_fixed_from_int(box->y1), |
| 1074 | &sx1, &sy1); |
| 1075 | weston_view_from_global_fixed(ev, |
| 1076 | wl_fixed_from_int(box->x2), |
| 1077 | wl_fixed_from_int(box->y2), |
| 1078 | &sx2, &sy2); |
Kristian Høgsberg | 3b00bae | 2012-07-13 15:25:07 -0400 | [diff] [blame] | 1079 | |
| 1080 | if (sx1 < 0) |
| 1081 | sx1 = 0; |
| 1082 | if (sy1 < 0) |
| 1083 | sy1 = 0; |
Jason Ekstrand | 918f2dd | 2013-12-02 21:01:53 -0600 | [diff] [blame] | 1084 | if (sx2 > wl_fixed_from_int(ev->surface->width)) |
| 1085 | sx2 = wl_fixed_from_int(ev->surface->width); |
| 1086 | if (sy2 > wl_fixed_from_int(ev->surface->height)) |
| 1087 | sy2 = wl_fixed_from_int(ev->surface->height); |
Kristian Høgsberg | 3b00bae | 2012-07-13 15:25:07 -0400 | [diff] [blame] | 1088 | |
Ander Conselvan de Oliveira | 409eebf | 2012-12-05 15:14:04 +0200 | [diff] [blame] | 1089 | tbox.x1 = sx1; |
| 1090 | tbox.y1 = sy1; |
| 1091 | tbox.x2 = sx2; |
| 1092 | tbox.y2 = sy2; |
| 1093 | |
Jason Ekstrand | 918f2dd | 2013-12-02 21:01:53 -0600 | [diff] [blame] | 1094 | tbox = weston_transformed_rect(wl_fixed_from_int(ev->surface->width), |
| 1095 | wl_fixed_from_int(ev->surface->height), |
Pekka Paalanen | 952b6c8 | 2014-03-14 14:38:15 +0200 | [diff] [blame] | 1096 | viewport->buffer.transform, |
| 1097 | viewport->buffer.scale, |
Pekka Paalanen | 1fd9c0f | 2013-11-26 18:19:41 +0100 | [diff] [blame] | 1098 | tbox); |
Ander Conselvan de Oliveira | 409eebf | 2012-12-05 15:14:04 +0200 | [diff] [blame] | 1099 | |
| 1100 | s->src_x = tbox.x1 << 8; |
| 1101 | s->src_y = tbox.y1 << 8; |
| 1102 | s->src_w = (tbox.x2 - tbox.x1) << 8; |
| 1103 | s->src_h = (tbox.y2 - tbox.y1) << 8; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1104 | pixman_region32_fini(&src_rect); |
| 1105 | |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1106 | return &s->plane; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1107 | } |
| 1108 | |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1109 | static struct weston_plane * |
Pekka Paalanen | 050c1ba | 2014-12-17 16:20:38 +0200 | [diff] [blame] | 1110 | drm_output_prepare_cursor_view(struct drm_output *output, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1111 | struct weston_view *ev) |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 1112 | { |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 1113 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
Neil Roberts | f37f82c | 2014-05-01 18:00:41 +0100 | [diff] [blame] | 1114 | struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport; |
Derek Foreman | 6c19b69 | 2015-12-03 14:07:12 -0600 | [diff] [blame] | 1115 | struct wl_shm_buffer *shmbuf; |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 1116 | |
Derek Foreman | be428b3 | 2015-11-24 11:39:38 -0600 | [diff] [blame] | 1117 | if (ev->transform.enabled && |
| 1118 | (ev->transform.matrix.type > WESTON_MATRIX_TRANSFORM_TRANSLATE)) |
| 1119 | return NULL; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1120 | if (b->gbm == NULL) |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1121 | return NULL; |
Ander Conselvan de Oliveira | 2908a3d | 2012-11-27 17:03:43 +0200 | [diff] [blame] | 1122 | if (output->base.transform != WL_OUTPUT_TRANSFORM_NORMAL) |
| 1123 | return NULL; |
Pekka Paalanen | 050c1ba | 2014-12-17 16:20:38 +0200 | [diff] [blame] | 1124 | if (viewport->buffer.scale != output->base.current_scale) |
Neil Roberts | f37f82c | 2014-05-01 18:00:41 +0100 | [diff] [blame] | 1125 | return NULL; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1126 | if (output->cursor_view) |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 1127 | return NULL; |
Pekka Paalanen | 050c1ba | 2014-12-17 16:20:38 +0200 | [diff] [blame] | 1128 | if (ev->output_mask != (1u << output->base.id)) |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 1129 | return NULL; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1130 | if (b->cursors_are_broken) |
Kristian Høgsberg | 8a01580 | 2012-08-09 17:19:23 -0400 | [diff] [blame] | 1131 | return NULL; |
Pekka Paalanen | 5580f22 | 2015-02-17 16:33:18 +0200 | [diff] [blame] | 1132 | if (ev->geometry.scissor_enabled) |
| 1133 | return NULL; |
Derek Foreman | 6c19b69 | 2015-12-03 14:07:12 -0600 | [diff] [blame] | 1134 | if (ev->surface->buffer_ref.buffer == NULL) |
| 1135 | return NULL; |
| 1136 | shmbuf = wl_shm_buffer_get(ev->surface->buffer_ref.buffer->resource); |
| 1137 | if (!shmbuf) |
| 1138 | return NULL; |
| 1139 | if (wl_shm_buffer_get_format(shmbuf) != WL_SHM_FORMAT_ARGB8888) |
| 1140 | return NULL; |
| 1141 | if (ev->surface->width > b->cursor_width || |
Daniel Stone | 70d337d | 2015-06-16 18:42:23 +0100 | [diff] [blame] | 1142 | ev->surface->height > b->cursor_height) |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 1143 | return NULL; |
| 1144 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1145 | output->cursor_view = ev; |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 1146 | |
| 1147 | return &output->cursor_plane; |
| 1148 | } |
| 1149 | |
Pekka Paalanen | d0ead48 | 2014-06-16 12:05:40 +0300 | [diff] [blame] | 1150 | /** |
| 1151 | * Update the image for the current cursor surface |
| 1152 | * |
| 1153 | * @param b DRM backend structure |
| 1154 | * @param bo GBM buffer object to write into |
| 1155 | * @param ev View to use for cursor image |
| 1156 | */ |
| 1157 | static void |
| 1158 | cursor_bo_update(struct drm_backend *b, struct gbm_bo *bo, |
| 1159 | struct weston_view *ev) |
| 1160 | { |
| 1161 | struct weston_buffer *buffer = ev->surface->buffer_ref.buffer; |
| 1162 | uint32_t buf[b->cursor_width * b->cursor_height]; |
| 1163 | int32_t stride; |
| 1164 | uint8_t *s; |
| 1165 | int i; |
| 1166 | |
| 1167 | assert(buffer && buffer->shm_buffer); |
| 1168 | assert(buffer->shm_buffer == wl_shm_buffer_get(buffer->resource)); |
| 1169 | assert(ev->surface->width <= b->cursor_width); |
| 1170 | assert(ev->surface->height <= b->cursor_height); |
| 1171 | |
| 1172 | memset(buf, 0, sizeof buf); |
| 1173 | stride = wl_shm_buffer_get_stride(buffer->shm_buffer); |
| 1174 | s = wl_shm_buffer_get_data(buffer->shm_buffer); |
| 1175 | |
| 1176 | wl_shm_buffer_begin_access(buffer->shm_buffer); |
| 1177 | for (i = 0; i < ev->surface->height; i++) |
| 1178 | memcpy(buf + i * b->cursor_width, |
| 1179 | s + i * stride, |
| 1180 | ev->surface->width * 4); |
| 1181 | wl_shm_buffer_end_access(buffer->shm_buffer); |
| 1182 | |
| 1183 | if (gbm_bo_write(bo, buf, sizeof buf) < 0) |
| 1184 | weston_log("failed update cursor: %m\n"); |
| 1185 | } |
| 1186 | |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 1187 | static void |
| 1188 | drm_output_set_cursor(struct drm_output *output) |
| 1189 | { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1190 | struct weston_view *ev = output->cursor_view; |
Neil Roberts | e505171 | 2013-11-13 15:44:06 +0000 | [diff] [blame] | 1191 | struct weston_buffer *buffer; |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 1192 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
Pekka Paalanen | d0ead48 | 2014-06-16 12:05:40 +0300 | [diff] [blame] | 1193 | EGLint handle; |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 1194 | struct gbm_bo *bo; |
Derek Foreman | be428b3 | 2015-11-24 11:39:38 -0600 | [diff] [blame] | 1195 | float x, y; |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 1196 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1197 | output->cursor_view = NULL; |
| 1198 | if (ev == NULL) { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1199 | drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0); |
Derek Foreman | b042756 | 2016-02-05 15:55:20 -0600 | [diff] [blame] | 1200 | output->cursor_plane.x = INT32_MIN; |
| 1201 | output->cursor_plane.y = INT32_MIN; |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 1202 | return; |
| 1203 | } |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 1204 | |
Neil Roberts | e505171 | 2013-11-13 15:44:06 +0000 | [diff] [blame] | 1205 | buffer = ev->surface->buffer_ref.buffer; |
| 1206 | |
| 1207 | if (buffer && |
Pekka Paalanen | de685b8 | 2012-12-04 15:58:12 +0200 | [diff] [blame] | 1208 | pixman_region32_not_empty(&output->cursor_plane.damage)) { |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 1209 | pixman_region32_fini(&output->cursor_plane.damage); |
| 1210 | pixman_region32_init(&output->cursor_plane.damage); |
Kristian Høgsberg | 1f5de35 | 2012-07-18 12:09:58 -0400 | [diff] [blame] | 1211 | output->current_cursor ^= 1; |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 1212 | bo = output->gbm_cursor_bo[output->current_cursor]; |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 1213 | |
Pekka Paalanen | d0ead48 | 2014-06-16 12:05:40 +0300 | [diff] [blame] | 1214 | cursor_bo_update(b, bo, ev); |
Kristian Høgsberg | 1f5de35 | 2012-07-18 12:09:58 -0400 | [diff] [blame] | 1215 | handle = gbm_bo_get_handle(bo).s32; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1216 | if (drmModeSetCursor(b->drm.fd, output->crtc_id, handle, |
| 1217 | b->cursor_width, b->cursor_height)) { |
Pekka Paalanen | ae29da2 | 2012-08-06 14:57:05 +0300 | [diff] [blame] | 1218 | weston_log("failed to set cursor: %m\n"); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1219 | b->cursors_are_broken = 1; |
Rob Clark | ab5b1e3 | 2012-08-09 13:24:45 -0500 | [diff] [blame] | 1220 | } |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 1221 | } |
| 1222 | |
Derek Foreman | be428b3 | 2015-11-24 11:39:38 -0600 | [diff] [blame] | 1223 | weston_view_to_global_float(ev, 0, 0, &x, &y); |
Pekka Paalanen | 7eaed40 | 2015-11-27 14:20:58 +0200 | [diff] [blame] | 1224 | |
| 1225 | /* From global to output space, output transform is guaranteed to be |
| 1226 | * NORMAL by drm_output_prepare_cursor_view(). |
| 1227 | */ |
| 1228 | x = (x - output->base.x) * output->base.current_scale; |
| 1229 | y = (y - output->base.y) * output->base.current_scale; |
| 1230 | |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1231 | if (output->cursor_plane.x != x || output->cursor_plane.y != y) { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1232 | if (drmModeMoveCursor(b->drm.fd, output->crtc_id, x, y)) { |
Kristian Høgsberg | 24e4275 | 2012-07-18 12:08:37 -0400 | [diff] [blame] | 1233 | weston_log("failed to move cursor: %m\n"); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1234 | b->cursors_are_broken = 1; |
Rob Clark | ab5b1e3 | 2012-08-09 13:24:45 -0500 | [diff] [blame] | 1235 | } |
| 1236 | |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1237 | output->cursor_plane.x = x; |
| 1238 | output->cursor_plane.y = y; |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 1239 | } |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 1240 | } |
| 1241 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1242 | static void |
Pekka Paalanen | 050c1ba | 2014-12-17 16:20:38 +0200 | [diff] [blame] | 1243 | drm_assign_planes(struct weston_output *output_base) |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1244 | { |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 1245 | struct drm_backend *b = to_drm_backend(output_base->compositor); |
| 1246 | struct drm_output *output = to_drm_output(output_base); |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1247 | struct weston_view *ev, *next; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1248 | pixman_region32_t overlap, surface_overlap; |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1249 | struct weston_plane *primary, *next_plane; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1250 | |
| 1251 | /* |
| 1252 | * Find a surface for each sprite in the output using some heuristics: |
| 1253 | * 1) size |
| 1254 | * 2) frequency of update |
| 1255 | * 3) opacity (though some hw might support alpha blending) |
| 1256 | * 4) clipping (this can be fixed with color keys) |
| 1257 | * |
| 1258 | * The idea is to save on blitting since this should save power. |
| 1259 | * If we can get a large video surface on the sprite for example, |
| 1260 | * the main display surface may not need to update at all, and |
| 1261 | * the client buffer can be used directly for the sprite surface |
| 1262 | * as we do for flipping full screen surfaces. |
| 1263 | */ |
| 1264 | pixman_region32_init(&overlap); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1265 | primary = &output_base->compositor->primary_plane; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1266 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1267 | wl_list_for_each_safe(ev, next, &output_base->compositor->view_list, link) { |
Ander Conselvan de Oliveira | 895b1fd | 2013-11-19 15:22:05 +0200 | [diff] [blame] | 1268 | struct weston_surface *es = ev->surface; |
| 1269 | |
| 1270 | /* Test whether this buffer can ever go into a plane: |
| 1271 | * non-shm, or small enough to be a cursor. |
| 1272 | * |
| 1273 | * Also, keep a reference when using the pixman renderer. |
| 1274 | * That makes it possible to do a seamless switch to the GL |
| 1275 | * renderer and since the pixman renderer keeps a reference |
| 1276 | * to the buffer anyway, there is no side effects. |
Pekka Paalanen | ccfeae2 | 2012-12-04 15:58:14 +0200 | [diff] [blame] | 1277 | */ |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1278 | if (b->use_pixman || |
Ander Conselvan de Oliveira | 895b1fd | 2013-11-19 15:22:05 +0200 | [diff] [blame] | 1279 | (es->buffer_ref.buffer && |
| 1280 | (!wl_shm_buffer_get(es->buffer_ref.buffer->resource) || |
Derek Foreman | 8743047 | 2015-10-15 10:24:48 -0500 | [diff] [blame] | 1281 | (ev->surface->width <= b->cursor_width && |
| 1282 | ev->surface->height <= b->cursor_height)))) |
Derek Foreman | 0fd6d4e | 2014-10-10 09:36:45 -0500 | [diff] [blame] | 1283 | es->keep_buffer = true; |
Ander Conselvan de Oliveira | 895b1fd | 2013-11-19 15:22:05 +0200 | [diff] [blame] | 1284 | else |
Derek Foreman | 0fd6d4e | 2014-10-10 09:36:45 -0500 | [diff] [blame] | 1285 | es->keep_buffer = false; |
Pekka Paalanen | ccfeae2 | 2012-12-04 15:58:14 +0200 | [diff] [blame] | 1286 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1287 | pixman_region32_init(&surface_overlap); |
| 1288 | pixman_region32_intersect(&surface_overlap, &overlap, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1289 | &ev->transform.boundingbox); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1290 | |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1291 | next_plane = NULL; |
Kristian Høgsberg | 6143f7d | 2012-07-14 00:31:32 -0400 | [diff] [blame] | 1292 | if (pixman_region32_not_empty(&surface_overlap)) |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1293 | next_plane = primary; |
| 1294 | if (next_plane == NULL) |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1295 | next_plane = drm_output_prepare_cursor_view(output, ev); |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1296 | if (next_plane == NULL) |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1297 | next_plane = drm_output_prepare_scanout_view(output, ev); |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1298 | if (next_plane == NULL) |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1299 | next_plane = drm_output_prepare_overlay_view(output, ev); |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1300 | if (next_plane == NULL) |
| 1301 | next_plane = primary; |
Pekka Paalanen | bf0e031 | 2014-12-17 16:20:41 +0200 | [diff] [blame] | 1302 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1303 | weston_view_move_to_plane(ev, next_plane); |
Pekka Paalanen | bf0e031 | 2014-12-17 16:20:41 +0200 | [diff] [blame] | 1304 | |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1305 | if (next_plane == primary) |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1306 | pixman_region32_union(&overlap, &overlap, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1307 | &ev->transform.boundingbox); |
Kristian Høgsberg | 6143f7d | 2012-07-14 00:31:32 -0400 | [diff] [blame] | 1308 | |
Pekka Paalanen | bf0e031 | 2014-12-17 16:20:41 +0200 | [diff] [blame] | 1309 | if (next_plane == primary || |
| 1310 | next_plane == &output->cursor_plane) { |
| 1311 | /* cursor plane involves a copy */ |
| 1312 | ev->psf_flags = 0; |
| 1313 | } else { |
| 1314 | /* All other planes are a direct scanout of a |
| 1315 | * single client buffer. |
| 1316 | */ |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 1317 | ev->psf_flags = WP_PRESENTATION_FEEDBACK_KIND_ZERO_COPY; |
Pekka Paalanen | bf0e031 | 2014-12-17 16:20:41 +0200 | [diff] [blame] | 1318 | } |
| 1319 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1320 | pixman_region32_fini(&surface_overlap); |
| 1321 | } |
| 1322 | pixman_region32_fini(&overlap); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1323 | } |
| 1324 | |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 1325 | /** |
| 1326 | * Find the closest-matching mode for a given target |
| 1327 | * |
| 1328 | * Given a target mode, find the most suitable mode amongst the output's |
| 1329 | * current mode list to use, preferring the current mode if possible, to |
| 1330 | * avoid an expensive mode switch. |
| 1331 | * |
| 1332 | * @param output DRM output |
| 1333 | * @param target_mode Mode to attempt to match |
| 1334 | * @returns Pointer to a mode from the output's mode list |
| 1335 | */ |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1336 | static struct drm_mode * |
| 1337 | choose_mode (struct drm_output *output, struct weston_mode *target_mode) |
| 1338 | { |
| 1339 | struct drm_mode *tmp_mode = NULL, *mode; |
| 1340 | |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 1341 | if (output->base.current_mode->width == target_mode->width && |
| 1342 | output->base.current_mode->height == target_mode->height && |
| 1343 | (output->base.current_mode->refresh == target_mode->refresh || |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1344 | target_mode->refresh == 0)) |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 1345 | return (struct drm_mode *)output->base.current_mode; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1346 | |
| 1347 | wl_list_for_each(mode, &output->base.mode_list, base.link) { |
| 1348 | if (mode->mode_info.hdisplay == target_mode->width && |
| 1349 | mode->mode_info.vdisplay == target_mode->height) { |
Mario Kleiner | 872797c | 2015-06-21 21:25:09 +0200 | [diff] [blame] | 1350 | if (mode->base.refresh == target_mode->refresh || |
| 1351 | target_mode->refresh == 0) { |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1352 | return mode; |
Daniel Stone | f556ebe | 2015-05-21 08:28:58 +0100 | [diff] [blame] | 1353 | } else if (!tmp_mode) |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1354 | tmp_mode = mode; |
| 1355 | } |
| 1356 | } |
| 1357 | |
| 1358 | return tmp_mode; |
| 1359 | } |
| 1360 | |
| 1361 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1362 | drm_output_init_egl(struct drm_output *output, struct drm_backend *b); |
Daniel Stone | 3e661f7 | 2016-11-04 17:24:06 +0000 | [diff] [blame] | 1363 | static void |
| 1364 | drm_output_fini_egl(struct drm_output *output); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1365 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1366 | drm_output_init_pixman(struct drm_output *output, struct drm_backend *b); |
Daniel Stone | 3e661f7 | 2016-11-04 17:24:06 +0000 | [diff] [blame] | 1367 | static void |
| 1368 | drm_output_fini_pixman(struct drm_output *output); |
Ander Conselvan de Oliveira | 6c01c9c | 2012-12-14 13:37:30 -0200 | [diff] [blame] | 1369 | |
| 1370 | static int |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1371 | drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mode) |
| 1372 | { |
| 1373 | struct drm_output *output; |
| 1374 | struct drm_mode *drm_mode; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1375 | struct drm_backend *b; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1376 | |
| 1377 | if (output_base == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1378 | weston_log("output is NULL.\n"); |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1379 | return -1; |
| 1380 | } |
| 1381 | |
| 1382 | if (mode == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1383 | weston_log("mode is NULL.\n"); |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1384 | return -1; |
| 1385 | } |
| 1386 | |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 1387 | b = to_drm_backend(output_base->compositor); |
| 1388 | output = to_drm_output(output_base); |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1389 | drm_mode = choose_mode (output, mode); |
| 1390 | |
| 1391 | if (!drm_mode) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1392 | weston_log("%s, invalid resolution:%dx%d\n", __func__, mode->width, mode->height); |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1393 | return -1; |
Ander Conselvan de Oliveira | 6c01c9c | 2012-12-14 13:37:30 -0200 | [diff] [blame] | 1394 | } |
| 1395 | |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 1396 | if (&drm_mode->base == output->base.current_mode) |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1397 | return 0; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1398 | |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 1399 | output->base.current_mode->flags = 0; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1400 | |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 1401 | output->base.current_mode = &drm_mode->base; |
| 1402 | output->base.current_mode->flags = |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1403 | WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED; |
| 1404 | |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1405 | /* reset rendering stuff. */ |
Ander Conselvan de Oliveira | 526d461 | 2013-01-25 15:13:03 +0200 | [diff] [blame] | 1406 | drm_output_release_fb(output, output->current); |
| 1407 | drm_output_release_fb(output, output->next); |
| 1408 | output->current = output->next = NULL; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1409 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1410 | if (b->use_pixman) { |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1411 | drm_output_fini_pixman(output); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1412 | if (drm_output_init_pixman(output, b) < 0) { |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1413 | weston_log("failed to init output pixman state with " |
| 1414 | "new mode\n"); |
| 1415 | return -1; |
| 1416 | } |
| 1417 | } else { |
Daniel Stone | 3e661f7 | 2016-11-04 17:24:06 +0000 | [diff] [blame] | 1418 | drm_output_fini_egl(output); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1419 | if (drm_output_init_egl(output, b) < 0) { |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1420 | weston_log("failed to init output egl state with " |
| 1421 | "new mode"); |
| 1422 | return -1; |
| 1423 | } |
Ander Conselvan de Oliveira | 6c01c9c | 2012-12-14 13:37:30 -0200 | [diff] [blame] | 1424 | } |
| 1425 | |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1426 | return 0; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1427 | } |
| 1428 | |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 1429 | static int |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1430 | on_drm_input(int fd, uint32_t mask, void *data) |
| 1431 | { |
| 1432 | drmEventContext evctx; |
| 1433 | |
| 1434 | memset(&evctx, 0, sizeof evctx); |
| 1435 | evctx.version = DRM_EVENT_CONTEXT_VERSION; |
| 1436 | evctx.page_flip_handler = page_flip_handler; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1437 | evctx.vblank_handler = vblank_handler; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1438 | drmHandleEvent(fd, &evctx); |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 1439 | |
| 1440 | return 1; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1441 | } |
| 1442 | |
| 1443 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1444 | init_drm(struct drm_backend *b, struct udev_device *device) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1445 | { |
Kristian Høgsberg | b5ef591 | 2012-03-28 22:53:49 -0400 | [diff] [blame] | 1446 | const char *filename, *sysnum; |
Ander Conselvan de Oliveira | 95eb3a2 | 2013-05-07 14:16:59 +0300 | [diff] [blame] | 1447 | uint64_t cap; |
| 1448 | int fd, ret; |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame] | 1449 | clockid_t clk_id; |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1450 | |
Kristian Høgsberg | b71302e | 2012-05-10 12:28:35 -0400 | [diff] [blame] | 1451 | sysnum = udev_device_get_sysnum(device); |
| 1452 | if (sysnum) |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1453 | b->drm.id = atoi(sysnum); |
| 1454 | if (!sysnum || b->drm.id < 0) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1455 | weston_log("cannot get device sysnum\n"); |
Kristian Høgsberg | b71302e | 2012-05-10 12:28:35 -0400 | [diff] [blame] | 1456 | return -1; |
| 1457 | } |
| 1458 | |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 1459 | filename = udev_device_get_devnode(device); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1460 | fd = weston_launcher_open(b->compositor->launcher, filename, O_RDWR); |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 1461 | if (fd < 0) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1462 | /* Probably permissions error */ |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1463 | weston_log("couldn't open %s, skipping\n", |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1464 | udev_device_get_devnode(device)); |
| 1465 | return -1; |
| 1466 | } |
| 1467 | |
Kristian Høgsberg | fc9c5e0 | 2012-06-08 16:45:33 -0400 | [diff] [blame] | 1468 | weston_log("using %s\n", filename); |
| 1469 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1470 | b->drm.fd = fd; |
| 1471 | b->drm.filename = strdup(filename); |
Ander Conselvan de Oliveira | 2292917 | 2013-01-25 15:13:02 +0200 | [diff] [blame] | 1472 | |
Ander Conselvan de Oliveira | 95eb3a2 | 2013-05-07 14:16:59 +0300 | [diff] [blame] | 1473 | ret = drmGetCap(fd, DRM_CAP_TIMESTAMP_MONOTONIC, &cap); |
| 1474 | if (ret == 0 && cap == 1) |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame] | 1475 | clk_id = CLOCK_MONOTONIC; |
Ander Conselvan de Oliveira | 95eb3a2 | 2013-05-07 14:16:59 +0300 | [diff] [blame] | 1476 | else |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame] | 1477 | clk_id = CLOCK_REALTIME; |
| 1478 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1479 | if (weston_compositor_set_presentation_clock(b->compositor, clk_id) < 0) { |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame] | 1480 | weston_log("Error: failed to set presentation clock %d.\n", |
| 1481 | clk_id); |
| 1482 | return -1; |
| 1483 | } |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 1484 | |
Alvaro Fernando García | dce7c6e | 2014-07-28 18:30:17 -0300 | [diff] [blame] | 1485 | ret = drmGetCap(fd, DRM_CAP_CURSOR_WIDTH, &cap); |
| 1486 | if (ret == 0) |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1487 | b->cursor_width = cap; |
Alvaro Fernando García | dce7c6e | 2014-07-28 18:30:17 -0300 | [diff] [blame] | 1488 | else |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1489 | b->cursor_width = 64; |
Alvaro Fernando García | dce7c6e | 2014-07-28 18:30:17 -0300 | [diff] [blame] | 1490 | |
| 1491 | ret = drmGetCap(fd, DRM_CAP_CURSOR_HEIGHT, &cap); |
| 1492 | if (ret == 0) |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1493 | b->cursor_height = cap; |
Alvaro Fernando García | dce7c6e | 2014-07-28 18:30:17 -0300 | [diff] [blame] | 1494 | else |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1495 | b->cursor_height = 64; |
Alvaro Fernando García | dce7c6e | 2014-07-28 18:30:17 -0300 | [diff] [blame] | 1496 | |
Ander Conselvan de Oliveira | 2292917 | 2013-01-25 15:13:02 +0200 | [diff] [blame] | 1497 | return 0; |
| 1498 | } |
| 1499 | |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 1500 | static struct gbm_device * |
| 1501 | create_gbm_device(int fd) |
Ander Conselvan de Oliveira | 2292917 | 2013-01-25 15:13:02 +0200 | [diff] [blame] | 1502 | { |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 1503 | struct gbm_device *gbm; |
Alexandru DAMIAN | be0ac5b | 2013-10-02 17:51:05 +0100 | [diff] [blame] | 1504 | |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 1505 | gl_renderer = weston_load_module("gl-renderer.so", |
| 1506 | "gl_renderer_interface"); |
| 1507 | if (!gl_renderer) |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 1508 | return NULL; |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 1509 | |
| 1510 | /* GBM will load a dri driver, but even though they need symbols from |
| 1511 | * libglapi, in some version of Mesa they are not linked to it. Since |
| 1512 | * only the gl-renderer module links to it, the call above won't make |
| 1513 | * these symbols globally available, and loading the DRI driver fails. |
| 1514 | * Workaround this by dlopen()'ing libglapi with RTLD_GLOBAL. */ |
| 1515 | dlopen("libglapi.so.0", RTLD_LAZY | RTLD_GLOBAL); |
| 1516 | |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 1517 | gbm = gbm_create_device(fd); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1518 | |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 1519 | return gbm; |
| 1520 | } |
| 1521 | |
Bryce Harrington | c056a98 | 2015-05-19 15:25:18 -0700 | [diff] [blame] | 1522 | /* When initializing EGL, if the preferred buffer format isn't available |
Bryce Harrington | b993998 | 2016-04-15 20:28:26 -0700 | [diff] [blame] | 1523 | * we may be able to substitute an ARGB format for an XRGB one. |
Derek Foreman | c4cfe85 | 2015-05-15 12:12:40 -0500 | [diff] [blame] | 1524 | * |
| 1525 | * This returns 0 if substitution isn't possible, but 0 might be a |
| 1526 | * legitimate format for other EGL platforms, so the caller is |
| 1527 | * responsible for checking for 0 before calling gl_renderer->create(). |
| 1528 | * |
| 1529 | * This works around https://bugs.freedesktop.org/show_bug.cgi?id=89689 |
| 1530 | * but it's entirely possible we'll see this again on other implementations. |
| 1531 | */ |
| 1532 | static int |
| 1533 | fallback_format_for(uint32_t format) |
| 1534 | { |
| 1535 | switch (format) { |
| 1536 | case GBM_FORMAT_XRGB8888: |
| 1537 | return GBM_FORMAT_ARGB8888; |
| 1538 | case GBM_FORMAT_XRGB2101010: |
| 1539 | return GBM_FORMAT_ARGB2101010; |
| 1540 | default: |
| 1541 | return 0; |
| 1542 | } |
| 1543 | } |
| 1544 | |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 1545 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1546 | drm_backend_create_gl_renderer(struct drm_backend *b) |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 1547 | { |
Derek Foreman | 6d55637 | 2015-11-04 14:47:33 -0600 | [diff] [blame] | 1548 | EGLint format[3] = { |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 1549 | b->gbm_format, |
| 1550 | fallback_format_for(b->gbm_format), |
Derek Foreman | 6d55637 | 2015-11-04 14:47:33 -0600 | [diff] [blame] | 1551 | 0, |
Derek Foreman | c4cfe85 | 2015-05-15 12:12:40 -0500 | [diff] [blame] | 1552 | }; |
Derek Foreman | 6d55637 | 2015-11-04 14:47:33 -0600 | [diff] [blame] | 1553 | int n_formats = 2; |
John Kåre Alsaker | ef591aa | 2013-03-02 12:27:39 +0100 | [diff] [blame] | 1554 | |
Derek Foreman | c4cfe85 | 2015-05-15 12:12:40 -0500 | [diff] [blame] | 1555 | if (format[1]) |
Derek Foreman | 6d55637 | 2015-11-04 14:47:33 -0600 | [diff] [blame] | 1556 | n_formats = 3; |
Miguel A. Vico | dddc670 | 2016-05-18 17:41:07 +0200 | [diff] [blame] | 1557 | if (gl_renderer->display_create(b->compositor, |
| 1558 | EGL_PLATFORM_GBM_KHR, |
| 1559 | (void *)b->gbm, |
Miguel A. Vico | 41700e3 | 2016-05-18 17:47:59 +0200 | [diff] [blame] | 1560 | NULL, |
Miguel A. Vico | dddc670 | 2016-05-18 17:41:07 +0200 | [diff] [blame] | 1561 | gl_renderer->opaque_attribs, |
| 1562 | format, |
| 1563 | n_formats) < 0) { |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 1564 | return -1; |
| 1565 | } |
| 1566 | |
| 1567 | return 0; |
| 1568 | } |
| 1569 | |
| 1570 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1571 | init_egl(struct drm_backend *b) |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 1572 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1573 | b->gbm = create_gbm_device(b->drm.fd); |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 1574 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1575 | if (!b->gbm) |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 1576 | return -1; |
| 1577 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1578 | if (drm_backend_create_gl_renderer(b) < 0) { |
| 1579 | gbm_device_destroy(b->gbm); |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1580 | return -1; |
| 1581 | } |
| 1582 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1583 | return 0; |
| 1584 | } |
| 1585 | |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1586 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1587 | init_pixman(struct drm_backend *b) |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1588 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1589 | return pixman_renderer_init(b->compositor); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1590 | } |
| 1591 | |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 1592 | /** |
| 1593 | * Add a mode to output's mode list |
| 1594 | * |
| 1595 | * Copy the supplied DRM mode into a Weston mode structure, and add it to the |
| 1596 | * output's mode list. |
| 1597 | * |
| 1598 | * @param output DRM output to add mode to |
| 1599 | * @param info DRM mode structure to add |
| 1600 | * @returns Newly-allocated Weston/DRM mode structure |
| 1601 | */ |
Ander Conselvan de Oliveira | 42c4646 | 2012-08-09 16:45:00 +0300 | [diff] [blame] | 1602 | static struct drm_mode * |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 1603 | drm_output_add_mode(struct drm_output *output, const drmModeModeInfo *info) |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1604 | { |
| 1605 | struct drm_mode *mode; |
Kristian Høgsberg | c4621b0 | 2012-05-10 12:23:53 -0400 | [diff] [blame] | 1606 | uint64_t refresh; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1607 | |
| 1608 | mode = malloc(sizeof *mode); |
| 1609 | if (mode == NULL) |
Ander Conselvan de Oliveira | 42c4646 | 2012-08-09 16:45:00 +0300 | [diff] [blame] | 1610 | return NULL; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1611 | |
| 1612 | mode->base.flags = 0; |
Alexander Larsson | 0b13506 | 2013-05-28 16:23:36 +0200 | [diff] [blame] | 1613 | mode->base.width = info->hdisplay; |
| 1614 | mode->base.height = info->vdisplay; |
Kristian Høgsberg | c4621b0 | 2012-05-10 12:23:53 -0400 | [diff] [blame] | 1615 | |
| 1616 | /* Calculate higher precision (mHz) refresh rate */ |
| 1617 | refresh = (info->clock * 1000000LL / info->htotal + |
| 1618 | info->vtotal / 2) / info->vtotal; |
| 1619 | |
| 1620 | if (info->flags & DRM_MODE_FLAG_INTERLACE) |
| 1621 | refresh *= 2; |
| 1622 | if (info->flags & DRM_MODE_FLAG_DBLSCAN) |
| 1623 | refresh /= 2; |
| 1624 | if (info->vscan > 1) |
| 1625 | refresh /= info->vscan; |
| 1626 | |
| 1627 | mode->base.refresh = refresh; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1628 | mode->mode_info = *info; |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 1629 | |
| 1630 | if (info->type & DRM_MODE_TYPE_PREFERRED) |
| 1631 | mode->base.flags |= WL_OUTPUT_MODE_PREFERRED; |
| 1632 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1633 | wl_list_insert(output->base.mode_list.prev, &mode->base.link); |
| 1634 | |
Ander Conselvan de Oliveira | 42c4646 | 2012-08-09 16:45:00 +0300 | [diff] [blame] | 1635 | return mode; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1636 | } |
| 1637 | |
| 1638 | static int |
| 1639 | drm_subpixel_to_wayland(int drm_value) |
| 1640 | { |
| 1641 | switch (drm_value) { |
| 1642 | default: |
| 1643 | case DRM_MODE_SUBPIXEL_UNKNOWN: |
| 1644 | return WL_OUTPUT_SUBPIXEL_UNKNOWN; |
| 1645 | case DRM_MODE_SUBPIXEL_NONE: |
| 1646 | return WL_OUTPUT_SUBPIXEL_NONE; |
| 1647 | case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB: |
| 1648 | return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB; |
| 1649 | case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR: |
| 1650 | return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR; |
| 1651 | case DRM_MODE_SUBPIXEL_VERTICAL_RGB: |
| 1652 | return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB; |
| 1653 | case DRM_MODE_SUBPIXEL_VERTICAL_BGR: |
| 1654 | return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR; |
| 1655 | } |
| 1656 | } |
| 1657 | |
Tiago Vignatti | 5ab91ad | 2012-03-12 19:40:09 -0300 | [diff] [blame] | 1658 | /* returns a value between 0-255 range, where higher is brighter */ |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1659 | static uint32_t |
| 1660 | drm_get_backlight(struct drm_output *output) |
| 1661 | { |
| 1662 | long brightness, max_brightness, norm; |
| 1663 | |
| 1664 | brightness = backlight_get_brightness(output->backlight); |
| 1665 | max_brightness = backlight_get_max_brightness(output->backlight); |
| 1666 | |
Tiago Vignatti | 5ab91ad | 2012-03-12 19:40:09 -0300 | [diff] [blame] | 1667 | /* convert it on a scale of 0 to 255 */ |
| 1668 | norm = (brightness * 255)/(max_brightness); |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1669 | |
| 1670 | return (uint32_t) norm; |
| 1671 | } |
| 1672 | |
Tiago Vignatti | 5ab91ad | 2012-03-12 19:40:09 -0300 | [diff] [blame] | 1673 | /* values accepted are between 0-255 range */ |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1674 | static void |
| 1675 | drm_set_backlight(struct weston_output *output_base, uint32_t value) |
| 1676 | { |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 1677 | struct drm_output *output = to_drm_output(output_base); |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1678 | long max_brightness, new_brightness; |
| 1679 | |
| 1680 | if (!output->backlight) |
| 1681 | return; |
| 1682 | |
Kristian Høgsberg | 875ab9e | 2012-03-30 11:52:39 -0400 | [diff] [blame] | 1683 | if (value > 255) |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1684 | return; |
| 1685 | |
| 1686 | max_brightness = backlight_get_max_brightness(output->backlight); |
| 1687 | |
| 1688 | /* get denormalized value */ |
Tiago Vignatti | 5ab91ad | 2012-03-12 19:40:09 -0300 | [diff] [blame] | 1689 | new_brightness = (value * max_brightness) / 255; |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1690 | |
| 1691 | backlight_set_brightness(output->backlight, new_brightness); |
| 1692 | } |
| 1693 | |
| 1694 | static drmModePropertyPtr |
| 1695 | drm_get_prop(int fd, drmModeConnectorPtr connector, const char *name) |
| 1696 | { |
| 1697 | drmModePropertyPtr props; |
| 1698 | int i; |
| 1699 | |
| 1700 | for (i = 0; i < connector->count_props; i++) { |
| 1701 | props = drmModeGetProperty(fd, connector->props[i]); |
| 1702 | if (!props) |
| 1703 | continue; |
| 1704 | |
| 1705 | if (!strcmp(props->name, name)) |
| 1706 | return props; |
| 1707 | |
| 1708 | drmModeFreeProperty(props); |
| 1709 | } |
| 1710 | |
| 1711 | return NULL; |
| 1712 | } |
| 1713 | |
| 1714 | static void |
| 1715 | drm_set_dpms(struct weston_output *output_base, enum dpms_enum level) |
| 1716 | { |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 1717 | struct drm_output *output = to_drm_output(output_base); |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1718 | struct weston_compositor *ec = output_base->compositor; |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 1719 | struct drm_backend *b = to_drm_backend(ec); |
Daniel Stone | 36609c7 | 2015-06-18 07:49:02 +0100 | [diff] [blame] | 1720 | int ret; |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1721 | |
Ander Conselvan de Oliveira | a0a433a | 2013-06-04 16:24:04 +0300 | [diff] [blame] | 1722 | if (!output->dpms_prop) |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1723 | return; |
| 1724 | |
Daniel Stone | 36609c7 | 2015-06-18 07:49:02 +0100 | [diff] [blame] | 1725 | ret = drmModeConnectorSetProperty(b->drm.fd, output->connector_id, |
| 1726 | output->dpms_prop->prop_id, level); |
| 1727 | if (ret) { |
| 1728 | weston_log("DRM: DPMS: failed property set for %s\n", |
| 1729 | output->base.name); |
| 1730 | return; |
| 1731 | } |
| 1732 | |
| 1733 | output->dpms = level; |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1734 | } |
| 1735 | |
Pekka Paalanen | 3ce6362 | 2014-06-04 16:29:49 +0300 | [diff] [blame] | 1736 | static const char * const connector_type_names[] = { |
Pekka Paalanen | 89c49b3 | 2015-08-19 15:25:57 +0300 | [diff] [blame] | 1737 | [DRM_MODE_CONNECTOR_Unknown] = "Unknown", |
| 1738 | [DRM_MODE_CONNECTOR_VGA] = "VGA", |
| 1739 | [DRM_MODE_CONNECTOR_DVII] = "DVI-I", |
| 1740 | [DRM_MODE_CONNECTOR_DVID] = "DVI-D", |
| 1741 | [DRM_MODE_CONNECTOR_DVIA] = "DVI-A", |
| 1742 | [DRM_MODE_CONNECTOR_Composite] = "Composite", |
| 1743 | [DRM_MODE_CONNECTOR_SVIDEO] = "SVIDEO", |
| 1744 | [DRM_MODE_CONNECTOR_LVDS] = "LVDS", |
| 1745 | [DRM_MODE_CONNECTOR_Component] = "Component", |
| 1746 | [DRM_MODE_CONNECTOR_9PinDIN] = "DIN", |
| 1747 | [DRM_MODE_CONNECTOR_DisplayPort] = "DP", |
| 1748 | [DRM_MODE_CONNECTOR_HDMIA] = "HDMI-A", |
| 1749 | [DRM_MODE_CONNECTOR_HDMIB] = "HDMI-B", |
| 1750 | [DRM_MODE_CONNECTOR_TV] = "TV", |
| 1751 | [DRM_MODE_CONNECTOR_eDP] = "eDP", |
Pekka Paalanen | ab81f15 | 2015-08-24 14:27:07 +0300 | [diff] [blame] | 1752 | #ifdef DRM_MODE_CONNECTOR_DSI |
Pekka Paalanen | 89c49b3 | 2015-08-19 15:25:57 +0300 | [diff] [blame] | 1753 | [DRM_MODE_CONNECTOR_VIRTUAL] = "Virtual", |
| 1754 | [DRM_MODE_CONNECTOR_DSI] = "DSI", |
Pekka Paalanen | ab81f15 | 2015-08-24 14:27:07 +0300 | [diff] [blame] | 1755 | #endif |
Kristian Høgsberg | 2f9ed71 | 2012-07-26 17:57:15 -0400 | [diff] [blame] | 1756 | }; |
| 1757 | |
Pekka Paalanen | 3ce6362 | 2014-06-04 16:29:49 +0300 | [diff] [blame] | 1758 | static char * |
| 1759 | make_connector_name(const drmModeConnector *con) |
| 1760 | { |
| 1761 | char name[32]; |
Pekka Paalanen | 89c49b3 | 2015-08-19 15:25:57 +0300 | [diff] [blame] | 1762 | const char *type_name = NULL; |
Pekka Paalanen | 3ce6362 | 2014-06-04 16:29:49 +0300 | [diff] [blame] | 1763 | |
| 1764 | if (con->connector_type < ARRAY_LENGTH(connector_type_names)) |
| 1765 | type_name = connector_type_names[con->connector_type]; |
Pekka Paalanen | 89c49b3 | 2015-08-19 15:25:57 +0300 | [diff] [blame] | 1766 | |
| 1767 | if (!type_name) |
| 1768 | type_name = "UNNAMED"; |
| 1769 | |
| 1770 | snprintf(name, sizeof name, "%s-%d", type_name, con->connector_type_id); |
Pekka Paalanen | 3ce6362 | 2014-06-04 16:29:49 +0300 | [diff] [blame] | 1771 | |
| 1772 | return strdup(name); |
| 1773 | } |
| 1774 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1775 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1776 | find_crtc_for_connector(struct drm_backend *b, |
Kristian Høgsberg | 9ca3846 | 2012-07-26 22:44:55 -0400 | [diff] [blame] | 1777 | drmModeRes *resources, drmModeConnector *connector) |
| 1778 | { |
| 1779 | drmModeEncoder *encoder; |
| 1780 | uint32_t possible_crtcs; |
| 1781 | int i, j; |
| 1782 | |
| 1783 | for (j = 0; j < connector->count_encoders; j++) { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1784 | encoder = drmModeGetEncoder(b->drm.fd, connector->encoders[j]); |
Kristian Høgsberg | 9ca3846 | 2012-07-26 22:44:55 -0400 | [diff] [blame] | 1785 | if (encoder == NULL) { |
| 1786 | weston_log("Failed to get encoder.\n"); |
| 1787 | return -1; |
| 1788 | } |
| 1789 | possible_crtcs = encoder->possible_crtcs; |
| 1790 | drmModeFreeEncoder(encoder); |
| 1791 | |
| 1792 | for (i = 0; i < resources->count_crtcs; i++) { |
| 1793 | if (possible_crtcs & (1 << i) && |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1794 | !(b->crtc_allocator & (1 << resources->crtcs[i]))) |
Kristian Høgsberg | 9ca3846 | 2012-07-26 22:44:55 -0400 | [diff] [blame] | 1795 | return i; |
| 1796 | } |
| 1797 | } |
| 1798 | |
| 1799 | return -1; |
| 1800 | } |
| 1801 | |
Ander Conselvan de Oliveira | 475cf15 | 2012-12-14 13:37:29 -0200 | [diff] [blame] | 1802 | /* Init output state that depends on gl or gbm */ |
| 1803 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1804 | drm_output_init_egl(struct drm_output *output, struct drm_backend *b) |
Ander Conselvan de Oliveira | 475cf15 | 2012-12-14 13:37:29 -0200 | [diff] [blame] | 1805 | { |
Derek Foreman | c4cfe85 | 2015-05-15 12:12:40 -0500 | [diff] [blame] | 1806 | EGLint format[2] = { |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 1807 | output->gbm_format, |
| 1808 | fallback_format_for(output->gbm_format), |
Derek Foreman | c4cfe85 | 2015-05-15 12:12:40 -0500 | [diff] [blame] | 1809 | }; |
| 1810 | int i, flags, n_formats = 1; |
Ander Conselvan de Oliveira | 6c01c9c | 2012-12-14 13:37:30 -0200 | [diff] [blame] | 1811 | |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 1812 | output->gbm_surface = gbm_surface_create(b->gbm, |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 1813 | output->base.current_mode->width, |
| 1814 | output->base.current_mode->height, |
Derek Foreman | c4cfe85 | 2015-05-15 12:12:40 -0500 | [diff] [blame] | 1815 | format[0], |
Ander Conselvan de Oliveira | 475cf15 | 2012-12-14 13:37:29 -0200 | [diff] [blame] | 1816 | GBM_BO_USE_SCANOUT | |
| 1817 | GBM_BO_USE_RENDERING); |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 1818 | if (!output->gbm_surface) { |
Ander Conselvan de Oliveira | 475cf15 | 2012-12-14 13:37:29 -0200 | [diff] [blame] | 1819 | weston_log("failed to create gbm surface\n"); |
| 1820 | return -1; |
| 1821 | } |
| 1822 | |
Derek Foreman | c4cfe85 | 2015-05-15 12:12:40 -0500 | [diff] [blame] | 1823 | if (format[1]) |
| 1824 | n_formats = 2; |
Miguel A. Vico | c095cde | 2016-05-18 17:43:00 +0200 | [diff] [blame] | 1825 | if (gl_renderer->output_window_create(&output->base, |
| 1826 | (EGLNativeWindowType)output->gbm_surface, |
| 1827 | output->gbm_surface, |
| 1828 | gl_renderer->opaque_attribs, |
| 1829 | format, |
| 1830 | n_formats) < 0) { |
Ander Conselvan de Oliveira | 6c01c9c | 2012-12-14 13:37:30 -0200 | [diff] [blame] | 1831 | weston_log("failed to create gl renderer output state\n"); |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 1832 | gbm_surface_destroy(output->gbm_surface); |
Ander Conselvan de Oliveira | 475cf15 | 2012-12-14 13:37:29 -0200 | [diff] [blame] | 1833 | return -1; |
| 1834 | } |
| 1835 | |
Alvaro Fernando García | dce7c6e | 2014-07-28 18:30:17 -0300 | [diff] [blame] | 1836 | flags = GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE; |
Ander Conselvan de Oliveira | 6c01c9c | 2012-12-14 13:37:30 -0200 | [diff] [blame] | 1837 | |
| 1838 | for (i = 0; i < 2; i++) { |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 1839 | if (output->gbm_cursor_bo[i]) |
Ander Conselvan de Oliveira | 6c01c9c | 2012-12-14 13:37:30 -0200 | [diff] [blame] | 1840 | continue; |
| 1841 | |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 1842 | output->gbm_cursor_bo[i] = |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1843 | gbm_bo_create(b->gbm, b->cursor_width, b->cursor_height, |
Alvaro Fernando García | dce7c6e | 2014-07-28 18:30:17 -0300 | [diff] [blame] | 1844 | GBM_FORMAT_ARGB8888, flags); |
Ander Conselvan de Oliveira | 6c01c9c | 2012-12-14 13:37:30 -0200 | [diff] [blame] | 1845 | } |
| 1846 | |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 1847 | if (output->gbm_cursor_bo[0] == NULL || output->gbm_cursor_bo[1] == NULL) { |
Ander Conselvan de Oliveira | 475cf15 | 2012-12-14 13:37:29 -0200 | [diff] [blame] | 1848 | weston_log("cursor buffers unavailable, using gl cursors\n"); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1849 | b->cursors_are_broken = 1; |
Ander Conselvan de Oliveira | 475cf15 | 2012-12-14 13:37:29 -0200 | [diff] [blame] | 1850 | } |
| 1851 | |
| 1852 | return 0; |
| 1853 | } |
| 1854 | |
Daniel Stone | 3e661f7 | 2016-11-04 17:24:06 +0000 | [diff] [blame] | 1855 | static void |
| 1856 | drm_output_fini_egl(struct drm_output *output) |
| 1857 | { |
| 1858 | gl_renderer->output_destroy(&output->base); |
| 1859 | gbm_surface_destroy(output->gbm_surface); |
| 1860 | } |
| 1861 | |
Kristian Høgsberg | 9ca3846 | 2012-07-26 22:44:55 -0400 | [diff] [blame] | 1862 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1863 | drm_output_init_pixman(struct drm_output *output, struct drm_backend *b) |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1864 | { |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 1865 | int w = output->base.current_mode->width; |
| 1866 | int h = output->base.current_mode->height; |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 1867 | uint32_t format = output->gbm_format; |
| 1868 | uint32_t pixman_format; |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1869 | unsigned int i; |
| 1870 | |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 1871 | switch (format) { |
| 1872 | case GBM_FORMAT_XRGB8888: |
| 1873 | pixman_format = PIXMAN_x8r8g8b8; |
| 1874 | break; |
| 1875 | case GBM_FORMAT_RGB565: |
| 1876 | pixman_format = PIXMAN_r5g6b5; |
| 1877 | break; |
| 1878 | default: |
| 1879 | weston_log("Unsupported pixman format 0x%x\n", format); |
| 1880 | return -1; |
| 1881 | } |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1882 | |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 1883 | /* FIXME error checking */ |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1884 | for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) { |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 1885 | output->dumb[i] = drm_fb_create_dumb(b, w, h, format); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1886 | if (!output->dumb[i]) |
| 1887 | goto err; |
| 1888 | |
| 1889 | output->image[i] = |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 1890 | pixman_image_create_bits(pixman_format, w, h, |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1891 | output->dumb[i]->map, |
| 1892 | output->dumb[i]->stride); |
| 1893 | if (!output->image[i]) |
| 1894 | goto err; |
| 1895 | } |
| 1896 | |
| 1897 | if (pixman_renderer_output_create(&output->base) < 0) |
| 1898 | goto err; |
| 1899 | |
| 1900 | pixman_region32_init_rect(&output->previous_damage, |
Alexander Larsson | 0b13506 | 2013-05-28 16:23:36 +0200 | [diff] [blame] | 1901 | output->base.x, output->base.y, output->base.width, output->base.height); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1902 | |
| 1903 | return 0; |
| 1904 | |
| 1905 | err: |
| 1906 | for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) { |
| 1907 | if (output->dumb[i]) |
| 1908 | drm_fb_destroy_dumb(output->dumb[i]); |
| 1909 | if (output->image[i]) |
| 1910 | pixman_image_unref(output->image[i]); |
| 1911 | |
| 1912 | output->dumb[i] = NULL; |
| 1913 | output->image[i] = NULL; |
| 1914 | } |
| 1915 | |
| 1916 | return -1; |
| 1917 | } |
| 1918 | |
| 1919 | static void |
| 1920 | drm_output_fini_pixman(struct drm_output *output) |
| 1921 | { |
| 1922 | unsigned int i; |
| 1923 | |
| 1924 | pixman_renderer_output_destroy(&output->base); |
| 1925 | pixman_region32_fini(&output->previous_damage); |
| 1926 | |
| 1927 | for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) { |
| 1928 | drm_fb_destroy_dumb(output->dumb[i]); |
| 1929 | pixman_image_unref(output->image[i]); |
| 1930 | output->dumb[i] = NULL; |
| 1931 | output->image[i] = NULL; |
| 1932 | } |
| 1933 | } |
| 1934 | |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 1935 | static void |
| 1936 | edid_parse_string(const uint8_t *data, char text[]) |
| 1937 | { |
| 1938 | int i; |
| 1939 | int replaced = 0; |
| 1940 | |
| 1941 | /* this is always 12 bytes, but we can't guarantee it's null |
| 1942 | * terminated or not junk. */ |
| 1943 | strncpy(text, (const char *) data, 12); |
| 1944 | |
Bryce Harrington | 9c7de16 | 2015-08-28 13:04:26 -0700 | [diff] [blame] | 1945 | /* guarantee our new string is null-terminated */ |
| 1946 | text[12] = '\0'; |
| 1947 | |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 1948 | /* remove insane chars */ |
| 1949 | for (i = 0; text[i] != '\0'; i++) { |
| 1950 | if (text[i] == '\n' || |
| 1951 | text[i] == '\r') { |
| 1952 | text[i] = '\0'; |
| 1953 | break; |
| 1954 | } |
| 1955 | } |
| 1956 | |
| 1957 | /* ensure string is printable */ |
| 1958 | for (i = 0; text[i] != '\0'; i++) { |
| 1959 | if (!isprint(text[i])) { |
| 1960 | text[i] = '-'; |
| 1961 | replaced++; |
| 1962 | } |
| 1963 | } |
| 1964 | |
| 1965 | /* if the string is random junk, ignore the string */ |
| 1966 | if (replaced > 4) |
| 1967 | text[0] = '\0'; |
| 1968 | } |
| 1969 | |
| 1970 | #define EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING 0xfe |
| 1971 | #define EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME 0xfc |
| 1972 | #define EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER 0xff |
| 1973 | #define EDID_OFFSET_DATA_BLOCKS 0x36 |
| 1974 | #define EDID_OFFSET_LAST_BLOCK 0x6c |
| 1975 | #define EDID_OFFSET_PNPID 0x08 |
| 1976 | #define EDID_OFFSET_SERIAL 0x0c |
| 1977 | |
| 1978 | static int |
| 1979 | edid_parse(struct drm_edid *edid, const uint8_t *data, size_t length) |
| 1980 | { |
| 1981 | int i; |
| 1982 | uint32_t serial_number; |
| 1983 | |
| 1984 | /* check header */ |
| 1985 | if (length < 128) |
| 1986 | return -1; |
| 1987 | if (data[0] != 0x00 || data[1] != 0xff) |
| 1988 | return -1; |
| 1989 | |
| 1990 | /* decode the PNP ID from three 5 bit words packed into 2 bytes |
| 1991 | * /--08--\/--09--\ |
| 1992 | * 7654321076543210 |
| 1993 | * |\---/\---/\---/ |
| 1994 | * R C1 C2 C3 */ |
| 1995 | edid->pnp_id[0] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x7c) / 4) - 1; |
| 1996 | edid->pnp_id[1] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x3) * 8) + ((data[EDID_OFFSET_PNPID + 1] & 0xe0) / 32) - 1; |
| 1997 | edid->pnp_id[2] = 'A' + (data[EDID_OFFSET_PNPID + 1] & 0x1f) - 1; |
| 1998 | edid->pnp_id[3] = '\0'; |
| 1999 | |
| 2000 | /* maybe there isn't a ASCII serial number descriptor, so use this instead */ |
| 2001 | serial_number = (uint32_t) data[EDID_OFFSET_SERIAL + 0]; |
| 2002 | serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 1] * 0x100; |
| 2003 | serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 2] * 0x10000; |
| 2004 | serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 3] * 0x1000000; |
| 2005 | if (serial_number > 0) |
| 2006 | sprintf(edid->serial_number, "%lu", (unsigned long) serial_number); |
| 2007 | |
| 2008 | /* parse EDID data */ |
| 2009 | for (i = EDID_OFFSET_DATA_BLOCKS; |
| 2010 | i <= EDID_OFFSET_LAST_BLOCK; |
| 2011 | i += 18) { |
| 2012 | /* ignore pixel clock data */ |
| 2013 | if (data[i] != 0) |
| 2014 | continue; |
| 2015 | if (data[i+2] != 0) |
| 2016 | continue; |
| 2017 | |
| 2018 | /* any useful blocks? */ |
| 2019 | if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME) { |
| 2020 | edid_parse_string(&data[i+5], |
| 2021 | edid->monitor_name); |
| 2022 | } else if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER) { |
| 2023 | edid_parse_string(&data[i+5], |
| 2024 | edid->serial_number); |
| 2025 | } else if (data[i+3] == EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING) { |
| 2026 | edid_parse_string(&data[i+5], |
| 2027 | edid->eisa_id); |
| 2028 | } |
| 2029 | } |
| 2030 | return 0; |
| 2031 | } |
| 2032 | |
| 2033 | static void |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2034 | find_and_parse_output_edid(struct drm_backend *b, |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 2035 | struct drm_output *output, |
| 2036 | drmModeConnector *connector) |
| 2037 | { |
| 2038 | drmModePropertyBlobPtr edid_blob = NULL; |
| 2039 | drmModePropertyPtr property; |
| 2040 | int i; |
| 2041 | int rc; |
| 2042 | |
| 2043 | for (i = 0; i < connector->count_props && !edid_blob; i++) { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2044 | property = drmModeGetProperty(b->drm.fd, connector->props[i]); |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 2045 | if (!property) |
| 2046 | continue; |
| 2047 | if ((property->flags & DRM_MODE_PROP_BLOB) && |
| 2048 | !strcmp(property->name, "EDID")) { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2049 | edid_blob = drmModeGetPropertyBlob(b->drm.fd, |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 2050 | connector->prop_values[i]); |
| 2051 | } |
| 2052 | drmModeFreeProperty(property); |
| 2053 | } |
| 2054 | if (!edid_blob) |
| 2055 | return; |
| 2056 | |
| 2057 | rc = edid_parse(&output->edid, |
| 2058 | edid_blob->data, |
| 2059 | edid_blob->length); |
| 2060 | if (!rc) { |
| 2061 | weston_log("EDID data '%s', '%s', '%s'\n", |
| 2062 | output->edid.pnp_id, |
| 2063 | output->edid.monitor_name, |
| 2064 | output->edid.serial_number); |
| 2065 | if (output->edid.pnp_id[0] != '\0') |
| 2066 | output->base.make = output->edid.pnp_id; |
| 2067 | if (output->edid.monitor_name[0] != '\0') |
| 2068 | output->base.model = output->edid.monitor_name; |
| 2069 | if (output->edid.serial_number[0] != '\0') |
| 2070 | output->base.serial_number = output->edid.serial_number; |
| 2071 | } |
| 2072 | drmModeFreePropertyBlob(edid_blob); |
| 2073 | } |
| 2074 | |
Kristian Høgsberg | a30989a | 2013-05-23 17:23:15 -0400 | [diff] [blame] | 2075 | |
| 2076 | |
| 2077 | static int |
| 2078 | parse_modeline(const char *s, drmModeModeInfo *mode) |
| 2079 | { |
| 2080 | char hsync[16]; |
| 2081 | char vsync[16]; |
| 2082 | float fclock; |
| 2083 | |
| 2084 | mode->type = DRM_MODE_TYPE_USERDEF; |
| 2085 | mode->hskew = 0; |
| 2086 | mode->vscan = 0; |
| 2087 | mode->vrefresh = 0; |
| 2088 | mode->flags = 0; |
| 2089 | |
Rob Bradford | 307e09e | 2013-07-26 16:29:40 +0100 | [diff] [blame] | 2090 | if (sscanf(s, "%f %hd %hd %hd %hd %hd %hd %hd %hd %15s %15s", |
Kristian Høgsberg | a30989a | 2013-05-23 17:23:15 -0400 | [diff] [blame] | 2091 | &fclock, |
| 2092 | &mode->hdisplay, |
| 2093 | &mode->hsync_start, |
| 2094 | &mode->hsync_end, |
| 2095 | &mode->htotal, |
| 2096 | &mode->vdisplay, |
| 2097 | &mode->vsync_start, |
| 2098 | &mode->vsync_end, |
| 2099 | &mode->vtotal, hsync, vsync) != 11) |
| 2100 | return -1; |
| 2101 | |
| 2102 | mode->clock = fclock * 1000; |
| 2103 | if (strcmp(hsync, "+hsync") == 0) |
| 2104 | mode->flags |= DRM_MODE_FLAG_PHSYNC; |
| 2105 | else if (strcmp(hsync, "-hsync") == 0) |
| 2106 | mode->flags |= DRM_MODE_FLAG_NHSYNC; |
| 2107 | else |
| 2108 | return -1; |
| 2109 | |
| 2110 | if (strcmp(vsync, "+vsync") == 0) |
| 2111 | mode->flags |= DRM_MODE_FLAG_PVSYNC; |
| 2112 | else if (strcmp(vsync, "-vsync") == 0) |
| 2113 | mode->flags |= DRM_MODE_FLAG_NVSYNC; |
| 2114 | else |
| 2115 | return -1; |
| 2116 | |
Emmanuel Gil Peyrot | a62138b | 2016-05-02 22:40:11 +0100 | [diff] [blame] | 2117 | snprintf(mode->name, sizeof mode->name, "%dx%d@%.3f", |
| 2118 | mode->hdisplay, mode->vdisplay, fclock); |
| 2119 | |
Kristian Høgsberg | a30989a | 2013-05-23 17:23:15 -0400 | [diff] [blame] | 2120 | return 0; |
| 2121 | } |
| 2122 | |
Rob Bradford | 66bd9f5 | 2013-06-25 18:56:42 +0100 | [diff] [blame] | 2123 | static void |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2124 | setup_output_seat_constraint(struct drm_backend *b, |
Rob Bradford | 66bd9f5 | 2013-06-25 18:56:42 +0100 | [diff] [blame] | 2125 | struct weston_output *output, |
| 2126 | const char *s) |
| 2127 | { |
| 2128 | if (strcmp(s, "") != 0) { |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 2129 | struct weston_pointer *pointer; |
Rob Bradford | 66bd9f5 | 2013-06-25 18:56:42 +0100 | [diff] [blame] | 2130 | struct udev_seat *seat; |
| 2131 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2132 | seat = udev_seat_get_named(&b->input, s); |
Derek Foreman | 0720ea3 | 2015-07-15 13:00:35 -0500 | [diff] [blame] | 2133 | if (!seat) |
| 2134 | return; |
Rob Bradford | 66bd9f5 | 2013-06-25 18:56:42 +0100 | [diff] [blame] | 2135 | |
Derek Foreman | 0720ea3 | 2015-07-15 13:00:35 -0500 | [diff] [blame] | 2136 | seat->base.output = output; |
| 2137 | |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 2138 | pointer = weston_seat_get_pointer(&seat->base); |
| 2139 | if (pointer) |
| 2140 | weston_pointer_clamp(pointer, |
| 2141 | &pointer->x, |
| 2142 | &pointer->y); |
Rob Bradford | 66bd9f5 | 2013-06-25 18:56:42 +0100 | [diff] [blame] | 2143 | } |
| 2144 | } |
| 2145 | |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 2146 | static int |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 2147 | parse_gbm_format(const char *s, uint32_t default_value, uint32_t *gbm_format) |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 2148 | { |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 2149 | int ret = 0; |
| 2150 | |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 2151 | if (s == NULL) |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 2152 | *gbm_format = default_value; |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 2153 | else if (strcmp(s, "xrgb8888") == 0) |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 2154 | *gbm_format = GBM_FORMAT_XRGB8888; |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 2155 | else if (strcmp(s, "rgb565") == 0) |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 2156 | *gbm_format = GBM_FORMAT_RGB565; |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 2157 | else if (strcmp(s, "xrgb2101010") == 0) |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 2158 | *gbm_format = GBM_FORMAT_XRGB2101010; |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 2159 | else { |
| 2160 | weston_log("fatal: unrecognized pixel format: %s\n", s); |
| 2161 | ret = -1; |
| 2162 | } |
| 2163 | |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 2164 | return ret; |
| 2165 | } |
| 2166 | |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 2167 | /** |
| 2168 | * Choose suitable mode for an output |
| 2169 | * |
| 2170 | * Find the most suitable mode to use for initial setup (or reconfiguration on |
| 2171 | * hotplug etc) for a DRM output. |
| 2172 | * |
| 2173 | * @param output DRM output to choose mode for |
| 2174 | * @param kind Strategy and preference to use when choosing mode |
| 2175 | * @param width Desired width for this output |
| 2176 | * @param height Desired height for this output |
| 2177 | * @param current_mode Mode currently being displayed on this output |
| 2178 | * @param modeline Manually-entered mode (may be NULL) |
| 2179 | * @returns A mode from the output's mode list, or NULL if none available |
| 2180 | */ |
| 2181 | static struct drm_mode * |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 2182 | drm_output_choose_initial_mode(struct drm_backend *backend, |
| 2183 | struct drm_output *output, |
| 2184 | enum weston_drm_backend_output_mode mode, |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2185 | const char *modeline, |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 2186 | const drmModeModeInfo *current_mode) |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 2187 | { |
| 2188 | struct drm_mode *preferred = NULL; |
| 2189 | struct drm_mode *current = NULL; |
| 2190 | struct drm_mode *configured = NULL; |
| 2191 | struct drm_mode *best = NULL; |
| 2192 | struct drm_mode *drm_mode; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2193 | drmModeModeInfo drm_modeline; |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 2194 | int32_t width = 0; |
| 2195 | int32_t height = 0; |
| 2196 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2197 | if (mode == WESTON_DRM_BACKEND_OUTPUT_PREFERRED && modeline) { |
| 2198 | if (sscanf(modeline, "%dx%d", &width, &height) != 2) { |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 2199 | width = -1; |
| 2200 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2201 | if (parse_modeline(modeline, &drm_modeline) == 0) { |
| 2202 | configured = drm_output_add_mode(output, &drm_modeline); |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 2203 | if (!configured) |
| 2204 | return NULL; |
| 2205 | } else { |
| 2206 | weston_log("Invalid modeline \"%s\" for output %s\n", |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2207 | modeline, output->base.name); |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 2208 | } |
| 2209 | } |
| 2210 | } |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 2211 | |
| 2212 | wl_list_for_each_reverse(drm_mode, &output->base.mode_list, base.link) { |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 2213 | if (width == drm_mode->base.width && |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 2214 | height == drm_mode->base.height) |
| 2215 | configured = drm_mode; |
| 2216 | |
comic fans | 7a5c562 | 2016-03-17 14:29:27 +0200 | [diff] [blame] | 2217 | if (memcmp(current_mode, &drm_mode->mode_info, |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 2218 | sizeof *current_mode) == 0) |
| 2219 | current = drm_mode; |
| 2220 | |
| 2221 | if (drm_mode->base.flags & WL_OUTPUT_MODE_PREFERRED) |
| 2222 | preferred = drm_mode; |
| 2223 | |
| 2224 | best = drm_mode; |
| 2225 | } |
| 2226 | |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 2227 | if (current == NULL && current_mode->clock != 0) { |
| 2228 | current = drm_output_add_mode(output, current_mode); |
| 2229 | if (!current) |
| 2230 | return NULL; |
| 2231 | } |
| 2232 | |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 2233 | if (mode == WESTON_DRM_BACKEND_OUTPUT_CURRENT) |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 2234 | configured = current; |
| 2235 | |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 2236 | if (configured) |
| 2237 | return configured; |
| 2238 | |
| 2239 | if (preferred) |
| 2240 | return preferred; |
| 2241 | |
| 2242 | if (current) |
| 2243 | return current; |
| 2244 | |
| 2245 | if (best) |
| 2246 | return best; |
| 2247 | |
| 2248 | weston_log("no available modes for %s\n", output->base.name); |
| 2249 | return NULL; |
| 2250 | } |
| 2251 | |
Pekka Paalanen | eee580b | 2014-06-04 16:43:06 +0300 | [diff] [blame] | 2252 | static int |
| 2253 | connector_get_current_mode(drmModeConnector *connector, int drm_fd, |
| 2254 | drmModeModeInfo *mode) |
| 2255 | { |
| 2256 | drmModeEncoder *encoder; |
| 2257 | drmModeCrtc *crtc; |
| 2258 | |
| 2259 | /* Get the current mode on the crtc that's currently driving |
| 2260 | * this connector. */ |
| 2261 | encoder = drmModeGetEncoder(drm_fd, connector->encoder_id); |
| 2262 | memset(mode, 0, sizeof *mode); |
| 2263 | if (encoder != NULL) { |
| 2264 | crtc = drmModeGetCrtc(drm_fd, encoder->crtc_id); |
| 2265 | drmModeFreeEncoder(encoder); |
| 2266 | if (crtc == NULL) |
| 2267 | return -1; |
| 2268 | if (crtc->mode_valid) |
| 2269 | *mode = crtc->mode; |
| 2270 | drmModeFreeCrtc(crtc); |
| 2271 | } |
| 2272 | |
| 2273 | return 0; |
| 2274 | } |
| 2275 | |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 2276 | static int |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2277 | drm_output_set_mode(struct weston_output *base, |
| 2278 | enum weston_drm_backend_output_mode mode, |
| 2279 | const char *modeline) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 2280 | { |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2281 | struct drm_output *output = to_drm_output(base); |
| 2282 | struct drm_backend *b = to_drm_backend(base->compositor); |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 2283 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2284 | struct drm_mode *drm_mode, *next, *current; |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 2285 | drmModeModeInfo crtc_mode; |
| 2286 | int i; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 2287 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 2288 | output->base.make = "unknown"; |
| 2289 | output->base.model = "unknown"; |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 2290 | output->base.serial_number = "unknown"; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 2291 | wl_list_init(&output->base.mode_list); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 2292 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2293 | output->original_crtc = drmModeGetCrtc(b->drm.fd, output->crtc_id); |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 2294 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2295 | if (connector_get_current_mode(output->connector, b->drm.fd, &crtc_mode) < 0) |
Pekka Paalanen | eee580b | 2014-06-04 16:43:06 +0300 | [diff] [blame] | 2296 | goto err_free; |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 2297 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2298 | for (i = 0; i < output->connector->count_modes; i++) { |
| 2299 | drm_mode = drm_output_add_mode(output, &output->connector->modes[i]); |
Ander Conselvan de Oliveira | 42c4646 | 2012-08-09 16:45:00 +0300 | [diff] [blame] | 2300 | if (!drm_mode) |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 2301 | goto err_free; |
| 2302 | } |
| 2303 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2304 | current = drm_output_choose_initial_mode(b, output, mode, modeline, &crtc_mode); |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 2305 | if (!current) |
Wang Quanxian | acb805a | 2012-07-30 18:09:46 -0400 | [diff] [blame] | 2306 | goto err_free; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2307 | |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 2308 | output->base.current_mode = ¤t->base; |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 2309 | output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT; |
Wang Quanxian | acb805a | 2012-07-30 18:09:46 -0400 | [diff] [blame] | 2310 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2311 | /* Set native_ fields, so weston_output_mode_switch_to_native() works */ |
| 2312 | output->base.native_mode = output->base.current_mode; |
| 2313 | output->base.native_scale = output->base.current_scale; |
| 2314 | |
| 2315 | output->base.mm_width = output->connector->mmWidth; |
| 2316 | output->base.mm_height = output->connector->mmHeight; |
| 2317 | |
| 2318 | return 0; |
| 2319 | |
| 2320 | err_free: |
| 2321 | drmModeFreeCrtc(output->original_crtc); |
| 2322 | output->original_crtc = NULL; |
| 2323 | |
| 2324 | wl_list_for_each_safe(drm_mode, next, &output->base.mode_list, |
| 2325 | base.link) { |
| 2326 | wl_list_remove(&drm_mode->base.link); |
| 2327 | free(drm_mode); |
| 2328 | } |
| 2329 | |
| 2330 | return -1; |
| 2331 | } |
| 2332 | |
| 2333 | static void |
| 2334 | drm_output_set_gbm_format(struct weston_output *base, |
| 2335 | const char *gbm_format) |
| 2336 | { |
| 2337 | struct drm_output *output = to_drm_output(base); |
| 2338 | struct drm_backend *b = to_drm_backend(base->compositor); |
| 2339 | |
| 2340 | if (parse_gbm_format(gbm_format, b->gbm_format, &output->gbm_format) == -1) |
| 2341 | output->gbm_format = b->gbm_format; |
| 2342 | } |
| 2343 | |
| 2344 | static void |
| 2345 | drm_output_set_seat(struct weston_output *base, |
| 2346 | const char *seat) |
| 2347 | { |
| 2348 | struct drm_output *output = to_drm_output(base); |
| 2349 | struct drm_backend *b = to_drm_backend(base->compositor); |
| 2350 | |
| 2351 | setup_output_seat_constraint(b, &output->base, |
| 2352 | seat ? seat : ""); |
| 2353 | } |
| 2354 | |
| 2355 | static int |
| 2356 | drm_output_enable(struct weston_output *base) |
| 2357 | { |
| 2358 | struct drm_output *output = to_drm_output(base); |
| 2359 | struct drm_backend *b = to_drm_backend(base->compositor); |
| 2360 | struct weston_mode *m; |
| 2361 | |
| 2362 | output->dpms_prop = drm_get_prop(b->drm.fd, output->connector, "DPMS"); |
John Kåre Alsaker | 9465927 | 2012-11-13 19:10:18 +0100 | [diff] [blame] | 2363 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2364 | if (b->use_pixman) { |
| 2365 | if (drm_output_init_pixman(output, b) < 0) { |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 2366 | weston_log("Failed to init output pixman state\n"); |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2367 | goto err_free; |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 2368 | } |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2369 | } else if (drm_output_init_egl(output, b) < 0) { |
Ander Conselvan de Oliveira | 475cf15 | 2012-12-14 13:37:29 -0200 | [diff] [blame] | 2370 | weston_log("Failed to init output gl state\n"); |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2371 | goto err_free; |
Kristian Høgsberg | 1d1e0a5 | 2012-10-21 13:29:26 -0400 | [diff] [blame] | 2372 | } |
Kristian Høgsberg | 8e1f77f | 2012-05-03 11:39:35 -0400 | [diff] [blame] | 2373 | |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 2374 | if (output->backlight) { |
Kristian Høgsberg | 220819f | 2013-05-23 20:29:40 -0400 | [diff] [blame] | 2375 | weston_log("Initialized backlight, device %s\n", |
| 2376 | output->backlight->path); |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 2377 | output->base.set_backlight = drm_set_backlight; |
| 2378 | output->base.backlight_current = drm_get_backlight(output); |
Kristian Høgsberg | 220819f | 2013-05-23 20:29:40 -0400 | [diff] [blame] | 2379 | } else { |
| 2380 | weston_log("Failed to initialize backlight\n"); |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 2381 | } |
| 2382 | |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 2383 | output->base.start_repaint_loop = drm_output_start_repaint_loop; |
Kristian Høgsberg | 68c479a | 2012-01-25 23:32:28 -0500 | [diff] [blame] | 2384 | output->base.repaint = drm_output_repaint; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2385 | output->base.assign_planes = drm_assign_planes; |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 2386 | output->base.set_dpms = drm_set_dpms; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 2387 | output->base.switch_mode = drm_output_switch_mode; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 2388 | |
Richard Hughes | e729996 | 2013-05-01 21:52:12 +0100 | [diff] [blame] | 2389 | output->base.gamma_size = output->original_crtc->gamma_size; |
| 2390 | output->base.set_gamma = drm_output_set_gamma; |
| 2391 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2392 | output->base.subpixel = drm_subpixel_to_wayland(output->connector->subpixel); |
| 2393 | |
| 2394 | find_and_parse_output_edid(b, output, output->connector); |
| 2395 | if (output->connector->connector_type == DRM_MODE_CONNECTOR_LVDS) |
| 2396 | output->base.connection_internal = 1; |
| 2397 | |
Derek Foreman | b042756 | 2016-02-05 15:55:20 -0600 | [diff] [blame] | 2398 | weston_plane_init(&output->cursor_plane, b->compositor, |
| 2399 | INT32_MIN, INT32_MIN); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2400 | weston_plane_init(&output->fb_plane, b->compositor, 0, 0); |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 2401 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2402 | weston_compositor_stack_plane(b->compositor, &output->cursor_plane, NULL); |
| 2403 | weston_compositor_stack_plane(b->compositor, &output->fb_plane, |
| 2404 | &b->compositor->primary_plane); |
Ander Conselvan de Oliveira | 8ad1982 | 2013-03-05 17:30:27 +0200 | [diff] [blame] | 2405 | |
Kristian Høgsberg | 2f9ed71 | 2012-07-26 17:57:15 -0400 | [diff] [blame] | 2406 | weston_log("Output %s, (connector %d, crtc %d)\n", |
Richard Hughes | afe690c | 2013-05-02 10:10:04 +0100 | [diff] [blame] | 2407 | output->base.name, output->connector_id, output->crtc_id); |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 2408 | wl_list_for_each(m, &output->base.mode_list, link) |
U. Artie Eoff | d3ed6cb | 2014-01-10 10:15:17 -0800 | [diff] [blame] | 2409 | weston_log_continue(STAMP_SPACE "mode %dx%d@%.1f%s%s%s\n", |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 2410 | m->width, m->height, m->refresh / 1000.0, |
| 2411 | m->flags & WL_OUTPUT_MODE_PREFERRED ? |
| 2412 | ", preferred" : "", |
| 2413 | m->flags & WL_OUTPUT_MODE_CURRENT ? |
| 2414 | ", current" : "", |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2415 | output->connector->count_modes == 0 ? |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 2416 | ", built-in" : ""); |
Kristian Høgsberg | fc9c5e0 | 2012-06-08 16:45:33 -0400 | [diff] [blame] | 2417 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 2418 | return 0; |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 2419 | |
| 2420 | err_free: |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2421 | drmModeFreeProperty(output->dpms_prop); |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 2422 | |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 2423 | return -1; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 2424 | } |
| 2425 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2426 | static void |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2427 | drm_output_deinit(struct weston_output *base) |
| 2428 | { |
| 2429 | struct drm_output *output = to_drm_output(base); |
| 2430 | struct drm_backend *b = to_drm_backend(base->compositor); |
| 2431 | |
Daniel Stone | 3e661f7 | 2016-11-04 17:24:06 +0000 | [diff] [blame] | 2432 | if (b->use_pixman) |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2433 | drm_output_fini_pixman(output); |
Daniel Stone | 3e661f7 | 2016-11-04 17:24:06 +0000 | [diff] [blame] | 2434 | else |
| 2435 | drm_output_fini_egl(output); |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2436 | |
| 2437 | weston_plane_release(&output->fb_plane); |
| 2438 | weston_plane_release(&output->cursor_plane); |
| 2439 | |
| 2440 | drmModeFreeProperty(output->dpms_prop); |
| 2441 | |
| 2442 | /* Turn off hardware cursor */ |
| 2443 | drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0); |
| 2444 | } |
| 2445 | |
| 2446 | static void |
| 2447 | drm_output_destroy(struct weston_output *base) |
| 2448 | { |
| 2449 | struct drm_output *output = to_drm_output(base); |
| 2450 | struct drm_backend *b = to_drm_backend(base->compositor); |
| 2451 | drmModeCrtcPtr origcrtc = output->original_crtc; |
| 2452 | |
| 2453 | if (output->page_flip_pending) { |
| 2454 | output->destroy_pending = 1; |
| 2455 | weston_log("destroy output while page flip pending\n"); |
| 2456 | return; |
| 2457 | } |
| 2458 | |
| 2459 | if (output->base.enabled) |
| 2460 | drm_output_deinit(&output->base); |
| 2461 | |
| 2462 | if (origcrtc) { |
| 2463 | /* Restore original CRTC state */ |
| 2464 | drmModeSetCrtc(b->drm.fd, origcrtc->crtc_id, origcrtc->buffer_id, |
| 2465 | origcrtc->x, origcrtc->y, |
| 2466 | &output->connector_id, 1, &origcrtc->mode); |
| 2467 | drmModeFreeCrtc(origcrtc); |
| 2468 | } |
| 2469 | |
| 2470 | weston_output_destroy(&output->base); |
| 2471 | |
| 2472 | drmModeFreeConnector(output->connector); |
| 2473 | |
| 2474 | if (output->backlight) |
| 2475 | backlight_destroy(output->backlight); |
| 2476 | |
| 2477 | b->crtc_allocator &= ~(1 << output->crtc_id); |
| 2478 | b->connector_allocator &= ~(1 << output->connector_id); |
| 2479 | |
| 2480 | free(output); |
| 2481 | } |
| 2482 | |
| 2483 | static int |
| 2484 | drm_output_disable(struct weston_output *base) |
| 2485 | { |
| 2486 | struct drm_output *output = to_drm_output(base); |
| 2487 | struct drm_backend *b = to_drm_backend(base->compositor); |
| 2488 | |
| 2489 | if (output->page_flip_pending) { |
| 2490 | output->disable_pending = 1; |
| 2491 | return -1; |
| 2492 | } |
| 2493 | |
| 2494 | if (output->base.enabled) |
| 2495 | drm_output_deinit(&output->base); |
| 2496 | |
| 2497 | output->disable_pending = 0; |
| 2498 | |
| 2499 | weston_log("Disabling output %s\n", output->base.name); |
| 2500 | drmModeSetCrtc(b->drm.fd, output->crtc_id, |
| 2501 | 0, 0, 0, 0, 0, NULL); |
| 2502 | |
| 2503 | return 0; |
| 2504 | } |
| 2505 | |
| 2506 | /** |
| 2507 | * Create a Weston output structure |
| 2508 | * |
| 2509 | * Given a DRM connector, create a matching drm_output structure and add it |
| 2510 | * to Weston's output list. It also takes ownership of the connector, which |
| 2511 | * is released when output is destroyed. |
| 2512 | * |
| 2513 | * @param b Weston backend structure |
| 2514 | * @param resources DRM resources for this device |
| 2515 | * @param connector DRM connector to use for this new output |
| 2516 | * @param drm_device udev device pointer |
| 2517 | * @returns 0 on success, or -1 on failure |
| 2518 | */ |
| 2519 | static int |
| 2520 | create_output_for_connector(struct drm_backend *b, |
| 2521 | drmModeRes *resources, |
| 2522 | drmModeConnector *connector, |
| 2523 | struct udev_device *drm_device) |
| 2524 | { |
| 2525 | struct drm_output *output; |
| 2526 | int i; |
| 2527 | |
| 2528 | i = find_crtc_for_connector(b, resources, connector); |
| 2529 | if (i < 0) { |
| 2530 | weston_log("No usable crtc/encoder pair for connector.\n"); |
| 2531 | return -1; |
| 2532 | } |
| 2533 | |
| 2534 | output = zalloc(sizeof *output); |
| 2535 | if (output == NULL) |
| 2536 | return -1; |
| 2537 | |
| 2538 | output->connector = connector; |
| 2539 | output->crtc_id = resources->crtcs[i]; |
| 2540 | output->pipe = i; |
| 2541 | output->connector_id = connector->connector_id; |
| 2542 | |
| 2543 | output->backlight = backlight_init(drm_device, |
| 2544 | connector->connector_type); |
| 2545 | |
| 2546 | output->base.enable = drm_output_enable; |
| 2547 | output->base.destroy = drm_output_destroy; |
| 2548 | output->base.disable = drm_output_disable; |
| 2549 | output->base.name = make_connector_name(connector); |
| 2550 | |
| 2551 | output->destroy_pending = 0; |
| 2552 | output->disable_pending = 0; |
| 2553 | output->original_crtc = NULL; |
| 2554 | |
| 2555 | b->crtc_allocator |= (1 << output->crtc_id); |
| 2556 | b->connector_allocator |= (1 << output->connector_id); |
| 2557 | |
Armin Krezović | 4008740 | 2016-09-30 14:11:12 +0200 | [diff] [blame] | 2558 | weston_output_init(&output->base, b->compositor); |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2559 | weston_compositor_add_pending_output(&output->base, b->compositor); |
| 2560 | |
| 2561 | return 0; |
| 2562 | } |
| 2563 | |
| 2564 | static void |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2565 | create_sprites(struct drm_backend *b) |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2566 | { |
| 2567 | struct drm_sprite *sprite; |
| 2568 | drmModePlaneRes *plane_res; |
| 2569 | drmModePlane *plane; |
Kristian Høgsberg | 875ab9e | 2012-03-30 11:52:39 -0400 | [diff] [blame] | 2570 | uint32_t i; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2571 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2572 | plane_res = drmModeGetPlaneResources(b->drm.fd); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2573 | if (!plane_res) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 2574 | weston_log("failed to get plane resources: %s\n", |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2575 | strerror(errno)); |
| 2576 | return; |
| 2577 | } |
| 2578 | |
| 2579 | for (i = 0; i < plane_res->count_planes; i++) { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2580 | plane = drmModeGetPlane(b->drm.fd, plane_res->planes[i]); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2581 | if (!plane) |
| 2582 | continue; |
| 2583 | |
Peter Hutterer | f3d6227 | 2013-08-08 11:57:05 +1000 | [diff] [blame] | 2584 | sprite = zalloc(sizeof(*sprite) + ((sizeof(uint32_t)) * |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2585 | plane->count_formats)); |
| 2586 | if (!sprite) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 2587 | weston_log("%s: out of memory\n", |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2588 | __func__); |
Chris Michael | 8b37687 | 2014-01-02 11:39:40 +0000 | [diff] [blame] | 2589 | drmModeFreePlane(plane); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2590 | continue; |
| 2591 | } |
| 2592 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2593 | sprite->possible_crtcs = plane->possible_crtcs; |
| 2594 | sprite->plane_id = plane->plane_id; |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 2595 | sprite->current = NULL; |
| 2596 | sprite->next = NULL; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2597 | sprite->backend = b; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2598 | sprite->count_formats = plane->count_formats; |
| 2599 | memcpy(sprite->formats, plane->formats, |
Rob Clark | 8efbc8e | 2012-03-11 19:48:44 -0500 | [diff] [blame] | 2600 | plane->count_formats * sizeof(plane->formats[0])); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2601 | drmModeFreePlane(plane); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2602 | weston_plane_init(&sprite->plane, b->compositor, 0, 0); |
| 2603 | weston_compositor_stack_plane(b->compositor, &sprite->plane, |
| 2604 | &b->compositor->primary_plane); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2605 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2606 | wl_list_insert(&b->sprite_list, &sprite->link); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2607 | } |
| 2608 | |
Samuel Iglesias Gonsalvez | de46673 | 2013-06-13 10:03:33 +0200 | [diff] [blame] | 2609 | drmModeFreePlaneResources(plane_res); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2610 | } |
| 2611 | |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 2612 | static void |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2613 | destroy_sprites(struct drm_backend *backend) |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 2614 | { |
| 2615 | struct drm_sprite *sprite, *next; |
| 2616 | struct drm_output *output; |
| 2617 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2618 | output = container_of(backend->compositor->output_list.next, |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 2619 | struct drm_output, base.link); |
| 2620 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2621 | wl_list_for_each_safe(sprite, next, &backend->sprite_list, link) { |
| 2622 | drmModeSetPlane(backend->drm.fd, |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 2623 | sprite->plane_id, |
| 2624 | output->crtc_id, 0, 0, |
| 2625 | 0, 0, 0, 0, 0, 0, 0, 0); |
Ander Conselvan de Oliveira | 526d461 | 2013-01-25 15:13:03 +0200 | [diff] [blame] | 2626 | drm_output_release_fb(output, sprite->current); |
| 2627 | drm_output_release_fb(output, sprite->next); |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 2628 | weston_plane_release(&sprite->plane); |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 2629 | free(sprite); |
| 2630 | } |
| 2631 | } |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2632 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 2633 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2634 | create_outputs(struct drm_backend *b, uint32_t option_connector, |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 2635 | struct udev_device *drm_device) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 2636 | { |
| 2637 | drmModeConnector *connector; |
| 2638 | drmModeRes *resources; |
| 2639 | int i; |
| 2640 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2641 | resources = drmModeGetResources(b->drm.fd); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 2642 | if (!resources) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 2643 | weston_log("drmModeGetResources failed\n"); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 2644 | return -1; |
| 2645 | } |
| 2646 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2647 | b->min_width = resources->min_width; |
| 2648 | b->max_width = resources->max_width; |
| 2649 | b->min_height = resources->min_height; |
| 2650 | b->max_height = resources->max_height; |
Rob Clark | 4339add | 2012-08-09 14:18:28 -0500 | [diff] [blame] | 2651 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 2652 | for (i = 0; i < resources->count_connectors; i++) { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2653 | connector = drmModeGetConnector(b->drm.fd, |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 2654 | resources->connectors[i]); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 2655 | if (connector == NULL) |
| 2656 | continue; |
| 2657 | |
| 2658 | if (connector->connection == DRM_MODE_CONNECTED && |
| 2659 | (option_connector == 0 || |
Benjamin Franzke | 9eaee35 | 2011-08-02 13:03:54 +0200 | [diff] [blame] | 2660 | connector->connector_id == option_connector)) { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2661 | if (create_output_for_connector(b, resources, |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2662 | connector, drm_device) < 0) { |
Benjamin Franzke | 439d986 | 2011-10-07 08:20:53 +0200 | [diff] [blame] | 2663 | drmModeFreeConnector(connector); |
| 2664 | continue; |
| 2665 | } |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2666 | } else { |
| 2667 | drmModeFreeConnector(connector); |
Benjamin Franzke | 9eaee35 | 2011-08-02 13:03:54 +0200 | [diff] [blame] | 2668 | } |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 2669 | } |
| 2670 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2671 | if (wl_list_empty(&b->compositor->output_list) && |
| 2672 | wl_list_empty(&b->compositor->pending_output_list)) |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 2673 | weston_log("No currently active connector found.\n"); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 2674 | |
| 2675 | drmModeFreeResources(resources); |
| 2676 | |
| 2677 | return 0; |
| 2678 | } |
| 2679 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2680 | static void |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2681 | update_outputs(struct drm_backend *b, struct udev_device *drm_device) |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2682 | { |
| 2683 | drmModeConnector *connector; |
| 2684 | drmModeRes *resources; |
| 2685 | struct drm_output *output, *next; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2686 | uint32_t connected = 0, disconnects = 0; |
| 2687 | int i; |
| 2688 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2689 | resources = drmModeGetResources(b->drm.fd); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2690 | if (!resources) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 2691 | weston_log("drmModeGetResources failed\n"); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2692 | return; |
| 2693 | } |
| 2694 | |
| 2695 | /* collect new connects */ |
| 2696 | for (i = 0; i < resources->count_connectors; i++) { |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 2697 | int connector_id = resources->connectors[i]; |
| 2698 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2699 | connector = drmModeGetConnector(b->drm.fd, connector_id); |
David Herrmann | 7551cff | 2011-12-08 17:05:43 +0100 | [diff] [blame] | 2700 | if (connector == NULL) |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2701 | continue; |
| 2702 | |
David Herrmann | 7551cff | 2011-12-08 17:05:43 +0100 | [diff] [blame] | 2703 | if (connector->connection != DRM_MODE_CONNECTED) { |
| 2704 | drmModeFreeConnector(connector); |
| 2705 | continue; |
| 2706 | } |
| 2707 | |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 2708 | connected |= (1 << connector_id); |
| 2709 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2710 | if (!(b->connector_allocator & (1 << connector_id))) { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2711 | create_output_for_connector(b, resources, |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2712 | connector, drm_device); |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 2713 | weston_log("connector %d connected\n", connector_id); |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 2714 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2715 | } else { |
| 2716 | drmModeFreeConnector(connector); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2717 | } |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2718 | } |
| 2719 | drmModeFreeResources(resources); |
| 2720 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2721 | disconnects = b->connector_allocator & ~connected; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2722 | if (disconnects) { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2723 | wl_list_for_each_safe(output, next, &b->compositor->output_list, |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2724 | base.link) { |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2725 | if (disconnects & (1 << output->connector_id)) { |
| 2726 | disconnects &= ~(1 << output->connector_id); |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 2727 | weston_log("connector %d disconnected\n", |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2728 | output->connector_id); |
Benjamin Franzke | 48c4ea2 | 2011-08-30 11:44:56 +0200 | [diff] [blame] | 2729 | drm_output_destroy(&output->base); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2730 | } |
| 2731 | } |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2732 | |
| 2733 | wl_list_for_each_safe(output, next, &b->compositor->pending_output_list, |
| 2734 | base.link) { |
| 2735 | if (disconnects & (1 << output->connector_id)) { |
| 2736 | disconnects &= ~(1 << output->connector_id); |
| 2737 | weston_log("connector %d disconnected\n", |
| 2738 | output->connector_id); |
| 2739 | drm_output_destroy(&output->base); |
| 2740 | } |
| 2741 | } |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2742 | } |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2743 | } |
| 2744 | |
| 2745 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2746 | udev_event_is_hotplug(struct drm_backend *b, struct udev_device *device) |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2747 | { |
David Herrmann | d7488c2 | 2012-03-11 20:05:21 +0100 | [diff] [blame] | 2748 | const char *sysnum; |
David Herrmann | 6ac52db | 2012-03-11 20:05:22 +0100 | [diff] [blame] | 2749 | const char *val; |
David Herrmann | d7488c2 | 2012-03-11 20:05:21 +0100 | [diff] [blame] | 2750 | |
| 2751 | sysnum = udev_device_get_sysnum(device); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2752 | if (!sysnum || atoi(sysnum) != b->drm.id) |
David Herrmann | d7488c2 | 2012-03-11 20:05:21 +0100 | [diff] [blame] | 2753 | return 0; |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 2754 | |
David Herrmann | 6ac52db | 2012-03-11 20:05:22 +0100 | [diff] [blame] | 2755 | val = udev_device_get_property_value(device, "HOTPLUG"); |
| 2756 | if (!val) |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2757 | return 0; |
| 2758 | |
David Herrmann | 6ac52db | 2012-03-11 20:05:22 +0100 | [diff] [blame] | 2759 | return strcmp(val, "1") == 0; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2760 | } |
| 2761 | |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 2762 | static int |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2763 | udev_drm_event(int fd, uint32_t mask, void *data) |
| 2764 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2765 | struct drm_backend *b = data; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2766 | struct udev_device *event; |
| 2767 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2768 | event = udev_monitor_receive_device(b->udev_monitor); |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 2769 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2770 | if (udev_event_is_hotplug(b, event)) |
| 2771 | update_outputs(b, event); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2772 | |
| 2773 | udev_device_unref(event); |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 2774 | |
| 2775 | return 1; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2776 | } |
| 2777 | |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 2778 | static void |
Kristian Høgsberg | 7b884bc | 2012-07-31 14:32:01 -0400 | [diff] [blame] | 2779 | drm_restore(struct weston_compositor *ec) |
| 2780 | { |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 2781 | weston_launcher_restore(ec->launcher); |
Kristian Høgsberg | 7b884bc | 2012-07-31 14:32:01 -0400 | [diff] [blame] | 2782 | } |
| 2783 | |
| 2784 | static void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 2785 | drm_destroy(struct weston_compositor *ec) |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 2786 | { |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 2787 | struct drm_backend *b = to_drm_backend(ec); |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 2788 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2789 | udev_input_destroy(&b->input); |
Jonas Ådahl | c97af92 | 2012-03-28 22:36:09 +0200 | [diff] [blame] | 2790 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2791 | wl_event_source_remove(b->udev_drm_source); |
| 2792 | wl_event_source_remove(b->drm_source); |
Jonas Ådahl | c97af92 | 2012-03-28 22:36:09 +0200 | [diff] [blame] | 2793 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2794 | destroy_sprites(b); |
Kristian Høgsberg | 3d64a3e | 2013-05-10 12:36:04 -0400 | [diff] [blame] | 2795 | |
Ander Conselvan de Oliveira | 6b16214 | 2013-10-25 16:26:32 +0300 | [diff] [blame] | 2796 | weston_compositor_shutdown(ec); |
| 2797 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2798 | if (b->gbm) |
| 2799 | gbm_device_destroy(b->gbm); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 2800 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2801 | weston_launcher_destroy(ec->launcher); |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 2802 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2803 | close(b->drm.fd); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2804 | free(b); |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 2805 | } |
| 2806 | |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 2807 | static void |
Kristian Høgsberg | 61741a2 | 2013-09-17 16:02:57 -0700 | [diff] [blame] | 2808 | session_notify(struct wl_listener *listener, void *data) |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 2809 | { |
Kristian Høgsberg | 61741a2 | 2013-09-17 16:02:57 -0700 | [diff] [blame] | 2810 | struct weston_compositor *compositor = data; |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 2811 | struct drm_backend *b = to_drm_backend(compositor); |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 2812 | struct drm_sprite *sprite; |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 2813 | struct drm_output *output; |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 2814 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2815 | if (compositor->session_active) { |
Kristian Høgsberg | 61741a2 | 2013-09-17 16:02:57 -0700 | [diff] [blame] | 2816 | weston_log("activating session\n"); |
Daniel Stone | f33e104 | 2016-11-05 08:10:13 +0000 | [diff] [blame^] | 2817 | weston_compositor_wake(compositor); |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 2818 | weston_compositor_damage_all(compositor); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2819 | udev_input_enable(&b->input); |
Kristian Høgsberg | 61741a2 | 2013-09-17 16:02:57 -0700 | [diff] [blame] | 2820 | } else { |
| 2821 | weston_log("deactivating session\n"); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2822 | udev_input_disable(&b->input); |
Kristian Høgsberg | 4014a6b | 2012-04-10 00:08:45 -0400 | [diff] [blame] | 2823 | |
Philipp Brüschweiler | 57edf7f | 2013-03-29 13:01:56 +0100 | [diff] [blame] | 2824 | weston_compositor_offscreen(compositor); |
Kristian Høgsberg | d8e181b | 2011-05-06 15:38:28 -0400 | [diff] [blame] | 2825 | |
Kristian Høgsberg | 34f80ff | 2012-01-18 11:50:31 -0500 | [diff] [blame] | 2826 | /* If we have a repaint scheduled (either from a |
| 2827 | * pending pageflip or the idle handler), make sure we |
| 2828 | * cancel that so we don't try to pageflip when we're |
Philipp Brüschweiler | 57edf7f | 2013-03-29 13:01:56 +0100 | [diff] [blame] | 2829 | * vt switched away. The OFFSCREEN state will prevent |
Kristian Høgsberg | 34f80ff | 2012-01-18 11:50:31 -0500 | [diff] [blame] | 2830 | * further attemps at repainting. When we switch |
| 2831 | * back, we schedule a repaint, which will process |
| 2832 | * pending frame callbacks. */ |
| 2833 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2834 | wl_list_for_each(output, &compositor->output_list, base.link) { |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 2835 | output->base.repaint_needed = 0; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2836 | drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0); |
Kristian Høgsberg | 34f80ff | 2012-01-18 11:50:31 -0500 | [diff] [blame] | 2837 | } |
| 2838 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2839 | output = container_of(compositor->output_list.next, |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 2840 | struct drm_output, base.link); |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 2841 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2842 | wl_list_for_each(sprite, &b->sprite_list, link) |
| 2843 | drmModeSetPlane(b->drm.fd, |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 2844 | sprite->plane_id, |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 2845 | output->crtc_id, 0, 0, |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 2846 | 0, 0, 0, 0, 0, 0, 0, 0); |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 2847 | }; |
| 2848 | } |
| 2849 | |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 2850 | /* |
| 2851 | * Find primary GPU |
| 2852 | * Some systems may have multiple DRM devices attached to a single seat. This |
| 2853 | * function loops over all devices and tries to find a PCI device with the |
| 2854 | * boot_vga sysfs attribute set to 1. |
| 2855 | * If no such device is found, the first DRM device reported by udev is used. |
| 2856 | */ |
| 2857 | static struct udev_device* |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2858 | find_primary_gpu(struct drm_backend *b, const char *seat) |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 2859 | { |
| 2860 | struct udev_enumerate *e; |
| 2861 | struct udev_list_entry *entry; |
| 2862 | const char *path, *device_seat, *id; |
| 2863 | struct udev_device *device, *drm_device, *pci; |
| 2864 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2865 | e = udev_enumerate_new(b->udev); |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 2866 | udev_enumerate_add_match_subsystem(e, "drm"); |
| 2867 | udev_enumerate_add_match_sysname(e, "card[0-9]*"); |
| 2868 | |
| 2869 | udev_enumerate_scan_devices(e); |
| 2870 | drm_device = NULL; |
| 2871 | udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) { |
| 2872 | path = udev_list_entry_get_name(entry); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2873 | device = udev_device_new_from_syspath(b->udev, path); |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 2874 | if (!device) |
| 2875 | continue; |
| 2876 | device_seat = udev_device_get_property_value(device, "ID_SEAT"); |
| 2877 | if (!device_seat) |
| 2878 | device_seat = default_seat; |
| 2879 | if (strcmp(device_seat, seat)) { |
| 2880 | udev_device_unref(device); |
| 2881 | continue; |
| 2882 | } |
| 2883 | |
| 2884 | pci = udev_device_get_parent_with_subsystem_devtype(device, |
| 2885 | "pci", NULL); |
| 2886 | if (pci) { |
| 2887 | id = udev_device_get_sysattr_value(pci, "boot_vga"); |
| 2888 | if (id && !strcmp(id, "1")) { |
| 2889 | if (drm_device) |
| 2890 | udev_device_unref(drm_device); |
| 2891 | drm_device = device; |
| 2892 | break; |
| 2893 | } |
| 2894 | } |
| 2895 | |
| 2896 | if (!drm_device) |
| 2897 | drm_device = device; |
| 2898 | else |
| 2899 | udev_device_unref(device); |
| 2900 | } |
| 2901 | |
| 2902 | udev_enumerate_unref(e); |
| 2903 | return drm_device; |
| 2904 | } |
| 2905 | |
Ander Conselvan de Oliveira | da1c908 | 2012-10-31 17:55:46 +0200 | [diff] [blame] | 2906 | static void |
Derek Foreman | 8ae2db5 | 2015-07-15 13:00:36 -0500 | [diff] [blame] | 2907 | planes_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key, |
| 2908 | void *data) |
Ander Conselvan de Oliveira | da1c908 | 2012-10-31 17:55:46 +0200 | [diff] [blame] | 2909 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2910 | struct drm_backend *b = data; |
Ander Conselvan de Oliveira | da1c908 | 2012-10-31 17:55:46 +0200 | [diff] [blame] | 2911 | |
Ander Conselvan de Oliveira | 7e918da | 2012-11-22 15:56:59 +0200 | [diff] [blame] | 2912 | switch (key) { |
| 2913 | case KEY_C: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2914 | b->cursors_are_broken ^= 1; |
Ander Conselvan de Oliveira | 7e918da | 2012-11-22 15:56:59 +0200 | [diff] [blame] | 2915 | break; |
| 2916 | case KEY_V: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2917 | b->sprites_are_broken ^= 1; |
Ander Conselvan de Oliveira | 7e918da | 2012-11-22 15:56:59 +0200 | [diff] [blame] | 2918 | break; |
| 2919 | case KEY_O: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2920 | b->sprites_hidden ^= 1; |
Ander Conselvan de Oliveira | 7e918da | 2012-11-22 15:56:59 +0200 | [diff] [blame] | 2921 | break; |
| 2922 | default: |
| 2923 | break; |
| 2924 | } |
Ander Conselvan de Oliveira | 180f42a | 2012-11-21 15:11:37 +0200 | [diff] [blame] | 2925 | } |
| 2926 | |
Kristian Høgsberg | 0eac34a | 2013-08-30 14:28:22 -0700 | [diff] [blame] | 2927 | #ifdef BUILD_VAAPI_RECORDER |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 2928 | static void |
Ander Conselvan de Oliveira | 2d13fde | 2014-05-09 15:57:38 +0300 | [diff] [blame] | 2929 | recorder_destroy(struct drm_output *output) |
| 2930 | { |
| 2931 | vaapi_recorder_destroy(output->recorder); |
| 2932 | output->recorder = NULL; |
| 2933 | |
| 2934 | output->base.disable_planes--; |
| 2935 | |
| 2936 | wl_list_remove(&output->recorder_frame_listener.link); |
| 2937 | weston_log("[libva recorder] done\n"); |
| 2938 | } |
| 2939 | |
| 2940 | static void |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 2941 | recorder_frame_notify(struct wl_listener *listener, void *data) |
| 2942 | { |
| 2943 | struct drm_output *output; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2944 | struct drm_backend *b; |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 2945 | int fd, ret; |
| 2946 | |
| 2947 | output = container_of(listener, struct drm_output, |
| 2948 | recorder_frame_listener); |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 2949 | b = to_drm_backend(output->base.compositor); |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 2950 | |
| 2951 | if (!output->recorder) |
| 2952 | return; |
| 2953 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2954 | ret = drmPrimeHandleToFD(b->drm.fd, output->current->handle, |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 2955 | DRM_CLOEXEC, &fd); |
| 2956 | if (ret) { |
| 2957 | weston_log("[libva recorder] " |
| 2958 | "failed to create prime fd for front buffer\n"); |
| 2959 | return; |
| 2960 | } |
| 2961 | |
Ander Conselvan de Oliveira | 2d13fde | 2014-05-09 15:57:38 +0300 | [diff] [blame] | 2962 | ret = vaapi_recorder_frame(output->recorder, fd, |
| 2963 | output->current->stride); |
| 2964 | if (ret < 0) { |
| 2965 | weston_log("[libva recorder] aborted: %m\n"); |
| 2966 | recorder_destroy(output); |
| 2967 | } |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 2968 | } |
| 2969 | |
| 2970 | static void * |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2971 | create_recorder(struct drm_backend *b, int width, int height, |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 2972 | const char *filename) |
| 2973 | { |
| 2974 | int fd; |
| 2975 | drm_magic_t magic; |
| 2976 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2977 | fd = open(b->drm.filename, O_RDWR | O_CLOEXEC); |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 2978 | if (fd < 0) |
| 2979 | return NULL; |
| 2980 | |
| 2981 | drmGetMagic(fd, &magic); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2982 | drmAuthMagic(b->drm.fd, magic); |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 2983 | |
| 2984 | return vaapi_recorder_create(fd, width, height, filename); |
| 2985 | } |
| 2986 | |
| 2987 | static void |
Derek Foreman | 8ae2db5 | 2015-07-15 13:00:36 -0500 | [diff] [blame] | 2988 | recorder_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key, |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 2989 | void *data) |
| 2990 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2991 | struct drm_backend *b = data; |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 2992 | struct drm_output *output; |
| 2993 | int width, height; |
| 2994 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2995 | output = container_of(b->compositor->output_list.next, |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 2996 | struct drm_output, base.link); |
| 2997 | |
| 2998 | if (!output->recorder) { |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 2999 | if (output->gbm_format != GBM_FORMAT_XRGB8888) { |
Ander Conselvan de Oliveira | 2ef1cd1 | 2014-05-06 16:49:06 +0300 | [diff] [blame] | 3000 | weston_log("failed to start vaapi recorder: " |
| 3001 | "output format not supported\n"); |
| 3002 | return; |
| 3003 | } |
| 3004 | |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 3005 | width = output->base.current_mode->width; |
| 3006 | height = output->base.current_mode->height; |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 3007 | |
| 3008 | output->recorder = |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3009 | create_recorder(b, width, height, "capture.h264"); |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 3010 | if (!output->recorder) { |
| 3011 | weston_log("failed to create vaapi recorder\n"); |
| 3012 | return; |
| 3013 | } |
| 3014 | |
| 3015 | output->base.disable_planes++; |
| 3016 | |
| 3017 | output->recorder_frame_listener.notify = recorder_frame_notify; |
| 3018 | wl_signal_add(&output->base.frame_signal, |
| 3019 | &output->recorder_frame_listener); |
| 3020 | |
| 3021 | weston_output_schedule_repaint(&output->base); |
| 3022 | |
| 3023 | weston_log("[libva recorder] initialized\n"); |
| 3024 | } else { |
Ander Conselvan de Oliveira | 2d13fde | 2014-05-09 15:57:38 +0300 | [diff] [blame] | 3025 | recorder_destroy(output); |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 3026 | } |
| 3027 | } |
| 3028 | #else |
| 3029 | static void |
Bryce Harrington | 4a8a3a1 | 2015-07-16 19:12:26 -0700 | [diff] [blame] | 3030 | recorder_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key, |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 3031 | void *data) |
| 3032 | { |
| 3033 | weston_log("Compiled without libva support\n"); |
| 3034 | } |
| 3035 | #endif |
| 3036 | |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3037 | static void |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3038 | switch_to_gl_renderer(struct drm_backend *b) |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3039 | { |
| 3040 | struct drm_output *output; |
Pekka Paalanen | e4d231e | 2014-06-12 15:12:48 +0300 | [diff] [blame] | 3041 | bool dmabuf_support_inited; |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3042 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3043 | if (!b->use_pixman) |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3044 | return; |
| 3045 | |
Pekka Paalanen | e4d231e | 2014-06-12 15:12:48 +0300 | [diff] [blame] | 3046 | dmabuf_support_inited = !!b->compositor->renderer->import_dmabuf; |
| 3047 | |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3048 | weston_log("Switching to GL renderer\n"); |
| 3049 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3050 | b->gbm = create_gbm_device(b->drm.fd); |
| 3051 | if (!b->gbm) { |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3052 | weston_log("Failed to create gbm device. " |
| 3053 | "Aborting renderer switch\n"); |
| 3054 | return; |
| 3055 | } |
| 3056 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3057 | wl_list_for_each(output, &b->compositor->output_list, base.link) |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3058 | pixman_renderer_output_destroy(&output->base); |
| 3059 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3060 | b->compositor->renderer->destroy(b->compositor); |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3061 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3062 | if (drm_backend_create_gl_renderer(b) < 0) { |
| 3063 | gbm_device_destroy(b->gbm); |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3064 | weston_log("Failed to create GL renderer. Quitting.\n"); |
| 3065 | /* FIXME: we need a function to shutdown cleanly */ |
| 3066 | assert(0); |
| 3067 | } |
| 3068 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3069 | wl_list_for_each(output, &b->compositor->output_list, base.link) |
| 3070 | drm_output_init_egl(output, b); |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3071 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3072 | b->use_pixman = 0; |
Pekka Paalanen | e4d231e | 2014-06-12 15:12:48 +0300 | [diff] [blame] | 3073 | |
| 3074 | if (!dmabuf_support_inited && b->compositor->renderer->import_dmabuf) { |
| 3075 | if (linux_dmabuf_setup(b->compositor) < 0) |
| 3076 | weston_log("Error: initializing dmabuf " |
| 3077 | "support failed.\n"); |
| 3078 | } |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3079 | } |
| 3080 | |
| 3081 | static void |
Derek Foreman | 8ae2db5 | 2015-07-15 13:00:36 -0500 | [diff] [blame] | 3082 | renderer_switch_binding(struct weston_keyboard *keyboard, uint32_t time, |
| 3083 | uint32_t key, void *data) |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3084 | { |
Derek Foreman | 8ae2db5 | 2015-07-15 13:00:36 -0500 | [diff] [blame] | 3085 | struct drm_backend *b = |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 3086 | to_drm_backend(keyboard->seat->compositor); |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3087 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3088 | switch_to_gl_renderer(b); |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3089 | } |
| 3090 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 3091 | static const struct weston_drm_output_api api = { |
| 3092 | drm_output_set_mode, |
| 3093 | drm_output_set_gbm_format, |
| 3094 | drm_output_set_seat, |
| 3095 | }; |
| 3096 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3097 | static struct drm_backend * |
| 3098 | drm_backend_create(struct weston_compositor *compositor, |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 3099 | struct weston_drm_backend_config *config) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 3100 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3101 | struct drm_backend *b; |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 3102 | struct udev_device *drm_device; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 3103 | struct wl_event_loop *loop; |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 3104 | const char *path; |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 3105 | const char *seat_id = default_seat; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 3106 | int ret; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 3107 | |
Kristian Høgsberg | fc9c5e0 | 2012-06-08 16:45:33 -0400 | [diff] [blame] | 3108 | weston_log("initializing drm backend\n"); |
| 3109 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3110 | b = zalloc(sizeof *b); |
| 3111 | if (b == NULL) |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 3112 | return NULL; |
Daniel Stone | 725c2c3 | 2012-06-22 14:04:36 +0100 | [diff] [blame] | 3113 | |
Pekka Paalanen | 6858383 | 2015-05-19 09:53:16 +0300 | [diff] [blame] | 3114 | /* |
| 3115 | * KMS support for hardware planes cannot properly synchronize |
| 3116 | * without nuclear page flip. Without nuclear/atomic, hw plane |
| 3117 | * and cursor plane updates would either tear or cause extra |
| 3118 | * waits for vblanks which means dropping the compositor framerate |
Pekka Paalanen | 3f32a13 | 2015-09-07 15:38:43 +0300 | [diff] [blame] | 3119 | * to a fraction. For cursors, it's not so bad, so they are |
| 3120 | * enabled. |
Pekka Paalanen | 6858383 | 2015-05-19 09:53:16 +0300 | [diff] [blame] | 3121 | * |
| 3122 | * These can be enabled again when nuclear/atomic support lands. |
| 3123 | */ |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3124 | b->sprites_are_broken = 1; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3125 | b->compositor = compositor; |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 3126 | b->use_pixman = config->use_pixman; |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 3127 | b->use_current_mode = config->use_current_mode; |
Kristian Høgsberg | 8e6f376 | 2013-10-16 16:31:42 -0700 | [diff] [blame] | 3128 | |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 3129 | if (parse_gbm_format(config->gbm_format, GBM_FORMAT_XRGB8888, &b->gbm_format) < 0) |
| 3130 | goto err_compositor; |
Kristian Høgsberg | 8e6f376 | 2013-10-16 16:31:42 -0700 | [diff] [blame] | 3131 | |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 3132 | if (config->seat_id) |
| 3133 | seat_id = config->seat_id; |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 3134 | |
Rafal Mielniczuk | 32069c0 | 2013-03-27 18:39:28 +0100 | [diff] [blame] | 3135 | /* Check if we run drm-backend using weston-launch */ |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 3136 | compositor->launcher = weston_launcher_connect(compositor, config->tty, |
| 3137 | seat_id, true); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3138 | if (compositor->launcher == NULL) { |
Rafal Mielniczuk | 32069c0 | 2013-03-27 18:39:28 +0100 | [diff] [blame] | 3139 | weston_log("fatal: drm backend should be run " |
Kristian Høgsberg | b76237e | 2013-04-04 21:36:20 -0400 | [diff] [blame] | 3140 | "using weston-launch binary or as root\n"); |
Rafal Mielniczuk | 32069c0 | 2013-03-27 18:39:28 +0100 | [diff] [blame] | 3141 | goto err_compositor; |
| 3142 | } |
| 3143 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3144 | b->udev = udev_new(); |
| 3145 | if (b->udev == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 3146 | weston_log("failed to initialize udev context\n"); |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 3147 | goto err_launcher; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 3148 | } |
| 3149 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3150 | b->session_listener.notify = session_notify; |
| 3151 | wl_signal_add(&compositor->session_signal, &b->session_listener); |
Kristian Høgsberg | c5b9ddb | 2012-01-15 14:29:09 -0500 | [diff] [blame] | 3152 | |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 3153 | drm_device = find_primary_gpu(b, seat_id); |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 3154 | if (drm_device == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 3155 | weston_log("no drm device found\n"); |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 3156 | goto err_udev; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 3157 | } |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 3158 | path = udev_device_get_syspath(drm_device); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 3159 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3160 | if (init_drm(b, drm_device) < 0) { |
Ander Conselvan de Oliveira | 2292917 | 2013-01-25 15:13:02 +0200 | [diff] [blame] | 3161 | weston_log("failed to initialize kms\n"); |
| 3162 | goto err_udev_dev; |
| 3163 | } |
| 3164 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3165 | if (b->use_pixman) { |
| 3166 | if (init_pixman(b) < 0) { |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 3167 | weston_log("failed to initialize pixman renderer\n"); |
| 3168 | goto err_udev_dev; |
| 3169 | } |
| 3170 | } else { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3171 | if (init_egl(b) < 0) { |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 3172 | weston_log("failed to initialize egl\n"); |
| 3173 | goto err_udev_dev; |
| 3174 | } |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 3175 | } |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 3176 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3177 | b->base.destroy = drm_destroy; |
| 3178 | b->base.restore = drm_restore; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 3179 | |
Bob Ham | 91880f1 | 2016-01-12 10:21:47 +0000 | [diff] [blame] | 3180 | weston_setup_vt_switch_bindings(compositor); |
Kristian Høgsberg | 5d1c0c5 | 2012-04-10 00:11:50 -0400 | [diff] [blame] | 3181 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3182 | wl_list_init(&b->sprite_list); |
| 3183 | create_sprites(b); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 3184 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3185 | if (udev_input_init(&b->input, |
Giulio Camuffo | 8aedf7b | 2016-06-02 21:48:12 +0300 | [diff] [blame] | 3186 | compositor, b->udev, seat_id, |
| 3187 | config->configure_device) < 0) { |
Ander Conselvan de Oliveira | 4ade0e4 | 2014-04-17 13:08:45 +0300 | [diff] [blame] | 3188 | weston_log("failed to create input devices\n"); |
| 3189 | goto err_sprite; |
| 3190 | } |
| 3191 | |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 3192 | if (create_outputs(b, config->connector, drm_device) < 0) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 3193 | weston_log("failed to create output for %s\n", path); |
Ander Conselvan de Oliveira | 4ade0e4 | 2014-04-17 13:08:45 +0300 | [diff] [blame] | 3194 | goto err_udev_input; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 3195 | } |
| 3196 | |
Jason Ekstrand | 9fc7151 | 2014-04-02 19:53:46 -0500 | [diff] [blame] | 3197 | /* A this point we have some idea of whether or not we have a working |
| 3198 | * cursor plane. */ |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3199 | if (!b->cursors_are_broken) |
| 3200 | compositor->capabilities |= WESTON_CAP_CURSOR_PLANE; |
Jason Ekstrand | 9fc7151 | 2014-04-02 19:53:46 -0500 | [diff] [blame] | 3201 | |
Benjamin Franzke | 02dee2c | 2011-10-07 08:27:26 +0200 | [diff] [blame] | 3202 | path = NULL; |
| 3203 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3204 | loop = wl_display_get_event_loop(compositor->wl_display); |
| 3205 | b->drm_source = |
| 3206 | wl_event_loop_add_fd(loop, b->drm.fd, |
| 3207 | WL_EVENT_READABLE, on_drm_input, b); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 3208 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3209 | b->udev_monitor = udev_monitor_new_from_netlink(b->udev, "udev"); |
| 3210 | if (b->udev_monitor == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 3211 | weston_log("failed to intialize udev monitor\n"); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 3212 | goto err_drm_source; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 3213 | } |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3214 | udev_monitor_filter_add_match_subsystem_devtype(b->udev_monitor, |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 3215 | "drm", NULL); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3216 | b->udev_drm_source = |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 3217 | wl_event_loop_add_fd(loop, |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3218 | udev_monitor_get_fd(b->udev_monitor), |
| 3219 | WL_EVENT_READABLE, udev_drm_event, b); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 3220 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3221 | if (udev_monitor_enable_receiving(b->udev_monitor) < 0) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 3222 | weston_log("failed to enable udev-monitor receiving\n"); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 3223 | goto err_udev_monitor; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 3224 | } |
| 3225 | |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 3226 | udev_device_unref(drm_device); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 3227 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3228 | weston_compositor_add_debug_binding(compositor, KEY_O, |
| 3229 | planes_binding, b); |
| 3230 | weston_compositor_add_debug_binding(compositor, KEY_C, |
| 3231 | planes_binding, b); |
| 3232 | weston_compositor_add_debug_binding(compositor, KEY_V, |
| 3233 | planes_binding, b); |
| 3234 | weston_compositor_add_debug_binding(compositor, KEY_Q, |
| 3235 | recorder_binding, b); |
| 3236 | weston_compositor_add_debug_binding(compositor, KEY_W, |
| 3237 | renderer_switch_binding, b); |
Ander Conselvan de Oliveira | da1c908 | 2012-10-31 17:55:46 +0200 | [diff] [blame] | 3238 | |
Pekka Paalanen | e4d231e | 2014-06-12 15:12:48 +0300 | [diff] [blame] | 3239 | if (compositor->renderer->import_dmabuf) { |
| 3240 | if (linux_dmabuf_setup(compositor) < 0) |
| 3241 | weston_log("Error: initializing dmabuf " |
| 3242 | "support failed.\n"); |
| 3243 | } |
| 3244 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3245 | compositor->backend = &b->base; |
Pekka Paalanen | e4d231e | 2014-06-12 15:12:48 +0300 | [diff] [blame] | 3246 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 3247 | ret = weston_plugin_api_register(compositor, WESTON_DRM_OUTPUT_API_NAME, |
| 3248 | &api, sizeof(api)); |
| 3249 | |
| 3250 | if (ret < 0) { |
| 3251 | weston_log("Failed to register output API.\n"); |
| 3252 | goto err_udev_monitor; |
| 3253 | } |
| 3254 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3255 | return b; |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 3256 | |
| 3257 | err_udev_monitor: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3258 | wl_event_source_remove(b->udev_drm_source); |
| 3259 | udev_monitor_unref(b->udev_monitor); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 3260 | err_drm_source: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3261 | wl_event_source_remove(b->drm_source); |
Ander Conselvan de Oliveira | 4ade0e4 | 2014-04-17 13:08:45 +0300 | [diff] [blame] | 3262 | err_udev_input: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3263 | udev_input_destroy(&b->input); |
Kristian Høgsberg | 2bc5e8e | 2012-09-06 20:51:00 -0400 | [diff] [blame] | 3264 | err_sprite: |
Emmanuel Gil Peyrot | b8347e3 | 2016-05-02 22:40:13 +0100 | [diff] [blame] | 3265 | if (b->gbm) |
| 3266 | gbm_device_destroy(b->gbm); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3267 | destroy_sprites(b); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 3268 | err_udev_dev: |
| 3269 | udev_device_unref(drm_device); |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 3270 | err_launcher: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3271 | weston_launcher_destroy(compositor->launcher); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 3272 | err_udev: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3273 | udev_unref(b->udev); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 3274 | err_compositor: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3275 | weston_compositor_shutdown(compositor); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3276 | free(b); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 3277 | return NULL; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 3278 | } |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 3279 | |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 3280 | static void |
| 3281 | config_init_to_defaults(struct weston_drm_backend_config *config) |
| 3282 | { |
| 3283 | } |
| 3284 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3285 | WL_EXPORT int |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 3286 | backend_init(struct weston_compositor *compositor, |
Giulio Camuffo | 93daabb | 2015-10-17 19:24:14 +0300 | [diff] [blame] | 3287 | struct weston_backend_config *config_base) |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 3288 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3289 | struct drm_backend *b; |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 3290 | struct weston_drm_backend_config config = {{ 0, }}; |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 3291 | |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 3292 | if (config_base == NULL || |
| 3293 | config_base->struct_version != WESTON_DRM_BACKEND_CONFIG_VERSION || |
| 3294 | config_base->struct_size > sizeof(struct weston_drm_backend_config)) { |
| 3295 | weston_log("drm backend config structure is invalid\n"); |
| 3296 | return -1; |
| 3297 | } |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 3298 | |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 3299 | config_init_to_defaults(&config); |
| 3300 | memcpy(&config, config_base, config_base->struct_size); |
Kristian Høgsberg | d8e9833 | 2013-10-16 16:15:11 -0700 | [diff] [blame] | 3301 | |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 3302 | b = drm_backend_create(compositor, &config); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3303 | if (b == NULL) |
| 3304 | return -1; |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 3305 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3306 | return 0; |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 3307 | } |