xref: /aosp_15_r20/external/libva/va/android/va_android.cpp (revision 54e60f844a168e9a219354de272cd517ee8cd4b7)
1 /*
2  * Copyright (c) 2007 Intel Corporation. All Rights Reserved.
3  * Copyright (c) 2023 Emil Velikov
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sub license, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the
14  * next paragraph) shall be included in all copies or substantial portions
15  * of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
20  * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
21  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25 
26 #define _GNU_SOURCE 1
27 #include "sysdeps.h"
28 #include "va.h"
29 #include "va_backend.h"
30 #include "va_internal.h"
31 #include "va_trace.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 
va_DisplayContextDestroy(VADisplayContextP pDisplayContext)47 static void va_DisplayContextDestroy(
48     VADisplayContextP pDisplayContext
49 )
50 {
51     struct drm_state *drm_state;
52 
53     if (pDisplayContext == NULL)
54         return;
55 
56     /* close the open-ed DRM fd */
57     drm_state = (struct drm_state *)pDisplayContext->pDriverContext->drm_state;
58     close(drm_state->fd);
59 
60     free(pDisplayContext->pDriverContext->drm_state);
61     free(pDisplayContext->pDriverContext);
62     free(pDisplayContext);
63 }
64 
va_DisplayContextConnect(VADisplayContextP pDisplayContext)65 static VAStatus va_DisplayContextConnect(
66     VADisplayContextP pDisplayContext
67 )
68 {
69     VADriverContextP const ctx = pDisplayContext->pDriverContext;
70     struct drm_state * const drm_state = (struct drm_state *)ctx->drm_state;
71 
72     drm_state->fd = open(DEVICE_NAME, O_RDWR | O_CLOEXEC);
73     if (drm_state->fd < 0) {
74         fprintf(stderr, "Cannot open DRM device '%s': %d, %s\n",
75                 DEVICE_NAME, errno, strerror(errno));
76         return VA_STATUS_ERROR_UNKNOWN;
77     }
78     drm_state->auth_type = VA_DRM_AUTH_CUSTOM;
79     return VA_STATUS_SUCCESS;
80 }
81 
82 static VAStatus
va_DisplayContextGetDriverNames(VADisplayContextP pDisplayContext,char ** drivers,unsigned * num_drivers)83 va_DisplayContextGetDriverNames(
84     VADisplayContextP pDisplayContext,
85     char            **drivers,
86     unsigned         *num_drivers
87 )
88 {
89     VADriverContextP const ctx = pDisplayContext->pDriverContext;
90     VAStatus status = va_DisplayContextConnect(pDisplayContext);
91     if (status != VA_STATUS_SUCCESS)
92         return status;
93 
94     return VA_DRM_GetDriverNames(ctx, drivers, num_drivers);
95 }
96 
vaGetDisplay(void * native_dpy)97 VADisplay vaGetDisplay(
98     void *native_dpy /* implementation specific */
99 )
100 {
101     VADisplayContextP pDisplayContext;
102     VADriverContextP  pDriverContext;
103     struct drm_state *drm_state;
104 
105     if (!native_dpy)
106         return NULL;
107 
108     pDisplayContext = va_newDisplayContext();
109     if (!pDisplayContext)
110         return NULL;
111 
112     pDisplayContext->vaDestroy       = va_DisplayContextDestroy;
113     pDisplayContext->vaGetDriverNames = va_DisplayContextGetDriverNames;
114 
115     pDriverContext = va_newDriverContext(pDisplayContext);
116     if (!pDriverContext) {
117         free(pDisplayContext);
118         return NULL;
119     }
120 
121     pDriverContext->native_dpy   = (void *)native_dpy;
122     pDriverContext->display_type = VA_DISPLAY_ANDROID;
123 
124     drm_state = (struct drm_state*)calloc(1, sizeof(*drm_state));
125     if (!drm_state) {
126         free(pDisplayContext);
127         free(pDriverContext);
128         return NULL;
129     }
130 
131     pDriverContext->drm_state = drm_state;
132 
133     return (VADisplay)pDisplayContext;
134 }
135