Enable video playback in libva

Change-Id: I008da4aa5966ef226b3ec65051970498490a1a8e
Signed-off-by: Liu, Shuo <shuo.liu@intel.com>
diff --git a/test/Android.mk b/test/Android.mk
index b28ba5f..58478f5 100644
--- a/test/Android.mk
+++ b/test/Android.mk
@@ -13,9 +13,8 @@
 
 LOCAL_C_INCLUDES += \
   $(TARGET_OUT_HEADERS)/libva
-
-LOCAL_MODULE := vainfo
 LOCAL_MODULE_TAGS := optional
+LOCAL_MODULE := vainfo
 
 LOCAL_SHARED_LIBRARIES := libva-android libva libdl libdrm libcutils
 
diff --git a/test/encode/loadsurface.h b/test/encode/loadsurface.h
deleted file mode 120000
index fb38e2f..0000000
--- a/test/encode/loadsurface.h
+++ /dev/null
@@ -1 +0,0 @@
-../putsurface/loadsurface.h
\ No newline at end of file
diff --git a/test/encode/loadsurface.h b/test/encode/loadsurface.h
new file mode 100644
index 0000000..ef792c8
--- /dev/null
+++ b/test/encode/loadsurface.h
@@ -0,0 +1,120 @@
+/*
+ * Copyright (c) 2008-2009 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.
+ */
+
+
+static int yuvgen_planar(int width, int height,
+                         unsigned char *Y_start, int Y_pitch,
+                         unsigned char *U_start, int U_pitch,
+                         unsigned char *V_start, int V_pitch,
+                         int UV_interleave, int box_width, int row_shift,
+                         int field)
+{
+    int row;
+
+    /* copy Y plane */
+    for (row=0;row<height;row++) {
+        unsigned char *Y_row = Y_start + row * Y_pitch;
+        int jj, xpos, ypos;
+
+        ypos = (row / box_width) & 0x1;
+
+        /* fill garbage data into the other field */
+        if (((field == VA_TOP_FIELD) && (row &1))
+            || ((field == VA_BOTTOM_FIELD) && ((row &1)==0))) { 
+            memset(Y_row, 0xff, width);
+            continue;
+        }
+        
+        for (jj=0; jj<width; jj++) {
+            xpos = ((row_shift + jj) / box_width) & 0x1;
+                        
+            if ((xpos == 0) && (ypos == 0))
+                Y_row[jj] = 0xeb;
+            if ((xpos == 1) && (ypos == 1))
+                Y_row[jj] = 0xeb;
+                        
+            if ((xpos == 1) && (ypos == 0))
+                Y_row[jj] = 0x10;
+            if ((xpos == 0) && (ypos == 1))
+                Y_row[jj] = 0x10;
+        }
+    }
+  
+    /* copy UV data */
+    for( row =0; row < height/2; row++) {
+        unsigned short value = 0x80;
+
+        /* fill garbage data into the other field */
+        if (((field == VA_TOP_FIELD) && (row &1))
+            || ((field == VA_BOTTOM_FIELD) && ((row &1)==0))) {
+            value = 0xff;
+        }
+
+        if (UV_interleave) {
+            unsigned short *UV_row = (unsigned short *)(U_start + row * U_pitch);
+
+            memset(UV_row, value, width);
+        } else {
+            unsigned char *U_row = U_start + row * U_pitch;
+            unsigned char *V_row = V_start + row * V_pitch;
+            
+            memset (U_row,value,width/2);
+            memset (V_row,value,width/2);
+        }
+    }
+
+    return 0;
+}
+
+static int upload_surface(VADisplay va_dpy, VASurfaceID surface_id,
+                          int box_width, int row_shift,
+                          int field)
+{
+    VAImage surface_image;
+    void *surface_p=NULL, *U_start,*V_start;
+    VAStatus va_status;
+    
+    va_status = vaDeriveImage(va_dpy,surface_id,&surface_image);
+    CHECK_VASTATUS(va_status,"vaDeriveImage");
+
+    vaMapBuffer(va_dpy,surface_image.buf,&surface_p);
+    assert(VA_STATUS_SUCCESS == va_status);
+        
+    U_start = surface_p + surface_image.offsets[1];
+    V_start = surface_p + surface_image.offsets[2];
+
+    /* assume surface is planar format */
+    yuvgen_planar(surface_image.width, surface_image.height,
+                  surface_p, surface_image.pitches[0],
+                  U_start, surface_image.pitches[1],
+                  V_start, surface_image.pitches[2],
+                  (surface_image.format.fourcc==VA_FOURCC_NV12),
+                  box_width, row_shift, field);
+        
+    vaUnmapBuffer(va_dpy,surface_image.buf);
+
+    vaDestroyImage(va_dpy,surface_image.image_id);
+
+    return 0;
+}
diff --git a/va/Android.mk b/va/Android.mk
index 0e84959..bd2417b 100644
--- a/va/Android.mk
+++ b/va/Android.mk
@@ -32,11 +32,10 @@
 	x11/va_dricommon.h 
 
 LOCAL_COPY_HEADERS_TO := libva/va
-
-LOCAL_MODULE := libva
 LOCAL_MODULE_TAGS := optional
+LOCAL_MODULE := libva
 
-LOCAL_SHARED_LIBRARIES := libdl libdrm libcutils
+LOCAL_SHARED_LIBRARIES := libdl libdrm libcutils libutils
 
 include $(BUILD_SHARED_LIBRARY)
 
@@ -59,11 +58,10 @@
 LOCAL_COPY_HEADERS_TO := libva/va
 
 LOCAL_COPY_HEADERS := va_android.h		
-
-LOCAL_MODULE := libva-android
 LOCAL_MODULE_TAGS := optional
+LOCAL_MODULE := libva-android
 
-LOCAL_SHARED_LIBRARIES := libva
+LOCAL_SHARED_LIBRARIES := libva libutils
 
 include $(BUILD_SHARED_LIBRARY)
 
@@ -88,8 +86,7 @@
 	va_backend_tpi.h
 
 LOCAL_SHARED_LIBRARIES := libva
-
-LOCAL_MODULE := libva-tpi
 LOCAL_MODULE_TAGS := optional
+LOCAL_MODULE := libva-tpi
 
 include $(BUILD_SHARED_LIBRARY)
