sis300_accel.c 35.7 KB
Newer Older
1
/* $XFree86$ */
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
2
/*
3
 * 2D Acceleration for SiS 530, 620, 300, 540, 630, 730.
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
4
 *
5
 * Copyright (C) 2001-2004 by Thomas Winischhofer, Vienna, Austria
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
6
 *
7 8 9 10 11 12 13 14
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1) Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2) Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
15
 * 3) The name of the author may not be used to endorse or promote products
16
 *    derived from this software without specific prior written permission.
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
17
 *
18 19 20 21 22 23 24 25 26 27
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESSED OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
28
 *
29 30
 * Authors:   Thomas Winischhofer <thomas@winischhofer.net>
 *	      Can-Ru Yeou, SiS Inc.
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
31 32
 *
 */
33

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
34 35 36 37 38 39 40 41 42 43 44
#if 0
#define DEBUG
#endif

#include "xf86.h"
#include "xf86_OSproc.h"
#include "xf86_ansic.h"
#include "xf86PciInfo.h"
#include "xf86Pci.h"
#include "compiler.h"
#include "xaa.h"
45
#include "xaalocal.h"
46
#include "xaarop.h"
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179

#include "sis.h"
#include "sis300_accel.h"

#ifdef SISDUALHEAD
/* TW: This is the offset to the memory for each head */
#define HEADOFFSET 	(pSiS->dhmOffset)
#endif

#undef STSCE    	/* TW: Use/Don't use ScreenToScreenColorExpand - does not work */

#undef TRAP     	/* TW: Use/Don't use Trapezoid Fills - does not work - XAA provides
		         * illegal trapezoid data (left and right edges cross each other
			 * sometimes) which causes drawing errors. Further, I have not found
			 * out how to draw polygones with a height greater than 127...
                         */

static void SiSInitializeAccelerator(ScrnInfoPtr pScrn);
static void SiSSync(ScrnInfoPtr pScrn);
static void SiSSetupForScreenToScreenCopy(ScrnInfoPtr pScrn,
                                int xdir, int ydir, int rop,
                                unsigned int planemask, int trans_color);
static void SiSSubsequentScreenToScreenCopy(ScrnInfoPtr pScrn,
                                int x1, int y1, int x2, int y2,
                                int width, int height);
static void SiSSetupForSolidFill(ScrnInfoPtr pScrn, int color,
                                int rop, unsigned int planemask);
static void SiSSubsequentSolidFillRect(ScrnInfoPtr pScrn,
                                int x, int y, int w, int h);
#ifdef TRAP
static void SiSSubsequentSolidFillTrap(ScrnInfoPtr pScrn, int y, int h,
	                        int left, int dxL, int dyL, int eL,
	                        int right, int dxR, int dyR, int eR);
#endif
static void SiSSetupForSolidLine(ScrnInfoPtr pScrn, int color,
                                int rop, unsigned int planemask);
static void SiSSubsequentSolidTwoPointLine(ScrnInfoPtr pScrn, int x1,
                                int y1, int x2, int y2, int flags);
static void SiSSubsequentSolidHorzVertLine(ScrnInfoPtr pScrn,
                                int x, int y, int len, int dir);
static void SiSSetupForDashedLine(ScrnInfoPtr pScrn,
                                int fg, int bg, int rop, unsigned int planemask,
                                int length, unsigned char *pattern);
static void SiSSubsequentDashedTwoPointLine(ScrnInfoPtr pScrn,
                                int x1, int y1, int x2, int y2,
                                int flags, int phase);
static void SiSSetupForMonoPatternFill(ScrnInfoPtr pScrn,
                                int patx, int paty, int fg, int bg,
                                int rop, unsigned int planemask);
static void SiSSubsequentMonoPatternFill(ScrnInfoPtr pScrn,
                                int patx, int paty,
                                int x, int y, int w, int h);
#ifdef TRAP
static void SiSSubsequentMonoPatternFillTrap(ScrnInfoPtr pScrn,
                                int patx, int paty,
                                int y, int h,
                                int left, int dxL, int dyL, int eL,
	                        int right, int dxR, int dyR, int eR );
#endif
#if 0
static void SiSSetupForColorPatternFill(ScrnInfoPtr pScrn,
                                int patx, int paty, int rop,
                                unsigned int planemask,
                                int trans_color);
static void SiSSubsequentColorPatternFill(ScrnInfoPtr pScrn,
                                int patx, int paty,
                                int x, int y, int w, int h);
#endif
#if 0
static void SiSSetupForCPUToScreenColorExpand(ScrnInfoPtr pScrn,
                                int fg, int bg,
                                int rop, unsigned int planemask);
static void SiSSubsequentCPUToScreenColorExpand(ScrnInfoPtr pScrn,
                                int x, int y, int w, int h, int skipleft);
#endif
#ifdef STSCE
static void SiSSetupForScreenToScreenColorExpand(ScrnInfoPtr pScrn,
                                int fg, int bg,
                                int rop, unsigned int planemask);
static void SiSSubsequentScreenToScreenColorExpand(ScrnInfoPtr pScrn,
                                int x, int y, int w, int h,
                                int srcx, int srcy, int skipleft);
#endif
static void SiSSetupForScanlineCPUToScreenColorExpandFill(ScrnInfoPtr pScrn,
                                int fg, int bg, int rop,
                                unsigned int planemask);
static void SiSSubsequentScanlineCPUToScreenColorExpandFill(ScrnInfoPtr pScrn,
                                int x, int y, int w, int h,
                                int skipleft);
static void SiSSubsequentColorExpandScanline(ScrnInfoPtr pScrn, int bufno);

#ifdef SISDUALHEAD
static void SiSRestoreAccelState(ScrnInfoPtr pScrn);
#endif

static void
SiSInitializeAccelerator(ScrnInfoPtr pScrn)
{
	SISPtr  pSiS = SISPTR(pScrn);

	pSiS->DoColorExpand = FALSE;
}

