surface.c revision dcf8ee7d6ac89bb2a9d608618a51604a3c78fe96
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 31#include <X11/Xlibint.h> 32 33#include "pipe/p_video_decoder.h" 34#include "pipe/p_video_state.h" 35#include "pipe/p_state.h" 36 37#include "util/u_inlines.h" 38#include "util/u_memory.h" 39#include "util/u_math.h" 40 41#include "vl_winsys.h" 42 43#include "xvmc_private.h" 44 45static void 46MacroBlocksToPipe(XvMCContextPrivate *context, 47 XvMCSurfacePrivate *surface, 48 unsigned int xvmc_picture_structure, 49 const XvMCMacroBlock *xvmc_mb, 50 const XvMCBlockArray *xvmc_blocks, 51 struct pipe_mpeg12_macroblock *mb, 52 unsigned int num_macroblocks) 53{ 54 unsigned int i, j, k; 55 56 assert(xvmc_mb); 57 assert(xvmc_blocks); 58 assert(num_macroblocks); 59 60 for (; num_macroblocks > 0; --num_macroblocks) { 61 mb->base.codec = PIPE_VIDEO_CODEC_MPEG12; 62 mb->x = xvmc_mb->x; 63 mb->y = xvmc_mb->y; 64 mb->macroblock_type = xvmc_mb->macroblock_type; 65 66 switch (xvmc_picture_structure) { 67 case XVMC_FRAME_PICTURE: 68 mb->macroblock_modes.bits.frame_motion_type = xvmc_mb->motion_type; 69 mb->macroblock_modes.bits.field_motion_type = 0; 70 break; 71 72 case XVMC_TOP_FIELD: 73 case XVMC_BOTTOM_FIELD: 74 mb->macroblock_modes.bits.frame_motion_type = 0; 75 mb->macroblock_modes.bits.field_motion_type = xvmc_mb->motion_type; 76 break; 77 78 default: 79 assert(0); 80 } 81 82 mb->macroblock_modes.bits.dct_type = xvmc_mb->dct_type; 83 mb->motion_vertical_field_select = xvmc_mb->motion_vertical_field_select; 84 85 for (i = 0; i < 2; ++i) 86 for (j = 0; j < 2; ++j) 87 for (k = 0; k < 2; ++k) 88 mb->PMV[i][j][k] = xvmc_mb->PMV[i][j][k]; 89 90 mb->coded_block_pattern = xvmc_mb->coded_block_pattern; 91 mb->blocks = xvmc_blocks->blocks + xvmc_mb->index * BLOCK_SIZE_SAMPLES; 92 mb->num_skipped_macroblocks = 0; 93 94 ++xvmc_mb; 95 ++mb; 96 } 97} 98 99static void 100GetPictureDescription(XvMCSurfacePrivate *surface, struct pipe_mpeg12_picture_desc *desc) 101{ 102 unsigned i, num_refs = 0; 103 104 assert(surface && desc); 105 106 memset(desc, 0, sizeof(*desc)); 107 desc->base.profile = PIPE_VIDEO_PROFILE_MPEG1; 108 desc->picture_structure = surface->picture_structure; 109 for (i = 0; i < 2; ++i) { 110 if (surface->ref[i]) { 111 XvMCSurfacePrivate *ref = surface->ref[i]->privData; 112 113 if (ref) 114 desc->ref[num_refs++] = ref->video_buffer; 115 } 116 } 117} 118 119static void 120RecursiveEndFrame(XvMCSurfacePrivate *surface) 121{ 122 XvMCContextPrivate *context_priv; 123 unsigned i; 124 125 assert(surface); 126 127 context_priv = surface->context->privData; 128 129 for ( i = 0; i < 2; ++i ) { 130 if (surface->ref[i]) { 131 XvMCSurface *ref = surface->ref[i]; 132 133 assert(ref); 134 135 surface->ref[i] = NULL; 136 RecursiveEndFrame(ref->privData); 137 surface->ref[i] = ref; 138 } 139 } 140 141 if (surface->picture_structure) { 142 struct pipe_mpeg12_picture_desc desc; 143 GetPictureDescription(surface, &desc); 144 surface->picture_structure = 0; 145 146 for (i = 0; i < 2; ++i) 147 surface->ref[i] = NULL; 148 149 context_priv->decoder->end_frame(context_priv->decoder, surface->video_buffer, &desc.base); 150 } 151} 152 153PUBLIC 154Status XvMCCreateSurface(Display *dpy, XvMCContext *context, XvMCSurface *surface) 155{ 156 XvMCContextPrivate *context_priv; 157 struct pipe_context *pipe; 158 XvMCSurfacePrivate *surface_priv; 159 struct pipe_video_buffer tmpl; 160 161 XVMC_MSG(XVMC_TRACE, "[XvMC] Creating surface %p.\n", surface); 162 163 assert(dpy); 164 165 if (!context) 166 return XvMCBadContext; 167 if (!surface) 168 return XvMCBadSurface; 169 170 context_priv = context->privData; 171 pipe = context_priv->pipe; 172 173 surface_priv = CALLOC(1, sizeof(XvMCSurfacePrivate)); 174 if (!surface_priv) 175 return BadAlloc; 176 177 memset(&tmpl, 0, sizeof(tmpl)); 178 tmpl.buffer_format = pipe->screen->get_video_param 179 ( 180 pipe->screen, 181 PIPE_VIDEO_PROFILE_MPEG2_MAIN, 182 PIPE_VIDEO_CAP_PREFERED_FORMAT 183 ); 184 tmpl.chroma_format = context_priv->decoder->chroma_format; 185 tmpl.width = context_priv->decoder->width; 186 tmpl.height = context_priv->decoder->height; 187 tmpl.interlaced = pipe->screen->get_video_param 188 ( 189 pipe->screen, 190 PIPE_VIDEO_PROFILE_MPEG2_MAIN, 191 PIPE_VIDEO_CAP_PREFERS_INTERLACED 192 ); 193 194 surface_priv->video_buffer = pipe->create_video_buffer(pipe, &tmpl); 195 surface_priv->context = context; 196 197 surface->surface_id = XAllocID(dpy); 198 surface->context_id = context->context_id; 199 surface->surface_type_id = context->surface_type_id; 200 surface->width = context->width; 201 surface->height = context->height; 202 surface->privData = surface_priv; 203 204 SyncHandle(); 205 206 XVMC_MSG(XVMC_TRACE, "[XvMC] Surface %p created.\n", surface); 207 208 return Success; 209} 210 211PUBLIC 212Status XvMCRenderSurface(Display *dpy, XvMCContext *context, unsigned int picture_structure, 213 XvMCSurface *target_surface, XvMCSurface *past_surface, XvMCSurface *future_surface, 214 unsigned int flags, unsigned int num_macroblocks, unsigned int first_macroblock, 215 XvMCMacroBlockArray *macroblocks, XvMCBlockArray *blocks 216) 217{ 218 struct pipe_mpeg12_macroblock mb[num_macroblocks]; 219 struct pipe_video_decoder *decoder; 220 struct pipe_mpeg12_picture_desc desc; 221 222 XvMCContextPrivate *context_priv; 223 XvMCSurfacePrivate *target_surface_priv; 224 XvMCSurfacePrivate *past_surface_priv; 225 XvMCSurfacePrivate *future_surface_priv; 226 XvMCMacroBlock *xvmc_mb; 227 228 XVMC_MSG(XVMC_TRACE, "[XvMC] Rendering to surface %p, with past %p and future %p\n", 229 target_surface, past_surface, future_surface); 230 231 assert(dpy); 232 233 if (!context || !context->privData) 234 return XvMCBadContext; 235 if (!target_surface || !target_surface->privData) 236 return XvMCBadSurface; 237 238 if (picture_structure != XVMC_TOP_FIELD && 239 picture_structure != XVMC_BOTTOM_FIELD && 240 picture_structure != XVMC_FRAME_PICTURE) 241 return BadValue; 242 /* Bkwd pred equivalent to fwd (past && !future) */ 243 if (future_surface && !past_surface) 244 return BadMatch; 245 246 assert(context->context_id == target_surface->context_id); 247 assert(!past_surface || context->context_id == past_surface->context_id); 248 assert(!future_surface || context->context_id == future_surface->context_id); 249 250 assert(macroblocks); 251 assert(blocks); 252 253 assert(macroblocks->context_id == context->context_id); 254 assert(blocks->context_id == context->context_id); 255 256 assert(flags == 0 || flags == XVMC_SECOND_FIELD); 257 258 context_priv = context->privData; 259 decoder = context_priv->decoder; 260 261 target_surface_priv = target_surface->privData; 262 past_surface_priv = past_surface ? past_surface->privData : NULL; 263 future_surface_priv = future_surface ? future_surface->privData : NULL; 264 265 assert(target_surface_priv->context == context); 266 assert(!past_surface || past_surface_priv->context == context); 267 assert(!future_surface || future_surface_priv->context == context); 268 269 // call end frame on all referenced frames 270 if (past_surface) 271 RecursiveEndFrame(past_surface->privData); 272 273 if (future_surface) 274 RecursiveEndFrame(future_surface->privData); 275 276 xvmc_mb = macroblocks->macro_blocks + first_macroblock; 277 278 /* If the surface we're rendering hasn't changed the ref frames shouldn't change. */ 279 if (target_surface_priv->picture_structure > 0 && ( 280 target_surface_priv->picture_structure != picture_structure || 281 target_surface_priv->ref[0] != past_surface || 282 target_surface_priv->ref[1] != future_surface || 283 (xvmc_mb->x == 0 && xvmc_mb->y == 0))) { 284 285 // If they change anyway we must assume that the current frame is ended 286 RecursiveEndFrame(target_surface_priv); 287 } 288 289 target_surface_priv->ref[0] = past_surface; 290 target_surface_priv->ref[1] = future_surface; 291 292 if (target_surface_priv->picture_structure) 293 GetPictureDescription(target_surface_priv, &desc); 294 else { 295 target_surface_priv->picture_structure = picture_structure; 296 GetPictureDescription(target_surface_priv, &desc); 297 decoder->begin_frame(decoder, target_surface_priv->video_buffer, &desc.base); 298 } 299 300 MacroBlocksToPipe(context_priv, target_surface_priv, picture_structure, 301 xvmc_mb, blocks, mb, num_macroblocks); 302 303 context_priv->decoder->decode_macroblock(context_priv->decoder, 304 target_surface_priv->video_buffer, 305 &desc.base, 306 &mb[0].base, num_macroblocks); 307 308 XVMC_MSG(XVMC_TRACE, "[XvMC] Submitted surface %p for rendering.\n", target_surface); 309 310 return Success; 311} 312 313PUBLIC 314Status XvMCFlushSurface(Display *dpy, XvMCSurface *surface) 315{ 316 assert(dpy); 317 318 if (!surface) 319 return XvMCBadSurface; 320 321 // don't call flush here, because this is usually 322 // called once for every slice instead of every frame 323 324 XVMC_MSG(XVMC_TRACE, "[XvMC] Flushing surface %p\n", surface); 325 326 return Success; 327} 328 329PUBLIC 330Status XvMCSyncSurface(Display *dpy, XvMCSurface *surface) 331{ 332 assert(dpy); 333 334 if (!surface) 335 return XvMCBadSurface; 336 337 XVMC_MSG(XVMC_TRACE, "[XvMC] Syncing surface %p\n", surface); 338 339 return Success; 340} 341 342PUBLIC 343Status XvMCPutSurface(Display *dpy, XvMCSurface *surface, Drawable drawable, 344 short srcx, short srcy, unsigned short srcw, unsigned short srch, 345 short destx, short desty, unsigned short destw, unsigned short desth, 346 int flags) 347{ 348 static int dump_window = -1; 349 350 struct pipe_context *pipe; 351 struct vl_compositor *compositor; 352 353 XvMCSurfacePrivate *surface_priv; 354 XvMCContextPrivate *context_priv; 355 XvMCSubpicturePrivate *subpicture_priv; 356 XvMCContext *context; 357 struct pipe_video_rect src_rect = {srcx, srcy, srcw, srch}; 358 struct pipe_video_rect dst_rect = {destx, desty, destw, desth}; 359 360 XVMC_MSG(XVMC_TRACE, "[XvMC] Displaying surface %p.\n", surface); 361 362 assert(dpy); 363 364 if (!surface || !surface->privData) 365 return XvMCBadSurface; 366 367 surface_priv = surface->privData; 368 context = surface_priv->context; 369 context_priv = context->privData; 370 371 assert(flags == XVMC_TOP_FIELD || flags == XVMC_BOTTOM_FIELD || flags == XVMC_FRAME_PICTURE); 372 assert(srcx + srcw - 1 < surface->width); 373 assert(srcy + srch - 1 < surface->height); 374 375 subpicture_priv = surface_priv->subpicture ? surface_priv->subpicture->privData : NULL; 376 pipe = context_priv->pipe; 377 compositor = &context_priv->compositor; 378 379 if (!context_priv->drawable_surface || 380 context_priv->dst_rect.x != dst_rect.x || context_priv->dst_rect.y != dst_rect.y || 381 context_priv->dst_rect.w != dst_rect.w || context_priv->dst_rect.h != dst_rect.h) { 382 383 struct pipe_surface surf_templ; 384 struct pipe_resource *tex = vl_screen_texture_from_drawable( 385 context_priv->vscreen, drawable); 386 387 pipe_surface_reference(&context_priv->drawable_surface, NULL); 388 389 memset(&surf_templ, 0, sizeof(surf_templ)); 390 surf_templ.format = tex->format; 391 surf_templ.usage = PIPE_BIND_RENDER_TARGET; 392 context_priv->drawable_surface = pipe->create_surface(pipe, tex, &surf_templ); 393 vl_compositor_reset_dirty_area(&context_priv->dirty_area); 394 context_priv->dst_rect = dst_rect; 395 } 396 397 if (!context_priv->drawable_surface) 398 return BadDrawable; 399 400 /* 401 * Some apps (mplayer) hit these asserts because they call 402 * this function after the window has been resized by the WM 403 * but before they've handled the corresponding XEvent and 404 * know about the new dimensions. The output should be clipped 405 * until the app updates destw and desth. 406 */ 407 /* 408 assert(destx + destw - 1 < drawable_surface->width); 409 assert(desty + desth - 1 < drawable_surface->height); 410 */ 411 412 RecursiveEndFrame(surface_priv); 413 414 context_priv->decoder->flush(context_priv->decoder); 415 416 vl_compositor_clear_layers(compositor); 417 vl_compositor_set_buffer_layer(compositor, 0, surface_priv->video_buffer, 418 &src_rect, NULL, VL_COMPOSITOR_WEAVE); 419 420 if (subpicture_priv) { 421 XVMC_MSG(XVMC_TRACE, "[XvMC] Surface %p has subpicture %p.\n", surface, surface_priv->subpicture); 422 423 assert(subpicture_priv->surface == surface); 424 425 if (subpicture_priv->palette) 426 vl_compositor_set_palette_layer(compositor, 1, subpicture_priv->sampler, subpicture_priv->palette, 427 &subpicture_priv->src_rect, &subpicture_priv->dst_rect, true); 428 else 429 vl_compositor_set_rgba_layer(compositor, 1, subpicture_priv->sampler, 430 &subpicture_priv->src_rect, &subpicture_priv->dst_rect); 431 432 surface_priv->subpicture = NULL; 433 subpicture_priv->surface = NULL; 434 } 435 436 // Workaround for r600g, there seems to be a bug in the fence refcounting code 437 pipe->screen->fence_reference(pipe->screen, &surface_priv->fence, NULL); 438 439 vl_compositor_render(compositor, context_priv->drawable_surface, &dst_rect, NULL, &context_priv->dirty_area); 440 441 pipe->flush(pipe, &surface_priv->fence); 442 443 XVMC_MSG(XVMC_TRACE, "[XvMC] Submitted surface %p for display. Pushing to front buffer.\n", surface); 444 445 pipe->screen->flush_frontbuffer 446 ( 447 pipe->screen, context_priv->drawable_surface->texture, 0, 0, 448 vl_screen_get_private(context_priv->vscreen) 449 ); 450 451 if(dump_window == -1) { 452 dump_window = debug_get_num_option("XVMC_DUMP", 0); 453 } 454 455 if(dump_window) { 456 static unsigned int framenum = 0; 457 char cmd[256]; 458 459 sprintf(cmd, "xwd -id %d -out xvmc_frame_%08d.xwd", (int)drawable, ++framenum); 460 if (system(cmd) != 0) 461 XVMC_MSG(XVMC_ERR, "[XvMC] Dumping surface %p failed.\n", surface); 462 } 463 464 XVMC_MSG(XVMC_TRACE, "[XvMC] Pushed surface %p to front buffer.\n", surface); 465 466 return Success; 467} 468 469PUBLIC 470Status XvMCGetSurfaceStatus(Display *dpy, XvMCSurface *surface, int *status) 471{ 472 struct pipe_context *pipe; 473 XvMCSurfacePrivate *surface_priv; 474 XvMCContextPrivate *context_priv; 475 476 assert(dpy); 477 478 if (!surface) 479 return XvMCBadSurface; 480 481 assert(status); 482 483 surface_priv = surface->privData; 484 context_priv = surface_priv->context->privData; 485 pipe = context_priv->pipe; 486 487 *status = 0; 488 489 if (surface_priv->fence) 490 if (!pipe->screen->fence_signalled(pipe->screen, surface_priv->fence)) 491 *status |= XVMC_RENDERING; 492 493 return Success; 494} 495 496PUBLIC 497Status XvMCDestroySurface(Display *dpy, XvMCSurface *surface) 498{ 499 XvMCSurfacePrivate *surface_priv; 500 XvMCContextPrivate *context_priv; 501 502 XVMC_MSG(XVMC_TRACE, "[XvMC] Destroying surface %p.\n", surface); 503 504 assert(dpy); 505 506 if (!surface || !surface->privData) 507 return XvMCBadSurface; 508 509 surface_priv = surface->privData; 510 context_priv = surface_priv->context->privData; 511 512 if (surface_priv->picture_structure) { 513 struct pipe_mpeg12_picture_desc desc; 514 GetPictureDescription(surface_priv, &desc); 515 context_priv->decoder->end_frame(context_priv->decoder, surface_priv->video_buffer, &desc.base); 516 } 517 surface_priv->video_buffer->destroy(surface_priv->video_buffer); 518 FREE(surface_priv); 519 surface->privData = NULL; 520 521 XVMC_MSG(XVMC_TRACE, "[XvMC] Surface %p destroyed.\n", surface); 522 523 return Success; 524} 525 526PUBLIC 527Status XvMCHideSurface(Display *dpy, XvMCSurface *surface) 528{ 529 assert(dpy); 530 531 if (!surface || !surface->privData) 532 return XvMCBadSurface; 533 534 /* No op, only for overlaid rendering */ 535 536 return Success; 537} 538