src/video/dga/SDL_dgavideo.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 09 Nov 2002 06:20:46 +0000
changeset 540 4bcfb93e0dfe
parent 462 1be0cdaf8092
child 559 b528214c8c9a
permissions -rw-r--r--
Greatly improved X11 DGA video speed (thanks Cezary!)
slouken@0
     1
/*
slouken@0
     2
	SDL - Simple DirectMedia Layer
slouken@297
     3
	Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002  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@252
    20
	slouken@libsdl.org
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@292
    34
#include <XFree86/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@292
    90
			if ( SDL_NAME(XDGAQueryExtension)(dpy, &events, &errors) &&
slouken@292
    91
			     SDL_NAME(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@292
    96
				     SDL_NAME(XDGAOpenFramebuffer)(dpy, screen) ) {
slouken@0
    97
					available = 1;
slouken@292
    98
					SDL_NAME(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@292
   219
	extern unsigned char *SDL_NAME(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@292
   226
	mem = (unsigned long)SDL_NAME(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@292
   243
static void PrintMode(SDL_NAME(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@292
   281
    const SDL_NAME(XDGAMode) *a = (const SDL_NAME(XDGAMode) *)va;
slouken@292
   282
    const SDL_NAME(XDGAMode) *b = (const SDL_NAME(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@292
   298
static void UpdateHWInfo(_THIS, SDL_NAME(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@292
   326
	SDL_NAME(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@292
   339
	if ( ! SDL_NAME(XDGAQueryExtension)(DGA_Display, &event_base, &error_base) ||
slouken@292
   340
	     ! SDL_NAME(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@292
   380
	if ( ! SDL_NAME(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@292
   387
	modes = SDL_NAME(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@292
   432
	SDL_NAME(XDGAMode) *modes;
slouken@0
   433
	int i, num_modes;
slouken@292
   434
	SDL_NAME(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@292
   446
	modes = SDL_NAME(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@292
   471
	mode = SDL_NAME(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@292
   498
		DGA_colormap = SDL_NAME(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@292
   510
		DGA_colormap = SDL_NAME(XDGACreateColormap)(DGA_Display, DGA_Screen,
slouken@0
   511
							mode, AllocNone);
slouken@0
   512
	}
slouken@292
   513
	SDL_NAME(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@292
   523
	SDL_NAME(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
		}
slouken@0
   535
	}
slouken@0
   536
slouken@0
   537
	/* Allocate memory tracking for hardware surfaces */
slouken@0
   538
	DGA_FreeHWSurfaces(this);
slouken@0
   539
	if ( surfaces_len < 0 ) {
slouken@0
   540
		surfaces_len = 0;
slouken@0
   541
	}
slouken@101
   542
	DGA_InitHWSurfaces(this, current, surfaces_mem, surfaces_len);
slouken@0
   543
slouken@202
   544
	/* Expose the back buffer as surface memory */
slouken@202
   545
	if ( current->flags & SDL_DOUBLEBUF ) {
slouken@202
   546
		this->screen = current;
slouken@202
   547
		DGA_FlipHWSurface(this, current);
slouken@202
   548
		this->screen = NULL;
slouken@202
   549
	}
slouken@202
   550
slouken@0
   551
	/* Set the update rectangle function */
slouken@0
   552
	this->UpdateRects = DGA_DirectUpdate;
slouken@0
   553
slouken@0
   554
	/* Enable mouse and keyboard support */
slouken@0
   555
	{ long input_mask;
slouken@0
   556
	  input_mask = (KeyPressMask | KeyReleaseMask);
slouken@0
   557
	  input_mask |= (ButtonPressMask | ButtonReleaseMask);
slouken@0
   558
	  input_mask |= PointerMotionMask;
slouken@292
   559
	  SDL_NAME(XDGASelectInput)(DGA_Display, DGA_Screen, input_mask);
slouken@0
   560
	}
slouken@0
   561
slouken@0
   562
	/* We're done */
slouken@0
   563
	return(current);
slouken@0
   564
}
slouken@0
   565
slouken@0
   566
