1 /* $NetBSD: rasops4.c,v 1.10 2010/05/04 04:57:34 macallan Exp $ */ 2 3 /*- 4 * Copyright (c) 1999 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Andrew Doran. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: rasops4.c,v 1.10 2010/05/04 04:57:34 macallan Exp $"); 34 35 #include "opt_rasops.h" 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/time.h> 40 #include <machine/endian.h> 41 42 #include <dev/wscons/wsdisplayvar.h> 43 #include <dev/wscons/wsconsio.h> 44 #include <dev/rasops/rasops.h> 45 #include <dev/rasops/rasops_masks.h> 46 47 static void rasops4_copycols(void *, int, int, int, int); 48 static void rasops4_erasecols(void *, int, int, int, long); 49 static void rasops4_do_cursor(struct rasops_info *); 50 static void rasops4_putchar(void *, int, int col, u_int, long); 51 #ifndef RASOPS_SMALL 52 static void rasops4_putchar8(void *, int, int col, u_int, long); 53 static void rasops4_putchar12(void *, int, int col, u_int, long); 54 static void rasops4_putchar16(void *, int, int col, u_int, long); 55 static void rasops4_makestamp(struct rasops_info *, long); 56 57 /* 58 * 4x1 stamp for optimized character blitting 59 */ 60 static u_int16_t stamp[16]; 61 static long stamp_attr; 62 static int stamp_mutex; /* XXX see note in README */ 63 #endif 64 65 /* 66 * Initialize rasops_info struct for this colordepth. 67 */ 68 void 69 rasops4_init(struct rasops_info *ri) 70 { 71 72 switch (ri->ri_font->fontwidth) { 73 #ifndef RASOPS_SMALL 74 case 8: 75 ri->ri_ops.putchar = rasops4_putchar8; 76 break; 77 case 12: 78 ri->ri_ops.putchar = rasops4_putchar12; 79 break; 80 case 16: 81 ri->ri_ops.putchar = rasops4_putchar16; 82 break; 83 #endif /* !RASOPS_SMALL */ 84 default: 85 panic("fontwidth not 8/12/16 or RASOPS_SMALL - fixme!"); 86 ri->ri_ops.putchar = rasops4_putchar; 87 break; 88 } 89 90 if ((ri->ri_font->fontwidth & 1) != 0) { 91 ri->ri_ops.erasecols = rasops4_erasecols; 92 ri->ri_ops.copycols = rasops4_copycols; 93 ri->ri_do_cursor = rasops4_do_cursor; 94 } 95 } 96 97 #ifdef notyet 98 /* 99 * Paint a single character. This is the generic version, this is ugly. 100 */ 101 static void 102 rasops4_putchar(void *cookie, int row, int col, u_int uc, long attr) 103 { 104 int height, width, fs, rs, fb, bg, fg, lmask, rmask; 105 struct rasops_info *ri = (struct rasops_info *)cookie; 106 struct wsdisplay_font *font = PICK_FONT(ri, uc); 107 int32_t *rp; 108 u_char *fr; 109 110 #ifdef RASOPS_CLIPPING 111 /* Catches 'row < 0' case too */ 112 if ((unsigned)row >= (unsigned)ri->ri_rows) 113 return; 114 115 if ((unsigned)col >= (unsigned)ri->ri_cols) 116 return; 117 #endif 118 119 width = font->fontwidth << 1; 120 height = font->fontheight; 121 col *= width; 122 rp = (int32_t *)(ri->ri_bits + row * ri->ri_yscale + ((col >> 3) & ~3)); 123 col = col & 31; 124 rs = ri->ri_stride; 125 126 bg = ri->ri_devcmap[(attr >> 16) & 0xf]; 127 fg = ri->ri_devcmap[(attr >> 24) & 0xf]; 128 129 /* If fg and bg match this becomes a space character */ 130 if (fg == bg || uc == ' ') { 131 uc = (u_int)-1; 132 fr = 0; /* shutup gcc */ 133 fs = 0; /* shutup gcc */ 134 } else { 135 uc -= font->firstchar; 136 fr = (u_char *)font->data + uc * ri->ri_fontscale; 137 fs = font->stride; 138 } 139 140 /* Single word, one mask */ 141 if ((col + width) <= 32) { 142 rmask = rasops_pmask[col][width]; 143 lmask = ~rmask; 144 145 if (uc == (u_int)-1) { 146 bg &= rmask; 147 148 while (height--) { 149 *rp = (*rp & lmask) | bg; 150 DELTA(rp, rs, int32_t *); 151 } 152 } else { 153 while (height--) { 154 /* get bits, mask */ 155 /* compose sl */ 156 /* mask sl */ 157 /* put word */ 158 } 159 } 160 161 /* Do underline */ 162 if (attr & 1) { 163 DELTA(rp, -(ri->ri_stride << 1), int32_t *); 164 *rp = (*rp & lmask) | (fg & rmask); 165 } 166 } else { 167 lmask = ~rasops_lmask[col]; 168 rmask = ~rasops_rmask[(col + width) & 31]; 169 170 if (uc == (u_int)-1) { 171 bg = bg & ~lmask; 172 width = bg & ~rmask; 173 174 while (height--) { 175 rp[0] = (rp[0] & lmask) | bg; 176 rp[1] = (rp[1] & rmask) | width; 177 DELTA(rp, rs, int32_t *); 178 } 179 } else { 180 width = 32 - col; 181 182 /* NOT fontbits if bg is white */ 183 while (height--) { 184 fb = ~(fr[3] | (fr[2] << 8) | 185 (fr[1] << 16) | (fr[0] << 24)); 186 187 rp[0] = (rp[0] & lmask) 188 | MBE((u_int)fb >> col); 189 190 rp[1] = (rp[1] & rmask) 191 | (MBE((u_int)fb << width) & ~rmask); 192 193 fr += fs; 194 DELTA(rp, rs, int32_t *); 195 } 196 } 197 198 /* Do underline */ 199 if (attr & 1) { 200 DELTA(rp, -(ri->ri_stride << 1), int32_t *); 201 rp[0] = (rp[0] & lmask) | (fg & ~lmask); 202 rp[1] = (rp[1] & rmask) | (fg & ~rmask); 203 } 204 } 205 } 206 #endif 207 208 /* 209 * Put a single character. This is the generic version. 210 */ 211 static void 212 rasops4_putchar(void *cookie, int row, int col, u_int uc, long attr) 213 { 214 215 /* XXX punt */ 216 } 217 218 #ifndef RASOPS_SMALL 219 /* 220 * Recompute the blitting stamp. 221 */ 222 static void 223 rasops4_makestamp(struct rasops_info *ri, long attr) 224 { 225 int i, fg, bg; 226 227 fg = ri->ri_devcmap[(attr >> 24) & 0xf] & 0xf; 228 bg = ri->ri_devcmap[(attr >> 16) & 0xf] & 0xf; 229 stamp_attr = attr; 230 231 for (i = 0; i < 16; i++) { 232 stamp[i] = (i & 1 ? fg : bg) << 8; 233 stamp[i] |= (i & 2 ? fg : bg) << 12; 234 stamp[i] |= (i & 4 ? fg : bg) << 0; 235 stamp[i] |= (i & 8 ? fg : bg) << 4; 236 } 237 } 238 239 /* 240 * Put a single character. This is for 8-pixel wide fonts. 241 */ 242 static void 243 rasops4_putchar8(void *cookie, int row, int col, u_int uc, long attr) 244 { 245 struct rasops_info *ri = (struct rasops_info *)cookie; 246 struct wsdisplay_font *font = PICK_FONT(ri, uc); 247 int height, fs, rs; 248 u_char *fr; 249 u_int16_t *rp; 250 251 /* Can't risk remaking the stamp if it's already in use */ 252 if (stamp_mutex++) { 253 stamp_mutex--; 254 rasops4_putchar(cookie, row, col, uc, attr); 255 return; 256 } 257 258 #ifdef RASOPS_CLIPPING 259 /* Catches 'row < 0' case too */ 260 if ((unsigned)row >= (unsigned)ri->ri_rows) { 261 stamp_mutex--; 262 return; 263 } 264 265 if ((unsigned)col >= (unsigned)ri->ri_cols) { 266 stamp_mutex--; 267 return; 268 } 269 #endif 270 271 rp = (u_int16_t *)(ri->ri_bits + row * ri->ri_yscale + col * ri->ri_xscale); 272 height = font->fontheight; 273 rs = ri->ri_stride / sizeof(*rp); 274 275 /* Recompute stamp? */ 276 if (attr != stamp_attr) 277 rasops4_makestamp(ri, attr); 278 279 if (uc == ' ') { 280 u_int16_t c = stamp[0]; 281 while (height--) { 282 rp[0] = c; 283 rp[1] = c; 284 rp += rs; 285 } 286 } else { 287 uc -= font->firstchar; 288 fr = (u_char *)font->data + uc * ri->ri_fontscale; 289 fs = font->stride; 290 291 while (height--) { 292 rp[0] = stamp[(*fr >> 4) & 0xf]; 293 rp[1] = stamp[*fr & 0xf]; 294 fr += fs; 295 rp += rs; 296 } 297 } 298 299 /* Do underline */ 300 if ((attr & 1) != 0) { 301 rp -= (rs << 1); 302 rp[0] = stamp[15]; 303 rp[1] = stamp[15]; 304 } 305 306 stamp_mutex--; 307 } 308 309 /* 310 * Put a single character. This is for 12-pixel wide fonts. 311 */ 312 static void 313 rasops4_putchar12(void *cookie, int row, int col, u_int uc, long attr) 314 { 315 struct rasops_info *ri = (struct rasops_info *)cookie; 316 struct wsdisplay_font *font = PICK_FONT(ri, uc); 317 int height, fs, rs; 318 u_char *fr; 319 u_int16_t *rp; 320 321 /* Can't risk remaking the stamp if it's already in use */ 322 if (stamp_mutex++) { 323 stamp_mutex--; 324 rasops4_putchar(cookie, row, col, uc, attr); 325 return; 326 } 327 328 #ifdef RASOPS_CLIPPING 329 /* Catches 'row < 0' case too */ 330 if ((unsigned)row >= (unsigned)ri->ri_rows) { 331 stamp_mutex--; 332 return; 333 } 334 335 if ((unsigned)col >= (unsigned)ri->ri_cols) { 336 stamp_mutex--; 337 return; 338 } 339 #endif 340 341 rp = (u_int16_t *)(ri->ri_bits + row * ri->ri_yscale + col * ri->ri_xscale); 342 height = font->fontheight; 343 rs = ri->ri_stride / sizeof(*rp); 344 345 /* Recompute stamp? */ 346 if (attr != stamp_attr) 347 rasops4_makestamp(ri, attr); 348 349 if (uc == ' ') { 350 u_int16_t c = stamp[0]; 351 while (height--) { 352 rp[0] = c; 353 rp[1] = c; 354 rp[2] = c; 355 rp += rs; 356 } 357 } else { 358 uc -= font->firstchar; 359 fr = (u_char *)font->data + uc * ri->ri_fontscale; 360 fs = font->stride; 361 362 while (height--) { 363 rp[0] = stamp[(fr[0] >> 4) & 0xf]; 364 rp[1] = stamp[fr[0] & 0xf]; 365 rp[2] = stamp[(fr[1] >> 4) & 0xf]; 366 fr += fs; 367 rp += rs; 368 } 369 } 370 371 /* Do underline */ 372 if ((attr & 1) != 0) { 373 rp -= (rs << 1); 374 rp[0] = stamp[15]; 375 rp[1] = stamp[15]; 376 rp[2] = stamp[15]; 377 } 378 379 stamp_mutex--; 380 } 381 382 /* 383 * Put a single character. This is for 16-pixel wide fonts. 384 */ 385 static void 386 rasops4_putchar16(void *cookie, int row, int col, u_int uc, long attr) 387 { 388 struct rasops_info *ri = (struct rasops_info *)cookie; 389 struct wsdisplay_font *font = PICK_FONT(ri, uc); 390 int height, fs, rs; 391 u_char *fr; 392 u_int16_t *rp; 393 394 /* Can't risk remaking the stamp if it's already in use */ 395 if (stamp_mutex++) { 396 stamp_mutex--; 397 rasops4_putchar(cookie, row, col, uc, attr); 398 return; 399 } 400 401 #ifdef RASOPS_CLIPPING 402 /* Catches 'row < 0' case too */ 403 if ((unsigned)row >= (unsigned)ri->ri_rows) { 404 stamp_mutex--; 405 return; 406 } 407 408 if ((unsigned)col >= (unsigned)ri->ri_cols) { 409 stamp_mutex--; 410 return; 411 } 412 #endif 413 414 rp = (u_int16_t *)(ri->ri_bits + row * ri->ri_yscale + col * ri->ri_xscale); 415 height = font->fontheight; 416 rs = ri->ri_stride / sizeof(*rp); 417 418 /* Recompute stamp? */ 419 if (attr != stamp_attr) 420 rasops4_makestamp(ri, attr); 421 422 if (uc == ' ') { 423 u_int16_t c = stamp[0]; 424 while (height--) { 425 rp[0] = c; 426 rp[1] = c; 427 rp[2] = c; 428 rp[3] = c; 429 rp += rs; 430 } 431 } else { 432 uc -= font->firstchar; 433 fr = (u_char *)font->data + uc * ri->ri_fontscale; 434 fs = font->stride; 435 436 while (height--) { 437 rp[0] = stamp[(fr[0] >> 4) & 0xf]; 438 rp[1] = stamp[fr[0] & 0xf]; 439 rp[2] = stamp[(fr[1] >> 4) & 0xf]; 440 rp[3] = stamp[fr[1] & 0xf]; 441 fr += fs; 442 rp += rs; 443 } 444 } 445 446 /* Do underline */ 447 if ((attr & 1) != 0) { 448 rp -= (rs << 1); 449 rp[0] = stamp[15]; 450 rp[1] = stamp[15]; 451 rp[2] = stamp[15]; 452 rp[3] = stamp[15]; 453 } 454 455 stamp_mutex--; 456 } 457 #endif /* !RASOPS_SMALL */ 458 459 /* 460 * Grab routines common to depths where (bpp < 8) 461 */ 462 #define NAME(ident) rasops4_##ident 463 #define PIXEL_SHIFT 3 464 465 #include <dev/rasops/rasops_bitops.h> 466