intel_xvmc.c 30.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 * Copyright © 2007 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * Authors:
 *    Zhenyu Wang <zhenyu.z.wang@intel.com>
 *
 */
27
#include "intel_xvmc.h"
28 29 30 31 32
#include <xcb/xcb.h>
#include <xcb/xcb_aux.h>
#include <xcb/dri2.h>
#include <X11/Xlib-xcb.h>
#include <X11/extensions/dri2tokens.h>
33

34
/* global */
35
struct _intel_xvmc_driver *xvmc_driver = NULL;
36 37 38 39

/* Lookup tables to speed common calculations for coded_block_pattern */
/* each block is ((8*8) * sizeof(short)) */
unsigned int mb_bytes_420[] = {
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
	0,			/* 0 */
	128,			/* 1 */
	128,			/* 10 */
	256,			/* 11 */
	128,			/* 100 */
	256,			/* 101 */
	256,			/* 110 */
	384,			/* 111 */
	128,			/* 1000 */
	256,			/* 1001 */
	256,			/* 1010 */
	384,			/* 1011 */
	256,			/* 1100 */
	384,			/* 1101 */
	384,			/* 1110 */
	512,			/* 1111 */
	128,			/* 10000 */
	256,			/* 10001 */
	256,			/* 10010 */
	384,			/* 10011 */
	256,			/* 10100 */
	384,			/* 10101 */
	384,			/* 10110 */
	512,			/* 10111 */
	256,			/* 11000 */
	384,			/* 11001 */
	384,			/* 11010 */
	512,			/* 11011 */
	384,			/* 11100 */
	512,			/* 11101 */
	512,			/* 11110 */
	640,			/* 11111 */
	128,			/* 100000 */
	256,			/* 100001 */
	256,			/* 100010 */
	384,			/* 100011 */
	256,			/* 100100 */
	384,			/* 100101 */
	384,			/* 100110 */
	512,			/* 100111 */
	256,			/* 101000 */
	384,			/* 101001 */
	384,			/* 101010 */
	512,			/* 101011 */
	384,			/* 101100 */
	512,			/* 101101 */
	512,			/* 101110 */
	640,			/* 101111 */
	256,			/* 110000 */
	384,			/* 110001 */
	384,			/* 110010 */
	512,			/* 110011 */
	384,			/* 110100 */
	512,			/* 110101 */
	512,			/* 110110 */
	640,			/* 110111 */
	384,			/* 111000 */
	512,			/* 111001 */
	512,			/* 111010 */
	640,			/* 111011 */
	512,			/* 111100 */
	640,			/* 111101 */
	640,			/* 111110 */
	768			/* 111111 */
104 105
};

106 107 108 109
int DEBUG;

static void intel_xvmc_debug_init(void)
{
110 111
	if (getenv("INTEL_XVMC_DEBUG"))
		DEBUG = 1;
112 113
}

114 115
void LOCK_HARDWARE(drm_context_t ctx)
{
116
	char __ret = 0;
117

118 119
	PPTHREAD_MUTEX_LOCK();
	assert(!xvmc_driver->locked);
120

121
	xvmc_driver->locked = 1;
122 123 124 125
}

void UNLOCK_HARDWARE(drm_context_t ctx)
{
126 127 128
	xvmc_driver->locked = 0;

	PPTHREAD_MUTEX_UNLOCK();
129 130
}

131
static intel_xvmc_context_ptr intel_xvmc_new_context(Display * dpy)
132
{
133
	intel_xvmc_context_ptr ret;
134

135 136 137
	ret = (intel_xvmc_context_ptr) calloc(1, sizeof(intel_xvmc_context_t));
	if (!ret)
		return NULL;
138

139 140 141 142 143 144
	if (!xvmc_driver->ctx_list)
		ret->next = NULL;
	else
		ret->next = xvmc_driver->ctx_list;
	xvmc_driver->ctx_list = ret;
	xvmc_driver->num_ctx++;
145

146
	return ret;
147 148 149 150 151

}

static void intel_xvmc_free_context(XID id)
{
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
	intel_xvmc_context_ptr p = xvmc_driver->ctx_list;
	intel_xvmc_context_ptr pre = p;

	while (p) {
		if (p->context && p->context->context_id == id) {
			if (p == xvmc_driver->ctx_list)
				xvmc_driver->ctx_list = p->next;
			else
				pre->next = p->next;
			break;
		}
		pre = p;
		p = p->next;
	}

	if (p) {
		free(p);
		xvmc_driver->num_ctx--;
170 171 172 173 174
	}
}

intel_xvmc_context_ptr intel_xvmc_find_context(XID id)
{
175 176 177 178 179 180 181 182
	intel_xvmc_context_ptr p = xvmc_driver->ctx_list;

	while (p) {
		if (p->context && p->context->context_id == id)
			return p;
		p = p->next;
	}
	return NULL;
183 184
}

