1 /**************************************************************************
2 *
3 * Copyright 2010 Younes Manton og Thomas Balling Sørensen.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include "util/compiler.h"
29
30 #include "util/u_memory.h"
31 #include "util/u_debug.h"
32 #include "util/format/u_format.h"
33 #include "util/u_sampler.h"
34
35 #include "vdpau_private.h"
36
37 /**
38 * Create a VdpDevice object for use with X11.
39 */
40 PUBLIC VdpStatus
vdp_imp_device_create_x11(Display * display,int screen,VdpDevice * device,VdpGetProcAddress ** get_proc_address)41 vdp_imp_device_create_x11(Display *display, int screen, VdpDevice *device,
42 VdpGetProcAddress **get_proc_address)
43 {
44 struct pipe_screen *pscreen;
45 struct pipe_resource *res, res_tmpl;
46 struct pipe_sampler_view sv_tmpl;
47 vlVdpDevice *dev = NULL;
48 VdpStatus ret;
49
50 if (!(display && device && get_proc_address))
51 return VDP_STATUS_INVALID_POINTER;
52
53 if (!vlCreateHTAB()) {
54 ret = VDP_STATUS_RESOURCES;
55 goto no_htab;
56 }
57
58 dev = CALLOC(1, sizeof(vlVdpDevice));
59 if (!dev) {
60 ret = VDP_STATUS_RESOURCES;
61 goto no_dev;
62 }
63
64 pipe_reference_init(&dev->reference, 1);
65
66 dev->vscreen = vl_dri3_screen_create(display, screen);
67 #ifdef HAVE_X11_DRI2
68 if (!dev->vscreen)
69 dev->vscreen = vl_dri2_screen_create(display, screen);
70 #endif
71 if (!dev->vscreen)
72 dev->vscreen = vl_xlib_swrast_screen_create(display, screen);
73 if (!dev->vscreen) {
74 ret = VDP_STATUS_RESOURCES;
75 goto no_vscreen;
76 }
77
78 pscreen = dev->vscreen->pscreen;
79 /* video cannot work if these are not supported */
80 if (!pscreen->get_video_param || !pscreen->is_video_format_supported) {
81 ret = VDP_STATUS_RESOURCES;
82 goto no_vscreen;
83 }
84
85 dev->context = pipe_create_multimedia_context(pscreen, false);
86 if (!dev->context) {
87 ret = VDP_STATUS_RESOURCES;
88 goto no_context;
89 }
90
91 if (!pscreen->caps.npot_textures) {
92 ret = VDP_STATUS_NO_IMPLEMENTATION;
93 goto no_context;
94 }
95
96 memset(&res_tmpl, 0, sizeof(res_tmpl));
97
98 res_tmpl.target = PIPE_TEXTURE_2D;
99 res_tmpl.format = PIPE_FORMAT_R8G8B8A8_UNORM;
100 res_tmpl.width0 = 1;
101 res_tmpl.height0 = 1;
102 res_tmpl.depth0 = 1;
103 res_tmpl.array_size = 1;
104 res_tmpl.bind = PIPE_BIND_SAMPLER_VIEW;
105 res_tmpl.usage = PIPE_USAGE_DEFAULT;
106
107 if (!CheckSurfaceParams(pscreen, &res_tmpl)) {
108 ret = VDP_STATUS_NO_IMPLEMENTATION;
109 goto no_resource;
110 }
111
112 res = pscreen->resource_create(pscreen, &res_tmpl);
113 if (!res) {
114 ret = VDP_STATUS_RESOURCES;
115 goto no_resource;
116 }
117
118 memset(&sv_tmpl, 0, sizeof(sv_tmpl));
119 u_sampler_view_default_template(&sv_tmpl, res, res->format);
120
121 sv_tmpl.swizzle_r = PIPE_SWIZZLE_1;
122 sv_tmpl.swizzle_g = PIPE_SWIZZLE_1;
123 sv_tmpl.swizzle_b = PIPE_SWIZZLE_1;
124 sv_tmpl.swizzle_a = PIPE_SWIZZLE_1;
125
126 dev->dummy_sv = dev->context->create_sampler_view(dev->context, res, &sv_tmpl);
127 pipe_resource_reference(&res, NULL);
128 if (!dev->dummy_sv) {
129 ret = VDP_STATUS_RESOURCES;
130 goto no_resource;
131 }
132
133 *device = vlAddDataHTAB(dev);
134 if (*device == 0) {
135 ret = VDP_STATUS_ERROR;
136 goto no_handle;
137 }
138
139 if (!vl_compositor_init(&dev->compositor, dev->context, false)) {
140 ret = VDP_STATUS_ERROR;
141 goto no_compositor;
142 }
143
144 (void) mtx_init(&dev->mutex, mtx_plain);
145
146 *get_proc_address = &vlVdpGetProcAddress;
147
148 return VDP_STATUS_OK;
149
150 no_compositor:
151 vlRemoveDataHTAB(*device);
152 no_handle:
153 pipe_sampler_view_reference(&dev->dummy_sv, NULL);
154 no_resource:
155 dev->context->destroy(dev->context);
156 no_context:
157 dev->vscreen->destroy(dev->vscreen);
158 no_vscreen:
159 FREE(dev);
160 no_dev:
161 vlDestroyHTAB();
162 no_htab:
163 return ret;
164 }
165
166 /**
167 * Create a VdpPresentationQueueTarget for use with X11.
168 */
169 VdpStatus
vlVdpPresentationQueueTargetCreateX11(VdpDevice device,Drawable drawable,VdpPresentationQueueTarget * target)170 vlVdpPresentationQueueTargetCreateX11(VdpDevice device, Drawable drawable,
171 VdpPresentationQueueTarget *target)
172 {
173 vlVdpPresentationQueueTarget *pqt;
174 VdpStatus ret;
175
176 if (!drawable)
177 return VDP_STATUS_INVALID_HANDLE;
178
179 vlVdpDevice *dev = vlGetDataHTAB(device);
180 if (!dev)
181 return VDP_STATUS_INVALID_HANDLE;
182
183 pqt = CALLOC(1, sizeof(vlVdpPresentationQueueTarget));
184 if (!pqt)
185 return VDP_STATUS_RESOURCES;
186
187 DeviceReference(&pqt->device, dev);
188 pqt->drawable = drawable;
189
190 *target = vlAddDataHTAB(pqt);
191 if (*target == 0) {
192 ret = VDP_STATUS_ERROR;
193 goto no_handle;
194 }
195
196 return VDP_STATUS_OK;
197
198 no_handle:
199 FREE(pqt);
200 return ret;
201 }
202
203 /**
204 * Destroy a VdpPresentationQueueTarget.
205 */
206 VdpStatus
vlVdpPresentationQueueTargetDestroy(VdpPresentationQueueTarget presentation_queue_target)207 vlVdpPresentationQueueTargetDestroy(VdpPresentationQueueTarget presentation_queue_target)
208 {
209 vlVdpPresentationQueueTarget *pqt;
210
211 pqt = vlGetDataHTAB(presentation_queue_target);
212 if (!pqt)
213 return VDP_STATUS_INVALID_HANDLE;
214
215 vlRemoveDataHTAB(presentation_queue_target);
216 DeviceReference(&pqt->device, NULL);
217 FREE(pqt);
218
219 return VDP_STATUS_OK;
220 }
221
222 /**
223 * Destroy a VdpDevice.
224 */
225 VdpStatus
vlVdpDeviceDestroy(VdpDevice device)226 vlVdpDeviceDestroy(VdpDevice device)
227 {
228 vlVdpDevice *dev = vlGetDataHTAB(device);
229 if (!dev)
230 return VDP_STATUS_INVALID_HANDLE;
231
232 vlRemoveDataHTAB(device);
233 DeviceReference(&dev, NULL);
234
235 return VDP_STATUS_OK;
236 }
237
238 /**
239 * Free a VdpDevice.
240 */
241 void
vlVdpDeviceFree(vlVdpDevice * dev)242 vlVdpDeviceFree(vlVdpDevice *dev)
243 {
244 mtx_destroy(&dev->mutex);
245 vl_compositor_cleanup(&dev->compositor);
246 pipe_sampler_view_reference(&dev->dummy_sv, NULL);
247 dev->context->destroy(dev->context);
248 dev->vscreen->destroy(dev->vscreen);
249 FREE(dev);
250 vlDestroyHTAB();
251 }
252
253 /**
254 * Retrieve a VDPAU function pointer.
255 */
256 VdpStatus
vlVdpGetProcAddress(VdpDevice device,VdpFuncId function_id,void ** function_pointer)257 vlVdpGetProcAddress(VdpDevice device, VdpFuncId function_id, void **function_pointer)
258 {
259 vlVdpDevice *dev = vlGetDataHTAB(device);
260 if (!dev)
261 return VDP_STATUS_INVALID_HANDLE;
262
263 if (!function_pointer)
264 return VDP_STATUS_INVALID_POINTER;
265
266 if (!vlGetFuncFTAB(function_id, function_pointer))
267 return VDP_STATUS_INVALID_FUNC_ID;
268
269 VDPAU_MSG(VDPAU_TRACE, "[VDPAU] Got proc address %p for id %d\n", *function_pointer, function_id);
270
271 return VDP_STATUS_OK;
272 }
273
274 #define _ERROR_TYPE(TYPE,STRING) case TYPE: return STRING;
275
276 /**
277 * Retrieve a string describing an error code.
278 */
279 char const *
vlVdpGetErrorString(VdpStatus status)280 vlVdpGetErrorString (VdpStatus status)
281 {
282 switch (status) {
283 _ERROR_TYPE(VDP_STATUS_OK,"The operation completed successfully; no error.");
284 _ERROR_TYPE(VDP_STATUS_NO_IMPLEMENTATION,"No backend implementation could be loaded.");
285 _ERROR_TYPE(VDP_STATUS_DISPLAY_PREEMPTED,"The display was preempted, or a fatal error occurred. The application must re-initialize VDPAU.");
286 _ERROR_TYPE(VDP_STATUS_INVALID_HANDLE,"An invalid handle value was provided. Either the handle does not exist at all, or refers to an object of an incorrect type.");
287 _ERROR_TYPE(VDP_STATUS_INVALID_POINTER,"An invalid pointer was provided. Typically, this means that a NULL pointer was provided for an 'output' parameter.");
288 _ERROR_TYPE(VDP_STATUS_INVALID_CHROMA_TYPE,"An invalid/unsupported VdpChromaType value was supplied.");
289 _ERROR_TYPE(VDP_STATUS_INVALID_Y_CB_CR_FORMAT,"An invalid/unsupported VdpYCbCrFormat value was supplied.");
290 _ERROR_TYPE(VDP_STATUS_INVALID_RGBA_FORMAT,"An invalid/unsupported VdpRGBAFormat value was supplied.");
291 _ERROR_TYPE(VDP_STATUS_INVALID_INDEXED_FORMAT,"An invalid/unsupported VdpIndexedFormat value was supplied.");
292 _ERROR_TYPE(VDP_STATUS_INVALID_COLOR_STANDARD,"An invalid/unsupported VdpColorStandard value was supplied.");
293 _ERROR_TYPE(VDP_STATUS_INVALID_COLOR_TABLE_FORMAT,"An invalid/unsupported VdpColorTableFormat value was supplied.");
294 _ERROR_TYPE(VDP_STATUS_INVALID_BLEND_FACTOR,"An invalid/unsupported VdpOutputSurfaceRenderBlendFactor value was supplied.");
295 _ERROR_TYPE(VDP_STATUS_INVALID_BLEND_EQUATION,"An invalid/unsupported VdpOutputSurfaceRenderBlendEquation value was supplied.");
296 _ERROR_TYPE(VDP_STATUS_INVALID_FLAG,"An invalid/unsupported flag value/combination was supplied.");
297 _ERROR_TYPE(VDP_STATUS_INVALID_DECODER_PROFILE,"An invalid/unsupported VdpDecoderProfile value was supplied.");
298 _ERROR_TYPE(VDP_STATUS_INVALID_VIDEO_MIXER_FEATURE,"An invalid/unsupported VdpVideoMixerFeature value was supplied.");
299 _ERROR_TYPE(VDP_STATUS_INVALID_VIDEO_MIXER_PARAMETER,"An invalid/unsupported VdpVideoMixerParameter value was supplied.");
300 _ERROR_TYPE(VDP_STATUS_INVALID_VIDEO_MIXER_ATTRIBUTE,"An invalid/unsupported VdpVideoMixerAttribute value was supplied.");
301 _ERROR_TYPE(VDP_STATUS_INVALID_VIDEO_MIXER_PICTURE_STRUCTURE,"An invalid/unsupported VdpVideoMixerPictureStructure value was supplied.");
302 _ERROR_TYPE(VDP_STATUS_INVALID_FUNC_ID,"An invalid/unsupported VdpFuncId value was supplied.");
303 _ERROR_TYPE(VDP_STATUS_INVALID_SIZE,"The size of a supplied object does not match the object it is being used with.\
304 For example, a VdpVideoMixer is configured to process VdpVideoSurface objects of a specific size.\
305 If presented with a VdpVideoSurface of a different size, this error will be raised.");
306 _ERROR_TYPE(VDP_STATUS_INVALID_VALUE,"An invalid/unsupported value was supplied.\
307 This is a catch-all error code for values of type other than those with a specific error code.");
308 _ERROR_TYPE(VDP_STATUS_INVALID_STRUCT_VERSION,"An invalid/unsupported structure version was specified in a versioned structure. \
309 This implies that the implementation is older than the header file the application was built against.");
310 _ERROR_TYPE(VDP_STATUS_RESOURCES,"The system does not have enough resources to complete the requested operation at this time.");
311 _ERROR_TYPE(VDP_STATUS_HANDLE_DEVICE_MISMATCH,"The set of handles supplied are not all related to the same VdpDevice.When performing operations \
312 that operate on multiple surfaces, such as VdpOutputSurfaceRenderOutputSurface or VdpVideoMixerRender, \
313 all supplied surfaces must have been created within the context of the same VdpDevice object. \
314 This error is raised if they were not.");
315 _ERROR_TYPE(VDP_STATUS_ERROR,"A catch-all error, used when no other error code applies.");
316 default: return "Unknown Error";
317 }
318 }
319
320 void
vlVdpDefaultSamplerViewTemplate(struct pipe_sampler_view * templ,struct pipe_resource * res)321 vlVdpDefaultSamplerViewTemplate(struct pipe_sampler_view *templ, struct pipe_resource *res)
322 {
323 const struct util_format_description *desc;
324
325 memset(templ, 0, sizeof(*templ));
326 u_sampler_view_default_template(templ, res, res->format);
327
328 desc = util_format_description(res->format);
329 if (desc->swizzle[0] == PIPE_SWIZZLE_0)
330 templ->swizzle_r = PIPE_SWIZZLE_1;
331 if (desc->swizzle[1] == PIPE_SWIZZLE_0)
332 templ->swizzle_g = PIPE_SWIZZLE_1;
333 if (desc->swizzle[2] == PIPE_SWIZZLE_0)
334 templ->swizzle_b = PIPE_SWIZZLE_1;
335 if (desc->swizzle[3] == PIPE_SWIZZLE_0)
336 templ->swizzle_a = PIPE_SWIZZLE_1;
337 }
338