/*-
* Copyright (c) 1999 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Andrew Doran.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/* ANSI colormap (R,G,B). Upper 8 are high-intensity */
const uint8_t rasops_cmap[256 * 3] = {
0x00, 0x00, 0x00, /* black */
0x7f, 0x00, 0x00, /* red */
0x00, 0x7f, 0x00, /* green */
0x7f, 0x7f, 0x00, /* brown */
0x00, 0x00, 0x7f, /* blue */
0x7f, 0x00, 0x7f, /* magenta */
0x00, 0x7f, 0x7f, /* cyan */
0xc7, 0xc7, 0xc7, /* white - XXX too dim? */
0x7f, 0x7f, 0x7f, /* black */
0xff, 0x00, 0x00, /* red */
0x00, 0xff, 0x00, /* green */
0xff, 0xff, 0x00, /* brown */
0x00, 0x00, 0xff, /* blue */
0xff, 0x00, 0xff, /* magenta */
0x00, 0xff, 0xff, /* cyan */
0xff, 0xff, 0xff, /* white */
/*
* For the cursor, we need at least the last (255th)
* color to be white. Fill up white completely for
* simplicity.
*/
#define _CMWHITE 0xff, 0xff, 0xff,
#define _CMWHITE16 _CMWHITE _CMWHITE _CMWHITE _CMWHITE \
_CMWHITE _CMWHITE _CMWHITE _CMWHITE \
_CMWHITE _CMWHITE _CMWHITE _CMWHITE \
_CMWHITE _CMWHITE _CMWHITE _CMWHITE
_CMWHITE16 _CMWHITE16 _CMWHITE16 _CMWHITE16 _CMWHITE16
_CMWHITE16 _CMWHITE16 _CMWHITE16 _CMWHITE16 _CMWHITE16
_CMWHITE16 _CMWHITE16 _CMWHITE16 _CMWHITE16 /* but not the last one */
#undef _CMWHITE16
#undef _CMWHITE
/*
* For the cursor the fg/bg indices are bit inverted, so
* provide complimentary colors in the upper 16 entries.
*/
0x7f, 0x7f, 0x7f, /* black */
0xff, 0x00, 0x00, /* red */
0x00, 0xff, 0x00, /* green */
0xff, 0xff, 0x00, /* brown */
0x00, 0x00, 0xff, /* blue */
0xff, 0x00, 0xff, /* magenta */
0x00, 0xff, 0xff, /* cyan */
0xff, 0xff, 0xff, /* white */
0x00, 0x00, 0x00, /* black */
0x7f, 0x00, 0x00, /* red */
0x00, 0x7f, 0x00, /* green */
0x7f, 0x7f, 0x00, /* brown */
0x00, 0x00, 0x7f, /* blue */
0x7f, 0x00, 0x7f, /* magenta */
0x00, 0x7f, 0x7f, /* cyan */
0xc7, 0xc7, 0xc7, /* white - XXX too dim? */
};
/* True if color is gray */
static const uint8_t rasops_isgray[16] = {
1, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 1,
};
#ifdef RASOPS_APPLE_PALETTE
/*
* Approximate ANSI colormap for legacy Apple color palettes
*/
static const uint8_t apple8_devcmap[16] = {
0xff, /* black 0x00, 0x00, 0x00 */
0x6b, /* red 0x99, 0x00, 0x00 */
0xc5, /* green 0x00, 0x99, 0x00 */
0x59, /* yellow 0x99, 0x99, 0x00 */
0xd4, /* blue 0x00, 0x00, 0x99 */
0x68, /* magenta 0x99, 0x00, 0x99 */
0xc2, /* cyan 0x00, 0x99, 0x99 */
0x2b, /* white 0xcc, 0xcc, 0xcc */
/*
* List of all rotated fonts
*/
SLIST_HEAD(, rotatedfont) rotatedfonts = SLIST_HEAD_INITIALIZER(rotatedfonts);
struct rotatedfont {
SLIST_ENTRY(rotatedfont) rf_next;
int rf_cookie;
int rf_rotated;
};
#endif /* NRASOPS_ROTATION > 0 */
/*
* Initialize a 'rasops_info' descriptor.
*/
int
rasops_init(struct rasops_info *ri, int wantrows, int wantcols)
{
memset(&ri->ri_optfont, 0, sizeof(ri->ri_optfont));
#ifdef _KERNEL
/* Select a font if the caller doesn't care */
if (ri->ri_font == NULL) {
int cookie = -1;
int flags;
wsfont_init();
/*
* first, try to find something that's as close as possible
* to the caller's requested terminal size
*/
if (wantrows == 0)
wantrows = RASOPS_DEFAULT_HEIGHT;
if (wantcols == 0)
wantcols = RASOPS_DEFAULT_WIDTH;
/*
* this means there is no supported font in the list
*/
if (cookie <= 0) {
aprint_error("%s: font table is empty\n", __func__);
return -1;
}
#if NRASOPS_ROTATION > 0
/*
* Pick the rotated version of this font. This will create it
* if necessary.
*/
if (ri->ri_flg & RI_ROTATE_MASK) {
if (ri->ri_flg & RI_ROTATE_CW)
rasops_rotate_font(&cookie, WSFONT_ROTATE_CW);
else if (ri->ri_flg & RI_ROTATE_CCW)
rasops_rotate_font(&cookie, WSFONT_ROTATE_CCW);
}
#endif
/* This should never happen in reality... */
if ((uintptr_t)ri->ri_bits & 3) {
aprint_error("%s: bits not aligned on 32-bit boundary\n",
__func__);
return -1;
}
if (ri->ri_stride & 3) {
aprint_error("%s: stride not aligned on 32-bit boundary\n",
__func__);
return -1;
}
if (rasops_reconfig(ri, wantrows, wantcols))
return -1;
rasops_init_devcmap(ri);
return 0;
}
/*
* Reconfigure (because parameters have changed in some way).
*/
int
rasops_reconfig(struct rasops_info *ri, int wantrows, int wantcols)
{
int bpp, height, s;
size_t len;
s = splhigh();
if (wantrows == 0)
wantrows = RASOPS_DEFAULT_HEIGHT;
if (wantcols == 0)
wantcols = RASOPS_DEFAULT_WIDTH;
/* throw away old line drawing character bitmaps, if we have any */
if (ri->ri_optfont.data != NULL) {
kmem_free(ri->ri_optfont.data, ri->ri_optfont.stride *
ri->ri_optfont.fontheight * ri->ri_optfont.numchars);
ri->ri_optfont.data = NULL;
}
/*
* Allocate a color attribute.
*/
static int
rasops_allocattr_color(void *cookie, int fg0, int bg0, int flg, long *attr)
{
uint32_t fg = fg0, bg = bg0;
if (__predict_false(fg >= sizeof(rasops_isgray) ||
bg >= sizeof(rasops_isgray)))
return EINVAL;
/*
* Allocate a mono attribute.
*/
static int
rasops_allocattr_mono(void *cookie, int fg0, int bg0, int flg, long *attr)
{
uint32_t fg = fg0, bg = bg0;
sp = ri->ri_bits + src;
dp = ri->ri_bits + dst;
if (ri->ri_hwbits)
hp = ri->ri_hwbits + dst;
while (num--) {
memcpy(dp, sp, ri->ri_emustride);
if (ri->ri_hwbits) {
memcpy(hp, dp, ri->ri_emustride);
hp += stride;
}
sp += stride;
dp += stride;
}
}
/*
* Copy columns. This is slow, and hard to optimize due to alignment,
* and the fact that we have to copy both left->right and right->left.
* We simply cop-out here and use memmove(), since it handles all of
* these cases anyway.
*/
static void
rasops_copycols(void *cookie, int row, int src, int dst, int num)
{
struct rasops_info *ri = (struct rasops_info *)cookie;
int height;
uint8_t *sp, *dp, *hp;
hp = NULL; /* XXX GCC */
if (__predict_false(dst == src))
return;
#ifdef RASOPS_CLIPPING
/* Catches < 0 case too */
if ((unsigned)row >= (unsigned)ri->ri_rows)
return;
if (src < 0) {
num += src;
src = 0;
}
if (src + num > ri->ri_cols)
num = ri->ri_cols - src;
if (dst < 0) {
num += dst;
dst = 0;
}
if (dst + num > ri->ri_cols)
num = ri->ri_cols - dst;
if (num <= 0)
return;
#endif
height = ri->ri_font->fontheight;
row *= ri->ri_yscale;
num *= ri->ri_xscale;
case 4:
for (i = 0; i < 16; i++) {
#ifdef RASOPS_APPLE_PALETTE
c = apple4_devcmap[i];
#else
c = i;
#endif
ri->ri_devcmap[i] =
(c << 0) | (c << 4) | (c << 8) | (c << 12) |
(c << 16) | (c << 20) | (c << 24) | (c << 28);
}
return;
case 8:
if ((ri->ri_flg & RI_8BIT_IS_RGB) == 0) {
for (i = 0; i < 16; i++) {
#ifdef RASOPS_APPLE_PALETTE
c = apple8_devcmap[i];
#else
c = i;
#endif
ri->ri_devcmap[i] =
c | (c << 8) | (c << 16) | (c << 24);
}
return;
}
}
p = rasops_cmap;
for (i = 0; i < 16; i++) {
if (ri->ri_rnum <= 8)
c = (uint32_t)(*p >> (8 - ri->ri_rnum)) << ri->ri_rpos;
else
c = (uint32_t)(*p << (ri->ri_rnum - 8)) << ri->ri_rpos;
p++;
if (ri->ri_gnum <= 8)
c |= (uint32_t)(*p >> (8 - ri->ri_gnum)) << ri->ri_gpos;
else
c |= (uint32_t)(*p << (ri->ri_gnum - 8)) << ri->ri_gpos;
p++;
if (ri->ri_bnum <= 8)
c |= (uint32_t)(*p >> (8 - ri->ri_bnum)) << ri->ri_bpos;
else
c |= (uint32_t)(*p << (ri->ri_bnum - 8)) << ri->ri_bpos;
p++;
/*
* Swap byte order if necessary. Then, fill the word for
* generic routines, which want this.
*/
switch (ri->ri_depth) {
case 8:
c |= c << 8;
c |= c << 16;
break;
case 15:
case 16:
if ((ri->ri_flg & RI_BSWAP) != 0)
c = bswap16(c);
c |= c << 16;
break;
case 24:
#if BYTE_ORDER == LITTLE_ENDIAN
if ((ri->ri_flg & RI_BSWAP) == 0)
#else
if ((ri->ri_flg & RI_BSWAP) != 0)
#endif
{
/*
* Convert to ``big endian'' if not RI_BSWAP.
*/
c = (c & 0x0000ff) << 16|
(c & 0x00ff00) |
(c & 0xff0000) >> 16;
}
/*
* No worries, we use generic routines only for
* gray colors, where all 3 bytes are same.
*/
c |= (c & 0xff) << 24;
break;
case 32:
if ((ri->ri_flg & RI_BSWAP) != 0)
c = bswap32(c);
break;
}
ri->ri_devcmap[i] = c;
}
}
/*
* Unpack a rasops attribute
*/
void
rasops_unpack_attr(long attr, int *fg, int *bg, int *underline)
{
/*
* Erase rows. This isn't static, since 24-bpp uses it in special cases.
*/
void
rasops_eraserows(void *cookie, int row, int num, long attr)
{
struct rasops_info *ri = (struct rasops_info *)cookie;
int bytes;
uint32_t bg, *rp, *hp;
hp = NULL; /* XXX GCC */
#ifdef RASOPS_CLIPPING
if (row < 0) {
num += row;
row = 0;
}
if (row + num > ri->ri_rows)
num = ri->ri_rows - row;
if (num <= 0)
return;
#endif
/*
* XXX The wsdisplay_emulops interface seems a little deficient in
* that there is no way to clear the *entire* screen. We provide a
* workaround here: if the entire console area is being cleared, and
* the RI_FULLCLEAR flag is set, clear the entire display.
*/
if (num == ri->ri_rows && (ri->ri_flg & RI_FULLCLEAR) != 0) {
bytes = ri->ri_stride;
num = ri->ri_height;
rp = (uint32_t *)ri->ri_origbits;
if (ri->ri_hwbits)
hp = (uint32_t *)ri->ri_hworigbits;
} else {
bytes = ri->ri_emustride;
num *= ri->ri_font->fontheight;
rp = (uint32_t *)(ri->ri_bits + row * ri->ri_yscale);
if (ri->ri_hwbits)
hp = (uint32_t *)(ri->ri_hwbits + row * ri->ri_yscale);
}
/*
* Actually turn the cursor on or off. This does the dirty work for
* rasops_cursor().
*/
static void
rasops_do_cursor(struct rasops_info *ri)
{
int row, col, height, slop1, slop2, full, cnt;
uint32_t mask1, mask2, *dp;
uint8_t tmp, *rp, *hp;
rp = ri->ri_bits + FBOFFSET(ri, row, col);
if (ri->ri_hwbits)
hp = ri->ri_hwbits + FBOFFSET(ri, row, col);
/*
* For ri_xscale = 1:
*
* Logic below does not work for ri_xscale = 1, e.g.,
* fontwidth = 8 and bpp = 1. So we take care of it.
*/
if (ri->ri_xscale == 1) {
while (height--) {
tmp = ~*rp;
*rp = tmp;
if (ri->ri_hwbits) {
*hp = tmp;
hp += ri->ri_stride;
}
rp += ri->ri_stride;
}
return;
}
/*
* For ri_xscale = 2, 3, 4, ...:
*
* Note that siop1 <= ri_xscale even for ri_xscale = 2,
* since rp % 3 = 0 or 2 (ri_stride % 4 = 0).
*/
slop1 = (4 - ((uintptr_t)rp & 3)) & 3;
slop2 = (ri->ri_xscale - slop1) & 3;
full = (ri->ri_xscale - slop1 /* - slop2 */) >> 2;
rp = (uint8_t *)((uintptr_t)rp & ~3);
hp = (uint8_t *)((uintptr_t)hp & ~3);
/* 0x00 would be empty anyway so don't bother */
for (c = 1; c < 16; c++) {
data += ri->ri_font->fontheight;
if (c & 1) {
/* upper segment */
for (i = 0; i < mid; i++)
data[i] = vert_mask;
}
if (c & 4) {
/* lower segment */
for (i = mid; i < ri->ri_font->fontheight; i++)
data[i] = vert_mask;
}
if (c & 2) {
/* right segment */
i = ri->ri_font->fontheight >> 1;
data[mid - 1] |= hmask_right;
data[mid] |= hmask_right;
}
if (c & 8) {
/* left segment */
data[mid - 1] |= hmask_left;
data[mid] |= hmask_left;
}
}
}
void
rasops_make_box_chars_alpha(struct rasops_info *ri)
{
int c, i, hmid, vmid, wi, he;
uint8_t *data = (uint8_t *)ri->ri_optfont.data;
uint8_t *ddata;
he = ri->ri_font->fontheight;
wi = ri->ri_font->fontwidth;
vmid = (he + 1) >> 1;
hmid = (wi + 1) >> 1;
/* 0x00 would be empty anyway so don't bother */
for (c = 1; c < 16; c++) {
data += ri->ri_fontscale;
if (c & 1) {
/* upper segment */
ddata = data + hmid;
for (i = 0; i <= vmid; i++) {
*ddata = 0xff;
ddata += wi;
}
}
if (c & 4) {
/* lower segment */
ddata = data + wi * vmid + hmid;
for (i = vmid; i < he; i++) {
*ddata = 0xff;
ddata += wi;
}
}
if (c & 2) {
/* right segment */
ddata = data + wi * vmid + hmid;
for (i = hmid; i < wi; i++) {
*ddata = 0xff;
ddata++;
}
}
if (c & 8) {
/* left segment */
ddata = data + wi * vmid;
for (i = 0; i <= hmid; i++) {
*ddata = 0xff;
ddata++;
}
}
}
}
/*
* Return a colour map appropriate for the given struct rasops_info in the
* same form used by rasops_cmap[]
* For now this is either a copy of rasops_cmap[] or an R3G3B2 map, it should
* probably be a linear ( or gamma corrected? ) ramp for higher depths.
*/
int
rasops_get_cmap(struct rasops_info *ri, uint8_t *palette, size_t bytes)
{
if ((ri->ri_depth == 8) && ((ri->ri_flg & RI_8BIT_IS_RGB) != 0)) {
/* generate an R3G3B2 palette */
int i, idx = 0;
uint8_t tmp;
if (bytes < 256 * 3)
return EINVAL;
for (i = 0; i < 256; i++) {
tmp = i & 0xe0;
/*
* replicate bits so 0xe0 maps to a red value of 0xff
* in order to make white look actually white
*/
tmp |= (tmp >> 3) | (tmp >> 6);
palette[idx] = tmp;
idx++;
static void
rasops_erasecols_rotated_cw(void *cookie, int row, int col, int num, long attr)
{
struct rasops_info *ri = (struct rasops_info *)cookie;
int i;
for (i = col; i < col + num; i++)
ri->ri_ops.putchar(cookie, row, i, ' ', attr);
}
/* XXX: these could likely be optimised somewhat. */
static void
rasops_copyrows_rotated_cw(void *cookie, int src, int dst, int num)
{
struct rasops_info *ri = (struct rasops_info *)cookie;
int col, roff;
if (src > dst)
for (roff = 0; roff < num; roff++)
for (col = 0; col < ri->ri_cols; col++)
rasops_copychar(cookie, src + roff, dst + roff,
col, col);
else
for (roff = num - 1; roff >= 0; roff--)
for (col = 0; col < ri->ri_cols; col++)
rasops_copychar(cookie, src + roff, dst + roff,
col, col);
}
static void
rasops_copycols_rotated_cw(void *cookie, int row, int src, int dst, int num)
{
int coff;
/* XXX: these could likely be optimised somewhat. */
static void
rasops_copyrows_rotated_ccw(void *cookie, int src, int dst, int num)
{
struct rasops_info *ri = (struct rasops_info *)cookie;
int col, roff;
if (src > dst)
for (roff = 0; roff < num; roff++)
for (col = 0; col < ri->ri_cols; col++)
rasops_copychar_ccw(cookie,
src + roff, dst + roff, col, col);
else
for (roff = num - 1; roff >= 0; roff--)
for (col = 0; col < ri->ri_cols; col++)
rasops_copychar_ccw(cookie,
src + roff, dst + roff, col, col);
}
static void
rasops_copycols_rotated_ccw(void *cookie, int row, int src, int dst, int num)
{
int coff;