allow caching of glyphs with attributes other than the default

while there, also add support for underlined characters
This commit is contained in:
macallan 2012-10-04 10:26:32 +00:00
parent 5e521e6304
commit f952894398
2 changed files with 192 additions and 26 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: wsdisplay_glyphcache.c,v 1.3 2012/07/12 01:21:08 macallan Exp $ */
/* $NetBSD: wsdisplay_glyphcache.c,v 1.4 2012/10/04 10:26:32 macallan Exp $ */
/*
* Copyright (c) 2012 Michael Lorenz
@ -31,18 +31,38 @@
* the most commonly used glyphs ) but the API should at least not prevent
* more sophisticated caching algorithms
*/
#include <sys/systm.h>
#include <sys/atomic.h>
#include <sys/errno.h>
#include <sys/kmem.h>
#include <dev/wscons/wsdisplay_glyphcachevar.h>
#include "opt_glyphcache.h"
#ifdef GLYPHCACHE_DEBUG
#define DPRINTF aprint_normal
#else
#define DPRINTF while (0) printf
#endif
static inline int
attr2idx(long attr)
{
if ((attr & 0xf0f0fff8) != 0)
return -1;
return (((attr >> 16) & 0x0f) | ((attr >> 20) & 0xf0));
}
/* first line, lines, width, attr */
int
glyphcache_init(glyphcache *gc, int first, int lines, int width,
int cellwidth, int cellheight, long attr)
{
int cache_lines;
int cache_lines, buckets, i, usedcells = 0, idx;
gc_bucket *b;
/* first the geometry stuff */
gc->gc_cellwidth = cellwidth;
gc->gc_cellheight = cellheight;
gc->gc_firstline = first;
@ -50,21 +70,70 @@ glyphcache_init(glyphcache *gc, int first, int lines, int width,
if (lines < 0) lines = 0;
cache_lines = lines / cellheight;
gc->gc_numcells = cache_lines * gc->gc_cellsperline;
if (gc->gc_numcells > 256)
gc->gc_numcells = 256;
gc->gc_attr = attr;
/* now allocate buckets */
buckets = (gc->gc_numcells / 223);
if ((buckets * 223) < gc->gc_numcells)
buckets++;
gc->gc_buckets = kmem_alloc(sizeof(gc_bucket) * buckets, KM_SLEEP);
if (gc->gc_buckets == NULL) {
aprint_error("%s: can't allocate memory\n", __func__);
return ENOMEM;
}
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 = min(223, gc->gc_numcells - usedcells);
usedcells += 223;
b->gb_usedcells = 0;
b->gb_index = -1;
}
/* initialize the attribute map... */
for (i = 0; i < 256; i++) {
gc->gc_attrmap[i] = -1;
}
/* first bucket goes to default attr */
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);
return 0;
}
void
glyphcache_wipe(glyphcache *gc)
{
int i;
gc_bucket *b;
int i, j, idx;
gc->gc_usedcells = 0;
for (i = 0; i < 256; i++)
gc->gc_map[i] = -1;
idx = gc->gc_buckets[0].gb_index;
/* empty all the buckets */
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;
}
/* now put the first bucket back where it was */
gc->gc_attrmap[idx] = 0;
gc->gc_buckets[0].gb_index = idx;
}
/*
@ -75,23 +144,35 @@ glyphcache_wipe(glyphcache *gc)
int
glyphcache_add(glyphcache *gc, int c, int x, int y)
{
gc_bucket *b = gc->gc_next;
int cell;
int cx, cy;
if (gc->gc_map[c] != -1)
return EINVAL;
if (gc->gc_usedcells >= gc->gc_numcells)
if (b->gb_usedcells >= b->gb_numcells)
return ENOMEM;
cell = atomic_add_int_nv(&gc->gc_usedcells, 1) - 1;
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 = (cell % gc->gc_cellsperline) * gc->gc_cellwidth;
gc->gc_map[c] = (cx << 16) | cy;
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 & 1) {
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);
}
/*
* check if (c) is in the cache, if so draw it at (x,y)
* return:
@ -102,17 +183,82 @@ glyphcache_add(glyphcache *gc, int c, int x, int y)
int
glyphcache_try(glyphcache *gc, int c, int x, int y, long attr)
{
int cell, cx, cy;
if ((c < 0) || (c > 255) || (attr != gc->gc_attr))
int cell, cx, cy, idx, bi;
gc_bucket *b;
idx = attr2idx(attr);
/* see if we're in range */
if ((c < 33) || (c > 255) || (idx < 0))
return GC_NOPE;
if (gc->gc_usedcells >= gc->gc_numcells)
/* see if there's already a bucket for this attribute */
bi = gc->gc_attrmap[idx];
if (bi == -1) {
/* nope, see if there's an empty one left */
bi = 1;
while ((bi < gc->gc_numbuckets) &&
(gc->gc_buckets[bi].gb_index != -1)) {
bi++;
}
if (bi < gc->gc_numbuckets) {
/* found one -> grab it */
gc->gc_attrmap[idx] = bi;
b = &gc->gc_buckets[bi];
b->gb_index = idx;
b->gb_usedcells = 0;
/* make sure this doesn't get evicted right away */
b->gb_lastread = time_uptime;
} else {
/*
* still nothing
* steal the least recently read bucket
*/
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;
}
}
/* if we end up here all buckets must be in use */
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;
/* now scrub it */
for (i = 0; i < b->gb_numcells; i++)
b->gb_map[i] = -1;
/* and set the time stamp */
b->gb_lastread = time_uptime;
}
} else {
/* found one */
b = &gc->gc_buckets[bi];
}
/* see if there's room in the bucket */
if (b->gb_usedcells >= b->gb_numcells)
return GC_NOPE;
cell = gc->gc_map[c];
if (cell == -1)
cell = b->gb_map[c - 33];
if (cell == -1) {
gc->gc_next = b;
gc->gc_underline = attr;
return GC_ADD;
}
/* it's in the cache - draw it */
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);
/* and underline it if needed */
if (attr & 1)
glyphcache_underline(gc, x, y, attr);
/* update bucket's time stamp */
b->gb_lastread = time_uptime;
return GC_OK;
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: wsdisplay_glyphcachevar.h,v 1.3 2012/04/22 03:47:53 uebayasi Exp $ */
/* $NetBSD: wsdisplay_glyphcachevar.h,v 1.4 2012/10/04 10:26:32 macallan Exp $ */
/*
* Copyright (c) 2012 Michael Lorenz
@ -30,22 +30,36 @@
#ifndef WSDISPLAY_GLYPHCACHEVAR_H
#define WSDISPLAY_GLYPHCACHEVAR_H
#include <sys/time.h>
typedef struct _gc_bucket {
int gb_firstcell;
int gb_numcells;
volatile unsigned int gb_usedcells;
int gb_index; /* -1 for unused buckets */
uint32_t gb_map[223]; /* we only care about char codes 0x21 and up */
time_t gb_lastread;
} gc_bucket;
typedef struct _glyphcache {
/* mapping char codes to cache cells */
volatile unsigned int gc_usedcells;
int gc_numcells;
uint32_t gc_map[256];
/* geometry */
int gc_numcells;
int gc_cellwidth;
int gc_cellheight;
int gc_cellsperline;
int gc_firstline; /* first line in vram to use for glyphs */
long gc_attr;
/* buckets */
int gc_numbuckets;
gc_bucket *gc_buckets; /* we allocate as many as we can get into vram */
gc_bucket *gc_next; /* bucket the next glyph goes into */
long gc_underline; /* draw an underline in glyphcache_add() */
int gc_attrmap[256]; /* mapping a colour attribute to a bucket */
/*
* method to copy glyphs within vram,
* to be initialized before calling glyphcache_init()
*/
void (*gc_bitblt)(void *, int, int, int, int, int, int, int);
void (*gc_rectfill)(void *, int, int, int, int, long);
void *gc_blitcookie;
int gc_rop;
} glyphcache;
@ -61,5 +75,11 @@ int glyphcache_try(glyphcache *, int, int, int, long); /* char code, x, y, attr
#define GC_OK 0 /* glyph was in cache and has been drawn */
#define GC_ADD 1 /* glyph is not in cache but can be added */
#define GC_NOPE 2 /* glyph is not in cache and can't be added */
/*
* draw an underline in the given attribute
* must be called by the driver if glyphcache_try() returns GC_NOPE and for
* blanks
*/
void glyphcache_underline(glyphcache *, int, int, long);
#endif /* WSDISPLAY_GLYPHCACHEVAR_H */