First import
[xorg_rtime.git] / xorg-server-1.4 / afb / afbbitblt.c
blob2e416e3564ee52609df23e3e2ea2705fa2c5f4ab
1 /* Combined Purdue/PurduePlus patches, level 2.0, 1/17/89 */
2 /***********************************************************
4 Copyright (c) 1987 X Consortium
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
20 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 Except as contained in this notice, the name of the X Consortium shall not be
24 used in advertising or otherwise to promote the sale, use or other dealings
25 in this Software without prior written authorization from the X Consortium.
28 Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
30 All Rights Reserved
32 Permission to use, copy, modify, and distribute this software and its
33 documentation for any purpose and without fee is hereby granted,
34 provided that the above copyright notice appear in all copies and that
35 both that copyright notice and this permission notice appear in
36 supporting documentation, and that the name of Digital not be
37 used in advertising or publicity pertaining to distribution of the
38 software without specific, written prior permission.
40 DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
41 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
42 DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
43 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
44 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
45 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
46 SOFTWARE.
48 ******************************************************************/
50 #ifdef HAVE_DIX_CONFIG_H
51 #include <dix-config.h>
52 #endif
54 #include <stdlib.h>
56 #include <X11/X.h>
57 #include <X11/Xprotostr.h>
59 #include "regionstr.h"
60 #include "gcstruct.h"
61 #include "windowstr.h"
62 #include "pixmapstr.h"
63 #include "scrnintstr.h"
65 #include "mi.h"
67 #include "afb.h"
68 #include "maskbits.h"
70 /* CopyArea and CopyPlane for a monchrome frame buffer
73 clip the source rectangle to the source's available bits. (this
74 avoids copying unnecessary pieces that will just get exposed anyway.)
75 this becomes the new shape of the destination.
76 clip the destination region to the composite clip in the
77 GC. this requires translating the destination region to (dstx, dsty).
78 build a list of source points, one for each rectangle in the
79 destination. this is a simple translation.
80 go do the multiple rectangle copies
81 do graphics exposures
83 /** Optimized for drawing pixmaps into windows, especially when drawing into
84 ** unobscured windows. Calls to the general-purpose region code were
85 ** replaced with rectangle-to-rectangle clipping comparisions. This is
86 ** possible, since the pixmap is a single rectangle. In an unobscured
87 ** window, the destination clip is also a single rectangle, and region
88 ** code can be avoided entirely. This is a big savings, since the region
89 ** code uses XAlloc() and makes many function calls.
91 ** In addition, if source is a pixmap, there is no need to call the
92 ** expensive miHandleExposures() routine. Instead, we simply return NULL.
94 ** Previously, drawing a pixmap into an unobscured window executed at least
95 ** 8 XAlloc()'s, 30 function calls, and hundreds of lines of code.
97 ** Now, the same operation requires no XAlloc()'s, no region function calls,
98 ** and much less overhead. Nice for drawing lots of small pixmaps.
101 void
102 afbDoBitblt(DrawablePtr pSrc, DrawablePtr pDst, int alu, RegionPtr prgnDst, DDXPointPtr pptSrc, long unsigned int planemask)
104 switch (alu) {
105 case GXcopy:
106 afbDoBitbltCopy(pSrc, pDst, alu, prgnDst, pptSrc, planemask);
107 break;
108 case GXxor:
109 afbDoBitbltXor(pSrc, pDst, alu, prgnDst, pptSrc, planemask);
110 break;
111 case GXcopyInverted:
112 afbDoBitbltCopyInverted(pSrc, pDst, alu, prgnDst, pptSrc, planemask);
113 break;
114 case GXor:
115 afbDoBitbltOr(pSrc, pDst, alu, prgnDst, pptSrc, planemask);
116 break;
117 default:
118 afbDoBitbltGeneral(pSrc, pDst, alu, prgnDst, pptSrc, planemask);
119 break;
123 typedef void (*afb_blit_func)
124 (DrawablePtr, DrawablePtr, int, RegionPtr, DDXPointPtr, unsigned long);
126 static RegionPtr
127 afbBitBlt(register DrawablePtr pSrcDrawable, register DrawablePtr pDstDrawable, register GC *pGC, int srcx, int srcy, int width, int height, int dstx, int dsty, afb_blit_func doBitBlt, long unsigned int planemask)
129 RegionPtr prgnSrcClip = NULL; /* may be a new region, or just a copy */
130 Bool freeSrcClip = FALSE;
132 RegionPtr prgnExposed;
133 RegionRec rgnDst;
134 DDXPointPtr pptSrc;
135 register DDXPointPtr ppt;
136 register BoxPtr pbox;
137 int i;
138 register int dx;
139 register int dy;
140 xRectangle origSource;
141 DDXPointRec origDest;
142 int numRects;
143 BoxRec fastBox;
144 int fastClip = 0; /* for fast clipping with pixmap source */
145 int fastExpose = 0; /* for fast exposures with pixmap source */
147 origSource.x = srcx;
148 origSource.y = srcy;
149 origSource.width = width;
150 origSource.height = height;
151 origDest.x = dstx;
152 origDest.y = dsty;
154 if ((pSrcDrawable != pDstDrawable) && pSrcDrawable->pScreen->SourceValidate)
155 (*pSrcDrawable->pScreen->SourceValidate)(pSrcDrawable, srcx, srcy, width,
156 height);
158 srcx += pSrcDrawable->x;
159 srcy += pSrcDrawable->y;
161 /* clip the source */
163 if (pSrcDrawable->type == DRAWABLE_PIXMAP)
164 if ((pSrcDrawable == pDstDrawable) && (pGC->clientClipType == CT_NONE))
165 prgnSrcClip = pGC->pCompositeClip;
166 else
167 fastClip = 1;
168 else if (pGC->subWindowMode == IncludeInferiors)
169 if (!((WindowPtr)pSrcDrawable)->parent)
171 * special case bitblt from root window in
172 * IncludeInferiors mode; just like from a pixmap
174 fastClip = 1;
175 else if ((pSrcDrawable == pDstDrawable) && (pGC->clientClipType == CT_NONE))
176 prgnSrcClip = pGC->pCompositeClip;
177 else {
178 prgnSrcClip = NotClippedByChildren((WindowPtr)pSrcDrawable);
179 freeSrcClip = TRUE;
181 else
182 prgnSrcClip = &((WindowPtr)pSrcDrawable)->clipList;
184 fastBox.x1 = srcx;
185 fastBox.y1 = srcy;
186 fastBox.x2 = srcx + width;
187 fastBox.y2 = srcy + height;
189 /* Don't create a source region if we are doing a fast clip */
190 if (fastClip) {
191 fastExpose = 1;
193 * clip the source; if regions extend beyond the source size,
194 * make sure exposure events get sent
196 if (fastBox.x1 < pSrcDrawable->x) {
197 fastBox.x1 = pSrcDrawable->x;
198 fastExpose = 0;
200 if (fastBox.y1 < pSrcDrawable->y) {
201 fastBox.y1 = pSrcDrawable->y;
202 fastExpose = 0;
204 if (fastBox.x2 > pSrcDrawable->x + (int)pSrcDrawable->width) {
205 fastBox.x2 = pSrcDrawable->x + (int)pSrcDrawable->width;
206 fastExpose = 0;
208 if (fastBox.y2 > pSrcDrawable->y + (int)pSrcDrawable->height) {
209 fastBox.y2 = pSrcDrawable->y + (int)pSrcDrawable->height;
210 fastExpose = 0;
212 } else {
213 REGION_INIT(pGC->pScreen, &rgnDst, &fastBox, 1);
214 REGION_INTERSECT(pGC->pScreen, &rgnDst, &rgnDst, prgnSrcClip);
217 dstx += pDstDrawable->x;
218 dsty += pDstDrawable->y;
220 if (pDstDrawable->type == DRAWABLE_WINDOW)
221 if (!((WindowPtr)pDstDrawable)->realized) {
222 if (!fastClip)
223 REGION_UNINIT(pGC->pScreen, &rgnDst);
224 if (freeSrcClip)
225 REGION_DESTROY(pGC->pScreen, prgnSrcClip);
226 return NULL;
229 dx = srcx - dstx;
230 dy = srcy - dsty;
232 /* Translate and clip the dst to the destination composite clip */
233 if (fastClip) {
234 RegionPtr cclip;
236 /* Translate the region directly */
237 fastBox.x1 -= dx;
238 fastBox.x2 -= dx;
239 fastBox.y1 -= dy;
240 fastBox.y2 -= dy;
242 /* If the destination composite clip is one rectangle we can
243 do the clip directly. Otherwise we have to create a full
244 blown region and call intersect */
245 cclip = pGC->pCompositeClip;
246 if (REGION_NUM_RECTS(cclip) == 1) {
247 BoxPtr pBox = REGION_RECTS(cclip);
249 if (fastBox.x1 < pBox->x1)
250 fastBox.x1 = pBox->x1;
251 if (fastBox.x2 > pBox->x2)
252 fastBox.x2 = pBox->x2;
253 if (fastBox.y1 < pBox->y1)
254 fastBox.y1 = pBox->y1;
255 if (fastBox.y2 > pBox->y2)
256 fastBox.y2 = pBox->y2;
258 /* Check to see if the region is empty */
259 if (fastBox.x1 >= fastBox.x2 || fastBox.y1 >= fastBox.y2) {
260 REGION_NULL(pGC->pScreen, &rgnDst);
261 } else {
262 REGION_INIT(pGC->pScreen, &rgnDst, &fastBox, 1);
264 } else {
265 /* We must turn off fastClip now, since we must create
266 a full blown region. It is intersected with the
267 composite clip below. */
268 fastClip = 0;
269 REGION_INIT(pGC->pScreen, &rgnDst, &fastBox,1);
271 } else
272 REGION_TRANSLATE(pGC->pScreen, &rgnDst, -dx, -dy);
274 if (!fastClip) {
275 REGION_INTERSECT(pGC->pScreen, &rgnDst, &rgnDst,
276 pGC->pCompositeClip);
279 /* Do bit blitting */
280 numRects = REGION_NUM_RECTS(&rgnDst);
281 if (numRects && width && height) {
282 if(!(pptSrc = (DDXPointPtr)ALLOCATE_LOCAL(numRects *
283 sizeof(DDXPointRec)))) {
284 REGION_UNINIT(pGC->pScreen, &rgnDst);
285 if (freeSrcClip)
286 REGION_DESTROY(pGC->pScreen, prgnSrcClip);
287 return NULL;
289 pbox = REGION_RECTS(&rgnDst);
290 ppt = pptSrc;
291 for (i = numRects; --i >= 0; pbox++, ppt++) {
292 ppt->x = pbox->x1 + dx;
293 ppt->y = pbox->y1 + dy;
296 (*doBitBlt)(pSrcDrawable, pDstDrawable, pGC->alu, &rgnDst, pptSrc,
297 planemask);
299 DEALLOCATE_LOCAL(pptSrc);
302 prgnExposed = NULL;
303 if (pGC->fExpose) {
304 /* Pixmap sources generate a NoExposed (we return NULL to do this) */
305 if (!fastExpose)
306 prgnExposed = miHandleExposures(pSrcDrawable, pDstDrawable, pGC,
307 origSource.x, origSource.y,
308 (int)origSource.width,
309 (int)origSource.height, origDest.x,
310 origDest.y, (unsigned long)0);
312 REGION_UNINIT(pGC->pScreen, &rgnDst);
313 if (freeSrcClip)
314 REGION_DESTROY(pGC->pScreen, prgnSrcClip);
315 return prgnExposed;
318 RegionPtr
319 afbCopyArea(DrawablePtr pSrcDrawable, DrawablePtr pDstDrawable, GC *pGC, int srcx, int srcy, int width, int height, int dstx, int dsty)
321 afb_blit_func doBitBlt;
323 switch (pGC->alu) {
324 case GXcopy:
325 doBitBlt = afbDoBitbltCopy;
326 break;
327 case GXxor:
328 doBitBlt = afbDoBitbltXor;
329 break;
330 case GXcopyInverted:
331 doBitBlt = afbDoBitbltCopyInverted;
332 break;
333 case GXor:
334 doBitBlt = afbDoBitbltOr;
335 break;
336 default:
337 doBitBlt = afbDoBitbltGeneral;
338 break;
341 return(afbBitBlt(pSrcDrawable, pDstDrawable, pGC, srcx, srcy,
342 width, height, dstx, dsty, doBitBlt, pGC->planemask));