#ifdef DGA_DEBUG
slouken@0
   567
static void DGA_DumpHWSurfaces(_THIS)
slouken@0
   568
{
slouken@0
   569
	vidmem_bucket *bucket;
slouken@0
   570
slouken@0
   571
	printf("Memory left: %d (%d total)\n", surfaces_memleft, surfaces_memtotal);
slouken@0
   572
	printf("\n");
slouken@0
   573
	printf("         Base  Size\n");
slouken@0
   574
	for ( bucket=&surfaces; bucket; bucket=bucket->next ) {
slouken@0
   575
		printf("Bucket:  %p, %d (%s)\n", bucket->base, bucket->size, bucket->used ? "used" : "free");
slouken@0
   576
		if ( bucket->prev ) {
slouken@0
   577
			if ( bucket->base != bucket->prev->base+bucket->prev->size ) {
slouken@0
   578
				printf("Warning, corrupt bucket list! (prev)\n");
slouken@0
   579
			}
slouken@0
   580
		} else {
slouken@0
   581
			if ( bucket != &surfaces ) {
slouken@0
   582
				printf("Warning, corrupt bucket list! (!prev)\n");
slouken@0
   583
			}
slouken@0
   584
		}
slouken@0
   585
		if ( bucket->next ) {
slouken@0
   586
			if ( bucket->next->base != bucket->base+bucket->size ) {
slouken@0
   587
				printf("Warning, corrupt bucket list! (next)\n");
slouken@0
   588
			}
slouken@0
   589
		}
slouken@0
   590
	}
slouken@0
   591
	printf("\n");
slouken@0
   592
}
slouken@0
   593
#endif
slouken@0
   594
slouken@101
   595
static int DGA_InitHWSurfaces(_THIS, SDL_Surface *screen, Uint8 *base, int size)
slouken@0
   596
{
slouken@101
   597
	vidmem_bucket *bucket;
slouken@101
   598
slouken@0
   599
	surfaces_memtotal = size;
slouken@0
   600
	surfaces_memleft = size;
slouken@101
   601
slouken@101
   602
	if ( surfaces_memleft > 0 ) {
slouken@101
   603
		bucket = (vidmem_bucket *)malloc(sizeof(*bucket));
slouken@101
   604
		if ( bucket == NULL ) {
slouken@101
   605
			SDL_OutOfMemory();
slouken@101
   606
			return(-1);
slouken@101
   607
		}
slouken@101
   608
		bucket->prev = &surfaces;
slouken@101
   609
		bucket->used = 0;
slouken@101
   610
		bucket->dirty = 0;
slouken@101
   611
		bucket->base = base;
slouken@101
   612
		bucket->size = size;
slouken@101
   613
		bucket->next = NULL;
slouken@101
   614
	} else {
slouken@101
   615
		bucket = NULL;
slouken@101
   616
	}
slouken@101
   617
slouken@101
   618
	surfaces.prev = NULL;
slouken@101
   619
	surfaces.used = 1;
slouken@101
   620
	surfaces.dirty = 0;
slouken@101
   621
	surfaces.base = screen->pixels;
slouken@101
   622
	surfaces.size = (unsigned int)((long)base - (long)surfaces.base);
slouken@101
   623
	surfaces.next = bucket;
slouken@101
   624
	screen->hwdata = (struct private_hwdata *)&surfaces;
slouken@0
   625
	return(0);
slouken@0
   626
}
slouken@0
   627
static void DGA_FreeHWSurfaces(_THIS)
slouken@0
   628
{
slouken@0
   629
	vidmem_bucket *bucket, *freeable;
slouken@0
   630
slouken@0
   631
	bucket = surfaces.next;
slouken@0
   632
	while ( bucket ) {
slouken@0
   633
		freeable = bucket;
slouken@0
   634
		bucket = bucket->next;
slouken@0
   635
		free(freeable);
slouken@0
   636
	}
slouken@0
   637
	surfaces.next = NULL;
slouken@0
   638
}
slouken@0
   639
slouken@105
   640