diff --git a/va/android/va_android.cpp b/va/android/va_android.cpp
index b4727aa..8632279 100644
--- a/va/android/va_android.cpp
+++ b/va/android/va_android.cpp
@@ -22,6 +22,9 @@
  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  */
 
+#include <utils/Log.h>
+#define LOG_TAG "VA-ANDROID"
+
 #define _GNU_SOURCE 1
 #include "va.h"
 #include "va_backend.h"
@@ -137,7 +140,7 @@
 
     memset(dri_state, 0, sizeof(*dri_state));
     dri_state->fd = open_device(DEVICE_NAME);
-    
+LOGE("%s:%s:%d fd=%d:Device Name=%s", __FILE__, __func__, __LINE__, dri_state->fd, DEVICE_NAME);   
     if (dri_state->fd < 0) {
         fprintf(stderr,"can't open DRM devices\n");
         return VA_STATUS_ERROR_UNKNOWN;
@@ -264,6 +267,13 @@
         }
     }
   
+    LOGE("%s:%s:%d - native dispaly = %x",
+            __FILE__,
+            __func__,
+            __LINE__,
+            //*((VADisplayContextP)dpy->native_dpy));
+            *(unsigned int*)native_dpy);
+
     return dpy;
 }
 
diff --git a/va/android/va_dummy.c b/va/android/va_dummy.c
deleted file mode 120000
index b47bd16..0000000
--- a/va/android/va_dummy.c
+++ /dev/null
@@ -1 +0,0 @@
-va_android.cpp
\ No newline at end of file
diff --git a/va/android/va_dummy.c b/va/android/va_dummy.c
new file mode 100644
index 0000000..b4727aa
--- /dev/null
+++ b/va/android/va_dummy.c
@@ -0,0 +1,335 @@
+/*
+ * Copyright (c) 2007 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.
+ */
+
+#define _GNU_SOURCE 1
+#include "va.h"
+#include "va_backend.h"
+#include "va_android.h"
+#include "va_dricommon.h" /* needs some helper functions from this file */
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <dlfcn.h>
+#include <errno.h>
+#ifndef ANDROID
+#include <libudev.h>
+#include "drmtest.h"
+#endif
+
+#define CHECK_SYMBOL(func) { if (!func) printf("func %s not found\n", #func); return VA_STATUS_ERROR_UNKNOWN; }
+#define DEVICE_NAME "/dev/card0"
+
+static VADisplayContextP pDisplayContexts = NULL;
+
+static int open_device (char *dev_name)
+{
+    struct stat st;
+    int fd;
+
+    if (-1 == stat (dev_name, &st))
+    {
+        printf ("Cannot identify '%s': %d, %s\n",
+                dev_name, errno, strerror (errno));
+        return -1;
+    }
+
+    if (!S_ISCHR (st.st_mode))
+    {
+        printf ("%s is no device\n", dev_name);
+        return -1;
+    }
+
+    fd = open (dev_name, O_RDWR);
+
+    if (-1 == fd)
+    {
+        fprintf (stderr, "Cannot open '%s': %d, %s\n",
+                 dev_name, errno, strerror (errno));
+        return -1;
+    }
+
+    return fd;
+}
+
+static int va_DisplayContextIsValid (
+    VADisplayContextP pDisplayContext
+                                  )
+{
+    VADisplayContextP ctx = pDisplayContexts;
+
+    while (ctx)
+    {
+	if (ctx == pDisplayContext && pDisplayContext->pDriverContext)
+	    return 1;
+	ctx = ctx->pNext;
+    }
+    return 0;
+}
+
+static void va_DisplayContextDestroy (
+    VADisplayContextP pDisplayContext
+)
+{
+    VADisplayContextP *ctx = &pDisplayContexts;
+
+    /* Throw away pDisplayContext */
+    while (*ctx)
+    {
+	if (*ctx == pDisplayContext)
+	{
+	    *ctx = pDisplayContext->pNext;
+	    pDisplayContext->pNext = NULL;
+	    break;
+	}
+	ctx = &((*ctx)->pNext);
+    }
+    free(pDisplayContext->pDriverContext->dri_state);
+    free(pDisplayContext->pDriverContext);
+    free(pDisplayContext);
+}
+
+#ifdef ANDROID
+static VAStatus va_DisplayContextGetDriverName (
+    VADisplayContextP pDisplayContext,
+    char **driver_name
+)
+{
+    VADriverContextP ctx = pDisplayContext->pDriverContext;
+    struct dri_state *dri_state = (struct dri_state *)ctx->dri_state;
+    char *driver_name_env;
+    int vendor_id, device_id;
+    
+    struct {
+        int vendor_id;
+        int device_id;
+        char driver_name[64];
+    } devices[] = {
+        { 0x8086, 0x4100, "pvr" },
+        { 0x8086, 0x0130, "pvr" },
+        { 0x0,    0x0,    "\0" },
+    };
+
+    memset(dri_state, 0, sizeof(*dri_state));
+    dri_state->fd = open_device(DEVICE_NAME);
+    
+    if (dri_state->fd < 0) {
+        fprintf(stderr,"can't open DRM devices\n");
+        return VA_STATUS_ERROR_UNKNOWN;
+    }
+
+    /* TBD: other vendor driver names */
+    vendor_id = devices[0].vendor_id;
+    device_id = devices[0].device_id;
+    *driver_name = strdup(devices[0].driver_name);
+        
+    dri_state->driConnectedFlag = VA_DUMMY;
+    
+    return VA_STATUS_SUCCESS;
+}
+#else
+static VAStatus va_DisplayContextGetDriverName (
+    VADisplayContextP pDisplayContext,
+    char **driver_name
+)
+{
+    VADriverContextP ctx = pDisplayContext->pDriverContext;
+    struct dri_state *dri_state = (struct dri_state *)ctx->dri_state;
+    char *driver_name_env;
+    int vendor_id, device_id;
+    int i = 0;
+    
+    struct {
+        int vendor_id;
+        int device_id;
+        char driver_name[64];
+    } devices[] = {
+        { 0x8086, 0x4100, "pvr" },
+        { 0x8086, 0x0130, "pvr" },
+        { 0x0,    0x0,    "\0" },
+    };
+
+    memset(dri_state, 0, sizeof(*dri_state));
+    dri_state->fd = drm_open_any(&vendor_id, &device_id);
+    
+    if (dri_state->fd < 0) {
+        fprintf(stderr,"can't open DRM devices\n");
+        return VA_STATUS_ERROR_UNKNOWN;
+    }
+    
+    /* TBD: other vendor driver names */
+
+    while (devices[i].device_id != 0) {
+        if ((devices[i].vendor_id == vendor_id) &&
+            (devices[i].device_id == device_id))
+            break;
+        i++;
+    }
+
+    if (devices[i].device_id != 0)
+        *driver_name = strdup(devices[i].driver_name);
+    else {
+        fprintf(stderr,"device (0x%04x:0x%04x) is not supported\n",
+                vendor_id, device_id);
+        
+        return VA_STATUS_ERROR_UNKNOWN;
+    }            
+
+    printf("DRM device is opened, loading driver %s for device 0x%04x:0x%04x\n",
+           driver_name, vendor_id, device_id);
+    
+    dri_state->driConnectedFlag = VA_DUMMY;
+    
+    return VA_STATUS_SUCCESS;
+}
+#endif
+
+VADisplay vaGetDisplay (
+    void *native_dpy /* implementation specific */
+)
+{
+    VADisplay dpy = NULL;
+    VADisplayContextP pDisplayContext = pDisplayContexts;
+
+    if (!native_dpy)
+        return NULL;
+
+    while (pDisplayContext)
+    {
+        if (pDisplayContext->pDriverContext &&
+            pDisplayContext->pDriverContext->native_dpy == (void *)native_dpy)
+        {
+            dpy = (VADisplay)pDisplayContext;
+            break;
+        }
+        pDisplayContext = pDisplayContext->pNext;
+    }
+
+
+    if (!dpy)
+    {
+        /* create new entry */
+        VADriverContextP pDriverContext;
+        struct dri_state *dri_state;
+        pDisplayContext = (VADisplayContextP)calloc(1, sizeof(*pDisplayContext));
+        pDriverContext  = (VADriverContextP)calloc(1, sizeof(*pDriverContext));
+        dri_state       = (struct dri_state*)calloc(1, sizeof(*dri_state));
+        if (pDisplayContext && pDriverContext && dri_state)
+        {
+            pDisplayContext->vadpy_magic = VA_DISPLAY_MAGIC;          
+
+            pDriverContext->native_dpy       = (void *)native_dpy;
+            pDisplayContext->pNext           = pDisplayContexts;
+            pDisplayContext->pDriverContext  = pDriverContext;
+            pDisplayContext->vaIsValid       = va_DisplayContextIsValid;
+            pDisplayContext->vaDestroy       = va_DisplayContextDestroy;
+            pDisplayContext->vaGetDriverName = va_DisplayContextGetDriverName;
+            pDisplayContexts                 = pDisplayContext;
+            pDriverContext->dri_state 	     = dri_state;
+            dpy                              = (VADisplay)pDisplayContext;
+        }
+        else
+        {
+            if (pDisplayContext)
+                free(pDisplayContext);
+            if (pDriverContext)
+                free(pDriverContext);
+            if (dri_state)
+                free(dri_state);
+        }
+    }
+  
+    return dpy;
+}
+
+#define CTX(dpy) (((VADisplayContextP)dpy)->pDriverContext)
+#define CHECK_DISPLAY(dpy) if( !vaDisplayIsValid(dpy) ) { return VA_STATUS_ERROR_INVALID_DISPLAY; }
+
+
+#ifdef ANDROID
+extern "C"  {
+    extern int fool_postp; /* do nothing for vaPutSurface if set */
+    extern int trace_flag; /* trace vaPutSurface parameters */
+
+    void va_TracePutSurface (
+        VADisplay dpy,
+        VASurfaceID surface,
+        void *draw, /* the target Drawable */
+        short srcx,
+        short srcy,
+        unsigned short srcw,
+        unsigned short srch,
+        short destx,
+        short desty,
+        unsigned short destw,
+        unsigned short desth,
+        VARectangle *cliprects, /* client supplied clip list */
+        unsigned int number_cliprects, /* number of clip rects in the clip list */
+        unsigned int flags /* de-interlacing flags */
+        );
+}
+
+#define VA_TRACE(trace_func,...)                \
+    if (trace_flag) {                           \
+        trace_func(__VA_ARGS__);                \
+    }
+
+VAStatus vaPutSurface (
+    VADisplay dpy,
+    VASurfaceID surface,
+    sp<ISurface> draw, /* Android Surface/Window */
+    short srcx,
+    short srcy,
+    unsigned short srcw,
+    unsigned short srch,
+    short destx,
+    short desty,
+    unsigned short destw,
+    unsigned short desth,
+    VARectangle *cliprects, /* client supplied clip list */
+    unsigned int number_cliprects, /* number of clip rects in the clip list */
+    unsigned int flags /* de-interlacing flags */
+)
+{
+    VADriverContextP ctx;
+
+    if (fool_postp)
+        return VA_STATUS_SUCCESS;
+
+    CHECK_DISPLAY(dpy);
+    ctx = CTX(dpy);
+
+    VA_TRACE(va_TracePutSurface, dpy, surface, static_cast<void*>(&draw), srcx, srcy, srcw, srch,
+             destx, desty, destw, desth,
+             cliprects, number_cliprects, flags );
+    
+    return ctx->vtable.vaPutSurface( ctx, surface, static_cast<void*>(&draw), srcx, srcy, srcw, srch, 
+                                     destx, desty, destw, desth,
+                                     cliprects, number_cliprects, flags );
+}
+#endif
diff --git a/va/dummy b/va/dummy
deleted file mode 120000
index 1fd74d1..0000000
--- a/va/dummy
+++ /dev/null
@@ -1 +0,0 @@
-android
\ No newline at end of file
diff --git a/va/dummy/Makefile.am b/va/dummy/Makefile.am
new file mode 100644
index 0000000..04c2724
--- /dev/null
+++ b/va/dummy/Makefile.am
@@ -0,0 +1,33 @@
+# Copyright (c) 2007 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.
+
+
+AM_CFLAGS = -DLINUX -I$(top_srcdir)/va -I$(top_srcdir)/va/x11 $(DRM_CFLAGS)
+
+noinst_LTLIBRARIES = libva_dummy.la	
+
+libva_dummy_la_LIBADD = $(LIBVA_LIBS) -ldl -ludev
+
+libva_dummyincludedir = ${includedir}/va
+
+libva_dummy_la_SOURCES = va_dummy.c drmtest.c
+
diff --git a/va/dummy/drmtest.c b/va/dummy/drmtest.c
new file mode 100644
index 0000000..444ef47
--- /dev/null
+++ b/va/dummy/drmtest.c
@@ -0,0 +1,139 @@
+/*
+ * Copyright © 2007 Intel Corporation
+ *
+ * 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, sublicense,
+ * 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 NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS 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.
+ *
+ * Authors:
+ *    Eric Anholt <eric@anholt.net>
+ *
+ */
+
+#include <string.h>
+#include <fcntl.h>
+#include <fnmatch.h>
+#include <sys/stat.h>
+#include <sys/ioctl.h>
+#include "drmtest.h"
+
+#define LIBUDEV_I_KNOW_THE_API_IS_SUBJECT_TO_CHANGE
+#include <libudev.h>
+
+static int is_master(int fd)
+{
+	drm_client_t client;
+	int ret;
+
+	/* Check that we're the only opener and authed. */
+	client.idx = 0;
+	ret = ioctl(fd, DRM_IOCTL_GET_CLIENT, &client);
+	assert (ret == 0);
+	if (!client.auth)
+		return 0;
+	client.idx = 1;
+	ret = ioctl(fd, DRM_IOCTL_GET_CLIENT, &client);
+	if (ret != -1 || errno != EINVAL)
+		return 0;
+
+	return 1;
+}
+
+/** Open the first DRM device matching the criteria */
+int drm_open_matching(const char *pci_glob, int flags, int *vendor_id, int *device_id)
+{
+	struct udev *udev;
+	struct udev_enumerate *e;
+	struct udev_device *device, *parent;
+        struct udev_list_entry *entry;
+	const char *pci_id, *path;
+        char *tmp;
+	int fd;
+
+        *vendor_id = 0;
+        *device_id = 0;
+        
+	udev = udev_new();
+	if (udev == NULL) {
+		fprintf(stderr, "failed to initialize udev context\n");
+                return -1;
+		//abort();
+	}
+
+	fd = -1;
+	e = udev_enumerate_new(udev);
+	udev_enumerate_add_match_subsystem(e, "drm");
+        udev_enumerate_scan_devices(e);
+        udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
+		path = udev_list_entry_get_name(entry);
+		device = udev_device_new_from_syspath(udev, path);
+		parent = udev_device_get_parent(device);
+		/* Filter out KMS output devices. */
+		if (strcmp(udev_device_get_subsystem(parent), "pci") != 0)
+			continue;
+		pci_id = udev_device_get_property_value(parent, "PCI_ID");
+		if (fnmatch(pci_glob, pci_id, 0) != 0)
+			continue;
+		fd = open(udev_device_get_devnode(device), O_RDWR);
+		if (fd < 0)
+			continue;
+		if ((flags & DRM_TEST_MASTER) && !is_master(fd)) {
+			close(fd);
+			fd = -1;
+			continue;
+		}
+
+		break;
+	}
+        udev_enumerate_unref(e);
+	udev_unref(udev);
+
+        *vendor_id = (int) strtol(pci_id, &tmp, 16);
+        *device_id = (int) strtol((tmp+1), NULL, 16);
+        
+	return fd;
+}
+
+int drm_open_any(int *vendor_id, int *device_id)
+{
+        int fd = drm_open_matching("*:*", 0, vendor_id, device_id);
+
+	if (fd < 0) {
+		fprintf(stderr, "failed to open any drm device\n");
+		//abort();
+	}
+
+	return fd;
+}
+
+/**
+ * Open the first DRM device we can find where we end up being the master.
+ */
+int drm_open_any_master(void)
+{
+        int vendor_id, device_id;
+	int fd = drm_open_matching("*:*", DRM_TEST_MASTER, &vendor_id, &device_id);
+
+	if (fd < 0) {
+		fprintf(stderr, "failed to open any drm device\n");
+		abort();
+	}
+
+	return fd;
+
+}
diff --git a/va/dummy/drmtest.h b/va/dummy/drmtest.h
new file mode 100644
index 0000000..5f10f08
--- /dev/null
+++ b/va/dummy/drmtest.h
@@ -0,0 +1,40 @@
+/*
+ * Copyright © 2007 Intel Corporation
+ *
+ * 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, sublicense,
+ * 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 NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS 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.
+ *
+ * Authors:
+ *    Eric Anholt <eric@anholt.net>
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <assert.h>
+#include <errno.h>
+
+#include "xf86drm.h"
+
+#define DRM_TEST_MASTER 0x01
+
+int drm_open_any(int *vendor_id, int *device_id);
+int drm_open_any_master(void);
+int drm_open_matching(const char *pci_glob, int flags, int *vendor_id, int *device_id);
diff --git a/va/dummy/va_android.cpp b/va/dummy/va_android.cpp
new file mode 100644
index 0000000..b4727aa
--- /dev/null
+++ b/va/dummy/va_android.cpp
@@ -0,0 +1,335 @@
+/*
+ * Copyright (c) 2007 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.
+ */
+
+#define _GNU_SOURCE 1
+#include "va.h"
+#include "va_backend.h"
+#include "va_android.h"
+#include "va_dricommon.h" /* needs some helper functions from this file */
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <dlfcn.h>
+#include <errno.h>
+#ifndef ANDROID
+#include <libudev.h>
+#include "drmtest.h"
+#endif
+
+#define CHECK_SYMBOL(func) { if (!func) printf("func %s not found\n", #func); return VA_STATUS_ERROR_UNKNOWN; }
+#define DEVICE_NAME "/dev/card0"
+
+static VADisplayContextP pDisplayContexts = NULL;
+
+static int open_device (char *dev_name)
+{
+    struct stat st;
+    int fd;
+
+    if (-1 == stat (dev_name, &st))
+    {
+        printf ("Cannot identify '%s': %d, %s\n",
+                dev_name, errno, strerror (errno));
+        return -1;
+    }
+
+    if (!S_ISCHR (st.st_mode))
+    {
+        printf ("%s is no device\n", dev_name);
+        return -1;
+    }
+
+    fd = open (dev_name, O_RDWR);
+
+    if (-1 == fd)
+    {
+        fprintf (stderr, "Cannot open '%s': %d, %s\n",
+                 dev_name, errno, strerror (errno));
+        return -1;
+    }
+
+    return fd;
+}
+
+static int va_DisplayContextIsValid (
+    VADisplayContextP pDisplayContext
+                                  )
+{
+    VADisplayContextP ctx = pDisplayContexts;
+
+    while (ctx)
+    {
+	if (ctx == pDisplayContext && pDisplayContext->pDriverContext)
+	    return 1;
+	ctx = ctx->pNext;
+    }
+    return 0;
+}
+
+static void va_DisplayContextDestroy (
+    VADisplayContextP pDisplayContext
+)
+{
+    VADisplayContextP *ctx = &pDisplayContexts;
+
+    /* Throw away pDisplayContext */
+    while (*ctx)
+    {
+	if (*ctx == pDisplayContext)
+	{
+	    *ctx = pDisplayContext->pNext;
+	    pDisplayContext->pNext = NULL;
+	    break;
+	}
+	ctx = &((*ctx)->pNext);
+    }
+    free(pDisplayContext->pDriverContext->dri_state);
+    free(pDisplayContext->pDriverContext);
+    free(pDisplayContext);
+}
+
+#ifdef ANDROID
+static VAStatus va_DisplayContextGetDriverName (
+    VADisplayContextP pDisplayContext,
+    char **driver_name
+)
+{
+    VADriverContextP ctx = pDisplayContext->pDriverContext;
+    struct dri_state *dri_state = (struct dri_state *)ctx->dri_state;
+    char *driver_name_env;
+    int vendor_id, device_id;
+    
+    struct {
+        int vendor_id;
+        int device_id;
+        char driver_name[64];
+    } devices[] = {
+        { 0x8086, 0x4100, "pvr" },
+        { 0x8086, 0x0130, "pvr" },
+        { 0x0,    0x0,    "\0" },
+    };
+
+    memset(dri_state, 0, sizeof(*dri_state));
+    dri_state->fd = open_device(DEVICE_NAME);
+    
+    if (dri_state->fd < 0) {
+        fprintf(stderr,"can't open DRM devices\n");
+        return VA_STATUS_ERROR_UNKNOWN;
+    }
+
+    /* TBD: other vendor driver names */
+    vendor_id = devices[0].vendor_id;
+    device_id = devices[0].device_id;
+    *driver_name = strdup(devices[0].driver_name);
+        
+    dri_state->driConnectedFlag = VA_DUMMY;
+    
+    return VA_STATUS_SUCCESS;
+}
+#else
+static VAStatus va_DisplayContextGetDriverName (
+    VADisplayContextP pDisplayContext,
+    char **driver_name
+)
+{
+    VADriverContextP ctx = pDisplayContext->pDriverContext;
+    struct dri_state *dri_state = (struct dri_state *)ctx->dri_state;
+    char *driver_name_env;
+    int vendor_id, device_id;
+    int i = 0;
+    
+    struct {
+        int vendor_id;
+        int device_id;
+        char driver_name[64];
+    } devices[] = {
+        { 0x8086, 0x4100, "pvr" },
+        { 0x8086, 0x0130, "pvr" },
+        { 0x0,    0x0,    "\0" },
+    };
+
+    memset(dri_state, 0, sizeof(*dri_state));
+    dri_state->fd = drm_open_any(&vendor_id, &device_id);
+    
+    if (dri_state->fd < 0) {
+        fprintf(stderr,"can't open DRM devices\n");
+        return VA_STATUS_ERROR_UNKNOWN;
+    }
+    
+    /* TBD: other vendor driver names */
+
+    while (devices[i].device_id != 0) {
+        if ((devices[i].vendor_id == vendor_id) &&
+            (devices[i].device_id == device_id))
+            break;
+        i++;
+    }
+
+    if (devices[i].device_id != 0)
+        *driver_name = strdup(devices[i].driver_name);
+    else {
+        fprintf(stderr,"device (0x%04x:0x%04x) is not supported\n",
+                vendor_id, device_id);
+        
+        return VA_STATUS_ERROR_UNKNOWN;
+    }            
+
+    printf("DRM device is opened, loading driver %s for device 0x%04x:0x%04x\n",
+           driver_name, vendor_id, device_id);
+    
+    dri_state->driConnectedFlag = VA_DUMMY;
+    
+    return VA_STATUS_SUCCESS;
+}
+#endif
+
+VADisplay vaGetDisplay (
+    void *native_dpy /* implementation specific */
+)
+{
+    VADisplay dpy = NULL;
+    VADisplayContextP pDisplayContext = pDisplayContexts;
+
+    if (!native_dpy)
+        return NULL;
+
+    while (pDisplayContext)
+    {
+        if (pDisplayContext->pDriverContext &&
+            pDisplayContext->pDriverContext->native_dpy == (void *)native_dpy)
+        {
+            dpy = (VADisplay)pDisplayContext;
+            break;
+        }
+        pDisplayContext = pDisplayContext->pNext;
+    }
+
+
+    if (!dpy)
+    {
+        /* create new entry */
+        VADriverContextP pDriverContext;
+        struct dri_state *dri_state;
+        pDisplayContext = (VADisplayContextP)calloc(1, sizeof(*pDisplayContext));
+        pDriverContext  = (VADriverContextP)calloc(1, sizeof(*pDriverContext));
+        dri_state       = (struct dri_state*)calloc(1, sizeof(*dri_state));
+        if (pDisplayContext && pDriverContext && dri_state)
+        {
+            pDisplayContext->vadpy_magic = VA_DISPLAY_MAGIC;          
+
+            pDriverContext->native_dpy       = (void *)native_dpy;
+            pDisplayContext->pNext           = pDisplayContexts;
+            pDisplayContext->pDriverContext  = pDriverContext;
+            pDisplayContext->vaIsValid       = va_DisplayContextIsValid;
+            pDisplayContext->vaDestroy       = va_DisplayContextDestroy;
+            pDisplayContext->vaGetDriverName = va_DisplayContextGetDriverName;
+            pDisplayContexts                 = pDisplayContext;
+            pDriverContext->dri_state 	     = dri_state;
+            dpy                              = (VADisplay)pDisplayContext;
+        }
+        else
+        {
+            if (pDisplayContext)
+                free(pDisplayContext);
+            if (pDriverContext)
+                free(pDriverContext);
+            if (dri_state)
+                free(dri_state);
+        }
+    }
+  
+    return dpy;
+}
+
+#define CTX(dpy) (((VADisplayContextP)dpy)->pDriverContext)
+#define CHECK_DISPLAY(dpy) if( !vaDisplayIsValid(dpy) ) { return VA_STATUS_ERROR_INVALID_DISPLAY; }
+
+
+#ifdef ANDROID
+extern "C"  {
+    extern int fool_postp; /* do nothing for vaPutSurface if set */
+    extern int trace_flag; /* trace vaPutSurface parameters */
+
+    void va_TracePutSurface (
+        VADisplay dpy,
+        VASurfaceID surface,
+        void *draw, /* the target Drawable */
+        short srcx,
+        short srcy,
+        unsigned short srcw,
+        unsigned short srch,
+        short destx,
+        short desty,
+        unsigned short destw,
+        unsigned short desth,
+        VARectangle *cliprects, /* client supplied clip list */
+        unsigned int number_cliprects, /* number of clip rects in the clip list */
+        unsigned int flags /* de-interlacing flags */
+        );
+}
+
+#define VA_TRACE(trace_func,...)                \
+    if (trace_flag) {                           \
+        trace_func(__VA_ARGS__);                \
+    }
+
+VAStatus vaPutSurface (
+    VADisplay dpy,
+    VASurfaceID surface,
+    sp<ISurface> draw, /* Android Surface/Window */
+    short srcx,
+    short srcy,
+    unsigned short srcw,
+    unsigned short srch,
+    short destx,
+    short desty,
+    unsigned short destw,
+    unsigned short desth,
+    VARectangle *cliprects, /* client supplied clip list */
+    unsigned int number_cliprects, /* number of clip rects in the clip list */
+    unsigned int flags /* de-interlacing flags */
+)
+{
+    VADriverContextP ctx;
+
+    if (fool_postp)
+        return VA_STATUS_SUCCESS;
+
+    CHECK_DISPLAY(dpy);
+    ctx = CTX(dpy);
+
+    VA_TRACE(va_TracePutSurface, dpy, surface, static_cast<void*>(&draw), srcx, srcy, srcw, srch,
+             destx, desty, destw, desth,
+             cliprects, number_cliprects, flags );
+    
+    return ctx->vtable.vaPutSurface( ctx, surface, static_cast<void*>(&draw), srcx, srcy, srcw, srch, 
+                                     destx, desty, destw, desth,
+                                     cliprects, number_cliprects, flags );
+}
+#endif
diff --git a/va/dummy/va_dummy.c b/va/dummy/va_dummy.c
new file mode 100644
index 0000000..b4727aa
--- /dev/null
+++ b/va/dummy/va_dummy.c
@@ -0,0 +1,335 @@
+/*
+ * Copyright (c) 2007 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.
+ */
+
+#define _GNU_SOURCE 1
+#include "va.h"
+#include "va_backend.h"
+#include "va_android.h"
+#include "va_dricommon.h" /* needs some helper functions from this file */
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <dlfcn.h>
+#include <errno.h>
+#ifndef ANDROID
+#include <libudev.h>
+#include "drmtest.h"
+#endif
+
+#define CHECK_SYMBOL(func) { if (!func) printf("func %s not found\n", #func); return VA_STATUS_ERROR_UNKNOWN; }
+#define DEVICE_NAME "/dev/card0"
+
+static VADisplayContextP pDisplayContexts = NULL;
+
+static int open_device (char *dev_name)
+{
+    struct stat st;
+    int fd;
+
+    if (-1 == stat (dev_name, &st))
+    {
+        printf ("Cannot identify '%s': %d, %s\n",
+                dev_name, errno, strerror (errno));
+        return -1;
+    }
+
+    if (!S_ISCHR (st.st_mode))
+    {
+        printf ("%s is no device\n", dev_name);
+        return -1;
+    }
+
+    fd = open (dev_name, O_RDWR);
+
+    if (-1 == fd)
+    {
+        fprintf (stderr, "Cannot open '%s': %d, %s\n",
+                 dev_name, errno, strerror (errno));
+        return -1;
+    }
+
+    return fd;
+}
+
+static int va_DisplayContextIsValid (
+    VADisplayContextP pDisplayContext
+                                  )
+{
+    VADisplayContextP ctx = pDisplayContexts;
+
+    while (ctx)
+    {
+	if (ctx == pDisplayContext && pDisplayContext->pDriverContext)
+	    return 1;
+	ctx = ctx->pNext;
+    }
+    return 0;
+}
+
+static void va_DisplayContextDestroy (
+    VADisplayContextP pDisplayContext
+)
+{
+    VADisplayContextP *ctx = &pDisplayContexts;
+
+    /* Throw away pDisplayContext */
+    while (*ctx)
+    {
+	if (*ctx == pDisplayContext)
+	{
+	    *ctx = pDisplayContext->pNext;
+	    pDisplayContext->pNext = NULL;
+	    break;
+	}
+	ctx = &((*ctx)->pNext);
+    }
+    free(pDisplayContext->pDriverContext->dri_state);
+    free(pDisplayContext->pDriverContext);
+    free(pDisplayContext);
+}
+
+#ifdef ANDROID
+static VAStatus va_DisplayContextGetDriverName (
+    VADisplayContextP pDisplayContext,
+    char **driver_name
+)
+{
+    VADriverContextP ctx = pDisplayContext->pDriverContext;
+    struct dri_state *dri_state = (struct dri_state *)ctx->dri_state;
+    char *driver_name_env;
+    int vendor_id, device_id;
+    
+    struct {
+        int vendor_id;
+        int device_id;
+        char driver_name[64];
+    } devices[] = {
+        { 0x8086, 0x4100, "pvr" },
+        { 0x8086, 0x0130, "pvr" },
+        { 0x0,    0x0,    "\0" },
+    };
+
+    memset(dri_state, 0, sizeof(*dri_state));
+    dri_state->fd = open_device(DEVICE_NAME);
+    
+    if (dri_state->fd < 0) {
+        fprintf(stderr,"can't open DRM devices\n");
+        return VA_STATUS_ERROR_UNKNOWN;
+    }
+
+    /* TBD: other vendor driver names */
+    vendor_id = devices[0].vendor_id;
+    device_id = devices[0].device_id;
+    *driver_name = strdup(devices[0].driver_name);
+        
+    dri_state->driConnectedFlag = VA_DUMMY;
+    
+    return VA_STATUS_SUCCESS;
+}
+#else
+static VAStatus va_DisplayContextGetDriverName (
+    VADisplayContextP pDisplayContext,
+    char **driver_name
+)
+{
+    VADriverContextP ctx = pDisplayContext->pDriverContext;
+    struct dri_state *dri_state = (struct dri_state *)ctx->dri_state;
+    char *driver_name_env;
+    int vendor_id, device_id;
+    int i = 0;
+    
+    struct {
+        int vendor_id;
+        int device_id;
+        char driver_name[64];
+    } devices[] = {
+        { 0x8086, 0x4100, "pvr" },
+        { 0x8086, 0x0130, "pvr" },
+        { 0x0,    0x0,    "\0" },
+    };
+
+    memset(dri_state, 0, sizeof(*dri_state));
+    dri_state->fd = drm_open_any(&vendor_id, &device_id);
+    
+    if (dri_state->fd < 0) {
+        fprintf(stderr,"can't open DRM devices\n");
+        return VA_STATUS_ERROR_UNKNOWN;
+    }
+    
+    /* TBD: other vendor driver names */
+
+    while (devices[i].device_id != 0) {
+        if ((devices[i].vendor_id == vendor_id) &&
+            (devices[i].device_id == device_id))
+            break;
+        i++;
+    }
+
+    if (devices[i].device_id != 0)
+        *driver_name = strdup(devices[i].driver_name);
+    else {
+        fprintf(stderr,"device (0x%04x:0x%04x) is not supported\n",
+                vendor_id, device_id);
+        
+        return VA_STATUS_ERROR_UNKNOWN;
+    }            
+
+    printf("DRM device is opened, loading driver %s for device 0x%04x:0x%04x\n",
+           driver_name, vendor_id, device_id);
+    
+    dri_state->driConnectedFlag = VA_DUMMY;
+    
+    return VA_STATUS_SUCCESS;
+}
+#endif
+
+VADisplay vaGetDisplay (
+    void *native_dpy /* implementation specific */
+)
+{
+    VADisplay dpy = NULL;
+    VADisplayContextP pDisplayContext = pDisplayContexts;
+
+    if (!native_dpy)
+        return NULL;
+
+    while (pDisplayContext)
+    {
+        if (pDisplayContext->pDriverContext &&
+            pDisplayContext->pDriverContext->native_dpy == (void *)native_dpy)
+        {
+            dpy = (VADisplay)pDisplayContext;
+            break;
+        }
+        pDisplayContext = pDisplayContext->pNext;
+    }
+
+
+    if (!dpy)
+    {
+        /* create new entry */
+        VADriverContextP pDriverContext;
+        struct dri_state *dri_state;
+        pDisplayContext = (VADisplayContextP)calloc(1, sizeof(*pDisplayContext));
+        pDriverContext  = (VADriverContextP)calloc(1, sizeof(*pDriverContext));
+        dri_state       = (struct dri_state*)calloc(1, sizeof(*dri_state));
+        if (pDisplayContext && pDriverContext && dri_state)
+        {
+            pDisplayContext->vadpy_magic = VA_DISPLAY_MAGIC;          
+
+            pDriverContext->native_dpy       = (void *)native_dpy;
+            pDisplayContext->pNext           = pDisplayContexts;
+            pDisplayContext->pDriverContext  = pDriverContext;
+            pDisplayContext->vaIsValid       = va_DisplayContextIsValid;
+            pDisplayContext->vaDestroy       = va_DisplayContextDestroy;
+            pDisplayContext->vaGetDriverName = va_DisplayContextGetDriverName;
+            pDisplayContexts                 = pDisplayContext;
+            pDriverContext->dri_state 	     = dri_state;
+            dpy                              = (VADisplay)pDisplayContext;
+        }
+        else
+        {
+            if (pDisplayContext)
+                free(pDisplayContext);
+            if (pDriverContext)
+                free(pDriverContext);
+            if (dri_state)
+                free(dri_state);
+        }
+    }
+  
+    return dpy;
+}
+
+#define CTX(dpy) (((VADisplayContextP)dpy)->pDriverContext)
+#define CHECK_DISPLAY(dpy) if( !vaDisplayIsValid(dpy) ) { return VA_STATUS_ERROR_INVALID_DISPLAY; }
+
+
+#ifdef ANDROID
+extern "C"  {
+    extern int fool_postp; /* do nothing for vaPutSurface if set */
+    extern int trace_flag; /* trace vaPutSurface parameters */
+
+    void va_TracePutSurface (
+        VADisplay dpy,
+        VASurfaceID surface,
+        void *draw, /* the target Drawable */
+        short srcx,
+        short srcy,
+        unsigned short srcw,
+        unsigned short srch,
+        short destx,
+        short desty,
+        unsigned short destw,
+        unsigned short desth,
+        VARectangle *cliprects, /* client supplied clip list */
+        unsigned int number_cliprects, /* number of clip rects in the clip list */
+        unsigned int flags /* de-interlacing flags */
+        );
+}
+
+#define VA_TRACE(trace_func,...)                \
+    if (trace_flag) {                           \
+        trace_func(__VA_ARGS__);                \
+    }
+
+VAStatus vaPutSurface (
+    VADisplay dpy,
+    VASurfaceID surface,
+    sp<ISurface> draw, /* Android Surface/Window */
+    short srcx,
+    short srcy,
+    unsigned short srcw,
+    unsigned short srch,
+    short destx,
+    short desty,
+    unsigned short destw,
+    unsigned short desth,
+    VARectangle *cliprects, /* client supplied clip list */
+    unsigned int number_cliprects, /* number of clip rects in the clip list */
+    unsigned int flags /* de-interlacing flags */
+)
+{
+    VADriverContextP ctx;
+
+    if (fool_postp)
+        return VA_STATUS_SUCCESS;
+
+    CHECK_DISPLAY(dpy);
+    ctx = CTX(dpy);
+
+    VA_TRACE(va_TracePutSurface, dpy, surface, static_cast<void*>(&draw), srcx, srcy, srcw, srch,
+             destx, desty, destw, desth,
+             cliprects, number_cliprects, flags );
+    
+    return ctx->vtable.vaPutSurface( ctx, surface, static_cast<void*>(&draw), srcx, srcy, srcw, srch, 
+                                     destx, desty, destw, desth,
+                                     cliprects, number_cliprects, flags );
+}
+#endif
diff --git a/va/va.c b/va/va.c
index 8cda412..d19251c 100644
--- a/va/va.c
+++ b/va/va.c
@@ -22,6 +22,9 @@
  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  */
 