185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
static int
dri2_connect(Display *display)
{
	xcb_dri2_query_version_cookie_t query_version_cookie;
	xcb_dri2_query_version_reply_t *query_version_reply;
	xcb_dri2_connect_cookie_t connect_cookie;
	xcb_dri2_connect_reply_t *connect_reply;
	xcb_dri2_authenticate_cookie_t auth_cookie;
	xcb_dri2_authenticate_reply_t *auth_reply;
	xcb_screen_t *root;
	xcb_connection_t *c = XGetXCBConnection(display);
	drm_magic_t magic;
	const xcb_query_extension_reply_t *dri2_reply;
	char *device_name;
	int len;

	root = xcb_aux_get_screen(c, DefaultScreen(display));

	dri2_reply = xcb_get_extension_data(c, &xcb_dri2_id);

	if (!dri2_reply) {
		XVMC_ERR("DRI2 required");
		return BadValue;
	}

	/* Query the extension and make our first use of it at the same time. */
	query_version_cookie = xcb_dri2_query_version(c, 1, 0);
	connect_cookie = xcb_dri2_connect(c, root->root, DRI2DriverDRI);

	query_version_reply =
		xcb_dri2_query_version_reply(c, query_version_cookie, NULL);
	connect_reply = xcb_dri2_connect_reply(c, connect_cookie, NULL);

	if (!query_version_reply) {
		XVMC_ERR("DRI2 required");
		return BadValue;
	}
	free(query_version_reply);

	len = xcb_dri2_connect_device_name_length(connect_reply);
	device_name = malloc(len + 1);
	if (!device_name) {
		XVMC_ERR("malloc failure");
		return BadAlloc;
	}
	strncpy(device_name, xcb_dri2_connect_device_name(connect_reply), len);
	device_name[len] = 0;
	xvmc_driver->fd = open(device_name, O_RDWR);
	free(device_name);
	free(connect_reply);
	if (xvmc_driver->fd < 0) {
		XVMC_ERR("Failed to open drm device: %s\n", strerror(errno));
		return BadValue;
	}

	if (drmGetMagic(xvmc_driver->fd, &magic)) {
		XVMC_ERR("Failed to get magic\n");
		return BadValue;
	}

	auth_cookie = xcb_dri2_authenticate(c, root->root, magic);
	auth_reply = xcb_dri2_authenticate_reply(c, auth_cookie, NULL);
	if (!auth_reply) {
		XVMC_ERR("Failed to authenticate magic %d\n", magic);
		return BadValue;
	}
	free(auth_reply);

	return Success;
}

256 257 258 259 260 261 262 263 264 265 266
/*
* Function: XvMCCreateContext
* Description: Create a XvMC context for the given surface parameters.
* Arguments:
*   display - Connection to the X server.
*   port - XvPortID to use as avertised by the X connection.
*   surface_type_id - Unique identifier for the Surface type.
*   width - Width of the surfaces.
*   height - Height of the surfaces.
*   flags - one or more of the following
*      XVMC_DIRECT - A direct rendered context is requested.
267
*
268 269 270 271
* Notes: surface_type_id and width/height parameters must match those
*        returned by XvMCListSurfaceTypes.
* Returns: Status
*/
272 273 274
_X_EXPORT Status XvMCCreateContext(Display * display, XvPortID port,
				   int surface_type_id, int width, int height,
				   int flags, XvMCContext * context)
