#include <X11/X.h>
#include <X11/Xmd.h>
#include "servermd.h"
#include "compiler.h"
#ifndef PSZ
#define PSZ 8
#endif
#ifndef PixelGroup
#define PixelGroup CARD32
#define PGSZB 4
#endif
#ifndef CfbBits
#define CfbBits CARD32
#endif
#define PGSZ (PGSZB << 3)
#define PPW (PGSZ/PSZ)
#define PLST (PPW-1)
#define PIM PLST
#define PMSK (((PixelGroup)1 << PSZ) - 1)
#define PPWMSK (((PixelGroup)1 << PPW) - 1)
#define PGSZBMSK (((PixelGroup)1 << PGSZB) - 1)
#if PPW == 1
#define PWSH 0
#else
#if PPW == 2
#define PWSH 1
#else
#if PPW == 4
#define PWSH 2
#else
#if PPW == 8
#define PWSH 3
#else
#if PPW == 16
#define PWSH 4
#endif
#endif
#endif
#endif
#endif
#if PSZ == 8
#define PIXEL_ADDR
typedef CARD8 PixelType;
#define FOUR_BIT_CODE
#endif
#if PSZ == 16
#define PIXEL_ADDR
typedef CARD16 PixelType;
#endif
#if PSZ == 24
#undef PMSK
#define PMSK 0xFFFFFF
#define PIXEL_ADDR
typedef CARD32 PixelType;
#endif
#if PSZ == 32
#undef PMSK
#define PMSK 0xFFFFFFFF
#define PIXEL_ADDR
typedef CARD32 PixelType;
#endif
#if (BITMAP_BIT_ORDER == MSBFirst)
#define BitRight(lw,n) ((lw) >> (n))
#define BitLeft(lw,n) ((lw) << (n))
#else
#define BitRight(lw,n) ((lw) << (n))
#define BitLeft(lw,n) ((lw) >> (n))
#endif
#define SCRLEFT(lw, n) BitLeft (lw, (n) * PSZ)
#define SCRRIGHT(lw, n) BitRight(lw, (n) * PSZ)
#if PPW == 16
#define PFILL(p) ( ((p)&PMSK) | \
((p)&PMSK) << PSZ | \
((p)&PMSK) << 2*PSZ | \
((p)&PMSK) << 3*PSZ | \
((p)&PMSK) << 4*PSZ | \
((p)&PMSK) << 5*PSZ | \
((p)&PMSK) << 6*PSZ | \
((p)&PMSK) << 7*PSZ | \
((p)&PMSK) << 8*PSZ | \
((p)&PMSK) << 9*PSZ | \
((p)&PMSK) << 10*PSZ | \
((p)&PMSK) << 11*PSZ | \
((p)&PMSK) << 12*PSZ | \
((p)&PMSK) << 13*PSZ | \
((p)&PMSK) << 14*PSZ | \
((p)&PMSK) << 15*PSZ )
#define PFILL2(p, pf) { \
pf = (p) & PMSK; \
pf |= (pf << PSZ); \
pf |= (pf << 2*PSZ); \
pf |= (pf << 4*PSZ); \
pf |= (pf << 8*PSZ); \
}
#endif
#if PPW == 8
#define PFILL(p) ( ((p)&PMSK) | \
((p)&PMSK) << PSZ | \
((p)&PMSK) << 2*PSZ | \
((p)&PMSK) << 3*PSZ | \
((p)&PMSK) << 4*PSZ | \
((p)&PMSK) << 5*PSZ | \
((p)&PMSK) << 6*PSZ | \
((p)&PMSK) << 7*PSZ )
#define PFILL2(p, pf) { \
pf = (p) & PMSK; \
pf |= (pf << PSZ); \
pf |= (pf << 2*PSZ); \
pf |= (pf << 4*PSZ); \
}
#endif
#if PPW == 4
#define PFILL(p) ( ((p)&PMSK) | \
((p)&PMSK) << PSZ | \
((p)&PMSK) << 2*PSZ | \
((p)&PMSK) << 3*PSZ )
#define PFILL2(p, pf) { \
pf = (p) & PMSK; \
pf |= (pf << PSZ); \
pf |= (pf << 2*PSZ); \
}
#endif
#if PPW == 2
#define PFILL(p) ( ((p)&PMSK) | \
((p)&PMSK) << PSZ )
#define PFILL2(p, pf) { \
pf = (p) & PMSK; \
pf |= (pf << PSZ); \
}
#endif
#if PPW == 1
#define PFILL(p) (p)
#define PFILL2(p,pf) (pf = (p))
#endif
#define DoRRop(dst, and, xor) (((dst) & (and)) ^ (xor))
#define DoMaskRRop(dst, and, xor, mask) \
(((dst) & ((and) | ~(mask))) ^ (xor & mask))
#if PSZ != 32 || PPW != 1
# if (PSZ == 24 && PPW == 1)
#define maskbits(x, w, startmask, endmask, nlw) {\
startmask = cfbstarttab[(x)&3]; \
endmask = cfbendtab[((x)+(w)) & 3]; \
nlw = ((((x)+(w))*3)>>2) - (((x)*3 +3)>>2); \
}
#define mask32bits(x, w, startmask, endmask) \
startmask = cfbstarttab[(x)&3]; \
endmask = cfbendtab[((x)+(w)) & 3];
#define maskpartialbits(x, w, mask) \
mask = cfbstartpartial[(x) & 3] & cfbendpartial[((x)+(w)) & 3];
#define maskbits24(x, w, startmask, endmask, nlw) \
startmask = cfbstarttab24[(x) & 3]; \
endmask = cfbendtab24[((x)+(w)) & 3]; \
if (startmask){ \
nlw = (((w) - (4 - ((x) & 3))) >> 2); \
} else { \
nlw = (w) >> 2; \
}
#define getbits24(psrc, dst, index) {\
register int idx; \
switch(idx = ((index)&3)<<1){ \
case 0: \
dst = (*(psrc) &cfbmask[idx]); \
break; \
case 6: \
dst = BitLeft((*(psrc) &cfbmask[idx]), cfb24Shift[idx]); \
break; \
default: \
dst = BitLeft((*(psrc) &cfbmask[idx]), cfb24Shift[idx]) | \
BitRight(((*((psrc)+1)) &cfbmask[idx+1]), cfb24Shift[idx+1]); \
}; \
}
#define putbits24(src, w, pdst, planemask, index) {\
register PixelGroup dstpixel; \
register unsigned int idx; \
switch(idx = ((index)&3)<<1){ \
case 0: \
dstpixel = (*(pdst) &cfbmask[idx]); \
break; \
case 6: \
dstpixel = BitLeft((*(pdst) &cfbmask[idx]), cfb24Shift[idx]); \
break; \
default: \
dstpixel = BitLeft((*(pdst) &cfbmask[idx]), cfb24Shift[idx])| \
BitRight(((*((pdst)+1)) &cfbmask[idx+1]), cfb24Shift[idx+1]); \
}; \
dstpixel &= ~(planemask); \
dstpixel |= (src & planemask); \
*(pdst) &= cfbrmask[idx]; \
switch(idx){ \
case 0: \
*(pdst) |= (dstpixel & cfbmask[idx]); \
break; \
case 2: \
case 4: \
pdst++;idx++; \
*(pdst) = ((*(pdst)) & cfbrmask[idx]) | \
(BitLeft(dstpixel, cfb24Shift[idx]) & cfbmask[idx]); \
pdst--;idx--; \
case 6: \
*(pdst) |= (BitRight(dstpixel, cfb24Shift[idx]) & cfbmask[idx]); \
break; \
}; \
}
#define putbitsrop24(src, x, pdst, planemask, rop) \
{ \
register PixelGroup t1, dstpixel; \
register unsigned int idx; \
switch(idx = (x)<<1){ \
case 0: \
dstpixel = (*(pdst) &cfbmask[idx]); \
break; \
case 6: \
dstpixel = BitLeft((*(pdst) &cfbmask[idx]), cfb24Shift[idx]); \
break; \
default: \
dstpixel = BitLeft((*(pdst) &cfbmask[idx]), cfb24Shift[idx])| \
BitRight(((*((pdst)+1)) &cfbmask[idx+1]), cfb24Shift[idx+1]); \
}; \
DoRop(t1, rop, (src), dstpixel); \
dstpixel &= ~planemask; \
dstpixel |= (t1 & planemask); \
*(pdst) &= cfbrmask[idx]; \
switch(idx){ \
case 0: \
*(pdst) |= (dstpixel & cfbmask[idx]); \
break; \
case 2: \
case 4: \
*((pdst)+1) = ((*((pdst)+1)) & cfbrmask[idx+1]) | \
(BitLeft(dstpixel, cfb24Shift[idx+1]) & (cfbmask[idx+1])); \
case 6: \
*(pdst) |= (BitRight(dstpixel, cfb24Shift[idx]) & cfbmask[idx]); \
}; \
}
# else
#define maskbits(x, w, startmask, endmask, nlw) \
startmask = cfbstarttab[(x)&PIM]; \
endmask = cfbendtab[((x)+(w)) & PIM]; \
if (startmask) \
nlw = (((w) - (PPW - ((x)&PIM))) >> PWSH); \
else \
nlw = (w) >> PWSH;
#define maskpartialbits(x, w, mask) \
mask = cfbstartpartial[(x) & PIM] & cfbendpartial[((x) + (w)) & PIM];
#define mask32bits(x, w, startmask, endmask) \
startmask = cfbstarttab[(x)&PIM]; \
endmask = cfbendtab[((x)+(w)) & PIM];
#define maskbits24(x, w, startmask, endmask, nlw) \
abort()
#define getbits24(psrc, dst, index) \
abort()
#define putbits24(src, w, pdst, planemask, index) \
abort()
#define putbitsrop24(src, x, pdst, planemask, rop) \
abort()
#endif
#define getbits(psrc, x, w, dst) \
if ( ((x) + (w)) <= PPW) \
{ \
dst = SCRLEFT(*(psrc), (x)); \
} \
else \
{ \
int m; \
m = PPW-(x); \
dst = (SCRLEFT(*(psrc), (x)) & cfbendtab[m]) | \
(SCRRIGHT(*((psrc)+1), m) & cfbstarttab[m]); \
}
#define putbits(src, x, w, pdst, planemask) \
if ( ((x)+(w)) <= PPW) \
{ \
PixelGroup tmpmask; \
maskpartialbits((x), (w), tmpmask); \
tmpmask &= PFILL(planemask); \
*(pdst) = (*(pdst) & ~tmpmask) | (SCRRIGHT(src, x) & tmpmask); \
} \
else \
{ \
unsigned int m; \
unsigned int n; \
PixelGroup pm = PFILL(planemask); \
m = PPW-(x); \
n = (w) - m; \
*(pdst) = (*(pdst) & (cfbendtab[x] | ~pm)) | \
(SCRRIGHT(src, x) & (cfbstarttab[x] & pm)); \
*((pdst)+1) = (*((pdst)+1) & (cfbstarttab[n] | ~pm)) | \
(SCRLEFT(src, m) & (cfbendtab[n] & pm)); \
}
#if defined(__GNUC__) && defined(mc68020)
#undef getbits
#define FASTGETBITS(psrc, x, w, dst) \
asm ("bfextu %3{%1:%2},%0" \
: "=d" (dst) : "di" (x), "di" (w), "o" (*(char *)(psrc)))
#define getbits(psrc,x,w,dst) \
{ \
FASTGETBITS(psrc, (x) * PSZ, (w) * PSZ, dst); \
dst = SCRLEFT(dst,PPW-(w)); \
}
#define FASTPUTBITS(src, x, w, pdst) \
asm ("bfins %3,%0{%1:%2}" \
: "=o" (*(char *)(pdst)) \
: "di" (x), "di" (w), "d" (src), "0" (*(char *) (pdst)))
#undef putbits
#define putbits(src, x, w, pdst, planemask) \
{ \
if (planemask != PMSK) { \
PixelGroup _m, _pm; \
FASTGETBITS(pdst, (x) * PSZ , (w) * PSZ, _m); \
PFILL2(planemask, _pm); \
_m &= (~_pm); \
_m |= (SCRRIGHT(src, PPW-(w)) & _pm); \
FASTPUTBITS(_m, (x) * PSZ, (w) * PSZ, pdst); \
} else { \
FASTPUTBITS(SCRRIGHT(src, PPW-(w)), (x) * PSZ, (w) * PSZ, pdst); \
} \
}
#endif
#define putbitsrop(src, x, w, pdst, planemask, rop) \
if ( ((x)+(w)) <= PPW) \
{ \
PixelGroup tmpmask; \
PixelGroup t1, t2; \
maskpartialbits((x), (w), tmpmask); \
PFILL2(planemask, t1); \
tmpmask &= t1; \
t1 = SCRRIGHT((src), (x)); \
DoRop(t2, rop, t1, *(pdst)); \
*(pdst) = (*(pdst) & ~tmpmask) | (t2 & tmpmask); \
} \
else \
{ \
CfbBits m; \
CfbBits n; \
PixelGroup t1, t2; \
PixelGroup pm; \
PFILL2(planemask, pm); \
m = PPW-(x); \
n = (w) - m; \
t1 = SCRRIGHT((src), (x)); \
DoRop(t2, rop, t1, *(pdst)); \
*(pdst) = (*(pdst) & (cfbendtab[x] | ~pm)) | (t2 & (cfbstarttab[x] & pm));\
t1 = SCRLEFT((src), m); \
DoRop(t2, rop, t1, *((pdst) + 1)); \
*((pdst)+1) = (*((pdst)+1) & (cfbstarttab[n] | ~pm)) | \
(t2 & (cfbendtab[n] & pm)); \
}
#else
#define maskbits(x, w, startmask, endmask, nlw) \
startmask = endmask = 0; \
nlw = (w);
#define maskpartialbits(x, w, mask) \
mask = 0xFFFFFFFF;
#define mask32bits(x, w, startmask, endmask) \
startmask = endmask = 0;
#define getbits(psrc, x, w, dst) (dst) = *(psrc)
#define putbits(src, x, w, pdst, planemask) \
*(pdst) = (*(pdst) & ~planemask) | (src & planemask);
#define putbitsrop(src, x, w, pdst, planemask, rop) \
{ \
PixelGroup t1; \
DoRop(t1, rop, (src), *(pdst)); \
*(pdst) = (*(pdst) & ~planemask) | (t1 & planemask); \
}
#endif
#if PSZ == 24
#define putbitsmropshort24(src,x,w,pdst,index) {\
PixelGroup _tmpmask; \
PixelGroup _t1; \
maskpartialbits ((x), (w), _tmpmask); \
_t1 = SCRRIGHT((src), (x)); \
DoMaskMergeRop24(_t1, pdst, _tmpmask, index); \
}
#endif
#define putbitsmropshort(src,x,w,pdst) {\
PixelGroup _tmpmask; \
PixelGroup _t1; \
maskpartialbits ((x), (w), _tmpmask); \
_t1 = SCRRIGHT((src), (x)); \
*pdst = DoMaskMergeRop(_t1, *pdst, _tmpmask); \
}
#define putbitsmroplong(src,x,w,pdst) { \
PixelGroup _startmask, _endmask; \
int _m; \
PixelGroup _t1; \
_m = PPW - (x); \
_startmask = cfbstarttab[x]; \
_endmask = cfbendtab[(w) - _m]; \
_t1 = SCRRIGHT((src), (x)); \
pdst[0] = DoMaskMergeRop(_t1,pdst[0],_startmask); \
_t1 = SCRLEFT ((src),_m); \
pdst[1] = DoMaskMergeRop(_t1,pdst[1],_endmask); \
}
#define putbitsmrop(src,x,w,pdst) \
if ((x) + (w) <= PPW) {\
putbitsmropshort(src,x,w,pdst); \
} else { \
putbitsmroplong(src,x,w,pdst); \
}
#if GETLEFTBITS_ALIGNMENT == 1
#define getleftbits(psrc, w, dst) dst = *((unsigned int *) psrc)
#define getleftbits24(psrc, w, dst, idx){ \
regiseter int index; \
switch(index = ((idx)&3)<<1){ \
case 0: \
dst = (*((unsigned int *) psrc))&cfbmask[index]; \
break; \
case 2: \
case 4: \
dst = BitLeft(((*((unsigned int *) psrc))&cfbmask[index]), cfb24Shift[index]); \
dst |= BitRight(((*((unsigned int *) psrc)+1)&cfbmask[index]), cfb4Shift[index]); \
break; \
case 6: \
dst = BitLeft((*((unsigned int *) psrc)),cfb24Shift[index]); \
break; \
}; \
}
#endif
#define getglyphbits(psrc, x, w, dst) \
{ \
dst = BitLeft((unsigned) *(psrc), (x)); \
if ( ((x) + (w)) > 32) \
dst |= (BitRight((unsigned) *((psrc)+1), 32-(x))); \
}
#if GETLEFTBITS_ALIGNMENT == 2
#define getleftbits(psrc, w, dst) \
{ \
if ( ((int)(psrc)) & 0x01 ) \
getglyphbits( ((unsigned int *)(((char *)(psrc))-1)), 8, (w), (dst) ); \
else \
dst = *((unsigned int *) psrc); \
}
#endif
#if GETLEFTBITS_ALIGNMENT == 4
#define getleftbits(psrc, w, dst) \
{ \
int off, off_b; \
off_b = (off = ( ((int)(psrc)) & 0x03)) << 3; \
getglyphbits( \
(unsigned int *)( ((char *)(psrc)) - off), \
(off_b), (w), (dst) \
); \
}
#endif
#if (BITMAP_BIT_ORDER == MSBFirst)
#define getstipplepixels( psrcstip, x, w, ones, psrcpix, destpix ) \
{ \
PixelGroup q; \
int m; \
if ((m = ((x) - ((PPW*PSZ)-PPW))) > 0) { \
q = (*(psrcstip)) << m; \
if ( (x)+(w) > (PPW*PSZ) ) \
q |= *((psrcstip)+1) >> ((PPW*PSZ)-m); \
} \
else \
q = (*(psrcstip)) >> -m; \
q = QuartetBitsTable[(w)] & ((ones) ? q : ~q); \
*(destpix) = (*(psrcpix)) & QuartetPixelMaskTable[q]; \
}
#define getstipplepixels24(psrcstip,xt,ones,psrcpix,destpix,stipindex) \
{ \
PixelGroup q; \
q = *(psrcstip) >> (xt); \
q = ((ones) ? q : ~q) & 1; \
*(destpix) = (*(psrcpix)) & QuartetPixelMaskTable[q]; \
}
#else
#define CfbFetchUnaligned(x) ldl_u(x)
#define getstipplepixels( psrcstip, xt, w, ones, psrcpix, destpix ) \
{ \
PixelGroup q; \
q = CfbFetchUnaligned(psrcstip) >> (xt); \
if ( ((xt)+(w)) > (PPW*PSZ) ) \
q |= (CfbFetchUnaligned((psrcstip)+1)) << ((PPW*PSZ)-(xt)); \
q = QuartetBitsTable[(w)] & ((ones) ? q : ~q); \
*(destpix) = (*(psrcpix)) & QuartetPixelMaskTable[q]; \
}
#if PSZ == 24
# if 0
#define getstipplepixels24(psrcstip,xt,w,ones,psrcpix,destpix,stipindex,srcindex,dstindex) \
{ \
PixelGroup q; \
CfbBits src; \
register unsigned int sidx; \
register unsigned int didx; \
sidx = ((srcindex) & 3)<<1; \
didx = ((dstindex) & 3)<<1; \
q = *(psrcstip) >> (xt); \
\
\
\
\
src = (*(psrcpix))&0xFFFFFF; \
if ( ((xt)+(w)) > PGSZ ) \
q |= (*((psrcstip)+1)) << (PGSZ -(xt)); \
q = QuartetBitsTable[(w)] & ((ones) ? q : ~q); \
src &= QuartetPixelMaskTable[q]; \
*(destpix) &= cfbrmask[didx]; \
switch(didx) {\
case 0: \
*(destpix) |= (src &cfbmask[didx]); \
break; \
case 2: \
case 4: \
destpix++;didx++; \
*(destpix) = ((*(destpix)) & (cfbrmask[didx]))| \
(BitLeft(src, cfb24Shift[didx]) & (cfbmask[didx])); \
destpix--; didx--;\
case 6: \
*(destpix) |= (BitRight(src, cfb24Shift[didx]) & cfbmask[didx]); \
break; \
}; \
}
# else
#define getstipplepixels24(psrcstip,xt,ones,psrcpix,destpix,stipindex) \
{ \
PixelGroup q; \
q = *(psrcstip) >> (xt); \
q = ((ones) ? q : ~q) & 1; \
*(destpix) = (*(psrcpix)) & QuartetPixelMaskTable[q]; \
}
# endif
#endif
#endif
extern PixelGroup cfbstarttab[];
extern PixelGroup cfbendtab[];
extern PixelGroup cfbstartpartial[];
extern PixelGroup cfbendpartial[];
extern PixelGroup cfbrmask[];
extern PixelGroup cfbmask[];
extern PixelGroup QuartetBitsTable[];
extern PixelGroup QuartetPixelMaskTable[];
#if PSZ == 24
extern int cfb24Shift[];
#endif