GstVaapiObject: initialize the derived object data with init() hook.
[platform/upstream/gstreamer-vaapi.git] / gst-libs / gst / vaapi / gstvaapiobject.c
index cee67b5..5069dbf 100644 (file)
 /*
  *  gstvaapiobject.c - Base VA object
  *
- *  gstreamer-vaapi (C) 2010 Splitted-Desktop Systems
+ *  Copyright (C) 2010-2011 Splitted-Desktop Systems
+ *    Author: Gwenole Beauchesne <gwenole.beauchesne@splitted-desktop.com>
+ *  Copyright (C) 2012-2013 Intel Corporation
+ *    Author: Gwenole Beauchesne <gwenole.beauchesne@intel.com>
  *
- *  This program is free software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
- *  (at your option) any later version.
+ *  This library is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU Lesser General Public License
+ *  as published by the Free Software Foundation; either version 2.1
+ *  of the License, or (at your option) any later version.
  *
- *  This program is distributed in the hope that it will be useful,
+ *  This library is distributed in the hope that it will be useful,
  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  Lesser General Public License for more details.
  *
- *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
+ *  You should have received a copy of the GNU Lesser General Public
+ *  License along with this library; if not, write to the Free
+ *  Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ *  Boston, MA 02110-1301 USA
  */
 
-#include "config.h"
-#include "gstvaapiobject.h"
+/**
+ * SECTION:gstvaapiobject
+ * @short_description: Base VA object
+ */
 
+#include "sysdeps.h"
+#include "gstvaapiobject.h"
+#include "gstvaapiobject_priv.h"
+#include "gstvaapiminiobject.h"
+#include "gstvaapidisplay_priv.h"
 
 #define DEBUG 1
 #include "gstvaapidebug.h"
 
-G_DEFINE_TYPE(GstVaapiObject, gst_vaapi_object, G_TYPE_OBJECT);
+/* Ensure those symbols are actually defined in the resulting libraries */
+#undef gst_vaapi_object_ref
+#undef gst_vaapi_object_unref
+#undef gst_vaapi_object_replace
 
-#define GST_VAAPI_OBJECT_GET_PRIVATE(obj)                       \
-    (G_TYPE_INSTANCE_GET_PRIVATE((obj),                         \
-                                 GST_VAAPI_TYPE_OBJECT,         \
-                                 GstVaapiObjectPrivate))
+static void
+gst_vaapi_object_finalize(GstVaapiObject *object)
+{
+    const GstVaapiObjectClass * const klass =
+        GST_VAAPI_OBJECT_GET_CLASS(object);
 
-struct _GstVaapiObjectPrivate {
-    GstVaapiDisplay    *display;
-};
+    if (klass->finalize)
+        klass->finalize(object);
+    gst_vaapi_display_replace(&object->display, NULL);
+}
 
-enum {
-    PROP_0,
+void
+gst_vaapi_object_class_init(GstVaapiObjectClass *klass, guint size)
+{
+    GstVaapiMiniObjectClass * const object_class =
+        GST_VAAPI_MINI_OBJECT_CLASS(klass);
 
-    PROP_DISPLAY,
-};
+    object_class->size = size;
+    object_class->finalize = (GDestroyNotify)gst_vaapi_object_finalize;
+}
 
-static void
-gst_vaapi_object_finalize(GObject *object)
+/**
+ * gst_vaapi_object_new:
+ * @klass: The object class
+ * @display: The #GstVaapiDisplay
+ *
+ * Creates a new #GstVaapiObject. If @klass is NULL, then the size of
+ * the allocated object is the same as sizeof(GstVaapiObject).
+ * If @klass is not NULL, typically when a sub-class is implemented,
+ * that pointer shall reference a statically allocated descriptor.
+ *
+ * This function zero-initializes the derived object data. Also note
+ * that this is an internal function that shall not be used outside of
+ * libgstvaapi libraries.
+ *
+ * Returns: The newly allocated #GstVaapiObject
+ */
+gpointer
+gst_vaapi_object_new(const GstVaapiObjectClass *klass, GstVaapiDisplay *display)
 {
-    GstVaapiObjectPrivate * const priv = GST_VAAPI_OBJECT(object)->priv;
+    const GstVaapiMiniObjectClass * const object_class =
+        GST_VAAPI_MINI_OBJECT_CLASS(klass);
+    GstVaapiObject *object;
+    guint sub_size;
 
-    if (priv->display) {
-        g_object_unref(priv->display);
-        priv->display = NULL;
-    }
+    g_return_val_if_fail(display != NULL, NULL);
 
-    G_OBJECT_CLASS(gst_vaapi_object_parent_class)->finalize(object);
-}
+    object = (GstVaapiObject *)gst_vaapi_mini_object_new(object_class);
+    if (!object)
+        return NULL;
 
-static void
-gst_vaapi_object_set_property(
-    GObject      *gobject,
-    guint         prop_id,
-    const GValue *value,
-    GParamSpec   *pspec
-)
-{
-    GstVaapiObject * const object = GST_VAAPI_OBJECT(gobject);
-
-    switch (prop_id) {
-    case PROP_DISPLAY:
-        object->priv->display = g_object_ref(g_value_get_object(value));
-        break;
-    default:
-        G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, prop_id, pspec);
-        break;
-    }
+    object->display     = gst_vaapi_display_ref(display);
+    object->object_id   = VA_INVALID_ID;
+
+    sub_size = object_class->size - sizeof(*object);
+    if (sub_size > 0)
+        memset(((guchar *)object) + sizeof(*object), 0, sub_size);
+
+    if (klass && klass->init)
+        klass->init (object);
+    return object;
 }
 
