src/video/dga/SDL_dgavideo.c
author Sam Lantinga <slouken@lokigames.com>
Thu, 12 Jul 2001 20:42:22 +0000
changeset 101 825b2fa28e2e
parent 91 e85e03f195b4
child 102 9162d62280b5
permissions -rw-r--r--
DGA video driver is now thread-safe
Improved DGA hardware acceleration code
slouken@0
     1
/*
slouken@0
     2
	SDL - Simple DirectMedia Layer
slouken@0
     3
	Copyright (C) 1997, 1998, 1999, 2000, 2001  Sam Lantinga
slouken@0
     4
slouken@0
     5
	This library is free software; you can redistribute it and/or
slouken@0
     6
	modify it under the terms of the GNU Library General Public
slouken@0
     7
	License as published by the Free Software Foundation; either
slouken@0
     8
	version 2 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
	This library is distributed in the hope that it will be useful,
slouken@0
    11
	but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@0
    13
	Library General Public License for more details.
slouken@0
    14
slouken@0
    15
	You should have received a copy of the GNU Library General Public
slouken@0
    16
	License along with this library; if not, write to the Free
slouken@0
    17
	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@0
    18
slouken@0
    19
	Sam Lantinga
slouken@0
    20
	slouken@devolution.com
slouken@0
    21
*/
slouken@0
    22
slouken@0
    23
#ifdef SAVE_RCSID
slouken@0
    24
static char rcsid =
slouken@0
    25
 "@(#) $Id$";
slouken@0
    26
#endif
slouken@0
    27
slouken@0
    28
/* DGA 2.0 based SDL video driver implementation.
slouken@0
    29
*/
slouken@0
    30
slouken@0
    31
#include <stdlib.h>
slouken@0
    32
#include <string.h>
slouken@0
    33
#include <X11/Xlib.h>
slouken@0
    34
#include <X11/extensions/xf86dga.h>
slouken@0
    35
slouken@0
    36
#ifdef HAVE_ALLOCA_H
slouken@0
    37
#include <alloca.h>
slouken@0
    38
#endif
slouken@0
    39
slouken@0
    40
#include "SDL.h"
slouken@0
    41
#include "SDL_error.h"
slouken@0
    42
#include "SDL_video.h"
slouken@0
    43
#include "SDL_mouse.h"
slouken@0
    44
#include "SDL_sysvideo.h"
slouken@0
    45
#include "SDL_pixels_c.h"
slouken@0
    46
#include "SDL_events_c.h"
slouken@0
    47
#include "SDL_dgavideo.h"
slouken@0
    48
#include "SDL_dgamouse_c.h"
slouken@0
    49
#include "SDL_dgaevents_c.h"
slouken@0
    50
slouken@0
    51
/* Initialization/Query functions */
slouken@0
    52
static int DGA_VideoInit(_THIS, SDL_PixelFormat *vformat);
slouken@0
    53
static SDL_Rect **DGA_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags);
slouken@0
    54
static SDL_Surface *DGA_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags);
slouken@0
    55
static int DGA_SetColors(_THIS, int firstcolor, int ncolors,
slouken@0
    56
			 SDL_Color *colors);
slouken@0
    57
static int DGA_SetGammaRamp(_THIS, Uint16 *ramp);
slouken@0
    58
static void DGA_VideoQuit(_THIS);
slouken@0
    59
slouken@0
    60
/* Hardware surface functions */
slouken@101
    61
static int DGA_InitHWSurfaces(_THIS, SDL_Surface *screen, Uint8 *base, int size);
slouken@0
    62
static void DGA_FreeHWSurfaces(_THIS);
slouken@0
    63
static int DGA_AllocHWSurface(_THIS, SDL_Surface *surface);
slouken@0
    64
static int DGA_FillHWRect(_THIS, SDL_Surface *dst, SDL_Rect *rect, Uint32 color);
slouken@0
    65
static int DGA_CheckHWBlit(_THIS, SDL_Surface *src, SDL_Surface *dst);
slouken@0
    66
static int DGA_LockHWSurface(_THIS, SDL_Surface *surface);
slouken@0
    67
static void DGA_UnlockHWSurface(_THIS, SDL_Surface *surface);
slouken@0
    68
static void DGA_FreeHWSurface(_THIS, SDL_Surface *surface);
slouken@0
    69
static int DGA_FlipHWSurface(_THIS, SDL_Surface *surface);
slouken@0
    70
slouken@0
    71
/* DGA driver bootstrap functions */
slouken@0
    72
slouken@0
    73
static int DGA_Available(void)
slouken@0
    74
{
slouken@0
    75
	const char *display;
slouken@0
    76
	Display *dpy;
slouken@0
    77
	int available;
slouken@0
    78
slouken@0
    79
	/* The driver is available is available if the display is local
slouken@0
    80
	   and the DGA 2.0+ extension is available, and we can map mem.
slouken@0
    81
	*/
slouken@0
    82
	available = 0;
slouken@0
    83
	display = NULL;
slouken@0
    84
	if ( (strncmp(XDisplayName(display), ":", 1) == 0) ||
slouken@0
    85
	     (strncmp(XDisplayName(display), "unix:", 5) == 0) ) {
slouken@0
    86
		dpy = XOpenDisplay(display);
slouken@0
    87
		if ( dpy ) {
slouken@0
    88
			int events, errors, major, minor;
slouken@0
    89
slouken@0
    90
			if ( XDGAQueryExtension(dpy, &events, &errors) &&
slouken@0
    91
			     XDGAQueryVersion(dpy, &major, &minor) ) {
slouken@0
    92
				int screen;
slouken@0
    93
slouken@0
    94
				screen = DefaultScreen(dpy);
slouken@0
    95
				if ( (major >= 2) && 
slouken@0
    96
				     XDGAOpenFramebuffer(dpy, screen) ) {
slouken@0
    97
					available = 1;
slouken@0
    98
					XDGACloseFramebuffer(dpy, screen);
slouken@0
    99
				}
slouken@0
   100
			}
slouken@0
   101
			XCloseDisplay(dpy);
slouken@0
   102
		}
slouken@0
   103
	}
slouken@0
   104
	return(available);
slouken@0
   105
}
slouken@0
   106
slouken@0
   107
static void DGA_DeleteDevice(SDL_VideoDevice *device)
slouken@0
   108
{
slouken@0
   109
	free(device->hidden);
slouken@0
   110
	free(device);
slouken@0
   111
}
slouken@0
   112
slouken@0
   113