275
{
276 277
	Status ret;
	CARD32 *priv_data = NULL;
278
	struct intel_xvmc_hw_context *comm;
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
	int major, minor;
	int error_base;
	int event_base;
	int priv_count;
	int isCapable;
	int screen = DefaultScreen(display);
	intel_xvmc_context_ptr intel_ctx;
	int fd;
	char *driverName = NULL, *deviceName = NULL;

	/* Verify Obvious things first */
	if (!display || !context)
		return BadValue;

	if (!(flags & XVMC_DIRECT)) {
		XVMC_ERR("Indirect Rendering not supported! Using Direct.");
Zhenyu Wang's avatar
Zhenyu Wang committed
295
		return BadValue;
296 297
	}

298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
	intel_xvmc_debug_init();

	/*
	   Width, Height, and flags are checked against surface_type_id
	   and port for validity inside the X server, no need to check
	   here.
	 */
	context->surface_type_id = surface_type_id;
	context->width = (unsigned short)((width + 15) & ~15);
	context->height = (unsigned short)((height + 15) & ~15);
	context->flags = flags;
	context->port = port;

	if (!XvMCQueryExtension(display, &event_base, &error_base)) {
		XVMC_ERR("XvMCExtension is not available!");
		return BadValue;
	}
315

316 317 318 319 320 321
	ret = XvMCQueryVersion(display, &major, &minor);
	if (ret) {
		XVMC_ERR
		    ("XvMCQueryVersion Failed, unable to determine protocol version.");
		return ret;
	}
322

323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
	/* XXX: major and minor could be checked in future for XvMC
	 * protocol capability (i.e H.264/AVC decode available)
	 */

	/*
	   Pass control to the X server to create a drm_context_t for us and
	   validate the with/height and flags.
	 */
	if ((ret =
	     _xvmc_create_context(display, context, &priv_count, &priv_data))) {
		XVMC_ERR("Unable to create XvMC Context.");
		return ret;
	}
	XVMC_DBG("new context %d created\n", (int)context->context_id);

338
	comm = (struct intel_xvmc_hw_context *)priv_data;
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378

	if (xvmc_driver == NULL || xvmc_driver->type != comm->type) {
		switch (comm->type) {
		case XVMC_I915_MPEG2_MC:
			xvmc_driver = &i915_xvmc_mc_driver;
			break;
		case XVMC_I965_MPEG2_MC:
			xvmc_driver = &i965_xvmc_mc_driver;
			break;
		case XVMC_I965_MPEG2_VLD:
			xvmc_driver = &xvmc_vld_driver;
			break;
		case XVMC_I945_MPEG2_VLD:
		default:
			XVMC_ERR("unimplemented xvmc type %d", comm->type);
			XFree(priv_data);
			priv_data = NULL;
			return BadValue;
		}
	}

	if (xvmc_driver == NULL || xvmc_driver->type != comm->type) {
		XVMC_ERR("fail to load xvmc driver for type %d\n", comm->type);
		return BadValue;
	}

	XVMC_INFO("decoder type is %s", intel_xvmc_decoder_string(comm->type));

	/* assign local ctx info */
	intel_ctx = intel_xvmc_new_context(display);
	if (!intel_ctx) {
		XVMC_ERR("Intel XvMC context create fail\n");
		return BadAlloc;
	}
	intel_ctx->context = context;

	/* check DRI2 */
	ret = Success;
	xvmc_driver->fd = -1;

379
	ret = dri2_connect(display);
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
	if (ret != Success) {
		XFree(priv_data);
		context->privData = NULL;
		if (xvmc_driver->fd >= 0)
			close(xvmc_driver->fd);
		xvmc_driver = NULL;
		return ret;
	}

	if ((xvmc_driver->bufmgr =
	     intel_bufmgr_gem_init(xvmc_driver->fd, 1024 * 64)) == NULL) {
		XVMC_ERR("Can't init bufmgr\n");
		return BadAlloc;
	}
	drm_intel_bufmgr_gem_enable_reuse(xvmc_driver->bufmgr);

	/* call driver hook.
	 * driver hook should free priv_data after return if success.*/
	ret =
	    (xvmc_driver->create_context) (display, context, priv_count,
					   priv_data);
	if (ret) {
		XVMC_ERR("driver create context failed\n");
		XFree(priv_data);
		context->privData = NULL;
		xvmc_driver = NULL;
		return ret;
	}

	pthread_mutex_init(&xvmc_driver->ctxmutex, NULL);
	intelInitBatchBuffer();
	intel_xvmc_dump_open();

	return Success;
414 415
}

416 417 418 419 420 421 422 423 424
/*
 * Function: XvMCDestroyContext
 * Description: Destorys the specified context.
 *
 * Arguments:
 *   display - Specifies the connection to the server.
 *   context - The context to be destroyed.
 *
 */
425
_X_EXPORT Status XvMCDestroyContext(Display * display, XvMCContext * context)
426
{
427 428 429 430 431 432 433 434 435 436 437
	Status ret;
	int screen;

	if (!display || !context)
		return XvMCBadContext;
	screen = DefaultScreen(display);
	ret = (xvmc_driver->destroy_context) (display, context);
	if (ret) {
		XVMC_ERR("destroy context fail\n");
		return ret;
	}
438

439
	intelFiniBatchBuffer();
Li Shao Hua's avatar
Li Shao Hua committed
440

441
	dri_bufmgr_destroy(xvmc_driver->bufmgr);
Li Shao Hua's avatar
Li Shao Hua committed
442

443
	intel_xvmc_free_context(context->context_id);
444

445 446 447 448 449
	ret = _xvmc_destroy_context(display, context);
	if (ret != Success) {
		XVMC_ERR("_xvmc_destroy_context fail\n");
		return ret;
	}
450

451 452
	if (xvmc_driver->num_ctx == 0) {
		pthread_mutex_destroy(&xvmc_driver->ctxmutex);
453

454 455 456 457 458 459 460
		if (xvmc_driver->fd >= 0)
			close(xvmc_driver->fd);

		xvmc_driver->fd = -1;
		intel_xvmc_dump_close();
	}
	return Success;
461 462
}

463 464 465
/*
 * Function: XvMCCreateSurface
 */
466 467
_X_EXPORT Status XvMCCreateSurface(Display * display, XvMCContext * context,
				   XvMCSurface * surface)
