1 /* Copyright (C) <2018, 2019, 2020> Philippe Normand <philn@igalia.com>
2 * Copyright (C) <2018> Žan Doberšek <zdobersek@igalia.com>
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.
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.
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.
24 #include "WPEThreadedView.h"
26 #include "gstwpesrcbin.h"
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>
36 #include <wpe/unstable/fdo-shm.h>
38 GST_DEBUG_CATEGORY_EXTERN (wpe_view_debug);
39 #define GST_CAT_DEFAULT wpe_view_debug
43 GMutexHolder(GMutex& mutex)
57 static WPEContextThread *s_view = NULL;
59 WPEContextThread& WPEContextThread::singleton()
61 static gsize initialized = 0;
63 if (g_once_init_enter (&initialized)) {
64 s_view = new WPEContextThread;
66 g_once_init_leave (&initialized, 1);
72 WPEContextThread::WPEContextThread()
74 g_mutex_init(&threading.mutex);
75 g_cond_init(&threading.cond);
78 GMutexHolder lock(threading.mutex);
79 threading.thread = g_thread_new("WPEContextThread", s_viewThread, this);
80 while (!threading.ready) {
81 g_cond_wait(&threading.cond, &threading.mutex);
83 GST_DEBUG("thread spawned");
87 WPEContextThread::~WPEContextThread()
89 if (threading.thread) {
90 g_thread_unref(threading.thread);
91 threading.thread = nullptr;
94 g_mutex_clear(&threading.mutex);
95 g_cond_clear(&threading.cond);
98 template<typename Function>
99 void WPEContextThread::dispatch(Function func)
105 g_mutex_init(&mutex);
110 g_mutex_clear(&mutex);
115 GMutexHolder lock(mutex);
118 g_cond_signal(&cond);
121 void waitCompletion() {
122 GMutexHolder lock(mutex);
124 g_cond_wait(&cond, &mutex);
134 struct Job job(func);
135 GSource* source = g_idle_source_new();
136 g_source_set_callback(source, [](gpointer data) -> gboolean {
137 auto* job = static_cast<struct Job*>(data);
139 return G_SOURCE_REMOVE;
141 g_source_set_priority(source, G_PRIORITY_DEFAULT);
142 g_source_attach(source, glib.context);
143 job.waitCompletion();
144 g_source_unref(source);
147 gpointer WPEContextThread::s_viewThread(gpointer data)
149 auto& view = *static_cast<WPEContextThread*>(data);
151 view.glib.context = g_main_context_new();
152 view.glib.loop = g_main_loop_new(view.glib.context, FALSE);
154 g_main_context_push_thread_default(view.glib.context);
157 GSource* source = g_idle_source_new();
158 g_source_set_callback(source,
159 [](gpointer data) -> gboolean {
160 auto& view = *static_cast<WPEContextThread*>(data);
161 GMutexHolder lock(view.threading.mutex);
162 view.threading.ready = TRUE;
163 g_cond_signal(&view.threading.cond);
164 return G_SOURCE_REMOVE;
167 g_source_attach(source, view.glib.context);
168 g_source_unref(source);
171 g_main_loop_run(view.glib.loop);
173 g_main_loop_unref(view.glib.loop);
174 view.glib.loop = nullptr;
176 g_main_context_pop_thread_default(view.glib.context);
177 g_main_context_unref(view.glib.context);
178 view.glib.context = nullptr;
184 initialize_web_extensions (WebKitWebContext *context)
186 const gchar *local_path = gst_wpe_get_devenv_extension_path ();
187 const gchar *path = g_file_test (local_path, G_FILE_TEST_IS_DIR) ? local_path : G_STRINGIFY (WPE_EXTENSION_INSTALL_DIR);
188 GST_INFO ("Loading WebExtension from %s", path);
189 webkit_web_context_set_web_extensions_directory (context, path);
193 webkit_extension_gerror_msg_received (GstWpeSrc *src, GVariant *params)
195 GstStructure *structure;
196 GstMessage *forwarded;
197 const gchar *src_path, *src_type, *src_name, *error_domain, *msg, *debug_str, *details_str;
201 g_variant_get (params, "(issssusss)",
213 GError *error = g_error_new(g_quark_from_string(error_domain), error_code, "%s", msg);
214 GstStructure *details = (details_str[0] != '\0') ? gst_structure_new_from_string(details_str) : NULL;
215 gchar * our_message = g_strdup_printf(
216 "`%s` posted from %s running inside the web page",
221 if (message_type == GST_MESSAGE_ERROR) {
223 gst_message_new_error_with_details(GST_OBJECT(src), error,
224 our_message, details);
225 } else if (message_type == GST_MESSAGE_WARNING) {
227 gst_message_new_warning_with_details(GST_OBJECT(src), error,
228 our_message, details);
231 gst_message_new_info_with_details(GST_OBJECT(src), error, our_message, details);
234 structure = gst_structure_new ("WpeForwarded",
235 "message", GST_TYPE_MESSAGE, forwarded,
236 "wpe-original-src-name", G_TYPE_STRING, src_name,
237 "wpe-original-src-type", G_TYPE_STRING, src_type,
238 "wpe-original-src-path", G_TYPE_STRING, src_path,
242 g_free (our_message);
243 gst_element_post_message(GST_ELEMENT(src), gst_message_new_custom(GST_MESSAGE_ELEMENT,
244 GST_OBJECT(src), structure));
246 gst_message_unref (forwarded);
250 webkit_extension_bus_message_received (GstWpeSrc *src, GVariant *params)
252 GstStructure *original_structure, *structure;
253 const gchar *src_name, *src_type, *src_path, *struct_str;
254 GstMessageType message_type;
255 GstMessage *forwarded;
257 g_variant_get (params, "(issss)",
265 original_structure = (struct_str[0] != '\0') ? gst_structure_new_from_string(struct_str) : NULL;
266 if (!original_structure)
268 if (struct_str[0] != '\0')
269 GST_ERROR_OBJECT(src, "Could not deserialize: %s", struct_str);
270 original_structure = gst_structure_new_empty("wpesrc");
274 forwarded = gst_message_new_custom(message_type,
275 GST_OBJECT (src), original_structure);
276 structure = gst_structure_new ("WpeForwarded",
277 "message", GST_TYPE_MESSAGE, forwarded,
278 "wpe-original-src-name", G_TYPE_STRING, src_name,
279 "wpe-original-src-type", G_TYPE_STRING, src_type,
280 "wpe-original-src-path", G_TYPE_STRING, src_path,
284 gst_element_post_message(GST_ELEMENT(src), gst_message_new_custom(GST_MESSAGE_ELEMENT,
285 GST_OBJECT(src), structure));
287 gst_message_unref (forwarded);
291 webkit_extension_msg_received (WebKitWebContext *context,
292 WebKitUserMessage *message,
295 const gchar *name = webkit_user_message_get_name (message);
296 GVariant *params = webkit_user_message_get_parameters (message);
299 GST_TRACE_OBJECT(src, "Handling message %s", name);
300 if (!g_strcmp0(name, "gstwpe.new_stream")) {
301 guint32 id = g_variant_get_uint32 (g_variant_get_child_value (params, 0));
302 const gchar *capsstr = g_variant_get_string (g_variant_get_child_value (params, 1), NULL);
303 GstCaps *caps = gst_caps_from_string (capsstr);
304 const gchar *stream_id = g_variant_get_string (g_variant_get_child_value (params, 2), NULL);
305 gst_wpe_src_new_audio_stream(src, id, caps, stream_id);
306 gst_caps_unref (caps);
307 } else if (!g_strcmp0(name, "gstwpe.set_shm")) {
308 auto fdlist = webkit_user_message_get_fd_list (message);
309 gint id = g_variant_get_uint32 (g_variant_get_child_value (params, 0));
310 gst_wpe_src_set_audio_shm (src, fdlist, id);
311 } else if (!g_strcmp0(name, "gstwpe.new_buffer")) {
312 guint32 id = g_variant_get_uint32 (g_variant_get_child_value (params, 0));
313 guint64 size = g_variant_get_uint64 (g_variant_get_child_value (params, 1));
314 gst_wpe_src_push_audio_buffer (src, id, size);
316 webkit_user_message_send_reply(message, webkit_user_message_new ("gstwpe.buffer_processed", NULL));
317 } else if (!g_strcmp0(name, "gstwpe.pause")) {
318 guint32 id = g_variant_get_uint32 (params);
320 gst_wpe_src_pause_audio_stream (src, id);
321 } else if (!g_strcmp0(name, "gstwpe.stop")) {
322 guint32 id = g_variant_get_uint32 (params);
324 gst_wpe_src_stop_audio_stream (src, id);
325 } else if (!g_strcmp0(name, "gstwpe.bus_gerror_message")) {
326 webkit_extension_gerror_msg_received (src, params);
327 } else if (!g_strcmp0(name, "gstwpe.bus_message")) {
328 webkit_extension_bus_message_received (src, params);
331 g_error("Unknown event: %s", name);
338 WPEView* WPEContextThread::createWPEView(GstWpeVideoSrc* src, GstGLContext* context, GstGLDisplay* display, int width, int height)
340 GST_DEBUG("context %p display %p, size (%d,%d)", context, display, width, height);
342 static std::once_flag s_loaderFlag;
343 std::call_once(s_loaderFlag,
345 wpe_loader_init("libWPEBackend-fdo-1.0.so");
348 WPEView* view = nullptr;
349 dispatch([&]() mutable {
350 if (!glib.web_context) {
351 auto *manager = webkit_website_data_manager_new_ephemeral();
353 webkit_web_context_new_with_website_data_manager(manager);
354 g_object_unref(manager);
356 view = new WPEView(glib.web_context, src, context, display, width, height);
359 if (view && view->hasUri()) {
360 GST_DEBUG("waiting load to finish");
361 view->waitLoadCompletion();
368 static gboolean s_loadFailed(WebKitWebView*, WebKitLoadEvent, gchar* failing_uri, GError* error, gpointer data)
370 GstWpeVideoSrc* src = GST_WPE_VIDEO_SRC(data);
372 if (g_error_matches(error, WEBKIT_NETWORK_ERROR, WEBKIT_NETWORK_ERROR_CANCELLED)) {
373 GST_INFO_OBJECT (src, "Loading cancelled.");
378 GST_ELEMENT_ERROR (GST_ELEMENT_CAST(src), RESOURCE, FAILED, (NULL), ("Failed to load %s (%s)", failing_uri, error->message));
382 static gboolean s_loadFailedWithTLSErrors(WebKitWebView*, gchar* failing_uri, GTlsCertificate*, GTlsCertificateFlags, gpointer data)
384 // Defer to load-failed.
388 static void s_loadProgressChaned(GObject* object, GParamSpec*, gpointer data)
390 GstElement* src = GST_ELEMENT_CAST (data);
391 // The src element is locked already so we can't call
392 // gst_element_post_message(). Instead retrieve the bus manually and use it
394 GstBus* bus = GST_ELEMENT_BUS (src);
395 double estimatedProgress;
396 g_object_get(object, "estimated-load-progress", &estimatedProgress, nullptr);
397 gst_object_ref (bus);
398 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)));
399 gst_object_unref (bus);
402 WPEView::WPEView(WebKitWebContext* web_context, GstWpeVideoSrc* src, GstGLContext* context, GstGLDisplay* display, int width, int height)
406 GstObject *parent = gst_object_get_parent (GST_OBJECT (src));
408 if (parent && GST_IS_WPE_SRC (parent)) {
409 audio.init_ext_sigid = g_signal_connect (web_context,
410 "initialize-web-extensions",
411 G_CALLBACK (initialize_web_extensions),
413 audio.extension_msg_sigid = g_signal_connect (web_context,
414 "user-message-received",
415 G_CALLBACK (webkit_extension_msg_received),
417 GST_INFO_OBJECT (parent, "Enabled audio");
420 gst_clear_object (&parent);
424 g_mutex_init(&threading.ready_mutex);
425 g_cond_init(&threading.ready_cond);
426 threading.ready = FALSE;
428 g_mutex_init(&images_mutex);
430 gst.context = GST_GL_CONTEXT(gst_object_ref(context));
432 gst.display = GST_GL_DISPLAY(gst_object_ref(display));
438 if (context && display) {
439 if (gst_gl_context_get_gl_platform(context) == GST_GL_PLATFORM_EGL) {
440 gst.display_egl = gst_gl_display_egl_from_gl_display (gst.display);
442 GST_DEBUG ("Available GStreamer GL Context is not EGL - not creating an EGL display from it");
446 if (gst.display_egl) {
447 EGLDisplay eglDisplay = (EGLDisplay)gst_gl_display_get_handle (GST_GL_DISPLAY(gst.display_egl));
448 GST_DEBUG("eglDisplay %p", eglDisplay);
450 m_isValid = wpe_fdo_initialize_for_egl_display(eglDisplay);
451 GST_DEBUG("FDO EGL display initialisation result: %d", m_isValid);
453 m_isValid = wpe_fdo_initialize_shm();
454 GST_DEBUG("FDO SHM initialisation result: %d", m_isValid);
459 if (gst.display_egl) {
460 wpe.exportable = wpe_view_backend_exportable_fdo_egl_create(&s_exportableEGLClient, this, wpe.width, wpe.height);
462 wpe.exportable = wpe_view_backend_exportable_fdo_create(&s_exportableClient, this, wpe.width, wpe.height);
465 auto* wpeViewBackend = wpe_view_backend_exportable_fdo_get_view_backend(wpe.exportable);
466 auto* viewBackend = webkit_web_view_backend_new(wpeViewBackend, (GDestroyNotify) wpe_view_backend_exportable_fdo_destroy, wpe.exportable);
467 wpe_view_backend_add_activity_state(wpeViewBackend, wpe_view_activity_state_visible | wpe_view_activity_state_focused | wpe_view_activity_state_in_window);
469 webkit.view = WEBKIT_WEB_VIEW(g_object_new(WEBKIT_TYPE_WEB_VIEW,
470 "web-context", web_context,
471 "backend", viewBackend,
474 g_signal_connect(webkit.view, "load-failed", G_CALLBACK(s_loadFailed), src);
475 g_signal_connect(webkit.view, "load-failed-with-tls-errors", G_CALLBACK(s_loadFailedWithTLSErrors), src);
476 g_signal_connect(webkit.view, "notify::estimated-load-progress", G_CALLBACK(s_loadProgressChaned), src);
478 auto* settings = webkit_web_view_get_settings(webkit.view);
479 webkit_settings_set_enable_webaudio(settings, TRUE);
481 gst_wpe_video_src_configure_web_view(src, webkit.view);
484 gboolean drawBackground = TRUE;
485 g_object_get(src, "location", &location, "draw-background", &drawBackground, nullptr);
486 setDrawBackground(drawBackground);
488 loadUriUnlocked(location);
495 GstEGLImage *egl_pending = NULL;
496 GstEGLImage *egl_committed = NULL;
497 GstBuffer *shm_pending = NULL;
498 GstBuffer *shm_committed = NULL;
499 GST_TRACE ("%p destroying", this);
501 g_mutex_clear(&threading.ready_mutex);
502 g_cond_clear(&threading.ready_cond);
505 GMutexHolder lock(images_mutex);
508 egl_pending = egl.pending;
509 egl.pending = nullptr;
512 egl_committed = egl.committed;
513 egl.committed = nullptr;
516 GST_TRACE ("%p freeing shm pending %" GST_PTR_FORMAT, this, shm.pending);
517 shm_pending = shm.pending;
518 shm.pending = nullptr;
521 GST_TRACE ("%p freeing shm commited %" GST_PTR_FORMAT, this, shm.committed);
522 shm_committed = shm.committed;
523 shm.committed = nullptr;
528 gst_egl_image_unref (egl_pending);
530 gst_egl_image_unref (egl_committed);
532 gst_buffer_unref (shm_pending);
534 gst_buffer_unref (shm_committed);
536 if (audio.init_ext_sigid) {
537 WebKitWebContext* web_context = webkit_web_view_get_context (webkit.view);
539 g_signal_handler_disconnect(web_context, audio.init_ext_sigid);
540 g_signal_handler_disconnect(web_context, audio.extension_msg_sigid);
541 audio.init_ext_sigid = 0;
542 audio.extension_msg_sigid = 0;
545 WPEContextThread::singleton().dispatch([&]() {
547 g_object_unref(webkit.view);
548 webkit.view = nullptr;
552 if (gst.display_egl) {
553 gst_object_unref(gst.display_egl);
554 gst.display_egl = nullptr;
558 gst_object_unref(gst.display);
559 gst.display = nullptr;
563 gst_object_unref(gst.context);
564 gst.context = nullptr;
568 webkit.uri = nullptr;
571 g_mutex_clear(&images_mutex);
572 GST_TRACE ("%p destroyed", this);
575 void WPEView::notifyLoadFinished()
577 GMutexHolder lock(threading.ready_mutex);
578 if (!threading.ready) {
579 threading.ready = TRUE;
580 g_cond_signal(&threading.ready_cond);
584 void WPEView::waitLoadCompletion()
586 GMutexHolder lock(threading.ready_mutex);
587 while (!threading.ready)
588 g_cond_wait(&threading.ready_cond, &threading.ready_mutex);
591 GstEGLImage* WPEView::image()
593 GstEGLImage* ret = nullptr;
594 bool dispatchFrameComplete = false;
595 GstEGLImage *prev_image = NULL;
598 GMutexHolder lock(images_mutex);
600 GST_TRACE("pending %" GST_PTR_FORMAT " (%d) committed %" GST_PTR_FORMAT " (%d)", egl.pending,
601 GST_IS_EGL_IMAGE(egl.pending) ? GST_MINI_OBJECT_REFCOUNT_VALUE(GST_MINI_OBJECT_CAST(egl.pending)) : 0,
603 GST_IS_EGL_IMAGE(egl.committed) ? GST_MINI_OBJECT_REFCOUNT_VALUE(GST_MINI_OBJECT_CAST(egl.committed)) : 0);
606 prev_image = egl.committed;
607 egl.committed = egl.pending;
608 egl.pending = nullptr;
610 dispatchFrameComplete = true;
618 gst_egl_image_unref(prev_image);
620 if (dispatchFrameComplete)
626 GstBuffer* WPEView::buffer()
628 GstBuffer* ret = nullptr;
629 bool dispatchFrameComplete = false;
630 GstBuffer *prev_image = NULL;
633 GMutexHolder lock(images_mutex);
635 GST_TRACE("pending %" GST_PTR_FORMAT " (%d) committed %" GST_PTR_FORMAT " (%d)", shm.pending,
636 GST_IS_BUFFER(shm.pending) ? GST_MINI_OBJECT_REFCOUNT_VALUE(GST_MINI_OBJECT_CAST(shm.pending)) : 0,
638 GST_IS_BUFFER(shm.committed) ? GST_MINI_OBJECT_REFCOUNT_VALUE(GST_MINI_OBJECT_CAST(shm.committed)) : 0);
641 prev_image = shm.committed;
642 shm.committed = shm.pending;
643 shm.pending = nullptr;
645 dispatchFrameComplete = true;
653 gst_buffer_unref(prev_image);
655 if (dispatchFrameComplete)
661 void WPEView::resize(int width, int height)
663 GST_DEBUG("resize to %dx%d", width, height);
667 s_view->dispatch([&]() {
668 if (wpe.exportable && wpe_view_backend_exportable_fdo_get_view_backend(wpe.exportable))
669 wpe_view_backend_dispatch_set_size(wpe_view_backend_exportable_fdo_get_view_backend(wpe.exportable), wpe.width, wpe.height);
673 void WPEView::frameComplete()
675 GST_TRACE("frame complete");
676 s_view->dispatch([&]() {
677 GST_TRACE("dispatching");
678 wpe_view_backend_exportable_fdo_dispatch_frame_complete(wpe.exportable);
682 void WPEView::loadUriUnlocked(const gchar* uri)
687 GST_DEBUG("loading %s", uri);
688 webkit.uri = g_strdup(uri);
689 webkit_web_view_load_uri(webkit.view, webkit.uri);
692 void WPEView::loadUri(const gchar* uri)
694 s_view->dispatch([&]() {
695 loadUriUnlocked(uri);
699 static void s_runJavascriptFinished(GObject* object, GAsyncResult* result, gpointer user_data)
701 WebKitJavascriptResult* js_result;
702 GError* error = NULL;
704 js_result = webkit_web_view_run_javascript_finish(WEBKIT_WEB_VIEW(object), result, &error);
706 GST_WARNING("Error running javascript: %s", error->message);
710 webkit_javascript_result_unref(js_result);
713 void WPEView::runJavascript(const char* script)
715 s_view->dispatch([&]() {
716 webkit_web_view_run_javascript(webkit.view, script, nullptr, s_runJavascriptFinished, nullptr);
720 void WPEView::loadData(GBytes* bytes)
722 s_view->dispatch([this, bytes = g_bytes_ref(bytes)]() {
723 webkit_web_view_load_bytes(webkit.view, bytes, nullptr, nullptr, nullptr);
724 g_bytes_unref(bytes);
728 void WPEView::setDrawBackground(gboolean drawsBackground)
730 GST_DEBUG("%s background rendering", drawsBackground ? "Enabling" : "Disabling");
732 webkit_color_parse(&color, drawsBackground ? "white" : "transparent");
733 webkit_web_view_set_background_color(webkit.view, &color);
736 void WPEView::releaseImage(gpointer imagePointer)
738 s_view->dispatch([&]() {
739 GST_TRACE("Dispatch release exported image %p", imagePointer);
740 wpe_view_backend_exportable_fdo_egl_dispatch_release_exported_image(wpe.exportable,
741 static_cast<struct wpe_fdo_egl_exported_image*>(imagePointer));
745 struct ImageContext {
750 void WPEView::handleExportedImage(gpointer image)
752 ImageContext* imageContext = g_slice_new(ImageContext);
753 imageContext->view = this;
754 imageContext->image = static_cast<gpointer>(image);
755 EGLImageKHR eglImage = wpe_fdo_egl_exported_image_get_egl_image(static_cast<struct wpe_fdo_egl_exported_image*>(image));
757 auto* gstImage = gst_egl_image_new_wrapped(gst.context, eglImage, GST_GL_RGBA, imageContext, s_releaseImage);
759 GMutexHolder lock(images_mutex);
761 GST_TRACE("EGLImage %p wrapped in GstEGLImage %" GST_PTR_FORMAT, eglImage, gstImage);
762 gst_clear_mini_object ((GstMiniObject **) &egl.pending);
763 egl.pending = gstImage;
765 notifyLoadFinished();
769 struct SHMBufferContext {
771 struct wpe_fdo_shm_exported_buffer* buffer;
774 void WPEView::releaseSHMBuffer(gpointer data)
776 SHMBufferContext* context = static_cast<SHMBufferContext*>(data);
777 s_view->dispatch([&]() {
778 auto* buffer = static_cast<struct wpe_fdo_shm_exported_buffer*>(context->buffer);
779 GST_TRACE("Dispatch release exported buffer %p", buffer);
780 wpe_view_backend_exportable_fdo_dispatch_release_shm_exported_buffer(wpe.exportable, buffer);
784 void WPEView::s_releaseSHMBuffer(gpointer data)
786 SHMBufferContext* context = static_cast<SHMBufferContext*>(data);
787 context->view->releaseSHMBuffer(data);
788 g_slice_free(SHMBufferContext, context);
791 void WPEView::handleExportedBuffer(struct wpe_fdo_shm_exported_buffer* buffer)
793 struct wl_shm_buffer* shmBuffer = wpe_fdo_shm_exported_buffer_get_shm_buffer(buffer);
794 auto format = wl_shm_buffer_get_format(shmBuffer);
795 if (format != WL_SHM_FORMAT_ARGB8888 && format != WL_SHM_FORMAT_XRGB8888) {
796 GST_ERROR("Unsupported pixel format: %d", format);
800 int32_t width = wl_shm_buffer_get_width(shmBuffer);
801 int32_t height = wl_shm_buffer_get_height(shmBuffer);
802 gint stride = wl_shm_buffer_get_stride(shmBuffer);
803 gsize size = width * height * 4;
804 auto* data = static_cast<uint8_t*>(wl_shm_buffer_get_data(shmBuffer));
806 SHMBufferContext* bufferContext = g_slice_new(SHMBufferContext);
807 bufferContext->view = this;
808 bufferContext->buffer = buffer;
810 auto* gstBuffer = gst_buffer_new_wrapped_full(GST_MEMORY_FLAG_READONLY, data, size, 0, size, bufferContext, s_releaseSHMBuffer);
815 gst_buffer_add_video_meta_full(gstBuffer, GST_VIDEO_FRAME_FLAG_NONE, GST_VIDEO_FORMAT_BGRA, width, height, 1, offsets, strides);
818 GMutexHolder lock(images_mutex);
819 GST_TRACE("SHM buffer %p wrapped in buffer %" GST_PTR_FORMAT, buffer, gstBuffer);
820 gst_clear_buffer (&shm.pending);
821 shm.pending = gstBuffer;
822 notifyLoadFinished();
826 struct wpe_view_backend_exportable_fdo_egl_client WPEView::s_exportableEGLClient = {
829 [](void* data, struct wpe_fdo_egl_exported_image* image) {
830 auto& view = *static_cast<WPEView*>(data);
831 view.handleExportedImage(static_cast<gpointer>(image));
838 struct wpe_view_backend_exportable_fdo_client WPEView::s_exportableClient = {
842 [](void* data, struct wpe_fdo_shm_exported_buffer* buffer) {
843 auto& view = *static_cast<WPEView*>(data);
844 view.handleExportedBuffer(buffer);
850 void WPEView::s_releaseImage(GstEGLImage* image, gpointer data)
852 ImageContext* context = static_cast<ImageContext*>(data);
853 context->view->releaseImage(context->image);
854 g_slice_free(ImageContext, context);
857 struct wpe_view_backend* WPEView::backend() const
859 return wpe.exportable ? wpe_view_backend_exportable_fdo_get_view_backend(wpe.exportable) : nullptr;
862 void WPEView::dispatchKeyboardEvent(struct wpe_input_keyboard_event& wpe_event)
864 s_view->dispatch([&]() {
865 wpe_view_backend_dispatch_keyboard_event(backend(), &wpe_event);
869 void WPEView::dispatchPointerEvent(struct wpe_input_pointer_event& wpe_event)
871 s_view->dispatch([&]() {
872 wpe_view_backend_dispatch_pointer_event(backend(), &wpe_event);
876 void WPEView::dispatchAxisEvent(struct wpe_input_axis_event& wpe_event)
878 s_view->dispatch([&]() {
879 wpe_view_backend_dispatch_axis_event(backend(), &wpe_event);
883 void WPEView::dispatchTouchEvent(struct wpe_input_touch_event& wpe_event)
885 s_view->dispatch([&]() {
886 wpe_view_backend_dispatch_touch_event(backend(), &wpe_event);