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