19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL - Simple DirectMedia Layer
39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Copyright (C) 1997-2012 Sam Lantinga
49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is free software; you can redistribute it and/or
69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    modify it under the terms of the GNU Lesser General Public
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License as published by the Free Software Foundation; either
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    version 2.1 of the License, or (at your option) any later version.
99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is distributed in the hope that it will be useful,
119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    but WITHOUT ANY WARRANTY; without even the implied warranty of
129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Lesser General Public License for more details.
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    You should have received a copy of the GNU Lesser General Public
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License along with this library; if not, write to the Free Software
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Sam Lantinga
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    slouken@libsdl.org
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h"
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_video.h"
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_blit.h"
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_fbmatrox.h"
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "matrox_mmio.h"
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Wait for vertical retrace - taken from the XFree86 Matrox driver */
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void WaitVBL(_THIS)
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int count;
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* find start of retrace */
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mga_waitidle();
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	while (  (mga_in8(0x1FDA) & 0x08) )
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		;
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	while ( !(mga_in8(0x1FDA) & 0x08) )
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		;
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* wait until we're past the start */
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	count = mga_in32(0x1E20) + 2;
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	while ( mga_in32(0x1E20) < count )
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		;
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void WaitIdle(_THIS)
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mga_waitidle();
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Sets video mem colorkey and accelerated blit function */
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SetHWColorKey(_THIS, SDL_Surface *surface, Uint32 key)
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(0);
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Sets per surface hardware alpha value */
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if 0
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SetHWAlpha(_THIS, SDL_Surface *surface, Uint8 value)
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(0);
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int FillHWRect(_THIS, SDL_Surface *dst, SDL_Rect *rect, Uint32 color)
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int dstX, dstY;
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint32 fxbndry;
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint32 ydstlen;
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint32 fillop;
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Don't blit to the display surface when switched away */
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( switched_away ) {
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -2; /* no hardware access */
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( dst == this->screen ) {
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_mutexP(hw_lock);
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	switch (dst->format->BytesPerPixel) {
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case 1:
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		color |= (color<<8);
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case 2:
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		color |= (color<<16);
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set up the X/Y base coordinates */
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	FB_dst_to_xy(this, dst, &dstX, &dstY);
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Adjust for the current rectangle */
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	dstX += rect->x;
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	dstY += rect->y;
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set up the X boundaries */
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	fxbndry = (dstX | ((dstX+rect->w) << 16));
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set up the Y boundaries */
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	ydstlen = (rect->h | (dstY << 16));
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set up for color fill operation */
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	fillop = MGADWG_TRAP | MGADWG_SOLID |
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	         MGADWG_ARZERO | MGADWG_SGNZERO | MGADWG_SHIFTZERO;
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Execute the operations! */
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mga_wait(5);
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mga_out32(MGAREG_DWGCTL, fillop | MGADWG_REPLACE);
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mga_out32(MGAREG_FCOL, color);
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mga_out32(MGAREG_FXBNDRY, fxbndry);
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mga_out32(MGAREG_YDSTLEN + MGAREG_EXEC, ydstlen);
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	FB_AddBusySurface(dst);
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( dst == this->screen ) {
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_mutexV(hw_lock);
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(0);
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int HWAccelBlit(SDL_Surface *src, SDL_Rect *srcrect,
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                       SDL_Surface *dst, SDL_Rect *dstrect)
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_VideoDevice *this = current_video;
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int pitch, w, h;
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int srcX, srcY;
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int dstX, dstY;
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint32 sign;
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint32 start, stop;
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int skip;
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint32 blitop;
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* FIXME: For now, only blit to display surface */
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( dst->pitch != SDL_VideoSurface->pitch ) {
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(src->map->sw_blit(src, srcrect, dst, dstrect));
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Don't blit to the display surface when switched away */
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( switched_away ) {
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -2; /* no hardware access */
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( dst == this->screen ) {
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_mutexP(hw_lock);
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Calculate source and destination base coordinates (in pixels) */
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	w = dstrect->w;
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	h = dstrect->h;
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	FB_dst_to_xy(this, src, &srcX, &srcY);
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	FB_dst_to_xy(this, dst, &dstX, &dstY);
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Adjust for the current blit rectangles */
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	srcX += srcrect->x;
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	srcY += srcrect->y;
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	dstX += dstrect->x;
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	dstY += dstrect->y;
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	pitch = dst->pitch/dst->format->BytesPerPixel;
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set up the blit direction (sign) flags */
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	sign = 0;
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( srcX < dstX ) {
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		sign |= 1;
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( srcY < dstY ) {
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		sign |= 4;
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		srcY += (h - 1);
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		dstY += (h - 1);
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set up the blit source row start, end, and skip (in pixels) */
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	stop = start = (srcY * pitch) + srcX;
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( srcX < dstX ) {
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		start += (w - 1);
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		stop  += (w - 1);
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( srcY < dstY ) {
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		skip = -pitch;
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		skip = pitch;
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set up the blit operation */
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( (src->flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY ) {
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		Uint32 colorkey;
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		blitop = MGADWG_BFCOL | MGADWG_BITBLT |
1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		         MGADWG_SHIFTZERO | MGADWG_RSTR | (0x0C << 16) |
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		         MGADWG_TRANSC;
1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		colorkey = src->format->colorkey;
1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		switch (dst->format->BytesPerPixel) {
1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    case 1:
1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			colorkey |= (colorkey<<8);
1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    case 2:
1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			colorkey |= (colorkey<<16);
1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		mga_wait(2);
1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		mga_out32(MGAREG_FCOL, colorkey);
2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		mga_out32(MGAREG_BCOL, 0xFFFFFFFF);
2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		blitop = MGADWG_BFCOL | MGADWG_BITBLT |
2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		         MGADWG_SHIFTZERO | MGADWG_RSTR | (0x0C << 16);
2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mga_wait(7);
2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mga_out32(MGAREG_SGN, sign);
2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mga_out32(MGAREG_AR3, start);
2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mga_out32(MGAREG_AR0, stop);
2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mga_out32(MGAREG_AR5, skip);
2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mga_out32(MGAREG_FXBNDRY, (dstX | ((dstX + w-1) << 16)));
2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mga_out32(MGAREG_YDSTLEN, (dstY << 16) | h);
2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mga_out32(MGAREG_DWGCTL + MGAREG_EXEC, blitop);
2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	FB_AddBusySurface(src);
2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	FB_AddBusySurface(dst);
2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( dst == this->screen ) {
2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_mutexV(hw_lock);
2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(0);
2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int CheckHWBlit(_THIS, SDL_Surface *src, SDL_Surface *dst)
2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int accelerated;
2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set initial acceleration on */
2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	src->flags |= SDL_HWACCEL;
2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set the surface attributes */
2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( (src->flags & SDL_SRCALPHA) == SDL_SRCALPHA ) {
2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( ! this->info.blit_hw_A ) {
2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			src->flags &= ~SDL_HWACCEL;
2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( (src->flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY ) {
2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( ! this->info.blit_hw_CC ) {
2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			src->flags &= ~SDL_HWACCEL;
2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Check to see if final surface blit is accelerated */
2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	accelerated = !!(src->flags & SDL_HWACCEL);
2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( accelerated ) {
2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		src->map->hw_blit = HWAccelBlit;
2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(accelerated);
2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid FB_MatroxAccel(_THIS, __u32 card)
2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* We have hardware accelerated surface functions */
2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->CheckHWBlit = CheckHWBlit;
2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	wait_vbl = WaitVBL;
2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	wait_idle = WaitIdle;
2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* The Matrox has an accelerated color fill */
2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->info.blit_fill = 1;
2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->FillHWRect = FillHWRect;
2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* The Matrox has accelerated normal and colorkey blits. */
2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->info.blit_hw = 1;
2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* The Millenium I appears to do the colorkey test a word
2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   at a time, and the transparency is intverted. (?)
2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 */
2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( card != FB_ACCEL_MATROX_MGA2064W ) {
2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		this->info.blit_hw_CC = 1;
2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		this->SetHWColorKey = SetHWColorKey;
2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if 0 /* Not yet implemented? */
2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* The Matrox G200/G400 has an accelerated alpha blit */
2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( (card == FB_ACCEL_MATROX_MGAG200)
2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	  || (card == FB_ACCEL_MATROX_MGAG400)
2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	) {
2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		this->info.blit_hw_A = 1;
2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		this->SetHWAlpha = SetHWAlpha;
2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
281