Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/dev/wscons allow caching of glyphs with attributes other...



details:   https://anonhg.NetBSD.org/src/rev/77966519945c
branches:  trunk
changeset: 781860:77966519945c
user:      macallan <macallan%NetBSD.org@localhost>
date:      Thu Oct 04 10:26:32 2012 +0000

description:
allow caching of glyphs with attributes other than the default
while there, also add support for underlined characters

diffstat:

 sys/dev/wscons/wsdisplay_glyphcache.c    |  186 +++++++++++++++++++++++++++---
 sys/dev/wscons/wsdisplay_glyphcachevar.h |   32 ++++-
 2 files changed, 192 insertions(+), 26 deletions(-)

diffs (truncated from 320 to 300 lines):

diff -r c8567cb0b5b1 -r 77966519945c sys/dev/wscons/wsdisplay_glyphcache.c
--- a/sys/dev/wscons/wsdisplay_glyphcache.c     Thu Oct 04 10:22:45 2012 +0000
+++ b/sys/dev/wscons/wsdisplay_glyphcache.c     Thu Oct 04 10:26:32 2012 +0000
@@ -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 @@
        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;
+
+       idx = gc->gc_buckets[0].gb_index;
 
-       gc->gc_usedcells = 0;
-       for (i = 0; i < 256; i++)
-               gc->gc_map[i] = -1;
+       /* 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 @@
 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 @@
 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;
 }
diff -r c8567cb0b5b1 -r 77966519945c sys/dev/wscons/wsdisplay_glyphcachevar.h
--- a/sys/dev/wscons/wsdisplay_glyphcachevar.h  Thu Oct 04 10:22:45 2012 +0000
+++ b/sys/dev/wscons/wsdisplay_glyphcachevar.h  Thu Oct 04 10:26:32 2012 +0000
@@ -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;
+       /* geometry */
        int gc_numcells;
-       uint32_t gc_map[256];
-       /* geometry */
        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 */
        /*



Home | Main Index | Thread Index | Old Index