• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * GStreamer
3  * Copyright (C) 2019 Matthew Waters <matthew@centricular.com>
4  *
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.
9  *
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.
14  *
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.
19  */
20 
21 /**
22  * SECTION:vulkanhandlepool
23  * @title: GstVulkanHandlePool
24  * @short_description: Vulkan handle pool
25  * @see_also: #GstVulkanHandle, #GstVulkanDevice
26  *
27  * #GstVulkanHandlePool holds a number of handles that are pooled together.
28  */
29 
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33 
34 #include "gstvkhandlepool.h"
35 #include "gstvkdevice.h"
36 
37 #define GST_VULKAN_HANDLE_POOL_LARGE_OUTSTANDING 1024
38 
39 #define GST_CAT_DEFAULT gst_debug_vulkan_handle_pool
40 GST_DEBUG_CATEGORY (GST_CAT_DEFAULT);
41 
42 #define parent_class gst_vulkan_handle_pool_parent_class
43 G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GstVulkanHandlePool, gst_vulkan_handle_pool,
44     GST_TYPE_OBJECT, GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT,
45         "vulkanhandlepool", 0, "Vulkan handle pool"));
46 
47 static gpointer
gst_vulkan_handle_pool_default_alloc(GstVulkanHandlePool * pool,GError ** error)48 gst_vulkan_handle_pool_default_alloc (GstVulkanHandlePool * pool,
49     GError ** error)
50 {
51   return NULL;
52 }
53 
54 static gpointer
gst_vulkan_handle_pool_default_acquire(GstVulkanHandlePool * pool,GError ** error)55 gst_vulkan_handle_pool_default_acquire (GstVulkanHandlePool * pool,
56     GError ** error)
57 {
58   gpointer ret;
59 
60   GST_OBJECT_LOCK (pool);
61   if (pool->available->len > 0) {
62     ret = g_ptr_array_remove_index_fast (pool->available, 0);
63   } else {
64     ret = gst_vulkan_handle_pool_alloc (pool, error);
65   }
66 
67   if (ret) {
68     g_ptr_array_add (pool->outstanding, ret);
69 
70 #if defined(GST_ENABLE_EXTRA_CHECKS)
71     if (pool->outstanding->len > GST_VULKAN_HANDLE_POOL_LARGE_OUTSTANDING)
72       g_critical ("%s: There are a large number of handles outstanding! "
73           "This usually means there is a reference counting issue somewhere.",
74           GST_OBJECT_NAME (pool));
75 #endif
76   }
77   GST_OBJECT_UNLOCK (pool);
78 
79   return ret;
80 }
81 
82 static void
gst_vulkan_handle_pool_default_release(GstVulkanHandlePool * pool,gpointer handle)83 gst_vulkan_handle_pool_default_release (GstVulkanHandlePool * pool,
84     gpointer handle)
85 {
86   GST_OBJECT_LOCK (pool);
87   if (!g_ptr_array_remove_fast (pool->outstanding, handle)) {
88     g_warning ("%s: Attempt was made to release a handle (%p) that does not "
89         "belong to us", GST_OBJECT_NAME (pool), handle);
90     GST_OBJECT_UNLOCK (pool);
91     return;
92   }
93 
94   g_ptr_array_add (pool->available, handle);
95   GST_OBJECT_UNLOCK (pool);
96 }
97 
98 static void
gst_vulkan_handle_pool_default_free(GstVulkanHandlePool * pool,gpointer handle)99 gst_vulkan_handle_pool_default_free (GstVulkanHandlePool * pool,
100     gpointer handle)
101 {
102 }
103 
104 static void
do_free_handle(gpointer handle,GstVulkanHandlePool * pool)105 do_free_handle (gpointer handle, GstVulkanHandlePool * pool)
106 {
107   GstVulkanHandlePoolClass *klass = GST_VULKAN_HANDLE_POOL_GET_CLASS (pool);
108   klass->free (pool, handle);
109 }
110 
111 static void
gst_vulkan_handle_pool_dispose(GObject * object)112 gst_vulkan_handle_pool_dispose (GObject * object)
113 {
114   GstVulkanHandlePool *pool = GST_VULKAN_HANDLE_POOL (object);
115 
116   if (pool->outstanding) {
117     g_warn_if_fail (pool->outstanding->len <= 0);
118     g_ptr_array_unref (pool->outstanding);
119   }
120   pool->outstanding = NULL;
121 
122   if (pool->available) {
123     g_ptr_array_foreach (pool->available, (GFunc) do_free_handle, pool);
124     g_ptr_array_unref (pool->available);
125   }
126   pool->available = NULL;
127 
128   G_OBJECT_CLASS (parent_class)->dispose (object);
129 }
130 
131 static void
gst_vulkan_handle_pool_finalize(GObject * object)132 gst_vulkan_handle_pool_finalize (GObject * object)
133 {
134   GstVulkanHandlePool *pool = GST_VULKAN_HANDLE_POOL (object);
135 
136   gst_clear_object (&pool->device);
137 
138   G_OBJECT_CLASS (parent_class)->finalize (object);
139 }
140 
141 static void
gst_vulkan_handle_pool_init(GstVulkanHandlePool * handle)142 gst_vulkan_handle_pool_init (GstVulkanHandlePool * handle)
143 {
144   handle->outstanding = g_ptr_array_new ();
145   handle->available = g_ptr_array_new ();
146 }
147 
148 static void
gst_vulkan_handle_pool_class_init(GstVulkanHandlePoolClass * klass)149 gst_vulkan_handle_pool_class_init (GstVulkanHandlePoolClass * klass)
150 {
151   GObjectClass *gobject_class = (GObjectClass *) klass;
152 
153   gobject_class->dispose = gst_vulkan_handle_pool_dispose;
154   gobject_class->finalize = gst_vulkan_handle_pool_finalize;
155 
156   klass->alloc = gst_vulkan_handle_pool_default_alloc;
157   klass->acquire = gst_vulkan_handle_pool_default_acquire;
158   klass->release = gst_vulkan_handle_pool_default_release;
159   klass->free = gst_vulkan_handle_pool_default_free;
160 }
161 
162 gpointer
gst_vulkan_handle_pool_alloc(GstVulkanHandlePool * pool,GError ** error)163 gst_vulkan_handle_pool_alloc (GstVulkanHandlePool * pool, GError ** error)
164 {
165   GstVulkanHandlePoolClass *klass;
166 
167   g_return_val_if_fail (GST_IS_VULKAN_HANDLE_POOL (pool), NULL);
168   klass = GST_VULKAN_HANDLE_POOL_GET_CLASS (pool);
169   g_return_val_if_fail (klass->alloc != NULL, NULL);
170 
171   return klass->alloc (pool, error);
172 }
173 
174 gpointer
gst_vulkan_handle_pool_acquire(GstVulkanHandlePool * pool,GError ** error)175 gst_vulkan_handle_pool_acquire (GstVulkanHandlePool * pool, GError ** error)
176 {
177   GstVulkanHandlePoolClass *klass;
178 
179   g_return_val_if_fail (GST_IS_VULKAN_HANDLE_POOL (pool), NULL);
180   klass = GST_VULKAN_HANDLE_POOL_GET_CLASS (pool);
181   g_return_val_if_fail (klass->acquire != NULL, NULL);
182 
183   return klass->acquire (pool, error);
184 }
185 
186 void
gst_vulkan_handle_pool_release(GstVulkanHandlePool * pool,gpointer handle)187 gst_vulkan_handle_pool_release (GstVulkanHandlePool * pool, gpointer handle)
188 {
189   GstVulkanHandlePoolClass *klass;
190 
191   g_return_if_fail (GST_IS_VULKAN_HANDLE_POOL (pool));
192   klass = GST_VULKAN_HANDLE_POOL_GET_CLASS (pool);
193   g_return_if_fail (klass->release != NULL);
194 
195   klass->release (pool, handle);
196 }
197