0cdda73f1c074b94963cf4ad03fb28ac7c70bdf4
[mesa.git] / src / gallium / state_trackers / vdpau / device.c
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 "pipe/p_compiler.h"
29
30 #include "util/u_memory.h"
31 #include "util/u_debug.h"
32 #include "util/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
41 vdp_imp_device_create_x11(Display *display, int screen, VdpDevice *device,
42 VdpGetProcAddress **get_proc_address)
43 {
44 struct pipe_screen *pscreen;
45 vlVdpDevice *dev = NULL;
46 VdpStatus ret;
47
48 if (!(display && device && get_proc_address))
49 return VDP_STATUS_INVALID_POINTER;
50
51 if (!vlCreateHTAB()) {
52 ret = VDP_STATUS_RESOURCES;
53 goto no_htab;
54 }
55
56 dev = CALLOC(1, sizeof(vlVdpDevice));
57 if (!dev) {
58 ret = VDP_STATUS_RESOURCES;
59 goto no_dev;
60 }
61
62 dev->vscreen = vl_screen_create(display, screen);
63 if (!dev->vscreen) {
64 ret = VDP_STATUS_RESOURCES;
65 goto no_vscreen;
66 }
67
68 pscreen = dev->vscreen->pscreen;
69 dev->context = pscreen->context_create(pscreen, dev->vscreen);
70 if (!dev->context) {
71 ret = VDP_STATUS_RESOURCES;
72 goto no_context;
73 }
74
75 if (!pscreen->get_param(pscreen, PIPE_CAP_NPOT_TEXTURES)) {
76 ret = VDP_STATUS_NO_IMPLEMENTATION;
77 goto no_context;
78 }
79
80 *device = vlAddDataHTAB(dev);
81 if (*device == 0) {
82 ret = VDP_STATUS_ERROR;
83 goto no_handle;
84 }
85
86 vl_compositor_init(&dev->compositor, dev->context);
87 pipe_mutex_init(dev->mutex);
88
89 *get_proc_address = &vlVdpGetProcAddress;
90
91 return VDP_STATUS_OK;
92
93 no_handle:
94 dev->context->destroy(dev->context);
95 /* Destroy vscreen */
96 no_context:
97 vl_screen_destroy(dev->vscreen);
98 no_vscreen:
99 FREE(dev);
100 no_dev:
101 vlDestroyHTAB();
102 no_htab:
103 return ret;
104 }
105
106 /**
107 * Create a VdpPresentationQueueTarget for use with X11.
108 */
109 VdpStatus
110 vlVdpPresentationQueueTargetCreateX11(VdpDevice device, Drawable drawable,
111 VdpPresentationQueueTarget *target)
112 {
113 vlVdpPresentationQueueTarget *pqt;
114 VdpStatus ret;
115
116 if (!drawable)
117 return VDP_STATUS_INVALID_HANDLE;
118
119 vlVdpDevice *dev = vlGetDataHTAB(device);
120 if (!dev)
121 return VDP_STATUS_INVALID_HANDLE;
122
123 pqt = CALLOC(1, sizeof(vlVdpPresentationQueue));
124 if (!pqt)
125 return VDP_STATUS_RESOURCES;
126
127 pqt->device = dev;
128 pqt->drawable = drawable;
129
130 *target = vlAddDataHTAB(pqt);
131 if (*target == 0) {
132 ret = VDP_STATUS_ERROR;
133 goto no_handle;
134 }
135
136 return VDP_STATUS_OK;
137
138 no_handle:
139 FREE(pqt);
140 return ret;
141 }
142
143 /**
144 * Destroy a VdpPresentationQueueTarget.
145 */
146 VdpStatus
147 vlVdpPresentationQueueTargetDestroy(VdpPresentationQueueTarget presentation_queue_target)
148 {
149 vlVdpPresentationQueueTarget *pqt;
150
151 pqt = vlGetDataHTAB(presentation_queue_target);
152 if (!pqt)
153 return VDP_STATUS_INVALID_HANDLE;
154
155 vlRemoveDataHTAB(presentation_queue_target);
156 FREE(pqt);
157
158 return VDP_STATUS_OK;
159 }
160
161 /**
162 * Destroy a VdpDevice.
163 */
164 VdpStatus
165 vlVdpDeviceDestroy(VdpDevice device)
166 {
167 vlVdpDevice *dev = vlGetDataHTAB(device);
168 if (!dev)
169 return VDP_STATUS_INVALID_HANDLE;
170
171 pipe_mutex_destroy(dev->mutex);
172 vl_compositor_cleanup(&dev->compositor);
173 dev->context->destroy(dev->context);
174 vl_screen_destroy(dev->vscreen);
175
176 vlRemoveDataHTAB(device);
177 FREE(dev);
178 vlDestroyHTAB();
179
180 return VDP_STATUS_OK;
181 }
182
183 /**
184 * Retrieve a VDPAU function pointer.
185 */
186 VdpStatus
187 vlVdpGetProcAddress(VdpDevice device, VdpFuncId function_id, void **function_pointer)
188 {
189 vlVdpDevice *dev = vlGetDataHTAB(device);
190 if (!dev)
191 return VDP_STATUS_INVALID_HANDLE;
192
193 if (!function_pointer)
194 return VDP_STATUS_INVALID_POINTER;
195
196 if (!vlGetFuncFTAB(function_id, function_pointer))
197 return VDP_STATUS_INVALID_FUNC_ID;
198
199 VDPAU_MSG(VDPAU_TRACE, "[VDPAU] Got proc adress %p for id %d\n", *function_pointer, function_id);
200
201 return VDP_STATUS_OK;
202 }
203
204 #define _ERROR_TYPE(TYPE,STRING) case TYPE: return STRING;
205
206 /**
207 * Retrieve a string describing an error code.
208 */
209 char const *
210 vlVdpGetErrorString (VdpStatus status)
211 {
212 switch (status) {
213 _ERROR_TYPE(VDP_STATUS_OK,"The operation completed successfully; no error.");
214 _ERROR_TYPE(VDP_STATUS_NO_IMPLEMENTATION,"No backend implementation could be loaded.");
215 _ERROR_TYPE(VDP_STATUS_DISPLAY_PREEMPTED,"The display was preempted, or a fatal error occurred. The application must re-initialize VDPAU.");
216 _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.");
217 _ERROR_TYPE(VDP_STATUS_INVALID_POINTER,"An invalid pointer was provided. Typically, this means that a NULL pointer was provided for an 'output' parameter.");
218 _ERROR_TYPE(VDP_STATUS_INVALID_CHROMA_TYPE,"An invalid/unsupported VdpChromaType value was supplied.");
219 _ERROR_TYPE(VDP_STATUS_INVALID_Y_CB_CR_FORMAT,"An invalid/unsupported VdpYCbCrFormat value was supplied.");
220 _ERROR_TYPE(VDP_STATUS_INVALID_RGBA_FORMAT,"An invalid/unsupported VdpRGBAFormat value was supplied.");
221 _ERROR_TYPE(VDP_STATUS_INVALID_INDEXED_FORMAT,"An invalid/unsupported VdpIndexedFormat value was supplied.");
222 _ERROR_TYPE(VDP_STATUS_INVALID_COLOR_STANDARD,"An invalid/unsupported VdpColorStandard value was supplied.");
223 _ERROR_TYPE(VDP_STATUS_INVALID_COLOR_TABLE_FORMAT,"An invalid/unsupported VdpColorTableFormat value was supplied.");
224 _ERROR_TYPE(VDP_STATUS_INVALID_BLEND_FACTOR,"An invalid/unsupported VdpOutputSurfaceRenderBlendFactor value was supplied.");
225 _ERROR_TYPE(VDP_STATUS_INVALID_BLEND_EQUATION,"An invalid/unsupported VdpOutputSurfaceRenderBlendEquation value was supplied.");
226 _ERROR_TYPE(VDP_STATUS_INVALID_FLAG,"An invalid/unsupported flag value/combination was supplied.");
227 _ERROR_TYPE(VDP_STATUS_INVALID_DECODER_PROFILE,"An invalid/unsupported VdpDecoderProfile value was supplied.");
228 _ERROR_TYPE(VDP_STATUS_INVALID_VIDEO_MIXER_FEATURE,"An invalid/unsupported VdpVideoMixerFeature value was supplied.");
229 _ERROR_TYPE(VDP_STATUS_INVALID_VIDEO_MIXER_PARAMETER,"An invalid/unsupported VdpVideoMixerParameter value was supplied.");
230 _ERROR_TYPE(VDP_STATUS_INVALID_VIDEO_MIXER_ATTRIBUTE,"An invalid/unsupported VdpVideoMixerAttribute value was supplied.");
231 _ERROR_TYPE(VDP_STATUS_INVALID_VIDEO_MIXER_PICTURE_STRUCTURE,"An invalid/unsupported VdpVideoMixerPictureStructure value was supplied.");
232 _ERROR_TYPE(VDP_STATUS_INVALID_FUNC_ID,"An invalid/unsupported VdpFuncId value was supplied.");
233 _ERROR_TYPE(VDP_STATUS_INVALID_SIZE,"The size of a supplied object does not match the object it is being used with.\
234 For example, a VdpVideoMixer is configured to process VdpVideoSurface objects of a specific size.\
235 If presented with a VdpVideoSurface of a different size, this error will be raised.");
236 _ERROR_TYPE(VDP_STATUS_INVALID_VALUE,"An invalid/unsupported value was supplied.\
237 This is a catch-all error code for values of type other than those with a specific error code.");
238 _ERROR_TYPE(VDP_STATUS_INVALID_STRUCT_VERSION,"An invalid/unsupported structure version was specified in a versioned structure. \
239 This implies that the implementation is older than the header file the application was built against.");
240 _ERROR_TYPE(VDP_STATUS_RESOURCES,"The system does not have enough resources to complete the requested operation at this time.");
241 _ERROR_TYPE(VDP_STATUS_HANDLE_DEVICE_MISMATCH,"The set of handles supplied are not all related to the same VdpDevice.When performing operations \
242 that operate on multiple surfaces, such as VdpOutputSurfaceRenderOutputSurface or VdpVideoMixerRender, \
243 all supplied surfaces must have been created within the context of the same VdpDevice object. \
244 This error is raised if they were not.");
245 _ERROR_TYPE(VDP_STATUS_ERROR,"A catch-all error, used when no other error code applies.");
246 default: return "Unknown Error";
247 }
248 }
249
250 void
251 vlVdpDefaultSamplerViewTemplate(struct pipe_sampler_view *templ, struct pipe_resource *res)
252 {
253 const struct util_format_description *desc;
254
255 memset(templ, 0, sizeof(*templ));
256 u_sampler_view_default_template(templ, res, res->format);
257
258 desc = util_format_description(res->format);
259 if (desc->swizzle[0] == UTIL_FORMAT_SWIZZLE_0)
260 templ->swizzle_r = PIPE_SWIZZLE_ONE;
261 if (desc->swizzle[1] == UTIL_FORMAT_SWIZZLE_0)
262 templ->swizzle_g = PIPE_SWIZZLE_ONE;
263 if (desc->swizzle[2] == UTIL_FORMAT_SWIZZLE_0)
264 templ->swizzle_b = PIPE_SWIZZLE_ONE;
265 if (desc->swizzle[3] == UTIL_FORMAT_SWIZZLE_0)
266 templ->swizzle_a = PIPE_SWIZZLE_ONE;
267 }
268
269 void
270 vlVdpResolveDelayedRendering(vlVdpDevice *dev, struct pipe_surface *surface, struct u_rect *dirty_area)
271 {
272 struct vl_compositor_state *cstate;
273 vlVdpOutputSurface *vlsurface;
274
275 assert(dev);
276
277 cstate = dev->delayed_rendering.cstate;
278 if (!cstate)
279 return;
280
281 vlsurface = vlGetDataHTAB(dev->delayed_rendering.surface);
282 if (!vlsurface)
283 return;
284
285 if (!surface) {
286 surface = vlsurface->surface;
287 dirty_area = &vlsurface->dirty_area;
288 }
289
290 vl_compositor_render(cstate, &dev->compositor, surface, dirty_area, true);
291
292 dev->delayed_rendering.surface = VDP_INVALID_HANDLE;
293 dev->delayed_rendering.cstate = NULL;
294
295 /* test if we need to create a new sampler for the just filled texture */
296 if (surface->texture != vlsurface->sampler_view->texture) {
297 struct pipe_resource *res = surface->texture;
298 struct pipe_sampler_view sv_templ;
299
300 vlVdpDefaultSamplerViewTemplate(&sv_templ, res);
301 pipe_sampler_view_reference(&vlsurface->sampler_view, NULL);
302 vlsurface->sampler_view = dev->context->create_sampler_view(dev->context, res, &sv_templ);
303 }
304
305 return;
306 }
307
308 void
309 vlVdpSave4DelayedRendering(vlVdpDevice *dev, VdpOutputSurface surface, struct vl_compositor_state *cstate)
310 {
311 assert(dev);
312
313 vlVdpResolveDelayedRendering(dev, NULL, NULL);
314
315 dev->delayed_rendering.surface = surface;
316 dev->delayed_rendering.cstate = cstate;
317 }