OSDN Git Service

Bump wayland-client dependency to 1.11.0
[android-x86/hardware-intel-common-libva.git] / va / drm / va_drm.c
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_drmcommon.h"
30 #include "va_drm_auth.h"
31 #include "va_drm_utils.h"
32
33 static int
34 va_DisplayContextIsValid(VADisplayContextP pDisplayContext)
35 {
36     VADriverContextP const pDriverContext = pDisplayContext->pDriverContext;
37
38     return (pDriverContext &&
39             ((pDriverContext->display_type & VA_DISPLAY_MAJOR_MASK) ==
40              VA_DISPLAY_DRM));
41 }
42
43 static void
44 va_DisplayContextDestroy(VADisplayContextP pDisplayContext)
45 {
46     if (!pDisplayContext)
47         return;
48
49     free(pDisplayContext->pDriverContext->drm_state);
50     free(pDisplayContext->pDriverContext);
51     free(pDisplayContext);
52 }
53
54 static VAStatus
55 va_DisplayContextGetDriverName(
56     VADisplayContextP pDisplayContext,
57     char            **driver_name_ptr
58 )
59 {
60
61     VADriverContextP const ctx = pDisplayContext->pDriverContext;
62     struct drm_state * const drm_state = ctx->drm_state;
63     drm_magic_t magic;
64     VAStatus status;
65     int ret;
66
67     status = VA_DRM_GetDriverName(ctx, driver_name_ptr);
68     if (status != VA_STATUS_SUCCESS)
69         return status;
70
71     /* Authentication is only needed for a legacy DRM device */
72     if (ctx->display_type != VA_DISPLAY_DRM_RENDERNODES) {
73         ret = drmGetMagic(drm_state->fd, &magic);
74         if (ret < 0)
75             return VA_STATUS_ERROR_OPERATION_FAILED;
76
77         if (!va_drm_authenticate(drm_state->fd, magic))
78             return VA_STATUS_ERROR_OPERATION_FAILED;
79     }
80
81     drm_state->auth_type = VA_DRM_AUTH_CUSTOM;
82
83     return VA_STATUS_SUCCESS;
84 }
85
86 VADisplay
87 vaGetDisplayDRM(int fd)
88 {
89     VADisplayContextP pDisplayContext = NULL;
90     VADriverContextP  pDriverContext  = NULL;
91     struct drm_state *drm_state       = NULL;
92     int is_render_nodes;
93
94     if (fd < 0 || (is_render_nodes = VA_DRM_IsRenderNodeFd(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     pDriverContext = calloc(1, sizeof(*pDriverContext));
105     if (!pDriverContext)
106         goto error;
107     pDriverContext->native_dpy   = NULL;
108     pDriverContext->display_type = is_render_nodes ?
109         VA_DISPLAY_DRM_RENDERNODES : VA_DISPLAY_DRM;
110     pDriverContext->drm_state    = drm_state;
111
112     pDisplayContext = calloc(1, sizeof(*pDisplayContext));
113     if (!pDisplayContext)
114         goto error;
115
116     pDisplayContext->vadpy_magic     = VA_DISPLAY_MAGIC;
117     pDisplayContext->pDriverContext  = pDriverContext;
118     pDisplayContext->vaIsValid       = va_DisplayContextIsValid;
119     pDisplayContext->vaDestroy       = va_DisplayContextDestroy;
120     pDisplayContext->vaGetDriverName = va_DisplayContextGetDriverName;
121     return pDisplayContext;
122
123 error:
124     free(pDisplayContext);
125     free(pDriverContext);
126     free(drm_state);
127     return NULL;
128 }