Bool
SiS300AccelInit(ScreenPtr pScreen)
{
	XAAInfoRecPtr   infoPtr;
	ScrnInfoPtr     pScrn = xf86Screens[pScreen->myNum];
	SISPtr          pSiS = SISPTR(pScrn);
	int		topFB;
	int             reservedFbSize;
	int             UsableFbSize;
	unsigned char   *AvailBufBase;
	BoxRec          Avail;
	int             i;

	pSiS->AccelInfoPtr = infoPtr = XAACreateInfoRec();
	if (!infoPtr)  return FALSE;

	SiSInitializeAccelerator(pScrn);

	infoPtr->Flags = LINEAR_FRAMEBUFFER |
			 OFFSCREEN_PIXMAPS |
			 PIXMAP_CACHE;

	/* sync */
	infoPtr->Sync = SiSSync;

	/* Acceleration only supported at 8, 16 and 32 bpp */
	if((pScrn->bitsPerPixel != 8) && (pScrn->bitsPerPixel != 16) &&
	         (pScrn->bitsPerPixel != 32))
	    return FALSE;

180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
	/* TW: Although SiS states that the 300 series supports a
	 *     virtual framebuffer of 4096x4096, the 2D accelerator
	 *     does not seem to know that. If the destination bitmap
	 *     pitch is > 8192 (which easily happens in 32bpp mode),
	 *     the accelerator engine collapses.
	 *     TODO: Find out about the 530 and 620
	 */

	if(pSiS->scrnOffset < 8192) {

	   /* screen to screen copy */
	   infoPtr->SetupForScreenToScreenCopy = SiSSetupForScreenToScreenCopy;
	   infoPtr->SubsequentScreenToScreenCopy = SiSSubsequentScreenToScreenCopy;
	   infoPtr->ScreenToScreenCopyFlags = NO_PLANEMASK |
	                                      TRANSPARENCY_GXCOPY_ONLY;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
195

196 197 198
	   /* solid fills */
	   infoPtr->SetupForSolidFill = SiSSetupForSolidFill;
	   infoPtr->SubsequentSolidFillRect = SiSSubsequentSolidFillRect;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
199
#ifdef TRAP
200
	   infoPtr->SubsequentSolidFillTrap = SiSSubsequentSolidFillTrap;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
201
#endif
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
	   infoPtr->SolidFillFlags = NO_PLANEMASK;

	   /* solid line */
	   infoPtr->SetupForSolidLine = SiSSetupForSolidLine;
	   infoPtr->SubsequentSolidTwoPointLine = SiSSubsequentSolidTwoPointLine;
	   infoPtr->SubsequentSolidHorVertLine = SiSSubsequentSolidHorzVertLine;
	   infoPtr->SolidLineFlags = NO_PLANEMASK;

	   /* dashed line */
	   infoPtr->SetupForDashedLine = SiSSetupForDashedLine;
	   infoPtr->SubsequentDashedTwoPointLine = SiSSubsequentDashedTwoPointLine;
	   infoPtr->DashPatternMaxLength = 64;
	   infoPtr->DashedLineFlags = NO_PLANEMASK |
	  			      LINE_PATTERN_MSBFIRST_LSBJUSTIFIED;

	   /* 8x8 mono pattern fill */
	   infoPtr->SetupForMono8x8PatternFill = SiSSetupForMonoPatternFill;
	   infoPtr->SubsequentMono8x8PatternFillRect = SiSSubsequentMonoPatternFill;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
220
#ifdef TRAP
221
	   infoPtr->SubsequentMono8x8PatternFillTrap = SiSSubsequentMonoPatternFillTrap;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
222
#endif
223 224 225 226 227
	   infoPtr->Mono8x8PatternFillFlags = NO_PLANEMASK |
					      HARDWARE_PATTERN_SCREEN_ORIGIN |
					      HARDWARE_PATTERN_PROGRAMMED_BITS |
					      /* NO_TRANSPARENCY | */
					      BIT_ORDER_IN_BYTE_MSBFIRST ;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
228 229

#ifdef STSCE
230 231 232
	   /* Screen To Screen Color Expand */
	   /* TW: The hardware does support this the way we need it */
	   infoPtr->SetupForScreenToScreenColorExpandFill =
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
233
	    			SiSSetupForScreenToScreenColorExpand;
234
	   infoPtr->SubsequentScreenToScreenColorExpandFill =
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
235
	    			SiSSubsequentScreenToScreenColorExpand;
236
	   infoPtr->ScreenToScreenColorExpandFillFlags = NO_PLANEMASK |
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
237 238 239 240
	                                              BIT_ORDER_IN_BYTE_MSBFIRST ;
#endif

#if 0
241 242 243 244 245 246 247 248 249 250 251 252 253
	   /* CPU To Screen Color Expand --- implement another instead of this one! */
	   infoPtr->SetupForCPUToScreenColorExpandFill =
	       SiSSetupForCPUToScreenColorExpand;
	   infoPtr->SubsequentCPUToScreenColorExpandFill =
	       SiSSubsequentCPUToScreenColorExpand;
	   infoPtr->ColorExpandRange = PATREGSIZE;
	   infoPtr->ColorExpandBase = pSiS->IOBase+PBR(0);
	   infoPtr->CPUToScreenColorExpandFillFlags = NO_PLANEMASK |
	   					      BIT_ORDER_IN_BYTE_MSBFIRST |
	    					      NO_TRANSPARENCY |
	    					      SYNC_AFTER_COLOR_EXPAND |
	    					      HARDWARE_PATTERN_SCREEN_ORIGIN |
	    					      HARDWARE_PATTERN_PROGRAMMED_BITS ;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
254 255
#endif

256 257 258 259 260 261 262 263 264 265 266
	   /* per-scanline color expansion (using indirect method) */
	   if(pSiS->VGAEngine == SIS_530_VGA) {
	      pSiS->ColorExpandBufferNumber = 4;
	      pSiS->ColorExpandBufferCountMask = 0x03;
	   } else {
	      pSiS->ColorExpandBufferNumber = 16;
	      pSiS->ColorExpandBufferCountMask = 0x0F;
	   }
	   pSiS->PerColorExpandBufferSize = ((pScrn->virtualX + 31)/32) * 4;
	   infoPtr->NumScanlineColorExpandBuffers = pSiS->ColorExpandBufferNumber;
	   infoPtr->ScanlineColorExpandBuffers = (unsigned char **)&pSiS->ColorExpandBufferAddr[0];
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
267

268
	   infoPtr->SetupForScanlineCPUToScreenColorExpandFill =
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
269
	                            SiSSetupForScanlineCPUToScreenColorExpandFill;
270
	   infoPtr->SubsequentScanlineCPUToScreenColorExpandFill =
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
271
	                            SiSSubsequentScanlineCPUToScreenColorExpandFill;
272
	   infoPtr->SubsequentColorExpandScanline =
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
273
	                            SiSSubsequentColorExpandScanline;
274 275 276 277 278 279 280 281 282 283 284 285
	   infoPtr->ScanlineCPUToScreenColorExpandFillFlags = NO_PLANEMASK |
	    						      CPU_TRANSFER_PAD_DWORD |
	    						      SCANLINE_PAD_DWORD |
	    						      BIT_ORDER_IN_BYTE_MSBFIRST |
	    						      LEFT_EDGE_CLIPPING;
        } else {
	   xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
	   	"Virtual screen width too large for accelerator engine\n");
           xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
	   	"2D acceleration and Xv disabled\n");
           pSiS->NoXvideo = TRUE;
	}
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
286 287