468
{
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
	Status ret;
	int priv_count;
	CARD32 *priv_data;
	intel_xvmc_surface_ptr intel_surf = NULL;

	if (!display || !context)
		return XvMCBadContext;

	if (!surface)
		return XvMCBadSurface;

	if ((ret = _xvmc_create_surface(display, context, surface,
					&priv_count, &priv_data))) {
		XVMC_ERR("Unable to create XvMCSurface.");
		return ret;
	}
485

486 487 488 489 490 491 492 493 494 495
	ret =
	    (xvmc_driver->create_surface) (display, context, surface,
					   priv_count, priv_data);
	if (ret) {
		XVMC_ERR("create surface failed\n");
		return ret;
	}

	intel_surf = surface->privData;

496 497
	intel_surf->image = XvCreateImage(display, context->port,
					  FOURCC_XVMC,
498
					  (char *) &intel_surf->gem_handle,
499 500 501 502 503 504 505 506 507
					  surface->width, surface->height);
	if (!intel_surf->image) {
		XVMC_ERR("Can't create XvImage for surface\n");
		_xvmc_destroy_surface(display, surface);
		return BadAlloc;
	}

	return Success;
}
508

509 510 511
/*
 * Function: XvMCDestroySurface
 */
512
_X_EXPORT Status XvMCDestroySurface(Display * display, XvMCSurface * surface)
513
{
514
	intel_xvmc_surface_ptr intel_surf;
515

516 517
	if (!display || !surface)
		return XvMCBadSurface;
518

519
	intel_surf = surface->privData;
520 521
	if (!intel_surf)
		return XvMCBadSurface;
522

523 524 525
	XFree(intel_surf->image);
	if (intel_surf->gc_init)
		XFreeGC(display, intel_surf->gc);
526

527
	(xvmc_driver->destroy_surface) (display, surface);
528

529
	_xvmc_destroy_surface(display, surface);
530

531
	return Success;
532 533
}

534 535 536
/*
 * Function: XvMCCreateBlocks
 */
537 538 539
_X_EXPORT Status XvMCCreateBlocks(Display * display, XvMCContext * context,
				  unsigned int num_blocks,
				  XvMCBlockArray * block)
540
{
541 542 543
	Status ret;
	if (!display || !context || !num_blocks || !block)
		return BadValue;
544

545
	memset(block, 0, sizeof(XvMCBlockArray));
546

547 548 549 550
	if (!
	    (block->blocks =
	     (short *)malloc((num_blocks << 6) * sizeof(short))))
		return BadAlloc;
551

552 553 554
	block->num_blocks = num_blocks;
	block->context_id = context->context_id;
	block->privData = NULL;
555

556
	return Success;
557 558
}

559 560 561
/*
 * Function: XvMCDestroyBlocks
 */
562
_X_EXPORT Status XvMCDestroyBlocks(Display * display, XvMCBlockArray * block)
563
{
564 565 566
	Status ret;
	if (!display || !block)
		return BadValue;
567

568 569
	if (block->blocks)
		free(block->blocks);
570

571 572 573 574
	block->context_id = 0;
	block->num_blocks = 0;
	block->blocks = NULL;
	block->privData = NULL;
575

576
	return Success;
577 578
}

579 580 581
/*
 * Function: XvMCCreateMacroBlocks
 */
582 583 584
_X_EXPORT Status XvMCCreateMacroBlocks(Display * display, XvMCContext * context,
				       unsigned int num_blocks,
				       XvMCMacroBlockArray * blocks)
585
{
586 587
	if (!display || !context || !blocks || !num_blocks)
		return BadValue;
588

589 590 591
	memset(blocks, 0, sizeof(XvMCMacroBlockArray));
	blocks->macro_blocks =
	    (XvMCMacroBlock *) malloc(num_blocks * sizeof(XvMCMacroBlock));
592

593 594
	if (!blocks->macro_blocks)
		return BadAlloc;
595

596 597 598
	blocks->num_blocks = num_blocks;
	blocks->context_id = context->context_id;
	blocks->privData = NULL;
599

600
	return Success;
601 602
}

603 604 605
/*
 * Function: XvMCDestroyMacroBlocks
 */
606 607
_X_EXPORT Status XvMCDestroyMacroBlocks(Display * display,
					XvMCMacroBlockArray * block)
608
{
609 610 611 612
	if (!display || !block)
		return BadValue;
	if (block->macro_blocks)
		free(block->macro_blocks);
613

614 615 616 617
	block->context_id = 0;
	block->num_blocks = 0;
	block->macro_blocks = NULL;
	block->privData = NULL;
618

619
	return Success;
620 621
}

622 623 624 625 626 627 628
/*
 * Function: XvMCRenderSurface
 *
 * Description: This function does the actual HWMC. Given a list of
 *  macroblock structures it dispatched the hardware commands to execute
 *  them.
 */
629 630 631 632 633 634 635 636 637 638
_X_EXPORT Status XvMCRenderSurface(Display * display, XvMCContext * context,
				   unsigned int picture_structure,
				   XvMCSurface * target_surface,
				   XvMCSurface * past_surface,
				   XvMCSurface * future_surface,
				   unsigned int flags,
				   unsigned int num_macroblocks,
				   unsigned int first_macroblock,
				   XvMCMacroBlockArray * macroblock_array,
				   XvMCBlockArray * blocks)
