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)
142 offset = (y * object->VXsize + x) * object->PixelBytes;
143 switch (object->PixelBytes) {
145 memcpy(&color, &object->Bitmap[offset], 1);
146 return le32toh(color) & 0xff;
148 memcpy(&color, &object->Bitmap[offset], 2);
149 return le32toh(color) & 0xffff;
151 memcpy(&color, &object->Bitmap[offset], 3);
152 return le32toh(color) & 0xffffff;
154 memcpy(&color, &object->Bitmap[offset], 4);
155 return le32toh(color);
157 return 0; /* invalid */
161 VGLGetXY(VGLBitmap *object, int x, int y)
164 if (x<0 || x>=object->VXsize || y<0 || y>=object->VYsize)
166 if (object == VGLDisplay)
167 object = &VGLVDisplay;
168 else if (object->Type != MEMBUF)
169 return 0; /* invalid */
170 return __VGLGetXY(object, x, y);
174 * Symmetric Double Step Line Algorithm by Brian Wyvill from
175 * "Graphics Gems", Academic Press, 1990.
178 #define SL_SWAP(a,b) {a^=b; b^=a; a^=b;}
179 #define SL_ABSOLUTE(i,j,k) ( (i-j)*(k = ( (i-j)<0 ? -1 : 1)))
182 plot(VGLBitmap * object, int x, int y, int flag, u_long color)
184 /* non-zero flag indicates the pixels need swapping back. */
186 VGLSetXY(object, y, x, color);
188 VGLSetXY(object, x, y, color);
193 VGLLine(VGLBitmap *object, int x1, int y1, int x2, int y2, u_long color)
195 int dx, dy, incr1, incr2, D, x, y, xend, c, pixels_left;
196 int sign_x, sign_y, step, reverse, i;
198 dx = SL_ABSOLUTE(x2, x1, sign_x);
199 dy = SL_ABSOLUTE(y2, y1, sign_y);
200 /* decide increment sign by the slope sign */
201 if (sign_x == sign_y)
206 if (dy > dx) { /* chooses axis of greatest movement (make dx) */
213 /* note error check for dx==0 should be included here */
214 if (x1 > x2) { /* start from the smaller coordinate */
227 /* Note dx=n implies 0 - n or (dx+1) pixels to be set */
228 /* Go round loop dx/4 times then plot last 0,1,2 or 3 pixels */
229 /* In fact (dx-1)/4 as 2 pixels are already plotted */
231 pixels_left = (dx - 1) % 4; /* number of pixels left over at the
233 plot(object, x, y, reverse, color);
235 return; /* plot only one pixel for zero length
237 plot(object, x1, y1, reverse, color); /* plot first two points */
238 incr2 = 4 * dy - 2 * dx;
239 if (incr2 < 0) { /* slope less than 1/2 */
244 for (i = 0; i < xend; i++) { /* plotting loop */
248 /* pattern 1 forwards */
249 plot(object, x, y, reverse, color);
250 plot(object, ++x, y, reverse, color);
251 /* pattern 1 backwards */
252 plot(object, x1, y1, reverse, color);
253 plot(object, --x1, y1, reverse, color);
257 /* pattern 2 forwards */
258 plot(object, x, y, reverse, color);
259 plot(object, ++x, y += step, reverse,
261 /* pattern 2 backwards */
262 plot(object, x1, y1, reverse, color);
263 plot(object, --x1, y1 -= step, reverse,
266 /* pattern 3 forwards */
267 plot(object, x, y += step, reverse, color);
268 plot(object, ++x, y, reverse, color);
269 /* pattern 3 backwards */
270 plot(object, x1, y1 -= step, reverse,
272 plot(object, --x1, y1, reverse, color);
278 /* plot last pattern */
281 plot(object, ++x, y, reverse, color); /* pattern 1 */
283 plot(object, ++x, y, reverse, color);
285 plot(object, --x1, y1, reverse, color);
288 plot(object, ++x, y, reverse, color); /* pattern 2 */
290 plot(object, ++x, y += step, reverse, color);
292 plot(object, --x1, y1, reverse, color);
295 plot(object, ++x, y += step, reverse, color);
297 plot(object, ++x, y, reverse, color);
299 plot(object, --x1, y1 -= step, reverse, color);
302 } /* end if pixels_left */
304 /* end slope < 1/2 */
305 else { /* slope greater than 1/2 */
309 for (i = 0; i < xend; i++) {
313 /* pattern 4 forwards */
314 plot(object, x, y += step, reverse, color);
315 plot(object, ++x, y += step, reverse, color);
316 /* pattern 4 backwards */
317 plot(object, x1, y1 -= step, reverse, color);
318 plot(object, --x1, y1 -= step, reverse, color);
322 /* pattern 2 forwards */
323 plot(object, x, y, reverse, color);
324 plot(object, ++x, y += step, reverse,
327 /* pattern 2 backwards */
328 plot(object, x1, y1, reverse, color);
329 plot(object, --x1, y1 -= step, reverse,
332 /* pattern 3 forwards */
333 plot(object, x, y += step, reverse, color);
334 plot(object, ++x, y, reverse, color);
335 /* pattern 3 backwards */
336 plot(object, x1, y1 -= step, reverse, color);
337 plot(object, --x1, y1, reverse, color);
342 /* plot last pattern */
345 plot(object, ++x, y += step, reverse, color); /* pattern 4 */
347 plot(object, ++x, y += step, reverse,
350 plot(object, --x1, y1 -= step, reverse,
354 plot(object, ++x, y, reverse, color); /* pattern 2 */
356 plot(object, ++x, y += step, reverse, color);
358 plot(object, --x1, y1, reverse, color);
361 plot(object, ++x, y += step, reverse, color);
363 plot(object, ++x, y, reverse, color);
364 if (pixels_left > 2) {
365 if (D > c) /* step 3 */
366 plot(object, --x1, y1 -= step, reverse, color);
368 plot(object, --x1, y1, reverse, color);
377 VGLBox(VGLBitmap *object, int x1, int y1, int x2, int y2, u_long color)
379 VGLLine(object, x1, y1, x2, y1, color);
380 VGLLine(object, x2, y1, x2, y2, color);
381 VGLLine(object, x2, y2, x1, y2, color);
382 VGLLine(object, x1, y2, x1, y1, color);
386 VGLFilledBox(VGLBitmap *object, int x1, int y1, int x2, int y2, u_long color)
390 for (y=y1; y<=y2; y++) VGLLine(object, x1, y, x2, y, color);
394 set4pixels(VGLBitmap *object, int x, int y, int xc, int yc, u_long color)
397 VGLSetXY(object, xc+x, yc+y, color);
398 VGLSetXY(object, xc-x, yc+y, color);
400 VGLSetXY(object, xc+x, yc-y, color);
401 VGLSetXY(object, xc-x, yc-y, color);
405 VGLSetXY(object, xc, yc+y, color);
407 VGLSetXY(object, xc, yc-y, color);
412 VGLEllipse(VGLBitmap *object, int xc, int yc, int a, int b, u_long color)
414 int x = 0, y = b, asq = a*a, asq2 = a*a*2, bsq = b*b;
415 int bsq2 = b*b*2, d = bsq-asq*b+asq/4, dx = 0, dy = asq2*b;
418 set4pixels(object, x, y, xc, yc, color);
420 y--; dy-=asq2; d-=dy;
422 x++; dx+=bsq2; d+=bsq+dx;
424 d+=(3*(asq-bsq)/2-(dx+dy))/2;
426 set4pixels(object, x, y, xc, yc, color);
428 x++; dx+=bsq2; d+=dx;
430 y--; dy-=asq2; d+=asq-dy;
435 set2lines(VGLBitmap *object, int x, int y, int xc, int yc, u_long color)
438 VGLLine(object, xc+x, yc+y, xc-x, yc+y, color);
440 VGLLine(object, xc+x, yc-y, xc-x, yc-y, color);
443 VGLLine(object, xc, yc+y, xc, yc-y, color);
448 VGLFilledEllipse(VGLBitmap *object, int xc, int yc, int a, int b, u_long color)
450 int x = 0, y = b, asq = a*a, asq2 = a*a*2, bsq = b*b;
451 int bsq2 = b*b*2, d = bsq-asq*b+asq/4, dx = 0, dy = asq2*b;
454 set2lines(object, x, y, xc, yc, color);
456 y--; dy-=asq2; d-=dy;
458 x++; dx+=bsq2; d+=bsq+dx;
460 d+=(3*(asq-bsq)/2-(dx+dy))/2;
462 set2lines(object, x, y, xc, yc, color);
464 x++; dx+=bsq2; d+=dx;
466 y--; dy-=asq2; d+=asq-dy;
471 VGLClear(VGLBitmap *object, u_long color)
474 int i, len, mouseoverlap, offset;
477 if (object == VGLDisplay) {
479 mouseoverlap = VGLMouseOverlap(0, 0, object->VXsize, object->VYsize);
481 VGLMousePointerHide();
482 VGLClear(&VGLVDisplay, color);
483 } else if (object->Type != MEMBUF)
484 return; /* invalid */
485 switch (object->Type) {
496 src.Xsize = object->Xsize;
497 src.VXsize = object->VXsize;
502 src.Bitmap = alloca(object->VXsize * object->PixelBytes);
503 src.PixelBytes = object->PixelBytes;
504 color = htole32(color);
505 for (i = 0; i < object->VXsize; i++)
506 bcopy(&color, src.Bitmap + i * object->PixelBytes, object->PixelBytes);
507 for (i = 0; i < object->VYsize; i++)
508 __VGLBitmapCopy(&src, 0, 0, object, 0, i, object->VXsize, 1);
512 /* XXX works only for Xsize % 4 = 0 */
514 outb(0x3c4, 0x02); outb(0x3c5, 0x0f);
515 memset(object->Bitmap, (byte)color, VGLAdpInfo.va_line_width*object->VYsize);
520 /* XXX works only for Xsize % 8 = 0 */
521 outb(0x3c4, 0x02); outb(0x3c5, 0x0f);
522 outb(0x3ce, 0x05); outb(0x3cf, 0x02); /* mode 2 */
523 outb(0x3ce, 0x01); outb(0x3cf, 0x00); /* set/reset enable */
524 outb(0x3ce, 0x08); outb(0x3cf, 0xff); /* bit mask */
525 for (offset = 0; offset < VGLAdpInfo.va_line_width*object->VYsize; ) {
526 VGLSetSegment(offset);
527 len = min(object->VXsize*object->VYsize - offset,
528 VGLAdpInfo.va_window_size);
529 memset(object->Bitmap, (byte)color, len);
532 outb(0x3ce, 0x05); outb(0x3cf, 0x00);
535 if (object == VGLDisplay) {
537 VGLMousePointerShow();
543 VGLrgbToNative(uint16_t r, uint16_t g, uint16_t b)
547 nr = VGLModeInfo.vi_pixel_fsizes[2];
548 ng = VGLModeInfo.vi_pixel_fsizes[1];
549 nb = VGLModeInfo.vi_pixel_fsizes[0];
550 return (r >> (16 - nr) << (ng + nb)) | (g >> (16 - ng) << nb) |
551 (b >> (16 - nb) << 0);
555 VGLrgb332ToNative(byte c)
559 /* 3:3:2 to 16:16:16 */
560 r = ((c & 0xe0) >> 5) * 0xffff / 7;
561 g = ((c & 0x1c) >> 2) * 0xffff / 7;
562 b = ((c & 0x03) >> 0) * 0xffff / 3;
564 return VGLrgbToNative(r, g, b);
572 if (VGLModeInfo.vi_mem_model == V_INFO_MM_DIRECT)
577 for (i=0; i<256; i++) {
578 outb(0x3C9, VGLSavePaletteRed[i]);
580 outb(0x3C9, VGLSavePaletteGreen[i]);
582 outb(0x3C9, VGLSavePaletteBlue[i]);
594 if (VGLModeInfo.vi_mem_model == V_INFO_MM_DIRECT)
599 for (i=0; i<256; i++) {
600 VGLSavePaletteRed[i] = inb(0x3C9);
602 VGLSavePaletteGreen[i] = inb(0x3C9);
604 VGLSavePaletteBlue[i] = inb(0x3C9);
612 VGLSetPalette(byte *red, byte *green, byte *blue)
616 if (VGLModeInfo.vi_mem_model == V_INFO_MM_DIRECT)
618 for (i=0; i<256; i++) {
619 VGLSavePaletteRed[i] = red[i];
620 VGLSavePaletteGreen[i] = green[i];
621 VGLSavePaletteBlue[i] = blue[i];
627 for (i=0; i<256; i++) {
628 outb(0x3C9, VGLSavePaletteRed[i]);
630 outb(0x3C9, VGLSavePaletteGreen[i]);
632 outb(0x3C9, VGLSavePaletteBlue[i]);
640 VGLSetPaletteIndex(byte color, byte red, byte green, byte blue)
642 if (VGLModeInfo.vi_mem_model == V_INFO_MM_DIRECT)
644 VGLSavePaletteRed[color] = red;
645 VGLSavePaletteGreen[color] = green;
646 VGLSavePaletteBlue[color] = blue;
651 outb(0x3C9, red); outb(0x3C9, green); outb(0x3C9, blue);
657 VGLRestoreBorder(void)
659 VGLSetBorder(VGLBorderColor);
663 VGLSetBorder(byte color)
665 if (VGLModeInfo.vi_mem_model == V_INFO_MM_DIRECT && ioctl(0, KDENABIO, 0))
669 outb(0x3C0,0x11); outb(0x3C0, color);
672 VGLBorderColor = color;
673 if (VGLModeInfo.vi_mem_model == V_INFO_MM_DIRECT)
674 ioctl(0, KDDISABIO, 0);
678 VGLRestoreBlank(void)
680 VGLBlankDisplay(VGLBlank);
684 VGLBlankDisplay(int blank)
688 if (VGLModeInfo.vi_mem_model == V_INFO_MM_DIRECT && ioctl(0, KDENABIO, 0))
691 outb(0x3C4, 0x01); val = inb(0x3C5); outb(0x3C4, 0x01);
692 outb(0x3C5, ((blank) ? (val |= 0x20) : (val &= 0xDF)));
694 if (VGLModeInfo.vi_mem_model == V_INFO_MM_DIRECT)
695 ioctl(0, KDDISABIO, 0);