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