1 /* GStreamer
2 *
3 * Copyright (C) 2016 Igalia
4 *
5 * Authors:
6 * Víctor Manuel Jáquez Leal <vjaquez@igalia.com>
7 * Javier Martin <javiermartin@by.com.es>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
18 *
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
23 *
24 */
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include <fcntl.h>
31 #include <xf86drm.h>
32 #include <xf86drmMode.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <sys/mman.h>
36 #include <unistd.h>
37
38 /* it needs to be below because is internal to libdrm */
39 #include <drm.h>
40
41 #include <gst/allocators/gstdmabuf.h>
42
43 #include "gstkmsallocator.h"
44 #include "gstkmsutils.h"
45
46 #ifndef DRM_RDWR
47 #define DRM_RDWR O_RDWR
48 #endif
49
50 #define GST_CAT_DEFAULT kmsallocator_debug
51 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
52
53 #define GST_KMS_MEMORY_TYPE "KMSMemory"
54
55 struct kms_bo
56 {
57 void *ptr;
58 size_t size;
59 unsigned handle;
60 unsigned int refs;
61 };
62
63 struct _GstKMSAllocatorPrivate
64 {
65 int fd;
66 /* protected by GstKMSAllocator object lock */
67 GList *mem_cache;
68 GstAllocator *dmabuf_alloc;
69 };
70
71 #define parent_class gst_kms_allocator_parent_class
72 G_DEFINE_TYPE_WITH_CODE (GstKMSAllocator, gst_kms_allocator, GST_TYPE_ALLOCATOR,
73 G_ADD_PRIVATE (GstKMSAllocator);
74 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "kmsallocator", 0,
75 "KMS allocator"));
76
77 enum
78 {
79 PROP_DRM_FD = 1,
80 PROP_N,
81 };
82
83 static GParamSpec *g_props[PROP_N] = { NULL, };
84
85 gboolean
gst_is_kms_memory(GstMemory * mem)86 gst_is_kms_memory (GstMemory * mem)
87 {
88 return gst_memory_is_type (mem, GST_KMS_MEMORY_TYPE);
89 }
90
91 guint32
gst_kms_memory_get_fb_id(GstMemory * mem)92 gst_kms_memory_get_fb_id (GstMemory * mem)
93 {
94 if (!gst_is_kms_memory (mem))
95 return 0;
96 return ((GstKMSMemory *) mem)->fb_id;
97 }
98
99 static gboolean
check_fd(GstKMSAllocator * alloc)100 check_fd (GstKMSAllocator * alloc)
101 {
102 return alloc->priv->fd > -1;
103 }
104
105 static void
gst_kms_allocator_memory_reset(GstKMSAllocator * allocator,GstKMSMemory * mem)106 gst_kms_allocator_memory_reset (GstKMSAllocator * allocator, GstKMSMemory * mem)
107 {
108 int err;
109 struct drm_mode_destroy_dumb arg = { 0, };
110
111 if (!check_fd (allocator))
112 return;
113
114 if (mem->fb_id) {
115 GST_DEBUG_OBJECT (allocator, "removing fb id %d", mem->fb_id);
116 drmModeRmFB (allocator->priv->fd, mem->fb_id);
117 mem->fb_id = 0;
118 }
119
120 if (!mem->bo)
121 return;
122
123 if (mem->bo->ptr != NULL) {
124 GST_WARNING_OBJECT (allocator, "destroying mapped bo (refcount=%d)",
125 mem->bo->refs);
126 munmap (mem->bo->ptr, mem->bo->size);
127 mem->bo->ptr = NULL;
128 }
129
130 arg.handle = mem->bo->handle;
131
132 err = drmIoctl (allocator->priv->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &arg);
133 if (err)
134 GST_WARNING_OBJECT (allocator,
135 "Failed to destroy dumb buffer object: %s %d", strerror (errno), errno);
136
137 g_free (mem->bo);
138 mem->bo = NULL;
139 }
140
141 /* Copied from gst_v4l2_object_extrapolate_stride() */
142 static gint
extrapolate_stride(const GstVideoFormatInfo * finfo,gint plane,gint stride)143 extrapolate_stride (const GstVideoFormatInfo * finfo, gint plane, gint stride)
144 {
145 gint estride;
146
147 switch (finfo->format) {
148 case GST_VIDEO_FORMAT_NV12:
149 case GST_VIDEO_FORMAT_NV12_64Z32:
150 case GST_VIDEO_FORMAT_NV21:
151 case GST_VIDEO_FORMAT_NV16:
152 case GST_VIDEO_FORMAT_NV61:
153 case GST_VIDEO_FORMAT_NV24:
154 estride = (plane == 0 ? 1 : 2) *
155 GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (finfo, plane, stride);
156 break;
157 default:
158 estride = GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (finfo, plane, stride);
159 break;
160 }
161
162 return estride;
163 }
164
165 static gboolean
gst_kms_allocator_memory_create(GstKMSAllocator * allocator,GstKMSMemory * kmsmem,GstVideoInfo * vinfo)166 gst_kms_allocator_memory_create (GstKMSAllocator * allocator,
167 GstKMSMemory * kmsmem, GstVideoInfo * vinfo)
168 {
169 gint i, ret, h;
170 struct drm_mode_create_dumb arg = { 0, };
171 guint32 fmt;
172 gint num_planes = GST_VIDEO_INFO_N_PLANES (vinfo);
173 gsize offs = 0;
174
175 if (kmsmem->bo)
176 return TRUE;
177
178 if (!check_fd (allocator))
179 return FALSE;
180
181 kmsmem->bo = g_malloc0 (sizeof (*kmsmem->bo));
182 if (!kmsmem->bo)
183 return FALSE;
184
185 fmt = gst_drm_format_from_video (GST_VIDEO_INFO_FORMAT (vinfo));
186 arg.bpp = gst_drm_bpp_from_drm (fmt);
187 arg.width = GST_VIDEO_INFO_WIDTH (vinfo);
188 h = GST_VIDEO_INFO_HEIGHT (vinfo);
189 arg.height = gst_drm_height_from_drm (fmt, h);
190
191 ret = drmIoctl (allocator->priv->fd, DRM_IOCTL_MODE_CREATE_DUMB, &arg);
192 if (ret)
193 goto create_failed;
194
195 if (!arg.pitch)
196 goto done;
197
198 for (i = 0; i < num_planes; i++) {
199 guint32 pitch;
200
201 if (!arg.pitch)
202 continue;
203
204 /* Overwrite the video info's stride and offset using the pitch calculcated
205 * by the kms driver. */
206 pitch = extrapolate_stride (vinfo->finfo, i, arg.pitch);
207 GST_VIDEO_INFO_PLANE_STRIDE (vinfo, i) = pitch;
208 GST_VIDEO_INFO_PLANE_OFFSET (vinfo, i) = offs;
209
210 /* Note that we cannot negotiate special padding betweem each planes,
211 * hence using the display height here. */
212 offs += pitch * GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (vinfo->finfo, i, h);
213
214 GST_DEBUG_OBJECT (allocator, "Created BO plane %i with stride %i and "
215 "offset %" G_GSIZE_FORMAT, i,
216 GST_VIDEO_INFO_PLANE_STRIDE (vinfo, i),
217 GST_VIDEO_INFO_PLANE_OFFSET (vinfo, i));
218 }
219
220 /* Update with the size use for display, excluding any padding at the end */
221 GST_VIDEO_INFO_SIZE (vinfo) = offs;
222
223 done:
224 kmsmem->bo->handle = arg.handle;
225 /* will be used a memory maxsize */
226 kmsmem->bo->size = arg.size;
227
228 /* Validate the size to prevent overflow */
229 if (kmsmem->bo->size < GST_VIDEO_INFO_SIZE (vinfo)) {
230 GST_ERROR_OBJECT (allocator,
231 "DUMB buffer has a size of %" G_GSIZE_FORMAT
232 " but we require at least %" G_GSIZE_FORMAT " to hold a frame",
233 kmsmem->bo->size, GST_VIDEO_INFO_SIZE (vinfo));
234 return FALSE;
235 }
236
237 return TRUE;
238
239 /* ERRORS */
240 create_failed:
241 {
242 GST_ERROR_OBJECT (allocator, "Failed to create buffer object: %s (%d)",
243 strerror (-ret), ret);
244 g_free (kmsmem->bo);
245 kmsmem->bo = NULL;
246 return FALSE;
247 }
248 }
249
250 static void
gst_kms_allocator_free(GstAllocator * allocator,GstMemory * mem)251 gst_kms_allocator_free (GstAllocator * allocator, GstMemory * mem)
252 {
253 GstKMSAllocator *alloc;
254 GstKMSMemory *kmsmem;
255
256 alloc = GST_KMS_ALLOCATOR (allocator);
257 kmsmem = (GstKMSMemory *) mem;
258
259 gst_kms_allocator_memory_reset (alloc, kmsmem);
260 g_slice_free (GstKMSMemory, kmsmem);
261 }
262
263 static void
gst_kms_allocator_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)264 gst_kms_allocator_set_property (GObject * object, guint prop_id,
265 const GValue * value, GParamSpec * pspec)
266 {
267 GstKMSAllocator *alloc;
268
269 alloc = GST_KMS_ALLOCATOR (object);
270
271 switch (prop_id) {
272 case PROP_DRM_FD:{
273 int fd = g_value_get_int (value);
274 if (fd > -1)
275 alloc->priv->fd = dup (fd);
276 break;
277 }
278 default:
279 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
280 break;
281 }
282 }
283
284 static void
gst_kms_allocator_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)285 gst_kms_allocator_get_property (GObject * object, guint prop_id,
286 GValue * value, GParamSpec * pspec)
287 {
288 GstKMSAllocator *alloc;
289
290 alloc = GST_KMS_ALLOCATOR (object);
291
292 switch (prop_id) {
293 case PROP_DRM_FD:
294 g_value_set_int (value, alloc->priv->fd);
295 break;
296 default:
297 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
298 break;
299 }
300 }
301
302 static void
gst_kms_allocator_finalize(GObject * obj)303 gst_kms_allocator_finalize (GObject * obj)
304 {
305 GstKMSAllocator *alloc;
306
307 alloc = GST_KMS_ALLOCATOR (obj);
308
309 gst_kms_allocator_clear_cache (GST_ALLOCATOR (alloc));
310
311 if (alloc->priv->dmabuf_alloc)
312 gst_object_unref (alloc->priv->dmabuf_alloc);
313
314 if (check_fd (alloc))
315 close (alloc->priv->fd);
316
317 G_OBJECT_CLASS (parent_class)->finalize (obj);
318 }
319
320 static void
gst_kms_allocator_class_init(GstKMSAllocatorClass * klass)321 gst_kms_allocator_class_init (GstKMSAllocatorClass * klass)
322 {
323 GObjectClass *gobject_class;
324 GstAllocatorClass *allocator_class;
325
326 allocator_class = GST_ALLOCATOR_CLASS (klass);
327 gobject_class = G_OBJECT_CLASS (klass);
328
329 allocator_class->free = gst_kms_allocator_free;
330
331 gobject_class->set_property = gst_kms_allocator_set_property;
332 gobject_class->get_property = gst_kms_allocator_get_property;
333 gobject_class->finalize = gst_kms_allocator_finalize;
334
335 g_props[PROP_DRM_FD] = g_param_spec_int ("drm-fd", "DRM fd",
336 "DRM file descriptor", -1, G_MAXINT, -1,
337 G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
338
339 g_object_class_install_properties (gobject_class, PROP_N, g_props);
340 }
341
342 static gpointer
gst_kms_memory_map(GstMemory * mem,gsize maxsize,GstMapFlags flags)343 gst_kms_memory_map (GstMemory * mem, gsize maxsize, GstMapFlags flags)
344 {
345 GstKMSMemory *kmsmem;
346 GstKMSAllocator *alloc;
347 int err;
348 gpointer out;
349 struct drm_mode_map_dumb arg = { 0, };
350
351 alloc = (GstKMSAllocator *) mem->allocator;
352
353 if (!check_fd (alloc))
354 return NULL;
355
356 kmsmem = (GstKMSMemory *) mem;
357 if (!kmsmem->bo)
358 return NULL;
359
360 /* Reuse existing buffer object mapping if possible */
361 if (kmsmem->bo->ptr != NULL) {
362 goto out;
363 }
364
365 arg.handle = kmsmem->bo->handle;
366
367 err = drmIoctl (alloc->priv->fd, DRM_IOCTL_MODE_MAP_DUMB, &arg);
368 if (err) {
369 GST_ERROR_OBJECT (alloc, "Failed to get offset of buffer object: %s %d",
370 strerror (-err), err);
371 return NULL;
372 }
373
374 out = mmap (0, kmsmem->bo->size,
375 PROT_READ | PROT_WRITE, MAP_SHARED, alloc->priv->fd, arg.offset);
376 if (out == MAP_FAILED) {
377 GST_ERROR_OBJECT (alloc, "Failed to map dumb buffer object: %s %d",
378 strerror (errno), errno);
379 return NULL;
380 }
381 kmsmem->bo->ptr = out;
382
383 out:
384 g_atomic_int_inc (&kmsmem->bo->refs);
385 return kmsmem->bo->ptr;
386 }
387
388 static void
gst_kms_memory_unmap(GstMemory * mem)389 gst_kms_memory_unmap (GstMemory * mem)
390 {
391 GstKMSMemory *kmsmem;
392
393 if (!check_fd ((GstKMSAllocator *) mem->allocator))
394 return;
395
396 kmsmem = (GstKMSMemory *) mem;
397 if (!kmsmem->bo)
398 return;
399
400 if (g_atomic_int_dec_and_test (&kmsmem->bo->refs)) {
401 munmap (kmsmem->bo->ptr, kmsmem->bo->size);
402 kmsmem->bo->ptr = NULL;
403 }
404 }
405
406 static void
gst_kms_allocator_init(GstKMSAllocator * allocator)407 gst_kms_allocator_init (GstKMSAllocator * allocator)
408 {
409 GstAllocator *alloc;
410
411 alloc = GST_ALLOCATOR_CAST (allocator);
412
413 allocator->priv = gst_kms_allocator_get_instance_private (allocator);
414 allocator->priv->fd = -1;
415
416 alloc->mem_type = GST_KMS_MEMORY_TYPE;
417 alloc->mem_map = gst_kms_memory_map;
418 alloc->mem_unmap = gst_kms_memory_unmap;
419 /* Use the default, fallback copy function */
420
421 GST_OBJECT_FLAG_SET (allocator, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC);
422 }
423
424 GstAllocator *
gst_kms_allocator_new(int fd)425 gst_kms_allocator_new (int fd)
426 {
427 GstAllocator *alloc;
428
429 alloc = g_object_new (GST_TYPE_KMS_ALLOCATOR, "name",
430 "KMSMemory::allocator", "drm-fd", fd, NULL);
431 gst_object_ref_sink (alloc);
432
433 return alloc;
434 }
435
436 /* The mem_offsets are relative to the GstMemory start, unlike the vinfo->offset
437 * which are relative to the GstBuffer start. */
438 static gboolean
gst_kms_allocator_add_fb(GstKMSAllocator * alloc,GstKMSMemory * kmsmem,gsize in_offsets[GST_VIDEO_MAX_PLANES],GstVideoInfo * vinfo)439 gst_kms_allocator_add_fb (GstKMSAllocator * alloc, GstKMSMemory * kmsmem,
440 gsize in_offsets[GST_VIDEO_MAX_PLANES], GstVideoInfo * vinfo)
441 {
442 gint i, ret;
443 gint num_planes = GST_VIDEO_INFO_N_PLANES (vinfo);
444 guint32 w, h, fmt, bo_handles[4] = { 0, };
445 guint32 pitches[4] = { 0, };
446 guint32 offsets[4] = { 0, };
447
448 if (kmsmem->fb_id)
449 return TRUE;
450
451 w = GST_VIDEO_INFO_WIDTH (vinfo);
452 h = GST_VIDEO_INFO_HEIGHT (vinfo);
453 fmt = gst_drm_format_from_video (GST_VIDEO_INFO_FORMAT (vinfo));
454
455 for (i = 0; i < num_planes; i++) {
456 if (kmsmem->bo)
457 bo_handles[i] = kmsmem->bo->handle;
458 else
459 bo_handles[i] = kmsmem->gem_handle[i];
460
461 pitches[i] = GST_VIDEO_INFO_PLANE_STRIDE (vinfo, i);
462 offsets[i] = in_offsets[i];
463 }
464
465 GST_DEBUG_OBJECT (alloc, "bo handles: %d, %d, %d, %d", bo_handles[0],
466 bo_handles[1], bo_handles[2], bo_handles[3]);
467
468 ret = drmModeAddFB2 (alloc->priv->fd, w, h, fmt, bo_handles, pitches,
469 offsets, &kmsmem->fb_id, 0);
470 if (ret) {
471 GST_ERROR_OBJECT (alloc, "Failed to bind to framebuffer: %s (%d)",
472 strerror (-ret), ret);
473 return FALSE;
474 }
475
476 return TRUE;
477 }
478
479 GstMemory *
gst_kms_allocator_bo_alloc(GstAllocator * allocator,GstVideoInfo * vinfo)480 gst_kms_allocator_bo_alloc (GstAllocator * allocator, GstVideoInfo * vinfo)
481 {
482 GstKMSAllocator *alloc;
483 GstKMSMemory *kmsmem;
484 GstMemory *mem;
485
486 kmsmem = g_slice_new0 (GstKMSMemory);
487 if (!kmsmem)
488 return NULL;
489
490 alloc = GST_KMS_ALLOCATOR (allocator);
491
492 mem = GST_MEMORY_CAST (kmsmem);
493
494 if (!gst_kms_allocator_memory_create (alloc, kmsmem, vinfo)) {
495 g_slice_free (GstKMSMemory, kmsmem);
496 return NULL;
497 }
498
499 gst_memory_init (mem, GST_MEMORY_FLAG_NO_SHARE, allocator, NULL,
500 kmsmem->bo->size, 0, 0, GST_VIDEO_INFO_SIZE (vinfo));
501
502 if (!gst_kms_allocator_add_fb (alloc, kmsmem, vinfo->offset, vinfo))
503 goto fail;
504
505 return mem;
506
507 /* ERRORS */
508 fail:
509 gst_memory_unref (mem);
510 return NULL;
511 }
512
513 GstKMSMemory *
gst_kms_allocator_dmabuf_import(GstAllocator * allocator,gint * prime_fds,gint n_planes,gsize offsets[GST_VIDEO_MAX_PLANES],GstVideoInfo * vinfo)514 gst_kms_allocator_dmabuf_import (GstAllocator * allocator, gint * prime_fds,
515 gint n_planes, gsize offsets[GST_VIDEO_MAX_PLANES], GstVideoInfo * vinfo)
516 {
517 GstKMSAllocator *alloc;
518 GstKMSMemory *kmsmem;
519 GstMemory *mem;
520 gint i, ret;
521
522 g_return_val_if_fail (n_planes <= GST_VIDEO_MAX_PLANES, FALSE);
523
524 kmsmem = g_slice_new0 (GstKMSMemory);
525 if (!kmsmem)
526 return FALSE;
527
528 mem = GST_MEMORY_CAST (kmsmem);
529 gst_memory_init (mem, GST_MEMORY_FLAG_NO_SHARE, allocator, NULL,
530 GST_VIDEO_INFO_SIZE (vinfo), 0, 0, GST_VIDEO_INFO_SIZE (vinfo));
531
532 alloc = GST_KMS_ALLOCATOR (allocator);
533 for (i = 0; i < n_planes; i++) {
534 ret = drmPrimeFDToHandle (alloc->priv->fd, prime_fds[i],
535 &kmsmem->gem_handle[i]);
536 if (ret)
537 goto import_fd_failed;
538 }
539
540 if (!gst_kms_allocator_add_fb (alloc, kmsmem, offsets, vinfo))
541 goto failed;
542
543 for (i = 0; i < n_planes; i++) {
544 struct drm_gem_close arg = { kmsmem->gem_handle[i], };
545 gint err;
546
547 err = drmIoctl (alloc->priv->fd, DRM_IOCTL_GEM_CLOSE, &arg);
548 if (err)
549 GST_WARNING_OBJECT (allocator,
550 "Failed to close GEM handle: %s %d", strerror (errno), errno);
551
552 kmsmem->gem_handle[i] = 0;
553 }
554
555 return kmsmem;
556
557 /* ERRORS */
558 import_fd_failed:
559 {
560 GST_ERROR_OBJECT (alloc, "Failed to import prime fd %d: %s (%d)",
561 prime_fds[i], strerror (-ret), ret);
562 /* fallback */
563 }
564
565 failed:
566 {
567 gst_memory_unref (mem);
568 return NULL;
569 }
570 }
571
572 GstMemory *
gst_kms_allocator_dmabuf_export(GstAllocator * allocator,GstMemory * _kmsmem)573 gst_kms_allocator_dmabuf_export (GstAllocator * allocator, GstMemory * _kmsmem)
574 {
575 GstKMSMemory *kmsmem = (GstKMSMemory *) _kmsmem;
576 GstKMSAllocator *alloc = GST_KMS_ALLOCATOR (allocator);
577 GstMemory *mem;
578 gint ret;
579 gint prime_fd;
580
581 /* We can only export DUMB buffers */
582 g_return_val_if_fail (kmsmem->bo, NULL);
583
584
585 ret = drmPrimeHandleToFD (alloc->priv->fd, kmsmem->bo->handle,
586 DRM_CLOEXEC | DRM_RDWR, &prime_fd);
587 if (ret)
588 goto export_fd_failed;
589
590 if (G_UNLIKELY (alloc->priv->dmabuf_alloc == NULL))
591 alloc->priv->dmabuf_alloc = gst_dmabuf_allocator_new ();
592
593 mem = gst_dmabuf_allocator_alloc (alloc->priv->dmabuf_alloc, prime_fd,
594 gst_memory_get_sizes (_kmsmem, NULL, NULL));
595
596 /* Populate the cache so KMSSink can find the kmsmem back when it receives
597 * one of these DMABuf. This call takes ownership of the kmsmem. */
598 gst_kms_allocator_cache (allocator, mem, _kmsmem);
599
600 GST_DEBUG_OBJECT (alloc, "Exported bo handle %d as %d", kmsmem->bo->handle,
601 prime_fd);
602
603 return mem;
604
605 /* ERRORS */
606 export_fd_failed:
607 {
608 GST_ERROR_OBJECT (alloc, "Failed to export bo handle %d: %s (%d)",
609 kmsmem->bo->handle, g_strerror (errno), ret);
610 return NULL;
611 }
612 }
613
614 /* FIXME, using gdata for caching on upstream memory is not tee safe */
615 GstMemory *
gst_kms_allocator_get_cached(GstMemory * mem)616 gst_kms_allocator_get_cached (GstMemory * mem)
617 {
618 return gst_mini_object_get_qdata (GST_MINI_OBJECT (mem),
619 g_quark_from_static_string ("kmsmem"));
620 }
621
622 static void
cached_kmsmem_disposed_cb(GstKMSAllocator * alloc,GstMiniObject * obj)623 cached_kmsmem_disposed_cb (GstKMSAllocator * alloc, GstMiniObject * obj)
624 {
625 GST_OBJECT_LOCK (alloc);
626 alloc->priv->mem_cache = g_list_remove (alloc->priv->mem_cache, obj);
627 GST_OBJECT_UNLOCK (alloc);
628 }
629
630 void
gst_kms_allocator_clear_cache(GstAllocator * allocator)631 gst_kms_allocator_clear_cache (GstAllocator * allocator)
632 {
633 GstKMSAllocator *alloc = GST_KMS_ALLOCATOR (allocator);
634 GList *iter;
635
636 GST_OBJECT_LOCK (alloc);
637
638 iter = alloc->priv->mem_cache;
639 while (iter) {
640 GstMiniObject *obj = iter->data;
641 gst_mini_object_weak_unref (obj,
642 (GstMiniObjectNotify) cached_kmsmem_disposed_cb, alloc);
643 gst_mini_object_set_qdata (obj,
644 g_quark_from_static_string ("kmsmem"), NULL, NULL);
645 iter = iter->next;
646 }
647
648 g_list_free (alloc->priv->mem_cache);
649 alloc->priv->mem_cache = NULL;
650
651 GST_OBJECT_UNLOCK (alloc);
652 }
653
654 /* @kmsmem is transfer-full */
655 void
gst_kms_allocator_cache(GstAllocator * allocator,GstMemory * mem,GstMemory * kmsmem)656 gst_kms_allocator_cache (GstAllocator * allocator, GstMemory * mem,
657 GstMemory * kmsmem)
658 {
659 GstKMSAllocator *alloc = GST_KMS_ALLOCATOR (allocator);
660
661 GST_OBJECT_LOCK (alloc);
662 gst_mini_object_weak_ref (GST_MINI_OBJECT (mem),
663 (GstMiniObjectNotify) cached_kmsmem_disposed_cb, alloc);
664 alloc->priv->mem_cache = g_list_prepend (alloc->priv->mem_cache, mem);
665 GST_OBJECT_UNLOCK (alloc);
666
667 gst_mini_object_set_qdata (GST_MINI_OBJECT (mem),
668 g_quark_from_static_string ("kmsmem"), kmsmem,
669 (GDestroyNotify) gst_memory_unref);
670 }
671