static __inline__ void DGA_AddBusySurface(SDL_Surface *surface)
slouken@101
   641
{
slouken@101
   642
	((vidmem_bucket *)surface->hwdata)->dirty = 1;
slouken@101
   643
}
slouken@101
   644
slouken@105
   645
static __inline__ int DGA_IsSurfaceBusy(SDL_Surface *surface)
slouken@101
   646
{
slouken@101
   647
	return ((vidmem_bucket *)surface->hwdata)->dirty;
slouken@101
   648
}
slouken@101
   649
slouken@105
   650
static __inline__ void DGA_WaitBusySurfaces(_THIS)
slouken@101
   651
{
slouken@101
   652
	vidmem_bucket *bucket;
slouken@101
   653
slouken@101
   654
	/* Wait for graphic operations to complete */
slouken@292
   655
	SDL_NAME(XDGASync)(DGA_Display, DGA_Screen);
slouken@101
   656
slouken@101
   657
	/* Clear all surface dirty bits */
slouken@101
   658
	for ( bucket=&surfaces; bucket; bucket=bucket->next ) {
slouken@101
   659
		bucket->dirty = 0;
slouken@101
   660
	}
slouken@101
   661
}
slouken@101
   662
slouken@0
   663
static int DGA_AllocHWSurface(_THIS, SDL_Surface *surface)
slouken@0
   664
{
slouken@0
   665
	vidmem_bucket *bucket;
slouken@0
   666
	int size;
slouken@0
   667
	int extra;
slouken@101
   668
	int retval = 0;
slouken@0
   669
slouken@0
   670
/* Temporarily, we only allow surfaces the same width as display.
slouken@0
   671
   Some blitters require the pitch between two hardware surfaces
slouken@0
   672
   to be the same.  Others have interesting alignment restrictions.
slouken@0
   673
*/
slouken@0
   674
if ( surface->pitch > SDL_VideoSurface->pitch ) {
slouken@0
   675
	SDL_SetError("Surface requested wider than screen");
slouken@0
   676
	return(-1);
slouken@0
   677
}
slouken@0
   678
surface->pitch = SDL_VideoSurface->pitch;
slouken@0
   679
	size = surface->h * surface->pitch;
slouken@0
   680
#ifdef DGA_DEBUG
slouken@0
   681
	fprintf(stderr, "Allocating bucket of %d bytes\n", size);
slouken@0
   682
#endif
slouken@101
   683
	LOCK_DISPLAY();
slouken@0
   684
slouken@0
   685
	/* Quick check for available mem */
slouken@0
   686
	if ( size > surfaces_memleft ) {
slouken@0
   687
		SDL_SetError("Not enough video memory");
slouken@101
   688
		retval = -1;
slouken@101
   689
		goto done;
slouken@0
   690
	}
slouken@0
   691
slouken@0
   692
	/* Search for an empty bucket big enough */
slouken@0
   693
	for ( bucket=&surfaces; bucket; bucket=bucket->next ) {
slouken@0
   694
		if ( ! bucket->used && (size <= bucket->size) ) {
slouken@0
   695
			break;
slouken@0
   696
		}
slouken@0
   697
	}
slouken@0
   698
	if ( bucket == NULL ) {
slouken@0
   699
		SDL_SetError("Video memory too fragmented");
slouken@101
   700
		retval = -1;
slouken@101
   701
		goto done;
slouken@0
   702
	}
slouken@0
   703
slouken@0
   704
	/* Create a new bucket for left-over memory */
slouken@0
   705
	extra = (bucket->size - size);
slouken@0
   706
	if ( extra ) {
slouken@0
   707
		vidmem_bucket *newbucket;
slouken@0
   708
slouken@0
   709
#ifdef DGA_DEBUG
slouken@0
   710
	fprintf(stderr, "Adding new free bucket of %d bytes\n", extra);
slouken@0
   711
#endif
slouken@0
   712
		newbucket = (vidmem_bucket *)malloc(sizeof(*newbucket));
slouken@0
   713
		if ( newbucket == NULL ) {
slouken@0
   714
			SDL_OutOfMemory();
slouken@101
   715
			retval = -1;
slouken@101
   716
			goto done;
slouken@0
   717
		}
slouken@0
   718
		newbucket->prev = bucket;
slouken@0
   719
		newbucket->used = 0;
slouken@0
   720
		newbucket->base = bucket->base+size;
slouken@0
   721
		newbucket->size = extra;
slouken@0
   722
		newbucket->next = bucket->next;
slouken@0
   723
		if ( bucket->next ) {
slouken@0
   724
			bucket->next->prev = newbucket;
slouken@0
   725
		}
slouken@0
   726
		bucket->next = newbucket;
slouken@0
   727
	}
slouken@0
   728
slouken@0
   729
	/* Set the current bucket values and return it! */
slouken@0
   730
	bucket->used = 1;
slouken@0
   731
	bucket->size = size;
slouken@101
   732
	bucket->dirty = 0;
slouken@0
   733
#ifdef DGA_DEBUG
slouken@0
   734
	fprintf(stderr, "Allocated %d bytes at %p\n", bucket->size, bucket->base);
slouken@0
   735
#endif
slouken@0
   736
	surfaces_memleft -= size;
slouken@0
   737
	surface->flags |= SDL_HWSURFACE;
slouken@0
   738
	surface->pixels = bucket->base;
slouken@101
   739
	surface->hwdata = (struct private_hwdata *)bucket;
slouken@101
   740
done:
slouken@101
   741
	UNLOCK_DISPLAY();
slouken@101
   742
	return(retval);
slouken@0
   743
}
slouken@0
   744
