radeonsi: properly handler raster_config setup on CZ
[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
32 #include "util/u_memory.h"
33 #include "util/u_handle_table.h"
34 #include "util/u_video.h"
35 #include "vl/vl_winsys.h"
36
37 #include "va_private.h"
38
39 static struct VADriverVTable vtable =
40 {
41 &vlVaTerminate,
42 &vlVaQueryConfigProfiles,
43 &vlVaQueryConfigEntrypoints,
44 &vlVaGetConfigAttributes,
45 &vlVaCreateConfig,
46 &vlVaDestroyConfig,
47 &vlVaQueryConfigAttributes,
48 &vlVaCreateSurfaces,
49 &vlVaDestroySurfaces,
50 &vlVaCreateContext,
51 &vlVaDestroyContext,
52 &vlVaCreateBuffer,
53 &vlVaBufferSetNumElements,
54 &vlVaMapBuffer,
55 &vlVaUnmapBuffer,
56 &vlVaDestroyBuffer,
57 &vlVaBeginPicture,
58 &vlVaRenderPicture,
59 &vlVaEndPicture,
60 &vlVaSyncSurface,
61 &vlVaQuerySurfaceStatus,
62 &vlVaQuerySurfaceError,
63 &vlVaPutSurface,
64 &vlVaQueryImageFormats,
65 &vlVaCreateImage,
66 &vlVaDeriveImage,
67 &vlVaDestroyImage,
68 &vlVaSetImagePalette,
69 &vlVaGetImage,
70 &vlVaPutImage,
71 &vlVaQuerySubpictureFormats,
72 &vlVaCreateSubpicture,
73 &vlVaDestroySubpicture,
74 &vlVaSubpictureImage,
75 &vlVaSetSubpictureChromakey,
76 &vlVaSetSubpictureGlobalAlpha,
77 &vlVaAssociateSubpicture,
78 &vlVaDeassociateSubpicture,
79 &vlVaQueryDisplayAttributes,
80 &vlVaGetDisplayAttributes,
81 &vlVaSetDisplayAttributes,
82 &vlVaBufferInfo,
83 &vlVaLockSurface,
84 &vlVaUnlockSurface
85 };
86
87 PUBLIC VAStatus
88 VA_DRIVER_INIT_FUNC(VADriverContextP ctx)
89 {
90 vlVaDriver *drv;
91
92 if (!ctx)
93 return VA_STATUS_ERROR_INVALID_CONTEXT;
94
95 drv = CALLOC(1, sizeof(vlVaDriver));
96 if (!drv)
97 return VA_STATUS_ERROR_ALLOCATION_FAILED;
98
99 drv->vscreen = vl_screen_create(ctx->native_dpy, ctx->x11_screen);
100 if (!drv->vscreen)
101 goto error_screen;
102
103 drv->pipe = drv->vscreen->pscreen->context_create(drv->vscreen->pscreen, drv->vscreen);
104 if (!drv->pipe)
105 goto error_pipe;
106
107 drv->htab = handle_table_create();
108 if (!drv->htab)
109 goto error_htab;
110
111 vl_compositor_init(&drv->compositor, drv->pipe);
112 vl_compositor_init_state(&drv->cstate, drv->pipe);
113
114 vl_csc_get_matrix(VL_CSC_COLOR_STANDARD_BT_601, NULL, true, &drv->csc);
115 vl_compositor_set_csc_matrix(&drv->cstate, (const vl_csc_matrix *)&drv->csc);
116
117 ctx->pDriverData = (void *)drv;
118 ctx->version_major = 0;
119 ctx->version_minor = 1;
120 *ctx->vtable = vtable;
121 ctx->max_profiles = PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH - PIPE_VIDEO_PROFILE_UNKNOWN;
122 ctx->max_entrypoints = 1;
123 ctx->max_attributes = 1;
124 ctx->max_image_formats = VL_VA_MAX_IMAGE_FORMATS;
125 ctx->max_subpic_formats = 1;
126 ctx->max_display_attributes = 1;
127 ctx->str_vendor = "mesa gallium vaapi";
128
129 return VA_STATUS_SUCCESS;
130
131 error_htab:
132 drv->pipe->destroy(drv->pipe);
133
134 error_pipe:
135 vl_screen_destroy(drv->vscreen);
136
137 error_screen:
138 FREE(drv);
139 return VA_STATUS_ERROR_ALLOCATION_FAILED;
140 }
141
142 VAStatus
143 vlVaCreateContext(VADriverContextP ctx, VAConfigID config_id, int picture_width,
144 int picture_height, int flag, VASurfaceID *render_targets,
145 int num_render_targets, VAContextID *context_id)
146 {
147 struct pipe_video_codec templat = {};
148 vlVaDriver *drv;
149 vlVaContext *context;
150
151 if (!ctx)
152 return VA_STATUS_ERROR_INVALID_CONTEXT;
153
154 if (!(picture_width && picture_height))
155 return VA_STATUS_ERROR_INVALID_IMAGE_FORMAT;
156
157 drv = VL_VA_DRIVER(ctx);
158 context = CALLOC(1, sizeof(vlVaContext));
159 if (!context)
160 return VA_STATUS_ERROR_ALLOCATION_FAILED;
161
162 templat.profile = config_id;
163 templat.entrypoint = PIPE_VIDEO_ENTRYPOINT_BITSTREAM;
164 templat.chroma_format = PIPE_VIDEO_CHROMA_FORMAT_420;
165 templat.width = picture_width;
166 templat.height = picture_height;
167 templat.max_references = num_render_targets;
168 templat.expect_chunked_decode = true;
169
170 if (u_reduce_video_profile(templat.profile) ==
171 PIPE_VIDEO_FORMAT_MPEG4_AVC)
172 templat.level = u_get_h264_level(templat.width, templat.height,
173 &templat.max_references);
174
175 context->decoder = drv->pipe->create_video_codec(drv->pipe, &templat);
176 if (!context->decoder) {
177 FREE(context);
178 return VA_STATUS_ERROR_ALLOCATION_FAILED;
179 }
180
181 if (u_reduce_video_profile(context->decoder->profile) ==
182 PIPE_VIDEO_FORMAT_MPEG4_AVC) {
183 context->desc.h264.pps = CALLOC_STRUCT(pipe_h264_pps);
184 if (!context->desc.h264.pps) {
185 FREE(context);
186 return VA_STATUS_ERROR_ALLOCATION_FAILED;
187 }
188 context->desc.h264.pps->sps = CALLOC_STRUCT(pipe_h264_sps);
189 if (!context->desc.h264.pps->sps) {
190 FREE(context->desc.h264.pps);
191 FREE(context);
192 return VA_STATUS_ERROR_ALLOCATION_FAILED;
193 }
194 }
195
196 context->desc.base.profile = config_id;
197 *context_id = handle_table_add(drv->htab, context);
198
199 return VA_STATUS_SUCCESS;
200 }
201
202 VAStatus
203 vlVaDestroyContext(VADriverContextP ctx, VAContextID context_id)
204 {
205 vlVaDriver *drv;
206 vlVaContext *context;
207
208 if (!ctx)
209 return VA_STATUS_ERROR_INVALID_CONTEXT;
210
211 drv = VL_VA_DRIVER(ctx);
212 context = handle_table_get(drv->htab, context_id);
213 if (u_reduce_video_profile(context->decoder->profile) ==
214 PIPE_VIDEO_FORMAT_MPEG4_AVC) {
215 FREE(context->desc.h264.pps->sps);
216 FREE(context->desc.h264.pps);
217 }
218 context->decoder->destroy(context->decoder);
219 FREE(context);
220 handle_table_remove(drv->htab, context_id);
221
222 return VA_STATUS_SUCCESS;
223 }
224
225 VAStatus
226 vlVaTerminate(VADriverContextP ctx)
227 {
228 vlVaDriver *drv;
229
230 if (!ctx)
231 return VA_STATUS_ERROR_INVALID_CONTEXT;
232
233 drv = ctx->pDriverData;
234 vl_compositor_cleanup_state(&drv->cstate);
235 vl_compositor_cleanup(&drv->compositor);
236 drv->pipe->destroy(drv->pipe);
237 vl_screen_destroy(drv->vscreen);
238 handle_table_destroy(drv->htab);
239 FREE(drv);
240
241 return VA_STATUS_SUCCESS;
242 }