wpe: Rewrite wpesrc as a glbasesrc subclass
[platform/upstream/gstreamer.git] / ext / wpe / WPEThreadedView.cpp
1 /* Copyright (C) <2018> 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
30 #include <cstdio>
31 #include <mutex>
32
33 GST_DEBUG_CATEGORY_EXTERN (wpe_src_debug);
34 #define GST_CAT_DEFAULT wpe_src_debug
35
36 #if defined(WPE_FDO_CHECK_VERSION) && WPE_FDO_CHECK_VERSION(1, 3, 0)
37 #define USE_DEPRECATED_FDO_EGL_IMAGE 0
38 #define WPE_GLIB_SOURCE_PRIORITY G_PRIORITY_DEFAULT
39 #else
40 #define USE_DEPRECATED_FDO_EGL_IMAGE 1
41 #define WPE_GLIB_SOURCE_PRIORITY -70
42 #endif
43
44 class GMutexHolder {
45 public:
46     GMutexHolder(GMutex& mutex)
47         : m(mutex)
48     {
49         g_mutex_lock(&m);
50     }
51     ~GMutexHolder()
52     {
53         g_mutex_unlock(&m);
54     }
55
56 private:
57     GMutex& m;
58 };
59
60 WPEThreadedView::WPEThreadedView()
61 {
62     g_mutex_init(&threading.mutex);
63     g_cond_init(&threading.cond);
64     g_mutex_init(&threading.ready_mutex);
65     g_cond_init(&threading.ready_cond);
66
67     g_mutex_init(&images.mutex);
68
69     {
70         GMutexHolder lock(threading.mutex);
71         threading.thread = g_thread_new("WPEThreadedView",
72             s_viewThread, this);
73         g_cond_wait(&threading.cond, &threading.mutex);
74         GST_DEBUG("thread spawned");
75     }
76 }
77
78 WPEThreadedView::~WPEThreadedView()
79 {
80     {
81         GMutexHolder lock(images.mutex);
82
83         if (images.pending) {
84             gst_egl_image_unref(images.pending);
85             images.pending = nullptr;
86         }
87         if (images.committed) {
88             gst_egl_image_unref(images.committed);
89             images.committed = nullptr;
90         }
91     }
92
93     {
94         GMutexHolder lock(threading.mutex);
95         wpe_view_backend_exportable_fdo_destroy(wpe.exportable);
96     }
97
98     if (gst.display) {
99         gst_object_unref(gst.display);
100         gst.display = nullptr;
101     }
102
103     if (gst.context) {
104         gst_object_unref(gst.context);
105         gst.context = nullptr;
106     }
107
108     if (threading.thread) {
109         g_thread_unref(threading.thread);
110         threading.thread = nullptr;
111     }
112
113     g_mutex_clear(&threading.mutex);
114     g_cond_clear(&threading.cond);
115     g_mutex_clear(&threading.ready_mutex);
116     g_cond_clear(&threading.ready_cond);
117     g_mutex_clear(&images.mutex);
118 }
119
120 gpointer WPEThreadedView::s_viewThread(gpointer data)
121 {
122     auto& view = *static_cast<WPEThreadedView*>(data);
123
124     view.glib.context = g_main_context_new();
125     view.glib.loop = g_main_loop_new(view.glib.context, FALSE);
126
127     g_main_context_push_thread_default(view.glib.context);
128
129     {
130         GSource* source = g_idle_source_new();
131         g_source_set_callback(source,
132             [](gpointer data) -> gboolean {
133                 auto& view = *static_cast<WPEThreadedView*>(data);
134                 GMutexHolder lock(view.threading.mutex);
135                 g_cond_signal(&view.threading.cond);
136                 return G_SOURCE_REMOVE;
137             },
138             &view, nullptr);
139         g_source_attach(source, view.glib.context);
140         g_source_unref(source);
141     }
142
143     g_main_loop_run(view.glib.loop);
144
145     g_main_loop_unref(view.glib.loop);
146     view.glib.loop = nullptr;
147
148     if (view.webkit.view) {
149         g_object_unref(view.webkit.view);
150         view.webkit.view = nullptr;
151     }
152     if (view.webkit.uri) {
153         g_free(view.webkit.uri);
154         view.webkit.uri = nullptr;
155     }
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 struct wpe_view_backend* WPEThreadedView::backend() const
164 {
165     return wpe.exportable ? wpe_view_backend_exportable_fdo_get_view_backend(wpe.exportable) : nullptr;
166 }
167
168 void WPEThreadedView::s_loadEvent(WebKitWebView*, WebKitLoadEvent event, gpointer data)
169 {
170     if (event == WEBKIT_LOAD_COMMITTED) {
171         auto& view = *static_cast<WPEThreadedView*>(data);
172         GMutexHolder lock(view.threading.ready_mutex);
173         g_cond_signal(&view.threading.ready_cond);
174     }
175 }
176
177 bool WPEThreadedView::initialize(GstWpeSrc* src, GstGLContext* context, GstGLDisplay* display, int width, int height)
178 {
179     GST_DEBUG("context %p display %p, size (%d,%d)", context, display, width, height);
180
181     static std::once_flag s_loaderFlag;
182     std::call_once(s_loaderFlag,
183         [] {
184 #if defined(WPE_BACKEND_CHECK_VERSION) && WPE_BACKEND_CHECK_VERSION(1, 2, 0)
185             wpe_loader_init("libWPEBackend-fdo-1.0.so");
186 #endif
187         });
188
189     EGLDisplay eglDisplay = gst_gl_display_egl_get_from_native(GST_GL_DISPLAY_TYPE_WAYLAND,
190         gst_gl_display_get_handle(display));
191     GST_DEBUG("eglDisplay %p", eglDisplay);
192
193     struct InitializeContext {
194         GstWpeSrc* src;
195         WPEThreadedView& view;
196         GstGLContext* context;
197         GstGLDisplay* display;
198         EGLDisplay eglDisplay;
199         int width;
200         int height;
201       bool result;
202     } initializeContext { src, *this, context, display, eglDisplay, width, height, FALSE };
203
204     GSource* source = g_idle_source_new();
205     g_source_set_callback(source,
206         [](gpointer data) -> gboolean {
207             GST_DEBUG("on view thread");
208             auto& initializeContext = *static_cast<InitializeContext*>(data);
209             auto& view = initializeContext.view;
210
211             GMutexHolder lock(view.threading.mutex);
212
213             view.gst.context = GST_GL_CONTEXT(gst_object_ref(initializeContext.context));
214             view.gst.display = GST_GL_DISPLAY(gst_object_ref(initializeContext.display));
215
216             view.wpe.width = initializeContext.width;
217             view.wpe.height = initializeContext.height;
218
219             initializeContext.result = wpe_fdo_initialize_for_egl_display(initializeContext.eglDisplay);
220             GST_DEBUG("FDO EGL display initialisation result: %d", initializeContext.result);
221             if (!initializeContext.result) {
222               g_cond_signal(&view.threading.cond);
223               return G_SOURCE_REMOVE;
224             }
225
226             view.wpe.exportable = wpe_view_backend_exportable_fdo_egl_create(&s_exportableClient,
227                 &view, view.wpe.width, view.wpe.height);
228             auto* wpeViewBackend = wpe_view_backend_exportable_fdo_get_view_backend(view.wpe.exportable);
229             auto* viewBackend = webkit_web_view_backend_new(wpeViewBackend, nullptr, nullptr);
230 #if defined(WPE_BACKEND_CHECK_VERSION) && WPE_BACKEND_CHECK_VERSION(1, 1, 0)
231             wpe_view_backend_add_activity_state(wpeViewBackend, wpe_view_activity_state_visible | wpe_view_activity_state_focused | wpe_view_activity_state_in_window);
232 #endif
233
234             view.webkit.view = WEBKIT_WEB_VIEW(g_object_new(WEBKIT_TYPE_WEB_VIEW,
235                 "backend", viewBackend, nullptr));
236
237             gst_wpe_src_configure_web_view(initializeContext.src, view.webkit.view);
238
239             g_signal_connect(view.webkit.view, "load-changed", G_CALLBACK(s_loadEvent), &view);
240
241             const gchar* location;
242             gboolean drawBackground = TRUE;
243             g_object_get(initializeContext.src, "location", &location, "draw-background", &drawBackground, nullptr);
244             if (!location)
245                 g_warning("Invalid location");
246             else {
247                 view.setDrawBackground(drawBackground);
248                 view.loadUriUnlocked(location);
249             }
250             g_cond_signal(&view.threading.cond);
251             return G_SOURCE_REMOVE;
252         },
253         &initializeContext, nullptr);
254     g_source_set_priority(source, WPE_GLIB_SOURCE_PRIORITY);
255
256     {
257         GMutexHolder lock(threading.mutex);
258         g_source_attach(source, glib.context);
259         g_cond_wait(&threading.cond, &threading.mutex);
260     }
261
262     g_source_unref(source);
263
264     if (initializeContext.result) {
265         GST_DEBUG("waiting load to finish");
266         GMutexHolder lock(threading.ready_mutex);
267         g_cond_wait(&threading.ready_cond, &threading.ready_mutex);
268         GST_DEBUG("done");
269     }
270     return initializeContext.result;
271 }
272
273 GstEGLImage* WPEThreadedView::image()
274 {
275     GstEGLImage* ret = nullptr;
276     GMutexHolder lock(images.mutex);
277
278     GST_TRACE("pending %" GST_PTR_FORMAT " (%d) committed %" GST_PTR_FORMAT " (%d)", images.pending,
279         GST_IS_EGL_IMAGE(images.pending) ? GST_MINI_OBJECT_REFCOUNT_VALUE(GST_MINI_OBJECT_CAST(images.pending)) : 0,
280         images.committed,
281         GST_IS_EGL_IMAGE(images.committed) ? GST_MINI_OBJECT_REFCOUNT_VALUE(GST_MINI_OBJECT_CAST(images.committed)) : 0);
282
283     if (images.pending) {
284         auto* previousImage = images.committed;
285         images.committed = images.pending;
286         images.pending = nullptr;
287
288         if (previousImage)
289             gst_egl_image_unref(previousImage);
290     }
291
292     if (images.committed) {
293         ret = images.committed;
294         frameComplete();
295     }
296
297     return ret;
298 }
299
300 void WPEThreadedView::resize(int width, int height)
301 {
302     GST_DEBUG("resize to %dx%d", width, height);
303     wpe.width = width;
304     wpe.height = height;
305
306     GSource* source = g_idle_source_new();
307     g_source_set_callback(source,
308         [](gpointer data) -> gboolean {
309             auto& view = *static_cast<WPEThreadedView*>(data);
310             GMutexHolder lock(view.threading.mutex);
311
312             GST_DEBUG("dispatching");
313             if (view.wpe.exportable && wpe_view_backend_exportable_fdo_get_view_backend(view.wpe.exportable))
314                 wpe_view_backend_dispatch_set_size(wpe_view_backend_exportable_fdo_get_view_backend(view.wpe.exportable), view.wpe.width, view.wpe.height);
315
316             g_cond_signal(&view.threading.cond);
317             return G_SOURCE_REMOVE;
318         },
319         this, nullptr);
320     g_source_set_priority(source, WPE_GLIB_SOURCE_PRIORITY);
321
322     {
323         GMutexHolder lock(threading.mutex);
324         g_source_attach(source, glib.context);
325         g_cond_wait(&threading.cond, &threading.mutex);
326     }
327
328     g_source_unref(source);
329 }
330
331 void WPEThreadedView::frameComplete()
332 {
333     GST_TRACE("frame complete");
334
335     GSource* source = g_idle_source_new();
336     g_source_set_callback(source,
337         [](gpointer data) -> gboolean {
338             auto& view = *static_cast<WPEThreadedView*>(data);
339             GMutexHolder lock(view.threading.mutex);
340
341             GST_TRACE("dispatching");
342             wpe_view_backend_exportable_fdo_dispatch_frame_complete(view.wpe.exportable);
343
344             g_cond_signal(&view.threading.cond);
345             return G_SOURCE_REMOVE;
346         },
347         this, nullptr);
348     g_source_set_priority(source, WPE_GLIB_SOURCE_PRIORITY);
349
350     {
351         GMutexHolder lock(threading.mutex);
352         g_source_attach(source, glib.context);
353         g_cond_wait(&threading.cond, &threading.mutex);
354     }
355
356     g_source_unref(source);
357 }
358
359 void WPEThreadedView::loadUriUnlocked(const gchar* uri)
360 {
361     if (webkit.uri)
362         g_free(webkit.uri);
363
364     GST_DEBUG("loading %s", uri);
365     webkit.uri = g_strdup(uri);
366     webkit_web_view_load_uri(webkit.view, webkit.uri);
367 }
368
369 void WPEThreadedView::loadUri(const gchar* uri)
370 {
371     struct UriContext {
372         WPEThreadedView& view;
373         const gchar* uri;
374     } uriContext { *this, uri };
375
376     GSource* source = g_idle_source_new();
377     g_source_set_callback(source,
378         [](gpointer data) -> gboolean {
379             GST_DEBUG("on view thread");
380             auto& uriContext = *static_cast<UriContext*>(data);
381             auto& view = uriContext.view;
382             GMutexHolder lock(view.threading.mutex);
383
384             view.loadUriUnlocked(uriContext.uri);
385
386             g_cond_signal(&view.threading.cond);
387             return G_SOURCE_REMOVE;
388         },
389         &uriContext, nullptr);
390     g_source_set_priority(source, WPE_GLIB_SOURCE_PRIORITY);
391
392     {
393         GMutexHolder lock(threading.mutex);
394         g_source_attach(source, glib.context);
395         g_cond_wait(&threading.cond, &threading.mutex);
396         GST_DEBUG("done");
397     }
398
399     g_source_unref(source);
400 }
401
402 void WPEThreadedView::setDrawBackground(gboolean drawsBackground)
403 {
404 #if WEBKIT_CHECK_VERSION(2, 24, 0)
405     GST_DEBUG("%s background rendering", drawsBackground ? "Enabling" : "Disabling");
406     WebKitColor color;
407     webkit_color_parse(&color, drawsBackground ? "white" : "transparent");
408     webkit_web_view_set_background_color(webkit.view, &color);
409 #else
410     GST_FIXME("webkit_web_view_set_background_color is not implemented in WPE %u.%u. Please upgrade to 2.24", webkit_get_major_version(), webkit_get_minor_version());
411 #endif
412 }
413
414 void WPEThreadedView::releaseImage(gpointer imagePointer)
415 {
416     struct ReleaseImageContext {
417         WPEThreadedView& view;
418         gpointer imagePointer;
419     } releaseImageContext{ *this, imagePointer };
420
421     GSource* source = g_idle_source_new();
422     g_source_set_callback(source,
423         [](gpointer data) -> gboolean {
424             auto& releaseImageContext = *static_cast<ReleaseImageContext*>(data);
425             auto& view = releaseImageContext.view;
426             GMutexHolder lock(view.threading.mutex);
427
428             GST_TRACE("Dispatch release exported image %p", releaseImageContext.imagePointer);
429 #if USE_DEPRECATED_FDO_EGL_IMAGE
430             wpe_view_backend_exportable_fdo_egl_dispatch_release_image(releaseImageContext.view.wpe.exportable,
431                 static_cast<EGLImageKHR>(releaseImageContext.imagePointer));
432 #else
433             wpe_view_backend_exportable_fdo_egl_dispatch_release_exported_image(releaseImageContext.view.wpe.exportable,
434                 static_cast<struct wpe_fdo_egl_exported_image*>(releaseImageContext.imagePointer));
435 #endif
436             g_cond_signal(&view.threading.cond);
437             return G_SOURCE_REMOVE;
438         },
439         &releaseImageContext, nullptr);
440     g_source_set_priority(source, WPE_GLIB_SOURCE_PRIORITY);
441
442     {
443         GMutexHolder lock(threading.mutex);
444         g_source_attach(source, glib.context);
445         g_cond_wait(&threading.cond, &threading.mutex);
446     }
447
448     g_source_unref(source);
449 }
450
451 struct ImageContext {
452     WPEThreadedView* view;
453     gpointer image;
454 };
455
456 void WPEThreadedView::handleExportedImage(gpointer image)
457 {
458     ImageContext* imageContext = g_slice_new(ImageContext);
459     imageContext->view = this;
460     imageContext->image = static_cast<gpointer>(image);
461     EGLImageKHR eglImage;
462 #if USE_DEPRECATED_FDO_EGL_IMAGE
463     eglImage = static_cast<EGLImageKHR>(image);
464 #else
465     eglImage = wpe_fdo_egl_exported_image_get_egl_image(static_cast<struct wpe_fdo_egl_exported_image*>(image));
466 #endif
467
468     auto* gstImage = gst_egl_image_new_wrapped(gst.context, eglImage, GST_GL_RGBA, imageContext, s_releaseImage);
469     {
470       GMutexHolder lock(images.mutex);
471
472       GST_TRACE("EGLImage %p wrapped in GstEGLImage %" GST_PTR_FORMAT, eglImage, gstImage);
473       images.pending = gstImage;
474     }
475 }
476
477 struct wpe_view_backend_exportable_fdo_egl_client WPEThreadedView::s_exportableClient = {
478 #if USE_DEPRECATED_FDO_EGL_IMAGE
479     // export_egl_image
480     [](void* data, EGLImageKHR image) {
481         auto& view = *static_cast<WPEThreadedView*>(data);
482         view.handleExportedImage(static_cast<gpointer>(image));
483     },
484     nullptr,
485 #else
486     // export_egl_image
487     nullptr,
488     [](void* data, struct wpe_fdo_egl_exported_image* image) {
489         auto& view = *static_cast<WPEThreadedView*>(data);
490         view.handleExportedImage(static_cast<gpointer>(image));
491     },
492 #endif
493     // padding
494     nullptr, nullptr, nullptr
495 };
496
497 void WPEThreadedView::s_releaseImage(GstEGLImage* image, gpointer data)
498 {
499     ImageContext* context = static_cast<ImageContext*>(data);
500     context->view->releaseImage(context->image);
501     g_slice_free(ImageContext, context);
502 }