687b86da7a4c7a2c197ff2f7b9ea068cfbc19ef3
[profile/ivi/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 "va.h"
27 #include "va_backend.h"
28 #include "va_android.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);
74     free(pDisplayContext);
75 }
76
77
78 static VAStatus va_DisplayContextGetDriverName (
79     VADisplayContextP pDisplayContext,
80     char **driver_name
81 )
82 {  
83     char *driver_name_env;
84     struct {
85         unsigned int verndor_id;
86         unsigned int device_id;
87         char driver_name[64];
88     } devices[] = {
89         { 0x8086, 0x4100, "pvr" },
90     };
91
92     if (driver_name)
93         *driver_name = NULL;
94
95     *driver_name = strdup(devices[0].driver_name);
96     
97     return VA_STATUS_SUCCESS;
98 }
99
100
101 VADisplay vaGetDisplay (
102     Display *native_dpy /* implementation specific */
103 )
104 {
105   VADisplay dpy = NULL;
106   VADisplayContextP pDisplayContext = pDisplayContexts;
107
108   if (!native_dpy)
109       return NULL;
110
111   while (pDisplayContext)
112   {
113       if (pDisplayContext->pDriverContext &&
114           pDisplayContext->pDriverContext->native_dpy == (void *)native_dpy)
115       {
116           dpy = (VADisplay)pDisplayContext;
117           break;
118       }
119       pDisplayContext = pDisplayContext->pNext;
120   }
121
122   if (!dpy)
123   {
124       /* create new entry */
125       VADriverContextP pDriverContext;
126       pDisplayContext = (VADisplayContextP)calloc(1, sizeof(*pDisplayContext));
127       pDriverContext  = (VADriverContextP)calloc(1, sizeof(*pDriverContext));
128       if (pDisplayContext && pDriverContext)
129       {
130           pDisplayContext->vadpy_magic = VA_DISPLAY_MAGIC;          
131
132           pDriverContext->native_dpy       = (void *)native_dpy;
133           pDisplayContext->pNext           = pDisplayContexts;
134           pDisplayContext->pDriverContext  = pDriverContext;
135           pDisplayContext->vaIsValid       = va_DisplayContextIsValid;
136           pDisplayContext->vaDestroy       = va_DisplayContextDestroy;
137           pDisplayContext->vaGetDriverName = va_DisplayContextGetDriverName;
138           pDisplayContexts                 = pDisplayContext;
139           dpy                              = (VADisplay)pDisplayContext;
140       }
141       else
142       {
143           if (pDisplayContext)
144               free(pDisplayContext);
145           if (pDriverContext)
146               free(pDriverContext);
147       }
148   }
149   
150   return dpy;
151 }
152
153
154 #define CTX(dpy) (((VADisplayContextP)dpy)->pDriverContext)
155 #define CHECK_DISPLAY(dpy) if( !vaDisplayIsValid(dpy) ) { return VA_STATUS_ERROR_INVALID_DISPLAY; }
156
157 static int vaDisplayIsValid(VADisplay dpy)
158 {
159     VADisplayContextP pDisplayContext = (VADisplayContextP)dpy;
160     return pDisplayContext && (pDisplayContext->vadpy_magic == VA_DISPLAY_MAGIC) && pDisplayContext->vaIsValid(pDisplayContext);
161 }
162
163 VAStatus vaPutSurface (
164     VADisplay dpy,
165     VASurfaceID surface,
166     Surface *draw, /* Android Surface/Window */
167     short srcx,
168     short srcy,
169     unsigned short srcw,
170     unsigned short srch,
171     short destx,
172     short desty,
173     unsigned short destw,
174     unsigned short desth,
175     VARectangle *cliprects, /* client supplied clip list */
176     unsigned int number_cliprects, /* number of clip rects in the clip list */
177     unsigned int flags /* de-interlacing flags */
178 )
179 {
180   VADriverContextP ctx;
181
182   CHECK_DISPLAY(dpy);
183   ctx = CTX(dpy);
184
185   return ctx->vtable.vaPutSurface( ctx, surface, draw, srcx, srcy, srcw, srch, 
186                                    destx, desty, destw, desth,
187                                    cliprects, number_cliprects, flags );
188 }