blob: 7cae93c58ce5003bfa3fcef3a2b4f7d105d3fcc8 [file] [log] [blame]
Pekka Paalanen230f3b12014-09-29 14:18:40 -04001/*
2 * Copyright © 2014, 2015 Collabora, Ltd.
3 *
Yong Bakos53361532017-01-23 06:17:44 -08004 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
Pekka Paalanen230f3b12014-09-29 14:18:40 -040011 *
Yong Bakos53361532017-01-23 06:17:44 -080012 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial
14 * portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
Pekka Paalanen230f3b12014-09-29 14:18:40 -040024 */
25
26#ifndef WESTON_LINUX_DMABUF_H
27#define WESTON_LINUX_DMABUF_H
28
29#include <stdint.h>
Leandro Ribeiro54293022021-10-12 14:48:36 -030030#include "linux-dmabuf-unstable-v1-server-protocol.h"
Pekka Paalanen230f3b12014-09-29 14:18:40 -040031
32#define MAX_DMABUF_PLANES 4
33
34struct linux_dmabuf_buffer;
35typedef void (*dmabuf_user_data_destroy_func)(
36 struct linux_dmabuf_buffer *buffer);
37
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +000038struct dmabuf_attributes {
Pekka Paalanen230f3b12014-09-29 14:18:40 -040039 int32_t width;
40 int32_t height;
41 uint32_t format;
42 uint32_t flags; /* enum zlinux_buffer_params_flags */
43 int n_planes;
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +000044 int fd[MAX_DMABUF_PLANES];
Pekka Paalanen230f3b12014-09-29 14:18:40 -040045 uint32_t offset[MAX_DMABUF_PLANES];
46 uint32_t stride[MAX_DMABUF_PLANES];
47 uint64_t modifier[MAX_DMABUF_PLANES];
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +000048};
49
50struct linux_dmabuf_buffer {
51 struct wl_resource *buffer_resource;
52 struct wl_resource *params_resource;
53 struct weston_compositor *compositor;
54 struct dmabuf_attributes attributes;
Pekka Paalanen230f3b12014-09-29 14:18:40 -040055
56 void *user_data;
57 dmabuf_user_data_destroy_func user_data_destroy_func;
58
59 /* XXX:
60 *
61 * Add backend private data. This would be for the backend
62 * to do all additional imports it might ever use in advance.
63 * The basic principle, even if not implemented in drivers today,
64 * is that dmabufs are first attached, but the actual allocation
65 * is deferred to first use. This would allow the exporter and all
66 * attachers to agree on how to allocate.
67 *
68 * The DRM backend would use this to create drmFBs for each
69 * dmabuf_buffer, just in case at some point it would become
70 * feasible to scan it out directly. This would improve the
71 * possibilities to successfully scan out, avoiding compositing.
72 */
Marius Vladebd10e52019-11-16 19:22:48 +020073
74 /**< marked as scan-out capable, avoids any composition */
75 bool direct_display;
Pekka Paalanen230f3b12014-09-29 14:18:40 -040076};
77
Leandro Ribeiro8eb84142021-01-18 19:36:48 -030078enum weston_dmabuf_feedback_tranche_preference {
79 RENDERER_PREF = 0,
80 SCANOUT_PREF = 1
81};
82
83struct weston_dmabuf_feedback_format_table {
84 int fd;
85 unsigned int size;
86
87 /* This is a pointer to the region of memory where we mapped the file
88 * that clients receive. We fill it with the format/modifier pairs
89 * supported by the renderer. We don't formats not supported by the
90 * renderer in the table, as we must always be able to fallback to the
91 * renderer if direct scanout fails. */
92 struct {
93 uint32_t format;
94 uint32_t pad; /* unused */
95 uint64_t modifier;
96 } *data;
97
98 /* Indices of the renderer formats in the table. As the table consists
99 * of formats supported by the renderer, this goes from 0 to the number
100 * of pairs in the table. */
101 struct wl_array renderer_formats_indices;
Leandro Ribeiro54293022021-10-12 14:48:36 -0300102 /* Indices of the scanout formats (union of KMS plane's supported
103 * formats intersected with the renderer formats). */
104 struct wl_array scanout_formats_indices;
Leandro Ribeiro8eb84142021-01-18 19:36:48 -0300105};
106
107struct weston_dmabuf_feedback {
108 /* We can have multiple clients subscribing to the same surface dma-buf
109 * feedback. As they are dynamic and we need to re-send them multiple
110 * times during Weston's lifetime, we need to keep track of the
111 * resources of each client. In the case of the default feedback this is
112 * not necessary, as we only advertise them when clients subscribe. IOW,
113 * default feedback events are never re-sent. */
114 struct wl_list resource_list;
115
116 dev_t main_device;
117
118 /* weston_dmabuf_feedback_tranche::link */
119 struct wl_list tranche_list;
Leandro Ribeiro54293022021-10-12 14:48:36 -0300120
121 /* We use this timer to know if the scene has stabilized and that would
122 * be useful to resend dma-buf feedback events to clients. Consider the
123 * timer off when action_needed == ACTION_NEEDED_NONE. See enum
124 * actions_needed_dmabuf_feedback. */
125 struct timespec timer;
126 uint32_t action_needed;
Leandro Ribeiro8eb84142021-01-18 19:36:48 -0300127};
128
129struct weston_dmabuf_feedback_tranche {
130 /* weston_dmabuf_feedback::tranche_list */
131 struct wl_list link;
132
Leandro Ribeiro54293022021-10-12 14:48:36 -0300133 /* Instead of destroying tranches and reconstructing them when necessary
134 * (it can be expensive), we have this flag to know if the tranche
135 * should be advertised or not. This is particularly useful for the
136 * scanout tranche, as based on the DRM-backend feedback and the current
137 * scene (which changes a lot during compositor lifetime) we can decide
138 * to send it or not. */
139 bool active;
140
Leandro Ribeiro8eb84142021-01-18 19:36:48 -0300141 dev_t target_device;
142 uint32_t flags;
143 enum weston_dmabuf_feedback_tranche_preference preference;
144
145 struct wl_array formats_indices;
146};
147
Pekka Paalanen230f3b12014-09-29 14:18:40 -0400148int
149linux_dmabuf_setup(struct weston_compositor *compositor);
150
Marius Vladebd10e52019-11-16 19:22:48 +0200151int
152weston_direct_display_setup(struct weston_compositor *compositor);
153
Pekka Paalanen230f3b12014-09-29 14:18:40 -0400154struct linux_dmabuf_buffer *
155linux_dmabuf_buffer_get(struct wl_resource *resource);
156
157void
158linux_dmabuf_buffer_set_user_data(struct linux_dmabuf_buffer *buffer,
159 void *data,
160 dmabuf_user_data_destroy_func func);
161void *
162linux_dmabuf_buffer_get_user_data(struct linux_dmabuf_buffer *buffer);
163
164void
165linux_dmabuf_buffer_send_server_error(struct linux_dmabuf_buffer *buffer,
166 const char *msg);
167
Leandro Ribeiro8eb84142021-01-18 19:36:48 -0300168struct weston_dmabuf_feedback *
169weston_dmabuf_feedback_create(dev_t main_device);
170
171void
172weston_dmabuf_feedback_destroy(struct weston_dmabuf_feedback *dmabuf_feedback);
173
Leandro Ribeiro54293022021-10-12 14:48:36 -0300174void
175weston_dmabuf_feedback_send_all(struct weston_dmabuf_feedback *dmabuf_feedback,
176 struct weston_dmabuf_feedback_format_table *format_table);
177
178struct weston_dmabuf_feedback_tranche *
179weston_dmabuf_feedback_find_tranche(struct weston_dmabuf_feedback *dmabuf_feedback,
180 dev_t target_device, uint32_t flags,
181 enum weston_dmabuf_feedback_tranche_preference preference);
182
Leandro Ribeiro8eb84142021-01-18 19:36:48 -0300183struct weston_dmabuf_feedback_format_table *
184weston_dmabuf_feedback_format_table_create(const struct weston_drm_format_array *renderer_formats);
185
186void
187weston_dmabuf_feedback_format_table_destroy(struct weston_dmabuf_feedback_format_table *format_table);
188
Leandro Ribeiro54293022021-10-12 14:48:36 -0300189int
190weston_dmabuf_feedback_format_table_set_scanout_indices(struct weston_dmabuf_feedback_format_table *format_table,
191 const struct weston_drm_format_array *scanout_formats);
192
Leandro Ribeiro8eb84142021-01-18 19:36:48 -0300193struct weston_dmabuf_feedback_tranche *
194weston_dmabuf_feedback_tranche_create(struct weston_dmabuf_feedback *dmabuf_feedback,
195 struct weston_dmabuf_feedback_format_table *format_table,
196 dev_t target_device, uint32_t flags,
197 enum weston_dmabuf_feedback_tranche_preference preference);
198
Pekka Paalanen230f3b12014-09-29 14:18:40 -0400199#endif /* WESTON_LINUX_DMABUF_H */