• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * GStreamer
3  * Copyright (C) 2008 Julien Isorce <julien.isorce@gmail.com>
4  * Copyright (C) 2012 Matthew Waters <ystreet00@gmail.com>
5  * Copyright (C) 2019 Seungha Yang <seungha.yang@navercorp.com>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22 
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 
27 #include "gstvkwindow_win32.h"
28 
29 LRESULT CALLBACK window_proc (HWND hWnd, UINT uMsg, WPARAM wParam,
30     LPARAM lParam);
31 LRESULT FAR PASCAL sub_class_proc (HWND hWnd, UINT uMsg, WPARAM wParam,
32     LPARAM lParam);
33 
34 enum
35 {
36   PROP_0
37 };
38 
39 struct _GstVulkanWindowWin32Private
40 {
41   GIOChannel *msg_io_channel;
42 
43   gint preferred_width;
44   gint preferred_height;
45 };
46 
47 #define GST_CAT_DEFAULT gst_vulkan_window_win32_debug
48 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
49 
50 #define DEBUG_INIT \
51   GST_DEBUG_CATEGORY_GET (GST_CAT_DEFAULT, "vulkanwindow");
52 #define gst_vulkan_window_win32_parent_class parent_class
53 G_DEFINE_TYPE_WITH_CODE (GstVulkanWindowWin32, gst_vulkan_window_win32,
54     GST_TYPE_VULKAN_WINDOW, G_ADD_PRIVATE (GstVulkanWindowWin32) DEBUG_INIT);
55 
56 #define GET_PRIV(window) gst_vulkan_window_win32_get_instance_private (window)
57 
58 static void gst_vulkan_window_win32_set_window_handle (GstVulkanWindow * window,
59     guintptr handle);
60 static VkSurfaceKHR
61 gst_vulkan_window_win32_get_surface (GstVulkanWindow * window, GError ** error);
62 static gboolean
63 gst_vulkan_window_win32_get_presentation_support (GstVulkanWindow * window,
64     GstVulkanDevice * device, guint32 queue_family_idx);
65 static gboolean gst_vulkan_window_win32_open (GstVulkanWindow * window,
66     GError ** error);
67 static void gst_vulkan_window_win32_close (GstVulkanWindow * window);
68 static void release_parent_win_id (GstVulkanWindowWin32 * window_win32);
69 static void gst_vulkan_window_win32_show (GstVulkanWindowWin32 * window);
70 
71 static void
gst_vulkan_window_win32_class_init(GstVulkanWindowWin32Class * klass)72 gst_vulkan_window_win32_class_init (GstVulkanWindowWin32Class * klass)
73 {
74   GstVulkanWindowClass *window_class = (GstVulkanWindowClass *) klass;
75 
76   window_class->set_window_handle =
77       GST_DEBUG_FUNCPTR (gst_vulkan_window_win32_set_window_handle);
78   window_class->get_surface =
79       GST_DEBUG_FUNCPTR (gst_vulkan_window_win32_get_surface);
80   window_class->open = GST_DEBUG_FUNCPTR (gst_vulkan_window_win32_open);
81   window_class->close = GST_DEBUG_FUNCPTR (gst_vulkan_window_win32_close);
82   window_class->get_presentation_support =
83       GST_DEBUG_FUNCPTR (gst_vulkan_window_win32_get_presentation_support);
84 }
85 
86 static void
gst_vulkan_window_win32_init(GstVulkanWindowWin32 * window)87 gst_vulkan_window_win32_init (GstVulkanWindowWin32 * window)
88 {
89   GstVulkanWindowWin32Private *priv = GET_PRIV (window);
90 
91   priv->preferred_width = 320;
92   priv->preferred_height = 240;
93 }
94 
95 GstVulkanWindowWin32 *
gst_vulkan_window_win32_new(GstVulkanDisplay * display)96 gst_vulkan_window_win32_new (GstVulkanDisplay * display)
97 {
98   GstVulkanWindowWin32 *window;
99 
100   if ((gst_vulkan_display_get_handle_type (display) &
101           GST_VULKAN_DISPLAY_TYPE_WIN32) == 0)
102     /* we require an win32 display to create win32 windows */
103     return NULL;
104 
105   window = g_object_new (GST_TYPE_VULKAN_WINDOW_WIN32, NULL);
106   gst_object_ref_sink (window);
107 
108   return window;
109 }
110 
111 static gboolean
msg_cb(GIOChannel * source,GIOCondition condition,gpointer data)112 msg_cb (GIOChannel * source, GIOCondition condition, gpointer data)
113 {
114   MSG msg;
115 
116   if (!PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
117     return G_SOURCE_CONTINUE;
118 
119   GST_TRACE ("handle message");
120   TranslateMessage (&msg);
121   DispatchMessage (&msg);
122 
123   return G_SOURCE_CONTINUE;
124 }
125 
126 typedef struct
127 {
128   GstVulkanWindowWin32 *self;
129   GError **error;
130   gboolean fired;
131   gboolean ret;
132   GMutex lock;
133   GCond cond;
134 } CreateWindowData;
135 
136 static gboolean
_create_window(CreateWindowData * data)137 _create_window (CreateWindowData * data)
138 {
139   data->ret = gst_vulkan_window_win32_create_window (data->self, data->error);
140 
141   g_mutex_lock (&data->lock);
142   data->fired = TRUE;
143   g_cond_signal (&data->cond);
144   g_mutex_unlock (&data->lock);
145 
146   return G_SOURCE_REMOVE;
147 }
148 
149 static gboolean
gst_vulkan_window_win32_open(GstVulkanWindow * window,GError ** error)150 gst_vulkan_window_win32_open (GstVulkanWindow * window, GError ** error)
151 {
152   GstVulkanWindowWin32 *window_win32 = GST_VULKAN_WINDOW_WIN32 (window);
153   GstVulkanWindowWin32Private *priv = GET_PRIV (window_win32);
154   GstVulkanDisplay *display;
155   GMainContext *context;
156   CreateWindowData data = { 0, };
157 
158   if (!GST_VULKAN_WINDOW_CLASS (parent_class)->open (window, error))
159     return FALSE;
160 
161   display = gst_vulkan_window_get_display (window);
162   context = g_main_context_ref (display->main_context);
163   gst_object_unref (display);
164 
165   priv->msg_io_channel = g_io_channel_win32_new_messages (0);
166   window_win32->msg_source = g_io_create_watch (priv->msg_io_channel, G_IO_IN);
167   g_source_set_callback (window_win32->msg_source, (GSourceFunc) msg_cb, NULL,
168       NULL);
169   g_source_attach (window_win32->msg_source, context);
170 
171   data.self = window_win32;
172   data.error = error;
173   g_mutex_init (&data.lock);
174   g_cond_init (&data.cond);
175 
176   g_main_context_invoke (context, (GSourceFunc) _create_window, &data);
177   g_mutex_lock (&data.lock);
178   while (!data.fired)
179     g_cond_wait (&data.cond, &data.lock);
180   g_mutex_unlock (&data.lock);
181 
182   g_mutex_clear (&data.lock);
183   g_cond_clear (&data.cond);
184   g_main_context_unref (context);
185 
186   if (!data.ret)
187     return FALSE;
188 
189   gst_vulkan_window_win32_show (window_win32);
190 
191   return TRUE;
192 }
193 
194 static void
gst_vulkan_window_win32_close(GstVulkanWindow * window)195 gst_vulkan_window_win32_close (GstVulkanWindow * window)
196 {
197   GstVulkanWindowWin32 *window_win32 = GST_VULKAN_WINDOW_WIN32 (window);
198   GstVulkanWindowWin32Private *priv = GET_PRIV (window_win32);
199 
200   release_parent_win_id (window_win32);
201 
202   if (window_win32->internal_win_id) {
203     RemoveProp (window_win32->internal_win_id, "vulkan_window");
204     ShowWindow (window_win32->internal_win_id, SW_HIDE);
205     SetParent (window_win32->internal_win_id, NULL);
206     if (!DestroyWindow (window_win32->internal_win_id))
207       GST_WARNING ("failed to destroy window %" G_GUINTPTR_FORMAT
208           ", 0x%x", (guintptr) window_win32->internal_win_id,
209           (unsigned int) GetLastError ());
210   }
211 
212   g_source_destroy (window_win32->msg_source);
213   g_source_unref (window_win32->msg_source);
214   window_win32->msg_source = NULL;
215   g_io_channel_unref (priv->msg_io_channel);
216   priv->msg_io_channel = NULL;
217 
218   GST_VULKAN_WINDOW_CLASS (parent_class)->close (window);
219 }
220 
221 static void
set_parent_win_id(GstVulkanWindowWin32 * window_win32)222 set_parent_win_id (GstVulkanWindowWin32 * window_win32)
223 {
224   WNDPROC window_parent_proc;
225   RECT rect;
226 
227   if (!window_win32->parent_win_id) {
228     /* no parent so the internal window needs borders and system menu */
229     SetWindowLongPtr (window_win32->internal_win_id, GWL_STYLE,
230         WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW);
231     SetParent (window_win32->internal_win_id, NULL);
232     return;
233   }
234 
235   window_parent_proc =
236       (WNDPROC) GetWindowLongPtr (window_win32->parent_win_id, GWLP_WNDPROC);
237 
238   GST_DEBUG ("set parent %" G_GUINTPTR_FORMAT,
239       (guintptr) window_win32->parent_win_id);
240 
241   SetProp (window_win32->parent_win_id, "vulkan_window_id",
242       window_win32->internal_win_id);
243   SetProp (window_win32->parent_win_id, "vulkan_window_parent_proc",
244       (WNDPROC) window_parent_proc);
245   SetWindowLongPtr (window_win32->parent_win_id, GWLP_WNDPROC,
246       (LONG_PTR) sub_class_proc);
247 
248   SetWindowLongPtr (window_win32->internal_win_id, GWL_STYLE,
249       WS_CHILD | WS_MAXIMIZE);
250   SetParent (window_win32->internal_win_id, window_win32->parent_win_id);
251 
252   /* take changes into account: SWP_FRAMECHANGED */
253   GetClientRect (window_win32->parent_win_id, &rect);
254   SetWindowPos (window_win32->internal_win_id, HWND_TOP, rect.left, rect.top,
255       rect.right, rect.bottom,
256       SWP_ASYNCWINDOWPOS | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER |
257       SWP_FRAMECHANGED | SWP_NOACTIVATE);
258   MoveWindow (window_win32->internal_win_id, rect.left, rect.top, rect.right,
259       rect.bottom, FALSE);
260 }
261 
262 static void
release_parent_win_id(GstVulkanWindowWin32 * window_win32)263 release_parent_win_id (GstVulkanWindowWin32 * window_win32)
264 {
265   WNDPROC parent_proc;
266 
267   if (!window_win32->parent_win_id)
268     return;
269 
270   parent_proc =
271       GetProp (window_win32->parent_win_id, "vulkan_window_parent_proc");
272   if (!parent_proc)
273     return;
274 
275   GST_DEBUG ("release parent %" G_GUINTPTR_FORMAT,
276       (guintptr) window_win32->parent_win_id);
277 
278   SetWindowLongPtr (window_win32->parent_win_id, GWLP_WNDPROC,
279       (LONG_PTR) parent_proc);
280 
281   RemoveProp (window_win32->parent_win_id, "vulkan_window_parent_proc");
282 }
283 
284 static gboolean
gst_vulkan_window_win32_create_window(GstVulkanWindowWin32 * window_win32,GError ** error)285 gst_vulkan_window_win32_create_window (GstVulkanWindowWin32 * window_win32,
286     GError ** error)
287 {
288   WNDCLASSEX wc;
289   ATOM atom = 0;
290   HINSTANCE hinstance = GetModuleHandle (NULL);
291 
292   static gint x = 0;
293   static gint y = 0;
294 
295   GST_LOG ("Attempting to create a win32 window");
296 
297   x += 20;
298   y += 20;
299 
300   atom = GetClassInfoEx (hinstance, "GSTVULKAN", &wc);
301 
302   if (atom == 0) {
303     ZeroMemory (&wc, sizeof (WNDCLASSEX));
304 
305     wc.cbSize = sizeof (WNDCLASSEX);
306     wc.lpfnWndProc = window_proc;
307     wc.cbClsExtra = 0;
308     wc.cbWndExtra = 0;
309     wc.hInstance = hinstance;
310     wc.hIcon = LoadIcon (NULL, IDI_WINLOGO);
311     wc.hIconSm = NULL;
312     wc.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
313     wc.hCursor = LoadCursor (NULL, IDC_ARROW);
314     wc.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);
315     wc.lpszMenuName = NULL;
316     wc.lpszClassName = "GSTVULKAN";
317 
318     atom = RegisterClassEx (&wc);
319 
320     if (atom == 0) {
321       g_set_error (error, GST_VULKAN_WINDOW_ERROR,
322           GST_VULKAN_WINDOW_ERROR_RESOURCE_UNAVAILABLE,
323           "Failed to register window class 0x%x\n",
324           (unsigned int) GetLastError ());
325       goto failure;
326     }
327   }
328 
329   window_win32->internal_win_id = 0;
330   window_win32->device = 0;
331   window_win32->visible = FALSE;
332 
333   window_win32->internal_win_id = CreateWindowEx (0,
334       "GSTVULKAN",
335       "Vulkan renderer",
336       WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW,
337       x, y, 0, 0, (HWND) NULL, (HMENU) NULL, hinstance, window_win32);
338 
339   if (!window_win32->internal_win_id) {
340     g_set_error (error, GST_VULKAN_WINDOW_ERROR,
341         GST_VULKAN_WINDOW_ERROR_RESOURCE_UNAVAILABLE,
342         "failed to create vulkan window");
343     goto failure;
344   }
345 
346   GST_DEBUG ("vulkan window created: %" G_GUINTPTR_FORMAT,
347       (guintptr) window_win32->internal_win_id);
348 
349   /* device is set in the window_proc */
350   if (!window_win32->device) {
351     g_set_error (error, GST_VULKAN_WINDOW_ERROR,
352         GST_VULKAN_WINDOW_ERROR_RESOURCE_UNAVAILABLE,
353         "failed to create device");
354     goto failure;
355   }
356 
357   ShowCursor (TRUE);
358 
359   GST_LOG ("Created a win32 window");
360 
361   /* The window has been created as if it had no parent, so there is nothing
362    * else to do in that case. Even if user has already set a window,
363    * parent_win_id could still be 0 at this point, and in that case calling
364    * set_parent_win_id() here would steal focus from the parent window. */
365   if (window_win32->parent_win_id)
366     set_parent_win_id (window_win32);
367 
368   return TRUE;
369 
370 failure:
371   return FALSE;
372 }
373 
374 static VkSurfaceKHR
gst_vulkan_window_win32_get_surface(GstVulkanWindow * window,GError ** error)375 gst_vulkan_window_win32_get_surface (GstVulkanWindow * window, GError ** error)
376 {
377   GstVulkanWindowWin32 *window_win32 = GST_VULKAN_WINDOW_WIN32 (window);
378   VkWin32SurfaceCreateInfoKHR info = { 0, };
379   VkSurfaceKHR ret;
380   VkResult err;
381 
382   info.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
383   info.pNext = NULL;
384   info.flags = 0;
385   info.hinstance = GetModuleHandle (NULL);
386   info.hwnd = window_win32->parent_win_id ? window_win32->parent_win_id :
387       window_win32->internal_win_id;
388 
389   if (!window_win32->CreateWin32Surface)
390     window_win32->CreateWin32Surface =
391         gst_vulkan_instance_get_proc_address (window->display->instance,
392         "vkCreateWin32SurfaceKHR");
393   if (!window_win32->CreateWin32Surface) {
394     g_set_error_literal (error, GST_VULKAN_ERROR, VK_ERROR_FEATURE_NOT_PRESENT,
395         "Could not retrieve \"vkCreateWin32SurfaceKHR\" function pointer");
396     return VK_NULL_HANDLE;
397   }
398 
399   err =
400       window_win32->CreateWin32Surface (window->display->instance->instance,
401       &info, NULL, &ret);
402 
403   if (gst_vulkan_error_to_g_error (err, error, "vkCreateWin32SurfaceKHR") < 0)
404     return VK_NULL_HANDLE;
405 
406   return ret;
407 }
408 
409 static gboolean
gst_vulkan_window_win32_get_presentation_support(GstVulkanWindow * window,GstVulkanDevice * device,guint32 queue_family_idx)410 gst_vulkan_window_win32_get_presentation_support (GstVulkanWindow * window,
411     GstVulkanDevice * device, guint32 queue_family_idx)
412 {
413   GstVulkanWindowWin32 *window_win32 = GST_VULKAN_WINDOW_WIN32 (window);
414   VkPhysicalDevice gpu;
415 
416   if (!window_win32->GetPhysicalDeviceWin32PresentationSupport)
417     window_win32->GetPhysicalDeviceWin32PresentationSupport =
418         gst_vulkan_instance_get_proc_address (window->display->instance,
419         "vkGetPhysicalDeviceWin32PresentationSupportKHR");
420   if (!window_win32->GetPhysicalDeviceWin32PresentationSupport) {
421     GST_WARNING_OBJECT (window, "Could not retrieve "
422         "\"GetPhysicalDeviceWin32PresentationSupport\" " "function pointer");
423     return FALSE;
424   }
425 
426   gpu = gst_vulkan_device_get_physical_device (device);
427   if (window_win32->GetPhysicalDeviceWin32PresentationSupport (gpu,
428           queue_family_idx))
429     return TRUE;
430 
431   return FALSE;
432 }
433 
434 
435 static void
gst_vulkan_window_win32_set_window_handle(GstVulkanWindow * window,guintptr id)436 gst_vulkan_window_win32_set_window_handle (GstVulkanWindow * window,
437     guintptr id)
438 {
439   GstVulkanWindowWin32 *window_win32;
440 
441   window_win32 = GST_VULKAN_WINDOW_WIN32 (window);
442 
443   if (!window_win32->internal_win_id) {
444     window_win32->parent_win_id = (HWND) id;
445     return;
446   }
447 
448   if (window_win32->visible) {
449     ShowWindow (window_win32->internal_win_id, SW_HIDE);
450     window_win32->visible = FALSE;
451   }
452 
453   release_parent_win_id (window_win32);
454   window_win32->parent_win_id = (HWND) id;
455   set_parent_win_id (window_win32);
456 }
457 
458 static void
gst_vulkan_window_win32_show(GstVulkanWindowWin32 * window)459 gst_vulkan_window_win32_show (GstVulkanWindowWin32 * window)
460 {
461   GstVulkanWindowWin32Private *priv = GET_PRIV (window);
462   gint width = priv->preferred_width;
463   gint height = priv->preferred_height;
464 
465   if (!window->visible) {
466     HWND parent_id = window->parent_win_id;
467 
468     /* if no parent the real size has to be set now because this has not been done
469      * when at window creation */
470     if (!parent_id) {
471       RECT rect;
472       GetClientRect (window->internal_win_id, &rect);
473       width += 2 * GetSystemMetrics (SM_CXSIZEFRAME);
474       height +=
475           2 * GetSystemMetrics (SM_CYSIZEFRAME) +
476           GetSystemMetrics (SM_CYCAPTION);
477       MoveWindow (window->internal_win_id, rect.left, rect.top, width,
478           height, FALSE);
479     }
480 
481     ShowWindowAsync (window->internal_win_id, SW_SHOW);
482     window->visible = TRUE;
483   }
484 }
485 
486 /* PRIVATE */
487 
488 LRESULT CALLBACK
window_proc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)489 window_proc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
490 {
491   GstVulkanWindowWin32 *window_win32;
492   LRESULT ret = 0;
493 
494   if (uMsg == WM_CREATE) {
495     window_win32 =
496         GST_VULKAN_WINDOW_WIN32 (((LPCREATESTRUCT) lParam)->lpCreateParams);
497 
498     GST_TRACE ("WM_CREATE");
499 
500     window_win32->device = GetDC (hWnd);
501     /* Do this, otherwise we hang on exit. We can still use it (due to the
502      * CS_OWNDC flag in the WindowClass) after we have Released.
503      */
504     ReleaseDC (hWnd, window_win32->device);
505 
506     SetProp (hWnd, "vulkan_window", window_win32);
507   } else if (GetProp (hWnd, "vulkan_window")) {
508     GstVulkanWindow *window;
509 
510     window_win32 = GST_VULKAN_WINDOW_WIN32 (GetProp (hWnd, "vulkan_window"));
511     window = GST_VULKAN_WINDOW (window_win32);
512 
513     g_assert (window_win32->internal_win_id == hWnd);
514 
515     switch (uMsg) {
516       case WM_SIZE:
517         gst_vulkan_window_resize (window, LOWORD (lParam), HIWORD (lParam));
518         break;
519       case WM_PAINT:
520       {
521         PAINTSTRUCT ps;
522 
523         BeginPaint (hWnd, &ps);
524         gst_vulkan_window_redraw (window);
525         EndPaint (hWnd, &ps);
526         break;
527       }
528       case WM_CLOSE:
529       {
530         ShowWindowAsync (window_win32->internal_win_id, SW_HIDE);
531 
532         gst_vulkan_window_win32_close (window);
533         break;
534       }
535       case WM_ERASEBKGND:
536       {
537         ret = TRUE;
538         break;
539       }
540       default:
541       {
542         ret = DefWindowProc (hWnd, uMsg, wParam, lParam);
543         break;
544       }
545     }
546   } else {
547     ret = DefWindowProc (hWnd, uMsg, wParam, lParam);
548   }
549 
550   return ret;
551 }
552 
553 LRESULT FAR PASCAL
sub_class_proc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)554 sub_class_proc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
555 {
556   WNDPROC window_parent_proc = GetProp (hWnd, "vulkan_window_parent_proc");
557 
558   if (uMsg == WM_SIZE) {
559     HWND vulkan_window_id = GetProp (hWnd, "vulkan_window_id");
560     MoveWindow (vulkan_window_id, 0, 0, LOWORD (lParam), HIWORD (lParam),
561         FALSE);
562   }
563 
564   return CallWindowProc (window_parent_proc, hWnd, uMsg, wParam, lParam);
565 }
566