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