OSDN Git Service

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