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