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$");
38 static byte VGLSavePaletteRed[256];
39 static byte VGLSavePaletteGreen[256];
40 static byte VGLSavePaletteBlue[256];
42 #define ABS(a) (((a)<0) ? -(a) : (a))
43 #define SGN(a) (((a)<0) ? -1 : 1)
44 #define min(x, y) (((x) < (y)) ? (x) : (y))
45 #define max(x, y) (((x) > (y)) ? (x) : (y))
48 color2mem(u_long color, byte *b, int len)
52 b[3] = (color >> 24) & 0xff;
55 b[2] = (color >> 16) & 0xff;
58 b[1] = (color >> 8) & 0xff;
70 mem2color(byte *b, int len)
76 color |= (b[3] & 0xff) << 24;
79 color |= (b[2] & 0xff) << 16;
82 color |= (b[1] & 0xff) << 8;
86 color |= (b[0] & 0xff);
94 VGLSetXY(VGLBitmap *object, int x, int y, u_long color)
100 if (x>=0 && x<object->VXsize && y>=0 && y<object->VYsize) {
101 if (object->Type == MEMBUF ||
102 !VGLMouseFreeze(x, y, 1, 1, 0x80000000 | color)) {
103 switch (object->Type) {
105 switch (object->PixelBytes) {
115 object->Bitmap[y*object->VXsize+x]=((byte)color);
118 object->Bitmap[VGLSetSegment(y*object->VXsize+x)]=((byte)color);
126 color2mem(color, b, object->PixelBytes);
127 bcopy(b, &object->Bitmap[(y*object->VXsize+x) * object->PixelBytes],
133 color2mem(color, b, object->PixelBytes);
134 offset = VGLSetSegment((y*object->VXsize+x) * object->PixelBytes);
135 bcopy(b, &object->Bitmap[offset], object->PixelBytes);
139 outb(0x3c5, 0x01 << (x&0x3));
140 object->Bitmap[(unsigned)(VGLAdpInfo.va_line_width*y)+(x/4)] = ((byte)color);
143 offset = VGLSetSegment(y*VGLAdpInfo.va_line_width + x/8);
146 offset = y*VGLAdpInfo.va_line_width + x/8;
148 outb(0x3c4, 0x02); outb(0x3c5, 0x0f);
149 outb(0x3ce, 0x00); outb(0x3cf, (byte)color & 0x0f); /* set/reset */
150 outb(0x3ce, 0x01); outb(0x3cf, 0x0f); /* set/reset enable */
151 outb(0x3ce, 0x08); outb(0x3cf, 0x80 >> (x%8)); /* bit mask */
152 object->Bitmap[offset] |= (byte)color;
155 if (object->Type != MEMBUF)
161 __VGLGetXY(VGLBitmap *object, int x, int y)
169 switch (object->Type) {
171 switch (object->PixelBytes) {
181 return object->Bitmap[((y*object->VXsize)+x)];
183 return object->Bitmap[VGLSetSegment(y*object->VXsize+x)];
190 bcopy(&object->Bitmap[(y*object->VXsize+x) * object->PixelBytes],
191 b, object->PixelBytes);
192 return (mem2color(b, object->PixelBytes));
196 offset = VGLSetSegment((y*object->VXsize+x) * object->PixelBytes);
197 bcopy(&object->Bitmap[offset], b, object->PixelBytes);
199 return (mem2color(b, object->PixelBytes));
201 outb(0x3ce, 0x04); outb(0x3cf, x & 0x3);
202 return object->Bitmap[(unsigned)(VGLAdpInfo.va_line_width*y)+(x/4)];
204 offset = VGLSetSegment(y*VGLAdpInfo.va_line_width + x/8);
207 offset = y*VGLAdpInfo.va_line_width + x/8;
210 mask = 0x80 >> (x%8);
211 for (i = 0; i < VGLModeInfo.vi_planes; i++) {
212 outb(0x3ce, 0x04); outb(0x3cf, i);
213 color |= (((volatile VGLBitmap *)object)->Bitmap[offset] & mask) ?
218 return 0; /* XXX black? */
222 VGLGetXY(VGLBitmap *object, int x, int y)
227 if (x<0 || x>=object->VXsize || y<0 || y>=object->VYsize)
229 if (object->Type != MEMBUF) {
230 color = VGLMouseFreeze(x, y, 1, 1, 0x40000000);
231 if (color & 0x40000000) {
233 return color & 0xffffff;
236 color = __VGLGetXY(object, x, y);
237 if (object->Type != MEMBUF)
243 * Symmetric Double Step Line Algorithm by Brian Wyvill from
244 * "Graphics Gems", Academic Press, 1990.
247 #define SL_SWAP(a,b) {a^=b; b^=a; a^=b;}
248 #define SL_ABSOLUTE(i,j,k) ( (i-j)*(k = ( (i-j)<0 ? -1 : 1)))
251 plot(VGLBitmap * object, int x, int y, int flag, byte color)
253 /* non-zero flag indicates the pixels need swapping back. */
255 VGLSetXY(object, y, x, color);
257 VGLSetXY(object, x, y, color);
262 VGLLine(VGLBitmap *object, int x1, int y1, int x2, int y2, u_long color)
264 int dx, dy, incr1, incr2, D, x, y, xend, c, pixels_left;
265 int sign_x, sign_y, step, reverse, i;
267 dx = SL_ABSOLUTE(x2, x1, sign_x);
268 dy = SL_ABSOLUTE(y2, y1, sign_y);
269 /* decide increment sign by the slope sign */
270 if (sign_x == sign_y)
275 if (dy > dx) { /* chooses axis of greatest movement (make dx) */
282 /* note error check for dx==0 should be included here */
283 if (x1 > x2) { /* start from the smaller coordinate */
296 /* Note dx=n implies 0 - n or (dx+1) pixels to be set */
297 /* Go round loop dx/4 times then plot last 0,1,2 or 3 pixels */
298 /* In fact (dx-1)/4 as 2 pixels are already plotted */
300 pixels_left = (dx - 1) % 4; /* number of pixels left over at the
302 plot(object, x, y, reverse, color);
304 return; /* plot only one pixel for zero length
306 plot(object, x1, y1, reverse, color); /* plot first two points */
307 incr2 = 4 * dy - 2 * dx;
308 if (incr2 < 0) { /* slope less than 1/2 */
313 for (i = 0; i < xend; i++) { /* plotting loop */
317 /* pattern 1 forwards */
318 plot(object, x, y, reverse, color);
319 plot(object, ++x, y, reverse, color);
320 /* pattern 1 backwards */
321 plot(object, x1, y1, reverse, color);
322 plot(object, --x1, y1, reverse, color);
326 /* pattern 2 forwards */
327 plot(object, x, y, reverse, color);
328 plot(object, ++x, y += step, reverse,
330 /* pattern 2 backwards */
331 plot(object, x1, y1, reverse, color);
332 plot(object, --x1, y1 -= step, reverse,
335 /* pattern 3 forwards */
336 plot(object, x, y += step, reverse, color);
337 plot(object, ++x, y, reverse, color);
338 /* pattern 3 backwards */
339 plot(object, x1, y1 -= step, reverse,
341 plot(object, --x1, y1, reverse, color);
347 /* plot last pattern */
350 plot(object, ++x, y, reverse, color); /* pattern 1 */
352 plot(object, ++x, y, reverse, color);
354 plot(object, --x1, y1, reverse, color);
357 plot(object, ++x, y, reverse, color); /* pattern 2 */
359 plot(object, ++x, y += step, reverse, color);
361 plot(object, --x1, y1, reverse, color);
364 plot(object, ++x, y += step, reverse, color);
366 plot(object, ++x, y, reverse, color);
368 plot(object, --x1, y1 -= step, reverse, color);
371 } /* end if pixels_left */
373 /* end slope < 1/2 */
374 else { /* slope greater than 1/2 */
378 for (i = 0; i < xend; i++) {
382 /* pattern 4 forwards */
383 plot(object, x, y += step, reverse, color);
384 plot(object, ++x, y += step, reverse, color);
385 /* pattern 4 backwards */
386 plot(object, x1, y1 -= step, reverse, color);
387 plot(object, --x1, y1 -= step, reverse, color);
391 /* pattern 2 forwards */
392 plot(object, x, y, reverse, color);
393 plot(object, ++x, y += step, reverse,
396 /* pattern 2 backwards */
397 plot(object, x1, y1, reverse, color);
398 plot(object, --x1, y1 -= step, reverse,
401 /* pattern 3 forwards */
402 plot(object, x, y += step, reverse, color);
403 plot(object, ++x, y, reverse, color);
404 /* pattern 3 backwards */
405 plot(object, x1, y1 -= step, reverse, color);
406 plot(object, --x1, y1, reverse, color);
411 /* plot last pattern */
414 plot(object, ++x, y += step, reverse, color); /* pattern 4 */
416 plot(object, ++x, y += step, reverse,
419 plot(object, --x1, y1 -= step, reverse,
423 plot(object, ++x, y, reverse, color); /* pattern 2 */
425 plot(object, ++x, y += step, reverse, color);
427 plot(object, --x1, y1, reverse, color);
430 plot(object, ++x, y += step, reverse, color);
432 plot(object, ++x, y, reverse, color);
433 if (pixels_left > 2) {
434 if (D > c) /* step 3 */
435 plot(object, --x1, y1 -= step, reverse, color);
437 plot(object, --x1, y1, reverse, color);
446 VGLBox(VGLBitmap *object, int x1, int y1, int x2, int y2, u_long color)
448 VGLLine(object, x1, y1, x2, y1, color);
449 VGLLine(object, x2, y1, x2, y2, color);
450 VGLLine(object, x2, y2, x1, y2, color);
451 VGLLine(object, x1, y2, x1, y1, color);
455 VGLFilledBox(VGLBitmap *object, int x1, int y1, int x2, int y2, u_long color)
459 for (y=y1; y<=y2; y++) VGLLine(object, x1, y, x2, y, color);
463 set4pixels(VGLBitmap *object, int x, int y, int xc, int yc, u_long color)
466 VGLSetXY(object, xc+x, yc+y, color);
467 VGLSetXY(object, xc-x, yc+y, color);
469 VGLSetXY(object, xc+x, yc-y, color);
470 VGLSetXY(object, xc-x, yc-y, color);
474 VGLSetXY(object, xc, yc+y, color);
476 VGLSetXY(object, xc, yc-y, color);
481 VGLEllipse(VGLBitmap *object, int xc, int yc, int a, int b, u_long color)
483 int x = 0, y = b, asq = a*a, asq2 = a*a*2, bsq = b*b;
484 int bsq2 = b*b*2, d = bsq-asq*b+asq/4, dx = 0, dy = asq2*b;
487 set4pixels(object, x, y, xc, yc, color);
489 y--; dy-=asq2; d-=dy;
491 x++; dx+=bsq2; d+=bsq+dx;
493 d+=(3*(asq-bsq)/2-(dx+dy))/2;
495 set4pixels(object, x, y, xc, yc, color);
497 x++; dx+=bsq2; d+=dx;
499 y--; dy-=asq2; d+=asq-dy;
504 set2lines(VGLBitmap *object, int x, int y, int xc, int yc, u_long color)
507 VGLLine(object, xc+x, yc+y, xc-x, yc+y, color);
509 VGLLine(object, xc+x, yc-y, xc-x, yc-y, color);
512 VGLLine(object, xc, yc+y, xc, yc-y, color);
517 VGLFilledEllipse(VGLBitmap *object, int xc, int yc, int a, int b, u_long color)
519 int x = 0, y = b, asq = a*a, asq2 = a*a*2, bsq = b*b;
520 int bsq2 = b*b*2, d = bsq-asq*b+asq/4, dx = 0, dy = asq2*b;
523 set2lines(object, x, y, xc, yc, color);
525 y--; dy-=asq2; d-=dy;
527 x++; dx+=bsq2; d+=bsq+dx;
529 d+=(3*(asq-bsq)/2-(dx+dy))/2;
531 set2lines(object, x, y, xc, yc, color);
533 x++; dx+=bsq2; d+=dx;
535 y--; dy-=asq2; d+=asq-dy;
540 VGLClear(VGLBitmap *object, u_long color)
548 if (object->Type != MEMBUF)
549 VGLMouseFreeze(0, 0, object->Xsize, object->Ysize, color);
550 switch (object->Type) {
552 switch (object->PixelBytes) {
562 memset(object->Bitmap, (byte)color, object->VXsize*object->VYsize);
566 for (offset = 0; offset < object->VXsize*object->VYsize; ) {
567 VGLSetSegment(offset);
568 len = min(object->VXsize*object->VYsize - offset,
569 VGLAdpInfo.va_window_size);
570 memset(object->Bitmap, (byte)color, len);
580 color2mem(color, b, object->PixelBytes);
581 total = object->VXsize*object->VYsize*object->PixelBytes;
582 for (i = 0; i < total; i += object->PixelBytes)
583 bcopy(b, object->Bitmap + i, object->PixelBytes);
589 color2mem(color, b, object->PixelBytes);
590 total = object->VXsize*object->VYsize*object->PixelBytes;
591 for (offset = 0; offset < total; ) {
592 VGLSetSegment(offset);
593 len = min(total - offset, VGLAdpInfo.va_window_size);
594 for (i = 0; i < len; i += object->PixelBytes)
595 bcopy(b, object->Bitmap + (offset + i) % VGLAdpInfo.va_window_size,
602 /* XXX works only for Xsize % 4 = 0 */
604 outb(0x3c4, 0x02); outb(0x3c5, 0x0f);
605 memset(object->Bitmap, (byte)color, VGLAdpInfo.va_line_width*object->VYsize);
610 /* XXX works only for Xsize % 8 = 0 */
611 outb(0x3c4, 0x02); outb(0x3c5, 0x0f);
612 outb(0x3ce, 0x05); outb(0x3cf, 0x02); /* mode 2 */
613 outb(0x3ce, 0x01); outb(0x3cf, 0x00); /* set/reset enable */
614 outb(0x3ce, 0x08); outb(0x3cf, 0xff); /* bit mask */
615 for (offset = 0; offset < VGLAdpInfo.va_line_width*object->VYsize; ) {
616 VGLSetSegment(offset);
617 len = min(object->VXsize*object->VYsize - offset,
618 VGLAdpInfo.va_window_size);
619 memset(object->Bitmap, (byte)color, len);
622 outb(0x3ce, 0x05); outb(0x3cf, 0x00);
625 if (object->Type != MEMBUF)
637 for (i=0; i<256; i++) {
638 outb(0x3C9, VGLSavePaletteRed[i]);
640 outb(0x3C9, VGLSavePaletteGreen[i]);
642 outb(0x3C9, VGLSavePaletteBlue[i]);
657 for (i=0; i<256; i++) {
658 VGLSavePaletteRed[i] = inb(0x3C9);
660 VGLSavePaletteGreen[i] = inb(0x3C9);
662 VGLSavePaletteBlue[i] = inb(0x3C9);
670 VGLSetPalette(byte *red, byte *green, byte *blue)
674 for (i=0; i<256; i++) {
675 VGLSavePaletteRed[i] = red[i];
676 VGLSavePaletteGreen[i] = green[i];
677 VGLSavePaletteBlue[i] = blue[i];
683 for (i=0; i<256; i++) {
684 outb(0x3C9, VGLSavePaletteRed[i]);
686 outb(0x3C9, VGLSavePaletteGreen[i]);
688 outb(0x3C9, VGLSavePaletteBlue[i]);
696 VGLSetPaletteIndex(byte color, byte red, byte green, byte blue)
698 VGLSavePaletteRed[color] = red;
699 VGLSavePaletteGreen[color] = green;
700 VGLSavePaletteBlue[color] = blue;
705 outb(0x3C9, red); outb(0x3C9, green); outb(0x3C9, blue);
711 VGLSetBorder(byte color)
715 outb(0x3C0,0x11); outb(0x3C0, color);
721 VGLBlankDisplay(int blank)
726 outb(0x3C4, 0x01); val = inb(0x3C5); outb(0x3C4, 0x01);
727 outb(0x3C5, ((blank) ? (val |= 0x20) : (val &= 0xDF)));