7 Copyright (c) 1989 X Consortium
9 Permission is hereby granted, free of charge, to any person obtaining a copy
10 of this software and associated documentation files (the "Software"), to deal
11 in the Software without restriction, including without limitation the rights
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the Software is
14 furnished to do so, subject to the following conditions:
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 Except as contained in this notice, the name of the X Consortium shall not be
27 used in advertising or otherwise to promote the sale, use or other dealings
28 in this Software without prior written authorization from the X Consortium.
33 /* $XConsortium: cfbblt.c,v 1.13 94/04/17 20:28:44 dpw Exp $ */
34 /* $XFree86: xc/programs/Xserver/cfb/cfbblt.c,v 3.1 1996/12/09 11:50:52 dawes Exp $ */
40 #include "windowstr.h"
41 #include "scrnintstr.h"
42 #include "pixmapstr.h"
43 #include "regionstr.h"
45 #include "cfbmskbits.h"
50 #ifdef notdef /* XXX fails right now, walks off end of pixmaps */
51 #if defined (FAST_UNALIGNED_READS) && PSZ == 8
52 #define DO_UNALIGNED_BITBLT
56 #if defined(FAST_MEMCPY) && (MROP == Mcopy) && PSZ == 8
61 MROP_NAME(cfbDoBitblt)(pSrc, pDst, alu, prgnDst, pptSrc, planemask)
62 DrawablePtr pSrc, pDst;
66 unsigned long planemask;
68 unsigned long *psrcBase, *pdstBase;
69 /* start of src and dst bitmaps */
70 int widthSrc, widthDst; /* add to get to same position in next line */
75 BoxPtr pboxTmp, pboxNext, pboxBase, pboxNew1, pboxNew2;
76 /* temporaries for shuffling rectangles */
77 DDXPointPtr pptTmp, pptNew1, pptNew2;
78 /* shuffling boxes entails shuffling the
81 int xdir; /* 1 = left right, -1 = right left/ */
82 int ydir; /* 1 = top down, -1 = bottom up */
84 unsigned long *psrcLine, *pdstLine;
85 /* pointers to line with current src and dst */
86 register unsigned long *psrc;/* pointer to current src longword */
87 register unsigned long *pdst;/* pointer to current dst longword */
91 /* following used for looping through a line */
92 unsigned long startmask, endmask; /* masks for writing ends of dst */
93 int nlMiddle; /* whole longwords in dst */
95 register int leftShift, rightShift;
96 register unsigned long bits;
97 register unsigned long bits1;
98 register int nl; /* temp copy of nlMiddle */
100 /* place to store full source word */
101 int nstart; /* number of ragged bits at start of dst */
102 int nend; /* number of ragged bits at end of dst */
103 int srcStartOver; /* pulling nstart bits from src
104 overflows into the next word? */
113 MROP_INITIALIZE(alu,planemask);
115 cfbGetLongWidthAndPointer (pSrc, widthSrc, psrcBase)
117 cfbGetLongWidthAndPointer (pDst, widthDst, pdstBase)
119 /* XXX we have to err on the side of safety when both are windows,
120 * because we don't know if IncludeInferiors is being used.
122 careful = ((pSrc == pDst) ||
123 ((pSrc->type == DRAWABLE_WINDOW) &&
124 (pDst->type == DRAWABLE_WINDOW)));
126 pbox = REGION_RECTS(prgnDst);
127 nbox = REGION_NUM_RECTS(prgnDst);
133 if (careful && (pptSrc->y < pbox->y1))
135 /* walk source botttom to top */
137 widthSrc = -widthSrc;
138 widthDst = -widthDst;
142 /* keep ordering in each band, reverse order of bands */
143 pboxNew1 = (BoxPtr)ALLOCATE_LOCAL(sizeof(BoxRec) * nbox);
146 pptNew1 = (DDXPointPtr)ALLOCATE_LOCAL(sizeof(DDXPointRec) * nbox);
149 DEALLOCATE_LOCAL(pboxNew1);
152 pboxBase = pboxNext = pbox+nbox-1;
153 while (pboxBase >= pbox)
155 while ((pboxNext >= pbox) &&
156 (pboxBase->y1 == pboxNext->y1))
158 pboxTmp = pboxNext+1;
159 pptTmp = pptSrc + (pboxTmp - pbox);
160 while (pboxTmp <= pboxBase)
162 *pboxNew1++ = *pboxTmp++;
163 *pptNew1++ = *pptTmp++;
175 /* walk source top to bottom */
179 if (careful && (pptSrc->x < pbox->x1))
181 /* walk source right to left */
186 /* reverse order of rects in each band */
187 pboxNew2 = (BoxPtr)ALLOCATE_LOCAL(sizeof(BoxRec) * nbox);
188 pptNew2 = (DDXPointPtr)ALLOCATE_LOCAL(sizeof(DDXPointRec) * nbox);
189 if(!pboxNew2 || !pptNew2)
191 if (pptNew2) DEALLOCATE_LOCAL(pptNew2);
192 if (pboxNew2) DEALLOCATE_LOCAL(pboxNew2);
195 DEALLOCATE_LOCAL(pptNew1);
196 DEALLOCATE_LOCAL(pboxNew1);
200 pboxBase = pboxNext = pbox;
201 while (pboxBase < pbox+nbox)
203 while ((pboxNext < pbox+nbox) &&
204 (pboxNext->y1 == pboxBase->y1))
207 pptTmp = pptSrc + (pboxTmp - pbox);
208 while (pboxTmp != pboxBase)
210 *pboxNew2++ = *--pboxTmp;
211 *pptNew2++ = *--pptTmp;
223 /* walk source left to right */
229 w = pbox->x2 - pbox->x1;
230 h = pbox->y2 - pbox->y1;
237 if (ydir == -1) /* start at last scanline of rectangle */
239 psrcLine = psrcBase + ((pptSrc->y+h-1) * -widthSrc);
240 pdstLine = pdstBase + ((pbox->y2-1) * -widthDst);
242 else /* start at first scanline */
244 psrcLine = psrcBase + (pptSrc->y * widthSrc);
245 pdstLine = pdstBase + (pbox->y1 * widthDst);
248 if (w == 1 && ((pbox->x1 & 3) == 0 || (pbox->x1 & 3) == 3))
250 if ((pbox->x1 & PIM) + w <= PPW)
253 maskpartialbits (pbox->x1, w, endmask);
259 maskbits(pbox->x1, w, startmask, endmask, nlMiddle);
263 /* If the src and dst scanline don't overlap, do forward case. */
265 if ((xdir == 1) || (pptSrc->y != pbox->y1)
266 || (pptSrc->x + w <= pbox->x1))
269 char *psrc = (char *) psrcLine + (pptSrc->x * 3);
270 char *pdst = (char *) pdstLine + (pbox->x1 * 3);
272 char *psrc = (char *) psrcLine + pptSrc->x;
273 char *pdst = (char *) pdstLine + pbox->x1;
278 memcpy(pdst, psrc, w2);
280 memcpy(pdst, psrc, w);
282 pdst += widthDst << PWSH;
283 psrc += widthSrc << PWSH;
286 #else /* ! DO_MEMCPY */
290 xoffSrc = (4 - pptSrc->x) & 3;
291 xoffDst = (4 - pbox->x1) & 3;
292 pdstLine += (pbox->x1 * 3) >> 2;
293 psrcLine += (pptSrc->x * 3) >> 2;
295 xoffSrc = pptSrc->x & PIM;
296 xoffDst = pbox->x1 & PIM;
297 pdstLine += (pbox->x1 >> PWSH);
298 psrcLine += (pptSrc->x >> PWSH);
300 #ifdef DO_UNALIGNED_BITBLT
301 nl = xoffSrc - xoffDst;
302 psrcLine = (unsigned long *)
303 (((unsigned char *) psrcLine) + nl);
305 if (xoffSrc == xoffDst)
312 pdstLine += widthDst;
313 psrcLine += widthSrc;
316 *pdst = MROP_MASK(*psrc, *pdst, startmask);
322 #ifdef LARGE_INSTRUCTION_CACHE
323 #ifdef FAST_CONSTANT_OFFSET_MODE
325 psrc += nl & (UNROLL-1);
326 pdst += nl & (UNROLL-1);
328 #define BodyOdd(n) pdst[-n] = MROP_SOLID (psrc[-n], pdst[-n]);
329 #define BodyEven(n) pdst[-n] = MROP_SOLID (psrc[-n], pdst[-n]);
337 #define BodyOdd(n) *pdst = MROP_SOLID (*psrc, *pdst); pdst++; psrc++;
338 #define BodyEven(n) BodyOdd(n)
351 /* you'd think this would be faster --
352 * a single instruction instead of 6
353 * but measurements show it to be ~15% slower
355 while ((nl -= 6) >= 0)
357 asm ("moveml %1+,#0x0c0f;moveml#0x0c0f,%0"
358 : "=m" (*(char *)pdst)
359 : "m" (*(char *)psrc)
360 : "d0", "d1", "d2", "d3",
369 *pdst = MROP_SOLID (*psrc, *pdst);
374 *pdst = MROP_MASK(*psrc, *pdst, endmask);
377 #ifndef DO_UNALIGNED_BITBLT
380 if (xoffSrc > xoffDst)
383 leftShift = (xoffSrc - xoffDst) << 3;
386 leftShift = (xoffSrc - xoffDst) << (5 - PWSH);
387 #else /* PGSZ == 64 */
388 leftShift = (xoffSrc - xoffDst) << (6 - PWSH);
391 rightShift = PGSZ - leftShift;
396 rightShift = (xoffDst - xoffSrc) << 3;
399 rightShift = (xoffDst - xoffSrc) << (5 - PWSH);
400 #else /* PGSZ == 64 */
401 rightShift = (xoffDst - xoffSrc) << (6 - PWSH);
404 leftShift = PGSZ - rightShift;
410 pdstLine += widthDst;
411 psrcLine += widthSrc;
413 if (xoffSrc > xoffDst)
417 bits1 = BitLeft(bits,leftShift);
419 bits1 |= BitRight(bits,rightShift);
420 *pdst = MROP_MASK(bits1, *pdst, startmask);
425 #ifdef LARGE_INSTRUCTION_CACHE
428 #ifdef FAST_CONSTANT_OFFSET_MODE
430 psrc += nl & (UNROLL-1);
431 pdst += nl & (UNROLL-1);
435 pdst[-n] = MROP_SOLID(BitLeft(bits1, leftShift) | BitRight(bits, rightShift), pdst[-n]);
437 #define BodyEven(n) \
439 pdst[-n] = MROP_SOLID(BitLeft(bits, leftShift) | BitRight(bits1, rightShift), pdst[-n]);
449 *pdst = MROP_SOLID(BitLeft(bits1, leftShift) | BitRight(bits, rightShift), *pdst); \
452 #define BodyEven(n) \
454 *pdst = MROP_SOLID(BitLeft(bits, leftShift) | BitRight(bits1, rightShift), *pdst); \
459 #endif /* !FAST_CONSTANT_OFFSET_MODE */
469 bits1 = BitLeft(bits, leftShift);
471 *pdst = MROP_SOLID (bits1 | BitRight(bits, rightShift), *pdst);
478 bits1 = BitLeft(bits, leftShift);
479 if (BitLeft(endmask, rightShift))
482 bits1 |= BitRight(bits, rightShift);
484 *pdst = MROP_MASK (bits1, *pdst, endmask);
488 #endif /* DO_UNALIGNED_BITBLT */
490 #endif /* ! DO_MEMCPY */
491 else /* xdir == -1 */
494 xoffSrc = (pptSrc->x + w) & 3;
495 xoffDst = pbox->x2 & 3;
496 pdstLine += ((pbox->x2 * 3 - 1) >> 2) + 1;
497 psrcLine += (((pptSrc->x+w) * 3 - 1) >> 2) + 1;
499 xoffSrc = (pptSrc->x + w - 1) & PIM;
500 xoffDst = (pbox->x2 - 1) & PIM;
501 pdstLine += ((pbox->x2-1) >> PWSH) + 1;
502 psrcLine += ((pptSrc->x+w - 1) >> PWSH) + 1;
504 #ifdef DO_UNALIGNED_BITBLT
506 nl = xoffDst - xoffSrc;
508 nl = xoffSrc - xoffDst;
510 psrcLine = (unsigned long *)
511 (((unsigned char *) psrcLine) + nl);
513 if (xoffSrc == xoffDst)
520 pdstLine += widthDst;
521 psrcLine += widthSrc;
526 *pdst = MROP_MASK (*psrc, *pdst, endmask);
530 #ifdef LARGE_INSTRUCTION_CACHE
531 #ifdef FAST_CONSTANT_OFFSET_MODE
532 psrc -= nl & (UNROLL - 1);
533 pdst -= nl & (UNROLL - 1);
535 #define BodyOdd(n) pdst[n-1] = MROP_SOLID (psrc[n-1], pdst[n-1]);
537 #define BodyEven(n) BodyOdd(n)
545 #define BodyOdd(n) --pdst; --psrc; *pdst = MROP_SOLID(*psrc, *pdst);
546 #define BodyEven(n) BodyOdd(n)
558 --pdst; --psrc; *pdst = MROP_SOLID (*psrc, *pdst);)
565 *pdst = MROP_MASK(*psrc, *pdst, startmask);
569 #ifndef DO_UNALIGNED_BITBLT
572 if (xoffDst > xoffSrc)
575 leftShift = (xoffDst - xoffSrc) << 3;
576 rightShift = PGSZ - leftShift;
579 rightShift = (xoffDst - xoffSrc) << (5 - PWSH);
580 #else /* PGSZ == 64 */
581 rightShift = (xoffDst - xoffSrc) << (6 - PWSH);
583 leftShift = PGSZ - rightShift;
589 rightShift = (xoffSrc - xoffDst) << 3;
590 leftShift = PGSZ - rightShift;
593 leftShift = (xoffSrc - xoffDst) << (5 - PWSH);
594 #else /* PGSZ == 64 */
595 leftShift = (xoffSrc - xoffDst) << (6 - PWSH);
597 rightShift = PGSZ - leftShift;
604 pdstLine += widthDst;
605 psrcLine += widthSrc;
608 if (xoffSrc > xoffDst)
610 if (xoffDst > xoffSrc)
615 bits1 = BitRight(bits, rightShift);
617 bits1 |= BitLeft(bits, leftShift);
619 *pdst = MROP_MASK(bits1, *pdst, endmask);
623 #ifdef LARGE_INSTRUCTION_CACHE
625 #ifdef FAST_CONSTANT_OFFSET_MODE
626 psrc -= nl & (UNROLL - 1);
627 pdst -= nl & (UNROLL - 1);
631 pdst[n-1] = MROP_SOLID(BitRight(bits1, rightShift) | BitLeft(bits, leftShift),pdst[n-1]);
633 #define BodyEven(n) \
635 pdst[n-1] = MROP_SOLID(BitRight(bits, rightShift) | BitLeft(bits1, leftShift),pdst[n-1]);
644 bits = *--psrc; --pdst; \
645 *pdst = MROP_SOLID(BitRight(bits1, rightShift) | BitLeft(bits, leftShift),*pdst);
647 #define BodyEven(n) \
648 bits1 = *--psrc; --pdst; \
649 *pdst = MROP_SOLID(BitRight(bits, rightShift) | BitLeft(bits1, leftShift),*pdst);
663 bits1 = BitRight(bits, rightShift);
666 *pdst = MROP_SOLID(bits1 | BitLeft(bits, leftShift),*pdst);
672 bits1 = BitRight(bits, rightShift);
673 if (BitRight (startmask, leftShift))
676 bits1 |= BitLeft(bits, leftShift);
679 *pdst = MROP_MASK(bits1, *pdst, startmask);
690 DEALLOCATE_LOCAL(pptNew2);
691 DEALLOCATE_LOCAL(pboxNew2);
695 DEALLOCATE_LOCAL(pptNew1);
696 DEALLOCATE_LOCAL(pboxNew1);