19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL - Simple DirectMedia Layer
39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Copyright (C) 1997-2012 Sam Lantinga
49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is free software; you can redistribute it and/or
69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    modify it under the terms of the GNU Lesser General Public
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License as published by the Free Software Foundation; either
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    version 2.1 of the License, or (at your option) any later version.
99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is distributed in the hope that it will be useful,
119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    but WITHOUT ANY WARRANTY; without even the implied warranty of
129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Lesser General Public License for more details.
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    You should have received a copy of the GNU Lesser General Public
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License along with this library; if not, write to the Free Software
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Sam Lantinga
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    slouken@libsdl.org
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h"
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* This is the XFree86 Xv extension implementation of YUV video overlays */
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_VIDEO_DRIVER_X11_XV
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <X11/Xlib.h>
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef NO_SHARED_MEMORY
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sys/ipc.h>
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sys/shm.h>
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <X11/extensions/XShm.h>
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../Xext/extensions/Xvlib.h"
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_x11yuv_c.h"
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_yuvfuncs.h"
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define XFREE86_REFRESH_HACK
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef XFREE86_REFRESH_HACK
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_x11image_c.h"
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Workaround when pitch != width */
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define PITCH_WORKAROUND
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Workaround intel i810 video overlay waiting with failing until the
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   first Xv[Shm]PutImage call <sigh> */
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define INTEL_XV_BADALLOC_WORKAROUND
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Fix for the NVidia GeForce 2 - use the last available adaptor */
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*#define USE_LAST_ADAPTOR*/  /* Apparently the NVidia drivers are fixed */
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* The functions used to manipulate software video overlays */
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic struct private_yuvhwfuncs x11_yuvfuncs = {
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	X11_LockYUVOverlay,
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	X11_UnlockYUVOverlay,
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	X11_DisplayYUVOverlay,
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	X11_FreeYUVOverlay
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstruct private_yuvhwdata {
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int port;
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef NO_SHARED_MEMORY
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int yuv_use_mitshm;
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	XShmSegmentInfo yuvshm;
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_NAME(XvImage) *image;
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int (*X_handler)(Display *, XErrorEvent *) = NULL;
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef NO_SHARED_MEMORY
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Shared memory error handler routine */
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int shm_error;
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int shm_errhandler(Display *d, XErrorEvent *e)
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( e->error_code == BadAccess ) {
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        	shm_error = True;
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        	return(0);
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        } else
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(X_handler(d,e));
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* !NO_SHARED_MEMORY */
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int xv_error;
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int xv_errhandler(Display *d, XErrorEvent *e)
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( e->error_code == BadMatch ) {
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        	xv_error = True;
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        	return(0);
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        } else
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(X_handler(d,e));
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef INTEL_XV_BADALLOC_WORKAROUND
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int intel_errhandler(Display *d, XErrorEvent *e)
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( e->error_code == BadAlloc ) {
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        	xv_error = True;
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        	return(0);
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        } else
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(X_handler(d,e));
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void X11_ClearYUVOverlay(SDL_Overlay *overlay)
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int x,y;
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	switch (overlay->format)
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	case SDL_YV12_OVERLAY:
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	case SDL_IYUV_OVERLAY:
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		for (y = 0; y < overlay->h; y++)
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			memset(overlay->pixels[0] + y * overlay->pitches[0],
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				0, overlay->w);
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		for (y = 0; y < (overlay->h / 2); y++)
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			memset(overlay->pixels[1] + y * overlay->pitches[1],
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				-128, overlay->w / 2);
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			memset(overlay->pixels[2] + y * overlay->pitches[2],
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				-128, overlay->w / 2);
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	case SDL_YUY2_OVERLAY:
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	case SDL_YVYU_OVERLAY:
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		for (y = 0; y < overlay->h; y++)
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			for (x = 0; x < overlay->w; x += 2)
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				Uint8 *pixel_pair = overlay->pixels[0] +
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					y * overlay->pitches[0] + x * 2;
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				pixel_pair[0] = 0;
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				pixel_pair[1] = -128;
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				pixel_pair[2] = 0;
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				pixel_pair[3] = -128;
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	case SDL_UYVY_OVERLAY:
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		for (y = 0; y < overlay->h; y++)
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			for (x = 0; x < overlay->w; x += 2)
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				Uint8 *pixel_pair = overlay->pixels[0] +
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					y * overlay->pitches[0] + x * 2;
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				pixel_pair[0] = -128;
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				pixel_pair[1] = 0;
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				pixel_pair[2] = -128;
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				pixel_pair[3] = 0;
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallSDL_Overlay *X11_CreateYUVOverlay(_THIS, int width, int height, Uint32 format, SDL_Surface *display)
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Overlay *overlay;
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	struct private_yuvhwdata *hwdata;
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int xv_port;
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	unsigned int i, j, k;
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	unsigned int adaptors;
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_NAME(XvAdaptorInfo) *ainfo;
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int bpp;
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef NO_SHARED_MEMORY
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	XShmSegmentInfo *yuvshm;
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef INTEL_XV_BADALLOC_WORKAROUND
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int intel_adapter = False;
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Look for the XVideo extension with a valid port for this format */
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	xv_port = -1;
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( (Success == SDL_NAME(XvQueryExtension)(GFX_Display, &j, &j, &j, &j, &j)) &&
1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	     (Success == SDL_NAME(XvQueryAdaptors)(GFX_Display,
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	                                 RootWindow(GFX_Display, SDL_Screen),
1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	                                 &adaptors, &ainfo)) ) {
1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef USE_LAST_ADAPTOR
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		for ( i=0; i < adaptors; ++i )
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		for ( i=0; (i < adaptors) && (xv_port == -1); ++i )
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* USE_LAST_ADAPTOR */
1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* Check to see if the visual can be used */
1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( BUGGY_XFREE86(<=, 4001) ) {
1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				int visual_ok = 0;
1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				for ( j=0; j<ainfo[i].num_formats; ++j ) {
1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					if ( ainfo[i].formats[j].visual_id ==
1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall							SDL_Visual->visualid ) {
1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						visual_ok = 1;
1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						break;
1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					}
1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( ! visual_ok ) {
1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					continue;
2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef INTEL_XV_BADALLOC_WORKAROUND
2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( !strcmp(ainfo[i].name, "Intel(R) Video Overla"))
2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				intel_adapter = True;
2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			else
2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				intel_adapter = False;
2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( (ainfo[i].type & XvInputMask) &&
2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			     (ainfo[i].type & XvImageMask) ) {
2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				int num_formats;
2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_NAME(XvImageFormatValues) *formats;
2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				formats = SDL_NAME(XvListImageFormats)(GFX_Display,
2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				              ainfo[i].base_id, &num_formats);
2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef USE_LAST_ADAPTOR
2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				for ( j=0; j < num_formats; ++j )
2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				for ( j=0; (j < num_formats) && (xv_port == -1); ++j )
2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* USE_LAST_ADAPTOR */
2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				{
2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					if ( (Uint32)formats[j].id == format ) {
2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						for ( k=0; k < ainfo[i].num_ports; ++k ) {
2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall							if ( Success == SDL_NAME(XvGrabPort)(GFX_Display, ainfo[i].base_id+k, CurrentTime) ) {
2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall								xv_port = ainfo[i].base_id+k;
2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall								break;
2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall							}
2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						}
2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					}
2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( formats ) {
2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					XFree(formats);
2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_NAME(XvFreeAdaptorInfo)(ainfo);
2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Precalculate the bpp for the pitch workaround below */
2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	switch (format) {
2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    /* Add any other cases we need to support... */
2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case SDL_YUY2_OVERLAY:
2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case SDL_UYVY_OVERLAY:
2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case SDL_YVYU_OVERLAY:
2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		bpp = 2;
2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    default:
2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		bpp = 1;
2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if 0
2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /*
2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     * !!! FIXME:
2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     * "Here are some diffs for X11 and yuv.  Note that the last part 2nd
2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     *  diff should probably be a new call to XvQueryAdaptorFree with ainfo
2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     *  and the number of adaptors, instead of the loop through like I did."
2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     *
2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     *  ACHTUNG: This is broken! It looks like XvFreeAdaptorInfo does this
2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     *  for you, so we end up with a double-free. I need to look at this
2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     *  more closely...  --ryan.
2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     */
2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 	for ( i=0; i < adaptors; ++i ) {
2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 	  if (ainfo[i].name != NULL) Xfree(ainfo[i].name);
2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 	  if (ainfo[i].formats != NULL) Xfree(ainfo[i].formats);
2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   	}
2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 	Xfree(ainfo);
2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( xv_port == -1 ) {
2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("No available video ports for requested format");
2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(NULL);
2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Enable auto-painting of the overlay colorkey */
2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		static const char *attr[] = { "XV_AUTOPAINT_COLORKEY", "XV_AUTOPAINT_COLOURKEY" };
2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		unsigned int i;
2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_NAME(XvSelectPortNotify)(GFX_Display, xv_port, True);
2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		X_handler = XSetErrorHandler(xv_errhandler);
2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		for ( i=0; i < sizeof(attr)/(sizeof attr[0]); ++i ) {
2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			Atom a;
2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			xv_error = False;
2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			a = XInternAtom(GFX_Display, attr[i], True);
2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( a != None ) {
2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     				SDL_NAME(XvSetPortAttribute)(GFX_Display, xv_port, a, 1);
2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				XSync(GFX_Display, True);
2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( ! xv_error ) {
2899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					break;
2909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
2919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
2929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		XSetErrorHandler(X_handler);
2949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_NAME(XvSelectPortNotify)(GFX_Display, xv_port, False);
2959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Create the overlay structure */
2989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	overlay = (SDL_Overlay *)SDL_malloc(sizeof *overlay);
2999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( overlay == NULL ) {
3009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_NAME(XvUngrabPort)(GFX_Display, xv_port, CurrentTime);
3019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_OutOfMemory();
3029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(NULL);
3039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memset(overlay, 0, (sizeof *overlay));
3059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Fill in the basic members */
3079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	overlay->format = format;
3089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	overlay->w = width;
3099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	overlay->h = height;
3109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set up the YUV surface function structure */
3129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	overlay->hwfuncs = &x11_yuvfuncs;
3139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	overlay->hw_overlay = 1;
3149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Create the pixel data and lookup tables */
3169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	hwdata = (struct private_yuvhwdata *)SDL_malloc(sizeof *hwdata);
3179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	overlay->hwdata = hwdata;
3189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( hwdata == NULL ) {
3199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_NAME(XvUngrabPort)(GFX_Display, xv_port, CurrentTime);
3209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_OutOfMemory();
3219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_FreeYUVOverlay(overlay);
3229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(NULL);
3239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	hwdata->port = xv_port;
3259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef NO_SHARED_MEMORY
3269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	yuvshm = &hwdata->yuvshm;
3279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memset(yuvshm, 0, sizeof(*yuvshm));
3289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	hwdata->image = SDL_NAME(XvShmCreateImage)(GFX_Display, xv_port, format,
3299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						   0, width, height, yuvshm);
3309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef PITCH_WORKAROUND
3319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( hwdata->image != NULL && hwdata->image->pitches[0] != (width*bpp) ) {
3329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Ajust overlay width according to pitch */
3339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		width = hwdata->image->pitches[0] / bpp;
3349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		XFree(hwdata->image);
3359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		hwdata->image = SDL_NAME(XvShmCreateImage)(GFX_Display, xv_port, format,
3369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall							   0, width, height, yuvshm);
3379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* PITCH_WORKAROUND */
3399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	hwdata->yuv_use_mitshm = (hwdata->image != NULL);
3409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( hwdata->yuv_use_mitshm ) {
3419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		yuvshm->shmid = shmget(IPC_PRIVATE, hwdata->image->data_size,
3429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				       IPC_CREAT | 0777);
3439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( yuvshm->shmid >= 0 ) {
3449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			yuvshm->shmaddr = (char *)shmat(yuvshm->shmid, 0, 0);
3459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			yuvshm->readOnly = False;
3469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( yuvshm->shmaddr != (char *)-1 ) {
3479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				shm_error = False;
3489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				X_handler = XSetErrorHandler(shm_errhandler);
3499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				XShmAttach(GFX_Display, yuvshm);
3509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				XSync(GFX_Display, True);
3519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				XSetErrorHandler(X_handler);
3529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( shm_error )
3539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					shmdt(yuvshm->shmaddr);
3549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			} else {
3559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				shm_error = True;
3569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
3579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			shmctl(yuvshm->shmid, IPC_RMID, NULL);
3589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else {
3599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			shm_error = True;
3609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( shm_error ) {
3629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			XFree(hwdata->image);
3639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			hwdata->yuv_use_mitshm = 0;
3649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else {
3659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			hwdata->image->data = yuvshm->shmaddr;
3669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( !hwdata->yuv_use_mitshm )
3699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* NO_SHARED_MEMORY */
3709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
3719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		hwdata->image = SDL_NAME(XvCreateImage)(GFX_Display, xv_port, format,
3729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall							0, width, height);
3739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef PITCH_WORKAROUND
3759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( hwdata->image != NULL && hwdata->image->pitches[0] != (width*bpp) ) {
3769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* Ajust overlay width according to pitch */
3779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			XFree(hwdata->image);
3789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			width = hwdata->image->pitches[0] / bpp;
3799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			hwdata->image = SDL_NAME(XvCreateImage)(GFX_Display, xv_port, format,
3809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall								0, width, height);
3819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* PITCH_WORKAROUND */
3839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( hwdata->image == NULL ) {
3849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_SetError("Couldn't create XVideo image");
3859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_FreeYUVOverlay(overlay);
3869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return(NULL);
3879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		hwdata->image->data = SDL_malloc(hwdata->image->data_size);
3899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( hwdata->image->data == NULL ) {
3909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_OutOfMemory();
3919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_FreeYUVOverlay(overlay);
3929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return(NULL);
3939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Find the pitch and offset values for the overlay */
3979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	overlay->planes = hwdata->image->num_planes;
3989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	overlay->pitches = (Uint16 *)SDL_malloc(overlay->planes * sizeof(Uint16));
3999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	overlay->pixels = (Uint8 **)SDL_malloc(overlay->planes * sizeof(Uint8 *));
4009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( !overlay->pitches || !overlay->pixels ) {
4019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_OutOfMemory();
4029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_FreeYUVOverlay(overlay);
4039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(NULL);
4049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for ( i=0; i<overlay->planes; ++i ) {
4069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		overlay->pitches[i] = hwdata->image->pitches[i];
4079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		overlay->pixels[i] = (Uint8 *)hwdata->image->data +
4089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		                              hwdata->image->offsets[i];
4099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef XFREE86_REFRESH_HACK
4129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Work around an XFree86 X server bug (?)
4139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   We can't perform normal updates in windows that have video
4149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   being output to them.  See SDL_x11image.c for more details.
4159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 */
4169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	X11_DisableAutoRefresh(this);
4179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
4189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef INTEL_XV_BADALLOC_WORKAROUND
4209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* HACK, GRRR sometimes (i810) creating the overlay succeeds, but the
4219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   first call to XvShm[Put]Image to a mapped window fails with:
4229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   "BadAlloc (insufficient resources for operation)". This happens with
4239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   certain formats when the XvImage is too large to the i810's liking.
4249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   We work around this by doing a test XvShm[Put]Image with a black
4269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   Xv image, this may cause some flashing, so only do this check if we
4279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   are running on an intel Xv-adapter. */
4289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (intel_adapter)
4299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
4309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		xv_error = False;
4319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		X_handler = XSetErrorHandler(intel_errhandler);
4329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		X11_ClearYUVOverlay(overlay);
4349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* We set the destination height and width to 1 pixel to avoid
4369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		   putting a large black rectangle over the screen, thus
4379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		   strongly reducing possible flashing. */
4389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef NO_SHARED_MEMORY
4399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( hwdata->yuv_use_mitshm ) {
4409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_NAME(XvShmPutImage)(GFX_Display, hwdata->port,
4419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_Window, SDL_GC,
4429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				hwdata->image,
4439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				0, 0, overlay->w, overlay->h,
4449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				0, 0, 1, 1, False);
4459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
4469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		else
4479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
4489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{
4499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_NAME(XvPutImage)(GFX_Display, hwdata->port,
4509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_Window, SDL_GC,
4519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				hwdata->image,
4529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				0, 0, overlay->w, overlay->h,
4539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				0, 0, 1, 1);
4549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
4559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		XSync(GFX_Display, False);
4569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		XSetErrorHandler(X_handler);
4579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (xv_error)
4599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{
4609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			X11_FreeYUVOverlay(this, overlay);
4619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return NULL;
4629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
4639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Repair the (1 pixel worth of) damage we've just done */
4649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		X11_RefreshDisplay(this);
4659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
4679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* We're all done.. */
4699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(overlay);
4709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint X11_LockYUVOverlay(_THIS, SDL_Overlay *overlay)
4739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(0);
4759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid X11_UnlockYUVOverlay(_THIS, SDL_Overlay *overlay)
4789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return;
4809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint X11_DisplayYUVOverlay(_THIS, SDL_Overlay *overlay, SDL_Rect *src, SDL_Rect *dst)
4839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	struct private_yuvhwdata *hwdata;
4859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	hwdata = overlay->hwdata;
4879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef NO_SHARED_MEMORY
4899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( hwdata->yuv_use_mitshm ) {
4909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_NAME(XvShmPutImage)(GFX_Display, hwdata->port, SDL_Window, SDL_GC,
4919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	              hwdata->image,
4929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		      src->x, src->y, src->w, src->h,
4939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		      dst->x, dst->y, dst->w, dst->h, False);
4949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	else
4969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
4979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
4989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_NAME(XvPutImage)(GFX_Display, hwdata->port, SDL_Window, SDL_GC,
4999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				     hwdata->image,
5009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		                     src->x, src->y, src->w, src->h,
5019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		                     dst->x, dst->y, dst->w, dst->h);
5029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	XSync(GFX_Display, False);
5049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(0);
5059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
5069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid X11_FreeYUVOverlay(_THIS, SDL_Overlay *overlay)
5089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
5099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	struct private_yuvhwdata *hwdata;
5109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	hwdata = overlay->hwdata;
5129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( hwdata ) {
5139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_NAME(XvUngrabPort)(GFX_Display, hwdata->port, CurrentTime);
5149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef NO_SHARED_MEMORY
5159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( hwdata->yuv_use_mitshm ) {
5169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			XShmDetach(GFX_Display, &hwdata->yuvshm);
5179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			shmdt(hwdata->yuvshm.shmaddr);
5189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
5199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
5209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( hwdata->image ) {
5219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			XFree(hwdata->image);
5229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
5239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_free(hwdata);
5249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( overlay->pitches ) {
5269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_free(overlay->pitches);
5279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		overlay->pitches = NULL;
5289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( overlay->pixels ) {
5309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_free(overlay->pixels);
5319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		overlay->pixels = NULL;
5329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef XFREE86_REFRESH_HACK
5349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	X11_EnableAutoRefresh(this);
5359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
5369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
5379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* SDL_VIDEO_DRIVER_X11_XV */
539