From 4a4eb7dc572ccd9bafd4b8b2fc9dae52a6529897 Mon Sep 17 00:00:00 2001
From: Tadao Tanikawa <tanikawa.tadao@jp.panasonic.com>
Date: Thu, 6 Oct 2016 18:25:44 +0900
Subject: [PATCH] QEMU accelerated GPU

Change-Id: Ied25218ab4f4b05062f775cfe292a215604d50a5
Signed-off-by: Tadao Tanikawa <tanikawa.tadao@jp.panasonic.com>
---
 .../recipes-devtools/qemu/qemu_2.7.%.bbappend      |   7 +
 .../recipes-graphics/mesa/mesa_11.%.bbappend       |  25 +-
 .../virglrenderer/virglrenderer_git.bb             |  32 ++
 .../linux/linux-yocto/virtio-gpu.cfg               |   1 +
 .../recipes-kernel/linux/linux-yocto_%.bbappend    |   1 +
 templates/machine/qemux86-64/50_local.conf.inc     |  14 +
 6 files changed, 79 insertions(+), 1 deletion(-)
 create mode 100644 meta-agl-bsp/recipes-devtools/qemu/qemu_2.7.%.bbappend
 create mode 100644 meta-agl-bsp/recipes-graphics/virglrenderer/virglrenderer_git.bb
 create mode 100644 meta-agl-bsp/recipes-kernel/linux/linux-yocto/virtio-gpu.cfg

diff --git a/meta-agl-bsp/recipes-devtools/qemu/qemu_2.7.%.bbappend b/meta-agl-bsp/recipes-devtools/qemu/qemu_2.7.%.bbappend
new file mode 100644
index 0000000..80b92d1
--- /dev/null
+++ b/meta-agl-bsp/recipes-devtools/qemu/qemu_2.7.%.bbappend
@@ -0,0 +1,7 @@
+PACKAGECONFIG[virgl3d] = "--enable-sdl --with-sdlabi=2.0 --enable-opengl --enable-gtk --with-gtkabi=3.0 --enable-virglrenderer,--disable-virglrenderer,"
+PACKAGECONFIG_append = " virgl3d"
+
+DEPENDS += "${@bb.utils.contains('MACHINE_FEATURES', 'virgl', 'virglrenderer-native', '', d)}"
+
+
+
diff --git a/meta-agl-bsp/recipes-graphics/mesa/mesa_11.%.bbappend b/meta-agl-bsp/recipes-graphics/mesa/mesa_11.%.bbappend
index 49e5277..ec1aad3 100644
--- a/meta-agl-bsp/recipes-graphics/mesa/mesa_11.%.bbappend
+++ b/meta-agl-bsp/recipes-graphics/mesa/mesa_11.%.bbappend
@@ -1,6 +1,29 @@
 # The gallium-llvm is recommended as software 3D graphics renderer
-GALLIUM_LLVM = "gallium-llvm"
+GALLIUM_LLVM = "${@bb.utils.contains('MACHINE_FEATURES', 'virtio-gpu', '', 'gallium-llvm', d)}"
 PACKAGECONFIG_append_qemux86 = " gallium ${GALLIUM_LLVM}"
 PACKAGECONFIG_append_qemux86-64 = " gallium ${GALLIUM_LLVM}"
 
 DRIDRIVERS_append_intel-corei7-64 = ",i965"
