home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / server / ddx / cfb / cfbmskbits.h < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-04  |  15.4 KB  |  480 lines

  1. /************************************************************
  2. Copyright 1987 by Sun Microsystems, Inc. Mountain View, CA.
  3.  
  4.                     All Rights Reserved
  5.  
  6. Permission  to  use,  copy,  modify,  and  distribute   this
  7. software  and  its documentation for any purpose and without
  8. fee is hereby granted, provided that the above copyright no-
  9. tice  appear  in all copies and that both that copyright no-
  10. tice and this permission notice appear in  supporting  docu-
  11. mentation,  and  that the names of Sun or MIT not be used in
  12. advertising or publicity pertaining to distribution  of  the
  13. software  without specific prior written permission. Sun and
  14. M.I.T. make no representations about the suitability of this
  15. software for any purpose. It is provided "as is" without any
  16. express or implied warranty.
  17.  
  18. SUN DISCLAIMS ALL WARRANTIES WITH REGARD TO  THIS  SOFTWARE,
  19. INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FIT-
  20. NESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SUN BE  LI-
  21. ABLE  FOR  ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  22. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,  DATA  OR
  23. PROFITS,  WHETHER  IN  AN  ACTION OF CONTRACT, NEGLIGENCE OR
  24. OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
  25. THE USE OR PERFORMANCE OF THIS SOFTWARE.
  26.  
  27. ********************************************************/
  28.  
  29. /* $XConsortium: cfbmskbits.h,v 4.19 91/07/05 10:53:09 rws Exp $ */
  30.  
  31. extern unsigned int cfbstarttab[];
  32. extern unsigned int cfbendtab[];
  33. extern unsigned int cfbstartpartial[];
  34. extern unsigned int cfbendpartial[];
  35. extern unsigned int cfbrmask[];
  36. extern unsigned int cfbmask[];
  37. extern unsigned int QuartetBitsTable[];
  38. extern unsigned int QuartetPixelMaskTable[];
  39.  
  40.  
  41. /*
  42.  * ==========================================================================
  43.  * Converted from mfb to support memory-mapped color framebuffer by smarks@sun, 
  44.  * April-May 1987.
  45.  *
  46.  * The way I did the conversion was to consider each longword as an
  47.  * array of four bytes instead of an array of 32 one-bit pixels.  So
  48.  * getbits() and putbits() retain much the same calling sequence, but
  49.  * they move bytes around instead of bits.  Of course, this entails the
  50.  * removal of all of the one-bit-pixel dependencies from the other
  51.  * files, but the major bit-hacking stuff should be covered here.
  52.  *
  53.  * I've created some new macros that make it easier to understand what's 
  54.  * going on in the pixel calculations, and that make it easier to change the 
  55.  * pixel size.
  56.  *
  57.  * name        mfb        cfb        explanation
  58.  * ----        ---        ---        -----------
  59.  * PPW        32        4        pixels per word
  60.  * PLST        31        3        last pixel in a word (should be PPW-1)
  61.  * PIM        0x1f    0x03    pixel index mask (index within a word)
  62.  * PWSH        5        2        pixel-to-word shift
  63.  * PSZ        1        8        pixel size (bits)
  64.  * PMSK        0x01    0xFF    single-pixel mask
  65.  *
  66.  * Note that even with these new macros, there are still many dependencies 
  67.  * in the code on the fact that there are 32 bits (not necessarily pixels) 
  68.  * in each word.  These macros remove the dependency that 1 pixel == 1 bit.
  69.  *
  70.  * I have also added a new macro, PFILL, that takes one pixel and
  71.  * replicates it throughout a word.  This macro definition is dependent
  72.  * upon pixel and word size; it doesn't use macros like PPW and so
  73.  * forth.  Examples: for monochrome, PFILL(1) => 0xffffffff, PFILL(0) =>
  74.  * 0x00000000.  For 8-bit color, PFILL(0x5d) => 0x5d5d5d5d.  This macro
  75.  * is used primarily for replicating a plane mask into a word.
  76.  *
  77.  * Color framebuffers operations also support the notion of a plane
  78.  * mask.  This mask determines which planes of the framebuffer can be
  79.  * altered; the others are left unchanged.  I have added another
  80.  * parameter to the putbits and putbitsrop macros that is the plane
  81.  * mask.
  82.  * ==========================================================================
  83.  */
  84.  
  85. #define PPW    4
  86. #define PLST    3
  87. #define PIM    0x03
  88. #define PWSH    2
  89. #define PSZ    8
  90. #define PMSK    0xFF
  91.  
  92. /* the following notes use the following conventions:
  93. SCREEN LEFT                SCREEN RIGHT
  94. in this file and maskbits.c, left and right refer to screen coordinates,
  95. NOT bit numbering in registers.
  96.  
  97. cfbstarttab[n] 
  98.     pixels[0,n-1] = 0's    pixels[n,PPW-1] = 1's
  99. cfbendtab[n] =
  100.     pixels[0,n-1] = 1's    pixels[n,PPW-1] = 0's
  101.  
  102. cfbstartpartial[], cfbendpartial[]
  103.     these are used as accelerators for doing putbits and masking out
  104. bits that are all contained between longword boudaries.  the extra
  105. 256 bytes of data seems a small price to pay -- code is smaller,
  106. and narrow things (e.g. window borders) go faster.
  107.  
  108. the names may seem misleading; they are derived not from which end
  109. of the word the bits are turned on, but at which end of a scanline
  110. the table tends to be used.
  111.  
  112. look at the tables and macros to understand boundary conditions.
  113. (careful readers will note that starttab[n] = ~endtab[n] for n != 0)
  114.  
  115. -----------------------------------------------------------------------
  116. these two macros depend on the screen's bit ordering.
  117. in both of them x is a screen position.  they are used to
  118. combine bits collected from multiple longwords into a
  119. single destination longword, and to unpack a single
  120. source longword into multiple destinations.
  121.  
  122. SCRLEFT(dst, x)
  123.     takes dst[x, PPW] and moves them to dst[0, PPW-x]
  124.     the contents of the rest of dst are 0 ONLY IF
  125.     dst is UNSIGNED.
  126.     is cast as an unsigned.
  127.     this is a right shift on the VAX, left shift on
  128.     Sun and pc-rt.
  129.  
  130. SCRRIGHT(dst, x)
  131.     takes dst[0,x] and moves them to dst[PPW-x, PPW]
  132.     the contents of the rest of dst are 0 ONLY IF
  133.     dst is UNSIGNED.
  134.     this is a left shift on the VAX, right shift on
  135.     Sun and pc-rt.
  136.  
  137.  
  138. the remaining macros are cpu-independent; all bit order dependencies
  139. are built into the tables and the two macros above.
  140.  
  141. maskbits(x, w, startmask, endmask, nlw)
  142.     for a span of width w starting at position x, returns
  143. a mask for ragged pixels at start, mask for ragged pixels at end,
  144. and the number of whole longwords between the ends.
  145.  
  146. maskpartialbits(x, w, mask)
  147.     works like maskbits(), except all the pixels are in the
  148.     same longword (i.e. (x&0xPIM + w) <= PPW)
  149.  
  150. mask32bits(x, w, startmask, endmask, nlw)
  151.     as maskbits, but does not calculate nlw.  it is used by
  152.     cfbGlyphBlt to put down glyphs <= PPW bits wide.
  153.  
  154. getbits(psrc, x, w, dst)
  155.     starting at position x in psrc (x < PPW), collect w
  156.     pixels and put them in the screen left portion of dst.
  157.     psrc is a longword pointer.  this may span longword boundaries.
  158.     it special-cases fetching all w bits from one longword.
  159.  
  160.     +--------+--------+        +--------+
  161.     |    | m |n|      |    ==>     | m |n|  |
  162.     +--------+--------+        +--------+
  163.         x      x+w            0     w
  164.     psrc     psrc+1            dst
  165.             m = PPW - x
  166.             n = w - m
  167.  
  168.     implementation:
  169.     get m pixels, move to screen-left of dst, zeroing rest of dst;
  170.     get n pixels from next word, move screen-right by m, zeroing
  171.          lower m pixels of word.
  172.     OR the two things together.
  173.  
  174. putbits(src, x, w, pdst, planemask)
  175.     starting at position x in pdst, put down the screen-leftmost
  176.     w bits of src.  pdst is a longword pointer.  this may
  177.     span longword boundaries.
  178.     it special-cases putting all w bits into the same longword.
  179.  
  180.     +--------+            +--------+--------+
  181.     | m |n|  |        ==>    |    | m |n|      |
  182.     +--------+            +--------+--------+
  183.     0     w                     x     x+w
  184.     dst                pdst     pdst+1
  185.             m = PPW - x
  186.             n = w - m
  187.  
  188.     implementation:
  189.     get m pixels, shift screen-right by x, zero screen-leftmost x
  190.         pixels; zero rightmost m bits of *pdst and OR in stuff
  191.         from before the semicolon.
  192.     shift src screen-left by m, zero bits n-32;
  193.         zero leftmost n pixels of *(pdst+1) and OR in the
  194.         stuff from before the semicolon.
  195.  
  196. putbitsrop(src, x, w, pdst, planemask, ROP)
  197.     like putbits but calls DoRop with the rasterop ROP (see cfb.h for
  198.     DoRop)
  199.  
  200. getleftbits(psrc, w, dst)
  201.     get the leftmost w (w<=PPW) bits from *psrc and put them
  202.     in dst.  this is used by the cfbGlyphBlt code for glyphs
  203.     <=PPW bits wide.
  204. */
  205.  
  206. #include    "X.h"
  207. #include    "Xmd.h"
  208. #include    "servermd.h"
  209. #if    (BITMAP_BIT_ORDER == MSBFirst)
  210. #define BitRight(lw,n)    ((lw) >> (n))
  211. #define BitLeft(lw,n)    ((lw) << (n))
  212. #else    /* (BITMAP_BIT_ORDER == LSBFirst) */
  213. #define BitRight(lw,n)    ((lw) << (n))
  214. #define BitLeft(lw,n)    ((lw) >> (n))
  215. #endif    /* (BITMAP_BIT_ORDER == MSBFirst) */
  216.  
  217. #define SCRLEFT(lw, n)    BitLeft (lw, (n) * PSZ)
  218. #define SCRRIGHT(lw, n)    BitRight(lw, (n) * PSZ)
  219.  
  220. /*
  221.  * Note that the shift direction is independent of the byte ordering of the 
  222.  * machine.  The following is portable code.
  223.  */
  224. #define PFILL(p) ( ((p)&PMSK)          | \
  225.            ((p)&PMSK) <<   PSZ | \
  226.            ((p)&PMSK) << 2*PSZ | \
  227.            ((p)&PMSK) << 3*PSZ )
  228. #define PFILL2(p, pf) { \
  229.     pf = (p) & PMSK; \
  230.     pf |= (pf << PSZ); \
  231.     pf |= (pf << 2*PSZ); \
  232. }
  233.  
  234. /*
  235.  * Reduced raster op - using precomputed values, perform the above
  236.  * in three instructions
  237.  */
  238.  
  239. #define DoRRop(dst, and, xor)    (((dst) & (and)) ^ (xor))
  240.  
  241. #define DoMaskRRop(dst, and, xor, mask) \
  242.     (((dst) & ((and) | ~(mask))) ^ (xor & mask))
  243.  
  244. #define maskbits(x, w, startmask, endmask, nlw) \
  245.     startmask = cfbstarttab[(x)&PIM]; \
  246.     endmask = cfbendtab[((x)+(w)) & PIM]; \
  247.     if (startmask) \
  248.     nlw = (((w) - (PPW - ((x)&PIM))) >> PWSH); \
  249.     else \
  250.     nlw = (w) >> PWSH;
  251.  
  252. #define maskpartialbits(x, w, mask) \
  253.     mask = cfbstartpartial[(x) & PIM] & cfbendpartial[((x) + (w)) & PIM];
  254.  
  255. #define mask32bits(x, w, startmask, endmask) \
  256.     startmask = cfbstarttab[(x)&PIM]; \
  257.     endmask = cfbendtab[((x)+(w)) & PIM];
  258.  
  259.  
  260. #define getbits(psrc, x, w, dst) \
  261. if ( ((x) + (w)) <= PPW) \
  262. { \
  263.     dst = SCRLEFT(*(psrc), (x)); \
  264. } \
  265. else \
  266. { \
  267.     int m; \
  268.     m = PPW-(x); \
  269.     dst = (SCRLEFT(*(psrc), (x)) & cfbendtab[m]) | \
  270.       (SCRRIGHT(*((psrc)+1), m) & cfbstarttab[m]); \
  271. }
  272.  
  273.  
  274. #define putbits(src, x, w, pdst, planemask) \
  275. if ( ((x)+(w)) <= PPW) \
  276. { \
  277.     unsigned long tmpmask; \
  278.     maskpartialbits((x), (w), tmpmask); \
  279.     tmpmask &= PFILL(planemask); \
  280.     *(pdst) = (*(pdst) & ~tmpmask) | (SCRRIGHT(src, x) & tmpmask); \
  281. } \
  282. else \
  283. { \
  284.     unsigned long m; \
  285.     unsigned long n; \
  286.     unsigned long pm = PFILL(planemask); \
  287.     m = PPW-(x); \
  288.     n = (w) - m; \
  289.     *(pdst) = (*(pdst) & (cfbendtab[x] | ~pm)) | \
  290.     (SCRRIGHT(src, x) & (cfbstarttab[x] & pm)); \
  291.     *((pdst)+1) = (*((pdst)+1) & (cfbstarttab[n] | ~pm)) | \
  292.     (SCRLEFT(src, m) & (cfbendtab[n] & pm)); \
  293. }
  294. #if defined(__GNUC__) && defined(mc68020)
  295. #undef getbits
  296. #define FASTGETBITS(psrc, x, w, dst) \
  297.     asm ("bfextu %3{%1:%2},%0" \
  298.      : "=d" (dst) : "di" (x), "di" (w), "o" (*(char *)(psrc)))
  299.  
  300. #define getbits(psrc,x,w,dst) \
  301. { \
  302.     FASTGETBITS(psrc, (x) * PSZ, (w) * PSZ, dst); \
  303.     dst = SCRLEFT(dst,PPW-(w)); \
  304. }
  305.  
  306. #define FASTPUTBITS(src, x, w, pdst) \
  307.     asm ("bfins %3,%0{%1:%2}" \
  308.      : "=o" (*(char *)(pdst)) \
  309.      : "di" (x), "di" (w), "d" (src), "0" (*(char *) (pdst)))
  310.  
  311. #undef putbits
  312. #define putbits(src, x, w, pdst, planemask) \
  313. { \
  314.     if (planemask != PMSK) { \
  315.         unsigned long _m, _pm; \
  316.         FASTGETBITS(pdst, (x) * PSZ , (w) * PSZ, _m); \
  317.         PFILL2(planemask, _pm); \
  318.         _m &= (~_pm); \
  319.         _m |= (SCRRIGHT(src, PPW-(w)) & _pm); \
  320.         FASTPUTBITS(_m, (x) * PSZ, (w) * PSZ, pdst); \
  321.     } else { \
  322.         FASTPUTBITS(SCRRIGHT(src, PPW-(w)), (x) * PSZ, (w) * PSZ, pdst); \
  323.     } \
  324. }
  325.     
  326.  
  327. #endif /* mc68020 */
  328.  
  329. #define putbitsrop(src, x, w, pdst, planemask, rop) \
  330. if ( ((x)+(w)) <= PPW) \
  331. { \
  332.     unsigned long tmpmask; \
  333.     unsigned long t1, t2; \
  334.     maskpartialbits((x), (w), tmpmask); \
  335.     PFILL2(planemask, t1); \
  336.     tmpmask &= t1; \
  337.     t1 = SCRRIGHT((src), (x)); \
  338.     DoRop(t2, rop, t1, *(pdst)); \
  339.     *(pdst) = (*(pdst) & ~tmpmask) | (t2 & tmpmask); \
  340. } \
  341. else \
  342. { \
  343.     unsigned long m; \
  344.     unsigned long n; \
  345.     unsigned long t1, t2; \
  346.     unsigned long pm; \
  347.     PFILL2(planemask, pm); \
  348.     m = PPW-(x); \
  349.     n = (w) - m; \
  350.     t1 = SCRRIGHT((src), (x)); \
  351.     DoRop(t2, rop, t1, *(pdst)); \
  352.     *(pdst) = (*(pdst) & (cfbendtab[x] | ~pm)) | (t2 & (cfbstarttab[x] & pm));\
  353.     t1 = SCRLEFT((src), m); \
  354.     DoRop(t2, rop, t1, *((pdst) + 1)); \
  355.     *((pdst)+1) = (*((pdst)+1) & (cfbstarttab[n] | ~pm)) | \
  356.     (t2 & (cfbendtab[n] & pm)); \
  357. }
  358.  
  359. /*
  360.  * Use these macros only when you're using the MergeRop stuff
  361.  * in ../mfb/mergerop.h
  362.  */
  363.  
  364. /* useful only when not spanning destination longwords */
  365. #define putbitsmropshort(src,x,w,pdst) {\
  366.     unsigned long   _tmpmask; \
  367.     unsigned long   _t1; \
  368.     maskpartialbits ((x), (w), _tmpmask); \
  369.     _t1 = SCRRIGHT((src), (x)); \
  370.     *pdst = DoMaskMergeRop(_t1, *pdst, _tmpmask); \
  371. }
  372.  
  373. /* useful only when spanning destination longwords */
  374. #define putbitsmroplong(src,x,w,pdst) { \
  375.     unsigned long   _startmask, _endmask; \
  376.     int            _m; \
  377.     unsigned long   _t1; \
  378.     _m = PPW - (x); \
  379.     _startmask = cfbstarttab[x]; \
  380.     _endmask = cfbendtab[(w) - _m]; \
  381.     _t1 = SCRRIGHT((src), (x)); \
  382.     pdst[0] = DoMaskMergeRop(_t1,pdst[0],_startmask); \
  383.     _t1 = SCRLEFT ((src),_m); \
  384.     pdst[1] = DoMaskMergeRop(_t1,pdst[1],_endmask); \
  385. }
  386.  
  387. #define putbitsmrop(src,x,w,pdst) \
  388. if ((x) + (w) <= PPW) {\
  389.     putbitsmropshort(src,x,w,pdst); \
  390. } else { \
  391.     putbitsmroplong(src,x,w,pdst); \
  392. }
  393.  
  394. #if GETLEFTBITS_ALIGNMENT == 1
  395. #define getleftbits(psrc, w, dst)    dst = *((unsigned int *) psrc)
  396. #endif /* GETLEFTBITS_ALIGNMENT == 1 */
  397.  
  398. #define getglyphbits(psrc, x, w, dst) \
  399. { \
  400.     dst = BitLeft((unsigned) *(psrc), (x)); \
  401.     if ( ((x) + (w)) > 32) \
  402.     dst |= (BitRight((unsigned) *((psrc)+1), 32-(x))); \
  403. }
  404. #if GETLEFTBITS_ALIGNMENT == 2
  405. #define getleftbits(psrc, w, dst) \
  406.     { \
  407.     if ( ((int)(psrc)) & 0x01 ) \
  408.         getglyphbits( ((unsigned int *)(((char *)(psrc))-1)), 8, (w), (dst) ); \
  409.     else \
  410.         dst = *((unsigned int *) psrc); \
  411.     }
  412. #endif /* GETLEFTBITS_ALIGNMENT == 2 */
  413.  
  414. #if GETLEFTBITS_ALIGNMENT == 4
  415. #define getleftbits(psrc, w, dst) \
  416.     { \
  417.     int off, off_b; \
  418.     off_b = (off = ( ((int)(psrc)) & 0x03)) << 3; \
  419.     getglyphbits( \
  420.         (unsigned int *)( ((char *)(psrc)) - off), \
  421.         (off_b), (w), (dst) \
  422.            ); \
  423.     }
  424. #endif /* GETLEFTBITS_ALIGNMENT == 4 */
  425.  
  426. #if (PPW*PSZ==32)
  427. #define GET_VALID_BITS_FROM_LONG(l) (l)
  428. #else
  429. #define GET_VALID_BITS_FROM_LONG(l) ((l)&((1L<<(PPW*PSZ))-1))
  430. #endif
  431.  
  432. /*
  433.  * getstipplepixels( psrcstip, x, w, ones, psrcpix, destpix )
  434.  *
  435.  * Converts bits to pixels in a reasonable way.  Takes w (1 <= w <= 4)
  436.  * bits from *psrcstip, starting at bit x; call this a quartet of bits.
  437.  * Then, takes the pixels from *psrcpix corresponding to the one-bits (if
  438.  * ones is TRUE) or the zero-bits (if ones is FALSE) of the quartet
  439.  * and puts these pixels into destpix.
  440.  *
  441.  * Example:
  442.  *
  443.  *      getstipplepixels( &(0x08192A3B), 17, 4, 1, &(0x4C5D6E7F), dest )
  444.  *
  445.  * 0x08192A3B = 0000 1000 0001 1001 0010 1010 0011 1011
  446.  *
  447.  * This will take 4 bits starting at bit 17, so the quartet is 0x5 = 0101.
  448.  * It will take pixels from 0x4C5D6E7F corresponding to the one-bits in this
  449.  * quartet, so dest = 0x005D007F.
  450.  *
  451.  * XXX Works with both byte order.
  452.  * XXX This works for all values of x and w within a doubleword.
  453.  */
  454. #if (BITMAP_BIT_ORDER == MSBFirst)
  455. #define getstipplepixels( psrcstip, x, w, ones, psrcpix, destpix ) \
  456. { \
  457.     unsigned int q; \
  458.     int m; \
  459.     if ((m = ((x) - ((PPW*PSZ)-4))) > 0) { \
  460.         q = (*(psrcstip)) << m; \
  461.     if ( (x)+(w) > (PPW*PSZ) ) \
  462.         q |= GET_VALID_BITS_FROM_LONG(*((psrcstip)+1)) >> ((PPW*PSZ)-m); \
  463.     } \
  464.     else \
  465.         q = (*(psrcstip)) >> -m; \
  466.     q = QuartetBitsTable[(w)] & ((ones) ? q : ~q); \
  467.     *(destpix) = (*(psrcpix)) & QuartetPixelMaskTable[q]; \
  468. }
  469. #else /* BITMAP_BIT_ORDER == LSB */
  470. #define getstipplepixels( psrcstip, xt, w, ones, psrcpix, destpix ) \
  471. { \
  472.     unsigned int q; \
  473.     q = GET_VALID_BITS_FROM_LONG(*(psrcstip)) >> (xt); \
  474.     if ( ((xt)+(w)) > (PPW*PSZ) ) \
  475.         q |= (*((psrcstip)+1)) << ((PPW*PSZ)-(xt)); \
  476.     q = QuartetBitsTable[(w)] & ((ones) ? q : ~q); \
  477.     *(destpix) = (*(psrcpix)) & QuartetPixelMaskTable[q]; \
  478. }
  479. #endif
  480.