-static void
-gst_vaapi_object_get_property(
-    GObject    *gobject,
-    guint       prop_id,
-    GValue     *value,
-    GParamSpec *pspec
-)
+/**
+ * gst_vaapi_object_ref:
+ * @object: a #GstVaapiObject
+ *
+ * Atomically increases the reference count of the given @object by one.
+ *
+ * Returns: The same @object argument
+ */
+gpointer
+gst_vaapi_object_ref(gpointer object)
 {
-    GstVaapiObject * const object = GST_VAAPI_OBJECT(gobject);
-
-    switch (prop_id) {
-    case PROP_DISPLAY:
-        g_value_set_object(value, gst_vaapi_object_get_display(object));
-        break;
-    default:
-        G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, prop_id, pspec);
-        break;
-    }
+    return gst_vaapi_object_ref_internal(object);
 }
 
-static void
-gst_vaapi_object_class_init(GstVaapiObjectClass *klass)
+/**
+ * gst_vaapi_object_unref:
+ * @object: a #GstVaapiObject
+ *
+ * Atomically decreases the reference count of the @object by one. If
+ * the reference count reaches zero, the object will be free'd.
+ */
+void
+gst_vaapi_object_unref(gpointer object)
 {
-    GObjectClass * const object_class = G_OBJECT_CLASS(klass);
-
-    g_type_class_add_private(klass, sizeof(GstVaapiObjectPrivate));
-
-    object_class->finalize     = gst_vaapi_object_finalize;
-    object_class->set_property = gst_vaapi_object_set_property;
-    object_class->get_property = gst_vaapi_object_get_property;
-
-    /**
-     * GstVaapiObject:display:
-     *
-     * The #GstVaapiDisplay this object is bound to.
-     */
-    g_object_class_install_property
-        (object_class,
-         PROP_DISPLAY,
-         g_param_spec_object("display",
-                             "Display",
-                             "The GstVaapiDisplay this object is bound to",
-                             GST_VAAPI_TYPE_DISPLAY,
-                             G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
+    gst_vaapi_object_unref_internal(object);
 }
 
-static void
-gst_vaapi_object_init(GstVaapiObject *object)
+/**
+ * gst_vaapi_object_replace:
+ * @old_object_ptr: a pointer to a #GstVaapiObject
+ * @new_object: a #GstVaapiObject
+ *
+ * Atomically replaces the object object held in @old_object_ptr with
+ * @new_object. This means that @old_object_ptr shall reference a
+ * valid object. However, @new_object can be NULL.
+ */
+void
+gst_vaapi_object_replace(gpointer old_object_ptr, gpointer new_object)
 {
-    GstVaapiObjectPrivate *priv = GST_VAAPI_OBJECT_GET_PRIVATE(object);
-
-    object->priv  = priv;
-    priv->display = NULL;
+    gst_vaapi_object_replace_internal(old_object_ptr, new_object);
 }
 
 /**
@@ -141,7 +157,55 @@ gst_vaapi_object_init(GstVaapiObject *object)
 GstVaapiDisplay *
 gst_vaapi_object_get_display(GstVaapiObject *object)
 {
-    g_return_val_if_fail(GST_VAAPI_IS_OBJECT(object), NULL);
+    g_return_val_if_fail(object != NULL, NULL);
+
+    return GST_VAAPI_OBJECT_DISPLAY(object);
+}
+
+/**
+ * gst_vaapi_object_lock_display:
+ * @object: a #GstVaapiObject
+ *
+ * Locks @object parent display. If display is already locked by
+ * another thread, the current thread will block until display is
+ * unlocked by the other thread.
+ */
+void
+gst_vaapi_object_lock_display(GstVaapiObject *object)
+{
+    g_return_if_fail(object != NULL);
+
+    GST_VAAPI_OBJECT_LOCK_DISPLAY(object);
+}
+
+/**
+ * gst_vaapi_object_unlock_display:
+ * @object: a #GstVaapiObject
+ *
+ * Unlocks @object parent display. If another thread is blocked in a
+ * gst_vaapi_object_lock_display() call, it will be woken and can lock
+ * display itself.
+ */
+void
+gst_vaapi_object_unlock_display(GstVaapiObject *object)
+{
+    g_return_if_fail(object != NULL);
+
+    GST_VAAPI_OBJECT_UNLOCK_DISPLAY(object);
+}
+
+/**
+ * gst_vaapi_object_get_id:
+ * @object: a #GstVaapiObject
+ *
+ * Returns the #GstVaapiID contained in the @object.
+ *
+ * Return value: the #GstVaapiID of the @object
+ */
+GstVaapiID
+gst_vaapi_object_get_id(GstVaapiObject *object)
+{
+    g_return_val_if_fail(object != NULL, 0);
 
-    return object->priv->display;
+    return GST_VAAPI_OBJECT_ID(object);
 }