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