+
+# If virtio-gpu is available then enabling the host GPU acceleration for 3D graphics.
+PACKAGECONFIG[virgl] = ""
+
+PACKAGECONFIG_append_qemux86 = "${@bb.utils.contains('MACHINE_FEATURES', 'virtio-gpu', 'virgl', '', d)}"
+PACKAGECONFIG_append_qemux86-64 = "${@bb.utils.contains('MACHINE_FEATURES', 'virtio-gpu', 'virgl', '', d)}"
+
+GALLIUM_VIRGL = "${@bb.utils.contains('MACHINE_FEATURES', 'virtio-gpu', 'virgl', '', d)}"
+GALLIUMDRIVERS_append_x86 = "${@bb.utils.contains('PACKAGECONFIG', 'virgl', ',${GALLIUM_VIRGL}', '', d)}"
+GALLIUMDRIVERS_append_x86-64 = "${@bb.utils.contains('PACKAGECONFIG', 'virgl', ',${GALLIUM_VIRGL}', '', d)}"
+
+# --with-egl-platforms=drm is required to build the virgl driver
+EGL_PLATFORMS .="${@bb.utils.contains('PACKAGECONFIG', 'virgl', ',drm', '', d)}"
+
+# Drity hack to avoid to build libdrm-native
+do_configure_prepend_class-native() {
+        # Append build host pkg-config paths for native target since the host may provide libdrm
+        BHOST_PKGCONFIG_PATH=$(PATH=/usr/bin:/bin pkg-config --variable pc_path pkg-config || echo "")
+        if [ ! -z "$BHOST_PKGCONFIG_PATH" ]; then
+                export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:$BHOST_PKGCONFIG_PATH
+        fi
+}
+
diff --git a/meta-agl-bsp/recipes-graphics/virglrenderer/virglrenderer_git.bb b/meta-agl-bsp/recipes-graphics/virglrenderer/virglrenderer_git.bb
new file mode 100644
index 0000000..928cb3f
--- /dev/null
+++ b/meta-agl-bsp/recipes-graphics/virglrenderer/virglrenderer_git.bb
@@ -0,0 +1,32 @@
+SRC_URI = "git://anongit.freedesktop.org/git/virglrenderer;protocol=http"
+SRCREV = "${AUTOREV}"
+
+LICENSE = "MIT"
+LIC_FILES_CHKSUM = "file://COPYING;md5=c81c08eeefd9418fca8f88309a76db10"
+
+#DEPENDS += "autogen-native"
+
+S = "${WORKDIR}/git"
+
+LIBTOOL = "${HOST_SYS}-libtool"
+EXTRA_OEMAKE = "'LIBTOOL=${LIBTOOL}'"
+
+do_configure_prepend_class-native() {
+        # Append build host pkg-config paths for native target since the host may provide sdl
+        BHOST_PKGCONFIG_PATH=$(PATH=/usr/bin:/bin pkg-config --variable pc_path pkg-config || echo "")
+        if [ ! -z "$BHOST_PKGCONFIG_PATH" ]; then
+                export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:$BHOST_PKGCONFIG_PATH
+        fi
+        ( cd ${S}
+          ${S}/autogen.sh --prefix=${prefix} --libdir=${libdir} )
+}
+
+do_compile (){
+        oe_runmake
+}
+
+do_install () {
+    oe_runmake DESTDIR=${D} install
+}
+
+BBCLASSEXTEND = "native"
diff --git a/meta-agl-bsp/recipes-kernel/linux/linux-yocto/virtio-gpu.cfg b/meta-agl-bsp/recipes-kernel/linux/linux-yocto/virtio-gpu.cfg
new file mode 100644
index 0000000..5db6150
--- /dev/null
+++ b/meta-agl-bsp/recipes-kernel/linux/linux-yocto/virtio-gpu.cfg
@@ -0,0 +1 @@
+CONFIG_DRM_VIRTIO_GPU=y
diff --git a/meta-agl-bsp/recipes-kernel/linux/linux-yocto_%.bbappend b/meta-agl-bsp/recipes-kernel/linux/linux-yocto_%.bbappend
index 8b29aac..dbefd64 100644
--- a/meta-agl-bsp/recipes-kernel/linux/linux-yocto_%.bbappend
+++ b/meta-agl-bsp/recipes-kernel/linux/linux-yocto_%.bbappend
@@ -5,6 +5,7 @@ SRC_URI += "file://fanotify.cfg \
             file://uinput.cfg \
             file://hid.cfg \
             file://drm.cfg \
+            file://virtio-gpu.cfg \
             "
 
 # disk drivers for vmdk
diff --git a/templates/machine/qemux86-64/50_local.conf.inc b/templates/machine/qemux86-64/50_local.conf.inc
index f5bb53c..58e3af7 100644
--- a/templates/machine/qemux86-64/50_local.conf.inc
+++ b/templates/machine/qemux86-64/50_local.conf.inc
@@ -18,3 +18,17 @@ APPEND += "console=ttyS0,115200n8"
 #
 # All boot message will be off
 APPEND += "quiet"
+
+# Configurations to use the host GPU to accelerate 3D rendering.
+#
+# Requirements:
+#   - Enabling Intel VT or AMD_V virtualization hardware extensions in BIOS
+#   - Linux kernel 4.4 or later on the host
+#   - The virgl 3D driver (Mesa contains it in Gallium 3D)
+#   - The virglrenderer library is required by QEMU-natvie
+#   - To build virglrenderer-native, some additional packages
+#     should be installed on the host. E.g. for Ubuntu,
+#       sudo apt-get install libgbm-dev libsdl2-dev libgtk-3-dev libgles2-mesa-dev libpixman-1-dev
+#
+#PREFERRED_VERSION_qemu-native ?= "2.7.%"
+#MACHINE_FEATURES_append = " virtio-gpu"
-- 
1.9.1

