libs: simplify GstVaapiID definitions.
[platform/upstream/gstreamer-vaapi.git] / gst-libs / gst / vaapi / gstvaapiobject.c
1 /*
2  *  gstvaapiobject.c - Base VA object
3  *
4  *  Copyright (C) 2010-2011 Splitted-Desktop Systems
5  *  Copyright (C) 2012 Intel Corporation
6  *
7  *  This library is free software; you can redistribute it and/or
8  *  modify it under the terms of the GNU Lesser General Public License
9  *  as published by the Free Software Foundation; either version 2.1
10  *  of the License, or (at your option) any later version.
11  *
12  *  This library is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  *  Lesser General Public License for more details.
16  *
17  *  You should have received a copy of the GNU Lesser General Public
18  *  License along with this library; if not, write to the Free
19  *  Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  *  Boston, MA 02110-1301 USA
21  */
22
23 /**
24  * SECTION:gstvaapiobject
25  * @short_description: Base VA object
26  */
27
28 #include "sysdeps.h"
29 #include "gstvaapiobject.h"
30 #include "gstvaapiobject_priv.h"
31 #include "gstvaapiminiobject.h"
32 #include "gstvaapidisplay_priv.h"
33
34 #define DEBUG 1
35 #include "gstvaapidebug.h"
36
37 /* Ensure those symbols are actually defined in the resulting libraries */
38 #undef gst_vaapi_object_ref
39 #undef gst_vaapi_object_unref
40 #undef gst_vaapi_object_replace
41
42 static void
43 gst_vaapi_object_finalize(GstVaapiObject *object)
44 {
45     const GstVaapiObjectClass * const klass =
46         GST_VAAPI_OBJECT_GET_CLASS(object);
47
48     if (klass->finalize)
49         klass->finalize(object);
50     gst_vaapi_display_replace(&object->display, NULL);
51 }
52
53 void
54 gst_vaapi_object_class_init(GstVaapiObjectClass *klass, guint size)
55 {
56     GstVaapiMiniObjectClass * const object_class =
57         GST_VAAPI_MINI_OBJECT_CLASS(klass);
58
59     object_class->size = size;
60     object_class->finalize = (GDestroyNotify)gst_vaapi_object_finalize;
61 }
62
63 /**
64  * gst_vaapi_object_new:
65  * @object_class: The object class
66  *
67  * Creates a new #GstVaapiObject. If @object_class is NULL, then the
68  * size of the allocated object is the same as sizeof(GstVaapiObject).
69  * If @object_class is not NULL, typically when a sub-class is implemented,
70  * that pointer shall reference a statically allocated descriptor.
71  *
72  * This function zero-initializes the derived object data. Also note
73  * that this is an internal function that shall not be used outside of
74  * libgstvaapi libraries.
75  *
76  * Returns: The newly allocated #GstVaapiObject
77  */
78 gpointer
79 gst_vaapi_object_new(const GstVaapiObjectClass *klass, GstVaapiDisplay *display)
80 {
81     const GstVaapiMiniObjectClass * const object_class =
82         GST_VAAPI_MINI_OBJECT_CLASS(klass);
83     GstVaapiObject *object;
84     guint sub_size;
85
86     g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), NULL);
87
88     object = (GstVaapiObject *)gst_vaapi_mini_object_new(object_class);
89     if (!object)
90         return NULL;
91
92     object->display     = gst_vaapi_display_ref(display);
93     object->object_id   = VA_INVALID_ID;
94
95     sub_size = object_class->size - sizeof(*object);
96     if (sub_size > 0)
97         memset(((guchar *)object) + sizeof(*object), 0, sub_size);
98     return object;
99 }
100
101 /**
102  * gst_vaapi_object_ref:
103  * @object: a #GstVaapiObject
104  *
105  * Atomically increases the reference count of the given @object by one.
106  *
107  * Returns: The same @object argument
108  */
109 gpointer
110 gst_vaapi_object_ref(gpointer object)
111 {
112     return gst_vaapi_object_ref_internal(object);
113 }
114
115 /**
116  * gst_vaapi_object_unref:
117  * @object: a #GstVaapiObject
118  *
119  * Atomically decreases the reference count of the @object by one. If
120  * the reference count reaches zero, the object will be free'd.
121  */
122 void
123 gst_vaapi_object_unref(gpointer object)
124 {
125     gst_vaapi_object_unref_internal(object);
126 }
127
128 /**
129  * gst_vaapi_object_replace:
130  * @old_object_ptr: a pointer to a #GstVaapiObject
131  * @new_object: a #GstVaapiObject
132  *
133  * Atomically replaces the object object held in @old_object_ptr with
134  * @new_object. This means that @old_object_ptr shall reference a
135  * valid object. However, @new_object can be NULL.
136  */
137 void
138 gst_vaapi_object_replace(gpointer old_object_ptr, gpointer new_object)
139 {
140     gst_vaapi_object_replace_internal(old_object_ptr, new_object);
141 }
142
143 /**
144  * gst_vaapi_object_get_display:
145  * @object: a #GstVaapiObject
146  *
147  * Returns the #GstVaapiDisplay this @object is bound to.
148  *
149  * Return value: the parent #GstVaapiDisplay object
150  */
151 GstVaapiDisplay *
152 gst_vaapi_object_get_display(GstVaapiObject *object)
153 {
154     g_return_val_if_fail(GST_VAAPI_IS_OBJECT(object), NULL);
155
156     return GST_VAAPI_OBJECT_DISPLAY(object);
157 }
158
159 /**
160  * gst_vaapi_object_lock_display:
161  * @object: a #GstVaapiObject
162  *
163  * Locks @object parent display. If display is already locked by
164  * another thread, the current thread will block until display is
165  * unlocked by the other thread.
166  */
167 void
168 gst_vaapi_object_lock_display(GstVaapiObject *object)
169 {
170     g_return_if_fail(GST_VAAPI_IS_OBJECT(object));
171
172     GST_VAAPI_OBJECT_LOCK_DISPLAY(object);
173 }
174
175 /**
176  * gst_vaapi_object_unlock_display:
177  * @object: a #GstVaapiObject
178  *
179  * Unlocks @object parent display. If another thread is blocked in a
180  * gst_vaapi_object_lock_display() call, it will be woken and can lock
181  * display itself.
182  */
183 void
184 gst_vaapi_object_unlock_display(GstVaapiObject *object)
185 {
186     g_return_if_fail(GST_VAAPI_IS_OBJECT(object));
187
188     GST_VAAPI_OBJECT_UNLOCK_DISPLAY(object);
189 }
190
191 /**
192  * gst_vaapi_object_get_id:
193  * @object: a #GstVaapiObject
194  *
195  * Returns the #GstVaapiID contained in the @object.
196  *
197  * Return value: the #GstVaapiID of the @object
198  */
199 GstVaapiID
200 gst_vaapi_object_get_id(GstVaapiObject *object)
201 {
202     g_return_val_if_fail(GST_VAAPI_IS_OBJECT(object), 0);
203
204     return GST_VAAPI_OBJECT_ID(object);
205 }