static void DGA_FreeHWSurface(_THIS, SDL_Surface *surface)
slouken@0
   745
{
slouken@0
   746
	vidmem_bucket *bucket, *freeable;
slouken@0
   747
slouken@0
   748
	/* Look for the bucket in the current list */
slouken@102
   749
	for ( bucket=&surfaces; bucket; bucket=bucket->next ) {
slouken@102
   750
		if ( bucket == (vidmem_bucket *)surface->hwdata ) {
slouken@102
   751
			break;
slouken@102
   752
		}
slouken@0
   753
	}
slouken@102
   754
	if ( bucket && bucket->used ) {
slouken@102
   755
		/* Add the memory back to the total */
slouken@0
   756
#ifdef DGA_DEBUG
slouken@0
   757
	printf("Freeing bucket of %d bytes\n", bucket->size);
slouken@0
   758
#endif
slouken@102
   759
		surfaces_memleft += bucket->size;
slouken@0
   760
slouken@102
   761
		/* Can we merge the space with surrounding buckets? */
slouken@102
   762
		bucket->used = 0;
slouken@102
   763
		if ( bucket->next && ! bucket->next->used ) {
slouken@0
   764
#ifdef DGA_DEBUG
slouken@0
   765
	printf("Merging with next bucket, for %d total bytes\n", bucket->size+bucket->next->size);
slouken@0
   766
#endif
slouken@102
   767
			freeable = bucket->next;
slouken@102
   768
			bucket->size += bucket->next->size;
slouken@102
   769
			bucket->next = bucket->next->next;
slouken@102
   770
			if ( bucket->next ) {
slouken@102
   771
				bucket->next->prev = bucket;
slouken@102
   772
			}
slouken@102
   773
			free(freeable);
slouken@0
   774
		}
slouken@102
   775
		if ( bucket->prev && ! bucket->prev->used ) {
slouken@0
   776
#ifdef DGA_DEBUG
slouken@0
   777
	printf("Merging with previous bucket, for %d total bytes\n", bucket->prev->size+bucket->size);
slouken@0
   778
#endif
slouken@102
   779
			freeable = bucket;
slouken@102
   780
			bucket->prev->size += bucket->size;
slouken@102
   781
			bucket->prev->next = bucket->next;
slouken@102
   782
			if ( bucket->next ) {
slouken@102
   783
				bucket->next->prev = bucket->prev;
slouken@102
   784
			}
slouken@102
   785
			free(freeable);
slouken@0
   786
		}
slouken@0
   787
	}
slouken@0
   788
	surface->pixels = NULL;
slouken@101
   789
	surface->hwdata = NULL;
slouken@0
   790
}
slouken@0
   791
