2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2009 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "SDL_config.h"
24 /* This is the XFree86 Xv extension implementation of YUV video overlays */
26 #if SDL_VIDEO_DRIVER_X11_XV
29 #ifndef NO_SHARED_MEMORY
32 #include <X11/extensions/XShm.h>
34 #include "../Xext/extensions/Xvlib.h"
36 #include "SDL_x11yuv_c.h"
37 #include "../SDL_yuvfuncs.h"
39 #define XFREE86_REFRESH_HACK
40 #ifdef XFREE86_REFRESH_HACK
41 #include "SDL_x11image_c.h"
44 /* Workaround when pitch != width */
45 #define PITCH_WORKAROUND
47 /* Workaround intel i810 video overlay waiting with failing until the
48 first Xv[Shm]PutImage call <sigh> */
49 #define INTEL_XV_BADALLOC_WORKAROUND
51 /* Fix for the NVidia GeForce 2 - use the last available adaptor */
52 /*#define USE_LAST_ADAPTOR*/ /* Apparently the NVidia drivers are fixed */
54 /* The functions used to manipulate software video overlays */
55 static struct private_yuvhwfuncs x11_yuvfuncs = {
58 X11_DisplayYUVOverlay,
62 struct private_yuvhwdata {
64 #ifndef NO_SHARED_MEMORY
66 XShmSegmentInfo yuvshm;
68 SDL_NAME(XvImage) *image;
72 static int (*X_handler)(Display *, XErrorEvent *) = NULL;
74 #ifndef NO_SHARED_MEMORY
75 /* Shared memory error handler routine */
77 static int shm_errhandler(Display *d, XErrorEvent *e)
79 if ( e->error_code == BadAccess ) {
83 return(X_handler(d,e));
85 #endif /* !NO_SHARED_MEMORY */
88 static int xv_errhandler(Display *d, XErrorEvent *e)
90 if ( e->error_code == BadMatch ) {
94 return(X_handler(d,e));
97 #ifdef INTEL_XV_BADALLOC_WORKAROUND
98 static int intel_errhandler(Display *d, XErrorEvent *e)
100 if ( e->error_code == BadAlloc ) {
104 return(X_handler(d,e));
107 static void X11_ClearYUVOverlay(SDL_Overlay *overlay)
111 switch (overlay->format)
113 case SDL_YV12_OVERLAY:
114 case SDL_IYUV_OVERLAY:
115 for (y = 0; y < overlay->h; y++)
116 memset(overlay->pixels[0] + y * overlay->pitches[0],
119 for (y = 0; y < (overlay->h / 2); y++)
121 memset(overlay->pixels[1] + y * overlay->pitches[1],
122 -128, overlay->w / 2);
123 memset(overlay->pixels[2] + y * overlay->pitches[2],
124 -128, overlay->w / 2);
127 case SDL_YUY2_OVERLAY:
128 case SDL_YVYU_OVERLAY:
129 for (y = 0; y < overlay->h; y++)
131 for (x = 0; x < overlay->w; x += 2)
133 Uint8 *pixel_pair = overlay->pixels[0] +
134 y * overlay->pitches[0] + x * 2;
136 pixel_pair[1] = -128;
138 pixel_pair[3] = -128;
142 case SDL_UYVY_OVERLAY:
143 for (y = 0; y < overlay->h; y++)
145 for (x = 0; x < overlay->w; x += 2)
147 Uint8 *pixel_pair = overlay->pixels[0] +
148 y * overlay->pitches[0] + x * 2;
149 pixel_pair[0] = -128;
151 pixel_pair[2] = -128;
160 SDL_Overlay *X11_CreateYUVOverlay(_THIS, int width, int height, Uint32 format, SDL_Surface *display)
162 SDL_Overlay *overlay;
163 struct private_yuvhwdata *hwdata;
165 unsigned int i, j, k;
166 unsigned int adaptors;
167 SDL_NAME(XvAdaptorInfo) *ainfo;
169 #ifndef NO_SHARED_MEMORY
170 XShmSegmentInfo *yuvshm;
172 #ifdef INTEL_XV_BADALLOC_WORKAROUND
173 int intel_adapter = False;
176 /* Look for the XVideo extension with a valid port for this format */
178 if ( (Success == SDL_NAME(XvQueryExtension)(GFX_Display, &j, &j, &j, &j, &j)) &&
179 (Success == SDL_NAME(XvQueryAdaptors)(GFX_Display,
180 RootWindow(GFX_Display, SDL_Screen),
181 &adaptors, &ainfo)) ) {
182 #ifdef USE_LAST_ADAPTOR
183 for ( i=0; i < adaptors; ++i )
185 for ( i=0; (i < adaptors) && (xv_port == -1); ++i )
186 #endif /* USE_LAST_ADAPTOR */
188 /* Check to see if the visual can be used */
189 if ( BUGGY_XFREE86(<=, 4001) ) {
191 for ( j=0; j<ainfo[i].num_formats; ++j ) {
192 if ( ainfo[i].formats[j].visual_id ==
193 SDL_Visual->visualid ) {
202 #ifdef INTEL_XV_BADALLOC_WORKAROUND
203 if ( !strcmp(ainfo[i].name, "Intel(R) Video Overla"))
204 intel_adapter = True;
206 intel_adapter = False;
208 if ( (ainfo[i].type & XvInputMask) &&
209 (ainfo[i].type & XvImageMask) ) {
211 SDL_NAME(XvImageFormatValues) *formats;
212 formats = SDL_NAME(XvListImageFormats)(GFX_Display,
213 ainfo[i].base_id, &num_formats);
214 #ifdef USE_LAST_ADAPTOR
215 for ( j=0; j < num_formats; ++j )
217 for ( j=0; (j < num_formats) && (xv_port == -1); ++j )
218 #endif /* USE_LAST_ADAPTOR */
220 if ( (Uint32)formats[j].id == format ) {
221 for ( k=0; k < ainfo[i].num_ports; ++k ) {
222 if ( Success == SDL_NAME(XvGrabPort)(GFX_Display, ainfo[i].base_id+k, CurrentTime) ) {
223 xv_port = ainfo[i].base_id+k;
234 SDL_NAME(XvFreeAdaptorInfo)(ainfo);
237 /* Precalculate the bpp for the pitch workaround below */
239 /* Add any other cases we need to support... */
240 case SDL_YUY2_OVERLAY:
241 case SDL_UYVY_OVERLAY:
242 case SDL_YVYU_OVERLAY:
253 * "Here are some diffs for X11 and yuv. Note that the last part 2nd
254 * diff should probably be a new call to XvQueryAdaptorFree with ainfo
255 * and the number of adaptors, instead of the loop through like I did."
257 * ACHTUNG: This is broken! It looks like XvFreeAdaptorInfo does this
258 * for you, so we end up with a double-free. I need to look at this
259 * more closely... --ryan.
261 for ( i=0; i < adaptors; ++i ) {
262 if (ainfo[i].name != NULL) Xfree(ainfo[i].name);
263 if (ainfo[i].formats != NULL) Xfree(ainfo[i].formats);
268 if ( xv_port == -1 ) {
269 SDL_SetError("No available video ports for requested format");
273 /* Enable auto-painting of the overlay colorkey */
275 static const char *attr[] = { "XV_AUTOPAINT_COLORKEY", "XV_AUTOPAINT_COLOURKEY" };
278 SDL_NAME(XvSelectPortNotify)(GFX_Display, xv_port, True);
279 X_handler = XSetErrorHandler(xv_errhandler);
280 for ( i=0; i < sizeof(attr)/(sizeof attr[0]); ++i ) {
284 a = XInternAtom(GFX_Display, attr[i], True);
286 SDL_NAME(XvSetPortAttribute)(GFX_Display, xv_port, a, 1);
287 XSync(GFX_Display, True);
293 XSetErrorHandler(X_handler);
294 SDL_NAME(XvSelectPortNotify)(GFX_Display, xv_port, False);
297 /* Create the overlay structure */
298 overlay = (SDL_Overlay *)SDL_malloc(sizeof *overlay);
299 if ( overlay == NULL ) {
300 SDL_NAME(XvUngrabPort)(GFX_Display, xv_port, CurrentTime);
304 SDL_memset(overlay, 0, (sizeof *overlay));
306 /* Fill in the basic members */
307 overlay->format = format;
311 /* Set up the YUV surface function structure */
312 overlay->hwfuncs = &x11_yuvfuncs;
313 overlay->hw_overlay = 1;
315 /* Create the pixel data and lookup tables */
316 hwdata = (struct private_yuvhwdata *)SDL_malloc(sizeof *hwdata);
317 overlay->hwdata = hwdata;
318 if ( hwdata == NULL ) {
319 SDL_NAME(XvUngrabPort)(GFX_Display, xv_port, CurrentTime);
321 SDL_FreeYUVOverlay(overlay);
324 hwdata->port = xv_port;
325 #ifndef NO_SHARED_MEMORY
326 yuvshm = &hwdata->yuvshm;
327 SDL_memset(yuvshm, 0, sizeof(*yuvshm));
328 hwdata->image = SDL_NAME(XvShmCreateImage)(GFX_Display, xv_port, format,
329 0, width, height, yuvshm);
330 #ifdef PITCH_WORKAROUND
331 if ( hwdata->image != NULL && hwdata->image->pitches[0] != (width*bpp) ) {
332 /* Ajust overlay width according to pitch */
333 XFree(hwdata->image);
334 width = hwdata->image->pitches[0] / bpp;
335 hwdata->image = SDL_NAME(XvShmCreateImage)(GFX_Display, xv_port, format,
336 0, width, height, yuvshm);
338 #endif /* PITCH_WORKAROUND */
339 hwdata->yuv_use_mitshm = (hwdata->image != NULL);
340 if ( hwdata->yuv_use_mitshm ) {
341 yuvshm->shmid = shmget(IPC_PRIVATE, hwdata->image->data_size,
343 if ( yuvshm->shmid >= 0 ) {
344 yuvshm->shmaddr = (char *)shmat(yuvshm->shmid, 0, 0);
345 yuvshm->readOnly = False;
346 if ( yuvshm->shmaddr != (char *)-1 ) {
348 X_handler = XSetErrorHandler(shm_errhandler);
349 XShmAttach(GFX_Display, yuvshm);
350 XSync(GFX_Display, True);
351 XSetErrorHandler(X_handler);
353 shmdt(yuvshm->shmaddr);
357 shmctl(yuvshm->shmid, IPC_RMID, NULL);
362 XFree(hwdata->image);
363 hwdata->yuv_use_mitshm = 0;
365 hwdata->image->data = yuvshm->shmaddr;
368 if ( !hwdata->yuv_use_mitshm )
369 #endif /* NO_SHARED_MEMORY */
371 hwdata->image = SDL_NAME(XvCreateImage)(GFX_Display, xv_port, format,
374 #ifdef PITCH_WORKAROUND
375 if ( hwdata->image != NULL && hwdata->image->pitches[0] != (width*bpp) ) {
376 /* Ajust overlay width according to pitch */
377 XFree(hwdata->image);
378 width = hwdata->image->pitches[0] / bpp;
379 hwdata->image = SDL_NAME(XvCreateImage)(GFX_Display, xv_port, format,
382 #endif /* PITCH_WORKAROUND */
383 if ( hwdata->image == NULL ) {
384 SDL_SetError("Couldn't create XVideo image");
385 SDL_FreeYUVOverlay(overlay);
388 hwdata->image->data = SDL_malloc(hwdata->image->data_size);
389 if ( hwdata->image->data == NULL ) {
391 SDL_FreeYUVOverlay(overlay);
396 /* Find the pitch and offset values for the overlay */
397 overlay->planes = hwdata->image->num_planes;
398 overlay->pitches = (Uint16 *)SDL_malloc(overlay->planes * sizeof(Uint16));
399 overlay->pixels = (Uint8 **)SDL_malloc(overlay->planes * sizeof(Uint8 *));
400 if ( !overlay->pitches || !overlay->pixels ) {
402 SDL_FreeYUVOverlay(overlay);
405 for ( i=0; i<overlay->planes; ++i ) {
406 overlay->pitches[i] = hwdata->image->pitches[i];
407 overlay->pixels[i] = (Uint8 *)hwdata->image->data +
408 hwdata->image->offsets[i];
411 #ifdef XFREE86_REFRESH_HACK
412 /* Work around an XFree86 X server bug (?)
413 We can't perform normal updates in windows that have video
414 being output to them. See SDL_x11image.c for more details.
416 X11_DisableAutoRefresh(this);
419 #ifdef INTEL_XV_BADALLOC_WORKAROUND
420 /* HACK, GRRR sometimes (i810) creating the overlay succeeds, but the
421 first call to XvShm[Put]Image to a mapped window fails with:
422 "BadAlloc (insufficient resources for operation)". This happens with
423 certain formats when the XvImage is too large to the i810's liking.
425 We work around this by doing a test XvShm[Put]Image with a black
426 Xv image, this may cause some flashing, so only do this check if we
427 are running on an intel Xv-adapter. */
431 X_handler = XSetErrorHandler(intel_errhandler);
433 X11_ClearYUVOverlay(overlay);
435 /* We set the destination height and width to 1 pixel to avoid
436 putting a large black rectangle over the screen, thus
437 strongly reducing possible flashing. */
438 #ifndef NO_SHARED_MEMORY
439 if ( hwdata->yuv_use_mitshm ) {
440 SDL_NAME(XvShmPutImage)(GFX_Display, hwdata->port,
443 0, 0, overlay->w, overlay->h,
449 SDL_NAME(XvPutImage)(GFX_Display, hwdata->port,
452 0, 0, overlay->w, overlay->h,
455 XSync(GFX_Display, False);
456 XSetErrorHandler(X_handler);
460 X11_FreeYUVOverlay(this, overlay);
463 /* Repair the (1 pixel worth of) damage we've just done */
464 X11_RefreshDisplay(this);
468 /* We're all done.. */
472 int X11_LockYUVOverlay(_THIS, SDL_Overlay *overlay)
477 void X11_UnlockYUVOverlay(_THIS, SDL_Overlay *overlay)
482 int X11_DisplayYUVOverlay(_THIS, SDL_Overlay *overlay, SDL_Rect *src, SDL_Rect *dst)
484 struct private_yuvhwdata *hwdata;
486 hwdata = overlay->hwdata;
488 #ifndef NO_SHARED_MEMORY
489 if ( hwdata->yuv_use_mitshm ) {
490 SDL_NAME(XvShmPutImage)(GFX_Display, hwdata->port, SDL_Window, SDL_GC,
492 src->x, src->y, src->w, src->h,
493 dst->x, dst->y, dst->w, dst->h, False);
498 SDL_NAME(XvPutImage)(GFX_Display, hwdata->port, SDL_Window, SDL_GC,
500 src->x, src->y, src->w, src->h,
501 dst->x, dst->y, dst->w, dst->h);
503 XSync(GFX_Display, False);
507 void X11_FreeYUVOverlay(_THIS, SDL_Overlay *overlay)
509 struct private_yuvhwdata *hwdata;
511 hwdata = overlay->hwdata;
513 SDL_NAME(XvUngrabPort)(GFX_Display, hwdata->port, CurrentTime);
514 #ifndef NO_SHARED_MEMORY
515 if ( hwdata->yuv_use_mitshm ) {
516 XShmDetach(GFX_Display, &hwdata->yuvshm);
517 shmdt(hwdata->yuvshm.shmaddr);
520 if ( hwdata->image ) {
521 XFree(hwdata->image);
525 if ( overlay->pitches ) {
526 SDL_free(overlay->pitches);
527 overlay->pitches = NULL;
529 if ( overlay->pixels ) {
530 SDL_free(overlay->pixels);
531 overlay->pixels = NULL;
533 #ifdef XFREE86_REFRESH_HACK
534 X11_EnableAutoRefresh(this);
538 #endif /* SDL_VIDEO_DRIVER_X11_XV */