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