Merge branch 'master' of ssh://git.freedesktop.org/git/mesa/mesa into pipe-video
[mesa.git] / src / gallium / state_trackers / xorg / xvmc / surface.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 <vl_winsys.h>
31 #include <pipe/p_video_context.h>
32 #include <pipe/p_video_state.h>
33 #include <pipe/p_state.h>
34 #include <util/u_inlines.h>
35 #include <util/u_memory.h>
36 #include <util/u_math.h>
37 #include "xvmc_private.h"
38
39 static enum pipe_mpeg12_macroblock_type TypeToPipe(int xvmc_mb_type)
40 {
41 if (xvmc_mb_type & XVMC_MB_TYPE_INTRA)
42 return PIPE_MPEG12_MACROBLOCK_TYPE_INTRA;
43 if ((xvmc_mb_type & (XVMC_MB_TYPE_MOTION_FORWARD | XVMC_MB_TYPE_MOTION_BACKWARD)) == XVMC_MB_TYPE_MOTION_FORWARD)
44 return PIPE_MPEG12_MACROBLOCK_TYPE_FWD;
45 if ((xvmc_mb_type & (XVMC_MB_TYPE_MOTION_FORWARD | XVMC_MB_TYPE_MOTION_BACKWARD)) == XVMC_MB_TYPE_MOTION_BACKWARD)
46 return PIPE_MPEG12_MACROBLOCK_TYPE_BKWD;
47 if ((xvmc_mb_type & (XVMC_MB_TYPE_MOTION_FORWARD | XVMC_MB_TYPE_MOTION_BACKWARD)) == (XVMC_MB_TYPE_MOTION_FORWARD | XVMC_MB_TYPE_MOTION_BACKWARD))
48 return PIPE_MPEG12_MACROBLOCK_TYPE_BI;
49
50 assert(0);
51
52 XVMC_MSG(XVMC_ERR, "[XvMC] Unrecognized mb type 0x%08X.\n", xvmc_mb_type);
53
54 return -1;
55 }
56
57 static enum pipe_mpeg12_picture_type PictureToPipe(int xvmc_pic)
58 {
59 switch (xvmc_pic) {
60 case XVMC_TOP_FIELD:
61 return PIPE_MPEG12_PICTURE_TYPE_FIELD_TOP;
62 case XVMC_BOTTOM_FIELD:
63 return PIPE_MPEG12_PICTURE_TYPE_FIELD_BOTTOM;
64 case XVMC_FRAME_PICTURE:
65 return PIPE_MPEG12_PICTURE_TYPE_FRAME;
66 default:
67 assert(0);
68 }
69
70 XVMC_MSG(XVMC_ERR, "[XvMC] Unrecognized picture type 0x%08X.\n", xvmc_pic);
71
72 return -1;
73 }
74
75 static enum pipe_mpeg12_motion_type MotionToPipe(int xvmc_motion_type, int xvmc_dct_type)
76 {
77 switch (xvmc_motion_type) {
78 case XVMC_PREDICTION_FRAME:
79 if (xvmc_dct_type == XVMC_DCT_TYPE_FIELD)
80 return PIPE_MPEG12_MOTION_TYPE_16x8;
81 else if (xvmc_dct_type == XVMC_DCT_TYPE_FRAME)
82 return PIPE_MPEG12_MOTION_TYPE_FRAME;
83 break;
84 case XVMC_PREDICTION_FIELD:
85 return PIPE_MPEG12_MOTION_TYPE_FIELD;
86 case XVMC_PREDICTION_DUAL_PRIME:
87 return PIPE_MPEG12_MOTION_TYPE_DUALPRIME;
88 default:
89 assert(0);
90 }
91
92 XVMC_MSG(XVMC_ERR, "[XvMC] Unrecognized motion type 0x%08X (with DCT type 0x%08X).\n", xvmc_motion_type, xvmc_dct_type);
93
94 return -1;
95 }
96
97 static bool
98 CreateOrResizeBackBuffer(struct vl_context *vctx, unsigned int width, unsigned int height,
99 struct pipe_surface **backbuffer)
100 {
101 struct pipe_video_context *vpipe;
102 struct pipe_resource template;
103 struct pipe_resource *tex;
104
105 assert(vctx);
106
107 vpipe = vctx->vpipe;
108
109 if (*backbuffer) {
110 if ((*backbuffer)->width != width || (*backbuffer)->height != height)
111 pipe_surface_reference(backbuffer, NULL);
112 else
113 return true;
114 }
115
116 memset(&template, 0, sizeof(struct pipe_resource));
117 template.target = PIPE_TEXTURE_2D;
118 template.format = vctx->vscreen->format;
119 template.last_level = 0;
120 template.width0 = width;
121 template.height0 = height;
122 template.depth0 = 1;
123 template.usage = PIPE_USAGE_DEFAULT;
124 template.bind = PIPE_BIND_RENDER_TARGET;
125 template.flags = 0;
126
127 tex = vpipe->screen->resource_create(vpipe->screen, &template);
128 if (!tex)
129 return false;
130
131 *backbuffer = vpipe->screen->get_tex_surface(vpipe->screen, tex, 0, 0, 0,
132 PIPE_BIND_RENDER_TARGET | PIPE_BIND_BLIT_SOURCE);
133 pipe_resource_reference(&tex, NULL);
134
135 if (!*backbuffer)
136 return false;
137
138 /* Clear the backbuffer in case the video doesn't cover the whole window */
139 /* FIXME: Need to clear every time a frame moves and leaves dirty rects */
140 vpipe->surface_fill(vpipe, *backbuffer, 0, 0, width, height, 0);
141
142 return true;
143 }
144
145 static void
146 MacroBlocksToPipe(struct pipe_screen *screen,
147 const XvMCMacroBlockArray *xvmc_macroblocks,
148 const XvMCBlockArray *xvmc_blocks,
149 unsigned int first_macroblock,
150 unsigned int num_macroblocks,
151 struct pipe_mpeg12_macroblock *pipe_macroblocks)
152 {
153 unsigned int i, j, k, l;
154 XvMCMacroBlock *xvmc_mb;
155
156 assert(xvmc_macroblocks);
157 assert(xvmc_blocks);
158 assert(pipe_macroblocks);
159 assert(num_macroblocks);
160
161 xvmc_mb = xvmc_macroblocks->macro_blocks + first_macroblock;
162
163 for (i = 0; i < num_macroblocks; ++i) {
164 pipe_macroblocks->base.codec = PIPE_VIDEO_CODEC_MPEG12;
165 pipe_macroblocks->mbx = xvmc_mb->x;
166 pipe_macroblocks->mby = xvmc_mb->y;
167 pipe_macroblocks->mb_type = TypeToPipe(xvmc_mb->macroblock_type);
168 if (pipe_macroblocks->mb_type != PIPE_MPEG12_MACROBLOCK_TYPE_INTRA)
169 pipe_macroblocks->mo_type = MotionToPipe(xvmc_mb->motion_type, xvmc_mb->dct_type);
170 /* Get rid of Valgrind 'undefined' warnings */
171 else
172 pipe_macroblocks->mo_type = -1;
173 pipe_macroblocks->dct_type = xvmc_mb->dct_type == XVMC_DCT_TYPE_FIELD ?
174 PIPE_MPEG12_DCT_TYPE_FIELD : PIPE_MPEG12_DCT_TYPE_FRAME;
175
176 for (j = 0; j < 2; ++j)
177 for (k = 0; k < 2; ++k)
178 for (l = 0; l < 2; ++l)
179 pipe_macroblocks->pmv[j][k][l] = xvmc_mb->PMV[j][k][l];
180
181 pipe_macroblocks->cbp = xvmc_mb->coded_block_pattern;
182 pipe_macroblocks->blocks = xvmc_blocks->blocks + xvmc_mb->index * BLOCK_SIZE_SAMPLES;
183
184 ++pipe_macroblocks;
185 ++xvmc_mb;
186 }
187 }
188
189 PUBLIC
190 Status XvMCCreateSurface(Display *dpy, XvMCContext *context, XvMCSurface *surface)
191 {
192 XvMCContextPrivate *context_priv;
193 struct pipe_video_context *vpipe;
194 XvMCSurfacePrivate *surface_priv;
195 struct pipe_resource template;
196 struct pipe_resource *vsfc_tex;
197 struct pipe_surface *vsfc;
198
199 XVMC_MSG(XVMC_TRACE, "[XvMC] Creating surface %p.\n", surface);
200
201 assert(dpy);
202
203 if (!context)
204 return XvMCBadContext;
205 if (!surface)
206 return XvMCBadSurface;
207
208 context_priv = context->privData;
209 vpipe = context_priv->vctx->vpipe;
210
211 surface_priv = CALLOC(1, sizeof(XvMCSurfacePrivate));
212 if (!surface_priv)
213 return BadAlloc;
214
215 memset(&template, 0, sizeof(struct pipe_resource));
216 template.target = PIPE_TEXTURE_2D;
217 template.format = (enum pipe_format)vpipe->get_param(vpipe, PIPE_CAP_DECODE_TARGET_PREFERRED_FORMAT);
218 template.last_level = 0;
219 if (vpipe->is_format_supported(vpipe, template.format,
220 PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET,
221 PIPE_TEXTURE_GEOM_NON_POWER_OF_TWO)) {
222 template.width0 = context->width;
223 template.height0 = context->height;
224 }
225 else {
226 assert(vpipe->is_format_supported(vpipe, template.format,
227 PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET,
228 PIPE_TEXTURE_GEOM_NON_SQUARE));
229 template.width0 = util_next_power_of_two(context->width);
230 template.height0 = util_next_power_of_two(context->height);
231 }
232 template.depth0 = 1;
233 template.usage = PIPE_USAGE_DEFAULT;
234 template.bind = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET;
235 template.flags = 0;
236 vsfc_tex = vpipe->screen->resource_create(vpipe->screen, &template);
237 if (!vsfc_tex) {
238 FREE(surface_priv);
239 return BadAlloc;
240 }
241
242 vsfc = vpipe->screen->get_tex_surface(vpipe->screen, vsfc_tex, 0, 0, 0,
243 PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET);
244 pipe_resource_reference(&vsfc_tex, NULL);
245 if (!vsfc) {
246 FREE(surface_priv);
247 return BadAlloc;
248 }
249
250 surface_priv->pipe_vsfc = vsfc;
251 surface_priv->context = context;
252
253 surface->surface_id = XAllocID(dpy);
254 surface->context_id = context->context_id;
255 surface->surface_type_id = context->surface_type_id;
256 surface->width = context->width;
257 surface->height = context->height;
258 surface->privData = surface_priv;
259
260 SyncHandle();
261
262 XVMC_MSG(XVMC_TRACE, "[XvMC] Surface %p created.\n", surface);
263
264 return Success;
265 }
266
267 PUBLIC
268 Status XvMCRenderSurface(Display *dpy, XvMCContext *context, unsigned int picture_structure,
269 XvMCSurface *target_surface, XvMCSurface *past_surface, XvMCSurface *future_surface,
270 unsigned int flags, unsigned int num_macroblocks, unsigned int first_macroblock,
271 XvMCMacroBlockArray *macroblocks, XvMCBlockArray *blocks
272 )
273 {
274 struct pipe_video_context *vpipe;
275 struct pipe_surface *t_vsfc;
276 struct pipe_surface *p_vsfc;
277 struct pipe_surface *f_vsfc;
278 XvMCContextPrivate *context_priv;
279 XvMCSurfacePrivate *target_surface_priv;
280 XvMCSurfacePrivate *past_surface_priv;
281 XvMCSurfacePrivate *future_surface_priv;
282 struct pipe_mpeg12_macroblock pipe_macroblocks[num_macroblocks];
283 unsigned int i;
284
285 XVMC_MSG(XVMC_TRACE, "[XvMC] Rendering to surface %p.\n", target_surface);
286
287 assert(dpy);
288
289 if (!context || !context->privData)
290 return XvMCBadContext;
291 if (!target_surface || !target_surface->privData)
292 return XvMCBadSurface;
293
294 if (picture_structure != XVMC_TOP_FIELD &&
295 picture_structure != XVMC_BOTTOM_FIELD &&
296 picture_structure != XVMC_FRAME_PICTURE)
297 return BadValue;
298 /* Bkwd pred equivalent to fwd (past && !future) */
299 if (future_surface && !past_surface)
300 return BadMatch;
301
302 assert(context->context_id == target_surface->context_id);
303 assert(!past_surface || context->context_id == past_surface->context_id);
304 assert(!future_surface || context->context_id == future_surface->context_id);
305
306 assert(macroblocks);
307 assert(blocks);
308
309 assert(macroblocks->context_id == context->context_id);
310 assert(blocks->context_id == context->context_id);
311
312 assert(flags == 0 || flags == XVMC_SECOND_FIELD);
313
314 target_surface_priv = target_surface->privData;
315 past_surface_priv = past_surface ? past_surface->privData : NULL;
316 future_surface_priv = future_surface ? future_surface->privData : NULL;
317
318 assert(target_surface_priv->context == context);
319 assert(!past_surface || past_surface_priv->context == context);
320 assert(!future_surface || future_surface_priv->context == context);
321
322 context_priv = context->privData;
323 vpipe = context_priv->vctx->vpipe;
324
325 t_vsfc = target_surface_priv->pipe_vsfc;
326 p_vsfc = past_surface ? past_surface_priv->pipe_vsfc : NULL;
327 f_vsfc = future_surface ? future_surface_priv->pipe_vsfc : NULL;
328
329 MacroBlocksToPipe(vpipe->screen, macroblocks, blocks, first_macroblock,
330 num_macroblocks, pipe_macroblocks);
331
332 vpipe->set_decode_target(vpipe, t_vsfc);
333 vpipe->decode_macroblocks(vpipe, p_vsfc, f_vsfc, num_macroblocks,
334 &pipe_macroblocks->base, target_surface_priv->render_fence);
335
336 XVMC_MSG(XVMC_TRACE, "[XvMC] Submitted surface %p for rendering.\n", target_surface);
337
338 return Success;
339 }
340
341 PUBLIC
342 Status XvMCFlushSurface(Display *dpy, XvMCSurface *surface)
343 {
344 assert(dpy);
345
346 if (!surface)
347 return XvMCBadSurface;
348
349 return Success;
350 }
351
352 PUBLIC
353 Status XvMCSyncSurface(Display *dpy, XvMCSurface *surface)
354 {
355 assert(dpy);
356
357 if (!surface)
358 return XvMCBadSurface;
359
360 return Success;
361 }
362
363 PUBLIC
364 Status XvMCPutSurface(Display *dpy, XvMCSurface *surface, Drawable drawable,
365 short srcx, short srcy, unsigned short srcw, unsigned short srch,
366 short destx, short desty, unsigned short destw, unsigned short desth,
367 int flags)
368 {
369 Window root;
370 int x, y;
371 unsigned int width, height;
372 unsigned int border_width;
373 unsigned int depth;
374 struct pipe_video_context *vpipe;
375 XvMCSurfacePrivate *surface_priv;
376 XvMCContextPrivate *context_priv;
377 XvMCSubpicturePrivate *subpicture_priv;
378 XvMCContext *context;
379 struct pipe_video_rect src_rect = {srcx, srcy, srcw, srch};
380 struct pipe_video_rect dst_rect = {destx, desty, destw, desth};
381
382 XVMC_MSG(XVMC_TRACE, "[XvMC] Displaying surface %p.\n", surface);
383
384 assert(dpy);
385
386 if (!surface || !surface->privData)
387 return XvMCBadSurface;
388
389 if (XGetGeometry(dpy, drawable, &root, &x, &y, &width, &height, &border_width, &depth) == BadDrawable)
390 return BadDrawable;
391
392 assert(flags == XVMC_TOP_FIELD || flags == XVMC_BOTTOM_FIELD || flags == XVMC_FRAME_PICTURE);
393 assert(srcx + srcw - 1 < surface->width);
394 assert(srcy + srch - 1 < surface->height);
395 /*
396 * Some apps (mplayer) hit these asserts because they call
397 * this function after the window has been resized by the WM
398 * but before they've handled the corresponding XEvent and
399 * know about the new dimensions. The output should be clipped
400 * until the app updates destw and desth.
401 */
402 /*
403 assert(destx + destw - 1 < width);
404 assert(desty + desth - 1 < height);
405 */
406
407 surface_priv = surface->privData;
408 context = surface_priv->context;
409 context_priv = context->privData;
410 subpicture_priv = surface_priv->subpicture ? surface_priv->subpicture->privData : NULL;
411 vpipe = context_priv->vctx->vpipe;
412
413 if (!CreateOrResizeBackBuffer(context_priv->vctx, width, height, &context_priv->backbuffer))
414 return BadAlloc;
415
416 if (subpicture_priv) {
417 struct pipe_video_rect src_rect = {surface_priv->subx, surface_priv->suby, surface_priv->subw, surface_priv->subh};
418 struct pipe_video_rect dst_rect = {surface_priv->surfx, surface_priv->surfy, surface_priv->surfw, surface_priv->surfh};
419 struct pipe_video_rect *src_rects[1] = {&src_rect};
420 struct pipe_video_rect *dst_rects[1] = {&dst_rect};
421
422 XVMC_MSG(XVMC_TRACE, "[XvMC] Surface %p has subpicture %p.\n", surface, surface_priv->subpicture);
423
424 assert(subpicture_priv->surface == surface);
425 vpipe->set_picture_layers(vpipe, &subpicture_priv->sfc, &src_rects, &dst_rects, 1);
426
427 surface_priv->subpicture = NULL;
428 subpicture_priv->surface = NULL;
429 }
430 else
431 vpipe->set_picture_layers(vpipe, NULL, NULL, NULL, 0);
432
433 vpipe->render_picture(vpipe, surface_priv->pipe_vsfc, PictureToPipe(flags), &src_rect,
434 context_priv->backbuffer, &dst_rect, surface_priv->disp_fence);
435
436 XVMC_MSG(XVMC_TRACE, "[XvMC] Submitted surface %p for display. Pushing to front buffer.\n", surface);
437
438 vl_video_bind_drawable(context_priv->vctx, drawable);
439
440 vpipe->screen->flush_frontbuffer
441 (
442 vpipe->screen,
443 context_priv->backbuffer,
444 vpipe->priv
445 );
446
447 XVMC_MSG(XVMC_TRACE, "[XvMC] Pushed surface %p to front buffer.\n", surface);
448
449 return Success;
450 }
451
452 PUBLIC
453 Status XvMCGetSurfaceStatus(Display *dpy, XvMCSurface *surface, int *status)
454 {
455 assert(dpy);
456
457 if (!surface)
458 return XvMCBadSurface;
459
460 assert(status);
461
462 *status = 0;
463
464 return Success;
465 }
466
467 PUBLIC
468 Status XvMCDestroySurface(Display *dpy, XvMCSurface *surface)
469 {
470 XvMCSurfacePrivate *surface_priv;
471
472 XVMC_MSG(XVMC_TRACE, "[XvMC] Destroying surface %p.\n", surface);
473
474 assert(dpy);
475
476 if (!surface || !surface->privData)
477 return XvMCBadSurface;
478
479 surface_priv = surface->privData;
480 pipe_surface_reference(&surface_priv->pipe_vsfc, NULL);
481 FREE(surface_priv);
482 surface->privData = NULL;
483
484 XVMC_MSG(XVMC_TRACE, "[XvMC] Surface %p destroyed.\n", surface);
485
486 return Success;
487 }
488
489 PUBLIC
490 Status XvMCHideSurface(Display *dpy, XvMCSurface *surface)
491 {
492 assert(dpy);
493
494 if (!surface || !surface->privData)
495 return XvMCBadSurface;
496
497 /* No op, only for overlaid rendering */
498
499 return Success;
500 }