639
{
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
	Status ret;

	if (!display || !context) {
		XVMC_ERR("Invalid Display, Context or Target!");
		return XvMCBadContext;
	}
	if (!target_surface)
		return XvMCBadSurface;

	intel_xvmc_dump_render(context, picture_structure, target_surface,
			       past_surface, future_surface, flags,
			       num_macroblocks, first_macroblock,
			       macroblock_array, blocks);

	ret =
	    (xvmc_driver->render_surface) (display, context, picture_structure,
					   target_surface, past_surface,
					   future_surface, flags,
					   num_macroblocks, first_macroblock,
					   macroblock_array, blocks);

	if (ret) {
		XVMC_ERR("render surface fail\n");
		return ret;
	}
	return Success;
666 667
}

668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
/*
 * Function: XvMCPutSurface
 *
 * Description:
 * Arguments:
 *  display: Connection to X server
 *  surface: Surface to be displayed
 *  draw: X Drawable on which to display the surface
 *  srcx: X coordinate of the top left corner of the region to be
 *          displayed within the surface.
 *  srcy: Y coordinate of the top left corner of the region to be
 *          displayed within the surface.
 *  srcw: Width of the region to be displayed.
 *  srch: Height of the region to be displayed.
 *  destx: X cordinate of the top left corner of the destination region
 *         in the drawable coordinates.
 *  desty: Y cordinate of the top left corner of the destination region
 *         in the drawable coordinates.
 *  destw: Width of the destination region.
 *  desth: Height of the destination region.
 *  flags: One or more of the following.
 *	XVMC_TOP_FIELD - Display only the Top field of the surface.
 *	XVMC_BOTTOM_FIELD - Display only the Bottom Field of the surface.
 *	XVMC_FRAME_PICTURE - Display both fields or frame.
 */
693 694 695 696 697 698
_X_EXPORT Status XvMCPutSurface(Display * display, XvMCSurface * surface,
				Drawable draw, short srcx, short srcy,
				unsigned short srcw, unsigned short srch,
				short destx, short desty,
				unsigned short destw, unsigned short desth,
				int flags)
699
{
700 701 702 703 704 705 706 707 708
	Status ret = Success;
	XvMCContext *context;
	intel_xvmc_context_ptr intel_ctx;
	intel_xvmc_surface_ptr intel_surf;

	if (!display || !surface)
		return XvMCBadSurface;

	intel_ctx = intel_xvmc_find_context(surface->context_id);
709
	intel_surf = surface->privData;
710 711 712 713 714 715 716 717 718 719 720 721 722 723
	if (!intel_ctx || !intel_surf)
		return XvMCBadSurface;
	context = intel_ctx->context;

	if (intel_surf->gc_init == FALSE) {
		intel_surf->gc = XCreateGC(display, draw, 0, NULL);
		intel_surf->gc_init = TRUE;
	} else if (draw != intel_surf->last_draw) {
		XFreeGC(display, intel_surf->gc);
		intel_surf->gc = XCreateGC(display, draw, 0, NULL);
	}
	intel_surf->last_draw = draw;
	ret = (xvmc_driver->put_surface) (display, surface, draw, srcx, srcy,
					  srcw, srch, destx, desty, destw,
724 725
					  desth, flags,
					  &intel_surf->gem_handle);
726 727 728 729 730 731 732
	if (ret) {
		XVMC_ERR("put surface fail\n");
		return ret;
	}
	ret = XvPutImage(display, context->port, draw, intel_surf->gc,
			 intel_surf->image, srcx, srcy, srcw, srch, destx,
			 desty, destw, desth);
Zhenyu Wang's avatar
Zhenyu Wang committed
733
	return ret;
734 735
}

736 737 738 739 740 741
/*
 * Function: XvMCSyncSurface
 * Arguments:
 *   display - Connection to the X server
 *   surface - The surface to synchronize
 */
742
_X_EXPORT Status XvMCSyncSurface(Display * display, XvMCSurface * surface)
743
{
744 745
	Status ret;
	int stat = 0;
746

747 748
	if (!display || !surface)
		return XvMCBadSurface;
Zhenyu Wang's avatar
Zhenyu Wang committed
749

750 751 752
	do {
		ret = XvMCGetSurfaceStatus(display, surface, &stat);
	} while (!ret && (stat & XVMC_RENDERING));
753

754
	return ret;
755 756
}

757 758 759 760 761 762 763 764 765 766
/*
 * Function: XvMCFlushSurface
 * Description:
 *   This function commits pending rendering requests to ensure that they
 *   wll be completed in a finite amount of time.
 * Arguments:
 *   display - Connection to X server
 *   surface - Surface to flush
 * Returns: Status
 */
767
_X_EXPORT Status XvMCFlushSurface(Display * display, XvMCSurface * surface)
768
{
769 770 771
	if (!display || !surface)
		return XvMCBadSurface;
	return Success;
772 773
}

