blob: 6cb1d5a2ffb3fb1a0e359e887af312f6d1672c82 [file] [log] [blame]
Daniel Stonedd1bc502019-06-17 12:13:46 +01001/*
2 * Copyright © 2008-2011 Kristian Høgsberg
3 * Copyright © 2011 Intel Corporation
4 * Copyright © 2017, 2018 Collabora, Ltd.
5 * Copyright © 2017, 2018 General Electric Company
6 * Copyright (c) 2018 DisplayLink (UK) Ltd.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining
9 * a copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sublicense, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
15 *
16 * The above copyright notice and this permission notice (including the
17 * next paragraph) shall be included in all copies or substantial
18 * portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * SOFTWARE.
28 */
29
30#include "config.h"
31
32#include <errno.h>
33#include <stdint.h>
34#include <stdlib.h>
35#include <ctype.h>
36#include <string.h>
37#include <fcntl.h>
38#include <unistd.h>
39#include <linux/input.h>
40#include <linux/vt.h>
41#include <assert.h>
42#include <sys/mman.h>
43#include <time.h>
44
45
46#include <xf86drm.h>
47#include <xf86drmMode.h>
Daniel Stonedd1bc502019-06-17 12:13:46 +010048
Stefan Agnerccf24072019-07-09 22:02:00 +020049#ifdef BUILD_DRM_GBM
Daniel Stonedd1bc502019-06-17 12:13:46 +010050#include <gbm.h>
Stefan Agnerccf24072019-07-09 22:02:00 +020051#endif
Daniel Stonedd1bc502019-06-17 12:13:46 +010052#include <libudev.h>
53
54#include <libweston/libweston.h>
55#include <libweston/backend-drm.h>
Marius Vladc901e892019-06-21 22:49:18 +030056#include <libweston/weston-log.h>
Daniel Stonedd1bc502019-06-17 12:13:46 +010057#include "shared/helpers.h"
Pekka Paalanen4b301fe2021-02-04 17:39:45 +020058#include "shared/weston-drm-fourcc.h"
Daniel Stonedd1bc502019-06-17 12:13:46 +010059#include "libinput-seat.h"
Marius Vlade41c1bf2019-07-16 23:11:25 +030060#include "backend.h"
Marius Vlada72e3712019-07-10 13:46:39 +030061#include "libweston-internal.h"
Daniel Stonedd1bc502019-06-17 12:13:46 +010062
Daniel Stonedd1bc502019-06-17 12:13:46 +010063#ifndef GBM_BO_USE_CURSOR
64#define GBM_BO_USE_CURSOR GBM_BO_USE_CURSOR_64X64
65#endif
66
67#ifndef GBM_BO_USE_LINEAR
68#define GBM_BO_USE_LINEAR (1 << 4)
69#endif
70
Marius Vladcdd6fa22019-08-29 20:42:00 +030071#ifndef DRM_PLANE_ZPOS_INVALID_PLANE
72#define DRM_PLANE_ZPOS_INVALID_PLANE 0xffffffffffffffffULL
73#endif
74
Daniel Stonedd1bc502019-06-17 12:13:46 +010075/**
76 * A small wrapper to print information into the 'drm-backend' debug scope.
77 *
78 * The following conventions are used to print variables:
79 *
80 * - fixed uint32_t values, including Weston object IDs such as weston_output
81 * IDs, DRM object IDs such as CRTCs or properties, and GBM/DRM formats:
82 * "%lu (0x%lx)" (unsigned long) value, (unsigned long) value
83 *
84 * - fixed uint64_t values, such as DRM property values (including object IDs
85 * when used as a value):
86 * "%llu (0x%llx)" (unsigned long long) value, (unsigned long long) value
87 *
88 * - non-fixed-width signed int:
89 * "%d" value
90 *
91 * - non-fixed-width unsigned int:
92 * "%u (0x%x)" value, value
93 *
94 * - non-fixed-width unsigned long:
95 * "%lu (0x%lx)" value, value
96 *
97 * Either the integer or hexadecimal forms may be omitted if it is known that
98 * one representation is not useful (e.g. width/height in hex are rarely what
99 * you want).
100 *
101 * This is to avoid implicit widening or narrowing when we use fixed-size
102 * types: uint32_t can be resolved by either unsigned int or unsigned long
103 * on a 32-bit system but only unsigned int on a 64-bit system, with uint64_t
104 * being unsigned long long on a 32-bit system and unsigned long on a 64-bit
105 * system. To avoid confusing side effects, we explicitly cast to the widest
106 * possible type and use a matching format specifier.
107 */
108#define drm_debug(b, ...) \
109 weston_log_scope_printf((b)->debug, __VA_ARGS__)
110
111#define MAX_CLONED_CONNECTORS 4
112
Stefan Agner723c6a12019-12-09 13:06:36 +0100113
Daniel Stonedd1bc502019-06-17 12:13:46 +0100114/**
Daniel Stonedd1bc502019-06-17 12:13:46 +0100115 * Represents the values of an enum-type KMS property
116 */
117struct drm_property_enum_info {
118 const char *name; /**< name as string (static, not freed) */
119 bool valid; /**< true if value is supported; ignore if false */
120 uint64_t value; /**< raw value */
121};
122
123/**
124 * Holds information on a DRM property, including its ID and the enum
125 * values it holds.
126 *
127 * DRM properties are allocated dynamically, and maintained as DRM objects
128 * within the normal object ID space; they thus do not have a stable ID
129 * to refer to. This includes enum values, which must be referred to by
130 * integer values, but these are not stable.
131 *
132 * drm_property_info allows a cache to be maintained where Weston can use
133 * enum values internally to refer to properties, with the mapping to DRM
134 * ID values being maintained internally.
135 */
136struct drm_property_info {
137 const char *name; /**< name as string (static, not freed) */
138 uint32_t prop_id; /**< KMS property object ID */
Marius Vlad1accffe2019-11-01 12:00:09 +0200139 uint32_t flags;
Daniel Stonedd1bc502019-06-17 12:13:46 +0100140 unsigned int num_enum_values; /**< number of enum values */
141 struct drm_property_enum_info *enum_values; /**< array of enum values */
Marius Vlad1accffe2019-11-01 12:00:09 +0200142 unsigned int num_range_values;
143 uint64_t range_values[2];
Daniel Stonedd1bc502019-06-17 12:13:46 +0100144};
145
146/**
147 * List of properties attached to DRM planes
148 */
149enum wdrm_plane_property {
150 WDRM_PLANE_TYPE = 0,
151 WDRM_PLANE_SRC_X,
152 WDRM_PLANE_SRC_Y,
153 WDRM_PLANE_SRC_W,
154 WDRM_PLANE_SRC_H,
155 WDRM_PLANE_CRTC_X,
156 WDRM_PLANE_CRTC_Y,
157 WDRM_PLANE_CRTC_W,
158 WDRM_PLANE_CRTC_H,
159 WDRM_PLANE_FB_ID,
160 WDRM_PLANE_CRTC_ID,
161 WDRM_PLANE_IN_FORMATS,
162 WDRM_PLANE_IN_FENCE_FD,
163 WDRM_PLANE_FB_DAMAGE_CLIPS,
Marius Vladcdd6fa22019-08-29 20:42:00 +0300164 WDRM_PLANE_ZPOS,
Daniel Stonedd1bc502019-06-17 12:13:46 +0100165 WDRM_PLANE__COUNT
166};
167
168/**
169 * Possible values for the WDRM_PLANE_TYPE property.
170 */
171enum wdrm_plane_type {
172 WDRM_PLANE_TYPE_PRIMARY = 0,
173 WDRM_PLANE_TYPE_CURSOR,
174 WDRM_PLANE_TYPE_OVERLAY,
175 WDRM_PLANE_TYPE__COUNT
176};
177
178/**
179 * List of properties attached to a DRM connector
180 */
181enum wdrm_connector_property {
182 WDRM_CONNECTOR_EDID = 0,
183 WDRM_CONNECTOR_DPMS,
184 WDRM_CONNECTOR_CRTC_ID,
185 WDRM_CONNECTOR_NON_DESKTOP,
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530186 WDRM_CONNECTOR_CONTENT_PROTECTION,
187 WDRM_CONNECTOR_HDCP_CONTENT_TYPE,
Lucas Stach72e7a1e2019-11-25 23:31:57 +0000188 WDRM_CONNECTOR_PANEL_ORIENTATION,
Daniel Stonedd1bc502019-06-17 12:13:46 +0100189 WDRM_CONNECTOR__COUNT
190};
191
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530192enum wdrm_content_protection_state {
193 WDRM_CONTENT_PROTECTION_UNDESIRED = 0,
194 WDRM_CONTENT_PROTECTION_DESIRED,
195 WDRM_CONTENT_PROTECTION_ENABLED,
196 WDRM_CONTENT_PROTECTION__COUNT
197};
198
199enum wdrm_hdcp_content_type {
200 WDRM_HDCP_CONTENT_TYPE0 = 0,
201 WDRM_HDCP_CONTENT_TYPE1,
202 WDRM_HDCP_CONTENT_TYPE__COUNT
203};
204
Daniel Stonedd1bc502019-06-17 12:13:46 +0100205enum wdrm_dpms_state {
206 WDRM_DPMS_STATE_OFF = 0,
207 WDRM_DPMS_STATE_ON,
208 WDRM_DPMS_STATE_STANDBY, /* unused */
209 WDRM_DPMS_STATE_SUSPEND, /* unused */
210 WDRM_DPMS_STATE__COUNT
211};
212
Lucas Stach72e7a1e2019-11-25 23:31:57 +0000213enum wdrm_panel_orientation {
214 WDRM_PANEL_ORIENTATION_NORMAL = 0,
215 WDRM_PANEL_ORIENTATION_UPSIDE_DOWN,
216 WDRM_PANEL_ORIENTATION_LEFT_SIDE_UP,
217 WDRM_PANEL_ORIENTATION_RIGHT_SIDE_UP,
218 WDRM_PANEL_ORIENTATION__COUNT
219};
220
Daniel Stonedd1bc502019-06-17 12:13:46 +0100221/**
222 * List of properties attached to DRM CRTCs
223 */
224enum wdrm_crtc_property {
225 WDRM_CRTC_MODE_ID = 0,
226 WDRM_CRTC_ACTIVE,
227 WDRM_CRTC__COUNT
228};
229
Leandro Ribeiro0a7034c2021-09-13 14:52:53 -0300230/**
231 * Reasons why placing a view on a plane failed. Needed by the dma-buf feedback.
232 */
233enum try_view_on_plane_failure_reasons {
234 FAILURE_REASONS_NONE = 0,
235 FAILURE_REASONS_FORCE_RENDERER = (1 << 0),
236 FAILURE_REASONS_FB_FORMAT_INCOMPATIBLE = (1 << 1),
237 FAILURE_REASONS_DMABUF_MODIFIER_INVALID = (1 << 2),
238 FAILURE_REASONS_ADD_FB_FAILED = (1 << 3),
239};
240
Leandro Ribeiro54293022021-10-12 14:48:36 -0300241/**
242 * We use this to keep track of actions we need to do with the dma-buf feedback
243 * in order to keep it up-to-date with the info we get from the DRM-backend.
244 */
245enum actions_needed_dmabuf_feedback {
246 ACTION_NEEDED_NONE = 0,
247 ACTION_NEEDED_ADD_SCANOUT_TRANCHE = (1 << 0),
248 ACTION_NEEDED_REMOVE_SCANOUT_TRANCHE = (1 << 1),
249};
250
Daniel Stonedd1bc502019-06-17 12:13:46 +0100251struct drm_backend {
252 struct weston_backend base;
253 struct weston_compositor *compositor;
254
255 struct udev *udev;
256 struct wl_event_source *drm_source;
257
258 struct udev_monitor *udev_monitor;
259 struct wl_event_source *udev_drm_source;
260
261 struct {
262 int id;
263 int fd;
264 char *filename;
265 dev_t devnum;
266 } drm;
267 struct gbm_device *gbm;
268 struct wl_listener session_listener;
269 uint32_t gbm_format;
270
271 /* we need these parameters in order to not fail drmModeAddFB2()
272 * due to out of bounds dimensions, and then mistakenly set
273 * sprites_are_broken:
274 */
275 int min_width, max_width;
276 int min_height, max_height;
277
278 struct wl_list plane_list;
Daniel Stone57d609a2021-11-16 18:56:09 +0000279 uint32_t next_plane_idx;
Daniel Stonedd1bc502019-06-17 12:13:46 +0100280
281 void *repaint_data;
282
283 bool state_invalid;
284
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300285 /* drm_crtc::link */
286 struct wl_list crtc_list;
287
Leandro Ribeiro96bef052020-09-09 15:23:49 -0300288 /* drm_writeback::link */
289 struct wl_list writeback_connector_list;
290
Emmanuel Gil Peyrot1b3ad092019-12-09 02:50:55 +0100291 bool sprites_are_broken;
292 bool cursors_are_broken;
Daniel Stonedd1bc502019-06-17 12:13:46 +0100293
Daniel Stonedd1bc502019-06-17 12:13:46 +0100294 bool atomic_modeset;
295
296 bool use_pixman;
297 bool use_pixman_shadow;
298
299 struct udev_input input;
300
301 int32_t cursor_width;
302 int32_t cursor_height;
303
304 uint32_t pageflip_timeout;
305
306 bool shutting_down;
307
308 bool aspect_ratio_supported;
309
310 bool fb_modifiers;
311
312 struct weston_log_scope *debug;
leng.fang32af9fc2024-06-13 11:22:15 +0800313
314#ifdef MESON_DRM_FIX_UI_SIZE
315 /** Set the logic window size
316 * the current_mode 's w/h will not equal real display mode size any more
317 * after enable fixed ui size. */
318 struct weston_size fixed_ui_size;
319#endif
320#ifdef BUILD_AML_TV
321 int vdin_detect_fd;
322#endif
323 bool allow_modeset;
Daniel Stonedd1bc502019-06-17 12:13:46 +0100324};
325
326struct drm_mode {
327 struct weston_mode base;
328 drmModeModeInfo mode_info;
329 uint32_t blob_id;
330};
331
332enum drm_fb_type {
333 BUFFER_INVALID = 0, /**< never used */
334 BUFFER_CLIENT, /**< directly sourced from client */
335 BUFFER_DMABUF, /**< imported from linux_dmabuf client */
336 BUFFER_PIXMAN_DUMB, /**< internal Pixman rendering */
337 BUFFER_GBM_SURFACE, /**< internal EGL rendering */
338 BUFFER_CURSOR, /**< internal cursor buffer */
339};
340
341struct drm_fb {
342 enum drm_fb_type type;
343
344 int refcnt;
345
346 uint32_t fb_id, size;
347 uint32_t handles[4];
348 uint32_t strides[4];
349 uint32_t offsets[4];
350 int num_planes;
351 const struct pixel_format_info *format;
352 uint64_t modifier;
353 int width, height;
354 int fd;
Daniel Stonedd1bc502019-06-17 12:13:46 +0100355
Daniel Stone57d609a2021-11-16 18:56:09 +0000356 uint32_t plane_mask;
357
Daniel Stonedd1bc502019-06-17 12:13:46 +0100358 /* Used by gbm fbs */
359 struct gbm_bo *bo;
360 struct gbm_surface *gbm_surface;
361
362 /* Used by dumb fbs */
363 void *map;
leng.fang32af9fc2024-06-13 11:22:15 +0800364 struct drm_plane *plane;
Daniel Stonedd1bc502019-06-17 12:13:46 +0100365};
366
Daniel Stone7d27df42021-11-18 16:01:03 +0000367struct drm_buffer_fb {
368 struct drm_fb *fb;
369 enum try_view_on_plane_failure_reasons failure_reasons;
370 struct wl_listener buffer_destroy_listener;
371};
372
Daniel Stonedd1bc502019-06-17 12:13:46 +0100373struct drm_edid {
374 char eisa_id[13];
375 char monitor_name[13];
376 char pnp_id[5];
377 char serial_number[13];
378};
379
380/**
381 * Pending state holds one or more drm_output_state structures, collected from
382 * performing repaint. This pending state is transient, and only lives between
383 * beginning a repaint group and flushing the results: after flush, each
384 * output state will complete and be retired separately.
385 */
386struct drm_pending_state {
387 struct drm_backend *backend;
388 struct wl_list output_list;
389};
390
391/*
392 * Output state holds the dynamic state for one Weston output, i.e. a KMS CRTC,
393 * plus >= 1 each of encoder/connector/plane. Since everything but the planes
394 * is currently statically assigned per-output, we mainly use this to track
395 * plane state.
396 *
397 * pending_state is set when the output state is owned by a pending_state,
398 * i.e. when it is being constructed and has not yet been applied. When the
399 * output state has been applied, the owning pending_state is freed.
400 */
401struct drm_output_state {
402 struct drm_pending_state *pending_state;
403 struct drm_output *output;
404 struct wl_list link;
405 enum dpms_enum dpms;
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530406 enum weston_hdcp_protection protection;
Daniel Stonedd1bc502019-06-17 12:13:46 +0100407 struct wl_list plane_list;
408};
409
410/**
Marius Vlad2538aac2019-10-14 11:05:30 +0300411 * An instance of this class is created each time we believe we have a plane
Maxime Roussin-Bélanger35e34502020-12-17 17:08:56 -0500412 * suitable to be used by a view as a direct scan-out. The list is initialized
Marius Vlad2538aac2019-10-14 11:05:30 +0300413 * and populated locally.
414 */
415struct drm_plane_zpos {
416 struct drm_plane *plane;
417 struct wl_list link; /**< :candidate_plane_zpos_list */
418};
419
420/**
Daniel Stonedd1bc502019-06-17 12:13:46 +0100421 * Plane state holds the dynamic state for a plane: where it is positioned,
422 * and which buffer it is currently displaying.
423 *
424 * The plane state is owned by an output state, except when setting an initial
425 * state. See drm_output_state for notes on state object lifetime.
426 */
427struct drm_plane_state {
428 struct drm_plane *plane;
429 struct drm_output *output;
430 struct drm_output_state *output_state;
431
432 struct drm_fb *fb;
Daniel Stone2ecc38b2021-11-18 15:33:17 +0000433 struct {
434 struct weston_buffer_reference buffer;
435 struct weston_buffer_release_reference release;
436 } fb_ref;
Daniel Stonedd1bc502019-06-17 12:13:46 +0100437
438 struct weston_view *ev; /**< maintained for drm_assign_planes only */
439
440 int32_t src_x, src_y;
441 uint32_t src_w, src_h;
442 int32_t dest_x, dest_y;
443 uint32_t dest_w, dest_h;
444
Marius Vladcdd6fa22019-08-29 20:42:00 +0300445 uint64_t zpos;
446
Daniel Stonedd1bc502019-06-17 12:13:46 +0100447 bool complete;
448
449 /* We don't own the fd, so we shouldn't close it */
450 int in_fence_fd;
451
Scott Anderson15c603c2020-06-02 17:39:43 +1200452 uint32_t damage_blob_id; /* damage to kernel */
Daniel Stonedd1bc502019-06-17 12:13:46 +0100453
454 struct wl_list link; /* drm_output_state::plane_list */
455};
456
457/**
458 * A plane represents one buffer, positioned within a CRTC, and stacked
459 * relative to other planes on the same CRTC.
460 *
461 * Each CRTC has a 'primary plane', which use used to display the classic
462 * framebuffer contents, as accessed through the legacy drmModeSetCrtc
463 * call (which combines setting the CRTC's actual physical mode, and the
464 * properties of the primary plane).
465 *
466 * The cursor plane also has its own alternate legacy API.
467 *
468 * Other planes are used opportunistically to display content we do not
469 * wish to blit into the primary plane. These non-primary/cursor planes
470 * are referred to as 'sprites'.
471 */
472struct drm_plane {
473 struct weston_plane base;
474
475 struct drm_backend *backend;
476
477 enum wdrm_plane_type type;
478
479 uint32_t possible_crtcs;
480 uint32_t plane_id;
Daniel Stone57d609a2021-11-16 18:56:09 +0000481 uint32_t plane_idx;
Daniel Stonedd1bc502019-06-17 12:13:46 +0100482
483 struct drm_property_info props[WDRM_PLANE__COUNT];
484
485 /* The last state submitted to the kernel for this plane. */
486 struct drm_plane_state *state_cur;
487
Marius Vladcdd6fa22019-08-29 20:42:00 +0300488 uint64_t zpos_min;
489 uint64_t zpos_max;
490
Daniel Stonedd1bc502019-06-17 12:13:46 +0100491 struct wl_list link;
492
leng.fang32af9fc2024-06-13 11:22:15 +0800493 bool is_video_plane;
494 int video_plane;
495 bool keep_last_frame;
496 uint32_t last_fb_id;
497 struct drm_crtc *crtc;
498
Scott Anderson74663092021-02-01 15:46:33 -0300499 struct weston_drm_format_array formats;
Daniel Stonedd1bc502019-06-17 12:13:46 +0100500};
501
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300502struct drm_connector {
503 struct drm_backend *backend;
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300504
505 drmModeConnector *conn;
506 uint32_t connector_id;
507
Leandro Ribeiro702fbf72020-08-18 17:35:05 -0300508 drmModeObjectProperties *props_drm;
509
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300510 /* Holds the properties for the connector */
511 struct drm_property_info props[WDRM_CONNECTOR__COUNT];
512};
513
Leandro Ribeiro96bef052020-09-09 15:23:49 -0300514struct drm_writeback {
515 /* drm_backend::writeback_connector_list */
516 struct wl_list link;
517
518 struct drm_backend *backend;
519 struct drm_connector connector;
520};
521
Daniel Stonedd1bc502019-06-17 12:13:46 +0100522struct drm_head {
523 struct weston_head base;
524 struct drm_backend *backend;
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300525 struct drm_connector connector;
Daniel Stonedd1bc502019-06-17 12:13:46 +0100526
Daniel Stonedd1bc502019-06-17 12:13:46 +0100527 struct drm_edid edid;
528
Daniel Stonedd1bc502019-06-17 12:13:46 +0100529 struct backlight *backlight;
530
531 drmModeModeInfo inherited_mode; /**< Original mode on the connector */
532 uint32_t inherited_crtc_id; /**< Original CRTC assignment */
533};
534
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300535struct drm_crtc {
536 /* drm_backend::crtc_list */
537 struct wl_list link;
Daniel Stonedd1bc502019-06-17 12:13:46 +0100538 struct drm_backend *backend;
539
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300540 /* The output driven by the CRTC */
541 struct drm_output *output;
542
Daniel Stonedd1bc502019-06-17 12:13:46 +0100543 uint32_t crtc_id; /* object ID to pass to DRM functions */
544 int pipe; /* index of CRTC in resource array / bitmasks */
545
546 /* Holds the properties for the CRTC */
547 struct drm_property_info props_crtc[WDRM_CRTC__COUNT];
leng.fang32af9fc2024-06-13 11:22:15 +0800548 bool output_change;
549 struct drm_output *disable_output;
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300550};
551
552struct drm_output {
553 struct weston_output base;
554 struct drm_backend *backend;
555 struct drm_crtc *crtc;
Daniel Stonedd1bc502019-06-17 12:13:46 +0100556
Emmanuel Gil Peyrot1b3ad092019-12-09 02:50:55 +0100557 bool page_flip_pending;
558 bool atomic_complete_pending;
559 bool destroy_pending;
560 bool disable_pending;
561 bool dpms_off_pending;
Daniel Stonedd1bc502019-06-17 12:13:46 +0100562
Stefan Agner974390a2019-07-08 00:42:05 +0200563 uint32_t gbm_cursor_handle[2];
Daniel Stonedd1bc502019-06-17 12:13:46 +0100564 struct drm_fb *gbm_cursor_fb[2];
565 struct drm_plane *cursor_plane;
566 struct weston_view *cursor_view;
Alexandros Frantzis10937fe2021-06-14 13:09:44 +0300567 struct wl_listener cursor_view_destroy_listener;
Daniel Stonedd1bc502019-06-17 12:13:46 +0100568 int current_cursor;
569
570 struct gbm_surface *gbm_surface;
571 uint32_t gbm_format;
572 uint32_t gbm_bo_flags;
573
574 /* Plane being displayed directly on the CRTC */
575 struct drm_plane *scanout_plane;
576
577 /* The last state submitted to the kernel for this CRTC. */
578 struct drm_output_state *state_cur;
579 /* The previously-submitted state, where the hardware has not
580 * yet acknowledged completion of state_cur. */
581 struct drm_output_state *state_last;
582
583 struct drm_fb *dumb[2];
584 pixman_image_t *image[2];
585 int current_image;
586 pixman_region32_t previous_damage;
587
588 struct vaapi_recorder *recorder;
589 struct wl_listener recorder_frame_listener;
590
591 struct wl_event_source *pageflip_timer;
592
593 bool virtual;
leng.fang32af9fc2024-06-13 11:22:15 +0800594 int video_plane_count;
Daniel Stonedd1bc502019-06-17 12:13:46 +0100595
596 submit_frame_cb virtual_submit_frame;
leng.fang32af9fc2024-06-13 11:22:15 +0800597
598#ifdef MESON_DRM_FIX_UI_SIZE
599 bool current_mode_need_restore;
600
601 /* the real display mode size will saved out of the current mode */
602 struct weston_size display_size;
603#endif
604
Daniel Stonedd1bc502019-06-17 12:13:46 +0100605};
606
607static inline struct drm_head *
608to_drm_head(struct weston_head *base)
609{
610 return container_of(base, struct drm_head, base);
611}
612
613static inline struct drm_output *
614to_drm_output(struct weston_output *base)
615{
616 return container_of(base, struct drm_output, base);
617}
618
619static inline struct drm_backend *
620to_drm_backend(struct weston_compositor *base)
621{
622 return container_of(base->backend, struct drm_backend, base);
623}
624
leng.fang32af9fc2024-06-13 11:22:15 +0800625static inline struct drm_plane *
626to_drm_plane(struct weston_plane *base)
627{
628 return container_of(base, struct drm_plane, base);
629}
630
Daniel Stonedd1bc502019-06-17 12:13:46 +0100631static inline struct drm_mode *
632to_drm_mode(struct weston_mode *base)
633{
634 return container_of(base, struct drm_mode, base);
635}
Daniel Stonefbe6c1d2019-06-17 16:04:26 +0100636
Marius Vlad3dea57a2019-09-27 20:45:41 +0300637static inline const char *
638drm_output_get_plane_type_name(struct drm_plane *p)
639{
640 switch (p->type) {
641 case WDRM_PLANE_TYPE_PRIMARY:
642 return "primary";
643 case WDRM_PLANE_TYPE_CURSOR:
644 return "cursor";
645 case WDRM_PLANE_TYPE_OVERLAY:
646 return "overlay";
647 default:
648 assert(0);
649 break;
650 }
651}
652
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300653struct drm_crtc *
654drm_crtc_find(struct drm_backend *b, uint32_t crtc_id);
Daniel Stone4c2fc702019-06-18 11:12:07 +0100655
656struct drm_head *
657drm_head_find_by_connector(struct drm_backend *backend, uint32_t connector_id);
658
Daniel Stone7580b3c2019-06-18 11:16:53 +0100659static inline bool
660drm_view_transform_supported(struct weston_view *ev, struct weston_output *output)
661{
662 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
663
664 /* This will incorrectly disallow cases where the combination of
665 * buffer and view transformations match the output transform.
666 * Fixing this requires a full analysis of the transformation
667 * chain. */
668 if (ev->transform.enabled &&
669 ev->transform.matrix.type >= WESTON_MATRIX_TRANSFORM_ROTATE)
670 return false;
671
672 if (viewport->buffer.transform != output->transform)
673 return false;
674
675 return true;
676}
leng.fang32af9fc2024-06-13 11:22:15 +0800677struct weston_plane *
678drm_get_weston_plane(struct drm_backend *backend, struct drm_plane* drm_plane);
Daniel Stone7580b3c2019-06-18 11:16:53 +0100679
Daniel Stonefbe6c1d2019-06-17 16:04:26 +0100680int
681drm_mode_ensure_blob(struct drm_backend *backend, struct drm_mode *mode);
682
683struct drm_mode *
684drm_output_choose_mode(struct drm_output *output,
685 struct weston_mode *target_mode);
686void
Leandro Ribeiro702fbf72020-08-18 17:35:05 -0300687update_head_from_connector(struct drm_head *head);
Daniel Stonefbe6c1d2019-06-17 16:04:26 +0100688
689void
690drm_mode_list_destroy(struct drm_backend *backend, struct wl_list *mode_list);
691
692void
693drm_output_print_modes(struct drm_output *output);
694
695int
696drm_output_set_mode(struct weston_output *base,
697 enum weston_drm_backend_output_mode mode,
698 const char *modeline);
699
Daniel Stone4c2fc702019-06-18 11:12:07 +0100700void
701drm_property_info_populate(struct drm_backend *b,
702 const struct drm_property_info *src,
703 struct drm_property_info *info,
704 unsigned int num_infos,
705 drmModeObjectProperties *props);
Daniel Stonefbe6c1d2019-06-17 16:04:26 +0100706uint64_t
707drm_property_get_value(struct drm_property_info *info,
708 const drmModeObjectProperties *props,
709 uint64_t def);
Marius Vlad1accffe2019-11-01 12:00:09 +0200710uint64_t *
711drm_property_get_range_values(struct drm_property_info *info,
712 const drmModeObjectProperties *props);
Daniel Stone4c2fc702019-06-18 11:12:07 +0100713int
714drm_plane_populate_formats(struct drm_plane *plane, const drmModePlane *kplane,
Stefan Agner465ab2c2020-06-17 23:36:44 +0200715 const drmModeObjectProperties *props,
716 const bool use_modifiers);
Daniel Stone4c2fc702019-06-18 11:12:07 +0100717void
718drm_property_info_free(struct drm_property_info *info, int num_props);
719
720extern struct drm_property_enum_info plane_type_enums[];
721extern const struct drm_property_info plane_props[];
722extern struct drm_property_enum_info dpms_state_enums[];
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530723extern struct drm_property_enum_info content_protection_enums[];
724extern struct drm_property_enum_info hdcp_content_type_enums[];
Daniel Stone4c2fc702019-06-18 11:12:07 +0100725extern const struct drm_property_info connector_props[];
726extern const struct drm_property_info crtc_props[];
727
728int
729init_kms_caps(struct drm_backend *b);
730
731int
732drm_pending_state_test(struct drm_pending_state *pending_state);
733int
734drm_pending_state_apply(struct drm_pending_state *pending_state);
735int
736drm_pending_state_apply_sync(struct drm_pending_state *pending_state);
737
738void
739drm_output_set_gamma(struct weston_output *output_base,
740 uint16_t size, uint16_t *r, uint16_t *g, uint16_t *b);
741
742void
743drm_output_update_msc(struct drm_output *output, unsigned int seq);
744void
745drm_output_update_complete(struct drm_output *output, uint32_t flags,
746 unsigned int sec, unsigned int usec);
747int
748on_drm_input(int fd, uint32_t mask, void *data);
749
Daniel Stone7580b3c2019-06-18 11:16:53 +0100750struct drm_fb *
751drm_fb_ref(struct drm_fb *fb);
752void
753drm_fb_unref(struct drm_fb *fb);
754
755struct drm_fb *
756drm_fb_create_dumb(struct drm_backend *b, int width, int height,
757 uint32_t format);
758struct drm_fb *
759drm_fb_get_from_bo(struct gbm_bo *bo, struct drm_backend *backend,
760 bool is_opaque, enum drm_fb_type type);
Daniel Stone6b466f22019-06-18 11:30:54 +0100761
Alexandros Frantzis10937fe2021-06-14 13:09:44 +0300762void
763drm_output_set_cursor_view(struct drm_output *output, struct weston_view *ev);
764
Stefan Agnerccf24072019-07-09 22:02:00 +0200765#ifdef BUILD_DRM_GBM
766extern struct drm_fb *
Leandro Ribeiro0a7034c2021-09-13 14:52:53 -0300767drm_fb_get_from_view(struct drm_output_state *state, struct weston_view *ev,
768 uint32_t *try_view_on_plane_failure_reasons);
Marius Vlad81bada52019-11-11 00:27:17 +0200769extern bool
770drm_can_scanout_dmabuf(struct weston_compositor *ec,
771 struct linux_dmabuf_buffer *dmabuf);
Stefan Agnerccf24072019-07-09 22:02:00 +0200772#else
773static inline struct drm_fb *
Leandro Ribeiro0a7034c2021-09-13 14:52:53 -0300774drm_fb_get_from_view(struct drm_output_state *state, struct weston_view *ev,
775 uint32_t *try_view_on_plane_failure_reasons)
Stefan Agnerccf24072019-07-09 22:02:00 +0200776{
777 return NULL;
778}
Marius Vlad81bada52019-11-11 00:27:17 +0200779static inline bool
780drm_can_scanout_dmabuf(struct weston_compositor *ec,
781 struct linux_dmabuf_buffer *dmabuf)
782{
783 return false;
784}
Stefan Agnerccf24072019-07-09 22:02:00 +0200785#endif
Daniel Stone6b466f22019-06-18 11:30:54 +0100786
787struct drm_pending_state *
788drm_pending_state_alloc(struct drm_backend *backend);
789void
790drm_pending_state_free(struct drm_pending_state *pending_state);
791struct drm_output_state *
792drm_pending_state_get_output(struct drm_pending_state *pending_state,
793 struct drm_output *output);
794
795
796/**
797 * Mode for drm_output_state_duplicate.
798 */
799enum drm_output_state_duplicate_mode {
800 DRM_OUTPUT_STATE_CLEAR_PLANES, /**< reset all planes to off */
801 DRM_OUTPUT_STATE_PRESERVE_PLANES, /**< preserve plane state */
802};
803
804struct drm_output_state *
805drm_output_state_alloc(struct drm_output *output,
806 struct drm_pending_state *pending_state);
807struct drm_output_state *
808drm_output_state_duplicate(struct drm_output_state *src,
809 struct drm_pending_state *pending_state,
810 enum drm_output_state_duplicate_mode plane_mode);
811void
812drm_output_state_free(struct drm_output_state *state);
813struct drm_plane_state *
814drm_output_state_get_plane(struct drm_output_state *state_output,
815 struct drm_plane *plane);
816struct drm_plane_state *
817drm_output_state_get_existing_plane(struct drm_output_state *state_output,
818 struct drm_plane *plane);
819
820
821
822struct drm_plane_state *
823drm_plane_state_alloc(struct drm_output_state *state_output,
824 struct drm_plane *plane);
825struct drm_plane_state *
826drm_plane_state_duplicate(struct drm_output_state *state_output,
827 struct drm_plane_state *src);
828void
829drm_plane_state_free(struct drm_plane_state *state, bool force);
830void
831drm_plane_state_put_back(struct drm_plane_state *state);
832bool
833drm_plane_state_coords_for_view(struct drm_plane_state *state,
Marius Vlad2538aac2019-10-14 11:05:30 +0300834 struct weston_view *ev, uint64_t zpos);
Alexandros Frantzis99751342020-05-18 15:22:49 +0300835void
836drm_plane_reset_state(struct drm_plane *plane);
Daniel Stonee404b722019-06-22 18:40:31 +0100837
838void
839drm_assign_planes(struct weston_output *output_base, void *repaint_data);
840
841bool
842drm_plane_is_available(struct drm_plane *plane, struct drm_output *output);
Stefan Agner3654c672019-07-09 00:50:30 +0200843
844void
845drm_output_render(struct drm_output_state *state, pixman_region32_t *damage);
846
847int
848parse_gbm_format(const char *s, uint32_t default_value, uint32_t *gbm_format);
849
850extern struct gl_renderer_interface *gl_renderer;
851
852#ifdef BUILD_DRM_VIRTUAL
853extern int
854drm_backend_init_virtual_output_api(struct weston_compositor *compositor);
855#else
856inline static int
857drm_backend_init_virtual_output_api(struct weston_compositor *compositor)
858{
859 return 0;
860}
861#endif
862
Stefan Agnerccf24072019-07-09 22:02:00 +0200863#ifdef BUILD_DRM_GBM
864int
865init_egl(struct drm_backend *b);
866
Stefan Agner3654c672019-07-09 00:50:30 +0200867int
868drm_output_init_egl(struct drm_output *output, struct drm_backend *b);
Stefan Agnerccf24072019-07-09 22:02:00 +0200869
Stefan Agner3654c672019-07-09 00:50:30 +0200870void
871drm_output_fini_egl(struct drm_output *output);
872
Stefan Agnerccf24072019-07-09 22:02:00 +0200873struct drm_fb *
874drm_output_render_gl(struct drm_output_state *state, pixman_region32_t *damage);
875
876void
877renderer_switch_binding(struct weston_keyboard *keyboard,
878 const struct timespec *time, uint32_t key, void *data);
879#else
880inline static int
881init_egl(struct drm_backend *b)
882{
883 weston_log("Compiled without GBM/EGL support\n");
884 return -1;
885}
886
887inline static int
888drm_output_init_egl(struct drm_output *output, struct drm_backend *b)
889{
890 return -1;
891}
892
893inline static void
894drm_output_fini_egl(struct drm_output *output)
895{
896}
897
898inline static struct drm_fb *
899drm_output_render_gl(struct drm_output_state *state, pixman_region32_t *damage)
900{
901 return NULL;
902}
903
904inline static void
905renderer_switch_binding(struct weston_keyboard *keyboard,
906 const struct timespec *time, uint32_t key, void *data)
907{
908 weston_log("Compiled without GBM/EGL support\n");
909}
910#endif