static SDL_VideoDevice *DGA_CreateDevice(int devindex)
slouken@0
   114
{
slouken@0
   115
	SDL_VideoDevice *device;
slouken@0
   116
slouken@0
   117
	/* Initialize all variables that we clean on shutdown */
slouken@0
   118
	device = (SDL_VideoDevice *)malloc(sizeof(SDL_VideoDevice));
slouken@0
   119
	if ( device ) {
slouken@0
   120
		memset(device, 0, (sizeof *device));
slouken@0
   121
		device->hidden = (struct SDL_PrivateVideoData *)
slouken@0
   122
				malloc((sizeof *device->hidden));
slouken@0
   123
	}
slouken@0
   124
	if ( (device == NULL) || (device->hidden == NULL) ) {
slouken@0
   125
		SDL_OutOfMemory();
slouken@0
   126
		if ( device ) {
slouken@0
   127
			free(device);
slouken@0
   128
		}
slouken@0
   129
		return(0);
slouken@0
   130
	}
slouken@0
   131
	memset(device->hidden, 0, (sizeof *device->hidden));
slouken@0
   132
slouken@0
   133
	/* Set the function pointers */
slouken@0
   134
	device->VideoInit = DGA_VideoInit;
slouken@0
   135
	device->ListModes = DGA_ListModes;
slouken@0
   136
	device->SetVideoMode = DGA_SetVideoMode;
slouken@0
   137
	device->SetColors = DGA_SetColors;
slouken@0
   138
	device->UpdateRects = NULL;
slouken@0
   139
	device->VideoQuit = DGA_VideoQuit;
slouken@0
   140
	device->AllocHWSurface = DGA_AllocHWSurface;
slouken@0
   141
	device->CheckHWBlit = DGA_CheckHWBlit;
slouken@0
   142
	device->FillHWRect = DGA_FillHWRect;
slouken@0
   143
	device->SetHWColorKey = NULL;
slouken@0
   144
	device->SetHWAlpha = NULL;
slouken@0
   145
	device->LockHWSurface = DGA_LockHWSurface;
slouken@0
   146
	device->UnlockHWSurface = DGA_UnlockHWSurface;
slouken@0
   147
	device->FlipHWSurface = DGA_FlipHWSurface;
slouken@0
   148
	device->FreeHWSurface = DGA_FreeHWSurface;
slouken@0
   149
	device->SetGammaRamp = DGA_SetGammaRamp;
slouken@0
   150
	device->GetGammaRamp = NULL;
slouken@0
   151
	device->SetCaption = NULL;
slouken@0
   152
	device->SetIcon = NULL;
slouken@0
   153
	device->IconifyWindow = NULL;
slouken@0
   154
	device->GrabInput = NULL;
slouken@0
   155
	device->GetWMInfo = NULL;
slouken@0
   156
	device->InitOSKeymap = DGA_InitOSKeymap;
slouken@0
   157
	device->PumpEvents = DGA_PumpEvents;
slouken@0
   158
slouken@0
   159
	device->free = DGA_DeleteDevice;
slouken@0
   160
slouken@0
   161
	return device;
slouken@0
   162
}
slouken@0
   163
slouken@0
   164
VideoBootStrap DGA_bootstrap = {
slouken@0
   165
	"dga", "XFree86 DGA 2.0",
slouken@0
   166
	DGA_Available, DGA_CreateDevice
slouken@0
   167
};
slouken@0
   168
slouken@0
   169
static int DGA_AddMode(_THIS, int bpp, int w, int h)
slouken@0
   170
{
slouken@0
   171
	SDL_Rect *mode;
slouken@0
   172
	int i, index;
slouken@0
   173
	int next_mode;
slouken@0
   174
slouken@0
   175
	/* Check to see if we already have this mode */
slouken@0
   176
	if ( bpp < 8 ) {  /* Not supported */
slouken@0
   177
		return(0);
slouken@0
   178
	}
slouken@0
   179
	index = ((bpp+7)/8)-1;
slouken@0
   180
	for ( i=0; i<SDL_nummodes[index]; ++i ) {
slouken@0
   181
		mode = SDL_modelist[index][i];
slouken@0
   182
		if ( (mode->w == w) && (mode->h == h) ) {
slouken@0
   183
			return(0);
slouken@0
   184
		}
slouken@0
   185
	}
slouken@0
   186
slouken@0
   187
	/* Set up the new video mode rectangle */
slouken@0
   188
	mode = (SDL_Rect *)malloc(sizeof *mode);
slouken@0
   189
	if ( mode == NULL ) {
slouken@0
   190
		SDL_OutOfMemory();
slouken@0
   191
		return(-1);
slouken@0
   192
	}
slouken@0
   193
	mode->x = 0;
slouken@0
   194
	mode->y = 0;
slouken@0
   195
	mode->w = w;
slouken@0
   196
	mode->h = h;
slouken@0
   197
slouken@0
   198
	/* Allocate the new list of modes, and fill in the new mode */
slouken@0
   199
	next_mode = SDL_nummodes[index];
slouken@0
   200
	SDL_modelist[index] = (SDL_Rect **)
slouken@0
   201
	       realloc(SDL_modelist[index], (1+next_mode+1)*sizeof(SDL_Rect *));
slouken@0
   202
	if ( SDL_modelist[index] == NULL ) {
slouken@0
   203
		SDL_OutOfMemory();
slouken@0
   204
		SDL_nummodes[index] = 0;
slouken@0
   205
		free(mode);
slouken@0
   206
		return(-1);
slouken@0
   207
	}
slouken@0
   208
	SDL_modelist[index][next_mode] = mode;
slouken@0
   209
	SDL_modelist[index][next_mode+1] = NULL;
slouken@0
   210
	SDL_nummodes[index]++;
slouken@0
   211
slouken@0
   212
	return(0);
slouken@0
   213
}
slouken@0
   214
slouken@0
   215
/* This whole function is a hack. :) */
slouken@0
   216
static Uint32 get_video_size(_THIS)
slouken@0
   217
{
slouken@0
   218
	/* This is a non-exported function from libXxf86dga.a */
slouken@0
   219
	extern unsigned char *XDGAGetMappedMemory(int screen);
slouken@0
   220
	FILE *proc;
slouken@0
   221
	unsigned long mem;
slouken@0
   222
	unsigned start, stop;
slouken@0
   223
	char line[BUFSIZ];
slouken@0
   224
	Uint32 size;
slouken@0
   225
slouken@0
   226
	mem = (unsigned long)XDGAGetMappedMemory(DGA_Screen);
slouken@0
   227
	size = 0;
slouken@0
   228
	proc = fopen("/proc/self/maps", "r");
slouken@0
   229
	if ( proc ) {
slouken@0
   230
		while ( fgets(line, sizeof(line)-1, proc) ) {
slouken@0
   231
			sscanf(line, "%x-%x", &start, &stop);
slouken@0
   232
			if ( start == mem ) {
slouken@0
   233
				size = (Uint32)((stop-start)/1024);
slouken@0
   234
				break;
slouken@0
   235
			}
slouken@0
   236
		}
slouken@0
   237
		fclose(proc);
slouken@0
   238
	}
slouken@0
   239
	return(size);
slouken@0
   240
}
slouken@0
   241
slouken@0
   242
#ifdef DGA_DEBUG
slouken@0
   243
static void PrintMode(XDGAMode *mode)
slouken@0
   244
{
slouken@0
   245
	printf("Mode: %s (%dx%d) at %d bpp (%f refresh, %d pitch) num: %d\n",
slouken@0
   246
		mode->name,
slouken@0
   247
		mode->viewportWidth, mode->viewportHeight,
slouken@0
   248
		mode->depth == 24 ? mode->bitsPerPixel : mode->depth,
slouken@0
   249
		mode->verticalRefresh, mode->bytesPerScanline, mode->num);
slouken@0
   250
	printf("\tRGB: 0x%8.8x 0x%8.8x 0x%8.8x (%d - %s)\n",
slouken@0
   251
		mode->redMask, mode->greenMask, mode->blueMask,
slouken@0
   252
		mode->visualClass,
slouken@0
   253
		mode->visualClass == TrueColor ? "truecolor" :
slouken@0
   254
		mode->visualClass == DirectColor ? "directcolor" :
slouken@0
   255
		mode->visualClass == PseudoColor ? "pseudocolor" : "unknown");
slouken@0
   256
	printf("\tFlags: ");
slouken@0
   257
	if ( mode->flags & XDGAConcurrentAccess )
slouken@0
   258
		printf(" XDGAConcurrentAccess");
slouken@0
   259
	if ( mode->flags & XDGASolidFillRect )
slouken@0
   260
		printf(" XDGASolidFillRect");
slouken@0
   261
	if ( mode->flags & XDGABlitRect )
slouken@0
   262
		printf(" XDGABlitRect");
slouken@0
   263
	if ( mode->flags & XDGABlitTransRect )
slouken@0
   264
		printf(" XDGABlitTransRect");
slouken@0
   265
	if ( mode->flags & XDGAPixmap )
slouken@0
   266
		printf(" XDGAPixmap");
slouken@0
   267
	if ( mode->flags & XDGAInterlaced )
slouken@0
   268
		printf(" XDGAInterlaced");
slouken@0
   269
	if ( mode->flags & XDGADoublescan )
slouken@0
   270
		printf(" XDGADoublescan");
slouken@0
   271
	if ( mode->viewportFlags & XDGAFlipRetrace )
slouken@0
   272
		printf(" XDGAFlipRetrace");
slouken@0
   273
	if ( mode->viewportFlags & XDGAFlipImmediate )
slouken@0
   274
		printf(" XDGAFlipImmediate");
slouken@0
   275
	printf("\n");
slouken@0
   276
}
slouken@0
   277
