Merge branch 'mesa_7_6_branch'
[mesa.git] / src / gallium / state_trackers / xorg / xvmc / surface.c
1 #include <assert.h>
2 #include <X11/Xlibint.h>
3 #include <pipe/p_video_context.h>
4 #include <pipe/p_video_state.h>
5 #include <pipe/p_state.h>
6 #include <util/u_memory.h>
7 #include "xvmc_private.h"
8
9 static enum pipe_mpeg12_macroblock_type TypeToPipe(int xvmc_mb_type)
10 {
11 if (xvmc_mb_type & XVMC_MB_TYPE_INTRA)
12 return PIPE_MPEG12_MACROBLOCK_TYPE_INTRA;
13 if ((xvmc_mb_type & (XVMC_MB_TYPE_MOTION_FORWARD | XVMC_MB_TYPE_MOTION_BACKWARD)) == XVMC_MB_TYPE_MOTION_FORWARD)
14 return PIPE_MPEG12_MACROBLOCK_TYPE_FWD;
15 if ((xvmc_mb_type & (XVMC_MB_TYPE_MOTION_FORWARD | XVMC_MB_TYPE_MOTION_BACKWARD)) == XVMC_MB_TYPE_MOTION_BACKWARD)
16 return PIPE_MPEG12_MACROBLOCK_TYPE_BKWD;
17 if ((xvmc_mb_type & (XVMC_MB_TYPE_MOTION_FORWARD | XVMC_MB_TYPE_MOTION_BACKWARD)) == (XVMC_MB_TYPE_MOTION_FORWARD | XVMC_MB_TYPE_MOTION_BACKWARD))
18 return PIPE_MPEG12_MACROBLOCK_TYPE_BI;
19
20 assert(0);
21
22 return -1;
23 }
24
25 static enum pipe_mpeg12_picture_type PictureToPipe(int xvmc_pic)
26 {
27 switch (xvmc_pic) {
28 case XVMC_TOP_FIELD:
29 return PIPE_MPEG12_PICTURE_TYPE_FIELD_TOP;
30 case XVMC_BOTTOM_FIELD:
31 return PIPE_MPEG12_PICTURE_TYPE_FIELD_BOTTOM;
32 case XVMC_FRAME_PICTURE:
33 return PIPE_MPEG12_PICTURE_TYPE_FRAME;
34 default:
35 assert(0);
36 }
37
38 return -1;
39 }
40
41 static enum pipe_mpeg12_motion_type MotionToPipe(int xvmc_motion_type, int xvmc_dct_type)
42 {
43 switch (xvmc_motion_type) {
44 case XVMC_PREDICTION_FRAME:
45 return xvmc_dct_type == XVMC_DCT_TYPE_FIELD ?
46 PIPE_MPEG12_MOTION_TYPE_16x8 : PIPE_MPEG12_MOTION_TYPE_FRAME;
47 case XVMC_PREDICTION_FIELD:
48 return PIPE_MPEG12_MOTION_TYPE_FIELD;
49 case XVMC_PREDICTION_DUAL_PRIME:
50 return PIPE_MPEG12_MOTION_TYPE_DUALPRIME;
51 default:
52 assert(0);
53 }
54
55 return -1;
56 }
57
58 static bool
59 CreateOrResizeBackBuffer(struct pipe_video_context *vpipe, unsigned int width, unsigned int height,
60 struct pipe_surface **backbuffer)
61 {
62 struct pipe_texture template;
63 struct pipe_texture *tex;
64
65 assert(vpipe);
66
67 if (*backbuffer) {
68 if ((*backbuffer)->width != width || (*backbuffer)->height != height)
69 pipe_surface_reference(backbuffer, NULL);
70 else
71 return true;
72 }
73
74 memset(&template, 0, sizeof(struct pipe_texture));
75 template.target = PIPE_TEXTURE_2D;
76 /* XXX: Needs to match the drawable's format? */
77 template.format = PIPE_FORMAT_X8R8G8B8_UNORM;
78 template.last_level = 0;
79 template.width[0] = width;
80 template.height[0] = height;
81 template.depth[0] = 1;
82 pf_get_block(template.format, &template.block);
83 template.tex_usage = PIPE_TEXTURE_USAGE_DISPLAY_TARGET;
84
85 tex = vpipe->screen->texture_create(vpipe->screen, &template);
86 if (!tex)
87 return false;
88
89 *backbuffer = vpipe->screen->get_tex_surface(vpipe->screen, tex, 0, 0, 0,
90 PIPE_BUFFER_USAGE_GPU_READ |
91 PIPE_BUFFER_USAGE_GPU_WRITE);
92 pipe_texture_reference(&tex, NULL);
93
94 if (!*backbuffer)
95 return false;
96
97 /* Clear the backbuffer in case the video doesn't cover the whole window */
98 /* FIXME: Need to clear every time a frame moves and leaves dirty rects */
99 vpipe->clear_surface(vpipe, 0, 0, width, height, 0, *backbuffer);
100
101 return true;
102 }
103
104 static void
105 MacroBlocksToPipe(const XvMCMacroBlockArray *xvmc_macroblocks,
106 const XvMCBlockArray *xvmc_blocks,
107 unsigned int first_macroblock,
108 unsigned int num_macroblocks,
109 struct pipe_mpeg12_macroblock *pipe_macroblocks)
110 {
111 unsigned int i, j, k, l;
112 XvMCMacroBlock *xvmc_mb;
113
114 assert(xvmc_macroblocks);
115 assert(xvmc_blocks);
116 assert(pipe_macroblocks);
117 assert(num_macroblocks);
118
119 xvmc_mb = xvmc_macroblocks->macro_blocks + first_macroblock;
120
121 for (i = 0; i < num_macroblocks; ++i) {
122 pipe_macroblocks->base.codec = PIPE_VIDEO_CODEC_MPEG12;
123 pipe_macroblocks->mbx = xvmc_mb->x;
124 pipe_macroblocks->mby = xvmc_mb->y;
125 pipe_macroblocks->mb_type = TypeToPipe(xvmc_mb->macroblock_type);
126 if (pipe_macroblocks->mb_type != PIPE_MPEG12_MACROBLOCK_TYPE_INTRA)
127 pipe_macroblocks->mo_type = MotionToPipe(xvmc_mb->motion_type, xvmc_mb->dct_type);
128 /* Get rid of Valgrind 'undefined' warnings */
129 else
130 pipe_macroblocks->mo_type = -1;
131 pipe_macroblocks->dct_type = xvmc_mb->dct_type == XVMC_DCT_TYPE_FIELD ?
132 PIPE_MPEG12_DCT_TYPE_FIELD : PIPE_MPEG12_DCT_TYPE_FRAME;
133
134 for (j = 0; j < 2; ++j)
135 for (k = 0; k < 2; ++k)
136 for (l = 0; l < 2; ++l)
137 pipe_macroblocks->pmv[j][k][l] = xvmc_mb->PMV[j][k][l];
138
139 pipe_macroblocks->cbp = xvmc_mb->coded_block_pattern;
140 pipe_macroblocks->blocks = xvmc_blocks->blocks + xvmc_mb->index * BLOCK_SIZE_SAMPLES;
141
142 ++pipe_macroblocks;
143 ++xvmc_mb;
144 }
145 }
146
147 Status XvMCCreateSurface(Display *dpy, XvMCContext *context, XvMCSurface *surface)
148 {
149 XvMCContextPrivate *context_priv;
150 struct pipe_video_context *vpipe;
151 XvMCSurfacePrivate *surface_priv;
152 struct pipe_video_surface *vsfc;
153
154 assert(dpy);
155
156 if (!context)
157 return XvMCBadContext;
158 if (!surface)
159 return XvMCBadSurface;
160
161 context_priv = context->privData;
162 vpipe = context_priv->vpipe;
163
164 surface_priv = CALLOC(1, sizeof(XvMCSurfacePrivate));
165 if (!surface_priv)
166 return BadAlloc;
167
168 vsfc = vpipe->screen->video_surface_create(vpipe->screen, vpipe->chroma_format,
169 vpipe->width, vpipe->height);
170 if (!vsfc) {
171 FREE(surface_priv);
172 return BadAlloc;
173 }
174
175 surface_priv->pipe_vsfc = vsfc;
176 surface_priv->context = context;
177
178 surface->surface_id = XAllocID(dpy);
179 surface->context_id = context->context_id;
180 surface->surface_type_id = context->surface_type_id;
181 surface->width = context->width;
182 surface->height = context->height;
183 surface->privData = surface_priv;
184
185 SyncHandle();
186
187 return Success;
188 }
189
190 Status XvMCRenderSurface(Display *dpy, XvMCContext *context, unsigned int picture_structure,
191 XvMCSurface *target_surface, XvMCSurface *past_surface, XvMCSurface *future_surface,
192 unsigned int flags, unsigned int num_macroblocks, unsigned int first_macroblock,
193 XvMCMacroBlockArray *macroblocks, XvMCBlockArray *blocks
194 )
195 {
196 struct pipe_video_context *vpipe;
197 struct pipe_surface *t_vsfc;
198 struct pipe_surface *p_vsfc;
199 struct pipe_surface *f_vsfc;
200 XvMCContextPrivate *context_priv;
201 XvMCSurfacePrivate *target_surface_priv;
202 XvMCSurfacePrivate *past_surface_priv;
203 XvMCSurfacePrivate *future_surface_priv;
204 struct pipe_mpeg12_macroblock pipe_macroblocks[num_macroblocks];
205
206 assert(dpy);
207
208 if (!context || !context->privData)
209 return XvMCBadContext;
210 if (!target_surface || !target_surface->privData)
211 return XvMCBadSurface;
212
213 if (picture_structure != XVMC_TOP_FIELD &&
214 picture_structure != XVMC_BOTTOM_FIELD &&
215 picture_structure != XVMC_FRAME_PICTURE)
216 return BadValue;
217 /* Bkwd pred equivalent to fwd (past && !future) */
218 if (future_surface && !past_surface)
219 return BadMatch;
220
221 assert(context->context_id == target_surface->context_id);
222 assert(!past_surface || context->context_id == past_surface->context_id);
223 assert(!future_surface || context->context_id == future_surface->context_id);
224
225 assert(macroblocks);
226 assert(blocks);
227
228 assert(macroblocks->context_id == context->context_id);
229 assert(blocks->context_id == context->context_id);
230
231 assert(flags == 0 || flags == XVMC_SECOND_FIELD);
232
233 target_surface_priv = target_surface->privData;
234 past_surface_priv = past_surface ? past_surface->privData : NULL;
235 future_surface_priv = future_surface ? future_surface->privData : NULL;
236
237 assert(target_surface_priv->context == context);
238 assert(!past_surface || past_surface_priv->context == context);
239 assert(!future_surface || future_surface_priv->context == context);
240
241 context_priv = context->privData;
242 vpipe = context_priv->vpipe;
243
244 t_vsfc = target_surface_priv->pipe_vsfc;
245 p_vsfc = past_surface ? past_surface_priv->pipe_vsfc : NULL;
246 f_vsfc = future_surface ? future_surface_priv->pipe_vsfc : NULL;
247
248 MacroBlocksToPipe(macroblocks, blocks, first_macroblock,
249 num_macroblocks, pipe_macroblocks);
250
251 vpipe->set_decode_target(vpipe, t_vsfc);
252 vpipe->decode_macroblocks(vpipe, p_vsfc, f_vsfc, num_macroblocks,
253 &pipe_macroblocks->base, target_surface_priv->render_fence);
254
255 return Success;
256 }
257
258 Status XvMCFlushSurface(Display *dpy, XvMCSurface *surface)
259 {
260 assert(dpy);
261
262 if (!surface)
263 return XvMCBadSurface;
264
265 return Success;
266 }
267
268 Status XvMCSyncSurface(Display *dpy, XvMCSurface *surface)
269 {
270 assert(dpy);
271
272 if (!surface)
273 return XvMCBadSurface;
274
275 return Success;
276 }
277
278 Status XvMCPutSurface(Display *dpy, XvMCSurface *surface, Drawable drawable,
279 short srcx, short srcy, unsigned short srcw, unsigned short srch,
280 short destx, short desty, unsigned short destw, unsigned short desth,
281 int flags)
282 {
283 Window root;
284 int x, y;
285 unsigned int width, height;
286 unsigned int border_width;
287 unsigned int depth;
288 struct pipe_video_context *vpipe;
289 XvMCSurfacePrivate *surface_priv;
290 XvMCContextPrivate *context_priv;
291 XvMCContext *context;
292 struct pipe_video_rect src_rect = {srcx, srcy, srcw, srch};
293 struct pipe_video_rect dst_rect = {destx, desty, destw, desth};
294
295 assert(dpy);
296
297 if (!surface || !surface->privData)
298 return XvMCBadSurface;
299
300 if (XGetGeometry(dpy, drawable, &root, &x, &y, &width, &height, &border_width, &depth) == BadDrawable)
301 return BadDrawable;
302
303 assert(flags == XVMC_TOP_FIELD || flags == XVMC_BOTTOM_FIELD || flags == XVMC_FRAME_PICTURE);
304 assert(srcx + srcw - 1 < surface->width);
305 assert(srcy + srch - 1 < surface->height);
306 /*
307 * Some apps (mplayer) hit these asserts because they call
308 * this function after the window has been resized by the WM
309 * but before they've handled the corresponding XEvent and
310 * know about the new dimensions. The output should be clipped
311 * until the app updates destw and desth.
312 */
313 /*
314 assert(destx + destw - 1 < width);
315 assert(desty + desth - 1 < height);
316 */
317
318 surface_priv = surface->privData;
319 context = surface_priv->context;
320 context_priv = context->privData;
321 vpipe = context_priv->vpipe;
322
323 if (!CreateOrResizeBackBuffer(vpipe, width, height, &context_priv->backbuffer))
324 return BadAlloc;
325
326 vpipe->render_picture(vpipe, surface_priv->pipe_vsfc, PictureToPipe(flags), &src_rect,
327 context_priv->backbuffer, &dst_rect, surface_priv->disp_fence);
328
329 vl_video_bind_drawable(vpipe, drawable);
330
331 vpipe->screen->flush_frontbuffer
332 (
333 vpipe->screen,
334 context_priv->backbuffer,
335 vpipe->priv
336 );
337
338 return Success;
339 }
340
341 Status XvMCGetSurfaceStatus(Display *dpy, XvMCSurface *surface, int *status)
342 {
343 assert(dpy);
344
345 if (!surface)
346 return XvMCBadSurface;
347
348 assert(status);
349
350 *status = 0;
351
352 return Success;
353 }
354
355 Status XvMCDestroySurface(Display *dpy, XvMCSurface *surface)
356 {
357 XvMCSurfacePrivate *surface_priv;
358
359 assert(dpy);
360
361 if (!surface || !surface->privData)
362 return XvMCBadSurface;
363
364 surface_priv = surface->privData;
365 pipe_video_surface_reference(&surface_priv->pipe_vsfc, NULL);
366 FREE(surface_priv);
367 surface->privData = NULL;
368
369 return Success;
370 }
371
372 Status XvMCHideSurface(Display *dpy, XvMCSurface *surface)
373 {
374 assert(dpy);
375
376 if (!surface || !surface->privData)
377 return XvMCBadSurface;
378
379 /* No op, only for overlaid rendering */
380
381 return Success;
382 }