1 /* $OpenBSD: rasops.c,v 1.59 2019/03/18 16:10:39 fcambus Exp $ */ 2 /* $NetBSD: rasops.c,v 1.35 2001/02/02 06:01:01 marcus Exp $ */ 3 4 /*- 5 * Copyright (c) 1999 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Andrew Doran. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 #include <sys/param.h> 34 #include <sys/malloc.h> 35 #include <sys/systm.h> 36 #include <sys/time.h> 37 #include <sys/task.h> 38 39 #include <dev/wscons/wsdisplayvar.h> 40 #include <dev/wscons/wsconsio.h> 41 #include <dev/wsfont/wsfont.h> 42 #include <dev/rasops/rasops.h> 43 44 #ifndef _KERNEL 45 #include <errno.h> 46 #endif 47 48 /* ANSI colormap (R,G,B) */ 49 50 #define NORMAL_BLACK 0x000000 51 #define NORMAL_RED 0x7f0000 52 #define NORMAL_GREEN 0x007f00 53 #define NORMAL_BROWN 0x7f7f00 54 #define NORMAL_BLUE 0x00007f 55 #define NORMAL_MAGENTA 0x7f007f 56 #define NORMAL_CYAN 0x007f7f 57 #define NORMAL_WHITE 0xc7c7c7 /* XXX too dim? */ 58 59 #define HILITE_BLACK 0x7f7f7f 60 #define HILITE_RED 0xff0000 61 #define HILITE_GREEN 0x00ff00 62 #define HILITE_BROWN 0xffff00 63 #define HILITE_BLUE 0x0000ff 64 #define HILITE_MAGENTA 0xff00ff 65 #define HILITE_CYAN 0x00ffff 66 #define HILITE_WHITE 0xffffff 67 68 const u_char rasops_cmap[256 * 3] = { 69 #define _C(x) ((x) & 0xff0000) >> 16, ((x) & 0x00ff00) >> 8, ((x) & 0x0000ff) 70 71 _C(NORMAL_BLACK), 72 _C(NORMAL_RED), 73 _C(NORMAL_GREEN), 74 _C(NORMAL_BROWN), 75 _C(NORMAL_BLUE), 76 _C(NORMAL_MAGENTA), 77 _C(NORMAL_CYAN), 78 _C(NORMAL_WHITE), 79 80 _C(HILITE_BLACK), 81 _C(HILITE_RED), 82 _C(HILITE_GREEN), 83 _C(HILITE_BROWN), 84 _C(HILITE_BLUE), 85 _C(HILITE_MAGENTA), 86 _C(HILITE_CYAN), 87 _C(HILITE_WHITE), 88 89 /* 90 * For the cursor, we need the last 16 colors to be the 91 * opposite of the first 16. Fill the intermediate space with 92 * white completely for simplicity. 93 */ 94 #define _CMWHITE16 \ 95 _C(HILITE_WHITE), _C(HILITE_WHITE), _C(HILITE_WHITE), _C(HILITE_WHITE), \ 96 _C(HILITE_WHITE), _C(HILITE_WHITE), _C(HILITE_WHITE), _C(HILITE_WHITE), \ 97 _C(HILITE_WHITE), _C(HILITE_WHITE), _C(HILITE_WHITE), _C(HILITE_WHITE), \ 98 _C(HILITE_WHITE), _C(HILITE_WHITE), _C(HILITE_WHITE), _C(HILITE_WHITE), 99 _CMWHITE16 _CMWHITE16 _CMWHITE16 _CMWHITE16 _CMWHITE16 100 _CMWHITE16 _CMWHITE16 _CMWHITE16 _CMWHITE16 _CMWHITE16 101 _CMWHITE16 _CMWHITE16 _CMWHITE16 _CMWHITE16 102 #undef _CMWHITE16 103 104 _C(~HILITE_WHITE), 105 _C(~HILITE_CYAN), 106 _C(~HILITE_MAGENTA), 107 _C(~HILITE_BLUE), 108 _C(~HILITE_BROWN), 109 _C(~HILITE_GREEN), 110 _C(~HILITE_RED), 111 _C(~HILITE_BLACK), 112 113 _C(~NORMAL_WHITE), 114 _C(~NORMAL_CYAN), 115 _C(~NORMAL_MAGENTA), 116 _C(~NORMAL_BLUE), 117 _C(~NORMAL_BROWN), 118 _C(~NORMAL_GREEN), 119 _C(~NORMAL_RED), 120 _C(~NORMAL_BLACK), 121 122 #undef _C 123 }; 124 125 /* True if color is gray */ 126 const u_char rasops_isgray[16] = { 127 1, 0, 0, 0, 128 0, 0, 0, 1, 129 1, 0, 0, 0, 130 0, 0, 0, 1 131 }; 132 133 struct rasops_screen { 134 LIST_ENTRY(rasops_screen) rs_next; 135 struct rasops_info *rs_ri; 136 137 struct wsdisplay_charcell *rs_bs; 138 int rs_visible; 139 int rs_crow; 140 int rs_ccol; 141 long rs_defattr; 142 143 int rs_sbscreens; 144 #define RS_SCROLLBACK_SCREENS 5 145 int rs_dispoffset; /* rs_bs index, start of our actual screen */ 146 int rs_visibleoffset; /* rs_bs index, current scrollback screen */ 147 }; 148 149 /* Generic functions */ 150 int rasops_copycols(void *, int, int, int, int); 151 int rasops_copyrows(void *, int, int, int); 152 int rasops_mapchar(void *, int, u_int *); 153 int rasops_cursor(void *, int, int, int); 154 int rasops_alloc_cattr(void *, int, int, int, long *); 155 int rasops_alloc_mattr(void *, int, int, int, long *); 156 int rasops_do_cursor(struct rasops_info *); 157 void rasops_init_devcmap(struct rasops_info *); 158 void rasops_unpack_attr(void *, long, int *, int *, int *); 159 #if NRASOPS_BSWAP > 0 160 static void slow_bcopy(void *, void *, size_t); 161 #endif 162 #if NRASOPS_ROTATION > 0 163 void rasops_copychar(void *, int, int, int, int); 164 int rasops_copycols_rotated(void *, int, int, int, int); 165 int rasops_copyrows_rotated(void *, int, int, int); 166 int rasops_erasecols_rotated(void *, int, int, int, long); 167 int rasops_eraserows_rotated(void *, int, int, long); 168 int rasops_putchar_rotated(void *, int, int, u_int, long); 169 void rasops_rotate_font(int *, int); 170 171 /* 172 * List of all rotated fonts 173 */ 174 SLIST_HEAD(, rotatedfont) rotatedfonts = SLIST_HEAD_INITIALIZER(rotatedfonts); 175 struct rotatedfont { 176 SLIST_ENTRY(rotatedfont) rf_next; 177 int rf_cookie; 178 int rf_rotated; 179 }; 180 #endif 181 182 void rasops_doswitch(void *); 183 int rasops_vcons_cursor(void *, int, int, int); 184 int rasops_vcons_mapchar(void *, int, u_int *); 185 int rasops_vcons_putchar(void *, int, int, u_int, long); 186 int rasops_vcons_copycols(void *, int, int, int, int); 187 int rasops_vcons_erasecols(void *, int, int, int, long); 188 int rasops_vcons_copyrows(void *, int, int, int); 189 int rasops_vcons_eraserows(void *, int, int, long); 190 int rasops_vcons_alloc_attr(void *, int, int, int, long *); 191 void rasops_vcons_unpack_attr(void *, long, int *, int *, int *); 192 193 int rasops_wronly_putchar(void *, int, int, u_int, long); 194 int rasops_wronly_copycols(void *, int, int, int, int); 195 int rasops_wronly_erasecols(void *, int, int, int, long); 196 int rasops_wronly_copyrows(void *, int, int, int); 197 int rasops_wronly_eraserows(void *, int, int, long); 198 int rasops_wronly_do_cursor(struct rasops_info *); 199 200 int rasops_add_font(struct rasops_info *, struct wsdisplay_font *); 201 int rasops_use_font(struct rasops_info *, struct wsdisplay_font *); 202 int rasops_list_font_cb(void *, struct wsdisplay_font *); 203 204 /* 205 * Initialize a 'rasops_info' descriptor. 206 */ 207 int 208 rasops_init(struct rasops_info *ri, int wantrows, int wantcols) 209 { 210 211 #ifdef _KERNEL 212 /* Select a font if the caller doesn't care */ 213 if (ri->ri_font == NULL) { 214 int cookie = -1; 215 216 wsfont_init(); 217 218 if (ri->ri_width >= 120 * 32) 219 /* Screen width larger than 3840px, 32px wide font */ 220 cookie = wsfont_find(NULL, 32, 0, 0); 221 222 if (cookie <= 0 && ri->ri_width >= 120 * 16) 223 /* Screen width larger than 1920px, 16px wide font */ 224 cookie = wsfont_find(NULL, 16, 0, 0); 225 226 if (cookie <= 0 && ri->ri_width > 80 * 12) 227 /* Screen width larger than 960px, 12px wide font */ 228 cookie = wsfont_find(NULL, 12, 0, 0); 229 230 if (cookie <= 0) 231 /* Lower resolution, choose a 8px wide font */ 232 cookie = wsfont_find(NULL, 8, 0, 0); 233 234 if (cookie <= 0) 235 cookie = wsfont_find(NULL, 0, 0, 0); 236 237 if (cookie <= 0) { 238 printf("rasops_init: font table is empty\n"); 239 return (-1); 240 } 241 242 #if NRASOPS_ROTATION > 0 243 /* 244 * Pick the rotated version of this font. This will create it 245 * if necessary. 246 */ 247 if (ri->ri_flg & (RI_ROTATE_CW | RI_ROTATE_CCW)) 248 rasops_rotate_font(&cookie, 249 ISSET(ri->ri_flg, RI_ROTATE_CCW)); 250 #endif 251 252 if (wsfont_lock(cookie, &ri->ri_font, 253 WSDISPLAY_FONTORDER_L2R, WSDISPLAY_FONTORDER_L2R) <= 0) { 254 printf("rasops_init: couldn't lock font\n"); 255 return (-1); 256 } 257 258 ri->ri_wsfcookie = cookie; 259 } 260 #endif 261 262 /* This should never happen in reality... */ 263 #ifdef DEBUG 264 if ((long)ri->ri_bits & 3) { 265 printf("rasops_init: bits not aligned on 32-bit boundary\n"); 266 return (-1); 267 } 268 269 if ((int)ri->ri_stride & 3) { 270 printf("rasops_init: stride not aligned on 32-bit boundary\n"); 271 return (-1); 272 } 273 #endif 274 275 if (rasops_reconfig(ri, wantrows, wantcols)) 276 return (-1); 277 278 LIST_INIT(&ri->ri_screens); 279 ri->ri_nscreens = 0; 280 281 ri->ri_putchar = ri->ri_ops.putchar; 282 ri->ri_copycols = ri->ri_ops.copycols; 283 ri->ri_erasecols = ri->ri_ops.erasecols; 284 ri->ri_copyrows = ri->ri_ops.copyrows; 285 ri->ri_eraserows = ri->ri_ops.eraserows; 286 ri->ri_alloc_attr = ri->ri_ops.alloc_attr; 287 288 if (ri->ri_flg & RI_VCONS) { 289 void *cookie; 290 int curx, cury; 291 long attr; 292 293 if (rasops_alloc_screen(ri, &cookie, &curx, &cury, &attr)) 294 return (-1); 295 296 ri->ri_active = cookie; 297 ri->ri_bs = 298 &ri->ri_active->rs_bs[ri->ri_active->rs_dispoffset]; 299 300 ri->ri_ops.cursor = rasops_vcons_cursor; 301 ri->ri_ops.mapchar = rasops_vcons_mapchar; 302 ri->ri_ops.putchar = rasops_vcons_putchar; 303 ri->ri_ops.copycols = rasops_vcons_copycols; 304 ri->ri_ops.erasecols = rasops_vcons_erasecols; 305 ri->ri_ops.copyrows = rasops_vcons_copyrows; 306 ri->ri_ops.eraserows = rasops_vcons_eraserows; 307 ri->ri_ops.alloc_attr = rasops_vcons_alloc_attr; 308 ri->ri_ops.unpack_attr = rasops_vcons_unpack_attr; 309 ri->ri_do_cursor = rasops_wronly_do_cursor; 310 } else if ((ri->ri_flg & RI_WRONLY) && ri->ri_bs != NULL) { 311 long attr; 312 int i; 313 314 ri->ri_ops.putchar = rasops_wronly_putchar; 315 ri->ri_ops.copycols = rasops_wronly_copycols; 316 ri->ri_ops.erasecols = rasops_wronly_erasecols; 317 ri->ri_ops.copyrows = rasops_wronly_copyrows; 318 ri->ri_ops.eraserows = rasops_wronly_eraserows; 319 ri->ri_do_cursor = rasops_wronly_do_cursor; 320 321 if (ri->ri_flg & RI_CLEAR) { 322 ri->ri_alloc_attr(ri, 0, 0, 0, &attr); 323 for (i = 0; i < ri->ri_rows * ri->ri_cols; i++) { 324 ri->ri_bs[i].uc = ' '; 325 ri->ri_bs[i].attr = attr; 326 } 327 } 328 } 329 330 task_set(&ri->ri_switchtask, rasops_doswitch, ri); 331 332 rasops_init_devcmap(ri); 333 return (0); 334 } 335 336 /* 337 * Reconfigure (because parameters have changed in some way). 338 */ 339 int 340 rasops_reconfig(struct rasops_info *ri, int wantrows, int wantcols) 341 { 342 int l, bpp, s; 343 344 s = splhigh(); 345 346 if (ri->ri_font->fontwidth > 32 || ri->ri_font->fontwidth < 4) 347 panic("rasops_init: fontwidth assumptions botched!"); 348 349 /* Need this to frob the setup below */ 350 bpp = (ri->ri_depth == 15 ? 16 : ri->ri_depth); 351 352 if ((ri->ri_flg & RI_CFGDONE) != 0) 353 ri->ri_bits = ri->ri_origbits; 354 355 /* Don't care if the caller wants a hideously small console */ 356 if (wantrows < 10) 357 wantrows = 10; 358 359 if (wantcols < 20) 360 wantcols = 20; 361 362 /* Now constrain what they get */ 363 ri->ri_emuwidth = ri->ri_font->fontwidth * wantcols; 364 ri->ri_emuheight = ri->ri_font->fontheight * wantrows; 365 366 if (ri->ri_emuwidth > ri->ri_width) 367 ri->ri_emuwidth = ri->ri_width; 368 369 if (ri->ri_emuheight > ri->ri_height) 370 ri->ri_emuheight = ri->ri_height; 371 372 /* Reduce width until aligned on a 32-bit boundary */ 373 while ((ri->ri_emuwidth * bpp & 31) != 0) 374 ri->ri_emuwidth--; 375 376 #if NRASOPS_ROTATION > 0 377 if (ri->ri_flg & (RI_ROTATE_CW | RI_ROTATE_CCW)) { 378 ri->ri_rows = ri->ri_emuwidth / ri->ri_font->fontwidth; 379 ri->ri_cols = ri->ri_emuheight / ri->ri_font->fontheight; 380 } else 381 #endif 382 { 383 ri->ri_cols = ri->ri_emuwidth / ri->ri_font->fontwidth; 384 ri->ri_rows = ri->ri_emuheight / ri->ri_font->fontheight; 385 } 386 ri->ri_emustride = ri->ri_emuwidth * bpp >> 3; 387 ri->ri_delta = ri->ri_stride - ri->ri_emustride; 388 ri->ri_ccol = 0; 389 ri->ri_crow = 0; 390 ri->ri_pelbytes = bpp >> 3; 391 392 ri->ri_xscale = (ri->ri_font->fontwidth * bpp) >> 3; 393 ri->ri_yscale = ri->ri_font->fontheight * ri->ri_stride; 394 ri->ri_fontscale = ri->ri_font->fontheight * ri->ri_font->stride; 395 396 #ifdef DEBUG 397 if ((ri->ri_delta & 3) != 0) 398 panic("rasops_init: ri_delta not aligned on 32-bit boundary"); 399 #endif 400 /* Clear the entire display */ 401 if ((ri->ri_flg & RI_CLEAR) != 0) { 402 memset(ri->ri_bits, 0, ri->ri_stride * ri->ri_height); 403 ri->ri_flg &= ~RI_CLEARMARGINS; 404 } 405 406 /* Now centre our window if needs be */ 407 ri->ri_origbits = ri->ri_bits; 408 409 if ((ri->ri_flg & RI_CENTER) != 0) { 410 ri->ri_bits += (((ri->ri_width * bpp >> 3) - 411 ri->ri_emustride) >> 1) & ~3; 412 ri->ri_bits += ((ri->ri_height - ri->ri_emuheight) >> 1) * 413 ri->ri_stride; 414 415 ri->ri_yorigin = (int)(ri->ri_bits - ri->ri_origbits) 416 / ri->ri_stride; 417 ri->ri_xorigin = (((int)(ri->ri_bits - ri->ri_origbits) 418 % ri->ri_stride) * 8 / bpp); 419 } else 420 ri->ri_xorigin = ri->ri_yorigin = 0; 421 422 /* Clear the margins */ 423 if ((ri->ri_flg & RI_CLEARMARGINS) != 0) { 424 memset(ri->ri_origbits, 0, ri->ri_bits - ri->ri_origbits); 425 for (l = 0; l < ri->ri_emuheight; l++) 426 memset(ri->ri_bits + ri->ri_emustride + 427 l * ri->ri_stride, 0, 428 ri->ri_stride - ri->ri_emustride); 429 memset(ri->ri_bits + ri->ri_emuheight * ri->ri_stride, 0, 430 (ri->ri_origbits + ri->ri_height * ri->ri_stride) - 431 (ri->ri_bits + ri->ri_emuheight * ri->ri_stride)); 432 } 433 434 /* 435 * Fill in defaults for operations set. XXX this nukes private 436 * routines used by accelerated fb drivers. 437 */ 438 ri->ri_ops.mapchar = rasops_mapchar; 439 ri->ri_ops.copyrows = rasops_copyrows; 440 ri->ri_ops.copycols = rasops_copycols; 441 ri->ri_ops.erasecols = rasops_erasecols; 442 ri->ri_ops.eraserows = rasops_eraserows; 443 ri->ri_ops.cursor = rasops_cursor; 444 ri->ri_ops.unpack_attr = rasops_unpack_attr; 445 ri->ri_do_cursor = rasops_do_cursor; 446 ri->ri_updatecursor = NULL; 447 448 if (ri->ri_depth < 8 || (ri->ri_flg & RI_FORCEMONO) != 0) { 449 ri->ri_ops.alloc_attr = rasops_alloc_mattr; 450 ri->ri_caps = WSSCREEN_UNDERLINE | WSSCREEN_REVERSE; 451 } else { 452 ri->ri_ops.alloc_attr = rasops_alloc_cattr; 453 ri->ri_caps = WSSCREEN_UNDERLINE | WSSCREEN_HILIT | 454 WSSCREEN_WSCOLORS | WSSCREEN_REVERSE; 455 } 456 457 switch (ri->ri_depth) { 458 #if NRASOPS1 > 0 459 case 1: 460 rasops1_init(ri); 461 break; 462 #endif 463 #if NRASOPS4 > 0 464 case 4: 465 rasops4_init(ri); 466 break; 467 #endif 468 #if NRASOPS8 > 0 469 case 8: 470 rasops8_init(ri); 471 break; 472 #endif 473 #if NRASOPS15 > 0 || NRASOPS16 > 0 474 case 15: 475 case 16: 476 rasops15_init(ri); 477 break; 478 #endif 479 #if NRASOPS24 > 0 480 case 24: 481 rasops24_init(ri); 482 break; 483 #endif 484 #if NRASOPS32 > 0 485 case 32: 486 rasops32_init(ri); 487 break; 488 #endif 489 default: 490 ri->ri_flg &= ~RI_CFGDONE; 491 splx(s); 492 return (-1); 493 } 494 495 #if NRASOPS_ROTATION > 0 496 if (ri->ri_flg & (RI_ROTATE_CW | RI_ROTATE_CCW)) { 497 ri->ri_real_ops = ri->ri_ops; 498 ri->ri_ops.copycols = rasops_copycols_rotated; 499 ri->ri_ops.copyrows = rasops_copyrows_rotated; 500 ri->ri_ops.erasecols = rasops_erasecols_rotated; 501 ri->ri_ops.eraserows = rasops_eraserows_rotated; 502 ri->ri_ops.putchar = rasops_putchar_rotated; 503 } 504 #endif 505 506 ri->ri_flg |= RI_CFGDONE; 507 splx(s); 508 return (0); 509 } 510 511 /* 512 * Map a character. 513 */ 514 int 515 rasops_mapchar(void *cookie, int c, u_int *cp) 516 { 517 struct rasops_info *ri; 518 519 ri = (struct rasops_info *)cookie; 520 521 #ifdef DIAGNOSTIC 522 if (ri->ri_font == NULL) 523 panic("rasops_mapchar: no font selected"); 524 #endif 525 if (ri->ri_font->encoding != WSDISPLAY_FONTENC_ISO) { 526 527 if ( (c = wsfont_map_unichar(ri->ri_font, c)) < 0) { 528 529 *cp = '?'; 530 return (0); 531 532 } 533 } 534 535 536 if (c < ri->ri_font->firstchar) { 537 *cp = '?'; 538 return (0); 539 } 540 541 if (c - ri->ri_font->firstchar >= ri->ri_font->numchars) { 542 *cp = '?'; 543 return (0); 544 } 545 546 *cp = c; 547 return (5); 548 } 549 550 /* 551 * Allocate a color attribute. 552 */ 553 int 554 rasops_alloc_cattr(void *cookie, int fg, int bg, int flg, long *attr) 555 { 556 int swap; 557 558 #ifdef RASOPS_CLIPPING 559 fg &= 7; 560 bg &= 7; 561 #endif 562 if ((flg & WSATTR_BLINK) != 0) 563 return (EINVAL); 564 565 if ((flg & WSATTR_WSCOLORS) == 0) { 566 fg = WS_DEFAULT_FG; 567 bg = WS_DEFAULT_BG; 568 } 569 570 if ((flg & WSATTR_REVERSE) != 0) { 571 swap = fg; 572 fg = bg; 573 bg = swap; 574 } 575 576 if ((flg & WSATTR_HILIT) != 0) 577 fg += 8; 578 579 flg = ((flg & WSATTR_UNDERLINE) ? 1 : 0); 580 581 if (rasops_isgray[fg]) 582 flg |= 2; 583 584 if (rasops_isgray[bg]) 585 flg |= 4; 586 587 *attr = (bg << 16) | (fg << 24) | flg; 588 return (0); 589 } 590 591 /* 592 * Allocate a mono attribute. 593 */ 594 int 595 rasops_alloc_mattr(void *cookie, int fg, int bg, int flg, long *attr) 596 { 597 int swap; 598 599 if ((flg & (WSATTR_BLINK | WSATTR_HILIT | WSATTR_WSCOLORS)) != 0) 600 return (EINVAL); 601 602 fg = 1; 603 bg = 0; 604 605 if ((flg & WSATTR_REVERSE) != 0) { 606 swap = fg; 607 fg = bg; 608 bg = swap; 609 } 610 611 *attr = (bg << 16) | (fg << 24) | ((flg & WSATTR_UNDERLINE) ? 7 : 6); 612 return (0); 613 } 614 615 /* 616 * Copy rows. 617 */ 618 int 619 rasops_copyrows(void *cookie, int src, int dst, int num) 620 { 621 int32_t *sp, *dp, *srp, *drp; 622 struct rasops_info *ri; 623 int n8, n1, cnt, delta; 624 625 ri = (struct rasops_info *)cookie; 626 627 #ifdef RASOPS_CLIPPING 628 if (dst == src) 629 return 0; 630 631 if (src < 0) { 632 num += src; 633 src = 0; 634 } 635 636 if ((src + num) > ri->ri_rows) 637 num = ri->ri_rows - src; 638 639 if (dst < 0) { 640 num += dst; 641 dst = 0; 642 } 643 644 if ((dst + num) > ri->ri_rows) 645 num = ri->ri_rows - dst; 646 647 if (num <= 0) 648 return 0; 649 #endif 650 651 num *= ri->ri_font->fontheight; 652 n8 = ri->ri_emustride >> 5; 653 n1 = (ri->ri_emustride >> 2) & 7; 654 655 if (dst < src) { 656 srp = (int32_t *)(ri->ri_bits + src * ri->ri_yscale); 657 drp = (int32_t *)(ri->ri_bits + dst * ri->ri_yscale); 658 delta = ri->ri_stride; 659 } else { 660 src = ri->ri_font->fontheight * src + num - 1; 661 dst = ri->ri_font->fontheight * dst + num - 1; 662 srp = (int32_t *)(ri->ri_bits + src * ri->ri_stride); 663 drp = (int32_t *)(ri->ri_bits + dst * ri->ri_stride); 664 delta = -ri->ri_stride; 665 } 666 667 while (num--) { 668 dp = drp; 669 sp = srp; 670 DELTA(drp, delta, int32_t *); 671 DELTA(srp, delta, int32_t *); 672 673 for (cnt = n8; cnt; cnt--) { 674 dp[0] = sp[0]; 675 dp[1] = sp[1]; 676 dp[2] = sp[2]; 677 dp[3] = sp[3]; 678 dp[4] = sp[4]; 679 dp[5] = sp[5]; 680 dp[6] = sp[6]; 681 dp[7] = sp[7]; 682 dp += 8; 683 sp += 8; 684 } 685 686 for (cnt = n1; cnt; cnt--) 687 *dp++ = *sp++; 688 } 689 690 return 0; 691 } 692 693 /* 694 * Copy columns. This is slow, and hard to optimize due to alignment, 695 * and the fact that we have to copy both left->right and right->left. 696 * We simply cop-out here and use either memmove() or slow_bcopy(), 697 * since they handle all of these cases anyway. 698 */ 699 int 700 rasops_copycols(void *cookie, int row, int src, int dst, int num) 701 { 702 struct rasops_info *ri; 703 u_char *sp, *dp; 704 int height; 705 706 ri = (struct rasops_info *)cookie; 707 708 #ifdef RASOPS_CLIPPING 709 if (dst == src) 710 return 0; 711 712 /* Catches < 0 case too */ 713 if ((unsigned)row >= (unsigned)ri->ri_rows) 714 return 0; 715 716 if (src < 0) { 717 num += src; 718 src = 0; 719 } 720 721 if ((src + num) > ri->ri_cols) 722 num = ri->ri_cols - src; 723 724 if (dst < 0) { 725 num += dst; 726 dst = 0; 727 } 728 729 if ((dst + num) > ri->ri_cols) 730 num = ri->ri_cols - dst; 731 732 if (num <= 0) 733 return 0; 734 #endif 735 736 num *= ri->ri_xscale; 737 row *= ri->ri_yscale; 738 height = ri->ri_font->fontheight; 739 740 sp = ri->ri_bits + row + src * ri->ri_xscale; 741 dp = ri->ri_bits + row + dst * ri->ri_xscale; 742 743 #if NRASOPS_BSWAP > 0 744 if (ri->ri_flg & RI_BSWAP) { 745 while (height--) { 746 slow_bcopy(sp, dp, num); 747 dp += ri->ri_stride; 748 sp += ri->ri_stride; 749 } 750 } else 751 #endif 752 { 753 while (height--) { 754 memmove(dp, sp, num); 755 dp += ri->ri_stride; 756 sp += ri->ri_stride; 757 } 758 } 759 760 return 0; 761 } 762 763 /* 764 * Turn cursor off/on. 765 */ 766 int 767 rasops_cursor(void *cookie, int on, int row, int col) 768 { 769 struct rasops_info *ri; 770 int rc; 771 772 ri = (struct rasops_info *)cookie; 773 774 /* Turn old cursor off */ 775 if ((ri->ri_flg & RI_CURSOR) != 0) { 776 #ifdef RASOPS_CLIPPING 777 if ((ri->ri_flg & RI_CURSORCLIP) == 0) 778 #endif 779 if ((rc = ri->ri_do_cursor(ri)) != 0) 780 return rc; 781 ri->ri_flg &= ~RI_CURSOR; 782 } 783 784 /* Select new cursor */ 785 #ifdef RASOPS_CLIPPING 786 ri->ri_flg &= ~RI_CURSORCLIP; 787 788 if (row < 0 || row >= ri->ri_rows) 789 ri->ri_flg |= RI_CURSORCLIP; 790 else if (col < 0 || col >= ri->ri_cols) 791 ri->ri_flg |= RI_CURSORCLIP; 792 #endif 793 ri->ri_crow = row; 794 ri->ri_ccol = col; 795 796 if (ri->ri_updatecursor != NULL) 797 ri->ri_updatecursor(ri); 798 799 if (on) { 800 #ifdef RASOPS_CLIPPING 801 if ((ri->ri_flg & RI_CURSORCLIP) == 0) 802 #endif 803 if ((rc = ri->ri_do_cursor(ri)) != 0) 804 return rc; 805 ri->ri_flg |= RI_CURSOR; 806 } 807 808 return 0; 809 } 810 811 /* 812 * Make the device colormap 813 */ 814 void 815 rasops_init_devcmap(struct rasops_info *ri) 816 { 817 int i; 818 #if NRASOPS15 > 0 || NRASOPS16 > 0 || NRASOPS24 > 0 || NRASOPS32 > 0 819 const u_char *p; 820 #endif 821 #if NRASOPS4 > 0 || NRASOPS15 > 0 || NRASOPS16 > 0 || NRASOPS24 > 0 || NRASOPS32 > 0 822 int c; 823 #endif 824 825 if (ri->ri_depth == 1 || (ri->ri_flg & RI_FORCEMONO) != 0) { 826 ri->ri_devcmap[0] = 0; 827 for (i = 1; i < 16; i++) 828 ri->ri_devcmap[i] = 0xffffffff; 829 return; 830 } 831 832 switch (ri->ri_depth) { 833 #if NRASOPS4 > 0 834 case 4: 835 for (i = 0; i < 16; i++) { 836 c = i | (i << 4); 837 ri->ri_devcmap[i] = c | (c<<8) | (c<<16) | (c<<24); 838 } 839 return; 840 #endif 841 #if NRASOPS8 > 0 842 case 8: 843 for (i = 0; i < 16; i++) 844 ri->ri_devcmap[i] = i | (i<<8) | (i<<16) | (i<<24); 845 return; 846 #endif 847 default: 848 break; 849 } 850 851 #if NRASOPS15 > 0 || NRASOPS16 > 0 || NRASOPS24 > 0 || NRASOPS32 > 0 852 p = rasops_cmap; 853 854 for (i = 0; i < 16; i++) { 855 if (ri->ri_rnum <= 8) 856 c = (*p >> (8 - ri->ri_rnum)) << ri->ri_rpos; 857 else 858 c = (*p << (ri->ri_rnum - 8)) << ri->ri_rpos; 859 p++; 860 861 if (ri->ri_gnum <= 8) 862 c |= (*p >> (8 - ri->ri_gnum)) << ri->ri_gpos; 863 else 864 c |= (*p << (ri->ri_gnum - 8)) << ri->ri_gpos; 865 p++; 866 867 if (ri->ri_bnum <= 8) 868 c |= (*p >> (8 - ri->ri_bnum)) << ri->ri_bpos; 869 else 870 c |= (*p << (ri->ri_bnum - 8)) << ri->ri_bpos; 871 p++; 872 873 /* Fill the word for generic routines, which want this */ 874 if (ri->ri_depth == 24) 875 c = c | ((c & 0xff) << 24); 876 else if (ri->ri_depth <= 16) 877 c = c | (c << 16); 878 879 /* 24bpp does bswap on the fly. {32,16,15}bpp do it here. */ 880 #if NRASOPS_BSWAP > 0 881 if ((ri->ri_flg & RI_BSWAP) == 0) 882 ri->ri_devcmap[i] = c; 883 else if (ri->ri_depth == 32) 884 ri->ri_devcmap[i] = swap32(c); 885 else if (ri->ri_depth == 16 || ri->ri_depth == 15) 886 ri->ri_devcmap[i] = swap16(c); 887 else 888 ri->ri_devcmap[i] = c; 889 #else 890 ri->ri_devcmap[i] = c; 891 #endif 892 } 893 #endif 894 } 895 896 /* 897 * Unpack a rasops attribute 898 */ 899 void 900 rasops_unpack_attr(void *cookie, long attr, int *fg, int *bg, int *underline) 901 { 902 *fg = ((u_int)attr >> 24) & 0xf; 903 *bg = ((u_int)attr >> 16) & 0xf; 904 if (underline != NULL) 905 *underline = (u_int)attr & 1; 906 } 907 908 /* 909 * Erase rows 910 */ 911 int 912 rasops_eraserows(void *cookie, int row, int num, long attr) 913 { 914 struct rasops_info *ri; 915 int np, nw, cnt, delta; 916 int32_t *dp, clr; 917 918 ri = (struct rasops_info *)cookie; 919 920 #ifdef RASOPS_CLIPPING 921 if (row < 0) { 922 num += row; 923 row = 0; 924 } 925 926 if ((row + num) > ri->ri_rows) 927 num = ri->ri_rows - row; 928 929 if (num <= 0) 930 return 0; 931 #endif 932 933 clr = ri->ri_devcmap[(attr >> 16) & 0xf]; 934 935 /* 936 * XXX The wsdisplay_emulops interface seems a little deficient in 937 * that there is no way to clear the *entire* screen. We provide a 938 * workaround here: if the entire console area is being cleared, and 939 * the RI_FULLCLEAR flag is set, clear the entire display. 940 */ 941 if (num == ri->ri_rows && (ri->ri_flg & RI_FULLCLEAR) != 0) { 942 np = ri->ri_stride >> 5; 943 nw = (ri->ri_stride >> 2) & 7; 944 num = ri->ri_height; 945 dp = (int32_t *)ri->ri_origbits; 946 delta = 0; 947 } else { 948 np = ri->ri_emustride >> 5; 949 nw = (ri->ri_emustride >> 2) & 7; 950 num *= ri->ri_font->fontheight; 951 dp = (int32_t *)(ri->ri_bits + row * ri->ri_yscale); 952 delta = ri->ri_delta; 953 } 954 955 while (num--) { 956 for (cnt = np; cnt; cnt--) { 957 dp[0] = clr; 958 dp[1] = clr; 959 dp[2] = clr; 960 dp[3] = clr; 961 dp[4] = clr; 962 dp[5] = clr; 963 dp[6] = clr; 964 dp[7] = clr; 965 dp += 8; 966 } 967 968 for (cnt = nw; cnt; cnt--) { 969 *(int32_t *)dp = clr; 970 DELTA(dp, 4, int32_t *); 971 } 972 973 DELTA(dp, delta, int32_t *); 974 } 975 976 return 0; 977 } 978 979 /* 980 * Actually turn the cursor on or off. This does the dirty work for 981 * rasops_cursor(). 982 */ 983 int 984 rasops_do_cursor(struct rasops_info *ri) 985 { 986 int full1, height, cnt, slop1, slop2, row, col; 987 u_char *dp, *rp; 988 989 #if NRASOPS_ROTATION > 0 990 if (ri->ri_flg & RI_ROTATE_CW) { 991 /* Rotate rows/columns */ 992 row = ri->ri_ccol; 993 col = ri->ri_rows - ri->ri_crow - 1; 994 } else if (ri->ri_flg & RI_ROTATE_CCW) { 995 /* Rotate rows/columns */ 996 row = ri->ri_cols - ri->ri_ccol - 1; 997 col = ri->ri_crow; 998 } else 999 #endif 1000 { 1001 row = ri->ri_crow; 1002 col = ri->ri_ccol; 1003 } 1004 1005 rp = ri->ri_bits + row * ri->ri_yscale + col * ri->ri_xscale; 1006 height = ri->ri_font->fontheight; 1007 slop1 = (4 - ((long)rp & 3)) & 3; 1008 1009 if (slop1 > ri->ri_xscale) 1010 slop1 = ri->ri_xscale; 1011 1012 slop2 = (ri->ri_xscale - slop1) & 3; 1013 full1 = (ri->ri_xscale - slop1 - slop2) >> 2; 1014 1015 if ((slop1 | slop2) == 0) { 1016 /* A common case */ 1017 while (height--) { 1018 dp = rp; 1019 rp += ri->ri_stride; 1020 1021 for (cnt = full1; cnt; cnt--) { 1022 *(int32_t *)dp ^= ~0; 1023 dp += 4; 1024 } 1025 } 1026 } else { 1027 /* XXX this is stupid.. use masks instead */ 1028 while (height--) { 1029 dp = rp; 1030 rp += ri->ri_stride; 1031 1032 if (slop1 & 1) 1033 *dp++ ^= ~0; 1034 1035 if (slop1 & 2) { 1036 *(int16_t *)dp ^= ~0; 1037 dp += 2; 1038 } 1039 1040 for (cnt = full1; cnt; cnt--) { 1041 *(int32_t *)dp ^= ~0; 1042 dp += 4; 1043 } 1044 1045 if (slop2 & 1) 1046 *dp++ ^= ~0; 1047 1048 if (slop2 & 2) 1049 *(int16_t *)dp ^= ~0; 1050 } 1051 } 1052 1053 return 0; 1054 } 1055 1056 /* 1057 * Erase columns. 1058 */ 1059 int 1060 rasops_erasecols(void *cookie, int row, int col, int num, long attr) 1061 { 1062 int n8, height, cnt, slop1, slop2, clr; 1063 struct rasops_info *ri; 1064 int32_t *rp, *dp; 1065 1066 ri = (struct rasops_info *)cookie; 1067 1068 #ifdef RASOPS_CLIPPING 1069 if ((unsigned)row >= (unsigned)ri->ri_rows) 1070 return 0; 1071 1072 if (col < 0) { 1073 num += col; 1074 col = 0; 1075 } 1076 1077 if ((col + num) > ri->ri_cols) 1078 num = ri->ri_cols - col; 1079 1080 if (num <= 0) 1081 return 0; 1082 #endif 1083 1084 num = num * ri->ri_xscale; 1085 rp = (int32_t *)(ri->ri_bits + row*ri->ri_yscale + col*ri->ri_xscale); 1086 height = ri->ri_font->fontheight; 1087 clr = ri->ri_devcmap[(attr >> 16) & 0xf]; 1088 1089 /* Don't bother using the full loop for <= 32 pels */ 1090 if (num <= 32) { 1091 if (((num | ri->ri_xscale) & 3) == 0) { 1092 /* Word aligned blt */ 1093 num >>= 2; 1094 1095 while (height--) { 1096 dp = rp; 1097 DELTA(rp, ri->ri_stride, int32_t *); 1098 1099 for (cnt = num; cnt; cnt--) 1100 *dp++ = clr; 1101 } 1102 } else if (((num | ri->ri_xscale) & 1) == 0) { 1103 /* 1104 * Halfword aligned blt. This is needed so the 1105 * 15/16 bit ops can use this function. 1106 */ 1107 num >>= 1; 1108 1109 while (height--) { 1110 dp = rp; 1111 DELTA(rp, ri->ri_stride, int32_t *); 1112 1113 for (cnt = num; cnt; cnt--) { 1114 *(int16_t *)dp = clr; 1115 DELTA(dp, 2, int32_t *); 1116 } 1117 } 1118 } else { 1119 while (height--) { 1120 dp = rp; 1121 DELTA(rp, ri->ri_stride, int32_t *); 1122 1123 for (cnt = num; cnt; cnt--) { 1124 *(u_char *)dp = clr; 1125 DELTA(dp, 1, int32_t *); 1126 } 1127 } 1128 } 1129 1130 return 0; 1131 } 1132 1133 slop1 = (4 - ((long)rp & 3)) & 3; 1134 slop2 = (num - slop1) & 3; 1135 num -= slop1 + slop2; 1136 n8 = num >> 5; 1137 num = (num >> 2) & 7; 1138 1139 while (height--) { 1140 dp = rp; 1141 DELTA(rp, ri->ri_stride, int32_t *); 1142 1143 /* Align span to 4 bytes */ 1144 if (slop1 & 1) { 1145 *(u_char *)dp = clr; 1146 DELTA(dp, 1, int32_t *); 1147 } 1148 1149 if (slop1 & 2) { 1150 *(int16_t *)dp = clr; 1151 DELTA(dp, 2, int32_t *); 1152 } 1153 1154 /* Write 32 bytes per loop */ 1155 for (cnt = n8; cnt; cnt--) { 1156 dp[0] = clr; 1157 dp[1] = clr; 1158 dp[2] = clr; 1159 dp[3] = clr; 1160 dp[4] = clr; 1161 dp[5] = clr; 1162 dp[6] = clr; 1163 dp[7] = clr; 1164 dp += 8; 1165 } 1166 1167 /* Write 4 bytes per loop */ 1168 for (cnt = num; cnt; cnt--) 1169 *dp++ = clr; 1170 1171 /* Write unaligned trailing slop */ 1172 if (slop2 & 1) { 1173 *(u_char *)dp = clr; 1174 DELTA(dp, 1, int32_t *); 1175 } 1176 1177 if (slop2 & 2) 1178 *(int16_t *)dp = clr; 1179 } 1180 1181 return 0; 1182 } 1183 1184 #if NRASOPS_ROTATION > 0 1185 /* 1186 * Quarter clockwise rotation routines (originally intended for the 1187 * built-in Zaurus C3x00 display in 16bpp). 1188 */ 1189 1190 #include <sys/malloc.h> 1191 1192 void 1193 rasops_rotate_font(int *cookie, int ccw) 1194 { 1195 struct rotatedfont *f; 1196 int ncookie; 1197 1198 SLIST_FOREACH(f, &rotatedfonts, rf_next) { 1199 if (f->rf_cookie == *cookie) { 1200 *cookie = f->rf_rotated; 1201 return; 1202 } 1203 } 1204 1205 /* 1206 * We did not find a rotated version of this font. Ask the wsfont 1207 * code to compute one for us. 1208 */ 1209 if ((ncookie = wsfont_rotate(*cookie, ccw)) == -1) 1210 return; 1211 1212 f = malloc(sizeof(struct rotatedfont), M_DEVBUF, M_WAITOK); 1213 f->rf_cookie = *cookie; 1214 f->rf_rotated = ncookie; 1215 SLIST_INSERT_HEAD(&rotatedfonts, f, rf_next); 1216 1217 *cookie = ncookie; 1218 } 1219 1220 void 1221 rasops_copychar(void *cookie, int srcrow, int dstrow, int srccol, int dstcol) 1222 { 1223 struct rasops_info *ri; 1224 u_char *sp, *dp; 1225 int height; 1226 int r_srcrow, r_dstrow, r_srccol, r_dstcol; 1227 1228 ri = (struct rasops_info *)cookie; 1229 1230 r_srcrow = srccol; 1231 r_dstrow = dstcol; 1232 r_srccol = ri->ri_rows - srcrow - 1; 1233 r_dstcol = ri->ri_rows - dstrow - 1; 1234 1235 r_srcrow *= ri->ri_yscale; 1236 r_dstrow *= ri->ri_yscale; 1237 height = ri->ri_font->fontheight; 1238 1239 sp = ri->ri_bits + r_srcrow + r_srccol * ri->ri_xscale; 1240 dp = ri->ri_bits + r_dstrow + r_dstcol * ri->ri_xscale; 1241 1242 #if NRASOPS_BSWAP > 0 1243 if (ri->ri_flg & RI_BSWAP) { 1244 while (height--) { 1245 slow_bcopy(sp, dp, ri->ri_xscale); 1246 dp += ri->ri_stride; 1247 sp += ri->ri_stride; 1248 } 1249 } else 1250 #endif 1251 { 1252 while (height--) { 1253 memmove(dp, sp, ri->ri_xscale); 1254 dp += ri->ri_stride; 1255 sp += ri->ri_stride; 1256 } 1257 } 1258 } 1259 1260 int 1261 rasops_putchar_rotated(void *cookie, int row, int col, u_int uc, long attr) 1262 { 1263 struct rasops_info *ri; 1264 u_char *rp; 1265 int height; 1266 int rc; 1267 1268 ri = (struct rasops_info *)cookie; 1269 1270 if (ri->ri_flg & RI_ROTATE_CW) 1271 row = ri->ri_rows - row - 1; 1272 else 1273 col = ri->ri_cols - col - 1; 1274 1275 /* Do rotated char sans (side)underline */ 1276 rc = ri->ri_real_ops.putchar(cookie, col, row, uc, attr & ~1); 1277 if (rc != 0) 1278 return rc; 1279 1280 /* Do rotated underline */ 1281 rp = ri->ri_bits + col * ri->ri_yscale + row * ri->ri_xscale; 1282 if (ri->ri_flg & RI_ROTATE_CCW) 1283 rp += (ri->ri_font->fontwidth - 1) * ri->ri_pelbytes; 1284 height = ri->ri_font->fontheight; 1285 1286 /* XXX this assumes 16-bit color depth */ 1287 if ((attr & 1) != 0) { 1288 int16_t c = (int16_t)ri->ri_devcmap[((u_int)attr >> 24) & 0xf]; 1289 1290 while (height--) { 1291 *(int16_t *)rp = c; 1292 rp += ri->ri_stride; 1293 } 1294 } 1295 1296 return 0; 1297 } 1298 1299 int 1300 rasops_erasecols_rotated(void *cookie, int row, int col, int num, long attr) 1301 { 1302 int i; 1303 int rc; 1304 1305 for (i = col; i < col + num; i++) { 1306 rc = rasops_putchar_rotated(cookie, row, i, ' ', attr); 1307 if (rc != 0) 1308 return rc; 1309 } 1310 1311 return 0; 1312 } 1313 1314 /* XXX: these could likely be optimised somewhat. */ 1315 int 1316 rasops_copyrows_rotated(void *cookie, int src, int dst, int num) 1317 { 1318 struct rasops_info *ri = (struct rasops_info *)cookie; 1319 int col, roff; 1320 1321 if (src > dst) { 1322 for (roff = 0; roff < num; roff++) 1323 for (col = 0; col < ri->ri_cols; col++) 1324 rasops_copychar(cookie, src + roff, dst + roff, 1325 col, col); 1326 } else { 1327 for (roff = num - 1; roff >= 0; roff--) 1328 for (col = 0; col < ri->ri_cols; col++) 1329 rasops_copychar(cookie, src + roff, dst + roff, 1330 col, col); 1331 } 1332 1333 return 0; 1334 } 1335 1336 int 1337 rasops_copycols_rotated(void *cookie, int row, int src, int dst, int num) 1338 { 1339 int coff; 1340 1341 if (src > dst) { 1342 for (coff = 0; coff < num; coff++) 1343 rasops_copychar(cookie, row, row, src + coff, 1344 dst + coff); 1345 } else { 1346 for (coff = num - 1; coff >= 0; coff--) 1347 rasops_copychar(cookie, row, row, src + coff, 1348 dst + coff); 1349 } 1350 1351 return 0; 1352 } 1353 1354 int 1355 rasops_eraserows_rotated(void *cookie, int row, int num, long attr) 1356 { 1357 struct rasops_info *ri; 1358 int col, rn; 1359 int rc; 1360 1361 ri = (struct rasops_info *)cookie; 1362 1363 for (rn = row; rn < row + num; rn++) 1364 for (col = 0; col < ri->ri_cols; col++) { 1365 rc = rasops_putchar_rotated(cookie, rn, col, ' ', attr); 1366 if (rc != 0) 1367 return rc; 1368 } 1369 1370 return 0; 1371 } 1372 #endif /* NRASOPS_ROTATION */ 1373 1374 #if NRASOPS_BSWAP > 0 1375 /* 1376 * Strictly byte-only bcopy() version, to be used with RI_BSWAP, as the 1377 * regular bcopy() may want to optimize things by doing larger-than-byte 1378 * reads or write. This may confuse things if src and dst have different 1379 * alignments. 1380 */ 1381 void 1382 slow_bcopy(void *s, void *d, size_t len) 1383 { 1384 u_int8_t *src = s; 1385 u_int8_t *dst = d; 1386 1387 if ((vaddr_t)dst <= (vaddr_t)src) { 1388 while (len-- != 0) 1389 *dst++ = *src++; 1390 } else { 1391 src += len; 1392 dst += len; 1393 if (len != 0) 1394 while (--len != 0) 1395 *--dst = *--src; 1396 } 1397 } 1398 #endif /* NRASOPS_BSWAP */ 1399 1400 int 1401 rasops_alloc_screen(void *v, void **cookiep, 1402 int *curxp, int *curyp, long *attrp) 1403 { 1404 struct rasops_info *ri = v; 1405 struct rasops_screen *scr; 1406 int i; 1407 1408 scr = malloc(sizeof(*scr), M_DEVBUF, M_NOWAIT); 1409 if (scr == NULL) 1410 return (ENOMEM); 1411 1412 scr->rs_sbscreens = RS_SCROLLBACK_SCREENS; 1413 scr->rs_bs = mallocarray(ri->ri_rows * (scr->rs_sbscreens + 1), 1414 ri->ri_cols * sizeof(struct wsdisplay_charcell), M_DEVBUF, 1415 M_NOWAIT); 1416 if (scr->rs_bs == NULL) { 1417 free(scr, M_DEVBUF, sizeof(*scr)); 1418 return (ENOMEM); 1419 } 1420 scr->rs_visibleoffset = scr->rs_dispoffset = ri->ri_rows * 1421 scr->rs_sbscreens * ri->ri_cols; 1422 1423 *cookiep = scr; 1424 *curxp = 0; 1425 *curyp = 0; 1426 ri->ri_alloc_attr(ri, 0, 0, 0, attrp); 1427 1428 scr->rs_ri = ri; 1429 scr->rs_visible = (ri->ri_nscreens == 0); 1430 scr->rs_crow = -1; 1431 scr->rs_ccol = -1; 1432 scr->rs_defattr = *attrp; 1433 1434 for (i = 0; i < scr->rs_dispoffset; i++) { 1435 scr->rs_bs[i].uc = ' '; 1436 scr->rs_bs[i].attr = scr->rs_defattr; 1437 } 1438 1439 if (ri->ri_bs && scr->rs_visible) { 1440 memcpy(scr->rs_bs + scr->rs_dispoffset, ri->ri_bs, 1441 ri->ri_rows * ri->ri_cols * 1442 sizeof(struct wsdisplay_charcell)); 1443 } else { 1444 for (i = 0; i < ri->ri_rows * ri->ri_cols; i++) { 1445 scr->rs_bs[scr->rs_dispoffset + i].uc = ' '; 1446 scr->rs_bs[scr->rs_dispoffset + i].attr = 1447 scr->rs_defattr; 1448 } 1449 } 1450 1451 LIST_INSERT_HEAD(&ri->ri_screens, scr, rs_next); 1452 ri->ri_nscreens++; 1453 1454 return (0); 1455 } 1456 1457 void 1458 rasops_free_screen(void *v, void *cookie) 1459 { 1460 struct rasops_info *ri = v; 1461 struct rasops_screen *scr = cookie; 1462 1463 LIST_REMOVE(scr, rs_next); 1464 ri->ri_nscreens--; 1465 1466 free(scr->rs_bs, M_DEVBUF, 1467 ri->ri_rows * (scr->rs_sbscreens + 1) * ri->ri_cols * 1468 sizeof(struct wsdisplay_charcell)); 1469 free(scr, M_DEVBUF, sizeof(*scr)); 1470 } 1471 1472 int 1473 rasops_show_screen(void *v, void *cookie, int waitok, 1474 void (*cb)(void *, int, int), void *cbarg) 1475 { 1476 struct rasops_info *ri = v; 1477 1478 ri->ri_switchcookie = cookie; 1479 if (cb) { 1480 ri->ri_switchcb = cb; 1481 ri->ri_switchcbarg = cbarg; 1482 task_add(systq, &ri->ri_switchtask); 1483 return (EAGAIN); 1484 } 1485 1486 rasops_doswitch(ri); 1487 return (0); 1488 } 1489 1490 void 1491 rasops_doswitch(void *v) 1492 { 1493 struct rasops_info *ri = v; 1494 struct rasops_screen *scr = ri->ri_switchcookie; 1495 int row, col; 1496 1497 rasops_cursor(ri, 0, 0, 0); 1498 ri->ri_active->rs_visible = 0; 1499 ri->ri_eraserows(ri, 0, ri->ri_rows, scr->rs_defattr); 1500 ri->ri_active = scr; 1501 ri->ri_bs = &ri->ri_active->rs_bs[ri->ri_active->rs_dispoffset]; 1502 ri->ri_active->rs_visible = 1; 1503 ri->ri_active->rs_visibleoffset = ri->ri_active->rs_dispoffset; 1504 for (row = 0; row < ri->ri_rows; row++) { 1505 for (col = 0; col < ri->ri_cols; col++) { 1506 int off = row * scr->rs_ri->ri_cols + col + 1507 scr->rs_visibleoffset; 1508 1509 ri->ri_putchar(ri, row, col, scr->rs_bs[off].uc, 1510 scr->rs_bs[off].attr); 1511 } 1512 } 1513 if (scr->rs_crow != -1) 1514 rasops_cursor(ri, 1, scr->rs_crow, scr->rs_ccol); 1515 1516 if (ri->ri_switchcb) 1517 (*ri->ri_switchcb)(ri->ri_switchcbarg, 0, 0); 1518 } 1519 1520 int 1521 rasops_getchar(void *v, int row, int col, struct wsdisplay_charcell *cell) 1522 { 1523 struct rasops_info *ri = v; 1524 struct rasops_screen *scr = ri->ri_active; 1525 1526 if (scr == NULL || scr->rs_bs == NULL) 1527 return (1); 1528 1529 *cell = scr->rs_bs[row * ri->ri_cols + col + scr->rs_dispoffset]; 1530 return (0); 1531 } 1532 1533 int 1534 rasops_vcons_cursor(void *cookie, int on, int row, int col) 1535 { 1536 struct rasops_screen *scr = cookie; 1537 1538 scr->rs_crow = on ? row : -1; 1539 scr->rs_ccol = on ? col : -1; 1540 1541 if (!scr->rs_visible) 1542 return 0; 1543 1544 return rasops_cursor(scr->rs_ri, on, row, col); 1545 } 1546 1547 int 1548 rasops_vcons_mapchar(void *cookie, int c, u_int *cp) 1549 { 1550 struct rasops_screen *scr = cookie; 1551 1552 return rasops_mapchar(scr->rs_ri, c, cp); 1553 } 1554 1555 int 1556 rasops_vcons_putchar(void *cookie, int row, int col, u_int uc, long attr) 1557 { 1558 struct rasops_screen *scr = cookie; 1559 int off = row * scr->rs_ri->ri_cols + col + scr->rs_dispoffset; 1560 1561 if (scr->rs_visible && scr->rs_visibleoffset != scr->rs_dispoffset) 1562 rasops_scrollback(scr->rs_ri, scr, 0); 1563 1564 scr->rs_bs[off].uc = uc; 1565 scr->rs_bs[off].attr = attr; 1566 1567 if (!scr->rs_visible) 1568 return 0; 1569 1570 return scr->rs_ri->ri_putchar(scr->rs_ri, row, col, uc, attr); 1571 } 1572 1573 int 1574 rasops_vcons_copycols(void *cookie, int row, int src, int dst, int num) 1575 { 1576 struct rasops_screen *scr = cookie; 1577 struct rasops_info *ri = scr->rs_ri; 1578 int cols = scr->rs_ri->ri_cols; 1579 int col, rc; 1580 1581 memmove(&scr->rs_bs[row * cols + dst + scr->rs_dispoffset], 1582 &scr->rs_bs[row * cols + src + scr->rs_dispoffset], 1583 num * sizeof(struct wsdisplay_charcell)); 1584 1585 if (!scr->rs_visible) 1586 return 0; 1587 1588 if ((ri->ri_flg & RI_WRONLY) == 0) 1589 return ri->ri_copycols(ri, row, src, dst, num); 1590 1591 for (col = dst; col < dst + num; col++) { 1592 int off = row * cols + col + scr->rs_dispoffset; 1593 1594 rc = ri->ri_putchar(ri, row, col, 1595 scr->rs_bs[off].uc, scr->rs_bs[off].attr); 1596 if (rc != 0) 1597 return rc; 1598 } 1599 1600 return 0; 1601 } 1602 1603 int 1604 rasops_vcons_erasecols(void *cookie, int row, int col, int num, long attr) 1605 { 1606 struct rasops_screen *scr = cookie; 1607 int cols = scr->rs_ri->ri_cols; 1608 int i; 1609 1610 for (i = 0; i < num; i++) { 1611 int off = row * cols + col + i + scr->rs_dispoffset; 1612 1613 scr->rs_bs[off].uc = ' '; 1614 scr->rs_bs[off].attr = attr; 1615 } 1616 1617 if (!scr->rs_visible) 1618 return 0; 1619 1620 return scr->rs_ri->ri_erasecols(scr->rs_ri, row, col, num, attr); 1621 } 1622 1623 int 1624 rasops_vcons_copyrows(void *cookie, int src, int dst, int num) 1625 { 1626 struct rasops_screen *scr = cookie; 1627 struct rasops_info *ri = scr->rs_ri; 1628 int cols = ri->ri_cols; 1629 int row, col, rc; 1630 1631 if (dst == 0 && (src + num == ri->ri_rows) && scr->rs_sbscreens > 0) 1632 memmove(&scr->rs_bs[dst], &scr->rs_bs[src * cols], 1633 ((ri->ri_rows * (scr->rs_sbscreens + 1) * cols) - 1634 (src * cols)) * sizeof(struct wsdisplay_charcell)); 1635 else 1636 memmove(&scr->rs_bs[dst * cols + scr->rs_dispoffset], 1637 &scr->rs_bs[src * cols + scr->rs_dispoffset], 1638 num * cols * sizeof(struct wsdisplay_charcell)); 1639 1640 if (!scr->rs_visible) 1641 return 0; 1642 1643 if ((ri->ri_flg & RI_WRONLY) == 0) 1644 return ri->ri_copyrows(ri, src, dst, num); 1645 1646 for (row = dst; row < dst + num; row++) { 1647 for (col = 0; col < cols; col++) { 1648 int off = row * cols + col + scr->rs_dispoffset; 1649 1650 rc = ri->ri_putchar(ri, row, col, 1651 scr->rs_bs[off].uc, scr->rs_bs[off].attr); 1652 if (rc != 0) 1653 return rc; 1654 } 1655 } 1656 1657 return 0; 1658 } 1659 1660 int 1661 rasops_vcons_eraserows(void *cookie, int row, int num, long attr) 1662 { 1663 struct rasops_screen *scr = cookie; 1664 int cols = scr->rs_ri->ri_cols; 1665 int i; 1666 1667 for (i = 0; i < num * cols; i++) { 1668 int off = row * cols + i + scr->rs_dispoffset; 1669 1670 scr->rs_bs[off].uc = ' '; 1671 scr->rs_bs[off].attr = attr; 1672 } 1673 1674 if (!scr->rs_visible) 1675 return 0; 1676 1677 return scr->rs_ri->ri_eraserows(scr->rs_ri, row, num, attr); 1678 } 1679 1680 int 1681 rasops_vcons_alloc_attr(void *cookie, int fg, int bg, int flg, long *attr) 1682 { 1683 struct rasops_screen *scr = cookie; 1684 1685 return scr->rs_ri->ri_alloc_attr(scr->rs_ri, fg, bg, flg, attr); 1686 } 1687 1688 void 1689 rasops_vcons_unpack_attr(void *cookie, long attr, int *fg, int *bg, 1690 int *underline) 1691 { 1692 struct rasops_screen *scr = cookie; 1693 1694 rasops_unpack_attr(scr->rs_ri, attr, fg, bg, underline); 1695 } 1696 1697 int 1698 rasops_wronly_putchar(void *cookie, int row, int col, u_int uc, long attr) 1699 { 1700 struct rasops_info *ri = cookie; 1701 int off = row * ri->ri_cols + col; 1702 1703 ri->ri_bs[off].uc = uc; 1704 ri->ri_bs[off].attr = attr; 1705 1706 return ri->ri_putchar(ri, row, col, uc, attr); 1707 } 1708 1709 int 1710 rasops_wronly_copycols(void *cookie, int row, int src, int dst, int num) 1711 { 1712 struct rasops_info *ri = cookie; 1713 int cols = ri->ri_cols; 1714 int col, rc; 1715 1716 memmove(&ri->ri_bs[row * cols + dst], &ri->ri_bs[row * cols + src], 1717 num * sizeof(struct wsdisplay_charcell)); 1718 1719 for (col = dst; col < dst + num; col++) { 1720 int off = row * cols + col; 1721 1722 rc = ri->ri_putchar(ri, row, col, 1723 ri->ri_bs[off].uc, ri->ri_bs[off].attr); 1724 if (rc != 0) 1725 return rc; 1726 } 1727 1728 return 0; 1729 } 1730 1731 int 1732 rasops_wronly_erasecols(void *cookie, int row, int col, int num, long attr) 1733 { 1734 struct rasops_info *ri = cookie; 1735 int cols = ri->ri_cols; 1736 int i; 1737 1738 for (i = 0; i < num; i++) { 1739 int off = row * cols + col + i; 1740 1741 ri->ri_bs[off].uc = ' '; 1742 ri->ri_bs[off].attr = attr; 1743 } 1744 1745 return ri->ri_erasecols(ri, row, col, num, attr); 1746 } 1747 1748 int 1749 rasops_wronly_copyrows(void *cookie, int src, int dst, int num) 1750 { 1751 struct rasops_info *ri = cookie; 1752 int cols = ri->ri_cols; 1753 int row, col, rc; 1754 1755 memmove(&ri->ri_bs[dst * cols], &ri->ri_bs[src * cols], 1756 num * cols * sizeof(struct wsdisplay_charcell)); 1757 1758 for (row = dst; row < dst + num; row++) { 1759 for (col = 0; col < cols; col++) { 1760 int off = row * cols + col; 1761 1762 rc = ri->ri_putchar(ri, row, col, 1763 ri->ri_bs[off].uc, ri->ri_bs[off].attr); 1764 if (rc != 0) 1765 return rc; 1766 } 1767 } 1768 1769 return 0; 1770 } 1771 1772 int 1773 rasops_wronly_eraserows(void *cookie, int row, int num, long attr) 1774 { 1775 struct rasops_info *ri = cookie; 1776 int cols = ri->ri_cols; 1777 int i; 1778 1779 for (i = 0; i < num * cols; i++) { 1780 int off = row * cols + i; 1781 1782 ri->ri_bs[off].uc = ' '; 1783 ri->ri_bs[off].attr = attr; 1784 } 1785 1786 return ri->ri_eraserows(ri, row, num, attr); 1787 } 1788 1789 int 1790 rasops_wronly_do_cursor(struct rasops_info *ri) 1791 { 1792 int off = ri->ri_crow * ri->ri_cols + ri->ri_ccol; 1793 u_int uc; 1794 long attr; 1795 int fg, bg; 1796 1797 uc = ri->ri_bs[off].uc; 1798 attr = ri->ri_bs[off].attr; 1799 1800 if ((ri->ri_flg & RI_CURSOR) == 0) { 1801 fg = ((u_int)attr >> 24) & 0xf; 1802 bg = ((u_int)attr >> 16) & 0xf; 1803 attr &= ~0x0ffff0000; 1804 attr |= (fg << 16) | (bg << 24); 1805 } 1806 1807 return ri->ri_putchar(ri, ri->ri_crow, ri->ri_ccol, uc, attr); 1808 } 1809 1810 /* 1811 * Font management. 1812 * 1813 * Fonts usable on raster frame buffers are managed by wsfont, and are not 1814 * tied to any particular display. 1815 */ 1816 1817 int 1818 rasops_add_font(struct rasops_info *ri, struct wsdisplay_font *font) 1819 { 1820 /* only accept matching metrics */ 1821 if (font->fontwidth != ri->ri_font->fontwidth || 1822 font->fontheight != ri->ri_font->fontheight) 1823 return EINVAL; 1824 1825 /* for raster consoles, only accept ISO Latin-1 or Unicode encoding */ 1826 if (font->encoding != WSDISPLAY_FONTENC_ISO) 1827 return EINVAL; 1828 1829 if (wsfont_add(font, 1) != 0) 1830 return EEXIST; /* name collision */ 1831 1832 font->index = -1; /* do not store in wsdisplay_softc */ 1833 1834 return 0; 1835 } 1836 1837 int 1838 rasops_use_font(struct rasops_info *ri, struct wsdisplay_font *font) 1839 { 1840 int wsfcookie; 1841 struct wsdisplay_font *wsf; 1842 const char *name; 1843 1844 /* allow an empty font name to revert to the initial font choice */ 1845 name = font->name; 1846 if (*name == '\0') 1847 name = NULL; 1848 1849 wsfcookie = wsfont_find(name, ri->ri_font->fontwidth, 1850 ri->ri_font->fontheight, 0); 1851 if (wsfcookie < 0) { 1852 wsfcookie = wsfont_find(name, 0, 0, 0); 1853 if (wsfcookie < 0) 1854 return ENOENT; /* font exist, but different metrics */ 1855 else 1856 return EINVAL; 1857 } 1858 if (wsfont_lock(wsfcookie, &wsf, WSDISPLAY_FONTORDER_KNOWN, 1859 WSDISPLAY_FONTORDER_KNOWN) < 0) 1860 return EINVAL; 1861 1862 /* if (ri->ri_wsfcookie >= 0) */ 1863 wsfont_unlock(ri->ri_wsfcookie); 1864 ri->ri_wsfcookie = wsfcookie; 1865 ri->ri_font = wsf; 1866 ri->ri_fontscale = ri->ri_font->fontheight * ri->ri_font->stride; 1867 1868 return 0; 1869 } 1870 1871 int 1872 rasops_load_font(void *v, void *cookie, struct wsdisplay_font *font) 1873 { 1874 struct rasops_info *ri = v; 1875 1876 /* 1877 * For now, we want to only allow loading fonts of the same 1878 * metrics as the currently in-use font. This requires the 1879 * rasops struct to have been correctly configured, and a 1880 * font to have been selected. 1881 */ 1882 if ((ri->ri_flg & RI_CFGDONE) == 0 || ri->ri_font == NULL) 1883 return EINVAL; 1884 1885 if (font->data != NULL) 1886 return rasops_add_font(ri, font); 1887 else 1888 return rasops_use_font(ri, font); 1889 } 1890 1891 struct rasops_list_font_ctx { 1892 struct rasops_info *ri; 1893 int cnt; 1894 struct wsdisplay_font *font; 1895 }; 1896 1897 int 1898 rasops_list_font_cb(void *cbarg, struct wsdisplay_font *font) 1899 { 1900 struct rasops_list_font_ctx *ctx = cbarg; 1901 1902 if (ctx->cnt-- == 0) { 1903 ctx->font = font; 1904 return 1; 1905 } 1906 1907 return 0; 1908 } 1909 1910 int 1911 rasops_list_font(void *v, struct wsdisplay_font *font) 1912 { 1913 struct rasops_info *ri = v; 1914 struct rasops_list_font_ctx ctx; 1915 int idx; 1916 1917 if ((ri->ri_flg & RI_CFGDONE) == 0 || ri->ri_font == NULL) 1918 return EINVAL; 1919 1920 if (font->index < 0) 1921 return EINVAL; 1922 1923 ctx.ri = ri; 1924 ctx.cnt = font->index; 1925 ctx.font = NULL; 1926 wsfont_enum(rasops_list_font_cb, &ctx); 1927 1928 if (ctx.font == NULL) 1929 return EINVAL; 1930 1931 idx = font->index; 1932 *font = *ctx.font; /* struct copy */ 1933 font->index = idx; 1934 font->cookie = font->data = NULL; /* don't leak kernel pointers */ 1935 return 0; 1936 } 1937 1938 void 1939 rasops_scrollback(void *v, void *cookie, int lines) 1940 { 1941 struct rasops_info *ri = v; 1942 struct rasops_screen *scr = cookie; 1943 int row, col, oldvoff; 1944 1945 oldvoff = scr->rs_visibleoffset; 1946 1947 if (lines == 0) 1948 scr->rs_visibleoffset = scr->rs_dispoffset; 1949 else { 1950 int off = scr->rs_visibleoffset + (lines * ri->ri_cols); 1951 1952 if (off < 0) 1953 off = 0; 1954 else if (off > scr->rs_dispoffset) 1955 off = scr->rs_dispoffset; 1956 1957 scr->rs_visibleoffset = off; 1958 } 1959 1960 if (scr->rs_visibleoffset == oldvoff) 1961 return; 1962 1963 rasops_cursor(ri, 0, 0, 0); 1964 ri->ri_eraserows(ri, 0, ri->ri_rows, scr->rs_defattr); 1965 for (row = 0; row < ri->ri_rows; row++) { 1966 for (col = 0; col < ri->ri_cols; col++) { 1967 int off = row * scr->rs_ri->ri_cols + col + 1968 scr->rs_visibleoffset; 1969 1970 ri->ri_putchar(ri, row, col, scr->rs_bs[off].uc, 1971 scr->rs_bs[off].attr); 1972 } 1973 } 1974 1975 if (scr->rs_crow != -1 && scr->rs_visibleoffset == scr->rs_dispoffset) 1976 rasops_cursor(ri, 1, scr->rs_crow, scr->rs_ccol); 1977 } 1978 1979 struct rasops_framebuffer { 1980 SLIST_ENTRY(rasops_framebuffer) rf_list; 1981 paddr_t rf_base; 1982 psize_t rf_size; 1983 struct device *rf_dev; 1984 }; 1985 1986 SLIST_HEAD(, rasops_framebuffer) rasops_framebuffers = 1987 SLIST_HEAD_INITIALIZER(&rasops_framebuffers); 1988 1989 void 1990 rasops_claim_framebuffer(paddr_t base, psize_t size, struct device *dev) 1991 { 1992 struct rasops_framebuffer *rf; 1993 1994 rf = malloc(sizeof(*rf), M_DEVBUF, M_WAITOK); 1995 rf->rf_base = base; 1996 rf->rf_size = size; 1997 rf->rf_dev = dev; 1998 1999 SLIST_INSERT_HEAD(&rasops_framebuffers, rf, rf_list); 2000 } 2001 2002 int 2003 rasops_check_framebuffer(paddr_t base) 2004 { 2005 struct rasops_framebuffer *rf; 2006 2007 SLIST_FOREACH(rf, &rasops_framebuffers, rf_list) { 2008 if (base >= rf->rf_base && base < rf->rf_base + rf->rf_size) 2009 return 1; 2010 } 2011 2012 return 0; 2013 } 2014