Merge branch 'move_subdir_good' into tizen_gst_1.19.2_mono
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-good / sys / v4l2 / gstv4l2allocator.h
1 /*
2  * Copyright (C) 2014 Collabora Ltd.
3  *     Author: Nicolas Dufresne <nicolas.dufresne@collabora.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  *
20  */
21
22
23 #ifndef __GST_V4L2_ALLOCATOR_H__
24 #define __GST_V4L2_ALLOCATOR_H__
25
26 #include "ext/videodev2.h"
27 #include <gst/gst.h>
28 #include <gst/gstatomicqueue.h>
29 #ifdef TIZEN_FEATURE_V4L2_TBM_SUPPORT
30 #include <tbm_surface.h>
31 #include <tbm_surface_internal.h>
32 #endif /* TIZEN_FEATURE_V4L2_TBM_SUPPORT */
33
34 G_BEGIN_DECLS
35
36 #define GST_TYPE_V4L2_ALLOCATOR                 (gst_v4l2_allocator_get_type())
37 #define GST_IS_V4L2_ALLOCATOR(obj)              (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_V4L2_ALLOCATOR))
38 #define GST_IS_V4L2_ALLOCATOR_CLASS(klass)      (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_V4L2_ALLOCATOR))
39 #define GST_V4L2_ALLOCATOR_GET_CLASS(obj)       (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_V4L2_ALLOCATOR, GstV4l2AllocatorClass))
40 #define GST_V4L2_ALLOCATOR(obj)                 (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_V4L2_ALLOCATOR, GstV4l2Allocator))
41 #define GST_V4L2_ALLOCATOR_CLASS(klass)         (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_V4L2_ALLOCATOR, GstV4l2AllocatorClass))
42 #define GST_V4L2_ALLOCATOR_CAST(obj)            ((GstV4l2Allocator *)(obj))
43
44 #define GST_V4L2_ALLOCATOR_CAN_REQUEST(obj,type) \
45         (GST_OBJECT_FLAG_IS_SET (obj, GST_V4L2_ALLOCATOR_FLAG_ ## type ## _REQBUFS))
46 #define GST_V4L2_ALLOCATOR_CAN_ALLOCATE(obj,type) \
47         (GST_OBJECT_FLAG_IS_SET (obj, GST_V4L2_ALLOCATOR_FLAG_ ## type ## _CREATE_BUFS))
48 #define GST_V4L2_ALLOCATOR_CAN_ORPHAN_BUFS(obj) \
49         (GST_OBJECT_FLAG_IS_SET (obj, GST_V4L2_ALLOCATOR_FLAG_SUPPORTS_ORPHANED_BUFS))
50 #define GST_V4L2_ALLOCATOR_IS_ORPHANED(obj) \
51         (GST_OBJECT_FLAG_IS_SET (obj, GST_V4L2_ALLOCATOR_FLAG_ORPHANED))
52
53 #define GST_V4L2_MEMORY_QUARK gst_v4l2_memory_quark ()
54
55 typedef struct _GstV4l2Allocator GstV4l2Allocator;
56 typedef struct _GstV4l2AllocatorClass GstV4l2AllocatorClass;
57 typedef struct _GstV4l2MemoryGroup GstV4l2MemoryGroup;
58 typedef struct _GstV4l2Memory GstV4l2Memory;
59 typedef enum _GstV4l2Capabilities GstV4l2Capabilities;
60 typedef enum _GstV4l2Return GstV4l2Return;
61
62 enum _GstV4l2AllocatorFlags
63 {
64   GST_V4L2_ALLOCATOR_FLAG_MMAP_REQBUFS        = (GST_ALLOCATOR_FLAG_LAST << 0),
65   GST_V4L2_ALLOCATOR_FLAG_MMAP_CREATE_BUFS    = (GST_ALLOCATOR_FLAG_LAST << 1),
66   GST_V4L2_ALLOCATOR_FLAG_USERPTR_REQBUFS     = (GST_ALLOCATOR_FLAG_LAST << 2),
67   GST_V4L2_ALLOCATOR_FLAG_USERPTR_CREATE_BUFS = (GST_ALLOCATOR_FLAG_LAST << 3),
68   GST_V4L2_ALLOCATOR_FLAG_DMABUF_REQBUFS      = (GST_ALLOCATOR_FLAG_LAST << 4),
69   GST_V4L2_ALLOCATOR_FLAG_DMABUF_CREATE_BUFS  = (GST_ALLOCATOR_FLAG_LAST << 5),
70   GST_V4L2_ALLOCATOR_FLAG_SUPPORTS_ORPHANED_BUFS = (GST_ALLOCATOR_FLAG_LAST << 6),
71   GST_V4L2_ALLOCATOR_FLAG_ORPHANED            = (GST_ALLOCATOR_FLAG_LAST << 7),
72 };
73
74 enum _GstV4l2Return
75 {
76   GST_V4L2_OK = 0,
77   GST_V4L2_ERROR = -1,
78   GST_V4L2_BUSY = -2
79 };
80
81 struct _GstV4l2Memory
82 {
83   GstMemory mem;
84   gint plane;
85   GstV4l2MemoryGroup *group;
86   gpointer data;
87   gint dmafd;
88 };
89
90 struct _GstV4l2MemoryGroup
91 {
92   gint n_mem;
93   GstMemory * mem[VIDEO_MAX_PLANES];
94   gint mems_allocated;
95   struct v4l2_buffer buffer;
96   struct v4l2_plane planes[VIDEO_MAX_PLANES];
97 #ifdef TIZEN_FEATURE_V4L2_TBM_SUPPORT
98   tbm_surface_h surface;
99 #endif /* TIZEN_FEATURE_V4L2_TBM_SUPPORT */
100 };
101
102 struct _GstV4l2Allocator
103 {
104   GstAllocator parent;
105   GstV4l2Object *obj;
106   guint32 count;
107   guint32 memory;
108   gboolean can_allocate;
109   gboolean active;
110
111   GstV4l2MemoryGroup * groups[VIDEO_MAX_FRAME];
112   GstAtomicQueue *free_queue;
113   GstAtomicQueue *pending_queue;
114 #ifdef TIZEN_FEATURE_V4L2_TBM_SUPPORT
115   tbm_bufmgr bufmgr;
116   tbm_surface_info_s s_info;
117   gint live_buffer_count;
118 #endif /* TIZEN_FEATURE_V4L2_TBM_SUPPORT */
119 };
120
121 struct _GstV4l2AllocatorClass {
122   GstAllocatorClass parent_class;
123 };
124
125 GType gst_v4l2_allocator_get_type(void);
126
127 gboolean             gst_is_v4l2_memory                (GstMemory * mem);
128
129 GQuark               gst_v4l2_memory_quark             (void);
130
131 gboolean             gst_v4l2_allocator_is_active      (GstV4l2Allocator * allocator);
132
133 guint                gst_v4l2_allocator_get_size       (GstV4l2Allocator * allocator);
134
135 GstV4l2Allocator*    gst_v4l2_allocator_new            (GstObject *parent, GstV4l2Object * obj);
136
137 guint                gst_v4l2_allocator_start          (GstV4l2Allocator * allocator,
138                                                         guint32 count, guint32 memory);
139
140 GstV4l2Return        gst_v4l2_allocator_stop           (GstV4l2Allocator * allocator);
141
142 gboolean             gst_v4l2_allocator_orphan         (GstV4l2Allocator * allocator);
143
144 GstV4l2MemoryGroup*  gst_v4l2_allocator_alloc_mmap     (GstV4l2Allocator * allocator);
145
146 GstV4l2MemoryGroup*  gst_v4l2_allocator_alloc_dmabuf   (GstV4l2Allocator * allocator,
147                                                         GstAllocator * dmabuf_allocator);
148
149 GstV4l2MemoryGroup * gst_v4l2_allocator_alloc_dmabufin (GstV4l2Allocator * allocator);
150
151 GstV4l2MemoryGroup * gst_v4l2_allocator_alloc_userptr  (GstV4l2Allocator * allocator);
152
153 gboolean             gst_v4l2_allocator_import_dmabuf  (GstV4l2Allocator * allocator,
154                                                         GstV4l2MemoryGroup *group,
155                                                         gint n_mem, GstMemory ** dma_mem);
156
157 gboolean             gst_v4l2_allocator_import_userptr (GstV4l2Allocator * allocator,
158                                                         GstV4l2MemoryGroup *group,
159                                                         gsize img_size, int n_planes,
160                                                         gpointer * data, gsize * size);
161
162 void                 gst_v4l2_allocator_flush          (GstV4l2Allocator * allocator);
163
164 gboolean             gst_v4l2_allocator_qbuf           (GstV4l2Allocator * allocator,
165                                                         GstV4l2MemoryGroup * group);
166
167 GstFlowReturn        gst_v4l2_allocator_dqbuf          (GstV4l2Allocator * allocator,
168                                                         GstV4l2MemoryGroup ** group);
169
170 void                 gst_v4l2_allocator_reset_group    (GstV4l2Allocator * allocator,
171                                                         GstV4l2MemoryGroup * group);
172
173 G_END_DECLS
174
175 #endif /* __GST_V4L2_ALLOCATOR_H__ */