OSDN Git Service

4a8fe9f66c00bd8fed60c20175cd0b78e5d6179c
[android-x86/hardware-intel-common-libva.git] / va / android / va_android.cpp
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 "sysdeps.h"
27 #include "va.h"
28 #include "va_backend.h"
29 #include "va_internal.h"
30 #include "va_trace.h"
31 #include "va_fool.h"
32 #include "va_android.h"
33 #include "va_drmcommon.h"
34 #include "va_drm_utils.h"
35 #include <stdarg.h>
36 #include <unistd.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <fcntl.h>
40 #include <dlfcn.h>
41 #include <errno.h>
42
43
44 #define CHECK_SYMBOL(func) { if (!func) printf("func %s not found\n", #func); return VA_STATUS_ERROR_UNKNOWN; }
45 #define DEVICE_NAME "/dev/dri/renderD128"
46
47 static int open_device (char *dev_name)
48 {
49     struct stat st;
50     int fd;
51
52     if (-1 == stat (dev_name, &st))
53     {
54         printf ("Cannot identify '%s': %d, %s\n",
55                 dev_name, errno, strerror (errno));
56         return -1;
57     }
58
59     if (!S_ISCHR (st.st_mode))
60     {
61         printf ("%s is no device\n", dev_name);
62         return -1;
63     }
64
65     fd = open (dev_name, O_RDWR);
66
67     if (-1 == fd)
68     {
69         fprintf (stderr, "Cannot open '%s': %d, %s\n",
70                  dev_name, errno, strerror (errno));
71         return -1;
72     }
73
74     return fd;
75 }
76
77 static int va_DisplayContextIsValid (
78     VADisplayContextP pDisplayContext
79                                   )
80 {
81     return (pDisplayContext != NULL &&
82             pDisplayContext->pDriverContext != NULL);
83 }
84
85 static void va_DisplayContextDestroy (
86     VADisplayContextP pDisplayContext
87 )
88 {
89     struct drm_state *drm_state;
90
91     if (pDisplayContext == NULL)
92         return;
93
94     /* close the open-ed DRM fd */
95     drm_state = (struct drm_state *)pDisplayContext->pDriverContext->drm_state;
96     close(drm_state->fd);
97
98     free(pDisplayContext->pDriverContext->drm_state);
99     free(pDisplayContext->pDriverContext);
100     free(pDisplayContext);
101 }
102
103 static VAStatus va_DisplayContextGetDriverName (
104     VADisplayContextP pDisplayContext,
105     char **driver_name
106 )
107 {
108     VADriverContextP const ctx = pDisplayContext->pDriverContext;
109     struct drm_state * drm_state = (struct drm_state *)ctx->drm_state;
110
111     memset(drm_state, 0, sizeof(*drm_state));
112     drm_state->fd = open_device((char *)DEVICE_NAME);
113
114     if (drm_state->fd < 0) {
115         fprintf(stderr,"can't open DRM devices\n");
116         return VA_STATUS_ERROR_UNKNOWN;
117     }
118     drm_state->auth_type = VA_DRM_AUTH_CUSTOM;
119
120     return VA_DRM_GetDriverName(ctx, driver_name);
121 }
122
123
124 VADisplay vaGetDisplay (
125     void *native_dpy /* implementation specific */
126 )
127 {
128     VADisplayContextP pDisplayContext;
129     VADriverContextP  pDriverContext;
130     struct drm_state *drm_state;
131
132     if (!native_dpy)
133         return NULL;
134
135     pDisplayContext = va_newDisplayContext();
136     if (!pDisplayContext)
137         return NULL;
138
139     pDisplayContext->vaIsValid       = va_DisplayContextIsValid;
140     pDisplayContext->vaDestroy       = va_DisplayContextDestroy;
141     pDisplayContext->vaGetDriverName = va_DisplayContextGetDriverName;
142
143     pDriverContext = va_newDriverContext(pDisplayContext);
144     if (!pDriverContext) {
145         free(pDisplayContext);
146         return NULL;
147     }
148
149     pDriverContext->native_dpy   = (void *)native_dpy;
150     pDriverContext->display_type = VA_DISPLAY_ANDROID
151
152     drm_state = calloc(1, sizeof(*drm_state));
153     if (!drm_state) {
154         free(pDisplayContext);
155         free(pDriverContext);
156         return NULL;
157     }
158
159     pDriverContext->drm_state = drm_state;
160
161     return (VADisplay)pDisplayContext;
162 }
163
164
165 extern "C"  {
166     extern int va_fool_postp; /* do nothing for vaPutSurface if set */
167     extern int va_trace_flag; /* trace vaPutSurface parameters */
168
169     void va_TracePutSurface (
170         VADisplay dpy,
171         VASurfaceID surface,
172         void *draw, /* the target Drawable */
173         short srcx,
174         short srcy,
175         unsigned short srcw,
176         unsigned short srch,
177         short destx,
178         short desty,
179         unsigned short destw,
180         unsigned short desth,
181         VARectangle *cliprects, /* client supplied clip list */
182         unsigned int number_cliprects, /* number of clip rects in the clip list */
183         unsigned int flags /* de-interlacing flags */
184         );
185 }
186
187 VAStatus vaPutSurface (
188     VADisplay dpy,
189     VASurfaceID surface,
190     sp<ANativeWindow> draw, /* Android Native Window */
191     short srcx,
192     short srcy,
193     unsigned short srcw,
194     unsigned short srch,
195     short destx,
196     short desty,
197     unsigned short destw,
198     unsigned short desth,
199     VARectangle *cliprects, /* client supplied clip list */
200     unsigned int number_cliprects, /* number of clip rects in the clip list */
201     unsigned int flags /* de-interlacing flags */
202 )
203 {
204     VADriverContextP ctx;
205
206     if (va_fool_postp)
207         return VA_STATUS_SUCCESS;
208
209     if (draw == NULL)
210         return VA_STATUS_ERROR_UNKNOWN;
211
212     CHECK_DISPLAY(dpy);
213     ctx = CTX(dpy);
214
215     VA_TRACE_LOG(va_TracePutSurface, dpy, surface, static_cast<void*>(&draw), srcx, srcy, srcw, srch,
216                  destx, desty, destw, desth,
217                  cliprects, number_cliprects, flags );
218     
219     return ctx->vtable->vaPutSurface( ctx, surface, static_cast<void*>(&draw), srcx, srcy, srcw, srch, 
220                                      destx, desty, destw, desth,
221                                      cliprects, number_cliprects, flags );
222 }