c075e9ae1d3b36c37ee008b2c343b210fd0bd903
[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
26 #include <gst/gl/gl.h>
27 #include <gst/gl/egl/gsteglimage.h>
28 #include <gst/gl/egl/gstgldisplay_egl.h>
29 #include <wayland-server.h>
30
31 #include <cstdio>
32 #include <mutex>
33
34 #if ENABLE_SHM_BUFFER_SUPPORT
35 #include <wpe/unstable/fdo-shm.h>
36 #endif
37
38 GST_DEBUG_CATEGORY_EXTERN (wpe_view_debug);
39 #define GST_CAT_DEFAULT wpe_view_debug
40
41 #if defined(WPE_FDO_CHECK_VERSION) && WPE_FDO_CHECK_VERSION(1, 3, 0)
42 #define USE_DEPRECATED_FDO_EGL_IMAGE 0
43 #define WPE_GLIB_SOURCE_PRIORITY G_PRIORITY_DEFAULT
44 #else
45 #define USE_DEPRECATED_FDO_EGL_IMAGE 1
46 #define WPE_GLIB_SOURCE_PRIORITY -70
47 #endif
48
49 class GMutexHolder {
50 public:
51     GMutexHolder(GMutex& mutex)
52         : m(mutex)
53     {
54         g_mutex_lock(&m);
55     }
56     ~GMutexHolder()
57     {
58         g_mutex_unlock(&m);
59     }
60
61 private:
62     GMutex& m;
63 };
64
65 static WPEContextThread *s_view = NULL;
66
67 WPEContextThread& WPEContextThread::singleton()
68 {
69     static gsize initialized = 0;
70
71     if (g_once_init_enter (&initialized)) {
72         s_view = new WPEContextThread;
73
74         g_once_init_leave (&initialized, 1);
75     }
76
77     return *s_view;
78 }
79
80 WPEContextThread::WPEContextThread()
81 {
82     g_mutex_init(&threading.mutex);
83     g_cond_init(&threading.cond);
84
85     {
86         GMutexHolder lock(threading.mutex);
87         threading.thread = g_thread_new("WPEContextThread", s_viewThread, this);
88         g_cond_wait(&threading.cond, &threading.mutex);
89         GST_DEBUG("thread spawned");
90     }
91 }
92
93 WPEContextThread::~WPEContextThread()
94 {
95     if (threading.thread) {
96         g_thread_unref(threading.thread);
97         threading.thread = nullptr;
98     }
99
100     g_mutex_clear(&threading.mutex);
101     g_cond_clear(&threading.cond);
102 }
103
104 template<typename Function>
105 void WPEContextThread::dispatch(Function func)
106 {
107     struct Payload {
108         Function& func;
109     };
110     struct Payload payload { func };
111
112     GSource* source = g_idle_source_new();
113     g_source_set_callback(source, [](gpointer data) -> gboolean {
114         auto& view = WPEContextThread::singleton();
115         GMutexHolder lock(view.threading.mutex);
116
117         auto* payload = static_cast<struct Payload*>(data);
118         payload->func();
119
120         g_cond_signal(&view.threading.cond);
121         return G_SOURCE_REMOVE;
122     }, &payload, nullptr);
123     g_source_set_priority(source, WPE_GLIB_SOURCE_PRIORITY);
124
125     {
126         GMutexHolder lock(threading.mutex);
127         g_source_attach(source, glib.context);
128         g_cond_wait(&threading.cond, &threading.mutex);
129     }
130
131     g_source_unref(source);
132 }
133
134 gpointer WPEContextThread::s_viewThread(gpointer data)
135 {
136     auto& view = *static_cast<WPEContextThread*>(data);
137
138     view.glib.context = g_main_context_new();
139     view.glib.loop = g_main_loop_new(view.glib.context, FALSE);
140
141     g_main_context_push_thread_default(view.glib.context);
142
143     {
144         GSource* source = g_idle_source_new();
145         g_source_set_callback(source,
146             [](gpointer data) -> gboolean {
147                 auto& view = *static_cast<WPEContextThread*>(data);
148                 GMutexHolder lock(view.threading.mutex);
149                 g_cond_signal(&view.threading.cond);
150                 return G_SOURCE_REMOVE;
151             },
152             &view, nullptr);
153         g_source_attach(source, view.glib.context);
154         g_source_unref(source);
155     }
156
157     g_main_loop_run(view.glib.loop);
158
159     g_main_loop_unref(view.glib.loop);
160     view.glib.loop = nullptr;
161
162     g_main_context_pop_thread_default(view.glib.context);
163     g_main_context_unref(view.glib.context);
164     view.glib.context = nullptr;
165     return nullptr;
166 }
167
168 WPEView* WPEContextThread::createWPEView(GstWpeVideoSrc* src, GstGLContext* context, GstGLDisplay* display, int width, int height)
169 {
170     GST_DEBUG("context %p display %p, size (%d,%d)", context, display, width, height);
171
172     static std::once_flag s_loaderFlag;
173     std::call_once(s_loaderFlag,
174         [] {
175 #if defined(WPE_BACKEND_CHECK_VERSION) && WPE_BACKEND_CHECK_VERSION(1, 2, 0)
176             wpe_loader_init("libWPEBackend-fdo-1.0.so");
177 #endif
178         });
179
180     WPEView* view = nullptr;
181     dispatch([&]() mutable {
182         if (!glib.web_context) {
183             auto* manager = webkit_website_data_manager_new_ephemeral();
184             glib.web_context = webkit_web_context_new_with_website_data_manager(manager);
185             g_object_unref(manager);
186         }
187
188         view = new WPEView(glib.web_context, src, context, display, width, height);
189     });
190
191     if (view && view->hasUri()) {
192         GST_DEBUG("waiting load to finish");
193         view->waitLoadCompletion();
194         GST_DEBUG("done");
195     }
196
197     return view;
198 }
199
200 static gboolean s_loadFailed(WebKitWebView*, WebKitLoadEvent, gchar* failing_uri, GError* error, gpointer data)
201 {
202     GstWpeVideoSrc* src = GST_WPE_VIDEO_SRC(data);
203
204     if (g_error_matches(error, WEBKIT_NETWORK_ERROR, WEBKIT_NETWORK_ERROR_CANCELLED)) {
205         GST_INFO_OBJECT (src, "Loading cancelled.");
206
207         return FALSE;
208     }
209
210     GST_ELEMENT_ERROR (GST_ELEMENT_CAST(src), RESOURCE, FAILED, (NULL), ("Failed to load %s (%s)", failing_uri, error->message));
211     return FALSE;
212 }
213
214 static gboolean s_loadFailedWithTLSErrors(WebKitWebView*,  gchar* failing_uri, GTlsCertificate*, GTlsCertificateFlags, gpointer data)
215 {
216     // Defer to load-failed.
217     return FALSE;
218 }
219
220 static void s_loadProgressChaned(GObject* object, GParamSpec*, gpointer data)
221 {
222     GstElement* src = GST_ELEMENT_CAST (data);
223     // The src element is locked already so we can't call
224     // gst_element_post_message(). Instead retrieve the bus manually and use it
225     // directly.
226     GstBus* bus = GST_ELEMENT_BUS (src);
227     double estimatedProgress;
228     g_object_get(object, "estimated-load-progress", &estimatedProgress, nullptr);
229     gst_object_ref (bus);
230     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)));
231     gst_object_unref (bus);
232 }
233
234 WPEView::WPEView(WebKitWebContext* web_context, GstWpeVideoSrc* src, GstGLContext* context, GstGLDisplay* display, int width, int height)
235 {
236     g_mutex_init(&threading.ready_mutex);
237     g_cond_init(&threading.ready_cond);
238     threading.ready = FALSE;
239
240     g_mutex_init(&images_mutex);
241     if (context)
242         gst.context = GST_GL_CONTEXT(gst_object_ref(context));
243     if (display) {
244         gst.display = GST_GL_DISPLAY(gst_object_ref(display));
245     }
246
247     wpe.width = width;
248     wpe.height = height;
249
250     if (context && display) {
251       if (gst_gl_context_get_gl_platform(context) == GST_GL_PLATFORM_EGL) {
252         gst.display_egl = gst_gl_display_egl_from_gl_display (gst.display);
253       } else {
254         GST_DEBUG ("Available GStreamer GL Context is not EGL - not creating an EGL display from it");
255       }
256     }
257
258     if (gst.display_egl) {
259         EGLDisplay eglDisplay = (EGLDisplay)gst_gl_display_get_handle (GST_GL_DISPLAY(gst.display_egl));
260         GST_DEBUG("eglDisplay %p", eglDisplay);
261
262         m_isValid = wpe_fdo_initialize_for_egl_display(eglDisplay);
263         GST_DEBUG("FDO EGL display initialisation result: %d", m_isValid);
264     } else {
265 #if ENABLE_SHM_BUFFER_SUPPORT
266         m_isValid = wpe_fdo_initialize_shm();
267         GST_DEBUG("FDO SHM initialisation result: %d", m_isValid);
268 #else
269         GST_WARNING("FDO SHM support is available only in WPEBackend-FDO 1.7.0");
270 #endif
271     }
272     if (!m_isValid)
273         return;
274
275     if (gst.display_egl) {
276         wpe.exportable = wpe_view_backend_exportable_fdo_egl_create(&s_exportableEGLClient, this, wpe.width, wpe.height);
277     } else {
278 #if ENABLE_SHM_BUFFER_SUPPORT
279         wpe.exportable = wpe_view_backend_exportable_fdo_create(&s_exportableClient, this, wpe.width, wpe.height);
280 #endif
281     }
282
283     auto* wpeViewBackend = wpe_view_backend_exportable_fdo_get_view_backend(wpe.exportable);
284     auto* viewBackend = webkit_web_view_backend_new(wpeViewBackend, (GDestroyNotify) wpe_view_backend_exportable_fdo_destroy, wpe.exportable);
285 #if defined(WPE_BACKEND_CHECK_VERSION) && WPE_BACKEND_CHECK_VERSION(1, 1, 0)
286     wpe_view_backend_add_activity_state(wpeViewBackend, wpe_view_activity_state_visible | wpe_view_activity_state_focused | wpe_view_activity_state_in_window);
287 #endif
288
289     webkit.view = WEBKIT_WEB_VIEW(g_object_new(WEBKIT_TYPE_WEB_VIEW,
290         "web-context", web_context,
291         "backend", viewBackend,
292         nullptr));
293
294     g_signal_connect(webkit.view, "load-failed", G_CALLBACK(s_loadFailed), src);
295     g_signal_connect(webkit.view, "load-failed-with-tls-errors", G_CALLBACK(s_loadFailedWithTLSErrors), src);
296     g_signal_connect(webkit.view, "notify::estimated-load-progress", G_CALLBACK(s_loadProgressChaned), src);
297
298     gst_wpe_video_src_configure_web_view(src, webkit.view);
299
300     gchar* location;
301     gboolean drawBackground = TRUE;
302     g_object_get(src, "location", &location, "draw-background", &drawBackground, nullptr);
303     setDrawBackground(drawBackground);
304     if (location) {
305         loadUriUnlocked(location);
306         g_free(location);
307     }
308 }
309
310 WPEView::~WPEView()
311 {
312     GstEGLImage *egl_pending = NULL;
313     GstEGLImage *egl_committed = NULL;
314     GstBuffer *shm_pending = NULL;
315     GstBuffer *shm_committed = NULL;
316     GST_TRACE ("%p destroying", this);
317
318     g_mutex_clear(&threading.ready_mutex);
319     g_cond_clear(&threading.ready_cond);
320
321     {
322         GMutexHolder lock(images_mutex);
323
324         if (egl.pending) {
325             egl_pending = egl.pending;
326             egl.pending = nullptr;
327         }
328         if (egl.committed) {
329             egl_committed = egl.committed;
330             egl.committed = nullptr;
331         }
332         if (shm.pending) {
333             GST_TRACE ("%p freeing shm pending %" GST_PTR_FORMAT, this, shm.pending);
334             shm_pending = shm.pending;
335             shm.pending = nullptr;
336         }
337         if (shm.committed) {
338             GST_TRACE ("%p freeing shm commited %" GST_PTR_FORMAT, this, shm.committed);
339             shm_committed = shm.committed;
340             shm.committed = nullptr;
341         }
342     }
343
344     if (egl_pending)
345         gst_egl_image_unref (egl_pending);
346     if (egl_committed)
347         gst_egl_image_unref (egl_committed);
348     if (shm_pending)
349         gst_buffer_unref (shm_pending);
350     if (shm_committed)
351         gst_buffer_unref (shm_committed);
352
353     WPEContextThread::singleton().dispatch([&]() {
354         if (webkit.view) {
355             g_object_unref(webkit.view);
356             webkit.view = nullptr;
357         }
358     });
359
360     if (gst.display_egl) {
361         gst_object_unref(gst.display_egl);
362         gst.display_egl = nullptr;
363     }
364
365     if (gst.display) {
366         gst_object_unref(gst.display);
367         gst.display = nullptr;
368     }
369
370     if (gst.context) {
371         gst_object_unref(gst.context);
372         gst.context = nullptr;
373     }
374     if (webkit.uri) {
375         g_free(webkit.uri);
376         webkit.uri = nullptr;
377     }
378
379     g_mutex_clear(&images_mutex);
380     GST_TRACE ("%p destroyed", this);
381 }
382
383 void WPEView::notifyLoadFinished()
384 {
385     GMutexHolder lock(threading.ready_mutex);
386     if (!threading.ready) {
387         threading.ready = TRUE;
388         g_cond_signal(&threading.ready_cond);
389     }
390 }
391
392 void WPEView::waitLoadCompletion()
393 {
394     GMutexHolder lock(threading.ready_mutex);
395     while (!threading.ready)
396         g_cond_wait(&threading.ready_cond, &threading.ready_mutex);
397 }
398
399 GstEGLImage* WPEView::image()
400 {
401     GstEGLImage* ret = nullptr;
402     bool dispatchFrameComplete = false;
403     GstEGLImage *prev_image = NULL;
404
405     {
406         GMutexHolder lock(images_mutex);
407
408         GST_TRACE("pending %" GST_PTR_FORMAT " (%d) committed %" GST_PTR_FORMAT " (%d)", egl.pending,
409                   GST_IS_EGL_IMAGE(egl.pending) ? GST_MINI_OBJECT_REFCOUNT_VALUE(GST_MINI_OBJECT_CAST(egl.pending)) : 0,
410                   egl.committed,
411                   GST_IS_EGL_IMAGE(egl.committed) ? GST_MINI_OBJECT_REFCOUNT_VALUE(GST_MINI_OBJECT_CAST(egl.committed)) : 0);
412
413         if (egl.pending) {
414             prev_image = egl.committed;
415             egl.committed = egl.pending;
416             egl.pending = nullptr;
417
418             dispatchFrameComplete = true;
419         }
420
421         if (egl.committed)
422             ret = egl.committed;
423     }
424
425     if (prev_image)
426         gst_egl_image_unref(prev_image);
427
428     if (dispatchFrameComplete)
429         frameComplete();
430
431     return ret;
432 }
433
434 GstBuffer* WPEView::buffer()
435 {
436     GstBuffer* ret = nullptr;
437     bool dispatchFrameComplete = false;
438     GstBuffer *prev_image = NULL;
439
440     {
441         GMutexHolder lock(images_mutex);
442
443         GST_TRACE("pending %" GST_PTR_FORMAT " (%d) committed %" GST_PTR_FORMAT " (%d)", shm.pending,
444                   GST_IS_BUFFER(shm.pending) ? GST_MINI_OBJECT_REFCOUNT_VALUE(GST_MINI_OBJECT_CAST(shm.pending)) : 0,
445                   shm.committed,
446                   GST_IS_BUFFER(shm.committed) ? GST_MINI_OBJECT_REFCOUNT_VALUE(GST_MINI_OBJECT_CAST(shm.committed)) : 0);
447
448         if (shm.pending) {
449             prev_image = shm.committed;
450             shm.committed = shm.pending;
451             shm.pending = nullptr;
452
453             dispatchFrameComplete = true;
454         }
455
456         if (shm.committed)
457             ret = shm.committed;
458     }
459
460     if (prev_image)
461         gst_buffer_unref(prev_image);
462
463     if (dispatchFrameComplete)
464         frameComplete();
465
466     return ret;
467 }
468
469 void WPEView::resize(int width, int height)
470 {
471     GST_DEBUG("resize to %dx%d", width, height);
472     wpe.width = width;
473     wpe.height = height;
474
475     s_view->dispatch([&]() {
476         if (wpe.exportable && wpe_view_backend_exportable_fdo_get_view_backend(wpe.exportable))
477           wpe_view_backend_dispatch_set_size(wpe_view_backend_exportable_fdo_get_view_backend(wpe.exportable), wpe.width, wpe.height);
478     });
479 }
480
481 void WPEView::frameComplete()
482 {
483     GST_TRACE("frame complete");
484     s_view->dispatch([&]() {
485         GST_TRACE("dispatching");
486         wpe_view_backend_exportable_fdo_dispatch_frame_complete(wpe.exportable);
487     });
488 }
489
490 void WPEView::loadUriUnlocked(const gchar* uri)
491 {
492     if (webkit.uri)
493         g_free(webkit.uri);
494
495     GST_DEBUG("loading %s", uri);
496     webkit.uri = g_strdup(uri);
497     webkit_web_view_load_uri(webkit.view, webkit.uri);
498 }
499
500 void WPEView::loadUri(const gchar* uri)
501 {
502     s_view->dispatch([&]() {
503         loadUriUnlocked(uri);
504     });
505 }
506
507 void WPEView::loadData(GBytes* bytes)
508 {
509     s_view->dispatch([this, bytes = g_bytes_ref(bytes)]() {
510         webkit_web_view_load_bytes(webkit.view, bytes, nullptr, nullptr, nullptr);
511         g_bytes_unref(bytes);
512     });
513 }
514
515 void WPEView::setDrawBackground(gboolean drawsBackground)
516 {
517     GST_DEBUG("%s background rendering", drawsBackground ? "Enabling" : "Disabling");
518     WebKitColor color;
519     webkit_color_parse(&color, drawsBackground ? "white" : "transparent");
520     webkit_web_view_set_background_color(webkit.view, &color);
521 }
522
523 void WPEView::registerAudioReceiver(const struct wpe_audio_receiver* audioReceiver, gpointer userData)
524 {
525     wpe_audio_register_receiver(audioReceiver, userData);
526 }
527
528 void WPEView::releaseImage(gpointer imagePointer)
529 {
530     s_view->dispatch([&]() {
531         GST_TRACE("Dispatch release exported image %p", imagePointer);
532 #if USE_DEPRECATED_FDO_EGL_IMAGE
533         wpe_view_backend_exportable_fdo_egl_dispatch_release_image(wpe.exportable,
534                                                                    static_cast<EGLImageKHR>(imagePointer));
535 #else
536         wpe_view_backend_exportable_fdo_egl_dispatch_release_exported_image(wpe.exportable,
537                                                                             static_cast<struct wpe_fdo_egl_exported_image*>(imagePointer));
538 #endif
539     });
540 }
541
542 struct ImageContext {
543     WPEView* view;
544     gpointer image;
545 };
546
547 void WPEView::handleExportedImage(gpointer image)
548 {
549     ImageContext* imageContext = g_slice_new(ImageContext);
550     imageContext->view = this;
551     imageContext->image = static_cast<gpointer>(image);
552     EGLImageKHR eglImage;
553 #if USE_DEPRECATED_FDO_EGL_IMAGE
554     eglImage = static_cast<EGLImageKHR>(image);
555 #else
556     eglImage = wpe_fdo_egl_exported_image_get_egl_image(static_cast<struct wpe_fdo_egl_exported_image*>(image));
557 #endif
558
559     auto* gstImage = gst_egl_image_new_wrapped(gst.context, eglImage, GST_GL_RGBA, imageContext, s_releaseImage);
560     {
561       GMutexHolder lock(images_mutex);
562
563       GST_TRACE("EGLImage %p wrapped in GstEGLImage %" GST_PTR_FORMAT, eglImage, gstImage);
564       gst_clear_mini_object ((GstMiniObject **) &egl.pending);
565       egl.pending = gstImage;
566
567       notifyLoadFinished();
568     }
569 }
570
571 #if ENABLE_SHM_BUFFER_SUPPORT
572 struct SHMBufferContext {
573     WPEView* view;
574     struct wpe_fdo_shm_exported_buffer* buffer;
575 };
576
577 void WPEView::releaseSHMBuffer(gpointer data)
578 {
579     SHMBufferContext* context = static_cast<SHMBufferContext*>(data);
580     s_view->dispatch([&]() {
581         auto* buffer = static_cast<struct wpe_fdo_shm_exported_buffer*>(context->buffer);
582         GST_TRACE("Dispatch release exported buffer %p", buffer);
583         wpe_view_backend_exportable_fdo_dispatch_release_shm_exported_buffer(wpe.exportable, buffer);
584     });
585 }
586
587 void WPEView::s_releaseSHMBuffer(gpointer data)
588 {
589     SHMBufferContext* context = static_cast<SHMBufferContext*>(data);
590     context->view->releaseSHMBuffer(data);
591     g_slice_free(SHMBufferContext, context);
592 }
593
594 void WPEView::handleExportedBuffer(struct wpe_fdo_shm_exported_buffer* buffer)
595 {
596     struct wl_shm_buffer* shmBuffer = wpe_fdo_shm_exported_buffer_get_shm_buffer(buffer);
597     auto format = wl_shm_buffer_get_format(shmBuffer);
598     if (format != WL_SHM_FORMAT_ARGB8888 && format != WL_SHM_FORMAT_XRGB8888) {
599         GST_ERROR("Unsupported pixel format: %d", format);
600         return;
601     }
602
603     int32_t width = wl_shm_buffer_get_width(shmBuffer);
604     int32_t height = wl_shm_buffer_get_height(shmBuffer);
605     gint stride = wl_shm_buffer_get_stride(shmBuffer);
606     gsize size = width * height * 4;
607     auto* data = static_cast<uint8_t*>(wl_shm_buffer_get_data(shmBuffer));
608
609     SHMBufferContext* bufferContext = g_slice_new(SHMBufferContext);
610     bufferContext->view = this;
611     bufferContext->buffer = buffer;
612
613     auto* gstBuffer = gst_buffer_new_wrapped_full(GST_MEMORY_FLAG_READONLY, data, size, 0, size, bufferContext, s_releaseSHMBuffer);
614     gsize offsets[1];
615     gint strides[1];
616     offsets[0] = 0;
617     strides[0] = stride;
618     gst_buffer_add_video_meta_full(gstBuffer, GST_VIDEO_FRAME_FLAG_NONE, GST_VIDEO_FORMAT_BGRA, width, height, 1, offsets, strides);
619
620     {
621         GMutexHolder lock(images_mutex);
622         GST_TRACE("SHM buffer %p wrapped in buffer %" GST_PTR_FORMAT, buffer, gstBuffer);
623         gst_clear_buffer (&shm.pending);
624         shm.pending = gstBuffer;
625         notifyLoadFinished();
626     }
627 }
628 #endif
629
630 struct wpe_view_backend_exportable_fdo_egl_client WPEView::s_exportableEGLClient = {
631 #if USE_DEPRECATED_FDO_EGL_IMAGE
632     // export_egl_image
633     [](void* data, EGLImageKHR image) {
634         auto& view = *static_cast<WPEView*>(data);
635         view.handleExportedImage(static_cast<gpointer>(image));
636     },
637     nullptr, nullptr,
638 #else
639     // export_egl_image
640     nullptr,
641     [](void* data, struct wpe_fdo_egl_exported_image* image) {
642         auto& view = *static_cast<WPEView*>(data);
643         view.handleExportedImage(static_cast<gpointer>(image));
644     },
645     nullptr,
646 #endif // USE_DEPRECATED_FDO_EGL_IMAGE
647     // padding
648     nullptr, nullptr
649 };
650
651 #if ENABLE_SHM_BUFFER_SUPPORT
652 struct wpe_view_backend_exportable_fdo_client WPEView::s_exportableClient = {
653     nullptr,
654     nullptr,
655     // export_shm_buffer
656     [](void* data, struct wpe_fdo_shm_exported_buffer* buffer) {
657         auto& view = *static_cast<WPEView*>(data);
658         view.handleExportedBuffer(buffer);
659     },
660     nullptr,
661     nullptr,
662 };
663 #endif
664
665 void WPEView::s_releaseImage(GstEGLImage* image, gpointer data)
666 {
667     ImageContext* context = static_cast<ImageContext*>(data);
668     context->view->releaseImage(context->image);
669     g_slice_free(ImageContext, context);
670 }
671
672 struct wpe_view_backend* WPEView::backend() const
673 {
674     return wpe.exportable ? wpe_view_backend_exportable_fdo_get_view_backend(wpe.exportable) : nullptr;
675 }
676
677 void WPEView::dispatchKeyboardEvent(struct wpe_input_keyboard_event& wpe_event)
678 {
679     s_view->dispatch([&]() {
680         wpe_view_backend_dispatch_keyboard_event(backend(), &wpe_event);
681     });
682 }
683
684 void WPEView::dispatchPointerEvent(struct wpe_input_pointer_event& wpe_event)
685 {
686     s_view->dispatch([&]() {
687         wpe_view_backend_dispatch_pointer_event(backend(), &wpe_event);
688     });
689 }
690
691 void WPEView::dispatchAxisEvent(struct wpe_input_axis_event& wpe_event)
692 {
693     s_view->dispatch([&]() {
694         wpe_view_backend_dispatch_axis_event(backend(), &wpe_event);
695     });
696 }