slouken@105
   792
static __inline__ void DGA_dst_to_xy(_THIS, SDL_Surface *dst, int *x, int *y)
slouken@0
   793
{
slouken@0
   794
	*x = (long)((Uint8 *)dst->pixels - memory_base)%memory_pitch;
slouken@0
   795
	*y = (long)((Uint8 *)dst->pixels - memory_base)/memory_pitch;
slouken@0
   796
}
slouken@0
   797
slouken@0
   798
static int DGA_FillHWRect(_THIS, SDL_Surface *dst, SDL_Rect *rect, Uint32 color)
slouken@0
   799
{
slouken@0
   800
	int x, y;
slouken@0
   801
	unsigned int w, h;
slouken@0
   802
slouken@0
   803
	/* Don't fill the visible part of the screen, wait until flipped */
slouken@101
   804
	LOCK_DISPLAY();
slouken@0
   805
	if ( was_flipped && (dst == this->screen) ) {
slouken@292
   806
		while ( SDL_NAME(XDGAGetViewportStatus)(DGA_Display, DGA_Screen) )
slouken@0
   807
			/* Keep waiting for the hardware ... */ ;
slouken@0
   808
		was_flipped = 0;
slouken@0
   809
	}
slouken@105
   810
	DGA_dst_to_xy(this, dst, &x, &y);
slouken@0
   811
	x += rect->x;
slouken@0
   812
	y += rect->y;
slouken@0
   813
	w = rect->w;
slouken@0
   814
	h = rect->h;
slouken@0
   815
#if 0
slouken@0
   816
  printf("Hardware accelerated rectangle fill: %dx%d at %d,%d\n", w, h, x, y);
slouken@0
   817
#endif
slouken@292
   818
	SDL_NAME(XDGAFillRectangle)(DGA_Display, DGA_Screen, x, y, w, h, color);
slouken@540
   819
	if ( !(this->screen->flags & SDL_DOUBLEBUF) ) {
slouken@540
   820
		XFlush(DGA_Display);
slouken@540
   821
	}
slouken@105
   822
	DGA_AddBusySurface(dst);
slouken@101
   823
	UNLOCK_DISPLAY();
slouken@0
   824
	return(0);
slouken@0
   825
}
slouken@0
   826
slouken@0
   827
static int HWAccelBlit(SDL_Surface *src, SDL_Rect *srcrect,
slouken@0
   828
                       SDL_Surface *dst, SDL_Rect *dstrect)