#endif /* DGA_DEBUG */
slouken@0
   278
slouken@0
   279
static int cmpmodes(const void *va, const void *vb)
slouken@0
   280
{
slouken@91
   281
    const XDGAMode *a = (const XDGAMode *)va;
slouken@91
   282
    const XDGAMode *b = (const XDGAMode *)vb;
slouken@0
   283
slouken@0
   284
    /* Prefer DirectColor visuals for otherwise equal modes */
slouken@0
   285
    if ( (a->viewportWidth == b->viewportWidth) &&
slouken@0
   286
         (b->viewportHeight == a->viewportHeight) ) {
slouken@0
   287
         if ( a->visualClass == DirectColor )
slouken@0
   288
             return -1;
slouken@0
   289
         if ( b->visualClass == DirectColor )
slouken@0
   290
             return 1;
slouken@0
   291
         return 0;
slouken@0
   292
    } else {
slouken@0
   293
        if(a->viewportWidth > b->viewportWidth)
slouken@0
   294
            return -1;
slouken@0
   295
        return b->viewportHeight - a->viewportHeight;
slouken@0
   296
    }
slouken@0
   297
}
slouken@0
   298
static void UpdateHWInfo(_THIS, XDGAMode *mode)
slouken@0
   299
{
slouken@0
   300
	this->info.wm_available = 0;
slouken@0
   301
	this->info.hw_available = 1;
slouken@0
   302
	if ( mode->flags & XDGABlitRect ) {
slouken@0
   303
		this->info.blit_hw = 1;
slouken@0
   304
	} else {
slouken@0
   305
		this->info.blit_hw = 0;
slouken@0
   306
	}
slouken@0
   307
	if ( mode->flags & XDGABlitTransRect ) {
slouken@0
   308
		this->info.blit_hw_CC = 1;
slouken@0
   309
	} else {
slouken@0
   310
		this->info.blit_hw_CC = 0;
slouken@0
   311
	}
slouken@0
   312
	if ( mode->flags & XDGASolidFillRect ) {
slouken@0
   313
		this->info.blit_fill = 1;
slouken@0
   314
	} else {
slouken@0
   315
		this->info.blit_fill = 0;
slouken@0
   316
	}
slouken@0
   317
	this->info.video_mem = get_video_size(this);
slouken@0
   318
}
slouken@0
   319
slouken@0
   320
static int DGA_VideoInit(_THIS, SDL_PixelFormat *vformat)
slouken@0
   321
{
slouken@0
   322
	const char *display;
slouken@0
   323
	int event_base, error_base;
slouken@0
   324
	int major_version, minor_version;
slouken@0
   325
	Visual *visual;
slouken@0
   326
	XDGAMode *modes;
slouken@0
   327
	int i, num_modes;
slouken@0
   328
slouken@0
   329
	/* Open the X11 display */
slouken@0
   330
	display = NULL;		/* Get it from DISPLAY environment variable */
slouken@0
   331
slouken@0
   332
	DGA_Display = XOpenDisplay(display);
slouken@0
   333
	if ( DGA_Display == NULL ) {
slouken@0
   334
		SDL_SetError("Couldn't open X11 display");
slouken@0
   335
		return(-1);
slouken@0
   336
	}
slouken@0
   337
slouken@0
   338
	/* Check for the DGA extension */
slouken@0
   339
	if ( ! XDGAQueryExtension(DGA_Display, &event_base, &error_base) ||
slouken@0
   340
	     ! XDGAQueryVersion(DGA_Display, &major_version, &minor_version) ) {
slouken@0
   341
		SDL_SetError("DGA extension not available");
slouken@0
   342
		XCloseDisplay(DGA_Display);
slouken@0
   343
		return(-1);
slouken@0
   344
	}
slouken@0
   345
	if ( major_version < 2 ) {
slouken@0
   346
		SDL_SetError("DGA driver requires DGA 2.0 or newer");
slouken@0
   347
		XCloseDisplay(DGA_Display);
slouken@0
   348
		return(-1);
slouken@0
   349
	}
slouken@0
   350
	DGA_event_base = event_base;
slouken@0
   351
slouken@0
   352
	/* Determine the current screen depth */
slouken@0
   353
	visual = DefaultVisual(DGA_Display, DGA_Screen);
slouken@0
   354
	{
slouken@0
   355
		XPixmapFormatValues *pix_format;
slouken@0
   356
		int i, num_formats;
slouken@0
   357
slouken@0
   358
		vformat->BitsPerPixel = DefaultDepth(DGA_Display, DGA_Screen);
slouken@0
   359
		pix_format = XListPixmapFormats(DGA_Display, &num_formats);
slouken@0
   360
		if ( pix_format == NULL ) {
slouken@0
   361
			SDL_SetError("Couldn't determine screen formats");
slouken@0
   362
			XCloseDisplay(DGA_Display);
slouken@0
   363
			return(-1);
slouken@0
   364
		}
slouken@0
   365
		for ( i=0; i<num_formats; ++i ) {
slouken@0
   366
			if ( vformat->BitsPerPixel == pix_format[i].depth )
slouken@0
   367
				break;
slouken@0
   368
		}
slouken@0
   369
		if ( i != num_formats )
slouken@0
   370
			vformat->BitsPerPixel = pix_format[i].bits_per_pixel;
slouken@0
   371
		XFree((char *)pix_format);
slouken@0
   372
	}
slouken@0
   373
	if ( vformat->BitsPerPixel > 8 ) {
slouken@0
   374
		vformat->Rmask = visual->red_mask;
slouken@0
   375
		vformat->Gmask = visual->green_mask;
slouken@0
   376
		vformat->Bmask = visual->blue_mask;
slouken@0
   377
	}
slouken@0
   378
slouken@0
   379
	/* Open access to the framebuffer */
slouken@0
   380
	if ( ! XDGAOpenFramebuffer(DGA_Display, DGA_Screen) ) {
slouken@0
   381
		SDL_SetError("Unable to map the video memory");
slouken@0
   382
		XCloseDisplay(DGA_Display);
slouken@0
   383
		return(-1);
slouken@0
   384
	}
slouken@0
   385
slouken@0
   386
	/* Query for the list of available video modes */
slouken@0
   387
	modes = XDGAQueryModes(DGA_Display, DGA_Screen, &num_modes);
slouken@0
   388
	qsort(modes, num_modes, sizeof *modes, cmpmodes);
slouken@0
   389
	for ( i=0; i<num_modes; ++i ) {
slouken@0
   390
#ifdef DGA_DEBUG
slouken@0
   391
		PrintMode(&modes[i]);
slouken@0
   392
#endif
slouken@0
   393
		if ( (modes[i].visualClass == PseudoColor) ||
slouken@0
   394
		     (modes[i].visualClass == DirectColor) ||
slouken@0
   395
		     (modes[i].visualClass == TrueColor) ) {
slouken@0
   396
			DGA_AddMode(this, modes[i].bitsPerPixel,
slouken@0
   397
			            modes[i].viewportWidth,
slouken@0
   398
			            modes[i].viewportHeight);
slouken@0
   399
		}
slouken@0
   400
	}
slouken@0
   401
	UpdateHWInfo(this, modes);
slouken@0
   402
	XFree(modes);
slouken@0
   403
slouken@0
   404
	/* Create the hardware surface lock mutex */
slouken@0
   405
	hw_lock = SDL_CreateMutex();
slouken@0
   406
	if ( hw_lock == NULL ) {
slouken@0
   407
		SDL_SetError("Unable to create lock mutex");
slouken@0
   408
		DGA_VideoQuit(this);
slouken@0
   409
		return(-1);
slouken@0
   410
	}
slouken@0
   411
slouken@101
   412
#ifdef LOCK_DGA_DISPLAY
slouken@101
   413
	/* Create the event lock so we're thread-safe.. :-/ */
slouken@101
   414
	event_lock = SDL_CreateMutex();
slouken@101
   415
#endif /* LOCK_DGA_DISPLAY */
slouken@101
   416
slouken@0
   417
	/* We're done! */
slouken@0
   418
	return(0);
slouken@0
   419
}
slouken@0
   420
