debugqroverlay: fix string leak
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-bad / 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 Job {
102         Job(Function& f)
103             : func(f)
104         {
105             g_mutex_init(&mutex);
106             g_cond_init(&cond);
107             dispatched = FALSE;
108         }
109         ~Job() {
110             g_mutex_clear(&mutex);
111             g_cond_clear(&cond);
112         }
113
114         void dispatch() {
115             GMutexHolder lock(mutex);
116             func();
117             dispatched = TRUE;
118             g_cond_signal(&cond);
119         }
120
121         void waitCompletion() {
122             GMutexHolder lock(mutex);
123             while(!dispatched) {
124                 g_cond_wait(&cond, &mutex);
125             }
126         }
127
128         Function& func;
129         GMutex mutex;
130         GCond cond;
131         gboolean dispatched;
132     };
133
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);
138         job->dispatch();
139         return G_SOURCE_REMOVE;
140     }, &job, nullptr);
141     g_source_set_priority(source, G_PRIORITY_DEFAULT);
142     g_source_attach(source, glib.context);
143     job.waitCompletion();
144     g_source_unref(source);
145 }
146
147 gpointer WPEContextThread::s_viewThread(gpointer data)
148 {
149     auto& view = *static_cast<WPEContextThread*>(data);
150
151     view.glib.context = g_main_context_new();
152     view.glib.loop = g_main_loop_new(view.glib.context, FALSE);
153
154     g_main_context_push_thread_default(view.glib.context);
155
156     {
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;
165             },
166             &view, nullptr);
167         g_source_attach(source, view.glib.context);
168         g_source_unref(source);
169     }
170
171     g_main_loop_run(view.glib.loop);
172
173     g_main_loop_unref(view.glib.loop);
174     view.glib.loop = nullptr;
175
176     g_main_context_pop_thread_default(view.glib.context);
177     g_main_context_unref(view.glib.context);
178     view.glib.context = nullptr;
179     return nullptr;
180 }
181
182 #ifdef G_OS_UNIX
183 static void
184 initialize_web_extensions (WebKitWebContext *context)
185 {
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);
190 }
191
192 static void
193 webkit_extension_gerror_msg_received (GstWpeSrc *src, GVariant *params)
194 {
195     GstStructure *structure;
196     GstMessage *forwarded;
197     const gchar *src_path, *src_type, *src_name, *error_domain, *msg, *debug_str, *details_str;
198     gint message_type;
199     guint32 error_code;
200
201     g_variant_get (params, "(issssusss)",
202        &message_type,
203        &src_type,
204        &src_name,
205        &src_path,
206        &error_domain,
207        &error_code,
208        &msg,
209        &debug_str,
210        &details_str
211     );
212
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",
217         debug_str, src_path
218     );
219
220
221     if (message_type == GST_MESSAGE_ERROR) {
222         forwarded =
223             gst_message_new_error_with_details(GST_OBJECT(src), error,
224                                                our_message, details);
225     } else if (message_type == GST_MESSAGE_WARNING) {
226         forwarded =
227             gst_message_new_warning_with_details(GST_OBJECT(src), error,
228                                                  our_message, details);
229     } else {
230         forwarded =
231             gst_message_new_info_with_details(GST_OBJECT(src), error, our_message, details);
232     }
233
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,
239         NULL
240     );
241
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));
245     g_error_free(error);
246     gst_message_unref (forwarded);
247 }
248
249 static void
250 webkit_extension_bus_message_received (GstWpeSrc *src, GVariant *params)
251 {
252     GstStructure *original_structure, *structure;
253     const gchar *src_name, *src_type, *src_path, *struct_str;
254     GstMessageType message_type;
255     GstMessage *forwarded;
256
257     g_variant_get (params, "(issss)",
258        &message_type,
259        &src_name,
260        &src_type,
261        &src_path,
262        &struct_str
263     );
264
265     original_structure = (struct_str[0] != '\0') ? gst_structure_new_from_string(struct_str) : NULL;
266     if (!original_structure)
267     {
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");
271
272     }
273
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,
281         NULL
282     );
283
284     gst_element_post_message(GST_ELEMENT(src), gst_message_new_custom(GST_MESSAGE_ELEMENT,
285                                                                       GST_OBJECT(src), structure));
286
287     gst_message_unref (forwarded);
288 }
289
290 static gboolean
291 webkit_extension_msg_received (WebKitWebContext  *context,
292                WebKitUserMessage *message,
293                GstWpeSrc           *src)
294 {
295     const gchar *name = webkit_user_message_get_name (message);
296     GVariant *params = webkit_user_message_get_parameters (message);
297     gboolean res = TRUE;
298
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);
315
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);
319
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);
323
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);
329     } else {
330         res = FALSE;
331         g_error("Unknown event: %s", name);
332     }
333
334     return res;
335 }
336 #endif
337
338 WPEView* WPEContextThread::createWPEView(GstWpeVideoSrc* src, GstGLContext* context, GstGLDisplay* display, int width, int height)
339 {
340     GST_DEBUG("context %p display %p, size (%d,%d)", context, display, width, height);
341
342     static std::once_flag s_loaderFlag;
343     std::call_once(s_loaderFlag,
344         [] {
345             wpe_loader_init("libWPEBackend-fdo-1.0.so");
346         });
347
348     WPEView* view = nullptr;
349     dispatch([&]() mutable {
350         if (!glib.web_context) {
351             auto *manager = webkit_website_data_manager_new_ephemeral();
352             glib.web_context =
353                 webkit_web_context_new_with_website_data_manager(manager);
354             g_object_unref(manager);
355         }
356         view = new WPEView(glib.web_context, src, context, display, width, height);
357     });
358
359     if (view && view->hasUri()) {
360         GST_DEBUG("waiting load to finish");
361         view->waitLoadCompletion();
362         GST_DEBUG("done");
363     }
364
365     return view;
366 }
367
368 static gboolean s_loadFailed(WebKitWebView*, WebKitLoadEvent, gchar* failing_uri, GError* error, gpointer data)
369 {
370     GstWpeVideoSrc* src = GST_WPE_VIDEO_SRC(data);
371
372     if (g_error_matches(error, WEBKIT_NETWORK_ERROR, WEBKIT_NETWORK_ERROR_CANCELLED)) {
373         GST_INFO_OBJECT (src, "Loading cancelled.");
374
375         return FALSE;
376     }
377
378     GST_ELEMENT_ERROR (GST_ELEMENT_CAST(src), RESOURCE, FAILED, (NULL), ("Failed to load %s (%s)", failing_uri, error->message));
379     return FALSE;
380 }
381
382 static gboolean s_loadFailedWithTLSErrors(WebKitWebView*,  gchar* failing_uri, GTlsCertificate*, GTlsCertificateFlags, gpointer data)
383 {
384     // Defer to load-failed.
385     return FALSE;
386 }
387
388 static void s_loadProgressChaned(GObject* object, GParamSpec*, gpointer data)
389 {
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
393     // directly.
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);
400 }
401
402 WPEView::WPEView(WebKitWebContext* web_context, GstWpeVideoSrc* src, GstGLContext* context, GstGLDisplay* display, int width, int height)
403 {
404 #ifdef G_OS_UNIX
405 {
406         GstObject *parent = gst_object_get_parent (GST_OBJECT (src));
407
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),
412                               NULL);
413             audio.extension_msg_sigid = g_signal_connect (web_context,
414                                 "user-message-received",
415                                 G_CALLBACK (webkit_extension_msg_received),
416                                 parent);
417             GST_INFO_OBJECT (parent, "Enabled audio");
418         }
419
420         gst_clear_object (&parent);
421 }
422 #endif // G_OS_UNIX
423
424     g_mutex_init(&threading.ready_mutex);
425     g_cond_init(&threading.ready_cond);
426     threading.ready = FALSE;
427
428     g_mutex_init(&images_mutex);
429     if (context)
430         gst.context = GST_GL_CONTEXT(gst_object_ref(context));
431     if (display) {
432         gst.display = GST_GL_DISPLAY(gst_object_ref(display));
433     }
434
435     wpe.width = width;
436     wpe.height = height;
437
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);
441       } else {
442         GST_DEBUG ("Available GStreamer GL Context is not EGL - not creating an EGL display from it");
443       }
444     }
445
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);
449
450         m_isValid = wpe_fdo_initialize_for_egl_display(eglDisplay);
451         GST_DEBUG("FDO EGL display initialisation result: %d", m_isValid);
452     } else {
453         m_isValid = wpe_fdo_initialize_shm();
454         GST_DEBUG("FDO SHM initialisation result: %d", m_isValid);
455     }
456     if (!m_isValid)
457         return;
458
459     if (gst.display_egl) {
460         wpe.exportable = wpe_view_backend_exportable_fdo_egl_create(&s_exportableEGLClient, this, wpe.width, wpe.height);
461     } else {
462         wpe.exportable = wpe_view_backend_exportable_fdo_create(&s_exportableClient, this, wpe.width, wpe.height);
463     }
464
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);
468
469     webkit.view = WEBKIT_WEB_VIEW(g_object_new(WEBKIT_TYPE_WEB_VIEW,
470         "web-context", web_context,
471         "backend", viewBackend,
472         nullptr));
473
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);
477
478     auto* settings = webkit_web_view_get_settings(webkit.view);
479     webkit_settings_set_enable_webaudio(settings, TRUE);
480
481     gst_wpe_video_src_configure_web_view(src, webkit.view);
482
483     gchar* location;
484     gboolean drawBackground = TRUE;
485     g_object_get(src, "location", &location, "draw-background", &drawBackground, nullptr);
486     setDrawBackground(drawBackground);
487     if (location) {
488         loadUriUnlocked(location);
489         g_free(location);
490     }
491 }
492
493 WPEView::~WPEView()
494 {
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);
500
501     g_mutex_clear(&threading.ready_mutex);
502     g_cond_clear(&threading.ready_cond);
503
504     {
505         GMutexHolder lock(images_mutex);
506
507         if (egl.pending) {
508             egl_pending = egl.pending;
509             egl.pending = nullptr;
510         }
511         if (egl.committed) {
512             egl_committed = egl.committed;
513             egl.committed = nullptr;
514         }
515         if (shm.pending) {
516             GST_TRACE ("%p freeing shm pending %" GST_PTR_FORMAT, this, shm.pending);
517             shm_pending = shm.pending;
518             shm.pending = nullptr;
519         }
520         if (shm.committed) {
521             GST_TRACE ("%p freeing shm commited %" GST_PTR_FORMAT, this, shm.committed);
522             shm_committed = shm.committed;
523             shm.committed = nullptr;
524         }
525     }
526
527     if (egl_pending)
528         gst_egl_image_unref (egl_pending);
529     if (egl_committed)
530         gst_egl_image_unref (egl_committed);
531     if (shm_pending)
532         gst_buffer_unref (shm_pending);
533     if (shm_committed)
534         gst_buffer_unref (shm_committed);
535
536     if (audio.init_ext_sigid) {
537         WebKitWebContext* web_context = webkit_web_view_get_context (webkit.view);
538
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;
543     }
544
545     WPEContextThread::singleton().dispatch([&]() {
546         if (webkit.view) {
547             g_object_unref(webkit.view);
548             webkit.view = nullptr;
549         }
550     });
551
552     if (gst.display_egl) {
553         gst_object_unref(gst.display_egl);
554         gst.display_egl = nullptr;
555     }
556
557     if (gst.display) {
558         gst_object_unref(gst.display);
559         gst.display = nullptr;
560     }
561
562     if (gst.context) {
563         gst_object_unref(gst.context);
564         gst.context = nullptr;
565     }
566     if (webkit.uri) {
567         g_free(webkit.uri);
568         webkit.uri = nullptr;
569     }
570
571     g_mutex_clear(&images_mutex);
572     GST_TRACE ("%p destroyed", this);
573 }
574
575 void WPEView::notifyLoadFinished()
576 {
577     GMutexHolder lock(threading.ready_mutex);
578     if (!threading.ready) {
579         threading.ready = TRUE;
580         g_cond_signal(&threading.ready_cond);
581     }
582 }
583
584 void WPEView::waitLoadCompletion()
585 {
586     GMutexHolder lock(threading.ready_mutex);
587     while (!threading.ready)
588         g_cond_wait(&threading.ready_cond, &threading.ready_mutex);
589 }
590
591 GstEGLImage* WPEView::image()
592 {
593     GstEGLImage* ret = nullptr;
594     bool dispatchFrameComplete = false;
595     GstEGLImage *prev_image = NULL;
596
597     {
598         GMutexHolder lock(images_mutex);
599
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,
602                   egl.committed,
603                   GST_IS_EGL_IMAGE(egl.committed) ? GST_MINI_OBJECT_REFCOUNT_VALUE(GST_MINI_OBJECT_CAST(egl.committed)) : 0);
604
605         if (egl.pending) {
606             prev_image = egl.committed;
607             egl.committed = egl.pending;
608             egl.pending = nullptr;
609
610             dispatchFrameComplete = true;
611         }
612
613         if (egl.committed)
614             ret = egl.committed;
615     }
616
617     if (prev_image)
618         gst_egl_image_unref(prev_image);
619
620     if (dispatchFrameComplete)
621         frameComplete();
622
623     return ret;
624 }
625
626 GstBuffer* WPEView::buffer()
627 {
628     GstBuffer* ret = nullptr;
629     bool dispatchFrameComplete = false;
630     GstBuffer *prev_image = NULL;
631
632     {
633         GMutexHolder lock(images_mutex);
634
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,
637                   shm.committed,
638                   GST_IS_BUFFER(shm.committed) ? GST_MINI_OBJECT_REFCOUNT_VALUE(GST_MINI_OBJECT_CAST(shm.committed)) : 0);
639
640         if (shm.pending) {
641             prev_image = shm.committed;
642             shm.committed = shm.pending;
643             shm.pending = nullptr;
644
645             dispatchFrameComplete = true;
646         }
647
648         if (shm.committed)
649             ret = shm.committed;
650     }
651
652     if (prev_image)
653         gst_buffer_unref(prev_image);
654
655     if (dispatchFrameComplete)
656         frameComplete();
657
658     return ret;
659 }
660
661 void WPEView::resize(int width, int height)
662 {
663     GST_DEBUG("resize to %dx%d", width, height);
664     wpe.width = width;
665     wpe.height = height;
666
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);
670     });
671 }
672
673 void WPEView::frameComplete()
674 {
675     GST_TRACE("frame complete");
676     s_view->dispatch([&]() {
677         GST_TRACE("dispatching");
678         wpe_view_backend_exportable_fdo_dispatch_frame_complete(wpe.exportable);
679     });
680 }
681
682 void WPEView::loadUriUnlocked(const gchar* uri)
683 {
684     if (webkit.uri)
685         g_free(webkit.uri);
686
687     GST_DEBUG("loading %s", uri);
688     webkit.uri = g_strdup(uri);
689     webkit_web_view_load_uri(webkit.view, webkit.uri);
690 }
691
692 void WPEView::loadUri(const gchar* uri)
693 {
694     s_view->dispatch([&]() {
695         loadUriUnlocked(uri);
696     });
697 }
698
699 static void s_runJavascriptFinished(GObject* object, GAsyncResult* result, gpointer user_data)
700 {
701     WebKitJavascriptResult* js_result;
702     GError* error = NULL;
703
704     js_result = webkit_web_view_run_javascript_finish(WEBKIT_WEB_VIEW(object), result, &error);
705     if (!js_result) {
706         GST_WARNING("Error running javascript: %s", error->message);
707         g_error_free(error);
708         return;
709     }
710     webkit_javascript_result_unref(js_result);
711 }
712
713 void WPEView::runJavascript(const char* script)
714 {
715     s_view->dispatch([&]() {
716         webkit_web_view_run_javascript(webkit.view, script, nullptr, s_runJavascriptFinished, nullptr);
717     });
718 }
719
720 void WPEView::loadData(GBytes* bytes)
721 {
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);
725     });
726 }
727
728 void WPEView::setDrawBackground(gboolean drawsBackground)
729 {
730     GST_DEBUG("%s background rendering", drawsBackground ? "Enabling" : "Disabling");
731     WebKitColor color;
732     webkit_color_parse(&color, drawsBackground ? "white" : "transparent");
733     webkit_web_view_set_background_color(webkit.view, &color);
734 }
735
736 void WPEView::releaseImage(gpointer imagePointer)
737 {
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));
742     });
743 }
744
745 struct ImageContext {
746     WPEView* view;
747     gpointer image;
748 };
749
750 void WPEView::handleExportedImage(gpointer image)
751 {
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));
756
757     auto* gstImage = gst_egl_image_new_wrapped(gst.context, eglImage, GST_GL_RGBA, imageContext, s_releaseImage);
758     {
759       GMutexHolder lock(images_mutex);
760
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;
764
765       notifyLoadFinished();
766     }
767 }
768
769 struct SHMBufferContext {
770     WPEView* view;
771     struct wpe_fdo_shm_exported_buffer* buffer;
772 };
773
774 void WPEView::releaseSHMBuffer(gpointer data)
775 {
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);
781     });
782 }
783
784 void WPEView::s_releaseSHMBuffer(gpointer data)
785 {
786     SHMBufferContext* context = static_cast<SHMBufferContext*>(data);
787     context->view->releaseSHMBuffer(data);
788     g_slice_free(SHMBufferContext, context);
789 }
790
791 void WPEView::handleExportedBuffer(struct wpe_fdo_shm_exported_buffer* buffer)
792 {
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);
797         return;
798     }
799
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));
805
806     SHMBufferContext* bufferContext = g_slice_new(SHMBufferContext);
807     bufferContext->view = this;
808     bufferContext->buffer = buffer;
809
810     auto* gstBuffer = gst_buffer_new_wrapped_full(GST_MEMORY_FLAG_READONLY, data, size, 0, size, bufferContext, s_releaseSHMBuffer);
811     gsize offsets[1];
812     gint strides[1];
813     offsets[0] = 0;
814     strides[0] = stride;
815     gst_buffer_add_video_meta_full(gstBuffer, GST_VIDEO_FRAME_FLAG_NONE, GST_VIDEO_FORMAT_BGRA, width, height, 1, offsets, strides);
816
817     {
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();
823     }
824 }
825
826 struct wpe_view_backend_exportable_fdo_egl_client WPEView::s_exportableEGLClient = {
827     // export_egl_image
828     nullptr,
829     [](void* data, struct wpe_fdo_egl_exported_image* image) {
830         auto& view = *static_cast<WPEView*>(data);
831         view.handleExportedImage(static_cast<gpointer>(image));
832     },
833     nullptr,
834     // padding
835     nullptr, nullptr
836 };
837
838 struct wpe_view_backend_exportable_fdo_client WPEView::s_exportableClient = {
839     nullptr,
840     nullptr,
841     // export_shm_buffer
842     [](void* data, struct wpe_fdo_shm_exported_buffer* buffer) {
843         auto& view = *static_cast<WPEView*>(data);
844         view.handleExportedBuffer(buffer);
845     },
846     nullptr,
847     nullptr,
848 };
849
850 void WPEView::s_releaseImage(GstEGLImage* image, gpointer data)
851 {
852     ImageContext* context = static_cast<ImageContext*>(data);
853     context->view->releaseImage(context->image);
854     g_slice_free(ImageContext, context);
855 }
856
857 struct wpe_view_backend* WPEView::backend() const
858 {
859     return wpe.exportable ? wpe_view_backend_exportable_fdo_get_view_backend(wpe.exportable) : nullptr;
860 }
861
862 void WPEView::dispatchKeyboardEvent(struct wpe_input_keyboard_event& wpe_event)
863 {
864     s_view->dispatch([&]() {
865         wpe_view_backend_dispatch_keyboard_event(backend(), &wpe_event);
866     });
867 }
868
869 void WPEView::dispatchPointerEvent(struct wpe_input_pointer_event& wpe_event)
870 {
871     s_view->dispatch([&]() {
872         wpe_view_backend_dispatch_pointer_event(backend(), &wpe_event);
873     });
874 }
875
876 void WPEView::dispatchAxisEvent(struct wpe_input_axis_event& wpe_event)
877 {
878     s_view->dispatch([&]() {
879         wpe_view_backend_dispatch_axis_event(backend(), &wpe_event);
880     });
881 }
882
883 void WPEView::dispatchTouchEvent(struct wpe_input_touch_event& wpe_event)
884 {
885     s_view->dispatch([&]() {
886         wpe_view_backend_dispatch_touch_event(backend(), &wpe_event);
887     });
888 }