slouken@0
   829
{
slouken@0
   830
	SDL_VideoDevice *this;
slouken@0
   831
	int srcx, srcy;
slouken@0
   832
	int dstx, dsty;
slouken@0
   833
	unsigned int w, h;
slouken@0
   834
slouken@0
   835
	this = current_video;
slouken@0
   836
	/* Don't blit to the visible part of the screen, wait until flipped */
slouken@101
   837
	LOCK_DISPLAY();
slouken@0
   838
	if ( was_flipped && (dst == this->screen) ) {
slouken@292
   839
		while ( SDL_NAME(XDGAGetViewportStatus)(DGA_Display, DGA_Screen) )
slouken@0
   840
			/* Keep waiting for the hardware ... */ ;
slouken@0
   841
		was_flipped = 0;
slouken@0
   842
	}
slouken@105
   843
	DGA_dst_to_xy(this, src, &srcx, &srcy);
slouken@0
   844
	srcx += srcrect->x;
slouken@0
   845
	srcy += srcrect->y;
slouken@105
   846
	DGA_dst_to_xy(this, dst, &dstx, &dsty);
slouken@0
   847
	dstx += dstrect->x;
slouken@0
   848
	dsty += dstrect->y;
slouken@0
   849
	w = srcrect->w;
slouken@0
   850
	h = srcrect->h;
slouken@0
   851
#if 0
slouken@0
   852
  printf("Blitting %dx%d from %d,%d to %d,%d\n", w, h, srcx, srcy, dstx, dsty);
slouken@0
   853
#endif
slouken@0
   854
	if ( (src->flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY ) {
slouken@292
   855
		SDL_NAME(XDGACopyTransparentArea)(DGA_Display, DGA_Screen,
slouken@0
   856
			srcx, srcy, w, h, dstx, dsty, src->format->colorkey);
slouken@0
   857
	} else {
slouken@292
   858
		SDL_NAME(XDGACopyArea)(DGA_Display, DGA_Screen,
slouken@0
   859
			srcx, srcy, w, h, dstx, dsty);
slouken@0
   860
	}
slouken@540
   861
	if ( !(this->screen->flags & SDL_DOUBLEBUF) ) {
slouken@540
   862
		XFlush(DGA_Display);
slouken@540
   863
	}
slouken@105
   864
	DGA_AddBusySurface(src);
slouken@105
   865
	DGA_AddBusySurface(dst);
slouken@101
   866
	UNLOCK_DISPLAY();
slouken@0
   867
	return(0);
slouken@0
   868
}
slouken@0
   869
slouken@0
   870
static int DGA_CheckHWBlit(_THIS, SDL_Surface *src, SDL_Surface *dst)
slouken@0
   871
{
slouken@0
   872
	int accelerated;
slouken@0
   873
slouken@0
   874
	/* Set initial acceleration on */
slouken@0
   875
	src->flags |= SDL_HWACCEL;
slouken@0
   876
slouken@0
   877
	/* Set the surface attributes */
slouken@0
   878
	if ( (src->flags & SDL_SRCALPHA) == SDL_SRCALPHA ) {
slouken@0
   879
		if ( ! this->info.blit_hw_A ) {
slouken@0
   880
			src->flags &= ~SDL_HWACCEL;
slouken@0
   881
		}
slouken@0
   882
	}
slouken@0
   883
	if ( (src->flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY ) {
slouken@0
   884
		if ( ! this->info.blit_hw_CC ) {
slouken@0
   885
			src->flags &= ~SDL_HWACCEL;
slouken@0
   886
		}
slouken@0
   887
	}
slouken@0
   888
slouken@0
   889
	/* Check to see if final surface blit is accelerated */
slouken@0
   890
	accelerated = !!(src->flags & SDL_HWACCEL);
slouken@0
   891
	if ( accelerated ) {
slouken@0
   892
		src->map->hw_blit = HWAccelBlit;
slouken@0
   893
	}
slouken@0
   894
	return(accelerated);
slouken@0
   895
}
slouken@0
   896
slouken@101
   897
static __inline__ void DGA_WaitFlip(_THIS)
slouken@0
   898
{
slouken@0
   899
	if ( was_flipped ) {
slouken@292
   900
		while ( SDL_NAME(XDGAGetViewportStatus)(DGA_Display, DGA_Screen) )
slouken@0
   901
			/* Keep waiting for the hardware ... */ ;
slouken@0
   902
		was_flipped = 0;
slouken@0
   903
	}
slouken@0
   904
}
slouken@0
   905
slouken@0
   906
static int DGA_LockHWSurface(_THIS, SDL_Surface *surface)
slouken@0
   907
{
slouken@101
   908
	if ( surface == this->screen ) {
slouken@0
   909
		SDL_mutexP(hw_lock);
slouken@101
   910
		LOCK_DISPLAY();
slouken@105
   911
		if ( DGA_IsSurfaceBusy(surface) ) {
slouken@105
   912
			DGA_WaitBusySurfaces(this);
slouken@101
   913
		}
slouken@101
   914
		DGA_WaitFlip(this);
slouken@101
   915
		UNLOCK_DISPLAY();
slouken@101
   916
	} else {
slouken@105
   917
		if ( DGA_IsSurfaceBusy(surface) ) {
slouken@101
   918
			LOCK_DISPLAY();
slouken@105
   919
			DGA_WaitBusySurfaces(this);
slouken@101
   920
			UNLOCK_DISPLAY();
slouken@101
   921
		}
slouken@0
   922
	}
slouken@0
   923
	return(0);
slouken@0
   924
}
slouken@0
   925
static void DGA_UnlockHWSurface(_THIS, SDL_Surface *surface)
slouken@0
   926
{
slouken@101
   927
	if ( surface == this->screen ) {
slouken@0
   928
		SDL_mutexV(hw_lock);
slouken@0
   929
	}
slouken@0
   930
}
slouken@0
   931
slouken@0
   932
static int DGA_FlipHWSurface(_THIS, SDL_Surface *surface)
slouken@0
   933
{
slouken@0
   934
	/* Wait for vertical retrace and then flip display */
slouken@101
   935
	LOCK_DISPLAY();
slouken@105
   936
	if ( DGA_IsSurfaceBusy(this->screen) ) {
slouken@105
   937
		DGA_WaitBusySurfaces(this);
slouken@101
   938
	}
slouken@101
   939
	DGA_WaitFlip(this);
slouken@292
   940
	SDL_NAME(XDGASetViewport)(DGA_Display, DGA_Screen,
slouken@0
   941
	                0, flip_yoffset[flip_page], XDGAFlipRetrace);
slouken@0
   942
	XFlush(DGA_Display);
slouken@101
   943
	UNLOCK_DISPLAY();
slouken@0
   944
	was_flipped = 1;
slouken@0
   945
	flip_page = !flip_page;
slouken@0
   946
slouken@0
   947
	surface->pixels = flip_address[flip_page];
slouken@0
   948
	return(0);
slouken@0
   949
}
slouken@0
   950
slouken@0
   951
static void DGA_DirectUpdate(_THIS, int numrects, SDL_Rect *rects)
slouken@0
   952
{
slouken@0
   953
	/* The application is already updating the visible video memory */
slouken@0
   954
	return;
slouken@0
   955
}
slouken@0
   956
slouken@0
   957
static int DGA_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors)
slouken@0
   958
{
slouken@0
   959
        int i;
slouken@0
   960
	XColor  *xcmap;
slouken@0
   961
slouken@0
   962
	/* This happens on initialization */
slouken@0
   963
	if ( ! DGA_colormap ) {
slouken@0
   964
		return(0);
slouken@0
   965
	}
slouken@0
   966
	xcmap = (XColor *)alloca(ncolors*sizeof(*xcmap));
slouken@0
   967
	for ( i=0; i<ncolors; ++i ) {
slouken@0
   968
		xcmap[i].pixel = firstcolor + i;
slouken@0
   969
		xcmap[i].red   = (colors[i].r<<8)|colors[i].r;
slouken@0
   970
		xcmap[i].green = (colors[i].g<<8)|colors[i].g;
slouken@0
   971
		xcmap[i].blue  = (colors[i].b<<8)|colors[i].b;
slouken@0
   972
		xcmap[i].flags = (DoRed|DoGreen|DoBlue);
slouken@0
   973
	}
slouken@101
   974
	LOCK_DISPLAY();
slouken@0
   975
	XStoreColors(DGA_Display, DGA_colormap, xcmap, ncolors);
slouken@0
   976
	XSync(DGA_Display, False);
slouken@101
   977
	UNLOCK_DISPLAY();
slouken@0
   978
slouken@0
   979
	/* That was easy. :) */
slouken@0
   980
	return(1);
slouken@0
   981
}
slouken@0
   982
slouken@0
   983
int DGA_SetGammaRamp(_THIS, Uint16 *ramp)
slouken@0
   984
{
slouken@0
   985
	int i, ncolors;
slouken@0
   986
	XColor xcmap[256];
slouken@0
   987
slouken@0
   988
	/* See if actually setting the gamma is supported */
slouken@0
   989
	if ( DGA_visualClass != DirectColor ) {
slouken@0
   990
	    SDL_SetError("Gamma correction not supported on this visual");
slouken@0
   991
	    return(-1);
slouken@0
   992
	}
slouken@0
   993
slouken@0
   994
	/* Calculate the appropriate palette for the given gamma ramp */
slouken@0
   995
	if ( this->screen->format->BitsPerPixel <= 16 ) {
slouken@0
   996
		ncolors = 64; /* Is this right? */
slouken@0
   997
	} else {
slouken@0
   998
		ncolors = 256;
slouken@0
   999
	}
slouken@0
  1000
	for ( i=0; i<ncolors; ++i ) {
slouken@0
  1001
		Uint8 c = (256 * i / ncolors);
slouken@0
  1002
		xcmap[i].pixel = SDL_MapRGB(this->screen->format, c, c, c);
slouken@0
  1003
		xcmap[i].red   = ramp[0*256+c];
slouken@0
  1004
		xcmap[i].green = ramp[1*256+c];
slouken@0
  1005
		xcmap[i].blue  = ramp[2*256+c];
slouken@0
  1006
		xcmap[i].flags = (DoRed|DoGreen|DoBlue);
slouken@0
  1007
	}
slouken@101
  1008
	LOCK_DISPLAY();
slouken@0
  1009
	XStoreColors(DGA_Display, DGA_colormap, xcmap, ncolors);
slouken@0
  1010
	XSync(DGA_Display, False);
slouken@101
  1011
	UNLOCK_DISPLAY();
slouken@0
  1012
	return(0);
slouken@0
  1013
}
slouken@0
  1014
slouken@0
  1015
void DGA_VideoQuit(_THIS)
slouken@0
  1016
{
slouken@0
  1017
	int i, j;
slouken@0
  1018
slouken@0
  1019
	if ( DGA_Display ) {
slouken@0
  1020
		/* Free colormap, if necessary */
slouken@0
  1021
		if ( DGA_colormap ) {
slouken@0
  1022
			XFreeColormap(DGA_Display, DGA_colormap);
slouken@0
  1023
			DGA_colormap = 0;
slouken@0
  1024
		}
slouken@0
  1025
slouken@0
  1026
		/* Unmap memory and reset video mode */
slouken@292
  1027
		SDL_NAME(XDGACloseFramebuffer)(DGA_Display, DGA_Screen);
slouken@0
  1028
		if ( this->screen ) {
slouken@0
  1029
			/* Tell SDL not to free the pixels */
slouken@0
  1030
			this->screen->pixels = NULL;
slouken@0
  1031
		}
slouken@292
  1032
		SDL_NAME(XDGASetMode)(DGA_Display, DGA_Screen, 0);
slouken@0
  1033
slouken@0
  1034
		/* Clear the lock mutex */
slouken@0
  1035
		if ( hw_lock != NULL ) {
slouken@0
  1036
			SDL_DestroyMutex(hw_lock);
slouken@0
  1037
			hw_lock = NULL;
slouken@0
  1038
		}
slouken@101
  1039
#ifdef LOCK_DGA_DISPLAY
slouken@101
  1040
		if ( event_lock != NULL ) {
slouken@101
  1041
			SDL_DestroyMutex(event_lock);
slouken@101
  1042
			event_lock = NULL;
slouken@101
  1043
		}
slouken@101
  1044
#endif /* LOCK_DGA_DISPLAY */
slouken@101
  1045
slouken@0
  1046
slouken@0
  1047
		/* Clean up defined video modes */
slouken@0
  1048
		for ( i=0; i<NUM_MODELISTS; ++i ) {
slouken@0
  1049
			if ( SDL_modelist[i] != NULL ) {
slouken@0
  1050
				for ( j=0; SDL_modelist[i][j]; ++j ) {
slouken@0
  1051
					free(SDL_modelist[i][j]);
slouken@0
  1052
				}
slouken@0
  1053
				free(SDL_modelist[i]);
slouken@0
  1054
				SDL_modelist[i] = NULL;
slouken@0
  1055
			}
slouken@0
  1056
		}
slouken@0
  1057
slouken@0
  1058
		/* Clean up the memory bucket list */
slouken@0
  1059
		DGA_FreeHWSurfaces(this);
slouken@0
  1060
slouken@0
  1061
		/* Close up the display */
slouken@0
  1062
		XCloseDisplay(DGA_Display);
slouken@0
  1063
	}
slouken@0
  1064
}