wayland: set opaque region for YUV surface.
[vaapi:jjardons-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     struct wl_region           *opaque_region;
53     guint                       redraw_pending  : 1;
54 };
55
56 static gboolean
57 gst_vaapi_window_wayland_show(GstVaapiWindow *window)
58 {
59     GST_WARNING("unimplemented GstVaapiWindowWayland::show()");
60
61     return TRUE;
62 }
63
64 static gboolean
65 gst_vaapi_window_wayland_hide(GstVaapiWindow *window)
66 {
67     GST_WARNING("unimplemented GstVaapiWindowWayland::hide()");
68
69     return TRUE;
70 }
71
72 static void
73 handle_ping(void *data, struct wl_shell_surface *shell_surface,
74             uint32_t serial)
75 {
76     wl_shell_surface_pong(shell_surface, serial);
77 }
78
79 static void
80 handle_configure(void *data, struct wl_shell_surface *shell_surface,
81                  uint32_t edges, int32_t width, int32_t height)
82 {
83 }
84
85 static void
86 handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
87 {
88 }
89
90 static const struct wl_shell_surface_listener shell_surface_listener = {
91     handle_ping,
92     handle_configure,
93     handle_popup_done
94 };
95
96 static gboolean
97 gst_vaapi_window_wayland_create(
98     GstVaapiWindow *window,
99     guint          *width,
100     guint          *height
101 )
102 {
103     GstVaapiWindowWaylandPrivate * const priv =
104         GST_VAAPI_WINDOW_WAYLAND(window)->priv;
105     GstVaapiDisplayWaylandPrivate * const priv_display =
106         GST_VAAPI_OBJECT_DISPLAY_WAYLAND(window)->priv;
107
108     GST_DEBUG("create window, size %ux%u", *width, *height);
109
110     g_return_val_if_fail(priv_display->compositor != NULL, FALSE);
111     g_return_val_if_fail(priv_display->shell != NULL, FALSE);
112
113     priv->surface = wl_compositor_create_surface(priv_display->compositor);
114     if (!priv->surface)
115         return FALSE;
116
117     priv->shell_surface =
118         wl_shell_get_shell_surface(priv_display->shell, priv->surface);
119     if (!priv->shell_surface)
120         return FALSE;
121
122     wl_shell_surface_add_listener(priv->shell_surface,
123                                   &shell_surface_listener, priv);
124     wl_shell_surface_set_toplevel(priv->shell_surface);
125     wl_shell_surface_set_fullscreen(
126         priv->shell_surface,
127         WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE,
128         0,
129         NULL
130     );
131
132     priv->redraw_pending = FALSE;
133     return TRUE;
134 }
135
136 static void
137 gst_vaapi_window_wayland_destroy(GstVaapiWindow * window)
138 {
139     GstVaapiWindowWaylandPrivate * const priv =
140         GST_VAAPI_WINDOW_WAYLAND(window)->priv;
141
142     if (priv->shell_surface) {
143         wl_shell_surface_destroy(priv->shell_surface);
144         priv->shell_surface = NULL;
145     }
146
147     if (priv->surface) {
148         wl_surface_destroy(priv->surface);
149         priv->surface = NULL;
150     }
151
152     if (priv->buffer) {
153         wl_buffer_destroy(priv->buffer);
154         priv->buffer = NULL;
155     }
156 }
157
158 static gboolean
159 gst_vaapi_window_wayland_resize(
160     GstVaapiWindow * window,
161     guint            width,
162     guint            height
163 )
164 {
165     GstVaapiWindowWaylandPrivate * const priv =
166         GST_VAAPI_WINDOW_WAYLAND(window)->priv;
167     GstVaapiDisplayWaylandPrivate * const priv_display =
168         GST_VAAPI_OBJECT_DISPLAY_WAYLAND(window)->priv;
169
170     GST_DEBUG("resize window, new size %ux%u", width, height);
171
172     if (priv->opaque_region)
173         wl_region_destroy(priv->opaque_region);
174     priv->opaque_region = wl_compositor_create_region(priv_display->compositor);
175     wl_region_add(priv->opaque_region, 0, 0, width, height);
176
177     return TRUE;
178 }
179
180 static void
181 frame_redraw_callback(void *data, struct wl_callback *callback, uint32_t time)
182 {
183     GstVaapiWindowWaylandPrivate * const priv = data;
184
185     priv->redraw_pending = FALSE;
186     wl_buffer_destroy(priv->buffer);
187     priv->buffer = NULL;
188     wl_callback_destroy(callback);
189 }
190
191 static const struct wl_callback_listener frame_callback_listener = {
192     frame_redraw_callback
193 };
194
195 static gboolean
196 gst_vaapi_window_wayland_render(
197     GstVaapiWindow          *window,
198     GstVaapiSurface         *surface,
199     const GstVaapiRectangle *src_rect,
200     const GstVaapiRectangle *dst_rect,
201     guint                    flags
202 )
203 {
204     GstVaapiWindowWaylandPrivate * const priv =
205         GST_VAAPI_WINDOW_WAYLAND(window)->priv;
206     GstVaapiDisplay * const display = GST_VAAPI_OBJECT_DISPLAY(window);
207     struct wl_display * const wl_display = GST_VAAPI_OBJECT_WL_DISPLAY(window);
208     struct wl_buffer *buffer;
209     struct wl_callback *callback;
210     guint width, height, va_flags;
211     VASurfaceID surface_id;
212     VAStatus status;
213
214     /* XXX: use VPP to support unusual source and destination rectangles */
215     gst_vaapi_surface_get_size(surface, &width, &height);
216     if (src_rect->x      != 0     ||
217         src_rect->y      != 0     ||
218         src_rect->width  != width ||
219         src_rect->height != height) {
220         GST_ERROR("unsupported source rectangle for rendering");
221         return FALSE;
222     }
223
224     if (0 && (dst_rect->width != width || dst_rect->height != height)) {
225         GST_ERROR("unsupported target rectangle for rendering");
226         return FALSE;
227     }
228
229     surface_id = GST_VAAPI_OBJECT_ID(surface);
230     if (surface_id == VA_INVALID_ID)
231         return FALSE;
232
233     GST_VAAPI_OBJECT_LOCK_DISPLAY(window);
234
235     /* Wait for the previous frame to complete redraw */
236     if (priv->redraw_pending) 
237         wl_display_iterate(wl_display, WL_DISPLAY_READABLE);
238
239     /* XXX: use VA/VPP for other filters */
240     va_flags = from_GstVaapiSurfaceRenderFlags(flags);
241     status = vaGetSurfaceBufferWl(
242         GST_VAAPI_DISPLAY_VADISPLAY(display),
243         surface_id,
244         va_flags & (VA_TOP_FIELD|VA_BOTTOM_FIELD),
245         &buffer
246     );
247     if (status == VA_STATUS_ERROR_FLAG_NOT_SUPPORTED) {
248         /* XXX: de-interlacing flags not supported, try with VPP? */
249         status = vaGetSurfaceBufferWl(
250             GST_VAAPI_DISPLAY_VADISPLAY(display),
251             surface_id,
252             VA_FRAME_PICTURE,
253             &buffer
254         );
255     }
256     if (!vaapi_check_status(status, "vaGetSurfaceBufferWl()"))
257         return FALSE;
258
259     /* XXX: attach to the specified target rectangle */
260     wl_surface_attach(priv->surface, buffer, 0, 0);
261     wl_surface_damage(priv->surface, 0, 0, width, height);
262
263     if (priv->opaque_region) {
264         wl_surface_set_opaque_region(priv->surface, priv->opaque_region);
265         wl_region_destroy(priv->opaque_region);
266         priv->opaque_region = NULL;
267     }
268
269     wl_display_iterate(wl_display, WL_DISPLAY_WRITABLE);
270     priv->redraw_pending = TRUE;
271     priv->buffer = buffer;
272
273     callback = wl_surface_frame(priv->surface);
274     wl_callback_add_listener(callback, &frame_callback_listener, priv);
275     GST_VAAPI_OBJECT_UNLOCK_DISPLAY(window);
276     return TRUE;
277 }
278
279 static void
280 gst_vaapi_window_wayland_finalize(GObject *object)
281 {
282     G_OBJECT_CLASS(gst_vaapi_window_wayland_parent_class)->finalize(object);
283 }
284
285 static void
286 gst_vaapi_window_wayland_constructed(GObject *object)
287 {
288     GObjectClass *parent_class;
289
290     parent_class = G_OBJECT_CLASS(gst_vaapi_window_wayland_parent_class);
291     if (parent_class->constructed)
292         parent_class->constructed(object);
293 }
294
295 static void
296 gst_vaapi_window_wayland_class_init(GstVaapiWindowWaylandClass * klass)
297 {
298     GObjectClass * const object_class = G_OBJECT_CLASS(klass);
299     GstVaapiWindowClass * const window_class = GST_VAAPI_WINDOW_CLASS(klass);
300
301     g_type_class_add_private(klass, sizeof(GstVaapiWindowWaylandPrivate));
302
303     object_class->finalize      = gst_vaapi_window_wayland_finalize;
304     object_class->constructed   = gst_vaapi_window_wayland_constructed;
305
306     window_class->create        = gst_vaapi_window_wayland_create;
307     window_class->destroy       = gst_vaapi_window_wayland_destroy;
308     window_class->show          = gst_vaapi_window_wayland_show;
309     window_class->hide          = gst_vaapi_window_wayland_hide;
310     window_class->render        = gst_vaapi_window_wayland_render;
311     window_class->resize        = gst_vaapi_window_wayland_resize;
312 }
313
314 static void
315 gst_vaapi_window_wayland_init(GstVaapiWindowWayland * window)
316 {
317     GstVaapiWindowWaylandPrivate *priv =
318         GST_VAAPI_WINDOW_WAYLAND_GET_PRIVATE(window);
319
320     window->priv         = priv;
321     priv->shell_surface  = NULL;
322     priv->surface        = NULL;
323     priv->buffer         = NULL;
324     priv->redraw_pending = FALSE;
325 }
326
327 /**
328  * gst_vaapi_window_wayland_new:
329  * @display: a #GstVaapiDisplay
330  * @width: the requested window width, in pixels
331  * @height: the requested windo height, in pixels
332  *
333  * Creates a window with the specified @width and @height. The window
334  * will be attached to the @display and remains invisible to the user
335  * until gst_vaapi_window_show() is called.
336  *
337  * Return value: the newly allocated #GstVaapiWindow object
338  */
339 GstVaapiWindow *
340 gst_vaapi_window_wayland_new(
341     GstVaapiDisplay *display,
342     guint            width,
343     guint            height
344 )
345 {
346     GST_DEBUG("new window, size %ux%u", width, height);
347
348     g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), NULL);
349     g_return_val_if_fail(width  > 0, NULL);
350     g_return_val_if_fail(height > 0, NULL);
351
352     return g_object_new(GST_VAAPI_TYPE_WINDOW_WAYLAND,
353                         "display", display,
354                         "id",      GST_VAAPI_ID(0),
355                         "width",   width,
356                         "height",  height,
357                         NULL);
358 }