Tizen 2.0 Release
[framework/multimedia/gstreamer-vaapi.git] / gst-libs / gst / vaapi / gstvaapiwindow_wayland.c
1 /*
2  *  gstvaapiwindow_wayland.c - VA/Wayland window abstraction
3  *
4  *  Copyright (C) 2012 Intel Corporation
5  *
6  *  This library is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU Lesser General Public License
8  *  as published by the Free Software Foundation; either version 2.1
9  *  of the License, or (at your option) any later version.
10  *
11  *  This library 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 GNU
14  *  Lesser General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Lesser General Public
17  *  License along with this library; if not, write to the Free
18  *  Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  *  Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * SECTION:gstvaapiwindow_wayland
24  * @short_description: VA/Wayland window abstraction
25  */
26
27 #include "sysdeps.h"
28 #include <string.h>
29 #include "gstvaapicompat.h"
30 #include "gstvaapiwindow_wayland.h"
31 #include "gstvaapidisplay_wayland.h"
32 #include "gstvaapidisplay_wayland_priv.h"
33 #include "gstvaapiutils.h"
34 #include "gstvaapi_priv.h"
35
36 #define DEBUG 1
37 #include "gstvaapidebug.h"
38
39 G_DEFINE_TYPE(GstVaapiWindowWayland,
40               gst_vaapi_window_wayland,
41               GST_VAAPI_TYPE_WINDOW);
42
43 #define GST_VAAPI_WINDOW_WAYLAND_GET_PRIVATE(obj)               \
44     (G_TYPE_INSTANCE_GET_PRIVATE((obj),                         \
45                                  GST_VAAPI_TYPE_WINDOW_WAYLAND, \
46                                  GstVaapiWindowWaylandPrivate))
47
48 struct _GstVaapiWindowWaylandPrivate {
49     struct wl_shell_surface    *shell_surface;
50     struct wl_surface          *surface;
51     struct wl_buffer           *buffer;
52     guint                       redraw_pending  : 1;
53 };
54
55 static gboolean
56 gst_vaapi_window_wayland_show(GstVaapiWindow *window)
57 {
58     GST_WARNING("unimplemented GstVaapiWindowWayland::show()");
59
60     return TRUE;
61 }
62
63 static gboolean
64 gst_vaapi_window_wayland_hide(GstVaapiWindow *window)
65 {
66     GST_WARNING("unimplemented GstVaapiWindowWayland::hide()");
67
68     return TRUE;
69 }
70
71 static void
72 handle_ping(void *data, struct wl_shell_surface *shell_surface,
73             uint32_t serial)
74 {
75     wl_shell_surface_pong(shell_surface, serial);
76 }
77
78 static void
79 handle_configure(void *data, struct wl_shell_surface *shell_surface,
80                  uint32_t edges, int32_t width, int32_t height)
81 {
82 }
83
84 static void
85 handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
86 {
87 }
88
89 static const struct wl_shell_surface_listener shell_surface_listener = {
90     handle_ping,
91     handle_configure,
92     handle_popup_done
93 };
94
95 static gboolean
96 gst_vaapi_window_wayland_create(
97     GstVaapiWindow *window,
98     guint          *width,
99     guint          *height
100 )
101 {
102     GstVaapiWindowWaylandPrivate * const priv =
103         GST_VAAPI_WINDOW_WAYLAND(window)->priv;
104     GstVaapiDisplayWaylandPrivate * const priv_display =
105         GST_VAAPI_OBJECT_DISPLAY_WAYLAND(window)->priv;
106
107     GST_DEBUG("create window, size %ux%u", *width, *height);
108
109     g_return_val_if_fail(priv_display->compositor != NULL, FALSE);
110     g_return_val_if_fail(priv_display->shell != NULL, FALSE);
111
112     priv->surface = wl_compositor_create_surface(priv_display->compositor);
113     if (!priv->surface)
114         return FALSE;
115
116     priv->shell_surface =
117         wl_shell_get_shell_surface(priv_display->shell, priv->surface);
118     if (!priv->shell_surface)
119         return FALSE;
120
121     wl_shell_surface_add_listener(priv->shell_surface,
122                                   &shell_surface_listener, priv);
123     wl_shell_surface_set_toplevel(priv->shell_surface);
124     wl_shell_surface_set_fullscreen(
125         priv->shell_surface,
126         WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE,
127         0,
128         NULL
129     );
130
131     priv->redraw_pending = FALSE;
132     return TRUE;
133 }
134
135 static void
136 gst_vaapi_window_wayland_destroy(GstVaapiWindow * window)
137 {
138     GstVaapiWindowWaylandPrivate * const priv =
139         GST_VAAPI_WINDOW_WAYLAND(window)->priv;
140
141     if (priv->shell_surface) {
142         wl_shell_surface_destroy(priv->shell_surface);
143         priv->shell_surface = NULL;
144     }
145
146     if (priv->surface) {
147         wl_surface_destroy(priv->surface);
148         priv->surface = NULL;
149     }
150
151     if (priv->buffer) {
152         wl_buffer_destroy(priv->buffer);
153         priv->buffer = NULL;
154     }
155 }
156
157 static gboolean
158 gst_vaapi_window_wayland_resize(
159     GstVaapiWindow * window,
160     guint            width,
161     guint            height
162 )
163 {
164     GST_DEBUG("resize window, new size %ux%u", width, height);
165     return TRUE;
166 }
167
168 static void
169 frame_redraw_callback(void *data, struct wl_callback *callback, uint32_t time)
170 {
171     GstVaapiWindowWaylandPrivate * const priv = data;
172
173     priv->redraw_pending = FALSE;
174     wl_buffer_destroy(priv->buffer);
175     priv->buffer = NULL;
176     wl_callback_destroy(callback);
177 }
178
179 static const struct wl_callback_listener frame_callback_listener = {
180     frame_redraw_callback
181 };
182
183 static gboolean
184 gst_vaapi_window_wayland_render(
185     GstVaapiWindow          *window,
186     GstVaapiSurface         *surface,
187     const GstVaapiRectangle *src_rect,
188     const GstVaapiRectangle *dst_rect,
189     guint                    flags
190 )
191 {
192     GstVaapiWindowWaylandPrivate * const priv =
193         GST_VAAPI_WINDOW_WAYLAND(window)->priv;
194     GstVaapiDisplay * const display = GST_VAAPI_OBJECT_DISPLAY(window);
195     struct wl_display * const wl_display = GST_VAAPI_OBJECT_WL_DISPLAY(window);
196     struct wl_buffer *buffer;
197     struct wl_callback *callback;
198     guint width, height, va_flags;
199     VASurfaceID surface_id;
200     VAStatus status;
201
202     /* XXX: use VPP to support unusual source and destination rectangles */
203     gst_vaapi_surface_get_size(surface, &width, &height);
204     if (src_rect->x      != 0     ||
205         src_rect->y      != 0     ||
206         src_rect->width  != width ||
207         src_rect->height != height) {
208         GST_ERROR("unsupported source rectangle for rendering");
209         return FALSE;
210     }
211
212     if (0 && (dst_rect->width != width || dst_rect->height != height)) {
213         GST_ERROR("unsupported target rectangle for rendering");
214         return FALSE;
215     }
216
217     surface_id = GST_VAAPI_OBJECT_ID(surface);
218     if (surface_id == VA_INVALID_ID)
219         return FALSE;
220
221     GST_VAAPI_OBJECT_LOCK_DISPLAY(window);
222
223     /* Wait for the previous frame to complete redraw */
224     if (priv->redraw_pending) 
225         wl_display_iterate(wl_display, WL_DISPLAY_READABLE);
226
227     /* XXX: use VA/VPP for other filters */
228     va_flags = from_GstVaapiSurfaceRenderFlags(flags);
229     status = vaGetSurfaceBufferWl(
230         GST_VAAPI_DISPLAY_VADISPLAY(display),
231         surface_id,
232         va_flags & (VA_TOP_FIELD|VA_BOTTOM_FIELD),
233         &buffer
234     );
235     if (status == VA_STATUS_ERROR_FLAG_NOT_SUPPORTED) {
236         /* XXX: de-interlacing flags not supported, try with VPP? */
237         status = vaGetSurfaceBufferWl(
238             GST_VAAPI_DISPLAY_VADISPLAY(display),
239             surface_id,
240             VA_FRAME_PICTURE,
241             &buffer
242         );
243     }
244     if (!vaapi_check_status(status, "vaGetSurfaceBufferWl()"))
245         return FALSE;
246
247     /* XXX: attach to the specified target rectangle */
248     wl_surface_attach(priv->surface, buffer, 0, 0);
249     wl_surface_damage(priv->surface, 0, 0, width, height);
250
251     wl_display_iterate(wl_display, WL_DISPLAY_WRITABLE);
252     priv->redraw_pending = TRUE;
253     priv->buffer = buffer;
254
255     callback = wl_surface_frame(priv->surface);
256     wl_callback_add_listener(callback, &frame_callback_listener, priv);
257     GST_VAAPI_OBJECT_UNLOCK_DISPLAY(window);
258     return TRUE;
259 }
260
261 static void
262 gst_vaapi_window_wayland_finalize(GObject *object)
263 {
264     G_OBJECT_CLASS(gst_vaapi_window_wayland_parent_class)->finalize(object);
265 }
266
267 static void
268 gst_vaapi_window_wayland_constructed(GObject *object)
269 {
270     GObjectClass *parent_class;
271
272     parent_class = G_OBJECT_CLASS(gst_vaapi_window_wayland_parent_class);
273     if (parent_class->constructed)
274         parent_class->constructed(object);
275 }
276
277 static void
278 gst_vaapi_window_wayland_class_init(GstVaapiWindowWaylandClass * klass)
279 {
280     GObjectClass * const object_class = G_OBJECT_CLASS(klass);
281     GstVaapiWindowClass * const window_class = GST_VAAPI_WINDOW_CLASS(klass);
282
283     g_type_class_add_private(klass, sizeof(GstVaapiWindowWaylandPrivate));
284
285     object_class->finalize      = gst_vaapi_window_wayland_finalize;
286     object_class->constructed   = gst_vaapi_window_wayland_constructed;
287
288     window_class->create        = gst_vaapi_window_wayland_create;
289     window_class->destroy       = gst_vaapi_window_wayland_destroy;
290     window_class->show          = gst_vaapi_window_wayland_show;
291     window_class->hide          = gst_vaapi_window_wayland_hide;
292     window_class->render        = gst_vaapi_window_wayland_render;
293     window_class->resize        = gst_vaapi_window_wayland_resize;
294 }
295
296 static void
297 gst_vaapi_window_wayland_init(GstVaapiWindowWayland * window)
298 {
299     GstVaapiWindowWaylandPrivate *priv =
300         GST_VAAPI_WINDOW_WAYLAND_GET_PRIVATE(window);
301
302     window->priv         = priv;
303     priv->shell_surface  = NULL;
304     priv->surface        = NULL;
305     priv->buffer         = NULL;
306     priv->redraw_pending = FALSE;
307 }
308
309 /**
310  * gst_vaapi_window_wayland_new:
311  * @display: a #GstVaapiDisplay
312  * @width: the requested window width, in pixels
313  * @height: the requested windo height, in pixels
314  *
315  * Creates a window with the specified @width and @height. The window
316  * will be attached to the @display and remains invisible to the user
317  * until gst_vaapi_window_show() is called.
318  *
319  * Return value: the newly allocated #GstVaapiWindow object
320  */
321 GstVaapiWindow *
322 gst_vaapi_window_wayland_new(
323     GstVaapiDisplay *display,
324     guint            width,
325     guint            height
326 )
327 {
328     GST_DEBUG("new window, size %ux%u", width, height);
329
330     g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), NULL);
331     g_return_val_if_fail(width  > 0, NULL);
332     g_return_val_if_fail(height > 0, NULL);
333
334     return g_object_new(GST_VAAPI_TYPE_WINDOW_WAYLAND,
335                         "display", display,
336                         "id",      GST_VAAPI_ID(0),
337                         "width",   width,
338                         "height",  height,
339                         NULL);
340 }