blob: b9ee319cc394b49ff06b598aaf5bda1abe0873fe [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;
leng.fang9cf09e22024-07-17 20:01:11 +0800324 int display_enable;
Daniel Stonedd1bc502019-06-17 12:13:46 +0100325};
326
327struct drm_mode {
328 struct weston_mode base;
329 drmModeModeInfo mode_info;
330 uint32_t blob_id;
331};
332
333enum drm_fb_type {
334 BUFFER_INVALID = 0, /**< never used */
335 BUFFER_CLIENT, /**< directly sourced from client */
336 BUFFER_DMABUF, /**< imported from linux_dmabuf client */
337 BUFFER_PIXMAN_DUMB, /**< internal Pixman rendering */
338 BUFFER_GBM_SURFACE, /**< internal EGL rendering */
339 BUFFER_CURSOR, /**< internal cursor buffer */
340};
341
342struct drm_fb {
343 enum drm_fb_type type;
344
345 int refcnt;
346
347 uint32_t fb_id, size;
348 uint32_t handles[4];
349 uint32_t strides[4];
350 uint32_t offsets[4];
351 int num_planes;
352 const struct pixel_format_info *format;
353 uint64_t modifier;
354 int width, height;
355 int fd;
Daniel Stonedd1bc502019-06-17 12:13:46 +0100356
Daniel Stone57d609a2021-11-16 18:56:09 +0000357 uint32_t plane_mask;
358
Daniel Stonedd1bc502019-06-17 12:13:46 +0100359 /* Used by gbm fbs */
360 struct gbm_bo *bo;
361 struct gbm_surface *gbm_surface;
362
363 /* Used by dumb fbs */
364 void *map;
leng.fang32af9fc2024-06-13 11:22:15 +0800365 struct drm_plane *plane;
Daniel Stonedd1bc502019-06-17 12:13:46 +0100366};
367
Daniel Stone7d27df42021-11-18 16:01:03 +0000368struct drm_buffer_fb {
369 struct drm_fb *fb;
370 enum try_view_on_plane_failure_reasons failure_reasons;
371 struct wl_listener buffer_destroy_listener;
372};
373
Daniel Stonedd1bc502019-06-17 12:13:46 +0100374struct drm_edid {
375 char eisa_id[13];
376 char monitor_name[13];
377 char pnp_id[5];
378 char serial_number[13];
379};
380
381/**
382 * Pending state holds one or more drm_output_state structures, collected from
383 * performing repaint. This pending state is transient, and only lives between
384 * beginning a repaint group and flushing the results: after flush, each
385 * output state will complete and be retired separately.
386 */
387struct drm_pending_state {
388 struct drm_backend *backend;
389 struct wl_list output_list;
390};
391
392/*
393 * Output state holds the dynamic state for one Weston output, i.e. a KMS CRTC,
394 * plus >= 1 each of encoder/connector/plane. Since everything but the planes
395 * is currently statically assigned per-output, we mainly use this to track
396 * plane state.
397 *
398 * pending_state is set when the output state is owned by a pending_state,
399 * i.e. when it is being constructed and has not yet been applied. When the
400 * output state has been applied, the owning pending_state is freed.
401 */
402struct drm_output_state {
403 struct drm_pending_state *pending_state;
404 struct drm_output *output;
405 struct wl_list link;
406 enum dpms_enum dpms;
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530407 enum weston_hdcp_protection protection;
Daniel Stonedd1bc502019-06-17 12:13:46 +0100408 struct wl_list plane_list;
409};
410
411/**
Marius Vlad2538aac2019-10-14 11:05:30 +0300412 * An instance of this class is created each time we believe we have a plane
Maxime Roussin-Bélanger35e34502020-12-17 17:08:56 -0500413 * suitable to be used by a view as a direct scan-out. The list is initialized
Marius Vlad2538aac2019-10-14 11:05:30 +0300414 * and populated locally.
415 */
416struct drm_plane_zpos {
417 struct drm_plane *plane;
418 struct wl_list link; /**< :candidate_plane_zpos_list */
419};
420
421/**
Daniel Stonedd1bc502019-06-17 12:13:46 +0100422 * Plane state holds the dynamic state for a plane: where it is positioned,
423 * and which buffer it is currently displaying.
424 *
425 * The plane state is owned by an output state, except when setting an initial
426 * state. See drm_output_state for notes on state object lifetime.
427 */
428struct drm_plane_state {
429 struct drm_plane *plane;
430 struct drm_output *output;
431 struct drm_output_state *output_state;
432
433 struct drm_fb *fb;
Daniel Stone2ecc38b2021-11-18 15:33:17 +0000434 struct {
435 struct weston_buffer_reference buffer;
436 struct weston_buffer_release_reference release;
437 } fb_ref;
Daniel Stonedd1bc502019-06-17 12:13:46 +0100438
439 struct weston_view *ev; /**< maintained for drm_assign_planes only */
440
441 int32_t src_x, src_y;
442 uint32_t src_w, src_h;
443 int32_t dest_x, dest_y;
444 uint32_t dest_w, dest_h;
445
Marius Vladcdd6fa22019-08-29 20:42:00 +0300446 uint64_t zpos;
447
Daniel Stonedd1bc502019-06-17 12:13:46 +0100448 bool complete;
449
450 /* We don't own the fd, so we shouldn't close it */
451 int in_fence_fd;
452
Scott Anderson15c603c2020-06-02 17:39:43 +1200453 uint32_t damage_blob_id; /* damage to kernel */
Daniel Stonedd1bc502019-06-17 12:13:46 +0100454
455 struct wl_list link; /* drm_output_state::plane_list */
456};
457
458/**
459 * A plane represents one buffer, positioned within a CRTC, and stacked
460 * relative to other planes on the same CRTC.
461 *
462 * Each CRTC has a 'primary plane', which use used to display the classic
463 * framebuffer contents, as accessed through the legacy drmModeSetCrtc
464 * call (which combines setting the CRTC's actual physical mode, and the
465 * properties of the primary plane).
466 *
467 * The cursor plane also has its own alternate legacy API.
468 *
469 * Other planes are used opportunistically to display content we do not
470 * wish to blit into the primary plane. These non-primary/cursor planes
471 * are referred to as 'sprites'.
472 */
473struct drm_plane {
474 struct weston_plane base;
475
476 struct drm_backend *backend;
477
478 enum wdrm_plane_type type;
479
480 uint32_t possible_crtcs;
481 uint32_t plane_id;
Daniel Stone57d609a2021-11-16 18:56:09 +0000482 uint32_t plane_idx;
Daniel Stonedd1bc502019-06-17 12:13:46 +0100483
484 struct drm_property_info props[WDRM_PLANE__COUNT];
485
486 /* The last state submitted to the kernel for this plane. */
487 struct drm_plane_state *state_cur;
488
Marius Vladcdd6fa22019-08-29 20:42:00 +0300489 uint64_t zpos_min;
490 uint64_t zpos_max;
491
Daniel Stonedd1bc502019-06-17 12:13:46 +0100492 struct wl_list link;
493
leng.fang32af9fc2024-06-13 11:22:15 +0800494 bool is_video_plane;
495 int video_plane;
496 bool keep_last_frame;
497 uint32_t last_fb_id;
498 struct drm_crtc *crtc;
499
Scott Anderson74663092021-02-01 15:46:33 -0300500 struct weston_drm_format_array formats;
Daniel Stonedd1bc502019-06-17 12:13:46 +0100501};
502
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300503struct drm_connector {
504 struct drm_backend *backend;
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300505
506 drmModeConnector *conn;
507 uint32_t connector_id;
508
Leandro Ribeiro702fbf72020-08-18 17:35:05 -0300509 drmModeObjectProperties *props_drm;
510
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300511 /* Holds the properties for the connector */
512 struct drm_property_info props[WDRM_CONNECTOR__COUNT];
513};
514
Leandro Ribeiro96bef052020-09-09 15:23:49 -0300515struct drm_writeback {
516 /* drm_backend::writeback_connector_list */
517 struct wl_list link;
518
519 struct drm_backend *backend;
520 struct drm_connector connector;
521};
522
Daniel Stonedd1bc502019-06-17 12:13:46 +0100523struct drm_head {
524 struct weston_head base;
525 struct drm_backend *backend;
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300526 struct drm_connector connector;
Daniel Stonedd1bc502019-06-17 12:13:46 +0100527
Daniel Stonedd1bc502019-06-17 12:13:46 +0100528 struct drm_edid edid;
529
Daniel Stonedd1bc502019-06-17 12:13:46 +0100530 struct backlight *backlight;
531
532 drmModeModeInfo inherited_mode; /**< Original mode on the connector */
533 uint32_t inherited_crtc_id; /**< Original CRTC assignment */
534};
535
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300536struct drm_crtc {
537 /* drm_backend::crtc_list */
538 struct wl_list link;
Daniel Stonedd1bc502019-06-17 12:13:46 +0100539 struct drm_backend *backend;
540
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300541 /* The output driven by the CRTC */
542 struct drm_output *output;
543
Daniel Stonedd1bc502019-06-17 12:13:46 +0100544 uint32_t crtc_id; /* object ID to pass to DRM functions */
545 int pipe; /* index of CRTC in resource array / bitmasks */
546
547 /* Holds the properties for the CRTC */
548 struct drm_property_info props_crtc[WDRM_CRTC__COUNT];
leng.fang32af9fc2024-06-13 11:22:15 +0800549 bool output_change;
550 struct drm_output *disable_output;
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300551};
552
553struct drm_output {
554 struct weston_output base;
555 struct drm_backend *backend;
556 struct drm_crtc *crtc;
Daniel Stonedd1bc502019-06-17 12:13:46 +0100557
Emmanuel Gil Peyrot1b3ad092019-12-09 02:50:55 +0100558 bool page_flip_pending;
559 bool atomic_complete_pending;
560 bool destroy_pending;
561 bool disable_pending;
562 bool dpms_off_pending;
Daniel Stonedd1bc502019-06-17 12:13:46 +0100563
Stefan Agner974390a2019-07-08 00:42:05 +0200564 uint32_t gbm_cursor_handle[2];
Daniel Stonedd1bc502019-06-17 12:13:46 +0100565 struct drm_fb *gbm_cursor_fb[2];
566 struct drm_plane *cursor_plane;
567 struct weston_view *cursor_view;
Alexandros Frantzis10937fe2021-06-14 13:09:44 +0300568 struct wl_listener cursor_view_destroy_listener;
Daniel Stonedd1bc502019-06-17 12:13:46 +0100569 int current_cursor;
570
571 struct gbm_surface *gbm_surface;
572 uint32_t gbm_format;
573 uint32_t gbm_bo_flags;
574
575 /* Plane being displayed directly on the CRTC */
576 struct drm_plane *scanout_plane;
577
578 /* The last state submitted to the kernel for this CRTC. */
579 struct drm_output_state *state_cur;
580 /* The previously-submitted state, where the hardware has not
581 * yet acknowledged completion of state_cur. */
582 struct drm_output_state *state_last;
583
584 struct drm_fb *dumb[2];
585 pixman_image_t *image[2];
586 int current_image;
587 pixman_region32_t previous_damage;
588
589 struct vaapi_recorder *recorder;
590 struct wl_listener recorder_frame_listener;
591
592 struct wl_event_source *pageflip_timer;
593
594 bool virtual;
leng.fang32af9fc2024-06-13 11:22:15 +0800595 int video_plane_count;
Daniel Stonedd1bc502019-06-17 12:13:46 +0100596
597 submit_frame_cb virtual_submit_frame;
leng.fang32af9fc2024-06-13 11:22:15 +0800598
599#ifdef MESON_DRM_FIX_UI_SIZE
600 bool current_mode_need_restore;
601
602 /* the real display mode size will saved out of the current mode */
603 struct weston_size display_size;
604#endif
605
Daniel Stonedd1bc502019-06-17 12:13:46 +0100606};
607
608static inline struct drm_head *
609to_drm_head(struct weston_head *base)
610{
611 return container_of(base, struct drm_head, base);
612}
613
614static inline struct drm_output *
615to_drm_output(struct weston_output *base)
616{
617 return container_of(base, struct drm_output, base);
618}
619
620static inline struct drm_backend *
621to_drm_backend(struct weston_compositor *base)
622{
623 return container_of(base->backend, struct drm_backend, base);
624}
625
leng.fang32af9fc2024-06-13 11:22:15 +0800626static inline struct drm_plane *
627to_drm_plane(struct weston_plane *base)
628{
629 return container_of(base, struct drm_plane, base);
630}
631
Daniel Stonedd1bc502019-06-17 12:13:46 +0100632static inline struct drm_mode *
633to_drm_mode(struct weston_mode *base)
634{
635 return container_of(base, struct drm_mode, base);
636}
Daniel Stonefbe6c1d2019-06-17 16:04:26 +0100637
Marius Vlad3dea57a2019-09-27 20:45:41 +0300638static inline const char *
639drm_output_get_plane_type_name(struct drm_plane *p)
640{
641 switch (p->type) {
642 case WDRM_PLANE_TYPE_PRIMARY:
643 return "primary";
644 case WDRM_PLANE_TYPE_CURSOR:
645 return "cursor";
646 case WDRM_PLANE_TYPE_OVERLAY:
647 return "overlay";
648 default:
649 assert(0);
650 break;
651 }
652}
653
Leandro Ribeirob00d1a22020-08-13 14:12:28 -0300654struct drm_crtc *
655drm_crtc_find(struct drm_backend *b, uint32_t crtc_id);
Daniel Stone4c2fc702019-06-18 11:12:07 +0100656
657struct drm_head *
658drm_head_find_by_connector(struct drm_backend *backend, uint32_t connector_id);
659
Daniel Stone7580b3c2019-06-18 11:16:53 +0100660static inline bool
661drm_view_transform_supported(struct weston_view *ev, struct weston_output *output)
662{
663 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
664
665 /* This will incorrectly disallow cases where the combination of
666 * buffer and view transformations match the output transform.
667 * Fixing this requires a full analysis of the transformation
668 * chain. */
669 if (ev->transform.enabled &&
670 ev->transform.matrix.type >= WESTON_MATRIX_TRANSFORM_ROTATE)
671 return false;
672
673 if (viewport->buffer.transform != output->transform)
674 return false;
675
676 return true;
677}
leng.fang32af9fc2024-06-13 11:22:15 +0800678struct weston_plane *
679drm_get_weston_plane(struct drm_backend *backend, struct drm_plane* drm_plane);
Daniel Stone7580b3c2019-06-18 11:16:53 +0100680
Daniel Stonefbe6c1d2019-06-17 16:04:26 +0100681int
682drm_mode_ensure_blob(struct drm_backend *backend, struct drm_mode *mode);
683
684struct drm_mode *
685drm_output_choose_mode(struct drm_output *output,
686 struct weston_mode *target_mode);
687void
Leandro Ribeiro702fbf72020-08-18 17:35:05 -0300688update_head_from_connector(struct drm_head *head);
Daniel Stonefbe6c1d2019-06-17 16:04:26 +0100689
690void
691drm_mode_list_destroy(struct drm_backend *backend, struct wl_list *mode_list);
692
693void
694drm_output_print_modes(struct drm_output *output);
695
696int
697drm_output_set_mode(struct weston_output *base,
698 enum weston_drm_backend_output_mode mode,
699 const char *modeline);
700
Daniel Stone4c2fc702019-06-18 11:12:07 +0100701void
702drm_property_info_populate(struct drm_backend *b,
703 const struct drm_property_info *src,
704 struct drm_property_info *info,
705 unsigned int num_infos,
706 drmModeObjectProperties *props);
Daniel Stonefbe6c1d2019-06-17 16:04:26 +0100707uint64_t
708drm_property_get_value(struct drm_property_info *info,
709 const drmModeObjectProperties *props,
710 uint64_t def);
Marius Vlad1accffe2019-11-01 12:00:09 +0200711uint64_t *
712drm_property_get_range_values(struct drm_property_info *info,
713 const drmModeObjectProperties *props);
Daniel Stone4c2fc702019-06-18 11:12:07 +0100714int
715drm_plane_populate_formats(struct drm_plane *plane, const drmModePlane *kplane,
Stefan Agner465ab2c2020-06-17 23:36:44 +0200716 const drmModeObjectProperties *props,
717 const bool use_modifiers);
Daniel Stone4c2fc702019-06-18 11:12:07 +0100718void
719drm_property_info_free(struct drm_property_info *info, int num_props);
720
721extern struct drm_property_enum_info plane_type_enums[];
722extern const struct drm_property_info plane_props[];
723extern struct drm_property_enum_info dpms_state_enums[];
Ankit Nautiyala344fe32019-05-14 18:36:08 +0530724extern struct drm_property_enum_info content_protection_enums[];
725extern struct drm_property_enum_info hdcp_content_type_enums[];
Daniel Stone4c2fc702019-06-18 11:12:07 +0100726extern const struct drm_property_info connector_props[];
727extern const struct drm_property_info crtc_props[];
728
729int
730init_kms_caps(struct drm_backend *b);
731
732int
733drm_pending_state_test(struct drm_pending_state *pending_state);
734int
735drm_pending_state_apply(struct drm_pending_state *pending_state);
736int
737drm_pending_state_apply_sync(struct drm_pending_state *pending_state);
738
739void
740drm_output_set_gamma(struct weston_output *output_base,
741 uint16_t size, uint16_t *r, uint16_t *g, uint16_t *b);
742
743void
744drm_output_update_msc(struct drm_output *output, unsigned int seq);
745void
746drm_output_update_complete(struct drm_output *output, uint32_t flags,
747 unsigned int sec, unsigned int usec);
748int
749on_drm_input(int fd, uint32_t mask, void *data);
750
Daniel Stone7580b3c2019-06-18 11:16:53 +0100751struct drm_fb *
752drm_fb_ref(struct drm_fb *fb);
753void
754drm_fb_unref(struct drm_fb *fb);
755
756struct drm_fb *
757drm_fb_create_dumb(struct drm_backend *b, int width, int height,
758 uint32_t format);
759struct drm_fb *
760drm_fb_get_from_bo(struct gbm_bo *bo, struct drm_backend *backend,
761 bool is_opaque, enum drm_fb_type type);
Daniel Stone6b466f22019-06-18 11:30:54 +0100762
Alexandros Frantzis10937fe2021-06-14 13:09:44 +0300763void
764drm_output_set_cursor_view(struct drm_output *output, struct weston_view *ev);
765
Stefan Agnerccf24072019-07-09 22:02:00 +0200766#ifdef BUILD_DRM_GBM
767extern struct drm_fb *
Leandro Ribeiro0a7034c2021-09-13 14:52:53 -0300768drm_fb_get_from_view(struct drm_output_state *state, struct weston_view *ev,
769 uint32_t *try_view_on_plane_failure_reasons);
Marius Vlad81bada52019-11-11 00:27:17 +0200770extern bool
771drm_can_scanout_dmabuf(struct weston_compositor *ec,
772 struct linux_dmabuf_buffer *dmabuf);
Stefan Agnerccf24072019-07-09 22:02:00 +0200773#else
774static inline struct drm_fb *
Leandro Ribeiro0a7034c2021-09-13 14:52:53 -0300775drm_fb_get_from_view(struct drm_output_state *state, struct weston_view *ev,
776 uint32_t *try_view_on_plane_failure_reasons)
Stefan Agnerccf24072019-07-09 22:02:00 +0200777{
778 return NULL;
779}
Marius Vlad81bada52019-11-11 00:27:17 +0200780static inline bool
781drm_can_scanout_dmabuf(struct weston_compositor *ec,
782 struct linux_dmabuf_buffer *dmabuf)
783{
784 return false;
785}
Stefan Agnerccf24072019-07-09 22:02:00 +0200786#endif
Daniel Stone6b466f22019-06-18 11:30:54 +0100787
788struct drm_pending_state *
789drm_pending_state_alloc(struct drm_backend *backend);
790void
791drm_pending_state_free(struct drm_pending_state *pending_state);
792struct drm_output_state *
793drm_pending_state_get_output(struct drm_pending_state *pending_state,
794 struct drm_output *output);
795
796
797/**
798 * Mode for drm_output_state_duplicate.
799 */
800enum drm_output_state_duplicate_mode {
801 DRM_OUTPUT_STATE_CLEAR_PLANES, /**< reset all planes to off */
802 DRM_OUTPUT_STATE_PRESERVE_PLANES, /**< preserve plane state */
803};
804
805struct drm_output_state *
806drm_output_state_alloc(struct drm_output *output,
807 struct drm_pending_state *pending_state);
808struct drm_output_state *
809drm_output_state_duplicate(struct drm_output_state *src,
810 struct drm_pending_state *pending_state,
811 enum drm_output_state_duplicate_mode plane_mode);
812void
813drm_output_state_free(struct drm_output_state *state);
814struct drm_plane_state *
815drm_output_state_get_plane(struct drm_output_state *state_output,
816 struct drm_plane *plane);
817struct drm_plane_state *
818drm_output_state_get_existing_plane(struct drm_output_state *state_output,
819 struct drm_plane *plane);
820
821
822
823struct drm_plane_state *
824drm_plane_state_alloc(struct drm_output_state *state_output,
825 struct drm_plane *plane);
826struct drm_plane_state *
827drm_plane_state_duplicate(struct drm_output_state *state_output,
828 struct drm_plane_state *src);
829void
830drm_plane_state_free(struct drm_plane_state *state, bool force);
831void
832drm_plane_state_put_back(struct drm_plane_state *state);
833bool
834drm_plane_state_coords_for_view(struct drm_plane_state *state,
Marius Vlad2538aac2019-10-14 11:05:30 +0300835 struct weston_view *ev, uint64_t zpos);
Alexandros Frantzis99751342020-05-18 15:22:49 +0300836void
837drm_plane_reset_state(struct drm_plane *plane);
Daniel Stonee404b722019-06-22 18:40:31 +0100838
839void
840drm_assign_planes(struct weston_output *output_base, void *repaint_data);
841
842bool
843drm_plane_is_available(struct drm_plane *plane, struct drm_output *output);
Stefan Agner3654c672019-07-09 00:50:30 +0200844
845void
846drm_output_render(struct drm_output_state *state, pixman_region32_t *damage);
847
848int
849parse_gbm_format(const char *s, uint32_t default_value, uint32_t *gbm_format);
850
851extern struct gl_renderer_interface *gl_renderer;
852
853#ifdef BUILD_DRM_VIRTUAL
854extern int
855drm_backend_init_virtual_output_api(struct weston_compositor *compositor);
856#else
857inline static int
858drm_backend_init_virtual_output_api(struct weston_compositor *compositor)
859{
860 return 0;
861}
862#endif
863
Stefan Agnerccf24072019-07-09 22:02:00 +0200864#ifdef BUILD_DRM_GBM
865int
866init_egl(struct drm_backend *b);
867
Stefan Agner3654c672019-07-09 00:50:30 +0200868int
869drm_output_init_egl(struct drm_output *output, struct drm_backend *b);
Stefan Agnerccf24072019-07-09 22:02:00 +0200870
Stefan Agner3654c672019-07-09 00:50:30 +0200871void
872drm_output_fini_egl(struct drm_output *output);
873
Stefan Agnerccf24072019-07-09 22:02:00 +0200874struct drm_fb *
875drm_output_render_gl(struct drm_output_state *state, pixman_region32_t *damage);
876
877void
878renderer_switch_binding(struct weston_keyboard *keyboard,
879 const struct timespec *time, uint32_t key, void *data);
880#else
881inline static int
882init_egl(struct drm_backend *b)
883{
884 weston_log("Compiled without GBM/EGL support\n");
885 return -1;
886}
887
888inline static int
889drm_output_init_egl(struct drm_output *output, struct drm_backend *b)
890{
891 return -1;
892}
893
894inline static void
895drm_output_fini_egl(struct drm_output *output)
896{
897}
898
899inline static struct drm_fb *
900drm_output_render_gl(struct drm_output_state *state, pixman_region32_t *damage)
901{
902 return NULL;
903}
904
905inline static void
906renderer_switch_binding(struct weston_keyboard *keyboard,
907 const struct timespec *time, uint32_t key, void *data)
908{
909 weston_log("Compiled without GBM/EGL support\n");
910}
911#endif