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