Pekka Paalanen | 230f3b1 | 2014-09-29 14:18:40 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2014, 2015 Collabora, Ltd. |
| 3 | * |
Yong Bakos | 5336153 | 2017-01-23 06:17:44 -0800 | [diff] [blame] | 4 | * 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 Paalanen | 230f3b1 | 2014-09-29 14:18:40 -0400 | [diff] [blame] | 11 | * |
Yong Bakos | 5336153 | 2017-01-23 06:17:44 -0800 | [diff] [blame] | 12 | * 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 Paalanen | 230f3b1 | 2014-09-29 14:18:40 -0400 | [diff] [blame] | 24 | */ |
| 25 | |
| 26 | #ifndef WESTON_LINUX_DMABUF_H |
| 27 | #define WESTON_LINUX_DMABUF_H |
| 28 | |
| 29 | #include <stdint.h> |
Leandro Ribeiro | 5429302 | 2021-10-12 14:48:36 -0300 | [diff] [blame] | 30 | #include "linux-dmabuf-unstable-v1-server-protocol.h" |
Pekka Paalanen | 230f3b1 | 2014-09-29 14:18:40 -0400 | [diff] [blame] | 31 | |
| 32 | #define MAX_DMABUF_PLANES 4 |
| 33 | |
| 34 | struct linux_dmabuf_buffer; |
| 35 | typedef void (*dmabuf_user_data_destroy_func)( |
| 36 | struct linux_dmabuf_buffer *buffer); |
| 37 | |
Emmanuel Gil Peyrot | c399692 | 2015-11-24 19:28:24 +0000 | [diff] [blame] | 38 | struct dmabuf_attributes { |
Pekka Paalanen | 230f3b1 | 2014-09-29 14:18:40 -0400 | [diff] [blame] | 39 | 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 Peyrot | c399692 | 2015-11-24 19:28:24 +0000 | [diff] [blame] | 44 | int fd[MAX_DMABUF_PLANES]; |
Pekka Paalanen | 230f3b1 | 2014-09-29 14:18:40 -0400 | [diff] [blame] | 45 | uint32_t offset[MAX_DMABUF_PLANES]; |
| 46 | uint32_t stride[MAX_DMABUF_PLANES]; |
| 47 | uint64_t modifier[MAX_DMABUF_PLANES]; |
Emmanuel Gil Peyrot | c399692 | 2015-11-24 19:28:24 +0000 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | struct 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 Paalanen | 230f3b1 | 2014-09-29 14:18:40 -0400 | [diff] [blame] | 55 | |
| 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 Vlad | ebd10e5 | 2019-11-16 19:22:48 +0200 | [diff] [blame] | 73 | |
| 74 | /**< marked as scan-out capable, avoids any composition */ |
| 75 | bool direct_display; |
Pekka Paalanen | 230f3b1 | 2014-09-29 14:18:40 -0400 | [diff] [blame] | 76 | }; |
| 77 | |
Leandro Ribeiro | 8eb8414 | 2021-01-18 19:36:48 -0300 | [diff] [blame] | 78 | enum weston_dmabuf_feedback_tranche_preference { |
| 79 | RENDERER_PREF = 0, |
| 80 | SCANOUT_PREF = 1 |
| 81 | }; |
| 82 | |
| 83 | struct 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 Ribeiro | 5429302 | 2021-10-12 14:48:36 -0300 | [diff] [blame] | 102 | /* 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 Ribeiro | 8eb8414 | 2021-01-18 19:36:48 -0300 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | struct 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 Ribeiro | 5429302 | 2021-10-12 14:48:36 -0300 | [diff] [blame] | 120 | |
| 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 Ribeiro | 8eb8414 | 2021-01-18 19:36:48 -0300 | [diff] [blame] | 127 | }; |
| 128 | |
| 129 | struct weston_dmabuf_feedback_tranche { |
| 130 | /* weston_dmabuf_feedback::tranche_list */ |
| 131 | struct wl_list link; |
| 132 | |
Leandro Ribeiro | 5429302 | 2021-10-12 14:48:36 -0300 | [diff] [blame] | 133 | /* 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 Ribeiro | 8eb8414 | 2021-01-18 19:36:48 -0300 | [diff] [blame] | 141 | 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 Paalanen | 230f3b1 | 2014-09-29 14:18:40 -0400 | [diff] [blame] | 148 | int |
| 149 | linux_dmabuf_setup(struct weston_compositor *compositor); |
| 150 | |
Marius Vlad | ebd10e5 | 2019-11-16 19:22:48 +0200 | [diff] [blame] | 151 | int |
| 152 | weston_direct_display_setup(struct weston_compositor *compositor); |
| 153 | |
Pekka Paalanen | 230f3b1 | 2014-09-29 14:18:40 -0400 | [diff] [blame] | 154 | struct linux_dmabuf_buffer * |
| 155 | linux_dmabuf_buffer_get(struct wl_resource *resource); |
| 156 | |
| 157 | void |
| 158 | linux_dmabuf_buffer_set_user_data(struct linux_dmabuf_buffer *buffer, |
| 159 | void *data, |
| 160 | dmabuf_user_data_destroy_func func); |
| 161 | void * |
| 162 | linux_dmabuf_buffer_get_user_data(struct linux_dmabuf_buffer *buffer); |
| 163 | |
| 164 | void |
| 165 | linux_dmabuf_buffer_send_server_error(struct linux_dmabuf_buffer *buffer, |
| 166 | const char *msg); |
| 167 | |
Leandro Ribeiro | 8eb8414 | 2021-01-18 19:36:48 -0300 | [diff] [blame] | 168 | struct weston_dmabuf_feedback * |
| 169 | weston_dmabuf_feedback_create(dev_t main_device); |
| 170 | |
| 171 | void |
| 172 | weston_dmabuf_feedback_destroy(struct weston_dmabuf_feedback *dmabuf_feedback); |
| 173 | |
Leandro Ribeiro | 5429302 | 2021-10-12 14:48:36 -0300 | [diff] [blame] | 174 | void |
| 175 | weston_dmabuf_feedback_send_all(struct weston_dmabuf_feedback *dmabuf_feedback, |
| 176 | struct weston_dmabuf_feedback_format_table *format_table); |
| 177 | |
| 178 | struct weston_dmabuf_feedback_tranche * |
| 179 | weston_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 Ribeiro | 8eb8414 | 2021-01-18 19:36:48 -0300 | [diff] [blame] | 183 | struct weston_dmabuf_feedback_format_table * |
| 184 | weston_dmabuf_feedback_format_table_create(const struct weston_drm_format_array *renderer_formats); |
| 185 | |
| 186 | void |
| 187 | weston_dmabuf_feedback_format_table_destroy(struct weston_dmabuf_feedback_format_table *format_table); |
| 188 | |
Leandro Ribeiro | 5429302 | 2021-10-12 14:48:36 -0300 | [diff] [blame] | 189 | int |
| 190 | weston_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 Ribeiro | 8eb8414 | 2021-01-18 19:36:48 -0300 | [diff] [blame] | 193 | struct weston_dmabuf_feedback_tranche * |
| 194 | weston_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 Paalanen | 230f3b1 | 2014-09-29 14:18:40 -0400 | [diff] [blame] | 199 | #endif /* WESTON_LINUX_DMABUF_H */ |