slouken@0
   421
SDL_Rect **DGA_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags)
slouken@0
   422
{
slouken@0
   423
	return(SDL_modelist[((format->BitsPerPixel+7)/8)-1]);
slouken@0
   424
}
slouken@0
   425
slouken@0
   426
/* Various screen update functions available */
slouken@0
   427
static void DGA_DirectUpdate(_THIS, int numrects, SDL_Rect *rects);
slouken@0
   428
slouken@0
   429
SDL_Surface *DGA_SetVideoMode(_THIS, SDL_Surface *current,
slouken@0
   430
				int width, int height, int bpp, Uint32 flags)
slouken@0
   431
{
slouken@0
   432
	XDGAMode *modes;
slouken@0
   433
	int i, num_modes;
slouken@0
   434
	XDGADevice *mode;
slouken@0
   435
	int screen_len;
slouken@0
   436
	Uint8 *surfaces_mem;
slouken@0
   437
	int surfaces_len;
slouken@0
   438
slouken@0
   439
	/* Free any previous colormap */
slouken@0
   440
	if ( DGA_colormap ) {
slouken@0
   441
		XFreeColormap(DGA_Display, DGA_colormap);
slouken@0
   442
		DGA_colormap = 0;
slouken@0
   443
	}
slouken@0
   444
slouken@0
   445
	/* Search for a matching video mode */
slouken@0
   446
	modes = XDGAQueryModes(DGA_Display, DGA_Screen, &num_modes);
slouken@0
   447
	qsort(modes, num_modes, sizeof *modes, cmpmodes);
slouken@0
   448
	for ( i=0; i<num_modes; ++i ) {
slouken@0
   449
		int depth;
slouken@0
   450
slouken@0
   451
		
slouken@0
   452
		depth = modes[i].depth;
slouken@0
   453
		if ( depth == 24 ) { /* Distinguish between 24 and 32 bpp */
slouken@0
   454
			depth = modes[i].bitsPerPixel;
slouken@0
   455
		}
slouken@0
   456
		if ( (depth == bpp) &&
slouken@0
   457
		     (modes[i].viewportWidth == width) &&
slouken@0
   458
		     (modes[i].viewportHeight == height) &&
slouken@0
   459
		     ((modes[i].visualClass == PseudoColor) ||
slouken@0
   460
		      (modes[i].visualClass == DirectColor) ||
slouken@0
   461
		      (modes[i].visualClass == TrueColor)) ) {
slouken@0
   462
			break;
slouken@0
   463
		}
slouken@0
   464
	}
slouken@0
   465
	if ( i == num_modes ) {
slouken@0
   466
		SDL_SetError("No matching video mode found");
slouken@0
   467
		return(NULL);
slouken@0
   468
	}
slouken@0
   469
slouken@0
   470
	/* Set the video mode */
slouken@0
   471
	mode = XDGASetMode(DGA_Display, DGA_Screen, modes[i].num);
slouken@0
   472
	XFree(modes);
slouken@0
   473
	if ( mode == NULL ) {
slouken@0
   474
		SDL_SetError("Unable to switch to requested mode");
slouken@0
   475
		return(NULL);
slouken@0
   476
	}
slouken@0
   477
	DGA_visualClass = modes[i].visualClass;
slouken@0
   478
	memory_base = (Uint8 *)mode->data;
slouken@0
   479
	memory_pitch = mode->mode.bytesPerScanline;
slouken@0
   480
slouken@0
   481
	/* Set up the new mode framebuffer */
slouken@0
   482
	current->flags = (SDL_FULLSCREEN|SDL_HWSURFACE);
slouken@0
   483
	current->w = mode->mode.viewportWidth;
slouken@0
   484
	current->h = mode->mode.viewportHeight;
slouken@0
   485
	current->pitch = memory_pitch;
slouken@0
   486
	current->pixels = memory_base;
slouken@0
   487
	if ( ! SDL_ReallocFormat(current, mode->mode.bitsPerPixel,
slouken@0
   488
	                                  mode->mode.redMask,
slouken@0
   489
	                                  mode->mode.greenMask,
slouken@0
   490
	                                  mode->mode.blueMask, 0) ) {
slouken@0
   491
		return(NULL);
slouken@0
   492
	}
slouken@0
   493
	screen_len = current->h*current->pitch;
slouken@0
   494
slouken@0
   495
	/* Create a colormap if necessary */
slouken@0
   496
	if ( (DGA_visualClass == PseudoColor) ||
slouken@0
   497
             (DGA_visualClass == DirectColor) ) {
slouken@0
   498
		DGA_colormap = XDGACreateColormap(DGA_Display, DGA_Screen,
slouken@0
   499
							mode, AllocAll);
slouken@0
   500
		if ( DGA_visualClass == PseudoColor ) {
slouken@0
   501
			current->flags |= SDL_HWPALETTE;
slouken@0
   502
		} else {
slouken@0
   503
	    		/* Initialize the colormap to the identity mapping */
slouken@0
   504
	    		SDL_GetGammaRamp(0, 0, 0);
slouken@0
   505
	    		this->screen = current;
slouken@0
   506
	    		DGA_SetGammaRamp(this, this->gamma);
slouken@0
   507
			this->screen = NULL;
slouken@0
   508
		}
slouken@0
   509
	} else {
slouken@0
   510
		DGA_colormap = XDGACreateColormap(DGA_Display, DGA_Screen,
slouken@0
   511
							mode, AllocNone);
slouken@0
   512
	}
slouken@0
   513
	XDGAInstallColormap(DGA_Display, DGA_Screen, DGA_colormap);
slouken@0
   514
slouken@0
   515
	/* Update the hardware capabilities */
slouken@0
   516
	UpdateHWInfo(this, &mode->mode);
slouken@0
   517
slouken@0
   518
	/* Set up the information for hardware surfaces */
slouken@0
   519
	surfaces_mem = (Uint8 *)current->pixels + screen_len;
slouken@0
   520
	surfaces_len = (mode->mode.imageHeight*current->pitch - screen_len);
slouken@0
   521
slouken@0
   522
	/* Update for double-buffering, if we can */
slouken@0
   523
	XDGASetViewport(DGA_Display, DGA_Screen, 0, 0, XDGAFlipRetrace);
slouken@0
   524
	if ( flags & SDL_DOUBLEBUF ) {
slouken@0
   525
		if ( mode->mode.imageHeight >= (current->h*2) ) {
slouken@0
   526
			current->flags |= SDL_DOUBLEBUF;
slouken@0
   527
			flip_page = 0;
slouken@0
   528
			flip_yoffset[0] = 0;
slouken@0
   529
			flip_yoffset[1] = current->h;
slouken@0
   530
			flip_address[0] = memory_base;
slouken@0
   531
			flip_address[1] = memory_base+screen_len;
slouken@0
   532
			surfaces_mem += screen_len;
slouken@0
   533
			surfaces_len -= screen_len;
slouken@0
   534
			DGA_FlipHWSurface(this, current);
slouken@0
   535
		}
slouken@0
   536
	}
slouken@0
   537
slouken@0
   538
	/* Allocate memory tracking for hardware surfaces */
slouken@0
   539
	DGA_FreeHWSurfaces(this);
slouken@0
   540
	if ( surfaces_len < 0 ) {
slouken@0
   541
		surfaces_len = 0;
slouken@0
   542
	}
slouken@101
   543
	DGA_InitHWSurfaces(this, current, surfaces_mem, surfaces_len);
slouken@0
   544
slouken@0
   545
	/* Set the update rectangle function */
slouken@0
   546
	this->UpdateRects = DGA_DirectUpdate;
slouken@0
   547
slouken@0
   548
	/* Enable mouse and keyboard support */
slouken@0
   549
	{ long input_mask;
slouken@0
   550
	  input_mask = (KeyPressMask | KeyReleaseMask);
slouken@0
   551
	  input_mask |= (ButtonPressMask | ButtonReleaseMask);
slouken@0
   552
	  input_mask |= PointerMotionMask;
slouken@0
   553
	  XDGASelectInput(DGA_Display, DGA_Screen, input_mask);
slouken@0
   554
	}
slouken@0
   555
slouken@0
   556
	/* We're done */
slouken@0
   557
	return(current);
slouken@0
   558
}
slouken@0
   559
