/*
* Copyright (c) 2012 Michael Lorenz
* All rights reserved.
*
* 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 AUTHOR ``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 AUTHOR 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.
*/
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)
{
int
glyphcache_reconfig(glyphcache *gc, int cellwidth, int cellheight, long attr)
{
int cache_lines, buckets, i, usedcells = 0, idx;
gc_bucket *b;
/* see if we actually need to reconfigure anything */
if ((gc->gc_cellwidth == cellwidth) &&
(gc->gc_cellheight == cellheight) &&
((gc->gc_buckets != NULL) &&
(gc->gc_buckets[0].gb_index == attr2idx(attr)))) {
return 0;
}
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;
/* 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;
}
/*
* add a glyph drawn at (x,y) to the cache as (c)
* call this only if glyphcache_try() returned GC_ADD
* caller or gc_bitblt must make sure the glyph is actually completely drawn
*/
int
glyphcache_add(glyphcache *gc, int c, int x, int y)
{
gc_bucket *b = gc->gc_next;
int cell;
int cx, cy;
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:
* - GC_OK when the glyph was found
* - GC_ADD when the glyph wasn't found but can be added
* - GC_NOPE when the glyph can't be cached
*/
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);
/* see if we're in range */
if ((c < 33) || (c > 255) || (idx < 0))
return GC_NOPE;
/* 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;