+#include <utils/Log.h>
+#define LOG_TAG "VA"
+
 #define _GNU_SOURCE 1
 #include "va.h"
 #include "va_backend.h"
@@ -195,6 +198,12 @@
     driver_dir = strtok_r((const char *)search_path, ":", &saveptr);
     while(driver_dir)
     {
+                    LOGE("%s:%s:%d",
+                    __FILE__,
+                    __func__,
+                    __LINE__);
+
+
         void *handle = NULL;
         char *driver_path = (char *) malloc( strlen(driver_dir) +
                                              strlen(driver_name) +
@@ -208,6 +217,7 @@
 #ifndef ANDROID
         handle = dlopen( driver_path, RTLD_NOW | RTLD_GLOBAL | RTLD_NODELETE );
 #else
+        LOGE("ready to dlopen pvr_drv_video - name = %s", driver_path);
         handle = dlopen( driver_path, RTLD_NOW| RTLD_GLOBAL);
 #endif
         if (!handle)
@@ -220,6 +230,11 @@
         }
         else
         {
+            LOGE("%s:%s:%d",
+                    __FILE__,
+                    __func__,
+                    __LINE__);
+
             VADriverInit init_func;
             init_func = (VADriverInit) dlsym(handle, DRIVER_INIT_FUNC);
             if (!init_func)
@@ -229,10 +244,24 @@
             }
             else
             {
-                vaStatus = (*init_func)(ctx);
+                  LOGE("%s:%s:%d",
+                        __FILE__,
+                        __func__,
+                        __LINE__);
 
+
+                vaStatus = (*init_func)(ctx);
+                  LOGE("%s:%s:%d:%d",
+                        __FILE__,
+                        __func__,
+                        __LINE__,
+                        vaStatus);
                 if (VA_STATUS_SUCCESS == vaStatus)
                 {
+                  LOGE("%s:%s:%d",
+                        __FILE__,
+                        __func__,
+                        __LINE__);
                     CHECK_MAXIMUM(vaStatus, ctx, profiles);
                     CHECK_MAXIMUM(vaStatus, ctx, entrypoints);
                     CHECK_MAXIMUM(vaStatus, ctx, attributes);
@@ -298,7 +327,7 @@
         
         driver_dir = strtok_r(NULL, ":", &saveptr);
     }
-    
+
     free(search_path);    
     
     return vaStatus;
@@ -383,6 +412,12 @@
     int *minor_version 	 /* out */
 )
 {
+    LOGE("%s:%s:%d - dpy = %x", 
+            __FILE__,
+            __func__,
+            __LINE__,
+            dpy);
+
     const char *driver_name_env = NULL;
     char *driver_name = NULL;
     VAStatus vaStatus;
@@ -396,9 +431,12 @@
     va_infoMessage("libva version %s\n", VA_VERSION_S);
 
     driver_name_env = getenv("LIBVA_DRIVER_NAME");
+
+
     if (driver_name_env && geteuid() == getuid())
     {
         /* Don't allow setuid apps to use LIBVA_DRIVER_NAME */
+    LOGE("%s:%s:%d - driver_name_env = %s",__FILE__,__func__,__LINE__, driver_name_env);
         driver_name = strdup(driver_name_env);
         vaStatus = VA_STATUS_SUCCESS;
         va_infoMessage("User requested driver '%s'\n", driver_name);
@@ -406,9 +444,16 @@
     else
     {
         vaStatus = va_getDriverName(dpy, &driver_name);
+    LOGE("%s:%s:%d - driver_name = %s",__FILE__,__func__,__LINE__, driver_name);
         va_infoMessage("va_getDriverName() returns %d\n", vaStatus);
     }
 
+    LOGE("%s:%s:%d - driver name = %s, display=%x", 
+            __FILE__,
+            __func__,
+            __LINE__,
+            driver_name, (unsigned)dpy);
+
     if (VA_STATUS_SUCCESS == vaStatus)
     {
         vaStatus = va_openDriver(dpy, driver_name);
@@ -418,6 +463,13 @@
         *minor_version = VA_MINOR_VERSION;
     }
 
+        
+    LOGE("%s:%s:%d - driver name = %s", 
+            __FILE__,
+            __func__,
+            __LINE__,
+            driver_name);
+
     if (driver_name)
         free(driver_name);
     return vaStatus;
diff --git a/va/va_dummy.h b/va/va_dummy.h
deleted file mode 120000
index 69128f8..0000000
--- a/va/va_dummy.h
+++ /dev/null
@@ -1 +0,0 @@
-va_android.h
\ No newline at end of file
diff --git a/va/va_dummy.h b/va/va_dummy.h
new file mode 100644
index 0000000..7b98949
--- /dev/null
+++ b/va/va_dummy.h
@@ -0,0 +1,54 @@
+#ifndef _VA_ANDROID_H_
+#define _VA_ANDROID_H_
+
+#include <va/va.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*
+ * Returns a suitable VADisplay for VA API
+ */
+VADisplay vaGetDisplay (
+    void *android_dpy
+);
+
+#ifdef __cplusplus
+}
+#endif
+
+#ifdef __cplusplus
+#ifdef ANDROID
+#include <surfaceflinger/ISurface.h>
+using namespace android;
+
+/*
+ * Output rendering
+ * Following is the rendering interface for Android system, 
+ * to get the decode output surface to an ISurface object.
+ * It basically performs a de-interlacing (if needed), 
+ * color space conversion and scaling to the destination
+ * rectangle
+ */
+VAStatus vaPutSurface (
+    VADisplay dpy,
+    VASurfaceID surface,	
+    sp<ISurface> draw, /* Android Window/Surface */
+    short srcx,
+    short srcy,
+    unsigned short srcw,
+    unsigned short srch,
+    short destx,
+    short desty,
+    unsigned short destw,
+    unsigned short desth,
+    VARectangle *cliprects, /* client supplied destination clip list */
+    unsigned int number_cliprects, /* number of clip rects in the clip list */
+    unsigned int flags /* PutSurface flags */
+);
+
+#endif /* ANDROID */
+#endif /* __cplusplus */
+
+#endif /* _VA_ANDROID_H_ */