kms: rename variable used
[platform/upstream/gstreamer.git] / sys / kms / gstkmsallocator.c
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 <xf86drm.h>
31 #include <xf86drmMode.h>
32 #include <string.h>
33 #include <unistd.h>
34
35 #include "gstkmsallocator.h"
36 #include "gstkmsutils.h"
37
38 #define GST_CAT_DEFAULT kmsallocator_debug
39 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
40
41 #define GST_KMS_MEMORY_TYPE "KMSMemory"
42
43 struct _GstKMSAllocatorPrivate
44 {
45   int fd;
46   struct kms_driver *driver;
47 };
48
49 #define parent_class gst_kms_allocator_parent_class
50 G_DEFINE_TYPE_WITH_CODE (GstKMSAllocator, gst_kms_allocator, GST_TYPE_ALLOCATOR,
51     G_ADD_PRIVATE (GstKMSAllocator);
52     GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "kmsallocator", 0,
53         "KMS allocator"));
54
55 enum
56 {
57   PROP_DRM_FD = 1,
58   PROP_N,
59 };
60
61 static GParamSpec *g_props[PROP_N] = { NULL, };
62
63 gboolean
64 gst_is_kms_memory (GstMemory * mem)
65 {
66   return gst_memory_is_type (mem, GST_KMS_MEMORY_TYPE);
67 }
68
69 guint32
70 gst_kms_memory_get_fb_id (GstMemory * mem)
71 {
72   if (!gst_is_kms_memory (mem))
73     return 0;
74   return ((GstKMSMemory *) mem)->fb_id;
75 }
76
77 static gboolean
78 ensure_kms_driver (GstKMSAllocator * alloc)
79 {
80   GstKMSAllocatorPrivate *priv;
81   int err;
82
83   priv = alloc->priv;
84
85   if (priv->driver)
86     return TRUE;
87
88   if (priv->fd < 0)
89     return FALSE;
90
91   err = kms_create (priv->fd, &priv->driver);
92   if (err) {
93     GST_ERROR_OBJECT (alloc, "Could not create KMS driver: %s",
94         strerror (-err));
95     return FALSE;
96   }
97
98   return TRUE;
99 }
100
101 static void
102 gst_kms_allocator_memory_reset (GstKMSAllocator * allocator, GstKMSMemory * mem)
103 {
104   if (mem->fb_id) {
105     GST_DEBUG_OBJECT (allocator, "removing fb id %d", mem->fb_id);
106     drmModeRmFB (allocator->priv->fd, mem->fb_id);
107     mem->fb_id = 0;
108   }
109
110   if (!ensure_kms_driver (allocator))
111     return;
112
113   if (mem->bo) {
114     kms_bo_destroy (&mem->bo);
115     mem->bo = NULL;
116   }
117 }
118
119 static gboolean
120 gst_kms_allocator_memory_create (GstKMSAllocator * allocator,
121     GstKMSMemory * kmsmem, GstVideoInfo * vinfo)
122 {
123   gint ret;
124   guint attrs[] = {
125     KMS_WIDTH, GST_VIDEO_INFO_WIDTH (vinfo),
126     KMS_HEIGHT, GST_VIDEO_INFO_HEIGHT (vinfo),
127     KMS_TERMINATE_PROP_LIST,
128   };
129
130   if (kmsmem->bo)
131     return TRUE;
132
133   if (!ensure_kms_driver (allocator))
134     return FALSE;
135
136   ret = kms_bo_create (allocator->priv->driver, attrs, &kmsmem->bo);
137   if (ret) {
138     GST_ERROR_OBJECT (allocator, "Failed to create buffer object: %s (%d)",
139         strerror (-ret), ret);
140     return FALSE;
141   }
142
143   return TRUE;
144 }
145
146 static void
147 gst_kms_allocator_free (GstAllocator * allocator, GstMemory * mem)
148 {
149   GstKMSAllocator *alloc;
150   GstKMSMemory *kmsmem;
151
152   alloc = GST_KMS_ALLOCATOR (allocator);
153   kmsmem = (GstKMSMemory *) mem;
154
155   gst_kms_allocator_memory_reset (alloc, kmsmem);
156   g_slice_free (GstKMSMemory, kmsmem);
157 }
158
159 static void
160 gst_kms_allocator_set_property (GObject * object, guint prop_id,
161     const GValue * value, GParamSpec * pspec)
162 {
163   GstKMSAllocator *alloc;
164
165   alloc = GST_KMS_ALLOCATOR (object);
166
167   switch (prop_id) {
168     case PROP_DRM_FD:{
169       int fd = g_value_get_int (value);
170       if (fd > -1)
171         alloc->priv->fd = dup (fd);
172       break;
173     }
174     default:
175       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
176       break;
177   }
178 }
179
180 static void
181 gst_kms_allocator_get_property (GObject * object, guint prop_id,
182     GValue * value, GParamSpec * pspec)
183 {
184   GstKMSAllocator *alloc;
185
186   alloc = GST_KMS_ALLOCATOR (object);
187
188   switch (prop_id) {
189     case PROP_DRM_FD:
190       g_value_set_int (value, alloc->priv->fd);
191       break;
192     default:
193       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
194       break;
195   }
196 }
197
198 static void
199 gst_kms_allocator_finalize (GObject * obj)
200 {
201   GstKMSAllocator *alloc;
202
203   alloc = GST_KMS_ALLOCATOR (obj);
204
205   if (alloc->priv->driver)
206     kms_destroy (&alloc->priv->driver);
207
208   if (alloc->priv->fd > -1)
209     close (alloc->priv->fd);
210
211   G_OBJECT_CLASS (parent_class)->finalize (obj);
212 }
213
214 static void
215 gst_kms_allocator_class_init (GstKMSAllocatorClass * klass)
216 {
217   GObjectClass *gobject_class;
218   GstAllocatorClass *allocator_class;
219
220   allocator_class = GST_ALLOCATOR_CLASS (klass);
221   gobject_class = G_OBJECT_CLASS (klass);
222
223   allocator_class->free = gst_kms_allocator_free;
224
225   gobject_class->set_property = gst_kms_allocator_set_property;
226   gobject_class->get_property = gst_kms_allocator_get_property;
227   gobject_class->finalize = gst_kms_allocator_finalize;
228
229   g_props[PROP_DRM_FD] = g_param_spec_int ("drm-fd", "DRM fd",
230       "DRM file descriptor", -1, G_MAXINT, -1,
231       G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
232
233   g_object_class_install_properties (gobject_class, PROP_N, g_props);
234 }
235
236 static gpointer
237 gst_kms_memory_map (GstMemory * mem, gsize maxsize, GstMapFlags flags)
238 {
239   GstKMSMemory *kmsmem;
240   int err;
241   gpointer out;
242
243   if (!ensure_kms_driver ((GstKMSAllocator *) mem->allocator))
244     return NULL;
245
246   kmsmem = (GstKMSMemory *) mem;
247   if (!kmsmem->bo)
248     return NULL;
249
250   out = NULL;
251   err = kms_bo_map (kmsmem->bo, &out);
252   if (err) {
253     GST_ERROR ("could not map memory: %s %d", strerror (-err), err);
254     return NULL;
255   }
256
257   return out;
258 }
259
260 static void
261 gst_kms_memory_unmap (GstMemory * mem)
262 {
263   GstKMSMemory *kmsmem;
264
265   if (!ensure_kms_driver ((GstKMSAllocator *) mem->allocator))
266     return;
267
268   kmsmem = (GstKMSMemory *) mem;
269   if (kmsmem->bo)
270     kms_bo_unmap (kmsmem->bo);
271 }
272
273 static void
274 gst_kms_allocator_init (GstKMSAllocator * allocator)
275 {
276   GstAllocator *alloc;
277
278   alloc = GST_ALLOCATOR_CAST (allocator);
279
280   allocator->priv = gst_kms_allocator_get_instance_private (allocator);
281   allocator->priv->fd = -1;
282
283   alloc->mem_type = GST_KMS_MEMORY_TYPE;
284   alloc->mem_map = gst_kms_memory_map;
285   alloc->mem_unmap = gst_kms_memory_unmap;
286   /* Use the default, fallback copy function */
287
288   GST_OBJECT_FLAG_SET (allocator, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC);
289 }
290
291 GstAllocator *
292 gst_kms_allocator_new (int fd)
293 {
294   return g_object_new (GST_TYPE_KMS_ALLOCATOR, "name",
295       "KMSMemory::allocator", "drm-fd", fd, NULL);
296 }
297
298 /* The mem_offsets are relative to the GstMemory start, unlike the vinfo->offset
299  * which are relative to the GstBuffer start. */
300 static gboolean
301 gst_kms_allocator_add_fb (GstKMSAllocator * alloc, GstKMSMemory * kmsmem,
302     gsize mem_offsets[GST_VIDEO_MAX_PLANES], GstVideoInfo * vinfo)
303 {
304   int i, ret;
305   gint num_planes = GST_VIDEO_INFO_N_PLANES (vinfo);
306   guint32 w, h, fmt, pitch = 0, bo_handles[4] = { 0, };
307   guint32 offsets[4] = { 0, };
308   guint32 pitches[4] = { 0, };
309
310   if (kmsmem->fb_id)
311     return TRUE;
312
313   w = GST_VIDEO_INFO_WIDTH (vinfo);
314   h = GST_VIDEO_INFO_HEIGHT (vinfo);
315   fmt = gst_drm_format_from_video (GST_VIDEO_INFO_FORMAT (vinfo));
316
317   if (kmsmem->bo) {
318     kms_bo_get_prop (kmsmem->bo, KMS_HANDLE, &bo_handles[0]);
319     for (i = 1; i < num_planes; i++)
320       bo_handles[i] = bo_handles[0];
321
322     /* Get the bo pitch calculated by the kms driver.
323      * If it's defined, it will overwrite the video info's stride */
324     kms_bo_get_prop (kmsmem->bo, KMS_PITCH, &pitch);
325   } else {
326     for (i = 0; i < num_planes; i++)
327       bo_handles[i] = kmsmem->gem_handle[i];
328   }
329
330   GST_DEBUG_OBJECT (alloc, "bo handles: %d, %d, %d, %d", bo_handles[0],
331       bo_handles[1], bo_handles[2], bo_handles[3]);
332
333   for (i = 0; i < num_planes; i++) {
334     offsets[i] = mem_offsets[i];
335     if (pitch)
336       GST_VIDEO_INFO_PLANE_STRIDE (vinfo, i) = pitch;
337     pitches[i] = GST_VIDEO_INFO_PLANE_STRIDE (vinfo, i);
338     GST_DEBUG_OBJECT (alloc, "Create FB plane %i with stride %u and offset %u",
339         i, pitches[i], offsets[i]);
340   }
341
342   ret = drmModeAddFB2 (alloc->priv->fd, w, h, fmt, bo_handles, pitches,
343       offsets, &kmsmem->fb_id, 0);
344   if (ret) {
345     GST_ERROR_OBJECT (alloc, "Failed to bind to framebuffer: %s (%d)",
346         strerror (-ret), ret);
347     return FALSE;
348   }
349   return TRUE;
350 }
351
352 static GstMemory *
353 gst_kms_allocator_alloc_empty (GstAllocator * allocator, GstVideoInfo * vinfo)
354 {
355   GstKMSMemory *kmsmem;
356   GstMemory *mem;
357
358   kmsmem = g_slice_new0 (GstKMSMemory);
359   if (!kmsmem)
360     return NULL;
361   mem = GST_MEMORY_CAST (kmsmem);
362
363   gst_memory_init (mem, GST_MEMORY_FLAG_NO_SHARE, allocator, NULL,
364       GST_VIDEO_INFO_SIZE (vinfo), 0, 0, GST_VIDEO_INFO_SIZE (vinfo));
365
366   return mem;
367 }
368
369 GstMemory *
370 gst_kms_allocator_bo_alloc (GstAllocator * allocator, GstVideoInfo * vinfo)
371 {
372   GstKMSAllocator *alloc;
373   GstKMSMemory *kmsmem;
374   GstMemory *mem;
375
376   mem = gst_kms_allocator_alloc_empty (allocator, vinfo);
377   if (!mem)
378     return NULL;
379
380   alloc = GST_KMS_ALLOCATOR (allocator);
381   kmsmem = (GstKMSMemory *) mem;
382   if (!gst_kms_allocator_memory_create (alloc, kmsmem, vinfo))
383     goto fail;
384   if (!gst_kms_allocator_add_fb (alloc, kmsmem, vinfo->offset, vinfo))
385     goto fail;
386
387   return mem;
388
389   /* ERRORS */
390 fail:
391   gst_memory_unref (mem);
392   return NULL;
393 }
394
395 GstKMSMemory *
396 gst_kms_allocator_dmabuf_import (GstAllocator * allocator, gint * prime_fds,
397     gint n_planes, gsize offsets[GST_VIDEO_MAX_PLANES], GstVideoInfo * vinfo)
398 {
399   GstKMSAllocator *alloc;
400   GstMemory *mem;
401   GstKMSMemory *tmp;
402   gint i, ret;
403
404   g_return_val_if_fail (n_planes <= GST_VIDEO_MAX_PLANES, FALSE);
405
406   mem = gst_kms_allocator_alloc_empty (allocator, vinfo);
407   if (!mem)
408     return FALSE;
409
410   tmp = (GstKMSMemory *) mem;
411   alloc = GST_KMS_ALLOCATOR (allocator);
412   for (i = 0; i < n_planes; i++) {
413     ret = drmPrimeFDToHandle (alloc->priv->fd, prime_fds[i],
414         &tmp->gem_handle[i]);
415     if (ret)
416       goto import_fd_failed;
417   }
418
419   if (!gst_kms_allocator_add_fb (alloc, tmp, offsets, vinfo))
420     goto failed;
421
422   return tmp;
423
424   /* ERRORS */
425 import_fd_failed:
426   {
427     GST_ERROR_OBJECT (alloc, "Failed to import prime fd %d: %s (%d)",
428         prime_fds[i], strerror (-ret), ret);
429     /* fallback */
430   }
431
432 failed:
433   {
434     gst_memory_unref (mem);
435     return NULL;
436   }
437 }