#ifdef SISDUALHEAD
288 289
	if(pSiS->DualHeadMode) {
	   infoPtr->RestoreAccelState = SiSRestoreAccelState;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
	}
#endif

	/* init Frame Buffer Manager */
	topFB = pSiS->maxxfbmem;

	reservedFbSize = pSiS->ColorExpandBufferNumber * pSiS->PerColorExpandBufferSize;

	UsableFbSize = topFB - reservedFbSize;

	/* Layout: (Sizes do not reflect correct proportions)
	 * |--------------++++++++++++++++++++^************==========~~~~~~~~~~~~|
	 *   UsableFbSize  ColorExpandBuffers | DRI-Heap  |  HWCursor  TurboQueue   300/310/325 series
	 * |--------------++++++++++++++++++++|  ====================~~~~~~~~~~~~|
	 *   UsableFbSize  ColorExpandBuffers |        TurboQueue     HWCursor      530/620
	 *                                  topFB
	 */

	AvailBufBase = pSiS->FbBase + UsableFbSize;
309 310 311 312
	for(i = 0; i < pSiS->ColorExpandBufferNumber; i++) {
	   pSiS->ColorExpandBufferAddr[i] = AvailBufBase +
	 	    i * pSiS->PerColorExpandBufferSize;
	   pSiS->ColorExpandBufferScreenOffset[i] = UsableFbSize +
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
313 314 315 316 317 318 319 320 321 322 323 324
		    i * pSiS->PerColorExpandBufferSize;
	}
	Avail.x1 = 0;
	Avail.y1 = 0;
	Avail.x2 = pScrn->displayWidth;
	Avail.y2 = (UsableFbSize / (pScrn->displayWidth * pScrn->bitsPerPixel/8)) - 1;

	if(Avail.y2 < 0)  Avail.y2 = 32767;

	if(Avail.y2 < pScrn->currentMode->VDisplay) {
		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
			"Not enough video RAM for accelerator. At least "
325
			"%dKB needed, %ldKB available\n",
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
			((((pScrn->displayWidth * pScrn->bitsPerPixel/8)   /* TW: +8 for make it sure */
			     * pScrn->currentMode->VDisplay) + reservedFbSize) / 1024) + 8,
			pSiS->maxxfbmem/1024);
		pSiS->NoAccel = TRUE;
		pSiS->NoXvideo = TRUE;
		XAADestroyInfoRec(pSiS->AccelInfoPtr);
		pSiS->AccelInfoPtr = NULL;
		return FALSE;
	}

	xf86DrvMsg(pScrn->scrnIndex, X_INFO,
		   "Frame Buffer From (%d,%d) To (%d,%d)\n",
		   Avail.x1, Avail.y1, Avail.x2, Avail.y2);

	xf86InitFBManager(pScreen, &Avail);

	return(XAAInit(pScreen, infoPtr));
}


static void 
SiSSync(ScrnInfoPtr pScrn)
{
	SISPtr pSiS = SISPTR(pScrn);

	PDEBUG(ErrorF("SiSSync()\n"));

	pSiS->DoColorExpand = FALSE;
	SiSIdle
}

#ifdef SISDUALHEAD
static void
SiSRestoreAccelState(ScrnInfoPtr pScrn)
{
	SISPtr pSiS = SISPTR(pScrn);

	/* TW: We don't need to do anything special here; forcing the
	 *     other head to re-read the CmdQueLen is not necessary:
	 *     After the Sync in RestoreAccelState(), the real queue
	 *     length is always larger than (or at least equal to)
	 *     the amount stored in CmdQueueLen of the other head,
	 *     so the only thing that might happen is one unnecessary
	 *     Sync on the other head. I think we can live with that.
	 */
	pSiS->DoColorExpand = FALSE;
	SiSIdle
}
#endif

static void SiSSetupForScreenToScreenCopy(ScrnInfoPtr pScrn,
                                int xdir, int ydir, int rop,
                                unsigned int planemask, int trans_color)
{
	SISPtr  pSiS = SISPTR(pScrn);

	PDEBUG(ErrorF("Setup ScreenCopy(%d, %d, 0x%x, 0x%x, 0x%x)\n",
					xdir, ydir, rop, planemask, trans_color));

	SiSSetupDSTColorDepth(pSiS->DstColor);
	SiSSetupSRCPitch(pSiS->scrnOffset)
	SiSSetupDSTRect(pSiS->scrnOffset, -1)

	if(trans_color != -1) {
390 391 392
	   SiSSetupROP(0x0A)
	   SiSSetupSRCTrans(trans_color)
	   SiSSetupCMDFlag(TRANSPARENT_BITBLT)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
393
	} else {
394
	   SiSSetupROP(XAACopyROP[rop])
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
395 396
	}
	if(xdir > 0) {
397
	   SiSSetupCMDFlag(X_INC)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
398 399
	}
	if(ydir > 0) {
400
	   SiSSetupCMDFlag(Y_INC)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
	}
}

