d3d11videosink: Fix tearing in case of fullscreen mode
[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
116   GstVideoOrientationMethod method;
117 };
118
119 struct _GstD3D11WindowClass
120 {
121   GstObjectClass object_class;
122
123   void          (*show)                   (GstD3D11Window * window);
124
125   void          (*update_swap_chain)      (GstD3D11Window * window);
126
127   void          (*change_fullscreen_mode) (GstD3D11Window * window);
128
129   gboolean      (*create_swap_chain)      (GstD3D11Window * window,
130                                            DXGI_FORMAT format,
131                                            guint width,
132                                            guint height,
133                                            guint swapchain_flags,
134                                            IDXGISwapChain ** swap_chain);
135
136   GstFlowReturn (*present)                (GstD3D11Window * window,
137                                            guint present_flags);
138
139   gboolean      (*unlock)                 (GstD3D11Window * window);
140
141   gboolean      (*unlock_stop)            (GstD3D11Window * window);
142
143   void          (*on_resize)              (GstD3D11Window * window,
144                                            guint width,
145                                            guint height);
146
147   GstFlowReturn (*prepare)                (GstD3D11Window * window,
148                                            guint display_width,
149                                            guint display_height,
150                                            GstCaps * caps,
151                                            GstStructure * config,
152                                            DXGI_FORMAT display_format,
153                                            GError ** error);
154
155   void          (*unprepare)              (GstD3D11Window * window);
156
157   gboolean      (*open_shared_handle)     (GstD3D11Window * window,
158                                            GstD3D11WindowSharedHandleData * data);
159
160   gboolean      (*release_shared_handle)  (GstD3D11Window * window,
161                                            GstD3D11WindowSharedHandleData * data);
162
163   void          (*set_render_rectangle)   (GstD3D11Window * window,
164                                            const GstVideoRectangle * rect);
165
166   void          (*set_title)              (GstD3D11Window * window,
167                                            const gchar *title);
168 };
169
170 GType         gst_d3d11_window_get_type             (void);
171
172 void          gst_d3d11_window_show                 (GstD3D11Window * window);
173
174 void          gst_d3d11_window_set_render_rectangle (GstD3D11Window * window,
175                                                      const GstVideoRectangle * rect);
176
177 void          gst_d3d11_window_set_title            (GstD3D11Window * window,
178                                                      const gchar *title);
179
180 void          gst_d3d11_window_set_orientation      (GstD3D11Window * window,
181                                                      GstVideoOrientationMethod method);
182
183 GstFlowReturn gst_d3d11_window_prepare              (GstD3D11Window * window,
184                                                      guint display_width,
185                                                      guint display_height,
186                                                      GstCaps * caps,
187                                                      GstStructure * config,
188                                                      DXGI_FORMAT display_format,
189                                                      GError ** error);
190
191 GstFlowReturn gst_d3d11_window_render               (GstD3D11Window * window,
192                                                      GstBuffer * buffer);
193
194 GstFlowReturn gst_d3d11_window_render_on_shared_handle (GstD3D11Window * window,
195                                                         GstBuffer * buffer,
196                                                         HANDLE shared_handle,
197                                                         guint texture_misc_flags,
198                                                         guint64 acquire_key,
199                                                         guint64 release_key);
200
201 gboolean      gst_d3d11_window_unlock               (GstD3D11Window * window);
202
203 gboolean      gst_d3d11_window_unlock_stop          (GstD3D11Window * window);
204
205 void          gst_d3d11_window_unprepare            (GstD3D11Window * window);
206
207 void          gst_d3d11_window_on_key_event         (GstD3D11Window * window,
208                                                      const gchar * event,
209                                                      const gchar * key);
210
211 void          gst_d3d11_window_on_mouse_event       (GstD3D11Window * window,
212                                                      const gchar * event,
213                                                      gint button,
214                                                      gdouble x,
215                                                      gdouble y);
216
217 /* utils */
218 GstD3D11WindowNativeType gst_d3d11_window_get_native_type_from_handle (guintptr handle);
219
220 const gchar *            gst_d3d11_window_get_native_type_to_string   (GstD3D11WindowNativeType type);
221
222 G_DEFINE_AUTOPTR_CLEANUP_FUNC(GstD3D11Window, gst_object_unref)
223
224 G_END_DECLS
225
226 #endif /* __GST_D3D11_WINDOW_H__ */