Add surface proxy that holds a reference to the parent surface
[platform/upstream/gstreamer-vaapi.git] / gst-libs / gst / vaapi / gstvaapisurfaceproxy.c
1 /*
2  *  gstvaapisurfaceproxy.c - VA surface proxy
3  *
4  *  gstreamer-vaapi (C) 2010 Splitted-Desktop Systems
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
19  */
20
21 /**
22  * SECTION:gstvaapisurfaceproxy
23  * @short_description: VA surface proxy
24  */
25
26 #include "config.h"
27 #include "gstvaapisurfaceproxy.h"
28
29 #define DEBUG 1
30 #include "gstvaapidebug.h"
31
32 G_DEFINE_TYPE(GstVaapiSurfaceProxy, gst_vaapi_surface_proxy, G_TYPE_OBJECT);
33
34 enum {
35     PROP_0,
36
37     PROP_CONTEXT,
38     PROP_SURFACE
39 };
40
41 static void
42 gst_vaapi_surface_proxy_finalize(GObject *object)
43 {
44     GstVaapiSurfaceProxy * const proxy = GST_VAAPI_SURFACE_PROXY(object);
45
46     if (proxy->surface) {
47         if (proxy->context)
48             gst_vaapi_context_put_surface(proxy->context, proxy->surface);
49         g_object_unref(proxy->surface);
50         proxy->surface = NULL;
51     }
52
53     if (proxy->context) {
54         g_object_unref(proxy->context);
55         proxy->context = NULL;
56     }
57
58     G_OBJECT_CLASS(gst_vaapi_surface_proxy_parent_class)->finalize(object);
59 }
60
61 static void
62 gst_vaapi_surface_proxy_set_property(
63     GObject      *object,
64     guint         prop_id,
65     const GValue *value,
66     GParamSpec   *pspec
67 )
68 {
69     GstVaapiSurfaceProxy * const proxy = GST_VAAPI_SURFACE_PROXY(object);
70
71     switch (prop_id) {
72     case PROP_CONTEXT:
73         gst_vaapi_surface_proxy_set_context(proxy, g_value_get_pointer(value));
74         break;
75     case PROP_SURFACE:
76         gst_vaapi_surface_proxy_set_surface(proxy, g_value_get_pointer(value));
77         break;
78     default:
79         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
80         break;
81     }
82 }
83
84 static void
85 gst_vaapi_surface_proxy_get_property(
86     GObject    *object,
87     guint       prop_id,
88     GValue     *value,
89     GParamSpec *pspec
90 )
91 {
92     GstVaapiSurfaceProxy * const proxy = GST_VAAPI_SURFACE_PROXY(object);
93
94     switch (prop_id) {
95     case PROP_CONTEXT:
96         g_value_set_pointer(value, gst_vaapi_surface_proxy_get_context(proxy));
97         break;
98     case PROP_SURFACE:
99         g_value_set_pointer(value, gst_vaapi_surface_proxy_get_surface(proxy));
100         break;
101     default:
102         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
103         break;
104     }
105 }
106
107 static void
108 gst_vaapi_surface_proxy_class_init(GstVaapiSurfaceProxyClass *klass)
109 {
110     GObjectClass * const object_class = G_OBJECT_CLASS(klass);
111
112     object_class->finalize     = gst_vaapi_surface_proxy_finalize;
113     object_class->set_property = gst_vaapi_surface_proxy_set_property;
114     object_class->get_property = gst_vaapi_surface_proxy_get_property;
115
116     g_object_class_install_property
117         (object_class,
118          PROP_CONTEXT,
119          g_param_spec_pointer("context",
120                               "Context",
121                               "The context stored in the proxy",
122                               G_PARAM_READWRITE));
123
124     g_object_class_install_property
125         (object_class,
126          PROP_SURFACE,
127          g_param_spec_pointer("surface",
128                               "Surface",
129                               "The surface stored in the proxy",
130                               G_PARAM_READWRITE));
131 }
132
133 static void
134 gst_vaapi_surface_proxy_init(GstVaapiSurfaceProxy *proxy)
135 {
136     proxy->context = NULL;
137     proxy->surface = NULL;
138 }
139
140 /**
141  * gst_vaapi_surface_proxy_new:
142  * @context: a #GstVaapiContext
143  * @surface: a #GstVaapiSurface
144  *
145  * Creates a new #GstVaapiSurfaceProxy with the specified context and
146  * surface.
147  *
148  * Return value: the newly allocated #GstVaapiSurfaceProxy object
149  */
150 GstVaapiSurfaceProxy *
151 gst_vaapi_surface_proxy_new(GstVaapiContext *context, GstVaapiSurface *surface)
152 {
153     g_return_val_if_fail(GST_VAAPI_IS_CONTEXT(context), NULL);
154     g_return_val_if_fail(GST_VAAPI_IS_SURFACE(surface), NULL);
155
156     return g_object_new(GST_VAAPI_TYPE_SURFACE_PROXY,
157                         "context", context,
158                         "surface", surface,
159                         NULL);
160 }
161
162 /**
163  * gst_vaapi_surface_proxy_get_context:
164  * @proxy: a #GstVaapiSurfaceProxy
165  *
166  * Returns the #GstVaapiContext stored in the @proxy.
167  *
168  * Return value: the #GstVaapiContext
169  */
170 GstVaapiContext *
171 gst_vaapi_surface_proxy_get_context(GstVaapiSurfaceProxy *proxy)
172 {
173     g_return_val_if_fail(GST_VAAPI_IS_SURFACE_PROXY(proxy), NULL);
174
175     return proxy->context;
176 }
177
178 /**
179  * gst_vaapi_surface_proxy_set_context:
180  * @proxy: a #GstVaapiSurfaceProxy
181  * @context: the new #GstVaapiContext to be stored in @proxy
182  *
183  * Stores a new @context into the @proxy. The proxy releases the
184  * previous reference, if any, and then holds a reference to the new
185  * @context.
186  */
187 void
188 gst_vaapi_surface_proxy_set_context(
189     GstVaapiSurfaceProxy *proxy,
190     GstVaapiContext      *context
191 )
192 {
193     g_return_if_fail(GST_VAAPI_IS_SURFACE_PROXY(proxy));
194     g_return_if_fail(GST_VAAPI_IS_CONTEXT(context));
195
196     if (proxy->context) {
197         g_object_unref(proxy->context);
198         proxy->context = NULL;
199     }
200
201     if (context)
202         proxy->context = g_object_ref(context);
203 }
204
205 /**
206  * gst_vaapi_surface_proxy_get_surface:
207  * @proxy: a #GstVaapiSurfaceProxy
208  *
209  * Returns the #GstVaapiSurface stored in the @proxy.
210  *
211  * Return value: the #GstVaapiSurface
212  */
213 GstVaapiSurface *
214 gst_vaapi_surface_proxy_get_surface(GstVaapiSurfaceProxy *proxy)
215 {
216     g_return_val_if_fail(GST_VAAPI_IS_SURFACE_PROXY(proxy), NULL);
217
218     return proxy->surface;
219 }
220
221 /**
222  * gst_vaapi_surface_proxy_set_surface:
223  * @proxy: a #GstVaapiSurfaceProxy
224  * @surface: the new #GstVaapiSurface to be stored in @proxy
225  *
226  * Stores a new @surface into the @proxy. The proxy releases the
227  * previous reference, if any, and then holds a reference to the new
228  * @surface.
229  */
230 void
231 gst_vaapi_surface_proxy_set_surface(
232     GstVaapiSurfaceProxy *proxy,
233     GstVaapiSurface      *surface
234 )
235 {
236     g_return_if_fail(GST_VAAPI_IS_SURFACE_PROXY(proxy));
237     g_return_if_fail(GST_VAAPI_IS_SURFACE(surface));
238
239     if (proxy->surface) {
240         g_object_unref(proxy->surface);
241         proxy->surface = NULL;
242     }
243
244     if (surface)
245         proxy->surface = g_object_ref(surface);
246 }