First import
[xorg_rtime.git] / xorg-server-1.4 / mfb / mfbbitblt.c
blob0f84df35468d2783ea86820887feb723f6cb1322
1 /* Combined Purdue/PurduePlus patches, level 2.0, 1/17/89 */
2 /***********************************************************
4 Copyright 1987, 1998 The Open Group
6 Permission to use, copy, modify, distribute, and sell this software and its
7 documentation for any purpose is hereby granted without fee, provided that
8 the above copyright notice appear in all copies and that both that
9 copyright notice and this permission notice appear in supporting
10 documentation.
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
19 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 Except as contained in this notice, the name of The Open Group shall not be
23 used in advertising or otherwise to promote the sale, use or other dealings
24 in this Software without prior written authorization from The Open Group.
27 Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
29 All Rights Reserved
31 Permission to use, copy, modify, and distribute this software and its
32 documentation for any purpose and without fee is hereby granted,
33 provided that the above copyright notice appear in all copies and that
34 both that copyright notice and this permission notice appear in
35 supporting documentation, and that the name of Digital not be
36 used in advertising or publicity pertaining to distribution of the
37 software without specific, written prior permission.
39 DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
40 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
41 DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
42 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
43 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
44 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
45 SOFTWARE.
47 ******************************************************************/
48 #ifdef HAVE_DIX_CONFIG_H
49 #include <dix-config.h>
50 #endif
52 #include <X11/X.h>
53 #include <X11/Xprotostr.h>
55 #include <stdlib.h>
57 #include "regionstr.h"
58 #include "gcstruct.h"
59 #include "windowstr.h"
60 #include "pixmapstr.h"
61 #include "scrnintstr.h"
63 #include "mi.h"
65 #include "mfb.h"
66 #include "maskbits.h"
69 /* CopyArea and CopyPlane for a monchrome frame buffer
72 clip the source rectangle to the source's available bits. (this
73 avoids copying unnecessary pieces that will just get exposed anyway.)
74 this becomes the new shape of the destination.
75 clip the destination region to the composite clip in the
76 GC. this requires translating the destination region to (dstx, dsty).
77 build a list of source points, one for each rectangle in the
78 destination. this is a simple translation.
79 go do the multiple rectangle copies
80 do graphics exposures
82 /** Optimized for drawing pixmaps into windows, especially when drawing into
83 ** unobscured windows. Calls to the general-purpose region code were
84 ** replaced with rectangle-to-rectangle clipping comparisions. This is
85 ** possible, since the pixmap is a single rectangle. In an unobscured
86 ** window, the destination clip is also a single rectangle, and region
87 ** code can be avoided entirely. This is a big savings, since the region
88 ** code uses XAlloc() and makes many function calls.
90 ** In addition, if source is a pixmap, there is no need to call the
91 ** expensive miHandleExposures() routine. Instead, we simply return NULL.
93 ** Previously, drawing a pixmap into an unobscured window executed at least
94 ** 8 XAlloc()'s, 30 function calls, and hundreds of lines of code.
96 ** Now, the same operation requires no XAlloc()'s, no region function calls,
97 ** and much less overhead. Nice for drawing lots of small pixmaps.
101 void
102 mfbDoBitblt (pSrc, pDst, alu, prgnDst, pptSrc)
103 DrawablePtr pSrc, pDst;
104 int alu;
105 RegionPtr prgnDst;
106 DDXPointPtr pptSrc;
108 switch (alu)
110 case GXcopy:
111 mfbDoBitbltCopy (pSrc, pDst, alu, prgnDst, pptSrc);
112 break;
113 case GXxor:
114 mfbDoBitbltXor (pSrc, pDst, alu, prgnDst, pptSrc);
115 break;
116 case GXcopyInverted:
117 mfbDoBitbltCopyInverted (pSrc, pDst, alu, prgnDst, pptSrc);
118 break;
119 case GXor:
120 mfbDoBitbltOr (pSrc, pDst, alu, prgnDst, pptSrc);
121 break;
122 default:
123 mfbDoBitbltGeneral (pSrc, pDst, alu, prgnDst, pptSrc);
124 break;
128 RegionPtr
129 mfbCopyArea(pSrcDrawable, pDstDrawable,
130 pGC, srcx, srcy, width, height, dstx, dsty)
131 register DrawablePtr pSrcDrawable;
132 register DrawablePtr pDstDrawable;
133 register GC *pGC;
134 int srcx, srcy;
135 int width, height;
136 int dstx, dsty;
138 RegionPtr prgnSrcClip = NULL; /* may be a new region, or just a copy */
139 Bool freeSrcClip = FALSE;
141 RegionPtr prgnExposed;
142 RegionRec rgnDst;
143 DDXPointPtr pptSrc;
144 register DDXPointPtr ppt;
145 register BoxPtr pbox;
146 int i;
147 register int dx;
148 register int dy;
149 xRectangle origSource;
150 DDXPointRec origDest;
151 int numRects;
152 BoxRec fastBox;
153 int fastClip = 0; /* for fast clipping with pixmap source */
154 int fastExpose = 0; /* for fast exposures with pixmap source */
155 void (*localDoBitBlt)(
156 DrawablePtr /*pSrc*/,
157 DrawablePtr /*pDst*/,
158 int /*alu*/,
159 RegionPtr /*prgnDst*/,
160 DDXPointPtr /*pptSrc*/);
162 origSource.x = srcx;
163 origSource.y = srcy;
164 origSource.width = width;
165 origSource.height = height;
166 origDest.x = dstx;
167 origDest.y = dsty;
169 if ((pSrcDrawable != pDstDrawable) &&
170 pSrcDrawable->pScreen->SourceValidate)
172 (*pSrcDrawable->pScreen->SourceValidate) (pSrcDrawable, srcx, srcy, width, height);
175 switch (pGC->alu) {
176 case GXcopy:
177 localDoBitBlt = mfbDoBitbltCopy;
178 break;
179 case GXcopyInverted:
180 localDoBitBlt = mfbDoBitbltCopyInverted;
181 break;
182 case GXxor:
183 localDoBitBlt = mfbDoBitbltXor;
184 break;
185 case GXor:
186 localDoBitBlt = mfbDoBitbltOr;
187 break;
188 default:
189 localDoBitBlt = mfbDoBitbltGeneral;
190 break;
193 srcx += pSrcDrawable->x;
194 srcy += pSrcDrawable->y;
196 /* clip the source */
198 if (pSrcDrawable->type == DRAWABLE_PIXMAP)
200 if ((pSrcDrawable == pDstDrawable) &&
201 (pGC->clientClipType == CT_NONE))
203 prgnSrcClip = pGC->pCompositeClip;
205 else
207 fastClip = 1;
210 else
212 if (pGC->subWindowMode == IncludeInferiors)
214 if (!((WindowPtr) pSrcDrawable)->parent)
217 * special case bitblt from root window in
218 * IncludeInferiors mode; just like from a pixmap
220 fastClip = 1;
222 else if ((pSrcDrawable == pDstDrawable) &&
223 (pGC->clientClipType == CT_NONE))
225 prgnSrcClip = pGC->pCompositeClip;
227 else
229 prgnSrcClip = NotClippedByChildren((WindowPtr)pSrcDrawable);
230 freeSrcClip = TRUE;
233 else
235 prgnSrcClip = &((WindowPtr)pSrcDrawable)->clipList;
239 fastBox.x1 = srcx;
240 fastBox.y1 = srcy;
241 fastBox.x2 = srcx + width;
242 fastBox.y2 = srcy + height;
244 /* Don't create a source region if we are doing a fast clip */
245 if (fastClip)
247 fastExpose = 1;
249 * clip the source; if regions extend beyond the source size,
250 * make sure exposure events get sent
252 if (fastBox.x1 < pSrcDrawable->x)
254 fastBox.x1 = pSrcDrawable->x;
255 fastExpose = 0;
257 if (fastBox.y1 < pSrcDrawable->y)
259 fastBox.y1 = pSrcDrawable->y;
260 fastExpose = 0;
262 if (fastBox.x2 > pSrcDrawable->x + (int) pSrcDrawable->width)
264 fastBox.x2 = pSrcDrawable->x + (int) pSrcDrawable->width;
265 fastExpose = 0;
267 if (fastBox.y2 > pSrcDrawable->y + (int) pSrcDrawable->height)
269 fastBox.y2 = pSrcDrawable->y + (int) pSrcDrawable->height;
270 fastExpose = 0;
273 else
275 REGION_INIT(pGC->pScreen, &rgnDst, &fastBox, 1);
276 REGION_INTERSECT(pGC->pScreen, &rgnDst, &rgnDst, prgnSrcClip);
279 dstx += pDstDrawable->x;
280 dsty += pDstDrawable->y;
282 if (pDstDrawable->type == DRAWABLE_WINDOW)
284 if (!((WindowPtr)pDstDrawable)->realized)
286 if (!fastClip)
287 REGION_UNINIT(pGC->pScreen, &rgnDst);
288 if (freeSrcClip)
289 REGION_DESTROY(pGC->pScreen, prgnSrcClip);
290 return NULL;
294 dx = srcx - dstx;
295 dy = srcy - dsty;
297 /* Translate and clip the dst to the destination composite clip */
298 if (fastClip)
300 RegionPtr cclip;
302 /* Translate the region directly */
303 fastBox.x1 -= dx;
304 fastBox.x2 -= dx;
305 fastBox.y1 -= dy;
306 fastBox.y2 -= dy;
308 /* If the destination composite clip is one rectangle we can
309 do the clip directly. Otherwise we have to create a full
310 blown region and call intersect */
311 cclip = pGC->pCompositeClip;
312 if (REGION_NUM_RECTS(cclip) == 1)
314 BoxPtr pBox = REGION_RECTS(cclip);
316 if (fastBox.x1 < pBox->x1) fastBox.x1 = pBox->x1;
317 if (fastBox.x2 > pBox->x2) fastBox.x2 = pBox->x2;
318 if (fastBox.y1 < pBox->y1) fastBox.y1 = pBox->y1;
319 if (fastBox.y2 > pBox->y2) fastBox.y2 = pBox->y2;
321 /* Check to see if the region is empty */
322 if (fastBox.x1 >= fastBox.x2 || fastBox.y1 >= fastBox.y2)
324 REGION_NULL(pGC->pScreen, &rgnDst);
326 else
328 REGION_INIT(pGC->pScreen, &rgnDst, &fastBox, 1);
331 else
333 /* We must turn off fastClip now, since we must create
334 a full blown region. It is intersected with the
335 composite clip below. */
336 fastClip = 0;
337 REGION_INIT(pGC->pScreen, &rgnDst, &fastBox, 1);
340 else
342 REGION_TRANSLATE(pGC->pScreen, &rgnDst, -dx, -dy);
345 if (!fastClip)
347 REGION_INTERSECT(pGC->pScreen, &rgnDst, &rgnDst, pGC->pCompositeClip);
350 /* Do bit blitting */
351 numRects = REGION_NUM_RECTS(&rgnDst);
352 if (numRects && width && height)
354 if(!(pptSrc = (DDXPointPtr)ALLOCATE_LOCAL(numRects *
355 sizeof(DDXPointRec))))
357 REGION_UNINIT(pGC->pScreen, &rgnDst);
358 if (freeSrcClip)
359 REGION_DESTROY(pGC->pScreen, prgnSrcClip);
360 return NULL;
362 pbox = REGION_RECTS(&rgnDst);
363 ppt = pptSrc;
364 for (i = numRects; --i >= 0; pbox++, ppt++)
366 ppt->x = pbox->x1 + dx;
367 ppt->y = pbox->y1 + dy;
370 if (pGC->planemask & 1)
371 (*localDoBitBlt) (pSrcDrawable, pDstDrawable, pGC->alu, &rgnDst, pptSrc);
373 DEALLOCATE_LOCAL(pptSrc);
376 prgnExposed = NULL;
377 if (pGC->fExpose)
379 /* Pixmap sources generate a NoExposed (we return NULL to do this) */
380 if (!fastExpose)
381 prgnExposed =
382 miHandleExposures(pSrcDrawable, pDstDrawable, pGC,
383 origSource.x, origSource.y,
384 (int)origSource.width,
385 (int)origSource.height,
386 origDest.x, origDest.y, (unsigned long)0);
388 REGION_UNINIT(pGC->pScreen, &rgnDst);
389 if (freeSrcClip)
390 REGION_DESTROY(pGC->pScreen, prgnSrcClip);
391 return prgnExposed;
396 * Devices which use mfb for 1-bit pixmap support
397 * must register a function for n-to-1 copy operations
400 static unsigned long copyPlaneGeneration;
401 static int copyPlaneScreenIndex = -1;
403 Bool
404 mfbRegisterCopyPlaneProc (pScreen, proc)
405 ScreenPtr pScreen;
406 RegionPtr (*proc)(
407 DrawablePtr /* pSrcDrawable */,
408 DrawablePtr /* pDstDrawable */,
409 GCPtr /* pGC */,
410 int /* srcx */,
411 int /* srcy */,
412 int /* width */,
413 int /* height */,
414 int /* dstx */,
415 int /* dsty */,
416 unsigned long /* bitPlane */);
418 if (copyPlaneGeneration != serverGeneration)
420 copyPlaneScreenIndex = AllocateScreenPrivateIndex();
421 if (copyPlaneScreenIndex < 0)
422 return FALSE;
423 copyPlaneGeneration = serverGeneration;
425 pScreen->devPrivates[copyPlaneScreenIndex].fptr = proc;
426 return TRUE;
430 if fg == 1 and bg ==0, we can do an ordinary CopyArea.
431 if fg == bg, we can do a CopyArea with alu = mfbReduceRop(alu, fg)
432 if fg == 0 and bg == 1, we use the same rasterop, with
433 source operand inverted.
435 CopyArea deals with all of the graphics exposure events.
436 This code depends on knowing that we can change the
437 alu in the GC without having to call ValidateGC() before calling
438 CopyArea().
443 RegionPtr
444 mfbCopyPlane(pSrcDrawable, pDstDrawable,
445 pGC, srcx, srcy, width, height, dstx, dsty, plane)
446 DrawablePtr pSrcDrawable, pDstDrawable;
447 register GC *pGC;
448 int srcx, srcy;
449 int width, height;
450 int dstx, dsty;
451 unsigned long plane;
453 int alu;
454 RegionPtr prgnExposed;
455 RegionPtr (*copyPlane)(
456 DrawablePtr /* pSrcDrawable */,
457 DrawablePtr /* pDstDrawable */,
458 GCPtr /* pGC */,
459 int /* srcx */,
460 int /* srcy */,
461 int /* width */,
462 int /* height */,
463 int /* dstx */,
464 int /* dsty */,
465 unsigned long /* bitPlane */);
468 if (pSrcDrawable->depth != 1)
470 if (copyPlaneScreenIndex >= 0 &&
471 (copyPlane =
472 pSrcDrawable->pScreen->devPrivates[copyPlaneScreenIndex].fptr)
475 return (*copyPlane) (pSrcDrawable, pDstDrawable,
476 pGC, srcx, srcy, width, height, dstx, dsty, plane);
478 else
480 FatalError ("No copyPlane proc registered for depth %d\n",
481 pSrcDrawable->depth);
484 if (plane != 1)
485 return NULL;
487 if ((pGC->fgPixel & 1) == 1 && (pGC->bgPixel & 1) == 0)
489 prgnExposed = (*pGC->ops->CopyArea)(pSrcDrawable, pDstDrawable,
490 pGC, srcx, srcy, width, height, dstx, dsty);
492 else if ((pGC->fgPixel & 1) == (pGC->bgPixel & 1))
494 alu = pGC->alu;
495 pGC->alu = mfbReduceRop(pGC->alu, pGC->fgPixel);
496 prgnExposed = (*pGC->ops->CopyArea)(pSrcDrawable, pDstDrawable,
497 pGC, srcx, srcy, width, height, dstx, dsty);
498 pGC->alu = alu;
500 else /* need to invert the src */
502 alu = pGC->alu;
503 pGC->alu = InverseAlu[alu];
504 prgnExposed = (*pGC->ops->CopyArea)(pSrcDrawable, pDstDrawable,
505 pGC, srcx, srcy, width, height, dstx, dsty);
506 pGC->alu = alu;
508 return prgnExposed;