r300: respect radeon common code fallbacks
[mesa.git] / src / gallium / state_trackers / xorg / xvmc / context.c
1 /**************************************************************************
2 *
3 * Copyright 2009 Younes Manton.
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 <assert.h>
29 #include <X11/Xlibint.h>
30 #include <X11/extensions/XvMClib.h>
31 #include <pipe/p_screen.h>
32 #include <pipe/p_video_context.h>
33 #include <pipe/p_video_state.h>
34 #include <pipe/p_state.h>
35 #include <vl_winsys.h>
36 #include <util/u_memory.h>
37 #include <util/u_debug.h>
38 #include <vl/vl_csc.h>
39 #include "xvmc_private.h"
40
41 static Status Validate(Display *dpy, XvPortID port, int surface_type_id,
42 unsigned int width, unsigned int height, int flags,
43 bool *found_port, int *screen, int *chroma_format,
44 int *mc_type, int *surface_flags)
45 {
46 bool found_surface = false;
47 XvAdaptorInfo *adaptor_info;
48 unsigned int num_adaptors;
49 int num_types;
50 unsigned int max_width, max_height;
51 Status ret;
52
53 assert(dpy);
54 assert(found_port);
55 assert(screen);
56 assert(chroma_format);
57 assert(mc_type);
58 assert(surface_flags);
59
60 *found_port = false;
61
62 for (unsigned int i = 0; i < XScreenCount(dpy); ++i) {
63 ret = XvQueryAdaptors(dpy, XRootWindow(dpy, i), &num_adaptors, &adaptor_info);
64 if (ret != Success)
65 return ret;
66
67 for (unsigned int j = 0; j < num_adaptors && !*found_port; ++j) {
68 for (unsigned int k = 0; k < adaptor_info[j].num_ports && !*found_port; ++k) {
69 XvMCSurfaceInfo *surface_info;
70
71 if (adaptor_info[j].base_id + k != port)
72 continue;
73
74 *found_port = true;
75
76 surface_info = XvMCListSurfaceTypes(dpy, adaptor_info[j].base_id, &num_types);
77 if (!surface_info) {
78 XvFreeAdaptorInfo(adaptor_info);
79 return BadAlloc;
80 }
81
82 for (unsigned int l = 0; l < num_types && !found_surface; ++l) {
83 if (surface_info[l].surface_type_id != surface_type_id)
84 continue;
85
86 found_surface = true;
87 max_width = surface_info[l].max_width;
88 max_height = surface_info[l].max_height;
89 *chroma_format = surface_info[l].chroma_format;
90 *mc_type = surface_info[l].mc_type;
91 *surface_flags = surface_info[l].flags;
92 *screen = i;
93 }
94
95 XFree(surface_info);
96 }
97 }
98
99 XvFreeAdaptorInfo(adaptor_info);
100 }
101
102 if (!*found_port)
103 return XvBadPort;
104 if (!found_surface)
105 return BadMatch;
106 if (width > max_width || height > max_height)
107 return BadValue;
108 if (flags != XVMC_DIRECT && flags != 0)
109 return BadValue;
110
111 return Success;
112 }
113
114 static enum pipe_video_profile ProfileToPipe(int xvmc_profile)
115 {
116 if (xvmc_profile & XVMC_MPEG_1)
117 assert(0);
118 if (xvmc_profile & XVMC_MPEG_2)
119 return PIPE_VIDEO_PROFILE_MPEG2_MAIN;
120 if (xvmc_profile & XVMC_H263)
121 assert(0);
122 if (xvmc_profile & XVMC_MPEG_4)
123 assert(0);
124
125 assert(0);
126
127 return -1;
128 }
129
130 static enum pipe_video_chroma_format FormatToPipe(int xvmc_format)
131 {
132 switch (xvmc_format) {
133 case XVMC_CHROMA_FORMAT_420:
134 return PIPE_VIDEO_CHROMA_FORMAT_420;
135 case XVMC_CHROMA_FORMAT_422:
136 return PIPE_VIDEO_CHROMA_FORMAT_422;
137 case XVMC_CHROMA_FORMAT_444:
138 return PIPE_VIDEO_CHROMA_FORMAT_444;
139 default:
140 assert(0);
141 }
142
143 return -1;
144 }
145
146 Status XvMCCreateContext(Display *dpy, XvPortID port, int surface_type_id,
147 int width, int height, int flags, XvMCContext *context)
148 {
149 bool found_port;
150 int scrn;
151 int chroma_format;
152 int mc_type;
153 int surface_flags;
154 Status ret;
155 struct pipe_screen *screen;
156 struct pipe_video_context *vpipe;
157 XvMCContextPrivate *context_priv;
158 float csc[16];
159
160 assert(dpy);
161
162 if (!context)
163 return XvMCBadContext;
164
165 ret = Validate(dpy, port, surface_type_id, width, height, flags,
166 &found_port, &scrn, &chroma_format, &mc_type, &surface_flags);
167
168 /* Success and XvBadPort have the same value */
169 if (ret != Success || !found_port)
170 return ret;
171
172 /* XXX: Current limits */
173 if (chroma_format != XVMC_CHROMA_FORMAT_420) {
174 debug_printf("[XvMCg3dvl] Cannot decode requested surface type. Unsupported chroma format.\n");
175 return BadImplementation;
176 }
177 if (mc_type != (XVMC_MOCOMP | XVMC_MPEG_2)) {
178 debug_printf("[XvMCg3dvl] Cannot decode requested surface type. Non-MPEG2/Mocomp acceleration unsupported.\n");
179 return BadImplementation;
180 }
181 if (!(surface_flags & XVMC_INTRA_UNSIGNED)) {
182 debug_printf("[XvMCg3dvl] Cannot decode requested surface type. Signed intra unsupported.\n");
183 return BadImplementation;
184 }
185
186 context_priv = CALLOC(1, sizeof(XvMCContextPrivate));
187 if (!context_priv)
188 return BadAlloc;
189
190 /* TODO: Reuse screen if process creates another context */
191 screen = vl_screen_create(dpy, scrn);
192
193 if (!screen) {
194 FREE(context_priv);
195 return BadAlloc;
196 }
197
198 vpipe = vl_video_create(dpy, scrn, screen, ProfileToPipe(mc_type),
199 FormatToPipe(chroma_format), width, height);
200
201 if (!vpipe) {
202 screen->destroy(screen);
203 FREE(context_priv);
204 return BadAlloc;
205 }
206
207 /* TODO: Define some Xv attribs to allow users to specify color standard, procamp */
208 vl_csc_get_matrix
209 (
210 debug_get_bool_option("G3DVL_NO_CSC", FALSE) ?
211 VL_CSC_COLOR_STANDARD_IDENTITY : VL_CSC_COLOR_STANDARD_BT_601,
212 NULL, true, csc
213 );
214 vpipe->set_csc_matrix(vpipe, csc);
215
216 context_priv->vpipe = vpipe;
217
218 context->context_id = XAllocID(dpy);
219 context->surface_type_id = surface_type_id;
220 context->width = width;
221 context->height = height;
222 context->flags = flags;
223 context->port = port;
224 context->privData = context_priv;
225
226 SyncHandle();
227
228 return Success;
229 }
230
231 Status XvMCDestroyContext(Display *dpy, XvMCContext *context)
232 {
233 struct pipe_screen *screen;
234 struct pipe_video_context *vpipe;
235 XvMCContextPrivate *context_priv;
236
237 assert(dpy);
238
239 if (!context || !context->privData)
240 return XvMCBadContext;
241
242 context_priv = context->privData;
243 vpipe = context_priv->vpipe;
244 pipe_surface_reference(&context_priv->backbuffer, NULL);
245 screen = vpipe->screen;
246 vpipe->destroy(vpipe);
247 screen->destroy(screen);
248 FREE(context_priv);
249 context->privData = NULL;
250
251 return Success;
252 }