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$");
37 #include <sys/endian.h>
41 static byte VGLBorderColor;
42 static byte VGLSavePaletteRed[256];
43 static byte VGLSavePaletteGreen[256];
44 static byte VGLSavePaletteBlue[256];
46 #define ABS(a) (((a)<0) ? -(a) : (a))
47 #define SGN(a) (((a)<0) ? -1 : 1)
48 #define min(x, y) (((x) < (y)) ? (x) : (y))
49 #define max(x, y) (((x) > (y)) ? (x) : (y))
52 VGLSetXY(VGLBitmap *object, int x, int y, u_long color)
54 int offset, soffset, undermouse;
57 if (x>=0 && x<object->VXsize && y>=0 && y<object->VYsize) {
58 if (object == VGLDisplay) {
59 undermouse = VGLMouseFreezeXY(x, y);
60 VGLSetXY(&VGLVDisplay, x, y, color);
61 } else if (object->Type != MEMBUF)
66 offset = (y * object->VXsize + x) * object->PixelBytes;
67 switch (object->Type) {
71 offset = VGLSetSegment(offset);
78 color = htole32(color);
79 switch (object->PixelBytes) {
81 memcpy(&object->Bitmap[offset], &color, 1);
84 memcpy(&object->Bitmap[offset], &color, 2);
87 memcpy(&object->Bitmap[offset], &color, 3);
90 memcpy(&object->Bitmap[offset], &color, 4);
95 soffset = VGLSetSegment(offset);
96 color = htole32(color);
97 switch (VGLAdpInfo.va_window_size - soffset) {
99 memcpy(&object->Bitmap[soffset], &color, 1);
100 soffset = VGLSetSegment(offset + 1);
101 memcpy(&object->Bitmap[soffset], (byte *)&color + 1, 2);
104 memcpy(&object->Bitmap[soffset], &color, 2);
105 soffset = VGLSetSegment(offset + 2);
106 memcpy(&object->Bitmap[soffset], (byte *)&color + 2, 1);
109 memcpy(&object->Bitmap[soffset], &color, 3);
115 outb(0x3c5, 0x01 << (x&0x3));
116 object->Bitmap[(unsigned)(VGLAdpInfo.va_line_width*y)+(x/4)] = ((byte)color);
119 offset = VGLSetSegment(y*VGLAdpInfo.va_line_width + x/8);
122 offset = y*VGLAdpInfo.va_line_width + x/8;
124 outb(0x3c4, 0x02); outb(0x3c5, 0x0f);
125 outb(0x3ce, 0x00); outb(0x3cf, (byte)color & 0x0f); /* set/reset */
126 outb(0x3ce, 0x01); outb(0x3cf, 0x0f); /* set/reset enable */
127 outb(0x3ce, 0x08); outb(0x3cf, 0x80 >> (x%8)); /* bit mask */
128 object->Bitmap[offset] |= (byte)color;
131 if (object == VGLDisplay)
137 VGLGetXY(VGLBitmap *object, int x, int y)
143 if (x<0 || x>=object->VXsize || y<0 || y>=object->VYsize)
145 if (object == VGLDisplay)
146 object = &VGLVDisplay;
147 else if (object->Type != MEMBUF)
148 return 0; /* invalid */
149 offset = (y * object->VXsize + x) * object->PixelBytes;
150 switch (object->PixelBytes) {
152 memcpy(&color, &object->Bitmap[offset], 1);
153 return le32toh(color) & 0xff;
155 memcpy(&color, &object->Bitmap[offset], 2);
156 return le32toh(color) & 0xffff;
158 memcpy(&color, &object->Bitmap[offset], 3);
159 return le32toh(color) & 0xffffff;
161 memcpy(&color, &object->Bitmap[offset], 4);
162 return le32toh(color);
164 return 0; /* invalid */
168 * Symmetric Double Step Line Algorithm by Brian Wyvill from
169 * "Graphics Gems", Academic Press, 1990.
172 #define SL_SWAP(a,b) {a^=b; b^=a; a^=b;}
173 #define SL_ABSOLUTE(i,j,k) ( (i-j)*(k = ( (i-j)<0 ? -1 : 1)))
176 plot(VGLBitmap * object, int x, int y, int flag, u_long color)
178 /* non-zero flag indicates the pixels need swapping back. */
180 VGLSetXY(object, y, x, color);
182 VGLSetXY(object, x, y, color);
187 VGLLine(VGLBitmap *object, int x1, int y1, int x2, int y2, u_long color)
189 int dx, dy, incr1, incr2, D, x, y, xend, c, pixels_left;
190 int sign_x, sign_y, step, reverse, i;
192 dx = SL_ABSOLUTE(x2, x1, sign_x);
193 dy = SL_ABSOLUTE(y2, y1, sign_y);
194 /* decide increment sign by the slope sign */
195 if (sign_x == sign_y)
200 if (dy > dx) { /* chooses axis of greatest movement (make dx) */
207 /* note error check for dx==0 should be included here */
208 if (x1 > x2) { /* start from the smaller coordinate */
221 /* Note dx=n implies 0 - n or (dx+1) pixels to be set */
222 /* Go round loop dx/4 times then plot last 0,1,2 or 3 pixels */
223 /* In fact (dx-1)/4 as 2 pixels are already plotted */
225 pixels_left = (dx - 1) % 4; /* number of pixels left over at the
227 plot(object, x, y, reverse, color);
229 return; /* plot only one pixel for zero length
231 plot(object, x1, y1, reverse, color); /* plot first two points */
232 incr2 = 4 * dy - 2 * dx;
233 if (incr2 < 0) { /* slope less than 1/2 */
238 for (i = 0; i < xend; i++) { /* plotting loop */
242 /* pattern 1 forwards */
243 plot(object, x, y, reverse, color);
244 plot(object, ++x, y, reverse, color);
245 /* pattern 1 backwards */
246 plot(object, x1, y1, reverse, color);
247 plot(object, --x1, y1, reverse, color);
251 /* pattern 2 forwards */
252 plot(object, x, y, reverse, color);
253 plot(object, ++x, y += step, reverse,
255 /* pattern 2 backwards */
256 plot(object, x1, y1, reverse, color);
257 plot(object, --x1, y1 -= step, reverse,
260 /* pattern 3 forwards */
261 plot(object, x, y += step, reverse, color);
262 plot(object, ++x, y, reverse, color);
263 /* pattern 3 backwards */
264 plot(object, x1, y1 -= step, reverse,
266 plot(object, --x1, y1, reverse, color);
272 /* plot last pattern */
275 plot(object, ++x, y, reverse, color); /* pattern 1 */
277 plot(object, ++x, y, reverse, color);
279 plot(object, --x1, y1, reverse, color);
282 plot(object, ++x, y, reverse, color); /* pattern 2 */
284 plot(object, ++x, y += step, reverse, color);
286 plot(object, --x1, y1, reverse, color);
289 plot(object, ++x, y += step, reverse, color);
291 plot(object, ++x, y, reverse, color);
293 plot(object, --x1, y1 -= step, reverse, color);
296 } /* end if pixels_left */
298 /* end slope < 1/2 */
299 else { /* slope greater than 1/2 */
303 for (i = 0; i < xend; i++) {
307 /* pattern 4 forwards */
308 plot(object, x, y += step, reverse, color);
309 plot(object, ++x, y += step, reverse, color);
310 /* pattern 4 backwards */
311 plot(object, x1, y1 -= step, reverse, color);
312 plot(object, --x1, y1 -= step, reverse, color);
316 /* pattern 2 forwards */
317 plot(object, x, y, reverse, color);
318 plot(object, ++x, y += step, reverse,
321 /* pattern 2 backwards */
322 plot(object, x1, y1, reverse, color);
323 plot(object, --x1, y1 -= step, reverse,
326 /* pattern 3 forwards */
327 plot(object, x, y += step, reverse, color);
328 plot(object, ++x, y, reverse, color);
329 /* pattern 3 backwards */
330 plot(object, x1, y1 -= step, reverse, color);
331 plot(object, --x1, y1, reverse, color);
336 /* plot last pattern */
339 plot(object, ++x, y += step, reverse, color); /* pattern 4 */
341 plot(object, ++x, y += step, reverse,
344 plot(object, --x1, y1 -= step, reverse,
348 plot(object, ++x, y, reverse, color); /* pattern 2 */
350 plot(object, ++x, y += step, reverse, color);
352 plot(object, --x1, y1, reverse, color);
355 plot(object, ++x, y += step, reverse, color);
357 plot(object, ++x, y, reverse, color);
358 if (pixels_left > 2) {
359 if (D > c) /* step 3 */
360 plot(object, --x1, y1 -= step, reverse, color);
362 plot(object, --x1, y1, reverse, color);
371 VGLBox(VGLBitmap *object, int x1, int y1, int x2, int y2, u_long color)
373 VGLLine(object, x1, y1, x2, y1, color);
374 VGLLine(object, x2, y1, x2, y2, color);
375 VGLLine(object, x2, y2, x1, y2, color);
376 VGLLine(object, x1, y2, x1, y1, color);
380 VGLFilledBox(VGLBitmap *object, int x1, int y1, int x2, int y2, u_long color)
384 for (y=y1; y<=y2; y++) VGLLine(object, x1, y, x2, y, color);
388 set4pixels(VGLBitmap *object, int x, int y, int xc, int yc, u_long color)
391 VGLSetXY(object, xc+x, yc+y, color);
392 VGLSetXY(object, xc-x, yc+y, color);
394 VGLSetXY(object, xc+x, yc-y, color);
395 VGLSetXY(object, xc-x, yc-y, color);
399 VGLSetXY(object, xc, yc+y, color);
401 VGLSetXY(object, xc, yc-y, color);
406 VGLEllipse(VGLBitmap *object, int xc, int yc, int a, int b, u_long color)
408 int x = 0, y = b, asq = a*a, asq2 = a*a*2, bsq = b*b;
409 int bsq2 = b*b*2, d = bsq-asq*b+asq/4, dx = 0, dy = asq2*b;
412 set4pixels(object, x, y, xc, yc, color);
414 y--; dy-=asq2; d-=dy;
416 x++; dx+=bsq2; d+=bsq+dx;
418 d+=(3*(asq-bsq)/2-(dx+dy))/2;
420 set4pixels(object, x, y, xc, yc, color);
422 x++; dx+=bsq2; d+=dx;
424 y--; dy-=asq2; d+=asq-dy;
429 set2lines(VGLBitmap *object, int x, int y, int xc, int yc, u_long color)
432 VGLLine(object, xc+x, yc+y, xc-x, yc+y, color);
434 VGLLine(object, xc+x, yc-y, xc-x, yc-y, color);
437 VGLLine(object, xc, yc+y, xc, yc-y, color);
442 VGLFilledEllipse(VGLBitmap *object, int xc, int yc, int a, int b, u_long color)
444 int x = 0, y = b, asq = a*a, asq2 = a*a*2, bsq = b*b;
445 int bsq2 = b*b*2, d = bsq-asq*b+asq/4, dx = 0, dy = asq2*b;
448 set2lines(object, x, y, xc, yc, color);
450 y--; dy-=asq2; d-=dy;
452 x++; dx+=bsq2; d+=bsq+dx;
454 d+=(3*(asq-bsq)/2-(dx+dy))/2;
456 set2lines(object, x, y, xc, yc, color);
458 x++; dx+=bsq2; d+=dx;
460 y--; dy-=asq2; d+=asq-dy;
465 VGLClear(VGLBitmap *object, u_long color)
468 int i, len, mousemode, offset;
471 if (object == VGLDisplay) {
473 VGLClear(&VGLVDisplay, color);
474 } else if (object->Type != MEMBUF)
475 return; /* invalid */
476 switch (object->Type) {
487 src.Xsize = object->Xsize;
488 src.VXsize = object->VXsize;
493 src.Bitmap = alloca(object->VXsize * object->PixelBytes);
494 src.PixelBytes = object->PixelBytes;
495 color = htole32(color);
496 for (i = 0; i < object->VXsize; i++)
497 bcopy(&color, src.Bitmap + i * object->PixelBytes, object->PixelBytes);
498 for (i = 0; i < object->VYsize; i++)
499 __VGLBitmapCopy(&src, 0, 0, object, 0, i, object->VXsize, -1);
503 mousemode = __VGLMouseMode(VGL_MOUSEHIDE);
504 /* XXX works only for Xsize % 4 = 0 */
506 outb(0x3c4, 0x02); outb(0x3c5, 0x0f);
507 memset(object->Bitmap, (byte)color, VGLAdpInfo.va_line_width*object->VYsize);
508 __VGLMouseMode(mousemode);
513 mousemode = __VGLMouseMode(VGL_MOUSEHIDE);
514 /* XXX works only for Xsize % 8 = 0 */
515 outb(0x3c4, 0x02); outb(0x3c5, 0x0f);
516 outb(0x3ce, 0x05); outb(0x3cf, 0x02); /* mode 2 */
517 outb(0x3ce, 0x01); outb(0x3cf, 0x00); /* set/reset enable */
518 outb(0x3ce, 0x08); outb(0x3cf, 0xff); /* bit mask */
519 for (offset = 0; offset < VGLAdpInfo.va_line_width*object->VYsize; ) {
520 VGLSetSegment(offset);
521 len = min(object->VXsize*object->VYsize - offset,
522 VGLAdpInfo.va_window_size);
523 memset(object->Bitmap, (byte)color, len);
526 outb(0x3ce, 0x05); outb(0x3cf, 0x00);
527 __VGLMouseMode(mousemode);
530 if (object == VGLDisplay)
535 VGLrgbToNative(uint16_t r, uint16_t g, uint16_t b)
539 nr = VGLModeInfo.vi_pixel_fsizes[2];
540 ng = VGLModeInfo.vi_pixel_fsizes[1];
541 nb = VGLModeInfo.vi_pixel_fsizes[0];
542 return (r >> (16 - nr) << (ng + nb)) | (g >> (16 - ng) << nb) |
543 (b >> (16 - nb) << 0);
547 VGLrgb332ToNative(byte c)
551 /* 3:3:2 to 16:16:16 */
552 r = ((c & 0xe0) >> 5) * 0xffff / 7;
553 g = ((c & 0x1c) >> 2) * 0xffff / 7;
554 b = ((c & 0x03) >> 0) * 0xffff / 3;
556 return VGLrgbToNative(r, g, b);
564 if (VGLModeInfo.vi_mem_model == V_INFO_MM_DIRECT)
569 for (i=0; i<256; i++) {
570 outb(0x3C9, VGLSavePaletteRed[i]);
572 outb(0x3C9, VGLSavePaletteGreen[i]);
574 outb(0x3C9, VGLSavePaletteBlue[i]);
586 if (VGLModeInfo.vi_mem_model == V_INFO_MM_DIRECT)
591 for (i=0; i<256; i++) {
592 VGLSavePaletteRed[i] = inb(0x3C9);
594 VGLSavePaletteGreen[i] = inb(0x3C9);
596 VGLSavePaletteBlue[i] = inb(0x3C9);
604 VGLSetPalette(byte *red, byte *green, byte *blue)
608 if (VGLModeInfo.vi_mem_model == V_INFO_MM_DIRECT)
610 for (i=0; i<256; i++) {
611 VGLSavePaletteRed[i] = red[i];
612 VGLSavePaletteGreen[i] = green[i];
613 VGLSavePaletteBlue[i] = blue[i];
619 for (i=0; i<256; i++) {
620 outb(0x3C9, VGLSavePaletteRed[i]);
622 outb(0x3C9, VGLSavePaletteGreen[i]);
624 outb(0x3C9, VGLSavePaletteBlue[i]);
632 VGLSetPaletteIndex(byte color, byte red, byte green, byte blue)
634 if (VGLModeInfo.vi_mem_model == V_INFO_MM_DIRECT)
636 VGLSavePaletteRed[color] = red;
637 VGLSavePaletteGreen[color] = green;
638 VGLSavePaletteBlue[color] = blue;
643 outb(0x3C9, red); outb(0x3C9, green); outb(0x3C9, blue);
649 VGLRestoreBorder(void)
651 VGLSetBorder(VGLBorderColor);
655 VGLSetBorder(byte color)
657 if (VGLModeInfo.vi_mem_model == V_INFO_MM_DIRECT && ioctl(0, KDENABIO, 0))
661 outb(0x3C0,0x11); outb(0x3C0, color);
664 VGLBorderColor = color;
665 if (VGLModeInfo.vi_mem_model == V_INFO_MM_DIRECT)
666 ioctl(0, KDDISABIO, 0);
670 VGLRestoreBlank(void)
672 VGLBlankDisplay(VGLBlank);
676 VGLBlankDisplay(int blank)
680 if (VGLModeInfo.vi_mem_model == V_INFO_MM_DIRECT && ioctl(0, KDENABIO, 0))
683 outb(0x3C4, 0x01); val = inb(0x3C5); outb(0x3C4, 0x01);
684 outb(0x3C5, ((blank) ? (val |= 0x20) : (val &= 0xDF)));
686 if (VGLModeInfo.vi_mem_model == V_INFO_MM_DIRECT)
687 ioctl(0, KDDISABIO, 0);