#ifdef _KERNEL_OPT
#include "opt_glyphcache.h"
#endif
#include <sys/systm.h>
#include <sys/atomic.h>
#include <sys/errno.h>
#include <sys/kmem.h>
#include <dev/wscons/wsdisplayvar.h>
#include <dev/rasops/rasops.h>
#include <dev/wscons/wsdisplay_vconsvar.h>
#include <dev/wscons/wsdisplay_glyphcachevar.h>
#ifdef GLYPHCACHE_DEBUG
#define DPRINTF aprint_normal
#else
#define DPRINTF while (0) printf
#endif
#define NBUCKETS 32
static inline int
attr2idx(long attr)
{
return (((attr >> 16) & 0x0f) | ((attr >> 20) & 0xf0));
}
int
glyphcache_init(glyphcache *gc, int first, int lines, int width,
int cellwidth, int cellheight, long attr)
{
return glyphcache_init_align(gc, first, lines, width, cellwidth, cellheight,
attr, 0);
}
int
glyphcache_init_align(glyphcache *gc, int first, int lines, int width,
int cellwidth, int cellheight, long attr, int alignment)
{
if (lines < 0) lines = 0;
gc->gc_width = width;
gc->gc_cellwidth = -1;
gc->gc_cellheight = -1;
gc->gc_firstline = first;
gc->gc_firstcol = 0;
gc->gc_lines = lines;
gc->gc_cellalign = alignment;
gc->gc_buckets = NULL;
gc->gc_numbuckets = 0;
gc->gc_buckets = kmem_alloc(sizeof(*gc->gc_buckets) * NBUCKETS,
KM_SLEEP);
gc->gc_nbuckets = NBUCKETS;
return glyphcache_reconfig(gc, cellwidth, cellheight, attr);
}
int
glyphcache_init_x(glyphcache *gc, int x, int y, int lines, int width,
int cellwidth, int cellheight, long attr)
{
if (lines < 0) lines = 0;
gc->gc_width = width;
gc->gc_cellwidth = -1;
gc->gc_cellheight = -1;
gc->gc_firstline = y;
gc->gc_firstcol = x;
gc->gc_lines = lines;
gc->gc_cellalign = 0;
gc->gc_buckets = NULL;
gc->gc_numbuckets = 0;
gc->gc_buckets = kmem_alloc(sizeof(*gc->gc_buckets) * NBUCKETS,
KM_SLEEP);
gc->gc_nbuckets = NBUCKETS;
return glyphcache_reconfig(gc, cellwidth, cellheight, attr);
}
int
glyphcache_reconfig(glyphcache *gc, int cellwidth, int cellheight, long attr)
{
int cache_lines, buckets, i, usedcells = 0, idx;
gc_bucket *b;
if ((gc->gc_cellwidth == cellwidth) &&
(gc->gc_cellheight == cellheight) &&
((gc->gc_buckets != NULL) &&
(gc->gc_buckets[0].gb_index == attr2idx(attr)))) {
return 0;
}
gc->gc_cellwidth = cellwidth;
if (gc->gc_cellalign != 0) {
gc->gc_cellstride =
(gc->gc_cellwidth + gc->gc_cellalign - 1) &
~(gc->gc_cellalign - 1);
} else
gc->gc_cellstride = cellwidth;
gc->gc_cellheight = cellheight;
gc->gc_cellsperline = gc->gc_width / gc->gc_cellstride;
cache_lines = gc->gc_lines / cellheight;
gc->gc_numcells = cache_lines * gc->gc_cellsperline;
buckets = (gc->gc_numcells / 223);
if ((buckets * 223) < gc->gc_numcells)
buckets++;
if (buckets < 1)
return ENOMEM;
buckets = uimin(buckets, gc->gc_nbuckets);
gc->gc_numbuckets = buckets;
DPRINTF("%s: using %d buckets\n", __func__, buckets);
for (i = 0; i < buckets; i++) {
b = &gc->gc_buckets[i];
b->gb_firstcell = usedcells;
b->gb_numcells = uimin(223, gc->gc_numcells - usedcells);
usedcells += 223;
b->gb_usedcells = 0;
b->gb_index = -1;
}
for (i = 0; i < 256; i++) {
gc->gc_attrmap[i] = -1;
}
idx = attr2idx(attr);
if (idx >= 0) {
gc->gc_attrmap[idx] = 0;
gc->gc_buckets[0].gb_index = idx;
}
glyphcache_wipe(gc);
DPRINTF("%s: using %d cells total, from %d width %d\n", __func__,
gc->gc_numcells, gc->gc_firstline, gc->gc_cellsperline);
DPRINTF("%s: cell size %d x %d, stride %d\n", __func__,
gc->gc_cellwidth, gc->gc_cellheight, gc->gc_cellstride);
return 0;
}
void
glyphcache_adapt(struct vcons_screen *scr, void *cookie)
{
glyphcache *gc = cookie;
struct rasops_info *ri = &scr->scr_ri;
if (ri->ri_wsfcookie != gc->gc_fontcookie) {
glyphcache_wipe(gc);
gc->gc_fontcookie = ri->ri_wsfcookie;
}
glyphcache_reconfig(gc, ri->ri_font->fontwidth,
ri->ri_font->fontheight, scr->scr_defattr);
}
void
glyphcache_wipe(glyphcache *gc)
{
gc_bucket *b;
int i, j, idx;
if ((gc->gc_buckets == NULL) || (gc->gc_numbuckets < 1))
return;
idx = gc->gc_buckets[0].gb_index;
for (i = 0; i < gc->gc_numbuckets; i++) {
b = &gc->gc_buckets[i];
b->gb_usedcells = 0;
b->gb_index = -1;
for (j = 0; j < b->gb_numcells; j++)
b->gb_map[j] = -1;
}
for (i = 0; i < 256; i++) {
gc->gc_attrmap[i] = -1;
}
gc->gc_attrmap[idx] = 0;
gc->gc_buckets[0].gb_index = idx;
}
int
glyphcache_add(glyphcache *gc, int c, int x, int y)
{
gc_bucket *b = gc->gc_next;
int cell;
int cx, cy;
if (b->gb_usedcells >= b->gb_numcells)
return ENOMEM;
cell = atomic_add_int_nv(&b->gb_usedcells, 1) - 1;
cell += b->gb_firstcell;
cy = gc->gc_firstline +
(cell / gc->gc_cellsperline) * gc->gc_cellheight;
cx = gc->gc_firstcol +
(cell % gc->gc_cellsperline) * gc->gc_cellstride;
b->gb_map[c - 33] = (cx << 16) | cy;
gc->gc_bitblt(gc->gc_blitcookie, x, y, cx, cy,
gc->gc_cellwidth, gc->gc_cellheight, gc->gc_rop);
if (gc->gc_underline & WSATTR_UNDERLINE) {
glyphcache_underline(gc, x, y, gc->gc_underline);
}
return 0;
}
void
glyphcache_underline(glyphcache *gc, int x, int y, long attr)
{
if (gc->gc_rectfill == NULL)
return;
gc->gc_rectfill(gc->gc_blitcookie, x, y + gc->gc_cellheight - 2,
gc->gc_cellwidth, 1, attr);
}
int
glyphcache_try(glyphcache *gc, int c, int x, int y, long attr)
{
int cell, cx, cy, idx, bi;
gc_bucket *b;
idx = attr2idx(attr);
if ((c < 33) || (c > 255) || (idx < 0))
return GC_NOPE;
bi = gc->gc_attrmap[idx];
if (bi == -1) {
bi = 1;
while ((bi < gc->gc_numbuckets) &&
(gc->gc_buckets[bi].gb_index != -1)) {
bi++;
}
if (bi < gc->gc_numbuckets) {
gc->gc_attrmap[idx] = bi;
b = &gc->gc_buckets[bi];
b->gb_index = idx;
b->gb_usedcells = 0;
b->gb_lastread = time_uptime;
} else {
time_t moo = time_uptime;
int i, oldest = 1;
for (i = 1; i < gc->gc_numbuckets; i++) {
if (gc->gc_buckets[i].gb_lastread < moo) {
oldest = i;
moo = gc->gc_buckets[i].gb_lastread;
}
}
b = &gc->gc_buckets[oldest];
gc->gc_attrmap[b->gb_index] = -1;
b->gb_index = idx;
b->gb_usedcells = 0;
gc->gc_attrmap[idx] = oldest;
for (i = 0; i < b->gb_numcells; i++)
b->gb_map[i] = -1;
b->gb_lastread = time_uptime;
}
} else {
b = &gc->gc_buckets[bi];
}
if (b->gb_usedcells >= b->gb_numcells)
return GC_NOPE;
cell = b->gb_map[c - 33];
if (cell == -1) {
gc->gc_next = b;
gc->gc_underline = attr;
return GC_ADD;
}
cy = cell & 0xffff;
cx = (cell >> 16) & 0xffff;
gc->gc_bitblt(gc->gc_blitcookie, cx, cy, x, y,
gc->gc_cellwidth, gc->gc_cellheight, gc->gc_rop);
if (attr & WSATTR_UNDERLINE)
glyphcache_underline(gc, x, y, attr);
b->gb_lastread = time_uptime;
return GC_OK;
}