slouken@0
   560
#ifdef DGA_DEBUG
slouken@0
   561
static void DGA_DumpHWSurfaces(_THIS)
slouken@0
   562
{
slouken@0
   563
	vidmem_bucket *bucket;
slouken@0
   564
slouken@0
   565
	printf("Memory left: %d (%d total)\n", surfaces_memleft, surfaces_memtotal);
slouken@0
   566
	printf("\n");
slouken@0
   567
	printf("         Base  Size\n");
slouken@0
   568
	for ( bucket=&surfaces; bucket; bucket=bucket->next ) {
slouken@0
   569
		printf("Bucket:  %p, %d (%s)\n", bucket->base, bucket->size, bucket->used ? "used" : "free");
slouken@0
   570
		if ( bucket->prev ) {
slouken@0
   571
			if ( bucket->base != bucket->prev->base+bucket->prev->size ) {
slouken@0
   572
				printf("Warning, corrupt bucket list! (prev)\n");
slouken@0
   573
			}
slouken@0
   574
		} else {
slouken@0
   575
			if ( bucket != &surfaces ) {
slouken@0
   576
				printf("Warning, corrupt bucket list! (!prev)\n");
slouken@0
   577
			}
slouken@0
   578
		}
slouken@0
   579
		if ( bucket->next ) {
slouken@0
   580
			if ( bucket->next->base != bucket->base+bucket->size ) {
slouken@0
   581
				printf("Warning, corrupt bucket list! (next)\n");
slouken@0
   582
			}
slouken@0
   583
		}
slouken@0
   584
	}
slouken@0
   585
	printf("\n");
slouken@0
   586
}
slouken@0
   587
#endif
slouken@0
   588
slouken@101
   589
static int DGA_InitHWSurfaces(_THIS, SDL_Surface *screen, Uint8 *base, int size)
slouken@0
   590
{
slouken@101
   591
	vidmem_bucket *bucket;
slouken@101
   592
slouken@0
   593
	surfaces_memtotal = size;
slouken@0
   594
	surfaces_memleft = size;
slouken@101
   595
slouken@101
   596
	if ( surfaces_memleft > 0 ) {
slouken@101
   597
		bucket = (vidmem_bucket *)malloc(sizeof(*bucket));
slouken@101
   598
		if ( bucket == NULL ) {
slouken@101
   599
			SDL_OutOfMemory();
slouken@101
   600
			return(-1);
slouken@101
   601
		}
slouken@101
   602
		bucket->prev = &surfaces;
slouken@101
   603
		bucket->used = 0;
slouken@101
   604
		bucket->dirty = 0;
slouken@101
   605
		bucket->base = base;
slouken@101
   606
		bucket->size = size;
slouken@101
   607
		bucket->next = NULL;
slouken@101
   608
	} else {
slouken@101
   609
		bucket = NULL;
slouken@101
   610
	}
slouken@101
   611
slouken@101
   612
	surfaces.prev = NULL;
slouken@101
   613
	surfaces.used = 1;
slouken@101
   614
	surfaces.dirty = 0;
slouken@101
   615
	surfaces.base = screen->pixels;
slouken@101
   616
	surfaces.size = (unsigned int)((long)base - (long)surfaces.base);
slouken@101
   617
	surfaces.next = bucket;
slouken@101
   618
	screen->hwdata = (struct private_hwdata *)&surfaces;
slouken@0
   619
	return(0);
slouken@0
   620
}
slouken@0
   621
static void DGA_FreeHWSurfaces(_THIS)
slouken@0
   622
{
slouken@0
   623
	vidmem_bucket *bucket, *freeable;
slouken@0
   624
slouken@0
   625
	bucket = surfaces.next;
slouken@0
   626
	while ( bucket ) {
slouken@0
   627
		freeable = bucket;
slouken@0
   628
		bucket = bucket->next;
slouken@0
   629
		free(freeable);
slouken@0
   630
	}
slouken@0
   631
	surfaces.next = NULL;
slouken@0
   632
}
slouken@0
   633
slouken@101
   634
static __inline__ void DGA_AddDirtySurface(SDL_Surface *surface)
slouken@101
   635
{
slouken@101
   636
	((vidmem_bucket *)surface->hwdata)->dirty = 1;
slouken@101
   637
}
slouken@101
   638
slouken@101
   639
static __inline__ int DGA_IsSurfaceDirty(SDL_Surface *surface)
slouken@101
   640
{
slouken@101
   641
	return ((vidmem_bucket *)surface->hwdata)->dirty;
slouken@101
   642
}
slouken@101
   643
slouken@101
   644
static __inline__ void DGA_WaitDirtySurfaces(_THIS)
slouken@101
   645
{
slouken@101
   646
	vidmem_bucket *bucket;
slouken@101
   647
slouken@101
   648
	/* Wait for graphic operations to complete */
slouken@101
   649
	XDGASync(DGA_Display, DGA_Screen);
slouken@101
   650
slouken@101
   651
	/* Clear all surface dirty bits */
slouken@101
   652
	for ( bucket=&surfaces; bucket; bucket=bucket->next ) {
slouken@101
   653
		bucket->dirty = 0;
slouken@101
   654
	}
slouken@101
   655
}
slouken@101
   656
