xref: /aosp_15_r20/external/mesa3d/src/gallium/frontends/vdpau/device.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
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    dev->context = pipe_create_multimedia_context(pscreen);
80    if (!dev->context) {
81       ret = VDP_STATUS_RESOURCES;
82       goto no_context;
83    }
84 
85    if (!pscreen->get_param(pscreen, PIPE_CAP_NPOT_TEXTURES)) {
86       ret = VDP_STATUS_NO_IMPLEMENTATION;
87       goto no_context;
88    }
89 
90    memset(&res_tmpl, 0, sizeof(res_tmpl));
91 
92    res_tmpl.target = PIPE_TEXTURE_2D;
93    res_tmpl.format = PIPE_FORMAT_R8G8B8A8_UNORM;
94    res_tmpl.width0 = 1;
95    res_tmpl.height0 = 1;
96    res_tmpl.depth0 = 1;
97    res_tmpl.array_size = 1;
98    res_tmpl.bind = PIPE_BIND_SAMPLER_VIEW;
99    res_tmpl.usage = PIPE_USAGE_DEFAULT;
100 
101    if (!CheckSurfaceParams(pscreen, &res_tmpl)) {
102       ret = VDP_STATUS_NO_IMPLEMENTATION;
103       goto no_resource;
104    }
105 
106    res = pscreen->resource_create(pscreen, &res_tmpl);
107    if (!res) {
108       ret = VDP_STATUS_RESOURCES;
109       goto no_resource;
110    }
111 
112    memset(&sv_tmpl, 0, sizeof(sv_tmpl));
113    u_sampler_view_default_template(&sv_tmpl, res, res->format);
114 
115    sv_tmpl.swizzle_r = PIPE_SWIZZLE_1;
116    sv_tmpl.swizzle_g = PIPE_SWIZZLE_1;
117    sv_tmpl.swizzle_b = PIPE_SWIZZLE_1;
118    sv_tmpl.swizzle_a = PIPE_SWIZZLE_1;
119 
120    dev->dummy_sv = dev->context->create_sampler_view(dev->context, res, &sv_tmpl);
121    pipe_resource_reference(&res, NULL);
122    if (!dev->dummy_sv) {
123       ret = VDP_STATUS_RESOURCES;
124       goto no_resource;
125    }
126 
127    *device = vlAddDataHTAB(dev);
128    if (*device == 0) {
129       ret = VDP_STATUS_ERROR;
130       goto no_handle;
131    }
132 
133    if (!vl_compositor_init(&dev->compositor, dev->context)) {
134        ret = VDP_STATUS_ERROR;
135        goto no_compositor;
136    }
137 
138    (void) mtx_init(&dev->mutex, mtx_plain);
139 
140    *get_proc_address = &vlVdpGetProcAddress;
141 
142    return VDP_STATUS_OK;
143 
144 no_compositor:
145    vlRemoveDataHTAB(*device);
146 no_handle:
147    pipe_sampler_view_reference(&dev->dummy_sv, NULL);
148 no_resource:
149    dev->context->destroy(dev->context);
150 no_context:
151    dev->vscreen->destroy(dev->vscreen);
152 no_vscreen:
153    FREE(dev);
154 no_dev:
155    vlDestroyHTAB();
156 no_htab:
157    return ret;
158 }
159 
160 /**
161  * Create a VdpPresentationQueueTarget for use with X11.
162  */
163 VdpStatus
vlVdpPresentationQueueTargetCreateX11(VdpDevice device,Drawable drawable,VdpPresentationQueueTarget * target)164 vlVdpPresentationQueueTargetCreateX11(VdpDevice device, Drawable drawable,
165                                       VdpPresentationQueueTarget *target)
166 {
167    vlVdpPresentationQueueTarget *pqt;
168    VdpStatus ret;
169 
170    if (!drawable)
171       return VDP_STATUS_INVALID_HANDLE;
172 
173    vlVdpDevice *dev = vlGetDataHTAB(device);
174    if (!dev)
175       return VDP_STATUS_INVALID_HANDLE;
176 
177    pqt = CALLOC(1, sizeof(vlVdpPresentationQueueTarget));
178    if (!pqt)
179       return VDP_STATUS_RESOURCES;
180 
181    DeviceReference(&pqt->device, dev);
182    pqt->drawable = drawable;
183 
184    *target = vlAddDataHTAB(pqt);
185    if (*target == 0) {
186       ret = VDP_STATUS_ERROR;
187       goto no_handle;
188    }
189 
190    return VDP_STATUS_OK;
191 
192 no_handle:
193    FREE(pqt);
194    return ret;
195 }
196 
197 /**
198  * Destroy a VdpPresentationQueueTarget.
199  */
200 VdpStatus
vlVdpPresentationQueueTargetDestroy(VdpPresentationQueueTarget presentation_queue_target)201 vlVdpPresentationQueueTargetDestroy(VdpPresentationQueueTarget presentation_queue_target)
202 {
203    vlVdpPresentationQueueTarget *pqt;
204 
205    pqt = vlGetDataHTAB(presentation_queue_target);
206    if (!pqt)
207       return VDP_STATUS_INVALID_HANDLE;
208 
209    vlRemoveDataHTAB(presentation_queue_target);
210    DeviceReference(&pqt->device, NULL);
211    FREE(pqt);
212 
213    return VDP_STATUS_OK;
214 }
215 
216 /**
217  * Destroy a VdpDevice.
218  */
219 VdpStatus
vlVdpDeviceDestroy(VdpDevice device)220 vlVdpDeviceDestroy(VdpDevice device)
221 {
222    vlVdpDevice *dev = vlGetDataHTAB(device);
223    if (!dev)
224       return VDP_STATUS_INVALID_HANDLE;
225 
226    vlRemoveDataHTAB(device);
227    DeviceReference(&dev, NULL);
228 
229    return VDP_STATUS_OK;
230 }
231 
232 /**
233  * Free a VdpDevice.
234  */
235 void
vlVdpDeviceFree(vlVdpDevice * dev)236 vlVdpDeviceFree(vlVdpDevice *dev)
237 {
238    mtx_destroy(&dev->mutex);
239    vl_compositor_cleanup(&dev->compositor);
240    pipe_sampler_view_reference(&dev->dummy_sv, NULL);
241    dev->context->destroy(dev->context);
242    dev->vscreen->destroy(dev->vscreen);
243    FREE(dev);
244    vlDestroyHTAB();
245 }
246 
247 /**
248  * Retrieve a VDPAU function pointer.
249  */
250 VdpStatus
vlVdpGetProcAddress(VdpDevice device,VdpFuncId function_id,void ** function_pointer)251 vlVdpGetProcAddress(VdpDevice device, VdpFuncId function_id, void **function_pointer)
252 {
253    vlVdpDevice *dev = vlGetDataHTAB(device);
254    if (!dev)
255       return VDP_STATUS_INVALID_HANDLE;
256 
257    if (!function_pointer)
258       return VDP_STATUS_INVALID_POINTER;
259 
260    if (!vlGetFuncFTAB(function_id, function_pointer))
261       return VDP_STATUS_INVALID_FUNC_ID;
262 
263    VDPAU_MSG(VDPAU_TRACE, "[VDPAU] Got proc address %p for id %d\n", *function_pointer, function_id);
264 
265    return VDP_STATUS_OK;
266 }
267 
268 #define _ERROR_TYPE(TYPE,STRING) case TYPE: return STRING;
269 
270 /**
271  * Retrieve a string describing an error code.
272  */
273 char const *
vlVdpGetErrorString(VdpStatus status)274 vlVdpGetErrorString (VdpStatus status)
275 {
276    switch (status) {
277    _ERROR_TYPE(VDP_STATUS_OK,"The operation completed successfully; no error.");
278    _ERROR_TYPE(VDP_STATUS_NO_IMPLEMENTATION,"No backend implementation could be loaded.");
279    _ERROR_TYPE(VDP_STATUS_DISPLAY_PREEMPTED,"The display was preempted, or a fatal error occurred. The application must re-initialize VDPAU.");
280    _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.");
281    _ERROR_TYPE(VDP_STATUS_INVALID_POINTER,"An invalid pointer was provided. Typically, this means that a NULL pointer was provided for an 'output' parameter.");
282    _ERROR_TYPE(VDP_STATUS_INVALID_CHROMA_TYPE,"An invalid/unsupported VdpChromaType value was supplied.");
283    _ERROR_TYPE(VDP_STATUS_INVALID_Y_CB_CR_FORMAT,"An invalid/unsupported VdpYCbCrFormat value was supplied.");
284    _ERROR_TYPE(VDP_STATUS_INVALID_RGBA_FORMAT,"An invalid/unsupported VdpRGBAFormat value was supplied.");
285    _ERROR_TYPE(VDP_STATUS_INVALID_INDEXED_FORMAT,"An invalid/unsupported VdpIndexedFormat value was supplied.");
286    _ERROR_TYPE(VDP_STATUS_INVALID_COLOR_STANDARD,"An invalid/unsupported VdpColorStandard value was supplied.");
287    _ERROR_TYPE(VDP_STATUS_INVALID_COLOR_TABLE_FORMAT,"An invalid/unsupported VdpColorTableFormat value was supplied.");
288    _ERROR_TYPE(VDP_STATUS_INVALID_BLEND_FACTOR,"An invalid/unsupported VdpOutputSurfaceRenderBlendFactor value was supplied.");
289    _ERROR_TYPE(VDP_STATUS_INVALID_BLEND_EQUATION,"An invalid/unsupported VdpOutputSurfaceRenderBlendEquation value was supplied.");
290    _ERROR_TYPE(VDP_STATUS_INVALID_FLAG,"An invalid/unsupported flag value/combination was supplied.");
291    _ERROR_TYPE(VDP_STATUS_INVALID_DECODER_PROFILE,"An invalid/unsupported VdpDecoderProfile value was supplied.");
292    _ERROR_TYPE(VDP_STATUS_INVALID_VIDEO_MIXER_FEATURE,"An invalid/unsupported VdpVideoMixerFeature value was supplied.");
293    _ERROR_TYPE(VDP_STATUS_INVALID_VIDEO_MIXER_PARAMETER,"An invalid/unsupported VdpVideoMixerParameter value was supplied.");
294    _ERROR_TYPE(VDP_STATUS_INVALID_VIDEO_MIXER_ATTRIBUTE,"An invalid/unsupported VdpVideoMixerAttribute value was supplied.");
295    _ERROR_TYPE(VDP_STATUS_INVALID_VIDEO_MIXER_PICTURE_STRUCTURE,"An invalid/unsupported VdpVideoMixerPictureStructure value was supplied.");
296    _ERROR_TYPE(VDP_STATUS_INVALID_FUNC_ID,"An invalid/unsupported VdpFuncId value was supplied.");
297    _ERROR_TYPE(VDP_STATUS_INVALID_SIZE,"The size of a supplied object does not match the object it is being used with.\
298       For example, a VdpVideoMixer is configured to process VdpVideoSurface objects of a specific size.\
299       If presented with a VdpVideoSurface of a different size, this error will be raised.");
300    _ERROR_TYPE(VDP_STATUS_INVALID_VALUE,"An invalid/unsupported value was supplied.\
301       This is a catch-all error code for values of type other than those with a specific error code.");
302    _ERROR_TYPE(VDP_STATUS_INVALID_STRUCT_VERSION,"An invalid/unsupported structure version was specified in a versioned structure. \
303       This implies that the implementation is older than the header file the application was built against.");
304    _ERROR_TYPE(VDP_STATUS_RESOURCES,"The system does not have enough resources to complete the requested operation at this time.");
305    _ERROR_TYPE(VDP_STATUS_HANDLE_DEVICE_MISMATCH,"The set of handles supplied are not all related to the same VdpDevice.When performing operations \
306       that operate on multiple surfaces, such as VdpOutputSurfaceRenderOutputSurface or VdpVideoMixerRender, \
307       all supplied surfaces must have been created within the context of the same VdpDevice object. \
308       This error is raised if they were not.");
309    _ERROR_TYPE(VDP_STATUS_ERROR,"A catch-all error, used when no other error code applies.");
310    default: return "Unknown Error";
311    }
312 }
313 
314 void
vlVdpDefaultSamplerViewTemplate(struct pipe_sampler_view * templ,struct pipe_resource * res)315 vlVdpDefaultSamplerViewTemplate(struct pipe_sampler_view *templ, struct pipe_resource *res)
316 {
317    const struct util_format_description *desc;
318 
319    memset(templ, 0, sizeof(*templ));
320    u_sampler_view_default_template(templ, res, res->format);
321 
322    desc = util_format_description(res->format);
323    if (desc->swizzle[0] == PIPE_SWIZZLE_0)
324       templ->swizzle_r = PIPE_SWIZZLE_1;
325    if (desc->swizzle[1] == PIPE_SWIZZLE_0)
326       templ->swizzle_g = PIPE_SWIZZLE_1;
327    if (desc->swizzle[2] == PIPE_SWIZZLE_0)
328       templ->swizzle_b = PIPE_SWIZZLE_1;
329    if (desc->swizzle[3] == PIPE_SWIZZLE_0)
330       templ->swizzle_a = PIPE_SWIZZLE_1;
331 }
332