3 * Copyright (C) 2019 Matthew Waters <matthew@centricular.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
25 #include <gst/vulkan/vulkan.h>
27 #include "gstvkdisplay_cocoa.h"
28 #include "gstvkcocoa_utils.h"
30 #define GST_CAT_DEFAULT gst_vulkan_display_debug
31 GST_DEBUG_CATEGORY_STATIC (gst_vulkan_display_debug);
33 G_DEFINE_TYPE (GstVulkanDisplayCocoa, gst_vulkan_display_cocoa,
34 GST_TYPE_VULKAN_DISPLAY);
36 static void gst_vulkan_display_cocoa_finalize (GObject * object);
37 static gpointer gst_vulkan_display_cocoa_get_handle (GstVulkanDisplay * display);
39 /* Define this if the GLib patch from
40 * https://bugzilla.gnome.org/show_bug.cgi?id=741450
43 #ifndef GSTREAMER_GLIB_COCOA_NSAPPLICATION
45 static GstVulkanDisplayCocoa *singleton = NULL;
46 static gint nsapp_source_id = 0;
47 static GMutex nsapp_lock;
48 static GCond nsapp_cond;
51 gst_vulkan_display_cocoa_nsapp_iteration (gpointer data)
55 if (![NSThread isMainThread]) {
56 GST_WARNING ("NSApp iteration not running in the main thread");
60 while ((event = ([NSApp nextEventMatchingMask:NSEventMaskAny
61 untilDate:[NSDate dateWithTimeIntervalSinceNow:0.05]
62 inMode:NSDefaultRunLoopMode dequeue:YES])) != nil) {
63 [NSApp sendEvent:event];
70 gst_vulkan_display_cocoa_open_and_attach_source (gpointer data)
72 if ([NSThread isMainThread]) {
73 /* The sharedApplication class method initializes
74 * the display environment and connects your program
75 * to the window server and the display server.
76 * It has to be done in the main thread.
78 [NSApplication sharedApplication];
80 GST_DEBUG ("Custom NSApp initialization done");
82 nsapp_source_id = g_timeout_add (60, gst_vulkan_display_cocoa_nsapp_iteration,
85 GST_DEBUG ("NSApp iteration loop attached, id %d", nsapp_source_id);
90 gst_vulkan_display_cocoa_init_nsapp (gpointer data)
92 g_mutex_lock (&nsapp_lock);
94 gst_vulkan_display_cocoa_open_and_attach_source (data);
96 g_cond_signal (&nsapp_cond);
97 g_mutex_unlock (&nsapp_lock);
102 static GstVulkanDisplayCocoa *
103 gst_vulkan_display_cocoa_setup_nsapp (gpointer data)
105 GMainContext *context = g_main_context_default ();
108 g_mutex_lock (&nsapp_lock);
111 GST_DEBUG ("Get existing display");
112 singleton = gst_object_ref (singleton);
113 g_mutex_unlock (&nsapp_lock);
117 if (NSApp != nil && !singleton) {
118 GstVulkanDisplayCocoa *ret = g_object_new (GST_TYPE_VULKAN_DISPLAY_COCOA, NULL);
119 gst_object_ref_sink (ret);
120 g_mutex_unlock (&nsapp_lock);
124 /* All application have to start with [NSApplication sharedApplication]
125 * so if NSApp is nil here let's assume this is a debugging application
126 * that runs a glib main loop. */
127 g_assert (NSApp == nil);
129 GST_DEBUG ("The application has not initialized NSApp");
131 if ([NSThread isMainThread]) {
133 GST_DEBUG ("Setting up NSApp from the main thread");
134 if (g_main_context_is_owner (context)) {
135 GST_DEBUG ("The main thread own the context");
136 gst_vulkan_display_cocoa_open_and_attach_source (data);
137 } else if (g_main_context_acquire (context)) {
138 GST_DEBUG ("The main loop should be shortly running in the main thread");
139 gst_vulkan_display_cocoa_open_and_attach_source (data);
140 g_main_context_release (context);
142 GST_WARNING ("Main loop running in another thread");
146 GST_DEBUG ("Setting up NSApp not from the main thread");
148 if (g_main_context_is_owner (context)) {
149 GST_WARNING ("Default context not own by the main thread");
151 } else if (g_main_context_acquire (context)) {
152 GST_DEBUG ("The main loop should be shortly running in the main thread");
154 g_main_context_release (context);
156 GST_DEBUG ("Main loop running in main thread");
161 gint64 end_time = g_get_monotonic_time () + delta_ms * 1000;;
162 g_idle_add_full (G_PRIORITY_HIGH, gst_vulkan_display_cocoa_init_nsapp, data, NULL);
163 g_cond_wait_until (&nsapp_cond, &nsapp_lock, end_time);
168 GST_ERROR ("Custom NSApp initialization failed");
170 GST_DEBUG ("Create display");
171 singleton = g_object_new (GST_TYPE_VULKAN_DISPLAY_COCOA, NULL);
172 gst_object_ref_sink (singleton);
175 g_mutex_unlock (&nsapp_lock);
182 gst_vulkan_display_cocoa_class_init (GstVulkanDisplayCocoaClass * klass)
184 GST_VULKAN_DISPLAY_CLASS (klass)->get_handle =
185 GST_DEBUG_FUNCPTR (gst_vulkan_display_cocoa_get_handle);
187 G_OBJECT_CLASS (klass)->finalize = gst_vulkan_display_cocoa_finalize;
191 gst_vulkan_display_cocoa_init (GstVulkanDisplayCocoa * display_cocoa)
193 GstVulkanDisplay *display = (GstVulkanDisplay *) display_cocoa;
195 display->type = GST_VULKAN_DISPLAY_TYPE_COCOA;
199 gst_vulkan_display_cocoa_finalize (GObject * object)
201 #ifndef GSTREAMER_GLIB_COCOA_NSAPPLICATION
202 g_mutex_lock (&nsapp_lock);
204 GST_DEBUG ("Destroy display");
206 if (nsapp_source_id) {
207 GST_DEBUG ("Remove NSApp loop iteration, id %d", nsapp_source_id);
208 g_source_remove (nsapp_source_id);
211 g_mutex_unlock (&nsapp_lock);
213 g_mutex_unlock (&nsapp_lock);
216 G_OBJECT_CLASS (gst_vulkan_display_cocoa_parent_class)->finalize (object);
220 * gst_vulkan_display_cocoa_new:
222 * Create a new #GstVulkanDisplayCocoa.
224 * Returns: (transfer full): a new #GstVulkanDisplayCocoa or %NULL
226 GstVulkanDisplayCocoa *
227 gst_vulkan_display_cocoa_new (void)
229 GstVulkanDisplayCocoa *ret;
231 GST_DEBUG_CATEGORY_GET (gst_vulkan_display_debug, "vulkandisplay");
233 #ifndef GSTREAMER_GLIB_COCOA_NSAPPLICATION
234 ret = gst_vulkan_display_cocoa_setup_nsapp (NULL);
236 ret = g_object_new (GST_TYPE_VULKAN_DISPLAY_COCOA, NULL);
237 gst_object_ref_sink (ret);
244 gst_vulkan_display_cocoa_get_handle (GstVulkanDisplay * display)
246 return (gpointer) (__bridge gpointer) NSApp;