st/va: add headless support, i.e. VA_DISPLAY_DRM
[mesa.git] / src / gallium / state_trackers / va / context.c
1 /**************************************************************************
2 *
3 * Copyright 2010 Thomas Balling Sørensen & Orasanu Lucian.
4 * Copyright 2014 Advanced Micro Devices, Inc.
5 * All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 **************************************************************************/
28
29 #include "pipe/p_screen.h"
30 #include "pipe/p_video_codec.h"
31 #include "pipe-loader/pipe_loader.h"
32 #include "state_tracker/drm_driver.h"
33 #include "util/u_memory.h"
34 #include "util/u_handle_table.h"
35 #include "util/u_video.h"
36 #include "vl/vl_winsys.h"
37
38 #include "va_private.h"
39
40 #include <va/va_drmcommon.h>
41
42 static struct VADriverVTable vtable =
43 {
44 &vlVaTerminate,
45 &vlVaQueryConfigProfiles,
46 &vlVaQueryConfigEntrypoints,
47 &vlVaGetConfigAttributes,
48 &vlVaCreateConfig,
49 &vlVaDestroyConfig,
50 &vlVaQueryConfigAttributes,
51 &vlVaCreateSurfaces,
52 &vlVaDestroySurfaces,
53 &vlVaCreateContext,
54 &vlVaDestroyContext,
55 &vlVaCreateBuffer,
56 &vlVaBufferSetNumElements,
57 &vlVaMapBuffer,
58 &vlVaUnmapBuffer,
59 &vlVaDestroyBuffer,
60 &vlVaBeginPicture,
61 &vlVaRenderPicture,
62 &vlVaEndPicture,
63 &vlVaSyncSurface,
64 &vlVaQuerySurfaceStatus,
65 &vlVaQuerySurfaceError,
66 &vlVaPutSurface,
67 &vlVaQueryImageFormats,
68 &vlVaCreateImage,
69 &vlVaDeriveImage,
70 &vlVaDestroyImage,
71 &vlVaSetImagePalette,
72 &vlVaGetImage,
73 &vlVaPutImage,
74 &vlVaQuerySubpictureFormats,
75 &vlVaCreateSubpicture,
76 &vlVaDestroySubpicture,
77 &vlVaSubpictureImage,
78 &vlVaSetSubpictureChromakey,
79 &vlVaSetSubpictureGlobalAlpha,
80 &vlVaAssociateSubpicture,
81 &vlVaDeassociateSubpicture,
82 &vlVaQueryDisplayAttributes,
83 &vlVaGetDisplayAttributes,
84 &vlVaSetDisplayAttributes,
85 &vlVaBufferInfo,
86 &vlVaLockSurface,
87 &vlVaUnlockSurface,
88 NULL, /* DEPRECATED VaGetSurfaceAttributes */
89 &vlVaCreateSurfaces2,
90 &vlVaQuerySurfaceAttributes
91 };
92
93 static struct VADriverVTableVPP vtable_vpp =
94 {
95 1,
96 &vlVaQueryVideoProcFilters,
97 &vlVaQueryVideoProcFilterCaps,
98 &vlVaQueryVideoProcPipelineCaps
99 };
100
101 PUBLIC VAStatus
102 VA_DRIVER_INIT_FUNC(VADriverContextP ctx)
103 {
104 vlVaDriver *drv;
105 int drm_fd;
106 struct drm_state *drm_info;
107
108 if (!ctx)
109 return VA_STATUS_ERROR_INVALID_CONTEXT;
110
111 drv = CALLOC(1, sizeof(vlVaDriver));
112 if (!drv)
113 return VA_STATUS_ERROR_ALLOCATION_FAILED;
114
115 switch (ctx->display_type) {
116 case VA_DISPLAY_ANDROID:
117 case VA_DISPLAY_WAYLAND:
118 FREE(drv);
119 return VA_STATUS_ERROR_UNIMPLEMENTED;
120 case VA_DISPLAY_GLX:
121 case VA_DISPLAY_X11:
122 drv->vscreen = vl_screen_create(ctx->native_dpy, ctx->x11_screen);
123 if (!drv->vscreen)
124 goto error_screen;
125 break;
126 case VA_DISPLAY_DRM:
127 case VA_DISPLAY_DRM_RENDERNODES: {
128 drm_info = (struct drm_state *) ctx->drm_state;
129 if (!drm_info) {
130 FREE(drv);
131 return VA_STATUS_ERROR_INVALID_PARAMETER;
132 }
133
134 #if GALLIUM_STATIC_TARGETS
135 drm_fd = drm_info->fd;
136 #else
137 drm_fd = dup(drm_info->fd);
138 #endif
139
140 if (drm_fd < 0) {
141 FREE(drv);
142 return VA_STATUS_ERROR_INVALID_PARAMETER;
143 }
144
145 drv->vscreen = CALLOC_STRUCT(vl_screen);
146 if (!drv->vscreen)
147 goto error_screen;
148
149 #if GALLIUM_STATIC_TARGETS
150 drv->vscreen->pscreen = dd_create_screen(drm_fd);
151 #else
152 if (pipe_loader_drm_probe_fd(&drv->dev, drm_fd))
153 drv->vscreen->pscreen = pipe_loader_create_screen(drv->dev, PIPE_SEARCH_DIR);
154 #endif
155
156 if (!drv->vscreen->pscreen)
157 goto error_pipe;
158
159 }
160 break;
161 default:
162 FREE(drv);
163 return VA_STATUS_ERROR_INVALID_DISPLAY;
164 }
165
166 drv->pipe = drv->vscreen->pscreen->context_create(drv->vscreen->pscreen,
167 drv->vscreen, 0);
168 if (!drv->pipe)
169 goto error_pipe;
170
171 drv->htab = handle_table_create();
172 if (!drv->htab)
173 goto error_htab;
174
175 vl_compositor_init(&drv->compositor, drv->pipe);
176 vl_compositor_init_state(&drv->cstate, drv->pipe);
177
178 vl_csc_get_matrix(VL_CSC_COLOR_STANDARD_BT_601, NULL, true, &drv->csc);
179 vl_compositor_set_csc_matrix(&drv->cstate, (const vl_csc_matrix *)&drv->csc);
180
181 ctx->pDriverData = (void *)drv;
182 ctx->version_major = 0;
183 ctx->version_minor = 1;
184 *ctx->vtable = vtable;
185 *ctx->vtable_vpp = vtable_vpp;
186 ctx->max_profiles = PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH - PIPE_VIDEO_PROFILE_UNKNOWN;
187 ctx->max_entrypoints = 1;
188 ctx->max_attributes = 1;
189 ctx->max_image_formats = VL_VA_MAX_IMAGE_FORMATS;
190 ctx->max_subpic_formats = 1;
191 ctx->max_display_attributes = 1;
192 ctx->str_vendor = "mesa gallium vaapi";
193
194 return VA_STATUS_SUCCESS;
195
196 error_htab:
197 drv->pipe->destroy(drv->pipe);
198
199 error_pipe:
200 if (ctx->display_type == VA_DISPLAY_GLX || ctx->display_type == VA_DISPLAY_X11)
201 vl_screen_destroy(drv->vscreen);
202 else
203 FREE(drv->vscreen);
204
205 error_screen:
206 FREE(drv);
207 return VA_STATUS_ERROR_ALLOCATION_FAILED;
208 }
209
210 VAStatus
211 vlVaCreateContext(VADriverContextP ctx, VAConfigID config_id, int picture_width,
212 int picture_height, int flag, VASurfaceID *render_targets,
213 int num_render_targets, VAContextID *context_id)
214 {
215 struct pipe_video_codec templat = {};
216 vlVaDriver *drv;
217 vlVaContext *context;
218 int is_vpp;
219
220 if (!ctx)
221 return VA_STATUS_ERROR_INVALID_CONTEXT;
222
223 is_vpp = config_id == PIPE_VIDEO_PROFILE_UNKNOWN && !picture_width &&
224 !picture_height && !flag && !render_targets && !num_render_targets;
225
226 if (!(picture_width && picture_height) && !is_vpp)
227 return VA_STATUS_ERROR_INVALID_IMAGE_FORMAT;
228
229 drv = VL_VA_DRIVER(ctx);
230 context = CALLOC(1, sizeof(vlVaContext));
231 if (!context)
232 return VA_STATUS_ERROR_ALLOCATION_FAILED;
233
234 if (is_vpp) {
235 context->decoder = NULL;
236 if (!drv->compositor.upload) {
237 FREE(context);
238 return VA_STATUS_ERROR_INVALID_CONTEXT;
239 }
240 } else {
241 templat.profile = config_id;
242 templat.entrypoint = PIPE_VIDEO_ENTRYPOINT_BITSTREAM;
243 templat.chroma_format = PIPE_VIDEO_CHROMA_FORMAT_420;
244 templat.width = picture_width;
245 templat.height = picture_height;
246 templat.max_references = num_render_targets;
247 templat.expect_chunked_decode = true;
248
249 if (u_reduce_video_profile(templat.profile) ==
250 PIPE_VIDEO_FORMAT_MPEG4_AVC)
251 templat.level = u_get_h264_level(templat.width, templat.height,
252 &templat.max_references);
253
254 context->decoder = drv->pipe->create_video_codec(drv->pipe, &templat);
255 if (!context->decoder) {
256 FREE(context);
257 return VA_STATUS_ERROR_ALLOCATION_FAILED;
258 }
259
260 if (u_reduce_video_profile(context->decoder->profile) ==
261 PIPE_VIDEO_FORMAT_MPEG4_AVC) {
262 context->desc.h264.pps = CALLOC_STRUCT(pipe_h264_pps);
263 if (!context->desc.h264.pps) {
264 FREE(context);
265 return VA_STATUS_ERROR_ALLOCATION_FAILED;
266 }
267 context->desc.h264.pps->sps = CALLOC_STRUCT(pipe_h264_sps);
268 if (!context->desc.h264.pps->sps) {
269 FREE(context->desc.h264.pps);
270 FREE(context);
271 return VA_STATUS_ERROR_ALLOCATION_FAILED;
272 }
273 }
274
275 if (u_reduce_video_profile(context->decoder->profile) ==
276 PIPE_VIDEO_FORMAT_HEVC) {
277 context->desc.h265.pps = CALLOC_STRUCT(pipe_h265_pps);
278 if (!context->desc.h265.pps) {
279 FREE(context);
280 return VA_STATUS_ERROR_ALLOCATION_FAILED;
281 }
282 context->desc.h265.pps->sps = CALLOC_STRUCT(pipe_h265_sps);
283 if (!context->desc.h265.pps->sps) {
284 FREE(context->desc.h265.pps);
285 FREE(context);
286 return VA_STATUS_ERROR_ALLOCATION_FAILED;
287 }
288 }
289 }
290
291 context->desc.base.profile = config_id;
292 *context_id = handle_table_add(drv->htab, context);
293
294 return VA_STATUS_SUCCESS;
295 }
296
297 VAStatus
298 vlVaDestroyContext(VADriverContextP ctx, VAContextID context_id)
299 {
300 vlVaDriver *drv;
301 vlVaContext *context;
302
303 if (!ctx)
304 return VA_STATUS_ERROR_INVALID_CONTEXT;
305
306 drv = VL_VA_DRIVER(ctx);
307 context = handle_table_get(drv->htab, context_id);
308
309 if (context->decoder) {
310 if (u_reduce_video_profile(context->decoder->profile) ==
311 PIPE_VIDEO_FORMAT_MPEG4_AVC) {
312 FREE(context->desc.h264.pps->sps);
313 FREE(context->desc.h264.pps);
314 }
315 if (u_reduce_video_profile(context->decoder->profile) ==
316 PIPE_VIDEO_FORMAT_HEVC) {
317 FREE(context->desc.h265.pps->sps);
318 FREE(context->desc.h265.pps);
319 }
320 context->decoder->destroy(context->decoder);
321 }
322 FREE(context);
323 handle_table_remove(drv->htab, context_id);
324
325 return VA_STATUS_SUCCESS;
326 }
327
328 VAStatus
329 vlVaTerminate(VADriverContextP ctx)
330 {
331 vlVaDriver *drv;
332
333 if (!ctx)
334 return VA_STATUS_ERROR_INVALID_CONTEXT;
335
336 drv = ctx->pDriverData;
337 vl_compositor_cleanup_state(&drv->cstate);
338 vl_compositor_cleanup(&drv->compositor);
339 drv->pipe->destroy(drv->pipe);
340 if (ctx->display_type == VA_DISPLAY_GLX || ctx->display_type == VA_DISPLAY_X11)
341 vl_screen_destroy(drv->vscreen);
342 else
343 FREE(drv->vscreen);
344 handle_table_destroy(drv->htab);
345 FREE(drv);
346
347 return VA_STATUS_SUCCESS;
348 }