OSDN Git Service

Initial android backend
[android-x86/hardware-intel-common-libva.git] / va / android / va_android.c
1 /*
2  * Copyright (c) 2007 Intel Corporation. All Rights Reserved.
3  *
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:
11  * 
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial portions
14  * of the Software.
15  * 
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.
23  */
24
25 #define _GNU_SOURCE 1
26 #include "config.h"
27 #include "va.h"
28 #include "va_backend.h"
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <stdarg.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <fcntl.h>
37 #include <errno.h>
38
39 static VADisplayContextP pDisplayContexts = NULL;
40
41 static int va_DisplayContextIsValid (
42     VADisplayContextP pDisplayContext
43 )
44 {
45     VADisplayContextP ctx = pDisplayContexts;
46
47     while (ctx)
48     {
49         if (ctx == pDisplayContext && pDisplayContext->pDriverContext)
50             return 1;
51         ctx = ctx->pNext;
52     }
53     return 0;
54 }
55
56 static void va_DisplayContextDestroy (
57     VADisplayContextP pDisplayContext
58 )
59 {
60     VADisplayContextP *ctx = &pDisplayContexts;
61
62     /* Throw away pDisplayContext */
63     while (*ctx)
64     {
65         if (*ctx == pDisplayContext)
66         {
67             *ctx = pDisplayContext->pNext;
68             pDisplayContext->pNext = NULL;
69             break;
70         }
71         ctx = &((*ctx)->pNext);
72     }
73     free(pDisplayContext->pDriverContext->dri_state);
74     free(pDisplayContext->pDriverContext);
75     free(pDisplayContext);
76 }
77
78
79 static VAStatus va_DisplayContextGetDriverName (
80     VADisplayContextP pDisplayContext,
81     char **driver_name
82 )
83 {
84     VAStatus vaStatus VA_STATUS_SUCCESS;
85     char *driver_name_env;
86     struct {
87         unsigned int verndor_id;
88         unsigned int device_id;
89         char driver_name[64];
90     } devices[] = {
91         { 0x8086, 0x4100, "pvr" },
92     };
93
94     if (driver_name)
95         *driver_name = NULL;
96
97     if ((driver_name_env = getenv("LIBVA_DRIVER_NAME")) != NULL
98         && geteuid() == getuid())
99     {
100         /* don't allow setuid apps to use LIBVA_DRIVER_NAME */
101         *driver_name = strdup(driver_name_env);
102         return VA_STATUS_SUCCESS;
103     }
104
105     *driver_name = strdup(devices[0].driver_name);
106     
107     return vaStatus;
108 }
109
110
111 VADisplay vaGetDisplay (
112     Display *native_dpy /* implementation specific */
113 )
114 {
115   VADisplay dpy = NULL;
116   VADisplayContextP pDisplayContext = pDisplayContexts;
117
118   if (!native_dpy)
119       return NULL;
120
121   while (pDisplayContext)
122   {
123       if (pDisplayContext->pDriverContext &&
124           pDisplayContext->pDriverContext->native_dpy == (void *)native_dpy)
125       {
126           dpy = (VADisplay)pDisplayContext;
127           break;
128       }
129       pDisplayContext = pDisplayContext->pNext;
130   }
131
132   if (!dpy)
133   {
134       /* create new entry */
135       VADriverContextP pDriverContext;
136       struct dri_state *dri_state;
137       pDisplayContext = calloc(1, sizeof(*pDisplayContext));
138       pDriverContext  = calloc(1, sizeof(*pDriverContext));
139       if (pDisplayContext && pDriverContext && dri_state)
140       {
141           pDisplayContext->vadpy_magic = VA_DISPLAY_MAGIC;          
142
143           pDriverContext->native_dpy       = (void *)native_dpy;
144           pDisplayContext->pNext           = pDisplayContexts;
145           pDisplayContext->pDriverContext  = pDriverContext;
146           pDisplayContext->vaIsValid       = va_DisplayContextIsValid;
147           pDisplayContext->vaDestroy       = va_DisplayContextDestroy;
148           pDisplayContext->vaGetDriverName = va_DisplayContextGetDriverName;
149           pDisplayContexts                 = pDisplayContext;
150           pDriverContext->dri_state        = dri_state;
151           dpy                              = (VADisplay)pDisplayContext;
152       }
153       else
154       {
155           if (pDisplayContext)
156               free(pDisplayContext);
157           if (pDriverContext)
158               free(pDriverContext);
159       }
160   }
161   
162   return dpy;
163 }