slouken@0
   657
static int DGA_AllocHWSurface(_THIS, SDL_Surface *surface)
slouken@0
   658
{
slouken@0
   659
	vidmem_bucket *bucket;
slouken@0
   660
	int size;
slouken@0
   661
	int extra;
slouken@101
   662
	int retval = 0;
slouken@0
   663
slouken@0
   664
/* Temporarily, we only allow surfaces the same width as display.
slouken@0
   665
   Some blitters require the pitch between two hardware surfaces
slouken@0
   666
   to be the same.  Others have interesting alignment restrictions.
slouken@0
   667
*/
slouken@0
   668
if ( surface->pitch > SDL_VideoSurface->pitch ) {
slouken@0
   669
	SDL_SetError("Surface requested wider than screen");
slouken@0
   670
	return(-1);
slouken@0
   671
}
slouken@0
   672
surface->pitch = SDL_VideoSurface->pitch;
slouken@0
   673
	size = surface->h * surface->pitch;
slouken@0
   674
#ifdef DGA_DEBUG
slouken@0
   675
	fprintf(stderr, "Allocating bucket of %d bytes\n", size);
slouken@0
   676
#endif
slouken@101
   677
	LOCK_DISPLAY();
slouken@0
   678
slouken@0
   679
	/* Quick check for available mem */
slouken@0
   680
	if ( size > surfaces_memleft ) {
slouken@0
   681
		SDL_SetError("Not enough video memory");
slouken@101
   682
		retval = -1;
slouken@101
   683
		goto done;
slouken@0
   684
	}
slouken@0
   685
slouken@0
   686
	/* Search for an empty bucket big enough */
slouken@0
   687
	for ( bucket=&surfaces; bucket; bucket=bucket->next ) {
slouken@0
   688
		if ( ! bucket->used && (size <= bucket->size) ) {
slouken@0
   689
			break;
slouken@0
   690
		}
slouken@0
   691
	}
slouken@0
   692
	if ( bucket == NULL ) {
slouken@0
   693
		SDL_SetError("Video memory too fragmented");
slouken@101
   694
		retval = -1;
slouken@101
   695
		goto done;
slouken@0
   696
	}
slouken@0
   697
slouken@0
   698
	/* Create a new bucket for left-over memory */
slouken@0
   699
	extra = (bucket->size - size);
slouken@0
   700
	if ( extra ) {
slouken@0
   701
		vidmem_bucket *newbucket;
slouken@0
   702
slouken@0
   703
#ifdef DGA_DEBUG
slouken@0
   704
	fprintf(stderr, "Adding new free bucket of %d bytes\n", extra);
slouken@0
   705
#endif
slouken@0
   706
		newbucket = (vidmem_bucket *)malloc(sizeof(*newbucket));
slouken@0
   707
		if ( newbucket == NULL ) {
slouken@0
   708
			SDL_OutOfMemory();
slouken@101
   709
			retval = -1;
slouken@101
   710
			goto done;
slouken@0
   711
		}
slouken@0
   712
		newbucket->prev = bucket;
slouken@0
   713
		newbucket->used = 0;
slouken@0
   714
		newbucket->base = bucket->base+size;
slouken@0
   715
		newbucket->size = extra;
slouken@0
   716
		newbucket->next = bucket->next;
slouken@0
   717
		if ( bucket->next ) {
slouken@0
   718
			bucket->next->prev = newbucket;
slouken@0
   719
		}
slouken@0
   720
		bucket->next = newbucket;
slouken@0
   721
	}
slouken@0
   722
slouken@0
   723
	/* Set the current bucket values and return it! */
slouken@0
   724
	bucket->used = 1;
slouken@0
   725
	bucket->size = size;
slouken@101
   726
	bucket->dirty = 0;
slouken@0
   727
#ifdef DGA_DEBUG
slouken@0
   728
	fprintf(stderr, "Allocated %d bytes at %p\n", bucket->size, bucket->base);
slouken@0
   729
#endif
slouken@0
   730
	surfaces_memleft -= size;
slouken@0
   731
	surface->flags |= SDL_HWSURFACE;
slouken@0
   732
	surface->pixels = bucket->base;
slouken@101
   733
	surface->hwdata = (struct private_hwdata *)bucket;
slouken@101
   734
done:
slouken@101
   735
	UNLOCK_DISPLAY();
slouken@101
   736
	return(retval);
slouken@0
   737
}
slouken@0
   738
static void DGA_FreeHWSurface(_THIS, SDL_Surface *surface)
slouken@0
   739
{
slouken@0
   740
	vidmem_bucket *bucket, *freeable;
slouken@0
   741
slouken@0
   742
	/* Look for the bucket in the current list */
slouken@101
   743
	bucket = (vidmem_bucket *)surface->hwdata;
slouken@0
   744
	if ( (bucket == NULL) || ! bucket->used ) {
slouken@0
   745
		return;
slouken@0
   746
	}
slouken@0
   747
slouken@0
   748
	/* Add the memory back to the total */
slouken@0
   749
#ifdef DGA_DEBUG
slouken@0
   750
	printf("Freeing bucket of %d bytes\n", bucket->size);
slouken@0
   751
#endif
slouken@0
   752
	surfaces_memleft += bucket->size;
slouken@0
   753
slouken@0
   754
	/* Can we merge the space with surrounding buckets? */
slouken@0
   755
	bucket->used = 0;
slouken@0
   756
	if ( bucket->next && ! bucket->next->used ) {
slouken@0
   757
#ifdef DGA_DEBUG
slouken@0
   758
	printf("Merging with next bucket, for %d total bytes\n", bucket->size+bucket->next->size);
slouken@0
   759
#endif
slouken@0
   760
		freeable = bucket->next;
slouken@0
   761
		bucket->size += bucket->next->size;
slouken@0
   762
		bucket->next = bucket->next->next;
slouken@0
   763
		if ( bucket->next ) {
slouken@0
   764
			bucket->next->prev = bucket;
slouken@0
   765
		}
slouken@0
   766
		free(freeable);
slouken@0
   767
	}
slouken@0
   768
	if ( bucket->prev && ! bucket->prev->used ) {
slouken@0
   769
#ifdef DGA_DEBUG
slouken@0
   770
	printf("Merging with previous bucket, for %d total bytes\n", bucket->prev->size+bucket->size);
slouken@0
   771
#endif
slouken@0
   772
		freeable = bucket;
slouken@0
   773
		bucket->prev->size += bucket->size;
slouken@0
   774
		bucket->prev->next = bucket->next;
slouken@0
   775
		if ( bucket->next ) {
slouken@0
   776
			bucket->next->prev = bucket->prev;
slouken@0
   777
		}
slouken@0
   778
		free(freeable);
slouken@0
   779
	}
slouken@0
   780
	surface->pixels = NULL;
slouken@101
   781
	surface->hwdata = NULL;
slouken@0
   782
}
slouken@0
   783
slouken@0
   784
static __inline__ void dst_to_xy(_THIS, SDL_Surface *dst, int *x, int *y)
slouken@0
   785
{
slouken@0
   786
	*x = (long)((Uint8 *)dst->pixels - memory_base)%memory_pitch;
slouken@0
   787
	*y = (long)((Uint8 *)dst->pixels - memory_base)/memory_pitch;
slouken@0
   788
	if ( dst == this->screen ) {
slouken@0
   789
		*x += this->offset_x;
slouken@0
   790
		*y += this->offset_y;
slouken@0
   791
	}
slouken@0
   792
}
slouken@0
   793