774 775 776 777 778 779 780 781 782 783 784 785
/*
 * Function: XvMCGetSurfaceStatus
 * Description:
 * Arguments:
 *  display: connection to X server
 *  surface: The surface to query
 *  stat: One of the Following
 *    XVMC_RENDERING - The last XvMCRenderSurface command has not
 *                     completed.
 *    XVMC_DISPLAYING - The surface is currently being displayed or a
 *                     display is pending.
 */
786 787
_X_EXPORT Status XvMCGetSurfaceStatus(Display * display, XvMCSurface * surface,
				      int *stat)
788
{
789
	Status ret;
790

791 792
	if (!display || !surface || !stat)
		return XvMCBadSurface;
793

794 795 796 797 798
	ret = (xvmc_driver->get_surface_status) (display, surface, stat);
	if (ret) {
		XVMC_ERR("get surface status fail\n");
		return ret;
	}
799

800
	return Success;
801 802
}

803 804 805 806 807 808 809 810 811
/*
 * Function: XvMCHideSurface
 * Description: Stops the display of a surface.
 * Arguments:
 *   display - Connection to the X server.
 *   surface - surface to be hidden.
 *
 * Returns: Status
 */
812
_X_EXPORT Status XvMCHideSurface(Display * display, XvMCSurface * surface)
813
{
814 815
	int stat = 0;
	Status ret;
816

817 818
	if (!display || !surface)
		return XvMCBadSurface;
819

820
	XvMCSyncSurface(display, surface);
821

822 823 824 825 826 827
	/*
	   Get the status of the surface, if it is not currently displayed
	   we don't need to worry about it.
	 */
	if ((ret = XvMCGetSurfaceStatus(display, surface, &stat)) != Success)
		return ret;
828

829 830
	if (!(stat & XVMC_DISPLAYING))
		return Success;
831

832 833 834
	/* FIXME: */
	XVMC_ERR("XvMCHideSurface not implemented!\n");
	return BadValue;
835 836
}

837 838 839 840 841 842 843 844 845 846 847 848 849 850
/*
 * Function: XvMCCreateSubpicture
 * Description: This creates a subpicture by filling out the XvMCSubpicture
 *              structure passed to it and returning Success.
 * Arguments:
 *   display - Connection to the X server.
 *   context - The context to create the subpicture for.
 *   subpicture - Pre-allocated XvMCSubpicture structure to be filled in.
 *   width - of subpicture
 *   height - of subpicture
 *   xvimage_id - The id describing the XvImage format.
 *
 * Returns: Status
 */
851 852 853 854
_X_EXPORT Status XvMCCreateSubpicture(Display * display, XvMCContext * context,
				      XvMCSubpicture * subpicture,
				      unsigned short width,
				      unsigned short height, int xvimage_id)
855
{
856 857
	XVMC_ERR("XvMCCreateSubpicture not implemented!\n");
	return BadValue;
858 859
}

860 861 862 863 864 865 866 867 868 869 870 871
/*
 * Function: XvMCClearSubpicture
 * Description: Clear the area of the given subpicture to "color".
 *              structure passed to it and returning Success.
 * Arguments:
 *   display - Connection to the X server.
 *   subpicture - Subpicture to clear.
 *   x, y, width, height - rectangle in the subpicture to clear.
 *   color - The data to file the rectangle with.
 *
 * Returns: Status
 */
872 873 874 875
_X_EXPORT Status XvMCClearSubpicture(Display * display,
				     XvMCSubpicture * subpicture, short x,
				     short y, unsigned short width,
				     unsigned short height, unsigned int color)
876
{
877 878
	XVMC_ERR("XvMCClearSubpicture not implemented!");
	return BadValue;
879 880
}

881 882 883 884 885 886 887 888 889 890 891 892 893 894 895
/*
 * Function: XvMCCompositeSubpicture
 * Description: Composite the XvImae on the subpicture. This composit uses
 *              non-premultiplied alpha. Destination alpha is utilized
 *              except for with indexed subpictures. Indexed subpictures
 *              use a simple "replace".
 * Arguments:
 *   display - Connection to the X server.
 *   subpicture - Subpicture to clear.
 *   image - the XvImage to be used as the source of the composite.
 *   srcx, srcy, width, height - The rectangle from the image to be used.
 *   dstx, dsty - location in the subpicture to composite the source.
 *
 * Returns: Status
 */
896 897 898 899 900 901
_X_EXPORT Status XvMCCompositeSubpicture(Display * display,
					 XvMCSubpicture * subpicture,
					 XvImage * image, short srcx,
					 short srcy, unsigned short width,
					 unsigned short height, short dstx,
					 short dsty)
902
{
903 904
	XVMC_ERR("XvMCCompositeSubpicture not implemented!");
	return BadValue;
905 906
}

