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.
43 #include <directfb_version.h>
46 #include "SDL_error.h"
47 #include "SDL_video.h"
48 #include "SDL_mouse.h"
49 #include "SDL_sysvideo.h"
50 #include "SDL_pixels_c.h"
51 #include "SDL_events_c.h"
52 #include "SDL_DirectFB_video.h"
53 #include "SDL_DirectFB_events.h"
54 #include "SDL_DirectFB_yuv.h"
56 /* The implementation dependent data for the window manager cursor */
62 /* Initialization/Query functions */
63 static int DirectFB_VideoInit(_THIS, SDL_PixelFormat *vformat);
64 static SDL_Rect **DirectFB_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags);
65 static SDL_Surface *DirectFB_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags);
66 static int DirectFB_SetColors(_THIS, int firstcolor, int ncolors,
68 static void DirectFB_VideoQuit(_THIS);
70 /* Hardware surface functions */
71 static int DirectFB_AllocHWSurface(_THIS, SDL_Surface *surface);
72 static int DirectFB_FillHWRect(_THIS, SDL_Surface *dst, SDL_Rect *dstrect, Uint32 color);
73 static int DirectFB_LockHWSurface(_THIS, SDL_Surface *surface);
74 static void DirectFB_UnlockHWSurface(_THIS, SDL_Surface *surface);
75 static void DirectFB_FreeHWSurface(_THIS, SDL_Surface *surface);
76 static int DirectFB_CheckHWBlit(_THIS, SDL_Surface *src, SDL_Surface *dst);
77 static int DirectFB_HWAccelBlit(SDL_Surface *src, SDL_Rect *srcrect,
78 SDL_Surface *dst, SDL_Rect *dstrect);
79 static int DirectFB_SetHWColorKey(_THIS, SDL_Surface *surface, Uint32 key);
80 static int DirectFB_SetHWAlpha(_THIS, SDL_Surface *surface, Uint8 alpha);
81 static int DirectFB_FlipHWSurface(_THIS, SDL_Surface *surface);
82 static int DirectFB_ShowWMCursor(_THIS, WMcursor *cursor);
84 /* Various screen update functions available */
85 static void DirectFB_DirectUpdate(_THIS, int numrects, SDL_Rect *rects);
86 static void DirectFB_WindowedUpdate(_THIS, int numrects, SDL_Rect *rects);
88 /* This is the rect EnumModes2 uses */
89 struct DirectFBEnumRect {
91 struct DirectFBEnumRect* next;
94 static struct DirectFBEnumRect *enumlist = NULL;
97 /* DirectFB driver bootstrap functions */
99 static int DirectFB_Available(void)
104 static void DirectFB_DeleteDevice(SDL_VideoDevice *device)
106 free(device->hidden);
110 static SDL_VideoDevice *DirectFB_CreateDevice(int devindex)
112 SDL_VideoDevice *device;
114 /* Initialize all variables that we clean on shutdown */
115 device = (SDL_VideoDevice *)malloc(sizeof(SDL_VideoDevice));
118 memset (device, 0, (sizeof *device));
119 device->hidden = (struct SDL_PrivateVideoData *) malloc (sizeof (*device->hidden));
121 if (device == NULL || device->hidden == NULL)
130 memset (device->hidden, 0, sizeof (*device->hidden));
132 /* Set the function pointers */
133 device->VideoInit = DirectFB_VideoInit;
134 device->ListModes = DirectFB_ListModes;
135 device->SetVideoMode = DirectFB_SetVideoMode;
136 device->SetColors = DirectFB_SetColors;
137 device->UpdateRects = NULL;
138 device->CreateYUVOverlay = DirectFB_CreateYUVOverlay;
139 device->VideoQuit = DirectFB_VideoQuit;
140 device->AllocHWSurface = DirectFB_AllocHWSurface;
141 device->CheckHWBlit = DirectFB_CheckHWBlit;
142 device->FillHWRect = DirectFB_FillHWRect;
143 device->SetHWColorKey = DirectFB_SetHWColorKey;
144 device->SetHWAlpha = DirectFB_SetHWAlpha;
145 device->LockHWSurface = DirectFB_LockHWSurface;
146 device->UnlockHWSurface = DirectFB_UnlockHWSurface;
147 device->FlipHWSurface = DirectFB_FlipHWSurface;
148 device->FreeHWSurface = DirectFB_FreeHWSurface;
149 device->ShowWMCursor = DirectFB_ShowWMCursor;
150 device->SetCaption = NULL;
151 device->SetIcon = NULL;
152 device->IconifyWindow = NULL;
153 device->GrabInput = NULL;
154 device->GetWMInfo = NULL;
155 device->InitOSKeymap = DirectFB_InitOSKeymap;
156 device->PumpEvents = DirectFB_PumpEvents;
158 device->free = DirectFB_DeleteDevice;
163 VideoBootStrap DirectFB_bootstrap = {
164 "directfb", "DirectFB",
165 DirectFB_Available, DirectFB_CreateDevice
168 static DFBSurfacePixelFormat GetFormatForBpp (int bpp, IDirectFBDisplayLayer *layer)
170 DFBDisplayLayerConfig dlc;
171 int bytes = (bpp + 7) / 8;
173 layer->GetConfiguration (layer, &dlc);
175 if (bytes == DFB_BYTES_PER_PIXEL(dlc.pixelformat) && bytes > 1)
176 return dlc.pixelformat;
193 static DFBEnumerationResult EnumModesCallback (int width,
198 SDL_VideoDevice *this = (SDL_VideoDevice *)data;
199 struct DirectFBEnumRect *enumrect;
203 enumrect = calloc(1, sizeof(struct DirectFBEnumRect));
207 return DFENUM_CANCEL;
210 enumrect->r.w = (Uint16)width;
211 enumrect->r.h = (Uint16)height;
212 enumrect->next = enumlist;
219 struct private_hwdata {
220 IDirectFBSurface *surface;
221 IDirectFBPalette *palette;
224 void SetDirectFBerror (const char *function, DFBResult code)
226 const char *error = DirectFBErrorString (code);
229 SDL_SetError("%s: %s", function, error);
231 SDL_SetError("Unknown error code from %s", function);
234 static DFBSurfacePixelFormat SDLToDFBPixelFormat (SDL_PixelFormat *format)
236 if (format->Rmask && format->Gmask && format->Bmask)
238 switch (format->BitsPerPixel)
244 if (format->Rmask == 0xF800 &&
245 format->Gmask == 0x07E0 &&
246 format->Bmask == 0x001F)
251 if (format->Rmask == 0x7C00 &&
252 format->Gmask == 0x03E0 &&
253 format->Bmask == 0x001F)
254 return DSPF_ARGB1555;
258 if (format->Rmask == 0xFF0000 &&
259 format->Gmask == 0x00FF00 &&
260 format->Bmask == 0x0000FF)
265 if (format->Rmask == 0xFF0000 &&
266 format->Gmask == 0x00FF00 &&
267 format->Bmask == 0x0000FF)
269 if (format->Amask == 0xFF000000)
279 switch (format->BitsPerPixel)
284 return DSPF_ARGB1555;
297 static SDL_Palette *AllocatePalette(int size)
299 SDL_Palette *palette;
302 palette = calloc (1, sizeof(SDL_Palette));
309 colors = calloc (size, sizeof(SDL_Color));
316 palette->ncolors = size;
317 palette->colors = colors;
322 static int DFBToSDLPixelFormat (DFBSurfacePixelFormat pixelformat, SDL_PixelFormat *format)
324 format->Amask = format->Rmask = format->Gmask = format->Bmask = 0;
325 format->BitsPerPixel = format->BytesPerPixel = 0;
330 format->Amask = 0x000000FF;
334 format->Rmask = 0x00007C00;
335 format->Gmask = 0x000003E0;
336 format->Bmask = 0x0000001F;
340 format->Rmask = 0x0000F800;
341 format->Gmask = 0x000007E0;
342 format->Bmask = 0x0000001F;
346 format->Amask = 0; /* apps don't seem to like that: 0xFF000000; */
350 format->Rmask = 0x00FF0000;
351 format->Gmask = 0x0000FF00;
352 format->Bmask = 0x000000FF;
356 format->Rmask = 0x000000FF;
357 format->Gmask = 0x000000FF;
358 format->Bmask = 0x000000FF;
360 if (!format->palette)
361 format->palette = AllocatePalette(256);
365 fprintf (stderr, "SDL_DirectFB: Unsupported pixelformat (0x%08x)!\n", pixelformat);
369 format->BitsPerPixel = DFB_BYTES_PER_PIXEL(pixelformat) * 8;
370 format->BytesPerPixel = DFB_BYTES_PER_PIXEL(pixelformat);
376 int DirectFB_VideoInit(_THIS, SDL_PixelFormat *vformat)
380 #if (DIRECTFB_MAJOR_VERSION == 0) && (DIRECTFB_MINOR_VERSION == 9) && (DIRECTFB_MICRO_VERSION < 23)
381 DFBCardCapabilities caps;
383 DFBGraphicsDeviceDescription caps;
385 DFBDisplayLayerConfig dlc;
386 struct DirectFBEnumRect *rect;
387 IDirectFB *dfb = NULL;
388 IDirectFBDisplayLayer *layer = NULL;
389 IDirectFBEventBuffer *events = NULL;
391 HIDDEN->c2layer = NULL, HIDDEN->c2frame = NULL;
392 HIDDEN->enable_mga_crtc2 = 0;
393 HIDDEN->mga_crtc2_stretch_overscan = 1;
395 ret = DirectFBInit (NULL, NULL);
398 SetDirectFBerror ("DirectFBInit", ret);
402 ret = DirectFBCreate (&dfb);
405 SetDirectFBerror ("DirectFBCreate", ret);
409 ret = dfb->GetDisplayLayer (dfb, DLID_PRIMARY, &layer);
412 SetDirectFBerror ("dfb->GetDisplayLayer", ret);
416 ret = dfb->CreateInputEventBuffer (dfb, DICAPS_ALL, DFB_FALSE, &events);
419 SetDirectFBerror ("dfb->CreateEventBuffer", ret);
423 layer->EnableCursor (layer, 1);
425 /* Query layer configuration to determine the current mode and pixelformat */
426 layer->GetConfiguration (layer, &dlc);
428 /* If current format is not supported use LUT8 as the default */
429 if (DFBToSDLPixelFormat (dlc.pixelformat, vformat))
430 DFBToSDLPixelFormat (DSPF_LUT8, vformat);
432 /* Enumerate the available fullscreen modes */
433 ret = dfb->EnumVideoModes (dfb, EnumModesCallback, this);
436 SetDirectFBerror ("dfb->EnumVideoModes", ret);
440 HIDDEN->modelist = calloc (HIDDEN->nummodes + 1, sizeof(SDL_Rect *));
441 if (!HIDDEN->modelist)
447 for (i = 0, rect = enumlist; rect; ++i, rect = rect->next )
449 HIDDEN->modelist[i] = &rect->r;
452 HIDDEN->modelist[i] = NULL;
455 /* Query card capabilities to get the video memory size */
456 #if (DIRECTFB_MAJOR_VERSION == 0) && (DIRECTFB_MINOR_VERSION == 9) && (DIRECTFB_MICRO_VERSION < 23)
457 dfb->GetCardCapabilities (dfb, &caps);
459 dfb->GetDeviceDescription (dfb, &caps);
462 this->info.wm_available = 1;
463 this->info.hw_available = 1;
464 this->info.blit_hw = 1;
465 this->info.blit_hw_CC = 1;
466 this->info.blit_hw_A = 1;
467 this->info.blit_fill = 1;
468 this->info.video_mem = caps.video_memory / 1024;
470 HIDDEN->initialized = 1;
472 HIDDEN->layer = layer;
473 HIDDEN->eventbuffer = events;
475 if (getenv("SDL_DIRECTFB_MGA_CRTC2") != NULL)
476 HIDDEN->enable_mga_crtc2 = 1;
478 if (HIDDEN->enable_mga_crtc2)
480 DFBDisplayLayerConfig dlc;
481 DFBDisplayLayerConfigFlags failed;
483 ret = dfb->GetDisplayLayer (dfb, 2, &HIDDEN->c2layer);
486 SetDirectFBerror ("dfb->GetDisplayLayer(CRTC2)", ret);
490 ret = HIDDEN->layer->SetCooperativeLevel(HIDDEN->layer, DLSCL_EXCLUSIVE);
493 SetDirectFBerror ("layer->SetCooperativeLevel(CRTC2, EXCLUSIVE)", ret);
497 ret = HIDDEN->c2layer->SetCooperativeLevel(HIDDEN->c2layer, DLSCL_EXCLUSIVE);
500 SetDirectFBerror ("c2layer->SetCooperativeLevel(CRTC2, EXCLUSIVE)", ret);
504 HIDDEN->c2layer->SetOpacity(HIDDEN->c2layer, 0x0);
506 /* Init the surface here as it got a fixed size */
507 dlc.flags = DLCONF_PIXELFORMAT | DLCONF_BUFFERMODE;
508 dlc.buffermode = DLBM_BACKVIDEO;
509 dlc.pixelformat = DSPF_RGB32;
511 ret = HIDDEN->c2layer->TestConfiguration( HIDDEN->c2layer, &dlc, &failed );
514 SetDirectFBerror ("c2layer->TestConfiguration", ret);
518 ret = HIDDEN->c2layer->SetConfiguration( HIDDEN->c2layer, &dlc );
521 SetDirectFBerror ("c2layer->SetConfiguration", ret);
525 ret = HIDDEN->c2layer->GetSurface( HIDDEN->c2layer, &HIDDEN->c2frame );
528 SetDirectFBerror ("c2layer->GetSurface", ret);
532 HIDDEN->c2framesize.x = 0;
533 HIDDEN->c2framesize.y = 0;
534 HIDDEN->c2frame->GetSize( HIDDEN->c2frame, &HIDDEN->c2framesize.w, &HIDDEN->c2framesize.h);
536 HIDDEN->c2frame->SetBlittingFlags( HIDDEN->c2frame, DSBLIT_NOFX );
537 HIDDEN->c2frame->SetColor( HIDDEN->c2frame, 0, 0, 0, 0xff );
540 HIDDEN->c2frame->Clear(HIDDEN->c2frame, 0, 0, 0, 0xff );
541 HIDDEN->c2frame->Flip(HIDDEN->c2frame, NULL, 0 );
542 HIDDEN->c2frame->Clear(HIDDEN->c2frame, 0, 0, 0, 0xff );
543 HIDDEN->c2frame->Flip(HIDDEN->c2frame, NULL, 0 );
544 HIDDEN->c2frame->Clear(HIDDEN->c2frame, 0, 0, 0, 0xff );
546 HIDDEN->c2layer->SetOpacity(HIDDEN->c2layer, 0xFF );
548 /* Check if overscan is possibly set */
549 if (getenv("SDL_DIRECTFB_MGA_OVERSCAN") != NULL)
552 if (sscanf(getenv("SDL_DIRECTFB_MGA_OVERSCAN"), "%f", &overscan) == 1)
553 if (overscan > 0 && overscan < 2)
554 HIDDEN->mga_crtc2_stretch_overscan = overscan;
557 #ifdef DIRECTFB_CRTC2_DEBUG
558 printf("CRTC2 overscan: %f\n", HIDDEN->mga_crtc2_stretch_overscan);
566 events->Release (events);
569 HIDDEN->c2frame->Release (HIDDEN->c2frame);
572 HIDDEN->c2layer->Release (HIDDEN->c2layer);
575 layer->Release (layer);
583 static SDL_Rect **DirectFB_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags)
585 if (flags & SDL_FULLSCREEN)
586 return HIDDEN->modelist;
588 if (SDLToDFBPixelFormat (format) != DSPF_UNKNOWN)
589 return (SDL_Rect**) -1;
594 static SDL_Surface *DirectFB_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags)
597 DFBSurfaceDescription dsc;
598 DFBSurfacePixelFormat pixelformat;
599 IDirectFBSurface *surface;
601 fprintf (stderr, "SDL DirectFB_SetVideoMode: %dx%d@%d, flags: 0x%08x\n",
602 width, height, bpp, flags);
604 flags |= SDL_FULLSCREEN;
606 /* Release previous primary surface */
607 if (current->hwdata && current->hwdata->surface)
609 current->hwdata->surface->Release (current->hwdata->surface);
610 current->hwdata->surface = NULL;
612 /* And its palette if present */
613 if (current->hwdata->palette)
615 current->hwdata->palette->Release (current->hwdata->palette);
616 current->hwdata->palette = NULL;
619 else if (!current->hwdata)
621 /* Allocate the hardware acceleration data */
622 current->hwdata = (struct private_hwdata *) calloc (1, sizeof(*current->hwdata));
623 if (!current->hwdata)
630 /* Set cooperative level depending on flag SDL_FULLSCREEN */
631 if (flags & SDL_FULLSCREEN)
633 ret = HIDDEN->dfb->SetCooperativeLevel (HIDDEN->dfb, DFSCL_FULLSCREEN);
634 if (ret && !HIDDEN->enable_mga_crtc2)
636 DirectFBError ("dfb->SetCooperativeLevel", ret);
637 flags &= ~SDL_FULLSCREEN;
641 HIDDEN->dfb->SetCooperativeLevel (HIDDEN->dfb, DFSCL_NORMAL);
644 ret = HIDDEN->dfb->SetVideoMode (HIDDEN->dfb, width, height, bpp);
647 if (flags & SDL_FULLSCREEN)
649 flags &= ~SDL_FULLSCREEN;
650 HIDDEN->dfb->SetCooperativeLevel (HIDDEN->dfb, DFSCL_NORMAL);
651 ret = HIDDEN->dfb->SetVideoMode (HIDDEN->dfb, width, height, bpp);
656 SetDirectFBerror ("dfb->SetVideoMode", ret);
661 /* Create primary surface */
662 dsc.flags = DSDESC_CAPS | DSDESC_PIXELFORMAT;
663 dsc.caps = DSCAPS_PRIMARY | ((flags & SDL_DOUBLEBUF) ? DSCAPS_FLIPPING : 0);
664 dsc.pixelformat = GetFormatForBpp (bpp, HIDDEN->layer);
666 ret = HIDDEN->dfb->CreateSurface (HIDDEN->dfb, &dsc, &surface);
667 if (ret && (flags & SDL_DOUBLEBUF))
669 /* Try without double buffering */
670 dsc.caps &= ~DSCAPS_FLIPPING;
671 ret = HIDDEN->dfb->CreateSurface (HIDDEN->dfb, &dsc, &surface);
675 SetDirectFBerror ("dfb->CreateSurface", ret);
681 current->flags = SDL_HWSURFACE | SDL_PREALLOC;
683 if (flags & SDL_FULLSCREEN)
685 current->flags |= SDL_FULLSCREEN;
686 this->UpdateRects = DirectFB_DirectUpdate;
689 this->UpdateRects = DirectFB_WindowedUpdate;
691 if (dsc.caps & DSCAPS_FLIPPING)
692 current->flags |= SDL_DOUBLEBUF;
694 surface->GetPixelFormat (surface, &pixelformat);
696 DFBToSDLPixelFormat (pixelformat, current->format);
698 /* Get the surface palette (if supported) */
699 if (DFB_PIXELFORMAT_IS_INDEXED( pixelformat ))
701 surface->GetPalette (surface, ¤t->hwdata->palette);
703 current->flags |= SDL_HWPALETTE;
706 current->hwdata->surface = surface;
708 /* MGA CRTC2 stuff */
709 if (HIDDEN->enable_mga_crtc2)
711 /* no stretching if c2ssize == c2framesize */
712 HIDDEN->c2ssize.x = 0, HIDDEN->c2ssize.y = 0;
713 HIDDEN->c2ssize.w = width;
714 HIDDEN->c2ssize.h = height;
716 HIDDEN->c2dsize.x = 0, HIDDEN->c2dsize.y = 0;
717 HIDDEN->c2dsize.w = width;
718 HIDDEN->c2dsize.h = height;
720 HIDDEN->mga_crtc2_stretch = 0;
722 if (getenv("SDL_DIRECTFB_MGA_STRETCH") != NULL)
724 /* Normally assume a picture aspect ratio of 4:3 */
725 int zoom_aspect_x = 4, zoom_aspect_y = 3, i, j;
727 for (i = 1; i < 20; i++)
729 for (j = 1; j < 10; j++)
731 if ((float)width/(float)i*(float)j == height)
743 #ifdef DIRECTFB_CRTC2_DEBUG
744 printf("Source resolution: X: %d, Y: %d, Aspect ratio: %d:%d\n", width, height, zoom_aspect_x, zoom_aspect_y);
745 printf("CRTC2 resolution: X: %d, Y: %d\n", HIDDEN->c2framesize.w, HIDDEN->c2framesize.h);
748 /* don't stretch only slightly smaller/larger images */
749 if ((float)width < (float)HIDDEN->c2framesize.w*0.95 || (float)height < (float)HIDDEN->c2framesize.h*0.95)
751 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)
753 HIDDEN->c2dsize.w+=zoom_aspect_x;
754 HIDDEN->c2dsize.h+=zoom_aspect_y;
758 HIDDEN->c2dsize.w-=zoom_aspect_x;
759 HIDDEN->c2dsize.h-=zoom_aspect_y;
761 #ifdef DIRECTFB_CRTC2_DEBUG
762 printf("Stretched resolution: X: %d, Y: %d\n", HIDDEN->c2dsize.w, HIDDEN->c2dsize.h);
765 HIDDEN->mga_crtc2_stretch = 1;
767 else if ((float)width > (float)HIDDEN->c2framesize.w*0.95 || (float)height > (float)HIDDEN->c2framesize.h*0.95)
769 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)
771 HIDDEN->c2dsize.w-=zoom_aspect_x;
772 HIDDEN->c2dsize.h-=zoom_aspect_y;
775 #ifdef DIRECTFB_CRTC2_DEBUG
776 printf("Down-Stretched resolution: X: %d, Y: %d\n", HIDDEN->c2dsize.w, HIDDEN->c2dsize.h);
779 HIDDEN->mga_crtc2_stretch = 1;
781 #ifdef DIRECTFB_CRTC2_DEBUG
782 printf("Not stretching image\n");
787 if (HIDDEN->c2framesize.w > HIDDEN->c2dsize.w)
788 HIDDEN->c2dsize.x = (HIDDEN->c2framesize.w - HIDDEN->c2dsize.w) / 2;
790 HIDDEN->c2dsize.x = (HIDDEN->c2dsize.w - HIDDEN->c2framesize.w) / 2;
792 if (HIDDEN->c2framesize.h > HIDDEN->c2dsize.h)
793 HIDDEN->c2dsize.y = (HIDDEN->c2framesize.h - HIDDEN->c2dsize.h) / 2;
795 HIDDEN->c2dsize.y = (HIDDEN->c2dsize.h - HIDDEN->c2framesize.h) / 2;
797 #ifdef DIRECTFB_CRTC2_DEBUG
798 printf("CRTC2 position X: %d, Y: %d\n", HIDDEN->c2dsize.x, HIDDEN->c2dsize.y);
806 static int DirectFB_AllocHWSurface(_THIS, SDL_Surface *surface)
809 DFBSurfaceDescription dsc;
811 /* fprintf(stderr, "SDL: DirectFB_AllocHWSurface (%dx%d@%d, flags: 0x%08x)\n",
812 surface->w, surface->h, surface->format->BitsPerPixel, surface->flags);*/
814 if (surface->w < 8 || surface->h < 8)
817 /* fill surface description */
818 dsc.flags = DSDESC_WIDTH | DSDESC_HEIGHT | DSDESC_PIXELFORMAT | DSDESC_CAPS;
819 dsc.width = surface->w;
820 dsc.height = surface->h;
821 dsc.caps = (surface->flags & SDL_DOUBLEBUF) ? DSCAPS_FLIPPING : 0;
823 /* find the right pixelformat */
824 dsc.pixelformat = SDLToDFBPixelFormat (surface->format);
825 if (dsc.pixelformat == DSPF_UNKNOWN)
828 /* Allocate the hardware acceleration data */
829 surface->hwdata = (struct private_hwdata *) calloc (1, sizeof(*surface->hwdata));
830 if (surface->hwdata == NULL)
836 /* Create the surface */
837 ret = HIDDEN->dfb->CreateSurface (HIDDEN->dfb, &dsc, &surface->hwdata->surface);
840 SetDirectFBerror ("dfb->CreateSurface", ret);
841 free (surface->hwdata);
842 surface->hwdata = NULL;
846 surface->flags |= SDL_HWSURFACE | SDL_PREALLOC;
851 static void DirectFB_FreeHWSurface(_THIS, SDL_Surface *surface)
853 if (surface->hwdata && HIDDEN->initialized)
855 surface->hwdata->surface->Release (surface->hwdata->surface);
856 free (surface->hwdata);
857 surface->hwdata = NULL;
861 static int DirectFB_CheckHWBlit(_THIS, SDL_Surface *src, SDL_Surface *dst)
863 /* fprintf(stderr, "SDL: DirectFB_CheckHWBlit (src->hwdata: %p, dst->hwdata: %p)\n",
864 src->hwdata, dst->hwdata);*/
866 if (!src->hwdata || !dst->hwdata)
869 src->flags |= SDL_HWACCEL;
870 src->map->hw_blit = DirectFB_HWAccelBlit;
875 static int DirectFB_HWAccelBlit(SDL_Surface *src, SDL_Rect *srcrect,
876 SDL_Surface *dst, SDL_Rect *dstrect)
878 DFBSurfaceBlittingFlags flags = DSBLIT_NOFX;
880 DFBRectangle sr = { srcrect->x, srcrect->y, srcrect->w, srcrect->h };
881 DFBRectangle dr = { dstrect->x, dstrect->y, dstrect->w, dstrect->h };
883 IDirectFBSurface *surface = dst->hwdata->surface;
885 if (src->flags & SDL_SRCCOLORKEY)
887 flags |= DSBLIT_SRC_COLORKEY;
888 DirectFB_SetHWColorKey (NULL, src, src->format->colorkey);
891 if (src->flags & SDL_SRCALPHA)
893 flags |= DSBLIT_BLEND_COLORALPHA;
894 surface->SetColor (surface, 0xff, 0xff, 0xff, src->format->alpha);
897 surface->SetBlittingFlags (surface, flags);
899 if (sr.w == dr.w && sr.h == dr.h)
900 surface->Blit (surface, src->hwdata->surface, &sr, dr.x, dr.y);
902 surface->StretchBlit (surface, src->hwdata->surface, &sr, &dr);
907 static int DirectFB_FillHWRect(_THIS, SDL_Surface *dst, SDL_Rect *dstrect, Uint32 color)
909 SDL_PixelFormat *fmt = dst->format;
910 IDirectFBSurface *surface = dst->hwdata->surface;
913 surface->SetColor (surface,
914 (color & fmt->Rmask) >> (fmt->Rshift - fmt->Rloss),
915 (color & fmt->Gmask) >> (fmt->Gshift - fmt->Gloss),
916 (color & fmt->Bmask) << (fmt->Bloss - fmt->Bshift), 0xFF);
917 surface->FillRectangle (surface, dstrect->x, dstrect->y, dstrect->w, dstrect->h);
922 static int DirectFB_SetHWColorKey(_THIS, SDL_Surface *src, Uint32 key)
924 SDL_PixelFormat *fmt = src->format;
925 IDirectFBSurface *surface = src->hwdata->surface;
927 if (fmt->BitsPerPixel == 8)
928 surface->SetSrcColorKeyIndex (surface, key);
931 surface->SetSrcColorKey (surface,
932 (key & fmt->Rmask) >> (fmt->Rshift - fmt->Rloss),
933 (key & fmt->Gmask) >> (fmt->Gshift - fmt->Gloss),
934 (key & fmt->Bmask) << (fmt->Bloss - fmt->Bshift));
939 static int DirectFB_SetHWAlpha(_THIS, SDL_Surface *surface, Uint8 alpha)
944 static int DirectFB_FlipHWSurface(_THIS, SDL_Surface *surface)
946 if (HIDDEN->enable_mga_crtc2)
948 int rtn = surface->hwdata->surface->Flip (surface->hwdata->surface, NULL, 0);
949 if (HIDDEN->mga_crtc2_stretch)
950 HIDDEN->c2frame->StretchBlit(HIDDEN->c2frame, surface->hwdata->surface, &HIDDEN->c2ssize, &HIDDEN->c2dsize);
952 HIDDEN->c2frame->Blit(HIDDEN->c2frame, surface->hwdata->surface, NULL, HIDDEN->c2dsize.x, HIDDEN->c2dsize.y);
954 HIDDEN->c2frame->Flip(HIDDEN->c2frame, NULL, DSFLIP_WAITFORSYNC);
958 return surface->hwdata->surface->Flip (surface->hwdata->surface, NULL, DSFLIP_WAITFORSYNC);
961 static int DirectFB_LockHWSurface(_THIS, SDL_Surface *surface)
967 ret = surface->hwdata->surface->Lock (surface->hwdata->surface,
968 DSLF_WRITE, &data, &pitch);
971 SetDirectFBerror ("surface->Lock", ret);
975 surface->pixels = data;
976 surface->pitch = pitch;
981 static void DirectFB_UnlockHWSurface(_THIS, SDL_Surface *surface)
983 surface->hwdata->surface->Unlock (surface->hwdata->surface);
984 surface->pixels = NULL;
987 static void DirectFB_DirectUpdate(_THIS, int numrects, SDL_Rect *rects)
989 if (HIDDEN->enable_mga_crtc2)
991 if (HIDDEN->mga_crtc2_stretch)
992 HIDDEN->c2frame->StretchBlit(HIDDEN->c2frame, this->screen->hwdata->surface, &HIDDEN->c2ssize, &HIDDEN->c2dsize);
994 HIDDEN->c2frame->Blit(HIDDEN->c2frame, this->screen->hwdata->surface, NULL, HIDDEN->c2dsize.x, HIDDEN->c2dsize.y);
996 HIDDEN->c2frame->Flip(HIDDEN->c2frame, NULL, DSFLIP_WAITFORSYNC);
1000 static void DirectFB_WindowedUpdate(_THIS, int numrects, SDL_Rect *rects)
1004 int region_valid = 0;
1005 IDirectFBSurface *surface = this->screen->hwdata->surface;
1007 for (i=0; i<numrects; ++i)
1011 if ( ! rects[i].w ) /* Clipped? */
1014 x2 = rects[i].x + rects[i].w - 1;
1015 y2 = rects[i].y + rects[i].h - 1;
1019 if (rects[i].x < region.x1)
1020 region.x1 = rects[i].x;
1022 if (rects[i].y < region.y1)
1023 region.y1 = rects[i].y;
1033 region.x1 = rects[i].x;
1034 region.y1 = rects[i].y;
1044 if (HIDDEN->enable_mga_crtc2)
1046 if (HIDDEN->mga_crtc2_stretch)
1047 HIDDEN->c2frame->StretchBlit(HIDDEN->c2frame, surface, &HIDDEN->c2ssize, &HIDDEN->c2dsize);
1049 HIDDEN->c2frame->Blit(HIDDEN->c2frame, surface, NULL, HIDDEN->c2dsize.x, HIDDEN->c2dsize.y);
1051 HIDDEN->c2frame->Flip(HIDDEN->c2frame, NULL, DSFLIP_WAITFORSYNC);
1054 surface->Flip (surface, ®ion, DSFLIP_WAITFORSYNC);
1058 int DirectFB_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors)
1060 IDirectFBPalette *palette = this->screen->hwdata->palette;
1065 if (firstcolor > 255)
1068 if (firstcolor + ncolors > 256)
1069 ncolors = 256 - firstcolor;
1074 DFBColor entries[ncolors];
1076 for (i=0; i<ncolors; i++)
1078 entries[i].a = 0xff;
1079 entries[i].r = colors[i].r;
1080 entries[i].g = colors[i].g;
1081 entries[i].b = colors[i].b;
1084 palette->SetEntries (palette, entries, ncolors, firstcolor);
1090 void DirectFB_VideoQuit(_THIS)
1092 struct DirectFBEnumRect *rect = enumlist;
1094 if (this->screen->hwdata)
1096 IDirectFBSurface *surface = this->screen->hwdata->surface;
1097 IDirectFBPalette *palette = this->screen->hwdata->palette;
1100 palette->Release (palette);
1103 surface->Release (surface);
1105 this->screen->hwdata->surface = NULL;
1106 this->screen->hwdata->palette = NULL;
1109 if (HIDDEN->c2frame)
1111 HIDDEN->c2frame->Release (HIDDEN->c2frame);
1112 HIDDEN->c2frame = NULL;
1115 if (HIDDEN->eventbuffer)
1117 HIDDEN->eventbuffer->Release (HIDDEN->eventbuffer);
1118 HIDDEN->eventbuffer = NULL;
1121 if (HIDDEN->c2layer)
1123 HIDDEN->c2layer->Release (HIDDEN->c2layer);
1124 HIDDEN->c2layer = NULL;
1129 HIDDEN->layer->Release (HIDDEN->layer);
1130 HIDDEN->layer = NULL;
1135 HIDDEN->dfb->Release (HIDDEN->dfb);
1139 /* Free video mode list */
1140 if (HIDDEN->modelist)
1142 free (HIDDEN->modelist);
1143 HIDDEN->modelist = NULL;
1146 /* Free mode enumeration list */
1149 struct DirectFBEnumRect *next = rect->next;
1155 HIDDEN->initialized = 0;
1159 int DirectFB_ShowWMCursor(_THIS, WMcursor *cursor)
1161 /* We can only hide or show the default cursor */
1162 if ( cursor == NULL )
1164 HIDDEN->layer->SetCursorOpacity(HIDDEN->layer, 0x00);
1168 HIDDEN->layer->SetCursorOpacity(HIDDEN->layer, 0xFF);
1173 void DirectFB_FinalQuit(void)