wpe: Properly wait on context thread startup condition
[platform/upstream/gstreamer.git] / ext / wpe / WPEThreadedView.h
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 #pragma once
21
22 #include <EGL/egl.h>
23 #include <glib.h>
24 #include <gst/gl/gstglfuncs.h>
25 #include <gst/gl/egl/gstgldisplay_egl.h>
26 #include <wpe/fdo.h>
27 #include <wpe/fdo-egl.h>
28 #include <wpe/webkit.h>
29 #include "gstwpevideosrc.h"
30
31 typedef struct _GstGLContext GstGLContext;
32 typedef struct _GstGLDisplay GstGLDisplay;
33 typedef struct _GstEGLImage GstEGLImage;
34
35 class WPEView {
36 public:
37     WPEView(WebKitWebContext*, GstWpeVideoSrc*, GstGLContext*, GstGLDisplay*, int width, int height);
38     ~WPEView();
39
40     bool operator!() const { return m_isValid; }
41
42     /*  Used by wpevideosrc */
43     void resize(int width, int height);
44     void loadUri(const gchar*);
45     void loadData(GBytes*);
46     void setDrawBackground(gboolean);
47
48     GstEGLImage* image();
49     GstBuffer* buffer();
50
51     void dispatchKeyboardEvent(struct wpe_input_keyboard_event&);
52     void dispatchPointerEvent(struct wpe_input_pointer_event&);
53     void dispatchAxisEvent(struct wpe_input_axis_event&);
54
55     /*  Used by WPEContextThread */
56     bool hasUri() const { return webkit.uri; }
57     void disconnectLoadFailedSignal();
58     void waitLoadCompletion();
59
60 protected:
61     void handleExportedImage(gpointer);
62     void handleExportedBuffer(struct wpe_fdo_shm_exported_buffer*);
63
64 private:
65     struct wpe_view_backend* backend() const;
66     void frameComplete();
67     void loadUriUnlocked(const gchar*);
68     void notifyLoadFinished();
69
70     void releaseImage(gpointer);
71     void releaseSHMBuffer(gpointer);
72     static void s_releaseSHMBuffer(gpointer);
73
74     struct {
75         GstGLContext* context;
76         GstGLDisplay* display;
77         GstGLDisplayEGL* display_egl;
78     } gst { nullptr, nullptr, nullptr };
79
80     static struct wpe_view_backend_exportable_fdo_egl_client s_exportableEGLClient;
81     static struct wpe_view_backend_exportable_fdo_client s_exportableClient;
82
83     static void s_releaseImage(GstEGLImage*, gpointer);
84     struct {
85         struct wpe_view_backend_exportable_fdo* exportable;
86         int width;
87         int height;
88     } wpe { nullptr, 0, 0, };
89
90     struct {
91         gchar* uri;
92         WebKitWebView* view;
93     } webkit = { nullptr, nullptr };
94
95     bool m_isValid { false };
96
97     struct {
98         GMutex ready_mutex;
99         GCond ready_cond;
100         gboolean ready;
101     } threading;
102
103     // This mutex guards access to either egl or shm resources declared below,
104     // depending on the runtime behavior.
105     GMutex images_mutex;
106
107     struct {
108         GstEGLImage* pending;
109         GstEGLImage* committed;
110     } egl { nullptr, nullptr };
111
112     struct {
113         GstBuffer* pending;
114         GstBuffer* committed;
115     } shm { nullptr, nullptr };
116
117     struct {
118         gulong init_ext_sigid;
119         gulong extension_msg_sigid;
120     } audio {0, 0};
121
122 };
123
124 class WPEContextThread {
125 public:
126     static WPEContextThread& singleton();
127
128     WPEContextThread();
129     ~WPEContextThread();
130
131     WPEView* createWPEView(GstWpeVideoSrc*, GstGLContext*, GstGLDisplay*, int width, int height);
132
133     template<typename Function>
134     void dispatch(Function);
135
136 private:
137     static gpointer s_viewThread(gpointer);
138     struct {
139         GMutex mutex;
140         GCond cond;
141         gboolean ready;
142         GThread* thread { nullptr };
143     } threading;
144
145     struct {
146         GMainContext* context;
147         GMainLoop* loop;
148         WebKitWebContext* web_context;
149     } glib { nullptr, nullptr, nullptr };
150 };