1 /***********************************************************
2 
3 Copyright 1987, 1998  The Open Group
4 
5 Permission to use, copy, modify, distribute, and sell this software and its
6 documentation for any purpose is hereby granted without fee, provided that
7 the above copyright notice appear in all copies and that both that
8 copyright notice and this permission notice appear in supporting
9 documentation.
10 
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
17 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 
21 Except as contained in this notice, the name of The Open Group shall not be
22 used in advertising or otherwise to promote the sale, use or other dealings
23 in this Software without prior written authorization from The Open Group.
24 
25 Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
26 
27                         All Rights Reserved
28 
29 Permission to use, copy, modify, and distribute this software and its
30 documentation for any purpose and without fee is hereby granted,
31 provided that the above copyright notice appear in all copies and that
32 both that copyright notice and this permission notice appear in
33 supporting documentation, and that the name of Digital not be
34 used in advertising or publicity pertaining to distribution of the
35 software without specific, written prior permission.
36 
37 DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
38 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
39 DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
40 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
41 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
42 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
43 SOFTWARE.
44 
45 ******************************************************************/
46 /* Author: Todd Newman  (aided and abetted by Mr. Drewry) */
47 
48 #ifdef HAVE_DIX_CONFIG_H
49 #include <dix-config.h>
50 #endif
51 
52 #include <X11/X.h>
53 #include <X11/Xprotostr.h>
54 
55 #include "misc.h"
56 #include "gcstruct.h"
57 #include "pixmapstr.h"
58 #include "windowstr.h"
59 #include "scrnintstr.h"
60 #include "mi.h"
61 #include "regionstr.h"
62 #include <X11/Xmd.h>
63 #include "servermd.h"
64 
65 /* MICOPYAREA -- public entry for the CopyArea request
66  * For each rectangle in the source region
67  *     get the pixels with GetSpans
68  *     set them in the destination with SetSpans
69  * We let SetSpans worry about clipping to the destination.
70  */
71 _X_COLD RegionPtr
miCopyArea(DrawablePtr pSrcDrawable,DrawablePtr pDstDrawable,GCPtr pGC,int xIn,int yIn,int widthSrc,int heightSrc,int xOut,int yOut)72 miCopyArea(DrawablePtr pSrcDrawable,
73            DrawablePtr pDstDrawable,
74            GCPtr pGC,
75            int xIn, int yIn, int widthSrc, int heightSrc, int xOut, int yOut)
76 {
77     DDXPointPtr ppt, pptFirst;
78     unsigned int *pwidthFirst, *pwidth, *pbits;
79     BoxRec srcBox, *prect;
80 
81     /* may be a new region, or just a copy */
82     RegionPtr prgnSrcClip;
83 
84     /* non-0 if we've created a src clip */
85     RegionPtr prgnExposed;
86     int realSrcClip = 0;
87     int srcx, srcy, dstx, dsty, i, j, y, width, height, xMin, xMax, yMin, yMax;
88     unsigned int *ordering;
89     int numRects;
90     BoxPtr boxes;
91 
92     srcx = xIn + pSrcDrawable->x;
93     srcy = yIn + pSrcDrawable->y;
94 
95     /* If the destination isn't realized, this is easy */
96     if (pDstDrawable->type == DRAWABLE_WINDOW &&
97         !((WindowPtr) pDstDrawable)->realized)
98         return NULL;
99 
100     /* clip the source */
101     if (pSrcDrawable->type == DRAWABLE_PIXMAP) {
102         BoxRec box;
103 
104         box.x1 = pSrcDrawable->x;
105         box.y1 = pSrcDrawable->y;
106         box.x2 = pSrcDrawable->x + (int) pSrcDrawable->width;
107         box.y2 = pSrcDrawable->y + (int) pSrcDrawable->height;
108 
109         prgnSrcClip = RegionCreate(&box, 1);
110         realSrcClip = 1;
111     }
112     else {
113         if (pGC->subWindowMode == IncludeInferiors) {
114             prgnSrcClip = NotClippedByChildren((WindowPtr) pSrcDrawable);
115             realSrcClip = 1;
116         }
117         else
118             prgnSrcClip = &((WindowPtr) pSrcDrawable)->clipList;
119     }
120 
121     /* If the src drawable is a window, we need to translate the srcBox so
122      * that we can compare it with the window's clip region later on. */
123     srcBox.x1 = srcx;
124     srcBox.y1 = srcy;
125     srcBox.x2 = srcx + widthSrc;
126     srcBox.y2 = srcy + heightSrc;
127 
128     dstx = xOut;
129     dsty = yOut;
130     if (pGC->miTranslate) {
131         dstx += pDstDrawable->x;
132         dsty += pDstDrawable->y;
133     }
134 
135     pptFirst = ppt = xallocarray(heightSrc, sizeof(DDXPointRec));
136     pwidthFirst = pwidth = xallocarray(heightSrc, sizeof(unsigned int));
137     numRects = RegionNumRects(prgnSrcClip);
138     boxes = RegionRects(prgnSrcClip);
139     ordering = xallocarray(numRects, sizeof(unsigned int));
140     if (!pptFirst || !pwidthFirst || !ordering) {
141         free(ordering);
142         free(pwidthFirst);
143         free(pptFirst);
144         if (realSrcClip)
145             RegionDestroy(prgnSrcClip);
146         return NULL;
147     }
148 
149     /* If not the same drawable then order of move doesn't matter.
150        Following assumes that boxes are sorted from top
151        to bottom and left to right.
152      */
153     if ((pSrcDrawable != pDstDrawable) &&
154         ((pGC->subWindowMode != IncludeInferiors) ||
155          (pSrcDrawable->type == DRAWABLE_PIXMAP) ||
156          (pDstDrawable->type == DRAWABLE_PIXMAP)))
157         for (i = 0; i < numRects; i++)
158             ordering[i] = i;
159     else {                      /* within same drawable, must sequence moves carefully! */
160         if (dsty <= srcBox.y1) {        /* Scroll up or stationary vertical.
161                                            Vertical order OK */
162             if (dstx <= srcBox.x1)      /* Scroll left or stationary horizontal.
163                                            Horizontal order OK as well */
164                 for (i = 0; i < numRects; i++)
165                     ordering[i] = i;
166             else {              /* scroll right. must reverse horizontal banding of rects. */
167                 for (i = 0, j = 1, xMax = 0; i < numRects; j = i + 1, xMax = i) {
168                     /* find extent of current horizontal band */
169                     y = boxes[i].y1;    /* band has this y coordinate */
170                     while ((j < numRects) && (boxes[j].y1 == y))
171                         j++;
172                     /* reverse the horizontal band in the output ordering */
173                     for (j--; j >= xMax; j--, i++)
174                         ordering[i] = j;
175                 }
176             }
177         }
178         else {                  /* Scroll down. Must reverse vertical banding. */
179             if (dstx < srcBox.x1) {     /* Scroll left. Horizontal order OK. */
180                 for (i = numRects - 1, j = i - 1, yMin = i, yMax = 0;
181                      i >= 0; j = i - 1, yMin = i) {
182                     /* find extent of current horizontal band */
183                     y = boxes[i].y1;    /* band has this y coordinate */
184                     while ((j >= 0) && (boxes[j].y1 == y))
185                         j--;
186                     /* reverse the horizontal band in the output ordering */
187                     for (j++; j <= yMin; j++, i--, yMax++)
188                         ordering[yMax] = j;
189                 }
190             }
191             else                /* Scroll right or horizontal stationary.
192                                    Reverse horizontal order as well (if stationary, horizontal
193                                    order can be swapped without penalty and this is faster
194                                    to compute). */
195                 for (i = 0, j = numRects - 1; i < numRects; i++, j--)
196                     ordering[i] = j;
197         }
198     }
199 
200     for (i = 0; i < numRects; i++) {
201         prect = &boxes[ordering[i]];
202         xMin = max(prect->x1, srcBox.x1);
203         xMax = min(prect->x2, srcBox.x2);
204         yMin = max(prect->y1, srcBox.y1);
205         yMax = min(prect->y2, srcBox.y2);
206         /* is there anything visible here? */
207         if (xMax <= xMin || yMax <= yMin)
208             continue;
209 
210         ppt = pptFirst;
211         pwidth = pwidthFirst;
212         y = yMin;
213         height = yMax - yMin;
214         width = xMax - xMin;
215 
216         for (j = 0; j < height; j++) {
217             /* We must untranslate before calling GetSpans */
218             ppt->x = xMin;
219             ppt++->y = y++;
220             *pwidth++ = width;
221         }
222         pbits = xallocarray(height, PixmapBytePad(width, pSrcDrawable->depth));
223         if (pbits) {
224             (*pSrcDrawable->pScreen->GetSpans) (pSrcDrawable, width, pptFirst,
225                                                 (int *) pwidthFirst, height,
226                                                 (char *) pbits);
227             ppt = pptFirst;
228             pwidth = pwidthFirst;
229             xMin -= (srcx - dstx);
230             y = yMin - (srcy - dsty);
231             for (j = 0; j < height; j++) {
232                 ppt->x = xMin;
233                 ppt++->y = y++;
234                 *pwidth++ = width;
235             }
236 
237             (*pGC->ops->SetSpans) (pDstDrawable, pGC, (char *) pbits, pptFirst,
238                                    (int *) pwidthFirst, height, TRUE);
239             free(pbits);
240         }
241     }
242     prgnExposed = miHandleExposures(pSrcDrawable, pDstDrawable, pGC, xIn, yIn,
243                                     widthSrc, heightSrc, xOut, yOut);
244     if (realSrcClip)
245         RegionDestroy(prgnSrcClip);
246 
247     free(ordering);
248     free(pwidthFirst);
249     free(pptFirst);
250     return prgnExposed;
251 }
252 
253 /* MIGETPLANE -- gets a bitmap representing one plane of pDraw
254  * A helper used for CopyPlane and XY format GetImage
255  * No clever strategy here, we grab a scanline at a time, pull out the
256  * bits and then stuff them in a 1 bit deep map.
257  */
258 /*
259  * This should be replaced with something more general.  mi shouldn't have to
260  * care about such things as scanline padding et alia.
261  */
262 _X_COLD static MiBits *
miGetPlane(DrawablePtr pDraw,int planeNum,int sx,int sy,int w,int h,MiBits * result)263 miGetPlane(DrawablePtr pDraw, int planeNum,     /* number of the bitPlane */
264            int sx, int sy, int w, int h, MiBits * result)
265 {
266     int i, j, k, width, bitsPerPixel, widthInBytes;
267     DDXPointRec pt = { 0, 0 };
268     MiBits pixel;
269     MiBits bit;
270     unsigned char *pCharsOut = NULL;
271 
272 #if BITMAP_SCANLINE_UNIT == 8
273 #define OUT_TYPE unsigned char
274 #endif
275 #if BITMAP_SCANLINE_UNIT == 16
276 #define OUT_TYPE CARD16
277 #endif
278 #if BITMAP_SCANLINE_UNIT == 32
279 #define OUT_TYPE CARD32
280 #endif
281 #if BITMAP_SCANLINE_UNIT == 64
282 #define OUT_TYPE CARD64
283 #endif
284 
285     OUT_TYPE *pOut;
286     int delta = 0;
287 
288     sx += pDraw->x;
289     sy += pDraw->y;
290     widthInBytes = BitmapBytePad(w);
291     if (!result)
292         result = calloc(h, widthInBytes);
293     if (!result)
294         return NULL;
295     bitsPerPixel = pDraw->bitsPerPixel;
296     pOut = (OUT_TYPE *) result;
297     if (bitsPerPixel == 1) {
298         pCharsOut = (unsigned char *) result;
299         width = w;
300     }
301     else {
302         delta = (widthInBytes / (BITMAP_SCANLINE_UNIT / 8)) -
303             (w / BITMAP_SCANLINE_UNIT);
304         width = 1;
305 #if IMAGE_BYTE_ORDER == MSBFirst
306         planeNum += (32 - bitsPerPixel);
307 #endif
308     }
309     pt.y = sy;
310     for (i = h; --i >= 0; pt.y++) {
311         pt.x = sx;
312         if (bitsPerPixel == 1) {
313             (*pDraw->pScreen->GetSpans) (pDraw, width, &pt, &width, 1,
314                                          (char *) pCharsOut);
315             pCharsOut += widthInBytes;
316         }
317         else {
318             k = 0;
319             for (j = w; --j >= 0; pt.x++) {
320                 /* Fetch the next pixel */
321                 (*pDraw->pScreen->GetSpans) (pDraw, width, &pt, &width, 1,
322                                              (char *) &pixel);
323                 /*
324                  * Now get the bit and insert into a bitmap in XY format.
325                  */
326                 bit = (pixel >> planeNum) & 1;
327 #if 0
328                 /* XXX assuming bit order == byte order */
329 #if BITMAP_BIT_ORDER == LSBFirst
330                 bit <<= k;
331 #else
332                 bit <<= ((BITMAP_SCANLINE_UNIT - 1) - k);
333 #endif
334 #else
335                 /* XXX assuming byte order == LSBFirst */
336                 if (screenInfo.bitmapBitOrder == LSBFirst)
337                     bit <<= k;
338                 else
339                     bit <<= ((screenInfo.bitmapScanlineUnit - 1) -
340                              (k % screenInfo.bitmapScanlineUnit)) +
341                         ((k / screenInfo.bitmapScanlineUnit) *
342                          screenInfo.bitmapScanlineUnit);
343 #endif
344                 *pOut |= (OUT_TYPE) bit;
345                 k++;
346                 if (k == BITMAP_SCANLINE_UNIT) {
347                     pOut++;
348                     k = 0;
349                 }
350             }
351             pOut += delta;
352         }
353     }
354     return result;
355 
356 }
357 
358 /* MIOPQSTIPDRAWABLE -- use pbits as an opaque stipple for pDraw.
359  * Drawing through the clip mask we SetSpans() the bits into a
360  * bitmap and stipple those bits onto the destination drawable by doing a
361  * PolyFillRect over the whole drawable,
362  * then we invert the bitmap by copying it onto itself with an alu of
363  * GXinvert, invert the foreground/background colors of the gc, and draw
364  * the background bits.
365  * Note how the clipped out bits of the bitmap are always the background
366  * color so that the stipple never causes FillRect to draw them.
367  */
368 _X_COLD static void
miOpqStipDrawable(DrawablePtr pDraw,GCPtr pGC,RegionPtr prgnSrc,MiBits * pbits,int srcx,int w,int h,int dstx,int dsty)369 miOpqStipDrawable(DrawablePtr pDraw, GCPtr pGC, RegionPtr prgnSrc,
370                   MiBits * pbits, int srcx, int w, int h, int dstx, int dsty)
371 {
372     int oldfill, i;
373     unsigned long oldfg;
374     int *pwidth, *pwidthFirst;
375     ChangeGCVal gcv[6];
376     PixmapPtr pStipple, pPixmap;
377     DDXPointRec oldOrg;
378     GCPtr pGCT;
379     DDXPointPtr ppt, pptFirst;
380     xRectangle rect;
381     RegionPtr prgnSrcClip;
382 
383     pPixmap = (*pDraw->pScreen->CreatePixmap)
384         (pDraw->pScreen, w + srcx, h, 1, CREATE_PIXMAP_USAGE_SCRATCH);
385     if (!pPixmap)
386         return;
387 
388     /* Put the image into a 1 bit deep pixmap */
389     pGCT = GetScratchGC(1, pDraw->pScreen);
390     if (!pGCT) {
391         (*pDraw->pScreen->DestroyPixmap) (pPixmap);
392         return;
393     }
394     /* First set the whole pixmap to 0 */
395     gcv[0].val = 0;
396     ChangeGC(NullClient, pGCT, GCBackground, gcv);
397     ValidateGC((DrawablePtr) pPixmap, pGCT);
398     miClearDrawable((DrawablePtr) pPixmap, pGCT);
399     ppt = pptFirst = xallocarray(h, sizeof(DDXPointRec));
400     pwidth = pwidthFirst = xallocarray(h, sizeof(int));
401     if (!pptFirst || !pwidthFirst) {
402         free(pwidthFirst);
403         free(pptFirst);
404         FreeScratchGC(pGCT);
405         return;
406     }
407 
408     /* we need a temporary region because ChangeClip must be assumed
409        to destroy what it's sent.  note that this means we don't
410        have to free prgnSrcClip ourselves.
411      */
412     prgnSrcClip = RegionCreate(NULL, 0);
413     RegionCopy(prgnSrcClip, prgnSrc);
414     RegionTranslate(prgnSrcClip, srcx, 0);
415     (*pGCT->funcs->ChangeClip) (pGCT, CT_REGION, prgnSrcClip, 0);
416     ValidateGC((DrawablePtr) pPixmap, pGCT);
417 
418     /* Since we know pDraw is always a pixmap, we never need to think
419      * about translation here */
420     for (i = 0; i < h; i++) {
421         ppt->x = 0;
422         ppt++->y = i;
423         *pwidth++ = w + srcx;
424     }
425 
426     (*pGCT->ops->SetSpans) ((DrawablePtr) pPixmap, pGCT, (char *) pbits,
427                             pptFirst, pwidthFirst, h, TRUE);
428     free(pwidthFirst);
429     free(pptFirst);
430 
431     /* Save current values from the client GC */
432     oldfill = pGC->fillStyle;
433     pStipple = pGC->stipple;
434     if (pStipple)
435         pStipple->refcnt++;
436     oldOrg = pGC->patOrg;
437 
438     /* Set a new stipple in the drawable */
439     gcv[0].val = FillStippled;
440     gcv[1].ptr = pPixmap;
441     gcv[2].val = dstx - srcx;
442     gcv[3].val = dsty;
443 
444     ChangeGC(NullClient, pGC,
445              GCFillStyle | GCStipple | GCTileStipXOrigin | GCTileStipYOrigin,
446              gcv);
447     ValidateGC(pDraw, pGC);
448 
449     /* Fill the drawable with the stipple.  This will draw the
450      * foreground color whereever 1 bits are set, leaving everything
451      * with 0 bits untouched.  Note that the part outside the clip
452      * region is all 0s.  */
453     rect.x = dstx;
454     rect.y = dsty;
455     rect.width = w;
456     rect.height = h;
457     (*pGC->ops->PolyFillRect) (pDraw, pGC, 1, &rect);
458 
459     /* Invert the tiling pixmap. This sets 0s for 1s and 1s for 0s, only
460      * within the clipping region, the part outside is still all 0s */
461     gcv[0].val = GXinvert;
462     ChangeGC(NullClient, pGCT, GCFunction, gcv);
463     ValidateGC((DrawablePtr) pPixmap, pGCT);
464     (*pGCT->ops->CopyArea) ((DrawablePtr) pPixmap, (DrawablePtr) pPixmap,
465                             pGCT, 0, 0, w + srcx, h, 0, 0);
466 
467     /* Swap foreground and background colors on the GC for the drawable.
468      * Now when we fill the drawable, we will fill in the "Background"
469      * values */
470     oldfg = pGC->fgPixel;
471     gcv[0].val = pGC->bgPixel;
472     gcv[1].val = oldfg;
473     gcv[2].ptr = pPixmap;
474     ChangeGC(NullClient, pGC, GCForeground | GCBackground | GCStipple, gcv);
475     ValidateGC(pDraw, pGC);
476     /* PolyFillRect might have bashed the rectangle */
477     rect.x = dstx;
478     rect.y = dsty;
479     rect.width = w;
480     rect.height = h;
481     (*pGC->ops->PolyFillRect) (pDraw, pGC, 1, &rect);
482 
483     /* Now put things back */
484     if (pStipple)
485         pStipple->refcnt--;
486     gcv[0].val = oldfg;
487     gcv[1].val = pGC->fgPixel;
488     gcv[2].val = oldfill;
489     gcv[3].ptr = pStipple;
490     gcv[4].val = oldOrg.x;
491     gcv[5].val = oldOrg.y;
492     ChangeGC(NullClient, pGC,
493              GCForeground | GCBackground | GCFillStyle | GCStipple |
494              GCTileStipXOrigin | GCTileStipYOrigin, gcv);
495 
496     ValidateGC(pDraw, pGC);
497     /* put what we hope is a smaller clip region back in the scratch gc */
498     (*pGCT->funcs->ChangeClip) (pGCT, CT_NONE, NULL, 0);
499     FreeScratchGC(pGCT);
500     (*pDraw->pScreen->DestroyPixmap) (pPixmap);
501 
502 }
503 
504 /* MICOPYPLANE -- public entry for the CopyPlane request.
505  * strategy:
506  * First build up a bitmap out of the bits requested
507  * build a source clip
508  * Use the bitmap we've built up as a Stipple for the destination
509  */
510 _X_COLD RegionPtr
miCopyPlane(DrawablePtr pSrcDrawable,DrawablePtr pDstDrawable,GCPtr pGC,int srcx,int srcy,int width,int height,int dstx,int dsty,unsigned long bitPlane)511 miCopyPlane(DrawablePtr pSrcDrawable,
512             DrawablePtr pDstDrawable,
513             GCPtr pGC,
514             int srcx,
515             int srcy,
516             int width, int height, int dstx, int dsty, unsigned long bitPlane)
517 {
518     MiBits *ptile;
519     BoxRec box;
520     RegionPtr prgnSrc, prgnExposed;
521 
522     /* incorporate the source clip */
523 
524     box.x1 = srcx + pSrcDrawable->x;
525     box.y1 = srcy + pSrcDrawable->y;
526     box.x2 = box.x1 + width;
527     box.y2 = box.y1 + height;
528     /* clip to visible drawable */
529     if (box.x1 < pSrcDrawable->x)
530         box.x1 = pSrcDrawable->x;
531     if (box.y1 < pSrcDrawable->y)
532         box.y1 = pSrcDrawable->y;
533     if (box.x2 > pSrcDrawable->x + (int) pSrcDrawable->width)
534         box.x2 = pSrcDrawable->x + (int) pSrcDrawable->width;
535     if (box.y2 > pSrcDrawable->y + (int) pSrcDrawable->height)
536         box.y2 = pSrcDrawable->y + (int) pSrcDrawable->height;
537     if (box.x1 > box.x2)
538         box.x2 = box.x1;
539     if (box.y1 > box.y2)
540         box.y2 = box.y1;
541     prgnSrc = RegionCreate(&box, 1);
542 
543     if (pSrcDrawable->type != DRAWABLE_PIXMAP) {
544         /* clip to visible drawable */
545 
546         if (pGC->subWindowMode == IncludeInferiors) {
547             RegionPtr clipList = NotClippedByChildren((WindowPtr) pSrcDrawable);
548 
549             RegionIntersect(prgnSrc, prgnSrc, clipList);
550             RegionDestroy(clipList);
551         }
552         else
553             RegionIntersect(prgnSrc, prgnSrc,
554                             &((WindowPtr) pSrcDrawable)->clipList);
555     }
556 
557     box = *RegionExtents(prgnSrc);
558     RegionTranslate(prgnSrc, -box.x1, -box.y1);
559 
560     if ((box.x2 > box.x1) && (box.y2 > box.y1)) {
561         /* minimize the size of the data extracted */
562         /* note that we convert the plane mask bitPlane into a plane number */
563         box.x1 -= pSrcDrawable->x;
564         box.x2 -= pSrcDrawable->x;
565         box.y1 -= pSrcDrawable->y;
566         box.y2 -= pSrcDrawable->y;
567         ptile = miGetPlane(pSrcDrawable, ffs(bitPlane) - 1,
568                            box.x1, box.y1,
569                            box.x2 - box.x1, box.y2 - box.y1, (MiBits *) NULL);
570         if (ptile) {
571             miOpqStipDrawable(pDstDrawable, pGC, prgnSrc, ptile, 0,
572                               box.x2 - box.x1, box.y2 - box.y1,
573                               dstx + box.x1 - srcx, dsty + box.y1 - srcy);
574             free(ptile);
575         }
576     }
577     prgnExposed = miHandleExposures(pSrcDrawable, pDstDrawable, pGC, srcx, srcy,
578                                     width, height, dstx, dsty);
579     RegionDestroy(prgnSrc);
580     return prgnExposed;
581 }
582 
583 /* MIGETIMAGE -- public entry for the GetImage Request
584  * We're getting the image into a memory buffer. While we have to use GetSpans
585  * to read a line from the device (since we don't know what that looks like),
586  * we can just write into the destination buffer
587  *
588  * two different strategies are used, depending on whether we're getting the
589  * image in Z format or XY format
590  * Z format:
591  * Line at a time, GetSpans a line into the destination buffer, then if the
592  * planemask is not all ones, we do a SetSpans into a temporary buffer (to get
593  * bits turned off) and then another GetSpans to get stuff back (because
594  * pixmaps are opaque, and we are passed in the memory to write into).  This is
595  * pretty ugly and slow but works.  Life is hard.
596  * XY format:
597  * get the single plane specified in planemask
598  */
599 _X_COLD void
miGetImage(DrawablePtr pDraw,int sx,int sy,int w,int h,unsigned int format,unsigned long planeMask,char * pDst)600 miGetImage(DrawablePtr pDraw, int sx, int sy, int w, int h,
601            unsigned int format, unsigned long planeMask, char *pDst)
602 {
603     unsigned char depth;
604     int i, linelength, width, srcx, srcy;
605     DDXPointRec pt = { 0, 0 };
606     PixmapPtr pPixmap = NULL;
607     GCPtr pGC = NULL;
608 
609     depth = pDraw->depth;
610     if (format == ZPixmap) {
611         if ((((1LL << depth) - 1) & planeMask) != (1LL << depth) - 1) {
612             ChangeGCVal gcv;
613             xPoint xpt;
614 
615             pGC = GetScratchGC(depth, pDraw->pScreen);
616             if (!pGC)
617                 return;
618             pPixmap = (*pDraw->pScreen->CreatePixmap)
619                 (pDraw->pScreen, w, 1, depth, CREATE_PIXMAP_USAGE_SCRATCH);
620             if (!pPixmap) {
621                 FreeScratchGC(pGC);
622                 return;
623             }
624             /*
625              * Clear the pixmap before doing anything else
626              */
627             ValidateGC((DrawablePtr) pPixmap, pGC);
628             xpt.x = xpt.y = 0;
629             width = w;
630             (*pGC->ops->FillSpans) ((DrawablePtr) pPixmap, pGC, 1, &xpt, &width,
631                                     TRUE);
632 
633             /* alu is already GXCopy */
634             gcv.val = (XID) planeMask;
635             ChangeGC(NullClient, pGC, GCPlaneMask, &gcv);
636             ValidateGC((DrawablePtr) pPixmap, pGC);
637         }
638 
639         linelength = PixmapBytePad(w, depth);
640         srcx = sx + pDraw->x;
641         srcy = sy + pDraw->y;
642         for (i = 0; i < h; i++) {
643             pt.x = srcx;
644             pt.y = srcy + i;
645             width = w;
646             (*pDraw->pScreen->GetSpans) (pDraw, w, &pt, &width, 1, pDst);
647             if (pPixmap) {
648                 pt.x = 0;
649                 pt.y = 0;
650                 width = w;
651                 (*pGC->ops->SetSpans) ((DrawablePtr) pPixmap, pGC, pDst,
652                                        &pt, &width, 1, TRUE);
653                 (*pDraw->pScreen->GetSpans) ((DrawablePtr) pPixmap, w, &pt,
654                                              &width, 1, pDst);
655             }
656             pDst += linelength;
657         }
658         if (pPixmap) {
659             (*pGC->pScreen->DestroyPixmap) (pPixmap);
660             FreeScratchGC(pGC);
661         }
662     }
663     else {
664         (void) miGetPlane(pDraw, ffs(planeMask) - 1, sx, sy, w, h,
665                           (MiBits *) pDst);
666     }
667 }
668 
669 /* MIPUTIMAGE -- public entry for the PutImage request
670  * Here we benefit from knowing the format of the bits pointed to by pImage,
671  * even if we don't know how pDraw represents them.
672  * Three different strategies are used depending on the format
673  * XYBitmap Format:
674  * 	we just use the Opaque Stipple helper function to cover the destination
675  * 	Note that this covers all the planes of the drawable with the
676  *	foreground color (masked with the GC planemask) where there are 1 bits
677  *	and the background color (masked with the GC planemask) where there are
678  *	0 bits
679  * XYPixmap format:
680  *	what we're called with is a series of XYBitmaps, but we only want
681  *	each XYPixmap to update 1 plane, instead of updating all of them.
682  * 	we set the foreground color to be all 1s and the background to all 0s
683  *	then for each plane, we set the plane mask to only effect that one
684  *	plane and recursive call ourself with the format set to XYBitmap
685  *	(This clever idea courtesy of RGD.)
686  * ZPixmap format:
687  *	This part is simple, just call SetSpans
688  */
689 _X_COLD void
miPutImage(DrawablePtr pDraw,GCPtr pGC,int depth,int x,int y,int w,int h,int leftPad,int format,char * pImage)690 miPutImage(DrawablePtr pDraw, GCPtr pGC, int depth,
691            int x, int y, int w, int h, int leftPad, int format, char *pImage)
692 {
693     DDXPointPtr pptFirst, ppt;
694     int *pwidthFirst, *pwidth;
695     RegionPtr prgnSrc;
696     BoxRec box;
697     unsigned long oldFg, oldBg;
698     ChangeGCVal gcv[3];
699     unsigned long oldPlanemask;
700     unsigned long i;
701     long bytesPer;
702 
703     if (!w || !h)
704         return;
705     switch (format) {
706     case XYBitmap:
707 
708         box.x1 = 0;
709         box.y1 = 0;
710         box.x2 = w;
711         box.y2 = h;
712         prgnSrc = RegionCreate(&box, 1);
713 
714         miOpqStipDrawable(pDraw, pGC, prgnSrc, (MiBits *) pImage,
715                           leftPad, w, h, x, y);
716         RegionDestroy(prgnSrc);
717         break;
718 
719     case XYPixmap:
720         depth = pGC->depth;
721         oldPlanemask = pGC->planemask;
722         oldFg = pGC->fgPixel;
723         oldBg = pGC->bgPixel;
724         gcv[0].val = (XID) ~0;
725         gcv[1].val = (XID) 0;
726         ChangeGC(NullClient, pGC, GCForeground | GCBackground, gcv);
727         bytesPer = (long) h *BitmapBytePad(w + leftPad);
728 
729         for (i = (unsigned long) 1 << (depth - 1); i != 0; i >>= 1, pImage += bytesPer) {
730             if (i & oldPlanemask) {
731                 gcv[0].val = (XID) i;
732                 ChangeGC(NullClient, pGC, GCPlaneMask, gcv);
733                 ValidateGC(pDraw, pGC);
734                 (*pGC->ops->PutImage) (pDraw, pGC, 1, x, y, w, h, leftPad,
735                                        XYBitmap, (char *) pImage);
736             }
737         }
738         gcv[0].val = (XID) oldPlanemask;
739         gcv[1].val = (XID) oldFg;
740         gcv[2].val = (XID) oldBg;
741         ChangeGC(NullClient, pGC, GCPlaneMask | GCForeground | GCBackground,
742                  gcv);
743         ValidateGC(pDraw, pGC);
744         break;
745 
746     case ZPixmap:
747         ppt = pptFirst = xallocarray(h, sizeof(DDXPointRec));
748         pwidth = pwidthFirst = xallocarray(h, sizeof(int));
749         if (!pptFirst || !pwidthFirst) {
750             free(pwidthFirst);
751             free(pptFirst);
752             return;
753         }
754         if (pGC->miTranslate) {
755             x += pDraw->x;
756             y += pDraw->y;
757         }
758 
759         for (i = 0; i < h; i++) {
760             ppt->x = x;
761             ppt->y = y + i;
762             ppt++;
763             *pwidth++ = w;
764         }
765 
766         (*pGC->ops->SetSpans) (pDraw, pGC, (char *) pImage, pptFirst,
767                                pwidthFirst, h, TRUE);
768         free(pwidthFirst);
769         free(pptFirst);
770         break;
771     }
772 }
773