Debian Codegraph @

src/wayland-eglswap.c

raw
1 /*
2 * Copyright (c) 2014-2022, NVIDIA CORPORATION. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 */
22
23 #include "wayland-eglswap.h"
24 #include "wayland-eglstream-client-protocol.h"
25 #include "presentation-time-client-protocol.h"
26 #include "wayland-thread.h"
27 #include "wayland-egldisplay.h"
28 #include "wayland-eglsurface-internal.h"
29 #include "wayland-eglhandle.h"
30 #include "wayland-eglutils.h"
31 #include <assert.h>
32 #include <wayland-egl-backend.h>
33 #include <stdlib.h>
34
35 enum PresentationStatus { KICKED_OFF = 0, LANDED = 1 };
36
37 struct EventItem {
38 uint64_t capturedId;
39 void *presentInfo;
40 WlEglSurface *surface;
41 };
42
43 EGLBoolean wlEglSwapBuffersHook(EGLDisplay eglDisplay, EGLSurface eglSurface)
44 {
45 return wlEglSwapBuffersWithDamageHook(eglDisplay, eglSurface, NULL, 0);
46 }
47
48 EGLBoolean wlEglSwapBuffersWithDamageHook(EGLDisplay eglDisplay, EGLSurface eglSurface, EGLint *rects, EGLint n_rects)
49 {
50 WlEglDisplay *display = wlEglAcquireDisplay(eglDisplay);
51 WlEglPlatformData *data = NULL;
52 WlEglSurface *surface = NULL;
53 EGLStreamKHR eglStream = EGL_NO_STREAM_KHR;
54 EGLBoolean isOffscreen = EGL_FALSE;
55 EGLBoolean res;
56 EGLint err;
57
58 if (!display) {
59 return EGL_FALSE;
60 }
61 pthread_mutex_lock(&display->mutex);
62
63 data = display->data;
64
65 if (display->initCount == 0) {
66 err = EGL_NOT_INITIALIZED;
67 goto fail;
68 }
69
70 if (!wlEglSurfaceRef(display, eglSurface)) {
71 err = EGL_BAD_SURFACE;
72 goto fail;
73 }
74
75 surface = eglSurface;
76
77 if (surface->pendingSwapIntervalUpdate == EGL_TRUE) {
78 /* Send request from client to override swapinterval value based on
79 * server's swapinterval for overlay compositing
80 */
81 assert(surface->ctx.wlStreamResource);
82 wl_eglstream_display_swap_interval(display->wlStreamDpy,
83 surface->ctx.wlStreamResource,
84 surface->swapInterval);
85 /* For receiving any event in case of override */
86 if (wl_display_roundtrip_queue(display->nativeDpy,
87 display->wlEventQueue) < 0) {
88 err = EGL_BAD_ALLOC;
89 goto fail;
90 }
91 surface->pendingSwapIntervalUpdate = EGL_FALSE;
92 }
93
94 pthread_mutex_unlock(&display->mutex);
95
96 // Acquire wlEglSurface lock.
97 pthread_mutex_lock(&surface->mutexLock);
98
99 if (surface->isDestroyed) {
100 err = EGL_BAD_SURFACE;
101 goto fail_locked;
102 }
103
104 isOffscreen = surface->ctx.isOffscreen;
105 if (!isOffscreen) {
106 if (!wlEglIsWaylandWindowValid(surface->wlEglWin)) {
107 err = EGL_BAD_NATIVE_WINDOW;
108 goto fail_locked;
109 }
110
111 if (surface->ctx.useDamageThread) {
112 pthread_mutex_lock(&surface->mutexFrameSync);
113 // Wait for damage thread to submit the
114 // previous frame and generate frame sync
115 while (surface->ctx.framesProduced != surface->ctx.framesProcessed) {
116 pthread_cond_wait(&surface->condFrameSync, &surface->mutexFrameSync);
117 }
118 pthread_mutex_unlock(&surface->mutexFrameSync);
119 }
120
121 wlEglWaitFrameSync(surface);
122 }
123
124 /* Save the internal EGLDisplay, EGLSurface and EGLStream handles, as
125 * they are needed by the eglSwapBuffers() and streamFlush calls below */
126 eglDisplay = display->devDpy->eglDisplay;
127 eglSurface = surface->ctx.eglSurface;
128 eglStream = surface->ctx.eglStream;
129
130 /* eglSwapBuffers() is a blocking call. We must release the lock so other
131 * threads using the external platform are allowed to progress.
132 */
133 if (rects) {
134 res = data->egl.swapBuffersWithDamage(eglDisplay, eglSurface, rects, n_rects);
135 } else {
136 res = data->egl.swapBuffers(eglDisplay, eglSurface);
137 }
138 if (isOffscreen) {
139 goto done;
140 }
141 if (display->devDpy->exts.stream_flush) {
142 data->egl.streamFlush(eglDisplay, eglStream);
143 }
144
145 if (res) {
146 if (surface->ctx.useDamageThread) {
147 surface->ctx.framesProduced++;
148 } else {
149 wlEglCreateFrameSync(surface);
150 res = wlEglSendDamageEvent(surface, surface->wlEventQueue, rects, n_rects);
151 wlEglSurfaceCheckReleasePoints(display, surface);
152 }
153 }
154
155 /* Resize stream if window geometry or available modifiers have changed */
156 if (surface->isResized ||
157 surface->feedback.unprocessedFeedback ||
158 display->defaultFeedback.unprocessedFeedback) {
159 wlEglReallocSurface(display, data, surface);
160 }
161
162 done:
163 // Release wlEglSurface lock.
164 pthread_mutex_unlock(&surface->mutexLock);
165
166 /* reacquire display lock */
167 pthread_mutex_lock(&display->mutex);
168 wlEglSurfaceUnref(surface);
169 pthread_mutex_unlock(&display->mutex);
170 wlEglReleaseDisplay(display);
171
172 return res;
173
174 fail_locked:
175 pthread_mutex_unlock(&surface->mutexLock);
176 /* reacquire display lock */
177 pthread_mutex_lock(&display->mutex);
178 fail:
179 if (surface != NULL) {
180 wlEglSurfaceUnref(surface);
181 }
182 pthread_mutex_unlock(&display->mutex);
183 wlEglReleaseDisplay(display);
184
185 wlEglSetError(data, err);
186 return EGL_FALSE;
187 }
188
189 EGLBoolean wlEglSwapIntervalHook(EGLDisplay eglDisplay, EGLint interval)
190 {
191 WlEglDisplay *display = wlEglAcquireDisplay(eglDisplay);
192 WlEglPlatformData *data = NULL;
193 WlEglSurface *surface = NULL;
194 EGLBoolean ret = EGL_TRUE;
195 EGLint state;
196
197 if (!display) {
198 return EGL_FALSE;
199 }
200 pthread_mutex_lock(&display->mutex);
201
202 data = display->data;
203
204 if (display->initCount == 0) {
205 wlEglSetError(data, EGL_NOT_INITIALIZED);
206 ret = EGL_FALSE;
207 goto done;
208 }
209
210 /* Save the internal EGLDisplay handle, as it's needed by the actual
211 * eglSwapInterval() call */
212 eglDisplay = display->devDpy->eglDisplay;
213
214 pthread_mutex_unlock(&display->mutex);
215
216 if (!(data->egl.swapInterval(eglDisplay, interval))) {
217 wlEglReleaseDisplay(display);
218 return EGL_FALSE;
219 }
220
221 surface = (WlEglSurface *)data->egl.getCurrentSurface(EGL_DRAW);
222
223 pthread_mutex_lock(&display->mutex);
224
225 /* Check this is a valid wayland EGL surface */
226 if (display->initCount == 0 ||
227 !wlEglIsWlEglSurfaceForDisplay(display, surface) ||
228 (surface->swapInterval == interval) ||
229 (surface->ctx.eglStream == EGL_NO_STREAM_KHR)) {
230 goto done;
231 }
232
233 /* Cache interval value so we can reset it upon surface reattach */
234 surface->swapInterval = interval;
235
236 if (surface->ctx.wlStreamResource &&
237 data->egl.queryStream(display->devDpy->eglDisplay,
238 surface->ctx.eglStream,
239 EGL_STREAM_STATE_KHR, &state) &&
240 state != EGL_STREAM_STATE_DISCONNECTED_KHR) {
241 /* Set client's pendingSwapIntervalUpdate for updating client's
242 * swapinterval if the compositor supports wl_eglstream_display
243 * and the surface has a valid server-side stream
244 */
245 surface->pendingSwapIntervalUpdate = EGL_TRUE;
246 }
247
248 done:
249 pthread_mutex_unlock(&display->mutex);
250 wlEglReleaseDisplay(display);
251
252 return ret;
253 }
254
255 WL_EXPORT
256 EGLBoolean wlEglPrePresentExport(WlEglSurface *surface) {
257 WlEglDisplay *display = wlEglAcquireDisplay((WlEglDisplay *)surface->wlEglDpy);
258 if (!display) {
259 return EGL_FALSE;
260 }
261
262 pthread_mutex_lock(&display->mutex);
263
264 if (surface->pendingSwapIntervalUpdate == EGL_TRUE) {
265 /* Send request from client to override swapinterval value based on
266 * server's swapinterval for overlay compositing
267 */
268 wl_eglstream_display_swap_interval(display->wlStreamDpy,
269 surface->ctx.wlStreamResource,
270 surface->swapInterval);
271 /* For receiving any event in case of override */
272 if (wl_display_roundtrip_queue(display->nativeDpy,
273 display->wlEventQueue) < 0) {
274 pthread_mutex_unlock(&display->mutex);
275 wlEglReleaseDisplay(display);
276 return EGL_FALSE;
277 }
278 surface->pendingSwapIntervalUpdate = EGL_FALSE;
279 }
280
281 pthread_mutex_unlock(&display->mutex);
282
283 // Acquire wlEglSurface lock.
284 pthread_mutex_lock(&surface->mutexLock);
285
286 if (surface->ctx.useDamageThread) {
287 pthread_mutex_lock(&surface->mutexFrameSync);
288 // Wait for damage thread to submit the
289 // previous frame and generate frame sync
290 while (surface->ctx.framesProduced != surface->ctx.framesProcessed) {
291 pthread_cond_wait(&surface->condFrameSync, &surface->mutexFrameSync);
292 }
293 pthread_mutex_unlock(&surface->mutexFrameSync);
294 }
295
296 wlEglWaitFrameSync(surface);
297
298 // Release wlEglSurface lock.
299 pthread_mutex_unlock(&surface->mutexLock);
300 wlEglReleaseDisplay(display);
301
302 return EGL_TRUE;
303 }
304
305 WL_EXPORT
306 EGLBoolean wlEglPostPresentExport(WlEglSurface *surface) {
307 return wlEglPostPresentExport2(surface, 0, NULL);
308 }
309
310 static void present_feedback_sync_output(void *data,
311 struct wp_presentation_feedback *feedback,
312 struct wl_output *output)
313 {
314 // This function is intentionally left blank. sync_output events
315 // precede the presented events when wl_output is bound.
316 //
317 // The information provided by this function is not needed at the
318 // moment.
319
320 (void) data;
321 (void) feedback;
322 (void) output;
323 }
324
325 static void present_feedback_discarded(void *data,
326 struct wp_presentation_feedback *feedback)
327 {
328 struct EventItem *eventItem = data;
329 WlEglSurface *surface = eventItem->surface;
330
331 // If the following condition is not true, it means that this presentInfo
332 // was overwritten. The presentInfo that it was pointing to when this
333 // request was created is not the same as the one it is pointing to right
334 // now. Status of the previous presentInfo is not relevant.
335 if (surface->present_update_callback(eventItem->presentInfo,
336 eventItem->capturedId,
337 LANDED)) {
338 surface->landedPresentFeedbackCount++;
339 }
340
341 surface->inFlightPresentFeedbackCount--;
342
343 free(eventItem);
344 (void) feedback;
345 }
346
347 static void present_feedback_presented(void *data,
348 struct wp_presentation_feedback *feedback,
349 uint32_t tv_sec_hi,
350 uint32_t tv_sec_lo,
351 uint32_t tv_nsec,
352 uint32_t refresh,
353 uint32_t seq_hi,
354 uint32_t seq_lo,
355 uint32_t flags)
356 {
357 // For now, whatever the outcome of the presentation is, the same
358 // operations are performed as a result
359 present_feedback_discarded(data, feedback);
360
361 (void) tv_sec_hi;
362 (void) tv_sec_lo;
363 (void) tv_nsec;
364 (void) refresh;
365 (void) seq_hi;
366 (void) seq_lo;
367 (void) tv_sec_hi;
368 (void) flags;
369 }
370
371 static const struct wp_presentation_feedback_listener present_feedback_listener = {
372 present_feedback_sync_output,
373 present_feedback_presented,
374 present_feedback_discarded
375 };
376
377 WL_EXPORT
378 EGLBoolean wlEglPostPresentExport2(WlEglSurface *surface,
379 uint64_t presentId,
380 void *presentInfo) {
381 WlEglDisplay *display = wlEglAcquireDisplay((WlEglDisplay *)surface->wlEglDpy);
382 WlEglPlatformData *data = NULL;
383 EGLBoolean res = EGL_TRUE;
384
385 if (!display) {
386 return EGL_FALSE;
387 }
388
389 data = display->data;
390
391 // Acquire wlEglSurface lock.
392 pthread_mutex_lock(&surface->mutexLock);
393
394 if (display->devDpy->exts.stream_flush) {
395 data->egl.streamFlush((EGLDisplay) display, surface->ctx.eglStream);
396 }
397
398 if (presentInfo)
399 {
400 assert(surface->present_update_callback != NULL);
401
402 if (display->wpPresentation)
403 {
404 struct wp_presentation_feedback *presentationFeedback = NULL;
405 struct wp_presentation *wrapper = wl_proxy_create_wrapper(display->wpPresentation);
406
407 struct EventItem *eventItem = malloc(sizeof(struct EventItem));
408 eventItem->capturedId = presentId;
409 eventItem->presentInfo = presentInfo;
410 eventItem->surface = surface;
411
412 wl_proxy_set_queue((struct wl_proxy *)wrapper, surface->presentFeedbackQueue);
413 presentationFeedback = wp_presentation_feedback(wrapper, surface->wlSurface);
414 wl_proxy_wrapper_destroy(wrapper); /* Done with wrapper */
415 if (wp_presentation_feedback_add_listener(presentationFeedback,
416 &present_feedback_listener,
417 eventItem) == -1) {
418 pthread_mutex_unlock(&surface->mutexLock);
419 wlEglReleaseDisplay(display);
420 return EGL_FALSE;
421 }
422
423 surface->present_update_callback(presentInfo, presentId, KICKED_OFF);
424 surface->inFlightPresentFeedbackCount++;
425 } else {
426 // If the presentation feedback protocol is not supported by the compositor,
427 // there is not much we can do to get this information.
428 surface->present_update_callback(presentInfo, presentId, LANDED);
429 surface->landedPresentFeedbackCount++;
430 }
431 }
432
433 if (surface->ctx.useDamageThread) {
434 surface->ctx.framesProduced++;
435 } else {
436 wlEglCreateFrameSync(surface);
437 res = wlEglSendDamageEvent(surface, surface->wlEventQueue, NULL, 0);
438 }
439
440 // Release wlEglSurface lock.
441 pthread_mutex_unlock(&surface->mutexLock);
442 wlEglReleaseDisplay(display);
443
444 return res;
445 }
446
447 EGLint wlEglStreamSwapIntervalCallback(WlEglPlatformData *data,
448 EGLStreamKHR stream,
449 EGLint *interval)
450 {
451 EGLint res = EGL_SUCCESS;
452
453 if (data->callbacks.streamSwapInterval) {
454 res = data->callbacks.streamSwapInterval(stream, interval);
455 }
456
457 return res;
458 }