📄 sdl_dgavideo.c
字号:
}
}
/* Allocate memory tracking for hardware surfaces */
DGA_FreeHWSurfaces(this);
if ( surfaces_len < 0 ) {
surfaces_len = 0;
}
DGA_InitHWSurfaces(this, current, surfaces_mem, surfaces_len);
/* Expose the back buffer as surface memory */
if ( current->flags & SDL_DOUBLEBUF ) {
this->screen = current;
DGA_FlipHWSurface(this, current);
this->screen = NULL;
}
/* Set the update rectangle function */
this->UpdateRects = DGA_DirectUpdate;
/* Enable mouse and keyboard support */
{ long input_mask;
input_mask = (KeyPressMask | KeyReleaseMask);
input_mask |= (ButtonPressMask | ButtonReleaseMask);
input_mask |= PointerMotionMask;
SDL_NAME(XDGASelectInput)(DGA_Display, DGA_Screen, input_mask);
}
/* We're done */
return(current);
}
#ifdef DGA_DEBUG
static void DGA_DumpHWSurfaces(_THIS)
{
vidmem_bucket *bucket;
printf("Memory left: %d (%d total)\n", surfaces_memleft, surfaces_memtotal);
printf("\n");
printf(" Base Size\n");
for ( bucket=&surfaces; bucket; bucket=bucket->next ) {
printf("Bucket: %p, %d (%s)\n", bucket->base, bucket->size, bucket->used ? "used" : "free");
if ( bucket->prev ) {
if ( bucket->base != bucket->prev->base+bucket->prev->size ) {
printf("Warning, corrupt bucket list! (prev)\n");
}
} else {
if ( bucket != &surfaces ) {
printf("Warning, corrupt bucket list! (!prev)\n");
}
}
if ( bucket->next ) {
if ( bucket->next->base != bucket->base+bucket->size ) {
printf("Warning, corrupt bucket list! (next)\n");
}
}
}
printf("\n");
}
#endif
static int DGA_InitHWSurfaces(_THIS, SDL_Surface *screen, Uint8 *base, int size)
{
vidmem_bucket *bucket;
surfaces_memtotal = size;
surfaces_memleft = size;
if ( surfaces_memleft > 0 ) {
bucket = (vidmem_bucket *)malloc(sizeof(*bucket));
if ( bucket == NULL ) {
SDL_OutOfMemory();
return(-1);
}
bucket->prev = &surfaces;
bucket->used = 0;
bucket->dirty = 0;
bucket->base = base;
bucket->size = size;
bucket->next = NULL;
} else {
bucket = NULL;
}
surfaces.prev = NULL;
surfaces.used = 1;
surfaces.dirty = 0;
surfaces.base = screen->pixels;
surfaces.size = (unsigned int)((long)base - (long)surfaces.base);
surfaces.next = bucket;
screen->hwdata = (struct private_hwdata *)&surfaces;
return(0);
}
static void DGA_FreeHWSurfaces(_THIS)
{
vidmem_bucket *bucket, *freeable;
bucket = surfaces.next;
while ( bucket ) {
freeable = bucket;
bucket = bucket->next;
free(freeable);
}
surfaces.next = NULL;
}
static __inline__ void DGA_AddBusySurface(SDL_Surface *surface)
{
((vidmem_bucket *)surface->hwdata)->dirty = 1;
}
static __inline__ int DGA_IsSurfaceBusy(SDL_Surface *surface)
{
return ((vidmem_bucket *)surface->hwdata)->dirty;
}
static __inline__ void DGA_WaitBusySurfaces(_THIS)
{
vidmem_bucket *bucket;
/* Wait for graphic operations to complete */
SDL_NAME(XDGASync)(DGA_Display, DGA_Screen);
/* Clear all surface dirty bits */
for ( bucket=&surfaces; bucket; bucket=bucket->next ) {
bucket->dirty = 0;
}
}
static int DGA_AllocHWSurface(_THIS, SDL_Surface *surface)
{
vidmem_bucket *bucket;
int size;
int extra;
int retval = 0;
/* Temporarily, we only allow surfaces the same width as display.
Some blitters require the pitch between two hardware surfaces
to be the same. Others have interesting alignment restrictions.
*/
if ( surface->pitch > SDL_VideoSurface->pitch ) {
SDL_SetError("Surface requested wider than screen");
return(-1);
}
surface->pitch = SDL_VideoSurface->pitch;
size = surface->h * surface->pitch;
#ifdef DGA_DEBUG
fprintf(stderr, "Allocating bucket of %d bytes\n", size);
#endif
LOCK_DISPLAY();
/* Quick check for available mem */
if ( size > surfaces_memleft ) {
SDL_SetError("Not enough video memory");
retval = -1;
goto done;
}
/* Search for an empty bucket big enough */
for ( bucket=&surfaces; bucket; bucket=bucket->next ) {
if ( ! bucket->used && (size <= bucket->size) ) {
break;
}
}
if ( bucket == NULL ) {
SDL_SetError("Video memory too fragmented");
retval = -1;
goto done;
}
/* Create a new bucket for left-over memory */
extra = (bucket->size - size);
if ( extra ) {
vidmem_bucket *newbucket;
#ifdef DGA_DEBUG
fprintf(stderr, "Adding new free bucket of %d bytes\n", extra);
#endif
newbucket = (vidmem_bucket *)malloc(sizeof(*newbucket));
if ( newbucket == NULL ) {
SDL_OutOfMemory();
retval = -1;
goto done;
}
newbucket->prev = bucket;
newbucket->used = 0;
newbucket->base = bucket->base+size;
newbucket->size = extra;
newbucket->next = bucket->next;
if ( bucket->next ) {
bucket->next->prev = newbucket;
}
bucket->next = newbucket;
}
/* Set the current bucket values and return it! */
bucket->used = 1;
bucket->size = size;
bucket->dirty = 0;
#ifdef DGA_DEBUG
fprintf(stderr, "Allocated %d bytes at %p\n", bucket->size, bucket->base);
#endif
surfaces_memleft -= size;
surface->flags |= SDL_HWSURFACE;
surface->pixels = bucket->base;
surface->hwdata = (struct private_hwdata *)bucket;
done:
UNLOCK_DISPLAY();
return(retval);
}
static void DGA_FreeHWSurface(_THIS, SDL_Surface *surface)
{
vidmem_bucket *bucket, *freeable;
/* Look for the bucket in the current list */
for ( bucket=&surfaces; bucket; bucket=bucket->next ) {
if ( bucket == (vidmem_bucket *)surface->hwdata ) {
break;
}
}
if ( bucket && bucket->used ) {
/* Add the memory back to the total */
#ifdef DGA_DEBUG
printf("Freeing bucket of %d bytes\n", bucket->size);
#endif
surfaces_memleft += bucket->size;
/* Can we merge the space with surrounding buckets? */
bucket->used = 0;
if ( bucket->next && ! bucket->next->used ) {
#ifdef DGA_DEBUG
printf("Merging with next bucket, for %d total bytes\n", bucket->size+bucket->next->size);
#endif
freeable = bucket->next;
bucket->size += bucket->next->size;
bucket->next = bucket->next->next;
if ( bucket->next ) {
bucket->next->prev = bucket;
}
free(freeable);
}
if ( bucket->prev && ! bucket->prev->used ) {
#ifdef DGA_DEBUG
printf("Merging with previous bucket, for %d total bytes\n", bucket->prev->size+bucket->size);
#endif
freeable = bucket;
bucket->prev->size += bucket->size;
bucket->prev->next = bucket->next;
if ( bucket->next ) {
bucket->next->prev = bucket->prev;
}
free(freeable);
}
}
surface->pixels = NULL;
surface->hwdata = NULL;
}
static __inline__ void DGA_dst_to_xy(_THIS, SDL_Surface *dst, int *x, int *y)
{
*x = (long)((Uint8 *)dst->pixels - memory_base)%memory_pitch;
*y = (long)((Uint8 *)dst->pixels - memory_base)/memory_pitch;
if ( dst == this->screen ) {
*x += this->offset_x;
*y += this->offset_y;
}
}
static int DGA_FillHWRect(_THIS, SDL_Surface *dst, SDL_Rect *rect, Uint32 color)
{
int x, y;
unsigned int w, h;
/* Don't fill the visible part of the screen, wait until flipped */
LOCK_DISPLAY();
if ( was_flipped && (dst == this->screen) ) {
while ( SDL_NAME(XDGAGetViewportStatus)(DGA_Display, DGA_Screen) )
/* Keep waiting for the hardware ... */ ;
was_flipped = 0;
}
DGA_dst_to_xy(this, dst, &x, &y);
x += rect->x;
y += rect->y;
w = rect->w;
h = rect->h;
#if 0
printf("Hardware accelerated rectangle fill: %dx%d at %d,%d\n", w, h, x, y);
#endif
SDL_NAME(XDGAFillRectangle)(DGA_Display, DGA_Screen, x, y, w, h, color);
XFlush(DGA_Display);
DGA_AddBusySurface(dst);
UNLOCK_DISPLAY();
return(0);
}
static int HWAccelBlit(SDL_Surface *src, SDL_Rect *srcrect,
SDL_Surface *dst, SDL_Rect *dstrect)
{
SDL_VideoDevice *this;
int srcx, srcy;
int dstx, dsty;
unsigned int w, h;
this = current_video;
/* Don't blit to the visible part of the screen, wait until flipped */
LOCK_DISPLAY();
if ( was_flipped && (dst == this->screen) ) {
while ( SDL_NAME(XDGAGetViewportStatus)(DGA_Display, DGA_Screen) )
/* Keep waiting for the hardware ... */ ;
was_flipped = 0;
}
DGA_dst_to_xy(this, src, &srcx, &srcy);
srcx += srcrect->x;
srcy += srcrect->y;
DGA_dst_to_xy(this, dst, &dstx, &dsty);
dstx += dstrect->x;
dsty += dstrect->y;
w = srcrect->w;
h = srcrect->h;
#if 0
printf("Blitting %dx%d from %d,%d to %d,%d\n", w, h, srcx, srcy, dstx, dsty);
#endif
if ( (src->flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY ) {
SDL_NAME(XDGACopyTransparentArea)(DGA_Display, DGA_Screen,
srcx, srcy, w, h, dstx, dsty, src->format->colorkey);
} else {
SDL_NAME(XDGACopyArea)(DGA_Display, DGA_Screen,
srcx, srcy, w, h, dstx, dsty);
}
XFlush(DGA_Display);
DGA_AddBusySurface(src);
DGA_AddBusySurface(dst);
UNLOCK_DISPLAY();
return(0);
}
static int DGA_CheckHWBlit(_THIS, SDL_Surface *src, SDL_Surface *dst)
{
int accelerated;
/* Set initial acceleration on */
src->flags |= SDL_HWACCEL;
/* Set the surface attributes */
if ( (src->flags & SDL_SRCALPHA) == SDL_SRCALPHA ) {
if ( ! this->info.blit_hw_A ) {
src->flags &= ~SDL_HWACCEL;
}
}
if ( (src->flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY ) {
if ( ! this->info.blit_hw_CC ) {
src->flags &= ~SDL_HWACCEL;
}
}
/* Check to see if final surface blit is accelerated */
accelerated = !!(src->flags & SDL_HWACCEL);
if ( accelerated ) {
src->map->hw_blit = HWAccelBlit;
}
return(accelerated);
}
static __inline__ void DGA_WaitFlip(_THIS)
{
if ( was_flipped ) {
while ( SDL_NAME(XDGAGetViewportStatus)(DGA_Display, DGA_Screen) )
/* Keep waiting for the hardware ... */ ;
was_flipped = 0;
}
}
static int DGA_LockHWSurface(_THIS, SDL_Surface *surface)
{
if ( surface == this->screen ) {
SDL_mutexP(hw_lock);
LOCK_DISPLAY();
if ( DGA_IsSurfaceBusy(surface) ) {
DGA_WaitBusySurfaces(this);
}
DGA_WaitFlip(this);
UNLOCK_DISPLAY();
} else {
if ( DGA_IsSurfaceBusy(surface) ) {
LOCK_DISPLAY();
DGA_WaitBusySurfaces(this);
UNLOCK_DISPLAY();
}
}
return(0);
}
static void DGA_UnlockHWSurface(_THIS, SDL_Surface *surface)
{
if ( surface == this->screen ) {
SDL_mutexV(hw_lock);
}
}
static int DGA_FlipHWSurface(_THIS, SDL_Surface *surface)
{
/* Wait for vertical retrace and then flip display */
LOCK_DISPLAY();
if ( DGA_IsSurfaceBusy(this->screen) ) {
DGA_WaitBusySurfaces(this);
}
DGA_WaitFlip(this);
SDL_NAME(XDGASetViewport)(DGA_Display, DGA_Screen,
0, flip_yoffset[flip_page], XDGAFlipRetrace);
XFlush(DGA_Display);
UNLOCK_DISPLAY();
was_flipped = 1;
flip_page = !flip_page;
surface->pixels = flip_address[flip_page];
return(0);
}
static void DGA_DirectUpdate(_THIS, int numrects, SDL_Rect *rects)
{
/* The application is already updating the visible video memory */
return;
}
static int DGA_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors)
{
int i;
XColor *xcmap;
/* This happens on initialization */
if ( ! DGA_colormap ) {
return(0);
}
xcmap = (XColor *)alloca(ncolors*sizeof(*xcmap));
for ( i=0; i<ncolors; ++i ) {
xcmap[i].pixel = firstcolor + i;
xcmap[i].red = (colors[i].r<<8)|colors[i].r;
xcmap[i].green = (colors[i].g<<8)|colors[i].g;
xcmap[i].blue = (colors[i].b<<8)|colors[i].b;
xcmap[i].flags = (DoRed|DoGreen|DoBlue);
}
LOCK_DISPLAY();
XStoreColors(DGA_Display, DGA_colormap, xcmap, ncolors);
XSync(DGA_Display, False);
UNLOCK_DISPLAY();
/* That was easy. :) */
return(1);
}
int DGA_SetGammaRamp(_THIS, Uint16 *ramp)
{
int i, ncolors;
XColor xcmap[256];
/* See if actually setting the gamma is supported */
if ( DGA_visualClass != DirectColor ) {
SDL_SetError("Gamma correction not supported on this visual");
return(-1);
}
/* Calculate the appropriate palette for the given gamma ramp */
if ( this->screen->format->BitsPerPixel <= 16 ) {
ncolors = 64; /* Is this right? */
} else {
ncolors = 256;
}
for ( i=0; i<ncolors; ++i ) {
Uint8 c = (256 * i / ncolors);
xcmap[i].pixel = SDL_MapRGB(this->screen->format, c, c, c);
xcmap[i].red = ramp[0*256+c];
xcmap[i].green = ramp[1*256+c];
xcmap[i].blue = ramp[2*256+c];
xcmap[i].flags = (DoRed|DoGreen|DoBlue);
}
LOCK_DISPLAY();
XStoreColors(DGA_Display, DGA_colormap, xcmap, ncolors);
XSync(DGA_Display, False);
UNLOCK_DISPLAY();
return(0);
}
void DGA_VideoQuit(_THIS)
{
int i, j;
if ( DGA_Display ) {
/* Free colormap, if necessary */
if ( DGA_colormap ) {
XFreeColormap(DGA_Display, DGA_colormap);
DGA_colormap = 0;
}
/* Unmap memory and reset video mode */
SDL_NAME(XDGACloseFramebuffer)(DGA_Display, DGA_Screen);
if ( this->screen ) {
/* Tell SDL not to free the pixels */
this->screen->pixels = NULL;
}
SDL_NAME(XDGASetMode)(DGA_Display, DGA_Screen, 0);
/* Clear the lock mutex */
if ( hw_lock != NULL ) {
SDL_DestroyMutex(hw_lock);
hw_lock = NULL;
}
#ifdef LOCK_DGA_DISPLAY
if ( event_lock != NULL ) {
SDL_DestroyMutex(event_lock);
event_lock = NULL;
}
#endif /* LOCK_DGA_DISPLAY */
/* Clean up defined video modes */
for ( i=0; i<NUM_MODELISTS; ++i ) {
if ( SDL_modelist[i] != NULL ) {
for ( j=0; SDL_modelist[i][j]; ++j ) {
free(SDL_modelist[i][j]);
}
free(SDL_modelist[i]);
SDL_modelist[i] = NULL;
}
}
/* Clean up the memory bucket list */
DGA_FreeHWSurfaces(this);
/* Close up the display */
XCloseDisplay(DGA_Display);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -