2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2004 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with this library; if not, write to the Free
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 MGA CRTC2 support by Thomas Jarosch - tomj@simonv.com
23 CRTC2 support is inspired by mplayer's dfbmga driver
24 written by Ville Syrj��<syrjala@sci.fi>
32 /* DirectFB video driver implementation.
45 #include "SDL_error.h"
46 #include "SDL_video.h"
47 #include "SDL_mouse.h"
48 #include "SDL_sysvideo.h"
49 #include "SDL_pixels_c.h"
50 #include "SDL_events_c.h"
51 #include "SDL_DirectFB_video.h"
52 #include "SDL_DirectFB_events.h"
53 #include "SDL_DirectFB_yuv.h"
55 /* The implementation dependent data for the window manager cursor */
61 /* Initialization/Query functions */
62 static int DirectFB_VideoInit(_THIS, SDL_PixelFormat *vformat);
63 static SDL_Rect **DirectFB_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags);
64 static SDL_Surface *DirectFB_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags);
65 static int DirectFB_SetColors(_THIS, int firstcolor, int ncolors,
67 static void DirectFB_VideoQuit(_THIS);
69 /* Hardware surface functions */
70 static int DirectFB_AllocHWSurface(_THIS, SDL_Surface *surface);
71 static int DirectFB_FillHWRect(_THIS, SDL_Surface *dst, SDL_Rect *dstrect, Uint32 color);
72 static int DirectFB_LockHWSurface(_THIS, SDL_Surface *surface);
73 static void DirectFB_UnlockHWSurface(_THIS, SDL_Surface *surface);
74 static void DirectFB_FreeHWSurface(_THIS, SDL_Surface *surface);
75 static int DirectFB_CheckHWBlit(_THIS, SDL_Surface *src, SDL_Surface *dst);
76 static int DirectFB_HWAccelBlit(SDL_Surface *src, SDL_Rect *srcrect,
77 SDL_Surface *dst, SDL_Rect *dstrect);
78 static int DirectFB_SetHWColorKey(_THIS, SDL_Surface *surface, Uint32 key);
79 static int DirectFB_SetHWAlpha(_THIS, SDL_Surface *surface, Uint8 alpha);
80 static int DirectFB_FlipHWSurface(_THIS, SDL_Surface *surface);
81 static int DirectFB_ShowWMCursor(_THIS, WMcursor *cursor);
83 /* Various screen update functions available */
84 static void DirectFB_DirectUpdate(_THIS, int numrects, SDL_Rect *rects);
85 static void DirectFB_WindowedUpdate(_THIS, int numrects, SDL_Rect *rects);
87 /* This is the rect EnumModes2 uses */
88 struct DirectFBEnumRect {
90 struct DirectFBEnumRect* next;
93 static struct DirectFBEnumRect *enumlist = NULL;
96 /* DirectFB driver bootstrap functions */
98 static int DirectFB_Available(void)
103 static void DirectFB_DeleteDevice(SDL_VideoDevice *device)
105 free(device->hidden);
109 static SDL_VideoDevice *DirectFB_CreateDevice(int devindex)
111 SDL_VideoDevice *device;
113 /* Initialize all variables that we clean on shutdown */
114 device = (SDL_VideoDevice *)malloc(sizeof(SDL_VideoDevice));
117 memset (device, 0, (sizeof *device));
118 device->hidden = (struct SDL_PrivateVideoData *) malloc (sizeof (*device->hidden));
120 if (device == NULL || device->hidden == NULL)
129 memset (device->hidden, 0, sizeof (*device->hidden));
131 /* Set the function pointers */
132 device->VideoInit = DirectFB_VideoInit;
133 device->ListModes = DirectFB_ListModes;
134 device->SetVideoMode = DirectFB_SetVideoMode;
135 device->SetColors = DirectFB_SetColors;
136 device->UpdateRects = NULL;
137 device->CreateYUVOverlay = DirectFB_CreateYUVOverlay;
138 device->VideoQuit = DirectFB_VideoQuit;
139 device->AllocHWSurface = DirectFB_AllocHWSurface;
140 device->CheckHWBlit = DirectFB_CheckHWBlit;
141 device->FillHWRect = DirectFB_FillHWRect;
142 device->SetHWColorKey = DirectFB_SetHWColorKey;
143 device->SetHWAlpha = DirectFB_SetHWAlpha;
144 device->LockHWSurface = DirectFB_LockHWSurface;
145 device->UnlockHWSurface = DirectFB_UnlockHWSurface;
146 device->FlipHWSurface = DirectFB_FlipHWSurface;
147 device->FreeHWSurface = DirectFB_FreeHWSurface;
148 device->ShowWMCursor = DirectFB_ShowWMCursor;
149 device->SetCaption = NULL;
150 device->SetIcon = NULL;
151 device->IconifyWindow = NULL;
152 device->GrabInput = NULL;
153 device->GetWMInfo = NULL;
154 device->InitOSKeymap = DirectFB_InitOSKeymap;
155 device->PumpEvents = DirectFB_PumpEvents;
157 device->free = DirectFB_DeleteDevice;
162 VideoBootStrap DirectFB_bootstrap = {
163 "directfb", "DirectFB",
164 DirectFB_Available, DirectFB_CreateDevice
167 static DFBSurfacePixelFormat GetFormatForBpp (int bpp, IDirectFBDisplayLayer *layer)
169 DFBDisplayLayerConfig dlc;
170 int bytes = (bpp + 7) / 8;
172 layer->GetConfiguration (layer, &dlc);
174 if (bytes == DFB_BYTES_PER_PIXEL(dlc.pixelformat) && bytes > 1)
175 return dlc.pixelformat;
192 static DFBEnumerationResult EnumModesCallback (int width,
197 SDL_VideoDevice *this = (SDL_VideoDevice *)data;
198 struct DirectFBEnumRect *enumrect;
202 enumrect = calloc(1, sizeof(struct DirectFBEnumRect));
206 return DFENUM_CANCEL;
209 enumrect->r.w = (Uint16)width;
210 enumrect->r.h = (Uint16)height;
211 enumrect->next = enumlist;
218 struct private_hwdata {
219 IDirectFBSurface *surface;
220 IDirectFBPalette *palette;
223 void SetDirectFBerror (const char *function, DFBResult code)
225 const char *error = DirectFBErrorString (code);
228 SDL_SetError("%s: %s", function, error);
230 SDL_SetError("Unknown error code from %s", function);
233 static DFBSurfacePixelFormat SDLToDFBPixelFormat (SDL_PixelFormat *format)
235 if (format->Rmask && format->Gmask && format->Bmask)
237 switch (format->BitsPerPixel)
243 if (format->Rmask == 0xF800 &&
244 format->Gmask == 0x07E0 &&
245 format->Bmask == 0x001F)
250 if (format->Rmask == 0x7C00 &&
251 format->Gmask == 0x03E0 &&
252 format->Bmask == 0x001F)
253 return DSPF_ARGB1555;
257 if (format->Rmask == 0xFF0000 &&
258 format->Gmask == 0x00FF00 &&
259 format->Bmask == 0x0000FF)
264 if (format->Rmask == 0xFF0000 &&
265 format->Gmask == 0x00FF00 &&
266 format->Bmask == 0x0000FF)
268 if (format->Amask == 0xFF000000)
278 switch (format->BitsPerPixel)
283 return DSPF_ARGB1555;
296 static SDL_Palette *AllocatePalette(int size)
298 SDL_Palette *palette;
301 palette = calloc (1, sizeof(SDL_Palette));
308 colors = calloc (size, sizeof(SDL_Color));
315 palette->ncolors = size;
316 palette->colors = colors;
321 static int DFBToSDLPixelFormat (DFBSurfacePixelFormat pixelformat, SDL_PixelFormat *format)
323 format->Amask = format->Rmask = format->Gmask = format->Bmask = 0;
324 format->BitsPerPixel = format->BytesPerPixel = 0;
329 format->Amask = 0x000000FF;
333 format->Rmask = 0x00007C00;
334 format->Gmask = 0x000003E0;
335 format->Bmask = 0x0000001F;
339 format->Rmask = 0x0000F800;
340 format->Gmask = 0x000007E0;
341 format->Bmask = 0x0000001F;
345 format->Amask = 0; /* apps don't seem to like that: 0xFF000000; */
349 format->Rmask = 0x00FF0000;
350 format->Gmask = 0x0000FF00;
351 format->Bmask = 0x000000FF;
355 format->Rmask = 0x000000FF;
356 format->Gmask = 0x000000FF;
357 format->Bmask = 0x000000FF;
359 if (!format->palette)
360 format->palette = AllocatePalette(256);
364 fprintf (stderr, "SDL_DirectFB: Unsupported pixelformat (0x%08x)!\n", pixelformat);
368 format->BitsPerPixel = DFB_BYTES_PER_PIXEL(pixelformat) * 8;
369 format->BytesPerPixel = DFB_BYTES_PER_PIXEL(pixelformat);
375 int DirectFB_VideoInit(_THIS, SDL_PixelFormat *vformat)
379 DFBCardCapabilities caps;
380 DFBDisplayLayerConfig dlc;
381 struct DirectFBEnumRect *rect;
382 IDirectFB *dfb = NULL;
383 IDirectFBDisplayLayer *layer = NULL;
384 IDirectFBEventBuffer *events = NULL;
386 HIDDEN->c2layer = NULL, HIDDEN->c2frame = NULL;
387 HIDDEN->enable_mga_crtc2 = 0;
388 HIDDEN->mga_crtc2_stretch_overscan = 1;
390 ret = DirectFBInit (NULL, NULL);
393 SetDirectFBerror ("DirectFBInit", ret);
397 ret = DirectFBCreate (&dfb);
400 SetDirectFBerror ("DirectFBCreate", ret);
404 ret = dfb->GetDisplayLayer (dfb, DLID_PRIMARY, &layer);
407 SetDirectFBerror ("dfb->GetDisplayLayer", ret);
411 ret = dfb->CreateInputEventBuffer (dfb, DICAPS_ALL, DFB_FALSE, &events);
414 SetDirectFBerror ("dfb->CreateEventBuffer", ret);
418 layer->EnableCursor (layer, 1);
420 /* Query layer configuration to determine the current mode and pixelformat */
421 layer->GetConfiguration (layer, &dlc);
423 /* If current format is not supported use LUT8 as the default */
424 if (DFBToSDLPixelFormat (dlc.pixelformat, vformat))
425 DFBToSDLPixelFormat (DSPF_LUT8, vformat);
427 /* Enumerate the available fullscreen modes */
428 ret = dfb->EnumVideoModes (dfb, EnumModesCallback, this);
431 SetDirectFBerror ("dfb->EnumVideoModes", ret);
435 HIDDEN->modelist = calloc (HIDDEN->nummodes + 1, sizeof(SDL_Rect *));
436 if (!HIDDEN->modelist)
442 for (i = 0, rect = enumlist; rect; ++i, rect = rect->next )
444 HIDDEN->modelist[i] = &rect->r;
447 HIDDEN->modelist[i] = NULL;
450 /* Query card capabilities to get the video memory size */
451 dfb->GetCardCapabilities (dfb, &caps);
453 this->info.wm_available = 1;
454 this->info.hw_available = 1;
455 this->info.blit_hw = 1;
456 this->info.blit_hw_CC = 1;
457 this->info.blit_hw_A = 1;
458 this->info.blit_fill = 1;
459 this->info.video_mem = caps.video_memory / 1024;
461 HIDDEN->initialized = 1;
463 HIDDEN->layer = layer;
464 HIDDEN->eventbuffer = events;
466 if (getenv("SDL_DIRECTFB_MGA_CRTC2") != NULL)
467 HIDDEN->enable_mga_crtc2 = 1;
469 if (HIDDEN->enable_mga_crtc2)
471 ret = dfb->GetDisplayLayer (dfb, 2, &HIDDEN->c2layer);
474 SetDirectFBerror ("dfb->GetDisplayLayer(CRTC2)", ret);
478 ret = HIDDEN->layer->SetCooperativeLevel(HIDDEN->layer, DLSCL_EXCLUSIVE);
481 SetDirectFBerror ("layer->SetCooperativeLevel(CRTC2, EXCLUSIVE)", ret);
485 ret = HIDDEN->c2layer->SetCooperativeLevel(HIDDEN->c2layer, DLSCL_EXCLUSIVE);
488 SetDirectFBerror ("c2layer->SetCooperativeLevel(CRTC2, EXCLUSIVE)", ret);
492 HIDDEN->c2layer->SetOpacity(HIDDEN->c2layer, 0x0);
494 /* Init the surface here as it got a fixed size */
495 DFBDisplayLayerConfig dlc;
496 DFBDisplayLayerConfigFlags failed;
498 dlc.flags = DLCONF_PIXELFORMAT | DLCONF_BUFFERMODE | DLCONF_OPTIONS;
499 dlc.buffermode = DLBM_BACKVIDEO;
500 dlc.options = DLOP_FLICKER_FILTERING;
501 dlc.pixelformat = DSPF_RGB32;
503 ret = HIDDEN->c2layer->TestConfiguration( HIDDEN->c2layer, &dlc, &failed );
506 SetDirectFBerror ("c2layer->TestConfiguration", ret);
510 ret = HIDDEN->c2layer->SetConfiguration( HIDDEN->c2layer, &dlc );
513 SetDirectFBerror ("c2layer->SetConfiguration", ret);
517 ret = HIDDEN->c2layer->GetSurface( HIDDEN->c2layer, &HIDDEN->c2frame );
520 SetDirectFBerror ("c2layer->GetSurface", ret);
524 HIDDEN->c2framesize.x = 0;
525 HIDDEN->c2framesize.y = 0;
526 HIDDEN->c2frame->GetSize( HIDDEN->c2frame, &HIDDEN->c2framesize.w, &HIDDEN->c2framesize.h);
528 HIDDEN->c2frame->SetBlittingFlags( HIDDEN->c2frame, DSBLIT_NOFX );
529 HIDDEN->c2frame->SetColor( HIDDEN->c2frame, 0, 0, 0, 0xff );
532 HIDDEN->c2frame->Clear(HIDDEN->c2frame, 0, 0, 0, 0xff );
533 HIDDEN->c2frame->Flip(HIDDEN->c2frame, NULL, 0 );
534 HIDDEN->c2frame->Clear(HIDDEN->c2frame, 0, 0, 0, 0xff );
535 HIDDEN->c2frame->Flip(HIDDEN->c2frame, NULL, 0 );
536 HIDDEN->c2frame->Clear(HIDDEN->c2frame, 0, 0, 0, 0xff );
538 HIDDEN->c2layer->SetOpacity(HIDDEN->c2layer, 0xFF );
540 /* Check if overscan is possibly set */
541 if (getenv("SDL_DIRECTFB_MGA_OVERSCAN") != NULL)
544 if (sscanf(getenv("SDL_DIRECTFB_MGA_OVERSCAN"), "%f", &overscan) == 1)
545 if (overscan > 0 && overscan < 2)
546 HIDDEN->mga_crtc2_stretch_overscan = overscan;
549 #ifdef DIRECTFB_CRTC2_DEBUG
550 printf("CRTC2 overscan: %f\n", HIDDEN->mga_crtc2_stretch_overscan);
558 events->Release (events);
561 HIDDEN->c2frame->Release (HIDDEN->c2frame);
564 HIDDEN->c2layer->Release (HIDDEN->c2layer);
567 layer->Release (layer);
575 static SDL_Rect **DirectFB_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags)
577 if (flags & SDL_FULLSCREEN)
578 return HIDDEN->modelist;
580 if (SDLToDFBPixelFormat (format) != DSPF_UNKNOWN)
581 return (SDL_Rect**) -1;
586 static SDL_Surface *DirectFB_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags)
589 DFBSurfaceDescription dsc;
590 DFBSurfacePixelFormat pixelformat;
591 IDirectFBSurface *surface;
593 fprintf (stderr, "SDL DirectFB_SetVideoMode: %dx%d@%d, flags: 0x%08x\n",
594 width, height, bpp, flags);
596 flags |= SDL_FULLSCREEN;
598 /* Release previous primary surface */
599 if (current->hwdata && current->hwdata->surface)
601 current->hwdata->surface->Release (current->hwdata->surface);
602 current->hwdata->surface = NULL;
604 /* And its palette if present */
605 if (current->hwdata->palette)
607 current->hwdata->palette->Release (current->hwdata->palette);
608 current->hwdata->palette = NULL;
611 else if (!current->hwdata)
613 /* Allocate the hardware acceleration data */
614 current->hwdata = (struct private_hwdata *) calloc (1, sizeof(*current->hwdata));
615 if (!current->hwdata)
622 /* Set cooperative level depending on flag SDL_FULLSCREEN */
623 if (flags & SDL_FULLSCREEN)
625 ret = HIDDEN->dfb->SetCooperativeLevel (HIDDEN->dfb, DFSCL_FULLSCREEN);
626 if (ret && !HIDDEN->enable_mga_crtc2)
628 DirectFBError ("dfb->SetCooperativeLevel", ret);
629 flags &= ~SDL_FULLSCREEN;
633 HIDDEN->dfb->SetCooperativeLevel (HIDDEN->dfb, DFSCL_NORMAL);
636 ret = HIDDEN->dfb->SetVideoMode (HIDDEN->dfb, width, height, bpp);
639 if (flags & SDL_FULLSCREEN)
641 flags &= ~SDL_FULLSCREEN;
642 HIDDEN->dfb->SetCooperativeLevel (HIDDEN->dfb, DFSCL_NORMAL);
643 ret = HIDDEN->dfb->SetVideoMode (HIDDEN->dfb, width, height, bpp);
648 SetDirectFBerror ("dfb->SetVideoMode", ret);
653 /* Create primary surface */
654 dsc.flags = DSDESC_CAPS | DSDESC_PIXELFORMAT;
655 dsc.caps = DSCAPS_PRIMARY | ((flags & SDL_DOUBLEBUF) ? DSCAPS_FLIPPING : 0);
656 dsc.pixelformat = GetFormatForBpp (bpp, HIDDEN->layer);
658 ret = HIDDEN->dfb->CreateSurface (HIDDEN->dfb, &dsc, &surface);
659 if (ret && (flags & SDL_DOUBLEBUF))
661 /* Try without double buffering */
662 dsc.caps &= ~DSCAPS_FLIPPING;
663 ret = HIDDEN->dfb->CreateSurface (HIDDEN->dfb, &dsc, &surface);
667 SetDirectFBerror ("dfb->CreateSurface", ret);
673 current->flags = SDL_HWSURFACE | SDL_PREALLOC;
675 if (flags & SDL_FULLSCREEN)
677 current->flags |= SDL_FULLSCREEN;
678 this->UpdateRects = DirectFB_DirectUpdate;
681 this->UpdateRects = DirectFB_WindowedUpdate;
683 if (dsc.caps & DSCAPS_FLIPPING)
684 current->flags |= SDL_DOUBLEBUF;
686 surface->GetPixelFormat (surface, &pixelformat);
688 DFBToSDLPixelFormat (pixelformat, current->format);
690 /* Get the surface palette (if supported) */
691 if (DFB_PIXELFORMAT_IS_INDEXED( pixelformat ))
693 surface->GetPalette (surface, ¤t->hwdata->palette);
695 current->flags |= SDL_HWPALETTE;
698 current->hwdata->surface = surface;
700 /* MGA CRTC2 stuff */
701 if (HIDDEN->enable_mga_crtc2)
703 /* no stretching if c2ssize == c2framesize */
704 HIDDEN->c2ssize.x = 0, HIDDEN->c2ssize.y = 0;
705 HIDDEN->c2ssize.w = width;
706 HIDDEN->c2ssize.h = height;
708 HIDDEN->c2dsize.x = 0, HIDDEN->c2dsize.y = 0;
709 HIDDEN->c2dsize.w = width;
710 HIDDEN->c2dsize.h = height;
712 HIDDEN->mga_crtc2_stretch = 0;
714 if (getenv("SDL_DIRECTFB_MGA_STRETCH") != NULL)
716 /* Normally assume a picture aspect ratio of 4:3 */
717 int zoom_aspect_x = 4, zoom_aspect_y = 3, i, j;
719 for (i = 1; i < 20; i++)
721 for (j = 1; j < 10; j++)
723 if ((float)width/(float)i*(float)j == height)
735 #ifdef DIRECTFB_CRTC2_DEBUG
736 printf("Source resolution: X: %d, Y: %d, Aspect ratio: %d:%d\n", width, height, zoom_aspect_x, zoom_aspect_y);
737 printf("CRTC2 resolution: X: %d, Y: %d\n", HIDDEN->c2framesize.w, HIDDEN->c2framesize.h);
740 /* don't stretch only slightly smaller/larger images */
741 if ((float)width < (float)HIDDEN->c2framesize.w*0.95 || (float)height < (float)HIDDEN->c2framesize.h*0.95)
743 while ((float)HIDDEN->c2dsize.w < (float)HIDDEN->c2framesize.w*HIDDEN->mga_crtc2_stretch_overscan && (float)HIDDEN->c2dsize.h < (float)HIDDEN->c2framesize.h*HIDDEN->mga_crtc2_stretch_overscan)
745 HIDDEN->c2dsize.w+=zoom_aspect_x;
746 HIDDEN->c2dsize.h+=zoom_aspect_y;
750 HIDDEN->c2dsize.w-=zoom_aspect_x;
751 HIDDEN->c2dsize.h-=zoom_aspect_y;
753 #ifdef DIRECTFB_CRTC2_DEBUG
754 printf("Stretched resolution: X: %d, Y: %d\n", HIDDEN->c2dsize.w, HIDDEN->c2dsize.h);
757 HIDDEN->mga_crtc2_stretch = 1;
759 else if ((float)width > (float)HIDDEN->c2framesize.w*0.95 || (float)height > (float)HIDDEN->c2framesize.h*0.95)
761 while ((float)HIDDEN->c2dsize.w > (float)HIDDEN->c2framesize.w*HIDDEN->mga_crtc2_stretch_overscan || (float)HIDDEN->c2dsize.h > (float)HIDDEN->c2framesize.h*HIDDEN->mga_crtc2_stretch_overscan)
763 HIDDEN->c2dsize.w-=zoom_aspect_x;
764 HIDDEN->c2dsize.h-=zoom_aspect_y;
767 #ifdef DIRECTFB_CRTC2_DEBUG
768 printf("Down-Stretched resolution: X: %d, Y: %d\n", HIDDEN->c2dsize.w, HIDDEN->c2dsize.h);
771 HIDDEN->mga_crtc2_stretch = 1;
773 #ifdef DIRECTFB_CRTC2_DEBUG
774 printf("Not stretching image\n");
779 if (HIDDEN->c2framesize.w > HIDDEN->c2dsize.w)
780 HIDDEN->c2dsize.x = (HIDDEN->c2framesize.w - HIDDEN->c2dsize.w) / 2;
782 HIDDEN->c2dsize.x = (HIDDEN->c2dsize.w - HIDDEN->c2framesize.w) / 2;
784 if (HIDDEN->c2framesize.h > HIDDEN->c2dsize.h)
785 HIDDEN->c2dsize.y = (HIDDEN->c2framesize.h - HIDDEN->c2dsize.h) / 2;
787 HIDDEN->c2dsize.y = (HIDDEN->c2dsize.h - HIDDEN->c2framesize.h) / 2;
789 #ifdef DIRECTFB_CRTC2_DEBUG
790 printf("CRTC2 position X: %d, Y: %d\n", HIDDEN->c2dsize.x, HIDDEN->c2dsize.y);
798 static int DirectFB_AllocHWSurface(_THIS, SDL_Surface *surface)
801 DFBSurfaceDescription dsc;
803 /* fprintf(stderr, "SDL: DirectFB_AllocHWSurface (%dx%d@%d, flags: 0x%08x)\n",
804 surface->w, surface->h, surface->format->BitsPerPixel, surface->flags);*/
806 if (surface->w < 8 || surface->h < 8)
809 /* fill surface description */
810 dsc.flags = DSDESC_WIDTH | DSDESC_HEIGHT | DSDESC_PIXELFORMAT | DSDESC_CAPS;
811 dsc.width = surface->w;
812 dsc.height = surface->h;
813 dsc.caps = (surface->flags & SDL_DOUBLEBUF) ? DSCAPS_FLIPPING : 0;
815 /* find the right pixelformat */
816 dsc.pixelformat = SDLToDFBPixelFormat (surface->format);
817 if (dsc.pixelformat == DSPF_UNKNOWN)
820 /* Allocate the hardware acceleration data */
821 surface->hwdata = (struct private_hwdata *) calloc (1, sizeof(*surface->hwdata));
822 if (surface->hwdata == NULL)
828 /* Create the surface */
829 ret = HIDDEN->dfb->CreateSurface (HIDDEN->dfb, &dsc, &surface->hwdata->surface);
832 SetDirectFBerror ("dfb->CreateSurface", ret);
833 free (surface->hwdata);
834 surface->hwdata = NULL;
838 surface->flags |= SDL_HWSURFACE | SDL_PREALLOC;
843 static void DirectFB_FreeHWSurface(_THIS, SDL_Surface *surface)
845 if (surface->hwdata && HIDDEN->initialized)
847 surface->hwdata->surface->Release (surface->hwdata->surface);
848 free (surface->hwdata);
849 surface->hwdata = NULL;
853 static int DirectFB_CheckHWBlit(_THIS, SDL_Surface *src, SDL_Surface *dst)
855 /* fprintf(stderr, "SDL: DirectFB_CheckHWBlit (src->hwdata: %p, dst->hwdata: %p)\n",
856 src->hwdata, dst->hwdata);*/
858 if (!src->hwdata || !dst->hwdata)
861 src->flags |= SDL_HWACCEL;
862 src->map->hw_blit = DirectFB_HWAccelBlit;
867 static int DirectFB_HWAccelBlit(SDL_Surface *src, SDL_Rect *srcrect,
868 SDL_Surface *dst, SDL_Rect *dstrect)
870 DFBSurfaceBlittingFlags flags = DSBLIT_NOFX;
872 DFBRectangle sr = { srcrect->x, srcrect->y, srcrect->w, srcrect->h };
873 DFBRectangle dr = { dstrect->x, dstrect->y, dstrect->w, dstrect->h };
875 IDirectFBSurface *surface = dst->hwdata->surface;
877 if (src->flags & SDL_SRCCOLORKEY)
879 flags |= DSBLIT_SRC_COLORKEY;
880 DirectFB_SetHWColorKey (NULL, src, src->format->colorkey);
883 if (src->flags & SDL_SRCALPHA)
885 flags |= DSBLIT_BLEND_COLORALPHA;
886 surface->SetColor (surface, 0xff, 0xff, 0xff, src->format->alpha);
889 surface->SetBlittingFlags (surface, flags);
891 if (sr.w == dr.w && sr.h == dr.h)
892 surface->Blit (surface, src->hwdata->surface, &sr, dr.x, dr.y);
894 surface->StretchBlit (surface, src->hwdata->surface, &sr, &dr);
899 static int DirectFB_FillHWRect(_THIS, SDL_Surface *dst, SDL_Rect *dstrect, Uint32 color)
901 SDL_PixelFormat *fmt = dst->format;
902 IDirectFBSurface *surface = dst->hwdata->surface;
905 surface->SetColor (surface,
906 (color & fmt->Rmask) >> (fmt->Rshift - fmt->Rloss),
907 (color & fmt->Gmask) >> (fmt->Gshift - fmt->Gloss),
908 (color & fmt->Bmask) << (fmt->Bloss - fmt->Bshift), 0xFF);
909 surface->FillRectangle (surface, dstrect->x, dstrect->y, dstrect->w, dstrect->h);
914 static int DirectFB_SetHWColorKey(_THIS, SDL_Surface *src, Uint32 key)
916 SDL_PixelFormat *fmt = src->format;
917 IDirectFBSurface *surface = src->hwdata->surface;
919 if (fmt->BitsPerPixel == 8)
920 surface->SetSrcColorKeyIndex (surface, key);
923 surface->SetSrcColorKey (surface,
924 (key & fmt->Rmask) >> (fmt->Rshift - fmt->Rloss),
925 (key & fmt->Gmask) >> (fmt->Gshift - fmt->Gloss),
926 (key & fmt->Bmask) << (fmt->Bloss - fmt->Bshift));
931 static int DirectFB_SetHWAlpha(_THIS, SDL_Surface *surface, Uint8 alpha)
936 static int DirectFB_FlipHWSurface(_THIS, SDL_Surface *surface)
938 if (HIDDEN->enable_mga_crtc2)
940 int rtn = surface->hwdata->surface->Flip (surface->hwdata->surface, NULL, 0);
941 if (HIDDEN->mga_crtc2_stretch)
942 HIDDEN->c2frame->StretchBlit(HIDDEN->c2frame, surface->hwdata->surface, &HIDDEN->c2ssize, &HIDDEN->c2dsize);
944 HIDDEN->c2frame->Blit(HIDDEN->c2frame, surface->hwdata->surface, NULL, HIDDEN->c2dsize.x, HIDDEN->c2dsize.y);
946 HIDDEN->c2frame->Flip(HIDDEN->c2frame, NULL, DSFLIP_WAITFORSYNC);
950 return surface->hwdata->surface->Flip (surface->hwdata->surface, NULL, DSFLIP_WAITFORSYNC);
953 static int DirectFB_LockHWSurface(_THIS, SDL_Surface *surface)
959 ret = surface->hwdata->surface->Lock (surface->hwdata->surface,
960 DSLF_WRITE, &data, &pitch);
963 SetDirectFBerror ("surface->Lock", ret);
967 surface->pixels = data;
968 surface->pitch = pitch;
973 static void DirectFB_UnlockHWSurface(_THIS, SDL_Surface *surface)
975 surface->hwdata->surface->Unlock (surface->hwdata->surface);
976 surface->pixels = NULL;
979 static void DirectFB_DirectUpdate(_THIS, int numrects, SDL_Rect *rects)
981 if (HIDDEN->enable_mga_crtc2)
983 if (HIDDEN->mga_crtc2_stretch)
984 HIDDEN->c2frame->StretchBlit(HIDDEN->c2frame, this->screen->hwdata->surface, &HIDDEN->c2ssize, &HIDDEN->c2dsize);
986 HIDDEN->c2frame->Blit(HIDDEN->c2frame, this->screen->hwdata->surface, NULL, HIDDEN->c2dsize.x, HIDDEN->c2dsize.y);
988 HIDDEN->c2frame->Flip(HIDDEN->c2frame, NULL, DSFLIP_WAITFORSYNC);
992 static void DirectFB_WindowedUpdate(_THIS, int numrects, SDL_Rect *rects)
996 int region_valid = 0;
997 IDirectFBSurface *surface = this->screen->hwdata->surface;
999 for (i=0; i<numrects; ++i)
1003 if ( ! rects[i].w ) /* Clipped? */
1006 x2 = rects[i].x + rects[i].w - 1;
1007 y2 = rects[i].y + rects[i].h - 1;
1011 if (rects[i].x < region.x1)
1012 region.x1 = rects[i].x;
1014 if (rects[i].y < region.y1)
1015 region.y1 = rects[i].y;
1025 region.x1 = rects[i].x;
1026 region.y1 = rects[i].y;
1036 if (HIDDEN->enable_mga_crtc2)
1038 if (HIDDEN->mga_crtc2_stretch)
1039 HIDDEN->c2frame->StretchBlit(HIDDEN->c2frame, surface, &HIDDEN->c2ssize, &HIDDEN->c2dsize);
1041 HIDDEN->c2frame->Blit(HIDDEN->c2frame, surface, NULL, HIDDEN->c2dsize.x, HIDDEN->c2dsize.y);
1043 HIDDEN->c2frame->Flip(HIDDEN->c2frame, NULL, DSFLIP_WAITFORSYNC);
1046 surface->Flip (surface, ®ion, DSFLIP_WAITFORSYNC);
1050 int DirectFB_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors)
1052 IDirectFBPalette *palette = this->screen->hwdata->palette;
1057 if (firstcolor > 255)
1060 if (firstcolor + ncolors > 256)
1061 ncolors = 256 - firstcolor;
1066 DFBColor entries[ncolors];
1068 for (i=0; i<ncolors; i++)
1070 entries[i].a = 0xff;
1071 entries[i].r = colors[i].r;
1072 entries[i].g = colors[i].g;
1073 entries[i].b = colors[i].b;
1076 palette->SetEntries (palette, entries, ncolors, firstcolor);
1082 void DirectFB_VideoQuit(_THIS)
1084 struct DirectFBEnumRect *rect = enumlist;
1086 if (this->screen->hwdata)
1088 IDirectFBSurface *surface = this->screen->hwdata->surface;
1089 IDirectFBPalette *palette = this->screen->hwdata->palette;
1092 palette->Release (palette);
1095 surface->Release (surface);
1097 this->screen->hwdata->surface = NULL;
1098 this->screen->hwdata->palette = NULL;
1101 if (HIDDEN->c2frame)
1103 HIDDEN->c2frame->Release (HIDDEN->c2frame);
1104 HIDDEN->c2frame = NULL;
1107 this->screen->hwdata->surface = NULL;
1108 this->screen->hwdata->palette = NULL;
1110 if (HIDDEN->eventbuffer)
1112 HIDDEN->eventbuffer->Release (HIDDEN->eventbuffer);
1113 HIDDEN->eventbuffer = NULL;
1116 if (HIDDEN->c2layer)
1118 HIDDEN->c2layer->Release (HIDDEN->c2layer);
1119 HIDDEN->c2layer = NULL;
1124 HIDDEN->layer->Release (HIDDEN->layer);
1125 HIDDEN->layer = NULL;
1130 HIDDEN->dfb->Release (HIDDEN->dfb);
1134 /* Free video mode list */
1135 if (HIDDEN->modelist)
1137 free (HIDDEN->modelist);
1138 HIDDEN->modelist = NULL;
1141 /* Free mode enumeration list */
1144 struct DirectFBEnumRect *next = rect->next;
1150 HIDDEN->initialized = 0;
1154 int DirectFB_ShowWMCursor(_THIS, WMcursor *cursor)
1156 /* We can only hide or show the default cursor */
1157 if ( cursor == NULL )
1159 HIDDEN->layer->SetCursorOpacity(HIDDEN->layer, 0x00);
1163 HIDDEN->layer->SetCursorOpacity(HIDDEN->layer, 0xFF);
1168 void DirectFB_FinalQuit(void)