xref: /aosp_15_r20/external/libva/va/drm/va_drm.c (revision 54e60f844a168e9a219354de272cd517ee8cd4b7)
1 /*
2  * Copyright (c) 2012 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 #include "sysdeps.h"
27 #include <xf86drm.h>
28 #include "va_drm.h"
29 #include "va_backend.h"
30 #include "va_internal.h"
31 #include "va_drmcommon.h"
32 #include "va_drm_auth.h"
33 #include "va_drm_utils.h"
34 
35 static void
va_DisplayContextDestroy(VADisplayContextP pDisplayContext)36 va_DisplayContextDestroy(VADisplayContextP pDisplayContext)
37 {
38     if (!pDisplayContext)
39         return;
40 
41     free(pDisplayContext->pDriverContext->drm_state);
42     free(pDisplayContext->pDriverContext);
43     free(pDisplayContext);
44 }
45 
46 
va_DisplayContextConnect(VADisplayContextP pDisplayContext)47 static VAStatus va_DisplayContextConnect(
48     VADisplayContextP pDisplayContext
49 )
50 {
51     VADriverContextP const ctx = pDisplayContext->pDriverContext;
52     struct drm_state * const drm_state = ctx->drm_state;
53     drm_magic_t magic;
54     int ret;
55 
56     /* Authentication is only needed for a legacy DRM device */
57     if (ctx->display_type != VA_DISPLAY_DRM_RENDERNODES) {
58         ret = drmGetMagic(drm_state->fd, &magic);
59         if (ret < 0)
60             return VA_STATUS_ERROR_OPERATION_FAILED;
61 
62         if (!va_drm_authenticate(drm_state->fd, magic))
63             return VA_STATUS_ERROR_OPERATION_FAILED;
64     }
65 
66     drm_state->auth_type = VA_DRM_AUTH_CUSTOM;
67     return VA_STATUS_SUCCESS;
68 }
69 
70 
71 static VAStatus
va_DisplayContextGetDriverNames(VADisplayContextP pDisplayContext,char ** drivers,unsigned * num_drivers)72 va_DisplayContextGetDriverNames(
73     VADisplayContextP pDisplayContext,
74     char            **drivers,
75     unsigned         *num_drivers
76 )
77 {
78     VADriverContextP const ctx = pDisplayContext->pDriverContext;
79     VAStatus status = va_DisplayContextConnect(pDisplayContext);
80     if (status != VA_STATUS_SUCCESS)
81         return status;
82 
83     return VA_DRM_GetDriverNames(ctx, drivers, num_drivers);
84 }
85 
86 VADisplay
vaGetDisplayDRM(int fd)87 vaGetDisplayDRM(int fd)
88 {
89     VADisplayContextP pDisplayContext = NULL;
90     VADriverContextP  pDriverContext  = NULL;
91     struct drm_state *drm_state       = NULL;
92     int node_type;
93 
94     if (fd < 0 || (node_type = drmGetNodeTypeFromFd(fd)) < 0)
95         return NULL;
96 
97     /* Create new entry */
98     /* XXX: handle cache? */
99     drm_state = calloc(1, sizeof(*drm_state));
100     if (!drm_state)
101         goto error;
102     drm_state->fd = fd;
103 
104     pDisplayContext = va_newDisplayContext();
105     if (!pDisplayContext)
106         goto error;
107 
108     pDisplayContext->vaDestroy       = va_DisplayContextDestroy;
109     pDisplayContext->vaGetDriverNames = va_DisplayContextGetDriverNames;
110 
111     pDriverContext = va_newDriverContext(pDisplayContext);
112     if (!pDriverContext)
113         goto error;
114 
115     pDriverContext->native_dpy   = NULL;
116     pDriverContext->display_type = node_type == DRM_NODE_RENDER ?
117                                    VA_DISPLAY_DRM_RENDERNODES : VA_DISPLAY_DRM;
118     pDriverContext->drm_state    = drm_state;
119 
120     return pDisplayContext;
121 
122 error:
123     free(pDisplayContext);
124     free(pDriverContext);
125     free(drm_state);
126     return NULL;
127 }
128