summaryrefslogtreecommitdiff
path: root/gst-libs/gst/vulkan/gstvkhandlepool.c
blob: 22cc975aa8884c3ecfbc6a8bdd7ec6d0cad321af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
 * GStreamer
 * Copyright (C) 2019 Matthew Waters <matthew@centricular.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

/**
 * SECTION:vulkanhandlepool
 * @title: vulkanhandlepool
 *
 * #GstVulkanHandlePool holds a number of handles that are pooled together.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "gstvkhandlepool.h"
#include "gstvkdevice.h"

#define GST_VULKAN_HANDLE_POOL_LARGE_OUTSTANDING 1024

#define GST_CAT_DEFAULT gst_debug_vulkan_handle_pool
GST_DEBUG_CATEGORY (GST_CAT_DEFAULT);

#define parent_class gst_vulkan_handle_pool_parent_class
G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GstVulkanHandlePool, gst_vulkan_handle_pool,
    GST_TYPE_OBJECT, GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT,
        "vulkanhandlepool", 0, "Vulkan handle pool"));

static gpointer
gst_vulkan_handle_pool_default_alloc (GstVulkanHandlePool * pool,
    GError ** error)
{
  return NULL;
}

static gpointer
gst_vulkan_handle_pool_default_acquire (GstVulkanHandlePool * pool,
    GError ** error)
{
  gpointer ret;

  GST_OBJECT_LOCK (pool);
  if (pool->available->len > 0) {
    ret = g_ptr_array_remove_index_fast (pool->available, 0);
  } else {
    ret = gst_vulkan_handle_pool_alloc (pool, error);
  }

  if (ret) {
    g_ptr_array_add (pool->outstanding, ret);

#if defined(GST_ENABLE_EXTRA_CHECKS)
    if (pool->outstanding->len > GST_VULKAN_HANDLE_POOL_LARGE_OUTSTANDING)
      g_critical ("%s: There are a large number of handles outstanding! "
          "This usually means there is a reference counting issue somewhere.",
          GST_OBJECT_NAME (pool));
#endif
  }
  GST_OBJECT_UNLOCK (pool);

  return ret;
}

static void
gst_vulkan_handle_pool_default_release (GstVulkanHandlePool * pool,
    gpointer handle)
{
  GST_OBJECT_LOCK (pool);
  if (!g_ptr_array_remove_fast (pool->outstanding, handle)) {
    g_warning ("%s: Attempt was made to release a handle (%p) that does not "
        "belong to us", GST_OBJECT_NAME (pool), handle);
    GST_OBJECT_UNLOCK (pool);
    return;
  }

  g_ptr_array_add (pool->available, handle);
  GST_OBJECT_UNLOCK (pool);
}

static void
gst_vulkan_handle_pool_default_free (GstVulkanHandlePool * pool,
    gpointer handle)
{
}

static void
do_free_handle (gpointer handle, GstVulkanHandlePool * pool)
{
  GstVulkanHandlePoolClass *klass = GST_VULKAN_HANDLE_POOL_GET_CLASS (pool);
  klass->free (pool, handle);
}

static void
gst_vulkan_handle_pool_dispose (GObject * object)
{
  GstVulkanHandlePool *pool = GST_VULKAN_HANDLE_POOL (object);

  if (pool->outstanding) {
    g_warn_if_fail (pool->outstanding->len <= 0);
    g_ptr_array_unref (pool->outstanding);
  }
  pool->outstanding = NULL;

  if (pool->available) {
    g_ptr_array_foreach (pool->available, (GFunc) do_free_handle, pool);
    g_ptr_array_unref (pool->available);
  }
  pool->available = NULL;

  G_OBJECT_CLASS (parent_class)->dispose (object);
}

static void
gst_vulkan_handle_pool_finalize (GObject * object)
{
  GstVulkanHandlePool *pool = GST_VULKAN_HANDLE_POOL (object);

  gst_clear_object (&pool->device);

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

static void
gst_vulkan_handle_pool_init (GstVulkanHandlePool * handle)
{
  handle->outstanding = g_ptr_array_new ();
  handle->available = g_ptr_array_new ();
}

static void
gst_vulkan_handle_pool_class_init (GstVulkanHandlePoolClass * klass)
{
  GObjectClass *gobject_class = (GObjectClass *) klass;

  gobject_class->dispose = gst_vulkan_handle_pool_dispose;
  gobject_class->finalize = gst_vulkan_handle_pool_finalize;

  klass->alloc = gst_vulkan_handle_pool_default_alloc;
  klass->acquire = gst_vulkan_handle_pool_default_acquire;
  klass->release = gst_vulkan_handle_pool_default_release;
  klass->free = gst_vulkan_handle_pool_default_free;
}

gpointer
gst_vulkan_handle_pool_alloc (GstVulkanHandlePool * pool, GError ** error)
{
  GstVulkanHandlePoolClass *klass;

  g_return_val_if_fail (GST_IS_VULKAN_HANDLE_POOL (pool), NULL);
  klass = GST_VULKAN_HANDLE_POOL_GET_CLASS (pool);
  g_return_val_if_fail (klass->alloc != NULL, NULL);

  return klass->alloc (pool, error);
}

gpointer
gst_vulkan_handle_pool_acquire (GstVulkanHandlePool * pool, GError ** error)
{
  GstVulkanHandlePoolClass *klass;

  g_return_val_if_fail (GST_IS_VULKAN_HANDLE_POOL (pool), NULL);
  klass = GST_VULKAN_HANDLE_POOL_GET_CLASS (pool);
  g_return_val_if_fail (klass->acquire != NULL, NULL);

  return klass->acquire (pool, error);
}

void
gst_vulkan_handle_pool_release (GstVulkanHandlePool * pool, gpointer handle)
{
  GstVulkanHandlePoolClass *klass;

  g_return_if_fail (GST_IS_VULKAN_HANDLE_POOL (pool));
  klass = GST_VULKAN_HANDLE_POOL_GET_CLASS (pool);
  g_return_if_fail (klass->release != NULL);

  klass->release (pool, handle);
}