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>
35 #include <sys/endian.h>
39 static byte VGLBorderColor;
40 static byte VGLSavePaletteRed[256];
41 static byte VGLSavePaletteGreen[256];
42 static byte VGLSavePaletteBlue[256];
44 #define ABS(a) (((a)<0) ? -(a) : (a))
45 #define SGN(a) (((a)<0) ? -1 : 1)
46 #define min(x, y) (((x) < (y)) ? (x) : (y))
47 #define max(x, y) (((x) > (y)) ? (x) : (y))
50 VGLSetXY(VGLBitmap *object, int x, int y, u_long color)
52 int offset, soffset, undermouse;
55 if (x>=0 && x<object->VXsize && y>=0 && y<object->VYsize) {
56 if (object == VGLDisplay) {
57 undermouse = VGLMouseFreezeXY(x, y);
58 VGLSetXY(&VGLVDisplay, x, y, color);
59 } else if (object->Type != MEMBUF)
64 offset = (y * object->VXsize + x) * object->PixelBytes;
65 switch (object->Type) {
69 offset = VGLSetSegment(offset);
76 color = htole32(color);
77 switch (object->PixelBytes) {
79 memcpy(&object->Bitmap[offset], &color, 1);
82 memcpy(&object->Bitmap[offset], &color, 2);
85 memcpy(&object->Bitmap[offset], &color, 3);
88 memcpy(&object->Bitmap[offset], &color, 4);
93 soffset = VGLSetSegment(offset);
94 color = htole32(color);
95 switch (VGLAdpInfo.va_window_size - soffset) {
97 memcpy(&object->Bitmap[soffset], &color, 1);
98 soffset = VGLSetSegment(offset + 1);
99 memcpy(&object->Bitmap[soffset], (byte *)&color + 1, 2);
102 memcpy(&object->Bitmap[soffset], &color, 2);
103 soffset = VGLSetSegment(offset + 2);
104 memcpy(&object->Bitmap[soffset], (byte *)&color + 2, 1);
107 memcpy(&object->Bitmap[soffset], &color, 3);
113 outb(0x3c5, 0x01 << (x&0x3));
114 object->Bitmap[(unsigned)(VGLAdpInfo.va_line_width*y)+(x/4)] = ((byte)color);
117 offset = VGLSetSegment(y*VGLAdpInfo.va_line_width + x/8);
120 offset = y*VGLAdpInfo.va_line_width + x/8;
122 outb(0x3c4, 0x02); outb(0x3c5, 0x0f);
123 outb(0x3ce, 0x00); outb(0x3cf, (byte)color & 0x0f); /* set/reset */
124 outb(0x3ce, 0x01); outb(0x3cf, 0x0f); /* set/reset enable */
125 outb(0x3ce, 0x08); outb(0x3cf, 0x80 >> (x%8)); /* bit mask */
126 object->Bitmap[offset] |= (byte)color;
129 if (object == VGLDisplay)
135 VGLGetXY(VGLBitmap *object, int x, int y)
141 if (x<0 || x>=object->VXsize || y<0 || y>=object->VYsize)
143 if (object == VGLDisplay)
144 object = &VGLVDisplay;
145 else if (object->Type != MEMBUF)
146 return 0; /* invalid */
147 offset = (y * object->VXsize + x) * object->PixelBytes;
148 switch (object->PixelBytes) {
150 memcpy(&color, &object->Bitmap[offset], 1);
151 return le32toh(color) & 0xff;
153 memcpy(&color, &object->Bitmap[offset], 2);
154 return le32toh(color) & 0xffff;
156 memcpy(&color, &object->Bitmap[offset], 3);
157 return le32toh(color) & 0xffffff;
159 memcpy(&color, &object->Bitmap[offset], 4);
160 return le32toh(color);
162 return 0; /* invalid */
166 * Symmetric Double Step Line Algorithm by Brian Wyvill from
167 * "Graphics Gems", Academic Press, 1990.
170 #define SL_SWAP(a,b) {a^=b; b^=a; a^=b;}
171 #define SL_ABSOLUTE(i,j,k) ( (i-j)*(k = ( (i-j)<0 ? -1 : 1)))
174 plot(VGLBitmap * object, int x, int y, int flag, u_long color)
176 /* non-zero flag indicates the pixels need swapping back. */
178 VGLSetXY(object, y, x, color);
180 VGLSetXY(object, x, y, color);
185 VGLLine(VGLBitmap *object, int x1, int y1, int x2, int y2, u_long color)
187 int dx, dy, incr1, incr2, D, x, y, xend, c, pixels_left;
188 int sign_x, sign_y, step, reverse, i;
190 dx = SL_ABSOLUTE(x2, x1, sign_x);
191 dy = SL_ABSOLUTE(y2, y1, sign_y);
192 /* decide increment sign by the slope sign */
193 if (sign_x == sign_y)
198 if (dy > dx) { /* chooses axis of greatest movement (make dx) */
205 /* note error check for dx==0 should be included here */
206 if (x1 > x2) { /* start from the smaller coordinate */
219 /* Note dx=n implies 0 - n or (dx+1) pixels to be set */
220 /* Go round loop dx/4 times then plot last 0,1,2 or 3 pixels */
221 /* In fact (dx-1)/4 as 2 pixels are already plotted */
223 pixels_left = (dx - 1) % 4; /* number of pixels left over at the
225 plot(object, x, y, reverse, color);
227 return; /* plot only one pixel for zero length
229 plot(object, x1, y1, reverse, color); /* plot first two points */
230 incr2 = 4 * dy - 2 * dx;
231 if (incr2 < 0) { /* slope less than 1/2 */
236 for (i = 0; i < xend; i++) { /* plotting loop */
240 /* pattern 1 forwards */
241 plot(object, x, y, reverse, color);
242 plot(object, ++x, y, reverse, color);
243 /* pattern 1 backwards */
244 plot(object, x1, y1, reverse, color);
245 plot(object, --x1, y1, reverse, color);
249 /* pattern 2 forwards */
250 plot(object, x, y, reverse, color);
251 plot(object, ++x, y += step, reverse,
253 /* pattern 2 backwards */
254 plot(object, x1, y1, reverse, color);
255 plot(object, --x1, y1 -= step, reverse,
258 /* pattern 3 forwards */
259 plot(object, x, y += step, reverse, color);
260 plot(object, ++x, y, reverse, color);
261 /* pattern 3 backwards */
262 plot(object, x1, y1 -= step, reverse,
264 plot(object, --x1, y1, reverse, color);
270 /* plot last pattern */
273 plot(object, ++x, y, reverse, color); /* pattern 1 */
275 plot(object, ++x, y, reverse, color);
277 plot(object, --x1, y1, reverse, color);
280 plot(object, ++x, y, reverse, color); /* pattern 2 */
282 plot(object, ++x, y += step, reverse, color);
284 plot(object, --x1, y1, reverse, color);
287 plot(object, ++x, y += step, reverse, color);
289 plot(object, ++x, y, reverse, color);
291 plot(object, --x1, y1 -= step, reverse, color);
294 } /* end if pixels_left */
296 /* end slope < 1/2 */
297 else { /* slope greater than 1/2 */
301 for (i = 0; i < xend; i++) {
305 /* pattern 4 forwards */
306 plot(object, x, y += step, reverse, color);
307 plot(object, ++x, y += step, reverse, color);
308 /* pattern 4 backwards */
309 plot(object, x1, y1 -= step, reverse, color);
310 plot(object, --x1, y1 -= step, reverse, color);
314 /* pattern 2 forwards */
315 plot(object, x, y, reverse, color);
316 plot(object, ++x, y += step, reverse,
319 /* pattern 2 backwards */
320 plot(object, x1, y1, reverse, color);
321 plot(object, --x1, y1 -= step, reverse,
324 /* pattern 3 forwards */
325 plot(object, x, y += step, reverse, color);
326 plot(object, ++x, y, reverse, color);
327 /* pattern 3 backwards */
328 plot(object, x1, y1 -= step, reverse, color);
329 plot(object, --x1, y1, reverse, color);
334 /* plot last pattern */
337 plot(object, ++x, y += step, reverse, color); /* pattern 4 */
339 plot(object, ++x, y += step, reverse,
342 plot(object, --x1, y1 -= step, reverse,
346 plot(object, ++x, y, reverse, color); /* pattern 2 */
348 plot(object, ++x, y += step, reverse, color);
350 plot(object, --x1, y1, reverse, color);
353 plot(object, ++x, y += step, reverse, color);
355 plot(object, ++x, y, reverse, color);
356 if (pixels_left > 2) {
357 if (D > c) /* step 3 */
358 plot(object, --x1, y1 -= step, reverse, color);
360 plot(object, --x1, y1, reverse, color);
369 VGLBox(VGLBitmap *object, int x1, int y1, int x2, int y2, u_long color)
371 VGLLine(object, x1, y1, x2, y1, color);
372 VGLLine(object, x2, y1, x2, y2, color);
373 VGLLine(object, x2, y2, x1, y2, color);
374 VGLLine(object, x1, y2, x1, y1, color);
378 VGLFilledBox(VGLBitmap *object, int x1, int y1, int x2, int y2, u_long color)
382 for (y=y1; y<=y2; y++) VGLLine(object, x1, y, x2, y, color);
386 set4pixels(VGLBitmap *object, int x, int y, int xc, int yc, u_long color)
389 VGLSetXY(object, xc+x, yc+y, color);
390 VGLSetXY(object, xc-x, yc+y, color);
392 VGLSetXY(object, xc+x, yc-y, color);
393 VGLSetXY(object, xc-x, yc-y, color);
397 VGLSetXY(object, xc, yc+y, color);
399 VGLSetXY(object, xc, yc-y, color);
404 VGLEllipse(VGLBitmap *object, int xc, int yc, int a, int b, u_long color)
406 int x = 0, y = b, asq = a*a, asq2 = a*a*2, bsq = b*b;
407 int bsq2 = b*b*2, d = bsq-asq*b+asq/4, dx = 0, dy = asq2*b;
410 set4pixels(object, x, y, xc, yc, color);
412 y--; dy-=asq2; d-=dy;
414 x++; dx+=bsq2; d+=bsq+dx;
416 d+=(3*(asq-bsq)/2-(dx+dy))/2;
418 set4pixels(object, x, y, xc, yc, color);
420 x++; dx+=bsq2; d+=dx;
422 y--; dy-=asq2; d+=asq-dy;
427 set2lines(VGLBitmap *object, int x, int y, int xc, int yc, u_long color)
430 VGLLine(object, xc+x, yc+y, xc-x, yc+y, color);
432 VGLLine(object, xc+x, yc-y, xc-x, yc-y, color);
435 VGLLine(object, xc, yc+y, xc, yc-y, color);
440 VGLFilledEllipse(VGLBitmap *object, int xc, int yc, int a, int b, u_long color)
442 int x = 0, y = b, asq = a*a, asq2 = a*a*2, bsq = b*b;
443 int bsq2 = b*b*2, d = bsq-asq*b+asq/4, dx = 0, dy = asq2*b;
446 set2lines(object, x, y, xc, yc, color);
448 y--; dy-=asq2; d-=dy;
450 x++; dx+=bsq2; d+=bsq+dx;
452 d+=(3*(asq-bsq)/2-(dx+dy))/2;
454 set2lines(object, x, y, xc, yc, color);
456 x++; dx+=bsq2; d+=dx;
458 y--; dy-=asq2; d+=asq-dy;
463 VGLClear(VGLBitmap *object, u_long color)
466 int i, len, mousemode, offset;
469 if (object == VGLDisplay) {
471 VGLClear(&VGLVDisplay, color);
472 } else if (object->Type != MEMBUF)
473 return; /* invalid */
474 switch (object->Type) {
485 src.Xsize = object->Xsize;
486 src.VXsize = object->VXsize;
491 src.Bitmap = alloca(object->VXsize * object->PixelBytes);
492 src.PixelBytes = object->PixelBytes;
493 color = htole32(color);
494 for (i = 0; i < object->VXsize; i++)
495 bcopy(&color, src.Bitmap + i * object->PixelBytes, object->PixelBytes);
496 for (i = 0; i < object->VYsize; i++)
497 __VGLBitmapCopy(&src, 0, 0, object, 0, i, object->VXsize, -1);
501 mousemode = __VGLMouseMode(VGL_MOUSEHIDE);
502 /* XXX works only for Xsize % 4 = 0 */
504 outb(0x3c4, 0x02); outb(0x3c5, 0x0f);
505 memset(object->Bitmap, (byte)color, VGLAdpInfo.va_line_width*object->VYsize);
506 __VGLMouseMode(mousemode);
511 mousemode = __VGLMouseMode(VGL_MOUSEHIDE);
512 /* XXX works only for Xsize % 8 = 0 */
513 outb(0x3c4, 0x02); outb(0x3c5, 0x0f);
514 outb(0x3ce, 0x05); outb(0x3cf, 0x02); /* mode 2 */
515 outb(0x3ce, 0x01); outb(0x3cf, 0x00); /* set/reset enable */
516 outb(0x3ce, 0x08); outb(0x3cf, 0xff); /* bit mask */
517 for (offset = 0; offset < VGLAdpInfo.va_line_width*object->VYsize; ) {
518 VGLSetSegment(offset);
519 len = min(object->VXsize*object->VYsize - offset,
520 VGLAdpInfo.va_window_size);
521 memset(object->Bitmap, (byte)color, len);
524 outb(0x3ce, 0x05); outb(0x3cf, 0x00);
525 __VGLMouseMode(mousemode);
528 if (object == VGLDisplay)
533 VGLrgbToNative(uint16_t r, uint16_t g, uint16_t b)
537 nr = VGLModeInfo.vi_pixel_fsizes[2];
538 ng = VGLModeInfo.vi_pixel_fsizes[1];
539 nb = VGLModeInfo.vi_pixel_fsizes[0];
540 return (r >> (16 - nr) << (ng + nb)) | (g >> (16 - ng) << nb) |
541 (b >> (16 - nb) << 0);
545 VGLrgb332ToNative(byte c)
549 /* 3:3:2 to 16:16:16 */
550 r = ((c & 0xe0) >> 5) * 0xffff / 7;
551 g = ((c & 0x1c) >> 2) * 0xffff / 7;
552 b = ((c & 0x03) >> 0) * 0xffff / 3;
554 return VGLrgbToNative(r, g, b);
562 if (VGLModeInfo.vi_mem_model == V_INFO_MM_DIRECT)
567 for (i=0; i<256; i++) {
568 outb(0x3C9, VGLSavePaletteRed[i]);
570 outb(0x3C9, VGLSavePaletteGreen[i]);
572 outb(0x3C9, VGLSavePaletteBlue[i]);
584 if (VGLModeInfo.vi_mem_model == V_INFO_MM_DIRECT)
589 for (i=0; i<256; i++) {
590 VGLSavePaletteRed[i] = inb(0x3C9);
592 VGLSavePaletteGreen[i] = inb(0x3C9);
594 VGLSavePaletteBlue[i] = inb(0x3C9);
602 VGLSetPalette(byte *red, byte *green, byte *blue)
606 if (VGLModeInfo.vi_mem_model == V_INFO_MM_DIRECT)
608 for (i=0; i<256; i++) {
609 VGLSavePaletteRed[i] = red[i];
610 VGLSavePaletteGreen[i] = green[i];
611 VGLSavePaletteBlue[i] = blue[i];
617 for (i=0; i<256; i++) {
618 outb(0x3C9, VGLSavePaletteRed[i]);
620 outb(0x3C9, VGLSavePaletteGreen[i]);
622 outb(0x3C9, VGLSavePaletteBlue[i]);
630 VGLSetPaletteIndex(byte color, byte red, byte green, byte blue)
632 if (VGLModeInfo.vi_mem_model == V_INFO_MM_DIRECT)
634 VGLSavePaletteRed[color] = red;
635 VGLSavePaletteGreen[color] = green;
636 VGLSavePaletteBlue[color] = blue;
641 outb(0x3C9, red); outb(0x3C9, green); outb(0x3C9, blue);
647 VGLRestoreBorder(void)
649 VGLSetBorder(VGLBorderColor);
653 VGLSetBorder(byte color)
655 if (VGLModeInfo.vi_mem_model == V_INFO_MM_DIRECT && ioctl(0, KDENABIO, 0))
659 outb(0x3C0,0x11); outb(0x3C0, color);
662 VGLBorderColor = color;
663 if (VGLModeInfo.vi_mem_model == V_INFO_MM_DIRECT)
664 ioctl(0, KDDISABIO, 0);
668 VGLRestoreBlank(void)
670 VGLBlankDisplay(VGLBlank);
674 VGLBlankDisplay(int blank)
678 if (VGLModeInfo.vi_mem_model == V_INFO_MM_DIRECT && ioctl(0, KDENABIO, 0))
681 outb(0x3C4, 0x01); val = inb(0x3C5); outb(0x3C4, 0x01);
682 outb(0x3C5, ((blank) ? (val |= 0x20) : (val &= 0xDF)));
684 if (VGLModeInfo.vi_mem_model == V_INFO_MM_DIRECT)
685 ioctl(0, KDDISABIO, 0);