d3d11videosink: Fix deadlock when parent window is busy
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-bad / sys / d3d11 / gstd3d11window.h
1 /*
2  * GStreamer
3  * Copyright (C) 2012 Matthew Waters <ystreet00@gmail.com>
4  * Copyright (C) 2019 Seungha Yang <seungha.yang@navercorp.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21
22 #ifndef __GST_D3D11_WINDOW_H__
23 #define __GST_D3D11_WINDOW_H__
24
25 #include <gst/gst.h>
26 #include <gst/video/video.h>
27 #include <gst/d3d11/gstd3d11.h>
28 #include "gstd3d11overlaycompositor.h"
29 #include "gstd3d11pluginutils.h"
30
31 G_BEGIN_DECLS
32
33 #define GST_TYPE_D3D11_WINDOW             (gst_d3d11_window_get_type())
34 #define GST_D3D11_WINDOW(obj)             (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_D3D11_WINDOW, GstD3D11Window))
35 #define GST_D3D11_WINDOW_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST((klass), GST_TYPE_D3D11_WINDOW, GstD3D11WindowClass))
36 #define GST_IS_D3D11_WINDOW(obj)          (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_D3D11_WINDOW))
37 #define GST_IS_D3D11_WINDOW_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE((klass), GST_TYPE_D3D11_WINDOW))
38 #define GST_D3D11_WINDOW_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS((obj), GST_TYPE_D3D11_WINDOW, GstD3D11WindowClass))
39 #define GST_D3D11_WINDOW_TOGGLE_MODE_GET_TYPE (gst_d3d11_window_fullscreen_toggle_mode_type())
40
41 typedef struct _GstD3D11Window        GstD3D11Window;
42 typedef struct _GstD3D11WindowClass   GstD3D11WindowClass;
43
44 #define GST_D3D11_WINDOW_FLOW_CLOSED GST_FLOW_CUSTOM_ERROR
45
46 typedef enum
47 {
48   GST_D3D11_WINDOW_FULLSCREEN_TOGGLE_MODE_NONE = 0,
49   GST_D3D11_WINDOW_FULLSCREEN_TOGGLE_MODE_ALT_ENTER = (1 << 1),
50   GST_D3D11_WINDOW_FULLSCREEN_TOGGLE_MODE_PROPERTY = (1 << 2),
51 } GstD3D11WindowFullscreenToggleMode;
52
53 GType gst_d3d11_window_fullscreen_toggle_mode_type (void);
54
55 typedef enum
56 {
57   GST_D3D11_WINDOW_NATIVE_TYPE_NONE = 0,
58   GST_D3D11_WINDOW_NATIVE_TYPE_HWND,
59   GST_D3D11_WINDOW_NATIVE_TYPE_CORE_WINDOW,
60   GST_D3D11_WINDOW_NATIVE_TYPE_SWAP_CHAIN_PANEL,
61 } GstD3D11WindowNativeType;
62
63 typedef struct
64 {
65   HANDLE shared_handle;
66   guint texture_misc_flags;
67   guint64 acquire_key;
68   guint64 release_key;
69
70   GstBuffer *render_target;
71   IDXGIKeyedMutex *keyed_mutex;
72 } GstD3D11WindowSharedHandleData;
73
74 struct _GstD3D11Window
75 {
76   GstObject parent;
77
78   /*< protected >*/
79   gboolean initialized;
80   GstD3D11Device *device;
81   guintptr external_handle;
82
83   /* properties */
84   gboolean force_aspect_ratio;
85   gboolean enable_navigation_events;
86   GstD3D11WindowFullscreenToggleMode fullscreen_toggle_mode;
87   gboolean requested_fullscreen;
88   gboolean fullscreen;
89   gboolean emit_present;
90
91   GstVideoInfo info;
92   GstVideoInfo render_info;
93   GstD3D11Converter *converter;
94   GstD3D11OverlayCompositor *compositor;
95
96   /* calculated rect with aspect ratio and window area */
97   RECT render_rect;
98
99   /* input resolution */
100   RECT input_rect;
101   RECT prev_input_rect;
102
103   /* requested rect via gst_d3d11_window_render */
104   GstVideoRectangle rect;
105
106   guint surface_width;
107   guint surface_height;
108
109   IDXGISwapChain *swap_chain;
110   GstBuffer *backbuffer;
111   DXGI_FORMAT dxgi_format;
112
113   GstBuffer *cached_buffer;
114   gboolean first_present;
115   gboolean allow_tearing;
116
117   GstVideoOrientationMethod method;
118 };
119
120 struct _GstD3D11WindowClass
121 {
122   GstObjectClass object_class;
123
124   void          (*show)                   (GstD3D11Window * window);
125
126   void          (*update_swap_chain)      (GstD3D11Window * window);
127
128   void          (*change_fullscreen_mode) (GstD3D11Window * window);
129
130   gboolean      (*create_swap_chain)      (GstD3D11Window * window,
131                                            DXGI_FORMAT format,
132                                            guint width,
133                                            guint height,
134                                            guint swapchain_flags,
135                                            IDXGISwapChain ** swap_chain);
136
137   GstFlowReturn (*present)                (GstD3D11Window * window,
138                                            guint present_flags);
139
140   gboolean      (*unlock)                 (GstD3D11Window * window);
141
142   gboolean      (*unlock_stop)            (GstD3D11Window * window);
143
144   void          (*on_resize)              (GstD3D11Window * window,
145                                            guint width,
146                                            guint height);
147
148   GstFlowReturn (*prepare)                (GstD3D11Window * window,
149                                            guint display_width,
150                                            guint display_height,
151                                            GstCaps * caps,
152                                            GstStructure * config,
153                                            DXGI_FORMAT display_format,
154                                            GError ** error);
155
156   void          (*unprepare)              (GstD3D11Window * window);
157
158   gboolean      (*open_shared_handle)     (GstD3D11Window * window,
159                                            GstD3D11WindowSharedHandleData * data);
160
161   gboolean      (*release_shared_handle)  (GstD3D11Window * window,
162                                            GstD3D11WindowSharedHandleData * data);
163
164   void          (*set_render_rectangle)   (GstD3D11Window * window,
165                                            const GstVideoRectangle * rect);
166
167   void          (*set_title)              (GstD3D11Window * window,
168                                            const gchar *title);
169 };
170
171 GType         gst_d3d11_window_get_type             (void);
172
173 void          gst_d3d11_window_show                 (GstD3D11Window * window);
174
175 void          gst_d3d11_window_set_render_rectangle (GstD3D11Window * window,
176                                                      const GstVideoRectangle * rect);
177
178 void          gst_d3d11_window_set_title            (GstD3D11Window * window,
179                                                      const gchar *title);
180
181 void          gst_d3d11_window_set_orientation      (GstD3D11Window * window,
182                                                      GstVideoOrientationMethod method);
183
184 GstFlowReturn gst_d3d11_window_prepare              (GstD3D11Window * window,
185                                                      guint display_width,
186                                                      guint display_height,
187                                                      GstCaps * caps,
188                                                      GstStructure * config,
189                                                      DXGI_FORMAT display_format,
190                                                      GError ** error);
191
192 GstFlowReturn gst_d3d11_window_render               (GstD3D11Window * window,
193                                                      GstBuffer * buffer);
194
195 GstFlowReturn gst_d3d11_window_render_on_shared_handle (GstD3D11Window * window,
196                                                         GstBuffer * buffer,
197                                                         HANDLE shared_handle,
198                                                         guint texture_misc_flags,
199                                                         guint64 acquire_key,
200                                                         guint64 release_key);
201
202 gboolean      gst_d3d11_window_unlock               (GstD3D11Window * window);
203
204 gboolean      gst_d3d11_window_unlock_stop          (GstD3D11Window * window);
205
206 void          gst_d3d11_window_unprepare            (GstD3D11Window * window);
207
208 void          gst_d3d11_window_on_key_event         (GstD3D11Window * window,
209                                                      const gchar * event,
210                                                      const gchar * key);
211
212 void          gst_d3d11_window_on_mouse_event       (GstD3D11Window * window,
213                                                      const gchar * event,
214                                                      gint button,
215                                                      gdouble x,
216                                                      gdouble y);
217
218 /* utils */
219 GstD3D11WindowNativeType gst_d3d11_window_get_native_type_from_handle (guintptr handle);
220
221 const gchar *            gst_d3d11_window_get_native_type_to_string   (GstD3D11WindowNativeType type);
222
223 G_DEFINE_AUTOPTR_CLEANUP_FUNC(GstD3D11Window, gst_object_unref)
224
225 G_END_DECLS
226
227 #endif /* __GST_D3D11_WINDOW_H__ */