Initial commit
[xorg_rtime.git] / xorg-server-1.4 / afb / afbpixmap.c
blob77ba53513fa167c439d4dfa4af1ec977e28d624a
1 /***********************************************************
3 Copyright (c) 1987 X Consortium
5 Permission is hereby granted, free of charge, to any person obtaining a copy
6 of this software and associated documentation files (the "Software"), to deal
7 in the Software without restriction, including without limitation the rights
8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 copies of the Software, and to permit persons to whom the Software is
10 furnished to do so, subject to the following conditions:
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 X CONSORTIUM 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 X Consortium 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 X Consortium.
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 ******************************************************************/
49 /* pixmap management
50 written by drewry, september 1986
52 on a monchrome device, a pixmap is a bitmap.
55 #ifdef HAVE_DIX_CONFIG_H
56 #include <dix-config.h>
57 #endif
59 #include <string.h>
61 #include <X11/Xmd.h>
62 #include "scrnintstr.h"
63 #include "pixmapstr.h"
64 #include "maskbits.h"
66 #include "afb.h"
67 #include "mi.h"
69 #include "servermd.h"
70 #include "mfb.h"
72 PixmapPtr
73 afbCreatePixmap(pScreen, width, height, depth)
74 ScreenPtr pScreen;
75 int width;
76 int height;
77 int depth;
79 PixmapPtr pPixmap;
80 size_t datasize;
81 size_t paddedWidth;
83 paddedWidth = BitmapBytePad(width);
85 if (paddedWidth > 32767 || height > 32767 || depth > 4)
86 return NullPixmap;
88 datasize = height * paddedWidth * depth;
89 pPixmap = AllocatePixmap(pScreen, datasize);
90 if (!pPixmap)
91 return(NullPixmap);
92 pPixmap->drawable.type = DRAWABLE_PIXMAP;
93 pPixmap->drawable.class = 0;
94 pPixmap->drawable.pScreen = pScreen;
95 pPixmap->drawable.depth = depth;
96 pPixmap->drawable.bitsPerPixel = depth;
97 pPixmap->drawable.id = 0;
98 pPixmap->drawable.serialNumber = NEXT_SERIAL_NUMBER;
99 pPixmap->drawable.x = 0;
100 pPixmap->drawable.y = 0;
101 pPixmap->drawable.width = width;
102 pPixmap->drawable.height = height;
103 pPixmap->devKind = paddedWidth;
104 pPixmap->refcnt = 1;
105 pPixmap->devPrivate.ptr = datasize ?
106 (pointer)((char *)pPixmap + pScreen->totalPixmapSize) : NULL;
107 return(pPixmap);
110 Bool
111 afbDestroyPixmap(pPixmap)
112 PixmapPtr pPixmap;
114 if(--pPixmap->refcnt)
115 return(TRUE);
116 xfree(pPixmap);
117 return(TRUE);
121 static PixmapPtr
122 afbCopyPixmap(PixmapPtr pSrc)
124 register PixmapPtr pDst;
125 int size;
126 ScreenPtr pScreen;
128 size = pSrc->drawable.height * pSrc->devKind * pSrc->drawable.depth;
129 pScreen = pSrc->drawable.pScreen;
130 pDst = (*pScreen->CreatePixmap)(pScreen, pSrc->drawable.width,
131 pSrc->drawable.height, pSrc->drawable.depth);
132 if (!pDst)
133 return(NullPixmap);
134 memmove((char *)pDst->devPrivate.ptr, (char *)pSrc->devPrivate.ptr, size);
135 return(pDst);
139 /* replicates a pattern to be a full 32 bits wide.
140 relies on the fact that each scnaline is longword padded.
141 doesn't do anything if pixmap is not a factor of 32 wide.
142 changes width field of pixmap if successful, so that the fast
143 XRotatePixmap code gets used if we rotate the pixmap later.
145 calculate number of times to repeat
146 for each scanline of pattern
147 zero out area to be filled with replicate
148 left shift and or in original as many times as needed
150 static void
151 afbPadPixmap(PixmapPtr pPixmap)
153 register int width = pPixmap->drawable.width;
154 register int h;
155 register PixelType mask;
156 register PixelType *p;
157 register PixelType bits; /* real pattern bits */
158 register int i;
159 int d;
160 int rep; /* repeat count for pattern */
162 if (width >= PPW)
163 return;
165 rep = PPW/width;
166 if (rep*width != PPW)
167 return;
169 mask = mfbGetendtab(width);
171 p = (PixelType *)(pPixmap->devPrivate.ptr);
173 for (d = 0; d < pPixmap->drawable.depth; d++) {
174 for (h = 0; h < pPixmap->drawable.height; h++) {
175 *p &= mask;
176 bits = *p;
177 for(i = 1; i < rep; i++) {
178 bits = SCRRIGHT(bits, width);
179 *p |= bits;
181 p++; /* @@@ NEXT PLANE @@@ */
184 pPixmap->drawable.width = PPW;
187 /* Rotates pixmap pPix by w pixels to the right on the screen. Assumes that
188 * words are PPW bits wide, and that the least significant bit appears on the
189 * left.
191 void
192 afbXRotatePixmap(pPix, rw)
193 PixmapPtr pPix;
194 register int rw;
196 register PixelType *pw, *pwFinal;
197 register PixelType t;
199 if (pPix == NullPixmap)
200 return;
202 pw = (PixelType *)pPix->devPrivate.ptr;
203 rw %= (int)pPix->drawable.width;
204 if (rw < 0)
205 rw += (int)pPix->drawable.width;
206 if(pPix->drawable.width == PPW) {
207 pwFinal = pw + pPix->drawable.height * pPix->drawable.depth;
208 while(pw < pwFinal) {
209 t = *pw;
210 *pw++ = SCRRIGHT(t, rw) |
211 (SCRLEFT(t, (PPW-rw)) & mfbGetendtab(rw));
213 } else {
214 /* We no longer do this. Validate doesn't try to rotate odd-size
215 * tiles or stipples. afbUnnatural<tile/stipple>FS works directly off
216 * the unrotate tile/stipple in the GC
218 ErrorF("X internal error: trying to rotate odd-sized pixmap.\n");
223 /* Rotates pixmap pPix by h lines. Assumes that h is always less than
224 pPix->height
225 works on any width.
227 void
228 afbYRotatePixmap(pPix, rh)
229 register PixmapPtr pPix;
230 int rh;
232 int nbyDown; /* bytes to move down to row 0; also offset of
233 row rh */
234 int nbyUp; /* bytes to move up to line rh; also
235 offset of first line moved down to 0 */
236 char *pbase;
237 char *ptmp;
238 int height;
239 int d;
241 if (pPix == NullPixmap)
242 return;
243 height = (int) pPix->drawable.height;
244 rh %= height;
245 if (rh < 0)
246 rh += height;
248 nbyDown = rh * pPix->devKind;
249 nbyUp = (pPix->devKind * height) - nbyDown;
251 if(!(ptmp = (char *)ALLOCATE_LOCAL(nbyUp)))
252 return;
254 for (d = 0; d < pPix->drawable.depth; d++) {
255 pbase = (char *)pPix->devPrivate.ptr + pPix->devKind * height * d; /* @@@ NEXT PLANE @@@ */
257 memmove(ptmp, pbase, nbyUp); /* save the low rows */
258 memmove(pbase, pbase+nbyUp, nbyDown); /* slide the top rows down */
259 memmove(pbase+nbyDown, ptmp, nbyUp); /* move lower rows up to row rh */
261 DEALLOCATE_LOCAL(ptmp);
264 void
265 afbCopyRotatePixmap(psrcPix, ppdstPix, xrot, yrot)
266 register PixmapPtr psrcPix, *ppdstPix;
267 int xrot, yrot;
269 register PixmapPtr pdstPix;
271 if ((pdstPix = *ppdstPix) &&
272 (pdstPix->devKind == psrcPix->devKind) &&
273 (pdstPix->drawable.height == psrcPix->drawable.height) &&
274 (pdstPix->drawable.depth == psrcPix->drawable.depth)) {
275 memmove((char *)pdstPix->devPrivate.ptr,
276 (char *)psrcPix->devPrivate.ptr,
277 psrcPix->drawable.height * psrcPix->devKind *
278 psrcPix->drawable.depth);
279 pdstPix->drawable.width = psrcPix->drawable.width;
280 pdstPix->drawable.serialNumber = NEXT_SERIAL_NUMBER;
281 } else {
282 if (pdstPix)
283 /* FIX XBUG 6168 */
284 (*pdstPix->drawable.pScreen->DestroyPixmap)(pdstPix);
285 *ppdstPix = pdstPix = afbCopyPixmap(psrcPix);
286 if (!pdstPix)
287 return;
289 afbPadPixmap(pdstPix);
290 if (xrot)
291 afbXRotatePixmap(pdstPix, xrot);
292 if (yrot)
293 afbYRotatePixmap(pdstPix, yrot);