blob: efbc35aa262f1dc731612a850a14791d7ffed8fd [file] [log] [blame]
Fei Jiangb0fac492010-06-11 10:38:21 +08001/*
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
Austin Yuan409de6a2013-03-05 14:54:42 +080026#include "sysdeps.h"
Fei Jiangb0fac492010-06-11 10:38:21 +080027#include "va.h"
28#include "va_backend.h"
Shuduo Sang5b3d55a2011-10-08 14:26:26 +080029#include "va_trace.h"
30#include "va_fool.h"
Fei Jiangb0fac492010-06-11 10:38:21 +080031#include "va_android.h"
Austin Yuan409de6a2013-03-05 14:54:42 +080032#include "va_drmcommon.h"
33#include "va_drm_utils.h"
Fei Jiangb0fac492010-06-11 10:38:21 +080034#include <stdarg.h>
Fei Jiangb0fac492010-06-11 10:38:21 +080035#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>
Austin Yuan409de6a2013-03-05 14:54:42 +080041
Mathias Agopiana49a9542017-05-09 23:00:59 -070042#include <system/window.h>
Fei Jiangb0fac492010-06-11 10:38:21 +080043
44#define CHECK_SYMBOL(func) { if (!func) printf("func %s not found\n", #func); return VA_STATUS_ERROR_UNKNOWN; }
Austin Yuan409de6a2013-03-05 14:54:42 +080045#define DEVICE_NAME "/dev/dri/card0"
Fei Jiangb0fac492010-06-11 10:38:21 +080046
Fei Jiangb0fac492010-06-11 10:38:21 +080047static 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
77static int va_DisplayContextIsValid (
78 VADisplayContextP pDisplayContext
79 )
80{
Austin Yuana00f54a2011-06-07 15:48:11 +080081 return (pDisplayContext != NULL &&
82 pDisplayContext->pDriverContext != NULL);
Fei Jiangb0fac492010-06-11 10:38:21 +080083}
84
85static void va_DisplayContextDestroy (
86 VADisplayContextP pDisplayContext
87)
88{
Austin Yuan409de6a2013-03-05 14:54:42 +080089 struct drm_state *drm_state;
Fei Jiangb0fac492010-06-11 10:38:21 +080090
Austin Yuana00f54a2011-06-07 15:48:11 +080091 if (pDisplayContext == NULL)
92 return;
93
94 /* close the open-ed DRM fd */
Austin Yuan409de6a2013-03-05 14:54:42 +080095 drm_state = (struct drm_state *)pDisplayContext->pDriverContext->drm_state;
96 close(drm_state->fd);
Austin Yuana00f54a2011-06-07 15:48:11 +080097
Austin Yuan409de6a2013-03-05 14:54:42 +080098 free(pDisplayContext->pDriverContext->drm_state);
Fei Jiangb0fac492010-06-11 10:38:21 +080099 free(pDisplayContext->pDriverContext);
100 free(pDisplayContext);
101}
102
Fei Jiangb0fac492010-06-11 10:38:21 +0800103static VAStatus va_DisplayContextGetDriverName (
104 VADisplayContextP pDisplayContext,
105 char **driver_name
106)
107{
Austin Yuan409de6a2013-03-05 14:54:42 +0800108 VADriverContextP const ctx = pDisplayContext->pDriverContext;
109 struct drm_state * drm_state = (struct drm_state *)ctx->drm_state;
Fei Jiangb0fac492010-06-11 10:38:21 +0800110
Austin Yuan409de6a2013-03-05 14:54:42 +0800111 memset(drm_state, 0, sizeof(*drm_state));
112 drm_state->fd = open_device((char *)DEVICE_NAME);
113
114 if (drm_state->fd < 0) {
Fei Jiangb0fac492010-06-11 10:38:21 +0800115 fprintf(stderr,"can't open DRM devices\n");
116 return VA_STATUS_ERROR_UNKNOWN;
117 }
Austin Yuan409de6a2013-03-05 14:54:42 +0800118 drm_state->auth_type = VA_DRM_AUTH_CUSTOM;
hding3c31b44d2013-04-23 18:11:03 +0800119
120 if (driver_name == NULL)
121 return VA_STATUS_ERROR_UNKNOWN;
Fei Jiangb0fac492010-06-11 10:38:21 +0800122
Fei Jiang77690022013-04-11 22:43:20 +0800123 if (strncmp((char *)ctx->native_dpy, "libva_driver_name=", 18) == 0) {
hding3c31b44d2013-04-23 18:11:03 +0800124 *driver_name = strdup((char *)ctx->native_dpy + 18);
125 if (*driver_name == NULL)
Fei Jiang77690022013-04-11 22:43:20 +0800126 return VA_STATUS_ERROR_ALLOCATION_FAILED;
127 else
128 return VA_STATUS_SUCCESS;
129 } else {
130 return VA_DRM_GetDriverName(ctx, driver_name);
131 }
Fei Jiangb0fac492010-06-11 10:38:21 +0800132}
Fei Jiangb0fac492010-06-11 10:38:21 +0800133
Fei Jiangb0fac492010-06-11 10:38:21 +0800134
135VADisplay vaGetDisplay (
136 void *native_dpy /* implementation specific */
137)
138{
139 VADisplay dpy = NULL;
Austin Yuana00f54a2011-06-07 15:48:11 +0800140 VADisplayContextP pDisplayContext;
Fei Jiangb0fac492010-06-11 10:38:21 +0800141
142 if (!native_dpy)
143 return NULL;
144
Fei Jiangb0fac492010-06-11 10:38:21 +0800145 if (!dpy)
146 {
147 /* create new entry */
Austin Yuan409de6a2013-03-05 14:54:42 +0800148 VADriverContextP pDriverContext = 0;
149 struct drm_state *drm_state = 0;
Fei Jiangb0fac492010-06-11 10:38:21 +0800150 pDisplayContext = (VADisplayContextP)calloc(1, sizeof(*pDisplayContext));
151 pDriverContext = (VADriverContextP)calloc(1, sizeof(*pDriverContext));
Austin Yuan409de6a2013-03-05 14:54:42 +0800152 drm_state = (struct drm_state*)calloc(1, sizeof(*drm_state));
153 if (pDisplayContext && pDriverContext && drm_state)
Fei Jiangb0fac492010-06-11 10:38:21 +0800154 {
155 pDisplayContext->vadpy_magic = VA_DISPLAY_MAGIC;
156
157 pDriverContext->native_dpy = (void *)native_dpy;
Austin Yuan409de6a2013-03-05 14:54:42 +0800158 pDriverContext->display_type = VA_DISPLAY_ANDROID;
Fei Jiangb0fac492010-06-11 10:38:21 +0800159 pDisplayContext->pDriverContext = pDriverContext;
160 pDisplayContext->vaIsValid = va_DisplayContextIsValid;
161 pDisplayContext->vaDestroy = va_DisplayContextDestroy;
162 pDisplayContext->vaGetDriverName = va_DisplayContextGetDriverName;
Austin Yuan409de6a2013-03-05 14:54:42 +0800163 pDriverContext->drm_state = drm_state;
Fei Jiangb0fac492010-06-11 10:38:21 +0800164 dpy = (VADisplay)pDisplayContext;
165 }
166 else
167 {
168 if (pDisplayContext)
169 free(pDisplayContext);
170 if (pDriverContext)
171 free(pDriverContext);
Austin Yuan409de6a2013-03-05 14:54:42 +0800172 if (drm_state)
173 free(drm_state);
Fei Jiangb0fac492010-06-11 10:38:21 +0800174 }
175 }
176
177 return dpy;
178}
179
180#define CTX(dpy) (((VADisplayContextP)dpy)->pDriverContext)
181#define CHECK_DISPLAY(dpy) if( !vaDisplayIsValid(dpy) ) { return VA_STATUS_ERROR_INVALID_DISPLAY; }
182
183
Austin Yuan21e26472010-10-27 11:48:36 +0800184extern "C" {
185 extern int fool_postp; /* do nothing for vaPutSurface if set */
186 extern int trace_flag; /* trace vaPutSurface parameters */
187
188 void va_TracePutSurface (
189 VADisplay dpy,
190 VASurfaceID surface,
191 void *draw, /* the target Drawable */
192 short srcx,
193 short srcy,
194 unsigned short srcw,
195 unsigned short srch,
196 short destx,
197 short desty,
198 unsigned short destw,
199 unsigned short desth,
200 VARectangle *cliprects, /* client supplied clip list */
201 unsigned int number_cliprects, /* number of clip rects in the clip list */
202 unsigned int flags /* de-interlacing flags */
203 );
204}
205
Fei Jiangb0fac492010-06-11 10:38:21 +0800206VAStatus vaPutSurface (
207 VADisplay dpy,
208 VASurfaceID surface,
Austin Yuan409de6a2013-03-05 14:54:42 +0800209 sp<ANativeWindow> draw, /* Android Native Window */
Fei Jiangb0fac492010-06-11 10:38:21 +0800210 short srcx,
211 short srcy,
212 unsigned short srcw,
213 unsigned short srch,
214 short destx,
215 short desty,
216 unsigned short destw,
217 unsigned short desth,
218 VARectangle *cliprects, /* client supplied clip list */
219 unsigned int number_cliprects, /* number of clip rects in the clip list */
220 unsigned int flags /* de-interlacing flags */
221)
222{
223 VADriverContextP ctx;
224
Austin Yuan21e26472010-10-27 11:48:36 +0800225 if (fool_postp)
226 return VA_STATUS_SUCCESS;
Austin Yuan409de6a2013-03-05 14:54:42 +0800227
hding34b672db2012-06-11 21:10:54 +0800228 if (draw == NULL)
229 return VA_STATUS_ERROR_UNKNOWN;
Austin Yuan409de6a2013-03-05 14:54:42 +0800230
Fei Jiangb0fac492010-06-11 10:38:21 +0800231 CHECK_DISPLAY(dpy);
232 ctx = CTX(dpy);
Austin Yuan21e26472010-10-27 11:48:36 +0800233
Shuduo Sang5b3d55a2011-10-08 14:26:26 +0800234 VA_TRACE_LOG(va_TracePutSurface, dpy, surface, static_cast<void*>(&draw), srcx, srcy, srcw, srch,
235 destx, desty, destw, desth,
236 cliprects, number_cliprects, flags );
Austin Yuan21e26472010-10-27 11:48:36 +0800237
wangkuncceaa052011-03-09 13:39:43 +0800238 return ctx->vtable->vaPutSurface( ctx, surface, static_cast<void*>(&draw), srcx, srcy, srcw, srch,
Fei Jiangb0fac492010-06-11 10:38:21 +0800239 destx, desty, destw, desth,
240 cliprects, number_cliprects, flags );
241}
Austin Yuan409de6a2013-03-05 14:54:42 +0800242