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