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