1/*-
2 * Copyright (c) 1991-1997 Søren Schmidt
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer
10 *    in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 *    derived from this software without specific prior written permission
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD$");
31
32#include <signal.h>
33#include <sys/fbio.h>
34#include "vgl.h"
35
36static byte VGLSavePaletteRed[256];
37static byte VGLSavePaletteGreen[256];
38static byte VGLSavePaletteBlue[256];
39
40#define ABS(a)		(((a)<0) ? -(a) : (a))
41#define SGN(a)		(((a)<0) ? -1 : 1)
42#define min(x, y)	(((x) < (y)) ? (x) : (y))
43#define max(x, y)	(((x) > (y)) ? (x) : (y))
44
45static void
46color2mem(u_long color, byte *b, int len)
47{
48  switch (len) {
49  case 4:
50    b[3] = (color >> 24) & 0xff;
51    /* fallthrough */
52  case 3:
53    b[2] = (color >> 16) & 0xff;
54    /* fallthrough */
55  case 2:
56    b[1] = (color >> 8) & 0xff;
57    /* fallthrough */
58  case 1:
59  default:
60    b[0] = color & 0xff;
61    break;
62  }
63
64  return;
65}
66
67static u_long
68mem2color(byte *b, int len)
69{
70  u_long color = 0;
71
72  switch (len) {
73  case 4:
74    color |= (b[3] & 0xff) << 24;
75    /* fallthrough */
76  case 3:
77    color |= (b[2] & 0xff) << 16;
78    /* fallthrough */
79  case 2:
80    color |= (b[1] & 0xff) << 8;
81    /* fallthrough */
82  case 1:
83  default:
84    color |= (b[0] & 0xff);
85    break;
86  }
87
88  return color;
89}
90
91void
92VGLSetXY(VGLBitmap *object, int x, int y, u_long color)
93{
94  int offset;
95  byte b[4];
96
97  VGLCheckSwitch();
98  if (x>=0 && x<object->VXsize && y>=0 && y<object->VYsize) {
99    if (!VGLMouseFreeze(x, y, 1, 1, color)) {
100      switch (object->Type) {
101      case MEMBUF:
102      case VIDBUF8:
103        object->Bitmap[y*object->VXsize+x]=((byte)color);
104        break;
105      case VIDBUF8S:
106	object->Bitmap[VGLSetSegment(y*object->VXsize+x)]=((byte)color);
107	break;
108      case VIDBUF16:
109      case VIDBUF24:
110      case VIDBUF32:
111	color2mem(color, b, object->PixelBytes);
112        bcopy(b, &object->Bitmap[(y*object->VXsize+x) * object->PixelBytes],
113		object->PixelBytes);
114        break;
115      case VIDBUF16S:
116      case VIDBUF24S:
117      case VIDBUF32S:
118	color2mem(color, b, object->PixelBytes);
119	offset = VGLSetSegment((y*object->VXsize+x) * object->PixelBytes);
120	bcopy(b, &object->Bitmap[offset], object->PixelBytes);
121	break;
122      case VIDBUF8X:
123        outb(0x3c4, 0x02);
124        outb(0x3c5, 0x01 << (x&0x3));
125	object->Bitmap[(unsigned)(VGLAdpInfo.va_line_width*y)+(x/4)] = ((byte)color);
126	break;
127      case VIDBUF4S:
128	offset = VGLSetSegment(y*VGLAdpInfo.va_line_width + x/8);
129	goto set_planar;
130      case VIDBUF4:
131	offset = y*VGLAdpInfo.va_line_width + x/8;
132set_planar:
133        outb(0x3c4, 0x02); outb(0x3c5, 0x0f);
134        outb(0x3ce, 0x00); outb(0x3cf, (byte)color & 0x0f);	/* set/reset */
135        outb(0x3ce, 0x01); outb(0x3cf, 0x0f);		/* set/reset enable */
136        outb(0x3ce, 0x08); outb(0x3cf, 0x80 >> (x%8));	/* bit mask */
137	object->Bitmap[offset] |= (byte)color;
138      }
139    }
140    VGLMouseUnFreeze();
141  }
142}
143
144u_long
145VGLGetXY(VGLBitmap *object, int x, int y)
146{
147  int offset;
148  byte b[4];
149#if 0
150  int i;
151  u_long color;
152  byte mask;
153#endif
154
155  VGLCheckSwitch();
156  if (x<0 || x>=object->VXsize || y<0 || y>=object->VYsize)
157    return 0;
158  switch (object->Type) {
159    case MEMBUF:
160    case VIDBUF8:
161      return object->Bitmap[((y*object->VXsize)+x)];
162    case VIDBUF8S:
163      return object->Bitmap[VGLSetSegment(y*object->VXsize+x)];
164    case VIDBUF16:
165    case VIDBUF24:
166    case VIDBUF32:
167      bcopy(&object->Bitmap[(y*object->VXsize+x) * object->PixelBytes],
168		b, object->PixelBytes);
169      return (mem2color(b, object->PixelBytes));
170    case VIDBUF16S:
171    case VIDBUF24S:
172    case VIDBUF32S:
173	offset = VGLSetSegment((y*object->VXsize+x) * object->PixelBytes);
174	bcopy(&object->Bitmap[offset], b, object->PixelBytes);
175
176      return (mem2color(b, object->PixelBytes));
177    case VIDBUF8X:
178      outb(0x3ce, 0x04); outb(0x3cf, x & 0x3);
179      return object->Bitmap[(unsigned)(VGLAdpInfo.va_line_width*y)+(x/4)];
180    case VIDBUF4S:
181      offset = VGLSetSegment(y*VGLAdpInfo.va_line_width + x/8);
182      goto get_planar;
183    case VIDBUF4:
184      offset = y*VGLAdpInfo.va_line_width + x/8;
185get_planar:
186#if 1
187      return (object->Bitmap[offset]&(0x80>>(x%8))) ? 1 : 0;	/* XXX */
188#else
189      color = 0;
190      mask = 0x80 >> (x%8);
191      for (i = 0; i < VGLModeInfo.vi_planes; i++) {
192	outb(0x3ce, 0x04); outb(0x3cf, i);
193	color |= (object->Bitmap[offset] & mask) ? (1 << i) : 0;
194      }
195      return color;
196#endif
197  }
198  return 0;		/* XXX black? */
199}
200
201 /*
202  * Symmetric Double Step Line Algorithm by Brian Wyvill from
203  * "Graphics Gems", Academic Press, 1990.
204  */
205
206#define SL_SWAP(a,b)           {a^=b; b^=a; a^=b;}
207#define SL_ABSOLUTE(i,j,k)     ( (i-j)*(k = ( (i-j)<0 ? -1 : 1)))
208
209void
210plot(VGLBitmap * object, int x, int y, int flag, byte color)
211{
212  /* non-zero flag indicates the pixels need swapping back. */
213  if (flag)
214    VGLSetXY(object, y, x, color);
215  else
216    VGLSetXY(object, x, y, color);
217}
218
219
220void
221VGLLine(VGLBitmap *object, int x1, int y1, int x2, int y2, u_long color)
222{
223  int dx, dy, incr1, incr2, D, x, y, xend, c, pixels_left;
224  int sign_x, sign_y, step, reverse, i;
225
226  dx = SL_ABSOLUTE(x2, x1, sign_x);
227  dy = SL_ABSOLUTE(y2, y1, sign_y);
228  /* decide increment sign by the slope sign */
229  if (sign_x == sign_y)
230    step = 1;
231  else
232    step = -1;
233
234  if (dy > dx) {	/* chooses axis of greatest movement (make dx) */
235    SL_SWAP(x1, y1);
236    SL_SWAP(x2, y2);
237    SL_SWAP(dx, dy);
238    reverse = 1;
239  } else
240    reverse = 0;
241  /* note error check for dx==0 should be included here */
242  if (x1 > x2) {      /* start from the smaller coordinate */
243    x = x2;
244    y = y2;
245/*  x1 = x1;
246    y1 = y1; */
247  } else {
248    x = x1;
249    y = y1;
250    x1 = x2;
251    y1 = y2;
252  }
253
254
255  /* Note dx=n implies 0 - n or (dx+1) pixels to be set */
256  /* Go round loop dx/4 times then plot last 0,1,2 or 3 pixels */
257  /* In fact (dx-1)/4 as 2 pixels are already plotted */
258  xend = (dx - 1) / 4;
259  pixels_left = (dx - 1) % 4;  /* number of pixels left over at the
260           * end */
261  plot(object, x, y, reverse, color);
262  if (pixels_left < 0)
263    return;      /* plot only one pixel for zero length
264           * vectors */
265  plot(object, x1, y1, reverse, color);  /* plot first two points */
266  incr2 = 4 * dy - 2 * dx;
267  if (incr2 < 0) {    /* slope less than 1/2 */
268    c = 2 * dy;
269    incr1 = 2 * c;
270    D = incr1 - dx;
271
272    for (i = 0; i < xend; i++) {  /* plotting loop */
273      ++x;
274      --x1;
275      if (D < 0) {
276        /* pattern 1 forwards */
277        plot(object, x, y, reverse, color);
278        plot(object, ++x, y, reverse, color);
279        /* pattern 1 backwards */
280        plot(object, x1, y1, reverse, color);
281        plot(object, --x1, y1, reverse, color);
282        D += incr1;
283      } else {
284        if (D < c) {
285          /* pattern 2 forwards */
286          plot(object, x, y, reverse, color);
287          plot(object, ++x, y += step, reverse,
288              color);
289          /* pattern 2 backwards */
290          plot(object, x1, y1, reverse, color);
291          plot(object, --x1, y1 -= step, reverse,
292              color);
293        } else {
294          /* pattern 3 forwards */
295          plot(object, x, y += step, reverse, color);
296          plot(object, ++x, y, reverse, color);
297          /* pattern 3 backwards */
298          plot(object, x1, y1 -= step, reverse,
299              color);
300          plot(object, --x1, y1, reverse, color);
301        }
302        D += incr2;
303      }
304    }      /* end for */
305
306    /* plot last pattern */
307    if (pixels_left) {
308      if (D < 0) {
309        plot(object, ++x, y, reverse, color);  /* pattern 1 */
310        if (pixels_left > 1)
311          plot(object, ++x, y, reverse, color);
312        if (pixels_left > 2)
313          plot(object, --x1, y1, reverse, color);
314      } else {
315        if (D < c) {
316          plot(object, ++x, y, reverse, color);  /* pattern 2  */
317          if (pixels_left > 1)
318            plot(object, ++x, y += step, reverse, color);
319          if (pixels_left > 2)
320            plot(object, --x1, y1, reverse, color);
321        } else {
322          /* pattern 3 */
323          plot(object, ++x, y += step, reverse, color);
324          if (pixels_left > 1)
325            plot(object, ++x, y, reverse, color);
326          if (pixels_left > 2)
327            plot(object, --x1, y1 -= step, reverse, color);
328        }
329      }
330    }      /* end if pixels_left */
331  }
332  /* end slope < 1/2 */
333  else {        /* slope greater than 1/2 */
334    c = 2 * (dy - dx);
335    incr1 = 2 * c;
336    D = incr1 + dx;
337    for (i = 0; i < xend; i++) {
338      ++x;
339      --x1;
340      if (D > 0) {
341        /* pattern 4 forwards */
342        plot(object, x, y += step, reverse, color);
343        plot(object, ++x, y += step, reverse, color);
344        /* pattern 4 backwards */
345        plot(object, x1, y1 -= step, reverse, color);
346        plot(object, --x1, y1 -= step, reverse, color);
347        D += incr1;
348      } else {
349        if (D < c) {
350          /* pattern 2 forwards */
351          plot(object, x, y, reverse, color);
352          plot(object, ++x, y += step, reverse,
353              color);
354
355          /* pattern 2 backwards */
356          plot(object, x1, y1, reverse, color);
357          plot(object, --x1, y1 -= step, reverse,
358              color);
359        } else {
360          /* pattern 3 forwards */
361          plot(object, x, y += step, reverse, color);
362          plot(object, ++x, y, reverse, color);
363          /* pattern 3 backwards */
364          plot(object, x1, y1 -= step, reverse, color);
365          plot(object, --x1, y1, reverse, color);
366        }
367        D += incr2;
368      }
369    }      /* end for */
370    /* plot last pattern */
371    if (pixels_left) {
372      if (D > 0) {
373        plot(object, ++x, y += step, reverse, color);  /* pattern 4 */
374        if (pixels_left > 1)
375          plot(object, ++x, y += step, reverse,
376              color);
377        if (pixels_left > 2)
378          plot(object, --x1, y1 -= step, reverse,
379              color);
380      } else {
381        if (D < c) {
382          plot(object, ++x, y, reverse, color);  /* pattern 2  */
383          if (pixels_left > 1)
384            plot(object, ++x, y += step, reverse, color);
385          if (pixels_left > 2)
386            plot(object, --x1, y1, reverse, color);
387        } else {
388          /* pattern 3 */
389          plot(object, ++x, y += step, reverse, color);
390          if (pixels_left > 1)
391            plot(object, ++x, y, reverse, color);
392          if (pixels_left > 2) {
393            if (D > c)  /* step 3 */
394              plot(object, --x1, y1 -= step, reverse, color);
395            else  /* step 2 */
396              plot(object, --x1, y1, reverse, color);
397          }
398        }
399      }
400    }
401  }
402}
403
404void
405VGLBox(VGLBitmap *object, int x1, int y1, int x2, int y2, u_long color)
406{
407  VGLLine(object, x1, y1, x2, y1, color);
408  VGLLine(object, x2, y1, x2, y2, color);
409  VGLLine(object, x2, y2, x1, y2, color);
410  VGLLine(object, x1, y2, x1, y1, color);
411}
412
413void
414VGLFilledBox(VGLBitmap *object, int x1, int y1, int x2, int y2, u_long color)
415{
416  int y;
417
418  for (y=y1; y<=y2; y++) VGLLine(object, x1, y, x2, y, color);
419}
420
421static inline void
422set4pixels(VGLBitmap *object, int x, int y, int xc, int yc, u_long color)
423{
424  if (x!=0) {
425    VGLSetXY(object, xc+x, yc+y, color);
426    VGLSetXY(object, xc-x, yc+y, color);
427    if (y!=0) {
428      VGLSetXY(object, xc+x, yc-y, color);
429      VGLSetXY(object, xc-x, yc-y, color);
430    }
431  }
432  else {
433    VGLSetXY(object, xc, yc+y, color);
434    if (y!=0)
435      VGLSetXY(object, xc, yc-y, color);
436  }
437}
438
439void
440VGLEllipse(VGLBitmap *object, int xc, int yc, int a, int b, u_long color)
441{
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;
444
445  while (dx<dy) {
446    set4pixels(object, x, y, xc, yc, color);
447    if (d>0) {
448      y--; dy-=asq2; d-=dy;
449    }
450    x++; dx+=bsq2; d+=bsq+dx;
451  }
452  d+=(3*(asq-bsq)/2-(dx+dy))/2;
453  while (y>=0) {
454    set4pixels(object, x, y, xc, yc, color);
455    if (d<0) {
456      x++; dx+=bsq2; d+=dx;
457    }
458    y--; dy-=asq2; d+=asq-dy;
459  }
460}
461
462static inline void
463set2lines(VGLBitmap *object, int x, int y, int xc, int yc, u_long color)
464{
465  if (x!=0) {
466    VGLLine(object, xc+x, yc+y, xc-x, yc+y, color);
467    if (y!=0)
468      VGLLine(object, xc+x, yc-y, xc-x, yc-y, color);
469  }
470  else {
471    VGLLine(object, xc, yc+y, xc, yc-y, color);
472  }
473}
474
475void
476VGLFilledEllipse(VGLBitmap *object, int xc, int yc, int a, int b, u_long color)
477{
478  int x = 0, y = b, asq = a*a, asq2 = a*a*2, bsq = b*b;
479  int bsq2 = b*b*2, d = bsq-asq*b+asq/4, dx = 0, dy = asq2*b;
480
481  while (dx<dy) {
482    set2lines(object, x, y, xc, yc, color);
483    if (d>0) {
484      y--; dy-=asq2; d-=dy;
485    }
486    x++; dx+=bsq2; d+=bsq+dx;
487  }
488  d+=(3*(asq-bsq)/2-(dx+dy))/2;
489  while (y>=0) {
490    set2lines(object, x, y, xc, yc, color);
491    if (d<0) {
492      x++; dx+=bsq2; d+=dx;
493    }
494    y--; dy-=asq2; d+=asq-dy;
495  }
496}
497
498void
499VGLClear(VGLBitmap *object, u_long color)
500{
501  int offset;
502  int len;
503  int i, total = 0;
504  byte b[4];
505
506  VGLCheckSwitch();
507  VGLMouseFreeze(0, 0, object->Xsize, object->Ysize, color); /* XXX */
508  switch (object->Type) {
509  case MEMBUF:
510  case VIDBUF8:
511    memset(object->Bitmap, (byte)color, object->VXsize*object->VYsize);
512    break;
513
514  case VIDBUF8S:
515    for (offset = 0; offset < object->VXsize*object->VYsize; ) {
516      VGLSetSegment(offset);
517      len = min(object->VXsize*object->VYsize - offset,
518		VGLAdpInfo.va_window_size);
519      memset(object->Bitmap, (byte)color, len);
520      offset += len;
521    }
522    break;
523  case VIDBUF16:
524  case VIDBUF24:
525  case VIDBUF32:
526    color2mem(color, b, object->PixelBytes);
527    total = object->VXsize*object->VYsize*object->PixelBytes;
528    for (i = 0; i < total; i += object->PixelBytes)
529      bcopy(b, object->Bitmap + i, object->PixelBytes);
530    break;
531
532  case VIDBUF16S:
533  case VIDBUF24S:
534  case VIDBUF32S:
535    color2mem(color, b, object->PixelBytes);
536    total = object->VXsize*object->VYsize*object->PixelBytes;
537    for (offset = 0; offset < total; ) {
538      VGLSetSegment(offset);
539      len = min(total - offset, VGLAdpInfo.va_window_size);
540      for (i = 0; i < len; i += object->PixelBytes)
541	bcopy(b, object->Bitmap + offset + i, object->PixelBytes);
542      offset += len;
543    }
544    break;
545
546  case VIDBUF8X:
547    /* XXX works only for Xsize % 4 = 0 */
548    outb(0x3c6, 0xff);
549    outb(0x3c4, 0x02); outb(0x3c5, 0x0f);
550    memset(object->Bitmap, (byte)color, VGLAdpInfo.va_line_width*object->VYsize);
551    break;
552
553  case VIDBUF4:
554  case VIDBUF4S:
555    /* XXX works only for Xsize % 8 = 0 */
556    outb(0x3c4, 0x02); outb(0x3c5, 0x0f);
557    outb(0x3ce, 0x05); outb(0x3cf, 0x02);		/* mode 2 */
558    outb(0x3ce, 0x01); outb(0x3cf, 0x00);		/* set/reset enable */
559    outb(0x3ce, 0x08); outb(0x3cf, 0xff);		/* bit mask */
560    for (offset = 0; offset < VGLAdpInfo.va_line_width*object->VYsize; ) {
561      VGLSetSegment(offset);
562      len = min(object->VXsize*object->VYsize - offset,
563		VGLAdpInfo.va_window_size);
564      memset(object->Bitmap, (byte)color, len);
565      offset += len;
566    }
567    outb(0x3ce, 0x05); outb(0x3cf, 0x00);
568    break;
569  }
570  VGLMouseUnFreeze();
571}
572
573void
574VGLRestorePalette()
575{
576  int i;
577
578  outb(0x3C6, 0xFF);
579  inb(0x3DA);
580  outb(0x3C8, 0x00);
581  for (i=0; i<256; i++) {
582    outb(0x3C9, VGLSavePaletteRed[i]);
583    inb(0x84);
584    outb(0x3C9, VGLSavePaletteGreen[i]);
585    inb(0x84);
586    outb(0x3C9, VGLSavePaletteBlue[i]);
587    inb(0x84);
588  }
589  inb(0x3DA);
590  outb(0x3C0, 0x20);
591}
592
593void
594VGLSavePalette()
595{
596  int i;
597
598  outb(0x3C6, 0xFF);
599  inb(0x3DA);
600  outb(0x3C7, 0x00);
601  for (i=0; i<256; i++) {
602    VGLSavePaletteRed[i] = inb(0x3C9);
603    inb(0x84);
604    VGLSavePaletteGreen[i] = inb(0x3C9);
605    inb(0x84);
606    VGLSavePaletteBlue[i] = inb(0x3C9);
607    inb(0x84);
608  }
609  inb(0x3DA);
610  outb(0x3C0, 0x20);
611}
612
613void
614VGLSetPalette(byte *red, byte *green, byte *blue)
615{
616  int i;
617
618  for (i=0; i<256; i++) {
619    VGLSavePaletteRed[i] = red[i];
620    VGLSavePaletteGreen[i] = green[i];
621    VGLSavePaletteBlue[i] = blue[i];
622  }
623  VGLCheckSwitch();
624  outb(0x3C6, 0xFF);
625  inb(0x3DA);
626  outb(0x3C8, 0x00);
627  for (i=0; i<256; i++) {
628    outb(0x3C9, VGLSavePaletteRed[i]);
629    inb(0x84);
630    outb(0x3C9, VGLSavePaletteGreen[i]);
631    inb(0x84);
632    outb(0x3C9, VGLSavePaletteBlue[i]);
633    inb(0x84);
634  }
635  inb(0x3DA);
636  outb(0x3C0, 0x20);
637}
638
639void
640VGLSetPaletteIndex(byte color, byte red, byte green, byte blue)
641{
642  VGLSavePaletteRed[color] = red;
643  VGLSavePaletteGreen[color] = green;
644  VGLSavePaletteBlue[color] = blue;
645  VGLCheckSwitch();
646  outb(0x3C6, 0xFF);
647  inb(0x3DA);
648  outb(0x3C8, color);
649  outb(0x3C9, red); outb(0x3C9, green); outb(0x3C9, blue);
650  inb(0x3DA);
651  outb(0x3C0, 0x20);
652}
653
654void
655VGLSetBorder(byte color)
656{
657  VGLCheckSwitch();
658  inb(0x3DA);
659  outb(0x3C0,0x11); outb(0x3C0, color);
660  inb(0x3DA);
661  outb(0x3C0, 0x20);
662}
663
664void
665VGLBlankDisplay(int blank)
666{
667  byte val;
668
669  VGLCheckSwitch();
670  outb(0x3C4, 0x01); val = inb(0x3C5); outb(0x3C4, 0x01);
671  outb(0x3C5, ((blank) ? (val |= 0x20) : (val &= 0xDF)));
672}
673