1 /*
2 
3 Copyright 1993 by Davor Matic
4 
5 Permission to use, copy, modify, distribute, and sell this software
6 and its documentation for any purpose is hereby granted without fee,
7 provided that the above copyright notice appear in all copies and that
8 both that copyright notice and this permission notice appear in
9 supporting documentation.  Davor Matic makes no representations about
10 the suitability of this software for any purpose.  It is provided "as
11 is" without express or implied warranty.
12 
13 */
14 
15 #ifdef HAVE_XNEST_CONFIG_H
16 #include <xnest-config.h>
17 #endif
18 
19 #include <X11/X.h>
20 #include <X11/Xproto.h>
21 #include "regionstr.h"
22 #include "pixmapstr.h"
23 #include "scrnintstr.h"
24 #include "gc.h"
25 #include "servermd.h"
26 #include "privates.h"
27 #include "mi.h"
28 
29 #include "Xnest.h"
30 
31 #include "Display.h"
32 #include "Screen.h"
33 #include "XNPixmap.h"
34 
35 DevPrivateKeyRec xnestPixmapPrivateKeyRec;
36 
37 PixmapPtr
xnestCreatePixmap(ScreenPtr pScreen,int width,int height,int depth,unsigned usage_hint)38 xnestCreatePixmap(ScreenPtr pScreen, int width, int height, int depth,
39                   unsigned usage_hint)
40 {
41     PixmapPtr pPixmap;
42 
43     pPixmap = AllocatePixmap(pScreen, 0);
44     if (!pPixmap)
45         return NullPixmap;
46     pPixmap->drawable.type = DRAWABLE_PIXMAP;
47     pPixmap->drawable.class = 0;
48     pPixmap->drawable.depth = depth;
49     pPixmap->drawable.bitsPerPixel = depth;
50     pPixmap->drawable.id = 0;
51     pPixmap->drawable.x = 0;
52     pPixmap->drawable.y = 0;
53     pPixmap->drawable.width = width;
54     pPixmap->drawable.height = height;
55     pPixmap->drawable.pScreen = pScreen;
56     pPixmap->drawable.serialNumber = NEXT_SERIAL_NUMBER;
57     pPixmap->refcnt = 1;
58     pPixmap->devKind = PixmapBytePad(width, depth);
59     pPixmap->usage_hint = usage_hint;
60     if (width && height)
61         xnestPixmapPriv(pPixmap)->pixmap =
62             XCreatePixmap(xnestDisplay,
63                           xnestDefaultWindows[pScreen->myNum],
64                           width, height, depth);
65     else
66         xnestPixmapPriv(pPixmap)->pixmap = 0;
67 
68     return pPixmap;
69 }
70 
71 Bool
xnestDestroyPixmap(PixmapPtr pPixmap)72 xnestDestroyPixmap(PixmapPtr pPixmap)
73 {
74     if (--pPixmap->refcnt)
75         return TRUE;
76     XFreePixmap(xnestDisplay, xnestPixmap(pPixmap));
77     FreePixmap(pPixmap);
78     return TRUE;
79 }
80 
81 Bool
xnestModifyPixmapHeader(PixmapPtr pPixmap,int width,int height,int depth,int bitsPerPixel,int devKind,void * pPixData)82 xnestModifyPixmapHeader(PixmapPtr pPixmap, int width, int height, int depth,
83                         int bitsPerPixel, int devKind, void *pPixData)
84 {
85   if(!xnestPixmapPriv(pPixmap)->pixmap && width > 0 && height > 0) {
86     xnestPixmapPriv(pPixmap)->pixmap =
87         XCreatePixmap(xnestDisplay,
88                       xnestDefaultWindows[pPixmap->drawable.pScreen->myNum],
89                       width, height, depth);
90   }
91 
92   return miModifyPixmapHeader(pPixmap, width, height, depth,
93                               bitsPerPixel, devKind, pPixData);
94 }
95 
96 RegionPtr
xnestPixmapToRegion(PixmapPtr pPixmap)97 xnestPixmapToRegion(PixmapPtr pPixmap)
98 {
99     XImage *ximage;
100     register RegionPtr pReg, pTmpReg;
101     register int x, y;
102     unsigned long previousPixel, currentPixel;
103     BoxRec Box = { 0, 0, 0, 0 };
104     Bool overlap;
105 
106     ximage = XGetImage(xnestDisplay, xnestPixmap(pPixmap), 0, 0,
107                        pPixmap->drawable.width, pPixmap->drawable.height,
108                        1, XYPixmap);
109 
110     pReg = RegionCreate(NULL, 1);
111     pTmpReg = RegionCreate(NULL, 1);
112     if (!pReg || !pTmpReg) {
113         XDestroyImage(ximage);
114         return NullRegion;
115     }
116 
117     for (y = 0; y < pPixmap->drawable.height; y++) {
118         Box.y1 = y;
119         Box.y2 = y + 1;
120         previousPixel = 0L;
121         for (x = 0; x < pPixmap->drawable.width; x++) {
122             currentPixel = XGetPixel(ximage, x, y);
123             if (previousPixel != currentPixel) {
124                 if (previousPixel == 0L) {
125                     /* left edge */
126                     Box.x1 = x;
127                 }
128                 else if (currentPixel == 0L) {
129                     /* right edge */
130                     Box.x2 = x;
131                     RegionReset(pTmpReg, &Box);
132                     RegionAppend(pReg, pTmpReg);
133                 }
134                 previousPixel = currentPixel;
135             }
136         }
137         if (previousPixel != 0L) {
138             /* right edge because of the end of pixmap */
139             Box.x2 = pPixmap->drawable.width;
140             RegionReset(pTmpReg, &Box);
141             RegionAppend(pReg, pTmpReg);
142         }
143     }
144 
145     RegionDestroy(pTmpReg);
146     XDestroyImage(ximage);
147 
148     RegionValidate(pReg, &overlap);
149 
150     return pReg;
151 }
152