wpe: Base wpe audio implementation on a web extension
[platform/upstream/gstreamer.git] / ext / wpe / WPEThreadedView.cpp
1 /* Copyright (C) <2018, 2019, 2020> Philippe Normand <philn@igalia.com>
2  * Copyright (C) <2018> Žan Doberšek <zdobersek@igalia.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include "WPEThreadedView.h"
25 #include "gstwpe.h"
26 #include "gstwpesrcbin.h"
27
28 #include <gst/gl/gl.h>
29 #include <gst/gl/egl/gsteglimage.h>
30 #include <gst/gl/egl/gstgldisplay_egl.h>
31 #include <wayland-server.h>
32
33 #include <cstdio>
34 #include <mutex>
35
36 #if ENABLE_SHM_BUFFER_SUPPORT
37 #include <wpe/unstable/fdo-shm.h>
38 #endif
39
40 GST_DEBUG_CATEGORY_EXTERN (wpe_view_debug);
41 #define GST_CAT_DEFAULT wpe_view_debug
42
43 #if defined(WPE_FDO_CHECK_VERSION) && WPE_FDO_CHECK_VERSION(1, 3, 0)
44 #define USE_DEPRECATED_FDO_EGL_IMAGE 0
45 #define WPE_GLIB_SOURCE_PRIORITY G_PRIORITY_DEFAULT
46 #else
47 #define USE_DEPRECATED_FDO_EGL_IMAGE 1
48 #define WPE_GLIB_SOURCE_PRIORITY -70
49 #endif
50
51 class GMutexHolder {
52 public:
53     GMutexHolder(GMutex& mutex)
54         : m(mutex)
55     {
56         g_mutex_lock(&m);
57     }
58     ~GMutexHolder()
59     {
60         g_mutex_unlock(&m);
61     }
62
63 private:
64     GMutex& m;
65 };
66
67 static WPEContextThread *s_view = NULL;
68
69 WPEContextThread& WPEContextThread::singleton()
70 {
71     static gsize initialized = 0;
72
73     if (g_once_init_enter (&initialized)) {
74         s_view = new WPEContextThread;
75
76         g_once_init_leave (&initialized, 1);
77     }
78
79     return *s_view;
80 }
81
82 WPEContextThread::WPEContextThread()
83 {
84     g_mutex_init(&threading.mutex);
85     g_cond_init(&threading.cond);
86
87     {
88         GMutexHolder lock(threading.mutex);
89         threading.thread = g_thread_new("WPEContextThread", s_viewThread, this);
90         g_cond_wait(&threading.cond, &threading.mutex);
91         GST_DEBUG("thread spawned");
92     }
93 }
94
95 WPEContextThread::~WPEContextThread()
96 {
97     if (threading.thread) {
98         g_thread_unref(threading.thread);
99         threading.thread = nullptr;
100     }
101
102     g_mutex_clear(&threading.mutex);
103     g_cond_clear(&threading.cond);
104 }
105
106 template<typename Function>
107 void WPEContextThread::dispatch(Function func)
108 {
109     struct Payload {
110         Function& func;
111     };
112     struct Payload payload { func };
113
114     GSource* source = g_idle_source_new();
115     g_source_set_callback(source, [](gpointer data) -> gboolean {
116         auto& view = WPEContextThread::singleton();
117         GMutexHolder lock(view.threading.mutex);
118
119         auto* payload = static_cast<struct Payload*>(data);
120         payload->func();
121
122         g_cond_signal(&view.threading.cond);
123         return G_SOURCE_REMOVE;
124     }, &payload, nullptr);
125     g_source_set_priority(source, WPE_GLIB_SOURCE_PRIORITY);
126
127     {
128         GMutexHolder lock(threading.mutex);
129         g_source_attach(source, glib.context);
130         g_cond_wait(&threading.cond, &threading.mutex);
131     }
132
133     g_source_unref(source);
134 }
135
136 gpointer WPEContextThread::s_viewThread(gpointer data)
137 {
138     auto& view = *static_cast<WPEContextThread*>(data);
139
140     view.glib.context = g_main_context_new();
141     view.glib.loop = g_main_loop_new(view.glib.context, FALSE);
142
143     g_main_context_push_thread_default(view.glib.context);
144
145     {
146         GSource* source = g_idle_source_new();
147         g_source_set_callback(source,
148             [](gpointer data) -> gboolean {
149                 auto& view = *static_cast<WPEContextThread*>(data);
150                 GMutexHolder lock(view.threading.mutex);
151                 g_cond_signal(&view.threading.cond);
152                 return G_SOURCE_REMOVE;
153             },
154             &view, nullptr);
155         g_source_attach(source, view.glib.context);
156         g_source_unref(source);
157     }
158
159     g_main_loop_run(view.glib.loop);
160
161     g_main_loop_unref(view.glib.loop);
162     view.glib.loop = nullptr;
163
164     g_main_context_pop_thread_default(view.glib.context);
165     g_main_context_unref(view.glib.context);
166     view.glib.context = nullptr;
167     return nullptr;
168 }
169
170 #ifdef G_OS_UNIX
171 static void
172 initialize_web_extensions (WebKitWebContext *context)
173 {
174     webkit_web_context_set_web_extensions_directory (context, gst_wpe_get_extension_path ());
175 }
176
177 static gboolean
178 webkit_extension_msg_received (WebKitWebContext  *context,
179                WebKitUserMessage *message,
180                GstWpeSrc           *src)
181 {
182     const gchar *name = webkit_user_message_get_name (message);
183     GVariant *params = webkit_user_message_get_parameters (message);
184     gboolean res = TRUE;
185
186     if (!g_strcmp0(name, "gstwpe.new_stream")) {
187         guint32 id = g_variant_get_uint32 (g_variant_get_child_value (params, 0));
188         const gchar *capsstr = g_variant_get_string (g_variant_get_child_value (params, 1), NULL);
189         GstCaps *caps = gst_caps_from_string (capsstr);
190         const gchar *stream_id = g_variant_get_string (g_variant_get_child_value (params, 2), NULL);
191         gst_wpe_src_new_audio_stream(src, id, caps, stream_id);
192         gst_caps_unref (caps);
193     } else if (!g_strcmp0(name, "gstwpe.set_shm")) {
194         auto fdlist = webkit_user_message_get_fd_list (message);
195         gint id = g_variant_get_uint32 (g_variant_get_child_value (params, 0));
196         gst_wpe_src_set_audio_shm (src, fdlist, id);
197     } else if (!g_strcmp0(name, "gstwpe.new_buffer")) {
198         guint32 id = g_variant_get_uint32 (g_variant_get_child_value (params, 0));
199         guint64 size = g_variant_get_uint64 (g_variant_get_child_value (params, 1));
200         gst_wpe_src_push_audio_buffer (src, id, size);
201
202         webkit_user_message_send_reply(message, webkit_user_message_new ("gstwpe.buffer_processed", NULL));
203     } else if (!g_strcmp0(name, "gstwpe.pause")) {
204         guint32 id = g_variant_get_uint32 (params);
205
206         gst_wpe_src_pause_audio_stream (src, id);
207     } else if (!g_strcmp0(name, "gstwpe.stop")) {
208         guint32 id = g_variant_get_uint32 (params);
209
210         gst_wpe_src_stop_audio_stream (src, id);
211     } else {
212         res = FALSE;
213         g_error("Unknown event: %s", name);
214     }
215
216     return res;
217 }
218 #endif
219
220 WPEView* WPEContextThread::createWPEView(GstWpeVideoSrc* src, GstGLContext* context, GstGLDisplay* display, int width, int height)
221 {
222     GST_DEBUG("context %p display %p, size (%d,%d)", context, display, width, height);
223
224     static std::once_flag s_loaderFlag;
225     std::call_once(s_loaderFlag,
226         [] {
227 #if defined(WPE_BACKEND_CHECK_VERSION) && WPE_BACKEND_CHECK_VERSION(1, 2, 0)
228             wpe_loader_init("libWPEBackend-fdo-1.0.so");
229 #endif
230         });
231
232     WPEView* view = nullptr;
233     dispatch([&]() mutable {
234         auto* manager = webkit_website_data_manager_new_ephemeral();
235         auto web_context = webkit_web_context_new_with_website_data_manager(manager);
236         g_object_unref(manager);
237
238         view = new WPEView(web_context, src, context, display, width, height);
239     });
240
241     if (view && view->hasUri()) {
242         GST_DEBUG("waiting load to finish");
243         view->waitLoadCompletion();
244         GST_DEBUG("done");
245     }
246
247     return view;
248 }
249
250 static gboolean s_loadFailed(WebKitWebView*, WebKitLoadEvent, gchar* failing_uri, GError* error, gpointer data)
251 {
252     GstWpeVideoSrc* src = GST_WPE_VIDEO_SRC(data);
253
254     if (g_error_matches(error, WEBKIT_NETWORK_ERROR, WEBKIT_NETWORK_ERROR_CANCELLED)) {
255         GST_INFO_OBJECT (src, "Loading cancelled.");
256
257         return FALSE;
258     }
259
260     GST_ELEMENT_ERROR (GST_ELEMENT_CAST(src), RESOURCE, FAILED, (NULL), ("Failed to load %s (%s)", failing_uri, error->message));
261     return FALSE;
262 }
263
264 static gboolean s_loadFailedWithTLSErrors(WebKitWebView*,  gchar* failing_uri, GTlsCertificate*, GTlsCertificateFlags, gpointer data)
265 {
266     // Defer to load-failed.
267     return FALSE;
268 }
269
270 static void s_loadProgressChaned(GObject* object, GParamSpec*, gpointer data)
271 {
272     GstElement* src = GST_ELEMENT_CAST (data);
273     // The src element is locked already so we can't call
274     // gst_element_post_message(). Instead retrieve the bus manually and use it
275     // directly.
276     GstBus* bus = GST_ELEMENT_BUS (src);
277     double estimatedProgress;
278     g_object_get(object, "estimated-load-progress", &estimatedProgress, nullptr);
279     gst_object_ref (bus);
280     gst_bus_post (bus, gst_message_new_element(GST_OBJECT_CAST(src), gst_structure_new("wpe-stats", "estimated-load-progress", G_TYPE_DOUBLE, estimatedProgress * 100, nullptr)));
281     gst_object_unref (bus);
282 }
283
284 WPEView::WPEView(WebKitWebContext* web_context, GstWpeVideoSrc* src, GstGLContext* context, GstGLDisplay* display, int width, int height)
285 {
286 #ifdef G_OS_UNIX
287 {
288         GstObject *parent = gst_object_get_parent (GST_OBJECT (src));
289
290         if (parent && GST_IS_WPE_SRC (parent)) {
291             audio.init_ext_sigid = g_signal_connect (web_context,
292                               "initialize-web-extensions",
293                               G_CALLBACK (initialize_web_extensions),
294                               NULL);
295             audio.extension_msg_sigid = g_signal_connect (web_context,
296                                 "user-message-received",
297                                 G_CALLBACK (webkit_extension_msg_received),
298                                 parent);
299             GST_INFO_OBJECT (parent, "Enabled audio");
300         }
301
302         gst_clear_object (&parent);
303 }
304 #endif // G_OS_UNIX
305
306     g_mutex_init(&threading.ready_mutex);
307     g_cond_init(&threading.ready_cond);
308     threading.ready = FALSE;
309
310     g_mutex_init(&images_mutex);
311     if (context)
312         gst.context = GST_GL_CONTEXT(gst_object_ref(context));
313     if (display) {
314         gst.display = GST_GL_DISPLAY(gst_object_ref(display));
315     }
316
317     wpe.width = width;
318     wpe.height = height;
319
320     if (context && display) {
321       if (gst_gl_context_get_gl_platform(context) == GST_GL_PLATFORM_EGL) {
322         gst.display_egl = gst_gl_display_egl_from_gl_display (gst.display);
323       } else {
324         GST_DEBUG ("Available GStreamer GL Context is not EGL - not creating an EGL display from it");
325       }
326     }
327
328     if (gst.display_egl) {
329         EGLDisplay eglDisplay = (EGLDisplay)gst_gl_display_get_handle (GST_GL_DISPLAY(gst.display_egl));
330         GST_DEBUG("eglDisplay %p", eglDisplay);
331
332         m_isValid = wpe_fdo_initialize_for_egl_display(eglDisplay);
333         GST_DEBUG("FDO EGL display initialisation result: %d", m_isValid);
334     } else {
335 #if ENABLE_SHM_BUFFER_SUPPORT
336         m_isValid = wpe_fdo_initialize_shm();
337         GST_DEBUG("FDO SHM initialisation result: %d", m_isValid);
338 #else
339         GST_WARNING("FDO SHM support is available only in WPEBackend-FDO 1.7.0");
340 #endif
341     }
342     if (!m_isValid)
343         return;
344
345     if (gst.display_egl) {
346         wpe.exportable = wpe_view_backend_exportable_fdo_egl_create(&s_exportableEGLClient, this, wpe.width, wpe.height);
347     } else {
348 #if ENABLE_SHM_BUFFER_SUPPORT
349         wpe.exportable = wpe_view_backend_exportable_fdo_create(&s_exportableClient, this, wpe.width, wpe.height);
350 #endif
351     }
352
353     auto* wpeViewBackend = wpe_view_backend_exportable_fdo_get_view_backend(wpe.exportable);
354     auto* viewBackend = webkit_web_view_backend_new(wpeViewBackend, (GDestroyNotify) wpe_view_backend_exportable_fdo_destroy, wpe.exportable);
355 #if defined(WPE_BACKEND_CHECK_VERSION) && WPE_BACKEND_CHECK_VERSION(1, 1, 0)
356     wpe_view_backend_add_activity_state(wpeViewBackend, wpe_view_activity_state_visible | wpe_view_activity_state_focused | wpe_view_activity_state_in_window);
357 #endif
358
359     webkit.view = WEBKIT_WEB_VIEW(g_object_new(WEBKIT_TYPE_WEB_VIEW,
360         "web-context", web_context,
361         "backend", viewBackend,
362         nullptr));
363
364     g_signal_connect(webkit.view, "load-failed", G_CALLBACK(s_loadFailed), src);
365     g_signal_connect(webkit.view, "load-failed-with-tls-errors", G_CALLBACK(s_loadFailedWithTLSErrors), src);
366     g_signal_connect(webkit.view, "notify::estimated-load-progress", G_CALLBACK(s_loadProgressChaned), src);
367
368     auto* settings = webkit_web_view_get_settings(webkit.view);
369     webkit_settings_set_enable_webaudio(settings, TRUE);
370
371     gst_wpe_video_src_configure_web_view(src, webkit.view);
372
373     gchar* location;
374     gboolean drawBackground = TRUE;
375     g_object_get(src, "location", &location, "draw-background", &drawBackground, nullptr);
376     setDrawBackground(drawBackground);
377     if (location) {
378         loadUriUnlocked(location);
379         g_free(location);
380     }
381 }
382
383 WPEView::~WPEView()
384 {
385     GstEGLImage *egl_pending = NULL;
386     GstEGLImage *egl_committed = NULL;
387     GstBuffer *shm_pending = NULL;
388     GstBuffer *shm_committed = NULL;
389     GST_TRACE ("%p destroying", this);
390
391     g_mutex_clear(&threading.ready_mutex);
392     g_cond_clear(&threading.ready_cond);
393
394     {
395         GMutexHolder lock(images_mutex);
396
397         if (egl.pending) {
398             egl_pending = egl.pending;
399             egl.pending = nullptr;
400         }
401         if (egl.committed) {
402             egl_committed = egl.committed;
403             egl.committed = nullptr;
404         }
405         if (shm.pending) {
406             GST_TRACE ("%p freeing shm pending %" GST_PTR_FORMAT, this, shm.pending);
407             shm_pending = shm.pending;
408             shm.pending = nullptr;
409         }
410         if (shm.committed) {
411             GST_TRACE ("%p freeing shm commited %" GST_PTR_FORMAT, this, shm.committed);
412             shm_committed = shm.committed;
413             shm.committed = nullptr;
414         }
415     }
416
417     if (egl_pending)
418         gst_egl_image_unref (egl_pending);
419     if (egl_committed)
420         gst_egl_image_unref (egl_committed);
421     if (shm_pending)
422         gst_buffer_unref (shm_pending);
423     if (shm_committed)
424         gst_buffer_unref (shm_committed);
425
426     if (audio.init_ext_sigid) {
427         WebKitWebContext* web_context = webkit_web_view_get_context (webkit.view);
428
429         g_signal_handler_disconnect(web_context, audio.init_ext_sigid);
430         g_signal_handler_disconnect(web_context, audio.extension_msg_sigid);
431         audio.init_ext_sigid = 0;
432         audio.extension_msg_sigid = 0;
433     }
434
435     WPEContextThread::singleton().dispatch([&]() {
436         if (webkit.view) {
437             g_object_unref(webkit.view);
438             webkit.view = nullptr;
439         }
440     });
441
442     if (gst.display_egl) {
443         gst_object_unref(gst.display_egl);
444         gst.display_egl = nullptr;
445     }
446
447     if (gst.display) {
448         gst_object_unref(gst.display);
449         gst.display = nullptr;
450     }
451
452     if (gst.context) {
453         gst_object_unref(gst.context);
454         gst.context = nullptr;
455     }
456     if (webkit.uri) {
457         g_free(webkit.uri);
458         webkit.uri = nullptr;
459     }
460
461     g_mutex_clear(&images_mutex);
462     GST_TRACE ("%p destroyed", this);
463 }
464
465 void WPEView::notifyLoadFinished()
466 {
467     GMutexHolder lock(threading.ready_mutex);
468     if (!threading.ready) {
469         threading.ready = TRUE;
470         g_cond_signal(&threading.ready_cond);
471     }
472 }
473
474 void WPEView::waitLoadCompletion()
475 {
476     GMutexHolder lock(threading.ready_mutex);
477     while (!threading.ready)
478         g_cond_wait(&threading.ready_cond, &threading.ready_mutex);
479 }
480
481 GstEGLImage* WPEView::image()
482 {
483     GstEGLImage* ret = nullptr;
484     bool dispatchFrameComplete = false;
485     GstEGLImage *prev_image = NULL;
486
487     {
488         GMutexHolder lock(images_mutex);
489
490         GST_TRACE("pending %" GST_PTR_FORMAT " (%d) committed %" GST_PTR_FORMAT " (%d)", egl.pending,
491                   GST_IS_EGL_IMAGE(egl.pending) ? GST_MINI_OBJECT_REFCOUNT_VALUE(GST_MINI_OBJECT_CAST(egl.pending)) : 0,
492                   egl.committed,
493                   GST_IS_EGL_IMAGE(egl.committed) ? GST_MINI_OBJECT_REFCOUNT_VALUE(GST_MINI_OBJECT_CAST(egl.committed)) : 0);
494
495         if (egl.pending) {
496             prev_image = egl.committed;
497             egl.committed = egl.pending;
498             egl.pending = nullptr;
499
500             dispatchFrameComplete = true;
501         }
502
503         if (egl.committed)
504             ret = egl.committed;
505     }
506
507     if (prev_image)
508         gst_egl_image_unref(prev_image);
509
510     if (dispatchFrameComplete)
511         frameComplete();
512
513     return ret;
514 }
515
516 GstBuffer* WPEView::buffer()
517 {
518     GstBuffer* ret = nullptr;
519     bool dispatchFrameComplete = false;
520     GstBuffer *prev_image = NULL;
521
522     {
523         GMutexHolder lock(images_mutex);
524
525         GST_TRACE("pending %" GST_PTR_FORMAT " (%d) committed %" GST_PTR_FORMAT " (%d)", shm.pending,
526                   GST_IS_BUFFER(shm.pending) ? GST_MINI_OBJECT_REFCOUNT_VALUE(GST_MINI_OBJECT_CAST(shm.pending)) : 0,
527                   shm.committed,
528                   GST_IS_BUFFER(shm.committed) ? GST_MINI_OBJECT_REFCOUNT_VALUE(GST_MINI_OBJECT_CAST(shm.committed)) : 0);
529
530         if (shm.pending) {
531             prev_image = shm.committed;
532             shm.committed = shm.pending;
533             shm.pending = nullptr;
534
535             dispatchFrameComplete = true;
536         }
537
538         if (shm.committed)
539             ret = shm.committed;
540     }
541
542     if (prev_image)
543         gst_buffer_unref(prev_image);
544
545     if (dispatchFrameComplete)
546         frameComplete();
547
548     return ret;
549 }
550
551 void WPEView::resize(int width, int height)
552 {
553     GST_DEBUG("resize to %dx%d", width, height);
554     wpe.width = width;
555     wpe.height = height;
556
557     s_view->dispatch([&]() {
558         if (wpe.exportable && wpe_view_backend_exportable_fdo_get_view_backend(wpe.exportable))
559           wpe_view_backend_dispatch_set_size(wpe_view_backend_exportable_fdo_get_view_backend(wpe.exportable), wpe.width, wpe.height);
560     });
561 }
562
563 void WPEView::frameComplete()
564 {
565     GST_TRACE("frame complete");
566     s_view->dispatch([&]() {
567         GST_TRACE("dispatching");
568         wpe_view_backend_exportable_fdo_dispatch_frame_complete(wpe.exportable);
569     });
570 }
571
572 void WPEView::loadUriUnlocked(const gchar* uri)
573 {
574     if (webkit.uri)
575         g_free(webkit.uri);
576
577     GST_DEBUG("loading %s", uri);
578     webkit.uri = g_strdup(uri);
579     webkit_web_view_load_uri(webkit.view, webkit.uri);
580 }
581
582 void WPEView::loadUri(const gchar* uri)
583 {
584     s_view->dispatch([&]() {
585         loadUriUnlocked(uri);
586     });
587 }
588
589 void WPEView::loadData(GBytes* bytes)
590 {
591     s_view->dispatch([this, bytes = g_bytes_ref(bytes)]() {
592         webkit_web_view_load_bytes(webkit.view, bytes, nullptr, nullptr, nullptr);
593         g_bytes_unref(bytes);
594     });
595 }
596
597 void WPEView::setDrawBackground(gboolean drawsBackground)
598 {
599     GST_DEBUG("%s background rendering", drawsBackground ? "Enabling" : "Disabling");
600     WebKitColor color;
601     webkit_color_parse(&color, drawsBackground ? "white" : "transparent");
602     webkit_web_view_set_background_color(webkit.view, &color);
603 }
604
605 void WPEView::releaseImage(gpointer imagePointer)
606 {
607     s_view->dispatch([&]() {
608         GST_TRACE("Dispatch release exported image %p", imagePointer);
609 #if USE_DEPRECATED_FDO_EGL_IMAGE
610         wpe_view_backend_exportable_fdo_egl_dispatch_release_image(wpe.exportable,
611                                                                    static_cast<EGLImageKHR>(imagePointer));
612 #else
613         wpe_view_backend_exportable_fdo_egl_dispatch_release_exported_image(wpe.exportable,
614                                                                             static_cast<struct wpe_fdo_egl_exported_image*>(imagePointer));
615 #endif
616     });
617 }
618
619 struct ImageContext {
620     WPEView* view;
621     gpointer image;
622 };
623
624 void WPEView::handleExportedImage(gpointer image)
625 {
626     ImageContext* imageContext = g_slice_new(ImageContext);
627     imageContext->view = this;
628     imageContext->image = static_cast<gpointer>(image);
629     EGLImageKHR eglImage;
630 #if USE_DEPRECATED_FDO_EGL_IMAGE
631     eglImage = static_cast<EGLImageKHR>(image);
632 #else
633     eglImage = wpe_fdo_egl_exported_image_get_egl_image(static_cast<struct wpe_fdo_egl_exported_image*>(image));
634 #endif
635
636     auto* gstImage = gst_egl_image_new_wrapped(gst.context, eglImage, GST_GL_RGBA, imageContext, s_releaseImage);
637     {
638       GMutexHolder lock(images_mutex);
639
640       GST_TRACE("EGLImage %p wrapped in GstEGLImage %" GST_PTR_FORMAT, eglImage, gstImage);
641       gst_clear_mini_object ((GstMiniObject **) &egl.pending);
642       egl.pending = gstImage;
643
644       notifyLoadFinished();
645     }
646 }
647
648 #if ENABLE_SHM_BUFFER_SUPPORT
649 struct SHMBufferContext {
650     WPEView* view;
651     struct wpe_fdo_shm_exported_buffer* buffer;
652 };
653
654 void WPEView::releaseSHMBuffer(gpointer data)
655 {
656     SHMBufferContext* context = static_cast<SHMBufferContext*>(data);
657     s_view->dispatch([&]() {
658         auto* buffer = static_cast<struct wpe_fdo_shm_exported_buffer*>(context->buffer);
659         GST_TRACE("Dispatch release exported buffer %p", buffer);
660         wpe_view_backend_exportable_fdo_dispatch_release_shm_exported_buffer(wpe.exportable, buffer);
661     });
662 }
663
664 void WPEView::s_releaseSHMBuffer(gpointer data)
665 {
666     SHMBufferContext* context = static_cast<SHMBufferContext*>(data);
667     context->view->releaseSHMBuffer(data);
668     g_slice_free(SHMBufferContext, context);
669 }
670
671 void WPEView::handleExportedBuffer(struct wpe_fdo_shm_exported_buffer* buffer)
672 {
673     struct wl_shm_buffer* shmBuffer = wpe_fdo_shm_exported_buffer_get_shm_buffer(buffer);
674     auto format = wl_shm_buffer_get_format(shmBuffer);
675     if (format != WL_SHM_FORMAT_ARGB8888 && format != WL_SHM_FORMAT_XRGB8888) {
676         GST_ERROR("Unsupported pixel format: %d", format);
677         return;
678     }
679
680     int32_t width = wl_shm_buffer_get_width(shmBuffer);
681     int32_t height = wl_shm_buffer_get_height(shmBuffer);
682     gint stride = wl_shm_buffer_get_stride(shmBuffer);
683     gsize size = width * height * 4;
684     auto* data = static_cast<uint8_t*>(wl_shm_buffer_get_data(shmBuffer));
685
686     SHMBufferContext* bufferContext = g_slice_new(SHMBufferContext);
687     bufferContext->view = this;
688     bufferContext->buffer = buffer;
689
690     auto* gstBuffer = gst_buffer_new_wrapped_full(GST_MEMORY_FLAG_READONLY, data, size, 0, size, bufferContext, s_releaseSHMBuffer);
691     gsize offsets[1];
692     gint strides[1];
693     offsets[0] = 0;
694     strides[0] = stride;
695     gst_buffer_add_video_meta_full(gstBuffer, GST_VIDEO_FRAME_FLAG_NONE, GST_VIDEO_FORMAT_BGRA, width, height, 1, offsets, strides);
696
697     {
698         GMutexHolder lock(images_mutex);
699         GST_TRACE("SHM buffer %p wrapped in buffer %" GST_PTR_FORMAT, buffer, gstBuffer);
700         gst_clear_buffer (&shm.pending);
701         shm.pending = gstBuffer;
702         notifyLoadFinished();
703     }
704 }
705 #endif
706
707 struct wpe_view_backend_exportable_fdo_egl_client WPEView::s_exportableEGLClient = {
708 #if USE_DEPRECATED_FDO_EGL_IMAGE
709     // export_egl_image
710     [](void* data, EGLImageKHR image) {
711         auto& view = *static_cast<WPEView*>(data);
712         view.handleExportedImage(static_cast<gpointer>(image));
713     },
714     nullptr, nullptr,
715 #else
716     // export_egl_image
717     nullptr,
718     [](void* data, struct wpe_fdo_egl_exported_image* image) {
719         auto& view = *static_cast<WPEView*>(data);
720         view.handleExportedImage(static_cast<gpointer>(image));
721     },
722     nullptr,
723 #endif // USE_DEPRECATED_FDO_EGL_IMAGE
724     // padding
725     nullptr, nullptr
726 };
727
728 #if ENABLE_SHM_BUFFER_SUPPORT
729 struct wpe_view_backend_exportable_fdo_client WPEView::s_exportableClient = {
730     nullptr,
731     nullptr,
732     // export_shm_buffer
733     [](void* data, struct wpe_fdo_shm_exported_buffer* buffer) {
734         auto& view = *static_cast<WPEView*>(data);
735         view.handleExportedBuffer(buffer);
736     },
737     nullptr,
738     nullptr,
739 };
740 #endif
741
742 void WPEView::s_releaseImage(GstEGLImage* image, gpointer data)
743 {
744     ImageContext* context = static_cast<ImageContext*>(data);
745     context->view->releaseImage(context->image);
746     g_slice_free(ImageContext, context);
747 }
748
749 struct wpe_view_backend* WPEView::backend() const
750 {
751     return wpe.exportable ? wpe_view_backend_exportable_fdo_get_view_backend(wpe.exportable) : nullptr;
752 }
753
754 void WPEView::dispatchKeyboardEvent(struct wpe_input_keyboard_event& wpe_event)
755 {
756     s_view->dispatch([&]() {
757         wpe_view_backend_dispatch_keyboard_event(backend(), &wpe_event);
758     });
759 }
760
761 void WPEView::dispatchPointerEvent(struct wpe_input_pointer_event& wpe_event)
762 {
763     s_view->dispatch([&]() {
764         wpe_view_backend_dispatch_pointer_event(backend(), &wpe_event);
765     });
766 }
767
768 void WPEView::dispatchAxisEvent(struct wpe_input_axis_event& wpe_event)
769 {
770     s_view->dispatch([&]() {
771         wpe_view_backend_dispatch_axis_event(backend(), &wpe_event);
772     });
773 }