summaryrefslogtreecommitdiff
path: root/gst-libs/gst/vaapi/gstvaapibufferproxy.c
blob: c177cebcd066e13f600bfccb27af3044015e0dd6 (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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
 *  gstvaapibufferproxy.c - Buffer proxy abstraction
 *
 *  Copyright (C) 2014 Intel Corporation
 *    Author: Gwenole Beauchesne <gwenole.beauchesne@intel.com>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public License
 *  as published by the Free Software Foundation; either version 2.1
 *  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
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free
 *  Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301 USA
 */

#include "sysdeps.h"
#include "gstvaapicompat.h"
#include "gstvaapibufferproxy.h"
#include "gstvaapibufferproxy_priv.h"
#include "gstvaapiutils.h"
#include "gstvaapiobject_priv.h"

#define DEBUG 1
#include "gstvaapidebug.h"

static gboolean
gst_vaapi_buffer_proxy_acquire_handle (GstVaapiBufferProxy * proxy)
{
  const guint mem_type = proxy->va_info.mem_type;
  VAStatus va_status;

  if (proxy->va_info.handle)
    return TRUE;

  if (!proxy->parent || proxy->va_buf == VA_INVALID_ID)
    return FALSE;

  GST_VAAPI_OBJECT_LOCK_DISPLAY (proxy->parent);
  va_status = vaAcquireBufferHandle (GST_VAAPI_OBJECT_VADISPLAY (proxy->parent),
      proxy->va_buf, &proxy->va_info);
  GST_VAAPI_OBJECT_UNLOCK_DISPLAY (proxy->parent);
  if (!vaapi_check_status (va_status, "vaAcquireBufferHandle()"))
    return FALSE;
  if (proxy->va_info.mem_type != mem_type)
    return FALSE;
  return TRUE;
}

static gboolean
gst_vaapi_buffer_proxy_release_handle (GstVaapiBufferProxy * proxy)
{
  VAStatus va_status;

  if (!proxy->va_info.handle)
    return TRUE;

  if (!proxy->parent || proxy->va_buf == VA_INVALID_ID)
    return FALSE;

  GST_VAAPI_OBJECT_LOCK_DISPLAY (proxy->parent);
  va_status = vaReleaseBufferHandle (GST_VAAPI_OBJECT_VADISPLAY (proxy->parent),
      proxy->va_buf);
  GST_VAAPI_OBJECT_UNLOCK_DISPLAY (proxy->parent);
  if (!vaapi_check_status (va_status, "vaReleaseBufferHandle()"))
    return FALSE;
  return TRUE;
}

static void
gst_vaapi_buffer_proxy_finalize (GstVaapiBufferProxy * proxy)
{
  gst_vaapi_buffer_proxy_release_handle (proxy);

  if (proxy->mem) {
    gst_memory_unref (proxy->mem);
    proxy->mem = NULL;
  }

  /* Notify the user function that the object is now destroyed */
  if (proxy->destroy_func)
    proxy->destroy_func (proxy->destroy_data);

  gst_vaapi_object_replace (&proxy->parent, NULL);
}

static inline const GstVaapiMiniObjectClass *
gst_vaapi_buffer_proxy_class (void)
{
  static const GstVaapiMiniObjectClass GstVaapiBufferProxyClass = {
    sizeof (GstVaapiBufferProxy),
    (GDestroyNotify) gst_vaapi_buffer_proxy_finalize
  };
  return &GstVaapiBufferProxyClass;
}

GstVaapiBufferProxy *
gst_vaapi_buffer_proxy_new (guintptr handle, guint type, gsize size,
    GDestroyNotify destroy_func, gpointer user_data)
{
  GstVaapiBufferProxy *proxy;

  g_return_val_if_fail (handle != 0, NULL);
  g_return_val_if_fail (size > 0, NULL);

  proxy = (GstVaapiBufferProxy *)
      gst_vaapi_mini_object_new (gst_vaapi_buffer_proxy_class ());
  if (!proxy)
    return NULL;

  proxy->parent = NULL;
  proxy->destroy_func = destroy_func;
  proxy->destroy_data = user_data;
  proxy->type = type;
  proxy->va_buf = VA_INVALID_ID;
  proxy->va_info.handle = handle;
  proxy->va_info.type = VAImageBufferType;
  proxy->va_info.mem_type = from_GstVaapiBufferMemoryType (proxy->type);
  proxy->va_info.mem_size = size;
  proxy->mem = NULL;
  if (!proxy->va_info.mem_type)
    goto error_unsupported_mem_type;
  return proxy;

  /* ERRORS */
error_unsupported_mem_type:
  {
    GST_ERROR ("unsupported buffer type (%d)", proxy->type);
    gst_vaapi_buffer_proxy_unref (proxy);
    return NULL;
  }
}

GstVaapiBufferProxy *
gst_vaapi_buffer_proxy_new_from_object (GstVaapiObject * object,
    VABufferID buf_id, guint type, GDestroyNotify destroy_func, gpointer data)
{
  GstVaapiBufferProxy *proxy;

  g_return_val_if_fail (object != NULL, NULL);

  proxy = (GstVaapiBufferProxy *)
      gst_vaapi_mini_object_new (gst_vaapi_buffer_proxy_class ());
  if (!proxy)
    return NULL;

  proxy->parent = gst_vaapi_object_ref (object);
  proxy->destroy_func = destroy_func;
  proxy->destroy_data = data;
  proxy->type = type;
  proxy->va_buf = buf_id;
  proxy->mem = NULL;
  memset (&proxy->va_info, 0, sizeof (proxy->va_info));
  proxy->va_info.mem_type = from_GstVaapiBufferMemoryType (proxy->type);
  if (!proxy->va_info.mem_type)
    goto error_unsupported_mem_type;
  if (!gst_vaapi_buffer_proxy_acquire_handle (proxy))
    goto error_acquire_handle;
  return proxy;

  /* ERRORS */
error_unsupported_mem_type:
  {
    GST_ERROR ("unsupported buffer type (%d)", proxy->type);
    gst_vaapi_buffer_proxy_unref (proxy);
    return NULL;
  }
error_acquire_handle:
  {
    GST_ERROR ("failed to acquire the underlying VA buffer handle");
    gst_vaapi_buffer_proxy_unref (proxy);
    return NULL;
  }
}

/**
 * gst_vaapi_buffer_proxy_ref:
 * @proxy: a #GstVaapiBufferProxy
 *
 * Atomically increases the reference count of the given @proxy by one.
 *
 * Returns: The same @proxy argument
 */
GstVaapiBufferProxy *
gst_vaapi_buffer_proxy_ref (GstVaapiBufferProxy * proxy)
{
  g_return_val_if_fail (proxy != NULL, NULL);

  return (GstVaapiBufferProxy *)
      gst_vaapi_mini_object_ref (GST_VAAPI_MINI_OBJECT (proxy));
}

/**
 * gst_vaapi_buffer_proxy_unref:
 * @proxy: a #GstVaapiBufferProxy
 *
 * Atomically decreases the reference count of the @proxy by one. If
 * the reference count reaches zero, the object will be free'd.
 */
void
gst_vaapi_buffer_proxy_unref (GstVaapiBufferProxy * proxy)
{
  g_return_if_fail (proxy != NULL);

  gst_vaapi_mini_object_unref (GST_VAAPI_MINI_OBJECT (proxy));
}

/**
 * gst_vaapi_buffer_proxy_replace:
 * @old_proxy_ptr: a pointer to a #GstVaapiBufferProxy
 * @new_proxy: a #GstVaapiBufferProxy
 *
 * Atomically replaces the proxy object held in @old_proxy_ptr with
 * @new_proxy. This means that @old_proxy_ptr shall reference a valid
 * object. However, @new_proxy can be NULL.
 */
void
gst_vaapi_buffer_proxy_replace (GstVaapiBufferProxy ** old_proxy_ptr,
    GstVaapiBufferProxy * new_proxy)
{
  g_return_if_fail (old_proxy_ptr != NULL);

  gst_vaapi_mini_object_replace ((GstVaapiMiniObject **) (old_proxy_ptr),
      GST_VAAPI_MINI_OBJECT (new_proxy));
}

/**
 * gst_vaapi_buffer_proxy_get_type:
 * @proxy: a #GstVaapiBufferProxy
 *
 * Returns the underlying VA buffer memory type.
 *
 * Return value: the buffer memory type
 */
guint
gst_vaapi_buffer_proxy_get_type (GstVaapiBufferProxy * proxy)
{
  g_return_val_if_fail (proxy != NULL, 0);

  return GST_VAAPI_BUFFER_PROXY_TYPE (proxy);
}

/**
 * gst_vaapi_buffer_proxy_get_handle:
 * @proxy: a #GstVaapiBufferProxy
 *
 * Returns the underlying VA buffer handle stored in the @proxy.
 *
 * Return value: the buffer handle
 */
guintptr
gst_vaapi_buffer_proxy_get_handle (GstVaapiBufferProxy * proxy)
{
  g_return_val_if_fail (proxy != NULL, 0);

  return GST_VAAPI_BUFFER_PROXY_HANDLE (proxy);
}

/**
 * gst_vaapi_buffer_proxy_get_size:
 * @proxy: a #GstVaapiBufferProxy
 *
 * Returns the underlying VA buffer memory size in bytes.
 *
 * Return value: the buffer size in bytes
 */
gsize
gst_vaapi_buffer_proxy_get_size (GstVaapiBufferProxy * proxy)
{
  g_return_val_if_fail (proxy != NULL, 0);

  return GST_VAAPI_BUFFER_PROXY_SIZE (proxy);
}

/**
 * gst_vaapi_buffer_proxy_release_data:
 * @proxy: a #GstVaapiBufferProxy
 *
 * Notifies the user to destroy the user's data, though the @proxy is
 * not going to be destroyed.
 **/
void
gst_vaapi_buffer_proxy_release_data (GstVaapiBufferProxy * proxy)
{
  g_return_if_fail (proxy != NULL);

  if (proxy->destroy_func) {
    proxy->destroy_func (proxy->destroy_data);
    proxy->destroy_func = NULL;
    proxy->destroy_data = NULL;
  }
}

/**
 * gst_vaapi_buffer_proxy_set_mem:
 * @proxy: a #GstVaapiBufferProxy
 * @mem: a #GstMemory
 *
 * Relates @mem with @proxy, hence we later will know which memory
 * correspond to processed surface.
 *
 * This is useful when a dmabuf-based memory is instantiated and
 * associated with a surface.
 **/
void
gst_vaapi_buffer_proxy_set_mem (GstVaapiBufferProxy * proxy, GstMemory * mem)
{
  gst_mini_object_replace ((GstMiniObject **) & proxy->mem,
      GST_MINI_OBJECT (mem));
}

/**
 * gst_vaapi_buffer_proxy_peek_mem:
 * @proxy: a #GstVaapiBufferProxy
 *
 * This is useful when a dmabuf-based memory is instantiated and
 * associated with a surface.
 *
 * Returns: (transfer none): the assigned #GstMemory to the @proxy.
 **/
GstMemory *
gst_vaapi_buffer_proxy_peek_mem (GstVaapiBufferProxy * proxy)
{
  return proxy->mem;
}