2 * Copyright (c) 2011 Intel Corporation. All Rights Reserved.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sub license, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial portions
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
19 * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
20 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 #include <sys/select.h>
29 # include "va/wayland/va_wayland.h"
31 # include <va/va_wayland.h>
33 #include <wayland-server.h>
35 static void *open_display(void);
36 static void close_display(void *win_display);
37 static int create_window(void *win_display,
38 int x, int y, int width, int height);
39 static int check_window_event(void *win_display, void *drawable,
40 int *width, int *height, int *quit);
48 struct drawable *wl_drawable,
49 VASurfaceID va_surface,
50 const VARectangle *src_rect,
51 const VARectangle *dst_rect,
52 const VARectangle *cliprects,
53 unsigned int num_cliprects,
57 /* Glue code for the current PutSurface test design */
58 #define CAST_DRAWABLE(a) (struct drawable *)(a)
60 static inline VADisplay
61 vaGetDisplay(VANativeDisplay native_dpy)
63 return vaGetDisplayWl(native_dpy);
70 struct drawable *wl_drawable,
79 const VARectangle *cliprects,
80 unsigned int num_cliprects,
84 VARectangle src_rect, dst_rect;
88 src_rect.width = src_w;
89 src_rect.height = src_h;
93 dst_rect.width = src_w;
94 dst_rect.height = src_h;
95 return va_put_surface(dpy, wl_drawable, surface, &src_rect, &dst_rect,
96 cliprects, num_cliprects, flags);
99 #include "putsurface_common.c"
102 struct wl_display *display;
103 struct wl_compositor *compositor;
104 struct wl_shell *shell;
105 struct wl_registry *registry;
110 struct wl_display *display;
111 struct wl_surface *surface;
112 unsigned int redraw_pending : 1;
116 frame_redraw_callback(void *data, struct wl_callback *callback, uint32_t time)
118 struct drawable * const drawable = data;
120 drawable->redraw_pending = 0;
121 wl_callback_destroy(callback);
124 static const struct wl_callback_listener frame_callback_listener = {
125 frame_redraw_callback
131 struct drawable *wl_drawable,
132 VASurfaceID va_surface,
133 const VARectangle *src_rect,
134 const VARectangle *dst_rect,
135 const VARectangle *cliprects,
136 unsigned int num_cliprects,
141 struct wl_callback *callback;
143 struct wl_buffer *buffer;
146 return VA_STATUS_ERROR_INVALID_SURFACE;
148 d = wl_display_get_user_data(wl_drawable->display);
150 return VA_STATUS_ERROR_INVALID_DISPLAY;
152 /* Wait for the previous frame to complete redraw */
153 if (wl_drawable->redraw_pending) {
154 wl_display_flush(d->display);
155 while (wl_drawable->redraw_pending)
156 wl_display_dispatch(wl_drawable->display);
159 va_status = vaGetSurfaceBufferWl(va_dpy, va_surface, VA_FRAME_PICTURE, &buffer);
160 if (va_status != VA_STATUS_SUCCESS)
163 wl_surface_attach(wl_drawable->surface, buffer, 0, 0);
165 wl_drawable->surface,
166 dst_rect->x, dst_rect->y, dst_rect->width, dst_rect->height
169 wl_display_flush(d->display);
170 wl_drawable->redraw_pending = 1;
171 callback = wl_surface_frame(wl_drawable->surface);
172 wl_surface_commit(wl_drawable->surface);
173 wl_callback_add_listener(callback, &frame_callback_listener, wl_drawable);
174 return VA_STATUS_SUCCESS;
178 registry_handle_global(
180 struct wl_registry *registry,
182 const char *interface,
186 struct display * const d = data;
188 if (strcmp(interface, "wl_compositor") == 0)
190 wl_registry_bind(registry, id, &wl_compositor_interface, 1);
191 else if (strcmp(interface, "wl_shell") == 0)
192 d->shell = wl_registry_bind(registry, id, &wl_shell_interface, 1);
195 static const struct wl_registry_listener registry_listener = {
196 registry_handle_global,
205 d = calloc(1, sizeof *d);
209 d->display = wl_display_connect(NULL);
213 wl_display_set_user_data(d->display, d);
214 d->registry = wl_display_get_registry(d->display);
215 wl_registry_add_listener(d->registry, ®istry_listener, d);
216 d->event_fd = wl_display_get_fd(d->display);
217 wl_display_dispatch(d->display);
222 close_display(void *win_display)
224 struct display * const d = wl_display_get_user_data(win_display);
227 wl_shell_destroy(d->shell);
232 wl_compositor_destroy(d->compositor);
233 d->compositor = NULL;
237 wl_display_disconnect(d->display);
244 create_window(void *win_display, int x, int y, int width, int height)
246 struct wl_display * const display = win_display;
247 struct display * const d = wl_display_get_user_data(display);
248 struct wl_surface *surface1, *surface2;
249 struct wl_shell_surface *shell_surface;
250 struct wl_shell_surface *shell_surface_2;
251 struct drawable *drawable1, *drawable2;
253 surface1 = wl_compositor_create_surface(d->compositor);
254 shell_surface = wl_shell_get_shell_surface(d->shell, surface1);
255 wl_shell_surface_set_toplevel(shell_surface);
257 drawable1 = malloc(sizeof(*drawable1));
258 drawable1->display = display;
259 drawable1->surface = surface1;
260 drawable1->redraw_pending = 0;
263 drawable_thread0 = drawable1;
265 if (multi_thread == 0)
268 surface2 = wl_compositor_create_surface(d->compositor);
269 shell_surface_2 = wl_shell_get_shell_surface(d->shell, surface2);
270 wl_shell_surface_set_toplevel(shell_surface_2);
272 drawable2 = malloc(sizeof(*drawable2));
273 drawable2->display = display;
274 drawable1->surface = surface2;
275 drawable2->redraw_pending = 0;
278 drawable_thread1 = drawable2;
291 struct wl_display * const display = win_display;
292 struct display * const d = wl_display_get_user_data(display);
297 if (check_event == 0)
304 FD_SET(d->event_fd, &rfds);
306 retval = select(d->event_fd + 1, &rfds, NULL, NULL, &tv);
312 wl_display_dispatch(d->display);
313 } while (retval > 0);
316 /* bail on any focused key press */
317 if(event.type == KeyPress) {
324 /* rescale the video to fit the window */
325 if(event.type == ConfigureNotify) {
326 *width = event.xconfigure.width;
327 *height = event.xconfigure.height;
328 printf("Scale window to %dx%d\n", width, height);