slouken@0
   794
static int DGA_FillHWRect(_THIS, SDL_Surface *dst, SDL_Rect *rect, Uint32 color)
slouken@0
   795
{
slouken@0
   796
	int x, y;
slouken@0
   797
	unsigned int w, h;
slouken@0
   798
slouken@0
   799
	/* Don't fill the visible part of the screen, wait until flipped */
slouken@101
   800
	LOCK_DISPLAY();
slouken@0
   801
	if ( was_flipped && (dst == this->screen) ) {
slouken@0
   802
		while ( XDGAGetViewportStatus(DGA_Display, DGA_Screen) )
slouken@0
   803
			/* Keep waiting for the hardware ... */ ;
slouken@0
   804
		was_flipped = 0;
slouken@0
   805
	}
slouken@0
   806
	dst_to_xy(this, dst, &x, &y);
slouken@0
   807
	x += rect->x;
slouken@0
   808
	y += rect->y;
slouken@0
   809
	w = rect->w;
slouken@0
   810
	h = rect->h;
slouken@0
   811
#if 0
slouken@0
   812
  printf("Hardware accelerated rectangle fill: %dx%d at %d,%d\n", w, h, x, y);
slouken@0
   813
#endif
slouken@0
   814
	XDGAFillRectangle(DGA_Display, DGA_Screen, x, y, w, h, color);
slouken@0
   815
	XFlush(DGA_Display);
slouken@101
   816
	DGA_AddDirtySurface(dst);
slouken@101
   817
	UNLOCK_DISPLAY();
slouken@0
   818
	return(0);
slouken@0
   819
}
slouken@0
   820
slouken@0
   821
static int HWAccelBlit(SDL_Surface *src, SDL_Rect *srcrect,
slouken@0
   822
                       SDL_Surface *dst, SDL_Rect *dstrect)
