2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1991-1997 Søren Schmidt
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer,
12 * in this position and unchanged.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/types.h>
39 #define min(x, y) (((x) < (y)) ? (x) : (y))
41 static byte mask[8] = {0xff, 0x7f, 0x3f, 0x1f, 0x0f, 0x07, 0x03, 0x01};
42 static int color2bit[16] = {0x00000000, 0x00000001, 0x00000100, 0x00000101,
43 0x00010000, 0x00010001, 0x00010100, 0x00010101,
44 0x01000000, 0x01000001, 0x01000100, 0x01000101,
45 0x01010000, 0x01010001, 0x01010100, 0x01010101};
48 WriteVerticalLine(VGLBitmap *dst, int x, int y, int width, byte *line)
50 int i, pos, last, planepos, start_offset, end_offset, offset;
52 unsigned int word = 0;
59 start_offset = (x & 0x07);
60 end_offset = (x + width) & 0x07;
61 i = (width + start_offset) / 8;
65 VGLPlane[1] = VGLPlane[0] + i;
66 VGLPlane[2] = VGLPlane[1] + i;
67 VGLPlane[3] = VGLPlane[2] + i;
70 last = 8 - start_offset;
73 while (pos < last && pos < width)
74 word = (word<<1) | color2bit[line[pos++]&0x0f];
75 VGLPlane[0][planepos] = word;
76 VGLPlane[1][planepos] = word>>8;
77 VGLPlane[2][planepos] = word>>16;
78 VGLPlane[3][planepos] = word>>24;
84 word <<= (8 - end_offset);
85 VGLPlane[0][planepos] = word;
86 VGLPlane[1][planepos] = word>>8;
87 VGLPlane[2][planepos] = word>>16;
88 VGLPlane[3][planepos] = word>>24;
90 if (start_offset || end_offset)
93 outb(0x3ce, 0x01); outb(0x3cf, 0x00); /* set/reset enable */
94 outb(0x3ce, 0x08); outb(0x3cf, 0xff); /* bit mask */
100 pos = VGLAdpInfo.va_line_width*y + x/8;
101 if (dst->Type == VIDBUF4) {
103 VGLPlane[i][planepos] |= dst->Bitmap[pos+planepos] & mask[end_offset];
105 VGLPlane[i][0] |= dst->Bitmap[pos] & ~mask[start_offset];
106 bcopy(&VGLPlane[i][0], dst->Bitmap + pos, width);
107 } else { /* VIDBUF4S */
109 offset = VGLSetSegment(pos + planepos);
110 VGLPlane[i][planepos] |= dst->Bitmap[offset] & mask[end_offset];
112 offset = VGLSetSegment(pos);
114 VGLPlane[i][0] |= dst->Bitmap[offset] & ~mask[start_offset];
115 for (last = width; ; ) {
116 len = min(VGLAdpInfo.va_window_size - offset, last);
117 bcopy(&VGLPlane[i][width - last], dst->Bitmap + offset, len);
122 offset = VGLSetSegment(pos);
128 address = dst->Bitmap + VGLAdpInfo.va_line_width * y + x/4;
129 for (i=0; i<4; i++) {
131 outb(0x3c5, 0x01 << ((x + i)%4));
132 for (planepos=0, pos=i; pos<width; planepos++, pos+=4)
133 address[planepos] = line[pos];
142 width = width * dst->PixelBytes;
143 pos = (dst->VXsize * y + x) * dst->PixelBytes;
145 offset = VGLSetSegment(pos);
146 i = min(VGLAdpInfo.va_window_size - offset, width);
147 bcopy(line, dst->Bitmap + offset, i);
158 address = dst->Bitmap + (dst->VXsize * y + x) * dst->PixelBytes;
159 bcopy(line, address, width * dst->PixelBytes);
167 ReadVerticalLine(VGLBitmap *src, int x, int y, int width, byte *line)
169 int i, bit, pos, count, planepos, start_offset, end_offset, offset;
176 start_offset = (x & 0x07);
177 end_offset = (x + width) & 0x07;
178 count = (width + start_offset) / 8;
181 VGLPlane[0] = VGLBuf;
182 VGLPlane[1] = VGLPlane[0] + count;
183 VGLPlane[2] = VGLPlane[1] + count;
184 VGLPlane[3] = VGLPlane[2] + count;
185 for (i=0; i<4; i++) {
188 pos = VGLAdpInfo.va_line_width*y + x/8;
189 for (width2 = count; width2 > 0; ) {
190 offset = VGLSetSegment(pos);
191 len = min(VGLAdpInfo.va_window_size - offset, width2);
192 bcopy(src->Bitmap + offset, &VGLPlane[i][count - width2], len);
199 address = src->Bitmap + VGLAdpInfo.va_line_width * y + x/8;
200 start_offset = (x & 0x07);
201 end_offset = (x + width) & 0x07;
202 count = (width + start_offset) / 8;
205 VGLPlane[0] = VGLBuf;
206 VGLPlane[1] = VGLPlane[0] + count;
207 VGLPlane[2] = VGLPlane[1] + count;
208 VGLPlane[3] = VGLPlane[2] + count;
209 for (i=0; i<4; i++) {
212 bcopy(address, &VGLPlane[i][0], count);
217 bit = 7 - start_offset;
218 while (pos < width) {
219 for (; bit >= 0 && pos < width; bit--, pos++) {
220 line[pos] = (VGLPlane[0][planepos] & (1<<bit) ? 1 : 0) |
221 ((VGLPlane[1][planepos] & (1<<bit) ? 1 : 0) << 1) |
222 ((VGLPlane[2][planepos] & (1<<bit) ? 1 : 0) << 2) |
223 ((VGLPlane[3][planepos] & (1<<bit) ? 1 : 0) << 3);
230 address = src->Bitmap + VGLAdpInfo.va_line_width * y + x/4;
231 for (i=0; i<4; i++) {
233 outb(0x3cf, (x + i)%4);
234 for (planepos=0, pos=i; pos<width; planepos++, pos+=4)
235 line[pos] = address[planepos];
244 width = width * src->PixelBytes;
245 pos = (src->VXsize * y + x) * src->PixelBytes;
247 offset = VGLSetSegment(pos);
248 i = min(VGLAdpInfo.va_window_size - offset, width);
249 bcopy(src->Bitmap + offset, line, i);
260 address = src->Bitmap + (src->VXsize * y + x) * src->PixelBytes;
261 bcopy(address, line, width * src->PixelBytes);
269 __VGLBitmapCopy(VGLBitmap *src, int srcx, int srcy,
270 VGLBitmap *dst, int dstx, int dsty, int width, int hight)
272 int srcline, dstline;
274 if (srcx>src->VXsize || srcy>src->VYsize
275 || dstx>dst->VXsize || dsty>dst->VYsize)
278 width=width+srcx; dstx-=srcx; srcx=0;
281 hight=hight+srcy; dsty-=srcy; srcy=0;
284 width=width+dstx; srcx-=dstx; dstx=0;
287 hight=hight+dsty; srcy-=dsty; dsty=0;
289 if (srcx+width > src->VXsize)
290 width=src->VXsize-srcx;
291 if (srcy+hight > src->VYsize)
292 hight=src->VYsize-srcy;
293 if (dstx+width > dst->VXsize)
294 width=dst->VXsize-dstx;
295 if (dsty+hight > dst->VYsize)
296 hight=dst->VYsize-dsty;
297 if (width < 0 || hight < 0)
299 if (src->Type == MEMBUF) {
300 for (srcline=srcy, dstline=dsty; srcline<srcy+hight; srcline++, dstline++) {
301 WriteVerticalLine(dst, dstx, dstline, width,
302 src->Bitmap+(srcline*src->VXsize+srcx)*dst->PixelBytes);
305 else if (dst->Type == MEMBUF) {
306 for (srcline=srcy, dstline=dsty; srcline<srcy+hight; srcline++, dstline++) {
307 ReadVerticalLine(src, srcx, srcline, width,
308 dst->Bitmap+(dstline*dst->VXsize+dstx)*src->PixelBytes);
312 byte buffer[2048]; /* XXX */
315 if (width * src->PixelBytes > sizeof(buffer)) {
316 p = malloc(width * src->PixelBytes);
322 for (srcline=srcy, dstline=dsty; srcline<srcy+hight; srcline++, dstline++) {
323 ReadVerticalLine(src, srcx, srcline, width, p);
324 WriteVerticalLine(dst, dstx, dstline, width, p);
326 if (width * src->PixelBytes > sizeof(buffer))
333 VGLBitmapCopy(VGLBitmap *src, int srcx, int srcy,
334 VGLBitmap *dst, int dstx, int dsty, int width, int hight)
338 if (src->Type != MEMBUF)
339 VGLMouseFreeze(srcx, srcy, width, hight, 0);
340 if (dst->Type != MEMBUF)
341 VGLMouseFreeze(dstx, dsty, width, hight, 0);
342 error = __VGLBitmapCopy(src, srcx, srcy, dst, dstx, dsty, width, hight);
343 if (src->Type != MEMBUF || dst->Type != MEMBUF)
349 *VGLBitmapCreate(int type, int xsize, int ysize, byte *bits)
355 if (xsize < 0 || ysize < 0)
357 object = (VGLBitmap *)malloc(sizeof(*object));
361 object->Xsize = xsize;
362 object->Ysize = ysize;
363 object->VXsize = xsize;
364 object->VYsize = ysize;
367 object->Bitmap = bits;
368 object->PixelBytes = VGLDisplay->PixelBytes;
373 VGLBitmapDestroy(VGLBitmap *object)
376 free(object->Bitmap);
381 VGLBitmapAllocateBits(VGLBitmap *object)
383 object->Bitmap = malloc(object->VXsize*object->VYsize*object->PixelBytes);
384 if (object->Bitmap == NULL)