Path: blob/21.2-virgl/src/gallium/frontends/va/context.c
4561 views
/**************************************************************************1*2* Copyright 2010 Thomas Balling Sørensen & Orasanu Lucian.3* Copyright 2014 Advanced Micro Devices, Inc.4* All Rights Reserved.5*6* Permission is hereby granted, free of charge, to any person obtaining a7* copy of this software and associated documentation files (the8* "Software"), to deal in the Software without restriction, including9* without limitation the rights to use, copy, modify, merge, publish,10* distribute, sub license, and/or sell copies of the Software, and to11* permit persons to whom the Software is furnished to do so, subject to12* the following conditions:13*14* The above copyright notice and this permission notice (including the15* next paragraph) shall be included in all copies or substantial portions16* of the Software.17*18* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS19* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF20* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.21* IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR22* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,23* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE24* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.25*26**************************************************************************/2728#include "pipe/p_screen.h"29#include "pipe/p_video_codec.h"30#include "util/u_memory.h"31#include "util/u_handle_table.h"32#include "util/u_video.h"33#include "vl/vl_deint_filter.h"34#include "vl/vl_winsys.h"3536#include "va_private.h"3738#include <va/va_drmcommon.h>3940static 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,86NULL, /* DEPRECATED VaGetSurfaceAttributes */87&vlVaCreateSurfaces2,88&vlVaQuerySurfaceAttributes,89&vlVaAcquireBufferHandle,90&vlVaReleaseBufferHandle,91#if VA_CHECK_VERSION(1, 1, 0)92NULL, /* vaCreateMFContext */93NULL, /* vaMFAddContext */94NULL, /* vaMFReleaseContext */95NULL, /* vaMFSubmit */96NULL, /* vaCreateBuffer2 */97NULL, /* vaQueryProcessingRate */98&vlVaExportSurfaceHandle,99#endif100};101102static struct VADriverVTableVPP vtable_vpp =103{1041,105&vlVaQueryVideoProcFilters,106&vlVaQueryVideoProcFilterCaps,107&vlVaQueryVideoProcPipelineCaps108};109110PUBLIC VAStatus111VA_DRIVER_INIT_FUNC(VADriverContextP ctx)112{113vlVaDriver *drv;114115if (!ctx)116return VA_STATUS_ERROR_INVALID_CONTEXT;117118drv = CALLOC(1, sizeof(vlVaDriver));119if (!drv)120return VA_STATUS_ERROR_ALLOCATION_FAILED;121122switch (ctx->display_type) {123case VA_DISPLAY_ANDROID:124FREE(drv);125return VA_STATUS_ERROR_UNIMPLEMENTED;126case VA_DISPLAY_GLX:127case VA_DISPLAY_X11:128drv->vscreen = vl_dri3_screen_create(ctx->native_dpy, ctx->x11_screen);129if (!drv->vscreen)130drv->vscreen = vl_dri2_screen_create(ctx->native_dpy, ctx->x11_screen);131break;132case VA_DISPLAY_WAYLAND:133case VA_DISPLAY_DRM:134case VA_DISPLAY_DRM_RENDERNODES: {135const struct drm_state *drm_info = (struct drm_state *) ctx->drm_state;136137if (!drm_info || drm_info->fd < 0) {138FREE(drv);139return VA_STATUS_ERROR_INVALID_PARAMETER;140}141142drv->vscreen = vl_drm_screen_create(drm_info->fd);143break;144}145default:146FREE(drv);147return VA_STATUS_ERROR_INVALID_DISPLAY;148}149150if (!drv->vscreen)151goto error_screen;152153drv->pipe = pipe_create_multimedia_context(drv->vscreen->pscreen);154if (!drv->pipe)155goto error_pipe;156157drv->htab = handle_table_create();158if (!drv->htab)159goto error_htab;160161if (!vl_compositor_init(&drv->compositor, drv->pipe))162goto error_compositor;163if (!vl_compositor_init_state(&drv->cstate, drv->pipe))164goto error_compositor_state;165166vl_csc_get_matrix(VL_CSC_COLOR_STANDARD_BT_601, NULL, true, &drv->csc);167if (!vl_compositor_set_csc_matrix(&drv->cstate, (const vl_csc_matrix *)&drv->csc, 1.0f, 0.0f))168goto error_csc_matrix;169(void) mtx_init(&drv->mutex, mtx_plain);170171ctx->pDriverData = (void *)drv;172ctx->version_major = 0;173ctx->version_minor = 1;174*ctx->vtable = vtable;175*ctx->vtable_vpp = vtable_vpp;176ctx->max_profiles = PIPE_VIDEO_PROFILE_MAX - PIPE_VIDEO_PROFILE_UNKNOWN - 1;177ctx->max_entrypoints = 2;178ctx->max_attributes = 1;179ctx->max_image_formats = VL_VA_MAX_IMAGE_FORMATS;180ctx->max_subpic_formats = 1;181ctx->max_display_attributes = 1;182183snprintf(drv->vendor_string, sizeof(drv->vendor_string),184"Mesa Gallium driver " PACKAGE_VERSION " for %s",185drv->vscreen->pscreen->get_name(drv->vscreen->pscreen));186ctx->str_vendor = drv->vendor_string;187188return VA_STATUS_SUCCESS;189190error_csc_matrix:191vl_compositor_cleanup_state(&drv->cstate);192193error_compositor_state:194vl_compositor_cleanup(&drv->compositor);195196error_compositor:197handle_table_destroy(drv->htab);198199error_htab:200drv->pipe->destroy(drv->pipe);201202error_pipe:203drv->vscreen->destroy(drv->vscreen);204205error_screen:206FREE(drv);207return VA_STATUS_ERROR_ALLOCATION_FAILED;208}209210VAStatus211vlVaCreateContext(VADriverContextP ctx, VAConfigID config_id, int picture_width,212int picture_height, int flag, VASurfaceID *render_targets,213int num_render_targets, VAContextID *context_id)214{215vlVaDriver *drv;216vlVaContext *context;217vlVaConfig *config;218int is_vpp;219int max_supported_width,max_supported_height;220221if (!ctx)222return VA_STATUS_ERROR_INVALID_CONTEXT;223224drv = VL_VA_DRIVER(ctx);225mtx_lock(&drv->mutex);226config = handle_table_get(drv->htab, config_id);227mtx_unlock(&drv->mutex);228229if (!config)230return VA_STATUS_ERROR_INVALID_CONFIG;231232is_vpp = config->profile == PIPE_VIDEO_PROFILE_UNKNOWN && !picture_width &&233!picture_height && !flag && !render_targets && !num_render_targets;234235if (!(picture_width && picture_height) && !is_vpp)236return VA_STATUS_ERROR_INVALID_IMAGE_FORMAT;237238context = CALLOC(1, sizeof(vlVaContext));239if (!context)240return VA_STATUS_ERROR_ALLOCATION_FAILED;241242if (is_vpp) {243context->decoder = NULL;244} else {245if (config->entrypoint != PIPE_VIDEO_ENTRYPOINT_UNKNOWN) {246max_supported_width = drv->vscreen->pscreen->get_video_param(drv->vscreen->pscreen,247config->profile, config->entrypoint,248PIPE_VIDEO_CAP_MAX_WIDTH);249max_supported_height = drv->vscreen->pscreen->get_video_param(drv->vscreen->pscreen,250config->profile, config->entrypoint,251PIPE_VIDEO_CAP_MAX_HEIGHT);252253if (picture_width > max_supported_width || picture_height > max_supported_height) {254FREE(context);255return VA_STATUS_ERROR_RESOLUTION_NOT_SUPPORTED;256}257}258context->templat.profile = config->profile;259context->templat.entrypoint = config->entrypoint;260context->templat.chroma_format = PIPE_VIDEO_CHROMA_FORMAT_420;261context->templat.width = picture_width;262context->templat.height = picture_height;263context->templat.expect_chunked_decode = true;264265switch (u_reduce_video_profile(context->templat.profile)) {266case PIPE_VIDEO_FORMAT_MPEG12:267case PIPE_VIDEO_FORMAT_VC1:268case PIPE_VIDEO_FORMAT_MPEG4:269context->templat.max_references = 2;270break;271272case PIPE_VIDEO_FORMAT_MPEG4_AVC:273context->templat.max_references = 0;274if (config->entrypoint != PIPE_VIDEO_ENTRYPOINT_ENCODE) {275context->desc.h264.pps = CALLOC_STRUCT(pipe_h264_pps);276if (!context->desc.h264.pps) {277FREE(context);278return VA_STATUS_ERROR_ALLOCATION_FAILED;279}280context->desc.h264.pps->sps = CALLOC_STRUCT(pipe_h264_sps);281if (!context->desc.h264.pps->sps) {282FREE(context->desc.h264.pps);283FREE(context);284return VA_STATUS_ERROR_ALLOCATION_FAILED;285}286}287break;288289case PIPE_VIDEO_FORMAT_HEVC:290if (config->entrypoint != PIPE_VIDEO_ENTRYPOINT_ENCODE) {291context->desc.h265.pps = CALLOC_STRUCT(pipe_h265_pps);292if (!context->desc.h265.pps) {293FREE(context);294return VA_STATUS_ERROR_ALLOCATION_FAILED;295}296context->desc.h265.pps->sps = CALLOC_STRUCT(pipe_h265_sps);297if (!context->desc.h265.pps->sps) {298FREE(context->desc.h265.pps);299FREE(context);300return VA_STATUS_ERROR_ALLOCATION_FAILED;301}302}303break;304305case PIPE_VIDEO_FORMAT_VP9:306break;307308default:309break;310}311}312313context->desc.base.profile = config->profile;314context->desc.base.entry_point = config->entrypoint;315if (config->entrypoint == PIPE_VIDEO_ENTRYPOINT_ENCODE) {316switch (u_reduce_video_profile(context->templat.profile)) {317case PIPE_VIDEO_FORMAT_MPEG4_AVC:318context->desc.h264enc.rate_ctrl.rate_ctrl_method = config->rc;319context->desc.h264enc.frame_idx = util_hash_table_create_ptr_keys();320break;321case PIPE_VIDEO_FORMAT_HEVC:322context->desc.h265enc.rc.rate_ctrl_method = config->rc;323context->desc.h265enc.frame_idx = util_hash_table_create_ptr_keys();324break;325default:326break;327}328}329330mtx_lock(&drv->mutex);331*context_id = handle_table_add(drv->htab, context);332mtx_unlock(&drv->mutex);333334return VA_STATUS_SUCCESS;335}336337VAStatus338vlVaDestroyContext(VADriverContextP ctx, VAContextID context_id)339{340vlVaDriver *drv;341vlVaContext *context;342343if (!ctx)344return VA_STATUS_ERROR_INVALID_CONTEXT;345346drv = VL_VA_DRIVER(ctx);347mtx_lock(&drv->mutex);348context = handle_table_get(drv->htab, context_id);349if (!context) {350mtx_unlock(&drv->mutex);351return VA_STATUS_ERROR_INVALID_CONTEXT;352}353354if (context->decoder) {355if (context->desc.base.entry_point == PIPE_VIDEO_ENTRYPOINT_ENCODE) {356if (u_reduce_video_profile(context->decoder->profile) ==357PIPE_VIDEO_FORMAT_MPEG4_AVC) {358if (context->desc.h264enc.frame_idx)359_mesa_hash_table_destroy(context->desc.h264enc.frame_idx, NULL);360}361if (u_reduce_video_profile(context->decoder->profile) ==362PIPE_VIDEO_FORMAT_HEVC) {363if (context->desc.h265enc.frame_idx)364_mesa_hash_table_destroy(context->desc.h265enc.frame_idx, NULL);365}366} else {367if (u_reduce_video_profile(context->decoder->profile) ==368PIPE_VIDEO_FORMAT_MPEG4_AVC) {369FREE(context->desc.h264.pps->sps);370FREE(context->desc.h264.pps);371}372if (u_reduce_video_profile(context->decoder->profile) ==373PIPE_VIDEO_FORMAT_HEVC) {374FREE(context->desc.h265.pps->sps);375FREE(context->desc.h265.pps);376}377}378context->decoder->destroy(context->decoder);379}380if (context->blit_cs)381drv->pipe->delete_compute_state(drv->pipe, context->blit_cs);382if (context->deint) {383vl_deint_filter_cleanup(context->deint);384FREE(context->deint);385}386FREE(context);387handle_table_remove(drv->htab, context_id);388mtx_unlock(&drv->mutex);389390return VA_STATUS_SUCCESS;391}392393VAStatus394vlVaTerminate(VADriverContextP ctx)395{396vlVaDriver *drv;397398if (!ctx)399return VA_STATUS_ERROR_INVALID_CONTEXT;400401drv = ctx->pDriverData;402vl_compositor_cleanup_state(&drv->cstate);403vl_compositor_cleanup(&drv->compositor);404drv->pipe->destroy(drv->pipe);405drv->vscreen->destroy(drv->vscreen);406handle_table_destroy(drv->htab);407mtx_destroy(&drv->mutex);408FREE(drv);409410return VA_STATUS_SUCCESS;411}412413414