slouken@0
   823
{
slouken@0
   824
	SDL_VideoDevice *this;
slouken@0
   825
	int srcx, srcy;
slouken@0
   826
	int dstx, dsty;
slouken@0
   827
	unsigned int w, h;
slouken@0
   828
slouken@0
   829
	this = current_video;
slouken@0
   830
	/* Don't blit to the visible part of the screen, wait until flipped */
slouken@101
   831
	LOCK_DISPLAY();
slouken@0
   832
	if ( was_flipped && (dst == this->screen) ) {
slouken@0
   833
		while ( XDGAGetViewportStatus(DGA_Display, DGA_Screen) )
slouken@0
   834
			/* Keep waiting for the hardware ... */ ;
slouken@0
   835
		was_flipped = 0;
slouken@0
   836
	}
slouken@0
   837
	dst_to_xy(this, src, &srcx, &srcy);
slouken@0
   838
	srcx += srcrect->x;
slouken@0
   839
	srcy += srcrect->y;
slouken@0
   840
	dst_to_xy(this, dst, &dstx, &dsty);
slouken@0
   841
	dstx += dstrect->x;
slouken@0
   842
	dsty += dstrect->y;
slouken@0
   843
	w = srcrect->w;
slouken@0
   844
	h = srcrect->h;
slouken@0
   845
#if 0
slouken@0
   846
  printf("Blitting %dx%d from %d,%d to %d,%d\n", w, h, srcx, srcy, dstx, dsty);
slouken@0
   847
#endif
slouken@0
   848
	if ( (src->flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY ) {
slouken@0
   849
		XDGACopyTransparentArea(DGA_Display, DGA_Screen,
slouken@0
   850
			srcx, srcy, w, h, dstx, dsty, src->format->colorkey);
slouken@0
   851
	} else {
slouken@0
   852
		XDGACopyArea(DGA_Display, DGA_Screen,
slouken@0
   853
			srcx, srcy, w, h, dstx, dsty);
slouken@0
   854
	}
slouken@0
   855
	XFlush(DGA_Display);
slouken@101
   856
	DGA_AddDirtySurface(src);
slouken@101
   857
	DGA_AddDirtySurface(dst);
slouken@101
   858
	UNLOCK_DISPLAY();
slouken@0
   859
	return(0);
slouken@0
   860
}
slouken@0
   861
slouken@0
   862
static int DGA_CheckHWBlit(_THIS, SDL_Surface *src, SDL_Surface *dst)
slouken@0
   863
{
slouken@0
   864
	int accelerated;
slouken@0
   865
slouken@0
   866
	/* Set initial acceleration on */
slouken@0
   867
	src->flags |= SDL_HWACCEL;
slouken@0
   868
slouken@0
   869
	/* Set the surface attributes */
slouken@0
   870
	if ( (src->flags & SDL_SRCALPHA) == SDL_SRCALPHA ) {
slouken@0
   871
		if ( ! this->info.blit_hw_A ) {
slouken@0
   872
			src->flags &= ~SDL_HWACCEL;
slouken@0
   873
		}
slouken@0
   874
	}
slouken@0
   875
	if ( (src->flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY ) {
slouken@0
   876
		if ( ! this->info.blit_hw_CC ) {
slouken@0
   877
			src->flags &= ~SDL_HWACCEL;
slouken@0
   878
		}
slouken@0
   879
	}
slouken@0
   880
slouken@0
   881
	/* Check to see if final surface blit is accelerated */
slouken@0
   882
	accelerated = !!(src->flags & SDL_HWACCEL);
slouken@0
   883
	if ( accelerated ) {
slouken@0
   884
		src->map->hw_blit = HWAccelBlit;
slouken@0
   885
	}
slouken@0
   886
	return(accelerated);
slouken@0
   887
}
slouken@0
   888
slouken@101
   889
static __inline__ void DGA_WaitFlip(_THIS)
slouken@0
   890
{
slouken@0
   891
	if ( was_flipped ) {
slouken@0
   892
		while ( XDGAGetViewportStatus(DGA_Display, DGA_Screen) )
slouken@0
   893
			/* Keep waiting for the hardware ... */ ;
slouken@0
   894
		was_flipped = 0;
slouken@0
   895
	}
slouken@0
   896
}
slouken@0
   897
slouken@0
   898
static int DGA_LockHWSurface(_THIS, SDL_Surface *surface)
slouken@0
   899
{
slouken@101
   900
	if ( surface == this->screen ) {
slouken@0
   901
		SDL_mutexP(hw_lock);
slouken@101
   902
		LOCK_DISPLAY();
slouken@101
   903
		if ( DGA_IsSurfaceDirty(surface) ) {
slouken@101
   904
			DGA_WaitDirtySurfaces(this);
slouken@101
   905
		}
slouken@101
   906
		DGA_WaitFlip(this);
slouken@101
   907
		UNLOCK_DISPLAY();
slouken@101
   908
	} else {
slouken@101
   909
		if ( DGA_IsSurfaceDirty(surface) ) {
slouken@101
   910
			LOCK_DISPLAY();
slouken@101
   911
			DGA_WaitDirtySurfaces(this);
slouken@101
   912
			UNLOCK_DISPLAY();
slouken@101
   913
		}
slouken@0
   914
	}
slouken@0
   915
	return(0);
slouken@0
   916
}
slouken@0
   917
static void DGA_UnlockHWSurface(_THIS, SDL_Surface *surface)
slouken@0
   918
{
slouken@101
   919
	if ( surface == this->screen ) {
slouken@0
   920
		SDL_mutexV(hw_lock);
slouken@0
   921
	}
slouken@0
   922
}
slouken@0
   923
slouken@0
   924
static int DGA_FlipHWSurface(_THIS, SDL_Surface *surface)
slouken@0
   925
{
slouken@0
   926
	/* Wait for vertical retrace and then flip display */
slouken@101
   927
	LOCK_DISPLAY();
slouken@101
   928
	if ( DGA_IsSurfaceDirty(this->screen) ) {
slouken@101
   929
		DGA_WaitDirtySurfaces(this);
slouken@101
   930
	}
slouken@101
   931
	DGA_WaitFlip(this);
slouken@0
   932
	XDGASetViewport(DGA_Display, DGA_Screen,
slouken@0
   933
	                0, flip_yoffset[flip_page], XDGAFlipRetrace);
slouken@0
   934
	XFlush(DGA_Display);
slouken@101
   935
	UNLOCK_DISPLAY();
slouken@0
   936
	was_flipped = 1;
slouken@0
   937
	flip_page = !flip_page;
slouken@0
   938
slouken@0
   939
	surface->pixels = flip_address[flip_page];
slouken@0
   940
	return(0);
slouken@0
   941
}
slouken@0
   942
slouken@0
   943
static void DGA_DirectUpdate(_THIS, int numrects, SDL_Rect *rects)
slouken@0
   944
{
slouken@0
   945
	/* The application is already updating the visible video memory */
slouken@0
   946
	return;
slouken@0
   947
}
slouken@0
   948
slouken@0
   949
static int DGA_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors)
slouken@0
   950
{
slouken@0
   951
        int i;
slouken@0
   952
	XColor  *xcmap;
slouken@0
   953
slouken@0
   954
	/* This happens on initialization */
slouken@0
   955
	if ( ! DGA_colormap ) {
slouken@0
   956
		return(0);
slouken@0
   957
	}
slouken@0
   958
	xcmap = (XColor *)alloca(ncolors*sizeof(*xcmap));
slouken@0
   959
	for ( i=0; i<ncolors; ++i ) {
slouken@0
   960
		xcmap[i].pixel = firstcolor + i;
slouken@0
   961
		xcmap[i].red   = (colors[i].r<<8)|colors[i].r;
slouken@0
   962
		xcmap[i].green = (colors[i].g<<8)|colors[i].g;
slouken@0
   963
		xcmap[i].blue  = (colors[i].b<<8)|colors[i].b;
slouken@0
   964
		xcmap[i].flags = (DoRed|DoGreen|DoBlue);
slouken@0
   965
	}
slouken@101
   966
	LOCK_DISPLAY();
slouken@0
   967
	XStoreColors(DGA_Display, DGA_colormap, xcmap, ncolors);
slouken@0
   968
	XSync(DGA_Display, False);
slouken@101
   969
	UNLOCK_DISPLAY();
slouken@0
   970
slouken@0
   971
	/* That was easy. :) */
slouken@0
   972
	return(1);
slouken@0
   973
}
slouken@0
   974
slouken@0
   975
int DGA_SetGammaRamp(_THIS, Uint16 *ramp)
slouken@0
   976
{
slouken@0
   977
	int i, ncolors;
slouken@0
   978
	XColor xcmap[256];
slouken@0
   979
slouken@0
   980
	/* See if actually setting the gamma is supported */
slouken@0
   981
	if ( DGA_visualClass != DirectColor ) {
slouken@0
   982
	    SDL_SetError("Gamma correction not supported on this visual");
slouken@0
   983
	    return(-1);
slouken@0
   984
	}
slouken@0
   985
slouken@0
   986
	/* Calculate the appropriate palette for the given gamma ramp */
slouken@0
   987
	if ( this->screen->format->BitsPerPixel <= 16 ) {
slouken@0
   988
		ncolors = 64; /* Is this right? */
slouken@0
   989
	} else {
slouken@0
   990
		ncolors = 256;
slouken@0
   991
	}
slouken@0
   992
	for ( i=0; i<ncolors; ++i ) {
slouken@0
   993
		Uint8 c = (256 * i / ncolors);
slouken@0
   994
		xcmap[i].pixel = SDL_MapRGB(this->screen->format, c, c, c);
slouken@0
   995
		xcmap[i].red   = ramp[0*256+c];
slouken@0
   996
		xcmap[i].green = ramp[1*256+c];
slouken@0
   997
		xcmap[i].blue  = ramp[2*256+c];
slouken@0
   998
		xcmap[i].flags = (DoRed|DoGreen|DoBlue);
slouken@0
   999
	}
slouken@101
  1000
	LOCK_DISPLAY();
slouken@0
  1001
	XStoreColors(DGA_Display, DGA_colormap, xcmap, ncolors);
slouken@0
  1002
	XSync(DGA_Display, False);
slouken@101
  1003
	UNLOCK_DISPLAY();
slouken@0
  1004
	return(0);
slouken@0
  1005
}
slouken@0
  1006
slouken@0
  1007
void DGA_VideoQuit(_THIS)
slouken@0
  1008
{
slouken@0
  1009
	int i, j;
slouken@0
  1010
slouken@0
  1011
	if ( DGA_Display ) {
slouken@0
  1012
		/* Free colormap, if necessary */
slouken@0
  1013
		if ( DGA_colormap ) {
slouken@0
  1014
			XFreeColormap(DGA_Display, DGA_colormap);
slouken@0
  1015
			DGA_colormap = 0;
slouken@0
  1016
		}
slouken@0
  1017
slouken@0
  1018
		/* Unmap memory and reset video mode */
slouken@0
  1019
		XDGACloseFramebuffer(DGA_Display, DGA_Screen);
slouken@0
  1020
		if ( this->screen ) {
slouken@0
  1021
			/* Tell SDL not to free the pixels */
slouken@0
  1022
			this->screen->pixels = NULL;
slouken@0
  1023
		}
slouken@0
  1024
		XDGASetMode(DGA_Display, DGA_Screen, 0);
slouken@0
  1025
slouken@0
  1026
		/* Clear the lock mutex */
slouken@0
  1027
		if ( hw_lock != NULL ) {
slouken@0
  1028
			SDL_DestroyMutex(hw_lock);
slouken@0
  1029
			hw_lock = NULL;
slouken@0
  1030
		}
slouken@101
  1031
#ifdef LOCK_DGA_DISPLAY
slouken@101
  1032
		if ( event_lock != NULL ) {
slouken@101
  1033
			SDL_DestroyMutex(event_lock);
slouken@101
  1034
			event_lock = NULL;
slouken@101
  1035
		}
slouken@101
  1036
#endif /* LOCK_DGA_DISPLAY */
slouken@101
  1037
slouken@0
  1038
slouken@0
  1039
		/* Clean up defined video modes */
slouken@0
  1040
		for ( i=0; i<NUM_MODELISTS; ++i ) {
slouken@0
  1041
			if ( SDL_modelist[i] != NULL ) {
slouken@0
  1042
				for ( j=0; SDL_modelist[i][j]; ++j ) {
slouken@0
  1043
					free(SDL_modelist[i][j]);
slouken@0
  1044
				}
slouken@0
  1045
				free(SDL_modelist[i]);
slouken@0
  1046
				SDL_modelist[i] = NULL;
slouken@0
  1047
			}
slouken@0
  1048
		}
slouken@0
  1049
slouken@0
  1050
		/* Clean up the memory bucket list */
slouken@0
  1051
		DGA_FreeHWSurfaces(this);
slouken@0
  1052
slouken@0
  1053
		/* Close up the display */
slouken@0
  1054
		XCloseDisplay(DGA_Display);
slouken@0
  1055
	}
slouken@0
  1056
}