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