1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723
|
/* BOGL - Ben's Own Graphics Library.
Written by Ben Pfaff <pfaffben@debian.org>.
VGA optimisations
(c) Copyright Red Hat Inc 2002, <alan@redhat.com>.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA. */
/*#define NDEBUG*/
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <linux/fb.h>
#include <linux/kd.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/io.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include "bogl.h"
#include "boglP.h"
#include "bogl-vga16.h"
/* VGA16 support for BOGL. */
/* bits_set[X] is the number of nonzero bits in X. */
static unsigned char bits_set[256];
static int cached_color = -2;
static int cached_sr_mask = -1;
static int cached_op = -1;
static int cached_write_mask = -1;
static int cached_plane = -1;
static int cached_mode = -1;
/* Program the Set/Reset Register for drawing in color COLOR for write
mode 0. */
static inline void
set_color (int c)
{
if(cached_color != c)
{
outb (0, 0x3ce);
outb (c, 0x3cf);
cached_color = c;
}
}
/* Set the Enable Set/Reset Register. */
static inline void
set_enable_sr (int mask)
{
if(cached_sr_mask != mask)
{
outb (1, 0x3ce);
outb (mask, 0x3cf);
cached_sr_mask = mask;
}
}
/* Select the Bit Mask Register on the Graphics Controller. */
static inline void
select_mask (void)
{
outb (8, 0x3ce);
}
/* Program the Bit Mask Register to affect only the pixels selected in
MASK. The Bit Mask Register must already have been selected with
select_mask (). */
static inline void
set_mask (int mask)
{
outb (mask, 0x3cf);
}
/* Set the Data Rotate Register. Bits 0-2 are rotate count, bits 3-4
are logical operation (0=NOP, 1=AND, 2=OR, 3=XOR). */
static inline void
set_op (int op)
{
if(op != cached_op)
{
outb (3, 0x3ce);
outb (op, 0x3cf);
cached_op = op;
}
}
/* Set the Memory Plane Write Enable register. */
static inline void
set_write_planes (int mask)
{
if(mask != cached_write_mask)
{
outb (2, 0x3c4);
outb (mask, 0x3c5);
mask = cached_write_mask;
}
}
/* Set the Read Map Select register. */
static inline void
set_read_plane (int plane)
{
if(cached_plane != plane)
{
outb (4, 0x3ce);
outb (plane, 0x3cf);
cached_plane = plane;
}
}
/* Set the Graphics Mode Register. The write mode is in bits 0-1, the
read mode is in bit 3. */
static inline void
set_mode (int mode)
{
if(cached_mode != mode)
{
outb (5, 0x3ce);
outb (mode, 0x3cf);
cached_mode = mode;
}
}
/* Read-modify-write the specified memory byte. */
static inline void
rmw (volatile char *p)
{
*p |= 1;
}
/* Set pixel (X,Y) to color C. */
void
bogl_vga16_pixel (int x, int y, int c)
{
bogl_drawing = 1;
assert (x >= 0 && x < bogl_xres);
assert (y >= 0 && y < bogl_yres);
set_color (c);
select_mask ();
set_mask (0x80 >> (x % 8));
rmw (bogl_frame + x / 8 + y * bogl_line_len);
bogl_drawing = 0;
}
/* Paint a horizontal line from (X1,Y) to (X2,Y) in color C, where X2
>= X1. The final point is not painted. */
void
bogl_vga16_hline (int x1, int x2, int y, int c)
{
volatile char *dst;
assert (x1 >= 0 && x1 < bogl_xres);
assert (x2 >= 0 && x2 <= bogl_xres);
assert (x2 >= x1);
assert (y >= 0 && y < bogl_yres);
if (x1 == x2)
return;
x2--;
bogl_drawing = 1;
set_color (c);
dst = bogl_frame + x1 / 8 + y * bogl_line_len;
select_mask ();
if (x1 / 8 == x2 / 8)
{
set_mask ((0xff >> (x1 % 8)) & (0xff << (7 - x2 % 8)));
rmw (dst);
}
else
{
volatile char *last;
set_mask (0xff >> (x1 % 8));
rmw (dst++);
set_mask (0xff);
last = bogl_frame + x2 / 8 + y * bogl_line_len;
while (dst < last)
{
while(!((unsigned long)dst&3) && last-dst > 3)
{
*(unsigned int *)dst = 0x01010101;
dst+=4;
}
if(dst < last)
*dst++ = 1;
}
set_mask (0xff << (7 - x2 % 8));
rmw (dst);
}
bogl_drawing = 0;
}
/* Paints a vertical line from (X,Y1) to (X,Y2) in color C. The final
point is not painted. */
void
bogl_vga16_vline (int x, int y1, int y2, int c)
{
volatile char *dst, *last;
assert (x >= 0 && x < bogl_xres);
assert (y1 >= 0 && y1 < bogl_yres);
assert (y2 >= 0 && y2 <= bogl_yres);
assert (y2 >= y1);
y2--;
bogl_drawing = 1;
set_color (c);
select_mask ();
set_mask (0x80 >> (x % 8));
dst = bogl_frame + x / 8 + y1 * bogl_line_len;
last = bogl_frame + x / 8 + y2 * bogl_line_len;
while (dst <= last)
{
rmw (dst);
dst += bogl_line_len;
}
bogl_drawing = 0;
}
/* Clear the region from (X1,Y1) to (X2,Y2) to color C, not including
the last row or column. If C == -1 then the region's colors are
inverted rather than set to a particular color. */
void
bogl_vga16_clear (int x1, int y1, int x2, int y2, int c)
{
volatile char *dst;
/* A kludge to make dbootstrap work in a bterm
assert (x1 >= 0 && x1 < bogl_xres);
assert (x2 >= 0 && x2 <= bogl_xres);
assert (x2 >= x1);
assert (y1 >= 0 && y1 < bogl_yres);
assert (y2 >= 0 && y2 <= bogl_yres);
assert (y2 >= y1);
*/
if (x1 == x2)
return;
x2--;
bogl_drawing = 1;
set_color (c);
if (c == -1)
set_op (0x18);
select_mask ();
if (x1 / 8 == x2 / 8)
{
volatile char *last;
dst = bogl_frame + x1 / 8 + y1 * bogl_line_len;
last = bogl_frame + x2 / 8 + y2 * bogl_line_len;
set_mask ((0xff >> (x1 % 8)) & (0xff << (7 - x2 % 8)));
while (dst < last)
{
rmw (dst);
dst += bogl_line_len;
}
}
else
{
/* FIXME: the following code could admittedly be more efficient,
but my first attempt at optimization was buggy. */
int y;
for (y = y1; y < y2; y++)
{
volatile char *last;
dst = bogl_frame + x1 / 8 + y * bogl_line_len;
set_mask (0xff >> (x1 % 8));
rmw (dst++);
set_mask (0xff);
last = bogl_frame + x2 / 8 + y * bogl_line_len;
while (dst < last)
{
while(!((unsigned long)dst&3) && last-dst > 3)
{
*(unsigned int *)dst = 0x01010101;
dst+=4;
}
if(dst < last)
*dst++ = 1;
}
set_mask (0xff << (7 - x2 % 8));
rmw (dst);
}
}
set_op (0);
bogl_drawing = 0;
}
/* FIXME: it would be faster to use write mode 3 to write the middle
bytes (but also more complex). */
#define ul_size (sizeof (u_int32_t))
#define ul_bits (CHAR_BIT * ul_size)
void
bogl_vga16_text (int xx, int yy, const char *s, int n, int fg, int bg, int ul,
const struct bogl_font *font)
{
/* Font height, or possibly less due to clipping. */
int h;
int x, y;
u_int32_t bits[font->height];
int k;
wchar_t wc;
void plot (int bg)
{
volatile char *dst = bogl_frame + xx / 8 + yy * bogl_line_len;
int y, i;
for (y = 0; y < h; y++)
{
u_int32_t b = bits[y];
for (i = ul_size - 1; i >= 0; i--)
{
if((b & 0xFF) || bg == -1)
{
set_mask (b);
rmw (dst + i);
}
b >>= 8;
}
dst += bogl_line_len;
}
}
assert (xx >= 0 && xx < bogl_xres);
assert (yy >= 0 && yy < bogl_yres);
h = font->height;
if (yy + h > bogl_yres)
h = bogl_yres - yy;
if (bg != -1)
{
int x2 = xx + bogl_metrics (s, n, font);
if (x2 >= bogl_xres)
x2 = bogl_xres - 1;
bogl_vga16_clear (xx, yy, x2, yy + h, bg);
}
bogl_drawing = 1;
for (y = 0; y < h; y++)
bits[y] = 0;
set_color (fg);
select_mask ();
x = xx % ul_bits;
xx = xx / ul_bits * ul_bits;
mbtowc (0, 0, 0);
for (; (k = mbtowc (&wc, s, n)) > 0; s += k, n -= k)
{
u_int32_t *character = NULL;
int width = bogl_font_glyph (font, wc, &character);
if (character == NULL)
continue;
for (y = 0; y < h; y++)
bits[y] |= character[y] >> x;
x += width;
if (x >= (int) ul_bits)
{
plot (bg);
x -= ul_bits;
for (y = 0; y < h; y++)
bits[y] = character[y] << (width - x);
xx += ul_bits;
if (xx >= bogl_xres)
goto done;
}
}
plot (bg);
done:
bogl_drawing = 0;
}
/* Write PIXMAP at location (XX,YY), with the pixmap's colors mapped
according to COLOR_MAP. */
void
bogl_vga16_put (int xx, int yy, const struct bogl_pixmap *pixmap,
const int color_map[16])
{
volatile char *dst;
const unsigned char *src;
int x, y;
assert (xx >= 0 && xx < bogl_xres);
assert (xx + pixmap->width <= bogl_xres);
assert (yy >= 0 && yy < bogl_yres);
assert (yy + pixmap->height <= bogl_yres);
src = pixmap->data;
bogl_drawing = 1;
y = yy;
while (y < yy + pixmap->height)
{
x = xx;
dst = bogl_frame + x / 8 + y * bogl_line_len;
while (x < xx + pixmap->width)
{
int color = *src & 0xf;
int count = *src >> 4;
src++;
if (color == pixmap->transparent)
{
dst += (x + count) / 8 - x / 8;
x += count;
continue;
}
set_color (color_map[color]);
select_mask ();
if (count == 1)
{
set_mask (0x80 >> (x % 8));
*dst |= 1;
if (++x % 8 == 0)
dst++;
continue;
}
for (;;)
{
/* Get a mask for COUNT bits starting at X, or at least
as many as will fit in one byte. */
unsigned mask;
mask = (0xffff00 >> count) & 0xff;
mask >>= x % 8;
set_mask (mask);
/* Write the bits and bow out if we don't need to
advance to the next byte. */
*dst |= 1;
if ((mask & 1) == 0)
{
x += count;
break;
}
/* Advance to the next byte. */
dst++;
x += bits_set[mask];
count -= bits_set[mask];
if (!count)
break;
}
}
y++;
}
bogl_drawing = 0;
}
/* Draw mouse pointer POINTER with its hotspot at (X,Y), if VISIBLE !=
0. Restores the previously saved background at that point, if
VISIBLE == 0. COLORS[] gives the color indices to paint the
cursor.
This routine performs full clipping on all sides of the screen. */
void
bogl_vga16_pointer (int visible, int x1, int y1,
const struct bogl_pointer *pointer,
int colors[2])
{
int y_count; /* Number of scanlines. */
int y_ofs; /* Number of scanlines to skip drawing. */
int x_ofs; /* Number of pixels to skip drawing on each line. */
assert (pointer != NULL);
x1 -= pointer->hx;
y1 -= pointer->hy;
if (y1 + 16 > bogl_yres)
y_count = bogl_yres - y1;
else
y_count = 16;
if (x1 < 0)
{
x_ofs = -x1;
x1 = 0;
}
else
x_ofs = 0;
if (y1 < 0)
{
y_ofs = -y1;
y1 = 0;
y_count -= y_ofs;
}
else
y_ofs = 0;
bogl_drawing = 1;
/* Save or restore the framebuffer contents. */
{
/* Four planes of sixteen rows of four bytes each. */
static unsigned char saved[4 * 16 * 3];
int plane; /* Current plane. */
int sx_ofs; /* Byte offset within a scanline to save/restore. */
sx_ofs = x1 / 8;
if (sx_ofs + 3 > bogl_line_len)
sx_ofs = bogl_line_len - 3;
if (visible)
{
for (plane = 0; plane < 4; plane++)
{
volatile char *dst = (char *) saved + plane * 16 * 3;
volatile char *src = bogl_frame + sx_ofs + y1 * bogl_line_len;
int y = y_count;
set_read_plane (plane);
while (y--)
{
*dst++ = *src++;
*dst++ = *src++;
*dst++ = *src++;
src += bogl_line_len - 3;
}
}
}
else
{
set_enable_sr (0);
select_mask ();
set_mask (0xff);
for (plane = 0; plane < 4; plane++)
{
volatile char *dst = bogl_frame + sx_ofs + y1 * bogl_line_len;
volatile char *src = (char *) saved + plane * 16 * 3;
int y = y_count;
set_write_planes (1 << plane);
while (y--)
{
*dst++ = *src++;
*dst++ = *src++;
*dst++ = *src++;
dst += bogl_line_len - 3;
}
}
set_write_planes (0xf);
set_enable_sr (0xf);
}
}
if (visible)
{
const unsigned short *mask_p, *color_p;
int x_count;
int y;
int color;
x_count = x1 % 8 ? 3 : 2;
if (x1 / 8 + x_count > bogl_line_len)
x_count = bogl_line_len - x1 / 8;
for (color = 0; color < 2; color++)
{
set_color (colors[color]);
select_mask ();
mask_p = pointer->mask + y_ofs;
color_p = pointer->color + y_ofs;
for (y = 0; y < y_count; y++, mask_p++, color_p++)
{
volatile char *dst;
u_int32_t bits;
int x;
dst = bogl_frame + x1 / 8 + (y1 + y) * bogl_line_len;
if (color)
bits = *mask_p ^ *color_p;
else
bits = *mask_p & *color_p;
bits <<= (CHAR_BIT * (sizeof (u_int32_t) - sizeof (short))) + x_ofs;
bits >>= x1 % 8;
x = x_count;
while (x--)
{
set_mask (bits >> 24);
rmw (dst++);
bits <<= 8;
}
}
}
}
bogl_drawing = 0;
}
/* Initialize the VGA controller. Returns the number of bytes to
memory map on success, or zero on failure. */
size_t
bogl_vga16_init (void)
{
#ifdef __x86_64__
/* temp. workaround until ioperm works on hammer */
if (-1 == iopl(3))
{
bogl_fail ("can't get IO permissions: %s", strerror (errno));
return 0;
}
#endif
if (-1 == ioperm (0x3c0, 0x20, 1))
{
bogl_fail ("can't get IO permissions: %s", strerror (errno));
return 0;
}
/* Set up some default values for the VGA Graphics Registers. */
set_enable_sr (0xf);
set_op (0);
set_mode (0);
/* Initialize bits_set array. */
{
int i;
for (i = 0; i < 256; i++)
{
int c, j;
for (c = j = 0; j < 8; j++)
c += (i & (1 << j)) != 0;
bits_set[i] = c;
}
}
return 0x10000;
}
/* Clear cached values that may no longer be correct after a VT switch. */
void
bogl_vga16_reinit (void)
{
cached_color = -1;
cached_sr_mask = -1;
cached_op = -1;
cached_write_mask = -1;
cached_plane = -1;
cached_mode = -1;
set_enable_sr (0xf);
set_write_planes (0xf);
set_op (0);
set_mode (0);
set_color (0);
set_read_plane (0);
}
/*
* vim:ts=8:sw=2
*/
|