tu: Implement fallback linear staging blit for CopyImage
[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_deint_filter.h"
35 #include "vl/vl_winsys.h"
36
37 #include "va_private.h"
38
39 #include <va/va_drmcommon.h>
40
41 static struct VADriverVTable vtable =
42 {
43 &vlVaTerminate,
44 &vlVaQueryConfigProfiles,
45 &vlVaQueryConfigEntrypoints,
46 &vlVaGetConfigAttributes,
47 &vlVaCreateConfig,
48 &vlVaDestroyConfig,
49 &vlVaQueryConfigAttributes,
50 &vlVaCreateSurfaces,
51 &vlVaDestroySurfaces,
52 &vlVaCreateContext,
53 &vlVaDestroyContext,
54 &vlVaCreateBuffer,
55 &vlVaBufferSetNumElements,
56 &vlVaMapBuffer,
57 &vlVaUnmapBuffer,
58 &vlVaDestroyBuffer,
59 &vlVaBeginPicture,
60 &vlVaRenderPicture,
61 &vlVaEndPicture,
62 &vlVaSyncSurface,
63 &vlVaQuerySurfaceStatus,
64 &vlVaQuerySurfaceError,
65 &vlVaPutSurface,
66 &vlVaQueryImageFormats,
67 &vlVaCreateImage,
68 &vlVaDeriveImage,
69 &vlVaDestroyImage,
70 &vlVaSetImagePalette,
71 &vlVaGetImage,
72 &vlVaPutImage,
73 &vlVaQuerySubpictureFormats,
74 &vlVaCreateSubpicture,
75 &vlVaDestroySubpicture,
76 &vlVaSubpictureImage,
77 &vlVaSetSubpictureChromakey,
78 &vlVaSetSubpictureGlobalAlpha,
79 &vlVaAssociateSubpicture,
80 &vlVaDeassociateSubpicture,
81 &vlVaQueryDisplayAttributes,
82 &vlVaGetDisplayAttributes,
83 &vlVaSetDisplayAttributes,
84 &vlVaBufferInfo,
85 &vlVaLockSurface,
86 &vlVaUnlockSurface,
87 NULL, /* DEPRECATED VaGetSurfaceAttributes */
88 &vlVaCreateSurfaces2,
89 &vlVaQuerySurfaceAttributes,
90 &vlVaAcquireBufferHandle,
91 &vlVaReleaseBufferHandle,
92 #if VA_CHECK_VERSION(1, 1, 0)
93 NULL, /* vaCreateMFContext */
94 NULL, /* vaMFAddContext */
95 NULL, /* vaMFReleaseContext */
96 NULL, /* vaMFSubmit */
97 NULL, /* vaCreateBuffer2 */
98 NULL, /* vaQueryProcessingRate */
99 &vlVaExportSurfaceHandle,
100 #endif
101 };
102
103 static struct VADriverVTableVPP vtable_vpp =
104 {
105 1,
106 &vlVaQueryVideoProcFilters,
107 &vlVaQueryVideoProcFilterCaps,
108 &vlVaQueryVideoProcPipelineCaps
109 };
110
111 PUBLIC VAStatus
112 VA_DRIVER_INIT_FUNC(VADriverContextP ctx)
113 {
114 vlVaDriver *drv;
115
116 if (!ctx)
117 return VA_STATUS_ERROR_INVALID_CONTEXT;
118
119 drv = CALLOC(1, sizeof(vlVaDriver));
120 if (!drv)
121 return VA_STATUS_ERROR_ALLOCATION_FAILED;
122
123 switch (ctx->display_type) {
124 case VA_DISPLAY_ANDROID:
125 FREE(drv);
126 return VA_STATUS_ERROR_UNIMPLEMENTED;
127 case VA_DISPLAY_GLX:
128 case VA_DISPLAY_X11:
129 drv->vscreen = vl_dri3_screen_create(ctx->native_dpy, ctx->x11_screen);
130 if (!drv->vscreen)
131 drv->vscreen = vl_dri2_screen_create(ctx->native_dpy, ctx->x11_screen);
132 break;
133 case VA_DISPLAY_WAYLAND:
134 case VA_DISPLAY_DRM:
135 case VA_DISPLAY_DRM_RENDERNODES: {
136 const struct drm_state *drm_info = (struct drm_state *) ctx->drm_state;
137
138 if (!drm_info || drm_info->fd < 0) {
139 FREE(drv);
140 return VA_STATUS_ERROR_INVALID_PARAMETER;
141 }
142
143 drv->vscreen = vl_drm_screen_create(drm_info->fd);
144 break;
145 }
146 default:
147 FREE(drv);
148 return VA_STATUS_ERROR_INVALID_DISPLAY;
149 }
150
151 if (!drv->vscreen)
152 goto error_screen;
153
154 drv->pipe = pipe_create_multimedia_context(drv->vscreen->pscreen);
155 if (!drv->pipe)
156 goto error_pipe;
157
158 drv->htab = handle_table_create();
159 if (!drv->htab)
160 goto error_htab;
161
162 if (!vl_compositor_init(&drv->compositor, drv->pipe))
163 goto error_compositor;
164 if (!vl_compositor_init_state(&drv->cstate, drv->pipe))
165 goto error_compositor_state;
166
167 vl_csc_get_matrix(VL_CSC_COLOR_STANDARD_BT_601, NULL, true, &drv->csc);
168 if (!vl_compositor_set_csc_matrix(&drv->cstate, (const vl_csc_matrix *)&drv->csc, 1.0f, 0.0f))
169 goto error_csc_matrix;
170 (void) mtx_init(&drv->mutex, mtx_plain);
171
172 ctx->pDriverData = (void *)drv;
173 ctx->version_major = 0;
174 ctx->version_minor = 1;
175 *ctx->vtable = vtable;
176 *ctx->vtable_vpp = vtable_vpp;
177 ctx->max_profiles = PIPE_VIDEO_PROFILE_MAX - PIPE_VIDEO_PROFILE_UNKNOWN - 1;
178 ctx->max_entrypoints = 2;
179 ctx->max_attributes = 1;
180 ctx->max_image_formats = VL_VA_MAX_IMAGE_FORMATS;
181 ctx->max_subpic_formats = 1;
182 ctx->max_display_attributes = 1;
183
184 snprintf(drv->vendor_string, sizeof(drv->vendor_string),
185 "Mesa Gallium driver " PACKAGE_VERSION " for %s",
186 drv->vscreen->pscreen->get_name(drv->vscreen->pscreen));
187 ctx->str_vendor = drv->vendor_string;
188
189 return VA_STATUS_SUCCESS;
190
191 error_csc_matrix:
192 vl_compositor_cleanup_state(&drv->cstate);
193
194 error_compositor_state:
195 vl_compositor_cleanup(&drv->compositor);
196
197 error_compositor:
198 handle_table_destroy(drv->htab);
199
200 error_htab:
201 drv->pipe->destroy(drv->pipe);
202
203 error_pipe:
204 drv->vscreen->destroy(drv->vscreen);
205
206 error_screen:
207 FREE(drv);
208 return VA_STATUS_ERROR_ALLOCATION_FAILED;
209 }
210
211 VAStatus
212 vlVaCreateContext(VADriverContextP ctx, VAConfigID config_id, int picture_width,
213 int picture_height, int flag, VASurfaceID *render_targets,
214 int num_render_targets, VAContextID *context_id)
215 {
216 vlVaDriver *drv;
217 vlVaContext *context;
218 vlVaConfig *config;
219 int is_vpp;
220
221 if (!ctx)
222 return VA_STATUS_ERROR_INVALID_CONTEXT;
223
224 drv = VL_VA_DRIVER(ctx);
225 mtx_lock(&drv->mutex);
226 config = handle_table_get(drv->htab, config_id);
227 mtx_unlock(&drv->mutex);
228
229 is_vpp = config->profile == PIPE_VIDEO_PROFILE_UNKNOWN && !picture_width &&
230 !picture_height && !flag && !render_targets && !num_render_targets;
231
232 if (!(picture_width && picture_height) && !is_vpp)
233 return VA_STATUS_ERROR_INVALID_IMAGE_FORMAT;
234
235 context = CALLOC(1, sizeof(vlVaContext));
236 if (!context)
237 return VA_STATUS_ERROR_ALLOCATION_FAILED;
238
239 if (is_vpp) {
240 context->decoder = NULL;
241 } else {
242 context->templat.profile = config->profile;
243 context->templat.entrypoint = config->entrypoint;
244 context->templat.chroma_format = PIPE_VIDEO_CHROMA_FORMAT_420;
245 context->templat.width = picture_width;
246 context->templat.height = picture_height;
247 context->templat.expect_chunked_decode = true;
248
249 switch (u_reduce_video_profile(context->templat.profile)) {
250 case PIPE_VIDEO_FORMAT_MPEG12:
251 case PIPE_VIDEO_FORMAT_VC1:
252 case PIPE_VIDEO_FORMAT_MPEG4:
253 context->templat.max_references = 2;
254 break;
255
256 case PIPE_VIDEO_FORMAT_MPEG4_AVC:
257 context->templat.max_references = 0;
258 if (config->entrypoint != PIPE_VIDEO_ENTRYPOINT_ENCODE) {
259 context->desc.h264.pps = CALLOC_STRUCT(pipe_h264_pps);
260 if (!context->desc.h264.pps) {
261 FREE(context);
262 return VA_STATUS_ERROR_ALLOCATION_FAILED;
263 }
264 context->desc.h264.pps->sps = CALLOC_STRUCT(pipe_h264_sps);
265 if (!context->desc.h264.pps->sps) {
266 FREE(context->desc.h264.pps);
267 FREE(context);
268 return VA_STATUS_ERROR_ALLOCATION_FAILED;
269 }
270 }
271 break;
272
273 case PIPE_VIDEO_FORMAT_HEVC:
274 context->templat.max_references = num_render_targets;
275 if (config->entrypoint != PIPE_VIDEO_ENTRYPOINT_ENCODE) {
276 context->desc.h265.pps = CALLOC_STRUCT(pipe_h265_pps);
277 if (!context->desc.h265.pps) {
278 FREE(context);
279 return VA_STATUS_ERROR_ALLOCATION_FAILED;
280 }
281 context->desc.h265.pps->sps = CALLOC_STRUCT(pipe_h265_sps);
282 if (!context->desc.h265.pps->sps) {
283 FREE(context->desc.h265.pps);
284 FREE(context);
285 return VA_STATUS_ERROR_ALLOCATION_FAILED;
286 }
287 }
288 break;
289
290 case PIPE_VIDEO_FORMAT_VP9:
291 context->templat.max_references = num_render_targets;
292 break;
293
294 default:
295 break;
296 }
297 }
298
299 context->desc.base.profile = config->profile;
300 context->desc.base.entry_point = config->entrypoint;
301 if (config->entrypoint == PIPE_VIDEO_ENTRYPOINT_ENCODE) {
302 switch (u_reduce_video_profile(context->templat.profile)) {
303 case PIPE_VIDEO_FORMAT_MPEG4_AVC:
304 context->desc.h264enc.rate_ctrl.rate_ctrl_method = config->rc;
305 context->desc.h264enc.frame_idx = util_hash_table_create_ptr_keys();
306 break;
307 case PIPE_VIDEO_FORMAT_HEVC:
308 context->desc.h265enc.rc.rate_ctrl_method = config->rc;
309 context->desc.h265enc.frame_idx = util_hash_table_create_ptr_keys();
310 break;
311 default:
312 break;
313 }
314 }
315
316 mtx_lock(&drv->mutex);
317 *context_id = handle_table_add(drv->htab, context);
318 mtx_unlock(&drv->mutex);
319
320 return VA_STATUS_SUCCESS;
321 }
322
323 VAStatus
324 vlVaDestroyContext(VADriverContextP ctx, VAContextID context_id)
325 {
326 vlVaDriver *drv;
327 vlVaContext *context;
328
329 if (!ctx)
330 return VA_STATUS_ERROR_INVALID_CONTEXT;
331
332 drv = VL_VA_DRIVER(ctx);
333 mtx_lock(&drv->mutex);
334 context = handle_table_get(drv->htab, context_id);
335 if (!context) {
336 mtx_unlock(&drv->mutex);
337 return VA_STATUS_ERROR_INVALID_CONTEXT;
338 }
339
340 if (context->decoder) {
341 if (context->desc.base.entry_point == PIPE_VIDEO_ENTRYPOINT_ENCODE) {
342 if (u_reduce_video_profile(context->decoder->profile) ==
343 PIPE_VIDEO_FORMAT_MPEG4_AVC) {
344 if (context->desc.h264enc.frame_idx)
345 _mesa_hash_table_destroy(context->desc.h264enc.frame_idx, NULL);
346 }
347 if (u_reduce_video_profile(context->decoder->profile) ==
348 PIPE_VIDEO_FORMAT_HEVC) {
349 if (context->desc.h265enc.frame_idx)
350 _mesa_hash_table_destroy(context->desc.h265enc.frame_idx, NULL);
351 }
352 } else {
353 if (u_reduce_video_profile(context->decoder->profile) ==
354 PIPE_VIDEO_FORMAT_MPEG4_AVC) {
355 FREE(context->desc.h264.pps->sps);
356 FREE(context->desc.h264.pps);
357 }
358 if (u_reduce_video_profile(context->decoder->profile) ==
359 PIPE_VIDEO_FORMAT_HEVC) {
360 FREE(context->desc.h265.pps->sps);
361 FREE(context->desc.h265.pps);
362 }
363 }
364 context->decoder->destroy(context->decoder);
365 }
366 if (context->blit_cs)
367 drv->pipe->delete_compute_state(drv->pipe, context->blit_cs);
368 if (context->deint) {
369 vl_deint_filter_cleanup(context->deint);
370 FREE(context->deint);
371 }
372 FREE(context);
373 handle_table_remove(drv->htab, context_id);
374 mtx_unlock(&drv->mutex);
375
376 return VA_STATUS_SUCCESS;
377 }
378
379 VAStatus
380 vlVaTerminate(VADriverContextP ctx)
381 {
382 vlVaDriver *drv;
383
384 if (!ctx)
385 return VA_STATUS_ERROR_INVALID_CONTEXT;
386
387 drv = ctx->pDriverData;
388 vl_compositor_cleanup_state(&drv->cstate);
389 vl_compositor_cleanup(&drv->compositor);
390 drv->pipe->destroy(drv->pipe);
391 drv->vscreen->destroy(drv->vscreen);
392 handle_table_destroy(drv->htab);
393 mtx_destroy(&drv->mutex);
394 FREE(drv);
395
396 return VA_STATUS_SUCCESS;
397 }