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