192794fefaa00c47c61dfa1e7a8830303b91259b
[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 "util/u_memory.h"
32 #include "util/u_handle_table.h"
33 #include "util/u_video.h"
34 #include "vl/vl_winsys.h"
35
36 #include "va_private.h"
37
38 #include <va/va_drmcommon.h>
39
40 static struct VADriverVTable vtable =
41 {
42 &vlVaTerminate,
43 &vlVaQueryConfigProfiles,
44 &vlVaQueryConfigEntrypoints,
45 &vlVaGetConfigAttributes,
46 &vlVaCreateConfig,
47 &vlVaDestroyConfig,
48 &vlVaQueryConfigAttributes,
49 &vlVaCreateSurfaces,
50 &vlVaDestroySurfaces,
51 &vlVaCreateContext,
52 &vlVaDestroyContext,
53 &vlVaCreateBuffer,
54 &vlVaBufferSetNumElements,
55 &vlVaMapBuffer,
56 &vlVaUnmapBuffer,
57 &vlVaDestroyBuffer,
58 &vlVaBeginPicture,
59 &vlVaRenderPicture,
60 &vlVaEndPicture,
61 &vlVaSyncSurface,
62 &vlVaQuerySurfaceStatus,
63 &vlVaQuerySurfaceError,
64 &vlVaPutSurface,
65 &vlVaQueryImageFormats,
66 &vlVaCreateImage,
67 &vlVaDeriveImage,
68 &vlVaDestroyImage,
69 &vlVaSetImagePalette,
70 &vlVaGetImage,
71 &vlVaPutImage,
72 &vlVaQuerySubpictureFormats,
73 &vlVaCreateSubpicture,
74 &vlVaDestroySubpicture,
75 &vlVaSubpictureImage,
76 &vlVaSetSubpictureChromakey,
77 &vlVaSetSubpictureGlobalAlpha,
78 &vlVaAssociateSubpicture,
79 &vlVaDeassociateSubpicture,
80 &vlVaQueryDisplayAttributes,
81 &vlVaGetDisplayAttributes,
82 &vlVaSetDisplayAttributes,
83 &vlVaBufferInfo,
84 &vlVaLockSurface,
85 &vlVaUnlockSurface,
86 NULL, /* DEPRECATED VaGetSurfaceAttributes */
87 &vlVaCreateSurfaces2,
88 &vlVaQuerySurfaceAttributes,
89 &vlVaAcquireBufferHandle,
90 &vlVaReleaseBufferHandle
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 struct drm_state *drm_info;
106
107 if (!ctx)
108 return VA_STATUS_ERROR_INVALID_CONTEXT;
109
110 drv = CALLOC(1, sizeof(vlVaDriver));
111 if (!drv)
112 return VA_STATUS_ERROR_ALLOCATION_FAILED;
113
114 switch (ctx->display_type) {
115 case VA_DISPLAY_ANDROID:
116 case VA_DISPLAY_WAYLAND:
117 FREE(drv);
118 return VA_STATUS_ERROR_UNIMPLEMENTED;
119 case VA_DISPLAY_GLX:
120 case VA_DISPLAY_X11:
121 drv->vscreen = vl_dri2_screen_create(ctx->native_dpy, ctx->x11_screen);
122 if (!drv->vscreen)
123 goto error_screen;
124 break;
125 case VA_DISPLAY_DRM:
126 case VA_DISPLAY_DRM_RENDERNODES: {
127 drm_info = (struct drm_state *) ctx->drm_state;
128
129 if (!drm_info || drm_info->fd < 0) {
130 FREE(drv);
131 return VA_STATUS_ERROR_INVALID_PARAMETER;
132 }
133
134 drv->vscreen = vl_drm_screen_create(drm_info->fd);
135 if (!drv->vscreen)
136 goto error_screen;
137 }
138 break;
139 default:
140 FREE(drv);
141 return VA_STATUS_ERROR_INVALID_DISPLAY;
142 }
143
144 drv->pipe = drv->vscreen->pscreen->context_create(drv->vscreen->pscreen,
145 drv->vscreen, 0);
146 if (!drv->pipe)
147 goto error_pipe;
148
149 drv->htab = handle_table_create();
150 if (!drv->htab)
151 goto error_htab;
152
153 vl_compositor_init(&drv->compositor, drv->pipe);
154 vl_compositor_init_state(&drv->cstate, drv->pipe);
155
156 vl_csc_get_matrix(VL_CSC_COLOR_STANDARD_BT_601, NULL, true, &drv->csc);
157 vl_compositor_set_csc_matrix(&drv->cstate, (const vl_csc_matrix *)&drv->csc);
158
159 ctx->pDriverData = (void *)drv;
160 ctx->version_major = 0;
161 ctx->version_minor = 1;
162 *ctx->vtable = vtable;
163 *ctx->vtable_vpp = vtable_vpp;
164 ctx->max_profiles = PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH - PIPE_VIDEO_PROFILE_UNKNOWN;
165 ctx->max_entrypoints = 1;
166 ctx->max_attributes = 1;
167 ctx->max_image_formats = VL_VA_MAX_IMAGE_FORMATS;
168 ctx->max_subpic_formats = 1;
169 ctx->max_display_attributes = 1;
170 ctx->str_vendor = "mesa gallium vaapi";
171
172 return VA_STATUS_SUCCESS;
173
174 error_htab:
175 drv->pipe->destroy(drv->pipe);
176
177 error_pipe:
178 drv->vscreen->destroy(drv->vscreen);
179
180 error_screen:
181 FREE(drv);
182 return VA_STATUS_ERROR_ALLOCATION_FAILED;
183 }
184
185 VAStatus
186 vlVaCreateContext(VADriverContextP ctx, VAConfigID config_id, int picture_width,
187 int picture_height, int flag, VASurfaceID *render_targets,
188 int num_render_targets, VAContextID *context_id)
189 {
190 vlVaDriver *drv;
191 vlVaContext *context;
192 int is_vpp;
193
194 if (!ctx)
195 return VA_STATUS_ERROR_INVALID_CONTEXT;
196
197 is_vpp = config_id == PIPE_VIDEO_PROFILE_UNKNOWN && !picture_width &&
198 !picture_height && !flag && !render_targets && !num_render_targets;
199
200 if (!(picture_width && picture_height) && !is_vpp)
201 return VA_STATUS_ERROR_INVALID_IMAGE_FORMAT;
202
203 drv = VL_VA_DRIVER(ctx);
204 context = CALLOC(1, sizeof(vlVaContext));
205 if (!context)
206 return VA_STATUS_ERROR_ALLOCATION_FAILED;
207
208 if (is_vpp) {
209 context->decoder = NULL;
210 if (!drv->compositor.upload) {
211 FREE(context);
212 return VA_STATUS_ERROR_INVALID_CONTEXT;
213 }
214 } else {
215 context->templat.profile = config_id;
216 context->templat.entrypoint = PIPE_VIDEO_ENTRYPOINT_BITSTREAM;
217 context->templat.chroma_format = PIPE_VIDEO_CHROMA_FORMAT_420;
218 context->templat.width = picture_width;
219 context->templat.height = picture_height;
220 context->templat.expect_chunked_decode = true;
221
222 switch (u_reduce_video_profile(context->templat.profile)) {
223 case PIPE_VIDEO_FORMAT_MPEG12:
224 case PIPE_VIDEO_FORMAT_VC1:
225 case PIPE_VIDEO_FORMAT_MPEG4:
226 context->templat.max_references = 2;
227 break;
228
229 case PIPE_VIDEO_FORMAT_MPEG4_AVC:
230 context->templat.max_references = 0;
231 context->desc.h264.pps = CALLOC_STRUCT(pipe_h264_pps);
232 if (!context->desc.h264.pps) {
233 FREE(context);
234 return VA_STATUS_ERROR_ALLOCATION_FAILED;
235 }
236 context->desc.h264.pps->sps = CALLOC_STRUCT(pipe_h264_sps);
237 if (!context->desc.h264.pps->sps) {
238 FREE(context->desc.h264.pps);
239 FREE(context);
240 return VA_STATUS_ERROR_ALLOCATION_FAILED;
241 }
242 break;
243
244 case PIPE_VIDEO_FORMAT_HEVC:
245 context->templat.max_references = num_render_targets;
246 context->desc.h265.pps = CALLOC_STRUCT(pipe_h265_pps);
247 if (!context->desc.h265.pps) {
248 FREE(context);
249 return VA_STATUS_ERROR_ALLOCATION_FAILED;
250 }
251 context->desc.h265.pps->sps = CALLOC_STRUCT(pipe_h265_sps);
252 if (!context->desc.h265.pps->sps) {
253 FREE(context->desc.h265.pps);
254 FREE(context);
255 return VA_STATUS_ERROR_ALLOCATION_FAILED;
256 }
257 break;
258
259 default:
260 break;
261 }
262 }
263
264 context->desc.base.profile = config_id;
265 *context_id = handle_table_add(drv->htab, context);
266
267 return VA_STATUS_SUCCESS;
268 }
269
270 VAStatus
271 vlVaDestroyContext(VADriverContextP ctx, VAContextID context_id)
272 {
273 vlVaDriver *drv;
274 vlVaContext *context;
275
276 if (!ctx)
277 return VA_STATUS_ERROR_INVALID_CONTEXT;
278
279 drv = VL_VA_DRIVER(ctx);
280 context = handle_table_get(drv->htab, context_id);
281
282 if (context->decoder) {
283 if (u_reduce_video_profile(context->decoder->profile) ==
284 PIPE_VIDEO_FORMAT_MPEG4_AVC) {
285 FREE(context->desc.h264.pps->sps);
286 FREE(context->desc.h264.pps);
287 }
288 if (u_reduce_video_profile(context->decoder->profile) ==
289 PIPE_VIDEO_FORMAT_HEVC) {
290 FREE(context->desc.h265.pps->sps);
291 FREE(context->desc.h265.pps);
292 }
293 context->decoder->destroy(context->decoder);
294 }
295 FREE(context);
296 handle_table_remove(drv->htab, context_id);
297
298 return VA_STATUS_SUCCESS;
299 }
300
301 VAStatus
302 vlVaTerminate(VADriverContextP ctx)
303 {
304 vlVaDriver *drv;
305
306 if (!ctx)
307 return VA_STATUS_ERROR_INVALID_CONTEXT;
308
309 drv = ctx->pDriverData;
310 vl_compositor_cleanup_state(&drv->cstate);
311 vl_compositor_cleanup(&drv->compositor);
312 drv->pipe->destroy(drv->pipe);
313 drv->vscreen->destroy(drv->vscreen);
314 handle_table_destroy(drv->htab);
315 FREE(drv);
316
317 return VA_STATUS_SUCCESS;
318 }