[Libva] [PATCH 2/4] tests: add support for Wayland.

Gwenole Beauchesne gb.devel at gmail.com
Fri Jul 13 07:02:41 PDT 2012


Signed-off-by: Gwenole Beauchesne <gwenole.beauchesne at intel.com>
---
 test/common/Makefile.am          |    7 ++
 test/common/va_display.c         |    4 +
 test/common/va_display_wayland.c |  197 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 208 insertions(+)
 create mode 100644 test/common/va_display_wayland.c

diff --git a/test/common/Makefile.am b/test/common/Makefile.am
index c348fd7..9711930 100644
--- a/test/common/Makefile.am
+++ b/test/common/Makefile.am
@@ -25,6 +25,7 @@ noinst_LTLIBRARIES = libva-display.la
 libva_display_cflags = \
 	-I$(top_srcdir)				\
 	-I$(top_builddir)			\
+	-DIN_LIBVA				\
 	$(NULL)
 
 libva_display_libs = \
@@ -39,6 +40,12 @@ source_c		+= va_display_x11.c
 libva_display_cflags	+= $(X11_CFLAGS)
 libva_display_libs	+= $(X11_LIBS)
 
+if USE_WAYLAND
+source_c		+= va_display_wayland.c
+libva_display_cflags	+= $(WAYLAND_CFLAGS)
+libva_display_libs	+= $(top_builddir)/va/libva-wayland.la $(WAYLAND_LIBS)
+endif
+
 libva_display_la_SOURCES= $(source_c)
 noinst_HEADERS		= $(source_h)
 libva_display_la_CFLAGS	= $(libva_display_cflags)
diff --git a/test/common/va_display.c b/test/common/va_display.c
index 80955a5..be50e40 100644
--- a/test/common/va_display.c
+++ b/test/common/va_display.c
@@ -33,6 +33,7 @@
 #include "va_display.h"
 
 extern const VADisplayHooks va_display_hooks_android;
+extern const VADisplayHooks va_display_hooks_wayland;
 extern const VADisplayHooks va_display_hooks_x11;
 
 static const VADisplayHooks *g_display_hooks;
@@ -40,6 +41,9 @@ static const VADisplayHooks *g_display_hooks_available[] = {
 #ifdef ANDROID
     &va_display_hooks_android,
 #else
+#if USE_WAYLAND
+    &va_display_hooks_wayland,
+#endif
     &va_display_hooks_x11,
 #endif
     NULL
diff --git a/test/common/va_display_wayland.c b/test/common/va_display_wayland.c
new file mode 100644
index 0000000..c2a5571
--- /dev/null
+++ b/test/common/va_display_wayland.c
@@ -0,0 +1,197 @@
+/*
+ * Copyright (c) 2012 Intel Corporation. All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ * 
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+ * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
+ * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#ifdef IN_LIBVA
+# include "va/wayland/va_wayland.h"
+#else
+# include <va/va_wayland.h>
+#endif
+#include "va_display.h"
+
+struct display {
+    struct wl_display          *display;
+    struct wl_compositor       *compositor;
+    struct wl_shell            *shell;
+    struct wl_shell_surface    *shell_surface;
+    struct wl_surface          *surface;
+    unsigned int                ref_count;
+    int                         event_fd;
+    unsigned int                event_mask;
+};
+
+static struct display *g_display;
+
+static void
+display_handle_global(
+    struct wl_display *display,
+    uint32_t           id,
+    const char        *interface,
+    uint32_t           version,
+    void              *data
+)
+{
+    struct display * const d = data;
+
+    if (strcmp(interface, "wl_compositor") == 0)
+        d->compositor = wl_display_bind(display, id, &wl_compositor_interface);
+    else if (strcmp(interface, "wl_shell") == 0)
+        d->shell = wl_display_bind(display, id, &wl_shell_interface);
+}
+
+static int
+event_mask_update(uint32_t mask, void *data)
+{
+    struct display * const d = data;
+
+    d->event_mask = mask;
+    return 0;
+}
+
+static VADisplay
+va_open_display_wayland(void)
+{
+    struct display *d;
+
+    if (g_display) {
+        d = g_display;
+        d->ref_count++;
+    }
+    else {
+        d = calloc(1, sizeof(*d));
+        if (!d)
+            return NULL;
+        d->event_fd = -1;
+
+        d->display = wl_display_connect(NULL);
+        if (!d->display) {
+            free(d);
+            return NULL;
+        }
+        wl_display_set_user_data(d->display, d);
+        wl_display_add_global_listener(d->display, display_handle_global, d);
+        d->event_fd = wl_display_get_fd(d->display, event_mask_update, d);
+        wl_display_iterate(d->display, d->event_mask);
+
+        d->ref_count = 1;
+        g_display = d;
+    }
+    return vaGetDisplayWl(d->display);
+}
+
+static void
+va_close_display_wayland(VADisplay va_dpy)
+{
+    struct display * const d = g_display;
+
+    if (!d || --d->ref_count > 0)
+        return;
+
+    if (d->surface) {
+        wl_surface_destroy(d->surface);
+        d->surface = NULL;
+    }
+
+    if (d->shell_surface) {
+        wl_shell_surface_destroy(d->shell_surface);
+        d->shell_surface = NULL;
+    }
+
+    if (d->shell) {
+        wl_shell_destroy(d->shell);
+        d->shell = NULL;
+    }
+
+    if (d->compositor) {
+        wl_compositor_destroy(d->compositor);
+        d->compositor = NULL;
+    }
+
+    if (d->display) {
+#if 0
+        /* XXX: wayland server protocol only? */
+        wl_display_destroy(d->display);
+#endif
+        d->display = NULL;
+    }
+    free(g_display);
+    g_display = NULL;
+}
+
+static int
+ensure_window(VADisplay va_dpy, unsigned int width, unsigned int height)
+{
+    struct display * const d = g_display;
+
+    if (!d->surface) {
+        d->surface = wl_compositor_create_surface(d->compositor);
+        if (!d->surface)
+            return 0;
+    }
+
+    if (!d->shell_surface) {
+        d->shell_surface = wl_shell_get_shell_surface(d->shell, d->surface);
+        if (!d->shell_surface)
+            return 0;
+        wl_shell_surface_set_toplevel(d->shell_surface);
+    }
+    return 1;
+}
+
+static VAStatus
+va_put_surface_wayland(
+    VADisplay          va_dpy,
+    VASurfaceID        surface,
+    const VARectangle *src_rect,
+    const VARectangle *dst_rect
+)
+{
+    struct display * const d = g_display;
+    VAStatus va_status;
+    struct wl_buffer *buffer;
+
+    if (!ensure_window(va_dpy, dst_rect->width, dst_rect->height))
+        return VA_STATUS_ERROR_ALLOCATION_FAILED;
+
+    va_status = vaGetSurfaceBufferWl(va_dpy, surface, &buffer);
+    if (va_status != VA_STATUS_SUCCESS)
+        return va_status;
+
+    wl_surface_attach(d->surface, buffer, 0, 0);
+    wl_surface_damage(
+         d->surface,
+         dst_rect->x, dst_rect->y, dst_rect->width, dst_rect->height
+     );
+
+    wl_display_flush(d->display);
+    return VA_STATUS_SUCCESS;
+}
+
+const VADisplayHooks va_display_hooks_wayland = {
+    "wayland",
+    va_open_display_wayland,
+    va_close_display_wayland,
+    va_put_surface_wayland,
+};
-- 
1.7.9.5



More information about the Libva mailing list