blob: 99e30a1fc4b90f452ec75d44bccb5cdd4939b9a5 [file] [log] [blame]
Alexandros Frantzis27d7c392018-10-19 12:14:11 +03001/*
2 * Copyright © 2018 Collabora, Ltd.
3 *
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:
11 *
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.
24 */
25
26#include "config.h"
27
Alexandros Frantzisacff29b2018-10-19 12:14:11 +030028#include <assert.h>
Alexandros Frantzis27d7c392018-10-19 12:14:11 +030029#include <inttypes.h>
30
31#include "compositor.h"
32#include "linux-explicit-synchronization.h"
33#include "linux-explicit-synchronization-unstable-v1-server-protocol.h"
Alexandros Frantzisacff29b2018-10-19 12:14:11 +030034#include "linux-sync-file.h"
35#include "shared/fd-util.h"
Alexandros Frantzis27d7c392018-10-19 12:14:11 +030036
37static void
Alexandros Frantzis67629672018-10-19 12:14:11 +030038destroy_linux_buffer_release(struct wl_resource *resource)
39{
40 struct weston_buffer_release *buffer_release =
41 wl_resource_get_user_data(resource);
42
43 fd_clear(&buffer_release->fence_fd);
44 free(buffer_release);
45}
46
47static void
Alexandros Frantzis27d7c392018-10-19 12:14:11 +030048destroy_linux_surface_synchronization(struct wl_resource *resource)
49{
50 struct weston_surface *surface =
51 wl_resource_get_user_data(resource);
52
Alexandros Frantzisacff29b2018-10-19 12:14:11 +030053 if (surface) {
54 fd_clear(&surface->pending.acquire_fence_fd);
Alexandros Frantzis27d7c392018-10-19 12:14:11 +030055 surface->synchronization_resource = NULL;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +030056 }
Alexandros Frantzis27d7c392018-10-19 12:14:11 +030057}
58
59static void
60linux_surface_synchronization_destroy(struct wl_client *client,
61 struct wl_resource *resource)
62{
63 wl_resource_destroy(resource);
64}
65
66static void
67linux_surface_synchronization_set_acquire_fence(struct wl_client *client,
68 struct wl_resource *resource,
69 int32_t fd)
70{
Alexandros Frantzisacff29b2018-10-19 12:14:11 +030071 struct weston_surface *surface = wl_resource_get_user_data(resource);
72
73 if (!surface) {
74 wl_resource_post_error(
75 resource,
76 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_SURFACE,
77 "surface no longer exists");
78 goto err;
79 }
80
81 if (!linux_sync_file_is_valid(fd)) {
82 wl_resource_post_error(
83 resource,
84 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_INVALID_FENCE,
85 "invalid fence fd");
86 goto err;
87 }
88
89 if (surface->pending.acquire_fence_fd != -1) {
90 wl_resource_post_error(
91 resource,
92 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_DUPLICATE_FENCE,
93 "already have a fence fd");
94 goto err;
95 }
96
97 fd_update(&surface->pending.acquire_fence_fd, fd);
98
99 return;
100
101err:
102 close(fd);
Alexandros Frantzis27d7c392018-10-19 12:14:11 +0300103}
104
105static void
106linux_surface_synchronization_get_release(struct wl_client *client,
107 struct wl_resource *resource,
108 uint32_t id)
109{
Alexandros Frantzis67629672018-10-19 12:14:11 +0300110 struct weston_surface *surface =
111 wl_resource_get_user_data(resource);
112 struct weston_buffer_release *buffer_release;
113
114 if (!surface) {
115 wl_resource_post_error(
116 resource,
117 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_SURFACE,
118 "surface no longer exists");
119 return;
120 }
121
122 if (surface->pending.buffer_release_ref.buffer_release) {
123 wl_resource_post_error(
124 resource,
125 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_DUPLICATE_RELEASE,
126 "already has a buffer release");
127 return;
128 }
129
130 buffer_release = zalloc(sizeof *buffer_release);
131 if (buffer_release == NULL)
132 goto err_alloc;
133
134 buffer_release->fence_fd = -1;
135 buffer_release->resource =
136 wl_resource_create(client,
137 &zwp_linux_buffer_release_v1_interface,
138 wl_resource_get_version(resource), id);
139 if (!buffer_release->resource)
140 goto err_create;
141
142 wl_resource_set_implementation(buffer_release->resource, NULL,
143 buffer_release,
144 destroy_linux_buffer_release);
145
146 weston_buffer_release_reference(&surface->pending.buffer_release_ref,
147 buffer_release);
148
149 return;
150
151err_create:
152 free(buffer_release);
153
154err_alloc:
Alexandros Frantzis27d7c392018-10-19 12:14:11 +0300155 wl_client_post_no_memory(client);
Alexandros Frantzis67629672018-10-19 12:14:11 +0300156
Alexandros Frantzis27d7c392018-10-19 12:14:11 +0300157}
158
159const struct zwp_linux_surface_synchronization_v1_interface
160linux_surface_synchronization_implementation = {
161 linux_surface_synchronization_destroy,
162 linux_surface_synchronization_set_acquire_fence,
163 linux_surface_synchronization_get_release,
164};
165
166static void
167linux_explicit_synchronization_destroy(struct wl_client *client,
168 struct wl_resource *resource)
169{
170 wl_resource_destroy(resource);
171}
172
173static void
174linux_explicit_synchronization_get_synchronization(struct wl_client *client,
175 struct wl_resource *resource,
176 uint32_t id,
177 struct wl_resource *surface_resource)
178{
179 struct weston_surface *surface =
180 wl_resource_get_user_data(surface_resource);
181
182 if (surface->synchronization_resource) {
183 wl_resource_post_error(
184 resource,
185 ZWP_LINUX_EXPLICIT_SYNCHRONIZATION_V1_ERROR_SYNCHRONIZATION_EXISTS,
186 "wl_surface@%"PRIu32" already has a synchronization object",
187 wl_resource_get_id(surface_resource));
188 return;
189 }
190
191 surface->synchronization_resource =
192 wl_resource_create(client,
193 &zwp_linux_surface_synchronization_v1_interface,
194 wl_resource_get_version(resource), id);
195 if (!surface->synchronization_resource) {
196 wl_client_post_no_memory(client);
197 return;
198 }
199
200 wl_resource_set_implementation(surface->synchronization_resource,
201 &linux_surface_synchronization_implementation,
202 surface,
203 destroy_linux_surface_synchronization);
204}
205
206static const struct zwp_linux_explicit_synchronization_v1_interface
207linux_explicit_synchronization_implementation = {
208 linux_explicit_synchronization_destroy,
209 linux_explicit_synchronization_get_synchronization
210};
211
212static void
213bind_linux_explicit_synchronization(struct wl_client *client,
214 void *data, uint32_t version,
215 uint32_t id)
216{
217 struct weston_compositor *compositor = data;
218 struct wl_resource *resource;
219
220 resource = wl_resource_create(client,
221 &zwp_linux_explicit_synchronization_v1_interface,
222 version, id);
223 if (resource == NULL) {
224 wl_client_post_no_memory(client);
225 return;
226 }
227
228 wl_resource_set_implementation(resource,
229 &linux_explicit_synchronization_implementation,
230 compositor, NULL);
231}
232
233/** Advertise linux_explicit_synchronization support
234 *
235 * Calling this initializes the zwp_linux_explicit_synchronization_v1
236 * protocol support, so that the interface will be advertised to clients.
237 * Essentially it creates a global. Do not call this function multiple times
238 * in the compositor's lifetime. There is no way to deinit explicitly, globals
239 * will be reaped when the wl_display gets destroyed.
240 *
241 * \param compositor The compositor to init for.
242 * \return Zero on success, -1 on failure.
243 */
244WL_EXPORT int
245linux_explicit_synchronization_setup(struct weston_compositor *compositor)
246{
247 /* TODO: Update to minor version 2 when the next version of
248 * wayland-protocols that contains it is released. */
249 if (!wl_global_create(compositor->wl_display,
250 &zwp_linux_explicit_synchronization_v1_interface,
251 1, compositor,
252 bind_linux_explicit_synchronization))
253 return -1;
254
255 return 0;
256}
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300257
258/** Resolve an internal compositor error by disconnecting the client.
259 *
260 * This function is used in cases when explicit synchronization
261 * turns out to be unusable and there is no fallback path.
262 *
263 * It is possible the fault is caused by a compositor bug, the underlying
264 * graphics stack bug or normal behaviour, or perhaps a client mistake.
265 * In any case, the options are to either composite garbage or nothing,
266 * or disconnect the client. This is a helper function for the latter.
267 *
268 * The error is sent as an INVALID_OBJECT error on the client's wl_display.
269 *
270 * \param sync The explicit synchronization related resource that is unusable.
271 * \param msg A custom error message attached to the protocol error.
272 */
273WL_EXPORT void
274linux_explicit_synchronization_send_server_error(struct wl_resource *resource,
275 const char *msg)
276{
277 uint32_t id = wl_resource_get_id(resource);
278 const char *class = wl_resource_get_class(resource);
279 struct wl_client *client = wl_resource_get_client(resource);
280 struct wl_resource *display_resource = wl_client_get_object(client, 1);
281
282 assert(display_resource);
283 wl_resource_post_error(display_resource,
284 WL_DISPLAY_ERROR_INVALID_OBJECT,
285 "linux_explicit_synchronization server error "
286 "with %s@%"PRIu32": %s",
287 class, id, msg);
288}