Hans de Goede fixed bug #495 SDL-1.2
authorSam Lantinga <slouken@libsdl.org>
Sat, 29 Dec 2007 02:23:48 +0000
branchSDL-1.2
changeset 410612bb6311fd5d
parent 4105 84882a89ca50
child 4107 4e3b250c950e
Hans de Goede fixed bug #495

When running boswars: http://www.boswars.org/ on a machine with intel
integrathed graphics it crashes when it tries to play the initial theora
splashscreen video:
X Error of failed request: BadAlloc (insufficient resources for operation)
Major opcode of failed request: 140 (XVideo)
Minor opcode of failed request: 19 ()
Serial number of failed request: 25
Current serial number in output stream: 26
boswars: xcb_xlib.c:41: xcb_xlib_lock: Assertion `!c->xlib.lock' failed.
Aborted

I recognized this problem from a few years back, when I encountered it while
working on the Xv blitter for xmame. The problem is that for some reason
creation the Xvport and XvImage succeeds, and failure (lack of resources / hw
capability?) is only indicated during the first XvPut[Shm]Image. I've written a
patch for SDL using the work around for this I developed for xmame (and which
is still used successfully in xmame after many years of usage).

I'll admit it isn't very pretty, but after investigating several possibilities
this was the best option, any other fixes would need changes to the SDL api and
abi.
src/video/x11/SDL_x11yuv.c
     1.1 --- a/src/video/x11/SDL_x11yuv.c	Fri Dec 28 22:05:17 2007 +0000
     1.2 +++ b/src/video/x11/SDL_x11yuv.c	Sat Dec 29 02:23:48 2007 +0000
     1.3 @@ -44,6 +44,10 @@
     1.4  /* Workaround when pitch != width */
     1.5  #define PITCH_WORKAROUND
     1.6  
     1.7 +/* Workaround intel i810 video overlay waiting with failing until the
     1.8 +   first Xv[Shm]PutImage call <sigh> */
     1.9 +#define INTEL_XV_BADALLOC_WORKAROUND
    1.10 +
    1.11  /* Fix for the NVidia GeForce 2 - use the last available adaptor */
    1.12  /*#define USE_LAST_ADAPTOR*/  /* Apparently the NVidia drivers are fixed */
    1.13  
    1.14 @@ -90,6 +94,69 @@
    1.15  		return(X_handler(d,e));
    1.16  }
    1.17  
    1.18 +#ifdef INTEL_XV_BADALLOC_WORKAROUND
    1.19 +static int intel_errhandler(Display *d, XErrorEvent *e)
    1.20 +{
    1.21 +        if ( e->error_code == BadAlloc ) {
    1.22 +        	xv_error = True;
    1.23 +        	return(0);
    1.24 +        } else
    1.25 +		return(X_handler(d,e));
    1.26 +}
    1.27 +
    1.28 +static void X11_ClearYUVOverlay(SDL_Overlay *overlay)
    1.29 +{
    1.30 +	int x,y;
    1.31 +	    
    1.32 +	switch (overlay->format)
    1.33 +	{
    1.34 +	case SDL_YV12_OVERLAY:
    1.35 +	case SDL_IYUV_OVERLAY:
    1.36 +		for (y = 0; y < overlay->h; y++)
    1.37 +			memset(overlay->pixels[0] + y * overlay->pitches[0],
    1.38 +				0, overlay->w);
    1.39 +		
    1.40 +		for (y = 0; y < (overlay->h / 2); y++)
    1.41 +		{
    1.42 +			memset(overlay->pixels[1] + y * overlay->pitches[1],
    1.43 +				-128, overlay->w / 2);
    1.44 +			memset(overlay->pixels[2] + y * overlay->pitches[2],
    1.45 +				-128, overlay->w / 2);
    1.46 +		}
    1.47 +		break;
    1.48 +	case SDL_YUY2_OVERLAY:
    1.49 +	case SDL_YVYU_OVERLAY:
    1.50 +		for (y = 0; y < overlay->h; y++)
    1.51 +		{
    1.52 +			for (x = 0; x < overlay->w; x += 2)
    1.53 +			{
    1.54 +				Uint8 *pixel_pair = overlay->pixels[0] +
    1.55 +					y * overlay->pitches[0] + x * 2;
    1.56 +				pixel_pair[0] = 0;
    1.57 +				pixel_pair[1] = -128;
    1.58 +				pixel_pair[2] = 0;
    1.59 +				pixel_pair[3] = -128;
    1.60 +			}
    1.61 +		}
    1.62 +		break;
    1.63 +	case SDL_UYVY_OVERLAY:
    1.64 +		for (y = 0; y < overlay->h; y++)
    1.65 +		{
    1.66 +			for (x = 0; x < overlay->w; x += 2)
    1.67 +			{
    1.68 +				Uint8 *pixel_pair = overlay->pixels[0] +
    1.69 +					y * overlay->pitches[0] + x * 2;
    1.70 +				pixel_pair[0] = -128;
    1.71 +				pixel_pair[1] = 0;
    1.72 +				pixel_pair[2] = -128;
    1.73 +				pixel_pair[3] = 0;
    1.74 +			}
    1.75 +		}
    1.76 +		break;
    1.77 +	}
    1.78 +}
    1.79 +#endif
    1.80 +
    1.81  SDL_Overlay *X11_CreateYUVOverlay(_THIS, int width, int height, Uint32 format, SDL_Surface *display)
    1.82  {
    1.83  	SDL_Overlay *overlay;
    1.84 @@ -102,6 +169,9 @@
    1.85  #ifndef NO_SHARED_MEMORY
    1.86  	XShmSegmentInfo *yuvshm;
    1.87  #endif
    1.88 +#ifdef INTEL_XV_BADALLOC_WORKAROUND
    1.89 +	int intel_adapter = False;
    1.90 +#endif
    1.91  
    1.92  	/* Look for the XVideo extension with a valid port for this format */
    1.93  	xv_port = -1;
    1.94 @@ -129,6 +199,12 @@
    1.95  					continue;
    1.96  				}
    1.97  			}
    1.98 +#ifdef INTEL_XV_BADALLOC_WORKAROUND
    1.99 +			if ( !strcmp(ainfo[i].name, "Intel(R) Video Overla"))
   1.100 +				intel_adapter = True;
   1.101 +			else
   1.102 +				intel_adapter = False;
   1.103 +#endif
   1.104  			if ( (ainfo[i].type & XvInputMask) &&
   1.105  			     (ainfo[i].type & XvImageMask) ) {
   1.106  				int num_formats;
   1.107 @@ -340,6 +416,55 @@
   1.108  	X11_DisableAutoRefresh(this);
   1.109  #endif
   1.110  
   1.111 +#ifdef INTEL_XV_BADALLOC_WORKAROUND
   1.112 +	/* HACK, GRRR sometimes (i810) creating the overlay succeeds, but the
   1.113 +	   first call to XvShm[Put]Image to a mapped window fails with:
   1.114 +	   "BadAlloc (insufficient resources for operation)". This happens with
   1.115 +	   certain formats when the XvImage is too large to the i810's liking.
   1.116 +
   1.117 +	   We work around this by doing a test XvShm[Put]Image with a black
   1.118 +	   Xv image, this may cause some flashing, so only do this check if we
   1.119 +	   are running on an intel Xv-adapter. */
   1.120 +	if (intel_adapter)
   1.121 +	{
   1.122 +		xv_error = False;
   1.123 +		X_handler = XSetErrorHandler(intel_errhandler);
   1.124 +		
   1.125 +		X11_ClearYUVOverlay(overlay);
   1.126 +
   1.127 +		/* We set the destination height and width to 1 pixel to avoid
   1.128 +		   putting a large black rectangle over the screen, thus
   1.129 +		   strongly reducing possible flashing. */
   1.130 +#ifndef NO_SHARED_MEMORY
   1.131 +		if ( hwdata->yuv_use_mitshm ) {
   1.132 +			SDL_NAME(XvShmPutImage)(GFX_Display, hwdata->port,
   1.133 +				SDL_Window, SDL_GC,
   1.134 +				hwdata->image,
   1.135 +				0, 0, overlay->w, overlay->h,
   1.136 +				0, 0, 1, 1, False);
   1.137 +		}
   1.138 +		else
   1.139 +#endif
   1.140 +		{
   1.141 +			SDL_NAME(XvPutImage)(GFX_Display, hwdata->port,
   1.142 +				SDL_Window, SDL_GC,
   1.143 +				hwdata->image,
   1.144 +				0, 0, overlay->w, overlay->h,
   1.145 +				0, 0, 1, 1);
   1.146 +		}
   1.147 +		XSync(GFX_Display, False);
   1.148 +		XSetErrorHandler(X_handler);
   1.149 +
   1.150 +		if (xv_error)
   1.151 +		{
   1.152 +			X11_FreeYUVOverlay(this, overlay);
   1.153 +			return NULL;
   1.154 +		}
   1.155 +		/* Repair the (1 pixel worth of) damage we've just done */
   1.156 +		X11_RefreshDisplay(this);
   1.157 +	}
   1.158 +#endif
   1.159 +
   1.160  	/* We're all done.. */
   1.161  	return(overlay);
   1.162  }