907 908 909 910 911 912 913 914 915
/*
 * Function: XvMCDestroySubpicture
 * Description: Destroys the specified subpicture.
 * Arguments:
 *   display - Connection to the X server.
 *   subpicture - Subpicture to be destroyed.
 *
 * Returns: Status
 */
916 917
_X_EXPORT Status XvMCDestroySubpicture(Display * display,
				       XvMCSubpicture * subpicture)
918
{
919 920
	XVMC_ERR("XvMCDestroySubpicture not implemented!");
	return BadValue;
921 922
}

923 924 925 926 927 928 929 930 931 932
/*
 * Function: XvMCSetSubpicturePalette
 * Description: Set the subpictures palette
 * Arguments:
 *   display - Connection to the X server.
 *   subpicture - Subpiture to set palette for.
 *   palette - A pointer to an array holding the palette data. The array
 *     is num_palette_entries * entry_bytes in size.
 * Returns: Status
 */
933 934 935
_X_EXPORT Status XvMCSetSubpicturePalette(Display * display,
					  XvMCSubpicture * subpicture,
					  unsigned char *palette)
936
{
937 938
	XVMC_ERR("XvMCSetSubpicturePalette not implemented!");
	return BadValue;
939 940
}

941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963
/*
 * Function: XvMCBlendSubpicture
 * Description:
 *    The behavior of this function is different depending on whether
 *    or not the XVMC_BACKEND_SUBPICTURE flag is set in the XvMCSurfaceInfo.
 *    i915 only support frontend behavior.
 *
 *    XVMC_BACKEND_SUBPICTURE not set ("frontend" behavior):
 *
 *    XvMCBlendSubpicture is a no-op in this case.
 *
 * Arguments:
 *   display - Connection to the X server.
 *   subpicture - The subpicture to be blended into the video.
 *   target_surface - The surface to be displayed with the blended subpic.
 *   source_surface - Source surface prior to blending.
 *   subx, suby, subw, subh - The rectangle from the subpicture to use.
 *   surfx, surfy, surfw, surfh - The rectangle in the surface to blend
 *      blend the subpicture rectangle into. Scaling can ocure if
 *      XVMC_SUBPICTURE_INDEPENDENT_SCALING is set.
 *
 * Returns: Status
 */
964 965 966 967 968 969 970
_X_EXPORT Status XvMCBlendSubpicture(Display * display,
				     XvMCSurface * target_surface,
				     XvMCSubpicture * subpicture, short subx,
				     short suby, unsigned short subw,
				     unsigned short subh, short surfx,
				     short surfy, unsigned short surfw,
				     unsigned short surfh)
971
{
972 973
	XVMC_ERR("XvMCBlendSubpicture not implemented!");
	return BadValue;
974 975
}

976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001
/*
 * Function: XvMCBlendSubpicture2
 * Description:
 *    The behavior of this function is different depending on whether
 *    or not the XVMC_BACKEND_SUBPICTURE flag is set in the XvMCSurfaceInfo.
 *    i915 only supports frontend blending.
 *
 *    XVMC_BACKEND_SUBPICTURE not set ("frontend" behavior):
 *
 *    XvMCBlendSubpicture2 blends the source_surface and subpicture and
 *    puts it in the target_surface.  This does not effect the status of
 *    the source surface but will cause the target_surface to query
 *    XVMC_RENDERING until the blend is completed.
 *
 * Arguments:
 *   display - Connection to the X server.
 *   subpicture - The subpicture to be blended into the video.
 *   target_surface - The surface to be displayed with the blended subpic.
 *   source_surface - Source surface prior to blending.
 *   subx, suby, subw, subh - The rectangle from the subpicture to use.
 *   surfx, surfy, surfw, surfh - The rectangle in the surface to blend
 *      blend the subpicture rectangle into. Scaling can ocure if
 *      XVMC_SUBPICTURE_INDEPENDENT_SCALING is set.
 *
 * Returns: Status
 */
1002 1003 1004 1005 1006 1007 1008 1009 1010
_X_EXPORT Status XvMCBlendSubpicture2(Display * display,
				      XvMCSurface * source_surface,
				      XvMCSurface * target_surface,
				      XvMCSubpicture * subpicture,
				      short subx, short suby,
				      unsigned short subw, unsigned short subh,
				      short surfx, short surfy,
				      unsigned short surfw,
				      unsigned short surfh)
1011
{
1012 1013
	XVMC_ERR("XvMCBlendSubpicture2 not implemented!");
	return BadValue;
1014 1015
}

1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
/*
 * Function: XvMCSyncSubpicture
 * Description: This function blocks until all composite/clear requests on
 *              the subpicture have been complete.
 * Arguments:
 *   display - Connection to the X server.
 *   subpicture - The subpicture to synchronize
 *
 * Returns: Status
 */
1026 1027
_X_EXPORT Status XvMCSyncSubpicture(Display * display,
				    XvMCSubpicture * subpicture)
1028
{
1029 1030
	XVMC_ERR("XvMCSyncSubpicture not implemented!");
	return BadValue;
1031 1032
}

