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