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