1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
/*
 * Function: XvMCFlushSubpicture
 * Description: This function commits pending composite/clear requests to
 *              ensure that they will be completed in a finite amount of
 *              time.
 * Arguments:
 *   display - Connection to the X server.
 *   subpicture - The subpicture whos compsiting should be flushed
 *
 * Returns: Status
 */
1044 1045
_X_EXPORT Status XvMCFlushSubpicture(Display * display,
				     XvMCSubpicture * subpicture)
1046
{
1047 1048
	XVMC_ERR("XvMCFlushSubpicture not implemented!");
	return BadValue;
1049 1050
}

1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
/*
 * Function: XvMCGetSubpictureStatus
 * Description: This function gets the current status of a subpicture
 *
 * Arguments:
 *   display - Connection to the X server.
 *   subpicture - The subpicture whos status is being queried
 *   stat - The status of the subpicture. It can be any of the following
 *          OR'd together:
 *          XVMC_RENDERING  - Last composite or clear request not completed
 *          XVMC_DISPLAYING - Suppicture currently being displayed.
 *
 * Returns: Status
 */
1065 1066
_X_EXPORT Status XvMCGetSubpictureStatus(Display * display,
					 XvMCSubpicture * subpicture, int *stat)
1067
{
1068 1069
	XVMC_ERR("XvMCGetSubpictureStatus not implemented!");
	return BadValue;
1070 1071
}

1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
/*
 * Function: XvMCQueryAttributes
 * Description: An array of XvAttributes of size "number" is returned by
 *   this function. If there are no attributes, NULL is returned and number
 *   is set to 0. The array may be freed with xfree().
 *
 * Arguments:
 *   display - Connection to the X server.
 *   context - The context whos attributes we are querying.
 *   number - The returned number of recognized atoms
 *
 * Returns:
 *  An array of XvAttributes.
 */
1086 1087
_X_EXPORT XvAttribute *XvMCQueryAttributes(Display * display,
					   XvMCContext * context, int *number)
1088
{
1089 1090 1091
	/* now XvMC has no extra attribs than Xv */
	*number = 0;
	return NULL;
1092 1093
}

1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
/*
 * Function: XvMCSetAttribute
 * Description: This function sets a context-specific attribute.
 *
 * Arguments:
 *   display - Connection to the X server.
 *   context - The context whos attributes we are querying.
 *   attribute - The X atom of the attribute to be changed.
 *   value - The new value for the attribute.
 *
 * Returns:
 *  Status
 */
1107 1108
_X_EXPORT Status XvMCSetAttribute(Display * display, XvMCContext * context,
				  Atom attribute, int value)
1109
{
1110
	return Success;
1111 1112
}

1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
/*
 * Function: XvMCGetAttribute
 * Description: This function queries a context-specific attribute and
 *   returns the value.
 *
 * Arguments:
 *   display - Connection to the X server.
 *   context - The context whos attributes we are querying.
 *   attribute - The X atom of the attribute to be queried
 *   value - The returned attribute value
 *
 * Returns:
 *  Status
 */
1127 1128
_X_EXPORT Status XvMCGetAttribute(Display * display, XvMCContext * context,
				  Atom attribute, int *value)
1129
{
1130
	return Success;
1131
}
1132

1133 1134 1135 1136 1137
_X_EXPORT Status XvMCBeginSurface(Display * display, XvMCContext * context,
				  XvMCSurface * target,
				  XvMCSurface * past,
				  XvMCSurface * future,
				  const XvMCMpegControl * control)
1138
{
1139 1140 1141 1142 1143 1144
	if (xvmc_driver->begin_surface(display, context,
				       target, past, future, control)) {
		XVMC_ERR("BeginSurface fail\n");
		return BadValue;
	}
	return Success;
1145 1146
}

1147 1148
_X_EXPORT Status XvMCLoadQMatrix(Display * display, XvMCContext * context,
				 const XvMCQMatrix * qmx)
1149
{
1150 1151 1152 1153 1154
	if (xvmc_driver->load_qmatrix(display, context, qmx)) {
		XVMC_ERR("LoadQMatrix fail\n");
		return BadValue;
	}
	return Success;
1155 1156
}

1157 1158
_X_EXPORT Status XvMCPutSlice(Display * display, XvMCContext * context,
			      char *slice, int nbytes)
1159
{
1160 1161 1162 1163 1164
	if (xvmc_driver->put_slice(display, context, slice, nbytes)) {
		XVMC_ERR("PutSlice fail\n");
		return BadValue;
	}
	return Success;
1165 1166
}

1167 1168
_X_EXPORT Status XvMCPutSlice2(Display * display, XvMCContext * context,
			       char *slice, int nbytes, int slice_code)
1169
{
1170 1171 1172 1173 1174 1175
	if (xvmc_driver->put_slice2
	    (display, context, slice, nbytes, slice_code)) {
		XVMC_ERR("PutSlice2 fail\n");
		return BadValue;
	}
	return Success;
1176
}