autotest: implement hardware_VideoDecodeCapable for freon

BUG=chromium:413125
TEST=hardware_VideoDecodeCapable/control.vaapi

Change-Id: I2eac1365739c17ea054fa4c562b0a7e199d95b22
Reviewed-on: https://chromium-review.googlesource.com/234877
Tested-by: Haixia Shi <hshi@chromium.org>
Reviewed-by: Ilja Friedel <ihf@chromium.org>
Commit-Queue: Haixia Shi <hshi@chromium.org>
Trybot-Ready: Haixia Shi <hshi@chromium.org>
diff --git a/client/site_tests/hardware_VideoDecodeCapable/hardware_VideoDecodeCapable.py b/client/site_tests/hardware_VideoDecodeCapable/hardware_VideoDecodeCapable.py
index 3979617..dddf180 100644
--- a/client/site_tests/hardware_VideoDecodeCapable/hardware_VideoDecodeCapable.py
+++ b/client/site_tests/hardware_VideoDecodeCapable/hardware_VideoDecodeCapable.py
@@ -82,17 +82,22 @@
         # we will test with the v4l2 module.
         utils.make('v4l2', ignore_status = True)
         utils.make('vaapi', ignore_status = True)
+        utils.make('vaapi_drm', ignore_status = True)
 
 
     def run_once_vaapi(self):
         sys.path.append(self.bindir)
         import vaapi
 
-        # Set the XAUTHORITY for connecting to the X server
-        utils.assert_has_X_server()
-        os.environ.setdefault('XAUTHORITY', '/home/chronos/.Xauthority')
+        if not utils.is_freon():
+            # Set the XAUTHORITY for connecting to the X server
+            utils.assert_has_X_server()
+            os.environ.setdefault('XAUTHORITY', '/home/chronos/.Xauthority')
 
-        display = vaapi.create_display(':0.0')
+            display = vaapi.create_display(':0.0')
+        else:
+            display = vaapi.create_display('/dev/dri/card0')
+
         supported_profiles = vaapi.query_profiles(display)
         logging.info('Vaapi Profiles: %s', supported_profiles)
 
diff --git a/client/site_tests/hardware_VideoDecodeCapable/src/Makefile b/client/site_tests/hardware_VideoDecodeCapable/src/Makefile
index ae836d8..55121ff 100644
--- a/client/site_tests/hardware_VideoDecodeCapable/src/Makefile
+++ b/client/site_tests/hardware_VideoDecodeCapable/src/Makefile
@@ -2,10 +2,13 @@
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
 
-.PHONY:v4l2, vaapi
+.PHONY:v4l2, vaapi, vaapi_drm
 
 v4l2:
 	python setup_v4l2.py install --install-lib=..
 
 vaapi:
 	python setup_vaapi.py install --install-lib=..
+
+vaapi_drm:
+	python setup_vaapi_drm.py install --install-lib=..
diff --git a/client/site_tests/hardware_VideoDecodeCapable/src/setup_vaapi_drm.py b/client/site_tests/hardware_VideoDecodeCapable/src/setup_vaapi_drm.py
new file mode 100644
index 0000000..1eb42c7
--- /dev/null
+++ b/client/site_tests/hardware_VideoDecodeCapable/src/setup_vaapi_drm.py
@@ -0,0 +1,14 @@
+# Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+"""Script to set up vaapi extension module.
+"""
+from distutils.core import setup, Extension
+
+module = Extension('vaapi',
+                   define_macros = [('USE_DRM', '1')],
+                   libraries = ['drm', 'va', 'va-drm'],
+                   sources = ['vaapimodule.cc'])
+
+setup(name = 'vaapi', version = '1.0', ext_modules = [module])
diff --git a/client/site_tests/hardware_VideoDecodeCapable/src/vaapimodule.cc b/client/site_tests/hardware_VideoDecodeCapable/src/vaapimodule.cc
index 61c9fac..5a4424d 100644
--- a/client/site_tests/hardware_VideoDecodeCapable/src/vaapimodule.cc
+++ b/client/site_tests/hardware_VideoDecodeCapable/src/vaapimodule.cc
@@ -3,27 +3,45 @@
 // found in the LICENSE file.
 
 #include <Python.h>
+
+#if !defined(USE_DRM)
 #include <X11/Xlib.h>
 #include <va/va.h>
 #include <va/va_x11.h>
+#else
+#include <stdio.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include <va/va.h>
+#include <va/va_drm.h>
+#endif
 
 static PyObject *VaapiError;
 
 namespace {
 
 struct DisplayBundle {
+#if !defined(USE_DRM)
   Display *x11_display;
+#else
+  int drm_fd;
+#endif
   VADisplay va_display;
 };
 
 static void destroy_display_bundle(PyObject* object) {
   DisplayBundle* bundle = (DisplayBundle*) PyCapsule_GetPointer(object, NULL);
   vaTerminate(bundle->va_display);
+#if !defined(USE_DRM)
   XCloseDisplay(bundle->x11_display);
+#else
+  close(bundle->drm_fd);
+#endif
   delete bundle;
 }
 
 static PyObject* va_create_display(PyObject* self, PyObject* args) {
+#if !defined(USE_DRM)
   const char* display_name;
   if (!PyArg_ParseTuple(args, "s", &display_name))
     return NULL;
@@ -36,6 +54,19 @@
   }
 
   VADisplay va_display = vaGetDisplay(x11_display);
+#else
+  const char* drm_card_path;
+  if (!PyArg_ParseTuple(args, "s", &drm_card_path))
+    return NULL;
+
+  int drm_fd = open(drm_card_path, O_RDWR);
+  if (drm_fd < 0) {
+    PyErr_SetString(VaapiError, "Cannot open drm card path");
+    return NULL;
+  }
+
+  VADisplay va_display = vaGetDisplayDRM(drm_fd);
+#endif
   if (!vaDisplayIsValid(va_display)) {
     PyErr_SetString(VaapiError, "Cannot get a valid display");
     return NULL;
@@ -50,7 +81,11 @@
   }
 
   DisplayBundle* bundle = new DisplayBundle();
+#if !defined(USE_DRM)
   bundle->x11_display = x11_display;
+#else
+  bundle->drm_fd = drm_fd;
+#endif
   bundle->va_display = va_display;
 
   return PyCapsule_New(bundle, NULL, destroy_display_bundle);