static void SiSSubsequentScreenToScreenCopy(ScrnInfoPtr pScrn,
                                int src_x, int src_y, int dst_x, int dst_y,
                                int width, int height)
{
	SISPtr  pSiS = SISPTR(pScrn);
	long srcbase, dstbase;

	PDEBUG(ErrorF("Subsequent ScreenCopy(%d,%d, %d,%d, %d,%d)\n",
					src_x, src_y, dst_x, dst_y, width, height));

	srcbase = dstbase = 0;
	if(src_y >= 2048) {
416 417
	   srcbase = pSiS->scrnOffset * src_y;
	   src_y = 0;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
418
	}
419 420 421
	if((dst_y >= pScrn->virtualY) || (dst_y >= 2048)) {
	   dstbase = pSiS->scrnOffset * dst_y;
	   dst_y = 0;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
422 423 424
	}
#ifdef SISDUALHEAD
	if(pSiS->VGAEngine != SIS_530_VGA) {
425 426
	   srcbase += HEADOFFSET;
	   dstbase += HEADOFFSET;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
427 428 429 430 431 432
	}
#endif
	SiSSetupSRCBase(srcbase);
	SiSSetupDSTBase(dstbase);

	if(!(pSiS->CommandReg & X_INC))  {
433 434
	   src_x += width-1;
	   dst_x += width-1;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
435 436
	}
	if(!(pSiS->CommandReg & Y_INC))  {
437 438
	   src_y += height-1;
	   dst_y += height-1;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
	}
	SiSSetupRect(width, height)
	SiSSetupSRCXY(src_x, src_y)
	SiSSetupDSTXY(dst_x, dst_y)

	SiSDoCMD
}

static void
SiSSetupForSolidFill(ScrnInfoPtr pScrn,
                        int color, int rop, unsigned int planemask)
{
	SISPtr  pSiS = SISPTR(pScrn);

	PDEBUG(ErrorF("Setup SolidFill(0x%x, 0x%x, 0x%x)\n",
					color, rop, planemask));

456 457 458 459 460
	if(pSiS->disablecolorkeycurrent) {
	   if((CARD32)color == pSiS->colorKey) {
	      rop = 5;  /* NOOP */
	   }
	}
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
461 462 463
	SiSSetupPATFG(color)
	SiSSetupDSTRect(pSiS->scrnOffset, -1)
	SiSSetupDSTColorDepth(pSiS->DstColor);
464
	SiSSetupROP(XAAPatternROP[rop])
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
465 466 467 468 469 470 471 472 473 474 475 476 477 478
	/* SiSSetupCMDFlag(PATFG) - is zero */
}

static void
SiSSubsequentSolidFillRect(ScrnInfoPtr pScrn,
                        int x, int y, int w, int h)
{
	SISPtr  pSiS = SISPTR(pScrn);
	long dstbase;

	PDEBUG(ErrorF("Subsequent SolidFillRect(%d, %d, %d, %d)\n",
					x, y, w, h));
	dstbase = 0;

479 480 481
	if(y >= 2048) {
	   dstbase = pSiS->scrnOffset * y;
	   y = 0;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
482 483 484
	}
#ifdef SISDUALHEAD
        if(pSiS->VGAEngine != SIS_530_VGA) {
485
	   dstbase += HEADOFFSET;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
        }
#endif
	SiSSetupDSTBase(dstbase)
	SiSSetupDSTXY(x,y)
	SiSSetupRect(w,h)
	/* Clear commandReg because Setup can be used for Rect and Trap */
	pSiS->CommandReg &= ~(T_XISMAJORL | T_XISMAJORR |
	                      T_L_X_INC | T_L_Y_INC |
	                      T_R_X_INC | T_R_Y_INC |
			      TRAPAZOID_FILL);
	SiSSetupCMDFlag(X_INC | Y_INC | BITBLT)

	SiSDoCMD
}

/* TW: Trapezoid */
/* This would work better if XAA would provide us with valid trapezoids.
 * In fact, with small trapezoids the left and the right edge often cross
 * each other or result in a line length of 0 which causes drawing errors
 * (filling over whole scanline).
 * Furthermore, I have not found out how to draw trapezoids with a height
 * greater than 127.
 */
#ifdef TRAP
static void
SiSSubsequentSolidFillTrap(ScrnInfoPtr pScrn, int y, int h,
	       int left,  int dxL, int dyL, int eL,
	       int right, int dxR, int dyR, int eR )
{
	SISPtr  pSiS = SISPTR(pScrn);
	long dstbase;
#if 0
	float kL, kR;
#endif

	dstbase = 0;
522 523 524
	if(y >= 2048) {
	   dstbase=pSiS->scrnOffset*y;
	   y = 0;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
525 526 527
	}
#ifdef SISDUALHEAD
	if(pSiS->VGAEngine != SIS_530_VGA) {
528
	   dstbase += HEADOFFSET;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
	}
#endif
	SiSSetupDSTBase(dstbase)
	/* SiSSetupRect(w,h) */

#if 1
	SiSSetupPATFG(0xff0000) /* FOR TESTING */
#endif

	/* Clear CommandReg because SetUp can be used for Rect and Trap */
	pSiS->CommandReg &= ~(T_L_X_INC | T_L_Y_INC |
	                      T_R_X_INC | T_R_Y_INC |
	                      T_XISMAJORL | T_XISMAJORR |
			      BITBLT);

        xf86DrvMsg(0, X_INFO, "Trap (%d %d %d %d) dxL %d dyL %d eL %d   dxR %d dyR %d eR %d\n",
		left, right, y, h, dxL, dyL, eL, dxR, dyR, eR);

	/* Unfortunately, we must check if the right and the left edge
	 * cross each other...  INCOMPLETE (line equation wrong)
	 */
#if 0
	if (dxL == 0) kL = 0;
	else kL = (float)dyL / (float)dxL;
	if (dxR == 0) kR = 0;
	else kR = (float)dyR / (float)dxR;
	xf86DrvMsg(0, X_INFO, "kL %f kR %f!\n", kL, kR);
	if ( (kR != kL) &&
	     (!(kR == 0 && kL == 0)) &&
	     (!(kR <  0 && kL >  0)) ) {
	   xf86DrvMsg(0, X_INFO, "Inside if (%f - %d)\n", ( kL * ( ( ((float)right - (float)left) / (kL - kR) ) - left) + y), h+y);
           if ( ( ( kL * ( ( ((float)right - (float)left) / (kL - kR) ) - (float)left) + (float)y) < (h + y) ) ) {
	     xf86DrvMsg(0, X_INFO, "Cross detected!\n");
	   }
	}
#endif

	/* Determine egde angles */
567
	if(dxL < 0) { dxL = -dxL; }
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
568
	else { SiSSetupCMDFlag(T_L_X_INC) }
569
	if(dxR < 0) { dxR = -dxR; }
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
570 571 572
	else { SiSSetupCMDFlag(T_R_X_INC) }

	/* (Y direction always positive - do this anyway) */
573
	if(dyL < 0) { dyL = -dyL; }
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
574
	else { SiSSetupCMDFlag(T_L_Y_INC) }
575
	if(dyR < 0) { dyR = -dyR; }
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
576 577 578
	else { SiSSetupCMDFlag(T_R_Y_INC) }

	/* Determine major axis */
579 580
	if(dxL >= dyL) {      /* X is major axis */
	   SiSSetupCMDFlag(T_XISMAJORL)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
581
	}
582 583
	if(dxR >= dyR) {      /* X is major axis */
	   SiSSetupCMDFlag(T_XISMAJORR)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
	}

	/* Set up deltas */
	SiSSetupdL(dxL, dyL)
	SiSSetupdR(dxR, dyR)

#if 0   /* Could it be that this crappy engine can only draw trapezoids up to 127 pixels high? */
	h &= 0x7F;
	if (h == 0) h = 10;
#endif

	/* Set up y, h, left, right */
	SiSSetupYH(y,h)
	SiSSetupLR(left,right)

	/* Set up initial error term */
	SiSSetupEL(eL)
	SiSSetupER(eR)

	SiSSetupCMDFlag(TRAPAZOID_FILL);

	SiSDoCMD
}
#endif

static void
SiSSetupForSolidLine(ScrnInfoPtr pScrn,
                        int color, int rop, unsigned int planemask)
{
	SISPtr  pSiS = SISPTR(pScrn);

	PDEBUG(ErrorF("Setup SolidLine(0x%x, 0x%x, 0x%x)\n",
					color, rop, planemask));

	SiSSetupLineCount(1)
	SiSSetupPATFG(color)
	SiSSetupDSTRect(pSiS->scrnOffset, -1)
	SiSSetupDSTColorDepth(pSiS->DstColor);
622
	SiSSetupROP(XAAPatternROP[rop])
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
	SiSSetupCMDFlag(PATFG | LINE)
}

static void
SiSSubsequentSolidTwoPointLine(ScrnInfoPtr pScrn,
                        int x1, int y1, int x2, int y2, int flags)
{
	SISPtr  pSiS = SISPTR(pScrn);
	long dstbase,miny,maxy;

	PDEBUG(ErrorF("Subsequent SolidLine(%d, %d, %d, %d, 0x%x)\n",
					x1, y1, x2, y2, flags));

	dstbase = 0;
	miny = (y1 > y2) ? y2 : y1;
	maxy = (y1 > y2) ? y1 : y2;
	if(maxy >= 2048) {
640 641 642
	   dstbase = pSiS->scrnOffset * miny;
	   y1 -= miny;
	   y2 -= miny;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
643 644 645 646 647 648 649 650 651 652
	}
#ifdef SISDUALHEAD
	if(pSiS->VGAEngine != SIS_530_VGA) {
	   dstbase += HEADOFFSET;
	}
#endif
	SiSSetupDSTBase(dstbase)

	SiSSetupX0Y0(x1,y1)
	SiSSetupX1Y1(x2,y2)
653 654
	if(flags & OMIT_LAST) {
	   SiSSetupCMDFlag(NO_LAST_PIXEL)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
655
	} else {
656
	   pSiS->CommandReg &= ~(NO_LAST_PIXEL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
	}

	SiSDoCMD
}

static void
SiSSubsequentSolidHorzVertLine(ScrnInfoPtr pScrn,
                                int x, int y, int len, int dir)
{
	SISPtr  pSiS = SISPTR(pScrn);
	long dstbase;

	PDEBUG(ErrorF("Subsequent SolidHorzVertLine(%d, %d, %d, %d)\n",
					x, y, len, dir));
	len--; /* starting point is included! */

	dstbase = 0;
674 675 676
	if((y >= 2048) || ((dir != DEGREES_0) && ((y + len) >= 2048))) {
	   dstbase = pSiS->scrnOffset * y;
	   y = 0;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
677 678 679
	}
#ifdef SISDUALHEAD
	if(pSiS->VGAEngine != SIS_530_VGA) {
680
	   dstbase += HEADOFFSET;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
681 682 683 684 685
	}
#endif
	SiSSetupDSTBase(dstbase)

	SiSSetupX0Y0(x,y)
686 687
	if(dir == DEGREES_0) {
	   SiSSetupX1Y1(x + len, y);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
688
	} else {
689
	   SiSSetupX1Y1(x, y + len);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
	}

	SiSDoCMD
}

static void
SiSSetupForDashedLine(ScrnInfoPtr pScrn,
                                int fg, int bg, int rop, unsigned int planemask,
                                int length, unsigned char *pattern)
{
	SISPtr pSiS = SISPTR(pScrn);

	PDEBUG(ErrorF("Setup DashedLine(0x%x, 0x%x, 0x%x, 0x%x, %d, 0x%x:%x)\n",
			fg, bg, rop, planemask, length, *(pattern+4), *pattern));

	SiSSetupLineCount(1)
	SiSSetupDSTRect(pSiS->scrnOffset, -1)
	SiSSetupDSTColorDepth(pSiS->DstColor);
	SiSSetupStyleLow(*pattern)
	SiSSetupStyleHigh(*(pattern+4))
710
	SiSSetupStylePeriod(length-1);
711
	SiSSetupROP(XAAPatternROP[rop])
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
712
	SiSSetupPATFG(fg)
713
	SiSSetupCMDFlag(LINE | LINE_STYLE)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
714
	if(bg != -1) {
715
	   SiSSetupPATBG(bg)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
716
	} else {
717
	   SiSSetupCMDFlag(TRANSPARENT);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735
	}
}

static void
SiSSubsequentDashedTwoPointLine(ScrnInfoPtr pScrn,
                                int x1, int y1, int x2, int y2,
                                int flags, int phase)
{
	SISPtr pSiS = SISPTR(pScrn);
	long dstbase,miny,maxy;

	PDEBUG(ErrorF("Subsequent DashedLine(%d,%d, %d,%d, 0x%x,0x%x)\n",
			x1, y1, x2, y2, flags, phase));

	dstbase = 0;
	miny = (y1 > y2) ? y2 : y1;
	maxy = (y1 > y2) ? y1 : y2;
	if(maxy >= 2048) {
736 737 738
	   dstbase = pSiS->scrnOffset * miny;
	   y1 -= miny;
	   y2 -= miny;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
739 740 741 742 743 744 745 746 747 748 749
	}
#ifdef SISDUALHEAD
	if(pSiS->VGAEngine != SIS_530_VGA) {
	   dstbase += HEADOFFSET;
	}
#endif
	SiSSetupDSTBase(dstbase)

	SiSSetupX0Y0(x1,y1)
	SiSSetupX1Y1(x2,y2)
	if(flags & OMIT_LAST) {
750
	   SiSSetupCMDFlag(NO_LAST_PIXEL)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
751
	} else {
752
	   pSiS->CommandReg &= ~(NO_LAST_PIXEL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
	}

	SiSDoCMD
}

static void
SiSSetupForMonoPatternFill(ScrnInfoPtr pScrn,
                                int patx, int paty, int fg, int bg,
                                int rop, unsigned int planemask)
{
	SISPtr pSiS = SISPTR(pScrn);

	PDEBUG(ErrorF("Setup MonoPatFill(0x%x,0x%x, 0x%x,0x%x, 0x%x, 0x%x)\n",
					patx, paty, fg, bg, rop, planemask));
	SiSSetupDSTRect(pSiS->scrnOffset, -1)
	SiSSetupDSTColorDepth(pSiS->DstColor);
	SiSSetupMONOPAT(patx,paty)
	SiSSetupPATFG(fg)
771
	SiSSetupROP(XAAPatternROP[rop])
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
772
	SiSSetupCMDFlag(PATMONO)
773 774 775 776 777
	if(bg != -1) {
	   SiSSetupPATBG(bg)
	} else {
	   SiSSetupCMDFlag(TRANSPARENT)
	}
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
778 779 780 781 782 783 784 785 786 787 788 789 790 791
}

static void
SiSSubsequentMonoPatternFill(ScrnInfoPtr pScrn,
                                int patx, int paty,
                                int x, int y, int w, int h)
{
	SISPtr pSiS = SISPTR(pScrn);
	long dstbase;

	PDEBUG(ErrorF("Subsequent MonoPatFill(0x%x,0x%x, %d,%d, %d,%d)\n",
							patx, paty, x, y, w, h));
	dstbase = 0;

792 793 794
	if(y >= 2048) {
	   dstbase = pSiS->scrnOffset * y;
	   y = 0;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829
	}
#ifdef SISDUALHEAD
	if(pSiS->VGAEngine != SIS_530_VGA) {
	   dstbase += HEADOFFSET;
	}
#endif
	SiSSetupDSTBase(dstbase)
	SiSSetupDSTXY(x, y)
	SiSSetupRect(w, h)
	/* Clear commandReg because Setup can be used for Rect and Trap */
	pSiS->CommandReg &= ~(T_XISMAJORL | T_XISMAJORR |
	                      T_L_X_INC | T_L_Y_INC |
	                      T_R_X_INC | T_R_Y_INC |
	                      TRAPAZOID_FILL);
	SiSSetupCMDFlag(X_INC | Y_INC)

	SiSDoCMD
}

/* Trapezoid */
#ifdef TRAP
static void
SiSSubsequentMonoPatternFillTrap(ScrnInfoPtr pScrn,
               int patx, int paty,
               int y, int h,
	       int left, int dxL, int dyL, int eL,
	       int right, int dxR, int dyR, int eR )
{
	SISPtr  pSiS = SISPTR(pScrn);
	long dstbase;

	PDEBUG(ErrorF("Subsequent Mono8x8PatternFillTrap(%d, %d, %d - %d %d/%d %d/%d)\n",
					y, h, left, right, dxL, dxR, eL, eR));

	dstbase = 0;
830 831 832
	if(y >= 2048) {
	   dstbase = pSiS->scrnOffset * y;
	   y = 0;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
833 834 835 836 837 838 839 840 841 842 843 844 845 846
	}
#ifdef SISDUALHEAD
	if(pSiS->VGAEngine != SIS_530_VGA) {
	   dstbase += HEADOFFSET;
	}
#endif
	SiSSetupDSTBase(dstbase)

	/* Clear CommandReg because SetUp can be used for Rect and Trap */
	pSiS->CommandReg &= ~(T_XISMAJORL | T_XISMAJORR |
	                      T_L_X_INC | T_L_Y_INC |
			      T_R_X_INC | T_R_Y_INC |
			      BITBLT);

847
	if(dxL < 0) { dxL = -dxL;  }
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
848
	else { SiSSetupCMDFlag(T_L_X_INC) }
849
	if(dxR < 0) { dxR = -dxR; }
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
850 851
	else { SiSSetupCMDFlag(T_R_X_INC) }

852
	if(dyL < 0) { dyL = -dyL; }
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
853
	else { SiSSetupCMDFlag(T_L_Y_INC) }
854
	if(dyR < 0) { dyR = -dyR; }
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
855 856 857
	else { SiSSetupCMDFlag(T_R_Y_INC) }

	/* Determine major axis */
858 859
	if(dxL >= dyL) {      /* X is major axis */
	   SiSSetupCMDFlag(T_XISMAJORL)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
860
	}
861 862
	if(dxR >= dyR) {      /* X is major axis */
	   SiSSetupCMDFlag(T_XISMAJORR)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904
	}

	SiSSetupYH(y,h)
	SiSSetupLR(left,right)

	SiSSetupdL(dxL, dyL)
	SiSSetupdR(dxR, dyR)

	SiSSetupEL(eL)
	SiSSetupER(eR)

	SiSSetupCMDFlag(TRAPAZOID_FILL);

	SiSDoCMD
}
#endif


#if 0

/* TW: The following (already commented) functions have NOT been adapted for dual-head mode */

/* ----- CPU To Screen Color Expand (single task) ------------------------- */

/* This does not work. Assumingly for the same
 * reason why STSColorExpand does not work either.
 */
static void
SiSSetupForCPUToScreenColorExpand(ScrnInfoPtr pScrn,
                                int fg, int bg,
                                int rop, unsigned int planemask)
{
	SISPtr pSiS = SISPTR(pScrn);

	PDEBUG(ErrorF("Setup CPUToScreen ColorExpand(0x%x,0x%x, 0x%x,0x%x)\n",
					fg, bg, rop, planemask));

/*      SiSSetupDSTRect(pSiS->scrnOffset, pScrn->virtualY)*/
	SiSSetupDSTRect(pSiS->scrnOffset, -1)
	SiSSetupDSTColorDepth(pSiS->DstColor);
	SiSSetupSRCXY(0,0)
	SiSSetupSRCFG(fg)
905
	SiSSetupROP(XAAPatternROP[rop])
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
906
	SiSSetupCMDFlag(X_INC | Y_INC | COLOREXP)
907 908 909 910
	if(bg == -1) {
	   SiSSetupCMDFlag(TRANSPARENT)
	} else {
	   SiSSetupSRCBG(bg)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
911 912 913 914 915 916 917 918 919 920 921 922 923 924
	}
}

static void
SiSSubsequentCPUToScreenColorExpand(ScrnInfoPtr pScrn,
                                int x, int y, int w, int h, int skipleft)
{
	SISPtr pSiS = SISPTR(pScrn);
	long dstbase;

	PDEBUG(ErrorF("Subsequent CPUToScreen ColorExpand(%d,%d, %d,%d, %d)\n",
							x, y, w, h, skipleft));

	dstbase = 0;
925 926 927
	if(y >= 2048) {
	   dstbase = pSiS->scrnOffset * y;
	    y = 0;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956
	}
	SiSSetupDSTBase(dstbase)

/*      SiSSetupSRCPitch(((w+31)&0xFFE0)/8)*/
	SiSSetupSRCPitch((w+7)/8)
	SiSSetupDSTXY(x,y)
	SiSSetupRect(w,h)
/*      SiSDoCMD*/
	pSiS->DoColorExpand = TRUE;
}
#endif

/* ------ Screen To Screen Color Expand ------------------------------- */

/* TW: The hareware does not seem to support this the way we need it */

#ifdef STSCE
static void
SiSSetupForScreenToScreenColorExpand(ScrnInfoPtr pScrn,
                                int fg, int bg,
                                int rop, unsigned int planemask)
{
	SISPtr          pSiS = SISPTR(pScrn);

	PDEBUG(ErrorF("Setup ScreenToScreen ColorExp(0x%x,0x%x, 0x%x)\n",
							fg, bg, rop));

	SiSSetupDSTColorDepth(pSiS->DstColor)
	SiSSetupDSTRect(pSiS->scrnOffset, -1)
957
	SiSSetupROP(XAACopyROP[rop])
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
958 959 960 961
	SiSSetupSRCFG(fg)
	/* SiSSetupSRCXY(0,0) */

	if(bg == -1) {
962
	   SiSSetupCMDFlag(TRANSPARENT | ENCOLOREXP | X_INC |
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
963 964
				Y_INC | SRCVIDEO);
	} else {
965 966
	   SiSSetupSRCBG(bg);
	   SiSSetupCMDFlag(ENCOLOREXP | X_INC | Y_INC |
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
				SRCVIDEO);
	};
}
#endif

/* TW. This method blits in a single task; this does not seem to work
 * because the hardware does not use the source pitch as scanline
 * offset but only to calculate pattern address from source X and Y.
 * XAA provides the pattern bitmap with scrnOffset (displayWidth * bpp/8)
 * offset, but this does not seem to be supported by the hardware.
 */
#ifdef STSCE

/* For testing, these are the methods: (use only one at a time!) */

#undef npitch 		/* Normal: Use srcx/y as srcx/y, use scrnOffset as source pitch
			 * This would work if the hareware used the source pitch for
			 * incrementing the source address after each scanline - but
			 * it doesn't do this! The first line of the area is correctly
			 * color expanded, but since the source pitch is ignored and
			 * the source address not incremented correctly, the following
			 * lines are color expanded with any bit pattern that is left
			 * in the unused space of the source bitmap (which is organized
			 * with the depth of the screen framebuffer hence with a pitch
			 * of scrnOffset).
			 */

#undef pitchdw    	/* Use source pitch "displayWidth / 8" instead
		   	 * of scrnOffset (=displayWidth * bpp / 8)
			 * This can't work, because the pitch of the source
			 * bitmap is scrnoffset!
		   	 */

#define  nopitch   	/* Calculate srcbase with srcx and srcy, set the
		   	 * pitch to scrnOffset (which IS the correct pitch
		   	 * for the source bitmap) and set srcx and srcy both
		   	 * to 0.
			 * This would work if the hareware used the source pitch for
			 * incrementing the source address after each scanline - but
			 * it doesn't do this! Again: The first line of the area is
			 * correctly color expanded, but since the source pitch is
			 * ignored for scanline address incremention, the following
			 * lines are not correctly color expanded.
			 * WHATEVER I write to source pitch is ignored!
		   	 */

static void
SiSSubsequentScreenToScreenColorExpand(ScrnInfoPtr pScrn,
                                int x, int y, int w, int h,
                                int srcx, int srcy, int skipleft)
{
	SISPtr pSiS = SISPTR(pScrn);
        long srcbase, dstbase;
#if 0
	int _x0, _y0, _x1, _y1;
#endif
#ifdef pitchdw
	int newsrcx, newsrcy;

	/* srcx and srcy are provided based on a scrnOffset pitch ( = displayWidth * bpp / 8 )
	 * We recalulate srcx and srcy based on pitch = displayWidth / 8
	 */
        newsrcy = ((pSiS->scrnOffset * srcy) + (srcx * ((pScrn->bitsPerPixel+7)/8))) /
					  (pScrn->displayWidth/8);
        newsrcx = ((pSiS->scrnOffset * srcy) + (srcx * ((pScrn->bitsPerPixel+7)/8))) %
					  (pScrn->displayWidth/8);
#endif
	xf86DrvMsg(0, X_INFO, "Sub ScreenToScreen ColorExp(%d,%d, %d,%d, %d,%d, %d)\n",
					x, y, w, h, srcx, srcy, skipleft);

	srcbase = dstbase = 0;

#ifdef pitchdw
1040 1041 1042
	if(newsrcy >= 2048) {
	   srcbase = (pScrn->displayWidth / 8) * newsrcy;
	   newsrcy = 0;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1043 1044 1045 1046 1047 1048
	}
#endif
#ifdef nopitch
	srcbase = (pSiS->scrnOffset * srcy) + (srcx * ((pScrn->bitsPerPixel+7)/8));
#endif
#ifdef npitch
1049 1050 1051
	if(srcy >= 2048) {
	   srcbase = pSiS->scrnOffset * srcy;
	   srcy = 0;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1052 1053
	}
#endif
1054 1055 1056
	if(y >= 2048) {
	   dstbase = pSiS->scrnOffset * y;
	   y = 0;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
	}
#ifdef SISDUALHEAD
	if(pSiS->VGAEngine != SIS_530_VGA) {
	   srcbase += HEADOFFSET;
	   dstbase += HEADOFFSET;
	}
#endif
	SiSSetupSRCBase(srcbase)
	SiSSetupDSTBase(dstbase)

#ifdef pitchdw
	SiSSetupSRCPitch(pScrn->displayWidth/8)
#endif
#ifdef nopitch
	SiSSetupSRCPitch(pSiS->scrnOffset)
	/* SiSSetupSRCPitch(100) */ /* For test - has NO effect WHATSOEVER */
#endif
#ifdef npitch
	SiSSetupSRCPitch(pSiS->scrnOffset)
#endif

	SiSSetupRect(w,h)

#if 0   /* How do I implement the offset? Not this way, that's for sure.. */
	if (skipleft > 0) {
		_x0 = x+skipleft;
		_y0 = y;
		_x1 = x+w;
		_y1 = y+h;
		SiSSetupClipLT(_x0, _y0);
		SiSSetupClipRB(_x1, _y1);
		SiSSetupCMDFlag(CLIPENABLE);
	}
#endif
#ifdef pitchdw
	SiSSetupSRCXY(newsrcx, newsrcy)
#endif
#ifdef nopitch
	SiSSetupSRCXY(0,0)
#endif
#ifdef npitch
	SiSSetupSRCXY(srcx, srcy)
#endif

	SiSSetupDSTXY(x,y)

	SiSDoCMD
}
#endif

/* ----- CPU To Screen Color Expand (scanline-wise) ----------------- */

/* We do it using the indirect method */

static void
SiSSetupForScanlineCPUToScreenColorExpandFill(ScrnInfoPtr pScrn,
		int fg, int bg, int rop, unsigned int planemask)
{
	SISPtr pSiS=SISPTR(pScrn);

        /* TW: Make sure that current CPU-driven BitBlt buffer stage is 0
	 *     This is required!!! (Otherwise -> drawing errors)
	 */
	while((MMIO_IN16(pSiS->IOBase, 0x8242) & 0x1F00) != 0) {} /* WDR: == 0x10 */

	SiSSetupSRCXY(0,0);
1123
	SiSSetupROP(XAACopyROP[rop]);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1124 1125 1126 1127
	SiSSetupSRCFG(fg);
	SiSSetupDSTRect(pSiS->scrnOffset, -1);
	SiSSetupDSTColorDepth(pSiS->DstColor);
	if(bg == -1) {
1128 1129 1130 1131
	   SiSSetupCMDFlag(TRANSPARENT |
	                   ENCOLOREXP |
	   		   X_INC | Y_INC |
			   SRCCPUBLITBUF);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1132
	} else {
1133 1134 1135 1136
	   SiSSetupSRCBG(bg);
	   SiSSetupCMDFlag(ENCOLOREXP |
	                   X_INC | Y_INC |
		   	   SRCCPUBLITBUF);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150
	}
}


static void
SiSSubsequentScanlineCPUToScreenColorExpandFill(
                        ScrnInfoPtr pScrn, int x, int y, int w,
                        int h, int skipleft)
{
	SISPtr pSiS = SISPTR(pScrn);
	int _x0, _y0, _x1, _y1;
	long dstbase;

	dstbase = 0;
1151 1152 1153
	if((y >= 2048) || ((y + h) >= 2048)) {
	   dstbase = pSiS->scrnOffset * y;
	   y = 0;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1154 1155 1156
	}
#ifdef SISDUALHEAD
	if(pSiS->VGAEngine != SIS_530_VGA) {
1157
	   dstbase += HEADOFFSET;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175
	}
#endif

	/* TW: Wait until there is no color expansion command in queue
	 *     (This solves the OpenOffice.org window-move bug)
	 *     Added Idle-check - bit 23 is set sometimes, although
	 *     engine is actually idle!
	 *     Update: Bit 23 is not reliable. After heavy 3D engine
	 *     action, this bit never gets cleared again. So do
	 *     SiSIdle instead.
	 */
	if((MMIO_IN16(pSiS->IOBase, 0x8242) & 0xe000) != 0xe000) {
           /* while ((MMIO_IN16(pSiS->IOBase, 0x8242) & 0x0080) != 0) {} */
	   SiSIdle
	}

	SiSSetupDSTBase(dstbase)

1176 1177 1178 1179 1180 1181 1182 1183
	if(skipleft > 0) {
	   _x0 = x + skipleft;
	   _y0 = y;
	   _x1 = x + w;
	   _y1 = y + h;
	   SiSSetupClipLT(_x0, _y0);
	   SiSSetupClipRB(_x1, _y1);
	   SiSSetupCMDFlag(CLIPENABLE);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1184
	} else {
1185
	   pSiS->CommandReg &= (~CLIPENABLE);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218
	}

	SiSSetupRect(w, 1);
        SiSSetupSRCPitch(((((w+7)/8)+3) >> 2) * 4);
	pSiS->xcurrent = x;
	pSiS->ycurrent = y;
}

static void
SiSSubsequentColorExpandScanline(ScrnInfoPtr pScrn, int bufno)
{
	SISPtr pSiS=SISPTR(pScrn);
#if 0
	int newhead,bltbufstage,newtail;
#endif
	long cbo;

	cbo = pSiS->ColorExpandBufferScreenOffset[bufno];
#ifdef SISDUALHEAD
	if(pSiS->VGAEngine != SIS_530_VGA) {
	   cbo += HEADOFFSET;
	}
#endif

	/* TW: Wait until there is no color expansion command in queue
	 *     (This solves the GTK-big-font bug)
	 *     Added Idle-check - bit 23 is set sometimes, although
	 *     engine is actually idle!
	 *     Update: Bit 23 is not reliable. After heavy 3D engine
	 *     action, this bit never gets cleared again. So do
	 *     SiSIdle instead.
	 */
	if((MMIO_IN16(pSiS->IOBase, 0x8242) & 0xe000) != 0xe000) {
1219 1220
	   /* while ((MMIO_IN16(pSiS->IOBase, 0x8242) & 0x0080) != 0) {} */
	   SiSIdle
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235
	}

	SiSSetupSRCBase(cbo);

	SiSSetupDSTXY(pSiS->xcurrent, pSiS->ycurrent);

	SiSDoCMD

	pSiS->ycurrent++;

	if(pSiS->VGAEngine == SIS_530_VGA) {
	   while(MMIO_IN8(pSiS->IOBase, 0x8242) & 0x80) {}
	}
}

1236 1237